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>
32 #include <linux/anon_inodes.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
;
77 struct mdev_parent parent
;
80 struct mdev_region_info
{
87 #if defined(DEBUG_REGS)
88 static const char *wr_reg
[] = {
99 static const char *rd_reg
[] = {
111 /* loop back buffer */
113 u8 fifo
[MAX_FIFO_SIZE
];
119 u8 uart_reg
[8]; /* 8 registers */
120 struct rxtx rxtx
; /* loop back buffer */
124 u8 fcr
; /* FIFO control register */
126 u8 intr_trigger_level
; /* interrupt trigger level */
131 #define MTTY_MAGIC 0x7e9d09898c3e2c4e /* Nothing clever, just random */
133 #define MTTY_MAJOR_VER 1
135 #define MTTY_MINOR_VER 0
138 struct serial_port ports
[2];
143 struct mtty_migration_file
{
146 struct mdev_state
*mdev_state
;
147 struct mtty_data data
;
152 /* State of each mdev device */
154 struct vfio_device vdev
;
155 struct eventfd_ctx
*intx_evtfd
;
156 struct eventfd_ctx
*msi_evtfd
;
159 struct mutex ops_lock
;
160 struct mdev_device
*mdev
;
161 struct mdev_region_info region_info
[VFIO_PCI_NUM_REGIONS
];
162 u32 bar_mask
[VFIO_PCI_NUM_REGIONS
];
163 struct list_head next
;
164 struct serial_port s
[2];
165 struct mutex rxtx_lock
;
166 struct vfio_device_info dev_info
;
168 enum vfio_device_mig_state state
;
169 struct mutex state_mutex
;
170 struct mutex reset_mutex
;
171 struct mtty_migration_file
*saving_migf
;
172 struct mtty_migration_file
*resuming_migf
;
177 static struct mtty_type
{
178 struct mdev_type type
;
181 { .nr_ports
= 1, .type
.sysfs_name
= "1",
182 .type
.pretty_name
= "Single port serial" },
183 { .nr_ports
= 2, .type
.sysfs_name
= "2",
184 .type
.pretty_name
= "Dual port serial" },
187 static struct mdev_type
*mtty_mdev_types
[] = {
192 static atomic_t mdev_avail_ports
= ATOMIC_INIT(MAX_MTTYS
);
194 static const struct file_operations vd_fops
= {
195 .owner
= THIS_MODULE
,
198 static const struct vfio_device_ops mtty_dev_ops
;
200 /* Helper functions */
202 static void dump_buffer(u8
*buf
, uint32_t count
)
207 pr_info("Buffer:\n");
208 for (i
= 0; i
< count
; i
++) {
209 pr_info("%2x ", *(buf
+ i
));
210 if ((i
+ 1) % 16 == 0)
216 static bool is_intx(struct mdev_state
*mdev_state
)
218 return mdev_state
->irq_index
== VFIO_PCI_INTX_IRQ_INDEX
;
221 static bool is_msi(struct mdev_state
*mdev_state
)
223 return mdev_state
->irq_index
== VFIO_PCI_MSI_IRQ_INDEX
;
226 static bool is_noirq(struct mdev_state
*mdev_state
)
228 return !is_intx(mdev_state
) && !is_msi(mdev_state
);
231 static void mtty_trigger_interrupt(struct mdev_state
*mdev_state
)
233 lockdep_assert_held(&mdev_state
->ops_lock
);
235 if (is_msi(mdev_state
)) {
236 if (mdev_state
->msi_evtfd
)
237 eventfd_signal(mdev_state
->msi_evtfd
);
238 } else if (is_intx(mdev_state
)) {
239 if (mdev_state
->intx_evtfd
&& !mdev_state
->intx_mask
) {
240 eventfd_signal(mdev_state
->intx_evtfd
);
241 mdev_state
->intx_mask
= true;
246 static void mtty_create_config_space(struct mdev_state
*mdev_state
)
249 STORE_LE32((u32
*) &mdev_state
->vconfig
[0x0], 0x32534348);
251 /* Control: I/O+, Mem-, BusMaster- */
252 STORE_LE16((u16
*) &mdev_state
->vconfig
[0x4], 0x0001);
254 /* Status: capabilities list absent */
255 STORE_LE16((u16
*) &mdev_state
->vconfig
[0x6], 0x0200);
258 mdev_state
->vconfig
[0x8] = 0x10;
260 /* programming interface class : 16550-compatible serial controller */
261 mdev_state
->vconfig
[0x9] = 0x02;
264 mdev_state
->vconfig
[0xa] = 0x00;
266 /* Base class : Simple Communication controllers */
267 mdev_state
->vconfig
[0xb] = 0x07;
269 /* base address registers */
271 STORE_LE32((u32
*) &mdev_state
->vconfig
[0x10], 0x000001);
272 mdev_state
->bar_mask
[0] = ~(MTTY_IO_BAR_SIZE
) + 1;
274 if (mdev_state
->nr_ports
== 2) {
276 STORE_LE32((u32
*) &mdev_state
->vconfig
[0x14], 0x000001);
277 mdev_state
->bar_mask
[1] = ~(MTTY_IO_BAR_SIZE
) + 1;
281 STORE_LE32((u32
*) &mdev_state
->vconfig
[0x2c], 0x32534348);
283 mdev_state
->vconfig
[0x34] = 0x00; /* Cap Ptr */
284 mdev_state
->vconfig
[0x3d] = 0x01; /* interrupt pin (INTA#) */
286 /* Vendor specific data */
287 mdev_state
->vconfig
[0x40] = 0x23;
288 mdev_state
->vconfig
[0x43] = 0x80;
289 mdev_state
->vconfig
[0x44] = 0x23;
290 mdev_state
->vconfig
[0x48] = 0x23;
291 mdev_state
->vconfig
[0x4c] = 0x23;
293 mdev_state
->vconfig
[0x60] = 0x50;
294 mdev_state
->vconfig
[0x61] = 0x43;
295 mdev_state
->vconfig
[0x62] = 0x49;
296 mdev_state
->vconfig
[0x63] = 0x20;
297 mdev_state
->vconfig
[0x64] = 0x53;
298 mdev_state
->vconfig
[0x65] = 0x65;
299 mdev_state
->vconfig
[0x66] = 0x72;
300 mdev_state
->vconfig
[0x67] = 0x69;
301 mdev_state
->vconfig
[0x68] = 0x61;
302 mdev_state
->vconfig
[0x69] = 0x6c;
303 mdev_state
->vconfig
[0x6a] = 0x2f;
304 mdev_state
->vconfig
[0x6b] = 0x55;
305 mdev_state
->vconfig
[0x6c] = 0x41;
306 mdev_state
->vconfig
[0x6d] = 0x52;
307 mdev_state
->vconfig
[0x6e] = 0x54;
310 static void handle_pci_cfg_write(struct mdev_state
*mdev_state
, u16 offset
,
313 u32 cfg_addr
, bar_mask
, bar_index
= 0;
316 case 0x04: /* device control */
317 case 0x06: /* device status */
320 case 0x3c: /* interrupt line */
321 mdev_state
->vconfig
[0x3c] = buf
[0];
325 * Interrupt Pin is hardwired to INTA.
326 * This field is write protected by hardware
329 case 0x10: /* BAR0 */
330 case 0x14: /* BAR1 */
333 else if (offset
== 0x14)
336 if ((mdev_state
->nr_ports
== 1) && (bar_index
== 1)) {
337 STORE_LE32(&mdev_state
->vconfig
[offset
], 0);
341 cfg_addr
= *(u32
*)buf
;
342 pr_info("BAR%d addr 0x%x\n", bar_index
, cfg_addr
);
344 if (cfg_addr
== 0xffffffff) {
345 bar_mask
= mdev_state
->bar_mask
[bar_index
];
346 cfg_addr
= (cfg_addr
& bar_mask
);
349 cfg_addr
|= (mdev_state
->vconfig
[offset
] & 0x3ul
);
350 STORE_LE32(&mdev_state
->vconfig
[offset
], cfg_addr
);
352 case 0x18: /* BAR2 */
353 case 0x1c: /* BAR3 */
354 case 0x20: /* BAR4 */
355 STORE_LE32(&mdev_state
->vconfig
[offset
], 0);
358 pr_info("PCI config write @0x%x of %d bytes not handled\n",
364 static void handle_bar_write(unsigned int index
, struct mdev_state
*mdev_state
,
365 u16 offset
, u8
*buf
, u32 count
)
369 /* Handle data written by guest */
372 /* if DLAB set, data is LSB of divisor */
373 if (mdev_state
->s
[index
].dlab
) {
374 mdev_state
->s
[index
].divisor
|= data
;
378 mutex_lock(&mdev_state
->rxtx_lock
);
380 /* save in TX buffer */
381 if (mdev_state
->s
[index
].rxtx
.count
<
382 mdev_state
->s
[index
].max_fifo_size
) {
383 mdev_state
->s
[index
].rxtx
.fifo
[
384 mdev_state
->s
[index
].rxtx
.head
] = data
;
385 mdev_state
->s
[index
].rxtx
.count
++;
386 CIRCULAR_BUF_INC_IDX(mdev_state
->s
[index
].rxtx
.head
);
387 mdev_state
->s
[index
].overrun
= false;
390 * Trigger interrupt if receive data interrupt is
391 * enabled and fifo reached trigger level
393 if ((mdev_state
->s
[index
].uart_reg
[UART_IER
] &
395 (mdev_state
->s
[index
].rxtx
.count
==
396 mdev_state
->s
[index
].intr_trigger_level
)) {
397 /* trigger interrupt */
398 #if defined(DEBUG_INTR)
399 pr_err("Serial port %d: Fifo level trigger\n",
402 mtty_trigger_interrupt(mdev_state
);
405 #if defined(DEBUG_INTR)
406 pr_err("Serial port %d: Buffer Overflow\n", index
);
408 mdev_state
->s
[index
].overrun
= true;
411 * Trigger interrupt if receiver line status interrupt
414 if (mdev_state
->s
[index
].uart_reg
[UART_IER
] &
416 mtty_trigger_interrupt(mdev_state
);
418 mutex_unlock(&mdev_state
->rxtx_lock
);
422 /* if DLAB set, data is MSB of divisor */
423 if (mdev_state
->s
[index
].dlab
)
424 mdev_state
->s
[index
].divisor
|= (u16
)data
<< 8;
426 mdev_state
->s
[index
].uart_reg
[offset
] = data
;
427 mutex_lock(&mdev_state
->rxtx_lock
);
428 if ((data
& UART_IER_THRI
) &&
429 (mdev_state
->s
[index
].rxtx
.head
==
430 mdev_state
->s
[index
].rxtx
.tail
)) {
431 #if defined(DEBUG_INTR)
432 pr_err("Serial port %d: IER_THRI write\n",
435 mtty_trigger_interrupt(mdev_state
);
438 mutex_unlock(&mdev_state
->rxtx_lock
);
444 mdev_state
->s
[index
].fcr
= data
;
446 mutex_lock(&mdev_state
->rxtx_lock
);
447 if (data
& (UART_FCR_CLEAR_RCVR
| UART_FCR_CLEAR_XMIT
)) {
448 /* clear loop back FIFO */
449 mdev_state
->s
[index
].rxtx
.count
= 0;
450 mdev_state
->s
[index
].rxtx
.head
= 0;
451 mdev_state
->s
[index
].rxtx
.tail
= 0;
453 mutex_unlock(&mdev_state
->rxtx_lock
);
455 switch (data
& UART_FCR_TRIGGER_MASK
) {
456 case UART_FCR_TRIGGER_1
:
457 mdev_state
->s
[index
].intr_trigger_level
= 1;
460 case UART_FCR_TRIGGER_4
:
461 mdev_state
->s
[index
].intr_trigger_level
= 4;
464 case UART_FCR_TRIGGER_8
:
465 mdev_state
->s
[index
].intr_trigger_level
= 8;
468 case UART_FCR_TRIGGER_14
:
469 mdev_state
->s
[index
].intr_trigger_level
= 14;
474 * Set trigger level to 1 otherwise or implement timer with
475 * timeout of 4 characters and on expiring that timer set
476 * Recevice data timeout in IIR register
478 mdev_state
->s
[index
].intr_trigger_level
= 1;
479 if (data
& UART_FCR_ENABLE_FIFO
)
480 mdev_state
->s
[index
].max_fifo_size
= MAX_FIFO_SIZE
;
482 mdev_state
->s
[index
].max_fifo_size
= 1;
483 mdev_state
->s
[index
].intr_trigger_level
= 1;
489 if (data
& UART_LCR_DLAB
) {
490 mdev_state
->s
[index
].dlab
= true;
491 mdev_state
->s
[index
].divisor
= 0;
493 mdev_state
->s
[index
].dlab
= false;
495 mdev_state
->s
[index
].uart_reg
[offset
] = data
;
499 mdev_state
->s
[index
].uart_reg
[offset
] = data
;
501 if ((mdev_state
->s
[index
].uart_reg
[UART_IER
] & UART_IER_MSI
) &&
502 (data
& UART_MCR_OUT2
)) {
503 #if defined(DEBUG_INTR)
504 pr_err("Serial port %d: MCR_OUT2 write\n", index
);
506 mtty_trigger_interrupt(mdev_state
);
509 if ((mdev_state
->s
[index
].uart_reg
[UART_IER
] & UART_IER_MSI
) &&
510 (data
& (UART_MCR_RTS
| UART_MCR_DTR
))) {
511 #if defined(DEBUG_INTR)
512 pr_err("Serial port %d: MCR RTS/DTR write\n", index
);
514 mtty_trigger_interrupt(mdev_state
);
524 mdev_state
->s
[index
].uart_reg
[offset
] = data
;
532 static void handle_bar_read(unsigned int index
, struct mdev_state
*mdev_state
,
533 u16 offset
, u8
*buf
, u32 count
)
535 /* Handle read requests by guest */
538 /* if DLAB set, data is LSB of divisor */
539 if (mdev_state
->s
[index
].dlab
) {
540 *buf
= (u8
)mdev_state
->s
[index
].divisor
;
544 mutex_lock(&mdev_state
->rxtx_lock
);
545 /* return data in tx buffer */
546 if (mdev_state
->s
[index
].rxtx
.head
!=
547 mdev_state
->s
[index
].rxtx
.tail
) {
548 *buf
= mdev_state
->s
[index
].rxtx
.fifo
[
549 mdev_state
->s
[index
].rxtx
.tail
];
550 mdev_state
->s
[index
].rxtx
.count
--;
551 CIRCULAR_BUF_INC_IDX(mdev_state
->s
[index
].rxtx
.tail
);
554 if (mdev_state
->s
[index
].rxtx
.head
==
555 mdev_state
->s
[index
].rxtx
.tail
) {
557 * Trigger interrupt if tx buffer empty interrupt is
558 * enabled and fifo is empty
560 #if defined(DEBUG_INTR)
561 pr_err("Serial port %d: Buffer Empty\n", index
);
563 if (mdev_state
->s
[index
].uart_reg
[UART_IER
] &
565 mtty_trigger_interrupt(mdev_state
);
567 mutex_unlock(&mdev_state
->rxtx_lock
);
572 if (mdev_state
->s
[index
].dlab
) {
573 *buf
= (u8
)(mdev_state
->s
[index
].divisor
>> 8);
576 *buf
= mdev_state
->s
[index
].uart_reg
[offset
] & 0x0f;
581 u8 ier
= mdev_state
->s
[index
].uart_reg
[UART_IER
];
584 mutex_lock(&mdev_state
->rxtx_lock
);
585 /* Interrupt priority 1: Parity, overrun, framing or break */
586 if ((ier
& UART_IER_RLSI
) && mdev_state
->s
[index
].overrun
)
587 *buf
|= UART_IIR_RLSI
;
589 /* Interrupt priority 2: Fifo trigger level reached */
590 if ((ier
& UART_IER_RDI
) &&
591 (mdev_state
->s
[index
].rxtx
.count
>=
592 mdev_state
->s
[index
].intr_trigger_level
))
593 *buf
|= UART_IIR_RDI
;
595 /* Interrupt priotiry 3: transmitter holding register empty */
596 if ((ier
& UART_IER_THRI
) &&
597 (mdev_state
->s
[index
].rxtx
.head
==
598 mdev_state
->s
[index
].rxtx
.tail
))
599 *buf
|= UART_IIR_THRI
;
601 /* Interrupt priotiry 4: Modem status: CTS, DSR, RI or DCD */
602 if ((ier
& UART_IER_MSI
) &&
603 (mdev_state
->s
[index
].uart_reg
[UART_MCR
] &
604 (UART_MCR_RTS
| UART_MCR_DTR
)))
605 *buf
|= UART_IIR_MSI
;
607 /* bit0: 0=> interrupt pending, 1=> no interrupt is pending */
609 *buf
= UART_IIR_NO_INT
;
611 /* set bit 6 & 7 to be 16550 compatible */
613 mutex_unlock(&mdev_state
->rxtx_lock
);
619 *buf
= mdev_state
->s
[index
].uart_reg
[offset
];
626 mutex_lock(&mdev_state
->rxtx_lock
);
627 /* atleast one char in FIFO */
628 if (mdev_state
->s
[index
].rxtx
.head
!=
629 mdev_state
->s
[index
].rxtx
.tail
)
632 /* if FIFO overrun */
633 if (mdev_state
->s
[index
].overrun
)
636 /* transmit FIFO empty and tramsitter empty */
637 if (mdev_state
->s
[index
].rxtx
.head
==
638 mdev_state
->s
[index
].rxtx
.tail
)
639 lsr
|= UART_LSR_TEMT
| UART_LSR_THRE
;
641 mutex_unlock(&mdev_state
->rxtx_lock
);
646 *buf
= UART_MSR_DSR
| UART_MSR_DDSR
| UART_MSR_DCD
;
648 mutex_lock(&mdev_state
->rxtx_lock
);
649 /* if AFE is 1 and FIFO have space, set CTS bit */
650 if (mdev_state
->s
[index
].uart_reg
[UART_MCR
] &
652 if (mdev_state
->s
[index
].rxtx
.count
<
653 mdev_state
->s
[index
].max_fifo_size
)
654 *buf
|= UART_MSR_CTS
| UART_MSR_DCTS
;
656 *buf
|= UART_MSR_CTS
| UART_MSR_DCTS
;
657 mutex_unlock(&mdev_state
->rxtx_lock
);
662 *buf
= mdev_state
->s
[index
].uart_reg
[offset
];
670 static void mdev_read_base(struct mdev_state
*mdev_state
)
673 u32 start_lo
, start_hi
;
676 pos
= PCI_BASE_ADDRESS_0
;
678 for (index
= 0; index
<= VFIO_PCI_BAR5_REGION_INDEX
; index
++) {
680 if (!mdev_state
->region_info
[index
].size
)
683 start_lo
= (*(u32
*)(mdev_state
->vconfig
+ pos
)) &
684 PCI_BASE_ADDRESS_MEM_MASK
;
685 mem_type
= (*(u32
*)(mdev_state
->vconfig
+ pos
)) &
686 PCI_BASE_ADDRESS_MEM_TYPE_MASK
;
689 case PCI_BASE_ADDRESS_MEM_TYPE_64
:
690 start_hi
= (*(u32
*)(mdev_state
->vconfig
+ pos
+ 4));
693 case PCI_BASE_ADDRESS_MEM_TYPE_32
:
694 case PCI_BASE_ADDRESS_MEM_TYPE_1M
:
695 /* 1M mem BAR treated as 32-bit BAR */
697 /* mem unknown type treated as 32-bit BAR */
702 mdev_state
->region_info
[index
].start
= ((u64
)start_hi
<< 32) |
707 static ssize_t
mdev_access(struct mdev_state
*mdev_state
, u8
*buf
, size_t count
,
708 loff_t pos
, bool is_write
)
717 mutex_lock(&mdev_state
->ops_lock
);
719 index
= MTTY_VFIO_PCI_OFFSET_TO_INDEX(pos
);
720 offset
= pos
& MTTY_VFIO_PCI_OFFSET_MASK
;
722 case VFIO_PCI_CONFIG_REGION_INDEX
:
725 pr_info("%s: PCI config space %s at offset 0x%llx\n",
726 __func__
, is_write
? "write" : "read", offset
);
729 dump_buffer(buf
, count
);
730 handle_pci_cfg_write(mdev_state
, offset
, buf
, count
);
732 memcpy(buf
, (mdev_state
->vconfig
+ offset
), count
);
733 dump_buffer(buf
, count
);
738 case VFIO_PCI_BAR0_REGION_INDEX
... VFIO_PCI_BAR5_REGION_INDEX
:
739 if (!mdev_state
->region_info
[index
].start
)
740 mdev_read_base(mdev_state
);
743 dump_buffer(buf
, count
);
745 #if defined(DEBUG_REGS)
746 pr_info("%s: BAR%d WR @0x%llx %s val:0x%02x dlab:%d\n",
747 __func__
, index
, offset
, wr_reg
[offset
],
748 *buf
, mdev_state
->s
[index
].dlab
);
750 handle_bar_write(index
, mdev_state
, offset
, buf
, count
);
752 handle_bar_read(index
, mdev_state
, offset
, buf
, count
);
753 dump_buffer(buf
, count
);
755 #if defined(DEBUG_REGS)
756 pr_info("%s: BAR%d RD @0x%llx %s val:0x%02x dlab:%d\n",
757 __func__
, index
, offset
, rd_reg
[offset
],
758 *buf
, mdev_state
->s
[index
].dlab
);
772 mutex_unlock(&mdev_state
->ops_lock
);
777 static size_t mtty_data_size(struct mdev_state
*mdev_state
)
779 return offsetof(struct mtty_data
, ports
) +
780 (mdev_state
->nr_ports
* sizeof(struct serial_port
));
783 static void mtty_disable_file(struct mtty_migration_file
*migf
)
785 mutex_lock(&migf
->lock
);
786 migf
->disabled
= true;
787 migf
->filled_size
= 0;
788 migf
->filp
->f_pos
= 0;
789 mutex_unlock(&migf
->lock
);
792 static void mtty_disable_files(struct mdev_state
*mdev_state
)
794 if (mdev_state
->saving_migf
) {
795 mtty_disable_file(mdev_state
->saving_migf
);
796 fput(mdev_state
->saving_migf
->filp
);
797 mdev_state
->saving_migf
= NULL
;
800 if (mdev_state
->resuming_migf
) {
801 mtty_disable_file(mdev_state
->resuming_migf
);
802 fput(mdev_state
->resuming_migf
->filp
);
803 mdev_state
->resuming_migf
= NULL
;
807 static void mtty_state_mutex_unlock(struct mdev_state
*mdev_state
)
810 mutex_lock(&mdev_state
->reset_mutex
);
811 if (mdev_state
->deferred_reset
) {
812 mdev_state
->deferred_reset
= false;
813 mutex_unlock(&mdev_state
->reset_mutex
);
814 mdev_state
->state
= VFIO_DEVICE_STATE_RUNNING
;
815 mtty_disable_files(mdev_state
);
818 mutex_unlock(&mdev_state
->state_mutex
);
819 mutex_unlock(&mdev_state
->reset_mutex
);
822 static int mtty_release_migf(struct inode
*inode
, struct file
*filp
)
824 struct mtty_migration_file
*migf
= filp
->private_data
;
826 mtty_disable_file(migf
);
827 mutex_destroy(&migf
->lock
);
833 static long mtty_precopy_ioctl(struct file
*filp
, unsigned int cmd
,
836 struct mtty_migration_file
*migf
= filp
->private_data
;
837 struct mdev_state
*mdev_state
= migf
->mdev_state
;
838 loff_t
*pos
= &filp
->f_pos
;
839 struct vfio_precopy_info info
= {};
843 if (cmd
!= VFIO_MIG_GET_PRECOPY_INFO
)
846 minsz
= offsetofend(struct vfio_precopy_info
, dirty_bytes
);
848 if (copy_from_user(&info
, (void __user
*)arg
, minsz
))
850 if (info
.argsz
< minsz
)
853 mutex_lock(&mdev_state
->state_mutex
);
854 if (mdev_state
->state
!= VFIO_DEVICE_STATE_PRE_COPY
&&
855 mdev_state
->state
!= VFIO_DEVICE_STATE_PRE_COPY_P2P
) {
860 mutex_lock(&migf
->lock
);
862 if (migf
->disabled
) {
863 mutex_unlock(&migf
->lock
);
868 if (*pos
> migf
->filled_size
) {
869 mutex_unlock(&migf
->lock
);
874 info
.dirty_bytes
= 0;
875 info
.initial_bytes
= migf
->filled_size
- *pos
;
876 mutex_unlock(&migf
->lock
);
878 ret
= copy_to_user((void __user
*)arg
, &info
, minsz
) ? -EFAULT
: 0;
880 mtty_state_mutex_unlock(mdev_state
);
884 static ssize_t
mtty_save_read(struct file
*filp
, char __user
*buf
,
885 size_t len
, loff_t
*pos
)
887 struct mtty_migration_file
*migf
= filp
->private_data
;
895 mutex_lock(&migf
->lock
);
897 dev_dbg(migf
->mdev_state
->vdev
.dev
, "%s ask %zu\n", __func__
, len
);
899 if (migf
->disabled
) {
904 if (*pos
> migf
->filled_size
) {
909 len
= min_t(size_t, migf
->filled_size
- *pos
, len
);
911 if (copy_to_user(buf
, (void *)&migf
->data
+ *pos
, len
)) {
919 dev_dbg(migf
->mdev_state
->vdev
.dev
, "%s read %zu\n", __func__
, ret
);
920 mutex_unlock(&migf
->lock
);
924 static const struct file_operations mtty_save_fops
= {
925 .owner
= THIS_MODULE
,
926 .read
= mtty_save_read
,
927 .unlocked_ioctl
= mtty_precopy_ioctl
,
928 .compat_ioctl
= compat_ptr_ioctl
,
929 .release
= mtty_release_migf
,
932 static void mtty_save_state(struct mdev_state
*mdev_state
)
934 struct mtty_migration_file
*migf
= mdev_state
->saving_migf
;
937 mutex_lock(&migf
->lock
);
938 for (i
= 0; i
< mdev_state
->nr_ports
; i
++) {
939 memcpy(&migf
->data
.ports
[i
],
940 &mdev_state
->s
[i
], sizeof(struct serial_port
));
941 migf
->filled_size
+= sizeof(struct serial_port
);
943 dev_dbg(mdev_state
->vdev
.dev
,
944 "%s filled to %zu\n", __func__
, migf
->filled_size
);
945 mutex_unlock(&migf
->lock
);
948 static int mtty_load_state(struct mdev_state
*mdev_state
)
950 struct mtty_migration_file
*migf
= mdev_state
->resuming_migf
;
953 mutex_lock(&migf
->lock
);
954 /* magic and version already tested by resume write fn */
955 if (migf
->filled_size
< mtty_data_size(mdev_state
)) {
956 dev_dbg(mdev_state
->vdev
.dev
, "%s expected %zu, got %zu\n",
957 __func__
, mtty_data_size(mdev_state
),
959 mutex_unlock(&migf
->lock
);
963 for (i
= 0; i
< mdev_state
->nr_ports
; i
++)
964 memcpy(&mdev_state
->s
[i
],
965 &migf
->data
.ports
[i
], sizeof(struct serial_port
));
967 mutex_unlock(&migf
->lock
);
971 static struct mtty_migration_file
*
972 mtty_save_device_data(struct mdev_state
*mdev_state
,
973 enum vfio_device_mig_state state
)
975 struct mtty_migration_file
*migf
= mdev_state
->saving_migf
;
976 struct mtty_migration_file
*ret
= NULL
;
979 if (state
== VFIO_DEVICE_STATE_STOP_COPY
)
984 migf
= kzalloc(sizeof(*migf
), GFP_KERNEL_ACCOUNT
);
986 return ERR_PTR(-ENOMEM
);
988 migf
->filp
= anon_inode_getfile("mtty_mig", &mtty_save_fops
,
990 if (IS_ERR(migf
->filp
)) {
991 int rc
= PTR_ERR(migf
->filp
);
997 stream_open(migf
->filp
->f_inode
, migf
->filp
);
998 mutex_init(&migf
->lock
);
999 migf
->mdev_state
= mdev_state
;
1001 migf
->data
.magic
= MTTY_MAGIC
;
1002 migf
->data
.major_ver
= MTTY_MAJOR_VER
;
1003 migf
->data
.minor_ver
= MTTY_MINOR_VER
;
1004 migf
->data
.nr_ports
= mdev_state
->nr_ports
;
1006 migf
->filled_size
= offsetof(struct mtty_data
, ports
);
1008 dev_dbg(mdev_state
->vdev
.dev
, "%s filled header to %zu\n",
1009 __func__
, migf
->filled_size
);
1011 ret
= mdev_state
->saving_migf
= migf
;
1014 if (state
== VFIO_DEVICE_STATE_STOP_COPY
)
1015 mtty_save_state(mdev_state
);
1020 static ssize_t
mtty_resume_write(struct file
*filp
, const char __user
*buf
,
1021 size_t len
, loff_t
*pos
)
1023 struct mtty_migration_file
*migf
= filp
->private_data
;
1024 struct mdev_state
*mdev_state
= migf
->mdev_state
;
1025 loff_t requested_length
;
1034 check_add_overflow((loff_t
)len
, *pos
, &requested_length
))
1037 if (requested_length
> mtty_data_size(mdev_state
))
1040 mutex_lock(&migf
->lock
);
1042 if (migf
->disabled
) {
1047 if (copy_from_user((void *)&migf
->data
+ *pos
, buf
, len
)) {
1055 dev_dbg(migf
->mdev_state
->vdev
.dev
, "%s received %zu, total %zu\n",
1056 __func__
, len
, migf
->filled_size
+ len
);
1058 if (migf
->filled_size
< offsetof(struct mtty_data
, ports
) &&
1059 migf
->filled_size
+ len
>= offsetof(struct mtty_data
, ports
)) {
1060 if (migf
->data
.magic
!= MTTY_MAGIC
|| migf
->data
.flags
||
1061 migf
->data
.major_ver
!= MTTY_MAJOR_VER
||
1062 migf
->data
.minor_ver
!= MTTY_MINOR_VER
||
1063 migf
->data
.nr_ports
!= mdev_state
->nr_ports
) {
1064 dev_dbg(migf
->mdev_state
->vdev
.dev
,
1065 "%s failed validation\n", __func__
);
1068 dev_dbg(migf
->mdev_state
->vdev
.dev
,
1069 "%s header validated\n", __func__
);
1073 migf
->filled_size
+= len
;
1076 mutex_unlock(&migf
->lock
);
1080 static const struct file_operations mtty_resume_fops
= {
1081 .owner
= THIS_MODULE
,
1082 .write
= mtty_resume_write
,
1083 .release
= mtty_release_migf
,
1086 static struct mtty_migration_file
*
1087 mtty_resume_device_data(struct mdev_state
*mdev_state
)
1089 struct mtty_migration_file
*migf
;
1092 migf
= kzalloc(sizeof(*migf
), GFP_KERNEL_ACCOUNT
);
1094 return ERR_PTR(-ENOMEM
);
1096 migf
->filp
= anon_inode_getfile("mtty_mig", &mtty_resume_fops
,
1098 if (IS_ERR(migf
->filp
)) {
1099 ret
= PTR_ERR(migf
->filp
);
1101 return ERR_PTR(ret
);
1104 stream_open(migf
->filp
->f_inode
, migf
->filp
);
1105 mutex_init(&migf
->lock
);
1106 migf
->mdev_state
= mdev_state
;
1108 mdev_state
->resuming_migf
= migf
;
1113 static struct file
*mtty_step_state(struct mdev_state
*mdev_state
,
1114 enum vfio_device_mig_state
new)
1116 enum vfio_device_mig_state cur
= mdev_state
->state
;
1118 dev_dbg(mdev_state
->vdev
.dev
, "%s: %d -> %d\n", __func__
, cur
, new);
1121 * The following state transitions are no-op considering
1122 * mtty does not do DMA nor require any explicit start/stop.
1124 * RUNNING -> RUNNING_P2P
1125 * RUNNING_P2P -> RUNNING
1126 * RUNNING_P2P -> STOP
1127 * PRE_COPY -> PRE_COPY_P2P
1128 * PRE_COPY_P2P -> PRE_COPY
1129 * STOP -> RUNNING_P2P
1131 if ((cur
== VFIO_DEVICE_STATE_RUNNING
&&
1132 new == VFIO_DEVICE_STATE_RUNNING_P2P
) ||
1133 (cur
== VFIO_DEVICE_STATE_RUNNING_P2P
&&
1134 (new == VFIO_DEVICE_STATE_RUNNING
||
1135 new == VFIO_DEVICE_STATE_STOP
)) ||
1136 (cur
== VFIO_DEVICE_STATE_PRE_COPY
&&
1137 new == VFIO_DEVICE_STATE_PRE_COPY_P2P
) ||
1138 (cur
== VFIO_DEVICE_STATE_PRE_COPY_P2P
&&
1139 new == VFIO_DEVICE_STATE_PRE_COPY
) ||
1140 (cur
== VFIO_DEVICE_STATE_STOP
&&
1141 new == VFIO_DEVICE_STATE_RUNNING_P2P
))
1145 * The following state transitions simply close migration files,
1146 * with the exception of RESUMING -> STOP, which needs to load
1150 * PRE_COPY -> RUNNING
1151 * PRE_COPY_P2P -> RUNNING_P2P
1154 if (cur
== VFIO_DEVICE_STATE_RESUMING
&&
1155 new == VFIO_DEVICE_STATE_STOP
) {
1158 ret
= mtty_load_state(mdev_state
);
1160 return ERR_PTR(ret
);
1161 mtty_disable_files(mdev_state
);
1165 if ((cur
== VFIO_DEVICE_STATE_PRE_COPY
&&
1166 new == VFIO_DEVICE_STATE_RUNNING
) ||
1167 (cur
== VFIO_DEVICE_STATE_PRE_COPY_P2P
&&
1168 new == VFIO_DEVICE_STATE_RUNNING_P2P
) ||
1169 (cur
== VFIO_DEVICE_STATE_STOP_COPY
&&
1170 new == VFIO_DEVICE_STATE_STOP
)) {
1171 mtty_disable_files(mdev_state
);
1176 * The following state transitions return migration files.
1178 * RUNNING -> PRE_COPY
1179 * RUNNING_P2P -> PRE_COPY_P2P
1182 * PRE_COPY_P2P -> STOP_COPY
1184 if ((cur
== VFIO_DEVICE_STATE_RUNNING
&&
1185 new == VFIO_DEVICE_STATE_PRE_COPY
) ||
1186 (cur
== VFIO_DEVICE_STATE_RUNNING_P2P
&&
1187 new == VFIO_DEVICE_STATE_PRE_COPY_P2P
) ||
1188 (cur
== VFIO_DEVICE_STATE_STOP
&&
1189 new == VFIO_DEVICE_STATE_STOP_COPY
) ||
1190 (cur
== VFIO_DEVICE_STATE_PRE_COPY_P2P
&&
1191 new == VFIO_DEVICE_STATE_STOP_COPY
)) {
1192 struct mtty_migration_file
*migf
;
1194 migf
= mtty_save_device_data(mdev_state
, new);
1196 return ERR_CAST(migf
);
1199 get_file(migf
->filp
);
1206 if (cur
== VFIO_DEVICE_STATE_STOP
&&
1207 new == VFIO_DEVICE_STATE_RESUMING
) {
1208 struct mtty_migration_file
*migf
;
1210 migf
= mtty_resume_device_data(mdev_state
);
1212 return ERR_CAST(migf
);
1214 get_file(migf
->filp
);
1219 /* vfio_mig_get_next_state() does not use arcs other than the above */
1221 return ERR_PTR(-EINVAL
);
1224 static struct file
*mtty_set_state(struct vfio_device
*vdev
,
1225 enum vfio_device_mig_state new_state
)
1227 struct mdev_state
*mdev_state
=
1228 container_of(vdev
, struct mdev_state
, vdev
);
1229 struct file
*ret
= NULL
;
1231 dev_dbg(vdev
->dev
, "%s -> %d\n", __func__
, new_state
);
1233 mutex_lock(&mdev_state
->state_mutex
);
1234 while (mdev_state
->state
!= new_state
) {
1235 enum vfio_device_mig_state next_state
;
1236 int rc
= vfio_mig_get_next_state(vdev
, mdev_state
->state
,
1237 new_state
, &next_state
);
1243 ret
= mtty_step_state(mdev_state
, next_state
);
1247 mdev_state
->state
= next_state
;
1249 if (WARN_ON(ret
&& new_state
!= next_state
)) {
1251 ret
= ERR_PTR(-EINVAL
);
1255 mtty_state_mutex_unlock(mdev_state
);
1259 static int mtty_get_state(struct vfio_device
*vdev
,
1260 enum vfio_device_mig_state
*current_state
)
1262 struct mdev_state
*mdev_state
=
1263 container_of(vdev
, struct mdev_state
, vdev
);
1265 mutex_lock(&mdev_state
->state_mutex
);
1266 *current_state
= mdev_state
->state
;
1267 mtty_state_mutex_unlock(mdev_state
);
1271 static int mtty_get_data_size(struct vfio_device
*vdev
,
1272 unsigned long *stop_copy_length
)
1274 struct mdev_state
*mdev_state
=
1275 container_of(vdev
, struct mdev_state
, vdev
);
1277 *stop_copy_length
= mtty_data_size(mdev_state
);
1281 static const struct vfio_migration_ops mtty_migration_ops
= {
1282 .migration_set_state
= mtty_set_state
,
1283 .migration_get_state
= mtty_get_state
,
1284 .migration_get_data_size
= mtty_get_data_size
,
1287 static int mtty_log_start(struct vfio_device
*vdev
,
1288 struct rb_root_cached
*ranges
,
1289 u32 nnodes
, u64
*page_size
)
1294 static int mtty_log_stop(struct vfio_device
*vdev
)
1299 static int mtty_log_read_and_clear(struct vfio_device
*vdev
,
1300 unsigned long iova
, unsigned long length
,
1301 struct iova_bitmap
*dirty
)
1306 static const struct vfio_log_ops mtty_log_ops
= {
1307 .log_start
= mtty_log_start
,
1308 .log_stop
= mtty_log_stop
,
1309 .log_read_and_clear
= mtty_log_read_and_clear
,
1312 static int mtty_init_dev(struct vfio_device
*vdev
)
1314 struct mdev_state
*mdev_state
=
1315 container_of(vdev
, struct mdev_state
, vdev
);
1316 struct mdev_device
*mdev
= to_mdev_device(vdev
->dev
);
1317 struct mtty_type
*type
=
1318 container_of(mdev
->type
, struct mtty_type
, type
);
1319 int avail_ports
= atomic_read(&mdev_avail_ports
);
1323 if (avail_ports
< type
->nr_ports
)
1325 } while (!atomic_try_cmpxchg(&mdev_avail_ports
,
1327 avail_ports
- type
->nr_ports
));
1329 mdev_state
->nr_ports
= type
->nr_ports
;
1330 mdev_state
->irq_index
= -1;
1331 mdev_state
->s
[0].max_fifo_size
= MAX_FIFO_SIZE
;
1332 mdev_state
->s
[1].max_fifo_size
= MAX_FIFO_SIZE
;
1333 mutex_init(&mdev_state
->rxtx_lock
);
1335 mdev_state
->vconfig
= kzalloc(MTTY_CONFIG_SPACE_SIZE
, GFP_KERNEL
);
1336 if (!mdev_state
->vconfig
) {
1341 mutex_init(&mdev_state
->ops_lock
);
1342 mdev_state
->mdev
= mdev
;
1343 mtty_create_config_space(mdev_state
);
1345 mutex_init(&mdev_state
->state_mutex
);
1346 mutex_init(&mdev_state
->reset_mutex
);
1347 vdev
->migration_flags
= VFIO_MIGRATION_STOP_COPY
|
1348 VFIO_MIGRATION_P2P
|
1349 VFIO_MIGRATION_PRE_COPY
;
1350 vdev
->mig_ops
= &mtty_migration_ops
;
1351 vdev
->log_ops
= &mtty_log_ops
;
1352 mdev_state
->state
= VFIO_DEVICE_STATE_RUNNING
;
1357 atomic_add(type
->nr_ports
, &mdev_avail_ports
);
1361 static int mtty_probe(struct mdev_device
*mdev
)
1363 struct mdev_state
*mdev_state
;
1366 mdev_state
= vfio_alloc_device(mdev_state
, vdev
, &mdev
->dev
,
1368 if (IS_ERR(mdev_state
))
1369 return PTR_ERR(mdev_state
);
1371 ret
= vfio_register_emulated_iommu_dev(&mdev_state
->vdev
);
1374 dev_set_drvdata(&mdev
->dev
, mdev_state
);
1378 vfio_put_device(&mdev_state
->vdev
);
1382 static void mtty_release_dev(struct vfio_device
*vdev
)
1384 struct mdev_state
*mdev_state
=
1385 container_of(vdev
, struct mdev_state
, vdev
);
1387 mutex_destroy(&mdev_state
->reset_mutex
);
1388 mutex_destroy(&mdev_state
->state_mutex
);
1389 atomic_add(mdev_state
->nr_ports
, &mdev_avail_ports
);
1390 kfree(mdev_state
->vconfig
);
1393 static void mtty_remove(struct mdev_device
*mdev
)
1395 struct mdev_state
*mdev_state
= dev_get_drvdata(&mdev
->dev
);
1397 vfio_unregister_group_dev(&mdev_state
->vdev
);
1398 vfio_put_device(&mdev_state
->vdev
);
1401 static int mtty_reset(struct mdev_state
*mdev_state
)
1403 pr_info("%s: called\n", __func__
);
1405 mutex_lock(&mdev_state
->reset_mutex
);
1406 mdev_state
->deferred_reset
= true;
1407 if (!mutex_trylock(&mdev_state
->state_mutex
)) {
1408 mutex_unlock(&mdev_state
->reset_mutex
);
1411 mutex_unlock(&mdev_state
->reset_mutex
);
1412 mtty_state_mutex_unlock(mdev_state
);
1417 static ssize_t
mtty_read(struct vfio_device
*vdev
, char __user
*buf
,
1418 size_t count
, loff_t
*ppos
)
1420 struct mdev_state
*mdev_state
=
1421 container_of(vdev
, struct mdev_state
, vdev
);
1422 unsigned int done
= 0;
1428 if (count
>= 4 && !(*ppos
% 4)) {
1431 ret
= mdev_access(mdev_state
, (u8
*)&val
, sizeof(val
),
1436 if (copy_to_user(buf
, &val
, sizeof(val
)))
1440 } else if (count
>= 2 && !(*ppos
% 2)) {
1443 ret
= mdev_access(mdev_state
, (u8
*)&val
, sizeof(val
),
1448 if (copy_to_user(buf
, &val
, sizeof(val
)))
1455 ret
= mdev_access(mdev_state
, (u8
*)&val
, sizeof(val
),
1460 if (copy_to_user(buf
, &val
, sizeof(val
)))
1478 static ssize_t
mtty_write(struct vfio_device
*vdev
, const char __user
*buf
,
1479 size_t count
, loff_t
*ppos
)
1481 struct mdev_state
*mdev_state
=
1482 container_of(vdev
, struct mdev_state
, vdev
);
1483 unsigned int done
= 0;
1489 if (count
>= 4 && !(*ppos
% 4)) {
1492 if (copy_from_user(&val
, buf
, sizeof(val
)))
1495 ret
= mdev_access(mdev_state
, (u8
*)&val
, sizeof(val
),
1501 } else if (count
>= 2 && !(*ppos
% 2)) {
1504 if (copy_from_user(&val
, buf
, sizeof(val
)))
1507 ret
= mdev_access(mdev_state
, (u8
*)&val
, sizeof(val
),
1516 if (copy_from_user(&val
, buf
, sizeof(val
)))
1519 ret
= mdev_access(mdev_state
, (u8
*)&val
, sizeof(val
),
1537 static void mtty_disable_intx(struct mdev_state
*mdev_state
)
1539 if (mdev_state
->intx_evtfd
) {
1540 eventfd_ctx_put(mdev_state
->intx_evtfd
);
1541 mdev_state
->intx_evtfd
= NULL
;
1542 mdev_state
->intx_mask
= false;
1543 mdev_state
->irq_index
= -1;
1547 static void mtty_disable_msi(struct mdev_state
*mdev_state
)
1549 if (mdev_state
->msi_evtfd
) {
1550 eventfd_ctx_put(mdev_state
->msi_evtfd
);
1551 mdev_state
->msi_evtfd
= NULL
;
1552 mdev_state
->irq_index
= -1;
1556 static int mtty_set_irqs(struct mdev_state
*mdev_state
, uint32_t flags
,
1557 unsigned int index
, unsigned int start
,
1558 unsigned int count
, void *data
)
1562 mutex_lock(&mdev_state
->ops_lock
);
1564 case VFIO_PCI_INTX_IRQ_INDEX
:
1565 switch (flags
& VFIO_IRQ_SET_ACTION_TYPE_MASK
) {
1566 case VFIO_IRQ_SET_ACTION_MASK
:
1567 if (!is_intx(mdev_state
) || start
!= 0 || count
!= 1) {
1572 if (flags
& VFIO_IRQ_SET_DATA_NONE
) {
1573 mdev_state
->intx_mask
= true;
1574 } else if (flags
& VFIO_IRQ_SET_DATA_BOOL
) {
1575 uint8_t mask
= *(uint8_t *)data
;
1578 mdev_state
->intx_mask
= true;
1579 } else if (flags
& VFIO_IRQ_SET_DATA_EVENTFD
) {
1580 ret
= -ENOTTY
; /* No support for mask fd */
1583 case VFIO_IRQ_SET_ACTION_UNMASK
:
1584 if (!is_intx(mdev_state
) || start
!= 0 || count
!= 1) {
1589 if (flags
& VFIO_IRQ_SET_DATA_NONE
) {
1590 mdev_state
->intx_mask
= false;
1591 } else if (flags
& VFIO_IRQ_SET_DATA_BOOL
) {
1592 uint8_t mask
= *(uint8_t *)data
;
1595 mdev_state
->intx_mask
= false;
1596 } else if (flags
& VFIO_IRQ_SET_DATA_EVENTFD
) {
1597 ret
= -ENOTTY
; /* No support for unmask fd */
1600 case VFIO_IRQ_SET_ACTION_TRIGGER
:
1601 if (is_intx(mdev_state
) && !count
&&
1602 (flags
& VFIO_IRQ_SET_DATA_NONE
)) {
1603 mtty_disable_intx(mdev_state
);
1607 if (!(is_intx(mdev_state
) || is_noirq(mdev_state
)) ||
1608 start
!= 0 || count
!= 1) {
1613 if (flags
& VFIO_IRQ_SET_DATA_EVENTFD
) {
1614 int fd
= *(int *)data
;
1615 struct eventfd_ctx
*evt
;
1617 mtty_disable_intx(mdev_state
);
1622 evt
= eventfd_ctx_fdget(fd
);
1627 mdev_state
->intx_evtfd
= evt
;
1628 mdev_state
->irq_index
= index
;
1632 if (!is_intx(mdev_state
)) {
1637 if (flags
& VFIO_IRQ_SET_DATA_NONE
) {
1638 mtty_trigger_interrupt(mdev_state
);
1639 } else if (flags
& VFIO_IRQ_SET_DATA_BOOL
) {
1640 uint8_t trigger
= *(uint8_t *)data
;
1643 mtty_trigger_interrupt(mdev_state
);
1648 case VFIO_PCI_MSI_IRQ_INDEX
:
1649 switch (flags
& VFIO_IRQ_SET_ACTION_TYPE_MASK
) {
1650 case VFIO_IRQ_SET_ACTION_MASK
:
1651 case VFIO_IRQ_SET_ACTION_UNMASK
:
1654 case VFIO_IRQ_SET_ACTION_TRIGGER
:
1655 if (is_msi(mdev_state
) && !count
&&
1656 (flags
& VFIO_IRQ_SET_DATA_NONE
)) {
1657 mtty_disable_msi(mdev_state
);
1661 if (!(is_msi(mdev_state
) || is_noirq(mdev_state
)) ||
1662 start
!= 0 || count
!= 1) {
1667 if (flags
& VFIO_IRQ_SET_DATA_EVENTFD
) {
1668 int fd
= *(int *)data
;
1669 struct eventfd_ctx
*evt
;
1671 mtty_disable_msi(mdev_state
);
1676 evt
= eventfd_ctx_fdget(fd
);
1681 mdev_state
->msi_evtfd
= evt
;
1682 mdev_state
->irq_index
= index
;
1686 if (!is_msi(mdev_state
)) {
1691 if (flags
& VFIO_IRQ_SET_DATA_NONE
) {
1692 mtty_trigger_interrupt(mdev_state
);
1693 } else if (flags
& VFIO_IRQ_SET_DATA_BOOL
) {
1694 uint8_t trigger
= *(uint8_t *)data
;
1697 mtty_trigger_interrupt(mdev_state
);
1702 case VFIO_PCI_MSIX_IRQ_INDEX
:
1703 dev_dbg(mdev_state
->vdev
.dev
, "%s: MSIX_IRQ\n", __func__
);
1706 case VFIO_PCI_ERR_IRQ_INDEX
:
1707 dev_dbg(mdev_state
->vdev
.dev
, "%s: ERR_IRQ\n", __func__
);
1710 case VFIO_PCI_REQ_IRQ_INDEX
:
1711 dev_dbg(mdev_state
->vdev
.dev
, "%s: REQ_IRQ\n", __func__
);
1716 mutex_unlock(&mdev_state
->ops_lock
);
1720 static int mtty_get_region_info(struct mdev_state
*mdev_state
,
1721 struct vfio_region_info
*region_info
,
1722 u16
*cap_type_id
, void **cap_type
)
1724 unsigned int size
= 0;
1727 bar_index
= region_info
->index
;
1728 if (bar_index
>= VFIO_PCI_NUM_REGIONS
)
1731 mutex_lock(&mdev_state
->ops_lock
);
1733 switch (bar_index
) {
1734 case VFIO_PCI_CONFIG_REGION_INDEX
:
1735 size
= MTTY_CONFIG_SPACE_SIZE
;
1737 case VFIO_PCI_BAR0_REGION_INDEX
:
1738 size
= MTTY_IO_BAR_SIZE
;
1740 case VFIO_PCI_BAR1_REGION_INDEX
:
1741 if (mdev_state
->nr_ports
== 2)
1742 size
= MTTY_IO_BAR_SIZE
;
1749 mdev_state
->region_info
[bar_index
].size
= size
;
1750 mdev_state
->region_info
[bar_index
].vfio_offset
=
1751 MTTY_VFIO_PCI_INDEX_TO_OFFSET(bar_index
);
1753 region_info
->size
= size
;
1754 region_info
->offset
= MTTY_VFIO_PCI_INDEX_TO_OFFSET(bar_index
);
1755 region_info
->flags
= VFIO_REGION_INFO_FLAG_READ
|
1756 VFIO_REGION_INFO_FLAG_WRITE
;
1757 mutex_unlock(&mdev_state
->ops_lock
);
1761 static int mtty_get_irq_info(struct vfio_irq_info
*irq_info
)
1763 if (irq_info
->index
!= VFIO_PCI_INTX_IRQ_INDEX
&&
1764 irq_info
->index
!= VFIO_PCI_MSI_IRQ_INDEX
)
1767 irq_info
->flags
= VFIO_IRQ_INFO_EVENTFD
;
1768 irq_info
->count
= 1;
1770 if (irq_info
->index
== VFIO_PCI_INTX_IRQ_INDEX
)
1771 irq_info
->flags
|= VFIO_IRQ_INFO_MASKABLE
|
1772 VFIO_IRQ_INFO_AUTOMASKED
;
1774 irq_info
->flags
|= VFIO_IRQ_INFO_NORESIZE
;
1779 static int mtty_get_device_info(struct vfio_device_info
*dev_info
)
1781 dev_info
->flags
= VFIO_DEVICE_FLAGS_PCI
;
1782 dev_info
->num_regions
= VFIO_PCI_NUM_REGIONS
;
1783 dev_info
->num_irqs
= VFIO_PCI_NUM_IRQS
;
1788 static long mtty_ioctl(struct vfio_device
*vdev
, unsigned int cmd
,
1791 struct mdev_state
*mdev_state
=
1792 container_of(vdev
, struct mdev_state
, vdev
);
1794 unsigned long minsz
;
1797 case VFIO_DEVICE_GET_INFO
:
1799 struct vfio_device_info info
;
1801 minsz
= offsetofend(struct vfio_device_info
, num_irqs
);
1803 if (copy_from_user(&info
, (void __user
*)arg
, minsz
))
1806 if (info
.argsz
< minsz
)
1809 ret
= mtty_get_device_info(&info
);
1813 memcpy(&mdev_state
->dev_info
, &info
, sizeof(info
));
1815 if (copy_to_user((void __user
*)arg
, &info
, minsz
))
1820 case VFIO_DEVICE_GET_REGION_INFO
:
1822 struct vfio_region_info info
;
1823 u16 cap_type_id
= 0;
1824 void *cap_type
= NULL
;
1826 minsz
= offsetofend(struct vfio_region_info
, offset
);
1828 if (copy_from_user(&info
, (void __user
*)arg
, minsz
))
1831 if (info
.argsz
< minsz
)
1834 ret
= mtty_get_region_info(mdev_state
, &info
, &cap_type_id
,
1839 if (copy_to_user((void __user
*)arg
, &info
, minsz
))
1845 case VFIO_DEVICE_GET_IRQ_INFO
:
1847 struct vfio_irq_info info
;
1849 minsz
= offsetofend(struct vfio_irq_info
, count
);
1851 if (copy_from_user(&info
, (void __user
*)arg
, minsz
))
1854 if ((info
.argsz
< minsz
) ||
1855 (info
.index
>= mdev_state
->dev_info
.num_irqs
))
1858 ret
= mtty_get_irq_info(&info
);
1862 if (copy_to_user((void __user
*)arg
, &info
, minsz
))
1867 case VFIO_DEVICE_SET_IRQS
:
1869 struct vfio_irq_set hdr
;
1870 u8
*data
= NULL
, *ptr
= NULL
;
1871 size_t data_size
= 0;
1873 minsz
= offsetofend(struct vfio_irq_set
, count
);
1875 if (copy_from_user(&hdr
, (void __user
*)arg
, minsz
))
1878 ret
= vfio_set_irqs_validate_and_prepare(&hdr
,
1879 mdev_state
->dev_info
.num_irqs
,
1886 ptr
= data
= memdup_user((void __user
*)(arg
+ minsz
),
1889 return PTR_ERR(data
);
1892 ret
= mtty_set_irqs(mdev_state
, hdr
.flags
, hdr
.index
, hdr
.start
,
1898 case VFIO_DEVICE_RESET
:
1899 return mtty_reset(mdev_state
);
1905 sample_mdev_dev_show(struct device
*dev
, struct device_attribute
*attr
,
1908 return sprintf(buf
, "This is MDEV %s\n", dev_name(dev
));
1911 static DEVICE_ATTR_RO(sample_mdev_dev
);
1913 static struct attribute
*mdev_dev_attrs
[] = {
1914 &dev_attr_sample_mdev_dev
.attr
,
1918 static const struct attribute_group mdev_dev_group
= {
1920 .attrs
= mdev_dev_attrs
,
1923 static const struct attribute_group
*mdev_dev_groups
[] = {
1928 static unsigned int mtty_get_available(struct mdev_type
*mtype
)
1930 struct mtty_type
*type
= container_of(mtype
, struct mtty_type
, type
);
1932 return atomic_read(&mdev_avail_ports
) / type
->nr_ports
;
1935 static void mtty_close(struct vfio_device
*vdev
)
1937 struct mdev_state
*mdev_state
=
1938 container_of(vdev
, struct mdev_state
, vdev
);
1940 mtty_disable_files(mdev_state
);
1941 mtty_disable_intx(mdev_state
);
1942 mtty_disable_msi(mdev_state
);
1945 static const struct vfio_device_ops mtty_dev_ops
= {
1946 .name
= "vfio-mtty",
1947 .init
= mtty_init_dev
,
1948 .release
= mtty_release_dev
,
1950 .write
= mtty_write
,
1951 .ioctl
= mtty_ioctl
,
1952 .bind_iommufd
= vfio_iommufd_emulated_bind
,
1953 .unbind_iommufd
= vfio_iommufd_emulated_unbind
,
1954 .attach_ioas
= vfio_iommufd_emulated_attach_ioas
,
1955 .detach_ioas
= vfio_iommufd_emulated_detach_ioas
,
1956 .close_device
= mtty_close
,
1959 static struct mdev_driver mtty_driver
= {
1960 .device_api
= VFIO_DEVICE_API_PCI_STRING
,
1963 .owner
= THIS_MODULE
,
1964 .mod_name
= KBUILD_MODNAME
,
1965 .dev_groups
= mdev_dev_groups
,
1967 .probe
= mtty_probe
,
1968 .remove
= mtty_remove
,
1969 .get_available
= mtty_get_available
,
1972 static void mtty_device_release(struct device
*dev
)
1974 dev_dbg(dev
, "mtty: released\n");
1977 static int __init
mtty_dev_init(void)
1981 pr_info("mtty_dev: %s\n", __func__
);
1983 memset(&mtty_dev
, 0, sizeof(mtty_dev
));
1985 idr_init(&mtty_dev
.vd_idr
);
1987 ret
= alloc_chrdev_region(&mtty_dev
.vd_devt
, 0, MINORMASK
+ 1,
1991 pr_err("Error: failed to register mtty_dev, err:%d\n", ret
);
1995 cdev_init(&mtty_dev
.vd_cdev
, &vd_fops
);
1996 cdev_add(&mtty_dev
.vd_cdev
, mtty_dev
.vd_devt
, MINORMASK
+ 1);
1998 pr_info("major_number:%d\n", MAJOR(mtty_dev
.vd_devt
));
2000 ret
= mdev_register_driver(&mtty_driver
);
2004 mtty_dev
.vd_class
= class_create(MTTY_CLASS_NAME
);
2006 if (IS_ERR(mtty_dev
.vd_class
)) {
2007 pr_err("Error: failed to register mtty_dev class\n");
2008 ret
= PTR_ERR(mtty_dev
.vd_class
);
2012 mtty_dev
.dev
.class = mtty_dev
.vd_class
;
2013 mtty_dev
.dev
.release
= mtty_device_release
;
2014 dev_set_name(&mtty_dev
.dev
, "%s", MTTY_NAME
);
2016 ret
= device_register(&mtty_dev
.dev
);
2020 ret
= mdev_register_parent(&mtty_dev
.parent
, &mtty_dev
.dev
,
2021 &mtty_driver
, mtty_mdev_types
,
2022 ARRAY_SIZE(mtty_mdev_types
));
2028 device_del(&mtty_dev
.dev
);
2030 put_device(&mtty_dev
.dev
);
2031 class_destroy(mtty_dev
.vd_class
);
2033 mdev_unregister_driver(&mtty_driver
);
2035 cdev_del(&mtty_dev
.vd_cdev
);
2036 unregister_chrdev_region(mtty_dev
.vd_devt
, MINORMASK
+ 1);
2040 static void __exit
mtty_dev_exit(void)
2042 mtty_dev
.dev
.bus
= NULL
;
2043 mdev_unregister_parent(&mtty_dev
.parent
);
2045 device_unregister(&mtty_dev
.dev
);
2046 idr_destroy(&mtty_dev
.vd_idr
);
2047 mdev_unregister_driver(&mtty_driver
);
2048 cdev_del(&mtty_dev
.vd_cdev
);
2049 unregister_chrdev_region(mtty_dev
.vd_devt
, MINORMASK
+ 1);
2050 class_destroy(mtty_dev
.vd_class
);
2051 mtty_dev
.vd_class
= NULL
;
2052 pr_info("mtty_dev: Unloaded!\n");
2055 module_init(mtty_dev_init
)
2056 module_exit(mtty_dev_exit
)
2058 MODULE_LICENSE("GPL v2");
2059 MODULE_DESCRIPTION("Test driver that simulate serial port over PCI");
2060 MODULE_VERSION(VERSION_STRING
);
2061 MODULE_AUTHOR(DRIVER_AUTHOR
);