Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

drm/amdkfd: Add the events module

This patch adds the events module (kfd_events.c) and the interrupt
handle module for Kaveri (cik_event_interrupt.c).

The patch updates the interrupt_is_wanted(), so that it now calls the
interrupt isr function specific for the device that received the
interrupt. That function(implemented in cik_event_interrupt.c)
returns whether this interrupt is of interest to us or not.

The patch also updates the interrupt_wq(), so that it now calls the
device's specific wq function, which checks the interrupt source
and tries to signal relevant events.

v2:

Increase limit of signal events to 4096 per process
Remove bitfields from struct cik_ih_ring_entry
Rename radeon_kfd_event_mmap to kfd_event_mmap
Add debug prints to allocate_free_slot and allocate_signal_page
Make allocate_event_notification_slot return a correct value
Add warning prints to create_signal_event
Remove error print from IOCTL path
Reformatted debug prints in kfd_event_mmap
Map correct size (as received from mmap) in kfd_event_mmap

v3:

Reduce limit of signal events back to 256 per process
Fix allocation of kernel memory for signal events

Signed-off-by: Andrew Lewycky <Andrew.Lewycky@amd.com>
Signed-off-by: Oded Gabbay <oded.gabbay@gmail.com>

authored by

Andrew Lewycky and committed by
Oded Gabbay
f3a39818 29a5d3eb

