blob: dcbcf1331bb24f33a579116261a7c1012e0f070a [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}
Greg Kroah-Hartmanbb950bc2019-12-19 12:03:39 +000072EXPORT_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}
Greg Kroah-Hartmanbb950bc2019-12-19 12:03:39 +000091EXPORT_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}
Hao, Xudong1900ca12011-12-17 21:24:40 +0800105
Joerg Roedeldb3c33c2011-09-27 15:57:13 +0200106/**
107 * pci_ats_queue_depth - query the ATS Invalidate Queue Depth
108 * @dev: the PCI device
109 *
110 * Returns the queue depth on success, or negative on failure.
111 *
112 * The ATS spec uses 0 in the Invalidate Queue Depth field to
113 * indicate that the function can accept 32 Invalidate Request.
114 * But here we use the `real' values (i.e. 1~32) for the Queue
115 * Depth; and 0 indicates the function shares the Queue with
116 * other functions (doesn't exclusively own a Queue).
117 */
118int pci_ats_queue_depth(struct pci_dev *dev)
119{
Bjorn Helgaasa71f9382015-07-20 09:24:32 -0500120 u16 cap;
121
Bjorn Helgaas3c765392015-07-17 15:30:26 -0500122 if (!dev->ats_cap)
123 return -EINVAL;
124
Joerg Roedeldb3c33c2011-09-27 15:57:13 +0200125 if (dev->is_virtfn)
126 return 0;
127
Bjorn Helgaasa71f9382015-07-20 09:24:32 -0500128 pci_read_config_word(dev, dev->ats_cap + PCI_ATS_CAP, &cap);
129 return PCI_ATS_CAP_QDEP(cap) ? PCI_ATS_CAP_QDEP(cap) : PCI_ATS_MAX_QDEP;
Joerg Roedeldb3c33c2011-09-27 15:57:13 +0200130}
Joerg Roedelc320b972011-09-27 15:57:15 +0200131
Kuppuswamy Sathyanarayanan8c938dd2019-02-19 11:06:09 -0800132/**
133 * pci_ats_page_aligned - Return Page Aligned Request bit status.
134 * @pdev: the PCI device
135 *
136 * Returns 1, if the Untranslated Addresses generated by the device
137 * are always aligned or 0 otherwise.
138 *
139 * Per PCIe spec r4.0, sec 10.5.1.2, if the Page Aligned Request bit
140 * is set, it indicates the Untranslated Addresses generated by the
141 * device are always aligned to a 4096 byte boundary.
142 */
143int pci_ats_page_aligned(struct pci_dev *pdev)
144{
145 u16 cap;
146
147 if (!pdev->ats_cap)
148 return 0;
149
150 pci_read_config_word(pdev, pdev->ats_cap + PCI_ATS_CAP, &cap);
151
152 if (cap & PCI_ATS_CAP_PAGE_ALIGNED)
153 return 1;
154
155 return 0;
156}
Kuppuswamy Sathyanarayanan8c938dd2019-02-19 11:06:09 -0800157
Joerg Roedelc320b972011-09-27 15:57:15 +0200158#ifdef CONFIG_PCI_PRI
Kuppuswamy Sathyanarayananc0651902019-09-05 14:31:45 -0500159void pci_pri_init(struct pci_dev *pdev)
160{
Bjorn Helgaase5adf792019-10-09 16:07:51 -0500161 u16 status;
162
Kuppuswamy Sathyanarayananc0651902019-09-05 14:31:45 -0500163 pdev->pri_cap = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
Bjorn Helgaase5adf792019-10-09 16:07:51 -0500164
165 if (!pdev->pri_cap)
166 return;
167
168 pci_read_config_word(pdev, pdev->pri_cap + PCI_PRI_STATUS, &status);
169 if (status & PCI_PRI_STATUS_PASID)
170 pdev->pasid_required = 1;
Kuppuswamy Sathyanarayananc0651902019-09-05 14:31:45 -0500171}
172
Joerg Roedelc320b972011-09-27 15:57:15 +0200173/**
174 * pci_enable_pri - Enable PRI capability
175 * @ pdev: PCI device structure
176 *
177 * Returns 0 on success, negative value on error
178 */
179int pci_enable_pri(struct pci_dev *pdev, u32 reqs)
180{
181 u16 control, status;
182 u32 max_requests;
Kuppuswamy Sathyanarayananc0651902019-09-05 14:31:45 -0500183 int pri = pdev->pri_cap;
Joerg Roedelc320b972011-09-27 15:57:15 +0200184
Kuppuswamy Sathyanarayanan9bf49e32019-09-05 14:31:42 -0500185 /*
186 * VFs must not implement the PRI Capability. If their PF
187 * implements PRI, it is shared by the VFs, so if the PF PRI is
188 * enabled, it is also enabled for the VF.
189 */
190 if (pdev->is_virtfn) {
191 if (pci_physfn(pdev)->pri_enabled)
192 return 0;
193 return -EINVAL;
194 }
195
Jean-Philippe Bruckera4f4fa62017-05-30 09:25:48 -0700196 if (WARN_ON(pdev->pri_enabled))
197 return -EBUSY;
198
Kuppuswamy Sathyanarayananc0651902019-09-05 14:31:45 -0500199 if (!pri)
Joerg Roedelc320b972011-09-27 15:57:15 +0200200 return -EINVAL;
201
Kuppuswamy Sathyanarayananc0651902019-09-05 14:31:45 -0500202 pci_read_config_word(pdev, pri + PCI_PRI_STATUS, &status);
CQ Tang4ebeb1e2017-05-30 09:25:49 -0700203 if (!(status & PCI_PRI_STATUS_STOPPED))
Joerg Roedelc320b972011-09-27 15:57:15 +0200204 return -EBUSY;
205
Kuppuswamy Sathyanarayananc0651902019-09-05 14:31:45 -0500206 pci_read_config_dword(pdev, pri + PCI_PRI_MAX_REQ, &max_requests);
Joerg Roedelc320b972011-09-27 15:57:15 +0200207 reqs = min(max_requests, reqs);
CQ Tang4ebeb1e2017-05-30 09:25:49 -0700208 pdev->pri_reqs_alloc = reqs;
Kuppuswamy Sathyanarayananc0651902019-09-05 14:31:45 -0500209 pci_write_config_dword(pdev, pri + PCI_PRI_ALLOC_REQ, reqs);
Joerg Roedelc320b972011-09-27 15:57:15 +0200210
CQ Tang4ebeb1e2017-05-30 09:25:49 -0700211 control = PCI_PRI_CTRL_ENABLE;
Kuppuswamy Sathyanarayananc0651902019-09-05 14:31:45 -0500212 pci_write_config_word(pdev, pri + PCI_PRI_CTRL, control);
Joerg Roedelc320b972011-09-27 15:57:15 +0200213
Jean-Philippe Bruckera4f4fa62017-05-30 09:25:48 -0700214 pdev->pri_enabled = 1;
215
Joerg Roedelc320b972011-09-27 15:57:15 +0200216 return 0;
217}
Joerg Roedelc320b972011-09-27 15:57:15 +0200218
219/**
220 * pci_disable_pri - Disable PRI capability
221 * @pdev: PCI device structure
222 *
223 * Only clears the enabled-bit, regardless of its former value
224 */
225void pci_disable_pri(struct pci_dev *pdev)
226{
227 u16 control;
Kuppuswamy Sathyanarayananc0651902019-09-05 14:31:45 -0500228 int pri = pdev->pri_cap;
Joerg Roedelc320b972011-09-27 15:57:15 +0200229
Kuppuswamy Sathyanarayanan9bf49e32019-09-05 14:31:42 -0500230 /* VFs share the PF PRI */
231 if (pdev->is_virtfn)
232 return;
233
Jean-Philippe Bruckera4f4fa62017-05-30 09:25:48 -0700234 if (WARN_ON(!pdev->pri_enabled))
235 return;
236
Kuppuswamy Sathyanarayananc0651902019-09-05 14:31:45 -0500237 if (!pri)
Joerg Roedelc320b972011-09-27 15:57:15 +0200238 return;
239
Kuppuswamy Sathyanarayananc0651902019-09-05 14:31:45 -0500240 pci_read_config_word(pdev, pri + PCI_PRI_CTRL, &control);
Alex Williamson91f57d52011-11-11 10:07:36 -0700241 control &= ~PCI_PRI_CTRL_ENABLE;
Kuppuswamy Sathyanarayananc0651902019-09-05 14:31:45 -0500242 pci_write_config_word(pdev, pri + PCI_PRI_CTRL, control);
Jean-Philippe Bruckera4f4fa62017-05-30 09:25:48 -0700243
244 pdev->pri_enabled = 0;
Joerg Roedelc320b972011-09-27 15:57:15 +0200245}
246EXPORT_SYMBOL_GPL(pci_disable_pri);
247
248/**
CQ Tang4ebeb1e2017-05-30 09:25:49 -0700249 * pci_restore_pri_state - Restore PRI
250 * @pdev: PCI device structure
251 */
252void pci_restore_pri_state(struct pci_dev *pdev)
253{
254 u16 control = PCI_PRI_CTRL_ENABLE;
255 u32 reqs = pdev->pri_reqs_alloc;
Kuppuswamy Sathyanarayananc0651902019-09-05 14:31:45 -0500256 int pri = pdev->pri_cap;
CQ Tang4ebeb1e2017-05-30 09:25:49 -0700257
Kuppuswamy Sathyanarayanan9bf49e32019-09-05 14:31:42 -0500258 if (pdev->is_virtfn)
259 return;
260
CQ Tang4ebeb1e2017-05-30 09:25:49 -0700261 if (!pdev->pri_enabled)
262 return;
263
Kuppuswamy Sathyanarayananc0651902019-09-05 14:31:45 -0500264 if (!pri)
CQ Tang4ebeb1e2017-05-30 09:25:49 -0700265 return;
266
Kuppuswamy Sathyanarayananc0651902019-09-05 14:31:45 -0500267 pci_write_config_dword(pdev, pri + PCI_PRI_ALLOC_REQ, reqs);
268 pci_write_config_word(pdev, pri + PCI_PRI_CTRL, control);
CQ Tang4ebeb1e2017-05-30 09:25:49 -0700269}
CQ Tang4ebeb1e2017-05-30 09:25:49 -0700270
271/**
Joerg Roedelc320b972011-09-27 15:57:15 +0200272 * pci_reset_pri - Resets device's PRI state
273 * @pdev: PCI device structure
274 *
275 * The PRI capability must be disabled before this function is called.
276 * Returns 0 on success, negative value on error.
277 */
278int pci_reset_pri(struct pci_dev *pdev)
279{
280 u16 control;
Kuppuswamy Sathyanarayananc0651902019-09-05 14:31:45 -0500281 int pri = pdev->pri_cap;
Joerg Roedelc320b972011-09-27 15:57:15 +0200282
Kuppuswamy Sathyanarayanan9bf49e32019-09-05 14:31:42 -0500283 if (pdev->is_virtfn)
284 return 0;
285
Jean-Philippe Bruckera4f4fa62017-05-30 09:25:48 -0700286 if (WARN_ON(pdev->pri_enabled))
287 return -EBUSY;
288
Kuppuswamy Sathyanarayananc0651902019-09-05 14:31:45 -0500289 if (!pri)
Joerg Roedelc320b972011-09-27 15:57:15 +0200290 return -EINVAL;
291
CQ Tang4ebeb1e2017-05-30 09:25:49 -0700292 control = PCI_PRI_CTRL_RESET;
Kuppuswamy Sathyanarayananc0651902019-09-05 14:31:45 -0500293 pci_write_config_word(pdev, pri + PCI_PRI_CTRL, control);
Joerg Roedelc320b972011-09-27 15:57:15 +0200294
295 return 0;
296}
Bjorn Helgaas8cbb8a92019-10-09 14:54:01 -0500297
298/**
299 * pci_prg_resp_pasid_required - Return PRG Response PASID Required bit
300 * status.
301 * @pdev: PCI device structure
302 *
303 * Returns 1 if PASID is required in PRG Response Message, 0 otherwise.
304 */
305int pci_prg_resp_pasid_required(struct pci_dev *pdev)
306{
Kuppuswamy Sathyanarayanan9bf49e32019-09-05 14:31:42 -0500307 if (pdev->is_virtfn)
308 pdev = pci_physfn(pdev);
309
Bjorn Helgaase5adf792019-10-09 16:07:51 -0500310 return pdev->pasid_required;
Bjorn Helgaas8cbb8a92019-10-09 14:54:01 -0500311}
Joerg Roedelc320b972011-09-27 15:57:15 +0200312#endif /* CONFIG_PCI_PRI */
Joerg Roedel086ac112011-09-27 15:57:16 +0200313
314#ifdef CONFIG_PCI_PASID
Kuppuswamy Sathyanarayanan751035b2019-09-05 14:31:46 -0500315void pci_pasid_init(struct pci_dev *pdev)
316{
317 pdev->pasid_cap = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PASID);
318}
319
Joerg Roedel086ac112011-09-27 15:57:16 +0200320/**
321 * pci_enable_pasid - Enable the PASID capability
322 * @pdev: PCI device structure
323 * @features: Features to enable
324 *
325 * Returns 0 on success, negative value on error. This function checks
326 * whether the features are actually supported by the device and returns
327 * an error if not.
328 */
329int pci_enable_pasid(struct pci_dev *pdev, int features)
330{
331 u16 control, supported;
Kuppuswamy Sathyanarayanan751035b2019-09-05 14:31:46 -0500332 int pasid = pdev->pasid_cap;
Joerg Roedel086ac112011-09-27 15:57:16 +0200333
Kuppuswamy Sathyanarayanan2b0ae7c2019-09-05 14:31:43 -0500334 /*
335 * VFs must not implement the PASID Capability, but if a PF
336 * supports PASID, its VFs share the PF PASID configuration.
337 */
338 if (pdev->is_virtfn) {
339 if (pci_physfn(pdev)->pasid_enabled)
340 return 0;
341 return -EINVAL;
342 }
343
Jean-Philippe Bruckera4f4fa62017-05-30 09:25:48 -0700344 if (WARN_ON(pdev->pasid_enabled))
345 return -EBUSY;
346
Sinan Kaya7ce3f912018-06-30 11:24:24 -0400347 if (!pdev->eetlp_prefix_path)
348 return -EINVAL;
349
Kuppuswamy Sathyanarayanan751035b2019-09-05 14:31:46 -0500350 if (!pasid)
Joerg Roedel086ac112011-09-27 15:57:16 +0200351 return -EINVAL;
352
Kuppuswamy Sathyanarayanan751035b2019-09-05 14:31:46 -0500353 pci_read_config_word(pdev, pasid + PCI_PASID_CAP, &supported);
Alex Williamson91f57d52011-11-11 10:07:36 -0700354 supported &= PCI_PASID_CAP_EXEC | PCI_PASID_CAP_PRIV;
Joerg Roedel086ac112011-09-27 15:57:16 +0200355
356 /* User wants to enable anything unsupported? */
357 if ((supported & features) != features)
358 return -EINVAL;
359
Alex Williamson91f57d52011-11-11 10:07:36 -0700360 control = PCI_PASID_CTRL_ENABLE | features;
CQ Tang4ebeb1e2017-05-30 09:25:49 -0700361 pdev->pasid_features = features;
Joerg Roedel086ac112011-09-27 15:57:16 +0200362
Kuppuswamy Sathyanarayanan751035b2019-09-05 14:31:46 -0500363 pci_write_config_word(pdev, pasid + PCI_PASID_CTRL, control);
Joerg Roedel086ac112011-09-27 15:57:16 +0200364
Jean-Philippe Bruckera4f4fa62017-05-30 09:25:48 -0700365 pdev->pasid_enabled = 1;
366
Joerg Roedel086ac112011-09-27 15:57:16 +0200367 return 0;
368}
Joerg Roedel086ac112011-09-27 15:57:16 +0200369
370/**
371 * pci_disable_pasid - Disable the PASID capability
372 * @pdev: PCI device structure
Joerg Roedel086ac112011-09-27 15:57:16 +0200373 */
374void pci_disable_pasid(struct pci_dev *pdev)
375{
376 u16 control = 0;
Kuppuswamy Sathyanarayanan751035b2019-09-05 14:31:46 -0500377 int pasid = pdev->pasid_cap;
Joerg Roedel086ac112011-09-27 15:57:16 +0200378
Kuppuswamy Sathyanarayanan2b0ae7c2019-09-05 14:31:43 -0500379 /* VFs share the PF PASID configuration */
380 if (pdev->is_virtfn)
381 return;
382
Jean-Philippe Bruckera4f4fa62017-05-30 09:25:48 -0700383 if (WARN_ON(!pdev->pasid_enabled))
384 return;
385
Kuppuswamy Sathyanarayanan751035b2019-09-05 14:31:46 -0500386 if (!pasid)
Joerg Roedel086ac112011-09-27 15:57:16 +0200387 return;
388
Kuppuswamy Sathyanarayanan751035b2019-09-05 14:31:46 -0500389 pci_write_config_word(pdev, pasid + PCI_PASID_CTRL, control);
Jean-Philippe Bruckera4f4fa62017-05-30 09:25:48 -0700390
391 pdev->pasid_enabled = 0;
Joerg Roedel086ac112011-09-27 15:57:16 +0200392}
Joerg Roedel086ac112011-09-27 15:57:16 +0200393
394/**
CQ Tang4ebeb1e2017-05-30 09:25:49 -0700395 * pci_restore_pasid_state - Restore PASID capabilities
396 * @pdev: PCI device structure
397 */
398void pci_restore_pasid_state(struct pci_dev *pdev)
399{
400 u16 control;
Kuppuswamy Sathyanarayanan751035b2019-09-05 14:31:46 -0500401 int pasid = pdev->pasid_cap;
CQ Tang4ebeb1e2017-05-30 09:25:49 -0700402
Kuppuswamy Sathyanarayanan2b0ae7c2019-09-05 14:31:43 -0500403 if (pdev->is_virtfn)
404 return;
405
CQ Tang4ebeb1e2017-05-30 09:25:49 -0700406 if (!pdev->pasid_enabled)
407 return;
408
Kuppuswamy Sathyanarayanan751035b2019-09-05 14:31:46 -0500409 if (!pasid)
CQ Tang4ebeb1e2017-05-30 09:25:49 -0700410 return;
411
412 control = PCI_PASID_CTRL_ENABLE | pdev->pasid_features;
Kuppuswamy Sathyanarayanan751035b2019-09-05 14:31:46 -0500413 pci_write_config_word(pdev, pasid + PCI_PASID_CTRL, control);
CQ Tang4ebeb1e2017-05-30 09:25:49 -0700414}
CQ Tang4ebeb1e2017-05-30 09:25:49 -0700415
416/**
Joerg Roedel086ac112011-09-27 15:57:16 +0200417 * pci_pasid_features - Check which PASID features are supported
418 * @pdev: PCI device structure
419 *
420 * Returns a negative value when no PASI capability is present.
421 * Otherwise is returns a bitmask with supported features. Current
422 * features reported are:
Alex Williamson91f57d52011-11-11 10:07:36 -0700423 * PCI_PASID_CAP_EXEC - Execute permission supported
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700424 * PCI_PASID_CAP_PRIV - Privileged mode supported
Joerg Roedel086ac112011-09-27 15:57:16 +0200425 */
426int pci_pasid_features(struct pci_dev *pdev)
427{
428 u16 supported;
Kuppuswamy Sathyanarayanan751035b2019-09-05 14:31:46 -0500429 int pasid = pdev->pasid_cap;
Joerg Roedel086ac112011-09-27 15:57:16 +0200430
Kuppuswamy Sathyanarayanan2b0ae7c2019-09-05 14:31:43 -0500431 if (pdev->is_virtfn)
432 pdev = pci_physfn(pdev);
433
Kuppuswamy Sathyanarayanan751035b2019-09-05 14:31:46 -0500434 if (!pasid)
Joerg Roedel086ac112011-09-27 15:57:16 +0200435 return -EINVAL;
436
Kuppuswamy Sathyanarayanan751035b2019-09-05 14:31:46 -0500437 pci_read_config_word(pdev, pasid + PCI_PASID_CAP, &supported);
Joerg Roedel086ac112011-09-27 15:57:16 +0200438
Alex Williamson91f57d52011-11-11 10:07:36 -0700439 supported &= PCI_PASID_CAP_EXEC | PCI_PASID_CAP_PRIV;
Joerg Roedel086ac112011-09-27 15:57:16 +0200440
441 return supported;
442}
Joerg Roedel086ac112011-09-27 15:57:16 +0200443
444#define PASID_NUMBER_SHIFT 8
445#define PASID_NUMBER_MASK (0x1f << PASID_NUMBER_SHIFT)
446/**
447 * pci_max_pasid - Get maximum number of PASIDs supported by device
448 * @pdev: PCI device structure
449 *
450 * Returns negative value when PASID capability is not present.
Bjorn Helgaasf6b6aef2019-05-30 08:05:58 -0500451 * Otherwise it returns the number of supported PASIDs.
Joerg Roedel086ac112011-09-27 15:57:16 +0200452 */
453int pci_max_pasids(struct pci_dev *pdev)
454{
455 u16 supported;
Kuppuswamy Sathyanarayanan751035b2019-09-05 14:31:46 -0500456 int pasid = pdev->pasid_cap;
Joerg Roedel086ac112011-09-27 15:57:16 +0200457
Kuppuswamy Sathyanarayanan2b0ae7c2019-09-05 14:31:43 -0500458 if (pdev->is_virtfn)
459 pdev = pci_physfn(pdev);
460
Kuppuswamy Sathyanarayanan751035b2019-09-05 14:31:46 -0500461 if (!pasid)
Joerg Roedel086ac112011-09-27 15:57:16 +0200462 return -EINVAL;
463
Kuppuswamy Sathyanarayanan751035b2019-09-05 14:31:46 -0500464 pci_read_config_word(pdev, pasid + PCI_PASID_CAP, &supported);
Joerg Roedel086ac112011-09-27 15:57:16 +0200465
466 supported = (supported & PASID_NUMBER_MASK) >> PASID_NUMBER_SHIFT;
467
468 return (1 << supported);
469}
Joerg Roedel086ac112011-09-27 15:57:16 +0200470#endif /* CONFIG_PCI_PASID */