blob: 5ceff3e16e1be0ce9457b65d21f6afe326b598b4 [file] [log] [blame]
Joerg Roedeldb3c33c2011-09-27 15:57:13 +02001/*
2 * drivers/pci/ats.c
3 *
4 * Copyright (C) 2009 Intel Corporation, Yu Zhao <yu.zhao@intel.com>
5 *
6 * PCI Express I/O Virtualization (IOV) support.
7 * Address Translation Service 1.0
8 */
9
10#include <linux/pci-ats.h>
11#include <linux/pci.h>
12
13#include "pci.h"
14
15static int ats_alloc_one(struct pci_dev *dev, int ps)
16{
17 int pos;
18 u16 cap;
19 struct pci_ats *ats;
20
21 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ATS);
22 if (!pos)
23 return -ENODEV;
24
25 ats = kzalloc(sizeof(*ats), GFP_KERNEL);
26 if (!ats)
27 return -ENOMEM;
28
29 ats->pos = pos;
30 ats->stu = ps;
31 pci_read_config_word(dev, pos + PCI_ATS_CAP, &cap);
32 ats->qdep = PCI_ATS_CAP_QDEP(cap) ? PCI_ATS_CAP_QDEP(cap) :
33 PCI_ATS_MAX_QDEP;
34 dev->ats = ats;
35
36 return 0;
37}
38
39static void ats_free_one(struct pci_dev *dev)
40{
41 kfree(dev->ats);
42 dev->ats = NULL;
43}
44
45/**
46 * pci_enable_ats - enable the ATS capability
47 * @dev: the PCI device
48 * @ps: the IOMMU page shift
49 *
50 * Returns 0 on success, or negative on failure.
51 */
52int pci_enable_ats(struct pci_dev *dev, int ps)
53{
54 int rc;
55 u16 ctrl;
56
57 BUG_ON(dev->ats && dev->ats->is_enabled);
58
59 if (ps < PCI_ATS_MIN_STU)
60 return -EINVAL;
61
62 if (dev->is_physfn || dev->is_virtfn) {
63 struct pci_dev *pdev = dev->is_physfn ? dev : dev->physfn;
64
65 mutex_lock(&pdev->sriov->lock);
66 if (pdev->ats)
67 rc = pdev->ats->stu == ps ? 0 : -EINVAL;
68 else
69 rc = ats_alloc_one(pdev, ps);
70
71 if (!rc)
72 pdev->ats->ref_cnt++;
73 mutex_unlock(&pdev->sriov->lock);
74 if (rc)
75 return rc;
76 }
77
78 if (!dev->is_physfn) {
79 rc = ats_alloc_one(dev, ps);
80 if (rc)
81 return rc;
82 }
83
84 ctrl = PCI_ATS_CTRL_ENABLE;
85 if (!dev->is_virtfn)
86 ctrl |= PCI_ATS_CTRL_STU(ps - PCI_ATS_MIN_STU);
87 pci_write_config_word(dev, dev->ats->pos + PCI_ATS_CTRL, ctrl);
88
89 dev->ats->is_enabled = 1;
90
91 return 0;
92}
Joerg Roedeld4c06362011-09-27 15:57:14 +020093EXPORT_SYMBOL_GPL(pci_enable_ats);
Joerg Roedeldb3c33c2011-09-27 15:57:13 +020094
95/**
96 * pci_disable_ats - disable the ATS capability
97 * @dev: the PCI device
98 */
99void pci_disable_ats(struct pci_dev *dev)
100{
101 u16 ctrl;
102
103 BUG_ON(!dev->ats || !dev->ats->is_enabled);
104
105 pci_read_config_word(dev, dev->ats->pos + PCI_ATS_CTRL, &ctrl);
106 ctrl &= ~PCI_ATS_CTRL_ENABLE;
107 pci_write_config_word(dev, dev->ats->pos + PCI_ATS_CTRL, ctrl);
108
109 dev->ats->is_enabled = 0;
110
111 if (dev->is_physfn || dev->is_virtfn) {
112 struct pci_dev *pdev = dev->is_physfn ? dev : dev->physfn;
113
114 mutex_lock(&pdev->sriov->lock);
115 pdev->ats->ref_cnt--;
116 if (!pdev->ats->ref_cnt)
117 ats_free_one(pdev);
118 mutex_unlock(&pdev->sriov->lock);
119 }
120
121 if (!dev->is_physfn)
122 ats_free_one(dev);
123}
Joerg Roedeld4c06362011-09-27 15:57:14 +0200124EXPORT_SYMBOL_GPL(pci_disable_ats);
Joerg Roedeldb3c33c2011-09-27 15:57:13 +0200125
126/**
127 * pci_ats_queue_depth - query the ATS Invalidate Queue Depth
128 * @dev: the PCI device
129 *
130 * Returns the queue depth on success, or negative on failure.
131 *
132 * The ATS spec uses 0 in the Invalidate Queue Depth field to
133 * indicate that the function can accept 32 Invalidate Request.
134 * But here we use the `real' values (i.e. 1~32) for the Queue
135 * Depth; and 0 indicates the function shares the Queue with
136 * other functions (doesn't exclusively own a Queue).
137 */
138int pci_ats_queue_depth(struct pci_dev *dev)
139{
140 int pos;
141 u16 cap;
142
143 if (dev->is_virtfn)
144 return 0;
145
146 if (dev->ats)
147 return dev->ats->qdep;
148
149 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ATS);
150 if (!pos)
151 return -ENODEV;
152
153 pci_read_config_word(dev, pos + PCI_ATS_CAP, &cap);
154
155 return PCI_ATS_CAP_QDEP(cap) ? PCI_ATS_CAP_QDEP(cap) :
156 PCI_ATS_MAX_QDEP;
157}
Joerg Roedeld4c06362011-09-27 15:57:14 +0200158EXPORT_SYMBOL_GPL(pci_ats_queue_depth);