gic_pl400.c 7.0 KB
Newer Older
B
bigmagic 已提交
1
/*
B
bigmagic 已提交
2
 * Copyright (c) 2006-2018, RT-Thread Development Team
B
bigmagic 已提交
3
 *
B
bigmagic 已提交
4
 * SPDX-License-Identifier: Apache-2.0
B
bigmagic 已提交
5
 *
B
bigmagic 已提交
6 7 8 9 10 11
 * Change Logs:
 * Date           Author       Notes
 * 2013-07-20     Bernard      first version
 * 2014-04-03     Grissiom     many enhancements
 * 2018-11-22     Jesven       add rt_hw_ipi_send()
 *                             add rt_hw_ipi_handler_install()
B
bigmagic 已提交
12
 */
B
bigmagic 已提交
13
#include <rtthread.h>
B
bigmagic 已提交
14 15

#include "gic_pl400.h"
B
bigmagic 已提交
16
#include "cp15.h"
B
bigmagic 已提交
17 18
#include "iomap.h"

B
bigmagic 已提交
19 20 21 22
#define ARM_GIC_MAX_NR 1
struct arm_gic
{
    rt_uint32_t offset;         /* the first interrupt index in the vector table */
B
bigmagic 已提交
23

B
bigmagic 已提交
24 25 26
    rt_uint32_t dist_hw_base;   /* the base address of the gic distributor */
    rt_uint32_t cpu_hw_base;    /* the base addrees of the gic cpu interface */
};
B
bigmagic 已提交
27

B
bigmagic 已提交
28 29
/* 'ARM_GIC_MAX_NR' is the number of cores */
static struct arm_gic _gic_table[ARM_GIC_MAX_NR];
B
bigmagic 已提交
30

B
bigmagic 已提交
31
static unsigned int _gic_max_irq;
B
bigmagic 已提交
32

B
bigmagic 已提交
33 34 35
int arm_gic_get_active_irq(rt_uint32_t index)
{
    int irq;
B
bigmagic 已提交
36

B
bigmagic 已提交
37
    RT_ASSERT(index < ARM_GIC_MAX_NR);
B
bigmagic 已提交
38

B
bigmagic 已提交
39 40 41 42
    irq = GIC_CPU_INTACK(_gic_table[index].cpu_hw_base);
    irq += _gic_table[index].offset;
    return irq;
}
B
bigmagic 已提交
43

B
bigmagic 已提交
44 45 46
void arm_gic_ack(rt_uint32_t index, int irq)
{
    rt_uint32_t mask = 1 << (irq % 32);
B
bigmagic 已提交
47

B
bigmagic 已提交
48
    RT_ASSERT(index < ARM_GIC_MAX_NR);
B
bigmagic 已提交
49

B
bigmagic 已提交
50 51
    irq = irq - _gic_table[index].offset;
    RT_ASSERT(irq >= 0);
B
bigmagic 已提交
52

B
bigmagic 已提交
53 54 55 56
    GIC_DIST_ENABLE_CLEAR(_gic_table[index].dist_hw_base, irq) = mask;
    GIC_CPU_EOI(_gic_table[index].cpu_hw_base) = irq;
    GIC_DIST_ENABLE_SET(_gic_table[index].dist_hw_base, irq) = mask;
}
B
bigmagic 已提交
57

B
bigmagic 已提交
58 59 60
void arm_gic_mask(rt_uint32_t index, int irq)
{
    rt_uint32_t mask = 1 << (irq % 32);
B
bigmagic 已提交
61

B
bigmagic 已提交
62
    RT_ASSERT(index < ARM_GIC_MAX_NR);
B
bigmagic 已提交
63

B
bigmagic 已提交
64 65
    irq = irq - _gic_table[index].offset;
    RT_ASSERT(irq >= 0);
B
bigmagic 已提交
66

B
bigmagic 已提交
67 68
    GIC_DIST_ENABLE_CLEAR(_gic_table[index].dist_hw_base, irq) = mask;
}
B
bigmagic 已提交
69

B
bigmagic 已提交
70 71 72
void arm_gic_clear_pending(rt_uint32_t index, int irq)
{
    rt_uint32_t mask = 1 << (irq % 32);
B
bigmagic 已提交
73

B
bigmagic 已提交
74
    RT_ASSERT(index < ARM_GIC_MAX_NR);
B
bigmagic 已提交
75

B
bigmagic 已提交
76 77
    irq = irq - _gic_table[index].offset;
    RT_ASSERT(irq >= 0);
B
bigmagic 已提交
78

B
bigmagic 已提交
79
    GIC_DIST_PENDING_CLEAR(_gic_table[index].dist_hw_base, irq) = mask;
B
bigmagic 已提交
80 81
}

