blob: 8a9cbdd5561a0d0c35b5139df51ec980ac4c9d21 [file] [log] [blame]
pbrookd537cf62007-04-07 18:14:41 +00001/*
2 * QEMU IRQ/GPIO common code.
ths5fafdf22007-09-16 21:08:06 +00003 *
pbrookd537cf62007-04-07 18:14:41 +00004 * Copyright (c) 2007 CodeSourcery.
ths5fafdf22007-09-16 21:08:06 +00005 *
pbrookd537cf62007-04-07 18:14:41 +00006 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
Peter Maydell18c86e22016-01-26 18:17:29 +000024#include "qemu/osdep.h"
Jan Kiszka8d04fb52017-02-23 18:29:11 +000025#include "qemu/main-loop.h"
Paolo Bonzini83c9f4c2013-02-04 15:40:22 +010026#include "hw/irq.h"
Andreas Färber615c4892014-06-18 00:57:08 -070027#include "qom/object.h"
28
Eduardo Habkost8110fa12020-08-31 17:07:33 -040029DECLARE_INSTANCE_CHECKER(struct IRQState, IRQ,
30 TYPE_IRQ)
pbrookd537cf62007-04-07 18:14:41 +000031
32struct IRQState {
Andreas Färber615c4892014-06-18 00:57:08 -070033 Object parent_obj;
34
pbrookd537cf62007-04-07 18:14:41 +000035 qemu_irq_handler handler;
36 void *opaque;
37 int n;
38};
39
40void qemu_set_irq(qemu_irq irq, int level)
41{
42 if (!irq)
43 return;
44
45 irq->handler(irq->opaque, irq->n, level);
46}
47
Peter A. G. Crosthwaite1e5b31e2012-07-31 12:24:06 +100048qemu_irq *qemu_extend_irqs(qemu_irq *old, int n_old, qemu_irq_handler handler,
49 void *opaque, int n)
pbrookd537cf62007-04-07 18:14:41 +000050{
51 qemu_irq *s;
pbrookd537cf62007-04-07 18:14:41 +000052 int i;
53
Peter A. G. Crosthwaite1e5b31e2012-07-31 12:24:06 +100054 if (!old) {
55 n_old = 0;
56 }
57 s = old ? g_renew(qemu_irq, old, n + n_old) : g_new(qemu_irq, n);
Peter Crosthwaitef173d572014-06-18 00:56:31 -070058 for (i = n_old; i < n + n_old; i++) {
59 s[i] = qemu_allocate_irq(handler, opaque, i);
pbrookd537cf62007-04-07 18:14:41 +000060 }
61 return s;
62}
63
Peter A. G. Crosthwaite1e5b31e2012-07-31 12:24:06 +100064qemu_irq *qemu_allocate_irqs(qemu_irq_handler handler, void *opaque, int n)
65{
66 return qemu_extend_irqs(NULL, 0, handler, opaque, n);
67}
68
Marcel Apfelbauma8a9d302013-10-07 10:36:34 +030069qemu_irq qemu_allocate_irq(qemu_irq_handler handler, void *opaque, int n)
70{
71 struct IRQState *irq;
72
Andreas Färber615c4892014-06-18 00:57:08 -070073 irq = IRQ(object_new(TYPE_IRQ));
Marcel Apfelbauma8a9d302013-10-07 10:36:34 +030074 irq->handler = handler;
75 irq->opaque = opaque;
76 irq->n = n;
77
78 return irq;
79}
Peter A. G. Crosthwaite1e5b31e2012-07-31 12:24:06 +100080
Peter Crosthwaitef173d572014-06-18 00:56:31 -070081void qemu_free_irqs(qemu_irq *s, int n)
aliguori51bf9e72009-02-11 15:21:04 +000082{
Peter Crosthwaitef173d572014-06-18 00:56:31 -070083 int i;
84 for (i = 0; i < n; i++) {
85 qemu_free_irq(s[i]);
86 }
Anthony Liguori7267c092011-08-20 22:09:37 -050087 g_free(s);
aliguori51bf9e72009-02-11 15:21:04 +000088}
89
Marcel Apfelbauma8a9d302013-10-07 10:36:34 +030090void qemu_free_irq(qemu_irq irq)
91{
Andreas Färber615c4892014-06-18 00:57:08 -070092 object_unref(OBJECT(irq));
Marcel Apfelbauma8a9d302013-10-07 10:36:34 +030093}
94
balrogb50a6562007-10-29 10:59:29 +000095static void qemu_notirq(void *opaque, int line, int level)
96{
97 struct IRQState *irq = opaque;
98
99 irq->handler(irq->opaque, irq->n, !level);
100}
101
102qemu_irq qemu_irq_invert(qemu_irq irq)
103{
pbrookcf0dbb22007-11-18 14:36:08 +0000104 /* The default state for IRQs is low, so raise the output now. */
105 qemu_irq_raise(irq);
Andreas Färberf3c7d032014-06-18 00:55:18 -0700106 return qemu_allocate_irq(qemu_notirq, irq, 0);
balrogb50a6562007-10-29 10:59:29 +0000107}
Peter Maydell97932122011-02-21 20:57:52 +0000108
109static void qemu_splitirq(void *opaque, int line, int level)
110{
111 struct IRQState **irq = opaque;
112 irq[0]->handler(irq[0]->opaque, irq[0]->n, level);
113 irq[1]->handler(irq[1]->opaque, irq[1]->n, level);
114}
115
116qemu_irq qemu_irq_split(qemu_irq irq1, qemu_irq irq2)
117{
Anthony Liguori7267c092011-08-20 22:09:37 -0500118 qemu_irq *s = g_malloc0(2 * sizeof(qemu_irq));
Peter Maydell97932122011-02-21 20:57:52 +0000119 s[0] = irq1;
120 s[1] = irq2;
Andreas Färberf3c7d032014-06-18 00:55:18 -0700121 return qemu_allocate_irq(qemu_splitirq, s, 0);
Peter Maydell97932122011-02-21 20:57:52 +0000122}
Avi Kivity22ec3282011-09-18 15:58:26 +0300123
Paolo Bonzini20288342012-03-28 15:42:03 +0200124void qemu_irq_intercept_in(qemu_irq *gpio_in, qemu_irq_handler handler, int n)
125{
126 int i;
127 qemu_irq *old_irqs = qemu_allocate_irqs(NULL, NULL, n);
128 for (i = 0; i < n; i++) {
129 *old_irqs[i] = *gpio_in[i];
130 gpio_in[i]->handler = handler;
Peter Crosthwaite60a79012014-09-25 22:21:31 -0700131 gpio_in[i]->opaque = &old_irqs[i];
Paolo Bonzini20288342012-03-28 15:42:03 +0200132 }
133}
134
Andreas Färber615c4892014-06-18 00:57:08 -0700135static const TypeInfo irq_type_info = {
136 .name = TYPE_IRQ,
137 .parent = TYPE_OBJECT,
138 .instance_size = sizeof(struct IRQState),
139};
140
141static void irq_register_types(void)
142{
143 type_register_static(&irq_type_info);
144}
145
146type_init(irq_register_types)