treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / pci / ats.c
blob982b46f0a54da967dad65ac94ae87c81787789cd
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * PCI Express I/O Virtualization (IOV) support
4 * Address Translation Service 1.0
5 * Page Request Interface added by Joerg Roedel <joerg.roedel@amd.com>
6 * PASID support added by Joerg Roedel <joerg.roedel@amd.com>
8 * Copyright (C) 2009 Intel Corporation, Yu Zhao <yu.zhao@intel.com>
9 * Copyright (C) 2011 Advanced Micro Devices,
12 #include <linux/export.h>
13 #include <linux/pci-ats.h>
14 #include <linux/pci.h>
15 #include <linux/slab.h>
17 #include "pci.h"
19 void pci_ats_init(struct pci_dev *dev)
21 int pos;
23 if (pci_ats_disabled())
24 return;
26 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ATS);
27 if (!pos)
28 return;
30 dev->ats_cap = pos;
33 /**
34 * pci_enable_ats - enable the ATS capability
35 * @dev: the PCI device
36 * @ps: the IOMMU page shift
38 * Returns 0 on success, or negative on failure.
40 int pci_enable_ats(struct pci_dev *dev, int ps)
42 u16 ctrl;
43 struct pci_dev *pdev;
45 if (!dev->ats_cap)
46 return -EINVAL;
48 if (WARN_ON(dev->ats_enabled))
49 return -EBUSY;
51 if (ps < PCI_ATS_MIN_STU)
52 return -EINVAL;
55 * Note that enabling ATS on a VF fails unless it's already enabled
56 * with the same STU on the PF.
58 ctrl = PCI_ATS_CTRL_ENABLE;
59 if (dev->is_virtfn) {
60 pdev = pci_physfn(dev);
61 if (pdev->ats_stu != ps)
62 return -EINVAL;
63 } else {
64 dev->ats_stu = ps;
65 ctrl |= PCI_ATS_CTRL_STU(dev->ats_stu - PCI_ATS_MIN_STU);
67 pci_write_config_word(dev, dev->ats_cap + PCI_ATS_CTRL, ctrl);
69 dev->ats_enabled = 1;
70 return 0;
73 /**
74 * pci_disable_ats - disable the ATS capability
75 * @dev: the PCI device
77 void pci_disable_ats(struct pci_dev *dev)
79 u16 ctrl;
81 if (WARN_ON(!dev->ats_enabled))
82 return;
84 pci_read_config_word(dev, dev->ats_cap + PCI_ATS_CTRL, &ctrl);
85 ctrl &= ~PCI_ATS_CTRL_ENABLE;
86 pci_write_config_word(dev, dev->ats_cap + PCI_ATS_CTRL, ctrl);
88 dev->ats_enabled = 0;
91 void pci_restore_ats_state(struct pci_dev *dev)
93 u16 ctrl;
95 if (!dev->ats_enabled)
96 return;
98 ctrl = PCI_ATS_CTRL_ENABLE;
99 if (!dev->is_virtfn)
100 ctrl |= PCI_ATS_CTRL_STU(dev->ats_stu - PCI_ATS_MIN_STU);
101 pci_write_config_word(dev, dev->ats_cap + PCI_ATS_CTRL, ctrl);
105 * pci_ats_queue_depth - query the ATS Invalidate Queue Depth
106 * @dev: the PCI device
108 * Returns the queue depth on success, or negative on failure.
110 * The ATS spec uses 0 in the Invalidate Queue Depth field to
111 * indicate that the function can accept 32 Invalidate Request.
112 * But here we use the `real' values (i.e. 1~32) for the Queue
113 * Depth; and 0 indicates the function shares the Queue with
114 * other functions (doesn't exclusively own a Queue).
116 int pci_ats_queue_depth(struct pci_dev *dev)
118 u16 cap;
120 if (!dev->ats_cap)
121 return -EINVAL;
123 if (dev->is_virtfn)
124 return 0;
126 pci_read_config_word(dev, dev->ats_cap + PCI_ATS_CAP, &cap);
127 return PCI_ATS_CAP_QDEP(cap) ? PCI_ATS_CAP_QDEP(cap) : PCI_ATS_MAX_QDEP;
131 * pci_ats_page_aligned - Return Page Aligned Request bit status.
132 * @pdev: the PCI device
134 * Returns 1, if the Untranslated Addresses generated by the device
135 * are always aligned or 0 otherwise.
137 * Per PCIe spec r4.0, sec 10.5.1.2, if the Page Aligned Request bit
138 * is set, it indicates the Untranslated Addresses generated by the
139 * device are always aligned to a 4096 byte boundary.
141 int pci_ats_page_aligned(struct pci_dev *pdev)
143 u16 cap;
145 if (!pdev->ats_cap)
146 return 0;
148 pci_read_config_word(pdev, pdev->ats_cap + PCI_ATS_CAP, &cap);
150 if (cap & PCI_ATS_CAP_PAGE_ALIGNED)
151 return 1;
153 return 0;
156 #ifdef CONFIG_PCI_PRI
157 void pci_pri_init(struct pci_dev *pdev)
159 u16 status;
161 pdev->pri_cap = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
163 if (!pdev->pri_cap)
164 return;
166 pci_read_config_word(pdev, pdev->pri_cap + PCI_PRI_STATUS, &status);
167 if (status & PCI_PRI_STATUS_PASID)
168 pdev->pasid_required = 1;
172 * pci_enable_pri - Enable PRI capability
173 * @ pdev: PCI device structure
175 * Returns 0 on success, negative value on error
177 int pci_enable_pri(struct pci_dev *pdev, u32 reqs)
179 u16 control, status;
180 u32 max_requests;
181 int pri = pdev->pri_cap;
184 * VFs must not implement the PRI Capability. If their PF
185 * implements PRI, it is shared by the VFs, so if the PF PRI is
186 * enabled, it is also enabled for the VF.
188 if (pdev->is_virtfn) {
189 if (pci_physfn(pdev)->pri_enabled)
190 return 0;
191 return -EINVAL;
194 if (WARN_ON(pdev->pri_enabled))
195 return -EBUSY;
197 if (!pri)
198 return -EINVAL;
200 pci_read_config_word(pdev, pri + PCI_PRI_STATUS, &status);
201 if (!(status & PCI_PRI_STATUS_STOPPED))
202 return -EBUSY;
204 pci_read_config_dword(pdev, pri + PCI_PRI_MAX_REQ, &max_requests);
205 reqs = min(max_requests, reqs);
206 pdev->pri_reqs_alloc = reqs;
207 pci_write_config_dword(pdev, pri + PCI_PRI_ALLOC_REQ, reqs);
209 control = PCI_PRI_CTRL_ENABLE;
210 pci_write_config_word(pdev, pri + PCI_PRI_CTRL, control);
212 pdev->pri_enabled = 1;
214 return 0;
218 * pci_disable_pri - Disable PRI capability
219 * @pdev: PCI device structure
221 * Only clears the enabled-bit, regardless of its former value
223 void pci_disable_pri(struct pci_dev *pdev)
225 u16 control;
226 int pri = pdev->pri_cap;
228 /* VFs share the PF PRI */
229 if (pdev->is_virtfn)
230 return;
232 if (WARN_ON(!pdev->pri_enabled))
233 return;
235 if (!pri)
236 return;
238 pci_read_config_word(pdev, pri + PCI_PRI_CTRL, &control);
239 control &= ~PCI_PRI_CTRL_ENABLE;
240 pci_write_config_word(pdev, pri + PCI_PRI_CTRL, control);
242 pdev->pri_enabled = 0;
244 EXPORT_SYMBOL_GPL(pci_disable_pri);
247 * pci_restore_pri_state - Restore PRI
248 * @pdev: PCI device structure
250 void pci_restore_pri_state(struct pci_dev *pdev)
252 u16 control = PCI_PRI_CTRL_ENABLE;
253 u32 reqs = pdev->pri_reqs_alloc;
254 int pri = pdev->pri_cap;
256 if (pdev->is_virtfn)
257 return;
259 if (!pdev->pri_enabled)
260 return;
262 if (!pri)
263 return;
265 pci_write_config_dword(pdev, pri + PCI_PRI_ALLOC_REQ, reqs);
266 pci_write_config_word(pdev, pri + PCI_PRI_CTRL, control);
270 * pci_reset_pri - Resets device's PRI state
271 * @pdev: PCI device structure
273 * The PRI capability must be disabled before this function is called.
274 * Returns 0 on success, negative value on error.
276 int pci_reset_pri(struct pci_dev *pdev)
278 u16 control;
279 int pri = pdev->pri_cap;
281 if (pdev->is_virtfn)
282 return 0;
284 if (WARN_ON(pdev->pri_enabled))
285 return -EBUSY;
287 if (!pri)
288 return -EINVAL;
290 control = PCI_PRI_CTRL_RESET;
291 pci_write_config_word(pdev, pri + PCI_PRI_CTRL, control);
293 return 0;
297 * pci_prg_resp_pasid_required - Return PRG Response PASID Required bit
298 * status.
299 * @pdev: PCI device structure
301 * Returns 1 if PASID is required in PRG Response Message, 0 otherwise.
303 int pci_prg_resp_pasid_required(struct pci_dev *pdev)
305 if (pdev->is_virtfn)
306 pdev = pci_physfn(pdev);
308 return pdev->pasid_required;
310 #endif /* CONFIG_PCI_PRI */
312 #ifdef CONFIG_PCI_PASID
313 void pci_pasid_init(struct pci_dev *pdev)
315 pdev->pasid_cap = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PASID);
319 * pci_enable_pasid - Enable the PASID capability
320 * @pdev: PCI device structure
321 * @features: Features to enable
323 * Returns 0 on success, negative value on error. This function checks
324 * whether the features are actually supported by the device and returns
325 * an error if not.
327 int pci_enable_pasid(struct pci_dev *pdev, int features)
329 u16 control, supported;
330 int pasid = pdev->pasid_cap;
333 * VFs must not implement the PASID Capability, but if a PF
334 * supports PASID, its VFs share the PF PASID configuration.
336 if (pdev->is_virtfn) {
337 if (pci_physfn(pdev)->pasid_enabled)
338 return 0;
339 return -EINVAL;
342 if (WARN_ON(pdev->pasid_enabled))
343 return -EBUSY;
345 if (!pdev->eetlp_prefix_path)
346 return -EINVAL;
348 if (!pasid)
349 return -EINVAL;
351 pci_read_config_word(pdev, pasid + PCI_PASID_CAP, &supported);
352 supported &= PCI_PASID_CAP_EXEC | PCI_PASID_CAP_PRIV;
354 /* User wants to enable anything unsupported? */
355 if ((supported & features) != features)
356 return -EINVAL;
358 control = PCI_PASID_CTRL_ENABLE | features;
359 pdev->pasid_features = features;
361 pci_write_config_word(pdev, pasid + PCI_PASID_CTRL, control);
363 pdev->pasid_enabled = 1;
365 return 0;
369 * pci_disable_pasid - Disable the PASID capability
370 * @pdev: PCI device structure
372 void pci_disable_pasid(struct pci_dev *pdev)
374 u16 control = 0;
375 int pasid = pdev->pasid_cap;
377 /* VFs share the PF PASID configuration */
378 if (pdev->is_virtfn)
379 return;
381 if (WARN_ON(!pdev->pasid_enabled))
382 return;
384 if (!pasid)
385 return;
387 pci_write_config_word(pdev, pasid + PCI_PASID_CTRL, control);
389 pdev->pasid_enabled = 0;
393 * pci_restore_pasid_state - Restore PASID capabilities
394 * @pdev: PCI device structure
396 void pci_restore_pasid_state(struct pci_dev *pdev)
398 u16 control;
399 int pasid = pdev->pasid_cap;
401 if (pdev->is_virtfn)
402 return;
404 if (!pdev->pasid_enabled)
405 return;
407 if (!pasid)
408 return;
410 control = PCI_PASID_CTRL_ENABLE | pdev->pasid_features;
411 pci_write_config_word(pdev, pasid + PCI_PASID_CTRL, control);
415 * pci_pasid_features - Check which PASID features are supported
416 * @pdev: PCI device structure
418 * Returns a negative value when no PASI capability is present.
419 * Otherwise is returns a bitmask with supported features. Current
420 * features reported are:
421 * PCI_PASID_CAP_EXEC - Execute permission supported
422 * PCI_PASID_CAP_PRIV - Privileged mode supported
424 int pci_pasid_features(struct pci_dev *pdev)
426 u16 supported;
427 int pasid = pdev->pasid_cap;
429 if (pdev->is_virtfn)
430 pdev = pci_physfn(pdev);
432 if (!pasid)
433 return -EINVAL;
435 pci_read_config_word(pdev, pasid + PCI_PASID_CAP, &supported);
437 supported &= PCI_PASID_CAP_EXEC | PCI_PASID_CAP_PRIV;
439 return supported;
442 #define PASID_NUMBER_SHIFT 8
443 #define PASID_NUMBER_MASK (0x1f << PASID_NUMBER_SHIFT)
445 * pci_max_pasid - Get maximum number of PASIDs supported by device
446 * @pdev: PCI device structure
448 * Returns negative value when PASID capability is not present.
449 * Otherwise it returns the number of supported PASIDs.
451 int pci_max_pasids(struct pci_dev *pdev)
453 u16 supported;
454 int pasid = pdev->pasid_cap;
456 if (pdev->is_virtfn)
457 pdev = pci_physfn(pdev);
459 if (!pasid)
460 return -EINVAL;
462 pci_read_config_word(pdev, pasid + PCI_PASID_CAP, &supported);
464 supported = (supported & PASID_NUMBER_MASK) >> PASID_NUMBER_SHIFT;
466 return (1 << supported);
468 #endif /* CONFIG_PCI_PASID */