1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 2009 Intel Corporation, Yu Zhao <yu.zhao@intel.com>
6 * Copyright (C) 2011 Advanced Micro Devices,
8 * PCI Express I/O Virtualization (IOV) support.
9 * Address Translation Service 1.0
10 * Page Request Interface added by Joerg Roedel <joerg.roedel@amd.com>
11 * PASID support added by Joerg Roedel <joerg.roedel@amd.com>
14 #include <linux/export.h>
15 #include <linux/pci-ats.h>
16 #include <linux/pci.h>
17 #include <linux/slab.h>
21 void pci_ats_init(struct pci_dev
*dev
)
25 pos
= pci_find_ext_capability(dev
, PCI_EXT_CAP_ID_ATS
);
33 * pci_enable_ats - enable the ATS capability
34 * @dev: the PCI device
35 * @ps: the IOMMU page shift
37 * Returns 0 on success, or negative on failure.
39 int pci_enable_ats(struct pci_dev
*dev
, int ps
)
47 if (WARN_ON(dev
->ats_enabled
))
50 if (ps
< PCI_ATS_MIN_STU
)
54 * Note that enabling ATS on a VF fails unless it's already enabled
55 * with the same STU on the PF.
57 ctrl
= PCI_ATS_CTRL_ENABLE
;
59 pdev
= pci_physfn(dev
);
60 if (pdev
->ats_stu
!= ps
)
63 atomic_inc(&pdev
->ats_ref_cnt
); /* count enabled VFs */
66 ctrl
|= PCI_ATS_CTRL_STU(dev
->ats_stu
- PCI_ATS_MIN_STU
);
68 pci_write_config_word(dev
, dev
->ats_cap
+ PCI_ATS_CTRL
, ctrl
);
73 EXPORT_SYMBOL_GPL(pci_enable_ats
);
76 * pci_disable_ats - disable the ATS capability
77 * @dev: the PCI device
79 void pci_disable_ats(struct pci_dev
*dev
)
84 if (WARN_ON(!dev
->ats_enabled
))
87 if (atomic_read(&dev
->ats_ref_cnt
))
88 return; /* VFs still enabled */
91 pdev
= pci_physfn(dev
);
92 atomic_dec(&pdev
->ats_ref_cnt
);
95 pci_read_config_word(dev
, dev
->ats_cap
+ PCI_ATS_CTRL
, &ctrl
);
96 ctrl
&= ~PCI_ATS_CTRL_ENABLE
;
97 pci_write_config_word(dev
, dev
->ats_cap
+ PCI_ATS_CTRL
, ctrl
);
101 EXPORT_SYMBOL_GPL(pci_disable_ats
);
103 void pci_restore_ats_state(struct pci_dev
*dev
)
107 if (!dev
->ats_enabled
)
110 ctrl
= PCI_ATS_CTRL_ENABLE
;
112 ctrl
|= PCI_ATS_CTRL_STU(dev
->ats_stu
- PCI_ATS_MIN_STU
);
113 pci_write_config_word(dev
, dev
->ats_cap
+ PCI_ATS_CTRL
, ctrl
);
115 EXPORT_SYMBOL_GPL(pci_restore_ats_state
);
118 * pci_ats_queue_depth - query the ATS Invalidate Queue Depth
119 * @dev: the PCI device
121 * Returns the queue depth on success, or negative on failure.
123 * The ATS spec uses 0 in the Invalidate Queue Depth field to
124 * indicate that the function can accept 32 Invalidate Request.
125 * But here we use the `real' values (i.e. 1~32) for the Queue
126 * Depth; and 0 indicates the function shares the Queue with
127 * other functions (doesn't exclusively own a Queue).
129 int pci_ats_queue_depth(struct pci_dev
*dev
)
139 pci_read_config_word(dev
, dev
->ats_cap
+ PCI_ATS_CAP
, &cap
);
140 return PCI_ATS_CAP_QDEP(cap
) ? PCI_ATS_CAP_QDEP(cap
) : PCI_ATS_MAX_QDEP
;
142 EXPORT_SYMBOL_GPL(pci_ats_queue_depth
);
144 #ifdef CONFIG_PCI_PRI
146 * pci_enable_pri - Enable PRI capability
147 * @ pdev: PCI device structure
149 * Returns 0 on success, negative value on error
151 int pci_enable_pri(struct pci_dev
*pdev
, u32 reqs
)
157 if (WARN_ON(pdev
->pri_enabled
))
160 pos
= pci_find_ext_capability(pdev
, PCI_EXT_CAP_ID_PRI
);
164 pci_read_config_word(pdev
, pos
+ PCI_PRI_STATUS
, &status
);
165 if (!(status
& PCI_PRI_STATUS_STOPPED
))
168 pci_read_config_dword(pdev
, pos
+ PCI_PRI_MAX_REQ
, &max_requests
);
169 reqs
= min(max_requests
, reqs
);
170 pdev
->pri_reqs_alloc
= reqs
;
171 pci_write_config_dword(pdev
, pos
+ PCI_PRI_ALLOC_REQ
, reqs
);
173 control
= PCI_PRI_CTRL_ENABLE
;
174 pci_write_config_word(pdev
, pos
+ PCI_PRI_CTRL
, control
);
176 pdev
->pri_enabled
= 1;
180 EXPORT_SYMBOL_GPL(pci_enable_pri
);
183 * pci_disable_pri - Disable PRI capability
184 * @pdev: PCI device structure
186 * Only clears the enabled-bit, regardless of its former value
188 void pci_disable_pri(struct pci_dev
*pdev
)
193 if (WARN_ON(!pdev
->pri_enabled
))
196 pos
= pci_find_ext_capability(pdev
, PCI_EXT_CAP_ID_PRI
);
200 pci_read_config_word(pdev
, pos
+ PCI_PRI_CTRL
, &control
);
201 control
&= ~PCI_PRI_CTRL_ENABLE
;
202 pci_write_config_word(pdev
, pos
+ PCI_PRI_CTRL
, control
);
204 pdev
->pri_enabled
= 0;
206 EXPORT_SYMBOL_GPL(pci_disable_pri
);
209 * pci_restore_pri_state - Restore PRI
210 * @pdev: PCI device structure
212 void pci_restore_pri_state(struct pci_dev
*pdev
)
214 u16 control
= PCI_PRI_CTRL_ENABLE
;
215 u32 reqs
= pdev
->pri_reqs_alloc
;
218 if (!pdev
->pri_enabled
)
221 pos
= pci_find_ext_capability(pdev
, PCI_EXT_CAP_ID_PRI
);
225 pci_write_config_dword(pdev
, pos
+ PCI_PRI_ALLOC_REQ
, reqs
);
226 pci_write_config_word(pdev
, pos
+ PCI_PRI_CTRL
, control
);
228 EXPORT_SYMBOL_GPL(pci_restore_pri_state
);
231 * pci_reset_pri - Resets device's PRI state
232 * @pdev: PCI device structure
234 * The PRI capability must be disabled before this function is called.
235 * Returns 0 on success, negative value on error.
237 int pci_reset_pri(struct pci_dev
*pdev
)
242 if (WARN_ON(pdev
->pri_enabled
))
245 pos
= pci_find_ext_capability(pdev
, PCI_EXT_CAP_ID_PRI
);
249 control
= PCI_PRI_CTRL_RESET
;
250 pci_write_config_word(pdev
, pos
+ PCI_PRI_CTRL
, control
);
254 EXPORT_SYMBOL_GPL(pci_reset_pri
);
255 #endif /* CONFIG_PCI_PRI */
257 #ifdef CONFIG_PCI_PASID
259 * pci_enable_pasid - Enable the PASID capability
260 * @pdev: PCI device structure
261 * @features: Features to enable
263 * Returns 0 on success, negative value on error. This function checks
264 * whether the features are actually supported by the device and returns
267 int pci_enable_pasid(struct pci_dev
*pdev
, int features
)
269 u16 control
, supported
;
272 if (WARN_ON(pdev
->pasid_enabled
))
275 pos
= pci_find_ext_capability(pdev
, PCI_EXT_CAP_ID_PASID
);
279 pci_read_config_word(pdev
, pos
+ PCI_PASID_CAP
, &supported
);
280 supported
&= PCI_PASID_CAP_EXEC
| PCI_PASID_CAP_PRIV
;
282 /* User wants to enable anything unsupported? */
283 if ((supported
& features
) != features
)
286 control
= PCI_PASID_CTRL_ENABLE
| features
;
287 pdev
->pasid_features
= features
;
289 pci_write_config_word(pdev
, pos
+ PCI_PASID_CTRL
, control
);
291 pdev
->pasid_enabled
= 1;
295 EXPORT_SYMBOL_GPL(pci_enable_pasid
);
298 * pci_disable_pasid - Disable the PASID capability
299 * @pdev: PCI device structure
301 void pci_disable_pasid(struct pci_dev
*pdev
)
306 if (WARN_ON(!pdev
->pasid_enabled
))
309 pos
= pci_find_ext_capability(pdev
, PCI_EXT_CAP_ID_PASID
);
313 pci_write_config_word(pdev
, pos
+ PCI_PASID_CTRL
, control
);
315 pdev
->pasid_enabled
= 0;
317 EXPORT_SYMBOL_GPL(pci_disable_pasid
);
320 * pci_restore_pasid_state - Restore PASID capabilities
321 * @pdev: PCI device structure
323 void pci_restore_pasid_state(struct pci_dev
*pdev
)
328 if (!pdev
->pasid_enabled
)
331 pos
= pci_find_ext_capability(pdev
, PCI_EXT_CAP_ID_PASID
);
335 control
= PCI_PASID_CTRL_ENABLE
| pdev
->pasid_features
;
336 pci_write_config_word(pdev
, pos
+ PCI_PASID_CTRL
, control
);
338 EXPORT_SYMBOL_GPL(pci_restore_pasid_state
);
341 * pci_pasid_features - Check which PASID features are supported
342 * @pdev: PCI device structure
344 * Returns a negative value when no PASI capability is present.
345 * Otherwise is returns a bitmask with supported features. Current
346 * features reported are:
347 * PCI_PASID_CAP_EXEC - Execute permission supported
348 * PCI_PASID_CAP_PRIV - Privileged mode supported
350 int pci_pasid_features(struct pci_dev
*pdev
)
355 pos
= pci_find_ext_capability(pdev
, PCI_EXT_CAP_ID_PASID
);
359 pci_read_config_word(pdev
, pos
+ PCI_PASID_CAP
, &supported
);
361 supported
&= PCI_PASID_CAP_EXEC
| PCI_PASID_CAP_PRIV
;
365 EXPORT_SYMBOL_GPL(pci_pasid_features
);
367 #define PASID_NUMBER_SHIFT 8
368 #define PASID_NUMBER_MASK (0x1f << PASID_NUMBER_SHIFT)
370 * pci_max_pasid - Get maximum number of PASIDs supported by device
371 * @pdev: PCI device structure
373 * Returns negative value when PASID capability is not present.
374 * Otherwise it returns the numer of supported PASIDs.
376 int pci_max_pasids(struct pci_dev
*pdev
)
381 pos
= pci_find_ext_capability(pdev
, PCI_EXT_CAP_ID_PASID
);
385 pci_read_config_word(pdev
, pos
+ PCI_PASID_CAP
, &supported
);
387 supported
= (supported
& PASID_NUMBER_MASK
) >> PASID_NUMBER_SHIFT
;
389 return (1 << supported
);
391 EXPORT_SYMBOL_GPL(pci_max_pasids
);
392 #endif /* CONFIG_PCI_PASID */