drm/radeon: Add ATC VMID<-->PASID functions to kfd->kgd
[deliverable/linux.git] / drivers / gpu / drm / amd / amdkfd / kfd_process.c
CommitLineData
19f6d2a6
OG
1/*
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>
b17f068a 27#include <linux/amd-iommu.h>
19f6d2a6 28#include <linux/notifier.h>
dd59239a
AS
29#include <linux/compat.h>
30
19f6d2a6
OG
31struct mm_struct;
32
33#include "kfd_priv.h"
34
35/*
36 * Initial size for the array of queues.
37 * The allocated size is doubled each time
38 * it is exceeded up to MAX_PROCESS_QUEUES.
39 */
40#define INITIAL_QUEUE_ARRAY_SIZE 16
41
42/*
43 * List of struct kfd_process (field kfd_process).
44 * Unique/indexed by mm_struct*
45 */
46#define KFD_PROCESS_TABLE_SIZE 5 /* bits: 32 entries */
47static DEFINE_HASHTABLE(kfd_processes_table, KFD_PROCESS_TABLE_SIZE);
48static DEFINE_MUTEX(kfd_processes_mutex);
49
50DEFINE_STATIC_SRCU(kfd_processes_srcu);
51
52static struct workqueue_struct *kfd_process_wq;
53
54struct kfd_process_release_work {
55 struct work_struct kfd_work;
56 struct kfd_process *p;
57};
58
59static struct kfd_process *find_process(const struct task_struct *thread);
60static struct kfd_process *create_process(const struct task_struct *thread);
61
62void kfd_process_create_wq(void)
63{
64 if (!kfd_process_wq)
65 kfd_process_wq = create_workqueue("kfd_process_wq");
66}
67
68void kfd_process_destroy_wq(void)
69{
70 if (kfd_process_wq) {
71 flush_workqueue(kfd_process_wq);
72 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
94a1ee09
OG
165 pr_debug("Releasing process (pasid %d) in workqueue\n",
166 p->pasid);
167
19f6d2a6
OG
168 mutex_lock(&p->mutex);
169
170 list_for_each_entry_safe(pdd, temp, &p->per_device_data,
171 per_device_list) {
94a1ee09
OG
172 pr_debug("Releasing pdd (topology id %d) for process (pasid %d) in workqueue\n",
173 pdd->dev->id, p->pasid);
174
b17f068a 175 amd_iommu_unbind_pasid(pdd->dev->pdev, p->pasid);
19f6d2a6
OG
176 list_del(&pdd->per_device_list);
177
178 kfree(pdd);
179 }
180
f3a39818
AL
181 kfd_event_free_process(p);
182
19f6d2a6
OG
183 kfd_pasid_free(p->pasid);
184
185 mutex_unlock(&p->mutex);
186
187 mutex_destroy(&p->mutex);
188
189 kfree(p->queues);
190
191 kfree(p);
192
193 kfree((void *)work);
194}
195
196static void kfd_process_destroy_delayed(struct rcu_head *rcu)
197{
198 struct kfd_process_release_work *work;
199 struct kfd_process *p;
200
201 BUG_ON(!kfd_process_wq);
202
203 p = container_of(rcu, struct kfd_process, rcu);
204 BUG_ON(atomic_read(&p->mm->mm_count) <= 0);
205
206 mmdrop(p->mm);
207
1549fcd1 208 work = kmalloc(sizeof(struct kfd_process_release_work), GFP_ATOMIC);
19f6d2a6
OG
209
210 if (work) {
211 INIT_WORK((struct work_struct *) work, kfd_process_wq_release);
212 work->p = p;
213 queue_work(kfd_process_wq, (struct work_struct *) work);
214 }
215}
216
217static void kfd_process_notifier_release(struct mmu_notifier *mn,
218 struct mm_struct *mm)
219{
220 struct kfd_process *p;
221
222 /*
223 * The kfd_process structure can not be free because the
224 * mmu_notifier srcu is read locked
225 */
226 p = container_of(mn, struct kfd_process, mmu_notifier);
227 BUG_ON(p->mm != mm);
228
229 mutex_lock(&kfd_processes_mutex);
230 hash_del_rcu(&p->kfd_processes);
231 mutex_unlock(&kfd_processes_mutex);
232 synchronize_srcu(&kfd_processes_srcu);
233
45102048
BG
234 mutex_lock(&p->mutex);
235
236 /* In case our notifier is called before IOMMU notifier */
237 pqm_uninit(&p->pqm);
238
239 mutex_unlock(&p->mutex);
240
19f6d2a6
OG
241 /*
242 * Because we drop mm_count inside kfd_process_destroy_delayed
243 * and because the mmu_notifier_unregister function also drop
244 * mm_count we need to take an extra count here.
245 */
246 atomic_inc(&p->mm->mm_count);
247 mmu_notifier_unregister_no_release(&p->mmu_notifier, p->mm);
248 mmu_notifier_call_srcu(&p->rcu, &kfd_process_destroy_delayed);
249}
250
251static const struct mmu_notifier_ops kfd_process_mmu_notifier_ops = {
252 .release = kfd_process_notifier_release,
253};
254
255static struct kfd_process *create_process(const struct task_struct *thread)
256{
257 struct kfd_process *process;
258 int err = -ENOMEM;
259
260 process = kzalloc(sizeof(*process), GFP_KERNEL);
261
262 if (!process)
263 goto err_alloc_process;
264
265 process->queues = kmalloc_array(INITIAL_QUEUE_ARRAY_SIZE,
266 sizeof(process->queues[0]), GFP_KERNEL);
267 if (!process->queues)
268 goto err_alloc_queues;
269
270 process->pasid = kfd_pasid_alloc();
271 if (process->pasid == 0)
272 goto err_alloc_pasid;
273
274 mutex_init(&process->mutex);
275
276 process->mm = thread->mm;
277
278 /* register notifier */
279 process->mmu_notifier.ops = &kfd_process_mmu_notifier_ops;
280 err = __mmu_notifier_register(&process->mmu_notifier, process->mm);
281 if (err)
282 goto err_mmu_notifier;
283
284 hash_add_rcu(kfd_processes_table, &process->kfd_processes,
285 (uintptr_t)process->mm);
286
287 process->lead_thread = thread->group_leader;
288
289 process->queue_array_size = INITIAL_QUEUE_ARRAY_SIZE;
290
291 INIT_LIST_HEAD(&process->per_device_data);
292
f3a39818
AL
293 kfd_event_init_process(process);
294
45102048
BG
295 err = pqm_init(&process->pqm, process);
296 if (err != 0)
297 goto err_process_pqm_init;
298
dd59239a
AS
299 /* init process apertures*/
300 process->is_32bit_user_mode = is_compat_task();
301 if (kfd_init_apertures(process) != 0)
302 goto err_init_apretures;
303
19f6d2a6
OG
304 return process;
305
dd59239a
AS
306err_init_apretures:
307 pqm_uninit(&process->pqm);
45102048
BG
308err_process_pqm_init:
309 hash_del_rcu(&process->kfd_processes);
310 synchronize_rcu();
311 mmu_notifier_unregister_no_release(&process->mmu_notifier, process->mm);
19f6d2a6
OG
312err_mmu_notifier:
313 kfd_pasid_free(process->pasid);
314err_alloc_pasid:
315 kfree(process->queues);
316err_alloc_queues:
317 kfree(process);
318err_alloc_process:
319 return ERR_PTR(err);
320}
321
322struct kfd_process_device *kfd_get_process_device_data(struct kfd_dev *dev,
093c7d8c 323 struct kfd_process *p)
19f6d2a6
OG
324{
325 struct kfd_process_device *pdd = NULL;
326
327 list_for_each_entry(pdd, &p->per_device_data, per_device_list)
328 if (pdd->dev == dev)
093c7d8c
AS
329 break;
330
331 return pdd;
332}
333
334struct kfd_process_device *kfd_create_process_device_data(struct kfd_dev *dev,
335 struct kfd_process *p)
336{
337 struct kfd_process_device *pdd = NULL;
338
339 pdd = kzalloc(sizeof(*pdd), GFP_KERNEL);
340 if (pdd != NULL) {
341 pdd->dev = dev;
342 INIT_LIST_HEAD(&pdd->qpd.queues_list);
343 INIT_LIST_HEAD(&pdd->qpd.priv_queue_list);
344 pdd->qpd.dqm = dev->dqm;
345 list_add(&pdd->per_device_list, &p->per_device_data);
19f6d2a6
OG
346 }
347
348 return pdd;
349}
350
351/*
352 * Direct the IOMMU to bind the process (specifically the pasid->mm)
353 * to the device.
354 * Unbinding occurs when the process dies or the device is removed.
355 *
356 * Assumes that the process lock is held.
357 */
358struct kfd_process_device *kfd_bind_process_to_device(struct kfd_dev *dev,
359 struct kfd_process *p)
360{
093c7d8c 361 struct kfd_process_device *pdd;
b17f068a 362 int err;
19f6d2a6 363
093c7d8c
AS
364 pdd = kfd_get_process_device_data(dev, p);
365 if (!pdd) {
366 pr_err("Process device data doesn't exist\n");
19f6d2a6 367 return ERR_PTR(-ENOMEM);
093c7d8c 368 }
19f6d2a6
OG
369
370 if (pdd->bound)
371 return pdd;
372
b17f068a
OG
373 err = amd_iommu_bind_pasid(dev->pdev, p->pasid, p->lead_thread);
374 if (err < 0)
375 return ERR_PTR(err);
376
19f6d2a6
OG
377 pdd->bound = true;
378
379 return pdd;
380}
381
382void kfd_unbind_process_from_device(struct kfd_dev *dev, unsigned int pasid)
383{
384 struct kfd_process *p;
385 struct kfd_process_device *pdd;
386 int idx, i;
387
388 BUG_ON(dev == NULL);
389
390 idx = srcu_read_lock(&kfd_processes_srcu);
391
392 hash_for_each_rcu(kfd_processes_table, i, p, kfd_processes)
393 if (p->pasid == pasid)
394 break;
395
396 srcu_read_unlock(&kfd_processes_srcu, idx);
397
398 BUG_ON(p->pasid != pasid);
399
400 mutex_lock(&p->mutex);
401
45102048
BG
402 pqm_uninit(&p->pqm);
403
093c7d8c 404 pdd = kfd_get_process_device_data(dev, p);
19f6d2a6
OG
405
406 /*
407 * Just mark pdd as unbound, because we still need it to call
408 * amd_iommu_unbind_pasid() in when the process exits.
409 * We don't call amd_iommu_unbind_pasid() here
410 * because the IOMMU called us.
411 */
412 if (pdd)
413 pdd->bound = false;
414
415 mutex_unlock(&p->mutex);
416}
417
418struct kfd_process_device *kfd_get_first_process_device_data(struct kfd_process *p)
419{
420 return list_first_entry(&p->per_device_data,
421 struct kfd_process_device,
422 per_device_list);
423}
424
425struct kfd_process_device *kfd_get_next_process_device_data(struct kfd_process *p,
426 struct kfd_process_device *pdd)
427{
428 if (list_is_last(&pdd->per_device_list, &p->per_device_data))
429 return NULL;
430 return list_next_entry(pdd, per_device_list);
431}
432
433bool kfd_has_process_device_data(struct kfd_process *p)
434{
435 return !(list_empty(&p->per_device_data));
436}
f3a39818
AL
437
438/* This returns with process->mutex locked. */
439struct kfd_process *kfd_lookup_process_by_pasid(unsigned int pasid)
440{
441 struct kfd_process *p;
442 unsigned int temp;
443
444 int idx = srcu_read_lock(&kfd_processes_srcu);
445
446 hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) {
447 if (p->pasid == pasid) {
448 mutex_lock(&p->mutex);
449 break;
450 }
451 }
452
453 srcu_read_unlock(&kfd_processes_srcu, idx);
454
455 return p;
456}
This page took 0.17148 seconds and 5 git commands to generate.