1 // SPDX-License-Identifier: GPL-2.0
3 * ACRN_HSM: Handle I/O requests
5 * Copyright (C) 2020 Intel Corporation. All rights reserved.
8 * Jason Chen CJ <jason.cj.chen@intel.com>
9 * Fengwei Yin <fengwei.yin@intel.com>
12 #include <linux/interrupt.h>
14 #include <linux/kthread.h>
16 #include <linux/slab.h>
22 static void ioreq_pause(void);
23 static void ioreq_resume(void);
25 static void ioreq_dispatcher(struct work_struct *work);
26 static struct workqueue_struct *ioreq_wq;
27 static DECLARE_WORK(ioreq_work, ioreq_dispatcher);
29 static inline bool has_pending_request(struct acrn_ioreq_client *client)
31 return !bitmap_empty(client->ioreqs_map, ACRN_IO_REQUEST_MAX);
34 static inline bool is_destroying(struct acrn_ioreq_client *client)
36 return test_bit(ACRN_IOREQ_CLIENT_DESTROYING, &client->flags);
39 static int ioreq_complete_request(struct acrn_vm *vm, u16 vcpu,
40 struct acrn_io_request *acrn_req)
45 polling_mode = acrn_req->completion_polling;
46 /* Add barrier() to make sure the writes are done before completion */
47 smp_store_release(&acrn_req->processed, ACRN_IOREQ_STATE_COMPLETE);
50 * To fulfill the requirement of real-time in several industry
51 * scenarios, like automotive, ACRN can run under the partition mode,
52 * in which User VMs and Service VM are bound to dedicated CPU cores.
53 * Polling mode of handling the I/O request is introduced to achieve a
54 * faster I/O request handling. In polling mode, the hypervisor polls
55 * I/O request's completion. Once an I/O request is marked as
56 * ACRN_IOREQ_STATE_COMPLETE, hypervisor resumes from the polling point
57 * to continue the I/O request flow. Thus, the completion notification
58 * from HSM of I/O request is not needed. Please note,
59 * completion_polling needs to be read before the I/O request being
60 * marked as ACRN_IOREQ_STATE_COMPLETE to avoid racing with the
64 ret = hcall_notify_req_finish(vm->vmid, vcpu);
66 dev_err(acrn_dev.this_device,
67 "Notify I/O request finished failed!\n");
73 static int acrn_ioreq_complete_request(struct acrn_ioreq_client *client,
75 struct acrn_io_request *acrn_req)
79 if (vcpu >= client->vm->vcpu_num)
82 clear_bit(vcpu, client->ioreqs_map);
84 acrn_req = (struct acrn_io_request *)client->vm->ioreq_buf;
88 ret = ioreq_complete_request(client->vm, vcpu, acrn_req);
93 int acrn_ioreq_request_default_complete(struct acrn_vm *vm, u16 vcpu)
97 spin_lock_bh(&vm->ioreq_clients_lock);
98 if (vm->default_client)
99 ret = acrn_ioreq_complete_request(vm->default_client,
101 spin_unlock_bh(&vm->ioreq_clients_lock);
107 * acrn_ioreq_range_add() - Add an iorange monitored by an ioreq client
108 * @client: The ioreq client
109 * @type: Type (ACRN_IOREQ_TYPE_MMIO or ACRN_IOREQ_TYPE_PORTIO)
110 * @start: Start address of iorange
111 * @end: End address of iorange
113 * Return: 0 on success, <0 on error
115 int acrn_ioreq_range_add(struct acrn_ioreq_client *client,
116 u32 type, u64 start, u64 end)
118 struct acrn_ioreq_range *range;
121 dev_err(acrn_dev.this_device,
122 "Invalid IO range [0x%llx,0x%llx]\n", start, end);
126 range = kzalloc(sizeof(*range), GFP_KERNEL);
131 range->start = start;
134 write_lock_bh(&client->range_lock);
135 list_add(&range->list, &client->range_list);
136 write_unlock_bh(&client->range_lock);
142 * acrn_ioreq_range_del() - Del an iorange monitored by an ioreq client
143 * @client: The ioreq client
144 * @type: Type (ACRN_IOREQ_TYPE_MMIO or ACRN_IOREQ_TYPE_PORTIO)
145 * @start: Start address of iorange
146 * @end: End address of iorange
148 void acrn_ioreq_range_del(struct acrn_ioreq_client *client,
149 u32 type, u64 start, u64 end)
151 struct acrn_ioreq_range *range;
153 write_lock_bh(&client->range_lock);
154 list_for_each_entry(range, &client->range_list, list) {
155 if (type == range->type &&
156 start == range->start &&
158 list_del(&range->list);
163 write_unlock_bh(&client->range_lock);
167 * ioreq_task() is the execution entity of handler thread of an I/O client.
168 * The handler callback of the I/O client is called within the handler thread.
170 static int ioreq_task(void *data)
172 struct acrn_ioreq_client *client = data;
173 struct acrn_io_request *req;
174 unsigned long *ioreqs_map;
178 * Lockless access to ioreqs_map is safe, because
179 * 1) set_bit() and clear_bit() are atomic operations.
180 * 2) I/O requests arrives serialized. The access flow of ioreqs_map is:
181 * set_bit() - in ioreq_work handler
182 * Handler callback handles corresponding I/O request
183 * clear_bit() - in handler thread (include ACRN userspace)
184 * Mark corresponding I/O request completed
185 * Loop again if a new I/O request occurs
187 ioreqs_map = client->ioreqs_map;
188 while (!kthread_should_stop()) {
189 acrn_ioreq_client_wait(client);
190 while (has_pending_request(client)) {
191 vcpu = find_first_bit(ioreqs_map, client->vm->vcpu_num);
192 req = client->vm->ioreq_buf->req_slot + vcpu;
193 ret = client->handler(client, req);
195 dev_err(acrn_dev.this_device,
196 "IO handle failure: %d\n", ret);
199 acrn_ioreq_complete_request(client, vcpu, req);
207 * For the non-default I/O clients, give them chance to complete the current
208 * I/O requests if there are any. For the default I/O client, it is safe to
209 * clear all pending I/O requests because the clearing request is from ACRN
212 void acrn_ioreq_request_clear(struct acrn_vm *vm)
214 struct acrn_ioreq_client *client;
215 bool has_pending = false;
220 * IO requests of this VM will be completed directly in
221 * acrn_ioreq_dispatch if ACRN_VM_FLAG_CLEARING_IOREQ flag is set.
223 set_bit(ACRN_VM_FLAG_CLEARING_IOREQ, &vm->flags);
226 * acrn_ioreq_request_clear is only called in VM reset case. Simply
227 * wait 100ms in total for the IO requests' completion.
230 spin_lock_bh(&vm->ioreq_clients_lock);
231 list_for_each_entry(client, &vm->ioreq_clients, list) {
232 has_pending = has_pending_request(client);
236 spin_unlock_bh(&vm->ioreq_clients_lock);
239 schedule_timeout_interruptible(HZ / 100);
240 } while (has_pending && --retry > 0);
242 dev_warn(acrn_dev.this_device,
243 "%s cannot flush pending request!\n", client->name);
245 /* Clear all ioreqs belonging to the default client */
246 spin_lock_bh(&vm->ioreq_clients_lock);
247 client = vm->default_client;
249 for_each_set_bit(vcpu, client->ioreqs_map, ACRN_IO_REQUEST_MAX)
250 acrn_ioreq_complete_request(client, vcpu, NULL);
252 spin_unlock_bh(&vm->ioreq_clients_lock);
254 /* Clear ACRN_VM_FLAG_CLEARING_IOREQ flag after the clearing */
255 clear_bit(ACRN_VM_FLAG_CLEARING_IOREQ, &vm->flags);
258 int acrn_ioreq_client_wait(struct acrn_ioreq_client *client)
260 if (client->is_default) {
262 * In the default client, a user space thread waits on the
263 * waitqueue. The is_destroying() check is used to notify user
264 * space the client is going to be destroyed.
266 wait_event_interruptible(client->wq,
267 has_pending_request(client) ||
268 is_destroying(client));
269 if (is_destroying(client))
272 wait_event_interruptible(client->wq,
273 has_pending_request(client) ||
274 kthread_should_stop());
280 static bool is_cfg_addr(struct acrn_io_request *req)
282 return ((req->type == ACRN_IOREQ_TYPE_PORTIO) &&
283 (req->reqs.pio_request.address == 0xcf8));
286 static bool is_cfg_data(struct acrn_io_request *req)
288 return ((req->type == ACRN_IOREQ_TYPE_PORTIO) &&
289 ((req->reqs.pio_request.address >= 0xcfc) &&
290 (req->reqs.pio_request.address < (0xcfc + 4))));
293 /* The low 8-bit of supported pci_reg addr.*/
294 #define PCI_LOWREG_MASK 0xFC
295 /* The high 4-bit of supported pci_reg addr */
296 #define PCI_HIGHREG_MASK 0xF00
297 /* Max number of supported functions */
298 #define PCI_FUNCMAX 7
299 /* Max number of supported slots */
300 #define PCI_SLOTMAX 31
301 /* Max number of supported buses */
302 #define PCI_BUSMAX 255
303 #define CONF1_ENABLE 0x80000000UL
305 * A PCI configuration space access via PIO 0xCF8 and 0xCFC normally has two
307 * 1) writes address into 0xCF8 port
308 * 2) accesses data in/from 0xCFC
309 * This function combines such paired PCI configuration space I/O requests into
310 * one ACRN_IOREQ_TYPE_PCICFG type I/O request and continues the processing.
312 static bool handle_cf8cfc(struct acrn_vm *vm,
313 struct acrn_io_request *req, u16 vcpu)
315 int offset, pci_cfg_addr, pci_reg;
316 bool is_handled = false;
318 if (is_cfg_addr(req)) {
319 WARN_ON(req->reqs.pio_request.size != 4);
320 if (req->reqs.pio_request.direction == ACRN_IOREQ_DIR_WRITE)
321 vm->pci_conf_addr = req->reqs.pio_request.value;
323 req->reqs.pio_request.value = vm->pci_conf_addr;
325 } else if (is_cfg_data(req)) {
326 if (!(vm->pci_conf_addr & CONF1_ENABLE)) {
327 if (req->reqs.pio_request.direction ==
329 req->reqs.pio_request.value = 0xffffffff;
332 offset = req->reqs.pio_request.address - 0xcfc;
334 req->type = ACRN_IOREQ_TYPE_PCICFG;
335 pci_cfg_addr = vm->pci_conf_addr;
336 req->reqs.pci_request.bus =
337 (pci_cfg_addr >> 16) & PCI_BUSMAX;
338 req->reqs.pci_request.dev =
339 (pci_cfg_addr >> 11) & PCI_SLOTMAX;
340 req->reqs.pci_request.func =
341 (pci_cfg_addr >> 8) & PCI_FUNCMAX;
342 pci_reg = (pci_cfg_addr & PCI_LOWREG_MASK) +
343 ((pci_cfg_addr >> 16) & PCI_HIGHREG_MASK);
344 req->reqs.pci_request.reg = pci_reg + offset;
349 ioreq_complete_request(vm, vcpu, req);
354 static bool acrn_in_range(struct acrn_ioreq_range *range,
355 struct acrn_io_request *req)
359 if (range->type == req->type) {
361 case ACRN_IOREQ_TYPE_MMIO:
362 if (req->reqs.mmio_request.address >= range->start &&
363 (req->reqs.mmio_request.address +
364 req->reqs.mmio_request.size - 1) <= range->end)
367 case ACRN_IOREQ_TYPE_PORTIO:
368 if (req->reqs.pio_request.address >= range->start &&
369 (req->reqs.pio_request.address +
370 req->reqs.pio_request.size - 1) <= range->end)
381 static struct acrn_ioreq_client *find_ioreq_client(struct acrn_vm *vm,
382 struct acrn_io_request *req)
384 struct acrn_ioreq_client *client, *found = NULL;
385 struct acrn_ioreq_range *range;
387 lockdep_assert_held(&vm->ioreq_clients_lock);
389 list_for_each_entry(client, &vm->ioreq_clients, list) {
390 read_lock_bh(&client->range_lock);
391 list_for_each_entry(range, &client->range_list, list) {
392 if (acrn_in_range(range, req)) {
397 read_unlock_bh(&client->range_lock);
401 return found ? found : vm->default_client;
405 * acrn_ioreq_client_create() - Create an ioreq client
406 * @vm: The VM that this client belongs to
407 * @handler: The ioreq_handler of ioreq client acrn_hsm will create a kernel
408 * thread and call the handler to handle I/O requests.
409 * @priv: Private data for the handler
410 * @is_default: If it is the default client
411 * @name: The name of ioreq client
413 * Return: acrn_ioreq_client pointer on success, NULL on error
415 struct acrn_ioreq_client *acrn_ioreq_client_create(struct acrn_vm *vm,
416 ioreq_handler_t handler,
417 void *priv, bool is_default,
420 struct acrn_ioreq_client *client;
422 if (!handler && !is_default) {
423 dev_dbg(acrn_dev.this_device,
424 "Cannot create non-default client w/o handler!\n");
427 client = kzalloc(sizeof(*client), GFP_KERNEL);
431 client->handler = handler;
434 client->is_default = is_default;
436 strncpy(client->name, name, sizeof(client->name) - 1);
437 rwlock_init(&client->range_lock);
438 INIT_LIST_HEAD(&client->range_list);
439 init_waitqueue_head(&client->wq);
441 if (client->handler) {
442 client->thread = kthread_run(ioreq_task, client, "VM%u-%s",
443 client->vm->vmid, client->name);
444 if (IS_ERR(client->thread)) {
450 spin_lock_bh(&vm->ioreq_clients_lock);
452 vm->default_client = client;
454 list_add(&client->list, &vm->ioreq_clients);
455 spin_unlock_bh(&vm->ioreq_clients_lock);
457 dev_dbg(acrn_dev.this_device, "Created ioreq client %s.\n", name);
462 * acrn_ioreq_client_destroy() - Destroy an ioreq client
463 * @client: The ioreq client
465 void acrn_ioreq_client_destroy(struct acrn_ioreq_client *client)
467 struct acrn_ioreq_range *range, *next;
468 struct acrn_vm *vm = client->vm;
470 dev_dbg(acrn_dev.this_device,
471 "Destroy ioreq client %s.\n", client->name);
473 set_bit(ACRN_IOREQ_CLIENT_DESTROYING, &client->flags);
474 if (client->is_default)
475 wake_up_interruptible(&client->wq);
477 kthread_stop(client->thread);
479 spin_lock_bh(&vm->ioreq_clients_lock);
480 if (client->is_default)
481 vm->default_client = NULL;
483 list_del(&client->list);
484 spin_unlock_bh(&vm->ioreq_clients_lock);
486 write_lock_bh(&client->range_lock);
487 list_for_each_entry_safe(range, next, &client->range_list, list) {
488 list_del(&range->list);
491 write_unlock_bh(&client->range_lock);
497 static int acrn_ioreq_dispatch(struct acrn_vm *vm)
499 struct acrn_ioreq_client *client;
500 struct acrn_io_request *req;
503 for (i = 0; i < vm->vcpu_num; i++) {
504 req = vm->ioreq_buf->req_slot + i;
506 /* barrier the read of processed of acrn_io_request */
507 if (smp_load_acquire(&req->processed) ==
508 ACRN_IOREQ_STATE_PENDING) {
509 /* Complete the IO request directly in clearing stage */
510 if (test_bit(ACRN_VM_FLAG_CLEARING_IOREQ, &vm->flags)) {
511 ioreq_complete_request(vm, i, req);
514 if (handle_cf8cfc(vm, req, i))
517 spin_lock_bh(&vm->ioreq_clients_lock);
518 client = find_ioreq_client(vm, req);
520 dev_err(acrn_dev.this_device,
521 "Failed to find ioreq client!\n");
522 spin_unlock_bh(&vm->ioreq_clients_lock);
525 if (!client->is_default)
526 req->kernel_handled = 1;
528 req->kernel_handled = 0;
530 * Add barrier() to make sure the writes are done
531 * before setting ACRN_IOREQ_STATE_PROCESSING
533 smp_store_release(&req->processed,
534 ACRN_IOREQ_STATE_PROCESSING);
535 set_bit(i, client->ioreqs_map);
536 wake_up_interruptible(&client->wq);
537 spin_unlock_bh(&vm->ioreq_clients_lock);
544 static void ioreq_dispatcher(struct work_struct *work)
548 read_lock(&acrn_vm_list_lock);
549 list_for_each_entry(vm, &acrn_vm_list, list) {
552 acrn_ioreq_dispatch(vm);
554 read_unlock(&acrn_vm_list_lock);
557 static void ioreq_intr_handler(void)
559 queue_work(ioreq_wq, &ioreq_work);
562 static void ioreq_pause(void)
564 /* Flush and unarm the handler to ensure no I/O requests pending */
565 acrn_remove_intr_handler();
566 drain_workqueue(ioreq_wq);
569 static void ioreq_resume(void)
571 /* Schedule after enabling in case other clients miss interrupt */
572 acrn_setup_intr_handler(ioreq_intr_handler);
573 queue_work(ioreq_wq, &ioreq_work);
576 int acrn_ioreq_intr_setup(void)
578 acrn_setup_intr_handler(ioreq_intr_handler);
579 ioreq_wq = alloc_ordered_workqueue("ioreq_wq",
580 WQ_HIGHPRI | WQ_MEM_RECLAIM);
582 dev_err(acrn_dev.this_device, "Failed to alloc workqueue!\n");
583 acrn_remove_intr_handler();
589 void acrn_ioreq_intr_remove(void)
592 destroy_workqueue(ioreq_wq);
593 acrn_remove_intr_handler();
596 int acrn_ioreq_init(struct acrn_vm *vm, u64 buf_vma)
598 struct acrn_ioreq_buffer *set_buffer;
605 set_buffer = kzalloc(sizeof(*set_buffer), GFP_KERNEL);
609 ret = pin_user_pages_fast(buf_vma, 1,
610 FOLL_WRITE | FOLL_LONGTERM, &page);
611 if (unlikely(ret != 1) || !page) {
612 dev_err(acrn_dev.this_device, "Failed to pin ioreq page!\n");
617 vm->ioreq_buf = page_address(page);
618 vm->ioreq_page = page;
619 set_buffer->ioreq_buf = page_to_phys(page);
620 ret = hcall_set_ioreq_buffer(vm->vmid, virt_to_phys(set_buffer));
622 dev_err(acrn_dev.this_device, "Failed to init ioreq buffer!\n");
623 unpin_user_page(page);
624 vm->ioreq_buf = NULL;
628 dev_dbg(acrn_dev.this_device,
629 "Init ioreq buffer %pK!\n", vm->ioreq_buf);
636 void acrn_ioreq_deinit(struct acrn_vm *vm)
638 struct acrn_ioreq_client *client, *next;
640 dev_dbg(acrn_dev.this_device,
641 "Deinit ioreq buffer %pK!\n", vm->ioreq_buf);
642 /* Destroy all clients belonging to this VM */
643 list_for_each_entry_safe(client, next, &vm->ioreq_clients, list)
644 acrn_ioreq_client_destroy(client);
645 if (vm->default_client)
646 acrn_ioreq_client_destroy(vm->default_client);
648 if (vm->ioreq_buf && vm->ioreq_page) {
649 unpin_user_page(vm->ioreq_page);
650 vm->ioreq_buf = NULL;