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