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