xref: /kvmtool/pci.c (revision 023fdaae48b45aec087551804616e0836a74d675)
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();
42b5981636SWill Deacon }
43b5981636SWill Deacon 
443fdf659dSSasha Levin static void *pci_config_address_ptr(u16 port)
45ba824677SPekka Enberg {
46ba824677SPekka Enberg 	unsigned long offset;
47ba824677SPekka Enberg 	void *base;
48ba824677SPekka Enberg 
49ba824677SPekka Enberg 	offset	= port - PCI_CONFIG_ADDRESS;
50a0a7d66fSDavid Daney 	base	= &pci_config_address_bits;
51ba824677SPekka Enberg 
52ba824677SPekka Enberg 	return base + offset;
53ba824677SPekka Enberg }
54ba824677SPekka Enberg 
554123ca55SMarc Zyngier static bool pci_config_address_out(struct ioport *ioport, struct kvm_cpu *vcpu, u16 port, void *data, int size)
5660742802SPekka Enberg {
57ba824677SPekka Enberg 	void *p = pci_config_address_ptr(port);
5860742802SPekka Enberg 
59ba824677SPekka Enberg 	memcpy(p, data, size);
6060742802SPekka Enberg 
6160742802SPekka Enberg 	return true;
6260742802SPekka Enberg }
6360742802SPekka Enberg 
644123ca55SMarc Zyngier static bool pci_config_address_in(struct ioport *ioport, struct kvm_cpu *vcpu, u16 port, void *data, int size)
6560742802SPekka Enberg {
66ba824677SPekka Enberg 	void *p = pci_config_address_ptr(port);
6760742802SPekka Enberg 
68ba824677SPekka Enberg 	memcpy(data, p, size);
6960742802SPekka Enberg 
7060742802SPekka Enberg 	return true;
7160742802SPekka Enberg }
7260742802SPekka Enberg 
73305b72ceSCyrill Gorcunov static struct ioport_operations pci_config_address_ops = {
74305b72ceSCyrill Gorcunov 	.io_in	= pci_config_address_in,
75305b72ceSCyrill Gorcunov 	.io_out	= pci_config_address_out,
7660742802SPekka Enberg };
7760742802SPekka Enberg 
783fdf659dSSasha Levin static bool pci_device_exists(u8 bus_number, u8 device_number, u8 function_number)
7976f9c841SCyrill Gorcunov {
80a0a7d66fSDavid Daney 	union pci_config_address pci_config_address;
81a0a7d66fSDavid Daney 
82a0a7d66fSDavid Daney 	pci_config_address.w = ioport__read32(&pci_config_address_bits);
83a0a7d66fSDavid Daney 
8476f9c841SCyrill Gorcunov 	if (pci_config_address.bus_number != bus_number)
8576f9c841SCyrill Gorcunov 		return false;
8676f9c841SCyrill Gorcunov 
87b30d05adSPekka Enberg 	if (pci_config_address.function_number != function_number)
8876f9c841SCyrill Gorcunov 		return false;
8976f9c841SCyrill Gorcunov 
9021ff329dSWill Deacon 	return !IS_ERR_OR_NULL(device__find_dev(DEVICE_BUS_PCI, device_number));
9176f9c841SCyrill Gorcunov }
9276f9c841SCyrill Gorcunov 
934123ca55SMarc Zyngier static bool pci_config_data_out(struct ioport *ioport, struct kvm_cpu *vcpu, u16 port, void *data, int size)
949575e724SSasha Levin {
95a0a7d66fSDavid Daney 	union pci_config_address pci_config_address;
96a0a7d66fSDavid Daney 
97a0a7d66fSDavid Daney 	pci_config_address.w = ioport__read32(&pci_config_address_bits);
989575e724SSasha Levin 	/*
999575e724SSasha Levin 	 * If someone accesses PCI configuration space offsets that are not
1009575e724SSasha Levin 	 * aligned to 4 bytes, it uses ioports to signify that.
1019575e724SSasha Levin 	 */
102d0297a59SMatt Evans 	pci_config_address.reg_offset = port - PCI_CONFIG_DATA;
1039575e724SSasha Levin 
1044123ca55SMarc Zyngier 	pci__config_wr(vcpu->kvm, pci_config_address, data, size);
105d0297a59SMatt Evans 
106d0297a59SMatt Evans 	return true;
107d0297a59SMatt Evans }
108d0297a59SMatt Evans 
1094123ca55SMarc Zyngier static bool pci_config_data_in(struct ioport *ioport, struct kvm_cpu *vcpu, u16 port, void *data, int size)
110d0297a59SMatt Evans {
111a0a7d66fSDavid Daney 	union pci_config_address pci_config_address;
112a0a7d66fSDavid Daney 
113a0a7d66fSDavid Daney 	pci_config_address.w = ioport__read32(&pci_config_address_bits);
114d0297a59SMatt Evans 	/*
115d0297a59SMatt Evans 	 * If someone accesses PCI configuration space offsets that are not
116d0297a59SMatt Evans 	 * aligned to 4 bytes, it uses ioports to signify that.
117d0297a59SMatt Evans 	 */
118d0297a59SMatt Evans 	pci_config_address.reg_offset = port - PCI_CONFIG_DATA;
119d0297a59SMatt Evans 
1204123ca55SMarc Zyngier 	pci__config_rd(vcpu->kvm, pci_config_address, data, size);
121d0297a59SMatt Evans 
122d0297a59SMatt Evans 	return true;
123d0297a59SMatt Evans }
124d0297a59SMatt Evans 
125d0297a59SMatt Evans static struct ioport_operations pci_config_data_ops = {
126d0297a59SMatt Evans 	.io_in	= pci_config_data_in,
127d0297a59SMatt Evans 	.io_out	= pci_config_data_out,
128d0297a59SMatt Evans };
129d0297a59SMatt Evans 
130d0297a59SMatt Evans void pci__config_wr(struct kvm *kvm, union pci_config_address addr, void *data, int size)
131d0297a59SMatt Evans {
132*023fdaaeSJean-Philippe Brucker 	void *base;
133*023fdaaeSJean-Philippe Brucker 	u8 bar, offset;
134*023fdaaeSJean-Philippe Brucker 	struct pci_device_header *pci_hdr;
135*023fdaaeSJean-Philippe Brucker 	u8 dev_num = addr.device_number;
136d0297a59SMatt Evans 
137*023fdaaeSJean-Philippe Brucker 	if (!pci_device_exists(addr.bus_number, dev_num, 0))
138*023fdaaeSJean-Philippe Brucker 		return;
1399575e724SSasha Levin 
140*023fdaaeSJean-Philippe Brucker 	offset = addr.w & PCI_DEV_CFG_MASK;
141*023fdaaeSJean-Philippe Brucker 	base = pci_hdr = device__find_dev(DEVICE_BUS_PCI, dev_num)->data;
1429575e724SSasha Levin 
143*023fdaaeSJean-Philippe Brucker 	if (pci_hdr->cfg_ops.write)
144*023fdaaeSJean-Philippe Brucker 		pci_hdr->cfg_ops.write(kvm, pci_hdr, offset, data, size);
145c64f7ff0SSasha Levin 
1469575e724SSasha Levin 	/*
147*023fdaaeSJean-Philippe Brucker 	 * legacy hack: ignore writes to uninitialized regions (e.g. ROM BAR).
148*023fdaaeSJean-Philippe Brucker 	 * Not very nice but has been working so far.
1499575e724SSasha Levin 	 */
150*023fdaaeSJean-Philippe Brucker 	if (*(u32 *)(base + offset) == 0)
151*023fdaaeSJean-Philippe Brucker 		return;
152*023fdaaeSJean-Philippe Brucker 
153*023fdaaeSJean-Philippe Brucker 	bar = (offset - PCI_BAR_OFFSET(0)) / sizeof(u32);
154*023fdaaeSJean-Philippe Brucker 
155*023fdaaeSJean-Philippe Brucker 	/*
156*023fdaaeSJean-Philippe Brucker 	 * If the kernel masks the BAR it would expect to find the size of the
157*023fdaaeSJean-Philippe Brucker 	 * BAR there next time it reads from it. When the kernel got the size it
158*023fdaaeSJean-Philippe Brucker 	 * would write the address back.
159*023fdaaeSJean-Philippe Brucker 	 */
160*023fdaaeSJean-Philippe Brucker 	if (bar < 6 && ioport__read32(data) == 0xFFFFFFFF) {
161*023fdaaeSJean-Philippe Brucker 		u32 sz = pci_hdr->bar_size[bar];
162*023fdaaeSJean-Philippe Brucker 		memcpy(base + offset, &sz, sizeof(sz));
163*023fdaaeSJean-Philippe Brucker 	} else {
164*023fdaaeSJean-Philippe Brucker 		memcpy(base + offset, data, size);
1659575e724SSasha Levin 	}
1669575e724SSasha Levin }
1679575e724SSasha Levin 
168d0297a59SMatt Evans void pci__config_rd(struct kvm *kvm, union pci_config_address addr, void *data, int size)
16960742802SPekka Enberg {
170*023fdaaeSJean-Philippe Brucker 	u8 offset;
171*023fdaaeSJean-Philippe Brucker 	struct pci_device_header *pci_hdr;
172*023fdaaeSJean-Philippe Brucker 	u8 dev_num = addr.device_number;
173e4d2cea2SPekka Enberg 
174*023fdaaeSJean-Philippe Brucker 	if (pci_device_exists(addr.bus_number, dev_num, 0)) {
175*023fdaaeSJean-Philippe Brucker 		pci_hdr = device__find_dev(DEVICE_BUS_PCI, dev_num)->data;
176*023fdaaeSJean-Philippe Brucker 		offset = addr.w & PCI_DEV_CFG_MASK;
177b30d05adSPekka Enberg 
178*023fdaaeSJean-Philippe Brucker 		if (pci_hdr->cfg_ops.read)
179*023fdaaeSJean-Philippe Brucker 			pci_hdr->cfg_ops.read(kvm, pci_hdr, offset, data, size);
180598419d5SPekka Enberg 
181*023fdaaeSJean-Philippe Brucker 		memcpy(data, (void *)pci_hdr + offset, size);
1823a60be06SSasha Levin 	} else {
183e498ea08SPekka Enberg 		memset(data, 0xff, size);
18460742802SPekka Enberg 	}
1853a60be06SSasha Levin }
18660742802SPekka Enberg 
1879b735910SMarc Zyngier static void pci_config_mmio_access(struct kvm_cpu *vcpu, u64 addr, u8 *data,
1889b735910SMarc Zyngier 				   u32 len, u8 is_write, void *kvm)
189b403f2f7SWill Deacon {
190b403f2f7SWill Deacon 	union pci_config_address cfg_addr;
191b403f2f7SWill Deacon 
192b403f2f7SWill Deacon 	addr			-= KVM_PCI_CFG_AREA;
193b403f2f7SWill Deacon 	cfg_addr.w		= (u32)addr;
194b403f2f7SWill Deacon 	cfg_addr.enable_bit	= 1;
195b403f2f7SWill Deacon 
196b403f2f7SWill Deacon 	if (is_write)
197b403f2f7SWill Deacon 		pci__config_wr(kvm, cfg_addr, data, len);
198b403f2f7SWill Deacon 	else
199b403f2f7SWill Deacon 		pci__config_rd(kvm, cfg_addr, data, len);
200b403f2f7SWill Deacon }
201b403f2f7SWill Deacon 
202d0297a59SMatt Evans struct pci_device_header *pci__find_dev(u8 dev_num)
203d0297a59SMatt Evans {
20421ff329dSWill Deacon 	struct device_header *hdr = device__find_dev(DEVICE_BUS_PCI, dev_num);
2056d987703SSasha Levin 
20621ff329dSWill Deacon 	if (IS_ERR_OR_NULL(hdr))
20721ff329dSWill Deacon 		return NULL;
20821ff329dSWill Deacon 
20921ff329dSWill Deacon 	return hdr->data;
210d0297a59SMatt Evans }
211d0297a59SMatt Evans 
2126d987703SSasha Levin int pci__init(struct kvm *kvm)
21360742802SPekka Enberg {
2146d987703SSasha Levin 	int r;
2156d987703SSasha Levin 
2164346fd8fSSasha Levin 	r = ioport__register(kvm, PCI_CONFIG_DATA + 0, &pci_config_data_ops, 4, NULL);
2176d987703SSasha Levin 	if (r < 0)
2186d987703SSasha Levin 		return r;
2196d987703SSasha Levin 
2204346fd8fSSasha Levin 	r = ioport__register(kvm, PCI_CONFIG_ADDRESS + 0, &pci_config_address_ops, 4, NULL);
221b403f2f7SWill Deacon 	if (r < 0)
222b403f2f7SWill Deacon 		goto err_unregister_data;
223b403f2f7SWill Deacon 
224b403f2f7SWill Deacon 	r = kvm__register_mmio(kvm, KVM_PCI_CFG_AREA, PCI_CFG_SIZE, false,
225b403f2f7SWill Deacon 			       pci_config_mmio_access, kvm);
226b403f2f7SWill Deacon 	if (r < 0)
227b403f2f7SWill Deacon 		goto err_unregister_addr;
2286d987703SSasha Levin 
2296d987703SSasha Levin 	return 0;
230b403f2f7SWill Deacon 
231b403f2f7SWill Deacon err_unregister_addr:
232b403f2f7SWill Deacon 	ioport__unregister(kvm, PCI_CONFIG_ADDRESS);
233b403f2f7SWill Deacon err_unregister_data:
234b403f2f7SWill Deacon 	ioport__unregister(kvm, PCI_CONFIG_DATA);
235b403f2f7SWill Deacon 	return r;
2366d987703SSasha Levin }
237bca12bf6SSasha Levin dev_base_init(pci__init);
2386d987703SSasha Levin 
2396d987703SSasha Levin int pci__exit(struct kvm *kvm)
2406d987703SSasha Levin {
2414346fd8fSSasha Levin 	ioport__unregister(kvm, PCI_CONFIG_DATA);
2424346fd8fSSasha Levin 	ioport__unregister(kvm, PCI_CONFIG_ADDRESS);
2436d987703SSasha Levin 
2446d987703SSasha Levin 	return 0;
24560742802SPekka Enberg }
246bca12bf6SSasha Levin dev_base_exit(pci__exit);
247