B
bigmagic 已提交
82
void arm_gic_clear_active(rt_uint32_t index, int irq)
B
bigmagic 已提交
83
{
B
bigmagic 已提交
84
    rt_uint32_t mask = 1 << (irq % 32);
B
bigmagic 已提交
85

B
bigmagic 已提交
86
    RT_ASSERT(index < ARM_GIC_MAX_NR);
B
bigmagic 已提交
87

B
bigmagic 已提交
88 89
    irq = irq - _gic_table[index].offset;
    RT_ASSERT(irq >= 0);
B
bigmagic 已提交
90

B
bigmagic 已提交
91
    GIC_DIST_ACTIVE_CLEAR(_gic_table[index].dist_hw_base, irq) = mask;
B
bigmagic 已提交
92 93
}

B
bigmagic 已提交
94 95
/* Set up the cpu mask for the specific interrupt */
void arm_gic_set_cpu(rt_uint32_t index, int irq, unsigned int cpumask)
B
bigmagic 已提交
96
{
B
bigmagic 已提交
97
    rt_uint32_t old_tgt;
B
bigmagic 已提交
98

B
bigmagic 已提交
99
    RT_ASSERT(index < ARM_GIC_MAX_NR);
B
bigmagic 已提交
100

B
bigmagic 已提交
101 102 103 104 105 106 107 108 109
    irq = irq - _gic_table[index].offset;
    RT_ASSERT(irq >= 0);

    old_tgt = GIC_DIST_TARGET(_gic_table[index].dist_hw_base, irq);

    old_tgt &= ~(0x0FFUL << ((irq % 4)*8));
    old_tgt |=   cpumask << ((irq % 4)*8);

    GIC_DIST_TARGET(_gic_table[index].dist_hw_base, irq) = old_tgt;
B
bigmagic 已提交
110 111
}

B
bigmagic 已提交
112
void arm_gic_umask(rt_uint32_t index, int irq)
B
bigmagic 已提交
113
{
B
bigmagic 已提交
114 115 116 117 118 119 120 121
    rt_uint32_t mask = 1 << (irq % 32);

    RT_ASSERT(index < ARM_GIC_MAX_NR);

    irq = irq - _gic_table[index].offset;
    RT_ASSERT(irq >= 0);

    GIC_DIST_ENABLE_SET(_gic_table[index].dist_hw_base, irq) = mask;
B
bigmagic 已提交
122 123
}

B
bigmagic 已提交
124
void arm_gic_dump_type(rt_uint32_t index)
B
bigmagic 已提交
125
{
B
bigmagic 已提交
126 127 128 129 130 131 132 133 134
    unsigned int gic_type;

    gic_type = GIC_DIST_TYPE(_gic_table[index].dist_hw_base);
    rt_kprintf("GICv%d on %p, max IRQs: %d, %s security extension(%08x)\n",
               (GIC_DIST_ICPIDR2(_gic_table[index].dist_hw_base) >> 4) & 0xf,
               _gic_table[index].dist_hw_base,
               _gic_max_irq,
               gic_type & (1 << 10) ? "has" : "no",
               gic_type);
B
bigmagic 已提交
135 136
}

B
bigmagic 已提交
137
void arm_gic_dump(rt_uint32_t index)
B
bigmagic 已提交
138
{
B
bigmagic 已提交
139 140 141 142 143 144 145 146 147 148
    unsigned int i, k;

    k = GIC_CPU_HIGHPRI(_gic_table[index].cpu_hw_base);
    rt_kprintf("--- high pending priority: %d(%08x)\n", k, k);
    rt_kprintf("--- hw mask ---\n");
    for (i = 0; i < _gic_max_irq / 32; i++)
    {
        rt_kprintf("0x%08x, ",
                   GIC_DIST_ENABLE_SET(_gic_table[index].dist_hw_base,
                                       i * 32));
B
bigmagic 已提交
149
    }
B
bigmagic 已提交
150 151 152 153 154 155
    rt_kprintf("\n--- hw pending ---\n");
    for (i = 0; i < _gic_max_irq / 32; i++)
    {
        rt_kprintf("0x%08x, ",
                   GIC_DIST_PENDING_SET(_gic_table[index].dist_hw_base,
                                        i * 32));
B
bigmagic 已提交
156
    }
B
bigmagic 已提交
157 158 159 160 161 162
    rt_kprintf("\n--- hw active ---\n");
    for (i = 0; i < _gic_max_irq / 32; i++)
    {
        rt_kprintf("0x%08x, ",
                   GIC_DIST_ACTIVE_SET(_gic_table[index].dist_hw_base,
                                       i * 32));
B
bigmagic 已提交
163
    }
B
bigmagic 已提交
164
    rt_kprintf("\n");
B
bigmagic 已提交
165
}
B
bigmagic 已提交
166 167 168 169
#ifdef RT_USING_FINSH
#include <finsh.h>
FINSH_FUNCTION_EXPORT_ALIAS(arm_gic_dump, gic, show gic status);
#endif
B
bigmagic 已提交
170

