]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm/arm/gic.c
Merge ACPICA 20140926.
[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_FIRST_IPI            0      /* Irqs 0-15 are SGIs/IPIs. */
87 #define GIC_LAST_IPI            15
88 #define GIC_FIRST_PPI           16      /* Irqs 16-31 are private (per */
89 #define GIC_LAST_PPI            31      /* core) peripheral interrupts. */
90 #define GIC_FIRST_SPI           32      /* Irqs 32+ are shared peripherals. */
91
92 /* First bit is a polarity bit (0 - low, 1 - high) */
93 #define GICD_ICFGR_POL_LOW      (0 << 0)
94 #define GICD_ICFGR_POL_HIGH     (1 << 0)
95 #define GICD_ICFGR_POL_MASK     0x1
96 /* Second bit is a trigger bit (0 - level, 1 - edge) */
97 #define GICD_ICFGR_TRIG_LVL     (0 << 1)
98 #define GICD_ICFGR_TRIG_EDGE    (1 << 1)
99 #define GICD_ICFGR_TRIG_MASK    0x2
100
101 struct arm_gic_softc {
102         struct resource *       gic_res[3];
103         bus_space_tag_t         gic_c_bst;
104         bus_space_tag_t         gic_d_bst;
105         bus_space_handle_t      gic_c_bsh;
106         bus_space_handle_t      gic_d_bsh;
107         uint8_t                 ver;
108         device_t                dev;
109         struct mtx              mutex;
110         uint32_t                nirqs;
111 };
112
113 static struct resource_spec arm_gic_spec[] = {
114         { SYS_RES_MEMORY,       0,      RF_ACTIVE },    /* Distributor registers */
115         { SYS_RES_MEMORY,       1,      RF_ACTIVE },    /* CPU Interrupt Intf. registers */
116         { -1, 0 }
117 };
118
119 static struct arm_gic_softc *arm_gic_sc = NULL;
120
121 #define gic_c_read_4(reg)               \
122     bus_space_read_4(arm_gic_sc->gic_c_bst, arm_gic_sc->gic_c_bsh, reg)
123 #define gic_c_write_4(reg, val)         \
124     bus_space_write_4(arm_gic_sc->gic_c_bst, arm_gic_sc->gic_c_bsh, reg, val)
125 #define gic_d_read_4(reg)               \
126     bus_space_read_4(arm_gic_sc->gic_d_bst, arm_gic_sc->gic_d_bsh, reg)
127 #define gic_d_write_4(reg, val)         \
128     bus_space_write_4(arm_gic_sc->gic_d_bst, arm_gic_sc->gic_d_bsh, reg, val)
129
130 static int gic_config_irq(int irq, enum intr_trigger trig,
131     enum intr_polarity pol);
132 static void gic_post_filter(void *);
133
134 static struct ofw_compat_data compat_data[] = {
135         {"arm,gic",             true},  /* Non-standard, used in FreeBSD dts. */
136         {"arm,gic-400",         true},
137         {"arm,cortex-a15-gic",  true},
138         {"arm,cortex-a9-gic",   true},
139         {"arm,cortex-a7-gic",   true},
140         {"arm,arm11mp-gic",     true},
141         {"brcm,brahma-b15-gic", true},
142         {NULL,                  false}
143 };
144
145 static int
146 arm_gic_probe(device_t dev)
147 {
148
149         if (!ofw_bus_status_okay(dev))
150                 return (ENXIO);
151
152         if (!ofw_bus_search_compatible(dev, compat_data)->ocd_data)
153                 return (ENXIO);
154         device_set_desc(dev, "ARM Generic Interrupt Controller");
155         return (BUS_PROBE_DEFAULT);
156 }
157
158 void
159 gic_init_secondary(void)
160 {
161         int i, nirqs;
162
163         /* Get the number of interrupts */
164         nirqs = gic_d_read_4(GICD_TYPER);
165         nirqs = 32 * ((nirqs & 0x1f) + 1);
166
167         for (i = 0; i < nirqs; i += 4)
168                 gic_d_write_4(GICD_IPRIORITYR(i >> 2), 0);
169
170         /* Set all the interrupts to be in Group 0 (secure) */
171         for (i = 0; i < nirqs; i += 32) {
172                 gic_d_write_4(GICD_IGROUPR(i >> 5), 0);
173         }
174
175         /* Enable CPU interface */
176         gic_c_write_4(GICC_CTLR, 1);
177
178         /* Set priority mask register. */
179         gic_c_write_4(GICC_PMR, 0xff);
180
181         /* Enable interrupt distribution */
182         gic_d_write_4(GICD_CTLR, 0x01);
183
184         /*
185          * Activate the timer interrupts: virtual, secure, and non-secure.
186          */
187         gic_d_write_4(GICD_ISENABLER(27 >> 5), (1UL << (27 & 0x1F)));
188         gic_d_write_4(GICD_ISENABLER(29 >> 5), (1UL << (29 & 0x1F)));
189         gic_d_write_4(GICD_ISENABLER(30 >> 5), (1UL << (30 & 0x1F)));
190 }
191
192 int
193 gic_decode_fdt(uint32_t iparent, uint32_t *intr, int *interrupt,
194     int *trig, int *pol)
195 {
196         static u_int num_intr_cells;
197
198         if (num_intr_cells == 0) {
199                 if (OF_searchencprop(OF_node_from_xref(iparent), 
200                     "#interrupt-cells", &num_intr_cells, 
201                     sizeof(num_intr_cells)) == -1) {
202                         num_intr_cells = 1;
203                 }
204         }
205
206         if (num_intr_cells == 1) {
207                 *interrupt = fdt32_to_cpu(intr[0]);
208                 *trig = INTR_TRIGGER_CONFORM;
209                 *pol = INTR_POLARITY_CONFORM;
210         } else {
211                 if (intr[0] == 0)
212                         *interrupt = fdt32_to_cpu(intr[1]) + GIC_FIRST_SPI;
213                 else
214                         *interrupt = fdt32_to_cpu(intr[1]) + GIC_FIRST_PPI;
215                 /*
216                  * In intr[2], bits[3:0] are trigger type and level flags.
217                  *   1 = low-to-high edge triggered
218                  *   2 = high-to-low edge triggered
219                  *   4 = active high level-sensitive
220                  *   8 = active low level-sensitive
221                  * The hardware only supports active-high-level or rising-edge.
222                  */
223                 if (intr[2] & 0x0a) {
224                         printf("unsupported trigger/polarity configuration "
225                             "0x%2x\n", intr[2] & 0x0f);
226                         return (ENOTSUP);
227                 }
228                 *pol  = INTR_POLARITY_CONFORM;
229                 if (intr[2] & 0x01)
230                         *trig = INTR_TRIGGER_EDGE;
231                 else
232                         *trig = INTR_TRIGGER_LEVEL;
233         }
234         return (0);
235 }
236
237 static int
238 arm_gic_attach(device_t dev)
239 {
240         struct          arm_gic_softc *sc;
241         int             i;
242         uint32_t        icciidr;
243
244         if (arm_gic_sc)
245                 return (ENXIO);
246
247         sc = device_get_softc(dev);
248         sc->dev = dev;
249
250         if (bus_alloc_resources(dev, arm_gic_spec, sc->gic_res)) {
251                 device_printf(dev, "could not allocate resources\n");
252                 return (ENXIO);
253         }
254
255         /* Initialize mutex */
256         mtx_init(&sc->mutex, "GIC lock", "", MTX_SPIN);
257
258         /* Distributor Interface */
259         sc->gic_d_bst = rman_get_bustag(sc->gic_res[0]);
260         sc->gic_d_bsh = rman_get_bushandle(sc->gic_res[0]);
261
262         /* CPU Interface */
263         sc->gic_c_bst = rman_get_bustag(sc->gic_res[1]);
264         sc->gic_c_bsh = rman_get_bushandle(sc->gic_res[1]);
265
266         arm_gic_sc = sc;
267
268         /* Disable interrupt forwarding to the CPU interface */
269         gic_d_write_4(GICD_CTLR, 0x00);
270
271         /* Get the number of interrupts */
272         sc->nirqs = gic_d_read_4(GICD_TYPER);
273         sc->nirqs = 32 * ((sc->nirqs & 0x1f) + 1);
274
275         /* Set up function pointers */
276         arm_post_filter = gic_post_filter;
277         arm_config_irq = gic_config_irq;
278
279         icciidr = gic_c_read_4(GICC_IIDR);
280         device_printf(dev,"pn 0x%x, arch 0x%x, rev 0x%x, implementer 0x%x sc->nirqs %u\n",
281                         icciidr>>20, (icciidr>>16) & 0xF, (icciidr>>12) & 0xf,
282                         (icciidr & 0xfff), sc->nirqs);
283
284         /* Set all global interrupts to be level triggered, active low. */
285         for (i = 32; i < sc->nirqs; i += 16) {
286                 gic_d_write_4(GICD_ICFGR(i >> 4), 0x00000000);
287         }
288
289         /* Disable all interrupts. */
290         for (i = 32; i < sc->nirqs; i += 32) {
291                 gic_d_write_4(GICD_ICENABLER(i >> 5), 0xFFFFFFFF);
292         }
293
294         for (i = 0; i < sc->nirqs; i += 4) {
295                 gic_d_write_4(GICD_IPRIORITYR(i >> 2), 0);
296                 gic_d_write_4(GICD_ITARGETSR(i >> 2), 1 << 0 | 1 << 8 | 1 << 16 | 1 << 24);
297         }
298
299         /* Set all the interrupts to be in Group 0 (secure) */
300         for (i = 0; i < sc->nirqs; i += 32) {
301                 gic_d_write_4(GICD_IGROUPR(i >> 5), 0);
302         }
303
304         /* Enable CPU interface */
305         gic_c_write_4(GICC_CTLR, 1);
306
307         /* Set priority mask register. */
308         gic_c_write_4(GICC_PMR, 0xff);
309
310         /* Enable interrupt distribution */
311         gic_d_write_4(GICD_CTLR, 0x01);
312
313         return (0);
314 }
315
316 static device_method_t arm_gic_methods[] = {
317         DEVMETHOD(device_probe,         arm_gic_probe),
318         DEVMETHOD(device_attach,        arm_gic_attach),
319         { 0, 0 }
320 };
321
322 static driver_t arm_gic_driver = {
323         "gic",
324         arm_gic_methods,
325         sizeof(struct arm_gic_softc),
326 };
327
328 static devclass_t arm_gic_devclass;
329
330 EARLY_DRIVER_MODULE(gic, simplebus, arm_gic_driver, arm_gic_devclass, 0, 0,
331     BUS_PASS_INTERRUPT + BUS_PASS_ORDER_MIDDLE);
332 EARLY_DRIVER_MODULE(gic, ofwbus, arm_gic_driver, arm_gic_devclass, 0, 0,
333     BUS_PASS_INTERRUPT + BUS_PASS_ORDER_MIDDLE);
334
335 static void
336 gic_post_filter(void *arg)
337 {
338         uintptr_t irq = (uintptr_t) arg;
339
340         if (irq > GIC_LAST_IPI)
341                 arm_irq_memory_barrier(irq);
342         gic_c_write_4(GICC_EOIR, irq);
343 }
344
345 int
346 arm_get_next_irq(int last_irq)
347 {
348         uint32_t active_irq;
349
350         active_irq = gic_c_read_4(GICC_IAR);
351
352         /*
353          * Immediatly EOIR the SGIs, because doing so requires the other
354          * bits (ie CPU number), not just the IRQ number, and we do not
355          * have this information later.
356          */
357
358         if ((active_irq & 0x3ff) <= GIC_LAST_IPI)
359                 gic_c_write_4(GICC_EOIR, active_irq);
360         active_irq &= 0x3FF;
361
362         if (active_irq == 0x3FF) {
363                 if (last_irq == -1)
364                         printf("Spurious interrupt detected\n");
365                 return -1;
366         }
367
368         return active_irq;
369 }
370
371 void
372 arm_mask_irq(uintptr_t nb)
373 {
374
375         gic_d_write_4(GICD_ICENABLER(nb >> 5), (1UL << (nb & 0x1F)));
376         gic_c_write_4(GICC_EOIR, nb);
377 }
378
379 void
380 arm_unmask_irq(uintptr_t nb)
381 {
382
383         if (nb > GIC_LAST_IPI)
384                 arm_irq_memory_barrier(nb);
385         gic_d_write_4(GICD_ISENABLER(nb >> 5), (1UL << (nb & 0x1F)));
386 }
387
388 static int
389 gic_config_irq(int irq, enum intr_trigger trig,
390     enum intr_polarity pol)
391 {
392         uint32_t reg;
393         uint32_t mask;
394
395         /* Function is public-accessible, so validate input arguments */
396         if ((irq < 0) || (irq >= arm_gic_sc->nirqs))
397                 goto invalid_args;
398         if ((trig != INTR_TRIGGER_EDGE) && (trig != INTR_TRIGGER_LEVEL) &&
399             (trig != INTR_TRIGGER_CONFORM))
400                 goto invalid_args;
401         if ((pol != INTR_POLARITY_HIGH) && (pol != INTR_POLARITY_LOW) &&
402             (pol != INTR_POLARITY_CONFORM))
403                 goto invalid_args;
404
405         mtx_lock_spin(&arm_gic_sc->mutex);
406
407         reg = gic_d_read_4(GICD_ICFGR(irq >> 4));
408         mask = (reg >> 2*(irq % 16)) & 0x3;
409
410         if (pol == INTR_POLARITY_LOW) {
411                 mask &= ~GICD_ICFGR_POL_MASK;
412                 mask |= GICD_ICFGR_POL_LOW;
413         } else if (pol == INTR_POLARITY_HIGH) {
414                 mask &= ~GICD_ICFGR_POL_MASK;
415                 mask |= GICD_ICFGR_POL_HIGH;
416         }
417
418         if (trig == INTR_TRIGGER_LEVEL) {
419                 mask &= ~GICD_ICFGR_TRIG_MASK;
420                 mask |= GICD_ICFGR_TRIG_LVL;
421         } else if (trig == INTR_TRIGGER_EDGE) {
422                 mask &= ~GICD_ICFGR_TRIG_MASK;
423                 mask |= GICD_ICFGR_TRIG_EDGE;
424         }
425
426         /* Set mask */
427         reg = reg & ~(0x3 << 2*(irq % 16));
428         reg = reg | (mask << 2*(irq % 16));
429         gic_d_write_4(GICD_ICFGR(irq >> 4), reg);
430
431         mtx_unlock_spin(&arm_gic_sc->mutex);
432
433         return (0);
434
435 invalid_args:
436         device_printf(arm_gic_sc->dev, "gic_config_irg, invalid parameters\n");
437         return (EINVAL);
438 }
439
440 #ifdef SMP
441 void
442 pic_ipi_send(cpuset_t cpus, u_int ipi)
443 {
444         uint32_t val = 0, i;
445
446         for (i = 0; i < MAXCPU; i++)
447                 if (CPU_ISSET(i, &cpus))
448                         val |= 1 << (16 + i);
449         gic_d_write_4(GICD_SGIR(0), val | ipi);
450
451 }
452
453 int
454 pic_ipi_get(int i)
455 {
456
457         if (i != -1) {
458                 /*
459                  * The intr code will automagically give the frame pointer
460                  * if the interrupt argument is 0.
461                  */
462                 if ((unsigned int)i > 16)
463                         return (0);
464                 return (i);
465         }
466         return (0x3ff);
467 }
468
469 void
470 pic_ipi_clear(int ipi)
471 {
472 }
473 #endif
474