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
->use_iommu_v2
)
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
;
66 if (!kfd
->use_iommu_v2
)
70 err
= amd_iommu_device_info(kfd
->pdev
, &iommu_info
);
73 "error getting iommu info. is the iommu enabled?\n");
77 if ((iommu_info
.flags
& required_iommu_flags
) != required_iommu_flags
) {
79 "error required iommu flags ats %i, pri %i, pasid %i\n",
80 (iommu_info
.flags
& AMD_IOMMU_DEVICE_FLAG_ATS_SUP
) != 0,
81 (iommu_info
.flags
& AMD_IOMMU_DEVICE_FLAG_PRI_SUP
) != 0,
82 (iommu_info
.flags
& AMD_IOMMU_DEVICE_FLAG_PASID_SUP
)
87 pasid_limit
= min_t(unsigned int,
88 (unsigned int)(1 << kfd
->device_info
->max_pasid_bits
),
89 iommu_info
.max_pasids
);
91 if (!kfd_set_pasid_limit(pasid_limit
)) {
92 dev_err(kfd_device
, "error setting pasid limit\n");
99 /** kfd_iommu_bind_process_to_device - Have the IOMMU bind a process
101 * Binds the given process to the given device using its PASID. This
102 * enables IOMMUv2 address translation for the process on the device.
104 * This function assumes that the process mutex is held.
106 int kfd_iommu_bind_process_to_device(struct kfd_process_device
*pdd
)
108 struct kfd_dev
*dev
= pdd
->dev
;
109 struct kfd_process
*p
= pdd
->process
;
112 if (!dev
->use_iommu_v2
|| pdd
->bound
== PDD_BOUND
)
115 if (unlikely(pdd
->bound
== PDD_BOUND_SUSPENDED
)) {
116 pr_err("Binding PDD_BOUND_SUSPENDED pdd is unexpected!\n");
120 err
= amd_iommu_bind_pasid(dev
->pdev
, p
->pasid
, p
->lead_thread
);
122 pdd
->bound
= PDD_BOUND
;
127 /** kfd_iommu_unbind_process - Unbind process from all devices
129 * This removes all IOMMU device bindings of the process. To be used
130 * before process termination.
132 void kfd_iommu_unbind_process(struct kfd_process
*p
)
134 struct kfd_process_device
*pdd
;
136 list_for_each_entry(pdd
, &p
->per_device_data
, per_device_list
)
137 if (pdd
->bound
== PDD_BOUND
)
138 amd_iommu_unbind_pasid(pdd
->dev
->pdev
, p
->pasid
);
141 /* Callback for process shutdown invoked by the IOMMU driver */
142 static void iommu_pasid_shutdown_callback(struct pci_dev
*pdev
, u32 pasid
)
144 struct kfd_dev
*dev
= kfd_device_by_pci_dev(pdev
);
145 struct kfd_process
*p
;
146 struct kfd_process_device
*pdd
;
152 * Look for the process that matches the pasid. If there is no such
153 * process, we either released it in amdkfd's own notifier, or there
154 * is a bug. Unfortunately, there is no way to tell...
156 p
= kfd_lookup_process_by_pasid(pasid
);
160 pr_debug("Unbinding process 0x%x from IOMMU\n", pasid
);
162 mutex_lock(kfd_get_dbgmgr_mutex());
164 if (dev
->dbgmgr
&& dev
->dbgmgr
->pasid
== p
->pasid
) {
165 if (!kfd_dbgmgr_unregister(dev
->dbgmgr
, p
)) {
166 kfd_dbgmgr_destroy(dev
->dbgmgr
);
171 mutex_unlock(kfd_get_dbgmgr_mutex());
173 mutex_lock(&p
->mutex
);
175 pdd
= kfd_get_process_device_data(dev
, p
);
177 /* For GPU relying on IOMMU, we need to dequeue here
178 * when PASID is still bound.
180 kfd_process_dequeue_from_device(pdd
);
182 mutex_unlock(&p
->mutex
);
184 kfd_unref_process(p
);
187 /* This function called by IOMMU driver on PPR failure */
188 static int iommu_invalid_ppr_cb(struct pci_dev
*pdev
, u32 pasid
,
189 unsigned long address
, u16 flags
)
193 dev_warn_ratelimited(kfd_device
,
194 "Invalid PPR device %x:%x.%x pasid 0x%x address 0x%lX flags 0x%X",
196 PCI_SLOT(pdev
->devfn
),
197 PCI_FUNC(pdev
->devfn
),
202 dev
= kfd_device_by_pci_dev(pdev
);
204 kfd_signal_iommu_event(dev
, pasid
, address
,
205 flags
& PPR_FAULT_WRITE
, flags
& PPR_FAULT_EXEC
);
207 return AMD_IOMMU_INV_PRI_RSP_INVALID
;
211 * Bind processes do the device that have been temporarily unbound
212 * (PDD_BOUND_SUSPENDED) in kfd_unbind_processes_from_device.
214 static int kfd_bind_processes_to_device(struct kfd_dev
*kfd
)
216 struct kfd_process_device
*pdd
;
217 struct kfd_process
*p
;
221 int idx
= srcu_read_lock(&kfd_processes_srcu
);
223 hash_for_each_rcu(kfd_processes_table
, temp
, p
, kfd_processes
) {
224 mutex_lock(&p
->mutex
);
225 pdd
= kfd_get_process_device_data(kfd
, p
);
227 if (WARN_ON(!pdd
) || pdd
->bound
!= PDD_BOUND_SUSPENDED
) {
228 mutex_unlock(&p
->mutex
);
232 err
= amd_iommu_bind_pasid(kfd
->pdev
, p
->pasid
,
235 pr_err("Unexpected pasid 0x%x binding failure\n",
237 mutex_unlock(&p
->mutex
);
241 pdd
->bound
= PDD_BOUND
;
242 mutex_unlock(&p
->mutex
);
245 srcu_read_unlock(&kfd_processes_srcu
, idx
);
251 * Mark currently bound processes as PDD_BOUND_SUSPENDED. These
252 * processes will be restored to PDD_BOUND state in
253 * kfd_bind_processes_to_device.
255 static void kfd_unbind_processes_from_device(struct kfd_dev
*kfd
)
257 struct kfd_process_device
*pdd
;
258 struct kfd_process
*p
;
261 int idx
= srcu_read_lock(&kfd_processes_srcu
);
263 hash_for_each_rcu(kfd_processes_table
, temp
, p
, kfd_processes
) {
264 mutex_lock(&p
->mutex
);
265 pdd
= kfd_get_process_device_data(kfd
, p
);
268 mutex_unlock(&p
->mutex
);
272 if (pdd
->bound
== PDD_BOUND
)
273 pdd
->bound
= PDD_BOUND_SUSPENDED
;
274 mutex_unlock(&p
->mutex
);
277 srcu_read_unlock(&kfd_processes_srcu
, idx
);
280 /** kfd_iommu_suspend - Prepare IOMMU for suspend
282 * This unbinds processes from the device and disables the IOMMU for
285 void kfd_iommu_suspend(struct kfd_dev
*kfd
)
287 if (!kfd
->use_iommu_v2
)
290 kfd_unbind_processes_from_device(kfd
);
292 amd_iommu_set_invalidate_ctx_cb(kfd
->pdev
, NULL
);
293 amd_iommu_set_invalid_ppr_cb(kfd
->pdev
, NULL
);
294 amd_iommu_free_device(kfd
->pdev
);
297 /** kfd_iommu_resume - Restore IOMMU after resume
299 * This reinitializes the IOMMU for the device and re-binds previously
300 * suspended processes to the device.
302 int kfd_iommu_resume(struct kfd_dev
*kfd
)
304 unsigned int pasid_limit
;
307 if (!kfd
->use_iommu_v2
)
310 pasid_limit
= kfd_get_pasid_limit();
312 err
= amd_iommu_init_device(kfd
->pdev
, pasid_limit
);
316 amd_iommu_set_invalidate_ctx_cb(kfd
->pdev
,
317 iommu_pasid_shutdown_callback
);
318 amd_iommu_set_invalid_ppr_cb(kfd
->pdev
,
319 iommu_invalid_ppr_cb
);
321 err
= kfd_bind_processes_to_device(kfd
);
323 amd_iommu_set_invalidate_ctx_cb(kfd
->pdev
, NULL
);
324 amd_iommu_set_invalid_ppr_cb(kfd
->pdev
, NULL
);
325 amd_iommu_free_device(kfd
->pdev
);
332 extern bool amd_iommu_pc_supported(void);
333 extern u8
amd_iommu_pc_get_max_banks(u16 devid
);
334 extern u8
amd_iommu_pc_get_max_counters(u16 devid
);
336 /** kfd_iommu_add_perf_counters - Add IOMMU performance counters to topology
338 int kfd_iommu_add_perf_counters(struct kfd_topology_device
*kdev
)
340 struct kfd_perf_properties
*props
;
342 if (!(kdev
->node_props
.capability
& HSA_CAP_ATS_PRESENT
))
345 if (!amd_iommu_pc_supported())
348 props
= kfd_alloc_struct(props
);
351 strcpy(props
->block_name
, "iommu");
352 props
->max_concurrent
= amd_iommu_pc_get_max_banks(0) *
353 amd_iommu_pc_get_max_counters(0); /* assume one iommu */
354 list_add_tail(&props
->list
, &kdev
->perf_props
);