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