4 * Copyright (C) 2009 Intel Corporation, Yu Zhao <yu.zhao@intel.com>
6 * PCI Express I/O Virtualization (IOV) support.
7 * Address Translation Service 1.0
10 #include <linux/pci-ats.h>
11 #include <linux/pci.h>
15 static int ats_alloc_one(struct pci_dev *dev, int ps)
21 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ATS);
25 ats = kzalloc(sizeof(*ats), GFP_KERNEL);
31 pci_read_config_word(dev, pos + PCI_ATS_CAP, &cap);
32 ats->qdep = PCI_ATS_CAP_QDEP(cap) ? PCI_ATS_CAP_QDEP(cap) :
39 static void ats_free_one(struct pci_dev *dev)
46 * pci_enable_ats - enable the ATS capability
47 * @dev: the PCI device
48 * @ps: the IOMMU page shift
50 * Returns 0 on success, or negative on failure.
52 int pci_enable_ats(struct pci_dev *dev, int ps)
57 BUG_ON(dev->ats && dev->ats->is_enabled);
59 if (ps < PCI_ATS_MIN_STU)
62 if (dev->is_physfn || dev->is_virtfn) {
63 struct pci_dev *pdev = dev->is_physfn ? dev : dev->physfn;
65 mutex_lock(&pdev->sriov->lock);
67 rc = pdev->ats->stu == ps ? 0 : -EINVAL;
69 rc = ats_alloc_one(pdev, ps);
73 mutex_unlock(&pdev->sriov->lock);
78 if (!dev->is_physfn) {
79 rc = ats_alloc_one(dev, ps);
84 ctrl = PCI_ATS_CTRL_ENABLE;
86 ctrl |= PCI_ATS_CTRL_STU(ps - PCI_ATS_MIN_STU);
87 pci_write_config_word(dev, dev->ats->pos + PCI_ATS_CTRL, ctrl);
89 dev->ats->is_enabled = 1;
93 EXPORT_SYMBOL_GPL(pci_enable_ats);
96 * pci_disable_ats - disable the ATS capability
97 * @dev: the PCI device
99 void pci_disable_ats(struct pci_dev *dev)
103 BUG_ON(!dev->ats || !dev->ats->is_enabled);
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);
109 dev->ats->is_enabled = 0;
111 if (dev->is_physfn || dev->is_virtfn) {
112 struct pci_dev *pdev = dev->is_physfn ? dev : dev->physfn;
114 mutex_lock(&pdev->sriov->lock);
115 pdev->ats->ref_cnt--;
116 if (!pdev->ats->ref_cnt)
118 mutex_unlock(&pdev->sriov->lock);
124 EXPORT_SYMBOL_GPL(pci_disable_ats);
127 * pci_ats_queue_depth - query the ATS Invalidate Queue Depth
128 * @dev: the PCI device
130 * Returns the queue depth on success, or negative on failure.
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).
138 int pci_ats_queue_depth(struct pci_dev *dev)
147 return dev->ats->qdep;
149 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ATS);
153 pci_read_config_word(dev, pos + PCI_ATS_CAP, &cap);
155 return PCI_ATS_CAP_QDEP(cap) ? PCI_ATS_CAP_QDEP(cap) :
158 EXPORT_SYMBOL_GPL(pci_ats_queue_depth);