blob: aeb339db8a902712a12821f07ab35920ee18645d [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>
Felix Kuehlingb84394e2018-03-15 17:27:44 -040033#include <linux/file.h>
Alexey Skidanovdd592392014-11-18 13:56:23 +020034
Oded Gabbay19f6d2a2014-07-16 23:25:31 +030035struct mm_struct;
36
37#include "kfd_priv.h"
Felix Kuehling403575c2018-02-06 20:32:44 -050038#include "kfd_device_queue_manager.h"
Ben Gozc3447e82015-05-20 18:05:44 +030039#include "kfd_dbgmgr.h"
Felix Kuehling64d1c3a2017-12-08 19:22:12 -050040#include "kfd_iommu.h"
Oded Gabbay19f6d2a2014-07-16 23:25:31 +030041
42/*
Oded Gabbay19f6d2a2014-07-16 23:25:31 +030043 * List of struct kfd_process (field kfd_process).
44 * Unique/indexed by mm_struct*
45 */
Felix Kuehling64d1c3a2017-12-08 19:22:12 -050046DEFINE_HASHTABLE(kfd_processes_table, KFD_PROCESS_TABLE_SIZE);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +030047static DEFINE_MUTEX(kfd_processes_mutex);
48
Felix Kuehling64d1c3a2017-12-08 19:22:12 -050049DEFINE_SRCU(kfd_processes_srcu);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +030050
51static struct workqueue_struct *kfd_process_wq;
52
Oded Gabbay19f6d2a2014-07-16 23:25:31 +030053static struct kfd_process *find_process(const struct task_struct *thread);
Felix Kuehlingabb208a2017-11-27 18:29:52 -050054static void kfd_process_ref_release(struct kref *ref);
Yong Zhaoc0ede1f2017-11-27 18:29:56 -050055static struct kfd_process *create_process(const struct task_struct *thread,
56 struct file *filep);
Felix Kuehling373d7082017-11-14 16:41:19 -050057
Felix Kuehling26103432018-02-06 20:32:45 -050058static void evict_process_worker(struct work_struct *work);
59static void restore_process_worker(struct work_struct *work);
60
Oded Gabbay19f6d2a2014-07-16 23:25:31 +030061
62void kfd_process_create_wq(void)
63{
64 if (!kfd_process_wq)
Bhaktipriya Shridharfd320bf2016-05-29 21:14:11 +053065 kfd_process_wq = alloc_workqueue("kfd_process_wq", 0, 0);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +030066}
67
68void kfd_process_destroy_wq(void)
69{
70 if (kfd_process_wq) {
Oded Gabbay19f6d2a2014-07-16 23:25:31 +030071 destroy_workqueue(kfd_process_wq);
72 kfd_process_wq = NULL;
73 }
74}
75
Felix Kuehlingf35751b2018-03-15 17:27:49 -040076static void kfd_process_free_gpuvm(struct kgd_mem *mem,
77 struct kfd_process_device *pdd)
78{
79 struct kfd_dev *dev = pdd->dev;
80
81 dev->kfd2kgd->unmap_memory_to_gpu(dev->kgd, mem, pdd->vm);
82 dev->kfd2kgd->free_memory_of_gpu(dev->kgd, mem);
83}
84
85/* kfd_process_alloc_gpuvm - Allocate GPU VM for the KFD process
86 * This function should be only called right after the process
87 * is created and when kfd_processes_mutex is still being held
88 * to avoid concurrency. Because of that exclusiveness, we do
89 * not need to take p->mutex.
90 */
91static int kfd_process_alloc_gpuvm(struct kfd_process_device *pdd,
92 uint64_t gpu_va, uint32_t size,
93 uint32_t flags, void **kptr)
94{
95 struct kfd_dev *kdev = pdd->dev;
96 struct kgd_mem *mem = NULL;
97 int handle;
98 int err;
99
100 err = kdev->kfd2kgd->alloc_memory_of_gpu(kdev->kgd, gpu_va, size,
101 pdd->vm, &mem, NULL, flags);
102 if (err)
103 goto err_alloc_mem;
104
105 err = kdev->kfd2kgd->map_memory_to_gpu(kdev->kgd, mem, pdd->vm);
106 if (err)
107 goto err_map_mem;
108
109 err = kdev->kfd2kgd->sync_memory(kdev->kgd, mem, true);
110 if (err) {
111 pr_debug("Sync memory failed, wait interrupted by user signal\n");
112 goto sync_memory_failed;
113 }
114
115 /* Create an obj handle so kfd_process_device_remove_obj_handle
116 * will take care of the bo removal when the process finishes.
117 * We do not need to take p->mutex, because the process is just
118 * created and the ioctls have not had the chance to run.
119 */
120 handle = kfd_process_device_create_obj_handle(pdd, mem);
121
122 if (handle < 0) {
123 err = handle;
124 goto free_gpuvm;
125 }
126
127 if (kptr) {
128 err = kdev->kfd2kgd->map_gtt_bo_to_kernel(kdev->kgd,
129 (struct kgd_mem *)mem, kptr, NULL);
130 if (err) {
131 pr_debug("Map GTT BO to kernel failed\n");
132 goto free_obj_handle;
133 }
134 }
135
136 return err;
137
138free_obj_handle:
139 kfd_process_device_remove_obj_handle(pdd, handle);
140free_gpuvm:
141sync_memory_failed:
142 kfd_process_free_gpuvm(mem, pdd);
143 return err;
144
145err_map_mem:
146 kdev->kfd2kgd->free_memory_of_gpu(kdev->kgd, mem);
147err_alloc_mem:
148 *kptr = NULL;
149 return err;
150}
151
Felix Kuehling373d7082017-11-14 16:41:19 -0500152struct kfd_process *kfd_create_process(struct file *filep)
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300153{
154 struct kfd_process *process;
Felix Kuehling373d7082017-11-14 16:41:19 -0500155 struct task_struct *thread = current;
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300156
Kent Russell4eacc26b2017-08-15 23:00:06 -0400157 if (!thread->mm)
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300158 return ERR_PTR(-EINVAL);
159
160 /* Only the pthreads threading model is supported. */
161 if (thread->group_leader->mm != thread->mm)
162 return ERR_PTR(-EINVAL);
163
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300164 /*
165 * take kfd processes mutex before starting of process creation
166 * so there won't be a case where two threads of the same process
167 * create two kfd_process structures
168 */
169 mutex_lock(&kfd_processes_mutex);
170
171 /* A prior open of /dev/kfd could have already created the process. */
172 process = find_process(thread);
173 if (process)
Kent Russell79775b62017-08-15 23:00:05 -0400174 pr_debug("Process already found\n");
Yong Zhaoc0ede1f2017-11-27 18:29:56 -0500175 else
176 process = create_process(thread, filep);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300177
178 mutex_unlock(&kfd_processes_mutex);
179
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300180 return process;
181}
182
183struct kfd_process *kfd_get_process(const struct task_struct *thread)
184{
185 struct kfd_process *process;
186
Kent Russell4eacc26b2017-08-15 23:00:06 -0400187 if (!thread->mm)
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300188 return ERR_PTR(-EINVAL);
189
190 /* Only the pthreads threading model is supported. */
191 if (thread->group_leader->mm != thread->mm)
192 return ERR_PTR(-EINVAL);
193
194 process = find_process(thread);
195
196 return process;
197}
198
199static struct kfd_process *find_process_by_mm(const struct mm_struct *mm)
200{
201 struct kfd_process *process;
202
203 hash_for_each_possible_rcu(kfd_processes_table, process,
204 kfd_processes, (uintptr_t)mm)
205 if (process->mm == mm)
206 return process;
207
208 return NULL;
209}
210
211static struct kfd_process *find_process(const struct task_struct *thread)
212{
213 struct kfd_process *p;
214 int idx;
215
216 idx = srcu_read_lock(&kfd_processes_srcu);
217 p = find_process_by_mm(thread->mm);
218 srcu_read_unlock(&kfd_processes_srcu, idx);
219
220 return p;
221}
222
Felix Kuehlingabb208a2017-11-27 18:29:52 -0500223void kfd_unref_process(struct kfd_process *p)
224{
225 kref_put(&p->ref, kfd_process_ref_release);
226}
227
Felix Kuehling52b29d72018-03-15 17:27:48 -0400228static void kfd_process_device_free_bos(struct kfd_process_device *pdd)
229{
230 struct kfd_process *p = pdd->process;
231 void *mem;
232 int id;
233
234 /*
235 * Remove all handles from idr and release appropriate
236 * local memory object
237 */
238 idr_for_each_entry(&pdd->alloc_idr, mem, id) {
239 struct kfd_process_device *peer_pdd;
240
241 list_for_each_entry(peer_pdd, &p->per_device_data,
242 per_device_list) {
243 if (!peer_pdd->vm)
244 continue;
245 peer_pdd->dev->kfd2kgd->unmap_memory_to_gpu(
246 peer_pdd->dev->kgd, mem, peer_pdd->vm);
247 }
248
249 pdd->dev->kfd2kgd->free_memory_of_gpu(pdd->dev->kgd, mem);
250 kfd_process_device_remove_obj_handle(pdd, id);
251 }
252}
253
254static void kfd_process_free_outstanding_kfd_bos(struct kfd_process *p)
255{
256 struct kfd_process_device *pdd;
257
258 list_for_each_entry(pdd, &p->per_device_data, per_device_list)
259 kfd_process_device_free_bos(pdd);
260}
261
Felix Kuehlingde1450a2017-11-27 18:29:55 -0500262static void kfd_process_destroy_pdds(struct kfd_process *p)
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300263{
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300264 struct kfd_process_device *pdd, *temp;
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300265
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300266 list_for_each_entry_safe(pdd, temp, &p->per_device_data,
Felix Kuehlingde1450a2017-11-27 18:29:55 -0500267 per_device_list) {
268 pr_debug("Releasing pdd (topology id %d) for process (pasid %d)\n",
Oded Gabbay94a1ee02015-02-24 10:51:59 +0200269 pdd->dev->id, p->pasid);
270
Felix Kuehlingb84394e2018-03-15 17:27:44 -0400271 if (pdd->drm_file)
272 fput(pdd->drm_file);
273 else if (pdd->vm)
Felix Kuehling403575c2018-02-06 20:32:44 -0500274 pdd->dev->kfd2kgd->destroy_process_vm(
275 pdd->dev->kgd, pdd->vm);
276
Yong Zhao733fa1f2017-09-20 18:10:14 -0400277 list_del(&pdd->per_device_list);
Felix Kuehling373d7082017-11-14 16:41:19 -0500278
Felix Kuehlingf35751b2018-03-15 17:27:49 -0400279 if (pdd->qpd.cwsr_kaddr && !pdd->qpd.cwsr_base)
Felix Kuehling373d7082017-11-14 16:41:19 -0500280 free_pages((unsigned long)pdd->qpd.cwsr_kaddr,
281 get_order(KFD_CWSR_TBA_TMA_SIZE));
282
Felix Kuehling52b29d72018-03-15 17:27:48 -0400283 idr_destroy(&pdd->alloc_idr);
284
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300285 kfree(pdd);
286 }
Felix Kuehlingde1450a2017-11-27 18:29:55 -0500287}
288
289/* No process locking is needed in this function, because the process
290 * is not findable any more. We must assume that no other thread is
291 * using it any more, otherwise we couldn't safely free the process
292 * structure in the end.
293 */
294static void kfd_process_wq_release(struct work_struct *work)
295{
296 struct kfd_process *p = container_of(work, struct kfd_process,
297 release_work);
Felix Kuehlingde1450a2017-11-27 18:29:55 -0500298
Felix Kuehling64d1c3a2017-12-08 19:22:12 -0500299 kfd_iommu_unbind_process(p);
Felix Kuehlingde1450a2017-11-27 18:29:55 -0500300
Felix Kuehling52b29d72018-03-15 17:27:48 -0400301 kfd_process_free_outstanding_kfd_bos(p);
302
Felix Kuehlingde1450a2017-11-27 18:29:55 -0500303 kfd_process_destroy_pdds(p);
Felix Kuehling403575c2018-02-06 20:32:44 -0500304 dma_fence_put(p->ef);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300305
Andrew Lewyckyf3a39812015-05-10 12:15:46 +0300306 kfd_event_free_process(p);
307
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300308 kfd_pasid_free(p->pasid);
Felix Kuehlinga91e70e2017-08-26 02:00:57 -0400309 kfd_free_process_doorbells(p);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300310
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300311 mutex_destroy(&p->mutex);
312
Felix Kuehlingc7b12432017-11-27 18:29:50 -0500313 put_task_struct(p->lead_thread);
314
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300315 kfree(p);
Felix Kuehling5ce10682017-11-27 18:29:51 -0500316}
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300317
Felix Kuehling5ce10682017-11-27 18:29:51 -0500318static void kfd_process_ref_release(struct kref *ref)
319{
320 struct kfd_process *p = container_of(ref, struct kfd_process, ref);
321
322 INIT_WORK(&p->release_work, kfd_process_wq_release);
323 queue_work(kfd_process_wq, &p->release_work);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300324}
325
326static void kfd_process_destroy_delayed(struct rcu_head *rcu)
327{
Felix Kuehling5ce10682017-11-27 18:29:51 -0500328 struct kfd_process *p = container_of(rcu, struct kfd_process, rcu);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300329
Felix Kuehlingabb208a2017-11-27 18:29:52 -0500330 kfd_unref_process(p);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300331}
332
333static void kfd_process_notifier_release(struct mmu_notifier *mn,
334 struct mm_struct *mm)
335{
336 struct kfd_process *p;
Ben Goza82918f2015-03-25 13:12:20 +0200337 struct kfd_process_device *pdd = NULL;
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300338
339 /*
340 * The kfd_process structure can not be free because the
341 * mmu_notifier srcu is read locked
342 */
343 p = container_of(mn, struct kfd_process, mmu_notifier);
Felix Kuehling32fa8212017-08-15 23:00:12 -0400344 if (WARN_ON(p->mm != mm))
345 return;
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300346
347 mutex_lock(&kfd_processes_mutex);
348 hash_del_rcu(&p->kfd_processes);
349 mutex_unlock(&kfd_processes_mutex);
350 synchronize_srcu(&kfd_processes_srcu);
351
Felix Kuehling26103432018-02-06 20:32:45 -0500352 cancel_delayed_work_sync(&p->eviction_work);
353 cancel_delayed_work_sync(&p->restore_work);
354
Ben Goz45102042014-07-17 01:04:10 +0300355 mutex_lock(&p->mutex);
356
Yair Shachar062c5672017-11-01 19:21:29 -0400357 /* Iterate over all process device data structures and if the
358 * pdd is in debug mode, we should first force unregistration,
359 * then we will be able to destroy the queues
360 */
361 list_for_each_entry(pdd, &p->per_device_data, per_device_list) {
362 struct kfd_dev *dev = pdd->dev;
363
364 mutex_lock(kfd_get_dbgmgr_mutex());
365 if (dev && dev->dbgmgr && dev->dbgmgr->pasid == p->pasid) {
366 if (!kfd_dbgmgr_unregister(dev->dbgmgr, p)) {
367 kfd_dbgmgr_destroy(dev->dbgmgr);
368 dev->dbgmgr = NULL;
369 }
370 }
371 mutex_unlock(kfd_get_dbgmgr_mutex());
372 }
373
Felix Kuehling9fd3f1bf2017-09-27 00:09:52 -0400374 kfd_process_dequeue_from_all_devices(p);
Ben Goz45102042014-07-17 01:04:10 +0300375 pqm_uninit(&p->pqm);
376
Felix Kuehling5ce10682017-11-27 18:29:51 -0500377 /* Indicate to other users that MM is no longer valid */
378 p->mm = NULL;
379
Ben Goz45102042014-07-17 01:04:10 +0300380 mutex_unlock(&p->mutex);
381
Felix Kuehling5ce10682017-11-27 18:29:51 -0500382 mmu_notifier_unregister_no_release(&p->mmu_notifier, mm);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300383 mmu_notifier_call_srcu(&p->rcu, &kfd_process_destroy_delayed);
384}
385
386static const struct mmu_notifier_ops kfd_process_mmu_notifier_ops = {
387 .release = kfd_process_notifier_release,
388};
389
Felix Kuehlingf35751b2018-03-15 17:27:49 -0400390static int kfd_process_init_cwsr_apu(struct kfd_process *p, struct file *filep)
Felix Kuehling373d7082017-11-14 16:41:19 -0500391{
Felix Kuehling373d7082017-11-14 16:41:19 -0500392 unsigned long offset;
Felix Kuehlingf35751b2018-03-15 17:27:49 -0400393 struct kfd_process_device *pdd;
Felix Kuehling373d7082017-11-14 16:41:19 -0500394
Yong Zhaoc0ede1f2017-11-27 18:29:56 -0500395 list_for_each_entry(pdd, &p->per_device_data, per_device_list) {
Felix Kuehlingf35751b2018-03-15 17:27:49 -0400396 struct kfd_dev *dev = pdd->dev;
397 struct qcm_process_device *qpd = &pdd->qpd;
398
399 if (!dev->cwsr_enabled || qpd->cwsr_kaddr || qpd->cwsr_base)
Felix Kuehling373d7082017-11-14 16:41:19 -0500400 continue;
Felix Kuehlingf35751b2018-03-15 17:27:49 -0400401
Felix Kuehling373d7082017-11-14 16:41:19 -0500402 offset = (dev->id | KFD_MMAP_RESERVED_MEM_MASK) << PAGE_SHIFT;
403 qpd->tba_addr = (int64_t)vm_mmap(filep, 0,
404 KFD_CWSR_TBA_TMA_SIZE, PROT_READ | PROT_EXEC,
405 MAP_SHARED, offset);
406
407 if (IS_ERR_VALUE(qpd->tba_addr)) {
Yong Zhaoc0ede1f2017-11-27 18:29:56 -0500408 int err = qpd->tba_addr;
409
410 pr_err("Failure to set tba address. error %d.\n", err);
Felix Kuehling373d7082017-11-14 16:41:19 -0500411 qpd->tba_addr = 0;
412 qpd->cwsr_kaddr = NULL;
Yong Zhaoc0ede1f2017-11-27 18:29:56 -0500413 return err;
Felix Kuehling373d7082017-11-14 16:41:19 -0500414 }
415
416 memcpy(qpd->cwsr_kaddr, dev->cwsr_isa, dev->cwsr_isa_size);
417
418 qpd->tma_addr = qpd->tba_addr + KFD_CWSR_TMA_OFFSET;
419 pr_debug("set tba :0x%llx, tma:0x%llx, cwsr_kaddr:%p for pqm.\n",
420 qpd->tba_addr, qpd->tma_addr, qpd->cwsr_kaddr);
421 }
Yong Zhaoc0ede1f2017-11-27 18:29:56 -0500422
423 return 0;
Felix Kuehling373d7082017-11-14 16:41:19 -0500424}
425
Felix Kuehlingf35751b2018-03-15 17:27:49 -0400426static int kfd_process_device_init_cwsr_dgpu(struct kfd_process_device *pdd)
427{
428 struct kfd_dev *dev = pdd->dev;
429 struct qcm_process_device *qpd = &pdd->qpd;
430 uint32_t flags = ALLOC_MEM_FLAGS_GTT |
431 ALLOC_MEM_FLAGS_NO_SUBSTITUTE | ALLOC_MEM_FLAGS_EXECUTABLE;
432 void *kaddr;
433 int ret;
434
435 if (!dev->cwsr_enabled || qpd->cwsr_kaddr || !qpd->cwsr_base)
436 return 0;
437
438 /* cwsr_base is only set for dGPU */
439 ret = kfd_process_alloc_gpuvm(pdd, qpd->cwsr_base,
440 KFD_CWSR_TBA_TMA_SIZE, flags, &kaddr);
441 if (ret)
442 return ret;
443
444 qpd->cwsr_kaddr = kaddr;
445 qpd->tba_addr = qpd->cwsr_base;
446
447 memcpy(qpd->cwsr_kaddr, dev->cwsr_isa, dev->cwsr_isa_size);
448
449 qpd->tma_addr = qpd->tba_addr + KFD_CWSR_TMA_OFFSET;
450 pr_debug("set tba :0x%llx, tma:0x%llx, cwsr_kaddr:%p for pqm.\n",
451 qpd->tba_addr, qpd->tma_addr, qpd->cwsr_kaddr);
452
453 return 0;
454}
455
Yong Zhaoc0ede1f2017-11-27 18:29:56 -0500456static struct kfd_process *create_process(const struct task_struct *thread,
457 struct file *filep)
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300458{
459 struct kfd_process *process;
460 int err = -ENOMEM;
461
462 process = kzalloc(sizeof(*process), GFP_KERNEL);
463
464 if (!process)
465 goto err_alloc_process;
466
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300467 process->pasid = kfd_pasid_alloc();
468 if (process->pasid == 0)
469 goto err_alloc_pasid;
470
Felix Kuehlinga91e70e2017-08-26 02:00:57 -0400471 if (kfd_alloc_process_doorbells(process) < 0)
472 goto err_alloc_doorbells;
473
Felix Kuehling5ce10682017-11-27 18:29:51 -0500474 kref_init(&process->ref);
475
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300476 mutex_init(&process->mutex);
477
478 process->mm = thread->mm;
479
480 /* register notifier */
481 process->mmu_notifier.ops = &kfd_process_mmu_notifier_ops;
Yong Zhaoc0ede1f2017-11-27 18:29:56 -0500482 err = mmu_notifier_register(&process->mmu_notifier, process->mm);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300483 if (err)
484 goto err_mmu_notifier;
485
486 hash_add_rcu(kfd_processes_table, &process->kfd_processes,
487 (uintptr_t)process->mm);
488
489 process->lead_thread = thread->group_leader;
Felix Kuehlingc7b12432017-11-27 18:29:50 -0500490 get_task_struct(process->lead_thread);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300491
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300492 INIT_LIST_HEAD(&process->per_device_data);
493
Andrew Lewyckyf3a39812015-05-10 12:15:46 +0300494 kfd_event_init_process(process);
495
Ben Goz45102042014-07-17 01:04:10 +0300496 err = pqm_init(&process->pqm, process);
497 if (err != 0)
498 goto err_process_pqm_init;
499
Alexey Skidanovdd592392014-11-18 13:56:23 +0200500 /* init process apertures*/
Andy Lutomirski10f16852016-03-22 14:25:19 -0700501 process->is_32bit_user_mode = in_compat_syscall();
Dan Carpenterb312b2b2017-06-14 13:58:53 +0300502 err = kfd_init_apertures(process);
503 if (err != 0)
Geert Uytterhoeven7a10d632017-06-01 12:28:38 +0200504 goto err_init_apertures;
Alexey Skidanovdd592392014-11-18 13:56:23 +0200505
Felix Kuehling26103432018-02-06 20:32:45 -0500506 INIT_DELAYED_WORK(&process->eviction_work, evict_process_worker);
507 INIT_DELAYED_WORK(&process->restore_work, restore_process_worker);
508 process->last_restore_timestamp = get_jiffies_64();
509
Felix Kuehlingf35751b2018-03-15 17:27:49 -0400510 err = kfd_process_init_cwsr_apu(process, filep);
Yong Zhaoc0ede1f2017-11-27 18:29:56 -0500511 if (err)
512 goto err_init_cwsr;
513
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300514 return process;
515
Yong Zhaoc0ede1f2017-11-27 18:29:56 -0500516err_init_cwsr:
Felix Kuehling52b29d72018-03-15 17:27:48 -0400517 kfd_process_free_outstanding_kfd_bos(process);
Yong Zhaoc0ede1f2017-11-27 18:29:56 -0500518 kfd_process_destroy_pdds(process);
Geert Uytterhoeven7a10d632017-06-01 12:28:38 +0200519err_init_apertures:
Alexey Skidanovdd592392014-11-18 13:56:23 +0200520 pqm_uninit(&process->pqm);
Ben Goz45102042014-07-17 01:04:10 +0300521err_process_pqm_init:
522 hash_del_rcu(&process->kfd_processes);
523 synchronize_rcu();
524 mmu_notifier_unregister_no_release(&process->mmu_notifier, process->mm);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300525err_mmu_notifier:
Oded Gabbay7fd5e032016-06-23 17:54:29 +0300526 mutex_destroy(&process->mutex);
Felix Kuehlinga91e70e2017-08-26 02:00:57 -0400527 kfd_free_process_doorbells(process);
528err_alloc_doorbells:
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300529 kfd_pasid_free(process->pasid);
530err_alloc_pasid:
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300531 kfree(process);
532err_alloc_process:
533 return ERR_PTR(err);
534}
535
536struct kfd_process_device *kfd_get_process_device_data(struct kfd_dev *dev,
Alexey Skidanov093c7d82014-11-18 14:00:04 +0200537 struct kfd_process *p)
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300538{
539 struct kfd_process_device *pdd = NULL;
540
541 list_for_each_entry(pdd, &p->per_device_data, per_device_list)
542 if (pdd->dev == dev)
Yong Zhao733fa1f2017-09-20 18:10:14 -0400543 return pdd;
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300544
Yong Zhao733fa1f2017-09-20 18:10:14 -0400545 return NULL;
Alexey Skidanov093c7d82014-11-18 14:00:04 +0200546}
547
548struct kfd_process_device *kfd_create_process_device_data(struct kfd_dev *dev,
549 struct kfd_process *p)
550{
551 struct kfd_process_device *pdd = NULL;
552
553 pdd = kzalloc(sizeof(*pdd), GFP_KERNEL);
Felix Kuehling2d9b36f2017-11-27 18:29:54 -0500554 if (!pdd)
555 return NULL;
556
557 pdd->dev = dev;
558 INIT_LIST_HEAD(&pdd->qpd.queues_list);
559 INIT_LIST_HEAD(&pdd->qpd.priv_queue_list);
560 pdd->qpd.dqm = dev->dqm;
561 pdd->qpd.pqm = &p->pqm;
Felix Kuehling26103432018-02-06 20:32:45 -0500562 pdd->qpd.evicted = 0;
Felix Kuehling2d9b36f2017-11-27 18:29:54 -0500563 pdd->process = p;
564 pdd->bound = PDD_UNBOUND;
565 pdd->already_dequeued = false;
566 list_add(&pdd->per_device_list, &p->per_device_data);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300567
Felix Kuehling52b29d72018-03-15 17:27:48 -0400568 /* Init idr used for memory handle translation */
569 idr_init(&pdd->alloc_idr);
570
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300571 return pdd;
Felix Kuehlingb84394e2018-03-15 17:27:44 -0400572}
Felix Kuehling403575c2018-02-06 20:32:44 -0500573
Felix Kuehlingb84394e2018-03-15 17:27:44 -0400574/**
575 * kfd_process_device_init_vm - Initialize a VM for a process-device
576 *
577 * @pdd: The process-device
578 * @drm_file: Optional pointer to a DRM file descriptor
579 *
580 * If @drm_file is specified, it will be used to acquire the VM from
581 * that file descriptor. If successful, the @pdd takes ownership of
582 * the file descriptor.
583 *
584 * If @drm_file is NULL, a new VM is created.
585 *
586 * Returns 0 on success, -errno on failure.
587 */
588int kfd_process_device_init_vm(struct kfd_process_device *pdd,
589 struct file *drm_file)
590{
591 struct kfd_process *p;
592 struct kfd_dev *dev;
593 int ret;
594
595 if (pdd->vm)
596 return drm_file ? -EBUSY : 0;
597
598 p = pdd->process;
599 dev = pdd->dev;
600
601 if (drm_file)
602 ret = dev->kfd2kgd->acquire_process_vm(
603 dev->kgd, drm_file,
604 &pdd->vm, &p->kgd_process_info, &p->ef);
605 else
606 ret = dev->kfd2kgd->create_process_vm(
607 dev->kgd, &pdd->vm, &p->kgd_process_info, &p->ef);
608 if (ret) {
609 pr_err("Failed to create process VM object\n");
610 return ret;
611 }
612
Felix Kuehlingf35751b2018-03-15 17:27:49 -0400613 ret = kfd_process_device_init_cwsr_dgpu(pdd);
614 if (ret)
615 goto err_init_cwsr;
616
Felix Kuehlingb84394e2018-03-15 17:27:44 -0400617 pdd->drm_file = drm_file;
618
619 return 0;
Felix Kuehlingf35751b2018-03-15 17:27:49 -0400620
621err_init_cwsr:
622 kfd_process_device_free_bos(pdd);
623 if (!drm_file)
624 dev->kfd2kgd->destroy_process_vm(dev->kgd, pdd->vm);
625 pdd->vm = NULL;
626
627 return ret;
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300628}
629
630/*
631 * Direct the IOMMU to bind the process (specifically the pasid->mm)
632 * to the device.
633 * Unbinding occurs when the process dies or the device is removed.
634 *
635 * Assumes that the process lock is held.
636 */
637struct kfd_process_device *kfd_bind_process_to_device(struct kfd_dev *dev,
638 struct kfd_process *p)
639{
Alexey Skidanov093c7d82014-11-18 14:00:04 +0200640 struct kfd_process_device *pdd;
Oded Gabbayb17f0682014-07-17 00:06:27 +0300641 int err;
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300642
Alexey Skidanov093c7d82014-11-18 14:00:04 +0200643 pdd = kfd_get_process_device_data(dev, p);
644 if (!pdd) {
645 pr_err("Process device data doesn't exist\n");
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300646 return ERR_PTR(-ENOMEM);
Alexey Skidanov093c7d82014-11-18 14:00:04 +0200647 }
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300648
Felix Kuehling64d1c3a2017-12-08 19:22:12 -0500649 err = kfd_iommu_bind_process_to_device(pdd);
650 if (err)
Oded Gabbayb17f0682014-07-17 00:06:27 +0300651 return ERR_PTR(err);
652
Felix Kuehlingb84394e2018-03-15 17:27:44 -0400653 err = kfd_process_device_init_vm(pdd, NULL);
654 if (err)
655 return ERR_PTR(err);
656
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300657 return pdd;
658}
659
Kent Russell8eabaf52017-08-15 23:00:04 -0400660struct kfd_process_device *kfd_get_first_process_device_data(
661 struct kfd_process *p)
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300662{
663 return list_first_entry(&p->per_device_data,
664 struct kfd_process_device,
665 per_device_list);
666}
667
Kent Russell8eabaf52017-08-15 23:00:04 -0400668struct kfd_process_device *kfd_get_next_process_device_data(
669 struct kfd_process *p,
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300670 struct kfd_process_device *pdd)
671{
672 if (list_is_last(&pdd->per_device_list, &p->per_device_data))
673 return NULL;
674 return list_next_entry(pdd, per_device_list);
675}
676
677bool kfd_has_process_device_data(struct kfd_process *p)
678{
679 return !(list_empty(&p->per_device_data));
680}
Andrew Lewyckyf3a39812015-05-10 12:15:46 +0300681
Felix Kuehling52b29d72018-03-15 17:27:48 -0400682/* Create specific handle mapped to mem from process local memory idr
683 * Assumes that the process lock is held.
684 */
685int kfd_process_device_create_obj_handle(struct kfd_process_device *pdd,
686 void *mem)
687{
688 return idr_alloc(&pdd->alloc_idr, mem, 0, 0, GFP_KERNEL);
689}
690
691/* Translate specific handle from process local memory idr
692 * Assumes that the process lock is held.
693 */
694void *kfd_process_device_translate_handle(struct kfd_process_device *pdd,
695 int handle)
696{
697 if (handle < 0)
698 return NULL;
699
700 return idr_find(&pdd->alloc_idr, handle);
701}
702
703/* Remove specific handle from process local memory idr
704 * Assumes that the process lock is held.
705 */
706void kfd_process_device_remove_obj_handle(struct kfd_process_device *pdd,
707 int handle)
708{
709 if (handle >= 0)
710 idr_remove(&pdd->alloc_idr, handle);
711}
712
Felix Kuehlingabb208a2017-11-27 18:29:52 -0500713/* This increments the process->ref counter. */
Andrew Lewyckyf3a39812015-05-10 12:15:46 +0300714struct kfd_process *kfd_lookup_process_by_pasid(unsigned int pasid)
715{
Yong Zhao82c16b42017-11-27 18:29:53 -0500716 struct kfd_process *p, *ret_p = NULL;
Andrew Lewyckyf3a39812015-05-10 12:15:46 +0300717 unsigned int temp;
718
719 int idx = srcu_read_lock(&kfd_processes_srcu);
720
721 hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) {
722 if (p->pasid == pasid) {
Felix Kuehlingabb208a2017-11-27 18:29:52 -0500723 kref_get(&p->ref);
Yong Zhao82c16b42017-11-27 18:29:53 -0500724 ret_p = p;
Andrew Lewyckyf3a39812015-05-10 12:15:46 +0300725 break;
726 }
727 }
728
729 srcu_read_unlock(&kfd_processes_srcu, idx);
730
Yong Zhao82c16b42017-11-27 18:29:53 -0500731 return ret_p;
Andrew Lewyckyf3a39812015-05-10 12:15:46 +0300732}
Felix Kuehling373d7082017-11-14 16:41:19 -0500733
Felix Kuehling26103432018-02-06 20:32:45 -0500734/* This increments the process->ref counter. */
735struct kfd_process *kfd_lookup_process_by_mm(const struct mm_struct *mm)
736{
737 struct kfd_process *p;
738
739 int idx = srcu_read_lock(&kfd_processes_srcu);
740
741 p = find_process_by_mm(mm);
742 if (p)
743 kref_get(&p->ref);
744
745 srcu_read_unlock(&kfd_processes_srcu, idx);
746
747 return p;
748}
749
750/* process_evict_queues - Evict all user queues of a process
751 *
752 * Eviction is reference-counted per process-device. This means multiple
753 * evictions from different sources can be nested safely.
754 */
755static int process_evict_queues(struct kfd_process *p)
756{
757 struct kfd_process_device *pdd;
758 int r = 0;
759 unsigned int n_evicted = 0;
760
761 list_for_each_entry(pdd, &p->per_device_data, per_device_list) {
762 r = pdd->dev->dqm->ops.evict_process_queues(pdd->dev->dqm,
763 &pdd->qpd);
764 if (r) {
765 pr_err("Failed to evict process queues\n");
766 goto fail;
767 }
768 n_evicted++;
769 }
770
771 return r;
772
773fail:
774 /* To keep state consistent, roll back partial eviction by
775 * restoring queues
776 */
777 list_for_each_entry(pdd, &p->per_device_data, per_device_list) {
778 if (n_evicted == 0)
779 break;
780 if (pdd->dev->dqm->ops.restore_process_queues(pdd->dev->dqm,
781 &pdd->qpd))
782 pr_err("Failed to restore queues\n");
783
784 n_evicted--;
785 }
786
787 return r;
788}
789
790/* process_restore_queues - Restore all user queues of a process */
791static int process_restore_queues(struct kfd_process *p)
792{
793 struct kfd_process_device *pdd;
794 int r, ret = 0;
795
796 list_for_each_entry(pdd, &p->per_device_data, per_device_list) {
797 r = pdd->dev->dqm->ops.restore_process_queues(pdd->dev->dqm,
798 &pdd->qpd);
799 if (r) {
800 pr_err("Failed to restore process queues\n");
801 if (!ret)
802 ret = r;
803 }
804 }
805
806 return ret;
807}
808
809static void evict_process_worker(struct work_struct *work)
810{
811 int ret;
812 struct kfd_process *p;
813 struct delayed_work *dwork;
814
815 dwork = to_delayed_work(work);
816
817 /* Process termination destroys this worker thread. So during the
818 * lifetime of this thread, kfd_process p will be valid
819 */
820 p = container_of(dwork, struct kfd_process, eviction_work);
821 WARN_ONCE(p->last_eviction_seqno != p->ef->seqno,
822 "Eviction fence mismatch\n");
823
824 /* Narrow window of overlap between restore and evict work
825 * item is possible. Once amdgpu_amdkfd_gpuvm_restore_process_bos
826 * unreserves KFD BOs, it is possible to evicted again. But
827 * restore has few more steps of finish. So lets wait for any
828 * previous restore work to complete
829 */
830 flush_delayed_work(&p->restore_work);
831
832 pr_debug("Started evicting pasid %d\n", p->pasid);
833 ret = process_evict_queues(p);
834 if (!ret) {
835 dma_fence_signal(p->ef);
836 dma_fence_put(p->ef);
837 p->ef = NULL;
838 schedule_delayed_work(&p->restore_work,
839 msecs_to_jiffies(PROCESS_RESTORE_TIME_MS));
840
841 pr_debug("Finished evicting pasid %d\n", p->pasid);
842 } else
843 pr_err("Failed to evict queues of pasid %d\n", p->pasid);
844}
845
846static void restore_process_worker(struct work_struct *work)
847{
848 struct delayed_work *dwork;
849 struct kfd_process *p;
850 struct kfd_process_device *pdd;
851 int ret = 0;
852
853 dwork = to_delayed_work(work);
854
855 /* Process termination destroys this worker thread. So during the
856 * lifetime of this thread, kfd_process p will be valid
857 */
858 p = container_of(dwork, struct kfd_process, restore_work);
859
860 /* Call restore_process_bos on the first KGD device. This function
861 * takes care of restoring the whole process including other devices.
862 * Restore can fail if enough memory is not available. If so,
863 * reschedule again.
864 */
865 pdd = list_first_entry(&p->per_device_data,
866 struct kfd_process_device,
867 per_device_list);
868
869 pr_debug("Started restoring pasid %d\n", p->pasid);
870
871 /* Setting last_restore_timestamp before successful restoration.
872 * Otherwise this would have to be set by KGD (restore_process_bos)
873 * before KFD BOs are unreserved. If not, the process can be evicted
874 * again before the timestamp is set.
875 * If restore fails, the timestamp will be set again in the next
876 * attempt. This would mean that the minimum GPU quanta would be
877 * PROCESS_ACTIVE_TIME_MS - (time to execute the following two
878 * functions)
879 */
880
881 p->last_restore_timestamp = get_jiffies_64();
882 ret = pdd->dev->kfd2kgd->restore_process_bos(p->kgd_process_info,
883 &p->ef);
884 if (ret) {
885 pr_debug("Failed to restore BOs of pasid %d, retry after %d ms\n",
886 p->pasid, PROCESS_BACK_OFF_TIME_MS);
887 ret = schedule_delayed_work(&p->restore_work,
888 msecs_to_jiffies(PROCESS_BACK_OFF_TIME_MS));
889 WARN(!ret, "reschedule restore work failed\n");
890 return;
891 }
892
893 ret = process_restore_queues(p);
894 if (!ret)
895 pr_debug("Finished restoring pasid %d\n", p->pasid);
896 else
897 pr_err("Failed to restore queues of pasid %d\n", p->pasid);
898}
899
900void kfd_suspend_all_processes(void)
901{
902 struct kfd_process *p;
903 unsigned int temp;
904 int idx = srcu_read_lock(&kfd_processes_srcu);
905
906 hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) {
907 cancel_delayed_work_sync(&p->eviction_work);
908 cancel_delayed_work_sync(&p->restore_work);
909
910 if (process_evict_queues(p))
911 pr_err("Failed to suspend process %d\n", p->pasid);
912 dma_fence_signal(p->ef);
913 dma_fence_put(p->ef);
914 p->ef = NULL;
915 }
916 srcu_read_unlock(&kfd_processes_srcu, idx);
917}
918
919int kfd_resume_all_processes(void)
920{
921 struct kfd_process *p;
922 unsigned int temp;
923 int ret = 0, idx = srcu_read_lock(&kfd_processes_srcu);
924
925 hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) {
926 if (!schedule_delayed_work(&p->restore_work, 0)) {
927 pr_err("Restore process %d failed during resume\n",
928 p->pasid);
929 ret = -EFAULT;
930 }
931 }
932 srcu_read_unlock(&kfd_processes_srcu, idx);
933 return ret;
934}
935
Felix Kuehling373d7082017-11-14 16:41:19 -0500936int kfd_reserved_mem_mmap(struct kfd_process *process,
937 struct vm_area_struct *vma)
938{
939 struct kfd_dev *dev = kfd_device_by_id(vma->vm_pgoff);
940 struct kfd_process_device *pdd;
941 struct qcm_process_device *qpd;
942
943 if (!dev)
944 return -EINVAL;
945 if ((vma->vm_end - vma->vm_start) != KFD_CWSR_TBA_TMA_SIZE) {
946 pr_err("Incorrect CWSR mapping size.\n");
947 return -EINVAL;
948 }
949
950 pdd = kfd_get_process_device_data(dev, process);
951 if (!pdd)
952 return -EINVAL;
953 qpd = &pdd->qpd;
954
955 qpd->cwsr_kaddr = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
956 get_order(KFD_CWSR_TBA_TMA_SIZE));
957 if (!qpd->cwsr_kaddr) {
958 pr_err("Error allocating per process CWSR buffer.\n");
959 return -ENOMEM;
960 }
961
962 vma->vm_flags |= VM_IO | VM_DONTCOPY | VM_DONTEXPAND
963 | VM_NORESERVE | VM_DONTDUMP | VM_PFNMAP;
964 /* Mapping pages to user process */
965 return remap_pfn_range(vma, vma->vm_start,
966 PFN_DOWN(__pa(qpd->cwsr_kaddr)),
967 KFD_CWSR_TBA_TMA_SIZE, vma->vm_page_prot);
968}
Felix Kuehling851a6452017-11-27 18:29:49 -0500969
Felix Kuehling403575c2018-02-06 20:32:44 -0500970void kfd_flush_tlb(struct kfd_process_device *pdd)
971{
972 struct kfd_dev *dev = pdd->dev;
973 const struct kfd2kgd_calls *f2g = dev->kfd2kgd;
974
975 if (dev->dqm->sched_policy == KFD_SCHED_POLICY_NO_HWS) {
976 /* Nothing to flush until a VMID is assigned, which
977 * only happens when the first queue is created.
978 */
979 if (pdd->qpd.vmid)
980 f2g->invalidate_tlbs_vmid(dev->kgd, pdd->qpd.vmid);
981 } else {
982 f2g->invalidate_tlbs(dev->kgd, pdd->process->pasid);
983 }
984}
985
Felix Kuehling851a6452017-11-27 18:29:49 -0500986#if defined(CONFIG_DEBUG_FS)
987
988int kfd_debugfs_mqds_by_process(struct seq_file *m, void *data)
989{
990 struct kfd_process *p;
991 unsigned int temp;
992 int r = 0;
993
994 int idx = srcu_read_lock(&kfd_processes_srcu);
995
996 hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) {
997 seq_printf(m, "Process %d PASID %d:\n",
998 p->lead_thread->tgid, p->pasid);
999
1000 mutex_lock(&p->mutex);
1001 r = pqm_debugfs_mqds(m, &p->pqm);
1002 mutex_unlock(&p->mutex);
1003
1004 if (r)
1005 break;
1006 }
1007
1008 srcu_read_unlock(&kfd_processes_srcu, idx);
1009
1010 return r;
1011}
1012
1013#endif