blob: 6ad80a1fd5a7a6098ac41b3dbd38bea427dc7839 [file] [log] [blame]
Bjorn Helgaas7328c8f2018-01-26 11:45:16 -06001// SPDX-License-Identifier: GPL-2.0
Joerg Roedeldb3c33c2011-09-27 15:57:13 +02002/*
3 * drivers/pci/ats.c
4 *
5 * Copyright (C) 2009 Intel Corporation, Yu Zhao <yu.zhao@intel.com>
Joerg Roedelc320b972011-09-27 15:57:15 +02006 * Copyright (C) 2011 Advanced Micro Devices,
Joerg Roedeldb3c33c2011-09-27 15:57:13 +02007 *
8 * PCI Express I/O Virtualization (IOV) support.
9 * Address Translation Service 1.0
Joerg Roedelc320b972011-09-27 15:57:15 +020010 * Page Request Interface added by Joerg Roedel <joerg.roedel@amd.com>
Joerg Roedel086ac112011-09-27 15:57:16 +020011 * PASID support added by Joerg Roedel <joerg.roedel@amd.com>
Joerg Roedeldb3c33c2011-09-27 15:57:13 +020012 */
13
Paul Gortmaker363c75d2011-05-27 09:37:25 -040014#include <linux/export.h>
Joerg Roedeldb3c33c2011-09-27 15:57:13 +020015#include <linux/pci-ats.h>
16#include <linux/pci.h>
James Bottomley8c451942011-11-29 19:20:23 +000017#include <linux/slab.h>
Joerg Roedeldb3c33c2011-09-27 15:57:13 +020018
19#include "pci.h"
20
Bjorn Helgaasafdd5962015-07-17 15:35:18 -050021void pci_ats_init(struct pci_dev *dev)
Joerg Roedeldb3c33c2011-09-27 15:57:13 +020022{
23 int pos;
Joerg Roedeldb3c33c2011-09-27 15:57:13 +020024
25 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ATS);
26 if (!pos)
Bjorn Helgaasedc90fe2015-07-17 15:05:46 -050027 return;
Joerg Roedeldb3c33c2011-09-27 15:57:13 +020028
Bjorn Helgaasd544d752015-07-17 15:15:19 -050029 dev->ats_cap = pos;
Joerg Roedeldb3c33c2011-09-27 15:57:13 +020030}
31
32/**
33 * pci_enable_ats - enable the ATS capability
34 * @dev: the PCI device
35 * @ps: the IOMMU page shift
36 *
37 * Returns 0 on success, or negative on failure.
38 */
39int pci_enable_ats(struct pci_dev *dev, int ps)
40{
Joerg Roedeldb3c33c2011-09-27 15:57:13 +020041 u16 ctrl;
Bjorn Helgaasc39127d2015-07-17 15:38:13 -050042 struct pci_dev *pdev;
Joerg Roedeldb3c33c2011-09-27 15:57:13 +020043
Bjorn Helgaasd544d752015-07-17 15:15:19 -050044 if (!dev->ats_cap)
Bjorn Helgaasedc90fe2015-07-17 15:05:46 -050045 return -EINVAL;
46
Bjorn Helgaasf7ef1342015-07-20 09:23:37 -050047 if (WARN_ON(dev->ats_enabled))
Bjorn Helgaasa021f302015-07-17 15:43:27 -050048 return -EBUSY;
49
Joerg Roedeldb3c33c2011-09-27 15:57:13 +020050 if (ps < PCI_ATS_MIN_STU)
51 return -EINVAL;
52
Bjorn Helgaasedc90fe2015-07-17 15:05:46 -050053 /*
54 * Note that enabling ATS on a VF fails unless it's already enabled
55 * with the same STU on the PF.
56 */
Joerg Roedeldb3c33c2011-09-27 15:57:13 +020057 ctrl = PCI_ATS_CTRL_ENABLE;
Bjorn Helgaasedc90fe2015-07-17 15:05:46 -050058 if (dev->is_virtfn) {
Bjorn Helgaasc39127d2015-07-17 15:38:13 -050059 pdev = pci_physfn(dev);
Bjorn Helgaasd544d752015-07-17 15:15:19 -050060 if (pdev->ats_stu != ps)
Bjorn Helgaasedc90fe2015-07-17 15:05:46 -050061 return -EINVAL;
62
Bjorn Helgaasd544d752015-07-17 15:15:19 -050063 atomic_inc(&pdev->ats_ref_cnt); /* count enabled VFs */
Bjorn Helgaasedc90fe2015-07-17 15:05:46 -050064 } else {
Bjorn Helgaasd544d752015-07-17 15:15:19 -050065 dev->ats_stu = ps;
66 ctrl |= PCI_ATS_CTRL_STU(dev->ats_stu - PCI_ATS_MIN_STU);
Bjorn Helgaasedc90fe2015-07-17 15:05:46 -050067 }
Bjorn Helgaasd544d752015-07-17 15:15:19 -050068 pci_write_config_word(dev, dev->ats_cap + PCI_ATS_CTRL, ctrl);
Joerg Roedeldb3c33c2011-09-27 15:57:13 +020069
Bjorn Helgaasd544d752015-07-17 15:15:19 -050070 dev->ats_enabled = 1;
Joerg Roedeldb3c33c2011-09-27 15:57:13 +020071 return 0;
72}
Joerg Roedeld4c06362011-09-27 15:57:14 +020073EXPORT_SYMBOL_GPL(pci_enable_ats);
Joerg Roedeldb3c33c2011-09-27 15:57:13 +020074
75/**
76 * pci_disable_ats - disable the ATS capability
77 * @dev: the PCI device
78 */
79void pci_disable_ats(struct pci_dev *dev)
80{
Bjorn Helgaasc39127d2015-07-17 15:38:13 -050081 struct pci_dev *pdev;
Joerg Roedeldb3c33c2011-09-27 15:57:13 +020082 u16 ctrl;
83
Bjorn Helgaasf7ef1342015-07-20 09:23:37 -050084 if (WARN_ON(!dev->ats_enabled))
Bjorn Helgaasa021f302015-07-17 15:43:27 -050085 return;
Joerg Roedeldb3c33c2011-09-27 15:57:13 +020086
Bjorn Helgaasd544d752015-07-17 15:15:19 -050087 if (atomic_read(&dev->ats_ref_cnt))
Bjorn Helgaasedc90fe2015-07-17 15:05:46 -050088 return; /* VFs still enabled */
89
90 if (dev->is_virtfn) {
Bjorn Helgaasc39127d2015-07-17 15:38:13 -050091 pdev = pci_physfn(dev);
Bjorn Helgaasd544d752015-07-17 15:15:19 -050092 atomic_dec(&pdev->ats_ref_cnt);
Bjorn Helgaasedc90fe2015-07-17 15:05:46 -050093 }
94
Bjorn Helgaasd544d752015-07-17 15:15:19 -050095 pci_read_config_word(dev, dev->ats_cap + PCI_ATS_CTRL, &ctrl);
Joerg Roedeldb3c33c2011-09-27 15:57:13 +020096 ctrl &= ~PCI_ATS_CTRL_ENABLE;
Bjorn Helgaasd544d752015-07-17 15:15:19 -050097 pci_write_config_word(dev, dev->ats_cap + PCI_ATS_CTRL, ctrl);
Joerg Roedeldb3c33c2011-09-27 15:57:13 +020098
Bjorn Helgaasd544d752015-07-17 15:15:19 -050099 dev->ats_enabled = 0;
Joerg Roedeldb3c33c2011-09-27 15:57:13 +0200100}
Joerg Roedeld4c06362011-09-27 15:57:14 +0200101EXPORT_SYMBOL_GPL(pci_disable_ats);
Joerg Roedeldb3c33c2011-09-27 15:57:13 +0200102
Hao, Xudong1900ca12011-12-17 21:24:40 +0800103void pci_restore_ats_state(struct pci_dev *dev)
104{
105 u16 ctrl;
106
Bjorn Helgaasf7ef1342015-07-20 09:23:37 -0500107 if (!dev->ats_enabled)
Hao, Xudong1900ca12011-12-17 21:24:40 +0800108 return;
Hao, Xudong1900ca12011-12-17 21:24:40 +0800109
110 ctrl = PCI_ATS_CTRL_ENABLE;
111 if (!dev->is_virtfn)
Bjorn Helgaasd544d752015-07-17 15:15:19 -0500112 ctrl |= PCI_ATS_CTRL_STU(dev->ats_stu - PCI_ATS_MIN_STU);
113 pci_write_config_word(dev, dev->ats_cap + PCI_ATS_CTRL, ctrl);
Hao, Xudong1900ca12011-12-17 21:24:40 +0800114}
115EXPORT_SYMBOL_GPL(pci_restore_ats_state);
116
Joerg Roedeldb3c33c2011-09-27 15:57:13 +0200117/**
118 * pci_ats_queue_depth - query the ATS Invalidate Queue Depth
119 * @dev: the PCI device
120 *
121 * Returns the queue depth on success, or negative on failure.
122 *
123 * The ATS spec uses 0 in the Invalidate Queue Depth field to
124 * indicate that the function can accept 32 Invalidate Request.
125 * But here we use the `real' values (i.e. 1~32) for the Queue
126 * Depth; and 0 indicates the function shares the Queue with
127 * other functions (doesn't exclusively own a Queue).
128 */
129int pci_ats_queue_depth(struct pci_dev *dev)
130{
Bjorn Helgaasa71f9382015-07-20 09:24:32 -0500131 u16 cap;
132
Bjorn Helgaas3c765392015-07-17 15:30:26 -0500133 if (!dev->ats_cap)
134 return -EINVAL;
135
Joerg Roedeldb3c33c2011-09-27 15:57:13 +0200136 if (dev->is_virtfn)
137 return 0;
138
Bjorn Helgaasa71f9382015-07-20 09:24:32 -0500139 pci_read_config_word(dev, dev->ats_cap + PCI_ATS_CAP, &cap);
140 return PCI_ATS_CAP_QDEP(cap) ? PCI_ATS_CAP_QDEP(cap) : PCI_ATS_MAX_QDEP;
Joerg Roedeldb3c33c2011-09-27 15:57:13 +0200141}
Joerg Roedeld4c06362011-09-27 15:57:14 +0200142EXPORT_SYMBOL_GPL(pci_ats_queue_depth);
Joerg Roedelc320b972011-09-27 15:57:15 +0200143
144#ifdef CONFIG_PCI_PRI
145/**
146 * pci_enable_pri - Enable PRI capability
147 * @ pdev: PCI device structure
148 *
149 * Returns 0 on success, negative value on error
150 */
151int pci_enable_pri(struct pci_dev *pdev, u32 reqs)
152{
153 u16 control, status;
154 u32 max_requests;
155 int pos;
156
Jean-Philippe Bruckera4f4fa62017-05-30 09:25:48 -0700157 if (WARN_ON(pdev->pri_enabled))
158 return -EBUSY;
159
Alex Williamson69166fb2011-11-02 14:07:15 -0600160 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
Joerg Roedelc320b972011-09-27 15:57:15 +0200161 if (!pos)
162 return -EINVAL;
163
Alex Williamson91f57d52011-11-11 10:07:36 -0700164 pci_read_config_word(pdev, pos + PCI_PRI_STATUS, &status);
CQ Tang4ebeb1e2017-05-30 09:25:49 -0700165 if (!(status & PCI_PRI_STATUS_STOPPED))
Joerg Roedelc320b972011-09-27 15:57:15 +0200166 return -EBUSY;
167
Alex Williamson91f57d52011-11-11 10:07:36 -0700168 pci_read_config_dword(pdev, pos + PCI_PRI_MAX_REQ, &max_requests);
Joerg Roedelc320b972011-09-27 15:57:15 +0200169 reqs = min(max_requests, reqs);
CQ Tang4ebeb1e2017-05-30 09:25:49 -0700170 pdev->pri_reqs_alloc = reqs;
Alex Williamson91f57d52011-11-11 10:07:36 -0700171 pci_write_config_dword(pdev, pos + PCI_PRI_ALLOC_REQ, reqs);
Joerg Roedelc320b972011-09-27 15:57:15 +0200172
CQ Tang4ebeb1e2017-05-30 09:25:49 -0700173 control = PCI_PRI_CTRL_ENABLE;
Alex Williamson91f57d52011-11-11 10:07:36 -0700174 pci_write_config_word(pdev, pos + PCI_PRI_CTRL, control);
Joerg Roedelc320b972011-09-27 15:57:15 +0200175
Jean-Philippe Bruckera4f4fa62017-05-30 09:25:48 -0700176 pdev->pri_enabled = 1;
177
Joerg Roedelc320b972011-09-27 15:57:15 +0200178 return 0;
179}
180EXPORT_SYMBOL_GPL(pci_enable_pri);
181
182/**
183 * pci_disable_pri - Disable PRI capability
184 * @pdev: PCI device structure
185 *
186 * Only clears the enabled-bit, regardless of its former value
187 */
188void pci_disable_pri(struct pci_dev *pdev)
189{
190 u16 control;
191 int pos;
192
Jean-Philippe Bruckera4f4fa62017-05-30 09:25:48 -0700193 if (WARN_ON(!pdev->pri_enabled))
194 return;
195
Alex Williamson69166fb2011-11-02 14:07:15 -0600196 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
Joerg Roedelc320b972011-09-27 15:57:15 +0200197 if (!pos)
198 return;
199
Alex Williamson91f57d52011-11-11 10:07:36 -0700200 pci_read_config_word(pdev, pos + PCI_PRI_CTRL, &control);
201 control &= ~PCI_PRI_CTRL_ENABLE;
202 pci_write_config_word(pdev, pos + PCI_PRI_CTRL, control);
Jean-Philippe Bruckera4f4fa62017-05-30 09:25:48 -0700203
204 pdev->pri_enabled = 0;
Joerg Roedelc320b972011-09-27 15:57:15 +0200205}
206EXPORT_SYMBOL_GPL(pci_disable_pri);
207
208/**
CQ Tang4ebeb1e2017-05-30 09:25:49 -0700209 * pci_restore_pri_state - Restore PRI
210 * @pdev: PCI device structure
211 */
212void pci_restore_pri_state(struct pci_dev *pdev)
213{
214 u16 control = PCI_PRI_CTRL_ENABLE;
215 u32 reqs = pdev->pri_reqs_alloc;
216 int pos;
217
218 if (!pdev->pri_enabled)
219 return;
220
221 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
222 if (!pos)
223 return;
224
225 pci_write_config_dword(pdev, pos + PCI_PRI_ALLOC_REQ, reqs);
226 pci_write_config_word(pdev, pos + PCI_PRI_CTRL, control);
227}
228EXPORT_SYMBOL_GPL(pci_restore_pri_state);
229
230/**
Joerg Roedelc320b972011-09-27 15:57:15 +0200231 * pci_reset_pri - Resets device's PRI state
232 * @pdev: PCI device structure
233 *
234 * The PRI capability must be disabled before this function is called.
235 * Returns 0 on success, negative value on error.
236 */
237int pci_reset_pri(struct pci_dev *pdev)
238{
239 u16 control;
240 int pos;
241
Jean-Philippe Bruckera4f4fa62017-05-30 09:25:48 -0700242 if (WARN_ON(pdev->pri_enabled))
243 return -EBUSY;
244
Alex Williamson69166fb2011-11-02 14:07:15 -0600245 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
Joerg Roedelc320b972011-09-27 15:57:15 +0200246 if (!pos)
247 return -EINVAL;
248
CQ Tang4ebeb1e2017-05-30 09:25:49 -0700249 control = PCI_PRI_CTRL_RESET;
Alex Williamson91f57d52011-11-11 10:07:36 -0700250 pci_write_config_word(pdev, pos + PCI_PRI_CTRL, control);
Joerg Roedelc320b972011-09-27 15:57:15 +0200251
252 return 0;
253}
254EXPORT_SYMBOL_GPL(pci_reset_pri);
Joerg Roedelc320b972011-09-27 15:57:15 +0200255#endif /* CONFIG_PCI_PRI */
Joerg Roedel086ac112011-09-27 15:57:16 +0200256
257#ifdef CONFIG_PCI_PASID
258/**
259 * pci_enable_pasid - Enable the PASID capability
260 * @pdev: PCI device structure
261 * @features: Features to enable
262 *
263 * Returns 0 on success, negative value on error. This function checks
264 * whether the features are actually supported by the device and returns
265 * an error if not.
266 */
267int pci_enable_pasid(struct pci_dev *pdev, int features)
268{
269 u16 control, supported;
270 int pos;
271
Jean-Philippe Bruckera4f4fa62017-05-30 09:25:48 -0700272 if (WARN_ON(pdev->pasid_enabled))
273 return -EBUSY;
274
Alex Williamson69166fb2011-11-02 14:07:15 -0600275 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PASID);
Joerg Roedel086ac112011-09-27 15:57:16 +0200276 if (!pos)
277 return -EINVAL;
278
Alex Williamson91f57d52011-11-11 10:07:36 -0700279 pci_read_config_word(pdev, pos + PCI_PASID_CAP, &supported);
Alex Williamson91f57d52011-11-11 10:07:36 -0700280 supported &= PCI_PASID_CAP_EXEC | PCI_PASID_CAP_PRIV;
Joerg Roedel086ac112011-09-27 15:57:16 +0200281
282 /* User wants to enable anything unsupported? */
283 if ((supported & features) != features)
284 return -EINVAL;
285
Alex Williamson91f57d52011-11-11 10:07:36 -0700286 control = PCI_PASID_CTRL_ENABLE | features;
CQ Tang4ebeb1e2017-05-30 09:25:49 -0700287 pdev->pasid_features = features;
Joerg Roedel086ac112011-09-27 15:57:16 +0200288
Alex Williamson91f57d52011-11-11 10:07:36 -0700289 pci_write_config_word(pdev, pos + PCI_PASID_CTRL, control);
Joerg Roedel086ac112011-09-27 15:57:16 +0200290
Jean-Philippe Bruckera4f4fa62017-05-30 09:25:48 -0700291 pdev->pasid_enabled = 1;
292
Joerg Roedel086ac112011-09-27 15:57:16 +0200293 return 0;
294}
295EXPORT_SYMBOL_GPL(pci_enable_pasid);
296
297/**
298 * pci_disable_pasid - Disable the PASID capability
299 * @pdev: PCI device structure
Joerg Roedel086ac112011-09-27 15:57:16 +0200300 */
301void pci_disable_pasid(struct pci_dev *pdev)
302{
303 u16 control = 0;
304 int pos;
305
Jean-Philippe Bruckera4f4fa62017-05-30 09:25:48 -0700306 if (WARN_ON(!pdev->pasid_enabled))
307 return;
308
Alex Williamson69166fb2011-11-02 14:07:15 -0600309 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PASID);
Joerg Roedel086ac112011-09-27 15:57:16 +0200310 if (!pos)
311 return;
312
Alex Williamson91f57d52011-11-11 10:07:36 -0700313 pci_write_config_word(pdev, pos + PCI_PASID_CTRL, control);
Jean-Philippe Bruckera4f4fa62017-05-30 09:25:48 -0700314
315 pdev->pasid_enabled = 0;
Joerg Roedel086ac112011-09-27 15:57:16 +0200316}
317EXPORT_SYMBOL_GPL(pci_disable_pasid);
318
319/**
CQ Tang4ebeb1e2017-05-30 09:25:49 -0700320 * pci_restore_pasid_state - Restore PASID capabilities
321 * @pdev: PCI device structure
322 */
323void pci_restore_pasid_state(struct pci_dev *pdev)
324{
325 u16 control;
326 int pos;
327
328 if (!pdev->pasid_enabled)
329 return;
330
331 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PASID);
332 if (!pos)
333 return;
334
335 control = PCI_PASID_CTRL_ENABLE | pdev->pasid_features;
336 pci_write_config_word(pdev, pos + PCI_PASID_CTRL, control);
337}
338EXPORT_SYMBOL_GPL(pci_restore_pasid_state);
339
340/**
Joerg Roedel086ac112011-09-27 15:57:16 +0200341 * pci_pasid_features - Check which PASID features are supported
342 * @pdev: PCI device structure
343 *
344 * Returns a negative value when no PASI capability is present.
345 * Otherwise is returns a bitmask with supported features. Current
346 * features reported are:
Alex Williamson91f57d52011-11-11 10:07:36 -0700347 * PCI_PASID_CAP_EXEC - Execute permission supported
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700348 * PCI_PASID_CAP_PRIV - Privileged mode supported
Joerg Roedel086ac112011-09-27 15:57:16 +0200349 */
350int pci_pasid_features(struct pci_dev *pdev)
351{
352 u16 supported;
353 int pos;
354
Alex Williamson69166fb2011-11-02 14:07:15 -0600355 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PASID);
Joerg Roedel086ac112011-09-27 15:57:16 +0200356 if (!pos)
357 return -EINVAL;
358
Alex Williamson91f57d52011-11-11 10:07:36 -0700359 pci_read_config_word(pdev, pos + PCI_PASID_CAP, &supported);
Joerg Roedel086ac112011-09-27 15:57:16 +0200360
Alex Williamson91f57d52011-11-11 10:07:36 -0700361 supported &= PCI_PASID_CAP_EXEC | PCI_PASID_CAP_PRIV;
Joerg Roedel086ac112011-09-27 15:57:16 +0200362
363 return supported;
364}
365EXPORT_SYMBOL_GPL(pci_pasid_features);
366
367#define PASID_NUMBER_SHIFT 8
368#define PASID_NUMBER_MASK (0x1f << PASID_NUMBER_SHIFT)
369/**
370 * pci_max_pasid - Get maximum number of PASIDs supported by device
371 * @pdev: PCI device structure
372 *
373 * Returns negative value when PASID capability is not present.
374 * Otherwise it returns the numer of supported PASIDs.
375 */
376int pci_max_pasids(struct pci_dev *pdev)
377{
378 u16 supported;
379 int pos;
380
Alex Williamson69166fb2011-11-02 14:07:15 -0600381 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PASID);
Joerg Roedel086ac112011-09-27 15:57:16 +0200382 if (!pos)
383 return -EINVAL;
384
Alex Williamson91f57d52011-11-11 10:07:36 -0700385 pci_read_config_word(pdev, pos + PCI_PASID_CAP, &supported);
Joerg Roedel086ac112011-09-27 15:57:16 +0200386
387 supported = (supported & PASID_NUMBER_MASK) >> PASID_NUMBER_SHIFT;
388
389 return (1 << supported);
390}
391EXPORT_SYMBOL_GPL(pci_max_pasids);
392#endif /* CONFIG_PCI_PASID */