Skip to contentSkip to conversation

hvmloader: Intel GPU passthrough, reverse OpRegion

PATCH In review Jean Guyader 5 participants 9 messages Nov 23, 2011 – Nov 24, 2011

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hvmloader: Intel GPU passthrough, reverse OpRegion

The Intel GPU uses a two pages NVS region called OpRegion. In order to get full support for the driver in the guest we need to map this region.

This patch reserves 2 pages on the top of the RAM and mark this region as NVS in the e820. Then we write the address to the config space (offset 0xfc) so the device model can map the OpRegion at this address in the guest.

---
 tools/firmware/hvmloader/config.h   |    1 +
 tools/firmware/hvmloader/e820.c     |    8 ++++++++
 tools/firmware/hvmloader/pci.c      |   28 ++++++++++++++++++++++++++++
 tools/firmware/hvmloader/pci_regs.h |    2 ++
 4 files changed, 39 insertions(+), 0 deletions(-)
tools/firmware/hvmloader/config.h +1 -0
index d911352..9cac9c1 100644
@@ -5,6 +5,7 @@
 
 enum virtual_vga { VGA_none, VGA_std, VGA_cirrus, VGA_pt };
 extern enum virtual_vga virtual_vga;
+extern unsigned long igd_opregion_pgbase;
 
 struct bios_config {
     const char *name;
tools/firmware/hvmloader/e820.c +8 -0
index 3b50dd0..fd993fe 100644
@@ -124,6 +124,14 @@ int build_e820_table(struct e820entry *e820,
     e820[nr].type = E820_RAM;
     nr++;
 
+    if ( igd_opregion_pgbase )
+    {
+        e820[nr].addr = igd_opregion_pgbase << PAGE_SHIFT;
+        e820[nr].size = 2 * PAGE_SIZE;
+        e820[nr].type = E820_NVS;
+        nr++;
+    }
+
     /*
      * Explicitly reserve space for special pages.
      * This space starts at RESERVED_MEMBASE an extends to cover various
tools/firmware/hvmloader/pci.c +28 -0
index 00490f1..13d3822 100644
@@ -32,6 +32,7 @@ unsigned long pci_mem_start = PCI_MEM_START;
 unsigned long pci_mem_end = PCI_MEM_END;
 
 enum virtual_vga virtual_vga = VGA_none;
+unsigned long igd_opregion_pgbase = 0;
 
 void pci_setup(void)
 {
@@ -271,6 +272,33 @@ void pci_setup(void)
         cmd |= PCI_COMMAND_IO;
         pci_writew(vga_devfn, PCI_COMMAND, cmd);
     }
+
+    if ( virtual_vga == VGA_pt )
+    {
+        class     = pci_readw(vga_devfn, PCI_CLASS_DEVICE);
+        vendor_id = pci_readw(vga_devfn, PCI_VENDOR_ID);
+        device_id = pci_readw(vga_devfn, PCI_DEVICE_ID);
+
+        switch ( vendor_id )
+        {
+            case 0x8086: /* PCI_VENDOR_INTEL */
+                for ( i = 0; i < 2; i++ )
+                {
+                    struct xen_add_to_physmap xatp;
+                    xatp.domid = DOMID_SELF;
+                    xatp.space = XENMAPSPACE_gmfn;
+                    xatp.idx   = --hvm_info->low_mem_pgend;
+                    xatp.gpfn  = hvm_info->high_mem_pgend++;
+                    if ( hypercall_memory_op(XENMEM_add_to_physmap, &xatp) != 0 )
+                        BUG();
+                }
+                igd_opregion_pgbase = hvm_info->low_mem_pgend;
+                pci_writel(vga_devfn, PCI_INTEL_OPREGION,
+                        igd_opregion_pgbase << PAGE_SHIFT);
+                break;
+        }
+    }
+
 }
 
 /*
tools/firmware/hvmloader/pci_regs.h +2 -0
index f37affe..873255c 100644
@@ -105,6 +105,8 @@
 #define PCI_MIN_GNT  0x3e /* 8 bits */
 #define PCI_MAX_LAT  0x3f /* 8 bits */
 
