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 | |
Felix Kuehling | 9fd3f1bf | 2017-09-27 00:09:52 -0400 | [diff] [blame] | 227 | kfd_process_dequeue_from_all_devices(p); |
Ben Goz | 4510204 | 2014-07-17 01:04:10 +0300 | [diff] [blame] | 228 | pqm_uninit(&p->pqm); |
| 229 | |
Ben Goz | a82918f | 2015-03-25 13:12:20 +0200 | [diff] [blame] | 230 | /* Iterate over all process device data structure and check |
Felix Kuehling | 9fd3f1bf | 2017-09-27 00:09:52 -0400 | [diff] [blame] | 231 | * if we should delete debug managers |
Oded Gabbay | bc4755a | 2016-05-26 08:41:48 +0300 | [diff] [blame] | 232 | */ |
Felix Kuehling | 9fd3f1bf | 2017-09-27 00:09:52 -0400 | [diff] [blame] | 233 | list_for_each_entry(pdd, &p->per_device_data, per_device_list) |
Oded Gabbay | bc4755a | 2016-05-26 08:41:48 +0300 | [diff] [blame] | 234 | if ((pdd->dev->dbgmgr) && |
| 235 | (pdd->dev->dbgmgr->pasid == p->pasid)) |
| 236 | kfd_dbgmgr_destroy(pdd->dev->dbgmgr); |
| 237 | |
Ben Goz | 4510204 | 2014-07-17 01:04:10 +0300 | [diff] [blame] | 238 | mutex_unlock(&p->mutex); |
| 239 | |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 240 | /* |
| 241 | * Because we drop mm_count inside kfd_process_destroy_delayed |
| 242 | * and because the mmu_notifier_unregister function also drop |
| 243 | * mm_count we need to take an extra count here. |
| 244 | */ |
Vegard Nossum | f1f1007 | 2017-02-27 14:30:07 -0800 | [diff] [blame] | 245 | mmgrab(p->mm); |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 246 | mmu_notifier_unregister_no_release(&p->mmu_notifier, p->mm); |
| 247 | mmu_notifier_call_srcu(&p->rcu, &kfd_process_destroy_delayed); |
| 248 | } |
| 249 | |
| 250 | static const struct mmu_notifier_ops kfd_process_mmu_notifier_ops = { |
| 251 | .release = kfd_process_notifier_release, |
| 252 | }; |
| 253 | |
| 254 | static struct kfd_process *create_process(const struct task_struct *thread) |
| 255 | { |
| 256 | struct kfd_process *process; |
| 257 | int err = -ENOMEM; |
| 258 | |
| 259 | process = kzalloc(sizeof(*process), GFP_KERNEL); |
| 260 | |
| 261 | if (!process) |
| 262 | goto err_alloc_process; |
| 263 | |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 264 | process->pasid = kfd_pasid_alloc(); |
| 265 | if (process->pasid == 0) |
| 266 | goto err_alloc_pasid; |
| 267 | |
Felix Kuehling | a91e70e | 2017-08-26 02:00:57 -0400 | [diff] [blame] | 268 | if (kfd_alloc_process_doorbells(process) < 0) |
| 269 | goto err_alloc_doorbells; |
| 270 | |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 271 | mutex_init(&process->mutex); |
| 272 | |
| 273 | process->mm = thread->mm; |
| 274 | |
| 275 | /* register notifier */ |
| 276 | process->mmu_notifier.ops = &kfd_process_mmu_notifier_ops; |
| 277 | err = __mmu_notifier_register(&process->mmu_notifier, process->mm); |
| 278 | if (err) |
| 279 | goto err_mmu_notifier; |
| 280 | |
| 281 | hash_add_rcu(kfd_processes_table, &process->kfd_processes, |
| 282 | (uintptr_t)process->mm); |
| 283 | |
| 284 | process->lead_thread = thread->group_leader; |
| 285 | |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 286 | INIT_LIST_HEAD(&process->per_device_data); |
| 287 | |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 288 | kfd_event_init_process(process); |
| 289 | |
Ben Goz | 4510204 | 2014-07-17 01:04:10 +0300 | [diff] [blame] | 290 | err = pqm_init(&process->pqm, process); |
| 291 | if (err != 0) |
| 292 | goto err_process_pqm_init; |
| 293 | |
Alexey Skidanov | dd59239 | 2014-11-18 13:56:23 +0200 | [diff] [blame] | 294 | /* init process apertures*/ |
Andy Lutomirski | 10f1685 | 2016-03-22 14:25:19 -0700 | [diff] [blame] | 295 | process->is_32bit_user_mode = in_compat_syscall(); |
Dan Carpenter | b312b2b | 2017-06-14 13:58:53 +0300 | [diff] [blame] | 296 | err = kfd_init_apertures(process); |
| 297 | if (err != 0) |
Geert Uytterhoeven | 7a10d63 | 2017-06-01 12:28:38 +0200 | [diff] [blame] | 298 | goto err_init_apertures; |
Alexey Skidanov | dd59239 | 2014-11-18 13:56:23 +0200 | [diff] [blame] | 299 | |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 300 | return process; |
| 301 | |
Geert Uytterhoeven | 7a10d63 | 2017-06-01 12:28:38 +0200 | [diff] [blame] | 302 | err_init_apertures: |
Alexey Skidanov | dd59239 | 2014-11-18 13:56:23 +0200 | [diff] [blame] | 303 | pqm_uninit(&process->pqm); |
Ben Goz | 4510204 | 2014-07-17 01:04:10 +0300 | [diff] [blame] | 304 | err_process_pqm_init: |
| 305 | hash_del_rcu(&process->kfd_processes); |
| 306 | synchronize_rcu(); |
| 307 | mmu_notifier_unregister_no_release(&process->mmu_notifier, process->mm); |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 308 | err_mmu_notifier: |
Oded Gabbay | 7fd5e03 | 2016-06-23 17:54:29 +0300 | [diff] [blame] | 309 | mutex_destroy(&process->mutex); |
Felix Kuehling | a91e70e | 2017-08-26 02:00:57 -0400 | [diff] [blame] | 310 | kfd_free_process_doorbells(process); |
| 311 | err_alloc_doorbells: |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 312 | kfd_pasid_free(process->pasid); |
| 313 | err_alloc_pasid: |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 314 | kfree(process); |
| 315 | err_alloc_process: |
| 316 | return ERR_PTR(err); |
| 317 | } |
| 318 | |
| 319 | 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] | 320 | struct kfd_process *p) |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 321 | { |
| 322 | struct kfd_process_device *pdd = NULL; |
| 323 | |
| 324 | list_for_each_entry(pdd, &p->per_device_data, per_device_list) |
| 325 | if (pdd->dev == dev) |
Yong Zhao | 733fa1f | 2017-09-20 18:10:14 -0400 | [diff] [blame] | 326 | return pdd; |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 327 | |
Yong Zhao | 733fa1f | 2017-09-20 18:10:14 -0400 | [diff] [blame] | 328 | return NULL; |
Alexey Skidanov | 093c7d8 | 2014-11-18 14:00:04 +0200 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | struct kfd_process_device *kfd_create_process_device_data(struct kfd_dev *dev, |
| 332 | struct kfd_process *p) |
| 333 | { |
| 334 | struct kfd_process_device *pdd = NULL; |
| 335 | |
| 336 | pdd = kzalloc(sizeof(*pdd), GFP_KERNEL); |
| 337 | if (pdd != NULL) { |
| 338 | pdd->dev = dev; |
| 339 | INIT_LIST_HEAD(&pdd->qpd.queues_list); |
| 340 | INIT_LIST_HEAD(&pdd->qpd.priv_queue_list); |
| 341 | pdd->qpd.dqm = dev->dqm; |
Felix Kuehling | 9fd3f1bf | 2017-09-27 00:09:52 -0400 | [diff] [blame] | 342 | pdd->process = p; |
Yong Zhao | 733fa1f | 2017-09-20 18:10:14 -0400 | [diff] [blame] | 343 | pdd->bound = PDD_UNBOUND; |
Felix Kuehling | 9fd3f1bf | 2017-09-27 00:09:52 -0400 | [diff] [blame] | 344 | pdd->already_dequeued = false; |
Alexey Skidanov | 093c7d8 | 2014-11-18 14:00:04 +0200 | [diff] [blame] | 345 | list_add(&pdd->per_device_list, &p->per_device_data); |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | return pdd; |
| 349 | } |
| 350 | |
| 351 | /* |
| 352 | * Direct the IOMMU to bind the process (specifically the pasid->mm) |
| 353 | * to the device. |
| 354 | * Unbinding occurs when the process dies or the device is removed. |
| 355 | * |
| 356 | * Assumes that the process lock is held. |
| 357 | */ |
| 358 | struct kfd_process_device *kfd_bind_process_to_device(struct kfd_dev *dev, |
| 359 | struct kfd_process *p) |
| 360 | { |
Alexey Skidanov | 093c7d8 | 2014-11-18 14:00:04 +0200 | [diff] [blame] | 361 | struct kfd_process_device *pdd; |
Oded Gabbay | b17f068 | 2014-07-17 00:06:27 +0300 | [diff] [blame] | 362 | int err; |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 363 | |
Alexey Skidanov | 093c7d8 | 2014-11-18 14:00:04 +0200 | [diff] [blame] | 364 | pdd = kfd_get_process_device_data(dev, p); |
| 365 | if (!pdd) { |
| 366 | pr_err("Process device data doesn't exist\n"); |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 367 | return ERR_PTR(-ENOMEM); |
Alexey Skidanov | 093c7d8 | 2014-11-18 14:00:04 +0200 | [diff] [blame] | 368 | } |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 369 | |
Yong Zhao | 733fa1f | 2017-09-20 18:10:14 -0400 | [diff] [blame] | 370 | if (pdd->bound == PDD_BOUND) { |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 371 | return pdd; |
Yong Zhao | 733fa1f | 2017-09-20 18:10:14 -0400 | [diff] [blame] | 372 | } else if (unlikely(pdd->bound == PDD_BOUND_SUSPENDED)) { |
| 373 | pr_err("Binding PDD_BOUND_SUSPENDED pdd is unexpected!\n"); |
| 374 | return ERR_PTR(-EINVAL); |
| 375 | } |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 376 | |
Oded Gabbay | b17f068 | 2014-07-17 00:06:27 +0300 | [diff] [blame] | 377 | err = amd_iommu_bind_pasid(dev->pdev, p->pasid, p->lead_thread); |
| 378 | if (err < 0) |
| 379 | return ERR_PTR(err); |
| 380 | |
Yong Zhao | 733fa1f | 2017-09-20 18:10:14 -0400 | [diff] [blame] | 381 | pdd->bound = PDD_BOUND; |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 382 | |
| 383 | return pdd; |
| 384 | } |
| 385 | |
Yong Zhao | 733fa1f | 2017-09-20 18:10:14 -0400 | [diff] [blame] | 386 | /* |
| 387 | * Bind processes do the device that have been temporarily unbound |
| 388 | * (PDD_BOUND_SUSPENDED) in kfd_unbind_processes_from_device. |
| 389 | */ |
| 390 | int kfd_bind_processes_to_device(struct kfd_dev *dev) |
| 391 | { |
| 392 | struct kfd_process_device *pdd; |
| 393 | struct kfd_process *p; |
| 394 | unsigned int temp; |
| 395 | int err = 0; |
| 396 | |
| 397 | int idx = srcu_read_lock(&kfd_processes_srcu); |
| 398 | |
| 399 | hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) { |
| 400 | mutex_lock(&p->mutex); |
| 401 | pdd = kfd_get_process_device_data(dev, p); |
| 402 | if (pdd->bound != PDD_BOUND_SUSPENDED) { |
| 403 | mutex_unlock(&p->mutex); |
| 404 | continue; |
| 405 | } |
| 406 | |
| 407 | err = amd_iommu_bind_pasid(dev->pdev, p->pasid, |
| 408 | p->lead_thread); |
| 409 | if (err < 0) { |
| 410 | pr_err("unexpected pasid %d binding failure\n", |
| 411 | p->pasid); |
| 412 | mutex_unlock(&p->mutex); |
| 413 | break; |
| 414 | } |
| 415 | |
| 416 | pdd->bound = PDD_BOUND; |
| 417 | mutex_unlock(&p->mutex); |
| 418 | } |
| 419 | |
| 420 | srcu_read_unlock(&kfd_processes_srcu, idx); |
| 421 | |
| 422 | return err; |
| 423 | } |
| 424 | |
| 425 | /* |
Yong Zhao | e2a8e99 | 2017-11-01 19:21:28 -0400 | [diff] [blame^] | 426 | * Mark currently bound processes as PDD_BOUND_SUSPENDED. These |
| 427 | * processes will be restored to PDD_BOUND state in |
| 428 | * kfd_bind_processes_to_device. |
Yong Zhao | 733fa1f | 2017-09-20 18:10:14 -0400 | [diff] [blame] | 429 | */ |
| 430 | void kfd_unbind_processes_from_device(struct kfd_dev *dev) |
| 431 | { |
| 432 | struct kfd_process_device *pdd; |
| 433 | struct kfd_process *p; |
Yong Zhao | e2a8e99 | 2017-11-01 19:21:28 -0400 | [diff] [blame^] | 434 | unsigned int temp; |
Yong Zhao | 733fa1f | 2017-09-20 18:10:14 -0400 | [diff] [blame] | 435 | |
| 436 | int idx = srcu_read_lock(&kfd_processes_srcu); |
| 437 | |
| 438 | hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) { |
| 439 | mutex_lock(&p->mutex); |
| 440 | pdd = kfd_get_process_device_data(dev, p); |
Yong Zhao | e2a8e99 | 2017-11-01 19:21:28 -0400 | [diff] [blame^] | 441 | |
Yong Zhao | 733fa1f | 2017-09-20 18:10:14 -0400 | [diff] [blame] | 442 | if (pdd->bound == PDD_BOUND) |
| 443 | pdd->bound = PDD_BOUND_SUSPENDED; |
| 444 | mutex_unlock(&p->mutex); |
Yong Zhao | 733fa1f | 2017-09-20 18:10:14 -0400 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | srcu_read_unlock(&kfd_processes_srcu, idx); |
| 448 | } |
| 449 | |
| 450 | 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] | 451 | { |
| 452 | struct kfd_process *p; |
| 453 | struct kfd_process_device *pdd; |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 454 | |
Oded Gabbay | 121b78e | 2016-05-26 08:41:08 +0300 | [diff] [blame] | 455 | /* |
| 456 | * Look for the process that matches the pasid. If there is no such |
| 457 | * process, we either released it in amdkfd's own notifier, or there |
| 458 | * is a bug. Unfortunately, there is no way to tell... |
| 459 | */ |
Edward O'Callaghan | ad16a469 | 2016-09-17 15:01:42 +1000 | [diff] [blame] | 460 | p = kfd_lookup_process_by_pasid(pasid); |
| 461 | if (!p) |
| 462 | return; |
Oded Gabbay | 121b78e | 2016-05-26 08:41:08 +0300 | [diff] [blame] | 463 | |
Edward O'Callaghan | ad16a469 | 2016-09-17 15:01:42 +1000 | [diff] [blame] | 464 | pr_debug("Unbinding process %d from IOMMU\n", pasid); |
Oded Gabbay | 121b78e | 2016-05-26 08:41:08 +0300 | [diff] [blame] | 465 | |
Edward O'Callaghan | ad16a469 | 2016-09-17 15:01:42 +1000 | [diff] [blame] | 466 | if ((dev->dbgmgr) && (dev->dbgmgr->pasid == p->pasid)) |
| 467 | kfd_dbgmgr_destroy(dev->dbgmgr); |
Oded Gabbay | 121b78e | 2016-05-26 08:41:08 +0300 | [diff] [blame] | 468 | |
Edward O'Callaghan | ad16a469 | 2016-09-17 15:01:42 +1000 | [diff] [blame] | 469 | pdd = kfd_get_process_device_data(dev, p); |
Felix Kuehling | 9fd3f1bf | 2017-09-27 00:09:52 -0400 | [diff] [blame] | 470 | if (pdd) |
| 471 | /* For GPU relying on IOMMU, we need to dequeue here |
| 472 | * when PASID is still bound. |
| 473 | */ |
| 474 | kfd_process_dequeue_from_device(pdd); |
Oded Gabbay | 121b78e | 2016-05-26 08:41:08 +0300 | [diff] [blame] | 475 | |
Edward O'Callaghan | ad16a469 | 2016-09-17 15:01:42 +1000 | [diff] [blame] | 476 | mutex_unlock(&p->mutex); |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 477 | } |
| 478 | |
Kent Russell | 8eabaf5 | 2017-08-15 23:00:04 -0400 | [diff] [blame] | 479 | struct kfd_process_device *kfd_get_first_process_device_data( |
| 480 | struct kfd_process *p) |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 481 | { |
| 482 | return list_first_entry(&p->per_device_data, |
| 483 | struct kfd_process_device, |
| 484 | per_device_list); |
| 485 | } |
| 486 | |
Kent Russell | 8eabaf5 | 2017-08-15 23:00:04 -0400 | [diff] [blame] | 487 | struct kfd_process_device *kfd_get_next_process_device_data( |
| 488 | struct kfd_process *p, |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 489 | struct kfd_process_device *pdd) |
| 490 | { |
| 491 | if (list_is_last(&pdd->per_device_list, &p->per_device_data)) |
| 492 | return NULL; |
| 493 | return list_next_entry(pdd, per_device_list); |
| 494 | } |
| 495 | |
| 496 | bool kfd_has_process_device_data(struct kfd_process *p) |
| 497 | { |
| 498 | return !(list_empty(&p->per_device_data)); |
| 499 | } |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 500 | |
| 501 | /* This returns with process->mutex locked. */ |
| 502 | struct kfd_process *kfd_lookup_process_by_pasid(unsigned int pasid) |
| 503 | { |
| 504 | struct kfd_process *p; |
| 505 | unsigned int temp; |
| 506 | |
| 507 | int idx = srcu_read_lock(&kfd_processes_srcu); |
| 508 | |
| 509 | hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) { |
| 510 | if (p->pasid == pasid) { |
| 511 | mutex_lock(&p->mutex); |
| 512 | break; |
| 513 | } |
| 514 | } |
| 515 | |
| 516 | srcu_read_unlock(&kfd_processes_srcu, idx); |
| 517 | |
| 518 | return p; |
| 519 | } |