18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Copyright 2014 Advanced Micro Devices, Inc.
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
58c2ecf20Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
68c2ecf20Sopenharmony_ci * to deal in the Software without restriction, including without limitation
78c2ecf20Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
88c2ecf20Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
98c2ecf20Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
108c2ecf20Sopenharmony_ci *
118c2ecf20Sopenharmony_ci * The above copyright notice and this permission notice shall be included in
128c2ecf20Sopenharmony_ci * all copies or substantial portions of the Software.
138c2ecf20Sopenharmony_ci *
148c2ecf20Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
158c2ecf20Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
168c2ecf20Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
178c2ecf20Sopenharmony_ci * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
188c2ecf20Sopenharmony_ci * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
198c2ecf20Sopenharmony_ci * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
208c2ecf20Sopenharmony_ci * OTHER DEALINGS IN THE SOFTWARE.
218c2ecf20Sopenharmony_ci */
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci#include <linux/mm_types.h>
248c2ecf20Sopenharmony_ci#include <linux/slab.h>
258c2ecf20Sopenharmony_ci#include <linux/types.h>
268c2ecf20Sopenharmony_ci#include <linux/sched/signal.h>
278c2ecf20Sopenharmony_ci#include <linux/sched/mm.h>
288c2ecf20Sopenharmony_ci#include <linux/uaccess.h>
298c2ecf20Sopenharmony_ci#include <linux/mman.h>
308c2ecf20Sopenharmony_ci#include <linux/memory.h>
318c2ecf20Sopenharmony_ci#include "kfd_priv.h"
328c2ecf20Sopenharmony_ci#include "kfd_events.h"
338c2ecf20Sopenharmony_ci#include "kfd_iommu.h"
348c2ecf20Sopenharmony_ci#include <linux/device.h>
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci/*
378c2ecf20Sopenharmony_ci * Wrapper around wait_queue_entry_t
388c2ecf20Sopenharmony_ci */
398c2ecf20Sopenharmony_cistruct kfd_event_waiter {
408c2ecf20Sopenharmony_ci	wait_queue_entry_t wait;
418c2ecf20Sopenharmony_ci	struct kfd_event *event; /* Event to wait for */
428c2ecf20Sopenharmony_ci	bool activated;		 /* Becomes true when event is signaled */
438c2ecf20Sopenharmony_ci};
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci/*
468c2ecf20Sopenharmony_ci * Each signal event needs a 64-bit signal slot where the signaler will write
478c2ecf20Sopenharmony_ci * a 1 before sending an interrupt. (This is needed because some interrupts
488c2ecf20Sopenharmony_ci * do not contain enough spare data bits to identify an event.)
498c2ecf20Sopenharmony_ci * We get whole pages and map them to the process VA.
508c2ecf20Sopenharmony_ci * Individual signal events use their event_id as slot index.
518c2ecf20Sopenharmony_ci */
528c2ecf20Sopenharmony_cistruct kfd_signal_page {
538c2ecf20Sopenharmony_ci	uint64_t *kernel_address;
548c2ecf20Sopenharmony_ci	uint64_t __user *user_address;
558c2ecf20Sopenharmony_ci	bool need_to_free_pages;
568c2ecf20Sopenharmony_ci};
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_cistatic uint64_t *page_slots(struct kfd_signal_page *page)
608c2ecf20Sopenharmony_ci{
618c2ecf20Sopenharmony_ci	return page->kernel_address;
628c2ecf20Sopenharmony_ci}
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_cistatic struct kfd_signal_page *allocate_signal_page(struct kfd_process *p)
658c2ecf20Sopenharmony_ci{
668c2ecf20Sopenharmony_ci	void *backing_store;
678c2ecf20Sopenharmony_ci	struct kfd_signal_page *page;
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci	page = kzalloc(sizeof(*page), GFP_KERNEL);
708c2ecf20Sopenharmony_ci	if (!page)
718c2ecf20Sopenharmony_ci		return NULL;
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci	backing_store = (void *) __get_free_pages(GFP_KERNEL,
748c2ecf20Sopenharmony_ci					get_order(KFD_SIGNAL_EVENT_LIMIT * 8));
758c2ecf20Sopenharmony_ci	if (!backing_store)
768c2ecf20Sopenharmony_ci		goto fail_alloc_signal_store;
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci	/* Initialize all events to unsignaled */
798c2ecf20Sopenharmony_ci	memset(backing_store, (uint8_t) UNSIGNALED_EVENT_SLOT,
808c2ecf20Sopenharmony_ci	       KFD_SIGNAL_EVENT_LIMIT * 8);
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci	page->kernel_address = backing_store;
838c2ecf20Sopenharmony_ci	page->need_to_free_pages = true;
848c2ecf20Sopenharmony_ci	pr_debug("Allocated new event signal page at %p, for process %p\n",
858c2ecf20Sopenharmony_ci			page, p);
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci	return page;
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_cifail_alloc_signal_store:
908c2ecf20Sopenharmony_ci	kfree(page);
918c2ecf20Sopenharmony_ci	return NULL;
928c2ecf20Sopenharmony_ci}
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_cistatic int allocate_event_notification_slot(struct kfd_process *p,
958c2ecf20Sopenharmony_ci					    struct kfd_event *ev)
968c2ecf20Sopenharmony_ci{
978c2ecf20Sopenharmony_ci	int id;
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci	if (!p->signal_page) {
1008c2ecf20Sopenharmony_ci		p->signal_page = allocate_signal_page(p);
1018c2ecf20Sopenharmony_ci		if (!p->signal_page)
1028c2ecf20Sopenharmony_ci			return -ENOMEM;
1038c2ecf20Sopenharmony_ci		/* Oldest user mode expects 256 event slots */
1048c2ecf20Sopenharmony_ci		p->signal_mapped_size = 256*8;
1058c2ecf20Sopenharmony_ci	}
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci	/*
1088c2ecf20Sopenharmony_ci	 * Compatibility with old user mode: Only use signal slots
1098c2ecf20Sopenharmony_ci	 * user mode has mapped, may be less than
1108c2ecf20Sopenharmony_ci	 * KFD_SIGNAL_EVENT_LIMIT. This also allows future increase
1118c2ecf20Sopenharmony_ci	 * of the event limit without breaking user mode.
1128c2ecf20Sopenharmony_ci	 */
1138c2ecf20Sopenharmony_ci	id = idr_alloc(&p->event_idr, ev, 0, p->signal_mapped_size / 8,
1148c2ecf20Sopenharmony_ci		       GFP_KERNEL);
1158c2ecf20Sopenharmony_ci	if (id < 0)
1168c2ecf20Sopenharmony_ci		return id;
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci	ev->event_id = id;
1198c2ecf20Sopenharmony_ci	page_slots(p->signal_page)[id] = UNSIGNALED_EVENT_SLOT;
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci	return 0;
1228c2ecf20Sopenharmony_ci}
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci/*
1258c2ecf20Sopenharmony_ci * Assumes that p->event_mutex is held and of course that p is not going
1268c2ecf20Sopenharmony_ci * away (current or locked).
1278c2ecf20Sopenharmony_ci */
1288c2ecf20Sopenharmony_cistatic struct kfd_event *lookup_event_by_id(struct kfd_process *p, uint32_t id)
1298c2ecf20Sopenharmony_ci{
1308c2ecf20Sopenharmony_ci	return idr_find(&p->event_idr, id);
1318c2ecf20Sopenharmony_ci}
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci/**
1348c2ecf20Sopenharmony_ci * lookup_signaled_event_by_partial_id - Lookup signaled event from partial ID
1358c2ecf20Sopenharmony_ci * @p:     Pointer to struct kfd_process
1368c2ecf20Sopenharmony_ci * @id:    ID to look up
1378c2ecf20Sopenharmony_ci * @bits:  Number of valid bits in @id
1388c2ecf20Sopenharmony_ci *
1398c2ecf20Sopenharmony_ci * Finds the first signaled event with a matching partial ID. If no
1408c2ecf20Sopenharmony_ci * matching signaled event is found, returns NULL. In that case the
1418c2ecf20Sopenharmony_ci * caller should assume that the partial ID is invalid and do an
1428c2ecf20Sopenharmony_ci * exhaustive search of all siglaned events.
1438c2ecf20Sopenharmony_ci *
1448c2ecf20Sopenharmony_ci * If multiple events with the same partial ID signal at the same
1458c2ecf20Sopenharmony_ci * time, they will be found one interrupt at a time, not necessarily
1468c2ecf20Sopenharmony_ci * in the same order the interrupts occurred. As long as the number of
1478c2ecf20Sopenharmony_ci * interrupts is correct, all signaled events will be seen by the
1488c2ecf20Sopenharmony_ci * driver.
1498c2ecf20Sopenharmony_ci */
1508c2ecf20Sopenharmony_cistatic struct kfd_event *lookup_signaled_event_by_partial_id(
1518c2ecf20Sopenharmony_ci	struct kfd_process *p, uint32_t id, uint32_t bits)
1528c2ecf20Sopenharmony_ci{
1538c2ecf20Sopenharmony_ci	struct kfd_event *ev;
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci	if (!p->signal_page || id >= KFD_SIGNAL_EVENT_LIMIT)
1568c2ecf20Sopenharmony_ci		return NULL;
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ci	/* Fast path for the common case that @id is not a partial ID
1598c2ecf20Sopenharmony_ci	 * and we only need a single lookup.
1608c2ecf20Sopenharmony_ci	 */
1618c2ecf20Sopenharmony_ci	if (bits > 31 || (1U << bits) >= KFD_SIGNAL_EVENT_LIMIT) {
1628c2ecf20Sopenharmony_ci		if (page_slots(p->signal_page)[id] == UNSIGNALED_EVENT_SLOT)
1638c2ecf20Sopenharmony_ci			return NULL;
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci		return idr_find(&p->event_idr, id);
1668c2ecf20Sopenharmony_ci	}
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci	/* General case for partial IDs: Iterate over all matching IDs
1698c2ecf20Sopenharmony_ci	 * and find the first one that has signaled.
1708c2ecf20Sopenharmony_ci	 */
1718c2ecf20Sopenharmony_ci	for (ev = NULL; id < KFD_SIGNAL_EVENT_LIMIT && !ev; id += 1U << bits) {
1728c2ecf20Sopenharmony_ci		if (page_slots(p->signal_page)[id] == UNSIGNALED_EVENT_SLOT)
1738c2ecf20Sopenharmony_ci			continue;
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci		ev = idr_find(&p->event_idr, id);
1768c2ecf20Sopenharmony_ci	}
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ci	return ev;
1798c2ecf20Sopenharmony_ci}
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_cistatic int create_signal_event(struct file *devkfd,
1828c2ecf20Sopenharmony_ci				struct kfd_process *p,
1838c2ecf20Sopenharmony_ci				struct kfd_event *ev)
1848c2ecf20Sopenharmony_ci{
1858c2ecf20Sopenharmony_ci	int ret;
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_ci	if (p->signal_mapped_size &&
1888c2ecf20Sopenharmony_ci	    p->signal_event_count == p->signal_mapped_size / 8) {
1898c2ecf20Sopenharmony_ci		if (!p->signal_event_limit_reached) {
1908c2ecf20Sopenharmony_ci			pr_debug("Signal event wasn't created because limit was reached\n");
1918c2ecf20Sopenharmony_ci			p->signal_event_limit_reached = true;
1928c2ecf20Sopenharmony_ci		}
1938c2ecf20Sopenharmony_ci		return -ENOSPC;
1948c2ecf20Sopenharmony_ci	}
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci	ret = allocate_event_notification_slot(p, ev);
1978c2ecf20Sopenharmony_ci	if (ret) {
1988c2ecf20Sopenharmony_ci		pr_warn("Signal event wasn't created because out of kernel memory\n");
1998c2ecf20Sopenharmony_ci		return ret;
2008c2ecf20Sopenharmony_ci	}
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_ci	p->signal_event_count++;
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ci	ev->user_signal_address = &p->signal_page->user_address[ev->event_id];
2058c2ecf20Sopenharmony_ci	pr_debug("Signal event number %zu created with id %d, address %p\n",
2068c2ecf20Sopenharmony_ci			p->signal_event_count, ev->event_id,
2078c2ecf20Sopenharmony_ci			ev->user_signal_address);
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_ci	return 0;
2108c2ecf20Sopenharmony_ci}
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_cistatic int create_other_event(struct kfd_process *p, struct kfd_event *ev)
2138c2ecf20Sopenharmony_ci{
2148c2ecf20Sopenharmony_ci	/* Cast KFD_LAST_NONSIGNAL_EVENT to uint32_t. This allows an
2158c2ecf20Sopenharmony_ci	 * intentional integer overflow to -1 without a compiler
2168c2ecf20Sopenharmony_ci	 * warning. idr_alloc treats a negative value as "maximum
2178c2ecf20Sopenharmony_ci	 * signed integer".
2188c2ecf20Sopenharmony_ci	 */
2198c2ecf20Sopenharmony_ci	int id = idr_alloc(&p->event_idr, ev, KFD_FIRST_NONSIGNAL_EVENT_ID,
2208c2ecf20Sopenharmony_ci			   (uint32_t)KFD_LAST_NONSIGNAL_EVENT_ID + 1,
2218c2ecf20Sopenharmony_ci			   GFP_KERNEL);
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_ci	if (id < 0)
2248c2ecf20Sopenharmony_ci		return id;
2258c2ecf20Sopenharmony_ci	ev->event_id = id;
2268c2ecf20Sopenharmony_ci
2278c2ecf20Sopenharmony_ci	return 0;
2288c2ecf20Sopenharmony_ci}
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_civoid kfd_event_init_process(struct kfd_process *p)
2318c2ecf20Sopenharmony_ci{
2328c2ecf20Sopenharmony_ci	mutex_init(&p->event_mutex);
2338c2ecf20Sopenharmony_ci	idr_init(&p->event_idr);
2348c2ecf20Sopenharmony_ci	p->signal_page = NULL;
2358c2ecf20Sopenharmony_ci	p->signal_event_count = 0;
2368c2ecf20Sopenharmony_ci}
2378c2ecf20Sopenharmony_ci
2388c2ecf20Sopenharmony_cistatic void destroy_event(struct kfd_process *p, struct kfd_event *ev)
2398c2ecf20Sopenharmony_ci{
2408c2ecf20Sopenharmony_ci	struct kfd_event_waiter *waiter;
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_ci	/* Wake up pending waiters. They will return failure */
2438c2ecf20Sopenharmony_ci	list_for_each_entry(waiter, &ev->wq.head, wait.entry)
2448c2ecf20Sopenharmony_ci		waiter->event = NULL;
2458c2ecf20Sopenharmony_ci	wake_up_all(&ev->wq);
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_ci	if (ev->type == KFD_EVENT_TYPE_SIGNAL ||
2488c2ecf20Sopenharmony_ci	    ev->type == KFD_EVENT_TYPE_DEBUG)
2498c2ecf20Sopenharmony_ci		p->signal_event_count--;
2508c2ecf20Sopenharmony_ci
2518c2ecf20Sopenharmony_ci	idr_remove(&p->event_idr, ev->event_id);
2528c2ecf20Sopenharmony_ci	kfree(ev);
2538c2ecf20Sopenharmony_ci}
2548c2ecf20Sopenharmony_ci
2558c2ecf20Sopenharmony_cistatic void destroy_events(struct kfd_process *p)
2568c2ecf20Sopenharmony_ci{
2578c2ecf20Sopenharmony_ci	struct kfd_event *ev;
2588c2ecf20Sopenharmony_ci	uint32_t id;
2598c2ecf20Sopenharmony_ci
2608c2ecf20Sopenharmony_ci	idr_for_each_entry(&p->event_idr, ev, id)
2618c2ecf20Sopenharmony_ci		destroy_event(p, ev);
2628c2ecf20Sopenharmony_ci	idr_destroy(&p->event_idr);
2638c2ecf20Sopenharmony_ci}
2648c2ecf20Sopenharmony_ci
2658c2ecf20Sopenharmony_ci/*
2668c2ecf20Sopenharmony_ci * We assume that the process is being destroyed and there is no need to
2678c2ecf20Sopenharmony_ci * unmap the pages or keep bookkeeping data in order.
2688c2ecf20Sopenharmony_ci */
2698c2ecf20Sopenharmony_cistatic void shutdown_signal_page(struct kfd_process *p)
2708c2ecf20Sopenharmony_ci{
2718c2ecf20Sopenharmony_ci	struct kfd_signal_page *page = p->signal_page;
2728c2ecf20Sopenharmony_ci
2738c2ecf20Sopenharmony_ci	if (page) {
2748c2ecf20Sopenharmony_ci		if (page->need_to_free_pages)
2758c2ecf20Sopenharmony_ci			free_pages((unsigned long)page->kernel_address,
2768c2ecf20Sopenharmony_ci				   get_order(KFD_SIGNAL_EVENT_LIMIT * 8));
2778c2ecf20Sopenharmony_ci		kfree(page);
2788c2ecf20Sopenharmony_ci	}
2798c2ecf20Sopenharmony_ci}
2808c2ecf20Sopenharmony_ci
2818c2ecf20Sopenharmony_civoid kfd_event_free_process(struct kfd_process *p)
2828c2ecf20Sopenharmony_ci{
2838c2ecf20Sopenharmony_ci	destroy_events(p);
2848c2ecf20Sopenharmony_ci	shutdown_signal_page(p);
2858c2ecf20Sopenharmony_ci}
2868c2ecf20Sopenharmony_ci
2878c2ecf20Sopenharmony_cistatic bool event_can_be_gpu_signaled(const struct kfd_event *ev)
2888c2ecf20Sopenharmony_ci{
2898c2ecf20Sopenharmony_ci	return ev->type == KFD_EVENT_TYPE_SIGNAL ||
2908c2ecf20Sopenharmony_ci					ev->type == KFD_EVENT_TYPE_DEBUG;
2918c2ecf20Sopenharmony_ci}
2928c2ecf20Sopenharmony_ci
2938c2ecf20Sopenharmony_cistatic bool event_can_be_cpu_signaled(const struct kfd_event *ev)
2948c2ecf20Sopenharmony_ci{
2958c2ecf20Sopenharmony_ci	return ev->type == KFD_EVENT_TYPE_SIGNAL;
2968c2ecf20Sopenharmony_ci}
2978c2ecf20Sopenharmony_ci
2988c2ecf20Sopenharmony_ciint kfd_event_page_set(struct kfd_process *p, void *kernel_address,
2998c2ecf20Sopenharmony_ci		       uint64_t size)
3008c2ecf20Sopenharmony_ci{
3018c2ecf20Sopenharmony_ci	struct kfd_signal_page *page;
3028c2ecf20Sopenharmony_ci
3038c2ecf20Sopenharmony_ci	if (p->signal_page)
3048c2ecf20Sopenharmony_ci		return -EBUSY;
3058c2ecf20Sopenharmony_ci
3068c2ecf20Sopenharmony_ci	page = kzalloc(sizeof(*page), GFP_KERNEL);
3078c2ecf20Sopenharmony_ci	if (!page)
3088c2ecf20Sopenharmony_ci		return -ENOMEM;
3098c2ecf20Sopenharmony_ci
3108c2ecf20Sopenharmony_ci	/* Initialize all events to unsignaled */
3118c2ecf20Sopenharmony_ci	memset(kernel_address, (uint8_t) UNSIGNALED_EVENT_SLOT,
3128c2ecf20Sopenharmony_ci	       KFD_SIGNAL_EVENT_LIMIT * 8);
3138c2ecf20Sopenharmony_ci
3148c2ecf20Sopenharmony_ci	page->kernel_address = kernel_address;
3158c2ecf20Sopenharmony_ci
3168c2ecf20Sopenharmony_ci	p->signal_page = page;
3178c2ecf20Sopenharmony_ci	p->signal_mapped_size = size;
3188c2ecf20Sopenharmony_ci
3198c2ecf20Sopenharmony_ci	return 0;
3208c2ecf20Sopenharmony_ci}
3218c2ecf20Sopenharmony_ci
3228c2ecf20Sopenharmony_ciint kfd_event_create(struct file *devkfd, struct kfd_process *p,
3238c2ecf20Sopenharmony_ci		     uint32_t event_type, bool auto_reset, uint32_t node_id,
3248c2ecf20Sopenharmony_ci		     uint32_t *event_id, uint32_t *event_trigger_data,
3258c2ecf20Sopenharmony_ci		     uint64_t *event_page_offset, uint32_t *event_slot_index)
3268c2ecf20Sopenharmony_ci{
3278c2ecf20Sopenharmony_ci	int ret = 0;
3288c2ecf20Sopenharmony_ci	struct kfd_event *ev = kzalloc(sizeof(*ev), GFP_KERNEL);
3298c2ecf20Sopenharmony_ci
3308c2ecf20Sopenharmony_ci	if (!ev)
3318c2ecf20Sopenharmony_ci		return -ENOMEM;
3328c2ecf20Sopenharmony_ci
3338c2ecf20Sopenharmony_ci	ev->type = event_type;
3348c2ecf20Sopenharmony_ci	ev->auto_reset = auto_reset;
3358c2ecf20Sopenharmony_ci	ev->signaled = false;
3368c2ecf20Sopenharmony_ci
3378c2ecf20Sopenharmony_ci	init_waitqueue_head(&ev->wq);
3388c2ecf20Sopenharmony_ci
3398c2ecf20Sopenharmony_ci	*event_page_offset = 0;
3408c2ecf20Sopenharmony_ci
3418c2ecf20Sopenharmony_ci	mutex_lock(&p->event_mutex);
3428c2ecf20Sopenharmony_ci
3438c2ecf20Sopenharmony_ci	switch (event_type) {
3448c2ecf20Sopenharmony_ci	case KFD_EVENT_TYPE_SIGNAL:
3458c2ecf20Sopenharmony_ci	case KFD_EVENT_TYPE_DEBUG:
3468c2ecf20Sopenharmony_ci		ret = create_signal_event(devkfd, p, ev);
3478c2ecf20Sopenharmony_ci		if (!ret) {
3488c2ecf20Sopenharmony_ci			*event_page_offset = KFD_MMAP_TYPE_EVENTS;
3498c2ecf20Sopenharmony_ci			*event_slot_index = ev->event_id;
3508c2ecf20Sopenharmony_ci		}
3518c2ecf20Sopenharmony_ci		break;
3528c2ecf20Sopenharmony_ci	default:
3538c2ecf20Sopenharmony_ci		ret = create_other_event(p, ev);
3548c2ecf20Sopenharmony_ci		break;
3558c2ecf20Sopenharmony_ci	}
3568c2ecf20Sopenharmony_ci
3578c2ecf20Sopenharmony_ci	if (!ret) {
3588c2ecf20Sopenharmony_ci		*event_id = ev->event_id;
3598c2ecf20Sopenharmony_ci		*event_trigger_data = ev->event_id;
3608c2ecf20Sopenharmony_ci	} else {
3618c2ecf20Sopenharmony_ci		kfree(ev);
3628c2ecf20Sopenharmony_ci	}
3638c2ecf20Sopenharmony_ci
3648c2ecf20Sopenharmony_ci	mutex_unlock(&p->event_mutex);
3658c2ecf20Sopenharmony_ci
3668c2ecf20Sopenharmony_ci	return ret;
3678c2ecf20Sopenharmony_ci}
3688c2ecf20Sopenharmony_ci
3698c2ecf20Sopenharmony_ci/* Assumes that p is current. */
3708c2ecf20Sopenharmony_ciint kfd_event_destroy(struct kfd_process *p, uint32_t event_id)
3718c2ecf20Sopenharmony_ci{
3728c2ecf20Sopenharmony_ci	struct kfd_event *ev;
3738c2ecf20Sopenharmony_ci	int ret = 0;
3748c2ecf20Sopenharmony_ci
3758c2ecf20Sopenharmony_ci	mutex_lock(&p->event_mutex);
3768c2ecf20Sopenharmony_ci
3778c2ecf20Sopenharmony_ci	ev = lookup_event_by_id(p, event_id);
3788c2ecf20Sopenharmony_ci
3798c2ecf20Sopenharmony_ci	if (ev)
3808c2ecf20Sopenharmony_ci		destroy_event(p, ev);
3818c2ecf20Sopenharmony_ci	else
3828c2ecf20Sopenharmony_ci		ret = -EINVAL;
3838c2ecf20Sopenharmony_ci
3848c2ecf20Sopenharmony_ci	mutex_unlock(&p->event_mutex);
3858c2ecf20Sopenharmony_ci	return ret;
3868c2ecf20Sopenharmony_ci}
3878c2ecf20Sopenharmony_ci
3888c2ecf20Sopenharmony_cistatic void set_event(struct kfd_event *ev)
3898c2ecf20Sopenharmony_ci{
3908c2ecf20Sopenharmony_ci	struct kfd_event_waiter *waiter;
3918c2ecf20Sopenharmony_ci
3928c2ecf20Sopenharmony_ci	/* Auto reset if the list is non-empty and we're waking
3938c2ecf20Sopenharmony_ci	 * someone. waitqueue_active is safe here because we're
3948c2ecf20Sopenharmony_ci	 * protected by the p->event_mutex, which is also held when
3958c2ecf20Sopenharmony_ci	 * updating the wait queues in kfd_wait_on_events.
3968c2ecf20Sopenharmony_ci	 */
3978c2ecf20Sopenharmony_ci	ev->signaled = !ev->auto_reset || !waitqueue_active(&ev->wq);
3988c2ecf20Sopenharmony_ci
3998c2ecf20Sopenharmony_ci	list_for_each_entry(waiter, &ev->wq.head, wait.entry)
4008c2ecf20Sopenharmony_ci		waiter->activated = true;
4018c2ecf20Sopenharmony_ci
4028c2ecf20Sopenharmony_ci	wake_up_all(&ev->wq);
4038c2ecf20Sopenharmony_ci}
4048c2ecf20Sopenharmony_ci
4058c2ecf20Sopenharmony_ci/* Assumes that p is current. */
4068c2ecf20Sopenharmony_ciint kfd_set_event(struct kfd_process *p, uint32_t event_id)
4078c2ecf20Sopenharmony_ci{
4088c2ecf20Sopenharmony_ci	int ret = 0;
4098c2ecf20Sopenharmony_ci	struct kfd_event *ev;
4108c2ecf20Sopenharmony_ci
4118c2ecf20Sopenharmony_ci	mutex_lock(&p->event_mutex);
4128c2ecf20Sopenharmony_ci
4138c2ecf20Sopenharmony_ci	ev = lookup_event_by_id(p, event_id);
4148c2ecf20Sopenharmony_ci
4158c2ecf20Sopenharmony_ci	if (ev && event_can_be_cpu_signaled(ev))
4168c2ecf20Sopenharmony_ci		set_event(ev);
4178c2ecf20Sopenharmony_ci	else
4188c2ecf20Sopenharmony_ci		ret = -EINVAL;
4198c2ecf20Sopenharmony_ci
4208c2ecf20Sopenharmony_ci	mutex_unlock(&p->event_mutex);
4218c2ecf20Sopenharmony_ci	return ret;
4228c2ecf20Sopenharmony_ci}
4238c2ecf20Sopenharmony_ci
4248c2ecf20Sopenharmony_cistatic void reset_event(struct kfd_event *ev)
4258c2ecf20Sopenharmony_ci{
4268c2ecf20Sopenharmony_ci	ev->signaled = false;
4278c2ecf20Sopenharmony_ci}
4288c2ecf20Sopenharmony_ci
4298c2ecf20Sopenharmony_ci/* Assumes that p is current. */
4308c2ecf20Sopenharmony_ciint kfd_reset_event(struct kfd_process *p, uint32_t event_id)
4318c2ecf20Sopenharmony_ci{
4328c2ecf20Sopenharmony_ci	int ret = 0;
4338c2ecf20Sopenharmony_ci	struct kfd_event *ev;
4348c2ecf20Sopenharmony_ci
4358c2ecf20Sopenharmony_ci	mutex_lock(&p->event_mutex);
4368c2ecf20Sopenharmony_ci
4378c2ecf20Sopenharmony_ci	ev = lookup_event_by_id(p, event_id);
4388c2ecf20Sopenharmony_ci
4398c2ecf20Sopenharmony_ci	if (ev && event_can_be_cpu_signaled(ev))
4408c2ecf20Sopenharmony_ci		reset_event(ev);
4418c2ecf20Sopenharmony_ci	else
4428c2ecf20Sopenharmony_ci		ret = -EINVAL;
4438c2ecf20Sopenharmony_ci
4448c2ecf20Sopenharmony_ci	mutex_unlock(&p->event_mutex);
4458c2ecf20Sopenharmony_ci	return ret;
4468c2ecf20Sopenharmony_ci
4478c2ecf20Sopenharmony_ci}
4488c2ecf20Sopenharmony_ci
4498c2ecf20Sopenharmony_cistatic void acknowledge_signal(struct kfd_process *p, struct kfd_event *ev)
4508c2ecf20Sopenharmony_ci{
4518c2ecf20Sopenharmony_ci	page_slots(p->signal_page)[ev->event_id] = UNSIGNALED_EVENT_SLOT;
4528c2ecf20Sopenharmony_ci}
4538c2ecf20Sopenharmony_ci
4548c2ecf20Sopenharmony_cistatic void set_event_from_interrupt(struct kfd_process *p,
4558c2ecf20Sopenharmony_ci					struct kfd_event *ev)
4568c2ecf20Sopenharmony_ci{
4578c2ecf20Sopenharmony_ci	if (ev && event_can_be_gpu_signaled(ev)) {
4588c2ecf20Sopenharmony_ci		acknowledge_signal(p, ev);
4598c2ecf20Sopenharmony_ci		set_event(ev);
4608c2ecf20Sopenharmony_ci	}
4618c2ecf20Sopenharmony_ci}
4628c2ecf20Sopenharmony_ci
4638c2ecf20Sopenharmony_civoid kfd_signal_event_interrupt(u32 pasid, uint32_t partial_id,
4648c2ecf20Sopenharmony_ci				uint32_t valid_id_bits)
4658c2ecf20Sopenharmony_ci{
4668c2ecf20Sopenharmony_ci	struct kfd_event *ev = NULL;
4678c2ecf20Sopenharmony_ci
4688c2ecf20Sopenharmony_ci	/*
4698c2ecf20Sopenharmony_ci	 * Because we are called from arbitrary context (workqueue) as opposed
4708c2ecf20Sopenharmony_ci	 * to process context, kfd_process could attempt to exit while we are
4718c2ecf20Sopenharmony_ci	 * running so the lookup function increments the process ref count.
4728c2ecf20Sopenharmony_ci	 */
4738c2ecf20Sopenharmony_ci	struct kfd_process *p = kfd_lookup_process_by_pasid(pasid);
4748c2ecf20Sopenharmony_ci
4758c2ecf20Sopenharmony_ci	if (!p)
4768c2ecf20Sopenharmony_ci		return; /* Presumably process exited. */
4778c2ecf20Sopenharmony_ci
4788c2ecf20Sopenharmony_ci	mutex_lock(&p->event_mutex);
4798c2ecf20Sopenharmony_ci
4808c2ecf20Sopenharmony_ci	if (valid_id_bits)
4818c2ecf20Sopenharmony_ci		ev = lookup_signaled_event_by_partial_id(p, partial_id,
4828c2ecf20Sopenharmony_ci							 valid_id_bits);
4838c2ecf20Sopenharmony_ci	if (ev) {
4848c2ecf20Sopenharmony_ci		set_event_from_interrupt(p, ev);
4858c2ecf20Sopenharmony_ci	} else if (p->signal_page) {
4868c2ecf20Sopenharmony_ci		/*
4878c2ecf20Sopenharmony_ci		 * Partial ID lookup failed. Assume that the event ID
4888c2ecf20Sopenharmony_ci		 * in the interrupt payload was invalid and do an
4898c2ecf20Sopenharmony_ci		 * exhaustive search of signaled events.
4908c2ecf20Sopenharmony_ci		 */
4918c2ecf20Sopenharmony_ci		uint64_t *slots = page_slots(p->signal_page);
4928c2ecf20Sopenharmony_ci		uint32_t id;
4938c2ecf20Sopenharmony_ci
4948c2ecf20Sopenharmony_ci		if (valid_id_bits)
4958c2ecf20Sopenharmony_ci			pr_debug_ratelimited("Partial ID invalid: %u (%u valid bits)\n",
4968c2ecf20Sopenharmony_ci					     partial_id, valid_id_bits);
4978c2ecf20Sopenharmony_ci
4988c2ecf20Sopenharmony_ci		if (p->signal_event_count < KFD_SIGNAL_EVENT_LIMIT / 64) {
4998c2ecf20Sopenharmony_ci			/* With relatively few events, it's faster to
5008c2ecf20Sopenharmony_ci			 * iterate over the event IDR
5018c2ecf20Sopenharmony_ci			 */
5028c2ecf20Sopenharmony_ci			idr_for_each_entry(&p->event_idr, ev, id) {
5038c2ecf20Sopenharmony_ci				if (id >= KFD_SIGNAL_EVENT_LIMIT)
5048c2ecf20Sopenharmony_ci					break;
5058c2ecf20Sopenharmony_ci
5068c2ecf20Sopenharmony_ci				if (slots[id] != UNSIGNALED_EVENT_SLOT)
5078c2ecf20Sopenharmony_ci					set_event_from_interrupt(p, ev);
5088c2ecf20Sopenharmony_ci			}
5098c2ecf20Sopenharmony_ci		} else {
5108c2ecf20Sopenharmony_ci			/* With relatively many events, it's faster to
5118c2ecf20Sopenharmony_ci			 * iterate over the signal slots and lookup
5128c2ecf20Sopenharmony_ci			 * only signaled events from the IDR.
5138c2ecf20Sopenharmony_ci			 */
5148c2ecf20Sopenharmony_ci			for (id = 0; id < KFD_SIGNAL_EVENT_LIMIT; id++)
5158c2ecf20Sopenharmony_ci				if (slots[id] != UNSIGNALED_EVENT_SLOT) {
5168c2ecf20Sopenharmony_ci					ev = lookup_event_by_id(p, id);
5178c2ecf20Sopenharmony_ci					set_event_from_interrupt(p, ev);
5188c2ecf20Sopenharmony_ci				}
5198c2ecf20Sopenharmony_ci		}
5208c2ecf20Sopenharmony_ci	}
5218c2ecf20Sopenharmony_ci
5228c2ecf20Sopenharmony_ci	mutex_unlock(&p->event_mutex);
5238c2ecf20Sopenharmony_ci	kfd_unref_process(p);
5248c2ecf20Sopenharmony_ci}
5258c2ecf20Sopenharmony_ci
5268c2ecf20Sopenharmony_cistatic struct kfd_event_waiter *alloc_event_waiters(uint32_t num_events)
5278c2ecf20Sopenharmony_ci{
5288c2ecf20Sopenharmony_ci	struct kfd_event_waiter *event_waiters;
5298c2ecf20Sopenharmony_ci	uint32_t i;
5308c2ecf20Sopenharmony_ci
5318c2ecf20Sopenharmony_ci	event_waiters = kcalloc(num_events, sizeof(struct kfd_event_waiter),
5328c2ecf20Sopenharmony_ci				GFP_KERNEL);
5338c2ecf20Sopenharmony_ci	if (!event_waiters)
5348c2ecf20Sopenharmony_ci		return NULL;
5358c2ecf20Sopenharmony_ci
5368c2ecf20Sopenharmony_ci	for (i = 0; i < num_events; i++)
5378c2ecf20Sopenharmony_ci		init_wait(&event_waiters[i].wait);
5388c2ecf20Sopenharmony_ci
5398c2ecf20Sopenharmony_ci	return event_waiters;
5408c2ecf20Sopenharmony_ci}
5418c2ecf20Sopenharmony_ci
5428c2ecf20Sopenharmony_cistatic int init_event_waiter_get_status(struct kfd_process *p,
5438c2ecf20Sopenharmony_ci		struct kfd_event_waiter *waiter,
5448c2ecf20Sopenharmony_ci		uint32_t event_id)
5458c2ecf20Sopenharmony_ci{
5468c2ecf20Sopenharmony_ci	struct kfd_event *ev = lookup_event_by_id(p, event_id);
5478c2ecf20Sopenharmony_ci
5488c2ecf20Sopenharmony_ci	if (!ev)
5498c2ecf20Sopenharmony_ci		return -EINVAL;
5508c2ecf20Sopenharmony_ci
5518c2ecf20Sopenharmony_ci	waiter->event = ev;
5528c2ecf20Sopenharmony_ci	waiter->activated = ev->signaled;
5538c2ecf20Sopenharmony_ci	ev->signaled = ev->signaled && !ev->auto_reset;
5548c2ecf20Sopenharmony_ci
5558c2ecf20Sopenharmony_ci	return 0;
5568c2ecf20Sopenharmony_ci}
5578c2ecf20Sopenharmony_ci
5588c2ecf20Sopenharmony_cistatic void init_event_waiter_add_to_waitlist(struct kfd_event_waiter *waiter)
5598c2ecf20Sopenharmony_ci{
5608c2ecf20Sopenharmony_ci	struct kfd_event *ev = waiter->event;
5618c2ecf20Sopenharmony_ci
5628c2ecf20Sopenharmony_ci	/* Only add to the wait list if we actually need to
5638c2ecf20Sopenharmony_ci	 * wait on this event.
5648c2ecf20Sopenharmony_ci	 */
5658c2ecf20Sopenharmony_ci	if (!waiter->activated)
5668c2ecf20Sopenharmony_ci		add_wait_queue(&ev->wq, &waiter->wait);
5678c2ecf20Sopenharmony_ci}
5688c2ecf20Sopenharmony_ci
5698c2ecf20Sopenharmony_ci/* test_event_condition - Test condition of events being waited for
5708c2ecf20Sopenharmony_ci * @all:           Return completion only if all events have signaled
5718c2ecf20Sopenharmony_ci * @num_events:    Number of events to wait for
5728c2ecf20Sopenharmony_ci * @event_waiters: Array of event waiters, one per event
5738c2ecf20Sopenharmony_ci *
5748c2ecf20Sopenharmony_ci * Returns KFD_IOC_WAIT_RESULT_COMPLETE if all (or one) event(s) have
5758c2ecf20Sopenharmony_ci * signaled. Returns KFD_IOC_WAIT_RESULT_TIMEOUT if no (or not all)
5768c2ecf20Sopenharmony_ci * events have signaled. Returns KFD_IOC_WAIT_RESULT_FAIL if any of
5778c2ecf20Sopenharmony_ci * the events have been destroyed.
5788c2ecf20Sopenharmony_ci */
5798c2ecf20Sopenharmony_cistatic uint32_t test_event_condition(bool all, uint32_t num_events,
5808c2ecf20Sopenharmony_ci				struct kfd_event_waiter *event_waiters)
5818c2ecf20Sopenharmony_ci{
5828c2ecf20Sopenharmony_ci	uint32_t i;
5838c2ecf20Sopenharmony_ci	uint32_t activated_count = 0;
5848c2ecf20Sopenharmony_ci
5858c2ecf20Sopenharmony_ci	for (i = 0; i < num_events; i++) {
5868c2ecf20Sopenharmony_ci		if (!event_waiters[i].event)
5878c2ecf20Sopenharmony_ci			return KFD_IOC_WAIT_RESULT_FAIL;
5888c2ecf20Sopenharmony_ci
5898c2ecf20Sopenharmony_ci		if (event_waiters[i].activated) {
5908c2ecf20Sopenharmony_ci			if (!all)
5918c2ecf20Sopenharmony_ci				return KFD_IOC_WAIT_RESULT_COMPLETE;
5928c2ecf20Sopenharmony_ci
5938c2ecf20Sopenharmony_ci			activated_count++;
5948c2ecf20Sopenharmony_ci		}
5958c2ecf20Sopenharmony_ci	}
5968c2ecf20Sopenharmony_ci
5978c2ecf20Sopenharmony_ci	return activated_count == num_events ?
5988c2ecf20Sopenharmony_ci		KFD_IOC_WAIT_RESULT_COMPLETE : KFD_IOC_WAIT_RESULT_TIMEOUT;
5998c2ecf20Sopenharmony_ci}
6008c2ecf20Sopenharmony_ci
6018c2ecf20Sopenharmony_ci/*
6028c2ecf20Sopenharmony_ci * Copy event specific data, if defined.
6038c2ecf20Sopenharmony_ci * Currently only memory exception events have additional data to copy to user
6048c2ecf20Sopenharmony_ci */
6058c2ecf20Sopenharmony_cistatic int copy_signaled_event_data(uint32_t num_events,
6068c2ecf20Sopenharmony_ci		struct kfd_event_waiter *event_waiters,
6078c2ecf20Sopenharmony_ci		struct kfd_event_data __user *data)
6088c2ecf20Sopenharmony_ci{
6098c2ecf20Sopenharmony_ci	struct kfd_hsa_memory_exception_data *src;
6108c2ecf20Sopenharmony_ci	struct kfd_hsa_memory_exception_data __user *dst;
6118c2ecf20Sopenharmony_ci	struct kfd_event_waiter *waiter;
6128c2ecf20Sopenharmony_ci	struct kfd_event *event;
6138c2ecf20Sopenharmony_ci	uint32_t i;
6148c2ecf20Sopenharmony_ci
6158c2ecf20Sopenharmony_ci	for (i = 0; i < num_events; i++) {
6168c2ecf20Sopenharmony_ci		waiter = &event_waiters[i];
6178c2ecf20Sopenharmony_ci		event = waiter->event;
6188c2ecf20Sopenharmony_ci		if (waiter->activated && event->type == KFD_EVENT_TYPE_MEMORY) {
6198c2ecf20Sopenharmony_ci			dst = &data[i].memory_exception_data;
6208c2ecf20Sopenharmony_ci			src = &event->memory_exception_data;
6218c2ecf20Sopenharmony_ci			if (copy_to_user(dst, src,
6228c2ecf20Sopenharmony_ci				sizeof(struct kfd_hsa_memory_exception_data)))
6238c2ecf20Sopenharmony_ci				return -EFAULT;
6248c2ecf20Sopenharmony_ci		}
6258c2ecf20Sopenharmony_ci	}
6268c2ecf20Sopenharmony_ci
6278c2ecf20Sopenharmony_ci	return 0;
6288c2ecf20Sopenharmony_ci
6298c2ecf20Sopenharmony_ci}
6308c2ecf20Sopenharmony_ci
6318c2ecf20Sopenharmony_ci
6328c2ecf20Sopenharmony_ci
6338c2ecf20Sopenharmony_cistatic long user_timeout_to_jiffies(uint32_t user_timeout_ms)
6348c2ecf20Sopenharmony_ci{
6358c2ecf20Sopenharmony_ci	if (user_timeout_ms == KFD_EVENT_TIMEOUT_IMMEDIATE)
6368c2ecf20Sopenharmony_ci		return 0;
6378c2ecf20Sopenharmony_ci
6388c2ecf20Sopenharmony_ci	if (user_timeout_ms == KFD_EVENT_TIMEOUT_INFINITE)
6398c2ecf20Sopenharmony_ci		return MAX_SCHEDULE_TIMEOUT;
6408c2ecf20Sopenharmony_ci
6418c2ecf20Sopenharmony_ci	/*
6428c2ecf20Sopenharmony_ci	 * msecs_to_jiffies interprets all values above 2^31-1 as infinite,
6438c2ecf20Sopenharmony_ci	 * but we consider them finite.
6448c2ecf20Sopenharmony_ci	 * This hack is wrong, but nobody is likely to notice.
6458c2ecf20Sopenharmony_ci	 */
6468c2ecf20Sopenharmony_ci	user_timeout_ms = min_t(uint32_t, user_timeout_ms, 0x7FFFFFFF);
6478c2ecf20Sopenharmony_ci
6488c2ecf20Sopenharmony_ci	return msecs_to_jiffies(user_timeout_ms) + 1;
6498c2ecf20Sopenharmony_ci}
6508c2ecf20Sopenharmony_ci
6518c2ecf20Sopenharmony_cistatic void free_waiters(uint32_t num_events, struct kfd_event_waiter *waiters)
6528c2ecf20Sopenharmony_ci{
6538c2ecf20Sopenharmony_ci	uint32_t i;
6548c2ecf20Sopenharmony_ci
6558c2ecf20Sopenharmony_ci	for (i = 0; i < num_events; i++)
6568c2ecf20Sopenharmony_ci		if (waiters[i].event)
6578c2ecf20Sopenharmony_ci			remove_wait_queue(&waiters[i].event->wq,
6588c2ecf20Sopenharmony_ci					  &waiters[i].wait);
6598c2ecf20Sopenharmony_ci
6608c2ecf20Sopenharmony_ci	kfree(waiters);
6618c2ecf20Sopenharmony_ci}
6628c2ecf20Sopenharmony_ci
6638c2ecf20Sopenharmony_ciint kfd_wait_on_events(struct kfd_process *p,
6648c2ecf20Sopenharmony_ci		       uint32_t num_events, void __user *data,
6658c2ecf20Sopenharmony_ci		       bool all, uint32_t user_timeout_ms,
6668c2ecf20Sopenharmony_ci		       uint32_t *wait_result)
6678c2ecf20Sopenharmony_ci{
6688c2ecf20Sopenharmony_ci	struct kfd_event_data __user *events =
6698c2ecf20Sopenharmony_ci			(struct kfd_event_data __user *) data;
6708c2ecf20Sopenharmony_ci	uint32_t i;
6718c2ecf20Sopenharmony_ci	int ret = 0;
6728c2ecf20Sopenharmony_ci
6738c2ecf20Sopenharmony_ci	struct kfd_event_waiter *event_waiters = NULL;
6748c2ecf20Sopenharmony_ci	long timeout = user_timeout_to_jiffies(user_timeout_ms);
6758c2ecf20Sopenharmony_ci
6768c2ecf20Sopenharmony_ci	event_waiters = alloc_event_waiters(num_events);
6778c2ecf20Sopenharmony_ci	if (!event_waiters) {
6788c2ecf20Sopenharmony_ci		ret = -ENOMEM;
6798c2ecf20Sopenharmony_ci		goto out;
6808c2ecf20Sopenharmony_ci	}
6818c2ecf20Sopenharmony_ci
6828c2ecf20Sopenharmony_ci	mutex_lock(&p->event_mutex);
6838c2ecf20Sopenharmony_ci
6848c2ecf20Sopenharmony_ci	for (i = 0; i < num_events; i++) {
6858c2ecf20Sopenharmony_ci		struct kfd_event_data event_data;
6868c2ecf20Sopenharmony_ci
6878c2ecf20Sopenharmony_ci		if (copy_from_user(&event_data, &events[i],
6888c2ecf20Sopenharmony_ci				sizeof(struct kfd_event_data))) {
6898c2ecf20Sopenharmony_ci			ret = -EFAULT;
6908c2ecf20Sopenharmony_ci			goto out_unlock;
6918c2ecf20Sopenharmony_ci		}
6928c2ecf20Sopenharmony_ci
6938c2ecf20Sopenharmony_ci		ret = init_event_waiter_get_status(p, &event_waiters[i],
6948c2ecf20Sopenharmony_ci				event_data.event_id);
6958c2ecf20Sopenharmony_ci		if (ret)
6968c2ecf20Sopenharmony_ci			goto out_unlock;
6978c2ecf20Sopenharmony_ci	}
6988c2ecf20Sopenharmony_ci
6998c2ecf20Sopenharmony_ci	/* Check condition once. */
7008c2ecf20Sopenharmony_ci	*wait_result = test_event_condition(all, num_events, event_waiters);
7018c2ecf20Sopenharmony_ci	if (*wait_result == KFD_IOC_WAIT_RESULT_COMPLETE) {
7028c2ecf20Sopenharmony_ci		ret = copy_signaled_event_data(num_events,
7038c2ecf20Sopenharmony_ci					       event_waiters, events);
7048c2ecf20Sopenharmony_ci		goto out_unlock;
7058c2ecf20Sopenharmony_ci	} else if (WARN_ON(*wait_result == KFD_IOC_WAIT_RESULT_FAIL)) {
7068c2ecf20Sopenharmony_ci		/* This should not happen. Events shouldn't be
7078c2ecf20Sopenharmony_ci		 * destroyed while we're holding the event_mutex
7088c2ecf20Sopenharmony_ci		 */
7098c2ecf20Sopenharmony_ci		goto out_unlock;
7108c2ecf20Sopenharmony_ci	}
7118c2ecf20Sopenharmony_ci
7128c2ecf20Sopenharmony_ci	/* Add to wait lists if we need to wait. */
7138c2ecf20Sopenharmony_ci	for (i = 0; i < num_events; i++)
7148c2ecf20Sopenharmony_ci		init_event_waiter_add_to_waitlist(&event_waiters[i]);
7158c2ecf20Sopenharmony_ci
7168c2ecf20Sopenharmony_ci	mutex_unlock(&p->event_mutex);
7178c2ecf20Sopenharmony_ci
7188c2ecf20Sopenharmony_ci	while (true) {
7198c2ecf20Sopenharmony_ci		if (fatal_signal_pending(current)) {
7208c2ecf20Sopenharmony_ci			ret = -EINTR;
7218c2ecf20Sopenharmony_ci			break;
7228c2ecf20Sopenharmony_ci		}
7238c2ecf20Sopenharmony_ci
7248c2ecf20Sopenharmony_ci		if (signal_pending(current)) {
7258c2ecf20Sopenharmony_ci			/*
7268c2ecf20Sopenharmony_ci			 * This is wrong when a nonzero, non-infinite timeout
7278c2ecf20Sopenharmony_ci			 * is specified. We need to use
7288c2ecf20Sopenharmony_ci			 * ERESTARTSYS_RESTARTBLOCK, but struct restart_block
7298c2ecf20Sopenharmony_ci			 * contains a union with data for each user and it's
7308c2ecf20Sopenharmony_ci			 * in generic kernel code that I don't want to
7318c2ecf20Sopenharmony_ci			 * touch yet.
7328c2ecf20Sopenharmony_ci			 */
7338c2ecf20Sopenharmony_ci			ret = -ERESTARTSYS;
7348c2ecf20Sopenharmony_ci			break;
7358c2ecf20Sopenharmony_ci		}
7368c2ecf20Sopenharmony_ci
7378c2ecf20Sopenharmony_ci		/* Set task state to interruptible sleep before
7388c2ecf20Sopenharmony_ci		 * checking wake-up conditions. A concurrent wake-up
7398c2ecf20Sopenharmony_ci		 * will put the task back into runnable state. In that
7408c2ecf20Sopenharmony_ci		 * case schedule_timeout will not put the task to
7418c2ecf20Sopenharmony_ci		 * sleep and we'll get a chance to re-check the
7428c2ecf20Sopenharmony_ci		 * updated conditions almost immediately. Otherwise,
7438c2ecf20Sopenharmony_ci		 * this race condition would lead to a soft hang or a
7448c2ecf20Sopenharmony_ci		 * very long sleep.
7458c2ecf20Sopenharmony_ci		 */
7468c2ecf20Sopenharmony_ci		set_current_state(TASK_INTERRUPTIBLE);
7478c2ecf20Sopenharmony_ci
7488c2ecf20Sopenharmony_ci		*wait_result = test_event_condition(all, num_events,
7498c2ecf20Sopenharmony_ci						    event_waiters);
7508c2ecf20Sopenharmony_ci		if (*wait_result != KFD_IOC_WAIT_RESULT_TIMEOUT)
7518c2ecf20Sopenharmony_ci			break;
7528c2ecf20Sopenharmony_ci
7538c2ecf20Sopenharmony_ci		if (timeout <= 0)
7548c2ecf20Sopenharmony_ci			break;
7558c2ecf20Sopenharmony_ci
7568c2ecf20Sopenharmony_ci		timeout = schedule_timeout(timeout);
7578c2ecf20Sopenharmony_ci	}
7588c2ecf20Sopenharmony_ci	__set_current_state(TASK_RUNNING);
7598c2ecf20Sopenharmony_ci
7608c2ecf20Sopenharmony_ci	/* copy_signaled_event_data may sleep. So this has to happen
7618c2ecf20Sopenharmony_ci	 * after the task state is set back to RUNNING.
7628c2ecf20Sopenharmony_ci	 */
7638c2ecf20Sopenharmony_ci	if (!ret && *wait_result == KFD_IOC_WAIT_RESULT_COMPLETE)
7648c2ecf20Sopenharmony_ci		ret = copy_signaled_event_data(num_events,
7658c2ecf20Sopenharmony_ci					       event_waiters, events);
7668c2ecf20Sopenharmony_ci
7678c2ecf20Sopenharmony_ci	mutex_lock(&p->event_mutex);
7688c2ecf20Sopenharmony_ciout_unlock:
7698c2ecf20Sopenharmony_ci	free_waiters(num_events, event_waiters);
7708c2ecf20Sopenharmony_ci	mutex_unlock(&p->event_mutex);
7718c2ecf20Sopenharmony_ciout:
7728c2ecf20Sopenharmony_ci	if (ret)
7738c2ecf20Sopenharmony_ci		*wait_result = KFD_IOC_WAIT_RESULT_FAIL;
7748c2ecf20Sopenharmony_ci	else if (*wait_result == KFD_IOC_WAIT_RESULT_FAIL)
7758c2ecf20Sopenharmony_ci		ret = -EIO;
7768c2ecf20Sopenharmony_ci
7778c2ecf20Sopenharmony_ci	return ret;
7788c2ecf20Sopenharmony_ci}
7798c2ecf20Sopenharmony_ci
7808c2ecf20Sopenharmony_ciint kfd_event_mmap(struct kfd_process *p, struct vm_area_struct *vma)
7818c2ecf20Sopenharmony_ci{
7828c2ecf20Sopenharmony_ci	unsigned long pfn;
7838c2ecf20Sopenharmony_ci	struct kfd_signal_page *page;
7848c2ecf20Sopenharmony_ci	int ret;
7858c2ecf20Sopenharmony_ci
7868c2ecf20Sopenharmony_ci	/* check required size doesn't exceed the allocated size */
7878c2ecf20Sopenharmony_ci	if (get_order(KFD_SIGNAL_EVENT_LIMIT * 8) <
7888c2ecf20Sopenharmony_ci			get_order(vma->vm_end - vma->vm_start)) {
7898c2ecf20Sopenharmony_ci		pr_err("Event page mmap requested illegal size\n");
7908c2ecf20Sopenharmony_ci		return -EINVAL;
7918c2ecf20Sopenharmony_ci	}
7928c2ecf20Sopenharmony_ci
7938c2ecf20Sopenharmony_ci	page = p->signal_page;
7948c2ecf20Sopenharmony_ci	if (!page) {
7958c2ecf20Sopenharmony_ci		/* Probably KFD bug, but mmap is user-accessible. */
7968c2ecf20Sopenharmony_ci		pr_debug("Signal page could not be found\n");
7978c2ecf20Sopenharmony_ci		return -EINVAL;
7988c2ecf20Sopenharmony_ci	}
7998c2ecf20Sopenharmony_ci
8008c2ecf20Sopenharmony_ci	pfn = __pa(page->kernel_address);
8018c2ecf20Sopenharmony_ci	pfn >>= PAGE_SHIFT;
8028c2ecf20Sopenharmony_ci
8038c2ecf20Sopenharmony_ci	vma->vm_flags |= VM_IO | VM_DONTCOPY | VM_DONTEXPAND | VM_NORESERVE
8048c2ecf20Sopenharmony_ci		       | VM_DONTDUMP | VM_PFNMAP;
8058c2ecf20Sopenharmony_ci
8068c2ecf20Sopenharmony_ci	pr_debug("Mapping signal page\n");
8078c2ecf20Sopenharmony_ci	pr_debug("     start user address  == 0x%08lx\n", vma->vm_start);
8088c2ecf20Sopenharmony_ci	pr_debug("     end user address    == 0x%08lx\n", vma->vm_end);
8098c2ecf20Sopenharmony_ci	pr_debug("     pfn                 == 0x%016lX\n", pfn);
8108c2ecf20Sopenharmony_ci	pr_debug("     vm_flags            == 0x%08lX\n", vma->vm_flags);
8118c2ecf20Sopenharmony_ci	pr_debug("     size                == 0x%08lX\n",
8128c2ecf20Sopenharmony_ci			vma->vm_end - vma->vm_start);
8138c2ecf20Sopenharmony_ci
8148c2ecf20Sopenharmony_ci	page->user_address = (uint64_t __user *)vma->vm_start;
8158c2ecf20Sopenharmony_ci
8168c2ecf20Sopenharmony_ci	/* mapping the page to user process */
8178c2ecf20Sopenharmony_ci	ret = remap_pfn_range(vma, vma->vm_start, pfn,
8188c2ecf20Sopenharmony_ci			vma->vm_end - vma->vm_start, vma->vm_page_prot);
8198c2ecf20Sopenharmony_ci	if (!ret)
8208c2ecf20Sopenharmony_ci		p->signal_mapped_size = vma->vm_end - vma->vm_start;
8218c2ecf20Sopenharmony_ci
8228c2ecf20Sopenharmony_ci	return ret;
8238c2ecf20Sopenharmony_ci}
8248c2ecf20Sopenharmony_ci
8258c2ecf20Sopenharmony_ci/*
8268c2ecf20Sopenharmony_ci * Assumes that p->event_mutex is held and of course
8278c2ecf20Sopenharmony_ci * that p is not going away (current or locked).
8288c2ecf20Sopenharmony_ci */
8298c2ecf20Sopenharmony_cistatic void lookup_events_by_type_and_signal(struct kfd_process *p,
8308c2ecf20Sopenharmony_ci		int type, void *event_data)
8318c2ecf20Sopenharmony_ci{
8328c2ecf20Sopenharmony_ci	struct kfd_hsa_memory_exception_data *ev_data;
8338c2ecf20Sopenharmony_ci	struct kfd_event *ev;
8348c2ecf20Sopenharmony_ci	uint32_t id;
8358c2ecf20Sopenharmony_ci	bool send_signal = true;
8368c2ecf20Sopenharmony_ci
8378c2ecf20Sopenharmony_ci	ev_data = (struct kfd_hsa_memory_exception_data *) event_data;
8388c2ecf20Sopenharmony_ci
8398c2ecf20Sopenharmony_ci	id = KFD_FIRST_NONSIGNAL_EVENT_ID;
8408c2ecf20Sopenharmony_ci	idr_for_each_entry_continue(&p->event_idr, ev, id)
8418c2ecf20Sopenharmony_ci		if (ev->type == type) {
8428c2ecf20Sopenharmony_ci			send_signal = false;
8438c2ecf20Sopenharmony_ci			dev_dbg(kfd_device,
8448c2ecf20Sopenharmony_ci					"Event found: id %X type %d",
8458c2ecf20Sopenharmony_ci					ev->event_id, ev->type);
8468c2ecf20Sopenharmony_ci			set_event(ev);
8478c2ecf20Sopenharmony_ci			if (ev->type == KFD_EVENT_TYPE_MEMORY && ev_data)
8488c2ecf20Sopenharmony_ci				ev->memory_exception_data = *ev_data;
8498c2ecf20Sopenharmony_ci		}
8508c2ecf20Sopenharmony_ci
8518c2ecf20Sopenharmony_ci	if (type == KFD_EVENT_TYPE_MEMORY) {
8528c2ecf20Sopenharmony_ci		dev_warn(kfd_device,
8538c2ecf20Sopenharmony_ci			"Sending SIGSEGV to process %d (pasid 0x%x)",
8548c2ecf20Sopenharmony_ci				p->lead_thread->pid, p->pasid);
8558c2ecf20Sopenharmony_ci		send_sig(SIGSEGV, p->lead_thread, 0);
8568c2ecf20Sopenharmony_ci	}
8578c2ecf20Sopenharmony_ci
8588c2ecf20Sopenharmony_ci	/* Send SIGTERM no event of type "type" has been found*/
8598c2ecf20Sopenharmony_ci	if (send_signal) {
8608c2ecf20Sopenharmony_ci		if (send_sigterm) {
8618c2ecf20Sopenharmony_ci			dev_warn(kfd_device,
8628c2ecf20Sopenharmony_ci				"Sending SIGTERM to process %d (pasid 0x%x)",
8638c2ecf20Sopenharmony_ci					p->lead_thread->pid, p->pasid);
8648c2ecf20Sopenharmony_ci			send_sig(SIGTERM, p->lead_thread, 0);
8658c2ecf20Sopenharmony_ci		} else {
8668c2ecf20Sopenharmony_ci			dev_err(kfd_device,
8678c2ecf20Sopenharmony_ci				"Process %d (pasid 0x%x) got unhandled exception",
8688c2ecf20Sopenharmony_ci				p->lead_thread->pid, p->pasid);
8698c2ecf20Sopenharmony_ci		}
8708c2ecf20Sopenharmony_ci	}
8718c2ecf20Sopenharmony_ci}
8728c2ecf20Sopenharmony_ci
8738c2ecf20Sopenharmony_ci#ifdef KFD_SUPPORT_IOMMU_V2
8748c2ecf20Sopenharmony_civoid kfd_signal_iommu_event(struct kfd_dev *dev, u32 pasid,
8758c2ecf20Sopenharmony_ci		unsigned long address, bool is_write_requested,
8768c2ecf20Sopenharmony_ci		bool is_execute_requested)
8778c2ecf20Sopenharmony_ci{
8788c2ecf20Sopenharmony_ci	struct kfd_hsa_memory_exception_data memory_exception_data;
8798c2ecf20Sopenharmony_ci	struct vm_area_struct *vma;
8808c2ecf20Sopenharmony_ci
8818c2ecf20Sopenharmony_ci	/*
8828c2ecf20Sopenharmony_ci	 * Because we are called from arbitrary context (workqueue) as opposed
8838c2ecf20Sopenharmony_ci	 * to process context, kfd_process could attempt to exit while we are
8848c2ecf20Sopenharmony_ci	 * running so the lookup function increments the process ref count.
8858c2ecf20Sopenharmony_ci	 */
8868c2ecf20Sopenharmony_ci	struct kfd_process *p = kfd_lookup_process_by_pasid(pasid);
8878c2ecf20Sopenharmony_ci	struct mm_struct *mm;
8888c2ecf20Sopenharmony_ci
8898c2ecf20Sopenharmony_ci	if (!p)
8908c2ecf20Sopenharmony_ci		return; /* Presumably process exited. */
8918c2ecf20Sopenharmony_ci
8928c2ecf20Sopenharmony_ci	/* Take a safe reference to the mm_struct, which may otherwise
8938c2ecf20Sopenharmony_ci	 * disappear even while the kfd_process is still referenced.
8948c2ecf20Sopenharmony_ci	 */
8958c2ecf20Sopenharmony_ci	mm = get_task_mm(p->lead_thread);
8968c2ecf20Sopenharmony_ci	if (!mm) {
8978c2ecf20Sopenharmony_ci		kfd_unref_process(p);
8988c2ecf20Sopenharmony_ci		return; /* Process is exiting */
8998c2ecf20Sopenharmony_ci	}
9008c2ecf20Sopenharmony_ci
9018c2ecf20Sopenharmony_ci	memset(&memory_exception_data, 0, sizeof(memory_exception_data));
9028c2ecf20Sopenharmony_ci
9038c2ecf20Sopenharmony_ci	mmap_read_lock(mm);
9048c2ecf20Sopenharmony_ci	vma = find_vma(mm, address);
9058c2ecf20Sopenharmony_ci
9068c2ecf20Sopenharmony_ci	memory_exception_data.gpu_id = dev->id;
9078c2ecf20Sopenharmony_ci	memory_exception_data.va = address;
9088c2ecf20Sopenharmony_ci	/* Set failure reason */
9098c2ecf20Sopenharmony_ci	memory_exception_data.failure.NotPresent = 1;
9108c2ecf20Sopenharmony_ci	memory_exception_data.failure.NoExecute = 0;
9118c2ecf20Sopenharmony_ci	memory_exception_data.failure.ReadOnly = 0;
9128c2ecf20Sopenharmony_ci	if (vma && address >= vma->vm_start) {
9138c2ecf20Sopenharmony_ci		memory_exception_data.failure.NotPresent = 0;
9148c2ecf20Sopenharmony_ci
9158c2ecf20Sopenharmony_ci		if (is_write_requested && !(vma->vm_flags & VM_WRITE))
9168c2ecf20Sopenharmony_ci			memory_exception_data.failure.ReadOnly = 1;
9178c2ecf20Sopenharmony_ci		else
9188c2ecf20Sopenharmony_ci			memory_exception_data.failure.ReadOnly = 0;
9198c2ecf20Sopenharmony_ci
9208c2ecf20Sopenharmony_ci		if (is_execute_requested && !(vma->vm_flags & VM_EXEC))
9218c2ecf20Sopenharmony_ci			memory_exception_data.failure.NoExecute = 1;
9228c2ecf20Sopenharmony_ci		else
9238c2ecf20Sopenharmony_ci			memory_exception_data.failure.NoExecute = 0;
9248c2ecf20Sopenharmony_ci	}
9258c2ecf20Sopenharmony_ci
9268c2ecf20Sopenharmony_ci	mmap_read_unlock(mm);
9278c2ecf20Sopenharmony_ci	mmput(mm);
9288c2ecf20Sopenharmony_ci
9298c2ecf20Sopenharmony_ci	pr_debug("notpresent %d, noexecute %d, readonly %d\n",
9308c2ecf20Sopenharmony_ci			memory_exception_data.failure.NotPresent,
9318c2ecf20Sopenharmony_ci			memory_exception_data.failure.NoExecute,
9328c2ecf20Sopenharmony_ci			memory_exception_data.failure.ReadOnly);
9338c2ecf20Sopenharmony_ci
9348c2ecf20Sopenharmony_ci	/* Workaround on Raven to not kill the process when memory is freed
9358c2ecf20Sopenharmony_ci	 * before IOMMU is able to finish processing all the excessive PPRs
9368c2ecf20Sopenharmony_ci	 */
9378c2ecf20Sopenharmony_ci	if (dev->device_info->asic_family != CHIP_RAVEN &&
9388c2ecf20Sopenharmony_ci	    dev->device_info->asic_family != CHIP_RENOIR) {
9398c2ecf20Sopenharmony_ci		mutex_lock(&p->event_mutex);
9408c2ecf20Sopenharmony_ci
9418c2ecf20Sopenharmony_ci		/* Lookup events by type and signal them */
9428c2ecf20Sopenharmony_ci		lookup_events_by_type_and_signal(p, KFD_EVENT_TYPE_MEMORY,
9438c2ecf20Sopenharmony_ci				&memory_exception_data);
9448c2ecf20Sopenharmony_ci
9458c2ecf20Sopenharmony_ci		mutex_unlock(&p->event_mutex);
9468c2ecf20Sopenharmony_ci	}
9478c2ecf20Sopenharmony_ci
9488c2ecf20Sopenharmony_ci	kfd_unref_process(p);
9498c2ecf20Sopenharmony_ci}
9508c2ecf20Sopenharmony_ci#endif /* KFD_SUPPORT_IOMMU_V2 */
9518c2ecf20Sopenharmony_ci
9528c2ecf20Sopenharmony_civoid kfd_signal_hw_exception_event(u32 pasid)
9538c2ecf20Sopenharmony_ci{
9548c2ecf20Sopenharmony_ci	/*
9558c2ecf20Sopenharmony_ci	 * Because we are called from arbitrary context (workqueue) as opposed
9568c2ecf20Sopenharmony_ci	 * to process context, kfd_process could attempt to exit while we are
9578c2ecf20Sopenharmony_ci	 * running so the lookup function increments the process ref count.
9588c2ecf20Sopenharmony_ci	 */
9598c2ecf20Sopenharmony_ci	struct kfd_process *p = kfd_lookup_process_by_pasid(pasid);
9608c2ecf20Sopenharmony_ci
9618c2ecf20Sopenharmony_ci	if (!p)
9628c2ecf20Sopenharmony_ci		return; /* Presumably process exited. */
9638c2ecf20Sopenharmony_ci
9648c2ecf20Sopenharmony_ci	mutex_lock(&p->event_mutex);
9658c2ecf20Sopenharmony_ci
9668c2ecf20Sopenharmony_ci	/* Lookup events by type and signal them */
9678c2ecf20Sopenharmony_ci	lookup_events_by_type_and_signal(p, KFD_EVENT_TYPE_HW_EXCEPTION, NULL);
9688c2ecf20Sopenharmony_ci
9698c2ecf20Sopenharmony_ci	mutex_unlock(&p->event_mutex);
9708c2ecf20Sopenharmony_ci	kfd_unref_process(p);
9718c2ecf20Sopenharmony_ci}
9728c2ecf20Sopenharmony_ci
9738c2ecf20Sopenharmony_civoid kfd_signal_vm_fault_event(struct kfd_dev *dev, u32 pasid,
9748c2ecf20Sopenharmony_ci				struct kfd_vm_fault_info *info)
9758c2ecf20Sopenharmony_ci{
9768c2ecf20Sopenharmony_ci	struct kfd_event *ev;
9778c2ecf20Sopenharmony_ci	uint32_t id;
9788c2ecf20Sopenharmony_ci	struct kfd_process *p = kfd_lookup_process_by_pasid(pasid);
9798c2ecf20Sopenharmony_ci	struct kfd_hsa_memory_exception_data memory_exception_data;
9808c2ecf20Sopenharmony_ci
9818c2ecf20Sopenharmony_ci	if (!p)
9828c2ecf20Sopenharmony_ci		return; /* Presumably process exited. */
9838c2ecf20Sopenharmony_ci	memset(&memory_exception_data, 0, sizeof(memory_exception_data));
9848c2ecf20Sopenharmony_ci	memory_exception_data.gpu_id = dev->id;
9858c2ecf20Sopenharmony_ci	memory_exception_data.failure.imprecise = true;
9868c2ecf20Sopenharmony_ci	/* Set failure reason */
9878c2ecf20Sopenharmony_ci	if (info) {
9888c2ecf20Sopenharmony_ci		memory_exception_data.va = (info->page_addr) << PAGE_SHIFT;
9898c2ecf20Sopenharmony_ci		memory_exception_data.failure.NotPresent =
9908c2ecf20Sopenharmony_ci			info->prot_valid ? 1 : 0;
9918c2ecf20Sopenharmony_ci		memory_exception_data.failure.NoExecute =
9928c2ecf20Sopenharmony_ci			info->prot_exec ? 1 : 0;
9938c2ecf20Sopenharmony_ci		memory_exception_data.failure.ReadOnly =
9948c2ecf20Sopenharmony_ci			info->prot_write ? 1 : 0;
9958c2ecf20Sopenharmony_ci		memory_exception_data.failure.imprecise = 0;
9968c2ecf20Sopenharmony_ci	}
9978c2ecf20Sopenharmony_ci	mutex_lock(&p->event_mutex);
9988c2ecf20Sopenharmony_ci
9998c2ecf20Sopenharmony_ci	id = KFD_FIRST_NONSIGNAL_EVENT_ID;
10008c2ecf20Sopenharmony_ci	idr_for_each_entry_continue(&p->event_idr, ev, id)
10018c2ecf20Sopenharmony_ci		if (ev->type == KFD_EVENT_TYPE_MEMORY) {
10028c2ecf20Sopenharmony_ci			ev->memory_exception_data = memory_exception_data;
10038c2ecf20Sopenharmony_ci			set_event(ev);
10048c2ecf20Sopenharmony_ci		}
10058c2ecf20Sopenharmony_ci
10068c2ecf20Sopenharmony_ci	mutex_unlock(&p->event_mutex);
10078c2ecf20Sopenharmony_ci	kfd_unref_process(p);
10088c2ecf20Sopenharmony_ci}
10098c2ecf20Sopenharmony_ci
10108c2ecf20Sopenharmony_civoid kfd_signal_reset_event(struct kfd_dev *dev)
10118c2ecf20Sopenharmony_ci{
10128c2ecf20Sopenharmony_ci	struct kfd_hsa_hw_exception_data hw_exception_data;
10138c2ecf20Sopenharmony_ci	struct kfd_hsa_memory_exception_data memory_exception_data;
10148c2ecf20Sopenharmony_ci	struct kfd_process *p;
10158c2ecf20Sopenharmony_ci	struct kfd_event *ev;
10168c2ecf20Sopenharmony_ci	unsigned int temp;
10178c2ecf20Sopenharmony_ci	uint32_t id, idx;
10188c2ecf20Sopenharmony_ci	int reset_cause = atomic_read(&dev->sram_ecc_flag) ?
10198c2ecf20Sopenharmony_ci			KFD_HW_EXCEPTION_ECC :
10208c2ecf20Sopenharmony_ci			KFD_HW_EXCEPTION_GPU_HANG;
10218c2ecf20Sopenharmony_ci
10228c2ecf20Sopenharmony_ci	/* Whole gpu reset caused by GPU hang and memory is lost */
10238c2ecf20Sopenharmony_ci	memset(&hw_exception_data, 0, sizeof(hw_exception_data));
10248c2ecf20Sopenharmony_ci	hw_exception_data.gpu_id = dev->id;
10258c2ecf20Sopenharmony_ci	hw_exception_data.memory_lost = 1;
10268c2ecf20Sopenharmony_ci	hw_exception_data.reset_cause = reset_cause;
10278c2ecf20Sopenharmony_ci
10288c2ecf20Sopenharmony_ci	memset(&memory_exception_data, 0, sizeof(memory_exception_data));
10298c2ecf20Sopenharmony_ci	memory_exception_data.ErrorType = KFD_MEM_ERR_SRAM_ECC;
10308c2ecf20Sopenharmony_ci	memory_exception_data.gpu_id = dev->id;
10318c2ecf20Sopenharmony_ci	memory_exception_data.failure.imprecise = true;
10328c2ecf20Sopenharmony_ci
10338c2ecf20Sopenharmony_ci	idx = srcu_read_lock(&kfd_processes_srcu);
10348c2ecf20Sopenharmony_ci	hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) {
10358c2ecf20Sopenharmony_ci		mutex_lock(&p->event_mutex);
10368c2ecf20Sopenharmony_ci		id = KFD_FIRST_NONSIGNAL_EVENT_ID;
10378c2ecf20Sopenharmony_ci		idr_for_each_entry_continue(&p->event_idr, ev, id) {
10388c2ecf20Sopenharmony_ci			if (ev->type == KFD_EVENT_TYPE_HW_EXCEPTION) {
10398c2ecf20Sopenharmony_ci				ev->hw_exception_data = hw_exception_data;
10408c2ecf20Sopenharmony_ci				set_event(ev);
10418c2ecf20Sopenharmony_ci			}
10428c2ecf20Sopenharmony_ci			if (ev->type == KFD_EVENT_TYPE_MEMORY &&
10438c2ecf20Sopenharmony_ci			    reset_cause == KFD_HW_EXCEPTION_ECC) {
10448c2ecf20Sopenharmony_ci				ev->memory_exception_data = memory_exception_data;
10458c2ecf20Sopenharmony_ci				set_event(ev);
10468c2ecf20Sopenharmony_ci			}
10478c2ecf20Sopenharmony_ci		}
10488c2ecf20Sopenharmony_ci		mutex_unlock(&p->event_mutex);
10498c2ecf20Sopenharmony_ci	}
10508c2ecf20Sopenharmony_ci	srcu_read_unlock(&kfd_processes_srcu, idx);
10518c2ecf20Sopenharmony_ci}
1052