xref: /openbmc/linux/drivers/vfio/pci/vfio_pci_igd.c (revision 5846ff54)
1 /*
2  * VFIO PCI Intel Graphics support
3  *
4  * Copyright (C) 2016 Red Hat, Inc.  All rights reserved.
5  *	Author: Alex Williamson <alex.williamson@redhat.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  *
11  * Register a device specific region through which to provide read-only
12  * access to the Intel IGD opregion.  The register defining the opregion
13  * address is also virtualized to prevent user modification.
14  */
15 
16 #include <linux/io.h>
17 #include <linux/pci.h>
18 #include <linux/uaccess.h>
19 #include <linux/vfio.h>
20 
21 #include "vfio_pci_private.h"
22 
23 #define OPREGION_SIGNATURE	"IntelGraphicsMem"
24 #define OPREGION_SIZE		(8 * 1024)
25 #define OPREGION_PCI_ADDR	0xfc
26 
27 static size_t vfio_pci_igd_rw(struct vfio_pci_device *vdev, char __user *buf,
28 			      size_t count, loff_t *ppos, bool iswrite)
29 {
30 	unsigned int i = VFIO_PCI_OFFSET_TO_INDEX(*ppos) - VFIO_PCI_NUM_REGIONS;
31 	void *base = vdev->region[i].data;
32 	loff_t pos = *ppos & VFIO_PCI_OFFSET_MASK;
33 
34 	if (pos >= vdev->region[i].size || iswrite)
35 		return -EINVAL;
36 
37 	count = min(count, (size_t)(vdev->region[i].size - pos));
38 
39 	if (copy_to_user(buf, base + pos, count))
40 		return -EFAULT;
41 
42 	*ppos += count;
43 
44 	return count;
45 }
46 
47 static void vfio_pci_igd_release(struct vfio_pci_device *vdev,
48 				 struct vfio_pci_region *region)
49 {
50 	memunmap(region->data);
51 }
52 
53 static const struct vfio_pci_regops vfio_pci_igd_regops = {
54 	.rw		= vfio_pci_igd_rw,
55 	.release	= vfio_pci_igd_release,
56 };
57 
58 int vfio_pci_igd_opregion_init(struct vfio_pci_device *vdev)
59 {
60 	__le32 *dwordp = (__le32 *)(vdev->vconfig + OPREGION_PCI_ADDR);
61 	u32 addr, size;
62 	void *base;
63 	int ret;
64 
65 	ret = pci_read_config_dword(vdev->pdev, OPREGION_PCI_ADDR, &addr);
66 	if (ret)
67 		return ret;
68 
69 	if (!addr || !(~addr))
70 		return -ENODEV;
71 
72 	base = memremap(addr, OPREGION_SIZE, MEMREMAP_WB);
73 	if (!base)
74 		return -ENOMEM;
75 
76 	if (memcmp(base, OPREGION_SIGNATURE, 16)) {
77 		memunmap(base);
78 		return -EINVAL;
79 	}
80 
81 	size = le32_to_cpu(*(__le32 *)(base + 16));
82 	if (!size) {
83 		memunmap(base);
84 		return -EINVAL;
85 	}
86 
87 	size *= 1024; /* In KB */
88 
89 	if (size != OPREGION_SIZE) {
90 		memunmap(base);
91 		base = memremap(addr, size, MEMREMAP_WB);
92 		if (!base)
93 			return -ENOMEM;
94 	}
95 
96 	ret = vfio_pci_register_dev_region(vdev,
97 		PCI_VENDOR_ID_INTEL | VFIO_REGION_TYPE_PCI_VENDOR_TYPE,
98 		VFIO_REGION_SUBTYPE_INTEL_IGD_OPREGION,
99 		&vfio_pci_igd_regops, size, VFIO_REGION_INFO_FLAG_READ, base);
100 	if (ret) {
101 		memunmap(base);
102 		return ret;
103 	}
104 
105 	/* Fill vconfig with the hw value and virtualize register */
106 	*dwordp = cpu_to_le32(addr);
107 	memset(vdev->pci_config_map + OPREGION_PCI_ADDR,
108 	       PCI_CAP_ID_INVALID_VIRT, 4);
109 
110 	return ret;
111 }
112