device-assignment: Fix kvm_get_irq_route_gsi() return check
[qemu-kvm/fedora.git] / hw / device-assignment.c
blob853307bff6df406d443c576f74e2863bb975de9c
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 <unistd.h>
30 #include <sys/io.h>
31 #include <pci/pci.h>
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include "qemu-kvm.h"
35 #include "hw.h"
36 #include "pc.h"
37 #include "sysemu.h"
38 #include "console.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, ...) \
52 do { \
53 fprintf(stderr, "%s: " fmt, __func__ , __VA_ARGS__); \
54 } while (0)
55 #else
56 #define DEBUG(fmt, ...) do { } while(0)
57 #endif
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,
65 uint32_t value)
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);
74 outb(value, r_pio);
77 static void assigned_dev_ioport_writew(void *opaque, uint32_t addr,
78 uint32_t value)
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);
87 outw(value, r_pio);
90 static void assigned_dev_ioport_writel(void *opaque, uint32_t addr,
91 uint32_t value)
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);
100 outl(value, r_pio);
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);
107 uint32_t value;
109 value = inb(r_pio);
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);
115 return 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);
122 uint32_t value;
124 value = inw(r_pio);
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);
130 return 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);
137 uint32_t value;
139 value = inl(r_pio);
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);
145 return 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);
157 int ret = 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;
165 if (!first_map)
166 kvm_destroy_phys_mem(kvm_context, old_ephys,
167 TARGET_PAGE_ALIGN(old_esize));
169 if (e_size > 0) {
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);
176 if (ret == 0)
177 DEBUG("munmap done, virt_base 0x%p\n",
178 region->u.r_virtbase + offset);
179 else {
180 fprintf(stderr, "%s: fail munmap msix table!\n", __func__);
181 exit(1);
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);
191 if (ret != 0) {
192 fprintf(stderr, "%s: Error: create new mapping failed\n", __func__);
193 exit(1);
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);
203 CPUState *env;
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);
211 if (first_map) {
212 struct ioperm_data *data;
214 data = qemu_mallocz(sizeof(struct ioperm_data));
215 if (data == NULL) {
216 fprintf(stderr, "%s: Out of memory\n", __func__);
217 exit(1);
220 data->start_port = region->u.r_baseport;
221 data->num = region->r_size;
222 data->turn_on = 1;
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)
246 int id;
247 int max_cap = 48;
248 int pos = PCI_CAPABILITY_LIST;
249 int status;
251 status = pci_read_byte(pci_dev, PCI_STATUS);
252 if ((status & PCI_STATUS_CAP_LIST) == 0)
253 return 0;
255 while (max_cap--) {
256 pos = pci_read_byte(pci_dev, pos);
257 if (pos < 0x40)
258 break;
260 pos &= ~3;
261 id = pci_read_byte(pci_dev, pos + PCI_CAP_LIST_ID);
263 if (id == 0xff)
264 break;
265 if (id == cap)
266 return pos;
268 pos += PCI_CAP_LIST_NEXT;
270 return 0;
273 static void assigned_dev_pci_write_config(PCIDevice *d, uint32_t address,
274 uint32_t val, int len)
276 int fd;
277 ssize_t ret;
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);
294 return;
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;
303 again:
304 ret = pwrite(fd, &val, len, address);
305 if (ret != len) {
306 if ((ret < 0) && (errno == EINTR || errno == EAGAIN))
307 goto again;
309 fprintf(stderr, "%s: pwrite failed, ret = %zd errno = %d\n",
310 __func__, ret, errno);
312 exit(1);
316 static uint32_t assigned_dev_pci_read_config(PCIDevice *d, uint32_t address,
317 int len)
319 uint32_t val = 0;
320 int fd;
321 ssize_t ret;
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);
331 return val;
334 /* vga specific, remove later */
335 if (address == 0xFC)
336 goto do_log;
338 fd = pci_dev->real_device.config_fd;
340 again:
341 ret = pread(fd, &val, len, address);
342 if (ret != len) {
343 if ((ret < 0) && (errno == EINTR || errno == EAGAIN))
344 goto again;
346 fprintf(stderr, "%s: pread failed, ret = %zd errno = %d\n",
347 __func__, ret, errno);
349 exit(1);
352 do_log:
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)
359 val &= ~0x100000;
360 else if (address == 6)
361 val &= ~0x10;
364 return val;
367 static int assigned_dev_register_regions(PCIRegion *io_regions,
368 unsigned long regions_num,
369 AssignedDevice *pci_dev)
371 uint32_t i;
372 PCIRegion *cur_region = io_regions;
374 for (i = 0; i < regions_num; i++, cur_region++) {
375 if (!cur_region->valid)
376 continue;
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 =
388 mmap(NULL,
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!"
396 "\n", __func__,
397 (uint32_t) (cur_region->base_addr));
398 return -1;
400 pci_dev->v_addrs[i].r_size = cur_region->size;
401 pci_dev->v_addrs[i].e_size = 0;
403 /* add offset */
404 pci_dev->v_addrs[i].u.r_virtbase +=
405 (cur_region->base_addr & 0xFFF);
407 pci_register_io_region((PCIDevice *) pci_dev, i,
408 cur_region->size, t,
409 assigned_dev_iomem_map);
410 continue;
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;
426 /* success */
427 return 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];
434 int fd, r = 0;
435 FILE *f;
436 unsigned long long start, end, size, flags;
437 unsigned long id;
438 struct stat statbuf;
439 PCIRegion *rp;
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);
450 if (fd == -1) {
451 fprintf(stderr, "%s: %s: %m\n", __func__, name);
452 return 1;
454 dev->config_fd = fd;
455 again:
456 r = read(fd, pci_dev->dev.config, sizeof(pci_dev->dev.config));
457 if (r < 0) {
458 if (errno == EINTR || errno == EAGAIN)
459 goto again;
460 fprintf(stderr, "%s: read failed, errno = %d\n", __func__, errno);
463 snprintf(name, sizeof(name), "%sresource", dir);
465 f = fopen(name, "r");
466 if (f == NULL) {
467 fprintf(stderr, "%s: %s: %m\n", __func__, name);
468 return 1;
471 for (r = 0; r < MAX_IO_REGIONS; r++) {
472 if (fscanf(f, "%lli %lli %lli\n", &start, &end, &flags) != 3)
473 break;
475 rp = dev->regions + r;
476 rp->valid = 0;
477 size = end - start + 1;
478 flags &= IORESOURCE_IO | IORESOURCE_MEM | IORESOURCE_PREFETCH;
479 if (size == 0 || (flags & ~IORESOURCE_PREFETCH) == 0)
480 continue;
481 if (flags & IORESOURCE_MEM) {
482 flags &= ~IORESOURCE_IO;
483 snprintf(name, sizeof(name), "%sresource%d", dir, r);
484 fd = open(name, O_RDWR);
485 if (fd == -1)
486 continue; /* probably ROM */
487 rp->resource_fd = fd;
488 } else
489 flags &= ~IORESOURCE_PREFETCH;
491 rp->type = flags;
492 rp->valid = 1;
493 rp->base_addr = start;
494 rp->size = size;
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);
499 fclose(f);
501 /* read and fill device ID */
502 snprintf(name, sizeof(name), "%svendor", dir);
503 f = fopen(name, "r");
504 if (f == NULL) {
505 fprintf(stderr, "%s: %s: %m\n", __func__, name);
506 return 1;
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;
512 fclose(f);
514 /* read and fill vendor ID */
515 snprintf(name, sizeof(name), "%sdevice", dir);
516 f = fopen(name, "r");
517 if (f == NULL) {
518 fprintf(stderr, "%s: %s: %m\n", __func__, name);
519 return 1;
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;
525 fclose(f);
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;
531 else
532 pci_dev->need_emulate_cmd = 0;
534 dev->region_number = r;
535 return 0;
538 static LIST_HEAD(, AssignedDevInfo) adev_head;
540 #ifdef KVM_CAP_IRQ_ROUTING
541 static void free_dev_irq_entries(AssignedDevice *dev)
543 int i;
545 for (i = 0; i < dev->irq_entries_nr; i++)
546 kvm_del_routing_entry(kvm_context, &dev->entry[i]);
547 free(dev->entry);
548 dev->entry = NULL;
549 dev->irq_entries_nr = 0;
551 #endif
553 static void free_assigned_device(AssignedDevInfo *adev)
555 AssignedDevice *dev = adev->assigned_dev;
557 if (dev) {
558 int i;
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))
565 continue;
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);
572 if (ret != 0)
573 fprintf(stderr,
574 "Failed to unmap assigned device region: %s\n",
575 strerror(errno));
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);
587 #endif
588 adev->assigned_dev = dev = NULL;
591 LIST_REMOVE(adev, next);
592 qemu_free(adev);
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;
604 int r;
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;
612 #ifdef KVM_CAP_IOMMU
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;
619 #endif
621 r = kvm_assign_pci_device(kvm_context, &assigned_dev_data);
622 if (r < 0)
623 fprintf(stderr, "Failed to assign device \"%s\" : %s\n",
624 adev->name, strerror(-r));
625 return r;
628 static int assign_irq(AssignedDevInfo *adev)
630 struct kvm_assigned_irq assigned_irq_data;
631 AssignedDevice *dev = adev->assigned_dev;
632 int irq, r = 0;
634 /* Interrupt PIN 0 means don't use INTx */
635 if (pci_read_byte(dev->pdev, PCI_INTERRUPT_PIN) == 0)
636 return 0;
638 irq = pci_map_irq(&dev->dev, dev->intpin);
639 irq = piix_get_irq(irq);
641 #ifdef TARGET_IA64
642 irq = ipf_map_irq(&dev->dev, irq);
643 #endif
645 if (dev->girq == irq)
646 return r;
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;
665 else
666 assigned_irq_data.flags |= KVM_DEV_IRQ_HOST_INTX;
667 #endif
669 r = kvm_assign_irq(kvm_context, &assigned_irq_data);
670 if (r < 0) {
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");
675 return r;
678 dev->girq = irq;
679 dev->irq_requested_type = assigned_irq_data.flags;
680 return r;
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;
688 int r;
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);
695 if (r < 0)
696 fprintf(stderr, "Failed to deassign device \"%s\" : %s\n",
697 adev->name, strerror(-r));
698 #endif
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)
716 return adev;
719 return NULL;
722 /* The pci config space got updated. Check if irq numbers have changed
723 * for our devices
725 void assigned_dev_update_irqs()
727 AssignedDevInfo *adev;
729 adev = LIST_FIRST(&adev_head);
730 while (adev) {
731 AssignedDevInfo *next = LIST_NEXT(adev, next);
732 int r;
734 r = assign_irq(adev);
735 if (r < 0)
736 remove_assigned_device(adev);
738 adev = next;
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];
750 int r;
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: ");
770 return;
772 assigned_dev->entry->u.msi.address_lo =
773 *(uint32_t *)(pci_dev->config + pci_dev->cap.start +
774 PCI_MSI_ADDRESS_LO);
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 r = kvm_get_irq_route_gsi(kvm_context);
780 if (r < 0) {
781 perror("assigned_dev_update_msi: kvm_get_irq_route_gsi");
782 return;
784 assigned_dev->entry->gsi = r;
786 kvm_add_routing_entry(kvm_context, assigned_dev->entry);
787 if (kvm_commit_irq_routes(kvm_context) < 0) {
788 perror("assigned_dev_update_msi: kvm_commit_irq_routes");
789 assigned_dev->cap.state &= ~ASSIGNED_DEVICE_MSI_ENABLED;
790 return;
792 assigned_dev->irq_entries_nr = 1;
794 assigned_irq_data.guest_irq = assigned_dev->entry->gsi;
795 assigned_irq_data.flags = KVM_DEV_IRQ_HOST_MSI | KVM_DEV_IRQ_GUEST_MSI;
796 if (kvm_assign_irq(kvm_context, &assigned_irq_data) < 0)
797 perror("assigned_dev_enable_msi: assign irq");
799 assigned_dev->irq_requested_type = assigned_irq_data.flags;
802 #endif
804 #ifdef KVM_CAP_DEVICE_MSIX
805 static int assigned_dev_update_msix_mmio(PCIDevice *pci_dev)
807 AssignedDevice *adev = container_of(pci_dev, AssignedDevice, dev);
808 u16 entries_nr = 0, entries_max_nr;
809 int pos = 0, i, r = 0;
810 u32 msg_addr, msg_upper_addr, msg_data, msg_ctrl;
811 struct kvm_assigned_msix_nr msix_nr;
812 struct kvm_assigned_msix_entry msix_entry;
813 void *va = adev->msix_table_page;
815 if (adev->cap.available & ASSIGNED_DEVICE_CAP_MSI)
816 pos = pci_dev->cap.start + PCI_CAPABILITY_CONFIG_MSI_LENGTH;
817 else
818 pos = pci_dev->cap.start;
820 entries_max_nr = pci_dev->config[pos + 2];
821 entries_max_nr &= PCI_MSIX_TABSIZE;
822 entries_max_nr += 1;
824 /* Get the usable entry number for allocating */
825 for (i = 0; i < entries_max_nr; i++) {
826 memcpy(&msg_ctrl, va + i * 16 + 12, 4);
827 memcpy(&msg_data, va + i * 16 + 8, 4);
828 /* Ignore unused entry even it's unmasked */
829 if (msg_data == 0)
830 continue;
831 entries_nr ++;
834 if (entries_nr == 0) {
835 fprintf(stderr, "MSI-X entry number is zero!\n");
836 return -EINVAL;
838 msix_nr.assigned_dev_id = calc_assigned_dev_id(adev->h_busnr,
839 (uint8_t)adev->h_devfn);
840 msix_nr.entry_nr = entries_nr;
841 r = kvm_assign_set_msix_nr(kvm_context, &msix_nr);
842 if (r != 0) {
843 fprintf(stderr, "fail to set MSI-X entry number for MSIX! %s\n",
844 strerror(-r));
845 return r;
848 free_dev_irq_entries(adev);
849 adev->irq_entries_nr = entries_nr;
850 adev->entry = calloc(entries_nr, sizeof(struct kvm_irq_routing_entry));
851 if (!adev->entry) {
852 perror("assigned_dev_update_msix_mmio: ");
853 return -errno;
856 msix_entry.assigned_dev_id = msix_nr.assigned_dev_id;
857 entries_nr = 0;
858 for (i = 0; i < entries_max_nr; i++) {
859 if (entries_nr >= msix_nr.entry_nr)
860 break;
861 memcpy(&msg_ctrl, va + i * 16 + 12, 4);
862 if (msg_ctrl & 0x1)
863 continue;
864 memcpy(&msg_data, va + i * 16 + 8, 4);
865 if (msg_data == 0)
866 continue;
868 memcpy(&msg_addr, va + i * 16, 4);
869 memcpy(&msg_upper_addr, va + i * 16 + 4, 4);
871 r = kvm_get_irq_route_gsi(kvm_context);
872 if (r < 0)
873 return r;
875 adev->entry[entries_nr].gsi = r;
876 adev->entry[entries_nr].type = KVM_IRQ_ROUTING_MSI;
877 adev->entry[entries_nr].flags = 0;
878 adev->entry[entries_nr].u.msi.address_lo = msg_addr;
879 adev->entry[entries_nr].u.msi.address_hi = msg_upper_addr;
880 adev->entry[entries_nr].u.msi.data = msg_data;
881 DEBUG("MSI-X data 0x%x, MSI-X addr_lo 0x%x\n!", msg_data, msg_addr);
882 kvm_add_routing_entry(kvm_context, &adev->entry[entries_nr]);
884 msix_entry.gsi = adev->entry[entries_nr].gsi;
885 msix_entry.entry = i;
886 r = kvm_assign_set_msix_entry(kvm_context, &msix_entry);
887 if (r) {
888 fprintf(stderr, "fail to set MSI-X entry! %s\n", strerror(-r));
889 break;
891 DEBUG("MSI-X entry gsi 0x%x, entry %d\n!",
892 msix_entry.gsi, msix_entry.entry);
893 entries_nr ++;
896 if (r == 0 && kvm_commit_irq_routes(kvm_context) < 0) {
897 perror("assigned_dev_update_msix_mmio: kvm_commit_irq_routes");
898 return -EINVAL;
901 return r;
904 static void assigned_dev_update_msix(PCIDevice *pci_dev, unsigned int ctrl_pos)
906 struct kvm_assigned_irq assigned_irq_data;
907 AssignedDevice *assigned_dev = container_of(pci_dev, AssignedDevice, dev);
908 uint16_t *ctrl_word = (uint16_t *)(pci_dev->config + ctrl_pos);
909 int r;
911 memset(&assigned_irq_data, 0, sizeof assigned_irq_data);
912 assigned_irq_data.assigned_dev_id =
913 calc_assigned_dev_id(assigned_dev->h_busnr,
914 (uint8_t)assigned_dev->h_devfn);
916 if (assigned_dev->irq_requested_type) {
917 assigned_irq_data.flags = assigned_dev->irq_requested_type;
918 free_dev_irq_entries(assigned_dev);
919 r = kvm_deassign_irq(kvm_context, &assigned_irq_data);
920 /* -ENXIO means no assigned irq */
921 if (r && r != -ENXIO)
922 perror("assigned_dev_update_msix: deassign irq");
924 assigned_irq_data.flags = KVM_DEV_IRQ_HOST_MSIX | KVM_DEV_IRQ_GUEST_MSIX;
926 if (*ctrl_word & PCI_MSIX_ENABLE) {
927 if (assigned_dev_update_msix_mmio(pci_dev) < 0) {
928 perror("assigned_dev_update_msix_mmio");
929 return;
931 if (kvm_assign_irq(kvm_context, &assigned_irq_data) < 0) {
932 perror("assigned_dev_enable_msix: assign irq");
933 return;
935 assigned_dev->irq_requested_type = assigned_irq_data.flags;
938 #endif
939 #endif
941 static void assigned_device_pci_cap_write_config(PCIDevice *pci_dev, uint32_t address,
942 uint32_t val, int len)
944 AssignedDevice *assigned_dev = container_of(pci_dev, AssignedDevice, dev);
945 unsigned int pos = pci_dev->cap.start, ctrl_pos;
947 pci_default_cap_write_config(pci_dev, address, val, len);
948 #ifdef KVM_CAP_IRQ_ROUTING
949 #ifdef KVM_CAP_DEVICE_MSI
950 if (assigned_dev->cap.available & ASSIGNED_DEVICE_CAP_MSI) {
951 ctrl_pos = pos + PCI_MSI_FLAGS;
952 if (address <= ctrl_pos && address + len > ctrl_pos)
953 assigned_dev_update_msi(pci_dev, ctrl_pos);
954 pos += PCI_CAPABILITY_CONFIG_MSI_LENGTH;
956 #endif
957 #ifdef KVM_CAP_DEVICE_MSIX
958 if (assigned_dev->cap.available & ASSIGNED_DEVICE_CAP_MSIX) {
959 ctrl_pos = pos + 3;
960 if (address <= ctrl_pos && address + len > ctrl_pos) {
961 ctrl_pos--; /* control is word long */
962 assigned_dev_update_msix(pci_dev, ctrl_pos);
964 pos += PCI_CAPABILITY_CONFIG_MSIX_LENGTH;
966 #endif
967 #endif
968 return;
971 static int assigned_device_pci_cap_init(PCIDevice *pci_dev)
973 AssignedDevice *dev = container_of(pci_dev, AssignedDevice, dev);
974 PCIRegion *pci_region = dev->real_device.regions;
975 int next_cap_pt = 0;
977 pci_dev->cap.length = 0;
978 #ifdef KVM_CAP_IRQ_ROUTING
979 #ifdef KVM_CAP_DEVICE_MSI
980 /* Expose MSI capability
981 * MSI capability is the 1st capability in capability config */
982 if (pci_find_cap_offset(dev->pdev, PCI_CAP_ID_MSI)) {
983 dev->cap.available |= ASSIGNED_DEVICE_CAP_MSI;
984 memset(&pci_dev->config[pci_dev->cap.start + pci_dev->cap.length],
985 0, PCI_CAPABILITY_CONFIG_MSI_LENGTH);
986 pci_dev->config[pci_dev->cap.start + pci_dev->cap.length] =
987 PCI_CAP_ID_MSI;
988 pci_dev->cap.length += PCI_CAPABILITY_CONFIG_MSI_LENGTH;
989 next_cap_pt = 1;
991 #endif
992 #ifdef KVM_CAP_DEVICE_MSIX
993 /* Expose MSI-X capability */
994 if (pci_find_cap_offset(dev->pdev, PCI_CAP_ID_MSIX)) {
995 int pos, entry_nr, bar_nr;
996 u32 msix_table_entry;
997 dev->cap.available |= ASSIGNED_DEVICE_CAP_MSIX;
998 memset(&pci_dev->config[pci_dev->cap.start + pci_dev->cap.length],
999 0, PCI_CAPABILITY_CONFIG_MSIX_LENGTH);
1000 pos = pci_find_cap_offset(dev->pdev, PCI_CAP_ID_MSIX);
1001 entry_nr = pci_read_word(dev->pdev, pos + 2) & PCI_MSIX_TABSIZE;
1002 pci_dev->config[pci_dev->cap.start + pci_dev->cap.length] = 0x11;
1003 pci_dev->config[pci_dev->cap.start +
1004 pci_dev->cap.length + 2] = entry_nr;
1005 msix_table_entry = pci_read_long(dev->pdev, pos + PCI_MSIX_TABLE);
1006 *(uint32_t *)(pci_dev->config + pci_dev->cap.start +
1007 pci_dev->cap.length + PCI_MSIX_TABLE) = msix_table_entry;
1008 *(uint32_t *)(pci_dev->config + pci_dev->cap.start +
1009 pci_dev->cap.length + PCI_MSIX_PBA) =
1010 pci_read_long(dev->pdev, pos + PCI_MSIX_PBA);
1011 bar_nr = msix_table_entry & PCI_MSIX_BIR;
1012 msix_table_entry &= ~PCI_MSIX_BIR;
1013 dev->msix_table_addr = pci_region[bar_nr].base_addr + msix_table_entry;
1014 if (next_cap_pt != 0) {
1015 pci_dev->config[pci_dev->cap.start + next_cap_pt] =
1016 pci_dev->cap.start + pci_dev->cap.length;
1017 next_cap_pt += PCI_CAPABILITY_CONFIG_MSI_LENGTH;
1018 } else
1019 next_cap_pt = 1;
1020 pci_dev->cap.length += PCI_CAPABILITY_CONFIG_MSIX_LENGTH;
1022 #endif
1023 #endif
1025 return 0;
1028 static uint32_t msix_mmio_readl(void *opaque, target_phys_addr_t addr)
1030 AssignedDevice *adev = opaque;
1031 unsigned int offset = addr & 0xfff;
1032 void *page = adev->msix_table_page;
1033 uint32_t val = 0;
1035 memcpy(&val, (void *)((char *)page + offset), 4);
1037 return val;
1040 static uint32_t msix_mmio_readb(void *opaque, target_phys_addr_t addr)
1042 return ((msix_mmio_readl(opaque, addr & ~3)) >>
1043 (8 * (addr & 3))) & 0xff;
1046 static uint32_t msix_mmio_readw(void *opaque, target_phys_addr_t addr)
1048 return ((msix_mmio_readl(opaque, addr & ~3)) >>
1049 (8 * (addr & 3))) & 0xffff;
1052 static void msix_mmio_writel(void *opaque,
1053 target_phys_addr_t addr, uint32_t val)
1055 AssignedDevice *adev = opaque;
1056 unsigned int offset = addr & 0xfff;
1057 void *page = adev->msix_table_page;
1059 DEBUG("write to MSI-X entry table mmio offset 0x%lx, val 0x%lx\n",
1060 addr, val);
1061 memcpy((void *)((char *)page + offset), &val, 4);
1064 static void msix_mmio_writew(void *opaque,
1065 target_phys_addr_t addr, uint32_t val)
1067 msix_mmio_writel(opaque, addr & ~3,
1068 (val & 0xffff) << (8*(addr & 3)));
1071 static void msix_mmio_writeb(void *opaque,
1072 target_phys_addr_t addr, uint32_t val)
1074 msix_mmio_writel(opaque, addr & ~3,
1075 (val & 0xff) << (8*(addr & 3)));
1078 static CPUWriteMemoryFunc *msix_mmio_write[] = {
1079 msix_mmio_writeb, msix_mmio_writew, msix_mmio_writel
1082 static CPUReadMemoryFunc *msix_mmio_read[] = {
1083 msix_mmio_readb, msix_mmio_readw, msix_mmio_readl
1086 static int assigned_dev_register_msix_mmio(AssignedDevice *dev)
1088 dev->msix_table_page = mmap(NULL, 0x1000,
1089 PROT_READ|PROT_WRITE,
1090 MAP_ANONYMOUS|MAP_PRIVATE, 0, 0);
1091 if (dev->msix_table_page == MAP_FAILED) {
1092 fprintf(stderr, "fail allocate msix_table_page! %s\n",
1093 strerror(errno));
1094 return -EFAULT;
1096 memset(dev->msix_table_page, 0, 0x1000);
1097 dev->mmio_index = cpu_register_io_memory(0,
1098 msix_mmio_read, msix_mmio_write, dev);
1099 return 0;
1102 struct PCIDevice *init_assigned_device(AssignedDevInfo *adev, PCIBus *bus)
1104 int r;
1105 AssignedDevice *dev;
1106 PCIDevice *pci_dev;
1107 struct pci_access *pacc;
1108 uint8_t e_device, e_intx;
1110 DEBUG("Registering real physical device %s (bus=%x dev=%x func=%x)\n",
1111 adev->name, adev->bus, adev->dev, adev->func);
1113 pci_dev = pci_register_device(bus, adev->name,
1114 sizeof(AssignedDevice), -1, assigned_dev_pci_read_config,
1115 assigned_dev_pci_write_config);
1116 dev = container_of(pci_dev, AssignedDevice, dev);
1118 if (NULL == dev) {
1119 fprintf(stderr, "%s: Error: Couldn't register real device %s\n",
1120 __func__, adev->name);
1121 return NULL;
1124 adev->assigned_dev = dev;
1126 if (get_real_device(dev, adev->bus, adev->dev, adev->func)) {
1127 fprintf(stderr, "%s: Error: Couldn't get real device (%s)!\n",
1128 __func__, adev->name);
1129 goto out;
1132 /* handle real device's MMIO/PIO BARs */
1133 if (assigned_dev_register_regions(dev->real_device.regions,
1134 dev->real_device.region_number,
1135 dev))
1136 goto out;
1138 /* handle interrupt routing */
1139 e_device = (dev->dev.devfn >> 3) & 0x1f;
1140 e_intx = dev->dev.config[0x3d] - 1;
1141 dev->intpin = e_intx;
1142 dev->run = 0;
1143 dev->girq = 0;
1144 dev->h_busnr = adev->bus;
1145 dev->h_devfn = PCI_DEVFN(adev->dev, adev->func);
1147 pacc = pci_alloc();
1148 pci_init(pacc);
1149 dev->pdev = pci_get_dev(pacc, 0, adev->bus, adev->dev, adev->func);
1151 if (pci_enable_capability_support(pci_dev, 0, NULL,
1152 assigned_device_pci_cap_write_config,
1153 assigned_device_pci_cap_init) < 0)
1154 goto assigned_out;
1156 /* assign device to guest */
1157 r = assign_device(adev);
1158 if (r < 0)
1159 goto assigned_out;
1161 /* assign irq for the device */
1162 r = assign_irq(adev);
1163 if (r < 0)
1164 goto assigned_out;
1166 /* intercept MSI-X entry page in the MMIO */
1167 if (dev->cap.available & ASSIGNED_DEVICE_CAP_MSIX)
1168 if (assigned_dev_register_msix_mmio(dev))
1169 return NULL;
1171 return &dev->dev;
1173 assigned_out:
1174 deassign_device(adev);
1175 out:
1176 free_assigned_device(adev);
1177 return NULL;
1181 * Syntax to assign device:
1183 * -pcidevice host=bus:dev.func[,dma=none][,name=Foo]
1185 * Example:
1186 * -pcidevice host=00:13.0,dma=pvdma
1188 * dma can currently only be 'none' to disable iommu support.
1190 AssignedDevInfo *add_assigned_device(const char *arg)
1192 char device[16];
1193 char dma[6];
1194 int r;
1195 AssignedDevInfo *adev;
1197 adev = qemu_mallocz(sizeof(AssignedDevInfo));
1198 if (adev == NULL) {
1199 fprintf(stderr, "%s: Out of memory\n", __func__);
1200 return NULL;
1202 r = get_param_value(device, sizeof(device), "host", arg);
1203 if (!r)
1204 goto bad;
1206 r = pci_parse_host_devaddr(device, &adev->bus, &adev->dev, &adev->func);
1207 if (r)
1208 goto bad;
1210 r = get_param_value(adev->name, sizeof(adev->name), "name", arg);
1211 if (!r)
1212 snprintf(adev->name, sizeof(adev->name), "%s", device);
1214 #ifdef KVM_CAP_IOMMU
1215 r = get_param_value(dma, sizeof(dma), "dma", arg);
1216 if (r && !strncmp(dma, "none", 4))
1217 adev->disable_iommu = 1;
1218 #endif
1220 LIST_INSERT_HEAD(&adev_head, adev, next);
1221 return adev;
1222 bad:
1223 fprintf(stderr, "pcidevice argument parse error; "
1224 "please check the help text for usage\n");
1225 qemu_free(adev);
1226 return NULL;
1229 void add_assigned_devices(PCIBus *bus, const char **devices, int n_devices)
1231 int i;
1233 for (i = 0; i < n_devices; i++) {
1234 struct AssignedDevInfo *adev;
1236 adev = add_assigned_device(devices[i]);
1237 if (!adev) {
1238 fprintf(stderr, "Could not add assigned device %s\n", devices[i]);
1239 exit(1);
1242 if (!init_assigned_device(adev, bus)) {
1243 fprintf(stderr, "Failed to initialize assigned device %s\n",
1244 devices[i]);
1245 exit(1);
1250 /* Option ROM header */
1251 struct option_rom_header {
1252 uint8_t signature[2];
1253 uint8_t rom_size;
1254 uint32_t entry_point;
1255 uint8_t reserved[17];
1256 uint16_t pci_header_offset;
1257 uint16_t expansion_header_offset;
1258 } __attribute__ ((packed));
1260 /* Option ROM PCI data structure */
1261 struct option_rom_pci_header {
1262 uint8_t signature[4];
1263 uint16_t vendor_id;
1264 uint16_t device_id;
1265 uint16_t vital_product_data_offset;
1266 uint16_t structure_length;
1267 uint8_t structure_revision;
1268 uint8_t class_code[3];
1269 uint16_t image_length;
1270 uint16_t image_revision;
1271 uint8_t code_type;
1272 uint8_t indicator;
1273 uint16_t reserved;
1274 } __attribute__ ((packed));
1277 * Scan the list of Option ROMs at roms. If a suitable Option ROM is found,
1278 * allocate a ram space and copy it there. Then return its size aligned to
1279 * both 2KB and target page size.
1281 #define OPTION_ROM_ALIGN(x) (((x) + 2047) & ~2047)
1282 static int scan_option_rom(uint8_t devfn, void *roms, ram_addr_t offset)
1284 int i, size, total_size;
1285 uint8_t csum;
1286 ram_addr_t addr;
1287 struct option_rom_header *rom;
1288 struct option_rom_pci_header *pcih;
1290 rom = roms;
1292 for ( ; ; ) {
1293 /* Invalid signature means we're out of option ROMs. */
1294 if (strncmp((char *)rom->signature, "\x55\xaa", 2) ||
1295 (rom->rom_size == 0))
1296 break;
1298 size = rom->rom_size * 512;
1299 /* Invalid checksum means we're out of option ROMs. */
1300 csum = 0;
1301 for (i = 0; i < size; i++)
1302 csum += ((uint8_t *)rom)[i];
1303 if (csum != 0)
1304 break;
1306 /* Check the PCI header (if any) for a match. */
1307 pcih = (struct option_rom_pci_header *)
1308 ((char *)rom + rom->pci_header_offset);
1309 if ((rom->pci_header_offset != 0) &&
1310 !strncmp((char *)pcih->signature, "PCIR", 4))
1311 goto found;
1313 rom = (struct option_rom_header *)((char *)rom + size);
1316 return 0;
1318 found:
1319 /* The size should be both 2K-aligned and page-aligned */
1320 total_size = (TARGET_PAGE_SIZE < 2048)
1321 ? OPTION_ROM_ALIGN(size + 1)
1322 : TARGET_PAGE_ALIGN(size + 1);
1324 /* Size of all available ram space is 0x10000 (0xd0000 to 0xe0000) */
1325 if ((offset + total_size) > 0x10000u) {
1326 fprintf(stderr, "Option ROM size %x exceeds available space\n", size);
1327 return 0;
1330 addr = qemu_ram_alloc(total_size);
1331 cpu_register_physical_memory(0xd0000 + offset, total_size, addr | IO_MEM_ROM);
1333 /* Write ROM data and devfn to phys_addr */
1334 cpu_physical_memory_write_rom(0xd0000 + offset, (uint8_t *)rom, size);
1335 cpu_physical_memory_write_rom(0xd0000 + offset + size, &devfn, 1);
1337 return total_size;
1341 * Scan the assigned devices for the devices that have an option ROM, and then
1342 * load the corresponding ROM data to RAM. If an error occurs while loading an
1343 * option ROM, we just ignore that option ROM and continue with the next one.
1345 ram_addr_t assigned_dev_load_option_roms(ram_addr_t rom_base_offset)
1347 ram_addr_t offset = rom_base_offset;
1348 AssignedDevInfo *adev;
1350 LIST_FOREACH(adev, &adev_head, next) {
1351 int size, len;
1352 void *buf;
1353 FILE *fp;
1354 uint8_t i = 1;
1355 char rom_file[64];
1357 snprintf(rom_file, sizeof(rom_file),
1358 "/sys/bus/pci/devices/0000:%02x:%02x.%01x/rom",
1359 adev->bus, adev->dev, adev->func);
1361 if (access(rom_file, F_OK))
1362 continue;
1364 /* Write something to the ROM file to enable it */
1365 fp = fopen(rom_file, "wb");
1366 if (fp == NULL)
1367 continue;
1368 len = fwrite(&i, 1, 1, fp);
1369 fclose(fp);
1370 if (len != 1)
1371 continue;
1373 /* The file has to be closed and reopened, otherwise it won't work */
1374 fp = fopen(rom_file, "rb");
1375 if (fp == NULL)
1376 continue;
1378 fseek(fp, 0, SEEK_END);
1379 size = ftell(fp);
1380 fseek(fp, 0, SEEK_SET);
1382 buf = malloc(size);
1383 if (buf == NULL) {
1384 fclose(fp);
1385 continue;
1388 fread(buf, size, 1, fp);
1389 if (!feof(fp) || ferror(fp)) {
1390 free(buf);
1391 fclose(fp);
1392 continue;
1395 /* Scan the buffer for suitable ROMs and increase the offset */
1396 offset += scan_option_rom(adev->assigned_dev->dev.devfn, buf, offset);
1398 free(buf);
1399 fclose(fp);
1402 return offset;