Linux 4.16.11
[linux/fpc-iii.git] / drivers / pci / ats.c
blob6ad80a1fd5a7a6098ac41b3dbd38bea427dc7839
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * drivers/pci/ats.c
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>
19 #include "pci.h"
21 void pci_ats_init(struct pci_dev *dev)
23 int pos;
25 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ATS);
26 if (!pos)
27 return;
29 dev->ats_cap = pos;
32 /**
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)
41 u16 ctrl;
42 struct pci_dev *pdev;
44 if (!dev->ats_cap)
45 return -EINVAL;
47 if (WARN_ON(dev->ats_enabled))
48 return -EBUSY;
50 if (ps < PCI_ATS_MIN_STU)
51 return -EINVAL;
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;
58 if (dev->is_virtfn) {
59 pdev = pci_physfn(dev);
60 if (pdev->ats_stu != ps)
61 return -EINVAL;
63 atomic_inc(&pdev->ats_ref_cnt); /* count enabled VFs */
64 } else {
65 dev->ats_stu = ps;
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);
70 dev->ats_enabled = 1;
71 return 0;
73 EXPORT_SYMBOL_GPL(pci_enable_ats);
75 /**
76 * pci_disable_ats - disable the ATS capability
77 * @dev: the PCI device
79 void pci_disable_ats(struct pci_dev *dev)
81 struct pci_dev *pdev;
82 u16 ctrl;
84 if (WARN_ON(!dev->ats_enabled))
85 return;
87 if (atomic_read(&dev->ats_ref_cnt))
88 return; /* VFs still enabled */
90 if (dev->is_virtfn) {
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);
99 dev->ats_enabled = 0;
101 EXPORT_SYMBOL_GPL(pci_disable_ats);
103 void pci_restore_ats_state(struct pci_dev *dev)
105 u16 ctrl;
107 if (!dev->ats_enabled)
108 return;
110 ctrl = PCI_ATS_CTRL_ENABLE;
111 if (!dev->is_virtfn)
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)
131 u16 cap;
133 if (!dev->ats_cap)
134 return -EINVAL;
136 if (dev->is_virtfn)
137 return 0;
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)
153 u16 control, status;
154 u32 max_requests;
155 int pos;
157 if (WARN_ON(pdev->pri_enabled))
158 return -EBUSY;
160 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
161 if (!pos)
162 return -EINVAL;
164 pci_read_config_word(pdev, pos + PCI_PRI_STATUS, &status);
165 if (!(status & PCI_PRI_STATUS_STOPPED))
166 return -EBUSY;
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;
178 return 0;
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)
190 u16 control;
191 int pos;
193 if (WARN_ON(!pdev->pri_enabled))
194 return;
196 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
197 if (!pos)
198 return;
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;
216 int pos;
218 if (!pdev->pri_enabled)
219 return;
221 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
222 if (!pos)
223 return;
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)
239 u16 control;
240 int pos;
242 if (WARN_ON(pdev->pri_enabled))
243 return -EBUSY;
245 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
246 if (!pos)
247 return -EINVAL;
249 control = PCI_PRI_CTRL_RESET;
250 pci_write_config_word(pdev, pos + PCI_PRI_CTRL, control);
252 return 0;
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
265 * an error if not.
267 int pci_enable_pasid(struct pci_dev *pdev, int features)
269 u16 control, supported;
270 int pos;
272 if (WARN_ON(pdev->pasid_enabled))
273 return -EBUSY;
275 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PASID);
276 if (!pos)
277 return -EINVAL;
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)
284 return -EINVAL;
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;
293 return 0;
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)
303 u16 control = 0;
304 int pos;
306 if (WARN_ON(!pdev->pasid_enabled))
307 return;
309 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PASID);
310 if (!pos)
311 return;
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)
325 u16 control;
326 int pos;
328 if (!pdev->pasid_enabled)
329 return;
331 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PASID);
332 if (!pos)
333 return;
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)
352 u16 supported;
353 int pos;
355 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PASID);
356 if (!pos)
357 return -EINVAL;
359 pci_read_config_word(pdev, pos + PCI_PASID_CAP, &supported);
361 supported &= PCI_PASID_CAP_EXEC | PCI_PASID_CAP_PRIV;
363 return supported;
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)
378 u16 supported;
379 int pos;
381 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PASID);
382 if (!pos)
383 return -EINVAL;
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 */