+1074 -5
+1 -1
drivers/gpu/drm/amd/amdkfd/Makefile
··· 12 12 kfd_kernel_queue_vi.o kfd_packet_manager.o \ 13 13 kfd_process_queue_manager.o kfd_device_queue_manager.o \ 14 14 kfd_device_queue_manager_cik.o kfd_device_queue_manager_vi.o \ 15 - kfd_interrupt.o 15 + kfd_interrupt.o kfd_events.o cik_event_interrupt.o 16 16 17 17 obj-$(CONFIG_HSA_AMD) += amdkfd.o
+63
drivers/gpu/drm/amd/amdkfd/cik_event_interrupt.c
··· 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 "kfd_priv.h" 24 + #include "kfd_events.h" 25 + #include "cik_int.h" 26 + 27 + static bool cik_event_interrupt_isr(struct kfd_dev *dev, 28 + const uint32_t *ih_ring_entry) 29 + { 30 + unsigned int pasid; 31 + const struct cik_ih_ring_entry *ihre = 32 + (const struct cik_ih_ring_entry *)ih_ring_entry; 33 + 34 + pasid = (ihre->ring_id & 0xffff0000) >> 16; 35 + 36 + /* Do not process in ISR, just request it to be forwarded to WQ. */ 37 + return (pasid != 0) && 38 + (ihre->source_id == CIK_INTSRC_CP_END_OF_PIPE || 39 + ihre->source_id == CIK_INTSRC_SQ_INTERRUPT_MSG); 40 + } 41 + 42 + static void cik_event_interrupt_wq(struct kfd_dev *dev, 43 + const uint32_t *ih_ring_entry) 44 + { 45 + unsigned int pasid; 46 + const struct cik_ih_ring_entry *ihre = 47 + (const struct cik_ih_ring_entry *)ih_ring_entry; 48 + 49 + pasid = (ihre->ring_id & 0xffff0000) >> 16; 50 + 51 + if (pasid == 0) 52 + return; 53 + 54 + if (ihre->source_id == CIK_INTSRC_CP_END_OF_PIPE) 55 + kfd_signal_event_interrupt(pasid, 0, 0); 56 + else if (ihre->source_id == CIK_INTSRC_SQ_INTERRUPT_MSG) 57 + kfd_signal_event_interrupt(pasid, ihre->data & 0xFF, 8); 58 + } 59 + 60 + const struct kfd_event_interrupt_class event_interrupt_class_cik = { 61 + .interrupt_isr = cik_event_interrupt_isr, 62 + .interrupt_wq = cik_event_interrupt_wq, 63 + };
+40
drivers/gpu/drm/amd/amdkfd/cik_int.h
··· 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 + #ifndef HSA_RADEON_CIK_INT_H_INCLUDED 24 + #define HSA_RADEON_CIK_INT_H_INCLUDED 25 + 26 + #include <linux/types.h> 27 + 28 + struct cik_ih_ring_entry { 29 + uint32_t source_id; 30 + uint32_t data; 31 + uint32_t ring_id; 32 + uint32_t reserved; 33 + }; 34 + 35 + #define CIK_INTSRC_DEQUEUE_COMPLETE 0xC6 36 + #define CIK_INTSRC_CP_END_OF_PIPE 0xB5 37 + #define CIK_INTSRC_SQ_INTERRUPT_MSG 0xEF 38 + 39 + #endif 40 +
+14 -2
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c
··· 289 289 290 290 args->queue_id = queue_id; 291 291 292 + 292 293 /* Return gpu_id as doorbell offset for mmap usage */ 293 - args->doorbell_offset = args->gpu_id << PAGE_SHIFT; 294 + args->doorbell_offset = (KFD_MMAP_DOORBELL_MASK | args->gpu_id); 295 + args->doorbell_offset <<= PAGE_SHIFT; 294 296 295 297 mutex_unlock(&p->mutex); 296 298 ··· 686 684 if (IS_ERR(process)) 687 685 return PTR_ERR(process); 688 686 689 - return kfd_doorbell_mmap(process, vma); 687 + if ((vma->vm_pgoff & KFD_MMAP_DOORBELL_MASK) == 688 + KFD_MMAP_DOORBELL_MASK) { 689 + vma->vm_pgoff = vma->vm_pgoff ^ KFD_MMAP_DOORBELL_MASK; 690 + return kfd_doorbell_mmap(process, vma); 691 + } else if ((vma->vm_pgoff & KFD_MMAP_EVENTS_MASK) == 692 + KFD_MMAP_EVENTS_MASK) { 693 + vma->vm_pgoff = vma->vm_pgoff ^ KFD_MMAP_EVENTS_MASK; 694 + return kfd_event_mmap(process, vma); 695 + } 696 + 697 + return -EFAULT; 690 698 }
+1
drivers/gpu/drm/amd/amdkfd/kfd_device.c
··· 34 34 .asic_family = CHIP_KAVERI, 35 35 .max_pasid_bits = 16, 36 36 .ih_ring_entry_size = 4 * sizeof(uint32_t), 37 + .event_interrupt_class = &event_interrupt_class_cik, 37 38 .mqd_size_aligned = MQD_SIZE_ALIGNED 38 39 }; 39 40
+799
drivers/gpu/drm/amd/amdkfd/kfd_events.c
··· 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/mm_types.h> 24 + #include <linux/slab.h> 25 + #include <linux/types.h> 26 + #include <linux/sched.h> 27 + #include <linux/uaccess.h> 28 + #include <linux/mm.h> 29 + #include <linux/mman.h> 30 + #include <linux/memory.h> 31 + #include "kfd_priv.h" 32 + #include "kfd_events.h" 33 + 34 + /* 35 + * A task can only be on a single wait_queue at a time, but we need to support 36 + * waiting on multiple events (any/all). 37 + * Instead of each event simply having a wait_queue with sleeping tasks, it 38 + * has a singly-linked list of tasks. 39 + * A thread that wants to sleep creates an array of these, one for each event 40 + * and adds one to each event's waiter chain. 41 + */ 42 + struct kfd_event_waiter { 43 + struct list_head waiters; 44 + struct task_struct *sleeping_task; 45 + 46 + /* Transitions to true when the event this belongs to is signaled. */ 47 + bool activated; 48 + }; 49 + 50 + /* 51 + * Over-complicated pooled allocator for event notification slots. 52 + * 53 + * Each signal event needs a 64-bit signal slot where the signaler will write 54 + * a 1 before sending an interrupt.l (This is needed because some interrupts 55 + * do not contain enough spare data bits to identify an event.) 56 + * We get whole pages from vmalloc and map them to the process VA. 57 + * Individual signal events are then allocated a slot in a page. 58 + */ 59 + 60 + struct signal_page { 61 + struct list_head event_pages; /* kfd_process.signal_event_pages */ 62 + uint64_t *kernel_address; 63 + uint64_t __user *user_address; 64 + uint32_t page_index; /* Index into the mmap aperture. */ 65 + unsigned int free_slots; 66 + unsigned long used_slot_bitmap[0]; 67 + }; 68 + 69 + #define SLOTS_PER_PAGE KFD_SIGNAL_EVENT_LIMIT 70 + #define SLOT_BITMAP_SIZE BITS_TO_LONGS(SLOTS_PER_PAGE) 71 + #define BITS_PER_PAGE (ilog2(SLOTS_PER_PAGE)+1) 72 + #define SIGNAL_PAGE_SIZE (sizeof(struct signal_page) + \ 73 + SLOT_BITMAP_SIZE * sizeof(long)) 74 + 75 + /* 76 + * For signal events, the event ID is used as the interrupt user data. 77 + * For SQ s_sendmsg interrupts, this is limited to 8 bits. 78 + */ 79 + 80 + #define INTERRUPT_DATA_BITS 8 81 + #define SIGNAL_EVENT_ID_SLOT_SHIFT 0 82 + 83 + static uint64_t *page_slots(struct signal_page *page) 84 + { 85 + return page->kernel_address; 86 + } 87 + 88 + static bool allocate_free_slot(struct kfd_process *process, 89 + struct signal_page **out_page, 90 + unsigned int *out_slot_index) 91 + { 92 + struct signal_page *page; 93 + 94 + list_for_each_entry(page, &process->signal_event_pages, event_pages) { 95 + if (page->free_slots > 0) { 96 + unsigned int slot = 97 + find_first_zero_bit(page->used_slot_bitmap, 98 + SLOTS_PER_PAGE); 99 + 100 + __set_bit(slot, page->used_slot_bitmap); 101 + page->free_slots--; 102 + 103 + page_slots(page)[slot] = UNSIGNALED_EVENT_SLOT; 104 + 105 + *out_page = page; 106 + *out_slot_index = slot; 107 + 108 + pr_debug("allocated event signal slot in page %p, slot %d\n", 109 + page, slot); 110 + 111 + return true; 112 + } 113 + } 114 + 115 + pr_debug("No free event signal slots were found for process %p\n", 116 + process); 117 + 118 + return false; 119 + } 120 + 121 + #define list_tail_entry(head, type, member) \ 122 + list_entry((head)->prev, type, member) 123 + 124 + static bool allocate_signal_page(struct file *devkfd, struct kfd_process *p) 125 + { 126 + void *backing_store; 127 + struct signal_page *page; 128 + 129 + page = kzalloc(SIGNAL_PAGE_SIZE, GFP_KERNEL); 130 + if (!page) 131 + goto fail_alloc_signal_page; 132 + 133 + page->free_slots = SLOTS_PER_PAGE; 134 + 135 + backing_store = (void *) __get_free_pages(GFP_KERNEL | __GFP_ZERO, 136 + get_order(KFD_SIGNAL_EVENT_LIMIT * 8)); 137 + if (!backing_store) 138 + goto fail_alloc_signal_store; 139 + 140 + /* prevent user-mode info leaks */ 141 + memset(backing_store, (uint8_t) UNSIGNALED_EVENT_SLOT, 142 + KFD_SIGNAL_EVENT_LIMIT * 8); 143 + 144 + page->kernel_address = backing_store; 145 + 146 + if (list_empty(&p->signal_event_pages)) 147 + page->page_index = 0; 148 + else 149 + page->page_index = list_tail_entry(&p->signal_event_pages, 150 + struct signal_page, 151 + event_pages)->page_index + 1; 152 + 153 + pr_debug("allocated new event signal page at %p, for process %p\n", 154 + page, p); 155 + pr_debug("page index is %d\n", page->page_index); 156 + 157 + list_add(&page->event_pages, &p->signal_event_pages); 158 + 159 + return true; 160 + 161 + fail_alloc_signal_store: 162 + kfree(page); 163 + fail_alloc_signal_page: 164 + return false; 165 + } 166 + 167 + static bool allocate_event_notification_slot(struct file *devkfd, 168 + struct kfd_process *p, 169 + struct signal_page **page, 170 + unsigned int *signal_slot_index) 171 + { 172 + bool ret; 173 + 174 + ret = allocate_free_slot(p, page, signal_slot_index); 175 + if (ret == false) { 176 + ret = allocate_signal_page(devkfd, p); 177 + if (ret == true) 178 + ret = allocate_free_slot(p, page, signal_slot_index); 179 + } 180 + 181 + return ret; 182 + } 183 + 184 + /* Assumes that the process's event_mutex is locked. */ 185 + static void release_event_notification_slot(struct signal_page *page, 186 + size_t slot_index) 187 + { 188 + __clear_bit(slot_index, page->used_slot_bitmap); 189 + page->free_slots++; 190 + 191 + /* We don't free signal pages, they are retained by the process 192 + * and reused until it exits. */ 193 + } 194 + 195 + static struct signal_page *lookup_signal_page_by_index(struct kfd_process *p, 196 + unsigned int page_index) 197 + { 198 + struct signal_page *page; 199 + 200 + /* 201 + * This is safe because we don't delete signal pages until the 202 + * process exits. 203 + */ 204 + list_for_each_entry(page, &p->signal_event_pages, event_pages) 205 + if (page->page_index == page_index) 206 + return page; 207 + 208 + return NULL; 209 + } 210 + 211 + /* 212 + * Assumes that p->event_mutex is held and of course that p is not going 213 + * away (current or locked). 214 + */ 215 + static struct kfd_event *lookup_event_by_id(struct kfd_process *p, uint32_t id) 216 + { 217 + struct kfd_event *ev; 218 + 219 + hash_for_each_possible(p->events, ev, events, id) 220 + if (ev->event_id == id) 221 + return ev; 222 + 223 + return NULL; 224 + } 225 + 226 + static u32 make_signal_event_id(struct signal_page *page, 227 + unsigned int signal_slot_index) 228 + { 229 + return page->page_index | 230 + (signal_slot_index << SIGNAL_EVENT_ID_SLOT_SHIFT); 231 + } 232 + 233 + /* 234 + * Produce a kfd event id for a nonsignal event. 235 + * These are arbitrary numbers, so we do a sequential search through 236 + * the hash table for an unused number. 237 + */ 238 + static u32 make_nonsignal_event_id(struct kfd_process *p) 239 + { 240 + u32 id; 241 + 242 + for (id = p->next_nonsignal_event_id; 243 + id < KFD_LAST_NONSIGNAL_EVENT_ID && 244 + lookup_event_by_id(p, id) != NULL; 245 + id++) 246 + ; 247 + 248 + if (id < KFD_LAST_NONSIGNAL_EVENT_ID) { 249 + 250 + /* 251 + * What if id == LAST_NONSIGNAL_EVENT_ID - 1? 252 + * Then next_nonsignal_event_id = LAST_NONSIGNAL_EVENT_ID so 253 + * the first loop fails immediately and we proceed with the 254 + * wraparound loop below. 255 + */ 256 + p->next_nonsignal_event_id = id + 1; 257 + 258 + return id; 259 + } 260 + 261 + for (id = KFD_FIRST_NONSIGNAL_EVENT_ID; 262 + id < KFD_LAST_NONSIGNAL_EVENT_ID && 263 + lookup_event_by_id(p, id) != NULL; 264 + id++) 265 + ; 266 + 267 + 268 + if (id < KFD_LAST_NONSIGNAL_EVENT_ID) { 269 + p->next_nonsignal_event_id = id + 1; 270 + return id; 271 + } 272 + 273 + p->next_nonsignal_event_id = KFD_FIRST_NONSIGNAL_EVENT_ID; 274 + return 0; 275 + } 276 + 277 + static struct kfd_event *lookup_event_by_page_slot(struct kfd_process *p, 278 + struct signal_page *page, 279 + unsigned int signal_slot) 280 + { 281 + return lookup_event_by_id(p, make_signal_event_id(page, signal_slot)); 282 + } 283 + 284 + static int create_signal_event(struct file *devkfd, 285 + struct kfd_process *p, 286 + struct kfd_event *ev) 287 + { 288 + if (p->signal_event_count == KFD_SIGNAL_EVENT_LIMIT) { 289 + pr_warn("amdkfd: Signal event wasn't created because limit was reached\n"); 290 + return -ENOMEM; 291 + } 292 + 293 + if (!allocate_event_notification_slot(devkfd, p, &ev->signal_page, 294 + &ev->signal_slot_index)) { 295 + pr_warn("amdkfd: Signal event wasn't created because out of kernel memory\n"); 296 + return -ENOMEM; 297 + } 298 + 299 + p->signal_event_count++; 300 + 301 + ev->user_signal_address = 302 + &ev->signal_page->user_address[ev->signal_slot_index]; 303 + 304 + ev->event_id = make_signal_event_id(ev->signal_page, 305 + ev->signal_slot_index); 306 + 307 + pr_debug("signal event number %zu created with id %d, address %p\n", 308 + p->signal_event_count, ev->event_id, 309 + ev->user_signal_address); 310 + 311 + return 0; 312 + } 313 + 314 + /* 315 + * No non-signal events are supported yet. 316 + * We create them as events that never signal. 317 + * Set event calls from user-mode are failed. 318 + */ 319 + static int create_other_event(struct kfd_process *p, struct kfd_event *ev) 320 + { 321 + ev->event_id = make_nonsignal_event_id(p); 322 + if (ev->event_id == 0) 323 + return -ENOMEM; 324 + 325 + return 0; 326 + } 327 + 328 + void kfd_event_init_process(struct kfd_process *p) 329 + { 330 + mutex_init(&p->event_mutex); 331 + hash_init(p->events); 332 + INIT_LIST_HEAD(&p->signal_event_pages); 333 + p->next_nonsignal_event_id = KFD_FIRST_NONSIGNAL_EVENT_ID; 334 + p->signal_event_count = 0; 335 + } 336 + 337 + static void destroy_event(struct kfd_process *p, struct kfd_event *ev) 338 + { 339 + if (ev->signal_page != NULL) { 340 + release_event_notification_slot(ev->signal_page, 341 + ev->signal_slot_index); 342 + p->signal_event_count--; 343 + } 344 + 345 + /* 346 + * Abandon the list of waiters. Individual waiting threads will 347 + * clean up their own data. 348 + */ 349 + list_del(&ev->waiters); 350 + 351 + hash_del(&ev->events); 352 + kfree(ev); 353 + } 354 + 355 + static void destroy_events(struct kfd_process *p) 356 + { 357 + struct kfd_event *ev; 358 + struct hlist_node *tmp; 359 + unsigned int hash_bkt; 360 + 361 + hash_for_each_safe(p->events, hash_bkt, tmp, ev, events) 362 + destroy_event(p, ev); 363 + } 364 + 365 + /* 366 + * We assume that the process is being destroyed and there is no need to 367 + * unmap the pages or keep bookkeeping data in order. 368 + */ 369 + static void shutdown_signal_pages(struct kfd_process *p) 370 + { 371 + struct signal_page *page, *tmp; 372 + 373 + list_for_each_entry_safe(page, tmp, &p->signal_event_pages, 374 + event_pages) { 375 + free_pages((unsigned long)page->kernel_address, 376 + get_order(KFD_SIGNAL_EVENT_LIMIT * 8)); 377 + kfree(page); 378 + } 379 + } 380 + 381 + void kfd_event_free_process(struct kfd_process *p) 382 + { 383 + destroy_events(p); 384 + shutdown_signal_pages(p); 385 + } 386 + 387 + static bool event_can_be_gpu_signaled(const struct kfd_event *ev) 388 + { 389 + return ev->type == KFD_EVENT_TYPE_SIGNAL || 390 + ev->type == KFD_EVENT_TYPE_DEBUG; 391 + } 392 + 393 + static bool event_can_be_cpu_signaled(const struct kfd_event *ev) 394 + { 395 + return ev->type == KFD_EVENT_TYPE_SIGNAL; 396 + } 397 + 398 + int kfd_event_create(struct file *devkfd, struct kfd_process *p, 399 + uint32_t event_type, bool auto_reset, uint32_t node_id, 400 + uint32_t *event_id, uint32_t *event_trigger_data, 401 + uint64_t *event_page_offset, uint32_t *event_slot_index) 402 + { 403 + int ret = 0; 404 + struct kfd_event *ev = kzalloc(sizeof(*ev), GFP_KERNEL); 405 + 406 + if (!ev) 407 + return -ENOMEM; 408 + 409 + ev->type = event_type; 410 + ev->auto_reset = auto_reset; 411 + ev->signaled = false; 412 + 413 + INIT_LIST_HEAD(&ev->waiters); 414 + 415 + *event_page_offset = 0; 416 + 417 + mutex_lock(&p->event_mutex); 418 + 419 + switch (event_type) { 420 + case KFD_EVENT_TYPE_SIGNAL: 421 + case KFD_EVENT_TYPE_DEBUG: 422 + ret = create_signal_event(devkfd, p, ev); 423 + if (!ret) { 424 + *event_page_offset = (ev->signal_page->page_index | 425 + KFD_MMAP_EVENTS_MASK); 426 + *event_page_offset <<= PAGE_SHIFT; 427 + *event_slot_index = ev->signal_slot_index; 428 + } 429 + break; 430 + default: 431 + ret = create_other_event(p, ev); 432 + break; 433 + } 434 + 435 + if (!ret) { 436 + hash_add(p->events, &ev->events, ev->event_id); 437 + 438 + *event_id = ev->event_id; 439 + *event_trigger_data = ev->event_id; 440 + } else { 441 + kfree(ev); 442 + } 443 + 444 + mutex_unlock(&p->event_mutex); 445 + 446 + return ret; 447 + } 448 + 449 + /* Assumes that p is current. */ 450 + int kfd_event_destroy(struct kfd_process *p, uint32_t event_id) 451 + { 452 + struct kfd_event *ev; 453 + int ret = 0; 454 + 455 + mutex_lock(&p->event_mutex); 456 + 457 + ev = lookup_event_by_id(p, event_id); 458 + 459 + if (ev) 460 + destroy_event(p, ev); 461 + else 462 + ret = -EINVAL; 463 + 464 + mutex_unlock(&p->event_mutex); 465 + return ret; 466 + } 467 + 468 + static void set_event(struct kfd_event *ev) 469 + { 470 + struct kfd_event_waiter *waiter; 471 + struct kfd_event_waiter *next; 472 + 473 + /* Auto reset if the list is non-empty and we're waking someone. */ 474 + ev->signaled = !ev->auto_reset || list_empty(&ev->waiters); 475 + 476 + list_for_each_entry_safe(waiter, next, &ev->waiters, waiters) { 477 + waiter->activated = true; 478 + 479 + /* _init because free_waiters will call list_del */ 480 + list_del_init(&waiter->waiters); 481 + 482 + wake_up_process(waiter->sleeping_task); 483 + } 484 + } 485 + 486 + /* Assumes that p is current. */ 487 + int kfd_set_event(struct kfd_process *p, uint32_t event_id) 488 + { 489 + int ret = 0; 490 + struct kfd_event *ev; 491 + 492 + mutex_lock(&p->event_mutex); 493 + 494 + ev = lookup_event_by_id(p, event_id); 495 + 496 + if (ev && event_can_be_cpu_signaled(ev)) 497 + set_event(ev); 498 + else 499 + ret = -EINVAL; 500 + 501 + mutex_unlock(&p->event_mutex); 502 + return ret; 503 + } 504 + 505 + static void reset_event(struct kfd_event *ev) 506 + { 507 + ev->signaled = false; 508 + } 509 + 510 + /* Assumes that p is current. */ 511 + int kfd_reset_event(struct kfd_process *p, uint32_t event_id) 512 + { 513 + int ret = 0; 514 + struct kfd_event *ev; 515 + 516 + mutex_lock(&p->event_mutex); 517 + 518 + ev = lookup_event_by_id(p, event_id); 519 + 520 + if (ev && event_can_be_cpu_signaled(ev)) 521 + reset_event(ev); 522 + else 523 + ret = -EINVAL; 524 + 525 + mutex_unlock(&p->event_mutex); 526 + return ret; 527 + 528 + } 529 + 530 + static void acknowledge_signal(struct kfd_process *p, struct kfd_event *ev) 531 + { 532 + page_slots(ev->signal_page)[ev->signal_slot_index] = 533 + UNSIGNALED_EVENT_SLOT; 534 + } 535 + 536 + static bool is_slot_signaled(struct signal_page *page, unsigned int index) 537 + { 538 + return page_slots(page)[index] != UNSIGNALED_EVENT_SLOT; 539 + } 540 + 541 + static void set_event_from_interrupt(struct kfd_process *p, 542 + struct kfd_event *ev) 543 + { 544 + if (ev && event_can_be_gpu_signaled(ev)) { 545 + acknowledge_signal(p, ev); 546 + set_event(ev); 547 + } 548 + } 549 + 550 + void kfd_signal_event_interrupt(unsigned int pasid, uint32_t partial_id, 551 + uint32_t valid_id_bits) 552 + { 553 + struct kfd_event *ev; 554 + 555 + /* 556 + * Because we are called from arbitrary context (workqueue) as opposed 557 + * to process context, kfd_process could attempt to exit while we are 558 + * running so the lookup function returns a locked process. 559 + */ 560 + struct kfd_process *p = kfd_lookup_process_by_pasid(pasid); 561 + 562 + if (!p) 563 + return; /* Presumably process exited. */ 564 + 565 + mutex_lock(&p->event_mutex); 566 + 567 + if (valid_id_bits >= INTERRUPT_DATA_BITS) { 568 + /* Partial ID is a full ID. */ 569 + ev = lookup_event_by_id(p, partial_id); 570 + set_event_from_interrupt(p, ev); 571 + } else { 572 + /* 573 + * Partial ID is in fact partial. For now we completely 574 + * ignore it, but we could use any bits we did receive to 575 + * search faster. 576 + */ 577 + struct signal_page *page; 578 + unsigned i; 579 + 580 + list_for_each_entry(page, &p->signal_event_pages, event_pages) 581 + for (i = 0; i < SLOTS_PER_PAGE; i++) 582 + if (is_slot_signaled(page, i)) { 583 + ev = lookup_event_by_page_slot(p, 584 + page, i); 585 + set_event_from_interrupt(p, ev); 586 + } 587 + } 588 + 589 + mutex_unlock(&p->event_mutex); 590 + mutex_unlock(&p->mutex); 591 + } 592 + 593 + static struct kfd_event_waiter *alloc_event_waiters(uint32_t num_events) 594 + { 595 + struct kfd_event_waiter *event_waiters; 596 + uint32_t i; 597 + 598 + event_waiters = kmalloc_array(num_events, 599 + sizeof(struct kfd_event_waiter), 600 + GFP_KERNEL); 601 + 602 + for (i = 0; (event_waiters) && (i < num_events) ; i++) { 603 + INIT_LIST_HEAD(&event_waiters[i].waiters); 604 + event_waiters[i].sleeping_task = current; 605 + event_waiters[i].activated = false; 606 + } 607 + 608 + return event_waiters; 609 + } 610 + 611 + static int init_event_waiter(struct kfd_process *p, 612 + struct kfd_event_waiter *waiter, 613 + uint32_t event_id) 614 + { 615 + struct kfd_event *ev = lookup_event_by_id(p, event_id); 616 + 617 + if (!ev) 618 + return -EINVAL; 619 + 620 + waiter->activated = ev->signaled; 621 + ev->signaled = ev->signaled && !ev->auto_reset; 622 + 623 + list_add(&waiter->waiters, &ev->waiters); 624 + 625 + return 0; 626 + } 627 + 628 + static bool test_event_condition(bool all, uint32_t num_events, 629 + struct kfd_event_waiter *event_waiters) 630 + { 631 + uint32_t i; 632 + uint32_t activated_count = 0; 633 + 634 + for (i = 0; i < num_events; i++) { 635 + if (event_waiters[i].activated) { 636 + if (!all) 637 + return true; 638 + 639 + activated_count++; 640 + } 641 + } 642 + 643 + return activated_count == num_events; 644 + } 645 + 646 + static long user_timeout_to_jiffies(uint32_t user_timeout_ms) 647 + { 648 + if (user_timeout_ms == KFD_EVENT_TIMEOUT_IMMEDIATE) 649 + return 0; 650 + 651 + if (user_timeout_ms == KFD_EVENT_TIMEOUT_INFINITE) 652 + return MAX_SCHEDULE_TIMEOUT; 653 + 654 + /* 655 + * msecs_to_jiffies interprets all values above 2^31-1 as infinite, 656 + * but we consider them finite. 657 + * This hack is wrong, but nobody is likely to notice. 658 + */ 659 + user_timeout_ms = min_t(uint32_t, user_timeout_ms, 0x7FFFFFFF); 660 + 661 + return msecs_to_jiffies(user_timeout_ms) + 1; 662 + } 663 + 664 + static void free_waiters(uint32_t num_events, struct kfd_event_waiter *waiters) 665 + { 666 + uint32_t i; 667 + 668 + for (i = 0; i < num_events; i++) 669 + list_del(&waiters[i].waiters); 670 + 671 + kfree(waiters); 672 + } 673 + 674 + int kfd_wait_on_events(struct kfd_process *p, 675 + uint32_t num_events, const uint32_t __user *event_ids, 676 + bool all, uint32_t user_timeout_ms, 677 + enum kfd_event_wait_result *wait_result) 678 + { 679 + uint32_t i; 680 + int ret = 0; 681 + struct kfd_event_waiter *event_waiters = NULL; 682 + long timeout = user_timeout_to_jiffies(user_timeout_ms); 683 + 684 + mutex_lock(&p->event_mutex); 685 + 686 + event_waiters = alloc_event_waiters(num_events); 687 + if (!event_waiters) { 688 + ret = -ENOMEM; 689 + goto fail; 690 + } 691 + 692 + for (i = 0; i < num_events; i++) { 693 + uint32_t event_id; 694 + 695 + ret = get_user(event_id, &event_ids[i]); 696 + if (ret) 697 + goto fail; 698 + 699 + ret = init_event_waiter(p, &event_waiters[i], event_id); 700 + if (ret) 701 + goto fail; 702 + } 703 + 704 + mutex_unlock(&p->event_mutex); 705 + 706 + while (true) { 707 + if (fatal_signal_pending(current)) { 708 + ret = -EINTR; 709 + break; 710 + } 711 + 712 + if (signal_pending(current)) { 713 + /* 714 + * This is wrong when a nonzero, non-infinite timeout 715 + * is specified. We need to use 716 + * ERESTARTSYS_RESTARTBLOCK, but struct restart_block 717 + * contains a union with data for each user and it's 718 + * in generic kernel code that I don't want to 719 + * touch yet. 720 + */ 721 + ret = -ERESTARTSYS; 722 + break; 723 + } 724 + 725 + if (test_event_condition(all, num_events, event_waiters)) { 726 + *wait_result = KFD_WAIT_COMPLETE; 727 + break; 728 + } 729 + 730 + if (timeout <= 0) { 731 + *wait_result = KFD_WAIT_TIMEOUT; 732 + break; 733 + } 734 + 735 + timeout = schedule_timeout_interruptible(timeout); 736 + } 737 + __set_current_state(TASK_RUNNING); 738 + 739 + mutex_lock(&p->event_mutex); 740 + free_waiters(num_events, event_waiters); 741 + mutex_unlock(&p->event_mutex); 742 + 743 + return ret; 744 + 745 + fail: 746 + if (event_waiters) 747 + free_waiters(num_events, event_waiters); 748 + 749 + mutex_unlock(&p->event_mutex); 750 + 751 + *wait_result = KFD_WAIT_ERROR; 752 + 753 + return ret; 754 + } 755 + 756 + int kfd_event_mmap(struct kfd_process *p, struct vm_area_struct *vma) 757 + { 758 + 759 + unsigned int page_index; 760 + unsigned long pfn; 761 + struct signal_page *page; 762 + 763 + /* check required size is logical */ 764 + if (get_order(KFD_SIGNAL_EVENT_LIMIT * 8) != 765 + get_order(vma->vm_end - vma->vm_start)) { 766 + pr_err("amdkfd: event page mmap requested illegal size\n"); 767 + return -EINVAL; 768 + } 769 + 770 + page_index = vma->vm_pgoff; 771 + 772 + page = lookup_signal_page_by_index(p, page_index); 773 + if (!page) { 774 + /* Probably KFD bug, but mmap is user-accessible. */ 775 + pr_debug("signal page could not be found for page_index %u\n", 776 + page_index); 777 + return -EINVAL; 778 + } 779 + 780 + pfn = __pa(page->kernel_address); 781 + pfn >>= PAGE_SHIFT; 782 + 783 + vma->vm_flags |= VM_IO | VM_DONTCOPY | VM_DONTEXPAND | VM_NORESERVE 784 + | VM_DONTDUMP | VM_PFNMAP; 785 + 786 + pr_debug("mapping signal page\n"); 787 + pr_debug(" start user address == 0x%08lx\n", vma->vm_start); 788 + pr_debug(" end user address == 0x%08lx\n", vma->vm_end); 789 + pr_debug(" pfn == 0x%016lX\n", pfn); 790 + pr_debug(" vm_flags == 0x%08lX\n", vma->vm_flags); 791 + pr_debug(" size == 0x%08lX\n", 792 + vma->vm_end - vma->vm_start); 793 + 794 + page->user_address = (uint64_t __user *)vma->vm_start; 795 + 796 + /* mapping the page to user process */ 797 + return remap_pfn_range(vma, vma->vm_start, pfn, 798 + vma->vm_end - vma->vm_start, vma->vm_page_prot); 799 + }
+76
drivers/gpu/drm/amd/amdkfd/kfd_events.h
··· 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 + #ifndef KFD_EVENTS_H_INCLUDED 24 + #define KFD_EVENTS_H_INCLUDED 25 + 26 + #include <linux/kernel.h> 27 + #include <linux/hashtable.h> 28 + #include <linux/types.h> 29 + #include <linux/list.h> 30 + #include "kfd_priv.h" 31 + 32 + #define KFD_EVENT_ID_NONSIGNAL_MASK 0x80000000U 33 + #define KFD_FIRST_NONSIGNAL_EVENT_ID KFD_EVENT_ID_NONSIGNAL_MASK 34 + #define KFD_LAST_NONSIGNAL_EVENT_ID UINT_MAX 35 + 36 + /* 37 + * Written into kfd_signal_slot_t to indicate that the event is not signaled. 38 + * Since the event protocol may need to write the event ID into memory, this 39 + * must not be a valid event ID. 40 + * For the sake of easy memset-ing, this must be a byte pattern. 41 + */ 42 + #define UNSIGNALED_EVENT_SLOT ((uint64_t)-1) 43 + 44 + struct kfd_event_waiter; 45 + struct signal_page; 46 + 47 + struct kfd_event { 48 + /* All events in process, rooted at kfd_process.events. */ 49 + struct hlist_node events; 50 + 51 + u32 event_id; 52 + 53 + bool signaled; 54 + bool auto_reset; 55 + 56 + int type; 57 + 58 + struct list_head waiters; /* List of kfd_event_waiter by waiters. */ 59 + 60 + /* Only for signal events. */ 61 + struct signal_page *signal_page; 62 + unsigned int signal_slot_index; 63 + uint64_t __user *user_signal_address; 64 + }; 65 + 66 + #define KFD_EVENT_TIMEOUT_IMMEDIATE 0 67 + #define KFD_EVENT_TIMEOUT_INFINITE 0xFFFFFFFFu 68 + 69 + /* Matching HSA_EVENTTYPE */ 70 + #define KFD_EVENT_TYPE_SIGNAL 0 71 + #define KFD_EVENT_TYPE_DEBUG 5 72 + 73 + extern void kfd_signal_event_interrupt(unsigned int pasid, uint32_t partial_id, 74 + uint32_t valid_id_bits); 75 + 76 + #endif
+9 -2
drivers/gpu/drm/amd/amdkfd/kfd_interrupt.c
··· 172 172 sizeof(uint32_t))]; 173 173 174 174 while (dequeue_ih_ring_entry(dev, ih_ring_entry)) 175 - ; 175 + dev->device_info->event_interrupt_class->interrupt_wq(dev, 176 + ih_ring_entry); 176 177 } 177 178 178 179 bool interrupt_is_wanted(struct kfd_dev *dev, const uint32_t *ih_ring_entry) 179 180 { 180 - return false; 181 + /* integer and bitwise OR so there is no boolean short-circuiting */ 182 + unsigned wanted = 0; 183 + 184 + wanted |= dev->device_info->event_interrupt_class->interrupt_isr(dev, 185 + ih_ring_entry); 186 + 187 + return wanted != 0; 181 188 }
+47
drivers/gpu/drm/amd/amdkfd/kfd_priv.h
··· 35 35 36 36 #define KFD_SYSFS_FILE_MODE 0444 37 37 38 + #define KFD_MMAP_DOORBELL_MASK 0x8000000000000 39 + #define KFD_MMAP_EVENTS_MASK 0x4000000000000 40 + 38 41 /* 39 42 * When working with cp scheduler we should assign the HIQ manually or via 40 43 * the radeon driver to a fixed hqd slot, here are the fixed HIQ hqd slot ··· 111 108 CHIP_CARRIZO 112 109 }; 113 110 111 + struct kfd_event_interrupt_class { 112 + bool (*interrupt_isr)(struct kfd_dev *dev, 113 + const uint32_t *ih_ring_entry); 114 + void (*interrupt_wq)(struct kfd_dev *dev, 115 + const uint32_t *ih_ring_entry); 116 + }; 117 + 114 118 struct kfd_device_info { 115 119 unsigned int asic_family; 120 + const struct kfd_event_interrupt_class *event_interrupt_class; 116 121 unsigned int max_pasid_bits; 117 122 size_t ih_ring_entry_size; 118 123 uint8_t num_of_watch_points; ··· 501 490 502 491 /*Is the user space process 32 bit?*/ 503 492 bool is_32bit_user_mode; 493 + 494 + /* Event-related data */ 495 + struct mutex event_mutex; 496 + /* All events in process hashed by ID, linked on kfd_event.events. */ 497 + DECLARE_HASHTABLE(events, 4); 498 + struct list_head signal_event_pages; /* struct slot_page_header. 499 + event_pages */ 500 + u32 next_nonsignal_event_id; 501 + size_t signal_event_count; 504 502 }; 505 503 506 504 /** ··· 534 514 void kfd_process_destroy_wq(void); 535 515 struct kfd_process *kfd_create_process(const struct task_struct *); 536 516 struct kfd_process *kfd_get_process(const struct task_struct *); 517 + struct kfd_process *kfd_lookup_process_by_pasid(unsigned int pasid); 537 518 538 519 struct kfd_process_device *kfd_bind_process_to_device(struct kfd_dev *dev, 539 520 struct kfd_process *p); ··· 679 658 uint64_t kfd_get_number_elems(struct kfd_dev *kfd); 680 659 phys_addr_t kfd_get_process_doorbells(struct kfd_dev *dev, 681 660 struct kfd_process *process); 661 + 662 + /* Events */ 663 + extern const struct kfd_event_interrupt_class event_interrupt_class_cik; 664 + 665 + enum kfd_event_wait_result { 666 + KFD_WAIT_COMPLETE, 667 + KFD_WAIT_TIMEOUT, 668 + KFD_WAIT_ERROR 669 + }; 670 + 671 + void kfd_event_init_process(struct kfd_process *p); 672 + void kfd_event_free_process(struct kfd_process *p); 673 + int kfd_event_mmap(struct kfd_process *process, struct vm_area_struct *vma); 674 + int kfd_wait_on_events(struct kfd_process *p, 675 + uint32_t num_events, const uint32_t __user *event_ids, 676 + bool all, uint32_t user_timeout_ms, 677 + enum kfd_event_wait_result *wait_result); 678 + void kfd_signal_event_interrupt(unsigned int pasid, uint32_t partial_id, 679 + uint32_t valid_id_bits); 680 + int kfd_set_event(struct kfd_process *p, uint32_t event_id); 681 + int kfd_reset_event(struct kfd_process *p, uint32_t event_id); 682 + int kfd_event_create(struct file *devkfd, struct kfd_process *p, 683 + uint32_t event_type, bool auto_reset, uint32_t node_id, 684 + uint32_t *event_id, uint32_t *event_trigger_data, 685 + uint64_t *event_page_offset, uint32_t *event_slot_index); 686 + int kfd_event_destroy(struct kfd_process *p, uint32_t event_id); 682 687 683 688 #endif
+24
drivers/gpu/drm/amd/amdkfd/kfd_process.c
··· 178 178 kfree(pdd); 179 179 } 180 180 181 + kfd_event_free_process(p); 182 + 181 183 kfd_pasid_free(p->pasid); 182 184 183 185 mutex_unlock(&p->mutex); ··· 289 287 process->queue_array_size = INITIAL_QUEUE_ARRAY_SIZE; 290 288 291 289 INIT_LIST_HEAD(&process->per_device_data); 290 + 291 + kfd_event_init_process(process); 292 292 293 293 err = pqm_init(&process->pqm, process); 294 294 if (err != 0) ··· 433 429 bool kfd_has_process_device_data(struct kfd_process *p) 434 430 { 435 431 return !(list_empty(&p->per_device_data)); 432 + } 433 + 434 + /* This returns with process->mutex locked. */ 435 + struct kfd_process *kfd_lookup_process_by_pasid(unsigned int pasid) 436 + { 437 + struct kfd_process *p; 438 + unsigned int temp; 439 + 440 + int idx = srcu_read_lock(&kfd_processes_srcu); 441 + 442 + hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) { 443 + if (p->pasid == pasid) { 444 + mutex_lock(&p->mutex); 445 + break; 446 + } 447 + } 448 + 449 + srcu_read_unlock(&kfd_processes_srcu, idx); 450 + 451 + return p; 436 452 }