blob: c97e862b538a0e6899c186fbe6df1cea105fb48c [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
Gil Kupfercef74402018-05-10 17:56:02 -050023 if (pci_ats_disabled())
24 return;
25
Joerg Roedeldb3c33c2011-09-27 15:57:13 +020026 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ATS);
27 if (!pos)
Bjorn Helgaasedc90fe2015-07-17 15:05:46 -050028 return;
Joerg Roedeldb3c33c2011-09-27 15:57:13 +020029
Bjorn Helgaasd544d752015-07-17 15:15:19 -050030 dev->ats_cap = pos;
Joerg Roedeldb3c33c2011-09-27 15:57:13 +020031}
32
33/**
34 * pci_enable_ats - enable the ATS capability
35 * @dev: the PCI device
36 * @ps: the IOMMU page shift
37 *
38 * Returns 0 on success, or negative on failure.
39 */
40int pci_enable_ats(struct pci_dev *dev, int ps)
41{
Joerg Roedeldb3c33c2011-09-27 15:57:13 +020042 u16 ctrl;
Bjorn Helgaasc39127d2015-07-17 15:38:13 -050043 struct pci_dev *pdev;
Joerg Roedeldb3c33c2011-09-27 15:57:13 +020044
Bjorn Helgaasd544d752015-07-17 15:15:19 -050045 if (!dev->ats_cap)
Bjorn Helgaasedc90fe2015-07-17 15:05:46 -050046 return -EINVAL;
47
Bjorn Helgaasf7ef1342015-07-20 09:23:37 -050048 if (WARN_ON(dev->ats_enabled))
Bjorn Helgaasa021f302015-07-17 15:43:27 -050049 return -EBUSY;
50
Joerg Roedeldb3c33c2011-09-27 15:57:13 +020051 if (ps < PCI_ATS_MIN_STU)
52 return -EINVAL;
53
Bjorn Helgaasedc90fe2015-07-17 15:05:46 -050054 /*
55 * Note that enabling ATS on a VF fails unless it's already enabled
56 * with the same STU on the PF.
57 */
Joerg Roedeldb3c33c2011-09-27 15:57:13 +020058 ctrl = PCI_ATS_CTRL_ENABLE;
Bjorn Helgaasedc90fe2015-07-17 15:05:46 -050059 if (dev->is_virtfn) {
Bjorn Helgaasc39127d2015-07-17 15:38:13 -050060 pdev = pci_physfn(dev);
Bjorn Helgaasd544d752015-07-17 15:15:19 -050061 if (pdev->ats_stu != ps)
Bjorn Helgaasedc90fe2015-07-17 15:05:46 -050062 return -EINVAL;
Bjorn Helgaasedc90fe2015-07-17 15:05:46 -050063 } else {
Bjorn Helgaasd544d752015-07-17 15:15:19 -050064 dev->ats_stu = ps;
65 ctrl |= PCI_ATS_CTRL_STU(dev->ats_stu - PCI_ATS_MIN_STU);
Bjorn Helgaasedc90fe2015-07-17 15:05:46 -050066 }
Bjorn Helgaasd544d752015-07-17 15:15:19 -050067 pci_write_config_word(dev, dev->ats_cap + PCI_ATS_CTRL, ctrl);
Joerg Roedeldb3c33c2011-09-27 15:57:13 +020068
Bjorn Helgaasd544d752015-07-17 15:15:19 -050069 dev->ats_enabled = 1;
Joerg Roedeldb3c33c2011-09-27 15:57:13 +020070 return 0;
71}
Joerg Roedeld4c06362011-09-27 15:57:14 +020072EXPORT_SYMBOL_GPL(pci_enable_ats);
Joerg Roedeldb3c33c2011-09-27 15:57:13 +020073
74/**
75 * pci_disable_ats - disable the ATS capability
76 * @dev: the PCI device
77 */
78void pci_disable_ats(struct pci_dev *dev)
79{
80 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 pci_read_config_word(dev, dev->ats_cap + PCI_ATS_CTRL, &ctrl);
Joerg Roedeldb3c33c2011-09-27 15:57:13 +020086 ctrl &= ~PCI_ATS_CTRL_ENABLE;
Bjorn Helgaasd544d752015-07-17 15:15:19 -050087 pci_write_config_word(dev, dev->ats_cap + PCI_ATS_CTRL, ctrl);
Joerg Roedeldb3c33c2011-09-27 15:57:13 +020088
Bjorn Helgaasd544d752015-07-17 15:15:19 -050089 dev->ats_enabled = 0;
Joerg Roedeldb3c33c2011-09-27 15:57:13 +020090}
Joerg Roedeld4c06362011-09-27 15:57:14 +020091EXPORT_SYMBOL_GPL(pci_disable_ats);
Joerg Roedeldb3c33c2011-09-27 15:57:13 +020092
Hao, Xudong1900ca12011-12-17 21:24:40 +080093void pci_restore_ats_state(struct pci_dev *dev)
94{
95 u16 ctrl;
96
Bjorn Helgaasf7ef1342015-07-20 09:23:37 -050097 if (!dev->ats_enabled)
Hao, Xudong1900ca12011-12-17 21:24:40 +080098 return;
Hao, Xudong1900ca12011-12-17 21:24:40 +080099
100 ctrl = PCI_ATS_CTRL_ENABLE;
101 if (!dev->is_virtfn)
Bjorn Helgaasd544d752015-07-17 15:15:19 -0500102 ctrl |= PCI_ATS_CTRL_STU(dev->ats_stu - PCI_ATS_MIN_STU);
103 pci_write_config_word(dev, dev->ats_cap + PCI_ATS_CTRL, ctrl);
Hao, Xudong1900ca12011-12-17 21:24:40 +0800104}
105EXPORT_SYMBOL_GPL(pci_restore_ats_state);
106
Joerg Roedeldb3c33c2011-09-27 15:57:13 +0200107/**
108 * pci_ats_queue_depth - query the ATS Invalidate Queue Depth
109 * @dev: the PCI device
110 *
111 * Returns the queue depth on success, or negative on failure.
112 *
113 * The ATS spec uses 0 in the Invalidate Queue Depth field to
114 * indicate that the function can accept 32 Invalidate Request.
115 * But here we use the `real' values (i.e. 1~32) for the Queue
116 * Depth; and 0 indicates the function shares the Queue with
117 * other functions (doesn't exclusively own a Queue).
118 */
119int pci_ats_queue_depth(struct pci_dev *dev)
120{
Bjorn Helgaasa71f9382015-07-20 09:24:32 -0500121 u16 cap;
122
Bjorn Helgaas3c765392015-07-17 15:30:26 -0500123 if (!dev->ats_cap)
124 return -EINVAL;
125
Joerg Roedeldb3c33c2011-09-27 15:57:13 +0200126 if (dev->is_virtfn)
127 return 0;
128
Bjorn Helgaasa71f9382015-07-20 09:24:32 -0500129 pci_read_config_word(dev, dev->ats_cap + PCI_ATS_CAP, &cap);
130 return PCI_ATS_CAP_QDEP(cap) ? PCI_ATS_CAP_QDEP(cap) : PCI_ATS_MAX_QDEP;
Joerg Roedeldb3c33c2011-09-27 15:57:13 +0200131}
Joerg Roedeld4c06362011-09-27 15:57:14 +0200132EXPORT_SYMBOL_GPL(pci_ats_queue_depth);
Joerg Roedelc320b972011-09-27 15:57:15 +0200133
Kuppuswamy Sathyanarayanan8c938dd2019-02-19 11:06:09 -0800134/**
135 * pci_ats_page_aligned - Return Page Aligned Request bit status.
136 * @pdev: the PCI device
137 *
138 * Returns 1, if the Untranslated Addresses generated by the device
139 * are always aligned or 0 otherwise.
140 *
141 * Per PCIe spec r4.0, sec 10.5.1.2, if the Page Aligned Request bit
142 * is set, it indicates the Untranslated Addresses generated by the
143 * device are always aligned to a 4096 byte boundary.
144 */
145int pci_ats_page_aligned(struct pci_dev *pdev)
146{
147 u16 cap;
148
149 if (!pdev->ats_cap)
150 return 0;
151
152 pci_read_config_word(pdev, pdev->ats_cap + PCI_ATS_CAP, &cap);
153
154 if (cap & PCI_ATS_CAP_PAGE_ALIGNED)
155 return 1;
156
157 return 0;
158}
159EXPORT_SYMBOL_GPL(pci_ats_page_aligned);
160
Joerg Roedelc320b972011-09-27 15:57:15 +0200161#ifdef CONFIG_PCI_PRI
Kuppuswamy Sathyanarayananc0651902019-09-05 14:31:45 -0500162void pci_pri_init(struct pci_dev *pdev)
163{
164 pdev->pri_cap = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
165}
166
Joerg Roedelc320b972011-09-27 15:57:15 +0200167/**
168 * pci_enable_pri - Enable PRI capability
169 * @ pdev: PCI device structure
170 *
171 * Returns 0 on success, negative value on error
172 */
173int pci_enable_pri(struct pci_dev *pdev, u32 reqs)
174{
175 u16 control, status;
176 u32 max_requests;
Kuppuswamy Sathyanarayananc0651902019-09-05 14:31:45 -0500177 int pri = pdev->pri_cap;
Joerg Roedelc320b972011-09-27 15:57:15 +0200178
Kuppuswamy Sathyanarayanan9bf49e32019-09-05 14:31:42 -0500179 /*
180 * VFs must not implement the PRI Capability. If their PF
181 * implements PRI, it is shared by the VFs, so if the PF PRI is
182 * enabled, it is also enabled for the VF.
183 */
184 if (pdev->is_virtfn) {
185 if (pci_physfn(pdev)->pri_enabled)
186 return 0;
187 return -EINVAL;
188 }
189
Jean-Philippe Bruckera4f4fa62017-05-30 09:25:48 -0700190 if (WARN_ON(pdev->pri_enabled))
191 return -EBUSY;
192
Kuppuswamy Sathyanarayananc0651902019-09-05 14:31:45 -0500193 if (!pri)
Joerg Roedelc320b972011-09-27 15:57:15 +0200194 return -EINVAL;
195
Kuppuswamy Sathyanarayananc0651902019-09-05 14:31:45 -0500196 pci_read_config_word(pdev, pri + PCI_PRI_STATUS, &status);
CQ Tang4ebeb1e2017-05-30 09:25:49 -0700197 if (!(status & PCI_PRI_STATUS_STOPPED))
Joerg Roedelc320b972011-09-27 15:57:15 +0200198 return -EBUSY;
199
Kuppuswamy Sathyanarayananc0651902019-09-05 14:31:45 -0500200 pci_read_config_dword(pdev, pri + PCI_PRI_MAX_REQ, &max_requests);
Joerg Roedelc320b972011-09-27 15:57:15 +0200201 reqs = min(max_requests, reqs);
CQ Tang4ebeb1e2017-05-30 09:25:49 -0700202 pdev->pri_reqs_alloc = reqs;
Kuppuswamy Sathyanarayananc0651902019-09-05 14:31:45 -0500203 pci_write_config_dword(pdev, pri + PCI_PRI_ALLOC_REQ, reqs);
Joerg Roedelc320b972011-09-27 15:57:15 +0200204
CQ Tang4ebeb1e2017-05-30 09:25:49 -0700205 control = PCI_PRI_CTRL_ENABLE;
Kuppuswamy Sathyanarayananc0651902019-09-05 14:31:45 -0500206 pci_write_config_word(pdev, pri + PCI_PRI_CTRL, control);
Joerg Roedelc320b972011-09-27 15:57:15 +0200207
Jean-Philippe Bruckera4f4fa62017-05-30 09:25:48 -0700208 pdev->pri_enabled = 1;
209
Joerg Roedelc320b972011-09-27 15:57:15 +0200210 return 0;
211}
212EXPORT_SYMBOL_GPL(pci_enable_pri);
213
214/**
215 * pci_disable_pri - Disable PRI capability
216 * @pdev: PCI device structure
217 *
218 * Only clears the enabled-bit, regardless of its former value
219 */
220void pci_disable_pri(struct pci_dev *pdev)
221{
222 u16 control;
Kuppuswamy Sathyanarayananc0651902019-09-05 14:31:45 -0500223 int pri = pdev->pri_cap;
Joerg Roedelc320b972011-09-27 15:57:15 +0200224
Kuppuswamy Sathyanarayanan9bf49e32019-09-05 14:31:42 -0500225 /* VFs share the PF PRI */
226 if (pdev->is_virtfn)
227 return;
228
Jean-Philippe Bruckera4f4fa62017-05-30 09:25:48 -0700229 if (WARN_ON(!pdev->pri_enabled))
230 return;
231
Kuppuswamy Sathyanarayananc0651902019-09-05 14:31:45 -0500232 if (!pri)
Joerg Roedelc320b972011-09-27 15:57:15 +0200233 return;
234
Kuppuswamy Sathyanarayananc0651902019-09-05 14:31:45 -0500235 pci_read_config_word(pdev, pri + PCI_PRI_CTRL, &control);
Alex Williamson91f57d52011-11-11 10:07:36 -0700236 control &= ~PCI_PRI_CTRL_ENABLE;
Kuppuswamy Sathyanarayananc0651902019-09-05 14:31:45 -0500237 pci_write_config_word(pdev, pri + PCI_PRI_CTRL, control);
Jean-Philippe Bruckera4f4fa62017-05-30 09:25:48 -0700238
239 pdev->pri_enabled = 0;
Joerg Roedelc320b972011-09-27 15:57:15 +0200240}
241EXPORT_SYMBOL_GPL(pci_disable_pri);
242
243/**
CQ Tang4ebeb1e2017-05-30 09:25:49 -0700244 * pci_restore_pri_state - Restore PRI
245 * @pdev: PCI device structure
246 */
247void pci_restore_pri_state(struct pci_dev *pdev)
248{
249 u16 control = PCI_PRI_CTRL_ENABLE;
250 u32 reqs = pdev->pri_reqs_alloc;
Kuppuswamy Sathyanarayananc0651902019-09-05 14:31:45 -0500251 int pri = pdev->pri_cap;
CQ Tang4ebeb1e2017-05-30 09:25:49 -0700252
Kuppuswamy Sathyanarayanan9bf49e32019-09-05 14:31:42 -0500253 if (pdev->is_virtfn)
254 return;
255
CQ Tang4ebeb1e2017-05-30 09:25:49 -0700256 if (!pdev->pri_enabled)
257 return;
258
Kuppuswamy Sathyanarayananc0651902019-09-05 14:31:45 -0500259 if (!pri)
CQ Tang4ebeb1e2017-05-30 09:25:49 -0700260 return;
261
Kuppuswamy Sathyanarayananc0651902019-09-05 14:31:45 -0500262 pci_write_config_dword(pdev, pri + PCI_PRI_ALLOC_REQ, reqs);
263 pci_write_config_word(pdev, pri + PCI_PRI_CTRL, control);
CQ Tang4ebeb1e2017-05-30 09:25:49 -0700264}
265EXPORT_SYMBOL_GPL(pci_restore_pri_state);
266
267/**
Joerg Roedelc320b972011-09-27 15:57:15 +0200268 * pci_reset_pri - Resets device's PRI state
269 * @pdev: PCI device structure
270 *
271 * The PRI capability must be disabled before this function is called.
272 * Returns 0 on success, negative value on error.
273 */
274int pci_reset_pri(struct pci_dev *pdev)
275{
276 u16 control;
Kuppuswamy Sathyanarayananc0651902019-09-05 14:31:45 -0500277 int pri = pdev->pri_cap;
Joerg Roedelc320b972011-09-27 15:57:15 +0200278
Kuppuswamy Sathyanarayanan9bf49e32019-09-05 14:31:42 -0500279 if (pdev->is_virtfn)
280 return 0;
281
Jean-Philippe Bruckera4f4fa62017-05-30 09:25:48 -0700282 if (WARN_ON(pdev->pri_enabled))
283 return -EBUSY;
284
Kuppuswamy Sathyanarayananc0651902019-09-05 14:31:45 -0500285 if (!pri)
Joerg Roedelc320b972011-09-27 15:57:15 +0200286 return -EINVAL;
287
CQ Tang4ebeb1e2017-05-30 09:25:49 -0700288 control = PCI_PRI_CTRL_RESET;
Kuppuswamy Sathyanarayananc0651902019-09-05 14:31:45 -0500289 pci_write_config_word(pdev, pri + PCI_PRI_CTRL, control);
Joerg Roedelc320b972011-09-27 15:57:15 +0200290
291 return 0;
292}
293EXPORT_SYMBOL_GPL(pci_reset_pri);
Bjorn Helgaas8cbb8a92019-10-09 14:54:01 -0500294
295/**
296 * pci_prg_resp_pasid_required - Return PRG Response PASID Required bit
297 * status.
298 * @pdev: PCI device structure
299 *
300 * Returns 1 if PASID is required in PRG Response Message, 0 otherwise.
301 */
302int pci_prg_resp_pasid_required(struct pci_dev *pdev)
303{
304 u16 status;
Kuppuswamy Sathyanarayananc0651902019-09-05 14:31:45 -0500305 int pri;
Bjorn Helgaas8cbb8a92019-10-09 14:54:01 -0500306
Kuppuswamy Sathyanarayanan9bf49e32019-09-05 14:31:42 -0500307 if (pdev->is_virtfn)
308 pdev = pci_physfn(pdev);
309
Kuppuswamy Sathyanarayananc0651902019-09-05 14:31:45 -0500310 pri = pdev->pri_cap;
311 if (!pri)
Bjorn Helgaas8cbb8a92019-10-09 14:54:01 -0500312 return 0;
313
Kuppuswamy Sathyanarayananc0651902019-09-05 14:31:45 -0500314 pci_read_config_word(pdev, pri + PCI_PRI_STATUS, &status);
Bjorn Helgaas8cbb8a92019-10-09 14:54:01 -0500315
316 if (status & PCI_PRI_STATUS_PASID)
317 return 1;
318
319 return 0;
320}
321EXPORT_SYMBOL_GPL(pci_prg_resp_pasid_required);
Joerg Roedelc320b972011-09-27 15:57:15 +0200322#endif /* CONFIG_PCI_PRI */
Joerg Roedel086ac112011-09-27 15:57:16 +0200323
324#ifdef CONFIG_PCI_PASID
325/**
326 * pci_enable_pasid - Enable the PASID capability
327 * @pdev: PCI device structure
328 * @features: Features to enable
329 *
330 * Returns 0 on success, negative value on error. This function checks
331 * whether the features are actually supported by the device and returns
332 * an error if not.
333 */
334int pci_enable_pasid(struct pci_dev *pdev, int features)
335{
336 u16 control, supported;
337 int pos;
338
Kuppuswamy Sathyanarayanan2b0ae7c2019-09-05 14:31:43 -0500339 /*
340 * VFs must not implement the PASID Capability, but if a PF
341 * supports PASID, its VFs share the PF PASID configuration.
342 */
343 if (pdev->is_virtfn) {
344 if (pci_physfn(pdev)->pasid_enabled)
345 return 0;
346 return -EINVAL;
347 }
348
Jean-Philippe Bruckera4f4fa62017-05-30 09:25:48 -0700349 if (WARN_ON(pdev->pasid_enabled))
350 return -EBUSY;
351
Sinan Kaya7ce3f912018-06-30 11:24:24 -0400352 if (!pdev->eetlp_prefix_path)
353 return -EINVAL;
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);
Alex Williamson91f57d52011-11-11 10:07:36 -0700360 supported &= PCI_PASID_CAP_EXEC | PCI_PASID_CAP_PRIV;
Joerg Roedel086ac112011-09-27 15:57:16 +0200361
362 /* User wants to enable anything unsupported? */
363 if ((supported & features) != features)
364 return -EINVAL;
365
Alex Williamson91f57d52011-11-11 10:07:36 -0700366 control = PCI_PASID_CTRL_ENABLE | features;
CQ Tang4ebeb1e2017-05-30 09:25:49 -0700367 pdev->pasid_features = features;
Joerg Roedel086ac112011-09-27 15:57:16 +0200368
Alex Williamson91f57d52011-11-11 10:07:36 -0700369 pci_write_config_word(pdev, pos + PCI_PASID_CTRL, control);
Joerg Roedel086ac112011-09-27 15:57:16 +0200370
Jean-Philippe Bruckera4f4fa62017-05-30 09:25:48 -0700371 pdev->pasid_enabled = 1;
372
Joerg Roedel086ac112011-09-27 15:57:16 +0200373 return 0;
374}
375EXPORT_SYMBOL_GPL(pci_enable_pasid);
376
377/**
378 * pci_disable_pasid - Disable the PASID capability
379 * @pdev: PCI device structure
Joerg Roedel086ac112011-09-27 15:57:16 +0200380 */
381void pci_disable_pasid(struct pci_dev *pdev)
382{
383 u16 control = 0;
384 int pos;
385
Kuppuswamy Sathyanarayanan2b0ae7c2019-09-05 14:31:43 -0500386 /* VFs share the PF PASID configuration */
387 if (pdev->is_virtfn)
388 return;
389
Jean-Philippe Bruckera4f4fa62017-05-30 09:25:48 -0700390 if (WARN_ON(!pdev->pasid_enabled))
391 return;
392
Alex Williamson69166fb2011-11-02 14:07:15 -0600393 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PASID);
Joerg Roedel086ac112011-09-27 15:57:16 +0200394 if (!pos)
395 return;
396
Alex Williamson91f57d52011-11-11 10:07:36 -0700397 pci_write_config_word(pdev, pos + PCI_PASID_CTRL, control);
Jean-Philippe Bruckera4f4fa62017-05-30 09:25:48 -0700398
399 pdev->pasid_enabled = 0;
Joerg Roedel086ac112011-09-27 15:57:16 +0200400}
401EXPORT_SYMBOL_GPL(pci_disable_pasid);
402
403/**
CQ Tang4ebeb1e2017-05-30 09:25:49 -0700404 * pci_restore_pasid_state - Restore PASID capabilities
405 * @pdev: PCI device structure
406 */
407void pci_restore_pasid_state(struct pci_dev *pdev)
408{
409 u16 control;
410 int pos;
411
Kuppuswamy Sathyanarayanan2b0ae7c2019-09-05 14:31:43 -0500412 if (pdev->is_virtfn)
413 return;
414
CQ Tang4ebeb1e2017-05-30 09:25:49 -0700415 if (!pdev->pasid_enabled)
416 return;
417
418 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PASID);
419 if (!pos)
420 return;
421
422 control = PCI_PASID_CTRL_ENABLE | pdev->pasid_features;
423 pci_write_config_word(pdev, pos + PCI_PASID_CTRL, control);
424}
425EXPORT_SYMBOL_GPL(pci_restore_pasid_state);
426
427/**
Joerg Roedel086ac112011-09-27 15:57:16 +0200428 * pci_pasid_features - Check which PASID features are supported
429 * @pdev: PCI device structure
430 *
431 * Returns a negative value when no PASI capability is present.
432 * Otherwise is returns a bitmask with supported features. Current
433 * features reported are:
Alex Williamson91f57d52011-11-11 10:07:36 -0700434 * PCI_PASID_CAP_EXEC - Execute permission supported
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700435 * PCI_PASID_CAP_PRIV - Privileged mode supported
Joerg Roedel086ac112011-09-27 15:57:16 +0200436 */
437int pci_pasid_features(struct pci_dev *pdev)
438{
439 u16 supported;
440 int pos;
441
Kuppuswamy Sathyanarayanan2b0ae7c2019-09-05 14:31:43 -0500442 if (pdev->is_virtfn)
443 pdev = pci_physfn(pdev);
444
Alex Williamson69166fb2011-11-02 14:07:15 -0600445 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PASID);
Joerg Roedel086ac112011-09-27 15:57:16 +0200446 if (!pos)
447 return -EINVAL;
448
Alex Williamson91f57d52011-11-11 10:07:36 -0700449 pci_read_config_word(pdev, pos + PCI_PASID_CAP, &supported);
Joerg Roedel086ac112011-09-27 15:57:16 +0200450
Alex Williamson91f57d52011-11-11 10:07:36 -0700451 supported &= PCI_PASID_CAP_EXEC | PCI_PASID_CAP_PRIV;
Joerg Roedel086ac112011-09-27 15:57:16 +0200452
453 return supported;
454}
455EXPORT_SYMBOL_GPL(pci_pasid_features);
456
457#define PASID_NUMBER_SHIFT 8
458#define PASID_NUMBER_MASK (0x1f << PASID_NUMBER_SHIFT)
459/**
460 * pci_max_pasid - Get maximum number of PASIDs supported by device
461 * @pdev: PCI device structure
462 *
463 * Returns negative value when PASID capability is not present.
Bjorn Helgaasf6b6aef2019-05-30 08:05:58 -0500464 * Otherwise it returns the number of supported PASIDs.
Joerg Roedel086ac112011-09-27 15:57:16 +0200465 */
466int pci_max_pasids(struct pci_dev *pdev)
467{
468 u16 supported;
469 int pos;
470
Kuppuswamy Sathyanarayanan2b0ae7c2019-09-05 14:31:43 -0500471 if (pdev->is_virtfn)
472 pdev = pci_physfn(pdev);
473
Alex Williamson69166fb2011-11-02 14:07:15 -0600474 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PASID);
Joerg Roedel086ac112011-09-27 15:57:16 +0200475 if (!pos)
476 return -EINVAL;
477
Alex Williamson91f57d52011-11-11 10:07:36 -0700478 pci_read_config_word(pdev, pos + PCI_PASID_CAP, &supported);
Joerg Roedel086ac112011-09-27 15:57:16 +0200479
480 supported = (supported & PASID_NUMBER_MASK) >> PASID_NUMBER_SHIFT;
481
482 return (1 << supported);
483}
484EXPORT_SYMBOL_GPL(pci_max_pasids);
485#endif /* CONFIG_PCI_PASID */