2 * Copyright 2018 Advanced Micro Devices, Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
23 #include <linux/printk.h>
24 #include <linux/device.h>
25 #include <linux/slab.h>
26 #include <linux/pci.h>
27 #include <linux/amd-iommu.h>
29 #include "kfd_dbgmgr.h"
30 #include "kfd_topology.h"
31 #include "kfd_iommu.h"
33 static const u32 required_iommu_flags = AMD_IOMMU_DEVICE_FLAG_ATS_SUP |
34 AMD_IOMMU_DEVICE_FLAG_PRI_SUP |
35 AMD_IOMMU_DEVICE_FLAG_PASID_SUP;
37 /** kfd_iommu_check_device - Check whether IOMMU is available for device
39 int kfd_iommu_check_device(struct kfd_dev *kfd)
41 struct amd_iommu_device_info iommu_info;
44 if (!kfd->device_info->needs_iommu_device)
48 err = amd_iommu_device_info(kfd->pdev, &iommu_info);
52 if ((iommu_info.flags & required_iommu_flags) != required_iommu_flags)
58 /** kfd_iommu_device_init - Initialize IOMMU for device
60 int kfd_iommu_device_init(struct kfd_dev *kfd)
62 struct amd_iommu_device_info iommu_info;
63 unsigned int pasid_limit;
65 struct kfd_topology_device *top_dev;
67 top_dev = kfd_topology_device_by_id(kfd->id);
69 if (!kfd->device_info->needs_iommu_device)
73 err = amd_iommu_device_info(kfd->pdev, &iommu_info);
76 "error getting iommu info. is the iommu enabled?\n");
80 if ((iommu_info.flags & required_iommu_flags) != required_iommu_flags) {
82 "error required iommu flags ats %i, pri %i, pasid %i\n",
83 (iommu_info.flags & AMD_IOMMU_DEVICE_FLAG_ATS_SUP) != 0,
84 (iommu_info.flags & AMD_IOMMU_DEVICE_FLAG_PRI_SUP) != 0,
85 (iommu_info.flags & AMD_IOMMU_DEVICE_FLAG_PASID_SUP)
90 pasid_limit = min_t(unsigned int,
91 (unsigned int)(1 << kfd->device_info->max_pasid_bits),
92 iommu_info.max_pasids);
94 if (!kfd_set_pasid_limit(pasid_limit)) {
95 dev_err(kfd_device, "error setting pasid limit\n");
102 /** kfd_iommu_bind_process_to_device - Have the IOMMU bind a process
104 * Binds the given process to the given device using its PASID. This
105 * enables IOMMUv2 address translation for the process on the device.
107 * This function assumes that the process mutex is held.
109 int kfd_iommu_bind_process_to_device(struct kfd_process_device *pdd)
111 struct kfd_dev *dev = pdd->dev;
112 struct kfd_process *p = pdd->process;
115 if (!dev->device_info->needs_iommu_device || pdd->bound == PDD_BOUND)
118 if (unlikely(pdd->bound == PDD_BOUND_SUSPENDED)) {
119 pr_err("Binding PDD_BOUND_SUSPENDED pdd is unexpected!\n");
123 err = amd_iommu_bind_pasid(dev->pdev, p->pasid, p->lead_thread);
125 pdd->bound = PDD_BOUND;
130 /** kfd_iommu_unbind_process - Unbind process from all devices
132 * This removes all IOMMU device bindings of the process. To be used
133 * before process termination.
135 void kfd_iommu_unbind_process(struct kfd_process *p)
137 struct kfd_process_device *pdd;
139 list_for_each_entry(pdd, &p->per_device_data, per_device_list)
140 if (pdd->bound == PDD_BOUND)
141 amd_iommu_unbind_pasid(pdd->dev->pdev, p->pasid);
144 /* Callback for process shutdown invoked by the IOMMU driver */
145 static void iommu_pasid_shutdown_callback(struct pci_dev *pdev, int pasid)
147 struct kfd_dev *dev = kfd_device_by_pci_dev(pdev);
148 struct kfd_process *p;
149 struct kfd_process_device *pdd;
155 * Look for the process that matches the pasid. If there is no such
156 * process, we either released it in amdkfd's own notifier, or there
157 * is a bug. Unfortunately, there is no way to tell...
159 p = kfd_lookup_process_by_pasid(pasid);
163 pr_debug("Unbinding process %d from IOMMU\n", pasid);
165 mutex_lock(kfd_get_dbgmgr_mutex());
167 if (dev->dbgmgr && dev->dbgmgr->pasid == p->pasid) {
168 if (!kfd_dbgmgr_unregister(dev->dbgmgr, p)) {
169 kfd_dbgmgr_destroy(dev->dbgmgr);
174 mutex_unlock(kfd_get_dbgmgr_mutex());
176 mutex_lock(&p->mutex);
178 pdd = kfd_get_process_device_data(dev, p);
180 /* For GPU relying on IOMMU, we need to dequeue here
181 * when PASID is still bound.
183 kfd_process_dequeue_from_device(pdd);
185 mutex_unlock(&p->mutex);
187 kfd_unref_process(p);
190 /* This function called by IOMMU driver on PPR failure */
191 static int iommu_invalid_ppr_cb(struct pci_dev *pdev, int pasid,
192 unsigned long address, u16 flags)
196 dev_warn_ratelimited(kfd_device,
197 "Invalid PPR device %x:%x.%x pasid %d address 0x%lX flags 0x%X",
198 PCI_BUS_NUM(pdev->devfn),
199 PCI_SLOT(pdev->devfn),
200 PCI_FUNC(pdev->devfn),
205 dev = kfd_device_by_pci_dev(pdev);
207 kfd_signal_iommu_event(dev, pasid, address,
208 flags & PPR_FAULT_WRITE, flags & PPR_FAULT_EXEC);
210 return AMD_IOMMU_INV_PRI_RSP_INVALID;
214 * Bind processes do the device that have been temporarily unbound
215 * (PDD_BOUND_SUSPENDED) in kfd_unbind_processes_from_device.
217 static int kfd_bind_processes_to_device(struct kfd_dev *kfd)
219 struct kfd_process_device *pdd;
220 struct kfd_process *p;
224 int idx = srcu_read_lock(&kfd_processes_srcu);
226 hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) {
227 mutex_lock(&p->mutex);
228 pdd = kfd_get_process_device_data(kfd, p);
230 if (WARN_ON(!pdd) || pdd->bound != PDD_BOUND_SUSPENDED) {
231 mutex_unlock(&p->mutex);
235 err = amd_iommu_bind_pasid(kfd->pdev, p->pasid,
238 pr_err("Unexpected pasid %d binding failure\n",
240 mutex_unlock(&p->mutex);
244 pdd->bound = PDD_BOUND;
245 mutex_unlock(&p->mutex);
248 srcu_read_unlock(&kfd_processes_srcu, idx);
254 * Mark currently bound processes as PDD_BOUND_SUSPENDED. These
255 * processes will be restored to PDD_BOUND state in
256 * kfd_bind_processes_to_device.
258 static void kfd_unbind_processes_from_device(struct kfd_dev *kfd)
260 struct kfd_process_device *pdd;
261 struct kfd_process *p;
264 int idx = srcu_read_lock(&kfd_processes_srcu);
266 hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) {
267 mutex_lock(&p->mutex);
268 pdd = kfd_get_process_device_data(kfd, p);
271 mutex_unlock(&p->mutex);
275 if (pdd->bound == PDD_BOUND)
276 pdd->bound = PDD_BOUND_SUSPENDED;
277 mutex_unlock(&p->mutex);
280 srcu_read_unlock(&kfd_processes_srcu, idx);
283 /** kfd_iommu_suspend - Prepare IOMMU for suspend
285 * This unbinds processes from the device and disables the IOMMU for
288 void kfd_iommu_suspend(struct kfd_dev *kfd)
290 if (!kfd->device_info->needs_iommu_device)
293 kfd_unbind_processes_from_device(kfd);
295 amd_iommu_set_invalidate_ctx_cb(kfd->pdev, NULL);
296 amd_iommu_set_invalid_ppr_cb(kfd->pdev, NULL);
297 amd_iommu_free_device(kfd->pdev);
300 /** kfd_iommu_resume - Restore IOMMU after resume
302 * This reinitializes the IOMMU for the device and re-binds previously
303 * suspended processes to the device.
305 int kfd_iommu_resume(struct kfd_dev *kfd)
307 unsigned int pasid_limit;
310 if (!kfd->device_info->needs_iommu_device)
313 pasid_limit = kfd_get_pasid_limit();
315 err = amd_iommu_init_device(kfd->pdev, pasid_limit);
319 amd_iommu_set_invalidate_ctx_cb(kfd->pdev,
320 iommu_pasid_shutdown_callback);
321 amd_iommu_set_invalid_ppr_cb(kfd->pdev,
322 iommu_invalid_ppr_cb);
324 err = kfd_bind_processes_to_device(kfd);
326 amd_iommu_set_invalidate_ctx_cb(kfd->pdev, NULL);
327 amd_iommu_set_invalid_ppr_cb(kfd->pdev, NULL);
328 amd_iommu_free_device(kfd->pdev);
335 extern bool amd_iommu_pc_supported(void);
336 extern u8 amd_iommu_pc_get_max_banks(u16 devid);
337 extern u8 amd_iommu_pc_get_max_counters(u16 devid);
339 /** kfd_iommu_add_perf_counters - Add IOMMU performance counters to topology
341 int kfd_iommu_add_perf_counters(struct kfd_topology_device *kdev)
343 struct kfd_perf_properties *props;
345 if (!(kdev->node_props.capability & HSA_CAP_ATS_PRESENT))
348 if (!amd_iommu_pc_supported())
351 props = kfd_alloc_struct(props);
354 strcpy(props->block_name, "iommu");
355 props->max_concurrent = amd_iommu_pc_get_max_banks(0) *
356 amd_iommu_pc_get_max_counters(0); /* assume one iommu */
357 list_add_tail(&props->list, &kdev->perf_props);