xref: /kvmtool/hw/vesa.c (revision 8f46c736eae508f0973d41618db17cced6b5796c)
1 #include "kvm/vesa.h"
2 
3 #include "kvm/devices.h"
4 #include "kvm/virtio-pci-dev.h"
5 #include "kvm/framebuffer.h"
6 #include "kvm/kvm-cpu.h"
7 #include "kvm/ioport.h"
8 #include "kvm/util.h"
9 #include "kvm/irq.h"
10 #include "kvm/kvm.h"
11 #include "kvm/pci.h"
12 
13 #include <linux/byteorder.h>
14 #include <sys/mman.h>
15 #include <linux/err.h>
16 #include <sys/types.h>
17 #include <sys/ioctl.h>
18 #include <inttypes.h>
19 #include <unistd.h>
20 
21 static bool vesa_pci_io_in(struct ioport *ioport, struct kvm_cpu *vcpu, u16 port, void *data, int size)
22 {
23 	return true;
24 }
25 
26 static bool vesa_pci_io_out(struct ioport *ioport, struct kvm_cpu *vcpu, u16 port, void *data, int size)
27 {
28 	return true;
29 }
30 
31 static struct ioport_operations vesa_io_ops = {
32 	.io_in			= vesa_pci_io_in,
33 	.io_out			= vesa_pci_io_out,
34 };
35 
36 static struct pci_device_header vesa_pci_device = {
37 	.vendor_id		= cpu_to_le16(PCI_VENDOR_ID_REDHAT_QUMRANET),
38 	.device_id		= cpu_to_le16(PCI_DEVICE_ID_VESA),
39 	.header_type		= PCI_HEADER_TYPE_NORMAL,
40 	.revision_id		= 0,
41 	.class[2]		= 0x03,
42 	.subsys_vendor_id	= cpu_to_le16(PCI_SUBSYSTEM_VENDOR_ID_REDHAT_QUMRANET),
43 	.subsys_id		= cpu_to_le16(PCI_SUBSYSTEM_ID_VESA),
44 	.bar[1]			= cpu_to_le32(VESA_MEM_ADDR | PCI_BASE_ADDRESS_SPACE_MEMORY),
45 	.bar_size[1]		= VESA_MEM_SIZE,
46 };
47 
48 static struct device_header vesa_device = {
49 	.bus_type	= DEVICE_BUS_PCI,
50 	.data		= &vesa_pci_device,
51 };
52 
53 static struct framebuffer vesafb;
54 
55 struct framebuffer *vesa__init(struct kvm *kvm)
56 {
57 	u16 vesa_base_addr;
58 	char *mem;
59 	int r;
60 
61 	if (!kvm->cfg.vnc && !kvm->cfg.sdl && !kvm->cfg.gtk)
62 		return NULL;
63 
64 	r = ioport__register(kvm, IOPORT_EMPTY, &vesa_io_ops, IOPORT_SIZE, NULL);
65 	if (r < 0)
66 		return ERR_PTR(r);
67 
68 	vesa_base_addr			= (u16)r;
69 	vesa_pci_device.bar[0]		= cpu_to_le32(vesa_base_addr | PCI_BASE_ADDRESS_SPACE_IO);
70 	device__register(&vesa_device);
71 
72 	mem = mmap(NULL, VESA_MEM_SIZE, PROT_RW, MAP_ANON_NORESERVE, -1, 0);
73 	if (mem == MAP_FAILED)
74 		ERR_PTR(-errno);
75 
76 	kvm__register_dev_mem(kvm, VESA_MEM_ADDR, VESA_MEM_SIZE, mem);
77 
78 	vesafb = (struct framebuffer) {
79 		.width			= VESA_WIDTH,
80 		.height			= VESA_HEIGHT,
81 		.depth			= VESA_BPP,
82 		.mem			= mem,
83 		.mem_addr		= VESA_MEM_ADDR,
84 		.mem_size		= VESA_MEM_SIZE,
85 		.kvm			= kvm,
86 	};
87 	return fb__register(&vesafb);
88 }
89