]> Pileus Git - ~andy/linux/blob - drivers/pci/ats.c
Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs
[~andy/linux] / drivers / pci / ats.c
1 /*
2  * drivers/pci/ats.c
3  *
4  * Copyright (C) 2009 Intel Corporation, Yu Zhao <yu.zhao@intel.com>
5  * Copyright (C) 2011 Advanced Micro Devices,
6  *
7  * PCI Express I/O Virtualization (IOV) support.
8  *   Address Translation Service 1.0
9  *   Page Request Interface added by Joerg Roedel <joerg.roedel@amd.com>
10  *   PASID support added by Joerg Roedel <joerg.roedel@amd.com>
11  */
12
13 #include <linux/export.h>
14 #include <linux/pci-ats.h>
15 #include <linux/pci.h>
16 #include <linux/slab.h>
17
18 #include "pci.h"
19
20 static int ats_alloc_one(struct pci_dev *dev, int ps)
21 {
22         int pos;
23         u16 cap;
24         struct pci_ats *ats;
25
26         pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ATS);
27         if (!pos)
28                 return -ENODEV;
29
30         ats = kzalloc(sizeof(*ats), GFP_KERNEL);
31         if (!ats)
32                 return -ENOMEM;
33
34         ats->pos = pos;
35         ats->stu = ps;
36         pci_read_config_word(dev, pos + PCI_ATS_CAP, &cap);
37         ats->qdep = PCI_ATS_CAP_QDEP(cap) ? PCI_ATS_CAP_QDEP(cap) :
38                                             PCI_ATS_MAX_QDEP;
39         dev->ats = ats;
40
41         return 0;
42 }
43
44 static void ats_free_one(struct pci_dev *dev)
45 {
46         kfree(dev->ats);
47         dev->ats = NULL;
48 }
49
50 /**
51  * pci_enable_ats - enable the ATS capability
52  * @dev: the PCI device
53  * @ps: the IOMMU page shift
54  *
55  * Returns 0 on success, or negative on failure.
56  */
57 int pci_enable_ats(struct pci_dev *dev, int ps)
58 {
59         int rc;
60         u16 ctrl;
61
62         BUG_ON(dev->ats && dev->ats->is_enabled);
63
64         if (ps < PCI_ATS_MIN_STU)
65                 return -EINVAL;
66
67         if (dev->is_physfn || dev->is_virtfn) {
68                 struct pci_dev *pdev = dev->is_physfn ? dev : dev->physfn;
69
70                 mutex_lock(&pdev->sriov->lock);
71                 if (pdev->ats)
72                         rc = pdev->ats->stu == ps ? 0 : -EINVAL;
73                 else
74                         rc = ats_alloc_one(pdev, ps);
75
76                 if (!rc)
77                         pdev->ats->ref_cnt++;
78                 mutex_unlock(&pdev->sriov->lock);
79                 if (rc)
80                         return rc;
81         }
82
83         if (!dev->is_physfn) {
84                 rc = ats_alloc_one(dev, ps);
85                 if (rc)
86                         return rc;
87         }
88
89         ctrl = PCI_ATS_CTRL_ENABLE;
90         if (!dev->is_virtfn)
91                 ctrl |= PCI_ATS_CTRL_STU(ps - PCI_ATS_MIN_STU);
92         pci_write_config_word(dev, dev->ats->pos + PCI_ATS_CTRL, ctrl);
93
94         dev->ats->is_enabled = 1;
95
96         return 0;
97 }
98 EXPORT_SYMBOL_GPL(pci_enable_ats);
99
100 /**
101  * pci_disable_ats - disable the ATS capability
102  * @dev: the PCI device
103  */
104 void pci_disable_ats(struct pci_dev *dev)
105 {
106         u16 ctrl;
107
108         BUG_ON(!dev->ats || !dev->ats->is_enabled);
109
110         pci_read_config_word(dev, dev->ats->pos + PCI_ATS_CTRL, &ctrl);
111         ctrl &= ~PCI_ATS_CTRL_ENABLE;
112         pci_write_config_word(dev, dev->ats->pos + PCI_ATS_CTRL, ctrl);
113
114         dev->ats->is_enabled = 0;
115
116         if (dev->is_physfn || dev->is_virtfn) {
117                 struct pci_dev *pdev = dev->is_physfn ? dev : dev->physfn;
118
119                 mutex_lock(&pdev->sriov->lock);
120                 pdev->ats->ref_cnt--;
121                 if (!pdev->ats->ref_cnt)
122                         ats_free_one(pdev);
123                 mutex_unlock(&pdev->sriov->lock);
124         }
125
126         if (!dev->is_physfn)
127                 ats_free_one(dev);
128 }
129 EXPORT_SYMBOL_GPL(pci_disable_ats);
130
131 /**
132  * pci_ats_queue_depth - query the ATS Invalidate Queue Depth
133  * @dev: the PCI device
134  *
135  * Returns the queue depth on success, or negative on failure.
136  *
137  * The ATS spec uses 0 in the Invalidate Queue Depth field to
138  * indicate that the function can accept 32 Invalidate Request.
139  * But here we use the `real' values (i.e. 1~32) for the Queue
140  * Depth; and 0 indicates the function shares the Queue with
141  * other functions (doesn't exclusively own a Queue).
142  */
143 int pci_ats_queue_depth(struct pci_dev *dev)
144 {
145         int pos;
146         u16 cap;
147
148         if (dev->is_virtfn)
149                 return 0;
150
151         if (dev->ats)
152                 return dev->ats->qdep;
153
154         pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ATS);
155         if (!pos)
156                 return -ENODEV;
157
158         pci_read_config_word(dev, pos + PCI_ATS_CAP, &cap);
159
160         return PCI_ATS_CAP_QDEP(cap) ? PCI_ATS_CAP_QDEP(cap) :
161                                        PCI_ATS_MAX_QDEP;
162 }
163 EXPORT_SYMBOL_GPL(pci_ats_queue_depth);
164
165 #ifdef CONFIG_PCI_PRI
166 /**
167  * pci_enable_pri - Enable PRI capability
168  * @ pdev: PCI device structure
169  *
170  * Returns 0 on success, negative value on error
171  */
172 int pci_enable_pri(struct pci_dev *pdev, u32 reqs)
173 {
174         u16 control, status;
175         u32 max_requests;
176         int pos;
177
178         pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
179         if (!pos)
180                 return -EINVAL;
181
182         pci_read_config_word(pdev, pos + PCI_PRI_CTRL, &control);
183         pci_read_config_word(pdev, pos + PCI_PRI_STATUS, &status);
184         if ((control & PCI_PRI_CTRL_ENABLE) ||
185             !(status & PCI_PRI_STATUS_STOPPED))
186                 return -EBUSY;
187
188         pci_read_config_dword(pdev, pos + PCI_PRI_MAX_REQ, &max_requests);
189         reqs = min(max_requests, reqs);
190         pci_write_config_dword(pdev, pos + PCI_PRI_ALLOC_REQ, reqs);
191
192         control |= PCI_PRI_CTRL_ENABLE;
193         pci_write_config_word(pdev, pos + PCI_PRI_CTRL, control);
194
195         return 0;
196 }
197 EXPORT_SYMBOL_GPL(pci_enable_pri);
198
199 /**
200  * pci_disable_pri - Disable PRI capability
201  * @pdev: PCI device structure
202  *
203  * Only clears the enabled-bit, regardless of its former value
204  */
205 void pci_disable_pri(struct pci_dev *pdev)
206 {
207         u16 control;
208         int pos;
209
210         pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
211         if (!pos)
212                 return;
213
214         pci_read_config_word(pdev, pos + PCI_PRI_CTRL, &control);
215         control &= ~PCI_PRI_CTRL_ENABLE;
216         pci_write_config_word(pdev, pos + PCI_PRI_CTRL, control);
217 }
218 EXPORT_SYMBOL_GPL(pci_disable_pri);
219
220 /**
221  * pci_pri_enabled - Checks if PRI capability is enabled
222  * @pdev: PCI device structure
223  *
224  * Returns true if PRI is enabled on the device, false otherwise
225  */
226 bool pci_pri_enabled(struct pci_dev *pdev)
227 {
228         u16 control;
229         int pos;
230
231         pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
232         if (!pos)
233                 return false;
234
235         pci_read_config_word(pdev, pos + PCI_PRI_CTRL, &control);
236
237         return (control & PCI_PRI_CTRL_ENABLE) ? true : false;
238 }
239 EXPORT_SYMBOL_GPL(pci_pri_enabled);
240
241 /**
242  * pci_reset_pri - Resets device's PRI state
243  * @pdev: PCI device structure
244  *
245  * The PRI capability must be disabled before this function is called.
246  * Returns 0 on success, negative value on error.
247  */
248 int pci_reset_pri(struct pci_dev *pdev)
249 {
250         u16 control;
251         int pos;
252
253         pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
254         if (!pos)
255                 return -EINVAL;
256
257         pci_read_config_word(pdev, pos + PCI_PRI_CTRL, &control);
258         if (control & PCI_PRI_CTRL_ENABLE)
259                 return -EBUSY;
260
261         control |= PCI_PRI_CTRL_RESET;
262
263         pci_write_config_word(pdev, pos + PCI_PRI_CTRL, control);
264
265         return 0;
266 }
267 EXPORT_SYMBOL_GPL(pci_reset_pri);
268
269 /**
270  * pci_pri_stopped - Checks whether the PRI capability is stopped
271  * @pdev: PCI device structure
272  *
273  * Returns true if the PRI capability on the device is disabled and the
274  * device has no outstanding PRI requests, false otherwise. The device
275  * indicates this via the STOPPED bit in the status register of the
276  * capability.
277  * The device internal state can be cleared by resetting the PRI state
278  * with pci_reset_pri(). This can force the capability into the STOPPED
279  * state.
280  */
281 bool pci_pri_stopped(struct pci_dev *pdev)
282 {
283         u16 control, status;
284         int pos;
285
286         pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
287         if (!pos)
288                 return true;
289
290         pci_read_config_word(pdev, pos + PCI_PRI_CTRL, &control);
291         pci_read_config_word(pdev, pos + PCI_PRI_STATUS, &status);
292
293         if (control & PCI_PRI_CTRL_ENABLE)
294                 return false;
295
296         return (status & PCI_PRI_STATUS_STOPPED) ? true : false;
297 }
298 EXPORT_SYMBOL_GPL(pci_pri_stopped);
299
300 /**
301  * pci_pri_status - Request PRI status of a device
302  * @pdev: PCI device structure
303  *
304  * Returns negative value on failure, status on success. The status can
305  * be checked against status-bits. Supported bits are currently:
306  * PCI_PRI_STATUS_RF:      Response failure
307  * PCI_PRI_STATUS_UPRGI:   Unexpected Page Request Group Index
308  * PCI_PRI_STATUS_STOPPED: PRI has stopped
309  */
310 int pci_pri_status(struct pci_dev *pdev)
311 {
312         u16 status, control;
313         int pos;
314
315         pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
316         if (!pos)
317                 return -EINVAL;
318
319         pci_read_config_word(pdev, pos + PCI_PRI_CTRL, &control);
320         pci_read_config_word(pdev, pos + PCI_PRI_STATUS, &status);
321
322         /* Stopped bit is undefined when enable == 1, so clear it */
323         if (control & PCI_PRI_CTRL_ENABLE)
324                 status &= ~PCI_PRI_STATUS_STOPPED;
325
326         return status;
327 }
328 EXPORT_SYMBOL_GPL(pci_pri_status);
329 #endif /* CONFIG_PCI_PRI */
330
331 #ifdef CONFIG_PCI_PASID
332 /**
333  * pci_enable_pasid - Enable the PASID capability
334  * @pdev: PCI device structure
335  * @features: Features to enable
336  *
337  * Returns 0 on success, negative value on error. This function checks
338  * whether the features are actually supported by the device and returns
339  * an error if not.
340  */
341 int pci_enable_pasid(struct pci_dev *pdev, int features)
342 {
343         u16 control, supported;
344         int pos;
345
346         pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PASID);
347         if (!pos)
348                 return -EINVAL;
349
350         pci_read_config_word(pdev, pos + PCI_PASID_CTRL, &control);
351         pci_read_config_word(pdev, pos + PCI_PASID_CAP, &supported);
352
353         if (control & PCI_PASID_CTRL_ENABLE)
354                 return -EINVAL;
355
356         supported &= PCI_PASID_CAP_EXEC | PCI_PASID_CAP_PRIV;
357
358         /* User wants to enable anything unsupported? */
359         if ((supported & features) != features)
360                 return -EINVAL;
361
362         control = PCI_PASID_CTRL_ENABLE | features;
363
364         pci_write_config_word(pdev, pos + PCI_PASID_CTRL, control);
365
366         return 0;
367 }
368 EXPORT_SYMBOL_GPL(pci_enable_pasid);
369
370 /**
371  * pci_disable_pasid - Disable the PASID capability
372  * @pdev: PCI device structure
373  *
374  */
375 void pci_disable_pasid(struct pci_dev *pdev)
376 {
377         u16 control = 0;
378         int pos;
379
380         pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PASID);
381         if (!pos)
382                 return;
383
384         pci_write_config_word(pdev, pos + PCI_PASID_CTRL, control);
385 }
386 EXPORT_SYMBOL_GPL(pci_disable_pasid);
387
388 /**
389  * pci_pasid_features - Check which PASID features are supported
390  * @pdev: PCI device structure
391  *
392  * Returns a negative value when no PASI capability is present.
393  * Otherwise is returns a bitmask with supported features. Current
394  * features reported are:
395  * PCI_PASID_CAP_EXEC - Execute permission supported
396  * PCI_PASID_CAP_PRIV - Priviledged mode supported
397  */
398 int pci_pasid_features(struct pci_dev *pdev)
399 {
400         u16 supported;
401         int pos;
402
403         pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PASID);
404         if (!pos)
405                 return -EINVAL;
406
407         pci_read_config_word(pdev, pos + PCI_PASID_CAP, &supported);
408
409         supported &= PCI_PASID_CAP_EXEC | PCI_PASID_CAP_PRIV;
410
411         return supported;
412 }
413 EXPORT_SYMBOL_GPL(pci_pasid_features);
414
415 #define PASID_NUMBER_SHIFT      8
416 #define PASID_NUMBER_MASK       (0x1f << PASID_NUMBER_SHIFT)
417 /**
418  * pci_max_pasid - Get maximum number of PASIDs supported by device
419  * @pdev: PCI device structure
420  *
421  * Returns negative value when PASID capability is not present.
422  * Otherwise it returns the numer of supported PASIDs.
423  */
424 int pci_max_pasids(struct pci_dev *pdev)
425 {
426         u16 supported;
427         int pos;
428
429         pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PASID);
430         if (!pos)
431                 return -EINVAL;
432
433         pci_read_config_word(pdev, pos + PCI_PASID_CAP, &supported);
434
435         supported = (supported & PASID_NUMBER_MASK) >> PASID_NUMBER_SHIFT;
436
437         return (1 << supported);
438 }
439 EXPORT_SYMBOL_GPL(pci_max_pasids);
440 #endif /* CONFIG_PCI_PASID */