xref: /openbmc/qemu/hw/pci-host/pnv_phb3_pbcq.c (revision 19f70347)
1 /*
2  * QEMU PowerPC PowerNV (POWER8) PHB3 model
3  *
4  * Copyright (c) 2014-2020, IBM Corporation.
5  *
6  * This code is licensed under the GPL version 2 or later. See the
7  * COPYING file in the top-level directory.
8  */
9 #include "qemu/osdep.h"
10 #include "qapi/error.h"
11 #include "qemu-common.h"
12 #include "qemu/log.h"
13 #include "target/ppc/cpu.h"
14 #include "hw/ppc/fdt.h"
15 #include "hw/pci-host/pnv_phb3_regs.h"
16 #include "hw/pci-host/pnv_phb3.h"
17 #include "hw/ppc/pnv.h"
18 #include "hw/ppc/pnv_xscom.h"
19 #include "hw/pci/pci_bridge.h"
20 #include "hw/pci/pci_bus.h"
21 
22 #include <libfdt.h>
23 
24 #define phb3_pbcq_error(pbcq, fmt, ...)                                 \
25     qemu_log_mask(LOG_GUEST_ERROR, "phb3_pbcq[%d:%d]: " fmt "\n",       \
26                   (pbcq)->phb->chip_id, (pbcq)->phb->phb_id, ## __VA_ARGS__)
27 
28 static uint64_t pnv_pbcq_nest_xscom_read(void *opaque, hwaddr addr,
29                                          unsigned size)
30 {
31     PnvPBCQState *pbcq = PNV_PBCQ(opaque);
32     uint32_t offset = addr >> 3;
33 
34     return pbcq->nest_regs[offset];
35 }
36 
37 static uint64_t pnv_pbcq_pci_xscom_read(void *opaque, hwaddr addr,
38                                         unsigned size)
39 {
40     PnvPBCQState *pbcq = PNV_PBCQ(opaque);
41     uint32_t offset = addr >> 3;
42 
43     return pbcq->pci_regs[offset];
44 }
45 
46 static uint64_t pnv_pbcq_spci_xscom_read(void *opaque, hwaddr addr,
47                                          unsigned size)
48 {
49     PnvPBCQState *pbcq = PNV_PBCQ(opaque);
50     uint32_t offset = addr >> 3;
51 
52     if (offset == PBCQ_SPCI_ASB_DATA) {
53         return pnv_phb3_reg_read(pbcq->phb,
54                                  pbcq->spci_regs[PBCQ_SPCI_ASB_ADDR], 8);
55     }
56     return pbcq->spci_regs[offset];
57 }
58 
59 static void pnv_pbcq_update_map(PnvPBCQState *pbcq)
60 {
61     uint64_t bar_en = pbcq->nest_regs[PBCQ_NEST_BAR_EN];
62     uint64_t bar, mask, size;
63 
64     /*
65      * NOTE: This will really not work well if those are remapped
66      * after the PHB has created its sub regions. We could do better
67      * if we had a way to resize regions but we don't really care
68      * that much in practice as the stuff below really only happens
69      * once early during boot
70      */
71 
72     /* Handle unmaps */
73     if (memory_region_is_mapped(&pbcq->mmbar0) &&
74         !(bar_en & PBCQ_NEST_BAR_EN_MMIO0)) {
75         memory_region_del_subregion(get_system_memory(), &pbcq->mmbar0);
76     }
77     if (memory_region_is_mapped(&pbcq->mmbar1) &&
78         !(bar_en & PBCQ_NEST_BAR_EN_MMIO1)) {
79         memory_region_del_subregion(get_system_memory(), &pbcq->mmbar1);
80     }
81     if (memory_region_is_mapped(&pbcq->phbbar) &&
82         !(bar_en & PBCQ_NEST_BAR_EN_PHB)) {
83         memory_region_del_subregion(get_system_memory(), &pbcq->phbbar);
84     }
85 
86     /* Update PHB */
87     pnv_phb3_update_regions(pbcq->phb);
88 
89     /* Handle maps */
90     if (!memory_region_is_mapped(&pbcq->mmbar0) &&
91         (bar_en & PBCQ_NEST_BAR_EN_MMIO0)) {
92         bar = pbcq->nest_regs[PBCQ_NEST_MMIO_BAR0] >> 14;
93         mask = pbcq->nest_regs[PBCQ_NEST_MMIO_MASK0];
94         size = ((~mask) >> 14) + 1;
95         memory_region_init(&pbcq->mmbar0, OBJECT(pbcq), "pbcq-mmio0", size);
96         memory_region_add_subregion(get_system_memory(), bar, &pbcq->mmbar0);
97         pbcq->mmio0_base = bar;
98         pbcq->mmio0_size = size;
99     }
100     if (!memory_region_is_mapped(&pbcq->mmbar1) &&
101         (bar_en & PBCQ_NEST_BAR_EN_MMIO1)) {
102         bar = pbcq->nest_regs[PBCQ_NEST_MMIO_BAR1] >> 14;
103         mask = pbcq->nest_regs[PBCQ_NEST_MMIO_MASK1];
104         size = ((~mask) >> 14) + 1;
105         memory_region_init(&pbcq->mmbar1, OBJECT(pbcq), "pbcq-mmio1", size);
106         memory_region_add_subregion(get_system_memory(), bar, &pbcq->mmbar1);
107         pbcq->mmio1_base = bar;
108         pbcq->mmio1_size = size;
109     }
110     if (!memory_region_is_mapped(&pbcq->phbbar)
111         && (bar_en & PBCQ_NEST_BAR_EN_PHB)) {
112         bar = pbcq->nest_regs[PBCQ_NEST_PHB_BAR] >> 14;
113         size = 0x1000;
114         memory_region_init(&pbcq->phbbar, OBJECT(pbcq), "pbcq-phb", size);
115         memory_region_add_subregion(get_system_memory(), bar, &pbcq->phbbar);
116     }
117 
118     /* Update PHB */
119     pnv_phb3_update_regions(pbcq->phb);
120 }
121 
122 static void pnv_pbcq_nest_xscom_write(void *opaque, hwaddr addr,
123                                 uint64_t val, unsigned size)
124 {
125     PnvPBCQState *pbcq = PNV_PBCQ(opaque);
126     uint32_t reg = addr >> 3;
127 
128     switch (reg) {
129     case PBCQ_NEST_MMIO_BAR0:
130     case PBCQ_NEST_MMIO_BAR1:
131     case PBCQ_NEST_MMIO_MASK0:
132     case PBCQ_NEST_MMIO_MASK1:
133         if (pbcq->nest_regs[PBCQ_NEST_BAR_EN] &
134             (PBCQ_NEST_BAR_EN_MMIO0 |
135              PBCQ_NEST_BAR_EN_MMIO1)) {
136             phb3_pbcq_error(pbcq, "Changing enabled BAR unsupported");
137         }
138         pbcq->nest_regs[reg] = val & 0xffffffffc0000000ull;
139         break;
140     case PBCQ_NEST_PHB_BAR:
141         if (pbcq->nest_regs[PBCQ_NEST_BAR_EN] & PBCQ_NEST_BAR_EN_PHB) {
142             phb3_pbcq_error(pbcq, "Changing enabled BAR unsupported");
143         }
144         pbcq->nest_regs[reg] = val & 0xfffffffffc000000ull;
145         break;
146     case PBCQ_NEST_BAR_EN:
147         pbcq->nest_regs[reg] = val & 0xf800000000000000ull;
148         pnv_pbcq_update_map(pbcq);
149         pnv_phb3_remap_irqs(pbcq->phb);
150         break;
151     case PBCQ_NEST_IRSN_COMPARE:
152     case PBCQ_NEST_IRSN_MASK:
153         pbcq->nest_regs[reg] = val & PBCQ_NEST_IRSN_COMP;
154         pnv_phb3_remap_irqs(pbcq->phb);
155         break;
156     case PBCQ_NEST_LSI_SRC_ID:
157         pbcq->nest_regs[reg] = val & PBCQ_NEST_LSI_SRC;
158         pnv_phb3_remap_irqs(pbcq->phb);
159         break;
160     default:
161         phb3_pbcq_error(pbcq, "%s @0x%"HWADDR_PRIx"=%"PRIx64, __func__,
162                         addr, val);
163     }
164 }
165 
166 static void pnv_pbcq_pci_xscom_write(void *opaque, hwaddr addr,
167                                      uint64_t val, unsigned size)
168 {
169     PnvPBCQState *pbcq = PNV_PBCQ(opaque);
170     uint32_t reg = addr >> 3;
171 
172     switch (reg) {
173     case PBCQ_PCI_BAR2:
174         pbcq->pci_regs[reg] = val & 0xfffffffffc000000ull;
175         pnv_pbcq_update_map(pbcq);
176     default:
177         phb3_pbcq_error(pbcq, "%s @0x%"HWADDR_PRIx"=%"PRIx64, __func__,
178                         addr, val);
179     }
180 }
181 
182 static void pnv_pbcq_spci_xscom_write(void *opaque, hwaddr addr,
183                                 uint64_t val, unsigned size)
184 {
185     PnvPBCQState *pbcq = PNV_PBCQ(opaque);
186     uint32_t reg = addr >> 3;
187 
188     switch (reg) {
189     case PBCQ_SPCI_ASB_ADDR:
190         pbcq->spci_regs[reg] = val & 0xfff;
191         break;
192     case PBCQ_SPCI_ASB_STATUS:
193         pbcq->spci_regs[reg] &= ~val;
194         break;
195     case PBCQ_SPCI_ASB_DATA:
196         pnv_phb3_reg_write(pbcq->phb, pbcq->spci_regs[PBCQ_SPCI_ASB_ADDR],
197                            val, 8);
198         break;
199     case PBCQ_SPCI_AIB_CAPP_EN:
200     case PBCQ_SPCI_CAPP_SEC_TMR:
201         break;
202     default:
203         phb3_pbcq_error(pbcq, "%s @0x%"HWADDR_PRIx"=%"PRIx64, __func__,
204                         addr, val);
205     }
206 }
207 
208 static const MemoryRegionOps pnv_pbcq_nest_xscom_ops = {
209     .read = pnv_pbcq_nest_xscom_read,
210     .write = pnv_pbcq_nest_xscom_write,
211     .valid.min_access_size = 8,
212     .valid.max_access_size = 8,
213     .impl.min_access_size = 8,
214     .impl.max_access_size = 8,
215     .endianness = DEVICE_BIG_ENDIAN,
216 };
217 
218 static const MemoryRegionOps pnv_pbcq_pci_xscom_ops = {
219     .read = pnv_pbcq_pci_xscom_read,
220     .write = pnv_pbcq_pci_xscom_write,
221     .valid.min_access_size = 8,
222     .valid.max_access_size = 8,
223     .impl.min_access_size = 8,
224     .impl.max_access_size = 8,
225     .endianness = DEVICE_BIG_ENDIAN,
226 };
227 
228 static const MemoryRegionOps pnv_pbcq_spci_xscom_ops = {
229     .read = pnv_pbcq_spci_xscom_read,
230     .write = pnv_pbcq_spci_xscom_write,
231     .valid.min_access_size = 8,
232     .valid.max_access_size = 8,
233     .impl.min_access_size = 8,
234     .impl.max_access_size = 8,
235     .endianness = DEVICE_BIG_ENDIAN,
236 };
237 
238 static void pnv_pbcq_default_bars(PnvPBCQState *pbcq)
239 {
240     uint64_t mm0, mm1, reg;
241     PnvPHB3 *phb = pbcq->phb;
242 
243     mm0 = 0x3d00000000000ull + 0x4000000000ull * phb->chip_id +
244             0x1000000000ull * phb->phb_id;
245     mm1 = 0x3ff8000000000ull + 0x0200000000ull * phb->chip_id +
246             0x0080000000ull * phb->phb_id;
247     reg = 0x3fffe40000000ull + 0x0000400000ull * phb->chip_id +
248             0x0000100000ull * phb->phb_id;
249 
250     pbcq->nest_regs[PBCQ_NEST_MMIO_BAR0] = mm0 << 14;
251     pbcq->nest_regs[PBCQ_NEST_MMIO_BAR1] = mm1 << 14;
252     pbcq->nest_regs[PBCQ_NEST_PHB_BAR] = reg << 14;
253     pbcq->nest_regs[PBCQ_NEST_MMIO_MASK0] = 0x3fff000000000ull << 14;
254     pbcq->nest_regs[PBCQ_NEST_MMIO_MASK1] = 0x3ffff80000000ull << 14;
255     pbcq->pci_regs[PBCQ_PCI_BAR2] = reg << 14;
256 }
257 
258 static void pnv_pbcq_realize(DeviceState *dev, Error **errp)
259 {
260     PnvPBCQState *pbcq = PNV_PBCQ(dev);
261     PnvPHB3 *phb;
262     char name[32];
263 
264     assert(pbcq->phb);
265     phb = pbcq->phb;
266 
267     /* TODO: Fix OPAL to do that: establish default BAR values */
268     pnv_pbcq_default_bars(pbcq);
269 
270     /* Initialize the XSCOM region for the PBCQ registers */
271     snprintf(name, sizeof(name), "xscom-pbcq-nest-%d.%d",
272              phb->chip_id, phb->phb_id);
273     pnv_xscom_region_init(&pbcq->xscom_nest_regs, OBJECT(dev),
274                           &pnv_pbcq_nest_xscom_ops, pbcq, name,
275                           PNV_XSCOM_PBCQ_NEST_SIZE);
276     snprintf(name, sizeof(name), "xscom-pbcq-pci-%d.%d",
277              phb->chip_id, phb->phb_id);
278     pnv_xscom_region_init(&pbcq->xscom_pci_regs, OBJECT(dev),
279                           &pnv_pbcq_pci_xscom_ops, pbcq, name,
280                           PNV_XSCOM_PBCQ_PCI_SIZE);
281     snprintf(name, sizeof(name), "xscom-pbcq-spci-%d.%d",
282              phb->chip_id, phb->phb_id);
283     pnv_xscom_region_init(&pbcq->xscom_spci_regs, OBJECT(dev),
284                           &pnv_pbcq_spci_xscom_ops, pbcq, name,
285                           PNV_XSCOM_PBCQ_SPCI_SIZE);
286 }
287 
288 static int pnv_pbcq_dt_xscom(PnvXScomInterface *dev, void *fdt,
289                              int xscom_offset)
290 {
291     const char compat[] = "ibm,power8-pbcq";
292     PnvPHB3 *phb = PNV_PBCQ(dev)->phb;
293     char *name;
294     int offset;
295     uint32_t lpc_pcba = PNV_XSCOM_PBCQ_NEST_BASE + 0x400 * phb->phb_id;
296     uint32_t reg[] = {
297         cpu_to_be32(lpc_pcba),
298         cpu_to_be32(PNV_XSCOM_PBCQ_NEST_SIZE),
299         cpu_to_be32(PNV_XSCOM_PBCQ_PCI_BASE + 0x400 * phb->phb_id),
300         cpu_to_be32(PNV_XSCOM_PBCQ_PCI_SIZE),
301         cpu_to_be32(PNV_XSCOM_PBCQ_SPCI_BASE + 0x040 * phb->phb_id),
302         cpu_to_be32(PNV_XSCOM_PBCQ_SPCI_SIZE)
303     };
304 
305     name = g_strdup_printf("pbcq@%x", lpc_pcba);
306     offset = fdt_add_subnode(fdt, xscom_offset, name);
307     _FDT(offset);
308     g_free(name);
309 
310     _FDT((fdt_setprop(fdt, offset, "reg", reg, sizeof(reg))));
311 
312     _FDT((fdt_setprop_cell(fdt, offset, "ibm,phb-index", phb->phb_id)));
313     _FDT((fdt_setprop_cell(fdt, offset, "ibm,chip-id", phb->chip_id)));
314     _FDT((fdt_setprop(fdt, offset, "compatible", compat,
315                       sizeof(compat))));
316     return 0;
317 }
318 
319 static void phb3_pbcq_instance_init(Object *obj)
320 {
321     PnvPBCQState *pbcq = PNV_PBCQ(obj);
322 
323     object_property_add_link(obj, "phb", TYPE_PNV_PHB3,
324                              (Object **)&pbcq->phb,
325                              object_property_allow_set_link,
326                              OBJ_PROP_LINK_STRONG,
327                              &error_abort);
328 }
329 
330 static void pnv_pbcq_class_init(ObjectClass *klass, void *data)
331 {
332     DeviceClass *dc = DEVICE_CLASS(klass);
333     PnvXScomInterfaceClass *xdc = PNV_XSCOM_INTERFACE_CLASS(klass);
334 
335     xdc->dt_xscom = pnv_pbcq_dt_xscom;
336 
337     dc->realize = pnv_pbcq_realize;
338     dc->user_creatable = false;
339 }
340 
341 static const TypeInfo pnv_pbcq_type_info = {
342     .name          = TYPE_PNV_PBCQ,
343     .parent        = TYPE_DEVICE,
344     .instance_size = sizeof(PnvPBCQState),
345     .instance_init = phb3_pbcq_instance_init,
346     .class_init    = pnv_pbcq_class_init,
347     .interfaces    = (InterfaceInfo[]) {
348         { TYPE_PNV_XSCOM_INTERFACE },
349         { }
350     }
351 };
352 
353 static void pnv_pbcq_register_types(void)
354 {
355     type_register_static(&pnv_pbcq_type_info);
356 }
357 
358 type_init(pnv_pbcq_register_types)
359