ppc/xive: Rework setup of XiveSource::esb_mmio
[qemu/ar7.git] / include / hw / ppc / xive.h
blob82a61eaca74fe2c1173b0c7f786fa9576fb97c0e
1 /*
2 * QEMU PowerPC XIVE interrupt controller model
5 * The POWER9 processor comes with a new interrupt controller, called
6 * XIVE as "eXternal Interrupt Virtualization Engine".
8 * = Overall architecture
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
35 * SC: Source Controller (aka. IVSE)
36 * VC: Virtualization Controller (aka. IVRE)
37 * PC: Presentation Controller (aka. IVPE)
38 * CQ: Common Queue (Bridge)
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
48 * The XIVE IC is composed of three sub-engines :
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.
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).
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.
65 * In XIVE 1.0, the sub-engines used to be referred as:
67 * SC Source Controller
68 * VC Virtualization Controller
69 * PC Presentation Controller
70 * CQ Common Queue (PowerBUS Bridge)
73 * = XIVE internal tables
75 * Each of the sub-engines uses a set of tables to redirect exceptions
76 * from event sources to CPU threads.
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 * +---------------------------------+ +-------+
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.
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.
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.
119 * = Acronyms
121 * Description In XIVE 1.0, used to be referred as
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
133 * Copyright (c) 2017-2018, IBM Corporation.
135 * This code is licensed under the GPL version 2 or later. See the
136 * COPYING file in the top-level directory.
140 #ifndef PPC_XIVE_H
141 #define PPC_XIVE_H
143 #include "sysemu/kvm.h"
144 #include "hw/sysbus.h"
145 #include "hw/ppc/xive_regs.h"
148 * XIVE Notifier (Interface between Source and Router)
151 typedef struct XiveNotifier XiveNotifier;
153 #define TYPE_XIVE_NOTIFIER "xive-notifier"
154 #define XIVE_NOTIFIER(obj) \
155 INTERFACE_CHECK(XiveNotifier, (obj), TYPE_XIVE_NOTIFIER)
156 #define XIVE_NOTIFIER_CLASS(klass) \
157 OBJECT_CLASS_CHECK(XiveNotifierClass, (klass), TYPE_XIVE_NOTIFIER)
158 #define XIVE_NOTIFIER_GET_CLASS(obj) \
159 OBJECT_GET_CLASS(XiveNotifierClass, (obj), TYPE_XIVE_NOTIFIER)
161 typedef struct XiveNotifierClass {
162 InterfaceClass parent;
163 void (*notify)(XiveNotifier *xn, uint32_t lisn);
164 } XiveNotifierClass;
167 * XIVE Interrupt Source
170 #define TYPE_XIVE_SOURCE "xive-source"
171 #define XIVE_SOURCE(obj) OBJECT_CHECK(XiveSource, (obj), TYPE_XIVE_SOURCE)
174 * XIVE Interrupt Source characteristics, which define how the ESB are
175 * controlled.
177 #define XIVE_SRC_H_INT_ESB 0x1 /* ESB managed with hcall H_INT_ESB */
178 #define XIVE_SRC_STORE_EOI 0x2 /* Store EOI supported */
180 typedef struct XiveSource {
181 DeviceState parent;
183 /* IRQs */
184 uint32_t nr_irqs;
185 unsigned long *lsi_map;
187 /* PQ bits and LSI assertion bit */
188 uint8_t *status;
190 /* ESB memory region */
191 uint64_t esb_flags;
192 uint32_t esb_shift;
193 MemoryRegion esb_mmio;
194 MemoryRegion esb_mmio_emulated;
196 /* KVM support */
197 void *esb_mmap;
198 MemoryRegion esb_mmio_kvm;
200 XiveNotifier *xive;
201 } XiveSource;
204 * ESB MMIO setting. Can be one page, for both source triggering and
205 * source management, or two different pages. See below for magic
206 * values.
208 #define XIVE_ESB_4K 12 /* PSI HB only */
209 #define XIVE_ESB_4K_2PAGE 13
210 #define XIVE_ESB_64K 16
211 #define XIVE_ESB_64K_2PAGE 17
213 static inline bool xive_source_esb_has_2page(XiveSource *xsrc)
215 return xsrc->esb_shift == XIVE_ESB_64K_2PAGE ||
216 xsrc->esb_shift == XIVE_ESB_4K_2PAGE;
219 static inline size_t xive_source_esb_len(XiveSource *xsrc)
221 return (1ull << xsrc->esb_shift) * xsrc->nr_irqs;
224 /* The trigger page is always the first/even page */
225 static inline hwaddr xive_source_esb_page(XiveSource *xsrc, uint32_t srcno)
227 assert(srcno < xsrc->nr_irqs);
228 return (1ull << xsrc->esb_shift) * srcno;
231 /* In a two pages ESB MMIO setting, the odd page is for management */
232 static inline hwaddr xive_source_esb_mgmt(XiveSource *xsrc, int srcno)
234 hwaddr addr = xive_source_esb_page(xsrc, srcno);
236 if (xive_source_esb_has_2page(xsrc)) {
237 addr += (1 << (xsrc->esb_shift - 1));
240 return addr;
244 * Each interrupt source has a 2-bit state machine which can be
245 * controlled by MMIO. P indicates that an interrupt is pending (has
246 * been sent to a queue and is waiting for an EOI). Q indicates that
247 * the interrupt has been triggered while pending.
249 * This acts as a coalescing mechanism in order to guarantee that a
250 * given interrupt only occurs at most once in a queue.
252 * When doing an EOI, the Q bit will indicate if the interrupt
253 * needs to be re-triggered.
255 #define XIVE_STATUS_ASSERTED 0x4 /* Extra bit for LSI */
256 #define XIVE_ESB_VAL_P 0x2
257 #define XIVE_ESB_VAL_Q 0x1
259 #define XIVE_ESB_RESET 0x0
260 #define XIVE_ESB_PENDING XIVE_ESB_VAL_P
261 #define XIVE_ESB_QUEUED (XIVE_ESB_VAL_P | XIVE_ESB_VAL_Q)
262 #define XIVE_ESB_OFF XIVE_ESB_VAL_Q
265 * "magic" Event State Buffer (ESB) MMIO offsets.
267 * The following offsets into the ESB MMIO allow to read or manipulate
268 * the PQ bits. They must be used with an 8-byte load instruction.
269 * They all return the previous state of the interrupt (atomically).
271 * Additionally, some ESB pages support doing an EOI via a store and
272 * some ESBs support doing a trigger via a separate trigger page.
274 #define XIVE_ESB_STORE_EOI 0x400 /* Store */
275 #define XIVE_ESB_LOAD_EOI 0x000 /* Load */
276 #define XIVE_ESB_GET 0x800 /* Load */
277 #define XIVE_ESB_SET_PQ_00 0xc00 /* Load */
278 #define XIVE_ESB_SET_PQ_01 0xd00 /* Load */
279 #define XIVE_ESB_SET_PQ_10 0xe00 /* Load */
280 #define XIVE_ESB_SET_PQ_11 0xf00 /* Load */
282 uint8_t xive_source_esb_get(XiveSource *xsrc, uint32_t srcno);
283 uint8_t xive_source_esb_set(XiveSource *xsrc, uint32_t srcno, uint8_t pq);
285 void xive_source_pic_print_info(XiveSource *xsrc, uint32_t offset,
286 Monitor *mon);
288 static inline bool xive_source_irq_is_lsi(XiveSource *xsrc, uint32_t srcno)
290 assert(srcno < xsrc->nr_irqs);
291 return test_bit(srcno, xsrc->lsi_map);
294 static inline void xive_source_irq_set_lsi(XiveSource *xsrc, uint32_t srcno)
296 assert(srcno < xsrc->nr_irqs);
297 bitmap_set(xsrc->lsi_map, srcno, 1);
300 void xive_source_set_irq(void *opaque, int srcno, int val);
303 * XIVE Thread interrupt Management (TM) context
306 #define TYPE_XIVE_TCTX "xive-tctx"
307 #define XIVE_TCTX(obj) OBJECT_CHECK(XiveTCTX, (obj), TYPE_XIVE_TCTX)
310 * XIVE Thread interrupt Management register rings :
312 * QW-0 User event-based exception state
313 * QW-1 O/S OS context for priority management, interrupt acks
314 * QW-2 Pool hypervisor pool context for virtual processors dispatched
315 * QW-3 Physical physical thread context and security context
317 #define XIVE_TM_RING_COUNT 4
318 #define XIVE_TM_RING_SIZE 0x10
320 typedef struct XivePresenter XivePresenter;
322 typedef struct XiveTCTX {
323 DeviceState parent_obj;
325 CPUState *cs;
326 qemu_irq hv_output;
327 qemu_irq os_output;
329 uint8_t regs[XIVE_TM_RING_COUNT * XIVE_TM_RING_SIZE];
331 XivePresenter *xptr;
332 } XiveTCTX;
335 * XIVE Router
337 typedef struct XiveFabric XiveFabric;
339 typedef struct XiveRouter {
340 SysBusDevice parent;
342 XiveFabric *xfb;
343 } XiveRouter;
345 #define TYPE_XIVE_ROUTER "xive-router"
346 #define XIVE_ROUTER(obj) \
347 OBJECT_CHECK(XiveRouter, (obj), TYPE_XIVE_ROUTER)
348 #define XIVE_ROUTER_CLASS(klass) \
349 OBJECT_CLASS_CHECK(XiveRouterClass, (klass), TYPE_XIVE_ROUTER)
350 #define XIVE_ROUTER_GET_CLASS(obj) \
351 OBJECT_GET_CLASS(XiveRouterClass, (obj), TYPE_XIVE_ROUTER)
353 typedef struct XiveRouterClass {
354 SysBusDeviceClass parent;
356 /* XIVE table accessors */
357 int (*get_eas)(XiveRouter *xrtr, uint8_t eas_blk, uint32_t eas_idx,
358 XiveEAS *eas);
359 int (*get_end)(XiveRouter *xrtr, uint8_t end_blk, uint32_t end_idx,
360 XiveEND *end);
361 int (*write_end)(XiveRouter *xrtr, uint8_t end_blk, uint32_t end_idx,
362 XiveEND *end, uint8_t word_number);
363 int (*get_nvt)(XiveRouter *xrtr, uint8_t nvt_blk, uint32_t nvt_idx,
364 XiveNVT *nvt);
365 int (*write_nvt)(XiveRouter *xrtr, uint8_t nvt_blk, uint32_t nvt_idx,
366 XiveNVT *nvt, uint8_t word_number);
367 uint8_t (*get_block_id)(XiveRouter *xrtr);
368 } XiveRouterClass;
370 int xive_router_get_eas(XiveRouter *xrtr, uint8_t eas_blk, uint32_t eas_idx,
371 XiveEAS *eas);
372 int xive_router_get_end(XiveRouter *xrtr, uint8_t end_blk, uint32_t end_idx,
373 XiveEND *end);
374 int xive_router_write_end(XiveRouter *xrtr, uint8_t end_blk, uint32_t end_idx,
375 XiveEND *end, uint8_t word_number);
376 int xive_router_get_nvt(XiveRouter *xrtr, uint8_t nvt_blk, uint32_t nvt_idx,
377 XiveNVT *nvt);
378 int xive_router_write_nvt(XiveRouter *xrtr, uint8_t nvt_blk, uint32_t nvt_idx,
379 XiveNVT *nvt, uint8_t word_number);
380 void xive_router_notify(XiveNotifier *xn, uint32_t lisn);
383 * XIVE Presenter
386 typedef struct XiveTCTXMatch {
387 XiveTCTX *tctx;
388 uint8_t ring;
389 } XiveTCTXMatch;
391 #define TYPE_XIVE_PRESENTER "xive-presenter"
392 #define XIVE_PRESENTER(obj) \
393 INTERFACE_CHECK(XivePresenter, (obj), TYPE_XIVE_PRESENTER)
394 #define XIVE_PRESENTER_CLASS(klass) \
395 OBJECT_CLASS_CHECK(XivePresenterClass, (klass), TYPE_XIVE_PRESENTER)
396 #define XIVE_PRESENTER_GET_CLASS(obj) \
397 OBJECT_GET_CLASS(XivePresenterClass, (obj), TYPE_XIVE_PRESENTER)
399 typedef struct XivePresenterClass {
400 InterfaceClass parent;
401 int (*match_nvt)(XivePresenter *xptr, uint8_t format,
402 uint8_t nvt_blk, uint32_t nvt_idx,
403 bool cam_ignore, uint8_t priority,
404 uint32_t logic_serv, XiveTCTXMatch *match);
405 } XivePresenterClass;
407 int xive_presenter_tctx_match(XivePresenter *xptr, XiveTCTX *tctx,
408 uint8_t format,
409 uint8_t nvt_blk, uint32_t nvt_idx,
410 bool cam_ignore, uint32_t logic_serv);
413 * XIVE Fabric (Interface between Interrupt Controller and Machine)
416 #define TYPE_XIVE_FABRIC "xive-fabric"
417 #define XIVE_FABRIC(obj) \
418 INTERFACE_CHECK(XiveFabric, (obj), TYPE_XIVE_FABRIC)
419 #define XIVE_FABRIC_CLASS(klass) \
420 OBJECT_CLASS_CHECK(XiveFabricClass, (klass), TYPE_XIVE_FABRIC)
421 #define XIVE_FABRIC_GET_CLASS(obj) \
422 OBJECT_GET_CLASS(XiveFabricClass, (obj), TYPE_XIVE_FABRIC)
424 typedef struct XiveFabricClass {
425 InterfaceClass parent;
426 int (*match_nvt)(XiveFabric *xfb, uint8_t format,
427 uint8_t nvt_blk, uint32_t nvt_idx,
428 bool cam_ignore, uint8_t priority,
429 uint32_t logic_serv, XiveTCTXMatch *match);
430 } XiveFabricClass;
433 * XIVE END ESBs
436 #define TYPE_XIVE_END_SOURCE "xive-end-source"
437 #define XIVE_END_SOURCE(obj) \
438 OBJECT_CHECK(XiveENDSource, (obj), TYPE_XIVE_END_SOURCE)
440 typedef struct XiveENDSource {
441 DeviceState parent;
443 uint32_t nr_ends;
445 /* ESB memory region */
446 uint32_t esb_shift;
447 MemoryRegion esb_mmio;
449 XiveRouter *xrtr;
450 } XiveENDSource;
453 * For legacy compatibility, the exceptions define up to 256 different
454 * priorities. P9 implements only 9 levels : 8 active levels [0 - 7]
455 * and the least favored level 0xFF.
457 #define XIVE_PRIORITY_MAX 7
460 * XIVE Thread Interrupt Management Aera (TIMA)
462 * This region gives access to the registers of the thread interrupt
463 * management context. It is four page wide, each page providing a
464 * different view of the registers. The page with the lower offset is
465 * the most privileged and gives access to the entire context.
467 #define XIVE_TM_HW_PAGE 0x0
468 #define XIVE_TM_HV_PAGE 0x1
469 #define XIVE_TM_OS_PAGE 0x2
470 #define XIVE_TM_USER_PAGE 0x3
472 void xive_tctx_tm_write(XivePresenter *xptr, XiveTCTX *tctx, hwaddr offset,
473 uint64_t value, unsigned size);
474 uint64_t xive_tctx_tm_read(XivePresenter *xptr, XiveTCTX *tctx, hwaddr offset,
475 unsigned size);
477 void xive_tctx_pic_print_info(XiveTCTX *tctx, Monitor *mon);
478 Object *xive_tctx_create(Object *cpu, XivePresenter *xptr, Error **errp);
479 void xive_tctx_reset(XiveTCTX *tctx);
480 void xive_tctx_destroy(XiveTCTX *tctx);
481 void xive_tctx_ipb_update(XiveTCTX *tctx, uint8_t ring, uint8_t ipb);
484 * KVM XIVE device helpers
487 int kvmppc_xive_source_reset_one(XiveSource *xsrc, int srcno, Error **errp);
488 void kvmppc_xive_source_set_irq(void *opaque, int srcno, int val);
489 void kvmppc_xive_cpu_connect(XiveTCTX *tctx, Error **errp);
490 void kvmppc_xive_cpu_synchronize_state(XiveTCTX *tctx, Error **errp);
491 void kvmppc_xive_cpu_get_state(XiveTCTX *tctx, Error **errp);
492 void kvmppc_xive_cpu_set_state(XiveTCTX *tctx, Error **errp);
494 #endif /* PPC_XIVE_H */