1 // SPDX-License-Identifier: GPL-2.0-only
3 * VFIO PCI I/O Port & MMIO access
5 * Copyright (C) 2012 Red Hat, Inc. All rights reserved.
6 * Author: Alex Williamson <alex.williamson@redhat.com>
8 * Derived from original vfio:
9 * Copyright 2010 Cisco Systems, Inc. All rights reserved.
10 * Author: Tom Lyon, pugs@cisco.com
14 #include <linux/pci.h>
15 #include <linux/uaccess.h>
17 #include <linux/vfio.h>
18 #include <linux/vgaarb.h>
20 #include "vfio_pci_private.h"
22 #ifdef __LITTLE_ENDIAN
23 #define vfio_ioread64 ioread64
24 #define vfio_iowrite64 iowrite64
25 #define vfio_ioread32 ioread32
26 #define vfio_iowrite32 iowrite32
27 #define vfio_ioread16 ioread16
28 #define vfio_iowrite16 iowrite16
30 #define vfio_ioread64 ioread64be
31 #define vfio_iowrite64 iowrite64be
32 #define vfio_ioread32 ioread32be
33 #define vfio_iowrite32 iowrite32be
34 #define vfio_ioread16 ioread16be
35 #define vfio_iowrite16 iowrite16be
37 #define vfio_ioread8 ioread8
38 #define vfio_iowrite8 iowrite8
41 * Read or write from an __iomem region (MMIO or I/O port) with an excluded
42 * range which is inaccessible. The excluded range drops writes and fills
43 * reads with -1. This is intended for handling MSI-X vector tables and
44 * leftover space for ROM BARs.
46 static ssize_t
do_io_rw(void __iomem
*io
, char __user
*buf
,
47 loff_t off
, size_t count
, size_t x_start
,
48 size_t x_end
, bool iswrite
)
53 size_t fillable
, filled
;
56 fillable
= min(count
, (size_t)(x_start
- off
));
57 else if (off
>= x_end
)
62 if (fillable
>= 4 && !(off
% 4)) {
66 if (copy_from_user(&val
, buf
, 4))
69 vfio_iowrite32(val
, io
+ off
);
71 val
= vfio_ioread32(io
+ off
);
73 if (copy_to_user(buf
, &val
, 4))
78 } else if (fillable
>= 2 && !(off
% 2)) {
82 if (copy_from_user(&val
, buf
, 2))
85 vfio_iowrite16(val
, io
+ off
);
87 val
= vfio_ioread16(io
+ off
);
89 if (copy_to_user(buf
, &val
, 2))
94 } else if (fillable
) {
98 if (copy_from_user(&val
, buf
, 1))
101 vfio_iowrite8(val
, io
+ off
);
103 val
= vfio_ioread8(io
+ off
);
105 if (copy_to_user(buf
, &val
, 1))
111 /* Fill reads with -1, drop writes */
112 filled
= min(count
, (size_t)(x_end
- off
));
117 for (i
= 0; i
< filled
; i
++)
118 if (copy_to_user(buf
+ i
, &val
, 1))
132 static int vfio_pci_setup_barmap(struct vfio_pci_device
*vdev
, int bar
)
134 struct pci_dev
*pdev
= vdev
->pdev
;
138 if (vdev
->barmap
[bar
])
141 ret
= pci_request_selected_regions(pdev
, 1 << bar
, "vfio");
145 io
= pci_iomap(pdev
, bar
, 0);
147 pci_release_selected_regions(pdev
, 1 << bar
);
151 vdev
->barmap
[bar
] = io
;
156 ssize_t
vfio_pci_bar_rw(struct vfio_pci_device
*vdev
, char __user
*buf
,
157 size_t count
, loff_t
*ppos
, bool iswrite
)
159 struct pci_dev
*pdev
= vdev
->pdev
;
160 loff_t pos
= *ppos
& VFIO_PCI_OFFSET_MASK
;
161 int bar
= VFIO_PCI_OFFSET_TO_INDEX(*ppos
);
162 size_t x_start
= 0, x_end
= 0;
167 if (pci_resource_start(pdev
, bar
))
168 end
= pci_resource_len(pdev
, bar
);
169 else if (bar
== PCI_ROM_RESOURCE
&&
170 pdev
->resource
[bar
].flags
& IORESOURCE_ROM_SHADOW
)
178 count
= min(count
, (size_t)(end
- pos
));
180 if (bar
== PCI_ROM_RESOURCE
) {
182 * The ROM can fill less space than the BAR, so we start the
183 * excluded range at the end of the actual ROM. This makes
184 * filling large ROM BARs much faster.
186 io
= pci_map_rom(pdev
, &x_start
);
191 int ret
= vfio_pci_setup_barmap(vdev
, bar
);
195 io
= vdev
->barmap
[bar
];
198 if (bar
== vdev
->msix_bar
) {
199 x_start
= vdev
->msix_offset
;
200 x_end
= vdev
->msix_offset
+ vdev
->msix_size
;
203 done
= do_io_rw(io
, buf
, pos
, count
, x_start
, x_end
, iswrite
);
208 if (bar
== PCI_ROM_RESOURCE
)
209 pci_unmap_rom(pdev
, io
);
214 ssize_t
vfio_pci_vga_rw(struct vfio_pci_device
*vdev
, char __user
*buf
,
215 size_t count
, loff_t
*ppos
, bool iswrite
)
218 loff_t off
, pos
= *ppos
& VFIO_PCI_OFFSET_MASK
;
219 void __iomem
*iomem
= NULL
;
231 case 0xa0000 ... 0xbffff:
232 count
= min(count
, (size_t)(0xc0000 - pos
));
233 iomem
= ioremap(0xa0000, 0xbffff - 0xa0000 + 1);
235 rsrc
= VGA_RSRC_LEGACY_MEM
;
238 case 0x3b0 ... 0x3bb:
239 count
= min(count
, (size_t)(0x3bc - pos
));
240 iomem
= ioport_map(0x3b0, 0x3bb - 0x3b0 + 1);
242 rsrc
= VGA_RSRC_LEGACY_IO
;
245 case 0x3c0 ... 0x3df:
246 count
= min(count
, (size_t)(0x3e0 - pos
));
247 iomem
= ioport_map(0x3c0, 0x3df - 0x3c0 + 1);
249 rsrc
= VGA_RSRC_LEGACY_IO
;
259 ret
= vga_get_interruptible(vdev
->pdev
, rsrc
);
261 is_ioport
? ioport_unmap(iomem
) : iounmap(iomem
);
265 done
= do_io_rw(iomem
, buf
, off
, count
, 0, 0, iswrite
);
267 vga_put(vdev
->pdev
, rsrc
);
269 is_ioport
? ioport_unmap(iomem
) : iounmap(iomem
);
277 static int vfio_pci_ioeventfd_handler(void *opaque
, void *unused
)
279 struct vfio_pci_ioeventfd
*ioeventfd
= opaque
;
281 switch (ioeventfd
->count
) {
283 vfio_iowrite8(ioeventfd
->data
, ioeventfd
->addr
);
286 vfio_iowrite16(ioeventfd
->data
, ioeventfd
->addr
);
289 vfio_iowrite32(ioeventfd
->data
, ioeventfd
->addr
);
293 vfio_iowrite64(ioeventfd
->data
, ioeventfd
->addr
);
301 long vfio_pci_ioeventfd(struct vfio_pci_device
*vdev
, loff_t offset
,
302 uint64_t data
, int count
, int fd
)
304 struct pci_dev
*pdev
= vdev
->pdev
;
305 loff_t pos
= offset
& VFIO_PCI_OFFSET_MASK
;
306 int ret
, bar
= VFIO_PCI_OFFSET_TO_INDEX(offset
);
307 struct vfio_pci_ioeventfd
*ioeventfd
;
309 /* Only support ioeventfds into BARs */
310 if (bar
> VFIO_PCI_BAR5_REGION_INDEX
)
313 if (pos
+ count
> pci_resource_len(pdev
, bar
))
316 /* Disallow ioeventfds working around MSI-X table writes */
317 if (bar
== vdev
->msix_bar
&&
318 !(pos
+ count
<= vdev
->msix_offset
||
319 pos
>= vdev
->msix_offset
+ vdev
->msix_size
))
327 ret
= vfio_pci_setup_barmap(vdev
, bar
);
331 mutex_lock(&vdev
->ioeventfds_lock
);
333 list_for_each_entry(ioeventfd
, &vdev
->ioeventfds_list
, next
) {
334 if (ioeventfd
->pos
== pos
&& ioeventfd
->bar
== bar
&&
335 ioeventfd
->data
== data
&& ioeventfd
->count
== count
) {
337 vfio_virqfd_disable(&ioeventfd
->virqfd
);
338 list_del(&ioeventfd
->next
);
339 vdev
->ioeventfds_nr
--;
354 if (vdev
->ioeventfds_nr
>= VFIO_PCI_IOEVENTFD_MAX
) {
359 ioeventfd
= kzalloc(sizeof(*ioeventfd
), GFP_KERNEL
);
365 ioeventfd
->addr
= vdev
->barmap
[bar
] + pos
;
366 ioeventfd
->data
= data
;
367 ioeventfd
->pos
= pos
;
368 ioeventfd
->bar
= bar
;
369 ioeventfd
->count
= count
;
371 ret
= vfio_virqfd_enable(ioeventfd
, vfio_pci_ioeventfd_handler
,
372 NULL
, NULL
, &ioeventfd
->virqfd
, fd
);
378 list_add(&ioeventfd
->next
, &vdev
->ioeventfds_list
);
379 vdev
->ioeventfds_nr
++;
382 mutex_unlock(&vdev
->ioeventfds_lock
);