xref: /kvmtool/arm/pci.c (revision 14421de9e076ac4f22dd2b2715bfc878986dc66a)
116242d27SWill Deacon #include "kvm/devices.h"
216242d27SWill Deacon #include "kvm/fdt.h"
316242d27SWill Deacon #include "kvm/of_pci.h"
416242d27SWill Deacon #include "kvm/pci.h"
516242d27SWill Deacon #include "kvm/util.h"
616242d27SWill Deacon 
716242d27SWill Deacon #include "arm-common/pci.h"
816242d27SWill Deacon 
916242d27SWill Deacon /*
1016242d27SWill Deacon  * An entry in the interrupt-map table looks like:
1116242d27SWill Deacon  * <pci unit address> <pci interrupt pin> <gic phandle> <gic interrupt>
1216242d27SWill Deacon  */
1316242d27SWill Deacon 
1416242d27SWill Deacon struct of_gic_irq {
1516242d27SWill Deacon 	u32 type, num, flags;
1616242d27SWill Deacon } __attribute__((packed));
1716242d27SWill Deacon 
1816242d27SWill Deacon struct of_interrupt_map_entry {
1916242d27SWill Deacon 	struct of_pci_irq_mask		pci_irq_mask;
2016242d27SWill Deacon 	u32				gic_phandle;
21*14421de9SAndre Przywara 	u32				gic_addr_hi;
22*14421de9SAndre Przywara 	u32				gic_addr_lo;
2316242d27SWill Deacon 	struct of_gic_irq		gic_irq;
2416242d27SWill Deacon } __attribute__((packed));
2516242d27SWill Deacon 
260063d50cSAndre Przywara void pci__generate_fdt_nodes(void *fdt)
2716242d27SWill Deacon {
2816242d27SWill Deacon 	struct device_header *dev_hdr;
2916242d27SWill Deacon 	struct of_interrupt_map_entry irq_map[OF_PCI_IRQ_MAP_MAX];
3016242d27SWill Deacon 	unsigned nentries = 0;
311fcf0d77SWill Deacon 	/* Bus range */
321fcf0d77SWill Deacon 	u32 bus_range[] = { cpu_to_fdt32(0), cpu_to_fdt32(1), };
331fcf0d77SWill Deacon 	/* Configuration Space */
341fcf0d77SWill Deacon 	u64 cfg_reg_prop[] = { cpu_to_fdt64(KVM_PCI_CFG_AREA),
351fcf0d77SWill Deacon 			       cpu_to_fdt64(ARM_PCI_CFG_SIZE), };
361fcf0d77SWill Deacon 	/* Describe the memory ranges */
3716242d27SWill Deacon 	struct of_pci_ranges_entry ranges[] = {
3816242d27SWill Deacon 		{
3916242d27SWill Deacon 			.pci_addr = {
4016242d27SWill Deacon 				.hi	= cpu_to_fdt32(of_pci_b_ss(OF_PCI_SS_IO)),
4116242d27SWill Deacon 				.mid	= 0,
4216242d27SWill Deacon 				.lo	= 0,
4316242d27SWill Deacon 			},
4416242d27SWill Deacon 			.cpu_addr	= cpu_to_fdt64(KVM_IOPORT_AREA),
4516242d27SWill Deacon 			.length		= cpu_to_fdt64(ARM_IOPORT_SIZE),
4616242d27SWill Deacon 		},
4716242d27SWill Deacon 		{
4816242d27SWill Deacon 			.pci_addr = {
4916242d27SWill Deacon 				.hi	= cpu_to_fdt32(of_pci_b_ss(OF_PCI_SS_M32)),
501fcf0d77SWill Deacon 				.mid	= cpu_to_fdt32(KVM_PCI_MMIO_AREA >> 32),
511fcf0d77SWill Deacon 				.lo	= cpu_to_fdt32(KVM_PCI_MMIO_AREA),
5216242d27SWill Deacon 			},
5316242d27SWill Deacon 			.cpu_addr	= cpu_to_fdt64(KVM_PCI_MMIO_AREA),
5416242d27SWill Deacon 			.length		= cpu_to_fdt64(ARM_PCI_MMIO_SIZE),
5516242d27SWill Deacon 		},
5616242d27SWill Deacon 	};
5716242d27SWill Deacon 
5816242d27SWill Deacon 	/* Boilerplate PCI properties */
5916242d27SWill Deacon 	_FDT(fdt_begin_node(fdt, "pci"));
601fcf0d77SWill Deacon 	_FDT(fdt_property_string(fdt, "device_type", "pci"));
6116242d27SWill Deacon 	_FDT(fdt_property_cell(fdt, "#address-cells", 0x3));
6216242d27SWill Deacon 	_FDT(fdt_property_cell(fdt, "#size-cells", 0x2));
6316242d27SWill Deacon 	_FDT(fdt_property_cell(fdt, "#interrupt-cells", 0x1));
641fcf0d77SWill Deacon 	_FDT(fdt_property_string(fdt, "compatible", "pci-host-cam-generic"));
659a8af7e3SRobin Murphy 	_FDT(fdt_property(fdt, "dma-coherent", NULL, 0));
6616242d27SWill Deacon 
671fcf0d77SWill Deacon 	_FDT(fdt_property(fdt, "bus-range", bus_range, sizeof(bus_range)));
681fcf0d77SWill Deacon 	_FDT(fdt_property(fdt, "reg", &cfg_reg_prop, sizeof(cfg_reg_prop)));
6916242d27SWill Deacon 	_FDT(fdt_property(fdt, "ranges", ranges, sizeof(ranges)));
70*14421de9SAndre Przywara 	_FDT(fdt_property_cell(fdt, "msi-parent", PHANDLE_MSI));
7116242d27SWill Deacon 
7216242d27SWill Deacon 	/* Generate the interrupt map ... */
7316242d27SWill Deacon 	dev_hdr = device__first_dev(DEVICE_BUS_PCI);
7416242d27SWill Deacon 	while (dev_hdr && nentries < ARRAY_SIZE(irq_map)) {
7516242d27SWill Deacon 		struct of_interrupt_map_entry *entry = &irq_map[nentries];
7616242d27SWill Deacon 		struct pci_device_header *pci_hdr = dev_hdr->data;
7716242d27SWill Deacon 		u8 dev_num = dev_hdr->dev_num;
7816242d27SWill Deacon 		u8 pin = pci_hdr->irq_pin;
7916242d27SWill Deacon 		u8 irq = pci_hdr->irq_line;
8016242d27SWill Deacon 
8116242d27SWill Deacon 		*entry = (struct of_interrupt_map_entry) {
8216242d27SWill Deacon 			.pci_irq_mask = {
8316242d27SWill Deacon 				.pci_addr = {
8416242d27SWill Deacon 					.hi	= cpu_to_fdt32(of_pci_b_ddddd(dev_num)),
8516242d27SWill Deacon 					.mid	= 0,
8616242d27SWill Deacon 					.lo	= 0,
8716242d27SWill Deacon 				},
8816242d27SWill Deacon 				.pci_pin	= cpu_to_fdt32(pin),
8916242d27SWill Deacon 			},
900063d50cSAndre Przywara 			.gic_phandle	= cpu_to_fdt32(PHANDLE_GIC),
91*14421de9SAndre Przywara 			.gic_addr_hi	= 0,
92*14421de9SAndre Przywara 			.gic_addr_lo	= 0,
9316242d27SWill Deacon 			.gic_irq = {
9416242d27SWill Deacon 				.type	= cpu_to_fdt32(GIC_FDT_IRQ_TYPE_SPI),
9516242d27SWill Deacon 				.num	= cpu_to_fdt32(irq - GIC_SPI_IRQ_BASE),
96045fc040SAndre Przywara 				.flags	= cpu_to_fdt32(IRQ_TYPE_EDGE_RISING),
9716242d27SWill Deacon 			},
9816242d27SWill Deacon 		};
9916242d27SWill Deacon 
10016242d27SWill Deacon 		nentries++;
10116242d27SWill Deacon 		dev_hdr = device__next_dev(dev_hdr);
10216242d27SWill Deacon 	}
10316242d27SWill Deacon 
10416242d27SWill Deacon 	_FDT(fdt_property(fdt, "interrupt-map", irq_map,
10516242d27SWill Deacon 			  sizeof(struct of_interrupt_map_entry) * nentries));
10616242d27SWill Deacon 
10716242d27SWill Deacon 	/* ... and the corresponding mask. */
10816242d27SWill Deacon 	if (nentries) {
10916242d27SWill Deacon 		struct of_pci_irq_mask irq_mask = {
11016242d27SWill Deacon 			.pci_addr = {
11116242d27SWill Deacon 				.hi	= cpu_to_fdt32(of_pci_b_ddddd(-1)),
11216242d27SWill Deacon 				.mid	= 0,
11316242d27SWill Deacon 				.lo	= 0,
11416242d27SWill Deacon 			},
11516242d27SWill Deacon 			.pci_pin	= cpu_to_fdt32(7),
11616242d27SWill Deacon 		};
11716242d27SWill Deacon 
11816242d27SWill Deacon 		_FDT(fdt_property(fdt, "interrupt-map-mask", &irq_mask,
11916242d27SWill Deacon 				  sizeof(irq_mask)));
12016242d27SWill Deacon 	}
12116242d27SWill Deacon 
12216242d27SWill Deacon 	_FDT(fdt_end_node(fdt));
12316242d27SWill Deacon }
124