]> git.proxmox.com Git - mirror_qemu.git/blame - hw/ppc/spapr_pci.c
xics-kvm: Fix reset function
[mirror_qemu.git] / hw / ppc / spapr_pci.c
CommitLineData
3384f95c
DG
1/*
2 * QEMU sPAPR PCI host originated from Uninorth PCI host
3 *
4 * Copyright (c) 2011 Alexey Kardashevskiy, IBM Corporation.
5 * Copyright (C) 2011 David Gibson, IBM Corporation.
6 *
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:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
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
23 * THE SOFTWARE.
24 */
83c9f4ca
PB
25#include "hw/hw.h"
26#include "hw/pci/pci.h"
27#include "hw/pci/msi.h"
28#include "hw/pci/msix.h"
29#include "hw/pci/pci_host.h"
0d09e41a
PB
30#include "hw/ppc/spapr.h"
31#include "hw/pci-host/spapr.h"
022c62cb 32#include "exec/address-spaces.h"
3384f95c 33#include <libfdt.h>
a2950fb6 34#include "trace.h"
3384f95c 35
06aac7bd 36#include "hw/pci/pci_bus.h"
3384f95c 37
0ee2c058
AK
38/* Copied from the kernel arch/powerpc/platforms/pseries/msi.c */
39#define RTAS_QUERY_FN 0
40#define RTAS_CHANGE_FN 1
41#define RTAS_RESET_FN 2
42#define RTAS_CHANGE_MSI_FN 3
43#define RTAS_CHANGE_MSIX_FN 4
44
45/* Interrupt types to return on RTAS_CHANGE_* */
46#define RTAS_TYPE_MSI 1
47#define RTAS_TYPE_MSIX 2
48
9894c5d4 49static sPAPRPHBState *find_phb(sPAPREnvironment *spapr, uint64_t buid)
3384f95c 50{
8c9f64df 51 sPAPRPHBState *sphb;
3384f95c 52
8c9f64df
AF
53 QLIST_FOREACH(sphb, &spapr->phbs, list) {
54 if (sphb->buid != buid) {
3384f95c
DG
55 continue;
56 }
8c9f64df 57 return sphb;
9894c5d4
AK
58 }
59
60 return NULL;
61}
62
63static PCIDevice *find_dev(sPAPREnvironment *spapr, uint64_t buid,
64 uint32_t config_addr)
65{
8c9f64df 66 sPAPRPHBState *sphb = find_phb(spapr, buid);
8558d942 67 PCIHostState *phb = PCI_HOST_BRIDGE(sphb);
5dac82ce 68 int bus_num = (config_addr >> 16) & 0xFF;
9894c5d4
AK
69 int devfn = (config_addr >> 8) & 0xFF;
70
71 if (!phb) {
72 return NULL;
73 }
3384f95c 74
5dac82ce 75 return pci_find_device(phb->bus, bus_num, devfn);
3384f95c
DG
76}
77
3f7565c9
BH
78static uint32_t rtas_pci_cfgaddr(uint32_t arg)
79{
92615a5a 80 /* This handles the encoding of extended config space addresses */
3f7565c9
BH
81 return ((arg >> 20) & 0xf00) | (arg & 0xff);
82}
83
92615a5a
DG
84static void finish_read_pci_config(sPAPREnvironment *spapr, uint64_t buid,
85 uint32_t addr, uint32_t size,
86 target_ulong rets)
88045ac5 87{
92615a5a
DG
88 PCIDevice *pci_dev;
89 uint32_t val;
90
91 if ((size != 1) && (size != 2) && (size != 4)) {
92 /* access must be 1, 2 or 4 bytes */
a64d325d 93 rtas_st(rets, 0, RTAS_OUT_HW_ERROR);
92615a5a 94 return;
88045ac5 95 }
88045ac5 96
92615a5a
DG
97 pci_dev = find_dev(spapr, buid, addr);
98 addr = rtas_pci_cfgaddr(addr);
99
100 if (!pci_dev || (addr % size) || (addr >= pci_config_size(pci_dev))) {
101 /* Access must be to a valid device, within bounds and
102 * naturally aligned */
a64d325d 103 rtas_st(rets, 0, RTAS_OUT_HW_ERROR);
92615a5a 104 return;
88045ac5 105 }
92615a5a
DG
106
107 val = pci_host_config_read_common(pci_dev, addr,
108 pci_config_size(pci_dev), size);
109
a64d325d 110 rtas_st(rets, 0, RTAS_OUT_SUCCESS);
92615a5a 111 rtas_st(rets, 1, val);
88045ac5
AG
112}
113
210b580b 114static void rtas_ibm_read_pci_config(PowerPCCPU *cpu, sPAPREnvironment *spapr,
3384f95c
DG
115 uint32_t token, uint32_t nargs,
116 target_ulong args,
117 uint32_t nret, target_ulong rets)
118{
92615a5a
DG
119 uint64_t buid;
120 uint32_t size, addr;
3384f95c 121
92615a5a 122 if ((nargs != 4) || (nret != 2)) {
a64d325d 123 rtas_st(rets, 0, RTAS_OUT_HW_ERROR);
3384f95c
DG
124 return;
125 }
92615a5a
DG
126
127 buid = ((uint64_t)rtas_ld(args, 1) << 32) | rtas_ld(args, 2);
3384f95c 128 size = rtas_ld(args, 3);
92615a5a
DG
129 addr = rtas_ld(args, 0);
130
131 finish_read_pci_config(spapr, buid, addr, size, rets);
3384f95c
DG
132}
133
210b580b 134static void rtas_read_pci_config(PowerPCCPU *cpu, sPAPREnvironment *spapr,
3384f95c
DG
135 uint32_t token, uint32_t nargs,
136 target_ulong args,
137 uint32_t nret, target_ulong rets)
138{
92615a5a 139 uint32_t size, addr;
3384f95c 140
92615a5a 141 if ((nargs != 2) || (nret != 2)) {
a64d325d 142 rtas_st(rets, 0, RTAS_OUT_HW_ERROR);
3384f95c
DG
143 return;
144 }
92615a5a 145
3384f95c 146 size = rtas_ld(args, 1);
92615a5a
DG
147 addr = rtas_ld(args, 0);
148
149 finish_read_pci_config(spapr, 0, addr, size, rets);
150}
151
152static void finish_write_pci_config(sPAPREnvironment *spapr, uint64_t buid,
153 uint32_t addr, uint32_t size,
154 uint32_t val, target_ulong rets)
155{
156 PCIDevice *pci_dev;
157
158 if ((size != 1) && (size != 2) && (size != 4)) {
159 /* access must be 1, 2 or 4 bytes */
a64d325d 160 rtas_st(rets, 0, RTAS_OUT_HW_ERROR);
92615a5a
DG
161 return;
162 }
163
164 pci_dev = find_dev(spapr, buid, addr);
165 addr = rtas_pci_cfgaddr(addr);
166
167 if (!pci_dev || (addr % size) || (addr >= pci_config_size(pci_dev))) {
168 /* Access must be to a valid device, within bounds and
169 * naturally aligned */
a64d325d 170 rtas_st(rets, 0, RTAS_OUT_HW_ERROR);
92615a5a
DG
171 return;
172 }
173
174 pci_host_config_write_common(pci_dev, addr, pci_config_size(pci_dev),
175 val, size);
176
a64d325d 177 rtas_st(rets, 0, RTAS_OUT_SUCCESS);
3384f95c
DG
178}
179
210b580b 180static void rtas_ibm_write_pci_config(PowerPCCPU *cpu, sPAPREnvironment *spapr,
3384f95c
DG
181 uint32_t token, uint32_t nargs,
182 target_ulong args,
183 uint32_t nret, target_ulong rets)
184{
92615a5a 185 uint64_t buid;
3384f95c 186 uint32_t val, size, addr;
3384f95c 187
92615a5a 188 if ((nargs != 5) || (nret != 1)) {
a64d325d 189 rtas_st(rets, 0, RTAS_OUT_HW_ERROR);
3384f95c
DG
190 return;
191 }
92615a5a
DG
192
193 buid = ((uint64_t)rtas_ld(args, 1) << 32) | rtas_ld(args, 2);
3384f95c
DG
194 val = rtas_ld(args, 4);
195 size = rtas_ld(args, 3);
92615a5a
DG
196 addr = rtas_ld(args, 0);
197
198 finish_write_pci_config(spapr, buid, addr, size, val, rets);
3384f95c
DG
199}
200
210b580b 201static void rtas_write_pci_config(PowerPCCPU *cpu, sPAPREnvironment *spapr,
3384f95c
DG
202 uint32_t token, uint32_t nargs,
203 target_ulong args,
204 uint32_t nret, target_ulong rets)
205{
206 uint32_t val, size, addr;
3384f95c 207
92615a5a 208 if ((nargs != 3) || (nret != 1)) {
a64d325d 209 rtas_st(rets, 0, RTAS_OUT_HW_ERROR);
3384f95c
DG
210 return;
211 }
92615a5a
DG
212
213
3384f95c
DG
214 val = rtas_ld(args, 2);
215 size = rtas_ld(args, 1);
92615a5a
DG
216 addr = rtas_ld(args, 0);
217
218 finish_write_pci_config(spapr, 0, addr, size, val, rets);
3384f95c
DG
219}
220
0ee2c058
AK
221/*
222 * Find an entry with config_addr or returns the empty one if not found AND
223 * alloc_new is set.
224 * At the moment the msi_table entries are never released so there is
225 * no point to look till the end of the list if we need to find the free entry.
226 */
227static int spapr_msicfg_find(sPAPRPHBState *phb, uint32_t config_addr,
228 bool alloc_new)
229{
230 int i;
231
232 for (i = 0; i < SPAPR_MSIX_MAX_DEVS; ++i) {
233 if (!phb->msi_table[i].nvec) {
234 break;
235 }
236 if (phb->msi_table[i].config_addr == config_addr) {
237 return i;
238 }
239 }
240 if ((i < SPAPR_MSIX_MAX_DEVS) && alloc_new) {
241 trace_spapr_pci_msi("Allocating new MSI config", i, config_addr);
242 return i;
243 }
244
245 return -1;
246}
247
248/*
249 * Set MSI/MSIX message data.
250 * This is required for msi_notify()/msix_notify() which
251 * will write at the addresses via spapr_msi_write().
252 */
f1c2dc7c
AK
253static void spapr_msi_setmsg(PCIDevice *pdev, hwaddr addr, bool msix,
254 unsigned first_irq, unsigned req_num)
0ee2c058
AK
255{
256 unsigned i;
f1c2dc7c 257 MSIMessage msg = { .address = addr, .data = first_irq };
0ee2c058
AK
258
259 if (!msix) {
260 msi_set_message(pdev, msg);
261 trace_spapr_pci_msi_setup(pdev->name, 0, msg.address);
262 return;
263 }
264
f1c2dc7c 265 for (i = 0; i < req_num; ++i, ++msg.data) {
0ee2c058
AK
266 msix_set_message(pdev, i, msg);
267 trace_spapr_pci_msi_setup(pdev->name, i, msg.address);
268 }
269}
270
210b580b 271static void rtas_ibm_change_msi(PowerPCCPU *cpu, sPAPREnvironment *spapr,
0ee2c058
AK
272 uint32_t token, uint32_t nargs,
273 target_ulong args, uint32_t nret,
274 target_ulong rets)
275{
276 uint32_t config_addr = rtas_ld(args, 0);
277 uint64_t buid = ((uint64_t)rtas_ld(args, 1) << 32) | rtas_ld(args, 2);
278 unsigned int func = rtas_ld(args, 3);
279 unsigned int req_num = rtas_ld(args, 4); /* 0 == remove all */
280 unsigned int seq_num = rtas_ld(args, 5);
281 unsigned int ret_intr_type;
282 int ndev, irq;
283 sPAPRPHBState *phb = NULL;
284 PCIDevice *pdev = NULL;
285
286 switch (func) {
287 case RTAS_CHANGE_MSI_FN:
288 case RTAS_CHANGE_FN:
289 ret_intr_type = RTAS_TYPE_MSI;
290 break;
291 case RTAS_CHANGE_MSIX_FN:
292 ret_intr_type = RTAS_TYPE_MSIX;
293 break;
294 default:
295 fprintf(stderr, "rtas_ibm_change_msi(%u) is not implemented\n", func);
a64d325d 296 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
0ee2c058
AK
297 return;
298 }
299
300 /* Fins sPAPRPHBState */
301 phb = find_phb(spapr, buid);
302 if (phb) {
303 pdev = find_dev(spapr, buid, config_addr);
304 }
305 if (!phb || !pdev) {
a64d325d 306 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
0ee2c058
AK
307 return;
308 }
309
310 /* Releasing MSIs */
311 if (!req_num) {
312 ndev = spapr_msicfg_find(phb, config_addr, false);
313 if (ndev < 0) {
314 trace_spapr_pci_msi("MSI has not been enabled", -1, config_addr);
a64d325d 315 rtas_st(rets, 0, RTAS_OUT_HW_ERROR);
0ee2c058
AK
316 return;
317 }
318 trace_spapr_pci_msi("Released MSIs", ndev, config_addr);
a64d325d 319 rtas_st(rets, 0, RTAS_OUT_SUCCESS);
0ee2c058
AK
320 rtas_st(rets, 1, 0);
321 return;
322 }
323
324 /* Enabling MSI */
325
326 /* Find a device number in the map to add or reuse the existing one */
327 ndev = spapr_msicfg_find(phb, config_addr, true);
328 if (ndev >= SPAPR_MSIX_MAX_DEVS || ndev < 0) {
329 fprintf(stderr, "No free entry for a new MSI device\n");
a64d325d 330 rtas_st(rets, 0, RTAS_OUT_HW_ERROR);
0ee2c058
AK
331 return;
332 }
333 trace_spapr_pci_msi("Configuring MSI", ndev, config_addr);
334
335 /* Check if there is an old config and MSI number has not changed */
336 if (phb->msi_table[ndev].nvec && (req_num != phb->msi_table[ndev].nvec)) {
337 /* Unexpected behaviour */
338 fprintf(stderr, "Cannot reuse MSI config for device#%d", ndev);
a64d325d 339 rtas_st(rets, 0, RTAS_OUT_HW_ERROR);
0ee2c058
AK
340 return;
341 }
342
343 /* There is no cached config, allocate MSIs */
344 if (!phb->msi_table[ndev].nvec) {
f1c2dc7c
AK
345 irq = spapr_allocate_irq_block(req_num, false,
346 ret_intr_type == RTAS_TYPE_MSI);
0ee2c058
AK
347 if (irq < 0) {
348 fprintf(stderr, "Cannot allocate MSIs for device#%d", ndev);
a64d325d 349 rtas_st(rets, 0, RTAS_OUT_HW_ERROR);
0ee2c058
AK
350 return;
351 }
352 phb->msi_table[ndev].irq = irq;
353 phb->msi_table[ndev].nvec = req_num;
354 phb->msi_table[ndev].config_addr = config_addr;
355 }
356
357 /* Setup MSI/MSIX vectors in the device (via cfgspace or MSIX BAR) */
f1c2dc7c
AK
358 spapr_msi_setmsg(pdev, spapr->msi_win_addr, ret_intr_type == RTAS_TYPE_MSIX,
359 phb->msi_table[ndev].irq, req_num);
0ee2c058 360
a64d325d 361 rtas_st(rets, 0, RTAS_OUT_SUCCESS);
0ee2c058
AK
362 rtas_st(rets, 1, req_num);
363 rtas_st(rets, 2, ++seq_num);
364 rtas_st(rets, 3, ret_intr_type);
365
366 trace_spapr_pci_rtas_ibm_change_msi(func, req_num);
367}
368
210b580b
AL
369static void rtas_ibm_query_interrupt_source_number(PowerPCCPU *cpu,
370 sPAPREnvironment *spapr,
0ee2c058
AK
371 uint32_t token,
372 uint32_t nargs,
373 target_ulong args,
374 uint32_t nret,
375 target_ulong rets)
376{
377 uint32_t config_addr = rtas_ld(args, 0);
378 uint64_t buid = ((uint64_t)rtas_ld(args, 1) << 32) | rtas_ld(args, 2);
379 unsigned int intr_src_num = -1, ioa_intr_num = rtas_ld(args, 3);
380 int ndev;
381 sPAPRPHBState *phb = NULL;
382
383 /* Fins sPAPRPHBState */
384 phb = find_phb(spapr, buid);
385 if (!phb) {
a64d325d 386 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
0ee2c058
AK
387 return;
388 }
389
390 /* Find device descriptor and start IRQ */
391 ndev = spapr_msicfg_find(phb, config_addr, false);
392 if (ndev < 0) {
393 trace_spapr_pci_msi("MSI has not been enabled", -1, config_addr);
a64d325d 394 rtas_st(rets, 0, RTAS_OUT_HW_ERROR);
0ee2c058
AK
395 return;
396 }
397
398 intr_src_num = phb->msi_table[ndev].irq + ioa_intr_num;
399 trace_spapr_pci_rtas_ibm_query_interrupt_source_number(ioa_intr_num,
400 intr_src_num);
401
a64d325d 402 rtas_st(rets, 0, RTAS_OUT_SUCCESS);
0ee2c058
AK
403 rtas_st(rets, 1, intr_src_num);
404 rtas_st(rets, 2, 1);/* 0 == level; 1 == edge */
405}
406
7fb0bd34
DG
407static int pci_spapr_swizzle(int slot, int pin)
408{
409 return (slot + pin) % PCI_NUM_PINS;
410}
411
3384f95c
DG
412static int pci_spapr_map_irq(PCIDevice *pci_dev, int irq_num)
413{
414 /*
415 * Here we need to convert pci_dev + irq_num to some unique value
7fb0bd34
DG
416 * which is less than number of IRQs on the specific bus (4). We
417 * use standard PCI swizzling, that is (slot number + pin number)
418 * % 4.
3384f95c 419 */
7fb0bd34 420 return pci_spapr_swizzle(PCI_SLOT(pci_dev->devfn), irq_num);
3384f95c
DG
421}
422
423static void pci_spapr_set_irq(void *opaque, int irq_num, int level)
424{
425 /*
426 * Here we use the number returned by pci_spapr_map_irq to find a
427 * corresponding qemu_irq.
428 */
429 sPAPRPHBState *phb = opaque;
430
caae58cb 431 trace_spapr_pci_lsi_set(phb->dtbusname, irq_num, phb->lsi_table[irq_num].irq);
a307d594 432 qemu_set_irq(spapr_phb_lsi_qirq(phb, irq_num), level);
3384f95c
DG
433}
434
5cc7a967
AK
435static PCIINTxRoute spapr_route_intx_pin_to_irq(void *opaque, int pin)
436{
437 sPAPRPHBState *sphb = SPAPR_PCI_HOST_BRIDGE(opaque);
438 PCIINTxRoute route;
439
440 route.mode = PCI_INTX_ENABLED;
441 route.irq = sphb->lsi_table[pin].irq;
442
443 return route;
444}
445
0ee2c058
AK
446/*
447 * MSI/MSIX memory region implementation.
448 * The handler handles both MSI and MSIX.
449 * For MSI-X, the vector number is encoded as a part of the address,
450 * data is set to 0.
451 * For MSI, the vector number is encoded in least bits in data.
452 */
a8170e5e 453static void spapr_msi_write(void *opaque, hwaddr addr,
0ee2c058
AK
454 uint64_t data, unsigned size)
455{
f1c2dc7c 456 uint32_t irq = data;
0ee2c058
AK
457
458 trace_spapr_pci_msi_write(addr, data, irq);
459
460 qemu_irq_pulse(xics_get_qirq(spapr->icp, irq));
461}
462
463static const MemoryRegionOps spapr_msi_ops = {
464 /* There is no .read as the read result is undefined by PCI spec */
465 .read = NULL,
466 .write = spapr_msi_write,
467 .endianness = DEVICE_LITTLE_ENDIAN
468};
469
f1c2dc7c
AK
470void spapr_pci_msi_init(sPAPREnvironment *spapr, hwaddr addr)
471{
3c3b0dde
AG
472 uint64_t window_size = 4096;
473
f1c2dc7c
AK
474 /*
475 * As MSI/MSIX interrupts trigger by writing at MSI/MSIX vectors,
476 * we need to allocate some memory to catch those writes coming
477 * from msi_notify()/msix_notify().
478 * As MSIMessage:addr is going to be the same and MSIMessage:data
479 * is going to be a VIRQ number, 4 bytes of the MSI MR will only
480 * be used.
3c3b0dde
AG
481 *
482 * For KVM we want to ensure that this memory is a full page so that
483 * our memory slot is of page size granularity.
f1c2dc7c 484 */
3c3b0dde
AG
485#ifdef CONFIG_KVM
486 if (kvm_enabled()) {
487 window_size = getpagesize();
488 }
489#endif
490
f1c2dc7c
AK
491 spapr->msi_win_addr = addr;
492 memory_region_init_io(&spapr->msiwindow, NULL, &spapr_msi_ops, spapr,
3c3b0dde 493 "msi", window_size);
f1c2dc7c
AK
494 memory_region_add_subregion(get_system_memory(), spapr->msi_win_addr,
495 &spapr->msiwindow);
496}
497
298a9710
DG
498/*
499 * PHB PCI device
500 */
e00387d5 501static AddressSpace *spapr_pci_dma_iommu(PCIBus *bus, void *opaque, int devfn)
edded454
DG
502{
503 sPAPRPHBState *phb = opaque;
504
e00387d5 505 return &phb->iommu_as;
edded454
DG
506}
507
298a9710 508static int spapr_phb_init(SysBusDevice *s)
3384f95c 509{
38fb090a 510 DeviceState *dev = DEVICE(s);
8c9f64df 511 sPAPRPHBState *sphb = SPAPR_PCI_HOST_BRIDGE(s);
8558d942 512 PCIHostState *phb = PCI_HOST_BRIDGE(s);
89dfd6e1 513 const char *busname;
298a9710
DG
514 char *namebuf;
515 int i;
3384f95c 516 PCIBus *bus;
3384f95c 517
caae58cb
DG
518 if (sphb->index != -1) {
519 hwaddr windows_base;
520
521 if ((sphb->buid != -1) || (sphb->dma_liobn != -1)
522 || (sphb->mem_win_addr != -1)
f1c2dc7c 523 || (sphb->io_win_addr != -1)) {
caae58cb
DG
524 fprintf(stderr, "Either \"index\" or other parameters must"
525 " be specified for PAPR PHB, not both\n");
526 return -1;
527 }
528
529 sphb->buid = SPAPR_PCI_BASE_BUID + sphb->index;
530 sphb->dma_liobn = SPAPR_PCI_BASE_LIOBN + sphb->index;
531
532 windows_base = SPAPR_PCI_WINDOW_BASE
533 + sphb->index * SPAPR_PCI_WINDOW_SPACING;
534 sphb->mem_win_addr = windows_base + SPAPR_PCI_MMIO_WIN_OFF;
535 sphb->io_win_addr = windows_base + SPAPR_PCI_IO_WIN_OFF;
caae58cb
DG
536 }
537
538 if (sphb->buid == -1) {
539 fprintf(stderr, "BUID not specified for PHB\n");
540 return -1;
541 }
542
543 if (sphb->dma_liobn == -1) {
544 fprintf(stderr, "LIOBN not specified for PHB\n");
545 return -1;
546 }
547
548 if (sphb->mem_win_addr == -1) {
549 fprintf(stderr, "Memory window address not specified for PHB\n");
550 return -1;
551 }
552
553 if (sphb->io_win_addr == -1) {
554 fprintf(stderr, "IO window address not specified for PHB\n");
555 return -1;
556 }
557
caae58cb
DG
558 if (find_phb(spapr, sphb->buid)) {
559 fprintf(stderr, "PCI host bridges must have unique BUIDs\n");
560 return -1;
561 }
562
8c9f64df 563 sphb->dtbusname = g_strdup_printf("pci@%" PRIx64, sphb->buid);
caae58cb 564
8c9f64df 565 namebuf = alloca(strlen(sphb->dtbusname) + 32);
3384f95c 566
298a9710 567 /* Initialize memory regions */
8c9f64df 568 sprintf(namebuf, "%s.mmio", sphb->dtbusname);
92b8e39c 569 memory_region_init(&sphb->memspace, OBJECT(sphb), namebuf, UINT64_MAX);
3384f95c 570
8c9f64df 571 sprintf(namebuf, "%s.mmio-alias", sphb->dtbusname);
40c5dce9
PB
572 memory_region_init_alias(&sphb->memwindow, OBJECT(sphb),
573 namebuf, &sphb->memspace,
8c9f64df
AF
574 SPAPR_PCI_MEM_WIN_BUS_OFFSET, sphb->mem_win_size);
575 memory_region_add_subregion(get_system_memory(), sphb->mem_win_addr,
576 &sphb->memwindow);
3384f95c 577
3384f95c
DG
578 /* On ppc, we only have MMIO no specific IO space from the CPU
579 * perspective. In theory we ought to be able to embed the PCI IO
580 * memory region direction in the system memory space. However,
581 * if any of the IO BAR subregions use the old_portio mechanism,
582 * that won't be processed properly unless accessed from the
583 * system io address space. This hack to bounce things via
584 * system_io works around the problem until all the users of
585 * old_portion are updated */
8c9f64df 586 sprintf(namebuf, "%s.io", sphb->dtbusname);
40c5dce9
PB
587 memory_region_init(&sphb->iospace, OBJECT(sphb),
588 namebuf, SPAPR_PCI_IO_WIN_SIZE);
a3cfa18e
DG
589 /* FIXME: fix to support multiple PHBs */
590 memory_region_add_subregion(get_system_io(), 0, &sphb->iospace);
3384f95c 591
a3cfa18e 592 sprintf(namebuf, "%s.io-alias", sphb->dtbusname);
66aab867
AK
593 memory_region_init_alias(&sphb->iowindow, OBJECT(sphb), namebuf,
594 get_system_io(), 0, SPAPR_PCI_IO_WIN_SIZE);
8c9f64df 595 memory_region_add_subregion(get_system_memory(), sphb->io_win_addr,
a3cfa18e 596 &sphb->iowindow);
89dfd6e1
DG
597 /*
598 * Selecting a busname is more complex than you'd think, due to
599 * interacting constraints. If the user has specified an id
600 * explicitly for the phb , then we want to use the qdev default
601 * of naming the bus based on the bridge device (so the user can
602 * then assign devices to it in the way they expect). For the
603 * first / default PCI bus (index=0) we want to use just "pci"
604 * because libvirt expects there to be a bus called, simply,
605 * "pci". Otherwise, we use the same name as in the device tree,
606 * since it's unique by construction, and makes the guest visible
607 * BUID clear.
608 */
38fb090a 609 if (dev->id) {
89dfd6e1
DG
610 busname = NULL;
611 } else if (sphb->index == 0) {
612 busname = "pci";
613 } else {
614 busname = sphb->dtbusname;
615 }
38fb090a 616 bus = pci_register_bus(dev, busname,
8c9f64df
AF
617 pci_spapr_set_irq, pci_spapr_map_irq, sphb,
618 &sphb->memspace, &sphb->iospace,
60a0e443 619 PCI_DEVFN(0, 0), PCI_NUM_PINS, TYPE_PCI_BUS);
8c9f64df 620 phb->bus = bus;
298a9710 621
8c9f64df
AF
622 sphb->dma_window_start = 0;
623 sphb->dma_window_size = 0x40000000;
38fb090a 624 sphb->tcet = spapr_tce_new_table(dev, sphb->dma_liobn,
84af6d9f 625 sphb->dma_window_size);
2b7dc949 626 if (!sphb->tcet) {
caae58cb
DG
627 fprintf(stderr, "Unable to create TCE table for %s\n", sphb->dtbusname);
628 return -1;
629 }
7dca8043
AK
630 address_space_init(&sphb->iommu_as, spapr_tce_get_iommu(sphb->tcet),
631 sphb->dtbusname);
632
e00387d5 633 pci_setup_iommu(bus, spapr_pci_dma_iommu, sphb);
edded454 634
5cc7a967
AK
635 pci_bus_set_route_irq_fn(bus, spapr_route_intx_pin_to_irq);
636
8c9f64df 637 QLIST_INSERT_HEAD(&spapr->phbs, sphb, list);
298a9710
DG
638
639 /* Initialize the LSI table */
7fb0bd34 640 for (i = 0; i < PCI_NUM_PINS; i++) {
a307d594 641 uint32_t irq;
298a9710 642
a307d594
AK
643 irq = spapr_allocate_lsi(0);
644 if (!irq) {
298a9710
DG
645 return -1;
646 }
647
8c9f64df 648 sphb->lsi_table[i].irq = irq;
298a9710
DG
649 }
650
651 return 0;
652}
653
eddeed26
DG
654static void spapr_phb_reset(DeviceState *qdev)
655{
1356b98d 656 SysBusDevice *s = SYS_BUS_DEVICE(qdev);
eddeed26
DG
657 sPAPRPHBState *sphb = SPAPR_PCI_HOST_BRIDGE(s);
658
659 /* Reset the IOMMU state */
a83000f5 660 device_reset(DEVICE(sphb->tcet));
eddeed26
DG
661}
662
298a9710 663static Property spapr_phb_properties[] = {
caae58cb 664 DEFINE_PROP_INT32("index", sPAPRPHBState, index, -1),
c7bcc85d
PB
665 DEFINE_PROP_UINT64("buid", sPAPRPHBState, buid, -1),
666 DEFINE_PROP_UINT32("liobn", sPAPRPHBState, dma_liobn, -1),
667 DEFINE_PROP_UINT64("mem_win_addr", sPAPRPHBState, mem_win_addr, -1),
668 DEFINE_PROP_UINT64("mem_win_size", sPAPRPHBState, mem_win_size,
669 SPAPR_PCI_MMIO_WIN_SIZE),
670 DEFINE_PROP_UINT64("io_win_addr", sPAPRPHBState, io_win_addr, -1),
671 DEFINE_PROP_UINT64("io_win_size", sPAPRPHBState, io_win_size,
672 SPAPR_PCI_IO_WIN_SIZE),
298a9710
DG
673 DEFINE_PROP_END_OF_LIST(),
674};
675
1112cf94
DG
676static const VMStateDescription vmstate_spapr_pci_lsi = {
677 .name = "spapr_pci/lsi",
678 .version_id = 1,
679 .minimum_version_id = 1,
680 .minimum_version_id_old = 1,
681 .fields = (VMStateField []) {
682 VMSTATE_UINT32_EQUAL(irq, struct spapr_pci_lsi),
683
684 VMSTATE_END_OF_LIST()
685 },
686};
687
688static const VMStateDescription vmstate_spapr_pci_msi = {
689 .name = "spapr_pci/lsi",
690 .version_id = 1,
691 .minimum_version_id = 1,
692 .minimum_version_id_old = 1,
693 .fields = (VMStateField []) {
694 VMSTATE_UINT32(config_addr, struct spapr_pci_msi),
695 VMSTATE_UINT32(irq, struct spapr_pci_msi),
696 VMSTATE_UINT32(nvec, struct spapr_pci_msi),
697
698 VMSTATE_END_OF_LIST()
699 },
700};
701
702static const VMStateDescription vmstate_spapr_pci = {
703 .name = "spapr_pci",
704 .version_id = 1,
705 .minimum_version_id = 1,
706 .minimum_version_id_old = 1,
707 .fields = (VMStateField []) {
708 VMSTATE_UINT64_EQUAL(buid, sPAPRPHBState),
709 VMSTATE_UINT32_EQUAL(dma_liobn, sPAPRPHBState),
710 VMSTATE_UINT64_EQUAL(mem_win_addr, sPAPRPHBState),
711 VMSTATE_UINT64_EQUAL(mem_win_size, sPAPRPHBState),
712 VMSTATE_UINT64_EQUAL(io_win_addr, sPAPRPHBState),
713 VMSTATE_UINT64_EQUAL(io_win_size, sPAPRPHBState),
1112cf94
DG
714 VMSTATE_STRUCT_ARRAY(lsi_table, sPAPRPHBState, PCI_NUM_PINS, 0,
715 vmstate_spapr_pci_lsi, struct spapr_pci_lsi),
716 VMSTATE_STRUCT_ARRAY(msi_table, sPAPRPHBState, SPAPR_MSIX_MAX_DEVS, 0,
717 vmstate_spapr_pci_msi, struct spapr_pci_msi),
718
719 VMSTATE_END_OF_LIST()
720 },
721};
722
568f0690
DG
723static const char *spapr_phb_root_bus_path(PCIHostState *host_bridge,
724 PCIBus *rootbus)
725{
726 sPAPRPHBState *sphb = SPAPR_PCI_HOST_BRIDGE(host_bridge);
727
728 return sphb->dtbusname;
729}
730
298a9710
DG
731static void spapr_phb_class_init(ObjectClass *klass, void *data)
732{
568f0690 733 PCIHostBridgeClass *hc = PCI_HOST_BRIDGE_CLASS(klass);
298a9710
DG
734 SysBusDeviceClass *sdc = SYS_BUS_DEVICE_CLASS(klass);
735 DeviceClass *dc = DEVICE_CLASS(klass);
736
568f0690 737 hc->root_bus_path = spapr_phb_root_bus_path;
298a9710
DG
738 sdc->init = spapr_phb_init;
739 dc->props = spapr_phb_properties;
eddeed26 740 dc->reset = spapr_phb_reset;
1112cf94 741 dc->vmsd = &vmstate_spapr_pci;
09aa9a52
AK
742 set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories);
743 dc->cannot_instantiate_with_device_add_yet = false;
298a9710 744}
3384f95c 745
4240abff 746static const TypeInfo spapr_phb_info = {
8c9f64df 747 .name = TYPE_SPAPR_PCI_HOST_BRIDGE,
8558d942 748 .parent = TYPE_PCI_HOST_BRIDGE,
298a9710
DG
749 .instance_size = sizeof(sPAPRPHBState),
750 .class_init = spapr_phb_class_init,
751};
752
89dfd6e1 753PCIHostState *spapr_create_phb(sPAPREnvironment *spapr, int index)
298a9710
DG
754{
755 DeviceState *dev;
756
8c9f64df 757 dev = qdev_create(NULL, TYPE_SPAPR_PCI_HOST_BRIDGE);
caae58cb 758 qdev_prop_set_uint32(dev, "index", index);
298a9710 759 qdev_init_nofail(dev);
caae58cb
DG
760
761 return PCI_HOST_BRIDGE(dev);
3384f95c
DG
762}
763
764/* Macros to operate with address in OF binding to PCI */
765#define b_x(x, p, l) (((x) & ((1<<(l))-1)) << (p))
766#define b_n(x) b_x((x), 31, 1) /* 0 if relocatable */
767#define b_p(x) b_x((x), 30, 1) /* 1 if prefetchable */
768#define b_t(x) b_x((x), 29, 1) /* 1 if the address is aliased */
769#define b_ss(x) b_x((x), 24, 2) /* the space code */
770#define b_bbbbbbbb(x) b_x((x), 16, 8) /* bus number */
771#define b_ddddd(x) b_x((x), 11, 5) /* device number */
772#define b_fff(x) b_x((x), 8, 3) /* function number */
773#define b_rrrrrrrr(x) b_x((x), 0, 8) /* register number */
774
e0fdbd7c
AK
775int spapr_populate_pci_dt(sPAPRPHBState *phb,
776 uint32_t xics_phandle,
777 void *fdt)
3384f95c 778{
7fb0bd34 779 int bus_off, i, j;
3384f95c 780 char nodename[256];
3384f95c
DG
781 uint32_t bus_range[] = { cpu_to_be32(0), cpu_to_be32(0xff) };
782 struct {
783 uint32_t hi;
784 uint64_t child;
785 uint64_t parent;
786 uint64_t size;
c4889f54 787 } QEMU_PACKED ranges[] = {
3384f95c
DG
788 {
789 cpu_to_be32(b_ss(1)), cpu_to_be64(0),
790 cpu_to_be64(phb->io_win_addr),
791 cpu_to_be64(memory_region_size(&phb->iospace)),
792 },
793 {
794 cpu_to_be32(b_ss(2)), cpu_to_be64(SPAPR_PCI_MEM_WIN_BUS_OFFSET),
795 cpu_to_be64(phb->mem_win_addr),
796 cpu_to_be64(memory_region_size(&phb->memwindow)),
797 },
798 };
799 uint64_t bus_reg[] = { cpu_to_be64(phb->buid), 0 };
800 uint32_t interrupt_map_mask[] = {
7fb0bd34
DG
801 cpu_to_be32(b_ddddd(-1)|b_fff(0)), 0x0, 0x0, cpu_to_be32(-1)};
802 uint32_t interrupt_map[PCI_SLOT_MAX * PCI_NUM_PINS][7];
3384f95c
DG
803
804 /* Start populating the FDT */
805 sprintf(nodename, "pci@%" PRIx64, phb->buid);
806 bus_off = fdt_add_subnode(fdt, 0, nodename);
807 if (bus_off < 0) {
808 return bus_off;
809 }
810
811#define _FDT(exp) \
812 do { \
813 int ret = (exp); \
814 if (ret < 0) { \
815 return ret; \
816 } \
817 } while (0)
818
819 /* Write PHB properties */
820 _FDT(fdt_setprop_string(fdt, bus_off, "device_type", "pci"));
821 _FDT(fdt_setprop_string(fdt, bus_off, "compatible", "IBM,Logical_PHB"));
822 _FDT(fdt_setprop_cell(fdt, bus_off, "#address-cells", 0x3));
823 _FDT(fdt_setprop_cell(fdt, bus_off, "#size-cells", 0x2));
824 _FDT(fdt_setprop_cell(fdt, bus_off, "#interrupt-cells", 0x1));
825 _FDT(fdt_setprop(fdt, bus_off, "used-by-rtas", NULL, 0));
826 _FDT(fdt_setprop(fdt, bus_off, "bus-range", &bus_range, sizeof(bus_range)));
827 _FDT(fdt_setprop(fdt, bus_off, "ranges", &ranges, sizeof(ranges)));
828 _FDT(fdt_setprop(fdt, bus_off, "reg", &bus_reg, sizeof(bus_reg)));
3f7565c9 829 _FDT(fdt_setprop_cell(fdt, bus_off, "ibm,pci-config-space-type", 0x1));
3384f95c 830
4d8d5467
BH
831 /* Build the interrupt-map, this must matches what is done
832 * in pci_spapr_map_irq
833 */
834 _FDT(fdt_setprop(fdt, bus_off, "interrupt-map-mask",
835 &interrupt_map_mask, sizeof(interrupt_map_mask)));
7fb0bd34
DG
836 for (i = 0; i < PCI_SLOT_MAX; i++) {
837 for (j = 0; j < PCI_NUM_PINS; j++) {
838 uint32_t *irqmap = interrupt_map[i*PCI_NUM_PINS + j];
839 int lsi_num = pci_spapr_swizzle(i, j);
840
841 irqmap[0] = cpu_to_be32(b_ddddd(i)|b_fff(0));
842 irqmap[1] = 0;
843 irqmap[2] = 0;
844 irqmap[3] = cpu_to_be32(j+1);
845 irqmap[4] = cpu_to_be32(xics_phandle);
a307d594 846 irqmap[5] = cpu_to_be32(phb->lsi_table[lsi_num].irq);
7fb0bd34
DG
847 irqmap[6] = cpu_to_be32(0x8);
848 }
3384f95c 849 }
3384f95c
DG
850 /* Write interrupt map */
851 _FDT(fdt_setprop(fdt, bus_off, "interrupt-map", &interrupt_map,
7fb0bd34 852 sizeof(interrupt_map)));
3384f95c 853
5c4cbcf2
AK
854 spapr_dma_dt(fdt, bus_off, "ibm,dma-window",
855 phb->dma_liobn, phb->dma_window_start,
856 phb->dma_window_size);
edded454 857
3384f95c
DG
858 return 0;
859}
298a9710 860
fa28f71b
AK
861void spapr_pci_rtas_init(void)
862{
863 spapr_rtas_register("read-pci-config", rtas_read_pci_config);
864 spapr_rtas_register("write-pci-config", rtas_write_pci_config);
865 spapr_rtas_register("ibm,read-pci-config", rtas_ibm_read_pci_config);
866 spapr_rtas_register("ibm,write-pci-config", rtas_ibm_write_pci_config);
0ee2c058
AK
867 if (msi_supported) {
868 spapr_rtas_register("ibm,query-interrupt-source-number",
869 rtas_ibm_query_interrupt_source_number);
870 spapr_rtas_register("ibm,change-msi", rtas_ibm_change_msi);
871 }
fa28f71b
AK
872}
873
8c9f64df 874static void spapr_pci_register_types(void)
298a9710
DG
875{
876 type_register_static(&spapr_phb_info);
877}
8c9f64df
AF
878
879type_init(spapr_pci_register_types)