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