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; 2116242d27SWill Deacon struct of_gic_irq gic_irq; 2216242d27SWill Deacon } __attribute__((packed)); 2316242d27SWill Deacon 2416242d27SWill Deacon void pci__generate_fdt_nodes(void *fdt, u32 gic_phandle) 2516242d27SWill Deacon { 2616242d27SWill Deacon struct device_header *dev_hdr; 2716242d27SWill Deacon struct of_interrupt_map_entry irq_map[OF_PCI_IRQ_MAP_MAX]; 2816242d27SWill Deacon unsigned nentries = 0; 291fcf0d77SWill Deacon /* Bus range */ 301fcf0d77SWill Deacon u32 bus_range[] = { cpu_to_fdt32(0), cpu_to_fdt32(1), }; 311fcf0d77SWill Deacon /* Configuration Space */ 321fcf0d77SWill Deacon u64 cfg_reg_prop[] = { cpu_to_fdt64(KVM_PCI_CFG_AREA), 331fcf0d77SWill Deacon cpu_to_fdt64(ARM_PCI_CFG_SIZE), }; 341fcf0d77SWill Deacon /* Describe the memory ranges */ 3516242d27SWill Deacon struct of_pci_ranges_entry ranges[] = { 3616242d27SWill Deacon { 3716242d27SWill Deacon .pci_addr = { 3816242d27SWill Deacon .hi = cpu_to_fdt32(of_pci_b_ss(OF_PCI_SS_IO)), 3916242d27SWill Deacon .mid = 0, 4016242d27SWill Deacon .lo = 0, 4116242d27SWill Deacon }, 4216242d27SWill Deacon .cpu_addr = cpu_to_fdt64(KVM_IOPORT_AREA), 4316242d27SWill Deacon .length = cpu_to_fdt64(ARM_IOPORT_SIZE), 4416242d27SWill Deacon }, 4516242d27SWill Deacon { 4616242d27SWill Deacon .pci_addr = { 4716242d27SWill Deacon .hi = cpu_to_fdt32(of_pci_b_ss(OF_PCI_SS_M32)), 481fcf0d77SWill Deacon .mid = cpu_to_fdt32(KVM_PCI_MMIO_AREA >> 32), 491fcf0d77SWill Deacon .lo = cpu_to_fdt32(KVM_PCI_MMIO_AREA), 5016242d27SWill Deacon }, 5116242d27SWill Deacon .cpu_addr = cpu_to_fdt64(KVM_PCI_MMIO_AREA), 5216242d27SWill Deacon .length = cpu_to_fdt64(ARM_PCI_MMIO_SIZE), 5316242d27SWill Deacon }, 5416242d27SWill Deacon }; 5516242d27SWill Deacon 5616242d27SWill Deacon /* Boilerplate PCI properties */ 5716242d27SWill Deacon _FDT(fdt_begin_node(fdt, "pci")); 581fcf0d77SWill Deacon _FDT(fdt_property_string(fdt, "device_type", "pci")); 5916242d27SWill Deacon _FDT(fdt_property_cell(fdt, "#address-cells", 0x3)); 6016242d27SWill Deacon _FDT(fdt_property_cell(fdt, "#size-cells", 0x2)); 6116242d27SWill Deacon _FDT(fdt_property_cell(fdt, "#interrupt-cells", 0x1)); 621fcf0d77SWill Deacon _FDT(fdt_property_string(fdt, "compatible", "pci-host-cam-generic")); 63*9a8af7e3SRobin Murphy _FDT(fdt_property(fdt, "dma-coherent", NULL, 0)); 6416242d27SWill Deacon 651fcf0d77SWill Deacon _FDT(fdt_property(fdt, "bus-range", bus_range, sizeof(bus_range))); 661fcf0d77SWill Deacon _FDT(fdt_property(fdt, "reg", &cfg_reg_prop, sizeof(cfg_reg_prop))); 6716242d27SWill Deacon _FDT(fdt_property(fdt, "ranges", ranges, sizeof(ranges))); 6816242d27SWill Deacon 6916242d27SWill Deacon /* Generate the interrupt map ... */ 7016242d27SWill Deacon dev_hdr = device__first_dev(DEVICE_BUS_PCI); 7116242d27SWill Deacon while (dev_hdr && nentries < ARRAY_SIZE(irq_map)) { 7216242d27SWill Deacon struct of_interrupt_map_entry *entry = &irq_map[nentries]; 7316242d27SWill Deacon struct pci_device_header *pci_hdr = dev_hdr->data; 7416242d27SWill Deacon u8 dev_num = dev_hdr->dev_num; 7516242d27SWill Deacon u8 pin = pci_hdr->irq_pin; 7616242d27SWill Deacon u8 irq = pci_hdr->irq_line; 7716242d27SWill Deacon 7816242d27SWill Deacon *entry = (struct of_interrupt_map_entry) { 7916242d27SWill Deacon .pci_irq_mask = { 8016242d27SWill Deacon .pci_addr = { 8116242d27SWill Deacon .hi = cpu_to_fdt32(of_pci_b_ddddd(dev_num)), 8216242d27SWill Deacon .mid = 0, 8316242d27SWill Deacon .lo = 0, 8416242d27SWill Deacon }, 8516242d27SWill Deacon .pci_pin = cpu_to_fdt32(pin), 8616242d27SWill Deacon }, 8716242d27SWill Deacon .gic_phandle = cpu_to_fdt32(gic_phandle), 8816242d27SWill Deacon .gic_irq = { 8916242d27SWill Deacon .type = cpu_to_fdt32(GIC_FDT_IRQ_TYPE_SPI), 9016242d27SWill Deacon .num = cpu_to_fdt32(irq - GIC_SPI_IRQ_BASE), 91045fc040SAndre Przywara .flags = cpu_to_fdt32(IRQ_TYPE_EDGE_RISING), 9216242d27SWill Deacon }, 9316242d27SWill Deacon }; 9416242d27SWill Deacon 9516242d27SWill Deacon nentries++; 9616242d27SWill Deacon dev_hdr = device__next_dev(dev_hdr); 9716242d27SWill Deacon } 9816242d27SWill Deacon 9916242d27SWill Deacon _FDT(fdt_property(fdt, "interrupt-map", irq_map, 10016242d27SWill Deacon sizeof(struct of_interrupt_map_entry) * nentries)); 10116242d27SWill Deacon 10216242d27SWill Deacon /* ... and the corresponding mask. */ 10316242d27SWill Deacon if (nentries) { 10416242d27SWill Deacon struct of_pci_irq_mask irq_mask = { 10516242d27SWill Deacon .pci_addr = { 10616242d27SWill Deacon .hi = cpu_to_fdt32(of_pci_b_ddddd(-1)), 10716242d27SWill Deacon .mid = 0, 10816242d27SWill Deacon .lo = 0, 10916242d27SWill Deacon }, 11016242d27SWill Deacon .pci_pin = cpu_to_fdt32(7), 11116242d27SWill Deacon }; 11216242d27SWill Deacon 11316242d27SWill Deacon _FDT(fdt_property(fdt, "interrupt-map-mask", &irq_mask, 11416242d27SWill Deacon sizeof(irq_mask))); 11516242d27SWill Deacon } 11616242d27SWill Deacon 11716242d27SWill Deacon _FDT(fdt_end_node(fdt)); 11816242d27SWill Deacon } 119