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> |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 27 | #include <linux/slab.h> |
Oded Gabbay | b17f068 | 2014-07-17 00:06:27 +0300 | [diff] [blame] | 28 | #include <linux/amd-iommu.h> |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 29 | #include <linux/notifier.h> |
Alexey Skidanov | dd59239 | 2014-11-18 13:56:23 +0200 | [diff] [blame] | 30 | #include <linux/compat.h> |
| 31 | |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 32 | struct mm_struct; |
| 33 | |
| 34 | #include "kfd_priv.h" |
Ben Goz | c3447e8 | 2015-05-20 18:05:44 +0300 | [diff] [blame] | 35 | #include "kfd_dbgmgr.h" |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 36 | |
| 37 | /* |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 38 | * List of struct kfd_process (field kfd_process). |
| 39 | * Unique/indexed by mm_struct* |
| 40 | */ |
| 41 | #define KFD_PROCESS_TABLE_SIZE 5 /* bits: 32 entries */ |
| 42 | static DEFINE_HASHTABLE(kfd_processes_table, KFD_PROCESS_TABLE_SIZE); |
| 43 | static DEFINE_MUTEX(kfd_processes_mutex); |
| 44 | |
| 45 | DEFINE_STATIC_SRCU(kfd_processes_srcu); |
| 46 | |
| 47 | static struct workqueue_struct *kfd_process_wq; |
| 48 | |
| 49 | struct kfd_process_release_work { |
| 50 | struct work_struct kfd_work; |
| 51 | struct kfd_process *p; |
| 52 | }; |
| 53 | |
| 54 | static struct kfd_process *find_process(const struct task_struct *thread); |
| 55 | static struct kfd_process *create_process(const struct task_struct *thread); |
| 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 | |
| 71 | struct kfd_process *kfd_create_process(const struct task_struct *thread) |
| 72 | { |
| 73 | struct kfd_process *process; |
| 74 | |
Kent Russell | 4eacc26b | 2017-08-15 23:00:06 -0400 | [diff] [blame] | 75 | if (!thread->mm) |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 76 | return ERR_PTR(-EINVAL); |
| 77 | |
| 78 | /* Only the pthreads threading model is supported. */ |
| 79 | if (thread->group_leader->mm != thread->mm) |
| 80 | return ERR_PTR(-EINVAL); |
| 81 | |
| 82 | /* Take mmap_sem because we call __mmu_notifier_register inside */ |
| 83 | down_write(&thread->mm->mmap_sem); |
| 84 | |
| 85 | /* |
| 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 Russell | 79775b6 | 2017-08-15 23:00:05 -0400 | [diff] [blame] | 95 | pr_debug("Process already found\n"); |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 96 | |
| 97 | if (!process) |
| 98 | process = create_process(thread); |
| 99 | |
| 100 | mutex_unlock(&kfd_processes_mutex); |
| 101 | |
| 102 | up_write(&thread->mm->mmap_sem); |
| 103 | |
| 104 | return process; |
| 105 | } |
| 106 | |
| 107 | struct kfd_process *kfd_get_process(const struct task_struct *thread) |
| 108 | { |
| 109 | struct kfd_process *process; |
| 110 | |
Kent Russell | 4eacc26b | 2017-08-15 23:00:06 -0400 | [diff] [blame] | 111 | if (!thread->mm) |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 112 | return ERR_PTR(-EINVAL); |
| 113 | |
| 114 | /* Only the pthreads threading model is supported. */ |
| 115 | if (thread->group_leader->mm != thread->mm) |
| 116 | return ERR_PTR(-EINVAL); |
| 117 | |
| 118 | process = find_process(thread); |
| 119 | |
| 120 | return process; |
| 121 | } |
| 122 | |
| 123 | static struct kfd_process *find_process_by_mm(const struct mm_struct *mm) |
| 124 | { |
| 125 | struct kfd_process *process; |
| 126 | |
| 127 | hash_for_each_possible_rcu(kfd_processes_table, process, |
| 128 | kfd_processes, (uintptr_t)mm) |
| 129 | if (process->mm == mm) |
| 130 | return process; |
| 131 | |
| 132 | return NULL; |
| 133 | } |
| 134 | |
| 135 | static struct kfd_process *find_process(const struct task_struct *thread) |
| 136 | { |
| 137 | struct kfd_process *p; |
| 138 | int idx; |
| 139 | |
| 140 | idx = srcu_read_lock(&kfd_processes_srcu); |
| 141 | p = find_process_by_mm(thread->mm); |
| 142 | srcu_read_unlock(&kfd_processes_srcu, idx); |
| 143 | |
| 144 | return p; |
| 145 | } |
| 146 | |
| 147 | static void kfd_process_wq_release(struct work_struct *work) |
| 148 | { |
| 149 | struct kfd_process_release_work *my_work; |
| 150 | struct kfd_process_device *pdd, *temp; |
| 151 | struct kfd_process *p; |
| 152 | |
| 153 | my_work = (struct kfd_process_release_work *) work; |
| 154 | |
| 155 | p = my_work->p; |
| 156 | |
Oded Gabbay | 94a1ee0 | 2015-02-24 10:51:59 +0200 | [diff] [blame] | 157 | pr_debug("Releasing process (pasid %d) in workqueue\n", |
| 158 | p->pasid); |
| 159 | |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 160 | mutex_lock(&p->mutex); |
| 161 | |
| 162 | list_for_each_entry_safe(pdd, temp, &p->per_device_data, |
| 163 | per_device_list) { |
Oded Gabbay | 94a1ee0 | 2015-02-24 10:51:59 +0200 | [diff] [blame] | 164 | pr_debug("Releasing pdd (topology id %d) for process (pasid %d) in workqueue\n", |
| 165 | pdd->dev->id, p->pasid); |
| 166 | |
Yong Zhao | 733fa1f | 2017-09-20 18:10:14 -0400 | [diff] [blame] | 167 | if (pdd->bound == PDD_BOUND) |
| 168 | amd_iommu_unbind_pasid(pdd->dev->pdev, p->pasid); |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 169 | |
Yong Zhao | 733fa1f | 2017-09-20 18:10:14 -0400 | [diff] [blame] | 170 | list_del(&pdd->per_device_list); |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 171 | kfree(pdd); |
| 172 | } |
| 173 | |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 174 | kfd_event_free_process(p); |
| 175 | |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 176 | kfd_pasid_free(p->pasid); |
Felix Kuehling | a91e70e | 2017-08-26 02:00:57 -0400 | [diff] [blame] | 177 | kfd_free_process_doorbells(p); |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 178 | |
| 179 | mutex_unlock(&p->mutex); |
| 180 | |
| 181 | mutex_destroy(&p->mutex); |
| 182 | |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 183 | kfree(p); |
| 184 | |
Amitoj Kaur Chawla | 642f0f2 | 2016-01-25 23:03:57 +0530 | [diff] [blame] | 185 | kfree(work); |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | static void kfd_process_destroy_delayed(struct rcu_head *rcu) |
| 189 | { |
| 190 | struct kfd_process_release_work *work; |
| 191 | struct kfd_process *p; |
| 192 | |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 193 | p = container_of(rcu, struct kfd_process, rcu); |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 194 | |
| 195 | mmdrop(p->mm); |
| 196 | |
Firo Yang | 1549fcd | 2015-04-23 17:58:05 +0800 | [diff] [blame] | 197 | work = kmalloc(sizeof(struct kfd_process_release_work), GFP_ATOMIC); |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 198 | |
| 199 | if (work) { |
| 200 | INIT_WORK((struct work_struct *) work, kfd_process_wq_release); |
| 201 | work->p = p; |
| 202 | queue_work(kfd_process_wq, (struct work_struct *) work); |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | static void kfd_process_notifier_release(struct mmu_notifier *mn, |
| 207 | struct mm_struct *mm) |
| 208 | { |
| 209 | struct kfd_process *p; |
Ben Goz | a82918f | 2015-03-25 13:12:20 +0200 | [diff] [blame] | 210 | struct kfd_process_device *pdd = NULL; |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 211 | |
| 212 | /* |
| 213 | * The kfd_process structure can not be free because the |
| 214 | * mmu_notifier srcu is read locked |
| 215 | */ |
| 216 | p = container_of(mn, struct kfd_process, mmu_notifier); |
Felix Kuehling | 32fa821 | 2017-08-15 23:00:12 -0400 | [diff] [blame] | 217 | if (WARN_ON(p->mm != mm)) |
| 218 | return; |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 219 | |
| 220 | mutex_lock(&kfd_processes_mutex); |
| 221 | hash_del_rcu(&p->kfd_processes); |
| 222 | mutex_unlock(&kfd_processes_mutex); |
| 223 | synchronize_srcu(&kfd_processes_srcu); |
| 224 | |
Ben Goz | 4510204 | 2014-07-17 01:04:10 +0300 | [diff] [blame] | 225 | mutex_lock(&p->mutex); |
| 226 | |
Yair Shachar | 062c567 | 2017-11-01 19:21:29 -0400 | [diff] [blame] | 227 | /* Iterate over all process device data structures and if the |
| 228 | * pdd is in debug mode, we should first force unregistration, |
| 229 | * then we will be able to destroy the queues |
| 230 | */ |
| 231 | list_for_each_entry(pdd, &p->per_device_data, per_device_list) { |
| 232 | struct kfd_dev *dev = pdd->dev; |
| 233 | |
| 234 | mutex_lock(kfd_get_dbgmgr_mutex()); |
| 235 | if (dev && dev->dbgmgr && dev->dbgmgr->pasid == p->pasid) { |
| 236 | if (!kfd_dbgmgr_unregister(dev->dbgmgr, p)) { |
| 237 | kfd_dbgmgr_destroy(dev->dbgmgr); |
| 238 | dev->dbgmgr = NULL; |
| 239 | } |
| 240 | } |
| 241 | mutex_unlock(kfd_get_dbgmgr_mutex()); |
| 242 | } |
| 243 | |
Felix Kuehling | 9fd3f1bf | 2017-09-27 00:09:52 -0400 | [diff] [blame] | 244 | kfd_process_dequeue_from_all_devices(p); |
Ben Goz | 4510204 | 2014-07-17 01:04:10 +0300 | [diff] [blame] | 245 | pqm_uninit(&p->pqm); |
| 246 | |
| 247 | mutex_unlock(&p->mutex); |
| 248 | |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 249 | /* |
| 250 | * Because we drop mm_count inside kfd_process_destroy_delayed |
| 251 | * and because the mmu_notifier_unregister function also drop |
| 252 | * mm_count we need to take an extra count here. |
| 253 | */ |
Vegard Nossum | f1f1007 | 2017-02-27 14:30:07 -0800 | [diff] [blame] | 254 | mmgrab(p->mm); |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 255 | mmu_notifier_unregister_no_release(&p->mmu_notifier, p->mm); |
| 256 | mmu_notifier_call_srcu(&p->rcu, &kfd_process_destroy_delayed); |
| 257 | } |
| 258 | |
| 259 | static const struct mmu_notifier_ops kfd_process_mmu_notifier_ops = { |
| 260 | .release = kfd_process_notifier_release, |
| 261 | }; |
| 262 | |
| 263 | static struct kfd_process *create_process(const struct task_struct *thread) |
| 264 | { |
| 265 | struct kfd_process *process; |
| 266 | int err = -ENOMEM; |
| 267 | |
| 268 | process = kzalloc(sizeof(*process), GFP_KERNEL); |
| 269 | |
| 270 | if (!process) |
| 271 | goto err_alloc_process; |
| 272 | |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 273 | process->pasid = kfd_pasid_alloc(); |
| 274 | if (process->pasid == 0) |
| 275 | goto err_alloc_pasid; |
| 276 | |
Felix Kuehling | a91e70e | 2017-08-26 02:00:57 -0400 | [diff] [blame] | 277 | if (kfd_alloc_process_doorbells(process) < 0) |
| 278 | goto err_alloc_doorbells; |
| 279 | |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 280 | mutex_init(&process->mutex); |
| 281 | |
| 282 | process->mm = thread->mm; |
| 283 | |
| 284 | /* register notifier */ |
| 285 | process->mmu_notifier.ops = &kfd_process_mmu_notifier_ops; |
| 286 | err = __mmu_notifier_register(&process->mmu_notifier, process->mm); |
| 287 | if (err) |
| 288 | goto err_mmu_notifier; |
| 289 | |
| 290 | hash_add_rcu(kfd_processes_table, &process->kfd_processes, |
| 291 | (uintptr_t)process->mm); |
| 292 | |
| 293 | process->lead_thread = thread->group_leader; |
| 294 | |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 295 | INIT_LIST_HEAD(&process->per_device_data); |
| 296 | |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 297 | kfd_event_init_process(process); |
| 298 | |
Ben Goz | 4510204 | 2014-07-17 01:04:10 +0300 | [diff] [blame] | 299 | err = pqm_init(&process->pqm, process); |
| 300 | if (err != 0) |
| 301 | goto err_process_pqm_init; |
| 302 | |
Alexey Skidanov | dd59239 | 2014-11-18 13:56:23 +0200 | [diff] [blame] | 303 | /* init process apertures*/ |
Andy Lutomirski | 10f1685 | 2016-03-22 14:25:19 -0700 | [diff] [blame] | 304 | process->is_32bit_user_mode = in_compat_syscall(); |
Dan Carpenter | b312b2b | 2017-06-14 13:58:53 +0300 | [diff] [blame] | 305 | err = kfd_init_apertures(process); |
| 306 | if (err != 0) |
Geert Uytterhoeven | 7a10d63 | 2017-06-01 12:28:38 +0200 | [diff] [blame] | 307 | goto err_init_apertures; |
Alexey Skidanov | dd59239 | 2014-11-18 13:56:23 +0200 | [diff] [blame] | 308 | |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 309 | return process; |
| 310 | |
Geert Uytterhoeven | 7a10d63 | 2017-06-01 12:28:38 +0200 | [diff] [blame] | 311 | err_init_apertures: |
Alexey Skidanov | dd59239 | 2014-11-18 13:56:23 +0200 | [diff] [blame] | 312 | pqm_uninit(&process->pqm); |
Ben Goz | 4510204 | 2014-07-17 01:04:10 +0300 | [diff] [blame] | 313 | err_process_pqm_init: |
| 314 | hash_del_rcu(&process->kfd_processes); |
| 315 | synchronize_rcu(); |
| 316 | mmu_notifier_unregister_no_release(&process->mmu_notifier, process->mm); |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 317 | err_mmu_notifier: |
Oded Gabbay | 7fd5e03 | 2016-06-23 17:54:29 +0300 | [diff] [blame] | 318 | mutex_destroy(&process->mutex); |
Felix Kuehling | a91e70e | 2017-08-26 02:00:57 -0400 | [diff] [blame] | 319 | kfd_free_process_doorbells(process); |
| 320 | err_alloc_doorbells: |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 321 | kfd_pasid_free(process->pasid); |
| 322 | err_alloc_pasid: |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 323 | kfree(process); |
| 324 | err_alloc_process: |
| 325 | return ERR_PTR(err); |
| 326 | } |
| 327 | |
| 328 | 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] | 329 | struct kfd_process *p) |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 330 | { |
| 331 | struct kfd_process_device *pdd = NULL; |
| 332 | |
| 333 | list_for_each_entry(pdd, &p->per_device_data, per_device_list) |
| 334 | if (pdd->dev == dev) |
Yong Zhao | 733fa1f | 2017-09-20 18:10:14 -0400 | [diff] [blame] | 335 | return pdd; |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 336 | |
Yong Zhao | 733fa1f | 2017-09-20 18:10:14 -0400 | [diff] [blame] | 337 | return NULL; |
Alexey Skidanov | 093c7d8 | 2014-11-18 14:00:04 +0200 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | struct kfd_process_device *kfd_create_process_device_data(struct kfd_dev *dev, |
| 341 | struct kfd_process *p) |
| 342 | { |
| 343 | struct kfd_process_device *pdd = NULL; |
| 344 | |
| 345 | pdd = kzalloc(sizeof(*pdd), GFP_KERNEL); |
| 346 | if (pdd != NULL) { |
| 347 | pdd->dev = dev; |
| 348 | INIT_LIST_HEAD(&pdd->qpd.queues_list); |
| 349 | INIT_LIST_HEAD(&pdd->qpd.priv_queue_list); |
| 350 | pdd->qpd.dqm = dev->dqm; |
Felix Kuehling | b20cd0d | 2017-11-14 16:41:17 -0500 | [diff] [blame] | 351 | pdd->qpd.pqm = &p->pqm; |
Felix Kuehling | 9fd3f1bf | 2017-09-27 00:09:52 -0400 | [diff] [blame] | 352 | pdd->process = p; |
Yong Zhao | 733fa1f | 2017-09-20 18:10:14 -0400 | [diff] [blame] | 353 | pdd->bound = PDD_UNBOUND; |
Felix Kuehling | 9fd3f1bf | 2017-09-27 00:09:52 -0400 | [diff] [blame] | 354 | pdd->already_dequeued = false; |
Alexey Skidanov | 093c7d8 | 2014-11-18 14:00:04 +0200 | [diff] [blame] | 355 | list_add(&pdd->per_device_list, &p->per_device_data); |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | return pdd; |
| 359 | } |
| 360 | |
| 361 | /* |
| 362 | * Direct the IOMMU to bind the process (specifically the pasid->mm) |
| 363 | * to the device. |
| 364 | * Unbinding occurs when the process dies or the device is removed. |
| 365 | * |
| 366 | * Assumes that the process lock is held. |
| 367 | */ |
| 368 | struct kfd_process_device *kfd_bind_process_to_device(struct kfd_dev *dev, |
| 369 | struct kfd_process *p) |
| 370 | { |
Alexey Skidanov | 093c7d8 | 2014-11-18 14:00:04 +0200 | [diff] [blame] | 371 | struct kfd_process_device *pdd; |
Oded Gabbay | b17f068 | 2014-07-17 00:06:27 +0300 | [diff] [blame] | 372 | int err; |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 373 | |
Alexey Skidanov | 093c7d8 | 2014-11-18 14:00:04 +0200 | [diff] [blame] | 374 | pdd = kfd_get_process_device_data(dev, p); |
| 375 | if (!pdd) { |
| 376 | pr_err("Process device data doesn't exist\n"); |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 377 | return ERR_PTR(-ENOMEM); |
Alexey Skidanov | 093c7d8 | 2014-11-18 14:00:04 +0200 | [diff] [blame] | 378 | } |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 379 | |
Yong Zhao | 733fa1f | 2017-09-20 18:10:14 -0400 | [diff] [blame] | 380 | if (pdd->bound == PDD_BOUND) { |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 381 | return pdd; |
Yong Zhao | 733fa1f | 2017-09-20 18:10:14 -0400 | [diff] [blame] | 382 | } else if (unlikely(pdd->bound == PDD_BOUND_SUSPENDED)) { |
| 383 | pr_err("Binding PDD_BOUND_SUSPENDED pdd is unexpected!\n"); |
| 384 | return ERR_PTR(-EINVAL); |
| 385 | } |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 386 | |
Oded Gabbay | b17f068 | 2014-07-17 00:06:27 +0300 | [diff] [blame] | 387 | err = amd_iommu_bind_pasid(dev->pdev, p->pasid, p->lead_thread); |
| 388 | if (err < 0) |
| 389 | return ERR_PTR(err); |
| 390 | |
Yong Zhao | 733fa1f | 2017-09-20 18:10:14 -0400 | [diff] [blame] | 391 | pdd->bound = PDD_BOUND; |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 392 | |
| 393 | return pdd; |
| 394 | } |
| 395 | |
Yong Zhao | 733fa1f | 2017-09-20 18:10:14 -0400 | [diff] [blame] | 396 | /* |
| 397 | * Bind processes do the device that have been temporarily unbound |
| 398 | * (PDD_BOUND_SUSPENDED) in kfd_unbind_processes_from_device. |
| 399 | */ |
| 400 | int kfd_bind_processes_to_device(struct kfd_dev *dev) |
| 401 | { |
| 402 | struct kfd_process_device *pdd; |
| 403 | struct kfd_process *p; |
| 404 | unsigned int temp; |
| 405 | int err = 0; |
| 406 | |
| 407 | int idx = srcu_read_lock(&kfd_processes_srcu); |
| 408 | |
| 409 | hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) { |
| 410 | mutex_lock(&p->mutex); |
| 411 | pdd = kfd_get_process_device_data(dev, p); |
| 412 | if (pdd->bound != PDD_BOUND_SUSPENDED) { |
| 413 | mutex_unlock(&p->mutex); |
| 414 | continue; |
| 415 | } |
| 416 | |
| 417 | err = amd_iommu_bind_pasid(dev->pdev, p->pasid, |
| 418 | p->lead_thread); |
| 419 | if (err < 0) { |
Felix Kuehling | 894a829 | 2017-11-01 19:21:33 -0400 | [diff] [blame] | 420 | pr_err("Unexpected pasid %d binding failure\n", |
Yong Zhao | 733fa1f | 2017-09-20 18:10:14 -0400 | [diff] [blame] | 421 | p->pasid); |
| 422 | mutex_unlock(&p->mutex); |
| 423 | break; |
| 424 | } |
| 425 | |
| 426 | pdd->bound = PDD_BOUND; |
| 427 | mutex_unlock(&p->mutex); |
| 428 | } |
| 429 | |
| 430 | srcu_read_unlock(&kfd_processes_srcu, idx); |
| 431 | |
| 432 | return err; |
| 433 | } |
| 434 | |
| 435 | /* |
Yong Zhao | e2a8e99 | 2017-11-01 19:21:28 -0400 | [diff] [blame] | 436 | * Mark currently bound processes as PDD_BOUND_SUSPENDED. These |
| 437 | * processes will be restored to PDD_BOUND state in |
| 438 | * kfd_bind_processes_to_device. |
Yong Zhao | 733fa1f | 2017-09-20 18:10:14 -0400 | [diff] [blame] | 439 | */ |
| 440 | void kfd_unbind_processes_from_device(struct kfd_dev *dev) |
| 441 | { |
| 442 | struct kfd_process_device *pdd; |
| 443 | struct kfd_process *p; |
Yong Zhao | e2a8e99 | 2017-11-01 19:21:28 -0400 | [diff] [blame] | 444 | unsigned int temp; |
Yong Zhao | 733fa1f | 2017-09-20 18:10:14 -0400 | [diff] [blame] | 445 | |
| 446 | int idx = srcu_read_lock(&kfd_processes_srcu); |
| 447 | |
| 448 | hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) { |
| 449 | mutex_lock(&p->mutex); |
| 450 | pdd = kfd_get_process_device_data(dev, p); |
Yong Zhao | e2a8e99 | 2017-11-01 19:21:28 -0400 | [diff] [blame] | 451 | |
Yong Zhao | 733fa1f | 2017-09-20 18:10:14 -0400 | [diff] [blame] | 452 | if (pdd->bound == PDD_BOUND) |
| 453 | pdd->bound = PDD_BOUND_SUSPENDED; |
| 454 | mutex_unlock(&p->mutex); |
Yong Zhao | 733fa1f | 2017-09-20 18:10:14 -0400 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | srcu_read_unlock(&kfd_processes_srcu, idx); |
| 458 | } |
| 459 | |
| 460 | 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] | 461 | { |
| 462 | struct kfd_process *p; |
| 463 | struct kfd_process_device *pdd; |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 464 | |
Oded Gabbay | 121b78e | 2016-05-26 08:41:08 +0300 | [diff] [blame] | 465 | /* |
| 466 | * Look for the process that matches the pasid. If there is no such |
| 467 | * process, we either released it in amdkfd's own notifier, or there |
| 468 | * is a bug. Unfortunately, there is no way to tell... |
| 469 | */ |
Edward O'Callaghan | ad16a469 | 2016-09-17 15:01:42 +1000 | [diff] [blame] | 470 | p = kfd_lookup_process_by_pasid(pasid); |
| 471 | if (!p) |
| 472 | return; |
Oded Gabbay | 121b78e | 2016-05-26 08:41:08 +0300 | [diff] [blame] | 473 | |
Edward O'Callaghan | ad16a469 | 2016-09-17 15:01:42 +1000 | [diff] [blame] | 474 | pr_debug("Unbinding process %d from IOMMU\n", pasid); |
Oded Gabbay | 121b78e | 2016-05-26 08:41:08 +0300 | [diff] [blame] | 475 | |
Yair Shachar | 062c567 | 2017-11-01 19:21:29 -0400 | [diff] [blame] | 476 | mutex_lock(kfd_get_dbgmgr_mutex()); |
| 477 | |
| 478 | if (dev->dbgmgr && dev->dbgmgr->pasid == p->pasid) { |
| 479 | if (!kfd_dbgmgr_unregister(dev->dbgmgr, p)) { |
| 480 | kfd_dbgmgr_destroy(dev->dbgmgr); |
| 481 | dev->dbgmgr = NULL; |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | mutex_unlock(kfd_get_dbgmgr_mutex()); |
Oded Gabbay | 121b78e | 2016-05-26 08:41:08 +0300 | [diff] [blame] | 486 | |
Edward O'Callaghan | ad16a469 | 2016-09-17 15:01:42 +1000 | [diff] [blame] | 487 | pdd = kfd_get_process_device_data(dev, p); |
Felix Kuehling | 9fd3f1bf | 2017-09-27 00:09:52 -0400 | [diff] [blame] | 488 | if (pdd) |
| 489 | /* For GPU relying on IOMMU, we need to dequeue here |
| 490 | * when PASID is still bound. |
| 491 | */ |
| 492 | kfd_process_dequeue_from_device(pdd); |
Oded Gabbay | 121b78e | 2016-05-26 08:41:08 +0300 | [diff] [blame] | 493 | |
Edward O'Callaghan | ad16a469 | 2016-09-17 15:01:42 +1000 | [diff] [blame] | 494 | mutex_unlock(&p->mutex); |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 495 | } |
| 496 | |
Kent Russell | 8eabaf5 | 2017-08-15 23:00:04 -0400 | [diff] [blame] | 497 | struct kfd_process_device *kfd_get_first_process_device_data( |
| 498 | struct kfd_process *p) |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 499 | { |
| 500 | return list_first_entry(&p->per_device_data, |
| 501 | struct kfd_process_device, |
| 502 | per_device_list); |
| 503 | } |
| 504 | |
Kent Russell | 8eabaf5 | 2017-08-15 23:00:04 -0400 | [diff] [blame] | 505 | struct kfd_process_device *kfd_get_next_process_device_data( |
| 506 | struct kfd_process *p, |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 507 | struct kfd_process_device *pdd) |
| 508 | { |
| 509 | if (list_is_last(&pdd->per_device_list, &p->per_device_data)) |
| 510 | return NULL; |
| 511 | return list_next_entry(pdd, per_device_list); |
| 512 | } |
| 513 | |
| 514 | bool kfd_has_process_device_data(struct kfd_process *p) |
| 515 | { |
| 516 | return !(list_empty(&p->per_device_data)); |
| 517 | } |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 518 | |
| 519 | /* This returns with process->mutex locked. */ |
| 520 | struct kfd_process *kfd_lookup_process_by_pasid(unsigned int pasid) |
| 521 | { |
| 522 | struct kfd_process *p; |
| 523 | unsigned int temp; |
| 524 | |
| 525 | int idx = srcu_read_lock(&kfd_processes_srcu); |
| 526 | |
| 527 | hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) { |
| 528 | if (p->pasid == pasid) { |
| 529 | mutex_lock(&p->mutex); |
| 530 | break; |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | srcu_read_unlock(&kfd_processes_srcu, idx); |
| 535 | |
| 536 | return p; |
| 537 | } |