Statistics
| Branch: | Revision:

root / hw / irq.c @ 5f8ae8e2

History | View | Annotate | Download (3.7 kB)

1 d537cf6c pbrook
/*
2 d537cf6c pbrook
 * QEMU IRQ/GPIO common code.
3 5fafdf24 ths
 *
4 d537cf6c pbrook
 * Copyright (c) 2007 CodeSourcery.
5 5fafdf24 ths
 *
6 d537cf6c pbrook
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 d537cf6c pbrook
 * of this software and associated documentation files (the "Software"), to deal
8 d537cf6c pbrook
 * in the Software without restriction, including without limitation the rights
9 d537cf6c pbrook
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 d537cf6c pbrook
 * copies of the Software, and to permit persons to whom the Software is
11 d537cf6c pbrook
 * furnished to do so, subject to the following conditions:
12 d537cf6c pbrook
 *
13 d537cf6c pbrook
 * The above copyright notice and this permission notice shall be included in
14 d537cf6c pbrook
 * all copies or substantial portions of the Software.
15 d537cf6c pbrook
 *
16 d537cf6c pbrook
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 d537cf6c pbrook
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 d537cf6c pbrook
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 d537cf6c pbrook
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 d537cf6c pbrook
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 d537cf6c pbrook
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 d537cf6c pbrook
 * THE SOFTWARE.
23 d537cf6c pbrook
 */
24 87ecb68b pbrook
#include "qemu-common.h"
25 87ecb68b pbrook
#include "irq.h"
26 d537cf6c pbrook
27 d537cf6c pbrook
struct IRQState {
28 d537cf6c pbrook
    qemu_irq_handler handler;
29 d537cf6c pbrook
    void *opaque;
30 d537cf6c pbrook
    int n;
31 d537cf6c pbrook
};
32 d537cf6c pbrook
33 d537cf6c pbrook
void qemu_set_irq(qemu_irq irq, int level)
34 d537cf6c pbrook
{
35 d537cf6c pbrook
    if (!irq)
36 d537cf6c pbrook
        return;
37 d537cf6c pbrook
38 d537cf6c pbrook
    irq->handler(irq->opaque, irq->n, level);
39 d537cf6c pbrook
}
40 d537cf6c pbrook
41 1e5b31e6 Peter A. G. Crosthwaite
qemu_irq *qemu_extend_irqs(qemu_irq *old, int n_old, qemu_irq_handler handler,
42 1e5b31e6 Peter A. G. Crosthwaite
                           void *opaque, int n)
