xref: /openbmc/qemu/hw/intc/pnv_xive2.c (revision a1577527e212efd27a8ceefbd95321c306abf739)
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, offset);
1201         return;
1202     }
1203 
1204     xive->cq_regs[reg] = val;
1205 }
1206 
1207 static const MemoryRegionOps pnv_xive2_ic_cq_ops = {
1208     .read = pnv_xive2_ic_cq_read,
1209     .write = pnv_xive2_ic_cq_write,
1210     .endianness = DEVICE_BIG_ENDIAN,
1211     .valid = {
1212         .min_access_size = 8,
1213         .max_access_size = 8,
1214     },
1215     .impl = {
1216         .min_access_size = 8,
1217         .max_access_size = 8,
1218     },
1219 };
1220 
1221 static uint8_t pnv_xive2_cache_watch_assign(uint64_t engine_mask,
1222                                             uint64_t *state)
1223 {
1224     uint8_t val = 0xFF;
1225     int i;
1226 
1227     for (i = 3; i >= 0; i--) {
1228         if (BIT(i) & engine_mask) {
1229             if (!(BIT(i) & *state)) {
1230                 *state |= BIT(i);
1231                 val = 3 - i;
1232                 break;
1233             }
1234         }
1235     }
1236     return val;
1237 }
1238 
1239 static void pnv_xive2_cache_watch_release(uint64_t *state, uint8_t watch_engine)
1240 {
1241     uint8_t engine_bit = 3 - watch_engine;
1242 
1243     if (*state & BIT(engine_bit)) {
1244         *state &= ~BIT(engine_bit);
1245     }
1246 }
1247 
1248 static uint8_t pnv_xive2_endc_cache_watch_assign(PnvXive2 *xive)
1249 {
1250     uint64_t engine_mask = GETFIELD(VC_ENDC_CFG_CACHE_WATCH_ASSIGN,
1251                                     xive->vc_regs[VC_ENDC_CFG >> 3]);
1252     uint64_t state = xive->vc_regs[VC_ENDC_WATCH_ASSIGN >> 3];
1253     uint8_t val;
1254 
1255     /*
1256      * We keep track of which engines are currently busy in the
1257      * VC_ENDC_WATCH_ASSIGN register directly. When the firmware reads
1258      * the register, we don't return its value but the ID of an engine
1259      * it can use.
1260      * There are 4 engines. 0xFF means no engine is available.
1261      */
1262     val = pnv_xive2_cache_watch_assign(engine_mask, &state);
1263     if (val != 0xFF) {
1264         xive->vc_regs[VC_ENDC_WATCH_ASSIGN >> 3] = state;
1265     }
1266     return val;
1267 }
1268 
1269 static void pnv_xive2_endc_cache_watch_release(PnvXive2 *xive,
1270                                                uint8_t watch_engine)
1271 {
1272     uint64_t state = xive->vc_regs[VC_ENDC_WATCH_ASSIGN >> 3];
1273 
1274     pnv_xive2_cache_watch_release(&state, watch_engine);
1275     xive->vc_regs[VC_ENDC_WATCH_ASSIGN >> 3] = state;
1276 }
1277 
1278 static uint64_t pnv_xive2_ic_vc_read(void *opaque, hwaddr offset,
1279                                      unsigned size)
1280 {
1281     PnvXive2 *xive = PNV_XIVE2(opaque);
1282     uint64_t val = 0;
1283     uint32_t reg = offset >> 3;
1284     uint8_t watch_engine;
1285 
1286     switch (offset) {
1287     /*
1288      * VSD table settings.
1289      */
1290     case VC_VSD_TABLE_ADDR:
1291     case VC_VSD_TABLE_DATA:
1292         val = xive->vc_regs[reg];
1293         break;
1294 
1295     /*
1296      * ESB cache updates (not modeled)
1297      */
1298     case VC_ESBC_FLUSH_CTRL:
1299         xive->vc_regs[reg] &= ~VC_ESBC_FLUSH_CTRL_POLL_VALID;
1300         val = xive->vc_regs[reg];
1301         break;
1302 
1303     case VC_ESBC_CFG:
1304         val = xive->vc_regs[reg];
1305         break;
1306 
1307     /*
1308      * EAS cache updates (not modeled)
1309      */
1310     case VC_EASC_FLUSH_CTRL:
1311         xive->vc_regs[reg] &= ~VC_EASC_FLUSH_CTRL_POLL_VALID;
1312         val = xive->vc_regs[reg];
1313         break;
1314 
1315     case VC_ENDC_WATCH_ASSIGN:
1316         val = pnv_xive2_endc_cache_watch_assign(xive);
1317         break;
1318 
1319     case VC_ENDC_CFG:
1320         val = xive->vc_regs[reg];
1321         break;
1322 
1323     /*
1324      * END cache updates
1325      */
1326     case VC_ENDC_WATCH0_SPEC:
1327     case VC_ENDC_WATCH1_SPEC:
1328     case VC_ENDC_WATCH2_SPEC:
1329     case VC_ENDC_WATCH3_SPEC:
1330         watch_engine = (offset - VC_ENDC_WATCH0_SPEC) >> 6;
1331         xive->vc_regs[reg] &= ~(VC_ENDC_WATCH_FULL | VC_ENDC_WATCH_CONFLICT);
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 @%"HWADDR_PRIx, offset);
1499         return;
1500     }
1501 
1502     xive->vc_regs[reg] = val;
1503 }
1504 
1505 static const MemoryRegionOps pnv_xive2_ic_vc_ops = {
1506     .read = pnv_xive2_ic_vc_read,
1507     .write = pnv_xive2_ic_vc_write,
1508     .endianness = DEVICE_BIG_ENDIAN,
1509     .valid = {
1510         .min_access_size = 8,
1511         .max_access_size = 8,
1512     },
1513     .impl = {
1514         .min_access_size = 8,
1515         .max_access_size = 8,
1516     },
1517 };
1518 
1519 static uint8_t pnv_xive2_nxc_cache_watch_assign(PnvXive2 *xive)
1520 {
1521     uint64_t engine_mask = GETFIELD(PC_NXC_PROC_CONFIG_WATCH_ASSIGN,
1522                                     xive->pc_regs[PC_NXC_PROC_CONFIG >> 3]);
1523     uint64_t state = xive->pc_regs[PC_NXC_WATCH_ASSIGN >> 3];
1524     uint8_t val;
1525 
1526     /*
1527      * We keep track of which engines are currently busy in the
1528      * PC_NXC_WATCH_ASSIGN register directly. When the firmware reads
1529      * the register, we don't return its value but the ID of an engine
1530      * it can use.
1531      * There are 4 engines. 0xFF means no engine is available.
1532      */
1533     val = pnv_xive2_cache_watch_assign(engine_mask, &state);
1534     if (val != 0xFF) {
1535         xive->pc_regs[PC_NXC_WATCH_ASSIGN >> 3] = state;
1536     }
1537     return val;
1538 }
1539 
1540 static void pnv_xive2_nxc_cache_watch_release(PnvXive2 *xive,
1541                                               uint8_t watch_engine)
1542 {
1543     uint64_t state = xive->pc_regs[PC_NXC_WATCH_ASSIGN >> 3];
1544 
1545     pnv_xive2_cache_watch_release(&state, watch_engine);
1546     xive->pc_regs[PC_NXC_WATCH_ASSIGN >> 3] = state;
1547 }
1548 
1549 static uint64_t pnv_xive2_ic_pc_read(void *opaque, hwaddr offset,
1550                                      unsigned size)
1551 {
1552     PnvXive2 *xive = PNV_XIVE2(opaque);
1553     uint64_t val = -1;
1554     uint32_t reg = offset >> 3;
1555     uint8_t watch_engine;
1556 
1557     switch (offset) {
1558     /*
1559      * VSD table settings.
1560      */
1561     case PC_VSD_TABLE_ADDR:
1562     case PC_VSD_TABLE_DATA:
1563         val = xive->pc_regs[reg];
1564         break;
1565 
1566     case PC_NXC_WATCH_ASSIGN:
1567         val = pnv_xive2_nxc_cache_watch_assign(xive);
1568         break;
1569 
1570     case PC_NXC_PROC_CONFIG:
1571         val = xive->pc_regs[reg];
1572         break;
1573 
1574     /*
1575      * cache updates
1576      */
1577     case PC_NXC_WATCH0_SPEC:
1578     case PC_NXC_WATCH1_SPEC:
1579     case PC_NXC_WATCH2_SPEC:
1580     case PC_NXC_WATCH3_SPEC:
1581         watch_engine = (offset - PC_NXC_WATCH0_SPEC) >> 6;
1582         xive->pc_regs[reg] &= ~(PC_NXC_WATCH_FULL | PC_NXC_WATCH_CONFLICT);
1583         pnv_xive2_nxc_cache_watch_release(xive, watch_engine);
1584         val = xive->pc_regs[reg];
1585         break;
1586 
1587     case PC_NXC_WATCH0_DATA0:
1588     case PC_NXC_WATCH1_DATA0:
1589     case PC_NXC_WATCH2_DATA0:
1590     case PC_NXC_WATCH3_DATA0:
1591        /*
1592         * Load DATA registers from cache with data requested by the
1593         * SPEC register
1594         */
1595         watch_engine = (offset - PC_NXC_WATCH0_DATA0) >> 6;
1596         pnv_xive2_nxc_cache_load(xive, watch_engine);
1597         val = xive->pc_regs[reg];
1598         break;
1599 
1600     case PC_NXC_WATCH0_DATA1 ... PC_NXC_WATCH0_DATA3:
1601     case PC_NXC_WATCH1_DATA1 ... PC_NXC_WATCH1_DATA3:
1602     case PC_NXC_WATCH2_DATA1 ... PC_NXC_WATCH2_DATA3:
1603     case PC_NXC_WATCH3_DATA1 ... PC_NXC_WATCH3_DATA3:
1604         val = xive->pc_regs[reg];
1605         break;
1606 
1607     case PC_NXC_FLUSH_CTRL:
1608         xive->pc_regs[reg] &= ~PC_NXC_FLUSH_CTRL_POLL_VALID;
1609         val = xive->pc_regs[reg];
1610         break;
1611 
1612     /*
1613      * Indirect invalidation
1614      */
1615     case PC_AT_KILL:
1616         xive->pc_regs[reg] &= ~PC_AT_KILL_VALID;
1617         val = xive->pc_regs[reg];
1618         break;
1619 
1620     default:
1621         xive2_error(xive, "PC: invalid read @%"HWADDR_PRIx, offset);
1622     }
1623 
1624     return val;
1625 }
1626 
1627 static void pnv_xive2_pc_vst_set_data(PnvXive2 *xive, uint64_t vsd)
1628 {
1629     uint8_t type = GETFIELD(PC_VSD_TABLE_SELECT,
1630                             xive->pc_regs[PC_VSD_TABLE_ADDR >> 3]);
1631     uint8_t blk = GETFIELD(PC_VSD_TABLE_ADDRESS,
1632                            xive->pc_regs[PC_VSD_TABLE_ADDR >> 3]);
1633 
1634     pnv_xive2_vst_set_data(xive, vsd, type, blk);
1635 }
1636 
1637 static void pnv_xive2_ic_pc_write(void *opaque, hwaddr offset,
1638                                   uint64_t val, unsigned size)
1639 {
1640     PnvXive2 *xive = PNV_XIVE2(opaque);
1641     uint32_t reg = offset >> 3;
1642     uint8_t watch_engine;
1643 
1644     switch (offset) {
1645 
1646     /*
1647      * VSD table settings.
1648      * The Xive2Router model combines both VC and PC sub-engines. We
1649      * allow to configure the tables through both, for the rare cases
1650      * where a table only really needs to be configured for one of
1651      * them (e.g. the NVG table for the presenter). It assumes that
1652      * firmware passes the same address to the VC and PC when tables
1653      * are defined for both, which seems acceptable.
1654      */
1655     case PC_VSD_TABLE_ADDR:
1656         break;
1657     case PC_VSD_TABLE_DATA:
1658         pnv_xive2_pc_vst_set_data(xive, val);
1659         break;
1660 
1661     case PC_NXC_PROC_CONFIG:
1662         break;
1663 
1664     /*
1665      * cache updates
1666      */
1667     case PC_NXC_WATCH0_SPEC:
1668     case PC_NXC_WATCH1_SPEC:
1669     case PC_NXC_WATCH2_SPEC:
1670     case PC_NXC_WATCH3_SPEC:
1671         val &= ~PC_NXC_WATCH_CONFLICT; /* HW will set this bit */
1672         break;
1673 
1674     case PC_NXC_WATCH0_DATA1 ... PC_NXC_WATCH0_DATA3:
1675     case PC_NXC_WATCH1_DATA1 ... PC_NXC_WATCH1_DATA3:
1676     case PC_NXC_WATCH2_DATA1 ... PC_NXC_WATCH2_DATA3:
1677     case PC_NXC_WATCH3_DATA1 ... PC_NXC_WATCH3_DATA3:
1678         break;
1679     case PC_NXC_WATCH0_DATA0:
1680     case PC_NXC_WATCH1_DATA0:
1681     case PC_NXC_WATCH2_DATA0:
1682     case PC_NXC_WATCH3_DATA0:
1683         /* writing to DATA0 triggers the cache write */
1684         watch_engine = (offset - PC_NXC_WATCH0_DATA0) >> 6;
1685         xive->pc_regs[reg] = val;
1686         pnv_xive2_nxc_update(xive, watch_engine);
1687         break;
1688 
1689    /* case PC_NXC_FLUSH_CTRL: */
1690     case PC_NXC_FLUSH_POLL:
1691         xive->pc_regs[PC_NXC_FLUSH_CTRL >> 3] |= PC_NXC_FLUSH_CTRL_POLL_VALID;
1692         break;
1693 
1694     case PC_NXC_FLUSH_INJECT:
1695         pnv_xive2_inject_notify(xive, PNV_XIVE2_CACHE_NXC);
1696         break;
1697 
1698     /*
1699      * Indirect invalidation
1700      */
1701     case PC_AT_KILL:
1702     case PC_AT_KILL_MASK:
1703         break;
1704 
1705     default:
1706         xive2_error(xive, "PC: invalid write @%"HWADDR_PRIx, offset);
1707         return;
1708     }
1709 
1710     xive->pc_regs[reg] = val;
1711 }
1712 
1713 static const MemoryRegionOps pnv_xive2_ic_pc_ops = {
1714     .read = pnv_xive2_ic_pc_read,
1715     .write = pnv_xive2_ic_pc_write,
1716     .endianness = DEVICE_BIG_ENDIAN,
1717     .valid = {
1718         .min_access_size = 8,
1719         .max_access_size = 8,
1720     },
1721     .impl = {
1722         .min_access_size = 8,
1723         .max_access_size = 8,
1724     },
1725 };
1726 
1727 
1728 static uint64_t pnv_xive2_ic_tctxt_read(void *opaque, hwaddr offset,
1729                                         unsigned size)
1730 {
1731     PnvXive2 *xive = PNV_XIVE2(opaque);
1732     uint64_t val = -1;
1733     uint32_t reg = offset >> 3;
1734 
1735     switch (offset) {
1736     /*
1737      * XIVE2 hardware thread enablement
1738      */
1739     case TCTXT_EN0:
1740     case TCTXT_EN1:
1741         val = xive->tctxt_regs[reg];
1742         break;
1743 
1744     case TCTXT_EN0_SET:
1745     case TCTXT_EN0_RESET:
1746         val = xive->tctxt_regs[TCTXT_EN0 >> 3];
1747         break;
1748     case TCTXT_EN1_SET:
1749     case TCTXT_EN1_RESET:
1750         val = xive->tctxt_regs[TCTXT_EN1 >> 3];
1751         break;
1752     case TCTXT_CFG:
1753         val = xive->tctxt_regs[reg];
1754         break;
1755     default:
1756         xive2_error(xive, "TCTXT: invalid read @%"HWADDR_PRIx, offset);
1757     }
1758 
1759     return val;
1760 }
1761 
1762 static void pnv_xive2_ic_tctxt_write(void *opaque, hwaddr offset,
1763                                      uint64_t val, unsigned size)
1764 {
1765     PnvXive2 *xive = PNV_XIVE2(opaque);
1766     uint32_t reg = offset >> 3;
1767 
1768     switch (offset) {
1769     /*
1770      * XIVE2 hardware thread enablement
1771      */
1772     case TCTXT_EN0: /* Physical Thread Enable */
1773     case TCTXT_EN1: /* Physical Thread Enable (fused core) */
1774         xive->tctxt_regs[reg] = val;
1775         break;
1776 
1777     case TCTXT_EN0_SET:
1778         xive->tctxt_regs[TCTXT_EN0 >> 3] |= val;
1779         break;
1780     case TCTXT_EN1_SET:
1781         xive->tctxt_regs[TCTXT_EN1 >> 3] |= val;
1782         break;
1783     case TCTXT_EN0_RESET:
1784         xive->tctxt_regs[TCTXT_EN0 >> 3] &= ~val;
1785         break;
1786     case TCTXT_EN1_RESET:
1787         xive->tctxt_regs[TCTXT_EN1 >> 3] &= ~val;
1788         break;
1789     case TCTXT_CFG:
1790         xive->tctxt_regs[reg] = val;
1791         break;
1792     default:
1793         xive2_error(xive, "TCTXT: invalid write @%"HWADDR_PRIx, offset);
1794         return;
1795     }
1796 }
1797 
1798 static const MemoryRegionOps pnv_xive2_ic_tctxt_ops = {
1799     .read = pnv_xive2_ic_tctxt_read,
1800     .write = pnv_xive2_ic_tctxt_write,
1801     .endianness = DEVICE_BIG_ENDIAN,
1802     .valid = {
1803         .min_access_size = 8,
1804         .max_access_size = 8,
1805     },
1806     .impl = {
1807         .min_access_size = 8,
1808         .max_access_size = 8,
1809     },
1810 };
1811 
1812 /*
1813  * Redirect XSCOM to MMIO handlers
1814  */
1815 static uint64_t pnv_xive2_xscom_read(void *opaque, hwaddr offset,
1816                                      unsigned size)
1817 {
1818     PnvXive2 *xive = PNV_XIVE2(opaque);
1819     uint64_t val = -1;
1820     uint32_t xscom_reg = offset >> 3;
1821     uint32_t mmio_offset = (xscom_reg & 0xFF) << 3;
1822 
1823     switch (xscom_reg) {
1824     case 0x000 ... 0x0FF:
1825         val = pnv_xive2_ic_cq_read(opaque, mmio_offset, size);
1826         break;
1827     case 0x100 ... 0x1FF:
1828         val = pnv_xive2_ic_vc_read(opaque, mmio_offset, size);
1829         break;
1830     case 0x200 ... 0x2FF:
1831         val = pnv_xive2_ic_pc_read(opaque, mmio_offset, size);
1832         break;
1833     case 0x300 ... 0x3FF:
1834         val = pnv_xive2_ic_tctxt_read(opaque, mmio_offset, size);
1835         break;
1836     default:
1837         xive2_error(xive, "XSCOM: invalid read @%"HWADDR_PRIx, offset);
1838     }
1839 
1840     return val;
1841 }
1842 
1843 static void pnv_xive2_xscom_write(void *opaque, hwaddr offset,
1844                                   uint64_t val, unsigned size)
1845 {
1846     PnvXive2 *xive = PNV_XIVE2(opaque);
1847     uint32_t xscom_reg = offset >> 3;
1848     uint32_t mmio_offset = (xscom_reg & 0xFF) << 3;
1849 
1850     switch (xscom_reg) {
1851     case 0x000 ... 0x0FF:
1852         pnv_xive2_ic_cq_write(opaque, mmio_offset, val, size);
1853         break;
1854     case 0x100 ... 0x1FF:
1855         pnv_xive2_ic_vc_write(opaque, mmio_offset, val, size);
1856         break;
1857     case 0x200 ... 0x2FF:
1858         pnv_xive2_ic_pc_write(opaque, mmio_offset, val, size);
1859         break;
1860     case 0x300 ... 0x3FF:
1861         pnv_xive2_ic_tctxt_write(opaque, mmio_offset, val, size);
1862         break;
1863     default:
1864         xive2_error(xive, "XSCOM: invalid write @%"HWADDR_PRIx, offset);
1865     }
1866 }
1867 
1868 static const MemoryRegionOps pnv_xive2_xscom_ops = {
1869     .read = pnv_xive2_xscom_read,
1870     .write = pnv_xive2_xscom_write,
1871     .endianness = DEVICE_BIG_ENDIAN,
1872     .valid = {
1873         .min_access_size = 8,
1874         .max_access_size = 8,
1875     },
1876     .impl = {
1877         .min_access_size = 8,
1878         .max_access_size = 8,
1879     },
1880 };
1881 
1882 /*
1883  * Notify port page. The layout is compatible between 4K and 64K pages :
1884  *
1885  * Page 1           Notify page (writes only)
1886  *  0x000 - 0x7FF   IPI interrupt (NPU)
1887  *  0x800 - 0xFFF   HW interrupt triggers (PSI, PHB)
1888  */
1889 
1890 static void pnv_xive2_ic_hw_trigger(PnvXive2 *xive, hwaddr addr,
1891                                     uint64_t val)
1892 {
1893     uint8_t blk;
1894     uint32_t idx;
1895 
1896     if (val & XIVE_TRIGGER_END) {
1897         xive2_error(xive, "IC: END trigger at @0x%"HWADDR_PRIx" data 0x%"PRIx64,
1898                    addr, val);
1899         return;
1900     }
1901 
1902     /*
1903      * Forward the source event notification directly to the Router.
1904      * The source interrupt number should already be correctly encoded
1905      * with the chip block id by the sending device (PHB, PSI).
1906      */
1907     blk = XIVE_EAS_BLOCK(val);
1908     idx = XIVE_EAS_INDEX(val);
1909 
1910     xive2_router_notify(XIVE_NOTIFIER(xive), XIVE_EAS(blk, idx),
1911                          !!(val & XIVE_TRIGGER_PQ));
1912 }
1913 
1914 static void pnv_xive2_ic_notify_write(void *opaque, hwaddr offset,
1915                                       uint64_t val, unsigned size)
1916 {
1917     PnvXive2 *xive = PNV_XIVE2(opaque);
1918 
1919     /* VC: IPI triggers */
1920     switch (offset) {
1921     case 0x000 ... 0x7FF:
1922         /* TODO: check IPI notify sub-page routing */
1923         pnv_xive2_ic_hw_trigger(opaque, offset, val);
1924         break;
1925 
1926     /* VC: HW triggers */
1927     case 0x800 ... 0xFFF:
1928         pnv_xive2_ic_hw_trigger(opaque, offset, val);
1929         break;
1930 
1931     default:
1932         xive2_error(xive, "NOTIFY: invalid write @%"HWADDR_PRIx, offset);
1933     }
1934 }
1935 
1936 static uint64_t pnv_xive2_ic_notify_read(void *opaque, hwaddr offset,
1937                                          unsigned size)
1938 {
1939     PnvXive2 *xive = PNV_XIVE2(opaque);
1940 
1941    /* loads are invalid */
1942     xive2_error(xive, "NOTIFY: invalid read @%"HWADDR_PRIx, offset);
1943     return -1;
1944 }
1945 
1946 static const MemoryRegionOps pnv_xive2_ic_notify_ops = {
1947     .read = pnv_xive2_ic_notify_read,
1948     .write = pnv_xive2_ic_notify_write,
1949     .endianness = DEVICE_BIG_ENDIAN,
1950     .valid = {
1951         .min_access_size = 8,
1952         .max_access_size = 8,
1953     },
1954     .impl = {
1955         .min_access_size = 8,
1956         .max_access_size = 8,
1957     },
1958 };
1959 
1960 static uint64_t pnv_xive2_ic_lsi_read(void *opaque, hwaddr offset,
1961                                       unsigned size)
1962 {
1963     PnvXive2 *xive = PNV_XIVE2(opaque);
1964 
1965     xive2_error(xive, "LSI: invalid read @%"HWADDR_PRIx, offset);
1966     return -1;
1967 }
1968 
1969 static void pnv_xive2_ic_lsi_write(void *opaque, hwaddr offset,
1970                                    uint64_t val, unsigned size)
1971 {
1972     PnvXive2 *xive = PNV_XIVE2(opaque);
1973 
1974     xive2_error(xive, "LSI: invalid write @%"HWADDR_PRIx, offset);
1975 }
1976 
1977 static const MemoryRegionOps pnv_xive2_ic_lsi_ops = {
1978     .read = pnv_xive2_ic_lsi_read,
1979     .write = pnv_xive2_ic_lsi_write,
1980     .endianness = DEVICE_BIG_ENDIAN,
1981     .valid = {
1982         .min_access_size = 8,
1983         .max_access_size = 8,
1984     },
1985     .impl = {
1986         .min_access_size = 8,
1987         .max_access_size = 8,
1988     },
1989 };
1990 
1991 /*
1992  * Sync MMIO page (write only)
1993  */
1994 #define PNV_XIVE2_SYNC_IPI              0x000
1995 #define PNV_XIVE2_SYNC_HW               0x080
1996 #define PNV_XIVE2_SYNC_NxC              0x100
1997 #define PNV_XIVE2_SYNC_INT              0x180
1998 #define PNV_XIVE2_SYNC_OS_ESC           0x200
1999 #define PNV_XIVE2_SYNC_POOL_ESC         0x280
2000 #define PNV_XIVE2_SYNC_HARD_ESC         0x300
2001 #define PNV_XIVE2_SYNC_NXC_LD_LCL_NCO   0x800
2002 #define PNV_XIVE2_SYNC_NXC_LD_LCL_CO    0x880
2003 #define PNV_XIVE2_SYNC_NXC_ST_LCL_NCI   0x900
2004 #define PNV_XIVE2_SYNC_NXC_ST_LCL_CI    0x980
2005 #define PNV_XIVE2_SYNC_NXC_ST_RMT_NCI   0xA00
2006 #define PNV_XIVE2_SYNC_NXC_ST_RMT_CI    0xA80
2007 
2008 static uint64_t pnv_xive2_ic_sync_read(void *opaque, hwaddr offset,
2009                                        unsigned size)
2010 {
2011     PnvXive2 *xive = PNV_XIVE2(opaque);
2012 
2013     /* loads are invalid */
2014     xive2_error(xive, "SYNC: invalid read @%"HWADDR_PRIx, offset);
2015     return -1;
2016 }
2017 
2018 /*
2019  * The sync MMIO space spans two pages.  The lower page is use for
2020  * queue sync "poll" requests while the upper page is used for queue
2021  * sync "inject" requests.  Inject requests require the HW to write
2022  * a byte of all 1's to a predetermined location in memory in order
2023  * to signal completion of the request.  Both pages have the same
2024  * layout, so it is easiest to handle both with a single function.
2025  */
2026 static void pnv_xive2_ic_sync_write(void *opaque, hwaddr offset,
2027                                     uint64_t val, unsigned size)
2028 {
2029     PnvXive2 *xive = PNV_XIVE2(opaque);
2030     int inject_type;
2031     hwaddr pg_offset_mask = (1ull << xive->ic_shift) - 1;
2032 
2033     /* adjust offset for inject page */
2034     hwaddr adj_offset = offset & pg_offset_mask;
2035 
2036     switch (adj_offset) {
2037     case PNV_XIVE2_SYNC_IPI:
2038         inject_type = PNV_XIVE2_QUEUE_IPI;
2039         break;
2040     case PNV_XIVE2_SYNC_HW:
2041         inject_type = PNV_XIVE2_QUEUE_HW;
2042         break;
2043     case PNV_XIVE2_SYNC_NxC:
2044         inject_type = PNV_XIVE2_QUEUE_NXC;
2045         break;
2046     case PNV_XIVE2_SYNC_INT:
2047         inject_type = PNV_XIVE2_QUEUE_INT;
2048         break;
2049     case PNV_XIVE2_SYNC_OS_ESC:
2050         inject_type = PNV_XIVE2_QUEUE_OS;
2051         break;
2052     case PNV_XIVE2_SYNC_POOL_ESC:
2053         inject_type = PNV_XIVE2_QUEUE_POOL;
2054         break;
2055     case PNV_XIVE2_SYNC_HARD_ESC:
2056         inject_type = PNV_XIVE2_QUEUE_HARD;
2057         break;
2058     case PNV_XIVE2_SYNC_NXC_LD_LCL_NCO:
2059         inject_type = PNV_XIVE2_QUEUE_NXC_LD_LCL_NCO;
2060         break;
2061     case PNV_XIVE2_SYNC_NXC_LD_LCL_CO:
2062         inject_type = PNV_XIVE2_QUEUE_NXC_LD_LCL_CO;
2063         break;
2064     case PNV_XIVE2_SYNC_NXC_ST_LCL_NCI:
2065         inject_type = PNV_XIVE2_QUEUE_NXC_ST_LCL_NCI;
2066         break;
2067     case PNV_XIVE2_SYNC_NXC_ST_LCL_CI:
2068         inject_type = PNV_XIVE2_QUEUE_NXC_ST_LCL_CI;
2069         break;
2070     case PNV_XIVE2_SYNC_NXC_ST_RMT_NCI:
2071         inject_type = PNV_XIVE2_QUEUE_NXC_ST_RMT_NCI;
2072         break;
2073     case PNV_XIVE2_SYNC_NXC_ST_RMT_CI:
2074         inject_type = PNV_XIVE2_QUEUE_NXC_ST_RMT_CI;
2075         break;
2076     default:
2077         xive2_error(xive, "SYNC: invalid write @%"HWADDR_PRIx, offset);
2078         return;
2079     }
2080 
2081     /* Write Queue Sync notification byte if writing to sync inject page */
2082     if ((offset & ~pg_offset_mask) != 0) {
2083         pnv_xive2_inject_notify(xive, inject_type);
2084     }
2085 }
2086 
2087 static const MemoryRegionOps pnv_xive2_ic_sync_ops = {
2088     .read = pnv_xive2_ic_sync_read,
2089     .write = pnv_xive2_ic_sync_write,
2090     .endianness = DEVICE_BIG_ENDIAN,
2091     .valid = {
2092         .min_access_size = 8,
2093         .max_access_size = 8,
2094     },
2095     .impl = {
2096         .min_access_size = 8,
2097         .max_access_size = 8,
2098     },
2099 };
2100 
2101 /*
2102  * When the TM direct pages of the IC controller are accessed, the
2103  * target HW thread is deduced from the page offset.
2104  */
2105 static uint32_t pnv_xive2_ic_tm_get_pir(PnvXive2 *xive, hwaddr offset)
2106 {
2107     /* On P10, the node ID shift in the PIR register is 8 bits */
2108     return xive->chip->chip_id << 8 | offset >> xive->ic_shift;
2109 }
2110 
2111 static uint32_t pnv_xive2_ic_tm_get_hw_page_offset(PnvXive2 *xive,
2112                                                    hwaddr offset)
2113 {
2114     /*
2115      * Indirect TIMA accesses are similar to direct accesses for
2116      * privilege ring 0. So remove any traces of the hw thread ID from
2117      * the offset in the IC BAR as it could be interpreted as the ring
2118      * privilege when calling the underlying direct access functions.
2119      */
2120     return offset & ((1ull << xive->ic_shift) - 1);
2121 }
2122 
2123 static XiveTCTX *pnv_xive2_get_indirect_tctx(PnvXive2 *xive, uint32_t pir)
2124 {
2125     PnvChip *chip = xive->chip;
2126     PowerPCCPU *cpu = NULL;
2127 
2128     cpu = pnv_chip_find_cpu(chip, pir);
2129     if (!cpu) {
2130         xive2_error(xive, "IC: invalid PIR %x for indirect access", pir);
2131         return NULL;
2132     }
2133 
2134     if (!pnv_xive2_is_cpu_enabled(xive, cpu)) {
2135         xive2_error(xive, "IC: CPU %x is not enabled", pir);
2136     }
2137 
2138     return XIVE_TCTX(pnv_cpu_state(cpu)->intc);
2139 }
2140 
2141 static uint64_t pnv_xive2_ic_tm_indirect_read(void *opaque, hwaddr offset,
2142                                               unsigned size)
2143 {
2144     PnvXive2 *xive = PNV_XIVE2(opaque);
2145     XivePresenter *xptr = XIVE_PRESENTER(xive);
2146     hwaddr hw_page_offset;
2147     uint32_t pir;
2148     XiveTCTX *tctx;
2149     uint64_t val = -1;
2150 
2151     pir = pnv_xive2_ic_tm_get_pir(xive, offset);
2152     hw_page_offset = pnv_xive2_ic_tm_get_hw_page_offset(xive, offset);
2153     tctx = pnv_xive2_get_indirect_tctx(xive, pir);
2154     if (tctx) {
2155         val = xive_tctx_tm_read(xptr, tctx, hw_page_offset, size);
2156     }
2157 
2158     return val;
2159 }
2160 
2161 static void pnv_xive2_ic_tm_indirect_write(void *opaque, hwaddr offset,
2162                                            uint64_t val, unsigned size)
2163 {
2164     PnvXive2 *xive = PNV_XIVE2(opaque);
2165     XivePresenter *xptr = XIVE_PRESENTER(xive);
2166     hwaddr hw_page_offset;
2167     uint32_t pir;
2168     XiveTCTX *tctx;
2169 
2170     pir = pnv_xive2_ic_tm_get_pir(xive, offset);
2171     hw_page_offset = pnv_xive2_ic_tm_get_hw_page_offset(xive, offset);
2172     tctx = pnv_xive2_get_indirect_tctx(xive, pir);
2173     if (tctx) {
2174         xive_tctx_tm_write(xptr, tctx, hw_page_offset, val, size);
2175     }
2176 }
2177 
2178 static const MemoryRegionOps pnv_xive2_ic_tm_indirect_ops = {
2179     .read = pnv_xive2_ic_tm_indirect_read,
2180     .write = pnv_xive2_ic_tm_indirect_write,
2181     .endianness = DEVICE_BIG_ENDIAN,
2182     .valid = {
2183         .min_access_size = 1,
2184         .max_access_size = 8,
2185     },
2186     .impl = {
2187         .min_access_size = 1,
2188         .max_access_size = 8,
2189     },
2190 };
2191 
2192 /*
2193  * TIMA ops
2194  */
2195 static void pnv_xive2_tm_write(void *opaque, hwaddr offset,
2196                                uint64_t value, unsigned size)
2197 {
2198     PowerPCCPU *cpu = POWERPC_CPU(current_cpu);
2199     PnvXive2 *xive = pnv_xive2_tm_get_xive(cpu);
2200     XiveTCTX *tctx = XIVE_TCTX(pnv_cpu_state(cpu)->intc);
2201     XivePresenter *xptr = XIVE_PRESENTER(xive);
2202 
2203     xive_tctx_tm_write(xptr, tctx, offset, value, size);
2204 }
2205 
2206 static uint64_t pnv_xive2_tm_read(void *opaque, hwaddr offset, unsigned size)
2207 {
2208     PowerPCCPU *cpu = POWERPC_CPU(current_cpu);
2209     PnvXive2 *xive = pnv_xive2_tm_get_xive(cpu);
2210     XiveTCTX *tctx = XIVE_TCTX(pnv_cpu_state(cpu)->intc);
2211     XivePresenter *xptr = XIVE_PRESENTER(xive);
2212 
2213     return xive_tctx_tm_read(xptr, tctx, offset, size);
2214 }
2215 
2216 static const MemoryRegionOps pnv_xive2_tm_ops = {
2217     .read = pnv_xive2_tm_read,
2218     .write = pnv_xive2_tm_write,
2219     .endianness = DEVICE_BIG_ENDIAN,
2220     .valid = {
2221         .min_access_size = 1,
2222         .max_access_size = 8,
2223     },
2224     .impl = {
2225         .min_access_size = 1,
2226         .max_access_size = 8,
2227     },
2228 };
2229 
2230 static uint64_t pnv_xive2_nvc_read(void *opaque, hwaddr addr,
2231                                    unsigned size)
2232 {
2233     PnvXive2 *xive = PNV_XIVE2(opaque);
2234     XivePresenter *xptr = XIVE_PRESENTER(xive);
2235     uint32_t page = addr >> xive->nvpg_shift;
2236     uint16_t op = addr & 0xFFF;
2237     uint8_t blk = pnv_xive2_block_id(xive);
2238 
2239     if (size != 2) {
2240         qemu_log_mask(LOG_GUEST_ERROR, "XIVE: invalid nvc load size %d\n",
2241                       size);
2242         return -1;
2243     }
2244 
2245     return xive2_presenter_nvgc_backlog_op(xptr, true, blk, page, op, 1);
2246 }
2247 
2248 static void pnv_xive2_nvc_write(void *opaque, hwaddr addr,
2249                                 uint64_t val, unsigned size)
2250 {
2251     PnvXive2 *xive = PNV_XIVE2(opaque);
2252     XivePresenter *xptr = XIVE_PRESENTER(xive);
2253     uint32_t page = addr >> xive->nvc_shift;
2254     uint16_t op = addr & 0xFFF;
2255     uint8_t blk = pnv_xive2_block_id(xive);
2256 
2257     if (size != 1) {
2258         qemu_log_mask(LOG_GUEST_ERROR, "XIVE: invalid nvc write size %d\n",
2259                       size);
2260         return;
2261     }
2262 
2263     (void)xive2_presenter_nvgc_backlog_op(xptr, true, blk, page, op, val);
2264 }
2265 
2266 static const MemoryRegionOps pnv_xive2_nvc_ops = {
2267     .read = pnv_xive2_nvc_read,
2268     .write = pnv_xive2_nvc_write,
2269     .endianness = DEVICE_BIG_ENDIAN,
2270     .valid = {
2271         .min_access_size = 1,
2272         .max_access_size = 8,
2273     },
2274     .impl = {
2275         .min_access_size = 1,
2276         .max_access_size = 8,
2277     },
2278 };
2279 
2280 static uint64_t pnv_xive2_nvpg_read(void *opaque, hwaddr addr,
2281                                     unsigned size)
2282 {
2283     PnvXive2 *xive = PNV_XIVE2(opaque);
2284     XivePresenter *xptr = XIVE_PRESENTER(xive);
2285     uint32_t page = addr >> xive->nvpg_shift;
2286     uint16_t op = addr & 0xFFF;
2287     uint32_t index = page >> 1;
2288     uint8_t blk = pnv_xive2_block_id(xive);
2289 
2290     if (size != 2) {
2291         qemu_log_mask(LOG_GUEST_ERROR, "XIVE: invalid nvpg load size %d\n",
2292                       size);
2293         return -1;
2294     }
2295 
2296     if (page % 2) {
2297         /* odd page - NVG */
2298         return xive2_presenter_nvgc_backlog_op(xptr, false, blk, index, op, 1);
2299     } else {
2300         /* even page - NVP */
2301         return xive2_presenter_nvp_backlog_op(xptr, blk, index, op);
2302     }
2303 }
2304 
2305 static void pnv_xive2_nvpg_write(void *opaque, hwaddr addr,
2306                                  uint64_t val, unsigned size)
2307 {
2308     PnvXive2 *xive = PNV_XIVE2(opaque);
2309     XivePresenter *xptr = XIVE_PRESENTER(xive);
2310     uint32_t page = addr >> xive->nvpg_shift;
2311     uint16_t op = addr & 0xFFF;
2312     uint32_t index = page >> 1;
2313     uint8_t blk = pnv_xive2_block_id(xive);
2314 
2315     if (size != 1) {
2316         qemu_log_mask(LOG_GUEST_ERROR, "XIVE: invalid nvpg write size %d\n",
2317                       size);
2318         return;
2319     }
2320 
2321     if (page % 2) {
2322         /* odd page - NVG */
2323         (void)xive2_presenter_nvgc_backlog_op(xptr, false, blk, index, op, val);
2324     } else {
2325         /* even page - NVP */
2326         (void)xive2_presenter_nvp_backlog_op(xptr, blk, index, op);
2327     }
2328 }
2329 
2330 static const MemoryRegionOps pnv_xive2_nvpg_ops = {
2331     .read = pnv_xive2_nvpg_read,
2332     .write = pnv_xive2_nvpg_write,
2333     .endianness = DEVICE_BIG_ENDIAN,
2334     .valid = {
2335         .min_access_size = 1,
2336         .max_access_size = 8,
2337     },
2338     .impl = {
2339         .min_access_size = 1,
2340         .max_access_size = 8,
2341     },
2342 };
2343 
2344 /*
2345  * POWER10 default capabilities: 0x2000120076f000FC
2346  */
2347 #define PNV_XIVE2_CAPABILITIES  0x2000120076f000FC
2348 
2349 /*
2350  * POWER10 default configuration: 0x0030000033000000
2351  *
2352  * 8bits thread id was dropped for P10
2353  */
2354 #define PNV_XIVE2_CONFIGURATION 0x0030000033000000
2355 
2356 static void pnv_xive2_reset(void *dev)
2357 {
2358     PnvXive2 *xive = PNV_XIVE2(dev);
2359     XiveSource *xsrc = &xive->ipi_source;
2360     Xive2EndSource *end_xsrc = &xive->end_source;
2361 
2362     xive->cq_regs[CQ_XIVE_CAP >> 3] = xive->capabilities;
2363     xive->cq_regs[CQ_XIVE_CFG >> 3] = xive->config;
2364 
2365     /* HW hardwires the #Topology of the chip in the block field */
2366     xive->cq_regs[CQ_XIVE_CFG >> 3] |=
2367         SETFIELD(CQ_XIVE_CFG_HYP_HARD_BLOCK_ID, 0ull, xive->chip->chip_id);
2368 
2369     /* VC and PC cache watch assign mechanism */
2370     xive->vc_regs[VC_ENDC_CFG >> 3] =
2371         SETFIELD(VC_ENDC_CFG_CACHE_WATCH_ASSIGN, 0ull, 0b0111);
2372     xive->pc_regs[PC_NXC_PROC_CONFIG >> 3] =
2373         SETFIELD(PC_NXC_PROC_CONFIG_WATCH_ASSIGN, 0ull, 0b0111);
2374 
2375     /* Set default page size to 64k */
2376     xive->ic_shift = xive->esb_shift = xive->end_shift = 16;
2377     xive->nvc_shift = xive->nvpg_shift = xive->tm_shift = 16;
2378 
2379     /* Clear source MMIOs */
2380     if (memory_region_is_mapped(&xsrc->esb_mmio)) {
2381         memory_region_del_subregion(&xive->esb_mmio, &xsrc->esb_mmio);
2382     }
2383 
2384     if (memory_region_is_mapped(&end_xsrc->esb_mmio)) {
2385         memory_region_del_subregion(&xive->end_mmio, &end_xsrc->esb_mmio);
2386     }
2387 }
2388 
2389 /*
2390  *  Maximum number of IRQs and ENDs supported by HW. Will be tuned by
2391  *  software.
2392  */
2393 #define PNV_XIVE2_NR_IRQS (PNV10_XIVE2_ESB_SIZE / (1ull << XIVE_ESB_64K_2PAGE))
2394 #define PNV_XIVE2_NR_ENDS (PNV10_XIVE2_END_SIZE / (1ull << XIVE_ESB_64K_2PAGE))
2395 
2396 static void pnv_xive2_realize(DeviceState *dev, Error **errp)
2397 {
2398     PnvXive2 *xive = PNV_XIVE2(dev);
2399     PnvXive2Class *pxc = PNV_XIVE2_GET_CLASS(dev);
2400     XiveSource *xsrc = &xive->ipi_source;
2401     Xive2EndSource *end_xsrc = &xive->end_source;
2402     Error *local_err = NULL;
2403     int i;
2404 
2405     pxc->parent_realize(dev, &local_err);
2406     if (local_err) {
2407         error_propagate(errp, local_err);
2408         return;
2409     }
2410 
2411     assert(xive->chip);
2412 
2413     /*
2414      * The XiveSource and Xive2EndSource objects are realized with the
2415      * maximum allowed HW configuration. The ESB MMIO regions will be
2416      * resized dynamically when the controller is configured by the FW
2417      * to limit accesses to resources not provisioned.
2418      */
2419     object_property_set_int(OBJECT(xsrc), "flags", XIVE_SRC_STORE_EOI,
2420                             &error_fatal);
2421     object_property_set_int(OBJECT(xsrc), "nr-irqs", PNV_XIVE2_NR_IRQS,
2422                             &error_fatal);
2423     object_property_set_link(OBJECT(xsrc), "xive", OBJECT(xive),
2424                              &error_fatal);
2425     qdev_realize(DEVICE(xsrc), NULL, &local_err);
2426     if (local_err) {
2427         error_propagate(errp, local_err);
2428         return;
2429     }
2430 
2431     object_property_set_int(OBJECT(end_xsrc), "nr-ends", PNV_XIVE2_NR_ENDS,
2432                             &error_fatal);
2433     object_property_set_link(OBJECT(end_xsrc), "xive", OBJECT(xive),
2434                              &error_abort);
2435     qdev_realize(DEVICE(end_xsrc), NULL, &local_err);
2436     if (local_err) {
2437         error_propagate(errp, local_err);
2438         return;
2439     }
2440 
2441     /* XSCOM region, used for initial configuration of the BARs */
2442     memory_region_init_io(&xive->xscom_regs, OBJECT(dev),
2443                           &pnv_xive2_xscom_ops, xive, "xscom-xive",
2444                           PNV10_XSCOM_XIVE2_SIZE << 3);
2445 
2446     /* Interrupt controller MMIO regions */
2447     xive->ic_shift = 16;
2448     memory_region_init(&xive->ic_mmio, OBJECT(dev), "xive-ic",
2449                        PNV10_XIVE2_IC_SIZE);
2450 
2451     for (i = 0; i < ARRAY_SIZE(xive->ic_mmios); i++) {
2452         memory_region_init_io(&xive->ic_mmios[i], OBJECT(dev),
2453                          pnv_xive2_ic_regions[i].ops, xive,
2454                          pnv_xive2_ic_regions[i].name,
2455                          pnv_xive2_ic_regions[i].pgsize << xive->ic_shift);
2456     }
2457 
2458     /*
2459      * VC MMIO regions.
2460      */
2461     xive->esb_shift = 16;
2462     xive->end_shift = 16;
2463     memory_region_init(&xive->esb_mmio, OBJECT(xive), "xive-esb",
2464                        PNV10_XIVE2_ESB_SIZE);
2465     memory_region_init(&xive->end_mmio, OBJECT(xive), "xive-end",
2466                        PNV10_XIVE2_END_SIZE);
2467 
2468     /* Presenter Controller MMIO region (not modeled) */
2469     xive->nvc_shift = 16;
2470     xive->nvpg_shift = 16;
2471     memory_region_init_io(&xive->nvc_mmio, OBJECT(dev),
2472                           &pnv_xive2_nvc_ops, xive,
2473                           "xive-nvc", PNV10_XIVE2_NVC_SIZE);
2474 
2475     memory_region_init_io(&xive->nvpg_mmio, OBJECT(dev),
2476                           &pnv_xive2_nvpg_ops, xive,
2477                           "xive-nvpg", PNV10_XIVE2_NVPG_SIZE);
2478 
2479     /* Thread Interrupt Management Area (Direct) */
2480     xive->tm_shift = 16;
2481     memory_region_init_io(&xive->tm_mmio, OBJECT(dev), &pnv_xive2_tm_ops,
2482                           xive, "xive-tima", PNV10_XIVE2_TM_SIZE);
2483 
2484     qemu_register_reset(pnv_xive2_reset, dev);
2485 }
2486 
2487 static const Property pnv_xive2_properties[] = {
2488     DEFINE_PROP_UINT64("ic-bar", PnvXive2, ic_base, 0),
2489     DEFINE_PROP_UINT64("esb-bar", PnvXive2, esb_base, 0),
2490     DEFINE_PROP_UINT64("end-bar", PnvXive2, end_base, 0),
2491     DEFINE_PROP_UINT64("nvc-bar", PnvXive2, nvc_base, 0),
2492     DEFINE_PROP_UINT64("nvpg-bar", PnvXive2, nvpg_base, 0),
2493     DEFINE_PROP_UINT64("tm-bar", PnvXive2, tm_base, 0),
2494     DEFINE_PROP_UINT64("capabilities", PnvXive2, capabilities,
2495                        PNV_XIVE2_CAPABILITIES),
2496     DEFINE_PROP_UINT64("config", PnvXive2, config,
2497                        PNV_XIVE2_CONFIGURATION),
2498     DEFINE_PROP_LINK("chip", PnvXive2, chip, TYPE_PNV_CHIP, PnvChip *),
2499 };
2500 
2501 static void pnv_xive2_instance_init(Object *obj)
2502 {
2503     PnvXive2 *xive = PNV_XIVE2(obj);
2504 
2505     object_initialize_child(obj, "ipi_source", &xive->ipi_source,
2506                             TYPE_XIVE_SOURCE);
2507     object_initialize_child(obj, "end_source", &xive->end_source,
2508                             TYPE_XIVE2_END_SOURCE);
2509 }
2510 
2511 static int pnv_xive2_dt_xscom(PnvXScomInterface *dev, void *fdt,
2512                               int xscom_offset)
2513 {
2514     const char compat_p10[] = "ibm,power10-xive-x";
2515     char *name;
2516     int offset;
2517     uint32_t reg[] = {
2518         cpu_to_be32(PNV10_XSCOM_XIVE2_BASE),
2519         cpu_to_be32(PNV10_XSCOM_XIVE2_SIZE)
2520     };
2521 
2522     name = g_strdup_printf("xive@%x", PNV10_XSCOM_XIVE2_BASE);
2523     offset = fdt_add_subnode(fdt, xscom_offset, name);
2524     _FDT(offset);
2525     g_free(name);
2526 
2527     _FDT((fdt_setprop(fdt, offset, "reg", reg, sizeof(reg))));
2528     _FDT(fdt_setprop(fdt, offset, "compatible", compat_p10,
2529                      sizeof(compat_p10)));
2530     return 0;
2531 }
2532 
2533 static void pnv_xive2_class_init(ObjectClass *klass, const void *data)
2534 {
2535     DeviceClass *dc = DEVICE_CLASS(klass);
2536     PnvXScomInterfaceClass *xdc = PNV_XSCOM_INTERFACE_CLASS(klass);
2537     Xive2RouterClass *xrc = XIVE2_ROUTER_CLASS(klass);
2538     XiveNotifierClass *xnc = XIVE_NOTIFIER_CLASS(klass);
2539     XivePresenterClass *xpc = XIVE_PRESENTER_CLASS(klass);
2540     PnvXive2Class *pxc = PNV_XIVE2_CLASS(klass);
2541 
2542     xdc->dt_xscom  = pnv_xive2_dt_xscom;
2543 
2544     dc->desc       = "PowerNV XIVE2 Interrupt Controller (POWER10)";
2545     device_class_set_parent_realize(dc, pnv_xive2_realize,
2546                                     &pxc->parent_realize);
2547     device_class_set_props(dc, pnv_xive2_properties);
2548 
2549     xrc->get_eas   = pnv_xive2_get_eas;
2550     xrc->get_pq    = pnv_xive2_get_pq;
2551     xrc->set_pq    = pnv_xive2_set_pq;
2552     xrc->get_end   = pnv_xive2_get_end;
2553     xrc->write_end = pnv_xive2_write_end;
2554     xrc->get_nvp   = pnv_xive2_get_nvp;
2555     xrc->write_nvp = pnv_xive2_write_nvp;
2556     xrc->get_nvgc   = pnv_xive2_get_nvgc;
2557     xrc->write_nvgc = pnv_xive2_write_nvgc;
2558     xrc->get_config  = pnv_xive2_get_config;
2559     xrc->get_block_id = pnv_xive2_get_block_id;
2560 
2561     xnc->notify    = pnv_xive2_notify;
2562 
2563     xpc->match_nvt  = pnv_xive2_match_nvt;
2564     xpc->get_config = pnv_xive2_presenter_get_config;
2565     xpc->broadcast  = pnv_xive2_broadcast;
2566 };
2567 
2568 static const TypeInfo pnv_xive2_info = {
2569     .name          = TYPE_PNV_XIVE2,
2570     .parent        = TYPE_XIVE2_ROUTER,
2571     .instance_init = pnv_xive2_instance_init,
2572     .instance_size = sizeof(PnvXive2),
2573     .class_init    = pnv_xive2_class_init,
2574     .class_size    = sizeof(PnvXive2Class),
2575     .interfaces    = (const InterfaceInfo[]) {
2576         { TYPE_PNV_XSCOM_INTERFACE },
2577         { }
2578     }
2579 };
2580 
2581 static void pnv_xive2_register_types(void)
2582 {
2583     type_register_static(&pnv_xive2_info);
2584 }
2585 
2586 type_init(pnv_xive2_register_types)
2587 
2588 /*
2589  * If the table is direct, we can compute the number of PQ entries
2590  * provisioned by FW.
2591  */
2592 static uint32_t pnv_xive2_nr_esbs(PnvXive2 *xive)
2593 {
2594     uint8_t blk = pnv_xive2_block_id(xive);
2595     uint64_t vsd = xive->vsds[VST_ESB][blk];
2596     uint64_t vst_tsize = 1ull << (GETFIELD(VSD_TSIZE, vsd) + 12);
2597 
2598     return VSD_INDIRECT & vsd ? 0 : vst_tsize * SBE_PER_BYTE;
2599 }
2600 
2601 /*
2602  * Compute the number of entries per indirect subpage.
2603  */
2604 static uint64_t pnv_xive2_vst_per_subpage(PnvXive2 *xive, uint32_t type)
2605 {
2606     uint8_t blk = pnv_xive2_block_id(xive);
2607     uint64_t vsd = xive->vsds[type][blk];
2608     const XiveVstInfo *info = &vst_infos[type];
2609     uint64_t vsd_addr;
2610     uint32_t page_shift;
2611 
2612     /* For direct tables, fake a valid value */
2613     if (!(VSD_INDIRECT & vsd)) {
2614         return 1;
2615     }
2616 
2617     /* Get the page size of the indirect table. */
2618     vsd_addr = vsd & VSD_ADDRESS_MASK;
2619     ldq_be_dma(&address_space_memory, vsd_addr, &vsd, MEMTXATTRS_UNSPECIFIED);
2620 
2621     if (!(vsd & VSD_ADDRESS_MASK)) {
2622 #ifdef XIVE2_DEBUG
2623         xive2_error(xive, "VST: invalid %s entry!?", info->name);
2624 #endif
2625         return 0;
2626     }
2627 
2628     page_shift = GETFIELD(VSD_TSIZE, vsd) + 12;
2629 
2630     if (!pnv_xive2_vst_page_size_allowed(page_shift)) {
2631         xive2_error(xive, "VST: invalid %s page shift %d", info->name,
2632                    page_shift);
2633         return 0;
2634     }
2635 
2636     return (1ull << page_shift) / info->size;
2637 }
2638 
2639 void pnv_xive2_pic_print_info(PnvXive2 *xive, GString *buf)
2640 {
2641     Xive2Router *xrtr = XIVE2_ROUTER(xive);
2642     uint8_t blk = pnv_xive2_block_id(xive);
2643     uint8_t chip_id = xive->chip->chip_id;
2644     uint32_t srcno0 = XIVE_EAS(blk, 0);
2645     uint32_t nr_esbs = pnv_xive2_nr_esbs(xive);
2646     Xive2Eas eas;
2647     Xive2End end;
2648     Xive2Nvp nvp;
2649     Xive2Nvgc nvgc;
2650     int i;
2651     uint64_t entries_per_subpage;
2652 
2653     g_string_append_printf(buf, "XIVE[%x] Source %08x .. %08x\n",
2654                            blk, srcno0, srcno0 + nr_esbs - 1);
2655     xive_source_pic_print_info(&xive->ipi_source, srcno0, buf);
2656 
2657     g_string_append_printf(buf, "XIVE[%x] EAT %08x .. %08x\n",
2658                            blk, srcno0, srcno0 + nr_esbs - 1);
2659     for (i = 0; i < nr_esbs; i++) {
2660         if (xive2_router_get_eas(xrtr, blk, i, &eas)) {
2661             break;
2662         }
2663         if (!xive2_eas_is_masked(&eas)) {
2664             xive2_eas_pic_print_info(&eas, i, buf);
2665         }
2666     }
2667 
2668     g_string_append_printf(buf, "XIVE[%x] #%d END Escalation EAT\n",
2669                            chip_id, blk);
2670     i = 0;
2671     while (!xive2_router_get_end(xrtr, blk, i, &end)) {
2672         xive2_end_eas_pic_print_info(&end, i++, buf);
2673     }
2674 
2675     g_string_append_printf(buf, "XIVE[%x] #%d ENDT\n", chip_id, blk);
2676     i = 0;
2677     while (!xive2_router_get_end(xrtr, blk, i, &end)) {
2678         xive2_end_pic_print_info(&end, i++, buf);
2679     }
2680 
2681     g_string_append_printf(buf, "XIVE[%x] #%d NVPT %08x .. %08x\n",
2682                            chip_id, blk, 0, XIVE2_NVP_COUNT - 1);
2683     entries_per_subpage = pnv_xive2_vst_per_subpage(xive, VST_NVP);
2684     for (i = 0; i < XIVE2_NVP_COUNT; i += entries_per_subpage) {
2685         while (!xive2_router_get_nvp(xrtr, blk, i, &nvp)) {
2686             xive2_nvp_pic_print_info(&nvp, i++, buf);
2687         }
2688     }
2689 
2690     g_string_append_printf(buf, "XIVE[%x] #%d NVGT %08x .. %08x\n",
2691                            chip_id, blk, 0, XIVE2_NVP_COUNT - 1);
2692     entries_per_subpage = pnv_xive2_vst_per_subpage(xive, VST_NVG);
2693     for (i = 0; i < XIVE2_NVP_COUNT; i += entries_per_subpage) {
2694         while (!xive2_router_get_nvgc(xrtr, false, blk, i, &nvgc)) {
2695             xive2_nvgc_pic_print_info(&nvgc, i++, buf);
2696         }
2697     }
2698 
2699     g_string_append_printf(buf, "XIVE[%x] #%d NVCT %08x .. %08x\n",
2700                           chip_id, blk, 0, XIVE2_NVP_COUNT - 1);
2701     entries_per_subpage = pnv_xive2_vst_per_subpage(xive, VST_NVC);
2702     for (i = 0; i < XIVE2_NVP_COUNT; i += entries_per_subpage) {
2703         while (!xive2_router_get_nvgc(xrtr, true, blk, i, &nvgc)) {
2704             xive2_nvgc_pic_print_info(&nvgc, i++, buf);
2705         }
2706     }
2707 }
2708