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