xref: /linux/drivers/gpu/drm/amd/amdkfd/kfd_events.c (revision 3a39d672e7f48b8d6b91a09afa4b55352773b4b5)
1d87f36a0SRajneesh Bhardwaj // SPDX-License-Identifier: GPL-2.0 OR MIT
2f3a39818SAndrew Lewycky /*
3d87f36a0SRajneesh Bhardwaj  * Copyright 2014-2022 Advanced Micro Devices, Inc.
4f3a39818SAndrew Lewycky  *
5f3a39818SAndrew Lewycky  * Permission is hereby granted, free of charge, to any person obtaining a
6f3a39818SAndrew Lewycky  * copy of this software and associated documentation files (the "Software"),
7f3a39818SAndrew Lewycky  * to deal in the Software without restriction, including without limitation
8f3a39818SAndrew Lewycky  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9f3a39818SAndrew Lewycky  * and/or sell copies of the Software, and to permit persons to whom the
10f3a39818SAndrew Lewycky  * Software is furnished to do so, subject to the following conditions:
11f3a39818SAndrew Lewycky  *
12f3a39818SAndrew Lewycky  * The above copyright notice and this permission notice shall be included in
13f3a39818SAndrew Lewycky  * all copies or substantial portions of the Software.
14f3a39818SAndrew Lewycky  *
15f3a39818SAndrew Lewycky  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16f3a39818SAndrew Lewycky  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17f3a39818SAndrew Lewycky  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18f3a39818SAndrew Lewycky  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19f3a39818SAndrew Lewycky  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20f3a39818SAndrew Lewycky  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21f3a39818SAndrew Lewycky  * OTHER DEALINGS IN THE SOFTWARE.
22f3a39818SAndrew Lewycky  */
23f3a39818SAndrew Lewycky 
24f3a39818SAndrew Lewycky #include <linux/mm_types.h>
25f3a39818SAndrew Lewycky #include <linux/slab.h>
26f3a39818SAndrew Lewycky #include <linux/types.h>
273f07c014SIngo Molnar #include <linux/sched/signal.h>
289b56bb11SFelix Kuehling #include <linux/sched/mm.h>
29f3a39818SAndrew Lewycky #include <linux/uaccess.h>
30f3a39818SAndrew Lewycky #include <linux/mman.h>
31f3a39818SAndrew Lewycky #include <linux/memory.h>
32f3a39818SAndrew Lewycky #include "kfd_priv.h"
33f3a39818SAndrew Lewycky #include "kfd_events.h"
34*ee0a469cSJonathan Kim #include "kfd_device_queue_manager.h"
3559d3e8beSAlexey Skidanov #include <linux/device.h>
36f3a39818SAndrew Lewycky 
37f3a39818SAndrew Lewycky /*
3874e40716SFelix Kuehling  * Wrapper around wait_queue_entry_t
39f3a39818SAndrew Lewycky  */
40f3a39818SAndrew Lewycky struct kfd_event_waiter {
4174e40716SFelix Kuehling 	wait_queue_entry_t wait;
4274e40716SFelix Kuehling 	struct kfd_event *event; /* Event to wait for */
4374e40716SFelix Kuehling 	bool activated;		 /* Becomes true when event is signaled */
4496cdb538SJames Zhu 	bool event_age_enabled;  /* set to true when last_event_age is non-zero */
45f3a39818SAndrew Lewycky };
46f3a39818SAndrew Lewycky 
47f3a39818SAndrew Lewycky /*
48f3a39818SAndrew Lewycky  * Each signal event needs a 64-bit signal slot where the signaler will write
49482f0777SFelix Kuehling  * a 1 before sending an interrupt. (This is needed because some interrupts
50f3a39818SAndrew Lewycky  * do not contain enough spare data bits to identify an event.)
51482f0777SFelix Kuehling  * We get whole pages and map them to the process VA.
52482f0777SFelix Kuehling  * Individual signal events use their event_id as slot index.
53f3a39818SAndrew Lewycky  */
5450cb7dd9SFelix Kuehling struct kfd_signal_page {
55f3a39818SAndrew Lewycky 	uint64_t *kernel_address;
56f3a39818SAndrew Lewycky 	uint64_t __user *user_address;
570fc8011fSFelix Kuehling 	bool need_to_free_pages;
58f3a39818SAndrew Lewycky };
59f3a39818SAndrew Lewycky 
page_slots(struct kfd_signal_page * page)6050cb7dd9SFelix Kuehling static uint64_t *page_slots(struct kfd_signal_page *page)
61f3a39818SAndrew Lewycky {
62f3a39818SAndrew Lewycky 	return page->kernel_address;
63f3a39818SAndrew Lewycky }
64f3a39818SAndrew Lewycky 
allocate_signal_page(struct kfd_process * p)6550cb7dd9SFelix Kuehling static struct kfd_signal_page *allocate_signal_page(struct kfd_process *p)
66f3a39818SAndrew Lewycky {
67f3a39818SAndrew Lewycky 	void *backing_store;
6850cb7dd9SFelix Kuehling 	struct kfd_signal_page *page;
69f3a39818SAndrew Lewycky 
7050cb7dd9SFelix Kuehling 	page = kzalloc(sizeof(*page), GFP_KERNEL);
71f3a39818SAndrew Lewycky 	if (!page)
7250cb7dd9SFelix Kuehling 		return NULL;
73f3a39818SAndrew Lewycky 
7450cb7dd9SFelix Kuehling 	backing_store = (void *) __get_free_pages(GFP_KERNEL,
75f3a39818SAndrew Lewycky 					get_order(KFD_SIGNAL_EVENT_LIMIT * 8));
76f3a39818SAndrew Lewycky 	if (!backing_store)
77f3a39818SAndrew Lewycky 		goto fail_alloc_signal_store;
78f3a39818SAndrew Lewycky 
7950cb7dd9SFelix Kuehling 	/* Initialize all events to unsignaled */
80f3a39818SAndrew Lewycky 	memset(backing_store, (uint8_t) UNSIGNALED_EVENT_SLOT,
81f3a39818SAndrew Lewycky 	       KFD_SIGNAL_EVENT_LIMIT * 8);
82f3a39818SAndrew Lewycky 
83f3a39818SAndrew Lewycky 	page->kernel_address = backing_store;
840fc8011fSFelix Kuehling 	page->need_to_free_pages = true;
8579775b62SKent Russell 	pr_debug("Allocated new event signal page at %p, for process %p\n",
86f3a39818SAndrew Lewycky 			page, p);
87f3a39818SAndrew Lewycky 
8850cb7dd9SFelix Kuehling 	return page;
89f3a39818SAndrew Lewycky 
90f3a39818SAndrew Lewycky fail_alloc_signal_store:
91f3a39818SAndrew Lewycky 	kfree(page);
9250cb7dd9SFelix Kuehling 	return NULL;
9350cb7dd9SFelix Kuehling }
9450cb7dd9SFelix Kuehling 
allocate_event_notification_slot(struct kfd_process * p,struct kfd_event * ev,const int * restore_id)95482f0777SFelix Kuehling static int allocate_event_notification_slot(struct kfd_process *p,
9640e8a766SDavid Yat Sin 					    struct kfd_event *ev,
9740e8a766SDavid Yat Sin 					    const int *restore_id)
9850cb7dd9SFelix Kuehling {
99482f0777SFelix Kuehling 	int id;
100482f0777SFelix Kuehling 
10150cb7dd9SFelix Kuehling 	if (!p->signal_page) {
10250cb7dd9SFelix Kuehling 		p->signal_page = allocate_signal_page(p);
10350cb7dd9SFelix Kuehling 		if (!p->signal_page)
104482f0777SFelix Kuehling 			return -ENOMEM;
105b9a5d0a5SFelix Kuehling 		/* Oldest user mode expects 256 event slots */
106b9a5d0a5SFelix Kuehling 		p->signal_mapped_size = 256*8;
107f3a39818SAndrew Lewycky 	}
108f3a39818SAndrew Lewycky 
10940e8a766SDavid Yat Sin 	if (restore_id) {
11040e8a766SDavid Yat Sin 		id = idr_alloc(&p->event_idr, ev, *restore_id, *restore_id + 1,
11140e8a766SDavid Yat Sin 				GFP_KERNEL);
11240e8a766SDavid Yat Sin 	} else {
113b9a5d0a5SFelix Kuehling 		/*
114b9a5d0a5SFelix Kuehling 		 * Compatibility with old user mode: Only use signal slots
115b9a5d0a5SFelix Kuehling 		 * user mode has mapped, may be less than
116b9a5d0a5SFelix Kuehling 		 * KFD_SIGNAL_EVENT_LIMIT. This also allows future increase
117b9a5d0a5SFelix Kuehling 		 * of the event limit without breaking user mode.
118b9a5d0a5SFelix Kuehling 		 */
119b9a5d0a5SFelix Kuehling 		id = idr_alloc(&p->event_idr, ev, 0, p->signal_mapped_size / 8,
120482f0777SFelix Kuehling 				GFP_KERNEL);
12140e8a766SDavid Yat Sin 	}
122482f0777SFelix Kuehling 	if (id < 0)
123482f0777SFelix Kuehling 		return id;
124f3a39818SAndrew Lewycky 
125482f0777SFelix Kuehling 	ev->event_id = id;
126482f0777SFelix Kuehling 	page_slots(p->signal_page)[id] = UNSIGNALED_EVENT_SLOT;
127f3a39818SAndrew Lewycky 
128482f0777SFelix Kuehling 	return 0;
129f3a39818SAndrew Lewycky }
130f3a39818SAndrew Lewycky 
131f3a39818SAndrew Lewycky /*
1325273e82cSFelix Kuehling  * Assumes that p->event_mutex or rcu_readlock is held and of course that p is
1335273e82cSFelix Kuehling  * not going away.
134f3a39818SAndrew Lewycky  */
lookup_event_by_id(struct kfd_process * p,uint32_t id)135f3a39818SAndrew Lewycky static struct kfd_event *lookup_event_by_id(struct kfd_process *p, uint32_t id)
136f3a39818SAndrew Lewycky {
137482f0777SFelix Kuehling 	return idr_find(&p->event_idr, id);
138f3a39818SAndrew Lewycky }
139f3a39818SAndrew Lewycky 
1403f04f961SFelix Kuehling /**
1413f04f961SFelix Kuehling  * lookup_signaled_event_by_partial_id - Lookup signaled event from partial ID
1423f04f961SFelix Kuehling  * @p:     Pointer to struct kfd_process
1433f04f961SFelix Kuehling  * @id:    ID to look up
1443f04f961SFelix Kuehling  * @bits:  Number of valid bits in @id
1453f04f961SFelix Kuehling  *
1463f04f961SFelix Kuehling  * Finds the first signaled event with a matching partial ID. If no
1473f04f961SFelix Kuehling  * matching signaled event is found, returns NULL. In that case the
1483f04f961SFelix Kuehling  * caller should assume that the partial ID is invalid and do an
1493f04f961SFelix Kuehling  * exhaustive search of all siglaned events.
1503f04f961SFelix Kuehling  *
1513f04f961SFelix Kuehling  * If multiple events with the same partial ID signal at the same
1523f04f961SFelix Kuehling  * time, they will be found one interrupt at a time, not necessarily
1533f04f961SFelix Kuehling  * in the same order the interrupts occurred. As long as the number of
1543f04f961SFelix Kuehling  * interrupts is correct, all signaled events will be seen by the
1553f04f961SFelix Kuehling  * driver.
1563f04f961SFelix Kuehling  */
lookup_signaled_event_by_partial_id(struct kfd_process * p,uint32_t id,uint32_t bits)1573f04f961SFelix Kuehling static struct kfd_event *lookup_signaled_event_by_partial_id(
1583f04f961SFelix Kuehling 	struct kfd_process *p, uint32_t id, uint32_t bits)
1593f04f961SFelix Kuehling {
1603f04f961SFelix Kuehling 	struct kfd_event *ev;
1613f04f961SFelix Kuehling 
1623f04f961SFelix Kuehling 	if (!p->signal_page || id >= KFD_SIGNAL_EVENT_LIMIT)
1633f04f961SFelix Kuehling 		return NULL;
1643f04f961SFelix Kuehling 
1653f04f961SFelix Kuehling 	/* Fast path for the common case that @id is not a partial ID
1663f04f961SFelix Kuehling 	 * and we only need a single lookup.
1673f04f961SFelix Kuehling 	 */
1683f04f961SFelix Kuehling 	if (bits > 31 || (1U << bits) >= KFD_SIGNAL_EVENT_LIMIT) {
1693f04f961SFelix Kuehling 		if (page_slots(p->signal_page)[id] == UNSIGNALED_EVENT_SLOT)
1703f04f961SFelix Kuehling 			return NULL;
1713f04f961SFelix Kuehling 
1723f04f961SFelix Kuehling 		return idr_find(&p->event_idr, id);
1733f04f961SFelix Kuehling 	}
1743f04f961SFelix Kuehling 
1753f04f961SFelix Kuehling 	/* General case for partial IDs: Iterate over all matching IDs
1763f04f961SFelix Kuehling 	 * and find the first one that has signaled.
1773f04f961SFelix Kuehling 	 */
1783f04f961SFelix Kuehling 	for (ev = NULL; id < KFD_SIGNAL_EVENT_LIMIT && !ev; id += 1U << bits) {
1793f04f961SFelix Kuehling 		if (page_slots(p->signal_page)[id] == UNSIGNALED_EVENT_SLOT)
1803f04f961SFelix Kuehling 			continue;
1813f04f961SFelix Kuehling 
1823f04f961SFelix Kuehling 		ev = idr_find(&p->event_idr, id);
1833f04f961SFelix Kuehling 	}
1843f04f961SFelix Kuehling 
1853f04f961SFelix Kuehling 	return ev;
1863f04f961SFelix Kuehling }
1873f04f961SFelix Kuehling 
create_signal_event(struct file * devkfd,struct kfd_process * p,struct kfd_event * ev,const int * restore_id)18840e8a766SDavid Yat Sin static int create_signal_event(struct file *devkfd, struct kfd_process *p,
18940e8a766SDavid Yat Sin 				struct kfd_event *ev, const int *restore_id)
190f3a39818SAndrew Lewycky {
191482f0777SFelix Kuehling 	int ret;
192482f0777SFelix Kuehling 
193b9a5d0a5SFelix Kuehling 	if (p->signal_mapped_size &&
194b9a5d0a5SFelix Kuehling 	    p->signal_event_count == p->signal_mapped_size / 8) {
195c986169fSFelix Kuehling 		if (!p->signal_event_limit_reached) {
1968f2e0c03SYong Zhao 			pr_debug("Signal event wasn't created because limit was reached\n");
197c986169fSFelix Kuehling 			p->signal_event_limit_reached = true;
198c986169fSFelix Kuehling 		}
199482f0777SFelix Kuehling 		return -ENOSPC;
200f3a39818SAndrew Lewycky 	}
201f3a39818SAndrew Lewycky 
20240e8a766SDavid Yat Sin 	ret = allocate_event_notification_slot(p, ev, restore_id);
203482f0777SFelix Kuehling 	if (ret) {
20479775b62SKent Russell 		pr_warn("Signal event wasn't created because out of kernel memory\n");
205482f0777SFelix Kuehling 		return ret;
206f3a39818SAndrew Lewycky 	}
207f3a39818SAndrew Lewycky 
208f3a39818SAndrew Lewycky 	p->signal_event_count++;
209f3a39818SAndrew Lewycky 
210482f0777SFelix Kuehling 	ev->user_signal_address = &p->signal_page->user_address[ev->event_id];
21179775b62SKent Russell 	pr_debug("Signal event number %zu created with id %d, address %p\n",
2126235e15eSOded Gabbay 			p->signal_event_count, ev->event_id,
2136235e15eSOded Gabbay 			ev->user_signal_address);
2146235e15eSOded Gabbay 
215f3a39818SAndrew Lewycky 	return 0;
216f3a39818SAndrew Lewycky }
217f3a39818SAndrew Lewycky 
create_other_event(struct kfd_process * p,struct kfd_event * ev,const int * restore_id)21840e8a766SDavid Yat Sin static int create_other_event(struct kfd_process *p, struct kfd_event *ev, const int *restore_id)
219f3a39818SAndrew Lewycky {
22040e8a766SDavid Yat Sin 	int id;
22140e8a766SDavid Yat Sin 
22240e8a766SDavid Yat Sin 	if (restore_id)
22340e8a766SDavid Yat Sin 		id = idr_alloc(&p->event_idr, ev, *restore_id, *restore_id + 1,
22440e8a766SDavid Yat Sin 			GFP_KERNEL);
22540e8a766SDavid Yat Sin 	else
226482f0777SFelix Kuehling 		/* Cast KFD_LAST_NONSIGNAL_EVENT to uint32_t. This allows an
227482f0777SFelix Kuehling 		 * intentional integer overflow to -1 without a compiler
228482f0777SFelix Kuehling 		 * warning. idr_alloc treats a negative value as "maximum
229482f0777SFelix Kuehling 		 * signed integer".
230482f0777SFelix Kuehling 		 */
23140e8a766SDavid Yat Sin 		id = idr_alloc(&p->event_idr, ev, KFD_FIRST_NONSIGNAL_EVENT_ID,
232482f0777SFelix Kuehling 				(uint32_t)KFD_LAST_NONSIGNAL_EVENT_ID + 1,
233482f0777SFelix Kuehling 				GFP_KERNEL);
234482f0777SFelix Kuehling 
235482f0777SFelix Kuehling 	if (id < 0)
236482f0777SFelix Kuehling 		return id;
237482f0777SFelix Kuehling 	ev->event_id = id;
238f3a39818SAndrew Lewycky 
239f3a39818SAndrew Lewycky 	return 0;
240f3a39818SAndrew Lewycky }
241f3a39818SAndrew Lewycky 
kfd_event_init_process(struct kfd_process * p)242c3eb12dfSFelix Kuehling int kfd_event_init_process(struct kfd_process *p)
243f3a39818SAndrew Lewycky {
244c3eb12dfSFelix Kuehling 	int id;
245c3eb12dfSFelix Kuehling 
246f3a39818SAndrew Lewycky 	mutex_init(&p->event_mutex);
247482f0777SFelix Kuehling 	idr_init(&p->event_idr);
24850cb7dd9SFelix Kuehling 	p->signal_page = NULL;
249c3eb12dfSFelix Kuehling 	p->signal_event_count = 1;
250c3eb12dfSFelix Kuehling 	/* Allocate event ID 0. It is used for a fast path to ignore bogus events
251c3eb12dfSFelix Kuehling 	 * that are sent by the CP without a context ID
252c3eb12dfSFelix Kuehling 	 */
253c3eb12dfSFelix Kuehling 	id = idr_alloc(&p->event_idr, NULL, 0, 1, GFP_KERNEL);
254c3eb12dfSFelix Kuehling 	if (id < 0) {
255c3eb12dfSFelix Kuehling 		idr_destroy(&p->event_idr);
256c3eb12dfSFelix Kuehling 		mutex_destroy(&p->event_mutex);
257c3eb12dfSFelix Kuehling 		return id;
258c3eb12dfSFelix Kuehling 	}
259c3eb12dfSFelix Kuehling 	return 0;
260f3a39818SAndrew Lewycky }
261f3a39818SAndrew Lewycky 
destroy_event(struct kfd_process * p,struct kfd_event * ev)262f3a39818SAndrew Lewycky static void destroy_event(struct kfd_process *p, struct kfd_event *ev)
263f3a39818SAndrew Lewycky {
26474e40716SFelix Kuehling 	struct kfd_event_waiter *waiter;
265fe528c13SFelix Kuehling 
26674e40716SFelix Kuehling 	/* Wake up pending waiters. They will return failure */
2675273e82cSFelix Kuehling 	spin_lock(&ev->lock);
26874e40716SFelix Kuehling 	list_for_each_entry(waiter, &ev->wq.head, wait.entry)
2695273e82cSFelix Kuehling 		WRITE_ONCE(waiter->event, NULL);
27074e40716SFelix Kuehling 	wake_up_all(&ev->wq);
2715273e82cSFelix Kuehling 	spin_unlock(&ev->lock);
272fe528c13SFelix Kuehling 
273482f0777SFelix Kuehling 	if (ev->type == KFD_EVENT_TYPE_SIGNAL ||
274482f0777SFelix Kuehling 	    ev->type == KFD_EVENT_TYPE_DEBUG)
275f3a39818SAndrew Lewycky 		p->signal_event_count--;
276f3a39818SAndrew Lewycky 
277482f0777SFelix Kuehling 	idr_remove(&p->event_idr, ev->event_id);
27834d292d5SFelix Kuehling 	kfree_rcu(ev, rcu);
279f3a39818SAndrew Lewycky }
280f3a39818SAndrew Lewycky 
destroy_events(struct kfd_process * p)281f3a39818SAndrew Lewycky static void destroy_events(struct kfd_process *p)
282f3a39818SAndrew Lewycky {
283f3a39818SAndrew Lewycky 	struct kfd_event *ev;
284482f0777SFelix Kuehling 	uint32_t id;
285f3a39818SAndrew Lewycky 
286482f0777SFelix Kuehling 	idr_for_each_entry(&p->event_idr, ev, id)
287c3eb12dfSFelix Kuehling 		if (ev)
288f3a39818SAndrew Lewycky 			destroy_event(p, ev);
289482f0777SFelix Kuehling 	idr_destroy(&p->event_idr);
290c3eb12dfSFelix Kuehling 	mutex_destroy(&p->event_mutex);
291f3a39818SAndrew Lewycky }
292f3a39818SAndrew Lewycky 
293f3a39818SAndrew Lewycky /*
294f3a39818SAndrew Lewycky  * We assume that the process is being destroyed and there is no need to
295f3a39818SAndrew Lewycky  * unmap the pages or keep bookkeeping data in order.
296f3a39818SAndrew Lewycky  */
shutdown_signal_page(struct kfd_process * p)29750cb7dd9SFelix Kuehling static void shutdown_signal_page(struct kfd_process *p)
298f3a39818SAndrew Lewycky {
29950cb7dd9SFelix Kuehling 	struct kfd_signal_page *page = p->signal_page;
300f3a39818SAndrew Lewycky 
30150cb7dd9SFelix Kuehling 	if (page) {
3020fc8011fSFelix Kuehling 		if (page->need_to_free_pages)
303f3a39818SAndrew Lewycky 			free_pages((unsigned long)page->kernel_address,
304f3a39818SAndrew Lewycky 				   get_order(KFD_SIGNAL_EVENT_LIMIT * 8));
305f3a39818SAndrew Lewycky 		kfree(page);
306f3a39818SAndrew Lewycky 	}
307f3a39818SAndrew Lewycky }
308f3a39818SAndrew Lewycky 
kfd_event_free_process(struct kfd_process * p)309f3a39818SAndrew Lewycky void kfd_event_free_process(struct kfd_process *p)
310f3a39818SAndrew Lewycky {
311f3a39818SAndrew Lewycky 	destroy_events(p);
31250cb7dd9SFelix Kuehling 	shutdown_signal_page(p);
313f3a39818SAndrew Lewycky }
314f3a39818SAndrew Lewycky 
event_can_be_gpu_signaled(const struct kfd_event * ev)315f3a39818SAndrew Lewycky static bool event_can_be_gpu_signaled(const struct kfd_event *ev)
316f3a39818SAndrew Lewycky {
317f3a39818SAndrew Lewycky 	return ev->type == KFD_EVENT_TYPE_SIGNAL ||
318f3a39818SAndrew Lewycky 					ev->type == KFD_EVENT_TYPE_DEBUG;
319f3a39818SAndrew Lewycky }
320f3a39818SAndrew Lewycky 
event_can_be_cpu_signaled(const struct kfd_event * ev)321f3a39818SAndrew Lewycky static bool event_can_be_cpu_signaled(const struct kfd_event *ev)
322f3a39818SAndrew Lewycky {
323f3a39818SAndrew Lewycky 	return ev->type == KFD_EVENT_TYPE_SIGNAL;
324f3a39818SAndrew Lewycky }
325f3a39818SAndrew Lewycky 
kfd_event_page_set(struct kfd_process * p,void * kernel_address,uint64_t size,uint64_t user_handle)32640e8a766SDavid Yat Sin static int kfd_event_page_set(struct kfd_process *p, void *kernel_address,
32740e8a766SDavid Yat Sin 		       uint64_t size, uint64_t user_handle)
3280fc8011fSFelix Kuehling {
3290fc8011fSFelix Kuehling 	struct kfd_signal_page *page;
3300fc8011fSFelix Kuehling 
3310fc8011fSFelix Kuehling 	if (p->signal_page)
3320fc8011fSFelix Kuehling 		return -EBUSY;
3330fc8011fSFelix Kuehling 
3340fc8011fSFelix Kuehling 	page = kzalloc(sizeof(*page), GFP_KERNEL);
3350fc8011fSFelix Kuehling 	if (!page)
3360fc8011fSFelix Kuehling 		return -ENOMEM;
3370fc8011fSFelix Kuehling 
3380fc8011fSFelix Kuehling 	/* Initialize all events to unsignaled */
3390fc8011fSFelix Kuehling 	memset(kernel_address, (uint8_t) UNSIGNALED_EVENT_SLOT,
3400fc8011fSFelix Kuehling 	       KFD_SIGNAL_EVENT_LIMIT * 8);
3410fc8011fSFelix Kuehling 
3420fc8011fSFelix Kuehling 	page->kernel_address = kernel_address;
3430fc8011fSFelix Kuehling 
3440fc8011fSFelix Kuehling 	p->signal_page = page;
3450fc8011fSFelix Kuehling 	p->signal_mapped_size = size;
34640e8a766SDavid Yat Sin 	p->signal_handle = user_handle;
3470fc8011fSFelix Kuehling 	return 0;
3480fc8011fSFelix Kuehling }
3490fc8011fSFelix Kuehling 
kfd_kmap_event_page(struct kfd_process * p,uint64_t event_page_offset)35040e8a766SDavid Yat Sin int kfd_kmap_event_page(struct kfd_process *p, uint64_t event_page_offset)
35140e8a766SDavid Yat Sin {
3528dc1db31SMukul Joshi 	struct kfd_node *kfd;
35340e8a766SDavid Yat Sin 	struct kfd_process_device *pdd;
35440e8a766SDavid Yat Sin 	void *mem, *kern_addr;
35540e8a766SDavid Yat Sin 	uint64_t size;
35640e8a766SDavid Yat Sin 	int err = 0;
35740e8a766SDavid Yat Sin 
35840e8a766SDavid Yat Sin 	if (p->signal_page) {
35940e8a766SDavid Yat Sin 		pr_err("Event page is already set\n");
36040e8a766SDavid Yat Sin 		return -EINVAL;
36140e8a766SDavid Yat Sin 	}
36240e8a766SDavid Yat Sin 
363bef153b7SDavid Yat Sin 	pdd = kfd_process_device_data_by_id(p, GET_GPU_ID(event_page_offset));
364bef153b7SDavid Yat Sin 	if (!pdd) {
36540e8a766SDavid Yat Sin 		pr_err("Getting device by id failed in %s\n", __func__);
36640e8a766SDavid Yat Sin 		return -EINVAL;
36740e8a766SDavid Yat Sin 	}
368bef153b7SDavid Yat Sin 	kfd = pdd->dev;
36940e8a766SDavid Yat Sin 
37040e8a766SDavid Yat Sin 	pdd = kfd_bind_process_to_device(kfd, p);
37140e8a766SDavid Yat Sin 	if (IS_ERR(pdd))
37240e8a766SDavid Yat Sin 		return PTR_ERR(pdd);
37340e8a766SDavid Yat Sin 
37440e8a766SDavid Yat Sin 	mem = kfd_process_device_translate_handle(pdd,
37540e8a766SDavid Yat Sin 			GET_IDR_HANDLE(event_page_offset));
37640e8a766SDavid Yat Sin 	if (!mem) {
37740e8a766SDavid Yat Sin 		pr_err("Can't find BO, offset is 0x%llx\n", event_page_offset);
37840e8a766SDavid Yat Sin 		return -EINVAL;
37940e8a766SDavid Yat Sin 	}
38040e8a766SDavid Yat Sin 
3814e2d1044SFelix Kuehling 	err = amdgpu_amdkfd_gpuvm_map_gtt_bo_to_kernel(mem, &kern_addr, &size);
38240e8a766SDavid Yat Sin 	if (err) {
38340e8a766SDavid Yat Sin 		pr_err("Failed to map event page to kernel\n");
38440e8a766SDavid Yat Sin 		return err;
38540e8a766SDavid Yat Sin 	}
38640e8a766SDavid Yat Sin 
38740e8a766SDavid Yat Sin 	err = kfd_event_page_set(p, kern_addr, size, event_page_offset);
38840e8a766SDavid Yat Sin 	if (err) {
38940e8a766SDavid Yat Sin 		pr_err("Failed to set event page\n");
3904e2d1044SFelix Kuehling 		amdgpu_amdkfd_gpuvm_unmap_gtt_bo_from_kernel(mem);
39140e8a766SDavid Yat Sin 		return err;
39240e8a766SDavid Yat Sin 	}
39340e8a766SDavid Yat Sin 	return err;
39440e8a766SDavid Yat Sin }
39540e8a766SDavid Yat Sin 
kfd_event_create(struct file * devkfd,struct kfd_process * p,uint32_t event_type,bool auto_reset,uint32_t node_id,uint32_t * event_id,uint32_t * event_trigger_data,uint64_t * event_page_offset,uint32_t * event_slot_index)396f3a39818SAndrew Lewycky int kfd_event_create(struct file *devkfd, struct kfd_process *p,
397f3a39818SAndrew Lewycky 		     uint32_t event_type, bool auto_reset, uint32_t node_id,
398f3a39818SAndrew Lewycky 		     uint32_t *event_id, uint32_t *event_trigger_data,
399f3a39818SAndrew Lewycky 		     uint64_t *event_page_offset, uint32_t *event_slot_index)
400f3a39818SAndrew Lewycky {
401f3a39818SAndrew Lewycky 	int ret = 0;
402f3a39818SAndrew Lewycky 	struct kfd_event *ev = kzalloc(sizeof(*ev), GFP_KERNEL);
403f3a39818SAndrew Lewycky 
404f3a39818SAndrew Lewycky 	if (!ev)
405f3a39818SAndrew Lewycky 		return -ENOMEM;
406f3a39818SAndrew Lewycky 
407f3a39818SAndrew Lewycky 	ev->type = event_type;
408f3a39818SAndrew Lewycky 	ev->auto_reset = auto_reset;
409f3a39818SAndrew Lewycky 	ev->signaled = false;
410f3a39818SAndrew Lewycky 
4115273e82cSFelix Kuehling 	spin_lock_init(&ev->lock);
41274e40716SFelix Kuehling 	init_waitqueue_head(&ev->wq);
413f3a39818SAndrew Lewycky 
414f3a39818SAndrew Lewycky 	*event_page_offset = 0;
415f3a39818SAndrew Lewycky 
416f3a39818SAndrew Lewycky 	mutex_lock(&p->event_mutex);
417f3a39818SAndrew Lewycky 
418f3a39818SAndrew Lewycky 	switch (event_type) {
419f3a39818SAndrew Lewycky 	case KFD_EVENT_TYPE_SIGNAL:
420f3a39818SAndrew Lewycky 	case KFD_EVENT_TYPE_DEBUG:
42140e8a766SDavid Yat Sin 		ret = create_signal_event(devkfd, p, ev, NULL);
422f3a39818SAndrew Lewycky 		if (!ret) {
423df03ef93SHarish Kasiviswanathan 			*event_page_offset = KFD_MMAP_TYPE_EVENTS;
424482f0777SFelix Kuehling 			*event_slot_index = ev->event_id;
425f3a39818SAndrew Lewycky 		}
426f3a39818SAndrew Lewycky 		break;
427f3a39818SAndrew Lewycky 	default:
42840e8a766SDavid Yat Sin 		ret = create_other_event(p, ev, NULL);
429f3a39818SAndrew Lewycky 		break;
430f3a39818SAndrew Lewycky 	}
431f3a39818SAndrew Lewycky 
432f3a39818SAndrew Lewycky 	if (!ret) {
433f3a39818SAndrew Lewycky 		*event_id = ev->event_id;
434f3a39818SAndrew Lewycky 		*event_trigger_data = ev->event_id;
4354057e6ceSJames Zhu 		ev->event_age = 1;
436f3a39818SAndrew Lewycky 	} else {
437f3a39818SAndrew Lewycky 		kfree(ev);
438f3a39818SAndrew Lewycky 	}
439f3a39818SAndrew Lewycky 
440f3a39818SAndrew Lewycky 	mutex_unlock(&p->event_mutex);
441f3a39818SAndrew Lewycky 
442f3a39818SAndrew Lewycky 	return ret;
443f3a39818SAndrew Lewycky }
444f3a39818SAndrew Lewycky 
kfd_criu_restore_event(struct file * devkfd,struct kfd_process * p,uint8_t __user * user_priv_ptr,uint64_t * priv_data_offset,uint64_t max_priv_data_size)44540e8a766SDavid Yat Sin int kfd_criu_restore_event(struct file *devkfd,
44640e8a766SDavid Yat Sin 			   struct kfd_process *p,
44740e8a766SDavid Yat Sin 			   uint8_t __user *user_priv_ptr,
44840e8a766SDavid Yat Sin 			   uint64_t *priv_data_offset,
44940e8a766SDavid Yat Sin 			   uint64_t max_priv_data_size)
45040e8a766SDavid Yat Sin {
45140e8a766SDavid Yat Sin 	struct kfd_criu_event_priv_data *ev_priv;
45240e8a766SDavid Yat Sin 	struct kfd_event *ev = NULL;
45340e8a766SDavid Yat Sin 	int ret = 0;
45440e8a766SDavid Yat Sin 
45540e8a766SDavid Yat Sin 	ev_priv = kmalloc(sizeof(*ev_priv), GFP_KERNEL);
45640e8a766SDavid Yat Sin 	if (!ev_priv)
45740e8a766SDavid Yat Sin 		return -ENOMEM;
45840e8a766SDavid Yat Sin 
45940e8a766SDavid Yat Sin 	ev = kzalloc(sizeof(*ev), GFP_KERNEL);
46040e8a766SDavid Yat Sin 	if (!ev) {
46140e8a766SDavid Yat Sin 		ret = -ENOMEM;
46240e8a766SDavid Yat Sin 		goto exit;
46340e8a766SDavid Yat Sin 	}
46440e8a766SDavid Yat Sin 
46540e8a766SDavid Yat Sin 	if (*priv_data_offset + sizeof(*ev_priv) > max_priv_data_size) {
46640e8a766SDavid Yat Sin 		ret = -EINVAL;
46740e8a766SDavid Yat Sin 		goto exit;
46840e8a766SDavid Yat Sin 	}
46940e8a766SDavid Yat Sin 
47040e8a766SDavid Yat Sin 	ret = copy_from_user(ev_priv, user_priv_ptr + *priv_data_offset, sizeof(*ev_priv));
47140e8a766SDavid Yat Sin 	if (ret) {
47240e8a766SDavid Yat Sin 		ret = -EFAULT;
47340e8a766SDavid Yat Sin 		goto exit;
47440e8a766SDavid Yat Sin 	}
47540e8a766SDavid Yat Sin 	*priv_data_offset += sizeof(*ev_priv);
47640e8a766SDavid Yat Sin 
47740e8a766SDavid Yat Sin 	if (ev_priv->user_handle) {
47840e8a766SDavid Yat Sin 		ret = kfd_kmap_event_page(p, ev_priv->user_handle);
47940e8a766SDavid Yat Sin 		if (ret)
48040e8a766SDavid Yat Sin 			goto exit;
48140e8a766SDavid Yat Sin 	}
48240e8a766SDavid Yat Sin 
48340e8a766SDavid Yat Sin 	ev->type = ev_priv->type;
48440e8a766SDavid Yat Sin 	ev->auto_reset = ev_priv->auto_reset;
48540e8a766SDavid Yat Sin 	ev->signaled = ev_priv->signaled;
48640e8a766SDavid Yat Sin 
4875273e82cSFelix Kuehling 	spin_lock_init(&ev->lock);
48840e8a766SDavid Yat Sin 	init_waitqueue_head(&ev->wq);
48940e8a766SDavid Yat Sin 
49040e8a766SDavid Yat Sin 	mutex_lock(&p->event_mutex);
49140e8a766SDavid Yat Sin 	switch (ev->type) {
49240e8a766SDavid Yat Sin 	case KFD_EVENT_TYPE_SIGNAL:
49340e8a766SDavid Yat Sin 	case KFD_EVENT_TYPE_DEBUG:
49440e8a766SDavid Yat Sin 		ret = create_signal_event(devkfd, p, ev, &ev_priv->event_id);
49540e8a766SDavid Yat Sin 		break;
49640e8a766SDavid Yat Sin 	case KFD_EVENT_TYPE_MEMORY:
49740e8a766SDavid Yat Sin 		memcpy(&ev->memory_exception_data,
49840e8a766SDavid Yat Sin 			&ev_priv->memory_exception_data,
49940e8a766SDavid Yat Sin 			sizeof(struct kfd_hsa_memory_exception_data));
50040e8a766SDavid Yat Sin 
50140e8a766SDavid Yat Sin 		ret = create_other_event(p, ev, &ev_priv->event_id);
50240e8a766SDavid Yat Sin 		break;
50340e8a766SDavid Yat Sin 	case KFD_EVENT_TYPE_HW_EXCEPTION:
50440e8a766SDavid Yat Sin 		memcpy(&ev->hw_exception_data,
50540e8a766SDavid Yat Sin 			&ev_priv->hw_exception_data,
50640e8a766SDavid Yat Sin 			sizeof(struct kfd_hsa_hw_exception_data));
50740e8a766SDavid Yat Sin 
50840e8a766SDavid Yat Sin 		ret = create_other_event(p, ev, &ev_priv->event_id);
50940e8a766SDavid Yat Sin 		break;
51040e8a766SDavid Yat Sin 	}
51166f79037SFelix Kuehling 	mutex_unlock(&p->event_mutex);
51240e8a766SDavid Yat Sin 
51340e8a766SDavid Yat Sin exit:
51440e8a766SDavid Yat Sin 	if (ret)
51540e8a766SDavid Yat Sin 		kfree(ev);
51640e8a766SDavid Yat Sin 
51740e8a766SDavid Yat Sin 	kfree(ev_priv);
51840e8a766SDavid Yat Sin 
51940e8a766SDavid Yat Sin 	return ret;
52040e8a766SDavid Yat Sin }
52140e8a766SDavid Yat Sin 
kfd_criu_checkpoint_events(struct kfd_process * p,uint8_t __user * user_priv_data,uint64_t * priv_data_offset)52240e8a766SDavid Yat Sin int kfd_criu_checkpoint_events(struct kfd_process *p,
52340e8a766SDavid Yat Sin 			 uint8_t __user *user_priv_data,
52440e8a766SDavid Yat Sin 			 uint64_t *priv_data_offset)
52540e8a766SDavid Yat Sin {
52640e8a766SDavid Yat Sin 	struct kfd_criu_event_priv_data *ev_privs;
52740e8a766SDavid Yat Sin 	int i = 0;
52840e8a766SDavid Yat Sin 	int ret =  0;
52940e8a766SDavid Yat Sin 	struct kfd_event *ev;
53040e8a766SDavid Yat Sin 	uint32_t ev_id;
53140e8a766SDavid Yat Sin 
53240e8a766SDavid Yat Sin 	uint32_t num_events = kfd_get_num_events(p);
53340e8a766SDavid Yat Sin 
53440e8a766SDavid Yat Sin 	if (!num_events)
53540e8a766SDavid Yat Sin 		return 0;
53640e8a766SDavid Yat Sin 
53740e8a766SDavid Yat Sin 	ev_privs = kvzalloc(num_events * sizeof(*ev_privs), GFP_KERNEL);
53840e8a766SDavid Yat Sin 	if (!ev_privs)
53940e8a766SDavid Yat Sin 		return -ENOMEM;
54040e8a766SDavid Yat Sin 
54140e8a766SDavid Yat Sin 
54240e8a766SDavid Yat Sin 	idr_for_each_entry(&p->event_idr, ev, ev_id) {
54340e8a766SDavid Yat Sin 		struct kfd_criu_event_priv_data *ev_priv;
54440e8a766SDavid Yat Sin 
54540e8a766SDavid Yat Sin 		/*
54640e8a766SDavid Yat Sin 		 * Currently, all events have same size of private_data, but the current ioctl's
54740e8a766SDavid Yat Sin 		 * and CRIU plugin supports private_data of variable sizes
54840e8a766SDavid Yat Sin 		 */
54940e8a766SDavid Yat Sin 		ev_priv = &ev_privs[i];
55040e8a766SDavid Yat Sin 
55140e8a766SDavid Yat Sin 		ev_priv->object_type = KFD_CRIU_OBJECT_TYPE_EVENT;
55240e8a766SDavid Yat Sin 
55340e8a766SDavid Yat Sin 		/* We store the user_handle with the first event */
55440e8a766SDavid Yat Sin 		if (i == 0 && p->signal_page)
55540e8a766SDavid Yat Sin 			ev_priv->user_handle = p->signal_handle;
55640e8a766SDavid Yat Sin 
55740e8a766SDavid Yat Sin 		ev_priv->event_id = ev->event_id;
55840e8a766SDavid Yat Sin 		ev_priv->auto_reset = ev->auto_reset;
55940e8a766SDavid Yat Sin 		ev_priv->type = ev->type;
56040e8a766SDavid Yat Sin 		ev_priv->signaled = ev->signaled;
56140e8a766SDavid Yat Sin 
56240e8a766SDavid Yat Sin 		if (ev_priv->type == KFD_EVENT_TYPE_MEMORY)
56340e8a766SDavid Yat Sin 			memcpy(&ev_priv->memory_exception_data,
56440e8a766SDavid Yat Sin 				&ev->memory_exception_data,
56540e8a766SDavid Yat Sin 				sizeof(struct kfd_hsa_memory_exception_data));
56640e8a766SDavid Yat Sin 		else if (ev_priv->type == KFD_EVENT_TYPE_HW_EXCEPTION)
56740e8a766SDavid Yat Sin 			memcpy(&ev_priv->hw_exception_data,
56840e8a766SDavid Yat Sin 				&ev->hw_exception_data,
56940e8a766SDavid Yat Sin 				sizeof(struct kfd_hsa_hw_exception_data));
57040e8a766SDavid Yat Sin 
57140e8a766SDavid Yat Sin 		pr_debug("Checkpointed event[%d] id = 0x%08x auto_reset = %x type = %x signaled = %x\n",
57240e8a766SDavid Yat Sin 			  i,
57340e8a766SDavid Yat Sin 			  ev_priv->event_id,
57440e8a766SDavid Yat Sin 			  ev_priv->auto_reset,
57540e8a766SDavid Yat Sin 			  ev_priv->type,
57640e8a766SDavid Yat Sin 			  ev_priv->signaled);
57740e8a766SDavid Yat Sin 		i++;
57840e8a766SDavid Yat Sin 	}
57940e8a766SDavid Yat Sin 
58040e8a766SDavid Yat Sin 	ret = copy_to_user(user_priv_data + *priv_data_offset,
58140e8a766SDavid Yat Sin 			   ev_privs, num_events * sizeof(*ev_privs));
58240e8a766SDavid Yat Sin 	if (ret) {
58340e8a766SDavid Yat Sin 		pr_err("Failed to copy events priv to user\n");
58440e8a766SDavid Yat Sin 		ret = -EFAULT;
58540e8a766SDavid Yat Sin 	}
58640e8a766SDavid Yat Sin 
58740e8a766SDavid Yat Sin 	*priv_data_offset += num_events * sizeof(*ev_privs);
58840e8a766SDavid Yat Sin 
58940e8a766SDavid Yat Sin 	kvfree(ev_privs);
59040e8a766SDavid Yat Sin 	return ret;
59140e8a766SDavid Yat Sin }
59240e8a766SDavid Yat Sin 
kfd_get_num_events(struct kfd_process * p)59340e8a766SDavid Yat Sin int kfd_get_num_events(struct kfd_process *p)
59440e8a766SDavid Yat Sin {
59540e8a766SDavid Yat Sin 	struct kfd_event *ev;
59640e8a766SDavid Yat Sin 	uint32_t id;
59740e8a766SDavid Yat Sin 	u32 num_events = 0;
59840e8a766SDavid Yat Sin 
59940e8a766SDavid Yat Sin 	idr_for_each_entry(&p->event_idr, ev, id)
60040e8a766SDavid Yat Sin 		num_events++;
60140e8a766SDavid Yat Sin 
60240e8a766SDavid Yat Sin 	return num_events;
60340e8a766SDavid Yat Sin }
60440e8a766SDavid Yat Sin 
605f3a39818SAndrew Lewycky /* Assumes that p is current. */
kfd_event_destroy(struct kfd_process * p,uint32_t event_id)606f3a39818SAndrew Lewycky int kfd_event_destroy(struct kfd_process *p, uint32_t event_id)
607f3a39818SAndrew Lewycky {
608f3a39818SAndrew Lewycky 	struct kfd_event *ev;
609f3a39818SAndrew Lewycky 	int ret = 0;
610f3a39818SAndrew Lewycky 
611f3a39818SAndrew Lewycky 	mutex_lock(&p->event_mutex);
612f3a39818SAndrew Lewycky 
613f3a39818SAndrew Lewycky 	ev = lookup_event_by_id(p, event_id);
614f3a39818SAndrew Lewycky 
615f3a39818SAndrew Lewycky 	if (ev)
616f3a39818SAndrew Lewycky 		destroy_event(p, ev);
617f3a39818SAndrew Lewycky 	else
618f3a39818SAndrew Lewycky 		ret = -EINVAL;
619f3a39818SAndrew Lewycky 
620f3a39818SAndrew Lewycky 	mutex_unlock(&p->event_mutex);
621f3a39818SAndrew Lewycky 	return ret;
622f3a39818SAndrew Lewycky }
623f3a39818SAndrew Lewycky 
set_event(struct kfd_event * ev)624f3a39818SAndrew Lewycky static void set_event(struct kfd_event *ev)
625f3a39818SAndrew Lewycky {
626f3a39818SAndrew Lewycky 	struct kfd_event_waiter *waiter;
627f3a39818SAndrew Lewycky 
62874e40716SFelix Kuehling 	/* Auto reset if the list is non-empty and we're waking
62974e40716SFelix Kuehling 	 * someone. waitqueue_active is safe here because we're
6305273e82cSFelix Kuehling 	 * protected by the ev->lock, which is also held when
63174e40716SFelix Kuehling 	 * updating the wait queues in kfd_wait_on_events.
63274e40716SFelix Kuehling 	 */
63374e40716SFelix Kuehling 	ev->signaled = !ev->auto_reset || !waitqueue_active(&ev->wq);
6344057e6ceSJames Zhu 	if (!(++ev->event_age)) {
6354057e6ceSJames Zhu 		/* Never wrap back to reserved/default event age 0/1 */
6364057e6ceSJames Zhu 		ev->event_age = 2;
6374057e6ceSJames Zhu 		WARN_ONCE(1, "event_age wrap back!");
6384057e6ceSJames Zhu 	}
639f3a39818SAndrew Lewycky 
64074e40716SFelix Kuehling 	list_for_each_entry(waiter, &ev->wq.head, wait.entry)
6415273e82cSFelix Kuehling 		WRITE_ONCE(waiter->activated, true);
642f3a39818SAndrew Lewycky 
64374e40716SFelix Kuehling 	wake_up_all(&ev->wq);
644f3a39818SAndrew Lewycky }
645f3a39818SAndrew Lewycky 
646f3a39818SAndrew Lewycky /* Assumes that p is current. */
kfd_set_event(struct kfd_process * p,uint32_t event_id)647f3a39818SAndrew Lewycky int kfd_set_event(struct kfd_process *p, uint32_t event_id)
648f3a39818SAndrew Lewycky {
649f3a39818SAndrew Lewycky 	int ret = 0;
650f3a39818SAndrew Lewycky 	struct kfd_event *ev;
651f3a39818SAndrew Lewycky 
6525273e82cSFelix Kuehling 	rcu_read_lock();
653f3a39818SAndrew Lewycky 
654f3a39818SAndrew Lewycky 	ev = lookup_event_by_id(p, event_id);
655abb5bc59SDan Carpenter 	if (!ev) {
656abb5bc59SDan Carpenter 		ret = -EINVAL;
657abb5bc59SDan Carpenter 		goto unlock_rcu;
658abb5bc59SDan Carpenter 	}
6595273e82cSFelix Kuehling 	spin_lock(&ev->lock);
660f3a39818SAndrew Lewycky 
661abb5bc59SDan Carpenter 	if (event_can_be_cpu_signaled(ev))
662f3a39818SAndrew Lewycky 		set_event(ev);
663f3a39818SAndrew Lewycky 	else
664f3a39818SAndrew Lewycky 		ret = -EINVAL;
665f3a39818SAndrew Lewycky 
6665273e82cSFelix Kuehling 	spin_unlock(&ev->lock);
667abb5bc59SDan Carpenter unlock_rcu:
6685273e82cSFelix Kuehling 	rcu_read_unlock();
669f3a39818SAndrew Lewycky 	return ret;
670f3a39818SAndrew Lewycky }
671f3a39818SAndrew Lewycky 
reset_event(struct kfd_event * ev)672f3a39818SAndrew Lewycky static void reset_event(struct kfd_event *ev)
673f3a39818SAndrew Lewycky {
674f3a39818SAndrew Lewycky 	ev->signaled = false;
675f3a39818SAndrew Lewycky }
676f3a39818SAndrew Lewycky 
677f3a39818SAndrew Lewycky /* Assumes that p is current. */
kfd_reset_event(struct kfd_process * p,uint32_t event_id)678f3a39818SAndrew Lewycky int kfd_reset_event(struct kfd_process *p, uint32_t event_id)
679f3a39818SAndrew Lewycky {
680f3a39818SAndrew Lewycky 	int ret = 0;
681f3a39818SAndrew Lewycky 	struct kfd_event *ev;
682f3a39818SAndrew Lewycky 
6835273e82cSFelix Kuehling 	rcu_read_lock();
684f3a39818SAndrew Lewycky 
685f3a39818SAndrew Lewycky 	ev = lookup_event_by_id(p, event_id);
686abb5bc59SDan Carpenter 	if (!ev) {
687abb5bc59SDan Carpenter 		ret = -EINVAL;
688abb5bc59SDan Carpenter 		goto unlock_rcu;
689abb5bc59SDan Carpenter 	}
6905273e82cSFelix Kuehling 	spin_lock(&ev->lock);
691f3a39818SAndrew Lewycky 
692abb5bc59SDan Carpenter 	if (event_can_be_cpu_signaled(ev))
693f3a39818SAndrew Lewycky 		reset_event(ev);
694f3a39818SAndrew Lewycky 	else
695f3a39818SAndrew Lewycky 		ret = -EINVAL;
696f3a39818SAndrew Lewycky 
6975273e82cSFelix Kuehling 	spin_unlock(&ev->lock);
698abb5bc59SDan Carpenter unlock_rcu:
6995273e82cSFelix Kuehling 	rcu_read_unlock();
700f3a39818SAndrew Lewycky 	return ret;
701f3a39818SAndrew Lewycky 
702f3a39818SAndrew Lewycky }
703f3a39818SAndrew Lewycky 
acknowledge_signal(struct kfd_process * p,struct kfd_event * ev)704f3a39818SAndrew Lewycky static void acknowledge_signal(struct kfd_process *p, struct kfd_event *ev)
705f3a39818SAndrew Lewycky {
7065273e82cSFelix Kuehling 	WRITE_ONCE(page_slots(p->signal_page)[ev->event_id], UNSIGNALED_EVENT_SLOT);
707f3a39818SAndrew Lewycky }
708f3a39818SAndrew Lewycky 
set_event_from_interrupt(struct kfd_process * p,struct kfd_event * ev)709f3a39818SAndrew Lewycky static void set_event_from_interrupt(struct kfd_process *p,
710f3a39818SAndrew Lewycky 					struct kfd_event *ev)
711f3a39818SAndrew Lewycky {
712f3a39818SAndrew Lewycky 	if (ev && event_can_be_gpu_signaled(ev)) {
713f3a39818SAndrew Lewycky 		acknowledge_signal(p, ev);
7145273e82cSFelix Kuehling 		spin_lock(&ev->lock);
715f3a39818SAndrew Lewycky 		set_event(ev);
7165273e82cSFelix Kuehling 		spin_unlock(&ev->lock);
717f3a39818SAndrew Lewycky 	}
718f3a39818SAndrew Lewycky }
719f3a39818SAndrew Lewycky 
kfd_signal_event_interrupt(u32 pasid,uint32_t partial_id,uint32_t valid_id_bits)720c7b6bac9SFenghua Yu void kfd_signal_event_interrupt(u32 pasid, uint32_t partial_id,
721f3a39818SAndrew Lewycky 				uint32_t valid_id_bits)
722f3a39818SAndrew Lewycky {
7233f04f961SFelix Kuehling 	struct kfd_event *ev = NULL;
724f3a39818SAndrew Lewycky 
725f3a39818SAndrew Lewycky 	/*
726f3a39818SAndrew Lewycky 	 * Because we are called from arbitrary context (workqueue) as opposed
727f3a39818SAndrew Lewycky 	 * to process context, kfd_process could attempt to exit while we are
728abb208a8SFelix Kuehling 	 * running so the lookup function increments the process ref count.
729f3a39818SAndrew Lewycky 	 */
730f3a39818SAndrew Lewycky 	struct kfd_process *p = kfd_lookup_process_by_pasid(pasid);
731f3a39818SAndrew Lewycky 
732f3a39818SAndrew Lewycky 	if (!p)
733f3a39818SAndrew Lewycky 		return; /* Presumably process exited. */
734f3a39818SAndrew Lewycky 
7355273e82cSFelix Kuehling 	rcu_read_lock();
736f3a39818SAndrew Lewycky 
7373f04f961SFelix Kuehling 	if (valid_id_bits)
7383f04f961SFelix Kuehling 		ev = lookup_signaled_event_by_partial_id(p, partial_id,
7393f04f961SFelix Kuehling 							 valid_id_bits);
7403f04f961SFelix Kuehling 	if (ev) {
741f3a39818SAndrew Lewycky 		set_event_from_interrupt(p, ev);
74250cb7dd9SFelix Kuehling 	} else if (p->signal_page) {
743f3a39818SAndrew Lewycky 		/*
7443f04f961SFelix Kuehling 		 * Partial ID lookup failed. Assume that the event ID
7453f04f961SFelix Kuehling 		 * in the interrupt payload was invalid and do an
7463f04f961SFelix Kuehling 		 * exhaustive search of signaled events.
747f3a39818SAndrew Lewycky 		 */
748482f0777SFelix Kuehling 		uint64_t *slots = page_slots(p->signal_page);
749482f0777SFelix Kuehling 		uint32_t id;
750f3a39818SAndrew Lewycky 
7513f04f961SFelix Kuehling 		if (valid_id_bits)
7523f04f961SFelix Kuehling 			pr_debug_ratelimited("Partial ID invalid: %u (%u valid bits)\n",
7533f04f961SFelix Kuehling 					     partial_id, valid_id_bits);
7543f04f961SFelix Kuehling 
755eeb27b7eSFelix Kuehling 		if (p->signal_event_count < KFD_SIGNAL_EVENT_LIMIT / 64) {
756482f0777SFelix Kuehling 			/* With relatively few events, it's faster to
757482f0777SFelix Kuehling 			 * iterate over the event IDR
758482f0777SFelix Kuehling 			 */
759482f0777SFelix Kuehling 			idr_for_each_entry(&p->event_idr, ev, id) {
760482f0777SFelix Kuehling 				if (id >= KFD_SIGNAL_EVENT_LIMIT)
761482f0777SFelix Kuehling 					break;
762482f0777SFelix Kuehling 
7635273e82cSFelix Kuehling 				if (READ_ONCE(slots[id]) != UNSIGNALED_EVENT_SLOT)
764f3a39818SAndrew Lewycky 					set_event_from_interrupt(p, ev);
765f3a39818SAndrew Lewycky 			}
766482f0777SFelix Kuehling 		} else {
767482f0777SFelix Kuehling 			/* With relatively many events, it's faster to
768482f0777SFelix Kuehling 			 * iterate over the signal slots and lookup
769482f0777SFelix Kuehling 			 * only signaled events from the IDR.
770482f0777SFelix Kuehling 			 */
771c3eb12dfSFelix Kuehling 			for (id = 1; id < KFD_SIGNAL_EVENT_LIMIT; id++)
7725273e82cSFelix Kuehling 				if (READ_ONCE(slots[id]) != UNSIGNALED_EVENT_SLOT) {
773482f0777SFelix Kuehling 					ev = lookup_event_by_id(p, id);
774482f0777SFelix Kuehling 					set_event_from_interrupt(p, ev);
775482f0777SFelix Kuehling 				}
776482f0777SFelix Kuehling 		}
777f3a39818SAndrew Lewycky 	}
778f3a39818SAndrew Lewycky 
7795273e82cSFelix Kuehling 	rcu_read_unlock();
780abb208a8SFelix Kuehling 	kfd_unref_process(p);
781f3a39818SAndrew Lewycky }
782f3a39818SAndrew Lewycky 
alloc_event_waiters(uint32_t num_events)783f3a39818SAndrew Lewycky static struct kfd_event_waiter *alloc_event_waiters(uint32_t num_events)
784f3a39818SAndrew Lewycky {
785f3a39818SAndrew Lewycky 	struct kfd_event_waiter *event_waiters;
786f3a39818SAndrew Lewycky 	uint32_t i;
787f3a39818SAndrew Lewycky 
7884fc8fff3SQu Huang 	event_waiters = kcalloc(num_events, sizeof(struct kfd_event_waiter),
789f3a39818SAndrew Lewycky 				GFP_KERNEL);
790ebbb7bb9SQintaoShen 	if (!event_waiters)
791ebbb7bb9SQintaoShen 		return NULL;
792f3a39818SAndrew Lewycky 
7934fc8fff3SQu Huang 	for (i = 0; i < num_events; i++)
79474e40716SFelix Kuehling 		init_wait(&event_waiters[i].wait);
795f3a39818SAndrew Lewycky 
796f3a39818SAndrew Lewycky 	return event_waiters;
797f3a39818SAndrew Lewycky }
798f3a39818SAndrew Lewycky 
init_event_waiter(struct kfd_process * p,struct kfd_event_waiter * waiter,struct kfd_event_data * event_data)799250e64a3SFelix Kuehling static int init_event_waiter(struct kfd_process *p,
800f3a39818SAndrew Lewycky 		struct kfd_event_waiter *waiter,
80196cdb538SJames Zhu 		struct kfd_event_data *event_data)
802f3a39818SAndrew Lewycky {
80396cdb538SJames Zhu 	struct kfd_event *ev = lookup_event_by_id(p, event_data->event_id);
804f3a39818SAndrew Lewycky 
805f3a39818SAndrew Lewycky 	if (!ev)
806f3a39818SAndrew Lewycky 		return -EINVAL;
807f3a39818SAndrew Lewycky 
8085273e82cSFelix Kuehling 	spin_lock(&ev->lock);
80959d3e8beSAlexey Skidanov 	waiter->event = ev;
810f3a39818SAndrew Lewycky 	waiter->activated = ev->signaled;
811f3a39818SAndrew Lewycky 	ev->signaled = ev->signaled && !ev->auto_reset;
81296cdb538SJames Zhu 
81396cdb538SJames Zhu 	/* last_event_age = 0 reserved for backward compatible */
81496cdb538SJames Zhu 	if (waiter->event->type == KFD_EVENT_TYPE_SIGNAL &&
81596cdb538SJames Zhu 		event_data->signal_event_data.last_event_age) {
81696cdb538SJames Zhu 		waiter->event_age_enabled = true;
81796cdb538SJames Zhu 		if (ev->event_age != event_data->signal_event_data.last_event_age)
81896cdb538SJames Zhu 			waiter->activated = true;
81996cdb538SJames Zhu 	}
82096cdb538SJames Zhu 
821250e64a3SFelix Kuehling 	if (!waiter->activated)
822250e64a3SFelix Kuehling 		add_wait_queue(&ev->wq, &waiter->wait);
8235273e82cSFelix Kuehling 	spin_unlock(&ev->lock);
824f3a39818SAndrew Lewycky 
825f3a39818SAndrew Lewycky 	return 0;
826f3a39818SAndrew Lewycky }
827f3a39818SAndrew Lewycky 
828fe528c13SFelix Kuehling /* test_event_condition - Test condition of events being waited for
829fe528c13SFelix Kuehling  * @all:           Return completion only if all events have signaled
830fe528c13SFelix Kuehling  * @num_events:    Number of events to wait for
831fe528c13SFelix Kuehling  * @event_waiters: Array of event waiters, one per event
832fe528c13SFelix Kuehling  *
833fe528c13SFelix Kuehling  * Returns KFD_IOC_WAIT_RESULT_COMPLETE if all (or one) event(s) have
834fe528c13SFelix Kuehling  * signaled. Returns KFD_IOC_WAIT_RESULT_TIMEOUT if no (or not all)
835fe528c13SFelix Kuehling  * events have signaled. Returns KFD_IOC_WAIT_RESULT_FAIL if any of
836fe528c13SFelix Kuehling  * the events have been destroyed.
837fe528c13SFelix Kuehling  */
test_event_condition(bool all,uint32_t num_events,struct kfd_event_waiter * event_waiters)838fe528c13SFelix Kuehling static uint32_t test_event_condition(bool all, uint32_t num_events,
839f3a39818SAndrew Lewycky 				struct kfd_event_waiter *event_waiters)
840f3a39818SAndrew Lewycky {
841f3a39818SAndrew Lewycky 	uint32_t i;
842f3a39818SAndrew Lewycky 	uint32_t activated_count = 0;
843f3a39818SAndrew Lewycky 
844f3a39818SAndrew Lewycky 	for (i = 0; i < num_events; i++) {
8455273e82cSFelix Kuehling 		if (!READ_ONCE(event_waiters[i].event))
846fe528c13SFelix Kuehling 			return KFD_IOC_WAIT_RESULT_FAIL;
847fe528c13SFelix Kuehling 
8485273e82cSFelix Kuehling 		if (READ_ONCE(event_waiters[i].activated)) {
849f3a39818SAndrew Lewycky 			if (!all)
850fe528c13SFelix Kuehling 				return KFD_IOC_WAIT_RESULT_COMPLETE;
851f3a39818SAndrew Lewycky 
852f3a39818SAndrew Lewycky 			activated_count++;
853f3a39818SAndrew Lewycky 		}
854f3a39818SAndrew Lewycky 	}
855f3a39818SAndrew Lewycky 
856fe528c13SFelix Kuehling 	return activated_count == num_events ?
857fe528c13SFelix Kuehling 		KFD_IOC_WAIT_RESULT_COMPLETE : KFD_IOC_WAIT_RESULT_TIMEOUT;
858f3a39818SAndrew Lewycky }
859f3a39818SAndrew Lewycky 
86059d3e8beSAlexey Skidanov /*
86159d3e8beSAlexey Skidanov  * Copy event specific data, if defined.
86259d3e8beSAlexey Skidanov  * Currently only memory exception events have additional data to copy to user
86359d3e8beSAlexey Skidanov  */
copy_signaled_event_data(uint32_t num_events,struct kfd_event_waiter * event_waiters,struct kfd_event_data __user * data)864fdf0c833SFelix Kuehling static int copy_signaled_event_data(uint32_t num_events,
86559d3e8beSAlexey Skidanov 		struct kfd_event_waiter *event_waiters,
86659d3e8beSAlexey Skidanov 		struct kfd_event_data __user *data)
86759d3e8beSAlexey Skidanov {
868973fddeaSJames Zhu 	void *src;
869973fddeaSJames Zhu 	void __user *dst;
87059d3e8beSAlexey Skidanov 	struct kfd_event_waiter *waiter;
87159d3e8beSAlexey Skidanov 	struct kfd_event *event;
872973fddeaSJames Zhu 	uint32_t i, size = 0;
87359d3e8beSAlexey Skidanov 
87459d3e8beSAlexey Skidanov 	for (i = 0; i < num_events; i++) {
87559d3e8beSAlexey Skidanov 		waiter = &event_waiters[i];
87659d3e8beSAlexey Skidanov 		event = waiter->event;
8775273e82cSFelix Kuehling 		if (!event)
8785273e82cSFelix Kuehling 			return -EINVAL; /* event was destroyed */
879973fddeaSJames Zhu 		if (waiter->activated) {
880973fddeaSJames Zhu 			if (event->type == KFD_EVENT_TYPE_MEMORY) {
881ebf947feSFelix Kuehling 				dst = &data[i].memory_exception_data;
88259d3e8beSAlexey Skidanov 				src = &event->memory_exception_data;
883973fddeaSJames Zhu 				size = sizeof(struct kfd_hsa_memory_exception_data);
884a2d3c692SDavid Yat Sin 			} else if (event->type == KFD_EVENT_TYPE_HW_EXCEPTION) {
885a2d3c692SDavid Yat Sin 				dst = &data[i].memory_exception_data;
886a2d3c692SDavid Yat Sin 				src = &event->hw_exception_data;
887a2d3c692SDavid Yat Sin 				size = sizeof(struct kfd_hsa_hw_exception_data);
888973fddeaSJames Zhu 			} else if (event->type == KFD_EVENT_TYPE_SIGNAL &&
889973fddeaSJames Zhu 				waiter->event_age_enabled) {
890973fddeaSJames Zhu 				dst = &data[i].signal_event_data.last_event_age;
891973fddeaSJames Zhu 				src = &event->event_age;
892973fddeaSJames Zhu 				size = sizeof(u64);
893973fddeaSJames Zhu 			}
894973fddeaSJames Zhu 			if (size && copy_to_user(dst, src, size))
895fdf0c833SFelix Kuehling 				return -EFAULT;
89659d3e8beSAlexey Skidanov 		}
89759d3e8beSAlexey Skidanov 	}
89859d3e8beSAlexey Skidanov 
899fdf0c833SFelix Kuehling 	return 0;
90059d3e8beSAlexey Skidanov }
90159d3e8beSAlexey Skidanov 
user_timeout_to_jiffies(uint32_t user_timeout_ms)902f3a39818SAndrew Lewycky static long user_timeout_to_jiffies(uint32_t user_timeout_ms)
903f3a39818SAndrew Lewycky {
904f3a39818SAndrew Lewycky 	if (user_timeout_ms == KFD_EVENT_TIMEOUT_IMMEDIATE)
905f3a39818SAndrew Lewycky 		return 0;
906f3a39818SAndrew Lewycky 
907f3a39818SAndrew Lewycky 	if (user_timeout_ms == KFD_EVENT_TIMEOUT_INFINITE)
908f3a39818SAndrew Lewycky 		return MAX_SCHEDULE_TIMEOUT;
909f3a39818SAndrew Lewycky 
910f3a39818SAndrew Lewycky 	/*
911f3a39818SAndrew Lewycky 	 * msecs_to_jiffies interprets all values above 2^31-1 as infinite,
912f3a39818SAndrew Lewycky 	 * but we consider them finite.
913f3a39818SAndrew Lewycky 	 * This hack is wrong, but nobody is likely to notice.
914f3a39818SAndrew Lewycky 	 */
915f3a39818SAndrew Lewycky 	user_timeout_ms = min_t(uint32_t, user_timeout_ms, 0x7FFFFFFF);
916f3a39818SAndrew Lewycky 
917f3a39818SAndrew Lewycky 	return msecs_to_jiffies(user_timeout_ms) + 1;
918f3a39818SAndrew Lewycky }
919f3a39818SAndrew Lewycky 
free_waiters(uint32_t num_events,struct kfd_event_waiter * waiters,bool undo_auto_reset)920bea9a56aSFelix Kuehling static void free_waiters(uint32_t num_events, struct kfd_event_waiter *waiters,
921bea9a56aSFelix Kuehling 			 bool undo_auto_reset)
922f3a39818SAndrew Lewycky {
923f3a39818SAndrew Lewycky 	uint32_t i;
924f3a39818SAndrew Lewycky 
925f3a39818SAndrew Lewycky 	for (i = 0; i < num_events; i++)
9265273e82cSFelix Kuehling 		if (waiters[i].event) {
9275273e82cSFelix Kuehling 			spin_lock(&waiters[i].event->lock);
92874e40716SFelix Kuehling 			remove_wait_queue(&waiters[i].event->wq,
92974e40716SFelix Kuehling 					  &waiters[i].wait);
930bea9a56aSFelix Kuehling 			if (undo_auto_reset && waiters[i].activated &&
931bea9a56aSFelix Kuehling 			    waiters[i].event && waiters[i].event->auto_reset)
932bea9a56aSFelix Kuehling 				set_event(waiters[i].event);
9335273e82cSFelix Kuehling 			spin_unlock(&waiters[i].event->lock);
9345273e82cSFelix Kuehling 		}
935f3a39818SAndrew Lewycky 
936f3a39818SAndrew Lewycky 	kfree(waiters);
937f3a39818SAndrew Lewycky }
938f3a39818SAndrew Lewycky 
kfd_wait_on_events(struct kfd_process * p,uint32_t num_events,void __user * data,bool all,uint32_t * user_timeout_ms,uint32_t * wait_result)939f3a39818SAndrew Lewycky int kfd_wait_on_events(struct kfd_process *p,
94059d3e8beSAlexey Skidanov 		       uint32_t num_events, void __user *data,
941bea9a56aSFelix Kuehling 		       bool all, uint32_t *user_timeout_ms,
942fdf0c833SFelix Kuehling 		       uint32_t *wait_result)
943f3a39818SAndrew Lewycky {
94459d3e8beSAlexey Skidanov 	struct kfd_event_data __user *events =
94559d3e8beSAlexey Skidanov 			(struct kfd_event_data __user *) data;
946f3a39818SAndrew Lewycky 	uint32_t i;
947f3a39818SAndrew Lewycky 	int ret = 0;
9481f9d09beSSean Keely 
949f3a39818SAndrew Lewycky 	struct kfd_event_waiter *event_waiters = NULL;
950bea9a56aSFelix Kuehling 	long timeout = user_timeout_to_jiffies(*user_timeout_ms);
951f3a39818SAndrew Lewycky 
952fdf0c833SFelix Kuehling 	event_waiters = alloc_event_waiters(num_events);
953fdf0c833SFelix Kuehling 	if (!event_waiters) {
954fdf0c833SFelix Kuehling 		ret = -ENOMEM;
955fdf0c833SFelix Kuehling 		goto out;
956fdf0c833SFelix Kuehling 	}
957fdf0c833SFelix Kuehling 
9585273e82cSFelix Kuehling 	/* Use p->event_mutex here to protect against concurrent creation and
9595273e82cSFelix Kuehling 	 * destruction of events while we initialize event_waiters.
9605273e82cSFelix Kuehling 	 */
961f3a39818SAndrew Lewycky 	mutex_lock(&p->event_mutex);
962f3a39818SAndrew Lewycky 
963f3a39818SAndrew Lewycky 	for (i = 0; i < num_events; i++) {
96459d3e8beSAlexey Skidanov 		struct kfd_event_data event_data;
965f3a39818SAndrew Lewycky 
96659d3e8beSAlexey Skidanov 		if (copy_from_user(&event_data, &events[i],
9678bf79388SPan Bian 				sizeof(struct kfd_event_data))) {
9688bf79388SPan Bian 			ret = -EFAULT;
969fdf0c833SFelix Kuehling 			goto out_unlock;
9708bf79388SPan Bian 		}
971f3a39818SAndrew Lewycky 
97296cdb538SJames Zhu 		ret = init_event_waiter(p, &event_waiters[i], &event_data);
973f3a39818SAndrew Lewycky 		if (ret)
974fdf0c833SFelix Kuehling 			goto out_unlock;
975f3a39818SAndrew Lewycky 	}
976f3a39818SAndrew Lewycky 
9771f9d09beSSean Keely 	/* Check condition once. */
978fe528c13SFelix Kuehling 	*wait_result = test_event_condition(all, num_events, event_waiters);
979fe528c13SFelix Kuehling 	if (*wait_result == KFD_IOC_WAIT_RESULT_COMPLETE) {
980fdf0c833SFelix Kuehling 		ret = copy_signaled_event_data(num_events,
981fdf0c833SFelix Kuehling 					       event_waiters, events);
982fdf0c833SFelix Kuehling 		goto out_unlock;
983fe528c13SFelix Kuehling 	} else if (WARN_ON(*wait_result == KFD_IOC_WAIT_RESULT_FAIL)) {
984fe528c13SFelix Kuehling 		/* This should not happen. Events shouldn't be
985fe528c13SFelix Kuehling 		 * destroyed while we're holding the event_mutex
986fe528c13SFelix Kuehling 		 */
987fe528c13SFelix Kuehling 		goto out_unlock;
988fe528c13SFelix Kuehling 	}
989fe528c13SFelix Kuehling 
990f3a39818SAndrew Lewycky 	mutex_unlock(&p->event_mutex);
991f3a39818SAndrew Lewycky 
992f3a39818SAndrew Lewycky 	while (true) {
993f3a39818SAndrew Lewycky 		if (fatal_signal_pending(current)) {
994f3a39818SAndrew Lewycky 			ret = -EINTR;
995f3a39818SAndrew Lewycky 			break;
996f3a39818SAndrew Lewycky 		}
997f3a39818SAndrew Lewycky 
998f3a39818SAndrew Lewycky 		if (signal_pending(current)) {
999f3a39818SAndrew Lewycky 			ret = -ERESTARTSYS;
1000bea9a56aSFelix Kuehling 			if (*user_timeout_ms != KFD_EVENT_TIMEOUT_IMMEDIATE &&
1001bea9a56aSFelix Kuehling 			    *user_timeout_ms != KFD_EVENT_TIMEOUT_INFINITE)
1002bea9a56aSFelix Kuehling 				*user_timeout_ms = jiffies_to_msecs(
1003bea9a56aSFelix Kuehling 					max(0l, timeout-1));
1004f3a39818SAndrew Lewycky 			break;
1005f3a39818SAndrew Lewycky 		}
1006f3a39818SAndrew Lewycky 
1007d9aeec4cSSean Keely 		/* Set task state to interruptible sleep before
1008d9aeec4cSSean Keely 		 * checking wake-up conditions. A concurrent wake-up
1009d9aeec4cSSean Keely 		 * will put the task back into runnable state. In that
1010d9aeec4cSSean Keely 		 * case schedule_timeout will not put the task to
1011d9aeec4cSSean Keely 		 * sleep and we'll get a chance to re-check the
1012d9aeec4cSSean Keely 		 * updated conditions almost immediately. Otherwise,
1013d9aeec4cSSean Keely 		 * this race condition would lead to a soft hang or a
1014d9aeec4cSSean Keely 		 * very long sleep.
1015d9aeec4cSSean Keely 		 */
1016d9aeec4cSSean Keely 		set_current_state(TASK_INTERRUPTIBLE);
1017d9aeec4cSSean Keely 
1018fe528c13SFelix Kuehling 		*wait_result = test_event_condition(all, num_events,
1019fe528c13SFelix Kuehling 						    event_waiters);
1020fe528c13SFelix Kuehling 		if (*wait_result != KFD_IOC_WAIT_RESULT_TIMEOUT)
1021f3a39818SAndrew Lewycky 			break;
1022f3a39818SAndrew Lewycky 
1023fe528c13SFelix Kuehling 		if (timeout <= 0)
1024f3a39818SAndrew Lewycky 			break;
1025f3a39818SAndrew Lewycky 
1026d9aeec4cSSean Keely 		timeout = schedule_timeout(timeout);
1027f3a39818SAndrew Lewycky 	}
1028f3a39818SAndrew Lewycky 	__set_current_state(TASK_RUNNING);
1029f3a39818SAndrew Lewycky 
10305273e82cSFelix Kuehling 	mutex_lock(&p->event_mutex);
1031fdf0c833SFelix Kuehling 	/* copy_signaled_event_data may sleep. So this has to happen
1032fdf0c833SFelix Kuehling 	 * after the task state is set back to RUNNING.
10335273e82cSFelix Kuehling 	 *
10345273e82cSFelix Kuehling 	 * The event may also have been destroyed after signaling. So
10355273e82cSFelix Kuehling 	 * copy_signaled_event_data also must confirm that the event
10365273e82cSFelix Kuehling 	 * still exists. Therefore this must be under the p->event_mutex
10375273e82cSFelix Kuehling 	 * which is also held when events are destroyed.
1038fdf0c833SFelix Kuehling 	 */
1039fdf0c833SFelix Kuehling 	if (!ret && *wait_result == KFD_IOC_WAIT_RESULT_COMPLETE)
1040fdf0c833SFelix Kuehling 		ret = copy_signaled_event_data(num_events,
1041fdf0c833SFelix Kuehling 					       event_waiters, events);
1042fdf0c833SFelix Kuehling 
1043fdf0c833SFelix Kuehling out_unlock:
1044bea9a56aSFelix Kuehling 	free_waiters(num_events, event_waiters, ret == -ERESTARTSYS);
1045f3a39818SAndrew Lewycky 	mutex_unlock(&p->event_mutex);
1046fdf0c833SFelix Kuehling out:
1047fdf0c833SFelix Kuehling 	if (ret)
1048fdf0c833SFelix Kuehling 		*wait_result = KFD_IOC_WAIT_RESULT_FAIL;
1049fe528c13SFelix Kuehling 	else if (*wait_result == KFD_IOC_WAIT_RESULT_FAIL)
1050fe528c13SFelix Kuehling 		ret = -EIO;
1051f3a39818SAndrew Lewycky 
1052f3a39818SAndrew Lewycky 	return ret;
1053f3a39818SAndrew Lewycky }
1054f3a39818SAndrew Lewycky 
kfd_event_mmap(struct kfd_process * p,struct vm_area_struct * vma)1055f3a39818SAndrew Lewycky int kfd_event_mmap(struct kfd_process *p, struct vm_area_struct *vma)
1056f3a39818SAndrew Lewycky {
1057f3a39818SAndrew Lewycky 	unsigned long pfn;
105850cb7dd9SFelix Kuehling 	struct kfd_signal_page *page;
1059b9a5d0a5SFelix Kuehling 	int ret;
1060f3a39818SAndrew Lewycky 
1061b9a5d0a5SFelix Kuehling 	/* check required size doesn't exceed the allocated size */
1062b9a5d0a5SFelix Kuehling 	if (get_order(KFD_SIGNAL_EVENT_LIMIT * 8) <
1063f3a39818SAndrew Lewycky 			get_order(vma->vm_end - vma->vm_start)) {
106479775b62SKent Russell 		pr_err("Event page mmap requested illegal size\n");
1065f3a39818SAndrew Lewycky 		return -EINVAL;
1066f3a39818SAndrew Lewycky 	}
1067f3a39818SAndrew Lewycky 
106850cb7dd9SFelix Kuehling 	page = p->signal_page;
1069f3a39818SAndrew Lewycky 	if (!page) {
1070f3a39818SAndrew Lewycky 		/* Probably KFD bug, but mmap is user-accessible. */
107150cb7dd9SFelix Kuehling 		pr_debug("Signal page could not be found\n");
1072f3a39818SAndrew Lewycky 		return -EINVAL;
1073f3a39818SAndrew Lewycky 	}
1074f3a39818SAndrew Lewycky 
1075f3a39818SAndrew Lewycky 	pfn = __pa(page->kernel_address);
1076f3a39818SAndrew Lewycky 	pfn >>= PAGE_SHIFT;
1077f3a39818SAndrew Lewycky 
10781c71222eSSuren Baghdasaryan 	vm_flags_set(vma, VM_IO | VM_DONTCOPY | VM_DONTEXPAND | VM_NORESERVE
10791c71222eSSuren Baghdasaryan 		       | VM_DONTDUMP | VM_PFNMAP);
1080f3a39818SAndrew Lewycky 
108179775b62SKent Russell 	pr_debug("Mapping signal page\n");
1082f3a39818SAndrew Lewycky 	pr_debug("     start user address  == 0x%08lx\n", vma->vm_start);
1083f3a39818SAndrew Lewycky 	pr_debug("     end user address    == 0x%08lx\n", vma->vm_end);
1084f3a39818SAndrew Lewycky 	pr_debug("     pfn                 == 0x%016lX\n", pfn);
1085f3a39818SAndrew Lewycky 	pr_debug("     vm_flags            == 0x%08lX\n", vma->vm_flags);
1086f3a39818SAndrew Lewycky 	pr_debug("     size                == 0x%08lX\n",
1087f3a39818SAndrew Lewycky 			vma->vm_end - vma->vm_start);
1088f3a39818SAndrew Lewycky 
1089f3a39818SAndrew Lewycky 	page->user_address = (uint64_t __user *)vma->vm_start;
1090f3a39818SAndrew Lewycky 
1091f3a39818SAndrew Lewycky 	/* mapping the page to user process */
1092b9a5d0a5SFelix Kuehling 	ret = remap_pfn_range(vma, vma->vm_start, pfn,
1093f3a39818SAndrew Lewycky 			vma->vm_end - vma->vm_start, vma->vm_page_prot);
1094b9a5d0a5SFelix Kuehling 	if (!ret)
1095b9a5d0a5SFelix Kuehling 		p->signal_mapped_size = vma->vm_end - vma->vm_start;
1096b9a5d0a5SFelix Kuehling 
1097b9a5d0a5SFelix Kuehling 	return ret;
1098f3a39818SAndrew Lewycky }
109959d3e8beSAlexey Skidanov 
110059d3e8beSAlexey Skidanov /*
11015273e82cSFelix Kuehling  * Assumes that p is not going away.
110259d3e8beSAlexey Skidanov  */
lookup_events_by_type_and_signal(struct kfd_process * p,int type,void * event_data)110359d3e8beSAlexey Skidanov static void lookup_events_by_type_and_signal(struct kfd_process *p,
110459d3e8beSAlexey Skidanov 		int type, void *event_data)
110559d3e8beSAlexey Skidanov {
110659d3e8beSAlexey Skidanov 	struct kfd_hsa_memory_exception_data *ev_data;
110759d3e8beSAlexey Skidanov 	struct kfd_event *ev;
1108482f0777SFelix Kuehling 	uint32_t id;
110959d3e8beSAlexey Skidanov 	bool send_signal = true;
111059d3e8beSAlexey Skidanov 
111159d3e8beSAlexey Skidanov 	ev_data = (struct kfd_hsa_memory_exception_data *) event_data;
111259d3e8beSAlexey Skidanov 
11135273e82cSFelix Kuehling 	rcu_read_lock();
11145273e82cSFelix Kuehling 
1115482f0777SFelix Kuehling 	id = KFD_FIRST_NONSIGNAL_EVENT_ID;
1116482f0777SFelix Kuehling 	idr_for_each_entry_continue(&p->event_idr, ev, id)
111759d3e8beSAlexey Skidanov 		if (ev->type == type) {
111859d3e8beSAlexey Skidanov 			send_signal = false;
111959d3e8beSAlexey Skidanov 			dev_dbg(kfd_device,
112059d3e8beSAlexey Skidanov 					"Event found: id %X type %d",
112159d3e8beSAlexey Skidanov 					ev->event_id, ev->type);
11225273e82cSFelix Kuehling 			spin_lock(&ev->lock);
112359d3e8beSAlexey Skidanov 			set_event(ev);
112459d3e8beSAlexey Skidanov 			if (ev->type == KFD_EVENT_TYPE_MEMORY && ev_data)
112559d3e8beSAlexey Skidanov 				ev->memory_exception_data = *ev_data;
11265273e82cSFelix Kuehling 			spin_unlock(&ev->lock);
112759d3e8beSAlexey Skidanov 		}
112859d3e8beSAlexey Skidanov 
1129101fee63SMoses Reuben 	if (type == KFD_EVENT_TYPE_MEMORY) {
1130101fee63SMoses Reuben 		dev_warn(kfd_device,
11316027b1bfSYong Zhao 			"Sending SIGSEGV to process %d (pasid 0x%x)",
11326027b1bfSYong Zhao 				p->lead_thread->pid, p->pasid);
1133101fee63SMoses Reuben 		send_sig(SIGSEGV, p->lead_thread, 0);
1134101fee63SMoses Reuben 	}
1135101fee63SMoses Reuben 
113659d3e8beSAlexey Skidanov 	/* Send SIGTERM no event of type "type" has been found*/
113759d3e8beSAlexey Skidanov 	if (send_signal) {
113881663016SOded Gabbay 		if (send_sigterm) {
113959d3e8beSAlexey Skidanov 			dev_warn(kfd_device,
11406027b1bfSYong Zhao 				"Sending SIGTERM to process %d (pasid 0x%x)",
11416027b1bfSYong Zhao 					p->lead_thread->pid, p->pasid);
114259d3e8beSAlexey Skidanov 			send_sig(SIGTERM, p->lead_thread, 0);
114381663016SOded Gabbay 		} else {
114481663016SOded Gabbay 			dev_err(kfd_device,
11456027b1bfSYong Zhao 				"Process %d (pasid 0x%x) got unhandled exception",
11466027b1bfSYong Zhao 				p->lead_thread->pid, p->pasid);
114781663016SOded Gabbay 		}
114859d3e8beSAlexey Skidanov 	}
11495273e82cSFelix Kuehling 
11505273e82cSFelix Kuehling 	rcu_read_unlock();
115159d3e8beSAlexey Skidanov }
115259d3e8beSAlexey Skidanov 
kfd_signal_hw_exception_event(u32 pasid)1153c7b6bac9SFenghua Yu void kfd_signal_hw_exception_event(u32 pasid)
1154930c5ff4SAlexey Skidanov {
1155930c5ff4SAlexey Skidanov 	/*
1156930c5ff4SAlexey Skidanov 	 * Because we are called from arbitrary context (workqueue) as opposed
1157930c5ff4SAlexey Skidanov 	 * to process context, kfd_process could attempt to exit while we are
1158abb208a8SFelix Kuehling 	 * running so the lookup function increments the process ref count.
1159930c5ff4SAlexey Skidanov 	 */
1160930c5ff4SAlexey Skidanov 	struct kfd_process *p = kfd_lookup_process_by_pasid(pasid);
1161930c5ff4SAlexey Skidanov 
1162930c5ff4SAlexey Skidanov 	if (!p)
1163930c5ff4SAlexey Skidanov 		return; /* Presumably process exited. */
1164930c5ff4SAlexey Skidanov 
1165930c5ff4SAlexey Skidanov 	lookup_events_by_type_and_signal(p, KFD_EVENT_TYPE_HW_EXCEPTION, NULL);
1166abb208a8SFelix Kuehling 	kfd_unref_process(p);
1167930c5ff4SAlexey Skidanov }
11682640c3faSshaoyunl 
kfd_signal_vm_fault_event(struct kfd_node * dev,u32 pasid,struct kfd_vm_fault_info * info,struct kfd_hsa_memory_exception_data * data)11698dc1db31SMukul Joshi void kfd_signal_vm_fault_event(struct kfd_node *dev, u32 pasid,
1170c2d2588cSJonathan Kim 				struct kfd_vm_fault_info *info,
1171c2d2588cSJonathan Kim 				struct kfd_hsa_memory_exception_data *data)
11722640c3faSshaoyunl {
11732640c3faSshaoyunl 	struct kfd_event *ev;
11742640c3faSshaoyunl 	uint32_t id;
11752640c3faSshaoyunl 	struct kfd_process *p = kfd_lookup_process_by_pasid(pasid);
11762640c3faSshaoyunl 	struct kfd_hsa_memory_exception_data memory_exception_data;
1177bef153b7SDavid Yat Sin 	int user_gpu_id;
11782640c3faSshaoyunl 
11792640c3faSshaoyunl 	if (!p)
11802640c3faSshaoyunl 		return; /* Presumably process exited. */
1181bef153b7SDavid Yat Sin 
1182bef153b7SDavid Yat Sin 	user_gpu_id = kfd_process_get_user_gpu_id(p, dev->id);
1183bef153b7SDavid Yat Sin 	if (unlikely(user_gpu_id == -EINVAL)) {
1184bef153b7SDavid Yat Sin 		WARN_ONCE(1, "Could not get user_gpu_id from dev->id:%x\n", dev->id);
1185bef153b7SDavid Yat Sin 		return;
1186bef153b7SDavid Yat Sin 	}
1187bef153b7SDavid Yat Sin 
11887386f88aSJonathan Kim 	/* SoC15 chips and onwards will pass in data from now on. */
11897386f88aSJonathan Kim 	if (!data) {
11902640c3faSshaoyunl 		memset(&memory_exception_data, 0, sizeof(memory_exception_data));
1191bef153b7SDavid Yat Sin 		memory_exception_data.gpu_id = user_gpu_id;
11920d87c9cfSKent Russell 		memory_exception_data.failure.imprecise = true;
11937386f88aSJonathan Kim 
11942640c3faSshaoyunl 		/* Set failure reason */
11952640c3faSshaoyunl 		if (info) {
11967386f88aSJonathan Kim 			memory_exception_data.va = (info->page_addr) <<
11977386f88aSJonathan Kim 								PAGE_SHIFT;
11982640c3faSshaoyunl 			memory_exception_data.failure.NotPresent =
11992640c3faSshaoyunl 				info->prot_valid ? 1 : 0;
12002640c3faSshaoyunl 			memory_exception_data.failure.NoExecute =
12012640c3faSshaoyunl 				info->prot_exec ? 1 : 0;
12022640c3faSshaoyunl 			memory_exception_data.failure.ReadOnly =
12032640c3faSshaoyunl 				info->prot_write ? 1 : 0;
12042640c3faSshaoyunl 			memory_exception_data.failure.imprecise = 0;
12052640c3faSshaoyunl 		}
12067386f88aSJonathan Kim 	}
12075273e82cSFelix Kuehling 
12085273e82cSFelix Kuehling 	rcu_read_lock();
12092640c3faSshaoyunl 
12102640c3faSshaoyunl 	id = KFD_FIRST_NONSIGNAL_EVENT_ID;
12112640c3faSshaoyunl 	idr_for_each_entry_continue(&p->event_idr, ev, id)
12122640c3faSshaoyunl 		if (ev->type == KFD_EVENT_TYPE_MEMORY) {
12135273e82cSFelix Kuehling 			spin_lock(&ev->lock);
12147386f88aSJonathan Kim 			ev->memory_exception_data = data ? *data :
12157386f88aSJonathan Kim 							memory_exception_data;
12162640c3faSshaoyunl 			set_event(ev);
12175273e82cSFelix Kuehling 			spin_unlock(&ev->lock);
12182640c3faSshaoyunl 		}
12192640c3faSshaoyunl 
12205273e82cSFelix Kuehling 	rcu_read_unlock();
12212640c3faSshaoyunl 	kfd_unref_process(p);
12222640c3faSshaoyunl }
1223e42051d2SShaoyun Liu 
kfd_signal_reset_event(struct kfd_node * dev)12248dc1db31SMukul Joshi void kfd_signal_reset_event(struct kfd_node *dev)
1225e42051d2SShaoyun Liu {
1226e42051d2SShaoyun Liu 	struct kfd_hsa_hw_exception_data hw_exception_data;
12279b54d201SEric Huang 	struct kfd_hsa_memory_exception_data memory_exception_data;
1228e42051d2SShaoyun Liu 	struct kfd_process *p;
1229e42051d2SShaoyun Liu 	struct kfd_event *ev;
1230e42051d2SShaoyun Liu 	unsigned int temp;
1231e42051d2SShaoyun Liu 	uint32_t id, idx;
12329b54d201SEric Huang 	int reset_cause = atomic_read(&dev->sram_ecc_flag) ?
12339b54d201SEric Huang 			KFD_HW_EXCEPTION_ECC :
12349b54d201SEric Huang 			KFD_HW_EXCEPTION_GPU_HANG;
1235e42051d2SShaoyun Liu 
1236e42051d2SShaoyun Liu 	/* Whole gpu reset caused by GPU hang and memory is lost */
1237e42051d2SShaoyun Liu 	memset(&hw_exception_data, 0, sizeof(hw_exception_data));
1238e42051d2SShaoyun Liu 	hw_exception_data.memory_lost = 1;
12399b54d201SEric Huang 	hw_exception_data.reset_cause = reset_cause;
12409b54d201SEric Huang 
12419b54d201SEric Huang 	memset(&memory_exception_data, 0, sizeof(memory_exception_data));
12429b54d201SEric Huang 	memory_exception_data.ErrorType = KFD_MEM_ERR_SRAM_ECC;
12439b54d201SEric Huang 	memory_exception_data.failure.imprecise = true;
1244e42051d2SShaoyun Liu 
1245e42051d2SShaoyun Liu 	idx = srcu_read_lock(&kfd_processes_srcu);
1246e42051d2SShaoyun Liu 	hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) {
1247bef153b7SDavid Yat Sin 		int user_gpu_id = kfd_process_get_user_gpu_id(p, dev->id);
1248*ee0a469cSJonathan Kim 		struct kfd_process_device *pdd = kfd_get_process_device_data(dev, p);
1249bef153b7SDavid Yat Sin 
1250bef153b7SDavid Yat Sin 		if (unlikely(user_gpu_id == -EINVAL)) {
1251bef153b7SDavid Yat Sin 			WARN_ONCE(1, "Could not get user_gpu_id from dev->id:%x\n", dev->id);
1252bef153b7SDavid Yat Sin 			continue;
1253bef153b7SDavid Yat Sin 		}
1254bef153b7SDavid Yat Sin 
1255*ee0a469cSJonathan Kim 		if (unlikely(!pdd)) {
1256*ee0a469cSJonathan Kim 			WARN_ONCE(1, "Could not get device data from pasid:0x%x\n", p->pasid);
1257*ee0a469cSJonathan Kim 			continue;
1258*ee0a469cSJonathan Kim 		}
1259*ee0a469cSJonathan Kim 
1260*ee0a469cSJonathan Kim 		if (dev->dqm->detect_hang_count && !pdd->has_reset_queue)
1261*ee0a469cSJonathan Kim 			continue;
1262*ee0a469cSJonathan Kim 
1263*ee0a469cSJonathan Kim 		if (dev->dqm->detect_hang_count) {
1264*ee0a469cSJonathan Kim 			struct amdgpu_task_info *ti;
1265*ee0a469cSJonathan Kim 
1266*ee0a469cSJonathan Kim 			ti = amdgpu_vm_get_task_info_pasid(dev->adev, p->pasid);
1267*ee0a469cSJonathan Kim 			if (ti) {
1268*ee0a469cSJonathan Kim 				dev_err(dev->adev->dev,
1269*ee0a469cSJonathan Kim 					"Queues reset on process %s tid %d thread %s pid %d\n",
1270*ee0a469cSJonathan Kim 					ti->process_name, ti->tgid, ti->task_name, ti->pid);
1271*ee0a469cSJonathan Kim 				amdgpu_vm_put_task_info(ti);
1272*ee0a469cSJonathan Kim 			}
1273*ee0a469cSJonathan Kim 		}
1274*ee0a469cSJonathan Kim 
12755273e82cSFelix Kuehling 		rcu_read_lock();
12765273e82cSFelix Kuehling 
1277e42051d2SShaoyun Liu 		id = KFD_FIRST_NONSIGNAL_EVENT_ID;
12789b54d201SEric Huang 		idr_for_each_entry_continue(&p->event_idr, ev, id) {
1279e42051d2SShaoyun Liu 			if (ev->type == KFD_EVENT_TYPE_HW_EXCEPTION) {
12805273e82cSFelix Kuehling 				spin_lock(&ev->lock);
1281e42051d2SShaoyun Liu 				ev->hw_exception_data = hw_exception_data;
1282bef153b7SDavid Yat Sin 				ev->hw_exception_data.gpu_id = user_gpu_id;
1283e42051d2SShaoyun Liu 				set_event(ev);
12845273e82cSFelix Kuehling 				spin_unlock(&ev->lock);
1285e42051d2SShaoyun Liu 			}
12869b54d201SEric Huang 			if (ev->type == KFD_EVENT_TYPE_MEMORY &&
12879b54d201SEric Huang 			    reset_cause == KFD_HW_EXCEPTION_ECC) {
12885273e82cSFelix Kuehling 				spin_lock(&ev->lock);
12899b54d201SEric Huang 				ev->memory_exception_data = memory_exception_data;
1290bef153b7SDavid Yat Sin 				ev->memory_exception_data.gpu_id = user_gpu_id;
12919b54d201SEric Huang 				set_event(ev);
12925273e82cSFelix Kuehling 				spin_unlock(&ev->lock);
12939b54d201SEric Huang 			}
12949b54d201SEric Huang 		}
12955273e82cSFelix Kuehling 
12965273e82cSFelix Kuehling 		rcu_read_unlock();
1297e42051d2SShaoyun Liu 	}
1298e42051d2SShaoyun Liu 	srcu_read_unlock(&kfd_processes_srcu, idx);
1299e42051d2SShaoyun Liu }
1300e2b1f9f5SDennis Li 
kfd_signal_poison_consumed_event(struct kfd_node * dev,u32 pasid)13018dc1db31SMukul Joshi void kfd_signal_poison_consumed_event(struct kfd_node *dev, u32 pasid)
1302e2b1f9f5SDennis Li {
1303e2b1f9f5SDennis Li 	struct kfd_process *p = kfd_lookup_process_by_pasid(pasid);
1304e2b1f9f5SDennis Li 	struct kfd_hsa_memory_exception_data memory_exception_data;
1305e2b1f9f5SDennis Li 	struct kfd_hsa_hw_exception_data hw_exception_data;
1306e2b1f9f5SDennis Li 	struct kfd_event *ev;
1307e2b1f9f5SDennis Li 	uint32_t id = KFD_FIRST_NONSIGNAL_EVENT_ID;
1308bef153b7SDavid Yat Sin 	int user_gpu_id;
1309e2b1f9f5SDennis Li 
131022f6e3e1SYiPeng Chai 	if (!p) {
131122f6e3e1SYiPeng Chai 		dev_warn(dev->adev->dev, "Not find process with pasid:%d\n", pasid);
1312e2b1f9f5SDennis Li 		return; /* Presumably process exited. */
131322f6e3e1SYiPeng Chai 	}
1314e2b1f9f5SDennis Li 
1315bef153b7SDavid Yat Sin 	user_gpu_id = kfd_process_get_user_gpu_id(p, dev->id);
1316bef153b7SDavid Yat Sin 	if (unlikely(user_gpu_id == -EINVAL)) {
1317bef153b7SDavid Yat Sin 		WARN_ONCE(1, "Could not get user_gpu_id from dev->id:%x\n", dev->id);
1318bef153b7SDavid Yat Sin 		return;
1319bef153b7SDavid Yat Sin 	}
1320bef153b7SDavid Yat Sin 
1321e2b1f9f5SDennis Li 	memset(&hw_exception_data, 0, sizeof(hw_exception_data));
1322bef153b7SDavid Yat Sin 	hw_exception_data.gpu_id = user_gpu_id;
1323e2b1f9f5SDennis Li 	hw_exception_data.memory_lost = 1;
1324e2b1f9f5SDennis Li 	hw_exception_data.reset_cause = KFD_HW_EXCEPTION_ECC;
1325e2b1f9f5SDennis Li 
1326e2b1f9f5SDennis Li 	memset(&memory_exception_data, 0, sizeof(memory_exception_data));
1327e2b1f9f5SDennis Li 	memory_exception_data.ErrorType = KFD_MEM_ERR_POISON_CONSUMED;
1328bef153b7SDavid Yat Sin 	memory_exception_data.gpu_id = user_gpu_id;
1329e2b1f9f5SDennis Li 	memory_exception_data.failure.imprecise = true;
1330e2b1f9f5SDennis Li 
13315273e82cSFelix Kuehling 	rcu_read_lock();
13325273e82cSFelix Kuehling 
1333e2b1f9f5SDennis Li 	idr_for_each_entry_continue(&p->event_idr, ev, id) {
1334e2b1f9f5SDennis Li 		if (ev->type == KFD_EVENT_TYPE_HW_EXCEPTION) {
13355273e82cSFelix Kuehling 			spin_lock(&ev->lock);
1336e2b1f9f5SDennis Li 			ev->hw_exception_data = hw_exception_data;
1337e2b1f9f5SDennis Li 			set_event(ev);
13385273e82cSFelix Kuehling 			spin_unlock(&ev->lock);
1339e2b1f9f5SDennis Li 		}
1340e2b1f9f5SDennis Li 
1341e2b1f9f5SDennis Li 		if (ev->type == KFD_EVENT_TYPE_MEMORY) {
13425273e82cSFelix Kuehling 			spin_lock(&ev->lock);
1343e2b1f9f5SDennis Li 			ev->memory_exception_data = memory_exception_data;
1344e2b1f9f5SDennis Li 			set_event(ev);
13455273e82cSFelix Kuehling 			spin_unlock(&ev->lock);
1346e2b1f9f5SDennis Li 		}
1347e2b1f9f5SDennis Li 	}
13485273e82cSFelix Kuehling 
134922f6e3e1SYiPeng Chai 	dev_warn(dev->adev->dev, "Send SIGBUS to process %s(pasid:%d)\n",
135022f6e3e1SYiPeng Chai 		p->lead_thread->comm, pasid);
13515273e82cSFelix Kuehling 	rcu_read_unlock();
1352e2b1f9f5SDennis Li 
1353e2b1f9f5SDennis Li 	/* user application will handle SIGBUS signal */
1354e2b1f9f5SDennis Li 	send_sig(SIGBUS, p->lead_thread, 0);
135596b62c8aSDennis Li 
135696b62c8aSDennis Li 	kfd_unref_process(p);
1357e2b1f9f5SDennis Li }
1358