2 * Copyright (c) 2007, Neocleus Corporation.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
15 * Place - Suite 330, Boston, MA 02111-1307 USA.
18 * Assign a PCI device from the host to a guest VM.
20 * Adapted for KVM by Qumranet.
22 * Copyright (c) 2007, Neocleus, Alex Novik (alex@neocleus.com)
23 * Copyright (c) 2007, Neocleus, Guy Zana (guy@neocleus.com)
24 * Copyright (C) 2008, Qumranet, Amit Shah (amit.shah@qumranet.com)
25 * Copyright (C) 2008, Red Hat, Amit Shah (amit.shah@redhat.com)
26 * Copyright (C) 2008, IBM, Muli Ben-Yehuda (muli@il.ibm.com)
35 #include "device-assignment.h"
37 /* From linux/ioport.h */
38 #define IORESOURCE_IO 0x00000100 /* Resource type */
39 #define IORESOURCE_MEM 0x00000200
40 #define IORESOURCE_IRQ 0x00000400
41 #define IORESOURCE_DMA 0x00000800
42 #define IORESOURCE_PREFETCH 0x00001000 /* No side effects */
44 /* #define DEVICE_ASSIGNMENT_DEBUG 1 */
46 #ifdef DEVICE_ASSIGNMENT_DEBUG
47 #define DEBUG(fmt, ...) \
49 fprintf(stderr, "%s: " fmt, __func__ , __VA_ARGS__); \
52 #define DEBUG(fmt, ...) do { } while(0)
55 static uint32_t guest_to_host_ioport(AssignedDevRegion
*region
, uint32_t addr
)
57 return region
->u
.r_baseport
+ (addr
- region
->e_physbase
);
60 static void assigned_dev_ioport_writeb(void *opaque
, uint32_t addr
,
63 AssignedDevRegion
*r_access
= opaque
;
64 uint32_t r_pio
= guest_to_host_ioport(r_access
, addr
);
66 DEBUG("r_pio=%08x e_physbase=%08x r_baseport=%08lx value=%08x\n",
67 r_pio
, (int)r_access
->e_physbase
,
68 (unsigned long)r_access
->u
.r_baseport
, value
);
73 static void assigned_dev_ioport_writew(void *opaque
, uint32_t addr
,
76 AssignedDevRegion
*r_access
= opaque
;
77 uint32_t r_pio
= guest_to_host_ioport(r_access
, addr
);
79 DEBUG("r_pio=%08x e_physbase=%08x r_baseport=%08lx value=%08x\n",
80 r_pio
, (int)r_access
->e_physbase
,
81 (unsigned long)r_access
->u
.r_baseport
, value
);
86 static void assigned_dev_ioport_writel(void *opaque
, uint32_t addr
,
89 AssignedDevRegion
*r_access
= opaque
;
90 uint32_t r_pio
= guest_to_host_ioport(r_access
, addr
);
92 DEBUG("r_pio=%08x e_physbase=%08x r_baseport=%08lx value=%08x\n",
93 r_pio
, (int)r_access
->e_physbase
,
94 (unsigned long)r_access
->u
.r_baseport
, value
);
99 static uint32_t assigned_dev_ioport_readb(void *opaque
, uint32_t addr
)
101 AssignedDevRegion
*r_access
= opaque
;
102 uint32_t r_pio
= guest_to_host_ioport(r_access
, addr
);
107 DEBUG("r_pio=%08x e_physbase=%08x r_=%08lx value=%08x\n",
108 r_pio
, (int)r_access
->e_physbase
,
109 (unsigned long)r_access
->u
.r_baseport
, value
);
114 static uint32_t assigned_dev_ioport_readw(void *opaque
, uint32_t addr
)
116 AssignedDevRegion
*r_access
= opaque
;
117 uint32_t r_pio
= guest_to_host_ioport(r_access
, addr
);
122 DEBUG("r_pio=%08x e_physbase=%08x r_baseport=%08lx value=%08x\n",
123 r_pio
, (int)r_access
->e_physbase
,
124 (unsigned long)r_access
->u
.r_baseport
, value
);
129 static uint32_t assigned_dev_ioport_readl(void *opaque
, uint32_t addr
)
131 AssignedDevRegion
*r_access
= opaque
;
132 uint32_t r_pio
= guest_to_host_ioport(r_access
, addr
);
137 DEBUG("r_pio=%08x e_physbase=%08x r_baseport=%08lx value=%08x\n",
138 r_pio
, (int)r_access
->e_physbase
,
139 (unsigned long)r_access
->u
.r_baseport
, value
);
144 static void assigned_dev_iomem_map(PCIDevice
*pci_dev
, int region_num
,
145 uint32_t e_phys
, uint32_t e_size
, int type
)
147 AssignedDevice
*r_dev
= (AssignedDevice
*) pci_dev
;
148 AssignedDevRegion
*region
= &r_dev
->v_addrs
[region_num
];
149 uint32_t old_ephys
= region
->e_physbase
;
150 uint32_t old_esize
= region
->e_size
;
151 int first_map
= (region
->e_size
== 0);
154 DEBUG("e_phys=%08x r_virt=%p type=%d len=%08x region_num=%d \n",
155 e_phys
, region
->u
.r_virtbase
, type
, e_size
, region_num
);
157 region
->e_physbase
= e_phys
;
158 region
->e_size
= e_size
;
161 int slot
= get_slot(old_ephys
);
163 kvm_destroy_phys_mem(kvm_context
, old_ephys
,
164 TARGET_PAGE_ALIGN(old_esize
));
168 ret
= kvm_register_phys_mem(kvm_context
, e_phys
,
169 region
->u
.r_virtbase
,
170 TARGET_PAGE_ALIGN(e_size
), 0);
173 fprintf(stderr
, "%s: Error: create new mapping failed\n", __func__
);
178 static void assigned_dev_ioport_map(PCIDevice
*pci_dev
, int region_num
,
179 uint32_t addr
, uint32_t size
, int type
)
181 AssignedDevice
*r_dev
= (AssignedDevice
*) pci_dev
;
182 AssignedDevRegion
*region
= &r_dev
->v_addrs
[region_num
];
183 int first_map
= (region
->e_size
== 0);
186 region
->e_physbase
= addr
;
187 region
->e_size
= size
;
189 DEBUG("e_phys=0x%x r_baseport=%x type=0x%x len=%d region_num=%d \n",
190 addr
, region
->u
.r_baseport
, type
, size
, region_num
);
193 struct ioperm_data
*data
;
195 data
= qemu_mallocz(sizeof(struct ioperm_data
));
197 fprintf(stderr
, "%s: Out of memory\n", __func__
);
201 data
->start_port
= region
->u
.r_baseport
;
202 data
->num
= region
->r_size
;
205 kvm_add_ioperm_data(data
);
207 for (env
= first_cpu
; env
; env
= env
->next_cpu
)
208 kvm_ioperm(env
, data
);
211 register_ioport_read(addr
, size
, 1, assigned_dev_ioport_readb
,
212 (r_dev
->v_addrs
+ region_num
));
213 register_ioport_read(addr
, size
, 2, assigned_dev_ioport_readw
,
214 (r_dev
->v_addrs
+ region_num
));
215 register_ioport_read(addr
, size
, 4, assigned_dev_ioport_readl
,
216 (r_dev
->v_addrs
+ region_num
));
217 register_ioport_write(addr
, size
, 1, assigned_dev_ioport_writeb
,
218 (r_dev
->v_addrs
+ region_num
));
219 register_ioport_write(addr
, size
, 2, assigned_dev_ioport_writew
,
220 (r_dev
->v_addrs
+ region_num
));
221 register_ioport_write(addr
, size
, 4, assigned_dev_ioport_writel
,
222 (r_dev
->v_addrs
+ region_num
));
225 static void assigned_dev_pci_write_config(PCIDevice
*d
, uint32_t address
,
226 uint32_t val
, int len
)
231 DEBUG("(%x.%x): address=%04x val=0x%08x len=%d\n",
232 ((d
->devfn
>> 3) & 0x1F), (d
->devfn
& 0x7),
233 (uint16_t) address
, val
, len
);
235 if (address
== 0x4) {
236 pci_default_write_config(d
, address
, val
, len
);
237 /* Continue to program the card */
240 if ((address
>= 0x10 && address
<= 0x24) || address
== 0x34 ||
241 address
== 0x3c || address
== 0x3d) {
242 /* used for update-mappings (BAR emulation) */
243 pci_default_write_config(d
, address
, val
, len
);
247 DEBUG("NON BAR (%x.%x): address=%04x val=0x%08x len=%d\n",
248 ((d
->devfn
>> 3) & 0x1F), (d
->devfn
& 0x7),
249 (uint16_t) address
, val
, len
);
251 fd
= ((AssignedDevice
*)d
)->real_device
.config_fd
;
254 ret
= pwrite(fd
, &val
, len
, address
);
256 if ((ret
< 0) && (errno
== EINTR
|| errno
== EAGAIN
))
259 fprintf(stderr
, "%s: pwrite failed, ret = %zd errno = %d\n",
260 __func__
, ret
, errno
);
266 static uint32_t assigned_dev_pci_read_config(PCIDevice
*d
, uint32_t address
,
273 if ((address
>= 0x10 && address
<= 0x24) || address
== 0x34 ||
274 address
== 0x3c || address
== 0x3d) {
275 val
= pci_default_read_config(d
, address
, len
);
276 DEBUG("(%x.%x): address=%04x val=0x%08x len=%d\n",
277 (d
->devfn
>> 3) & 0x1F, (d
->devfn
& 0x7), address
, val
, len
);
281 /* vga specific, remove later */
285 fd
= ((AssignedDevice
*)d
)->real_device
.config_fd
;
288 ret
= pread(fd
, &val
, len
, address
);
290 if ((ret
< 0) && (errno
== EINTR
|| errno
== EAGAIN
))
293 fprintf(stderr
, "%s: pread failed, ret = %zd errno = %d\n",
294 __func__
, ret
, errno
);
300 DEBUG("(%x.%x): address=%04x val=0x%08x len=%d\n",
301 (d
->devfn
>> 3) & 0x1F, (d
->devfn
& 0x7), address
, val
, len
);
303 /* kill the special capabilities */
304 if (address
== 4 && len
== 4)
306 else if (address
== 6)
312 static int assigned_dev_register_regions(PCIRegion
*io_regions
,
313 unsigned long regions_num
,
314 AssignedDevice
*pci_dev
)
317 PCIRegion
*cur_region
= io_regions
;
319 for (i
= 0; i
< regions_num
; i
++, cur_region
++) {
320 if (!cur_region
->valid
)
322 pci_dev
->v_addrs
[i
].num
= i
;
324 /* handle memory io regions */
325 if (cur_region
->type
& IORESOURCE_MEM
) {
326 int t
= cur_region
->type
& IORESOURCE_PREFETCH
327 ? PCI_ADDRESS_SPACE_MEM_PREFETCH
328 : PCI_ADDRESS_SPACE_MEM
;
330 /* map physical memory */
331 pci_dev
->v_addrs
[i
].e_physbase
= cur_region
->base_addr
;
332 pci_dev
->v_addrs
[i
].u
.r_virtbase
=
334 (cur_region
->size
+ 0xFFF) & 0xFFFFF000,
335 PROT_WRITE
| PROT_READ
, MAP_SHARED
,
336 cur_region
->resource_fd
, (off_t
) 0);
338 if (pci_dev
->v_addrs
[i
].u
.r_virtbase
== MAP_FAILED
) {
339 pci_dev
->v_addrs
[i
].u
.r_virtbase
= NULL
;
340 fprintf(stderr
, "%s: Error: Couldn't mmap 0x%x!"
342 (uint32_t) (cur_region
->base_addr
));
345 pci_dev
->v_addrs
[i
].r_size
= cur_region
->size
;
346 pci_dev
->v_addrs
[i
].e_size
= 0;
349 pci_dev
->v_addrs
[i
].u
.r_virtbase
+=
350 (cur_region
->base_addr
& 0xFFF);
352 pci_register_io_region((PCIDevice
*) pci_dev
, i
,
354 assigned_dev_iomem_map
);
357 /* handle port io regions */
358 pci_dev
->v_addrs
[i
].e_physbase
= cur_region
->base_addr
;
359 pci_dev
->v_addrs
[i
].u
.r_baseport
= cur_region
->base_addr
;
360 pci_dev
->v_addrs
[i
].r_size
= cur_region
->size
;
361 pci_dev
->v_addrs
[i
].e_size
= 0;
363 pci_register_io_region((PCIDevice
*) pci_dev
, i
,
364 cur_region
->size
, PCI_ADDRESS_SPACE_IO
,
365 assigned_dev_ioport_map
);
367 /* not relevant for port io */
368 pci_dev
->v_addrs
[i
].memory_index
= 0;
375 static int get_real_device(AssignedDevice
*pci_dev
, uint8_t r_bus
,
376 uint8_t r_dev
, uint8_t r_func
)
378 char dir
[128], name
[128];
381 unsigned long long start
, end
, size
, flags
;
383 PCIDevRegions
*dev
= &pci_dev
->real_device
;
385 dev
->region_number
= 0;
387 snprintf(dir
, sizeof(dir
), "/sys/bus/pci/devices/0000:%02x:%02x.%x/",
388 r_bus
, r_dev
, r_func
);
390 snprintf(name
, sizeof(name
), "%sconfig", dir
);
392 fd
= open(name
, O_RDWR
);
394 fprintf(stderr
, "%s: %s: %m\n", __func__
, name
);
399 r
= read(fd
, pci_dev
->dev
.config
, sizeof(pci_dev
->dev
.config
));
401 if (errno
== EINTR
|| errno
== EAGAIN
)
403 fprintf(stderr
, "%s: read failed, errno = %d\n", __func__
, errno
);
406 snprintf(name
, sizeof(name
), "%sresource", dir
);
408 f
= fopen(name
, "r");
410 fprintf(stderr
, "%s: %s: %m\n", __func__
, name
);
414 for (r
= 0; r
< MAX_IO_REGIONS
; r
++) {
415 if (fscanf(f
, "%lli %lli %lli\n", &start
, &end
, &flags
) != 3)
418 rp
= dev
->regions
+ r
;
420 size
= end
- start
+ 1;
421 flags
&= IORESOURCE_IO
| IORESOURCE_MEM
| IORESOURCE_PREFETCH
;
422 if (size
== 0 || (flags
& ~IORESOURCE_PREFETCH
) == 0)
424 if (flags
& IORESOURCE_MEM
) {
425 flags
&= ~IORESOURCE_IO
;
426 snprintf(name
, sizeof(name
), "%sresource%d", dir
, r
);
427 fd
= open(name
, O_RDWR
);
429 continue; /* probably ROM */
430 rp
->resource_fd
= fd
;
432 flags
&= ~IORESOURCE_PREFETCH
;
436 rp
->base_addr
= start
;
438 DEBUG("region %d size %d start 0x%llx type %d resource_fd %d\n",
439 r
, rp
->size
, start
, rp
->type
, rp
->resource_fd
);
443 dev
->region_number
= r
;
447 static LIST_HEAD(, AssignedDevInfo
) adev_head
;
449 void free_assigned_device(AssignedDevInfo
*adev
)
451 AssignedDevice
*dev
= adev
->assigned_dev
;
456 for (i
= 0; i
< dev
->real_device
.region_number
; i
++) {
457 PCIRegion
*pci_region
= &dev
->real_device
.regions
[i
];
458 AssignedDevRegion
*region
= &dev
->v_addrs
[i
];
460 if (!pci_region
->valid
|| !(pci_region
->type
& IORESOURCE_MEM
))
463 if (region
->u
.r_virtbase
) {
464 int ret
= munmap(region
->u
.r_virtbase
,
465 (pci_region
->size
+ 0xFFF) & 0xFFFFF000);
468 "Failed to unmap assigned device region: %s\n",
473 if (dev
->real_device
.config_fd
) {
474 close(dev
->real_device
.config_fd
);
475 dev
->real_device
.config_fd
= 0;
478 pci_unregister_device(&dev
->dev
);
479 adev
->assigned_dev
= dev
= NULL
;
482 LIST_REMOVE(adev
, next
);
486 static uint32_t calc_assigned_dev_id(uint8_t bus
, uint8_t devfn
)
488 return (uint32_t)bus
<< 8 | (uint32_t)devfn
;
491 /* The pci config space got updated. Check if irq numbers have changed
494 void assigned_dev_update_irq(PCIDevice
*d
)
496 AssignedDevInfo
*adev
;
498 adev
= LIST_FIRST(&adev_head
);
500 AssignedDevInfo
*next
= LIST_NEXT(adev
, next
);
501 AssignedDevice
*assigned_dev
= adev
->assigned_dev
;
504 irq
= pci_map_irq(&assigned_dev
->dev
, assigned_dev
->intpin
);
505 irq
= piix_get_irq(irq
);
508 irq
= ipf_map_irq(d
, irq
);
511 if (irq
!= assigned_dev
->girq
) {
512 struct kvm_assigned_irq assigned_irq_data
;
514 memset(&assigned_irq_data
, 0, sizeof(assigned_irq_data
));
515 assigned_irq_data
.assigned_dev_id
=
516 calc_assigned_dev_id(assigned_dev
->h_busnr
,
517 (uint8_t) assigned_dev
->h_devfn
);
518 assigned_irq_data
.guest_irq
= irq
;
519 assigned_irq_data
.host_irq
= assigned_dev
->real_device
.irq
;
520 r
= kvm_assign_irq(kvm_context
, &assigned_irq_data
);
522 fprintf(stderr
, "Failed to assign irq for \"%s\": %s\n",
523 adev
->name
, strerror(-r
));
524 fprintf(stderr
, "Perhaps you are assigning a device "
525 "that shares an IRQ with another device?\n");
526 LIST_REMOVE(adev
, next
);
527 free_assigned_device(adev
);
531 assigned_dev
->girq
= irq
;
538 struct PCIDevice
*init_assigned_device(AssignedDevInfo
*adev
, PCIBus
*bus
)
542 uint8_t e_device
, e_intx
;
543 struct kvm_assigned_pci_dev assigned_dev_data
;
545 DEBUG("Registering real physical device %s (bus=%x dev=%x func=%x)\n",
546 adev
->name
, adev
->bus
, adev
->dev
, adev
->func
);
548 dev
= (AssignedDevice
*)
549 pci_register_device(bus
, adev
->name
, sizeof(AssignedDevice
),
550 -1, assigned_dev_pci_read_config
,
551 assigned_dev_pci_write_config
);
553 fprintf(stderr
, "%s: Error: Couldn't register real device %s\n",
554 __func__
, adev
->name
);
558 adev
->assigned_dev
= dev
;
560 if (get_real_device(dev
, adev
->bus
, adev
->dev
, adev
->func
)) {
561 fprintf(stderr
, "%s: Error: Couldn't get real device (%s)!\n",
562 __func__
, adev
->name
);
566 /* handle real device's MMIO/PIO BARs */
567 if (assigned_dev_register_regions(dev
->real_device
.regions
,
568 dev
->real_device
.region_number
,
572 /* handle interrupt routing */
573 e_device
= (dev
->dev
.devfn
>> 3) & 0x1f;
574 e_intx
= dev
->dev
.config
[0x3d] - 1;
575 dev
->intpin
= e_intx
;
578 dev
->h_busnr
= adev
->bus
;
579 dev
->h_devfn
= PCI_DEVFN(adev
->dev
, adev
->func
);
581 memset(&assigned_dev_data
, 0, sizeof(assigned_dev_data
));
582 assigned_dev_data
.assigned_dev_id
=
583 calc_assigned_dev_id(dev
->h_busnr
, (uint32_t)dev
->h_devfn
);
584 assigned_dev_data
.busnr
= dev
->h_busnr
;
585 assigned_dev_data
.devfn
= dev
->h_devfn
;
588 /* We always enable the IOMMU if present
589 * (or when not disabled on the command line)
591 r
= kvm_check_extension(kvm_context
, KVM_CAP_IOMMU
);
592 if (r
&& !adev
->disable_iommu
)
593 assigned_dev_data
.flags
|= KVM_DEV_ASSIGN_ENABLE_IOMMU
;
596 r
= kvm_assign_pci_device(kvm_context
, &assigned_dev_data
);
598 fprintf(stderr
, "Failed to assign device \"%s\" : %s\n",
599 adev
->name
, strerror(-r
));
607 * Syntax to assign device:
609 * -pcidevice host=bus:dev.func[,dma=none][,name=Foo]
612 * -pcidevice host=00:13.0,dma=pvdma
614 * dma can currently only be 'none' to disable iommu support.
616 AssignedDevInfo
*add_assigned_device(const char *arg
)
622 AssignedDevInfo
*adev
;
624 adev
= qemu_mallocz(sizeof(AssignedDevInfo
));
626 fprintf(stderr
, "%s: Out of memory\n", __func__
);
629 r
= get_param_value(device
, sizeof(device
), "host", arg
);
630 r
= get_param_value(adev
->name
, sizeof(adev
->name
), "name", arg
);
632 snprintf(adev
->name
, sizeof(adev
->name
), "%s", device
);
635 r
= get_param_value(dma
, sizeof(dma
), "dma", arg
);
636 if (r
&& !strncmp(dma
, "none", 4))
637 adev
->disable_iommu
= 1;
640 adev
->bus
= strtoul(cp
, &cp1
, 16);
645 adev
->dev
= strtoul(cp
, &cp1
, 16);
650 adev
->func
= strtoul(cp
, &cp1
, 16);
652 LIST_INSERT_HEAD(&adev_head
, adev
, next
);
655 fprintf(stderr
, "pcidevice argument parse error; "
656 "please check the help text for usage\n");
661 void add_assigned_devices(PCIBus
*bus
, const char **devices
, int n_devices
)
665 for (i
= 0; i
< n_devices
; i
++) {
666 struct AssignedDevInfo
*adev
;
668 adev
= add_assigned_device(devices
[i
]);
670 fprintf(stderr
, "Could not add assigned device %s\n", devices
[i
]);
674 if (!init_assigned_device(adev
, bus
)) {
675 fprintf(stderr
, "Failed to initialize assigned device %s\n",
682 /* Option ROM header */
683 struct option_rom_header
{
684 uint8_t signature
[2];
686 uint32_t entry_point
;
687 uint8_t reserved
[17];
688 uint16_t pci_header_offset
;
689 uint16_t expansion_header_offset
;
690 } __attribute__ ((packed
));
692 /* Option ROM PCI data structure */
693 struct option_rom_pci_header
{
694 uint8_t signature
[4];
697 uint16_t vital_product_data_offset
;
698 uint16_t structure_length
;
699 uint8_t structure_revision
;
700 uint8_t class_code
[3];
701 uint16_t image_length
;
702 uint16_t image_revision
;
706 } __attribute__ ((packed
));
709 * Scan the list of Option ROMs at roms. If a suitable Option ROM is found,
710 * allocate a ram space and copy it there. Then return its size aligned to
711 * both 2KB and target page size.
713 #define OPTION_ROM_ALIGN(x) (((x) + 2047) & ~2047)
714 static int scan_option_rom(uint8_t devfn
, void *roms
, ram_addr_t offset
)
716 int i
, size
, total_size
;
719 struct option_rom_header
*rom
;
720 struct option_rom_pci_header
*pcih
;
725 /* Invalid signature means we're out of option ROMs. */
726 if (strncmp((char *)rom
->signature
, "\x55\xaa", 2) ||
727 (rom
->rom_size
== 0))
730 size
= rom
->rom_size
* 512;
731 /* Invalid checksum means we're out of option ROMs. */
733 for (i
= 0; i
< size
; i
++)
734 csum
+= ((uint8_t *)rom
)[i
];
738 /* Check the PCI header (if any) for a match. */
739 pcih
= (struct option_rom_pci_header
*)
740 ((char *)rom
+ rom
->pci_header_offset
);
741 if ((rom
->pci_header_offset
!= 0) &&
742 !strncmp((char *)pcih
->signature
, "PCIR", 4))
745 rom
= (struct option_rom_header
*)((char *)rom
+ size
);
751 /* The size should be both 2K-aligned and page-aligned */
752 total_size
= (TARGET_PAGE_SIZE
< 2048)
753 ? OPTION_ROM_ALIGN(size
+ 1)
754 : TARGET_PAGE_ALIGN(size
+ 1);
756 /* Size of all available ram space is 0x10000 (0xd0000 to 0xe0000) */
757 if ((offset
+ total_size
) > 0x10000u
) {
758 fprintf(stderr
, "Option ROM size %x exceeds available space\n", size
);
762 addr
= qemu_ram_alloc(total_size
);
763 cpu_register_physical_memory(0xd0000 + offset
, total_size
, addr
| IO_MEM_ROM
);
765 /* Write ROM data and devfn to phys_addr */
766 cpu_physical_memory_write_rom(0xd0000 + offset
, rom
, size
);
767 cpu_physical_memory_write_rom(0xd0000 + offset
+ size
, &devfn
, 1);
773 * Scan the assigned devices for the devices that have an option ROM, and then
774 * load the corresponding ROM data to RAM. If an error occurs while loading an
775 * option ROM, we just ignore that option ROM and continue with the next one.
777 ram_addr_t
assigned_dev_load_option_roms(ram_addr_t rom_base_offset
)
779 ram_addr_t offset
= rom_base_offset
;
780 AssignedDevInfo
*adev
;
782 LIST_FOREACH(adev
, &adev_head
, next
) {
789 snprintf(rom_file
, sizeof(rom_file
),
790 "/sys/bus/pci/devices/0000:%02x:%02x.%01x/rom",
791 adev
->bus
, adev
->dev
, adev
->func
);
793 if (access(rom_file
, F_OK
))
796 /* Write something to the ROM file to enable it */
797 fp
= fopen(rom_file
, "wb");
800 len
= fwrite(&i
, 1, 1, fp
);
805 /* The file has to be closed and reopened, otherwise it won't work */
806 fp
= fopen(rom_file
, "rb");
810 fseek(fp
, 0, SEEK_END
);
812 fseek(fp
, 0, SEEK_SET
);
820 fread(buf
, size
, 1, fp
);
821 if (!feof(fp
) || ferror(fp
)) {
827 /* Scan the buffer for suitable ROMs and increase the offset */
828 offset
+= scan_option_rom(adev
->assigned_dev
->dev
.devfn
, buf
, offset
);