xref: /kvmtool/pci.c (revision 3d62dea656058a6bc9248124521c6fc81efc6474)
1 #include "kvm/pci.h"
2 #include "kvm/ioport.h"
3 #include "kvm/util.h"
4 
5 #include <assert.h>
6 
7 #define PCI_MAX_DEVICES			256
8 
9 static struct pci_device_header		*pci_devices[PCI_MAX_DEVICES];
10 
11 static struct pci_config_address	pci_config_address;
12 
13 static void *pci_config_address_ptr(u16 port)
14 {
15 	unsigned long offset;
16 	void *base;
17 
18 	offset		= port - PCI_CONFIG_ADDRESS;
19 	base		= &pci_config_address;
20 
21 	return base + offset;
22 }
23 
24 static bool pci_config_address_out(struct ioport *ioport, struct kvm *kvm, u16 port, void *data, int size, u32 count)
25 {
26 	void *p = pci_config_address_ptr(port);
27 
28 	memcpy(p, data, size);
29 
30 	return true;
31 }
32 
33 static bool pci_config_address_in(struct ioport *ioport, struct kvm *kvm, u16 port, void *data, int size, u32 count)
34 {
35 	void *p = pci_config_address_ptr(port);
36 
37 	memcpy(data, p, size);
38 
39 	return true;
40 }
41 
42 static struct ioport_operations pci_config_address_ops = {
43 	.io_in		= pci_config_address_in,
44 	.io_out		= pci_config_address_out,
45 };
46 
47 static bool pci_config_data_out(struct ioport *ioport, struct kvm *kvm, u16 port, void *data, int size, u32 count)
48 {
49 	return true;
50 }
51 
52 static bool pci_device_exists(u8 bus_number, u8 device_number, u8 function_number)
53 {
54 	struct pci_device_header *dev;
55 
56 	if (pci_config_address.bus_number != bus_number)
57 		return false;
58 
59 	if (pci_config_address.function_number != function_number)
60 		return false;
61 
62 	if (device_number >= PCI_MAX_DEVICES)
63 		return false;
64 
65 	dev		= pci_devices[device_number];
66 
67 	return dev != NULL;
68 }
69 
70 static bool pci_config_data_in(struct ioport *ioport, struct kvm *kvm, u16 port, void *data, int size, u32 count)
71 {
72 	unsigned long start;
73 	u8 dev_num;
74 
75 	/*
76 	 * If someone accesses PCI configuration space offsets that are not
77 	 * aligned to 4 bytes, it uses ioports to signify that.
78 	 */
79 	start = port - PCI_CONFIG_DATA;
80 
81 	dev_num		= pci_config_address.device_number;
82 
83 	if (pci_device_exists(0, dev_num, 0)) {
84 		unsigned long offset;
85 
86 		offset = start + (pci_config_address.register_number << 2);
87 		if (offset < sizeof(struct pci_device_header)) {
88 			void *p = pci_devices[dev_num];
89 
90 			memcpy(data, p + offset, size);
91 		} else
92 			memset(data, 0x00, size);
93 	} else
94 		memset(data, 0xff, size);
95 
96 	return true;
97 }
98 
99 static struct ioport_operations pci_config_data_ops = {
100 	.io_in		= pci_config_data_in,
101 	.io_out		= pci_config_data_out,
102 };
103 
104 void pci__register(struct pci_device_header *dev, u8 dev_num)
105 {
106 	assert(dev_num < PCI_MAX_DEVICES);
107 
108 	pci_devices[dev_num]	= dev;
109 }
110 
111 void pci__init(void)
112 {
113 	ioport__register(PCI_CONFIG_DATA + 0, &pci_config_data_ops, 4, NULL);
114 	ioport__register(PCI_CONFIG_ADDRESS + 0, &pci_config_address_ops, 4, NULL);
115 }
116