xref: /openbmc/qemu/hw/intc/pnv_xive2.c (revision d273abbfba47ef47e331dc22731c7d2a15e95c9a)
1 /*
2  * QEMU PowerPC XIVE2 interrupt controller model  (POWER10)
3  *
4  * Copyright (c) 2019-2024, IBM Corporation.
5  *
6  * SPDX-License-Identifier: GPL-2.0-or-later
7  */
8 
9 #include "qemu/osdep.h"
10 #include "qemu/log.h"
11 #include "qapi/error.h"
12 #include "target/ppc/cpu.h"
13 #include "system/cpus.h"
14 #include "system/dma.h"
15 #include "hw/ppc/fdt.h"
16 #include "hw/ppc/pnv.h"
17 #include "hw/ppc/pnv_chip.h"
18 #include "hw/ppc/pnv_core.h"
19 #include "hw/ppc/pnv_xscom.h"
20 #include "hw/ppc/xive2.h"
21 #include "hw/ppc/pnv_xive.h"
22 #include "hw/ppc/xive_regs.h"
23 #include "hw/ppc/xive2_regs.h"
24 #include "hw/ppc/ppc.h"
25 #include "hw/qdev-properties.h"
26 #include "system/reset.h"
27 #include "system/qtest.h"
28 
29 #include <libfdt.h>
30 
31 #include "pnv_xive2_regs.h"
32 
33 #undef XIVE2_DEBUG
34 
35 /* XIVE Sync or Flush Notification Block */
36 typedef struct XiveSfnBlock {
37     uint8_t bytes[32];
38 } XiveSfnBlock;
39 
40 /* XIVE Thread Sync or Flush Notification Area */
41 typedef struct XiveThreadNA {
42     XiveSfnBlock topo[16];
43 } XiveThreadNA;
44 
45 /*
46  * Virtual structures table (VST)
47  */
48 #define SBE_PER_BYTE   4
49 
50 typedef struct XiveVstInfo {
51     const char *name;
52     uint32_t    size;
53     uint32_t    max_blocks;
54 } XiveVstInfo;
55 
56 static const XiveVstInfo vst_infos[] = {
57 
58     [VST_EAS]  = { "EAT",  sizeof(Xive2Eas),     16 },
59     [VST_ESB]  = { "ESB",  1,                    16 },
60     [VST_END]  = { "ENDT", sizeof(Xive2End),     16 },
61 
62     [VST_NVP]  = { "NVPT", sizeof(Xive2Nvp),     16 },
63     [VST_NVG]  = { "NVGT", sizeof(Xive2Nvgc),    16 },
64     [VST_NVC]  = { "NVCT", sizeof(Xive2Nvgc),    16 },
65 
66     [VST_IC]  =  { "IC",   1, /* ? */            16 }, /* Topology # */
67     [VST_SYNC] = { "SYNC", sizeof(XiveThreadNA), 16 }, /* Topology # */
68 
69     /*
70      * This table contains the backing store pages for the interrupt
71      * fifos of the VC sub-engine in case of overflow.
72      *
73      * 0 - IPI,
74      * 1 - HWD,
75      * 2 - NxC,
76      * 3 - INT,
77      * 4 - OS-Queue,
78      * 5 - Pool-Queue,
79      * 6 - Hard-Queue
80      */
81     [VST_ERQ]  = { "ERQ",  1,                   VC_QUEUE_COUNT },
82 };
83 
84 #define xive2_error(xive, fmt, ...)                                      \
85     qemu_log_mask(LOG_GUEST_ERROR, "XIVE[%x] - " fmt "\n",              \
86                   (xive)->chip->chip_id, ## __VA_ARGS__);
87 
88 /*
89  * TODO: Document block id override
90  */
91 static uint32_t pnv_xive2_block_id(PnvXive2 *xive)
92 {
93     uint8_t blk = xive->chip->chip_id;
94     uint64_t cfg_val = xive->cq_regs[CQ_XIVE_CFG >> 3];
95 
96     if (cfg_val & CQ_XIVE_CFG_HYP_HARD_BLKID_OVERRIDE) {
97         blk = GETFIELD(CQ_XIVE_CFG_HYP_HARD_BLOCK_ID, cfg_val);
98     }
99 
100     return blk;
101 }
102 
103 /*
104  * Remote access to INT controllers. HW uses MMIOs(?). For now, a simple
105  * scan of all the chips INT controller is good enough.
106  */
107 static PnvXive2 *pnv_xive2_get_remote(uint32_t vsd_type, hwaddr fwd_addr)
108 {
109     PnvMachineState *pnv = PNV_MACHINE(qdev_get_machine());
110     int i;
111 
112     for (i = 0; i < pnv->num_chips; i++) {
113         Pnv10Chip *chip10 = PNV10_CHIP(pnv->chips[i]);
114         PnvXive2 *xive = &chip10->xive;
115 
116         /*
117          * Is this the XIVE matching the forwarded VSD address is for this
118          * VSD type
119          */
120         if ((vsd_type == VST_ESB   && fwd_addr == xive->esb_base) ||
121             (vsd_type == VST_END   && fwd_addr == xive->end_base)  ||
122             ((vsd_type == VST_NVP ||
123               vsd_type == VST_NVG) && fwd_addr == xive->nvpg_base) ||
124             (vsd_type == VST_NVC   && fwd_addr == xive->nvc_base)) {
125             return xive;
126         }
127     }
128 
129     qemu_log_mask(LOG_GUEST_ERROR,
130                  "XIVE: >>>>> %s vsd_type %u  fwd_addr 0x%"HWADDR_PRIx
131                   " NOT FOUND\n",
132                   __func__, vsd_type, fwd_addr);
133     return NULL;
134 }
135 
136 /*
137  * VST accessors for ESB, EAT, ENDT, NVP
138  *
139  * Indirect VST tables are arrays of VSDs pointing to a page (of same
140  * size). Each page is a direct VST table.
141  */
142 
143 #define XIVE_VSD_SIZE 8
144 
145 /* Indirect page size can be 4K, 64K, 2M, 16M. */
146 static uint64_t pnv_xive2_vst_page_size_allowed(uint32_t page_shift)
147 {
148      return page_shift == 12 || page_shift == 16 ||
149          page_shift == 21 || page_shift == 24;
150 }
151 
152 static uint64_t pnv_xive2_vst_addr_direct(PnvXive2 *xive, uint32_t type,
153                                           uint64_t vsd, uint32_t idx)
154 {
155     const XiveVstInfo *info = &vst_infos[type];
156     uint64_t vst_addr = vsd & VSD_ADDRESS_MASK;
157     uint64_t vst_tsize = 1ull << (GETFIELD(VSD_TSIZE, vsd) + 12);
158     uint32_t idx_max;
159 
160     idx_max = vst_tsize / info->size - 1;
161     if (idx > idx_max) {
162 #ifdef XIVE2_DEBUG
163         xive2_error(xive, "VST: %s entry %x out of range [ 0 .. %x ] !?",
164                    info->name, idx, idx_max);
165 #endif
166         return 0;
167     }
168 
169     return vst_addr + idx * info->size;
170 }
171 
172 static uint64_t pnv_xive2_vst_addr_indirect(PnvXive2 *xive, uint32_t type,
173                                             uint64_t vsd, uint32_t idx)
174 {
175     const XiveVstInfo *info = &vst_infos[type];
176     uint64_t vsd_addr;
177     uint32_t vsd_idx;
178     uint32_t page_shift;
179     uint32_t vst_per_page;
180 
181     /* Get the page size of the indirect table. */
182     vsd_addr = vsd & VSD_ADDRESS_MASK;
183     ldq_be_dma(&address_space_memory, vsd_addr, &vsd, MEMTXATTRS_UNSPECIFIED);
184 
185     if (!(vsd & VSD_ADDRESS_MASK)) {
186 #ifdef XIVE2_DEBUG
187         xive2_error(xive, "VST: invalid %s entry %x !?", info->name, idx);
188 #endif
189         return 0;
190     }
191 
192     page_shift = GETFIELD(VSD_TSIZE, vsd) + 12;
193 
194     if (!pnv_xive2_vst_page_size_allowed(page_shift)) {
195         xive2_error(xive, "VST: invalid %s page shift %d", info->name,
196                    page_shift);
197         return 0;
198     }
199 
200     vst_per_page = (1ull << page_shift) / info->size;
201     vsd_idx = idx / vst_per_page;
202 
203     /* Load the VSD we are looking for, if not already done */
204     if (vsd_idx) {
205         vsd_addr = vsd_addr + vsd_idx * XIVE_VSD_SIZE;
206         ldq_be_dma(&address_space_memory, vsd_addr, &vsd,
207                    MEMTXATTRS_UNSPECIFIED);
208 
209         if (!(vsd & VSD_ADDRESS_MASK)) {
210 #ifdef XIVE2_DEBUG
211             xive2_error(xive, "VST: invalid %s entry %x !?", info->name, idx);
212 #endif
213             return 0;
214         }
215 
216         /*
217          * Check that the pages have a consistent size across the
218          * indirect table
219          */
220         if (page_shift != GETFIELD(VSD_TSIZE, vsd) + 12) {
221             xive2_error(xive, "VST: %s entry %x indirect page size differ !?",
222                        info->name, idx);
223             return 0;
224         }
225     }
226 
227     return pnv_xive2_vst_addr_direct(xive, type, vsd, (idx % vst_per_page));
228 }
229 
230 static uint8_t pnv_xive2_nvc_table_compress_shift(PnvXive2 *xive)
231 {
232     uint8_t shift =  GETFIELD(PC_NXC_PROC_CONFIG_NVC_TABLE_COMPRESS,
233                               xive->pc_regs[PC_NXC_PROC_CONFIG >> 3]);
234     return shift > 8 ? 0 : shift;
235 }
236 
237 static uint8_t pnv_xive2_nvg_table_compress_shift(PnvXive2 *xive)
238 {
239     uint8_t shift = GETFIELD(PC_NXC_PROC_CONFIG_NVG_TABLE_COMPRESS,
240                              xive->pc_regs[PC_NXC_PROC_CONFIG >> 3]);
241     return shift > 8 ? 0 : shift;
242 }
243 
244 static uint64_t pnv_xive2_vst_addr(PnvXive2 *xive, uint32_t type, uint8_t blk,
245                                    uint32_t idx)
246 {
247     const XiveVstInfo *info = &vst_infos[type];
248     uint64_t vsd;
249 
250     if (blk >= info->max_blocks) {
251         xive2_error(xive, "VST: invalid block id %d for VST %s %d !?",
252                    blk, info->name, idx);
253         return 0;
254     }
255 
256     vsd = xive->vsds[type][blk];
257     if (vsd == 0) {
258         xive2_error(xive, "VST: vsd == 0 block id %d for VST %s %d !?",
259                    blk, info->name, idx);
260         return 0;
261     }
262 
263     /* Remote VST access */
264     if (GETFIELD(VSD_MODE, vsd) == VSD_MODE_FORWARD) {
265         xive = pnv_xive2_get_remote(type, (vsd & VSD_ADDRESS_MASK));
266         return xive ? pnv_xive2_vst_addr(xive, type, blk, idx) : 0;
267     }
268 
269     if (type == VST_NVG) {
270         idx >>= pnv_xive2_nvg_table_compress_shift(xive);
271     } else if (type == VST_NVC) {
272         idx >>= pnv_xive2_nvc_table_compress_shift(xive);
273     }
274 
275     if (VSD_INDIRECT & vsd) {
276         return pnv_xive2_vst_addr_indirect(xive, type, vsd, idx);
277     }
278 
279     return pnv_xive2_vst_addr_direct(xive, type, vsd, idx);
280 }
281 
282 static int pnv_xive2_vst_read(PnvXive2 *xive, uint32_t type, uint8_t blk,
283                              uint32_t idx, void *data)
284 {
285     const XiveVstInfo *info = &vst_infos[type];
286     uint64_t addr = pnv_xive2_vst_addr(xive, type, blk, idx);
287     MemTxResult result;
288 
289     if (!addr) {
290         return -1;
291     }
292 
293     result = address_space_read(&address_space_memory, addr,
294                                 MEMTXATTRS_UNSPECIFIED, data,
295                                 info->size);
296     if (result != MEMTX_OK) {
297         xive2_error(xive, "VST: read failed at @0x%" HWADDR_PRIx
298                    " for VST %s %x/%x\n", addr, info->name, blk, idx);
299         return -1;
300     }
301     return 0;
302 }
303 
304 #define XIVE_VST_WORD_ALL -1
305 
306 static int pnv_xive2_vst_write(PnvXive2 *xive, uint32_t type, uint8_t blk,
307                                uint32_t idx, void *data, uint32_t word_number)
308 {
309     const XiveVstInfo *info = &vst_infos[type];
310     uint64_t addr = pnv_xive2_vst_addr(xive, type, blk, idx);
311     MemTxResult result;
312 
313     if (!addr) {
314         return -1;
315     }
316 
317     if (word_number == XIVE_VST_WORD_ALL) {
318         result = address_space_write(&address_space_memory, addr,
319                                      MEMTXATTRS_UNSPECIFIED, data,
320                                      info->size);
321     } else {
322         result = address_space_write(&address_space_memory,
323                                      addr + word_number * 4,
324                                      MEMTXATTRS_UNSPECIFIED,
325                                      data + word_number * 4, 4);
326     }
327 
328     if (result != MEMTX_OK) {
329         xive2_error(xive, "VST: write failed at @0x%" HWADDR_PRIx
330                    "for VST %s %x/%x\n", addr, info->name, blk, idx);
331         return -1;
332     }
333     return 0;
334 }
335 
336 static int pnv_xive2_get_pq(Xive2Router *xrtr, uint8_t blk, uint32_t idx,
337                              uint8_t *pq)
338 {
339     PnvXive2 *xive = PNV_XIVE2(xrtr);
340 
341     if (pnv_xive2_block_id(xive) != blk) {
342         xive2_error(xive, "VST: EAS %x is remote !?", XIVE_EAS(blk, idx));
343         return -1;
344     }
345 
346     *pq = xive_source_esb_get(&xive->ipi_source, idx);
347     return 0;
348 }
349 
350 static int pnv_xive2_set_pq(Xive2Router *xrtr, uint8_t blk, uint32_t idx,
351                              uint8_t *pq)
352 {
353     PnvXive2 *xive = PNV_XIVE2(xrtr);
354 
355     if (pnv_xive2_block_id(xive) != blk) {
356         xive2_error(xive, "VST: EAS %x is remote !?", XIVE_EAS(blk, idx));
357         return -1;
358     }
359 
360     *pq = xive_source_esb_set(&xive->ipi_source, idx, *pq);
361     return 0;
362 }
363 
364 static int pnv_xive2_get_end(Xive2Router *xrtr, uint8_t blk, uint32_t idx,
365                              Xive2End *end)
366 {
367     return pnv_xive2_vst_read(PNV_XIVE2(xrtr), VST_END, blk, idx, end);
368 }
369 
370 static int pnv_xive2_write_end(Xive2Router *xrtr, uint8_t blk, uint32_t idx,
371                                Xive2End *end, uint8_t word_number)
372 {
373     return pnv_xive2_vst_write(PNV_XIVE2(xrtr), VST_END, blk, idx, end,
374                               word_number);
375 }
376 
377 static inline int pnv_xive2_get_current_pir(PnvXive2 *xive)
378 {
379     if (!qtest_enabled()) {
380         PowerPCCPU *cpu = POWERPC_CPU(current_cpu);
381         return ppc_cpu_pir(cpu);
382     }
383     return 0;
384 }
385 
386 /*
387  * After SW injects a Queue Sync or Cache Flush operation, HW will notify
388  * SW of the completion of the operation by writing a byte of all 1's (0xff)
389  * to a specific memory location.  The memory location is calculated by first
390  * looking up a base address in the SYNC VSD using the Topology ID of the
391  * originating thread as the "block" number.  This points to a
392  * 64k block of memory that is further divided into 128 512 byte chunks of
393  * memory, which is indexed by the thread id of the requesting thread.
394  * Finally, this 512 byte chunk of memory is divided into 16 32 byte
395  * chunks which are indexed by the topology id of the targeted IC's chip.
396  * The values below are the offsets into that 32 byte chunk of memory for
397  * each type of cache flush or queue sync operation.
398  */
399 #define PNV_XIVE2_QUEUE_IPI              0x00
400 #define PNV_XIVE2_QUEUE_HW               0x01
401 #define PNV_XIVE2_QUEUE_NXC              0x02
402 #define PNV_XIVE2_QUEUE_INT              0x03
403 #define PNV_XIVE2_QUEUE_OS               0x04
404 #define PNV_XIVE2_QUEUE_POOL             0x05
405 #define PNV_XIVE2_QUEUE_HARD             0x06
406 #define PNV_XIVE2_CACHE_ENDC             0x08
407 #define PNV_XIVE2_CACHE_ESBC             0x09
408 #define PNV_XIVE2_CACHE_EASC             0x0a
409 #define PNV_XIVE2_QUEUE_NXC_LD_LCL_NCO   0x10
410 #define PNV_XIVE2_QUEUE_NXC_LD_LCL_CO    0x11
411 #define PNV_XIVE2_QUEUE_NXC_ST_LCL_NCI   0x12
412 #define PNV_XIVE2_QUEUE_NXC_ST_LCL_CI    0x13
413 #define PNV_XIVE2_QUEUE_NXC_ST_RMT_NCI   0x14
414 #define PNV_XIVE2_QUEUE_NXC_ST_RMT_CI    0x15
415 #define PNV_XIVE2_CACHE_NXC              0x18
416 
417 static int pnv_xive2_inject_notify(PnvXive2 *xive, int type)
418 {
419     uint64_t addr;
420     int pir = pnv_xive2_get_current_pir(xive);
421     int thread_nr = PNV10_PIR2THREAD(pir);
422     int thread_topo_id = PNV10_PIR2CHIP(pir);
423     int ic_topo_id = xive->chip->chip_id;
424     uint64_t offset = ic_topo_id * sizeof(XiveSfnBlock);
425     uint8_t byte = 0xff;
426     MemTxResult result;
427 
428     /* Retrieve the address of requesting thread's notification area */
429     addr = pnv_xive2_vst_addr(xive, VST_SYNC, thread_topo_id, thread_nr);
430 
431     if (!addr) {
432         xive2_error(xive, "VST: no SYNC entry %x/%x !?",
433                     thread_topo_id, thread_nr);
434         return -1;
435     }
436 
437     address_space_stb(&address_space_memory, addr + offset + type, byte,
438                       MEMTXATTRS_UNSPECIFIED, &result);
439     assert(result == MEMTX_OK);
440 
441     return 0;
442 }
443 
444 static int pnv_xive2_end_update(PnvXive2 *xive, uint8_t watch_engine)
445 {
446     uint8_t  blk;
447     uint32_t idx;
448     int i, spec_reg, data_reg;
449     uint64_t endc_watch[4];
450 
451     assert(watch_engine < ARRAY_SIZE(endc_watch));
452 
453     spec_reg = (VC_ENDC_WATCH0_SPEC + watch_engine * 0x40) >> 3;
454     data_reg = (VC_ENDC_WATCH0_DATA0 + watch_engine * 0x40) >> 3;
455     blk = GETFIELD(VC_ENDC_WATCH_BLOCK_ID, xive->vc_regs[spec_reg]);
456     idx = GETFIELD(VC_ENDC_WATCH_INDEX, xive->vc_regs[spec_reg]);
457 
458     for (i = 0; i < ARRAY_SIZE(endc_watch); i++) {
459         endc_watch[i] = cpu_to_be64(xive->vc_regs[data_reg + i]);
460     }
461 
462     return pnv_xive2_vst_write(xive, VST_END, blk, idx, endc_watch,
463                               XIVE_VST_WORD_ALL);
464 }
465 
466 static void pnv_xive2_end_cache_load(PnvXive2 *xive, uint8_t watch_engine)
467 {
468     uint8_t  blk;
469     uint32_t idx;
470     uint64_t endc_watch[4] = { 0 };
471     int i, spec_reg, data_reg;
472 
473     assert(watch_engine < ARRAY_SIZE(endc_watch));
474 
475     spec_reg = (VC_ENDC_WATCH0_SPEC + watch_engine * 0x40) >> 3;
476     data_reg = (VC_ENDC_WATCH0_DATA0 + watch_engine * 0x40) >> 3;
477     blk = GETFIELD(VC_ENDC_WATCH_BLOCK_ID, xive->vc_regs[spec_reg]);
478     idx = GETFIELD(VC_ENDC_WATCH_INDEX, xive->vc_regs[spec_reg]);
479 
480     if (pnv_xive2_vst_read(xive, VST_END, blk, idx, endc_watch)) {
481         xive2_error(xive, "VST: no END entry %x/%x !?", blk, idx);
482     }
483 
484     for (i = 0; i < ARRAY_SIZE(endc_watch); i++) {
485         xive->vc_regs[data_reg + i] = be64_to_cpu(endc_watch[i]);
486     }
487 }
488 
489 static int pnv_xive2_get_nvp(Xive2Router *xrtr, uint8_t blk, uint32_t idx,
490                              Xive2Nvp *nvp)
491 {
492     return pnv_xive2_vst_read(PNV_XIVE2(xrtr), VST_NVP, blk, idx, nvp);
493 }
494 
495 static int pnv_xive2_write_nvp(Xive2Router *xrtr, uint8_t blk, uint32_t idx,
496                                Xive2Nvp *nvp, uint8_t word_number)
497 {
498     return pnv_xive2_vst_write(PNV_XIVE2(xrtr), VST_NVP, blk, idx, nvp,
499                               word_number);
500 }
501 
502 static int pnv_xive2_get_nvgc(Xive2Router *xrtr, bool crowd,
503                               uint8_t blk, uint32_t idx,
504                               Xive2Nvgc *nvgc)
505 {
506     return pnv_xive2_vst_read(PNV_XIVE2(xrtr), crowd ? VST_NVC : VST_NVG,
507                               blk, idx, nvgc);
508 }
509 
510 static int pnv_xive2_write_nvgc(Xive2Router *xrtr, bool crowd,
511                                 uint8_t blk, uint32_t idx,
512                                 Xive2Nvgc *nvgc)
513 {
514     return pnv_xive2_vst_write(PNV_XIVE2(xrtr), crowd ? VST_NVC : VST_NVG,
515                                blk, idx, nvgc,
516                                XIVE_VST_WORD_ALL);
517 }
518 
519 static int pnv_xive2_nxc_to_table_type(uint8_t nxc_type, uint32_t *table_type)
520 {
521     switch (nxc_type) {
522     case PC_NXC_WATCH_NXC_NVP:
523         *table_type = VST_NVP;
524         break;
525     case PC_NXC_WATCH_NXC_NVG:
526         *table_type = VST_NVG;
527         break;
528     case PC_NXC_WATCH_NXC_NVC:
529         *table_type = VST_NVC;
530         break;
531     default:
532         qemu_log_mask(LOG_GUEST_ERROR,
533                       "XIVE: invalid table type for nxc operation\n");
534         return -1;
535     }
536     return 0;
537 }
538 
539 static int pnv_xive2_nxc_update(PnvXive2 *xive, uint8_t watch_engine)
540 {
541     uint8_t  blk, nxc_type;
542     uint32_t idx, table_type = -1;
543     int i, spec_reg, data_reg;
544     uint64_t nxc_watch[4];
545 
546     assert(watch_engine < ARRAY_SIZE(nxc_watch));
547 
548     spec_reg = (PC_NXC_WATCH0_SPEC + watch_engine * 0x40) >> 3;
549     data_reg = (PC_NXC_WATCH0_DATA0 + watch_engine * 0x40) >> 3;
550     nxc_type = GETFIELD(PC_NXC_WATCH_NXC_TYPE, xive->pc_regs[spec_reg]);
551     blk = GETFIELD(PC_NXC_WATCH_BLOCK_ID, xive->pc_regs[spec_reg]);
552     idx = GETFIELD(PC_NXC_WATCH_INDEX, xive->pc_regs[spec_reg]);
553 
554     assert(!pnv_xive2_nxc_to_table_type(nxc_type, &table_type));
555 
556     for (i = 0; i < ARRAY_SIZE(nxc_watch); i++) {
557         nxc_watch[i] = cpu_to_be64(xive->pc_regs[data_reg + i]);
558     }
559 
560     return pnv_xive2_vst_write(xive, table_type, blk, idx, nxc_watch,
561                               XIVE_VST_WORD_ALL);
562 }
563 
564 static void pnv_xive2_nxc_cache_load(PnvXive2 *xive, uint8_t watch_engine)
565 {
566     uint8_t  blk, nxc_type;
567     uint32_t idx, table_type = -1;
568     uint64_t nxc_watch[4] = { 0 };
569     int i, spec_reg, data_reg;
570 
571     assert(watch_engine < ARRAY_SIZE(nxc_watch));
572 
573     spec_reg = (PC_NXC_WATCH0_SPEC + watch_engine * 0x40) >> 3;
574     data_reg = (PC_NXC_WATCH0_DATA0 + watch_engine * 0x40) >> 3;
575     nxc_type = GETFIELD(PC_NXC_WATCH_NXC_TYPE, xive->pc_regs[spec_reg]);
576     blk = GETFIELD(PC_NXC_WATCH_BLOCK_ID, xive->pc_regs[spec_reg]);
577     idx = GETFIELD(PC_NXC_WATCH_INDEX, xive->pc_regs[spec_reg]);
578 
579     assert(!pnv_xive2_nxc_to_table_type(nxc_type, &table_type));
580 
581     if (pnv_xive2_vst_read(xive, table_type, blk, idx, nxc_watch)) {
582         xive2_error(xive, "VST: no NXC entry %x/%x in %s table!?",
583                     blk, idx, vst_infos[table_type].name);
584     }
585 
586     for (i = 0; i < ARRAY_SIZE(nxc_watch); i++) {
587         xive->pc_regs[data_reg + i] = be64_to_cpu(nxc_watch[i]);
588     }
589 }
590 
591 static int pnv_xive2_get_eas(Xive2Router *xrtr, uint8_t blk, uint32_t idx,
592                             Xive2Eas *eas)
593 {
594     PnvXive2 *xive = PNV_XIVE2(xrtr);
595 
596     if (pnv_xive2_block_id(xive) != blk) {
597         xive2_error(xive, "VST: EAS %x is remote !?", XIVE_EAS(blk, idx));
598         return -1;
599     }
600 
601     return pnv_xive2_vst_read(xive, VST_EAS, blk, idx, eas);
602 }
603 
604 static uint32_t pnv_xive2_get_config(Xive2Router *xrtr)
605 {
606     PnvXive2 *xive = PNV_XIVE2(xrtr);
607     uint32_t cfg = 0;
608 
609     if (xive->cq_regs[CQ_XIVE_CFG >> 3] & CQ_XIVE_CFG_GEN1_TIMA_OS) {
610         cfg |= XIVE2_GEN1_TIMA_OS;
611     }
612 
613     if (xive->cq_regs[CQ_XIVE_CFG >> 3] & CQ_XIVE_CFG_EN_VP_SAVE_RESTORE) {
614         cfg |= XIVE2_VP_SAVE_RESTORE;
615     }
616 
617     if (GETFIELD(CQ_XIVE_CFG_HYP_HARD_RANGE,
618               xive->cq_regs[CQ_XIVE_CFG >> 3]) == CQ_XIVE_CFG_THREADID_8BITS) {
619         cfg |= XIVE2_THREADID_8BITS;
620     }
621 
622     return cfg;
623 }
624 
625 static bool pnv_xive2_is_cpu_enabled(PnvXive2 *xive, PowerPCCPU *cpu)
626 {
627     int pir = ppc_cpu_pir(cpu);
628     uint32_t fc = PNV10_PIR2FUSEDCORE(pir);
629     uint64_t reg = fc < 8 ? TCTXT_EN0 : TCTXT_EN1;
630     uint32_t bit = pir & 0x3f;
631 
632     return xive->tctxt_regs[reg >> 3] & PPC_BIT(bit);
633 }
634 
635 static int pnv_xive2_match_nvt(XivePresenter *xptr, uint8_t format,
636                                uint8_t nvt_blk, uint32_t nvt_idx,
637                                bool crowd, bool cam_ignore, uint8_t priority,
638                                uint32_t logic_serv, XiveTCTXMatch *match)
639 {
640     PnvXive2 *xive = PNV_XIVE2(xptr);
641     PnvChip *chip = xive->chip;
642     int count = 0;
643     int i, j;
644     bool gen1_tima_os =
645         xive->cq_regs[CQ_XIVE_CFG >> 3] & CQ_XIVE_CFG_GEN1_TIMA_OS;
646     static int next_start_core;
647     static int next_start_thread;
648     int start_core = next_start_core;
649     int start_thread = next_start_thread;
650 
651     for (i = 0; i < chip->nr_cores; i++) {
652         PnvCore *pc = chip->cores[(i + start_core) % chip->nr_cores];
653         CPUCore *cc = CPU_CORE(pc);
654 
655         for (j = 0; j < cc->nr_threads; j++) {
656             /* Start search for match with different thread each call */
657             PowerPCCPU *cpu = pc->threads[(j + start_thread) % cc->nr_threads];
658             XiveTCTX *tctx;
659             int ring;
660 
661             if (!pnv_xive2_is_cpu_enabled(xive, cpu)) {
662                 continue;
663             }
664 
665             tctx = XIVE_TCTX(pnv_cpu_state(cpu)->intc);
666 
667             if (gen1_tima_os) {
668                 ring = xive_presenter_tctx_match(xptr, tctx, format, nvt_blk,
669                                                  nvt_idx, cam_ignore,
670                                                  logic_serv);
671             } else {
672                 ring = xive2_presenter_tctx_match(xptr, tctx, format, nvt_blk,
673                                                   nvt_idx, crowd, cam_ignore,
674                                                   logic_serv);
675             }
676 
677             if (ring != -1) {
678                 /*
679                  * For VP-specific match, finding more than one is a
680                  * problem. For group notification, it's possible.
681                  */
682                 if (!cam_ignore && match->tctx) {
683                     qemu_log_mask(LOG_GUEST_ERROR, "XIVE: already found a "
684                                   "thread context NVT %x/%x\n",
685                                   nvt_blk, nvt_idx);
686                     /* Should set a FIR if we ever model it */
687                     return -1;
688                 }
689                 /*
690                  * For a group notification, we need to know if the
691                  * match is precluded first by checking the current
692                  * thread priority. If the interrupt can be delivered,
693                  * we always notify the first match (for now).
694                  */
695                 if (cam_ignore &&
696                     xive2_tm_irq_precluded(tctx, ring, priority)) {
697                         match->precluded = true;
698                 } else {
699                     if (!match->tctx) {
700                         match->ring = ring;
701                         match->tctx = tctx;
702 
703                         next_start_thread = j + start_thread + 1;
704                         if (next_start_thread >= cc->nr_threads) {
705                             next_start_thread = 0;
706                             next_start_core = i + start_core + 1;
707                             if (next_start_core >= chip->nr_cores) {
708                                 next_start_core = 0;
709                             }
710                         }
711                     }
712                     count++;
713                 }
714             }
715         }
716     }
717 
718     return count;
719 }
720 
721 static uint32_t pnv_xive2_presenter_get_config(XivePresenter *xptr)
722 {
723     PnvXive2 *xive = PNV_XIVE2(xptr);
724     uint32_t cfg = 0;
725 
726     if (xive->cq_regs[CQ_XIVE_CFG >> 3] & CQ_XIVE_CFG_GEN1_TIMA_OS) {
727         cfg |= XIVE_PRESENTER_GEN1_TIMA_OS;
728     }
729     return cfg;
730 }
731 
732 static int pnv_xive2_broadcast(XivePresenter *xptr,
733                                uint8_t nvt_blk, uint32_t nvt_idx,
734                                bool crowd, bool ignore, uint8_t priority)
735 {
736     PnvXive2 *xive = PNV_XIVE2(xptr);
737     PnvChip *chip = xive->chip;
738     int i, j;
739     bool gen1_tima_os =
740         xive->cq_regs[CQ_XIVE_CFG >> 3] & CQ_XIVE_CFG_GEN1_TIMA_OS;
741 
742     for (i = 0; i < chip->nr_cores; i++) {
743         PnvCore *pc = chip->cores[i];
744         CPUCore *cc = CPU_CORE(pc);
745 
746         for (j = 0; j < cc->nr_threads; j++) {
747             PowerPCCPU *cpu = pc->threads[j];
748             XiveTCTX *tctx;
749             int ring;
750 
751             if (!pnv_xive2_is_cpu_enabled(xive, cpu)) {
752                 continue;
753             }
754 
755             tctx = XIVE_TCTX(pnv_cpu_state(cpu)->intc);
756 
757             if (gen1_tima_os) {
758                 ring = xive_presenter_tctx_match(xptr, tctx, 0, nvt_blk,
759                                                  nvt_idx, ignore, 0);
760             } else {
761                 ring = xive2_presenter_tctx_match(xptr, tctx, 0, nvt_blk,
762                                                   nvt_idx, crowd, ignore, 0);
763             }
764 
765             if (ring != -1) {
766                 xive2_tm_set_lsmfb(tctx, ring, priority);
767             }
768         }
769     }
770     return 0;
771 }
772 
773 static uint8_t pnv_xive2_get_block_id(Xive2Router *xrtr)
774 {
775     return pnv_xive2_block_id(PNV_XIVE2(xrtr));
776 }
777 
778 /*
779  * The TIMA MMIO space is shared among the chips and to identify the
780  * chip from which the access is being done, we extract the chip id
781  * from the PIR.
782  */
783 static PnvXive2 *pnv_xive2_tm_get_xive(PowerPCCPU *cpu)
784 {
785     int pir = ppc_cpu_pir(cpu);
786     XivePresenter *xptr = XIVE_TCTX(pnv_cpu_state(cpu)->intc)->xptr;
787     PnvXive2 *xive = PNV_XIVE2(xptr);
788 
789     if (!pnv_xive2_is_cpu_enabled(xive, cpu)) {
790         xive2_error(xive, "IC: CPU %x is not enabled", pir);
791     }
792     return xive;
793 }
794 
795 /*
796  * The internal sources of the interrupt controller have no knowledge
797  * of the XIVE2 chip on which they reside. Encode the block id in the
798  * source interrupt number before forwarding the source event
799  * notification to the Router. This is required on a multichip system.
800  */
801 static void pnv_xive2_notify(XiveNotifier *xn, uint32_t srcno, bool pq_checked)
802 {
803     PnvXive2 *xive = PNV_XIVE2(xn);
804     uint8_t blk = pnv_xive2_block_id(xive);
805 
806     xive2_router_notify(xn, XIVE_EAS(blk, srcno), pq_checked);
807 }
808 
809 /*
810  * Set Translation Tables
811  *
812  * TODO add support for multiple sets
813  */
814 static int pnv_xive2_stt_set_data(PnvXive2 *xive, uint64_t val)
815 {
816     uint8_t tsel = GETFIELD(CQ_TAR_SELECT, xive->cq_regs[CQ_TAR >> 3]);
817     uint8_t entry = GETFIELD(CQ_TAR_ENTRY_SELECT,
818                                   xive->cq_regs[CQ_TAR >> 3]);
819 
820     switch (tsel) {
821     case CQ_TAR_NVPG:
822     case CQ_TAR_ESB:
823     case CQ_TAR_END:
824     case CQ_TAR_NVC:
825         xive->tables[tsel][entry] = val;
826         break;
827     default:
828         xive2_error(xive, "IC: unsupported table %d", tsel);
829         return -1;
830     }
831 
832     if (xive->cq_regs[CQ_TAR >> 3] & CQ_TAR_AUTOINC) {
833         xive->cq_regs[CQ_TAR >> 3] = SETFIELD(CQ_TAR_ENTRY_SELECT,
834                      xive->cq_regs[CQ_TAR >> 3], ++entry);
835     }
836 
837     return 0;
838 }
839 /*
840  * Virtual Structure Tables (VST) configuration
841  */
842 static void pnv_xive2_vst_set_exclusive(PnvXive2 *xive, uint8_t type,
843                                         uint8_t blk, uint64_t vsd)
844 {
845     Xive2EndSource *end_xsrc = &xive->end_source;
846     XiveSource *xsrc = &xive->ipi_source;
847     const XiveVstInfo *info = &vst_infos[type];
848     uint32_t page_shift = GETFIELD(VSD_TSIZE, vsd) + 12;
849     uint64_t vst_tsize = 1ull << page_shift;
850     uint64_t vst_addr = vsd & VSD_ADDRESS_MASK;
851 
852     /* Basic checks */
853 
854     if (VSD_INDIRECT & vsd) {
855         if (!pnv_xive2_vst_page_size_allowed(page_shift)) {
856             xive2_error(xive, "VST: invalid %s page shift %d", info->name,
857                        page_shift);
858             return;
859         }
860     }
861 
862     if (!QEMU_IS_ALIGNED(vst_addr, 1ull << page_shift)) {
863         xive2_error(xive, "VST: %s table address 0x%"PRIx64
864                     " is not aligned with page shift %d",
865                     info->name, vst_addr, page_shift);
866         return;
867     }
868 
869     /* Record the table configuration (in SRAM on HW) */
870     xive->vsds[type][blk] = vsd;
871 
872     /* Now tune the models with the configuration provided by the FW */
873 
874     switch (type) {
875     case VST_ESB:
876         /*
877          * Backing store pages for the source PQ bits. The model does
878          * not use these PQ bits backed in RAM because the XiveSource
879          * model has its own.
880          *
881          * If the table is direct, we can compute the number of PQ
882          * entries provisioned by FW (such as skiboot) and resize the
883          * ESB window accordingly.
884          */
885         if (memory_region_is_mapped(&xsrc->esb_mmio)) {
886             memory_region_del_subregion(&xive->esb_mmio, &xsrc->esb_mmio);
887         }
888         if (!(VSD_INDIRECT & vsd)) {
889             memory_region_set_size(&xsrc->esb_mmio, vst_tsize * SBE_PER_BYTE
890                                    * (1ull << xsrc->esb_shift));
891         }
892 
893         memory_region_add_subregion(&xive->esb_mmio, 0, &xsrc->esb_mmio);
894         break;
895 
896     case VST_EAS:  /* Nothing to be done */
897         break;
898 
899     case VST_END:
900         /*
901          * Backing store pages for the END.
902          */
903         if (memory_region_is_mapped(&end_xsrc->esb_mmio)) {
904             memory_region_del_subregion(&xive->end_mmio, &end_xsrc->esb_mmio);
905         }
906         if (!(VSD_INDIRECT & vsd)) {
907             memory_region_set_size(&end_xsrc->esb_mmio, (vst_tsize / info->size)
908                                    * (1ull << end_xsrc->esb_shift));
909         }
910         memory_region_add_subregion(&xive->end_mmio, 0, &end_xsrc->esb_mmio);
911         break;
912 
913     case VST_NVP:  /* Not modeled */
914     case VST_NVG:  /* Not modeled */
915     case VST_NVC:  /* Not modeled */
916     case VST_IC:   /* Not modeled */
917     case VST_SYNC: /* Not modeled */
918     case VST_ERQ:  /* Not modeled */
919         break;
920 
921     default:
922         g_assert_not_reached();
923     }
924 }
925 
926 /*
927  * Both PC and VC sub-engines are configured as each use the Virtual
928  * Structure Tables
929  */
930 static void pnv_xive2_vst_set_data(PnvXive2 *xive, uint64_t vsd,
931                                    uint8_t type, uint8_t blk)
932 {
933     uint8_t mode = GETFIELD(VSD_MODE, vsd);
934     uint64_t vst_addr = vsd & VSD_ADDRESS_MASK;
935 
936     if (type > VST_ERQ) {
937         xive2_error(xive, "VST: invalid table type %d", type);
938         return;
939     }
940 
941     if (blk >= vst_infos[type].max_blocks) {
942         xive2_error(xive, "VST: invalid block id %d for"
943                       " %s table", blk, vst_infos[type].name);
944         return;
945     }
946 
947     if (!vst_addr) {
948         xive2_error(xive, "VST: invalid %s table address",
949                    vst_infos[type].name);
950         return;
951     }
952 
953     switch (mode) {
954     case VSD_MODE_FORWARD:
955         xive->vsds[type][blk] = vsd;
956         break;
957 
958     case VSD_MODE_EXCLUSIVE:
959         pnv_xive2_vst_set_exclusive(xive, type, blk, vsd);
960         break;
961 
962     default:
963         xive2_error(xive, "VST: unsupported table mode %d", mode);
964         return;
965     }
966 }
967 
968 static void pnv_xive2_vc_vst_set_data(PnvXive2 *xive, uint64_t vsd)
969 {
970     uint8_t type = GETFIELD(VC_VSD_TABLE_SELECT,
971                             xive->vc_regs[VC_VSD_TABLE_ADDR >> 3]);
972     uint8_t blk = GETFIELD(VC_VSD_TABLE_ADDRESS,
973                            xive->vc_regs[VC_VSD_TABLE_ADDR >> 3]);
974 
975     pnv_xive2_vst_set_data(xive, vsd, type, blk);
976 }
977 
978 /*
979  * MMIO handlers
980  */
981 
982 
983 /*
984  * IC BAR layout
985  *
986  * Page 0: Internal CQ register accesses (reads & writes)
987  * Page 1: Internal PC register accesses (reads & writes)
988  * Page 2: Internal VC register accesses (reads & writes)
989  * Page 3: Internal TCTXT (TIMA) reg accesses (read & writes)
990  * Page 4: Notify Port page (writes only, w/data),
991  * Page 5: Reserved
992  * Page 6: Sync Poll page (writes only, dataless)
993  * Page 7: Sync Inject page (writes only, dataless)
994  * Page 8: LSI Trigger page (writes only, dataless)
995  * Page 9: LSI SB Management page (reads & writes dataless)
996  * Pages 10-255: Reserved
997  * Pages 256-383: Direct mapped Thread Context Area (reads & writes)
998  *                covering the 128 threads in P10.
999  * Pages 384-511: Reserved
1000  */
1001 typedef struct PnvXive2Region {
1002     const char *name;
1003     uint32_t pgoff;
1004     uint32_t pgsize;
1005     const MemoryRegionOps *ops;
1006 } PnvXive2Region;
1007 
1008 static const MemoryRegionOps pnv_xive2_ic_cq_ops;
1009 static const MemoryRegionOps pnv_xive2_ic_pc_ops;
1010 static const MemoryRegionOps pnv_xive2_ic_vc_ops;
1011 static const MemoryRegionOps pnv_xive2_ic_tctxt_ops;
1012 static const MemoryRegionOps pnv_xive2_ic_notify_ops;
1013 static const MemoryRegionOps pnv_xive2_ic_sync_ops;
1014 static const MemoryRegionOps pnv_xive2_ic_lsi_ops;
1015 static const MemoryRegionOps pnv_xive2_ic_tm_indirect_ops;
1016 
1017 /* 512 pages. 4K: 2M range, 64K: 32M range */
1018 static const PnvXive2Region pnv_xive2_ic_regions[] = {
1019     { "xive-ic-cq",        0,   1,   &pnv_xive2_ic_cq_ops     },
1020     { "xive-ic-vc",        1,   1,   &pnv_xive2_ic_vc_ops     },
1021     { "xive-ic-pc",        2,   1,   &pnv_xive2_ic_pc_ops     },
1022     { "xive-ic-tctxt",     3,   1,   &pnv_xive2_ic_tctxt_ops  },
1023     { "xive-ic-notify",    4,   1,   &pnv_xive2_ic_notify_ops },
1024     /* page 5 reserved */
1025     { "xive-ic-sync",      6,   2,   &pnv_xive2_ic_sync_ops   },
1026     { "xive-ic-lsi",       8,   2,   &pnv_xive2_ic_lsi_ops    },
1027     /* pages 10-255 reserved */
1028     { "xive-ic-tm-indirect", 256, 128, &pnv_xive2_ic_tm_indirect_ops  },
1029     /* pages 384-511 reserved */
1030 };
1031 
1032 /*
1033  * CQ operations
1034  */
1035 
1036 static uint64_t pnv_xive2_ic_cq_read(void *opaque, hwaddr offset,
1037                                         unsigned size)
1038 {
1039     PnvXive2 *xive = PNV_XIVE2(opaque);
1040     uint32_t reg = offset >> 3;
1041     uint64_t val = 0;
1042 
1043     switch (offset) {
1044     case CQ_XIVE_CAP: /* Set at reset */
1045     case CQ_XIVE_CFG:
1046         val = xive->cq_regs[reg];
1047         break;
1048     case CQ_MSGSND: /* TODO check the #cores of the machine */
1049         val = 0xffffffff00000000;
1050         break;
1051     case CQ_CFG_PB_GEN:
1052         val = CQ_CFG_PB_GEN_PB_INIT; /* TODO: fix CQ_CFG_PB_GEN default value */
1053         break;
1054     default:
1055         xive2_error(xive, "CQ: invalid read @%"HWADDR_PRIx, offset);
1056     }
1057 
1058     return val;
1059 }
1060 
1061 static uint64_t pnv_xive2_bar_size(uint64_t val)
1062 {
1063     return 1ull << (GETFIELD(CQ_BAR_RANGE, val) + 24);
1064 }
1065 
1066 static void pnv_xive2_ic_cq_write(void *opaque, hwaddr offset,
1067                                   uint64_t val, unsigned size)
1068 {
1069     PnvXive2 *xive = PNV_XIVE2(opaque);
1070     MemoryRegion *sysmem = get_system_memory();
1071     uint32_t reg = offset >> 3;
1072     int i;
1073 
1074     switch (offset) {
1075     case CQ_XIVE_CFG:
1076     case CQ_RST_CTL: /* TODO: reset all BARs */
1077         break;
1078 
1079     case CQ_IC_BAR:
1080         xive->ic_shift = val & CQ_IC_BAR_64K ? 16 : 12;
1081         if (!(val & CQ_IC_BAR_VALID)) {
1082             xive->ic_base = 0;
1083             if (xive->cq_regs[reg] & CQ_IC_BAR_VALID) {
1084                 for (i = 0; i < ARRAY_SIZE(xive->ic_mmios); i++) {
1085                     memory_region_del_subregion(&xive->ic_mmio,
1086                                                 &xive->ic_mmios[i]);
1087                 }
1088                 memory_region_del_subregion(sysmem, &xive->ic_mmio);
1089             }
1090         } else {
1091             xive->ic_base = val & ~(CQ_IC_BAR_VALID | CQ_IC_BAR_64K);
1092             if (!(xive->cq_regs[reg] & CQ_IC_BAR_VALID)) {
1093                 for (i = 0; i < ARRAY_SIZE(xive->ic_mmios); i++) {
1094                     memory_region_add_subregion(&xive->ic_mmio,
1095                                pnv_xive2_ic_regions[i].pgoff << xive->ic_shift,
1096                                &xive->ic_mmios[i]);
1097                 }
1098                 memory_region_add_subregion(sysmem, xive->ic_base,
1099                                             &xive->ic_mmio);
1100             }
1101         }
1102         break;
1103 
1104     case CQ_TM_BAR:
1105         xive->tm_shift = val & CQ_TM_BAR_64K ? 16 : 12;
1106         if (!(val & CQ_TM_BAR_VALID)) {
1107             xive->tm_base = 0;
1108             if (xive->cq_regs[reg] & CQ_TM_BAR_VALID) {
1109                 memory_region_del_subregion(sysmem, &xive->tm_mmio);
1110             }
1111         } else {
1112             xive->tm_base = val & ~(CQ_TM_BAR_VALID | CQ_TM_BAR_64K);
1113             if (!(xive->cq_regs[reg] & CQ_TM_BAR_VALID)) {
1114                 memory_region_add_subregion(sysmem, xive->tm_base,
1115                                             &xive->tm_mmio);
1116             }
1117         }
1118         break;
1119 
1120     case CQ_ESB_BAR:
1121         xive->esb_shift = val & CQ_BAR_64K ? 16 : 12;
1122         if (!(val & CQ_BAR_VALID)) {
1123             xive->esb_base = 0;
1124             if (xive->cq_regs[reg] & CQ_BAR_VALID) {
1125                 memory_region_del_subregion(sysmem, &xive->esb_mmio);
1126             }
1127         } else {
1128             xive->esb_base = val & CQ_BAR_ADDR;
1129             if (!(xive->cq_regs[reg] & CQ_BAR_VALID)) {
1130                 memory_region_set_size(&xive->esb_mmio,
1131                                        pnv_xive2_bar_size(val));
1132                 memory_region_add_subregion(sysmem, xive->esb_base,
1133                                             &xive->esb_mmio);
1134             }
1135         }
1136         break;
1137 
1138     case CQ_END_BAR:
1139         xive->end_shift = val & CQ_BAR_64K ? 16 : 12;
1140         if (!(val & CQ_BAR_VALID)) {
1141             xive->end_base = 0;
1142             if (xive->cq_regs[reg] & CQ_BAR_VALID) {
1143                 memory_region_del_subregion(sysmem, &xive->end_mmio);
1144             }
1145         } else {
1146             xive->end_base = val & CQ_BAR_ADDR;
1147             if (!(xive->cq_regs[reg] & CQ_BAR_VALID)) {
1148                 memory_region_set_size(&xive->end_mmio,
1149                                        pnv_xive2_bar_size(val));
1150                 memory_region_add_subregion(sysmem, xive->end_base,
1151                                             &xive->end_mmio);
1152             }
1153         }
1154         break;
1155 
1156     case CQ_NVC_BAR:
1157         xive->nvc_shift = val & CQ_BAR_64K ? 16 : 12;
1158         if (!(val & CQ_BAR_VALID)) {
1159             xive->nvc_base = 0;
1160             if (xive->cq_regs[reg] & CQ_BAR_VALID) {
1161                 memory_region_del_subregion(sysmem, &xive->nvc_mmio);
1162             }
1163         } else {
1164             xive->nvc_base = val & CQ_BAR_ADDR;
1165             if (!(xive->cq_regs[reg] & CQ_BAR_VALID)) {
1166                 memory_region_set_size(&xive->nvc_mmio,
1167                                        pnv_xive2_bar_size(val));
1168                 memory_region_add_subregion(sysmem, xive->nvc_base,
1169                                             &xive->nvc_mmio);
1170             }
1171         }
1172         break;
1173 
1174     case CQ_NVPG_BAR:
1175         xive->nvpg_shift = val & CQ_BAR_64K ? 16 : 12;
1176         if (!(val & CQ_BAR_VALID)) {
1177             xive->nvpg_base = 0;
1178             if (xive->cq_regs[reg] & CQ_BAR_VALID) {
1179                 memory_region_del_subregion(sysmem, &xive->nvpg_mmio);
1180             }
1181         } else {
1182             xive->nvpg_base = val & CQ_BAR_ADDR;
1183             if (!(xive->cq_regs[reg] & CQ_BAR_VALID)) {
1184                 memory_region_set_size(&xive->nvpg_mmio,
1185                                        pnv_xive2_bar_size(val));
1186                 memory_region_add_subregion(sysmem, xive->nvpg_base,
1187                                             &xive->nvpg_mmio);
1188             }
1189         }
1190         break;
1191 
1192     case CQ_TAR: /* Set Translation Table Address */
1193         break;
1194     case CQ_TDR: /* Set Translation Table Data */
1195         pnv_xive2_stt_set_data(xive, val);
1196         break;
1197     case CQ_FIRMASK_OR: /* FIR error reporting */
1198         break;
1199     default:
1200         xive2_error(xive, "CQ: invalid write 0x%"HWADDR_PRIx" value 0x%"PRIx64,
1201                     offset, val);
1202         return;
1203     }
1204 
1205     xive->cq_regs[reg] = val;
1206 }
1207 
1208 static const MemoryRegionOps pnv_xive2_ic_cq_ops = {
1209     .read = pnv_xive2_ic_cq_read,
1210     .write = pnv_xive2_ic_cq_write,
1211     .endianness = DEVICE_BIG_ENDIAN,
1212     .valid = {
1213         .min_access_size = 8,
1214         .max_access_size = 8,
1215     },
1216     .impl = {
1217         .min_access_size = 8,
1218         .max_access_size = 8,
1219     },
1220 };
1221 
1222 static uint8_t pnv_xive2_cache_watch_assign(uint64_t engine_mask,
1223                                             uint64_t *state)
1224 {
1225     uint8_t val = 0xFF;
1226     int i;
1227 
1228     for (i = 3; i >= 0; i--) {
1229         if (BIT(i) & engine_mask) {
1230             if (!(BIT(i) & *state)) {
1231                 *state |= BIT(i);
1232                 val = 3 - i;
1233                 break;
1234             }
1235         }
1236     }
1237     return val;
1238 }
1239 
1240 static void pnv_xive2_cache_watch_release(uint64_t *state, uint8_t watch_engine)
1241 {
1242     uint8_t engine_bit = 3 - watch_engine;
1243 
1244     if (*state & BIT(engine_bit)) {
1245         *state &= ~BIT(engine_bit);
1246     }
1247 }
1248 
1249 static uint8_t pnv_xive2_endc_cache_watch_assign(PnvXive2 *xive)
1250 {
1251     uint64_t engine_mask = GETFIELD(VC_ENDC_CFG_CACHE_WATCH_ASSIGN,
1252                                     xive->vc_regs[VC_ENDC_CFG >> 3]);
1253     uint64_t state = xive->vc_regs[VC_ENDC_WATCH_ASSIGN >> 3];
1254     uint8_t val;
1255 
1256     /*
1257      * We keep track of which engines are currently busy in the
1258      * VC_ENDC_WATCH_ASSIGN register directly. When the firmware reads
1259      * the register, we don't return its value but the ID of an engine
1260      * it can use.
1261      * There are 4 engines. 0xFF means no engine is available.
1262      */
1263     val = pnv_xive2_cache_watch_assign(engine_mask, &state);
1264     if (val != 0xFF) {
1265         xive->vc_regs[VC_ENDC_WATCH_ASSIGN >> 3] = state;
1266     }
1267     return val;
1268 }
1269 
1270 static void pnv_xive2_endc_cache_watch_release(PnvXive2 *xive,
1271                                                uint8_t watch_engine)
1272 {
1273     uint64_t state = xive->vc_regs[VC_ENDC_WATCH_ASSIGN >> 3];
1274 
1275     pnv_xive2_cache_watch_release(&state, watch_engine);
1276     xive->vc_regs[VC_ENDC_WATCH_ASSIGN >> 3] = state;
1277 }
1278 
1279 static uint64_t pnv_xive2_ic_vc_read(void *opaque, hwaddr offset,
1280                                      unsigned size)
1281 {
1282     PnvXive2 *xive = PNV_XIVE2(opaque);
1283     uint64_t val = 0;
1284     uint32_t reg = offset >> 3;
1285     uint8_t watch_engine;
1286 
1287     switch (offset) {
1288     /*
1289      * VSD table settings.
1290      */
1291     case VC_VSD_TABLE_ADDR:
1292     case VC_VSD_TABLE_DATA:
1293         val = xive->vc_regs[reg];
1294         break;
1295 
1296     /*
1297      * ESB cache updates (not modeled)
1298      */
1299     case VC_ESBC_FLUSH_CTRL:
1300         xive->vc_regs[reg] &= ~VC_ESBC_FLUSH_CTRL_POLL_VALID;
1301         val = xive->vc_regs[reg];
1302         break;
1303 
1304     case VC_ESBC_CFG:
1305         val = xive->vc_regs[reg];
1306         break;
1307 
1308     /*
1309      * EAS cache updates (not modeled)
1310      */
1311     case VC_EASC_FLUSH_CTRL:
1312         xive->vc_regs[reg] &= ~VC_EASC_FLUSH_CTRL_POLL_VALID;
1313         val = xive->vc_regs[reg];
1314         break;
1315 
1316     case VC_ENDC_WATCH_ASSIGN:
1317         val = pnv_xive2_endc_cache_watch_assign(xive);
1318         break;
1319 
1320     case VC_ENDC_CFG:
1321         val = xive->vc_regs[reg];
1322         break;
1323 
1324     /*
1325      * END cache updates
1326      */
1327     case VC_ENDC_WATCH0_SPEC:
1328     case VC_ENDC_WATCH1_SPEC:
1329     case VC_ENDC_WATCH2_SPEC:
1330     case VC_ENDC_WATCH3_SPEC:
1331         watch_engine = (offset - VC_ENDC_WATCH0_SPEC) >> 6;
1332         pnv_xive2_endc_cache_watch_release(xive, watch_engine);
1333         val = xive->vc_regs[reg];
1334         break;
1335 
1336     case VC_ENDC_WATCH0_DATA0:
1337     case VC_ENDC_WATCH1_DATA0:
1338     case VC_ENDC_WATCH2_DATA0:
1339     case VC_ENDC_WATCH3_DATA0:
1340         /*
1341          * Load DATA registers from cache with data requested by the
1342          * SPEC register.  Clear gen_flipped bit in word 1.
1343          */
1344         watch_engine = (offset - VC_ENDC_WATCH0_DATA0) >> 6;
1345         pnv_xive2_end_cache_load(xive, watch_engine);
1346         xive->vc_regs[reg] &= ~(uint64_t)END2_W1_GEN_FLIPPED;
1347         val = xive->vc_regs[reg];
1348         break;
1349 
1350     case VC_ENDC_WATCH0_DATA1 ... VC_ENDC_WATCH0_DATA3:
1351     case VC_ENDC_WATCH1_DATA1 ... VC_ENDC_WATCH1_DATA3:
1352     case VC_ENDC_WATCH2_DATA1 ... VC_ENDC_WATCH2_DATA3:
1353     case VC_ENDC_WATCH3_DATA1 ... VC_ENDC_WATCH3_DATA3:
1354         val = xive->vc_regs[reg];
1355         break;
1356 
1357     case VC_ENDC_FLUSH_CTRL:
1358         xive->vc_regs[reg] &= ~VC_ENDC_FLUSH_CTRL_POLL_VALID;
1359         val = xive->vc_regs[reg];
1360         break;
1361 
1362     /*
1363      * Indirect invalidation
1364      */
1365     case VC_AT_MACRO_KILL_MASK:
1366         val = xive->vc_regs[reg];
1367         break;
1368 
1369     case VC_AT_MACRO_KILL:
1370         xive->vc_regs[reg] &= ~VC_AT_MACRO_KILL_VALID;
1371         val = xive->vc_regs[reg];
1372         break;
1373 
1374     /*
1375      * Interrupt fifo overflow in memory backing store (Not modeled)
1376      */
1377     case VC_QUEUES_CFG_REM0 ... VC_QUEUES_CFG_REM6:
1378         val = xive->vc_regs[reg];
1379         break;
1380 
1381     /*
1382      * Synchronisation
1383      */
1384     case VC_ENDC_SYNC_DONE:
1385         val = VC_ENDC_SYNC_POLL_DONE;
1386         break;
1387     default:
1388         xive2_error(xive, "VC: invalid read @%"HWADDR_PRIx, offset);
1389     }
1390 
1391     return val;
1392 }
1393 
1394 static void pnv_xive2_ic_vc_write(void *opaque, hwaddr offset,
1395                                   uint64_t val, unsigned size)
1396 {
1397     PnvXive2 *xive = PNV_XIVE2(opaque);
1398     uint32_t reg = offset >> 3;
1399     uint8_t watch_engine;
1400 
1401     switch (offset) {
1402     /*
1403      * VSD table settings.
1404      */
1405     case VC_VSD_TABLE_ADDR:
1406        break;
1407     case VC_VSD_TABLE_DATA:
1408         pnv_xive2_vc_vst_set_data(xive, val);
1409         break;
1410 
1411     /*
1412      * ESB cache updates (not modeled)
1413      */
1414     /* case VC_ESBC_FLUSH_CTRL: */
1415     case VC_ESBC_FLUSH_POLL:
1416         xive->vc_regs[VC_ESBC_FLUSH_CTRL >> 3] |= VC_ESBC_FLUSH_CTRL_POLL_VALID;
1417         /* ESB update */
1418         break;
1419 
1420     case VC_ESBC_FLUSH_INJECT:
1421         pnv_xive2_inject_notify(xive, PNV_XIVE2_CACHE_ESBC);
1422         break;
1423 
1424     case VC_ESBC_CFG:
1425         break;
1426 
1427     /*
1428      * EAS cache updates (not modeled)
1429      */
1430     /* case VC_EASC_FLUSH_CTRL: */
1431     case VC_EASC_FLUSH_POLL:
1432         xive->vc_regs[VC_EASC_FLUSH_CTRL >> 3] |= VC_EASC_FLUSH_CTRL_POLL_VALID;
1433         /* EAS update */
1434         break;
1435 
1436     case VC_EASC_FLUSH_INJECT:
1437         pnv_xive2_inject_notify(xive, PNV_XIVE2_CACHE_EASC);
1438         break;
1439 
1440     case VC_ENDC_CFG:
1441         break;
1442 
1443     /*
1444      * END cache updates
1445      */
1446     case VC_ENDC_WATCH0_SPEC:
1447     case VC_ENDC_WATCH1_SPEC:
1448     case VC_ENDC_WATCH2_SPEC:
1449     case VC_ENDC_WATCH3_SPEC:
1450          val &= ~VC_ENDC_WATCH_CONFLICT; /* HW will set this bit */
1451         break;
1452 
1453     case VC_ENDC_WATCH0_DATA1 ... VC_ENDC_WATCH0_DATA3:
1454     case VC_ENDC_WATCH1_DATA1 ... VC_ENDC_WATCH1_DATA3:
1455     case VC_ENDC_WATCH2_DATA1 ... VC_ENDC_WATCH2_DATA3:
1456     case VC_ENDC_WATCH3_DATA1 ... VC_ENDC_WATCH3_DATA3:
1457         break;
1458     case VC_ENDC_WATCH0_DATA0:
1459     case VC_ENDC_WATCH1_DATA0:
1460     case VC_ENDC_WATCH2_DATA0:
1461     case VC_ENDC_WATCH3_DATA0:
1462         /* writing to DATA0 triggers the cache write */
1463         watch_engine = (offset - VC_ENDC_WATCH0_DATA0) >> 6;
1464         xive->vc_regs[reg] = val;
1465         pnv_xive2_end_update(xive, watch_engine);
1466         break;
1467 
1468 
1469     /* case VC_ENDC_FLUSH_CTRL: */
1470     case VC_ENDC_FLUSH_POLL:
1471         xive->vc_regs[VC_ENDC_FLUSH_CTRL >> 3] |= VC_ENDC_FLUSH_CTRL_POLL_VALID;
1472         break;
1473 
1474     case VC_ENDC_FLUSH_INJECT:
1475         pnv_xive2_inject_notify(xive, PNV_XIVE2_CACHE_ENDC);
1476         break;
1477 
1478     /*
1479      * Indirect invalidation
1480      */
1481     case VC_AT_MACRO_KILL:
1482     case VC_AT_MACRO_KILL_MASK:
1483         break;
1484 
1485     /*
1486      * Interrupt fifo overflow in memory backing store (Not modeled)
1487      */
1488     case VC_QUEUES_CFG_REM0 ... VC_QUEUES_CFG_REM6:
1489         break;
1490 
1491     /*
1492      * Synchronisation
1493      */
1494     case VC_ENDC_SYNC_DONE:
1495         break;
1496 
1497     default:
1498         xive2_error(xive, "VC: invalid write @0x%"HWADDR_PRIx" value 0x%"PRIx64,
1499                     offset, val);
1500         return;
1501     }
1502 
1503     xive->vc_regs[reg] = val;
1504 }
1505 
1506 static const MemoryRegionOps pnv_xive2_ic_vc_ops = {
1507     .read = pnv_xive2_ic_vc_read,
1508     .write = pnv_xive2_ic_vc_write,
1509     .endianness = DEVICE_BIG_ENDIAN,
1510     .valid = {
1511         .min_access_size = 8,
1512         .max_access_size = 8,
1513     },
1514     .impl = {
1515         .min_access_size = 8,
1516         .max_access_size = 8,
1517     },
1518 };
1519 
1520 static uint8_t pnv_xive2_nxc_cache_watch_assign(PnvXive2 *xive)
1521 {
1522     uint64_t engine_mask = GETFIELD(PC_NXC_PROC_CONFIG_WATCH_ASSIGN,
1523                                     xive->pc_regs[PC_NXC_PROC_CONFIG >> 3]);
1524     uint64_t state = xive->pc_regs[PC_NXC_WATCH_ASSIGN >> 3];
1525     uint8_t val;
1526 
1527     /*
1528      * We keep track of which engines are currently busy in the
1529      * PC_NXC_WATCH_ASSIGN register directly. When the firmware reads
1530      * the register, we don't return its value but the ID of an engine
1531      * it can use.
1532      * There are 4 engines. 0xFF means no engine is available.
1533      */
1534     val = pnv_xive2_cache_watch_assign(engine_mask, &state);
1535     if (val != 0xFF) {
1536         xive->pc_regs[PC_NXC_WATCH_ASSIGN >> 3] = state;
1537     }
1538     return val;
1539 }
1540 
1541 static void pnv_xive2_nxc_cache_watch_release(PnvXive2 *xive,
1542                                               uint8_t watch_engine)
1543 {
1544     uint64_t state = xive->pc_regs[PC_NXC_WATCH_ASSIGN >> 3];
1545 
1546     pnv_xive2_cache_watch_release(&state, watch_engine);
1547     xive->pc_regs[PC_NXC_WATCH_ASSIGN >> 3] = state;
1548 }
1549 
1550 static uint64_t pnv_xive2_ic_pc_read(void *opaque, hwaddr offset,
1551                                      unsigned size)
1552 {
1553     PnvXive2 *xive = PNV_XIVE2(opaque);
1554     uint64_t val = -1;
1555     uint32_t reg = offset >> 3;
1556     uint8_t watch_engine;
1557 
1558     switch (offset) {
1559     /*
1560      * VSD table settings.
1561      */
1562     case PC_VSD_TABLE_ADDR:
1563     case PC_VSD_TABLE_DATA:
1564         val = xive->pc_regs[reg];
1565         break;
1566 
1567     case PC_NXC_WATCH_ASSIGN:
1568         val = pnv_xive2_nxc_cache_watch_assign(xive);
1569         break;
1570 
1571     case PC_NXC_PROC_CONFIG:
1572         val = xive->pc_regs[reg];
1573         break;
1574 
1575     /*
1576      * cache updates
1577      */
1578     case PC_NXC_WATCH0_SPEC:
1579     case PC_NXC_WATCH1_SPEC:
1580     case PC_NXC_WATCH2_SPEC:
1581     case PC_NXC_WATCH3_SPEC:
1582         watch_engine = (offset - PC_NXC_WATCH0_SPEC) >> 6;
1583         xive->pc_regs[reg] &= ~(PC_NXC_WATCH_FULL | PC_NXC_WATCH_CONFLICT);
1584         pnv_xive2_nxc_cache_watch_release(xive, watch_engine);
1585         val = xive->pc_regs[reg];
1586         break;
1587 
1588     case PC_NXC_WATCH0_DATA0:
1589     case PC_NXC_WATCH1_DATA0:
1590     case PC_NXC_WATCH2_DATA0:
1591     case PC_NXC_WATCH3_DATA0:
1592        /*
1593         * Load DATA registers from cache with data requested by the
1594         * SPEC register
1595         */
1596         watch_engine = (offset - PC_NXC_WATCH0_DATA0) >> 6;
1597         pnv_xive2_nxc_cache_load(xive, watch_engine);
1598         val = xive->pc_regs[reg];
1599         break;
1600 
1601     case PC_NXC_WATCH0_DATA1 ... PC_NXC_WATCH0_DATA3:
1602     case PC_NXC_WATCH1_DATA1 ... PC_NXC_WATCH1_DATA3:
1603     case PC_NXC_WATCH2_DATA1 ... PC_NXC_WATCH2_DATA3:
1604     case PC_NXC_WATCH3_DATA1 ... PC_NXC_WATCH3_DATA3:
1605         val = xive->pc_regs[reg];
1606         break;
1607 
1608     case PC_NXC_FLUSH_CTRL:
1609         xive->pc_regs[reg] &= ~PC_NXC_FLUSH_CTRL_POLL_VALID;
1610         val = xive->pc_regs[reg];
1611         break;
1612 
1613     /*
1614      * Indirect invalidation
1615      */
1616     case PC_AT_KILL:
1617         xive->pc_regs[reg] &= ~PC_AT_KILL_VALID;
1618         val = xive->pc_regs[reg];
1619         break;
1620 
1621     default:
1622         xive2_error(xive, "PC: invalid read @%"HWADDR_PRIx, offset);
1623     }
1624 
1625     return val;
1626 }
1627 
1628 static void pnv_xive2_pc_vst_set_data(PnvXive2 *xive, uint64_t vsd)
1629 {
1630     uint8_t type = GETFIELD(PC_VSD_TABLE_SELECT,
1631                             xive->pc_regs[PC_VSD_TABLE_ADDR >> 3]);
1632     uint8_t blk = GETFIELD(PC_VSD_TABLE_ADDRESS,
1633                            xive->pc_regs[PC_VSD_TABLE_ADDR >> 3]);
1634 
1635     pnv_xive2_vst_set_data(xive, vsd, type, blk);
1636 }
1637 
1638 static void pnv_xive2_ic_pc_write(void *opaque, hwaddr offset,
1639                                   uint64_t val, unsigned size)
1640 {
1641     PnvXive2 *xive = PNV_XIVE2(opaque);
1642     uint32_t reg = offset >> 3;
1643     uint8_t watch_engine;
1644 
1645     switch (offset) {
1646 
1647     /*
1648      * VSD table settings.
1649      * The Xive2Router model combines both VC and PC sub-engines. We
1650      * allow to configure the tables through both, for the rare cases
1651      * where a table only really needs to be configured for one of
1652      * them (e.g. the NVG table for the presenter). It assumes that
1653      * firmware passes the same address to the VC and PC when tables
1654      * are defined for both, which seems acceptable.
1655      */
1656     case PC_VSD_TABLE_ADDR:
1657         break;
1658     case PC_VSD_TABLE_DATA:
1659         pnv_xive2_pc_vst_set_data(xive, val);
1660         break;
1661 
1662     case PC_NXC_PROC_CONFIG:
1663         break;
1664 
1665     /*
1666      * cache updates
1667      */
1668     case PC_NXC_WATCH0_SPEC:
1669     case PC_NXC_WATCH1_SPEC:
1670     case PC_NXC_WATCH2_SPEC:
1671     case PC_NXC_WATCH3_SPEC:
1672         val &= ~PC_NXC_WATCH_CONFLICT; /* HW will set this bit */
1673         break;
1674 
1675     case PC_NXC_WATCH0_DATA1 ... PC_NXC_WATCH0_DATA3:
1676     case PC_NXC_WATCH1_DATA1 ... PC_NXC_WATCH1_DATA3:
1677     case PC_NXC_WATCH2_DATA1 ... PC_NXC_WATCH2_DATA3:
1678     case PC_NXC_WATCH3_DATA1 ... PC_NXC_WATCH3_DATA3:
1679         break;
1680     case PC_NXC_WATCH0_DATA0:
1681     case PC_NXC_WATCH1_DATA0:
1682     case PC_NXC_WATCH2_DATA0:
1683     case PC_NXC_WATCH3_DATA0:
1684         /* writing to DATA0 triggers the cache write */
1685         watch_engine = (offset - PC_NXC_WATCH0_DATA0) >> 6;
1686         xive->pc_regs[reg] = val;
1687         pnv_xive2_nxc_update(xive, watch_engine);
1688         break;
1689 
1690    /* case PC_NXC_FLUSH_CTRL: */
1691     case PC_NXC_FLUSH_POLL:
1692         xive->pc_regs[PC_NXC_FLUSH_CTRL >> 3] |= PC_NXC_FLUSH_CTRL_POLL_VALID;
1693         break;
1694 
1695     case PC_NXC_FLUSH_INJECT:
1696         pnv_xive2_inject_notify(xive, PNV_XIVE2_CACHE_NXC);
1697         break;
1698 
1699     /*
1700      * Indirect invalidation
1701      */
1702     case PC_AT_KILL:
1703     case PC_AT_KILL_MASK:
1704         break;
1705 
1706     default:
1707         xive2_error(xive, "PC: invalid write @0x%"HWADDR_PRIx" value 0x%"PRIx64,
1708                     offset, val);
1709         return;
1710     }
1711 
1712     xive->pc_regs[reg] = val;
1713 }
1714 
1715 static const MemoryRegionOps pnv_xive2_ic_pc_ops = {
1716     .read = pnv_xive2_ic_pc_read,
1717     .write = pnv_xive2_ic_pc_write,
1718     .endianness = DEVICE_BIG_ENDIAN,
1719     .valid = {
1720         .min_access_size = 8,
1721         .max_access_size = 8,
1722     },
1723     .impl = {
1724         .min_access_size = 8,
1725         .max_access_size = 8,
1726     },
1727 };
1728 
1729 
1730 static uint64_t pnv_xive2_ic_tctxt_read(void *opaque, hwaddr offset,
1731                                         unsigned size)
1732 {
1733     PnvXive2 *xive = PNV_XIVE2(opaque);
1734     uint64_t val = -1;
1735     uint32_t reg = offset >> 3;
1736 
1737     switch (offset) {
1738     /*
1739      * XIVE2 hardware thread enablement
1740      */
1741     case TCTXT_EN0:
1742     case TCTXT_EN1:
1743         val = xive->tctxt_regs[reg];
1744         break;
1745 
1746     case TCTXT_EN0_SET:
1747     case TCTXT_EN0_RESET:
1748         val = xive->tctxt_regs[TCTXT_EN0 >> 3];
1749         break;
1750     case TCTXT_EN1_SET:
1751     case TCTXT_EN1_RESET:
1752         val = xive->tctxt_regs[TCTXT_EN1 >> 3];
1753         break;
1754     case TCTXT_CFG:
1755         val = xive->tctxt_regs[reg];
1756         break;
1757     default:
1758         xive2_error(xive, "TCTXT: invalid read @%"HWADDR_PRIx, offset);
1759     }
1760 
1761     return val;
1762 }
1763 
1764 static void pnv_xive2_ic_tctxt_write(void *opaque, hwaddr offset,
1765                                      uint64_t val, unsigned size)
1766 {
1767     PnvXive2 *xive = PNV_XIVE2(opaque);
1768     uint32_t reg = offset >> 3;
1769 
1770     switch (offset) {
1771     /*
1772      * XIVE2 hardware thread enablement
1773      */
1774     case TCTXT_EN0: /* Physical Thread Enable */
1775     case TCTXT_EN1: /* Physical Thread Enable (fused core) */
1776         xive->tctxt_regs[reg] = val;
1777         break;
1778 
1779     case TCTXT_EN0_SET:
1780         xive->tctxt_regs[TCTXT_EN0 >> 3] |= val;
1781         break;
1782     case TCTXT_EN1_SET:
1783         xive->tctxt_regs[TCTXT_EN1 >> 3] |= val;
1784         break;
1785     case TCTXT_EN0_RESET:
1786         xive->tctxt_regs[TCTXT_EN0 >> 3] &= ~val;
1787         break;
1788     case TCTXT_EN1_RESET:
1789         xive->tctxt_regs[TCTXT_EN1 >> 3] &= ~val;
1790         break;
1791     case TCTXT_CFG:
1792         xive->tctxt_regs[reg] = val;
1793         break;
1794     default:
1795         xive2_error(xive, "TCTXT: invalid write @0x%"HWADDR_PRIx
1796                     " data 0x%"PRIx64, offset, val);
1797         return;
1798     }
1799 }
1800 
1801 static const MemoryRegionOps pnv_xive2_ic_tctxt_ops = {
1802     .read = pnv_xive2_ic_tctxt_read,
1803     .write = pnv_xive2_ic_tctxt_write,
1804     .endianness = DEVICE_BIG_ENDIAN,
1805     .valid = {
1806         .min_access_size = 8,
1807         .max_access_size = 8,
1808     },
1809     .impl = {
1810         .min_access_size = 8,
1811         .max_access_size = 8,
1812     },
1813 };
1814 
1815 /*
1816  * Redirect XSCOM to MMIO handlers
1817  */
1818 static uint64_t pnv_xive2_xscom_read(void *opaque, hwaddr offset,
1819                                      unsigned size)
1820 {
1821     PnvXive2 *xive = PNV_XIVE2(opaque);
1822     uint64_t val = -1;
1823     uint32_t xscom_reg = offset >> 3;
1824     uint32_t mmio_offset = (xscom_reg & 0xFF) << 3;
1825 
1826     switch (xscom_reg) {
1827     case 0x000 ... 0x0FF:
1828         val = pnv_xive2_ic_cq_read(opaque, mmio_offset, size);
1829         break;
1830     case 0x100 ... 0x1FF:
1831         val = pnv_xive2_ic_vc_read(opaque, mmio_offset, size);
1832         break;
1833     case 0x200 ... 0x2FF:
1834         val = pnv_xive2_ic_pc_read(opaque, mmio_offset, size);
1835         break;
1836     case 0x300 ... 0x3FF:
1837         val = pnv_xive2_ic_tctxt_read(opaque, mmio_offset, size);
1838         break;
1839     default:
1840         xive2_error(xive, "XSCOM: invalid read @%"HWADDR_PRIx, offset);
1841     }
1842 
1843     return val;
1844 }
1845 
1846 static void pnv_xive2_xscom_write(void *opaque, hwaddr offset,
1847                                   uint64_t val, unsigned size)
1848 {
1849     PnvXive2 *xive = PNV_XIVE2(opaque);
1850     uint32_t xscom_reg = offset >> 3;
1851     uint32_t mmio_offset = (xscom_reg & 0xFF) << 3;
1852 
1853     switch (xscom_reg) {
1854     case 0x000 ... 0x0FF:
1855         pnv_xive2_ic_cq_write(opaque, mmio_offset, val, size);
1856         break;
1857     case 0x100 ... 0x1FF:
1858         pnv_xive2_ic_vc_write(opaque, mmio_offset, val, size);
1859         break;
1860     case 0x200 ... 0x2FF:
1861         pnv_xive2_ic_pc_write(opaque, mmio_offset, val, size);
1862         break;
1863     case 0x300 ... 0x3FF:
1864         pnv_xive2_ic_tctxt_write(opaque, mmio_offset, val, size);
1865         break;
1866     default:
1867         xive2_error(xive, "XSCOM: invalid write @%"HWADDR_PRIx
1868                     " value 0x%"PRIx64, offset, val);
1869     }
1870 }
1871 
1872 static const MemoryRegionOps pnv_xive2_xscom_ops = {
1873     .read = pnv_xive2_xscom_read,
1874     .write = pnv_xive2_xscom_write,
1875     .endianness = DEVICE_BIG_ENDIAN,
1876     .valid = {
1877         .min_access_size = 8,
1878         .max_access_size = 8,
1879     },
1880     .impl = {
1881         .min_access_size = 8,
1882         .max_access_size = 8,
1883     },
1884 };
1885 
1886 /*
1887  * Notify port page. The layout is compatible between 4K and 64K pages :
1888  *
1889  * Page 1           Notify page (writes only)
1890  *  0x000 - 0x7FF   IPI interrupt (NPU)
1891  *  0x800 - 0xFFF   HW interrupt triggers (PSI, PHB)
1892  */
1893 
1894 static void pnv_xive2_ic_hw_trigger(PnvXive2 *xive, hwaddr addr,
1895                                     uint64_t val)
1896 {
1897     uint8_t blk;
1898     uint32_t idx;
1899 
1900     if (val & XIVE_TRIGGER_END) {
1901         xive2_error(xive, "IC: END trigger at @0x%"HWADDR_PRIx" data 0x%"PRIx64,
1902                    addr, val);
1903         return;
1904     }
1905 
1906     /*
1907      * Forward the source event notification directly to the Router.
1908      * The source interrupt number should already be correctly encoded
1909      * with the chip block id by the sending device (PHB, PSI).
1910      */
1911     blk = XIVE_EAS_BLOCK(val);
1912     idx = XIVE_EAS_INDEX(val);
1913 
1914     xive2_router_notify(XIVE_NOTIFIER(xive), XIVE_EAS(blk, idx),
1915                          !!(val & XIVE_TRIGGER_PQ));
1916 }
1917 
1918 static void pnv_xive2_ic_notify_write(void *opaque, hwaddr offset,
1919                                       uint64_t val, unsigned size)
1920 {
1921     PnvXive2 *xive = PNV_XIVE2(opaque);
1922 
1923     /* VC: IPI triggers */
1924     switch (offset) {
1925     case 0x000 ... 0x7FF:
1926         /* TODO: check IPI notify sub-page routing */
1927         pnv_xive2_ic_hw_trigger(opaque, offset, val);
1928         break;
1929 
1930     /* VC: HW triggers */
1931     case 0x800 ... 0xFFF:
1932         pnv_xive2_ic_hw_trigger(opaque, offset, val);
1933         break;
1934 
1935     default:
1936         xive2_error(xive, "NOTIFY: invalid write @%"HWADDR_PRIx
1937                     " value 0x%"PRIx64, offset, val);
1938     }
1939 }
1940 
1941 static uint64_t pnv_xive2_ic_notify_read(void *opaque, hwaddr offset,
1942                                          unsigned size)
1943 {
1944     PnvXive2 *xive = PNV_XIVE2(opaque);
1945 
1946    /* loads are invalid */
1947     xive2_error(xive, "NOTIFY: invalid read @%"HWADDR_PRIx, offset);
1948     return -1;
1949 }
1950 
1951 static const MemoryRegionOps pnv_xive2_ic_notify_ops = {
1952     .read = pnv_xive2_ic_notify_read,
1953     .write = pnv_xive2_ic_notify_write,
1954     .endianness = DEVICE_BIG_ENDIAN,
1955     .valid = {
1956         .min_access_size = 8,
1957         .max_access_size = 8,
1958     },
1959     .impl = {
1960         .min_access_size = 8,
1961         .max_access_size = 8,
1962     },
1963 };
1964 
1965 static uint64_t pnv_xive2_ic_lsi_read(void *opaque, hwaddr offset,
1966                                       unsigned size)
1967 {
1968     PnvXive2 *xive = PNV_XIVE2(opaque);
1969 
1970     xive2_error(xive, "LSI: invalid read @%"HWADDR_PRIx, offset);
1971     return -1;
1972 }
1973 
1974 static void pnv_xive2_ic_lsi_write(void *opaque, hwaddr offset,
1975                                    uint64_t val, unsigned size)
1976 {
1977     PnvXive2 *xive = PNV_XIVE2(opaque);
1978 
1979     xive2_error(xive, "LSI: invalid write @%"HWADDR_PRIx" value 0x%"PRIx64,
1980                 offset, val);
1981 }
1982 
1983 static const MemoryRegionOps pnv_xive2_ic_lsi_ops = {
1984     .read = pnv_xive2_ic_lsi_read,
1985     .write = pnv_xive2_ic_lsi_write,
1986     .endianness = DEVICE_BIG_ENDIAN,
1987     .valid = {
1988         .min_access_size = 8,
1989         .max_access_size = 8,
1990     },
1991     .impl = {
1992         .min_access_size = 8,
1993         .max_access_size = 8,
1994     },
1995 };
1996 
1997 /*
1998  * Sync MMIO page (write only)
1999  */
2000 #define PNV_XIVE2_SYNC_IPI              0x000
2001 #define PNV_XIVE2_SYNC_HW               0x080
2002 #define PNV_XIVE2_SYNC_NxC              0x100
2003 #define PNV_XIVE2_SYNC_INT              0x180
2004 #define PNV_XIVE2_SYNC_OS_ESC           0x200
2005 #define PNV_XIVE2_SYNC_POOL_ESC         0x280
2006 #define PNV_XIVE2_SYNC_HARD_ESC         0x300
2007 #define PNV_XIVE2_SYNC_NXC_LD_LCL_NCO   0x800
2008 #define PNV_XIVE2_SYNC_NXC_LD_LCL_CO    0x880
2009 #define PNV_XIVE2_SYNC_NXC_ST_LCL_NCI   0x900
2010 #define PNV_XIVE2_SYNC_NXC_ST_LCL_CI    0x980
2011 #define PNV_XIVE2_SYNC_NXC_ST_RMT_NCI   0xA00
2012 #define PNV_XIVE2_SYNC_NXC_ST_RMT_CI    0xA80
2013 
2014 static uint64_t pnv_xive2_ic_sync_read(void *opaque, hwaddr offset,
2015                                        unsigned size)
2016 {
2017     PnvXive2 *xive = PNV_XIVE2(opaque);
2018 
2019     /* loads are invalid */
2020     xive2_error(xive, "SYNC: invalid read @%"HWADDR_PRIx, offset);
2021     return -1;
2022 }
2023 
2024 /*
2025  * The sync MMIO space spans two pages.  The lower page is use for
2026  * queue sync "poll" requests while the upper page is used for queue
2027  * sync "inject" requests.  Inject requests require the HW to write
2028  * a byte of all 1's to a predetermined location in memory in order
2029  * to signal completion of the request.  Both pages have the same
2030  * layout, so it is easiest to handle both with a single function.
2031  */
2032 static void pnv_xive2_ic_sync_write(void *opaque, hwaddr offset,
2033                                     uint64_t val, unsigned size)
2034 {
2035     PnvXive2 *xive = PNV_XIVE2(opaque);
2036     int inject_type;
2037     hwaddr pg_offset_mask = (1ull << xive->ic_shift) - 1;
2038 
2039     /* adjust offset for inject page */
2040     hwaddr adj_offset = offset & pg_offset_mask;
2041 
2042     switch (adj_offset) {
2043     case PNV_XIVE2_SYNC_IPI:
2044         inject_type = PNV_XIVE2_QUEUE_IPI;
2045         break;
2046     case PNV_XIVE2_SYNC_HW:
2047         inject_type = PNV_XIVE2_QUEUE_HW;
2048         break;
2049     case PNV_XIVE2_SYNC_NxC:
2050         inject_type = PNV_XIVE2_QUEUE_NXC;
2051         break;
2052     case PNV_XIVE2_SYNC_INT:
2053         inject_type = PNV_XIVE2_QUEUE_INT;
2054         break;
2055     case PNV_XIVE2_SYNC_OS_ESC:
2056         inject_type = PNV_XIVE2_QUEUE_OS;
2057         break;
2058     case PNV_XIVE2_SYNC_POOL_ESC:
2059         inject_type = PNV_XIVE2_QUEUE_POOL;
2060         break;
2061     case PNV_XIVE2_SYNC_HARD_ESC:
2062         inject_type = PNV_XIVE2_QUEUE_HARD;
2063         break;
2064     case PNV_XIVE2_SYNC_NXC_LD_LCL_NCO:
2065         inject_type = PNV_XIVE2_QUEUE_NXC_LD_LCL_NCO;
2066         break;
2067     case PNV_XIVE2_SYNC_NXC_LD_LCL_CO:
2068         inject_type = PNV_XIVE2_QUEUE_NXC_LD_LCL_CO;
2069         break;
2070     case PNV_XIVE2_SYNC_NXC_ST_LCL_NCI:
2071         inject_type = PNV_XIVE2_QUEUE_NXC_ST_LCL_NCI;
2072         break;
2073     case PNV_XIVE2_SYNC_NXC_ST_LCL_CI:
2074         inject_type = PNV_XIVE2_QUEUE_NXC_ST_LCL_CI;
2075         break;
2076     case PNV_XIVE2_SYNC_NXC_ST_RMT_NCI:
2077         inject_type = PNV_XIVE2_QUEUE_NXC_ST_RMT_NCI;
2078         break;
2079     case PNV_XIVE2_SYNC_NXC_ST_RMT_CI:
2080         inject_type = PNV_XIVE2_QUEUE_NXC_ST_RMT_CI;
2081         break;
2082     default:
2083         xive2_error(xive, "SYNC: invalid write @%"HWADDR_PRIx" value 0x%"PRIx64,
2084                     offset, val);
2085         return;
2086     }
2087 
2088     /* Write Queue Sync notification byte if writing to sync inject page */
2089     if ((offset & ~pg_offset_mask) != 0) {
2090         pnv_xive2_inject_notify(xive, inject_type);
2091     }
2092 }
2093 
2094 static const MemoryRegionOps pnv_xive2_ic_sync_ops = {
2095     .read = pnv_xive2_ic_sync_read,
2096     .write = pnv_xive2_ic_sync_write,
2097     .endianness = DEVICE_BIG_ENDIAN,
2098     .valid = {
2099         .min_access_size = 8,
2100         .max_access_size = 8,
2101     },
2102     .impl = {
2103         .min_access_size = 8,
2104         .max_access_size = 8,
2105     },
2106 };
2107 
2108 /*
2109  * When the TM direct pages of the IC controller are accessed, the
2110  * target HW thread is deduced from the page offset.
2111  */
2112 static uint32_t pnv_xive2_ic_tm_get_pir(PnvXive2 *xive, hwaddr offset)
2113 {
2114     /* On P10, the node ID shift in the PIR register is 8 bits */
2115     return xive->chip->chip_id << 8 | offset >> xive->ic_shift;
2116 }
2117 
2118 static uint32_t pnv_xive2_ic_tm_get_hw_page_offset(PnvXive2 *xive,
2119                                                    hwaddr offset)
2120 {
2121     /*
2122      * Indirect TIMA accesses are similar to direct accesses for
2123      * privilege ring 0. So remove any traces of the hw thread ID from
2124      * the offset in the IC BAR as it could be interpreted as the ring
2125      * privilege when calling the underlying direct access functions.
2126      */
2127     return offset & ((1ull << xive->ic_shift) - 1);
2128 }
2129 
2130 static XiveTCTX *pnv_xive2_get_indirect_tctx(PnvXive2 *xive, uint32_t pir)
2131 {
2132     PnvChip *chip = xive->chip;
2133     PowerPCCPU *cpu = NULL;
2134 
2135     cpu = pnv_chip_find_cpu(chip, pir);
2136     if (!cpu) {
2137         xive2_error(xive, "IC: invalid PIR %x for indirect access", pir);
2138         return NULL;
2139     }
2140 
2141     if (!pnv_xive2_is_cpu_enabled(xive, cpu)) {
2142         xive2_error(xive, "IC: CPU %x is not enabled", pir);
2143     }
2144 
2145     return XIVE_TCTX(pnv_cpu_state(cpu)->intc);
2146 }
2147 
2148 static uint64_t pnv_xive2_ic_tm_indirect_read(void *opaque, hwaddr offset,
2149                                               unsigned size)
2150 {
2151     PnvXive2 *xive = PNV_XIVE2(opaque);
2152     XivePresenter *xptr = XIVE_PRESENTER(xive);
2153     hwaddr hw_page_offset;
2154     uint32_t pir;
2155     XiveTCTX *tctx;
2156     uint64_t val = -1;
2157 
2158     pir = pnv_xive2_ic_tm_get_pir(xive, offset);
2159     hw_page_offset = pnv_xive2_ic_tm_get_hw_page_offset(xive, offset);
2160     tctx = pnv_xive2_get_indirect_tctx(xive, pir);
2161     if (tctx) {
2162         val = xive_tctx_tm_read(xptr, tctx, hw_page_offset, size);
2163     }
2164 
2165     return val;
2166 }
2167 
2168 static void pnv_xive2_ic_tm_indirect_write(void *opaque, hwaddr offset,
2169                                            uint64_t val, unsigned size)
2170 {
2171     PnvXive2 *xive = PNV_XIVE2(opaque);
2172     XivePresenter *xptr = XIVE_PRESENTER(xive);
2173     hwaddr hw_page_offset;
2174     uint32_t pir;
2175     XiveTCTX *tctx;
2176 
2177     pir = pnv_xive2_ic_tm_get_pir(xive, offset);
2178     hw_page_offset = pnv_xive2_ic_tm_get_hw_page_offset(xive, offset);
2179     tctx = pnv_xive2_get_indirect_tctx(xive, pir);
2180     if (tctx) {
2181         xive_tctx_tm_write(xptr, tctx, hw_page_offset, val, size);
2182     }
2183 }
2184 
2185 static const MemoryRegionOps pnv_xive2_ic_tm_indirect_ops = {
2186     .read = pnv_xive2_ic_tm_indirect_read,
2187     .write = pnv_xive2_ic_tm_indirect_write,
2188     .endianness = DEVICE_BIG_ENDIAN,
2189     .valid = {
2190         .min_access_size = 1,
2191         .max_access_size = 8,
2192     },
2193     .impl = {
2194         .min_access_size = 1,
2195         .max_access_size = 8,
2196     },
2197 };
2198 
2199 /*
2200  * TIMA ops
2201  */
2202 static void pnv_xive2_tm_write(void *opaque, hwaddr offset,
2203                                uint64_t value, unsigned size)
2204 {
2205     PowerPCCPU *cpu = POWERPC_CPU(current_cpu);
2206     PnvXive2 *xive = pnv_xive2_tm_get_xive(cpu);
2207     XiveTCTX *tctx = XIVE_TCTX(pnv_cpu_state(cpu)->intc);
2208     XivePresenter *xptr = XIVE_PRESENTER(xive);
2209 
2210     xive_tctx_tm_write(xptr, tctx, offset, value, size);
2211 }
2212 
2213 static uint64_t pnv_xive2_tm_read(void *opaque, hwaddr offset, unsigned size)
2214 {
2215     PowerPCCPU *cpu = POWERPC_CPU(current_cpu);
2216     PnvXive2 *xive = pnv_xive2_tm_get_xive(cpu);
2217     XiveTCTX *tctx = XIVE_TCTX(pnv_cpu_state(cpu)->intc);
2218     XivePresenter *xptr = XIVE_PRESENTER(xive);
2219 
2220     return xive_tctx_tm_read(xptr, tctx, offset, size);
2221 }
2222 
2223 static const MemoryRegionOps pnv_xive2_tm_ops = {
2224     .read = pnv_xive2_tm_read,
2225     .write = pnv_xive2_tm_write,
2226     .endianness = DEVICE_BIG_ENDIAN,
2227     .valid = {
2228         .min_access_size = 1,
2229         .max_access_size = 8,
2230     },
2231     .impl = {
2232         .min_access_size = 1,
2233         .max_access_size = 8,
2234     },
2235 };
2236 
2237 static uint64_t pnv_xive2_nvc_read(void *opaque, hwaddr addr,
2238                                    unsigned size)
2239 {
2240     PnvXive2 *xive = PNV_XIVE2(opaque);
2241     XivePresenter *xptr = XIVE_PRESENTER(xive);
2242     uint32_t page = addr >> xive->nvpg_shift;
2243     uint16_t op = addr & 0xFFF;
2244     uint8_t blk = pnv_xive2_block_id(xive);
2245 
2246     if (size != 2) {
2247         qemu_log_mask(LOG_GUEST_ERROR, "XIVE: invalid nvc load size %d\n",
2248                       size);
2249         return -1;
2250     }
2251 
2252     return xive2_presenter_nvgc_backlog_op(xptr, true, blk, page, op, 1);
2253 }
2254 
2255 static void pnv_xive2_nvc_write(void *opaque, hwaddr addr,
2256                                 uint64_t val, unsigned size)
2257 {
2258     PnvXive2 *xive = PNV_XIVE2(opaque);
2259     XivePresenter *xptr = XIVE_PRESENTER(xive);
2260     uint32_t page = addr >> xive->nvc_shift;
2261     uint16_t op = addr & 0xFFF;
2262     uint8_t blk = pnv_xive2_block_id(xive);
2263 
2264     if (size != 1) {
2265         qemu_log_mask(LOG_GUEST_ERROR, "XIVE: invalid nvc write size %d\n",
2266                       size);
2267         return;
2268     }
2269 
2270     (void)xive2_presenter_nvgc_backlog_op(xptr, true, blk, page, op, val);
2271 }
2272 
2273 static const MemoryRegionOps pnv_xive2_nvc_ops = {
2274     .read = pnv_xive2_nvc_read,
2275     .write = pnv_xive2_nvc_write,
2276     .endianness = DEVICE_BIG_ENDIAN,
2277     .valid = {
2278         .min_access_size = 1,
2279         .max_access_size = 8,
2280     },
2281     .impl = {
2282         .min_access_size = 1,
2283         .max_access_size = 8,
2284     },
2285 };
2286 
2287 static uint64_t pnv_xive2_nvpg_read(void *opaque, hwaddr addr,
2288                                     unsigned size)
2289 {
2290     PnvXive2 *xive = PNV_XIVE2(opaque);
2291     XivePresenter *xptr = XIVE_PRESENTER(xive);
2292     uint32_t page = addr >> xive->nvpg_shift;
2293     uint16_t op = addr & 0xFFF;
2294     uint32_t index = page >> 1;
2295     uint8_t blk = pnv_xive2_block_id(xive);
2296 
2297     if (size != 2) {
2298         qemu_log_mask(LOG_GUEST_ERROR, "XIVE: invalid nvpg load size %d\n",
2299                       size);
2300         return -1;
2301     }
2302 
2303     if (page % 2) {
2304         /* odd page - NVG */
2305         return xive2_presenter_nvgc_backlog_op(xptr, false, blk, index, op, 1);
2306     } else {
2307         /* even page - NVP */
2308         return xive2_presenter_nvp_backlog_op(xptr, blk, index, op);
2309     }
2310 }
2311 
2312 static void pnv_xive2_nvpg_write(void *opaque, hwaddr addr,
2313                                  uint64_t val, unsigned size)
2314 {
2315     PnvXive2 *xive = PNV_XIVE2(opaque);
2316     XivePresenter *xptr = XIVE_PRESENTER(xive);
2317     uint32_t page = addr >> xive->nvpg_shift;
2318     uint16_t op = addr & 0xFFF;
2319     uint32_t index = page >> 1;
2320     uint8_t blk = pnv_xive2_block_id(xive);
2321 
2322     if (size != 1) {
2323         qemu_log_mask(LOG_GUEST_ERROR, "XIVE: invalid nvpg write size %d\n",
2324                       size);
2325         return;
2326     }
2327 
2328     if (page % 2) {
2329         /* odd page - NVG */
2330         (void)xive2_presenter_nvgc_backlog_op(xptr, false, blk, index, op, val);
2331     } else {
2332         /* even page - NVP */
2333         (void)xive2_presenter_nvp_backlog_op(xptr, blk, index, op);
2334     }
2335 }
2336 
2337 static const MemoryRegionOps pnv_xive2_nvpg_ops = {
2338     .read = pnv_xive2_nvpg_read,
2339     .write = pnv_xive2_nvpg_write,
2340     .endianness = DEVICE_BIG_ENDIAN,
2341     .valid = {
2342         .min_access_size = 1,
2343         .max_access_size = 8,
2344     },
2345     .impl = {
2346         .min_access_size = 1,
2347         .max_access_size = 8,
2348     },
2349 };
2350 
2351 /*
2352  * POWER10 default capabilities: 0x2000120076f000FC
2353  */
2354 #define PNV_XIVE2_CAPABILITIES  0x2000120076f000FC
2355 
2356 /*
2357  * POWER10 default configuration: 0x0030000033000000
2358  *
2359  * 8bits thread id was dropped for P10
2360  */
2361 #define PNV_XIVE2_CONFIGURATION 0x0030000033000000
2362 
2363 static void pnv_xive2_reset(void *dev)
2364 {
2365     PnvXive2 *xive = PNV_XIVE2(dev);
2366     XiveSource *xsrc = &xive->ipi_source;
2367     Xive2EndSource *end_xsrc = &xive->end_source;
2368 
2369     xive->cq_regs[CQ_XIVE_CAP >> 3] = xive->capabilities;
2370     xive->cq_regs[CQ_XIVE_CFG >> 3] = xive->config;
2371 
2372     /* HW hardwires the #Topology of the chip in the block field */
2373     xive->cq_regs[CQ_XIVE_CFG >> 3] |=
2374         SETFIELD(CQ_XIVE_CFG_HYP_HARD_BLOCK_ID, 0ull, xive->chip->chip_id);
2375 
2376     /* VC and PC cache watch assign mechanism */
2377     xive->vc_regs[VC_ENDC_CFG >> 3] =
2378         SETFIELD(VC_ENDC_CFG_CACHE_WATCH_ASSIGN, 0ull, 0b0111);
2379     xive->pc_regs[PC_NXC_PROC_CONFIG >> 3] =
2380         SETFIELD(PC_NXC_PROC_CONFIG_WATCH_ASSIGN, 0ull, 0b0111);
2381 
2382     /* Set default page size to 64k */
2383     xive->ic_shift = xive->esb_shift = xive->end_shift = 16;
2384     xive->nvc_shift = xive->nvpg_shift = xive->tm_shift = 16;
2385 
2386     /* Clear source MMIOs */
2387     if (memory_region_is_mapped(&xsrc->esb_mmio)) {
2388         memory_region_del_subregion(&xive->esb_mmio, &xsrc->esb_mmio);
2389     }
2390 
2391     if (memory_region_is_mapped(&end_xsrc->esb_mmio)) {
2392         memory_region_del_subregion(&xive->end_mmio, &end_xsrc->esb_mmio);
2393     }
2394 }
2395 
2396 /*
2397  *  Maximum number of IRQs and ENDs supported by HW. Will be tuned by
2398  *  software.
2399  */
2400 #define PNV_XIVE2_NR_IRQS (PNV10_XIVE2_ESB_SIZE / (1ull << XIVE_ESB_64K_2PAGE))
2401 #define PNV_XIVE2_NR_ENDS (PNV10_XIVE2_END_SIZE / (1ull << XIVE_ESB_64K_2PAGE))
2402 
2403 static void pnv_xive2_realize(DeviceState *dev, Error **errp)
2404 {
2405     PnvXive2 *xive = PNV_XIVE2(dev);
2406     PnvXive2Class *pxc = PNV_XIVE2_GET_CLASS(dev);
2407     XiveSource *xsrc = &xive->ipi_source;
2408     Xive2EndSource *end_xsrc = &xive->end_source;
2409     Error *local_err = NULL;
2410     int i;
2411 
2412     pxc->parent_realize(dev, &local_err);
2413     if (local_err) {
2414         error_propagate(errp, local_err);
2415         return;
2416     }
2417 
2418     assert(xive->chip);
2419 
2420     /*
2421      * The XiveSource and Xive2EndSource objects are realized with the
2422      * maximum allowed HW configuration. The ESB MMIO regions will be
2423      * resized dynamically when the controller is configured by the FW
2424      * to limit accesses to resources not provisioned.
2425      */
2426     object_property_set_int(OBJECT(xsrc), "flags", XIVE_SRC_STORE_EOI,
2427                             &error_fatal);
2428     object_property_set_int(OBJECT(xsrc), "nr-irqs", PNV_XIVE2_NR_IRQS,
2429                             &error_fatal);
2430     object_property_set_link(OBJECT(xsrc), "xive", OBJECT(xive),
2431                              &error_fatal);
2432     qdev_realize(DEVICE(xsrc), NULL, &local_err);
2433     if (local_err) {
2434         error_propagate(errp, local_err);
2435         return;
2436     }
2437 
2438     object_property_set_int(OBJECT(end_xsrc), "nr-ends", PNV_XIVE2_NR_ENDS,
2439                             &error_fatal);
2440     object_property_set_link(OBJECT(end_xsrc), "xive", OBJECT(xive),
2441                              &error_abort);
2442     qdev_realize(DEVICE(end_xsrc), NULL, &local_err);
2443     if (local_err) {
2444         error_propagate(errp, local_err);
2445         return;
2446     }
2447 
2448     /* XSCOM region, used for initial configuration of the BARs */
2449     memory_region_init_io(&xive->xscom_regs, OBJECT(dev),
2450                           &pnv_xive2_xscom_ops, xive, "xscom-xive",
2451                           PNV10_XSCOM_XIVE2_SIZE << 3);
2452 
2453     /* Interrupt controller MMIO regions */
2454     xive->ic_shift = 16;
2455     memory_region_init(&xive->ic_mmio, OBJECT(dev), "xive-ic",
2456                        PNV10_XIVE2_IC_SIZE);
2457 
2458     for (i = 0; i < ARRAY_SIZE(xive->ic_mmios); i++) {
2459         memory_region_init_io(&xive->ic_mmios[i], OBJECT(dev),
2460                          pnv_xive2_ic_regions[i].ops, xive,
2461                          pnv_xive2_ic_regions[i].name,
2462                          pnv_xive2_ic_regions[i].pgsize << xive->ic_shift);
2463     }
2464 
2465     /*
2466      * VC MMIO regions.
2467      */
2468     xive->esb_shift = 16;
2469     xive->end_shift = 16;
2470     memory_region_init(&xive->esb_mmio, OBJECT(xive), "xive-esb",
2471                        PNV10_XIVE2_ESB_SIZE);
2472     memory_region_init(&xive->end_mmio, OBJECT(xive), "xive-end",
2473                        PNV10_XIVE2_END_SIZE);
2474 
2475     /* Presenter Controller MMIO region (not modeled) */
2476     xive->nvc_shift = 16;
2477     xive->nvpg_shift = 16;
2478     memory_region_init_io(&xive->nvc_mmio, OBJECT(dev),
2479                           &pnv_xive2_nvc_ops, xive,
2480                           "xive-nvc", PNV10_XIVE2_NVC_SIZE);
2481 
2482     memory_region_init_io(&xive->nvpg_mmio, OBJECT(dev),
2483                           &pnv_xive2_nvpg_ops, xive,
2484                           "xive-nvpg", PNV10_XIVE2_NVPG_SIZE);
2485 
2486     /* Thread Interrupt Management Area (Direct) */
2487     xive->tm_shift = 16;
2488     memory_region_init_io(&xive->tm_mmio, OBJECT(dev), &pnv_xive2_tm_ops,
2489                           xive, "xive-tima", PNV10_XIVE2_TM_SIZE);
2490 
2491     qemu_register_reset(pnv_xive2_reset, dev);
2492 }
2493 
2494 static const Property pnv_xive2_properties[] = {
2495     DEFINE_PROP_UINT64("ic-bar", PnvXive2, ic_base, 0),
2496     DEFINE_PROP_UINT64("esb-bar", PnvXive2, esb_base, 0),
2497     DEFINE_PROP_UINT64("end-bar", PnvXive2, end_base, 0),
2498     DEFINE_PROP_UINT64("nvc-bar", PnvXive2, nvc_base, 0),
2499     DEFINE_PROP_UINT64("nvpg-bar", PnvXive2, nvpg_base, 0),
2500     DEFINE_PROP_UINT64("tm-bar", PnvXive2, tm_base, 0),
2501     DEFINE_PROP_UINT64("capabilities", PnvXive2, capabilities,
2502                        PNV_XIVE2_CAPABILITIES),
2503     DEFINE_PROP_UINT64("config", PnvXive2, config,
2504                        PNV_XIVE2_CONFIGURATION),
2505     DEFINE_PROP_LINK("chip", PnvXive2, chip, TYPE_PNV_CHIP, PnvChip *),
2506 };
2507 
2508 static void pnv_xive2_instance_init(Object *obj)
2509 {
2510     PnvXive2 *xive = PNV_XIVE2(obj);
2511 
2512     object_initialize_child(obj, "ipi_source", &xive->ipi_source,
2513                             TYPE_XIVE_SOURCE);
2514     object_initialize_child(obj, "end_source", &xive->end_source,
2515                             TYPE_XIVE2_END_SOURCE);
2516 }
2517 
2518 static int pnv_xive2_dt_xscom(PnvXScomInterface *dev, void *fdt,
2519                               int xscom_offset)
2520 {
2521     const char compat_p10[] = "ibm,power10-xive-x";
2522     char *name;
2523     int offset;
2524     uint32_t reg[] = {
2525         cpu_to_be32(PNV10_XSCOM_XIVE2_BASE),
2526         cpu_to_be32(PNV10_XSCOM_XIVE2_SIZE)
2527     };
2528 
2529     name = g_strdup_printf("xive@%x", PNV10_XSCOM_XIVE2_BASE);
2530     offset = fdt_add_subnode(fdt, xscom_offset, name);
2531     _FDT(offset);
2532     g_free(name);
2533 
2534     _FDT((fdt_setprop(fdt, offset, "reg", reg, sizeof(reg))));
2535     _FDT(fdt_setprop(fdt, offset, "compatible", compat_p10,
2536                      sizeof(compat_p10)));
2537     return 0;
2538 }
2539 
2540 static void pnv_xive2_class_init(ObjectClass *klass, const void *data)
2541 {
2542     DeviceClass *dc = DEVICE_CLASS(klass);
2543     PnvXScomInterfaceClass *xdc = PNV_XSCOM_INTERFACE_CLASS(klass);
2544     Xive2RouterClass *xrc = XIVE2_ROUTER_CLASS(klass);
2545     XiveNotifierClass *xnc = XIVE_NOTIFIER_CLASS(klass);
2546     XivePresenterClass *xpc = XIVE_PRESENTER_CLASS(klass);
2547     PnvXive2Class *pxc = PNV_XIVE2_CLASS(klass);
2548 
2549     xdc->dt_xscom  = pnv_xive2_dt_xscom;
2550 
2551     dc->desc       = "PowerNV XIVE2 Interrupt Controller (POWER10)";
2552     device_class_set_parent_realize(dc, pnv_xive2_realize,
2553                                     &pxc->parent_realize);
2554     device_class_set_props(dc, pnv_xive2_properties);
2555 
2556     xrc->get_eas   = pnv_xive2_get_eas;
2557     xrc->get_pq    = pnv_xive2_get_pq;
2558     xrc->set_pq    = pnv_xive2_set_pq;
2559     xrc->get_end   = pnv_xive2_get_end;
2560     xrc->write_end = pnv_xive2_write_end;
2561     xrc->get_nvp   = pnv_xive2_get_nvp;
2562     xrc->write_nvp = pnv_xive2_write_nvp;
2563     xrc->get_nvgc   = pnv_xive2_get_nvgc;
2564     xrc->write_nvgc = pnv_xive2_write_nvgc;
2565     xrc->get_config  = pnv_xive2_get_config;
2566     xrc->get_block_id = pnv_xive2_get_block_id;
2567 
2568     xnc->notify    = pnv_xive2_notify;
2569 
2570     xpc->match_nvt  = pnv_xive2_match_nvt;
2571     xpc->get_config = pnv_xive2_presenter_get_config;
2572     xpc->broadcast  = pnv_xive2_broadcast;
2573 };
2574 
2575 static const TypeInfo pnv_xive2_info = {
2576     .name          = TYPE_PNV_XIVE2,
2577     .parent        = TYPE_XIVE2_ROUTER,
2578     .instance_init = pnv_xive2_instance_init,
2579     .instance_size = sizeof(PnvXive2),
2580     .class_init    = pnv_xive2_class_init,
2581     .class_size    = sizeof(PnvXive2Class),
2582     .interfaces    = (const InterfaceInfo[]) {
2583         { TYPE_PNV_XSCOM_INTERFACE },
2584         { }
2585     }
2586 };
2587 
2588 static void pnv_xive2_register_types(void)
2589 {
2590     type_register_static(&pnv_xive2_info);
2591 }
2592 
2593 type_init(pnv_xive2_register_types)
2594 
2595 /*
2596  * If the table is direct, we can compute the number of PQ entries
2597  * provisioned by FW.
2598  */
2599 static uint32_t pnv_xive2_nr_esbs(PnvXive2 *xive)
2600 {
2601     uint8_t blk = pnv_xive2_block_id(xive);
2602     uint64_t vsd = xive->vsds[VST_ESB][blk];
2603     uint64_t vst_tsize = 1ull << (GETFIELD(VSD_TSIZE, vsd) + 12);
2604 
2605     return VSD_INDIRECT & vsd ? 0 : vst_tsize * SBE_PER_BYTE;
2606 }
2607 
2608 /*
2609  * Compute the number of entries per indirect subpage.
2610  */
2611 static uint64_t pnv_xive2_vst_per_subpage(PnvXive2 *xive, uint32_t type)
2612 {
2613     uint8_t blk = pnv_xive2_block_id(xive);
2614     uint64_t vsd = xive->vsds[type][blk];
2615     const XiveVstInfo *info = &vst_infos[type];
2616     uint64_t vsd_addr;
2617     uint32_t page_shift;
2618 
2619     /* For direct tables, fake a valid value */
2620     if (!(VSD_INDIRECT & vsd)) {
2621         return 1;
2622     }
2623 
2624     /* Get the page size of the indirect table. */
2625     vsd_addr = vsd & VSD_ADDRESS_MASK;
2626     ldq_be_dma(&address_space_memory, vsd_addr, &vsd, MEMTXATTRS_UNSPECIFIED);
2627 
2628     if (!(vsd & VSD_ADDRESS_MASK)) {
2629 #ifdef XIVE2_DEBUG
2630         xive2_error(xive, "VST: invalid %s entry!?", info->name);
2631 #endif
2632         return 0;
2633     }
2634 
2635     page_shift = GETFIELD(VSD_TSIZE, vsd) + 12;
2636 
2637     if (!pnv_xive2_vst_page_size_allowed(page_shift)) {
2638         xive2_error(xive, "VST: invalid %s page shift %d", info->name,
2639                    page_shift);
2640         return 0;
2641     }
2642 
2643     return (1ull << page_shift) / info->size;
2644 }
2645 
2646 void pnv_xive2_pic_print_info(PnvXive2 *xive, GString *buf)
2647 {
2648     Xive2Router *xrtr = XIVE2_ROUTER(xive);
2649     uint8_t blk = pnv_xive2_block_id(xive);
2650     uint8_t chip_id = xive->chip->chip_id;
2651     uint32_t srcno0 = XIVE_EAS(blk, 0);
2652     uint32_t nr_esbs = pnv_xive2_nr_esbs(xive);
2653     Xive2Eas eas;
2654     Xive2End end;
2655     Xive2Nvp nvp;
2656     Xive2Nvgc nvgc;
2657     int i;
2658     uint64_t entries_per_subpage;
2659 
2660     g_string_append_printf(buf, "XIVE[%x] Source %08x .. %08x\n",
2661                            blk, srcno0, srcno0 + nr_esbs - 1);
2662     xive_source_pic_print_info(&xive->ipi_source, srcno0, buf);
2663 
2664     g_string_append_printf(buf, "XIVE[%x] EAT %08x .. %08x\n",
2665                            blk, srcno0, srcno0 + nr_esbs - 1);
2666     for (i = 0; i < nr_esbs; i++) {
2667         if (xive2_router_get_eas(xrtr, blk, i, &eas)) {
2668             break;
2669         }
2670         if (!xive2_eas_is_masked(&eas)) {
2671             xive2_eas_pic_print_info(&eas, i, buf);
2672         }
2673     }
2674 
2675     g_string_append_printf(buf, "XIVE[%x] #%d END Escalation EAT\n",
2676                            chip_id, blk);
2677     i = 0;
2678     while (!xive2_router_get_end(xrtr, blk, i, &end)) {
2679         xive2_end_eas_pic_print_info(&end, i++, buf);
2680     }
2681 
2682     g_string_append_printf(buf, "XIVE[%x] #%d ENDT\n", chip_id, blk);
2683     i = 0;
2684     while (!xive2_router_get_end(xrtr, blk, i, &end)) {
2685         xive2_end_pic_print_info(&end, i++, buf);
2686     }
2687 
2688     g_string_append_printf(buf, "XIVE[%x] #%d NVPT %08x .. %08x\n",
2689                            chip_id, blk, 0, XIVE2_NVP_COUNT - 1);
2690     entries_per_subpage = pnv_xive2_vst_per_subpage(xive, VST_NVP);
2691     for (i = 0; i < XIVE2_NVP_COUNT; i += entries_per_subpage) {
2692         while (!xive2_router_get_nvp(xrtr, blk, i, &nvp)) {
2693             xive2_nvp_pic_print_info(&nvp, i++, buf);
2694         }
2695     }
2696 
2697     g_string_append_printf(buf, "XIVE[%x] #%d NVGT %08x .. %08x\n",
2698                            chip_id, blk, 0, XIVE2_NVP_COUNT - 1);
2699     entries_per_subpage = pnv_xive2_vst_per_subpage(xive, VST_NVG);
2700     for (i = 0; i < XIVE2_NVP_COUNT; i += entries_per_subpage) {
2701         while (!xive2_router_get_nvgc(xrtr, false, blk, i, &nvgc)) {
2702             xive2_nvgc_pic_print_info(&nvgc, i++, buf);
2703         }
2704     }
2705 
2706     g_string_append_printf(buf, "XIVE[%x] #%d NVCT %08x .. %08x\n",
2707                           chip_id, blk, 0, XIVE2_NVP_COUNT - 1);
2708     entries_per_subpage = pnv_xive2_vst_per_subpage(xive, VST_NVC);
2709     for (i = 0; i < XIVE2_NVP_COUNT; i += entries_per_subpage) {
2710         while (!xive2_router_get_nvgc(xrtr, true, blk, i, &nvgc)) {
2711             xive2_nvgc_pic_print_info(&nvgc, i++, buf);
2712         }
2713     }
2714 }
2715