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/device.h>
16 #include <linux/kernel.h>
18 #include <linux/poll.h>
19 #include <linux/slab.h>
20 #include <linux/cdev.h>
21 #include <linux/sched.h>
22 #include <linux/wait.h>
23 #include <linux/uuid.h>
24 #include <linux/vfio.h>
25 #include <linux/iommu.h>
26 #include <linux/sysfs.h>
27 #include <linux/ctype.h>
28 #include <linux/file.h>
29 #include <linux/mdev.h>
30 #include <linux/pci.h>
31 #include <linux/serial.h>
32 #include <uapi/linux/serial_reg.h>
33 #include <linux/eventfd.h>
38 #define VERSION_STRING "0.1"
39 #define DRIVER_AUTHOR "NVIDIA Corporation"
41 #define MTTY_CLASS_NAME "mtty"
43 #define MTTY_NAME "mtty"
45 #define MTTY_STRING_LEN 16
47 #define MTTY_CONFIG_SPACE_SIZE 0xff
48 #define MTTY_IO_BAR_SIZE 0x8
49 #define MTTY_MMIO_BAR_SIZE 0x100000
51 #define STORE_LE16(addr, val) (*(u16 *)addr = val)
52 #define STORE_LE32(addr, val) (*(u32 *)addr = val)
54 #define MAX_FIFO_SIZE 16
56 #define CIRCULAR_BUF_INC_IDX(idx) (idx = (idx + 1) & (MAX_FIFO_SIZE - 1))
58 #define MTTY_VFIO_PCI_OFFSET_SHIFT 40
60 #define MTTY_VFIO_PCI_OFFSET_TO_INDEX(off) (off >> MTTY_VFIO_PCI_OFFSET_SHIFT)
61 #define MTTY_VFIO_PCI_INDEX_TO_OFFSET(index) \
62 ((u64)(index) << MTTY_VFIO_PCI_OFFSET_SHIFT)
63 #define MTTY_VFIO_PCI_OFFSET_MASK \
64 (((u64)(1) << MTTY_VFIO_PCI_OFFSET_SHIFT) - 1)
71 static struct mtty_dev {
73 struct class *vd_class;
79 struct mdev_region_info {
86 #if defined(DEBUG_REGS)
87 static const char *wr_reg[] = {
98 static const char *rd_reg[] = {
110 /* loop back buffer */
112 u8 fifo[MAX_FIFO_SIZE];
118 u8 uart_reg[8]; /* 8 registers */
119 struct rxtx rxtx; /* loop back buffer */
123 u8 fcr; /* FIFO control register */
125 u8 intr_trigger_level; /* interrupt trigger level */
128 /* State of each mdev device */
130 struct vfio_device vdev;
132 struct eventfd_ctx *intx_evtfd;
133 struct eventfd_ctx *msi_evtfd;
136 struct mutex ops_lock;
137 struct mdev_device *mdev;
138 struct mdev_region_info region_info[VFIO_PCI_NUM_REGIONS];
139 u32 bar_mask[VFIO_PCI_NUM_REGIONS];
140 struct list_head next;
141 struct serial_port s[2];
142 struct mutex rxtx_lock;
143 struct vfio_device_info dev_info;
147 static atomic_t mdev_avail_ports = ATOMIC_INIT(MAX_MTTYS);
149 static const struct file_operations vd_fops = {
150 .owner = THIS_MODULE,
153 static const struct vfio_device_ops mtty_dev_ops;
155 /* function prototypes */
157 static int mtty_trigger_interrupt(struct mdev_state *mdev_state);
159 /* Helper functions */
161 static void dump_buffer(u8 *buf, uint32_t count)
166 pr_info("Buffer:\n");
167 for (i = 0; i < count; i++) {
168 pr_info("%2x ", *(buf + i));
169 if ((i + 1) % 16 == 0)
175 static void mtty_create_config_space(struct mdev_state *mdev_state)
178 STORE_LE32((u32 *) &mdev_state->vconfig[0x0], 0x32534348);
180 /* Control: I/O+, Mem-, BusMaster- */
181 STORE_LE16((u16 *) &mdev_state->vconfig[0x4], 0x0001);
183 /* Status: capabilities list absent */
184 STORE_LE16((u16 *) &mdev_state->vconfig[0x6], 0x0200);
187 mdev_state->vconfig[0x8] = 0x10;
189 /* programming interface class : 16550-compatible serial controller */
190 mdev_state->vconfig[0x9] = 0x02;
193 mdev_state->vconfig[0xa] = 0x00;
195 /* Base class : Simple Communication controllers */
196 mdev_state->vconfig[0xb] = 0x07;
198 /* base address registers */
200 STORE_LE32((u32 *) &mdev_state->vconfig[0x10], 0x000001);
201 mdev_state->bar_mask[0] = ~(MTTY_IO_BAR_SIZE) + 1;
203 if (mdev_state->nr_ports == 2) {
205 STORE_LE32((u32 *) &mdev_state->vconfig[0x14], 0x000001);
206 mdev_state->bar_mask[1] = ~(MTTY_IO_BAR_SIZE) + 1;
210 STORE_LE32((u32 *) &mdev_state->vconfig[0x2c], 0x32534348);
212 mdev_state->vconfig[0x34] = 0x00; /* Cap Ptr */
213 mdev_state->vconfig[0x3d] = 0x01; /* interrupt pin (INTA#) */
215 /* Vendor specific data */
216 mdev_state->vconfig[0x40] = 0x23;
217 mdev_state->vconfig[0x43] = 0x80;
218 mdev_state->vconfig[0x44] = 0x23;
219 mdev_state->vconfig[0x48] = 0x23;
220 mdev_state->vconfig[0x4c] = 0x23;
222 mdev_state->vconfig[0x60] = 0x50;
223 mdev_state->vconfig[0x61] = 0x43;
224 mdev_state->vconfig[0x62] = 0x49;
225 mdev_state->vconfig[0x63] = 0x20;
226 mdev_state->vconfig[0x64] = 0x53;
227 mdev_state->vconfig[0x65] = 0x65;
228 mdev_state->vconfig[0x66] = 0x72;
229 mdev_state->vconfig[0x67] = 0x69;
230 mdev_state->vconfig[0x68] = 0x61;
231 mdev_state->vconfig[0x69] = 0x6c;
232 mdev_state->vconfig[0x6a] = 0x2f;
233 mdev_state->vconfig[0x6b] = 0x55;
234 mdev_state->vconfig[0x6c] = 0x41;
235 mdev_state->vconfig[0x6d] = 0x52;
236 mdev_state->vconfig[0x6e] = 0x54;
239 static void handle_pci_cfg_write(struct mdev_state *mdev_state, u16 offset,
242 u32 cfg_addr, bar_mask, bar_index = 0;
245 case 0x04: /* device control */
246 case 0x06: /* device status */
249 case 0x3c: /* interrupt line */
250 mdev_state->vconfig[0x3c] = buf[0];
254 * Interrupt Pin is hardwired to INTA.
255 * This field is write protected by hardware
258 case 0x10: /* BAR0 */
259 case 0x14: /* BAR1 */
262 else if (offset == 0x14)
265 if ((mdev_state->nr_ports == 1) && (bar_index == 1)) {
266 STORE_LE32(&mdev_state->vconfig[offset], 0);
270 cfg_addr = *(u32 *)buf;
271 pr_info("BAR%d addr 0x%x\n", bar_index, cfg_addr);
273 if (cfg_addr == 0xffffffff) {
274 bar_mask = mdev_state->bar_mask[bar_index];
275 cfg_addr = (cfg_addr & bar_mask);
278 cfg_addr |= (mdev_state->vconfig[offset] & 0x3ul);
279 STORE_LE32(&mdev_state->vconfig[offset], cfg_addr);
281 case 0x18: /* BAR2 */
282 case 0x1c: /* BAR3 */
283 case 0x20: /* BAR4 */
284 STORE_LE32(&mdev_state->vconfig[offset], 0);
287 pr_info("PCI config write @0x%x of %d bytes not handled\n",
293 static void handle_bar_write(unsigned int index, struct mdev_state *mdev_state,
294 u16 offset, u8 *buf, u32 count)
298 /* Handle data written by guest */
301 /* if DLAB set, data is LSB of divisor */
302 if (mdev_state->s[index].dlab) {
303 mdev_state->s[index].divisor |= data;
307 mutex_lock(&mdev_state->rxtx_lock);
309 /* save in TX buffer */
310 if (mdev_state->s[index].rxtx.count <
311 mdev_state->s[index].max_fifo_size) {
312 mdev_state->s[index].rxtx.fifo[
313 mdev_state->s[index].rxtx.head] = data;
314 mdev_state->s[index].rxtx.count++;
315 CIRCULAR_BUF_INC_IDX(mdev_state->s[index].rxtx.head);
316 mdev_state->s[index].overrun = false;
319 * Trigger interrupt if receive data interrupt is
320 * enabled and fifo reached trigger level
322 if ((mdev_state->s[index].uart_reg[UART_IER] &
324 (mdev_state->s[index].rxtx.count ==
325 mdev_state->s[index].intr_trigger_level)) {
326 /* trigger interrupt */
327 #if defined(DEBUG_INTR)
328 pr_err("Serial port %d: Fifo level trigger\n",
331 mtty_trigger_interrupt(mdev_state);
334 #if defined(DEBUG_INTR)
335 pr_err("Serial port %d: Buffer Overflow\n", index);
337 mdev_state->s[index].overrun = true;
340 * Trigger interrupt if receiver line status interrupt
343 if (mdev_state->s[index].uart_reg[UART_IER] &
345 mtty_trigger_interrupt(mdev_state);
347 mutex_unlock(&mdev_state->rxtx_lock);
351 /* if DLAB set, data is MSB of divisor */
352 if (mdev_state->s[index].dlab)
353 mdev_state->s[index].divisor |= (u16)data << 8;
355 mdev_state->s[index].uart_reg[offset] = data;
356 mutex_lock(&mdev_state->rxtx_lock);
357 if ((data & UART_IER_THRI) &&
358 (mdev_state->s[index].rxtx.head ==
359 mdev_state->s[index].rxtx.tail)) {
360 #if defined(DEBUG_INTR)
361 pr_err("Serial port %d: IER_THRI write\n",
364 mtty_trigger_interrupt(mdev_state);
367 mutex_unlock(&mdev_state->rxtx_lock);
373 mdev_state->s[index].fcr = data;
375 mutex_lock(&mdev_state->rxtx_lock);
376 if (data & (UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT)) {
377 /* clear loop back FIFO */
378 mdev_state->s[index].rxtx.count = 0;
379 mdev_state->s[index].rxtx.head = 0;
380 mdev_state->s[index].rxtx.tail = 0;
382 mutex_unlock(&mdev_state->rxtx_lock);
384 switch (data & UART_FCR_TRIGGER_MASK) {
385 case UART_FCR_TRIGGER_1:
386 mdev_state->s[index].intr_trigger_level = 1;
389 case UART_FCR_TRIGGER_4:
390 mdev_state->s[index].intr_trigger_level = 4;
393 case UART_FCR_TRIGGER_8:
394 mdev_state->s[index].intr_trigger_level = 8;
397 case UART_FCR_TRIGGER_14:
398 mdev_state->s[index].intr_trigger_level = 14;
403 * Set trigger level to 1 otherwise or implement timer with
404 * timeout of 4 characters and on expiring that timer set
405 * Recevice data timeout in IIR register
407 mdev_state->s[index].intr_trigger_level = 1;
408 if (data & UART_FCR_ENABLE_FIFO)
409 mdev_state->s[index].max_fifo_size = MAX_FIFO_SIZE;
411 mdev_state->s[index].max_fifo_size = 1;
412 mdev_state->s[index].intr_trigger_level = 1;
418 if (data & UART_LCR_DLAB) {
419 mdev_state->s[index].dlab = true;
420 mdev_state->s[index].divisor = 0;
422 mdev_state->s[index].dlab = false;
424 mdev_state->s[index].uart_reg[offset] = data;
428 mdev_state->s[index].uart_reg[offset] = data;
430 if ((mdev_state->s[index].uart_reg[UART_IER] & UART_IER_MSI) &&
431 (data & UART_MCR_OUT2)) {
432 #if defined(DEBUG_INTR)
433 pr_err("Serial port %d: MCR_OUT2 write\n", index);
435 mtty_trigger_interrupt(mdev_state);
438 if ((mdev_state->s[index].uart_reg[UART_IER] & UART_IER_MSI) &&
439 (data & (UART_MCR_RTS | UART_MCR_DTR))) {
440 #if defined(DEBUG_INTR)
441 pr_err("Serial port %d: MCR RTS/DTR write\n", index);
443 mtty_trigger_interrupt(mdev_state);
453 mdev_state->s[index].uart_reg[offset] = data;
461 static void handle_bar_read(unsigned int index, struct mdev_state *mdev_state,
462 u16 offset, u8 *buf, u32 count)
464 /* Handle read requests by guest */
467 /* if DLAB set, data is LSB of divisor */
468 if (mdev_state->s[index].dlab) {
469 *buf = (u8)mdev_state->s[index].divisor;
473 mutex_lock(&mdev_state->rxtx_lock);
474 /* return data in tx buffer */
475 if (mdev_state->s[index].rxtx.head !=
476 mdev_state->s[index].rxtx.tail) {
477 *buf = mdev_state->s[index].rxtx.fifo[
478 mdev_state->s[index].rxtx.tail];
479 mdev_state->s[index].rxtx.count--;
480 CIRCULAR_BUF_INC_IDX(mdev_state->s[index].rxtx.tail);
483 if (mdev_state->s[index].rxtx.head ==
484 mdev_state->s[index].rxtx.tail) {
486 * Trigger interrupt if tx buffer empty interrupt is
487 * enabled and fifo is empty
489 #if defined(DEBUG_INTR)
490 pr_err("Serial port %d: Buffer Empty\n", index);
492 if (mdev_state->s[index].uart_reg[UART_IER] &
494 mtty_trigger_interrupt(mdev_state);
496 mutex_unlock(&mdev_state->rxtx_lock);
501 if (mdev_state->s[index].dlab) {
502 *buf = (u8)(mdev_state->s[index].divisor >> 8);
505 *buf = mdev_state->s[index].uart_reg[offset] & 0x0f;
510 u8 ier = mdev_state->s[index].uart_reg[UART_IER];
513 mutex_lock(&mdev_state->rxtx_lock);
514 /* Interrupt priority 1: Parity, overrun, framing or break */
515 if ((ier & UART_IER_RLSI) && mdev_state->s[index].overrun)
516 *buf |= UART_IIR_RLSI;
518 /* Interrupt priority 2: Fifo trigger level reached */
519 if ((ier & UART_IER_RDI) &&
520 (mdev_state->s[index].rxtx.count >=
521 mdev_state->s[index].intr_trigger_level))
522 *buf |= UART_IIR_RDI;
524 /* Interrupt priotiry 3: transmitter holding register empty */
525 if ((ier & UART_IER_THRI) &&
526 (mdev_state->s[index].rxtx.head ==
527 mdev_state->s[index].rxtx.tail))
528 *buf |= UART_IIR_THRI;
530 /* Interrupt priotiry 4: Modem status: CTS, DSR, RI or DCD */
531 if ((ier & UART_IER_MSI) &&
532 (mdev_state->s[index].uart_reg[UART_MCR] &
533 (UART_MCR_RTS | UART_MCR_DTR)))
534 *buf |= UART_IIR_MSI;
536 /* bit0: 0=> interrupt pending, 1=> no interrupt is pending */
538 *buf = UART_IIR_NO_INT;
540 /* set bit 6 & 7 to be 16550 compatible */
542 mutex_unlock(&mdev_state->rxtx_lock);
548 *buf = mdev_state->s[index].uart_reg[offset];
555 mutex_lock(&mdev_state->rxtx_lock);
556 /* atleast one char in FIFO */
557 if (mdev_state->s[index].rxtx.head !=
558 mdev_state->s[index].rxtx.tail)
561 /* if FIFO overrun */
562 if (mdev_state->s[index].overrun)
565 /* transmit FIFO empty and tramsitter empty */
566 if (mdev_state->s[index].rxtx.head ==
567 mdev_state->s[index].rxtx.tail)
568 lsr |= UART_LSR_TEMT | UART_LSR_THRE;
570 mutex_unlock(&mdev_state->rxtx_lock);
575 *buf = UART_MSR_DSR | UART_MSR_DDSR | UART_MSR_DCD;
577 mutex_lock(&mdev_state->rxtx_lock);
578 /* if AFE is 1 and FIFO have space, set CTS bit */
579 if (mdev_state->s[index].uart_reg[UART_MCR] &
581 if (mdev_state->s[index].rxtx.count <
582 mdev_state->s[index].max_fifo_size)
583 *buf |= UART_MSR_CTS | UART_MSR_DCTS;
585 *buf |= UART_MSR_CTS | UART_MSR_DCTS;
586 mutex_unlock(&mdev_state->rxtx_lock);
591 *buf = mdev_state->s[index].uart_reg[offset];
599 static void mdev_read_base(struct mdev_state *mdev_state)
602 u32 start_lo, start_hi;
605 pos = PCI_BASE_ADDRESS_0;
607 for (index = 0; index <= VFIO_PCI_BAR5_REGION_INDEX; index++) {
609 if (!mdev_state->region_info[index].size)
612 start_lo = (*(u32 *)(mdev_state->vconfig + pos)) &
613 PCI_BASE_ADDRESS_MEM_MASK;
614 mem_type = (*(u32 *)(mdev_state->vconfig + pos)) &
615 PCI_BASE_ADDRESS_MEM_TYPE_MASK;
618 case PCI_BASE_ADDRESS_MEM_TYPE_64:
619 start_hi = (*(u32 *)(mdev_state->vconfig + pos + 4));
622 case PCI_BASE_ADDRESS_MEM_TYPE_32:
623 case PCI_BASE_ADDRESS_MEM_TYPE_1M:
624 /* 1M mem BAR treated as 32-bit BAR */
626 /* mem unknown type treated as 32-bit BAR */
631 mdev_state->region_info[index].start = ((u64)start_hi << 32) |
636 static ssize_t mdev_access(struct mdev_state *mdev_state, u8 *buf, size_t count,
637 loff_t pos, bool is_write)
646 mutex_lock(&mdev_state->ops_lock);
648 index = MTTY_VFIO_PCI_OFFSET_TO_INDEX(pos);
649 offset = pos & MTTY_VFIO_PCI_OFFSET_MASK;
651 case VFIO_PCI_CONFIG_REGION_INDEX:
654 pr_info("%s: PCI config space %s at offset 0x%llx\n",
655 __func__, is_write ? "write" : "read", offset);
658 dump_buffer(buf, count);
659 handle_pci_cfg_write(mdev_state, offset, buf, count);
661 memcpy(buf, (mdev_state->vconfig + offset), count);
662 dump_buffer(buf, count);
667 case VFIO_PCI_BAR0_REGION_INDEX ... VFIO_PCI_BAR5_REGION_INDEX:
668 if (!mdev_state->region_info[index].start)
669 mdev_read_base(mdev_state);
672 dump_buffer(buf, count);
674 #if defined(DEBUG_REGS)
675 pr_info("%s: BAR%d WR @0x%llx %s val:0x%02x dlab:%d\n",
676 __func__, index, offset, wr_reg[offset],
677 *buf, mdev_state->s[index].dlab);
679 handle_bar_write(index, mdev_state, offset, buf, count);
681 handle_bar_read(index, mdev_state, offset, buf, count);
682 dump_buffer(buf, count);
684 #if defined(DEBUG_REGS)
685 pr_info("%s: BAR%d RD @0x%llx %s val:0x%02x dlab:%d\n",
686 __func__, index, offset, rd_reg[offset],
687 *buf, mdev_state->s[index].dlab);
701 mutex_unlock(&mdev_state->ops_lock);
706 static int mtty_probe(struct mdev_device *mdev)
708 struct mdev_state *mdev_state;
709 int nr_ports = mdev_get_type_group_id(mdev) + 1;
710 int avail_ports = atomic_read(&mdev_avail_ports);
714 if (avail_ports < nr_ports)
716 } while (!atomic_try_cmpxchg(&mdev_avail_ports,
717 &avail_ports, avail_ports - nr_ports));
719 mdev_state = kzalloc(sizeof(struct mdev_state), GFP_KERNEL);
720 if (mdev_state == NULL) {
721 atomic_add(nr_ports, &mdev_avail_ports);
725 vfio_init_group_dev(&mdev_state->vdev, &mdev->dev, &mtty_dev_ops);
727 mdev_state->nr_ports = nr_ports;
728 mdev_state->irq_index = -1;
729 mdev_state->s[0].max_fifo_size = MAX_FIFO_SIZE;
730 mdev_state->s[1].max_fifo_size = MAX_FIFO_SIZE;
731 mutex_init(&mdev_state->rxtx_lock);
732 mdev_state->vconfig = kzalloc(MTTY_CONFIG_SPACE_SIZE, GFP_KERNEL);
734 if (mdev_state->vconfig == NULL) {
736 atomic_add(nr_ports, &mdev_avail_ports);
740 mutex_init(&mdev_state->ops_lock);
741 mdev_state->mdev = mdev;
743 mtty_create_config_space(mdev_state);
745 ret = vfio_register_group_dev(&mdev_state->vdev);
748 atomic_add(nr_ports, &mdev_avail_ports);
752 dev_set_drvdata(&mdev->dev, mdev_state);
756 static void mtty_remove(struct mdev_device *mdev)
758 struct mdev_state *mdev_state = dev_get_drvdata(&mdev->dev);
759 int nr_ports = mdev_state->nr_ports;
761 vfio_unregister_group_dev(&mdev_state->vdev);
763 kfree(mdev_state->vconfig);
765 atomic_add(nr_ports, &mdev_avail_ports);
768 static int mtty_reset(struct mdev_state *mdev_state)
770 pr_info("%s: called\n", __func__);
775 static ssize_t mtty_read(struct vfio_device *vdev, char __user *buf,
776 size_t count, loff_t *ppos)
778 struct mdev_state *mdev_state =
779 container_of(vdev, struct mdev_state, vdev);
780 unsigned int done = 0;
786 if (count >= 4 && !(*ppos % 4)) {
789 ret = mdev_access(mdev_state, (u8 *)&val, sizeof(val),
794 if (copy_to_user(buf, &val, sizeof(val)))
798 } else if (count >= 2 && !(*ppos % 2)) {
801 ret = mdev_access(mdev_state, (u8 *)&val, sizeof(val),
806 if (copy_to_user(buf, &val, sizeof(val)))
813 ret = mdev_access(mdev_state, (u8 *)&val, sizeof(val),
818 if (copy_to_user(buf, &val, sizeof(val)))
836 static ssize_t mtty_write(struct vfio_device *vdev, const char __user *buf,
837 size_t count, loff_t *ppos)
839 struct mdev_state *mdev_state =
840 container_of(vdev, struct mdev_state, vdev);
841 unsigned int done = 0;
847 if (count >= 4 && !(*ppos % 4)) {
850 if (copy_from_user(&val, buf, sizeof(val)))
853 ret = mdev_access(mdev_state, (u8 *)&val, sizeof(val),
859 } else if (count >= 2 && !(*ppos % 2)) {
862 if (copy_from_user(&val, buf, sizeof(val)))
865 ret = mdev_access(mdev_state, (u8 *)&val, sizeof(val),
874 if (copy_from_user(&val, buf, sizeof(val)))
877 ret = mdev_access(mdev_state, (u8 *)&val, sizeof(val),
895 static int mtty_set_irqs(struct mdev_state *mdev_state, uint32_t flags,
896 unsigned int index, unsigned int start,
897 unsigned int count, void *data)
901 mutex_lock(&mdev_state->ops_lock);
903 case VFIO_PCI_INTX_IRQ_INDEX:
904 switch (flags & VFIO_IRQ_SET_ACTION_TYPE_MASK) {
905 case VFIO_IRQ_SET_ACTION_MASK:
906 case VFIO_IRQ_SET_ACTION_UNMASK:
908 case VFIO_IRQ_SET_ACTION_TRIGGER:
910 if (flags & VFIO_IRQ_SET_DATA_NONE) {
911 pr_info("%s: disable INTx\n", __func__);
912 if (mdev_state->intx_evtfd)
913 eventfd_ctx_put(mdev_state->intx_evtfd);
917 if (flags & VFIO_IRQ_SET_DATA_EVENTFD) {
918 int fd = *(int *)data;
921 struct eventfd_ctx *evt;
923 evt = eventfd_ctx_fdget(fd);
928 mdev_state->intx_evtfd = evt;
929 mdev_state->irq_fd = fd;
930 mdev_state->irq_index = index;
938 case VFIO_PCI_MSI_IRQ_INDEX:
939 switch (flags & VFIO_IRQ_SET_ACTION_TYPE_MASK) {
940 case VFIO_IRQ_SET_ACTION_MASK:
941 case VFIO_IRQ_SET_ACTION_UNMASK:
943 case VFIO_IRQ_SET_ACTION_TRIGGER:
944 if (flags & VFIO_IRQ_SET_DATA_NONE) {
945 if (mdev_state->msi_evtfd)
946 eventfd_ctx_put(mdev_state->msi_evtfd);
947 pr_info("%s: disable MSI\n", __func__);
948 mdev_state->irq_index = VFIO_PCI_INTX_IRQ_INDEX;
951 if (flags & VFIO_IRQ_SET_DATA_EVENTFD) {
952 int fd = *(int *)data;
953 struct eventfd_ctx *evt;
958 if (mdev_state->msi_evtfd)
961 evt = eventfd_ctx_fdget(fd);
966 mdev_state->msi_evtfd = evt;
967 mdev_state->irq_fd = fd;
968 mdev_state->irq_index = index;
973 case VFIO_PCI_MSIX_IRQ_INDEX:
974 pr_info("%s: MSIX_IRQ\n", __func__);
976 case VFIO_PCI_ERR_IRQ_INDEX:
977 pr_info("%s: ERR_IRQ\n", __func__);
979 case VFIO_PCI_REQ_IRQ_INDEX:
980 pr_info("%s: REQ_IRQ\n", __func__);
984 mutex_unlock(&mdev_state->ops_lock);
988 static int mtty_trigger_interrupt(struct mdev_state *mdev_state)
992 if ((mdev_state->irq_index == VFIO_PCI_MSI_IRQ_INDEX) &&
993 (!mdev_state->msi_evtfd))
995 else if ((mdev_state->irq_index == VFIO_PCI_INTX_IRQ_INDEX) &&
996 (!mdev_state->intx_evtfd)) {
997 pr_info("%s: Intr eventfd not found\n", __func__);
1001 if (mdev_state->irq_index == VFIO_PCI_MSI_IRQ_INDEX)
1002 ret = eventfd_signal(mdev_state->msi_evtfd, 1);
1004 ret = eventfd_signal(mdev_state->intx_evtfd, 1);
1006 #if defined(DEBUG_INTR)
1007 pr_info("Intx triggered\n");
1010 pr_err("%s: eventfd signal failed (%d)\n", __func__, ret);
1015 static int mtty_get_region_info(struct mdev_state *mdev_state,
1016 struct vfio_region_info *region_info,
1017 u16 *cap_type_id, void **cap_type)
1019 unsigned int size = 0;
1022 bar_index = region_info->index;
1023 if (bar_index >= VFIO_PCI_NUM_REGIONS)
1026 mutex_lock(&mdev_state->ops_lock);
1028 switch (bar_index) {
1029 case VFIO_PCI_CONFIG_REGION_INDEX:
1030 size = MTTY_CONFIG_SPACE_SIZE;
1032 case VFIO_PCI_BAR0_REGION_INDEX:
1033 size = MTTY_IO_BAR_SIZE;
1035 case VFIO_PCI_BAR1_REGION_INDEX:
1036 if (mdev_state->nr_ports == 2)
1037 size = MTTY_IO_BAR_SIZE;
1044 mdev_state->region_info[bar_index].size = size;
1045 mdev_state->region_info[bar_index].vfio_offset =
1046 MTTY_VFIO_PCI_INDEX_TO_OFFSET(bar_index);
1048 region_info->size = size;
1049 region_info->offset = MTTY_VFIO_PCI_INDEX_TO_OFFSET(bar_index);
1050 region_info->flags = VFIO_REGION_INFO_FLAG_READ |
1051 VFIO_REGION_INFO_FLAG_WRITE;
1052 mutex_unlock(&mdev_state->ops_lock);
1056 static int mtty_get_irq_info(struct vfio_irq_info *irq_info)
1058 switch (irq_info->index) {
1059 case VFIO_PCI_INTX_IRQ_INDEX:
1060 case VFIO_PCI_MSI_IRQ_INDEX:
1061 case VFIO_PCI_REQ_IRQ_INDEX:
1068 irq_info->flags = VFIO_IRQ_INFO_EVENTFD;
1069 irq_info->count = 1;
1071 if (irq_info->index == VFIO_PCI_INTX_IRQ_INDEX)
1072 irq_info->flags |= (VFIO_IRQ_INFO_MASKABLE |
1073 VFIO_IRQ_INFO_AUTOMASKED);
1075 irq_info->flags |= VFIO_IRQ_INFO_NORESIZE;
1080 static int mtty_get_device_info(struct vfio_device_info *dev_info)
1082 dev_info->flags = VFIO_DEVICE_FLAGS_PCI;
1083 dev_info->num_regions = VFIO_PCI_NUM_REGIONS;
1084 dev_info->num_irqs = VFIO_PCI_NUM_IRQS;
1089 static long mtty_ioctl(struct vfio_device *vdev, unsigned int cmd,
1092 struct mdev_state *mdev_state =
1093 container_of(vdev, struct mdev_state, vdev);
1095 unsigned long minsz;
1098 case VFIO_DEVICE_GET_INFO:
1100 struct vfio_device_info info;
1102 minsz = offsetofend(struct vfio_device_info, num_irqs);
1104 if (copy_from_user(&info, (void __user *)arg, minsz))
1107 if (info.argsz < minsz)
1110 ret = mtty_get_device_info(&info);
1114 memcpy(&mdev_state->dev_info, &info, sizeof(info));
1116 if (copy_to_user((void __user *)arg, &info, minsz))
1121 case VFIO_DEVICE_GET_REGION_INFO:
1123 struct vfio_region_info info;
1124 u16 cap_type_id = 0;
1125 void *cap_type = NULL;
1127 minsz = offsetofend(struct vfio_region_info, offset);
1129 if (copy_from_user(&info, (void __user *)arg, minsz))
1132 if (info.argsz < minsz)
1135 ret = mtty_get_region_info(mdev_state, &info, &cap_type_id,
1140 if (copy_to_user((void __user *)arg, &info, minsz))
1146 case VFIO_DEVICE_GET_IRQ_INFO:
1148 struct vfio_irq_info info;
1150 minsz = offsetofend(struct vfio_irq_info, count);
1152 if (copy_from_user(&info, (void __user *)arg, minsz))
1155 if ((info.argsz < minsz) ||
1156 (info.index >= mdev_state->dev_info.num_irqs))
1159 ret = mtty_get_irq_info(&info);
1163 if (copy_to_user((void __user *)arg, &info, minsz))
1168 case VFIO_DEVICE_SET_IRQS:
1170 struct vfio_irq_set hdr;
1171 u8 *data = NULL, *ptr = NULL;
1172 size_t data_size = 0;
1174 minsz = offsetofend(struct vfio_irq_set, count);
1176 if (copy_from_user(&hdr, (void __user *)arg, minsz))
1179 ret = vfio_set_irqs_validate_and_prepare(&hdr,
1180 mdev_state->dev_info.num_irqs,
1187 ptr = data = memdup_user((void __user *)(arg + minsz),
1190 return PTR_ERR(data);
1193 ret = mtty_set_irqs(mdev_state, hdr.flags, hdr.index, hdr.start,
1199 case VFIO_DEVICE_RESET:
1200 return mtty_reset(mdev_state);
1205 static int mtty_open(struct vfio_device *vdev)
1207 pr_info("%s\n", __func__);
1211 static void mtty_close(struct vfio_device *mdev)
1213 pr_info("%s\n", __func__);
1217 sample_mtty_dev_show(struct device *dev, struct device_attribute *attr,
1220 return sprintf(buf, "This is phy device\n");
1223 static DEVICE_ATTR_RO(sample_mtty_dev);
1225 static struct attribute *mtty_dev_attrs[] = {
1226 &dev_attr_sample_mtty_dev.attr,
1230 static const struct attribute_group mtty_dev_group = {
1232 .attrs = mtty_dev_attrs,
1235 static const struct attribute_group *mtty_dev_groups[] = {
1241 sample_mdev_dev_show(struct device *dev, struct device_attribute *attr,
1244 if (mdev_from_dev(dev))
1245 return sprintf(buf, "This is MDEV %s\n", dev_name(dev));
1247 return sprintf(buf, "\n");
1250 static DEVICE_ATTR_RO(sample_mdev_dev);
1252 static struct attribute *mdev_dev_attrs[] = {
1253 &dev_attr_sample_mdev_dev.attr,
1257 static const struct attribute_group mdev_dev_group = {
1259 .attrs = mdev_dev_attrs,
1262 static const struct attribute_group *mdev_dev_groups[] = {
1267 static ssize_t name_show(struct mdev_type *mtype,
1268 struct mdev_type_attribute *attr, char *buf)
1270 static const char *name_str[2] = { "Single port serial",
1271 "Dual port serial" };
1273 return sysfs_emit(buf, "%s\n",
1274 name_str[mtype_get_type_group_id(mtype)]);
1277 static MDEV_TYPE_ATTR_RO(name);
1279 static ssize_t available_instances_show(struct mdev_type *mtype,
1280 struct mdev_type_attribute *attr,
1283 unsigned int ports = mtype_get_type_group_id(mtype) + 1;
1285 return sprintf(buf, "%d\n", atomic_read(&mdev_avail_ports) / ports);
1288 static MDEV_TYPE_ATTR_RO(available_instances);
1290 static ssize_t device_api_show(struct mdev_type *mtype,
1291 struct mdev_type_attribute *attr, char *buf)
1293 return sprintf(buf, "%s\n", VFIO_DEVICE_API_PCI_STRING);
1296 static MDEV_TYPE_ATTR_RO(device_api);
1298 static struct attribute *mdev_types_attrs[] = {
1299 &mdev_type_attr_name.attr,
1300 &mdev_type_attr_device_api.attr,
1301 &mdev_type_attr_available_instances.attr,
1305 static struct attribute_group mdev_type_group1 = {
1307 .attrs = mdev_types_attrs,
1310 static struct attribute_group mdev_type_group2 = {
1312 .attrs = mdev_types_attrs,
1315 static struct attribute_group *mdev_type_groups[] = {
1321 static const struct vfio_device_ops mtty_dev_ops = {
1322 .name = "vfio-mtty",
1324 .release = mtty_close,
1326 .write = mtty_write,
1327 .ioctl = mtty_ioctl,
1330 static struct mdev_driver mtty_driver = {
1333 .owner = THIS_MODULE,
1334 .mod_name = KBUILD_MODNAME,
1335 .dev_groups = mdev_dev_groups,
1337 .probe = mtty_probe,
1338 .remove = mtty_remove,
1341 static const struct mdev_parent_ops mdev_fops = {
1342 .owner = THIS_MODULE,
1343 .device_driver = &mtty_driver,
1344 .dev_attr_groups = mtty_dev_groups,
1345 .supported_type_groups = mdev_type_groups,
1348 static void mtty_device_release(struct device *dev)
1350 dev_dbg(dev, "mtty: released\n");
1353 static int __init mtty_dev_init(void)
1357 pr_info("mtty_dev: %s\n", __func__);
1359 memset(&mtty_dev, 0, sizeof(mtty_dev));
1361 idr_init(&mtty_dev.vd_idr);
1363 ret = alloc_chrdev_region(&mtty_dev.vd_devt, 0, MINORMASK + 1,
1367 pr_err("Error: failed to register mtty_dev, err:%d\n", ret);
1371 cdev_init(&mtty_dev.vd_cdev, &vd_fops);
1372 cdev_add(&mtty_dev.vd_cdev, mtty_dev.vd_devt, MINORMASK + 1);
1374 pr_info("major_number:%d\n", MAJOR(mtty_dev.vd_devt));
1376 ret = mdev_register_driver(&mtty_driver);
1380 mtty_dev.vd_class = class_create(THIS_MODULE, MTTY_CLASS_NAME);
1382 if (IS_ERR(mtty_dev.vd_class)) {
1383 pr_err("Error: failed to register mtty_dev class\n");
1384 ret = PTR_ERR(mtty_dev.vd_class);
1388 mtty_dev.dev.class = mtty_dev.vd_class;
1389 mtty_dev.dev.release = mtty_device_release;
1390 dev_set_name(&mtty_dev.dev, "%s", MTTY_NAME);
1392 ret = device_register(&mtty_dev.dev);
1396 ret = mdev_register_device(&mtty_dev.dev, &mdev_fops);
1402 device_unregister(&mtty_dev.dev);
1404 class_destroy(mtty_dev.vd_class);
1406 mdev_unregister_driver(&mtty_driver);
1408 cdev_del(&mtty_dev.vd_cdev);
1409 unregister_chrdev_region(mtty_dev.vd_devt, MINORMASK + 1);
1413 static void __exit mtty_dev_exit(void)
1415 mtty_dev.dev.bus = NULL;
1416 mdev_unregister_device(&mtty_dev.dev);
1418 device_unregister(&mtty_dev.dev);
1419 idr_destroy(&mtty_dev.vd_idr);
1420 mdev_unregister_driver(&mtty_driver);
1421 cdev_del(&mtty_dev.vd_cdev);
1422 unregister_chrdev_region(mtty_dev.vd_devt, MINORMASK + 1);
1423 class_destroy(mtty_dev.vd_class);
1424 mtty_dev.vd_class = NULL;
1425 pr_info("mtty_dev: Unloaded!\n");
1428 module_init(mtty_dev_init)
1429 module_exit(mtty_dev_exit)
1431 MODULE_LICENSE("GPL v2");
1432 MODULE_INFO(supported, "Test driver that simulate serial port over PCI");
1433 MODULE_VERSION(VERSION_STRING);
1434 MODULE_AUTHOR(DRIVER_AUTHOR);