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