blob: c967ad6e262678790583bb3137ff273ec31a16c3 [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/**
Jean-Philippe Brucker52137672020-05-20 17:22:00 +020034 * pci_ats_supported - check if the device can use ATS
35 * @dev: the PCI device
36 *
37 * Returns true if the device supports ATS and is allowed to use it, false
38 * otherwise.
39 */
40bool pci_ats_supported(struct pci_dev *dev)
41{
42 if (!dev->ats_cap)
43 return false;
44
45 return (dev->untrusted == 0);
46}
47EXPORT_SYMBOL_GPL(pci_ats_supported);
48
49/**
Joerg Roedeldb3c33c2011-09-27 15:57:13 +020050 * pci_enable_ats - enable the ATS capability
51 * @dev: the PCI device
52 * @ps: the IOMMU page shift
53 *
54 * Returns 0 on success, or negative on failure.
55 */
56int pci_enable_ats(struct pci_dev *dev, int ps)
57{
Joerg Roedeldb3c33c2011-09-27 15:57:13 +020058 u16 ctrl;
Bjorn Helgaasc39127d2015-07-17 15:38:13 -050059 struct pci_dev *pdev;
Joerg Roedeldb3c33c2011-09-27 15:57:13 +020060
Jean-Philippe Brucker52137672020-05-20 17:22:00 +020061 if (!pci_ats_supported(dev))
Bjorn Helgaasedc90fe2015-07-17 15:05:46 -050062 return -EINVAL;
63
Bjorn Helgaasf7ef1342015-07-20 09:23:37 -050064 if (WARN_ON(dev->ats_enabled))
Bjorn Helgaasa021f302015-07-17 15:43:27 -050065 return -EBUSY;
66
Joerg Roedeldb3c33c2011-09-27 15:57:13 +020067 if (ps < PCI_ATS_MIN_STU)
68 return -EINVAL;
69
Bjorn Helgaasedc90fe2015-07-17 15:05:46 -050070 /*
71 * Note that enabling ATS on a VF fails unless it's already enabled
72 * with the same STU on the PF.
73 */
Joerg Roedeldb3c33c2011-09-27 15:57:13 +020074 ctrl = PCI_ATS_CTRL_ENABLE;
Bjorn Helgaasedc90fe2015-07-17 15:05:46 -050075 if (dev->is_virtfn) {
Bjorn Helgaasc39127d2015-07-17 15:38:13 -050076 pdev = pci_physfn(dev);
Bjorn Helgaasd544d752015-07-17 15:15:19 -050077 if (pdev->ats_stu != ps)
Bjorn Helgaasedc90fe2015-07-17 15:05:46 -050078 return -EINVAL;
Bjorn Helgaasedc90fe2015-07-17 15:05:46 -050079 } else {
Bjorn Helgaasd544d752015-07-17 15:15:19 -050080 dev->ats_stu = ps;
81 ctrl |= PCI_ATS_CTRL_STU(dev->ats_stu - PCI_ATS_MIN_STU);
Bjorn Helgaasedc90fe2015-07-17 15:05:46 -050082 }
Bjorn Helgaasd544d752015-07-17 15:15:19 -050083 pci_write_config_word(dev, dev->ats_cap + PCI_ATS_CTRL, ctrl);
Joerg Roedeldb3c33c2011-09-27 15:57:13 +020084
Bjorn Helgaasd544d752015-07-17 15:15:19 -050085 dev->ats_enabled = 1;
Joerg Roedeldb3c33c2011-09-27 15:57:13 +020086 return 0;
87}
Greg Kroah-Hartmanbb950bc2019-12-19 12:03:39 +000088EXPORT_SYMBOL_GPL(pci_enable_ats);
Joerg Roedeldb3c33c2011-09-27 15:57:13 +020089
90/**
91 * pci_disable_ats - disable the ATS capability
92 * @dev: the PCI device
93 */
94void pci_disable_ats(struct pci_dev *dev)
95{
96 u16 ctrl;
97
Bjorn Helgaasf7ef1342015-07-20 09:23:37 -050098 if (WARN_ON(!dev->ats_enabled))
Bjorn Helgaasa021f302015-07-17 15:43:27 -050099 return;
Joerg Roedeldb3c33c2011-09-27 15:57:13 +0200100
Bjorn Helgaasd544d752015-07-17 15:15:19 -0500101 pci_read_config_word(dev, dev->ats_cap + PCI_ATS_CTRL, &ctrl);
Joerg Roedeldb3c33c2011-09-27 15:57:13 +0200102 ctrl &= ~PCI_ATS_CTRL_ENABLE;
Bjorn Helgaasd544d752015-07-17 15:15:19 -0500103 pci_write_config_word(dev, dev->ats_cap + PCI_ATS_CTRL, ctrl);
Joerg Roedeldb3c33c2011-09-27 15:57:13 +0200104
Bjorn Helgaasd544d752015-07-17 15:15:19 -0500105 dev->ats_enabled = 0;
Joerg Roedeldb3c33c2011-09-27 15:57:13 +0200106}
Greg Kroah-Hartmanbb950bc2019-12-19 12:03:39 +0000107EXPORT_SYMBOL_GPL(pci_disable_ats);
Joerg Roedeldb3c33c2011-09-27 15:57:13 +0200108
Hao, Xudong1900ca12011-12-17 21:24:40 +0800109void pci_restore_ats_state(struct pci_dev *dev)
110{
111 u16 ctrl;
112
Bjorn Helgaasf7ef1342015-07-20 09:23:37 -0500113 if (!dev->ats_enabled)
Hao, Xudong1900ca12011-12-17 21:24:40 +0800114 return;
Hao, Xudong1900ca12011-12-17 21:24:40 +0800115
116 ctrl = PCI_ATS_CTRL_ENABLE;
117 if (!dev->is_virtfn)
Bjorn Helgaasd544d752015-07-17 15:15:19 -0500118 ctrl |= PCI_ATS_CTRL_STU(dev->ats_stu - PCI_ATS_MIN_STU);
119 pci_write_config_word(dev, dev->ats_cap + PCI_ATS_CTRL, ctrl);
Hao, Xudong1900ca12011-12-17 21:24:40 +0800120}
Hao, Xudong1900ca12011-12-17 21:24:40 +0800121
Joerg Roedeldb3c33c2011-09-27 15:57:13 +0200122/**
123 * pci_ats_queue_depth - query the ATS Invalidate Queue Depth
124 * @dev: the PCI device
125 *
126 * Returns the queue depth on success, or negative on failure.
127 *
128 * The ATS spec uses 0 in the Invalidate Queue Depth field to
129 * indicate that the function can accept 32 Invalidate Request.
130 * But here we use the `real' values (i.e. 1~32) for the Queue
131 * Depth; and 0 indicates the function shares the Queue with
132 * other functions (doesn't exclusively own a Queue).
133 */
134int pci_ats_queue_depth(struct pci_dev *dev)
135{
Bjorn Helgaasa71f9382015-07-20 09:24:32 -0500136 u16 cap;
137
Bjorn Helgaas3c765392015-07-17 15:30:26 -0500138 if (!dev->ats_cap)
139 return -EINVAL;
140
Joerg Roedeldb3c33c2011-09-27 15:57:13 +0200141 if (dev->is_virtfn)
142 return 0;
143
Bjorn Helgaasa71f9382015-07-20 09:24:32 -0500144 pci_read_config_word(dev, dev->ats_cap + PCI_ATS_CAP, &cap);
145 return PCI_ATS_CAP_QDEP(cap) ? PCI_ATS_CAP_QDEP(cap) : PCI_ATS_MAX_QDEP;
Joerg Roedeldb3c33c2011-09-27 15:57:13 +0200146}
Joerg Roedelc320b972011-09-27 15:57:15 +0200147
Kuppuswamy Sathyanarayanan8c938dd2019-02-19 11:06:09 -0800148/**
149 * pci_ats_page_aligned - Return Page Aligned Request bit status.
150 * @pdev: the PCI device
151 *
152 * Returns 1, if the Untranslated Addresses generated by the device
153 * are always aligned or 0 otherwise.
154 *
155 * Per PCIe spec r4.0, sec 10.5.1.2, if the Page Aligned Request bit
156 * is set, it indicates the Untranslated Addresses generated by the
157 * device are always aligned to a 4096 byte boundary.
158 */
159int pci_ats_page_aligned(struct pci_dev *pdev)
160{
161 u16 cap;
162
163 if (!pdev->ats_cap)
164 return 0;
165
166 pci_read_config_word(pdev, pdev->ats_cap + PCI_ATS_CAP, &cap);
167
168 if (cap & PCI_ATS_CAP_PAGE_ALIGNED)
169 return 1;
170
171 return 0;
172}
Kuppuswamy Sathyanarayanan8c938dd2019-02-19 11:06:09 -0800173
Joerg Roedelc320b972011-09-27 15:57:15 +0200174#ifdef CONFIG_PCI_PRI
Kuppuswamy Sathyanarayananc0651902019-09-05 14:31:45 -0500175void pci_pri_init(struct pci_dev *pdev)
176{
Bjorn Helgaase5adf792019-10-09 16:07:51 -0500177 u16 status;
178
Kuppuswamy Sathyanarayananc0651902019-09-05 14:31:45 -0500179 pdev->pri_cap = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
Bjorn Helgaase5adf792019-10-09 16:07:51 -0500180
181 if (!pdev->pri_cap)
182 return;
183
184 pci_read_config_word(pdev, pdev->pri_cap + PCI_PRI_STATUS, &status);
185 if (status & PCI_PRI_STATUS_PASID)
186 pdev->pasid_required = 1;
Kuppuswamy Sathyanarayananc0651902019-09-05 14:31:45 -0500187}
188
Joerg Roedelc320b972011-09-27 15:57:15 +0200189/**
190 * pci_enable_pri - Enable PRI capability
Krzysztof Kozlowski9b41d192020-07-29 22:12:19 +0200191 * @pdev: PCI device structure
192 * @reqs: outstanding requests
Joerg Roedelc320b972011-09-27 15:57:15 +0200193 *
194 * Returns 0 on success, negative value on error
195 */
196int pci_enable_pri(struct pci_dev *pdev, u32 reqs)
197{
198 u16 control, status;
199 u32 max_requests;
Kuppuswamy Sathyanarayananc0651902019-09-05 14:31:45 -0500200 int pri = pdev->pri_cap;
Joerg Roedelc320b972011-09-27 15:57:15 +0200201
Kuppuswamy Sathyanarayanan9bf49e32019-09-05 14:31:42 -0500202 /*
203 * VFs must not implement the PRI Capability. If their PF
204 * implements PRI, it is shared by the VFs, so if the PF PRI is
205 * enabled, it is also enabled for the VF.
206 */
207 if (pdev->is_virtfn) {
208 if (pci_physfn(pdev)->pri_enabled)
209 return 0;
210 return -EINVAL;
211 }
212
Jean-Philippe Bruckera4f4fa62017-05-30 09:25:48 -0700213 if (WARN_ON(pdev->pri_enabled))
214 return -EBUSY;
215
Kuppuswamy Sathyanarayananc0651902019-09-05 14:31:45 -0500216 if (!pri)
Joerg Roedelc320b972011-09-27 15:57:15 +0200217 return -EINVAL;
218
Kuppuswamy Sathyanarayananc0651902019-09-05 14:31:45 -0500219 pci_read_config_word(pdev, pri + PCI_PRI_STATUS, &status);
CQ Tang4ebeb1e2017-05-30 09:25:49 -0700220 if (!(status & PCI_PRI_STATUS_STOPPED))
Joerg Roedelc320b972011-09-27 15:57:15 +0200221 return -EBUSY;
222
Kuppuswamy Sathyanarayananc0651902019-09-05 14:31:45 -0500223 pci_read_config_dword(pdev, pri + PCI_PRI_MAX_REQ, &max_requests);
Joerg Roedelc320b972011-09-27 15:57:15 +0200224 reqs = min(max_requests, reqs);
CQ Tang4ebeb1e2017-05-30 09:25:49 -0700225 pdev->pri_reqs_alloc = reqs;
Kuppuswamy Sathyanarayananc0651902019-09-05 14:31:45 -0500226 pci_write_config_dword(pdev, pri + PCI_PRI_ALLOC_REQ, reqs);
Joerg Roedelc320b972011-09-27 15:57:15 +0200227
CQ Tang4ebeb1e2017-05-30 09:25:49 -0700228 control = PCI_PRI_CTRL_ENABLE;
Kuppuswamy Sathyanarayananc0651902019-09-05 14:31:45 -0500229 pci_write_config_word(pdev, pri + PCI_PRI_CTRL, control);
Joerg Roedelc320b972011-09-27 15:57:15 +0200230
Jean-Philippe Bruckera4f4fa62017-05-30 09:25:48 -0700231 pdev->pri_enabled = 1;
232
Joerg Roedelc320b972011-09-27 15:57:15 +0200233 return 0;
234}
Joerg Roedelc320b972011-09-27 15:57:15 +0200235
236/**
237 * pci_disable_pri - Disable PRI capability
238 * @pdev: PCI device structure
239 *
240 * Only clears the enabled-bit, regardless of its former value
241 */
242void pci_disable_pri(struct pci_dev *pdev)
243{
244 u16 control;
Kuppuswamy Sathyanarayananc0651902019-09-05 14:31:45 -0500245 int pri = pdev->pri_cap;
Joerg Roedelc320b972011-09-27 15:57:15 +0200246
Kuppuswamy Sathyanarayanan9bf49e32019-09-05 14:31:42 -0500247 /* VFs share the PF PRI */
248 if (pdev->is_virtfn)
249 return;
250
Jean-Philippe Bruckera4f4fa62017-05-30 09:25:48 -0700251 if (WARN_ON(!pdev->pri_enabled))
252 return;
253
Kuppuswamy Sathyanarayananc0651902019-09-05 14:31:45 -0500254 if (!pri)
Joerg Roedelc320b972011-09-27 15:57:15 +0200255 return;
256
Kuppuswamy Sathyanarayananc0651902019-09-05 14:31:45 -0500257 pci_read_config_word(pdev, pri + PCI_PRI_CTRL, &control);
Alex Williamson91f57d52011-11-11 10:07:36 -0700258 control &= ~PCI_PRI_CTRL_ENABLE;
Kuppuswamy Sathyanarayananc0651902019-09-05 14:31:45 -0500259 pci_write_config_word(pdev, pri + PCI_PRI_CTRL, control);
Jean-Philippe Bruckera4f4fa62017-05-30 09:25:48 -0700260
261 pdev->pri_enabled = 0;
Joerg Roedelc320b972011-09-27 15:57:15 +0200262}
263EXPORT_SYMBOL_GPL(pci_disable_pri);
264
265/**
CQ Tang4ebeb1e2017-05-30 09:25:49 -0700266 * pci_restore_pri_state - Restore PRI
267 * @pdev: PCI device structure
268 */
269void pci_restore_pri_state(struct pci_dev *pdev)
270{
271 u16 control = PCI_PRI_CTRL_ENABLE;
272 u32 reqs = pdev->pri_reqs_alloc;
Kuppuswamy Sathyanarayananc0651902019-09-05 14:31:45 -0500273 int pri = pdev->pri_cap;
CQ Tang4ebeb1e2017-05-30 09:25:49 -0700274
Kuppuswamy Sathyanarayanan9bf49e32019-09-05 14:31:42 -0500275 if (pdev->is_virtfn)
276 return;
277
CQ Tang4ebeb1e2017-05-30 09:25:49 -0700278 if (!pdev->pri_enabled)
279 return;
280
Kuppuswamy Sathyanarayananc0651902019-09-05 14:31:45 -0500281 if (!pri)
CQ Tang4ebeb1e2017-05-30 09:25:49 -0700282 return;
283
Kuppuswamy Sathyanarayananc0651902019-09-05 14:31:45 -0500284 pci_write_config_dword(pdev, pri + PCI_PRI_ALLOC_REQ, reqs);
285 pci_write_config_word(pdev, pri + PCI_PRI_CTRL, control);
CQ Tang4ebeb1e2017-05-30 09:25:49 -0700286}
CQ Tang4ebeb1e2017-05-30 09:25:49 -0700287
288/**
Joerg Roedelc320b972011-09-27 15:57:15 +0200289 * pci_reset_pri - Resets device's PRI state
290 * @pdev: PCI device structure
291 *
292 * The PRI capability must be disabled before this function is called.
293 * Returns 0 on success, negative value on error.
294 */
295int pci_reset_pri(struct pci_dev *pdev)
296{
297 u16 control;
Kuppuswamy Sathyanarayananc0651902019-09-05 14:31:45 -0500298 int pri = pdev->pri_cap;
Joerg Roedelc320b972011-09-27 15:57:15 +0200299
Kuppuswamy Sathyanarayanan9bf49e32019-09-05 14:31:42 -0500300 if (pdev->is_virtfn)
301 return 0;
302
Jean-Philippe Bruckera4f4fa62017-05-30 09:25:48 -0700303 if (WARN_ON(pdev->pri_enabled))
304 return -EBUSY;
305
Kuppuswamy Sathyanarayananc0651902019-09-05 14:31:45 -0500306 if (!pri)
Joerg Roedelc320b972011-09-27 15:57:15 +0200307 return -EINVAL;
308
CQ Tang4ebeb1e2017-05-30 09:25:49 -0700309 control = PCI_PRI_CTRL_RESET;
Kuppuswamy Sathyanarayananc0651902019-09-05 14:31:45 -0500310 pci_write_config_word(pdev, pri + PCI_PRI_CTRL, control);
Joerg Roedelc320b972011-09-27 15:57:15 +0200311
312 return 0;
313}
Bjorn Helgaas8cbb8a92019-10-09 14:54:01 -0500314
315/**
316 * pci_prg_resp_pasid_required - Return PRG Response PASID Required bit
317 * status.
318 * @pdev: PCI device structure
319 *
320 * Returns 1 if PASID is required in PRG Response Message, 0 otherwise.
321 */
322int pci_prg_resp_pasid_required(struct pci_dev *pdev)
323{
Kuppuswamy Sathyanarayanan9bf49e32019-09-05 14:31:42 -0500324 if (pdev->is_virtfn)
325 pdev = pci_physfn(pdev);
326
Bjorn Helgaase5adf792019-10-09 16:07:51 -0500327 return pdev->pasid_required;
Bjorn Helgaas8cbb8a92019-10-09 14:54:01 -0500328}
Ashok Raj3f9a7a12020-07-23 15:37:29 -0700329
330/**
331 * pci_pri_supported - Check if PRI is supported.
332 * @pdev: PCI device structure
333 *
334 * Returns true if PRI capability is present, false otherwise.
335 */
336bool pci_pri_supported(struct pci_dev *pdev)
337{
338 /* VFs share the PF PRI */
339 if (pci_physfn(pdev)->pri_cap)
340 return true;
341 return false;
342}
343EXPORT_SYMBOL_GPL(pci_pri_supported);
Joerg Roedelc320b972011-09-27 15:57:15 +0200344#endif /* CONFIG_PCI_PRI */
Joerg Roedel086ac112011-09-27 15:57:16 +0200345
346#ifdef CONFIG_PCI_PASID
Kuppuswamy Sathyanarayanan751035b2019-09-05 14:31:46 -0500347void pci_pasid_init(struct pci_dev *pdev)
348{
349 pdev->pasid_cap = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PASID);
350}
351
Joerg Roedel086ac112011-09-27 15:57:16 +0200352/**
353 * pci_enable_pasid - Enable the PASID capability
354 * @pdev: PCI device structure
355 * @features: Features to enable
356 *
357 * Returns 0 on success, negative value on error. This function checks
358 * whether the features are actually supported by the device and returns
359 * an error if not.
360 */
361int pci_enable_pasid(struct pci_dev *pdev, int features)
362{
363 u16 control, supported;
Kuppuswamy Sathyanarayanan751035b2019-09-05 14:31:46 -0500364 int pasid = pdev->pasid_cap;
Joerg Roedel086ac112011-09-27 15:57:16 +0200365
Kuppuswamy Sathyanarayanan2b0ae7c2019-09-05 14:31:43 -0500366 /*
367 * VFs must not implement the PASID Capability, but if a PF
368 * supports PASID, its VFs share the PF PASID configuration.
369 */
370 if (pdev->is_virtfn) {
371 if (pci_physfn(pdev)->pasid_enabled)
372 return 0;
373 return -EINVAL;
374 }
375
Jean-Philippe Bruckera4f4fa62017-05-30 09:25:48 -0700376 if (WARN_ON(pdev->pasid_enabled))
377 return -EBUSY;
378
Zhangfei Gao8c09e892021-07-13 10:54:34 +0800379 if (!pdev->eetlp_prefix_path && !pdev->pasid_no_tlp)
Sinan Kaya7ce3f912018-06-30 11:24:24 -0400380 return -EINVAL;
381
Kuppuswamy Sathyanarayanan751035b2019-09-05 14:31:46 -0500382 if (!pasid)
Joerg Roedel086ac112011-09-27 15:57:16 +0200383 return -EINVAL;
384
Kuppuswamy Sathyanarayanan751035b2019-09-05 14:31:46 -0500385 pci_read_config_word(pdev, pasid + PCI_PASID_CAP, &supported);
Alex Williamson91f57d52011-11-11 10:07:36 -0700386 supported &= PCI_PASID_CAP_EXEC | PCI_PASID_CAP_PRIV;
Joerg Roedel086ac112011-09-27 15:57:16 +0200387
388 /* User wants to enable anything unsupported? */
389 if ((supported & features) != features)
390 return -EINVAL;
391
Alex Williamson91f57d52011-11-11 10:07:36 -0700392 control = PCI_PASID_CTRL_ENABLE | features;
CQ Tang4ebeb1e2017-05-30 09:25:49 -0700393 pdev->pasid_features = features;
Joerg Roedel086ac112011-09-27 15:57:16 +0200394
Kuppuswamy Sathyanarayanan751035b2019-09-05 14:31:46 -0500395 pci_write_config_word(pdev, pasid + PCI_PASID_CTRL, control);
Joerg Roedel086ac112011-09-27 15:57:16 +0200396
Jean-Philippe Bruckera4f4fa62017-05-30 09:25:48 -0700397 pdev->pasid_enabled = 1;
398
Joerg Roedel086ac112011-09-27 15:57:16 +0200399 return 0;
400}
Jean-Philippe Brucker7682ce22020-02-24 17:58:41 +0100401EXPORT_SYMBOL_GPL(pci_enable_pasid);
Joerg Roedel086ac112011-09-27 15:57:16 +0200402
403/**
404 * pci_disable_pasid - Disable the PASID capability
405 * @pdev: PCI device structure
Joerg Roedel086ac112011-09-27 15:57:16 +0200406 */
407void pci_disable_pasid(struct pci_dev *pdev)
408{
409 u16 control = 0;
Kuppuswamy Sathyanarayanan751035b2019-09-05 14:31:46 -0500410 int pasid = pdev->pasid_cap;
Joerg Roedel086ac112011-09-27 15:57:16 +0200411
Kuppuswamy Sathyanarayanan2b0ae7c2019-09-05 14:31:43 -0500412 /* VFs share the PF PASID configuration */
413 if (pdev->is_virtfn)
414 return;
415
Jean-Philippe Bruckera4f4fa62017-05-30 09:25:48 -0700416 if (WARN_ON(!pdev->pasid_enabled))
417 return;
418
Kuppuswamy Sathyanarayanan751035b2019-09-05 14:31:46 -0500419 if (!pasid)
Joerg Roedel086ac112011-09-27 15:57:16 +0200420 return;
421
Kuppuswamy Sathyanarayanan751035b2019-09-05 14:31:46 -0500422 pci_write_config_word(pdev, pasid + PCI_PASID_CTRL, control);
Jean-Philippe Bruckera4f4fa62017-05-30 09:25:48 -0700423
424 pdev->pasid_enabled = 0;
Joerg Roedel086ac112011-09-27 15:57:16 +0200425}
Jean-Philippe Brucker7682ce22020-02-24 17:58:41 +0100426EXPORT_SYMBOL_GPL(pci_disable_pasid);
Joerg Roedel086ac112011-09-27 15:57:16 +0200427
428/**
CQ Tang4ebeb1e2017-05-30 09:25:49 -0700429 * pci_restore_pasid_state - Restore PASID capabilities
430 * @pdev: PCI device structure
431 */
432void pci_restore_pasid_state(struct pci_dev *pdev)
433{
434 u16 control;
Kuppuswamy Sathyanarayanan751035b2019-09-05 14:31:46 -0500435 int pasid = pdev->pasid_cap;
CQ Tang4ebeb1e2017-05-30 09:25:49 -0700436
Kuppuswamy Sathyanarayanan2b0ae7c2019-09-05 14:31:43 -0500437 if (pdev->is_virtfn)
438 return;
439
CQ Tang4ebeb1e2017-05-30 09:25:49 -0700440 if (!pdev->pasid_enabled)
441 return;
442
Kuppuswamy Sathyanarayanan751035b2019-09-05 14:31:46 -0500443 if (!pasid)
CQ Tang4ebeb1e2017-05-30 09:25:49 -0700444 return;
445
446 control = PCI_PASID_CTRL_ENABLE | pdev->pasid_features;
Kuppuswamy Sathyanarayanan751035b2019-09-05 14:31:46 -0500447 pci_write_config_word(pdev, pasid + PCI_PASID_CTRL, control);
CQ Tang4ebeb1e2017-05-30 09:25:49 -0700448}
CQ Tang4ebeb1e2017-05-30 09:25:49 -0700449
450/**
Joerg Roedel086ac112011-09-27 15:57:16 +0200451 * pci_pasid_features - Check which PASID features are supported
452 * @pdev: PCI device structure
453 *
454 * Returns a negative value when no PASI capability is present.
455 * Otherwise is returns a bitmask with supported features. Current
456 * features reported are:
Alex Williamson91f57d52011-11-11 10:07:36 -0700457 * PCI_PASID_CAP_EXEC - Execute permission supported
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700458 * PCI_PASID_CAP_PRIV - Privileged mode supported
Joerg Roedel086ac112011-09-27 15:57:16 +0200459 */
460int pci_pasid_features(struct pci_dev *pdev)
461{
462 u16 supported;
Kuppuswamy Sathyanarayanan2e346732020-01-29 11:14:00 -0800463 int pasid;
Joerg Roedel086ac112011-09-27 15:57:16 +0200464
Kuppuswamy Sathyanarayanan2b0ae7c2019-09-05 14:31:43 -0500465 if (pdev->is_virtfn)
466 pdev = pci_physfn(pdev);
467
Kuppuswamy Sathyanarayanan2e346732020-01-29 11:14:00 -0800468 pasid = pdev->pasid_cap;
Kuppuswamy Sathyanarayanan751035b2019-09-05 14:31:46 -0500469 if (!pasid)
Joerg Roedel086ac112011-09-27 15:57:16 +0200470 return -EINVAL;
471
Kuppuswamy Sathyanarayanan751035b2019-09-05 14:31:46 -0500472 pci_read_config_word(pdev, pasid + PCI_PASID_CAP, &supported);
Joerg Roedel086ac112011-09-27 15:57:16 +0200473
Alex Williamson91f57d52011-11-11 10:07:36 -0700474 supported &= PCI_PASID_CAP_EXEC | PCI_PASID_CAP_PRIV;
Joerg Roedel086ac112011-09-27 15:57:16 +0200475
476 return supported;
477}
Jean-Philippe Brucker7682ce22020-02-24 17:58:41 +0100478EXPORT_SYMBOL_GPL(pci_pasid_features);
Joerg Roedel086ac112011-09-27 15:57:16 +0200479
480#define PASID_NUMBER_SHIFT 8
481#define PASID_NUMBER_MASK (0x1f << PASID_NUMBER_SHIFT)
482/**
Krzysztof WilczyƄski43395d92021-03-11 00:17:17 +0000483 * pci_max_pasids - Get maximum number of PASIDs supported by device
Joerg Roedel086ac112011-09-27 15:57:16 +0200484 * @pdev: PCI device structure
485 *
486 * Returns negative value when PASID capability is not present.
Bjorn Helgaasf6b6aef2019-05-30 08:05:58 -0500487 * Otherwise it returns the number of supported PASIDs.
Joerg Roedel086ac112011-09-27 15:57:16 +0200488 */
489int pci_max_pasids(struct pci_dev *pdev)
490{
491 u16 supported;
Kuppuswamy Sathyanarayanan2e346732020-01-29 11:14:00 -0800492 int pasid;
Joerg Roedel086ac112011-09-27 15:57:16 +0200493
Kuppuswamy Sathyanarayanan2b0ae7c2019-09-05 14:31:43 -0500494 if (pdev->is_virtfn)
495 pdev = pci_physfn(pdev);
496
Kuppuswamy Sathyanarayanan2e346732020-01-29 11:14:00 -0800497 pasid = pdev->pasid_cap;
Kuppuswamy Sathyanarayanan751035b2019-09-05 14:31:46 -0500498 if (!pasid)
Joerg Roedel086ac112011-09-27 15:57:16 +0200499 return -EINVAL;
500
Kuppuswamy Sathyanarayanan751035b2019-09-05 14:31:46 -0500501 pci_read_config_word(pdev, pasid + PCI_PASID_CAP, &supported);
Joerg Roedel086ac112011-09-27 15:57:16 +0200502
503 supported = (supported & PASID_NUMBER_MASK) >> PASID_NUMBER_SHIFT;
504
505 return (1 << supported);
506}
Jean-Philippe Brucker7682ce22020-02-24 17:58:41 +0100507EXPORT_SYMBOL_GPL(pci_max_pasids);
Joerg Roedel086ac112011-09-27 15:57:16 +0200508#endif /* CONFIG_PCI_PASID */