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 kvm_destroy_phys_mem(kvm_context
, old_ephys
,
162 TARGET_PAGE_ALIGN(old_esize
));
165 ret
= kvm_register_phys_mem(kvm_context
, e_phys
,
166 region
->u
.r_virtbase
,
167 TARGET_PAGE_ALIGN(e_size
), 0);
170 fprintf(stderr
, "%s: Error: create new mapping failed\n", __func__
);
175 static void assigned_dev_ioport_map(PCIDevice
*pci_dev
, int region_num
,
176 uint32_t addr
, uint32_t size
, int type
)
178 AssignedDevice
*r_dev
= (AssignedDevice
*) pci_dev
;
179 AssignedDevRegion
*region
= &r_dev
->v_addrs
[region_num
];
180 int first_map
= (region
->e_size
== 0);
183 region
->e_physbase
= addr
;
184 region
->e_size
= size
;
186 DEBUG("e_phys=0x%x r_baseport=%x type=0x%x len=%d region_num=%d \n",
187 addr
, region
->u
.r_baseport
, type
, size
, region_num
);
190 struct ioperm_data
*data
;
192 data
= qemu_mallocz(sizeof(struct ioperm_data
));
194 fprintf(stderr
, "%s: Out of memory\n", __func__
);
198 data
->start_port
= region
->u
.r_baseport
;
199 data
->num
= region
->r_size
;
202 kvm_add_ioperm_data(data
);
204 for (env
= first_cpu
; env
; env
= env
->next_cpu
)
205 kvm_ioperm(env
, data
);
208 register_ioport_read(addr
, size
, 1, assigned_dev_ioport_readb
,
209 (r_dev
->v_addrs
+ region_num
));
210 register_ioport_read(addr
, size
, 2, assigned_dev_ioport_readw
,
211 (r_dev
->v_addrs
+ region_num
));
212 register_ioport_read(addr
, size
, 4, assigned_dev_ioport_readl
,
213 (r_dev
->v_addrs
+ region_num
));
214 register_ioport_write(addr
, size
, 1, assigned_dev_ioport_writeb
,
215 (r_dev
->v_addrs
+ region_num
));
216 register_ioport_write(addr
, size
, 2, assigned_dev_ioport_writew
,
217 (r_dev
->v_addrs
+ region_num
));
218 register_ioport_write(addr
, size
, 4, assigned_dev_ioport_writel
,
219 (r_dev
->v_addrs
+ region_num
));
222 static void assigned_dev_pci_write_config(PCIDevice
*d
, uint32_t address
,
223 uint32_t val
, int len
)
228 DEBUG("(%x.%x): address=%04x val=0x%08x len=%d\n",
229 ((d
->devfn
>> 3) & 0x1F), (d
->devfn
& 0x7),
230 (uint16_t) address
, val
, len
);
232 if (address
== 0x4) {
233 pci_default_write_config(d
, address
, val
, len
);
234 /* Continue to program the card */
237 if ((address
>= 0x10 && address
<= 0x24) || address
== 0x34 ||
238 address
== 0x3c || address
== 0x3d) {
239 /* used for update-mappings (BAR emulation) */
240 pci_default_write_config(d
, address
, val
, len
);
244 DEBUG("NON BAR (%x.%x): address=%04x val=0x%08x len=%d\n",
245 ((d
->devfn
>> 3) & 0x1F), (d
->devfn
& 0x7),
246 (uint16_t) address
, val
, len
);
248 fd
= ((AssignedDevice
*)d
)->real_device
.config_fd
;
251 ret
= pwrite(fd
, &val
, len
, address
);
253 if ((ret
< 0) && (errno
== EINTR
|| errno
== EAGAIN
))
256 fprintf(stderr
, "%s: pwrite failed, ret = %zd errno = %d\n",
257 __func__
, ret
, errno
);
263 static uint32_t assigned_dev_pci_read_config(PCIDevice
*d
, uint32_t address
,
270 if ((address
>= 0x10 && address
<= 0x24) || address
== 0x34 ||
271 address
== 0x3c || address
== 0x3d) {
272 val
= pci_default_read_config(d
, address
, len
);
273 DEBUG("(%x.%x): address=%04x val=0x%08x len=%d\n",
274 (d
->devfn
>> 3) & 0x1F, (d
->devfn
& 0x7), address
, val
, len
);
278 /* vga specific, remove later */
282 fd
= ((AssignedDevice
*)d
)->real_device
.config_fd
;
285 ret
= pread(fd
, &val
, len
, address
);
287 if ((ret
< 0) && (errno
== EINTR
|| errno
== EAGAIN
))
290 fprintf(stderr
, "%s: pread failed, ret = %zd errno = %d\n",
291 __func__
, ret
, errno
);
297 DEBUG("(%x.%x): address=%04x val=0x%08x len=%d\n",
298 (d
->devfn
>> 3) & 0x1F, (d
->devfn
& 0x7), address
, val
, len
);
300 /* kill the special capabilities */
301 if (address
== 4 && len
== 4)
303 else if (address
== 6)
309 static int assigned_dev_register_regions(PCIRegion
*io_regions
,
310 unsigned long regions_num
,
311 AssignedDevice
*pci_dev
)
314 PCIRegion
*cur_region
= io_regions
;
316 for (i
= 0; i
< regions_num
; i
++, cur_region
++) {
317 if (!cur_region
->valid
)
319 pci_dev
->v_addrs
[i
].num
= i
;
321 /* handle memory io regions */
322 if (cur_region
->type
& IORESOURCE_MEM
) {
323 int t
= cur_region
->type
& IORESOURCE_PREFETCH
324 ? PCI_ADDRESS_SPACE_MEM_PREFETCH
325 : PCI_ADDRESS_SPACE_MEM
;
327 /* map physical memory */
328 pci_dev
->v_addrs
[i
].e_physbase
= cur_region
->base_addr
;
329 pci_dev
->v_addrs
[i
].u
.r_virtbase
=
331 (cur_region
->size
+ 0xFFF) & 0xFFFFF000,
332 PROT_WRITE
| PROT_READ
, MAP_SHARED
,
333 cur_region
->resource_fd
, (off_t
) 0);
335 if (pci_dev
->v_addrs
[i
].u
.r_virtbase
== MAP_FAILED
) {
336 pci_dev
->v_addrs
[i
].u
.r_virtbase
= NULL
;
337 fprintf(stderr
, "%s: Error: Couldn't mmap 0x%x!"
339 (uint32_t) (cur_region
->base_addr
));
342 pci_dev
->v_addrs
[i
].r_size
= cur_region
->size
;
343 pci_dev
->v_addrs
[i
].e_size
= 0;
346 pci_dev
->v_addrs
[i
].u
.r_virtbase
+=
347 (cur_region
->base_addr
& 0xFFF);
349 pci_register_io_region((PCIDevice
*) pci_dev
, i
,
351 assigned_dev_iomem_map
);
354 /* handle port io regions */
355 pci_dev
->v_addrs
[i
].e_physbase
= cur_region
->base_addr
;
356 pci_dev
->v_addrs
[i
].u
.r_baseport
= cur_region
->base_addr
;
357 pci_dev
->v_addrs
[i
].r_size
= cur_region
->size
;
358 pci_dev
->v_addrs
[i
].e_size
= 0;
360 pci_register_io_region((PCIDevice
*) pci_dev
, i
,
361 cur_region
->size
, PCI_ADDRESS_SPACE_IO
,
362 assigned_dev_ioport_map
);
364 /* not relevant for port io */
365 pci_dev
->v_addrs
[i
].memory_index
= 0;
372 static int get_real_device(AssignedDevice
*pci_dev
, uint8_t r_bus
,
373 uint8_t r_dev
, uint8_t r_func
)
375 char dir
[128], name
[128];
378 unsigned long long start
, end
, size
, flags
;
380 PCIDevRegions
*dev
= &pci_dev
->real_device
;
382 dev
->region_number
= 0;
384 snprintf(dir
, sizeof(dir
), "/sys/bus/pci/devices/0000:%02x:%02x.%x/",
385 r_bus
, r_dev
, r_func
);
387 snprintf(name
, sizeof(name
), "%sconfig", dir
);
389 fd
= open(name
, O_RDWR
);
391 fprintf(stderr
, "%s: %s: %m\n", __func__
, name
);
396 r
= read(fd
, pci_dev
->dev
.config
, sizeof(pci_dev
->dev
.config
));
398 if (errno
== EINTR
|| errno
== EAGAIN
)
400 fprintf(stderr
, "%s: read failed, errno = %d\n", __func__
, errno
);
403 snprintf(name
, sizeof(name
), "%sresource", dir
);
405 f
= fopen(name
, "r");
407 fprintf(stderr
, "%s: %s: %m\n", __func__
, name
);
411 for (r
= 0; r
< MAX_IO_REGIONS
; r
++) {
412 if (fscanf(f
, "%lli %lli %lli\n", &start
, &end
, &flags
) != 3)
415 rp
= dev
->regions
+ r
;
417 size
= end
- start
+ 1;
418 flags
&= IORESOURCE_IO
| IORESOURCE_MEM
| IORESOURCE_PREFETCH
;
419 if (size
== 0 || (flags
& ~IORESOURCE_PREFETCH
) == 0)
421 if (flags
& IORESOURCE_MEM
) {
422 flags
&= ~IORESOURCE_IO
;
423 snprintf(name
, sizeof(name
), "%sresource%d", dir
, r
);
424 fd
= open(name
, O_RDWR
);
426 continue; /* probably ROM */
427 rp
->resource_fd
= fd
;
429 flags
&= ~IORESOURCE_PREFETCH
;
433 rp
->base_addr
= start
;
435 DEBUG("region %d size %d start 0x%llx type %d resource_fd %d\n",
436 r
, rp
->size
, start
, rp
->type
, rp
->resource_fd
);
440 dev
->region_number
= r
;
444 static LIST_HEAD(, AssignedDevInfo
) adev_head
;
446 static void free_assigned_device(AssignedDevInfo
*adev
)
448 AssignedDevice
*dev
= adev
->assigned_dev
;
453 for (i
= 0; i
< dev
->real_device
.region_number
; i
++) {
454 PCIRegion
*pci_region
= &dev
->real_device
.regions
[i
];
455 AssignedDevRegion
*region
= &dev
->v_addrs
[i
];
457 if (!pci_region
->valid
|| !(pci_region
->type
& IORESOURCE_MEM
))
460 kvm_remove_ioperm_data(region
->u
.r_baseport
, region
->r_size
);
462 if (region
->u
.r_virtbase
) {
463 int ret
= munmap(region
->u
.r_virtbase
,
464 (pci_region
->size
+ 0xFFF) & 0xFFFFF000);
467 "Failed to unmap assigned device region: %s\n",
472 if (dev
->real_device
.config_fd
) {
473 close(dev
->real_device
.config_fd
);
474 dev
->real_device
.config_fd
= 0;
477 pci_unregister_device(&dev
->dev
);
478 adev
->assigned_dev
= dev
= NULL
;
481 LIST_REMOVE(adev
, next
);
485 static uint32_t calc_assigned_dev_id(uint8_t bus
, uint8_t devfn
)
487 return (uint32_t)bus
<< 8 | (uint32_t)devfn
;
490 static int assign_device(AssignedDevInfo
*adev
)
492 struct kvm_assigned_pci_dev assigned_dev_data
;
493 AssignedDevice
*dev
= adev
->assigned_dev
;
496 memset(&assigned_dev_data
, 0, sizeof(assigned_dev_data
));
497 assigned_dev_data
.assigned_dev_id
=
498 calc_assigned_dev_id(dev
->h_busnr
, dev
->h_devfn
);
499 assigned_dev_data
.busnr
= dev
->h_busnr
;
500 assigned_dev_data
.devfn
= dev
->h_devfn
;
503 /* We always enable the IOMMU if present
504 * (or when not disabled on the command line)
506 r
= kvm_check_extension(kvm_context
, KVM_CAP_IOMMU
);
507 if (r
&& !adev
->disable_iommu
)
508 assigned_dev_data
.flags
|= KVM_DEV_ASSIGN_ENABLE_IOMMU
;
511 r
= kvm_assign_pci_device(kvm_context
, &assigned_dev_data
);
513 fprintf(stderr
, "Failed to assign device \"%s\" : %s\n",
514 adev
->name
, strerror(-r
));
518 static int assign_irq(AssignedDevInfo
*adev
)
520 struct kvm_assigned_irq assigned_irq_data
;
521 AssignedDevice
*dev
= adev
->assigned_dev
;
524 irq
= pci_map_irq(&dev
->dev
, dev
->intpin
);
525 irq
= piix_get_irq(irq
);
528 irq
= ipf_map_irq(&dev
->dev
, irq
);
531 if (dev
->girq
== irq
)
534 memset(&assigned_irq_data
, 0, sizeof(assigned_irq_data
));
535 assigned_irq_data
.assigned_dev_id
=
536 calc_assigned_dev_id(dev
->h_busnr
, dev
->h_devfn
);
537 assigned_irq_data
.guest_irq
= irq
;
538 assigned_irq_data
.host_irq
= dev
->real_device
.irq
;
539 r
= kvm_assign_irq(kvm_context
, &assigned_irq_data
);
541 fprintf(stderr
, "Failed to assign irq for \"%s\": %s\n",
542 adev
->name
, strerror(-r
));
543 fprintf(stderr
, "Perhaps you are assigning a device "
544 "that shares an IRQ with another device?\n");
552 static void deassign_device(AssignedDevInfo
*adev
)
554 struct kvm_assigned_pci_dev assigned_dev_data
;
555 AssignedDevice
*dev
= adev
->assigned_dev
;
558 memset(&assigned_dev_data
, 0, sizeof(assigned_dev_data
));
559 assigned_dev_data
.assigned_dev_id
=
560 calc_assigned_dev_id(dev
->h_busnr
, dev
->h_devfn
);
562 r
= kvm_deassign_pci_device(kvm_context
, &assigned_dev_data
);
564 fprintf(stderr
, "Failed to deassign device \"%s\" : %s\n",
565 adev
->name
, strerror(-r
));
568 void remove_assigned_device(AssignedDevInfo
*adev
)
570 deassign_device(adev
);
571 free_assigned_device(adev
);
574 /* The pci config space got updated. Check if irq numbers have changed
577 void assigned_dev_update_irqs()
579 AssignedDevInfo
*adev
;
581 adev
= LIST_FIRST(&adev_head
);
583 AssignedDevInfo
*next
= LIST_NEXT(adev
, next
);
586 r
= assign_irq(adev
);
588 remove_assigned_device(adev
);
594 struct PCIDevice
*init_assigned_device(AssignedDevInfo
*adev
, PCIBus
*bus
)
598 uint8_t e_device
, e_intx
;
599 struct kvm_assigned_pci_dev assigned_dev_data
;
601 DEBUG("Registering real physical device %s (bus=%x dev=%x func=%x)\n",
602 adev
->name
, adev
->bus
, adev
->dev
, adev
->func
);
604 dev
= (AssignedDevice
*)
605 pci_register_device(bus
, adev
->name
, sizeof(AssignedDevice
),
606 -1, assigned_dev_pci_read_config
,
607 assigned_dev_pci_write_config
);
609 fprintf(stderr
, "%s: Error: Couldn't register real device %s\n",
610 __func__
, adev
->name
);
614 adev
->assigned_dev
= dev
;
616 if (get_real_device(dev
, adev
->bus
, adev
->dev
, adev
->func
)) {
617 fprintf(stderr
, "%s: Error: Couldn't get real device (%s)!\n",
618 __func__
, adev
->name
);
622 /* handle real device's MMIO/PIO BARs */
623 if (assigned_dev_register_regions(dev
->real_device
.regions
,
624 dev
->real_device
.region_number
,
628 /* handle interrupt routing */
629 e_device
= (dev
->dev
.devfn
>> 3) & 0x1f;
630 e_intx
= dev
->dev
.config
[0x3d] - 1;
631 dev
->intpin
= e_intx
;
634 dev
->h_busnr
= adev
->bus
;
635 dev
->h_devfn
= PCI_DEVFN(adev
->dev
, adev
->func
);
637 /* assign device to guest */
638 r
= assign_device(adev
);
645 deassign_device(adev
);
647 free_assigned_device(adev
);
652 * Syntax to assign device:
654 * -pcidevice host=bus:dev.func[,dma=none][,name=Foo]
657 * -pcidevice host=00:13.0,dma=pvdma
659 * dma can currently only be 'none' to disable iommu support.
661 AssignedDevInfo
*add_assigned_device(const char *arg
)
667 AssignedDevInfo
*adev
;
669 adev
= qemu_mallocz(sizeof(AssignedDevInfo
));
671 fprintf(stderr
, "%s: Out of memory\n", __func__
);
674 r
= get_param_value(device
, sizeof(device
), "host", arg
);
675 r
= get_param_value(adev
->name
, sizeof(adev
->name
), "name", arg
);
677 snprintf(adev
->name
, sizeof(adev
->name
), "%s", device
);
680 r
= get_param_value(dma
, sizeof(dma
), "dma", arg
);
681 if (r
&& !strncmp(dma
, "none", 4))
682 adev
->disable_iommu
= 1;
685 adev
->bus
= strtoul(cp
, &cp1
, 16);
690 adev
->dev
= strtoul(cp
, &cp1
, 16);
695 adev
->func
= strtoul(cp
, &cp1
, 16);
697 LIST_INSERT_HEAD(&adev_head
, adev
, next
);
700 fprintf(stderr
, "pcidevice argument parse error; "
701 "please check the help text for usage\n");
706 void add_assigned_devices(PCIBus
*bus
, const char **devices
, int n_devices
)
710 for (i
= 0; i
< n_devices
; i
++) {
711 struct AssignedDevInfo
*adev
;
713 adev
= add_assigned_device(devices
[i
]);
715 fprintf(stderr
, "Could not add assigned device %s\n", devices
[i
]);
719 if (!init_assigned_device(adev
, bus
)) {
720 fprintf(stderr
, "Failed to initialize assigned device %s\n",
727 /* Option ROM header */
728 struct option_rom_header
{
729 uint8_t signature
[2];
731 uint32_t entry_point
;
732 uint8_t reserved
[17];
733 uint16_t pci_header_offset
;
734 uint16_t expansion_header_offset
;
735 } __attribute__ ((packed
));
737 /* Option ROM PCI data structure */
738 struct option_rom_pci_header
{
739 uint8_t signature
[4];
742 uint16_t vital_product_data_offset
;
743 uint16_t structure_length
;
744 uint8_t structure_revision
;
745 uint8_t class_code
[3];
746 uint16_t image_length
;
747 uint16_t image_revision
;
751 } __attribute__ ((packed
));
754 * Scan the list of Option ROMs at roms. If a suitable Option ROM is found,
755 * allocate a ram space and copy it there. Then return its size aligned to
756 * both 2KB and target page size.
758 #define OPTION_ROM_ALIGN(x) (((x) + 2047) & ~2047)
759 static int scan_option_rom(uint8_t devfn
, void *roms
, ram_addr_t offset
)
761 int i
, size
, total_size
;
764 struct option_rom_header
*rom
;
765 struct option_rom_pci_header
*pcih
;
770 /* Invalid signature means we're out of option ROMs. */
771 if (strncmp((char *)rom
->signature
, "\x55\xaa", 2) ||
772 (rom
->rom_size
== 0))
775 size
= rom
->rom_size
* 512;
776 /* Invalid checksum means we're out of option ROMs. */
778 for (i
= 0; i
< size
; i
++)
779 csum
+= ((uint8_t *)rom
)[i
];
783 /* Check the PCI header (if any) for a match. */
784 pcih
= (struct option_rom_pci_header
*)
785 ((char *)rom
+ rom
->pci_header_offset
);
786 if ((rom
->pci_header_offset
!= 0) &&
787 !strncmp((char *)pcih
->signature
, "PCIR", 4))
790 rom
= (struct option_rom_header
*)((char *)rom
+ size
);
796 /* The size should be both 2K-aligned and page-aligned */
797 total_size
= (TARGET_PAGE_SIZE
< 2048)
798 ? OPTION_ROM_ALIGN(size
+ 1)
799 : TARGET_PAGE_ALIGN(size
+ 1);
801 /* Size of all available ram space is 0x10000 (0xd0000 to 0xe0000) */
802 if ((offset
+ total_size
) > 0x10000u
) {
803 fprintf(stderr
, "Option ROM size %x exceeds available space\n", size
);
807 addr
= qemu_ram_alloc(total_size
);
808 cpu_register_physical_memory(0xd0000 + offset
, total_size
, addr
| IO_MEM_ROM
);
810 /* Write ROM data and devfn to phys_addr */
811 cpu_physical_memory_write_rom(0xd0000 + offset
, (uint8_t *)rom
, size
);
812 cpu_physical_memory_write_rom(0xd0000 + offset
+ size
, &devfn
, 1);
818 * Scan the assigned devices for the devices that have an option ROM, and then
819 * load the corresponding ROM data to RAM. If an error occurs while loading an
820 * option ROM, we just ignore that option ROM and continue with the next one.
822 ram_addr_t
assigned_dev_load_option_roms(ram_addr_t rom_base_offset
)
824 ram_addr_t offset
= rom_base_offset
;
825 AssignedDevInfo
*adev
;
827 LIST_FOREACH(adev
, &adev_head
, next
) {
834 snprintf(rom_file
, sizeof(rom_file
),
835 "/sys/bus/pci/devices/0000:%02x:%02x.%01x/rom",
836 adev
->bus
, adev
->dev
, adev
->func
);
838 if (access(rom_file
, F_OK
))
841 /* Write something to the ROM file to enable it */
842 fp
= fopen(rom_file
, "wb");
845 len
= fwrite(&i
, 1, 1, fp
);
850 /* The file has to be closed and reopened, otherwise it won't work */
851 fp
= fopen(rom_file
, "rb");
855 fseek(fp
, 0, SEEK_END
);
857 fseek(fp
, 0, SEEK_SET
);
865 fread(buf
, size
, 1, fp
);
866 if (!feof(fp
) || ferror(fp
)) {
872 /* Scan the buffer for suitable ROMs and increase the offset */
873 offset
+= scan_option_rom(adev
->assigned_dev
->dev
.devfn
, buf
, offset
);