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