]> git.proxmox.com Git - mirror_qemu.git/blame - hw/ppc/spapr_pci.c
spapr_pci: (Mostly) remove spapr-pci-vfio-host-bridge
[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 */
0d75590d 25#include "qemu/osdep.h"
83c9f4ca 26#include "hw/hw.h"
1d2d9742 27#include "hw/sysbus.h"
83c9f4ca
PB
28#include "hw/pci/pci.h"
29#include "hw/pci/msi.h"
30#include "hw/pci/msix.h"
31#include "hw/pci/pci_host.h"
0d09e41a
PB
32#include "hw/ppc/spapr.h"
33#include "hw/pci-host/spapr.h"
022c62cb 34#include "exec/address-spaces.h"
3384f95c 35#include <libfdt.h>
a2950fb6 36#include "trace.h"
295d51aa 37#include "qemu/error-report.h"
7454c7af 38#include "qapi/qmp/qerror.h"
3384f95c 39
1d2d9742 40#include "hw/pci/pci_bridge.h"
06aac7bd 41#include "hw/pci/pci_bus.h"
62083979 42#include "hw/ppc/spapr_drc.h"
7454c7af 43#include "sysemu/device_tree.h"
3384f95c 44
c1fa017c
DG
45#include "hw/vfio/vfio.h"
46
0ee2c058
AK
47/* Copied from the kernel arch/powerpc/platforms/pseries/msi.c */
48#define RTAS_QUERY_FN 0
49#define RTAS_CHANGE_FN 1
50#define RTAS_RESET_FN 2
51#define RTAS_CHANGE_MSI_FN 3
52#define RTAS_CHANGE_MSIX_FN 4
53
54/* Interrupt types to return on RTAS_CHANGE_* */
55#define RTAS_TYPE_MSI 1
56#define RTAS_TYPE_MSIX 2
57
9b7d9284
ND
58#define FDT_NAME_MAX 128
59
7454c7af
MR
60#define _FDT(exp) \
61 do { \
62 int ret = (exp); \
63 if (ret < 0) { \
64 return ret; \
65 } \
66 } while (0)
67
28e02042 68sPAPRPHBState *spapr_pci_find_phb(sPAPRMachineState *spapr, uint64_t buid)
3384f95c 69{
8c9f64df 70 sPAPRPHBState *sphb;
3384f95c 71
8c9f64df
AF
72 QLIST_FOREACH(sphb, &spapr->phbs, list) {
73 if (sphb->buid != buid) {
3384f95c
DG
74 continue;
75 }
8c9f64df 76 return sphb;
9894c5d4
AK
77 }
78
79 return NULL;
80}
81
28e02042 82PCIDevice *spapr_pci_find_dev(sPAPRMachineState *spapr, uint64_t buid,
46c5874e 83 uint32_t config_addr)
9894c5d4 84{
46c5874e 85 sPAPRPHBState *sphb = spapr_pci_find_phb(spapr, buid);
8558d942 86 PCIHostState *phb = PCI_HOST_BRIDGE(sphb);
5dac82ce 87 int bus_num = (config_addr >> 16) & 0xFF;
9894c5d4
AK
88 int devfn = (config_addr >> 8) & 0xFF;
89
90 if (!phb) {
91 return NULL;
92 }
3384f95c 93
5dac82ce 94 return pci_find_device(phb->bus, bus_num, devfn);
3384f95c
DG
95}
96
3f7565c9
BH
97static uint32_t rtas_pci_cfgaddr(uint32_t arg)
98{
92615a5a 99 /* This handles the encoding of extended config space addresses */
3f7565c9
BH
100 return ((arg >> 20) & 0xf00) | (arg & 0xff);
101}
102
28e02042 103static void finish_read_pci_config(sPAPRMachineState *spapr, uint64_t buid,
92615a5a
DG
104 uint32_t addr, uint32_t size,
105 target_ulong rets)
88045ac5 106{
92615a5a
DG
107 PCIDevice *pci_dev;
108 uint32_t val;
109
110 if ((size != 1) && (size != 2) && (size != 4)) {
111 /* access must be 1, 2 or 4 bytes */
a64d325d 112 rtas_st(rets, 0, RTAS_OUT_HW_ERROR);
92615a5a 113 return;
88045ac5 114 }
88045ac5 115
46c5874e 116 pci_dev = spapr_pci_find_dev(spapr, buid, addr);
92615a5a
DG
117 addr = rtas_pci_cfgaddr(addr);
118
119 if (!pci_dev || (addr % size) || (addr >= pci_config_size(pci_dev))) {
120 /* Access must be to a valid device, within bounds and
121 * naturally aligned */
a64d325d 122 rtas_st(rets, 0, RTAS_OUT_HW_ERROR);
92615a5a 123 return;
88045ac5 124 }
92615a5a
DG
125
126 val = pci_host_config_read_common(pci_dev, addr,
127 pci_config_size(pci_dev), size);
128
a64d325d 129 rtas_st(rets, 0, RTAS_OUT_SUCCESS);
92615a5a 130 rtas_st(rets, 1, val);
88045ac5
AG
131}
132
28e02042 133static void rtas_ibm_read_pci_config(PowerPCCPU *cpu, sPAPRMachineState *spapr,
3384f95c
DG
134 uint32_t token, uint32_t nargs,
135 target_ulong args,
136 uint32_t nret, target_ulong rets)
137{
92615a5a
DG
138 uint64_t buid;
139 uint32_t size, addr;
3384f95c 140
92615a5a 141 if ((nargs != 4) || (nret != 2)) {
a64d325d 142 rtas_st(rets, 0, RTAS_OUT_HW_ERROR);
3384f95c
DG
143 return;
144 }
92615a5a 145
a14aa92b 146 buid = rtas_ldq(args, 1);
3384f95c 147 size = rtas_ld(args, 3);
92615a5a
DG
148 addr = rtas_ld(args, 0);
149
150 finish_read_pci_config(spapr, buid, addr, size, rets);
3384f95c
DG
151}
152
28e02042 153static void rtas_read_pci_config(PowerPCCPU *cpu, sPAPRMachineState *spapr,
3384f95c
DG
154 uint32_t token, uint32_t nargs,
155 target_ulong args,
156 uint32_t nret, target_ulong rets)
157{
92615a5a 158 uint32_t size, addr;
3384f95c 159
92615a5a 160 if ((nargs != 2) || (nret != 2)) {
a64d325d 161 rtas_st(rets, 0, RTAS_OUT_HW_ERROR);
3384f95c
DG
162 return;
163 }
92615a5a 164
3384f95c 165 size = rtas_ld(args, 1);
92615a5a
DG
166 addr = rtas_ld(args, 0);
167
168 finish_read_pci_config(spapr, 0, addr, size, rets);
169}
170
28e02042 171static void finish_write_pci_config(sPAPRMachineState *spapr, uint64_t buid,
92615a5a
DG
172 uint32_t addr, uint32_t size,
173 uint32_t val, target_ulong rets)
174{
175 PCIDevice *pci_dev;
176
177 if ((size != 1) && (size != 2) && (size != 4)) {
178 /* access must be 1, 2 or 4 bytes */
a64d325d 179 rtas_st(rets, 0, RTAS_OUT_HW_ERROR);
92615a5a
DG
180 return;
181 }
182
46c5874e 183 pci_dev = spapr_pci_find_dev(spapr, buid, addr);
92615a5a
DG
184 addr = rtas_pci_cfgaddr(addr);
185
186 if (!pci_dev || (addr % size) || (addr >= pci_config_size(pci_dev))) {
187 /* Access must be to a valid device, within bounds and
188 * naturally aligned */
a64d325d 189 rtas_st(rets, 0, RTAS_OUT_HW_ERROR);
92615a5a
DG
190 return;
191 }
192
193 pci_host_config_write_common(pci_dev, addr, pci_config_size(pci_dev),
194 val, size);
195
a64d325d 196 rtas_st(rets, 0, RTAS_OUT_SUCCESS);
3384f95c
DG
197}
198
28e02042 199static void rtas_ibm_write_pci_config(PowerPCCPU *cpu, sPAPRMachineState *spapr,
3384f95c
DG
200 uint32_t token, uint32_t nargs,
201 target_ulong args,
202 uint32_t nret, target_ulong rets)
203{
92615a5a 204 uint64_t buid;
3384f95c 205 uint32_t val, size, addr;
3384f95c 206
92615a5a 207 if ((nargs != 5) || (nret != 1)) {
a64d325d 208 rtas_st(rets, 0, RTAS_OUT_HW_ERROR);
3384f95c
DG
209 return;
210 }
92615a5a 211
a14aa92b 212 buid = rtas_ldq(args, 1);
3384f95c
DG
213 val = rtas_ld(args, 4);
214 size = rtas_ld(args, 3);
92615a5a
DG
215 addr = rtas_ld(args, 0);
216
217 finish_write_pci_config(spapr, buid, addr, size, val, rets);
3384f95c
DG
218}
219
28e02042 220static void rtas_write_pci_config(PowerPCCPU *cpu, sPAPRMachineState *spapr,
3384f95c
DG
221 uint32_t token, uint32_t nargs,
222 target_ulong args,
223 uint32_t nret, target_ulong rets)
224{
225 uint32_t val, size, addr;
3384f95c 226
92615a5a 227 if ((nargs != 3) || (nret != 1)) {
a64d325d 228 rtas_st(rets, 0, RTAS_OUT_HW_ERROR);
3384f95c
DG
229 return;
230 }
92615a5a
DG
231
232
3384f95c
DG
233 val = rtas_ld(args, 2);
234 size = rtas_ld(args, 1);
92615a5a
DG
235 addr = rtas_ld(args, 0);
236
237 finish_write_pci_config(spapr, 0, addr, size, val, rets);
3384f95c
DG
238}
239
0ee2c058
AK
240/*
241 * Set MSI/MSIX message data.
242 * This is required for msi_notify()/msix_notify() which
243 * will write at the addresses via spapr_msi_write().
9a321e92
AK
244 *
245 * If hwaddr == 0, all entries will have .data == first_irq i.e.
246 * table will be reset.
0ee2c058 247 */
f1c2dc7c
AK
248static void spapr_msi_setmsg(PCIDevice *pdev, hwaddr addr, bool msix,
249 unsigned first_irq, unsigned req_num)
0ee2c058
AK
250{
251 unsigned i;
f1c2dc7c 252 MSIMessage msg = { .address = addr, .data = first_irq };
0ee2c058
AK
253
254 if (!msix) {
255 msi_set_message(pdev, msg);
256 trace_spapr_pci_msi_setup(pdev->name, 0, msg.address);
257 return;
258 }
259
9a321e92 260 for (i = 0; i < req_num; ++i) {
0ee2c058
AK
261 msix_set_message(pdev, i, msg);
262 trace_spapr_pci_msi_setup(pdev->name, i, msg.address);
9a321e92
AK
263 if (addr) {
264 ++msg.data;
265 }
0ee2c058
AK
266 }
267}
268
28e02042 269static void rtas_ibm_change_msi(PowerPCCPU *cpu, sPAPRMachineState *spapr,
0ee2c058
AK
270 uint32_t token, uint32_t nargs,
271 target_ulong args, uint32_t nret,
272 target_ulong rets)
273{
274 uint32_t config_addr = rtas_ld(args, 0);
a14aa92b 275 uint64_t buid = rtas_ldq(args, 1);
0ee2c058
AK
276 unsigned int func = rtas_ld(args, 3);
277 unsigned int req_num = rtas_ld(args, 4); /* 0 == remove all */
278 unsigned int seq_num = rtas_ld(args, 5);
279 unsigned int ret_intr_type;
d4a63ac8 280 unsigned int irq, max_irqs = 0;
0ee2c058
AK
281 sPAPRPHBState *phb = NULL;
282 PCIDevice *pdev = NULL;
9a321e92
AK
283 spapr_pci_msi *msi;
284 int *config_addr_key;
a005b3ef 285 Error *err = NULL;
0ee2c058
AK
286
287 switch (func) {
288 case RTAS_CHANGE_MSI_FN:
289 case RTAS_CHANGE_FN:
290 ret_intr_type = RTAS_TYPE_MSI;
291 break;
292 case RTAS_CHANGE_MSIX_FN:
293 ret_intr_type = RTAS_TYPE_MSIX;
294 break;
295 default:
295d51aa 296 error_report("rtas_ibm_change_msi(%u) is not implemented", func);
a64d325d 297 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
0ee2c058
AK
298 return;
299 }
300
301 /* Fins sPAPRPHBState */
46c5874e 302 phb = spapr_pci_find_phb(spapr, buid);
0ee2c058 303 if (phb) {
46c5874e 304 pdev = spapr_pci_find_dev(spapr, buid, config_addr);
0ee2c058
AK
305 }
306 if (!phb || !pdev) {
a64d325d 307 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
0ee2c058
AK
308 return;
309 }
310
ce266b75
GK
311 msi = (spapr_pci_msi *) g_hash_table_lookup(phb->msi, &config_addr);
312
0ee2c058
AK
313 /* Releasing MSIs */
314 if (!req_num) {
9a321e92
AK
315 if (!msi) {
316 trace_spapr_pci_msi("Releasing wrong config", config_addr);
a64d325d 317 rtas_st(rets, 0, RTAS_OUT_HW_ERROR);
0ee2c058
AK
318 return;
319 }
9a321e92
AK
320
321 xics_free(spapr->icp, msi->first_irq, msi->num);
32420522 322 if (msi_present(pdev)) {
d4a63ac8 323 spapr_msi_setmsg(pdev, 0, false, 0, 0);
32420522
AK
324 }
325 if (msix_present(pdev)) {
d4a63ac8 326 spapr_msi_setmsg(pdev, 0, true, 0, 0);
32420522 327 }
9a321e92
AK
328 g_hash_table_remove(phb->msi, &config_addr);
329
330 trace_spapr_pci_msi("Released MSIs", config_addr);
a64d325d 331 rtas_st(rets, 0, RTAS_OUT_SUCCESS);
0ee2c058
AK
332 rtas_st(rets, 1, 0);
333 return;
334 }
335
336 /* Enabling MSI */
337
28668b5f
AK
338 /* Check if the device supports as many IRQs as requested */
339 if (ret_intr_type == RTAS_TYPE_MSI) {
340 max_irqs = msi_nr_vectors_allocated(pdev);
341 } else if (ret_intr_type == RTAS_TYPE_MSIX) {
342 max_irqs = pdev->msix_entries_nr;
343 }
344 if (!max_irqs) {
9a321e92
AK
345 error_report("Requested interrupt type %d is not enabled for device %x",
346 ret_intr_type, config_addr);
28668b5f
AK
347 rtas_st(rets, 0, -1); /* Hardware error */
348 return;
349 }
350 /* Correct the number if the guest asked for too many */
351 if (req_num > max_irqs) {
9a321e92 352 trace_spapr_pci_msi_retry(config_addr, req_num, max_irqs);
28668b5f 353 req_num = max_irqs;
9a321e92
AK
354 irq = 0; /* to avoid misleading trace */
355 goto out;
28668b5f
AK
356 }
357
9a321e92
AK
358 /* Allocate MSIs */
359 irq = xics_alloc_block(spapr->icp, 0, req_num, false,
a005b3ef
GK
360 ret_intr_type == RTAS_TYPE_MSI, &err);
361 if (err) {
362 error_reportf_err(err, "Can't allocate MSIs for device %x: ",
363 config_addr);
a64d325d 364 rtas_st(rets, 0, RTAS_OUT_HW_ERROR);
0ee2c058
AK
365 return;
366 }
367
ce266b75
GK
368 /* Release previous MSIs */
369 if (msi) {
370 xics_free(spapr->icp, msi->first_irq, msi->num);
371 g_hash_table_remove(phb->msi, &config_addr);
372 }
373
0ee2c058 374 /* Setup MSI/MSIX vectors in the device (via cfgspace or MSIX BAR) */
8c46f7ec 375 spapr_msi_setmsg(pdev, SPAPR_PCI_MSI_WINDOW, ret_intr_type == RTAS_TYPE_MSIX,
9a321e92 376 irq, req_num);
0ee2c058 377
9a321e92
AK
378 /* Add MSI device to cache */
379 msi = g_new(spapr_pci_msi, 1);
380 msi->first_irq = irq;
381 msi->num = req_num;
382 config_addr_key = g_new(int, 1);
383 *config_addr_key = config_addr;
384 g_hash_table_insert(phb->msi, config_addr_key, msi);
385
386out:
a64d325d 387 rtas_st(rets, 0, RTAS_OUT_SUCCESS);
0ee2c058
AK
388 rtas_st(rets, 1, req_num);
389 rtas_st(rets, 2, ++seq_num);
b359bd6a
SB
390 if (nret > 3) {
391 rtas_st(rets, 3, ret_intr_type);
392 }
0ee2c058 393
9a321e92 394 trace_spapr_pci_rtas_ibm_change_msi(config_addr, func, req_num, irq);
0ee2c058
AK
395}
396
210b580b 397static void rtas_ibm_query_interrupt_source_number(PowerPCCPU *cpu,
28e02042 398 sPAPRMachineState *spapr,
0ee2c058
AK
399 uint32_t token,
400 uint32_t nargs,
401 target_ulong args,
402 uint32_t nret,
403 target_ulong rets)
404{
405 uint32_t config_addr = rtas_ld(args, 0);
a14aa92b 406 uint64_t buid = rtas_ldq(args, 1);
0ee2c058 407 unsigned int intr_src_num = -1, ioa_intr_num = rtas_ld(args, 3);
0ee2c058 408 sPAPRPHBState *phb = NULL;
9a321e92
AK
409 PCIDevice *pdev = NULL;
410 spapr_pci_msi *msi;
0ee2c058 411
9a321e92 412 /* Find sPAPRPHBState */
46c5874e 413 phb = spapr_pci_find_phb(spapr, buid);
9a321e92 414 if (phb) {
46c5874e 415 pdev = spapr_pci_find_dev(spapr, buid, config_addr);
9a321e92
AK
416 }
417 if (!phb || !pdev) {
a64d325d 418 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
0ee2c058
AK
419 return;
420 }
421
422 /* Find device descriptor and start IRQ */
9a321e92
AK
423 msi = (spapr_pci_msi *) g_hash_table_lookup(phb->msi, &config_addr);
424 if (!msi || !msi->first_irq || !msi->num || (ioa_intr_num >= msi->num)) {
425 trace_spapr_pci_msi("Failed to return vector", config_addr);
a64d325d 426 rtas_st(rets, 0, RTAS_OUT_HW_ERROR);
0ee2c058
AK
427 return;
428 }
9a321e92 429 intr_src_num = msi->first_irq + ioa_intr_num;
0ee2c058
AK
430 trace_spapr_pci_rtas_ibm_query_interrupt_source_number(ioa_intr_num,
431 intr_src_num);
432
a64d325d 433 rtas_st(rets, 0, RTAS_OUT_SUCCESS);
0ee2c058
AK
434 rtas_st(rets, 1, intr_src_num);
435 rtas_st(rets, 2, 1);/* 0 == level; 1 == edge */
436}
437
ee954280 438static void rtas_ibm_set_eeh_option(PowerPCCPU *cpu,
28e02042 439 sPAPRMachineState *spapr,
ee954280
GS
440 uint32_t token, uint32_t nargs,
441 target_ulong args, uint32_t nret,
442 target_ulong rets)
443{
444 sPAPRPHBState *sphb;
ee954280
GS
445 uint32_t addr, option;
446 uint64_t buid;
447 int ret;
448
449 if ((nargs != 4) || (nret != 1)) {
450 goto param_error_exit;
451 }
452
a14aa92b 453 buid = rtas_ldq(args, 1);
ee954280
GS
454 addr = rtas_ld(args, 0);
455 option = rtas_ld(args, 3);
456
46c5874e 457 sphb = spapr_pci_find_phb(spapr, buid);
ee954280
GS
458 if (!sphb) {
459 goto param_error_exit;
460 }
461
fbb4e983 462 if (!spapr_phb_eeh_available(sphb)) {
ee954280
GS
463 goto param_error_exit;
464 }
465
fbb4e983 466 ret = spapr_phb_vfio_eeh_set_option(sphb, addr, option);
ee954280
GS
467 rtas_st(rets, 0, ret);
468 return;
469
470param_error_exit:
471 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
472}
473
474static void rtas_ibm_get_config_addr_info2(PowerPCCPU *cpu,
28e02042 475 sPAPRMachineState *spapr,
ee954280
GS
476 uint32_t token, uint32_t nargs,
477 target_ulong args, uint32_t nret,
478 target_ulong rets)
479{
480 sPAPRPHBState *sphb;
ee954280
GS
481 PCIDevice *pdev;
482 uint32_t addr, option;
483 uint64_t buid;
484
485 if ((nargs != 4) || (nret != 2)) {
486 goto param_error_exit;
487 }
488
a14aa92b 489 buid = rtas_ldq(args, 1);
46c5874e 490 sphb = spapr_pci_find_phb(spapr, buid);
ee954280
GS
491 if (!sphb) {
492 goto param_error_exit;
493 }
494
fbb4e983 495 if (!spapr_phb_eeh_available(sphb)) {
ee954280
GS
496 goto param_error_exit;
497 }
498
499 /*
500 * We always have PE address of form "00BB0001". "BB"
501 * represents the bus number of PE's primary bus.
502 */
503 option = rtas_ld(args, 3);
504 switch (option) {
505 case RTAS_GET_PE_ADDR:
506 addr = rtas_ld(args, 0);
46c5874e 507 pdev = spapr_pci_find_dev(spapr, buid, addr);
ee954280
GS
508 if (!pdev) {
509 goto param_error_exit;
510 }
511
512 rtas_st(rets, 1, (pci_bus_num(pdev->bus) << 16) + 1);
513 break;
514 case RTAS_GET_PE_MODE:
515 rtas_st(rets, 1, RTAS_PE_MODE_SHARED);
516 break;
517 default:
518 goto param_error_exit;
519 }
520
521 rtas_st(rets, 0, RTAS_OUT_SUCCESS);
522 return;
523
524param_error_exit:
525 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
526}
527
528static void rtas_ibm_read_slot_reset_state2(PowerPCCPU *cpu,
28e02042 529 sPAPRMachineState *spapr,
ee954280
GS
530 uint32_t token, uint32_t nargs,
531 target_ulong args, uint32_t nret,
532 target_ulong rets)
533{
534 sPAPRPHBState *sphb;
ee954280
GS
535 uint64_t buid;
536 int state, ret;
537
538 if ((nargs != 3) || (nret != 4 && nret != 5)) {
539 goto param_error_exit;
540 }
541
a14aa92b 542 buid = rtas_ldq(args, 1);
46c5874e 543 sphb = spapr_pci_find_phb(spapr, buid);
ee954280
GS
544 if (!sphb) {
545 goto param_error_exit;
546 }
547
fbb4e983 548 if (!spapr_phb_eeh_available(sphb)) {
ee954280
GS
549 goto param_error_exit;
550 }
551
fbb4e983 552 ret = spapr_phb_vfio_eeh_get_state(sphb, &state);
ee954280
GS
553 rtas_st(rets, 0, ret);
554 if (ret != RTAS_OUT_SUCCESS) {
555 return;
556 }
557
558 rtas_st(rets, 1, state);
559 rtas_st(rets, 2, RTAS_EEH_SUPPORT);
560 rtas_st(rets, 3, RTAS_EEH_PE_UNAVAIL_INFO);
561 if (nret >= 5) {
562 rtas_st(rets, 4, RTAS_EEH_PE_RECOVER_INFO);
563 }
564 return;
565
566param_error_exit:
567 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
568}
569
570static void rtas_ibm_set_slot_reset(PowerPCCPU *cpu,
28e02042 571 sPAPRMachineState *spapr,
ee954280
GS
572 uint32_t token, uint32_t nargs,
573 target_ulong args, uint32_t nret,
574 target_ulong rets)
575{
576 sPAPRPHBState *sphb;
ee954280
GS
577 uint32_t option;
578 uint64_t buid;
579 int ret;
580
581 if ((nargs != 4) || (nret != 1)) {
582 goto param_error_exit;
583 }
584
a14aa92b 585 buid = rtas_ldq(args, 1);
ee954280 586 option = rtas_ld(args, 3);
46c5874e 587 sphb = spapr_pci_find_phb(spapr, buid);
ee954280
GS
588 if (!sphb) {
589 goto param_error_exit;
590 }
591
fbb4e983 592 if (!spapr_phb_eeh_available(sphb)) {
ee954280
GS
593 goto param_error_exit;
594 }
595
fbb4e983 596 ret = spapr_phb_vfio_eeh_reset(sphb, option);
ee954280
GS
597 rtas_st(rets, 0, ret);
598 return;
599
600param_error_exit:
601 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
602}
603
604static void rtas_ibm_configure_pe(PowerPCCPU *cpu,
28e02042 605 sPAPRMachineState *spapr,
ee954280
GS
606 uint32_t token, uint32_t nargs,
607 target_ulong args, uint32_t nret,
608 target_ulong rets)
609{
610 sPAPRPHBState *sphb;
ee954280
GS
611 uint64_t buid;
612 int ret;
613
614 if ((nargs != 3) || (nret != 1)) {
615 goto param_error_exit;
616 }
617
a14aa92b 618 buid = rtas_ldq(args, 1);
46c5874e 619 sphb = spapr_pci_find_phb(spapr, buid);
ee954280
GS
620 if (!sphb) {
621 goto param_error_exit;
622 }
623
fbb4e983 624 if (!spapr_phb_eeh_available(sphb)) {
ee954280
GS
625 goto param_error_exit;
626 }
627
fbb4e983 628 ret = spapr_phb_vfio_eeh_configure(sphb);
ee954280
GS
629 rtas_st(rets, 0, ret);
630 return;
631
632param_error_exit:
633 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
634}
635
636/* To support it later */
637static void rtas_ibm_slot_error_detail(PowerPCCPU *cpu,
28e02042 638 sPAPRMachineState *spapr,
ee954280
GS
639 uint32_t token, uint32_t nargs,
640 target_ulong args, uint32_t nret,
641 target_ulong rets)
642{
643 sPAPRPHBState *sphb;
ee954280
GS
644 int option;
645 uint64_t buid;
646
647 if ((nargs != 8) || (nret != 1)) {
648 goto param_error_exit;
649 }
650
a14aa92b 651 buid = rtas_ldq(args, 1);
46c5874e 652 sphb = spapr_pci_find_phb(spapr, buid);
ee954280
GS
653 if (!sphb) {
654 goto param_error_exit;
655 }
656
fbb4e983 657 if (!spapr_phb_eeh_available(sphb)) {
ee954280
GS
658 goto param_error_exit;
659 }
660
661 option = rtas_ld(args, 7);
662 switch (option) {
663 case RTAS_SLOT_TEMP_ERR_LOG:
664 case RTAS_SLOT_PERM_ERR_LOG:
665 break;
666 default:
667 goto param_error_exit;
668 }
669
670 /* We don't have error log yet */
671 rtas_st(rets, 0, RTAS_OUT_NO_ERRORS_FOUND);
672 return;
673
674param_error_exit:
675 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
676}
677
7fb0bd34
DG
678static int pci_spapr_swizzle(int slot, int pin)
679{
680 return (slot + pin) % PCI_NUM_PINS;
681}
682
3384f95c
DG
683static int pci_spapr_map_irq(PCIDevice *pci_dev, int irq_num)
684{
685 /*
686 * Here we need to convert pci_dev + irq_num to some unique value
7fb0bd34
DG
687 * which is less than number of IRQs on the specific bus (4). We
688 * use standard PCI swizzling, that is (slot number + pin number)
689 * % 4.
3384f95c 690 */
7fb0bd34 691 return pci_spapr_swizzle(PCI_SLOT(pci_dev->devfn), irq_num);
3384f95c
DG
692}
693
694static void pci_spapr_set_irq(void *opaque, int irq_num, int level)
695{
696 /*
697 * Here we use the number returned by pci_spapr_map_irq to find a
698 * corresponding qemu_irq.
699 */
700 sPAPRPHBState *phb = opaque;
701
caae58cb 702 trace_spapr_pci_lsi_set(phb->dtbusname, irq_num, phb->lsi_table[irq_num].irq);
a307d594 703 qemu_set_irq(spapr_phb_lsi_qirq(phb, irq_num), level);
3384f95c
DG
704}
705
5cc7a967
AK
706static PCIINTxRoute spapr_route_intx_pin_to_irq(void *opaque, int pin)
707{
708 sPAPRPHBState *sphb = SPAPR_PCI_HOST_BRIDGE(opaque);
709 PCIINTxRoute route;
710
711 route.mode = PCI_INTX_ENABLED;
712 route.irq = sphb->lsi_table[pin].irq;
713
714 return route;
715}
716
0ee2c058
AK
717/*
718 * MSI/MSIX memory region implementation.
719 * The handler handles both MSI and MSIX.
720 * For MSI-X, the vector number is encoded as a part of the address,
721 * data is set to 0.
722 * For MSI, the vector number is encoded in least bits in data.
723 */
a8170e5e 724static void spapr_msi_write(void *opaque, hwaddr addr,
0ee2c058
AK
725 uint64_t data, unsigned size)
726{
28e02042 727 sPAPRMachineState *spapr = SPAPR_MACHINE(qdev_get_machine());
f1c2dc7c 728 uint32_t irq = data;
0ee2c058
AK
729
730 trace_spapr_pci_msi_write(addr, data, irq);
731
732 qemu_irq_pulse(xics_get_qirq(spapr->icp, irq));
733}
734
735static const MemoryRegionOps spapr_msi_ops = {
736 /* There is no .read as the read result is undefined by PCI spec */
737 .read = NULL,
738 .write = spapr_msi_write,
739 .endianness = DEVICE_LITTLE_ENDIAN
740};
741
298a9710
DG
742/*
743 * PHB PCI device
744 */
e00387d5 745static AddressSpace *spapr_pci_dma_iommu(PCIBus *bus, void *opaque, int devfn)
edded454
DG
746{
747 sPAPRPHBState *phb = opaque;
748
e00387d5 749 return &phb->iommu_as;
edded454
DG
750}
751
16b0ea1d
ND
752static char *spapr_phb_vfio_get_loc_code(sPAPRPHBState *sphb, PCIDevice *pdev)
753{
754 char *path = NULL, *buf = NULL, *host = NULL;
755
756 /* Get the PCI VFIO host id */
757 host = object_property_get_str(OBJECT(pdev), "host", NULL);
758 if (!host) {
759 goto err_out;
760 }
761
762 /* Construct the path of the file that will give us the DT location */
763 path = g_strdup_printf("/sys/bus/pci/devices/%s/devspec", host);
764 g_free(host);
765 if (!path || !g_file_get_contents(path, &buf, NULL, NULL)) {
766 goto err_out;
767 }
768 g_free(path);
769
770 /* Construct and read from host device tree the loc-code */
771 path = g_strdup_printf("/proc/device-tree%s/ibm,loc-code", buf);
772 g_free(buf);
773 if (!path || !g_file_get_contents(path, &buf, NULL, NULL)) {
774 goto err_out;
775 }
776 return buf;
777
778err_out:
779 g_free(path);
780 return NULL;
781}
782
783static char *spapr_phb_get_loc_code(sPAPRPHBState *sphb, PCIDevice *pdev)
784{
785 char *buf;
786 const char *devtype = "qemu";
787 uint32_t busnr = pci_bus_num(PCI_BUS(qdev_get_parent_bus(DEVICE(pdev))));
788
789 if (object_dynamic_cast(OBJECT(pdev), "vfio-pci")) {
790 buf = spapr_phb_vfio_get_loc_code(sphb, pdev);
791 if (buf) {
792 return buf;
793 }
794 devtype = "vfio";
795 }
796 /*
797 * For emulated devices and VFIO-failure case, make up
798 * the loc-code.
799 */
800 buf = g_strdup_printf("%s_%s:%04x:%02x:%02x.%x",
801 devtype, pdev->name, sphb->index, busnr,
802 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
803 return buf;
804}
805
7454c7af
MR
806/* Macros to operate with address in OF binding to PCI */
807#define b_x(x, p, l) (((x) & ((1<<(l))-1)) << (p))
808#define b_n(x) b_x((x), 31, 1) /* 0 if relocatable */
809#define b_p(x) b_x((x), 30, 1) /* 1 if prefetchable */
810#define b_t(x) b_x((x), 29, 1) /* 1 if the address is aliased */
811#define b_ss(x) b_x((x), 24, 2) /* the space code */
812#define b_bbbbbbbb(x) b_x((x), 16, 8) /* bus number */
813#define b_ddddd(x) b_x((x), 11, 5) /* device number */
814#define b_fff(x) b_x((x), 8, 3) /* function number */
815#define b_rrrrrrrr(x) b_x((x), 0, 8) /* register number */
816
817/* for 'reg'/'assigned-addresses' OF properties */
818#define RESOURCE_CELLS_SIZE 2
819#define RESOURCE_CELLS_ADDRESS 3
820
821typedef struct ResourceFields {
822 uint32_t phys_hi;
823 uint32_t phys_mid;
824 uint32_t phys_lo;
825 uint32_t size_hi;
826 uint32_t size_lo;
827} QEMU_PACKED ResourceFields;
828
829typedef struct ResourceProps {
830 ResourceFields reg[8];
831 ResourceFields assigned[7];
832 uint32_t reg_len;
833 uint32_t assigned_len;
834} ResourceProps;
835
836/* fill in the 'reg'/'assigned-resources' OF properties for
837 * a PCI device. 'reg' describes resource requirements for a
838 * device's IO/MEM regions, 'assigned-addresses' describes the
839 * actual resource assignments.
840 *
841 * the properties are arrays of ('phys-addr', 'size') pairs describing
842 * the addressable regions of the PCI device, where 'phys-addr' is a
843 * RESOURCE_CELLS_ADDRESS-tuple of 32-bit integers corresponding to
844 * (phys.hi, phys.mid, phys.lo), and 'size' is a
845 * RESOURCE_CELLS_SIZE-tuple corresponding to (size.hi, size.lo).
846 *
847 * phys.hi = 0xYYXXXXZZ, where:
848 * 0xYY = npt000ss
849 * ||| |
72187935
ND
850 * ||| +-- space code
851 * ||| |
852 * ||| + 00 if configuration space
853 * ||| + 01 if IO region,
854 * ||| + 10 if 32-bit MEM region
855 * ||| + 11 if 64-bit MEM region
856 * |||
7454c7af
MR
857 * ||+------ for non-relocatable IO: 1 if aliased
858 * || for relocatable IO: 1 if below 64KB
859 * || for MEM: 1 if below 1MB
860 * |+------- 1 if region is prefetchable
861 * +-------- 1 if region is non-relocatable
862 * 0xXXXX = bbbbbbbb dddddfff, encoding bus, slot, and function
863 * bits respectively
864 * 0xZZ = rrrrrrrr, the register number of the BAR corresponding
865 * to the region
866 *
867 * phys.mid and phys.lo correspond respectively to the hi/lo portions
868 * of the actual address of the region.
869 *
870 * how the phys-addr/size values are used differ slightly between
871 * 'reg' and 'assigned-addresses' properties. namely, 'reg' has
872 * an additional description for the config space region of the
873 * device, and in the case of QEMU has n=0 and phys.mid=phys.lo=0
874 * to describe the region as relocatable, with an address-mapping
875 * that corresponds directly to the PHB's address space for the
876 * resource. 'assigned-addresses' always has n=1 set with an absolute
877 * address assigned for the resource. in general, 'assigned-addresses'
878 * won't be populated, since addresses for PCI devices are generally
879 * unmapped initially and left to the guest to assign.
880 *
881 * note also that addresses defined in these properties are, at least
882 * for PAPR guests, relative to the PHBs IO/MEM windows, and
883 * correspond directly to the addresses in the BARs.
884 *
885 * in accordance with PCI Bus Binding to Open Firmware,
886 * IEEE Std 1275-1994, section 4.1.1, as implemented by PAPR+ v2.7,
887 * Appendix C.
888 */
889static void populate_resource_props(PCIDevice *d, ResourceProps *rp)
890{
891 int bus_num = pci_bus_num(PCI_BUS(qdev_get_parent_bus(DEVICE(d))));
892 uint32_t dev_id = (b_bbbbbbbb(bus_num) |
893 b_ddddd(PCI_SLOT(d->devfn)) |
894 b_fff(PCI_FUNC(d->devfn)));
895 ResourceFields *reg, *assigned;
896 int i, reg_idx = 0, assigned_idx = 0;
897
898 /* config space region */
899 reg = &rp->reg[reg_idx++];
900 reg->phys_hi = cpu_to_be32(dev_id);
901 reg->phys_mid = 0;
902 reg->phys_lo = 0;
903 reg->size_hi = 0;
904 reg->size_lo = 0;
905
906 for (i = 0; i < PCI_NUM_REGIONS; i++) {
907 if (!d->io_regions[i].size) {
908 continue;
909 }
910
911 reg = &rp->reg[reg_idx++];
912
913 reg->phys_hi = cpu_to_be32(dev_id | b_rrrrrrrr(pci_bar(d, i)));
914 if (d->io_regions[i].type & PCI_BASE_ADDRESS_SPACE_IO) {
915 reg->phys_hi |= cpu_to_be32(b_ss(1));
72187935
ND
916 } else if (d->io_regions[i].type & PCI_BASE_ADDRESS_MEM_TYPE_64) {
917 reg->phys_hi |= cpu_to_be32(b_ss(3));
7454c7af
MR
918 } else {
919 reg->phys_hi |= cpu_to_be32(b_ss(2));
920 }
921 reg->phys_mid = 0;
922 reg->phys_lo = 0;
923 reg->size_hi = cpu_to_be32(d->io_regions[i].size >> 32);
924 reg->size_lo = cpu_to_be32(d->io_regions[i].size);
925
926 if (d->io_regions[i].addr == PCI_BAR_UNMAPPED) {
927 continue;
928 }
929
930 assigned = &rp->assigned[assigned_idx++];
931 assigned->phys_hi = cpu_to_be32(reg->phys_hi | b_n(1));
932 assigned->phys_mid = cpu_to_be32(d->io_regions[i].addr >> 32);
933 assigned->phys_lo = cpu_to_be32(d->io_regions[i].addr);
934 assigned->size_hi = reg->size_hi;
935 assigned->size_lo = reg->size_lo;
936 }
937
938 rp->reg_len = reg_idx * sizeof(ResourceFields);
939 rp->assigned_len = assigned_idx * sizeof(ResourceFields);
940}
941
e634b89c
ND
942static uint32_t spapr_phb_get_pci_drc_index(sPAPRPHBState *phb,
943 PCIDevice *pdev);
944
7454c7af 945static int spapr_populate_pci_child_dt(PCIDevice *dev, void *fdt, int offset,
16b0ea1d 946 sPAPRPHBState *sphb)
7454c7af
MR
947{
948 ResourceProps rp;
949 bool is_bridge = false;
16b0ea1d
ND
950 int pci_status, err;
951 char *buf = NULL;
e634b89c 952 uint32_t drc_index = spapr_phb_get_pci_drc_index(sphb, dev);
a8ad731a 953 uint32_t max_msi, max_msix;
7454c7af
MR
954
955 if (pci_default_read_config(dev, PCI_HEADER_TYPE, 1) ==
956 PCI_HEADER_TYPE_BRIDGE) {
957 is_bridge = true;
958 }
959
960 /* in accordance with PAPR+ v2.7 13.6.3, Table 181 */
961 _FDT(fdt_setprop_cell(fdt, offset, "vendor-id",
962 pci_default_read_config(dev, PCI_VENDOR_ID, 2)));
963 _FDT(fdt_setprop_cell(fdt, offset, "device-id",
964 pci_default_read_config(dev, PCI_DEVICE_ID, 2)));
965 _FDT(fdt_setprop_cell(fdt, offset, "revision-id",
966 pci_default_read_config(dev, PCI_REVISION_ID, 1)));
967 _FDT(fdt_setprop_cell(fdt, offset, "class-code",
4a7c3474 968 pci_default_read_config(dev, PCI_CLASS_PROG, 3)));
7454c7af
MR
969 if (pci_default_read_config(dev, PCI_INTERRUPT_PIN, 1)) {
970 _FDT(fdt_setprop_cell(fdt, offset, "interrupts",
971 pci_default_read_config(dev, PCI_INTERRUPT_PIN, 1)));
972 }
973
974 if (!is_bridge) {
975 _FDT(fdt_setprop_cell(fdt, offset, "min-grant",
976 pci_default_read_config(dev, PCI_MIN_GNT, 1)));
977 _FDT(fdt_setprop_cell(fdt, offset, "max-latency",
978 pci_default_read_config(dev, PCI_MAX_LAT, 1)));
979 }
980
981 if (pci_default_read_config(dev, PCI_SUBSYSTEM_ID, 2)) {
982 _FDT(fdt_setprop_cell(fdt, offset, "subsystem-id",
983 pci_default_read_config(dev, PCI_SUBSYSTEM_ID, 2)));
984 }
985
986 if (pci_default_read_config(dev, PCI_SUBSYSTEM_VENDOR_ID, 2)) {
987 _FDT(fdt_setprop_cell(fdt, offset, "subsystem-vendor-id",
988 pci_default_read_config(dev, PCI_SUBSYSTEM_VENDOR_ID, 2)));
989 }
990
991 _FDT(fdt_setprop_cell(fdt, offset, "cache-line-size",
992 pci_default_read_config(dev, PCI_CACHE_LINE_SIZE, 1)));
993
994 /* the following fdt cells are masked off the pci status register */
995 pci_status = pci_default_read_config(dev, PCI_STATUS, 2);
996 _FDT(fdt_setprop_cell(fdt, offset, "devsel-speed",
997 PCI_STATUS_DEVSEL_MASK & pci_status));
998
999 if (pci_status & PCI_STATUS_FAST_BACK) {
1000 _FDT(fdt_setprop(fdt, offset, "fast-back-to-back", NULL, 0));
1001 }
1002 if (pci_status & PCI_STATUS_66MHZ) {
1003 _FDT(fdt_setprop(fdt, offset, "66mhz-capable", NULL, 0));
1004 }
1005 if (pci_status & PCI_STATUS_UDF) {
1006 _FDT(fdt_setprop(fdt, offset, "udf-supported", NULL, 0));
1007 }
1008
1009 /* NOTE: this is normally generated by firmware via path/unit name,
1010 * but in our case we must set it manually since it does not get
1011 * processed by OF beforehand
1012 */
1013 _FDT(fdt_setprop_string(fdt, offset, "name", "pci"));
16b0ea1d
ND
1014 buf = spapr_phb_get_loc_code(sphb, dev);
1015 if (!buf) {
1016 error_report("Failed setting the ibm,loc-code");
1017 return -1;
1018 }
1019
1020 err = fdt_setprop_string(fdt, offset, "ibm,loc-code", buf);
1021 g_free(buf);
1022 if (err < 0) {
1023 return err;
1024 }
1025
e634b89c
ND
1026 if (drc_index) {
1027 _FDT(fdt_setprop_cell(fdt, offset, "ibm,my-drc-index", drc_index));
1028 }
7454c7af
MR
1029
1030 _FDT(fdt_setprop_cell(fdt, offset, "#address-cells",
1031 RESOURCE_CELLS_ADDRESS));
1032 _FDT(fdt_setprop_cell(fdt, offset, "#size-cells",
1033 RESOURCE_CELLS_SIZE));
a8ad731a
MR
1034
1035 max_msi = msi_nr_vectors_allocated(dev);
1036 if (max_msi) {
1037 _FDT(fdt_setprop_cell(fdt, offset, "ibm,req#msi", max_msi));
1038 }
1039 max_msix = dev->msix_entries_nr;
1040 if (max_msix) {
1041 _FDT(fdt_setprop_cell(fdt, offset, "ibm,req#msi-x", max_msix));
1042 }
7454c7af
MR
1043
1044 populate_resource_props(dev, &rp);
1045 _FDT(fdt_setprop(fdt, offset, "reg", (uint8_t *)rp.reg, rp.reg_len));
1046 _FDT(fdt_setprop(fdt, offset, "assigned-addresses",
1047 (uint8_t *)rp.assigned, rp.assigned_len));
1048
1049 return 0;
1050}
1051
1052/* create OF node for pci device and required OF DT properties */
1d2d9742 1053static int spapr_create_pci_child_dt(sPAPRPHBState *phb, PCIDevice *dev,
1d2d9742 1054 void *fdt, int node_offset)
7454c7af 1055{
1d2d9742 1056 int offset, ret;
7454c7af
MR
1057 int slot = PCI_SLOT(dev->devfn);
1058 int func = PCI_FUNC(dev->devfn);
9b7d9284 1059 char nodename[FDT_NAME_MAX];
7454c7af 1060
7454c7af 1061 if (func != 0) {
9b7d9284 1062 snprintf(nodename, FDT_NAME_MAX, "pci@%x,%x", slot, func);
7454c7af 1063 } else {
9b7d9284 1064 snprintf(nodename, FDT_NAME_MAX, "pci@%x", slot);
7454c7af 1065 }
1d2d9742 1066 offset = fdt_add_subnode(fdt, node_offset, nodename);
e634b89c
ND
1067 ret = spapr_populate_pci_child_dt(dev, fdt, offset, phb);
1068
7454c7af 1069 g_assert(!ret);
1d2d9742
ND
1070 if (ret) {
1071 return 0;
1072 }
1073 return offset;
7454c7af
MR
1074}
1075
1076static void spapr_phb_add_pci_device(sPAPRDRConnector *drc,
1077 sPAPRPHBState *phb,
1078 PCIDevice *pdev,
1079 Error **errp)
1080{
1081 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
1082 DeviceState *dev = DEVICE(pdev);
7454c7af 1083 void *fdt = NULL;
1d2d9742 1084 int fdt_start_offset = 0, fdt_size;
7454c7af 1085
185181f8
DG
1086 if (object_dynamic_cast(OBJECT(pdev), "vfio-pci")) {
1087 sPAPRTCETable *tcet = spapr_tce_find_by_liobn(phb->dma_liobn);
1088
1089 spapr_tce_set_need_vfio(tcet, true);
1090 }
1091
7454c7af 1092 if (dev->hotplugged) {
1d2d9742 1093 fdt = create_device_tree(&fdt_size);
e634b89c 1094 fdt_start_offset = spapr_create_pci_child_dt(phb, pdev, fdt, 0);
1d2d9742
ND
1095 if (!fdt_start_offset) {
1096 error_setg(errp, "Failed to create pci child device tree node");
1097 goto out;
1098 }
7454c7af
MR
1099 }
1100
1101 drck->attach(drc, DEVICE(pdev),
1102 fdt, fdt_start_offset, !dev->hotplugged, errp);
1d2d9742 1103out:
7454c7af
MR
1104 if (*errp) {
1105 g_free(fdt);
1106 }
1107}
1108
1109static void spapr_phb_remove_pci_device_cb(DeviceState *dev, void *opaque)
1110{
1111 /* some version guests do not wait for completion of a device
1112 * cleanup (generally done asynchronously by the kernel) before
1113 * signaling to QEMU that the device is safe, but instead sleep
1114 * for some 'safe' period of time. unfortunately on a busy host
1115 * this sleep isn't guaranteed to be long enough, resulting in
1116 * bad things like IRQ lines being left asserted during final
1117 * device removal. to deal with this we call reset just prior
1118 * to finalizing the device, which will put the device back into
1119 * an 'idle' state, as the device cleanup code expects.
1120 */
1121 pci_device_reset(PCI_DEVICE(dev));
1122 object_unparent(OBJECT(dev));
1123}
1124
1125static void spapr_phb_remove_pci_device(sPAPRDRConnector *drc,
1126 sPAPRPHBState *phb,
1127 PCIDevice *pdev,
1128 Error **errp)
1129{
1130 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
1131
1132 drck->detach(drc, DEVICE(pdev), spapr_phb_remove_pci_device_cb, phb, errp);
1133}
1134
788d2599
MR
1135static sPAPRDRConnector *spapr_phb_get_pci_func_drc(sPAPRPHBState *phb,
1136 uint32_t busnr,
1137 int32_t devfn)
7454c7af 1138{
7454c7af
MR
1139 return spapr_dr_connector_by_id(SPAPR_DR_CONNECTOR_TYPE_PCI,
1140 (phb->index << 16) |
1141 (busnr << 8) |
788d2599
MR
1142 devfn);
1143}
1144
1145static sPAPRDRConnector *spapr_phb_get_pci_drc(sPAPRPHBState *phb,
1146 PCIDevice *pdev)
1147{
1148 uint32_t busnr = pci_bus_num(PCI_BUS(qdev_get_parent_bus(DEVICE(pdev))));
1149 return spapr_phb_get_pci_func_drc(phb, busnr, pdev->devfn);
7454c7af
MR
1150}
1151
1d2d9742
ND
1152static uint32_t spapr_phb_get_pci_drc_index(sPAPRPHBState *phb,
1153 PCIDevice *pdev)
1154{
1155 sPAPRDRConnector *drc = spapr_phb_get_pci_drc(phb, pdev);
1156 sPAPRDRConnectorClass *drck;
1157
1158 if (!drc) {
1159 return 0;
1160 }
1161
1162 drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
1163 return drck->get_index(drc);
1164}
1165
7454c7af
MR
1166static void spapr_phb_hot_plug_child(HotplugHandler *plug_handler,
1167 DeviceState *plugged_dev, Error **errp)
1168{
1169 sPAPRPHBState *phb = SPAPR_PCI_HOST_BRIDGE(DEVICE(plug_handler));
1170 PCIDevice *pdev = PCI_DEVICE(plugged_dev);
1171 sPAPRDRConnector *drc = spapr_phb_get_pci_drc(phb, pdev);
1172 Error *local_err = NULL;
788d2599
MR
1173 PCIBus *bus = PCI_BUS(qdev_get_parent_bus(DEVICE(pdev)));
1174 uint32_t slotnr = PCI_SLOT(pdev->devfn);
7454c7af
MR
1175
1176 /* if DR is disabled we don't need to do anything in the case of
1177 * hotplug or coldplug callbacks
1178 */
1179 if (!phb->dr_enabled) {
1180 /* if this is a hotplug operation initiated by the user
1181 * we need to let them know it's not enabled
1182 */
1183 if (plugged_dev->hotplugged) {
c6bd8c70
MA
1184 error_setg(errp, QERR_BUS_NO_HOTPLUG,
1185 object_get_typename(OBJECT(phb)));
7454c7af
MR
1186 }
1187 return;
1188 }
1189
1190 g_assert(drc);
1191
788d2599
MR
1192 /* Following the QEMU convention used for PCIe multifunction
1193 * hotplug, we do not allow functions to be hotplugged to a
1194 * slot that already has function 0 present
1195 */
1196 if (plugged_dev->hotplugged && bus->devices[PCI_DEVFN(slotnr, 0)] &&
1197 PCI_FUNC(pdev->devfn) != 0) {
1198 error_setg(errp, "PCI: slot %d function 0 already ocuppied by %s,"
1199 " additional functions can no longer be exposed to guest.",
1200 slotnr, bus->devices[PCI_DEVFN(slotnr, 0)]->name);
1201 return;
1202 }
1203
7454c7af
MR
1204 spapr_phb_add_pci_device(drc, phb, pdev, &local_err);
1205 if (local_err) {
1206 error_propagate(errp, local_err);
1207 return;
1208 }
788d2599
MR
1209
1210 /* If this is function 0, signal hotplug for all the device functions.
1211 * Otherwise defer sending the hotplug event.
1212 */
1213 if (plugged_dev->hotplugged && PCI_FUNC(pdev->devfn) == 0) {
1214 int i;
1215
1216 for (i = 0; i < 8; i++) {
1217 sPAPRDRConnector *func_drc;
1218 sPAPRDRConnectorClass *func_drck;
1219 sPAPRDREntitySense state;
1220
1221 func_drc = spapr_phb_get_pci_func_drc(phb, pci_bus_num(bus),
1222 PCI_DEVFN(slotnr, i));
1223 func_drck = SPAPR_DR_CONNECTOR_GET_CLASS(func_drc);
1224 func_drck->entity_sense(func_drc, &state);
1225
1226 if (state == SPAPR_DR_ENTITY_SENSE_PRESENT) {
1227 spapr_hotplug_req_add_by_index(func_drc);
1228 }
1229 }
c5bc152b 1230 }
7454c7af
MR
1231}
1232
1233static void spapr_phb_hot_unplug_child(HotplugHandler *plug_handler,
1234 DeviceState *plugged_dev, Error **errp)
1235{
1236 sPAPRPHBState *phb = SPAPR_PCI_HOST_BRIDGE(DEVICE(plug_handler));
1237 PCIDevice *pdev = PCI_DEVICE(plugged_dev);
1238 sPAPRDRConnectorClass *drck;
1239 sPAPRDRConnector *drc = spapr_phb_get_pci_drc(phb, pdev);
1240 Error *local_err = NULL;
1241
1242 if (!phb->dr_enabled) {
c6bd8c70
MA
1243 error_setg(errp, QERR_BUS_NO_HOTPLUG,
1244 object_get_typename(OBJECT(phb)));
7454c7af
MR
1245 return;
1246 }
1247
1248 g_assert(drc);
1249
1250 drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
1251 if (!drck->release_pending(drc)) {
788d2599
MR
1252 PCIBus *bus = PCI_BUS(qdev_get_parent_bus(DEVICE(pdev)));
1253 uint32_t slotnr = PCI_SLOT(pdev->devfn);
1254 sPAPRDRConnector *func_drc;
1255 sPAPRDRConnectorClass *func_drck;
1256 sPAPRDREntitySense state;
1257 int i;
1258
1259 /* ensure any other present functions are pending unplug */
1260 if (PCI_FUNC(pdev->devfn) == 0) {
1261 for (i = 1; i < 8; i++) {
1262 func_drc = spapr_phb_get_pci_func_drc(phb, pci_bus_num(bus),
1263 PCI_DEVFN(slotnr, i));
1264 func_drck = SPAPR_DR_CONNECTOR_GET_CLASS(func_drc);
1265 func_drck->entity_sense(func_drc, &state);
1266 if (state == SPAPR_DR_ENTITY_SENSE_PRESENT
1267 && !func_drck->release_pending(func_drc)) {
1268 error_setg(errp,
1269 "PCI: slot %d, function %d still present. "
1270 "Must unplug all non-0 functions first.",
1271 slotnr, i);
1272 return;
1273 }
1274 }
1275 }
1276
7454c7af
MR
1277 spapr_phb_remove_pci_device(drc, phb, pdev, &local_err);
1278 if (local_err) {
1279 error_propagate(errp, local_err);
1280 return;
1281 }
788d2599
MR
1282
1283 /* if this isn't func 0, defer unplug event. otherwise signal removal
1284 * for all present functions
1285 */
1286 if (PCI_FUNC(pdev->devfn) == 0) {
1287 for (i = 7; i >= 0; i--) {
1288 func_drc = spapr_phb_get_pci_func_drc(phb, pci_bus_num(bus),
1289 PCI_DEVFN(slotnr, i));
1290 func_drck = SPAPR_DR_CONNECTOR_GET_CLASS(func_drc);
1291 func_drck->entity_sense(func_drc, &state);
1292 if (state == SPAPR_DR_ENTITY_SENSE_PRESENT) {
1293 spapr_hotplug_req_remove_by_index(func_drc);
1294 }
1295 }
1296 }
7454c7af
MR
1297 }
1298}
1299
c6ba42f6 1300static void spapr_phb_realize(DeviceState *dev, Error **errp)
3384f95c 1301{
28e02042 1302 sPAPRMachineState *spapr = SPAPR_MACHINE(qdev_get_machine());
c6ba42f6 1303 SysBusDevice *s = SYS_BUS_DEVICE(dev);
8c9f64df 1304 sPAPRPHBState *sphb = SPAPR_PCI_HOST_BRIDGE(s);
8558d942 1305 PCIHostState *phb = PCI_HOST_BRIDGE(s);
da6ccee4 1306 sPAPRPHBClass *info = SPAPR_PCI_HOST_BRIDGE_GET_CLASS(s);
298a9710
DG
1307 char *namebuf;
1308 int i;
3384f95c 1309 PCIBus *bus;
8c46f7ec 1310 uint64_t msi_window_size = 4096;
3384f95c 1311
421b1b27 1312 if (sphb->index != (uint32_t)-1) {
caae58cb
DG
1313 hwaddr windows_base;
1314
421b1b27
DG
1315 if ((sphb->buid != (uint64_t)-1) || (sphb->dma_liobn != (uint32_t)-1)
1316 || (sphb->mem_win_addr != (hwaddr)-1)
1317 || (sphb->io_win_addr != (hwaddr)-1)) {
c6ba42f6
AK
1318 error_setg(errp, "Either \"index\" or other parameters must"
1319 " be specified for PAPR PHB, not both");
1320 return;
caae58cb
DG
1321 }
1322
3e4ac968
DG
1323 if (sphb->index > SPAPR_PCI_MAX_INDEX) {
1324 error_setg(errp, "\"index\" for PAPR PHB is too large (max %u)",
1325 SPAPR_PCI_MAX_INDEX);
1326 return;
1327 }
1328
caae58cb 1329 sphb->buid = SPAPR_PCI_BASE_BUID + sphb->index;
c8545818 1330 sphb->dma_liobn = SPAPR_PCI_LIOBN(sphb->index, 0);
caae58cb
DG
1331
1332 windows_base = SPAPR_PCI_WINDOW_BASE
1333 + sphb->index * SPAPR_PCI_WINDOW_SPACING;
1334 sphb->mem_win_addr = windows_base + SPAPR_PCI_MMIO_WIN_OFF;
1335 sphb->io_win_addr = windows_base + SPAPR_PCI_IO_WIN_OFF;
caae58cb
DG
1336 }
1337
421b1b27 1338 if (sphb->buid == (uint64_t)-1) {
c6ba42f6
AK
1339 error_setg(errp, "BUID not specified for PHB");
1340 return;
caae58cb
DG
1341 }
1342
421b1b27 1343 if (sphb->dma_liobn == (uint32_t)-1) {
c6ba42f6
AK
1344 error_setg(errp, "LIOBN not specified for PHB");
1345 return;
caae58cb
DG
1346 }
1347
421b1b27 1348 if (sphb->mem_win_addr == (hwaddr)-1) {
c6ba42f6
AK
1349 error_setg(errp, "Memory window address not specified for PHB");
1350 return;
caae58cb
DG
1351 }
1352
421b1b27 1353 if (sphb->io_win_addr == (hwaddr)-1) {
c6ba42f6
AK
1354 error_setg(errp, "IO window address not specified for PHB");
1355 return;
caae58cb
DG
1356 }
1357
46c5874e 1358 if (spapr_pci_find_phb(spapr, sphb->buid)) {
c6ba42f6
AK
1359 error_setg(errp, "PCI host bridges must have unique BUIDs");
1360 return;
caae58cb
DG
1361 }
1362
8c9f64df 1363 sphb->dtbusname = g_strdup_printf("pci@%" PRIx64, sphb->buid);
caae58cb 1364
8c9f64df 1365 namebuf = alloca(strlen(sphb->dtbusname) + 32);
3384f95c 1366
298a9710 1367 /* Initialize memory regions */
8c9f64df 1368 sprintf(namebuf, "%s.mmio", sphb->dtbusname);
92b8e39c 1369 memory_region_init(&sphb->memspace, OBJECT(sphb), namebuf, UINT64_MAX);
3384f95c 1370
8c9f64df 1371 sprintf(namebuf, "%s.mmio-alias", sphb->dtbusname);
40c5dce9
PB
1372 memory_region_init_alias(&sphb->memwindow, OBJECT(sphb),
1373 namebuf, &sphb->memspace,
8c9f64df
AF
1374 SPAPR_PCI_MEM_WIN_BUS_OFFSET, sphb->mem_win_size);
1375 memory_region_add_subregion(get_system_memory(), sphb->mem_win_addr,
1376 &sphb->memwindow);
3384f95c 1377
fabe9ee1 1378 /* Initialize IO regions */
8c9f64df 1379 sprintf(namebuf, "%s.io", sphb->dtbusname);
40c5dce9
PB
1380 memory_region_init(&sphb->iospace, OBJECT(sphb),
1381 namebuf, SPAPR_PCI_IO_WIN_SIZE);
3384f95c 1382
a3cfa18e 1383 sprintf(namebuf, "%s.io-alias", sphb->dtbusname);
66aab867 1384 memory_region_init_alias(&sphb->iowindow, OBJECT(sphb), namebuf,
fabe9ee1 1385 &sphb->iospace, 0, SPAPR_PCI_IO_WIN_SIZE);
8c9f64df 1386 memory_region_add_subregion(get_system_memory(), sphb->io_win_addr,
a3cfa18e 1387 &sphb->iowindow);
1b8601b0
AK
1388
1389 bus = pci_register_bus(dev, NULL,
8c9f64df
AF
1390 pci_spapr_set_irq, pci_spapr_map_irq, sphb,
1391 &sphb->memspace, &sphb->iospace,
60a0e443 1392 PCI_DEVFN(0, 0), PCI_NUM_PINS, TYPE_PCI_BUS);
8c9f64df 1393 phb->bus = bus;
7454c7af 1394 qbus_set_hotplug_handler(BUS(phb->bus), DEVICE(sphb), NULL);
298a9710 1395
cca7fad5
AK
1396 /*
1397 * Initialize PHB address space.
1398 * By default there will be at least one subregion for default
1399 * 32bit DMA window.
1400 * Later the guest might want to create another DMA window
1401 * which will become another memory subregion.
1402 */
1403 sprintf(namebuf, "%s.iommu-root", sphb->dtbusname);
1404
1405 memory_region_init(&sphb->iommu_root, OBJECT(sphb),
1406 namebuf, UINT64_MAX);
1407 address_space_init(&sphb->iommu_as, &sphb->iommu_root,
1408 sphb->dtbusname);
1409
8c46f7ec
GK
1410 /*
1411 * As MSI/MSIX interrupts trigger by writing at MSI/MSIX vectors,
1412 * we need to allocate some memory to catch those writes coming
1413 * from msi_notify()/msix_notify().
1414 * As MSIMessage:addr is going to be the same and MSIMessage:data
1415 * is going to be a VIRQ number, 4 bytes of the MSI MR will only
1416 * be used.
1417 *
1418 * For KVM we want to ensure that this memory is a full page so that
1419 * our memory slot is of page size granularity.
1420 */
1421#ifdef CONFIG_KVM
1422 if (kvm_enabled()) {
1423 msi_window_size = getpagesize();
1424 }
1425#endif
1426
1427 memory_region_init_io(&sphb->msiwindow, NULL, &spapr_msi_ops, spapr,
1428 "msi", msi_window_size);
1429 memory_region_add_subregion(&sphb->iommu_root, SPAPR_PCI_MSI_WINDOW,
1430 &sphb->msiwindow);
1431
e00387d5 1432 pci_setup_iommu(bus, spapr_pci_dma_iommu, sphb);
edded454 1433
5cc7a967
AK
1434 pci_bus_set_route_irq_fn(bus, spapr_route_intx_pin_to_irq);
1435
8c9f64df 1436 QLIST_INSERT_HEAD(&spapr->phbs, sphb, list);
298a9710
DG
1437
1438 /* Initialize the LSI table */
7fb0bd34 1439 for (i = 0; i < PCI_NUM_PINS; i++) {
a307d594 1440 uint32_t irq;
a005b3ef 1441 Error *local_err = NULL;
298a9710 1442
a005b3ef
GK
1443 irq = xics_alloc_block(spapr->icp, 0, 1, true, false, &local_err);
1444 if (local_err) {
1445 error_propagate(errp, local_err);
1446 error_prepend(errp, "can't allocate LSIs: ");
c6ba42f6 1447 return;
298a9710
DG
1448 }
1449
8c9f64df 1450 sphb->lsi_table[i].irq = irq;
298a9710 1451 }
da6ccee4 1452
62083979
MR
1453 /* allocate connectors for child PCI devices */
1454 if (sphb->dr_enabled) {
1455 for (i = 0; i < PCI_SLOT_MAX * 8; i++) {
1456 spapr_dr_connector_new(OBJECT(phb),
1457 SPAPR_DR_CONNECTOR_TYPE_PCI,
1458 (sphb->index << 16) | i);
1459 }
1460 }
1461
da6ccee4
AK
1462 if (!info->finish_realize) {
1463 error_setg(errp, "finish_realize not defined");
1464 return;
1465 }
1466
1467 info->finish_realize(sphb, errp);
9a321e92
AK
1468
1469 sphb->msi = g_hash_table_new_full(g_int_hash, g_int_equal, g_free, g_free);
da6ccee4
AK
1470}
1471
1472static void spapr_phb_finish_realize(sPAPRPHBState *sphb, Error **errp)
1473{
e28c16f6 1474 sPAPRTCETable *tcet;
3e1a01cb 1475 uint32_t nb_table;
e28c16f6 1476
f93caaac 1477 nb_table = sphb->dma_win_size >> SPAPR_TCE_PAGE_SHIFT;
e28c16f6 1478 tcet = spapr_tce_new_table(DEVICE(sphb), sphb->dma_liobn,
3e1a01cb 1479 0, SPAPR_TCE_PAGE_SHIFT, nb_table, false);
e28c16f6 1480 if (!tcet) {
da6ccee4
AK
1481 error_setg(errp, "Unable to create TCE table for %s",
1482 sphb->dtbusname);
1483 return ;
1484 }
cca7fad5
AK
1485
1486 /* Register default 32bit DMA window */
f93caaac 1487 memory_region_add_subregion(&sphb->iommu_root, sphb->dma_win_addr,
e28c16f6 1488 spapr_tce_get_iommu(tcet));
298a9710
DG
1489}
1490
e28c16f6 1491static int spapr_phb_children_reset(Object *child, void *opaque)
eddeed26 1492{
e28c16f6
AK
1493 DeviceState *dev = (DeviceState *) object_dynamic_cast(child, TYPE_DEVICE);
1494
1495 if (dev) {
1496 device_reset(dev);
1497 }
eddeed26 1498
e28c16f6
AK
1499 return 0;
1500}
1501
1502static void spapr_phb_reset(DeviceState *qdev)
1503{
eddeed26 1504 /* Reset the IOMMU state */
e28c16f6 1505 object_child_foreach(OBJECT(qdev), spapr_phb_children_reset, NULL);
fbb4e983
DG
1506
1507 if (spapr_phb_eeh_available(SPAPR_PCI_HOST_BRIDGE(qdev))) {
1508 spapr_phb_vfio_reset(qdev);
1509 }
eddeed26
DG
1510}
1511
298a9710 1512static Property spapr_phb_properties[] = {
3e4ac968 1513 DEFINE_PROP_UINT32("index", sPAPRPHBState, index, -1),
c7bcc85d
PB
1514 DEFINE_PROP_UINT64("buid", sPAPRPHBState, buid, -1),
1515 DEFINE_PROP_UINT32("liobn", sPAPRPHBState, dma_liobn, -1),
1516 DEFINE_PROP_UINT64("mem_win_addr", sPAPRPHBState, mem_win_addr, -1),
1517 DEFINE_PROP_UINT64("mem_win_size", sPAPRPHBState, mem_win_size,
1518 SPAPR_PCI_MMIO_WIN_SIZE),
1519 DEFINE_PROP_UINT64("io_win_addr", sPAPRPHBState, io_win_addr, -1),
1520 DEFINE_PROP_UINT64("io_win_size", sPAPRPHBState, io_win_size,
1521 SPAPR_PCI_IO_WIN_SIZE),
7619c7b0
MR
1522 DEFINE_PROP_BOOL("dynamic-reconfiguration", sPAPRPHBState, dr_enabled,
1523 true),
f93caaac
DG
1524 /* Default DMA window is 0..1GB */
1525 DEFINE_PROP_UINT64("dma_win_addr", sPAPRPHBState, dma_win_addr, 0),
1526 DEFINE_PROP_UINT64("dma_win_size", sPAPRPHBState, dma_win_size, 0x40000000),
298a9710
DG
1527 DEFINE_PROP_END_OF_LIST(),
1528};
1529
1112cf94
DG
1530static const VMStateDescription vmstate_spapr_pci_lsi = {
1531 .name = "spapr_pci/lsi",
1532 .version_id = 1,
1533 .minimum_version_id = 1,
3aff6c2f 1534 .fields = (VMStateField[]) {
1112cf94
DG
1535 VMSTATE_UINT32_EQUAL(irq, struct spapr_pci_lsi),
1536
1537 VMSTATE_END_OF_LIST()
1538 },
1539};
1540
1541static const VMStateDescription vmstate_spapr_pci_msi = {
9a321e92 1542 .name = "spapr_pci/msi",
1112cf94
DG
1543 .version_id = 1,
1544 .minimum_version_id = 1,
9a321e92
AK
1545 .fields = (VMStateField []) {
1546 VMSTATE_UINT32(key, spapr_pci_msi_mig),
1547 VMSTATE_UINT32(value.first_irq, spapr_pci_msi_mig),
1548 VMSTATE_UINT32(value.num, spapr_pci_msi_mig),
1112cf94
DG
1549 VMSTATE_END_OF_LIST()
1550 },
1551};
1552
9a321e92
AK
1553static void spapr_pci_pre_save(void *opaque)
1554{
1555 sPAPRPHBState *sphb = opaque;
708414f0
MA
1556 GHashTableIter iter;
1557 gpointer key, value;
1558 int i;
9a321e92 1559
012aef07
MA
1560 g_free(sphb->msi_devs);
1561 sphb->msi_devs = NULL;
708414f0
MA
1562 sphb->msi_devs_num = g_hash_table_size(sphb->msi);
1563 if (!sphb->msi_devs_num) {
9a321e92
AK
1564 return;
1565 }
708414f0 1566 sphb->msi_devs = g_malloc(sphb->msi_devs_num * sizeof(spapr_pci_msi_mig));
9a321e92 1567
708414f0
MA
1568 g_hash_table_iter_init(&iter, sphb->msi);
1569 for (i = 0; g_hash_table_iter_next(&iter, &key, &value); ++i) {
1570 sphb->msi_devs[i].key = *(uint32_t *) key;
1571 sphb->msi_devs[i].value = *(spapr_pci_msi *) value;
1572 }
9a321e92
AK
1573}
1574
1575static int spapr_pci_post_load(void *opaque, int version_id)
1576{
1577 sPAPRPHBState *sphb = opaque;
1578 gpointer key, value;
1579 int i;
1580
1581 for (i = 0; i < sphb->msi_devs_num; ++i) {
1582 key = g_memdup(&sphb->msi_devs[i].key,
1583 sizeof(sphb->msi_devs[i].key));
1584 value = g_memdup(&sphb->msi_devs[i].value,
1585 sizeof(sphb->msi_devs[i].value));
1586 g_hash_table_insert(sphb->msi, key, value);
1587 }
012aef07
MA
1588 g_free(sphb->msi_devs);
1589 sphb->msi_devs = NULL;
9a321e92
AK
1590 sphb->msi_devs_num = 0;
1591
1592 return 0;
1593}
1594
1112cf94
DG
1595static const VMStateDescription vmstate_spapr_pci = {
1596 .name = "spapr_pci",
9a321e92
AK
1597 .version_id = 2,
1598 .minimum_version_id = 2,
1599 .pre_save = spapr_pci_pre_save,
1600 .post_load = spapr_pci_post_load,
3aff6c2f 1601 .fields = (VMStateField[]) {
1112cf94
DG
1602 VMSTATE_UINT64_EQUAL(buid, sPAPRPHBState),
1603 VMSTATE_UINT32_EQUAL(dma_liobn, sPAPRPHBState),
1604 VMSTATE_UINT64_EQUAL(mem_win_addr, sPAPRPHBState),
1605 VMSTATE_UINT64_EQUAL(mem_win_size, sPAPRPHBState),
1606 VMSTATE_UINT64_EQUAL(io_win_addr, sPAPRPHBState),
1607 VMSTATE_UINT64_EQUAL(io_win_size, sPAPRPHBState),
1112cf94
DG
1608 VMSTATE_STRUCT_ARRAY(lsi_table, sPAPRPHBState, PCI_NUM_PINS, 0,
1609 vmstate_spapr_pci_lsi, struct spapr_pci_lsi),
9a321e92
AK
1610 VMSTATE_INT32(msi_devs_num, sPAPRPHBState),
1611 VMSTATE_STRUCT_VARRAY_ALLOC(msi_devs, sPAPRPHBState, msi_devs_num, 0,
1612 vmstate_spapr_pci_msi, spapr_pci_msi_mig),
1112cf94
DG
1613 VMSTATE_END_OF_LIST()
1614 },
1615};
1616
568f0690
DG
1617static const char *spapr_phb_root_bus_path(PCIHostState *host_bridge,
1618 PCIBus *rootbus)
1619{
1620 sPAPRPHBState *sphb = SPAPR_PCI_HOST_BRIDGE(host_bridge);
1621
1622 return sphb->dtbusname;
1623}
1624
298a9710
DG
1625static void spapr_phb_class_init(ObjectClass *klass, void *data)
1626{
568f0690 1627 PCIHostBridgeClass *hc = PCI_HOST_BRIDGE_CLASS(klass);
298a9710 1628 DeviceClass *dc = DEVICE_CLASS(klass);
da6ccee4 1629 sPAPRPHBClass *spc = SPAPR_PCI_HOST_BRIDGE_CLASS(klass);
7454c7af 1630 HotplugHandlerClass *hp = HOTPLUG_HANDLER_CLASS(klass);
298a9710 1631
568f0690 1632 hc->root_bus_path = spapr_phb_root_bus_path;
c6ba42f6 1633 dc->realize = spapr_phb_realize;
298a9710 1634 dc->props = spapr_phb_properties;
eddeed26 1635 dc->reset = spapr_phb_reset;
1112cf94 1636 dc->vmsd = &vmstate_spapr_pci;
09aa9a52
AK
1637 set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories);
1638 dc->cannot_instantiate_with_device_add_yet = false;
da6ccee4 1639 spc->finish_realize = spapr_phb_finish_realize;
7454c7af
MR
1640 hp->plug = spapr_phb_hot_plug_child;
1641 hp->unplug = spapr_phb_hot_unplug_child;
298a9710 1642}
3384f95c 1643
4240abff 1644static const TypeInfo spapr_phb_info = {
8c9f64df 1645 .name = TYPE_SPAPR_PCI_HOST_BRIDGE,
8558d942 1646 .parent = TYPE_PCI_HOST_BRIDGE,
298a9710
DG
1647 .instance_size = sizeof(sPAPRPHBState),
1648 .class_init = spapr_phb_class_init,
da6ccee4 1649 .class_size = sizeof(sPAPRPHBClass),
7454c7af
MR
1650 .interfaces = (InterfaceInfo[]) {
1651 { TYPE_HOTPLUG_HANDLER },
1652 { }
1653 }
298a9710
DG
1654};
1655
28e02042 1656PCIHostState *spapr_create_phb(sPAPRMachineState *spapr, int index)
298a9710
DG
1657{
1658 DeviceState *dev;
1659
8c9f64df 1660 dev = qdev_create(NULL, TYPE_SPAPR_PCI_HOST_BRIDGE);
caae58cb 1661 qdev_prop_set_uint32(dev, "index", index);
298a9710 1662 qdev_init_nofail(dev);
caae58cb
DG
1663
1664 return PCI_HOST_BRIDGE(dev);
3384f95c
DG
1665}
1666
1d2d9742
ND
1667typedef struct sPAPRFDT {
1668 void *fdt;
1669 int node_off;
1670 sPAPRPHBState *sphb;
1671} sPAPRFDT;
1672
1673static void spapr_populate_pci_devices_dt(PCIBus *bus, PCIDevice *pdev,
1674 void *opaque)
1675{
1676 PCIBus *sec_bus;
1677 sPAPRFDT *p = opaque;
1678 int offset;
1679 sPAPRFDT s_fdt;
1d2d9742 1680
e634b89c 1681 offset = spapr_create_pci_child_dt(p->sphb, pdev, p->fdt, p->node_off);
1d2d9742
ND
1682 if (!offset) {
1683 error_report("Failed to create pci child device tree node");
1684 return;
1685 }
1686
1687 if ((pci_default_read_config(pdev, PCI_HEADER_TYPE, 1) !=
1688 PCI_HEADER_TYPE_BRIDGE)) {
1689 return;
1690 }
1691
1692 sec_bus = pci_bridge_get_sec_bus(PCI_BRIDGE(pdev));
1693 if (!sec_bus) {
1694 return;
1695 }
1696
1697 s_fdt.fdt = p->fdt;
1698 s_fdt.node_off = offset;
1699 s_fdt.sphb = p->sphb;
1700 pci_for_each_device(sec_bus, pci_bus_num(sec_bus),
1701 spapr_populate_pci_devices_dt,
1702 &s_fdt);
1703}
1704
1705static void spapr_phb_pci_enumerate_bridge(PCIBus *bus, PCIDevice *pdev,
1706 void *opaque)
1707{
1708 unsigned int *bus_no = opaque;
1709 unsigned int primary = *bus_no;
1710 unsigned int subordinate = 0xff;
1711 PCIBus *sec_bus = NULL;
1712
1713 if ((pci_default_read_config(pdev, PCI_HEADER_TYPE, 1) !=
1714 PCI_HEADER_TYPE_BRIDGE)) {
1715 return;
1716 }
1717
1718 (*bus_no)++;
1719 pci_default_write_config(pdev, PCI_PRIMARY_BUS, primary, 1);
1720 pci_default_write_config(pdev, PCI_SECONDARY_BUS, *bus_no, 1);
1721 pci_default_write_config(pdev, PCI_SUBORDINATE_BUS, *bus_no, 1);
1722
1723 sec_bus = pci_bridge_get_sec_bus(PCI_BRIDGE(pdev));
1724 if (!sec_bus) {
1725 return;
1726 }
1727
1728 pci_default_write_config(pdev, PCI_SUBORDINATE_BUS, subordinate, 1);
1729 pci_for_each_device(sec_bus, pci_bus_num(sec_bus),
1730 spapr_phb_pci_enumerate_bridge, bus_no);
1731 pci_default_write_config(pdev, PCI_SUBORDINATE_BUS, *bus_no, 1);
1732}
1733
1734static void spapr_phb_pci_enumerate(sPAPRPHBState *phb)
1735{
1736 PCIBus *bus = PCI_HOST_BRIDGE(phb)->bus;
1737 unsigned int bus_no = 0;
1738
1739 pci_for_each_device(bus, pci_bus_num(bus),
1740 spapr_phb_pci_enumerate_bridge,
1741 &bus_no);
1742
1743}
1744
e0fdbd7c
AK
1745int spapr_populate_pci_dt(sPAPRPHBState *phb,
1746 uint32_t xics_phandle,
1747 void *fdt)
3384f95c 1748{
62083979 1749 int bus_off, i, j, ret;
9b7d9284 1750 char nodename[FDT_NAME_MAX];
3384f95c 1751 uint32_t bus_range[] = { cpu_to_be32(0), cpu_to_be32(0xff) };
b194df47
AK
1752 const uint64_t mmiosize = memory_region_size(&phb->memwindow);
1753 const uint64_t w32max = (1ULL << 32) - SPAPR_PCI_MEM_WIN_BUS_OFFSET;
1754 const uint64_t w32size = MIN(w32max, mmiosize);
1755 const uint64_t w64size = (mmiosize > w32size) ? (mmiosize - w32size) : 0;
3384f95c
DG
1756 struct {
1757 uint32_t hi;
1758 uint64_t child;
1759 uint64_t parent;
1760 uint64_t size;
c4889f54 1761 } QEMU_PACKED ranges[] = {
3384f95c
DG
1762 {
1763 cpu_to_be32(b_ss(1)), cpu_to_be64(0),
1764 cpu_to_be64(phb->io_win_addr),
1765 cpu_to_be64(memory_region_size(&phb->iospace)),
1766 },
1767 {
1768 cpu_to_be32(b_ss(2)), cpu_to_be64(SPAPR_PCI_MEM_WIN_BUS_OFFSET),
1769 cpu_to_be64(phb->mem_win_addr),
b194df47
AK
1770 cpu_to_be64(w32size),
1771 },
1772 {
1773 cpu_to_be32(b_ss(3)), cpu_to_be64(1ULL << 32),
1774 cpu_to_be64(phb->mem_win_addr + w32size),
1775 cpu_to_be64(w64size)
3384f95c
DG
1776 },
1777 };
b194df47 1778 const unsigned sizeof_ranges = (w64size ? 3 : 2) * sizeof(ranges[0]);
3384f95c
DG
1779 uint64_t bus_reg[] = { cpu_to_be64(phb->buid), 0 };
1780 uint32_t interrupt_map_mask[] = {
7fb0bd34
DG
1781 cpu_to_be32(b_ddddd(-1)|b_fff(0)), 0x0, 0x0, cpu_to_be32(-1)};
1782 uint32_t interrupt_map[PCI_SLOT_MAX * PCI_NUM_PINS][7];
ccf9ff85 1783 sPAPRTCETable *tcet;
1d2d9742
ND
1784 PCIBus *bus = PCI_HOST_BRIDGE(phb)->bus;
1785 sPAPRFDT s_fdt;
3384f95c
DG
1786
1787 /* Start populating the FDT */
9b7d9284 1788 snprintf(nodename, FDT_NAME_MAX, "pci@%" PRIx64, phb->buid);
3384f95c
DG
1789 bus_off = fdt_add_subnode(fdt, 0, nodename);
1790 if (bus_off < 0) {
1791 return bus_off;
1792 }
1793
3384f95c
DG
1794 /* Write PHB properties */
1795 _FDT(fdt_setprop_string(fdt, bus_off, "device_type", "pci"));
1796 _FDT(fdt_setprop_string(fdt, bus_off, "compatible", "IBM,Logical_PHB"));
1797 _FDT(fdt_setprop_cell(fdt, bus_off, "#address-cells", 0x3));
1798 _FDT(fdt_setprop_cell(fdt, bus_off, "#size-cells", 0x2));
1799 _FDT(fdt_setprop_cell(fdt, bus_off, "#interrupt-cells", 0x1));
1800 _FDT(fdt_setprop(fdt, bus_off, "used-by-rtas", NULL, 0));
1801 _FDT(fdt_setprop(fdt, bus_off, "bus-range", &bus_range, sizeof(bus_range)));
b194df47 1802 _FDT(fdt_setprop(fdt, bus_off, "ranges", &ranges, sizeof_ranges));
3384f95c 1803 _FDT(fdt_setprop(fdt, bus_off, "reg", &bus_reg, sizeof(bus_reg)));
3f7565c9 1804 _FDT(fdt_setprop_cell(fdt, bus_off, "ibm,pci-config-space-type", 0x1));
9dbae977 1805 _FDT(fdt_setprop_cell(fdt, bus_off, "ibm,pe-total-#msi", XICS_IRQS));
3384f95c 1806
4d8d5467
BH
1807 /* Build the interrupt-map, this must matches what is done
1808 * in pci_spapr_map_irq
1809 */
1810 _FDT(fdt_setprop(fdt, bus_off, "interrupt-map-mask",
1811 &interrupt_map_mask, sizeof(interrupt_map_mask)));
7fb0bd34
DG
1812 for (i = 0; i < PCI_SLOT_MAX; i++) {
1813 for (j = 0; j < PCI_NUM_PINS; j++) {
1814 uint32_t *irqmap = interrupt_map[i*PCI_NUM_PINS + j];
1815 int lsi_num = pci_spapr_swizzle(i, j);
1816
1817 irqmap[0] = cpu_to_be32(b_ddddd(i)|b_fff(0));
1818 irqmap[1] = 0;
1819 irqmap[2] = 0;
1820 irqmap[3] = cpu_to_be32(j+1);
1821 irqmap[4] = cpu_to_be32(xics_phandle);
a307d594 1822 irqmap[5] = cpu_to_be32(phb->lsi_table[lsi_num].irq);
7fb0bd34
DG
1823 irqmap[6] = cpu_to_be32(0x8);
1824 }
3384f95c 1825 }
3384f95c
DG
1826 /* Write interrupt map */
1827 _FDT(fdt_setprop(fdt, bus_off, "interrupt-map", &interrupt_map,
7fb0bd34 1828 sizeof(interrupt_map)));
3384f95c 1829
ccf9ff85
AK
1830 tcet = spapr_tce_find_by_liobn(SPAPR_PCI_LIOBN(phb->index, 0));
1831 spapr_dma_dt(fdt, bus_off, "ibm,dma-window",
1832 tcet->liobn, tcet->bus_offset,
1833 tcet->nb_table << tcet->page_shift);
edded454 1834
1d2d9742
ND
1835 /* Walk the bridges and program the bus numbers*/
1836 spapr_phb_pci_enumerate(phb);
1837 _FDT(fdt_setprop_cell(fdt, bus_off, "qemu,phb-enumerated", 0x1));
1838
1839 /* Populate tree nodes with PCI devices attached */
1840 s_fdt.fdt = fdt;
1841 s_fdt.node_off = bus_off;
1842 s_fdt.sphb = phb;
1843 pci_for_each_device(bus, pci_bus_num(bus),
1844 spapr_populate_pci_devices_dt,
1845 &s_fdt);
1846
62083979
MR
1847 ret = spapr_drc_populate_dt(fdt, bus_off, OBJECT(phb),
1848 SPAPR_DR_CONNECTOR_TYPE_PCI);
1849 if (ret) {
1850 return ret;
1851 }
1852
3384f95c
DG
1853 return 0;
1854}
298a9710 1855
fa28f71b
AK
1856void spapr_pci_rtas_init(void)
1857{
3a3b8502
AK
1858 spapr_rtas_register(RTAS_READ_PCI_CONFIG, "read-pci-config",
1859 rtas_read_pci_config);
1860 spapr_rtas_register(RTAS_WRITE_PCI_CONFIG, "write-pci-config",
1861 rtas_write_pci_config);
1862 spapr_rtas_register(RTAS_IBM_READ_PCI_CONFIG, "ibm,read-pci-config",
1863 rtas_ibm_read_pci_config);
1864 spapr_rtas_register(RTAS_IBM_WRITE_PCI_CONFIG, "ibm,write-pci-config",
1865 rtas_ibm_write_pci_config);
226419d6 1866 if (msi_nonbroken) {
3a3b8502
AK
1867 spapr_rtas_register(RTAS_IBM_QUERY_INTERRUPT_SOURCE_NUMBER,
1868 "ibm,query-interrupt-source-number",
0ee2c058 1869 rtas_ibm_query_interrupt_source_number);
3a3b8502
AK
1870 spapr_rtas_register(RTAS_IBM_CHANGE_MSI, "ibm,change-msi",
1871 rtas_ibm_change_msi);
0ee2c058 1872 }
ee954280
GS
1873
1874 spapr_rtas_register(RTAS_IBM_SET_EEH_OPTION,
1875 "ibm,set-eeh-option",
1876 rtas_ibm_set_eeh_option);
1877 spapr_rtas_register(RTAS_IBM_GET_CONFIG_ADDR_INFO2,
1878 "ibm,get-config-addr-info2",
1879 rtas_ibm_get_config_addr_info2);
1880 spapr_rtas_register(RTAS_IBM_READ_SLOT_RESET_STATE2,
1881 "ibm,read-slot-reset-state2",
1882 rtas_ibm_read_slot_reset_state2);
1883 spapr_rtas_register(RTAS_IBM_SET_SLOT_RESET,
1884 "ibm,set-slot-reset",
1885 rtas_ibm_set_slot_reset);
1886 spapr_rtas_register(RTAS_IBM_CONFIGURE_PE,
1887 "ibm,configure-pe",
1888 rtas_ibm_configure_pe);
1889 spapr_rtas_register(RTAS_IBM_SLOT_ERROR_DETAIL,
1890 "ibm,slot-error-detail",
1891 rtas_ibm_slot_error_detail);
fa28f71b
AK
1892}
1893
8c9f64df 1894static void spapr_pci_register_types(void)
298a9710
DG
1895{
1896 type_register_static(&spapr_phb_info);
1897}
8c9f64df
AF
1898
1899type_init(spapr_pci_register_types)
eefaccc0
DG
1900
1901static int spapr_switch_one_vga(DeviceState *dev, void *opaque)
1902{
1903 bool be = *(bool *)opaque;
1904
1905 if (object_dynamic_cast(OBJECT(dev), "VGA")
1906 || object_dynamic_cast(OBJECT(dev), "secondary-vga")) {
1907 object_property_set_bool(OBJECT(dev), be, "big-endian-framebuffer",
1908 &error_abort);
1909 }
1910 return 0;
1911}
1912
1913void spapr_pci_switch_vga(bool big_endian)
1914{
28e02042 1915 sPAPRMachineState *spapr = SPAPR_MACHINE(qdev_get_machine());
eefaccc0
DG
1916 sPAPRPHBState *sphb;
1917
1918 /*
1919 * For backward compatibility with existing guests, we switch
1920 * the endianness of the VGA controller when changing the guest
1921 * interrupt mode
1922 */
1923 QLIST_FOREACH(sphb, &spapr->phbs, list) {
1924 BusState *bus = &PCI_HOST_BRIDGE(sphb)->bus->qbus;
1925 qbus_walk_children(bus, spapr_switch_one_vga, NULL, NULL, NULL,
1926 &big_endian);
1927 }
1928}