blob: 096710c3766d3a9cf99f52114d5bb630915bbe86 [file] [log] [blame]
Oded Gabbay19f6d2a2014-07-16 23:25:31 +03001/*
2 * Copyright 2014 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/mutex.h>
24#include <linux/log2.h>
25#include <linux/sched.h>
Ingo Molnar6e84f312017-02-08 18:51:29 +010026#include <linux/sched/mm.h>
Felix Kuehlingc7b12432017-11-27 18:29:50 -050027#include <linux/sched/task.h>
Oded Gabbay19f6d2a2014-07-16 23:25:31 +030028#include <linux/slab.h>
Oded Gabbayb17f0682014-07-17 00:06:27 +030029#include <linux/amd-iommu.h>
Oded Gabbay19f6d2a2014-07-16 23:25:31 +030030#include <linux/notifier.h>
Alexey Skidanovdd592392014-11-18 13:56:23 +020031#include <linux/compat.h>
Felix Kuehling373d7082017-11-14 16:41:19 -050032#include <linux/mman.h>
Alexey Skidanovdd592392014-11-18 13:56:23 +020033
Oded Gabbay19f6d2a2014-07-16 23:25:31 +030034struct mm_struct;
35
36#include "kfd_priv.h"
Ben Gozc3447e82015-05-20 18:05:44 +030037#include "kfd_dbgmgr.h"
Oded Gabbay19f6d2a2014-07-16 23:25:31 +030038
39/*
Oded Gabbay19f6d2a2014-07-16 23:25:31 +030040 * List of struct kfd_process (field kfd_process).
41 * Unique/indexed by mm_struct*
42 */
43#define KFD_PROCESS_TABLE_SIZE 5 /* bits: 32 entries */
44static DEFINE_HASHTABLE(kfd_processes_table, KFD_PROCESS_TABLE_SIZE);
45static DEFINE_MUTEX(kfd_processes_mutex);
46
47DEFINE_STATIC_SRCU(kfd_processes_srcu);
48
49static struct workqueue_struct *kfd_process_wq;
50
Oded Gabbay19f6d2a2014-07-16 23:25:31 +030051static struct kfd_process *find_process(const struct task_struct *thread);
Felix Kuehlingabb208a2017-11-27 18:29:52 -050052static void kfd_process_ref_release(struct kref *ref);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +030053static struct kfd_process *create_process(const struct task_struct *thread);
Felix Kuehling373d7082017-11-14 16:41:19 -050054static int kfd_process_init_cwsr(struct kfd_process *p, struct file *filep);
55
Oded Gabbay19f6d2a2014-07-16 23:25:31 +030056
57void kfd_process_create_wq(void)
58{
59 if (!kfd_process_wq)
Bhaktipriya Shridharfd320bf2016-05-29 21:14:11 +053060 kfd_process_wq = alloc_workqueue("kfd_process_wq", 0, 0);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +030061}
62
63void kfd_process_destroy_wq(void)
64{
65 if (kfd_process_wq) {
Oded Gabbay19f6d2a2014-07-16 23:25:31 +030066 destroy_workqueue(kfd_process_wq);
67 kfd_process_wq = NULL;
68 }
69}
70
Felix Kuehling373d7082017-11-14 16:41:19 -050071struct kfd_process *kfd_create_process(struct file *filep)
Oded Gabbay19f6d2a2014-07-16 23:25:31 +030072{
73 struct kfd_process *process;
Felix Kuehling373d7082017-11-14 16:41:19 -050074 struct task_struct *thread = current;
Oded Gabbay19f6d2a2014-07-16 23:25:31 +030075
Kent Russell4eacc26b2017-08-15 23:00:06 -040076 if (!thread->mm)
Oded Gabbay19f6d2a2014-07-16 23:25:31 +030077 return ERR_PTR(-EINVAL);
78
79 /* Only the pthreads threading model is supported. */
80 if (thread->group_leader->mm != thread->mm)
81 return ERR_PTR(-EINVAL);
82
83 /* Take mmap_sem because we call __mmu_notifier_register inside */
84 down_write(&thread->mm->mmap_sem);
85
86 /*
87 * take kfd processes mutex before starting of process creation
88 * so there won't be a case where two threads of the same process
89 * create two kfd_process structures
90 */
91 mutex_lock(&kfd_processes_mutex);
92
93 /* A prior open of /dev/kfd could have already created the process. */
94 process = find_process(thread);
95 if (process)
Kent Russell79775b62017-08-15 23:00:05 -040096 pr_debug("Process already found\n");
Oded Gabbay19f6d2a2014-07-16 23:25:31 +030097
98 if (!process)
99 process = create_process(thread);
100
101 mutex_unlock(&kfd_processes_mutex);
102
103 up_write(&thread->mm->mmap_sem);
104
Felix Kuehling373d7082017-11-14 16:41:19 -0500105 kfd_process_init_cwsr(process, filep);
106
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300107 return process;
108}
109
110struct kfd_process *kfd_get_process(const struct task_struct *thread)
111{
112 struct kfd_process *process;
113
Kent Russell4eacc26b2017-08-15 23:00:06 -0400114 if (!thread->mm)
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300115 return ERR_PTR(-EINVAL);
116
117 /* Only the pthreads threading model is supported. */
118 if (thread->group_leader->mm != thread->mm)
119 return ERR_PTR(-EINVAL);
120
121 process = find_process(thread);
122
123 return process;
124}
125
126static struct kfd_process *find_process_by_mm(const struct mm_struct *mm)
127{
128 struct kfd_process *process;
129
130 hash_for_each_possible_rcu(kfd_processes_table, process,
131 kfd_processes, (uintptr_t)mm)
132 if (process->mm == mm)
133 return process;
134
135 return NULL;
136}
137
138static struct kfd_process *find_process(const struct task_struct *thread)
139{
140 struct kfd_process *p;
141 int idx;
142
143 idx = srcu_read_lock(&kfd_processes_srcu);
144 p = find_process_by_mm(thread->mm);
145 srcu_read_unlock(&kfd_processes_srcu, idx);
146
147 return p;
148}
149
Felix Kuehlingabb208a2017-11-27 18:29:52 -0500150void kfd_unref_process(struct kfd_process *p)
151{
152 kref_put(&p->ref, kfd_process_ref_release);
153}
154
Felix Kuehlingde1450a2017-11-27 18:29:55 -0500155static void kfd_process_destroy_pdds(struct kfd_process *p)
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300156{
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300157 struct kfd_process_device *pdd, *temp;
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300158
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300159 list_for_each_entry_safe(pdd, temp, &p->per_device_data,
Felix Kuehlingde1450a2017-11-27 18:29:55 -0500160 per_device_list) {
161 pr_debug("Releasing pdd (topology id %d) for process (pasid %d)\n",
Oded Gabbay94a1ee02015-02-24 10:51:59 +0200162 pdd->dev->id, p->pasid);
163
Yong Zhao733fa1f2017-09-20 18:10:14 -0400164 list_del(&pdd->per_device_list);
Felix Kuehling373d7082017-11-14 16:41:19 -0500165
166 if (pdd->qpd.cwsr_kaddr)
167 free_pages((unsigned long)pdd->qpd.cwsr_kaddr,
168 get_order(KFD_CWSR_TBA_TMA_SIZE));
169
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300170 kfree(pdd);
171 }
Felix Kuehlingde1450a2017-11-27 18:29:55 -0500172}
173
174/* No process locking is needed in this function, because the process
175 * is not findable any more. We must assume that no other thread is
176 * using it any more, otherwise we couldn't safely free the process
177 * structure in the end.
178 */
179static void kfd_process_wq_release(struct work_struct *work)
180{
181 struct kfd_process *p = container_of(work, struct kfd_process,
182 release_work);
183 struct kfd_process_device *pdd;
184
185 pr_debug("Releasing process (pasid %d) in workqueue\n", p->pasid);
186
187 list_for_each_entry(pdd, &p->per_device_data, per_device_list) {
188 if (pdd->bound == PDD_BOUND)
189 amd_iommu_unbind_pasid(pdd->dev->pdev, p->pasid);
190 }
191
192 kfd_process_destroy_pdds(p);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300193
Andrew Lewyckyf3a39812015-05-10 12:15:46 +0300194 kfd_event_free_process(p);
195
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300196 kfd_pasid_free(p->pasid);
Felix Kuehlinga91e70e2017-08-26 02:00:57 -0400197 kfd_free_process_doorbells(p);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300198
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300199 mutex_destroy(&p->mutex);
200
Felix Kuehlingc7b12432017-11-27 18:29:50 -0500201 put_task_struct(p->lead_thread);
202
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300203 kfree(p);
Felix Kuehling5ce10682017-11-27 18:29:51 -0500204}
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300205
Felix Kuehling5ce10682017-11-27 18:29:51 -0500206static void kfd_process_ref_release(struct kref *ref)
207{
208 struct kfd_process *p = container_of(ref, struct kfd_process, ref);
209
210 INIT_WORK(&p->release_work, kfd_process_wq_release);
211 queue_work(kfd_process_wq, &p->release_work);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300212}
213
214static void kfd_process_destroy_delayed(struct rcu_head *rcu)
215{
Felix Kuehling5ce10682017-11-27 18:29:51 -0500216 struct kfd_process *p = container_of(rcu, struct kfd_process, rcu);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300217
Felix Kuehlingabb208a2017-11-27 18:29:52 -0500218 kfd_unref_process(p);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300219}
220
221static void kfd_process_notifier_release(struct mmu_notifier *mn,
222 struct mm_struct *mm)
223{
224 struct kfd_process *p;
Ben Goza82918f2015-03-25 13:12:20 +0200225 struct kfd_process_device *pdd = NULL;
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300226
227 /*
228 * The kfd_process structure can not be free because the
229 * mmu_notifier srcu is read locked
230 */
231 p = container_of(mn, struct kfd_process, mmu_notifier);
Felix Kuehling32fa8212017-08-15 23:00:12 -0400232 if (WARN_ON(p->mm != mm))
233 return;
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300234
235 mutex_lock(&kfd_processes_mutex);
236 hash_del_rcu(&p->kfd_processes);
237 mutex_unlock(&kfd_processes_mutex);
238 synchronize_srcu(&kfd_processes_srcu);
239
Ben Goz45102042014-07-17 01:04:10 +0300240 mutex_lock(&p->mutex);
241
Yair Shachar062c5672017-11-01 19:21:29 -0400242 /* Iterate over all process device data structures and if the
243 * pdd is in debug mode, we should first force unregistration,
244 * then we will be able to destroy the queues
245 */
246 list_for_each_entry(pdd, &p->per_device_data, per_device_list) {
247 struct kfd_dev *dev = pdd->dev;
248
249 mutex_lock(kfd_get_dbgmgr_mutex());
250 if (dev && dev->dbgmgr && dev->dbgmgr->pasid == p->pasid) {
251 if (!kfd_dbgmgr_unregister(dev->dbgmgr, p)) {
252 kfd_dbgmgr_destroy(dev->dbgmgr);
253 dev->dbgmgr = NULL;
254 }
255 }
256 mutex_unlock(kfd_get_dbgmgr_mutex());
257 }
258
Felix Kuehling9fd3f1bf2017-09-27 00:09:52 -0400259 kfd_process_dequeue_from_all_devices(p);
Ben Goz45102042014-07-17 01:04:10 +0300260 pqm_uninit(&p->pqm);
261
Felix Kuehling5ce10682017-11-27 18:29:51 -0500262 /* Indicate to other users that MM is no longer valid */
263 p->mm = NULL;
264
Ben Goz45102042014-07-17 01:04:10 +0300265 mutex_unlock(&p->mutex);
266
Felix Kuehling5ce10682017-11-27 18:29:51 -0500267 mmu_notifier_unregister_no_release(&p->mmu_notifier, mm);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300268 mmu_notifier_call_srcu(&p->rcu, &kfd_process_destroy_delayed);
269}
270
271static const struct mmu_notifier_ops kfd_process_mmu_notifier_ops = {
272 .release = kfd_process_notifier_release,
273};
274
Felix Kuehling373d7082017-11-14 16:41:19 -0500275static int kfd_process_init_cwsr(struct kfd_process *p, struct file *filep)
276{
277 int err = 0;
278 unsigned long offset;
279 struct kfd_process_device *temp, *pdd = NULL;
280 struct kfd_dev *dev = NULL;
281 struct qcm_process_device *qpd = NULL;
282
283 mutex_lock(&p->mutex);
284 list_for_each_entry_safe(pdd, temp, &p->per_device_data,
285 per_device_list) {
286 dev = pdd->dev;
287 qpd = &pdd->qpd;
288 if (!dev->cwsr_enabled || qpd->cwsr_kaddr)
289 continue;
290 offset = (dev->id | KFD_MMAP_RESERVED_MEM_MASK) << PAGE_SHIFT;
291 qpd->tba_addr = (int64_t)vm_mmap(filep, 0,
292 KFD_CWSR_TBA_TMA_SIZE, PROT_READ | PROT_EXEC,
293 MAP_SHARED, offset);
294
295 if (IS_ERR_VALUE(qpd->tba_addr)) {
296 pr_err("Failure to set tba address. error -%d.\n",
297 (int)qpd->tba_addr);
298 err = qpd->tba_addr;
299 qpd->tba_addr = 0;
300 qpd->cwsr_kaddr = NULL;
301 goto out;
302 }
303
304 memcpy(qpd->cwsr_kaddr, dev->cwsr_isa, dev->cwsr_isa_size);
305
306 qpd->tma_addr = qpd->tba_addr + KFD_CWSR_TMA_OFFSET;
307 pr_debug("set tba :0x%llx, tma:0x%llx, cwsr_kaddr:%p for pqm.\n",
308 qpd->tba_addr, qpd->tma_addr, qpd->cwsr_kaddr);
309 }
310out:
311 mutex_unlock(&p->mutex);
312 return err;
313}
314
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300315static struct kfd_process *create_process(const struct task_struct *thread)
316{
317 struct kfd_process *process;
318 int err = -ENOMEM;
319
320 process = kzalloc(sizeof(*process), GFP_KERNEL);
321
322 if (!process)
323 goto err_alloc_process;
324
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300325 process->pasid = kfd_pasid_alloc();
326 if (process->pasid == 0)
327 goto err_alloc_pasid;
328
Felix Kuehlinga91e70e2017-08-26 02:00:57 -0400329 if (kfd_alloc_process_doorbells(process) < 0)
330 goto err_alloc_doorbells;
331
Felix Kuehling5ce10682017-11-27 18:29:51 -0500332 kref_init(&process->ref);
333
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300334 mutex_init(&process->mutex);
335
336 process->mm = thread->mm;
337
338 /* register notifier */
339 process->mmu_notifier.ops = &kfd_process_mmu_notifier_ops;
340 err = __mmu_notifier_register(&process->mmu_notifier, process->mm);
341 if (err)
342 goto err_mmu_notifier;
343
344 hash_add_rcu(kfd_processes_table, &process->kfd_processes,
345 (uintptr_t)process->mm);
346
347 process->lead_thread = thread->group_leader;
Felix Kuehlingc7b12432017-11-27 18:29:50 -0500348 get_task_struct(process->lead_thread);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300349
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300350 INIT_LIST_HEAD(&process->per_device_data);
351
Andrew Lewyckyf3a39812015-05-10 12:15:46 +0300352 kfd_event_init_process(process);
353
Ben Goz45102042014-07-17 01:04:10 +0300354 err = pqm_init(&process->pqm, process);
355 if (err != 0)
356 goto err_process_pqm_init;
357
Alexey Skidanovdd592392014-11-18 13:56:23 +0200358 /* init process apertures*/
Andy Lutomirski10f16852016-03-22 14:25:19 -0700359 process->is_32bit_user_mode = in_compat_syscall();
Dan Carpenterb312b2b2017-06-14 13:58:53 +0300360 err = kfd_init_apertures(process);
361 if (err != 0)
Geert Uytterhoeven7a10d632017-06-01 12:28:38 +0200362 goto err_init_apertures;
Alexey Skidanovdd592392014-11-18 13:56:23 +0200363
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300364 return process;
365
Geert Uytterhoeven7a10d632017-06-01 12:28:38 +0200366err_init_apertures:
Alexey Skidanovdd592392014-11-18 13:56:23 +0200367 pqm_uninit(&process->pqm);
Ben Goz45102042014-07-17 01:04:10 +0300368err_process_pqm_init:
369 hash_del_rcu(&process->kfd_processes);
370 synchronize_rcu();
371 mmu_notifier_unregister_no_release(&process->mmu_notifier, process->mm);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300372err_mmu_notifier:
Oded Gabbay7fd5e032016-06-23 17:54:29 +0300373 mutex_destroy(&process->mutex);
Felix Kuehlinga91e70e2017-08-26 02:00:57 -0400374 kfd_free_process_doorbells(process);
375err_alloc_doorbells:
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300376 kfd_pasid_free(process->pasid);
377err_alloc_pasid:
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300378 kfree(process);
379err_alloc_process:
380 return ERR_PTR(err);
381}
382
383struct kfd_process_device *kfd_get_process_device_data(struct kfd_dev *dev,
Alexey Skidanov093c7d82014-11-18 14:00:04 +0200384 struct kfd_process *p)
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300385{
386 struct kfd_process_device *pdd = NULL;
387
388 list_for_each_entry(pdd, &p->per_device_data, per_device_list)
389 if (pdd->dev == dev)
Yong Zhao733fa1f2017-09-20 18:10:14 -0400390 return pdd;
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300391
Yong Zhao733fa1f2017-09-20 18:10:14 -0400392 return NULL;
Alexey Skidanov093c7d82014-11-18 14:00:04 +0200393}
394
395struct kfd_process_device *kfd_create_process_device_data(struct kfd_dev *dev,
396 struct kfd_process *p)
397{
398 struct kfd_process_device *pdd = NULL;
399
400 pdd = kzalloc(sizeof(*pdd), GFP_KERNEL);
Felix Kuehling2d9b36f2017-11-27 18:29:54 -0500401 if (!pdd)
402 return NULL;
403
404 pdd->dev = dev;
405 INIT_LIST_HEAD(&pdd->qpd.queues_list);
406 INIT_LIST_HEAD(&pdd->qpd.priv_queue_list);
407 pdd->qpd.dqm = dev->dqm;
408 pdd->qpd.pqm = &p->pqm;
409 pdd->process = p;
410 pdd->bound = PDD_UNBOUND;
411 pdd->already_dequeued = false;
412 list_add(&pdd->per_device_list, &p->per_device_data);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300413
414 return pdd;
415}
416
417/*
418 * Direct the IOMMU to bind the process (specifically the pasid->mm)
419 * to the device.
420 * Unbinding occurs when the process dies or the device is removed.
421 *
422 * Assumes that the process lock is held.
423 */
424struct kfd_process_device *kfd_bind_process_to_device(struct kfd_dev *dev,
425 struct kfd_process *p)
426{
Alexey Skidanov093c7d82014-11-18 14:00:04 +0200427 struct kfd_process_device *pdd;
Oded Gabbayb17f0682014-07-17 00:06:27 +0300428 int err;
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300429
Alexey Skidanov093c7d82014-11-18 14:00:04 +0200430 pdd = kfd_get_process_device_data(dev, p);
431 if (!pdd) {
432 pr_err("Process device data doesn't exist\n");
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300433 return ERR_PTR(-ENOMEM);
Alexey Skidanov093c7d82014-11-18 14:00:04 +0200434 }
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300435
Yong Zhao733fa1f2017-09-20 18:10:14 -0400436 if (pdd->bound == PDD_BOUND) {
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300437 return pdd;
Yong Zhao733fa1f2017-09-20 18:10:14 -0400438 } else if (unlikely(pdd->bound == PDD_BOUND_SUSPENDED)) {
439 pr_err("Binding PDD_BOUND_SUSPENDED pdd is unexpected!\n");
440 return ERR_PTR(-EINVAL);
441 }
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300442
Oded Gabbayb17f0682014-07-17 00:06:27 +0300443 err = amd_iommu_bind_pasid(dev->pdev, p->pasid, p->lead_thread);
444 if (err < 0)
445 return ERR_PTR(err);
446
Yong Zhao733fa1f2017-09-20 18:10:14 -0400447 pdd->bound = PDD_BOUND;
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300448
449 return pdd;
450}
451
Yong Zhao733fa1f2017-09-20 18:10:14 -0400452/*
453 * Bind processes do the device that have been temporarily unbound
454 * (PDD_BOUND_SUSPENDED) in kfd_unbind_processes_from_device.
455 */
456int kfd_bind_processes_to_device(struct kfd_dev *dev)
457{
458 struct kfd_process_device *pdd;
459 struct kfd_process *p;
460 unsigned int temp;
461 int err = 0;
462
463 int idx = srcu_read_lock(&kfd_processes_srcu);
464
465 hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) {
466 mutex_lock(&p->mutex);
467 pdd = kfd_get_process_device_data(dev, p);
468 if (pdd->bound != PDD_BOUND_SUSPENDED) {
469 mutex_unlock(&p->mutex);
470 continue;
471 }
472
473 err = amd_iommu_bind_pasid(dev->pdev, p->pasid,
474 p->lead_thread);
475 if (err < 0) {
Felix Kuehling894a8292017-11-01 19:21:33 -0400476 pr_err("Unexpected pasid %d binding failure\n",
Yong Zhao733fa1f2017-09-20 18:10:14 -0400477 p->pasid);
478 mutex_unlock(&p->mutex);
479 break;
480 }
481
482 pdd->bound = PDD_BOUND;
483 mutex_unlock(&p->mutex);
484 }
485
486 srcu_read_unlock(&kfd_processes_srcu, idx);
487
488 return err;
489}
490
491/*
Yong Zhaoe2a8e992017-11-01 19:21:28 -0400492 * Mark currently bound processes as PDD_BOUND_SUSPENDED. These
493 * processes will be restored to PDD_BOUND state in
494 * kfd_bind_processes_to_device.
Yong Zhao733fa1f2017-09-20 18:10:14 -0400495 */
496void kfd_unbind_processes_from_device(struct kfd_dev *dev)
497{
498 struct kfd_process_device *pdd;
499 struct kfd_process *p;
Yong Zhaoe2a8e992017-11-01 19:21:28 -0400500 unsigned int temp;
Yong Zhao733fa1f2017-09-20 18:10:14 -0400501
502 int idx = srcu_read_lock(&kfd_processes_srcu);
503
504 hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) {
505 mutex_lock(&p->mutex);
506 pdd = kfd_get_process_device_data(dev, p);
Yong Zhaoe2a8e992017-11-01 19:21:28 -0400507
Yong Zhao733fa1f2017-09-20 18:10:14 -0400508 if (pdd->bound == PDD_BOUND)
509 pdd->bound = PDD_BOUND_SUSPENDED;
510 mutex_unlock(&p->mutex);
Yong Zhao733fa1f2017-09-20 18:10:14 -0400511 }
512
513 srcu_read_unlock(&kfd_processes_srcu, idx);
514}
515
516void kfd_process_iommu_unbind_callback(struct kfd_dev *dev, unsigned int pasid)
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300517{
518 struct kfd_process *p;
519 struct kfd_process_device *pdd;
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300520
Oded Gabbay121b78e2016-05-26 08:41:08 +0300521 /*
522 * Look for the process that matches the pasid. If there is no such
523 * process, we either released it in amdkfd's own notifier, or there
524 * is a bug. Unfortunately, there is no way to tell...
525 */
Edward O'Callaghanad16a4692016-09-17 15:01:42 +1000526 p = kfd_lookup_process_by_pasid(pasid);
527 if (!p)
528 return;
Oded Gabbay121b78e2016-05-26 08:41:08 +0300529
Edward O'Callaghanad16a4692016-09-17 15:01:42 +1000530 pr_debug("Unbinding process %d from IOMMU\n", pasid);
Oded Gabbay121b78e2016-05-26 08:41:08 +0300531
Yair Shachar062c5672017-11-01 19:21:29 -0400532 mutex_lock(kfd_get_dbgmgr_mutex());
533
534 if (dev->dbgmgr && dev->dbgmgr->pasid == p->pasid) {
535 if (!kfd_dbgmgr_unregister(dev->dbgmgr, p)) {
536 kfd_dbgmgr_destroy(dev->dbgmgr);
537 dev->dbgmgr = NULL;
538 }
539 }
540
541 mutex_unlock(kfd_get_dbgmgr_mutex());
Oded Gabbay121b78e2016-05-26 08:41:08 +0300542
Felix Kuehlingabb208a2017-11-27 18:29:52 -0500543 mutex_lock(&p->mutex);
544
Edward O'Callaghanad16a4692016-09-17 15:01:42 +1000545 pdd = kfd_get_process_device_data(dev, p);
Felix Kuehling9fd3f1bf2017-09-27 00:09:52 -0400546 if (pdd)
547 /* For GPU relying on IOMMU, we need to dequeue here
548 * when PASID is still bound.
549 */
550 kfd_process_dequeue_from_device(pdd);
Oded Gabbay121b78e2016-05-26 08:41:08 +0300551
Edward O'Callaghanad16a4692016-09-17 15:01:42 +1000552 mutex_unlock(&p->mutex);
Felix Kuehlingabb208a2017-11-27 18:29:52 -0500553
554 kfd_unref_process(p);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300555}
556
Kent Russell8eabaf52017-08-15 23:00:04 -0400557struct kfd_process_device *kfd_get_first_process_device_data(
558 struct kfd_process *p)
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300559{
560 return list_first_entry(&p->per_device_data,
561 struct kfd_process_device,
562 per_device_list);
563}
564
Kent Russell8eabaf52017-08-15 23:00:04 -0400565struct kfd_process_device *kfd_get_next_process_device_data(
566 struct kfd_process *p,
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300567 struct kfd_process_device *pdd)
568{
569 if (list_is_last(&pdd->per_device_list, &p->per_device_data))
570 return NULL;
571 return list_next_entry(pdd, per_device_list);
572}
573
574bool kfd_has_process_device_data(struct kfd_process *p)
575{
576 return !(list_empty(&p->per_device_data));
577}
Andrew Lewyckyf3a39812015-05-10 12:15:46 +0300578
Felix Kuehlingabb208a2017-11-27 18:29:52 -0500579/* This increments the process->ref counter. */
Andrew Lewyckyf3a39812015-05-10 12:15:46 +0300580struct kfd_process *kfd_lookup_process_by_pasid(unsigned int pasid)
581{
Yong Zhao82c16b42017-11-27 18:29:53 -0500582 struct kfd_process *p, *ret_p = NULL;
Andrew Lewyckyf3a39812015-05-10 12:15:46 +0300583 unsigned int temp;
584
585 int idx = srcu_read_lock(&kfd_processes_srcu);
586
587 hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) {
588 if (p->pasid == pasid) {
Felix Kuehlingabb208a2017-11-27 18:29:52 -0500589 kref_get(&p->ref);
Yong Zhao82c16b42017-11-27 18:29:53 -0500590 ret_p = p;
Andrew Lewyckyf3a39812015-05-10 12:15:46 +0300591 break;
592 }
593 }
594
595 srcu_read_unlock(&kfd_processes_srcu, idx);
596
Yong Zhao82c16b42017-11-27 18:29:53 -0500597 return ret_p;
Andrew Lewyckyf3a39812015-05-10 12:15:46 +0300598}
Felix Kuehling373d7082017-11-14 16:41:19 -0500599
600int kfd_reserved_mem_mmap(struct kfd_process *process,
601 struct vm_area_struct *vma)
602{
603 struct kfd_dev *dev = kfd_device_by_id(vma->vm_pgoff);
604 struct kfd_process_device *pdd;
605 struct qcm_process_device *qpd;
606
607 if (!dev)
608 return -EINVAL;
609 if ((vma->vm_end - vma->vm_start) != KFD_CWSR_TBA_TMA_SIZE) {
610 pr_err("Incorrect CWSR mapping size.\n");
611 return -EINVAL;
612 }
613
614 pdd = kfd_get_process_device_data(dev, process);
615 if (!pdd)
616 return -EINVAL;
617 qpd = &pdd->qpd;
618
619 qpd->cwsr_kaddr = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
620 get_order(KFD_CWSR_TBA_TMA_SIZE));
621 if (!qpd->cwsr_kaddr) {
622 pr_err("Error allocating per process CWSR buffer.\n");
623 return -ENOMEM;
624 }
625
626 vma->vm_flags |= VM_IO | VM_DONTCOPY | VM_DONTEXPAND
627 | VM_NORESERVE | VM_DONTDUMP | VM_PFNMAP;
628 /* Mapping pages to user process */
629 return remap_pfn_range(vma, vma->vm_start,
630 PFN_DOWN(__pa(qpd->cwsr_kaddr)),
631 KFD_CWSR_TBA_TMA_SIZE, vma->vm_page_prot);
632}
Felix Kuehling851a6452017-11-27 18:29:49 -0500633
634#if defined(CONFIG_DEBUG_FS)
635
636int kfd_debugfs_mqds_by_process(struct seq_file *m, void *data)
637{
638 struct kfd_process *p;
639 unsigned int temp;
640 int r = 0;
641
642 int idx = srcu_read_lock(&kfd_processes_srcu);
643
644 hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) {
645 seq_printf(m, "Process %d PASID %d:\n",
646 p->lead_thread->tgid, p->pasid);
647
648 mutex_lock(&p->mutex);
649 r = pqm_debugfs_mqds(m, &p->pqm);
650 mutex_unlock(&p->mutex);
651
652 if (r)
653 break;
654 }
655
656 srcu_read_unlock(&kfd_processes_srcu, idx);
657
658 return r;
659}
660
661#endif