]> git.ipfire.org Git - thirdparty/qemu.git/blob - include/hw/ppc/xive.h
Move QOM typedefs and add missing includes
[thirdparty/qemu.git] / include / hw / ppc / xive.h
1 /*
2 * QEMU PowerPC XIVE interrupt controller model
3 *
4 *
5 * The POWER9 processor comes with a new interrupt controller, called
6 * XIVE as "eXternal Interrupt Virtualization Engine".
7 *
8 * = Overall architecture
9 *
10 *
11 * XIVE Interrupt Controller
12 * +------------------------------------+ IPIs
13 * | +---------+ +---------+ +--------+ | +-------+
14 * | |VC | |CQ | |PC |----> | CORES |
15 * | | esb | | | | |----> | |
16 * | | eas | | Bridge | | tctx |----> | |
17 * | |SC end | | | | nvt | | | |
18 * +------+ | +---------+ +----+----+ +--------+ | +-+-+-+-+
19 * | RAM | +------------------|-----------------+ | | |
20 * | | | | | |
21 * | | | | | |
22 * | | +--------------------v------------------------v-v-v--+ other
23 * | <--+ Power Bus +--> chips
24 * | esb | +---------+-----------------------+------------------+
25 * | eas | | |
26 * | end | +--|------+ |
27 * | nvt | +----+----+ | +----+----+
28 * +------+ |SC | | |SC |
29 * | | | | |
30 * | PQ-bits | | | PQ-bits |
31 * | local |-+ | in VC |
32 * +---------+ +---------+
33 * PCIe NX,NPU,CAPI
34 *
35 * SC: Source Controller (aka. IVSE)
36 * VC: Virtualization Controller (aka. IVRE)
37 * PC: Presentation Controller (aka. IVPE)
38 * CQ: Common Queue (Bridge)
39 *
40 * PQ-bits: 2 bits source state machine (P:pending Q:queued)
41 * esb: Event State Buffer (Array of PQ bits in an IVSE)
42 * eas: Event Assignment Structure
43 * end: Event Notification Descriptor
44 * nvt: Notification Virtual Target
45 * tctx: Thread interrupt Context
46 *
47 *
48 * The XIVE IC is composed of three sub-engines :
49 *
50 * - Interrupt Virtualization Source Engine (IVSE), or Source
51 * Controller (SC). These are found in PCI PHBs, in the PSI host
52 * bridge controller, but also inside the main controller for the
53 * core IPIs and other sub-chips (NX, CAP, NPU) of the
54 * chip/processor. They are configured to feed the IVRE with events.
55 *
56 * - Interrupt Virtualization Routing Engine (IVRE) or Virtualization
57 * Controller (VC). Its job is to match an event source with an
58 * Event Notification Descriptor (END).
59 *
60 * - Interrupt Virtualization Presentation Engine (IVPE) or
61 * Presentation Controller (PC). It maintains the interrupt context
62 * state of each thread and handles the delivery of the external
63 * exception to the thread.
64 *
65 * In XIVE 1.0, the sub-engines used to be referred as:
66 *
67 * SC Source Controller
68 * VC Virtualization Controller
69 * PC Presentation Controller
70 * CQ Common Queue (PowerBUS Bridge)
71 *
72 *
73 * = XIVE internal tables
74 *
75 * Each of the sub-engines uses a set of tables to redirect exceptions
76 * from event sources to CPU threads.
77 *
78 * +-------+
79 * User or OS | EQ |
80 * or +------>|entries|
81 * Hypervisor | | .. |
82 * Memory | +-------+
83 * | ^
84 * | |
85 * +-------------------------------------------------+
86 * | |
87 * Hypervisor +------+ +---+--+ +---+--+ +------+
88 * Memory | ESB | | EAT | | ENDT | | NVTT |
89 * (skiboot) +----+-+ +----+-+ +----+-+ +------+
90 * ^ | ^ | ^ | ^
91 * | | | | | | |
92 * +-------------------------------------------------+
93 * | | | | | | |
94 * | | | | | | |
95 * +----|--|--------|--|--------|--|-+ +-|-----+ +------+
96 * | | | | | | | | | | tctx| |Thread|
97 * IPI or --> | + v + v + v |---| + .. |-----> |
98 * HW events --> | | | | | |
99 * IVSE | IVRE | | IVPE | +------+
100 * +---------------------------------+ +-------+
101 *
102 *
103 *
104 * The IVSE have a 2-bits state machine, P for pending and Q for queued,
105 * for each source that allows events to be triggered. They are stored in
106 * an Event State Buffer (ESB) array and can be controlled by MMIOs.
107 *
108 * If the event is let through, the IVRE looks up in the Event Assignment
109 * Structure (EAS) table for an Event Notification Descriptor (END)
110 * configured for the source. Each Event Notification Descriptor defines
111 * a notification path to a CPU and an in-memory Event Queue, in which
112 * will be enqueued an EQ data for the OS to pull.
113 *
114 * The IVPE determines if a Notification Virtual Target (NVT) can
115 * handle the event by scanning the thread contexts of the VCPUs
116 * dispatched on the processor HW threads. It maintains the state of
117 * the thread interrupt context (TCTX) of each thread in a NVT table.
118 *
119 * = Acronyms
120 *
121 * Description In XIVE 1.0, used to be referred as
122 *
123 * EAS Event Assignment Structure IVE Interrupt Virt. Entry
124 * EAT Event Assignment Table IVT Interrupt Virt. Table
125 * ENDT Event Notif. Descriptor Table EQDT Event Queue Desc. Table
126 * EQ Event Queue same
127 * ESB Event State Buffer SBE State Bit Entry
128 * NVT Notif. Virtual Target VPD Virtual Processor Desc.
129 * NVTT Notif. Virtual Target Table VPDT Virtual Processor Desc. Table
130 * TCTX Thread interrupt Context
131 *
132 *
133 * Copyright (c) 2017-2018, IBM Corporation.
134 *
135 * This code is licensed under the GPL version 2 or later. See the
136 * COPYING file in the top-level directory.
137 *
138 */
139
140 #ifndef PPC_XIVE_H
141 #define PPC_XIVE_H
142
143 #include "sysemu/kvm.h"
144 #include "hw/sysbus.h"
145 #include "hw/ppc/xive_regs.h"
146 #include "qom/object.h"
147
148 /*
149 * XIVE Notifier (Interface between Source and Router)
150 */
151
152 typedef struct XiveNotifier XiveNotifier;
153
154 #define TYPE_XIVE_NOTIFIER "xive-notifier"
155 #define XIVE_NOTIFIER(obj) \
156 INTERFACE_CHECK(XiveNotifier, (obj), TYPE_XIVE_NOTIFIER)
157 typedef struct XiveNotifierClass XiveNotifierClass;
158 #define XIVE_NOTIFIER_CLASS(klass) \
159 OBJECT_CLASS_CHECK(XiveNotifierClass, (klass), TYPE_XIVE_NOTIFIER)
160 #define XIVE_NOTIFIER_GET_CLASS(obj) \
161 OBJECT_GET_CLASS(XiveNotifierClass, (obj), TYPE_XIVE_NOTIFIER)
162
163 struct XiveNotifierClass {
164 InterfaceClass parent;
165 void (*notify)(XiveNotifier *xn, uint32_t lisn);
166 };
167
168 /*
169 * XIVE Interrupt Source
170 */
171
172 #define TYPE_XIVE_SOURCE "xive-source"
173 typedef struct XiveSource XiveSource;
174 #define XIVE_SOURCE(obj) OBJECT_CHECK(XiveSource, (obj), TYPE_XIVE_SOURCE)
175
176 /*
177 * XIVE Interrupt Source characteristics, which define how the ESB are
178 * controlled.
179 */
180 #define XIVE_SRC_H_INT_ESB 0x1 /* ESB managed with hcall H_INT_ESB */
181 #define XIVE_SRC_STORE_EOI 0x2 /* Store EOI supported */
182
183 struct XiveSource {
184 DeviceState parent;
185
186 /* IRQs */
187 uint32_t nr_irqs;
188 unsigned long *lsi_map;
189
190 /* PQ bits and LSI assertion bit */
191 uint8_t *status;
192
193 /* ESB memory region */
194 uint64_t esb_flags;
195 uint32_t esb_shift;
196 MemoryRegion esb_mmio;
197 MemoryRegion esb_mmio_emulated;
198
199 /* KVM support */
200 void *esb_mmap;
201 MemoryRegion esb_mmio_kvm;
202
203 XiveNotifier *xive;
204 };
205
206 /*
207 * ESB MMIO setting. Can be one page, for both source triggering and
208 * source management, or two different pages. See below for magic
209 * values.
210 */
211 #define XIVE_ESB_4K 12 /* PSI HB only */
212 #define XIVE_ESB_4K_2PAGE 13
213 #define XIVE_ESB_64K 16
214 #define XIVE_ESB_64K_2PAGE 17
215
216 static inline bool xive_source_esb_has_2page(XiveSource *xsrc)
217 {
218 return xsrc->esb_shift == XIVE_ESB_64K_2PAGE ||
219 xsrc->esb_shift == XIVE_ESB_4K_2PAGE;
220 }
221
222 static inline size_t xive_source_esb_len(XiveSource *xsrc)
223 {
224 return (1ull << xsrc->esb_shift) * xsrc->nr_irqs;
225 }
226
227 /* The trigger page is always the first/even page */
228 static inline hwaddr xive_source_esb_page(XiveSource *xsrc, uint32_t srcno)
229 {
230 assert(srcno < xsrc->nr_irqs);
231 return (1ull << xsrc->esb_shift) * srcno;
232 }
233
234 /* In a two pages ESB MMIO setting, the odd page is for management */
235 static inline hwaddr xive_source_esb_mgmt(XiveSource *xsrc, int srcno)
236 {
237 hwaddr addr = xive_source_esb_page(xsrc, srcno);
238
239 if (xive_source_esb_has_2page(xsrc)) {
240 addr += (1 << (xsrc->esb_shift - 1));
241 }
242
243 return addr;
244 }
245
246 /*
247 * Each interrupt source has a 2-bit state machine which can be
248 * controlled by MMIO. P indicates that an interrupt is pending (has
249 * been sent to a queue and is waiting for an EOI). Q indicates that
250 * the interrupt has been triggered while pending.
251 *
252 * This acts as a coalescing mechanism in order to guarantee that a
253 * given interrupt only occurs at most once in a queue.
254 *
255 * When doing an EOI, the Q bit will indicate if the interrupt
256 * needs to be re-triggered.
257 */
258 #define XIVE_STATUS_ASSERTED 0x4 /* Extra bit for LSI */
259 #define XIVE_ESB_VAL_P 0x2
260 #define XIVE_ESB_VAL_Q 0x1
261
262 #define XIVE_ESB_RESET 0x0
263 #define XIVE_ESB_PENDING XIVE_ESB_VAL_P
264 #define XIVE_ESB_QUEUED (XIVE_ESB_VAL_P | XIVE_ESB_VAL_Q)
265 #define XIVE_ESB_OFF XIVE_ESB_VAL_Q
266
267 /*
268 * "magic" Event State Buffer (ESB) MMIO offsets.
269 *
270 * The following offsets into the ESB MMIO allow to read or manipulate
271 * the PQ bits. They must be used with an 8-byte load instruction.
272 * They all return the previous state of the interrupt (atomically).
273 *
274 * Additionally, some ESB pages support doing an EOI via a store and
275 * some ESBs support doing a trigger via a separate trigger page.
276 */
277 #define XIVE_ESB_STORE_EOI 0x400 /* Store */
278 #define XIVE_ESB_LOAD_EOI 0x000 /* Load */
279 #define XIVE_ESB_GET 0x800 /* Load */
280 #define XIVE_ESB_SET_PQ_00 0xc00 /* Load */
281 #define XIVE_ESB_SET_PQ_01 0xd00 /* Load */
282 #define XIVE_ESB_SET_PQ_10 0xe00 /* Load */
283 #define XIVE_ESB_SET_PQ_11 0xf00 /* Load */
284
285 uint8_t xive_source_esb_get(XiveSource *xsrc, uint32_t srcno);
286 uint8_t xive_source_esb_set(XiveSource *xsrc, uint32_t srcno, uint8_t pq);
287
288 void xive_source_pic_print_info(XiveSource *xsrc, uint32_t offset,
289 Monitor *mon);
290
291 static inline bool xive_source_irq_is_lsi(XiveSource *xsrc, uint32_t srcno)
292 {
293 assert(srcno < xsrc->nr_irqs);
294 return test_bit(srcno, xsrc->lsi_map);
295 }
296
297 static inline void xive_source_irq_set_lsi(XiveSource *xsrc, uint32_t srcno)
298 {
299 assert(srcno < xsrc->nr_irqs);
300 bitmap_set(xsrc->lsi_map, srcno, 1);
301 }
302
303 void xive_source_set_irq(void *opaque, int srcno, int val);
304
305 /*
306 * XIVE Thread interrupt Management (TM) context
307 */
308
309 #define TYPE_XIVE_TCTX "xive-tctx"
310 typedef struct XiveTCTX XiveTCTX;
311 #define XIVE_TCTX(obj) OBJECT_CHECK(XiveTCTX, (obj), TYPE_XIVE_TCTX)
312
313 /*
314 * XIVE Thread interrupt Management register rings :
315 *
316 * QW-0 User event-based exception state
317 * QW-1 O/S OS context for priority management, interrupt acks
318 * QW-2 Pool hypervisor pool context for virtual processors dispatched
319 * QW-3 Physical physical thread context and security context
320 */
321 #define XIVE_TM_RING_COUNT 4
322 #define XIVE_TM_RING_SIZE 0x10
323
324 typedef struct XivePresenter XivePresenter;
325
326 struct XiveTCTX {
327 DeviceState parent_obj;
328
329 CPUState *cs;
330 qemu_irq hv_output;
331 qemu_irq os_output;
332
333 uint8_t regs[XIVE_TM_RING_COUNT * XIVE_TM_RING_SIZE];
334
335 XivePresenter *xptr;
336 };
337
338 /*
339 * XIVE Router
340 */
341 typedef struct XiveFabric XiveFabric;
342
343 struct XiveRouter {
344 SysBusDevice parent;
345
346 XiveFabric *xfb;
347 };
348 typedef struct XiveRouter XiveRouter;
349
350 #define TYPE_XIVE_ROUTER "xive-router"
351 typedef struct XiveRouterClass XiveRouterClass;
352 #define XIVE_ROUTER(obj) \
353 OBJECT_CHECK(XiveRouter, (obj), TYPE_XIVE_ROUTER)
354 #define XIVE_ROUTER_CLASS(klass) \
355 OBJECT_CLASS_CHECK(XiveRouterClass, (klass), TYPE_XIVE_ROUTER)
356 #define XIVE_ROUTER_GET_CLASS(obj) \
357 OBJECT_GET_CLASS(XiveRouterClass, (obj), TYPE_XIVE_ROUTER)
358
359 struct XiveRouterClass {
360 SysBusDeviceClass parent;
361
362 /* XIVE table accessors */
363 int (*get_eas)(XiveRouter *xrtr, uint8_t eas_blk, uint32_t eas_idx,
364 XiveEAS *eas);
365 int (*get_end)(XiveRouter *xrtr, uint8_t end_blk, uint32_t end_idx,
366 XiveEND *end);
367 int (*write_end)(XiveRouter *xrtr, uint8_t end_blk, uint32_t end_idx,
368 XiveEND *end, uint8_t word_number);
369 int (*get_nvt)(XiveRouter *xrtr, uint8_t nvt_blk, uint32_t nvt_idx,
370 XiveNVT *nvt);
371 int (*write_nvt)(XiveRouter *xrtr, uint8_t nvt_blk, uint32_t nvt_idx,
372 XiveNVT *nvt, uint8_t word_number);
373 uint8_t (*get_block_id)(XiveRouter *xrtr);
374 };
375
376 int xive_router_get_eas(XiveRouter *xrtr, uint8_t eas_blk, uint32_t eas_idx,
377 XiveEAS *eas);
378 int xive_router_get_end(XiveRouter *xrtr, uint8_t end_blk, uint32_t end_idx,
379 XiveEND *end);
380 int xive_router_write_end(XiveRouter *xrtr, uint8_t end_blk, uint32_t end_idx,
381 XiveEND *end, uint8_t word_number);
382 int xive_router_get_nvt(XiveRouter *xrtr, uint8_t nvt_blk, uint32_t nvt_idx,
383 XiveNVT *nvt);
384 int xive_router_write_nvt(XiveRouter *xrtr, uint8_t nvt_blk, uint32_t nvt_idx,
385 XiveNVT *nvt, uint8_t word_number);
386 void xive_router_notify(XiveNotifier *xn, uint32_t lisn);
387
388 /*
389 * XIVE Presenter
390 */
391
392 typedef struct XiveTCTXMatch {
393 XiveTCTX *tctx;
394 uint8_t ring;
395 } XiveTCTXMatch;
396
397 #define TYPE_XIVE_PRESENTER "xive-presenter"
398 #define XIVE_PRESENTER(obj) \
399 INTERFACE_CHECK(XivePresenter, (obj), TYPE_XIVE_PRESENTER)
400 typedef struct XivePresenterClass XivePresenterClass;
401 #define XIVE_PRESENTER_CLASS(klass) \
402 OBJECT_CLASS_CHECK(XivePresenterClass, (klass), TYPE_XIVE_PRESENTER)
403 #define XIVE_PRESENTER_GET_CLASS(obj) \
404 OBJECT_GET_CLASS(XivePresenterClass, (obj), TYPE_XIVE_PRESENTER)
405
406 struct XivePresenterClass {
407 InterfaceClass parent;
408 int (*match_nvt)(XivePresenter *xptr, uint8_t format,
409 uint8_t nvt_blk, uint32_t nvt_idx,
410 bool cam_ignore, uint8_t priority,
411 uint32_t logic_serv, XiveTCTXMatch *match);
412 bool (*in_kernel)(const XivePresenter *xptr);
413 };
414
415 int xive_presenter_tctx_match(XivePresenter *xptr, XiveTCTX *tctx,
416 uint8_t format,
417 uint8_t nvt_blk, uint32_t nvt_idx,
418 bool cam_ignore, uint32_t logic_serv);
419
420 /*
421 * XIVE Fabric (Interface between Interrupt Controller and Machine)
422 */
423
424 #define TYPE_XIVE_FABRIC "xive-fabric"
425 #define XIVE_FABRIC(obj) \
426 INTERFACE_CHECK(XiveFabric, (obj), TYPE_XIVE_FABRIC)
427 typedef struct XiveFabricClass XiveFabricClass;
428 #define XIVE_FABRIC_CLASS(klass) \
429 OBJECT_CLASS_CHECK(XiveFabricClass, (klass), TYPE_XIVE_FABRIC)
430 #define XIVE_FABRIC_GET_CLASS(obj) \
431 OBJECT_GET_CLASS(XiveFabricClass, (obj), TYPE_XIVE_FABRIC)
432
433 struct XiveFabricClass {
434 InterfaceClass parent;
435 int (*match_nvt)(XiveFabric *xfb, uint8_t format,
436 uint8_t nvt_blk, uint32_t nvt_idx,
437 bool cam_ignore, uint8_t priority,
438 uint32_t logic_serv, XiveTCTXMatch *match);
439 };
440
441 /*
442 * XIVE END ESBs
443 */
444
445 #define TYPE_XIVE_END_SOURCE "xive-end-source"
446 typedef struct XiveENDSource XiveENDSource;
447 #define XIVE_END_SOURCE(obj) \
448 OBJECT_CHECK(XiveENDSource, (obj), TYPE_XIVE_END_SOURCE)
449
450 struct XiveENDSource {
451 DeviceState parent;
452
453 uint32_t nr_ends;
454
455 /* ESB memory region */
456 uint32_t esb_shift;
457 MemoryRegion esb_mmio;
458
459 XiveRouter *xrtr;
460 };
461
462 /*
463 * For legacy compatibility, the exceptions define up to 256 different
464 * priorities. P9 implements only 9 levels : 8 active levels [0 - 7]
465 * and the least favored level 0xFF.
466 */
467 #define XIVE_PRIORITY_MAX 7
468
469 /*
470 * XIVE Thread Interrupt Management Aera (TIMA)
471 *
472 * This region gives access to the registers of the thread interrupt
473 * management context. It is four page wide, each page providing a
474 * different view of the registers. The page with the lower offset is
475 * the most privileged and gives access to the entire context.
476 */
477 #define XIVE_TM_HW_PAGE 0x0
478 #define XIVE_TM_HV_PAGE 0x1
479 #define XIVE_TM_OS_PAGE 0x2
480 #define XIVE_TM_USER_PAGE 0x3
481
482 void xive_tctx_tm_write(XivePresenter *xptr, XiveTCTX *tctx, hwaddr offset,
483 uint64_t value, unsigned size);
484 uint64_t xive_tctx_tm_read(XivePresenter *xptr, XiveTCTX *tctx, hwaddr offset,
485 unsigned size);
486
487 void xive_tctx_pic_print_info(XiveTCTX *tctx, Monitor *mon);
488 Object *xive_tctx_create(Object *cpu, XivePresenter *xptr, Error **errp);
489 void xive_tctx_reset(XiveTCTX *tctx);
490 void xive_tctx_destroy(XiveTCTX *tctx);
491 void xive_tctx_ipb_update(XiveTCTX *tctx, uint8_t ring, uint8_t ipb);
492
493 /*
494 * KVM XIVE device helpers
495 */
496
497 int kvmppc_xive_source_reset_one(XiveSource *xsrc, int srcno, Error **errp);
498 void kvmppc_xive_source_set_irq(void *opaque, int srcno, int val);
499 int kvmppc_xive_cpu_connect(XiveTCTX *tctx, Error **errp);
500 int kvmppc_xive_cpu_synchronize_state(XiveTCTX *tctx, Error **errp);
501 int kvmppc_xive_cpu_get_state(XiveTCTX *tctx, Error **errp);
502 int kvmppc_xive_cpu_set_state(XiveTCTX *tctx, Error **errp);
503
504 #endif /* PPC_XIVE_H */