Kill redundant declarion of perror()
[qemu-kvm/fedora.git] / hw / device-assignment.c
blobf8faf5b714d579899daedb3fb7eb1a26b20c4ee3
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 kvm_destroy_phys_mem(kvm_context, old_ephys,
162 TARGET_PAGE_ALIGN(old_esize));
164 if (e_size > 0)
165 ret = kvm_register_phys_mem(kvm_context, e_phys,
166 region->u.r_virtbase,
167 TARGET_PAGE_ALIGN(e_size), 0);
169 if (ret != 0) {
170 fprintf(stderr, "%s: Error: create new mapping failed\n", __func__);
171 exit(1);
175 static void assigned_dev_ioport_map(PCIDevice *pci_dev, int region_num,
176 uint32_t addr, uint32_t size, int type)
178 AssignedDevice *r_dev = (AssignedDevice *) pci_dev;
179 AssignedDevRegion *region = &r_dev->v_addrs[region_num];
180 int first_map = (region->e_size == 0);
181 CPUState *env;
183 region->e_physbase = addr;
184 region->e_size = size;
186 DEBUG("e_phys=0x%x r_baseport=%x type=0x%x len=%d region_num=%d \n",
187 addr, region->u.r_baseport, type, size, region_num);
189 if (first_map) {
190 struct ioperm_data *data;
192 data = qemu_mallocz(sizeof(struct ioperm_data));
193 if (data == NULL) {
194 fprintf(stderr, "%s: Out of memory\n", __func__);
195 exit(1);
198 data->start_port = region->u.r_baseport;
199 data->num = region->r_size;
200 data->turn_on = 1;
202 kvm_add_ioperm_data(data);
204 for (env = first_cpu; env; env = env->next_cpu)
205 kvm_ioperm(env, data);
208 register_ioport_read(addr, size, 1, assigned_dev_ioport_readb,
209 (r_dev->v_addrs + region_num));
210 register_ioport_read(addr, size, 2, assigned_dev_ioport_readw,
211 (r_dev->v_addrs + region_num));
212 register_ioport_read(addr, size, 4, assigned_dev_ioport_readl,
213 (r_dev->v_addrs + region_num));
214 register_ioport_write(addr, size, 1, assigned_dev_ioport_writeb,
215 (r_dev->v_addrs + region_num));
216 register_ioport_write(addr, size, 2, assigned_dev_ioport_writew,
217 (r_dev->v_addrs + region_num));
218 register_ioport_write(addr, size, 4, assigned_dev_ioport_writel,
219 (r_dev->v_addrs + region_num));
222 static void assigned_dev_pci_write_config(PCIDevice *d, uint32_t address,
223 uint32_t val, int len)
225 int fd;
226 ssize_t ret;
228 DEBUG("(%x.%x): address=%04x val=0x%08x len=%d\n",
229 ((d->devfn >> 3) & 0x1F), (d->devfn & 0x7),
230 (uint16_t) address, val, len);
232 if (address == 0x4) {
233 pci_default_write_config(d, address, val, len);
234 /* Continue to program the card */
237 if ((address >= 0x10 && address <= 0x24) || address == 0x34 ||
238 address == 0x3c || address == 0x3d) {
239 /* used for update-mappings (BAR emulation) */
240 pci_default_write_config(d, address, val, len);
241 return;
244 DEBUG("NON BAR (%x.%x): address=%04x val=0x%08x len=%d\n",
245 ((d->devfn >> 3) & 0x1F), (d->devfn & 0x7),
246 (uint16_t) address, val, len);
248 fd = ((AssignedDevice *)d)->real_device.config_fd;
250 again:
251 ret = pwrite(fd, &val, len, address);
252 if (ret != len) {
253 if ((ret < 0) && (errno == EINTR || errno == EAGAIN))
254 goto again;
256 fprintf(stderr, "%s: pwrite failed, ret = %zd errno = %d\n",
257 __func__, ret, errno);
259 exit(1);
263 static uint32_t assigned_dev_pci_read_config(PCIDevice *d, uint32_t address,
264 int len)
266 uint32_t val = 0;
267 int fd;
268 ssize_t ret;
270 if ((address >= 0x10 && address <= 0x24) || address == 0x34 ||
271 address == 0x3c || address == 0x3d) {
272 val = pci_default_read_config(d, address, len);
273 DEBUG("(%x.%x): address=%04x val=0x%08x len=%d\n",
274 (d->devfn >> 3) & 0x1F, (d->devfn & 0x7), address, val, len);
275 return val;
278 /* vga specific, remove later */
279 if (address == 0xFC)
280 goto do_log;
282 fd = ((AssignedDevice *)d)->real_device.config_fd;
284 again:
285 ret = pread(fd, &val, len, address);
286 if (ret != len) {
287 if ((ret < 0) && (errno == EINTR || errno == EAGAIN))
288 goto again;
290 fprintf(stderr, "%s: pread failed, ret = %zd errno = %d\n",
291 __func__, ret, errno);
293 exit(1);
296 do_log:
297 DEBUG("(%x.%x): address=%04x val=0x%08x len=%d\n",
298 (d->devfn >> 3) & 0x1F, (d->devfn & 0x7), address, val, len);
300 /* kill the special capabilities */
301 if (address == 4 && len == 4)
302 val &= ~0x100000;
303 else if (address == 6)
304 val &= ~0x10;
306 return val;
309 static int assigned_dev_register_regions(PCIRegion *io_regions,
310 unsigned long regions_num,
311 AssignedDevice *pci_dev)
313 uint32_t i;
314 PCIRegion *cur_region = io_regions;
316 for (i = 0; i < regions_num; i++, cur_region++) {
317 if (!cur_region->valid)
318 continue;
319 pci_dev->v_addrs[i].num = i;
321 /* handle memory io regions */
322 if (cur_region->type & IORESOURCE_MEM) {
323 int t = cur_region->type & IORESOURCE_PREFETCH
324 ? PCI_ADDRESS_SPACE_MEM_PREFETCH
325 : PCI_ADDRESS_SPACE_MEM;
327 /* map physical memory */
328 pci_dev->v_addrs[i].e_physbase = cur_region->base_addr;
329 pci_dev->v_addrs[i].u.r_virtbase =
330 mmap(NULL,
331 (cur_region->size + 0xFFF) & 0xFFFFF000,
332 PROT_WRITE | PROT_READ, MAP_SHARED,
333 cur_region->resource_fd, (off_t) 0);
335 if (pci_dev->v_addrs[i].u.r_virtbase == MAP_FAILED) {
336 pci_dev->v_addrs[i].u.r_virtbase = NULL;
337 fprintf(stderr, "%s: Error: Couldn't mmap 0x%x!"
338 "\n", __func__,
339 (uint32_t) (cur_region->base_addr));
340 return -1;
342 pci_dev->v_addrs[i].r_size = cur_region->size;
343 pci_dev->v_addrs[i].e_size = 0;
345 /* add offset */
346 pci_dev->v_addrs[i].u.r_virtbase +=
347 (cur_region->base_addr & 0xFFF);
349 pci_register_io_region((PCIDevice *) pci_dev, i,
350 cur_region->size, t,
351 assigned_dev_iomem_map);
352 continue;
354 /* handle port io regions */
355 pci_dev->v_addrs[i].e_physbase = cur_region->base_addr;
356 pci_dev->v_addrs[i].u.r_baseport = cur_region->base_addr;
357 pci_dev->v_addrs[i].r_size = cur_region->size;
358 pci_dev->v_addrs[i].e_size = 0;
360 pci_register_io_region((PCIDevice *) pci_dev, i,
361 cur_region->size, PCI_ADDRESS_SPACE_IO,
362 assigned_dev_ioport_map);
364 /* not relevant for port io */
365 pci_dev->v_addrs[i].memory_index = 0;
368 /* success */
369 return 0;
372 static int get_real_device(AssignedDevice *pci_dev, uint8_t r_bus,
373 uint8_t r_dev, uint8_t r_func)
375 char dir[128], name[128];
376 int fd, r = 0;
377 FILE *f;
378 unsigned long long start, end, size, flags;
379 PCIRegion *rp;
380 PCIDevRegions *dev = &pci_dev->real_device;
382 dev->region_number = 0;
384 snprintf(dir, sizeof(dir), "/sys/bus/pci/devices/0000:%02x:%02x.%x/",
385 r_bus, r_dev, r_func);
387 snprintf(name, sizeof(name), "%sconfig", dir);
389 fd = open(name, O_RDWR);
390 if (fd == -1) {
391 fprintf(stderr, "%s: %s: %m\n", __func__, name);
392 return 1;
394 dev->config_fd = fd;
395 again:
396 r = read(fd, pci_dev->dev.config, sizeof(pci_dev->dev.config));
397 if (r < 0) {
398 if (errno == EINTR || errno == EAGAIN)
399 goto again;
400 fprintf(stderr, "%s: read failed, errno = %d\n", __func__, errno);
403 snprintf(name, sizeof(name), "%sresource", dir);
405 f = fopen(name, "r");
406 if (f == NULL) {
407 fprintf(stderr, "%s: %s: %m\n", __func__, name);
408 return 1;
411 for (r = 0; r < MAX_IO_REGIONS; r++) {
412 if (fscanf(f, "%lli %lli %lli\n", &start, &end, &flags) != 3)
413 break;
415 rp = dev->regions + r;
416 rp->valid = 0;
417 size = end - start + 1;
418 flags &= IORESOURCE_IO | IORESOURCE_MEM | IORESOURCE_PREFETCH;
419 if (size == 0 || (flags & ~IORESOURCE_PREFETCH) == 0)
420 continue;
421 if (flags & IORESOURCE_MEM) {
422 flags &= ~IORESOURCE_IO;
423 snprintf(name, sizeof(name), "%sresource%d", dir, r);
424 fd = open(name, O_RDWR);
425 if (fd == -1)
426 continue; /* probably ROM */
427 rp->resource_fd = fd;
428 } else
429 flags &= ~IORESOURCE_PREFETCH;
431 rp->type = flags;
432 rp->valid = 1;
433 rp->base_addr = start;
434 rp->size = size;
435 DEBUG("region %d size %d start 0x%llx type %d resource_fd %d\n",
436 r, rp->size, start, rp->type, rp->resource_fd);
438 fclose(f);
440 dev->region_number = r;
441 return 0;
444 static LIST_HEAD(, AssignedDevInfo) adev_head;
446 void free_assigned_device(AssignedDevInfo *adev)
448 AssignedDevice *dev = adev->assigned_dev;
450 if (dev) {
451 int i;
453 for (i = 0; i < dev->real_device.region_number; i++) {
454 PCIRegion *pci_region = &dev->real_device.regions[i];
455 AssignedDevRegion *region = &dev->v_addrs[i];
457 if (!pci_region->valid || !(pci_region->type & IORESOURCE_MEM))
458 continue;
460 if (region->u.r_virtbase) {
461 int ret = munmap(region->u.r_virtbase,
462 (pci_region->size + 0xFFF) & 0xFFFFF000);
463 if (ret != 0)
464 fprintf(stderr,
465 "Failed to unmap assigned device region: %s\n",
466 strerror(errno));
470 if (dev->real_device.config_fd) {
471 close(dev->real_device.config_fd);
472 dev->real_device.config_fd = 0;
475 pci_unregister_device(&dev->dev);
476 adev->assigned_dev = dev = NULL;
479 LIST_REMOVE(adev, next);
480 qemu_free(adev);
483 static uint32_t calc_assigned_dev_id(uint8_t bus, uint8_t devfn)
485 return (uint32_t)bus << 8 | (uint32_t)devfn;
488 /* The pci config space got updated. Check if irq numbers have changed
489 * for our devices
491 void assigned_dev_update_irq(PCIDevice *d)
493 AssignedDevInfo *adev;
495 adev = LIST_FIRST(&adev_head);
496 while (adev) {
497 AssignedDevInfo *next = LIST_NEXT(adev, next);
498 AssignedDevice *assigned_dev = adev->assigned_dev;
499 int irq, r;
501 irq = pci_map_irq(&assigned_dev->dev, assigned_dev->intpin);
502 irq = piix_get_irq(irq);
504 #ifdef TARGET_IA64
505 irq = ipf_map_irq(d, irq);
506 #endif
508 if (irq != assigned_dev->girq) {
509 struct kvm_assigned_irq assigned_irq_data;
511 memset(&assigned_irq_data, 0, sizeof(assigned_irq_data));
512 assigned_irq_data.assigned_dev_id =
513 calc_assigned_dev_id(assigned_dev->h_busnr,
514 (uint8_t) assigned_dev->h_devfn);
515 assigned_irq_data.guest_irq = irq;
516 assigned_irq_data.host_irq = assigned_dev->real_device.irq;
517 r = kvm_assign_irq(kvm_context, &assigned_irq_data);
518 if (r < 0) {
519 fprintf(stderr, "Failed to assign irq for \"%s\": %s\n",
520 adev->name, strerror(-r));
521 fprintf(stderr, "Perhaps you are assigning a device "
522 "that shares an IRQ with another device?\n");
523 LIST_REMOVE(adev, next);
524 free_assigned_device(adev);
525 adev = next;
526 continue;
528 assigned_dev->girq = irq;
531 adev = next;
535 struct PCIDevice *init_assigned_device(AssignedDevInfo *adev, PCIBus *bus)
537 int r;
538 AssignedDevice *dev;
539 uint8_t e_device, e_intx;
540 struct kvm_assigned_pci_dev assigned_dev_data;
542 DEBUG("Registering real physical device %s (bus=%x dev=%x func=%x)\n",
543 adev->name, adev->bus, adev->dev, adev->func);
545 dev = (AssignedDevice *)
546 pci_register_device(bus, adev->name, sizeof(AssignedDevice),
547 -1, assigned_dev_pci_read_config,
548 assigned_dev_pci_write_config);
549 if (NULL == dev) {
550 fprintf(stderr, "%s: Error: Couldn't register real device %s\n",
551 __func__, adev->name);
552 return NULL;
555 adev->assigned_dev = dev;
557 if (get_real_device(dev, adev->bus, adev->dev, adev->func)) {
558 fprintf(stderr, "%s: Error: Couldn't get real device (%s)!\n",
559 __func__, adev->name);
560 return NULL;
563 /* handle real device's MMIO/PIO BARs */
564 if (assigned_dev_register_regions(dev->real_device.regions,
565 dev->real_device.region_number,
566 dev))
567 return NULL;
569 /* handle interrupt routing */
570 e_device = (dev->dev.devfn >> 3) & 0x1f;
571 e_intx = dev->dev.config[0x3d] - 1;
572 dev->intpin = e_intx;
573 dev->run = 0;
574 dev->girq = 0;
575 dev->h_busnr = adev->bus;
576 dev->h_devfn = PCI_DEVFN(adev->dev, adev->func);
578 memset(&assigned_dev_data, 0, sizeof(assigned_dev_data));
579 assigned_dev_data.assigned_dev_id =
580 calc_assigned_dev_id(dev->h_busnr, (uint32_t)dev->h_devfn);
581 assigned_dev_data.busnr = dev->h_busnr;
582 assigned_dev_data.devfn = dev->h_devfn;
584 #ifdef KVM_CAP_IOMMU
585 /* We always enable the IOMMU if present
586 * (or when not disabled on the command line)
588 r = kvm_check_extension(kvm_context, KVM_CAP_IOMMU);
589 if (r && !adev->disable_iommu)
590 assigned_dev_data.flags |= KVM_DEV_ASSIGN_ENABLE_IOMMU;
591 #endif
593 r = kvm_assign_pci_device(kvm_context, &assigned_dev_data);
594 if (r < 0) {
595 fprintf(stderr, "Failed to assign device \"%s\" : %s\n",
596 adev->name, strerror(-r));
597 return NULL;
600 return &dev->dev;
604 * Syntax to assign device:
606 * -pcidevice host=bus:dev.func[,dma=none][,name=Foo]
608 * Example:
609 * -pcidevice host=00:13.0,dma=pvdma
611 * dma can currently only be 'none' to disable iommu support.
613 AssignedDevInfo *add_assigned_device(const char *arg)
615 char *cp, *cp1;
616 char device[8];
617 char dma[6];
618 int r;
619 AssignedDevInfo *adev;
621 adev = qemu_mallocz(sizeof(AssignedDevInfo));
622 if (adev == NULL) {
623 fprintf(stderr, "%s: Out of memory\n", __func__);
624 return NULL;
626 r = get_param_value(device, sizeof(device), "host", arg);
627 r = get_param_value(adev->name, sizeof(adev->name), "name", arg);
628 if (!r)
629 snprintf(adev->name, sizeof(adev->name), "%s", device);
631 #ifdef KVM_CAP_IOMMU
632 r = get_param_value(dma, sizeof(dma), "dma", arg);
633 if (r && !strncmp(dma, "none", 4))
634 adev->disable_iommu = 1;
635 #endif
636 cp = device;
637 adev->bus = strtoul(cp, &cp1, 16);
638 if (*cp1 != ':')
639 goto bad;
640 cp = cp1 + 1;
642 adev->dev = strtoul(cp, &cp1, 16);
643 if (*cp1 != '.')
644 goto bad;
645 cp = cp1 + 1;
647 adev->func = strtoul(cp, &cp1, 16);
649 LIST_INSERT_HEAD(&adev_head, adev, next);
650 return adev;
651 bad:
652 fprintf(stderr, "pcidevice argument parse error; "
653 "please check the help text for usage\n");
654 qemu_free(adev);
655 return NULL;
658 void add_assigned_devices(PCIBus *bus, const char **devices, int n_devices)
660 int i;
662 for (i = 0; i < n_devices; i++) {
663 struct AssignedDevInfo *adev;
665 adev = add_assigned_device(devices[i]);
666 if (!adev) {
667 fprintf(stderr, "Could not add assigned device %s\n", devices[i]);
668 exit(1);
671 if (!init_assigned_device(adev, bus)) {
672 fprintf(stderr, "Failed to initialize assigned device %s\n",
673 devices[i]);
674 exit(1);
679 /* Option ROM header */
680 struct option_rom_header {
681 uint8_t signature[2];
682 uint8_t rom_size;
683 uint32_t entry_point;
684 uint8_t reserved[17];
685 uint16_t pci_header_offset;
686 uint16_t expansion_header_offset;
687 } __attribute__ ((packed));
689 /* Option ROM PCI data structure */
690 struct option_rom_pci_header {
691 uint8_t signature[4];
692 uint16_t vendor_id;
693 uint16_t device_id;
694 uint16_t vital_product_data_offset;
695 uint16_t structure_length;
696 uint8_t structure_revision;
697 uint8_t class_code[3];
698 uint16_t image_length;
699 uint16_t image_revision;
700 uint8_t code_type;
701 uint8_t indicator;
702 uint16_t reserved;
703 } __attribute__ ((packed));
706 * Scan the list of Option ROMs at roms. If a suitable Option ROM is found,
707 * allocate a ram space and copy it there. Then return its size aligned to
708 * both 2KB and target page size.
710 #define OPTION_ROM_ALIGN(x) (((x) + 2047) & ~2047)
711 static int scan_option_rom(uint8_t devfn, void *roms, ram_addr_t offset)
713 int i, size, total_size;
714 uint8_t csum;
715 ram_addr_t addr;
716 struct option_rom_header *rom;
717 struct option_rom_pci_header *pcih;
719 rom = roms;
721 for ( ; ; ) {
722 /* Invalid signature means we're out of option ROMs. */
723 if (strncmp((char *)rom->signature, "\x55\xaa", 2) ||
724 (rom->rom_size == 0))
725 break;
727 size = rom->rom_size * 512;
728 /* Invalid checksum means we're out of option ROMs. */
729 csum = 0;
730 for (i = 0; i < size; i++)
731 csum += ((uint8_t *)rom)[i];
732 if (csum != 0)
733 break;
735 /* Check the PCI header (if any) for a match. */
736 pcih = (struct option_rom_pci_header *)
737 ((char *)rom + rom->pci_header_offset);
738 if ((rom->pci_header_offset != 0) &&
739 !strncmp((char *)pcih->signature, "PCIR", 4))
740 goto found;
742 rom = (struct option_rom_header *)((char *)rom + size);
745 return 0;
747 found:
748 /* The size should be both 2K-aligned and page-aligned */
749 total_size = (TARGET_PAGE_SIZE < 2048)
750 ? OPTION_ROM_ALIGN(size + 1)
751 : TARGET_PAGE_ALIGN(size + 1);
753 /* Size of all available ram space is 0x10000 (0xd0000 to 0xe0000) */
754 if ((offset + total_size) > 0x10000u) {
755 fprintf(stderr, "Option ROM size %x exceeds available space\n", size);
756 return 0;
759 addr = qemu_ram_alloc(total_size);
760 cpu_register_physical_memory(0xd0000 + offset, total_size, addr | IO_MEM_ROM);
762 /* Write ROM data and devfn to phys_addr */
763 cpu_physical_memory_write_rom(0xd0000 + offset, rom, size);
764 cpu_physical_memory_write_rom(0xd0000 + offset + size, &devfn, 1);
766 return total_size;
770 * Scan the assigned devices for the devices that have an option ROM, and then
771 * load the corresponding ROM data to RAM. If an error occurs while loading an
772 * option ROM, we just ignore that option ROM and continue with the next one.
774 ram_addr_t assigned_dev_load_option_roms(ram_addr_t rom_base_offset)
776 ram_addr_t offset = rom_base_offset;
777 AssignedDevInfo *adev;
779 LIST_FOREACH(adev, &adev_head, next) {
780 int size, len;
781 void *buf;
782 FILE *fp;
783 uint8_t i = 1;
784 char rom_file[64];
786 snprintf(rom_file, sizeof(rom_file),
787 "/sys/bus/pci/devices/0000:%02x:%02x.%01x/rom",
788 adev->bus, adev->dev, adev->func);
790 if (access(rom_file, F_OK))
791 continue;
793 /* Write something to the ROM file to enable it */
794 fp = fopen(rom_file, "wb");
795 if (fp == NULL)
796 continue;
797 len = fwrite(&i, 1, 1, fp);
798 fclose(fp);
799 if (len != 1)
800 continue;
802 /* The file has to be closed and reopened, otherwise it won't work */
803 fp = fopen(rom_file, "rb");
804 if (fp == NULL)
805 continue;
807 fseek(fp, 0, SEEK_END);
808 size = ftell(fp);
809 fseek(fp, 0, SEEK_SET);
811 buf = malloc(size);
812 if (buf == NULL) {
813 fclose(fp);
814 continue;
817 fread(buf, size, 1, fp);
818 if (!feof(fp) || ferror(fp)) {
819 free(buf);
820 fclose(fp);
821 continue;
824 /* Scan the buffer for suitable ROMs and increase the offset */
825 offset += scan_option_rom(adev->assigned_dev->dev.devfn, buf, offset);
827 free(buf);
828 fclose(fp);
831 return offset;