]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm/arm/gic.c
Add the virtual timer irq to the list of interrupts we enable on secondary
[FreeBSD/FreeBSD.git] / sys / arm / arm / gic.c
1 /*-
2  * Copyright (c) 2011 The FreeBSD Foundation
3  * All rights reserved.
4  *
5  * Developed by Damjan Marion <damjan.marion@gmail.com>
6  *
7  * Based on OMAP4 GIC code by Ben Gray
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. The name of the company nor the name of the author may be used to
18  *    endorse or promote products derived from this software without specific
19  *    prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/bus.h>
40 #include <sys/kernel.h>
41 #include <sys/ktr.h>
42 #include <sys/module.h>
43 #include <sys/rman.h>
44 #include <sys/pcpu.h>
45 #include <sys/proc.h>
46 #include <sys/cpuset.h>
47 #include <sys/lock.h>
48 #include <sys/mutex.h>
49 #include <machine/bus.h>
50 #include <machine/intr.h>
51 #include <machine/smp.h>
52
53 #include <dev/fdt/fdt_common.h>
54 #include <dev/ofw/openfirm.h>
55 #include <dev/ofw/ofw_bus.h>
56 #include <dev/ofw/ofw_bus_subr.h>
57
58 /* We are using GICv2 register naming */
59
60 /* Distributor Registers */
61 #define GICD_CTLR               0x000                   /* v1 ICDDCR */
62 #define GICD_TYPER              0x004                   /* v1 ICDICTR */
63 #define GICD_IIDR               0x008                   /* v1 ICDIIDR */
64 #define GICD_IGROUPR(n)         (0x0080 + ((n) * 4))    /* v1 ICDISER */
65 #define GICD_ISENABLER(n)       (0x0100 + ((n) * 4))    /* v1 ICDISER */
66 #define GICD_ICENABLER(n)       (0x0180 + ((n) * 4))    /* v1 ICDICER */
67 #define GICD_ISPENDR(n)         (0x0200 + ((n) * 4))    /* v1 ICDISPR */
68 #define GICD_ICPENDR(n)         (0x0280 + ((n) * 4))    /* v1 ICDICPR */
69 #define GICD_ICACTIVER(n)       (0x0380 + ((n) * 4))    /* v1 ICDABR */
70 #define GICD_IPRIORITYR(n)      (0x0400 + ((n) * 4))    /* v1 ICDIPR */
71 #define GICD_ITARGETSR(n)       (0x0800 + ((n) * 4))    /* v1 ICDIPTR */
72 #define GICD_ICFGR(n)           (0x0C00 + ((n) * 4))    /* v1 ICDICFR */
73 #define GICD_SGIR(n)            (0x0F00 + ((n) * 4))    /* v1 ICDSGIR */
74
75 /* CPU Registers */
76 #define GICC_CTLR               0x0000                  /* v1 ICCICR */
77 #define GICC_PMR                0x0004                  /* v1 ICCPMR */
78 #define GICC_BPR                0x0008                  /* v1 ICCBPR */
79 #define GICC_IAR                0x000C                  /* v1 ICCIAR */
80 #define GICC_EOIR               0x0010                  /* v1 ICCEOIR */
81 #define GICC_RPR                0x0014                  /* v1 ICCRPR */
82 #define GICC_HPPIR              0x0018                  /* v1 ICCHPIR */
83 #define GICC_ABPR               0x001C                  /* v1 ICCABPR */
84 #define GICC_IIDR               0x00FC                  /* v1 ICCIIDR*/
85
86 #define GIC_LAST_IPI            15      /* Irqs 0-15 are IPIs. */
87
88 /* First bit is a polarity bit (0 - low, 1 - high) */
89 #define GICD_ICFGR_POL_LOW      (0 << 0)
90 #define GICD_ICFGR_POL_HIGH     (1 << 0)
91 #define GICD_ICFGR_POL_MASK     0x1
92 /* Second bit is a trigger bit (0 - level, 1 - edge) */
93 #define GICD_ICFGR_TRIG_LVL     (0 << 1)
94 #define GICD_ICFGR_TRIG_EDGE    (1 << 1)
95 #define GICD_ICFGR_TRIG_MASK    0x2
96
97 struct arm_gic_softc {
98         struct resource *       gic_res[3];
99         bus_space_tag_t         gic_c_bst;
100         bus_space_tag_t         gic_d_bst;
101         bus_space_handle_t      gic_c_bsh;
102         bus_space_handle_t      gic_d_bsh;
103         uint8_t                 ver;
104         device_t                dev;
105         struct mtx              mutex;
106         uint32_t                nirqs;
107 };
108
109 static struct resource_spec arm_gic_spec[] = {
110         { SYS_RES_MEMORY,       0,      RF_ACTIVE },    /* Distributor registers */
111         { SYS_RES_MEMORY,       1,      RF_ACTIVE },    /* CPU Interrupt Intf. registers */
112         { -1, 0 }
113 };
114
115 static struct arm_gic_softc *arm_gic_sc = NULL;
116
117 #define gic_c_read_4(reg)               \
118     bus_space_read_4(arm_gic_sc->gic_c_bst, arm_gic_sc->gic_c_bsh, reg)
119 #define gic_c_write_4(reg, val)         \
120     bus_space_write_4(arm_gic_sc->gic_c_bst, arm_gic_sc->gic_c_bsh, reg, val)
121 #define gic_d_read_4(reg)               \
122     bus_space_read_4(arm_gic_sc->gic_d_bst, arm_gic_sc->gic_d_bsh, reg)
123 #define gic_d_write_4(reg, val)         \
124     bus_space_write_4(arm_gic_sc->gic_d_bst, arm_gic_sc->gic_d_bsh, reg, val)
125
126 static int gic_config_irq(int irq, enum intr_trigger trig,
127     enum intr_polarity pol);
128 static void gic_post_filter(void *);
129
130 static int
131 arm_gic_probe(device_t dev)
132 {
133
134         if (!ofw_bus_status_okay(dev))
135                 return (ENXIO);
136
137         if (!ofw_bus_is_compatible(dev, "arm,gic"))
138                 return (ENXIO);
139         device_set_desc(dev, "ARM Generic Interrupt Controller");
140         return (BUS_PROBE_DEFAULT);
141 }
142
143 void
144 gic_init_secondary(void)
145 {
146         int i, nirqs;
147
148         /* Get the number of interrupts */
149         nirqs = gic_d_read_4(GICD_TYPER);
150         nirqs = 32 * ((nirqs & 0x1f) + 1);
151
152         for (i = 0; i < nirqs; i += 4)
153                 gic_d_write_4(GICD_IPRIORITYR(i >> 2), 0);
154
155         /* Set all the interrupts to be in Group 0 (secure) */
156         for (i = 0; i < nirqs; i += 32) {
157                 gic_d_write_4(GICD_IGROUPR(i >> 5), 0);
158         }
159
160         /* Enable CPU interface */
161         gic_c_write_4(GICC_CTLR, 1);
162
163         /* Set priority mask register. */
164         gic_c_write_4(GICC_PMR, 0xff);
165
166         /* Enable interrupt distribution */
167         gic_d_write_4(GICD_CTLR, 0x01);
168
169         /*
170          * Activate the timer interrupts: virtual, secure, and non-secure.
171          */
172         gic_d_write_4(GICD_ISENABLER(27 >> 5), (1UL << (27 & 0x1F)));
173         gic_d_write_4(GICD_ISENABLER(29 >> 5), (1UL << (29 & 0x1F)));
174         gic_d_write_4(GICD_ISENABLER(30 >> 5), (1UL << (30 & 0x1F)));
175 }
176
177 static int
178 arm_gic_attach(device_t dev)
179 {
180         struct          arm_gic_softc *sc;
181         int             i;
182         uint32_t        icciidr;
183
184         if (arm_gic_sc)
185                 return (ENXIO);
186
187         sc = device_get_softc(dev);
188         sc->dev = dev;
189
190         if (bus_alloc_resources(dev, arm_gic_spec, sc->gic_res)) {
191                 device_printf(dev, "could not allocate resources\n");
192                 return (ENXIO);
193         }
194
195         /* Initialize mutex */
196         mtx_init(&sc->mutex, "GIC lock", "", MTX_SPIN);
197
198         /* Distributor Interface */
199         sc->gic_d_bst = rman_get_bustag(sc->gic_res[0]);
200         sc->gic_d_bsh = rman_get_bushandle(sc->gic_res[0]);
201
202         /* CPU Interface */
203         sc->gic_c_bst = rman_get_bustag(sc->gic_res[1]);
204         sc->gic_c_bsh = rman_get_bushandle(sc->gic_res[1]);
205
206         arm_gic_sc = sc;
207
208         /* Disable interrupt forwarding to the CPU interface */
209         gic_d_write_4(GICD_CTLR, 0x00);
210
211         /* Get the number of interrupts */
212         sc->nirqs = gic_d_read_4(GICD_TYPER);
213         sc->nirqs = 32 * ((sc->nirqs & 0x1f) + 1);
214
215         /* Set up function pointers */
216         arm_post_filter = gic_post_filter;
217         arm_config_irq = gic_config_irq;
218
219         icciidr = gic_c_read_4(GICC_IIDR);
220         device_printf(dev,"pn 0x%x, arch 0x%x, rev 0x%x, implementer 0x%x sc->nirqs %u\n",
221                         icciidr>>20, (icciidr>>16) & 0xF, (icciidr>>12) & 0xf,
222                         (icciidr & 0xfff), sc->nirqs);
223
224         /* Set all global interrupts to be level triggered, active low. */
225         for (i = 32; i < sc->nirqs; i += 16) {
226                 gic_d_write_4(GICD_ICFGR(i >> 4), 0x00000000);
227         }
228
229         /* Disable all interrupts. */
230         for (i = 32; i < sc->nirqs; i += 32) {
231                 gic_d_write_4(GICD_ICENABLER(i >> 5), 0xFFFFFFFF);
232         }
233
234         for (i = 0; i < sc->nirqs; i += 4) {
235                 gic_d_write_4(GICD_IPRIORITYR(i >> 2), 0);
236                 gic_d_write_4(GICD_ITARGETSR(i >> 2), 1 << 0 | 1 << 8 | 1 << 16 | 1 << 24);
237         }
238
239         /* Set all the interrupts to be in Group 0 (secure) */
240         for (i = 0; i < sc->nirqs; i += 32) {
241                 gic_d_write_4(GICD_IGROUPR(i >> 5), 0);
242         }
243
244         /* Enable CPU interface */
245         gic_c_write_4(GICC_CTLR, 1);
246
247         /* Set priority mask register. */
248         gic_c_write_4(GICC_PMR, 0xff);
249
250         /* Enable interrupt distribution */
251         gic_d_write_4(GICD_CTLR, 0x01);
252
253         return (0);
254 }
255
256 static device_method_t arm_gic_methods[] = {
257         DEVMETHOD(device_probe,         arm_gic_probe),
258         DEVMETHOD(device_attach,        arm_gic_attach),
259         { 0, 0 }
260 };
261
262 static driver_t arm_gic_driver = {
263         "gic",
264         arm_gic_methods,
265         sizeof(struct arm_gic_softc),
266 };
267
268 static devclass_t arm_gic_devclass;
269
270 EARLY_DRIVER_MODULE(gic, simplebus, arm_gic_driver, arm_gic_devclass, 0, 0,
271     BUS_PASS_INTERRUPT + BUS_PASS_ORDER_MIDDLE);
272
273 static void
274 gic_post_filter(void *arg)
275 {
276         uintptr_t irq = (uintptr_t) arg;
277
278         if (irq > GIC_LAST_IPI)
279                 arm_irq_memory_barrier(irq);
280         gic_c_write_4(GICC_EOIR, irq);
281 }
282
283 int
284 arm_get_next_irq(int last_irq)
285 {
286         uint32_t active_irq;
287
288         active_irq = gic_c_read_4(GICC_IAR);
289
290         /*
291          * Immediatly EOIR the SGIs, because doing so requires the other
292          * bits (ie CPU number), not just the IRQ number, and we do not
293          * have this information later.
294          */
295
296         if ((active_irq & 0x3ff) <= GIC_LAST_IPI)
297                 gic_c_write_4(GICC_EOIR, active_irq);
298         active_irq &= 0x3FF;
299
300         if (active_irq == 0x3FF) {
301                 if (last_irq == -1)
302                         printf("Spurious interrupt detected\n");
303                 return -1;
304         }
305
306         return active_irq;
307 }
308
309 void
310 arm_mask_irq(uintptr_t nb)
311 {
312
313         gic_d_write_4(GICD_ICENABLER(nb >> 5), (1UL << (nb & 0x1F)));
314         gic_c_write_4(GICC_EOIR, nb);
315 }
316
317 void
318 arm_unmask_irq(uintptr_t nb)
319 {
320
321         if (nb > GIC_LAST_IPI)
322                 arm_irq_memory_barrier(nb);
323         gic_d_write_4(GICD_ISENABLER(nb >> 5), (1UL << (nb & 0x1F)));
324 }
325
326 static int
327 gic_config_irq(int irq, enum intr_trigger trig,
328     enum intr_polarity pol)
329 {
330         uint32_t reg;
331         uint32_t mask;
332
333         /* Function is public-accessible, so validate input arguments */
334         if ((irq < 0) || (irq >= arm_gic_sc->nirqs))
335                 goto invalid_args;
336         if ((trig != INTR_TRIGGER_EDGE) && (trig != INTR_TRIGGER_LEVEL) &&
337             (trig != INTR_TRIGGER_CONFORM))
338                 goto invalid_args;
339         if ((pol != INTR_POLARITY_HIGH) && (pol != INTR_POLARITY_LOW) &&
340             (pol != INTR_POLARITY_CONFORM))
341                 goto invalid_args;
342
343         mtx_lock_spin(&arm_gic_sc->mutex);
344
345         reg = gic_d_read_4(GICD_ICFGR(irq >> 4));
346         mask = (reg >> 2*(irq % 16)) & 0x3;
347
348         if (pol == INTR_POLARITY_LOW) {
349                 mask &= ~GICD_ICFGR_POL_MASK;
350                 mask |= GICD_ICFGR_POL_LOW;
351         } else if (pol == INTR_POLARITY_HIGH) {
352                 mask &= ~GICD_ICFGR_POL_MASK;
353                 mask |= GICD_ICFGR_POL_HIGH;
354         }
355
356         if (trig == INTR_TRIGGER_LEVEL) {
357                 mask &= ~GICD_ICFGR_TRIG_MASK;
358                 mask |= GICD_ICFGR_TRIG_LVL;
359         } else if (trig == INTR_TRIGGER_EDGE) {
360                 mask &= ~GICD_ICFGR_TRIG_MASK;
361                 mask |= GICD_ICFGR_TRIG_EDGE;
362         }
363
364         /* Set mask */
365         reg = reg & ~(0x3 << 2*(irq % 16));
366         reg = reg | (mask << 2*(irq % 16));
367         gic_d_write_4(GICD_ICFGR(irq >> 4), reg);
368
369         mtx_unlock_spin(&arm_gic_sc->mutex);
370
371         return (0);
372
373 invalid_args:
374         device_printf(arm_gic_sc->dev, "gic_config_irg, invalid parameters\n");
375         return (EINVAL);
376 }
377
378 #ifdef SMP
379 void
380 pic_ipi_send(cpuset_t cpus, u_int ipi)
381 {
382         uint32_t val = 0, i;
383
384         for (i = 0; i < MAXCPU; i++)
385                 if (CPU_ISSET(i, &cpus))
386                         val |= 1 << (16 + i);
387         gic_d_write_4(GICD_SGIR(0), val | ipi);
388
389 }
390
391 int
392 pic_ipi_get(int i)
393 {
394
395         if (i != -1) {
396                 /*
397                  * The intr code will automagically give the frame pointer
398                  * if the interrupt argument is 0.
399                  */
400                 if ((unsigned int)i > 16)
401                         return (0);
402                 return (i);
403         }
404         return (0x3ff);
405 }
406
407 void
408 pic_ipi_clear(int ipi)
409 {
410 }
411 #endif
412