Merge branch 'master' of git://git.sv.gnu.org/qemu
[qemu-kvm/fedora.git] / hw / device-assignment.c
blob838bf5a621dc33066ab4dfa6765fb1d82f599979
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_bar((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_bar((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)
565 continue;
567 if (pci_region->type & IORESOURCE_IO) {
568 kvm_remove_ioperm_data(region->u.r_baseport, region->r_size);
569 continue;
570 } else if (pci_region->type & IORESOURCE_MEM) {
571 if (region->e_size > 0)
572 kvm_destroy_phys_mem(kvm_context, region->e_physbase,
573 TARGET_PAGE_ALIGN(region->e_size));
575 if (region->u.r_virtbase) {
576 int ret = munmap(region->u.r_virtbase,
577 (pci_region->size + 0xFFF) & 0xFFFFF000);
578 if (ret != 0)
579 fprintf(stderr,
580 "Failed to unmap assigned device region: %s\n",
581 strerror(errno));
586 if (dev->real_device.config_fd) {
587 close(dev->real_device.config_fd);
588 dev->real_device.config_fd = 0;
591 pci_unregister_device(&dev->dev, 1);
592 #ifdef KVM_CAP_IRQ_ROUTING
593 free_dev_irq_entries(dev);
594 #endif
595 adev->assigned_dev = dev = NULL;
598 LIST_REMOVE(adev, next);
599 qemu_free(adev);
602 static uint32_t calc_assigned_dev_id(uint8_t bus, uint8_t devfn)
604 return (uint32_t)bus << 8 | (uint32_t)devfn;
607 static int assign_device(AssignedDevInfo *adev)
609 struct kvm_assigned_pci_dev assigned_dev_data;
610 AssignedDevice *dev = adev->assigned_dev;
611 int r;
613 memset(&assigned_dev_data, 0, sizeof(assigned_dev_data));
614 assigned_dev_data.assigned_dev_id =
615 calc_assigned_dev_id(dev->h_busnr, dev->h_devfn);
616 assigned_dev_data.busnr = dev->h_busnr;
617 assigned_dev_data.devfn = dev->h_devfn;
619 #ifdef KVM_CAP_IOMMU
620 /* We always enable the IOMMU if present
621 * (or when not disabled on the command line)
623 r = kvm_check_extension(kvm_context, KVM_CAP_IOMMU);
624 if (r && !adev->disable_iommu)
625 assigned_dev_data.flags |= KVM_DEV_ASSIGN_ENABLE_IOMMU;
626 #endif
628 r = kvm_assign_pci_device(kvm_context, &assigned_dev_data);
629 if (r < 0)
630 fprintf(stderr, "Failed to assign device \"%s\" : %s\n",
631 adev->name, strerror(-r));
632 return r;
635 static int assign_irq(AssignedDevInfo *adev)
637 struct kvm_assigned_irq assigned_irq_data;
638 AssignedDevice *dev = adev->assigned_dev;
639 int irq, r = 0;
641 /* Interrupt PIN 0 means don't use INTx */
642 if (pci_read_byte(dev->pdev, PCI_INTERRUPT_PIN) == 0)
643 return 0;
645 irq = pci_map_irq(&dev->dev, dev->intpin);
646 irq = piix_get_irq(irq);
648 #ifdef TARGET_IA64
649 irq = ipf_map_irq(&dev->dev, irq);
650 #endif
652 if (dev->girq == irq)
653 return r;
655 memset(&assigned_irq_data, 0, sizeof(assigned_irq_data));
656 assigned_irq_data.assigned_dev_id =
657 calc_assigned_dev_id(dev->h_busnr, dev->h_devfn);
658 assigned_irq_data.guest_irq = irq;
659 assigned_irq_data.host_irq = dev->real_device.irq;
660 #ifdef KVM_CAP_ASSIGN_DEV_IRQ
661 if (dev->irq_requested_type) {
662 assigned_irq_data.flags = dev->irq_requested_type;
663 r = kvm_deassign_irq(kvm_context, &assigned_irq_data);
664 /* -ENXIO means no assigned irq */
665 if (r && r != -ENXIO)
666 perror("assign_irq: deassign");
669 assigned_irq_data.flags = KVM_DEV_IRQ_GUEST_INTX;
670 if (dev->cap.available & ASSIGNED_DEVICE_CAP_MSI)
671 assigned_irq_data.flags |= KVM_DEV_IRQ_HOST_MSI;
672 else
673 assigned_irq_data.flags |= KVM_DEV_IRQ_HOST_INTX;
674 #endif
676 r = kvm_assign_irq(kvm_context, &assigned_irq_data);
677 if (r < 0) {
678 fprintf(stderr, "Failed to assign irq for \"%s\": %s\n",
679 adev->name, strerror(-r));
680 fprintf(stderr, "Perhaps you are assigning a device "
681 "that shares an IRQ with another device?\n");
682 return r;
685 dev->girq = irq;
686 dev->irq_requested_type = assigned_irq_data.flags;
687 return r;
690 static void deassign_device(AssignedDevInfo *adev)
692 #ifdef KVM_CAP_DEVICE_DEASSIGNMENT
693 struct kvm_assigned_pci_dev assigned_dev_data;
694 AssignedDevice *dev = adev->assigned_dev;
695 int r;
697 memset(&assigned_dev_data, 0, sizeof(assigned_dev_data));
698 assigned_dev_data.assigned_dev_id =
699 calc_assigned_dev_id(dev->h_busnr, dev->h_devfn);
701 r = kvm_deassign_pci_device(kvm_context, &assigned_dev_data);
702 if (r < 0)
703 fprintf(stderr, "Failed to deassign device \"%s\" : %s\n",
704 adev->name, strerror(-r));
705 #endif
708 void remove_assigned_device(AssignedDevInfo *adev)
710 deassign_device(adev);
711 free_assigned_device(adev);
714 AssignedDevInfo *get_assigned_device(int pcibus, int slot)
716 AssignedDevice *assigned_dev = NULL;
717 AssignedDevInfo *adev = NULL;
719 LIST_FOREACH(adev, &adev_head, next) {
720 assigned_dev = adev->assigned_dev;
721 if (pci_bus_num(assigned_dev->dev.bus) == pcibus &&
722 PCI_SLOT(assigned_dev->dev.devfn) == slot)
723 return adev;
726 return NULL;
729 /* The pci config space got updated. Check if irq numbers have changed
730 * for our devices
732 void assigned_dev_update_irqs()
734 AssignedDevInfo *adev;
736 adev = LIST_FIRST(&adev_head);
737 while (adev) {
738 AssignedDevInfo *next = LIST_NEXT(adev, next);
739 int r;
741 r = assign_irq(adev);
742 if (r < 0)
743 remove_assigned_device(adev);
745 adev = next;
749 #ifdef KVM_CAP_IRQ_ROUTING
751 #ifdef KVM_CAP_DEVICE_MSI
752 static void assigned_dev_update_msi(PCIDevice *pci_dev, unsigned int ctrl_pos)
754 struct kvm_assigned_irq assigned_irq_data;
755 AssignedDevice *assigned_dev = container_of(pci_dev, AssignedDevice, dev);
756 uint8_t ctrl_byte = pci_dev->config[ctrl_pos];
757 int r;
759 memset(&assigned_irq_data, 0, sizeof assigned_irq_data);
760 assigned_irq_data.assigned_dev_id =
761 calc_assigned_dev_id(assigned_dev->h_busnr,
762 (uint8_t)assigned_dev->h_devfn);
764 if (assigned_dev->irq_requested_type) {
765 assigned_irq_data.flags = assigned_dev->irq_requested_type;
766 free_dev_irq_entries(assigned_dev);
767 r = kvm_deassign_irq(kvm_context, &assigned_irq_data);
768 /* -ENXIO means no assigned irq */
769 if (r && r != -ENXIO)
770 perror("assigned_dev_update_msi: deassign irq");
773 if (ctrl_byte & PCI_MSI_FLAGS_ENABLE) {
774 assigned_dev->entry = calloc(1, sizeof(struct kvm_irq_routing_entry));
775 if (!assigned_dev->entry) {
776 perror("assigned_dev_update_msi: ");
777 return;
779 assigned_dev->entry->u.msi.address_lo =
780 *(uint32_t *)(pci_dev->config + pci_dev->cap.start +
781 PCI_MSI_ADDRESS_LO);
782 assigned_dev->entry->u.msi.address_hi = 0;
783 assigned_dev->entry->u.msi.data = *(uint16_t *)(pci_dev->config +
784 pci_dev->cap.start + PCI_MSI_DATA_32);
785 assigned_dev->entry->type = KVM_IRQ_ROUTING_MSI;
786 r = kvm_get_irq_route_gsi(kvm_context);
787 if (r < 0) {
788 perror("assigned_dev_update_msi: kvm_get_irq_route_gsi");
789 return;
791 assigned_dev->entry->gsi = r;
793 kvm_add_routing_entry(kvm_context, assigned_dev->entry);
794 if (kvm_commit_irq_routes(kvm_context) < 0) {
795 perror("assigned_dev_update_msi: kvm_commit_irq_routes");
796 assigned_dev->cap.state &= ~ASSIGNED_DEVICE_MSI_ENABLED;
797 return;
799 assigned_dev->irq_entries_nr = 1;
801 assigned_irq_data.guest_irq = assigned_dev->entry->gsi;
802 assigned_irq_data.flags = KVM_DEV_IRQ_HOST_MSI | KVM_DEV_IRQ_GUEST_MSI;
803 if (kvm_assign_irq(kvm_context, &assigned_irq_data) < 0)
804 perror("assigned_dev_enable_msi: assign irq");
806 assigned_dev->irq_requested_type = assigned_irq_data.flags;
809 #endif
811 #ifdef KVM_CAP_DEVICE_MSIX
812 static int assigned_dev_update_msix_mmio(PCIDevice *pci_dev)
814 AssignedDevice *adev = container_of(pci_dev, AssignedDevice, dev);
815 u16 entries_nr = 0, entries_max_nr;
816 int pos = 0, i, r = 0;
817 u32 msg_addr, msg_upper_addr, msg_data, msg_ctrl;
818 struct kvm_assigned_msix_nr msix_nr;
819 struct kvm_assigned_msix_entry msix_entry;
820 void *va = adev->msix_table_page;
822 if (adev->cap.available & ASSIGNED_DEVICE_CAP_MSI)
823 pos = pci_dev->cap.start + PCI_CAPABILITY_CONFIG_MSI_LENGTH;
824 else
825 pos = pci_dev->cap.start;
827 entries_max_nr = pci_dev->config[pos + 2];
828 entries_max_nr &= PCI_MSIX_TABSIZE;
829 entries_max_nr += 1;
831 /* Get the usable entry number for allocating */
832 for (i = 0; i < entries_max_nr; i++) {
833 memcpy(&msg_ctrl, va + i * 16 + 12, 4);
834 memcpy(&msg_data, va + i * 16 + 8, 4);
835 /* Ignore unused entry even it's unmasked */
836 if (msg_data == 0)
837 continue;
838 entries_nr ++;
841 if (entries_nr == 0) {
842 fprintf(stderr, "MSI-X entry number is zero!\n");
843 return -EINVAL;
845 msix_nr.assigned_dev_id = calc_assigned_dev_id(adev->h_busnr,
846 (uint8_t)adev->h_devfn);
847 msix_nr.entry_nr = entries_nr;
848 r = kvm_assign_set_msix_nr(kvm_context, &msix_nr);
849 if (r != 0) {
850 fprintf(stderr, "fail to set MSI-X entry number for MSIX! %s\n",
851 strerror(-r));
852 return r;
855 free_dev_irq_entries(adev);
856 adev->irq_entries_nr = entries_nr;
857 adev->entry = calloc(entries_nr, sizeof(struct kvm_irq_routing_entry));
858 if (!adev->entry) {
859 perror("assigned_dev_update_msix_mmio: ");
860 return -errno;
863 msix_entry.assigned_dev_id = msix_nr.assigned_dev_id;
864 entries_nr = 0;
865 for (i = 0; i < entries_max_nr; i++) {
866 if (entries_nr >= msix_nr.entry_nr)
867 break;
868 memcpy(&msg_ctrl, va + i * 16 + 12, 4);
869 memcpy(&msg_data, va + i * 16 + 8, 4);
870 if (msg_data == 0)
871 continue;
873 memcpy(&msg_addr, va + i * 16, 4);
874 memcpy(&msg_upper_addr, va + i * 16 + 4, 4);
876 r = kvm_get_irq_route_gsi(kvm_context);
877 if (r < 0)
878 return r;
880 adev->entry[entries_nr].gsi = r;
881 adev->entry[entries_nr].type = KVM_IRQ_ROUTING_MSI;
882 adev->entry[entries_nr].flags = 0;
883 adev->entry[entries_nr].u.msi.address_lo = msg_addr;
884 adev->entry[entries_nr].u.msi.address_hi = msg_upper_addr;
885 adev->entry[entries_nr].u.msi.data = msg_data;
886 DEBUG("MSI-X data 0x%x, MSI-X addr_lo 0x%x\n!", msg_data, msg_addr);
887 kvm_add_routing_entry(kvm_context, &adev->entry[entries_nr]);
889 msix_entry.gsi = adev->entry[entries_nr].gsi;
890 msix_entry.entry = i;
891 r = kvm_assign_set_msix_entry(kvm_context, &msix_entry);
892 if (r) {
893 fprintf(stderr, "fail to set MSI-X entry! %s\n", strerror(-r));
894 break;
896 DEBUG("MSI-X entry gsi 0x%x, entry %d\n!",
897 msix_entry.gsi, msix_entry.entry);
898 entries_nr ++;
901 if (r == 0 && kvm_commit_irq_routes(kvm_context) < 0) {
902 perror("assigned_dev_update_msix_mmio: kvm_commit_irq_routes");
903 return -EINVAL;
906 return r;
909 static void assigned_dev_update_msix(PCIDevice *pci_dev, unsigned int ctrl_pos)
911 struct kvm_assigned_irq assigned_irq_data;
912 AssignedDevice *assigned_dev = container_of(pci_dev, AssignedDevice, dev);
913 uint16_t *ctrl_word = (uint16_t *)(pci_dev->config + ctrl_pos);
914 int r;
916 memset(&assigned_irq_data, 0, sizeof assigned_irq_data);
917 assigned_irq_data.assigned_dev_id =
918 calc_assigned_dev_id(assigned_dev->h_busnr,
919 (uint8_t)assigned_dev->h_devfn);
921 if (assigned_dev->irq_requested_type) {
922 assigned_irq_data.flags = assigned_dev->irq_requested_type;
923 free_dev_irq_entries(assigned_dev);
924 r = kvm_deassign_irq(kvm_context, &assigned_irq_data);
925 /* -ENXIO means no assigned irq */
926 if (r && r != -ENXIO)
927 perror("assigned_dev_update_msix: deassign irq");
929 assigned_irq_data.flags = KVM_DEV_IRQ_HOST_MSIX | KVM_DEV_IRQ_GUEST_MSIX;
931 if (*ctrl_word & PCI_MSIX_ENABLE) {
932 if (assigned_dev_update_msix_mmio(pci_dev) < 0) {
933 perror("assigned_dev_update_msix_mmio");
934 return;
936 if (kvm_assign_irq(kvm_context, &assigned_irq_data) < 0) {
937 perror("assigned_dev_enable_msix: assign irq");
938 return;
940 assigned_dev->irq_requested_type = assigned_irq_data.flags;
943 #endif
944 #endif
946 static void assigned_device_pci_cap_write_config(PCIDevice *pci_dev, uint32_t address,
947 uint32_t val, int len)
949 AssignedDevice *assigned_dev = container_of(pci_dev, AssignedDevice, dev);
950 unsigned int pos = pci_dev->cap.start, ctrl_pos;
952 pci_default_cap_write_config(pci_dev, address, val, len);
953 #ifdef KVM_CAP_IRQ_ROUTING
954 #ifdef KVM_CAP_DEVICE_MSI
955 if (assigned_dev->cap.available & ASSIGNED_DEVICE_CAP_MSI) {
956 ctrl_pos = pos + PCI_MSI_FLAGS;
957 if (address <= ctrl_pos && address + len > ctrl_pos)
958 assigned_dev_update_msi(pci_dev, ctrl_pos);
959 pos += PCI_CAPABILITY_CONFIG_MSI_LENGTH;
961 #endif
962 #ifdef KVM_CAP_DEVICE_MSIX
963 if (assigned_dev->cap.available & ASSIGNED_DEVICE_CAP_MSIX) {
964 ctrl_pos = pos + 3;
965 if (address <= ctrl_pos && address + len > ctrl_pos) {
966 ctrl_pos--; /* control is word long */
967 assigned_dev_update_msix(pci_dev, ctrl_pos);
969 pos += PCI_CAPABILITY_CONFIG_MSIX_LENGTH;
971 #endif
972 #endif
973 return;
976 static int assigned_device_pci_cap_init(PCIDevice *pci_dev)
978 AssignedDevice *dev = container_of(pci_dev, AssignedDevice, dev);
979 PCIRegion *pci_region = dev->real_device.regions;
980 int next_cap_pt = 0;
982 pci_dev->cap.length = 0;
983 #ifdef KVM_CAP_IRQ_ROUTING
984 #ifdef KVM_CAP_DEVICE_MSI
985 /* Expose MSI capability
986 * MSI capability is the 1st capability in capability config */
987 if (pci_find_cap_offset(dev->pdev, PCI_CAP_ID_MSI)) {
988 dev->cap.available |= ASSIGNED_DEVICE_CAP_MSI;
989 memset(&pci_dev->config[pci_dev->cap.start + pci_dev->cap.length],
990 0, PCI_CAPABILITY_CONFIG_MSI_LENGTH);
991 pci_dev->config[pci_dev->cap.start + pci_dev->cap.length] =
992 PCI_CAP_ID_MSI;
993 pci_dev->cap.length += PCI_CAPABILITY_CONFIG_MSI_LENGTH;
994 next_cap_pt = 1;
996 #endif
997 #ifdef KVM_CAP_DEVICE_MSIX
998 /* Expose MSI-X capability */
999 if (pci_find_cap_offset(dev->pdev, PCI_CAP_ID_MSIX)) {
1000 int pos, entry_nr, bar_nr;
1001 u32 msix_table_entry;
1002 dev->cap.available |= ASSIGNED_DEVICE_CAP_MSIX;
1003 memset(&pci_dev->config[pci_dev->cap.start + pci_dev->cap.length],
1004 0, PCI_CAPABILITY_CONFIG_MSIX_LENGTH);
1005 pos = pci_find_cap_offset(dev->pdev, PCI_CAP_ID_MSIX);
1006 entry_nr = pci_read_word(dev->pdev, pos + 2) & PCI_MSIX_TABSIZE;
1007 pci_dev->config[pci_dev->cap.start + pci_dev->cap.length] = 0x11;
1008 pci_dev->config[pci_dev->cap.start +
1009 pci_dev->cap.length + 2] = entry_nr;
1010 msix_table_entry = pci_read_long(dev->pdev, pos + PCI_MSIX_TABLE);
1011 *(uint32_t *)(pci_dev->config + pci_dev->cap.start +
1012 pci_dev->cap.length + PCI_MSIX_TABLE) = msix_table_entry;
1013 *(uint32_t *)(pci_dev->config + pci_dev->cap.start +
1014 pci_dev->cap.length + PCI_MSIX_PBA) =
1015 pci_read_long(dev->pdev, pos + PCI_MSIX_PBA);
1016 bar_nr = msix_table_entry & PCI_MSIX_BIR;
1017 msix_table_entry &= ~PCI_MSIX_BIR;
1018 dev->msix_table_addr = pci_region[bar_nr].base_addr + msix_table_entry;
1019 if (next_cap_pt != 0) {
1020 pci_dev->config[pci_dev->cap.start + next_cap_pt] =
1021 pci_dev->cap.start + pci_dev->cap.length;
1022 next_cap_pt += PCI_CAPABILITY_CONFIG_MSI_LENGTH;
1023 } else
1024 next_cap_pt = 1;
1025 pci_dev->cap.length += PCI_CAPABILITY_CONFIG_MSIX_LENGTH;
1027 #endif
1028 #endif
1030 return 0;
1033 static uint32_t msix_mmio_readl(void *opaque, target_phys_addr_t addr)
1035 AssignedDevice *adev = opaque;
1036 unsigned int offset = addr & 0xfff;
1037 void *page = adev->msix_table_page;
1038 uint32_t val = 0;
1040 memcpy(&val, (void *)((char *)page + offset), 4);
1042 return val;
1045 static uint32_t msix_mmio_readb(void *opaque, target_phys_addr_t addr)
1047 return ((msix_mmio_readl(opaque, addr & ~3)) >>
1048 (8 * (addr & 3))) & 0xff;
1051 static uint32_t msix_mmio_readw(void *opaque, target_phys_addr_t addr)
1053 return ((msix_mmio_readl(opaque, addr & ~3)) >>
1054 (8 * (addr & 3))) & 0xffff;
1057 static void msix_mmio_writel(void *opaque,
1058 target_phys_addr_t addr, uint32_t val)
1060 AssignedDevice *adev = opaque;
1061 unsigned int offset = addr & 0xfff;
1062 void *page = adev->msix_table_page;
1064 DEBUG("write to MSI-X entry table mmio offset 0x%lx, val 0x%lx\n",
1065 addr, val);
1066 memcpy((void *)((char *)page + offset), &val, 4);
1069 static void msix_mmio_writew(void *opaque,
1070 target_phys_addr_t addr, uint32_t val)
1072 msix_mmio_writel(opaque, addr & ~3,
1073 (val & 0xffff) << (8*(addr & 3)));
1076 static void msix_mmio_writeb(void *opaque,
1077 target_phys_addr_t addr, uint32_t val)
1079 msix_mmio_writel(opaque, addr & ~3,
1080 (val & 0xff) << (8*(addr & 3)));
1083 static CPUWriteMemoryFunc *msix_mmio_write[] = {
1084 msix_mmio_writeb, msix_mmio_writew, msix_mmio_writel
1087 static CPUReadMemoryFunc *msix_mmio_read[] = {
1088 msix_mmio_readb, msix_mmio_readw, msix_mmio_readl
1091 static int assigned_dev_register_msix_mmio(AssignedDevice *dev)
1093 dev->msix_table_page = mmap(NULL, 0x1000,
1094 PROT_READ|PROT_WRITE,
1095 MAP_ANONYMOUS|MAP_PRIVATE, 0, 0);
1096 if (dev->msix_table_page == MAP_FAILED) {
1097 fprintf(stderr, "fail allocate msix_table_page! %s\n",
1098 strerror(errno));
1099 return -EFAULT;
1101 memset(dev->msix_table_page, 0, 0x1000);
1102 dev->mmio_index = cpu_register_io_memory(
1103 msix_mmio_read, msix_mmio_write, dev);
1104 return 0;
1107 struct PCIDevice *init_assigned_device(AssignedDevInfo *adev, PCIBus *bus)
1109 int r;
1110 AssignedDevice *dev;
1111 PCIDevice *pci_dev;
1112 struct pci_access *pacc;
1113 uint8_t e_device, e_intx;
1115 DEBUG("Registering real physical device %s (bus=%x dev=%x func=%x)\n",
1116 adev->name, adev->bus, adev->dev, adev->func);
1118 pci_dev = pci_register_device(bus, adev->name,
1119 sizeof(AssignedDevice), -1, assigned_dev_pci_read_config,
1120 assigned_dev_pci_write_config);
1121 dev = container_of(pci_dev, AssignedDevice, dev);
1123 if (NULL == dev) {
1124 fprintf(stderr, "%s: Error: Couldn't register real device %s\n",
1125 __func__, adev->name);
1126 return NULL;
1129 adev->assigned_dev = dev;
1131 if (get_real_device(dev, adev->bus, adev->dev, adev->func)) {
1132 fprintf(stderr, "%s: Error: Couldn't get real device (%s)!\n",
1133 __func__, adev->name);
1134 goto out;
1137 /* handle real device's MMIO/PIO BARs */
1138 if (assigned_dev_register_regions(dev->real_device.regions,
1139 dev->real_device.region_number,
1140 dev))
1141 goto out;
1143 /* handle interrupt routing */
1144 e_device = (dev->dev.devfn >> 3) & 0x1f;
1145 e_intx = dev->dev.config[0x3d] - 1;
1146 dev->intpin = e_intx;
1147 dev->run = 0;
1148 dev->girq = 0;
1149 dev->h_busnr = adev->bus;
1150 dev->h_devfn = PCI_DEVFN(adev->dev, adev->func);
1152 pacc = pci_alloc();
1153 pci_init(pacc);
1154 dev->pdev = pci_get_dev(pacc, 0, adev->bus, adev->dev, adev->func);
1156 if (pci_enable_capability_support(pci_dev, 0, NULL,
1157 assigned_device_pci_cap_write_config,
1158 assigned_device_pci_cap_init) < 0)
1159 goto assigned_out;
1161 /* assign device to guest */
1162 r = assign_device(adev);
1163 if (r < 0)
1164 goto assigned_out;
1166 /* assign irq for the device */
1167 r = assign_irq(adev);
1168 if (r < 0)
1169 goto assigned_out;
1171 /* intercept MSI-X entry page in the MMIO */
1172 if (dev->cap.available & ASSIGNED_DEVICE_CAP_MSIX)
1173 if (assigned_dev_register_msix_mmio(dev))
1174 return NULL;
1176 return &dev->dev;
1178 assigned_out:
1179 deassign_device(adev);
1180 out:
1181 free_assigned_device(adev);
1182 return NULL;
1186 * Syntax to assign device:
1188 * -pcidevice host=bus:dev.func[,dma=none][,name=Foo]
1190 * Example:
1191 * -pcidevice host=00:13.0,dma=pvdma
1193 * dma can currently only be 'none' to disable iommu support.
1195 AssignedDevInfo *add_assigned_device(const char *arg)
1197 char device[16];
1198 char dma[6];
1199 int r;
1200 AssignedDevInfo *adev;
1202 adev = qemu_mallocz(sizeof(AssignedDevInfo));
1203 if (adev == NULL) {
1204 fprintf(stderr, "%s: Out of memory\n", __func__);
1205 return NULL;
1207 r = get_param_value(device, sizeof(device), "host", arg);
1208 if (!r)
1209 goto bad;
1211 r = pci_parse_host_devaddr(device, &adev->bus, &adev->dev, &adev->func);
1212 if (r)
1213 goto bad;
1215 r = get_param_value(adev->name, sizeof(adev->name), "name", arg);
1216 if (!r)
1217 snprintf(adev->name, sizeof(adev->name), "%s", device);
1219 #ifdef KVM_CAP_IOMMU
1220 r = get_param_value(dma, sizeof(dma), "dma", arg);
1221 if (r && !strncmp(dma, "none", 4))
1222 adev->disable_iommu = 1;
1223 #endif
1225 LIST_INSERT_HEAD(&adev_head, adev, next);
1226 return adev;
1227 bad:
1228 fprintf(stderr, "pcidevice argument parse error; "
1229 "please check the help text for usage\n");
1230 qemu_free(adev);
1231 return NULL;
1234 void add_assigned_devices(PCIBus *bus, const char **devices, int n_devices)
1236 int i;
1238 for (i = 0; i < n_devices; i++) {
1239 struct AssignedDevInfo *adev;
1241 adev = add_assigned_device(devices[i]);
1242 if (!adev) {
1243 fprintf(stderr, "Could not add assigned device %s\n", devices[i]);
1244 exit(1);
1247 if (!init_assigned_device(adev, bus)) {
1248 fprintf(stderr, "Failed to initialize assigned device %s\n",
1249 devices[i]);
1250 exit(1);
1255 /* Option ROM header */
1256 struct option_rom_header {
1257 uint8_t signature[2];
1258 uint8_t rom_size;
1259 uint32_t entry_point;
1260 uint8_t reserved[17];
1261 uint16_t pci_header_offset;
1262 uint16_t expansion_header_offset;
1263 } __attribute__ ((packed));
1265 /* Option ROM PCI data structure */
1266 struct option_rom_pci_header {
1267 uint8_t signature[4];
1268 uint16_t vendor_id;
1269 uint16_t device_id;
1270 uint16_t vital_product_data_offset;
1271 uint16_t structure_length;
1272 uint8_t structure_revision;
1273 uint8_t class_code[3];
1274 uint16_t image_length;
1275 uint16_t image_revision;
1276 uint8_t code_type;
1277 uint8_t indicator;
1278 uint16_t reserved;
1279 } __attribute__ ((packed));
1282 * Scan the list of Option ROMs at roms. If a suitable Option ROM is found,
1283 * allocate a ram space and copy it there. Then return its size aligned to
1284 * both 2KB and target page size.
1286 #define OPTION_ROM_ALIGN(x) (((x) + 2047) & ~2047)
1287 static int scan_option_rom(uint8_t devfn, void *roms, ram_addr_t offset)
1289 int i, size, total_size;
1290 uint8_t csum;
1291 ram_addr_t addr;
1292 struct option_rom_header *rom;
1293 struct option_rom_pci_header *pcih;
1295 rom = roms;
1297 for ( ; ; ) {
1298 /* Invalid signature means we're out of option ROMs. */
1299 if (strncmp((char *)rom->signature, "\x55\xaa", 2) ||
1300 (rom->rom_size == 0))
1301 break;
1303 size = rom->rom_size * 512;
1304 /* Invalid checksum means we're out of option ROMs. */
1305 csum = 0;
1306 for (i = 0; i < size; i++)
1307 csum += ((uint8_t *)rom)[i];
1308 if (csum != 0)
1309 break;
1311 /* Check the PCI header (if any) for a match. */
1312 pcih = (struct option_rom_pci_header *)
1313 ((char *)rom + rom->pci_header_offset);
1314 if ((rom->pci_header_offset != 0) &&
1315 !strncmp((char *)pcih->signature, "PCIR", 4))
1316 goto found;
1318 rom = (struct option_rom_header *)((char *)rom + size);
1321 return 0;
1323 found:
1324 /* The size should be both 2K-aligned and page-aligned */
1325 total_size = (TARGET_PAGE_SIZE < 2048)
1326 ? OPTION_ROM_ALIGN(size + 1)
1327 : TARGET_PAGE_ALIGN(size + 1);
1329 /* Size of all available ram space is 0x10000 (0xd0000 to 0xe0000) */
1330 if ((offset + total_size) > 0x10000u) {
1331 fprintf(stderr, "Option ROM size %x exceeds available space\n", size);
1332 return 0;
1335 addr = qemu_ram_alloc(total_size);
1336 cpu_register_physical_memory(0xd0000 + offset, total_size, addr | IO_MEM_ROM);
1338 /* Write ROM data and devfn to phys_addr */
1339 cpu_physical_memory_write_rom(0xd0000 + offset, (uint8_t *)rom, size);
1340 cpu_physical_memory_write_rom(0xd0000 + offset + size, &devfn, 1);
1342 return total_size;
1346 * Scan the assigned devices for the devices that have an option ROM, and then
1347 * load the corresponding ROM data to RAM. If an error occurs while loading an
1348 * option ROM, we just ignore that option ROM and continue with the next one.
1350 ram_addr_t assigned_dev_load_option_roms(ram_addr_t rom_base_offset)
1352 ram_addr_t offset = rom_base_offset;
1353 AssignedDevInfo *adev;
1355 LIST_FOREACH(adev, &adev_head, next) {
1356 int size, len;
1357 void *buf;
1358 FILE *fp;
1359 uint8_t i = 1;
1360 char rom_file[64];
1362 snprintf(rom_file, sizeof(rom_file),
1363 "/sys/bus/pci/devices/0000:%02x:%02x.%01x/rom",
1364 adev->bus, adev->dev, adev->func);
1366 if (access(rom_file, F_OK))
1367 continue;
1369 /* Write something to the ROM file to enable it */
1370 fp = fopen(rom_file, "wb");
1371 if (fp == NULL)
1372 continue;
1373 len = fwrite(&i, 1, 1, fp);
1374 fclose(fp);
1375 if (len != 1)
1376 continue;
1378 /* The file has to be closed and reopened, otherwise it won't work */
1379 fp = fopen(rom_file, "rb");
1380 if (fp == NULL)
1381 continue;
1383 fseek(fp, 0, SEEK_END);
1384 size = ftell(fp);
1385 fseek(fp, 0, SEEK_SET);
1387 buf = malloc(size);
1388 if (buf == NULL) {
1389 fclose(fp);
1390 continue;
1393 fread(buf, size, 1, fp);
1394 if (!feof(fp) || ferror(fp)) {
1395 free(buf);
1396 fclose(fp);
1397 continue;
1400 /* Scan the buffer for suitable ROMs and increase the offset */
1401 offset += scan_option_rom(adev->assigned_dev->dev.devfn, buf, offset);
1403 free(buf);
1404 fclose(fp);
1407 return offset;