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) {
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) {
739 mutex_init(&mdev_state->ops_lock);
740 mdev_state->mdev = mdev;
742 mtty_create_config_space(mdev_state);
744 ret = vfio_register_emulated_iommu_dev(&mdev_state->vdev);
747 dev_set_drvdata(&mdev->dev, mdev_state);
751 kfree(mdev_state->vconfig);
753 vfio_uninit_group_dev(&mdev_state->vdev);
756 atomic_add(nr_ports, &mdev_avail_ports);
760 static void mtty_remove(struct mdev_device *mdev)
762 struct mdev_state *mdev_state = dev_get_drvdata(&mdev->dev);
763 int nr_ports = mdev_state->nr_ports;
765 vfio_unregister_group_dev(&mdev_state->vdev);
767 kfree(mdev_state->vconfig);
768 vfio_uninit_group_dev(&mdev_state->vdev);
770 atomic_add(nr_ports, &mdev_avail_ports);
773 static int mtty_reset(struct mdev_state *mdev_state)
775 pr_info("%s: called\n", __func__);
780 static ssize_t mtty_read(struct vfio_device *vdev, char __user *buf,
781 size_t count, loff_t *ppos)
783 struct mdev_state *mdev_state =
784 container_of(vdev, struct mdev_state, vdev);
785 unsigned int done = 0;
791 if (count >= 4 && !(*ppos % 4)) {
794 ret = mdev_access(mdev_state, (u8 *)&val, sizeof(val),
799 if (copy_to_user(buf, &val, sizeof(val)))
803 } else if (count >= 2 && !(*ppos % 2)) {
806 ret = mdev_access(mdev_state, (u8 *)&val, sizeof(val),
811 if (copy_to_user(buf, &val, sizeof(val)))
818 ret = mdev_access(mdev_state, (u8 *)&val, sizeof(val),
823 if (copy_to_user(buf, &val, sizeof(val)))
841 static ssize_t mtty_write(struct vfio_device *vdev, const char __user *buf,
842 size_t count, loff_t *ppos)
844 struct mdev_state *mdev_state =
845 container_of(vdev, struct mdev_state, vdev);
846 unsigned int done = 0;
852 if (count >= 4 && !(*ppos % 4)) {
855 if (copy_from_user(&val, buf, sizeof(val)))
858 ret = mdev_access(mdev_state, (u8 *)&val, sizeof(val),
864 } else if (count >= 2 && !(*ppos % 2)) {
867 if (copy_from_user(&val, buf, sizeof(val)))
870 ret = mdev_access(mdev_state, (u8 *)&val, sizeof(val),
879 if (copy_from_user(&val, buf, sizeof(val)))
882 ret = mdev_access(mdev_state, (u8 *)&val, sizeof(val),
900 static int mtty_set_irqs(struct mdev_state *mdev_state, uint32_t flags,
901 unsigned int index, unsigned int start,
902 unsigned int count, void *data)
906 mutex_lock(&mdev_state->ops_lock);
908 case VFIO_PCI_INTX_IRQ_INDEX:
909 switch (flags & VFIO_IRQ_SET_ACTION_TYPE_MASK) {
910 case VFIO_IRQ_SET_ACTION_MASK:
911 case VFIO_IRQ_SET_ACTION_UNMASK:
913 case VFIO_IRQ_SET_ACTION_TRIGGER:
915 if (flags & VFIO_IRQ_SET_DATA_NONE) {
916 pr_info("%s: disable INTx\n", __func__);
917 if (mdev_state->intx_evtfd)
918 eventfd_ctx_put(mdev_state->intx_evtfd);
922 if (flags & VFIO_IRQ_SET_DATA_EVENTFD) {
923 int fd = *(int *)data;
926 struct eventfd_ctx *evt;
928 evt = eventfd_ctx_fdget(fd);
933 mdev_state->intx_evtfd = evt;
934 mdev_state->irq_fd = fd;
935 mdev_state->irq_index = index;
943 case VFIO_PCI_MSI_IRQ_INDEX:
944 switch (flags & VFIO_IRQ_SET_ACTION_TYPE_MASK) {
945 case VFIO_IRQ_SET_ACTION_MASK:
946 case VFIO_IRQ_SET_ACTION_UNMASK:
948 case VFIO_IRQ_SET_ACTION_TRIGGER:
949 if (flags & VFIO_IRQ_SET_DATA_NONE) {
950 if (mdev_state->msi_evtfd)
951 eventfd_ctx_put(mdev_state->msi_evtfd);
952 pr_info("%s: disable MSI\n", __func__);
953 mdev_state->irq_index = VFIO_PCI_INTX_IRQ_INDEX;
956 if (flags & VFIO_IRQ_SET_DATA_EVENTFD) {
957 int fd = *(int *)data;
958 struct eventfd_ctx *evt;
963 if (mdev_state->msi_evtfd)
966 evt = eventfd_ctx_fdget(fd);
971 mdev_state->msi_evtfd = evt;
972 mdev_state->irq_fd = fd;
973 mdev_state->irq_index = index;
978 case VFIO_PCI_MSIX_IRQ_INDEX:
979 pr_info("%s: MSIX_IRQ\n", __func__);
981 case VFIO_PCI_ERR_IRQ_INDEX:
982 pr_info("%s: ERR_IRQ\n", __func__);
984 case VFIO_PCI_REQ_IRQ_INDEX:
985 pr_info("%s: REQ_IRQ\n", __func__);
989 mutex_unlock(&mdev_state->ops_lock);
993 static int mtty_trigger_interrupt(struct mdev_state *mdev_state)
997 if ((mdev_state->irq_index == VFIO_PCI_MSI_IRQ_INDEX) &&
998 (!mdev_state->msi_evtfd))
1000 else if ((mdev_state->irq_index == VFIO_PCI_INTX_IRQ_INDEX) &&
1001 (!mdev_state->intx_evtfd)) {
1002 pr_info("%s: Intr eventfd not found\n", __func__);
1006 if (mdev_state->irq_index == VFIO_PCI_MSI_IRQ_INDEX)
1007 ret = eventfd_signal(mdev_state->msi_evtfd, 1);
1009 ret = eventfd_signal(mdev_state->intx_evtfd, 1);
1011 #if defined(DEBUG_INTR)
1012 pr_info("Intx triggered\n");
1015 pr_err("%s: eventfd signal failed (%d)\n", __func__, ret);
1020 static int mtty_get_region_info(struct mdev_state *mdev_state,
1021 struct vfio_region_info *region_info,
1022 u16 *cap_type_id, void **cap_type)
1024 unsigned int size = 0;
1027 bar_index = region_info->index;
1028 if (bar_index >= VFIO_PCI_NUM_REGIONS)
1031 mutex_lock(&mdev_state->ops_lock);
1033 switch (bar_index) {
1034 case VFIO_PCI_CONFIG_REGION_INDEX:
1035 size = MTTY_CONFIG_SPACE_SIZE;
1037 case VFIO_PCI_BAR0_REGION_INDEX:
1038 size = MTTY_IO_BAR_SIZE;
1040 case VFIO_PCI_BAR1_REGION_INDEX:
1041 if (mdev_state->nr_ports == 2)
1042 size = MTTY_IO_BAR_SIZE;
1049 mdev_state->region_info[bar_index].size = size;
1050 mdev_state->region_info[bar_index].vfio_offset =
1051 MTTY_VFIO_PCI_INDEX_TO_OFFSET(bar_index);
1053 region_info->size = size;
1054 region_info->offset = MTTY_VFIO_PCI_INDEX_TO_OFFSET(bar_index);
1055 region_info->flags = VFIO_REGION_INFO_FLAG_READ |
1056 VFIO_REGION_INFO_FLAG_WRITE;
1057 mutex_unlock(&mdev_state->ops_lock);
1061 static int mtty_get_irq_info(struct vfio_irq_info *irq_info)
1063 switch (irq_info->index) {
1064 case VFIO_PCI_INTX_IRQ_INDEX:
1065 case VFIO_PCI_MSI_IRQ_INDEX:
1066 case VFIO_PCI_REQ_IRQ_INDEX:
1073 irq_info->flags = VFIO_IRQ_INFO_EVENTFD;
1074 irq_info->count = 1;
1076 if (irq_info->index == VFIO_PCI_INTX_IRQ_INDEX)
1077 irq_info->flags |= (VFIO_IRQ_INFO_MASKABLE |
1078 VFIO_IRQ_INFO_AUTOMASKED);
1080 irq_info->flags |= VFIO_IRQ_INFO_NORESIZE;
1085 static int mtty_get_device_info(struct vfio_device_info *dev_info)
1087 dev_info->flags = VFIO_DEVICE_FLAGS_PCI;
1088 dev_info->num_regions = VFIO_PCI_NUM_REGIONS;
1089 dev_info->num_irqs = VFIO_PCI_NUM_IRQS;
1094 static long mtty_ioctl(struct vfio_device *vdev, unsigned int cmd,
1097 struct mdev_state *mdev_state =
1098 container_of(vdev, struct mdev_state, vdev);
1100 unsigned long minsz;
1103 case VFIO_DEVICE_GET_INFO:
1105 struct vfio_device_info info;
1107 minsz = offsetofend(struct vfio_device_info, num_irqs);
1109 if (copy_from_user(&info, (void __user *)arg, minsz))
1112 if (info.argsz < minsz)
1115 ret = mtty_get_device_info(&info);
1119 memcpy(&mdev_state->dev_info, &info, sizeof(info));
1121 if (copy_to_user((void __user *)arg, &info, minsz))
1126 case VFIO_DEVICE_GET_REGION_INFO:
1128 struct vfio_region_info info;
1129 u16 cap_type_id = 0;
1130 void *cap_type = NULL;
1132 minsz = offsetofend(struct vfio_region_info, offset);
1134 if (copy_from_user(&info, (void __user *)arg, minsz))
1137 if (info.argsz < minsz)
1140 ret = mtty_get_region_info(mdev_state, &info, &cap_type_id,
1145 if (copy_to_user((void __user *)arg, &info, minsz))
1151 case VFIO_DEVICE_GET_IRQ_INFO:
1153 struct vfio_irq_info info;
1155 minsz = offsetofend(struct vfio_irq_info, count);
1157 if (copy_from_user(&info, (void __user *)arg, minsz))
1160 if ((info.argsz < minsz) ||
1161 (info.index >= mdev_state->dev_info.num_irqs))
1164 ret = mtty_get_irq_info(&info);
1168 if (copy_to_user((void __user *)arg, &info, minsz))
1173 case VFIO_DEVICE_SET_IRQS:
1175 struct vfio_irq_set hdr;
1176 u8 *data = NULL, *ptr = NULL;
1177 size_t data_size = 0;
1179 minsz = offsetofend(struct vfio_irq_set, count);
1181 if (copy_from_user(&hdr, (void __user *)arg, minsz))
1184 ret = vfio_set_irqs_validate_and_prepare(&hdr,
1185 mdev_state->dev_info.num_irqs,
1192 ptr = data = memdup_user((void __user *)(arg + minsz),
1195 return PTR_ERR(data);
1198 ret = mtty_set_irqs(mdev_state, hdr.flags, hdr.index, hdr.start,
1204 case VFIO_DEVICE_RESET:
1205 return mtty_reset(mdev_state);
1211 sample_mtty_dev_show(struct device *dev, struct device_attribute *attr,
1214 return sprintf(buf, "This is phy device\n");
1217 static DEVICE_ATTR_RO(sample_mtty_dev);
1219 static struct attribute *mtty_dev_attrs[] = {
1220 &dev_attr_sample_mtty_dev.attr,
1224 static const struct attribute_group mtty_dev_group = {
1226 .attrs = mtty_dev_attrs,
1229 static const struct attribute_group *mtty_dev_groups[] = {
1235 sample_mdev_dev_show(struct device *dev, struct device_attribute *attr,
1238 if (mdev_from_dev(dev))
1239 return sprintf(buf, "This is MDEV %s\n", dev_name(dev));
1241 return sprintf(buf, "\n");
1244 static DEVICE_ATTR_RO(sample_mdev_dev);
1246 static struct attribute *mdev_dev_attrs[] = {
1247 &dev_attr_sample_mdev_dev.attr,
1251 static const struct attribute_group mdev_dev_group = {
1253 .attrs = mdev_dev_attrs,
1256 static const struct attribute_group *mdev_dev_groups[] = {
1261 static ssize_t name_show(struct mdev_type *mtype,
1262 struct mdev_type_attribute *attr, char *buf)
1264 static const char *name_str[2] = { "Single port serial",
1265 "Dual port serial" };
1267 return sysfs_emit(buf, "%s\n",
1268 name_str[mtype_get_type_group_id(mtype)]);
1271 static MDEV_TYPE_ATTR_RO(name);
1273 static ssize_t available_instances_show(struct mdev_type *mtype,
1274 struct mdev_type_attribute *attr,
1277 unsigned int ports = mtype_get_type_group_id(mtype) + 1;
1279 return sprintf(buf, "%d\n", atomic_read(&mdev_avail_ports) / ports);
1282 static MDEV_TYPE_ATTR_RO(available_instances);
1284 static ssize_t device_api_show(struct mdev_type *mtype,
1285 struct mdev_type_attribute *attr, char *buf)
1287 return sprintf(buf, "%s\n", VFIO_DEVICE_API_PCI_STRING);
1290 static MDEV_TYPE_ATTR_RO(device_api);
1292 static struct attribute *mdev_types_attrs[] = {
1293 &mdev_type_attr_name.attr,
1294 &mdev_type_attr_device_api.attr,
1295 &mdev_type_attr_available_instances.attr,
1299 static struct attribute_group mdev_type_group1 = {
1301 .attrs = mdev_types_attrs,
1304 static struct attribute_group mdev_type_group2 = {
1306 .attrs = mdev_types_attrs,
1309 static struct attribute_group *mdev_type_groups[] = {
1315 static const struct vfio_device_ops mtty_dev_ops = {
1316 .name = "vfio-mtty",
1318 .write = mtty_write,
1319 .ioctl = mtty_ioctl,
1322 static struct mdev_driver mtty_driver = {
1325 .owner = THIS_MODULE,
1326 .mod_name = KBUILD_MODNAME,
1327 .dev_groups = mdev_dev_groups,
1329 .probe = mtty_probe,
1330 .remove = mtty_remove,
1333 static const struct mdev_parent_ops mdev_fops = {
1334 .owner = THIS_MODULE,
1335 .device_driver = &mtty_driver,
1336 .dev_attr_groups = mtty_dev_groups,
1337 .supported_type_groups = mdev_type_groups,
1340 static void mtty_device_release(struct device *dev)
1342 dev_dbg(dev, "mtty: released\n");
1345 static int __init mtty_dev_init(void)
1349 pr_info("mtty_dev: %s\n", __func__);
1351 memset(&mtty_dev, 0, sizeof(mtty_dev));
1353 idr_init(&mtty_dev.vd_idr);
1355 ret = alloc_chrdev_region(&mtty_dev.vd_devt, 0, MINORMASK + 1,
1359 pr_err("Error: failed to register mtty_dev, err:%d\n", ret);
1363 cdev_init(&mtty_dev.vd_cdev, &vd_fops);
1364 cdev_add(&mtty_dev.vd_cdev, mtty_dev.vd_devt, MINORMASK + 1);
1366 pr_info("major_number:%d\n", MAJOR(mtty_dev.vd_devt));
1368 ret = mdev_register_driver(&mtty_driver);
1372 mtty_dev.vd_class = class_create(THIS_MODULE, MTTY_CLASS_NAME);
1374 if (IS_ERR(mtty_dev.vd_class)) {
1375 pr_err("Error: failed to register mtty_dev class\n");
1376 ret = PTR_ERR(mtty_dev.vd_class);
1380 mtty_dev.dev.class = mtty_dev.vd_class;
1381 mtty_dev.dev.release = mtty_device_release;
1382 dev_set_name(&mtty_dev.dev, "%s", MTTY_NAME);
1384 ret = device_register(&mtty_dev.dev);
1388 ret = mdev_register_device(&mtty_dev.dev, &mdev_fops);
1394 device_unregister(&mtty_dev.dev);
1396 class_destroy(mtty_dev.vd_class);
1398 mdev_unregister_driver(&mtty_driver);
1400 cdev_del(&mtty_dev.vd_cdev);
1401 unregister_chrdev_region(mtty_dev.vd_devt, MINORMASK + 1);
1405 static void __exit mtty_dev_exit(void)
1407 mtty_dev.dev.bus = NULL;
1408 mdev_unregister_device(&mtty_dev.dev);
1410 device_unregister(&mtty_dev.dev);
1411 idr_destroy(&mtty_dev.vd_idr);
1412 mdev_unregister_driver(&mtty_driver);
1413 cdev_del(&mtty_dev.vd_cdev);
1414 unregister_chrdev_region(mtty_dev.vd_devt, MINORMASK + 1);
1415 class_destroy(mtty_dev.vd_class);
1416 mtty_dev.vd_class = NULL;
1417 pr_info("mtty_dev: Unloaded!\n");
1420 module_init(mtty_dev_init)
1421 module_exit(mtty_dev_exit)
1423 MODULE_LICENSE("GPL v2");
1424 MODULE_INFO(supported, "Test driver that simulate serial port over PCI");
1425 MODULE_VERSION(VERSION_STRING);
1426 MODULE_AUTHOR(DRIVER_AUTHOR);