1967b7523SCédric Le Goater /*
2967b7523SCédric Le Goater * QEMU PowerPC PowerNV XSCOM bus
3967b7523SCédric Le Goater *
4967b7523SCédric Le Goater * Copyright (c) 2016, IBM Corporation.
5967b7523SCédric Le Goater *
6967b7523SCédric Le Goater * This library is free software; you can redistribute it and/or
7967b7523SCédric Le Goater * modify it under the terms of the GNU Lesser General Public
8967b7523SCédric Le Goater * License as published by the Free Software Foundation; either
9f70c5966SChetan Pant * version 2.1 of the License, or (at your option) any later version.
10967b7523SCédric Le Goater *
11967b7523SCédric Le Goater * This library is distributed in the hope that it will be useful,
12967b7523SCédric Le Goater * but WITHOUT ANY WARRANTY; without even the implied warranty of
13967b7523SCédric Le Goater * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14967b7523SCédric Le Goater * Lesser General Public License for more details.
15967b7523SCédric Le Goater *
16967b7523SCédric Le Goater * You should have received a copy of the GNU Lesser General Public
17967b7523SCédric Le Goater * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18967b7523SCédric Le Goater */
190b8fa32fSMarkus Armbruster
20967b7523SCédric Le Goater #include "qemu/osdep.h"
21967b7523SCédric Le Goater #include "qemu/log.h"
220b8fa32fSMarkus Armbruster #include "qemu/module.h"
23b3946626SVincent Palatin #include "sysemu/hw_accel.h"
24fcf5ef2aSThomas Huth #include "target/ppc/cpu.h"
25967b7523SCédric Le Goater #include "hw/sysbus.h"
26967b7523SCédric Le Goater
27967b7523SCédric Le Goater #include "hw/ppc/fdt.h"
28967b7523SCédric Le Goater #include "hw/ppc/pnv.h"
292c6fe2e2SMarkus Armbruster #include "hw/ppc/pnv_chip.h"
30ec575aa0SCédric Le Goater #include "hw/ppc/pnv_xscom.h"
31967b7523SCédric Le Goater
32967b7523SCédric Le Goater #include <libfdt.h>
33967b7523SCédric Le Goater
34ce4b1b56SCédric Le Goater /* PRD registers */
35ce4b1b56SCédric Le Goater #define PRD_P8_IPOLL_REG_MASK 0x01020013
36ce4b1b56SCédric Le Goater #define PRD_P8_IPOLL_REG_STATUS 0x01020014
37ce4b1b56SCédric Le Goater #define PRD_P9_IPOLL_REG_MASK 0x000F0033
38ce4b1b56SCédric Le Goater #define PRD_P9_IPOLL_REG_STATUS 0x000F0034
39ce4b1b56SCédric Le Goater
xscom_complete(CPUState * cs,uint64_t hmer_bits)40967b7523SCédric Le Goater static void xscom_complete(CPUState *cs, uint64_t hmer_bits)
41967b7523SCédric Le Goater {
42967b7523SCédric Le Goater /*
43967b7523SCédric Le Goater * TODO: When the read/write comes from the monitor, NULL is
44967b7523SCédric Le Goater * passed for the cpu, and no CPU completion is generated.
45967b7523SCédric Le Goater */
46967b7523SCédric Le Goater if (cs) {
47967b7523SCédric Le Goater /*
48967b7523SCédric Le Goater * TODO: Need a CPU helper to set HMER, also handle generation
49967b7523SCédric Le Goater * of HMIs
50967b7523SCédric Le Goater */
51967b7523SCédric Le Goater cpu_synchronize_state(cs);
52*794511bcSPhilippe Mathieu-Daudé cpu_env(cs)->spr[SPR_HMER] |= hmer_bits;
53967b7523SCédric Le Goater }
54967b7523SCédric Le Goater }
55967b7523SCédric Le Goater
pnv_xscom_pcba(PnvChip * chip,uint64_t addr)56967b7523SCédric Le Goater static uint32_t pnv_xscom_pcba(PnvChip *chip, uint64_t addr)
57967b7523SCédric Le Goater {
5870c059e9SGreg Kurz return PNV_CHIP_GET_CLASS(chip)->xscom_pcba(chip, addr);
59967b7523SCédric Le Goater }
60967b7523SCédric Le Goater
xscom_read_default(PnvChip * chip,uint32_t pcba)61967b7523SCédric Le Goater static uint64_t xscom_read_default(PnvChip *chip, uint32_t pcba)
62967b7523SCédric Le Goater {
63967b7523SCédric Le Goater switch (pcba) {
64967b7523SCédric Le Goater case 0xf000f:
65967b7523SCédric Le Goater return PNV_CHIP_GET_CLASS(chip)->chip_cfam_id;
66bc565116SCédric Le Goater case 0x18002: /* ECID2 */
67bc565116SCédric Le Goater return 0;
68bc565116SCédric Le Goater
69967b7523SCédric Le Goater case 0x1010c00: /* PIBAM FIR */
70967b7523SCédric Le Goater case 0x1010c03: /* PIBAM FIR MASK */
71bc565116SCédric Le Goater
72ce4b1b56SCédric Le Goater /* PRD registers */
73ce4b1b56SCédric Le Goater case PRD_P8_IPOLL_REG_MASK:
74ce4b1b56SCédric Le Goater case PRD_P8_IPOLL_REG_STATUS:
75ce4b1b56SCédric Le Goater case PRD_P9_IPOLL_REG_MASK:
76ce4b1b56SCédric Le Goater case PRD_P9_IPOLL_REG_STATUS:
77ce4b1b56SCédric Le Goater
78bc565116SCédric Le Goater /* P8 xscom reset */
79bc565116SCédric Le Goater case 0x2020007: /* ADU stuff, log register */
80bc565116SCédric Le Goater case 0x2020009: /* ADU stuff, error register */
81bc565116SCédric Le Goater case 0x202000f: /* ADU stuff, receive status register*/
82967b7523SCédric Le Goater return 0;
83967b7523SCédric Le Goater case 0x2013f01: /* PBA stuff */
84967b7523SCédric Le Goater case 0x2013f05: /* PBA stuff */
85967b7523SCédric Le Goater return 0;
86967b7523SCédric Le Goater case 0x2013028: /* CAPP stuff */
87967b7523SCédric Le Goater case 0x201302a: /* CAPP stuff */
88967b7523SCédric Le Goater case 0x2013801: /* CAPP stuff */
89967b7523SCédric Le Goater case 0x2013802: /* CAPP stuff */
9045a73a19SCédric Le Goater
9145a73a19SCédric Le Goater /* P9 CAPP regs */
9245a73a19SCédric Le Goater case 0x2010841:
9345a73a19SCédric Le Goater case 0x2010842:
9445a73a19SCédric Le Goater case 0x201082a:
9545a73a19SCédric Le Goater case 0x2010828:
9645a73a19SCédric Le Goater case 0x4010841:
9745a73a19SCédric Le Goater case 0x4010842:
9845a73a19SCédric Le Goater case 0x401082a:
9945a73a19SCédric Le Goater case 0x4010828:
100967b7523SCédric Le Goater return 0;
101967b7523SCédric Le Goater default:
102967b7523SCédric Le Goater return -1;
103967b7523SCédric Le Goater }
104967b7523SCédric Le Goater }
105967b7523SCédric Le Goater
xscom_write_default(PnvChip * chip,uint32_t pcba,uint64_t val)106967b7523SCédric Le Goater static bool xscom_write_default(PnvChip *chip, uint32_t pcba, uint64_t val)
107967b7523SCédric Le Goater {
108967b7523SCédric Le Goater /* We ignore writes to these */
109967b7523SCédric Le Goater switch (pcba) {
110967b7523SCédric Le Goater case 0xf000f: /* chip id is RO */
111967b7523SCédric Le Goater case 0x1010c00: /* PIBAM FIR */
112967b7523SCédric Le Goater case 0x1010c01: /* PIBAM FIR */
113967b7523SCédric Le Goater case 0x1010c02: /* PIBAM FIR */
114967b7523SCédric Le Goater case 0x1010c03: /* PIBAM FIR MASK */
115967b7523SCédric Le Goater case 0x1010c04: /* PIBAM FIR MASK */
116967b7523SCédric Le Goater case 0x1010c05: /* PIBAM FIR MASK */
117bc565116SCédric Le Goater
118bc565116SCédric Le Goater /* P8 xscom reset */
119bc565116SCédric Le Goater case 0x2020007: /* ADU stuff, log register */
120bc565116SCédric Le Goater case 0x2020009: /* ADU stuff, error register */
121bc565116SCédric Le Goater case 0x202000f: /* ADU stuff, receive status register*/
122bc565116SCédric Le Goater
123bc565116SCédric Le Goater case 0x2013028: /* CAPP stuff */
124bc565116SCédric Le Goater case 0x201302a: /* CAPP stuff */
125bc565116SCédric Le Goater case 0x2013801: /* CAPP stuff */
126bc565116SCédric Le Goater case 0x2013802: /* CAPP stuff */
127ce4b1b56SCédric Le Goater
12845a73a19SCédric Le Goater /* P9 CAPP regs */
12945a73a19SCédric Le Goater case 0x2010841:
13045a73a19SCédric Le Goater case 0x2010842:
13145a73a19SCédric Le Goater case 0x201082a:
13245a73a19SCédric Le Goater case 0x2010828:
13345a73a19SCédric Le Goater case 0x4010841:
13445a73a19SCédric Le Goater case 0x4010842:
13545a73a19SCédric Le Goater case 0x401082a:
13645a73a19SCédric Le Goater case 0x4010828:
13745a73a19SCédric Le Goater
138ce4b1b56SCédric Le Goater /* P8 PRD registers */
139ce4b1b56SCédric Le Goater case PRD_P8_IPOLL_REG_MASK:
140ce4b1b56SCédric Le Goater case PRD_P8_IPOLL_REG_STATUS:
141ce4b1b56SCédric Le Goater case PRD_P9_IPOLL_REG_MASK:
142ce4b1b56SCédric Le Goater case PRD_P9_IPOLL_REG_STATUS:
143967b7523SCédric Le Goater return true;
144967b7523SCédric Le Goater default:
145967b7523SCédric Le Goater return false;
146967b7523SCédric Le Goater }
147967b7523SCédric Le Goater }
148967b7523SCédric Le Goater
xscom_read(void * opaque,hwaddr addr,unsigned width)149967b7523SCédric Le Goater static uint64_t xscom_read(void *opaque, hwaddr addr, unsigned width)
150967b7523SCédric Le Goater {
151967b7523SCédric Le Goater PnvChip *chip = opaque;
152967b7523SCédric Le Goater uint32_t pcba = pnv_xscom_pcba(chip, addr);
153967b7523SCédric Le Goater uint64_t val = 0;
154967b7523SCédric Le Goater MemTxResult result;
155967b7523SCédric Le Goater
156967b7523SCédric Le Goater /* Handle some SCOMs here before dispatch */
157967b7523SCédric Le Goater val = xscom_read_default(chip, pcba);
158967b7523SCédric Le Goater if (val != -1) {
159967b7523SCédric Le Goater goto complete;
160967b7523SCédric Le Goater }
161967b7523SCédric Le Goater
162f81e5512SCédric Le Goater val = address_space_ldq(&chip->xscom_as, (uint64_t) pcba << 3,
163f81e5512SCédric Le Goater MEMTXATTRS_UNSPECIFIED, &result);
164967b7523SCédric Le Goater if (result != MEMTX_OK) {
165967b7523SCédric Le Goater qemu_log_mask(LOG_GUEST_ERROR, "XSCOM read failed at @0x%"
166967b7523SCédric Le Goater HWADDR_PRIx " pcba=0x%08x\n", addr, pcba);
167967b7523SCédric Le Goater xscom_complete(current_cpu, HMER_XSCOM_FAIL | HMER_XSCOM_DONE);
168967b7523SCédric Le Goater return 0;
169967b7523SCédric Le Goater }
170967b7523SCédric Le Goater
171967b7523SCédric Le Goater complete:
172967b7523SCédric Le Goater xscom_complete(current_cpu, HMER_XSCOM_DONE);
173967b7523SCédric Le Goater return val;
174967b7523SCédric Le Goater }
175967b7523SCédric Le Goater
xscom_write(void * opaque,hwaddr addr,uint64_t val,unsigned width)176967b7523SCédric Le Goater static void xscom_write(void *opaque, hwaddr addr, uint64_t val,
177967b7523SCédric Le Goater unsigned width)
178967b7523SCédric Le Goater {
179967b7523SCédric Le Goater PnvChip *chip = opaque;
180967b7523SCédric Le Goater uint32_t pcba = pnv_xscom_pcba(chip, addr);
181967b7523SCédric Le Goater MemTxResult result;
182967b7523SCédric Le Goater
183967b7523SCédric Le Goater /* Handle some SCOMs here before dispatch */
184967b7523SCédric Le Goater if (xscom_write_default(chip, pcba, val)) {
185967b7523SCédric Le Goater goto complete;
186967b7523SCédric Le Goater }
187967b7523SCédric Le Goater
188f81e5512SCédric Le Goater address_space_stq(&chip->xscom_as, (uint64_t) pcba << 3, val,
189f81e5512SCédric Le Goater MEMTXATTRS_UNSPECIFIED, &result);
190967b7523SCédric Le Goater if (result != MEMTX_OK) {
191967b7523SCédric Le Goater qemu_log_mask(LOG_GUEST_ERROR, "XSCOM write failed at @0x%"
192967b7523SCédric Le Goater HWADDR_PRIx " pcba=0x%08x data=0x%" PRIx64 "\n",
193967b7523SCédric Le Goater addr, pcba, val);
194967b7523SCédric Le Goater xscom_complete(current_cpu, HMER_XSCOM_FAIL | HMER_XSCOM_DONE);
195967b7523SCédric Le Goater return;
196967b7523SCédric Le Goater }
197967b7523SCédric Le Goater
198967b7523SCédric Le Goater complete:
199967b7523SCédric Le Goater xscom_complete(current_cpu, HMER_XSCOM_DONE);
200967b7523SCédric Le Goater }
201967b7523SCédric Le Goater
202967b7523SCédric Le Goater const MemoryRegionOps pnv_xscom_ops = {
203967b7523SCédric Le Goater .read = xscom_read,
204967b7523SCédric Le Goater .write = xscom_write,
205967b7523SCédric Le Goater .valid.min_access_size = 8,
206967b7523SCédric Le Goater .valid.max_access_size = 8,
207967b7523SCédric Le Goater .impl.min_access_size = 8,
208967b7523SCédric Le Goater .impl.max_access_size = 8,
209967b7523SCédric Le Goater .endianness = DEVICE_BIG_ENDIAN,
210967b7523SCédric Le Goater };
211967b7523SCédric Le Goater
pnv_xscom_init(PnvChip * chip,uint64_t size,hwaddr addr)212326f7acbSPhilippe Mathieu-Daudé void pnv_xscom_init(PnvChip *chip, uint64_t size, hwaddr addr)
213967b7523SCédric Le Goater {
214967b7523SCédric Le Goater char *name;
215967b7523SCédric Le Goater
216967b7523SCédric Le Goater name = g_strdup_printf("xscom-%x", chip->chip_id);
217967b7523SCédric Le Goater memory_region_init_io(&chip->xscom_mmio, OBJECT(chip), &pnv_xscom_ops,
218709044fdSCédric Le Goater chip, name, size);
219bddb6775SPhilippe Mathieu-Daudé memory_region_add_subregion(get_system_memory(), addr, &chip->xscom_mmio);
220967b7523SCédric Le Goater
221709044fdSCédric Le Goater memory_region_init(&chip->xscom, OBJECT(chip), name, size);
222967b7523SCédric Le Goater address_space_init(&chip->xscom_as, &chip->xscom, name);
223967b7523SCédric Le Goater g_free(name);
224967b7523SCédric Le Goater }
225967b7523SCédric Le Goater
226967b7523SCédric Le Goater static const TypeInfo pnv_xscom_interface_info = {
227967b7523SCédric Le Goater .name = TYPE_PNV_XSCOM_INTERFACE,
228967b7523SCédric Le Goater .parent = TYPE_INTERFACE,
229967b7523SCédric Le Goater .class_size = sizeof(PnvXScomInterfaceClass),
230967b7523SCédric Le Goater };
231967b7523SCédric Le Goater
pnv_xscom_register_types(void)232967b7523SCédric Le Goater static void pnv_xscom_register_types(void)
233967b7523SCédric Le Goater {
234967b7523SCédric Le Goater type_register_static(&pnv_xscom_interface_info);
235967b7523SCédric Le Goater }
236967b7523SCédric Le Goater
237967b7523SCédric Le Goater type_init(pnv_xscom_register_types)
238967b7523SCédric Le Goater
239967b7523SCédric Le Goater typedef struct ForeachPopulateArgs {
240967b7523SCédric Le Goater void *fdt;
241967b7523SCédric Le Goater int xscom_offset;
242967b7523SCédric Le Goater } ForeachPopulateArgs;
243967b7523SCédric Le Goater
xscom_dt_child(Object * child,void * opaque)244b168a138SCédric Le Goater static int xscom_dt_child(Object *child, void *opaque)
245967b7523SCédric Le Goater {
246967b7523SCédric Le Goater if (object_dynamic_cast(child, TYPE_PNV_XSCOM_INTERFACE)) {
247967b7523SCédric Le Goater ForeachPopulateArgs *args = opaque;
248967b7523SCédric Le Goater PnvXScomInterface *xd = PNV_XSCOM_INTERFACE(child);
249967b7523SCédric Le Goater PnvXScomInterfaceClass *xc = PNV_XSCOM_INTERFACE_GET_CLASS(xd);
250967b7523SCédric Le Goater
2519e028fffSCédric Le Goater /*
2529e028fffSCédric Le Goater * Only "realized" devices should be configured in the DT
2539e028fffSCédric Le Goater */
2549e028fffSCédric Le Goater if (xc->dt_xscom && DEVICE(child)->realized) {
255b168a138SCédric Le Goater _FDT((xc->dt_xscom(xd, args->fdt, args->xscom_offset)));
256967b7523SCédric Le Goater }
257967b7523SCédric Le Goater }
258967b7523SCédric Le Goater return 0;
259967b7523SCédric Le Goater }
260967b7523SCédric Le Goater
pnv_dt_xscom(PnvChip * chip,void * fdt,int root_offset,uint64_t xscom_base,uint64_t xscom_size,const char * compat,int compat_size)2613f5b45caSGreg Kurz int pnv_dt_xscom(PnvChip *chip, void *fdt, int root_offset,
262c396c58aSGreg Kurz uint64_t xscom_base, uint64_t xscom_size,
263c396c58aSGreg Kurz const char *compat, int compat_size)
264967b7523SCédric Le Goater {
2653f5b45caSGreg Kurz uint64_t reg[] = { xscom_base, xscom_size };
266967b7523SCédric Le Goater int xscom_offset;
267967b7523SCédric Le Goater ForeachPopulateArgs args;
268967b7523SCédric Le Goater char *name;
269967b7523SCédric Le Goater
270967b7523SCédric Le Goater name = g_strdup_printf("xscom@%" PRIx64, be64_to_cpu(reg[0]));
271967b7523SCédric Le Goater xscom_offset = fdt_add_subnode(fdt, root_offset, name);
272967b7523SCédric Le Goater _FDT(xscom_offset);
273967b7523SCédric Le Goater g_free(name);
274967b7523SCédric Le Goater _FDT((fdt_setprop_cell(fdt, xscom_offset, "ibm,chip-id", chip->chip_id)));
27540ef88baSCédric Le Goater /*
27640ef88baSCédric Le Goater * On P10, the xscom bus id has been deprecated and the chip id is
27740ef88baSCédric Le Goater * calculated from the "Primary topology table index". See skiboot.
27840ef88baSCédric Le Goater */
27937909030SCédric Le Goater _FDT((fdt_setprop_cell(fdt, xscom_offset, "ibm,primary-topology-index",
28037909030SCédric Le Goater chip->chip_id)));
281967b7523SCédric Le Goater _FDT((fdt_setprop_cell(fdt, xscom_offset, "#address-cells", 1)));
282967b7523SCédric Le Goater _FDT((fdt_setprop_cell(fdt, xscom_offset, "#size-cells", 1)));
283967b7523SCédric Le Goater _FDT((fdt_setprop(fdt, xscom_offset, "reg", reg, sizeof(reg))));
284c396c58aSGreg Kurz _FDT((fdt_setprop(fdt, xscom_offset, "compatible", compat, compat_size)));
285967b7523SCédric Le Goater _FDT((fdt_setprop(fdt, xscom_offset, "scom-controller", NULL, 0)));
2860bf4d77eSNicholas Piggin if (chip->chip_id == 0) {
2870bf4d77eSNicholas Piggin _FDT((fdt_setprop(fdt, xscom_offset, "primary", NULL, 0)));
2880bf4d77eSNicholas Piggin }
289967b7523SCédric Le Goater
290967b7523SCédric Le Goater args.fdt = fdt;
291967b7523SCédric Le Goater args.xscom_offset = xscom_offset;
292967b7523SCédric Le Goater
293109dce37SCédric Le Goater /*
294109dce37SCédric Le Goater * Loop on the whole object hierarchy to catch all
295109dce37SCédric Le Goater * PnvXScomInterface objects which can lie a bit deeper than the
296109dce37SCédric Le Goater * first layer.
297109dce37SCédric Le Goater */
298109dce37SCédric Le Goater object_child_foreach_recursive(OBJECT(chip), xscom_dt_child, &args);
299967b7523SCédric Le Goater return 0;
300967b7523SCédric Le Goater }
301967b7523SCédric Le Goater
pnv_xscom_add_subregion(PnvChip * chip,hwaddr offset,MemoryRegion * mr)302967b7523SCédric Le Goater void pnv_xscom_add_subregion(PnvChip *chip, hwaddr offset, MemoryRegion *mr)
303967b7523SCédric Le Goater {
304967b7523SCédric Le Goater memory_region_add_subregion(&chip->xscom, offset << 3, mr);
305967b7523SCédric Le Goater }
306967b7523SCédric Le Goater
pnv_xscom_region_init(MemoryRegion * mr,Object * owner,const MemoryRegionOps * ops,void * opaque,const char * name,uint64_t size)307967b7523SCédric Le Goater void pnv_xscom_region_init(MemoryRegion *mr,
308d32335e8SPhilippe Mathieu-Daudé Object *owner,
309967b7523SCédric Le Goater const MemoryRegionOps *ops,
310967b7523SCédric Le Goater void *opaque,
311967b7523SCédric Le Goater const char *name,
312967b7523SCédric Le Goater uint64_t size)
313967b7523SCédric Le Goater {
314967b7523SCédric Le Goater memory_region_init_io(mr, owner, ops, opaque, name, size << 3);
315967b7523SCédric Le Goater }
316