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)
32 #include <sys/types.h>
39 #include "device-assignment.h"
41 /* From linux/ioport.h */
42 #define IORESOURCE_IO 0x00000100 /* Resource type */
43 #define IORESOURCE_MEM 0x00000200
44 #define IORESOURCE_IRQ 0x00000400
45 #define IORESOURCE_DMA 0x00000800
46 #define IORESOURCE_PREFETCH 0x00001000 /* No side effects */
48 /* #define DEVICE_ASSIGNMENT_DEBUG 1 */
50 #ifdef DEVICE_ASSIGNMENT_DEBUG
51 #define DEBUG(fmt, ...) \
53 fprintf(stderr, "%s: " fmt, __func__ , __VA_ARGS__); \
56 #define DEBUG(fmt, ...) do { } while(0)
59 static uint32_t guest_to_host_ioport(AssignedDevRegion
*region
, uint32_t addr
)
61 return region
->u
.r_baseport
+ (addr
- region
->e_physbase
);
64 static void assigned_dev_ioport_writeb(void *opaque
, uint32_t addr
,
67 AssignedDevRegion
*r_access
= opaque
;
68 uint32_t r_pio
= guest_to_host_ioport(r_access
, addr
);
70 DEBUG("r_pio=%08x e_physbase=%08x r_baseport=%08lx value=%08x\n",
71 r_pio
, (int)r_access
->e_physbase
,
72 (unsigned long)r_access
->u
.r_baseport
, value
);
77 static void assigned_dev_ioport_writew(void *opaque
, uint32_t addr
,
80 AssignedDevRegion
*r_access
= opaque
;
81 uint32_t r_pio
= guest_to_host_ioport(r_access
, addr
);
83 DEBUG("r_pio=%08x e_physbase=%08x r_baseport=%08lx value=%08x\n",
84 r_pio
, (int)r_access
->e_physbase
,
85 (unsigned long)r_access
->u
.r_baseport
, value
);
90 static void assigned_dev_ioport_writel(void *opaque
, uint32_t addr
,
93 AssignedDevRegion
*r_access
= opaque
;
94 uint32_t r_pio
= guest_to_host_ioport(r_access
, addr
);
96 DEBUG("r_pio=%08x e_physbase=%08x r_baseport=%08lx value=%08x\n",
97 r_pio
, (int)r_access
->e_physbase
,
98 (unsigned long)r_access
->u
.r_baseport
, value
);
103 static uint32_t assigned_dev_ioport_readb(void *opaque
, uint32_t addr
)
105 AssignedDevRegion
*r_access
= opaque
;
106 uint32_t r_pio
= guest_to_host_ioport(r_access
, addr
);
111 DEBUG("r_pio=%08x e_physbase=%08x r_=%08lx value=%08x\n",
112 r_pio
, (int)r_access
->e_physbase
,
113 (unsigned long)r_access
->u
.r_baseport
, value
);
118 static uint32_t assigned_dev_ioport_readw(void *opaque
, uint32_t addr
)
120 AssignedDevRegion
*r_access
= opaque
;
121 uint32_t r_pio
= guest_to_host_ioport(r_access
, addr
);
126 DEBUG("r_pio=%08x e_physbase=%08x r_baseport=%08lx value=%08x\n",
127 r_pio
, (int)r_access
->e_physbase
,
128 (unsigned long)r_access
->u
.r_baseport
, value
);
133 static uint32_t assigned_dev_ioport_readl(void *opaque
, uint32_t addr
)
135 AssignedDevRegion
*r_access
= opaque
;
136 uint32_t r_pio
= guest_to_host_ioport(r_access
, addr
);
141 DEBUG("r_pio=%08x e_physbase=%08x r_baseport=%08lx value=%08x\n",
142 r_pio
, (int)r_access
->e_physbase
,
143 (unsigned long)r_access
->u
.r_baseport
, value
);
148 static void assigned_dev_iomem_map(PCIDevice
*pci_dev
, int region_num
,
149 uint32_t e_phys
, uint32_t e_size
, int type
)
151 AssignedDevice
*r_dev
= container_of(pci_dev
, AssignedDevice
, dev
);
152 AssignedDevRegion
*region
= &r_dev
->v_addrs
[region_num
];
153 PCIRegion
*real_region
= &r_dev
->real_device
.regions
[region_num
];
154 uint32_t old_ephys
= region
->e_physbase
;
155 uint32_t old_esize
= region
->e_size
;
156 int first_map
= (region
->e_size
== 0);
159 DEBUG("e_phys=%08x r_virt=%p type=%d len=%08x region_num=%d \n",
160 e_phys
, region
->u
.r_virtbase
, type
, e_size
, region_num
);
162 region
->e_physbase
= e_phys
;
163 region
->e_size
= e_size
;
166 kvm_destroy_phys_mem(kvm_context
, old_ephys
,
167 TARGET_PAGE_ALIGN(old_esize
));
170 /* deal with MSI-X MMIO page */
171 if (real_region
->base_addr
<= r_dev
->msix_table_addr
&&
172 real_region
->base_addr
+ real_region
->size
>=
173 r_dev
->msix_table_addr
) {
174 int offset
= r_dev
->msix_table_addr
- real_region
->base_addr
;
175 ret
= munmap(region
->u
.r_virtbase
+ offset
, TARGET_PAGE_SIZE
);
177 DEBUG("munmap done, virt_base 0x%p\n",
178 region
->u
.r_virtbase
+ offset
);
180 fprintf(stderr
, "%s: fail munmap msix table!\n", __func__
);
183 cpu_register_physical_memory(e_phys
+ offset
,
184 TARGET_PAGE_SIZE
, r_dev
->mmio_index
);
186 ret
= kvm_register_phys_mem(kvm_context
, e_phys
,
187 region
->u
.r_virtbase
,
188 TARGET_PAGE_ALIGN(e_size
), 0);
192 fprintf(stderr
, "%s: Error: create new mapping failed\n", __func__
);
197 static void assigned_dev_ioport_map(PCIDevice
*pci_dev
, int region_num
,
198 uint32_t addr
, uint32_t size
, int type
)
200 AssignedDevice
*r_dev
= container_of(pci_dev
, AssignedDevice
, dev
);
201 AssignedDevRegion
*region
= &r_dev
->v_addrs
[region_num
];
202 int first_map
= (region
->e_size
== 0);
205 region
->e_physbase
= addr
;
206 region
->e_size
= size
;
208 DEBUG("e_phys=0x%x r_baseport=%x type=0x%x len=%d region_num=%d \n",
209 addr
, region
->u
.r_baseport
, type
, size
, region_num
);
212 struct ioperm_data
*data
;
214 data
= qemu_mallocz(sizeof(struct ioperm_data
));
216 fprintf(stderr
, "%s: Out of memory\n", __func__
);
220 data
->start_port
= region
->u
.r_baseport
;
221 data
->num
= region
->r_size
;
224 kvm_add_ioperm_data(data
);
226 for (env
= first_cpu
; env
; env
= env
->next_cpu
)
227 kvm_ioperm(env
, data
);
230 register_ioport_read(addr
, size
, 1, assigned_dev_ioport_readb
,
231 (r_dev
->v_addrs
+ region_num
));
232 register_ioport_read(addr
, size
, 2, assigned_dev_ioport_readw
,
233 (r_dev
->v_addrs
+ region_num
));
234 register_ioport_read(addr
, size
, 4, assigned_dev_ioport_readl
,
235 (r_dev
->v_addrs
+ region_num
));
236 register_ioport_write(addr
, size
, 1, assigned_dev_ioport_writeb
,
237 (r_dev
->v_addrs
+ region_num
));
238 register_ioport_write(addr
, size
, 2, assigned_dev_ioport_writew
,
239 (r_dev
->v_addrs
+ region_num
));
240 register_ioport_write(addr
, size
, 4, assigned_dev_ioport_writel
,
241 (r_dev
->v_addrs
+ region_num
));
244 static uint8_t pci_find_cap_offset(struct pci_dev
*pci_dev
, uint8_t cap
)
248 int pos
= PCI_CAPABILITY_LIST
;
251 status
= pci_read_byte(pci_dev
, PCI_STATUS
);
252 if ((status
& PCI_STATUS_CAP_LIST
) == 0)
256 pos
= pci_read_byte(pci_dev
, pos
);
261 id
= pci_read_byte(pci_dev
, pos
+ PCI_CAP_LIST_ID
);
268 pos
+= PCI_CAP_LIST_NEXT
;
273 static void assigned_dev_pci_write_config(PCIDevice
*d
, uint32_t address
,
274 uint32_t val
, int len
)
278 AssignedDevice
*pci_dev
= container_of(d
, AssignedDevice
, dev
);
280 DEBUG("(%x.%x): address=%04x val=0x%08x len=%d\n",
281 ((d
->devfn
>> 3) & 0x1F), (d
->devfn
& 0x7),
282 (uint16_t) address
, val
, len
);
284 if (address
== 0x4) {
285 pci_default_write_config(d
, address
, val
, len
);
286 /* Continue to program the card */
289 if ((address
>= 0x10 && address
<= 0x24) || address
== 0x34 ||
290 address
== 0x3c || address
== 0x3d ||
291 pci_access_cap_config(d
, address
, len
)) {
292 /* used for update-mappings (BAR emulation) */
293 pci_default_write_config(d
, address
, val
, len
);
297 DEBUG("NON BAR (%x.%x): address=%04x val=0x%08x len=%d\n",
298 ((d
->devfn
>> 3) & 0x1F), (d
->devfn
& 0x7),
299 (uint16_t) address
, val
, len
);
301 fd
= pci_dev
->real_device
.config_fd
;
304 ret
= pwrite(fd
, &val
, len
, address
);
306 if ((ret
< 0) && (errno
== EINTR
|| errno
== EAGAIN
))
309 fprintf(stderr
, "%s: pwrite failed, ret = %zd errno = %d\n",
310 __func__
, ret
, errno
);
316 static uint32_t assigned_dev_pci_read_config(PCIDevice
*d
, uint32_t address
,
322 AssignedDevice
*pci_dev
= container_of(d
, AssignedDevice
, dev
);
324 if (address
< 0x4 || (pci_dev
->need_emulate_cmd
&& address
== 0x4) ||
325 (address
>= 0x10 && address
<= 0x24) || address
== 0x34 ||
326 address
== 0x3c || address
== 0x3d ||
327 pci_access_cap_config(d
, address
, len
)) {
328 val
= pci_default_read_config(d
, address
, len
);
329 DEBUG("(%x.%x): address=%04x val=0x%08x len=%d\n",
330 (d
->devfn
>> 3) & 0x1F, (d
->devfn
& 0x7), address
, val
, len
);
334 /* vga specific, remove later */
338 fd
= pci_dev
->real_device
.config_fd
;
341 ret
= pread(fd
, &val
, len
, address
);
343 if ((ret
< 0) && (errno
== EINTR
|| errno
== EAGAIN
))
346 fprintf(stderr
, "%s: pread failed, ret = %zd errno = %d\n",
347 __func__
, ret
, errno
);
353 DEBUG("(%x.%x): address=%04x val=0x%08x len=%d\n",
354 (d
->devfn
>> 3) & 0x1F, (d
->devfn
& 0x7), address
, val
, len
);
356 if (!pci_dev
->cap
.available
) {
357 /* kill the special capabilities */
358 if (address
== 4 && len
== 4)
360 else if (address
== 6)
367 static int assigned_dev_register_regions(PCIRegion
*io_regions
,
368 unsigned long regions_num
,
369 AssignedDevice
*pci_dev
)
372 PCIRegion
*cur_region
= io_regions
;
374 for (i
= 0; i
< regions_num
; i
++, cur_region
++) {
375 if (!cur_region
->valid
)
377 pci_dev
->v_addrs
[i
].num
= i
;
379 /* handle memory io regions */
380 if (cur_region
->type
& IORESOURCE_MEM
) {
381 int t
= cur_region
->type
& IORESOURCE_PREFETCH
382 ? PCI_ADDRESS_SPACE_MEM_PREFETCH
383 : PCI_ADDRESS_SPACE_MEM
;
385 /* map physical memory */
386 pci_dev
->v_addrs
[i
].e_physbase
= cur_region
->base_addr
;
387 pci_dev
->v_addrs
[i
].u
.r_virtbase
=
389 (cur_region
->size
+ 0xFFF) & 0xFFFFF000,
390 PROT_WRITE
| PROT_READ
, MAP_SHARED
,
391 cur_region
->resource_fd
, (off_t
) 0);
393 if (pci_dev
->v_addrs
[i
].u
.r_virtbase
== MAP_FAILED
) {
394 pci_dev
->v_addrs
[i
].u
.r_virtbase
= NULL
;
395 fprintf(stderr
, "%s: Error: Couldn't mmap 0x%x!"
397 (uint32_t) (cur_region
->base_addr
));
400 pci_dev
->v_addrs
[i
].r_size
= cur_region
->size
;
401 pci_dev
->v_addrs
[i
].e_size
= 0;
404 pci_dev
->v_addrs
[i
].u
.r_virtbase
+=
405 (cur_region
->base_addr
& 0xFFF);
407 pci_register_io_region((PCIDevice
*) pci_dev
, i
,
409 assigned_dev_iomem_map
);
412 /* handle port io regions */
413 pci_dev
->v_addrs
[i
].e_physbase
= cur_region
->base_addr
;
414 pci_dev
->v_addrs
[i
].u
.r_baseport
= cur_region
->base_addr
;
415 pci_dev
->v_addrs
[i
].r_size
= cur_region
->size
;
416 pci_dev
->v_addrs
[i
].e_size
= 0;
418 pci_register_io_region((PCIDevice
*) pci_dev
, i
,
419 cur_region
->size
, PCI_ADDRESS_SPACE_IO
,
420 assigned_dev_ioport_map
);
422 /* not relevant for port io */
423 pci_dev
->v_addrs
[i
].memory_index
= 0;
430 static int get_real_device(AssignedDevice
*pci_dev
, uint8_t r_bus
,
431 uint8_t r_dev
, uint8_t r_func
)
433 char dir
[128], name
[128];
436 unsigned long long start
, end
, size
, flags
;
440 PCIDevRegions
*dev
= &pci_dev
->real_device
;
442 dev
->region_number
= 0;
444 snprintf(dir
, sizeof(dir
), "/sys/bus/pci/devices/0000:%02x:%02x.%x/",
445 r_bus
, r_dev
, r_func
);
447 snprintf(name
, sizeof(name
), "%sconfig", dir
);
449 fd
= open(name
, O_RDWR
);
451 fprintf(stderr
, "%s: %s: %m\n", __func__
, name
);
456 r
= read(fd
, pci_dev
->dev
.config
, sizeof(pci_dev
->dev
.config
));
458 if (errno
== EINTR
|| errno
== EAGAIN
)
460 fprintf(stderr
, "%s: read failed, errno = %d\n", __func__
, errno
);
463 snprintf(name
, sizeof(name
), "%sresource", dir
);
465 f
= fopen(name
, "r");
467 fprintf(stderr
, "%s: %s: %m\n", __func__
, name
);
471 for (r
= 0; r
< MAX_IO_REGIONS
; r
++) {
472 if (fscanf(f
, "%lli %lli %lli\n", &start
, &end
, &flags
) != 3)
475 rp
= dev
->regions
+ r
;
477 size
= end
- start
+ 1;
478 flags
&= IORESOURCE_IO
| IORESOURCE_MEM
| IORESOURCE_PREFETCH
;
479 if (size
== 0 || (flags
& ~IORESOURCE_PREFETCH
) == 0)
481 if (flags
& IORESOURCE_MEM
) {
482 flags
&= ~IORESOURCE_IO
;
483 snprintf(name
, sizeof(name
), "%sresource%d", dir
, r
);
484 fd
= open(name
, O_RDWR
);
486 continue; /* probably ROM */
487 rp
->resource_fd
= fd
;
489 flags
&= ~IORESOURCE_PREFETCH
;
493 rp
->base_addr
= start
;
495 DEBUG("region %d size %d start 0x%llx type %d resource_fd %d\n",
496 r
, rp
->size
, start
, rp
->type
, rp
->resource_fd
);
501 /* read and fill device ID */
502 snprintf(name
, sizeof(name
), "%svendor", dir
);
503 f
= fopen(name
, "r");
505 fprintf(stderr
, "%s: %s: %m\n", __func__
, name
);
508 if (fscanf(f
, "%li\n", &id
) == 1) {
509 pci_dev
->dev
.config
[0] = id
& 0xff;
510 pci_dev
->dev
.config
[1] = (id
& 0xff00) >> 8;
514 /* read and fill vendor ID */
515 snprintf(name
, sizeof(name
), "%sdevice", dir
);
516 f
= fopen(name
, "r");
518 fprintf(stderr
, "%s: %s: %m\n", __func__
, name
);
521 if (fscanf(f
, "%li\n", &id
) == 1) {
522 pci_dev
->dev
.config
[2] = id
& 0xff;
523 pci_dev
->dev
.config
[3] = (id
& 0xff00) >> 8;
527 /* dealing with virtual function device */
528 snprintf(name
, sizeof(name
), "%sphysfn/", dir
);
529 if (!stat(name
, &statbuf
))
530 pci_dev
->need_emulate_cmd
= 1;
532 pci_dev
->need_emulate_cmd
= 0;
534 dev
->region_number
= r
;
538 static LIST_HEAD(, AssignedDevInfo
) adev_head
;
540 #ifdef KVM_CAP_IRQ_ROUTING
541 static void free_dev_irq_entries(AssignedDevice
*dev
)
545 for (i
= 0; i
< dev
->irq_entries_nr
; i
++)
546 kvm_del_routing_entry(kvm_context
, &dev
->entry
[i
]);
549 dev
->irq_entries_nr
= 0;
553 static void free_assigned_device(AssignedDevInfo
*adev
)
555 AssignedDevice
*dev
= adev
->assigned_dev
;
560 for (i
= 0; i
< dev
->real_device
.region_number
; i
++) {
561 PCIRegion
*pci_region
= &dev
->real_device
.regions
[i
];
562 AssignedDevRegion
*region
= &dev
->v_addrs
[i
];
564 if (!pci_region
->valid
|| !(pci_region
->type
& IORESOURCE_MEM
))
567 kvm_remove_ioperm_data(region
->u
.r_baseport
, region
->r_size
);
569 if (region
->u
.r_virtbase
) {
570 int ret
= munmap(region
->u
.r_virtbase
,
571 (pci_region
->size
+ 0xFFF) & 0xFFFFF000);
574 "Failed to unmap assigned device region: %s\n",
579 if (dev
->real_device
.config_fd
) {
580 close(dev
->real_device
.config_fd
);
581 dev
->real_device
.config_fd
= 0;
584 pci_unregister_device(&dev
->dev
);
585 #ifdef KVM_CAP_IRQ_ROUTING
586 free_dev_irq_entries(dev
);
588 adev
->assigned_dev
= dev
= NULL
;
591 LIST_REMOVE(adev
, next
);
595 static uint32_t calc_assigned_dev_id(uint8_t bus
, uint8_t devfn
)
597 return (uint32_t)bus
<< 8 | (uint32_t)devfn
;
600 static int assign_device(AssignedDevInfo
*adev
)
602 struct kvm_assigned_pci_dev assigned_dev_data
;
603 AssignedDevice
*dev
= adev
->assigned_dev
;
606 memset(&assigned_dev_data
, 0, sizeof(assigned_dev_data
));
607 assigned_dev_data
.assigned_dev_id
=
608 calc_assigned_dev_id(dev
->h_busnr
, dev
->h_devfn
);
609 assigned_dev_data
.busnr
= dev
->h_busnr
;
610 assigned_dev_data
.devfn
= dev
->h_devfn
;
613 /* We always enable the IOMMU if present
614 * (or when not disabled on the command line)
616 r
= kvm_check_extension(kvm_context
, KVM_CAP_IOMMU
);
617 if (r
&& !adev
->disable_iommu
)
618 assigned_dev_data
.flags
|= KVM_DEV_ASSIGN_ENABLE_IOMMU
;
621 r
= kvm_assign_pci_device(kvm_context
, &assigned_dev_data
);
623 fprintf(stderr
, "Failed to assign device \"%s\" : %s\n",
624 adev
->name
, strerror(-r
));
628 static int assign_irq(AssignedDevInfo
*adev
)
630 struct kvm_assigned_irq assigned_irq_data
;
631 AssignedDevice
*dev
= adev
->assigned_dev
;
634 /* Interrupt PIN 0 means don't use INTx */
635 if (pci_read_byte(dev
->pdev
, PCI_INTERRUPT_PIN
) == 0)
638 irq
= pci_map_irq(&dev
->dev
, dev
->intpin
);
639 irq
= piix_get_irq(irq
);
642 irq
= ipf_map_irq(&dev
->dev
, irq
);
645 if (dev
->girq
== irq
)
648 memset(&assigned_irq_data
, 0, sizeof(assigned_irq_data
));
649 assigned_irq_data
.assigned_dev_id
=
650 calc_assigned_dev_id(dev
->h_busnr
, dev
->h_devfn
);
651 assigned_irq_data
.guest_irq
= irq
;
652 assigned_irq_data
.host_irq
= dev
->real_device
.irq
;
653 #ifdef KVM_CAP_ASSIGN_DEV_IRQ
654 if (dev
->irq_requested_type
) {
655 assigned_irq_data
.flags
= dev
->irq_requested_type
;
656 r
= kvm_deassign_irq(kvm_context
, &assigned_irq_data
);
657 /* -ENXIO means no assigned irq */
658 if (r
&& r
!= -ENXIO
)
659 perror("assign_irq: deassign");
662 assigned_irq_data
.flags
= KVM_DEV_IRQ_GUEST_INTX
;
663 if (dev
->cap
.available
& ASSIGNED_DEVICE_CAP_MSI
)
664 assigned_irq_data
.flags
|= KVM_DEV_IRQ_HOST_MSI
;
666 assigned_irq_data
.flags
|= KVM_DEV_IRQ_HOST_INTX
;
669 r
= kvm_assign_irq(kvm_context
, &assigned_irq_data
);
671 fprintf(stderr
, "Failed to assign irq for \"%s\": %s\n",
672 adev
->name
, strerror(-r
));
673 fprintf(stderr
, "Perhaps you are assigning a device "
674 "that shares an IRQ with another device?\n");
679 dev
->irq_requested_type
= assigned_irq_data
.flags
;
683 static void deassign_device(AssignedDevInfo
*adev
)
685 #ifdef KVM_CAP_DEVICE_DEASSIGNMENT
686 struct kvm_assigned_pci_dev assigned_dev_data
;
687 AssignedDevice
*dev
= adev
->assigned_dev
;
690 memset(&assigned_dev_data
, 0, sizeof(assigned_dev_data
));
691 assigned_dev_data
.assigned_dev_id
=
692 calc_assigned_dev_id(dev
->h_busnr
, dev
->h_devfn
);
694 r
= kvm_deassign_pci_device(kvm_context
, &assigned_dev_data
);
696 fprintf(stderr
, "Failed to deassign device \"%s\" : %s\n",
697 adev
->name
, strerror(-r
));
701 void remove_assigned_device(AssignedDevInfo
*adev
)
703 deassign_device(adev
);
704 free_assigned_device(adev
);
707 AssignedDevInfo
*get_assigned_device(int pcibus
, int slot
)
709 AssignedDevice
*assigned_dev
= NULL
;
710 AssignedDevInfo
*adev
= NULL
;
712 LIST_FOREACH(adev
, &adev_head
, next
) {
713 assigned_dev
= adev
->assigned_dev
;
714 if (pci_bus_num(assigned_dev
->dev
.bus
) == pcibus
&&
715 PCI_SLOT(assigned_dev
->dev
.devfn
) == slot
)
722 /* The pci config space got updated. Check if irq numbers have changed
725 void assigned_dev_update_irqs()
727 AssignedDevInfo
*adev
;
729 adev
= LIST_FIRST(&adev_head
);
731 AssignedDevInfo
*next
= LIST_NEXT(adev
, next
);
734 r
= assign_irq(adev
);
736 remove_assigned_device(adev
);
742 #ifdef KVM_CAP_IRQ_ROUTING
744 #ifdef KVM_CAP_DEVICE_MSI
745 static void assigned_dev_update_msi(PCIDevice
*pci_dev
, unsigned int ctrl_pos
)
747 struct kvm_assigned_irq assigned_irq_data
;
748 AssignedDevice
*assigned_dev
= container_of(pci_dev
, AssignedDevice
, dev
);
749 uint8_t ctrl_byte
= pci_dev
->config
[ctrl_pos
];
752 memset(&assigned_irq_data
, 0, sizeof assigned_irq_data
);
753 assigned_irq_data
.assigned_dev_id
=
754 calc_assigned_dev_id(assigned_dev
->h_busnr
,
755 (uint8_t)assigned_dev
->h_devfn
);
757 if (assigned_dev
->irq_requested_type
) {
758 assigned_irq_data
.flags
= assigned_dev
->irq_requested_type
;
759 free_dev_irq_entries(assigned_dev
);
760 r
= kvm_deassign_irq(kvm_context
, &assigned_irq_data
);
761 /* -ENXIO means no assigned irq */
762 if (r
&& r
!= -ENXIO
)
763 perror("assigned_dev_update_msi: deassign irq");
766 if (ctrl_byte
& PCI_MSI_FLAGS_ENABLE
) {
767 assigned_dev
->entry
= calloc(1, sizeof(struct kvm_irq_routing_entry
));
768 if (!assigned_dev
->entry
) {
769 perror("assigned_dev_update_msi: ");
772 assigned_dev
->entry
->u
.msi
.address_lo
=
773 *(uint32_t *)(pci_dev
->config
+ pci_dev
->cap
.start
+
775 assigned_dev
->entry
->u
.msi
.address_hi
= 0;
776 assigned_dev
->entry
->u
.msi
.data
= *(uint16_t *)(pci_dev
->config
+
777 pci_dev
->cap
.start
+ PCI_MSI_DATA_32
);
778 assigned_dev
->entry
->type
= KVM_IRQ_ROUTING_MSI
;
779 assigned_dev
->entry
->gsi
= kvm_get_irq_route_gsi(kvm_context
);
780 if (assigned_dev
->entry
->gsi
< 0) {
781 perror("assigned_dev_update_msi: kvm_get_irq_route_gsi");
785 kvm_add_routing_entry(kvm_context
, assigned_dev
->entry
);
786 if (kvm_commit_irq_routes(kvm_context
) < 0) {
787 perror("assigned_dev_update_msi: kvm_commit_irq_routes");
788 assigned_dev
->cap
.state
&= ~ASSIGNED_DEVICE_MSI_ENABLED
;
791 assigned_dev
->irq_entries_nr
= 1;
793 assigned_irq_data
.guest_irq
= assigned_dev
->entry
->gsi
;
794 assigned_irq_data
.flags
= KVM_DEV_IRQ_HOST_MSI
| KVM_DEV_IRQ_GUEST_MSI
;
795 if (kvm_assign_irq(kvm_context
, &assigned_irq_data
) < 0)
796 perror("assigned_dev_enable_msi: assign irq");
798 assigned_dev
->irq_requested_type
= assigned_irq_data
.flags
;
803 #ifdef KVM_CAP_DEVICE_MSIX
804 static int assigned_dev_update_msix_mmio(PCIDevice
*pci_dev
)
806 AssignedDevice
*adev
= container_of(pci_dev
, AssignedDevice
, dev
);
807 u16 entries_nr
= 0, entries_max_nr
;
808 int pos
= 0, i
, r
= 0;
809 u32 msg_addr
, msg_upper_addr
, msg_data
, msg_ctrl
;
810 struct kvm_assigned_msix_nr msix_nr
;
811 struct kvm_assigned_msix_entry msix_entry
;
812 void *va
= adev
->msix_table_page
;
814 if (adev
->cap
.available
& ASSIGNED_DEVICE_CAP_MSI
)
815 pos
= pci_dev
->cap
.start
+ PCI_CAPABILITY_CONFIG_MSI_LENGTH
;
817 pos
= pci_dev
->cap
.start
;
819 entries_max_nr
= pci_dev
->config
[pos
+ 2];
820 entries_max_nr
&= PCI_MSIX_TABSIZE
;
823 /* Get the usable entry number for allocating */
824 for (i
= 0; i
< entries_max_nr
; i
++) {
825 memcpy(&msg_ctrl
, va
+ i
* 16 + 12, 4);
826 memcpy(&msg_data
, va
+ i
* 16 + 8, 4);
827 /* Ignore unused entry even it's unmasked */
833 if (entries_nr
== 0) {
834 fprintf(stderr
, "MSI-X entry number is zero!\n");
837 msix_nr
.assigned_dev_id
= calc_assigned_dev_id(adev
->h_busnr
,
838 (uint8_t)adev
->h_devfn
);
839 msix_nr
.entry_nr
= entries_nr
;
840 r
= kvm_assign_set_msix_nr(kvm_context
, &msix_nr
);
842 fprintf(stderr
, "fail to set MSI-X entry number for MSIX! %s\n",
847 free_dev_irq_entries(adev
);
848 adev
->irq_entries_nr
= entries_nr
;
849 adev
->entry
= calloc(entries_nr
, sizeof(struct kvm_irq_routing_entry
));
851 perror("assigned_dev_update_msix_mmio: ");
855 msix_entry
.assigned_dev_id
= msix_nr
.assigned_dev_id
;
857 for (i
= 0; i
< entries_max_nr
; i
++) {
858 if (entries_nr
>= msix_nr
.entry_nr
)
860 memcpy(&msg_ctrl
, va
+ i
* 16 + 12, 4);
863 memcpy(&msg_data
, va
+ i
* 16 + 8, 4);
867 memcpy(&msg_addr
, va
+ i
* 16, 4);
868 memcpy(&msg_upper_addr
, va
+ i
* 16 + 4, 4);
870 r
= kvm_get_irq_route_gsi(kvm_context
);
874 adev
->entry
[entries_nr
].gsi
= r
;
875 adev
->entry
[entries_nr
].type
= KVM_IRQ_ROUTING_MSI
;
876 adev
->entry
[entries_nr
].flags
= 0;
877 adev
->entry
[entries_nr
].u
.msi
.address_lo
= msg_addr
;
878 adev
->entry
[entries_nr
].u
.msi
.address_hi
= msg_upper_addr
;
879 adev
->entry
[entries_nr
].u
.msi
.data
= msg_data
;
880 DEBUG("MSI-X data 0x%x, MSI-X addr_lo 0x%x\n!", msg_data
, msg_addr
);
881 kvm_add_routing_entry(kvm_context
, &adev
->entry
[entries_nr
]);
883 msix_entry
.gsi
= adev
->entry
[entries_nr
].gsi
;
884 msix_entry
.entry
= i
;
885 r
= kvm_assign_set_msix_entry(kvm_context
, &msix_entry
);
887 fprintf(stderr
, "fail to set MSI-X entry! %s\n", strerror(-r
));
890 DEBUG("MSI-X entry gsi 0x%x, entry %d\n!",
891 msix_entry
.gsi
, msix_entry
.entry
);
895 if (r
== 0 && kvm_commit_irq_routes(kvm_context
) < 0) {
896 perror("assigned_dev_update_msix_mmio: kvm_commit_irq_routes");
903 static void assigned_dev_update_msix(PCIDevice
*pci_dev
, unsigned int ctrl_pos
)
905 struct kvm_assigned_irq assigned_irq_data
;
906 AssignedDevice
*assigned_dev
= container_of(pci_dev
, AssignedDevice
, dev
);
907 uint16_t *ctrl_word
= (uint16_t *)(pci_dev
->config
+ ctrl_pos
);
910 memset(&assigned_irq_data
, 0, sizeof assigned_irq_data
);
911 assigned_irq_data
.assigned_dev_id
=
912 calc_assigned_dev_id(assigned_dev
->h_busnr
,
913 (uint8_t)assigned_dev
->h_devfn
);
915 if (assigned_dev
->irq_requested_type
) {
916 assigned_irq_data
.flags
= assigned_dev
->irq_requested_type
;
917 free_dev_irq_entries(assigned_dev
);
918 r
= kvm_deassign_irq(kvm_context
, &assigned_irq_data
);
919 /* -ENXIO means no assigned irq */
920 if (r
&& r
!= -ENXIO
)
921 perror("assigned_dev_update_msix: deassign irq");
923 assigned_irq_data
.flags
= KVM_DEV_IRQ_HOST_MSIX
| KVM_DEV_IRQ_GUEST_MSIX
;
925 if (*ctrl_word
& PCI_MSIX_ENABLE
) {
926 if (assigned_dev_update_msix_mmio(pci_dev
) < 0) {
927 perror("assigned_dev_update_msix_mmio");
930 if (kvm_assign_irq(kvm_context
, &assigned_irq_data
) < 0) {
931 perror("assigned_dev_enable_msix: assign irq");
934 assigned_dev
->irq_requested_type
= assigned_irq_data
.flags
;
940 static void assigned_device_pci_cap_write_config(PCIDevice
*pci_dev
, uint32_t address
,
941 uint32_t val
, int len
)
943 AssignedDevice
*assigned_dev
= container_of(pci_dev
, AssignedDevice
, dev
);
944 unsigned int pos
= pci_dev
->cap
.start
, ctrl_pos
;
946 pci_default_cap_write_config(pci_dev
, address
, val
, len
);
947 #ifdef KVM_CAP_IRQ_ROUTING
948 #ifdef KVM_CAP_DEVICE_MSI
949 if (assigned_dev
->cap
.available
& ASSIGNED_DEVICE_CAP_MSI
) {
950 ctrl_pos
= pos
+ PCI_MSI_FLAGS
;
951 if (address
<= ctrl_pos
&& address
+ len
> ctrl_pos
)
952 assigned_dev_update_msi(pci_dev
, ctrl_pos
);
953 pos
+= PCI_CAPABILITY_CONFIG_MSI_LENGTH
;
956 #ifdef KVM_CAP_DEVICE_MSIX
957 if (assigned_dev
->cap
.available
& ASSIGNED_DEVICE_CAP_MSIX
) {
959 if (address
<= ctrl_pos
&& address
+ len
> ctrl_pos
) {
960 ctrl_pos
--; /* control is word long */
961 assigned_dev_update_msix(pci_dev
, ctrl_pos
);
963 pos
+= PCI_CAPABILITY_CONFIG_MSIX_LENGTH
;
970 static int assigned_device_pci_cap_init(PCIDevice
*pci_dev
)
972 AssignedDevice
*dev
= container_of(pci_dev
, AssignedDevice
, dev
);
973 PCIRegion
*pci_region
= dev
->real_device
.regions
;
976 pci_dev
->cap
.length
= 0;
977 #ifdef KVM_CAP_IRQ_ROUTING
978 #ifdef KVM_CAP_DEVICE_MSI
979 /* Expose MSI capability
980 * MSI capability is the 1st capability in capability config */
981 if (pci_find_cap_offset(dev
->pdev
, PCI_CAP_ID_MSI
)) {
982 dev
->cap
.available
|= ASSIGNED_DEVICE_CAP_MSI
;
983 memset(&pci_dev
->config
[pci_dev
->cap
.start
+ pci_dev
->cap
.length
],
984 0, PCI_CAPABILITY_CONFIG_MSI_LENGTH
);
985 pci_dev
->config
[pci_dev
->cap
.start
+ pci_dev
->cap
.length
] =
987 pci_dev
->cap
.length
+= PCI_CAPABILITY_CONFIG_MSI_LENGTH
;
991 #ifdef KVM_CAP_DEVICE_MSIX
992 /* Expose MSI-X capability */
993 if (pci_find_cap_offset(dev
->pdev
, PCI_CAP_ID_MSIX
)) {
994 int pos
, entry_nr
, bar_nr
;
995 u32 msix_table_entry
;
996 dev
->cap
.available
|= ASSIGNED_DEVICE_CAP_MSIX
;
997 memset(&pci_dev
->config
[pci_dev
->cap
.start
+ pci_dev
->cap
.length
],
998 0, PCI_CAPABILITY_CONFIG_MSIX_LENGTH
);
999 pos
= pci_find_cap_offset(dev
->pdev
, PCI_CAP_ID_MSIX
);
1000 entry_nr
= pci_read_word(dev
->pdev
, pos
+ 2) & PCI_MSIX_TABSIZE
;
1001 pci_dev
->config
[pci_dev
->cap
.start
+ pci_dev
->cap
.length
] = 0x11;
1002 pci_dev
->config
[pci_dev
->cap
.start
+
1003 pci_dev
->cap
.length
+ 2] = entry_nr
;
1004 msix_table_entry
= pci_read_long(dev
->pdev
, pos
+ PCI_MSIX_TABLE
);
1005 *(uint32_t *)(pci_dev
->config
+ pci_dev
->cap
.start
+
1006 pci_dev
->cap
.length
+ PCI_MSIX_TABLE
) = msix_table_entry
;
1007 *(uint32_t *)(pci_dev
->config
+ pci_dev
->cap
.start
+
1008 pci_dev
->cap
.length
+ PCI_MSIX_PBA
) =
1009 pci_read_long(dev
->pdev
, pos
+ PCI_MSIX_PBA
);
1010 bar_nr
= msix_table_entry
& PCI_MSIX_BIR
;
1011 msix_table_entry
&= ~PCI_MSIX_BIR
;
1012 dev
->msix_table_addr
= pci_region
[bar_nr
].base_addr
+ msix_table_entry
;
1013 if (next_cap_pt
!= 0) {
1014 pci_dev
->config
[pci_dev
->cap
.start
+ next_cap_pt
] =
1015 pci_dev
->cap
.start
+ pci_dev
->cap
.length
;
1016 next_cap_pt
+= PCI_CAPABILITY_CONFIG_MSI_LENGTH
;
1019 pci_dev
->cap
.length
+= PCI_CAPABILITY_CONFIG_MSIX_LENGTH
;
1027 static uint32_t msix_mmio_readl(void *opaque
, target_phys_addr_t addr
)
1029 AssignedDevice
*adev
= opaque
;
1030 unsigned int offset
= addr
& 0xfff;
1031 void *page
= adev
->msix_table_page
;
1034 memcpy(&val
, (void *)((char *)page
+ offset
), 4);
1039 static uint32_t msix_mmio_readb(void *opaque
, target_phys_addr_t addr
)
1041 return ((msix_mmio_readl(opaque
, addr
& ~3)) >>
1042 (8 * (addr
& 3))) & 0xff;
1045 static uint32_t msix_mmio_readw(void *opaque
, target_phys_addr_t addr
)
1047 return ((msix_mmio_readl(opaque
, addr
& ~3)) >>
1048 (8 * (addr
& 3))) & 0xffff;
1051 static void msix_mmio_writel(void *opaque
,
1052 target_phys_addr_t addr
, uint32_t val
)
1054 AssignedDevice
*adev
= opaque
;
1055 unsigned int offset
= addr
& 0xfff;
1056 void *page
= adev
->msix_table_page
;
1058 DEBUG("write to MSI-X entry table mmio offset 0x%lx, val 0x%lx\n",
1060 memcpy((void *)((char *)page
+ offset
), &val
, 4);
1063 static void msix_mmio_writew(void *opaque
,
1064 target_phys_addr_t addr
, uint32_t val
)
1066 msix_mmio_writel(opaque
, addr
& ~3,
1067 (val
& 0xffff) << (8*(addr
& 3)));
1070 static void msix_mmio_writeb(void *opaque
,
1071 target_phys_addr_t addr
, uint32_t val
)
1073 msix_mmio_writel(opaque
, addr
& ~3,
1074 (val
& 0xff) << (8*(addr
& 3)));
1077 static CPUWriteMemoryFunc
*msix_mmio_write
[] = {
1078 msix_mmio_writeb
, msix_mmio_writew
, msix_mmio_writel
1081 static CPUReadMemoryFunc
*msix_mmio_read
[] = {
1082 msix_mmio_readb
, msix_mmio_readw
, msix_mmio_readl
1085 static int assigned_dev_register_msix_mmio(AssignedDevice
*dev
)
1087 dev
->msix_table_page
= mmap(NULL
, 0x1000,
1088 PROT_READ
|PROT_WRITE
,
1089 MAP_ANONYMOUS
|MAP_PRIVATE
, 0, 0);
1090 memset(dev
->msix_table_page
, 0, 0x1000);
1091 if (dev
->msix_table_page
== MAP_FAILED
) {
1092 fprintf(stderr
, "fail allocate msix_table_page! %s\n",
1096 dev
->mmio_index
= cpu_register_io_memory(0,
1097 msix_mmio_read
, msix_mmio_write
, dev
);
1101 struct PCIDevice
*init_assigned_device(AssignedDevInfo
*adev
, PCIBus
*bus
)
1104 AssignedDevice
*dev
;
1106 struct pci_access
*pacc
;
1107 uint8_t e_device
, e_intx
;
1109 DEBUG("Registering real physical device %s (bus=%x dev=%x func=%x)\n",
1110 adev
->name
, adev
->bus
, adev
->dev
, adev
->func
);
1112 pci_dev
= pci_register_device(bus
, adev
->name
,
1113 sizeof(AssignedDevice
), -1, assigned_dev_pci_read_config
,
1114 assigned_dev_pci_write_config
);
1115 dev
= container_of(pci_dev
, AssignedDevice
, dev
);
1118 fprintf(stderr
, "%s: Error: Couldn't register real device %s\n",
1119 __func__
, adev
->name
);
1123 adev
->assigned_dev
= dev
;
1125 if (get_real_device(dev
, adev
->bus
, adev
->dev
, adev
->func
)) {
1126 fprintf(stderr
, "%s: Error: Couldn't get real device (%s)!\n",
1127 __func__
, adev
->name
);
1131 /* handle real device's MMIO/PIO BARs */
1132 if (assigned_dev_register_regions(dev
->real_device
.regions
,
1133 dev
->real_device
.region_number
,
1137 /* handle interrupt routing */
1138 e_device
= (dev
->dev
.devfn
>> 3) & 0x1f;
1139 e_intx
= dev
->dev
.config
[0x3d] - 1;
1140 dev
->intpin
= e_intx
;
1143 dev
->h_busnr
= adev
->bus
;
1144 dev
->h_devfn
= PCI_DEVFN(adev
->dev
, adev
->func
);
1148 dev
->pdev
= pci_get_dev(pacc
, 0, adev
->bus
, adev
->dev
, adev
->func
);
1150 if (pci_enable_capability_support(pci_dev
, 0, NULL
,
1151 assigned_device_pci_cap_write_config
,
1152 assigned_device_pci_cap_init
) < 0)
1155 /* assign device to guest */
1156 r
= assign_device(adev
);
1160 /* assign irq for the device */
1161 r
= assign_irq(adev
);
1165 /* intercept MSI-X entry page in the MMIO */
1166 if (dev
->cap
.available
& ASSIGNED_DEVICE_CAP_MSIX
)
1167 if (assigned_dev_register_msix_mmio(dev
))
1173 deassign_device(adev
);
1175 free_assigned_device(adev
);
1180 * Syntax to assign device:
1182 * -pcidevice host=bus:dev.func[,dma=none][,name=Foo]
1185 * -pcidevice host=00:13.0,dma=pvdma
1187 * dma can currently only be 'none' to disable iommu support.
1189 AssignedDevInfo
*add_assigned_device(const char *arg
)
1194 AssignedDevInfo
*adev
;
1196 adev
= qemu_mallocz(sizeof(AssignedDevInfo
));
1198 fprintf(stderr
, "%s: Out of memory\n", __func__
);
1201 r
= get_param_value(device
, sizeof(device
), "host", arg
);
1205 r
= pci_parse_host_devaddr(device
, &adev
->bus
, &adev
->dev
, &adev
->func
);
1209 r
= get_param_value(adev
->name
, sizeof(adev
->name
), "name", arg
);
1211 snprintf(adev
->name
, sizeof(adev
->name
), "%s", device
);
1213 #ifdef KVM_CAP_IOMMU
1214 r
= get_param_value(dma
, sizeof(dma
), "dma", arg
);
1215 if (r
&& !strncmp(dma
, "none", 4))
1216 adev
->disable_iommu
= 1;
1219 LIST_INSERT_HEAD(&adev_head
, adev
, next
);
1222 fprintf(stderr
, "pcidevice argument parse error; "
1223 "please check the help text for usage\n");
1228 void add_assigned_devices(PCIBus
*bus
, const char **devices
, int n_devices
)
1232 for (i
= 0; i
< n_devices
; i
++) {
1233 struct AssignedDevInfo
*adev
;
1235 adev
= add_assigned_device(devices
[i
]);
1237 fprintf(stderr
, "Could not add assigned device %s\n", devices
[i
]);
1241 if (!init_assigned_device(adev
, bus
)) {
1242 fprintf(stderr
, "Failed to initialize assigned device %s\n",
1249 /* Option ROM header */
1250 struct option_rom_header
{
1251 uint8_t signature
[2];
1253 uint32_t entry_point
;
1254 uint8_t reserved
[17];
1255 uint16_t pci_header_offset
;
1256 uint16_t expansion_header_offset
;
1257 } __attribute__ ((packed
));
1259 /* Option ROM PCI data structure */
1260 struct option_rom_pci_header
{
1261 uint8_t signature
[4];
1264 uint16_t vital_product_data_offset
;
1265 uint16_t structure_length
;
1266 uint8_t structure_revision
;
1267 uint8_t class_code
[3];
1268 uint16_t image_length
;
1269 uint16_t image_revision
;
1273 } __attribute__ ((packed
));
1276 * Scan the list of Option ROMs at roms. If a suitable Option ROM is found,
1277 * allocate a ram space and copy it there. Then return its size aligned to
1278 * both 2KB and target page size.
1280 #define OPTION_ROM_ALIGN(x) (((x) + 2047) & ~2047)
1281 static int scan_option_rom(uint8_t devfn
, void *roms
, ram_addr_t offset
)
1283 int i
, size
, total_size
;
1286 struct option_rom_header
*rom
;
1287 struct option_rom_pci_header
*pcih
;
1292 /* Invalid signature means we're out of option ROMs. */
1293 if (strncmp((char *)rom
->signature
, "\x55\xaa", 2) ||
1294 (rom
->rom_size
== 0))
1297 size
= rom
->rom_size
* 512;
1298 /* Invalid checksum means we're out of option ROMs. */
1300 for (i
= 0; i
< size
; i
++)
1301 csum
+= ((uint8_t *)rom
)[i
];
1305 /* Check the PCI header (if any) for a match. */
1306 pcih
= (struct option_rom_pci_header
*)
1307 ((char *)rom
+ rom
->pci_header_offset
);
1308 if ((rom
->pci_header_offset
!= 0) &&
1309 !strncmp((char *)pcih
->signature
, "PCIR", 4))
1312 rom
= (struct option_rom_header
*)((char *)rom
+ size
);
1318 /* The size should be both 2K-aligned and page-aligned */
1319 total_size
= (TARGET_PAGE_SIZE
< 2048)
1320 ? OPTION_ROM_ALIGN(size
+ 1)
1321 : TARGET_PAGE_ALIGN(size
+ 1);
1323 /* Size of all available ram space is 0x10000 (0xd0000 to 0xe0000) */
1324 if ((offset
+ total_size
) > 0x10000u
) {
1325 fprintf(stderr
, "Option ROM size %x exceeds available space\n", size
);
1329 addr
= qemu_ram_alloc(total_size
);
1330 cpu_register_physical_memory(0xd0000 + offset
, total_size
, addr
| IO_MEM_ROM
);
1332 /* Write ROM data and devfn to phys_addr */
1333 cpu_physical_memory_write_rom(0xd0000 + offset
, (uint8_t *)rom
, size
);
1334 cpu_physical_memory_write_rom(0xd0000 + offset
+ size
, &devfn
, 1);
1340 * Scan the assigned devices for the devices that have an option ROM, and then
1341 * load the corresponding ROM data to RAM. If an error occurs while loading an
1342 * option ROM, we just ignore that option ROM and continue with the next one.
1344 ram_addr_t
assigned_dev_load_option_roms(ram_addr_t rom_base_offset
)
1346 ram_addr_t offset
= rom_base_offset
;
1347 AssignedDevInfo
*adev
;
1349 LIST_FOREACH(adev
, &adev_head
, next
) {
1356 snprintf(rom_file
, sizeof(rom_file
),
1357 "/sys/bus/pci/devices/0000:%02x:%02x.%01x/rom",
1358 adev
->bus
, adev
->dev
, adev
->func
);
1360 if (access(rom_file
, F_OK
))
1363 /* Write something to the ROM file to enable it */
1364 fp
= fopen(rom_file
, "wb");
1367 len
= fwrite(&i
, 1, 1, fp
);
1372 /* The file has to be closed and reopened, otherwise it won't work */
1373 fp
= fopen(rom_file
, "rb");
1377 fseek(fp
, 0, SEEK_END
);
1379 fseek(fp
, 0, SEEK_SET
);
1387 fread(buf
, size
, 1, fp
);
1388 if (!feof(fp
) || ferror(fp
)) {
1394 /* Scan the buffer for suitable ROMs and increase the offset */
1395 offset
+= scan_option_rom(adev
->assigned_dev
->dev
.devfn
, buf
, offset
);