+#define PCI_INTEL_OPREGION 0xfc /* 32 bits */
+
 #endif /* __HVMLOADER_PCI_REGS_H__ */
 
 /*

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

#1 lore raw
Quoted 9 lines · Jean Guyader

The Intel GPU uses a two pages NVS region called OpRegion. In order to get full support for the driver in the guest we need to map this region.

This patch reserves 2 pages on the top of the RAM and mark this region as NVS in the e820. Then we write the address to the config space (offset 0xfc) so the device model can map the OpRegion at this address in the guest.

Please use mem_hole_alloc() rather than adjusting {low,high}_mem_pgend.

Is it correct to do this for *all* gfx devices with Intel vendor id?

-- Keir

Quoted 12 lines
---
 tools/firmware/hvmloader/config.h   |    1 +
 tools/firmware/hvmloader/e820.c     |    8 ++++++++
 tools/firmware/hvmloader/pci.c      |   28 ++++++++++++++++++++++++++++
 tools/firmware/hvmloader/pci_regs.h |    2 ++
 4 files changed, 39 insertions(+), 0 deletions(-)

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

#2 lore raw
Quoted 7 lines · Keir Fraser
Quoted 5 lines · Jean Guyader

The Intel GPU uses a two pages NVS region called OpRegion. In order to get full support for the driver in the guest we need to map this region.

Quoted 7 lines
Quoted 4 lines

This patch reserves 2 pages on the top of the RAM and mark this region as NVS in the e820. Then we write the address to the config space (offset 0xfc) so the device model can map the OpRegion at this address in the guest.

Please use mem_hole_alloc() rather than adjusting {low,high}_mem_pgend.

Ok, that is handy.

Quoted 2 lines

Is it correct to do this for *all* gfx devices with Intel vendor id?

The OpRegion is a Intel GPU specific thing.

Jean

#3 lore raw
Quoted 16 lines · Jean Guyader
Quoted 7 lines · Keir Fraser
Quoted 5 lines · Jean Guyader

The Intel GPU uses a two pages NVS region called OpRegion. In order to get full support for the driver in the guest we need to map this region.

Quoted 7 lines
Quoted 4 lines

This patch reserves 2 pages on the top of the RAM and mark this region as NVS in the e820. Then we write the address to the config space (offset 0xfc) so the device model can map the OpRegion at this address in the guest.

Please use mem_hole_alloc() rather than adjusting {low,high}_mem_pgend.

Quoted 7 lines

Ok, that is handy.

Quoted 2 lines

Is it correct to do this for *all* gfx devices with Intel vendor id?

The OpRegion is a Intel GPU specific thing.

Sorry didn't read carefully the first time, yes I think it's correct to do that for
all the Intel GPU. Maybe I can do a read on 0xfc first to check if I don't get something dodgy like 0xfffffff or 0. I could also double check in Qemu that it's a NVS region on the host, but that won't work for stub domain.

Jean

#4 lore raw
Quoted 22 lines · Jean Guyader
Quoted 21 lines · Jean Guyader
Quoted 7 lines · Keir Fraser
Quoted 5 lines · Jean Guyader

The Intel GPU uses a two pages NVS region called OpRegion. In order to get full support for the driver in the guest we need to map this region.

Quoted 7 lines
Quoted 4 lines

This patch reserves 2 pages on the top of the RAM and mark this region as NVS in the e820. Then we write the address to the config space (offset 0xfc) so the device model can map the OpRegion at this address in the guest.

Please use mem_hole_alloc() rather than adjusting {low,high}_mem_pgend.

Ok, that is handy.

Quoted 2 lines

Is it correct to do this for *all* gfx devices with Intel vendor id?

Quoted 8 lines
Quoted 2 lines

The OpRegion is a Intel GPU specific thing.

Sorry didn't read carefully the first time, yes I think it's correct to do that for
all the Intel GPU. Maybe I can do a read on 0xfc first to check if I don't get something dodgy like 0xfffffff or 0. I could also double check in Qemu that it's a NVS region on the host, but that won't work for stub domain.

access to physical memory through /dev/mem should work from the stubdom

#5 lore raw
Quoted 26 lines · Stefano Stabellini
Quoted 25 lines · Jean Guyader
Quoted 23 lines · Jean Guyader
Quoted 7 lines · Keir Fraser
Quoted 5 lines · Jean Guyader

The Intel GPU uses a two pages NVS region called OpRegion. In order to get full support for the driver in the guest we need to map this region.

Quoted 7 lines
Quoted 4 lines

This patch reserves 2 pages on the top of the RAM and mark this region as NVS in the e820. Then we write the address to the config space (offset 0xfc) so the device model can map the OpRegion at this address in the guest.

Please use mem_hole_alloc() rather than adjusting {low,high}_mem_pgend.

Ok, that is handy.

Quoted 2 lines

Is it correct to do this for *all* gfx devices with Intel vendor id?

The OpRegion is a Intel GPU specific thing.

Quoted 7 lines
Quoted 5 lines

Sorry didn't read carefully the first time, yes I think it's correct to do that for
all the Intel GPU. Maybe I can do a read on 0xfc first to check if I don't get something dodgy like 0xfffffff or 0. I could also double check in Qemu that it's a NVS region on the host, but that won't work for stub domain.

access to physical memory through /dev/mem should work from the stubdom

I would think that /dev/mem in a stubdom will expose the memory of the guest but maybe I'm wrong.

Jean

#6 lore raw
Quoted 33 lines · Jean Guyader
Quoted 32 lines · Stefano Stabellini
Quoted 30 lines · Jean Guyader
Quoted 16 lines · Jean Guyader
Quoted 7 lines · Keir Fraser
Quoted 5 lines · Jean Guyader

The Intel GPU uses a two pages NVS region called OpRegion. In order to get full support for the driver in the guest we need to map this region.

Quoted 7 lines
Quoted 4 lines

This patch reserves 2 pages on the top of the RAM and mark this region as NVS in the e820. Then we write the address to the config space (offset 0xfc) so the device model can map the OpRegion at this address in the guest.

Please use mem_hole_alloc() rather than adjusting {low,high}_mem_pgend.

Quoted 7 lines

Ok, that is handy.

Quoted 2 lines

Is it correct to do this for *all* gfx devices with Intel vendor id?

The OpRegion is a Intel GPU specific thing.

Sorry didn't read carefully the first time, yes I think it's correct to do that for
all the Intel GPU. Maybe I can do a read on 0xfc first to check if I don't get something dodgy like 0xfffffff or 0. I could also double check in Qemu that it's a NVS region on the host, but that won't work for stub domain.

Quoted 5 lines
Quoted 1 line

access to physical memory through /dev/mem should work from the stubdom

I would think that /dev/mem in a stubdom will expose the memory of the guest but maybe I'm wrong.

Nope, it maps host memory. Of course you need to make sure you have given enough privileges to the stubdom so that it can actually map the memory area.

#7 lore raw
In reply to Keir Fraser
Quoted 7 lines · Keir Fraser
Quoted 5 lines · Jean Guyader

The Intel GPU uses a two pages NVS region called OpRegion. In order to get full support for the driver in the guest we need to map this region.

Quoted 7 lines
Quoted 4 lines

This patch reserves 2 pages on the top of the RAM and mark this region as NVS in the e820. Then we write the address to the config space (offset 0xfc) so the device model can map the OpRegion at this address in the guest.

Please use mem_hole_alloc() rather than adjusting {low,high}_mem_pgend.

I'm calling mem_hole_alloc() in pci_setup (see patch attached), but that causes an overlap in e820, is that expected?

(XEN) HVM5: E820 table:

(XEN) HVM5:  [00]: 00000000:00000000 - 00000000:0009e000: RAM
(XEN) HVM5:  [01]: 00000000:0009e000 - 00000000:000a0000: RESERVED
(XEN) HVM5:  HOLE: 00000000:000a0000 - 00000000:000e0000
(XEN) HVM5:  [02]: 00000000:000e0000 - 00000000:00100000: RESERVED
(XEN) HVM5:  [03]: 00000000:00100000 - 00000000:3f800000: RAM
(XEN) HVM5:  HOLE: 00000000:3f800000 - 00000000:feff8000
(XEN) HVM5:  [04]: 00000000:feff8000 - 00000000:feffa000: NVS
(XEN) HVM5:  OVERLAP!!
(XEN) HVM5:  [05]: 00000000:fc000000 - 00000001:00000000: RESERVED

Jean

#8 lore raw
Quoted 13 lines · Jean Guyader
Quoted 12 lines · Keir Fraser
Quoted 9 lines · Jean Guyader

The Intel GPU uses a two pages NVS region called OpRegion. In order to get full support for the driver in the guest we need to map this region.

This patch reserves 2 pages on the top of the RAM and mark this region as NVS in the e820. Then we write the address to the config space (offset 0xfc) so the device model can map the OpRegion at this address in the guest.

Quoted 5 lines
Quoted 2 lines

Please use mem_hole_alloc() rather than adjusting {low,high}_mem_pgend.

I'm calling mem_hole_alloc() in pci_setup (see patch attached), but that causes an overlap in e820, is that expected?

You'll have to adjust your changes to build_e820_table() to split the range RESERVED_MEMBASE-0x10000000 into two pieces partitioned by your NVS region.

The region starting at RESERVED_MEMBASE comes *before* your NVS region. Then you add an another reserved region up to 0x1000000 if your NVS region exists.

-- Keir

Quoted 12 lines

(XEN) HVM5: E820 table:

(XEN) HVM5:  [00]: 00000000:00000000 - 00000000:0009e000: RAM
(XEN) HVM5:  [01]: 00000000:0009e000 - 00000000:000a0000: RESERVED
(XEN) HVM5:  HOLE: 00000000:000a0000 - 00000000:000e0000
(XEN) HVM5:  [02]: 00000000:000e0000 - 00000000:00100000: RESERVED
(XEN) HVM5:  [03]: 00000000:00100000 - 00000000:3f800000: RAM
(XEN) HVM5:  HOLE: 00000000:3f800000 - 00000000:feff8000
(XEN) HVM5:  [04]: 00000000:feff8000 - 00000000:feffa000: NVS
(XEN) HVM5:  OVERLAP!!
(XEN) HVM5:  [05]: 00000000:fc000000 - 00000001:00000000: RESERVED

Jean