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 | /* |
| 3 | * drivers/pci/ats.c |
| 4 | * |
| 5 | * Copyright (C) 2009 Intel Corporation, Yu Zhao <yu.zhao@intel.com> |
Joerg Roedel | c320b97 | 2011-09-27 15:57:15 +0200 | [diff] [blame] | 6 | * Copyright (C) 2011 Advanced Micro Devices, |
Joerg Roedel | db3c33c | 2011-09-27 15:57:13 +0200 | [diff] [blame] | 7 | * |
| 8 | * PCI Express I/O Virtualization (IOV) support. |
| 9 | * Address Translation Service 1.0 |
Joerg Roedel | c320b97 | 2011-09-27 15:57:15 +0200 | [diff] [blame] | 10 | * Page Request Interface added by Joerg Roedel <joerg.roedel@amd.com> |
Joerg Roedel | 086ac11 | 2011-09-27 15:57:16 +0200 | [diff] [blame] | 11 | * PASID support added by Joerg Roedel <joerg.roedel@amd.com> |
Joerg Roedel | db3c33c | 2011-09-27 15:57:13 +0200 | [diff] [blame] | 12 | */ |
| 13 | |
Paul Gortmaker | 363c75d | 2011-05-27 09:37:25 -0400 | [diff] [blame] | 14 | #include <linux/export.h> |
Joerg Roedel | db3c33c | 2011-09-27 15:57:13 +0200 | [diff] [blame] | 15 | #include <linux/pci-ats.h> |
| 16 | #include <linux/pci.h> |
James Bottomley | 8c45194 | 2011-11-29 19:20:23 +0000 | [diff] [blame] | 17 | #include <linux/slab.h> |
Joerg Roedel | db3c33c | 2011-09-27 15:57:13 +0200 | [diff] [blame] | 18 | |
| 19 | #include "pci.h" |
| 20 | |
Bjorn Helgaas | afdd596 | 2015-07-17 15:35:18 -0500 | [diff] [blame] | 21 | void pci_ats_init(struct pci_dev *dev) |
Joerg Roedel | db3c33c | 2011-09-27 15:57:13 +0200 | [diff] [blame] | 22 | { |
| 23 | int pos; |
Joerg Roedel | db3c33c | 2011-09-27 15:57:13 +0200 | [diff] [blame] | 24 | |
| 25 | pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ATS); |
| 26 | if (!pos) |
Bjorn Helgaas | edc90fe | 2015-07-17 15:05:46 -0500 | [diff] [blame] | 27 | return; |
Joerg Roedel | db3c33c | 2011-09-27 15:57:13 +0200 | [diff] [blame] | 28 | |
Bjorn Helgaas | d544d75 | 2015-07-17 15:15:19 -0500 | [diff] [blame] | 29 | dev->ats_cap = pos; |
Joerg Roedel | db3c33c | 2011-09-27 15:57:13 +0200 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | /** |
| 33 | * pci_enable_ats - enable the ATS capability |
| 34 | * @dev: the PCI device |
| 35 | * @ps: the IOMMU page shift |
| 36 | * |
| 37 | * Returns 0 on success, or negative on failure. |
| 38 | */ |
| 39 | int pci_enable_ats(struct pci_dev *dev, int ps) |
| 40 | { |
Joerg Roedel | db3c33c | 2011-09-27 15:57:13 +0200 | [diff] [blame] | 41 | u16 ctrl; |
Bjorn Helgaas | c39127d | 2015-07-17 15:38:13 -0500 | [diff] [blame] | 42 | struct pci_dev *pdev; |
Joerg Roedel | db3c33c | 2011-09-27 15:57:13 +0200 | [diff] [blame] | 43 | |
Bjorn Helgaas | d544d75 | 2015-07-17 15:15:19 -0500 | [diff] [blame] | 44 | if (!dev->ats_cap) |
Bjorn Helgaas | edc90fe | 2015-07-17 15:05:46 -0500 | [diff] [blame] | 45 | return -EINVAL; |
| 46 | |
Bjorn Helgaas | f7ef134 | 2015-07-20 09:23:37 -0500 | [diff] [blame] | 47 | if (WARN_ON(dev->ats_enabled)) |
Bjorn Helgaas | a021f30 | 2015-07-17 15:43:27 -0500 | [diff] [blame] | 48 | return -EBUSY; |
| 49 | |
Joerg Roedel | db3c33c | 2011-09-27 15:57:13 +0200 | [diff] [blame] | 50 | if (ps < PCI_ATS_MIN_STU) |
| 51 | return -EINVAL; |
| 52 | |
Bjorn Helgaas | edc90fe | 2015-07-17 15:05:46 -0500 | [diff] [blame] | 53 | /* |
| 54 | * Note that enabling ATS on a VF fails unless it's already enabled |
| 55 | * with the same STU on the PF. |
| 56 | */ |
Joerg Roedel | db3c33c | 2011-09-27 15:57:13 +0200 | [diff] [blame] | 57 | ctrl = PCI_ATS_CTRL_ENABLE; |
Bjorn Helgaas | edc90fe | 2015-07-17 15:05:46 -0500 | [diff] [blame] | 58 | if (dev->is_virtfn) { |
Bjorn Helgaas | c39127d | 2015-07-17 15:38:13 -0500 | [diff] [blame] | 59 | pdev = pci_physfn(dev); |
Bjorn Helgaas | d544d75 | 2015-07-17 15:15:19 -0500 | [diff] [blame] | 60 | if (pdev->ats_stu != ps) |
Bjorn Helgaas | edc90fe | 2015-07-17 15:05:46 -0500 | [diff] [blame] | 61 | return -EINVAL; |
| 62 | |
Bjorn Helgaas | d544d75 | 2015-07-17 15:15:19 -0500 | [diff] [blame] | 63 | atomic_inc(&pdev->ats_ref_cnt); /* count enabled VFs */ |
Bjorn Helgaas | edc90fe | 2015-07-17 15:05:46 -0500 | [diff] [blame] | 64 | } else { |
Bjorn Helgaas | d544d75 | 2015-07-17 15:15:19 -0500 | [diff] [blame] | 65 | dev->ats_stu = ps; |
| 66 | ctrl |= PCI_ATS_CTRL_STU(dev->ats_stu - PCI_ATS_MIN_STU); |
Bjorn Helgaas | edc90fe | 2015-07-17 15:05:46 -0500 | [diff] [blame] | 67 | } |
Bjorn Helgaas | d544d75 | 2015-07-17 15:15:19 -0500 | [diff] [blame] | 68 | pci_write_config_word(dev, dev->ats_cap + PCI_ATS_CTRL, ctrl); |
Joerg Roedel | db3c33c | 2011-09-27 15:57:13 +0200 | [diff] [blame] | 69 | |
Bjorn Helgaas | d544d75 | 2015-07-17 15:15:19 -0500 | [diff] [blame] | 70 | dev->ats_enabled = 1; |
Joerg Roedel | db3c33c | 2011-09-27 15:57:13 +0200 | [diff] [blame] | 71 | return 0; |
| 72 | } |
Joerg Roedel | d4c0636 | 2011-09-27 15:57:14 +0200 | [diff] [blame] | 73 | EXPORT_SYMBOL_GPL(pci_enable_ats); |
Joerg Roedel | db3c33c | 2011-09-27 15:57:13 +0200 | [diff] [blame] | 74 | |
| 75 | /** |
| 76 | * pci_disable_ats - disable the ATS capability |
| 77 | * @dev: the PCI device |
| 78 | */ |
| 79 | void pci_disable_ats(struct pci_dev *dev) |
| 80 | { |
Bjorn Helgaas | c39127d | 2015-07-17 15:38:13 -0500 | [diff] [blame] | 81 | struct pci_dev *pdev; |
Joerg Roedel | db3c33c | 2011-09-27 15:57:13 +0200 | [diff] [blame] | 82 | u16 ctrl; |
| 83 | |
Bjorn Helgaas | f7ef134 | 2015-07-20 09:23:37 -0500 | [diff] [blame] | 84 | if (WARN_ON(!dev->ats_enabled)) |
Bjorn Helgaas | a021f30 | 2015-07-17 15:43:27 -0500 | [diff] [blame] | 85 | return; |
Joerg Roedel | db3c33c | 2011-09-27 15:57:13 +0200 | [diff] [blame] | 86 | |
Bjorn Helgaas | d544d75 | 2015-07-17 15:15:19 -0500 | [diff] [blame] | 87 | if (atomic_read(&dev->ats_ref_cnt)) |
Bjorn Helgaas | edc90fe | 2015-07-17 15:05:46 -0500 | [diff] [blame] | 88 | return; /* VFs still enabled */ |
| 89 | |
| 90 | if (dev->is_virtfn) { |
Bjorn Helgaas | c39127d | 2015-07-17 15:38:13 -0500 | [diff] [blame] | 91 | pdev = pci_physfn(dev); |
Bjorn Helgaas | d544d75 | 2015-07-17 15:15:19 -0500 | [diff] [blame] | 92 | atomic_dec(&pdev->ats_ref_cnt); |
Bjorn Helgaas | edc90fe | 2015-07-17 15:05:46 -0500 | [diff] [blame] | 93 | } |
| 94 | |
Bjorn Helgaas | d544d75 | 2015-07-17 15:15:19 -0500 | [diff] [blame] | 95 | pci_read_config_word(dev, dev->ats_cap + PCI_ATS_CTRL, &ctrl); |
Joerg Roedel | db3c33c | 2011-09-27 15:57:13 +0200 | [diff] [blame] | 96 | ctrl &= ~PCI_ATS_CTRL_ENABLE; |
Bjorn Helgaas | d544d75 | 2015-07-17 15:15:19 -0500 | [diff] [blame] | 97 | pci_write_config_word(dev, dev->ats_cap + PCI_ATS_CTRL, ctrl); |
Joerg Roedel | db3c33c | 2011-09-27 15:57:13 +0200 | [diff] [blame] | 98 | |
Bjorn Helgaas | d544d75 | 2015-07-17 15:15:19 -0500 | [diff] [blame] | 99 | dev->ats_enabled = 0; |
Joerg Roedel | db3c33c | 2011-09-27 15:57:13 +0200 | [diff] [blame] | 100 | } |
Joerg Roedel | d4c0636 | 2011-09-27 15:57:14 +0200 | [diff] [blame] | 101 | EXPORT_SYMBOL_GPL(pci_disable_ats); |
Joerg Roedel | db3c33c | 2011-09-27 15:57:13 +0200 | [diff] [blame] | 102 | |
Hao, Xudong | 1900ca1 | 2011-12-17 21:24:40 +0800 | [diff] [blame] | 103 | void pci_restore_ats_state(struct pci_dev *dev) |
| 104 | { |
| 105 | u16 ctrl; |
| 106 | |
Bjorn Helgaas | f7ef134 | 2015-07-20 09:23:37 -0500 | [diff] [blame] | 107 | if (!dev->ats_enabled) |
Hao, Xudong | 1900ca1 | 2011-12-17 21:24:40 +0800 | [diff] [blame] | 108 | return; |
Hao, Xudong | 1900ca1 | 2011-12-17 21:24:40 +0800 | [diff] [blame] | 109 | |
| 110 | ctrl = PCI_ATS_CTRL_ENABLE; |
| 111 | if (!dev->is_virtfn) |
Bjorn Helgaas | d544d75 | 2015-07-17 15:15:19 -0500 | [diff] [blame] | 112 | ctrl |= PCI_ATS_CTRL_STU(dev->ats_stu - PCI_ATS_MIN_STU); |
| 113 | pci_write_config_word(dev, dev->ats_cap + PCI_ATS_CTRL, ctrl); |
Hao, Xudong | 1900ca1 | 2011-12-17 21:24:40 +0800 | [diff] [blame] | 114 | } |
| 115 | EXPORT_SYMBOL_GPL(pci_restore_ats_state); |
| 116 | |
Joerg Roedel | db3c33c | 2011-09-27 15:57:13 +0200 | [diff] [blame] | 117 | /** |
| 118 | * pci_ats_queue_depth - query the ATS Invalidate Queue Depth |
| 119 | * @dev: the PCI device |
| 120 | * |
| 121 | * Returns the queue depth on success, or negative on failure. |
| 122 | * |
| 123 | * The ATS spec uses 0 in the Invalidate Queue Depth field to |
| 124 | * indicate that the function can accept 32 Invalidate Request. |
| 125 | * But here we use the `real' values (i.e. 1~32) for the Queue |
| 126 | * Depth; and 0 indicates the function shares the Queue with |
| 127 | * other functions (doesn't exclusively own a Queue). |
| 128 | */ |
| 129 | int pci_ats_queue_depth(struct pci_dev *dev) |
| 130 | { |
Bjorn Helgaas | a71f938 | 2015-07-20 09:24:32 -0500 | [diff] [blame] | 131 | u16 cap; |
| 132 | |
Bjorn Helgaas | 3c76539 | 2015-07-17 15:30:26 -0500 | [diff] [blame] | 133 | if (!dev->ats_cap) |
| 134 | return -EINVAL; |
| 135 | |
Joerg Roedel | db3c33c | 2011-09-27 15:57:13 +0200 | [diff] [blame] | 136 | if (dev->is_virtfn) |
| 137 | return 0; |
| 138 | |
Bjorn Helgaas | a71f938 | 2015-07-20 09:24:32 -0500 | [diff] [blame] | 139 | pci_read_config_word(dev, dev->ats_cap + PCI_ATS_CAP, &cap); |
| 140 | return PCI_ATS_CAP_QDEP(cap) ? PCI_ATS_CAP_QDEP(cap) : PCI_ATS_MAX_QDEP; |
Joerg Roedel | db3c33c | 2011-09-27 15:57:13 +0200 | [diff] [blame] | 141 | } |
Joerg Roedel | d4c0636 | 2011-09-27 15:57:14 +0200 | [diff] [blame] | 142 | EXPORT_SYMBOL_GPL(pci_ats_queue_depth); |
Joerg Roedel | c320b97 | 2011-09-27 15:57:15 +0200 | [diff] [blame] | 143 | |
| 144 | #ifdef CONFIG_PCI_PRI |
| 145 | /** |
| 146 | * pci_enable_pri - Enable PRI capability |
| 147 | * @ pdev: PCI device structure |
| 148 | * |
| 149 | * Returns 0 on success, negative value on error |
| 150 | */ |
| 151 | int pci_enable_pri(struct pci_dev *pdev, u32 reqs) |
| 152 | { |
| 153 | u16 control, status; |
| 154 | u32 max_requests; |
| 155 | int pos; |
| 156 | |
Jean-Philippe Brucker | a4f4fa6 | 2017-05-30 09:25:48 -0700 | [diff] [blame] | 157 | if (WARN_ON(pdev->pri_enabled)) |
| 158 | return -EBUSY; |
| 159 | |
Alex Williamson | 69166fb | 2011-11-02 14:07:15 -0600 | [diff] [blame] | 160 | pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI); |
Joerg Roedel | c320b97 | 2011-09-27 15:57:15 +0200 | [diff] [blame] | 161 | if (!pos) |
| 162 | return -EINVAL; |
| 163 | |
Alex Williamson | 91f57d5 | 2011-11-11 10:07:36 -0700 | [diff] [blame] | 164 | pci_read_config_word(pdev, pos + PCI_PRI_STATUS, &status); |
CQ Tang | 4ebeb1e | 2017-05-30 09:25:49 -0700 | [diff] [blame] | 165 | if (!(status & PCI_PRI_STATUS_STOPPED)) |
Joerg Roedel | c320b97 | 2011-09-27 15:57:15 +0200 | [diff] [blame] | 166 | return -EBUSY; |
| 167 | |
Alex Williamson | 91f57d5 | 2011-11-11 10:07:36 -0700 | [diff] [blame] | 168 | pci_read_config_dword(pdev, pos + PCI_PRI_MAX_REQ, &max_requests); |
Joerg Roedel | c320b97 | 2011-09-27 15:57:15 +0200 | [diff] [blame] | 169 | reqs = min(max_requests, reqs); |
CQ Tang | 4ebeb1e | 2017-05-30 09:25:49 -0700 | [diff] [blame] | 170 | pdev->pri_reqs_alloc = reqs; |
Alex Williamson | 91f57d5 | 2011-11-11 10:07:36 -0700 | [diff] [blame] | 171 | pci_write_config_dword(pdev, pos + PCI_PRI_ALLOC_REQ, reqs); |
Joerg Roedel | c320b97 | 2011-09-27 15:57:15 +0200 | [diff] [blame] | 172 | |
CQ Tang | 4ebeb1e | 2017-05-30 09:25:49 -0700 | [diff] [blame] | 173 | control = PCI_PRI_CTRL_ENABLE; |
Alex Williamson | 91f57d5 | 2011-11-11 10:07:36 -0700 | [diff] [blame] | 174 | pci_write_config_word(pdev, pos + PCI_PRI_CTRL, control); |
Joerg Roedel | c320b97 | 2011-09-27 15:57:15 +0200 | [diff] [blame] | 175 | |
Jean-Philippe Brucker | a4f4fa6 | 2017-05-30 09:25:48 -0700 | [diff] [blame] | 176 | pdev->pri_enabled = 1; |
| 177 | |
Joerg Roedel | c320b97 | 2011-09-27 15:57:15 +0200 | [diff] [blame] | 178 | return 0; |
| 179 | } |
| 180 | EXPORT_SYMBOL_GPL(pci_enable_pri); |
| 181 | |
| 182 | /** |
| 183 | * pci_disable_pri - Disable PRI capability |
| 184 | * @pdev: PCI device structure |
| 185 | * |
| 186 | * Only clears the enabled-bit, regardless of its former value |
| 187 | */ |
| 188 | void pci_disable_pri(struct pci_dev *pdev) |
| 189 | { |
| 190 | u16 control; |
| 191 | int pos; |
| 192 | |
Jean-Philippe Brucker | a4f4fa6 | 2017-05-30 09:25:48 -0700 | [diff] [blame] | 193 | if (WARN_ON(!pdev->pri_enabled)) |
| 194 | return; |
| 195 | |
Alex Williamson | 69166fb | 2011-11-02 14:07:15 -0600 | [diff] [blame] | 196 | pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI); |
Joerg Roedel | c320b97 | 2011-09-27 15:57:15 +0200 | [diff] [blame] | 197 | if (!pos) |
| 198 | return; |
| 199 | |
Alex Williamson | 91f57d5 | 2011-11-11 10:07:36 -0700 | [diff] [blame] | 200 | pci_read_config_word(pdev, pos + PCI_PRI_CTRL, &control); |
| 201 | control &= ~PCI_PRI_CTRL_ENABLE; |
| 202 | pci_write_config_word(pdev, pos + PCI_PRI_CTRL, control); |
Jean-Philippe Brucker | a4f4fa6 | 2017-05-30 09:25:48 -0700 | [diff] [blame] | 203 | |
| 204 | pdev->pri_enabled = 0; |
Joerg Roedel | c320b97 | 2011-09-27 15:57:15 +0200 | [diff] [blame] | 205 | } |
| 206 | EXPORT_SYMBOL_GPL(pci_disable_pri); |
| 207 | |
| 208 | /** |
CQ Tang | 4ebeb1e | 2017-05-30 09:25:49 -0700 | [diff] [blame] | 209 | * pci_restore_pri_state - Restore PRI |
| 210 | * @pdev: PCI device structure |
| 211 | */ |
| 212 | void pci_restore_pri_state(struct pci_dev *pdev) |
| 213 | { |
| 214 | u16 control = PCI_PRI_CTRL_ENABLE; |
| 215 | u32 reqs = pdev->pri_reqs_alloc; |
| 216 | int pos; |
| 217 | |
| 218 | if (!pdev->pri_enabled) |
| 219 | return; |
| 220 | |
| 221 | pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI); |
| 222 | if (!pos) |
| 223 | return; |
| 224 | |
| 225 | pci_write_config_dword(pdev, pos + PCI_PRI_ALLOC_REQ, reqs); |
| 226 | pci_write_config_word(pdev, pos + PCI_PRI_CTRL, control); |
| 227 | } |
| 228 | EXPORT_SYMBOL_GPL(pci_restore_pri_state); |
| 229 | |
| 230 | /** |
Joerg Roedel | c320b97 | 2011-09-27 15:57:15 +0200 | [diff] [blame] | 231 | * pci_reset_pri - Resets device's PRI state |
| 232 | * @pdev: PCI device structure |
| 233 | * |
| 234 | * The PRI capability must be disabled before this function is called. |
| 235 | * Returns 0 on success, negative value on error. |
| 236 | */ |
| 237 | int pci_reset_pri(struct pci_dev *pdev) |
| 238 | { |
| 239 | u16 control; |
| 240 | int pos; |
| 241 | |
Jean-Philippe Brucker | a4f4fa6 | 2017-05-30 09:25:48 -0700 | [diff] [blame] | 242 | if (WARN_ON(pdev->pri_enabled)) |
| 243 | return -EBUSY; |
| 244 | |
Alex Williamson | 69166fb | 2011-11-02 14:07:15 -0600 | [diff] [blame] | 245 | pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI); |
Joerg Roedel | c320b97 | 2011-09-27 15:57:15 +0200 | [diff] [blame] | 246 | if (!pos) |
| 247 | return -EINVAL; |
| 248 | |
CQ Tang | 4ebeb1e | 2017-05-30 09:25:49 -0700 | [diff] [blame] | 249 | control = PCI_PRI_CTRL_RESET; |
Alex Williamson | 91f57d5 | 2011-11-11 10:07:36 -0700 | [diff] [blame] | 250 | pci_write_config_word(pdev, pos + PCI_PRI_CTRL, control); |
Joerg Roedel | c320b97 | 2011-09-27 15:57:15 +0200 | [diff] [blame] | 251 | |
| 252 | return 0; |
| 253 | } |
| 254 | EXPORT_SYMBOL_GPL(pci_reset_pri); |
Joerg Roedel | c320b97 | 2011-09-27 15:57:15 +0200 | [diff] [blame] | 255 | #endif /* CONFIG_PCI_PRI */ |
Joerg Roedel | 086ac11 | 2011-09-27 15:57:16 +0200 | [diff] [blame] | 256 | |
| 257 | #ifdef CONFIG_PCI_PASID |
| 258 | /** |
| 259 | * pci_enable_pasid - Enable the PASID capability |
| 260 | * @pdev: PCI device structure |
| 261 | * @features: Features to enable |
| 262 | * |
| 263 | * Returns 0 on success, negative value on error. This function checks |
| 264 | * whether the features are actually supported by the device and returns |
| 265 | * an error if not. |
| 266 | */ |
| 267 | int pci_enable_pasid(struct pci_dev *pdev, int features) |
| 268 | { |
| 269 | u16 control, supported; |
| 270 | int pos; |
| 271 | |
Jean-Philippe Brucker | a4f4fa6 | 2017-05-30 09:25:48 -0700 | [diff] [blame] | 272 | if (WARN_ON(pdev->pasid_enabled)) |
| 273 | return -EBUSY; |
| 274 | |
Alex Williamson | 69166fb | 2011-11-02 14:07:15 -0600 | [diff] [blame] | 275 | pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PASID); |
Joerg Roedel | 086ac11 | 2011-09-27 15:57:16 +0200 | [diff] [blame] | 276 | if (!pos) |
| 277 | return -EINVAL; |
| 278 | |
Alex Williamson | 91f57d5 | 2011-11-11 10:07:36 -0700 | [diff] [blame] | 279 | pci_read_config_word(pdev, pos + PCI_PASID_CAP, &supported); |
Alex Williamson | 91f57d5 | 2011-11-11 10:07:36 -0700 | [diff] [blame] | 280 | supported &= PCI_PASID_CAP_EXEC | PCI_PASID_CAP_PRIV; |
Joerg Roedel | 086ac11 | 2011-09-27 15:57:16 +0200 | [diff] [blame] | 281 | |
| 282 | /* User wants to enable anything unsupported? */ |
| 283 | if ((supported & features) != features) |
| 284 | return -EINVAL; |
| 285 | |
Alex Williamson | 91f57d5 | 2011-11-11 10:07:36 -0700 | [diff] [blame] | 286 | control = PCI_PASID_CTRL_ENABLE | features; |
CQ Tang | 4ebeb1e | 2017-05-30 09:25:49 -0700 | [diff] [blame] | 287 | pdev->pasid_features = features; |
Joerg Roedel | 086ac11 | 2011-09-27 15:57:16 +0200 | [diff] [blame] | 288 | |
Alex Williamson | 91f57d5 | 2011-11-11 10:07:36 -0700 | [diff] [blame] | 289 | pci_write_config_word(pdev, pos + PCI_PASID_CTRL, control); |
Joerg Roedel | 086ac11 | 2011-09-27 15:57:16 +0200 | [diff] [blame] | 290 | |
Jean-Philippe Brucker | a4f4fa6 | 2017-05-30 09:25:48 -0700 | [diff] [blame] | 291 | pdev->pasid_enabled = 1; |
| 292 | |
Joerg Roedel | 086ac11 | 2011-09-27 15:57:16 +0200 | [diff] [blame] | 293 | return 0; |
| 294 | } |
| 295 | EXPORT_SYMBOL_GPL(pci_enable_pasid); |
| 296 | |
| 297 | /** |
| 298 | * pci_disable_pasid - Disable the PASID capability |
| 299 | * @pdev: PCI device structure |
Joerg Roedel | 086ac11 | 2011-09-27 15:57:16 +0200 | [diff] [blame] | 300 | */ |
| 301 | void pci_disable_pasid(struct pci_dev *pdev) |
| 302 | { |
| 303 | u16 control = 0; |
| 304 | int pos; |
| 305 | |
Jean-Philippe Brucker | a4f4fa6 | 2017-05-30 09:25:48 -0700 | [diff] [blame] | 306 | if (WARN_ON(!pdev->pasid_enabled)) |
| 307 | return; |
| 308 | |
Alex Williamson | 69166fb | 2011-11-02 14:07:15 -0600 | [diff] [blame] | 309 | pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PASID); |
Joerg Roedel | 086ac11 | 2011-09-27 15:57:16 +0200 | [diff] [blame] | 310 | if (!pos) |
| 311 | return; |
| 312 | |
Alex Williamson | 91f57d5 | 2011-11-11 10:07:36 -0700 | [diff] [blame] | 313 | pci_write_config_word(pdev, pos + PCI_PASID_CTRL, control); |
Jean-Philippe Brucker | a4f4fa6 | 2017-05-30 09:25:48 -0700 | [diff] [blame] | 314 | |
| 315 | pdev->pasid_enabled = 0; |
Joerg Roedel | 086ac11 | 2011-09-27 15:57:16 +0200 | [diff] [blame] | 316 | } |
| 317 | EXPORT_SYMBOL_GPL(pci_disable_pasid); |
| 318 | |
| 319 | /** |
CQ Tang | 4ebeb1e | 2017-05-30 09:25:49 -0700 | [diff] [blame] | 320 | * pci_restore_pasid_state - Restore PASID capabilities |
| 321 | * @pdev: PCI device structure |
| 322 | */ |
| 323 | void pci_restore_pasid_state(struct pci_dev *pdev) |
| 324 | { |
| 325 | u16 control; |
| 326 | int pos; |
| 327 | |
| 328 | if (!pdev->pasid_enabled) |
| 329 | return; |
| 330 | |
| 331 | pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PASID); |
| 332 | if (!pos) |
| 333 | return; |
| 334 | |
| 335 | control = PCI_PASID_CTRL_ENABLE | pdev->pasid_features; |
| 336 | pci_write_config_word(pdev, pos + PCI_PASID_CTRL, control); |
| 337 | } |
| 338 | EXPORT_SYMBOL_GPL(pci_restore_pasid_state); |
| 339 | |
| 340 | /** |
Joerg Roedel | 086ac11 | 2011-09-27 15:57:16 +0200 | [diff] [blame] | 341 | * pci_pasid_features - Check which PASID features are supported |
| 342 | * @pdev: PCI device structure |
| 343 | * |
| 344 | * Returns a negative value when no PASI capability is present. |
| 345 | * Otherwise is returns a bitmask with supported features. Current |
| 346 | * features reported are: |
Alex Williamson | 91f57d5 | 2011-11-11 10:07:36 -0700 | [diff] [blame] | 347 | * PCI_PASID_CAP_EXEC - Execute permission supported |
Bjorn Helgaas | f762598 | 2013-11-14 11:28:18 -0700 | [diff] [blame] | 348 | * PCI_PASID_CAP_PRIV - Privileged mode supported |
Joerg Roedel | 086ac11 | 2011-09-27 15:57:16 +0200 | [diff] [blame] | 349 | */ |
| 350 | int pci_pasid_features(struct pci_dev *pdev) |
| 351 | { |
| 352 | u16 supported; |
| 353 | int pos; |
| 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); |
Joerg Roedel | 086ac11 | 2011-09-27 15:57:16 +0200 | [diff] [blame] | 360 | |
Alex Williamson | 91f57d5 | 2011-11-11 10:07:36 -0700 | [diff] [blame] | 361 | supported &= PCI_PASID_CAP_EXEC | PCI_PASID_CAP_PRIV; |
Joerg Roedel | 086ac11 | 2011-09-27 15:57:16 +0200 | [diff] [blame] | 362 | |
| 363 | return supported; |
| 364 | } |
| 365 | EXPORT_SYMBOL_GPL(pci_pasid_features); |
| 366 | |
| 367 | #define PASID_NUMBER_SHIFT 8 |
| 368 | #define PASID_NUMBER_MASK (0x1f << PASID_NUMBER_SHIFT) |
| 369 | /** |
| 370 | * pci_max_pasid - Get maximum number of PASIDs supported by device |
| 371 | * @pdev: PCI device structure |
| 372 | * |
| 373 | * Returns negative value when PASID capability is not present. |
| 374 | * Otherwise it returns the numer of supported PASIDs. |
| 375 | */ |
| 376 | int pci_max_pasids(struct pci_dev *pdev) |
| 377 | { |
| 378 | u16 supported; |
| 379 | int pos; |
| 380 | |
Alex Williamson | 69166fb | 2011-11-02 14:07:15 -0600 | [diff] [blame] | 381 | pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PASID); |
Joerg Roedel | 086ac11 | 2011-09-27 15:57:16 +0200 | [diff] [blame] | 382 | if (!pos) |
| 383 | return -EINVAL; |
| 384 | |
Alex Williamson | 91f57d5 | 2011-11-11 10:07:36 -0700 | [diff] [blame] | 385 | pci_read_config_word(pdev, pos + PCI_PASID_CAP, &supported); |
Joerg Roedel | 086ac11 | 2011-09-27 15:57:16 +0200 | [diff] [blame] | 386 | |
| 387 | supported = (supported & PASID_NUMBER_MASK) >> PASID_NUMBER_SHIFT; |
| 388 | |
| 389 | return (1 << supported); |
| 390 | } |
| 391 | EXPORT_SYMBOL_GPL(pci_max_pasids); |
| 392 | #endif /* CONFIG_PCI_PASID */ |