1 /*
2 * QEMU PowerPC PowerNV LPC controller
3 *
4 * Copyright (c) 2016, IBM Corporation.
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "qemu/osdep.h"
21 #include "target/ppc/cpu.h"
22 #include "qapi/error.h"
23 #include "qemu/log.h"
24 #include "qemu/module.h"
25 #include "hw/irq.h"
26 #include "hw/isa/isa.h"
27 #include "hw/qdev-properties.h"
28 #include "hw/ppc/pnv.h"
29 #include "hw/ppc/pnv_chip.h"
30 #include "hw/ppc/pnv_lpc.h"
31 #include "hw/ppc/pnv_xscom.h"
32 #include "hw/ppc/fdt.h"
33
34 #include <libfdt.h>
35
36 enum {
37 ECCB_CTL = 0,
38 ECCB_RESET = 1,
39 ECCB_STAT = 2,
40 ECCB_DATA = 3,
41 };
42
43 /* OPB Master LS registers */
44 #define OPB_MASTER_LS_ROUTE0 0x8
45 #define OPB_MASTER_LS_ROUTE1 0xC
46 #define OPB_MASTER_LS_IRQ_STAT 0x50
47 #define OPB_MASTER_IRQ_LPC 0x00000800
48 #define OPB_MASTER_LS_IRQ_MASK 0x54
49 #define OPB_MASTER_LS_IRQ_POL 0x58
50 #define OPB_MASTER_LS_IRQ_INPUT 0x5c
51
52 /* LPC HC registers */
53 #define LPC_HC_FW_SEG_IDSEL 0x24
54 #define LPC_HC_FW_RD_ACC_SIZE 0x28
55 #define LPC_HC_FW_RD_1B 0x00000000
56 #define LPC_HC_FW_RD_2B 0x01000000
57 #define LPC_HC_FW_RD_4B 0x02000000
58 #define LPC_HC_FW_RD_16B 0x04000000
59 #define LPC_HC_FW_RD_128B 0x07000000
60 #define LPC_HC_IRQSER_CTRL 0x30
61 #define LPC_HC_IRQSER_EN 0x80000000
62 #define LPC_HC_IRQSER_QMODE 0x40000000
63 #define LPC_HC_IRQSER_START_MASK 0x03000000
64 #define LPC_HC_IRQSER_START_4CLK 0x00000000
65 #define LPC_HC_IRQSER_START_6CLK 0x01000000
66 #define LPC_HC_IRQSER_START_8CLK 0x02000000
67 #define LPC_HC_IRQSER_AUTO_CLEAR 0x00800000
68 #define LPC_HC_IRQMASK 0x34 /* same bit defs as LPC_HC_IRQSTAT */
69 #define LPC_HC_IRQSTAT 0x38
70 #define LPC_HC_IRQ_SERIRQ0 0x80000000 /* all bits down to ... */
71 #define LPC_HC_IRQ_SERIRQ16 0x00008000 /* IRQ16=IOCHK#, IRQ2=SMI# */
72 #define LPC_HC_IRQ_SERIRQ_ALL 0xffff8000
73 #define LPC_HC_IRQ_LRESET 0x00000400
74 #define LPC_HC_IRQ_SYNC_ABNORM_ERR 0x00000080
75 #define LPC_HC_IRQ_SYNC_NORESP_ERR 0x00000040
76 #define LPC_HC_IRQ_SYNC_NORM_ERR 0x00000020
77 #define LPC_HC_IRQ_SYNC_TIMEOUT_ERR 0x00000010
78 #define LPC_HC_IRQ_SYNC_TARG_TAR_ERR 0x00000008
79 #define LPC_HC_IRQ_SYNC_BM_TAR_ERR 0x00000004
80 #define LPC_HC_IRQ_SYNC_BM0_REQ 0x00000002
81 #define LPC_HC_IRQ_SYNC_BM1_REQ 0x00000001
82 #define LPC_HC_ERROR_ADDRESS 0x40
83
84 #define LPC_OPB_SIZE 0x100000000ull
85
86 #define ISA_IO_SIZE 0x00010000
87 #define ISA_MEM_SIZE 0x10000000
88 #define ISA_FW_SIZE 0x100000000
89 #define LPC_IO_OPB_ADDR 0xd0010000
90 #define LPC_IO_OPB_SIZE 0x00010000
91 #define LPC_MEM_OPB_ADDR 0xe0000000
92 #define LPC_MEM_OPB_SIZE 0x10000000
93 #define LPC_FW_OPB_ADDR 0xf0000000
94 #define LPC_FW_OPB_SIZE 0x10000000
95
96 #define LPC_OPB_REGS_OPB_ADDR 0xc0010000
97 #define LPC_OPB_REGS_OPB_SIZE 0x00000060
98 #define LPC_OPB_REGS_OPBA_ADDR 0xc0011000
99 #define LPC_OPB_REGS_OPBA_SIZE 0x00000008
100 #define LPC_HC_REGS_OPB_ADDR 0xc0012000
101 #define LPC_HC_REGS_OPB_SIZE 0x00000100
102
pnv_lpc_dt_xscom(PnvXScomInterface * dev,void * fdt,int xscom_offset)103 static int pnv_lpc_dt_xscom(PnvXScomInterface *dev, void *fdt, int xscom_offset)
104 {
105 const char compat[] = "ibm,power8-lpc\0ibm,lpc";
106 char *name;
107 int offset;
108 uint32_t lpc_pcba = PNV_XSCOM_LPC_BASE;
109 uint32_t reg[] = {
110 cpu_to_be32(lpc_pcba),
111 cpu_to_be32(PNV_XSCOM_LPC_SIZE)
112 };
113
114 name = g_strdup_printf("isa@%x", lpc_pcba);
115 offset = fdt_add_subnode(fdt, xscom_offset, name);
116 _FDT(offset);
117 g_free(name);
118
119 _FDT((fdt_setprop(fdt, offset, "reg", reg, sizeof(reg))));
120 _FDT((fdt_setprop_cell(fdt, offset, "#address-cells", 2)));
121 _FDT((fdt_setprop_cell(fdt, offset, "#size-cells", 1)));
122 _FDT((fdt_setprop(fdt, offset, "compatible", compat, sizeof(compat))));
123 return 0;
124 }
125
126 /* POWER9 only */
pnv_dt_lpc(PnvChip * chip,void * fdt,int root_offset,uint64_t lpcm_addr,uint64_t lpcm_size)127 int pnv_dt_lpc(PnvChip *chip, void *fdt, int root_offset, uint64_t lpcm_addr,
128 uint64_t lpcm_size)
129 {
130 const char compat[] = "ibm,power9-lpcm-opb\0simple-bus";
131 const char lpc_compat[] = "ibm,power9-lpc\0ibm,lpc";
132 char *name;
133 int offset, lpcm_offset;
134 uint32_t opb_ranges[8] = { 0,
135 cpu_to_be32(lpcm_addr >> 32),
136 cpu_to_be32((uint32_t)lpcm_addr),
137 cpu_to_be32(lpcm_size / 2),
138 cpu_to_be32(lpcm_size / 2),
139 cpu_to_be32(lpcm_addr >> 32),
140 cpu_to_be32(lpcm_size / 2),
141 cpu_to_be32(lpcm_size / 2),
142 };
143 uint32_t opb_reg[4] = { cpu_to_be32(lpcm_addr >> 32),
144 cpu_to_be32((uint32_t)lpcm_addr),
145 cpu_to_be32(lpcm_size >> 32),
146 cpu_to_be32((uint32_t)lpcm_size),
147 };
148 uint32_t lpc_ranges[12] = { 0, 0,
149 cpu_to_be32(LPC_MEM_OPB_ADDR),
150 cpu_to_be32(LPC_MEM_OPB_SIZE),
151 cpu_to_be32(1), 0,
152 cpu_to_be32(LPC_IO_OPB_ADDR),
153 cpu_to_be32(LPC_IO_OPB_SIZE),
154 cpu_to_be32(3), 0,
155 cpu_to_be32(LPC_FW_OPB_ADDR),
156 cpu_to_be32(LPC_FW_OPB_SIZE),
157 };
158 uint32_t reg[2];
159
160 /*
161 * OPB bus
162 */
163 name = g_strdup_printf("lpcm-opb@%"PRIx64, lpcm_addr);
164 lpcm_offset = fdt_add_subnode(fdt, root_offset, name);
165 _FDT(lpcm_offset);
166 g_free(name);
167
168 _FDT((fdt_setprop(fdt, lpcm_offset, "reg", opb_reg, sizeof(opb_reg))));
169 _FDT((fdt_setprop_cell(fdt, lpcm_offset, "#address-cells", 1)));
170 _FDT((fdt_setprop_cell(fdt, lpcm_offset, "#size-cells", 1)));
171 _FDT((fdt_setprop(fdt, lpcm_offset, "compatible", compat, sizeof(compat))));
172 _FDT((fdt_setprop_cell(fdt, lpcm_offset, "ibm,chip-id", chip->chip_id)));
173 _FDT((fdt_setprop(fdt, lpcm_offset, "ranges", opb_ranges,
174 sizeof(opb_ranges))));
175
176 /*
177 * OPB Master registers
178 */
179 name = g_strdup_printf("opb-master@%x", LPC_OPB_REGS_OPB_ADDR);
180 offset = fdt_add_subnode(fdt, lpcm_offset, name);
181 _FDT(offset);
182 g_free(name);
183
184 reg[0] = cpu_to_be32(LPC_OPB_REGS_OPB_ADDR);
185 reg[1] = cpu_to_be32(LPC_OPB_REGS_OPB_SIZE);
186 _FDT((fdt_setprop(fdt, offset, "reg", reg, sizeof(reg))));
187 _FDT((fdt_setprop_string(fdt, offset, "compatible",
188 "ibm,power9-lpcm-opb-master")));
189
190 /*
191 * OPB arbitrer registers
192 */
193 name = g_strdup_printf("opb-arbitrer@%x", LPC_OPB_REGS_OPBA_ADDR);
194 offset = fdt_add_subnode(fdt, lpcm_offset, name);
195 _FDT(offset);
196 g_free(name);
197
198 reg[0] = cpu_to_be32(LPC_OPB_REGS_OPBA_ADDR);
199 reg[1] = cpu_to_be32(LPC_OPB_REGS_OPBA_SIZE);
200 _FDT((fdt_setprop(fdt, offset, "reg", reg, sizeof(reg))));
201 _FDT((fdt_setprop_string(fdt, offset, "compatible",
202 "ibm,power9-lpcm-opb-arbiter")));
203
204 /*
205 * LPC Host Controller registers
206 */
207 name = g_strdup_printf("lpc-controller@%x", LPC_HC_REGS_OPB_ADDR);
208 offset = fdt_add_subnode(fdt, lpcm_offset, name);
209 _FDT(offset);
210 g_free(name);
211
212 reg[0] = cpu_to_be32(LPC_HC_REGS_OPB_ADDR);
213 reg[1] = cpu_to_be32(LPC_HC_REGS_OPB_SIZE);
214 _FDT((fdt_setprop(fdt, offset, "reg", reg, sizeof(reg))));
215 _FDT((fdt_setprop_string(fdt, offset, "compatible",
216 "ibm,power9-lpc-controller")));
217
218 name = g_strdup_printf("lpc@0");
219 offset = fdt_add_subnode(fdt, lpcm_offset, name);
220 _FDT(offset);
221 g_free(name);
222 _FDT((fdt_setprop_cell(fdt, offset, "#address-cells", 2)));
223 _FDT((fdt_setprop_cell(fdt, offset, "#size-cells", 1)));
224 _FDT((fdt_setprop(fdt, offset, "compatible", lpc_compat,
225 sizeof(lpc_compat))));
226 _FDT((fdt_setprop(fdt, offset, "ranges", lpc_ranges,
227 sizeof(lpc_ranges))));
228
229 return 0;
230 }
231
232 /*
233 * These read/write handlers of the OPB address space should be common
234 * with the P9 LPC Controller which uses direct MMIOs.
235 *
236 * TODO: rework to use address_space_stq() and address_space_ldq()
237 * instead.
238 */
pnv_lpc_opb_read(PnvLpcController * lpc,uint32_t addr,uint8_t * data,int sz)239 bool pnv_lpc_opb_read(PnvLpcController *lpc, uint32_t addr,
240 uint8_t *data, int sz)
241 {
242 /* XXX Handle access size limits and FW read caching here */
243 return !address_space_read(&lpc->opb_as, addr, MEMTXATTRS_UNSPECIFIED,
244 data, sz);
245 }
246
pnv_lpc_opb_write(PnvLpcController * lpc,uint32_t addr,uint8_t * data,int sz)247 bool pnv_lpc_opb_write(PnvLpcController *lpc, uint32_t addr,
248 uint8_t *data, int sz)
249 {
250 /* XXX Handle access size limits here */
251 return !address_space_write(&lpc->opb_as, addr, MEMTXATTRS_UNSPECIFIED,
252 data, sz);
253 }
254
255 #define ECCB_CTL_READ PPC_BIT(15)
256 #define ECCB_CTL_SZ_LSH (63 - 7)
257 #define ECCB_CTL_SZ_MASK PPC_BITMASK(4, 7)
258 #define ECCB_CTL_ADDR_MASK PPC_BITMASK(32, 63)
259
260 #define ECCB_STAT_OP_DONE PPC_BIT(52)
261 #define ECCB_STAT_OP_ERR PPC_BIT(52)
262 #define ECCB_STAT_RD_DATA_LSH (63 - 37)
263 #define ECCB_STAT_RD_DATA_MASK (0xffffffff << ECCB_STAT_RD_DATA_LSH)
264
pnv_lpc_do_eccb(PnvLpcController * lpc,uint64_t cmd)265 static void pnv_lpc_do_eccb(PnvLpcController *lpc, uint64_t cmd)
266 {
267 /* XXX Check for magic bits at the top, addr size etc... */
268 unsigned int sz = (cmd & ECCB_CTL_SZ_MASK) >> ECCB_CTL_SZ_LSH;
269 uint32_t opb_addr = cmd & ECCB_CTL_ADDR_MASK;
270 uint8_t data[8];
271 bool success;
272
273 if (sz > sizeof(data)) {
274 qemu_log_mask(LOG_GUEST_ERROR,
275 "ECCB: invalid operation at @0x%08x size %d\n", opb_addr, sz);
276 return;
277 }
278
279 if (cmd & ECCB_CTL_READ) {
280 success = pnv_lpc_opb_read(lpc, opb_addr, data, sz);
281 if (success) {
282 lpc->eccb_stat_reg = ECCB_STAT_OP_DONE |
283 (((uint64_t)data[0]) << 24 |
284 ((uint64_t)data[1]) << 16 |
285 ((uint64_t)data[2]) << 8 |
286 ((uint64_t)data[3])) << ECCB_STAT_RD_DATA_LSH;
287 } else {
288 lpc->eccb_stat_reg = ECCB_STAT_OP_DONE |
289 (0xffffffffull << ECCB_STAT_RD_DATA_LSH);
290 }
291 } else {
292 data[0] = lpc->eccb_data_reg >> 24;
293 data[1] = lpc->eccb_data_reg >> 16;
294 data[2] = lpc->eccb_data_reg >> 8;
295 data[3] = lpc->eccb_data_reg;
296
297 success = pnv_lpc_opb_write(lpc, opb_addr, data, sz);
298 lpc->eccb_stat_reg = ECCB_STAT_OP_DONE;
299 }
300 /* XXX Which error bit (if any) to signal OPB error ? */
301 }
302
pnv_lpc_xscom_read(void * opaque,hwaddr addr,unsigned size)303 static uint64_t pnv_lpc_xscom_read(void *opaque, hwaddr addr, unsigned size)
304 {
305 PnvLpcController *lpc = PNV_LPC(opaque);
306 uint32_t offset = addr >> 3;
307 uint64_t val = 0;
308
309 switch (offset & 3) {
310 case ECCB_CTL:
311 case ECCB_RESET:
312 val = 0;
313 break;
314 case ECCB_STAT:
315 val = lpc->eccb_stat_reg;
316 lpc->eccb_stat_reg = 0;
317 break;
318 case ECCB_DATA:
319 val = ((uint64_t)lpc->eccb_data_reg) << 32;
320 break;
321 }
322 return val;
323 }
324
pnv_lpc_xscom_write(void * opaque,hwaddr addr,uint64_t val,unsigned size)325 static void pnv_lpc_xscom_write(void *opaque, hwaddr addr,
326 uint64_t val, unsigned size)
327 {
328 PnvLpcController *lpc = PNV_LPC(opaque);
329 uint32_t offset = addr >> 3;
330
331 switch (offset & 3) {
332 case ECCB_CTL:
333 pnv_lpc_do_eccb(lpc, val);
334 break;
335 case ECCB_RESET:
336 /* XXXX */
337 break;
338 case ECCB_STAT:
339 break;
340 case ECCB_DATA:
341 lpc->eccb_data_reg = val >> 32;
342 break;
343 }
344 }
345
346 static const MemoryRegionOps pnv_lpc_xscom_ops = {
347 .read = pnv_lpc_xscom_read,
348 .write = pnv_lpc_xscom_write,
349 .valid.min_access_size = 8,
350 .valid.max_access_size = 8,
351 .impl.min_access_size = 8,
352 .impl.max_access_size = 8,
353 .endianness = DEVICE_BIG_ENDIAN,
354 };
355
356 static void pnv_lpc_opb_noresponse(PnvLpcController *lpc);
357
pnv_lpc_mmio_read(void * opaque,hwaddr addr,unsigned size)358 static uint64_t pnv_lpc_mmio_read(void *opaque, hwaddr addr, unsigned size)
359 {
360 PnvLpcController *lpc = PNV_LPC(opaque);
361 uint64_t val = 0;
362 uint32_t opb_addr = addr & ECCB_CTL_ADDR_MASK;
363 MemTxResult result;
364
365 switch (size) {
366 case 4:
367 val = address_space_ldl(&lpc->opb_as, opb_addr, MEMTXATTRS_UNSPECIFIED,
368 &result);
369 break;
370 case 1:
371 val = address_space_ldub(&lpc->opb_as, opb_addr, MEMTXATTRS_UNSPECIFIED,
372 &result);
373 break;
374 default:
375 qemu_log_mask(LOG_GUEST_ERROR, "OPB read failed at @0x%"
376 HWADDR_PRIx " invalid size %d\n", addr, size);
377 return 0;
378 }
379
380 if (result != MEMTX_OK) {
381 pnv_lpc_opb_noresponse(lpc);
382 qemu_log_mask(LOG_GUEST_ERROR, "OPB read failed at @0x%"
383 HWADDR_PRIx "\n", addr);
384 }
385
386 return val;
387 }
388
pnv_lpc_mmio_write(void * opaque,hwaddr addr,uint64_t val,unsigned size)389 static void pnv_lpc_mmio_write(void *opaque, hwaddr addr,
390 uint64_t val, unsigned size)
391 {
392 PnvLpcController *lpc = PNV_LPC(opaque);
393 uint32_t opb_addr = addr & ECCB_CTL_ADDR_MASK;
394 MemTxResult result;
395
396 switch (size) {
397 case 4:
398 address_space_stl(&lpc->opb_as, opb_addr, val, MEMTXATTRS_UNSPECIFIED,
399 &result);
400 break;
401 case 1:
402 address_space_stb(&lpc->opb_as, opb_addr, val, MEMTXATTRS_UNSPECIFIED,
403 &result);
404 break;
405 default:
406 qemu_log_mask(LOG_GUEST_ERROR, "OPB write failed at @0x%"
407 HWADDR_PRIx " invalid size %d\n", addr, size);
408 return;
409 }
410
411 if (result != MEMTX_OK) {
412 pnv_lpc_opb_noresponse(lpc);
413 qemu_log_mask(LOG_GUEST_ERROR, "OPB write failed at @0x%"
414 HWADDR_PRIx "\n", addr);
415 }
416 }
417
418 static const MemoryRegionOps pnv_lpc_mmio_ops = {
419 .read = pnv_lpc_mmio_read,
420 .write = pnv_lpc_mmio_write,
421 .impl = {
422 .min_access_size = 1,
423 .max_access_size = 4,
424 },
425 .endianness = DEVICE_BIG_ENDIAN,
426 };
427
428 /* Program the POWER9 LPC irq to PSI serirq routing table */
pnv_lpc_eval_serirq_routes(PnvLpcController * lpc)429 static void pnv_lpc_eval_serirq_routes(PnvLpcController *lpc)
430 {
431 int irq;
432
433 if (!lpc->psi_has_serirq) {
434 if ((lpc->opb_irq_route0 & PPC_BITMASK32(8, 13)) ||
435 (lpc->opb_irq_route1 & PPC_BITMASK32(4, 31))) {
436 qemu_log_mask(LOG_GUEST_ERROR,
437 "OPB: setting serirq routing on POWER8 system, ignoring.\n");
438 }
439 return;
440 }
441
442 /*
443 * Each of the ISA irqs is routed to one of the 4 SERIRQ irqs with 2
444 * bits, split across 2 OPB registers.
445 */
446 for (irq = 0; irq <= 13; irq++) {
447 int serirq = extract32(lpc->opb_irq_route1,
448 PPC_BIT32_NR(5 + irq * 2), 2);
449 lpc->irq_to_serirq_route[irq] = serirq;
450 }
451
452 for (irq = 14; irq < ISA_NUM_IRQS; irq++) {
453 int serirq = extract32(lpc->opb_irq_route0,
454 PPC_BIT32_NR(9 + (irq - 14) * 2), 2);
455 lpc->irq_to_serirq_route[irq] = serirq;
456 }
457 }
458
pnv_lpc_eval_irqs(PnvLpcController * lpc)459 static void pnv_lpc_eval_irqs(PnvLpcController *lpc)
460 {
461 uint32_t active_irqs = 0;
462
463 active_irqs = lpc->lpc_hc_irqstat & lpc->lpc_hc_irqmask;
464 if (!(lpc->lpc_hc_irqser_ctrl & LPC_HC_IRQSER_EN)) {
465 active_irqs &= ~LPC_HC_IRQ_SERIRQ_ALL;
466 }
467
468 /* Reflect the interrupt */
469 if (lpc->psi_has_serirq) {
470 /*
471 * POWER9 and later have routing fields in OPB master registers that
472 * send LPC irqs to 4 output lines that raise the PSI SERIRQ irqs.
473 * These don't appear to get latched into an OPB register like the
474 * LPCHC irqs.
475 */
476 bool serirq_out[4] = { false, false, false, false };
477 int irq;
478
479 for (irq = 0; irq < ISA_NUM_IRQS; irq++) {
480 if (active_irqs & (LPC_HC_IRQ_SERIRQ0 >> irq)) {
481 serirq_out[lpc->irq_to_serirq_route[irq]] = true;
482 }
483 }
484
485 qemu_set_irq(lpc->psi_irq_serirq[0], serirq_out[0]);
486 qemu_set_irq(lpc->psi_irq_serirq[1], serirq_out[1]);
487 qemu_set_irq(lpc->psi_irq_serirq[2], serirq_out[2]);
488 qemu_set_irq(lpc->psi_irq_serirq[3], serirq_out[3]);
489
490 /*
491 * POWER9 and later LPC controller internal irqs still go via the OPB
492 * and LPCHC PSI irqs like P8, so take the SERIRQs out and continue.
493 */
494 active_irqs &= ~LPC_HC_IRQ_SERIRQ_ALL;
495 }
496
497 /*
498 * POWER8 ORs all irqs together (also with LPCHC internal interrupt
499 * sources) and outputs a single line that raises the PSI LPCHC irq
500 * which then latches an OPB IRQ status register that sends the irq
501 * to PSI.
502 *
503 * We don't honor the polarity register, it's pointless and unused
504 * anyway
505 */
506 if (active_irqs) {
507 lpc->opb_irq_input |= OPB_MASTER_IRQ_LPC;
508 } else {
509 lpc->opb_irq_input &= ~OPB_MASTER_IRQ_LPC;
510 }
511
512 /* Update OPB internal latch */
513 lpc->opb_irq_stat |= lpc->opb_irq_input & lpc->opb_irq_mask;
514
515 qemu_set_irq(lpc->psi_irq_lpchc, lpc->opb_irq_stat != 0);
516 }
517
pnv_lpc_opb_noresponse(PnvLpcController * lpc)518 static void pnv_lpc_opb_noresponse(PnvLpcController *lpc)
519 {
520 lpc->lpc_hc_irqstat |= LPC_HC_IRQ_SYNC_NORESP_ERR;
521 pnv_lpc_eval_irqs(lpc);
522 }
523
lpc_hc_read(void * opaque,hwaddr addr,unsigned size)524 static uint64_t lpc_hc_read(void *opaque, hwaddr addr, unsigned size)
525 {
526 PnvLpcController *lpc = opaque;
527 uint64_t val = 0xfffffffffffffffful;
528
529 switch (addr) {
530 case LPC_HC_FW_SEG_IDSEL:
531 val = lpc->lpc_hc_fw_seg_idsel;
532 break;
533 case LPC_HC_FW_RD_ACC_SIZE:
534 val = lpc->lpc_hc_fw_rd_acc_size;
535 break;
536 case LPC_HC_IRQSER_CTRL:
537 val = lpc->lpc_hc_irqser_ctrl;
538 break;
539 case LPC_HC_IRQMASK:
540 val = lpc->lpc_hc_irqmask;
541 break;
542 case LPC_HC_IRQSTAT:
543 val = lpc->lpc_hc_irqstat;
544 break;
545 case LPC_HC_ERROR_ADDRESS:
546 val = lpc->lpc_hc_error_addr;
547 break;
548 default:
549 qemu_log_mask(LOG_UNIMP, "LPC HC Unimplemented register: 0x%"
550 HWADDR_PRIx "\n", addr);
551 }
552 return val;
553 }
554
lpc_hc_write(void * opaque,hwaddr addr,uint64_t val,unsigned size)555 static void lpc_hc_write(void *opaque, hwaddr addr, uint64_t val,
556 unsigned size)
557 {
558 PnvLpcController *lpc = opaque;
559
560 /* XXX Filter out reserved bits */
561
562 switch (addr) {
563 case LPC_HC_FW_SEG_IDSEL:
564 /*
565 * ISA FW "devices" are modeled as 16x256MB windows into a
566 * 4GB LPC FW address space.
567 */
568 val &= 0xf; /* Selects device 0-15 */
569 lpc->lpc_hc_fw_seg_idsel = val;
570 memory_region_set_alias_offset(&lpc->opb_isa_fw, val * LPC_FW_OPB_SIZE);
571 break;
572 case LPC_HC_FW_RD_ACC_SIZE:
573 lpc->lpc_hc_fw_rd_acc_size = val;
574 break;
575 case LPC_HC_IRQSER_CTRL:
576 lpc->lpc_hc_irqser_ctrl = val;
577 pnv_lpc_eval_irqs(lpc);
578 break;
579 case LPC_HC_IRQMASK:
580 lpc->lpc_hc_irqmask = val;
581 pnv_lpc_eval_irqs(lpc);
582 break;
583 case LPC_HC_IRQSTAT:
584 /*
585 * This register is write-to-clear for the IRQSER (LPC device IRQ)
586 * status. However if the device has not de-asserted its interrupt
587 * that will just raise this IRQ status bit again. Model this by
588 * keeping track of the inputs and only clearing if the inputs are
589 * deasserted.
590 */
591 lpc->lpc_hc_irqstat &= ~(val & ~lpc->lpc_hc_irq_inputs);
592 pnv_lpc_eval_irqs(lpc);
593 break;
594 case LPC_HC_ERROR_ADDRESS:
595 break;
596 default:
597 qemu_log_mask(LOG_UNIMP, "LPC HC Unimplemented register: 0x%"
598 HWADDR_PRIx "\n", addr);
599 }
600 }
601
602 static const MemoryRegionOps lpc_hc_ops = {
603 .read = lpc_hc_read,
604 .write = lpc_hc_write,
605 .endianness = DEVICE_BIG_ENDIAN,
606 .valid = {
607 .min_access_size = 4,
608 .max_access_size = 4,
609 },
610 .impl = {
611 .min_access_size = 4,
612 .max_access_size = 4,
613 },
614 };
615
opb_master_read(void * opaque,hwaddr addr,unsigned size)616 static uint64_t opb_master_read(void *opaque, hwaddr addr, unsigned size)
617 {
618 PnvLpcController *lpc = opaque;
619 uint64_t val = 0xfffffffffffffffful;
620
621 switch (addr) {
622 case OPB_MASTER_LS_ROUTE0:
623 val = lpc->opb_irq_route0;
624 break;
625 case OPB_MASTER_LS_ROUTE1:
626 val = lpc->opb_irq_route1;
627 break;
628 case OPB_MASTER_LS_IRQ_STAT:
629 val = lpc->opb_irq_stat;
630 break;
631 case OPB_MASTER_LS_IRQ_MASK:
632 val = lpc->opb_irq_mask;
633 break;
634 case OPB_MASTER_LS_IRQ_POL:
635 val = lpc->opb_irq_pol;
636 break;
637 case OPB_MASTER_LS_IRQ_INPUT:
638 val = lpc->opb_irq_input;
639 break;
640 default:
641 qemu_log_mask(LOG_UNIMP, "OPBM: read on unimplemented register: 0x%"
642 HWADDR_PRIx "\n", addr);
643 }
644
645 return val;
646 }
647
opb_master_write(void * opaque,hwaddr addr,uint64_t val,unsigned size)648 static void opb_master_write(void *opaque, hwaddr addr,
649 uint64_t val, unsigned size)
650 {
651 PnvLpcController *lpc = opaque;
652
653 switch (addr) {
654 case OPB_MASTER_LS_ROUTE0:
655 lpc->opb_irq_route0 = val;
656 pnv_lpc_eval_serirq_routes(lpc);
657 pnv_lpc_eval_irqs(lpc);
658 break;
659 case OPB_MASTER_LS_ROUTE1:
660 lpc->opb_irq_route1 = val;
661 pnv_lpc_eval_serirq_routes(lpc);
662 pnv_lpc_eval_irqs(lpc);
663 break;
664 case OPB_MASTER_LS_IRQ_STAT:
665 lpc->opb_irq_stat &= ~val;
666 pnv_lpc_eval_irqs(lpc);
667 break;
668 case OPB_MASTER_LS_IRQ_MASK:
669 lpc->opb_irq_mask = val;
670 pnv_lpc_eval_irqs(lpc);
671 break;
672 case OPB_MASTER_LS_IRQ_POL:
673 lpc->opb_irq_pol = val;
674 pnv_lpc_eval_irqs(lpc);
675 break;
676 case OPB_MASTER_LS_IRQ_INPUT:
677 /* Read only */
678 break;
679 default:
680 qemu_log_mask(LOG_UNIMP, "OPBM: write on unimplemented register: 0x%"
681 HWADDR_PRIx " val=0x%08"PRIx64"\n", addr, val);
682 }
683 }
684
685 static const MemoryRegionOps opb_master_ops = {
686 .read = opb_master_read,
687 .write = opb_master_write,
688 .endianness = DEVICE_BIG_ENDIAN,
689 .valid = {
690 .min_access_size = 4,
691 .max_access_size = 4,
692 },
693 .impl = {
694 .min_access_size = 4,
695 .max_access_size = 4,
696 },
697 };
698
pnv_lpc_power8_realize(DeviceState * dev,Error ** errp)699 static void pnv_lpc_power8_realize(DeviceState *dev, Error **errp)
700 {
701 PnvLpcController *lpc = PNV_LPC(dev);
702 PnvLpcClass *plc = PNV_LPC_GET_CLASS(dev);
703 Error *local_err = NULL;
704
705 plc->parent_realize(dev, &local_err);
706 if (local_err) {
707 error_propagate(errp, local_err);
708 return;
709 }
710
711 /* P8 uses a XSCOM region for LPC registers */
712 pnv_xscom_region_init(&lpc->xscom_regs, OBJECT(lpc),
713 &pnv_lpc_xscom_ops, lpc, "xscom-lpc",
714 PNV_XSCOM_LPC_SIZE);
715 }
716
pnv_lpc_power8_class_init(ObjectClass * klass,const void * data)717 static void pnv_lpc_power8_class_init(ObjectClass *klass, const void *data)
718 {
719 DeviceClass *dc = DEVICE_CLASS(klass);
720 PnvXScomInterfaceClass *xdc = PNV_XSCOM_INTERFACE_CLASS(klass);
721 PnvLpcClass *plc = PNV_LPC_CLASS(klass);
722
723 dc->desc = "PowerNV LPC Controller POWER8";
724
725 xdc->dt_xscom = pnv_lpc_dt_xscom;
726
727 device_class_set_parent_realize(dc, pnv_lpc_power8_realize,
728 &plc->parent_realize);
729 }
730
731 static const TypeInfo pnv_lpc_power8_info = {
732 .name = TYPE_PNV8_LPC,
733 .parent = TYPE_PNV_LPC,
734 .class_init = pnv_lpc_power8_class_init,
735 .interfaces = (const InterfaceInfo[]) {
736 { TYPE_PNV_XSCOM_INTERFACE },
737 { }
738 }
739 };
740
pnv_lpc_power9_realize(DeviceState * dev,Error ** errp)741 static void pnv_lpc_power9_realize(DeviceState *dev, Error **errp)
742 {
743 PnvLpcController *lpc = PNV_LPC(dev);
744 PnvLpcClass *plc = PNV_LPC_GET_CLASS(dev);
745 Error *local_err = NULL;
746
747 object_property_set_bool(OBJECT(lpc), "psi-serirq", true, &error_abort);
748
749 plc->parent_realize(dev, &local_err);
750 if (local_err) {
751 error_propagate(errp, local_err);
752 return;
753 }
754
755 /* P9 uses a MMIO region */
756 memory_region_init_io(&lpc->xscom_regs, OBJECT(lpc), &pnv_lpc_mmio_ops,
757 lpc, "lpcm", PNV9_LPCM_SIZE);
758
759 /* P9 LPC routes ISA irqs to 4 PSI SERIRQ lines */
760 qdev_init_gpio_out_named(dev, lpc->psi_irq_serirq, "SERIRQ", 4);
761 }
762
pnv_lpc_power9_class_init(ObjectClass * klass,const void * data)763 static void pnv_lpc_power9_class_init(ObjectClass *klass, const void *data)
764 {
765 DeviceClass *dc = DEVICE_CLASS(klass);
766 PnvLpcClass *plc = PNV_LPC_CLASS(klass);
767
768 dc->desc = "PowerNV LPC Controller POWER9";
769
770 device_class_set_parent_realize(dc, pnv_lpc_power9_realize,
771 &plc->parent_realize);
772 }
773
774 static const TypeInfo pnv_lpc_power9_info = {
775 .name = TYPE_PNV9_LPC,
776 .parent = TYPE_PNV_LPC,
777 .class_init = pnv_lpc_power9_class_init,
778 };
779
pnv_lpc_power10_class_init(ObjectClass * klass,const void * data)780 static void pnv_lpc_power10_class_init(ObjectClass *klass, const void *data)
781 {
782 DeviceClass *dc = DEVICE_CLASS(klass);
783
784 dc->desc = "PowerNV LPC Controller POWER10";
785 }
786
787 static const TypeInfo pnv_lpc_power10_info = {
788 .name = TYPE_PNV10_LPC,
789 .parent = TYPE_PNV9_LPC,
790 .class_init = pnv_lpc_power10_class_init,
791 };
792
pnv_lpc_realize(DeviceState * dev,Error ** errp)793 static void pnv_lpc_realize(DeviceState *dev, Error **errp)
794 {
795 PnvLpcController *lpc = PNV_LPC(dev);
796
797 /* Reg inits */
798 lpc->lpc_hc_fw_rd_acc_size = LPC_HC_FW_RD_4B;
799
800 /* Create address space and backing MR for the OPB bus */
801 memory_region_init(&lpc->opb_mr, OBJECT(dev), "lpc-opb", 0x100000000ull);
802 address_space_init(&lpc->opb_as, &lpc->opb_mr, "lpc-opb");
803
804 /*
805 * Create ISA IO, Mem, and FW space regions which are the root of
806 * the ISA bus (ie, ISA address spaces).
807 */
808 memory_region_init(&lpc->isa_io, OBJECT(dev), "isa-io", ISA_IO_SIZE);
809 memory_region_init(&lpc->isa_mem, OBJECT(dev), "isa-mem", ISA_MEM_SIZE);
810 memory_region_init(&lpc->isa_fw, OBJECT(dev), "isa-fw", ISA_FW_SIZE);
811
812 /* Create windows from the OPB space to the ISA space */
813 memory_region_init_alias(&lpc->opb_isa_io, OBJECT(dev), "lpc-isa-io",
814 &lpc->isa_io, 0, LPC_IO_OPB_SIZE);
815 memory_region_add_subregion(&lpc->opb_mr, LPC_IO_OPB_ADDR,
816 &lpc->opb_isa_io);
817 memory_region_init_alias(&lpc->opb_isa_mem, OBJECT(dev), "lpc-isa-mem",
818 &lpc->isa_mem, 0, LPC_MEM_OPB_SIZE);
819 memory_region_add_subregion(&lpc->opb_mr, LPC_MEM_OPB_ADDR,
820 &lpc->opb_isa_mem);
821 memory_region_init_alias(&lpc->opb_isa_fw, OBJECT(dev), "lpc-isa-fw",
822 &lpc->isa_fw, 0, LPC_FW_OPB_SIZE);
823 memory_region_add_subregion(&lpc->opb_mr, LPC_FW_OPB_ADDR,
824 &lpc->opb_isa_fw);
825
826 /* Create MMIO regions for LPC HC and OPB registers */
827 memory_region_init_io(&lpc->opb_master_regs, OBJECT(dev), &opb_master_ops,
828 lpc, "lpc-opb-master", LPC_OPB_REGS_OPB_SIZE);
829 lpc->opb_master_regs.disable_reentrancy_guard = true;
830 memory_region_add_subregion(&lpc->opb_mr, LPC_OPB_REGS_OPB_ADDR,
831 &lpc->opb_master_regs);
832 memory_region_init_io(&lpc->lpc_hc_regs, OBJECT(dev), &lpc_hc_ops, lpc,
833 "lpc-hc", LPC_HC_REGS_OPB_SIZE);
834 /* xscom writes to lpc-hc. As such mark lpc-hc re-entrancy safe */
835 lpc->lpc_hc_regs.disable_reentrancy_guard = true;
836 memory_region_add_subregion(&lpc->opb_mr, LPC_HC_REGS_OPB_ADDR,
837 &lpc->lpc_hc_regs);
838
839 qdev_init_gpio_out_named(dev, &lpc->psi_irq_lpchc, "LPCHC", 1);
840 }
841
842 static const Property pnv_lpc_properties[] = {
843 DEFINE_PROP_BOOL("psi-serirq", PnvLpcController, psi_has_serirq, false),
844 };
845
pnv_lpc_class_init(ObjectClass * klass,const void * data)846 static void pnv_lpc_class_init(ObjectClass *klass, const void *data)
847 {
848 DeviceClass *dc = DEVICE_CLASS(klass);
849
850 device_class_set_props(dc, pnv_lpc_properties);
851 dc->realize = pnv_lpc_realize;
852 dc->desc = "PowerNV LPC Controller";
853 dc->user_creatable = false;
854 }
855
856 static const TypeInfo pnv_lpc_info = {
857 .name = TYPE_PNV_LPC,
858 .parent = TYPE_DEVICE,
859 .instance_size = sizeof(PnvLpcController),
860 .class_init = pnv_lpc_class_init,
861 .class_size = sizeof(PnvLpcClass),
862 .abstract = true,
863 };
864
pnv_lpc_register_types(void)865 static void pnv_lpc_register_types(void)
866 {
867 type_register_static(&pnv_lpc_info);
868 type_register_static(&pnv_lpc_power8_info);
869 type_register_static(&pnv_lpc_power9_info);
870 type_register_static(&pnv_lpc_power10_info);
871 }
872
type_init(pnv_lpc_register_types)873 type_init(pnv_lpc_register_types)
874
875 /* If we don't use the built-in LPC interrupt deserializer, we need
876 * to provide a set of qirqs for the ISA bus or things will go bad.
877 *
878 * Most machines using pre-Naples chips (without said deserializer)
879 * have a CPLD that will collect the SerIRQ and shoot them as a
880 * single level interrupt to the P8 chip. So let's setup a hook
881 * for doing just that.
882 */
883 static void pnv_lpc_isa_irq_handler_cpld(void *opaque, int n, int level)
884 {
885 PnvMachineState *pnv = PNV_MACHINE(qdev_get_machine());
886 uint32_t old_state = pnv->cpld_irqstate;
887 PnvLpcController *lpc = PNV_LPC(opaque);
888
889 if (level) {
890 pnv->cpld_irqstate |= 1u << n;
891 } else {
892 pnv->cpld_irqstate &= ~(1u << n);
893 }
894
895 if (pnv->cpld_irqstate != old_state) {
896 qemu_set_irq(lpc->psi_irq_lpchc, pnv->cpld_irqstate != 0);
897 }
898 }
899
pnv_lpc_isa_irq_handler(void * opaque,int n,int level)900 static void pnv_lpc_isa_irq_handler(void *opaque, int n, int level)
901 {
902 PnvLpcController *lpc = PNV_LPC(opaque);
903 uint32_t irq_bit = LPC_HC_IRQ_SERIRQ0 >> n;
904
905 if (level) {
906 lpc->lpc_hc_irq_inputs |= irq_bit;
907
908 /*
909 * The LPC HC in Naples and later latches LPC IRQ into a bit field in
910 * the IRQSTAT register, and that drives the PSI IRQ to the IC.
911 * Software clears this bit manually (see LPC_HC_IRQSTAT handler).
912 */
913 lpc->lpc_hc_irqstat |= irq_bit;
914 pnv_lpc_eval_irqs(lpc);
915 } else {
916 lpc->lpc_hc_irq_inputs &= ~irq_bit;
917
918 /* POWER9 adds an auto-clear mode that clears IRQSTAT bits on EOI */
919 if (lpc->psi_has_serirq &&
920 (lpc->lpc_hc_irqser_ctrl & LPC_HC_IRQSER_AUTO_CLEAR)) {
921 lpc->lpc_hc_irqstat &= ~irq_bit;
922 pnv_lpc_eval_irqs(lpc);
923 }
924 }
925 }
926
pnv_lpc_isa_create(PnvLpcController * lpc,bool use_cpld,Error ** errp)927 ISABus *pnv_lpc_isa_create(PnvLpcController *lpc, bool use_cpld, Error **errp)
928 {
929 Error *local_err = NULL;
930 ISABus *isa_bus;
931 qemu_irq *irqs;
932 qemu_irq_handler handler;
933
934 /* let isa_bus_new() create its own bridge on SysBus otherwise
935 * devices specified on the command line won't find the bus and
936 * will fail to create.
937 */
938 isa_bus = isa_bus_new(NULL, &lpc->isa_mem, &lpc->isa_io, &local_err);
939 if (local_err) {
940 error_propagate(errp, local_err);
941 return NULL;
942 }
943
944 /* Not all variants have a working serial irq decoder. If not,
945 * handling of LPC interrupts becomes a platform issue (some
946 * platforms have a CPLD to do it).
947 */
948 if (use_cpld) {
949 handler = pnv_lpc_isa_irq_handler_cpld;
950 } else {
951 handler = pnv_lpc_isa_irq_handler;
952 }
953
954 /* POWER has a 17th irq, QEMU only implements the 16 regular device irqs */
955 irqs = qemu_allocate_irqs(handler, lpc, ISA_NUM_IRQS);
956
957 isa_bus_register_input_irqs(isa_bus, irqs);
958
959 return isa_bus;
960 }
961