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
;
822 /* Get the usable entry number for allocating */
823 for (i
= 0; i
< entries_max_nr
; i
++) {
824 memcpy(&msg_ctrl
, va
+ i
* 16 + 12, 4);
825 memcpy(&msg_data
, va
+ i
* 16 + 8, 4);
826 /* Ignore unused entry even it's unmasked */
832 if (entries_nr
== 0) {
833 fprintf(stderr
, "MSI-X entry number is zero!\n");
836 msix_nr
.assigned_dev_id
= calc_assigned_dev_id(adev
->h_busnr
,
837 (uint8_t)adev
->h_devfn
);
838 msix_nr
.entry_nr
= entries_nr
;
839 r
= kvm_assign_set_msix_nr(kvm_context
, &msix_nr
);
841 fprintf(stderr
, "fail to set MSI-X entry number for MSIX! %s\n",
846 free_dev_irq_entries(adev
);
847 adev
->irq_entries_nr
= entries_nr
;
848 adev
->entry
= calloc(entries_nr
, sizeof(struct kvm_irq_routing_entry
));
850 perror("assigned_dev_update_msix_mmio: ");
854 msix_entry
.assigned_dev_id
= msix_nr
.assigned_dev_id
;
856 for (i
= 0; i
< entries_max_nr
; i
++) {
857 if (entries_nr
>= msix_nr
.entry_nr
)
859 memcpy(&msg_ctrl
, va
+ i
* 16 + 12, 4);
862 memcpy(&msg_data
, va
+ i
* 16 + 8, 4);
866 memcpy(&msg_addr
, va
+ i
* 16, 4);
867 memcpy(&msg_upper_addr
, va
+ i
* 16 + 4, 4);
869 r
= kvm_get_irq_route_gsi(kvm_context
);
873 adev
->entry
[entries_nr
].gsi
= r
;
874 adev
->entry
[entries_nr
].type
= KVM_IRQ_ROUTING_MSI
;
875 adev
->entry
[entries_nr
].flags
= 0;
876 adev
->entry
[entries_nr
].u
.msi
.address_lo
= msg_addr
;
877 adev
->entry
[entries_nr
].u
.msi
.address_hi
= msg_upper_addr
;
878 adev
->entry
[entries_nr
].u
.msi
.data
= msg_data
;
879 DEBUG("MSI-X data 0x%x, MSI-X addr_lo 0x%x\n!", msg_data
, msg_addr
);
880 kvm_add_routing_entry(kvm_context
, &adev
->entry
[entries_nr
]);
882 msix_entry
.gsi
= adev
->entry
[entries_nr
].gsi
;
883 msix_entry
.entry
= i
;
884 r
= kvm_assign_set_msix_entry(kvm_context
, &msix_entry
);
886 fprintf(stderr
, "fail to set MSI-X entry! %s\n", strerror(-r
));
889 DEBUG("MSI-X entry gsi 0x%x, entry %d\n!",
890 msix_entry
.gsi
, msix_entry
.entry
);
894 if (r
== 0 && kvm_commit_irq_routes(kvm_context
) < 0) {
895 perror("assigned_dev_update_msix_mmio: kvm_commit_irq_routes");
902 static void assigned_dev_update_msix(PCIDevice
*pci_dev
, unsigned int ctrl_pos
)
904 struct kvm_assigned_irq assigned_irq_data
;
905 AssignedDevice
*assigned_dev
= container_of(pci_dev
, AssignedDevice
, dev
);
906 uint16_t *ctrl_word
= (uint16_t *)(pci_dev
->config
+ ctrl_pos
);
909 memset(&assigned_irq_data
, 0, sizeof assigned_irq_data
);
910 assigned_irq_data
.assigned_dev_id
=
911 calc_assigned_dev_id(assigned_dev
->h_busnr
,
912 (uint8_t)assigned_dev
->h_devfn
);
914 if (assigned_dev
->irq_requested_type
) {
915 assigned_irq_data
.flags
= assigned_dev
->irq_requested_type
;
916 free_dev_irq_entries(assigned_dev
);
917 r
= kvm_deassign_irq(kvm_context
, &assigned_irq_data
);
918 /* -ENXIO means no assigned irq */
919 if (r
&& r
!= -ENXIO
)
920 perror("assigned_dev_update_msix: deassign irq");
922 assigned_irq_data
.flags
= KVM_DEV_IRQ_HOST_MSIX
| KVM_DEV_IRQ_GUEST_MSIX
;
924 if (*ctrl_word
& PCI_MSIX_ENABLE
) {
925 if (assigned_dev_update_msix_mmio(pci_dev
) < 0) {
926 perror("assigned_dev_update_msix_mmio");
929 if (kvm_assign_irq(kvm_context
, &assigned_irq_data
) < 0) {
930 perror("assigned_dev_enable_msix: assign irq");
933 assigned_dev
->irq_requested_type
= assigned_irq_data
.flags
;
939 static void assigned_device_pci_cap_write_config(PCIDevice
*pci_dev
, uint32_t address
,
940 uint32_t val
, int len
)
942 AssignedDevice
*assigned_dev
= container_of(pci_dev
, AssignedDevice
, dev
);
943 unsigned int pos
= pci_dev
->cap
.start
, ctrl_pos
;
945 pci_default_cap_write_config(pci_dev
, address
, val
, len
);
946 #ifdef KVM_CAP_IRQ_ROUTING
947 #ifdef KVM_CAP_DEVICE_MSI
948 if (assigned_dev
->cap
.available
& ASSIGNED_DEVICE_CAP_MSI
) {
949 ctrl_pos
= pos
+ PCI_MSI_FLAGS
;
950 if (address
<= ctrl_pos
&& address
+ len
> ctrl_pos
)
951 assigned_dev_update_msi(pci_dev
, ctrl_pos
);
952 pos
+= PCI_CAPABILITY_CONFIG_MSI_LENGTH
;
955 #ifdef KVM_CAP_DEVICE_MSIX
956 if (assigned_dev
->cap
.available
& ASSIGNED_DEVICE_CAP_MSIX
) {
958 if (address
<= ctrl_pos
&& address
+ len
> ctrl_pos
) {
959 ctrl_pos
--; /* control is word long */
960 assigned_dev_update_msix(pci_dev
, ctrl_pos
);
962 pos
+= PCI_CAPABILITY_CONFIG_MSIX_LENGTH
;
969 static int assigned_device_pci_cap_init(PCIDevice
*pci_dev
)
971 AssignedDevice
*dev
= container_of(pci_dev
, AssignedDevice
, dev
);
972 PCIRegion
*pci_region
= dev
->real_device
.regions
;
975 pci_dev
->cap
.length
= 0;
976 #ifdef KVM_CAP_IRQ_ROUTING
977 #ifdef KVM_CAP_DEVICE_MSI
978 /* Expose MSI capability
979 * MSI capability is the 1st capability in capability config */
980 if (pci_find_cap_offset(dev
->pdev
, PCI_CAP_ID_MSI
)) {
981 dev
->cap
.available
|= ASSIGNED_DEVICE_CAP_MSI
;
982 memset(&pci_dev
->config
[pci_dev
->cap
.start
+ pci_dev
->cap
.length
],
983 0, PCI_CAPABILITY_CONFIG_MSI_LENGTH
);
984 pci_dev
->config
[pci_dev
->cap
.start
+ pci_dev
->cap
.length
] =
986 pci_dev
->cap
.length
+= PCI_CAPABILITY_CONFIG_MSI_LENGTH
;
990 #ifdef KVM_CAP_DEVICE_MSIX
991 /* Expose MSI-X capability */
992 if (pci_find_cap_offset(dev
->pdev
, PCI_CAP_ID_MSIX
)) {
993 int pos
, entry_nr
, bar_nr
;
994 u32 msix_table_entry
;
995 dev
->cap
.available
|= ASSIGNED_DEVICE_CAP_MSIX
;
996 memset(&pci_dev
->config
[pci_dev
->cap
.start
+ pci_dev
->cap
.length
],
997 0, PCI_CAPABILITY_CONFIG_MSIX_LENGTH
);
998 pos
= pci_find_cap_offset(dev
->pdev
, PCI_CAP_ID_MSIX
);
999 entry_nr
= pci_read_word(dev
->pdev
, pos
+ 2) & PCI_MSIX_TABSIZE
;
1000 pci_dev
->config
[pci_dev
->cap
.start
+ pci_dev
->cap
.length
] = 0x11;
1001 pci_dev
->config
[pci_dev
->cap
.start
+
1002 pci_dev
->cap
.length
+ 2] = entry_nr
;
1003 msix_table_entry
= pci_read_long(dev
->pdev
, pos
+ PCI_MSIX_TABLE
);
1004 *(uint32_t *)(pci_dev
->config
+ pci_dev
->cap
.start
+
1005 pci_dev
->cap
.length
+ PCI_MSIX_TABLE
) = msix_table_entry
;
1006 *(uint32_t *)(pci_dev
->config
+ pci_dev
->cap
.start
+
1007 pci_dev
->cap
.length
+ PCI_MSIX_PBA
) =
1008 pci_read_long(dev
->pdev
, pos
+ PCI_MSIX_PBA
);
1009 bar_nr
= msix_table_entry
& PCI_MSIX_BIR
;
1010 msix_table_entry
&= ~PCI_MSIX_BIR
;
1011 dev
->msix_table_addr
= pci_region
[bar_nr
].base_addr
+ msix_table_entry
;
1012 if (next_cap_pt
!= 0) {
1013 pci_dev
->config
[pci_dev
->cap
.start
+ next_cap_pt
] =
1014 pci_dev
->cap
.start
+ pci_dev
->cap
.length
;
1015 next_cap_pt
+= PCI_CAPABILITY_CONFIG_MSI_LENGTH
;
1018 pci_dev
->cap
.length
+= PCI_CAPABILITY_CONFIG_MSIX_LENGTH
;
1026 static uint32_t msix_mmio_readl(void *opaque
, target_phys_addr_t addr
)
1028 AssignedDevice
*adev
= opaque
;
1029 unsigned int offset
= addr
& 0xfff;
1030 void *page
= adev
->msix_table_page
;
1033 memcpy(&val
, (void *)((char *)page
+ offset
), 4);
1038 static uint32_t msix_mmio_readb(void *opaque
, target_phys_addr_t addr
)
1040 return ((msix_mmio_readl(opaque
, addr
& ~3)) >>
1041 (8 * (addr
& 3))) & 0xff;
1044 static uint32_t msix_mmio_readw(void *opaque
, target_phys_addr_t addr
)
1046 return ((msix_mmio_readl(opaque
, addr
& ~3)) >>
1047 (8 * (addr
& 3))) & 0xffff;
1050 static void msix_mmio_writel(void *opaque
,
1051 target_phys_addr_t addr
, uint32_t val
)
1053 AssignedDevice
*adev
= opaque
;
1054 unsigned int offset
= addr
& 0xfff;
1055 void *page
= adev
->msix_table_page
;
1057 DEBUG("write to MSI-X entry table mmio offset 0x%lx, val 0x%lx\n",
1059 memcpy((void *)((char *)page
+ offset
), &val
, 4);
1062 static void msix_mmio_writew(void *opaque
,
1063 target_phys_addr_t addr
, uint32_t val
)
1065 msix_mmio_writel(opaque
, addr
& ~3,
1066 (val
& 0xffff) << (8*(addr
& 3)));
1069 static void msix_mmio_writeb(void *opaque
,
1070 target_phys_addr_t addr
, uint32_t val
)
1072 msix_mmio_writel(opaque
, addr
& ~3,
1073 (val
& 0xff) << (8*(addr
& 3)));
1076 static CPUWriteMemoryFunc
*msix_mmio_write
[] = {
1077 msix_mmio_writeb
, msix_mmio_writew
, msix_mmio_writel
1080 static CPUReadMemoryFunc
*msix_mmio_read
[] = {
1081 msix_mmio_readb
, msix_mmio_readw
, msix_mmio_readl
1084 static int assigned_dev_register_msix_mmio(AssignedDevice
*dev
)
1086 dev
->msix_table_page
= mmap(NULL
, 0x1000,
1087 PROT_READ
|PROT_WRITE
,
1088 MAP_ANONYMOUS
|MAP_PRIVATE
, 0, 0);
1089 memset(dev
->msix_table_page
, 0, 0x1000);
1090 if (dev
->msix_table_page
== MAP_FAILED
) {
1091 fprintf(stderr
, "fail allocate msix_table_page! %s\n",
1095 dev
->mmio_index
= cpu_register_io_memory(0,
1096 msix_mmio_read
, msix_mmio_write
, dev
);
1100 struct PCIDevice
*init_assigned_device(AssignedDevInfo
*adev
, PCIBus
*bus
)
1103 AssignedDevice
*dev
;
1105 struct pci_access
*pacc
;
1106 uint8_t e_device
, e_intx
;
1108 DEBUG("Registering real physical device %s (bus=%x dev=%x func=%x)\n",
1109 adev
->name
, adev
->bus
, adev
->dev
, adev
->func
);
1111 pci_dev
= pci_register_device(bus
, adev
->name
,
1112 sizeof(AssignedDevice
), -1, assigned_dev_pci_read_config
,
1113 assigned_dev_pci_write_config
);
1114 dev
= container_of(pci_dev
, AssignedDevice
, dev
);
1117 fprintf(stderr
, "%s: Error: Couldn't register real device %s\n",
1118 __func__
, adev
->name
);
1122 adev
->assigned_dev
= dev
;
1124 if (get_real_device(dev
, adev
->bus
, adev
->dev
, adev
->func
)) {
1125 fprintf(stderr
, "%s: Error: Couldn't get real device (%s)!\n",
1126 __func__
, adev
->name
);
1130 /* handle real device's MMIO/PIO BARs */
1131 if (assigned_dev_register_regions(dev
->real_device
.regions
,
1132 dev
->real_device
.region_number
,
1136 /* handle interrupt routing */
1137 e_device
= (dev
->dev
.devfn
>> 3) & 0x1f;
1138 e_intx
= dev
->dev
.config
[0x3d] - 1;
1139 dev
->intpin
= e_intx
;
1142 dev
->h_busnr
= adev
->bus
;
1143 dev
->h_devfn
= PCI_DEVFN(adev
->dev
, adev
->func
);
1147 dev
->pdev
= pci_get_dev(pacc
, 0, adev
->bus
, adev
->dev
, adev
->func
);
1149 if (pci_enable_capability_support(pci_dev
, 0, NULL
,
1150 assigned_device_pci_cap_write_config
,
1151 assigned_device_pci_cap_init
) < 0)
1154 /* assign device to guest */
1155 r
= assign_device(adev
);
1159 /* assign irq for the device */
1160 r
= assign_irq(adev
);
1164 /* intercept MSI-X entry page in the MMIO */
1165 if (dev
->cap
.available
& ASSIGNED_DEVICE_CAP_MSIX
)
1166 if (assigned_dev_register_msix_mmio(dev
))
1172 deassign_device(adev
);
1174 free_assigned_device(adev
);
1179 * Syntax to assign device:
1181 * -pcidevice host=bus:dev.func[,dma=none][,name=Foo]
1184 * -pcidevice host=00:13.0,dma=pvdma
1186 * dma can currently only be 'none' to disable iommu support.
1188 AssignedDevInfo
*add_assigned_device(const char *arg
)
1193 AssignedDevInfo
*adev
;
1195 adev
= qemu_mallocz(sizeof(AssignedDevInfo
));
1197 fprintf(stderr
, "%s: Out of memory\n", __func__
);
1200 r
= get_param_value(device
, sizeof(device
), "host", arg
);
1204 r
= pci_parse_host_devaddr(device
, &adev
->bus
, &adev
->dev
, &adev
->func
);
1208 r
= get_param_value(adev
->name
, sizeof(adev
->name
), "name", arg
);
1210 snprintf(adev
->name
, sizeof(adev
->name
), "%s", device
);
1212 #ifdef KVM_CAP_IOMMU
1213 r
= get_param_value(dma
, sizeof(dma
), "dma", arg
);
1214 if (r
&& !strncmp(dma
, "none", 4))
1215 adev
->disable_iommu
= 1;
1218 LIST_INSERT_HEAD(&adev_head
, adev
, next
);
1221 fprintf(stderr
, "pcidevice argument parse error; "
1222 "please check the help text for usage\n");
1227 void add_assigned_devices(PCIBus
*bus
, const char **devices
, int n_devices
)
1231 for (i
= 0; i
< n_devices
; i
++) {
1232 struct AssignedDevInfo
*adev
;
1234 adev
= add_assigned_device(devices
[i
]);
1236 fprintf(stderr
, "Could not add assigned device %s\n", devices
[i
]);
1240 if (!init_assigned_device(adev
, bus
)) {
1241 fprintf(stderr
, "Failed to initialize assigned device %s\n",
1248 /* Option ROM header */
1249 struct option_rom_header
{
1250 uint8_t signature
[2];
1252 uint32_t entry_point
;
1253 uint8_t reserved
[17];
1254 uint16_t pci_header_offset
;
1255 uint16_t expansion_header_offset
;
1256 } __attribute__ ((packed
));
1258 /* Option ROM PCI data structure */
1259 struct option_rom_pci_header
{
1260 uint8_t signature
[4];
1263 uint16_t vital_product_data_offset
;
1264 uint16_t structure_length
;
1265 uint8_t structure_revision
;
1266 uint8_t class_code
[3];
1267 uint16_t image_length
;
1268 uint16_t image_revision
;
1272 } __attribute__ ((packed
));
1275 * Scan the list of Option ROMs at roms. If a suitable Option ROM is found,
1276 * allocate a ram space and copy it there. Then return its size aligned to
1277 * both 2KB and target page size.
1279 #define OPTION_ROM_ALIGN(x) (((x) + 2047) & ~2047)
1280 static int scan_option_rom(uint8_t devfn
, void *roms
, ram_addr_t offset
)
1282 int i
, size
, total_size
;
1285 struct option_rom_header
*rom
;
1286 struct option_rom_pci_header
*pcih
;
1291 /* Invalid signature means we're out of option ROMs. */
1292 if (strncmp((char *)rom
->signature
, "\x55\xaa", 2) ||
1293 (rom
->rom_size
== 0))
1296 size
= rom
->rom_size
* 512;
1297 /* Invalid checksum means we're out of option ROMs. */
1299 for (i
= 0; i
< size
; i
++)
1300 csum
+= ((uint8_t *)rom
)[i
];
1304 /* Check the PCI header (if any) for a match. */
1305 pcih
= (struct option_rom_pci_header
*)
1306 ((char *)rom
+ rom
->pci_header_offset
);
1307 if ((rom
->pci_header_offset
!= 0) &&
1308 !strncmp((char *)pcih
->signature
, "PCIR", 4))
1311 rom
= (struct option_rom_header
*)((char *)rom
+ size
);
1317 /* The size should be both 2K-aligned and page-aligned */
1318 total_size
= (TARGET_PAGE_SIZE
< 2048)
1319 ? OPTION_ROM_ALIGN(size
+ 1)
1320 : TARGET_PAGE_ALIGN(size
+ 1);
1322 /* Size of all available ram space is 0x10000 (0xd0000 to 0xe0000) */
1323 if ((offset
+ total_size
) > 0x10000u
) {
1324 fprintf(stderr
, "Option ROM size %x exceeds available space\n", size
);
1328 addr
= qemu_ram_alloc(total_size
);
1329 cpu_register_physical_memory(0xd0000 + offset
, total_size
, addr
| IO_MEM_ROM
);
1331 /* Write ROM data and devfn to phys_addr */
1332 cpu_physical_memory_write_rom(0xd0000 + offset
, (uint8_t *)rom
, size
);
1333 cpu_physical_memory_write_rom(0xd0000 + offset
+ size
, &devfn
, 1);
1339 * Scan the assigned devices for the devices that have an option ROM, and then
1340 * load the corresponding ROM data to RAM. If an error occurs while loading an
1341 * option ROM, we just ignore that option ROM and continue with the next one.
1343 ram_addr_t
assigned_dev_load_option_roms(ram_addr_t rom_base_offset
)
1345 ram_addr_t offset
= rom_base_offset
;
1346 AssignedDevInfo
*adev
;
1348 LIST_FOREACH(adev
, &adev_head
, next
) {
1355 snprintf(rom_file
, sizeof(rom_file
),
1356 "/sys/bus/pci/devices/0000:%02x:%02x.%01x/rom",
1357 adev
->bus
, adev
->dev
, adev
->func
);
1359 if (access(rom_file
, F_OK
))
1362 /* Write something to the ROM file to enable it */
1363 fp
= fopen(rom_file
, "wb");
1366 len
= fwrite(&i
, 1, 1, fp
);
1371 /* The file has to be closed and reopened, otherwise it won't work */
1372 fp
= fopen(rom_file
, "rb");
1376 fseek(fp
, 0, SEEK_END
);
1378 fseek(fp
, 0, SEEK_SET
);
1386 fread(buf
, size
, 1, fp
);
1387 if (!feof(fp
) || ferror(fp
)) {
1393 /* Scan the buffer for suitable ROMs and increase the offset */
1394 offset
+= scan_option_rom(adev
->assigned_dev
->dev
.devfn
, buf
, offset
);