B
bigmagic 已提交
171
int arm_gic_dist_init(rt_uint32_t index, rt_uint32_t dist_base, int irq_start)
B
bigmagic 已提交
172
{
B
bigmagic 已提交
173 174
    unsigned int gic_type, i;
    rt_uint32_t cpumask = 1 << 0;
B
bigmagic 已提交
175

B
bigmagic 已提交
176
    RT_ASSERT(index < ARM_GIC_MAX_NR);
B
bigmagic 已提交
177

B
bigmagic 已提交
178 179
    _gic_table[index].dist_hw_base = dist_base;
    _gic_table[index].offset = irq_start;
B
bigmagic 已提交
180

B
bigmagic 已提交
181 182 183
    /* Find out how many interrupts are supported. */
    gic_type = GIC_DIST_TYPE(dist_base);
    _gic_max_irq = ((gic_type & 0x1f) + 1) * 32;
B
bigmagic 已提交
184

B
bigmagic 已提交
185 186 187 188 189 190 191 192 193
    /*
     * The GIC only supports up to 1020 interrupt sources.
     * Limit this to either the architected maximum, or the
     * platform maximum.
     */
    if (_gic_max_irq > 1020)
        _gic_max_irq = 1020;
    if (_gic_max_irq > ARM_GIC_NR_IRQS) /* the platform maximum interrupts */
        _gic_max_irq = ARM_GIC_NR_IRQS;
B
bigmagic 已提交
194

B
bigmagic 已提交
195 196 197
    cpumask |= cpumask << 8;
    cpumask |= cpumask << 16;
    cpumask |= cpumask << 24;
B
bigmagic 已提交
198

B
bigmagic 已提交
199
    GIC_DIST_CTRL(dist_base) = 0x0;
B
bigmagic 已提交
200

B
bigmagic 已提交
201 202 203
    /* Set all global interrupts to be level triggered, active low. */
    for (i = 32; i < _gic_max_irq; i += 16)
        GIC_DIST_CONFIG(dist_base, i) = 0x0;
B
bigmagic 已提交
204

B
bigmagic 已提交
205 206 207
    /* Set all global interrupts to this CPU only. */
    for (i = 32; i < _gic_max_irq; i += 4)
        GIC_DIST_TARGET(dist_base, i) = cpumask;
B
bigmagic 已提交
208

B
bigmagic 已提交
209 210 211
    /* Set priority on all interrupts. */
    for (i = 0; i < _gic_max_irq; i += 4)
        GIC_DIST_PRI(dist_base, i) = 0xa0a0a0a0;
B
bigmagic 已提交
212

B
bigmagic 已提交
213 214 215
    /* Disable all interrupts. */
    for (i = 0; i < _gic_max_irq; i += 32)
        GIC_DIST_ENABLE_CLEAR(dist_base, i) = 0xffffffff;
B
bigmagic 已提交
216

B
bigmagic 已提交
217 218 219 220 221 222 223
#if 0
    /* All interrupts defaults to IGROUP1(IRQ). */
    for (i = 0; i < _gic_max_irq; i += 32)
        GIC_DIST_IGROUP(dist_base, i) = 0xffffffff;
#endif
    for (i = 0; i < _gic_max_irq; i += 32)
        GIC_DIST_IGROUP(dist_base, i) = 0;
B
bigmagic 已提交
224

B
bigmagic 已提交
225 226
    /* Enable group0 and group1 interrupt forwarding. */
    GIC_DIST_CTRL(dist_base) = 0x01;
B
bigmagic 已提交
227

B
bigmagic 已提交
228
    return 0;
B
bigmagic 已提交
229 230
}

B
bigmagic 已提交
231
int arm_gic_cpu_init(rt_uint32_t index, rt_uint32_t cpu_base)
B
bigmagic 已提交
232
{
B
bigmagic 已提交
233
    RT_ASSERT(index < ARM_GIC_MAX_NR);
B
bigmagic 已提交
234

B
bigmagic 已提交
235
    _gic_table[index].cpu_hw_base = cpu_base;
B
bigmagic 已提交
236

B
bigmagic 已提交
237 238 239 240
    GIC_CPU_PRIMASK(cpu_base) = 0xf0;
    GIC_CPU_BINPOINT(cpu_base) = 0x7;
    /* Enable CPU interrupt */
    GIC_CPU_CTRL(cpu_base) = 0x01;
B
bigmagic 已提交
241

B
bigmagic 已提交
242
    return 0;
B
bigmagic 已提交
243 244
}

B
bigmagic 已提交
245
void arm_gic_set_group(rt_uint32_t index, int vector, int group)
B
bigmagic 已提交
246
{
B
bigmagic 已提交
247 248 249 250 251 252 253 254 255 256 257 258 259 260
    /* As for GICv2, there are only group0 and group1. */
    RT_ASSERT(group <= 1);
    RT_ASSERT(vector < _gic_max_irq);

    if (group == 0)
    {
        GIC_DIST_IGROUP(_gic_table[index].dist_hw_base,
                        vector) &= ~(1 << (vector % 32));
    }
    else if (group == 1)
    {
        GIC_DIST_IGROUP(_gic_table[index].dist_hw_base,
                        vector) |=  (1 << (vector % 32));
    }
B
bigmagic 已提交
261 262
}