2 * QEMU sPAPR PCI host originated from Uninorth PCI host
4 * Copyright (c) 2011 Alexey Kardashevskiy, IBM Corporation.
5 * Copyright (C) 2011 David Gibson, IBM Corporation.
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
29 #include "hw/spapr_pci.h"
30 #include "exec-memory.h"
33 #include "hw/pci_internals.h"
35 static PCIDevice
*find_dev(sPAPREnvironment
*spapr
,
36 uint64_t buid
, uint32_t config_addr
)
39 int devfn
= (config_addr
>> 8) & 0xFF;
42 QLIST_FOREACH(phb
, &spapr
->phbs
, list
) {
43 if (phb
->buid
!= buid
) {
47 QTAILQ_FOREACH(qdev
, &phb
->host_state
.bus
->qbus
.children
, sibling
) {
48 PCIDevice
*dev
= (PCIDevice
*)qdev
;
49 if (dev
->devfn
== devfn
) {
58 static uint32_t rtas_pci_cfgaddr(uint32_t arg
)
60 return ((arg
>> 20) & 0xf00) | (arg
& 0xff);
63 static uint32_t rtas_read_pci_config_do(PCIDevice
*pci_dev
, uint32_t addr
,
64 uint32_t limit
, uint32_t len
)
66 if ((addr
+ len
) <= limit
) {
67 return pci_host_config_read_common(pci_dev
, addr
, limit
, len
);
73 static void rtas_write_pci_config_do(PCIDevice
*pci_dev
, uint32_t addr
,
74 uint32_t limit
, uint32_t val
,
77 if ((addr
+ len
) <= limit
) {
78 pci_host_config_write_common(pci_dev
, addr
, limit
, val
, len
);
82 static void rtas_ibm_read_pci_config(sPAPREnvironment
*spapr
,
83 uint32_t token
, uint32_t nargs
,
85 uint32_t nret
, target_ulong rets
)
87 uint32_t val
, size
, addr
;
88 uint64_t buid
= ((uint64_t)rtas_ld(args
, 1) << 32) | rtas_ld(args
, 2);
89 PCIDevice
*dev
= find_dev(spapr
, buid
, rtas_ld(args
, 0));
95 size
= rtas_ld(args
, 3);
96 addr
= rtas_pci_cfgaddr(rtas_ld(args
, 0));
97 val
= rtas_read_pci_config_do(dev
, addr
, pci_config_size(dev
), size
);
99 rtas_st(rets
, 1, val
);
102 static void rtas_read_pci_config(sPAPREnvironment
*spapr
,
103 uint32_t token
, uint32_t nargs
,
105 uint32_t nret
, target_ulong rets
)
107 uint32_t val
, size
, addr
;
108 PCIDevice
*dev
= find_dev(spapr
, 0, rtas_ld(args
, 0));
111 rtas_st(rets
, 0, -1);
114 size
= rtas_ld(args
, 1);
115 addr
= rtas_pci_cfgaddr(rtas_ld(args
, 0));
116 val
= rtas_read_pci_config_do(dev
, addr
, pci_config_size(dev
), size
);
118 rtas_st(rets
, 1, val
);
121 static void rtas_ibm_write_pci_config(sPAPREnvironment
*spapr
,
122 uint32_t token
, uint32_t nargs
,
124 uint32_t nret
, target_ulong rets
)
126 uint32_t val
, size
, addr
;
127 uint64_t buid
= ((uint64_t)rtas_ld(args
, 1) << 32) | rtas_ld(args
, 2);
128 PCIDevice
*dev
= find_dev(spapr
, buid
, rtas_ld(args
, 0));
131 rtas_st(rets
, 0, -1);
134 val
= rtas_ld(args
, 4);
135 size
= rtas_ld(args
, 3);
136 addr
= rtas_pci_cfgaddr(rtas_ld(args
, 0));
137 rtas_write_pci_config_do(dev
, addr
, pci_config_size(dev
), val
, size
);
141 static void rtas_write_pci_config(sPAPREnvironment
*spapr
,
142 uint32_t token
, uint32_t nargs
,
144 uint32_t nret
, target_ulong rets
)
146 uint32_t val
, size
, addr
;
147 PCIDevice
*dev
= find_dev(spapr
, 0, rtas_ld(args
, 0));
150 rtas_st(rets
, 0, -1);
153 val
= rtas_ld(args
, 2);
154 size
= rtas_ld(args
, 1);
155 addr
= rtas_pci_cfgaddr(rtas_ld(args
, 0));
156 rtas_write_pci_config_do(dev
, addr
, pci_config_size(dev
), val
, size
);
160 static int pci_spapr_map_irq(PCIDevice
*pci_dev
, int irq_num
)
163 * Here we need to convert pci_dev + irq_num to some unique value
164 * which is less than number of IRQs on the specific bus (now it
165 * is 16). At the moment irq_num == device_id (number of the
167 * FIXME: we should swizzle in fn and irq_num
169 return (pci_dev
->devfn
>> 3) % SPAPR_PCI_NUM_LSI
;
172 static void pci_spapr_set_irq(void *opaque
, int irq_num
, int level
)
175 * Here we use the number returned by pci_spapr_map_irq to find a
176 * corresponding qemu_irq.
178 sPAPRPHBState
*phb
= opaque
;
180 qemu_set_irq(phb
->lsi_table
[irq_num
].qirq
, level
);
183 static int spapr_phb_init(SysBusDevice
*s
)
185 sPAPRPHBState
*phb
= FROM_SYSBUS(sPAPRPHBState
, s
);
188 /* Initialize the LSI table */
189 for (i
= 0; i
< SPAPR_PCI_NUM_LSI
; i
++) {
193 qirq
= spapr_allocate_irq(0, &num
);
198 phb
->lsi_table
[i
].dt_irq
= num
;
199 phb
->lsi_table
[i
].qirq
= qirq
;
205 static void spapr_phb_class_init(ObjectClass
*klass
, void *data
)
207 SysBusDeviceClass
*sdc
= SYS_BUS_DEVICE_CLASS(klass
);
209 sdc
->init
= spapr_phb_init
;
212 static TypeInfo spapr_phb_info
= {
213 .name
= "spapr-pci-host-bridge",
214 .parent
= TYPE_SYS_BUS_DEVICE
,
215 .instance_size
= sizeof(sPAPRPHBState
),
216 .class_init
= spapr_phb_class_init
,
219 static void spapr_register_types(void)
221 type_register_static(&spapr_phb_info
);
224 type_init(spapr_register_types
)
226 static uint64_t spapr_io_read(void *opaque
, target_phys_addr_t addr
,
231 return cpu_inb(addr
);
233 return cpu_inw(addr
);
235 return cpu_inl(addr
);
240 static void spapr_io_write(void *opaque
, target_phys_addr_t addr
,
241 uint64_t data
, unsigned size
)
245 cpu_outb(addr
, data
);
248 cpu_outw(addr
, data
);
251 cpu_outl(addr
, data
);
257 static const MemoryRegionOps spapr_io_ops
= {
258 .endianness
= DEVICE_LITTLE_ENDIAN
,
259 .read
= spapr_io_read
,
260 .write
= spapr_io_write
263 void spapr_create_phb(sPAPREnvironment
*spapr
,
264 const char *busname
, uint64_t buid
,
265 uint64_t mem_win_addr
, uint64_t mem_win_size
,
266 uint64_t io_win_addr
)
272 char namebuf
[strlen(busname
)+11];
274 dev
= qdev_create(NULL
, "spapr-pci-host-bridge");
275 qdev_init_nofail(dev
);
276 s
= sysbus_from_qdev(dev
);
277 phb
= FROM_SYSBUS(sPAPRPHBState
, s
);
279 phb
->mem_win_addr
= mem_win_addr
;
281 sprintf(namebuf
, "%s-mem", busname
);
282 memory_region_init(&phb
->memspace
, namebuf
, INT64_MAX
);
284 sprintf(namebuf
, "%s-memwindow", busname
);
285 memory_region_init_alias(&phb
->memwindow
, namebuf
, &phb
->memspace
,
286 SPAPR_PCI_MEM_WIN_BUS_OFFSET
, mem_win_size
);
287 memory_region_add_subregion(get_system_memory(), mem_win_addr
,
290 phb
->io_win_addr
= io_win_addr
;
292 /* On ppc, we only have MMIO no specific IO space from the CPU
293 * perspective. In theory we ought to be able to embed the PCI IO
294 * memory region direction in the system memory space. However,
295 * if any of the IO BAR subregions use the old_portio mechanism,
296 * that won't be processed properly unless accessed from the
297 * system io address space. This hack to bounce things via
298 * system_io works around the problem until all the users of
299 * old_portion are updated */
300 sprintf(namebuf
, "%s-io", busname
);
301 memory_region_init(&phb
->iospace
, namebuf
, SPAPR_PCI_IO_WIN_SIZE
);
302 /* FIXME: fix to support multiple PHBs */
303 memory_region_add_subregion(get_system_io(), 0, &phb
->iospace
);
305 sprintf(namebuf
, "%s-iowindow", busname
);
306 memory_region_init_io(&phb
->iowindow
, &spapr_io_ops
, phb
,
307 namebuf
, SPAPR_PCI_IO_WIN_SIZE
);
308 memory_region_add_subregion(get_system_memory(), io_win_addr
,
311 phb
->host_state
.bus
= bus
= pci_register_bus(&phb
->busdev
.qdev
, busname
,
315 &phb
->memspace
, &phb
->iospace
,
319 spapr_rtas_register("read-pci-config", rtas_read_pci_config
);
320 spapr_rtas_register("write-pci-config", rtas_write_pci_config
);
321 spapr_rtas_register("ibm,read-pci-config", rtas_ibm_read_pci_config
);
322 spapr_rtas_register("ibm,write-pci-config", rtas_ibm_write_pci_config
);
324 QLIST_INSERT_HEAD(&spapr
->phbs
, phb
, list
);
326 /* pci_bus_set_mem_base(bus, mem_va_start - SPAPR_PCI_MEM_BAR_START); */
329 /* Macros to operate with address in OF binding to PCI */
330 #define b_x(x, p, l) (((x) & ((1<<(l))-1)) << (p))
331 #define b_n(x) b_x((x), 31, 1) /* 0 if relocatable */
332 #define b_p(x) b_x((x), 30, 1) /* 1 if prefetchable */
333 #define b_t(x) b_x((x), 29, 1) /* 1 if the address is aliased */
334 #define b_ss(x) b_x((x), 24, 2) /* the space code */
335 #define b_bbbbbbbb(x) b_x((x), 16, 8) /* bus number */
336 #define b_ddddd(x) b_x((x), 11, 5) /* device number */
337 #define b_fff(x) b_x((x), 8, 3) /* function number */
338 #define b_rrrrrrrr(x) b_x((x), 0, 8) /* register number */
340 int spapr_populate_pci_devices(sPAPRPHBState
*phb
,
341 uint32_t xics_phandle
,
344 PCIBus
*bus
= phb
->host_state
.bus
;
347 uint32_t bus_range
[] = { cpu_to_be32(0), cpu_to_be32(0xff) };
353 } __attribute__((packed
)) ranges
[] = {
355 cpu_to_be32(b_ss(1)), cpu_to_be64(0),
356 cpu_to_be64(phb
->io_win_addr
),
357 cpu_to_be64(memory_region_size(&phb
->iospace
)),
360 cpu_to_be32(b_ss(2)), cpu_to_be64(SPAPR_PCI_MEM_WIN_BUS_OFFSET
),
361 cpu_to_be64(phb
->mem_win_addr
),
362 cpu_to_be64(memory_region_size(&phb
->memwindow
)),
365 uint64_t bus_reg
[] = { cpu_to_be64(phb
->buid
), 0 };
366 uint32_t interrupt_map_mask
[] = {
367 cpu_to_be32(b_ddddd(-1)|b_fff(0)), 0x0, 0x0, 0x0};
368 uint32_t interrupt_map
[bus
->nirq
][7];
370 /* Start populating the FDT */
371 sprintf(nodename
, "pci@%" PRIx64
, phb
->buid
);
372 bus_off
= fdt_add_subnode(fdt
, 0, nodename
);
385 /* Write PHB properties */
386 _FDT(fdt_setprop_string(fdt
, bus_off
, "device_type", "pci"));
387 _FDT(fdt_setprop_string(fdt
, bus_off
, "compatible", "IBM,Logical_PHB"));
388 _FDT(fdt_setprop_cell(fdt
, bus_off
, "#address-cells", 0x3));
389 _FDT(fdt_setprop_cell(fdt
, bus_off
, "#size-cells", 0x2));
390 _FDT(fdt_setprop_cell(fdt
, bus_off
, "#interrupt-cells", 0x1));
391 _FDT(fdt_setprop(fdt
, bus_off
, "used-by-rtas", NULL
, 0));
392 _FDT(fdt_setprop(fdt
, bus_off
, "bus-range", &bus_range
, sizeof(bus_range
)));
393 _FDT(fdt_setprop(fdt
, bus_off
, "ranges", &ranges
, sizeof(ranges
)));
394 _FDT(fdt_setprop(fdt
, bus_off
, "reg", &bus_reg
, sizeof(bus_reg
)));
395 _FDT(fdt_setprop_cell(fdt
, bus_off
, "ibm,pci-config-space-type", 0x1));
397 /* Build the interrupt-map, this must matches what is done
398 * in pci_spapr_map_irq
400 _FDT(fdt_setprop(fdt
, bus_off
, "interrupt-map-mask",
401 &interrupt_map_mask
, sizeof(interrupt_map_mask
)));
402 for (i
= 0; i
< 7; i
++) {
403 uint32_t *irqmap
= interrupt_map
[i
];
404 irqmap
[0] = cpu_to_be32(b_ddddd(i
)|b_fff(0));
408 irqmap
[4] = cpu_to_be32(xics_phandle
);
409 irqmap
[5] = cpu_to_be32(phb
->lsi_table
[i
% SPAPR_PCI_NUM_LSI
].dt_irq
);
410 irqmap
[6] = cpu_to_be32(0x8);
412 /* Write interrupt map */
413 _FDT(fdt_setprop(fdt
, bus_off
, "interrupt-map", &interrupt_map
,
414 7 * sizeof(interrupt_map
[0])));