1aa3b483fSShuo Liu // SPDX-License-Identifier: GPL-2.0
2aa3b483fSShuo Liu /*
3aa3b483fSShuo Liu * ACRN HSM irqfd: use eventfd objects to inject virtual interrupts
4aa3b483fSShuo Liu *
5aa3b483fSShuo Liu * Copyright (C) 2020 Intel Corporation. All rights reserved.
6aa3b483fSShuo Liu *
7aa3b483fSShuo Liu * Authors:
8aa3b483fSShuo Liu * Shuo Liu <shuo.a.liu@intel.com>
9aa3b483fSShuo Liu * Yakui Zhao <yakui.zhao@intel.com>
10aa3b483fSShuo Liu */
11aa3b483fSShuo Liu
12aa3b483fSShuo Liu #include <linux/eventfd.h>
13aa3b483fSShuo Liu #include <linux/file.h>
14aa3b483fSShuo Liu #include <linux/poll.h>
15aa3b483fSShuo Liu #include <linux/slab.h>
16aa3b483fSShuo Liu
17aa3b483fSShuo Liu #include "acrn_drv.h"
18aa3b483fSShuo Liu
19aa3b483fSShuo Liu static LIST_HEAD(acrn_irqfd_clients);
20aa3b483fSShuo Liu
21aa3b483fSShuo Liu /**
22aa3b483fSShuo Liu * struct hsm_irqfd - Properties of HSM irqfd
23aa3b483fSShuo Liu * @vm: Associated VM pointer
24aa3b483fSShuo Liu * @wait: Entry of wait-queue
25aa3b483fSShuo Liu * @shutdown: Async shutdown work
26aa3b483fSShuo Liu * @eventfd: Associated eventfd
27aa3b483fSShuo Liu * @list: Entry within &acrn_vm.irqfds of irqfds of a VM
28aa3b483fSShuo Liu * @pt: Structure for select/poll on the associated eventfd
29aa3b483fSShuo Liu * @msi: MSI data
30aa3b483fSShuo Liu */
31aa3b483fSShuo Liu struct hsm_irqfd {
32aa3b483fSShuo Liu struct acrn_vm *vm;
33aa3b483fSShuo Liu wait_queue_entry_t wait;
34aa3b483fSShuo Liu struct work_struct shutdown;
35aa3b483fSShuo Liu struct eventfd_ctx *eventfd;
36aa3b483fSShuo Liu struct list_head list;
37aa3b483fSShuo Liu poll_table pt;
38aa3b483fSShuo Liu struct acrn_msi_entry msi;
39aa3b483fSShuo Liu };
40aa3b483fSShuo Liu
acrn_irqfd_inject(struct hsm_irqfd * irqfd)41aa3b483fSShuo Liu static void acrn_irqfd_inject(struct hsm_irqfd *irqfd)
42aa3b483fSShuo Liu {
43aa3b483fSShuo Liu struct acrn_vm *vm = irqfd->vm;
44aa3b483fSShuo Liu
45aa3b483fSShuo Liu acrn_msi_inject(vm, irqfd->msi.msi_addr,
46aa3b483fSShuo Liu irqfd->msi.msi_data);
47aa3b483fSShuo Liu }
48aa3b483fSShuo Liu
hsm_irqfd_shutdown(struct hsm_irqfd * irqfd)49aa3b483fSShuo Liu static void hsm_irqfd_shutdown(struct hsm_irqfd *irqfd)
50aa3b483fSShuo Liu {
51aa3b483fSShuo Liu u64 cnt;
52aa3b483fSShuo Liu
53aa3b483fSShuo Liu lockdep_assert_held(&irqfd->vm->irqfds_lock);
54aa3b483fSShuo Liu
55aa3b483fSShuo Liu /* remove from wait queue */
56aa3b483fSShuo Liu list_del_init(&irqfd->list);
57aa3b483fSShuo Liu eventfd_ctx_remove_wait_queue(irqfd->eventfd, &irqfd->wait, &cnt);
58aa3b483fSShuo Liu eventfd_ctx_put(irqfd->eventfd);
59aa3b483fSShuo Liu kfree(irqfd);
60aa3b483fSShuo Liu }
61aa3b483fSShuo Liu
hsm_irqfd_shutdown_work(struct work_struct * work)62aa3b483fSShuo Liu static void hsm_irqfd_shutdown_work(struct work_struct *work)
63aa3b483fSShuo Liu {
64aa3b483fSShuo Liu struct hsm_irqfd *irqfd;
65aa3b483fSShuo Liu struct acrn_vm *vm;
66aa3b483fSShuo Liu
67aa3b483fSShuo Liu irqfd = container_of(work, struct hsm_irqfd, shutdown);
68aa3b483fSShuo Liu vm = irqfd->vm;
69aa3b483fSShuo Liu mutex_lock(&vm->irqfds_lock);
70aa3b483fSShuo Liu if (!list_empty(&irqfd->list))
71aa3b483fSShuo Liu hsm_irqfd_shutdown(irqfd);
72aa3b483fSShuo Liu mutex_unlock(&vm->irqfds_lock);
73aa3b483fSShuo Liu }
74aa3b483fSShuo Liu
75aa3b483fSShuo Liu /* Called with wqh->lock held and interrupts disabled */
hsm_irqfd_wakeup(wait_queue_entry_t * wait,unsigned int mode,int sync,void * key)76aa3b483fSShuo Liu static int hsm_irqfd_wakeup(wait_queue_entry_t *wait, unsigned int mode,
77aa3b483fSShuo Liu int sync, void *key)
78aa3b483fSShuo Liu {
79aa3b483fSShuo Liu unsigned long poll_bits = (unsigned long)key;
80aa3b483fSShuo Liu struct hsm_irqfd *irqfd;
81aa3b483fSShuo Liu struct acrn_vm *vm;
82aa3b483fSShuo Liu
83aa3b483fSShuo Liu irqfd = container_of(wait, struct hsm_irqfd, wait);
84aa3b483fSShuo Liu vm = irqfd->vm;
85aa3b483fSShuo Liu if (poll_bits & POLLIN)
86aa3b483fSShuo Liu /* An event has been signaled, inject an interrupt */
87aa3b483fSShuo Liu acrn_irqfd_inject(irqfd);
88aa3b483fSShuo Liu
89aa3b483fSShuo Liu if (poll_bits & POLLHUP)
90aa3b483fSShuo Liu /* Do shutdown work in thread to hold wqh->lock */
91aa3b483fSShuo Liu queue_work(vm->irqfd_wq, &irqfd->shutdown);
92aa3b483fSShuo Liu
93aa3b483fSShuo Liu return 0;
94aa3b483fSShuo Liu }
95aa3b483fSShuo Liu
hsm_irqfd_poll_func(struct file * file,wait_queue_head_t * wqh,poll_table * pt)96aa3b483fSShuo Liu static void hsm_irqfd_poll_func(struct file *file, wait_queue_head_t *wqh,
97aa3b483fSShuo Liu poll_table *pt)
98aa3b483fSShuo Liu {
99aa3b483fSShuo Liu struct hsm_irqfd *irqfd;
100aa3b483fSShuo Liu
101aa3b483fSShuo Liu irqfd = container_of(pt, struct hsm_irqfd, pt);
102aa3b483fSShuo Liu add_wait_queue(wqh, &irqfd->wait);
103aa3b483fSShuo Liu }
104aa3b483fSShuo Liu
105aa3b483fSShuo Liu /*
106aa3b483fSShuo Liu * Assign an eventfd to a VM and create a HSM irqfd associated with the
107aa3b483fSShuo Liu * eventfd. The properties of the HSM irqfd are built from a &struct
108aa3b483fSShuo Liu * acrn_irqfd.
109aa3b483fSShuo Liu */
acrn_irqfd_assign(struct acrn_vm * vm,struct acrn_irqfd * args)110aa3b483fSShuo Liu static int acrn_irqfd_assign(struct acrn_vm *vm, struct acrn_irqfd *args)
111aa3b483fSShuo Liu {
112aa3b483fSShuo Liu struct eventfd_ctx *eventfd = NULL;
113aa3b483fSShuo Liu struct hsm_irqfd *irqfd, *tmp;
114dcf9625fSYejune Deng __poll_t events;
115aa3b483fSShuo Liu struct fd f;
116aa3b483fSShuo Liu int ret = 0;
117aa3b483fSShuo Liu
118aa3b483fSShuo Liu irqfd = kzalloc(sizeof(*irqfd), GFP_KERNEL);
119aa3b483fSShuo Liu if (!irqfd)
120aa3b483fSShuo Liu return -ENOMEM;
121aa3b483fSShuo Liu
122aa3b483fSShuo Liu irqfd->vm = vm;
123aa3b483fSShuo Liu memcpy(&irqfd->msi, &args->msi, sizeof(args->msi));
124aa3b483fSShuo Liu INIT_LIST_HEAD(&irqfd->list);
125aa3b483fSShuo Liu INIT_WORK(&irqfd->shutdown, hsm_irqfd_shutdown_work);
126aa3b483fSShuo Liu
127aa3b483fSShuo Liu f = fdget(args->fd);
128*1da91ea8SAl Viro if (!fd_file(f)) {
129aa3b483fSShuo Liu ret = -EBADF;
130aa3b483fSShuo Liu goto out;
131aa3b483fSShuo Liu }
132aa3b483fSShuo Liu
133*1da91ea8SAl Viro eventfd = eventfd_ctx_fileget(fd_file(f));
134aa3b483fSShuo Liu if (IS_ERR(eventfd)) {
135aa3b483fSShuo Liu ret = PTR_ERR(eventfd);
136aa3b483fSShuo Liu goto fail;
137aa3b483fSShuo Liu }
138aa3b483fSShuo Liu
139aa3b483fSShuo Liu irqfd->eventfd = eventfd;
140aa3b483fSShuo Liu
141aa3b483fSShuo Liu /*
142aa3b483fSShuo Liu * Install custom wake-up handling to be notified whenever underlying
143aa3b483fSShuo Liu * eventfd is signaled.
144aa3b483fSShuo Liu */
145aa3b483fSShuo Liu init_waitqueue_func_entry(&irqfd->wait, hsm_irqfd_wakeup);
146aa3b483fSShuo Liu init_poll_funcptr(&irqfd->pt, hsm_irqfd_poll_func);
147aa3b483fSShuo Liu
148aa3b483fSShuo Liu mutex_lock(&vm->irqfds_lock);
149aa3b483fSShuo Liu list_for_each_entry(tmp, &vm->irqfds, list) {
150aa3b483fSShuo Liu if (irqfd->eventfd != tmp->eventfd)
151aa3b483fSShuo Liu continue;
152aa3b483fSShuo Liu ret = -EBUSY;
153aa3b483fSShuo Liu mutex_unlock(&vm->irqfds_lock);
154aa3b483fSShuo Liu goto fail;
155aa3b483fSShuo Liu }
156aa3b483fSShuo Liu list_add_tail(&irqfd->list, &vm->irqfds);
157aa3b483fSShuo Liu mutex_unlock(&vm->irqfds_lock);
158aa3b483fSShuo Liu
159aa3b483fSShuo Liu /* Check the pending event in this stage */
160*1da91ea8SAl Viro events = vfs_poll(fd_file(f), &irqfd->pt);
161aa3b483fSShuo Liu
162a758b7c4SYejune Deng if (events & EPOLLIN)
163aa3b483fSShuo Liu acrn_irqfd_inject(irqfd);
164aa3b483fSShuo Liu
165aa3b483fSShuo Liu fdput(f);
166aa3b483fSShuo Liu return 0;
167aa3b483fSShuo Liu fail:
168aa3b483fSShuo Liu if (eventfd && !IS_ERR(eventfd))
169aa3b483fSShuo Liu eventfd_ctx_put(eventfd);
170aa3b483fSShuo Liu
171aa3b483fSShuo Liu fdput(f);
172aa3b483fSShuo Liu out:
173aa3b483fSShuo Liu kfree(irqfd);
174aa3b483fSShuo Liu return ret;
175aa3b483fSShuo Liu }
176aa3b483fSShuo Liu
acrn_irqfd_deassign(struct acrn_vm * vm,struct acrn_irqfd * args)177aa3b483fSShuo Liu static int acrn_irqfd_deassign(struct acrn_vm *vm,
178aa3b483fSShuo Liu struct acrn_irqfd *args)
179aa3b483fSShuo Liu {
180aa3b483fSShuo Liu struct hsm_irqfd *irqfd, *tmp;
181aa3b483fSShuo Liu struct eventfd_ctx *eventfd;
182aa3b483fSShuo Liu
183aa3b483fSShuo Liu eventfd = eventfd_ctx_fdget(args->fd);
184aa3b483fSShuo Liu if (IS_ERR(eventfd))
185aa3b483fSShuo Liu return PTR_ERR(eventfd);
186aa3b483fSShuo Liu
187aa3b483fSShuo Liu mutex_lock(&vm->irqfds_lock);
188aa3b483fSShuo Liu list_for_each_entry_safe(irqfd, tmp, &vm->irqfds, list) {
189aa3b483fSShuo Liu if (irqfd->eventfd == eventfd) {
190aa3b483fSShuo Liu hsm_irqfd_shutdown(irqfd);
191aa3b483fSShuo Liu break;
192aa3b483fSShuo Liu }
193aa3b483fSShuo Liu }
194aa3b483fSShuo Liu mutex_unlock(&vm->irqfds_lock);
195aa3b483fSShuo Liu eventfd_ctx_put(eventfd);
196aa3b483fSShuo Liu
197aa3b483fSShuo Liu return 0;
198aa3b483fSShuo Liu }
199aa3b483fSShuo Liu
acrn_irqfd_config(struct acrn_vm * vm,struct acrn_irqfd * args)200aa3b483fSShuo Liu int acrn_irqfd_config(struct acrn_vm *vm, struct acrn_irqfd *args)
201aa3b483fSShuo Liu {
202aa3b483fSShuo Liu int ret;
203aa3b483fSShuo Liu
204aa3b483fSShuo Liu if (args->flags & ACRN_IRQFD_FLAG_DEASSIGN)
205aa3b483fSShuo Liu ret = acrn_irqfd_deassign(vm, args);
206aa3b483fSShuo Liu else
207aa3b483fSShuo Liu ret = acrn_irqfd_assign(vm, args);
208aa3b483fSShuo Liu
209aa3b483fSShuo Liu return ret;
210aa3b483fSShuo Liu }
211aa3b483fSShuo Liu
acrn_irqfd_init(struct acrn_vm * vm)212aa3b483fSShuo Liu int acrn_irqfd_init(struct acrn_vm *vm)
213aa3b483fSShuo Liu {
214aa3b483fSShuo Liu INIT_LIST_HEAD(&vm->irqfds);
215aa3b483fSShuo Liu mutex_init(&vm->irqfds_lock);
216aa3b483fSShuo Liu vm->irqfd_wq = alloc_workqueue("acrn_irqfd-%u", 0, 0, vm->vmid);
217aa3b483fSShuo Liu if (!vm->irqfd_wq)
218aa3b483fSShuo Liu return -ENOMEM;
219aa3b483fSShuo Liu
220aa3b483fSShuo Liu dev_dbg(acrn_dev.this_device, "VM %u irqfd init.\n", vm->vmid);
221aa3b483fSShuo Liu return 0;
222aa3b483fSShuo Liu }
223aa3b483fSShuo Liu
acrn_irqfd_deinit(struct acrn_vm * vm)224aa3b483fSShuo Liu void acrn_irqfd_deinit(struct acrn_vm *vm)
225aa3b483fSShuo Liu {
226aa3b483fSShuo Liu struct hsm_irqfd *irqfd, *next;
227aa3b483fSShuo Liu
228aa3b483fSShuo Liu dev_dbg(acrn_dev.this_device, "VM %u irqfd deinit.\n", vm->vmid);
229aa3b483fSShuo Liu destroy_workqueue(vm->irqfd_wq);
230aa3b483fSShuo Liu mutex_lock(&vm->irqfds_lock);
231aa3b483fSShuo Liu list_for_each_entry_safe(irqfd, next, &vm->irqfds, list)
232aa3b483fSShuo Liu hsm_irqfd_shutdown(irqfd);
233aa3b483fSShuo Liu mutex_unlock(&vm->irqfds_lock);
234aa3b483fSShuo Liu }
235