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