1c481cfd5SMatt Evans /* 2c481cfd5SMatt Evans * SPAPR PHB emulation, RTAS interface to PCI config space, device tree nodes 3c481cfd5SMatt Evans * for enumerated devices. 4c481cfd5SMatt Evans * 5c481cfd5SMatt Evans * Borrowed heavily from QEMU's spapr_pci.c, 6c481cfd5SMatt Evans * Copyright (c) 2011 Alexey Kardashevskiy, IBM Corporation. 7c481cfd5SMatt Evans * Copyright (c) 2011 David Gibson, IBM Corporation. 8c481cfd5SMatt Evans * 9c481cfd5SMatt Evans * Modifications copyright 2011 Matt Evans <matt@ozlabs.org>, IBM Corporation. 10c481cfd5SMatt Evans * 11c481cfd5SMatt Evans * This program is free software; you can redistribute it and/or modify it 12c481cfd5SMatt Evans * under the terms of the GNU General Public License version 2 as published 13c481cfd5SMatt Evans * by the Free Software Foundation. 14c481cfd5SMatt Evans */ 15c481cfd5SMatt Evans 16c481cfd5SMatt Evans #include "spapr.h" 17c481cfd5SMatt Evans #include "spapr_pci.h" 18a1166a18SWill Deacon #include "kvm/devices.h" 191299331aSWill Deacon #include "kvm/fdt.h" 20c481cfd5SMatt Evans #include "kvm/util.h" 21*6606883cSWill Deacon #include "kvm/of_pci.h" 22c481cfd5SMatt Evans #include "kvm/pci.h" 23c481cfd5SMatt Evans 24c481cfd5SMatt Evans #include <linux/pci_regs.h> 25c481cfd5SMatt Evans #include <linux/byteorder.h> 26c481cfd5SMatt Evans 27c481cfd5SMatt Evans 28c481cfd5SMatt Evans /* #define DEBUG_PHB yes */ 29c481cfd5SMatt Evans #ifdef DEBUG_PHB 30c481cfd5SMatt Evans #define phb_dprintf(fmt, ...) \ 31c481cfd5SMatt Evans do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0) 32c481cfd5SMatt Evans #else 33c481cfd5SMatt Evans #define phb_dprintf(fmt, ...) \ 34c481cfd5SMatt Evans do { } while (0) 35c481cfd5SMatt Evans #endif 36c481cfd5SMatt Evans 37c481cfd5SMatt Evans static const uint32_t bars[] = { 38c481cfd5SMatt Evans PCI_BASE_ADDRESS_0, PCI_BASE_ADDRESS_1, 39c481cfd5SMatt Evans PCI_BASE_ADDRESS_2, PCI_BASE_ADDRESS_3, 40c481cfd5SMatt Evans PCI_BASE_ADDRESS_4, PCI_BASE_ADDRESS_5 41c481cfd5SMatt Evans /*, PCI_ROM_ADDRESS*/ 42c481cfd5SMatt Evans }; 43c481cfd5SMatt Evans 44c481cfd5SMatt Evans #define PCI_NUM_REGIONS 7 45c481cfd5SMatt Evans 46c481cfd5SMatt Evans static struct spapr_phb phb; 47c481cfd5SMatt Evans 48c481cfd5SMatt Evans static void rtas_ibm_read_pci_config(struct kvm_cpu *vcpu, 49c481cfd5SMatt Evans uint32_t token, uint32_t nargs, 50c481cfd5SMatt Evans target_ulong args, 51c481cfd5SMatt Evans uint32_t nret, target_ulong rets) 52c481cfd5SMatt Evans { 53c481cfd5SMatt Evans uint32_t val = 0; 54c481cfd5SMatt Evans uint64_t buid = ((uint64_t)rtas_ld(vcpu->kvm, args, 1) << 32) | rtas_ld(vcpu->kvm, args, 2); 55c481cfd5SMatt Evans union pci_config_address addr = { .w = rtas_ld(vcpu->kvm, args, 0) }; 56c481cfd5SMatt Evans struct pci_device_header *dev = pci__find_dev(addr.device_number); 57c481cfd5SMatt Evans uint32_t size = rtas_ld(vcpu->kvm, args, 3); 58c481cfd5SMatt Evans 59c481cfd5SMatt Evans if (buid != phb.buid || !dev || (size > 4)) { 60c481cfd5SMatt Evans phb_dprintf("- cfgRd buid 0x%lx cfg addr 0x%x size %d not found\n", 61c481cfd5SMatt Evans buid, addr.w, size); 62c481cfd5SMatt Evans 63c481cfd5SMatt Evans rtas_st(vcpu->kvm, rets, 0, -1); 64c481cfd5SMatt Evans return; 65c481cfd5SMatt Evans } 66c481cfd5SMatt Evans pci__config_rd(vcpu->kvm, addr, &val, size); 67c481cfd5SMatt Evans /* It appears this wants a byteswapped result... */ 68c481cfd5SMatt Evans switch (size) { 69c481cfd5SMatt Evans case 4: 70c481cfd5SMatt Evans val = le32_to_cpu(val); 71c481cfd5SMatt Evans break; 72c481cfd5SMatt Evans case 2: 73c481cfd5SMatt Evans val = le16_to_cpu(val>>16); 74c481cfd5SMatt Evans break; 75c481cfd5SMatt Evans case 1: 76c481cfd5SMatt Evans val = val >> 24; 77c481cfd5SMatt Evans break; 78c481cfd5SMatt Evans } 79c481cfd5SMatt Evans phb_dprintf("- cfgRd buid 0x%lx addr 0x%x (/%d): b%d,d%d,f%d,r0x%x, val 0x%x\n", 80c481cfd5SMatt Evans buid, addr.w, size, addr.bus_number, addr.device_number, addr.function_number, 81c481cfd5SMatt Evans addr.register_number, val); 82c481cfd5SMatt Evans 83c481cfd5SMatt Evans rtas_st(vcpu->kvm, rets, 0, 0); 84c481cfd5SMatt Evans rtas_st(vcpu->kvm, rets, 1, val); 85c481cfd5SMatt Evans } 86c481cfd5SMatt Evans 87c481cfd5SMatt Evans static void rtas_read_pci_config(struct kvm_cpu *vcpu, 88c481cfd5SMatt Evans uint32_t token, uint32_t nargs, 89c481cfd5SMatt Evans target_ulong args, 90c481cfd5SMatt Evans uint32_t nret, target_ulong rets) 91c481cfd5SMatt Evans { 92c481cfd5SMatt Evans uint32_t val; 93c481cfd5SMatt Evans union pci_config_address addr = { .w = rtas_ld(vcpu->kvm, args, 0) }; 94c481cfd5SMatt Evans struct pci_device_header *dev = pci__find_dev(addr.device_number); 95c481cfd5SMatt Evans uint32_t size = rtas_ld(vcpu->kvm, args, 1); 96c481cfd5SMatt Evans 97c481cfd5SMatt Evans if (!dev || (size > 4)) { 98c481cfd5SMatt Evans rtas_st(vcpu->kvm, rets, 0, -1); 99c481cfd5SMatt Evans return; 100c481cfd5SMatt Evans } 101c481cfd5SMatt Evans pci__config_rd(vcpu->kvm, addr, &val, size); 102c481cfd5SMatt Evans switch (size) { 103c481cfd5SMatt Evans case 4: 104c481cfd5SMatt Evans val = le32_to_cpu(val); 105c481cfd5SMatt Evans break; 106c481cfd5SMatt Evans case 2: 107c481cfd5SMatt Evans val = le16_to_cpu(val>>16); /* We're yuck-endian. */ 108c481cfd5SMatt Evans break; 109c481cfd5SMatt Evans case 1: 110c481cfd5SMatt Evans val = val >> 24; 111c481cfd5SMatt Evans break; 112c481cfd5SMatt Evans } 113c481cfd5SMatt Evans phb_dprintf("- cfgRd addr 0x%x size %d, val 0x%x\n", addr.w, size, val); 114c481cfd5SMatt Evans rtas_st(vcpu->kvm, rets, 0, 0); 115c481cfd5SMatt Evans rtas_st(vcpu->kvm, rets, 1, val); 116c481cfd5SMatt Evans } 117c481cfd5SMatt Evans 118c481cfd5SMatt Evans static void rtas_ibm_write_pci_config(struct kvm_cpu *vcpu, 119c481cfd5SMatt Evans uint32_t token, uint32_t nargs, 120c481cfd5SMatt Evans target_ulong args, 121c481cfd5SMatt Evans uint32_t nret, target_ulong rets) 122c481cfd5SMatt Evans { 123c481cfd5SMatt Evans uint64_t buid = ((uint64_t)rtas_ld(vcpu->kvm, args, 1) << 32) | rtas_ld(vcpu->kvm, args, 2); 124c481cfd5SMatt Evans union pci_config_address addr = { .w = rtas_ld(vcpu->kvm, args, 0) }; 125c481cfd5SMatt Evans struct pci_device_header *dev = pci__find_dev(addr.device_number); 126c481cfd5SMatt Evans uint32_t size = rtas_ld(vcpu->kvm, args, 3); 127c481cfd5SMatt Evans uint32_t val = rtas_ld(vcpu->kvm, args, 4); 128c481cfd5SMatt Evans 129c481cfd5SMatt Evans if (buid != phb.buid || !dev || (size > 4)) { 130c481cfd5SMatt Evans phb_dprintf("- cfgWr buid 0x%lx cfg addr 0x%x/%d error (val 0x%x)\n", 131c481cfd5SMatt Evans buid, addr.w, size, val); 132c481cfd5SMatt Evans 133c481cfd5SMatt Evans rtas_st(vcpu->kvm, rets, 0, -1); 134c481cfd5SMatt Evans return; 135c481cfd5SMatt Evans } 136c481cfd5SMatt Evans phb_dprintf("- cfgWr buid 0x%lx addr 0x%x (/%d): b%d,d%d,f%d,r0x%x, val 0x%x\n", 137c481cfd5SMatt Evans buid, addr.w, size, addr.bus_number, addr.device_number, addr.function_number, 138c481cfd5SMatt Evans addr.register_number, val); 139c481cfd5SMatt Evans switch (size) { 140c481cfd5SMatt Evans case 4: 141c481cfd5SMatt Evans val = le32_to_cpu(val); 142c481cfd5SMatt Evans break; 143c481cfd5SMatt Evans case 2: 144c481cfd5SMatt Evans val = le16_to_cpu(val) << 16; 145c481cfd5SMatt Evans break; 146c481cfd5SMatt Evans case 1: 147c481cfd5SMatt Evans val = val >> 24; 148c481cfd5SMatt Evans break; 149c481cfd5SMatt Evans } 150c481cfd5SMatt Evans pci__config_wr(vcpu->kvm, addr, &val, size); 151c481cfd5SMatt Evans rtas_st(vcpu->kvm, rets, 0, 0); 152c481cfd5SMatt Evans } 153c481cfd5SMatt Evans 154c481cfd5SMatt Evans static void rtas_write_pci_config(struct kvm_cpu *vcpu, 155c481cfd5SMatt Evans uint32_t token, uint32_t nargs, 156c481cfd5SMatt Evans target_ulong args, 157c481cfd5SMatt Evans uint32_t nret, target_ulong rets) 158c481cfd5SMatt Evans { 159c481cfd5SMatt Evans union pci_config_address addr = { .w = rtas_ld(vcpu->kvm, args, 0) }; 160c481cfd5SMatt Evans struct pci_device_header *dev = pci__find_dev(addr.device_number); 161c481cfd5SMatt Evans uint32_t size = rtas_ld(vcpu->kvm, args, 1); 162c481cfd5SMatt Evans uint32_t val = rtas_ld(vcpu->kvm, args, 2); 163c481cfd5SMatt Evans 164c481cfd5SMatt Evans if (!dev || (size > 4)) { 165c481cfd5SMatt Evans rtas_st(vcpu->kvm, rets, 0, -1); 166c481cfd5SMatt Evans return; 167c481cfd5SMatt Evans } 168c481cfd5SMatt Evans 169c481cfd5SMatt Evans phb_dprintf("- cfgWr addr 0x%x (/%d): b%d,d%d,f%d,r0x%x, val 0x%x\n", 170c481cfd5SMatt Evans addr.w, size, addr.bus_number, addr.device_number, addr.function_number, 171c481cfd5SMatt Evans addr.register_number, val); 172c481cfd5SMatt Evans switch (size) { 173c481cfd5SMatt Evans case 4: 174c481cfd5SMatt Evans val = le32_to_cpu(val); 175c481cfd5SMatt Evans break; 176c481cfd5SMatt Evans case 2: 177c481cfd5SMatt Evans val = le16_to_cpu(val) << 16; 178c481cfd5SMatt Evans break; 179c481cfd5SMatt Evans case 1: 180c481cfd5SMatt Evans val = val >> 24; 181c481cfd5SMatt Evans break; 182c481cfd5SMatt Evans } 183c481cfd5SMatt Evans pci__config_wr(vcpu->kvm, addr, &val, size); 184c481cfd5SMatt Evans rtas_st(vcpu->kvm, rets, 0, 0); 185c481cfd5SMatt Evans } 186c481cfd5SMatt Evans 187c481cfd5SMatt Evans void spapr_create_phb(struct kvm *kvm, 188c481cfd5SMatt Evans const char *busname, uint64_t buid, 189c481cfd5SMatt Evans uint64_t mem_win_addr, uint64_t mem_win_size, 190c481cfd5SMatt Evans uint64_t io_win_addr, uint64_t io_win_size) 191c481cfd5SMatt Evans { 192c481cfd5SMatt Evans /* 193c481cfd5SMatt Evans * Since kvmtool doesn't really have any concept of buses etc., 194c481cfd5SMatt Evans * there's nothing to register here. Just register RTAS. 195c481cfd5SMatt Evans */ 196c481cfd5SMatt Evans spapr_rtas_register("read-pci-config", rtas_read_pci_config); 197c481cfd5SMatt Evans spapr_rtas_register("write-pci-config", rtas_write_pci_config); 198c481cfd5SMatt Evans spapr_rtas_register("ibm,read-pci-config", rtas_ibm_read_pci_config); 199c481cfd5SMatt Evans spapr_rtas_register("ibm,write-pci-config", rtas_ibm_write_pci_config); 200c481cfd5SMatt Evans 201c481cfd5SMatt Evans phb.buid = buid; 202c481cfd5SMatt Evans phb.mem_addr = mem_win_addr; 203c481cfd5SMatt Evans phb.mem_size = mem_win_size; 204c481cfd5SMatt Evans phb.io_addr = io_win_addr; 205c481cfd5SMatt Evans phb.io_size = io_win_size; 206c481cfd5SMatt Evans 20742ac24f9SSasha Levin kvm->arch.phb = &phb; 208c481cfd5SMatt Evans } 209c481cfd5SMatt Evans 210c481cfd5SMatt Evans static uint32_t bar_to_ss(unsigned long bar) 211c481cfd5SMatt Evans { 212c481cfd5SMatt Evans if ((bar & PCI_BASE_ADDRESS_SPACE) == 213c481cfd5SMatt Evans PCI_BASE_ADDRESS_SPACE_IO) 214*6606883cSWill Deacon return OF_PCI_SS_IO; 215c481cfd5SMatt Evans else if (bar & PCI_BASE_ADDRESS_MEM_TYPE_64) 216*6606883cSWill Deacon return OF_PCI_SS_M64; 217c481cfd5SMatt Evans else 218*6606883cSWill Deacon return OF_PCI_SS_M32; 219c481cfd5SMatt Evans } 220c481cfd5SMatt Evans 221c481cfd5SMatt Evans static unsigned long bar_to_addr(unsigned long bar) 222c481cfd5SMatt Evans { 223c481cfd5SMatt Evans if ((bar & PCI_BASE_ADDRESS_SPACE) == 224c481cfd5SMatt Evans PCI_BASE_ADDRESS_SPACE_IO) 225c481cfd5SMatt Evans return bar & PCI_BASE_ADDRESS_IO_MASK; 226c481cfd5SMatt Evans else 227c481cfd5SMatt Evans return bar & PCI_BASE_ADDRESS_MEM_MASK; 228c481cfd5SMatt Evans } 229c481cfd5SMatt Evans 230c481cfd5SMatt Evans int spapr_populate_pci_devices(struct kvm *kvm, 231c481cfd5SMatt Evans uint32_t xics_phandle, 232c481cfd5SMatt Evans void *fdt) 233c481cfd5SMatt Evans { 234c481cfd5SMatt Evans int bus_off, node_off = 0, devid, fn, i, n, devices; 235a1166a18SWill Deacon struct device_header *dev_hdr; 236c481cfd5SMatt Evans char nodename[256]; 237*6606883cSWill Deacon struct of_pci_unit_address reg[PCI_NUM_REGIONS + 1], 238c481cfd5SMatt Evans assigned_addresses[PCI_NUM_REGIONS]; 239c481cfd5SMatt Evans uint32_t bus_range[] = { cpu_to_be32(0), cpu_to_be32(0xff) }; 240*6606883cSWill Deacon struct of_pci_ranges_entry ranges[] = { 241c481cfd5SMatt Evans { 242*6606883cSWill Deacon { 243*6606883cSWill Deacon cpu_to_be32(of_pci_b_ss(1)), 244*6606883cSWill Deacon cpu_to_be32(0), 245*6606883cSWill Deacon cpu_to_be32(0), 246*6606883cSWill Deacon }, 247c481cfd5SMatt Evans cpu_to_be64(phb.io_addr), 248c481cfd5SMatt Evans cpu_to_be64(phb.io_size), 249c481cfd5SMatt Evans }, 250c481cfd5SMatt Evans { 251*6606883cSWill Deacon { 252*6606883cSWill Deacon cpu_to_be32(of_pci_b_ss(2)), 253*6606883cSWill Deacon cpu_to_be32(0), 254*6606883cSWill Deacon cpu_to_be32(0), 255*6606883cSWill Deacon }, 256c481cfd5SMatt Evans cpu_to_be64(phb.mem_addr), 257c481cfd5SMatt Evans cpu_to_be64(phb.mem_size), 258c481cfd5SMatt Evans }, 259c481cfd5SMatt Evans }; 260c481cfd5SMatt Evans uint64_t bus_reg[] = { cpu_to_be64(phb.buid), 0 }; 261c481cfd5SMatt Evans uint32_t interrupt_map_mask[] = { 262*6606883cSWill Deacon cpu_to_be32(of_pci_b_ddddd(-1)|of_pci_b_fff(-1)), 0x0, 0x0, 0x0}; 263c481cfd5SMatt Evans uint32_t interrupt_map[SPAPR_PCI_NUM_LSI][7]; 264c481cfd5SMatt Evans 265c481cfd5SMatt Evans /* Start populating the FDT */ 266c481cfd5SMatt Evans sprintf(nodename, "pci@%" PRIx64, phb.buid); 267c481cfd5SMatt Evans bus_off = fdt_add_subnode(fdt, 0, nodename); 268c481cfd5SMatt Evans if (bus_off < 0) { 269c481cfd5SMatt Evans die("error making bus subnode, %s\n", fdt_strerror(bus_off)); 270c481cfd5SMatt Evans return bus_off; 271c481cfd5SMatt Evans } 272c481cfd5SMatt Evans 273c481cfd5SMatt Evans /* Write PHB properties */ 274c481cfd5SMatt Evans _FDT(fdt_setprop_string(fdt, bus_off, "device_type", "pci")); 275c481cfd5SMatt Evans _FDT(fdt_setprop_string(fdt, bus_off, "compatible", "IBM,Logical_PHB")); 276c481cfd5SMatt Evans _FDT(fdt_setprop_cell(fdt, bus_off, "#address-cells", 0x3)); 277c481cfd5SMatt Evans _FDT(fdt_setprop_cell(fdt, bus_off, "#size-cells", 0x2)); 278c481cfd5SMatt Evans _FDT(fdt_setprop_cell(fdt, bus_off, "#interrupt-cells", 0x1)); 279c481cfd5SMatt Evans _FDT(fdt_setprop(fdt, bus_off, "used-by-rtas", NULL, 0)); 280c481cfd5SMatt Evans _FDT(fdt_setprop(fdt, bus_off, "bus-range", &bus_range, sizeof(bus_range))); 281c481cfd5SMatt Evans _FDT(fdt_setprop(fdt, bus_off, "ranges", &ranges, sizeof(ranges))); 282c481cfd5SMatt Evans _FDT(fdt_setprop(fdt, bus_off, "reg", &bus_reg, sizeof(bus_reg))); 283c481cfd5SMatt Evans _FDT(fdt_setprop(fdt, bus_off, "interrupt-map-mask", 284c481cfd5SMatt Evans &interrupt_map_mask, sizeof(interrupt_map_mask))); 285c481cfd5SMatt Evans 286c481cfd5SMatt Evans /* Populate PCI devices and allocate IRQs */ 287c481cfd5SMatt Evans devices = 0; 288a1166a18SWill Deacon dev_hdr = device__first_dev(DEVICE_BUS_PCI); 289a1166a18SWill Deacon while (dev_hdr) { 290c481cfd5SMatt Evans uint32_t *irqmap = interrupt_map[devices]; 291a1166a18SWill Deacon struct pci_device_header *hdr = dev_hdr->data; 292c481cfd5SMatt Evans 293c481cfd5SMatt Evans if (!hdr) 294c481cfd5SMatt Evans continue; 295c481cfd5SMatt Evans 296a1166a18SWill Deacon devid = dev_hdr->dev_num; 297c481cfd5SMatt Evans fn = 0; /* kvmtool doesn't yet do multifunction devices */ 298c481cfd5SMatt Evans 299c481cfd5SMatt Evans sprintf(nodename, "pci@%u,%u", devid, fn); 300c481cfd5SMatt Evans 301c481cfd5SMatt Evans /* Allocate interrupt from the map */ 302c481cfd5SMatt Evans if (devid > SPAPR_PCI_NUM_LSI) { 303c481cfd5SMatt Evans die("Unexpected behaviour in spapr_populate_pci_devices," 304c481cfd5SMatt Evans "wrong devid %u\n", devid); 305c481cfd5SMatt Evans } 306*6606883cSWill Deacon irqmap[0] = cpu_to_be32(of_pci_b_ddddd(devid)|of_pci_b_fff(fn)); 307c481cfd5SMatt Evans irqmap[1] = 0; 308c481cfd5SMatt Evans irqmap[2] = 0; 309c481cfd5SMatt Evans irqmap[3] = 0; 310c481cfd5SMatt Evans irqmap[4] = cpu_to_be32(xics_phandle); 311c481cfd5SMatt Evans /* 312c481cfd5SMatt Evans * This is nasty; the PCI devs are set up such that their own 313c481cfd5SMatt Evans * header's irq_line indicates the direct XICS IRQ number to 314c481cfd5SMatt Evans * use. There REALLY needs to be a hierarchical system in place 315c481cfd5SMatt Evans * to 'raise' an IRQ on the bridge which indexes/looks up which 316c481cfd5SMatt Evans * XICS IRQ to fire. 317c481cfd5SMatt Evans */ 318c481cfd5SMatt Evans irqmap[5] = cpu_to_be32(hdr->irq_line); 319c481cfd5SMatt Evans irqmap[6] = cpu_to_be32(0x8); 320c481cfd5SMatt Evans 321c481cfd5SMatt Evans /* Add node to FDT */ 322c481cfd5SMatt Evans node_off = fdt_add_subnode(fdt, bus_off, nodename); 323c481cfd5SMatt Evans if (node_off < 0) { 324c481cfd5SMatt Evans die("error making node subnode, %s\n", fdt_strerror(bus_off)); 325c481cfd5SMatt Evans return node_off; 326c481cfd5SMatt Evans } 327c481cfd5SMatt Evans 328c481cfd5SMatt Evans _FDT(fdt_setprop_cell(fdt, node_off, "vendor-id", 329c481cfd5SMatt Evans le16_to_cpu(hdr->vendor_id))); 330c481cfd5SMatt Evans _FDT(fdt_setprop_cell(fdt, node_off, "device-id", 331c481cfd5SMatt Evans le16_to_cpu(hdr->device_id))); 332c481cfd5SMatt Evans _FDT(fdt_setprop_cell(fdt, node_off, "revision-id", 333c481cfd5SMatt Evans hdr->revision_id)); 334c481cfd5SMatt Evans _FDT(fdt_setprop_cell(fdt, node_off, "class-code", 335c481cfd5SMatt Evans hdr->class[0] | (hdr->class[1] << 8) | (hdr->class[2] << 16))); 336c481cfd5SMatt Evans _FDT(fdt_setprop_cell(fdt, node_off, "subsystem-id", 337c481cfd5SMatt Evans le16_to_cpu(hdr->subsys_id))); 338c481cfd5SMatt Evans _FDT(fdt_setprop_cell(fdt, node_off, "subsystem-vendor-id", 339c481cfd5SMatt Evans le16_to_cpu(hdr->subsys_vendor_id))); 340c481cfd5SMatt Evans 341c481cfd5SMatt Evans /* Config space region comes first */ 342c481cfd5SMatt Evans reg[0].hi = cpu_to_be32( 343*6606883cSWill Deacon of_pci_b_n(0) | 344*6606883cSWill Deacon of_pci_b_p(0) | 345*6606883cSWill Deacon of_pci_b_t(0) | 346*6606883cSWill Deacon of_pci_b_ss(OF_PCI_SS_CONFIG) | 347*6606883cSWill Deacon of_pci_b_bbbbbbbb(0) | 348*6606883cSWill Deacon of_pci_b_ddddd(devid) | 349*6606883cSWill Deacon of_pci_b_fff(fn)); 350*6606883cSWill Deacon reg[0].mid = 0; 351*6606883cSWill Deacon reg[0].lo = 0; 352c481cfd5SMatt Evans 353c481cfd5SMatt Evans n = 0; 354c481cfd5SMatt Evans /* Six BARs, no ROM supported, addresses are 32bit */ 355c481cfd5SMatt Evans for (i = 0; i < 6; ++i) { 356c481cfd5SMatt Evans if (0 == hdr->bar[i]) { 357c481cfd5SMatt Evans continue; 358c481cfd5SMatt Evans } 359c481cfd5SMatt Evans 360c481cfd5SMatt Evans reg[n+1].hi = cpu_to_be32( 361*6606883cSWill Deacon of_pci_b_n(0) | 362*6606883cSWill Deacon of_pci_b_p(0) | 363*6606883cSWill Deacon of_pci_b_t(0) | 364*6606883cSWill Deacon of_pci_b_ss(bar_to_ss(le32_to_cpu(hdr->bar[i]))) | 365*6606883cSWill Deacon of_pci_b_bbbbbbbb(0) | 366*6606883cSWill Deacon of_pci_b_ddddd(devid) | 367*6606883cSWill Deacon of_pci_b_fff(fn) | 368*6606883cSWill Deacon of_pci_b_rrrrrrrr(bars[i])); 369*6606883cSWill Deacon reg[n+1].mid = 0; 370*6606883cSWill Deacon reg[n+1].lo = cpu_to_be64(hdr->bar_size[i]); 371c481cfd5SMatt Evans 372c481cfd5SMatt Evans assigned_addresses[n].hi = cpu_to_be32( 373*6606883cSWill Deacon of_pci_b_n(1) | 374*6606883cSWill Deacon of_pci_b_p(0) | 375*6606883cSWill Deacon of_pci_b_t(0) | 376*6606883cSWill Deacon of_pci_b_ss(bar_to_ss(le32_to_cpu(hdr->bar[i]))) | 377*6606883cSWill Deacon of_pci_b_bbbbbbbb(0) | 378*6606883cSWill Deacon of_pci_b_ddddd(devid) | 379*6606883cSWill Deacon of_pci_b_fff(fn) | 380*6606883cSWill Deacon of_pci_b_rrrrrrrr(bars[i])); 381c481cfd5SMatt Evans 382c481cfd5SMatt Evans /* 383c481cfd5SMatt Evans * Writing zeroes to assigned_addresses causes the guest kernel to 384c481cfd5SMatt Evans * reassign BARs 385c481cfd5SMatt Evans */ 386*6606883cSWill Deacon assigned_addresses[n].mid = cpu_to_be64(bar_to_addr(le32_to_cpu(hdr->bar[i]))); 387*6606883cSWill Deacon assigned_addresses[n].lo = reg[n+1].lo; 388c481cfd5SMatt Evans 389c481cfd5SMatt Evans ++n; 390c481cfd5SMatt Evans } 391c481cfd5SMatt Evans _FDT(fdt_setprop(fdt, node_off, "reg", reg, sizeof(reg[0])*(n+1))); 392c481cfd5SMatt Evans _FDT(fdt_setprop(fdt, node_off, "assigned-addresses", 393c481cfd5SMatt Evans assigned_addresses, 394c481cfd5SMatt Evans sizeof(assigned_addresses[0])*(n))); 395c481cfd5SMatt Evans _FDT(fdt_setprop_cell(fdt, node_off, "interrupts", 396c481cfd5SMatt Evans hdr->irq_pin)); 397c481cfd5SMatt Evans 398c481cfd5SMatt Evans /* We don't set ibm,dma-window property as we don't have an IOMMU. */ 399c481cfd5SMatt Evans 400c481cfd5SMatt Evans ++devices; 401a1166a18SWill Deacon dev_hdr = device__next_dev(dev_hdr); 402c481cfd5SMatt Evans } 403c481cfd5SMatt Evans 404c481cfd5SMatt Evans /* Write interrupt map */ 405c481cfd5SMatt Evans _FDT(fdt_setprop(fdt, bus_off, "interrupt-map", &interrupt_map, 406c481cfd5SMatt Evans devices * sizeof(interrupt_map[0]))); 407c481cfd5SMatt Evans 408c481cfd5SMatt Evans return 0; 409c481cfd5SMatt Evans } 410