Bjorn Helgaas | 7328c8f | 2018-01-26 11:45:16 -0600 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Joerg Roedel | db3c33c | 2011-09-27 15:57:13 +0200 | [diff] [blame] | 2 | /* |
Bjorn Helgaas | df62ab5 | 2018-03-09 16:36:33 -0600 | [diff] [blame] | 3 | * PCI Express I/O Virtualization (IOV) support |
Joerg Roedel | db3c33c | 2011-09-27 15:57:13 +0200 | [diff] [blame] | 4 | * Address Translation Service 1.0 |
Joerg Roedel | c320b97 | 2011-09-27 15:57:15 +0200 | [diff] [blame] | 5 | * Page Request Interface added by Joerg Roedel <joerg.roedel@amd.com> |
Joerg Roedel | 086ac11 | 2011-09-27 15:57:16 +0200 | [diff] [blame] | 6 | * PASID support added by Joerg Roedel <joerg.roedel@amd.com> |
Bjorn Helgaas | df62ab5 | 2018-03-09 16:36:33 -0600 | [diff] [blame] | 7 | * |
| 8 | * Copyright (C) 2009 Intel Corporation, Yu Zhao <yu.zhao@intel.com> |
| 9 | * Copyright (C) 2011 Advanced Micro Devices, |
Joerg Roedel | db3c33c | 2011-09-27 15:57:13 +0200 | [diff] [blame] | 10 | */ |
| 11 | |
Paul Gortmaker | 363c75d | 2011-05-27 09:37:25 -0400 | [diff] [blame] | 12 | #include <linux/export.h> |
Joerg Roedel | db3c33c | 2011-09-27 15:57:13 +0200 | [diff] [blame] | 13 | #include <linux/pci-ats.h> |
| 14 | #include <linux/pci.h> |
James Bottomley | 8c45194 | 2011-11-29 19:20:23 +0000 | [diff] [blame] | 15 | #include <linux/slab.h> |
Joerg Roedel | db3c33c | 2011-09-27 15:57:13 +0200 | [diff] [blame] | 16 | |
| 17 | #include "pci.h" |
| 18 | |
Bjorn Helgaas | afdd596 | 2015-07-17 15:35:18 -0500 | [diff] [blame] | 19 | void pci_ats_init(struct pci_dev *dev) |
Joerg Roedel | db3c33c | 2011-09-27 15:57:13 +0200 | [diff] [blame] | 20 | { |
| 21 | int pos; |
Joerg Roedel | db3c33c | 2011-09-27 15:57:13 +0200 | [diff] [blame] | 22 | |
Gil Kupfer | cef7440 | 2018-05-10 17:56:02 -0500 | [diff] [blame] | 23 | if (pci_ats_disabled()) |
| 24 | return; |
| 25 | |
Joerg Roedel | db3c33c | 2011-09-27 15:57:13 +0200 | [diff] [blame] | 26 | pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ATS); |
| 27 | if (!pos) |
Bjorn Helgaas | edc90fe | 2015-07-17 15:05:46 -0500 | [diff] [blame] | 28 | return; |
Joerg Roedel | db3c33c | 2011-09-27 15:57:13 +0200 | [diff] [blame] | 29 | |
Bjorn Helgaas | d544d75 | 2015-07-17 15:15:19 -0500 | [diff] [blame] | 30 | dev->ats_cap = pos; |
Joerg Roedel | db3c33c | 2011-09-27 15:57:13 +0200 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | /** |
Jean-Philippe Brucker | 5213767 | 2020-05-20 17:22:00 +0200 | [diff] [blame] | 34 | * pci_ats_supported - check if the device can use ATS |
| 35 | * @dev: the PCI device |
| 36 | * |
| 37 | * Returns true if the device supports ATS and is allowed to use it, false |
| 38 | * otherwise. |
| 39 | */ |
| 40 | bool pci_ats_supported(struct pci_dev *dev) |
| 41 | { |
| 42 | if (!dev->ats_cap) |
| 43 | return false; |
| 44 | |
| 45 | return (dev->untrusted == 0); |
| 46 | } |
| 47 | EXPORT_SYMBOL_GPL(pci_ats_supported); |
| 48 | |
| 49 | /** |
Joerg Roedel | db3c33c | 2011-09-27 15:57:13 +0200 | [diff] [blame] | 50 | * pci_enable_ats - enable the ATS capability |
| 51 | * @dev: the PCI device |
| 52 | * @ps: the IOMMU page shift |
| 53 | * |
| 54 | * Returns 0 on success, or negative on failure. |
| 55 | */ |
| 56 | int pci_enable_ats(struct pci_dev *dev, int ps) |
| 57 | { |
Joerg Roedel | db3c33c | 2011-09-27 15:57:13 +0200 | [diff] [blame] | 58 | u16 ctrl; |
Bjorn Helgaas | c39127d | 2015-07-17 15:38:13 -0500 | [diff] [blame] | 59 | struct pci_dev *pdev; |
Joerg Roedel | db3c33c | 2011-09-27 15:57:13 +0200 | [diff] [blame] | 60 | |
Jean-Philippe Brucker | 5213767 | 2020-05-20 17:22:00 +0200 | [diff] [blame] | 61 | if (!pci_ats_supported(dev)) |
Bjorn Helgaas | edc90fe | 2015-07-17 15:05:46 -0500 | [diff] [blame] | 62 | return -EINVAL; |
| 63 | |
Bjorn Helgaas | f7ef134 | 2015-07-20 09:23:37 -0500 | [diff] [blame] | 64 | if (WARN_ON(dev->ats_enabled)) |
Bjorn Helgaas | a021f30 | 2015-07-17 15:43:27 -0500 | [diff] [blame] | 65 | return -EBUSY; |
| 66 | |
Joerg Roedel | db3c33c | 2011-09-27 15:57:13 +0200 | [diff] [blame] | 67 | if (ps < PCI_ATS_MIN_STU) |
| 68 | return -EINVAL; |
| 69 | |
Bjorn Helgaas | edc90fe | 2015-07-17 15:05:46 -0500 | [diff] [blame] | 70 | /* |
| 71 | * Note that enabling ATS on a VF fails unless it's already enabled |
| 72 | * with the same STU on the PF. |
| 73 | */ |
Joerg Roedel | db3c33c | 2011-09-27 15:57:13 +0200 | [diff] [blame] | 74 | ctrl = PCI_ATS_CTRL_ENABLE; |
Bjorn Helgaas | edc90fe | 2015-07-17 15:05:46 -0500 | [diff] [blame] | 75 | if (dev->is_virtfn) { |
Bjorn Helgaas | c39127d | 2015-07-17 15:38:13 -0500 | [diff] [blame] | 76 | pdev = pci_physfn(dev); |
Bjorn Helgaas | d544d75 | 2015-07-17 15:15:19 -0500 | [diff] [blame] | 77 | if (pdev->ats_stu != ps) |
Bjorn Helgaas | edc90fe | 2015-07-17 15:05:46 -0500 | [diff] [blame] | 78 | return -EINVAL; |
Bjorn Helgaas | edc90fe | 2015-07-17 15:05:46 -0500 | [diff] [blame] | 79 | } else { |
Bjorn Helgaas | d544d75 | 2015-07-17 15:15:19 -0500 | [diff] [blame] | 80 | dev->ats_stu = ps; |
| 81 | ctrl |= PCI_ATS_CTRL_STU(dev->ats_stu - PCI_ATS_MIN_STU); |
Bjorn Helgaas | edc90fe | 2015-07-17 15:05:46 -0500 | [diff] [blame] | 82 | } |
Bjorn Helgaas | d544d75 | 2015-07-17 15:15:19 -0500 | [diff] [blame] | 83 | pci_write_config_word(dev, dev->ats_cap + PCI_ATS_CTRL, ctrl); |
Joerg Roedel | db3c33c | 2011-09-27 15:57:13 +0200 | [diff] [blame] | 84 | |
Bjorn Helgaas | d544d75 | 2015-07-17 15:15:19 -0500 | [diff] [blame] | 85 | dev->ats_enabled = 1; |
Joerg Roedel | db3c33c | 2011-09-27 15:57:13 +0200 | [diff] [blame] | 86 | return 0; |
| 87 | } |
Greg Kroah-Hartman | bb950bc | 2019-12-19 12:03:39 +0000 | [diff] [blame] | 88 | EXPORT_SYMBOL_GPL(pci_enable_ats); |
Joerg Roedel | db3c33c | 2011-09-27 15:57:13 +0200 | [diff] [blame] | 89 | |
| 90 | /** |
| 91 | * pci_disable_ats - disable the ATS capability |
| 92 | * @dev: the PCI device |
| 93 | */ |
| 94 | void pci_disable_ats(struct pci_dev *dev) |
| 95 | { |
| 96 | u16 ctrl; |
| 97 | |
Bjorn Helgaas | f7ef134 | 2015-07-20 09:23:37 -0500 | [diff] [blame] | 98 | if (WARN_ON(!dev->ats_enabled)) |
Bjorn Helgaas | a021f30 | 2015-07-17 15:43:27 -0500 | [diff] [blame] | 99 | return; |
Joerg Roedel | db3c33c | 2011-09-27 15:57:13 +0200 | [diff] [blame] | 100 | |
Bjorn Helgaas | d544d75 | 2015-07-17 15:15:19 -0500 | [diff] [blame] | 101 | pci_read_config_word(dev, dev->ats_cap + PCI_ATS_CTRL, &ctrl); |
Joerg Roedel | db3c33c | 2011-09-27 15:57:13 +0200 | [diff] [blame] | 102 | ctrl &= ~PCI_ATS_CTRL_ENABLE; |
Bjorn Helgaas | d544d75 | 2015-07-17 15:15:19 -0500 | [diff] [blame] | 103 | pci_write_config_word(dev, dev->ats_cap + PCI_ATS_CTRL, ctrl); |
Joerg Roedel | db3c33c | 2011-09-27 15:57:13 +0200 | [diff] [blame] | 104 | |
Bjorn Helgaas | d544d75 | 2015-07-17 15:15:19 -0500 | [diff] [blame] | 105 | dev->ats_enabled = 0; |
Joerg Roedel | db3c33c | 2011-09-27 15:57:13 +0200 | [diff] [blame] | 106 | } |
Greg Kroah-Hartman | bb950bc | 2019-12-19 12:03:39 +0000 | [diff] [blame] | 107 | EXPORT_SYMBOL_GPL(pci_disable_ats); |
Joerg Roedel | db3c33c | 2011-09-27 15:57:13 +0200 | [diff] [blame] | 108 | |
Hao, Xudong | 1900ca1 | 2011-12-17 21:24:40 +0800 | [diff] [blame] | 109 | void pci_restore_ats_state(struct pci_dev *dev) |
| 110 | { |
| 111 | u16 ctrl; |
| 112 | |
Bjorn Helgaas | f7ef134 | 2015-07-20 09:23:37 -0500 | [diff] [blame] | 113 | if (!dev->ats_enabled) |
Hao, Xudong | 1900ca1 | 2011-12-17 21:24:40 +0800 | [diff] [blame] | 114 | return; |
Hao, Xudong | 1900ca1 | 2011-12-17 21:24:40 +0800 | [diff] [blame] | 115 | |
| 116 | ctrl = PCI_ATS_CTRL_ENABLE; |
| 117 | if (!dev->is_virtfn) |
Bjorn Helgaas | d544d75 | 2015-07-17 15:15:19 -0500 | [diff] [blame] | 118 | ctrl |= PCI_ATS_CTRL_STU(dev->ats_stu - PCI_ATS_MIN_STU); |
| 119 | pci_write_config_word(dev, dev->ats_cap + PCI_ATS_CTRL, ctrl); |
Hao, Xudong | 1900ca1 | 2011-12-17 21:24:40 +0800 | [diff] [blame] | 120 | } |
Hao, Xudong | 1900ca1 | 2011-12-17 21:24:40 +0800 | [diff] [blame] | 121 | |
Joerg Roedel | db3c33c | 2011-09-27 15:57:13 +0200 | [diff] [blame] | 122 | /** |
| 123 | * pci_ats_queue_depth - query the ATS Invalidate Queue Depth |
| 124 | * @dev: the PCI device |
| 125 | * |
| 126 | * Returns the queue depth on success, or negative on failure. |
| 127 | * |
| 128 | * The ATS spec uses 0 in the Invalidate Queue Depth field to |
| 129 | * indicate that the function can accept 32 Invalidate Request. |
| 130 | * But here we use the `real' values (i.e. 1~32) for the Queue |
| 131 | * Depth; and 0 indicates the function shares the Queue with |
| 132 | * other functions (doesn't exclusively own a Queue). |
| 133 | */ |
| 134 | int pci_ats_queue_depth(struct pci_dev *dev) |
| 135 | { |
Bjorn Helgaas | a71f938 | 2015-07-20 09:24:32 -0500 | [diff] [blame] | 136 | u16 cap; |
| 137 | |
Bjorn Helgaas | 3c76539 | 2015-07-17 15:30:26 -0500 | [diff] [blame] | 138 | if (!dev->ats_cap) |
| 139 | return -EINVAL; |
| 140 | |
Joerg Roedel | db3c33c | 2011-09-27 15:57:13 +0200 | [diff] [blame] | 141 | if (dev->is_virtfn) |
| 142 | return 0; |
| 143 | |
Bjorn Helgaas | a71f938 | 2015-07-20 09:24:32 -0500 | [diff] [blame] | 144 | pci_read_config_word(dev, dev->ats_cap + PCI_ATS_CAP, &cap); |
| 145 | return PCI_ATS_CAP_QDEP(cap) ? PCI_ATS_CAP_QDEP(cap) : PCI_ATS_MAX_QDEP; |
Joerg Roedel | db3c33c | 2011-09-27 15:57:13 +0200 | [diff] [blame] | 146 | } |
Joerg Roedel | c320b97 | 2011-09-27 15:57:15 +0200 | [diff] [blame] | 147 | |
Kuppuswamy Sathyanarayanan | 8c938dd | 2019-02-19 11:06:09 -0800 | [diff] [blame] | 148 | /** |
| 149 | * pci_ats_page_aligned - Return Page Aligned Request bit status. |
| 150 | * @pdev: the PCI device |
| 151 | * |
| 152 | * Returns 1, if the Untranslated Addresses generated by the device |
| 153 | * are always aligned or 0 otherwise. |
| 154 | * |
| 155 | * Per PCIe spec r4.0, sec 10.5.1.2, if the Page Aligned Request bit |
| 156 | * is set, it indicates the Untranslated Addresses generated by the |
| 157 | * device are always aligned to a 4096 byte boundary. |
| 158 | */ |
| 159 | int pci_ats_page_aligned(struct pci_dev *pdev) |
| 160 | { |
| 161 | u16 cap; |
| 162 | |
| 163 | if (!pdev->ats_cap) |
| 164 | return 0; |
| 165 | |
| 166 | pci_read_config_word(pdev, pdev->ats_cap + PCI_ATS_CAP, &cap); |
| 167 | |
| 168 | if (cap & PCI_ATS_CAP_PAGE_ALIGNED) |
| 169 | return 1; |
| 170 | |
| 171 | return 0; |
| 172 | } |
Kuppuswamy Sathyanarayanan | 8c938dd | 2019-02-19 11:06:09 -0800 | [diff] [blame] | 173 | |
Joerg Roedel | c320b97 | 2011-09-27 15:57:15 +0200 | [diff] [blame] | 174 | #ifdef CONFIG_PCI_PRI |
Kuppuswamy Sathyanarayanan | c065190 | 2019-09-05 14:31:45 -0500 | [diff] [blame] | 175 | void pci_pri_init(struct pci_dev *pdev) |
| 176 | { |
Bjorn Helgaas | e5adf79 | 2019-10-09 16:07:51 -0500 | [diff] [blame] | 177 | u16 status; |
| 178 | |
Kuppuswamy Sathyanarayanan | c065190 | 2019-09-05 14:31:45 -0500 | [diff] [blame] | 179 | pdev->pri_cap = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI); |
Bjorn Helgaas | e5adf79 | 2019-10-09 16:07:51 -0500 | [diff] [blame] | 180 | |
| 181 | if (!pdev->pri_cap) |
| 182 | return; |
| 183 | |
| 184 | pci_read_config_word(pdev, pdev->pri_cap + PCI_PRI_STATUS, &status); |
| 185 | if (status & PCI_PRI_STATUS_PASID) |
| 186 | pdev->pasid_required = 1; |
Kuppuswamy Sathyanarayanan | c065190 | 2019-09-05 14:31:45 -0500 | [diff] [blame] | 187 | } |
| 188 | |
Joerg Roedel | c320b97 | 2011-09-27 15:57:15 +0200 | [diff] [blame] | 189 | /** |
| 190 | * pci_enable_pri - Enable PRI capability |
Krzysztof Kozlowski | 9b41d19 | 2020-07-29 22:12:19 +0200 | [diff] [blame] | 191 | * @pdev: PCI device structure |
| 192 | * @reqs: outstanding requests |
Joerg Roedel | c320b97 | 2011-09-27 15:57:15 +0200 | [diff] [blame] | 193 | * |
| 194 | * Returns 0 on success, negative value on error |
| 195 | */ |
| 196 | int pci_enable_pri(struct pci_dev *pdev, u32 reqs) |
| 197 | { |
| 198 | u16 control, status; |
| 199 | u32 max_requests; |
Kuppuswamy Sathyanarayanan | c065190 | 2019-09-05 14:31:45 -0500 | [diff] [blame] | 200 | int pri = pdev->pri_cap; |
Joerg Roedel | c320b97 | 2011-09-27 15:57:15 +0200 | [diff] [blame] | 201 | |
Kuppuswamy Sathyanarayanan | 9bf49e3 | 2019-09-05 14:31:42 -0500 | [diff] [blame] | 202 | /* |
| 203 | * VFs must not implement the PRI Capability. If their PF |
| 204 | * implements PRI, it is shared by the VFs, so if the PF PRI is |
| 205 | * enabled, it is also enabled for the VF. |
| 206 | */ |
| 207 | if (pdev->is_virtfn) { |
| 208 | if (pci_physfn(pdev)->pri_enabled) |
| 209 | return 0; |
| 210 | return -EINVAL; |
| 211 | } |
| 212 | |
Jean-Philippe Brucker | a4f4fa6 | 2017-05-30 09:25:48 -0700 | [diff] [blame] | 213 | if (WARN_ON(pdev->pri_enabled)) |
| 214 | return -EBUSY; |
| 215 | |
Kuppuswamy Sathyanarayanan | c065190 | 2019-09-05 14:31:45 -0500 | [diff] [blame] | 216 | if (!pri) |
Joerg Roedel | c320b97 | 2011-09-27 15:57:15 +0200 | [diff] [blame] | 217 | return -EINVAL; |
| 218 | |
Kuppuswamy Sathyanarayanan | c065190 | 2019-09-05 14:31:45 -0500 | [diff] [blame] | 219 | pci_read_config_word(pdev, pri + PCI_PRI_STATUS, &status); |
CQ Tang | 4ebeb1e | 2017-05-30 09:25:49 -0700 | [diff] [blame] | 220 | if (!(status & PCI_PRI_STATUS_STOPPED)) |
Joerg Roedel | c320b97 | 2011-09-27 15:57:15 +0200 | [diff] [blame] | 221 | return -EBUSY; |
| 222 | |
Kuppuswamy Sathyanarayanan | c065190 | 2019-09-05 14:31:45 -0500 | [diff] [blame] | 223 | pci_read_config_dword(pdev, pri + PCI_PRI_MAX_REQ, &max_requests); |
Joerg Roedel | c320b97 | 2011-09-27 15:57:15 +0200 | [diff] [blame] | 224 | reqs = min(max_requests, reqs); |
CQ Tang | 4ebeb1e | 2017-05-30 09:25:49 -0700 | [diff] [blame] | 225 | pdev->pri_reqs_alloc = reqs; |
Kuppuswamy Sathyanarayanan | c065190 | 2019-09-05 14:31:45 -0500 | [diff] [blame] | 226 | pci_write_config_dword(pdev, pri + PCI_PRI_ALLOC_REQ, reqs); |
Joerg Roedel | c320b97 | 2011-09-27 15:57:15 +0200 | [diff] [blame] | 227 | |
CQ Tang | 4ebeb1e | 2017-05-30 09:25:49 -0700 | [diff] [blame] | 228 | control = PCI_PRI_CTRL_ENABLE; |
Kuppuswamy Sathyanarayanan | c065190 | 2019-09-05 14:31:45 -0500 | [diff] [blame] | 229 | pci_write_config_word(pdev, pri + PCI_PRI_CTRL, control); |
Joerg Roedel | c320b97 | 2011-09-27 15:57:15 +0200 | [diff] [blame] | 230 | |
Jean-Philippe Brucker | a4f4fa6 | 2017-05-30 09:25:48 -0700 | [diff] [blame] | 231 | pdev->pri_enabled = 1; |
| 232 | |
Joerg Roedel | c320b97 | 2011-09-27 15:57:15 +0200 | [diff] [blame] | 233 | return 0; |
| 234 | } |
Joerg Roedel | c320b97 | 2011-09-27 15:57:15 +0200 | [diff] [blame] | 235 | |
| 236 | /** |
| 237 | * pci_disable_pri - Disable PRI capability |
| 238 | * @pdev: PCI device structure |
| 239 | * |
| 240 | * Only clears the enabled-bit, regardless of its former value |
| 241 | */ |
| 242 | void pci_disable_pri(struct pci_dev *pdev) |
| 243 | { |
| 244 | u16 control; |
Kuppuswamy Sathyanarayanan | c065190 | 2019-09-05 14:31:45 -0500 | [diff] [blame] | 245 | int pri = pdev->pri_cap; |
Joerg Roedel | c320b97 | 2011-09-27 15:57:15 +0200 | [diff] [blame] | 246 | |
Kuppuswamy Sathyanarayanan | 9bf49e3 | 2019-09-05 14:31:42 -0500 | [diff] [blame] | 247 | /* VFs share the PF PRI */ |
| 248 | if (pdev->is_virtfn) |
| 249 | return; |
| 250 | |
Jean-Philippe Brucker | a4f4fa6 | 2017-05-30 09:25:48 -0700 | [diff] [blame] | 251 | if (WARN_ON(!pdev->pri_enabled)) |
| 252 | return; |
| 253 | |
Kuppuswamy Sathyanarayanan | c065190 | 2019-09-05 14:31:45 -0500 | [diff] [blame] | 254 | if (!pri) |
Joerg Roedel | c320b97 | 2011-09-27 15:57:15 +0200 | [diff] [blame] | 255 | return; |
| 256 | |
Kuppuswamy Sathyanarayanan | c065190 | 2019-09-05 14:31:45 -0500 | [diff] [blame] | 257 | pci_read_config_word(pdev, pri + PCI_PRI_CTRL, &control); |
Alex Williamson | 91f57d5 | 2011-11-11 10:07:36 -0700 | [diff] [blame] | 258 | control &= ~PCI_PRI_CTRL_ENABLE; |
Kuppuswamy Sathyanarayanan | c065190 | 2019-09-05 14:31:45 -0500 | [diff] [blame] | 259 | pci_write_config_word(pdev, pri + PCI_PRI_CTRL, control); |
Jean-Philippe Brucker | a4f4fa6 | 2017-05-30 09:25:48 -0700 | [diff] [blame] | 260 | |
| 261 | pdev->pri_enabled = 0; |
Joerg Roedel | c320b97 | 2011-09-27 15:57:15 +0200 | [diff] [blame] | 262 | } |
| 263 | EXPORT_SYMBOL_GPL(pci_disable_pri); |
| 264 | |
| 265 | /** |
CQ Tang | 4ebeb1e | 2017-05-30 09:25:49 -0700 | [diff] [blame] | 266 | * pci_restore_pri_state - Restore PRI |
| 267 | * @pdev: PCI device structure |
| 268 | */ |
| 269 | void pci_restore_pri_state(struct pci_dev *pdev) |
| 270 | { |
| 271 | u16 control = PCI_PRI_CTRL_ENABLE; |
| 272 | u32 reqs = pdev->pri_reqs_alloc; |
Kuppuswamy Sathyanarayanan | c065190 | 2019-09-05 14:31:45 -0500 | [diff] [blame] | 273 | int pri = pdev->pri_cap; |
CQ Tang | 4ebeb1e | 2017-05-30 09:25:49 -0700 | [diff] [blame] | 274 | |
Kuppuswamy Sathyanarayanan | 9bf49e3 | 2019-09-05 14:31:42 -0500 | [diff] [blame] | 275 | if (pdev->is_virtfn) |
| 276 | return; |
| 277 | |
CQ Tang | 4ebeb1e | 2017-05-30 09:25:49 -0700 | [diff] [blame] | 278 | if (!pdev->pri_enabled) |
| 279 | return; |
| 280 | |
Kuppuswamy Sathyanarayanan | c065190 | 2019-09-05 14:31:45 -0500 | [diff] [blame] | 281 | if (!pri) |
CQ Tang | 4ebeb1e | 2017-05-30 09:25:49 -0700 | [diff] [blame] | 282 | return; |
| 283 | |
Kuppuswamy Sathyanarayanan | c065190 | 2019-09-05 14:31:45 -0500 | [diff] [blame] | 284 | pci_write_config_dword(pdev, pri + PCI_PRI_ALLOC_REQ, reqs); |
| 285 | pci_write_config_word(pdev, pri + PCI_PRI_CTRL, control); |
CQ Tang | 4ebeb1e | 2017-05-30 09:25:49 -0700 | [diff] [blame] | 286 | } |
CQ Tang | 4ebeb1e | 2017-05-30 09:25:49 -0700 | [diff] [blame] | 287 | |
| 288 | /** |
Joerg Roedel | c320b97 | 2011-09-27 15:57:15 +0200 | [diff] [blame] | 289 | * pci_reset_pri - Resets device's PRI state |
| 290 | * @pdev: PCI device structure |
| 291 | * |
| 292 | * The PRI capability must be disabled before this function is called. |
| 293 | * Returns 0 on success, negative value on error. |
| 294 | */ |
| 295 | int pci_reset_pri(struct pci_dev *pdev) |
| 296 | { |
| 297 | u16 control; |
Kuppuswamy Sathyanarayanan | c065190 | 2019-09-05 14:31:45 -0500 | [diff] [blame] | 298 | int pri = pdev->pri_cap; |
Joerg Roedel | c320b97 | 2011-09-27 15:57:15 +0200 | [diff] [blame] | 299 | |
Kuppuswamy Sathyanarayanan | 9bf49e3 | 2019-09-05 14:31:42 -0500 | [diff] [blame] | 300 | if (pdev->is_virtfn) |
| 301 | return 0; |
| 302 | |
Jean-Philippe Brucker | a4f4fa6 | 2017-05-30 09:25:48 -0700 | [diff] [blame] | 303 | if (WARN_ON(pdev->pri_enabled)) |
| 304 | return -EBUSY; |
| 305 | |
Kuppuswamy Sathyanarayanan | c065190 | 2019-09-05 14:31:45 -0500 | [diff] [blame] | 306 | if (!pri) |
Joerg Roedel | c320b97 | 2011-09-27 15:57:15 +0200 | [diff] [blame] | 307 | return -EINVAL; |
| 308 | |
CQ Tang | 4ebeb1e | 2017-05-30 09:25:49 -0700 | [diff] [blame] | 309 | control = PCI_PRI_CTRL_RESET; |
Kuppuswamy Sathyanarayanan | c065190 | 2019-09-05 14:31:45 -0500 | [diff] [blame] | 310 | pci_write_config_word(pdev, pri + PCI_PRI_CTRL, control); |
Joerg Roedel | c320b97 | 2011-09-27 15:57:15 +0200 | [diff] [blame] | 311 | |
| 312 | return 0; |
| 313 | } |
Bjorn Helgaas | 8cbb8a9 | 2019-10-09 14:54:01 -0500 | [diff] [blame] | 314 | |
| 315 | /** |
| 316 | * pci_prg_resp_pasid_required - Return PRG Response PASID Required bit |
| 317 | * status. |
| 318 | * @pdev: PCI device structure |
| 319 | * |
| 320 | * Returns 1 if PASID is required in PRG Response Message, 0 otherwise. |
| 321 | */ |
| 322 | int pci_prg_resp_pasid_required(struct pci_dev *pdev) |
| 323 | { |
Kuppuswamy Sathyanarayanan | 9bf49e3 | 2019-09-05 14:31:42 -0500 | [diff] [blame] | 324 | if (pdev->is_virtfn) |
| 325 | pdev = pci_physfn(pdev); |
| 326 | |
Bjorn Helgaas | e5adf79 | 2019-10-09 16:07:51 -0500 | [diff] [blame] | 327 | return pdev->pasid_required; |
Bjorn Helgaas | 8cbb8a9 | 2019-10-09 14:54:01 -0500 | [diff] [blame] | 328 | } |
Ashok Raj | 3f9a7a1 | 2020-07-23 15:37:29 -0700 | [diff] [blame] | 329 | |
| 330 | /** |
| 331 | * pci_pri_supported - Check if PRI is supported. |
| 332 | * @pdev: PCI device structure |
| 333 | * |
| 334 | * Returns true if PRI capability is present, false otherwise. |
| 335 | */ |
| 336 | bool pci_pri_supported(struct pci_dev *pdev) |
| 337 | { |
| 338 | /* VFs share the PF PRI */ |
| 339 | if (pci_physfn(pdev)->pri_cap) |
| 340 | return true; |
| 341 | return false; |
| 342 | } |
| 343 | EXPORT_SYMBOL_GPL(pci_pri_supported); |
Joerg Roedel | c320b97 | 2011-09-27 15:57:15 +0200 | [diff] [blame] | 344 | #endif /* CONFIG_PCI_PRI */ |
Joerg Roedel | 086ac11 | 2011-09-27 15:57:16 +0200 | [diff] [blame] | 345 | |
| 346 | #ifdef CONFIG_PCI_PASID |
Kuppuswamy Sathyanarayanan | 751035b | 2019-09-05 14:31:46 -0500 | [diff] [blame] | 347 | void pci_pasid_init(struct pci_dev *pdev) |
| 348 | { |
| 349 | pdev->pasid_cap = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PASID); |
| 350 | } |
| 351 | |
Joerg Roedel | 086ac11 | 2011-09-27 15:57:16 +0200 | [diff] [blame] | 352 | /** |
| 353 | * pci_enable_pasid - Enable the PASID capability |
| 354 | * @pdev: PCI device structure |
| 355 | * @features: Features to enable |
| 356 | * |
| 357 | * Returns 0 on success, negative value on error. This function checks |
| 358 | * whether the features are actually supported by the device and returns |
| 359 | * an error if not. |
| 360 | */ |
| 361 | int pci_enable_pasid(struct pci_dev *pdev, int features) |
| 362 | { |
| 363 | u16 control, supported; |
Kuppuswamy Sathyanarayanan | 751035b | 2019-09-05 14:31:46 -0500 | [diff] [blame] | 364 | int pasid = pdev->pasid_cap; |
Joerg Roedel | 086ac11 | 2011-09-27 15:57:16 +0200 | [diff] [blame] | 365 | |
Kuppuswamy Sathyanarayanan | 2b0ae7c | 2019-09-05 14:31:43 -0500 | [diff] [blame] | 366 | /* |
| 367 | * VFs must not implement the PASID Capability, but if a PF |
| 368 | * supports PASID, its VFs share the PF PASID configuration. |
| 369 | */ |
| 370 | if (pdev->is_virtfn) { |
| 371 | if (pci_physfn(pdev)->pasid_enabled) |
| 372 | return 0; |
| 373 | return -EINVAL; |
| 374 | } |
| 375 | |
Jean-Philippe Brucker | a4f4fa6 | 2017-05-30 09:25:48 -0700 | [diff] [blame] | 376 | if (WARN_ON(pdev->pasid_enabled)) |
| 377 | return -EBUSY; |
| 378 | |
Zhangfei Gao | 8c09e89 | 2021-07-13 10:54:34 +0800 | [diff] [blame] | 379 | if (!pdev->eetlp_prefix_path && !pdev->pasid_no_tlp) |
Sinan Kaya | 7ce3f91 | 2018-06-30 11:24:24 -0400 | [diff] [blame] | 380 | return -EINVAL; |
| 381 | |
Kuppuswamy Sathyanarayanan | 751035b | 2019-09-05 14:31:46 -0500 | [diff] [blame] | 382 | if (!pasid) |
Joerg Roedel | 086ac11 | 2011-09-27 15:57:16 +0200 | [diff] [blame] | 383 | return -EINVAL; |
| 384 | |
Kuppuswamy Sathyanarayanan | 751035b | 2019-09-05 14:31:46 -0500 | [diff] [blame] | 385 | pci_read_config_word(pdev, pasid + PCI_PASID_CAP, &supported); |
Alex Williamson | 91f57d5 | 2011-11-11 10:07:36 -0700 | [diff] [blame] | 386 | supported &= PCI_PASID_CAP_EXEC | PCI_PASID_CAP_PRIV; |
Joerg Roedel | 086ac11 | 2011-09-27 15:57:16 +0200 | [diff] [blame] | 387 | |
| 388 | /* User wants to enable anything unsupported? */ |
| 389 | if ((supported & features) != features) |
| 390 | return -EINVAL; |
| 391 | |
Alex Williamson | 91f57d5 | 2011-11-11 10:07:36 -0700 | [diff] [blame] | 392 | control = PCI_PASID_CTRL_ENABLE | features; |
CQ Tang | 4ebeb1e | 2017-05-30 09:25:49 -0700 | [diff] [blame] | 393 | pdev->pasid_features = features; |
Joerg Roedel | 086ac11 | 2011-09-27 15:57:16 +0200 | [diff] [blame] | 394 | |
Kuppuswamy Sathyanarayanan | 751035b | 2019-09-05 14:31:46 -0500 | [diff] [blame] | 395 | pci_write_config_word(pdev, pasid + PCI_PASID_CTRL, control); |
Joerg Roedel | 086ac11 | 2011-09-27 15:57:16 +0200 | [diff] [blame] | 396 | |
Jean-Philippe Brucker | a4f4fa6 | 2017-05-30 09:25:48 -0700 | [diff] [blame] | 397 | pdev->pasid_enabled = 1; |
| 398 | |
Joerg Roedel | 086ac11 | 2011-09-27 15:57:16 +0200 | [diff] [blame] | 399 | return 0; |
| 400 | } |
Jean-Philippe Brucker | 7682ce2 | 2020-02-24 17:58:41 +0100 | [diff] [blame] | 401 | EXPORT_SYMBOL_GPL(pci_enable_pasid); |
Joerg Roedel | 086ac11 | 2011-09-27 15:57:16 +0200 | [diff] [blame] | 402 | |
| 403 | /** |
| 404 | * pci_disable_pasid - Disable the PASID capability |
| 405 | * @pdev: PCI device structure |
Joerg Roedel | 086ac11 | 2011-09-27 15:57:16 +0200 | [diff] [blame] | 406 | */ |
| 407 | void pci_disable_pasid(struct pci_dev *pdev) |
| 408 | { |
| 409 | u16 control = 0; |
Kuppuswamy Sathyanarayanan | 751035b | 2019-09-05 14:31:46 -0500 | [diff] [blame] | 410 | int pasid = pdev->pasid_cap; |
Joerg Roedel | 086ac11 | 2011-09-27 15:57:16 +0200 | [diff] [blame] | 411 | |
Kuppuswamy Sathyanarayanan | 2b0ae7c | 2019-09-05 14:31:43 -0500 | [diff] [blame] | 412 | /* VFs share the PF PASID configuration */ |
| 413 | if (pdev->is_virtfn) |
| 414 | return; |
| 415 | |
Jean-Philippe Brucker | a4f4fa6 | 2017-05-30 09:25:48 -0700 | [diff] [blame] | 416 | if (WARN_ON(!pdev->pasid_enabled)) |
| 417 | return; |
| 418 | |
Kuppuswamy Sathyanarayanan | 751035b | 2019-09-05 14:31:46 -0500 | [diff] [blame] | 419 | if (!pasid) |
Joerg Roedel | 086ac11 | 2011-09-27 15:57:16 +0200 | [diff] [blame] | 420 | return; |
| 421 | |
Kuppuswamy Sathyanarayanan | 751035b | 2019-09-05 14:31:46 -0500 | [diff] [blame] | 422 | pci_write_config_word(pdev, pasid + PCI_PASID_CTRL, control); |
Jean-Philippe Brucker | a4f4fa6 | 2017-05-30 09:25:48 -0700 | [diff] [blame] | 423 | |
| 424 | pdev->pasid_enabled = 0; |
Joerg Roedel | 086ac11 | 2011-09-27 15:57:16 +0200 | [diff] [blame] | 425 | } |
Jean-Philippe Brucker | 7682ce2 | 2020-02-24 17:58:41 +0100 | [diff] [blame] | 426 | EXPORT_SYMBOL_GPL(pci_disable_pasid); |
Joerg Roedel | 086ac11 | 2011-09-27 15:57:16 +0200 | [diff] [blame] | 427 | |
| 428 | /** |
CQ Tang | 4ebeb1e | 2017-05-30 09:25:49 -0700 | [diff] [blame] | 429 | * pci_restore_pasid_state - Restore PASID capabilities |
| 430 | * @pdev: PCI device structure |
| 431 | */ |
| 432 | void pci_restore_pasid_state(struct pci_dev *pdev) |
| 433 | { |
| 434 | u16 control; |
Kuppuswamy Sathyanarayanan | 751035b | 2019-09-05 14:31:46 -0500 | [diff] [blame] | 435 | int pasid = pdev->pasid_cap; |
CQ Tang | 4ebeb1e | 2017-05-30 09:25:49 -0700 | [diff] [blame] | 436 | |
Kuppuswamy Sathyanarayanan | 2b0ae7c | 2019-09-05 14:31:43 -0500 | [diff] [blame] | 437 | if (pdev->is_virtfn) |
| 438 | return; |
| 439 | |
CQ Tang | 4ebeb1e | 2017-05-30 09:25:49 -0700 | [diff] [blame] | 440 | if (!pdev->pasid_enabled) |
| 441 | return; |
| 442 | |
Kuppuswamy Sathyanarayanan | 751035b | 2019-09-05 14:31:46 -0500 | [diff] [blame] | 443 | if (!pasid) |
CQ Tang | 4ebeb1e | 2017-05-30 09:25:49 -0700 | [diff] [blame] | 444 | return; |
| 445 | |
| 446 | control = PCI_PASID_CTRL_ENABLE | pdev->pasid_features; |
Kuppuswamy Sathyanarayanan | 751035b | 2019-09-05 14:31:46 -0500 | [diff] [blame] | 447 | pci_write_config_word(pdev, pasid + PCI_PASID_CTRL, control); |
CQ Tang | 4ebeb1e | 2017-05-30 09:25:49 -0700 | [diff] [blame] | 448 | } |
CQ Tang | 4ebeb1e | 2017-05-30 09:25:49 -0700 | [diff] [blame] | 449 | |
| 450 | /** |
Joerg Roedel | 086ac11 | 2011-09-27 15:57:16 +0200 | [diff] [blame] | 451 | * pci_pasid_features - Check which PASID features are supported |
| 452 | * @pdev: PCI device structure |
| 453 | * |
| 454 | * Returns a negative value when no PASI capability is present. |
| 455 | * Otherwise is returns a bitmask with supported features. Current |
| 456 | * features reported are: |
Alex Williamson | 91f57d5 | 2011-11-11 10:07:36 -0700 | [diff] [blame] | 457 | * PCI_PASID_CAP_EXEC - Execute permission supported |
Bjorn Helgaas | f762598 | 2013-11-14 11:28:18 -0700 | [diff] [blame] | 458 | * PCI_PASID_CAP_PRIV - Privileged mode supported |
Joerg Roedel | 086ac11 | 2011-09-27 15:57:16 +0200 | [diff] [blame] | 459 | */ |
| 460 | int pci_pasid_features(struct pci_dev *pdev) |
| 461 | { |
| 462 | u16 supported; |
Kuppuswamy Sathyanarayanan | 2e34673 | 2020-01-29 11:14:00 -0800 | [diff] [blame] | 463 | int pasid; |
Joerg Roedel | 086ac11 | 2011-09-27 15:57:16 +0200 | [diff] [blame] | 464 | |
Kuppuswamy Sathyanarayanan | 2b0ae7c | 2019-09-05 14:31:43 -0500 | [diff] [blame] | 465 | if (pdev->is_virtfn) |
| 466 | pdev = pci_physfn(pdev); |
| 467 | |
Kuppuswamy Sathyanarayanan | 2e34673 | 2020-01-29 11:14:00 -0800 | [diff] [blame] | 468 | pasid = pdev->pasid_cap; |
Kuppuswamy Sathyanarayanan | 751035b | 2019-09-05 14:31:46 -0500 | [diff] [blame] | 469 | if (!pasid) |
Joerg Roedel | 086ac11 | 2011-09-27 15:57:16 +0200 | [diff] [blame] | 470 | return -EINVAL; |
| 471 | |
Kuppuswamy Sathyanarayanan | 751035b | 2019-09-05 14:31:46 -0500 | [diff] [blame] | 472 | pci_read_config_word(pdev, pasid + PCI_PASID_CAP, &supported); |
Joerg Roedel | 086ac11 | 2011-09-27 15:57:16 +0200 | [diff] [blame] | 473 | |
Alex Williamson | 91f57d5 | 2011-11-11 10:07:36 -0700 | [diff] [blame] | 474 | supported &= PCI_PASID_CAP_EXEC | PCI_PASID_CAP_PRIV; |
Joerg Roedel | 086ac11 | 2011-09-27 15:57:16 +0200 | [diff] [blame] | 475 | |
| 476 | return supported; |
| 477 | } |
Jean-Philippe Brucker | 7682ce2 | 2020-02-24 17:58:41 +0100 | [diff] [blame] | 478 | EXPORT_SYMBOL_GPL(pci_pasid_features); |
Joerg Roedel | 086ac11 | 2011-09-27 15:57:16 +0200 | [diff] [blame] | 479 | |
| 480 | #define PASID_NUMBER_SHIFT 8 |
| 481 | #define PASID_NUMBER_MASK (0x1f << PASID_NUMBER_SHIFT) |
| 482 | /** |
Krzysztof WilczyĆski | 43395d9 | 2021-03-11 00:17:17 +0000 | [diff] [blame] | 483 | * pci_max_pasids - Get maximum number of PASIDs supported by device |
Joerg Roedel | 086ac11 | 2011-09-27 15:57:16 +0200 | [diff] [blame] | 484 | * @pdev: PCI device structure |
| 485 | * |
| 486 | * Returns negative value when PASID capability is not present. |
Bjorn Helgaas | f6b6aef | 2019-05-30 08:05:58 -0500 | [diff] [blame] | 487 | * Otherwise it returns the number of supported PASIDs. |
Joerg Roedel | 086ac11 | 2011-09-27 15:57:16 +0200 | [diff] [blame] | 488 | */ |
| 489 | int pci_max_pasids(struct pci_dev *pdev) |
| 490 | { |
| 491 | u16 supported; |
Kuppuswamy Sathyanarayanan | 2e34673 | 2020-01-29 11:14:00 -0800 | [diff] [blame] | 492 | int pasid; |
Joerg Roedel | 086ac11 | 2011-09-27 15:57:16 +0200 | [diff] [blame] | 493 | |
Kuppuswamy Sathyanarayanan | 2b0ae7c | 2019-09-05 14:31:43 -0500 | [diff] [blame] | 494 | if (pdev->is_virtfn) |
| 495 | pdev = pci_physfn(pdev); |
| 496 | |
Kuppuswamy Sathyanarayanan | 2e34673 | 2020-01-29 11:14:00 -0800 | [diff] [blame] | 497 | pasid = pdev->pasid_cap; |
Kuppuswamy Sathyanarayanan | 751035b | 2019-09-05 14:31:46 -0500 | [diff] [blame] | 498 | if (!pasid) |
Joerg Roedel | 086ac11 | 2011-09-27 15:57:16 +0200 | [diff] [blame] | 499 | return -EINVAL; |
| 500 | |
Kuppuswamy Sathyanarayanan | 751035b | 2019-09-05 14:31:46 -0500 | [diff] [blame] | 501 | pci_read_config_word(pdev, pasid + PCI_PASID_CAP, &supported); |
Joerg Roedel | 086ac11 | 2011-09-27 15:57:16 +0200 | [diff] [blame] | 502 | |
| 503 | supported = (supported & PASID_NUMBER_MASK) >> PASID_NUMBER_SHIFT; |
| 504 | |
| 505 | return (1 << supported); |
| 506 | } |
Jean-Philippe Brucker | 7682ce2 | 2020-02-24 17:58:41 +0100 | [diff] [blame] | 507 | EXPORT_SYMBOL_GPL(pci_max_pasids); |
Joerg Roedel | 086ac11 | 2011-09-27 15:57:16 +0200 | [diff] [blame] | 508 | #endif /* CONFIG_PCI_PASID */ |