xref: /qemu/include/hw/pci-host/spapr.h (revision ec132efaa81f09861a3bd6afad94827e74543b3f)
13384f95cSDavid Gibson /*
23384f95cSDavid Gibson  * QEMU SPAPR PCI BUS definitions
33384f95cSDavid Gibson  *
43384f95cSDavid Gibson  * Copyright (c) 2011 Alexey Kardashevskiy <aik@au1.ibm.com>
53384f95cSDavid Gibson  *
63384f95cSDavid Gibson  * This library is free software; you can redistribute it and/or
73384f95cSDavid Gibson  * modify it under the terms of the GNU Lesser General Public
83384f95cSDavid Gibson  * License as published by the Free Software Foundation; either
93384f95cSDavid Gibson  * version 2 of the License, or (at your option) any later version.
103384f95cSDavid Gibson  *
113384f95cSDavid Gibson  * This library is distributed in the hope that it will be useful,
123384f95cSDavid Gibson  * but WITHOUT ANY WARRANTY; without even the implied warranty of
133384f95cSDavid Gibson  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
143384f95cSDavid Gibson  * Lesser General Public License for more details.
153384f95cSDavid Gibson  *
163384f95cSDavid Gibson  * You should have received a copy of the GNU Lesser General Public
173384f95cSDavid Gibson  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
183384f95cSDavid Gibson  */
193384f95cSDavid Gibson 
20121d0712SMarkus Armbruster #ifndef PCI_HOST_SPAPR_H
21121d0712SMarkus Armbruster #define PCI_HOST_SPAPR_H
223384f95cSDavid Gibson 
2320668fdeSMarkus Armbruster #include "hw/ppc/spapr.h"
24a2cb15b0SMichael S. Tsirkin #include "hw/pci/pci.h"
25a2cb15b0SMichael S. Tsirkin #include "hw/pci/pci_host.h"
260d09e41aSPaolo Bonzini #include "hw/ppc/xics.h"
273384f95cSDavid Gibson 
288c9f64dfSAndreas Färber #define TYPE_SPAPR_PCI_HOST_BRIDGE "spapr-pci-host-bridge"
298c9f64dfSAndreas Färber 
308c9f64dfSAndreas Färber #define SPAPR_PCI_HOST_BRIDGE(obj) \
31ce2918cbSDavid Gibson     OBJECT_CHECK(SpaprPhbState, (obj), TYPE_SPAPR_PCI_HOST_BRIDGE)
328c9f64dfSAndreas Färber 
33ae4de14cSAlexey Kardashevskiy #define SPAPR_PCI_DMA_MAX_WINDOWS    2
34ae4de14cSAlexey Kardashevskiy 
35ce2918cbSDavid Gibson typedef struct SpaprPhbState SpaprPhbState;
36da6ccee4SAlexey Kardashevskiy 
379a321e92SAlexey Kardashevskiy typedef struct spapr_pci_msi {
389a321e92SAlexey Kardashevskiy     uint32_t first_irq;
399a321e92SAlexey Kardashevskiy     uint32_t num;
409a321e92SAlexey Kardashevskiy } spapr_pci_msi;
419a321e92SAlexey Kardashevskiy 
429a321e92SAlexey Kardashevskiy typedef struct spapr_pci_msi_mig {
439a321e92SAlexey Kardashevskiy     uint32_t key;
449a321e92SAlexey Kardashevskiy     spapr_pci_msi value;
459a321e92SAlexey Kardashevskiy } spapr_pci_msi_mig;
469a321e92SAlexey Kardashevskiy 
47ce2918cbSDavid Gibson struct SpaprPhbState {
4867c332fdSAndreas Färber     PCIHostState parent_obj;
493384f95cSDavid Gibson 
503e4ac968SDavid Gibson     uint32_t index;
513384f95cSDavid Gibson     uint64_t buid;
52298a9710SDavid Gibson     char *dtbusname;
537619c7b0SMichael Roth     bool dr_enabled;
543384f95cSDavid Gibson 
553384f95cSDavid Gibson     MemoryRegion memspace, iospace;
56daa23699SDavid Gibson     hwaddr mem_win_addr, mem_win_size, mem64_win_addr, mem64_win_size;
57daa23699SDavid Gibson     uint64_t mem64_win_pciaddr;
58daa23699SDavid Gibson     hwaddr io_win_addr, io_win_size;
59daa23699SDavid Gibson     MemoryRegion mem32window, mem64window, iowindow, msiwindow;
600ee2c058SAlexey Kardashevskiy 
61ae4de14cSAlexey Kardashevskiy     uint32_t dma_liobn[SPAPR_PCI_DMA_MAX_WINDOWS];
62f93caaacSDavid Gibson     hwaddr dma_win_addr, dma_win_size;
63e00387d5SAvi Kivity     AddressSpace iommu_as;
64cca7fad5SAlexey Kardashevskiy     MemoryRegion iommu_root;
653384f95cSDavid Gibson 
661112cf94SDavid Gibson     struct spapr_pci_lsi {
67a307d594SAlexey Kardashevskiy         uint32_t irq;
687fb0bd34SDavid Gibson     } lsi_table[PCI_NUM_PINS];
693384f95cSDavid Gibson 
709a321e92SAlexey Kardashevskiy     GHashTable *msi;
719a321e92SAlexey Kardashevskiy     /* Temporary cache for migration purposes */
729a321e92SAlexey Kardashevskiy     int32_t msi_devs_num;
739a321e92SAlexey Kardashevskiy     spapr_pci_msi_mig *msi_devs;
740ee2c058SAlexey Kardashevskiy 
75ce2918cbSDavid Gibson     QLIST_ENTRY(SpaprPhbState) list;
76ae4de14cSAlexey Kardashevskiy 
77ae4de14cSAlexey Kardashevskiy     bool ddw_enabled;
78ae4de14cSAlexey Kardashevskiy     uint64_t page_size_mask;
79ae4de14cSAlexey Kardashevskiy     uint64_t dma64_win_addr;
804814401fSAlexey Kardashevskiy 
814814401fSAlexey Kardashevskiy     uint32_t numa_node;
825c4537bdSDavid Gibson 
8382516263SDavid Gibson     bool pcie_ecs; /* Allow access to PCIe extended config space? */
8482516263SDavid Gibson 
855c4537bdSDavid Gibson     /* Fields for migration compatibility hacks */
865c4537bdSDavid Gibson     bool pre_2_8_migration;
875c4537bdSDavid Gibson     uint32_t mig_liobn;
885c4537bdSDavid Gibson     hwaddr mig_mem_win_addr, mig_mem_win_size;
895c4537bdSDavid Gibson     hwaddr mig_io_win_addr, mig_io_win_size;
90*ec132efaSAlexey Kardashevskiy     hwaddr nv2_gpa_win_addr;
91*ec132efaSAlexey Kardashevskiy     hwaddr nv2_atsd_win_addr;
92*ec132efaSAlexey Kardashevskiy     struct spapr_phb_pci_nvgpu_config *nvgpus;
93da6ccee4SAlexey Kardashevskiy };
943384f95cSDavid Gibson 
95b194df47SAlexey Kardashevskiy #define SPAPR_PCI_MEM_WIN_BUS_OFFSET 0x80000000ULL
96daa23699SDavid Gibson #define SPAPR_PCI_MEM32_WIN_SIZE     \
97daa23699SDavid Gibson     ((1ULL << 32) - SPAPR_PCI_MEM_WIN_BUS_OFFSET)
98357d1e3bSDavid Gibson #define SPAPR_PCI_MEM64_WIN_SIZE     0x10000000000ULL /* 1 TiB */
99b194df47SAlexey Kardashevskiy 
1001da85c2aSGreg Kurz /* All PCI outbound windows will be within this range */
101357d1e3bSDavid Gibson #define SPAPR_PCI_BASE               (1ULL << 45) /* 32 TiB */
102357d1e3bSDavid Gibson #define SPAPR_PCI_LIMIT              (1ULL << 46) /* 64 TiB */
103357d1e3bSDavid Gibson 
1041da85c2aSGreg Kurz #define SPAPR_MAX_PHBS ((SPAPR_PCI_LIMIT - SPAPR_PCI_BASE) / \
1051da85c2aSGreg Kurz                         SPAPR_PCI_MEM64_WIN_SIZE - 1)
1061da85c2aSGreg Kurz 
107caae58cbSDavid Gibson #define SPAPR_PCI_IO_WIN_SIZE        0x10000
108f1c2dc7cSAlexey Kardashevskiy 
109f1c2dc7cSAlexey Kardashevskiy #define SPAPR_PCI_MSI_WINDOW         0x40000000000ULL
110caae58cbSDavid Gibson 
111*ec132efaSAlexey Kardashevskiy #define SPAPR_PCI_NV2RAM64_WIN_BASE  SPAPR_PCI_LIMIT
112*ec132efaSAlexey Kardashevskiy #define SPAPR_PCI_NV2RAM64_WIN_SIZE  (2 * TiB) /* For up to 6 GPUs 256GB each */
113*ec132efaSAlexey Kardashevskiy 
114*ec132efaSAlexey Kardashevskiy /* Max number of these GPUsper a physical box */
115*ec132efaSAlexey Kardashevskiy #define NVGPU_MAX_NUM                6
116*ec132efaSAlexey Kardashevskiy /* Max number of NVLinks per GPU in any physical box */
117*ec132efaSAlexey Kardashevskiy #define NVGPU_MAX_LINKS              3
118*ec132efaSAlexey Kardashevskiy 
119*ec132efaSAlexey Kardashevskiy /*
120*ec132efaSAlexey Kardashevskiy  * GPU RAM starts at 64TiB so huge DMA window to cover it all ends at 128TiB
121*ec132efaSAlexey Kardashevskiy  * which is enough. We do not need DMA for ATSD so we put them at 128TiB.
122*ec132efaSAlexey Kardashevskiy  */
123*ec132efaSAlexey Kardashevskiy #define SPAPR_PCI_NV2ATSD_WIN_BASE   (128 * TiB)
124*ec132efaSAlexey Kardashevskiy #define SPAPR_PCI_NV2ATSD_WIN_SIZE   (NVGPU_MAX_NUM * NVGPU_MAX_LINKS * \
125*ec132efaSAlexey Kardashevskiy                                       64 * KiB)
126*ec132efaSAlexey Kardashevskiy 
127ce2918cbSDavid Gibson static inline qemu_irq spapr_phb_lsi_qirq(struct SpaprPhbState *phb, int pin)
128a307d594SAlexey Kardashevskiy {
129ce2918cbSDavid Gibson     SpaprMachineState *spapr = SPAPR_MACHINE(qdev_get_machine());
13028e02042SDavid Gibson 
13177183755SCédric Le Goater     return spapr_qirq(spapr, phb->lsi_table[pin].irq);
132a307d594SAlexey Kardashevskiy }
133a307d594SAlexey Kardashevskiy 
134ce2918cbSDavid Gibson int spapr_populate_pci_dt(SpaprPhbState *phb, uint32_t intc_phandle, void *fdt,
1350a0a66cdSMichael Roth                           uint32_t nr_msis, int *node_offset);
1363384f95cSDavid Gibson 
137fa28f71bSAlexey Kardashevskiy void spapr_pci_rtas_init(void);
138fa28f71bSAlexey Kardashevskiy 
139ce2918cbSDavid Gibson SpaprPhbState *spapr_pci_find_phb(SpaprMachineState *spapr, uint64_t buid);
140ce2918cbSDavid Gibson PCIDevice *spapr_pci_find_dev(SpaprMachineState *spapr, uint64_t buid,
14146c5874eSAlexey Kardashevskiy                               uint32_t config_addr);
14246c5874eSAlexey Kardashevskiy 
14346fd0299SGreg Kurz /* DRC callbacks */
14431834723SDaniel Henrique Barboza void spapr_phb_remove_pci_device_cb(DeviceState *dev);
145ce2918cbSDavid Gibson int spapr_pci_dt_populate(SpaprDrc *drc, SpaprMachineState *spapr,
14646fd0299SGreg Kurz                           void *fdt, int *fdt_start_offset, Error **errp);
14731834723SDaniel Henrique Barboza 
148fbb4e983SDavid Gibson /* VFIO EEH hooks */
149fbb4e983SDavid Gibson #ifdef CONFIG_LINUX
150ce2918cbSDavid Gibson bool spapr_phb_eeh_available(SpaprPhbState *sphb);
151ce2918cbSDavid Gibson int spapr_phb_vfio_eeh_set_option(SpaprPhbState *sphb,
152fbb4e983SDavid Gibson                                   unsigned int addr, int option);
153ce2918cbSDavid Gibson int spapr_phb_vfio_eeh_get_state(SpaprPhbState *sphb, int *state);
154ce2918cbSDavid Gibson int spapr_phb_vfio_eeh_reset(SpaprPhbState *sphb, int option);
155ce2918cbSDavid Gibson int spapr_phb_vfio_eeh_configure(SpaprPhbState *sphb);
156fbb4e983SDavid Gibson void spapr_phb_vfio_reset(DeviceState *qdev);
157*ec132efaSAlexey Kardashevskiy void spapr_phb_nvgpu_setup(SpaprPhbState *sphb, Error **errp);
158*ec132efaSAlexey Kardashevskiy void spapr_phb_nvgpu_free(SpaprPhbState *sphb);
159*ec132efaSAlexey Kardashevskiy void spapr_phb_nvgpu_populate_dt(SpaprPhbState *sphb, void *fdt, int bus_off,
160*ec132efaSAlexey Kardashevskiy                                  Error **errp);
161*ec132efaSAlexey Kardashevskiy void spapr_phb_nvgpu_ram_populate_dt(SpaprPhbState *sphb, void *fdt);
162*ec132efaSAlexey Kardashevskiy void spapr_phb_nvgpu_populate_pcidev_dt(PCIDevice *dev, void *fdt, int offset,
163*ec132efaSAlexey Kardashevskiy                                         SpaprPhbState *sphb);
164fbb4e983SDavid Gibson #else
165ce2918cbSDavid Gibson static inline bool spapr_phb_eeh_available(SpaprPhbState *sphb)
166c1fa017cSDavid Gibson {
167c1fa017cSDavid Gibson     return false;
168c1fa017cSDavid Gibson }
169ce2918cbSDavid Gibson static inline int spapr_phb_vfio_eeh_set_option(SpaprPhbState *sphb,
170fbb4e983SDavid Gibson                                                 unsigned int addr, int option)
171fbb4e983SDavid Gibson {
172fbb4e983SDavid Gibson     return RTAS_OUT_HW_ERROR;
173fbb4e983SDavid Gibson }
174ce2918cbSDavid Gibson static inline int spapr_phb_vfio_eeh_get_state(SpaprPhbState *sphb,
175fbb4e983SDavid Gibson                                                int *state)
176fbb4e983SDavid Gibson {
177fbb4e983SDavid Gibson     return RTAS_OUT_HW_ERROR;
178fbb4e983SDavid Gibson }
179ce2918cbSDavid Gibson static inline int spapr_phb_vfio_eeh_reset(SpaprPhbState *sphb, int option)
180fbb4e983SDavid Gibson {
181fbb4e983SDavid Gibson     return RTAS_OUT_HW_ERROR;
182fbb4e983SDavid Gibson }
183ce2918cbSDavid Gibson static inline int spapr_phb_vfio_eeh_configure(SpaprPhbState *sphb)
184fbb4e983SDavid Gibson {
185fbb4e983SDavid Gibson     return RTAS_OUT_HW_ERROR;
186fbb4e983SDavid Gibson }
187fbb4e983SDavid Gibson static inline void spapr_phb_vfio_reset(DeviceState *qdev)
188fbb4e983SDavid Gibson {
189fbb4e983SDavid Gibson }
190*ec132efaSAlexey Kardashevskiy static inline void spapr_phb_nvgpu_setup(SpaprPhbState *sphb, Error **errp)
191*ec132efaSAlexey Kardashevskiy {
192*ec132efaSAlexey Kardashevskiy }
193*ec132efaSAlexey Kardashevskiy static inline void spapr_phb_nvgpu_free(SpaprPhbState *sphb)
194*ec132efaSAlexey Kardashevskiy {
195*ec132efaSAlexey Kardashevskiy }
196*ec132efaSAlexey Kardashevskiy static inline void spapr_phb_nvgpu_populate_dt(SpaprPhbState *sphb, void *fdt,
197*ec132efaSAlexey Kardashevskiy                                                int bus_off, Error **errp)
198*ec132efaSAlexey Kardashevskiy {
199*ec132efaSAlexey Kardashevskiy }
200*ec132efaSAlexey Kardashevskiy static inline void spapr_phb_nvgpu_ram_populate_dt(SpaprPhbState *sphb,
201*ec132efaSAlexey Kardashevskiy                                                    void *fdt)
202*ec132efaSAlexey Kardashevskiy {
203*ec132efaSAlexey Kardashevskiy }
204*ec132efaSAlexey Kardashevskiy static inline void spapr_phb_nvgpu_populate_pcidev_dt(PCIDevice *dev, void *fdt,
205*ec132efaSAlexey Kardashevskiy                                                       int offset,
206*ec132efaSAlexey Kardashevskiy                                                       SpaprPhbState *sphb)
207*ec132efaSAlexey Kardashevskiy {
208*ec132efaSAlexey Kardashevskiy }
209fbb4e983SDavid Gibson #endif
210fbb4e983SDavid Gibson 
211ce2918cbSDavid Gibson void spapr_phb_dma_reset(SpaprPhbState *sphb);
212b3162f22SAlexey Kardashevskiy 
213ce2918cbSDavid Gibson static inline unsigned spapr_phb_windows_supported(SpaprPhbState *sphb)
214ef28b98dSGreg Kurz {
215ef28b98dSGreg Kurz     return sphb->ddw_enabled ? SPAPR_PCI_DMA_MAX_WINDOWS : 1;
216ef28b98dSGreg Kurz }
217ef28b98dSGreg Kurz 
218121d0712SMarkus Armbruster #endif /* PCI_HOST_SPAPR_H */
219