error: Fix use of error_prepend() with &error_fatal, &error_abort
[qemu.git] / hw / intc / xics.c
CommitLineData
b5cec4c5
DG
1/*
2 * QEMU PowerPC pSeries Logical Partition (aka sPAPR) hardware System Emulator
3 *
4 * PAPR Virtualized Interrupt System, aka ICS/ICP aka xics
5 *
6 * Copyright (c) 2010,2011 David Gibson, IBM Corporation.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 *
26 */
27
0d75590d 28#include "qemu/osdep.h"
da34e65c 29#include "qapi/error.h"
4771d756
PB
30#include "qemu-common.h"
31#include "cpu.h"
83c9f4ca 32#include "hw/hw.h"
500efa23 33#include "trace.h"
5d87e4b7 34#include "qemu/timer.h"
0d09e41a 35#include "hw/ppc/xics.h"
9ccff2a4 36#include "qemu/error-report.h"
5a3d7b23 37#include "qapi/visitor.h"
b1fc72f0
BH
38#include "monitor/monitor.h"
39#include "hw/intc/intc.h"
b5cec4c5 40
6449da45 41void icp_pic_print_info(ICPState *icp, Monitor *mon)
b1fc72f0 42{
dcb556fc 43 ICPStateClass *icpc = ICP_GET_CLASS(icp);
b9038e78
CLG
44 int cpu_index = icp->cs ? icp->cs->cpu_index : -1;
45
46 if (!icp->output) {
47 return;
48 }
dcb556fc
GK
49
50 if (icpc->synchronize_state) {
51 icpc->synchronize_state(icp);
52 }
53
b9038e78
CLG
54 monitor_printf(mon, "CPU %d XIRR=%08x (%p) PP=%02x MFRR=%02x\n",
55 cpu_index, icp->xirr, icp->xirr_owner,
56 icp->pending_priority, icp->mfrr);
57}
58
6449da45 59void ics_pic_print_info(ICSState *ics, Monitor *mon)
b9038e78 60{
dcb556fc 61 ICSStateClass *icsc = ICS_BASE_GET_CLASS(ics);
b1fc72f0
BH
62 uint32_t i;
63
b9038e78
CLG
64 monitor_printf(mon, "ICS %4x..%4x %p\n",
65 ics->offset, ics->offset + ics->nr_irqs - 1, ics);
b1fc72f0 66
b9038e78
CLG
67 if (!ics->irqs) {
68 return;
b1fc72f0
BH
69 }
70
dcb556fc
GK
71 if (icsc->synchronize_state) {
72 icsc->synchronize_state(ics);
73 }
74
b9038e78
CLG
75 for (i = 0; i < ics->nr_irqs; i++) {
76 ICSIRQState *irq = ics->irqs + i;
b1fc72f0 77
b9038e78 78 if (!(irq->flags & XICS_FLAGS_IRQ_MASK)) {
b1fc72f0
BH
79 continue;
80 }
b9038e78
CLG
81 monitor_printf(mon, " %4x %s %02x %02x\n",
82 ics->offset + i,
83 (irq->flags & XICS_FLAGS_IRQ_LSI) ?
84 "LSI" : "MSI",
85 irq->priority, irq->status);
b1fc72f0
BH
86 }
87}
88
b5cec4c5
DG
89/*
90 * ICP: Presentation layer
91 */
92
b5cec4c5
DG
93#define XISR_MASK 0x00ffffff
94#define CPPR_MASK 0xff000000
95
8e4fba20
CLG
96#define XISR(icp) (((icp)->xirr) & XISR_MASK)
97#define CPPR(icp) (((icp)->xirr) >> 24)
b5cec4c5 98
d4d7a59a
BH
99static void ics_reject(ICSState *ics, uint32_t nr)
100{
101 ICSStateClass *k = ICS_BASE_GET_CLASS(ics);
102
103 if (k->reject) {
104 k->reject(ics, nr);
105 }
106}
107
7844e12b 108void ics_resend(ICSState *ics)
d4d7a59a
BH
109{
110 ICSStateClass *k = ICS_BASE_GET_CLASS(ics);
111
112 if (k->resend) {
113 k->resend(ics);
114 }
115}
116
117static void ics_eoi(ICSState *ics, int nr)
118{
119 ICSStateClass *k = ICS_BASE_GET_CLASS(ics);
120
121 if (k->eoi) {
122 k->eoi(ics, nr);
123 }
124}
b5cec4c5 125
8e4fba20 126static void icp_check_ipi(ICPState *icp)
b5cec4c5 127{
8e4fba20 128 if (XISR(icp) && (icp->pending_priority <= icp->mfrr)) {
b5cec4c5
DG
129 return;
130 }
131
8e4fba20 132 trace_xics_icp_check_ipi(icp->cs->cpu_index, icp->mfrr);
500efa23 133
8e4fba20
CLG
134 if (XISR(icp) && icp->xirr_owner) {
135 ics_reject(icp->xirr_owner, XISR(icp));
b5cec4c5
DG
136 }
137
8e4fba20
CLG
138 icp->xirr = (icp->xirr & ~XISR_MASK) | XICS_IPI;
139 icp->pending_priority = icp->mfrr;
140 icp->xirr_owner = NULL;
141 qemu_irq_raise(icp->output);
b5cec4c5
DG
142}
143
8e4fba20 144void icp_resend(ICPState *icp)
b5cec4c5 145{
8e4fba20 146 XICSFabric *xi = icp->xics;
2cd908d0 147 XICSFabricClass *xic = XICS_FABRIC_GET_CLASS(xi);
b5cec4c5 148
8e4fba20
CLG
149 if (icp->mfrr < CPPR(icp)) {
150 icp_check_ipi(icp);
cc706a53 151 }
2cd908d0
CLG
152
153 xic->ics_resend(xi);
b5cec4c5
DG
154}
155
8e4fba20 156void icp_set_cppr(ICPState *icp, uint8_t cppr)
b5cec4c5 157{
b5cec4c5
DG
158 uint8_t old_cppr;
159 uint32_t old_xisr;
160
8e4fba20
CLG
161 old_cppr = CPPR(icp);
162 icp->xirr = (icp->xirr & ~CPPR_MASK) | (cppr << 24);
b5cec4c5
DG
163
164 if (cppr < old_cppr) {
8e4fba20
CLG
165 if (XISR(icp) && (cppr <= icp->pending_priority)) {
166 old_xisr = XISR(icp);
167 icp->xirr &= ~XISR_MASK; /* Clear XISR */
168 icp->pending_priority = 0xff;
169 qemu_irq_lower(icp->output);
170 if (icp->xirr_owner) {
171 ics_reject(icp->xirr_owner, old_xisr);
172 icp->xirr_owner = NULL;
cc706a53 173 }
b5cec4c5
DG
174 }
175 } else {
8e4fba20
CLG
176 if (!XISR(icp)) {
177 icp_resend(icp);
b5cec4c5
DG
178 }
179 }
180}
181
8e4fba20 182void icp_set_mfrr(ICPState *icp, uint8_t mfrr)
b5cec4c5 183{
8e4fba20
CLG
184 icp->mfrr = mfrr;
185 if (mfrr < CPPR(icp)) {
186 icp_check_ipi(icp);
b5cec4c5
DG
187 }
188}
189
8e4fba20 190uint32_t icp_accept(ICPState *icp)
b5cec4c5 191{
8e4fba20 192 uint32_t xirr = icp->xirr;
b5cec4c5 193
8e4fba20
CLG
194 qemu_irq_lower(icp->output);
195 icp->xirr = icp->pending_priority << 24;
196 icp->pending_priority = 0xff;
197 icp->xirr_owner = NULL;
500efa23 198
8e4fba20 199 trace_xics_icp_accept(xirr, icp->xirr);
500efa23 200
b5cec4c5
DG
201 return xirr;
202}
203
8e4fba20 204uint32_t icp_ipoll(ICPState *icp, uint32_t *mfrr)
1cbd2220
BH
205{
206 if (mfrr) {
8e4fba20 207 *mfrr = icp->mfrr;
1cbd2220 208 }
8e4fba20 209 return icp->xirr;
1cbd2220
BH
210}
211
8e4fba20 212void icp_eoi(ICPState *icp, uint32_t xirr)
b5cec4c5 213{
8e4fba20 214 XICSFabric *xi = icp->xics;
2cd908d0 215 XICSFabricClass *xic = XICS_FABRIC_GET_CLASS(xi);
cc706a53
BH
216 ICSState *ics;
217 uint32_t irq;
b5cec4c5 218
b5cec4c5 219 /* Send EOI -> ICS */
8e4fba20
CLG
220 icp->xirr = (icp->xirr & ~CPPR_MASK) | (xirr & CPPR_MASK);
221 trace_xics_icp_eoi(icp->cs->cpu_index, xirr, icp->xirr);
cc706a53 222 irq = xirr & XISR_MASK;
2cd908d0
CLG
223
224 ics = xic->ics_get(xi, irq);
225 if (ics) {
226 ics_eoi(ics, irq);
cc706a53 227 }
8e4fba20
CLG
228 if (!XISR(icp)) {
229 icp_resend(icp);
b5cec4c5
DG
230 }
231}
232
cc706a53 233static void icp_irq(ICSState *ics, int server, int nr, uint8_t priority)
b5cec4c5 234{
8e4fba20 235 ICPState *icp = xics_icp_get(ics->xics, server);
b5cec4c5 236
500efa23
DG
237 trace_xics_icp_irq(server, nr, priority);
238
8e4fba20
CLG
239 if ((priority >= CPPR(icp))
240 || (XISR(icp) && (icp->pending_priority <= priority))) {
cc706a53 241 ics_reject(ics, nr);
b5cec4c5 242 } else {
8e4fba20
CLG
243 if (XISR(icp) && icp->xirr_owner) {
244 ics_reject(icp->xirr_owner, XISR(icp));
245 icp->xirr_owner = NULL;
b5cec4c5 246 }
8e4fba20
CLG
247 icp->xirr = (icp->xirr & ~XISR_MASK) | (nr & XISR_MASK);
248 icp->xirr_owner = ics;
249 icp->pending_priority = priority;
250 trace_xics_icp_raise(icp->xirr, icp->pending_priority);
251 qemu_irq_raise(icp->output);
b5cec4c5
DG
252 }
253}
254
44b1ff31 255static int icp_dispatch_pre_save(void *opaque)
d1b5682d 256{
8e4fba20
CLG
257 ICPState *icp = opaque;
258 ICPStateClass *info = ICP_GET_CLASS(icp);
d1b5682d
AK
259
260 if (info->pre_save) {
8e4fba20 261 info->pre_save(icp);
d1b5682d 262 }
44b1ff31
DDAG
263
264 return 0;
d1b5682d
AK
265}
266
267static int icp_dispatch_post_load(void *opaque, int version_id)
268{
8e4fba20
CLG
269 ICPState *icp = opaque;
270 ICPStateClass *info = ICP_GET_CLASS(icp);
d1b5682d
AK
271
272 if (info->post_load) {
8e4fba20 273 return info->post_load(icp, version_id);
d1b5682d
AK
274 }
275
276 return 0;
277}
278
c04d6cfa
AL
279static const VMStateDescription vmstate_icp_server = {
280 .name = "icp/server",
281 .version_id = 1,
282 .minimum_version_id = 1,
d1b5682d
AK
283 .pre_save = icp_dispatch_pre_save,
284 .post_load = icp_dispatch_post_load,
3aff6c2f 285 .fields = (VMStateField[]) {
c04d6cfa
AL
286 /* Sanity check */
287 VMSTATE_UINT32(xirr, ICPState),
288 VMSTATE_UINT8(pending_priority, ICPState),
289 VMSTATE_UINT8(mfrr, ICPState),
290 VMSTATE_END_OF_LIST()
291 },
b5cec4c5
DG
292};
293
b585395b 294static void icp_reset(DeviceState *dev)
c04d6cfa
AL
295{
296 ICPState *icp = ICP(dev);
297
298 icp->xirr = 0;
299 icp->pending_priority = 0xff;
300 icp->mfrr = 0xff;
301
302 /* Make all outputs are deasserted */
303 qemu_set_irq(icp->output, 0);
304}
305
b585395b
GK
306static void icp_reset_handler(void *dev)
307{
308 DeviceClass *dc = DEVICE_GET_CLASS(dev);
309
310 dc->reset(dev);
311}
312
817bb6a4
CLG
313static void icp_realize(DeviceState *dev, Error **errp)
314{
315 ICPState *icp = ICP(dev);
9ed65663
GK
316 PowerPCCPU *cpu;
317 CPUPPCState *env;
817bb6a4
CLG
318 Object *obj;
319 Error *err = NULL;
320
ad265631 321 obj = object_property_get_link(OBJECT(dev), ICP_PROP_XICS, &err);
817bb6a4 322 if (!obj) {
4b576648
MA
323 error_propagate_prepend(errp, err,
324 "required link '" ICP_PROP_XICS
325 "' not found: ");
817bb6a4
CLG
326 return;
327 }
328
2cd908d0 329 icp->xics = XICS_FABRIC(obj);
7ea6e067 330
9ed65663
GK
331 obj = object_property_get_link(OBJECT(dev), ICP_PROP_CPU, &err);
332 if (!obj) {
4b576648
MA
333 error_propagate_prepend(errp, err,
334 "required link '" ICP_PROP_CPU
335 "' not found: ");
9ed65663
GK
336 return;
337 }
338
339 cpu = POWERPC_CPU(obj);
9ed65663
GK
340 icp->cs = CPU(obj);
341
9ed65663
GK
342 env = &cpu->env;
343 switch (PPC_INPUT(env)) {
344 case PPC_FLAGS_INPUT_POWER7:
345 icp->output = env->irq_inputs[POWER7_INPUT_INT];
346 break;
347
348 case PPC_FLAGS_INPUT_970:
349 icp->output = env->irq_inputs[PPC970_INPUT_INT];
350 break;
351
352 default:
353 error_setg(errp, "XICS interrupt controller does not support this CPU bus model");
354 return;
355 }
356
b585395b 357 qemu_register_reset(icp_reset_handler, dev);
c95f6161 358 vmstate_register(NULL, icp->cs->cpu_index, &vmstate_icp_server, icp);
817bb6a4
CLG
359}
360
62f94fc9
GK
361static void icp_unrealize(DeviceState *dev, Error **errp)
362{
c95f6161
GK
363 ICPState *icp = ICP(dev);
364
365 vmstate_unregister(NULL, &vmstate_icp_server, icp);
b585395b 366 qemu_unregister_reset(icp_reset_handler, dev);
62f94fc9 367}
817bb6a4 368
c04d6cfa
AL
369static void icp_class_init(ObjectClass *klass, void *data)
370{
371 DeviceClass *dc = DEVICE_CLASS(klass);
372
817bb6a4 373 dc->realize = icp_realize;
62f94fc9 374 dc->unrealize = icp_unrealize;
b585395b 375 dc->reset = icp_reset;
c04d6cfa
AL
376}
377
456df19c 378static const TypeInfo icp_info = {
c04d6cfa
AL
379 .name = TYPE_ICP,
380 .parent = TYPE_DEVICE,
381 .instance_size = sizeof(ICPState),
382 .class_init = icp_class_init,
d1b5682d 383 .class_size = sizeof(ICPStateClass),
b5cec4c5
DG
384};
385
4f7a47be
CLG
386Object *icp_create(Object *cpu, const char *type, XICSFabric *xi, Error **errp)
387{
388 Error *local_err = NULL;
389 Object *obj;
390
391 obj = object_new(type);
392 object_property_add_child(cpu, type, obj, &error_abort);
393 object_unref(obj);
394 object_property_add_const_link(obj, ICP_PROP_XICS, OBJECT(xi),
395 &error_abort);
396 object_property_add_const_link(obj, ICP_PROP_CPU, cpu, &error_abort);
397 object_property_set_bool(obj, true, "realized", &local_err);
398 if (local_err) {
399 object_unparent(obj);
400 error_propagate(errp, local_err);
401 obj = NULL;
402 }
403
404 return obj;
405}
406
c04d6cfa
AL
407/*
408 * ICS: Source layer
409 */
d4d7a59a 410static void ics_simple_resend_msi(ICSState *ics, int srcno)
d07fee7e 411{
c04d6cfa 412 ICSIRQState *irq = ics->irqs + srcno;
d07fee7e
DG
413
414 /* FIXME: filter by server#? */
98ca8c02
DG
415 if (irq->status & XICS_STATUS_REJECTED) {
416 irq->status &= ~XICS_STATUS_REJECTED;
d07fee7e 417 if (irq->priority != 0xff) {
cc706a53 418 icp_irq(ics, irq->server, srcno + ics->offset, irq->priority);
d07fee7e
DG
419 }
420 }
421}
422
d4d7a59a 423static void ics_simple_resend_lsi(ICSState *ics, int srcno)
d07fee7e 424{
c04d6cfa 425 ICSIRQState *irq = ics->irqs + srcno;
d07fee7e 426
98ca8c02
DG
427 if ((irq->priority != 0xff)
428 && (irq->status & XICS_STATUS_ASSERTED)
429 && !(irq->status & XICS_STATUS_SENT)) {
430 irq->status |= XICS_STATUS_SENT;
cc706a53 431 icp_irq(ics, irq->server, srcno + ics->offset, irq->priority);
d07fee7e
DG
432 }
433}
434
d4d7a59a 435static void ics_simple_set_irq_msi(ICSState *ics, int srcno, int val)
b5cec4c5 436{
c04d6cfa 437 ICSIRQState *irq = ics->irqs + srcno;
b5cec4c5 438
d4d7a59a 439 trace_xics_ics_simple_set_irq_msi(srcno, srcno + ics->offset);
500efa23 440
b5cec4c5
DG
441 if (val) {
442 if (irq->priority == 0xff) {
98ca8c02 443 irq->status |= XICS_STATUS_MASKED_PENDING;
500efa23 444 trace_xics_masked_pending();
b5cec4c5 445 } else {
cc706a53 446 icp_irq(ics, irq->server, srcno + ics->offset, irq->priority);
b5cec4c5
DG
447 }
448 }
449}
450
d4d7a59a 451static void ics_simple_set_irq_lsi(ICSState *ics, int srcno, int val)
b5cec4c5 452{
c04d6cfa 453 ICSIRQState *irq = ics->irqs + srcno;
b5cec4c5 454
d4d7a59a 455 trace_xics_ics_simple_set_irq_lsi(srcno, srcno + ics->offset);
98ca8c02
DG
456 if (val) {
457 irq->status |= XICS_STATUS_ASSERTED;
458 } else {
459 irq->status &= ~XICS_STATUS_ASSERTED;
460 }
d4d7a59a 461 ics_simple_resend_lsi(ics, srcno);
b5cec4c5
DG
462}
463
d4d7a59a 464static void ics_simple_set_irq(void *opaque, int srcno, int val)
b5cec4c5 465{
c04d6cfa 466 ICSState *ics = (ICSState *)opaque;
b5cec4c5 467
4af88944 468 if (ics->irqs[srcno].flags & XICS_FLAGS_IRQ_LSI) {
d4d7a59a 469 ics_simple_set_irq_lsi(ics, srcno, val);
d07fee7e 470 } else {
d4d7a59a 471 ics_simple_set_irq_msi(ics, srcno, val);
d07fee7e
DG
472 }
473}
b5cec4c5 474
d4d7a59a 475static void ics_simple_write_xive_msi(ICSState *ics, int srcno)
d07fee7e 476{
c04d6cfa 477 ICSIRQState *irq = ics->irqs + srcno;
d07fee7e 478
98ca8c02
DG
479 if (!(irq->status & XICS_STATUS_MASKED_PENDING)
480 || (irq->priority == 0xff)) {
d07fee7e 481 return;
b5cec4c5 482 }
d07fee7e 483
98ca8c02 484 irq->status &= ~XICS_STATUS_MASKED_PENDING;
cc706a53 485 icp_irq(ics, irq->server, srcno + ics->offset, irq->priority);
b5cec4c5
DG
486}
487
d4d7a59a 488static void ics_simple_write_xive_lsi(ICSState *ics, int srcno)
b5cec4c5 489{
d4d7a59a 490 ics_simple_resend_lsi(ics, srcno);
d07fee7e
DG
491}
492
d4d7a59a
BH
493void ics_simple_write_xive(ICSState *ics, int srcno, int server,
494 uint8_t priority, uint8_t saved_priority)
d07fee7e 495{
c04d6cfa 496 ICSIRQState *irq = ics->irqs + srcno;
b5cec4c5
DG
497
498 irq->server = server;
499 irq->priority = priority;
3fe719f4 500 irq->saved_priority = saved_priority;
b5cec4c5 501
d4d7a59a
BH
502 trace_xics_ics_simple_write_xive(ics->offset + srcno, srcno, server,
503 priority);
500efa23 504
4af88944 505 if (ics->irqs[srcno].flags & XICS_FLAGS_IRQ_LSI) {
d4d7a59a 506 ics_simple_write_xive_lsi(ics, srcno);
d07fee7e 507 } else {
d4d7a59a 508 ics_simple_write_xive_msi(ics, srcno);
b5cec4c5 509 }
b5cec4c5
DG
510}
511
d4d7a59a 512static void ics_simple_reject(ICSState *ics, uint32_t nr)
b5cec4c5 513{
c04d6cfa 514 ICSIRQState *irq = ics->irqs + nr - ics->offset;
d07fee7e 515
d4d7a59a 516 trace_xics_ics_simple_reject(nr, nr - ics->offset);
056b9775
ND
517 if (irq->flags & XICS_FLAGS_IRQ_MSI) {
518 irq->status |= XICS_STATUS_REJECTED;
519 } else if (irq->flags & XICS_FLAGS_IRQ_LSI) {
520 irq->status &= ~XICS_STATUS_SENT;
521 }
b5cec4c5
DG
522}
523
d4d7a59a 524static void ics_simple_resend(ICSState *ics)
b5cec4c5 525{
d07fee7e
DG
526 int i;
527
528 for (i = 0; i < ics->nr_irqs; i++) {
d07fee7e 529 /* FIXME: filter by server#? */
4af88944 530 if (ics->irqs[i].flags & XICS_FLAGS_IRQ_LSI) {
d4d7a59a 531 ics_simple_resend_lsi(ics, i);
d07fee7e 532 } else {
d4d7a59a 533 ics_simple_resend_msi(ics, i);
d07fee7e
DG
534 }
535 }
b5cec4c5
DG
536}
537
d4d7a59a 538static void ics_simple_eoi(ICSState *ics, uint32_t nr)
b5cec4c5 539{
d07fee7e 540 int srcno = nr - ics->offset;
c04d6cfa 541 ICSIRQState *irq = ics->irqs + srcno;
d07fee7e 542
d4d7a59a 543 trace_xics_ics_simple_eoi(nr);
500efa23 544
4af88944 545 if (ics->irqs[srcno].flags & XICS_FLAGS_IRQ_LSI) {
98ca8c02 546 irq->status &= ~XICS_STATUS_SENT;
d07fee7e 547 }
b5cec4c5
DG
548}
549
eeefd43b 550static void ics_simple_reset(DeviceState *dev)
c04d6cfa 551{
eeefd43b 552 ICSStateClass *icsc = ICS_BASE_GET_CLASS(dev);
a7e519a8 553
eeefd43b
CLG
554 icsc->parent_reset(dev);
555}
a7e519a8 556
eeefd43b
CLG
557static void ics_simple_reset_handler(void *dev)
558{
559 ics_simple_reset(dev);
c04d6cfa
AL
560}
561
0a647b76 562static void ics_simple_realize(DeviceState *dev, Error **errp)
c04d6cfa 563{
0a647b76
CLG
564 ICSState *ics = ICS_SIMPLE(dev);
565 ICSStateClass *icsc = ICS_BASE_GET_CLASS(ics);
566 Error *local_err = NULL;
567
568 icsc->parent_realize(dev, &local_err);
569 if (local_err) {
570 error_propagate(errp, local_err);
b45ff2d9
AK
571 return;
572 }
0a647b76 573
d4d7a59a 574 ics->qirqs = qemu_allocate_irqs(ics_simple_set_irq, ics, ics->nr_irqs);
7ea6e067 575
eeefd43b 576 qemu_register_reset(ics_simple_reset_handler, ics);
c04d6cfa
AL
577}
578
d4d7a59a 579static void ics_simple_class_init(ObjectClass *klass, void *data)
c04d6cfa
AL
580{
581 DeviceClass *dc = DEVICE_CLASS(klass);
d4d7a59a 582 ICSStateClass *isc = ICS_BASE_CLASS(klass);
c04d6cfa 583
0a647b76
CLG
584 device_class_set_parent_realize(dc, ics_simple_realize,
585 &isc->parent_realize);
eeefd43b
CLG
586 device_class_set_parent_reset(dc, ics_simple_reset,
587 &isc->parent_reset);
0a647b76 588
d4d7a59a
BH
589 isc->reject = ics_simple_reject;
590 isc->resend = ics_simple_resend;
591 isc->eoi = ics_simple_eoi;
c04d6cfa
AL
592}
593
d4d7a59a
BH
594static const TypeInfo ics_simple_info = {
595 .name = TYPE_ICS_SIMPLE,
596 .parent = TYPE_ICS_BASE,
597 .instance_size = sizeof(ICSState),
598 .class_init = ics_simple_class_init,
599 .class_size = sizeof(ICSStateClass),
d4d7a59a
BH
600};
601
eeefd43b
CLG
602static void ics_base_reset(DeviceState *dev)
603{
604 ICSState *ics = ICS_BASE(dev);
605 int i;
606 uint8_t flags[ics->nr_irqs];
607
608 for (i = 0; i < ics->nr_irqs; i++) {
609 flags[i] = ics->irqs[i].flags;
610 }
611
612 memset(ics->irqs, 0, sizeof(ICSIRQState) * ics->nr_irqs);
613
614 for (i = 0; i < ics->nr_irqs; i++) {
615 ics->irqs[i].priority = 0xff;
616 ics->irqs[i].saved_priority = 0xff;
617 ics->irqs[i].flags = flags[i];
618 }
619}
620
4e4169f7
CLG
621static void ics_base_realize(DeviceState *dev, Error **errp)
622{
4e4169f7
CLG
623 ICSState *ics = ICS_BASE(dev);
624 Object *obj;
625 Error *err = NULL;
626
ad265631 627 obj = object_property_get_link(OBJECT(dev), ICS_PROP_XICS, &err);
4e4169f7 628 if (!obj) {
4b576648
MA
629 error_propagate_prepend(errp, err,
630 "required link '" ICS_PROP_XICS
631 "' not found: ");
4e4169f7
CLG
632 return;
633 }
b4f27d71 634 ics->xics = XICS_FABRIC(obj);
4e4169f7 635
0a647b76
CLG
636 if (!ics->nr_irqs) {
637 error_setg(errp, "Number of interrupts needs to be greater 0");
638 return;
4e4169f7 639 }
0a647b76 640 ics->irqs = g_malloc0(ics->nr_irqs * sizeof(ICSIRQState));
4e4169f7
CLG
641}
642
815049a0
CLG
643static void ics_base_instance_init(Object *obj)
644{
645 ICSState *ics = ICS_BASE(obj);
646
647 ics->offset = XICS_IRQ_BASE;
648}
649
c8b1846f
CLG
650static int ics_base_dispatch_pre_save(void *opaque)
651{
652 ICSState *ics = opaque;
653 ICSStateClass *info = ICS_BASE_GET_CLASS(ics);
654
655 if (info->pre_save) {
656 info->pre_save(ics);
657 }
658
659 return 0;
660}
661
662static int ics_base_dispatch_post_load(void *opaque, int version_id)
663{
664 ICSState *ics = opaque;
665 ICSStateClass *info = ICS_BASE_GET_CLASS(ics);
666
667 if (info->post_load) {
668 return info->post_load(ics, version_id);
669 }
670
671 return 0;
672}
673
674static const VMStateDescription vmstate_ics_base_irq = {
675 .name = "ics/irq",
676 .version_id = 2,
677 .minimum_version_id = 1,
678 .fields = (VMStateField[]) {
679 VMSTATE_UINT32(server, ICSIRQState),
680 VMSTATE_UINT8(priority, ICSIRQState),
681 VMSTATE_UINT8(saved_priority, ICSIRQState),
682 VMSTATE_UINT8(status, ICSIRQState),
683 VMSTATE_UINT8(flags, ICSIRQState),
684 VMSTATE_END_OF_LIST()
685 },
686};
687
688static const VMStateDescription vmstate_ics_base = {
689 .name = "ics",
690 .version_id = 1,
691 .minimum_version_id = 1,
692 .pre_save = ics_base_dispatch_pre_save,
693 .post_load = ics_base_dispatch_post_load,
694 .fields = (VMStateField[]) {
695 /* Sanity check */
696 VMSTATE_UINT32_EQUAL(nr_irqs, ICSState, NULL),
697
698 VMSTATE_STRUCT_VARRAY_POINTER_UINT32(irqs, ICSState, nr_irqs,
699 vmstate_ics_base_irq,
700 ICSIRQState),
701 VMSTATE_END_OF_LIST()
702 },
703};
704
0a647b76
CLG
705static Property ics_base_properties[] = {
706 DEFINE_PROP_UINT32("nr-irqs", ICSState, nr_irqs, 0),
707 DEFINE_PROP_END_OF_LIST(),
708};
709
4e4169f7
CLG
710static void ics_base_class_init(ObjectClass *klass, void *data)
711{
712 DeviceClass *dc = DEVICE_CLASS(klass);
713
714 dc->realize = ics_base_realize;
0a647b76 715 dc->props = ics_base_properties;
eeefd43b 716 dc->reset = ics_base_reset;
c8b1846f 717 dc->vmsd = &vmstate_ics_base;
4e4169f7
CLG
718}
719
d4d7a59a
BH
720static const TypeInfo ics_base_info = {
721 .name = TYPE_ICS_BASE,
c04d6cfa 722 .parent = TYPE_DEVICE,
d4d7a59a 723 .abstract = true,
c04d6cfa 724 .instance_size = sizeof(ICSState),
815049a0 725 .instance_init = ics_base_instance_init,
4e4169f7 726 .class_init = ics_base_class_init,
d1b5682d 727 .class_size = sizeof(ICSStateClass),
c04d6cfa
AL
728};
729
51b18005
CLG
730static const TypeInfo xics_fabric_info = {
731 .name = TYPE_XICS_FABRIC,
732 .parent = TYPE_INTERFACE,
733 .class_size = sizeof(XICSFabricClass),
734};
735
b5cec4c5
DG
736/*
737 * Exported functions
738 */
b4f27d71
CLG
739ICPState *xics_icp_get(XICSFabric *xi, int server)
740{
741 XICSFabricClass *xic = XICS_FABRIC_GET_CLASS(xi);
742
743 return xic->icp_get(xi, server);
744}
745
9c7027ba 746void ics_set_irq_type(ICSState *ics, int srcno, bool lsi)
4af88944
AK
747{
748 assert(!(ics->irqs[srcno].flags & XICS_FLAGS_IRQ_MASK));
749
750 ics->irqs[srcno].flags |=
751 lsi ? XICS_FLAGS_IRQ_LSI : XICS_FLAGS_IRQ_MSI;
752}
753
c04d6cfa
AL
754static void xics_register_types(void)
755{
d4d7a59a
BH
756 type_register_static(&ics_simple_info);
757 type_register_static(&ics_base_info);
c04d6cfa 758 type_register_static(&icp_info);
51b18005 759 type_register_static(&xics_fabric_info);
b5cec4c5 760}
c04d6cfa
AL
761
762type_init(xics_register_types)
This page took 0.768243 seconds and 4 git commands to generate.