xref: /kvmtool/pci.c (revision ff01b5dbbd040d39619f0f033da6eac5dbed3b2b)
121ff329dSWill Deacon #include "kvm/devices.h"
260742802SPekka Enberg #include "kvm/pci.h"
360742802SPekka Enberg #include "kvm/ioport.h"
4b5981636SWill Deacon #include "kvm/irq.h"
576f9c841SCyrill Gorcunov #include "kvm/util.h"
69575e724SSasha Levin #include "kvm/kvm.h"
760742802SPekka Enberg 
86d987703SSasha Levin #include <linux/err.h>
96d987703SSasha Levin #include <assert.h>
106d987703SSasha Levin 
11a0a7d66fSDavid Daney static u32 pci_config_address_bits;
1260742802SPekka Enberg 
1340f2fd06SMatt Evans /* This is within our PCI gap - in an unused area.
1440f2fd06SMatt Evans  * Note this is a PCI *bus address*, is used to assign BARs etc.!
1540f2fd06SMatt Evans  * (That's why it can still 32bit even with 64bit guests-- 64bit
1640f2fd06SMatt Evans  * PCI isn't currently supported.)
1740f2fd06SMatt Evans  */
1840f2fd06SMatt Evans static u32 io_space_blocks		= KVM_PCI_MMIO_AREA;
199575e724SSasha Levin 
20c7575d17SWill Deacon /*
21c7575d17SWill Deacon  * BARs must be naturally aligned, so enforce this in the allocator.
22c7575d17SWill Deacon  */
2395d13a52SSasha Levin u32 pci_get_io_space_block(u32 size)
249575e724SSasha Levin {
25c7575d17SWill Deacon 	u32 block = ALIGN(io_space_blocks, size);
26c7575d17SWill Deacon 	io_space_blocks = block + size;
279575e724SSasha Levin 	return block;
289575e724SSasha Levin }
299575e724SSasha Levin 
30b5981636SWill Deacon void pci__assign_irq(struct device_header *dev_hdr)
31b5981636SWill Deacon {
32b5981636SWill Deacon 	struct pci_device_header *pci_hdr = dev_hdr->data;
33b5981636SWill Deacon 
34b5981636SWill Deacon 	/*
35b5981636SWill Deacon 	 * PCI supports only INTA#,B#,C#,D# per device.
36b5981636SWill Deacon 	 *
37b5981636SWill Deacon 	 * A#,B#,C#,D# are allowed for multifunctional devices so stick
38b5981636SWill Deacon 	 * with A# for our single function devices.
39b5981636SWill Deacon 	 */
40b5981636SWill Deacon 	pci_hdr->irq_pin	= 1;
41b5981636SWill Deacon 	pci_hdr->irq_line	= irq__alloc_line();
42*ff01b5dbSJean-Philippe Brucker 
43*ff01b5dbSJean-Philippe Brucker 	if (!pci_hdr->irq_type)
44*ff01b5dbSJean-Philippe Brucker 		pci_hdr->irq_type = IRQ_TYPE_EDGE_RISING;
45b5981636SWill Deacon }
46b5981636SWill Deacon 
473fdf659dSSasha Levin static void *pci_config_address_ptr(u16 port)
48ba824677SPekka Enberg {
49ba824677SPekka Enberg 	unsigned long offset;
50ba824677SPekka Enberg 	void *base;
51ba824677SPekka Enberg 
52ba824677SPekka Enberg 	offset	= port - PCI_CONFIG_ADDRESS;
53a0a7d66fSDavid Daney 	base	= &pci_config_address_bits;
54ba824677SPekka Enberg 
55ba824677SPekka Enberg 	return base + offset;
56ba824677SPekka Enberg }
57ba824677SPekka Enberg 
584123ca55SMarc Zyngier static bool pci_config_address_out(struct ioport *ioport, struct kvm_cpu *vcpu, u16 port, void *data, int size)
5960742802SPekka Enberg {
60ba824677SPekka Enberg 	void *p = pci_config_address_ptr(port);
6160742802SPekka Enberg 
62ba824677SPekka Enberg 	memcpy(p, data, size);
6360742802SPekka Enberg 
6460742802SPekka Enberg 	return true;
6560742802SPekka Enberg }
6660742802SPekka Enberg 
674123ca55SMarc Zyngier static bool pci_config_address_in(struct ioport *ioport, struct kvm_cpu *vcpu, u16 port, void *data, int size)
6860742802SPekka Enberg {
69ba824677SPekka Enberg 	void *p = pci_config_address_ptr(port);
7060742802SPekka Enberg 
71ba824677SPekka Enberg 	memcpy(data, p, size);
7260742802SPekka Enberg 
7360742802SPekka Enberg 	return true;
7460742802SPekka Enberg }
7560742802SPekka Enberg 
76305b72ceSCyrill Gorcunov static struct ioport_operations pci_config_address_ops = {
77305b72ceSCyrill Gorcunov 	.io_in	= pci_config_address_in,
78305b72ceSCyrill Gorcunov 	.io_out	= pci_config_address_out,
7960742802SPekka Enberg };
8060742802SPekka Enberg 
813fdf659dSSasha Levin static bool pci_device_exists(u8 bus_number, u8 device_number, u8 function_number)
8276f9c841SCyrill Gorcunov {
83a0a7d66fSDavid Daney 	union pci_config_address pci_config_address;
84a0a7d66fSDavid Daney 
85a0a7d66fSDavid Daney 	pci_config_address.w = ioport__read32(&pci_config_address_bits);
86a0a7d66fSDavid Daney 
8776f9c841SCyrill Gorcunov 	if (pci_config_address.bus_number != bus_number)
8876f9c841SCyrill Gorcunov 		return false;
8976f9c841SCyrill Gorcunov 
90b30d05adSPekka Enberg 	if (pci_config_address.function_number != function_number)
9176f9c841SCyrill Gorcunov 		return false;
9276f9c841SCyrill Gorcunov 
9321ff329dSWill Deacon 	return !IS_ERR_OR_NULL(device__find_dev(DEVICE_BUS_PCI, device_number));
9476f9c841SCyrill Gorcunov }
9576f9c841SCyrill Gorcunov 
964123ca55SMarc Zyngier static bool pci_config_data_out(struct ioport *ioport, struct kvm_cpu *vcpu, u16 port, void *data, int size)
979575e724SSasha Levin {
98a0a7d66fSDavid Daney 	union pci_config_address pci_config_address;
99a0a7d66fSDavid Daney 
100a0a7d66fSDavid Daney 	pci_config_address.w = ioport__read32(&pci_config_address_bits);
1019575e724SSasha Levin 	/*
1029575e724SSasha Levin 	 * If someone accesses PCI configuration space offsets that are not
1039575e724SSasha Levin 	 * aligned to 4 bytes, it uses ioports to signify that.
1049575e724SSasha Levin 	 */
105d0297a59SMatt Evans 	pci_config_address.reg_offset = port - PCI_CONFIG_DATA;
1069575e724SSasha Levin 
1074123ca55SMarc Zyngier 	pci__config_wr(vcpu->kvm, pci_config_address, data, size);
108d0297a59SMatt Evans 
109d0297a59SMatt Evans 	return true;
110d0297a59SMatt Evans }
111d0297a59SMatt Evans 
1124123ca55SMarc Zyngier static bool pci_config_data_in(struct ioport *ioport, struct kvm_cpu *vcpu, u16 port, void *data, int size)
113d0297a59SMatt Evans {
114a0a7d66fSDavid Daney 	union pci_config_address pci_config_address;
115a0a7d66fSDavid Daney 
116a0a7d66fSDavid Daney 	pci_config_address.w = ioport__read32(&pci_config_address_bits);
117d0297a59SMatt Evans 	/*
118d0297a59SMatt Evans 	 * If someone accesses PCI configuration space offsets that are not
119d0297a59SMatt Evans 	 * aligned to 4 bytes, it uses ioports to signify that.
120d0297a59SMatt Evans 	 */
121d0297a59SMatt Evans 	pci_config_address.reg_offset = port - PCI_CONFIG_DATA;
122d0297a59SMatt Evans 
1234123ca55SMarc Zyngier 	pci__config_rd(vcpu->kvm, pci_config_address, data, size);
124d0297a59SMatt Evans 
125d0297a59SMatt Evans 	return true;
126d0297a59SMatt Evans }
127d0297a59SMatt Evans 
128d0297a59SMatt Evans static struct ioport_operations pci_config_data_ops = {
129d0297a59SMatt Evans 	.io_in	= pci_config_data_in,
130d0297a59SMatt Evans 	.io_out	= pci_config_data_out,
131d0297a59SMatt Evans };
132d0297a59SMatt Evans 
133d0297a59SMatt Evans void pci__config_wr(struct kvm *kvm, union pci_config_address addr, void *data, int size)
134d0297a59SMatt Evans {
135023fdaaeSJean-Philippe Brucker 	void *base;
136023fdaaeSJean-Philippe Brucker 	u8 bar, offset;
137023fdaaeSJean-Philippe Brucker 	struct pci_device_header *pci_hdr;
138023fdaaeSJean-Philippe Brucker 	u8 dev_num = addr.device_number;
139d0297a59SMatt Evans 
140023fdaaeSJean-Philippe Brucker 	if (!pci_device_exists(addr.bus_number, dev_num, 0))
141023fdaaeSJean-Philippe Brucker 		return;
1429575e724SSasha Levin 
143023fdaaeSJean-Philippe Brucker 	offset = addr.w & PCI_DEV_CFG_MASK;
144023fdaaeSJean-Philippe Brucker 	base = pci_hdr = device__find_dev(DEVICE_BUS_PCI, dev_num)->data;
1459575e724SSasha Levin 
146023fdaaeSJean-Philippe Brucker 	if (pci_hdr->cfg_ops.write)
147023fdaaeSJean-Philippe Brucker 		pci_hdr->cfg_ops.write(kvm, pci_hdr, offset, data, size);
148c64f7ff0SSasha Levin 
1499575e724SSasha Levin 	/*
150023fdaaeSJean-Philippe Brucker 	 * legacy hack: ignore writes to uninitialized regions (e.g. ROM BAR).
151023fdaaeSJean-Philippe Brucker 	 * Not very nice but has been working so far.
1529575e724SSasha Levin 	 */
153023fdaaeSJean-Philippe Brucker 	if (*(u32 *)(base + offset) == 0)
154023fdaaeSJean-Philippe Brucker 		return;
155023fdaaeSJean-Philippe Brucker 
156023fdaaeSJean-Philippe Brucker 	bar = (offset - PCI_BAR_OFFSET(0)) / sizeof(u32);
157023fdaaeSJean-Philippe Brucker 
158023fdaaeSJean-Philippe Brucker 	/*
159023fdaaeSJean-Philippe Brucker 	 * If the kernel masks the BAR it would expect to find the size of the
160023fdaaeSJean-Philippe Brucker 	 * BAR there next time it reads from it. When the kernel got the size it
161023fdaaeSJean-Philippe Brucker 	 * would write the address back.
162023fdaaeSJean-Philippe Brucker 	 */
163023fdaaeSJean-Philippe Brucker 	if (bar < 6 && ioport__read32(data) == 0xFFFFFFFF) {
164023fdaaeSJean-Philippe Brucker 		u32 sz = pci_hdr->bar_size[bar];
165023fdaaeSJean-Philippe Brucker 		memcpy(base + offset, &sz, sizeof(sz));
166023fdaaeSJean-Philippe Brucker 	} else {
167023fdaaeSJean-Philippe Brucker 		memcpy(base + offset, data, size);
1689575e724SSasha Levin 	}
1699575e724SSasha Levin }
1709575e724SSasha Levin 
171d0297a59SMatt Evans void pci__config_rd(struct kvm *kvm, union pci_config_address addr, void *data, int size)
17260742802SPekka Enberg {
173023fdaaeSJean-Philippe Brucker 	u8 offset;
174023fdaaeSJean-Philippe Brucker 	struct pci_device_header *pci_hdr;
175023fdaaeSJean-Philippe Brucker 	u8 dev_num = addr.device_number;
176e4d2cea2SPekka Enberg 
177023fdaaeSJean-Philippe Brucker 	if (pci_device_exists(addr.bus_number, dev_num, 0)) {
178023fdaaeSJean-Philippe Brucker 		pci_hdr = device__find_dev(DEVICE_BUS_PCI, dev_num)->data;
179023fdaaeSJean-Philippe Brucker 		offset = addr.w & PCI_DEV_CFG_MASK;
180b30d05adSPekka Enberg 
181023fdaaeSJean-Philippe Brucker 		if (pci_hdr->cfg_ops.read)
182023fdaaeSJean-Philippe Brucker 			pci_hdr->cfg_ops.read(kvm, pci_hdr, offset, data, size);
183598419d5SPekka Enberg 
184023fdaaeSJean-Philippe Brucker 		memcpy(data, (void *)pci_hdr + offset, size);
1853a60be06SSasha Levin 	} else {
186e498ea08SPekka Enberg 		memset(data, 0xff, size);
18760742802SPekka Enberg 	}
1883a60be06SSasha Levin }
18960742802SPekka Enberg 
1909b735910SMarc Zyngier static void pci_config_mmio_access(struct kvm_cpu *vcpu, u64 addr, u8 *data,
1919b735910SMarc Zyngier 				   u32 len, u8 is_write, void *kvm)
192b403f2f7SWill Deacon {
193b403f2f7SWill Deacon 	union pci_config_address cfg_addr;
194b403f2f7SWill Deacon 
195b403f2f7SWill Deacon 	addr			-= KVM_PCI_CFG_AREA;
196b403f2f7SWill Deacon 	cfg_addr.w		= (u32)addr;
197b403f2f7SWill Deacon 	cfg_addr.enable_bit	= 1;
198b403f2f7SWill Deacon 
199b403f2f7SWill Deacon 	if (is_write)
200b403f2f7SWill Deacon 		pci__config_wr(kvm, cfg_addr, data, len);
201b403f2f7SWill Deacon 	else
202b403f2f7SWill Deacon 		pci__config_rd(kvm, cfg_addr, data, len);
203b403f2f7SWill Deacon }
204b403f2f7SWill Deacon 
205d0297a59SMatt Evans struct pci_device_header *pci__find_dev(u8 dev_num)
206d0297a59SMatt Evans {
20721ff329dSWill Deacon 	struct device_header *hdr = device__find_dev(DEVICE_BUS_PCI, dev_num);
2086d987703SSasha Levin 
20921ff329dSWill Deacon 	if (IS_ERR_OR_NULL(hdr))
21021ff329dSWill Deacon 		return NULL;
21121ff329dSWill Deacon 
21221ff329dSWill Deacon 	return hdr->data;
213d0297a59SMatt Evans }
214d0297a59SMatt Evans 
2156d987703SSasha Levin int pci__init(struct kvm *kvm)
21660742802SPekka Enberg {
2176d987703SSasha Levin 	int r;
2186d987703SSasha Levin 
2194346fd8fSSasha Levin 	r = ioport__register(kvm, PCI_CONFIG_DATA + 0, &pci_config_data_ops, 4, NULL);
2206d987703SSasha Levin 	if (r < 0)
2216d987703SSasha Levin 		return r;
2226d987703SSasha Levin 
2234346fd8fSSasha Levin 	r = ioport__register(kvm, PCI_CONFIG_ADDRESS + 0, &pci_config_address_ops, 4, NULL);
224b403f2f7SWill Deacon 	if (r < 0)
225b403f2f7SWill Deacon 		goto err_unregister_data;
226b403f2f7SWill Deacon 
227b403f2f7SWill Deacon 	r = kvm__register_mmio(kvm, KVM_PCI_CFG_AREA, PCI_CFG_SIZE, false,
228b403f2f7SWill Deacon 			       pci_config_mmio_access, kvm);
229b403f2f7SWill Deacon 	if (r < 0)
230b403f2f7SWill Deacon 		goto err_unregister_addr;
2316d987703SSasha Levin 
2326d987703SSasha Levin 	return 0;
233b403f2f7SWill Deacon 
234b403f2f7SWill Deacon err_unregister_addr:
235b403f2f7SWill Deacon 	ioport__unregister(kvm, PCI_CONFIG_ADDRESS);
236b403f2f7SWill Deacon err_unregister_data:
237b403f2f7SWill Deacon 	ioport__unregister(kvm, PCI_CONFIG_DATA);
238b403f2f7SWill Deacon 	return r;
2396d987703SSasha Levin }
240bca12bf6SSasha Levin dev_base_init(pci__init);
2416d987703SSasha Levin 
2426d987703SSasha Levin int pci__exit(struct kvm *kvm)
2436d987703SSasha Levin {
2444346fd8fSSasha Levin 	ioport__unregister(kvm, PCI_CONFIG_DATA);
2454346fd8fSSasha Levin 	ioport__unregister(kvm, PCI_CONFIG_ADDRESS);
2466d987703SSasha Levin 
2476d987703SSasha Levin 	return 0;
24860742802SPekka Enberg }
249bca12bf6SSasha Levin dev_base_exit(pci__exit);
250