]> git.proxmox.com Git - mirror_qemu.git/blame - hw/ppc/spapr_pci.c
spapr: Generate FDT fragment for CPUs at configure connector time
[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"
da34e65c 26#include "qapi/error.h"
4771d756
PB
27#include "qemu-common.h"
28#include "cpu.h"
83c9f4ca 29#include "hw/hw.h"
1d2d9742 30#include "hw/sysbus.h"
83c9f4ca
PB
31#include "hw/pci/pci.h"
32#include "hw/pci/msi.h"
33#include "hw/pci/msix.h"
34#include "hw/pci/pci_host.h"
0d09e41a
PB
35#include "hw/ppc/spapr.h"
36#include "hw/pci-host/spapr.h"
022c62cb 37#include "exec/address-spaces.h"
ae4de14c 38#include "exec/ram_addr.h"
3384f95c 39#include <libfdt.h>
a2950fb6 40#include "trace.h"
295d51aa 41#include "qemu/error-report.h"
7454c7af 42#include "qapi/qmp/qerror.h"
99372e78 43#include "hw/ppc/fdt.h"
1d2d9742 44#include "hw/pci/pci_bridge.h"
06aac7bd 45#include "hw/pci/pci_bus.h"
2530a1a5 46#include "hw/pci/pci_ids.h"
62083979 47#include "hw/ppc/spapr_drc.h"
7454c7af 48#include "sysemu/device_tree.h"
77ac58dd 49#include "sysemu/kvm.h"
ae4de14c 50#include "sysemu/hostmem.h"
4814401f 51#include "sysemu/numa.h"
3384f95c 52
0ee2c058
AK
53/* Copied from the kernel arch/powerpc/platforms/pseries/msi.c */
54#define RTAS_QUERY_FN 0
55#define RTAS_CHANGE_FN 1
56#define RTAS_RESET_FN 2
57#define RTAS_CHANGE_MSI_FN 3
58#define RTAS_CHANGE_MSIX_FN 4
59
60/* Interrupt types to return on RTAS_CHANGE_* */
61#define RTAS_TYPE_MSI 1
62#define RTAS_TYPE_MSIX 2
63
28e02042 64sPAPRPHBState *spapr_pci_find_phb(sPAPRMachineState *spapr, uint64_t buid)
3384f95c 65{
8c9f64df 66 sPAPRPHBState *sphb;
3384f95c 67
8c9f64df
AF
68 QLIST_FOREACH(sphb, &spapr->phbs, list) {
69 if (sphb->buid != buid) {
3384f95c
DG
70 continue;
71 }
8c9f64df 72 return sphb;
9894c5d4
AK
73 }
74
75 return NULL;
76}
77
28e02042 78PCIDevice *spapr_pci_find_dev(sPAPRMachineState *spapr, uint64_t buid,
46c5874e 79 uint32_t config_addr)
9894c5d4 80{
46c5874e 81 sPAPRPHBState *sphb = spapr_pci_find_phb(spapr, buid);
8558d942 82 PCIHostState *phb = PCI_HOST_BRIDGE(sphb);
5dac82ce 83 int bus_num = (config_addr >> 16) & 0xFF;
9894c5d4
AK
84 int devfn = (config_addr >> 8) & 0xFF;
85
86 if (!phb) {
87 return NULL;
88 }
3384f95c 89
5dac82ce 90 return pci_find_device(phb->bus, bus_num, devfn);
3384f95c
DG
91}
92
3f7565c9
BH
93static uint32_t rtas_pci_cfgaddr(uint32_t arg)
94{
92615a5a 95 /* This handles the encoding of extended config space addresses */
3f7565c9
BH
96 return ((arg >> 20) & 0xf00) | (arg & 0xff);
97}
98
28e02042 99static void finish_read_pci_config(sPAPRMachineState *spapr, uint64_t buid,
92615a5a
DG
100 uint32_t addr, uint32_t size,
101 target_ulong rets)
88045ac5 102{
92615a5a
DG
103 PCIDevice *pci_dev;
104 uint32_t val;
105
106 if ((size != 1) && (size != 2) && (size != 4)) {
107 /* access must be 1, 2 or 4 bytes */
a64d325d 108 rtas_st(rets, 0, RTAS_OUT_HW_ERROR);
92615a5a 109 return;
88045ac5 110 }
88045ac5 111
46c5874e 112 pci_dev = spapr_pci_find_dev(spapr, buid, addr);
92615a5a
DG
113 addr = rtas_pci_cfgaddr(addr);
114
115 if (!pci_dev || (addr % size) || (addr >= pci_config_size(pci_dev))) {
116 /* Access must be to a valid device, within bounds and
117 * naturally aligned */
a64d325d 118 rtas_st(rets, 0, RTAS_OUT_HW_ERROR);
92615a5a 119 return;
88045ac5 120 }
92615a5a
DG
121
122 val = pci_host_config_read_common(pci_dev, addr,
123 pci_config_size(pci_dev), size);
124
a64d325d 125 rtas_st(rets, 0, RTAS_OUT_SUCCESS);
92615a5a 126 rtas_st(rets, 1, val);
88045ac5
AG
127}
128
28e02042 129static void rtas_ibm_read_pci_config(PowerPCCPU *cpu, sPAPRMachineState *spapr,
3384f95c
DG
130 uint32_t token, uint32_t nargs,
131 target_ulong args,
132 uint32_t nret, target_ulong rets)
133{
92615a5a
DG
134 uint64_t buid;
135 uint32_t size, addr;
3384f95c 136
92615a5a 137 if ((nargs != 4) || (nret != 2)) {
a64d325d 138 rtas_st(rets, 0, RTAS_OUT_HW_ERROR);
3384f95c
DG
139 return;
140 }
92615a5a 141
a14aa92b 142 buid = rtas_ldq(args, 1);
3384f95c 143 size = rtas_ld(args, 3);
92615a5a
DG
144 addr = rtas_ld(args, 0);
145
146 finish_read_pci_config(spapr, buid, addr, size, rets);
3384f95c
DG
147}
148
28e02042 149static void rtas_read_pci_config(PowerPCCPU *cpu, sPAPRMachineState *spapr,
3384f95c
DG
150 uint32_t token, uint32_t nargs,
151 target_ulong args,
152 uint32_t nret, target_ulong rets)
153{
92615a5a 154 uint32_t size, addr;
3384f95c 155
92615a5a 156 if ((nargs != 2) || (nret != 2)) {
a64d325d 157 rtas_st(rets, 0, RTAS_OUT_HW_ERROR);
3384f95c
DG
158 return;
159 }
92615a5a 160
3384f95c 161 size = rtas_ld(args, 1);
92615a5a
DG
162 addr = rtas_ld(args, 0);
163
164 finish_read_pci_config(spapr, 0, addr, size, rets);
165}
166
28e02042 167static void finish_write_pci_config(sPAPRMachineState *spapr, uint64_t buid,
92615a5a
DG
168 uint32_t addr, uint32_t size,
169 uint32_t val, target_ulong rets)
170{
171 PCIDevice *pci_dev;
172
173 if ((size != 1) && (size != 2) && (size != 4)) {
174 /* access must be 1, 2 or 4 bytes */
a64d325d 175 rtas_st(rets, 0, RTAS_OUT_HW_ERROR);
92615a5a
DG
176 return;
177 }
178
46c5874e 179 pci_dev = spapr_pci_find_dev(spapr, buid, addr);
92615a5a
DG
180 addr = rtas_pci_cfgaddr(addr);
181
182 if (!pci_dev || (addr % size) || (addr >= pci_config_size(pci_dev))) {
183 /* Access must be to a valid device, within bounds and
184 * naturally aligned */
a64d325d 185 rtas_st(rets, 0, RTAS_OUT_HW_ERROR);
92615a5a
DG
186 return;
187 }
188
189 pci_host_config_write_common(pci_dev, addr, pci_config_size(pci_dev),
190 val, size);
191
a64d325d 192 rtas_st(rets, 0, RTAS_OUT_SUCCESS);
3384f95c
DG
193}
194
28e02042 195static void rtas_ibm_write_pci_config(PowerPCCPU *cpu, sPAPRMachineState *spapr,
3384f95c
DG
196 uint32_t token, uint32_t nargs,
197 target_ulong args,
198 uint32_t nret, target_ulong rets)
199{
92615a5a 200 uint64_t buid;
3384f95c 201 uint32_t val, size, addr;
3384f95c 202
92615a5a 203 if ((nargs != 5) || (nret != 1)) {
a64d325d 204 rtas_st(rets, 0, RTAS_OUT_HW_ERROR);
3384f95c
DG
205 return;
206 }
92615a5a 207
a14aa92b 208 buid = rtas_ldq(args, 1);
3384f95c
DG
209 val = rtas_ld(args, 4);
210 size = rtas_ld(args, 3);
92615a5a
DG
211 addr = rtas_ld(args, 0);
212
213 finish_write_pci_config(spapr, buid, addr, size, val, rets);
3384f95c
DG
214}
215
28e02042 216static void rtas_write_pci_config(PowerPCCPU *cpu, sPAPRMachineState *spapr,
3384f95c
DG
217 uint32_t token, uint32_t nargs,
218 target_ulong args,
219 uint32_t nret, target_ulong rets)
220{
221 uint32_t val, size, addr;
3384f95c 222
92615a5a 223 if ((nargs != 3) || (nret != 1)) {
a64d325d 224 rtas_st(rets, 0, RTAS_OUT_HW_ERROR);
3384f95c
DG
225 return;
226 }
92615a5a
DG
227
228
3384f95c
DG
229 val = rtas_ld(args, 2);
230 size = rtas_ld(args, 1);
92615a5a
DG
231 addr = rtas_ld(args, 0);
232
233 finish_write_pci_config(spapr, 0, addr, size, val, rets);
3384f95c
DG
234}
235
0ee2c058
AK
236/*
237 * Set MSI/MSIX message data.
238 * This is required for msi_notify()/msix_notify() which
239 * will write at the addresses via spapr_msi_write().
9a321e92
AK
240 *
241 * If hwaddr == 0, all entries will have .data == first_irq i.e.
242 * table will be reset.
0ee2c058 243 */
f1c2dc7c
AK
244static void spapr_msi_setmsg(PCIDevice *pdev, hwaddr addr, bool msix,
245 unsigned first_irq, unsigned req_num)
0ee2c058
AK
246{
247 unsigned i;
f1c2dc7c 248 MSIMessage msg = { .address = addr, .data = first_irq };
0ee2c058
AK
249
250 if (!msix) {
251 msi_set_message(pdev, msg);
252 trace_spapr_pci_msi_setup(pdev->name, 0, msg.address);
253 return;
254 }
255
9a321e92 256 for (i = 0; i < req_num; ++i) {
0ee2c058
AK
257 msix_set_message(pdev, i, msg);
258 trace_spapr_pci_msi_setup(pdev->name, i, msg.address);
9a321e92
AK
259 if (addr) {
260 ++msg.data;
261 }
0ee2c058
AK
262 }
263}
264
28e02042 265static void rtas_ibm_change_msi(PowerPCCPU *cpu, sPAPRMachineState *spapr,
0ee2c058
AK
266 uint32_t token, uint32_t nargs,
267 target_ulong args, uint32_t nret,
268 target_ulong rets)
269{
2c88b098 270 sPAPRMachineClass *smc = SPAPR_MACHINE_GET_CLASS(spapr);
0ee2c058 271 uint32_t config_addr = rtas_ld(args, 0);
a14aa92b 272 uint64_t buid = rtas_ldq(args, 1);
0ee2c058
AK
273 unsigned int func = rtas_ld(args, 3);
274 unsigned int req_num = rtas_ld(args, 4); /* 0 == remove all */
275 unsigned int seq_num = rtas_ld(args, 5);
276 unsigned int ret_intr_type;
d4a63ac8 277 unsigned int irq, max_irqs = 0;
0ee2c058
AK
278 sPAPRPHBState *phb = NULL;
279 PCIDevice *pdev = NULL;
9a321e92
AK
280 spapr_pci_msi *msi;
281 int *config_addr_key;
a005b3ef 282 Error *err = NULL;
4fe75a8c 283 int i;
0ee2c058 284
9cbe305b
GK
285 /* Fins sPAPRPHBState */
286 phb = spapr_pci_find_phb(spapr, buid);
287 if (phb) {
288 pdev = spapr_pci_find_dev(spapr, buid, config_addr);
289 }
290 if (!phb || !pdev) {
291 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
292 return;
293 }
294
0ee2c058 295 switch (func) {
0ee2c058 296 case RTAS_CHANGE_FN:
9cbe305b
GK
297 if (msi_present(pdev)) {
298 ret_intr_type = RTAS_TYPE_MSI;
299 } else if (msix_present(pdev)) {
300 ret_intr_type = RTAS_TYPE_MSIX;
301 } else {
302 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
303 return;
304 }
305 break;
306 case RTAS_CHANGE_MSI_FN:
307 if (msi_present(pdev)) {
308 ret_intr_type = RTAS_TYPE_MSI;
309 } else {
310 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
311 return;
312 }
0ee2c058
AK
313 break;
314 case RTAS_CHANGE_MSIX_FN:
9cbe305b
GK
315 if (msix_present(pdev)) {
316 ret_intr_type = RTAS_TYPE_MSIX;
317 } else {
318 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
319 return;
320 }
0ee2c058
AK
321 break;
322 default:
295d51aa 323 error_report("rtas_ibm_change_msi(%u) is not implemented", func);
a64d325d 324 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
0ee2c058
AK
325 return;
326 }
327
ce266b75
GK
328 msi = (spapr_pci_msi *) g_hash_table_lookup(phb->msi, &config_addr);
329
0ee2c058
AK
330 /* Releasing MSIs */
331 if (!req_num) {
9a321e92
AK
332 if (!msi) {
333 trace_spapr_pci_msi("Releasing wrong config", config_addr);
a64d325d 334 rtas_st(rets, 0, RTAS_OUT_HW_ERROR);
0ee2c058
AK
335 return;
336 }
9a321e92 337
2c88b098 338 if (!smc->legacy_irq_allocation) {
82cffa2e
CLG
339 spapr_irq_msi_free(spapr, msi->first_irq, msi->num);
340 }
60c6823b 341 spapr_irq_free(spapr, msi->first_irq, msi->num);
32420522 342 if (msi_present(pdev)) {
d4a63ac8 343 spapr_msi_setmsg(pdev, 0, false, 0, 0);
32420522
AK
344 }
345 if (msix_present(pdev)) {
d4a63ac8 346 spapr_msi_setmsg(pdev, 0, true, 0, 0);
32420522 347 }
9a321e92
AK
348 g_hash_table_remove(phb->msi, &config_addr);
349
350 trace_spapr_pci_msi("Released MSIs", config_addr);
a64d325d 351 rtas_st(rets, 0, RTAS_OUT_SUCCESS);
0ee2c058
AK
352 rtas_st(rets, 1, 0);
353 return;
354 }
355
356 /* Enabling MSI */
357
28668b5f
AK
358 /* Check if the device supports as many IRQs as requested */
359 if (ret_intr_type == RTAS_TYPE_MSI) {
360 max_irqs = msi_nr_vectors_allocated(pdev);
361 } else if (ret_intr_type == RTAS_TYPE_MSIX) {
362 max_irqs = pdev->msix_entries_nr;
363 }
364 if (!max_irqs) {
9a321e92
AK
365 error_report("Requested interrupt type %d is not enabled for device %x",
366 ret_intr_type, config_addr);
28668b5f
AK
367 rtas_st(rets, 0, -1); /* Hardware error */
368 return;
369 }
370 /* Correct the number if the guest asked for too many */
371 if (req_num > max_irqs) {
9a321e92 372 trace_spapr_pci_msi_retry(config_addr, req_num, max_irqs);
28668b5f 373 req_num = max_irqs;
9a321e92
AK
374 irq = 0; /* to avoid misleading trace */
375 goto out;
28668b5f
AK
376 }
377
9a321e92 378 /* Allocate MSIs */
2c88b098 379 if (smc->legacy_irq_allocation) {
82cffa2e
CLG
380 irq = spapr_irq_find(spapr, req_num, ret_intr_type == RTAS_TYPE_MSI,
381 &err);
382 } else {
383 irq = spapr_irq_msi_alloc(spapr, req_num,
384 ret_intr_type == RTAS_TYPE_MSI, &err);
385 }
a005b3ef
GK
386 if (err) {
387 error_reportf_err(err, "Can't allocate MSIs for device %x: ",
388 config_addr);
a64d325d 389 rtas_st(rets, 0, RTAS_OUT_HW_ERROR);
0ee2c058
AK
390 return;
391 }
392
4fe75a8c
CLG
393 for (i = 0; i < req_num; i++) {
394 spapr_irq_claim(spapr, irq + i, false, &err);
395 if (err) {
925969c3
GK
396 if (i) {
397 spapr_irq_free(spapr, irq, i);
398 }
399 if (!smc->legacy_irq_allocation) {
400 spapr_irq_msi_free(spapr, irq, req_num);
401 }
4fe75a8c
CLG
402 error_reportf_err(err, "Can't allocate MSIs for device %x: ",
403 config_addr);
404 rtas_st(rets, 0, RTAS_OUT_HW_ERROR);
405 return;
406 }
407 }
408
ce266b75
GK
409 /* Release previous MSIs */
410 if (msi) {
2c88b098 411 if (!smc->legacy_irq_allocation) {
82cffa2e
CLG
412 spapr_irq_msi_free(spapr, msi->first_irq, msi->num);
413 }
60c6823b 414 spapr_irq_free(spapr, msi->first_irq, msi->num);
ce266b75
GK
415 g_hash_table_remove(phb->msi, &config_addr);
416 }
417
0ee2c058 418 /* Setup MSI/MSIX vectors in the device (via cfgspace or MSIX BAR) */
8c46f7ec 419 spapr_msi_setmsg(pdev, SPAPR_PCI_MSI_WINDOW, ret_intr_type == RTAS_TYPE_MSIX,
9a321e92 420 irq, req_num);
0ee2c058 421
9a321e92
AK
422 /* Add MSI device to cache */
423 msi = g_new(spapr_pci_msi, 1);
424 msi->first_irq = irq;
425 msi->num = req_num;
426 config_addr_key = g_new(int, 1);
427 *config_addr_key = config_addr;
428 g_hash_table_insert(phb->msi, config_addr_key, msi);
429
430out:
a64d325d 431 rtas_st(rets, 0, RTAS_OUT_SUCCESS);
0ee2c058
AK
432 rtas_st(rets, 1, req_num);
433 rtas_st(rets, 2, ++seq_num);
b359bd6a
SB
434 if (nret > 3) {
435 rtas_st(rets, 3, ret_intr_type);
436 }
0ee2c058 437
9a321e92 438 trace_spapr_pci_rtas_ibm_change_msi(config_addr, func, req_num, irq);
0ee2c058
AK
439}
440
210b580b 441static void rtas_ibm_query_interrupt_source_number(PowerPCCPU *cpu,
28e02042 442 sPAPRMachineState *spapr,
0ee2c058
AK
443 uint32_t token,
444 uint32_t nargs,
445 target_ulong args,
446 uint32_t nret,
447 target_ulong rets)
448{
449 uint32_t config_addr = rtas_ld(args, 0);
a14aa92b 450 uint64_t buid = rtas_ldq(args, 1);
0ee2c058 451 unsigned int intr_src_num = -1, ioa_intr_num = rtas_ld(args, 3);
0ee2c058 452 sPAPRPHBState *phb = NULL;
9a321e92
AK
453 PCIDevice *pdev = NULL;
454 spapr_pci_msi *msi;
0ee2c058 455
9a321e92 456 /* Find sPAPRPHBState */
46c5874e 457 phb = spapr_pci_find_phb(spapr, buid);
9a321e92 458 if (phb) {
46c5874e 459 pdev = spapr_pci_find_dev(spapr, buid, config_addr);
9a321e92
AK
460 }
461 if (!phb || !pdev) {
a64d325d 462 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
0ee2c058
AK
463 return;
464 }
465
466 /* Find device descriptor and start IRQ */
9a321e92
AK
467 msi = (spapr_pci_msi *) g_hash_table_lookup(phb->msi, &config_addr);
468 if (!msi || !msi->first_irq || !msi->num || (ioa_intr_num >= msi->num)) {
469 trace_spapr_pci_msi("Failed to return vector", config_addr);
a64d325d 470 rtas_st(rets, 0, RTAS_OUT_HW_ERROR);
0ee2c058
AK
471 return;
472 }
9a321e92 473 intr_src_num = msi->first_irq + ioa_intr_num;
0ee2c058
AK
474 trace_spapr_pci_rtas_ibm_query_interrupt_source_number(ioa_intr_num,
475 intr_src_num);
476
a64d325d 477 rtas_st(rets, 0, RTAS_OUT_SUCCESS);
0ee2c058
AK
478 rtas_st(rets, 1, intr_src_num);
479 rtas_st(rets, 2, 1);/* 0 == level; 1 == edge */
480}
481
ee954280 482static void rtas_ibm_set_eeh_option(PowerPCCPU *cpu,
28e02042 483 sPAPRMachineState *spapr,
ee954280
GS
484 uint32_t token, uint32_t nargs,
485 target_ulong args, uint32_t nret,
486 target_ulong rets)
487{
488 sPAPRPHBState *sphb;
ee954280
GS
489 uint32_t addr, option;
490 uint64_t buid;
491 int ret;
492
493 if ((nargs != 4) || (nret != 1)) {
494 goto param_error_exit;
495 }
496
a14aa92b 497 buid = rtas_ldq(args, 1);
ee954280
GS
498 addr = rtas_ld(args, 0);
499 option = rtas_ld(args, 3);
500
46c5874e 501 sphb = spapr_pci_find_phb(spapr, buid);
ee954280
GS
502 if (!sphb) {
503 goto param_error_exit;
504 }
505
fbb4e983 506 if (!spapr_phb_eeh_available(sphb)) {
ee954280
GS
507 goto param_error_exit;
508 }
509
fbb4e983 510 ret = spapr_phb_vfio_eeh_set_option(sphb, addr, option);
ee954280
GS
511 rtas_st(rets, 0, ret);
512 return;
513
514param_error_exit:
515 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
516}
517
518static void rtas_ibm_get_config_addr_info2(PowerPCCPU *cpu,
28e02042 519 sPAPRMachineState *spapr,
ee954280
GS
520 uint32_t token, uint32_t nargs,
521 target_ulong args, uint32_t nret,
522 target_ulong rets)
523{
524 sPAPRPHBState *sphb;
ee954280
GS
525 PCIDevice *pdev;
526 uint32_t addr, option;
527 uint64_t buid;
528
529 if ((nargs != 4) || (nret != 2)) {
530 goto param_error_exit;
531 }
532
a14aa92b 533 buid = rtas_ldq(args, 1);
46c5874e 534 sphb = spapr_pci_find_phb(spapr, buid);
ee954280
GS
535 if (!sphb) {
536 goto param_error_exit;
537 }
538
fbb4e983 539 if (!spapr_phb_eeh_available(sphb)) {
ee954280
GS
540 goto param_error_exit;
541 }
542
543 /*
544 * We always have PE address of form "00BB0001". "BB"
545 * represents the bus number of PE's primary bus.
546 */
547 option = rtas_ld(args, 3);
548 switch (option) {
549 case RTAS_GET_PE_ADDR:
550 addr = rtas_ld(args, 0);
46c5874e 551 pdev = spapr_pci_find_dev(spapr, buid, addr);
ee954280
GS
552 if (!pdev) {
553 goto param_error_exit;
554 }
555
fd56e061 556 rtas_st(rets, 1, (pci_bus_num(pci_get_bus(pdev)) << 16) + 1);
ee954280
GS
557 break;
558 case RTAS_GET_PE_MODE:
559 rtas_st(rets, 1, RTAS_PE_MODE_SHARED);
560 break;
561 default:
562 goto param_error_exit;
563 }
564
565 rtas_st(rets, 0, RTAS_OUT_SUCCESS);
566 return;
567
568param_error_exit:
569 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
570}
571
572static void rtas_ibm_read_slot_reset_state2(PowerPCCPU *cpu,
28e02042 573 sPAPRMachineState *spapr,
ee954280
GS
574 uint32_t token, uint32_t nargs,
575 target_ulong args, uint32_t nret,
576 target_ulong rets)
577{
578 sPAPRPHBState *sphb;
ee954280
GS
579 uint64_t buid;
580 int state, ret;
581
582 if ((nargs != 3) || (nret != 4 && nret != 5)) {
583 goto param_error_exit;
584 }
585
a14aa92b 586 buid = rtas_ldq(args, 1);
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_get_state(sphb, &state);
ee954280
GS
597 rtas_st(rets, 0, ret);
598 if (ret != RTAS_OUT_SUCCESS) {
599 return;
600 }
601
602 rtas_st(rets, 1, state);
603 rtas_st(rets, 2, RTAS_EEH_SUPPORT);
604 rtas_st(rets, 3, RTAS_EEH_PE_UNAVAIL_INFO);
605 if (nret >= 5) {
606 rtas_st(rets, 4, RTAS_EEH_PE_RECOVER_INFO);
607 }
608 return;
609
610param_error_exit:
611 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
612}
613
614static void rtas_ibm_set_slot_reset(PowerPCCPU *cpu,
28e02042 615 sPAPRMachineState *spapr,
ee954280
GS
616 uint32_t token, uint32_t nargs,
617 target_ulong args, uint32_t nret,
618 target_ulong rets)
619{
620 sPAPRPHBState *sphb;
ee954280
GS
621 uint32_t option;
622 uint64_t buid;
623 int ret;
624
625 if ((nargs != 4) || (nret != 1)) {
626 goto param_error_exit;
627 }
628
a14aa92b 629 buid = rtas_ldq(args, 1);
ee954280 630 option = rtas_ld(args, 3);
46c5874e 631 sphb = spapr_pci_find_phb(spapr, buid);
ee954280
GS
632 if (!sphb) {
633 goto param_error_exit;
634 }
635
fbb4e983 636 if (!spapr_phb_eeh_available(sphb)) {
ee954280
GS
637 goto param_error_exit;
638 }
639
fbb4e983 640 ret = spapr_phb_vfio_eeh_reset(sphb, option);
ee954280
GS
641 rtas_st(rets, 0, ret);
642 return;
643
644param_error_exit:
645 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
646}
647
648static void rtas_ibm_configure_pe(PowerPCCPU *cpu,
28e02042 649 sPAPRMachineState *spapr,
ee954280
GS
650 uint32_t token, uint32_t nargs,
651 target_ulong args, uint32_t nret,
652 target_ulong rets)
653{
654 sPAPRPHBState *sphb;
ee954280
GS
655 uint64_t buid;
656 int ret;
657
658 if ((nargs != 3) || (nret != 1)) {
659 goto param_error_exit;
660 }
661
a14aa92b 662 buid = rtas_ldq(args, 1);
46c5874e 663 sphb = spapr_pci_find_phb(spapr, buid);
ee954280
GS
664 if (!sphb) {
665 goto param_error_exit;
666 }
667
fbb4e983 668 if (!spapr_phb_eeh_available(sphb)) {
ee954280
GS
669 goto param_error_exit;
670 }
671
fbb4e983 672 ret = spapr_phb_vfio_eeh_configure(sphb);
ee954280
GS
673 rtas_st(rets, 0, ret);
674 return;
675
676param_error_exit:
677 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
678}
679
680/* To support it later */
681static void rtas_ibm_slot_error_detail(PowerPCCPU *cpu,
28e02042 682 sPAPRMachineState *spapr,
ee954280
GS
683 uint32_t token, uint32_t nargs,
684 target_ulong args, uint32_t nret,
685 target_ulong rets)
686{
687 sPAPRPHBState *sphb;
ee954280
GS
688 int option;
689 uint64_t buid;
690
691 if ((nargs != 8) || (nret != 1)) {
692 goto param_error_exit;
693 }
694
a14aa92b 695 buid = rtas_ldq(args, 1);
46c5874e 696 sphb = spapr_pci_find_phb(spapr, buid);
ee954280
GS
697 if (!sphb) {
698 goto param_error_exit;
699 }
700
fbb4e983 701 if (!spapr_phb_eeh_available(sphb)) {
ee954280
GS
702 goto param_error_exit;
703 }
704
705 option = rtas_ld(args, 7);
706 switch (option) {
707 case RTAS_SLOT_TEMP_ERR_LOG:
708 case RTAS_SLOT_PERM_ERR_LOG:
709 break;
710 default:
711 goto param_error_exit;
712 }
713
714 /* We don't have error log yet */
715 rtas_st(rets, 0, RTAS_OUT_NO_ERRORS_FOUND);
716 return;
717
718param_error_exit:
719 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
720}
721
7fb0bd34
DG
722static int pci_spapr_swizzle(int slot, int pin)
723{
724 return (slot + pin) % PCI_NUM_PINS;
725}
726
3384f95c
DG
727static int pci_spapr_map_irq(PCIDevice *pci_dev, int irq_num)
728{
729 /*
730 * Here we need to convert pci_dev + irq_num to some unique value
7fb0bd34
DG
731 * which is less than number of IRQs on the specific bus (4). We
732 * use standard PCI swizzling, that is (slot number + pin number)
733 * % 4.
3384f95c 734 */
7fb0bd34 735 return pci_spapr_swizzle(PCI_SLOT(pci_dev->devfn), irq_num);
3384f95c
DG
736}
737
738static void pci_spapr_set_irq(void *opaque, int irq_num, int level)
739{
740 /*
741 * Here we use the number returned by pci_spapr_map_irq to find a
742 * corresponding qemu_irq.
743 */
744 sPAPRPHBState *phb = opaque;
745
caae58cb 746 trace_spapr_pci_lsi_set(phb->dtbusname, irq_num, phb->lsi_table[irq_num].irq);
a307d594 747 qemu_set_irq(spapr_phb_lsi_qirq(phb, irq_num), level);
3384f95c
DG
748}
749
5cc7a967
AK
750static PCIINTxRoute spapr_route_intx_pin_to_irq(void *opaque, int pin)
751{
752 sPAPRPHBState *sphb = SPAPR_PCI_HOST_BRIDGE(opaque);
753 PCIINTxRoute route;
754
755 route.mode = PCI_INTX_ENABLED;
756 route.irq = sphb->lsi_table[pin].irq;
757
758 return route;
759}
760
0ee2c058
AK
761/*
762 * MSI/MSIX memory region implementation.
763 * The handler handles both MSI and MSIX.
18f2330e 764 * The vector number is encoded in least bits in data.
0ee2c058 765 */
a8170e5e 766static void spapr_msi_write(void *opaque, hwaddr addr,
0ee2c058
AK
767 uint64_t data, unsigned size)
768{
28e02042 769 sPAPRMachineState *spapr = SPAPR_MACHINE(qdev_get_machine());
f1c2dc7c 770 uint32_t irq = data;
0ee2c058
AK
771
772 trace_spapr_pci_msi_write(addr, data, irq);
773
77183755 774 qemu_irq_pulse(spapr_qirq(spapr, irq));
0ee2c058
AK
775}
776
777static const MemoryRegionOps spapr_msi_ops = {
778 /* There is no .read as the read result is undefined by PCI spec */
779 .read = NULL,
780 .write = spapr_msi_write,
781 .endianness = DEVICE_LITTLE_ENDIAN
782};
783
298a9710
DG
784/*
785 * PHB PCI device
786 */
e00387d5 787static AddressSpace *spapr_pci_dma_iommu(PCIBus *bus, void *opaque, int devfn)
edded454
DG
788{
789 sPAPRPHBState *phb = opaque;
790
e00387d5 791 return &phb->iommu_as;
edded454
DG
792}
793
16b0ea1d
ND
794static char *spapr_phb_vfio_get_loc_code(sPAPRPHBState *sphb, PCIDevice *pdev)
795{
796 char *path = NULL, *buf = NULL, *host = NULL;
797
798 /* Get the PCI VFIO host id */
799 host = object_property_get_str(OBJECT(pdev), "host", NULL);
800 if (!host) {
801 goto err_out;
802 }
803
804 /* Construct the path of the file that will give us the DT location */
805 path = g_strdup_printf("/sys/bus/pci/devices/%s/devspec", host);
806 g_free(host);
8f687605 807 if (!g_file_get_contents(path, &buf, NULL, NULL)) {
16b0ea1d
ND
808 goto err_out;
809 }
810 g_free(path);
811
812 /* Construct and read from host device tree the loc-code */
813 path = g_strdup_printf("/proc/device-tree%s/ibm,loc-code", buf);
814 g_free(buf);
8f687605 815 if (!g_file_get_contents(path, &buf, NULL, NULL)) {
16b0ea1d
ND
816 goto err_out;
817 }
818 return buf;
819
820err_out:
821 g_free(path);
822 return NULL;
823}
824
825static char *spapr_phb_get_loc_code(sPAPRPHBState *sphb, PCIDevice *pdev)
826{
827 char *buf;
828 const char *devtype = "qemu";
829 uint32_t busnr = pci_bus_num(PCI_BUS(qdev_get_parent_bus(DEVICE(pdev))));
830
831 if (object_dynamic_cast(OBJECT(pdev), "vfio-pci")) {
832 buf = spapr_phb_vfio_get_loc_code(sphb, pdev);
833 if (buf) {
834 return buf;
835 }
836 devtype = "vfio";
837 }
838 /*
839 * For emulated devices and VFIO-failure case, make up
840 * the loc-code.
841 */
842 buf = g_strdup_printf("%s_%s:%04x:%02x:%02x.%x",
843 devtype, pdev->name, sphb->index, busnr,
844 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
845 return buf;
846}
847
7454c7af
MR
848/* Macros to operate with address in OF binding to PCI */
849#define b_x(x, p, l) (((x) & ((1<<(l))-1)) << (p))
850#define b_n(x) b_x((x), 31, 1) /* 0 if relocatable */
851#define b_p(x) b_x((x), 30, 1) /* 1 if prefetchable */
852#define b_t(x) b_x((x), 29, 1) /* 1 if the address is aliased */
853#define b_ss(x) b_x((x), 24, 2) /* the space code */
854#define b_bbbbbbbb(x) b_x((x), 16, 8) /* bus number */
855#define b_ddddd(x) b_x((x), 11, 5) /* device number */
856#define b_fff(x) b_x((x), 8, 3) /* function number */
857#define b_rrrrrrrr(x) b_x((x), 0, 8) /* register number */
858
859/* for 'reg'/'assigned-addresses' OF properties */
860#define RESOURCE_CELLS_SIZE 2
861#define RESOURCE_CELLS_ADDRESS 3
862
863typedef struct ResourceFields {
864 uint32_t phys_hi;
865 uint32_t phys_mid;
866 uint32_t phys_lo;
867 uint32_t size_hi;
868 uint32_t size_lo;
869} QEMU_PACKED ResourceFields;
870
871typedef struct ResourceProps {
872 ResourceFields reg[8];
873 ResourceFields assigned[7];
874 uint32_t reg_len;
875 uint32_t assigned_len;
876} ResourceProps;
877
878/* fill in the 'reg'/'assigned-resources' OF properties for
879 * a PCI device. 'reg' describes resource requirements for a
880 * device's IO/MEM regions, 'assigned-addresses' describes the
881 * actual resource assignments.
882 *
883 * the properties are arrays of ('phys-addr', 'size') pairs describing
884 * the addressable regions of the PCI device, where 'phys-addr' is a
885 * RESOURCE_CELLS_ADDRESS-tuple of 32-bit integers corresponding to
886 * (phys.hi, phys.mid, phys.lo), and 'size' is a
887 * RESOURCE_CELLS_SIZE-tuple corresponding to (size.hi, size.lo).
888 *
889 * phys.hi = 0xYYXXXXZZ, where:
890 * 0xYY = npt000ss
891 * ||| |
72187935
ND
892 * ||| +-- space code
893 * ||| |
894 * ||| + 00 if configuration space
895 * ||| + 01 if IO region,
896 * ||| + 10 if 32-bit MEM region
897 * ||| + 11 if 64-bit MEM region
898 * |||
7454c7af
MR
899 * ||+------ for non-relocatable IO: 1 if aliased
900 * || for relocatable IO: 1 if below 64KB
901 * || for MEM: 1 if below 1MB
902 * |+------- 1 if region is prefetchable
903 * +-------- 1 if region is non-relocatable
904 * 0xXXXX = bbbbbbbb dddddfff, encoding bus, slot, and function
905 * bits respectively
906 * 0xZZ = rrrrrrrr, the register number of the BAR corresponding
907 * to the region
908 *
909 * phys.mid and phys.lo correspond respectively to the hi/lo portions
910 * of the actual address of the region.
911 *
912 * how the phys-addr/size values are used differ slightly between
913 * 'reg' and 'assigned-addresses' properties. namely, 'reg' has
914 * an additional description for the config space region of the
915 * device, and in the case of QEMU has n=0 and phys.mid=phys.lo=0
916 * to describe the region as relocatable, with an address-mapping
917 * that corresponds directly to the PHB's address space for the
918 * resource. 'assigned-addresses' always has n=1 set with an absolute
919 * address assigned for the resource. in general, 'assigned-addresses'
920 * won't be populated, since addresses for PCI devices are generally
921 * unmapped initially and left to the guest to assign.
922 *
923 * note also that addresses defined in these properties are, at least
924 * for PAPR guests, relative to the PHBs IO/MEM windows, and
925 * correspond directly to the addresses in the BARs.
926 *
927 * in accordance with PCI Bus Binding to Open Firmware,
928 * IEEE Std 1275-1994, section 4.1.1, as implemented by PAPR+ v2.7,
929 * Appendix C.
930 */
931static void populate_resource_props(PCIDevice *d, ResourceProps *rp)
932{
933 int bus_num = pci_bus_num(PCI_BUS(qdev_get_parent_bus(DEVICE(d))));
934 uint32_t dev_id = (b_bbbbbbbb(bus_num) |
935 b_ddddd(PCI_SLOT(d->devfn)) |
936 b_fff(PCI_FUNC(d->devfn)));
937 ResourceFields *reg, *assigned;
938 int i, reg_idx = 0, assigned_idx = 0;
939
940 /* config space region */
941 reg = &rp->reg[reg_idx++];
942 reg->phys_hi = cpu_to_be32(dev_id);
943 reg->phys_mid = 0;
944 reg->phys_lo = 0;
945 reg->size_hi = 0;
946 reg->size_lo = 0;
947
948 for (i = 0; i < PCI_NUM_REGIONS; i++) {
949 if (!d->io_regions[i].size) {
950 continue;
951 }
952
953 reg = &rp->reg[reg_idx++];
954
955 reg->phys_hi = cpu_to_be32(dev_id | b_rrrrrrrr(pci_bar(d, i)));
956 if (d->io_regions[i].type & PCI_BASE_ADDRESS_SPACE_IO) {
957 reg->phys_hi |= cpu_to_be32(b_ss(1));
72187935
ND
958 } else if (d->io_regions[i].type & PCI_BASE_ADDRESS_MEM_TYPE_64) {
959 reg->phys_hi |= cpu_to_be32(b_ss(3));
7454c7af
MR
960 } else {
961 reg->phys_hi |= cpu_to_be32(b_ss(2));
962 }
963 reg->phys_mid = 0;
964 reg->phys_lo = 0;
965 reg->size_hi = cpu_to_be32(d->io_regions[i].size >> 32);
966 reg->size_lo = cpu_to_be32(d->io_regions[i].size);
967
968 if (d->io_regions[i].addr == PCI_BAR_UNMAPPED) {
969 continue;
970 }
971
972 assigned = &rp->assigned[assigned_idx++];
382b6f22 973 assigned->phys_hi = cpu_to_be32(be32_to_cpu(reg->phys_hi) | b_n(1));
7454c7af
MR
974 assigned->phys_mid = cpu_to_be32(d->io_regions[i].addr >> 32);
975 assigned->phys_lo = cpu_to_be32(d->io_regions[i].addr);
976 assigned->size_hi = reg->size_hi;
977 assigned->size_lo = reg->size_lo;
978 }
979
980 rp->reg_len = reg_idx * sizeof(ResourceFields);
981 rp->assigned_len = assigned_idx * sizeof(ResourceFields);
982}
983
2530a1a5
LV
984typedef struct PCIClass PCIClass;
985typedef struct PCISubClass PCISubClass;
986typedef struct PCIIFace PCIIFace;
987
988struct PCIIFace {
989 int iface;
990 const char *name;
991};
992
993struct PCISubClass {
994 int subclass;
995 const char *name;
996 const PCIIFace *iface;
997};
998
999struct PCIClass {
1000 const char *name;
1001 const PCISubClass *subc;
1002};
1003
1004static const PCISubClass undef_subclass[] = {
1005 { PCI_CLASS_NOT_DEFINED_VGA, "display", NULL },
1006 { 0xFF, NULL, NULL },
1007};
1008
1009static const PCISubClass mass_subclass[] = {
1010 { PCI_CLASS_STORAGE_SCSI, "scsi", NULL },
1011 { PCI_CLASS_STORAGE_IDE, "ide", NULL },
1012 { PCI_CLASS_STORAGE_FLOPPY, "fdc", NULL },
1013 { PCI_CLASS_STORAGE_IPI, "ipi", NULL },
1014 { PCI_CLASS_STORAGE_RAID, "raid", NULL },
1015 { PCI_CLASS_STORAGE_ATA, "ata", NULL },
1016 { PCI_CLASS_STORAGE_SATA, "sata", NULL },
1017 { PCI_CLASS_STORAGE_SAS, "sas", NULL },
1018 { 0xFF, NULL, NULL },
1019};
1020
1021static const PCISubClass net_subclass[] = {
1022 { PCI_CLASS_NETWORK_ETHERNET, "ethernet", NULL },
1023 { PCI_CLASS_NETWORK_TOKEN_RING, "token-ring", NULL },
1024 { PCI_CLASS_NETWORK_FDDI, "fddi", NULL },
1025 { PCI_CLASS_NETWORK_ATM, "atm", NULL },
1026 { PCI_CLASS_NETWORK_ISDN, "isdn", NULL },
1027 { PCI_CLASS_NETWORK_WORLDFIP, "worldfip", NULL },
1028 { PCI_CLASS_NETWORK_PICMG214, "picmg", NULL },
1029 { 0xFF, NULL, NULL },
1030};
1031
1032static const PCISubClass displ_subclass[] = {
1033 { PCI_CLASS_DISPLAY_VGA, "vga", NULL },
1034 { PCI_CLASS_DISPLAY_XGA, "xga", NULL },
1035 { PCI_CLASS_DISPLAY_3D, "3d-controller", NULL },
1036 { 0xFF, NULL, NULL },
1037};
1038
1039static const PCISubClass media_subclass[] = {
1040 { PCI_CLASS_MULTIMEDIA_VIDEO, "video", NULL },
1041 { PCI_CLASS_MULTIMEDIA_AUDIO, "sound", NULL },
1042 { PCI_CLASS_MULTIMEDIA_PHONE, "telephony", NULL },
1043 { 0xFF, NULL, NULL },
1044};
1045
1046static const PCISubClass mem_subclass[] = {
1047 { PCI_CLASS_MEMORY_RAM, "memory", NULL },
1048 { PCI_CLASS_MEMORY_FLASH, "flash", NULL },
1049 { 0xFF, NULL, NULL },
1050};
1051
1052static const PCISubClass bridg_subclass[] = {
1053 { PCI_CLASS_BRIDGE_HOST, "host", NULL },
1054 { PCI_CLASS_BRIDGE_ISA, "isa", NULL },
1055 { PCI_CLASS_BRIDGE_EISA, "eisa", NULL },
1056 { PCI_CLASS_BRIDGE_MC, "mca", NULL },
1057 { PCI_CLASS_BRIDGE_PCI, "pci", NULL },
1058 { PCI_CLASS_BRIDGE_PCMCIA, "pcmcia", NULL },
1059 { PCI_CLASS_BRIDGE_NUBUS, "nubus", NULL },
1060 { PCI_CLASS_BRIDGE_CARDBUS, "cardbus", NULL },
1061 { PCI_CLASS_BRIDGE_RACEWAY, "raceway", NULL },
1062 { PCI_CLASS_BRIDGE_PCI_SEMITP, "semi-transparent-pci", NULL },
1063 { PCI_CLASS_BRIDGE_IB_PCI, "infiniband", NULL },
1064 { 0xFF, NULL, NULL },
1065};
1066
1067static const PCISubClass comm_subclass[] = {
1068 { PCI_CLASS_COMMUNICATION_SERIAL, "serial", NULL },
1069 { PCI_CLASS_COMMUNICATION_PARALLEL, "parallel", NULL },
1070 { PCI_CLASS_COMMUNICATION_MULTISERIAL, "multiport-serial", NULL },
1071 { PCI_CLASS_COMMUNICATION_MODEM, "modem", NULL },
1072 { PCI_CLASS_COMMUNICATION_GPIB, "gpib", NULL },
1073 { PCI_CLASS_COMMUNICATION_SC, "smart-card", NULL },
1074 { 0xFF, NULL, NULL, },
1075};
1076
1077static const PCIIFace pic_iface[] = {
1078 { PCI_CLASS_SYSTEM_PIC_IOAPIC, "io-apic" },
1079 { PCI_CLASS_SYSTEM_PIC_IOXAPIC, "io-xapic" },
1080 { 0xFF, NULL },
1081};
1082
1083static const PCISubClass sys_subclass[] = {
1084 { PCI_CLASS_SYSTEM_PIC, "interrupt-controller", pic_iface },
1085 { PCI_CLASS_SYSTEM_DMA, "dma-controller", NULL },
1086 { PCI_CLASS_SYSTEM_TIMER, "timer", NULL },
1087 { PCI_CLASS_SYSTEM_RTC, "rtc", NULL },
1088 { PCI_CLASS_SYSTEM_PCI_HOTPLUG, "hot-plug-controller", NULL },
1089 { PCI_CLASS_SYSTEM_SDHCI, "sd-host-controller", NULL },
1090 { 0xFF, NULL, NULL },
1091};
1092
1093static const PCISubClass inp_subclass[] = {
1094 { PCI_CLASS_INPUT_KEYBOARD, "keyboard", NULL },
1095 { PCI_CLASS_INPUT_PEN, "pen", NULL },
1096 { PCI_CLASS_INPUT_MOUSE, "mouse", NULL },
1097 { PCI_CLASS_INPUT_SCANNER, "scanner", NULL },
1098 { PCI_CLASS_INPUT_GAMEPORT, "gameport", NULL },
1099 { 0xFF, NULL, NULL },
1100};
1101
1102static const PCISubClass dock_subclass[] = {
1103 { PCI_CLASS_DOCKING_GENERIC, "dock", NULL },
1104 { 0xFF, NULL, NULL },
1105};
1106
1107static const PCISubClass cpu_subclass[] = {
1108 { PCI_CLASS_PROCESSOR_PENTIUM, "pentium", NULL },
1109 { PCI_CLASS_PROCESSOR_POWERPC, "powerpc", NULL },
1110 { PCI_CLASS_PROCESSOR_MIPS, "mips", NULL },
1111 { PCI_CLASS_PROCESSOR_CO, "co-processor", NULL },
1112 { 0xFF, NULL, NULL },
1113};
1114
1115static const PCIIFace usb_iface[] = {
1116 { PCI_CLASS_SERIAL_USB_UHCI, "usb-uhci" },
1117 { PCI_CLASS_SERIAL_USB_OHCI, "usb-ohci", },
1118 { PCI_CLASS_SERIAL_USB_EHCI, "usb-ehci" },
1119 { PCI_CLASS_SERIAL_USB_XHCI, "usb-xhci" },
1120 { PCI_CLASS_SERIAL_USB_UNKNOWN, "usb-unknown" },
1121 { PCI_CLASS_SERIAL_USB_DEVICE, "usb-device" },
1122 { 0xFF, NULL },
1123};
1124
1125static const PCISubClass ser_subclass[] = {
1126 { PCI_CLASS_SERIAL_FIREWIRE, "firewire", NULL },
1127 { PCI_CLASS_SERIAL_ACCESS, "access-bus", NULL },
1128 { PCI_CLASS_SERIAL_SSA, "ssa", NULL },
1129 { PCI_CLASS_SERIAL_USB, "usb", usb_iface },
1130 { PCI_CLASS_SERIAL_FIBER, "fibre-channel", NULL },
1131 { PCI_CLASS_SERIAL_SMBUS, "smb", NULL },
1132 { PCI_CLASS_SERIAL_IB, "infiniband", NULL },
1133 { PCI_CLASS_SERIAL_IPMI, "ipmi", NULL },
1134 { PCI_CLASS_SERIAL_SERCOS, "sercos", NULL },
1135 { PCI_CLASS_SERIAL_CANBUS, "canbus", NULL },
1136 { 0xFF, NULL, NULL },
1137};
1138
1139static const PCISubClass wrl_subclass[] = {
1140 { PCI_CLASS_WIRELESS_IRDA, "irda", NULL },
1141 { PCI_CLASS_WIRELESS_CIR, "consumer-ir", NULL },
1142 { PCI_CLASS_WIRELESS_RF_CONTROLLER, "rf-controller", NULL },
1143 { PCI_CLASS_WIRELESS_BLUETOOTH, "bluetooth", NULL },
1144 { PCI_CLASS_WIRELESS_BROADBAND, "broadband", NULL },
1145 { 0xFF, NULL, NULL },
1146};
1147
1148static const PCISubClass sat_subclass[] = {
1149 { PCI_CLASS_SATELLITE_TV, "satellite-tv", NULL },
1150 { PCI_CLASS_SATELLITE_AUDIO, "satellite-audio", NULL },
1151 { PCI_CLASS_SATELLITE_VOICE, "satellite-voice", NULL },
1152 { PCI_CLASS_SATELLITE_DATA, "satellite-data", NULL },
1153 { 0xFF, NULL, NULL },
1154};
1155
1156static const PCISubClass crypt_subclass[] = {
1157 { PCI_CLASS_CRYPT_NETWORK, "network-encryption", NULL },
1158 { PCI_CLASS_CRYPT_ENTERTAINMENT,
1159 "entertainment-encryption", NULL },
1160 { 0xFF, NULL, NULL },
1161};
1162
1163static const PCISubClass spc_subclass[] = {
1164 { PCI_CLASS_SP_DPIO, "dpio", NULL },
1165 { PCI_CLASS_SP_PERF, "counter", NULL },
1166 { PCI_CLASS_SP_SYNCH, "measurement", NULL },
1167 { PCI_CLASS_SP_MANAGEMENT, "management-card", NULL },
1168 { 0xFF, NULL, NULL },
1169};
1170
1171static const PCIClass pci_classes[] = {
1172 { "legacy-device", undef_subclass },
1173 { "mass-storage", mass_subclass },
1174 { "network", net_subclass },
1175 { "display", displ_subclass, },
1176 { "multimedia-device", media_subclass },
1177 { "memory-controller", mem_subclass },
1178 { "unknown-bridge", bridg_subclass },
1179 { "communication-controller", comm_subclass},
1180 { "system-peripheral", sys_subclass },
1181 { "input-controller", inp_subclass },
1182 { "docking-station", dock_subclass },
1183 { "cpu", cpu_subclass },
1184 { "serial-bus", ser_subclass },
1185 { "wireless-controller", wrl_subclass },
1186 { "intelligent-io", NULL },
1187 { "satellite-device", sat_subclass },
1188 { "encryption", crypt_subclass },
1189 { "data-processing-controller", spc_subclass },
1190};
1191
1192static const char *pci_find_device_name(uint8_t class, uint8_t subclass,
1193 uint8_t iface)
1194{
1195 const PCIClass *pclass;
1196 const PCISubClass *psubclass;
1197 const PCIIFace *piface;
1198 const char *name;
1199
1200 if (class >= ARRAY_SIZE(pci_classes)) {
1201 return "pci";
1202 }
1203
1204 pclass = pci_classes + class;
1205 name = pclass->name;
1206
1207 if (pclass->subc == NULL) {
1208 return name;
1209 }
1210
1211 psubclass = pclass->subc;
1212 while ((psubclass->subclass & 0xff) != 0xff) {
1213 if ((psubclass->subclass & 0xff) == subclass) {
1214 name = psubclass->name;
1215 break;
1216 }
1217 psubclass++;
1218 }
1219
1220 piface = psubclass->iface;
1221 if (piface == NULL) {
1222 return name;
1223 }
1224 while ((piface->iface & 0xff) != 0xff) {
1225 if ((piface->iface & 0xff) == iface) {
1226 name = piface->name;
1227 break;
1228 }
1229 piface++;
1230 }
1231
1232 return name;
1233}
1234
549ce59e 1235static gchar *pci_get_node_name(PCIDevice *dev)
2530a1a5
LV
1236{
1237 int slot = PCI_SLOT(dev->devfn);
1238 int func = PCI_FUNC(dev->devfn);
1239 uint32_t ccode = pci_default_read_config(dev, PCI_CLASS_PROG, 3);
1240 const char *name;
1241
1242 name = pci_find_device_name((ccode >> 16) & 0xff, (ccode >> 8) & 0xff,
1243 ccode & 0xff);
1244
1245 if (func != 0) {
549ce59e 1246 return g_strdup_printf("%s@%x,%x", name, slot, func);
2530a1a5 1247 } else {
549ce59e 1248 return g_strdup_printf("%s@%x", name, slot);
2530a1a5
LV
1249 }
1250}
1251
e634b89c
ND
1252static uint32_t spapr_phb_get_pci_drc_index(sPAPRPHBState *phb,
1253 PCIDevice *pdev);
1254
9ba25536 1255static void spapr_populate_pci_child_dt(PCIDevice *dev, void *fdt, int offset,
16b0ea1d 1256 sPAPRPHBState *sphb)
7454c7af
MR
1257{
1258 ResourceProps rp;
1259 bool is_bridge = false;
9ba25536 1260 int pci_status;
16b0ea1d 1261 char *buf = NULL;
e634b89c 1262 uint32_t drc_index = spapr_phb_get_pci_drc_index(sphb, dev);
2530a1a5 1263 uint32_t ccode = pci_default_read_config(dev, PCI_CLASS_PROG, 3);
a8ad731a 1264 uint32_t max_msi, max_msix;
7454c7af
MR
1265
1266 if (pci_default_read_config(dev, PCI_HEADER_TYPE, 1) ==
1267 PCI_HEADER_TYPE_BRIDGE) {
1268 is_bridge = true;
1269 }
1270
1271 /* in accordance with PAPR+ v2.7 13.6.3, Table 181 */
1272 _FDT(fdt_setprop_cell(fdt, offset, "vendor-id",
1273 pci_default_read_config(dev, PCI_VENDOR_ID, 2)));
1274 _FDT(fdt_setprop_cell(fdt, offset, "device-id",
1275 pci_default_read_config(dev, PCI_DEVICE_ID, 2)));
1276 _FDT(fdt_setprop_cell(fdt, offset, "revision-id",
1277 pci_default_read_config(dev, PCI_REVISION_ID, 1)));
2530a1a5 1278 _FDT(fdt_setprop_cell(fdt, offset, "class-code", ccode));
7454c7af
MR
1279 if (pci_default_read_config(dev, PCI_INTERRUPT_PIN, 1)) {
1280 _FDT(fdt_setprop_cell(fdt, offset, "interrupts",
1281 pci_default_read_config(dev, PCI_INTERRUPT_PIN, 1)));
1282 }
1283
1284 if (!is_bridge) {
1285 _FDT(fdt_setprop_cell(fdt, offset, "min-grant",
1286 pci_default_read_config(dev, PCI_MIN_GNT, 1)));
1287 _FDT(fdt_setprop_cell(fdt, offset, "max-latency",
1288 pci_default_read_config(dev, PCI_MAX_LAT, 1)));
1289 }
1290
1291 if (pci_default_read_config(dev, PCI_SUBSYSTEM_ID, 2)) {
1292 _FDT(fdt_setprop_cell(fdt, offset, "subsystem-id",
1293 pci_default_read_config(dev, PCI_SUBSYSTEM_ID, 2)));
1294 }
1295
1296 if (pci_default_read_config(dev, PCI_SUBSYSTEM_VENDOR_ID, 2)) {
1297 _FDT(fdt_setprop_cell(fdt, offset, "subsystem-vendor-id",
1298 pci_default_read_config(dev, PCI_SUBSYSTEM_VENDOR_ID, 2)));
1299 }
1300
1301 _FDT(fdt_setprop_cell(fdt, offset, "cache-line-size",
1302 pci_default_read_config(dev, PCI_CACHE_LINE_SIZE, 1)));
1303
1304 /* the following fdt cells are masked off the pci status register */
1305 pci_status = pci_default_read_config(dev, PCI_STATUS, 2);
1306 _FDT(fdt_setprop_cell(fdt, offset, "devsel-speed",
1307 PCI_STATUS_DEVSEL_MASK & pci_status));
1308
1309 if (pci_status & PCI_STATUS_FAST_BACK) {
1310 _FDT(fdt_setprop(fdt, offset, "fast-back-to-back", NULL, 0));
1311 }
1312 if (pci_status & PCI_STATUS_66MHZ) {
1313 _FDT(fdt_setprop(fdt, offset, "66mhz-capable", NULL, 0));
1314 }
1315 if (pci_status & PCI_STATUS_UDF) {
1316 _FDT(fdt_setprop(fdt, offset, "udf-supported", NULL, 0));
1317 }
1318
2530a1a5
LV
1319 _FDT(fdt_setprop_string(fdt, offset, "name",
1320 pci_find_device_name((ccode >> 16) & 0xff,
1321 (ccode >> 8) & 0xff,
1322 ccode & 0xff)));
16b0ea1d 1323
d049bde6 1324 buf = spapr_phb_get_loc_code(sphb, dev);
9ba25536 1325 _FDT(fdt_setprop_string(fdt, offset, "ibm,loc-code", buf));
16b0ea1d 1326 g_free(buf);
16b0ea1d 1327
e634b89c
ND
1328 if (drc_index) {
1329 _FDT(fdt_setprop_cell(fdt, offset, "ibm,my-drc-index", drc_index));
1330 }
7454c7af
MR
1331
1332 _FDT(fdt_setprop_cell(fdt, offset, "#address-cells",
1333 RESOURCE_CELLS_ADDRESS));
1334 _FDT(fdt_setprop_cell(fdt, offset, "#size-cells",
1335 RESOURCE_CELLS_SIZE));
a8ad731a 1336
9cbe305b
GK
1337 if (msi_present(dev)) {
1338 max_msi = msi_nr_vectors_allocated(dev);
1339 if (max_msi) {
1340 _FDT(fdt_setprop_cell(fdt, offset, "ibm,req#msi", max_msi));
1341 }
a8ad731a 1342 }
9cbe305b
GK
1343 if (msix_present(dev)) {
1344 max_msix = dev->msix_entries_nr;
1345 if (max_msix) {
1346 _FDT(fdt_setprop_cell(fdt, offset, "ibm,req#msi-x", max_msix));
1347 }
a8ad731a 1348 }
7454c7af
MR
1349
1350 populate_resource_props(dev, &rp);
1351 _FDT(fdt_setprop(fdt, offset, "reg", (uint8_t *)rp.reg, rp.reg_len));
1352 _FDT(fdt_setprop(fdt, offset, "assigned-addresses",
1353 (uint8_t *)rp.assigned, rp.assigned_len));
1354
82516263 1355 if (sphb->pcie_ecs && pci_is_express(dev)) {
bb998645
DG
1356 _FDT(fdt_setprop_cell(fdt, offset, "ibm,pci-config-space-type", 0x1));
1357 }
7454c7af
MR
1358}
1359
1360/* create OF node for pci device and required OF DT properties */
1d2d9742 1361static int spapr_create_pci_child_dt(sPAPRPHBState *phb, PCIDevice *dev,
1d2d9742 1362 void *fdt, int node_offset)
7454c7af 1363{
9ba25536 1364 int offset;
549ce59e 1365 gchar *nodename;
7454c7af 1366
549ce59e 1367 nodename = pci_get_node_name(dev);
9ba25536 1368 _FDT(offset = fdt_add_subnode(fdt, node_offset, nodename));
549ce59e
GK
1369 g_free(nodename);
1370
9ba25536 1371 spapr_populate_pci_child_dt(dev, fdt, offset, phb);
e634b89c 1372
1d2d9742 1373 return offset;
7454c7af
MR
1374}
1375
31834723
DHB
1376/* Callback to be called during DRC release. */
1377void spapr_phb_remove_pci_device_cb(DeviceState *dev)
7454c7af 1378{
27c1da51
DH
1379 HotplugHandler *hotplug_ctrl = qdev_get_hotplug_handler(dev);
1380
1381 hotplug_handler_unplug(hotplug_ctrl, dev, &error_abort);
7454c7af
MR
1382}
1383
788d2599
MR
1384static sPAPRDRConnector *spapr_phb_get_pci_func_drc(sPAPRPHBState *phb,
1385 uint32_t busnr,
1386 int32_t devfn)
7454c7af 1387{
fbf55397
DG
1388 return spapr_drc_by_id(TYPE_SPAPR_DRC_PCI,
1389 (phb->index << 16) | (busnr << 8) | devfn);
788d2599
MR
1390}
1391
1392static sPAPRDRConnector *spapr_phb_get_pci_drc(sPAPRPHBState *phb,
1393 PCIDevice *pdev)
1394{
1395 uint32_t busnr = pci_bus_num(PCI_BUS(qdev_get_parent_bus(DEVICE(pdev))));
1396 return spapr_phb_get_pci_func_drc(phb, busnr, pdev->devfn);
7454c7af
MR
1397}
1398
1d2d9742
ND
1399static uint32_t spapr_phb_get_pci_drc_index(sPAPRPHBState *phb,
1400 PCIDevice *pdev)
1401{
1402 sPAPRDRConnector *drc = spapr_phb_get_pci_drc(phb, pdev);
1d2d9742
ND
1403
1404 if (!drc) {
1405 return 0;
1406 }
1407
0b55aa91 1408 return spapr_drc_index(drc);
1d2d9742
ND
1409}
1410
3340e5c4
DG
1411static void spapr_pci_plug(HotplugHandler *plug_handler,
1412 DeviceState *plugged_dev, Error **errp)
7454c7af
MR
1413{
1414 sPAPRPHBState *phb = SPAPR_PCI_HOST_BRIDGE(DEVICE(plug_handler));
1415 PCIDevice *pdev = PCI_DEVICE(plugged_dev);
1416 sPAPRDRConnector *drc = spapr_phb_get_pci_drc(phb, pdev);
1417 Error *local_err = NULL;
788d2599
MR
1418 PCIBus *bus = PCI_BUS(qdev_get_parent_bus(DEVICE(pdev)));
1419 uint32_t slotnr = PCI_SLOT(pdev->devfn);
6304fd27
DG
1420 void *fdt = NULL;
1421 int fdt_start_offset, fdt_size;
7454c7af
MR
1422
1423 /* if DR is disabled we don't need to do anything in the case of
1424 * hotplug or coldplug callbacks
1425 */
1426 if (!phb->dr_enabled) {
1427 /* if this is a hotplug operation initiated by the user
1428 * we need to let them know it's not enabled
1429 */
1430 if (plugged_dev->hotplugged) {
6304fd27 1431 error_setg(&local_err, QERR_BUS_NO_HOTPLUG,
c6bd8c70 1432 object_get_typename(OBJECT(phb)));
7454c7af 1433 }
6304fd27 1434 goto out;
7454c7af
MR
1435 }
1436
1437 g_assert(drc);
1438
788d2599
MR
1439 /* Following the QEMU convention used for PCIe multifunction
1440 * hotplug, we do not allow functions to be hotplugged to a
1441 * slot that already has function 0 present
1442 */
1443 if (plugged_dev->hotplugged && bus->devices[PCI_DEVFN(slotnr, 0)] &&
1444 PCI_FUNC(pdev->devfn) != 0) {
6304fd27 1445 error_setg(&local_err, "PCI: slot %d function 0 already ocuppied by %s,"
788d2599
MR
1446 " additional functions can no longer be exposed to guest.",
1447 slotnr, bus->devices[PCI_DEVFN(slotnr, 0)]->name);
6304fd27
DG
1448 goto out;
1449 }
1450
1451 fdt = create_device_tree(&fdt_size);
1452 fdt_start_offset = spapr_create_pci_child_dt(phb, pdev, fdt, 0);
788d2599 1453
5c1da812 1454 spapr_drc_attach(drc, DEVICE(pdev), fdt, fdt_start_offset, &local_err);
7454c7af 1455 if (local_err) {
6304fd27 1456 goto out;
7454c7af 1457 }
788d2599
MR
1458
1459 /* If this is function 0, signal hotplug for all the device functions.
1460 * Otherwise defer sending the hotplug event.
1461 */
94fd9cba
LV
1462 if (!spapr_drc_hotplugged(plugged_dev)) {
1463 spapr_drc_reset(drc);
1464 } else if (PCI_FUNC(pdev->devfn) == 0) {
788d2599
MR
1465 int i;
1466
1467 for (i = 0; i < 8; i++) {
1468 sPAPRDRConnector *func_drc;
1469 sPAPRDRConnectorClass *func_drck;
1470 sPAPRDREntitySense state;
1471
1472 func_drc = spapr_phb_get_pci_func_drc(phb, pci_bus_num(bus),
1473 PCI_DEVFN(slotnr, i));
1474 func_drck = SPAPR_DR_CONNECTOR_GET_CLASS(func_drc);
f224d35b 1475 state = func_drck->dr_entity_sense(func_drc);
788d2599
MR
1476
1477 if (state == SPAPR_DR_ENTITY_SENSE_PRESENT) {
1478 spapr_hotplug_req_add_by_index(func_drc);
1479 }
1480 }
c5bc152b 1481 }
6304fd27
DG
1482
1483out:
1484 if (local_err) {
1485 error_propagate(errp, local_err);
1486 g_free(fdt);
1487 }
7454c7af
MR
1488}
1489
27c1da51
DH
1490static void spapr_pci_unplug(HotplugHandler *plug_handler,
1491 DeviceState *plugged_dev, Error **errp)
1492{
1493 /* some version guests do not wait for completion of a device
1494 * cleanup (generally done asynchronously by the kernel) before
1495 * signaling to QEMU that the device is safe, but instead sleep
1496 * for some 'safe' period of time. unfortunately on a busy host
1497 * this sleep isn't guaranteed to be long enough, resulting in
1498 * bad things like IRQ lines being left asserted during final
1499 * device removal. to deal with this we call reset just prior
1500 * to finalizing the device, which will put the device back into
1501 * an 'idle' state, as the device cleanup code expects.
1502 */
1503 pci_device_reset(PCI_DEVICE(plugged_dev));
1504 object_unparent(OBJECT(plugged_dev));
1505}
1506
3340e5c4
DG
1507static void spapr_pci_unplug_request(HotplugHandler *plug_handler,
1508 DeviceState *plugged_dev, Error **errp)
7454c7af
MR
1509{
1510 sPAPRPHBState *phb = SPAPR_PCI_HOST_BRIDGE(DEVICE(plug_handler));
1511 PCIDevice *pdev = PCI_DEVICE(plugged_dev);
7454c7af 1512 sPAPRDRConnector *drc = spapr_phb_get_pci_drc(phb, pdev);
7454c7af
MR
1513
1514 if (!phb->dr_enabled) {
c6bd8c70
MA
1515 error_setg(errp, QERR_BUS_NO_HOTPLUG,
1516 object_get_typename(OBJECT(phb)));
7454c7af
MR
1517 return;
1518 }
1519
1520 g_assert(drc);
3340e5c4 1521 g_assert(drc->dev == plugged_dev);
7454c7af 1522
f1c52354 1523 if (!spapr_drc_unplug_requested(drc)) {
788d2599
MR
1524 PCIBus *bus = PCI_BUS(qdev_get_parent_bus(DEVICE(pdev)));
1525 uint32_t slotnr = PCI_SLOT(pdev->devfn);
1526 sPAPRDRConnector *func_drc;
1527 sPAPRDRConnectorClass *func_drck;
1528 sPAPRDREntitySense state;
1529 int i;
1530
1531 /* ensure any other present functions are pending unplug */
1532 if (PCI_FUNC(pdev->devfn) == 0) {
1533 for (i = 1; i < 8; i++) {
1534 func_drc = spapr_phb_get_pci_func_drc(phb, pci_bus_num(bus),
1535 PCI_DEVFN(slotnr, i));
1536 func_drck = SPAPR_DR_CONNECTOR_GET_CLASS(func_drc);
f224d35b 1537 state = func_drck->dr_entity_sense(func_drc);
788d2599 1538 if (state == SPAPR_DR_ENTITY_SENSE_PRESENT
f1c52354 1539 && !spapr_drc_unplug_requested(func_drc)) {
788d2599
MR
1540 error_setg(errp,
1541 "PCI: slot %d, function %d still present. "
1542 "Must unplug all non-0 functions first.",
1543 slotnr, i);
1544 return;
1545 }
1546 }
1547 }
1548
a8dc47fd 1549 spapr_drc_detach(drc);
788d2599
MR
1550
1551 /* if this isn't func 0, defer unplug event. otherwise signal removal
1552 * for all present functions
1553 */
1554 if (PCI_FUNC(pdev->devfn) == 0) {
1555 for (i = 7; i >= 0; i--) {
1556 func_drc = spapr_phb_get_pci_func_drc(phb, pci_bus_num(bus),
1557 PCI_DEVFN(slotnr, i));
1558 func_drck = SPAPR_DR_CONNECTOR_GET_CLASS(func_drc);
f224d35b 1559 state = func_drck->dr_entity_sense(func_drc);
788d2599
MR
1560 if (state == SPAPR_DR_ENTITY_SENSE_PRESENT) {
1561 spapr_hotplug_req_remove_by_index(func_drc);
1562 }
1563 }
1564 }
7454c7af
MR
1565 }
1566}
1567
c6ba42f6 1568static void spapr_phb_realize(DeviceState *dev, Error **errp)
3384f95c 1569{
f7d6bfcd
GK
1570 /* We don't use SPAPR_MACHINE() in order to exit gracefully if the user
1571 * tries to add a sPAPR PHB to a non-pseries machine.
1572 */
1573 sPAPRMachineState *spapr =
1574 (sPAPRMachineState *) object_dynamic_cast(qdev_get_machine(),
1575 TYPE_SPAPR_MACHINE);
bc9b1f10 1576 sPAPRMachineClass *smc = spapr ? SPAPR_MACHINE_GET_CLASS(spapr) : NULL;
c6ba42f6 1577 SysBusDevice *s = SYS_BUS_DEVICE(dev);
8c9f64df 1578 sPAPRPHBState *sphb = SPAPR_PCI_HOST_BRIDGE(s);
8558d942 1579 PCIHostState *phb = PCI_HOST_BRIDGE(s);
298a9710
DG
1580 char *namebuf;
1581 int i;
3384f95c 1582 PCIBus *bus;
8c46f7ec 1583 uint64_t msi_window_size = 4096;
a36304fd 1584 sPAPRTCETable *tcet;
ae4de14c
AK
1585 const unsigned windows_supported =
1586 sphb->ddw_enabled ? SPAPR_PCI_DMA_MAX_WINDOWS : 1;
3384f95c 1587
f7d6bfcd
GK
1588 if (!spapr) {
1589 error_setg(errp, TYPE_SPAPR_PCI_HOST_BRIDGE " needs a pseries machine");
1590 return;
1591 }
1592
421b1b27 1593 if (sphb->index != (uint32_t)-1) {
6737d9ad 1594 Error *local_err = NULL;
caae58cb 1595
daa23699
DG
1596 smc->phb_placement(spapr, sphb->index,
1597 &sphb->buid, &sphb->io_win_addr,
1598 &sphb->mem_win_addr, &sphb->mem64_win_addr,
6737d9ad
DG
1599 windows_supported, sphb->dma_liobn, &local_err);
1600 if (local_err) {
1601 error_propagate(errp, local_err);
3e4ac968
DG
1602 return;
1603 }
30b3bc5a
GK
1604 } else {
1605 error_setg(errp, "\"index\" for PAPR PHB is mandatory");
c6ba42f6 1606 return;
caae58cb
DG
1607 }
1608
daa23699 1609 if (sphb->mem64_win_size != 0) {
daa23699
DG
1610 if (sphb->mem_win_size > SPAPR_PCI_MEM32_WIN_SIZE) {
1611 error_setg(errp, "32-bit memory window of size 0x%"HWADDR_PRIx
1612 " (max 2 GiB)", sphb->mem_win_size);
1613 return;
1614 }
1615
30b3bc5a
GK
1616 /* 64-bit window defaults to identity mapping */
1617 sphb->mem64_win_pciaddr = sphb->mem64_win_addr;
daa23699
DG
1618 } else if (sphb->mem_win_size > SPAPR_PCI_MEM32_WIN_SIZE) {
1619 /*
1620 * For compatibility with old configuration, if no 64-bit MMIO
1621 * window is specified, but the ordinary (32-bit) memory
1622 * window is specified as > 2GiB, we treat it as a 2GiB 32-bit
1623 * window, with a 64-bit MMIO window following on immediately
1624 * afterwards
1625 */
1626 sphb->mem64_win_size = sphb->mem_win_size - SPAPR_PCI_MEM32_WIN_SIZE;
1627 sphb->mem64_win_addr = sphb->mem_win_addr + SPAPR_PCI_MEM32_WIN_SIZE;
1628 sphb->mem64_win_pciaddr =
1629 SPAPR_PCI_MEM_WIN_BUS_OFFSET + SPAPR_PCI_MEM32_WIN_SIZE;
1630 sphb->mem_win_size = SPAPR_PCI_MEM32_WIN_SIZE;
1631 }
1632
46c5874e 1633 if (spapr_pci_find_phb(spapr, sphb->buid)) {
c6ba42f6
AK
1634 error_setg(errp, "PCI host bridges must have unique BUIDs");
1635 return;
caae58cb
DG
1636 }
1637
4bcfa56c
MR
1638 if (sphb->numa_node != -1 &&
1639 (sphb->numa_node >= MAX_NODES || !numa_info[sphb->numa_node].present)) {
1640 error_setg(errp, "Invalid NUMA node ID for PCI host bridge");
1641 return;
1642 }
1643
8c9f64df 1644 sphb->dtbusname = g_strdup_printf("pci@%" PRIx64, sphb->buid);
caae58cb 1645
298a9710 1646 /* Initialize memory regions */
1d36da76 1647 namebuf = g_strdup_printf("%s.mmio", sphb->dtbusname);
92b8e39c 1648 memory_region_init(&sphb->memspace, OBJECT(sphb), namebuf, UINT64_MAX);
1d36da76 1649 g_free(namebuf);
3384f95c 1650
1d36da76 1651 namebuf = g_strdup_printf("%s.mmio32-alias", sphb->dtbusname);
daa23699 1652 memory_region_init_alias(&sphb->mem32window, OBJECT(sphb),
40c5dce9 1653 namebuf, &sphb->memspace,
8c9f64df 1654 SPAPR_PCI_MEM_WIN_BUS_OFFSET, sphb->mem_win_size);
1d36da76 1655 g_free(namebuf);
8c9f64df 1656 memory_region_add_subregion(get_system_memory(), sphb->mem_win_addr,
daa23699
DG
1657 &sphb->mem32window);
1658
30b3bc5a 1659 if (sphb->mem64_win_size != 0) {
96dbc9af
GK
1660 namebuf = g_strdup_printf("%s.mmio64-alias", sphb->dtbusname);
1661 memory_region_init_alias(&sphb->mem64window, OBJECT(sphb),
1662 namebuf, &sphb->memspace,
1663 sphb->mem64_win_pciaddr, sphb->mem64_win_size);
1664 g_free(namebuf);
1665
30b3bc5a
GK
1666 memory_region_add_subregion(get_system_memory(),
1667 sphb->mem64_win_addr,
1668 &sphb->mem64window);
96dbc9af 1669 }
3384f95c 1670
fabe9ee1 1671 /* Initialize IO regions */
1d36da76 1672 namebuf = g_strdup_printf("%s.io", sphb->dtbusname);
40c5dce9
PB
1673 memory_region_init(&sphb->iospace, OBJECT(sphb),
1674 namebuf, SPAPR_PCI_IO_WIN_SIZE);
1d36da76 1675 g_free(namebuf);
3384f95c 1676
1d36da76 1677 namebuf = g_strdup_printf("%s.io-alias", sphb->dtbusname);
66aab867 1678 memory_region_init_alias(&sphb->iowindow, OBJECT(sphb), namebuf,
fabe9ee1 1679 &sphb->iospace, 0, SPAPR_PCI_IO_WIN_SIZE);
1d36da76 1680 g_free(namebuf);
8c9f64df 1681 memory_region_add_subregion(get_system_memory(), sphb->io_win_addr,
a3cfa18e 1682 &sphb->iowindow);
1b8601b0 1683
1115ff6d
DG
1684 bus = pci_register_root_bus(dev, NULL,
1685 pci_spapr_set_irq, pci_spapr_map_irq, sphb,
1686 &sphb->memspace, &sphb->iospace,
1687 PCI_DEVFN(0, 0), PCI_NUM_PINS, TYPE_PCI_BUS);
8c9f64df 1688 phb->bus = bus;
94d1cc5f 1689 qbus_set_hotplug_handler(BUS(phb->bus), OBJECT(sphb), NULL);
298a9710 1690
cca7fad5
AK
1691 /*
1692 * Initialize PHB address space.
1693 * By default there will be at least one subregion for default
1694 * 32bit DMA window.
1695 * Later the guest might want to create another DMA window
1696 * which will become another memory subregion.
1697 */
1d36da76 1698 namebuf = g_strdup_printf("%s.iommu-root", sphb->dtbusname);
cca7fad5
AK
1699 memory_region_init(&sphb->iommu_root, OBJECT(sphb),
1700 namebuf, UINT64_MAX);
1d36da76 1701 g_free(namebuf);
cca7fad5
AK
1702 address_space_init(&sphb->iommu_as, &sphb->iommu_root,
1703 sphb->dtbusname);
1704
8c46f7ec
GK
1705 /*
1706 * As MSI/MSIX interrupts trigger by writing at MSI/MSIX vectors,
1707 * we need to allocate some memory to catch those writes coming
1708 * from msi_notify()/msix_notify().
1709 * As MSIMessage:addr is going to be the same and MSIMessage:data
1710 * is going to be a VIRQ number, 4 bytes of the MSI MR will only
1711 * be used.
1712 *
1713 * For KVM we want to ensure that this memory is a full page so that
1714 * our memory slot is of page size granularity.
1715 */
1716#ifdef CONFIG_KVM
1717 if (kvm_enabled()) {
1718 msi_window_size = getpagesize();
1719 }
1720#endif
1721
dba95ebb 1722 memory_region_init_io(&sphb->msiwindow, OBJECT(sphb), &spapr_msi_ops, spapr,
8c46f7ec
GK
1723 "msi", msi_window_size);
1724 memory_region_add_subregion(&sphb->iommu_root, SPAPR_PCI_MSI_WINDOW,
1725 &sphb->msiwindow);
1726
e00387d5 1727 pci_setup_iommu(bus, spapr_pci_dma_iommu, sphb);
edded454 1728
5cc7a967
AK
1729 pci_bus_set_route_irq_fn(bus, spapr_route_intx_pin_to_irq);
1730
8c9f64df 1731 QLIST_INSERT_HEAD(&spapr->phbs, sphb, list);
298a9710
DG
1732
1733 /* Initialize the LSI table */
7fb0bd34 1734 for (i = 0; i < PCI_NUM_PINS; i++) {
82cffa2e 1735 uint32_t irq = SPAPR_IRQ_PCI_LSI + sphb->index * PCI_NUM_PINS + i;
a005b3ef 1736 Error *local_err = NULL;
298a9710 1737
2c88b098 1738 if (smc->legacy_irq_allocation) {
82cffa2e
CLG
1739 irq = spapr_irq_findone(spapr, &local_err);
1740 if (local_err) {
4b576648
MA
1741 error_propagate_prepend(errp, local_err,
1742 "can't allocate LSIs: ");
82cffa2e
CLG
1743 return;
1744 }
4fe75a8c
CLG
1745 }
1746
1747 spapr_irq_claim(spapr, irq, true, &local_err);
a005b3ef 1748 if (local_err) {
4b576648 1749 error_propagate_prepend(errp, local_err, "can't allocate LSIs: ");
c6ba42f6 1750 return;
298a9710
DG
1751 }
1752
8c9f64df 1753 sphb->lsi_table[i].irq = irq;
298a9710 1754 }
da6ccee4 1755
62083979
MR
1756 /* allocate connectors for child PCI devices */
1757 if (sphb->dr_enabled) {
1758 for (i = 0; i < PCI_SLOT_MAX * 8; i++) {
2d335818 1759 spapr_dr_connector_new(OBJECT(phb), TYPE_SPAPR_DRC_PCI,
62083979
MR
1760 (sphb->index << 16) | i);
1761 }
1762 }
1763
ae4de14c
AK
1764 /* DMA setup */
1765 for (i = 0; i < windows_supported; ++i) {
1766 tcet = spapr_tce_new_table(DEVICE(sphb), sphb->dma_liobn[i]);
1767 if (!tcet) {
1768 error_setg(errp, "Creating window#%d failed for %s",
1769 i, sphb->dtbusname);
1770 return;
1771 }
5c3d70e9
GK
1772 memory_region_add_subregion(&sphb->iommu_root, 0,
1773 spapr_tce_get_iommu(tcet));
da6ccee4 1774 }
cca7fad5 1775
a36304fd 1776 sphb->msi = g_hash_table_new_full(g_int_hash, g_int_equal, g_free, g_free);
298a9710
DG
1777}
1778
e28c16f6 1779static int spapr_phb_children_reset(Object *child, void *opaque)
eddeed26 1780{
e28c16f6
AK
1781 DeviceState *dev = (DeviceState *) object_dynamic_cast(child, TYPE_DEVICE);
1782
1783 if (dev) {
1784 device_reset(dev);
1785 }
eddeed26 1786
e28c16f6
AK
1787 return 0;
1788}
1789
b3162f22 1790void spapr_phb_dma_reset(sPAPRPHBState *sphb)
e28c16f6 1791{
ae4de14c
AK
1792 int i;
1793 sPAPRTCETable *tcet;
1794
1795 for (i = 0; i < SPAPR_PCI_DMA_MAX_WINDOWS; ++i) {
1796 tcet = spapr_tce_find_by_liobn(sphb->dma_liobn[i]);
acf1b6dd 1797
ae4de14c
AK
1798 if (tcet && tcet->nb_table) {
1799 spapr_tce_table_disable(tcet);
1800 }
acf1b6dd
AK
1801 }
1802
1803 /* Register default 32bit DMA window */
ae4de14c 1804 tcet = spapr_tce_find_by_liobn(sphb->dma_liobn[0]);
acf1b6dd
AK
1805 spapr_tce_table_enable(tcet, SPAPR_TCE_PAGE_SHIFT, sphb->dma_win_addr,
1806 sphb->dma_win_size >> SPAPR_TCE_PAGE_SHIFT);
b3162f22
AK
1807}
1808
1809static void spapr_phb_reset(DeviceState *qdev)
1810{
1811 sPAPRPHBState *sphb = SPAPR_PCI_HOST_BRIDGE(qdev);
1812
1813 spapr_phb_dma_reset(sphb);
acf1b6dd 1814
eddeed26 1815 /* Reset the IOMMU state */
e28c16f6 1816 object_child_foreach(OBJECT(qdev), spapr_phb_children_reset, NULL);
fbb4e983
DG
1817
1818 if (spapr_phb_eeh_available(SPAPR_PCI_HOST_BRIDGE(qdev))) {
1819 spapr_phb_vfio_reset(qdev);
1820 }
eddeed26
DG
1821}
1822
298a9710 1823static Property spapr_phb_properties[] = {
3e4ac968 1824 DEFINE_PROP_UINT32("index", sPAPRPHBState, index, -1),
c7bcc85d 1825 DEFINE_PROP_UINT64("mem_win_size", sPAPRPHBState, mem_win_size,
357d1e3b 1826 SPAPR_PCI_MEM32_WIN_SIZE),
357d1e3b
DG
1827 DEFINE_PROP_UINT64("mem64_win_size", sPAPRPHBState, mem64_win_size,
1828 SPAPR_PCI_MEM64_WIN_SIZE),
c7bcc85d
PB
1829 DEFINE_PROP_UINT64("io_win_size", sPAPRPHBState, io_win_size,
1830 SPAPR_PCI_IO_WIN_SIZE),
7619c7b0
MR
1831 DEFINE_PROP_BOOL("dynamic-reconfiguration", sPAPRPHBState, dr_enabled,
1832 true),
f93caaac
DG
1833 /* Default DMA window is 0..1GB */
1834 DEFINE_PROP_UINT64("dma_win_addr", sPAPRPHBState, dma_win_addr, 0),
1835 DEFINE_PROP_UINT64("dma_win_size", sPAPRPHBState, dma_win_size, 0x40000000),
ae4de14c
AK
1836 DEFINE_PROP_UINT64("dma64_win_addr", sPAPRPHBState, dma64_win_addr,
1837 0x800000000000000ULL),
1838 DEFINE_PROP_BOOL("ddw", sPAPRPHBState, ddw_enabled, true),
1839 DEFINE_PROP_UINT64("pgsz", sPAPRPHBState, page_size_mask,
1840 (1ULL << 12) | (1ULL << 16)),
4814401f 1841 DEFINE_PROP_UINT32("numa_node", sPAPRPHBState, numa_node, -1),
5c4537bd
DG
1842 DEFINE_PROP_BOOL("pre-2.8-migration", sPAPRPHBState,
1843 pre_2_8_migration, false),
82516263
DG
1844 DEFINE_PROP_BOOL("pcie-extended-configuration-space", sPAPRPHBState,
1845 pcie_ecs, true),
298a9710
DG
1846 DEFINE_PROP_END_OF_LIST(),
1847};
1848
1112cf94
DG
1849static const VMStateDescription vmstate_spapr_pci_lsi = {
1850 .name = "spapr_pci/lsi",
1851 .version_id = 1,
1852 .minimum_version_id = 1,
3aff6c2f 1853 .fields = (VMStateField[]) {
d2164ad3 1854 VMSTATE_UINT32_EQUAL(irq, struct spapr_pci_lsi, NULL),
1112cf94
DG
1855
1856 VMSTATE_END_OF_LIST()
1857 },
1858};
1859
1860static const VMStateDescription vmstate_spapr_pci_msi = {
9a321e92 1861 .name = "spapr_pci/msi",
1112cf94
DG
1862 .version_id = 1,
1863 .minimum_version_id = 1,
9a321e92
AK
1864 .fields = (VMStateField []) {
1865 VMSTATE_UINT32(key, spapr_pci_msi_mig),
1866 VMSTATE_UINT32(value.first_irq, spapr_pci_msi_mig),
1867 VMSTATE_UINT32(value.num, spapr_pci_msi_mig),
1112cf94
DG
1868 VMSTATE_END_OF_LIST()
1869 },
1870};
1871
44b1ff31 1872static int spapr_pci_pre_save(void *opaque)
9a321e92
AK
1873{
1874 sPAPRPHBState *sphb = opaque;
708414f0
MA
1875 GHashTableIter iter;
1876 gpointer key, value;
1877 int i;
9a321e92 1878
5c4537bd
DG
1879 if (sphb->pre_2_8_migration) {
1880 sphb->mig_liobn = sphb->dma_liobn[0];
1881 sphb->mig_mem_win_addr = sphb->mem_win_addr;
1882 sphb->mig_mem_win_size = sphb->mem_win_size;
1883 sphb->mig_io_win_addr = sphb->io_win_addr;
1884 sphb->mig_io_win_size = sphb->io_win_size;
1885
1886 if ((sphb->mem64_win_size != 0)
1887 && (sphb->mem64_win_addr
1888 == (sphb->mem_win_addr + sphb->mem_win_size))) {
1889 sphb->mig_mem_win_size += sphb->mem64_win_size;
1890 }
1891 }
e806b4db
LV
1892
1893 g_free(sphb->msi_devs);
1894 sphb->msi_devs = NULL;
1895 sphb->msi_devs_num = g_hash_table_size(sphb->msi);
1896 if (!sphb->msi_devs_num) {
44b1ff31 1897 return 0;
e806b4db 1898 }
4fc4c6a5 1899 sphb->msi_devs = g_new(spapr_pci_msi_mig, sphb->msi_devs_num);
e806b4db
LV
1900
1901 g_hash_table_iter_init(&iter, sphb->msi);
1902 for (i = 0; g_hash_table_iter_next(&iter, &key, &value); ++i) {
1903 sphb->msi_devs[i].key = *(uint32_t *) key;
1904 sphb->msi_devs[i].value = *(spapr_pci_msi *) value;
1905 }
44b1ff31
DDAG
1906
1907 return 0;
9a321e92
AK
1908}
1909
1910static int spapr_pci_post_load(void *opaque, int version_id)
1911{
1912 sPAPRPHBState *sphb = opaque;
1913 gpointer key, value;
1914 int i;
1915
1916 for (i = 0; i < sphb->msi_devs_num; ++i) {
1917 key = g_memdup(&sphb->msi_devs[i].key,
1918 sizeof(sphb->msi_devs[i].key));
1919 value = g_memdup(&sphb->msi_devs[i].value,
1920 sizeof(sphb->msi_devs[i].value));
1921 g_hash_table_insert(sphb->msi, key, value);
1922 }
012aef07
MA
1923 g_free(sphb->msi_devs);
1924 sphb->msi_devs = NULL;
9a321e92
AK
1925 sphb->msi_devs_num = 0;
1926
1927 return 0;
1928}
1929
5c4537bd
DG
1930static bool pre_2_8_migration(void *opaque, int version_id)
1931{
1932 sPAPRPHBState *sphb = opaque;
1933
1934 return sphb->pre_2_8_migration;
1935}
1936
1112cf94
DG
1937static const VMStateDescription vmstate_spapr_pci = {
1938 .name = "spapr_pci",
5a78b821 1939 .version_id = 2,
9a321e92
AK
1940 .minimum_version_id = 2,
1941 .pre_save = spapr_pci_pre_save,
1942 .post_load = spapr_pci_post_load,
3aff6c2f 1943 .fields = (VMStateField[]) {
d2164ad3 1944 VMSTATE_UINT64_EQUAL(buid, sPAPRPHBState, NULL),
5c4537bd
DG
1945 VMSTATE_UINT32_TEST(mig_liobn, sPAPRPHBState, pre_2_8_migration),
1946 VMSTATE_UINT64_TEST(mig_mem_win_addr, sPAPRPHBState, pre_2_8_migration),
1947 VMSTATE_UINT64_TEST(mig_mem_win_size, sPAPRPHBState, pre_2_8_migration),
1948 VMSTATE_UINT64_TEST(mig_io_win_addr, sPAPRPHBState, pre_2_8_migration),
1949 VMSTATE_UINT64_TEST(mig_io_win_size, sPAPRPHBState, pre_2_8_migration),
1112cf94
DG
1950 VMSTATE_STRUCT_ARRAY(lsi_table, sPAPRPHBState, PCI_NUM_PINS, 0,
1951 vmstate_spapr_pci_lsi, struct spapr_pci_lsi),
9a321e92
AK
1952 VMSTATE_INT32(msi_devs_num, sPAPRPHBState),
1953 VMSTATE_STRUCT_VARRAY_ALLOC(msi_devs, sPAPRPHBState, msi_devs_num, 0,
1954 vmstate_spapr_pci_msi, spapr_pci_msi_mig),
1112cf94
DG
1955 VMSTATE_END_OF_LIST()
1956 },
1957};
1958
568f0690
DG
1959static const char *spapr_phb_root_bus_path(PCIHostState *host_bridge,
1960 PCIBus *rootbus)
1961{
1962 sPAPRPHBState *sphb = SPAPR_PCI_HOST_BRIDGE(host_bridge);
1963
1964 return sphb->dtbusname;
1965}
1966
298a9710
DG
1967static void spapr_phb_class_init(ObjectClass *klass, void *data)
1968{
568f0690 1969 PCIHostBridgeClass *hc = PCI_HOST_BRIDGE_CLASS(klass);
298a9710 1970 DeviceClass *dc = DEVICE_CLASS(klass);
7454c7af 1971 HotplugHandlerClass *hp = HOTPLUG_HANDLER_CLASS(klass);
298a9710 1972
568f0690 1973 hc->root_bus_path = spapr_phb_root_bus_path;
c6ba42f6 1974 dc->realize = spapr_phb_realize;
298a9710 1975 dc->props = spapr_phb_properties;
eddeed26 1976 dc->reset = spapr_phb_reset;
1112cf94 1977 dc->vmsd = &vmstate_spapr_pci;
e4f4fb1e
EH
1978 /* Supported by TYPE_SPAPR_MACHINE */
1979 dc->user_creatable = true;
09aa9a52 1980 set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories);
3340e5c4 1981 hp->plug = spapr_pci_plug;
27c1da51 1982 hp->unplug = spapr_pci_unplug;
3340e5c4 1983 hp->unplug_request = spapr_pci_unplug_request;
298a9710 1984}
3384f95c 1985
4240abff 1986static const TypeInfo spapr_phb_info = {
8c9f64df 1987 .name = TYPE_SPAPR_PCI_HOST_BRIDGE,
8558d942 1988 .parent = TYPE_PCI_HOST_BRIDGE,
298a9710
DG
1989 .instance_size = sizeof(sPAPRPHBState),
1990 .class_init = spapr_phb_class_init,
7454c7af
MR
1991 .interfaces = (InterfaceInfo[]) {
1992 { TYPE_HOTPLUG_HANDLER },
1993 { }
1994 }
298a9710
DG
1995};
1996
1d2d9742
ND
1997typedef struct sPAPRFDT {
1998 void *fdt;
1999 int node_off;
2000 sPAPRPHBState *sphb;
2001} sPAPRFDT;
2002
2003static void spapr_populate_pci_devices_dt(PCIBus *bus, PCIDevice *pdev,
2004 void *opaque)
2005{
2006 PCIBus *sec_bus;
2007 sPAPRFDT *p = opaque;
2008 int offset;
2009 sPAPRFDT s_fdt;
1d2d9742 2010
e634b89c 2011 offset = spapr_create_pci_child_dt(p->sphb, pdev, p->fdt, p->node_off);
1d2d9742
ND
2012 if (!offset) {
2013 error_report("Failed to create pci child device tree node");
2014 return;
2015 }
2016
2017 if ((pci_default_read_config(pdev, PCI_HEADER_TYPE, 1) !=
2018 PCI_HEADER_TYPE_BRIDGE)) {
2019 return;
2020 }
2021
2022 sec_bus = pci_bridge_get_sec_bus(PCI_BRIDGE(pdev));
2023 if (!sec_bus) {
2024 return;
2025 }
2026
2027 s_fdt.fdt = p->fdt;
2028 s_fdt.node_off = offset;
2029 s_fdt.sphb = p->sphb;
a8eeafda
GK
2030 pci_for_each_device_reverse(sec_bus, pci_bus_num(sec_bus),
2031 spapr_populate_pci_devices_dt,
2032 &s_fdt);
1d2d9742
ND
2033}
2034
2035static void spapr_phb_pci_enumerate_bridge(PCIBus *bus, PCIDevice *pdev,
2036 void *opaque)
2037{
2038 unsigned int *bus_no = opaque;
1d2d9742
ND
2039 PCIBus *sec_bus = NULL;
2040
2041 if ((pci_default_read_config(pdev, PCI_HEADER_TYPE, 1) !=
2042 PCI_HEADER_TYPE_BRIDGE)) {
2043 return;
2044 }
2045
2046 (*bus_no)++;
d8e81d6e 2047 pci_default_write_config(pdev, PCI_PRIMARY_BUS, pci_dev_bus_num(pdev), 1);
1d2d9742
ND
2048 pci_default_write_config(pdev, PCI_SECONDARY_BUS, *bus_no, 1);
2049 pci_default_write_config(pdev, PCI_SUBORDINATE_BUS, *bus_no, 1);
2050
2051 sec_bus = pci_bridge_get_sec_bus(PCI_BRIDGE(pdev));
2052 if (!sec_bus) {
2053 return;
2054 }
2055
1d2d9742
ND
2056 pci_for_each_device(sec_bus, pci_bus_num(sec_bus),
2057 spapr_phb_pci_enumerate_bridge, bus_no);
2058 pci_default_write_config(pdev, PCI_SUBORDINATE_BUS, *bus_no, 1);
2059}
2060
2061static void spapr_phb_pci_enumerate(sPAPRPHBState *phb)
2062{
2063 PCIBus *bus = PCI_HOST_BRIDGE(phb)->bus;
2064 unsigned int bus_no = 0;
2065
2066 pci_for_each_device(bus, pci_bus_num(bus),
2067 spapr_phb_pci_enumerate_bridge,
2068 &bus_no);
2069
2070}
2071
5c7adcf4 2072int spapr_populate_pci_dt(sPAPRPHBState *phb, uint32_t intc_phandle, void *fdt,
0976efd5 2073 uint32_t nr_msis)
3384f95c 2074{
62083979 2075 int bus_off, i, j, ret;
549ce59e 2076 gchar *nodename;
3384f95c
DG
2077 uint32_t bus_range[] = { cpu_to_be32(0), cpu_to_be32(0xff) };
2078 struct {
2079 uint32_t hi;
2080 uint64_t child;
2081 uint64_t parent;
2082 uint64_t size;
c4889f54 2083 } QEMU_PACKED ranges[] = {
3384f95c
DG
2084 {
2085 cpu_to_be32(b_ss(1)), cpu_to_be64(0),
2086 cpu_to_be64(phb->io_win_addr),
2087 cpu_to_be64(memory_region_size(&phb->iospace)),
2088 },
2089 {
2090 cpu_to_be32(b_ss(2)), cpu_to_be64(SPAPR_PCI_MEM_WIN_BUS_OFFSET),
2091 cpu_to_be64(phb->mem_win_addr),
daa23699 2092 cpu_to_be64(phb->mem_win_size),
b194df47
AK
2093 },
2094 {
daa23699
DG
2095 cpu_to_be32(b_ss(3)), cpu_to_be64(phb->mem64_win_pciaddr),
2096 cpu_to_be64(phb->mem64_win_addr),
2097 cpu_to_be64(phb->mem64_win_size),
3384f95c
DG
2098 },
2099 };
daa23699
DG
2100 const unsigned sizeof_ranges =
2101 (phb->mem64_win_size ? 3 : 2) * sizeof(ranges[0]);
3384f95c
DG
2102 uint64_t bus_reg[] = { cpu_to_be64(phb->buid), 0 };
2103 uint32_t interrupt_map_mask[] = {
7fb0bd34
DG
2104 cpu_to_be32(b_ddddd(-1)|b_fff(0)), 0x0, 0x0, cpu_to_be32(-1)};
2105 uint32_t interrupt_map[PCI_SLOT_MAX * PCI_NUM_PINS][7];
ae4de14c
AK
2106 uint32_t ddw_applicable[] = {
2107 cpu_to_be32(RTAS_IBM_QUERY_PE_DMA_WINDOW),
2108 cpu_to_be32(RTAS_IBM_CREATE_PE_DMA_WINDOW),
2109 cpu_to_be32(RTAS_IBM_REMOVE_PE_DMA_WINDOW)
2110 };
2111 uint32_t ddw_extensions[] = {
2112 cpu_to_be32(1),
2113 cpu_to_be32(RTAS_IBM_RESET_PE_DMA_WINDOW)
2114 };
4814401f
AK
2115 uint32_t associativity[] = {cpu_to_be32(0x4),
2116 cpu_to_be32(0x0),
2117 cpu_to_be32(0x0),
2118 cpu_to_be32(0x0),
2119 cpu_to_be32(phb->numa_node)};
ccf9ff85 2120 sPAPRTCETable *tcet;
1d2d9742
ND
2121 PCIBus *bus = PCI_HOST_BRIDGE(phb)->bus;
2122 sPAPRFDT s_fdt;
3384f95c
DG
2123
2124 /* Start populating the FDT */
549ce59e 2125 nodename = g_strdup_printf("pci@%" PRIx64, phb->buid);
9ba25536 2126 _FDT(bus_off = fdt_add_subnode(fdt, 0, nodename));
549ce59e 2127 g_free(nodename);
3384f95c 2128
3384f95c
DG
2129 /* Write PHB properties */
2130 _FDT(fdt_setprop_string(fdt, bus_off, "device_type", "pci"));
2131 _FDT(fdt_setprop_string(fdt, bus_off, "compatible", "IBM,Logical_PHB"));
2132 _FDT(fdt_setprop_cell(fdt, bus_off, "#address-cells", 0x3));
2133 _FDT(fdt_setprop_cell(fdt, bus_off, "#size-cells", 0x2));
2134 _FDT(fdt_setprop_cell(fdt, bus_off, "#interrupt-cells", 0x1));
2135 _FDT(fdt_setprop(fdt, bus_off, "used-by-rtas", NULL, 0));
2136 _FDT(fdt_setprop(fdt, bus_off, "bus-range", &bus_range, sizeof(bus_range)));
b194df47 2137 _FDT(fdt_setprop(fdt, bus_off, "ranges", &ranges, sizeof_ranges));
3384f95c 2138 _FDT(fdt_setprop(fdt, bus_off, "reg", &bus_reg, sizeof(bus_reg)));
3f7565c9 2139 _FDT(fdt_setprop_cell(fdt, bus_off, "ibm,pci-config-space-type", 0x1));
0976efd5 2140 _FDT(fdt_setprop_cell(fdt, bus_off, "ibm,pe-total-#msi", nr_msis));
3384f95c 2141
ae4de14c
AK
2142 /* Dynamic DMA window */
2143 if (phb->ddw_enabled) {
2144 _FDT(fdt_setprop(fdt, bus_off, "ibm,ddw-applicable", &ddw_applicable,
2145 sizeof(ddw_applicable)));
2146 _FDT(fdt_setprop(fdt, bus_off, "ibm,ddw-extensions",
2147 &ddw_extensions, sizeof(ddw_extensions)));
2148 }
2149
4814401f 2150 /* Advertise NUMA via ibm,associativity */
4bcfa56c 2151 if (phb->numa_node != -1) {
4814401f
AK
2152 _FDT(fdt_setprop(fdt, bus_off, "ibm,associativity", associativity,
2153 sizeof(associativity)));
2154 }
2155
4d8d5467
BH
2156 /* Build the interrupt-map, this must matches what is done
2157 * in pci_spapr_map_irq
2158 */
2159 _FDT(fdt_setprop(fdt, bus_off, "interrupt-map-mask",
2160 &interrupt_map_mask, sizeof(interrupt_map_mask)));
7fb0bd34
DG
2161 for (i = 0; i < PCI_SLOT_MAX; i++) {
2162 for (j = 0; j < PCI_NUM_PINS; j++) {
2163 uint32_t *irqmap = interrupt_map[i*PCI_NUM_PINS + j];
2164 int lsi_num = pci_spapr_swizzle(i, j);
2165
2166 irqmap[0] = cpu_to_be32(b_ddddd(i)|b_fff(0));
2167 irqmap[1] = 0;
2168 irqmap[2] = 0;
2169 irqmap[3] = cpu_to_be32(j+1);
5c7adcf4
GK
2170 irqmap[4] = cpu_to_be32(intc_phandle);
2171 spapr_dt_irq(&irqmap[5], phb->lsi_table[lsi_num].irq, true);
7fb0bd34 2172 }
3384f95c 2173 }
3384f95c
DG
2174 /* Write interrupt map */
2175 _FDT(fdt_setprop(fdt, bus_off, "interrupt-map", &interrupt_map,
7fb0bd34 2176 sizeof(interrupt_map)));
3384f95c 2177
ae4de14c 2178 tcet = spapr_tce_find_by_liobn(phb->dma_liobn[0]);
da34fed7
TH
2179 if (!tcet) {
2180 return -1;
2181 }
ccf9ff85
AK
2182 spapr_dma_dt(fdt, bus_off, "ibm,dma-window",
2183 tcet->liobn, tcet->bus_offset,
2184 tcet->nb_table << tcet->page_shift);
edded454 2185
1d2d9742
ND
2186 /* Walk the bridges and program the bus numbers*/
2187 spapr_phb_pci_enumerate(phb);
2188 _FDT(fdt_setprop_cell(fdt, bus_off, "qemu,phb-enumerated", 0x1));
2189
2190 /* Populate tree nodes with PCI devices attached */
2191 s_fdt.fdt = fdt;
2192 s_fdt.node_off = bus_off;
2193 s_fdt.sphb = phb;
a8eeafda
GK
2194 pci_for_each_device_reverse(bus, pci_bus_num(bus),
2195 spapr_populate_pci_devices_dt,
2196 &s_fdt);
1d2d9742 2197
62083979
MR
2198 ret = spapr_drc_populate_dt(fdt, bus_off, OBJECT(phb),
2199 SPAPR_DR_CONNECTOR_TYPE_PCI);
2200 if (ret) {
2201 return ret;
2202 }
2203
3384f95c
DG
2204 return 0;
2205}
298a9710 2206
fa28f71b
AK
2207void spapr_pci_rtas_init(void)
2208{
3a3b8502
AK
2209 spapr_rtas_register(RTAS_READ_PCI_CONFIG, "read-pci-config",
2210 rtas_read_pci_config);
2211 spapr_rtas_register(RTAS_WRITE_PCI_CONFIG, "write-pci-config",
2212 rtas_write_pci_config);
2213 spapr_rtas_register(RTAS_IBM_READ_PCI_CONFIG, "ibm,read-pci-config",
2214 rtas_ibm_read_pci_config);
2215 spapr_rtas_register(RTAS_IBM_WRITE_PCI_CONFIG, "ibm,write-pci-config",
2216 rtas_ibm_write_pci_config);
226419d6 2217 if (msi_nonbroken) {
3a3b8502
AK
2218 spapr_rtas_register(RTAS_IBM_QUERY_INTERRUPT_SOURCE_NUMBER,
2219 "ibm,query-interrupt-source-number",
0ee2c058 2220 rtas_ibm_query_interrupt_source_number);
3a3b8502
AK
2221 spapr_rtas_register(RTAS_IBM_CHANGE_MSI, "ibm,change-msi",
2222 rtas_ibm_change_msi);
0ee2c058 2223 }
ee954280
GS
2224
2225 spapr_rtas_register(RTAS_IBM_SET_EEH_OPTION,
2226 "ibm,set-eeh-option",
2227 rtas_ibm_set_eeh_option);
2228 spapr_rtas_register(RTAS_IBM_GET_CONFIG_ADDR_INFO2,
2229 "ibm,get-config-addr-info2",
2230 rtas_ibm_get_config_addr_info2);
2231 spapr_rtas_register(RTAS_IBM_READ_SLOT_RESET_STATE2,
2232 "ibm,read-slot-reset-state2",
2233 rtas_ibm_read_slot_reset_state2);
2234 spapr_rtas_register(RTAS_IBM_SET_SLOT_RESET,
2235 "ibm,set-slot-reset",
2236 rtas_ibm_set_slot_reset);
2237 spapr_rtas_register(RTAS_IBM_CONFIGURE_PE,
2238 "ibm,configure-pe",
2239 rtas_ibm_configure_pe);
2240 spapr_rtas_register(RTAS_IBM_SLOT_ERROR_DETAIL,
2241 "ibm,slot-error-detail",
2242 rtas_ibm_slot_error_detail);
fa28f71b
AK
2243}
2244
8c9f64df 2245static void spapr_pci_register_types(void)
298a9710
DG
2246{
2247 type_register_static(&spapr_phb_info);
2248}
8c9f64df
AF
2249
2250type_init(spapr_pci_register_types)
eefaccc0
DG
2251
2252static int spapr_switch_one_vga(DeviceState *dev, void *opaque)
2253{
2254 bool be = *(bool *)opaque;
2255
2256 if (object_dynamic_cast(OBJECT(dev), "VGA")
2257 || object_dynamic_cast(OBJECT(dev), "secondary-vga")) {
2258 object_property_set_bool(OBJECT(dev), be, "big-endian-framebuffer",
2259 &error_abort);
2260 }
2261 return 0;
2262}
2263
2264void spapr_pci_switch_vga(bool big_endian)
2265{
28e02042 2266 sPAPRMachineState *spapr = SPAPR_MACHINE(qdev_get_machine());
eefaccc0
DG
2267 sPAPRPHBState *sphb;
2268
2269 /*
2270 * For backward compatibility with existing guests, we switch
2271 * the endianness of the VGA controller when changing the guest
2272 * interrupt mode
2273 */
2274 QLIST_FOREACH(sphb, &spapr->phbs, list) {
2275 BusState *bus = &PCI_HOST_BRIDGE(sphb)->bus->qbus;
2276 qbus_walk_children(bus, spapr_switch_one_vga, NULL, NULL, NULL,
2277 &big_endian);
2278 }
2279}