]> Git Repo - qemu.git/blame - hw/intc/xive.c
ppc4xx: Move common dependency on serial to common option
[qemu.git] / hw / intc / xive.c
CommitLineData
02e3ff54
CLG
1/*
2 * QEMU PowerPC XIVE interrupt controller model
3 *
4 * Copyright (c) 2017-2018, IBM Corporation.
5 *
6 * This code is licensed under the GPL version 2 or later. See the
7 * COPYING file in the top-level directory.
8 */
9
10#include "qemu/osdep.h"
11#include "qemu/log.h"
0b8fa32f 12#include "qemu/module.h"
02e3ff54
CLG
13#include "qapi/error.h"
14#include "target/ppc/cpu.h"
15#include "sysemu/cpus.h"
16#include "sysemu/dma.h"
71e8a915 17#include "sysemu/reset.h"
02e3ff54 18#include "hw/qdev-properties.h"
d6454270 19#include "migration/vmstate.h"
02e3ff54 20#include "monitor/monitor.h"
64552b6b 21#include "hw/irq.h"
02e3ff54 22#include "hw/ppc/xive.h"
207d9fe9 23#include "hw/ppc/xive_regs.h"
4e960974 24#include "trace.h"
207d9fe9
CLG
25
26/*
27 * XIVE Thread Interrupt Management context
28 */
29
cdd4de68
CLG
30/*
31 * Convert a priority number to an Interrupt Pending Buffer (IPB)
32 * register, which indicates a pending interrupt at the priority
33 * corresponding to the bit number
34 */
35static uint8_t priority_to_ipb(uint8_t priority)
36{
37 return priority > XIVE_PRIORITY_MAX ?
38 0 : 1 << (XIVE_PRIORITY_MAX - priority);
39}
40
41/*
42 * Convert an Interrupt Pending Buffer (IPB) register to a Pending
43 * Interrupt Priority Register (PIPR), which contains the priority of
44 * the most favored pending notification.
45 */
46static uint8_t ipb_to_pipr(uint8_t ibp)
47{
48 return ibp ? clz32((uint32_t)ibp << 24) : 0xff;
49}
50
cdd4de68
CLG
51static uint8_t exception_mask(uint8_t ring)
52{
53 switch (ring) {
54 case TM_QW1_OS:
55 return TM_QW1_NSR_EO;
4836b455
CLG
56 case TM_QW3_HV_PHYS:
57 return TM_QW3_NSR_HE;
cdd4de68
CLG
58 default:
59 g_assert_not_reached();
60 }
61}
62
4aca9786
BH
63static qemu_irq xive_tctx_output(XiveTCTX *tctx, uint8_t ring)
64{
65 switch (ring) {
66 case TM_QW0_USER:
67 return 0; /* Not supported */
68 case TM_QW1_OS:
69 return tctx->os_output;
70 case TM_QW2_HV_POOL:
71 case TM_QW3_HV_PHYS:
72 return tctx->hv_output;
73 default:
74 return 0;
75 }
76}
77
207d9fe9
CLG
78static uint64_t xive_tctx_accept(XiveTCTX *tctx, uint8_t ring)
79{
cdd4de68
CLG
80 uint8_t *regs = &tctx->regs[ring];
81 uint8_t nsr = regs[TM_NSR];
82 uint8_t mask = exception_mask(ring);
83
4aca9786 84 qemu_irq_lower(xive_tctx_output(tctx, ring));
cdd4de68
CLG
85
86 if (regs[TM_NSR] & mask) {
87 uint8_t cppr = regs[TM_PIPR];
88
89 regs[TM_CPPR] = cppr;
90
91 /* Reset the pending buffer bit */
92 regs[TM_IPB] &= ~priority_to_ipb(cppr);
93 regs[TM_PIPR] = ipb_to_pipr(regs[TM_IPB]);
94
95 /* Drop Exception bit */
96 regs[TM_NSR] &= ~mask;
4e960974
CLG
97
98 trace_xive_tctx_accept(tctx->cs->cpu_index, ring,
99 regs[TM_IPB], regs[TM_PIPR],
100 regs[TM_CPPR], regs[TM_NSR]);
cdd4de68
CLG
101 }
102
103 return (nsr << 8) | regs[TM_CPPR];
104}
105
106static void xive_tctx_notify(XiveTCTX *tctx, uint8_t ring)
107{
108 uint8_t *regs = &tctx->regs[ring];
109
110 if (regs[TM_PIPR] < regs[TM_CPPR]) {
4836b455
CLG
111 switch (ring) {
112 case TM_QW1_OS:
113 regs[TM_NSR] |= TM_QW1_NSR_EO;
114 break;
115 case TM_QW3_HV_PHYS:
116 regs[TM_NSR] |= (TM_QW3_NSR_HE_PHYS << 6);
117 break;
118 default:
119 g_assert_not_reached();
120 }
4e960974
CLG
121 trace_xive_tctx_notify(tctx->cs->cpu_index, ring,
122 regs[TM_IPB], regs[TM_PIPR],
123 regs[TM_CPPR], regs[TM_NSR]);
4aca9786 124 qemu_irq_raise(xive_tctx_output(tctx, ring));
cdd4de68 125 }
207d9fe9
CLG
126}
127
128static void xive_tctx_set_cppr(XiveTCTX *tctx, uint8_t ring, uint8_t cppr)
129{
4e960974
CLG
130 uint8_t *regs = &tctx->regs[ring];
131
132 trace_xive_tctx_set_cppr(tctx->cs->cpu_index, ring,
133 regs[TM_IPB], regs[TM_PIPR],
134 cppr, regs[TM_NSR]);
135
207d9fe9
CLG
136 if (cppr > XIVE_PRIORITY_MAX) {
137 cppr = 0xff;
138 }
139
140 tctx->regs[ring + TM_CPPR] = cppr;
cdd4de68
CLG
141
142 /* CPPR has changed, check if we need to raise a pending exception */
143 xive_tctx_notify(tctx, ring);
207d9fe9
CLG
144}
145
a5b841f1
CLG
146void xive_tctx_ipb_update(XiveTCTX *tctx, uint8_t ring, uint8_t ipb)
147{
148 uint8_t *regs = &tctx->regs[ring];
149
150 regs[TM_IPB] |= ipb;
151 regs[TM_PIPR] = ipb_to_pipr(regs[TM_IPB]);
152 xive_tctx_notify(tctx, ring);
153}
154
aaa45030
CLG
155static inline uint32_t xive_tctx_word2(uint8_t *ring)
156{
157 return *((uint32_t *) &ring[TM_WORD2]);
158}
159
207d9fe9
CLG
160/*
161 * XIVE Thread Interrupt Management Area (TIMA)
162 */
163
4fb42350
CLG
164static void xive_tm_set_hv_cppr(XivePresenter *xptr, XiveTCTX *tctx,
165 hwaddr offset, uint64_t value, unsigned size)
4836b455
CLG
166{
167 xive_tctx_set_cppr(tctx, TM_QW3_HV_PHYS, value & 0xff);
168}
169
4fb42350
CLG
170static uint64_t xive_tm_ack_hv_reg(XivePresenter *xptr, XiveTCTX *tctx,
171 hwaddr offset, unsigned size)
4836b455
CLG
172{
173 return xive_tctx_accept(tctx, TM_QW3_HV_PHYS);
174}
175
4fb42350
CLG
176static uint64_t xive_tm_pull_pool_ctx(XivePresenter *xptr, XiveTCTX *tctx,
177 hwaddr offset, unsigned size)
4836b455 178{
aaa45030
CLG
179 uint32_t qw2w2_prev = xive_tctx_word2(&tctx->regs[TM_QW2_HV_POOL]);
180 uint32_t qw2w2;
4836b455 181
aaa45030
CLG
182 qw2w2 = xive_set_field32(TM_QW2W2_VP, qw2w2_prev, 0);
183 memcpy(&tctx->regs[TM_QW2_HV_POOL + TM_WORD2], &qw2w2, 4);
184 return qw2w2;
4836b455
CLG
185}
186
4fb42350 187static void xive_tm_vt_push(XivePresenter *xptr, XiveTCTX *tctx, hwaddr offset,
4836b455
CLG
188 uint64_t value, unsigned size)
189{
190 tctx->regs[TM_QW3_HV_PHYS + TM_WORD2] = value & 0xff;
191}
192
4fb42350
CLG
193static uint64_t xive_tm_vt_poll(XivePresenter *xptr, XiveTCTX *tctx,
194 hwaddr offset, unsigned size)
4836b455
CLG
195{
196 return tctx->regs[TM_QW3_HV_PHYS + TM_WORD2] & 0xff;
197}
198
207d9fe9
CLG
199/*
200 * Define an access map for each page of the TIMA that we will use in
201 * the memory region ops to filter values when doing loads and stores
202 * of raw registers values
203 *
204 * Registers accessibility bits :
205 *
206 * 0x0 - no access
207 * 0x1 - write only
208 * 0x2 - read only
209 * 0x3 - read/write
210 */
211
212static const uint8_t xive_tm_hw_view[] = {
8256870a
CLG
213 3, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 0, 0, 0, 0, /* QW-0 User */
214 3, 3, 3, 3, 3, 3, 0, 2, 3, 3, 3, 3, 0, 0, 0, 0, /* QW-1 OS */
215 0, 0, 3, 3, 0, 0, 0, 0, 3, 3, 3, 3, 0, 0, 0, 0, /* QW-2 POOL */
216 3, 3, 3, 3, 0, 3, 0, 2, 3, 0, 0, 3, 3, 3, 3, 0, /* QW-3 PHYS */
207d9fe9
CLG
217};
218
219static const uint8_t xive_tm_hv_view[] = {
8256870a
CLG
220 3, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 0, 0, 0, 0, /* QW-0 User */
221 3, 3, 3, 3, 3, 3, 0, 2, 3, 3, 3, 3, 0, 0, 0, 0, /* QW-1 OS */
222 0, 0, 3, 3, 0, 0, 0, 0, 0, 3, 3, 3, 0, 0, 0, 0, /* QW-2 POOL */
223 3, 3, 3, 3, 0, 3, 0, 2, 3, 0, 0, 3, 0, 0, 0, 0, /* QW-3 PHYS */
207d9fe9
CLG
224};
225
226static const uint8_t xive_tm_os_view[] = {
8256870a
CLG
227 3, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 0, 0, 0, 0, /* QW-0 User */
228 2, 3, 2, 2, 2, 2, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, /* QW-1 OS */
229 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* QW-2 POOL */
230 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* QW-3 PHYS */
207d9fe9
CLG
231};
232
233static const uint8_t xive_tm_user_view[] = {
8256870a
CLG
234 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* QW-0 User */
235 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* QW-1 OS */
236 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* QW-2 POOL */
237 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* QW-3 PHYS */
207d9fe9
CLG
238};
239
240/*
241 * Overall TIMA access map for the thread interrupt management context
242 * registers
243 */
244static const uint8_t *xive_tm_views[] = {
245 [XIVE_TM_HW_PAGE] = xive_tm_hw_view,
246 [XIVE_TM_HV_PAGE] = xive_tm_hv_view,
247 [XIVE_TM_OS_PAGE] = xive_tm_os_view,
248 [XIVE_TM_USER_PAGE] = xive_tm_user_view,
249};
250
251/*
252 * Computes a register access mask for a given offset in the TIMA
253 */
254static uint64_t xive_tm_mask(hwaddr offset, unsigned size, bool write)
255{
256 uint8_t page_offset = (offset >> TM_SHIFT) & 0x3;
257 uint8_t reg_offset = offset & 0x3F;
258 uint8_t reg_mask = write ? 0x1 : 0x2;
259 uint64_t mask = 0x0;
260 int i;
261
262 for (i = 0; i < size; i++) {
263 if (xive_tm_views[page_offset][reg_offset + i] & reg_mask) {
264 mask |= (uint64_t) 0xff << (8 * (size - i - 1));
265 }
266 }
267
268 return mask;
269}
270
271static void xive_tm_raw_write(XiveTCTX *tctx, hwaddr offset, uint64_t value,
272 unsigned size)
273{
274 uint8_t ring_offset = offset & 0x30;
275 uint8_t reg_offset = offset & 0x3F;
276 uint64_t mask = xive_tm_mask(offset, size, true);
277 int i;
278
279 /*
280 * Only 4 or 8 bytes stores are allowed and the User ring is
281 * excluded
282 */
283 if (size < 4 || !mask || ring_offset == TM_QW0_USER) {
284 qemu_log_mask(LOG_GUEST_ERROR, "XIVE: invalid write access at TIMA @%"
285 HWADDR_PRIx"\n", offset);
286 return;
287 }
288
289 /*
290 * Use the register offset for the raw values and filter out
291 * reserved values
292 */
293 for (i = 0; i < size; i++) {
294 uint8_t byte_mask = (mask >> (8 * (size - i - 1)));
295 if (byte_mask) {
296 tctx->regs[reg_offset + i] = (value >> (8 * (size - i - 1))) &
297 byte_mask;
298 }
299 }
300}
301
302static uint64_t xive_tm_raw_read(XiveTCTX *tctx, hwaddr offset, unsigned size)
303{
304 uint8_t ring_offset = offset & 0x30;
305 uint8_t reg_offset = offset & 0x3F;
306 uint64_t mask = xive_tm_mask(offset, size, false);
307 uint64_t ret;
308 int i;
309
310 /*
311 * Only 4 or 8 bytes loads are allowed and the User ring is
312 * excluded
313 */
314 if (size < 4 || !mask || ring_offset == TM_QW0_USER) {
315 qemu_log_mask(LOG_GUEST_ERROR, "XIVE: invalid read access at TIMA @%"
316 HWADDR_PRIx"\n", offset);
317 return -1;
318 }
319
320 /* Use the register offset for the raw values */
321 ret = 0;
322 for (i = 0; i < size; i++) {
323 ret |= (uint64_t) tctx->regs[reg_offset + i] << (8 * (size - i - 1));
324 }
325
326 /* filter out reserved values */
327 return ret & mask;
328}
329
330/*
331 * The TM context is mapped twice within each page. Stores and loads
332 * to the first mapping below 2K write and read the specified values
333 * without modification. The second mapping above 2K performs specific
334 * state changes (side effects) in addition to setting/returning the
335 * interrupt management area context of the processor thread.
336 */
4fb42350
CLG
337static uint64_t xive_tm_ack_os_reg(XivePresenter *xptr, XiveTCTX *tctx,
338 hwaddr offset, unsigned size)
207d9fe9
CLG
339{
340 return xive_tctx_accept(tctx, TM_QW1_OS);
341}
342
4fb42350
CLG
343static void xive_tm_set_os_cppr(XivePresenter *xptr, XiveTCTX *tctx,
344 hwaddr offset, uint64_t value, unsigned size)
207d9fe9
CLG
345{
346 xive_tctx_set_cppr(tctx, TM_QW1_OS, value & 0xff);
347}
348
cdd4de68
CLG
349/*
350 * Adjust the IPB to allow a CPU to process event queues of other
351 * priorities during one physical interrupt cycle.
352 */
4fb42350
CLG
353static void xive_tm_set_os_pending(XivePresenter *xptr, XiveTCTX *tctx,
354 hwaddr offset, uint64_t value, unsigned size)
cdd4de68 355{
a5b841f1 356 xive_tctx_ipb_update(tctx, TM_QW1_OS, priority_to_ipb(value & 0xff));
cdd4de68
CLG
357}
358
7065d067
CLG
359static void xive_os_cam_decode(uint32_t cam, uint8_t *nvt_blk,
360 uint32_t *nvt_idx, bool *vo)
361{
362 if (nvt_blk) {
363 *nvt_blk = xive_nvt_blk(cam);
364 }
365 if (nvt_idx) {
366 *nvt_idx = xive_nvt_idx(cam);
367 }
368 if (vo) {
369 *vo = !!(cam & TM_QW1W2_VO);
370 }
371}
372
373static uint32_t xive_tctx_get_os_cam(XiveTCTX *tctx, uint8_t *nvt_blk,
374 uint32_t *nvt_idx, bool *vo)
375{
376 uint32_t qw1w2 = xive_tctx_word2(&tctx->regs[TM_QW1_OS]);
377 uint32_t cam = be32_to_cpu(qw1w2);
378
379 xive_os_cam_decode(cam, nvt_blk, nvt_idx, vo);
380 return qw1w2;
381}
382
383static void xive_tctx_set_os_cam(XiveTCTX *tctx, uint32_t qw1w2)
384{
385 memcpy(&tctx->regs[TM_QW1_OS + TM_WORD2], &qw1w2, 4);
386}
387
4fb42350
CLG
388static uint64_t xive_tm_pull_os_ctx(XivePresenter *xptr, XiveTCTX *tctx,
389 hwaddr offset, unsigned size)
d98ec603 390{
d98ec603 391 uint32_t qw1w2;
7065d067
CLG
392 uint32_t qw1w2_new;
393 uint8_t nvt_blk;
394 uint32_t nvt_idx;
395 bool vo;
d98ec603 396
7065d067
CLG
397 qw1w2 = xive_tctx_get_os_cam(tctx, &nvt_blk, &nvt_idx, &vo);
398
1c27b252
CLG
399 if (!vo) {
400 qemu_log_mask(LOG_GUEST_ERROR, "XIVE: pulling invalid NVT %x/%x !?\n",
401 nvt_blk, nvt_idx);
402 }
403
7065d067
CLG
404 /* Invalidate CAM line */
405 qw1w2_new = xive_set_field32(TM_QW1W2_VO, qw1w2, 0);
406 xive_tctx_set_os_cam(tctx, qw1w2_new);
d98ec603
CLG
407 return qw1w2;
408}
409
d1f2a574
CLG
410static void xive_tctx_need_resend(XiveRouter *xrtr, XiveTCTX *tctx,
411 uint8_t nvt_blk, uint32_t nvt_idx)
412{
413 XiveNVT nvt;
414 uint8_t ipb;
415
416 /*
417 * Grab the associated NVT to pull the pending bits, and merge
418 * them with the IPB of the thread interrupt context registers
419 */
420 if (xive_router_get_nvt(xrtr, nvt_blk, nvt_idx, &nvt)) {
421 qemu_log_mask(LOG_GUEST_ERROR, "XIVE: invalid NVT %x/%x\n",
422 nvt_blk, nvt_idx);
423 return;
424 }
425
426 ipb = xive_get_field32(NVT_W4_IPB, nvt.w4);
427
428 if (ipb) {
429 /* Reset the NVT value */
430 nvt.w4 = xive_set_field32(NVT_W4_IPB, nvt.w4, 0);
431 xive_router_write_nvt(xrtr, nvt_blk, nvt_idx, &nvt, 4);
432
433 /* Merge in current context */
434 xive_tctx_ipb_update(tctx, TM_QW1_OS, ipb);
435 }
436}
437
438/*
439 * Updating the OS CAM line can trigger a resend of interrupt
440 */
441static void xive_tm_push_os_ctx(XivePresenter *xptr, XiveTCTX *tctx,
442 hwaddr offset, uint64_t value, unsigned size)
443{
444 uint32_t cam = value;
445 uint32_t qw1w2 = cpu_to_be32(cam);
446 uint8_t nvt_blk;
447 uint32_t nvt_idx;
448 bool vo;
449
450 xive_os_cam_decode(cam, &nvt_blk, &nvt_idx, &vo);
451
452 /* First update the registers */
453 xive_tctx_set_os_cam(tctx, qw1w2);
454
455 /* Check the interrupt pending bits */
456 if (vo) {
457 xive_tctx_need_resend(XIVE_ROUTER(xptr), tctx, nvt_blk, nvt_idx);
458 }
459}
460
207d9fe9
CLG
461/*
462 * Define a mapping of "special" operations depending on the TIMA page
463 * offset and the size of the operation.
464 */
465typedef struct XiveTmOp {
466 uint8_t page_offset;
467 uint32_t op_offset;
468 unsigned size;
4fb42350
CLG
469 void (*write_handler)(XivePresenter *xptr, XiveTCTX *tctx,
470 hwaddr offset,
471 uint64_t value, unsigned size);
472 uint64_t (*read_handler)(XivePresenter *xptr, XiveTCTX *tctx, hwaddr offset,
473 unsigned size);
207d9fe9
CLG
474} XiveTmOp;
475
476static const XiveTmOp xive_tm_operations[] = {
477 /*
478 * MMIOs below 2K : raw values and special operations without side
479 * effects
480 */
481 { XIVE_TM_OS_PAGE, TM_QW1_OS + TM_CPPR, 1, xive_tm_set_os_cppr, NULL },
d1f2a574 482 { XIVE_TM_HV_PAGE, TM_QW1_OS + TM_WORD2, 4, xive_tm_push_os_ctx, NULL },
4836b455
CLG
483 { XIVE_TM_HV_PAGE, TM_QW3_HV_PHYS + TM_CPPR, 1, xive_tm_set_hv_cppr, NULL },
484 { XIVE_TM_HV_PAGE, TM_QW3_HV_PHYS + TM_WORD2, 1, xive_tm_vt_push, NULL },
485 { XIVE_TM_HV_PAGE, TM_QW3_HV_PHYS + TM_WORD2, 1, NULL, xive_tm_vt_poll },
207d9fe9
CLG
486
487 /* MMIOs above 2K : special operations with side effects */
488 { XIVE_TM_OS_PAGE, TM_SPC_ACK_OS_REG, 2, NULL, xive_tm_ack_os_reg },
cdd4de68 489 { XIVE_TM_OS_PAGE, TM_SPC_SET_OS_PENDING, 1, xive_tm_set_os_pending, NULL },
d98ec603
CLG
490 { XIVE_TM_HV_PAGE, TM_SPC_PULL_OS_CTX, 4, NULL, xive_tm_pull_os_ctx },
491 { XIVE_TM_HV_PAGE, TM_SPC_PULL_OS_CTX, 8, NULL, xive_tm_pull_os_ctx },
4836b455
CLG
492 { XIVE_TM_HV_PAGE, TM_SPC_ACK_HV_REG, 2, NULL, xive_tm_ack_hv_reg },
493 { XIVE_TM_HV_PAGE, TM_SPC_PULL_POOL_CTX, 4, NULL, xive_tm_pull_pool_ctx },
494 { XIVE_TM_HV_PAGE, TM_SPC_PULL_POOL_CTX, 8, NULL, xive_tm_pull_pool_ctx },
207d9fe9
CLG
495};
496
497static const XiveTmOp *xive_tm_find_op(hwaddr offset, unsigned size, bool write)
498{
499 uint8_t page_offset = (offset >> TM_SHIFT) & 0x3;
500 uint32_t op_offset = offset & 0xFFF;
501 int i;
502
503 for (i = 0; i < ARRAY_SIZE(xive_tm_operations); i++) {
504 const XiveTmOp *xto = &xive_tm_operations[i];
505
506 /* Accesses done from a more privileged TIMA page is allowed */
507 if (xto->page_offset >= page_offset &&
508 xto->op_offset == op_offset &&
509 xto->size == size &&
510 ((write && xto->write_handler) || (!write && xto->read_handler))) {
511 return xto;
512 }
513 }
514 return NULL;
515}
516
517/*
518 * TIMA MMIO handlers
519 */
4fb42350
CLG
520void xive_tctx_tm_write(XivePresenter *xptr, XiveTCTX *tctx, hwaddr offset,
521 uint64_t value, unsigned size)
207d9fe9 522{
207d9fe9
CLG
523 const XiveTmOp *xto;
524
4e960974
CLG
525 trace_xive_tctx_tm_write(offset, size, value);
526
207d9fe9 527 /*
4836b455 528 * TODO: check V bit in Q[0-3]W2
207d9fe9
CLG
529 */
530
531 /*
532 * First, check for special operations in the 2K region
533 */
534 if (offset & 0x800) {
535 xto = xive_tm_find_op(offset, size, true);
536 if (!xto) {
d98ec603 537 qemu_log_mask(LOG_GUEST_ERROR, "XIVE: invalid write access at TIMA "
207d9fe9
CLG
538 "@%"HWADDR_PRIx"\n", offset);
539 } else {
4fb42350 540 xto->write_handler(xptr, tctx, offset, value, size);
207d9fe9
CLG
541 }
542 return;
543 }
544
545 /*
546 * Then, for special operations in the region below 2K.
547 */
548 xto = xive_tm_find_op(offset, size, true);
549 if (xto) {
4fb42350 550 xto->write_handler(xptr, tctx, offset, value, size);
207d9fe9
CLG
551 return;
552 }
553
554 /*
555 * Finish with raw access to the register values
556 */
557 xive_tm_raw_write(tctx, offset, value, size);
558}
559
4fb42350
CLG
560uint64_t xive_tctx_tm_read(XivePresenter *xptr, XiveTCTX *tctx, hwaddr offset,
561 unsigned size)
207d9fe9 562{
207d9fe9 563 const XiveTmOp *xto;
4e960974 564 uint64_t ret;
207d9fe9
CLG
565
566 /*
4836b455 567 * TODO: check V bit in Q[0-3]W2
207d9fe9
CLG
568 */
569
570 /*
571 * First, check for special operations in the 2K region
572 */
573 if (offset & 0x800) {
574 xto = xive_tm_find_op(offset, size, false);
575 if (!xto) {
576 qemu_log_mask(LOG_GUEST_ERROR, "XIVE: invalid read access to TIMA"
577 "@%"HWADDR_PRIx"\n", offset);
578 return -1;
579 }
4e960974
CLG
580 ret = xto->read_handler(xptr, tctx, offset, size);
581 goto out;
207d9fe9
CLG
582 }
583
584 /*
585 * Then, for special operations in the region below 2K.
586 */
587 xto = xive_tm_find_op(offset, size, false);
588 if (xto) {
4e960974
CLG
589 ret = xto->read_handler(xptr, tctx, offset, size);
590 goto out;
207d9fe9
CLG
591 }
592
593 /*
594 * Finish with raw access to the register values
595 */
4e960974
CLG
596 ret = xive_tm_raw_read(tctx, offset, size);
597out:
598 trace_xive_tctx_tm_read(offset, size, ret);
599 return ret;
207d9fe9 600}
207d9fe9 601
207d9fe9
CLG
602static char *xive_tctx_ring_print(uint8_t *ring)
603{
604 uint32_t w2 = xive_tctx_word2(ring);
605
606 return g_strdup_printf("%02x %02x %02x %02x %02x "
607 "%02x %02x %02x %08x",
608 ring[TM_NSR], ring[TM_CPPR], ring[TM_IPB], ring[TM_LSMFB],
609 ring[TM_ACK_CNT], ring[TM_INC], ring[TM_AGE], ring[TM_PIPR],
610 be32_to_cpu(w2));
611}
612
613static const char * const xive_tctx_ring_names[] = {
614 "USER", "OS", "POOL", "PHYS",
615};
616
e519cdd9
GK
617/*
618 * kvm_irqchip_in_kernel() will cause the compiler to turn this
619 * info a nop if CONFIG_KVM isn't defined.
620 */
621#define xive_in_kernel(xptr) \
622 (kvm_irqchip_in_kernel() && \
623 ({ \
624 XivePresenterClass *xpc = XIVE_PRESENTER_GET_CLASS(xptr); \
625 xpc->in_kernel ? xpc->in_kernel(xptr) : false; \
626 }))
627
207d9fe9
CLG
628void xive_tctx_pic_print_info(XiveTCTX *tctx, Monitor *mon)
629{
0a83b470 630 int cpu_index;
207d9fe9
CLG
631 int i;
632
0a83b470
GK
633 /* Skip partially initialized vCPUs. This can happen on sPAPR when vCPUs
634 * are hot plugged or unplugged.
635 */
636 if (!tctx) {
637 return;
638 }
639
640 cpu_index = tctx->cs ? tctx->cs->cpu_index : -1;
641
e519cdd9 642 if (xive_in_kernel(tctx->xptr)) {
7bfc759c
CLG
643 Error *local_err = NULL;
644
645 kvmppc_xive_cpu_synchronize_state(tctx, &local_err);
646 if (local_err) {
647 error_report_err(local_err);
648 return;
649 }
650 }
651
207d9fe9
CLG
652 monitor_printf(mon, "CPU[%04x]: QW NSR CPPR IPB LSMFB ACK# INC AGE PIPR"
653 " W2\n", cpu_index);
654
655 for (i = 0; i < XIVE_TM_RING_COUNT; i++) {
656 char *s = xive_tctx_ring_print(&tctx->regs[i * XIVE_TM_RING_SIZE]);
657 monitor_printf(mon, "CPU[%04x]: %4s %s\n", cpu_index,
658 xive_tctx_ring_names[i], s);
659 g_free(s);
660 }
661}
662
d49e8a9b 663void xive_tctx_reset(XiveTCTX *tctx)
207d9fe9 664{
207d9fe9
CLG
665 memset(tctx->regs, 0, sizeof(tctx->regs));
666
667 /* Set some defaults */
668 tctx->regs[TM_QW1_OS + TM_LSMFB] = 0xFF;
669 tctx->regs[TM_QW1_OS + TM_ACK_CNT] = 0xFF;
670 tctx->regs[TM_QW1_OS + TM_AGE] = 0xFF;
cdd4de68
CLG
671
672 /*
673 * Initialize PIPR to 0xFF to avoid phantom interrupts when the
674 * CPPR is first set.
675 */
676 tctx->regs[TM_QW1_OS + TM_PIPR] =
677 ipb_to_pipr(tctx->regs[TM_QW1_OS + TM_IPB]);
4836b455
CLG
678 tctx->regs[TM_QW3_HV_PHYS + TM_PIPR] =
679 ipb_to_pipr(tctx->regs[TM_QW3_HV_PHYS + TM_IPB]);
207d9fe9
CLG
680}
681
682static void xive_tctx_realize(DeviceState *dev, Error **errp)
683{
684 XiveTCTX *tctx = XIVE_TCTX(dev);
685 PowerPCCPU *cpu;
686 CPUPPCState *env;
207d9fe9 687
411c2a61 688 assert(tctx->cs);
47950946 689 assert(tctx->xptr);
207d9fe9 690
411c2a61 691 cpu = POWERPC_CPU(tctx->cs);
207d9fe9
CLG
692 env = &cpu->env;
693 switch (PPC_INPUT(env)) {
67afe775 694 case PPC_FLAGS_INPUT_POWER9:
4aca9786
BH
695 tctx->hv_output = env->irq_inputs[POWER9_INPUT_HINT];
696 tctx->os_output = env->irq_inputs[POWER9_INPUT_INT];
67afe775 697 break;
207d9fe9
CLG
698
699 default:
700 error_setg(errp, "XIVE interrupt controller does not support "
701 "this CPU bus model");
702 return;
703 }
704
38afd772 705 /* Connect the presenter to the VCPU (required for CPU hotplug) */
e519cdd9 706 if (xive_in_kernel(tctx->xptr)) {
61203f2b 707 if (kvmppc_xive_cpu_connect(tctx, errp) < 0) {
38afd772
CLG
708 return;
709 }
710 }
207d9fe9
CLG
711}
712
277dd3d7
CLG
713static int vmstate_xive_tctx_pre_save(void *opaque)
714{
e519cdd9 715 XiveTCTX *tctx = XIVE_TCTX(opaque);
277dd3d7 716 Error *local_err = NULL;
2a8100cb 717 int ret;
277dd3d7 718
e519cdd9 719 if (xive_in_kernel(tctx->xptr)) {
2a8100cb
GK
720 ret = kvmppc_xive_cpu_get_state(tctx, &local_err);
721 if (ret < 0) {
277dd3d7 722 error_report_err(local_err);
2a8100cb 723 return ret;
277dd3d7
CLG
724 }
725 }
726
727 return 0;
728}
729
310cda5b
CLG
730static int vmstate_xive_tctx_post_load(void *opaque, int version_id)
731{
e519cdd9 732 XiveTCTX *tctx = XIVE_TCTX(opaque);
310cda5b 733 Error *local_err = NULL;
2a8100cb 734 int ret;
310cda5b 735
e519cdd9 736 if (xive_in_kernel(tctx->xptr)) {
310cda5b
CLG
737 /*
738 * Required for hotplugged CPU, for which the state comes
739 * after all states of the machine.
740 */
2a8100cb
GK
741 ret = kvmppc_xive_cpu_set_state(tctx, &local_err);
742 if (ret < 0) {
310cda5b 743 error_report_err(local_err);
2a8100cb 744 return ret;
310cda5b
CLG
745 }
746 }
747
748 return 0;
749}
750
207d9fe9
CLG
751static const VMStateDescription vmstate_xive_tctx = {
752 .name = TYPE_XIVE_TCTX,
753 .version_id = 1,
754 .minimum_version_id = 1,
277dd3d7 755 .pre_save = vmstate_xive_tctx_pre_save,
310cda5b 756 .post_load = vmstate_xive_tctx_post_load,
207d9fe9
CLG
757 .fields = (VMStateField[]) {
758 VMSTATE_BUFFER(regs, XiveTCTX),
759 VMSTATE_END_OF_LIST()
760 },
761};
762
411c2a61
GK
763static Property xive_tctx_properties[] = {
764 DEFINE_PROP_LINK("cpu", XiveTCTX, cs, TYPE_CPU, CPUState *),
47950946
CLG
765 DEFINE_PROP_LINK("presenter", XiveTCTX, xptr, TYPE_XIVE_PRESENTER,
766 XivePresenter *),
411c2a61
GK
767 DEFINE_PROP_END_OF_LIST(),
768};
769
207d9fe9
CLG
770static void xive_tctx_class_init(ObjectClass *klass, void *data)
771{
772 DeviceClass *dc = DEVICE_CLASS(klass);
773
774 dc->desc = "XIVE Interrupt Thread Context";
775 dc->realize = xive_tctx_realize;
207d9fe9 776 dc->vmsd = &vmstate_xive_tctx;
4f67d30b 777 device_class_set_props(dc, xive_tctx_properties);
878b2b48
GK
778 /*
779 * Reason: part of XIVE interrupt controller, needs to be wired up
780 * by xive_tctx_create().
781 */
782 dc->user_creatable = false;
207d9fe9
CLG
783}
784
785static const TypeInfo xive_tctx_info = {
786 .name = TYPE_XIVE_TCTX,
787 .parent = TYPE_DEVICE,
788 .instance_size = sizeof(XiveTCTX),
789 .class_init = xive_tctx_class_init,
790};
02e3ff54 791
47950946 792Object *xive_tctx_create(Object *cpu, XivePresenter *xptr, Error **errp)
1a937ad7 793{
1a937ad7
CLG
794 Object *obj;
795
796 obj = object_new(TYPE_XIVE_TCTX);
d2623129 797 object_property_add_child(cpu, TYPE_XIVE_TCTX, obj);
1a937ad7 798 object_unref(obj);
5325cc34
MA
799 object_property_set_link(obj, "cpu", cpu, &error_abort);
800 object_property_set_link(obj, "presenter", OBJECT(xptr), &error_abort);
992861fb
MA
801 if (!qdev_realize(DEVICE(obj), NULL, errp)) {
802 object_unparent(obj);
803 return NULL;
1a937ad7 804 }
1a937ad7 805 return obj;
1a937ad7
CLG
806}
807
0990ce6a
GK
808void xive_tctx_destroy(XiveTCTX *tctx)
809{
35886de1
GK
810 Object *obj = OBJECT(tctx);
811
35886de1 812 object_unparent(obj);
0990ce6a
GK
813}
814
02e3ff54
CLG
815/*
816 * XIVE ESB helpers
817 */
818
819static uint8_t xive_esb_set(uint8_t *pq, uint8_t value)
820{
821 uint8_t old_pq = *pq & 0x3;
822
823 *pq &= ~0x3;
824 *pq |= value & 0x3;
825
826 return old_pq;
827}
828
829static bool xive_esb_trigger(uint8_t *pq)
830{
831 uint8_t old_pq = *pq & 0x3;
832
833 switch (old_pq) {
834 case XIVE_ESB_RESET:
835 xive_esb_set(pq, XIVE_ESB_PENDING);
836 return true;
837 case XIVE_ESB_PENDING:
838 case XIVE_ESB_QUEUED:
839 xive_esb_set(pq, XIVE_ESB_QUEUED);
840 return false;
841 case XIVE_ESB_OFF:
842 xive_esb_set(pq, XIVE_ESB_OFF);
843 return false;
844 default:
845 g_assert_not_reached();
846 }
847}
848
849static bool xive_esb_eoi(uint8_t *pq)
850{
851 uint8_t old_pq = *pq & 0x3;
852
853 switch (old_pq) {
854 case XIVE_ESB_RESET:
855 case XIVE_ESB_PENDING:
856 xive_esb_set(pq, XIVE_ESB_RESET);
857 return false;
858 case XIVE_ESB_QUEUED:
859 xive_esb_set(pq, XIVE_ESB_PENDING);
860 return true;
861 case XIVE_ESB_OFF:
862 xive_esb_set(pq, XIVE_ESB_OFF);
863 return false;
864 default:
865 g_assert_not_reached();
866 }
867}
868
869/*
870 * XIVE Interrupt Source (or IVSE)
871 */
872
873uint8_t xive_source_esb_get(XiveSource *xsrc, uint32_t srcno)
874{
875 assert(srcno < xsrc->nr_irqs);
876
877 return xsrc->status[srcno] & 0x3;
878}
879
880uint8_t xive_source_esb_set(XiveSource *xsrc, uint32_t srcno, uint8_t pq)
881{
882 assert(srcno < xsrc->nr_irqs);
883
884 return xive_esb_set(&xsrc->status[srcno], pq);
885}
886
5fd9ef18
CLG
887/*
888 * Returns whether the event notification should be forwarded.
889 */
890static bool xive_source_lsi_trigger(XiveSource *xsrc, uint32_t srcno)
891{
892 uint8_t old_pq = xive_source_esb_get(xsrc, srcno);
893
894 xsrc->status[srcno] |= XIVE_STATUS_ASSERTED;
895
896 switch (old_pq) {
897 case XIVE_ESB_RESET:
898 xive_source_esb_set(xsrc, srcno, XIVE_ESB_PENDING);
899 return true;
900 default:
901 return false;
902 }
903}
904
02e3ff54
CLG
905/*
906 * Returns whether the event notification should be forwarded.
907 */
908static bool xive_source_esb_trigger(XiveSource *xsrc, uint32_t srcno)
909{
5fd9ef18
CLG
910 bool ret;
911
02e3ff54
CLG
912 assert(srcno < xsrc->nr_irqs);
913
5fd9ef18
CLG
914 ret = xive_esb_trigger(&xsrc->status[srcno]);
915
916 if (xive_source_irq_is_lsi(xsrc, srcno) &&
917 xive_source_esb_get(xsrc, srcno) == XIVE_ESB_QUEUED) {
918 qemu_log_mask(LOG_GUEST_ERROR,
919 "XIVE: queued an event on LSI IRQ %d\n", srcno);
920 }
921
922 return ret;
02e3ff54
CLG
923}
924
925/*
926 * Returns whether the event notification should be forwarded.
927 */
928static bool xive_source_esb_eoi(XiveSource *xsrc, uint32_t srcno)
929{
5fd9ef18
CLG
930 bool ret;
931
02e3ff54
CLG
932 assert(srcno < xsrc->nr_irqs);
933
5fd9ef18
CLG
934 ret = xive_esb_eoi(&xsrc->status[srcno]);
935
936 /*
937 * LSI sources do not set the Q bit but they can still be
938 * asserted, in which case we should forward a new event
939 * notification
940 */
941 if (xive_source_irq_is_lsi(xsrc, srcno) &&
942 xsrc->status[srcno] & XIVE_STATUS_ASSERTED) {
943 ret = xive_source_lsi_trigger(xsrc, srcno);
944 }
945
946 return ret;
02e3ff54
CLG
947}
948
949/*
950 * Forward the source event notification to the Router
951 */
952static void xive_source_notify(XiveSource *xsrc, int srcno)
953{
5e79b155 954 XiveNotifierClass *xnc = XIVE_NOTIFIER_GET_CLASS(xsrc->xive);
02e3ff54 955
5e79b155
CLG
956 if (xnc->notify) {
957 xnc->notify(xsrc->xive, srcno);
958 }
02e3ff54
CLG
959}
960
961/*
962 * In a two pages ESB MMIO setting, even page is the trigger page, odd
963 * page is for management
964 */
965static inline bool addr_is_even(hwaddr addr, uint32_t shift)
966{
967 return !((addr >> shift) & 1);
968}
969
970static inline bool xive_source_is_trigger_page(XiveSource *xsrc, hwaddr addr)
971{
972 return xive_source_esb_has_2page(xsrc) &&
973 addr_is_even(addr, xsrc->esb_shift - 1);
974}
975
976/*
977 * ESB MMIO loads
978 * Trigger page Management/EOI page
979 *
980 * ESB MMIO setting 2 pages 1 or 2 pages
981 *
982 * 0x000 .. 0x3FF -1 EOI and return 0|1
983 * 0x400 .. 0x7FF -1 EOI and return 0|1
984 * 0x800 .. 0xBFF -1 return PQ
985 * 0xC00 .. 0xCFF -1 return PQ and atomically PQ=00
986 * 0xD00 .. 0xDFF -1 return PQ and atomically PQ=01
987 * 0xE00 .. 0xDFF -1 return PQ and atomically PQ=10
988 * 0xF00 .. 0xDFF -1 return PQ and atomically PQ=11
989 */
990static uint64_t xive_source_esb_read(void *opaque, hwaddr addr, unsigned size)
991{
992 XiveSource *xsrc = XIVE_SOURCE(opaque);
993 uint32_t offset = addr & 0xFFF;
994 uint32_t srcno = addr >> xsrc->esb_shift;
995 uint64_t ret = -1;
996
997 /* In a two pages ESB MMIO setting, trigger page should not be read */
998 if (xive_source_is_trigger_page(xsrc, addr)) {
999 qemu_log_mask(LOG_GUEST_ERROR,
1000 "XIVE: invalid load on IRQ %d trigger page at "
1001 "0x%"HWADDR_PRIx"\n", srcno, addr);
1002 return -1;
1003 }
1004
1005 switch (offset) {
1006 case XIVE_ESB_LOAD_EOI ... XIVE_ESB_LOAD_EOI + 0x7FF:
1007 ret = xive_source_esb_eoi(xsrc, srcno);
1008
1009 /* Forward the source event notification for routing */
1010 if (ret) {
1011 xive_source_notify(xsrc, srcno);
1012 }
1013 break;
1014
1015 case XIVE_ESB_GET ... XIVE_ESB_GET + 0x3FF:
1016 ret = xive_source_esb_get(xsrc, srcno);
1017 break;
1018
1019 case XIVE_ESB_SET_PQ_00 ... XIVE_ESB_SET_PQ_00 + 0x0FF:
1020 case XIVE_ESB_SET_PQ_01 ... XIVE_ESB_SET_PQ_01 + 0x0FF:
1021 case XIVE_ESB_SET_PQ_10 ... XIVE_ESB_SET_PQ_10 + 0x0FF:
1022 case XIVE_ESB_SET_PQ_11 ... XIVE_ESB_SET_PQ_11 + 0x0FF:
1023 ret = xive_source_esb_set(xsrc, srcno, (offset >> 8) & 0x3);
1024 break;
1025 default:
1026 qemu_log_mask(LOG_GUEST_ERROR, "XIVE: invalid ESB load addr %x\n",
1027 offset);
1028 }
1029
4e960974
CLG
1030 trace_xive_source_esb_read(addr, srcno, ret);
1031
02e3ff54
CLG
1032 return ret;
1033}
1034
1035/*
1036 * ESB MMIO stores
1037 * Trigger page Management/EOI page
1038 *
1039 * ESB MMIO setting 2 pages 1 or 2 pages
1040 *
1041 * 0x000 .. 0x3FF Trigger Trigger
1042 * 0x400 .. 0x7FF Trigger EOI
1043 * 0x800 .. 0xBFF Trigger undefined
1044 * 0xC00 .. 0xCFF Trigger PQ=00
1045 * 0xD00 .. 0xDFF Trigger PQ=01
1046 * 0xE00 .. 0xDFF Trigger PQ=10
1047 * 0xF00 .. 0xDFF Trigger PQ=11
1048 */
1049static void xive_source_esb_write(void *opaque, hwaddr addr,
1050 uint64_t value, unsigned size)
1051{
1052 XiveSource *xsrc = XIVE_SOURCE(opaque);
1053 uint32_t offset = addr & 0xFFF;
1054 uint32_t srcno = addr >> xsrc->esb_shift;
1055 bool notify = false;
1056
4e960974
CLG
1057 trace_xive_source_esb_write(addr, srcno, value);
1058
02e3ff54
CLG
1059 /* In a two pages ESB MMIO setting, trigger page only triggers */
1060 if (xive_source_is_trigger_page(xsrc, addr)) {
1061 notify = xive_source_esb_trigger(xsrc, srcno);
1062 goto out;
1063 }
1064
1065 switch (offset) {
1066 case 0 ... 0x3FF:
1067 notify = xive_source_esb_trigger(xsrc, srcno);
1068 break;
1069
1070 case XIVE_ESB_STORE_EOI ... XIVE_ESB_STORE_EOI + 0x3FF:
1071 if (!(xsrc->esb_flags & XIVE_SRC_STORE_EOI)) {
1072 qemu_log_mask(LOG_GUEST_ERROR,
1073 "XIVE: invalid Store EOI for IRQ %d\n", srcno);
1074 return;
1075 }
1076
1077 notify = xive_source_esb_eoi(xsrc, srcno);
1078 break;
1079
1080 case XIVE_ESB_SET_PQ_00 ... XIVE_ESB_SET_PQ_00 + 0x0FF:
1081 case XIVE_ESB_SET_PQ_01 ... XIVE_ESB_SET_PQ_01 + 0x0FF:
1082 case XIVE_ESB_SET_PQ_10 ... XIVE_ESB_SET_PQ_10 + 0x0FF:
1083 case XIVE_ESB_SET_PQ_11 ... XIVE_ESB_SET_PQ_11 + 0x0FF:
1084 xive_source_esb_set(xsrc, srcno, (offset >> 8) & 0x3);
1085 break;
1086
1087 default:
1088 qemu_log_mask(LOG_GUEST_ERROR, "XIVE: invalid ESB write addr %x\n",
1089 offset);
1090 return;
1091 }
1092
1093out:
1094 /* Forward the source event notification for routing */
1095 if (notify) {
1096 xive_source_notify(xsrc, srcno);
1097 }
1098}
1099
1100static const MemoryRegionOps xive_source_esb_ops = {
1101 .read = xive_source_esb_read,
1102 .write = xive_source_esb_write,
1103 .endianness = DEVICE_BIG_ENDIAN,
1104 .valid = {
1105 .min_access_size = 8,
1106 .max_access_size = 8,
1107 },
1108 .impl = {
1109 .min_access_size = 8,
1110 .max_access_size = 8,
1111 },
1112};
1113
734d9c89 1114void xive_source_set_irq(void *opaque, int srcno, int val)
02e3ff54
CLG
1115{
1116 XiveSource *xsrc = XIVE_SOURCE(opaque);
1117 bool notify = false;
1118
5fd9ef18
CLG
1119 if (xive_source_irq_is_lsi(xsrc, srcno)) {
1120 if (val) {
1121 notify = xive_source_lsi_trigger(xsrc, srcno);
1122 } else {
1123 xsrc->status[srcno] &= ~XIVE_STATUS_ASSERTED;
1124 }
1125 } else {
1126 if (val) {
1127 notify = xive_source_esb_trigger(xsrc, srcno);
1128 }
02e3ff54
CLG
1129 }
1130
1131 /* Forward the source event notification for routing */
1132 if (notify) {
1133 xive_source_notify(xsrc, srcno);
1134 }
1135}
1136
1137void xive_source_pic_print_info(XiveSource *xsrc, uint32_t offset, Monitor *mon)
1138{
1139 int i;
1140
1141 for (i = 0; i < xsrc->nr_irqs; i++) {
1142 uint8_t pq = xive_source_esb_get(xsrc, i);
1143
1144 if (pq == XIVE_ESB_OFF) {
1145 continue;
1146 }
1147
5fd9ef18
CLG
1148 monitor_printf(mon, " %08x %s %c%c%c\n", i + offset,
1149 xive_source_irq_is_lsi(xsrc, i) ? "LSI" : "MSI",
02e3ff54 1150 pq & XIVE_ESB_VAL_P ? 'P' : '-',
5fd9ef18
CLG
1151 pq & XIVE_ESB_VAL_Q ? 'Q' : '-',
1152 xsrc->status[i] & XIVE_STATUS_ASSERTED ? 'A' : ' ');
02e3ff54
CLG
1153 }
1154}
1155
1156static void xive_source_reset(void *dev)
1157{
1158 XiveSource *xsrc = XIVE_SOURCE(dev);
1159
5fd9ef18
CLG
1160 /* Do not clear the LSI bitmap */
1161
02e3ff54
CLG
1162 /* PQs are initialized to 0b01 (Q=1) which corresponds to "ints off" */
1163 memset(xsrc->status, XIVE_ESB_OFF, xsrc->nr_irqs);
1164}
1165
1166static void xive_source_realize(DeviceState *dev, Error **errp)
1167{
1168 XiveSource *xsrc = XIVE_SOURCE(dev);
cf36e5b3 1169 size_t esb_len = xive_source_esb_len(xsrc);
5e79b155 1170
82ea3a1b 1171 assert(xsrc->xive);
02e3ff54
CLG
1172
1173 if (!xsrc->nr_irqs) {
1174 error_setg(errp, "Number of interrupt needs to be greater than 0");
1175 return;
1176 }
1177
1178 if (xsrc->esb_shift != XIVE_ESB_4K &&
1179 xsrc->esb_shift != XIVE_ESB_4K_2PAGE &&
1180 xsrc->esb_shift != XIVE_ESB_64K &&
1181 xsrc->esb_shift != XIVE_ESB_64K_2PAGE) {
1182 error_setg(errp, "Invalid ESB shift setting");
1183 return;
1184 }
1185
1186 xsrc->status = g_malloc0(xsrc->nr_irqs);
5fd9ef18 1187 xsrc->lsi_map = bitmap_new(xsrc->nr_irqs);
02e3ff54 1188
cf36e5b3
GK
1189 memory_region_init(&xsrc->esb_mmio, OBJECT(xsrc), "xive.esb", esb_len);
1190 memory_region_init_io(&xsrc->esb_mmio_emulated, OBJECT(xsrc),
1191 &xive_source_esb_ops, xsrc, "xive.esb-emulated",
1192 esb_len);
1193 memory_region_add_subregion(&xsrc->esb_mmio, 0, &xsrc->esb_mmio_emulated);
02e3ff54 1194
02e3ff54
CLG
1195 qemu_register_reset(xive_source_reset, dev);
1196}
1197
1198static const VMStateDescription vmstate_xive_source = {
1199 .name = TYPE_XIVE_SOURCE,
1200 .version_id = 1,
1201 .minimum_version_id = 1,
1202 .fields = (VMStateField[]) {
1203 VMSTATE_UINT32_EQUAL(nr_irqs, XiveSource, NULL),
1204 VMSTATE_VBUFFER_UINT32(status, XiveSource, 1, NULL, nr_irqs),
1205 VMSTATE_END_OF_LIST()
1206 },
1207};
1208
1209/*
1210 * The default XIVE interrupt source setting for the ESB MMIOs is two
1211 * 64k pages without Store EOI, to be in sync with KVM.
1212 */
1213static Property xive_source_properties[] = {
1214 DEFINE_PROP_UINT64("flags", XiveSource, esb_flags, 0),
1215 DEFINE_PROP_UINT32("nr-irqs", XiveSource, nr_irqs, 0),
1216 DEFINE_PROP_UINT32("shift", XiveSource, esb_shift, XIVE_ESB_64K_2PAGE),
82ea3a1b
GK
1217 DEFINE_PROP_LINK("xive", XiveSource, xive, TYPE_XIVE_NOTIFIER,
1218 XiveNotifier *),
02e3ff54
CLG
1219 DEFINE_PROP_END_OF_LIST(),
1220};
1221
1222static void xive_source_class_init(ObjectClass *klass, void *data)
1223{
1224 DeviceClass *dc = DEVICE_CLASS(klass);
1225
1226 dc->desc = "XIVE Interrupt Source";
4f67d30b 1227 device_class_set_props(dc, xive_source_properties);
02e3ff54
CLG
1228 dc->realize = xive_source_realize;
1229 dc->vmsd = &vmstate_xive_source;
878b2b48
GK
1230 /*
1231 * Reason: part of XIVE interrupt controller, needs to be wired up,
1232 * e.g. by spapr_xive_instance_init().
1233 */
1234 dc->user_creatable = false;
02e3ff54
CLG
1235}
1236
1237static const TypeInfo xive_source_info = {
1238 .name = TYPE_XIVE_SOURCE,
1239 .parent = TYPE_DEVICE,
1240 .instance_size = sizeof(XiveSource),
1241 .class_init = xive_source_class_init,
1242};
1243
e4ddaac6
CLG
1244/*
1245 * XiveEND helpers
1246 */
1247
1248void xive_end_queue_pic_print_info(XiveEND *end, uint32_t width, Monitor *mon)
1249{
13df9324 1250 uint64_t qaddr_base = xive_end_qaddr(end);
e4ddaac6
CLG
1251 uint32_t qsize = xive_get_field32(END_W0_QSIZE, end->w0);
1252 uint32_t qindex = xive_get_field32(END_W1_PAGE_OFF, end->w1);
1253 uint32_t qentries = 1 << (qsize + 10);
1254 int i;
1255
1256 /*
1257 * print out the [ (qindex - (width - 1)) .. (qindex + 1)] window
1258 */
1259 monitor_printf(mon, " [ ");
1260 qindex = (qindex - (width - 1)) & (qentries - 1);
1261 for (i = 0; i < width; i++) {
1262 uint64_t qaddr = qaddr_base + (qindex << 2);
1263 uint32_t qdata = -1;
1264
1265 if (dma_memory_read(&address_space_memory, qaddr, &qdata,
1266 sizeof(qdata))) {
1267 qemu_log_mask(LOG_GUEST_ERROR, "XIVE: failed to read EQ @0x%"
1268 HWADDR_PRIx "\n", qaddr);
1269 return;
1270 }
1271 monitor_printf(mon, "%s%08x ", i == width - 1 ? "^" : "",
1272 be32_to_cpu(qdata));
1273 qindex = (qindex + 1) & (qentries - 1);
1274 }
c5e760e0 1275 monitor_printf(mon, "]");
e4ddaac6
CLG
1276}
1277
1278void xive_end_pic_print_info(XiveEND *end, uint32_t end_idx, Monitor *mon)
1279{
13df9324 1280 uint64_t qaddr_base = xive_end_qaddr(end);
e4ddaac6
CLG
1281 uint32_t qindex = xive_get_field32(END_W1_PAGE_OFF, end->w1);
1282 uint32_t qgen = xive_get_field32(END_W1_GENERATION, end->w1);
1283 uint32_t qsize = xive_get_field32(END_W0_QSIZE, end->w0);
1284 uint32_t qentries = 1 << (qsize + 10);
1285
c5e760e0
CLG
1286 uint32_t nvt_blk = xive_get_field32(END_W6_NVT_BLOCK, end->w6);
1287 uint32_t nvt_idx = xive_get_field32(END_W6_NVT_INDEX, end->w6);
e4ddaac6 1288 uint8_t priority = xive_get_field32(END_W7_F0_PRIORITY, end->w7);
c5e760e0 1289 uint8_t pq;
e4ddaac6
CLG
1290
1291 if (!xive_end_is_valid(end)) {
1292 return;
1293 }
1294
c5e760e0
CLG
1295 pq = xive_get_field32(END_W1_ESn, end->w1);
1296
1297 monitor_printf(mon, " %08x %c%c %c%c%c%c%c%c%c prio:%d nvt:%02x/%04x",
1298 end_idx,
1299 pq & XIVE_ESB_VAL_P ? 'P' : '-',
1300 pq & XIVE_ESB_VAL_Q ? 'Q' : '-',
e4ddaac6
CLG
1301 xive_end_is_valid(end) ? 'v' : '-',
1302 xive_end_is_enqueue(end) ? 'q' : '-',
1303 xive_end_is_notify(end) ? 'n' : '-',
1304 xive_end_is_backlog(end) ? 'b' : '-',
1305 xive_end_is_escalate(end) ? 'e' : '-',
c5e760e0
CLG
1306 xive_end_is_uncond_escalation(end) ? 'u' : '-',
1307 xive_end_is_silent_escalation(end) ? 's' : '-',
1308 priority, nvt_blk, nvt_idx);
1309
1310 if (qaddr_base) {
1311 monitor_printf(mon, " eq:@%08"PRIx64"% 6d/%5d ^%d",
1312 qaddr_base, qindex, qentries, qgen);
1313 xive_end_queue_pic_print_info(end, 6, mon);
1314 }
1315 monitor_printf(mon, "\n");
e4ddaac6
CLG
1316}
1317
1318static void xive_end_enqueue(XiveEND *end, uint32_t data)
1319{
13df9324 1320 uint64_t qaddr_base = xive_end_qaddr(end);
e4ddaac6
CLG
1321 uint32_t qsize = xive_get_field32(END_W0_QSIZE, end->w0);
1322 uint32_t qindex = xive_get_field32(END_W1_PAGE_OFF, end->w1);
1323 uint32_t qgen = xive_get_field32(END_W1_GENERATION, end->w1);
1324
1325 uint64_t qaddr = qaddr_base + (qindex << 2);
1326 uint32_t qdata = cpu_to_be32((qgen << 31) | (data & 0x7fffffff));
1327 uint32_t qentries = 1 << (qsize + 10);
1328
1329 if (dma_memory_write(&address_space_memory, qaddr, &qdata, sizeof(qdata))) {
1330 qemu_log_mask(LOG_GUEST_ERROR, "XIVE: failed to write END data @0x%"
1331 HWADDR_PRIx "\n", qaddr);
1332 return;
1333 }
1334
1335 qindex = (qindex + 1) & (qentries - 1);
1336 if (qindex == 0) {
1337 qgen ^= 1;
1338 end->w1 = xive_set_field32(END_W1_GENERATION, end->w1, qgen);
1339 }
1340 end->w1 = xive_set_field32(END_W1_PAGE_OFF, end->w1, qindex);
1341}
1342
c5e760e0
CLG
1343void xive_end_eas_pic_print_info(XiveEND *end, uint32_t end_idx,
1344 Monitor *mon)
1345{
1346 XiveEAS *eas = (XiveEAS *) &end->w4;
1347 uint8_t pq;
1348
1349 if (!xive_end_is_escalate(end)) {
1350 return;
1351 }
1352
1353 pq = xive_get_field32(END_W1_ESe, end->w1);
1354
1355 monitor_printf(mon, " %08x %c%c %c%c end:%02x/%04x data:%08x\n",
1356 end_idx,
1357 pq & XIVE_ESB_VAL_P ? 'P' : '-',
1358 pq & XIVE_ESB_VAL_Q ? 'Q' : '-',
1359 xive_eas_is_valid(eas) ? 'V' : ' ',
1360 xive_eas_is_masked(eas) ? 'M' : ' ',
1361 (uint8_t) xive_get_field64(EAS_END_BLOCK, eas->w),
1362 (uint32_t) xive_get_field64(EAS_END_INDEX, eas->w),
1363 (uint32_t) xive_get_field64(EAS_END_DATA, eas->w));
1364}
1365
7ff7ea92
CLG
1366/*
1367 * XIVE Router (aka. Virtualization Controller or IVRE)
1368 */
1369
1370int xive_router_get_eas(XiveRouter *xrtr, uint8_t eas_blk, uint32_t eas_idx,
1371 XiveEAS *eas)
1372{
1373 XiveRouterClass *xrc = XIVE_ROUTER_GET_CLASS(xrtr);
1374
1375 return xrc->get_eas(xrtr, eas_blk, eas_idx, eas);
1376}
1377
e4ddaac6
CLG
1378int xive_router_get_end(XiveRouter *xrtr, uint8_t end_blk, uint32_t end_idx,
1379 XiveEND *end)
1380{
1381 XiveRouterClass *xrc = XIVE_ROUTER_GET_CLASS(xrtr);
1382
1383 return xrc->get_end(xrtr, end_blk, end_idx, end);
1384}
1385
1386int xive_router_write_end(XiveRouter *xrtr, uint8_t end_blk, uint32_t end_idx,
1387 XiveEND *end, uint8_t word_number)
1388{
1389 XiveRouterClass *xrc = XIVE_ROUTER_GET_CLASS(xrtr);
1390
1391 return xrc->write_end(xrtr, end_blk, end_idx, end, word_number);
1392}
1393
af53dbf6
CLG
1394int xive_router_get_nvt(XiveRouter *xrtr, uint8_t nvt_blk, uint32_t nvt_idx,
1395 XiveNVT *nvt)
1396{
1397 XiveRouterClass *xrc = XIVE_ROUTER_GET_CLASS(xrtr);
1398
1399 return xrc->get_nvt(xrtr, nvt_blk, nvt_idx, nvt);
1400}
1401
1402int xive_router_write_nvt(XiveRouter *xrtr, uint8_t nvt_blk, uint32_t nvt_idx,
1403 XiveNVT *nvt, uint8_t word_number)
1404{
1405 XiveRouterClass *xrc = XIVE_ROUTER_GET_CLASS(xrtr);
1406
1407 return xrc->write_nvt(xrtr, nvt_blk, nvt_idx, nvt, word_number);
1408}
1409
f22f56dd
CLG
1410static int xive_router_get_block_id(XiveRouter *xrtr)
1411{
1412 XiveRouterClass *xrc = XIVE_ROUTER_GET_CLASS(xrtr);
1413
1414 return xrc->get_block_id(xrtr);
1415}
1416
53981dd5
GK
1417static void xive_router_realize(DeviceState *dev, Error **errp)
1418{
1419 XiveRouter *xrtr = XIVE_ROUTER(dev);
1420
1421 assert(xrtr->xfb);
1422}
1423
d514c48d 1424/*
fe9a9d52 1425 * Encode the HW CAM line in the block group mode format :
d514c48d 1426 *
fe9a9d52 1427 * chip << 19 | 0000000 0 0001 thread (7Bit)
d514c48d 1428 */
f22f56dd 1429static uint32_t xive_tctx_hw_cam_line(XivePresenter *xptr, XiveTCTX *tctx)
d514c48d
CLG
1430{
1431 CPUPPCState *env = &POWERPC_CPU(tctx->cs)->env;
1432 uint32_t pir = env->spr_cb[SPR_PIR].default_value;
f22f56dd 1433 uint8_t blk = xive_router_get_block_id(XIVE_ROUTER(xptr));
d514c48d 1434
f22f56dd 1435 return xive_nvt_cam_line(blk, 1 << 7 | (pir & 0x7f));
d514c48d
CLG
1436}
1437
af53dbf6
CLG
1438/*
1439 * The thread context register words are in big-endian format.
1440 */
13bee852
CLG
1441int xive_presenter_tctx_match(XivePresenter *xptr, XiveTCTX *tctx,
1442 uint8_t format,
1443 uint8_t nvt_blk, uint32_t nvt_idx,
1444 bool cam_ignore, uint32_t logic_serv)
af53dbf6
CLG
1445{
1446 uint32_t cam = xive_nvt_cam_line(nvt_blk, nvt_idx);
d514c48d 1447 uint32_t qw3w2 = xive_tctx_word2(&tctx->regs[TM_QW3_HV_PHYS]);
af53dbf6
CLG
1448 uint32_t qw2w2 = xive_tctx_word2(&tctx->regs[TM_QW2_HV_POOL]);
1449 uint32_t qw1w2 = xive_tctx_word2(&tctx->regs[TM_QW1_OS]);
1450 uint32_t qw0w2 = xive_tctx_word2(&tctx->regs[TM_QW0_USER]);
1451
1452 /*
1453 * TODO (PowerNV): ignore mode. The low order bits of the NVT
1454 * identifier are ignored in the "CAM" match.
1455 */
1456
1457 if (format == 0) {
1458 if (cam_ignore == true) {
1459 /*
1460 * F=0 & i=1: Logical server notification (bits ignored at
1461 * the end of the NVT identifier)
1462 */
1463 qemu_log_mask(LOG_UNIMP, "XIVE: no support for LS NVT %x/%x\n",
1464 nvt_blk, nvt_idx);
1465 return -1;
1466 }
1467
1468 /* F=0 & i=0: Specific NVT notification */
1469
d514c48d
CLG
1470 /* PHYS ring */
1471 if ((be32_to_cpu(qw3w2) & TM_QW3W2_VT) &&
f22f56dd 1472 cam == xive_tctx_hw_cam_line(xptr, tctx)) {
d514c48d
CLG
1473 return TM_QW3_HV_PHYS;
1474 }
af53dbf6
CLG
1475
1476 /* HV POOL ring */
1477 if ((be32_to_cpu(qw2w2) & TM_QW2W2_VP) &&
1478 cam == xive_get_field32(TM_QW2W2_POOL_CAM, qw2w2)) {
1479 return TM_QW2_HV_POOL;
1480 }
1481
1482 /* OS ring */
1483 if ((be32_to_cpu(qw1w2) & TM_QW1W2_VO) &&
1484 cam == xive_get_field32(TM_QW1W2_OS_CAM, qw1w2)) {
1485 return TM_QW1_OS;
1486 }
1487 } else {
1488 /* F=1 : User level Event-Based Branch (EBB) notification */
1489
1490 /* USER ring */
1491 if ((be32_to_cpu(qw1w2) & TM_QW1W2_VO) &&
1492 (cam == xive_get_field32(TM_QW1W2_OS_CAM, qw1w2)) &&
1493 (be32_to_cpu(qw0w2) & TM_QW0W2_VU) &&
1494 (logic_serv == xive_get_field32(TM_QW0W2_LOGIC_SERV, qw0w2))) {
1495 return TM_QW0_USER;
1496 }
1497 }
1498 return -1;
1499}
1500
af53dbf6
CLG
1501/*
1502 * This is our simple Xive Presenter Engine model. It is merged in the
1503 * Router as it does not require an extra object.
1504 *
1505 * It receives notification requests sent by the IVRE to find one
1506 * matching NVT (or more) dispatched on the processor threads. In case
1507 * of a single NVT notification, the process is abreviated and the
1508 * thread is signaled if a match is found. In case of a logical server
1509 * notification (bits ignored at the end of the NVT identifier), the
1510 * IVPE and IVRE select a winning thread using different filters. This
1511 * involves 2 or 3 exchanges on the PowerBus that the model does not
1512 * support.
1513 *
1514 * The parameters represent what is sent on the PowerBus
1515 */
53981dd5 1516static bool xive_presenter_notify(XiveFabric *xfb, uint8_t format,
af53dbf6
CLG
1517 uint8_t nvt_blk, uint32_t nvt_idx,
1518 bool cam_ignore, uint8_t priority,
1519 uint32_t logic_serv)
1520{
5662f291 1521 XiveFabricClass *xfc = XIVE_FABRIC_GET_CLASS(xfb);
af53dbf6 1522 XiveTCTXMatch match = { .tctx = NULL, .ring = 0 };
5662f291 1523 int count;
af53dbf6 1524
5662f291
CLG
1525 /*
1526 * Ask the machine to scan the interrupt controllers for a match
1527 */
1528 count = xfc->match_nvt(xfb, format, nvt_blk, nvt_idx, cam_ignore,
1529 priority, logic_serv, &match);
1530 if (count < 0) {
1531 return false;
1532 }
1533
1534 /* handle CPU exception delivery */
1535 if (count) {
4e960974 1536 trace_xive_presenter_notify(nvt_blk, nvt_idx, match.ring);
a5b841f1 1537 xive_tctx_ipb_update(match.tctx, match.ring, priority_to_ipb(priority));
af53dbf6
CLG
1538 }
1539
5662f291 1540 return !!count;
af53dbf6
CLG
1541}
1542
53e93492
CLG
1543/*
1544 * Notification using the END ESe/ESn bit (Event State Buffer for
4b160fad 1545 * escalation and notification). Provide further coalescing in the
53e93492
CLG
1546 * Router.
1547 */
1548static bool xive_router_end_es_notify(XiveRouter *xrtr, uint8_t end_blk,
1549 uint32_t end_idx, XiveEND *end,
1550 uint32_t end_esmask)
1551{
1552 uint8_t pq = xive_get_field32(end_esmask, end->w1);
1553 bool notify = xive_esb_trigger(&pq);
1554
1555 if (pq != xive_get_field32(end_esmask, end->w1)) {
1556 end->w1 = xive_set_field32(end_esmask, end->w1, pq);
1557 xive_router_write_end(xrtr, end_blk, end_idx, end, 1);
1558 }
1559
1560 /* ESe/n[Q]=1 : end of notification */
1561 return notify;
1562}
1563
e4ddaac6
CLG
1564/*
1565 * An END trigger can come from an event trigger (IPI or HW) or from
1566 * another chip. We don't model the PowerBus but the END trigger
1567 * message has the same parameters than in the function below.
1568 */
1569static void xive_router_end_notify(XiveRouter *xrtr, uint8_t end_blk,
1570 uint32_t end_idx, uint32_t end_data)
1571{
1572 XiveEND end;
1573 uint8_t priority;
1574 uint8_t format;
52c5acf0
CLG
1575 uint8_t nvt_blk;
1576 uint32_t nvt_idx;
1577 XiveNVT nvt;
1578 bool found;
e4ddaac6
CLG
1579
1580 /* END cache lookup */
1581 if (xive_router_get_end(xrtr, end_blk, end_idx, &end)) {
1582 qemu_log_mask(LOG_GUEST_ERROR, "XIVE: No END %x/%x\n", end_blk,
1583 end_idx);
1584 return;
1585 }
1586
1587 if (!xive_end_is_valid(&end)) {
4e960974 1588 trace_xive_router_end_notify(end_blk, end_idx, end_data);
e4ddaac6
CLG
1589 qemu_log_mask(LOG_GUEST_ERROR, "XIVE: END %x/%x is invalid\n",
1590 end_blk, end_idx);
1591 return;
1592 }
1593
1594 if (xive_end_is_enqueue(&end)) {
1595 xive_end_enqueue(&end, end_data);
1596 /* Enqueuing event data modifies the EQ toggle and index */
1597 xive_router_write_end(xrtr, end_blk, end_idx, &end, 1);
1598 }
1599
ad31e2d2
CLG
1600 /*
1601 * When the END is silent, we skip the notification part.
1602 */
1603 if (xive_end_is_silent_escalation(&end)) {
1604 goto do_escalation;
1605 }
1606
e4ddaac6
CLG
1607 /*
1608 * The W7 format depends on the F bit in W6. It defines the type
1609 * of the notification :
1610 *
1611 * F=0 : single or multiple NVT notification
1612 * F=1 : User level Event-Based Branch (EBB) notification, no
1613 * priority
1614 */
1615 format = xive_get_field32(END_W6_FORMAT_BIT, end.w6);
1616 priority = xive_get_field32(END_W7_F0_PRIORITY, end.w7);
1617
1618 /* The END is masked */
1619 if (format == 0 && priority == 0xff) {
1620 return;
1621 }
1622
1623 /*
1624 * Check the END ESn (Event State Buffer for notification) for
4b160fad 1625 * even further coalescing in the Router
e4ddaac6
CLG
1626 */
1627 if (!xive_end_is_notify(&end)) {
002686be 1628 /* ESn[Q]=1 : end of notification */
53e93492
CLG
1629 if (!xive_router_end_es_notify(xrtr, end_blk, end_idx,
1630 &end, END_W1_ESn)) {
002686be
CLG
1631 return;
1632 }
e4ddaac6
CLG
1633 }
1634
1635 /*
1636 * Follows IVPE notification
1637 */
52c5acf0
CLG
1638 nvt_blk = xive_get_field32(END_W6_NVT_BLOCK, end.w6);
1639 nvt_idx = xive_get_field32(END_W6_NVT_INDEX, end.w6);
1640
1641 /* NVT cache lookup */
1642 if (xive_router_get_nvt(xrtr, nvt_blk, nvt_idx, &nvt)) {
1643 qemu_log_mask(LOG_GUEST_ERROR, "XIVE: no NVT %x/%x\n",
1644 nvt_blk, nvt_idx);
1645 return;
1646 }
1647
1648 if (!xive_nvt_is_valid(&nvt)) {
1649 qemu_log_mask(LOG_GUEST_ERROR, "XIVE: NVT %x/%x is invalid\n",
1650 nvt_blk, nvt_idx);
1651 return;
1652 }
1653
53981dd5 1654 found = xive_presenter_notify(xrtr->xfb, format, nvt_blk, nvt_idx,
af53dbf6
CLG
1655 xive_get_field32(END_W7_F0_IGNORE, end.w7),
1656 priority,
1657 xive_get_field32(END_W7_F1_LOG_SERVER_ID, end.w7));
1658
1659 /* TODO: Auto EOI. */
52c5acf0
CLG
1660
1661 if (found) {
1662 return;
1663 }
1664
1665 /*
1666 * If no matching NVT is dispatched on a HW thread :
1667 * - specific VP: update the NVT structure if backlog is activated
1668 * - logical server : forward request to IVPE (not supported)
1669 */
1670 if (xive_end_is_backlog(&end)) {
516883c2
CLG
1671 uint8_t ipb;
1672
52c5acf0
CLG
1673 if (format == 1) {
1674 qemu_log_mask(LOG_GUEST_ERROR,
1675 "XIVE: END %x/%x invalid config: F1 & backlog\n",
1676 end_blk, end_idx);
1677 return;
1678 }
516883c2
CLG
1679 /*
1680 * Record the IPB in the associated NVT structure for later
1681 * use. The presenter will resend the interrupt when the vCPU
1682 * is dispatched again on a HW thread.
1683 */
1684 ipb = xive_get_field32(NVT_W4_IPB, nvt.w4) | priority_to_ipb(priority);
1685 nvt.w4 = xive_set_field32(NVT_W4_IPB, nvt.w4, ipb);
52c5acf0
CLG
1686 xive_router_write_nvt(xrtr, nvt_blk, nvt_idx, &nvt, 4);
1687
1688 /*
1689 * On HW, follows a "Broadcast Backlog" to IVPEs
1690 */
1691 }
b4e30666 1692
ad31e2d2 1693do_escalation:
b4e30666
CLG
1694 /*
1695 * If activated, escalate notification using the ESe PQ bits and
1696 * the EAS in w4-5
1697 */
1698 if (!xive_end_is_escalate(&end)) {
1699 return;
1700 }
1701
53e93492
CLG
1702 /*
1703 * Check the END ESe (Event State Buffer for escalation) for even
4b160fad 1704 * further coalescing in the Router
53e93492
CLG
1705 */
1706 if (!xive_end_is_uncond_escalation(&end)) {
1707 /* ESe[Q]=1 : end of notification */
1708 if (!xive_router_end_es_notify(xrtr, end_blk, end_idx,
1709 &end, END_W1_ESe)) {
1710 return;
1711 }
1712 }
1713
4e960974
CLG
1714 trace_xive_router_end_escalate(end_blk, end_idx,
1715 (uint8_t) xive_get_field32(END_W4_ESC_END_BLOCK, end.w4),
1716 (uint32_t) xive_get_field32(END_W4_ESC_END_INDEX, end.w4),
1717 (uint32_t) xive_get_field32(END_W5_ESC_END_DATA, end.w5));
b4e30666
CLG
1718 /*
1719 * The END trigger becomes an Escalation trigger
1720 */
1721 xive_router_end_notify(xrtr,
1722 xive_get_field32(END_W4_ESC_END_BLOCK, end.w4),
1723 xive_get_field32(END_W4_ESC_END_INDEX, end.w4),
1724 xive_get_field32(END_W5_ESC_END_DATA, end.w5));
e4ddaac6
CLG
1725}
1726
a58a18ad 1727void xive_router_notify(XiveNotifier *xn, uint32_t lisn)
7ff7ea92
CLG
1728{
1729 XiveRouter *xrtr = XIVE_ROUTER(xn);
106695ab
CLG
1730 uint8_t eas_blk = XIVE_EAS_BLOCK(lisn);
1731 uint32_t eas_idx = XIVE_EAS_INDEX(lisn);
7ff7ea92
CLG
1732 XiveEAS eas;
1733
1734 /* EAS cache lookup */
1735 if (xive_router_get_eas(xrtr, eas_blk, eas_idx, &eas)) {
1736 qemu_log_mask(LOG_GUEST_ERROR, "XIVE: Unknown LISN %x\n", lisn);
1737 return;
1738 }
1739
1740 /*
1741 * The IVRE checks the State Bit Cache at this point. We skip the
1742 * SBC lookup because the state bits of the sources are modeled
1743 * internally in QEMU.
1744 */
1745
1746 if (!xive_eas_is_valid(&eas)) {
1747 qemu_log_mask(LOG_GUEST_ERROR, "XIVE: invalid LISN %x\n", lisn);
1748 return;
1749 }
1750
1751 if (xive_eas_is_masked(&eas)) {
1752 /* Notification completed */
1753 return;
1754 }
e4ddaac6
CLG
1755
1756 /*
1757 * The event trigger becomes an END trigger
1758 */
1759 xive_router_end_notify(xrtr,
1760 xive_get_field64(EAS_END_BLOCK, eas.w),
1761 xive_get_field64(EAS_END_INDEX, eas.w),
1762 xive_get_field64(EAS_END_DATA, eas.w));
7ff7ea92
CLG
1763}
1764
d1214b81
GK
1765static Property xive_router_properties[] = {
1766 DEFINE_PROP_LINK("xive-fabric", XiveRouter, xfb,
1767 TYPE_XIVE_FABRIC, XiveFabric *),
1768 DEFINE_PROP_END_OF_LIST(),
1769};
1770
7ff7ea92
CLG
1771static void xive_router_class_init(ObjectClass *klass, void *data)
1772{
1773 DeviceClass *dc = DEVICE_CLASS(klass);
1774 XiveNotifierClass *xnc = XIVE_NOTIFIER_CLASS(klass);
1775
1776 dc->desc = "XIVE Router Engine";
4f67d30b 1777 device_class_set_props(dc, xive_router_properties);
53981dd5
GK
1778 /* Parent is SysBusDeviceClass. No need to call its realize hook */
1779 dc->realize = xive_router_realize;
7ff7ea92
CLG
1780 xnc->notify = xive_router_notify;
1781}
1782
1783static const TypeInfo xive_router_info = {
1784 .name = TYPE_XIVE_ROUTER,
1785 .parent = TYPE_SYS_BUS_DEVICE,
1786 .abstract = true,
d1214b81 1787 .instance_size = sizeof(XiveRouter),
7ff7ea92
CLG
1788 .class_size = sizeof(XiveRouterClass),
1789 .class_init = xive_router_class_init,
1790 .interfaces = (InterfaceInfo[]) {
1791 { TYPE_XIVE_NOTIFIER },
13bee852 1792 { TYPE_XIVE_PRESENTER },
7ff7ea92
CLG
1793 { }
1794 }
1795};
1796
1797void xive_eas_pic_print_info(XiveEAS *eas, uint32_t lisn, Monitor *mon)
1798{
1799 if (!xive_eas_is_valid(eas)) {
1800 return;
1801 }
1802
1803 monitor_printf(mon, " %08x %s end:%02x/%04x data:%08x\n",
1804 lisn, xive_eas_is_masked(eas) ? "M" : " ",
1805 (uint8_t) xive_get_field64(EAS_END_BLOCK, eas->w),
1806 (uint32_t) xive_get_field64(EAS_END_INDEX, eas->w),
1807 (uint32_t) xive_get_field64(EAS_END_DATA, eas->w));
1808}
1809
002686be
CLG
1810/*
1811 * END ESB MMIO loads
1812 */
1813static uint64_t xive_end_source_read(void *opaque, hwaddr addr, unsigned size)
1814{
1815 XiveENDSource *xsrc = XIVE_END_SOURCE(opaque);
1816 uint32_t offset = addr & 0xFFF;
1817 uint8_t end_blk;
1818 uint32_t end_idx;
1819 XiveEND end;
1820 uint32_t end_esmask;
1821 uint8_t pq;
1822 uint64_t ret = -1;
1823
f22f56dd
CLG
1824 /*
1825 * The block id should be deduced from the load address on the END
1826 * ESB MMIO but our model only supports a single block per XIVE chip.
1827 */
1828 end_blk = xive_router_get_block_id(xsrc->xrtr);
002686be
CLG
1829 end_idx = addr >> (xsrc->esb_shift + 1);
1830
4e960974
CLG
1831 trace_xive_end_source_read(end_blk, end_idx, addr);
1832
002686be
CLG
1833 if (xive_router_get_end(xsrc->xrtr, end_blk, end_idx, &end)) {
1834 qemu_log_mask(LOG_GUEST_ERROR, "XIVE: No END %x/%x\n", end_blk,
1835 end_idx);
1836 return -1;
1837 }
1838
1839 if (!xive_end_is_valid(&end)) {
1840 qemu_log_mask(LOG_GUEST_ERROR, "XIVE: END %x/%x is invalid\n",
1841 end_blk, end_idx);
1842 return -1;
1843 }
1844
1845 end_esmask = addr_is_even(addr, xsrc->esb_shift) ? END_W1_ESn : END_W1_ESe;
1846 pq = xive_get_field32(end_esmask, end.w1);
1847
1848 switch (offset) {
1849 case XIVE_ESB_LOAD_EOI ... XIVE_ESB_LOAD_EOI + 0x7FF:
1850 ret = xive_esb_eoi(&pq);
1851
1852 /* Forward the source event notification for routing ?? */
1853 break;
1854
1855 case XIVE_ESB_GET ... XIVE_ESB_GET + 0x3FF:
1856 ret = pq;
1857 break;
1858
1859 case XIVE_ESB_SET_PQ_00 ... XIVE_ESB_SET_PQ_00 + 0x0FF:
1860 case XIVE_ESB_SET_PQ_01 ... XIVE_ESB_SET_PQ_01 + 0x0FF:
1861 case XIVE_ESB_SET_PQ_10 ... XIVE_ESB_SET_PQ_10 + 0x0FF:
1862 case XIVE_ESB_SET_PQ_11 ... XIVE_ESB_SET_PQ_11 + 0x0FF:
1863 ret = xive_esb_set(&pq, (offset >> 8) & 0x3);
1864 break;
1865 default:
1866 qemu_log_mask(LOG_GUEST_ERROR, "XIVE: invalid END ESB load addr %d\n",
1867 offset);
1868 return -1;
1869 }
1870
1871 if (pq != xive_get_field32(end_esmask, end.w1)) {
1872 end.w1 = xive_set_field32(end_esmask, end.w1, pq);
1873 xive_router_write_end(xsrc->xrtr, end_blk, end_idx, &end, 1);
1874 }
1875
1876 return ret;
1877}
1878
1879/*
1880 * END ESB MMIO stores are invalid
1881 */
1882static void xive_end_source_write(void *opaque, hwaddr addr,
1883 uint64_t value, unsigned size)
1884{
1885 qemu_log_mask(LOG_GUEST_ERROR, "XIVE: invalid ESB write addr 0x%"
1886 HWADDR_PRIx"\n", addr);
1887}
1888
1889static const MemoryRegionOps xive_end_source_ops = {
1890 .read = xive_end_source_read,
1891 .write = xive_end_source_write,
1892 .endianness = DEVICE_BIG_ENDIAN,
1893 .valid = {
1894 .min_access_size = 8,
1895 .max_access_size = 8,
1896 },
1897 .impl = {
1898 .min_access_size = 8,
1899 .max_access_size = 8,
1900 },
1901};
1902
1903static void xive_end_source_realize(DeviceState *dev, Error **errp)
1904{
1905 XiveENDSource *xsrc = XIVE_END_SOURCE(dev);
002686be 1906
0ab2316e 1907 assert(xsrc->xrtr);
002686be
CLG
1908
1909 if (!xsrc->nr_ends) {
1910 error_setg(errp, "Number of interrupt needs to be greater than 0");
1911 return;
1912 }
1913
1914 if (xsrc->esb_shift != XIVE_ESB_4K &&
1915 xsrc->esb_shift != XIVE_ESB_64K) {
1916 error_setg(errp, "Invalid ESB shift setting");
1917 return;
1918 }
1919
1920 /*
1921 * Each END is assigned an even/odd pair of MMIO pages, the even page
1922 * manages the ESn field while the odd page manages the ESe field.
1923 */
1924 memory_region_init_io(&xsrc->esb_mmio, OBJECT(xsrc),
1925 &xive_end_source_ops, xsrc, "xive.end",
1926 (1ull << (xsrc->esb_shift + 1)) * xsrc->nr_ends);
1927}
1928
1929static Property xive_end_source_properties[] = {
002686be
CLG
1930 DEFINE_PROP_UINT32("nr-ends", XiveENDSource, nr_ends, 0),
1931 DEFINE_PROP_UINT32("shift", XiveENDSource, esb_shift, XIVE_ESB_64K),
0ab2316e
GK
1932 DEFINE_PROP_LINK("xive", XiveENDSource, xrtr, TYPE_XIVE_ROUTER,
1933 XiveRouter *),
002686be
CLG
1934 DEFINE_PROP_END_OF_LIST(),
1935};
1936
1937static void xive_end_source_class_init(ObjectClass *klass, void *data)
1938{
1939 DeviceClass *dc = DEVICE_CLASS(klass);
1940
1941 dc->desc = "XIVE END Source";
4f67d30b 1942 device_class_set_props(dc, xive_end_source_properties);
002686be 1943 dc->realize = xive_end_source_realize;
878b2b48
GK
1944 /*
1945 * Reason: part of XIVE interrupt controller, needs to be wired up,
1946 * e.g. by spapr_xive_instance_init().
1947 */
1948 dc->user_creatable = false;
002686be
CLG
1949}
1950
1951static const TypeInfo xive_end_source_info = {
1952 .name = TYPE_XIVE_END_SOURCE,
1953 .parent = TYPE_DEVICE,
1954 .instance_size = sizeof(XiveENDSource),
1955 .class_init = xive_end_source_class_init,
1956};
1957
5e79b155 1958/*
6bf6f3a1 1959 * XIVE Notifier
5e79b155 1960 */
6bf6f3a1 1961static const TypeInfo xive_notifier_info = {
5e79b155
CLG
1962 .name = TYPE_XIVE_NOTIFIER,
1963 .parent = TYPE_INTERFACE,
1964 .class_size = sizeof(XiveNotifierClass),
1965};
1966
13bee852
CLG
1967/*
1968 * XIVE Presenter
1969 */
1970static const TypeInfo xive_presenter_info = {
1971 .name = TYPE_XIVE_PRESENTER,
1972 .parent = TYPE_INTERFACE,
1973 .class_size = sizeof(XivePresenterClass),
1974};
1975
d3eb47a2
CLG
1976/*
1977 * XIVE Fabric
1978 */
1979static const TypeInfo xive_fabric_info = {
1980 .name = TYPE_XIVE_FABRIC,
1981 .parent = TYPE_INTERFACE,
1982 .class_size = sizeof(XiveFabricClass),
1983};
1984
02e3ff54
CLG
1985static void xive_register_types(void)
1986{
d3eb47a2 1987 type_register_static(&xive_fabric_info);
02e3ff54 1988 type_register_static(&xive_source_info);
6bf6f3a1 1989 type_register_static(&xive_notifier_info);
13bee852 1990 type_register_static(&xive_presenter_info);
7ff7ea92 1991 type_register_static(&xive_router_info);
002686be 1992 type_register_static(&xive_end_source_info);
207d9fe9 1993 type_register_static(&xive_tctx_info);
02e3ff54
CLG
1994}
1995
1996type_init(xive_register_types)
This page took 0.457002 seconds and 4 git commands to generate.