43 d537cf6c pbrook
{
44 d537cf6c pbrook
    qemu_irq *s;
45 d537cf6c pbrook
    struct IRQState *p;
46 d537cf6c pbrook
    int i;
47 d537cf6c pbrook
48 1e5b31e6 Peter A. G. Crosthwaite
    if (!old) {
49 1e5b31e6 Peter A. G. Crosthwaite
        n_old = 0;
50 1e5b31e6 Peter A. G. Crosthwaite
    }
51 1e5b31e6 Peter A. G. Crosthwaite
    s = old ? g_renew(qemu_irq, old, n + n_old) : g_new(qemu_irq, n);
52 1e5b31e6 Peter A. G. Crosthwaite
    p = old ? g_renew(struct IRQState, s[0], n + n_old) :
53 1e5b31e6 Peter A. G. Crosthwaite
                g_new(struct IRQState, n);
54 1e5b31e6 Peter A. G. Crosthwaite
    for (i = 0; i < n + n_old; i++) {
55 1e5b31e6 Peter A. G. Crosthwaite
        if (i >= n_old) {
56 1e5b31e6 Peter A. G. Crosthwaite
            p->handler = handler;
57 1e5b31e6 Peter A. G. Crosthwaite
            p->opaque = opaque;
58 1e5b31e6 Peter A. G. Crosthwaite
            p->n = i;
59 1e5b31e6 Peter A. G. Crosthwaite
        }
60 d537cf6c pbrook
        s[i] = p;
61 d537cf6c pbrook
        p++;
62 d537cf6c pbrook
    }
63 d537cf6c pbrook
    return s;
64 d537cf6c pbrook
}
65 d537cf6c pbrook
66 1e5b31e6 Peter A. G. Crosthwaite
qemu_irq *qemu_allocate_irqs(qemu_irq_handler handler, void *opaque, int n)
67 1e5b31e6 Peter A. G. Crosthwaite
{
68 1e5b31e6 Peter A. G. Crosthwaite
    return qemu_extend_irqs(NULL, 0, handler, opaque, n);
69 1e5b31e6 Peter A. G. Crosthwaite
}
70 1e5b31e6 Peter A. G. Crosthwaite
71 1e5b31e6 Peter A. G. Crosthwaite
72 51bf9e7e aliguori
void qemu_free_irqs(qemu_irq *s)
73 51bf9e7e aliguori
{
74 7267c094 Anthony Liguori
    g_free(s[0]);
75 7267c094 Anthony Liguori
    g_free(s);
76 51bf9e7e aliguori
}
77 51bf9e7e aliguori
78 b50a6563 balrog
static void qemu_notirq(void *opaque, int line, int level)
79 b50a6563 balrog
{
80 b50a6563 balrog
    struct IRQState *irq = opaque;
81 b50a6563 balrog
82 b50a6563 balrog
    irq->handler(irq->opaque, irq->n, !level);
83 b50a6563 balrog
}
84 b50a6563 balrog
85 b50a6563 balrog
qemu_irq qemu_irq_invert(qemu_irq irq)
86 b50a6563 balrog
{
87 cf0dbb21 pbrook
    /* The default state for IRQs is low, so raise the output now.  */
88 cf0dbb21 pbrook
    qemu_irq_raise(irq);
89 b50a6563 balrog
    return qemu_allocate_irqs(qemu_notirq, irq, 1)[0];
90 b50a6563 balrog
}
91 9793212b Peter Maydell
92 9793212b Peter Maydell
static void qemu_splitirq(void *opaque, int line, int level)
93 9793212b Peter Maydell
{
94 9793212b Peter Maydell
    struct IRQState **irq = opaque;
95 9793212b Peter Maydell
    irq[0]->handler(irq[0]->opaque, irq[0]->n, level);
96 9793212b Peter Maydell
    irq[1]->handler(irq[1]->opaque, irq[1]->n, level);
97 9793212b Peter Maydell
}
98 9793212b Peter Maydell
99 9793212b Peter Maydell
qemu_irq qemu_irq_split(qemu_irq irq1, qemu_irq irq2)
100 9793212b Peter Maydell
{
101 7267c094 Anthony Liguori
    qemu_irq *s = g_malloc0(2 * sizeof(qemu_irq));
102 9793212b Peter Maydell
    s[0] = irq1;
103 9793212b Peter Maydell
    s[1] = irq2;
104 9793212b Peter Maydell
    return qemu_allocate_irqs(qemu_splitirq, s, 1)[0];
105 9793212b Peter Maydell
}
106 22ec3283 Avi Kivity
107 22ec3283 Avi Kivity
static void proxy_irq_handler(void *opaque, int n, int level)
108 22ec3283 Avi Kivity
{
109 22ec3283 Avi Kivity
    qemu_irq **target = opaque;
110 22ec3283 Avi Kivity
111 22ec3283 Avi Kivity
    if (*target) {
112 22ec3283 Avi Kivity
        qemu_set_irq((*target)[n], level);
113 22ec3283 Avi Kivity
    }
114 22ec3283 Avi Kivity
}
115 22ec3283 Avi Kivity
116 22ec3283 Avi Kivity
qemu_irq *qemu_irq_proxy(qemu_irq **target, int n)
117 22ec3283 Avi Kivity
{
118 22ec3283 Avi Kivity
    return qemu_allocate_irqs(proxy_irq_handler, target, n);
119 22ec3283 Avi Kivity
}
120 20288345 Paolo Bonzini
121 20288345 Paolo Bonzini
void qemu_irq_intercept_in(qemu_irq *gpio_in, qemu_irq_handler handler, int n)
122 20288345 Paolo Bonzini
{
123 20288345 Paolo Bonzini
    int i;
124 20288345 Paolo Bonzini
    qemu_irq *old_irqs = qemu_allocate_irqs(NULL, NULL, n);
125 20288345 Paolo Bonzini
    for (i = 0; i < n; i++) {
126 20288345 Paolo Bonzini
        *old_irqs[i] = *gpio_in[i];
127 20288345 Paolo Bonzini
        gpio_in[i]->handler = handler;
128 20288345 Paolo Bonzini
        gpio_in[i]->opaque = old_irqs;
129 20288345 Paolo Bonzini
    }
130 20288345 Paolo Bonzini
}
131 20288345 Paolo Bonzini
132 20288345 Paolo Bonzini
void qemu_irq_intercept_out(qemu_irq **gpio_out, qemu_irq_handler handler, int n)
133 20288345 Paolo Bonzini
{
134 20288345 Paolo Bonzini
    qemu_irq *old_irqs = *gpio_out;
135 20288345 Paolo Bonzini
    *gpio_out = qemu_allocate_irqs(handler, old_irqs, n);
136 20288345 Paolo Bonzini
}