1 // SPDX-License-Identifier: GPL-2.0 OR MIT
3 * Copyright 2020-2022 Advanced Micro Devices, Inc.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21 * OTHER DEALINGS IN THE SOFTWARE.
24 #include <linux/poll.h>
25 #include <linux/wait.h>
26 #include <linux/anon_inodes.h>
27 #include <uapi/linux/kfd_ioctl.h>
29 #include "amdgpu_vm.h"
31 #include "kfd_smi_events.h"
33 struct kfd_smi_client {
34 struct list_head list;
36 wait_queue_head_t wait_queue;
43 #define MAX_KFIFO_SIZE 1024
45 static __poll_t kfd_smi_ev_poll(struct file *, struct poll_table_struct *);
46 static ssize_t kfd_smi_ev_read(struct file *, char __user *, size_t, loff_t *);
47 static ssize_t kfd_smi_ev_write(struct file *, const char __user *, size_t,
49 static int kfd_smi_ev_release(struct inode *, struct file *);
51 static const char kfd_smi_name[] = "kfd_smi_ev";
53 static const struct file_operations kfd_smi_ev_fops = {
55 .poll = kfd_smi_ev_poll,
56 .read = kfd_smi_ev_read,
57 .write = kfd_smi_ev_write,
58 .release = kfd_smi_ev_release
61 static __poll_t kfd_smi_ev_poll(struct file *filep,
62 struct poll_table_struct *wait)
64 struct kfd_smi_client *client = filep->private_data;
67 poll_wait(filep, &client->wait_queue, wait);
69 spin_lock(&client->lock);
70 if (!kfifo_is_empty(&client->fifo))
71 mask = EPOLLIN | EPOLLRDNORM;
72 spin_unlock(&client->lock);
77 static ssize_t kfd_smi_ev_read(struct file *filep, char __user *user,
78 size_t size, loff_t *offset)
82 struct kfd_smi_client *client = filep->private_data;
85 size = min_t(size_t, size, MAX_KFIFO_SIZE);
86 buf = kmalloc(size, GFP_KERNEL);
90 /* kfifo_to_user can sleep so we can't use spinlock protection around
91 * it. Instead, we kfifo out as spinlocked then copy them to the user.
93 spin_lock(&client->lock);
94 to_copy = kfifo_len(&client->fifo);
96 spin_unlock(&client->lock);
100 to_copy = min(size, to_copy);
101 ret = kfifo_out(&client->fifo, buf, to_copy);
102 spin_unlock(&client->lock);
108 ret = copy_to_user(user, buf, to_copy);
122 static ssize_t kfd_smi_ev_write(struct file *filep, const char __user *user,
123 size_t size, loff_t *offset)
125 struct kfd_smi_client *client = filep->private_data;
128 if (!access_ok(user, size) || size < sizeof(events))
130 if (copy_from_user(&events, user, sizeof(events)))
133 WRITE_ONCE(client->events, events);
135 return sizeof(events);
138 static int kfd_smi_ev_release(struct inode *inode, struct file *filep)
140 struct kfd_smi_client *client = filep->private_data;
141 struct kfd_dev *dev = client->dev;
143 spin_lock(&dev->smi_lock);
144 list_del_rcu(&client->list);
145 spin_unlock(&dev->smi_lock);
148 kfifo_free(&client->fifo);
154 static void add_event_to_kfifo(struct kfd_dev *dev, unsigned int smi_event,
155 char *event_msg, int len)
157 struct kfd_smi_client *client;
161 list_for_each_entry_rcu(client, &dev->smi_clients, list) {
162 if (!(READ_ONCE(client->events) &
163 KFD_SMI_EVENT_MASK_FROM_INDEX(smi_event)))
165 spin_lock(&client->lock);
166 if (kfifo_avail(&client->fifo) >= len) {
167 kfifo_in(&client->fifo, event_msg, len);
168 wake_up_all(&client->wait_queue);
170 pr_debug("smi_event(EventID: %u): no space left\n",
173 spin_unlock(&client->lock);
180 static void kfd_smi_event_add(struct kfd_dev *dev, unsigned int event,
183 char fifo_in[KFD_SMI_EVENT_MSG_SIZE];
187 if (list_empty(&dev->smi_clients))
190 len = snprintf(fifo_in, sizeof(fifo_in), "%x ", event);
193 len += vsnprintf(fifo_in + len, sizeof(fifo_in) - len, fmt, args);
196 add_event_to_kfifo(dev, event, fifo_in, len);
199 void kfd_smi_event_update_gpu_reset(struct kfd_dev *dev, bool post_reset)
204 event = KFD_SMI_EVENT_GPU_POST_RESET;
206 event = KFD_SMI_EVENT_GPU_PRE_RESET;
207 ++(dev->reset_seq_num);
209 kfd_smi_event_add(dev, event, "%x\n", dev->reset_seq_num);
212 void kfd_smi_event_update_thermal_throttling(struct kfd_dev *dev,
213 uint64_t throttle_bitmask)
215 kfd_smi_event_add(dev, KFD_SMI_EVENT_THERMAL_THROTTLE, "%llx:%llx\n",
217 amdgpu_dpm_get_thermal_throttling_counter(dev->adev));
220 void kfd_smi_event_update_vmfault(struct kfd_dev *dev, uint16_t pasid)
222 struct amdgpu_task_info task_info;
224 memset(&task_info, 0, sizeof(struct amdgpu_task_info));
225 amdgpu_vm_get_task_info(dev->adev, pasid, &task_info);
226 /* Report VM faults from user applications, not retry from kernel */
230 kfd_smi_event_add(dev, KFD_SMI_EVENT_VMFAULT, "%x:%s\n",
231 task_info.pid, task_info.task_name);
234 int kfd_smi_event_open(struct kfd_dev *dev, uint32_t *fd)
236 struct kfd_smi_client *client;
239 client = kzalloc(sizeof(struct kfd_smi_client), GFP_KERNEL);
242 INIT_LIST_HEAD(&client->list);
244 ret = kfifo_alloc(&client->fifo, MAX_KFIFO_SIZE, GFP_KERNEL);
250 init_waitqueue_head(&client->wait_queue);
251 spin_lock_init(&client->lock);
255 spin_lock(&dev->smi_lock);
256 list_add_rcu(&client->list, &dev->smi_clients);
257 spin_unlock(&dev->smi_lock);
259 ret = anon_inode_getfd(kfd_smi_name, &kfd_smi_ev_fops, (void *)client,
262 spin_lock(&dev->smi_lock);
263 list_del_rcu(&client->list);
264 spin_unlock(&dev->smi_lock);
268 kfifo_free(&client->fifo);