blob: cf4fa25cc43048070f37d7ecf86e1a6ed24f11cf [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"
Felix Kuehling403575c2018-02-06 20:32:44 -050037#include "kfd_device_queue_manager.h"
Ben Gozc3447e82015-05-20 18:05:44 +030038#include "kfd_dbgmgr.h"
Felix Kuehling64d1c3a2017-12-08 19:22:12 -050039#include "kfd_iommu.h"
Oded Gabbay19f6d2a2014-07-16 23:25:31 +030040
41/*
Oded Gabbay19f6d2a2014-07-16 23:25:31 +030042 * List of struct kfd_process (field kfd_process).
43 * Unique/indexed by mm_struct*
44 */
Felix Kuehling64d1c3a2017-12-08 19:22:12 -050045DEFINE_HASHTABLE(kfd_processes_table, KFD_PROCESS_TABLE_SIZE);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +030046static DEFINE_MUTEX(kfd_processes_mutex);
47
Felix Kuehling64d1c3a2017-12-08 19:22:12 -050048DEFINE_SRCU(kfd_processes_srcu);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +030049
50static struct workqueue_struct *kfd_process_wq;
51
Oded Gabbay19f6d2a2014-07-16 23:25:31 +030052static struct kfd_process *find_process(const struct task_struct *thread);
Felix Kuehlingabb208a2017-11-27 18:29:52 -050053static void kfd_process_ref_release(struct kref *ref);
Yong Zhaoc0ede1f2017-11-27 18:29:56 -050054static struct kfd_process *create_process(const struct task_struct *thread,
55 struct file *filep);
Felix Kuehling373d7082017-11-14 16:41:19 -050056static int kfd_process_init_cwsr(struct kfd_process *p, struct file *filep);
57
Oded Gabbay19f6d2a2014-07-16 23:25:31 +030058
59void kfd_process_create_wq(void)
60{
61 if (!kfd_process_wq)
Bhaktipriya Shridharfd320bf2016-05-29 21:14:11 +053062 kfd_process_wq = alloc_workqueue("kfd_process_wq", 0, 0);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +030063}
64
65void kfd_process_destroy_wq(void)
66{
67 if (kfd_process_wq) {
Oded Gabbay19f6d2a2014-07-16 23:25:31 +030068 destroy_workqueue(kfd_process_wq);
69 kfd_process_wq = NULL;
70 }
71}
72
Felix Kuehling373d7082017-11-14 16:41:19 -050073struct kfd_process *kfd_create_process(struct file *filep)
Oded Gabbay19f6d2a2014-07-16 23:25:31 +030074{
75 struct kfd_process *process;
Felix Kuehling373d7082017-11-14 16:41:19 -050076 struct task_struct *thread = current;
Oded Gabbay19f6d2a2014-07-16 23:25:31 +030077
Kent Russell4eacc26b2017-08-15 23:00:06 -040078 if (!thread->mm)
Oded Gabbay19f6d2a2014-07-16 23:25:31 +030079 return ERR_PTR(-EINVAL);
80
81 /* Only the pthreads threading model is supported. */
82 if (thread->group_leader->mm != thread->mm)
83 return ERR_PTR(-EINVAL);
84
Oded Gabbay19f6d2a2014-07-16 23:25:31 +030085 /*
86 * take kfd processes mutex before starting of process creation
87 * so there won't be a case where two threads of the same process
88 * create two kfd_process structures
89 */
90 mutex_lock(&kfd_processes_mutex);
91
92 /* A prior open of /dev/kfd could have already created the process. */
93 process = find_process(thread);
94 if (process)
Kent Russell79775b62017-08-15 23:00:05 -040095 pr_debug("Process already found\n");
Yong Zhaoc0ede1f2017-11-27 18:29:56 -050096 else
97 process = create_process(thread, filep);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +030098
99 mutex_unlock(&kfd_processes_mutex);
100
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300101 return process;
102}
103
104struct kfd_process *kfd_get_process(const struct task_struct *thread)
105{
106 struct kfd_process *process;
107
Kent Russell4eacc26b2017-08-15 23:00:06 -0400108 if (!thread->mm)
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300109 return ERR_PTR(-EINVAL);
110
111 /* Only the pthreads threading model is supported. */
112 if (thread->group_leader->mm != thread->mm)
113 return ERR_PTR(-EINVAL);
114
115 process = find_process(thread);
116
117 return process;
118}
119
120static struct kfd_process *find_process_by_mm(const struct mm_struct *mm)
121{
122 struct kfd_process *process;
123
124 hash_for_each_possible_rcu(kfd_processes_table, process,
125 kfd_processes, (uintptr_t)mm)
126 if (process->mm == mm)
127 return process;
128
129 return NULL;
130}
131
132static struct kfd_process *find_process(const struct task_struct *thread)
133{
134 struct kfd_process *p;
135 int idx;
136
137 idx = srcu_read_lock(&kfd_processes_srcu);
138 p = find_process_by_mm(thread->mm);
139 srcu_read_unlock(&kfd_processes_srcu, idx);
140
141 return p;
142}
143
Felix Kuehlingabb208a2017-11-27 18:29:52 -0500144void kfd_unref_process(struct kfd_process *p)
145{
146 kref_put(&p->ref, kfd_process_ref_release);
147}
148
Felix Kuehlingde1450a2017-11-27 18:29:55 -0500149static void kfd_process_destroy_pdds(struct kfd_process *p)
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300150{
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300151 struct kfd_process_device *pdd, *temp;
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300152
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300153 list_for_each_entry_safe(pdd, temp, &p->per_device_data,
Felix Kuehlingde1450a2017-11-27 18:29:55 -0500154 per_device_list) {
155 pr_debug("Releasing pdd (topology id %d) for process (pasid %d)\n",
Oded Gabbay94a1ee02015-02-24 10:51:59 +0200156 pdd->dev->id, p->pasid);
157
Felix Kuehling403575c2018-02-06 20:32:44 -0500158 if (pdd->vm)
159 pdd->dev->kfd2kgd->destroy_process_vm(
160 pdd->dev->kgd, pdd->vm);
161
Yong Zhao733fa1f2017-09-20 18:10:14 -0400162 list_del(&pdd->per_device_list);
Felix Kuehling373d7082017-11-14 16:41:19 -0500163
164 if (pdd->qpd.cwsr_kaddr)
165 free_pages((unsigned long)pdd->qpd.cwsr_kaddr,
166 get_order(KFD_CWSR_TBA_TMA_SIZE));
167
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300168 kfree(pdd);
169 }
Felix Kuehlingde1450a2017-11-27 18:29:55 -0500170}
171
172/* No process locking is needed in this function, because the process
173 * is not findable any more. We must assume that no other thread is
174 * using it any more, otherwise we couldn't safely free the process
175 * structure in the end.
176 */
177static void kfd_process_wq_release(struct work_struct *work)
178{
179 struct kfd_process *p = container_of(work, struct kfd_process,
180 release_work);
Felix Kuehlingde1450a2017-11-27 18:29:55 -0500181
Felix Kuehling64d1c3a2017-12-08 19:22:12 -0500182 kfd_iommu_unbind_process(p);
Felix Kuehlingde1450a2017-11-27 18:29:55 -0500183
184 kfd_process_destroy_pdds(p);
Felix Kuehling403575c2018-02-06 20:32:44 -0500185 dma_fence_put(p->ef);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300186
Andrew Lewyckyf3a39812015-05-10 12:15:46 +0300187 kfd_event_free_process(p);
188
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300189 kfd_pasid_free(p->pasid);
Felix Kuehlinga91e70e2017-08-26 02:00:57 -0400190 kfd_free_process_doorbells(p);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300191
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300192 mutex_destroy(&p->mutex);
193
Felix Kuehlingc7b12432017-11-27 18:29:50 -0500194 put_task_struct(p->lead_thread);
195
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300196 kfree(p);
Felix Kuehling5ce10682017-11-27 18:29:51 -0500197}
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300198
Felix Kuehling5ce10682017-11-27 18:29:51 -0500199static void kfd_process_ref_release(struct kref *ref)
200{
201 struct kfd_process *p = container_of(ref, struct kfd_process, ref);
202
203 INIT_WORK(&p->release_work, kfd_process_wq_release);
204 queue_work(kfd_process_wq, &p->release_work);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300205}
206
207static void kfd_process_destroy_delayed(struct rcu_head *rcu)
208{
Felix Kuehling5ce10682017-11-27 18:29:51 -0500209 struct kfd_process *p = container_of(rcu, struct kfd_process, rcu);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300210
Felix Kuehlingabb208a2017-11-27 18:29:52 -0500211 kfd_unref_process(p);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300212}
213
214static void kfd_process_notifier_release(struct mmu_notifier *mn,
215 struct mm_struct *mm)
216{
217 struct kfd_process *p;
Ben Goza82918f2015-03-25 13:12:20 +0200218 struct kfd_process_device *pdd = NULL;
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300219
220 /*
221 * The kfd_process structure can not be free because the
222 * mmu_notifier srcu is read locked
223 */
224 p = container_of(mn, struct kfd_process, mmu_notifier);
Felix Kuehling32fa8212017-08-15 23:00:12 -0400225 if (WARN_ON(p->mm != mm))
226 return;
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300227
228 mutex_lock(&kfd_processes_mutex);
229 hash_del_rcu(&p->kfd_processes);
230 mutex_unlock(&kfd_processes_mutex);
231 synchronize_srcu(&kfd_processes_srcu);
232
Ben Goz45102042014-07-17 01:04:10 +0300233 mutex_lock(&p->mutex);
234
Yair Shachar062c5672017-11-01 19:21:29 -0400235 /* Iterate over all process device data structures and if the
236 * pdd is in debug mode, we should first force unregistration,
237 * then we will be able to destroy the queues
238 */
239 list_for_each_entry(pdd, &p->per_device_data, per_device_list) {
240 struct kfd_dev *dev = pdd->dev;
241
242 mutex_lock(kfd_get_dbgmgr_mutex());
243 if (dev && dev->dbgmgr && dev->dbgmgr->pasid == p->pasid) {
244 if (!kfd_dbgmgr_unregister(dev->dbgmgr, p)) {
245 kfd_dbgmgr_destroy(dev->dbgmgr);
246 dev->dbgmgr = NULL;
247 }
248 }
249 mutex_unlock(kfd_get_dbgmgr_mutex());
250 }
251
Felix Kuehling9fd3f1bf2017-09-27 00:09:52 -0400252 kfd_process_dequeue_from_all_devices(p);
Ben Goz45102042014-07-17 01:04:10 +0300253 pqm_uninit(&p->pqm);
254
Felix Kuehling5ce10682017-11-27 18:29:51 -0500255 /* Indicate to other users that MM is no longer valid */
256 p->mm = NULL;
257
Ben Goz45102042014-07-17 01:04:10 +0300258 mutex_unlock(&p->mutex);
259
Felix Kuehling5ce10682017-11-27 18:29:51 -0500260 mmu_notifier_unregister_no_release(&p->mmu_notifier, mm);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300261 mmu_notifier_call_srcu(&p->rcu, &kfd_process_destroy_delayed);
262}
263
264static const struct mmu_notifier_ops kfd_process_mmu_notifier_ops = {
265 .release = kfd_process_notifier_release,
266};
267
Felix Kuehling373d7082017-11-14 16:41:19 -0500268static int kfd_process_init_cwsr(struct kfd_process *p, struct file *filep)
269{
Felix Kuehling373d7082017-11-14 16:41:19 -0500270 unsigned long offset;
Yong Zhaoc0ede1f2017-11-27 18:29:56 -0500271 struct kfd_process_device *pdd = NULL;
Felix Kuehling373d7082017-11-14 16:41:19 -0500272 struct kfd_dev *dev = NULL;
273 struct qcm_process_device *qpd = NULL;
274
Yong Zhaoc0ede1f2017-11-27 18:29:56 -0500275 list_for_each_entry(pdd, &p->per_device_data, per_device_list) {
Felix Kuehling373d7082017-11-14 16:41:19 -0500276 dev = pdd->dev;
277 qpd = &pdd->qpd;
278 if (!dev->cwsr_enabled || qpd->cwsr_kaddr)
279 continue;
280 offset = (dev->id | KFD_MMAP_RESERVED_MEM_MASK) << PAGE_SHIFT;
281 qpd->tba_addr = (int64_t)vm_mmap(filep, 0,
282 KFD_CWSR_TBA_TMA_SIZE, PROT_READ | PROT_EXEC,
283 MAP_SHARED, offset);
284
285 if (IS_ERR_VALUE(qpd->tba_addr)) {
Yong Zhaoc0ede1f2017-11-27 18:29:56 -0500286 int err = qpd->tba_addr;
287
288 pr_err("Failure to set tba address. error %d.\n", err);
Felix Kuehling373d7082017-11-14 16:41:19 -0500289 qpd->tba_addr = 0;
290 qpd->cwsr_kaddr = NULL;
Yong Zhaoc0ede1f2017-11-27 18:29:56 -0500291 return err;
Felix Kuehling373d7082017-11-14 16:41:19 -0500292 }
293
294 memcpy(qpd->cwsr_kaddr, dev->cwsr_isa, dev->cwsr_isa_size);
295
296 qpd->tma_addr = qpd->tba_addr + KFD_CWSR_TMA_OFFSET;
297 pr_debug("set tba :0x%llx, tma:0x%llx, cwsr_kaddr:%p for pqm.\n",
298 qpd->tba_addr, qpd->tma_addr, qpd->cwsr_kaddr);
299 }
Yong Zhaoc0ede1f2017-11-27 18:29:56 -0500300
301 return 0;
Felix Kuehling373d7082017-11-14 16:41:19 -0500302}
303
Yong Zhaoc0ede1f2017-11-27 18:29:56 -0500304static struct kfd_process *create_process(const struct task_struct *thread,
305 struct file *filep)
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300306{
307 struct kfd_process *process;
308 int err = -ENOMEM;
309
310 process = kzalloc(sizeof(*process), GFP_KERNEL);
311
312 if (!process)
313 goto err_alloc_process;
314
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300315 process->pasid = kfd_pasid_alloc();
316 if (process->pasid == 0)
317 goto err_alloc_pasid;
318
Felix Kuehlinga91e70e2017-08-26 02:00:57 -0400319 if (kfd_alloc_process_doorbells(process) < 0)
320 goto err_alloc_doorbells;
321
Felix Kuehling5ce10682017-11-27 18:29:51 -0500322 kref_init(&process->ref);
323
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300324 mutex_init(&process->mutex);
325
326 process->mm = thread->mm;
327
328 /* register notifier */
329 process->mmu_notifier.ops = &kfd_process_mmu_notifier_ops;
Yong Zhaoc0ede1f2017-11-27 18:29:56 -0500330 err = mmu_notifier_register(&process->mmu_notifier, process->mm);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300331 if (err)
332 goto err_mmu_notifier;
333
334 hash_add_rcu(kfd_processes_table, &process->kfd_processes,
335 (uintptr_t)process->mm);
336
337 process->lead_thread = thread->group_leader;
Felix Kuehlingc7b12432017-11-27 18:29:50 -0500338 get_task_struct(process->lead_thread);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300339
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300340 INIT_LIST_HEAD(&process->per_device_data);
341
Andrew Lewyckyf3a39812015-05-10 12:15:46 +0300342 kfd_event_init_process(process);
343
Ben Goz45102042014-07-17 01:04:10 +0300344 err = pqm_init(&process->pqm, process);
345 if (err != 0)
346 goto err_process_pqm_init;
347
Alexey Skidanovdd592392014-11-18 13:56:23 +0200348 /* init process apertures*/
Andy Lutomirski10f16852016-03-22 14:25:19 -0700349 process->is_32bit_user_mode = in_compat_syscall();
Dan Carpenterb312b2b2017-06-14 13:58:53 +0300350 err = kfd_init_apertures(process);
351 if (err != 0)
Geert Uytterhoeven7a10d632017-06-01 12:28:38 +0200352 goto err_init_apertures;
Alexey Skidanovdd592392014-11-18 13:56:23 +0200353
Yong Zhaoc0ede1f2017-11-27 18:29:56 -0500354 err = kfd_process_init_cwsr(process, filep);
355 if (err)
356 goto err_init_cwsr;
357
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300358 return process;
359
Yong Zhaoc0ede1f2017-11-27 18:29:56 -0500360err_init_cwsr:
361 kfd_process_destroy_pdds(process);
Geert Uytterhoeven7a10d632017-06-01 12:28:38 +0200362err_init_apertures:
Alexey Skidanovdd592392014-11-18 13:56:23 +0200363 pqm_uninit(&process->pqm);
Ben Goz45102042014-07-17 01:04:10 +0300364err_process_pqm_init:
365 hash_del_rcu(&process->kfd_processes);
366 synchronize_rcu();
367 mmu_notifier_unregister_no_release(&process->mmu_notifier, process->mm);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300368err_mmu_notifier:
Oded Gabbay7fd5e032016-06-23 17:54:29 +0300369 mutex_destroy(&process->mutex);
Felix Kuehlinga91e70e2017-08-26 02:00:57 -0400370 kfd_free_process_doorbells(process);
371err_alloc_doorbells:
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300372 kfd_pasid_free(process->pasid);
373err_alloc_pasid:
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300374 kfree(process);
375err_alloc_process:
376 return ERR_PTR(err);
377}
378
379struct kfd_process_device *kfd_get_process_device_data(struct kfd_dev *dev,
Alexey Skidanov093c7d82014-11-18 14:00:04 +0200380 struct kfd_process *p)
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300381{
382 struct kfd_process_device *pdd = NULL;
383
384 list_for_each_entry(pdd, &p->per_device_data, per_device_list)
385 if (pdd->dev == dev)
Yong Zhao733fa1f2017-09-20 18:10:14 -0400386 return pdd;
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300387
Yong Zhao733fa1f2017-09-20 18:10:14 -0400388 return NULL;
Alexey Skidanov093c7d82014-11-18 14:00:04 +0200389}
390
391struct kfd_process_device *kfd_create_process_device_data(struct kfd_dev *dev,
392 struct kfd_process *p)
393{
394 struct kfd_process_device *pdd = NULL;
395
396 pdd = kzalloc(sizeof(*pdd), GFP_KERNEL);
Felix Kuehling2d9b36f2017-11-27 18:29:54 -0500397 if (!pdd)
398 return NULL;
399
400 pdd->dev = dev;
401 INIT_LIST_HEAD(&pdd->qpd.queues_list);
402 INIT_LIST_HEAD(&pdd->qpd.priv_queue_list);
403 pdd->qpd.dqm = dev->dqm;
404 pdd->qpd.pqm = &p->pqm;
405 pdd->process = p;
406 pdd->bound = PDD_UNBOUND;
407 pdd->already_dequeued = false;
408 list_add(&pdd->per_device_list, &p->per_device_data);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300409
Felix Kuehling403575c2018-02-06 20:32:44 -0500410 /* Create the GPUVM context for this specific device */
411 if (dev->kfd2kgd->create_process_vm(dev->kgd, &pdd->vm,
412 &p->kgd_process_info, &p->ef)) {
413 pr_err("Failed to create process VM object\n");
414 goto err_create_pdd;
415 }
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300416 return pdd;
Felix Kuehling403575c2018-02-06 20:32:44 -0500417
418err_create_pdd:
419 list_del(&pdd->per_device_list);
420 kfree(pdd);
421 return NULL;
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300422}
423
424/*
425 * Direct the IOMMU to bind the process (specifically the pasid->mm)
426 * to the device.
427 * Unbinding occurs when the process dies or the device is removed.
428 *
429 * Assumes that the process lock is held.
430 */
431struct kfd_process_device *kfd_bind_process_to_device(struct kfd_dev *dev,
432 struct kfd_process *p)
433{
Alexey Skidanov093c7d82014-11-18 14:00:04 +0200434 struct kfd_process_device *pdd;
Oded Gabbayb17f0682014-07-17 00:06:27 +0300435 int err;
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300436
Alexey Skidanov093c7d82014-11-18 14:00:04 +0200437 pdd = kfd_get_process_device_data(dev, p);
438 if (!pdd) {
439 pr_err("Process device data doesn't exist\n");
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300440 return ERR_PTR(-ENOMEM);
Alexey Skidanov093c7d82014-11-18 14:00:04 +0200441 }
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300442
Felix Kuehling64d1c3a2017-12-08 19:22:12 -0500443 err = kfd_iommu_bind_process_to_device(pdd);
444 if (err)
Oded Gabbayb17f0682014-07-17 00:06:27 +0300445 return ERR_PTR(err);
446
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300447 return pdd;
448}
449
Kent Russell8eabaf52017-08-15 23:00:04 -0400450struct kfd_process_device *kfd_get_first_process_device_data(
451 struct kfd_process *p)
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300452{
453 return list_first_entry(&p->per_device_data,
454 struct kfd_process_device,
455 per_device_list);
456}
457
Kent Russell8eabaf52017-08-15 23:00:04 -0400458struct kfd_process_device *kfd_get_next_process_device_data(
459 struct kfd_process *p,
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300460 struct kfd_process_device *pdd)
461{
462 if (list_is_last(&pdd->per_device_list, &p->per_device_data))
463 return NULL;
464 return list_next_entry(pdd, per_device_list);
465}
466
467bool kfd_has_process_device_data(struct kfd_process *p)
468{
469 return !(list_empty(&p->per_device_data));
470}
Andrew Lewyckyf3a39812015-05-10 12:15:46 +0300471
Felix Kuehlingabb208a2017-11-27 18:29:52 -0500472/* This increments the process->ref counter. */
Andrew Lewyckyf3a39812015-05-10 12:15:46 +0300473struct kfd_process *kfd_lookup_process_by_pasid(unsigned int pasid)
474{
Yong Zhao82c16b42017-11-27 18:29:53 -0500475 struct kfd_process *p, *ret_p = NULL;
Andrew Lewyckyf3a39812015-05-10 12:15:46 +0300476 unsigned int temp;
477
478 int idx = srcu_read_lock(&kfd_processes_srcu);
479
480 hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) {
481 if (p->pasid == pasid) {
Felix Kuehlingabb208a2017-11-27 18:29:52 -0500482 kref_get(&p->ref);
Yong Zhao82c16b42017-11-27 18:29:53 -0500483 ret_p = p;
Andrew Lewyckyf3a39812015-05-10 12:15:46 +0300484 break;
485 }
486 }
487
488 srcu_read_unlock(&kfd_processes_srcu, idx);
489
Yong Zhao82c16b42017-11-27 18:29:53 -0500490 return ret_p;
Andrew Lewyckyf3a39812015-05-10 12:15:46 +0300491}
Felix Kuehling373d7082017-11-14 16:41:19 -0500492
493int kfd_reserved_mem_mmap(struct kfd_process *process,
494 struct vm_area_struct *vma)
495{
496 struct kfd_dev *dev = kfd_device_by_id(vma->vm_pgoff);
497 struct kfd_process_device *pdd;
498 struct qcm_process_device *qpd;
499
500 if (!dev)
501 return -EINVAL;
502 if ((vma->vm_end - vma->vm_start) != KFD_CWSR_TBA_TMA_SIZE) {
503 pr_err("Incorrect CWSR mapping size.\n");
504 return -EINVAL;
505 }
506
507 pdd = kfd_get_process_device_data(dev, process);
508 if (!pdd)
509 return -EINVAL;
510 qpd = &pdd->qpd;
511
512 qpd->cwsr_kaddr = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
513 get_order(KFD_CWSR_TBA_TMA_SIZE));
514 if (!qpd->cwsr_kaddr) {
515 pr_err("Error allocating per process CWSR buffer.\n");
516 return -ENOMEM;
517 }
518
519 vma->vm_flags |= VM_IO | VM_DONTCOPY | VM_DONTEXPAND
520 | VM_NORESERVE | VM_DONTDUMP | VM_PFNMAP;
521 /* Mapping pages to user process */
522 return remap_pfn_range(vma, vma->vm_start,
523 PFN_DOWN(__pa(qpd->cwsr_kaddr)),
524 KFD_CWSR_TBA_TMA_SIZE, vma->vm_page_prot);
525}
Felix Kuehling851a6452017-11-27 18:29:49 -0500526
Felix Kuehling403575c2018-02-06 20:32:44 -0500527void kfd_flush_tlb(struct kfd_process_device *pdd)
528{
529 struct kfd_dev *dev = pdd->dev;
530 const struct kfd2kgd_calls *f2g = dev->kfd2kgd;
531
532 if (dev->dqm->sched_policy == KFD_SCHED_POLICY_NO_HWS) {
533 /* Nothing to flush until a VMID is assigned, which
534 * only happens when the first queue is created.
535 */
536 if (pdd->qpd.vmid)
537 f2g->invalidate_tlbs_vmid(dev->kgd, pdd->qpd.vmid);
538 } else {
539 f2g->invalidate_tlbs(dev->kgd, pdd->process->pasid);
540 }
541}
542
Felix Kuehling851a6452017-11-27 18:29:49 -0500543#if defined(CONFIG_DEBUG_FS)
544
545int kfd_debugfs_mqds_by_process(struct seq_file *m, void *data)
546{
547 struct kfd_process *p;
548 unsigned int temp;
549 int r = 0;
550
551 int idx = srcu_read_lock(&kfd_processes_srcu);
552
553 hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) {
554 seq_printf(m, "Process %d PASID %d:\n",
555 p->lead_thread->tgid, p->pasid);
556
557 mutex_lock(&p->mutex);
558 r = pqm_debugfs_mqds(m, &p->pqm);
559 mutex_unlock(&p->mutex);
560
561 if (r)
562 break;
563 }
564
565 srcu_read_unlock(&kfd_processes_srcu, idx);
566
567 return r;
568}
569
570#endif