]>
Commit | Line | Data |
---|---|---|
eaab4d60 AK |
1 | /* |
2 | * Copyright (c) 2007, Neocleus Corporation. | |
3 | * Copyright (c) 2007, Intel Corporation. | |
4 | * | |
5 | * This work is licensed under the terms of the GNU GPL, version 2. See | |
6 | * the COPYING file in the top-level directory. | |
7 | * | |
8 | * Alex Novik <[email protected]> | |
9 | * Allen Kay <[email protected]> | |
10 | * Guy Zana <[email protected]> | |
11 | * | |
12 | * This file implements direct PCI assignment to a HVM guest | |
13 | */ | |
14 | ||
15 | /* | |
16 | * Interrupt Disable policy: | |
17 | * | |
18 | * INTx interrupt: | |
19 | * Initialize(register_real_device) | |
20 | * Map INTx(xc_physdev_map_pirq): | |
21 | * <fail> | |
22 | * - Set real Interrupt Disable bit to '1'. | |
23 | * - Set machine_irq and assigned_device->machine_irq to '0'. | |
24 | * * Don't bind INTx. | |
25 | * | |
26 | * Bind INTx(xc_domain_bind_pt_pci_irq): | |
27 | * <fail> | |
28 | * - Set real Interrupt Disable bit to '1'. | |
29 | * - Unmap INTx. | |
30 | * - Decrement xen_pt_mapped_machine_irq[machine_irq] | |
31 | * - Set assigned_device->machine_irq to '0'. | |
32 | * | |
33 | * Write to Interrupt Disable bit by guest software(xen_pt_cmd_reg_write) | |
34 | * Write '0' | |
35 | * - Set real bit to '0' if assigned_device->machine_irq isn't '0'. | |
36 | * | |
37 | * Write '1' | |
38 | * - Set real bit to '1'. | |
3854ca57 JY |
39 | * |
40 | * MSI interrupt: | |
41 | * Initialize MSI register(xen_pt_msi_setup, xen_pt_msi_update) | |
42 | * Bind MSI(xc_domain_update_msi_irq) | |
43 | * <fail> | |
44 | * - Unmap MSI. | |
45 | * - Set dev->msi->pirq to '-1'. | |
46 | * | |
47 | * MSI-X interrupt: | |
48 | * Initialize MSI-X register(xen_pt_msix_update_one) | |
49 | * Bind MSI-X(xc_domain_update_msi_irq) | |
50 | * <fail> | |
51 | * - Unmap MSI-X. | |
52 | * - Set entry->pirq to '-1'. | |
eaab4d60 AK |
53 | */ |
54 | ||
55 | #include <sys/ioctl.h> | |
56 | ||
83c9f4ca | 57 | #include "hw/pci/pci.h" |
0d09e41a PB |
58 | #include "hw/xen/xen.h" |
59 | #include "hw/xen/xen_backend.h" | |
47b43a1f | 60 | #include "xen_pt.h" |
1de7afc9 | 61 | #include "qemu/range.h" |
022c62cb | 62 | #include "exec/address-spaces.h" |
eaab4d60 AK |
63 | |
64 | #define XEN_PT_NR_IRQS (256) | |
65 | static uint8_t xen_pt_mapped_machine_irq[XEN_PT_NR_IRQS] = {0}; | |
66 | ||
67 | void xen_pt_log(const PCIDevice *d, const char *f, ...) | |
68 | { | |
69 | va_list ap; | |
70 | ||
71 | va_start(ap, f); | |
72 | if (d) { | |
73 | fprintf(stderr, "[%02x:%02x.%d] ", pci_bus_num(d->bus), | |
74 | PCI_SLOT(d->devfn), PCI_FUNC(d->devfn)); | |
75 | } | |
76 | vfprintf(stderr, f, ap); | |
77 | va_end(ap); | |
78 | } | |
79 | ||
80 | /* Config Space */ | |
81 | ||
82 | static int xen_pt_pci_config_access_check(PCIDevice *d, uint32_t addr, int len) | |
83 | { | |
84 | /* check offset range */ | |
85 | if (addr >= 0xFF) { | |
86 | XEN_PT_ERR(d, "Failed to access register with offset exceeding 0xFF. " | |
87 | "(addr: 0x%02x, len: %d)\n", addr, len); | |
88 | return -1; | |
89 | } | |
90 | ||
91 | /* check read size */ | |
92 | if ((len != 1) && (len != 2) && (len != 4)) { | |
93 | XEN_PT_ERR(d, "Failed to access register with invalid access length. " | |
94 | "(addr: 0x%02x, len: %d)\n", addr, len); | |
95 | return -1; | |
96 | } | |
97 | ||
98 | /* check offset alignment */ | |
99 | if (addr & (len - 1)) { | |
100 | XEN_PT_ERR(d, "Failed to access register with invalid access size " | |
101 | "alignment. (addr: 0x%02x, len: %d)\n", addr, len); | |
102 | return -1; | |
103 | } | |
104 | ||
105 | return 0; | |
106 | } | |
107 | ||
108 | int xen_pt_bar_offset_to_index(uint32_t offset) | |
109 | { | |
110 | int index = 0; | |
111 | ||
112 | /* check Exp ROM BAR */ | |
113 | if (offset == PCI_ROM_ADDRESS) { | |
114 | return PCI_ROM_SLOT; | |
115 | } | |
116 | ||
117 | /* calculate BAR index */ | |
118 | index = (offset - PCI_BASE_ADDRESS_0) >> 2; | |
119 | if (index >= PCI_NUM_REGIONS) { | |
120 | return -1; | |
121 | } | |
122 | ||
123 | return index; | |
124 | } | |
125 | ||
126 | static uint32_t xen_pt_pci_read_config(PCIDevice *d, uint32_t addr, int len) | |
127 | { | |
128 | XenPCIPassthroughState *s = DO_UPCAST(XenPCIPassthroughState, dev, d); | |
129 | uint32_t val = 0; | |
130 | XenPTRegGroup *reg_grp_entry = NULL; | |
131 | XenPTReg *reg_entry = NULL; | |
132 | int rc = 0; | |
133 | int emul_len = 0; | |
134 | uint32_t find_addr = addr; | |
135 | ||
136 | if (xen_pt_pci_config_access_check(d, addr, len)) { | |
137 | goto exit; | |
138 | } | |
139 | ||
140 | /* find register group entry */ | |
141 | reg_grp_entry = xen_pt_find_reg_grp(s, addr); | |
142 | if (reg_grp_entry) { | |
143 | /* check 0-Hardwired register group */ | |
144 | if (reg_grp_entry->reg_grp->grp_type == XEN_PT_GRP_TYPE_HARDWIRED) { | |
145 | /* no need to emulate, just return 0 */ | |
146 | val = 0; | |
147 | goto exit; | |
148 | } | |
149 | } | |
150 | ||
151 | /* read I/O device register value */ | |
152 | rc = xen_host_pci_get_block(&s->real_device, addr, (uint8_t *)&val, len); | |
153 | if (rc < 0) { | |
154 | XEN_PT_ERR(d, "pci_read_block failed. return value: %d.\n", rc); | |
155 | memset(&val, 0xff, len); | |
156 | } | |
157 | ||
158 | /* just return the I/O device register value for | |
159 | * passthrough type register group */ | |
160 | if (reg_grp_entry == NULL) { | |
161 | goto exit; | |
162 | } | |
163 | ||
164 | /* adjust the read value to appropriate CFC-CFF window */ | |
165 | val <<= (addr & 3) << 3; | |
166 | emul_len = len; | |
167 | ||
168 | /* loop around the guest requested size */ | |
169 | while (emul_len > 0) { | |
170 | /* find register entry to be emulated */ | |
171 | reg_entry = xen_pt_find_reg(reg_grp_entry, find_addr); | |
172 | if (reg_entry) { | |
173 | XenPTRegInfo *reg = reg_entry->reg; | |
174 | uint32_t real_offset = reg_grp_entry->base_offset + reg->offset; | |
175 | uint32_t valid_mask = 0xFFFFFFFF >> ((4 - emul_len) << 3); | |
176 | uint8_t *ptr_val = NULL; | |
177 | ||
178 | valid_mask <<= (find_addr - real_offset) << 3; | |
179 | ptr_val = (uint8_t *)&val + (real_offset & 3); | |
180 | ||
181 | /* do emulation based on register size */ | |
182 | switch (reg->size) { | |
183 | case 1: | |
184 | if (reg->u.b.read) { | |
185 | rc = reg->u.b.read(s, reg_entry, ptr_val, valid_mask); | |
186 | } | |
187 | break; | |
188 | case 2: | |
189 | if (reg->u.w.read) { | |
190 | rc = reg->u.w.read(s, reg_entry, | |
191 | (uint16_t *)ptr_val, valid_mask); | |
192 | } | |
193 | break; | |
194 | case 4: | |
195 | if (reg->u.dw.read) { | |
196 | rc = reg->u.dw.read(s, reg_entry, | |
197 | (uint32_t *)ptr_val, valid_mask); | |
198 | } | |
199 | break; | |
200 | } | |
201 | ||
202 | if (rc < 0) { | |
203 | xen_shutdown_fatal_error("Internal error: Invalid read " | |
204 | "emulation. (%s, rc: %d)\n", | |
205 | __func__, rc); | |
206 | return 0; | |
207 | } | |
208 | ||
209 | /* calculate next address to find */ | |
210 | emul_len -= reg->size; | |
211 | if (emul_len > 0) { | |
212 | find_addr = real_offset + reg->size; | |
213 | } | |
214 | } else { | |
215 | /* nothing to do with passthrough type register, | |
216 | * continue to find next byte */ | |
217 | emul_len--; | |
218 | find_addr++; | |
219 | } | |
220 | } | |
221 | ||
222 | /* need to shift back before returning them to pci bus emulator */ | |
223 | val >>= ((addr & 3) << 3); | |
224 | ||
225 | exit: | |
226 | XEN_PT_LOG_CONFIG(d, addr, val, len); | |
227 | return val; | |
228 | } | |
229 | ||
230 | static void xen_pt_pci_write_config(PCIDevice *d, uint32_t addr, | |
231 | uint32_t val, int len) | |
232 | { | |
233 | XenPCIPassthroughState *s = DO_UPCAST(XenPCIPassthroughState, dev, d); | |
234 | int index = 0; | |
235 | XenPTRegGroup *reg_grp_entry = NULL; | |
236 | int rc = 0; | |
237 | uint32_t read_val = 0; | |
238 | int emul_len = 0; | |
239 | XenPTReg *reg_entry = NULL; | |
240 | uint32_t find_addr = addr; | |
241 | XenPTRegInfo *reg = NULL; | |
242 | ||
243 | if (xen_pt_pci_config_access_check(d, addr, len)) { | |
244 | return; | |
245 | } | |
246 | ||
247 | XEN_PT_LOG_CONFIG(d, addr, val, len); | |
248 | ||
249 | /* check unused BAR register */ | |
250 | index = xen_pt_bar_offset_to_index(addr); | |
251 | if ((index >= 0) && (val > 0 && val < XEN_PT_BAR_ALLF) && | |
252 | (s->bases[index].bar_flag == XEN_PT_BAR_FLAG_UNUSED)) { | |
253 | XEN_PT_WARN(d, "Guest attempt to set address to unused Base Address " | |
254 | "Register. (addr: 0x%02x, len: %d)\n", addr, len); | |
255 | } | |
256 | ||
257 | /* find register group entry */ | |
258 | reg_grp_entry = xen_pt_find_reg_grp(s, addr); | |
259 | if (reg_grp_entry) { | |
260 | /* check 0-Hardwired register group */ | |
261 | if (reg_grp_entry->reg_grp->grp_type == XEN_PT_GRP_TYPE_HARDWIRED) { | |
262 | /* ignore silently */ | |
263 | XEN_PT_WARN(d, "Access to 0-Hardwired register. " | |
264 | "(addr: 0x%02x, len: %d)\n", addr, len); | |
265 | return; | |
266 | } | |
267 | } | |
268 | ||
269 | rc = xen_host_pci_get_block(&s->real_device, addr, | |
270 | (uint8_t *)&read_val, len); | |
271 | if (rc < 0) { | |
272 | XEN_PT_ERR(d, "pci_read_block failed. return value: %d.\n", rc); | |
273 | memset(&read_val, 0xff, len); | |
274 | } | |
275 | ||
276 | /* pass directly to the real device for passthrough type register group */ | |
277 | if (reg_grp_entry == NULL) { | |
278 | goto out; | |
279 | } | |
280 | ||
281 | memory_region_transaction_begin(); | |
282 | pci_default_write_config(d, addr, val, len); | |
283 | ||
284 | /* adjust the read and write value to appropriate CFC-CFF window */ | |
285 | read_val <<= (addr & 3) << 3; | |
286 | val <<= (addr & 3) << 3; | |
287 | emul_len = len; | |
288 | ||
289 | /* loop around the guest requested size */ | |
290 | while (emul_len > 0) { | |
291 | /* find register entry to be emulated */ | |
292 | reg_entry = xen_pt_find_reg(reg_grp_entry, find_addr); | |
293 | if (reg_entry) { | |
294 | reg = reg_entry->reg; | |
295 | uint32_t real_offset = reg_grp_entry->base_offset + reg->offset; | |
296 | uint32_t valid_mask = 0xFFFFFFFF >> ((4 - emul_len) << 3); | |
297 | uint8_t *ptr_val = NULL; | |
298 | ||
299 | valid_mask <<= (find_addr - real_offset) << 3; | |
300 | ptr_val = (uint8_t *)&val + (real_offset & 3); | |
301 | ||
302 | /* do emulation based on register size */ | |
303 | switch (reg->size) { | |
304 | case 1: | |
305 | if (reg->u.b.write) { | |
306 | rc = reg->u.b.write(s, reg_entry, ptr_val, | |
307 | read_val >> ((real_offset & 3) << 3), | |
308 | valid_mask); | |
309 | } | |
310 | break; | |
311 | case 2: | |
312 | if (reg->u.w.write) { | |
313 | rc = reg->u.w.write(s, reg_entry, (uint16_t *)ptr_val, | |
314 | (read_val >> ((real_offset & 3) << 3)), | |
315 | valid_mask); | |
316 | } | |
317 | break; | |
318 | case 4: | |
319 | if (reg->u.dw.write) { | |
320 | rc = reg->u.dw.write(s, reg_entry, (uint32_t *)ptr_val, | |
321 | (read_val >> ((real_offset & 3) << 3)), | |
322 | valid_mask); | |
323 | } | |
324 | break; | |
325 | } | |
326 | ||
327 | if (rc < 0) { | |
328 | xen_shutdown_fatal_error("Internal error: Invalid write" | |
329 | " emulation. (%s, rc: %d)\n", | |
330 | __func__, rc); | |
331 | return; | |
332 | } | |
333 | ||
334 | /* calculate next address to find */ | |
335 | emul_len -= reg->size; | |
336 | if (emul_len > 0) { | |
337 | find_addr = real_offset + reg->size; | |
338 | } | |
339 | } else { | |
340 | /* nothing to do with passthrough type register, | |
341 | * continue to find next byte */ | |
342 | emul_len--; | |
343 | find_addr++; | |
344 | } | |
345 | } | |
346 | ||
347 | /* need to shift back before passing them to xen_host_pci_device */ | |
348 | val >>= (addr & 3) << 3; | |
349 | ||
350 | memory_region_transaction_commit(); | |
351 | ||
352 | out: | |
353 | if (!(reg && reg->no_wb)) { | |
354 | /* unknown regs are passed through */ | |
355 | rc = xen_host_pci_set_block(&s->real_device, addr, | |
356 | (uint8_t *)&val, len); | |
357 | ||
358 | if (rc < 0) { | |
359 | XEN_PT_ERR(d, "pci_write_block failed. return value: %d.\n", rc); | |
360 | } | |
361 | } | |
362 | } | |
363 | ||
364 | /* register regions */ | |
365 | ||
a8170e5e | 366 | static uint64_t xen_pt_bar_read(void *o, hwaddr addr, |
eaab4d60 AK |
367 | unsigned size) |
368 | { | |
369 | PCIDevice *d = o; | |
370 | /* if this function is called, that probably means that there is a | |
371 | * misconfiguration of the IOMMU. */ | |
372 | XEN_PT_ERR(d, "Should not read BAR through QEMU. @0x"TARGET_FMT_plx"\n", | |
373 | addr); | |
374 | return 0; | |
375 | } | |
a8170e5e | 376 | static void xen_pt_bar_write(void *o, hwaddr addr, uint64_t val, |
eaab4d60 AK |
377 | unsigned size) |
378 | { | |
379 | PCIDevice *d = o; | |
380 | /* Same comment as xen_pt_bar_read function */ | |
381 | XEN_PT_ERR(d, "Should not write BAR through QEMU. @0x"TARGET_FMT_plx"\n", | |
382 | addr); | |
383 | } | |
384 | ||
385 | static const MemoryRegionOps ops = { | |
386 | .endianness = DEVICE_NATIVE_ENDIAN, | |
387 | .read = xen_pt_bar_read, | |
388 | .write = xen_pt_bar_write, | |
389 | }; | |
390 | ||
81b23ef8 | 391 | static int xen_pt_register_regions(XenPCIPassthroughState *s, uint16_t *cmd) |
eaab4d60 AK |
392 | { |
393 | int i = 0; | |
394 | XenHostPCIDevice *d = &s->real_device; | |
395 | ||
396 | /* Register PIO/MMIO BARs */ | |
397 | for (i = 0; i < PCI_ROM_SLOT; i++) { | |
398 | XenHostPCIIORegion *r = &d->io_regions[i]; | |
399 | uint8_t type; | |
400 | ||
401 | if (r->base_addr == 0 || r->size == 0) { | |
402 | continue; | |
403 | } | |
404 | ||
405 | s->bases[i].access.u = r->base_addr; | |
406 | ||
407 | if (r->type & XEN_HOST_PCI_REGION_TYPE_IO) { | |
408 | type = PCI_BASE_ADDRESS_SPACE_IO; | |
81b23ef8 | 409 | *cmd |= PCI_COMMAND_IO; |
eaab4d60 AK |
410 | } else { |
411 | type = PCI_BASE_ADDRESS_SPACE_MEMORY; | |
412 | if (r->type & XEN_HOST_PCI_REGION_TYPE_PREFETCH) { | |
413 | type |= PCI_BASE_ADDRESS_MEM_PREFETCH; | |
414 | } | |
aabc8530 XH |
415 | if (r->type & XEN_HOST_PCI_REGION_TYPE_MEM_64) { |
416 | type |= PCI_BASE_ADDRESS_MEM_TYPE_64; | |
417 | } | |
81b23ef8 | 418 | *cmd |= PCI_COMMAND_MEMORY; |
eaab4d60 AK |
419 | } |
420 | ||
22fc860b | 421 | memory_region_init_io(&s->bar[i], OBJECT(s), &ops, &s->dev, |
eaab4d60 AK |
422 | "xen-pci-pt-bar", r->size); |
423 | pci_register_bar(&s->dev, i, type, &s->bar[i]); | |
424 | ||
fc33b900 AP |
425 | XEN_PT_LOG(&s->dev, "IO region %i registered (size=0x%08"PRIx64 |
426 | " base_addr=0x%08"PRIx64" type: %#x)\n", | |
eaab4d60 AK |
427 | i, r->size, r->base_addr, type); |
428 | } | |
429 | ||
430 | /* Register expansion ROM address */ | |
431 | if (d->rom.base_addr && d->rom.size) { | |
432 | uint32_t bar_data = 0; | |
433 | ||
434 | /* Re-set BAR reported by OS, otherwise ROM can't be read. */ | |
435 | if (xen_host_pci_get_long(d, PCI_ROM_ADDRESS, &bar_data)) { | |
436 | return 0; | |
437 | } | |
438 | if ((bar_data & PCI_ROM_ADDRESS_MASK) == 0) { | |
439 | bar_data |= d->rom.base_addr & PCI_ROM_ADDRESS_MASK; | |
440 | xen_host_pci_set_long(d, PCI_ROM_ADDRESS, bar_data); | |
441 | } | |
442 | ||
443 | s->bases[PCI_ROM_SLOT].access.maddr = d->rom.base_addr; | |
444 | ||
794798e3 AP |
445 | memory_region_init_io(&s->rom, OBJECT(s), &ops, &s->dev, |
446 | "xen-pci-pt-rom", d->rom.size); | |
eaab4d60 AK |
447 | pci_register_bar(&s->dev, PCI_ROM_SLOT, PCI_BASE_ADDRESS_MEM_PREFETCH, |
448 | &s->rom); | |
449 | ||
450 | XEN_PT_LOG(&s->dev, "Expansion ROM registered (size=0x%08"PRIx64 | |
451 | " base_addr=0x%08"PRIx64")\n", | |
452 | d->rom.size, d->rom.base_addr); | |
453 | } | |
454 | ||
455 | return 0; | |
456 | } | |
457 | ||
eaab4d60 AK |
458 | /* region mapping */ |
459 | ||
460 | static int xen_pt_bar_from_region(XenPCIPassthroughState *s, MemoryRegion *mr) | |
461 | { | |
462 | int i = 0; | |
463 | ||
464 | for (i = 0; i < PCI_NUM_REGIONS - 1; i++) { | |
465 | if (mr == &s->bar[i]) { | |
466 | return i; | |
467 | } | |
468 | } | |
469 | if (mr == &s->rom) { | |
470 | return PCI_ROM_SLOT; | |
471 | } | |
472 | return -1; | |
473 | } | |
474 | ||
475 | /* | |
476 | * This function checks if an io_region overlaps an io_region from another | |
477 | * device. The io_region to check is provided with (addr, size and type) | |
478 | * A callback can be provided and will be called for every region that is | |
479 | * overlapped. | |
480 | * The return value indicates if the region is overlappsed */ | |
481 | struct CheckBarArgs { | |
482 | XenPCIPassthroughState *s; | |
483 | pcibus_t addr; | |
484 | pcibus_t size; | |
485 | uint8_t type; | |
486 | bool rc; | |
487 | }; | |
488 | static void xen_pt_check_bar_overlap(PCIBus *bus, PCIDevice *d, void *opaque) | |
489 | { | |
490 | struct CheckBarArgs *arg = opaque; | |
491 | XenPCIPassthroughState *s = arg->s; | |
492 | uint8_t type = arg->type; | |
493 | int i; | |
494 | ||
495 | if (d->devfn == s->dev.devfn) { | |
496 | return; | |
497 | } | |
498 | ||
499 | /* xxx: This ignores bridges. */ | |
500 | for (i = 0; i < PCI_NUM_REGIONS; i++) { | |
501 | const PCIIORegion *r = &d->io_regions[i]; | |
502 | ||
503 | if (!r->size) { | |
504 | continue; | |
505 | } | |
506 | if ((type & PCI_BASE_ADDRESS_SPACE_IO) | |
507 | != (r->type & PCI_BASE_ADDRESS_SPACE_IO)) { | |
508 | continue; | |
509 | } | |
510 | ||
511 | if (ranges_overlap(arg->addr, arg->size, r->addr, r->size)) { | |
512 | XEN_PT_WARN(&s->dev, | |
513 | "Overlapped to device [%02x:%02x.%d] Region: %i" | |
514 | " (addr: %#"FMT_PCIBUS", len: %#"FMT_PCIBUS")\n", | |
515 | pci_bus_num(bus), PCI_SLOT(d->devfn), | |
516 | PCI_FUNC(d->devfn), i, r->addr, r->size); | |
517 | arg->rc = true; | |
518 | } | |
519 | } | |
520 | } | |
521 | ||
522 | static void xen_pt_region_update(XenPCIPassthroughState *s, | |
523 | MemoryRegionSection *sec, bool adding) | |
524 | { | |
525 | PCIDevice *d = &s->dev; | |
526 | MemoryRegion *mr = sec->mr; | |
527 | int bar = -1; | |
528 | int rc; | |
529 | int op = adding ? DPCI_ADD_MAPPING : DPCI_REMOVE_MAPPING; | |
530 | struct CheckBarArgs args = { | |
531 | .s = s, | |
532 | .addr = sec->offset_within_address_space, | |
052e87b0 | 533 | .size = int128_get64(sec->size), |
eaab4d60 AK |
534 | .rc = false, |
535 | }; | |
536 | ||
537 | bar = xen_pt_bar_from_region(s, mr); | |
3854ca57 JY |
538 | if (bar == -1 && (!s->msix || &s->msix->mmio != mr)) { |
539 | return; | |
540 | } | |
541 | ||
542 | if (s->msix && &s->msix->mmio == mr) { | |
543 | if (adding) { | |
544 | s->msix->mmio_base_addr = sec->offset_within_address_space; | |
545 | rc = xen_pt_msix_update_remap(s, s->msix->bar_index); | |
546 | } | |
eaab4d60 AK |
547 | return; |
548 | } | |
549 | ||
550 | args.type = d->io_regions[bar].type; | |
551 | pci_for_each_device(d->bus, pci_bus_num(d->bus), | |
552 | xen_pt_check_bar_overlap, &args); | |
553 | if (args.rc) { | |
554 | XEN_PT_WARN(d, "Region: %d (addr: %#"FMT_PCIBUS | |
555 | ", len: %#"FMT_PCIBUS") is overlapped.\n", | |
d18e173a WL |
556 | bar, sec->offset_within_address_space, |
557 | int128_get64(sec->size)); | |
eaab4d60 AK |
558 | } |
559 | ||
560 | if (d->io_regions[bar].type & PCI_BASE_ADDRESS_SPACE_IO) { | |
561 | uint32_t guest_port = sec->offset_within_address_space; | |
562 | uint32_t machine_port = s->bases[bar].access.pio_base; | |
052e87b0 | 563 | uint32_t size = int128_get64(sec->size); |
eaab4d60 AK |
564 | rc = xc_domain_ioport_mapping(xen_xc, xen_domid, |
565 | guest_port, machine_port, size, | |
566 | op); | |
567 | if (rc) { | |
568 | XEN_PT_ERR(d, "%s ioport mapping failed! (rc: %i)\n", | |
569 | adding ? "create new" : "remove old", rc); | |
570 | } | |
571 | } else { | |
572 | pcibus_t guest_addr = sec->offset_within_address_space; | |
573 | pcibus_t machine_addr = s->bases[bar].access.maddr | |
574 | + sec->offset_within_region; | |
052e87b0 | 575 | pcibus_t size = int128_get64(sec->size); |
eaab4d60 AK |
576 | rc = xc_domain_memory_mapping(xen_xc, xen_domid, |
577 | XEN_PFN(guest_addr + XC_PAGE_SIZE - 1), | |
578 | XEN_PFN(machine_addr + XC_PAGE_SIZE - 1), | |
579 | XEN_PFN(size + XC_PAGE_SIZE - 1), | |
580 | op); | |
581 | if (rc) { | |
582 | XEN_PT_ERR(d, "%s mem mapping failed! (rc: %i)\n", | |
583 | adding ? "create new" : "remove old", rc); | |
584 | } | |
585 | } | |
586 | } | |
587 | ||
eaab4d60 AK |
588 | static void xen_pt_region_add(MemoryListener *l, MemoryRegionSection *sec) |
589 | { | |
590 | XenPCIPassthroughState *s = container_of(l, XenPCIPassthroughState, | |
591 | memory_listener); | |
592 | ||
dfde4e6e | 593 | memory_region_ref(sec->mr); |
eaab4d60 AK |
594 | xen_pt_region_update(s, sec, true); |
595 | } | |
596 | ||
597 | static void xen_pt_region_del(MemoryListener *l, MemoryRegionSection *sec) | |
598 | { | |
599 | XenPCIPassthroughState *s = container_of(l, XenPCIPassthroughState, | |
600 | memory_listener); | |
601 | ||
602 | xen_pt_region_update(s, sec, false); | |
dfde4e6e | 603 | memory_region_unref(sec->mr); |
eaab4d60 AK |
604 | } |
605 | ||
12b40e47 AK |
606 | static void xen_pt_io_region_add(MemoryListener *l, MemoryRegionSection *sec) |
607 | { | |
608 | XenPCIPassthroughState *s = container_of(l, XenPCIPassthroughState, | |
609 | io_listener); | |
610 | ||
dfde4e6e | 611 | memory_region_ref(sec->mr); |
12b40e47 AK |
612 | xen_pt_region_update(s, sec, true); |
613 | } | |
614 | ||
615 | static void xen_pt_io_region_del(MemoryListener *l, MemoryRegionSection *sec) | |
616 | { | |
617 | XenPCIPassthroughState *s = container_of(l, XenPCIPassthroughState, | |
618 | io_listener); | |
619 | ||
620 | xen_pt_region_update(s, sec, false); | |
dfde4e6e | 621 | memory_region_unref(sec->mr); |
12b40e47 AK |
622 | } |
623 | ||
eaab4d60 | 624 | static const MemoryListener xen_pt_memory_listener = { |
eaab4d60 | 625 | .region_add = xen_pt_region_add, |
eaab4d60 | 626 | .region_del = xen_pt_region_del, |
eaab4d60 AK |
627 | .priority = 10, |
628 | }; | |
629 | ||
12b40e47 | 630 | static const MemoryListener xen_pt_io_listener = { |
12b40e47 | 631 | .region_add = xen_pt_io_region_add, |
12b40e47 | 632 | .region_del = xen_pt_io_region_del, |
12b40e47 AK |
633 | .priority = 10, |
634 | }; | |
635 | ||
eaab4d60 AK |
636 | /* init */ |
637 | ||
638 | static int xen_pt_initfn(PCIDevice *d) | |
639 | { | |
640 | XenPCIPassthroughState *s = DO_UPCAST(XenPCIPassthroughState, dev, d); | |
641 | int rc = 0; | |
642 | uint8_t machine_irq = 0; | |
81b23ef8 | 643 | uint16_t cmd = 0; |
eaab4d60 AK |
644 | int pirq = XEN_PT_UNASSIGNED_PIRQ; |
645 | ||
646 | /* register real device */ | |
647 | XEN_PT_LOG(d, "Assigning real physical device %02x:%02x.%d" | |
648 | " to devfn %#x\n", | |
649 | s->hostaddr.bus, s->hostaddr.slot, s->hostaddr.function, | |
650 | s->dev.devfn); | |
651 | ||
652 | rc = xen_host_pci_device_get(&s->real_device, | |
653 | s->hostaddr.domain, s->hostaddr.bus, | |
654 | s->hostaddr.slot, s->hostaddr.function); | |
655 | if (rc) { | |
656 | XEN_PT_ERR(d, "Failed to \"open\" the real pci device. rc: %i\n", rc); | |
657 | return -1; | |
658 | } | |
659 | ||
660 | s->is_virtfn = s->real_device.is_virtfn; | |
661 | if (s->is_virtfn) { | |
662 | XEN_PT_LOG(d, "%04x:%02x:%02x.%d is a SR-IOV Virtual Function\n", | |
f1b8caf1 SE |
663 | s->real_device.domain, s->real_device.bus, |
664 | s->real_device.dev, s->real_device.func); | |
eaab4d60 AK |
665 | } |
666 | ||
667 | /* Initialize virtualized PCI configuration (Extended 256 Bytes) */ | |
668 | if (xen_host_pci_get_block(&s->real_device, 0, d->config, | |
669 | PCI_CONFIG_SPACE_SIZE) == -1) { | |
670 | xen_host_pci_device_put(&s->real_device); | |
671 | return -1; | |
672 | } | |
673 | ||
674 | s->memory_listener = xen_pt_memory_listener; | |
12b40e47 | 675 | s->io_listener = xen_pt_io_listener; |
eaab4d60 AK |
676 | |
677 | /* Handle real device's MMIO/PIO BARs */ | |
81b23ef8 | 678 | xen_pt_register_regions(s, &cmd); |
eaab4d60 | 679 | |
93d7ae8e AK |
680 | /* reinitialize each config register to be emulated */ |
681 | if (xen_pt_config_init(s)) { | |
682 | XEN_PT_ERR(d, "PCI Config space initialisation failed.\n"); | |
683 | xen_host_pci_device_put(&s->real_device); | |
684 | return -1; | |
685 | } | |
686 | ||
eaab4d60 AK |
687 | /* Bind interrupt */ |
688 | if (!s->dev.config[PCI_INTERRUPT_PIN]) { | |
689 | XEN_PT_LOG(d, "no pin interrupt\n"); | |
690 | goto out; | |
691 | } | |
692 | ||
693 | machine_irq = s->real_device.irq; | |
694 | rc = xc_physdev_map_pirq(xen_xc, xen_domid, machine_irq, &pirq); | |
695 | ||
696 | if (rc < 0) { | |
697 | XEN_PT_ERR(d, "Mapping machine irq %u to pirq %i failed, (rc: %d)\n", | |
698 | machine_irq, pirq, rc); | |
699 | ||
700 | /* Disable PCI intx assertion (turn on bit10 of devctl) */ | |
701 | xen_host_pci_set_word(&s->real_device, | |
702 | PCI_COMMAND, | |
703 | pci_get_word(s->dev.config + PCI_COMMAND) | |
704 | | PCI_COMMAND_INTX_DISABLE); | |
705 | machine_irq = 0; | |
706 | s->machine_irq = 0; | |
707 | } else { | |
708 | machine_irq = pirq; | |
709 | s->machine_irq = pirq; | |
710 | xen_pt_mapped_machine_irq[machine_irq]++; | |
711 | } | |
712 | ||
713 | /* bind machine_irq to device */ | |
714 | if (machine_irq != 0) { | |
715 | uint8_t e_intx = xen_pt_pci_intx(s); | |
716 | ||
717 | rc = xc_domain_bind_pt_pci_irq(xen_xc, xen_domid, machine_irq, | |
718 | pci_bus_num(d->bus), | |
719 | PCI_SLOT(d->devfn), | |
720 | e_intx); | |
721 | if (rc < 0) { | |
722 | XEN_PT_ERR(d, "Binding of interrupt %i failed! (rc: %d)\n", | |
723 | e_intx, rc); | |
724 | ||
725 | /* Disable PCI intx assertion (turn on bit10 of devctl) */ | |
726 | xen_host_pci_set_word(&s->real_device, PCI_COMMAND, | |
727 | *(uint16_t *)(&s->dev.config[PCI_COMMAND]) | |
728 | | PCI_COMMAND_INTX_DISABLE); | |
729 | xen_pt_mapped_machine_irq[machine_irq]--; | |
730 | ||
731 | if (xen_pt_mapped_machine_irq[machine_irq] == 0) { | |
732 | if (xc_physdev_unmap_pirq(xen_xc, xen_domid, machine_irq)) { | |
733 | XEN_PT_ERR(d, "Unmapping of machine interrupt %i failed!" | |
734 | " (rc: %d)\n", machine_irq, rc); | |
735 | } | |
736 | } | |
737 | s->machine_irq = 0; | |
738 | } | |
739 | } | |
740 | ||
741 | out: | |
81b23ef8 JB |
742 | if (cmd) { |
743 | xen_host_pci_set_word(&s->real_device, PCI_COMMAND, | |
744 | pci_get_word(d->config + PCI_COMMAND) | cmd); | |
745 | } | |
746 | ||
99605175 | 747 | memory_listener_register(&s->memory_listener, &s->dev.bus_master_as); |
f6790af6 | 748 | memory_listener_register(&s->io_listener, &address_space_io); |
52f35022 SW |
749 | XEN_PT_LOG(d, |
750 | "Real physical device %02x:%02x.%d registered successfully!\n", | |
f1b8caf1 | 751 | s->hostaddr.bus, s->hostaddr.slot, s->hostaddr.function); |
eaab4d60 AK |
752 | |
753 | return 0; | |
754 | } | |
755 | ||
fb5b0c6d | 756 | static void xen_pt_unregister_device(PCIDevice *d) |
eaab4d60 AK |
757 | { |
758 | XenPCIPassthroughState *s = DO_UPCAST(XenPCIPassthroughState, dev, d); | |
759 | uint8_t machine_irq = s->machine_irq; | |
760 | uint8_t intx = xen_pt_pci_intx(s); | |
761 | int rc; | |
762 | ||
763 | if (machine_irq) { | |
764 | rc = xc_domain_unbind_pt_irq(xen_xc, xen_domid, machine_irq, | |
765 | PT_IRQ_TYPE_PCI, | |
766 | pci_bus_num(d->bus), | |
767 | PCI_SLOT(s->dev.devfn), | |
768 | intx, | |
769 | 0 /* isa_irq */); | |
770 | if (rc < 0) { | |
771 | XEN_PT_ERR(d, "unbinding of interrupt INT%c failed." | |
772 | " (machine irq: %i, rc: %d)" | |
773 | " But bravely continuing on..\n", | |
774 | 'a' + intx, machine_irq, rc); | |
775 | } | |
776 | } | |
777 | ||
3854ca57 JY |
778 | if (s->msi) { |
779 | xen_pt_msi_disable(s); | |
780 | } | |
781 | if (s->msix) { | |
782 | xen_pt_msix_disable(s); | |
783 | } | |
784 | ||
eaab4d60 AK |
785 | if (machine_irq) { |
786 | xen_pt_mapped_machine_irq[machine_irq]--; | |
787 | ||
788 | if (xen_pt_mapped_machine_irq[machine_irq] == 0) { | |
789 | rc = xc_physdev_unmap_pirq(xen_xc, xen_domid, machine_irq); | |
790 | ||
791 | if (rc < 0) { | |
792 | XEN_PT_ERR(d, "unmapping of interrupt %i failed. (rc: %d)" | |
793 | " But bravely continuing on..\n", | |
794 | machine_irq, rc); | |
795 | } | |
796 | } | |
797 | } | |
798 | ||
93d7ae8e AK |
799 | /* delete all emulated config registers */ |
800 | xen_pt_config_delete(s); | |
801 | ||
eaab4d60 | 802 | memory_listener_unregister(&s->memory_listener); |
12b40e47 | 803 | memory_listener_unregister(&s->io_listener); |
eaab4d60 AK |
804 | |
805 | xen_host_pci_device_put(&s->real_device); | |
eaab4d60 AK |
806 | } |
807 | ||
808 | static Property xen_pci_passthrough_properties[] = { | |
809 | DEFINE_PROP_PCI_HOST_DEVADDR("hostaddr", XenPCIPassthroughState, hostaddr), | |
810 | DEFINE_PROP_END_OF_LIST(), | |
811 | }; | |
812 | ||
813 | static void xen_pci_passthrough_class_init(ObjectClass *klass, void *data) | |
814 | { | |
815 | DeviceClass *dc = DEVICE_CLASS(klass); | |
816 | PCIDeviceClass *k = PCI_DEVICE_CLASS(klass); | |
817 | ||
818 | k->init = xen_pt_initfn; | |
819 | k->exit = xen_pt_unregister_device; | |
820 | k->config_read = xen_pt_pci_read_config; | |
821 | k->config_write = xen_pt_pci_write_config; | |
125ee0ed | 822 | set_bit(DEVICE_CATEGORY_MISC, dc->categories); |
eaab4d60 AK |
823 | dc->desc = "Assign an host PCI device with Xen"; |
824 | dc->props = xen_pci_passthrough_properties; | |
825 | }; | |
826 | ||
8c43a6f0 | 827 | static const TypeInfo xen_pci_passthrough_info = { |
eaab4d60 AK |
828 | .name = "xen-pci-passthrough", |
829 | .parent = TYPE_PCI_DEVICE, | |
830 | .instance_size = sizeof(XenPCIPassthroughState), | |
831 | .class_init = xen_pci_passthrough_class_init, | |
832 | }; | |
833 | ||
834 | static void xen_pci_passthrough_register_types(void) | |
835 | { | |
836 | type_register_static(&xen_pci_passthrough_info); | |
837 | } | |
838 | ||
839 | type_init(xen_pci_passthrough_register_types) |