Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 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> |
Ingo Molnar | 6e84f31 | 2017-02-08 18:51:29 +0100 | [diff] [blame] | 26 | #include <linux/sched/mm.h> |
Felix Kuehling | c7b1243 | 2017-11-27 18:29:50 -0500 | [diff] [blame] | 27 | #include <linux/sched/task.h> |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 28 | #include <linux/slab.h> |
Oded Gabbay | b17f068 | 2014-07-17 00:06:27 +0300 | [diff] [blame] | 29 | #include <linux/amd-iommu.h> |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 30 | #include <linux/notifier.h> |
Alexey Skidanov | dd59239 | 2014-11-18 13:56:23 +0200 | [diff] [blame] | 31 | #include <linux/compat.h> |
Felix Kuehling | 373d708 | 2017-11-14 16:41:19 -0500 | [diff] [blame] | 32 | #include <linux/mman.h> |
Alexey Skidanov | dd59239 | 2014-11-18 13:56:23 +0200 | [diff] [blame] | 33 | |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 34 | struct mm_struct; |
| 35 | |
| 36 | #include "kfd_priv.h" |
Ben Goz | c3447e8 | 2015-05-20 18:05:44 +0300 | [diff] [blame] | 37 | #include "kfd_dbgmgr.h" |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 38 | |
| 39 | /* |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 40 | * List of struct kfd_process (field kfd_process). |
| 41 | * Unique/indexed by mm_struct* |
| 42 | */ |
| 43 | #define KFD_PROCESS_TABLE_SIZE 5 /* bits: 32 entries */ |
| 44 | static DEFINE_HASHTABLE(kfd_processes_table, KFD_PROCESS_TABLE_SIZE); |
| 45 | static DEFINE_MUTEX(kfd_processes_mutex); |
| 46 | |
| 47 | DEFINE_STATIC_SRCU(kfd_processes_srcu); |
| 48 | |
| 49 | static struct workqueue_struct *kfd_process_wq; |
| 50 | |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 51 | static struct kfd_process *find_process(const struct task_struct *thread); |
Felix Kuehling | abb208a | 2017-11-27 18:29:52 -0500 | [diff] [blame] | 52 | static void kfd_process_ref_release(struct kref *ref); |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 53 | static struct kfd_process *create_process(const struct task_struct *thread); |
Felix Kuehling | 373d708 | 2017-11-14 16:41:19 -0500 | [diff] [blame] | 54 | static int kfd_process_init_cwsr(struct kfd_process *p, struct file *filep); |
| 55 | |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 56 | |
| 57 | void kfd_process_create_wq(void) |
| 58 | { |
| 59 | if (!kfd_process_wq) |
Bhaktipriya Shridhar | fd320bf | 2016-05-29 21:14:11 +0530 | [diff] [blame] | 60 | kfd_process_wq = alloc_workqueue("kfd_process_wq", 0, 0); |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | void kfd_process_destroy_wq(void) |
| 64 | { |
| 65 | if (kfd_process_wq) { |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 66 | destroy_workqueue(kfd_process_wq); |
| 67 | kfd_process_wq = NULL; |
| 68 | } |
| 69 | } |
| 70 | |
Felix Kuehling | 373d708 | 2017-11-14 16:41:19 -0500 | [diff] [blame] | 71 | struct kfd_process *kfd_create_process(struct file *filep) |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 72 | { |
| 73 | struct kfd_process *process; |
Felix Kuehling | 373d708 | 2017-11-14 16:41:19 -0500 | [diff] [blame] | 74 | struct task_struct *thread = current; |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 75 | |
Kent Russell | 4eacc26b | 2017-08-15 23:00:06 -0400 | [diff] [blame] | 76 | if (!thread->mm) |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 77 | return ERR_PTR(-EINVAL); |
| 78 | |
| 79 | /* Only the pthreads threading model is supported. */ |
| 80 | if (thread->group_leader->mm != thread->mm) |
| 81 | return ERR_PTR(-EINVAL); |
| 82 | |
| 83 | /* Take mmap_sem because we call __mmu_notifier_register inside */ |
| 84 | down_write(&thread->mm->mmap_sem); |
| 85 | |
| 86 | /* |
| 87 | * take kfd processes mutex before starting of process creation |
| 88 | * so there won't be a case where two threads of the same process |
| 89 | * create two kfd_process structures |
| 90 | */ |
| 91 | mutex_lock(&kfd_processes_mutex); |
| 92 | |
| 93 | /* A prior open of /dev/kfd could have already created the process. */ |
| 94 | process = find_process(thread); |
| 95 | if (process) |
Kent Russell | 79775b6 | 2017-08-15 23:00:05 -0400 | [diff] [blame] | 96 | pr_debug("Process already found\n"); |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 97 | |
| 98 | if (!process) |
| 99 | process = create_process(thread); |
| 100 | |
| 101 | mutex_unlock(&kfd_processes_mutex); |
| 102 | |
| 103 | up_write(&thread->mm->mmap_sem); |
| 104 | |
Felix Kuehling | 373d708 | 2017-11-14 16:41:19 -0500 | [diff] [blame] | 105 | kfd_process_init_cwsr(process, filep); |
| 106 | |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 107 | return process; |
| 108 | } |
| 109 | |
| 110 | struct kfd_process *kfd_get_process(const struct task_struct *thread) |
| 111 | { |
| 112 | struct kfd_process *process; |
| 113 | |
Kent Russell | 4eacc26b | 2017-08-15 23:00:06 -0400 | [diff] [blame] | 114 | if (!thread->mm) |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 115 | return ERR_PTR(-EINVAL); |
| 116 | |
| 117 | /* Only the pthreads threading model is supported. */ |
| 118 | if (thread->group_leader->mm != thread->mm) |
| 119 | return ERR_PTR(-EINVAL); |
| 120 | |
| 121 | process = find_process(thread); |
| 122 | |
| 123 | return process; |
| 124 | } |
| 125 | |
| 126 | static struct kfd_process *find_process_by_mm(const struct mm_struct *mm) |
| 127 | { |
| 128 | struct kfd_process *process; |
| 129 | |
| 130 | hash_for_each_possible_rcu(kfd_processes_table, process, |
| 131 | kfd_processes, (uintptr_t)mm) |
| 132 | if (process->mm == mm) |
| 133 | return process; |
| 134 | |
| 135 | return NULL; |
| 136 | } |
| 137 | |
| 138 | static struct kfd_process *find_process(const struct task_struct *thread) |
| 139 | { |
| 140 | struct kfd_process *p; |
| 141 | int idx; |
| 142 | |
| 143 | idx = srcu_read_lock(&kfd_processes_srcu); |
| 144 | p = find_process_by_mm(thread->mm); |
| 145 | srcu_read_unlock(&kfd_processes_srcu, idx); |
| 146 | |
| 147 | return p; |
| 148 | } |
| 149 | |
Felix Kuehling | abb208a | 2017-11-27 18:29:52 -0500 | [diff] [blame] | 150 | void kfd_unref_process(struct kfd_process *p) |
| 151 | { |
| 152 | kref_put(&p->ref, kfd_process_ref_release); |
| 153 | } |
| 154 | |
Felix Kuehling | 5ce1068 | 2017-11-27 18:29:51 -0500 | [diff] [blame] | 155 | /* No process locking is needed in this function, because the process |
| 156 | * is not findable any more. We must assume that no other thread is |
| 157 | * using it any more, otherwise we couldn't safely free the process |
| 158 | * structure in the end. |
| 159 | */ |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 160 | static void kfd_process_wq_release(struct work_struct *work) |
| 161 | { |
Felix Kuehling | 5ce1068 | 2017-11-27 18:29:51 -0500 | [diff] [blame] | 162 | struct kfd_process *p = container_of(work, struct kfd_process, |
| 163 | release_work); |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 164 | struct kfd_process_device *pdd, *temp; |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 165 | |
Oded Gabbay | 94a1ee0 | 2015-02-24 10:51:59 +0200 | [diff] [blame] | 166 | pr_debug("Releasing process (pasid %d) in workqueue\n", |
| 167 | p->pasid); |
| 168 | |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 169 | list_for_each_entry_safe(pdd, temp, &p->per_device_data, |
| 170 | per_device_list) { |
Oded Gabbay | 94a1ee0 | 2015-02-24 10:51:59 +0200 | [diff] [blame] | 171 | pr_debug("Releasing pdd (topology id %d) for process (pasid %d) in workqueue\n", |
| 172 | pdd->dev->id, p->pasid); |
| 173 | |
Yong Zhao | 733fa1f | 2017-09-20 18:10:14 -0400 | [diff] [blame] | 174 | if (pdd->bound == PDD_BOUND) |
| 175 | amd_iommu_unbind_pasid(pdd->dev->pdev, p->pasid); |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 176 | |
Yong Zhao | 733fa1f | 2017-09-20 18:10:14 -0400 | [diff] [blame] | 177 | list_del(&pdd->per_device_list); |
Felix Kuehling | 373d708 | 2017-11-14 16:41:19 -0500 | [diff] [blame] | 178 | |
| 179 | if (pdd->qpd.cwsr_kaddr) |
| 180 | free_pages((unsigned long)pdd->qpd.cwsr_kaddr, |
| 181 | get_order(KFD_CWSR_TBA_TMA_SIZE)); |
| 182 | |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 183 | kfree(pdd); |
| 184 | } |
| 185 | |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 186 | kfd_event_free_process(p); |
| 187 | |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 188 | kfd_pasid_free(p->pasid); |
Felix Kuehling | a91e70e | 2017-08-26 02:00:57 -0400 | [diff] [blame] | 189 | kfd_free_process_doorbells(p); |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 190 | |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 191 | mutex_destroy(&p->mutex); |
| 192 | |
Felix Kuehling | c7b1243 | 2017-11-27 18:29:50 -0500 | [diff] [blame] | 193 | put_task_struct(p->lead_thread); |
| 194 | |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 195 | kfree(p); |
Felix Kuehling | 5ce1068 | 2017-11-27 18:29:51 -0500 | [diff] [blame] | 196 | } |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 197 | |
Felix Kuehling | 5ce1068 | 2017-11-27 18:29:51 -0500 | [diff] [blame] | 198 | static void kfd_process_ref_release(struct kref *ref) |
| 199 | { |
| 200 | struct kfd_process *p = container_of(ref, struct kfd_process, ref); |
| 201 | |
| 202 | INIT_WORK(&p->release_work, kfd_process_wq_release); |
| 203 | queue_work(kfd_process_wq, &p->release_work); |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | static void kfd_process_destroy_delayed(struct rcu_head *rcu) |
| 207 | { |
Felix Kuehling | 5ce1068 | 2017-11-27 18:29:51 -0500 | [diff] [blame] | 208 | struct kfd_process *p = container_of(rcu, struct kfd_process, rcu); |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 209 | |
Felix Kuehling | abb208a | 2017-11-27 18:29:52 -0500 | [diff] [blame] | 210 | kfd_unref_process(p); |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | static void kfd_process_notifier_release(struct mmu_notifier *mn, |
| 214 | struct mm_struct *mm) |
| 215 | { |
| 216 | struct kfd_process *p; |
Ben Goz | a82918f | 2015-03-25 13:12:20 +0200 | [diff] [blame] | 217 | struct kfd_process_device *pdd = NULL; |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 218 | |
| 219 | /* |
| 220 | * The kfd_process structure can not be free because the |
| 221 | * mmu_notifier srcu is read locked |
| 222 | */ |
| 223 | p = container_of(mn, struct kfd_process, mmu_notifier); |
Felix Kuehling | 32fa821 | 2017-08-15 23:00:12 -0400 | [diff] [blame] | 224 | if (WARN_ON(p->mm != mm)) |
| 225 | return; |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 226 | |
| 227 | mutex_lock(&kfd_processes_mutex); |
| 228 | hash_del_rcu(&p->kfd_processes); |
| 229 | mutex_unlock(&kfd_processes_mutex); |
| 230 | synchronize_srcu(&kfd_processes_srcu); |
| 231 | |
Ben Goz | 4510204 | 2014-07-17 01:04:10 +0300 | [diff] [blame] | 232 | mutex_lock(&p->mutex); |
| 233 | |
Yair Shachar | 062c567 | 2017-11-01 19:21:29 -0400 | [diff] [blame] | 234 | /* Iterate over all process device data structures and if the |
| 235 | * pdd is in debug mode, we should first force unregistration, |
| 236 | * then we will be able to destroy the queues |
| 237 | */ |
| 238 | list_for_each_entry(pdd, &p->per_device_data, per_device_list) { |
| 239 | struct kfd_dev *dev = pdd->dev; |
| 240 | |
| 241 | mutex_lock(kfd_get_dbgmgr_mutex()); |
| 242 | if (dev && dev->dbgmgr && dev->dbgmgr->pasid == p->pasid) { |
| 243 | if (!kfd_dbgmgr_unregister(dev->dbgmgr, p)) { |
| 244 | kfd_dbgmgr_destroy(dev->dbgmgr); |
| 245 | dev->dbgmgr = NULL; |
| 246 | } |
| 247 | } |
| 248 | mutex_unlock(kfd_get_dbgmgr_mutex()); |
| 249 | } |
| 250 | |
Felix Kuehling | 9fd3f1bf | 2017-09-27 00:09:52 -0400 | [diff] [blame] | 251 | kfd_process_dequeue_from_all_devices(p); |
Ben Goz | 4510204 | 2014-07-17 01:04:10 +0300 | [diff] [blame] | 252 | pqm_uninit(&p->pqm); |
| 253 | |
Felix Kuehling | 5ce1068 | 2017-11-27 18:29:51 -0500 | [diff] [blame] | 254 | /* Indicate to other users that MM is no longer valid */ |
| 255 | p->mm = NULL; |
| 256 | |
Ben Goz | 4510204 | 2014-07-17 01:04:10 +0300 | [diff] [blame] | 257 | mutex_unlock(&p->mutex); |
| 258 | |
Felix Kuehling | 5ce1068 | 2017-11-27 18:29:51 -0500 | [diff] [blame] | 259 | mmu_notifier_unregister_no_release(&p->mmu_notifier, mm); |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 260 | mmu_notifier_call_srcu(&p->rcu, &kfd_process_destroy_delayed); |
| 261 | } |
| 262 | |
| 263 | static const struct mmu_notifier_ops kfd_process_mmu_notifier_ops = { |
| 264 | .release = kfd_process_notifier_release, |
| 265 | }; |
| 266 | |
Felix Kuehling | 373d708 | 2017-11-14 16:41:19 -0500 | [diff] [blame] | 267 | static int kfd_process_init_cwsr(struct kfd_process *p, struct file *filep) |
| 268 | { |
| 269 | int err = 0; |
| 270 | unsigned long offset; |
| 271 | struct kfd_process_device *temp, *pdd = NULL; |
| 272 | struct kfd_dev *dev = NULL; |
| 273 | struct qcm_process_device *qpd = NULL; |
| 274 | |
| 275 | mutex_lock(&p->mutex); |
| 276 | list_for_each_entry_safe(pdd, temp, &p->per_device_data, |
| 277 | per_device_list) { |
| 278 | dev = pdd->dev; |
| 279 | qpd = &pdd->qpd; |
| 280 | if (!dev->cwsr_enabled || qpd->cwsr_kaddr) |
| 281 | continue; |
| 282 | offset = (dev->id | KFD_MMAP_RESERVED_MEM_MASK) << PAGE_SHIFT; |
| 283 | qpd->tba_addr = (int64_t)vm_mmap(filep, 0, |
| 284 | KFD_CWSR_TBA_TMA_SIZE, PROT_READ | PROT_EXEC, |
| 285 | MAP_SHARED, offset); |
| 286 | |
| 287 | if (IS_ERR_VALUE(qpd->tba_addr)) { |
| 288 | pr_err("Failure to set tba address. error -%d.\n", |
| 289 | (int)qpd->tba_addr); |
| 290 | err = qpd->tba_addr; |
| 291 | qpd->tba_addr = 0; |
| 292 | qpd->cwsr_kaddr = NULL; |
| 293 | goto out; |
| 294 | } |
| 295 | |
| 296 | memcpy(qpd->cwsr_kaddr, dev->cwsr_isa, dev->cwsr_isa_size); |
| 297 | |
| 298 | qpd->tma_addr = qpd->tba_addr + KFD_CWSR_TMA_OFFSET; |
| 299 | pr_debug("set tba :0x%llx, tma:0x%llx, cwsr_kaddr:%p for pqm.\n", |
| 300 | qpd->tba_addr, qpd->tma_addr, qpd->cwsr_kaddr); |
| 301 | } |
| 302 | out: |
| 303 | mutex_unlock(&p->mutex); |
| 304 | return err; |
| 305 | } |
| 306 | |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 307 | static struct kfd_process *create_process(const struct task_struct *thread) |
| 308 | { |
| 309 | struct kfd_process *process; |
| 310 | int err = -ENOMEM; |
| 311 | |
| 312 | process = kzalloc(sizeof(*process), GFP_KERNEL); |
| 313 | |
| 314 | if (!process) |
| 315 | goto err_alloc_process; |
| 316 | |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 317 | process->pasid = kfd_pasid_alloc(); |
| 318 | if (process->pasid == 0) |
| 319 | goto err_alloc_pasid; |
| 320 | |
Felix Kuehling | a91e70e | 2017-08-26 02:00:57 -0400 | [diff] [blame] | 321 | if (kfd_alloc_process_doorbells(process) < 0) |
| 322 | goto err_alloc_doorbells; |
| 323 | |
Felix Kuehling | 5ce1068 | 2017-11-27 18:29:51 -0500 | [diff] [blame] | 324 | kref_init(&process->ref); |
| 325 | |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 326 | mutex_init(&process->mutex); |
| 327 | |
| 328 | process->mm = thread->mm; |
| 329 | |
| 330 | /* register notifier */ |
| 331 | process->mmu_notifier.ops = &kfd_process_mmu_notifier_ops; |
| 332 | err = __mmu_notifier_register(&process->mmu_notifier, process->mm); |
| 333 | if (err) |
| 334 | goto err_mmu_notifier; |
| 335 | |
| 336 | hash_add_rcu(kfd_processes_table, &process->kfd_processes, |
| 337 | (uintptr_t)process->mm); |
| 338 | |
| 339 | process->lead_thread = thread->group_leader; |
Felix Kuehling | c7b1243 | 2017-11-27 18:29:50 -0500 | [diff] [blame] | 340 | get_task_struct(process->lead_thread); |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 341 | |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 342 | INIT_LIST_HEAD(&process->per_device_data); |
| 343 | |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 344 | kfd_event_init_process(process); |
| 345 | |
Ben Goz | 4510204 | 2014-07-17 01:04:10 +0300 | [diff] [blame] | 346 | err = pqm_init(&process->pqm, process); |
| 347 | if (err != 0) |
| 348 | goto err_process_pqm_init; |
| 349 | |
Alexey Skidanov | dd59239 | 2014-11-18 13:56:23 +0200 | [diff] [blame] | 350 | /* init process apertures*/ |
Andy Lutomirski | 10f1685 | 2016-03-22 14:25:19 -0700 | [diff] [blame] | 351 | process->is_32bit_user_mode = in_compat_syscall(); |
Dan Carpenter | b312b2b | 2017-06-14 13:58:53 +0300 | [diff] [blame] | 352 | err = kfd_init_apertures(process); |
| 353 | if (err != 0) |
Geert Uytterhoeven | 7a10d63 | 2017-06-01 12:28:38 +0200 | [diff] [blame] | 354 | goto err_init_apertures; |
Alexey Skidanov | dd59239 | 2014-11-18 13:56:23 +0200 | [diff] [blame] | 355 | |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 356 | return process; |
| 357 | |
Geert Uytterhoeven | 7a10d63 | 2017-06-01 12:28:38 +0200 | [diff] [blame] | 358 | err_init_apertures: |
Alexey Skidanov | dd59239 | 2014-11-18 13:56:23 +0200 | [diff] [blame] | 359 | pqm_uninit(&process->pqm); |
Ben Goz | 4510204 | 2014-07-17 01:04:10 +0300 | [diff] [blame] | 360 | err_process_pqm_init: |
| 361 | hash_del_rcu(&process->kfd_processes); |
| 362 | synchronize_rcu(); |
| 363 | mmu_notifier_unregister_no_release(&process->mmu_notifier, process->mm); |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 364 | err_mmu_notifier: |
Oded Gabbay | 7fd5e03 | 2016-06-23 17:54:29 +0300 | [diff] [blame] | 365 | mutex_destroy(&process->mutex); |
Felix Kuehling | a91e70e | 2017-08-26 02:00:57 -0400 | [diff] [blame] | 366 | kfd_free_process_doorbells(process); |
| 367 | err_alloc_doorbells: |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 368 | kfd_pasid_free(process->pasid); |
| 369 | err_alloc_pasid: |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 370 | kfree(process); |
| 371 | err_alloc_process: |
| 372 | return ERR_PTR(err); |
| 373 | } |
| 374 | |
| 375 | struct kfd_process_device *kfd_get_process_device_data(struct kfd_dev *dev, |
Alexey Skidanov | 093c7d8 | 2014-11-18 14:00:04 +0200 | [diff] [blame] | 376 | struct kfd_process *p) |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 377 | { |
| 378 | struct kfd_process_device *pdd = NULL; |
| 379 | |
| 380 | list_for_each_entry(pdd, &p->per_device_data, per_device_list) |
| 381 | if (pdd->dev == dev) |
Yong Zhao | 733fa1f | 2017-09-20 18:10:14 -0400 | [diff] [blame] | 382 | return pdd; |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 383 | |
Yong Zhao | 733fa1f | 2017-09-20 18:10:14 -0400 | [diff] [blame] | 384 | return NULL; |
Alexey Skidanov | 093c7d8 | 2014-11-18 14:00:04 +0200 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | struct kfd_process_device *kfd_create_process_device_data(struct kfd_dev *dev, |
| 388 | struct kfd_process *p) |
| 389 | { |
| 390 | struct kfd_process_device *pdd = NULL; |
| 391 | |
| 392 | pdd = kzalloc(sizeof(*pdd), GFP_KERNEL); |
Felix Kuehling | 2d9b36f | 2017-11-27 18:29:54 -0500 | [diff] [blame^] | 393 | if (!pdd) |
| 394 | return NULL; |
| 395 | |
| 396 | pdd->dev = dev; |
| 397 | INIT_LIST_HEAD(&pdd->qpd.queues_list); |
| 398 | INIT_LIST_HEAD(&pdd->qpd.priv_queue_list); |
| 399 | pdd->qpd.dqm = dev->dqm; |
| 400 | pdd->qpd.pqm = &p->pqm; |
| 401 | pdd->process = p; |
| 402 | pdd->bound = PDD_UNBOUND; |
| 403 | pdd->already_dequeued = false; |
| 404 | list_add(&pdd->per_device_list, &p->per_device_data); |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 405 | |
| 406 | return pdd; |
| 407 | } |
| 408 | |
| 409 | /* |
| 410 | * Direct the IOMMU to bind the process (specifically the pasid->mm) |
| 411 | * to the device. |
| 412 | * Unbinding occurs when the process dies or the device is removed. |
| 413 | * |
| 414 | * Assumes that the process lock is held. |
| 415 | */ |
| 416 | struct kfd_process_device *kfd_bind_process_to_device(struct kfd_dev *dev, |
| 417 | struct kfd_process *p) |
| 418 | { |
Alexey Skidanov | 093c7d8 | 2014-11-18 14:00:04 +0200 | [diff] [blame] | 419 | struct kfd_process_device *pdd; |
Oded Gabbay | b17f068 | 2014-07-17 00:06:27 +0300 | [diff] [blame] | 420 | int err; |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 421 | |
Alexey Skidanov | 093c7d8 | 2014-11-18 14:00:04 +0200 | [diff] [blame] | 422 | pdd = kfd_get_process_device_data(dev, p); |
| 423 | if (!pdd) { |
| 424 | pr_err("Process device data doesn't exist\n"); |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 425 | return ERR_PTR(-ENOMEM); |
Alexey Skidanov | 093c7d8 | 2014-11-18 14:00:04 +0200 | [diff] [blame] | 426 | } |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 427 | |
Yong Zhao | 733fa1f | 2017-09-20 18:10:14 -0400 | [diff] [blame] | 428 | if (pdd->bound == PDD_BOUND) { |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 429 | return pdd; |
Yong Zhao | 733fa1f | 2017-09-20 18:10:14 -0400 | [diff] [blame] | 430 | } else if (unlikely(pdd->bound == PDD_BOUND_SUSPENDED)) { |
| 431 | pr_err("Binding PDD_BOUND_SUSPENDED pdd is unexpected!\n"); |
| 432 | return ERR_PTR(-EINVAL); |
| 433 | } |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 434 | |
Oded Gabbay | b17f068 | 2014-07-17 00:06:27 +0300 | [diff] [blame] | 435 | err = amd_iommu_bind_pasid(dev->pdev, p->pasid, p->lead_thread); |
| 436 | if (err < 0) |
| 437 | return ERR_PTR(err); |
| 438 | |
Yong Zhao | 733fa1f | 2017-09-20 18:10:14 -0400 | [diff] [blame] | 439 | pdd->bound = PDD_BOUND; |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 440 | |
| 441 | return pdd; |
| 442 | } |
| 443 | |
Yong Zhao | 733fa1f | 2017-09-20 18:10:14 -0400 | [diff] [blame] | 444 | /* |
| 445 | * Bind processes do the device that have been temporarily unbound |
| 446 | * (PDD_BOUND_SUSPENDED) in kfd_unbind_processes_from_device. |
| 447 | */ |
| 448 | int kfd_bind_processes_to_device(struct kfd_dev *dev) |
| 449 | { |
| 450 | struct kfd_process_device *pdd; |
| 451 | struct kfd_process *p; |
| 452 | unsigned int temp; |
| 453 | int err = 0; |
| 454 | |
| 455 | int idx = srcu_read_lock(&kfd_processes_srcu); |
| 456 | |
| 457 | hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) { |
| 458 | mutex_lock(&p->mutex); |
| 459 | pdd = kfd_get_process_device_data(dev, p); |
| 460 | if (pdd->bound != PDD_BOUND_SUSPENDED) { |
| 461 | mutex_unlock(&p->mutex); |
| 462 | continue; |
| 463 | } |
| 464 | |
| 465 | err = amd_iommu_bind_pasid(dev->pdev, p->pasid, |
| 466 | p->lead_thread); |
| 467 | if (err < 0) { |
Felix Kuehling | 894a829 | 2017-11-01 19:21:33 -0400 | [diff] [blame] | 468 | pr_err("Unexpected pasid %d binding failure\n", |
Yong Zhao | 733fa1f | 2017-09-20 18:10:14 -0400 | [diff] [blame] | 469 | p->pasid); |
| 470 | mutex_unlock(&p->mutex); |
| 471 | break; |
| 472 | } |
| 473 | |
| 474 | pdd->bound = PDD_BOUND; |
| 475 | mutex_unlock(&p->mutex); |
| 476 | } |
| 477 | |
| 478 | srcu_read_unlock(&kfd_processes_srcu, idx); |
| 479 | |
| 480 | return err; |
| 481 | } |
| 482 | |
| 483 | /* |
Yong Zhao | e2a8e99 | 2017-11-01 19:21:28 -0400 | [diff] [blame] | 484 | * Mark currently bound processes as PDD_BOUND_SUSPENDED. These |
| 485 | * processes will be restored to PDD_BOUND state in |
| 486 | * kfd_bind_processes_to_device. |
Yong Zhao | 733fa1f | 2017-09-20 18:10:14 -0400 | [diff] [blame] | 487 | */ |
| 488 | void kfd_unbind_processes_from_device(struct kfd_dev *dev) |
| 489 | { |
| 490 | struct kfd_process_device *pdd; |
| 491 | struct kfd_process *p; |
Yong Zhao | e2a8e99 | 2017-11-01 19:21:28 -0400 | [diff] [blame] | 492 | unsigned int temp; |
Yong Zhao | 733fa1f | 2017-09-20 18:10:14 -0400 | [diff] [blame] | 493 | |
| 494 | int idx = srcu_read_lock(&kfd_processes_srcu); |
| 495 | |
| 496 | hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) { |
| 497 | mutex_lock(&p->mutex); |
| 498 | pdd = kfd_get_process_device_data(dev, p); |
Yong Zhao | e2a8e99 | 2017-11-01 19:21:28 -0400 | [diff] [blame] | 499 | |
Yong Zhao | 733fa1f | 2017-09-20 18:10:14 -0400 | [diff] [blame] | 500 | if (pdd->bound == PDD_BOUND) |
| 501 | pdd->bound = PDD_BOUND_SUSPENDED; |
| 502 | mutex_unlock(&p->mutex); |
Yong Zhao | 733fa1f | 2017-09-20 18:10:14 -0400 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | srcu_read_unlock(&kfd_processes_srcu, idx); |
| 506 | } |
| 507 | |
| 508 | void kfd_process_iommu_unbind_callback(struct kfd_dev *dev, unsigned int pasid) |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 509 | { |
| 510 | struct kfd_process *p; |
| 511 | struct kfd_process_device *pdd; |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 512 | |
Oded Gabbay | 121b78e | 2016-05-26 08:41:08 +0300 | [diff] [blame] | 513 | /* |
| 514 | * Look for the process that matches the pasid. If there is no such |
| 515 | * process, we either released it in amdkfd's own notifier, or there |
| 516 | * is a bug. Unfortunately, there is no way to tell... |
| 517 | */ |
Edward O'Callaghan | ad16a469 | 2016-09-17 15:01:42 +1000 | [diff] [blame] | 518 | p = kfd_lookup_process_by_pasid(pasid); |
| 519 | if (!p) |
| 520 | return; |
Oded Gabbay | 121b78e | 2016-05-26 08:41:08 +0300 | [diff] [blame] | 521 | |
Edward O'Callaghan | ad16a469 | 2016-09-17 15:01:42 +1000 | [diff] [blame] | 522 | pr_debug("Unbinding process %d from IOMMU\n", pasid); |
Oded Gabbay | 121b78e | 2016-05-26 08:41:08 +0300 | [diff] [blame] | 523 | |
Yair Shachar | 062c567 | 2017-11-01 19:21:29 -0400 | [diff] [blame] | 524 | mutex_lock(kfd_get_dbgmgr_mutex()); |
| 525 | |
| 526 | if (dev->dbgmgr && dev->dbgmgr->pasid == p->pasid) { |
| 527 | if (!kfd_dbgmgr_unregister(dev->dbgmgr, p)) { |
| 528 | kfd_dbgmgr_destroy(dev->dbgmgr); |
| 529 | dev->dbgmgr = NULL; |
| 530 | } |
| 531 | } |
| 532 | |
| 533 | mutex_unlock(kfd_get_dbgmgr_mutex()); |
Oded Gabbay | 121b78e | 2016-05-26 08:41:08 +0300 | [diff] [blame] | 534 | |
Felix Kuehling | abb208a | 2017-11-27 18:29:52 -0500 | [diff] [blame] | 535 | mutex_lock(&p->mutex); |
| 536 | |
Edward O'Callaghan | ad16a469 | 2016-09-17 15:01:42 +1000 | [diff] [blame] | 537 | pdd = kfd_get_process_device_data(dev, p); |
Felix Kuehling | 9fd3f1bf | 2017-09-27 00:09:52 -0400 | [diff] [blame] | 538 | if (pdd) |
| 539 | /* For GPU relying on IOMMU, we need to dequeue here |
| 540 | * when PASID is still bound. |
| 541 | */ |
| 542 | kfd_process_dequeue_from_device(pdd); |
Oded Gabbay | 121b78e | 2016-05-26 08:41:08 +0300 | [diff] [blame] | 543 | |
Edward O'Callaghan | ad16a469 | 2016-09-17 15:01:42 +1000 | [diff] [blame] | 544 | mutex_unlock(&p->mutex); |
Felix Kuehling | abb208a | 2017-11-27 18:29:52 -0500 | [diff] [blame] | 545 | |
| 546 | kfd_unref_process(p); |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 547 | } |
| 548 | |
Kent Russell | 8eabaf5 | 2017-08-15 23:00:04 -0400 | [diff] [blame] | 549 | struct kfd_process_device *kfd_get_first_process_device_data( |
| 550 | struct kfd_process *p) |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 551 | { |
| 552 | return list_first_entry(&p->per_device_data, |
| 553 | struct kfd_process_device, |
| 554 | per_device_list); |
| 555 | } |
| 556 | |
Kent Russell | 8eabaf5 | 2017-08-15 23:00:04 -0400 | [diff] [blame] | 557 | struct kfd_process_device *kfd_get_next_process_device_data( |
| 558 | struct kfd_process *p, |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 559 | struct kfd_process_device *pdd) |
| 560 | { |
| 561 | if (list_is_last(&pdd->per_device_list, &p->per_device_data)) |
| 562 | return NULL; |
| 563 | return list_next_entry(pdd, per_device_list); |
| 564 | } |
| 565 | |
| 566 | bool kfd_has_process_device_data(struct kfd_process *p) |
| 567 | { |
| 568 | return !(list_empty(&p->per_device_data)); |
| 569 | } |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 570 | |
Felix Kuehling | abb208a | 2017-11-27 18:29:52 -0500 | [diff] [blame] | 571 | /* This increments the process->ref counter. */ |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 572 | struct kfd_process *kfd_lookup_process_by_pasid(unsigned int pasid) |
| 573 | { |
Yong Zhao | 82c16b4 | 2017-11-27 18:29:53 -0500 | [diff] [blame] | 574 | struct kfd_process *p, *ret_p = NULL; |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 575 | unsigned int temp; |
| 576 | |
| 577 | int idx = srcu_read_lock(&kfd_processes_srcu); |
| 578 | |
| 579 | hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) { |
| 580 | if (p->pasid == pasid) { |
Felix Kuehling | abb208a | 2017-11-27 18:29:52 -0500 | [diff] [blame] | 581 | kref_get(&p->ref); |
Yong Zhao | 82c16b4 | 2017-11-27 18:29:53 -0500 | [diff] [blame] | 582 | ret_p = p; |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 583 | break; |
| 584 | } |
| 585 | } |
| 586 | |
| 587 | srcu_read_unlock(&kfd_processes_srcu, idx); |
| 588 | |
Yong Zhao | 82c16b4 | 2017-11-27 18:29:53 -0500 | [diff] [blame] | 589 | return ret_p; |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 590 | } |
Felix Kuehling | 373d708 | 2017-11-14 16:41:19 -0500 | [diff] [blame] | 591 | |
| 592 | int kfd_reserved_mem_mmap(struct kfd_process *process, |
| 593 | struct vm_area_struct *vma) |
| 594 | { |
| 595 | struct kfd_dev *dev = kfd_device_by_id(vma->vm_pgoff); |
| 596 | struct kfd_process_device *pdd; |
| 597 | struct qcm_process_device *qpd; |
| 598 | |
| 599 | if (!dev) |
| 600 | return -EINVAL; |
| 601 | if ((vma->vm_end - vma->vm_start) != KFD_CWSR_TBA_TMA_SIZE) { |
| 602 | pr_err("Incorrect CWSR mapping size.\n"); |
| 603 | return -EINVAL; |
| 604 | } |
| 605 | |
| 606 | pdd = kfd_get_process_device_data(dev, process); |
| 607 | if (!pdd) |
| 608 | return -EINVAL; |
| 609 | qpd = &pdd->qpd; |
| 610 | |
| 611 | qpd->cwsr_kaddr = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, |
| 612 | get_order(KFD_CWSR_TBA_TMA_SIZE)); |
| 613 | if (!qpd->cwsr_kaddr) { |
| 614 | pr_err("Error allocating per process CWSR buffer.\n"); |
| 615 | return -ENOMEM; |
| 616 | } |
| 617 | |
| 618 | vma->vm_flags |= VM_IO | VM_DONTCOPY | VM_DONTEXPAND |
| 619 | | VM_NORESERVE | VM_DONTDUMP | VM_PFNMAP; |
| 620 | /* Mapping pages to user process */ |
| 621 | return remap_pfn_range(vma, vma->vm_start, |
| 622 | PFN_DOWN(__pa(qpd->cwsr_kaddr)), |
| 623 | KFD_CWSR_TBA_TMA_SIZE, vma->vm_page_prot); |
| 624 | } |
Felix Kuehling | 851a645 | 2017-11-27 18:29:49 -0500 | [diff] [blame] | 625 | |
| 626 | #if defined(CONFIG_DEBUG_FS) |
| 627 | |
| 628 | int kfd_debugfs_mqds_by_process(struct seq_file *m, void *data) |
| 629 | { |
| 630 | struct kfd_process *p; |
| 631 | unsigned int temp; |
| 632 | int r = 0; |
| 633 | |
| 634 | int idx = srcu_read_lock(&kfd_processes_srcu); |
| 635 | |
| 636 | hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) { |
| 637 | seq_printf(m, "Process %d PASID %d:\n", |
| 638 | p->lead_thread->tgid, p->pasid); |
| 639 | |
| 640 | mutex_lock(&p->mutex); |
| 641 | r = pqm_debugfs_mqds(m, &p->pqm); |
| 642 | mutex_unlock(&p->mutex); |
| 643 | |
| 644 | if (r) |
| 645 | break; |
| 646 | } |
| 647 | |
| 648 | srcu_read_unlock(&kfd_processes_srcu, idx); |
| 649 | |
| 650 | return r; |
| 651 | } |
| 652 | |
| 653 | #endif |