1 // SPDX-License-Identifier: GPL-2.0-only
3 * Mediated virtual PCI serial host device driver
5 * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
6 * Author: Neo Jia <cjia@nvidia.com>
7 * Kirti Wankhede <kwankhede@nvidia.com>
9 * Sample driver that creates mdev device that simulates serial port over PCI
13 #include <linux/init.h>
14 #include <linux/module.h>
15 #include <linux/kernel.h>
17 #include <linux/poll.h>
18 #include <linux/slab.h>
19 #include <linux/cdev.h>
20 #include <linux/sched.h>
21 #include <linux/wait.h>
22 #include <linux/vfio.h>
23 #include <linux/iommu.h>
24 #include <linux/sysfs.h>
25 #include <linux/ctype.h>
26 #include <linux/file.h>
27 #include <linux/mdev.h>
28 #include <linux/pci.h>
29 #include <linux/serial.h>
30 #include <uapi/linux/serial_reg.h>
31 #include <linux/eventfd.h>
36 #define VERSION_STRING "0.1"
37 #define DRIVER_AUTHOR "NVIDIA Corporation"
39 #define MTTY_CLASS_NAME "mtty"
41 #define MTTY_NAME "mtty"
43 #define MTTY_STRING_LEN 16
45 #define MTTY_CONFIG_SPACE_SIZE 0xff
46 #define MTTY_IO_BAR_SIZE 0x8
47 #define MTTY_MMIO_BAR_SIZE 0x100000
49 #define STORE_LE16(addr, val) (*(u16 *)addr = val)
50 #define STORE_LE32(addr, val) (*(u32 *)addr = val)
52 #define MAX_FIFO_SIZE 16
54 #define CIRCULAR_BUF_INC_IDX(idx) (idx = (idx + 1) & (MAX_FIFO_SIZE - 1))
56 #define MTTY_VFIO_PCI_OFFSET_SHIFT 40
58 #define MTTY_VFIO_PCI_OFFSET_TO_INDEX(off) (off >> MTTY_VFIO_PCI_OFFSET_SHIFT)
59 #define MTTY_VFIO_PCI_INDEX_TO_OFFSET(index) \
60 ((u64)(index) << MTTY_VFIO_PCI_OFFSET_SHIFT)
61 #define MTTY_VFIO_PCI_OFFSET_MASK \
62 (((u64)(1) << MTTY_VFIO_PCI_OFFSET_SHIFT) - 1)
69 static struct mtty_dev {
71 struct class *vd_class;
75 struct mdev_parent parent;
78 struct mdev_region_info {
85 #if defined(DEBUG_REGS)
86 static const char *wr_reg[] = {
97 static const char *rd_reg[] = {
109 /* loop back buffer */
111 u8 fifo[MAX_FIFO_SIZE];
117 u8 uart_reg[8]; /* 8 registers */
118 struct rxtx rxtx; /* loop back buffer */
122 u8 fcr; /* FIFO control register */
124 u8 intr_trigger_level; /* interrupt trigger level */
127 /* State of each mdev device */
129 struct vfio_device vdev;
130 struct eventfd_ctx *intx_evtfd;
131 struct eventfd_ctx *msi_evtfd;
134 struct mutex ops_lock;
135 struct mdev_device *mdev;
136 struct mdev_region_info region_info[VFIO_PCI_NUM_REGIONS];
137 u32 bar_mask[VFIO_PCI_NUM_REGIONS];
138 struct list_head next;
139 struct serial_port s[2];
140 struct mutex rxtx_lock;
141 struct vfio_device_info dev_info;
146 static struct mtty_type {
147 struct mdev_type type;
150 { .nr_ports = 1, .type.sysfs_name = "1",
151 .type.pretty_name = "Single port serial" },
152 { .nr_ports = 2, .type.sysfs_name = "2",
153 .type.pretty_name = "Dual port serial" },
156 static struct mdev_type *mtty_mdev_types[] = {
161 static atomic_t mdev_avail_ports = ATOMIC_INIT(MAX_MTTYS);
163 static const struct file_operations vd_fops = {
164 .owner = THIS_MODULE,
167 static const struct vfio_device_ops mtty_dev_ops;
169 /* Helper functions */
171 static void dump_buffer(u8 *buf, uint32_t count)
176 pr_info("Buffer:\n");
177 for (i = 0; i < count; i++) {
178 pr_info("%2x ", *(buf + i));
179 if ((i + 1) % 16 == 0)
185 static bool is_intx(struct mdev_state *mdev_state)
187 return mdev_state->irq_index == VFIO_PCI_INTX_IRQ_INDEX;
190 static bool is_msi(struct mdev_state *mdev_state)
192 return mdev_state->irq_index == VFIO_PCI_MSI_IRQ_INDEX;
195 static bool is_noirq(struct mdev_state *mdev_state)
197 return !is_intx(mdev_state) && !is_msi(mdev_state);
200 static void mtty_trigger_interrupt(struct mdev_state *mdev_state)
202 lockdep_assert_held(&mdev_state->ops_lock);
204 if (is_msi(mdev_state)) {
205 if (mdev_state->msi_evtfd)
206 eventfd_signal(mdev_state->msi_evtfd, 1);
207 } else if (is_intx(mdev_state)) {
208 if (mdev_state->intx_evtfd && !mdev_state->intx_mask) {
209 eventfd_signal(mdev_state->intx_evtfd, 1);
210 mdev_state->intx_mask = true;
215 static void mtty_create_config_space(struct mdev_state *mdev_state)
218 STORE_LE32((u32 *) &mdev_state->vconfig[0x0], 0x32534348);
220 /* Control: I/O+, Mem-, BusMaster- */
221 STORE_LE16((u16 *) &mdev_state->vconfig[0x4], 0x0001);
223 /* Status: capabilities list absent */
224 STORE_LE16((u16 *) &mdev_state->vconfig[0x6], 0x0200);
227 mdev_state->vconfig[0x8] = 0x10;
229 /* programming interface class : 16550-compatible serial controller */
230 mdev_state->vconfig[0x9] = 0x02;
233 mdev_state->vconfig[0xa] = 0x00;
235 /* Base class : Simple Communication controllers */
236 mdev_state->vconfig[0xb] = 0x07;
238 /* base address registers */
240 STORE_LE32((u32 *) &mdev_state->vconfig[0x10], 0x000001);
241 mdev_state->bar_mask[0] = ~(MTTY_IO_BAR_SIZE) + 1;
243 if (mdev_state->nr_ports == 2) {
245 STORE_LE32((u32 *) &mdev_state->vconfig[0x14], 0x000001);
246 mdev_state->bar_mask[1] = ~(MTTY_IO_BAR_SIZE) + 1;
250 STORE_LE32((u32 *) &mdev_state->vconfig[0x2c], 0x32534348);
252 mdev_state->vconfig[0x34] = 0x00; /* Cap Ptr */
253 mdev_state->vconfig[0x3d] = 0x01; /* interrupt pin (INTA#) */
255 /* Vendor specific data */
256 mdev_state->vconfig[0x40] = 0x23;
257 mdev_state->vconfig[0x43] = 0x80;
258 mdev_state->vconfig[0x44] = 0x23;
259 mdev_state->vconfig[0x48] = 0x23;
260 mdev_state->vconfig[0x4c] = 0x23;
262 mdev_state->vconfig[0x60] = 0x50;
263 mdev_state->vconfig[0x61] = 0x43;
264 mdev_state->vconfig[0x62] = 0x49;
265 mdev_state->vconfig[0x63] = 0x20;
266 mdev_state->vconfig[0x64] = 0x53;
267 mdev_state->vconfig[0x65] = 0x65;
268 mdev_state->vconfig[0x66] = 0x72;
269 mdev_state->vconfig[0x67] = 0x69;
270 mdev_state->vconfig[0x68] = 0x61;
271 mdev_state->vconfig[0x69] = 0x6c;
272 mdev_state->vconfig[0x6a] = 0x2f;
273 mdev_state->vconfig[0x6b] = 0x55;
274 mdev_state->vconfig[0x6c] = 0x41;
275 mdev_state->vconfig[0x6d] = 0x52;
276 mdev_state->vconfig[0x6e] = 0x54;
279 static void handle_pci_cfg_write(struct mdev_state *mdev_state, u16 offset,
282 u32 cfg_addr, bar_mask, bar_index = 0;
285 case 0x04: /* device control */
286 case 0x06: /* device status */
289 case 0x3c: /* interrupt line */
290 mdev_state->vconfig[0x3c] = buf[0];
294 * Interrupt Pin is hardwired to INTA.
295 * This field is write protected by hardware
298 case 0x10: /* BAR0 */
299 case 0x14: /* BAR1 */
302 else if (offset == 0x14)
305 if ((mdev_state->nr_ports == 1) && (bar_index == 1)) {
306 STORE_LE32(&mdev_state->vconfig[offset], 0);
310 cfg_addr = *(u32 *)buf;
311 pr_info("BAR%d addr 0x%x\n", bar_index, cfg_addr);
313 if (cfg_addr == 0xffffffff) {
314 bar_mask = mdev_state->bar_mask[bar_index];
315 cfg_addr = (cfg_addr & bar_mask);
318 cfg_addr |= (mdev_state->vconfig[offset] & 0x3ul);
319 STORE_LE32(&mdev_state->vconfig[offset], cfg_addr);
321 case 0x18: /* BAR2 */
322 case 0x1c: /* BAR3 */
323 case 0x20: /* BAR4 */
324 STORE_LE32(&mdev_state->vconfig[offset], 0);
327 pr_info("PCI config write @0x%x of %d bytes not handled\n",
333 static void handle_bar_write(unsigned int index, struct mdev_state *mdev_state,
334 u16 offset, u8 *buf, u32 count)
338 /* Handle data written by guest */
341 /* if DLAB set, data is LSB of divisor */
342 if (mdev_state->s[index].dlab) {
343 mdev_state->s[index].divisor |= data;
347 mutex_lock(&mdev_state->rxtx_lock);
349 /* save in TX buffer */
350 if (mdev_state->s[index].rxtx.count <
351 mdev_state->s[index].max_fifo_size) {
352 mdev_state->s[index].rxtx.fifo[
353 mdev_state->s[index].rxtx.head] = data;
354 mdev_state->s[index].rxtx.count++;
355 CIRCULAR_BUF_INC_IDX(mdev_state->s[index].rxtx.head);
356 mdev_state->s[index].overrun = false;
359 * Trigger interrupt if receive data interrupt is
360 * enabled and fifo reached trigger level
362 if ((mdev_state->s[index].uart_reg[UART_IER] &
364 (mdev_state->s[index].rxtx.count ==
365 mdev_state->s[index].intr_trigger_level)) {
366 /* trigger interrupt */
367 #if defined(DEBUG_INTR)
368 pr_err("Serial port %d: Fifo level trigger\n",
371 mtty_trigger_interrupt(mdev_state);
374 #if defined(DEBUG_INTR)
375 pr_err("Serial port %d: Buffer Overflow\n", index);
377 mdev_state->s[index].overrun = true;
380 * Trigger interrupt if receiver line status interrupt
383 if (mdev_state->s[index].uart_reg[UART_IER] &
385 mtty_trigger_interrupt(mdev_state);
387 mutex_unlock(&mdev_state->rxtx_lock);
391 /* if DLAB set, data is MSB of divisor */
392 if (mdev_state->s[index].dlab)
393 mdev_state->s[index].divisor |= (u16)data << 8;
395 mdev_state->s[index].uart_reg[offset] = data;
396 mutex_lock(&mdev_state->rxtx_lock);
397 if ((data & UART_IER_THRI) &&
398 (mdev_state->s[index].rxtx.head ==
399 mdev_state->s[index].rxtx.tail)) {
400 #if defined(DEBUG_INTR)
401 pr_err("Serial port %d: IER_THRI write\n",
404 mtty_trigger_interrupt(mdev_state);
407 mutex_unlock(&mdev_state->rxtx_lock);
413 mdev_state->s[index].fcr = data;
415 mutex_lock(&mdev_state->rxtx_lock);
416 if (data & (UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT)) {
417 /* clear loop back FIFO */
418 mdev_state->s[index].rxtx.count = 0;
419 mdev_state->s[index].rxtx.head = 0;
420 mdev_state->s[index].rxtx.tail = 0;
422 mutex_unlock(&mdev_state->rxtx_lock);
424 switch (data & UART_FCR_TRIGGER_MASK) {
425 case UART_FCR_TRIGGER_1:
426 mdev_state->s[index].intr_trigger_level = 1;
429 case UART_FCR_TRIGGER_4:
430 mdev_state->s[index].intr_trigger_level = 4;
433 case UART_FCR_TRIGGER_8:
434 mdev_state->s[index].intr_trigger_level = 8;
437 case UART_FCR_TRIGGER_14:
438 mdev_state->s[index].intr_trigger_level = 14;
443 * Set trigger level to 1 otherwise or implement timer with
444 * timeout of 4 characters and on expiring that timer set
445 * Recevice data timeout in IIR register
447 mdev_state->s[index].intr_trigger_level = 1;
448 if (data & UART_FCR_ENABLE_FIFO)
449 mdev_state->s[index].max_fifo_size = MAX_FIFO_SIZE;
451 mdev_state->s[index].max_fifo_size = 1;
452 mdev_state->s[index].intr_trigger_level = 1;
458 if (data & UART_LCR_DLAB) {
459 mdev_state->s[index].dlab = true;
460 mdev_state->s[index].divisor = 0;
462 mdev_state->s[index].dlab = false;
464 mdev_state->s[index].uart_reg[offset] = data;
468 mdev_state->s[index].uart_reg[offset] = data;
470 if ((mdev_state->s[index].uart_reg[UART_IER] & UART_IER_MSI) &&
471 (data & UART_MCR_OUT2)) {
472 #if defined(DEBUG_INTR)
473 pr_err("Serial port %d: MCR_OUT2 write\n", index);
475 mtty_trigger_interrupt(mdev_state);
478 if ((mdev_state->s[index].uart_reg[UART_IER] & UART_IER_MSI) &&
479 (data & (UART_MCR_RTS | UART_MCR_DTR))) {
480 #if defined(DEBUG_INTR)
481 pr_err("Serial port %d: MCR RTS/DTR write\n", index);
483 mtty_trigger_interrupt(mdev_state);
493 mdev_state->s[index].uart_reg[offset] = data;
501 static void handle_bar_read(unsigned int index, struct mdev_state *mdev_state,
502 u16 offset, u8 *buf, u32 count)
504 /* Handle read requests by guest */
507 /* if DLAB set, data is LSB of divisor */
508 if (mdev_state->s[index].dlab) {
509 *buf = (u8)mdev_state->s[index].divisor;
513 mutex_lock(&mdev_state->rxtx_lock);
514 /* return data in tx buffer */
515 if (mdev_state->s[index].rxtx.head !=
516 mdev_state->s[index].rxtx.tail) {
517 *buf = mdev_state->s[index].rxtx.fifo[
518 mdev_state->s[index].rxtx.tail];
519 mdev_state->s[index].rxtx.count--;
520 CIRCULAR_BUF_INC_IDX(mdev_state->s[index].rxtx.tail);
523 if (mdev_state->s[index].rxtx.head ==
524 mdev_state->s[index].rxtx.tail) {
526 * Trigger interrupt if tx buffer empty interrupt is
527 * enabled and fifo is empty
529 #if defined(DEBUG_INTR)
530 pr_err("Serial port %d: Buffer Empty\n", index);
532 if (mdev_state->s[index].uart_reg[UART_IER] &
534 mtty_trigger_interrupt(mdev_state);
536 mutex_unlock(&mdev_state->rxtx_lock);
541 if (mdev_state->s[index].dlab) {
542 *buf = (u8)(mdev_state->s[index].divisor >> 8);
545 *buf = mdev_state->s[index].uart_reg[offset] & 0x0f;
550 u8 ier = mdev_state->s[index].uart_reg[UART_IER];
553 mutex_lock(&mdev_state->rxtx_lock);
554 /* Interrupt priority 1: Parity, overrun, framing or break */
555 if ((ier & UART_IER_RLSI) && mdev_state->s[index].overrun)
556 *buf |= UART_IIR_RLSI;
558 /* Interrupt priority 2: Fifo trigger level reached */
559 if ((ier & UART_IER_RDI) &&
560 (mdev_state->s[index].rxtx.count >=
561 mdev_state->s[index].intr_trigger_level))
562 *buf |= UART_IIR_RDI;
564 /* Interrupt priotiry 3: transmitter holding register empty */
565 if ((ier & UART_IER_THRI) &&
566 (mdev_state->s[index].rxtx.head ==
567 mdev_state->s[index].rxtx.tail))
568 *buf |= UART_IIR_THRI;
570 /* Interrupt priotiry 4: Modem status: CTS, DSR, RI or DCD */
571 if ((ier & UART_IER_MSI) &&
572 (mdev_state->s[index].uart_reg[UART_MCR] &
573 (UART_MCR_RTS | UART_MCR_DTR)))
574 *buf |= UART_IIR_MSI;
576 /* bit0: 0=> interrupt pending, 1=> no interrupt is pending */
578 *buf = UART_IIR_NO_INT;
580 /* set bit 6 & 7 to be 16550 compatible */
582 mutex_unlock(&mdev_state->rxtx_lock);
588 *buf = mdev_state->s[index].uart_reg[offset];
595 mutex_lock(&mdev_state->rxtx_lock);
596 /* atleast one char in FIFO */
597 if (mdev_state->s[index].rxtx.head !=
598 mdev_state->s[index].rxtx.tail)
601 /* if FIFO overrun */
602 if (mdev_state->s[index].overrun)
605 /* transmit FIFO empty and tramsitter empty */
606 if (mdev_state->s[index].rxtx.head ==
607 mdev_state->s[index].rxtx.tail)
608 lsr |= UART_LSR_TEMT | UART_LSR_THRE;
610 mutex_unlock(&mdev_state->rxtx_lock);
615 *buf = UART_MSR_DSR | UART_MSR_DDSR | UART_MSR_DCD;
617 mutex_lock(&mdev_state->rxtx_lock);
618 /* if AFE is 1 and FIFO have space, set CTS bit */
619 if (mdev_state->s[index].uart_reg[UART_MCR] &
621 if (mdev_state->s[index].rxtx.count <
622 mdev_state->s[index].max_fifo_size)
623 *buf |= UART_MSR_CTS | UART_MSR_DCTS;
625 *buf |= UART_MSR_CTS | UART_MSR_DCTS;
626 mutex_unlock(&mdev_state->rxtx_lock);
631 *buf = mdev_state->s[index].uart_reg[offset];
639 static void mdev_read_base(struct mdev_state *mdev_state)
642 u32 start_lo, start_hi;
645 pos = PCI_BASE_ADDRESS_0;
647 for (index = 0; index <= VFIO_PCI_BAR5_REGION_INDEX; index++) {
649 if (!mdev_state->region_info[index].size)
652 start_lo = (*(u32 *)(mdev_state->vconfig + pos)) &
653 PCI_BASE_ADDRESS_MEM_MASK;
654 mem_type = (*(u32 *)(mdev_state->vconfig + pos)) &
655 PCI_BASE_ADDRESS_MEM_TYPE_MASK;
658 case PCI_BASE_ADDRESS_MEM_TYPE_64:
659 start_hi = (*(u32 *)(mdev_state->vconfig + pos + 4));
662 case PCI_BASE_ADDRESS_MEM_TYPE_32:
663 case PCI_BASE_ADDRESS_MEM_TYPE_1M:
664 /* 1M mem BAR treated as 32-bit BAR */
666 /* mem unknown type treated as 32-bit BAR */
671 mdev_state->region_info[index].start = ((u64)start_hi << 32) |
676 static ssize_t mdev_access(struct mdev_state *mdev_state, u8 *buf, size_t count,
677 loff_t pos, bool is_write)
686 mutex_lock(&mdev_state->ops_lock);
688 index = MTTY_VFIO_PCI_OFFSET_TO_INDEX(pos);
689 offset = pos & MTTY_VFIO_PCI_OFFSET_MASK;
691 case VFIO_PCI_CONFIG_REGION_INDEX:
694 pr_info("%s: PCI config space %s at offset 0x%llx\n",
695 __func__, is_write ? "write" : "read", offset);
698 dump_buffer(buf, count);
699 handle_pci_cfg_write(mdev_state, offset, buf, count);
701 memcpy(buf, (mdev_state->vconfig + offset), count);
702 dump_buffer(buf, count);
707 case VFIO_PCI_BAR0_REGION_INDEX ... VFIO_PCI_BAR5_REGION_INDEX:
708 if (!mdev_state->region_info[index].start)
709 mdev_read_base(mdev_state);
712 dump_buffer(buf, count);
714 #if defined(DEBUG_REGS)
715 pr_info("%s: BAR%d WR @0x%llx %s val:0x%02x dlab:%d\n",
716 __func__, index, offset, wr_reg[offset],
717 *buf, mdev_state->s[index].dlab);
719 handle_bar_write(index, mdev_state, offset, buf, count);
721 handle_bar_read(index, mdev_state, offset, buf, count);
722 dump_buffer(buf, count);
724 #if defined(DEBUG_REGS)
725 pr_info("%s: BAR%d RD @0x%llx %s val:0x%02x dlab:%d\n",
726 __func__, index, offset, rd_reg[offset],
727 *buf, mdev_state->s[index].dlab);
741 mutex_unlock(&mdev_state->ops_lock);
746 static int mtty_init_dev(struct vfio_device *vdev)
748 struct mdev_state *mdev_state =
749 container_of(vdev, struct mdev_state, vdev);
750 struct mdev_device *mdev = to_mdev_device(vdev->dev);
751 struct mtty_type *type =
752 container_of(mdev->type, struct mtty_type, type);
753 int avail_ports = atomic_read(&mdev_avail_ports);
757 if (avail_ports < type->nr_ports)
759 } while (!atomic_try_cmpxchg(&mdev_avail_ports,
761 avail_ports - type->nr_ports));
763 mdev_state->nr_ports = type->nr_ports;
764 mdev_state->irq_index = -1;
765 mdev_state->s[0].max_fifo_size = MAX_FIFO_SIZE;
766 mdev_state->s[1].max_fifo_size = MAX_FIFO_SIZE;
767 mutex_init(&mdev_state->rxtx_lock);
769 mdev_state->vconfig = kzalloc(MTTY_CONFIG_SPACE_SIZE, GFP_KERNEL);
770 if (!mdev_state->vconfig) {
775 mutex_init(&mdev_state->ops_lock);
776 mdev_state->mdev = mdev;
777 mtty_create_config_space(mdev_state);
781 atomic_add(type->nr_ports, &mdev_avail_ports);
785 static int mtty_probe(struct mdev_device *mdev)
787 struct mdev_state *mdev_state;
790 mdev_state = vfio_alloc_device(mdev_state, vdev, &mdev->dev,
792 if (IS_ERR(mdev_state))
793 return PTR_ERR(mdev_state);
795 ret = vfio_register_emulated_iommu_dev(&mdev_state->vdev);
798 dev_set_drvdata(&mdev->dev, mdev_state);
802 vfio_put_device(&mdev_state->vdev);
806 static void mtty_release_dev(struct vfio_device *vdev)
808 struct mdev_state *mdev_state =
809 container_of(vdev, struct mdev_state, vdev);
811 atomic_add(mdev_state->nr_ports, &mdev_avail_ports);
812 kfree(mdev_state->vconfig);
815 static void mtty_remove(struct mdev_device *mdev)
817 struct mdev_state *mdev_state = dev_get_drvdata(&mdev->dev);
819 vfio_unregister_group_dev(&mdev_state->vdev);
820 vfio_put_device(&mdev_state->vdev);
823 static int mtty_reset(struct mdev_state *mdev_state)
825 pr_info("%s: called\n", __func__);
830 static ssize_t mtty_read(struct vfio_device *vdev, char __user *buf,
831 size_t count, loff_t *ppos)
833 struct mdev_state *mdev_state =
834 container_of(vdev, struct mdev_state, vdev);
835 unsigned int done = 0;
841 if (count >= 4 && !(*ppos % 4)) {
844 ret = mdev_access(mdev_state, (u8 *)&val, sizeof(val),
849 if (copy_to_user(buf, &val, sizeof(val)))
853 } else if (count >= 2 && !(*ppos % 2)) {
856 ret = mdev_access(mdev_state, (u8 *)&val, sizeof(val),
861 if (copy_to_user(buf, &val, sizeof(val)))
868 ret = mdev_access(mdev_state, (u8 *)&val, sizeof(val),
873 if (copy_to_user(buf, &val, sizeof(val)))
891 static ssize_t mtty_write(struct vfio_device *vdev, const char __user *buf,
892 size_t count, loff_t *ppos)
894 struct mdev_state *mdev_state =
895 container_of(vdev, struct mdev_state, vdev);
896 unsigned int done = 0;
902 if (count >= 4 && !(*ppos % 4)) {
905 if (copy_from_user(&val, buf, sizeof(val)))
908 ret = mdev_access(mdev_state, (u8 *)&val, sizeof(val),
914 } else if (count >= 2 && !(*ppos % 2)) {
917 if (copy_from_user(&val, buf, sizeof(val)))
920 ret = mdev_access(mdev_state, (u8 *)&val, sizeof(val),
929 if (copy_from_user(&val, buf, sizeof(val)))
932 ret = mdev_access(mdev_state, (u8 *)&val, sizeof(val),
950 static void mtty_disable_intx(struct mdev_state *mdev_state)
952 if (mdev_state->intx_evtfd) {
953 eventfd_ctx_put(mdev_state->intx_evtfd);
954 mdev_state->intx_evtfd = NULL;
955 mdev_state->intx_mask = false;
956 mdev_state->irq_index = -1;
960 static void mtty_disable_msi(struct mdev_state *mdev_state)
962 if (mdev_state->msi_evtfd) {
963 eventfd_ctx_put(mdev_state->msi_evtfd);
964 mdev_state->msi_evtfd = NULL;
965 mdev_state->irq_index = -1;
969 static int mtty_set_irqs(struct mdev_state *mdev_state, uint32_t flags,
970 unsigned int index, unsigned int start,
971 unsigned int count, void *data)
975 mutex_lock(&mdev_state->ops_lock);
977 case VFIO_PCI_INTX_IRQ_INDEX:
978 switch (flags & VFIO_IRQ_SET_ACTION_TYPE_MASK) {
979 case VFIO_IRQ_SET_ACTION_MASK:
980 if (!is_intx(mdev_state) || start != 0 || count != 1) {
985 if (flags & VFIO_IRQ_SET_DATA_NONE) {
986 mdev_state->intx_mask = true;
987 } else if (flags & VFIO_IRQ_SET_DATA_BOOL) {
988 uint8_t mask = *(uint8_t *)data;
991 mdev_state->intx_mask = true;
992 } else if (flags & VFIO_IRQ_SET_DATA_EVENTFD) {
993 ret = -ENOTTY; /* No support for mask fd */
996 case VFIO_IRQ_SET_ACTION_UNMASK:
997 if (!is_intx(mdev_state) || start != 0 || count != 1) {
1002 if (flags & VFIO_IRQ_SET_DATA_NONE) {
1003 mdev_state->intx_mask = false;
1004 } else if (flags & VFIO_IRQ_SET_DATA_BOOL) {
1005 uint8_t mask = *(uint8_t *)data;
1008 mdev_state->intx_mask = false;
1009 } else if (flags & VFIO_IRQ_SET_DATA_EVENTFD) {
1010 ret = -ENOTTY; /* No support for unmask fd */
1013 case VFIO_IRQ_SET_ACTION_TRIGGER:
1014 if (is_intx(mdev_state) && !count &&
1015 (flags & VFIO_IRQ_SET_DATA_NONE)) {
1016 mtty_disable_intx(mdev_state);
1020 if (!(is_intx(mdev_state) || is_noirq(mdev_state)) ||
1021 start != 0 || count != 1) {
1026 if (flags & VFIO_IRQ_SET_DATA_EVENTFD) {
1027 int fd = *(int *)data;
1028 struct eventfd_ctx *evt;
1030 mtty_disable_intx(mdev_state);
1035 evt = eventfd_ctx_fdget(fd);
1040 mdev_state->intx_evtfd = evt;
1041 mdev_state->irq_index = index;
1045 if (!is_intx(mdev_state)) {
1050 if (flags & VFIO_IRQ_SET_DATA_NONE) {
1051 mtty_trigger_interrupt(mdev_state);
1052 } else if (flags & VFIO_IRQ_SET_DATA_BOOL) {
1053 uint8_t trigger = *(uint8_t *)data;
1056 mtty_trigger_interrupt(mdev_state);
1061 case VFIO_PCI_MSI_IRQ_INDEX:
1062 switch (flags & VFIO_IRQ_SET_ACTION_TYPE_MASK) {
1063 case VFIO_IRQ_SET_ACTION_MASK:
1064 case VFIO_IRQ_SET_ACTION_UNMASK:
1067 case VFIO_IRQ_SET_ACTION_TRIGGER:
1068 if (is_msi(mdev_state) && !count &&
1069 (flags & VFIO_IRQ_SET_DATA_NONE)) {
1070 mtty_disable_msi(mdev_state);
1074 if (!(is_msi(mdev_state) || is_noirq(mdev_state)) ||
1075 start != 0 || count != 1) {
1080 if (flags & VFIO_IRQ_SET_DATA_EVENTFD) {
1081 int fd = *(int *)data;
1082 struct eventfd_ctx *evt;
1084 mtty_disable_msi(mdev_state);
1089 evt = eventfd_ctx_fdget(fd);
1094 mdev_state->msi_evtfd = evt;
1095 mdev_state->irq_index = index;
1099 if (!is_msi(mdev_state)) {
1104 if (flags & VFIO_IRQ_SET_DATA_NONE) {
1105 mtty_trigger_interrupt(mdev_state);
1106 } else if (flags & VFIO_IRQ_SET_DATA_BOOL) {
1107 uint8_t trigger = *(uint8_t *)data;
1110 mtty_trigger_interrupt(mdev_state);
1115 case VFIO_PCI_MSIX_IRQ_INDEX:
1116 dev_dbg(mdev_state->vdev.dev, "%s: MSIX_IRQ\n", __func__);
1119 case VFIO_PCI_ERR_IRQ_INDEX:
1120 dev_dbg(mdev_state->vdev.dev, "%s: ERR_IRQ\n", __func__);
1123 case VFIO_PCI_REQ_IRQ_INDEX:
1124 dev_dbg(mdev_state->vdev.dev, "%s: REQ_IRQ\n", __func__);
1129 mutex_unlock(&mdev_state->ops_lock);
1133 static int mtty_get_region_info(struct mdev_state *mdev_state,
1134 struct vfio_region_info *region_info,
1135 u16 *cap_type_id, void **cap_type)
1137 unsigned int size = 0;
1140 bar_index = region_info->index;
1141 if (bar_index >= VFIO_PCI_NUM_REGIONS)
1144 mutex_lock(&mdev_state->ops_lock);
1146 switch (bar_index) {
1147 case VFIO_PCI_CONFIG_REGION_INDEX:
1148 size = MTTY_CONFIG_SPACE_SIZE;
1150 case VFIO_PCI_BAR0_REGION_INDEX:
1151 size = MTTY_IO_BAR_SIZE;
1153 case VFIO_PCI_BAR1_REGION_INDEX:
1154 if (mdev_state->nr_ports == 2)
1155 size = MTTY_IO_BAR_SIZE;
1162 mdev_state->region_info[bar_index].size = size;
1163 mdev_state->region_info[bar_index].vfio_offset =
1164 MTTY_VFIO_PCI_INDEX_TO_OFFSET(bar_index);
1166 region_info->size = size;
1167 region_info->offset = MTTY_VFIO_PCI_INDEX_TO_OFFSET(bar_index);
1168 region_info->flags = VFIO_REGION_INFO_FLAG_READ |
1169 VFIO_REGION_INFO_FLAG_WRITE;
1170 mutex_unlock(&mdev_state->ops_lock);
1174 static int mtty_get_irq_info(struct vfio_irq_info *irq_info)
1176 if (irq_info->index != VFIO_PCI_INTX_IRQ_INDEX &&
1177 irq_info->index != VFIO_PCI_MSI_IRQ_INDEX)
1180 irq_info->flags = VFIO_IRQ_INFO_EVENTFD;
1181 irq_info->count = 1;
1183 if (irq_info->index == VFIO_PCI_INTX_IRQ_INDEX)
1184 irq_info->flags |= VFIO_IRQ_INFO_MASKABLE |
1185 VFIO_IRQ_INFO_AUTOMASKED;
1187 irq_info->flags |= VFIO_IRQ_INFO_NORESIZE;
1192 static int mtty_get_device_info(struct vfio_device_info *dev_info)
1194 dev_info->flags = VFIO_DEVICE_FLAGS_PCI;
1195 dev_info->num_regions = VFIO_PCI_NUM_REGIONS;
1196 dev_info->num_irqs = VFIO_PCI_NUM_IRQS;
1201 static long mtty_ioctl(struct vfio_device *vdev, unsigned int cmd,
1204 struct mdev_state *mdev_state =
1205 container_of(vdev, struct mdev_state, vdev);
1207 unsigned long minsz;
1210 case VFIO_DEVICE_GET_INFO:
1212 struct vfio_device_info info;
1214 minsz = offsetofend(struct vfio_device_info, num_irqs);
1216 if (copy_from_user(&info, (void __user *)arg, minsz))
1219 if (info.argsz < minsz)
1222 ret = mtty_get_device_info(&info);
1226 memcpy(&mdev_state->dev_info, &info, sizeof(info));
1228 if (copy_to_user((void __user *)arg, &info, minsz))
1233 case VFIO_DEVICE_GET_REGION_INFO:
1235 struct vfio_region_info info;
1236 u16 cap_type_id = 0;
1237 void *cap_type = NULL;
1239 minsz = offsetofend(struct vfio_region_info, offset);
1241 if (copy_from_user(&info, (void __user *)arg, minsz))
1244 if (info.argsz < minsz)
1247 ret = mtty_get_region_info(mdev_state, &info, &cap_type_id,
1252 if (copy_to_user((void __user *)arg, &info, minsz))
1258 case VFIO_DEVICE_GET_IRQ_INFO:
1260 struct vfio_irq_info info;
1262 minsz = offsetofend(struct vfio_irq_info, count);
1264 if (copy_from_user(&info, (void __user *)arg, minsz))
1267 if ((info.argsz < minsz) ||
1268 (info.index >= mdev_state->dev_info.num_irqs))
1271 ret = mtty_get_irq_info(&info);
1275 if (copy_to_user((void __user *)arg, &info, minsz))
1280 case VFIO_DEVICE_SET_IRQS:
1282 struct vfio_irq_set hdr;
1283 u8 *data = NULL, *ptr = NULL;
1284 size_t data_size = 0;
1286 minsz = offsetofend(struct vfio_irq_set, count);
1288 if (copy_from_user(&hdr, (void __user *)arg, minsz))
1291 ret = vfio_set_irqs_validate_and_prepare(&hdr,
1292 mdev_state->dev_info.num_irqs,
1299 ptr = data = memdup_user((void __user *)(arg + minsz),
1302 return PTR_ERR(data);
1305 ret = mtty_set_irqs(mdev_state, hdr.flags, hdr.index, hdr.start,
1311 case VFIO_DEVICE_RESET:
1312 return mtty_reset(mdev_state);
1318 sample_mdev_dev_show(struct device *dev, struct device_attribute *attr,
1321 return sprintf(buf, "This is MDEV %s\n", dev_name(dev));
1324 static DEVICE_ATTR_RO(sample_mdev_dev);
1326 static struct attribute *mdev_dev_attrs[] = {
1327 &dev_attr_sample_mdev_dev.attr,
1331 static const struct attribute_group mdev_dev_group = {
1333 .attrs = mdev_dev_attrs,
1336 static const struct attribute_group *mdev_dev_groups[] = {
1341 static unsigned int mtty_get_available(struct mdev_type *mtype)
1343 struct mtty_type *type = container_of(mtype, struct mtty_type, type);
1345 return atomic_read(&mdev_avail_ports) / type->nr_ports;
1348 static void mtty_close(struct vfio_device *vdev)
1350 struct mdev_state *mdev_state =
1351 container_of(vdev, struct mdev_state, vdev);
1353 mtty_disable_intx(mdev_state);
1354 mtty_disable_msi(mdev_state);
1357 static const struct vfio_device_ops mtty_dev_ops = {
1358 .name = "vfio-mtty",
1359 .init = mtty_init_dev,
1360 .release = mtty_release_dev,
1362 .write = mtty_write,
1363 .ioctl = mtty_ioctl,
1364 .bind_iommufd = vfio_iommufd_emulated_bind,
1365 .unbind_iommufd = vfio_iommufd_emulated_unbind,
1366 .attach_ioas = vfio_iommufd_emulated_attach_ioas,
1367 .detach_ioas = vfio_iommufd_emulated_detach_ioas,
1368 .close_device = mtty_close,
1371 static struct mdev_driver mtty_driver = {
1372 .device_api = VFIO_DEVICE_API_PCI_STRING,
1375 .owner = THIS_MODULE,
1376 .mod_name = KBUILD_MODNAME,
1377 .dev_groups = mdev_dev_groups,
1379 .probe = mtty_probe,
1380 .remove = mtty_remove,
1381 .get_available = mtty_get_available,
1384 static void mtty_device_release(struct device *dev)
1386 dev_dbg(dev, "mtty: released\n");
1389 static int __init mtty_dev_init(void)
1393 pr_info("mtty_dev: %s\n", __func__);
1395 memset(&mtty_dev, 0, sizeof(mtty_dev));
1397 idr_init(&mtty_dev.vd_idr);
1399 ret = alloc_chrdev_region(&mtty_dev.vd_devt, 0, MINORMASK + 1,
1403 pr_err("Error: failed to register mtty_dev, err:%d\n", ret);
1407 cdev_init(&mtty_dev.vd_cdev, &vd_fops);
1408 cdev_add(&mtty_dev.vd_cdev, mtty_dev.vd_devt, MINORMASK + 1);
1410 pr_info("major_number:%d\n", MAJOR(mtty_dev.vd_devt));
1412 ret = mdev_register_driver(&mtty_driver);
1416 mtty_dev.vd_class = class_create(MTTY_CLASS_NAME);
1418 if (IS_ERR(mtty_dev.vd_class)) {
1419 pr_err("Error: failed to register mtty_dev class\n");
1420 ret = PTR_ERR(mtty_dev.vd_class);
1424 mtty_dev.dev.class = mtty_dev.vd_class;
1425 mtty_dev.dev.release = mtty_device_release;
1426 dev_set_name(&mtty_dev.dev, "%s", MTTY_NAME);
1428 ret = device_register(&mtty_dev.dev);
1432 ret = mdev_register_parent(&mtty_dev.parent, &mtty_dev.dev,
1433 &mtty_driver, mtty_mdev_types,
1434 ARRAY_SIZE(mtty_mdev_types));
1440 device_del(&mtty_dev.dev);
1442 put_device(&mtty_dev.dev);
1443 class_destroy(mtty_dev.vd_class);
1445 mdev_unregister_driver(&mtty_driver);
1447 cdev_del(&mtty_dev.vd_cdev);
1448 unregister_chrdev_region(mtty_dev.vd_devt, MINORMASK + 1);
1452 static void __exit mtty_dev_exit(void)
1454 mtty_dev.dev.bus = NULL;
1455 mdev_unregister_parent(&mtty_dev.parent);
1457 device_unregister(&mtty_dev.dev);
1458 idr_destroy(&mtty_dev.vd_idr);
1459 mdev_unregister_driver(&mtty_driver);
1460 cdev_del(&mtty_dev.vd_cdev);
1461 unregister_chrdev_region(mtty_dev.vd_devt, MINORMASK + 1);
1462 class_destroy(mtty_dev.vd_class);
1463 mtty_dev.vd_class = NULL;
1464 pr_info("mtty_dev: Unloaded!\n");
1467 module_init(mtty_dev_init)
1468 module_exit(mtty_dev_exit)
1470 MODULE_LICENSE("GPL v2");
1471 MODULE_INFO(supported, "Test driver that simulate serial port over PCI");
1472 MODULE_VERSION(VERSION_STRING);
1473 MODULE_AUTHOR(DRIVER_AUTHOR);