1
2
3
4
5
6
7
8
9
10
11
12
13#include "qemu/osdep.h"
14#include "qemu/error-report.h"
15#include "qemu/range.h"
16#include "qapi/error.h"
17#include "qapi/visitor.h"
18#include "hw/nvram/fw_cfg.h"
19#include "pci.h"
20#include "trace.h"
21
22
23static bool vfio_pci_is(VFIOPCIDevice *vdev, uint32_t vendor, uint32_t device)
24{
25 return (vendor == PCI_ANY_ID || vendor == vdev->vendor_id) &&
26 (device == PCI_ANY_ID || device == vdev->device_id);
27}
28
29static bool vfio_is_vga(VFIOPCIDevice *vdev)
30{
31 PCIDevice *pdev = &vdev->pdev;
32 uint16_t class = pci_get_word(pdev->config + PCI_CLASS_DEVICE);
33
34 return class == PCI_CLASS_DISPLAY_VGA;
35}
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50static const struct {
51 uint32_t vendor;
52 uint32_t device;
53} romblacklist[] = {
54 { 0x14e4, 0x168e },
55};
56
57bool vfio_blacklist_opt_rom(VFIOPCIDevice *vdev)
58{
59 int i;
60
61 for (i = 0 ; i < ARRAY_SIZE(romblacklist); i++) {
62 if (vfio_pci_is(vdev, romblacklist[i].vendor, romblacklist[i].device)) {
63 trace_vfio_quirk_rom_blacklisted(vdev->vbasedev.name,
64 romblacklist[i].vendor,
65 romblacklist[i].device);
66 return true;
67 }
68 }
69 return false;
70}
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87typedef struct VFIOConfigWindowMatch {
88 uint32_t match;
89 uint32_t mask;
90} VFIOConfigWindowMatch;
91
92typedef struct VFIOConfigWindowQuirk {
93 struct VFIOPCIDevice *vdev;
94
95 uint32_t address_val;
96
97 uint32_t address_offset;
98 uint32_t data_offset;
99
100 bool window_enabled;
101 uint8_t bar;
102
103 MemoryRegion *addr_mem;
104 MemoryRegion *data_mem;
105
106 uint32_t nr_matches;
107 VFIOConfigWindowMatch matches[];
108} VFIOConfigWindowQuirk;
109
110static uint64_t vfio_generic_window_quirk_address_read(void *opaque,
111 hwaddr addr,
112 unsigned size)
113{
114 VFIOConfigWindowQuirk *window = opaque;
115 VFIOPCIDevice *vdev = window->vdev;
116
117 return vfio_region_read(&vdev->bars[window->bar].region,
118 addr + window->address_offset, size);
119}
120
121static void vfio_generic_window_quirk_address_write(void *opaque, hwaddr addr,
122 uint64_t data,
123 unsigned size)
124{
125 VFIOConfigWindowQuirk *window = opaque;
126 VFIOPCIDevice *vdev = window->vdev;
127 int i;
128
129 window->window_enabled = false;
130
131 vfio_region_write(&vdev->bars[window->bar].region,
132 addr + window->address_offset, data, size);
133
134 for (i = 0; i < window->nr_matches; i++) {
135 if ((data & ~window->matches[i].mask) == window->matches[i].match) {
136 window->window_enabled = true;
137 window->address_val = data & window->matches[i].mask;
138 trace_vfio_quirk_generic_window_address_write(vdev->vbasedev.name,
139 memory_region_name(window->addr_mem), data);
140 break;
141 }
142 }
143}
144
145static const MemoryRegionOps vfio_generic_window_address_quirk = {
146 .read = vfio_generic_window_quirk_address_read,
147 .write = vfio_generic_window_quirk_address_write,
148 .endianness = DEVICE_LITTLE_ENDIAN,
149};
150
151static uint64_t vfio_generic_window_quirk_data_read(void *opaque,
152 hwaddr addr, unsigned size)
153{
154 VFIOConfigWindowQuirk *window = opaque;
155 VFIOPCIDevice *vdev = window->vdev;
156 uint64_t data;
157
158
159 data = vfio_region_read(&vdev->bars[window->bar].region,
160 addr + window->data_offset, size);
161
162 if (window->window_enabled) {
163 data = vfio_pci_read_config(&vdev->pdev, window->address_val, size);
164 trace_vfio_quirk_generic_window_data_read(vdev->vbasedev.name,
165 memory_region_name(window->data_mem), data);
166 }
167
168 return data;
169}
170
171static void vfio_generic_window_quirk_data_write(void *opaque, hwaddr addr,
172 uint64_t data, unsigned size)
173{
174 VFIOConfigWindowQuirk *window = opaque;
175 VFIOPCIDevice *vdev = window->vdev;
176
177 if (window->window_enabled) {
178 vfio_pci_write_config(&vdev->pdev, window->address_val, data, size);
179 trace_vfio_quirk_generic_window_data_write(vdev->vbasedev.name,
180 memory_region_name(window->data_mem), data);
181 return;
182 }
183
184 vfio_region_write(&vdev->bars[window->bar].region,
185 addr + window->data_offset, data, size);
186}
187
188static const MemoryRegionOps vfio_generic_window_data_quirk = {
189 .read = vfio_generic_window_quirk_data_read,
190 .write = vfio_generic_window_quirk_data_write,
191 .endianness = DEVICE_LITTLE_ENDIAN,
192};
193
194
195
196
197
198
199
200typedef struct VFIOConfigMirrorQuirk {
201 struct VFIOPCIDevice *vdev;
202 uint32_t offset;
203 uint8_t bar;
204 MemoryRegion *mem;
205} VFIOConfigMirrorQuirk;
206
207static uint64_t vfio_generic_quirk_mirror_read(void *opaque,
208 hwaddr addr, unsigned size)
209{
210 VFIOConfigMirrorQuirk *mirror = opaque;
211 VFIOPCIDevice *vdev = mirror->vdev;
212 uint64_t data;
213
214
215 (void)vfio_region_read(&vdev->bars[mirror->bar].region,
216 addr + mirror->offset, size);
217
218 data = vfio_pci_read_config(&vdev->pdev, addr, size);
219 trace_vfio_quirk_generic_mirror_read(vdev->vbasedev.name,
220 memory_region_name(mirror->mem),
221 addr, data);
222 return data;
223}
224
225static void vfio_generic_quirk_mirror_write(void *opaque, hwaddr addr,
226 uint64_t data, unsigned size)
227{
228 VFIOConfigMirrorQuirk *mirror = opaque;
229 VFIOPCIDevice *vdev = mirror->vdev;
230
231 vfio_pci_write_config(&vdev->pdev, addr, data, size);
232 trace_vfio_quirk_generic_mirror_write(vdev->vbasedev.name,
233 memory_region_name(mirror->mem),
234 addr, data);
235}
236
237static const MemoryRegionOps vfio_generic_mirror_quirk = {
238 .read = vfio_generic_quirk_mirror_read,
239 .write = vfio_generic_quirk_mirror_write,
240 .endianness = DEVICE_LITTLE_ENDIAN,
241};
242
243
244static bool vfio_range_contained(uint64_t first1, uint64_t len1,
245 uint64_t first2, uint64_t len2) {
246 return (first1 >= first2 && first1 + len1 <= first2 + len2);
247}
248
249#define PCI_VENDOR_ID_ATI 0x1002
250
251
252
253
254
255
256
257
258
259
260
261static uint64_t vfio_ati_3c3_quirk_read(void *opaque,
262 hwaddr addr, unsigned size)
263{
264 VFIOPCIDevice *vdev = opaque;
265 uint64_t data = vfio_pci_read_config(&vdev->pdev,
266 PCI_BASE_ADDRESS_4 + 1, size);
267
268 trace_vfio_quirk_ati_3c3_read(vdev->vbasedev.name, data);
269
270 return data;
271}
272
273static const MemoryRegionOps vfio_ati_3c3_quirk = {
274 .read = vfio_ati_3c3_quirk_read,
275 .endianness = DEVICE_LITTLE_ENDIAN,
276};
277
278static void vfio_vga_probe_ati_3c3_quirk(VFIOPCIDevice *vdev)
279{
280 VFIOQuirk *quirk;
281
282
283
284
285
286 if (!vfio_pci_is(vdev, PCI_VENDOR_ID_ATI, PCI_ANY_ID) ||
287 !vdev->bars[4].ioport || vdev->bars[4].region.size < 256) {
288 return;
289 }
290
291 quirk = g_malloc0(sizeof(*quirk));
292 quirk->mem = g_new0(MemoryRegion, 1);
293 quirk->nr_mem = 1;
294
295 memory_region_init_io(quirk->mem, OBJECT(vdev), &vfio_ati_3c3_quirk, vdev,
296 "vfio-ati-3c3-quirk", 1);
297 memory_region_add_subregion(&vdev->vga->region[QEMU_PCI_VGA_IO_HI].mem,
298 3 , quirk->mem);
299
300 QLIST_INSERT_HEAD(&vdev->vga->region[QEMU_PCI_VGA_IO_HI].quirks,
301 quirk, next);
302
303 trace_vfio_quirk_ati_3c3_probe(vdev->vbasedev.name);
304}
305
306
307
308
309
310
311
312
313
314
315static void vfio_probe_ati_bar4_quirk(VFIOPCIDevice *vdev, int nr)
316{
317 VFIOQuirk *quirk;
318 VFIOConfigWindowQuirk *window;
319
320
321 if (!vfio_pci_is(vdev, PCI_VENDOR_ID_ATI, PCI_ANY_ID) ||
322 !vdev->vga || nr != 4) {
323 return;
324 }
325
326 quirk = g_malloc0(sizeof(*quirk));
327 quirk->mem = g_new0(MemoryRegion, 2);
328 quirk->nr_mem = 2;
329 window = quirk->data = g_malloc0(sizeof(*window) +
330 sizeof(VFIOConfigWindowMatch));
331 window->vdev = vdev;
332 window->address_offset = 0;
333 window->data_offset = 4;
334 window->nr_matches = 1;
335 window->matches[0].match = 0x4000;
336 window->matches[0].mask = vdev->config_size - 1;
337 window->bar = nr;
338 window->addr_mem = &quirk->mem[0];
339 window->data_mem = &quirk->mem[1];
340
341 memory_region_init_io(window->addr_mem, OBJECT(vdev),
342 &vfio_generic_window_address_quirk, window,
343 "vfio-ati-bar4-window-address-quirk", 4);
344 memory_region_add_subregion_overlap(vdev->bars[nr].region.mem,
345 window->address_offset,
346 window->addr_mem, 1);
347
348 memory_region_init_io(window->data_mem, OBJECT(vdev),
349 &vfio_generic_window_data_quirk, window,
350 "vfio-ati-bar4-window-data-quirk", 4);
351 memory_region_add_subregion_overlap(vdev->bars[nr].region.mem,
352 window->data_offset,
353 window->data_mem, 1);
354
355 QLIST_INSERT_HEAD(&vdev->bars[nr].quirks, quirk, next);
356
357 trace_vfio_quirk_ati_bar4_probe(vdev->vbasedev.name);
358}
359
360
361
362
363static void vfio_probe_ati_bar2_quirk(VFIOPCIDevice *vdev, int nr)
364{
365 VFIOQuirk *quirk;
366 VFIOConfigMirrorQuirk *mirror;
367
368
369 if (!vfio_pci_is(vdev, PCI_VENDOR_ID_ATI, PCI_ANY_ID) ||
370 !vdev->vga || nr != 2 || !vdev->bars[2].mem64) {
371 return;
372 }
373
374 quirk = g_malloc0(sizeof(*quirk));
375 mirror = quirk->data = g_malloc0(sizeof(*mirror));
376 mirror->mem = quirk->mem = g_new0(MemoryRegion, 1);
377 quirk->nr_mem = 1;
378 mirror->vdev = vdev;
379 mirror->offset = 0x4000;
380 mirror->bar = nr;
381
382 memory_region_init_io(mirror->mem, OBJECT(vdev),
383 &vfio_generic_mirror_quirk, mirror,
384 "vfio-ati-bar2-4000-quirk", PCI_CONFIG_SPACE_SIZE);
385 memory_region_add_subregion_overlap(vdev->bars[nr].region.mem,
386 mirror->offset, mirror->mem, 1);
387
388 QLIST_INSERT_HEAD(&vdev->bars[nr].quirks, quirk, next);
389
390 trace_vfio_quirk_ati_bar2_probe(vdev->vbasedev.name);
391}
392
393
394
395
396
397
398
399
400#define PCI_VENDOR_ID_NVIDIA 0x10de
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417typedef enum {NONE = 0, SELECT, WINDOW, READ, WRITE} VFIONvidia3d0State;
418static const char *nv3d0_states[] = { "NONE", "SELECT",
419 "WINDOW", "READ", "WRITE" };
420
421typedef struct VFIONvidia3d0Quirk {
422 VFIOPCIDevice *vdev;
423 VFIONvidia3d0State state;
424 uint32_t offset;
425} VFIONvidia3d0Quirk;
426
427static uint64_t vfio_nvidia_3d4_quirk_read(void *opaque,
428 hwaddr addr, unsigned size)
429{
430 VFIONvidia3d0Quirk *quirk = opaque;
431 VFIOPCIDevice *vdev = quirk->vdev;
432
433 quirk->state = NONE;
434
435 return vfio_vga_read(&vdev->vga->region[QEMU_PCI_VGA_IO_HI],
436 addr + 0x14, size);
437}
438
439static void vfio_nvidia_3d4_quirk_write(void *opaque, hwaddr addr,
440 uint64_t data, unsigned size)
441{
442 VFIONvidia3d0Quirk *quirk = opaque;
443 VFIOPCIDevice *vdev = quirk->vdev;
444 VFIONvidia3d0State old_state = quirk->state;
445
446 quirk->state = NONE;
447
448 switch (data) {
449 case 0x338:
450 if (old_state == NONE) {
451 quirk->state = SELECT;
452 trace_vfio_quirk_nvidia_3d0_state(vdev->vbasedev.name,
453 nv3d0_states[quirk->state]);
454 }
455 break;
456 case 0x538:
457 if (old_state == WINDOW) {
458 quirk->state = READ;
459 trace_vfio_quirk_nvidia_3d0_state(vdev->vbasedev.name,
460 nv3d0_states[quirk->state]);
461 }
462 break;
463 case 0x738:
464 if (old_state == WINDOW) {
465 quirk->state = WRITE;
466 trace_vfio_quirk_nvidia_3d0_state(vdev->vbasedev.name,
467 nv3d0_states[quirk->state]);
468 }
469 break;
470 }
471
472 vfio_vga_write(&vdev->vga->region[QEMU_PCI_VGA_IO_HI],
473 addr + 0x14, data, size);
474}
475
476static const MemoryRegionOps vfio_nvidia_3d4_quirk = {
477 .read = vfio_nvidia_3d4_quirk_read,
478 .write = vfio_nvidia_3d4_quirk_write,
479 .endianness = DEVICE_LITTLE_ENDIAN,
480};
481
482static uint64_t vfio_nvidia_3d0_quirk_read(void *opaque,
483 hwaddr addr, unsigned size)
484{
485 VFIONvidia3d0Quirk *quirk = opaque;
486 VFIOPCIDevice *vdev = quirk->vdev;
487 VFIONvidia3d0State old_state = quirk->state;
488 uint64_t data = vfio_vga_read(&vdev->vga->region[QEMU_PCI_VGA_IO_HI],
489 addr + 0x10, size);
490
491 quirk->state = NONE;
492
493 if (old_state == READ &&
494 (quirk->offset & ~(PCI_CONFIG_SPACE_SIZE - 1)) == 0x1800) {
495 uint8_t offset = quirk->offset & (PCI_CONFIG_SPACE_SIZE - 1);
496
497 data = vfio_pci_read_config(&vdev->pdev, offset, size);
498 trace_vfio_quirk_nvidia_3d0_read(vdev->vbasedev.name,
499 offset, size, data);
500 }
501
502 return data;
503}
504
505static void vfio_nvidia_3d0_quirk_write(void *opaque, hwaddr addr,
506 uint64_t data, unsigned size)
507{
508 VFIONvidia3d0Quirk *quirk = opaque;
509 VFIOPCIDevice *vdev = quirk->vdev;
510 VFIONvidia3d0State old_state = quirk->state;
511
512 quirk->state = NONE;
513
514 if (old_state == SELECT) {
515 quirk->offset = (uint32_t)data;
516 quirk->state = WINDOW;
517 trace_vfio_quirk_nvidia_3d0_state(vdev->vbasedev.name,
518 nv3d0_states[quirk->state]);
519 } else if (old_state == WRITE) {
520 if ((quirk->offset & ~(PCI_CONFIG_SPACE_SIZE - 1)) == 0x1800) {
521 uint8_t offset = quirk->offset & (PCI_CONFIG_SPACE_SIZE - 1);
522
523 vfio_pci_write_config(&vdev->pdev, offset, data, size);
524 trace_vfio_quirk_nvidia_3d0_write(vdev->vbasedev.name,
525 offset, data, size);
526 return;
527 }
528 }
529
530 vfio_vga_write(&vdev->vga->region[QEMU_PCI_VGA_IO_HI],
531 addr + 0x10, data, size);
532}
533
534static const MemoryRegionOps vfio_nvidia_3d0_quirk = {
535 .read = vfio_nvidia_3d0_quirk_read,
536 .write = vfio_nvidia_3d0_quirk_write,
537 .endianness = DEVICE_LITTLE_ENDIAN,
538};
539
540static void vfio_vga_probe_nvidia_3d0_quirk(VFIOPCIDevice *vdev)
541{
542 VFIOQuirk *quirk;
543 VFIONvidia3d0Quirk *data;
544
545 if (!vfio_pci_is(vdev, PCI_VENDOR_ID_NVIDIA, PCI_ANY_ID) ||
546 !vdev->bars[1].region.size) {
547 return;
548 }
549
550 quirk = g_malloc0(sizeof(*quirk));
551 quirk->data = data = g_malloc0(sizeof(*data));
552 quirk->mem = g_new0(MemoryRegion, 2);
553 quirk->nr_mem = 2;
554 data->vdev = vdev;
555
556 memory_region_init_io(&quirk->mem[0], OBJECT(vdev), &vfio_nvidia_3d4_quirk,
557 data, "vfio-nvidia-3d4-quirk", 2);
558 memory_region_add_subregion(&vdev->vga->region[QEMU_PCI_VGA_IO_HI].mem,
559 0x14 , &quirk->mem[0]);
560
561 memory_region_init_io(&quirk->mem[1], OBJECT(vdev), &vfio_nvidia_3d0_quirk,
562 data, "vfio-nvidia-3d0-quirk", 2);
563 memory_region_add_subregion(&vdev->vga->region[QEMU_PCI_VGA_IO_HI].mem,
564 0x10 , &quirk->mem[1]);
565
566 QLIST_INSERT_HEAD(&vdev->vga->region[QEMU_PCI_VGA_IO_HI].quirks,
567 quirk, next);
568
569 trace_vfio_quirk_nvidia_3d0_probe(vdev->vbasedev.name);
570}
571
572
573
574
575
576
577
578
579typedef struct VFIONvidiaBAR5Quirk {
580 uint32_t master;
581 uint32_t enable;
582 MemoryRegion *addr_mem;
583 MemoryRegion *data_mem;
584 bool enabled;
585 VFIOConfigWindowQuirk window;
586} VFIONvidiaBAR5Quirk;
587
588static void vfio_nvidia_bar5_enable(VFIONvidiaBAR5Quirk *bar5)
589{
590 VFIOPCIDevice *vdev = bar5->window.vdev;
591
592 if (((bar5->master & bar5->enable) & 0x1) == bar5->enabled) {
593 return;
594 }
595
596 bar5->enabled = !bar5->enabled;
597 trace_vfio_quirk_nvidia_bar5_state(vdev->vbasedev.name,
598 bar5->enabled ? "Enable" : "Disable");
599 memory_region_set_enabled(bar5->addr_mem, bar5->enabled);
600 memory_region_set_enabled(bar5->data_mem, bar5->enabled);
601}
602
603static uint64_t vfio_nvidia_bar5_quirk_master_read(void *opaque,
604 hwaddr addr, unsigned size)
605{
606 VFIONvidiaBAR5Quirk *bar5 = opaque;
607 VFIOPCIDevice *vdev = bar5->window.vdev;
608
609 return vfio_region_read(&vdev->bars[5].region, addr, size);
610}
611
612static void vfio_nvidia_bar5_quirk_master_write(void *opaque, hwaddr addr,
613 uint64_t data, unsigned size)
614{
615 VFIONvidiaBAR5Quirk *bar5 = opaque;
616 VFIOPCIDevice *vdev = bar5->window.vdev;
617
618 vfio_region_write(&vdev->bars[5].region, addr, data, size);
619
620 bar5->master = data;
621 vfio_nvidia_bar5_enable(bar5);
622}
623
624static const MemoryRegionOps vfio_nvidia_bar5_quirk_master = {
625 .read = vfio_nvidia_bar5_quirk_master_read,
626 .write = vfio_nvidia_bar5_quirk_master_write,
627 .endianness = DEVICE_LITTLE_ENDIAN,
628};
629
630static uint64_t vfio_nvidia_bar5_quirk_enable_read(void *opaque,
631 hwaddr addr, unsigned size)
632{
633 VFIONvidiaBAR5Quirk *bar5 = opaque;
634 VFIOPCIDevice *vdev = bar5->window.vdev;
635
636 return vfio_region_read(&vdev->bars[5].region, addr + 4, size);
637}
638
639static void vfio_nvidia_bar5_quirk_enable_write(void *opaque, hwaddr addr,
640 uint64_t data, unsigned size)
641{
642 VFIONvidiaBAR5Quirk *bar5 = opaque;
643 VFIOPCIDevice *vdev = bar5->window.vdev;
644
645 vfio_region_write(&vdev->bars[5].region, addr + 4, data, size);
646
647 bar5->enable = data;
648 vfio_nvidia_bar5_enable(bar5);
649}
650
651static const MemoryRegionOps vfio_nvidia_bar5_quirk_enable = {
652 .read = vfio_nvidia_bar5_quirk_enable_read,
653 .write = vfio_nvidia_bar5_quirk_enable_write,
654 .endianness = DEVICE_LITTLE_ENDIAN,
655};
656
657static void vfio_probe_nvidia_bar5_quirk(VFIOPCIDevice *vdev, int nr)
658{
659 VFIOQuirk *quirk;
660 VFIONvidiaBAR5Quirk *bar5;
661 VFIOConfigWindowQuirk *window;
662
663 if (!vfio_pci_is(vdev, PCI_VENDOR_ID_NVIDIA, PCI_ANY_ID) ||
664 !vdev->vga || nr != 5 || !vdev->bars[5].ioport) {
665 return;
666 }
667
668 quirk = g_malloc0(sizeof(*quirk));
669 quirk->mem = g_new0(MemoryRegion, 4);
670 quirk->nr_mem = 4;
671 bar5 = quirk->data = g_malloc0(sizeof(*bar5) +
672 (sizeof(VFIOConfigWindowMatch) * 2));
673 window = &bar5->window;
674
675 window->vdev = vdev;
676 window->address_offset = 0x8;
677 window->data_offset = 0xc;
678 window->nr_matches = 2;
679 window->matches[0].match = 0x1800;
680 window->matches[0].mask = PCI_CONFIG_SPACE_SIZE - 1;
681 window->matches[1].match = 0x88000;
682 window->matches[1].mask = vdev->config_size - 1;
683 window->bar = nr;
684 window->addr_mem = bar5->addr_mem = &quirk->mem[0];
685 window->data_mem = bar5->data_mem = &quirk->mem[1];
686
687 memory_region_init_io(window->addr_mem, OBJECT(vdev),
688 &vfio_generic_window_address_quirk, window,
689 "vfio-nvidia-bar5-window-address-quirk", 4);
690 memory_region_add_subregion_overlap(vdev->bars[nr].region.mem,
691 window->address_offset,
692 window->addr_mem, 1);
693 memory_region_set_enabled(window->addr_mem, false);
694
695 memory_region_init_io(window->data_mem, OBJECT(vdev),
696 &vfio_generic_window_data_quirk, window,
697 "vfio-nvidia-bar5-window-data-quirk", 4);
698 memory_region_add_subregion_overlap(vdev->bars[nr].region.mem,
699 window->data_offset,
700 window->data_mem, 1);
701 memory_region_set_enabled(window->data_mem, false);
702
703 memory_region_init_io(&quirk->mem[2], OBJECT(vdev),
704 &vfio_nvidia_bar5_quirk_master, bar5,
705 "vfio-nvidia-bar5-master-quirk", 4);
706 memory_region_add_subregion_overlap(vdev->bars[nr].region.mem,
707 0, &quirk->mem[2], 1);
708
709 memory_region_init_io(&quirk->mem[3], OBJECT(vdev),
710 &vfio_nvidia_bar5_quirk_enable, bar5,
711 "vfio-nvidia-bar5-enable-quirk", 4);
712 memory_region_add_subregion_overlap(vdev->bars[nr].region.mem,
713 4, &quirk->mem[3], 1);
714
715 QLIST_INSERT_HEAD(&vdev->bars[nr].quirks, quirk, next);
716
717 trace_vfio_quirk_nvidia_bar5_probe(vdev->vbasedev.name);
718}
719
720
721
722
723
724static void vfio_nvidia_quirk_mirror_write(void *opaque, hwaddr addr,
725 uint64_t data, unsigned size)
726{
727 VFIOConfigMirrorQuirk *mirror = opaque;
728 VFIOPCIDevice *vdev = mirror->vdev;
729 PCIDevice *pdev = &vdev->pdev;
730
731 vfio_generic_quirk_mirror_write(opaque, addr, data, size);
732
733
734
735
736
737
738 if ((pdev->cap_present & QEMU_PCI_CAP_MSI) &&
739 vfio_range_contained(addr, size, pdev->msi_cap, PCI_MSI_FLAGS)) {
740 vfio_region_write(&vdev->bars[mirror->bar].region,
741 addr + mirror->offset, data, size);
742 trace_vfio_quirk_nvidia_bar0_msi_ack(vdev->vbasedev.name);
743 }
744}
745
746static const MemoryRegionOps vfio_nvidia_mirror_quirk = {
747 .read = vfio_generic_quirk_mirror_read,
748 .write = vfio_nvidia_quirk_mirror_write,
749 .endianness = DEVICE_LITTLE_ENDIAN,
750};
751
752static void vfio_probe_nvidia_bar0_quirk(VFIOPCIDevice *vdev, int nr)
753{
754 VFIOQuirk *quirk;
755 VFIOConfigMirrorQuirk *mirror;
756
757 if (!vfio_pci_is(vdev, PCI_VENDOR_ID_NVIDIA, PCI_ANY_ID) ||
758 !vfio_is_vga(vdev) || nr != 0) {
759 return;
760 }
761
762 quirk = g_malloc0(sizeof(*quirk));
763 mirror = quirk->data = g_malloc0(sizeof(*mirror));
764 mirror->mem = quirk->mem = g_new0(MemoryRegion, 1);
765 quirk->nr_mem = 1;
766 mirror->vdev = vdev;
767 mirror->offset = 0x88000;
768 mirror->bar = nr;
769
770 memory_region_init_io(mirror->mem, OBJECT(vdev),
771 &vfio_nvidia_mirror_quirk, mirror,
772 "vfio-nvidia-bar0-88000-mirror-quirk",
773 vdev->config_size);
774 memory_region_add_subregion_overlap(vdev->bars[nr].region.mem,
775 mirror->offset, mirror->mem, 1);
776
777 QLIST_INSERT_HEAD(&vdev->bars[nr].quirks, quirk, next);
778
779
780 if (vdev->vga) {
781 quirk = g_malloc0(sizeof(*quirk));
782 mirror = quirk->data = g_malloc0(sizeof(*mirror));
783 mirror->mem = quirk->mem = g_new0(MemoryRegion, 1);
784 quirk->nr_mem = 1;
785 mirror->vdev = vdev;
786 mirror->offset = 0x1800;
787 mirror->bar = nr;
788
789 memory_region_init_io(mirror->mem, OBJECT(vdev),
790 &vfio_nvidia_mirror_quirk, mirror,
791 "vfio-nvidia-bar0-1800-mirror-quirk",
792 PCI_CONFIG_SPACE_SIZE);
793 memory_region_add_subregion_overlap(vdev->bars[nr].region.mem,
794 mirror->offset, mirror->mem, 1);
795
796 QLIST_INSERT_HEAD(&vdev->bars[nr].quirks, quirk, next);
797 }
798
799 trace_vfio_quirk_nvidia_bar0_probe(vdev->vbasedev.name);
800}
801
802
803
804
805
806
807
808#define PCI_VENDOR_ID_REALTEK 0x10ec
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832typedef struct VFIOrtl8168Quirk {
833 VFIOPCIDevice *vdev;
834 uint32_t addr;
835 uint32_t data;
836 bool enabled;
837} VFIOrtl8168Quirk;
838
839static uint64_t vfio_rtl8168_quirk_address_read(void *opaque,
840 hwaddr addr, unsigned size)
841{
842 VFIOrtl8168Quirk *rtl = opaque;
843 VFIOPCIDevice *vdev = rtl->vdev;
844 uint64_t data = vfio_region_read(&vdev->bars[2].region, addr + 0x74, size);
845
846 if (rtl->enabled) {
847 data = rtl->addr ^ 0x80000000U;
848 trace_vfio_quirk_rtl8168_fake_latch(vdev->vbasedev.name, data);
849 }
850
851 return data;
852}
853
854static void vfio_rtl8168_quirk_address_write(void *opaque, hwaddr addr,
855 uint64_t data, unsigned size)
856{
857 VFIOrtl8168Quirk *rtl = opaque;
858 VFIOPCIDevice *vdev = rtl->vdev;
859
860 rtl->enabled = false;
861
862 if ((data & 0x7fff0000) == 0x10000) {
863 rtl->enabled = true;
864 rtl->addr = (uint32_t)data;
865
866 if (data & 0x80000000U) {
867 if (vdev->pdev.cap_present & QEMU_PCI_CAP_MSIX) {
868 hwaddr offset = data & 0xfff;
869 uint64_t val = rtl->data;
870
871 trace_vfio_quirk_rtl8168_msix_write(vdev->vbasedev.name,
872 (uint16_t)offset, val);
873
874
875 memory_region_dispatch_write(&vdev->pdev.msix_table_mmio,
876 offset, val, size,
877 MEMTXATTRS_UNSPECIFIED);
878 }
879 return;
880 }
881 }
882
883 vfio_region_write(&vdev->bars[2].region, addr + 0x74, data, size);
884}
885
886static const MemoryRegionOps vfio_rtl_address_quirk = {
887 .read = vfio_rtl8168_quirk_address_read,
888 .write = vfio_rtl8168_quirk_address_write,
889 .valid = {
890 .min_access_size = 4,
891 .max_access_size = 4,
892 .unaligned = false,
893 },
894 .endianness = DEVICE_LITTLE_ENDIAN,
895};
896
897static uint64_t vfio_rtl8168_quirk_data_read(void *opaque,
898 hwaddr addr, unsigned size)
899{
900 VFIOrtl8168Quirk *rtl = opaque;
901 VFIOPCIDevice *vdev = rtl->vdev;
902 uint64_t data = vfio_region_read(&vdev->bars[2].region, addr + 0x70, size);
903
904 if (rtl->enabled && (vdev->pdev.cap_present & QEMU_PCI_CAP_MSIX)) {
905 hwaddr offset = rtl->addr & 0xfff;
906 memory_region_dispatch_read(&vdev->pdev.msix_table_mmio, offset,
907 &data, size, MEMTXATTRS_UNSPECIFIED);
908 trace_vfio_quirk_rtl8168_msix_read(vdev->vbasedev.name, offset, data);
909 }
910
911 return data;
912}
913
914static void vfio_rtl8168_quirk_data_write(void *opaque, hwaddr addr,
915 uint64_t data, unsigned size)
916{
917 VFIOrtl8168Quirk *rtl = opaque;
918 VFIOPCIDevice *vdev = rtl->vdev;
919
920 rtl->data = (uint32_t)data;
921
922 vfio_region_write(&vdev->bars[2].region, addr + 0x70, data, size);
923}
924
925static const MemoryRegionOps vfio_rtl_data_quirk = {
926 .read = vfio_rtl8168_quirk_data_read,
927 .write = vfio_rtl8168_quirk_data_write,
928 .valid = {
929 .min_access_size = 4,
930 .max_access_size = 4,
931 .unaligned = false,
932 },
933 .endianness = DEVICE_LITTLE_ENDIAN,
934};
935
936static void vfio_probe_rtl8168_bar2_quirk(VFIOPCIDevice *vdev, int nr)
937{
938 VFIOQuirk *quirk;
939 VFIOrtl8168Quirk *rtl;
940
941 if (!vfio_pci_is(vdev, PCI_VENDOR_ID_REALTEK, 0x8168) || nr != 2) {
942 return;
943 }
944
945 quirk = g_malloc0(sizeof(*quirk));
946 quirk->mem = g_new0(MemoryRegion, 2);
947 quirk->nr_mem = 2;
948 quirk->data = rtl = g_malloc0(sizeof(*rtl));
949 rtl->vdev = vdev;
950
951 memory_region_init_io(&quirk->mem[0], OBJECT(vdev),
952 &vfio_rtl_address_quirk, rtl,
953 "vfio-rtl8168-window-address-quirk", 4);
954 memory_region_add_subregion_overlap(vdev->bars[nr].region.mem,
955 0x74, &quirk->mem[0], 1);
956
957 memory_region_init_io(&quirk->mem[1], OBJECT(vdev),
958 &vfio_rtl_data_quirk, rtl,
959 "vfio-rtl8168-window-data-quirk", 4);
960 memory_region_add_subregion_overlap(vdev->bars[nr].region.mem,
961 0x70, &quirk->mem[1], 1);
962
963 QLIST_INSERT_HEAD(&vdev->bars[nr].quirks, quirk, next);
964
965 trace_vfio_quirk_rtl8168_probe(vdev->vbasedev.name);
966}
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006static int igd_gen(VFIOPCIDevice *vdev)
1007{
1008 if ((vdev->device_id & 0xfff) == 0xa84) {
1009 return 8;
1010 }
1011
1012 switch (vdev->device_id & 0xff00) {
1013
1014 case 0x0000:
1015 case 0x2500:
1016 case 0x2700:
1017 case 0x2900:
1018 case 0x2a00:
1019 case 0x2e00:
1020 case 0x3500:
1021 case 0xa000:
1022 return -1;
1023
1024 case 0x0100:
1025 case 0x0400:
1026 case 0x0a00:
1027 case 0x0c00:
1028 case 0x0d00:
1029 case 0x0f00:
1030 return 6;
1031
1032 case 0x1600:
1033 case 0x1900:
1034 case 0x2200:
1035 case 0x5900:
1036 return 8;
1037 }
1038
1039 return 8;
1040}
1041
1042typedef struct VFIOIGDQuirk {
1043 struct VFIOPCIDevice *vdev;
1044 uint32_t index;
1045 uint32_t bdsm;
1046} VFIOIGDQuirk;
1047
1048#define IGD_GMCH 0x50
1049#define IGD_BDSM 0x5c
1050#define IGD_ASLS 0xfc
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060int vfio_pci_igd_opregion_init(VFIOPCIDevice *vdev,
1061 struct vfio_region_info *info, Error **errp)
1062{
1063 int ret;
1064
1065 vdev->igd_opregion = g_malloc0(info->size);
1066 ret = pread(vdev->vbasedev.fd, vdev->igd_opregion,
1067 info->size, info->offset);
1068 if (ret != info->size) {
1069 error_setg(errp, "failed to read IGD OpRegion");
1070 g_free(vdev->igd_opregion);
1071 vdev->igd_opregion = NULL;
1072 return -EINVAL;
1073 }
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088 fw_cfg_add_file(fw_cfg_find(), "etc/igd-opregion",
1089 vdev->igd_opregion, info->size);
1090
1091 trace_vfio_pci_igd_opregion_enabled(vdev->vbasedev.name);
1092
1093 pci_set_long(vdev->pdev.config + IGD_ASLS, 0);
1094 pci_set_long(vdev->pdev.wmask + IGD_ASLS, ~0);
1095 pci_set_long(vdev->emulated_config_bits + IGD_ASLS, ~0);
1096
1097 return 0;
1098}
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108typedef struct {
1109 uint8_t offset;
1110 uint8_t len;
1111} IGDHostInfo;
1112
1113static const IGDHostInfo igd_host_bridge_infos[] = {
1114 {PCI_REVISION_ID, 2},
1115 {PCI_SUBSYSTEM_VENDOR_ID, 2},
1116 {PCI_SUBSYSTEM_ID, 2},
1117};
1118
1119static const IGDHostInfo igd_lpc_bridge_infos[] = {
1120 {PCI_VENDOR_ID, 2},
1121 {PCI_DEVICE_ID, 2},
1122 {PCI_REVISION_ID, 2},
1123 {PCI_SUBSYSTEM_VENDOR_ID, 2},
1124 {PCI_SUBSYSTEM_ID, 2},
1125};
1126
1127static int vfio_pci_igd_copy(VFIOPCIDevice *vdev, PCIDevice *pdev,
1128 struct vfio_region_info *info,
1129 const IGDHostInfo *list, int len)
1130{
1131 int i, ret;
1132
1133 for (i = 0; i < len; i++) {
1134 ret = pread(vdev->vbasedev.fd, pdev->config + list[i].offset,
1135 list[i].len, info->offset + list[i].offset);
1136 if (ret != list[i].len) {
1137 error_report("IGD copy failed: %m");
1138 return -errno;
1139 }
1140 }
1141
1142 return 0;
1143}
1144
1145
1146
1147
1148static int vfio_pci_igd_host_init(VFIOPCIDevice *vdev,
1149 struct vfio_region_info *info)
1150{
1151 PCIBus *bus;
1152 PCIDevice *host_bridge;
1153 int ret;
1154
1155 bus = pci_device_root_bus(&vdev->pdev);
1156 host_bridge = pci_find_device(bus, 0, PCI_DEVFN(0, 0));
1157
1158 if (!host_bridge) {
1159 error_report("Can't find host bridge");
1160 return -ENODEV;
1161 }
1162
1163 ret = vfio_pci_igd_copy(vdev, host_bridge, info, igd_host_bridge_infos,
1164 ARRAY_SIZE(igd_host_bridge_infos));
1165 if (!ret) {
1166 trace_vfio_pci_igd_host_bridge_enabled(vdev->vbasedev.name);
1167 }
1168
1169 return ret;
1170}
1171
1172
1173
1174
1175
1176
1177
1178static void vfio_pci_igd_lpc_bridge_realize(PCIDevice *pdev, Error **errp)
1179{
1180 if (pdev->devfn != PCI_DEVFN(0x1f, 0)) {
1181 error_setg(errp, "VFIO dummy ISA/LPC bridge must have address 1f.0");
1182 }
1183}
1184
1185static void vfio_pci_igd_lpc_bridge_class_init(ObjectClass *klass, void *data)
1186{
1187 DeviceClass *dc = DEVICE_CLASS(klass);
1188 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
1189
1190 set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories);
1191 dc->desc = "VFIO dummy ISA/LPC bridge for IGD assignment";
1192 dc->hotpluggable = false;
1193 k->realize = vfio_pci_igd_lpc_bridge_realize;
1194 k->class_id = PCI_CLASS_BRIDGE_ISA;
1195}
1196
1197static TypeInfo vfio_pci_igd_lpc_bridge_info = {
1198 .name = "vfio-pci-igd-lpc-bridge",
1199 .parent = TYPE_PCI_DEVICE,
1200 .class_init = vfio_pci_igd_lpc_bridge_class_init,
1201 .interfaces = (InterfaceInfo[]) {
1202 { INTERFACE_CONVENTIONAL_PCI_DEVICE },
1203 { },
1204 },
1205};
1206
1207static void vfio_pci_igd_register_types(void)
1208{
1209 type_register_static(&vfio_pci_igd_lpc_bridge_info);
1210}
1211
1212type_init(vfio_pci_igd_register_types)
1213
1214static int vfio_pci_igd_lpc_init(VFIOPCIDevice *vdev,
1215 struct vfio_region_info *info)
1216{
1217 PCIDevice *lpc_bridge;
1218 int ret;
1219
1220 lpc_bridge = pci_find_device(pci_device_root_bus(&vdev->pdev),
1221 0, PCI_DEVFN(0x1f, 0));
1222 if (!lpc_bridge) {
1223 lpc_bridge = pci_create_simple(pci_device_root_bus(&vdev->pdev),
1224 PCI_DEVFN(0x1f, 0), "vfio-pci-igd-lpc-bridge");
1225 }
1226
1227 ret = vfio_pci_igd_copy(vdev, lpc_bridge, info, igd_lpc_bridge_infos,
1228 ARRAY_SIZE(igd_lpc_bridge_infos));
1229 if (!ret) {
1230 trace_vfio_pci_igd_lpc_bridge_enabled(vdev->vbasedev.name);
1231 }
1232
1233 return ret;
1234}
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244static int vfio_igd_gtt_max(VFIOPCIDevice *vdev)
1245{
1246 uint32_t gmch = vfio_pci_read_config(&vdev->pdev, IGD_GMCH, sizeof(gmch));
1247 int ggms, gen = igd_gen(vdev);
1248
1249 gmch = vfio_pci_read_config(&vdev->pdev, IGD_GMCH, sizeof(gmch));
1250 ggms = (gmch >> (gen < 8 ? 8 : 6)) & 0x3;
1251 if (gen > 6) {
1252 ggms = 1 << ggms;
1253 }
1254
1255 ggms *= 1024 * 1024;
1256
1257 return (ggms / (4 * 1024)) * (gen < 8 ? 4 : 8);
1258}
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271static uint64_t vfio_igd_quirk_data_read(void *opaque,
1272 hwaddr addr, unsigned size)
1273{
1274 VFIOIGDQuirk *igd = opaque;
1275 VFIOPCIDevice *vdev = igd->vdev;
1276
1277 igd->index = ~0;
1278
1279 return vfio_region_read(&vdev->bars[4].region, addr + 4, size);
1280}
1281
1282static void vfio_igd_quirk_data_write(void *opaque, hwaddr addr,
1283 uint64_t data, unsigned size)
1284{
1285 VFIOIGDQuirk *igd = opaque;
1286 VFIOPCIDevice *vdev = igd->vdev;
1287 uint64_t val = data;
1288 int gen = igd_gen(vdev);
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304 if ((igd->index % 4 == 1) && igd->index < vfio_igd_gtt_max(vdev)) {
1305 if (gen < 8 || (igd->index % 8 == 1)) {
1306 uint32_t base;
1307
1308 base = pci_get_long(vdev->pdev.config + IGD_BDSM);
1309 if (!base) {
1310 hw_error("vfio-igd: Guest attempted to program IGD GTT before "
1311 "BIOS reserved stolen memory. Unsupported BIOS?");
1312 }
1313
1314 val = data - igd->bdsm + base;
1315 } else {
1316 val = 0;
1317 }
1318
1319 trace_vfio_pci_igd_bar4_write(vdev->vbasedev.name,
1320 igd->index, data, val);
1321 }
1322
1323 vfio_region_write(&vdev->bars[4].region, addr + 4, val, size);
1324
1325 igd->index = ~0;
1326}
1327
1328static const MemoryRegionOps vfio_igd_data_quirk = {
1329 .read = vfio_igd_quirk_data_read,
1330 .write = vfio_igd_quirk_data_write,
1331 .endianness = DEVICE_LITTLE_ENDIAN,
1332};
1333
1334static uint64_t vfio_igd_quirk_index_read(void *opaque,
1335 hwaddr addr, unsigned size)
1336{
1337 VFIOIGDQuirk *igd = opaque;
1338 VFIOPCIDevice *vdev = igd->vdev;
1339
1340 igd->index = ~0;
1341
1342 return vfio_region_read(&vdev->bars[4].region, addr, size);
1343}
1344
1345static void vfio_igd_quirk_index_write(void *opaque, hwaddr addr,
1346 uint64_t data, unsigned size)
1347{
1348 VFIOIGDQuirk *igd = opaque;
1349 VFIOPCIDevice *vdev = igd->vdev;
1350
1351 igd->index = data;
1352
1353 vfio_region_write(&vdev->bars[4].region, addr, data, size);
1354}
1355
1356static const MemoryRegionOps vfio_igd_index_quirk = {
1357 .read = vfio_igd_quirk_index_read,
1358 .write = vfio_igd_quirk_index_write,
1359 .endianness = DEVICE_LITTLE_ENDIAN,
1360};
1361
1362static void vfio_probe_igd_bar4_quirk(VFIOPCIDevice *vdev, int nr)
1363{
1364 struct vfio_region_info *rom = NULL, *opregion = NULL,
1365 *host = NULL, *lpc = NULL;
1366 VFIOQuirk *quirk;
1367 VFIOIGDQuirk *igd;
1368 PCIDevice *lpc_bridge;
1369 int i, ret, ggms_mb, gms_mb = 0, gen;
1370 uint64_t *bdsm_size;
1371 uint32_t gmch;
1372 uint16_t cmd_orig, cmd;
1373 Error *err = NULL;
1374
1375
1376
1377
1378
1379
1380 if (!vfio_pci_is(vdev, PCI_VENDOR_ID_INTEL, PCI_ANY_ID) ||
1381 !vfio_is_vga(vdev) || nr != 4 ||
1382 &vdev->pdev != pci_find_device(pci_device_root_bus(&vdev->pdev),
1383 0, PCI_DEVFN(0x2, 0))) {
1384 return;
1385 }
1386
1387
1388
1389
1390
1391
1392 lpc_bridge = pci_find_device(pci_device_root_bus(&vdev->pdev),
1393 0, PCI_DEVFN(0x1f, 0));
1394 if (lpc_bridge && !object_dynamic_cast(OBJECT(lpc_bridge),
1395 "vfio-pci-igd-lpc-bridge")) {
1396 error_report("IGD device %s cannot support legacy mode due to existing "
1397 "devices at address 1f.0", vdev->vbasedev.name);
1398 return;
1399 }
1400
1401
1402
1403
1404
1405
1406 gen = igd_gen(vdev);
1407 if (gen != 6 && gen != 8) {
1408 error_report("IGD device %s is unsupported in legacy mode, "
1409 "try SandyBridge or newer", vdev->vbasedev.name);
1410 return;
1411 }
1412
1413
1414
1415
1416
1417
1418 ret = vfio_get_region_info(&vdev->vbasedev,
1419 VFIO_PCI_ROM_REGION_INDEX, &rom);
1420 if ((ret || !rom->size) && !vdev->pdev.romfile) {
1421 error_report("IGD device %s has no ROM, legacy mode disabled",
1422 vdev->vbasedev.name);
1423 goto out;
1424 }
1425
1426
1427
1428
1429
1430 if (vdev->pdev.qdev.hotplugged) {
1431 error_report("IGD device %s hotplugged, ROM disabled, "
1432 "legacy mode disabled", vdev->vbasedev.name);
1433 vdev->rom_read_failed = true;
1434 goto out;
1435 }
1436
1437
1438
1439
1440
1441 ret = vfio_get_dev_region_info(&vdev->vbasedev,
1442 VFIO_REGION_TYPE_PCI_VENDOR_TYPE | PCI_VENDOR_ID_INTEL,
1443 VFIO_REGION_SUBTYPE_INTEL_IGD_OPREGION, &opregion);
1444 if (ret) {
1445 error_report("IGD device %s does not support OpRegion access,"
1446 "legacy mode disabled", vdev->vbasedev.name);
1447 goto out;
1448 }
1449
1450 ret = vfio_get_dev_region_info(&vdev->vbasedev,
1451 VFIO_REGION_TYPE_PCI_VENDOR_TYPE | PCI_VENDOR_ID_INTEL,
1452 VFIO_REGION_SUBTYPE_INTEL_IGD_HOST_CFG, &host);
1453 if (ret) {
1454 error_report("IGD device %s does not support host bridge access,"
1455 "legacy mode disabled", vdev->vbasedev.name);
1456 goto out;
1457 }
1458
1459 ret = vfio_get_dev_region_info(&vdev->vbasedev,
1460 VFIO_REGION_TYPE_PCI_VENDOR_TYPE | PCI_VENDOR_ID_INTEL,
1461 VFIO_REGION_SUBTYPE_INTEL_IGD_LPC_CFG, &lpc);
1462 if (ret) {
1463 error_report("IGD device %s does not support LPC bridge access,"
1464 "legacy mode disabled", vdev->vbasedev.name);
1465 goto out;
1466 }
1467
1468 gmch = vfio_pci_read_config(&vdev->pdev, IGD_GMCH, 4);
1469
1470
1471
1472
1473
1474
1475 if (!(gmch & 0x2) && !vdev->vga && vfio_populate_vga(vdev, &err)) {
1476 error_reportf_err(err, ERR_PREFIX, vdev->vbasedev.name);
1477 error_report("IGD device %s failed to enable VGA access, "
1478 "legacy mode disabled", vdev->vbasedev.name);
1479 goto out;
1480 }
1481
1482
1483 ret = vfio_pci_igd_lpc_init(vdev, lpc);
1484 if (ret) {
1485 error_report("IGD device %s failed to create LPC bridge, "
1486 "legacy mode disabled", vdev->vbasedev.name);
1487 goto out;
1488 }
1489
1490
1491 ret = vfio_pci_igd_host_init(vdev, host);
1492 if (ret) {
1493 error_report("IGD device %s failed to modify host bridge, "
1494 "legacy mode disabled", vdev->vbasedev.name);
1495 goto out;
1496 }
1497
1498
1499 ret = vfio_pci_igd_opregion_init(vdev, opregion, &err);
1500 if (ret) {
1501 error_append_hint(&err, "IGD legacy mode disabled\n");
1502 error_reportf_err(err, ERR_PREFIX, vdev->vbasedev.name);
1503 goto out;
1504 }
1505
1506
1507 quirk = g_malloc0(sizeof(*quirk));
1508 quirk->mem = g_new0(MemoryRegion, 2);
1509 quirk->nr_mem = 2;
1510 igd = quirk->data = g_malloc0(sizeof(*igd));
1511 igd->vdev = vdev;
1512 igd->index = ~0;
1513 igd->bdsm = vfio_pci_read_config(&vdev->pdev, IGD_BDSM, 4);
1514 igd->bdsm &= ~((1 << 20) - 1);
1515
1516 memory_region_init_io(&quirk->mem[0], OBJECT(vdev), &vfio_igd_index_quirk,
1517 igd, "vfio-igd-index-quirk", 4);
1518 memory_region_add_subregion_overlap(vdev->bars[nr].region.mem,
1519 0, &quirk->mem[0], 1);
1520
1521 memory_region_init_io(&quirk->mem[1], OBJECT(vdev), &vfio_igd_data_quirk,
1522 igd, "vfio-igd-data-quirk", 4);
1523 memory_region_add_subregion_overlap(vdev->bars[nr].region.mem,
1524 4, &quirk->mem[1], 1);
1525
1526 QLIST_INSERT_HEAD(&vdev->bars[nr].quirks, quirk, next);
1527
1528
1529 ggms_mb = (gmch >> (gen < 8 ? 8 : 6)) & 0x3;
1530 if (gen > 6) {
1531 ggms_mb = 1 << ggms_mb;
1532 }
1533
1534
1535
1536
1537
1538
1539
1540 gmch &= ~((gen < 8 ? 0x1f : 0xff) << (gen < 8 ? 3 : 8));
1541
1542 if (vdev->igd_gms) {
1543 if (vdev->igd_gms <= 0x10) {
1544 gms_mb = vdev->igd_gms * 32;
1545 gmch |= vdev->igd_gms << (gen < 8 ? 3 : 8);
1546 } else {
1547 error_report("Unsupported IGD GMS value 0x%x", vdev->igd_gms);
1548 vdev->igd_gms = 0;
1549 }
1550 }
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560 bdsm_size = g_malloc(sizeof(*bdsm_size));
1561 *bdsm_size = cpu_to_le64((ggms_mb + gms_mb) * 1024 * 1024);
1562 fw_cfg_add_file(fw_cfg_find(), "etc/igd-bdsm-size",
1563 bdsm_size, sizeof(*bdsm_size));
1564
1565
1566 pci_set_long(vdev->pdev.config + IGD_GMCH, gmch);
1567 pci_set_long(vdev->pdev.wmask + IGD_GMCH, 0);
1568 pci_set_long(vdev->emulated_config_bits + IGD_GMCH, ~0);
1569
1570
1571 pci_set_long(vdev->pdev.config + IGD_BDSM, 0);
1572 pci_set_long(vdev->pdev.wmask + IGD_BDSM, ~0);
1573 pci_set_long(vdev->emulated_config_bits + IGD_BDSM, ~0);
1574
1575
1576
1577
1578
1579
1580
1581 if (pread(vdev->vbasedev.fd, &cmd_orig, sizeof(cmd_orig),
1582 vdev->config_offset + PCI_COMMAND) != sizeof(cmd_orig)) {
1583 error_report("IGD device %s - failed to read PCI command register",
1584 vdev->vbasedev.name);
1585 }
1586
1587 cmd = cmd_orig | PCI_COMMAND_IO;
1588
1589 if (pwrite(vdev->vbasedev.fd, &cmd, sizeof(cmd),
1590 vdev->config_offset + PCI_COMMAND) != sizeof(cmd)) {
1591 error_report("IGD device %s - failed to write PCI command register",
1592 vdev->vbasedev.name);
1593 }
1594
1595 for (i = 1; i < vfio_igd_gtt_max(vdev); i += 4) {
1596 vfio_region_write(&vdev->bars[4].region, 0, i, 4);
1597 vfio_region_write(&vdev->bars[4].region, 4, 0, 4);
1598 }
1599
1600 if (pwrite(vdev->vbasedev.fd, &cmd_orig, sizeof(cmd_orig),
1601 vdev->config_offset + PCI_COMMAND) != sizeof(cmd_orig)) {
1602 error_report("IGD device %s - failed to restore PCI command register",
1603 vdev->vbasedev.name);
1604 }
1605
1606 trace_vfio_pci_igd_bdsm_enabled(vdev->vbasedev.name, ggms_mb + gms_mb);
1607
1608out:
1609 g_free(rom);
1610 g_free(opregion);
1611 g_free(host);
1612 g_free(lpc);
1613}
1614
1615
1616
1617
1618void vfio_vga_quirk_setup(VFIOPCIDevice *vdev)
1619{
1620 vfio_vga_probe_ati_3c3_quirk(vdev);
1621 vfio_vga_probe_nvidia_3d0_quirk(vdev);
1622}
1623
1624void vfio_vga_quirk_exit(VFIOPCIDevice *vdev)
1625{
1626 VFIOQuirk *quirk;
1627 int i, j;
1628
1629 for (i = 0; i < ARRAY_SIZE(vdev->vga->region); i++) {
1630 QLIST_FOREACH(quirk, &vdev->vga->region[i].quirks, next) {
1631 for (j = 0; j < quirk->nr_mem; j++) {
1632 memory_region_del_subregion(&vdev->vga->region[i].mem,
1633 &quirk->mem[j]);
1634 }
1635 }
1636 }
1637}
1638
1639void vfio_vga_quirk_finalize(VFIOPCIDevice *vdev)
1640{
1641 int i, j;
1642
1643 for (i = 0; i < ARRAY_SIZE(vdev->vga->region); i++) {
1644 while (!QLIST_EMPTY(&vdev->vga->region[i].quirks)) {
1645 VFIOQuirk *quirk = QLIST_FIRST(&vdev->vga->region[i].quirks);
1646 QLIST_REMOVE(quirk, next);
1647 for (j = 0; j < quirk->nr_mem; j++) {
1648 object_unparent(OBJECT(&quirk->mem[j]));
1649 }
1650 g_free(quirk->mem);
1651 g_free(quirk->data);
1652 g_free(quirk);
1653 }
1654 }
1655}
1656
1657void vfio_bar_quirk_setup(VFIOPCIDevice *vdev, int nr)
1658{
1659 vfio_probe_ati_bar4_quirk(vdev, nr);
1660 vfio_probe_ati_bar2_quirk(vdev, nr);
1661 vfio_probe_nvidia_bar5_quirk(vdev, nr);
1662 vfio_probe_nvidia_bar0_quirk(vdev, nr);
1663 vfio_probe_rtl8168_bar2_quirk(vdev, nr);
1664 vfio_probe_igd_bar4_quirk(vdev, nr);
1665}
1666
1667void vfio_bar_quirk_exit(VFIOPCIDevice *vdev, int nr)
1668{
1669 VFIOBAR *bar = &vdev->bars[nr];
1670 VFIOQuirk *quirk;
1671 int i;
1672
1673 QLIST_FOREACH(quirk, &bar->quirks, next) {
1674 for (i = 0; i < quirk->nr_mem; i++) {
1675 memory_region_del_subregion(bar->region.mem, &quirk->mem[i]);
1676 }
1677 }
1678}
1679
1680void vfio_bar_quirk_finalize(VFIOPCIDevice *vdev, int nr)
1681{
1682 VFIOBAR *bar = &vdev->bars[nr];
1683 int i;
1684
1685 while (!QLIST_EMPTY(&bar->quirks)) {
1686 VFIOQuirk *quirk = QLIST_FIRST(&bar->quirks);
1687 QLIST_REMOVE(quirk, next);
1688 for (i = 0; i < quirk->nr_mem; i++) {
1689 object_unparent(OBJECT(&quirk->mem[i]));
1690 }
1691 g_free(quirk->mem);
1692 g_free(quirk->data);
1693 g_free(quirk);
1694 }
1695}
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722static bool vfio_radeon_smc_is_running(VFIOPCIDevice *vdev)
1723{
1724 uint32_t clk, pc_c;
1725
1726
1727
1728
1729
1730 vfio_region_write(&vdev->bars[5].region, 0x200, 0x80000004, 4);
1731 clk = vfio_region_read(&vdev->bars[5].region, 0x204, 4);
1732 vfio_region_write(&vdev->bars[5].region, 0x200, 0x80000370, 4);
1733 pc_c = vfio_region_read(&vdev->bars[5].region, 0x204, 4);
1734
1735 return (!(clk & 1) && (0x20100 <= pc_c));
1736}
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746static void vfio_radeon_set_gfx_only_reset(VFIOPCIDevice *vdev)
1747{
1748 uint32_t misc, fuse;
1749 bool a, b;
1750
1751 vfio_region_write(&vdev->bars[5].region, 0x200, 0xc00c0000, 4);
1752 fuse = vfio_region_read(&vdev->bars[5].region, 0x204, 4);
1753 b = fuse & 64;
1754
1755 vfio_region_write(&vdev->bars[5].region, 0x200, 0xc0000010, 4);
1756 misc = vfio_region_read(&vdev->bars[5].region, 0x204, 4);
1757 a = misc & 2;
1758
1759 if (a == b) {
1760 vfio_region_write(&vdev->bars[5].region, 0x204, misc ^ 2, 4);
1761 vfio_region_read(&vdev->bars[5].region, 0x204, 4);
1762 }
1763}
1764
1765static int vfio_radeon_reset(VFIOPCIDevice *vdev)
1766{
1767 PCIDevice *pdev = &vdev->pdev;
1768 int i, ret = 0;
1769 uint32_t data;
1770
1771
1772 if (vdev->vbasedev.reset_works) {
1773 trace_vfio_quirk_ati_bonaire_reset_skipped(vdev->vbasedev.name);
1774 return -ENODEV;
1775 }
1776
1777
1778 vfio_pci_write_config(pdev, PCI_COMMAND, PCI_COMMAND_MEMORY, 2);
1779
1780
1781 if (!vfio_radeon_smc_is_running(vdev)) {
1782 ret = -EINVAL;
1783 trace_vfio_quirk_ati_bonaire_reset_no_smc(vdev->vbasedev.name);
1784 goto out;
1785 }
1786
1787
1788 vfio_radeon_set_gfx_only_reset(vdev);
1789
1790
1791 vfio_pci_write_config(pdev, 0x7c, 0x39d5e86b, 4);
1792 usleep(100);
1793
1794
1795 for (i = 0; i < 100000; i++) {
1796 if (vfio_region_read(&vdev->bars[5].region, 0x5428, 4) != 0xffffffff) {
1797 goto reset_smc;
1798 }
1799 usleep(1);
1800 }
1801
1802 trace_vfio_quirk_ati_bonaire_reset_timeout(vdev->vbasedev.name);
1803
1804reset_smc:
1805
1806 vfio_region_write(&vdev->bars[5].region, 0x200, 0x80000000, 4);
1807 data = vfio_region_read(&vdev->bars[5].region, 0x204, 4);
1808 data |= 1;
1809 vfio_region_write(&vdev->bars[5].region, 0x204, data, 4);
1810
1811
1812 vfio_region_write(&vdev->bars[5].region, 0x200, 0x80000004, 4);
1813 data = vfio_region_read(&vdev->bars[5].region, 0x204, 4);
1814 data |= 1;
1815 vfio_region_write(&vdev->bars[5].region, 0x204, data, 4);
1816
1817 trace_vfio_quirk_ati_bonaire_reset_done(vdev->vbasedev.name);
1818
1819out:
1820
1821 vfio_pci_write_config(pdev, PCI_COMMAND, 0, 2);
1822
1823 return ret;
1824}
1825
1826void vfio_setup_resetfn_quirk(VFIOPCIDevice *vdev)
1827{
1828 switch (vdev->vendor_id) {
1829 case 0x1002:
1830 switch (vdev->device_id) {
1831
1832 case 0x6649:
1833 case 0x6650:
1834 case 0x6651:
1835 case 0x6658:
1836 case 0x665c:
1837 case 0x665d:
1838
1839 case 0x67A0:
1840 case 0x67A1:
1841 case 0x67A2:
1842 case 0x67A8:
1843 case 0x67A9:
1844 case 0x67AA:
1845 case 0x67B0:
1846 case 0x67B1:
1847 case 0x67B8:
1848 case 0x67B9:
1849 case 0x67BA:
1850 case 0x67BE:
1851 vdev->resetfn = vfio_radeon_reset;
1852 trace_vfio_quirk_ati_bonaire_reset(vdev->vbasedev.name);
1853 break;
1854 }
1855 break;
1856 }
1857}
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876static void get_nv_gpudirect_clique_id(Object *obj, Visitor *v,
1877 const char *name, void *opaque,
1878 Error **errp)
1879{
1880 DeviceState *dev = DEVICE(obj);
1881 Property *prop = opaque;
1882 uint8_t *ptr = qdev_get_prop_ptr(dev, prop);
1883
1884 visit_type_uint8(v, name, ptr, errp);
1885}
1886
1887static void set_nv_gpudirect_clique_id(Object *obj, Visitor *v,
1888 const char *name, void *opaque,
1889 Error **errp)
1890{
1891 DeviceState *dev = DEVICE(obj);
1892 Property *prop = opaque;
1893 uint8_t value, *ptr = qdev_get_prop_ptr(dev, prop);
1894 Error *local_err = NULL;
1895
1896 if (dev->realized) {
1897 qdev_prop_set_after_realize(dev, name, errp);
1898 return;
1899 }
1900
1901 visit_type_uint8(v, name, &value, &local_err);
1902 if (local_err) {
1903 error_propagate(errp, local_err);
1904 return;
1905 }
1906
1907 if (value & ~0xF) {
1908 error_setg(errp, "Property %s: valid range 0-15", name);
1909 return;
1910 }
1911
1912 *ptr = value;
1913}
1914
1915const PropertyInfo qdev_prop_nv_gpudirect_clique = {
1916 .name = "uint4",
1917 .description = "NVIDIA GPUDirect Clique ID (0 - 15)",
1918 .get = get_nv_gpudirect_clique_id,
1919 .set = set_nv_gpudirect_clique_id,
1920};
1921
1922static int vfio_add_nv_gpudirect_cap(VFIOPCIDevice *vdev, Error **errp)
1923{
1924 PCIDevice *pdev = &vdev->pdev;
1925 int ret, pos = 0xC8;
1926
1927 if (vdev->nv_gpudirect_clique == 0xFF) {
1928 return 0;
1929 }
1930
1931 if (!vfio_pci_is(vdev, PCI_VENDOR_ID_NVIDIA, PCI_ANY_ID)) {
1932 error_setg(errp, "NVIDIA GPUDirect Clique ID: invalid device vendor");
1933 return -EINVAL;
1934 }
1935
1936 if (pci_get_byte(pdev->config + PCI_CLASS_DEVICE + 1) !=
1937 PCI_BASE_CLASS_DISPLAY) {
1938 error_setg(errp, "NVIDIA GPUDirect Clique ID: unsupported PCI class");
1939 return -EINVAL;
1940 }
1941
1942 ret = pci_add_capability(pdev, PCI_CAP_ID_VNDR, pos, 8, errp);
1943 if (ret < 0) {
1944 error_prepend(errp, "Failed to add NVIDIA GPUDirect cap: ");
1945 return ret;
1946 }
1947
1948 memset(vdev->emulated_config_bits + pos, 0xFF, 8);
1949 pos += PCI_CAP_FLAGS;
1950 pci_set_byte(pdev->config + pos++, 8);
1951 pci_set_byte(pdev->config + pos++, 'P');
1952 pci_set_byte(pdev->config + pos++, '2');
1953 pci_set_byte(pdev->config + pos++, 'P');
1954 pci_set_byte(pdev->config + pos++, vdev->nv_gpudirect_clique << 3);
1955 pci_set_byte(pdev->config + pos, 0);
1956
1957 return 0;
1958}
1959
1960int vfio_add_virt_caps(VFIOPCIDevice *vdev, Error **errp)
1961{
1962 int ret;
1963
1964 ret = vfio_add_nv_gpudirect_cap(vdev, errp);
1965 if (ret) {
1966 return ret;
1967 }
1968
1969 return 0;
1970}
1971