blob: 89305b569d3d8899e1ea093474010b73e6468669 [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/*
Bjorn Helgaasdf62ab52018-03-09 16:36:33 -06003 * PCI Express I/O Virtualization (IOV) support
Joerg Roedeldb3c33c2011-09-27 15:57:13 +02004 * Address Translation Service 1.0
Joerg Roedelc320b972011-09-27 15:57:15 +02005 * Page Request Interface added by Joerg Roedel <joerg.roedel@amd.com>
Joerg Roedel086ac112011-09-27 15:57:16 +02006 * PASID support added by Joerg Roedel <joerg.roedel@amd.com>
Bjorn Helgaasdf62ab52018-03-09 16:36:33 -06007 *
8 * Copyright (C) 2009 Intel Corporation, Yu Zhao <yu.zhao@intel.com>
9 * Copyright (C) 2011 Advanced Micro Devices,
Joerg Roedeldb3c33c2011-09-27 15:57:13 +020010 */
11
Paul Gortmaker363c75d2011-05-27 09:37:25 -040012#include <linux/export.h>
Joerg Roedeldb3c33c2011-09-27 15:57:13 +020013#include <linux/pci-ats.h>
14#include <linux/pci.h>
James Bottomley8c451942011-11-29 19:20:23 +000015#include <linux/slab.h>
Joerg Roedeldb3c33c2011-09-27 15:57:13 +020016
17#include "pci.h"
18
Bjorn Helgaasafdd5962015-07-17 15:35:18 -050019void pci_ats_init(struct pci_dev *dev)
Joerg Roedeldb3c33c2011-09-27 15:57:13 +020020{
21 int pos;
Joerg Roedeldb3c33c2011-09-27 15:57:13 +020022
23 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ATS);
24 if (!pos)
Bjorn Helgaasedc90fe2015-07-17 15:05:46 -050025 return;
Joerg Roedeldb3c33c2011-09-27 15:57:13 +020026
Bjorn Helgaasd544d752015-07-17 15:15:19 -050027 dev->ats_cap = pos;
Joerg Roedeldb3c33c2011-09-27 15:57:13 +020028}
29
30/**
31 * pci_enable_ats - enable the ATS capability
32 * @dev: the PCI device
33 * @ps: the IOMMU page shift
34 *
35 * Returns 0 on success, or negative on failure.
36 */
37int pci_enable_ats(struct pci_dev *dev, int ps)
38{
Joerg Roedeldb3c33c2011-09-27 15:57:13 +020039 u16 ctrl;
Bjorn Helgaasc39127d2015-07-17 15:38:13 -050040 struct pci_dev *pdev;
Joerg Roedeldb3c33c2011-09-27 15:57:13 +020041
Bjorn Helgaasd544d752015-07-17 15:15:19 -050042 if (!dev->ats_cap)
Bjorn Helgaasedc90fe2015-07-17 15:05:46 -050043 return -EINVAL;
44
Bjorn Helgaasf7ef1342015-07-20 09:23:37 -050045 if (WARN_ON(dev->ats_enabled))
Bjorn Helgaasa021f302015-07-17 15:43:27 -050046 return -EBUSY;
47
Joerg Roedeldb3c33c2011-09-27 15:57:13 +020048 if (ps < PCI_ATS_MIN_STU)
49 return -EINVAL;
50
Bjorn Helgaasedc90fe2015-07-17 15:05:46 -050051 /*
52 * Note that enabling ATS on a VF fails unless it's already enabled
53 * with the same STU on the PF.
54 */
Joerg Roedeldb3c33c2011-09-27 15:57:13 +020055 ctrl = PCI_ATS_CTRL_ENABLE;
Bjorn Helgaasedc90fe2015-07-17 15:05:46 -050056 if (dev->is_virtfn) {
Bjorn Helgaasc39127d2015-07-17 15:38:13 -050057 pdev = pci_physfn(dev);
Bjorn Helgaasd544d752015-07-17 15:15:19 -050058 if (pdev->ats_stu != ps)
Bjorn Helgaasedc90fe2015-07-17 15:05:46 -050059 return -EINVAL;
60
Bjorn Helgaasd544d752015-07-17 15:15:19 -050061 atomic_inc(&pdev->ats_ref_cnt); /* count enabled VFs */
Bjorn Helgaasedc90fe2015-07-17 15:05:46 -050062 } else {
Bjorn Helgaasd544d752015-07-17 15:15:19 -050063 dev->ats_stu = ps;
64 ctrl |= PCI_ATS_CTRL_STU(dev->ats_stu - PCI_ATS_MIN_STU);
Bjorn Helgaasedc90fe2015-07-17 15:05:46 -050065 }
Bjorn Helgaasd544d752015-07-17 15:15:19 -050066 pci_write_config_word(dev, dev->ats_cap + PCI_ATS_CTRL, ctrl);
Joerg Roedeldb3c33c2011-09-27 15:57:13 +020067
Bjorn Helgaasd544d752015-07-17 15:15:19 -050068 dev->ats_enabled = 1;
Joerg Roedeldb3c33c2011-09-27 15:57:13 +020069 return 0;
70}
Joerg Roedeld4c06362011-09-27 15:57:14 +020071EXPORT_SYMBOL_GPL(pci_enable_ats);
Joerg Roedeldb3c33c2011-09-27 15:57:13 +020072
73/**
74 * pci_disable_ats - disable the ATS capability
75 * @dev: the PCI device
76 */
77void pci_disable_ats(struct pci_dev *dev)
78{
Bjorn Helgaasc39127d2015-07-17 15:38:13 -050079 struct pci_dev *pdev;
Joerg Roedeldb3c33c2011-09-27 15:57:13 +020080 u16 ctrl;
81
Bjorn Helgaasf7ef1342015-07-20 09:23:37 -050082 if (WARN_ON(!dev->ats_enabled))
Bjorn Helgaasa021f302015-07-17 15:43:27 -050083 return;
Joerg Roedeldb3c33c2011-09-27 15:57:13 +020084
Bjorn Helgaasd544d752015-07-17 15:15:19 -050085 if (atomic_read(&dev->ats_ref_cnt))
Bjorn Helgaasedc90fe2015-07-17 15:05:46 -050086 return; /* VFs still enabled */
87
88 if (dev->is_virtfn) {
Bjorn Helgaasc39127d2015-07-17 15:38:13 -050089 pdev = pci_physfn(dev);
Bjorn Helgaasd544d752015-07-17 15:15:19 -050090 atomic_dec(&pdev->ats_ref_cnt);
Bjorn Helgaasedc90fe2015-07-17 15:05:46 -050091 }
92
Bjorn Helgaasd544d752015-07-17 15:15:19 -050093 pci_read_config_word(dev, dev->ats_cap + PCI_ATS_CTRL, &ctrl);
Joerg Roedeldb3c33c2011-09-27 15:57:13 +020094 ctrl &= ~PCI_ATS_CTRL_ENABLE;
Bjorn Helgaasd544d752015-07-17 15:15:19 -050095 pci_write_config_word(dev, dev->ats_cap + PCI_ATS_CTRL, ctrl);
Joerg Roedeldb3c33c2011-09-27 15:57:13 +020096
Bjorn Helgaasd544d752015-07-17 15:15:19 -050097 dev->ats_enabled = 0;
Joerg Roedeldb3c33c2011-09-27 15:57:13 +020098}
Joerg Roedeld4c06362011-09-27 15:57:14 +020099EXPORT_SYMBOL_GPL(pci_disable_ats);
Joerg Roedeldb3c33c2011-09-27 15:57:13 +0200100
Hao, Xudong1900ca12011-12-17 21:24:40 +0800101void pci_restore_ats_state(struct pci_dev *dev)
102{
103 u16 ctrl;
104
Bjorn Helgaasf7ef1342015-07-20 09:23:37 -0500105 if (!dev->ats_enabled)
Hao, Xudong1900ca12011-12-17 21:24:40 +0800106 return;
Hao, Xudong1900ca12011-12-17 21:24:40 +0800107
108 ctrl = PCI_ATS_CTRL_ENABLE;
109 if (!dev->is_virtfn)
Bjorn Helgaasd544d752015-07-17 15:15:19 -0500110 ctrl |= PCI_ATS_CTRL_STU(dev->ats_stu - PCI_ATS_MIN_STU);
111 pci_write_config_word(dev, dev->ats_cap + PCI_ATS_CTRL, ctrl);
Hao, Xudong1900ca12011-12-17 21:24:40 +0800112}
113EXPORT_SYMBOL_GPL(pci_restore_ats_state);
114
Joerg Roedeldb3c33c2011-09-27 15:57:13 +0200115/**
116 * pci_ats_queue_depth - query the ATS Invalidate Queue Depth
117 * @dev: the PCI device
118 *
119 * Returns the queue depth on success, or negative on failure.
120 *
121 * The ATS spec uses 0 in the Invalidate Queue Depth field to
122 * indicate that the function can accept 32 Invalidate Request.
123 * But here we use the `real' values (i.e. 1~32) for the Queue
124 * Depth; and 0 indicates the function shares the Queue with
125 * other functions (doesn't exclusively own a Queue).
126 */
127int pci_ats_queue_depth(struct pci_dev *dev)
128{
Bjorn Helgaasa71f9382015-07-20 09:24:32 -0500129 u16 cap;
130
Bjorn Helgaas3c765392015-07-17 15:30:26 -0500131 if (!dev->ats_cap)
132 return -EINVAL;
133
Joerg Roedeldb3c33c2011-09-27 15:57:13 +0200134 if (dev->is_virtfn)
135 return 0;
136
Bjorn Helgaasa71f9382015-07-20 09:24:32 -0500137 pci_read_config_word(dev, dev->ats_cap + PCI_ATS_CAP, &cap);
138 return PCI_ATS_CAP_QDEP(cap) ? PCI_ATS_CAP_QDEP(cap) : PCI_ATS_MAX_QDEP;
Joerg Roedeldb3c33c2011-09-27 15:57:13 +0200139}
Joerg Roedeld4c06362011-09-27 15:57:14 +0200140EXPORT_SYMBOL_GPL(pci_ats_queue_depth);
Joerg Roedelc320b972011-09-27 15:57:15 +0200141
142#ifdef CONFIG_PCI_PRI
143/**
144 * pci_enable_pri - Enable PRI capability
145 * @ pdev: PCI device structure
146 *
147 * Returns 0 on success, negative value on error
148 */
149int pci_enable_pri(struct pci_dev *pdev, u32 reqs)
150{
151 u16 control, status;
152 u32 max_requests;
153 int pos;
154
Jean-Philippe Bruckera4f4fa62017-05-30 09:25:48 -0700155 if (WARN_ON(pdev->pri_enabled))
156 return -EBUSY;
157
Alex Williamson69166fb2011-11-02 14:07:15 -0600158 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
Joerg Roedelc320b972011-09-27 15:57:15 +0200159 if (!pos)
160 return -EINVAL;
161
Alex Williamson91f57d52011-11-11 10:07:36 -0700162 pci_read_config_word(pdev, pos + PCI_PRI_STATUS, &status);
CQ Tang4ebeb1e2017-05-30 09:25:49 -0700163 if (!(status & PCI_PRI_STATUS_STOPPED))
Joerg Roedelc320b972011-09-27 15:57:15 +0200164 return -EBUSY;
165
Alex Williamson91f57d52011-11-11 10:07:36 -0700166 pci_read_config_dword(pdev, pos + PCI_PRI_MAX_REQ, &max_requests);
Joerg Roedelc320b972011-09-27 15:57:15 +0200167 reqs = min(max_requests, reqs);
CQ Tang4ebeb1e2017-05-30 09:25:49 -0700168 pdev->pri_reqs_alloc = reqs;
Alex Williamson91f57d52011-11-11 10:07:36 -0700169 pci_write_config_dword(pdev, pos + PCI_PRI_ALLOC_REQ, reqs);
Joerg Roedelc320b972011-09-27 15:57:15 +0200170
CQ Tang4ebeb1e2017-05-30 09:25:49 -0700171 control = PCI_PRI_CTRL_ENABLE;
Alex Williamson91f57d52011-11-11 10:07:36 -0700172 pci_write_config_word(pdev, pos + PCI_PRI_CTRL, control);
Joerg Roedelc320b972011-09-27 15:57:15 +0200173
Jean-Philippe Bruckera4f4fa62017-05-30 09:25:48 -0700174 pdev->pri_enabled = 1;
175
Joerg Roedelc320b972011-09-27 15:57:15 +0200176 return 0;
177}
178EXPORT_SYMBOL_GPL(pci_enable_pri);
179
180/**
181 * pci_disable_pri - Disable PRI capability
182 * @pdev: PCI device structure
183 *
184 * Only clears the enabled-bit, regardless of its former value
185 */
186void pci_disable_pri(struct pci_dev *pdev)
187{
188 u16 control;
189 int pos;
190
Jean-Philippe Bruckera4f4fa62017-05-30 09:25:48 -0700191 if (WARN_ON(!pdev->pri_enabled))
192 return;
193
Alex Williamson69166fb2011-11-02 14:07:15 -0600194 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
Joerg Roedelc320b972011-09-27 15:57:15 +0200195 if (!pos)
196 return;
197
Alex Williamson91f57d52011-11-11 10:07:36 -0700198 pci_read_config_word(pdev, pos + PCI_PRI_CTRL, &control);
199 control &= ~PCI_PRI_CTRL_ENABLE;
200 pci_write_config_word(pdev, pos + PCI_PRI_CTRL, control);
Jean-Philippe Bruckera4f4fa62017-05-30 09:25:48 -0700201
202 pdev->pri_enabled = 0;
Joerg Roedelc320b972011-09-27 15:57:15 +0200203}
204EXPORT_SYMBOL_GPL(pci_disable_pri);
205
206/**
CQ Tang4ebeb1e2017-05-30 09:25:49 -0700207 * pci_restore_pri_state - Restore PRI
208 * @pdev: PCI device structure
209 */
210void pci_restore_pri_state(struct pci_dev *pdev)
211{
212 u16 control = PCI_PRI_CTRL_ENABLE;
213 u32 reqs = pdev->pri_reqs_alloc;
214 int pos;
215
216 if (!pdev->pri_enabled)
217 return;
218
219 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
220 if (!pos)
221 return;
222
223 pci_write_config_dword(pdev, pos + PCI_PRI_ALLOC_REQ, reqs);
224 pci_write_config_word(pdev, pos + PCI_PRI_CTRL, control);
225}
226EXPORT_SYMBOL_GPL(pci_restore_pri_state);
227
228/**
Joerg Roedelc320b972011-09-27 15:57:15 +0200229 * pci_reset_pri - Resets device's PRI state
230 * @pdev: PCI device structure
231 *
232 * The PRI capability must be disabled before this function is called.
233 * Returns 0 on success, negative value on error.
234 */
235int pci_reset_pri(struct pci_dev *pdev)
236{
237 u16 control;
238 int pos;
239
Jean-Philippe Bruckera4f4fa62017-05-30 09:25:48 -0700240 if (WARN_ON(pdev->pri_enabled))
241 return -EBUSY;
242
Alex Williamson69166fb2011-11-02 14:07:15 -0600243 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
Joerg Roedelc320b972011-09-27 15:57:15 +0200244 if (!pos)
245 return -EINVAL;
246
CQ Tang4ebeb1e2017-05-30 09:25:49 -0700247 control = PCI_PRI_CTRL_RESET;
Alex Williamson91f57d52011-11-11 10:07:36 -0700248 pci_write_config_word(pdev, pos + PCI_PRI_CTRL, control);
Joerg Roedelc320b972011-09-27 15:57:15 +0200249
250 return 0;
251}
252EXPORT_SYMBOL_GPL(pci_reset_pri);
Joerg Roedelc320b972011-09-27 15:57:15 +0200253#endif /* CONFIG_PCI_PRI */
Joerg Roedel086ac112011-09-27 15:57:16 +0200254
255#ifdef CONFIG_PCI_PASID
256/**
257 * pci_enable_pasid - Enable the PASID capability
258 * @pdev: PCI device structure
259 * @features: Features to enable
260 *
261 * Returns 0 on success, negative value on error. This function checks
262 * whether the features are actually supported by the device and returns
263 * an error if not.
264 */
265int pci_enable_pasid(struct pci_dev *pdev, int features)
266{
267 u16 control, supported;
268 int pos;
269
Jean-Philippe Bruckera4f4fa62017-05-30 09:25:48 -0700270 if (WARN_ON(pdev->pasid_enabled))
271 return -EBUSY;
272
Alex Williamson69166fb2011-11-02 14:07:15 -0600273 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PASID);
Joerg Roedel086ac112011-09-27 15:57:16 +0200274 if (!pos)
275 return -EINVAL;
276
Alex Williamson91f57d52011-11-11 10:07:36 -0700277 pci_read_config_word(pdev, pos + PCI_PASID_CAP, &supported);
Alex Williamson91f57d52011-11-11 10:07:36 -0700278 supported &= PCI_PASID_CAP_EXEC | PCI_PASID_CAP_PRIV;
Joerg Roedel086ac112011-09-27 15:57:16 +0200279
280 /* User wants to enable anything unsupported? */
281 if ((supported & features) != features)
282 return -EINVAL;
283
Alex Williamson91f57d52011-11-11 10:07:36 -0700284 control = PCI_PASID_CTRL_ENABLE | features;
CQ Tang4ebeb1e2017-05-30 09:25:49 -0700285 pdev->pasid_features = features;
Joerg Roedel086ac112011-09-27 15:57:16 +0200286
Alex Williamson91f57d52011-11-11 10:07:36 -0700287 pci_write_config_word(pdev, pos + PCI_PASID_CTRL, control);
Joerg Roedel086ac112011-09-27 15:57:16 +0200288
Jean-Philippe Bruckera4f4fa62017-05-30 09:25:48 -0700289 pdev->pasid_enabled = 1;
290
Joerg Roedel086ac112011-09-27 15:57:16 +0200291 return 0;
292}
293EXPORT_SYMBOL_GPL(pci_enable_pasid);
294
295/**
296 * pci_disable_pasid - Disable the PASID capability
297 * @pdev: PCI device structure
Joerg Roedel086ac112011-09-27 15:57:16 +0200298 */
299void pci_disable_pasid(struct pci_dev *pdev)
300{
301 u16 control = 0;
302 int pos;
303
Jean-Philippe Bruckera4f4fa62017-05-30 09:25:48 -0700304 if (WARN_ON(!pdev->pasid_enabled))
305 return;
306
Alex Williamson69166fb2011-11-02 14:07:15 -0600307 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PASID);
Joerg Roedel086ac112011-09-27 15:57:16 +0200308 if (!pos)
309 return;
310
Alex Williamson91f57d52011-11-11 10:07:36 -0700311 pci_write_config_word(pdev, pos + PCI_PASID_CTRL, control);
Jean-Philippe Bruckera4f4fa62017-05-30 09:25:48 -0700312
313 pdev->pasid_enabled = 0;
Joerg Roedel086ac112011-09-27 15:57:16 +0200314}
315EXPORT_SYMBOL_GPL(pci_disable_pasid);
316
317/**
CQ Tang4ebeb1e2017-05-30 09:25:49 -0700318 * pci_restore_pasid_state - Restore PASID capabilities
319 * @pdev: PCI device structure
320 */
321void pci_restore_pasid_state(struct pci_dev *pdev)
322{
323 u16 control;
324 int pos;
325
326 if (!pdev->pasid_enabled)
327 return;
328
329 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PASID);
330 if (!pos)
331 return;
332
333 control = PCI_PASID_CTRL_ENABLE | pdev->pasid_features;
334 pci_write_config_word(pdev, pos + PCI_PASID_CTRL, control);
335}
336EXPORT_SYMBOL_GPL(pci_restore_pasid_state);
337
338/**
Joerg Roedel086ac112011-09-27 15:57:16 +0200339 * pci_pasid_features - Check which PASID features are supported
340 * @pdev: PCI device structure
341 *
342 * Returns a negative value when no PASI capability is present.
343 * Otherwise is returns a bitmask with supported features. Current
344 * features reported are:
Alex Williamson91f57d52011-11-11 10:07:36 -0700345 * PCI_PASID_CAP_EXEC - Execute permission supported
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700346 * PCI_PASID_CAP_PRIV - Privileged mode supported
Joerg Roedel086ac112011-09-27 15:57:16 +0200347 */
348int pci_pasid_features(struct pci_dev *pdev)
349{
350 u16 supported;
351 int pos;
352
Alex Williamson69166fb2011-11-02 14:07:15 -0600353 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PASID);
Joerg Roedel086ac112011-09-27 15:57:16 +0200354 if (!pos)
355 return -EINVAL;
356
Alex Williamson91f57d52011-11-11 10:07:36 -0700357 pci_read_config_word(pdev, pos + PCI_PASID_CAP, &supported);
Joerg Roedel086ac112011-09-27 15:57:16 +0200358
Alex Williamson91f57d52011-11-11 10:07:36 -0700359 supported &= PCI_PASID_CAP_EXEC | PCI_PASID_CAP_PRIV;
Joerg Roedel086ac112011-09-27 15:57:16 +0200360
361 return supported;
362}
363EXPORT_SYMBOL_GPL(pci_pasid_features);
364
365#define PASID_NUMBER_SHIFT 8
366#define PASID_NUMBER_MASK (0x1f << PASID_NUMBER_SHIFT)
367/**
368 * pci_max_pasid - Get maximum number of PASIDs supported by device
369 * @pdev: PCI device structure
370 *
371 * Returns negative value when PASID capability is not present.
372 * Otherwise it returns the numer of supported PASIDs.
373 */
374int pci_max_pasids(struct pci_dev *pdev)
375{
376 u16 supported;
377 int pos;
378
Alex Williamson69166fb2011-11-02 14:07:15 -0600379 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PASID);
Joerg Roedel086ac112011-09-27 15:57:16 +0200380 if (!pos)
381 return -EINVAL;
382
Alex Williamson91f57d52011-11-11 10:07:36 -0700383 pci_read_config_word(pdev, pos + PCI_PASID_CAP, &supported);
Joerg Roedel086ac112011-09-27 15:57:16 +0200384
385 supported = (supported & PASID_NUMBER_MASK) >> PASID_NUMBER_SHIFT;
386
387 return (1 << supported);
388}
389EXPORT_SYMBOL_GPL(pci_max_pasids);
390#endif /* CONFIG_PCI_PASID */