blob: 171480bb95d0ffba492298b41aa9ef4d299aa672 [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>
26#include <linux/slab.h>
Oded Gabbayb17f0682014-07-17 00:06:27 +030027#include <linux/amd-iommu.h>
Oded Gabbay19f6d2a2014-07-16 23:25:31 +030028#include <linux/notifier.h>
Alexey Skidanovdd592392014-11-18 13:56:23 +020029#include <linux/compat.h>
30
Oded Gabbay19f6d2a2014-07-16 23:25:31 +030031struct mm_struct;
32
33#include "kfd_priv.h"
Ben Gozc3447e82015-05-20 18:05:44 +030034#include "kfd_dbgmgr.h"
Oded Gabbay19f6d2a2014-07-16 23:25:31 +030035
36/*
37 * Initial size for the array of queues.
38 * The allocated size is doubled each time
39 * it is exceeded up to MAX_PROCESS_QUEUES.
40 */
41#define INITIAL_QUEUE_ARRAY_SIZE 16
42
43/*
44 * List of struct kfd_process (field kfd_process).
45 * Unique/indexed by mm_struct*
46 */
47#define KFD_PROCESS_TABLE_SIZE 5 /* bits: 32 entries */
48static DEFINE_HASHTABLE(kfd_processes_table, KFD_PROCESS_TABLE_SIZE);
49static DEFINE_MUTEX(kfd_processes_mutex);
50
51DEFINE_STATIC_SRCU(kfd_processes_srcu);
52
53static struct workqueue_struct *kfd_process_wq;
54
55struct kfd_process_release_work {
56 struct work_struct kfd_work;
57 struct kfd_process *p;
58};
59
60static struct kfd_process *find_process(const struct task_struct *thread);
61static struct kfd_process *create_process(const struct task_struct *thread);
62
63void kfd_process_create_wq(void)
64{
65 if (!kfd_process_wq)
Bhaktipriya Shridharfd320bf2016-05-29 21:14:11 +053066 kfd_process_wq = alloc_workqueue("kfd_process_wq", 0, 0);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +030067}
68
69void kfd_process_destroy_wq(void)
70{
71 if (kfd_process_wq) {
Oded Gabbay19f6d2a2014-07-16 23:25:31 +030072 destroy_workqueue(kfd_process_wq);
73 kfd_process_wq = NULL;
74 }
75}
76
77struct kfd_process *kfd_create_process(const struct task_struct *thread)
78{
79 struct kfd_process *process;
80
81 BUG_ON(!kfd_process_wq);
82
83 if (thread->mm == NULL)
84 return ERR_PTR(-EINVAL);
85
86 /* Only the pthreads threading model is supported. */
87 if (thread->group_leader->mm != thread->mm)
88 return ERR_PTR(-EINVAL);
89
90 /* Take mmap_sem because we call __mmu_notifier_register inside */
91 down_write(&thread->mm->mmap_sem);
92
93 /*
94 * take kfd processes mutex before starting of process creation
95 * so there won't be a case where two threads of the same process
96 * create two kfd_process structures
97 */
98 mutex_lock(&kfd_processes_mutex);
99
100 /* A prior open of /dev/kfd could have already created the process. */
101 process = find_process(thread);
102 if (process)
103 pr_debug("kfd: process already found\n");
104
105 if (!process)
106 process = create_process(thread);
107
108 mutex_unlock(&kfd_processes_mutex);
109
110 up_write(&thread->mm->mmap_sem);
111
112 return process;
113}
114
115struct kfd_process *kfd_get_process(const struct task_struct *thread)
116{
117 struct kfd_process *process;
118
119 if (thread->mm == NULL)
120 return ERR_PTR(-EINVAL);
121
122 /* Only the pthreads threading model is supported. */
123 if (thread->group_leader->mm != thread->mm)
124 return ERR_PTR(-EINVAL);
125
126 process = find_process(thread);
127
128 return process;
129}
130
131static struct kfd_process *find_process_by_mm(const struct mm_struct *mm)
132{
133 struct kfd_process *process;
134
135 hash_for_each_possible_rcu(kfd_processes_table, process,
136 kfd_processes, (uintptr_t)mm)
137 if (process->mm == mm)
138 return process;
139
140 return NULL;
141}
142
143static struct kfd_process *find_process(const struct task_struct *thread)
144{
145 struct kfd_process *p;
146 int idx;
147
148 idx = srcu_read_lock(&kfd_processes_srcu);
149 p = find_process_by_mm(thread->mm);
150 srcu_read_unlock(&kfd_processes_srcu, idx);
151
152 return p;
153}
154
155static void kfd_process_wq_release(struct work_struct *work)
156{
157 struct kfd_process_release_work *my_work;
158 struct kfd_process_device *pdd, *temp;
159 struct kfd_process *p;
160
161 my_work = (struct kfd_process_release_work *) work;
162
163 p = my_work->p;
164
Oded Gabbay94a1ee02015-02-24 10:51:59 +0200165 pr_debug("Releasing process (pasid %d) in workqueue\n",
166 p->pasid);
167
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300168 mutex_lock(&p->mutex);
169
170 list_for_each_entry_safe(pdd, temp, &p->per_device_data,
171 per_device_list) {
Oded Gabbay94a1ee02015-02-24 10:51:59 +0200172 pr_debug("Releasing pdd (topology id %d) for process (pasid %d) in workqueue\n",
173 pdd->dev->id, p->pasid);
174
Ben Goza82918f2015-03-25 13:12:20 +0200175 if (pdd->reset_wavefronts)
Ben Gozc3447e82015-05-20 18:05:44 +0300176 dbgdev_wave_reset_wavefronts(pdd->dev, p);
177
Oded Gabbayb17f0682014-07-17 00:06:27 +0300178 amd_iommu_unbind_pasid(pdd->dev->pdev, p->pasid);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300179 list_del(&pdd->per_device_list);
180
181 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);
187
188 mutex_unlock(&p->mutex);
189
190 mutex_destroy(&p->mutex);
191
192 kfree(p->queues);
193
194 kfree(p);
195
Amitoj Kaur Chawla642f0f22016-01-25 23:03:57 +0530196 kfree(work);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300197}
198
199static void kfd_process_destroy_delayed(struct rcu_head *rcu)
200{
201 struct kfd_process_release_work *work;
202 struct kfd_process *p;
203
204 BUG_ON(!kfd_process_wq);
205
206 p = container_of(rcu, struct kfd_process, rcu);
207 BUG_ON(atomic_read(&p->mm->mm_count) <= 0);
208
209 mmdrop(p->mm);
210
Firo Yang1549fcd2015-04-23 17:58:05 +0800211 work = kmalloc(sizeof(struct kfd_process_release_work), GFP_ATOMIC);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300212
213 if (work) {
214 INIT_WORK((struct work_struct *) work, kfd_process_wq_release);
215 work->p = p;
216 queue_work(kfd_process_wq, (struct work_struct *) work);
217 }
218}
219
220static void kfd_process_notifier_release(struct mmu_notifier *mn,
221 struct mm_struct *mm)
222{
223 struct kfd_process *p;
Ben Goza82918f2015-03-25 13:12:20 +0200224 struct kfd_process_device *pdd = NULL;
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300225
226 /*
227 * The kfd_process structure can not be free because the
228 * mmu_notifier srcu is read locked
229 */
230 p = container_of(mn, struct kfd_process, mmu_notifier);
231 BUG_ON(p->mm != mm);
232
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) {
252 pr_warn("amdkfd: Resetting all wave fronts\n");
253 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 */
265 atomic_inc(&p->mm->mm_count);
266 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
293 mutex_init(&process->mutex);
294
295 process->mm = thread->mm;
296
297 /* register notifier */
298 process->mmu_notifier.ops = &kfd_process_mmu_notifier_ops;
299 err = __mmu_notifier_register(&process->mmu_notifier, process->mm);
300 if (err)
301 goto err_mmu_notifier;
302
303 hash_add_rcu(kfd_processes_table, &process->kfd_processes,
304 (uintptr_t)process->mm);
305
306 process->lead_thread = thread->group_leader;
307
308 process->queue_array_size = INITIAL_QUEUE_ARRAY_SIZE;
309
310 INIT_LIST_HEAD(&process->per_device_data);
311
Andrew Lewyckyf3a39812015-05-10 12:15:46 +0300312 kfd_event_init_process(process);
313
Ben Goz45102042014-07-17 01:04:10 +0300314 err = pqm_init(&process->pqm, process);
315 if (err != 0)
316 goto err_process_pqm_init;
317
Alexey Skidanovdd592392014-11-18 13:56:23 +0200318 /* init process apertures*/
Andy Lutomirski10f16852016-03-22 14:25:19 -0700319 process->is_32bit_user_mode = in_compat_syscall();
Dan Carpenter00ceff92017-06-14 13:58:53 +0300320 err = kfd_init_apertures(process);
321 if (err != 0)
Alexey Skidanovdd592392014-11-18 13:56:23 +0200322 goto err_init_apretures;
323
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300324 return process;
325
Alexey Skidanovdd592392014-11-18 13:56:23 +0200326err_init_apretures:
327 pqm_uninit(&process->pqm);
Ben Goz45102042014-07-17 01:04:10 +0300328err_process_pqm_init:
329 hash_del_rcu(&process->kfd_processes);
330 synchronize_rcu();
331 mmu_notifier_unregister_no_release(&process->mmu_notifier, process->mm);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300332err_mmu_notifier:
Oded Gabbay7fd5e032016-06-23 17:54:29 +0300333 mutex_destroy(&process->mutex);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300334 kfd_pasid_free(process->pasid);
335err_alloc_pasid:
336 kfree(process->queues);
337err_alloc_queues:
338 kfree(process);
339err_alloc_process:
340 return ERR_PTR(err);
341}
342
343struct kfd_process_device *kfd_get_process_device_data(struct kfd_dev *dev,
Alexey Skidanov093c7d82014-11-18 14:00:04 +0200344 struct kfd_process *p)
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300345{
346 struct kfd_process_device *pdd = NULL;
347
348 list_for_each_entry(pdd, &p->per_device_data, per_device_list)
349 if (pdd->dev == dev)
Alexey Skidanov093c7d82014-11-18 14:00:04 +0200350 break;
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300351
Alexey Skidanov093c7d82014-11-18 14:00:04 +0200352 return pdd;
353}
354
355struct kfd_process_device *kfd_create_process_device_data(struct kfd_dev *dev,
356 struct kfd_process *p)
357{
358 struct kfd_process_device *pdd = NULL;
359
360 pdd = kzalloc(sizeof(*pdd), GFP_KERNEL);
361 if (pdd != NULL) {
362 pdd->dev = dev;
363 INIT_LIST_HEAD(&pdd->qpd.queues_list);
364 INIT_LIST_HEAD(&pdd->qpd.priv_queue_list);
365 pdd->qpd.dqm = dev->dqm;
Ben Goza82918f2015-03-25 13:12:20 +0200366 pdd->reset_wavefronts = false;
Alexey Skidanov093c7d82014-11-18 14:00:04 +0200367 list_add(&pdd->per_device_list, &p->per_device_data);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300368 }
369
370 return pdd;
371}
372
373/*
374 * Direct the IOMMU to bind the process (specifically the pasid->mm)
375 * to the device.
376 * Unbinding occurs when the process dies or the device is removed.
377 *
378 * Assumes that the process lock is held.
379 */
380struct kfd_process_device *kfd_bind_process_to_device(struct kfd_dev *dev,
381 struct kfd_process *p)
382{
Alexey Skidanov093c7d82014-11-18 14:00:04 +0200383 struct kfd_process_device *pdd;
Oded Gabbayb17f0682014-07-17 00:06:27 +0300384 int err;
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300385
Alexey Skidanov093c7d82014-11-18 14:00:04 +0200386 pdd = kfd_get_process_device_data(dev, p);
387 if (!pdd) {
388 pr_err("Process device data doesn't exist\n");
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300389 return ERR_PTR(-ENOMEM);
Alexey Skidanov093c7d82014-11-18 14:00:04 +0200390 }
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300391
392 if (pdd->bound)
393 return pdd;
394
Oded Gabbayb17f0682014-07-17 00:06:27 +0300395 err = amd_iommu_bind_pasid(dev->pdev, p->pasid, p->lead_thread);
396 if (err < 0)
397 return ERR_PTR(err);
398
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300399 pdd->bound = true;
400
401 return pdd;
402}
403
404void kfd_unbind_process_from_device(struct kfd_dev *dev, unsigned int pasid)
405{
406 struct kfd_process *p;
407 struct kfd_process_device *pdd;
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300408
409 BUG_ON(dev == NULL);
410
Oded Gabbay121b78e2016-05-26 08:41:08 +0300411 /*
412 * Look for the process that matches the pasid. If there is no such
413 * process, we either released it in amdkfd's own notifier, or there
414 * is a bug. Unfortunately, there is no way to tell...
415 */
Edward O'Callaghanad16a4692016-09-17 15:01:42 +1000416 p = kfd_lookup_process_by_pasid(pasid);
417 if (!p)
418 return;
Oded Gabbay121b78e2016-05-26 08:41:08 +0300419
Edward O'Callaghanad16a4692016-09-17 15:01:42 +1000420 pr_debug("Unbinding process %d from IOMMU\n", pasid);
Oded Gabbay121b78e2016-05-26 08:41:08 +0300421
Edward O'Callaghanad16a4692016-09-17 15:01:42 +1000422 if ((dev->dbgmgr) && (dev->dbgmgr->pasid == p->pasid))
423 kfd_dbgmgr_destroy(dev->dbgmgr);
Oded Gabbay121b78e2016-05-26 08:41:08 +0300424
Edward O'Callaghanad16a4692016-09-17 15:01:42 +1000425 pqm_uninit(&p->pqm);
Oded Gabbay121b78e2016-05-26 08:41:08 +0300426
Edward O'Callaghanad16a4692016-09-17 15:01:42 +1000427 pdd = kfd_get_process_device_data(dev, p);
Oded Gabbay121b78e2016-05-26 08:41:08 +0300428
Edward O'Callaghanad16a4692016-09-17 15:01:42 +1000429 if (!pdd) {
430 mutex_unlock(&p->mutex);
431 return;
432 }
Oded Gabbay121b78e2016-05-26 08:41:08 +0300433
Edward O'Callaghanad16a4692016-09-17 15:01:42 +1000434 if (pdd->reset_wavefronts) {
435 dbgdev_wave_reset_wavefronts(pdd->dev, p);
436 pdd->reset_wavefronts = false;
437 }
Oded Gabbay121b78e2016-05-26 08:41:08 +0300438
Edward O'Callaghanad16a4692016-09-17 15:01:42 +1000439 /*
440 * Just mark pdd as unbound, because we still need it
441 * to call amd_iommu_unbind_pasid() in when the
442 * process exits.
443 * We don't call amd_iommu_unbind_pasid() here
444 * because the IOMMU called us.
445 */
446 pdd->bound = false;
Oded Gabbay121b78e2016-05-26 08:41:08 +0300447
Edward O'Callaghanad16a4692016-09-17 15:01:42 +1000448 mutex_unlock(&p->mutex);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300449}
450
451struct kfd_process_device *kfd_get_first_process_device_data(struct kfd_process *p)
452{
453 return list_first_entry(&p->per_device_data,
454 struct kfd_process_device,
455 per_device_list);
456}
457
458struct kfd_process_device *kfd_get_next_process_device_data(struct kfd_process *p,
459 struct kfd_process_device *pdd)
460{
461 if (list_is_last(&pdd->per_device_list, &p->per_device_data))
462 return NULL;
463 return list_next_entry(pdd, per_device_list);
464}
465
466bool kfd_has_process_device_data(struct kfd_process *p)
467{
468 return !(list_empty(&p->per_device_data));
469}
Andrew Lewyckyf3a39812015-05-10 12:15:46 +0300470
471/* This returns with process->mutex locked. */
472struct kfd_process *kfd_lookup_process_by_pasid(unsigned int pasid)
473{
474 struct kfd_process *p;
475 unsigned int temp;
476
477 int idx = srcu_read_lock(&kfd_processes_srcu);
478
479 hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) {
480 if (p->pasid == pasid) {
481 mutex_lock(&p->mutex);
482 break;
483 }
484 }
485
486 srcu_read_unlock(&kfd_processes_srcu, idx);
487
488 return p;
489}