Fix virtio_blk_init() calls
[qemu-kvm/fedora.git] / hw / device-assignment.c
blobd5eb7b214c43a14ef0cc58c7beb2694d0b81ade7
1 /*
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
11 * more details.
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)
28 #include <stdio.h>
29 #include <sys/io.h>
30 #include "qemu-kvm.h"
31 #include "hw.h"
32 #include "pc.h"
33 #include "sysemu.h"
34 #include "console.h"
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, ...) \
48 do { \
49 fprintf(stderr, "%s: " fmt, __func__ , __VA_ARGS__); \
50 } while (0)
51 #else
52 #define DEBUG(fmt, ...) do { } while(0)
53 #endif
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,
61 uint32_t value)
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);
70 outb(value, r_pio);
73 static void assigned_dev_ioport_writew(void *opaque, uint32_t addr,
74 uint32_t value)
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);
83 outw(value, r_pio);
86 static void assigned_dev_ioport_writel(void *opaque, uint32_t addr,
87 uint32_t value)
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);
96 outl(value, r_pio);
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);
103 uint32_t value;
105 value = inb(r_pio);
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);
111 return 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);
118 uint32_t value;
120 value = inw(r_pio);
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);
126 return 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);
133 uint32_t value;
135 value = inl(r_pio);
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);
141 return 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);
152 int ret = 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;
160 if (!first_map) {
161 int slot = get_slot(old_ephys);
162 if (slot != -1)
163 kvm_destroy_phys_mem(kvm_context, old_ephys,
164 TARGET_PAGE_ALIGN(old_esize));
167 if (e_size > 0)
168 ret = kvm_register_phys_mem(kvm_context, e_phys,
169 region->u.r_virtbase,
170 TARGET_PAGE_ALIGN(e_size), 0);
172 if (ret != 0) {
173 fprintf(stderr, "%s: Error: create new mapping failed\n", __func__);
174 exit(1);
178 static void assigned_dev_ioport_map(PCIDevice *pci_dev, int region_num,
179 uint32_t addr, uint32_t size, int type)
181 AssignedDevice *r_dev = (AssignedDevice *) pci_dev;
182 AssignedDevRegion *region = &r_dev->v_addrs[region_num];
183 int first_map = (region->e_size == 0);
184 CPUState *env;
186 region->e_physbase = addr;
187 region->e_size = size;
189 DEBUG("e_phys=0x%x r_baseport=%x type=0x%x len=%d region_num=%d \n",
190 addr, region->u.r_baseport, type, size, region_num);
192 if (first_map) {
193 struct ioperm_data *data;
195 data = qemu_mallocz(sizeof(struct ioperm_data));
196 if (data == NULL) {
197 fprintf(stderr, "%s: Out of memory\n", __func__);
198 exit(1);
201 data->start_port = region->u.r_baseport;
202 data->num = region->r_size;
203 data->turn_on = 1;
205 kvm_add_ioperm_data(data);
207 for (env = first_cpu; env; env = env->next_cpu)
208 kvm_ioperm(env, data);
211 register_ioport_read(addr, size, 1, assigned_dev_ioport_readb,
212 (r_dev->v_addrs + region_num));
213 register_ioport_read(addr, size, 2, assigned_dev_ioport_readw,
214 (r_dev->v_addrs + region_num));
215 register_ioport_read(addr, size, 4, assigned_dev_ioport_readl,
216 (r_dev->v_addrs + region_num));
217 register_ioport_write(addr, size, 1, assigned_dev_ioport_writeb,
218 (r_dev->v_addrs + region_num));
219 register_ioport_write(addr, size, 2, assigned_dev_ioport_writew,
220 (r_dev->v_addrs + region_num));
221 register_ioport_write(addr, size, 4, assigned_dev_ioport_writel,
222 (r_dev->v_addrs + region_num));
225 static void assigned_dev_pci_write_config(PCIDevice *d, uint32_t address,
226 uint32_t val, int len)
228 int fd;
229 ssize_t ret;
231 DEBUG("(%x.%x): address=%04x val=0x%08x len=%d\n",
232 ((d->devfn >> 3) & 0x1F), (d->devfn & 0x7),
233 (uint16_t) address, val, len);
235 if (address == 0x4) {
236 pci_default_write_config(d, address, val, len);
237 /* Continue to program the card */
240 if ((address >= 0x10 && address <= 0x24) || address == 0x34 ||
241 address == 0x3c || address == 0x3d) {
242 /* used for update-mappings (BAR emulation) */
243 pci_default_write_config(d, address, val, len);
244 return;
247 DEBUG("NON BAR (%x.%x): address=%04x val=0x%08x len=%d\n",
248 ((d->devfn >> 3) & 0x1F), (d->devfn & 0x7),
249 (uint16_t) address, val, len);
251 fd = ((AssignedDevice *)d)->real_device.config_fd;
253 again:
254 ret = pwrite(fd, &val, len, address);
255 if (ret != len) {
256 if ((ret < 0) && (errno == EINTR || errno == EAGAIN))
257 goto again;
259 fprintf(stderr, "%s: pwrite failed, ret = %zd errno = %d\n",
260 __func__, ret, errno);
262 exit(1);
266 static uint32_t assigned_dev_pci_read_config(PCIDevice *d, uint32_t address,
267 int len)
269 uint32_t val = 0;
270 int fd;
271 ssize_t ret;
273 if ((address >= 0x10 && address <= 0x24) || address == 0x34 ||
274 address == 0x3c || address == 0x3d) {
275 val = pci_default_read_config(d, address, len);
276 DEBUG("(%x.%x): address=%04x val=0x%08x len=%d\n",
277 (d->devfn >> 3) & 0x1F, (d->devfn & 0x7), address, val, len);
278 return val;
281 /* vga specific, remove later */
282 if (address == 0xFC)
283 goto do_log;
285 fd = ((AssignedDevice *)d)->real_device.config_fd;
287 again:
288 ret = pread(fd, &val, len, address);
289 if (ret != len) {
290 if ((ret < 0) && (errno == EINTR || errno == EAGAIN))
291 goto again;
293 fprintf(stderr, "%s: pread failed, ret = %zd errno = %d\n",
294 __func__, ret, errno);
296 exit(1);
299 do_log:
300 DEBUG("(%x.%x): address=%04x val=0x%08x len=%d\n",
301 (d->devfn >> 3) & 0x1F, (d->devfn & 0x7), address, val, len);
303 /* kill the special capabilities */
304 if (address == 4 && len == 4)
305 val &= ~0x100000;
306 else if (address == 6)
307 val &= ~0x10;
309 return val;
312 static int assigned_dev_register_regions(PCIRegion *io_regions,
313 unsigned long regions_num,
314 AssignedDevice *pci_dev)
316 uint32_t i;
317 PCIRegion *cur_region = io_regions;
319 for (i = 0; i < regions_num; i++, cur_region++) {
320 if (!cur_region->valid)
321 continue;
322 pci_dev->v_addrs[i].num = i;
324 /* handle memory io regions */
325 if (cur_region->type & IORESOURCE_MEM) {
326 int t = cur_region->type & IORESOURCE_PREFETCH
327 ? PCI_ADDRESS_SPACE_MEM_PREFETCH
328 : PCI_ADDRESS_SPACE_MEM;
330 /* map physical memory */
331 pci_dev->v_addrs[i].e_physbase = cur_region->base_addr;
332 pci_dev->v_addrs[i].u.r_virtbase =
333 mmap(NULL,
334 (cur_region->size + 0xFFF) & 0xFFFFF000,
335 PROT_WRITE | PROT_READ, MAP_SHARED,
336 cur_region->resource_fd, (off_t) 0);
338 if (pci_dev->v_addrs[i].u.r_virtbase == MAP_FAILED) {
339 pci_dev->v_addrs[i].u.r_virtbase = NULL;
340 fprintf(stderr, "%s: Error: Couldn't mmap 0x%x!"
341 "\n", __func__,
342 (uint32_t) (cur_region->base_addr));
343 return -1;
345 pci_dev->v_addrs[i].r_size = cur_region->size;
346 pci_dev->v_addrs[i].e_size = 0;
348 /* add offset */
349 pci_dev->v_addrs[i].u.r_virtbase +=
350 (cur_region->base_addr & 0xFFF);
352 pci_register_io_region((PCIDevice *) pci_dev, i,
353 cur_region->size, t,
354 assigned_dev_iomem_map);
355 continue;
357 /* handle port io regions */
358 pci_dev->v_addrs[i].e_physbase = cur_region->base_addr;
359 pci_dev->v_addrs[i].u.r_baseport = cur_region->base_addr;
360 pci_dev->v_addrs[i].r_size = cur_region->size;
361 pci_dev->v_addrs[i].e_size = 0;
363 pci_register_io_region((PCIDevice *) pci_dev, i,
364 cur_region->size, PCI_ADDRESS_SPACE_IO,
365 assigned_dev_ioport_map);
367 /* not relevant for port io */
368 pci_dev->v_addrs[i].memory_index = 0;
371 /* success */
372 return 0;
375 static int get_real_device(AssignedDevice *pci_dev, uint8_t r_bus,
376 uint8_t r_dev, uint8_t r_func)
378 char dir[128], name[128];
379 int fd, r = 0;
380 FILE *f;
381 unsigned long long start, end, size, flags;
382 PCIRegion *rp;
383 PCIDevRegions *dev = &pci_dev->real_device;
385 dev->region_number = 0;
387 snprintf(dir, sizeof(dir), "/sys/bus/pci/devices/0000:%02x:%02x.%x/",
388 r_bus, r_dev, r_func);
390 snprintf(name, sizeof(name), "%sconfig", dir);
392 fd = open(name, O_RDWR);
393 if (fd == -1) {
394 fprintf(stderr, "%s: %s: %m\n", __func__, name);
395 return 1;
397 dev->config_fd = fd;
398 again:
399 r = read(fd, pci_dev->dev.config, sizeof(pci_dev->dev.config));
400 if (r < 0) {
401 if (errno == EINTR || errno == EAGAIN)
402 goto again;
403 fprintf(stderr, "%s: read failed, errno = %d\n", __func__, errno);
406 snprintf(name, sizeof(name), "%sresource", dir);
408 f = fopen(name, "r");
409 if (f == NULL) {
410 fprintf(stderr, "%s: %s: %m\n", __func__, name);
411 return 1;
414 for (r = 0; r < MAX_IO_REGIONS; r++) {
415 if (fscanf(f, "%lli %lli %lli\n", &start, &end, &flags) != 3)
416 break;
418 rp = dev->regions + r;
419 rp->valid = 0;
420 size = end - start + 1;
421 flags &= IORESOURCE_IO | IORESOURCE_MEM | IORESOURCE_PREFETCH;
422 if (size == 0 || (flags & ~IORESOURCE_PREFETCH) == 0)
423 continue;
424 if (flags & IORESOURCE_MEM) {
425 flags &= ~IORESOURCE_IO;
426 snprintf(name, sizeof(name), "%sresource%d", dir, r);
427 fd = open(name, O_RDWR);
428 if (fd == -1)
429 continue; /* probably ROM */
430 rp->resource_fd = fd;
431 } else
432 flags &= ~IORESOURCE_PREFETCH;
434 rp->type = flags;
435 rp->valid = 1;
436 rp->base_addr = start;
437 rp->size = size;
438 DEBUG("region %d size %d start 0x%llx type %d resource_fd %d\n",
439 r, rp->size, start, rp->type, rp->resource_fd);
441 fclose(f);
443 dev->region_number = r;
444 return 0;
447 static LIST_HEAD(, AssignedDevInfo) adev_head;
449 void free_assigned_device(AssignedDevInfo *adev)
451 AssignedDevice *dev = adev->assigned_dev;
453 if (dev) {
454 int i;
456 for (i = 0; i < dev->real_device.region_number; i++) {
457 PCIRegion *pci_region = &dev->real_device.regions[i];
458 AssignedDevRegion *region = &dev->v_addrs[i];
460 if (!pci_region->valid || !(pci_region->type & IORESOURCE_MEM))
461 continue;
463 if (region->u.r_virtbase) {
464 int ret = munmap(region->u.r_virtbase,
465 (pci_region->size + 0xFFF) & 0xFFFFF000);
466 if (ret != 0)
467 fprintf(stderr,
468 "Failed to unmap assigned device region: %s\n",
469 strerror(errno));
473 if (dev->real_device.config_fd) {
474 close(dev->real_device.config_fd);
475 dev->real_device.config_fd = 0;
478 pci_unregister_device(&dev->dev);
479 adev->assigned_dev = dev = NULL;
482 LIST_REMOVE(adev, next);
483 qemu_free(adev);
486 static uint32_t calc_assigned_dev_id(uint8_t bus, uint8_t devfn)
488 return (uint32_t)bus << 8 | (uint32_t)devfn;
491 /* The pci config space got updated. Check if irq numbers have changed
492 * for our devices
494 void assigned_dev_update_irq(PCIDevice *d)
496 AssignedDevInfo *adev;
498 adev = LIST_FIRST(&adev_head);
499 while (adev) {
500 AssignedDevInfo *next = LIST_NEXT(adev, next);
501 AssignedDevice *assigned_dev = adev->assigned_dev;
502 int irq, r;
504 irq = pci_map_irq(&assigned_dev->dev, assigned_dev->intpin);
505 irq = piix_get_irq(irq);
507 #ifdef TARGET_IA64
508 irq = ipf_map_irq(d, irq);
509 #endif
511 if (irq != assigned_dev->girq) {
512 struct kvm_assigned_irq assigned_irq_data;
514 memset(&assigned_irq_data, 0, sizeof(assigned_irq_data));
515 assigned_irq_data.assigned_dev_id =
516 calc_assigned_dev_id(assigned_dev->h_busnr,
517 (uint8_t) assigned_dev->h_devfn);
518 assigned_irq_data.guest_irq = irq;
519 assigned_irq_data.host_irq = assigned_dev->real_device.irq;
520 r = kvm_assign_irq(kvm_context, &assigned_irq_data);
521 if (r < 0) {
522 fprintf(stderr, "Failed to assign irq for \"%s\": %s\n",
523 adev->name, strerror(-r));
524 fprintf(stderr, "Perhaps you are assigning a device "
525 "that shares an IRQ with another device?\n");
526 LIST_REMOVE(adev, next);
527 free_assigned_device(adev);
528 adev = next;
529 continue;
531 assigned_dev->girq = irq;
534 adev = next;
538 struct PCIDevice *init_assigned_device(AssignedDevInfo *adev, PCIBus *bus)
540 int r;
541 AssignedDevice *dev;
542 uint8_t e_device, e_intx;
543 struct kvm_assigned_pci_dev assigned_dev_data;
545 DEBUG("Registering real physical device %s (bus=%x dev=%x func=%x)\n",
546 adev->name, adev->bus, adev->dev, adev->func);
548 dev = (AssignedDevice *)
549 pci_register_device(bus, adev->name, sizeof(AssignedDevice),
550 -1, assigned_dev_pci_read_config,
551 assigned_dev_pci_write_config);
552 if (NULL == dev) {
553 fprintf(stderr, "%s: Error: Couldn't register real device %s\n",
554 __func__, adev->name);
555 return NULL;
558 adev->assigned_dev = dev;
560 if (get_real_device(dev, adev->bus, adev->dev, adev->func)) {
561 fprintf(stderr, "%s: Error: Couldn't get real device (%s)!\n",
562 __func__, adev->name);
563 return NULL;
566 /* handle real device's MMIO/PIO BARs */
567 if (assigned_dev_register_regions(dev->real_device.regions,
568 dev->real_device.region_number,
569 dev))
570 return NULL;
572 /* handle interrupt routing */
573 e_device = (dev->dev.devfn >> 3) & 0x1f;
574 e_intx = dev->dev.config[0x3d] - 1;
575 dev->intpin = e_intx;
576 dev->run = 0;
577 dev->girq = 0;
578 dev->h_busnr = adev->bus;
579 dev->h_devfn = PCI_DEVFN(adev->dev, adev->func);
581 memset(&assigned_dev_data, 0, sizeof(assigned_dev_data));
582 assigned_dev_data.assigned_dev_id =
583 calc_assigned_dev_id(dev->h_busnr, (uint32_t)dev->h_devfn);
584 assigned_dev_data.busnr = dev->h_busnr;
585 assigned_dev_data.devfn = dev->h_devfn;
587 #ifdef KVM_CAP_IOMMU
588 /* We always enable the IOMMU if present
589 * (or when not disabled on the command line)
591 r = kvm_check_extension(kvm_context, KVM_CAP_IOMMU);
592 if (r && !adev->disable_iommu)
593 assigned_dev_data.flags |= KVM_DEV_ASSIGN_ENABLE_IOMMU;
594 #endif
596 r = kvm_assign_pci_device(kvm_context, &assigned_dev_data);
597 if (r < 0) {
598 fprintf(stderr, "Failed to assign device \"%s\" : %s\n",
599 adev->name, strerror(-r));
600 return NULL;
603 return &dev->dev;
607 * Syntax to assign device:
609 * -pcidevice host=bus:dev.func[,dma=none][,name=Foo]
611 * Example:
612 * -pcidevice host=00:13.0,dma=pvdma
614 * dma can currently only be 'none' to disable iommu support.
616 AssignedDevInfo *add_assigned_device(const char *arg)
618 char *cp, *cp1;
619 char device[8];
620 char dma[6];
621 int r;
622 AssignedDevInfo *adev;
624 adev = qemu_mallocz(sizeof(AssignedDevInfo));
625 if (adev == NULL) {
626 fprintf(stderr, "%s: Out of memory\n", __func__);
627 return NULL;
629 r = get_param_value(device, sizeof(device), "host", arg);
630 r = get_param_value(adev->name, sizeof(adev->name), "name", arg);
631 if (!r)
632 snprintf(adev->name, sizeof(adev->name), "%s", device);
634 #ifdef KVM_CAP_IOMMU
635 r = get_param_value(dma, sizeof(dma), "dma", arg);
636 if (r && !strncmp(dma, "none", 4))
637 adev->disable_iommu = 1;
638 #endif
639 cp = device;
640 adev->bus = strtoul(cp, &cp1, 16);
641 if (*cp1 != ':')
642 goto bad;
643 cp = cp1 + 1;
645 adev->dev = strtoul(cp, &cp1, 16);
646 if (*cp1 != '.')
647 goto bad;
648 cp = cp1 + 1;
650 adev->func = strtoul(cp, &cp1, 16);
652 LIST_INSERT_HEAD(&adev_head, adev, next);
653 return adev;
654 bad:
655 fprintf(stderr, "pcidevice argument parse error; "
656 "please check the help text for usage\n");
657 qemu_free(adev);
658 return NULL;
661 void add_assigned_devices(PCIBus *bus, const char **devices, int n_devices)
663 int i;
665 for (i = 0; i < n_devices; i++) {
666 struct AssignedDevInfo *adev;
668 adev = add_assigned_device(devices[i]);
669 if (!adev) {
670 fprintf(stderr, "Could not add assigned device %s\n", devices[i]);
671 exit(1);
674 if (!init_assigned_device(adev, bus)) {
675 fprintf(stderr, "Failed to initialize assigned device %s\n",
676 devices[i]);
677 exit(1);
682 /* Option ROM header */
683 struct option_rom_header {
684 uint8_t signature[2];
685 uint8_t rom_size;
686 uint32_t entry_point;
687 uint8_t reserved[17];
688 uint16_t pci_header_offset;
689 uint16_t expansion_header_offset;
690 } __attribute__ ((packed));
692 /* Option ROM PCI data structure */
693 struct option_rom_pci_header {
694 uint8_t signature[4];
695 uint16_t vendor_id;
696 uint16_t device_id;
697 uint16_t vital_product_data_offset;
698 uint16_t structure_length;
699 uint8_t structure_revision;
700 uint8_t class_code[3];
701 uint16_t image_length;
702 uint16_t image_revision;
703 uint8_t code_type;
704 uint8_t indicator;
705 uint16_t reserved;
706 } __attribute__ ((packed));
709 * Scan the list of Option ROMs at roms. If a suitable Option ROM is found,
710 * allocate a ram space and copy it there. Then return its size aligned to
711 * both 2KB and target page size.
713 #define OPTION_ROM_ALIGN(x) (((x) + 2047) & ~2047)
714 static int scan_option_rom(uint8_t devfn, void *roms, ram_addr_t offset)
716 int i, size, total_size;
717 uint8_t csum;
718 ram_addr_t addr;
719 struct option_rom_header *rom;
720 struct option_rom_pci_header *pcih;
722 rom = roms;
724 for ( ; ; ) {
725 /* Invalid signature means we're out of option ROMs. */
726 if (strncmp((char *)rom->signature, "\x55\xaa", 2) ||
727 (rom->rom_size == 0))
728 break;
730 size = rom->rom_size * 512;
731 /* Invalid checksum means we're out of option ROMs. */
732 csum = 0;
733 for (i = 0; i < size; i++)
734 csum += ((uint8_t *)rom)[i];
735 if (csum != 0)
736 break;
738 /* Check the PCI header (if any) for a match. */
739 pcih = (struct option_rom_pci_header *)
740 ((char *)rom + rom->pci_header_offset);
741 if ((rom->pci_header_offset != 0) &&
742 !strncmp((char *)pcih->signature, "PCIR", 4))
743 goto found;
745 rom = (struct option_rom_header *)((char *)rom + size);
748 return 0;
750 found:
751 /* The size should be both 2K-aligned and page-aligned */
752 total_size = (TARGET_PAGE_SIZE < 2048)
753 ? OPTION_ROM_ALIGN(size + 1)
754 : TARGET_PAGE_ALIGN(size + 1);
756 /* Size of all available ram space is 0x10000 (0xd0000 to 0xe0000) */
757 if ((offset + total_size) > 0x10000u) {
758 fprintf(stderr, "Option ROM size %x exceeds available space\n", size);
759 return 0;
762 addr = qemu_ram_alloc(total_size);
763 cpu_register_physical_memory(0xd0000 + offset, total_size, addr | IO_MEM_ROM);
765 /* Write ROM data and devfn to phys_addr */
766 cpu_physical_memory_write_rom(0xd0000 + offset, rom, size);
767 cpu_physical_memory_write_rom(0xd0000 + offset + size, &devfn, 1);
769 return total_size;
773 * Scan the assigned devices for the devices that have an option ROM, and then
774 * load the corresponding ROM data to RAM. If an error occurs while loading an
775 * option ROM, we just ignore that option ROM and continue with the next one.
777 ram_addr_t assigned_dev_load_option_roms(ram_addr_t rom_base_offset)
779 ram_addr_t offset = rom_base_offset;
780 AssignedDevInfo *adev;
782 LIST_FOREACH(adev, &adev_head, next) {
783 int size, len;
784 void *buf;
785 FILE *fp;
786 uint8_t i = 1;
787 char rom_file[64];
789 snprintf(rom_file, sizeof(rom_file),
790 "/sys/bus/pci/devices/0000:%02x:%02x.%01x/rom",
791 adev->bus, adev->dev, adev->func);
793 if (access(rom_file, F_OK))
794 continue;
796 /* Write something to the ROM file to enable it */
797 fp = fopen(rom_file, "wb");
798 if (fp == NULL)
799 continue;
800 len = fwrite(&i, 1, 1, fp);
801 fclose(fp);
802 if (len != 1)
803 continue;
805 /* The file has to be closed and reopened, otherwise it won't work */
806 fp = fopen(rom_file, "rb");
807 if (fp == NULL)
808 continue;
810 fseek(fp, 0, SEEK_END);
811 size = ftell(fp);
812 fseek(fp, 0, SEEK_SET);
814 buf = malloc(size);
815 if (buf == NULL) {
816 fclose(fp);
817 continue;
820 fread(buf, size, 1, fp);
821 if (!feof(fp) || ferror(fp)) {
822 free(buf);
823 fclose(fp);
824 continue;
827 /* Scan the buffer for suitable ROMs and increase the offset */
828 offset += scan_option_rom(adev->assigned_dev->dev.devfn, buf, offset);
830 free(buf);
831 fclose(fp);
834 return offset;