GNU Linux-libre 4.19.245-gnu1
[releases.git] / drivers / gpu / drm / amd / amdkfd / kfd_iommu.c
1 /*
2  * Copyright 2018 Advanced Micro Devices, Inc.
3  *
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:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
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.
21  */
22
23 #include <linux/kconfig.h>
24
25 #if IS_REACHABLE(CONFIG_AMD_IOMMU_V2)
26
27 #include <linux/printk.h>
28 #include <linux/device.h>
29 #include <linux/slab.h>
30 #include <linux/pci.h>
31 #include <linux/amd-iommu.h>
32 #include "kfd_priv.h"
33 #include "kfd_dbgmgr.h"
34 #include "kfd_topology.h"
35 #include "kfd_iommu.h"
36
37 static const u32 required_iommu_flags = AMD_IOMMU_DEVICE_FLAG_ATS_SUP |
38                                         AMD_IOMMU_DEVICE_FLAG_PRI_SUP |
39                                         AMD_IOMMU_DEVICE_FLAG_PASID_SUP;
40
41 /** kfd_iommu_check_device - Check whether IOMMU is available for device
42  */
43 int kfd_iommu_check_device(struct kfd_dev *kfd)
44 {
45         struct amd_iommu_device_info iommu_info;
46         int err;
47
48         if (!kfd->device_info->needs_iommu_device)
49                 return -ENODEV;
50
51         iommu_info.flags = 0;
52         err = amd_iommu_device_info(kfd->pdev, &iommu_info);
53         if (err)
54                 return err;
55
56         if ((iommu_info.flags & required_iommu_flags) != required_iommu_flags)
57                 return -ENODEV;
58
59         return 0;
60 }
61
62 /** kfd_iommu_device_init - Initialize IOMMU for device
63  */
64 int kfd_iommu_device_init(struct kfd_dev *kfd)
65 {
66         struct amd_iommu_device_info iommu_info;
67         unsigned int pasid_limit;
68         int err;
69         struct kfd_topology_device *top_dev;
70
71         top_dev = kfd_topology_device_by_id(kfd->id);
72
73         /*
74          * Overwrite ATS capability according to needs_iommu_device to fix
75          * potential missing corresponding bit in CRAT of BIOS.
76          */
77         if (!kfd->device_info->needs_iommu_device) {
78                 top_dev->node_props.capability &= ~HSA_CAP_ATS_PRESENT;
79                 return 0;
80         }
81
82         top_dev->node_props.capability |= HSA_CAP_ATS_PRESENT;
83
84         iommu_info.flags = 0;
85         err = amd_iommu_device_info(kfd->pdev, &iommu_info);
86         if (err < 0) {
87                 dev_err(kfd_device,
88                         "error getting iommu info. is the iommu enabled?\n");
89                 return -ENODEV;
90         }
91
92         if ((iommu_info.flags & required_iommu_flags) != required_iommu_flags) {
93                 dev_err(kfd_device,
94                         "error required iommu flags ats %i, pri %i, pasid %i\n",
95                        (iommu_info.flags & AMD_IOMMU_DEVICE_FLAG_ATS_SUP) != 0,
96                        (iommu_info.flags & AMD_IOMMU_DEVICE_FLAG_PRI_SUP) != 0,
97                        (iommu_info.flags & AMD_IOMMU_DEVICE_FLAG_PASID_SUP)
98                                                                         != 0);
99                 return -ENODEV;
100         }
101
102         pasid_limit = min_t(unsigned int,
103                         (unsigned int)(1 << kfd->device_info->max_pasid_bits),
104                         iommu_info.max_pasids);
105
106         if (!kfd_set_pasid_limit(pasid_limit)) {
107                 dev_err(kfd_device, "error setting pasid limit\n");
108                 return -EBUSY;
109         }
110
111         return 0;
112 }
113
114 /** kfd_iommu_bind_process_to_device - Have the IOMMU bind a process
115  *
116  * Binds the given process to the given device using its PASID. This
117  * enables IOMMUv2 address translation for the process on the device.
118  *
119  * This function assumes that the process mutex is held.
120  */
121 int kfd_iommu_bind_process_to_device(struct kfd_process_device *pdd)
122 {
123         struct kfd_dev *dev = pdd->dev;
124         struct kfd_process *p = pdd->process;
125         int err;
126
127         if (!dev->device_info->needs_iommu_device || pdd->bound == PDD_BOUND)
128                 return 0;
129
130         if (unlikely(pdd->bound == PDD_BOUND_SUSPENDED)) {
131                 pr_err("Binding PDD_BOUND_SUSPENDED pdd is unexpected!\n");
132                 return -EINVAL;
133         }
134
135         err = amd_iommu_bind_pasid(dev->pdev, p->pasid, p->lead_thread);
136         if (!err)
137                 pdd->bound = PDD_BOUND;
138
139         return err;
140 }
141
142 /** kfd_iommu_unbind_process - Unbind process from all devices
143  *
144  * This removes all IOMMU device bindings of the process. To be used
145  * before process termination.
146  */
147 void kfd_iommu_unbind_process(struct kfd_process *p)
148 {
149         struct kfd_process_device *pdd;
150
151         list_for_each_entry(pdd, &p->per_device_data, per_device_list)
152                 if (pdd->bound == PDD_BOUND)
153                         amd_iommu_unbind_pasid(pdd->dev->pdev, p->pasid);
154 }
155
156 /* Callback for process shutdown invoked by the IOMMU driver */
157 static void iommu_pasid_shutdown_callback(struct pci_dev *pdev, int pasid)
158 {
159         struct kfd_dev *dev = kfd_device_by_pci_dev(pdev);
160         struct kfd_process *p;
161         struct kfd_process_device *pdd;
162
163         if (!dev)
164                 return;
165
166         /*
167          * Look for the process that matches the pasid. If there is no such
168          * process, we either released it in amdkfd's own notifier, or there
169          * is a bug. Unfortunately, there is no way to tell...
170          */
171         p = kfd_lookup_process_by_pasid(pasid);
172         if (!p)
173                 return;
174
175         pr_debug("Unbinding process %d from IOMMU\n", pasid);
176
177         mutex_lock(kfd_get_dbgmgr_mutex());
178
179         if (dev->dbgmgr && dev->dbgmgr->pasid == p->pasid) {
180                 if (!kfd_dbgmgr_unregister(dev->dbgmgr, p)) {
181                         kfd_dbgmgr_destroy(dev->dbgmgr);
182                         dev->dbgmgr = NULL;
183                 }
184         }
185
186         mutex_unlock(kfd_get_dbgmgr_mutex());
187
188         mutex_lock(&p->mutex);
189
190         pdd = kfd_get_process_device_data(dev, p);
191         if (pdd)
192                 /* For GPU relying on IOMMU, we need to dequeue here
193                  * when PASID is still bound.
194                  */
195                 kfd_process_dequeue_from_device(pdd);
196
197         mutex_unlock(&p->mutex);
198
199         kfd_unref_process(p);
200 }
201
202 /* This function called by IOMMU driver on PPR failure */
203 static int iommu_invalid_ppr_cb(struct pci_dev *pdev, int pasid,
204                 unsigned long address, u16 flags)
205 {
206         struct kfd_dev *dev;
207
208         dev_warn_ratelimited(kfd_device,
209                         "Invalid PPR device %x:%x.%x pasid %d address 0x%lX flags 0x%X",
210                         PCI_BUS_NUM(pdev->devfn),
211                         PCI_SLOT(pdev->devfn),
212                         PCI_FUNC(pdev->devfn),
213                         pasid,
214                         address,
215                         flags);
216
217         dev = kfd_device_by_pci_dev(pdev);
218         if (!WARN_ON(!dev))
219                 kfd_signal_iommu_event(dev, pasid, address,
220                         flags & PPR_FAULT_WRITE, flags & PPR_FAULT_EXEC);
221
222         return AMD_IOMMU_INV_PRI_RSP_INVALID;
223 }
224
225 /*
226  * Bind processes do the device that have been temporarily unbound
227  * (PDD_BOUND_SUSPENDED) in kfd_unbind_processes_from_device.
228  */
229 static int kfd_bind_processes_to_device(struct kfd_dev *kfd)
230 {
231         struct kfd_process_device *pdd;
232         struct kfd_process *p;
233         unsigned int temp;
234         int err = 0;
235
236         int idx = srcu_read_lock(&kfd_processes_srcu);
237
238         hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) {
239                 mutex_lock(&p->mutex);
240                 pdd = kfd_get_process_device_data(kfd, p);
241
242                 if (WARN_ON(!pdd) || pdd->bound != PDD_BOUND_SUSPENDED) {
243                         mutex_unlock(&p->mutex);
244                         continue;
245                 }
246
247                 err = amd_iommu_bind_pasid(kfd->pdev, p->pasid,
248                                 p->lead_thread);
249                 if (err < 0) {
250                         pr_err("Unexpected pasid %d binding failure\n",
251                                         p->pasid);
252                         mutex_unlock(&p->mutex);
253                         break;
254                 }
255
256                 pdd->bound = PDD_BOUND;
257                 mutex_unlock(&p->mutex);
258         }
259
260         srcu_read_unlock(&kfd_processes_srcu, idx);
261
262         return err;
263 }
264
265 /*
266  * Mark currently bound processes as PDD_BOUND_SUSPENDED. These
267  * processes will be restored to PDD_BOUND state in
268  * kfd_bind_processes_to_device.
269  */
270 static void kfd_unbind_processes_from_device(struct kfd_dev *kfd)
271 {
272         struct kfd_process_device *pdd;
273         struct kfd_process *p;
274         unsigned int temp;
275
276         int idx = srcu_read_lock(&kfd_processes_srcu);
277
278         hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) {
279                 mutex_lock(&p->mutex);
280                 pdd = kfd_get_process_device_data(kfd, p);
281
282                 if (WARN_ON(!pdd)) {
283                         mutex_unlock(&p->mutex);
284                         continue;
285                 }
286
287                 if (pdd->bound == PDD_BOUND)
288                         pdd->bound = PDD_BOUND_SUSPENDED;
289                 mutex_unlock(&p->mutex);
290         }
291
292         srcu_read_unlock(&kfd_processes_srcu, idx);
293 }
294
295 /** kfd_iommu_suspend - Prepare IOMMU for suspend
296  *
297  * This unbinds processes from the device and disables the IOMMU for
298  * the device.
299  */
300 void kfd_iommu_suspend(struct kfd_dev *kfd)
301 {
302         if (!kfd->device_info->needs_iommu_device)
303                 return;
304
305         kfd_unbind_processes_from_device(kfd);
306
307         amd_iommu_set_invalidate_ctx_cb(kfd->pdev, NULL);
308         amd_iommu_set_invalid_ppr_cb(kfd->pdev, NULL);
309         amd_iommu_free_device(kfd->pdev);
310 }
311
312 /** kfd_iommu_resume - Restore IOMMU after resume
313  *
314  * This reinitializes the IOMMU for the device and re-binds previously
315  * suspended processes to the device.
316  */
317 int kfd_iommu_resume(struct kfd_dev *kfd)
318 {
319         unsigned int pasid_limit;
320         int err;
321
322         if (!kfd->device_info->needs_iommu_device)
323                 return 0;
324
325         pasid_limit = kfd_get_pasid_limit();
326
327         err = amd_iommu_init_device(kfd->pdev, pasid_limit);
328         if (err)
329                 return -ENXIO;
330
331         amd_iommu_set_invalidate_ctx_cb(kfd->pdev,
332                                         iommu_pasid_shutdown_callback);
333         amd_iommu_set_invalid_ppr_cb(kfd->pdev,
334                                      iommu_invalid_ppr_cb);
335
336         err = kfd_bind_processes_to_device(kfd);
337         if (err) {
338                 amd_iommu_set_invalidate_ctx_cb(kfd->pdev, NULL);
339                 amd_iommu_set_invalid_ppr_cb(kfd->pdev, NULL);
340                 amd_iommu_free_device(kfd->pdev);
341                 return err;
342         }
343
344         return 0;
345 }
346
347 extern bool amd_iommu_pc_supported(void);
348 extern u8 amd_iommu_pc_get_max_banks(u16 devid);
349 extern u8 amd_iommu_pc_get_max_counters(u16 devid);
350
351 /** kfd_iommu_add_perf_counters - Add IOMMU performance counters to topology
352  */
353 int kfd_iommu_add_perf_counters(struct kfd_topology_device *kdev)
354 {
355         struct kfd_perf_properties *props;
356
357         if (!(kdev->node_props.capability & HSA_CAP_ATS_PRESENT))
358                 return 0;
359
360         if (!amd_iommu_pc_supported())
361                 return 0;
362
363         props = kfd_alloc_struct(props);
364         if (!props)
365                 return -ENOMEM;
366         strcpy(props->block_name, "iommu");
367         props->max_concurrent = amd_iommu_pc_get_max_banks(0) *
368                 amd_iommu_pc_get_max_counters(0); /* assume one iommu */
369         list_add_tail(&props->list, &kdev->perf_props);
370
371         return 0;
372 }
373
374 #endif