blob: 1325f88591ae0c611e184ec9bc3b057d257fb36b [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>
Oded Gabbay19f6d2a2014-07-16 23:25:31 +030027#include <linux/slab.h>
Oded Gabbayb17f0682014-07-17 00:06:27 +030028#include <linux/amd-iommu.h>
Oded Gabbay19f6d2a2014-07-16 23:25:31 +030029#include <linux/notifier.h>
Alexey Skidanovdd592392014-11-18 13:56:23 +020030#include <linux/compat.h>
31
Oded Gabbay19f6d2a2014-07-16 23:25:31 +030032struct mm_struct;
33
34#include "kfd_priv.h"
Ben Gozc3447e82015-05-20 18:05:44 +030035#include "kfd_dbgmgr.h"
Oded Gabbay19f6d2a2014-07-16 23:25:31 +030036
37/*
38 * Initial size for the array of queues.
39 * The allocated size is doubled each time
40 * it is exceeded up to MAX_PROCESS_QUEUES.
41 */
42#define INITIAL_QUEUE_ARRAY_SIZE 16
43
44/*
45 * List of struct kfd_process (field kfd_process).
46 * Unique/indexed by mm_struct*
47 */
48#define KFD_PROCESS_TABLE_SIZE 5 /* bits: 32 entries */
49static DEFINE_HASHTABLE(kfd_processes_table, KFD_PROCESS_TABLE_SIZE);
50static DEFINE_MUTEX(kfd_processes_mutex);
51
52DEFINE_STATIC_SRCU(kfd_processes_srcu);
53
54static struct workqueue_struct *kfd_process_wq;
55
56struct kfd_process_release_work {
57 struct work_struct kfd_work;
58 struct kfd_process *p;
59};
60
61static struct kfd_process *find_process(const struct task_struct *thread);
62static struct kfd_process *create_process(const struct task_struct *thread);
63
64void kfd_process_create_wq(void)
65{
66 if (!kfd_process_wq)
Bhaktipriya Shridharfd320bf2016-05-29 21:14:11 +053067 kfd_process_wq = alloc_workqueue("kfd_process_wq", 0, 0);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +030068}
69
70void kfd_process_destroy_wq(void)
71{
72 if (kfd_process_wq) {
Oded Gabbay19f6d2a2014-07-16 23:25:31 +030073 destroy_workqueue(kfd_process_wq);
74 kfd_process_wq = NULL;
75 }
76}
77
78struct kfd_process *kfd_create_process(const struct task_struct *thread)
79{
80 struct kfd_process *process;
81
Kent Russell4eacc26b2017-08-15 23:00:06 -040082 if (!thread->mm)
Oded Gabbay19f6d2a2014-07-16 23:25:31 +030083 return ERR_PTR(-EINVAL);
84
85 /* Only the pthreads threading model is supported. */
86 if (thread->group_leader->mm != thread->mm)
87 return ERR_PTR(-EINVAL);
88
89 /* Take mmap_sem because we call __mmu_notifier_register inside */
90 down_write(&thread->mm->mmap_sem);
91
92 /*
93 * take kfd processes mutex before starting of process creation
94 * so there won't be a case where two threads of the same process
95 * create two kfd_process structures
96 */
97 mutex_lock(&kfd_processes_mutex);
98
99 /* A prior open of /dev/kfd could have already created the process. */
100 process = find_process(thread);
101 if (process)
Kent Russell79775b62017-08-15 23:00:05 -0400102 pr_debug("Process already found\n");
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300103
104 if (!process)
105 process = create_process(thread);
106
107 mutex_unlock(&kfd_processes_mutex);
108
109 up_write(&thread->mm->mmap_sem);
110
111 return process;
112}
113
114struct kfd_process *kfd_get_process(const struct task_struct *thread)
115{
116 struct kfd_process *process;
117
Kent Russell4eacc26b2017-08-15 23:00:06 -0400118 if (!thread->mm)
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300119 return ERR_PTR(-EINVAL);
120
121 /* Only the pthreads threading model is supported. */
122 if (thread->group_leader->mm != thread->mm)
123 return ERR_PTR(-EINVAL);
124
125 process = find_process(thread);
126
127 return process;
128}
129
130static struct kfd_process *find_process_by_mm(const struct mm_struct *mm)
131{
132 struct kfd_process *process;
133
134 hash_for_each_possible_rcu(kfd_processes_table, process,
135 kfd_processes, (uintptr_t)mm)
136 if (process->mm == mm)
137 return process;
138
139 return NULL;
140}
141
142static struct kfd_process *find_process(const struct task_struct *thread)
143{
144 struct kfd_process *p;
145 int idx;
146
147 idx = srcu_read_lock(&kfd_processes_srcu);
148 p = find_process_by_mm(thread->mm);
149 srcu_read_unlock(&kfd_processes_srcu, idx);
150
151 return p;
152}
153
154static void kfd_process_wq_release(struct work_struct *work)
155{
156 struct kfd_process_release_work *my_work;
157 struct kfd_process_device *pdd, *temp;
158 struct kfd_process *p;
159
160 my_work = (struct kfd_process_release_work *) work;
161
162 p = my_work->p;
163
Oded Gabbay94a1ee02015-02-24 10:51:59 +0200164 pr_debug("Releasing process (pasid %d) in workqueue\n",
165 p->pasid);
166
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300167 mutex_lock(&p->mutex);
168
169 list_for_each_entry_safe(pdd, temp, &p->per_device_data,
170 per_device_list) {
Oded Gabbay94a1ee02015-02-24 10:51:59 +0200171 pr_debug("Releasing pdd (topology id %d) for process (pasid %d) in workqueue\n",
172 pdd->dev->id, p->pasid);
173
Ben Goza82918f2015-03-25 13:12:20 +0200174 if (pdd->reset_wavefronts)
Ben Gozc3447e82015-05-20 18:05:44 +0300175 dbgdev_wave_reset_wavefronts(pdd->dev, p);
176
Yong Zhao733fa1f2017-09-20 18:10:14 -0400177 if (pdd->bound == PDD_BOUND)
178 amd_iommu_unbind_pasid(pdd->dev->pdev, p->pasid);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300179
Yong Zhao733fa1f2017-09-20 18:10:14 -0400180 list_del(&pdd->per_device_list);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300181 kfree(pdd);
182 }
183
Andrew Lewyckyf3a39812015-05-10 12:15:46 +0300184 kfd_event_free_process(p);
185
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300186 kfd_pasid_free(p->pasid);
Felix Kuehlinga91e70e2017-08-26 02:00:57 -0400187 kfd_free_process_doorbells(p);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300188
189 mutex_unlock(&p->mutex);
190
191 mutex_destroy(&p->mutex);
192
193 kfree(p->queues);
194
195 kfree(p);
196
Amitoj Kaur Chawla642f0f22016-01-25 23:03:57 +0530197 kfree(work);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300198}
199
200static void kfd_process_destroy_delayed(struct rcu_head *rcu)
201{
202 struct kfd_process_release_work *work;
203 struct kfd_process *p;
204
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300205 p = container_of(rcu, struct kfd_process, rcu);
Felix Kuehling32fa8212017-08-15 23:00:12 -0400206 WARN_ON(atomic_read(&p->mm->mm_count) <= 0);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300207
208 mmdrop(p->mm);
209
Firo Yang1549fcd2015-04-23 17:58:05 +0800210 work = kmalloc(sizeof(struct kfd_process_release_work), GFP_ATOMIC);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300211
212 if (work) {
213 INIT_WORK((struct work_struct *) work, kfd_process_wq_release);
214 work->p = p;
215 queue_work(kfd_process_wq, (struct work_struct *) work);
216 }
217}
218
219static void kfd_process_notifier_release(struct mmu_notifier *mn,
220 struct mm_struct *mm)
221{
222 struct kfd_process *p;
Ben Goza82918f2015-03-25 13:12:20 +0200223 struct kfd_process_device *pdd = NULL;
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300224
225 /*
226 * The kfd_process structure can not be free because the
227 * mmu_notifier srcu is read locked
228 */
229 p = container_of(mn, struct kfd_process, mmu_notifier);
Felix Kuehling32fa8212017-08-15 23:00:12 -0400230 if (WARN_ON(p->mm != mm))
231 return;
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300232
233 mutex_lock(&kfd_processes_mutex);
234 hash_del_rcu(&p->kfd_processes);
235 mutex_unlock(&kfd_processes_mutex);
236 synchronize_srcu(&kfd_processes_srcu);
237
Ben Goz45102042014-07-17 01:04:10 +0300238 mutex_lock(&p->mutex);
239
240 /* In case our notifier is called before IOMMU notifier */
241 pqm_uninit(&p->pqm);
242
Ben Goza82918f2015-03-25 13:12:20 +0200243 /* Iterate over all process device data structure and check
Oded Gabbaybc4755a2016-05-26 08:41:48 +0300244 * if we should delete debug managers and reset all wavefronts
245 */
246 list_for_each_entry(pdd, &p->per_device_data, per_device_list) {
247 if ((pdd->dev->dbgmgr) &&
248 (pdd->dev->dbgmgr->pasid == p->pasid))
249 kfd_dbgmgr_destroy(pdd->dev->dbgmgr);
250
Ben Goza82918f2015-03-25 13:12:20 +0200251 if (pdd->reset_wavefronts) {
Kent Russell79775b62017-08-15 23:00:05 -0400252 pr_warn("Resetting all wave fronts\n");
Ben Goza82918f2015-03-25 13:12:20 +0200253 dbgdev_wave_reset_wavefronts(pdd->dev, p);
254 pdd->reset_wavefronts = false;
255 }
Oded Gabbaybc4755a2016-05-26 08:41:48 +0300256 }
Ben Goza82918f2015-03-25 13:12:20 +0200257
Ben Goz45102042014-07-17 01:04:10 +0300258 mutex_unlock(&p->mutex);
259
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300260 /*
261 * Because we drop mm_count inside kfd_process_destroy_delayed
262 * and because the mmu_notifier_unregister function also drop
263 * mm_count we need to take an extra count here.
264 */
Vegard Nossumf1f10072017-02-27 14:30:07 -0800265 mmgrab(p->mm);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300266 mmu_notifier_unregister_no_release(&p->mmu_notifier, p->mm);
267 mmu_notifier_call_srcu(&p->rcu, &kfd_process_destroy_delayed);
268}
269
270static const struct mmu_notifier_ops kfd_process_mmu_notifier_ops = {
271 .release = kfd_process_notifier_release,
272};
273
274static struct kfd_process *create_process(const struct task_struct *thread)
275{
276 struct kfd_process *process;
277 int err = -ENOMEM;
278
279 process = kzalloc(sizeof(*process), GFP_KERNEL);
280
281 if (!process)
282 goto err_alloc_process;
283
284 process->queues = kmalloc_array(INITIAL_QUEUE_ARRAY_SIZE,
285 sizeof(process->queues[0]), GFP_KERNEL);
286 if (!process->queues)
287 goto err_alloc_queues;
288
289 process->pasid = kfd_pasid_alloc();
290 if (process->pasid == 0)
291 goto err_alloc_pasid;
292
Felix Kuehlinga91e70e2017-08-26 02:00:57 -0400293 if (kfd_alloc_process_doorbells(process) < 0)
294 goto err_alloc_doorbells;
295
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300296 mutex_init(&process->mutex);
297
298 process->mm = thread->mm;
299
300 /* register notifier */
301 process->mmu_notifier.ops = &kfd_process_mmu_notifier_ops;
302 err = __mmu_notifier_register(&process->mmu_notifier, process->mm);
303 if (err)
304 goto err_mmu_notifier;
305
306 hash_add_rcu(kfd_processes_table, &process->kfd_processes,
307 (uintptr_t)process->mm);
308
309 process->lead_thread = thread->group_leader;
310
311 process->queue_array_size = INITIAL_QUEUE_ARRAY_SIZE;
312
313 INIT_LIST_HEAD(&process->per_device_data);
314
Andrew Lewyckyf3a39812015-05-10 12:15:46 +0300315 kfd_event_init_process(process);
316
Ben Goz45102042014-07-17 01:04:10 +0300317 err = pqm_init(&process->pqm, process);
318 if (err != 0)
319 goto err_process_pqm_init;
320
Alexey Skidanovdd592392014-11-18 13:56:23 +0200321 /* init process apertures*/
Andy Lutomirski10f16852016-03-22 14:25:19 -0700322 process->is_32bit_user_mode = in_compat_syscall();
Dan Carpenterb312b2b2017-06-14 13:58:53 +0300323 err = kfd_init_apertures(process);
324 if (err != 0)
Geert Uytterhoeven7a10d632017-06-01 12:28:38 +0200325 goto err_init_apertures;
Alexey Skidanovdd592392014-11-18 13:56:23 +0200326
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300327 return process;
328
Geert Uytterhoeven7a10d632017-06-01 12:28:38 +0200329err_init_apertures:
Alexey Skidanovdd592392014-11-18 13:56:23 +0200330 pqm_uninit(&process->pqm);
Ben Goz45102042014-07-17 01:04:10 +0300331err_process_pqm_init:
332 hash_del_rcu(&process->kfd_processes);
333 synchronize_rcu();
334 mmu_notifier_unregister_no_release(&process->mmu_notifier, process->mm);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300335err_mmu_notifier:
Oded Gabbay7fd5e032016-06-23 17:54:29 +0300336 mutex_destroy(&process->mutex);
Felix Kuehlinga91e70e2017-08-26 02:00:57 -0400337 kfd_free_process_doorbells(process);
338err_alloc_doorbells:
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300339 kfd_pasid_free(process->pasid);
340err_alloc_pasid:
341 kfree(process->queues);
342err_alloc_queues:
343 kfree(process);
344err_alloc_process:
345 return ERR_PTR(err);
346}
347
348struct kfd_process_device *kfd_get_process_device_data(struct kfd_dev *dev,
Alexey Skidanov093c7d82014-11-18 14:00:04 +0200349 struct kfd_process *p)
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300350{
351 struct kfd_process_device *pdd = NULL;
352
353 list_for_each_entry(pdd, &p->per_device_data, per_device_list)
354 if (pdd->dev == dev)
Yong Zhao733fa1f2017-09-20 18:10:14 -0400355 return pdd;
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300356
Yong Zhao733fa1f2017-09-20 18:10:14 -0400357 return NULL;
Alexey Skidanov093c7d82014-11-18 14:00:04 +0200358}
359
360struct kfd_process_device *kfd_create_process_device_data(struct kfd_dev *dev,
361 struct kfd_process *p)
362{
363 struct kfd_process_device *pdd = NULL;
364
365 pdd = kzalloc(sizeof(*pdd), GFP_KERNEL);
366 if (pdd != NULL) {
367 pdd->dev = dev;
368 INIT_LIST_HEAD(&pdd->qpd.queues_list);
369 INIT_LIST_HEAD(&pdd->qpd.priv_queue_list);
370 pdd->qpd.dqm = dev->dqm;
Ben Goza82918f2015-03-25 13:12:20 +0200371 pdd->reset_wavefronts = false;
Yong Zhao733fa1f2017-09-20 18:10:14 -0400372 pdd->bound = PDD_UNBOUND;
Alexey Skidanov093c7d82014-11-18 14:00:04 +0200373 list_add(&pdd->per_device_list, &p->per_device_data);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300374 }
375
376 return pdd;
377}
378
379/*
380 * Direct the IOMMU to bind the process (specifically the pasid->mm)
381 * to the device.
382 * Unbinding occurs when the process dies or the device is removed.
383 *
384 * Assumes that the process lock is held.
385 */
386struct kfd_process_device *kfd_bind_process_to_device(struct kfd_dev *dev,
387 struct kfd_process *p)
388{
Alexey Skidanov093c7d82014-11-18 14:00:04 +0200389 struct kfd_process_device *pdd;
Oded Gabbayb17f0682014-07-17 00:06:27 +0300390 int err;
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300391
Alexey Skidanov093c7d82014-11-18 14:00:04 +0200392 pdd = kfd_get_process_device_data(dev, p);
393 if (!pdd) {
394 pr_err("Process device data doesn't exist\n");
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300395 return ERR_PTR(-ENOMEM);
Alexey Skidanov093c7d82014-11-18 14:00:04 +0200396 }
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300397
Yong Zhao733fa1f2017-09-20 18:10:14 -0400398 if (pdd->bound == PDD_BOUND) {
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300399 return pdd;
Yong Zhao733fa1f2017-09-20 18:10:14 -0400400 } else if (unlikely(pdd->bound == PDD_BOUND_SUSPENDED)) {
401 pr_err("Binding PDD_BOUND_SUSPENDED pdd is unexpected!\n");
402 return ERR_PTR(-EINVAL);
403 }
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300404
Oded Gabbayb17f0682014-07-17 00:06:27 +0300405 err = amd_iommu_bind_pasid(dev->pdev, p->pasid, p->lead_thread);
406 if (err < 0)
407 return ERR_PTR(err);
408
Yong Zhao733fa1f2017-09-20 18:10:14 -0400409 pdd->bound = PDD_BOUND;
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300410
411 return pdd;
412}
413
Yong Zhao733fa1f2017-09-20 18:10:14 -0400414/*
415 * Bind processes do the device that have been temporarily unbound
416 * (PDD_BOUND_SUSPENDED) in kfd_unbind_processes_from_device.
417 */
418int kfd_bind_processes_to_device(struct kfd_dev *dev)
419{
420 struct kfd_process_device *pdd;
421 struct kfd_process *p;
422 unsigned int temp;
423 int err = 0;
424
425 int idx = srcu_read_lock(&kfd_processes_srcu);
426
427 hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) {
428 mutex_lock(&p->mutex);
429 pdd = kfd_get_process_device_data(dev, p);
430 if (pdd->bound != PDD_BOUND_SUSPENDED) {
431 mutex_unlock(&p->mutex);
432 continue;
433 }
434
435 err = amd_iommu_bind_pasid(dev->pdev, p->pasid,
436 p->lead_thread);
437 if (err < 0) {
438 pr_err("unexpected pasid %d binding failure\n",
439 p->pasid);
440 mutex_unlock(&p->mutex);
441 break;
442 }
443
444 pdd->bound = PDD_BOUND;
445 mutex_unlock(&p->mutex);
446 }
447
448 srcu_read_unlock(&kfd_processes_srcu, idx);
449
450 return err;
451}
452
453/*
454 * Temporarily unbind currently bound processes from the device and
455 * mark them as PDD_BOUND_SUSPENDED. These processes will be restored
456 * to PDD_BOUND state in kfd_bind_processes_to_device.
457 */
458void kfd_unbind_processes_from_device(struct kfd_dev *dev)
459{
460 struct kfd_process_device *pdd;
461 struct kfd_process *p;
462 unsigned int temp, temp_bound, temp_pasid;
463
464 int idx = srcu_read_lock(&kfd_processes_srcu);
465
466 hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) {
467 mutex_lock(&p->mutex);
468 pdd = kfd_get_process_device_data(dev, p);
469 temp_bound = pdd->bound;
470 temp_pasid = p->pasid;
471 if (pdd->bound == PDD_BOUND)
472 pdd->bound = PDD_BOUND_SUSPENDED;
473 mutex_unlock(&p->mutex);
474
475 if (temp_bound == PDD_BOUND)
476 amd_iommu_unbind_pasid(dev->pdev, temp_pasid);
477 }
478
479 srcu_read_unlock(&kfd_processes_srcu, idx);
480}
481
482void kfd_process_iommu_unbind_callback(struct kfd_dev *dev, unsigned int pasid)
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300483{
484 struct kfd_process *p;
485 struct kfd_process_device *pdd;
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300486
Oded Gabbay121b78e2016-05-26 08:41:08 +0300487 /*
488 * Look for the process that matches the pasid. If there is no such
489 * process, we either released it in amdkfd's own notifier, or there
490 * is a bug. Unfortunately, there is no way to tell...
491 */
Edward O'Callaghanad16a4692016-09-17 15:01:42 +1000492 p = kfd_lookup_process_by_pasid(pasid);
493 if (!p)
494 return;
Oded Gabbay121b78e2016-05-26 08:41:08 +0300495
Edward O'Callaghanad16a4692016-09-17 15:01:42 +1000496 pr_debug("Unbinding process %d from IOMMU\n", pasid);
Oded Gabbay121b78e2016-05-26 08:41:08 +0300497
Edward O'Callaghanad16a4692016-09-17 15:01:42 +1000498 if ((dev->dbgmgr) && (dev->dbgmgr->pasid == p->pasid))
499 kfd_dbgmgr_destroy(dev->dbgmgr);
Oded Gabbay121b78e2016-05-26 08:41:08 +0300500
Edward O'Callaghanad16a4692016-09-17 15:01:42 +1000501 pqm_uninit(&p->pqm);
Oded Gabbay121b78e2016-05-26 08:41:08 +0300502
Edward O'Callaghanad16a4692016-09-17 15:01:42 +1000503 pdd = kfd_get_process_device_data(dev, p);
Oded Gabbay121b78e2016-05-26 08:41:08 +0300504
Edward O'Callaghanad16a4692016-09-17 15:01:42 +1000505 if (!pdd) {
506 mutex_unlock(&p->mutex);
507 return;
508 }
Oded Gabbay121b78e2016-05-26 08:41:08 +0300509
Edward O'Callaghanad16a4692016-09-17 15:01:42 +1000510 if (pdd->reset_wavefronts) {
511 dbgdev_wave_reset_wavefronts(pdd->dev, p);
512 pdd->reset_wavefronts = false;
513 }
Oded Gabbay121b78e2016-05-26 08:41:08 +0300514
Edward O'Callaghanad16a4692016-09-17 15:01:42 +1000515 mutex_unlock(&p->mutex);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300516}
517
Kent Russell8eabaf52017-08-15 23:00:04 -0400518struct kfd_process_device *kfd_get_first_process_device_data(
519 struct kfd_process *p)
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300520{
521 return list_first_entry(&p->per_device_data,
522 struct kfd_process_device,
523 per_device_list);
524}
525
Kent Russell8eabaf52017-08-15 23:00:04 -0400526struct kfd_process_device *kfd_get_next_process_device_data(
527 struct kfd_process *p,
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300528 struct kfd_process_device *pdd)
529{
530 if (list_is_last(&pdd->per_device_list, &p->per_device_data))
531 return NULL;
532 return list_next_entry(pdd, per_device_list);
533}
534
535bool kfd_has_process_device_data(struct kfd_process *p)
536{
537 return !(list_empty(&p->per_device_data));
538}
Andrew Lewyckyf3a39812015-05-10 12:15:46 +0300539
540/* This returns with process->mutex locked. */
541struct kfd_process *kfd_lookup_process_by_pasid(unsigned int pasid)
542{
543 struct kfd_process *p;
544 unsigned int temp;
545
546 int idx = srcu_read_lock(&kfd_processes_srcu);
547
548 hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) {
549 if (p->pasid == pasid) {
550 mutex_lock(&p->mutex);
551 break;
552 }
553 }
554
555 srcu_read_unlock(&kfd_processes_srcu, idx);
556
557 return p;
558}