xref: /kvmtool/pci.c (revision 6ea32ebdb84b1ba9d72459029eb92363efd4ffd6)
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  */
18854aa2efSJulien Thierry static u32 mmio_blocks			= KVM_PCI_MMIO_AREA;
19854aa2efSJulien Thierry static u16 io_port_blocks		= PCI_IOPORT_START;
20854aa2efSJulien Thierry 
21854aa2efSJulien Thierry u16 pci_get_io_port_block(u32 size)
22854aa2efSJulien Thierry {
2348843d10SJulien Thierry 	u16 port = ALIGN(io_port_blocks, PCI_IO_SIZE);
24854aa2efSJulien Thierry 
25854aa2efSJulien Thierry 	io_port_blocks = port + size;
26854aa2efSJulien Thierry 	return port;
27854aa2efSJulien Thierry }
289575e724SSasha Levin 
29c7575d17SWill Deacon /*
30c7575d17SWill Deacon  * BARs must be naturally aligned, so enforce this in the allocator.
31c7575d17SWill Deacon  */
32854aa2efSJulien Thierry u32 pci_get_mmio_block(u32 size)
339575e724SSasha Levin {
34854aa2efSJulien Thierry 	u32 block = ALIGN(mmio_blocks, size);
35854aa2efSJulien Thierry 	mmio_blocks = block + size;
369575e724SSasha Levin 	return block;
379575e724SSasha Levin }
389575e724SSasha Levin 
391a51c93dSJean-Philippe Brucker void *pci_find_cap(struct pci_device_header *hdr, u8 cap_type)
401a51c93dSJean-Philippe Brucker {
411a51c93dSJean-Philippe Brucker 	u8 pos;
421a51c93dSJean-Philippe Brucker 	struct pci_cap_hdr *cap;
431a51c93dSJean-Philippe Brucker 
441a51c93dSJean-Philippe Brucker 	pci_for_each_cap(pos, cap, hdr) {
451a51c93dSJean-Philippe Brucker 		if (cap->type == cap_type)
461a51c93dSJean-Philippe Brucker 			return cap;
471a51c93dSJean-Philippe Brucker 	}
481a51c93dSJean-Philippe Brucker 
491a51c93dSJean-Philippe Brucker 	return NULL;
501a51c93dSJean-Philippe Brucker }
511a51c93dSJean-Philippe Brucker 
52c0c45eedSAndre Przywara int pci__assign_irq(struct pci_device_header *pci_hdr)
53b5981636SWill Deacon {
54b5981636SWill Deacon 	/*
55b5981636SWill Deacon 	 * PCI supports only INTA#,B#,C#,D# per device.
56b5981636SWill Deacon 	 *
57b5981636SWill Deacon 	 * A#,B#,C#,D# are allowed for multifunctional devices so stick
58b5981636SWill Deacon 	 * with A# for our single function devices.
59b5981636SWill Deacon 	 */
60b5981636SWill Deacon 	pci_hdr->irq_pin	= 1;
61b5981636SWill Deacon 	pci_hdr->irq_line	= irq__alloc_line();
62ff01b5dbSJean-Philippe Brucker 
63ff01b5dbSJean-Philippe Brucker 	if (!pci_hdr->irq_type)
64ff01b5dbSJean-Philippe Brucker 		pci_hdr->irq_type = IRQ_TYPE_EDGE_RISING;
65c0c45eedSAndre Przywara 
66c0c45eedSAndre Przywara 	return pci_hdr->irq_line;
67b5981636SWill Deacon }
68b5981636SWill Deacon 
693fdf659dSSasha Levin static void *pci_config_address_ptr(u16 port)
70ba824677SPekka Enberg {
71ba824677SPekka Enberg 	unsigned long offset;
72ba824677SPekka Enberg 	void *base;
73ba824677SPekka Enberg 
74ba824677SPekka Enberg 	offset	= port - PCI_CONFIG_ADDRESS;
75a0a7d66fSDavid Daney 	base	= &pci_config_address_bits;
76ba824677SPekka Enberg 
77ba824677SPekka Enberg 	return base + offset;
78ba824677SPekka Enberg }
79ba824677SPekka Enberg 
804123ca55SMarc Zyngier static bool pci_config_address_out(struct ioport *ioport, struct kvm_cpu *vcpu, u16 port, void *data, int size)
8160742802SPekka Enberg {
82ba824677SPekka Enberg 	void *p = pci_config_address_ptr(port);
8360742802SPekka Enberg 
84ba824677SPekka Enberg 	memcpy(p, data, size);
8560742802SPekka Enberg 
8660742802SPekka Enberg 	return true;
8760742802SPekka Enberg }
8860742802SPekka Enberg 
894123ca55SMarc Zyngier static bool pci_config_address_in(struct ioport *ioport, struct kvm_cpu *vcpu, u16 port, void *data, int size)
9060742802SPekka Enberg {
91ba824677SPekka Enberg 	void *p = pci_config_address_ptr(port);
9260742802SPekka Enberg 
93ba824677SPekka Enberg 	memcpy(data, p, size);
9460742802SPekka Enberg 
9560742802SPekka Enberg 	return true;
9660742802SPekka Enberg }
9760742802SPekka Enberg 
98305b72ceSCyrill Gorcunov static struct ioport_operations pci_config_address_ops = {
99305b72ceSCyrill Gorcunov 	.io_in	= pci_config_address_in,
100305b72ceSCyrill Gorcunov 	.io_out	= pci_config_address_out,
10160742802SPekka Enberg };
10260742802SPekka Enberg 
1033fdf659dSSasha Levin static bool pci_device_exists(u8 bus_number, u8 device_number, u8 function_number)
10476f9c841SCyrill Gorcunov {
105a0a7d66fSDavid Daney 	union pci_config_address pci_config_address;
106a0a7d66fSDavid Daney 
107a0a7d66fSDavid Daney 	pci_config_address.w = ioport__read32(&pci_config_address_bits);
108a0a7d66fSDavid Daney 
10976f9c841SCyrill Gorcunov 	if (pci_config_address.bus_number != bus_number)
11076f9c841SCyrill Gorcunov 		return false;
11176f9c841SCyrill Gorcunov 
112b30d05adSPekka Enberg 	if (pci_config_address.function_number != function_number)
11376f9c841SCyrill Gorcunov 		return false;
11476f9c841SCyrill Gorcunov 
11521ff329dSWill Deacon 	return !IS_ERR_OR_NULL(device__find_dev(DEVICE_BUS_PCI, device_number));
11676f9c841SCyrill Gorcunov }
11776f9c841SCyrill Gorcunov 
1184123ca55SMarc Zyngier static bool pci_config_data_out(struct ioport *ioport, struct kvm_cpu *vcpu, u16 port, void *data, int size)
1199575e724SSasha Levin {
120a0a7d66fSDavid Daney 	union pci_config_address pci_config_address;
121a0a7d66fSDavid Daney 
122*6ea32ebdSAlexandru Elisei 	if (size > 4)
123*6ea32ebdSAlexandru Elisei 		size = 4;
124*6ea32ebdSAlexandru Elisei 
125a0a7d66fSDavid Daney 	pci_config_address.w = ioport__read32(&pci_config_address_bits);
1269575e724SSasha Levin 	/*
1279575e724SSasha Levin 	 * If someone accesses PCI configuration space offsets that are not
1289575e724SSasha Levin 	 * aligned to 4 bytes, it uses ioports to signify that.
1299575e724SSasha Levin 	 */
130d0297a59SMatt Evans 	pci_config_address.reg_offset = port - PCI_CONFIG_DATA;
1319575e724SSasha Levin 
1324123ca55SMarc Zyngier 	pci__config_wr(vcpu->kvm, pci_config_address, data, size);
133d0297a59SMatt Evans 
134d0297a59SMatt Evans 	return true;
135d0297a59SMatt Evans }
136d0297a59SMatt Evans 
1374123ca55SMarc Zyngier static bool pci_config_data_in(struct ioport *ioport, struct kvm_cpu *vcpu, u16 port, void *data, int size)
138d0297a59SMatt Evans {
139a0a7d66fSDavid Daney 	union pci_config_address pci_config_address;
140a0a7d66fSDavid Daney 
141*6ea32ebdSAlexandru Elisei 	if (size > 4)
142*6ea32ebdSAlexandru Elisei 		size = 4;
143*6ea32ebdSAlexandru Elisei 
144a0a7d66fSDavid Daney 	pci_config_address.w = ioport__read32(&pci_config_address_bits);
145d0297a59SMatt Evans 	/*
146d0297a59SMatt Evans 	 * If someone accesses PCI configuration space offsets that are not
147d0297a59SMatt Evans 	 * aligned to 4 bytes, it uses ioports to signify that.
148d0297a59SMatt Evans 	 */
149d0297a59SMatt Evans 	pci_config_address.reg_offset = port - PCI_CONFIG_DATA;
150d0297a59SMatt Evans 
1514123ca55SMarc Zyngier 	pci__config_rd(vcpu->kvm, pci_config_address, data, size);
152d0297a59SMatt Evans 
153d0297a59SMatt Evans 	return true;
154d0297a59SMatt Evans }
155d0297a59SMatt Evans 
156d0297a59SMatt Evans static struct ioport_operations pci_config_data_ops = {
157d0297a59SMatt Evans 	.io_in	= pci_config_data_in,
158d0297a59SMatt Evans 	.io_out	= pci_config_data_out,
159d0297a59SMatt Evans };
160d0297a59SMatt Evans 
161d0297a59SMatt Evans void pci__config_wr(struct kvm *kvm, union pci_config_address addr, void *data, int size)
162d0297a59SMatt Evans {
163023fdaaeSJean-Philippe Brucker 	void *base;
164023fdaaeSJean-Philippe Brucker 	u8 bar, offset;
165023fdaaeSJean-Philippe Brucker 	struct pci_device_header *pci_hdr;
166023fdaaeSJean-Philippe Brucker 	u8 dev_num = addr.device_number;
167bb0d509bSSami Mujawar 	u32 value = 0;
168bb0d509bSSami Mujawar 	u32 mask;
169d0297a59SMatt Evans 
170023fdaaeSJean-Philippe Brucker 	if (!pci_device_exists(addr.bus_number, dev_num, 0))
171023fdaaeSJean-Philippe Brucker 		return;
1729575e724SSasha Levin 
173023fdaaeSJean-Philippe Brucker 	offset = addr.w & PCI_DEV_CFG_MASK;
174023fdaaeSJean-Philippe Brucker 	base = pci_hdr = device__find_dev(DEVICE_BUS_PCI, dev_num)->data;
1759575e724SSasha Levin 
176023fdaaeSJean-Philippe Brucker 	if (pci_hdr->cfg_ops.write)
177023fdaaeSJean-Philippe Brucker 		pci_hdr->cfg_ops.write(kvm, pci_hdr, offset, data, size);
178c64f7ff0SSasha Levin 
1799575e724SSasha Levin 	/*
180023fdaaeSJean-Philippe Brucker 	 * legacy hack: ignore writes to uninitialized regions (e.g. ROM BAR).
181023fdaaeSJean-Philippe Brucker 	 * Not very nice but has been working so far.
1829575e724SSasha Levin 	 */
183023fdaaeSJean-Philippe Brucker 	if (*(u32 *)(base + offset) == 0)
184023fdaaeSJean-Philippe Brucker 		return;
185023fdaaeSJean-Philippe Brucker 
186023fdaaeSJean-Philippe Brucker 	bar = (offset - PCI_BAR_OFFSET(0)) / sizeof(u32);
187023fdaaeSJean-Philippe Brucker 
188023fdaaeSJean-Philippe Brucker 	/*
189bb0d509bSSami Mujawar 	 * If the kernel masks the BAR, it will expect to find the size of the
190bb0d509bSSami Mujawar 	 * BAR there next time it reads from it. After the kernel reads the
191bb0d509bSSami Mujawar 	 * size, it will write the address back.
192023fdaaeSJean-Philippe Brucker 	 */
193bb0d509bSSami Mujawar 	if (bar < 6) {
1942f6384f9SAlexandru Elisei 		if (pci__bar_is_io(pci_hdr, bar))
195bb0d509bSSami Mujawar 			mask = (u32)PCI_BASE_ADDRESS_IO_MASK;
196bb0d509bSSami Mujawar 		else
197bb0d509bSSami Mujawar 			mask = (u32)PCI_BASE_ADDRESS_MEM_MASK;
198bb0d509bSSami Mujawar 		/*
199bb0d509bSSami Mujawar 		 * According to the PCI local bus specification REV 3.0:
200bb0d509bSSami Mujawar 		 * The number of upper bits that a device actually implements
201bb0d509bSSami Mujawar 		 * depends on how much of the address space the device will
202bb0d509bSSami Mujawar 		 * respond to. A device that wants a 1 MB memory address space
203bb0d509bSSami Mujawar 		 * (using a 32-bit base address register) would build the top
204bb0d509bSSami Mujawar 		 * 12 bits of the address register, hardwiring the other bits
205bb0d509bSSami Mujawar 		 * to 0.
206bb0d509bSSami Mujawar 		 *
207bb0d509bSSami Mujawar 		 * Furthermore, software can determine how much address space
208bb0d509bSSami Mujawar 		 * the device requires by writing a value of all 1's to the
209bb0d509bSSami Mujawar 		 * register and then reading the value back. The device will
210bb0d509bSSami Mujawar 		 * return 0's in all don't-care address bits, effectively
211bb0d509bSSami Mujawar 		 * specifying the address space required.
212bb0d509bSSami Mujawar 		 *
213bb0d509bSSami Mujawar 		 * Software computes the size of the address space with the
214bb0d509bSSami Mujawar 		 * formula S = ~B + 1, where S is the memory size and B is the
215bb0d509bSSami Mujawar 		 * value read from the BAR. This means that the BAR value that
216bb0d509bSSami Mujawar 		 * kvmtool should return is B = ~(S - 1).
217bb0d509bSSami Mujawar 		 */
218bb0d509bSSami Mujawar 		memcpy(&value, data, size);
219bb0d509bSSami Mujawar 		if (value == 0xffffffff)
2202f6384f9SAlexandru Elisei 			value = ~(pci__bar_size(pci_hdr, bar) - 1);
221bb0d509bSSami Mujawar 		/* Preserve the special bits. */
222bb0d509bSSami Mujawar 		value = (value & mask) | (pci_hdr->bar[bar] & ~mask);
223bb0d509bSSami Mujawar 		memcpy(base + offset, &value, size);
224023fdaaeSJean-Philippe Brucker 	} else {
225023fdaaeSJean-Philippe Brucker 		memcpy(base + offset, data, size);
2269575e724SSasha Levin 	}
2279575e724SSasha Levin }
2289575e724SSasha Levin 
229d0297a59SMatt Evans void pci__config_rd(struct kvm *kvm, union pci_config_address addr, void *data, int size)
23060742802SPekka Enberg {
231023fdaaeSJean-Philippe Brucker 	u8 offset;
232023fdaaeSJean-Philippe Brucker 	struct pci_device_header *pci_hdr;
233023fdaaeSJean-Philippe Brucker 	u8 dev_num = addr.device_number;
234e4d2cea2SPekka Enberg 
235023fdaaeSJean-Philippe Brucker 	if (pci_device_exists(addr.bus_number, dev_num, 0)) {
236023fdaaeSJean-Philippe Brucker 		pci_hdr = device__find_dev(DEVICE_BUS_PCI, dev_num)->data;
237023fdaaeSJean-Philippe Brucker 		offset = addr.w & PCI_DEV_CFG_MASK;
238b30d05adSPekka Enberg 
239023fdaaeSJean-Philippe Brucker 		if (pci_hdr->cfg_ops.read)
240023fdaaeSJean-Philippe Brucker 			pci_hdr->cfg_ops.read(kvm, pci_hdr, offset, data, size);
241598419d5SPekka Enberg 
242023fdaaeSJean-Philippe Brucker 		memcpy(data, (void *)pci_hdr + offset, size);
2433a60be06SSasha Levin 	} else {
244e498ea08SPekka Enberg 		memset(data, 0xff, size);
24560742802SPekka Enberg 	}
2463a60be06SSasha Levin }
24760742802SPekka Enberg 
2489b735910SMarc Zyngier static void pci_config_mmio_access(struct kvm_cpu *vcpu, u64 addr, u8 *data,
2499b735910SMarc Zyngier 				   u32 len, u8 is_write, void *kvm)
250b403f2f7SWill Deacon {
251b403f2f7SWill Deacon 	union pci_config_address cfg_addr;
252b403f2f7SWill Deacon 
253b403f2f7SWill Deacon 	addr			-= KVM_PCI_CFG_AREA;
254b403f2f7SWill Deacon 	cfg_addr.w		= (u32)addr;
255b403f2f7SWill Deacon 	cfg_addr.enable_bit	= 1;
256b403f2f7SWill Deacon 
257*6ea32ebdSAlexandru Elisei 	if (len > 4)
258*6ea32ebdSAlexandru Elisei 		len = 4;
259*6ea32ebdSAlexandru Elisei 
260b403f2f7SWill Deacon 	if (is_write)
261b403f2f7SWill Deacon 		pci__config_wr(kvm, cfg_addr, data, len);
262b403f2f7SWill Deacon 	else
263b403f2f7SWill Deacon 		pci__config_rd(kvm, cfg_addr, data, len);
264b403f2f7SWill Deacon }
265b403f2f7SWill Deacon 
266d0297a59SMatt Evans struct pci_device_header *pci__find_dev(u8 dev_num)
267d0297a59SMatt Evans {
26821ff329dSWill Deacon 	struct device_header *hdr = device__find_dev(DEVICE_BUS_PCI, dev_num);
2696d987703SSasha Levin 
27021ff329dSWill Deacon 	if (IS_ERR_OR_NULL(hdr))
27121ff329dSWill Deacon 		return NULL;
27221ff329dSWill Deacon 
27321ff329dSWill Deacon 	return hdr->data;
274d0297a59SMatt Evans }
275d0297a59SMatt Evans 
2766d987703SSasha Levin int pci__init(struct kvm *kvm)
27760742802SPekka Enberg {
2786d987703SSasha Levin 	int r;
2796d987703SSasha Levin 
2804346fd8fSSasha Levin 	r = ioport__register(kvm, PCI_CONFIG_DATA + 0, &pci_config_data_ops, 4, NULL);
2816d987703SSasha Levin 	if (r < 0)
2826d987703SSasha Levin 		return r;
2836d987703SSasha Levin 
2844346fd8fSSasha Levin 	r = ioport__register(kvm, PCI_CONFIG_ADDRESS + 0, &pci_config_address_ops, 4, NULL);
285b403f2f7SWill Deacon 	if (r < 0)
286b403f2f7SWill Deacon 		goto err_unregister_data;
287b403f2f7SWill Deacon 
288b403f2f7SWill Deacon 	r = kvm__register_mmio(kvm, KVM_PCI_CFG_AREA, PCI_CFG_SIZE, false,
289b403f2f7SWill Deacon 			       pci_config_mmio_access, kvm);
290b403f2f7SWill Deacon 	if (r < 0)
291b403f2f7SWill Deacon 		goto err_unregister_addr;
2926d987703SSasha Levin 
2936d987703SSasha Levin 	return 0;
294b403f2f7SWill Deacon 
295b403f2f7SWill Deacon err_unregister_addr:
296b403f2f7SWill Deacon 	ioport__unregister(kvm, PCI_CONFIG_ADDRESS);
297b403f2f7SWill Deacon err_unregister_data:
298b403f2f7SWill Deacon 	ioport__unregister(kvm, PCI_CONFIG_DATA);
299b403f2f7SWill Deacon 	return r;
3006d987703SSasha Levin }
301bca12bf6SSasha Levin dev_base_init(pci__init);
3026d987703SSasha Levin 
3036d987703SSasha Levin int pci__exit(struct kvm *kvm)
3046d987703SSasha Levin {
3054346fd8fSSasha Levin 	ioport__unregister(kvm, PCI_CONFIG_DATA);
3064346fd8fSSasha Levin 	ioport__unregister(kvm, PCI_CONFIG_ADDRESS);
3076d987703SSasha Levin 
3086d987703SSasha Levin 	return 0;
30960742802SPekka Enberg }
310bca12bf6SSasha Levin dev_base_exit(pci__exit);
311