Improve pci host device address parsing
[qemu-kvm/fedora.git] / hw / device-assignment.c
blob09e54ae33f0c61c9717fe312fb29758511bb9fa9
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 assigned_dev->entry->gsi = kvm_get_irq_route_gsi(kvm_context);
780 if (assigned_dev->entry->gsi < 0) {
781 perror("assigned_dev_update_msi: kvm_get_irq_route_gsi");
782 return;
785 kvm_add_routing_entry(kvm_context, assigned_dev->entry);
786 if (kvm_commit_irq_routes(kvm_context) < 0) {
787 perror("assigned_dev_update_msi: kvm_commit_irq_routes");
788 assigned_dev->cap.state &= ~ASSIGNED_DEVICE_MSI_ENABLED;
789 return;
791 assigned_dev->irq_entries_nr = 1;
793 assigned_irq_data.guest_irq = assigned_dev->entry->gsi;
794 assigned_irq_data.flags = KVM_DEV_IRQ_HOST_MSI | KVM_DEV_IRQ_GUEST_MSI;
795 if (kvm_assign_irq(kvm_context, &assigned_irq_data) < 0)
796 perror("assigned_dev_enable_msi: assign irq");
798 assigned_dev->irq_requested_type = assigned_irq_data.flags;
801 #endif
803 #ifdef KVM_CAP_DEVICE_MSIX
804 static int assigned_dev_update_msix_mmio(PCIDevice *pci_dev)
806 AssignedDevice *adev = container_of(pci_dev, AssignedDevice, dev);
807 u16 entries_nr = 0, entries_max_nr;
808 int pos = 0, i, r = 0;
809 u32 msg_addr, msg_upper_addr, msg_data, msg_ctrl;
810 struct kvm_assigned_msix_nr msix_nr;
811 struct kvm_assigned_msix_entry msix_entry;
812 void *va = adev->msix_table_page;
814 if (adev->cap.available & ASSIGNED_DEVICE_CAP_MSI)
815 pos = pci_dev->cap.start + PCI_CAPABILITY_CONFIG_MSI_LENGTH;
816 else
817 pos = pci_dev->cap.start;
819 entries_max_nr = pci_dev->config[pos + 2];
820 entries_max_nr &= PCI_MSIX_TABSIZE;
822 /* Get the usable entry number for allocating */
823 for (i = 0; i < entries_max_nr; i++) {
824 memcpy(&msg_ctrl, va + i * 16 + 12, 4);
825 /* 0x1 is mask bit for per vector */
826 if (msg_ctrl & 0x1)
827 continue;
828 memcpy(&msg_data, va + i * 16 + 8, 4);
829 /* Ignore unused entry even it's unmasked */
830 if (msg_data == 0)
831 continue;
832 entries_nr ++;
835 if (entries_nr == 0) {
836 fprintf(stderr, "MSI-X entry number is zero!\n");
837 return -EINVAL;
839 msix_nr.assigned_dev_id = calc_assigned_dev_id(adev->h_busnr,
840 (uint8_t)adev->h_devfn);
841 msix_nr.entry_nr = entries_nr;
842 r = kvm_assign_set_msix_nr(kvm_context, &msix_nr);
843 if (r != 0) {
844 fprintf(stderr, "fail to set MSI-X entry number for MSIX! %s\n",
845 strerror(-r));
846 return r;
849 free_dev_irq_entries(adev);
850 adev->irq_entries_nr = entries_nr;
851 adev->entry = calloc(entries_nr, sizeof(struct kvm_irq_routing_entry));
852 if (!adev->entry) {
853 perror("assigned_dev_update_msix_mmio: ");
854 return -errno;
857 msix_entry.assigned_dev_id = msix_nr.assigned_dev_id;
858 entries_nr = 0;
859 for (i = 0; i < entries_max_nr; i++) {
860 if (entries_nr >= msix_nr.entry_nr)
861 break;
862 memcpy(&msg_ctrl, va + i * 16 + 12, 4);
863 if (msg_ctrl & 0x1)
864 continue;
865 memcpy(&msg_data, va + i * 16 + 8, 4);
866 if (msg_data == 0)
867 continue;
869 memcpy(&msg_addr, va + i * 16, 4);
870 memcpy(&msg_upper_addr, va + i * 16 + 4, 4);
872 r = kvm_get_irq_route_gsi(kvm_context);
873 if (r < 0)
874 return r;
876 adev->entry[entries_nr].gsi = r;
877 adev->entry[entries_nr].type = KVM_IRQ_ROUTING_MSI;
878 adev->entry[entries_nr].flags = 0;
879 adev->entry[entries_nr].u.msi.address_lo = msg_addr;
880 adev->entry[entries_nr].u.msi.address_hi = msg_upper_addr;
881 adev->entry[entries_nr].u.msi.data = msg_data;
882 DEBUG("MSI-X data 0x%x, MSI-X addr_lo 0x%x\n!", msg_data, msg_addr);
883 kvm_add_routing_entry(kvm_context, &adev->entry[entries_nr]);
885 msix_entry.gsi = adev->entry[entries_nr].gsi;
886 msix_entry.entry = i;
887 r = kvm_assign_set_msix_entry(kvm_context, &msix_entry);
888 if (r) {
889 fprintf(stderr, "fail to set MSI-X entry! %s\n", strerror(-r));
890 break;
892 DEBUG("MSI-X entry gsi 0x%x, entry %d\n!",
893 msix_entry.gsi, msix_entry.entry);
894 entries_nr ++;
897 if (r == 0 && kvm_commit_irq_routes(kvm_context) < 0) {
898 perror("assigned_dev_update_msix_mmio: kvm_commit_irq_routes");
899 return -EINVAL;
902 return r;
905 static void assigned_dev_update_msix(PCIDevice *pci_dev, unsigned int ctrl_pos)
907 struct kvm_assigned_irq assigned_irq_data;
908 AssignedDevice *assigned_dev = container_of(pci_dev, AssignedDevice, dev);
909 uint16_t *ctrl_word = (uint16_t *)(pci_dev->config + ctrl_pos);
910 int r;
912 memset(&assigned_irq_data, 0, sizeof assigned_irq_data);
913 assigned_irq_data.assigned_dev_id =
914 calc_assigned_dev_id(assigned_dev->h_busnr,
915 (uint8_t)assigned_dev->h_devfn);
917 if (assigned_dev->irq_requested_type) {
918 assigned_irq_data.flags = assigned_dev->irq_requested_type;
919 free_dev_irq_entries(assigned_dev);
920 r = kvm_deassign_irq(kvm_context, &assigned_irq_data);
921 /* -ENXIO means no assigned irq */
922 if (r && r != -ENXIO)
923 perror("assigned_dev_update_msix: deassign irq");
925 assigned_irq_data.flags = KVM_DEV_IRQ_HOST_MSIX | KVM_DEV_IRQ_GUEST_MSIX;
927 if (*ctrl_word & PCI_MSIX_ENABLE) {
928 if (assigned_dev_update_msix_mmio(pci_dev) < 0) {
929 perror("assigned_dev_update_msix_mmio");
930 return;
932 if (kvm_assign_irq(kvm_context, &assigned_irq_data) < 0) {
933 perror("assigned_dev_enable_msix: assign irq");
934 return;
936 assigned_dev->irq_requested_type = assigned_irq_data.flags;
939 #endif
940 #endif
942 static void assigned_device_pci_cap_write_config(PCIDevice *pci_dev, uint32_t address,
943 uint32_t val, int len)
945 AssignedDevice *assigned_dev = container_of(pci_dev, AssignedDevice, dev);
946 unsigned int pos = pci_dev->cap.start, ctrl_pos;
948 pci_default_cap_write_config(pci_dev, address, val, len);
949 #ifdef KVM_CAP_IRQ_ROUTING
950 #ifdef KVM_CAP_DEVICE_MSI
951 if (assigned_dev->cap.available & ASSIGNED_DEVICE_CAP_MSI) {
952 ctrl_pos = pos + PCI_MSI_FLAGS;
953 if (address <= ctrl_pos && address + len > ctrl_pos)
954 assigned_dev_update_msi(pci_dev, ctrl_pos);
955 pos += PCI_CAPABILITY_CONFIG_MSI_LENGTH;
957 #endif
958 #ifdef KVM_CAP_DEVICE_MSIX
959 if (assigned_dev->cap.available & ASSIGNED_DEVICE_CAP_MSIX) {
960 ctrl_pos = pos + 3;
961 if (address <= ctrl_pos && address + len > ctrl_pos) {
962 ctrl_pos--; /* control is word long */
963 assigned_dev_update_msix(pci_dev, ctrl_pos);
965 pos += PCI_CAPABILITY_CONFIG_MSIX_LENGTH;
967 #endif
968 #endif
969 return;
972 static int assigned_device_pci_cap_init(PCIDevice *pci_dev)
974 AssignedDevice *dev = container_of(pci_dev, AssignedDevice, dev);
975 PCIRegion *pci_region = dev->real_device.regions;
976 int next_cap_pt = 0;
978 pci_dev->cap.length = 0;
979 #ifdef KVM_CAP_IRQ_ROUTING
980 #ifdef KVM_CAP_DEVICE_MSI
981 /* Expose MSI capability
982 * MSI capability is the 1st capability in capability config */
983 if (pci_find_cap_offset(dev->pdev, PCI_CAP_ID_MSI)) {
984 dev->cap.available |= ASSIGNED_DEVICE_CAP_MSI;
985 memset(&pci_dev->config[pci_dev->cap.start + pci_dev->cap.length],
986 0, PCI_CAPABILITY_CONFIG_MSI_LENGTH);
987 pci_dev->config[pci_dev->cap.start + pci_dev->cap.length] =
988 PCI_CAP_ID_MSI;
989 pci_dev->cap.length += PCI_CAPABILITY_CONFIG_MSI_LENGTH;
990 next_cap_pt = 1;
992 #endif
993 #ifdef KVM_CAP_DEVICE_MSIX
994 /* Expose MSI-X capability */
995 if (pci_find_cap_offset(dev->pdev, PCI_CAP_ID_MSIX)) {
996 int pos, entry_nr, bar_nr;
997 u32 msix_table_entry;
998 dev->cap.available |= ASSIGNED_DEVICE_CAP_MSIX;
999 memset(&pci_dev->config[pci_dev->cap.start + pci_dev->cap.length],
1000 0, PCI_CAPABILITY_CONFIG_MSIX_LENGTH);
1001 pos = pci_find_cap_offset(dev->pdev, PCI_CAP_ID_MSIX);
1002 entry_nr = pci_read_word(dev->pdev, pos + 2) & PCI_MSIX_TABSIZE;
1003 pci_dev->config[pci_dev->cap.start + pci_dev->cap.length] = 0x11;
1004 pci_dev->config[pci_dev->cap.start +
1005 pci_dev->cap.length + 2] = entry_nr;
1006 msix_table_entry = pci_read_long(dev->pdev, pos + PCI_MSIX_TABLE);
1007 *(uint32_t *)(pci_dev->config + pci_dev->cap.start +
1008 pci_dev->cap.length + PCI_MSIX_TABLE) = msix_table_entry;
1009 *(uint32_t *)(pci_dev->config + pci_dev->cap.start +
1010 pci_dev->cap.length + PCI_MSIX_PBA) =
1011 pci_read_long(dev->pdev, pos + PCI_MSIX_PBA);
1012 bar_nr = msix_table_entry & PCI_MSIX_BIR;
1013 msix_table_entry &= ~PCI_MSIX_BIR;
1014 dev->msix_table_addr = pci_region[bar_nr].base_addr + msix_table_entry;
1015 if (next_cap_pt != 0) {
1016 pci_dev->config[pci_dev->cap.start + next_cap_pt] =
1017 pci_dev->cap.start + pci_dev->cap.length;
1018 next_cap_pt += PCI_CAPABILITY_CONFIG_MSI_LENGTH;
1019 } else
1020 next_cap_pt = 1;
1021 pci_dev->cap.length += PCI_CAPABILITY_CONFIG_MSIX_LENGTH;
1023 #endif
1024 #endif
1026 return 0;
1029 static uint32_t msix_mmio_readl(void *opaque, target_phys_addr_t addr)
1031 AssignedDevice *adev = opaque;
1032 unsigned int offset = addr & 0xfff;
1033 void *page = adev->msix_table_page;
1034 uint32_t val = 0;
1036 memcpy(&val, (void *)((char *)page + offset), 4);
1038 return val;
1041 static uint32_t msix_mmio_readb(void *opaque, target_phys_addr_t addr)
1043 return ((msix_mmio_readl(opaque, addr & ~3)) >>
1044 (8 * (addr & 3))) & 0xff;
1047 static uint32_t msix_mmio_readw(void *opaque, target_phys_addr_t addr)
1049 return ((msix_mmio_readl(opaque, addr & ~3)) >>
1050 (8 * (addr & 3))) & 0xffff;
1053 static void msix_mmio_writel(void *opaque,
1054 target_phys_addr_t addr, uint32_t val)
1056 AssignedDevice *adev = opaque;
1057 unsigned int offset = addr & 0xfff;
1058 void *page = adev->msix_table_page;
1060 DEBUG("write to MSI-X entry table mmio offset 0x%lx, val 0x%lx\n",
1061 addr, val);
1062 memcpy((void *)((char *)page + offset), &val, 4);
1065 static void msix_mmio_writew(void *opaque,
1066 target_phys_addr_t addr, uint32_t val)
1068 msix_mmio_writel(opaque, addr & ~3,
1069 (val & 0xffff) << (8*(addr & 3)));
1072 static void msix_mmio_writeb(void *opaque,
1073 target_phys_addr_t addr, uint32_t val)
1075 msix_mmio_writel(opaque, addr & ~3,
1076 (val & 0xff) << (8*(addr & 3)));
1079 static CPUWriteMemoryFunc *msix_mmio_write[] = {
1080 msix_mmio_writeb, msix_mmio_writew, msix_mmio_writel
1083 static CPUReadMemoryFunc *msix_mmio_read[] = {
1084 msix_mmio_readb, msix_mmio_readw, msix_mmio_readl
1087 static int assigned_dev_register_msix_mmio(AssignedDevice *dev)
1089 dev->msix_table_page = mmap(NULL, 0x1000,
1090 PROT_READ|PROT_WRITE,
1091 MAP_ANONYMOUS|MAP_PRIVATE, 0, 0);
1092 memset(dev->msix_table_page, 0, 0x1000);
1093 if (dev->msix_table_page == MAP_FAILED) {
1094 fprintf(stderr, "fail allocate msix_table_page! %s\n",
1095 strerror(errno));
1096 return -EFAULT;
1098 dev->mmio_index = cpu_register_io_memory(0,
1099 msix_mmio_read, msix_mmio_write, dev);
1100 return 0;
1103 struct PCIDevice *init_assigned_device(AssignedDevInfo *adev, PCIBus *bus)
1105 int r;
1106 AssignedDevice *dev;
1107 PCIDevice *pci_dev;
1108 struct pci_access *pacc;
1109 uint8_t e_device, e_intx;
1111 DEBUG("Registering real physical device %s (bus=%x dev=%x func=%x)\n",
1112 adev->name, adev->bus, adev->dev, adev->func);
1114 pci_dev = pci_register_device(bus, adev->name,
1115 sizeof(AssignedDevice), -1, assigned_dev_pci_read_config,
1116 assigned_dev_pci_write_config);
1117 dev = container_of(pci_dev, AssignedDevice, dev);
1119 if (NULL == dev) {
1120 fprintf(stderr, "%s: Error: Couldn't register real device %s\n",
1121 __func__, adev->name);
1122 return NULL;
1125 adev->assigned_dev = dev;
1127 if (get_real_device(dev, adev->bus, adev->dev, adev->func)) {
1128 fprintf(stderr, "%s: Error: Couldn't get real device (%s)!\n",
1129 __func__, adev->name);
1130 goto out;
1133 /* handle real device's MMIO/PIO BARs */
1134 if (assigned_dev_register_regions(dev->real_device.regions,
1135 dev->real_device.region_number,
1136 dev))
1137 goto out;
1139 /* handle interrupt routing */
1140 e_device = (dev->dev.devfn >> 3) & 0x1f;
1141 e_intx = dev->dev.config[0x3d] - 1;
1142 dev->intpin = e_intx;
1143 dev->run = 0;
1144 dev->girq = 0;
1145 dev->h_busnr = adev->bus;
1146 dev->h_devfn = PCI_DEVFN(adev->dev, adev->func);
1148 pacc = pci_alloc();
1149 pci_init(pacc);
1150 dev->pdev = pci_get_dev(pacc, 0, adev->bus, adev->dev, adev->func);
1152 if (pci_enable_capability_support(pci_dev, 0, NULL,
1153 assigned_device_pci_cap_write_config,
1154 assigned_device_pci_cap_init) < 0)
1155 goto assigned_out;
1157 /* assign device to guest */
1158 r = assign_device(adev);
1159 if (r < 0)
1160 goto assigned_out;
1162 /* assign irq for the device */
1163 r = assign_irq(adev);
1164 if (r < 0)
1165 goto assigned_out;
1167 /* intercept MSI-X entry page in the MMIO */
1168 if (dev->cap.available & ASSIGNED_DEVICE_CAP_MSIX)
1169 if (assigned_dev_register_msix_mmio(dev))
1170 return NULL;
1172 return &dev->dev;
1174 assigned_out:
1175 deassign_device(adev);
1176 out:
1177 free_assigned_device(adev);
1178 return NULL;
1182 * Syntax to assign device:
1184 * -pcidevice host=bus:dev.func[,dma=none][,name=Foo]
1186 * Example:
1187 * -pcidevice host=00:13.0,dma=pvdma
1189 * dma can currently only be 'none' to disable iommu support.
1191 AssignedDevInfo *add_assigned_device(const char *arg)
1193 char device[16];
1194 char dma[6];
1195 int r;
1196 AssignedDevInfo *adev;
1198 adev = qemu_mallocz(sizeof(AssignedDevInfo));
1199 if (adev == NULL) {
1200 fprintf(stderr, "%s: Out of memory\n", __func__);
1201 return NULL;
1203 r = get_param_value(device, sizeof(device), "host", arg);
1204 if (!r)
1205 goto bad;
1207 r = pci_parse_host_devaddr(device, &adev->bus, &adev->dev, &adev->func);
1208 if (r)
1209 goto bad;
1211 r = get_param_value(adev->name, sizeof(adev->name), "name", arg);
1212 if (!r)
1213 snprintf(adev->name, sizeof(adev->name), "%s", device);
1215 #ifdef KVM_CAP_IOMMU
1216 r = get_param_value(dma, sizeof(dma), "dma", arg);
1217 if (r && !strncmp(dma, "none", 4))
1218 adev->disable_iommu = 1;
1219 #endif
1221 LIST_INSERT_HEAD(&adev_head, adev, next);
1222 return adev;
1223 bad:
1224 fprintf(stderr, "pcidevice argument parse error; "
1225 "please check the help text for usage\n");
1226 qemu_free(adev);
1227 return NULL;
1230 void add_assigned_devices(PCIBus *bus, const char **devices, int n_devices)
1232 int i;
1234 for (i = 0; i < n_devices; i++) {
1235 struct AssignedDevInfo *adev;
1237 adev = add_assigned_device(devices[i]);
1238 if (!adev) {
1239 fprintf(stderr, "Could not add assigned device %s\n", devices[i]);
1240 exit(1);
1243 if (!init_assigned_device(adev, bus)) {
1244 fprintf(stderr, "Failed to initialize assigned device %s\n",
1245 devices[i]);
1246 exit(1);
1251 /* Option ROM header */
1252 struct option_rom_header {
1253 uint8_t signature[2];
1254 uint8_t rom_size;
1255 uint32_t entry_point;
1256 uint8_t reserved[17];
1257 uint16_t pci_header_offset;
1258 uint16_t expansion_header_offset;
1259 } __attribute__ ((packed));
1261 /* Option ROM PCI data structure */
1262 struct option_rom_pci_header {
1263 uint8_t signature[4];
1264 uint16_t vendor_id;
1265 uint16_t device_id;
1266 uint16_t vital_product_data_offset;
1267 uint16_t structure_length;
1268 uint8_t structure_revision;
1269 uint8_t class_code[3];
1270 uint16_t image_length;
1271 uint16_t image_revision;
1272 uint8_t code_type;
1273 uint8_t indicator;
1274 uint16_t reserved;
1275 } __attribute__ ((packed));
1278 * Scan the list of Option ROMs at roms. If a suitable Option ROM is found,
1279 * allocate a ram space and copy it there. Then return its size aligned to
1280 * both 2KB and target page size.
1282 #define OPTION_ROM_ALIGN(x) (((x) + 2047) & ~2047)
1283 static int scan_option_rom(uint8_t devfn, void *roms, ram_addr_t offset)
1285 int i, size, total_size;
1286 uint8_t csum;
1287 ram_addr_t addr;
1288 struct option_rom_header *rom;
1289 struct option_rom_pci_header *pcih;
1291 rom = roms;
1293 for ( ; ; ) {
1294 /* Invalid signature means we're out of option ROMs. */
1295 if (strncmp((char *)rom->signature, "\x55\xaa", 2) ||
1296 (rom->rom_size == 0))
1297 break;
1299 size = rom->rom_size * 512;
1300 /* Invalid checksum means we're out of option ROMs. */
1301 csum = 0;
1302 for (i = 0; i < size; i++)
1303 csum += ((uint8_t *)rom)[i];
1304 if (csum != 0)
1305 break;
1307 /* Check the PCI header (if any) for a match. */
1308 pcih = (struct option_rom_pci_header *)
1309 ((char *)rom + rom->pci_header_offset);
1310 if ((rom->pci_header_offset != 0) &&
1311 !strncmp((char *)pcih->signature, "PCIR", 4))
1312 goto found;
1314 rom = (struct option_rom_header *)((char *)rom + size);
1317 return 0;
1319 found:
1320 /* The size should be both 2K-aligned and page-aligned */
1321 total_size = (TARGET_PAGE_SIZE < 2048)
1322 ? OPTION_ROM_ALIGN(size + 1)
1323 : TARGET_PAGE_ALIGN(size + 1);
1325 /* Size of all available ram space is 0x10000 (0xd0000 to 0xe0000) */
1326 if ((offset + total_size) > 0x10000u) {
1327 fprintf(stderr, "Option ROM size %x exceeds available space\n", size);
1328 return 0;
1331 addr = qemu_ram_alloc(total_size);
1332 cpu_register_physical_memory(0xd0000 + offset, total_size, addr | IO_MEM_ROM);
1334 /* Write ROM data and devfn to phys_addr */
1335 cpu_physical_memory_write_rom(0xd0000 + offset, (uint8_t *)rom, size);
1336 cpu_physical_memory_write_rom(0xd0000 + offset + size, &devfn, 1);
1338 return total_size;
1342 * Scan the assigned devices for the devices that have an option ROM, and then
1343 * load the corresponding ROM data to RAM. If an error occurs while loading an
1344 * option ROM, we just ignore that option ROM and continue with the next one.
1346 ram_addr_t assigned_dev_load_option_roms(ram_addr_t rom_base_offset)
1348 ram_addr_t offset = rom_base_offset;
1349 AssignedDevInfo *adev;
1351 LIST_FOREACH(adev, &adev_head, next) {
1352 int size, len;
1353 void *buf;
1354 FILE *fp;
1355 uint8_t i = 1;
1356 char rom_file[64];
1358 snprintf(rom_file, sizeof(rom_file),
1359 "/sys/bus/pci/devices/0000:%02x:%02x.%01x/rom",
1360 adev->bus, adev->dev, adev->func);
1362 if (access(rom_file, F_OK))
1363 continue;
1365 /* Write something to the ROM file to enable it */
1366 fp = fopen(rom_file, "wb");
1367 if (fp == NULL)
1368 continue;
1369 len = fwrite(&i, 1, 1, fp);
1370 fclose(fp);
1371 if (len != 1)
1372 continue;
1374 /* The file has to be closed and reopened, otherwise it won't work */
1375 fp = fopen(rom_file, "rb");
1376 if (fp == NULL)
1377 continue;
1379 fseek(fp, 0, SEEK_END);
1380 size = ftell(fp);
1381 fseek(fp, 0, SEEK_SET);
1383 buf = malloc(size);
1384 if (buf == NULL) {
1385 fclose(fp);
1386 continue;
1389 fread(buf, size, 1, fp);
1390 if (!feof(fp) || ferror(fp)) {
1391 free(buf);
1392 fclose(fp);
1393 continue;
1396 /* Scan the buffer for suitable ROMs and increase the offset */
1397 offset += scan_option_rom(adev->assigned_dev->dev.devfn, buf, offset);
1399 free(buf);
1400 fclose(fp);
1403 return offset;