blob: 6e7eb76189f9958789682f6991e5c990d8d3bd17 [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);
Wei Lu4dfa5ee2018-07-11 22:32:47 -0400127 if (!process)
128 return ERR_PTR(-EINVAL);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300129
130 return process;
131}
132
133static struct kfd_process *find_process_by_mm(const struct mm_struct *mm)
134{
135 struct kfd_process *process;
136
137 hash_for_each_possible_rcu(kfd_processes_table, process,
138 kfd_processes, (uintptr_t)mm)
139 if (process->mm == mm)
140 return process;
141
142 return NULL;
143}
144
145static struct kfd_process *find_process(const struct task_struct *thread)
146{
147 struct kfd_process *p;
148 int idx;
149
150 idx = srcu_read_lock(&kfd_processes_srcu);
151 p = find_process_by_mm(thread->mm);
152 srcu_read_unlock(&kfd_processes_srcu, idx);
153
154 return p;
155}
156
157static void kfd_process_wq_release(struct work_struct *work)
158{
159 struct kfd_process_release_work *my_work;
160 struct kfd_process_device *pdd, *temp;
161 struct kfd_process *p;
162
163 my_work = (struct kfd_process_release_work *) work;
164
165 p = my_work->p;
166
Oded Gabbay94a1ee02015-02-24 10:51:59 +0200167 pr_debug("Releasing process (pasid %d) in workqueue\n",
168 p->pasid);
169
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300170 mutex_lock(&p->mutex);
171
172 list_for_each_entry_safe(pdd, temp, &p->per_device_data,
173 per_device_list) {
Oded Gabbay94a1ee02015-02-24 10:51:59 +0200174 pr_debug("Releasing pdd (topology id %d) for process (pasid %d) in workqueue\n",
175 pdd->dev->id, p->pasid);
176
Ben Goza82918f2015-03-25 13:12:20 +0200177 if (pdd->reset_wavefronts)
Ben Gozc3447e82015-05-20 18:05:44 +0300178 dbgdev_wave_reset_wavefronts(pdd->dev, p);
179
Oded Gabbayb17f0682014-07-17 00:06:27 +0300180 amd_iommu_unbind_pasid(pdd->dev->pdev, p->pasid);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300181 list_del(&pdd->per_device_list);
182
183 kfree(pdd);
184 }
185
Andrew Lewyckyf3a39812015-05-10 12:15:46 +0300186 kfd_event_free_process(p);
187
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300188 kfd_pasid_free(p->pasid);
189
190 mutex_unlock(&p->mutex);
191
192 mutex_destroy(&p->mutex);
193
194 kfree(p->queues);
195
196 kfree(p);
197
Amitoj Kaur Chawla642f0f22016-01-25 23:03:57 +0530198 kfree(work);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300199}
200
201static void kfd_process_destroy_delayed(struct rcu_head *rcu)
202{
203 struct kfd_process_release_work *work;
204 struct kfd_process *p;
205
206 BUG_ON(!kfd_process_wq);
207
208 p = container_of(rcu, struct kfd_process, rcu);
209 BUG_ON(atomic_read(&p->mm->mm_count) <= 0);
210
211 mmdrop(p->mm);
212
Firo Yang1549fcd2015-04-23 17:58:05 +0800213 work = kmalloc(sizeof(struct kfd_process_release_work), GFP_ATOMIC);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300214
215 if (work) {
216 INIT_WORK((struct work_struct *) work, kfd_process_wq_release);
217 work->p = p;
218 queue_work(kfd_process_wq, (struct work_struct *) work);
219 }
220}
221
222static void kfd_process_notifier_release(struct mmu_notifier *mn,
223 struct mm_struct *mm)
224{
225 struct kfd_process *p;
Ben Goza82918f2015-03-25 13:12:20 +0200226 struct kfd_process_device *pdd = NULL;
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300227
228 /*
229 * The kfd_process structure can not be free because the
230 * mmu_notifier srcu is read locked
231 */
232 p = container_of(mn, struct kfd_process, mmu_notifier);
233 BUG_ON(p->mm != mm);
234
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
242 /* In case our notifier is called before IOMMU notifier */
243 pqm_uninit(&p->pqm);
244
Ben Goza82918f2015-03-25 13:12:20 +0200245 /* Iterate over all process device data structure and check
Oded Gabbaybc4755a2016-05-26 08:41:48 +0300246 * if we should delete debug managers and reset all wavefronts
247 */
248 list_for_each_entry(pdd, &p->per_device_data, per_device_list) {
249 if ((pdd->dev->dbgmgr) &&
250 (pdd->dev->dbgmgr->pasid == p->pasid))
251 kfd_dbgmgr_destroy(pdd->dev->dbgmgr);
252
Ben Goza82918f2015-03-25 13:12:20 +0200253 if (pdd->reset_wavefronts) {
254 pr_warn("amdkfd: Resetting all wave fronts\n");
255 dbgdev_wave_reset_wavefronts(pdd->dev, p);
256 pdd->reset_wavefronts = false;
257 }
Oded Gabbaybc4755a2016-05-26 08:41:48 +0300258 }
Ben Goza82918f2015-03-25 13:12:20 +0200259
Ben Goz45102042014-07-17 01:04:10 +0300260 mutex_unlock(&p->mutex);
261
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300262 /*
263 * Because we drop mm_count inside kfd_process_destroy_delayed
264 * and because the mmu_notifier_unregister function also drop
265 * mm_count we need to take an extra count here.
266 */
267 atomic_inc(&p->mm->mm_count);
268 mmu_notifier_unregister_no_release(&p->mmu_notifier, p->mm);
269 mmu_notifier_call_srcu(&p->rcu, &kfd_process_destroy_delayed);
270}
271
272static const struct mmu_notifier_ops kfd_process_mmu_notifier_ops = {
273 .release = kfd_process_notifier_release,
274};
275
276static struct kfd_process *create_process(const struct task_struct *thread)
277{
278 struct kfd_process *process;
279 int err = -ENOMEM;
280
281 process = kzalloc(sizeof(*process), GFP_KERNEL);
282
283 if (!process)
284 goto err_alloc_process;
285
286 process->queues = kmalloc_array(INITIAL_QUEUE_ARRAY_SIZE,
287 sizeof(process->queues[0]), GFP_KERNEL);
288 if (!process->queues)
289 goto err_alloc_queues;
290
291 process->pasid = kfd_pasid_alloc();
292 if (process->pasid == 0)
293 goto err_alloc_pasid;
294
295 mutex_init(&process->mutex);
296
297 process->mm = thread->mm;
298
299 /* register notifier */
300 process->mmu_notifier.ops = &kfd_process_mmu_notifier_ops;
301 err = __mmu_notifier_register(&process->mmu_notifier, process->mm);
302 if (err)
303 goto err_mmu_notifier;
304
305 hash_add_rcu(kfd_processes_table, &process->kfd_processes,
306 (uintptr_t)process->mm);
307
308 process->lead_thread = thread->group_leader;
309
310 process->queue_array_size = INITIAL_QUEUE_ARRAY_SIZE;
311
312 INIT_LIST_HEAD(&process->per_device_data);
313
Andrew Lewyckyf3a39812015-05-10 12:15:46 +0300314 kfd_event_init_process(process);
315
Ben Goz45102042014-07-17 01:04:10 +0300316 err = pqm_init(&process->pqm, process);
317 if (err != 0)
318 goto err_process_pqm_init;
319
Alexey Skidanovdd592392014-11-18 13:56:23 +0200320 /* init process apertures*/
Andy Lutomirski10f16852016-03-22 14:25:19 -0700321 process->is_32bit_user_mode = in_compat_syscall();
Dan Carpenter00ceff92017-06-14 13:58:53 +0300322 err = kfd_init_apertures(process);
323 if (err != 0)
Alexey Skidanovdd592392014-11-18 13:56:23 +0200324 goto err_init_apretures;
325
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300326 return process;
327
Alexey Skidanovdd592392014-11-18 13:56:23 +0200328err_init_apretures:
329 pqm_uninit(&process->pqm);
Ben Goz45102042014-07-17 01:04:10 +0300330err_process_pqm_init:
331 hash_del_rcu(&process->kfd_processes);
332 synchronize_rcu();
333 mmu_notifier_unregister_no_release(&process->mmu_notifier, process->mm);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300334err_mmu_notifier:
Oded Gabbay7fd5e032016-06-23 17:54:29 +0300335 mutex_destroy(&process->mutex);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300336 kfd_pasid_free(process->pasid);
337err_alloc_pasid:
338 kfree(process->queues);
339err_alloc_queues:
340 kfree(process);
341err_alloc_process:
342 return ERR_PTR(err);
343}
344
345struct kfd_process_device *kfd_get_process_device_data(struct kfd_dev *dev,
Alexey Skidanov093c7d82014-11-18 14:00:04 +0200346 struct kfd_process *p)
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300347{
348 struct kfd_process_device *pdd = NULL;
349
350 list_for_each_entry(pdd, &p->per_device_data, per_device_list)
351 if (pdd->dev == dev)
Alexey Skidanov093c7d82014-11-18 14:00:04 +0200352 break;
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300353
Alexey Skidanov093c7d82014-11-18 14:00:04 +0200354 return pdd;
355}
356
357struct kfd_process_device *kfd_create_process_device_data(struct kfd_dev *dev,
358 struct kfd_process *p)
359{
360 struct kfd_process_device *pdd = NULL;
361
362 pdd = kzalloc(sizeof(*pdd), GFP_KERNEL);
363 if (pdd != NULL) {
364 pdd->dev = dev;
365 INIT_LIST_HEAD(&pdd->qpd.queues_list);
366 INIT_LIST_HEAD(&pdd->qpd.priv_queue_list);
367 pdd->qpd.dqm = dev->dqm;
Ben Goza82918f2015-03-25 13:12:20 +0200368 pdd->reset_wavefronts = false;
Alexey Skidanov093c7d82014-11-18 14:00:04 +0200369 list_add(&pdd->per_device_list, &p->per_device_data);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300370 }
371
372 return pdd;
373}
374
375/*
376 * Direct the IOMMU to bind the process (specifically the pasid->mm)
377 * to the device.
378 * Unbinding occurs when the process dies or the device is removed.
379 *
380 * Assumes that the process lock is held.
381 */
382struct kfd_process_device *kfd_bind_process_to_device(struct kfd_dev *dev,
383 struct kfd_process *p)
384{
Alexey Skidanov093c7d82014-11-18 14:00:04 +0200385 struct kfd_process_device *pdd;
Oded Gabbayb17f0682014-07-17 00:06:27 +0300386 int err;
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300387
Alexey Skidanov093c7d82014-11-18 14:00:04 +0200388 pdd = kfd_get_process_device_data(dev, p);
389 if (!pdd) {
390 pr_err("Process device data doesn't exist\n");
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300391 return ERR_PTR(-ENOMEM);
Alexey Skidanov093c7d82014-11-18 14:00:04 +0200392 }
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300393
394 if (pdd->bound)
395 return pdd;
396
Oded Gabbayb17f0682014-07-17 00:06:27 +0300397 err = amd_iommu_bind_pasid(dev->pdev, p->pasid, p->lead_thread);
398 if (err < 0)
399 return ERR_PTR(err);
400
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300401 pdd->bound = true;
402
403 return pdd;
404}
405
406void kfd_unbind_process_from_device(struct kfd_dev *dev, unsigned int pasid)
407{
408 struct kfd_process *p;
409 struct kfd_process_device *pdd;
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300410
411 BUG_ON(dev == NULL);
412
Oded Gabbay121b78e2016-05-26 08:41:08 +0300413 /*
414 * Look for the process that matches the pasid. If there is no such
415 * process, we either released it in amdkfd's own notifier, or there
416 * is a bug. Unfortunately, there is no way to tell...
417 */
Edward O'Callaghanad16a4692016-09-17 15:01:42 +1000418 p = kfd_lookup_process_by_pasid(pasid);
419 if (!p)
420 return;
Oded Gabbay121b78e2016-05-26 08:41:08 +0300421
Edward O'Callaghanad16a4692016-09-17 15:01:42 +1000422 pr_debug("Unbinding process %d from IOMMU\n", pasid);
Oded Gabbay121b78e2016-05-26 08:41:08 +0300423
Edward O'Callaghanad16a4692016-09-17 15:01:42 +1000424 if ((dev->dbgmgr) && (dev->dbgmgr->pasid == p->pasid))
425 kfd_dbgmgr_destroy(dev->dbgmgr);
Oded Gabbay121b78e2016-05-26 08:41:08 +0300426
Edward O'Callaghanad16a4692016-09-17 15:01:42 +1000427 pqm_uninit(&p->pqm);
Oded Gabbay121b78e2016-05-26 08:41:08 +0300428
Edward O'Callaghanad16a4692016-09-17 15:01:42 +1000429 pdd = kfd_get_process_device_data(dev, p);
Oded Gabbay121b78e2016-05-26 08:41:08 +0300430
Edward O'Callaghanad16a4692016-09-17 15:01:42 +1000431 if (!pdd) {
432 mutex_unlock(&p->mutex);
433 return;
434 }
Oded Gabbay121b78e2016-05-26 08:41:08 +0300435
Edward O'Callaghanad16a4692016-09-17 15:01:42 +1000436 if (pdd->reset_wavefronts) {
437 dbgdev_wave_reset_wavefronts(pdd->dev, p);
438 pdd->reset_wavefronts = false;
439 }
Oded Gabbay121b78e2016-05-26 08:41:08 +0300440
Edward O'Callaghanad16a4692016-09-17 15:01:42 +1000441 /*
442 * Just mark pdd as unbound, because we still need it
443 * to call amd_iommu_unbind_pasid() in when the
444 * process exits.
445 * We don't call amd_iommu_unbind_pasid() here
446 * because the IOMMU called us.
447 */
448 pdd->bound = false;
Oded Gabbay121b78e2016-05-26 08:41:08 +0300449
Edward O'Callaghanad16a4692016-09-17 15:01:42 +1000450 mutex_unlock(&p->mutex);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300451}
452
453struct kfd_process_device *kfd_get_first_process_device_data(struct kfd_process *p)
454{
455 return list_first_entry(&p->per_device_data,
456 struct kfd_process_device,
457 per_device_list);
458}
459
460struct kfd_process_device *kfd_get_next_process_device_data(struct kfd_process *p,
461 struct kfd_process_device *pdd)
462{
463 if (list_is_last(&pdd->per_device_list, &p->per_device_data))
464 return NULL;
465 return list_next_entry(pdd, per_device_list);
466}
467
468bool kfd_has_process_device_data(struct kfd_process *p)
469{
470 return !(list_empty(&p->per_device_data));
471}
Andrew Lewyckyf3a39812015-05-10 12:15:46 +0300472
473/* This returns with process->mutex locked. */
474struct kfd_process *kfd_lookup_process_by_pasid(unsigned int pasid)
475{
476 struct kfd_process *p;
477 unsigned int temp;
478
479 int idx = srcu_read_lock(&kfd_processes_srcu);
480
481 hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) {
482 if (p->pasid == pasid) {
483 mutex_lock(&p->mutex);
484 break;
485 }
486 }
487
488 srcu_read_unlock(&kfd_processes_srcu, idx);
489
490 return p;
491}