4 * Copyright (C) 2009 Intel Corporation, Yu Zhao <yu.zhao@intel.com>
5 * Copyright (C) 2011 Advanced Micro Devices,
7 * PCI Express I/O Virtualization (IOV) support.
8 * Address Translation Service 1.0
9 * Page Request Interface added by Joerg Roedel <joerg.roedel@amd.com>
10 * PASID support added by Joerg Roedel <joerg.roedel@amd.com>
13 #include <linux/export.h>
14 #include <linux/pci-ats.h>
15 #include <linux/pci.h>
16 #include <linux/slab.h>
20 static void ats_alloc_one(struct pci_dev
*dev
)
25 pos
= pci_find_ext_capability(dev
, PCI_EXT_CAP_ID_ATS
);
30 pci_read_config_word(dev
, dev
->ats_cap
+ PCI_ATS_CAP
, &cap
);
31 dev
->ats_qdep
= PCI_ATS_CAP_QDEP(cap
) ? PCI_ATS_CAP_QDEP(cap
) :
35 void pci_ats_init(struct pci_dev
*dev
)
41 * pci_enable_ats - enable the ATS capability
42 * @dev: the PCI device
43 * @ps: the IOMMU page shift
45 * Returns 0 on success, or negative on failure.
47 int pci_enable_ats(struct pci_dev
*dev
, int ps
)
51 BUG_ON(dev
->ats_cap
&& dev
->ats_enabled
);
56 if (ps
< PCI_ATS_MIN_STU
)
60 * Note that enabling ATS on a VF fails unless it's already enabled
61 * with the same STU on the PF.
63 ctrl
= PCI_ATS_CTRL_ENABLE
;
65 struct pci_dev
*pdev
= dev
->physfn
;
67 if (pdev
->ats_stu
!= ps
)
70 atomic_inc(&pdev
->ats_ref_cnt
); /* count enabled VFs */
73 ctrl
|= PCI_ATS_CTRL_STU(dev
->ats_stu
- PCI_ATS_MIN_STU
);
75 pci_write_config_word(dev
, dev
->ats_cap
+ PCI_ATS_CTRL
, ctrl
);
80 EXPORT_SYMBOL_GPL(pci_enable_ats
);
83 * pci_disable_ats - disable the ATS capability
84 * @dev: the PCI device
86 void pci_disable_ats(struct pci_dev
*dev
)
90 BUG_ON(!dev
->ats_cap
|| !dev
->ats_enabled
);
92 if (atomic_read(&dev
->ats_ref_cnt
))
93 return; /* VFs still enabled */
96 struct pci_dev
*pdev
= dev
->physfn
;
98 atomic_dec(&pdev
->ats_ref_cnt
);
101 pci_read_config_word(dev
, dev
->ats_cap
+ PCI_ATS_CTRL
, &ctrl
);
102 ctrl
&= ~PCI_ATS_CTRL_ENABLE
;
103 pci_write_config_word(dev
, dev
->ats_cap
+ PCI_ATS_CTRL
, ctrl
);
105 dev
->ats_enabled
= 0;
107 EXPORT_SYMBOL_GPL(pci_disable_ats
);
109 void pci_restore_ats_state(struct pci_dev
*dev
)
113 if (!pci_ats_enabled(dev
))
115 if (!pci_find_ext_capability(dev
, PCI_EXT_CAP_ID_ATS
))
118 ctrl
= PCI_ATS_CTRL_ENABLE
;
120 ctrl
|= PCI_ATS_CTRL_STU(dev
->ats_stu
- PCI_ATS_MIN_STU
);
121 pci_write_config_word(dev
, dev
->ats_cap
+ PCI_ATS_CTRL
, ctrl
);
123 EXPORT_SYMBOL_GPL(pci_restore_ats_state
);
126 * pci_ats_queue_depth - query the ATS Invalidate Queue Depth
127 * @dev: the PCI device
129 * Returns the queue depth on success, or negative on failure.
131 * The ATS spec uses 0 in the Invalidate Queue Depth field to
132 * indicate that the function can accept 32 Invalidate Request.
133 * But here we use the `real' values (i.e. 1~32) for the Queue
134 * Depth; and 0 indicates the function shares the Queue with
135 * other functions (doesn't exclusively own a Queue).
137 int pci_ats_queue_depth(struct pci_dev
*dev
)
143 return dev
->ats_qdep
;
147 EXPORT_SYMBOL_GPL(pci_ats_queue_depth
);
149 #ifdef CONFIG_PCI_PRI
151 * pci_enable_pri - Enable PRI capability
152 * @ pdev: PCI device structure
154 * Returns 0 on success, negative value on error
156 int pci_enable_pri(struct pci_dev
*pdev
, u32 reqs
)
162 pos
= pci_find_ext_capability(pdev
, PCI_EXT_CAP_ID_PRI
);
166 pci_read_config_word(pdev
, pos
+ PCI_PRI_CTRL
, &control
);
167 pci_read_config_word(pdev
, pos
+ PCI_PRI_STATUS
, &status
);
168 if ((control
& PCI_PRI_CTRL_ENABLE
) ||
169 !(status
& PCI_PRI_STATUS_STOPPED
))
172 pci_read_config_dword(pdev
, pos
+ PCI_PRI_MAX_REQ
, &max_requests
);
173 reqs
= min(max_requests
, reqs
);
174 pci_write_config_dword(pdev
, pos
+ PCI_PRI_ALLOC_REQ
, reqs
);
176 control
|= PCI_PRI_CTRL_ENABLE
;
177 pci_write_config_word(pdev
, pos
+ PCI_PRI_CTRL
, control
);
181 EXPORT_SYMBOL_GPL(pci_enable_pri
);
184 * pci_disable_pri - Disable PRI capability
185 * @pdev: PCI device structure
187 * Only clears the enabled-bit, regardless of its former value
189 void pci_disable_pri(struct pci_dev
*pdev
)
194 pos
= pci_find_ext_capability(pdev
, PCI_EXT_CAP_ID_PRI
);
198 pci_read_config_word(pdev
, pos
+ PCI_PRI_CTRL
, &control
);
199 control
&= ~PCI_PRI_CTRL_ENABLE
;
200 pci_write_config_word(pdev
, pos
+ PCI_PRI_CTRL
, control
);
202 EXPORT_SYMBOL_GPL(pci_disable_pri
);
205 * pci_reset_pri - Resets device's PRI state
206 * @pdev: PCI device structure
208 * The PRI capability must be disabled before this function is called.
209 * Returns 0 on success, negative value on error.
211 int pci_reset_pri(struct pci_dev
*pdev
)
216 pos
= pci_find_ext_capability(pdev
, PCI_EXT_CAP_ID_PRI
);
220 pci_read_config_word(pdev
, pos
+ PCI_PRI_CTRL
, &control
);
221 if (control
& PCI_PRI_CTRL_ENABLE
)
224 control
|= PCI_PRI_CTRL_RESET
;
226 pci_write_config_word(pdev
, pos
+ PCI_PRI_CTRL
, control
);
230 EXPORT_SYMBOL_GPL(pci_reset_pri
);
231 #endif /* CONFIG_PCI_PRI */
233 #ifdef CONFIG_PCI_PASID
235 * pci_enable_pasid - Enable the PASID capability
236 * @pdev: PCI device structure
237 * @features: Features to enable
239 * Returns 0 on success, negative value on error. This function checks
240 * whether the features are actually supported by the device and returns
243 int pci_enable_pasid(struct pci_dev
*pdev
, int features
)
245 u16 control
, supported
;
248 pos
= pci_find_ext_capability(pdev
, PCI_EXT_CAP_ID_PASID
);
252 pci_read_config_word(pdev
, pos
+ PCI_PASID_CTRL
, &control
);
253 pci_read_config_word(pdev
, pos
+ PCI_PASID_CAP
, &supported
);
255 if (control
& PCI_PASID_CTRL_ENABLE
)
258 supported
&= PCI_PASID_CAP_EXEC
| PCI_PASID_CAP_PRIV
;
260 /* User wants to enable anything unsupported? */
261 if ((supported
& features
) != features
)
264 control
= PCI_PASID_CTRL_ENABLE
| features
;
266 pci_write_config_word(pdev
, pos
+ PCI_PASID_CTRL
, control
);
270 EXPORT_SYMBOL_GPL(pci_enable_pasid
);
273 * pci_disable_pasid - Disable the PASID capability
274 * @pdev: PCI device structure
277 void pci_disable_pasid(struct pci_dev
*pdev
)
282 pos
= pci_find_ext_capability(pdev
, PCI_EXT_CAP_ID_PASID
);
286 pci_write_config_word(pdev
, pos
+ PCI_PASID_CTRL
, control
);
288 EXPORT_SYMBOL_GPL(pci_disable_pasid
);
291 * pci_pasid_features - Check which PASID features are supported
292 * @pdev: PCI device structure
294 * Returns a negative value when no PASI capability is present.
295 * Otherwise is returns a bitmask with supported features. Current
296 * features reported are:
297 * PCI_PASID_CAP_EXEC - Execute permission supported
298 * PCI_PASID_CAP_PRIV - Privileged mode supported
300 int pci_pasid_features(struct pci_dev
*pdev
)
305 pos
= pci_find_ext_capability(pdev
, PCI_EXT_CAP_ID_PASID
);
309 pci_read_config_word(pdev
, pos
+ PCI_PASID_CAP
, &supported
);
311 supported
&= PCI_PASID_CAP_EXEC
| PCI_PASID_CAP_PRIV
;
315 EXPORT_SYMBOL_GPL(pci_pasid_features
);
317 #define PASID_NUMBER_SHIFT 8
318 #define PASID_NUMBER_MASK (0x1f << PASID_NUMBER_SHIFT)
320 * pci_max_pasid - Get maximum number of PASIDs supported by device
321 * @pdev: PCI device structure
323 * Returns negative value when PASID capability is not present.
324 * Otherwise it returns the numer of supported PASIDs.
326 int pci_max_pasids(struct pci_dev
*pdev
)
331 pos
= pci_find_ext_capability(pdev
, PCI_EXT_CAP_ID_PASID
);
335 pci_read_config_word(pdev
, pos
+ PCI_PASID_CAP
, &supported
);
337 supported
= (supported
& PASID_NUMBER_MASK
) >> PASID_NUMBER_SHIFT
;
339 return (1 << supported
);
341 EXPORT_SYMBOL_GPL(pci_max_pasids
);
342 #endif /* CONFIG_PCI_PASID */
This page took 0.038497 seconds and 5 git commands to generate.