summaryrefslogtreecommitdiff
path: root/components/drivers/pic/pic-gicv2m.c
blob: ae6013739b57777242260b7b0750ada62289892f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/*
 * Copyright (c) 2006-2022, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2022-11-07     GuEe-GUI     first version
 */

#include <rthw.h>
#include <rtthread.h>
#include <rtdevice.h>

#define DBG_TAG "pic.gicv2m"
#define DBG_LVL DBG_INFO
#include <rtdbg.h>

#include <mmu.h>
#include <cpuport.h>
#include "pic-gic-common.h"

/*
* MSI_TYPER:
*   [31:26] Reserved
*   [25:16] lowest SPI assigned to MSI
*   [15:10] Reserved
*   [9:0]   Numer of SPIs assigned to MSI
*/
#define V2M_MSI_TYPER                   0x008
#define V2M_MSI_TYPER_BASE_SHIFT        16
#define V2M_MSI_TYPER_BASE_MASK         0x3ff
#define V2M_MSI_TYPER_NUM_MASK          0x3ff
#define V2M_MSI_SETSPI_NS               0x040
#define V2M_MIN_SPI                     32
#define V2M_MAX_SPI                     1019
#define V2M_MSI_IIDR                    0xfcc

#define V2M_MSI_TYPER_BASE_SPI(x)       (((x) >> V2M_MSI_TYPER_BASE_SHIFT) & V2M_MSI_TYPER_BASE_MASK)
#define V2M_MSI_TYPER_NUM_SPI(x)        ((x) & V2M_MSI_TYPER_NUM_MASK)

/* APM X-Gene with GICv2m MSI_IIDR register value */
#define XGENE_GICV2M_MSI_IIDR           0x06000170
/* Broadcom NS2 GICv2m MSI_IIDR register value */
#define BCM_NS2_GICV2M_MSI_IIDR         0x0000013f

/* List of flags for specific v2m implementation */
#define GICV2M_NEEDS_SPI_OFFSET         0x00000001
#define GICV2M_GRAVITON_ADDRESS_ONLY    0x00000002

struct gicv2m
{
    struct rt_pic parent;

    void *base;
    void *base_phy;
    rt_uint32_t spi_start;  /* The SPI number that MSIs start */
    rt_uint32_t spis_nr;    /* The number of SPIs for MSIs */
    rt_uint32_t spi_offset; /* Offset to be subtracted from SPI number */

    rt_bitmap_t *vectors;   /* MSI vector bitmap */
    rt_uint32_t flags;      /* Flags for v2m's specific implementation */

    void *gic;
    struct rt_spinlock lock;
};

#define raw_to_gicv2m(raw) rt_container_of(raw, struct gicv2m, parent)

static rt_ubase_t gicv2m_get_msi_addr(struct gicv2m *v2m, int hwirq)
{
    rt_ubase_t addr;

    if (v2m->flags & GICV2M_GRAVITON_ADDRESS_ONLY)
    {
        addr = (rt_ubase_t)v2m->base_phy | ((hwirq - 32) << 3);
    }
    else
    {
        addr = (rt_ubase_t)v2m->base_phy + V2M_MSI_SETSPI_NS;
    }

    return addr;
}

static rt_bool_t is_msi_spi_valid(rt_uint32_t base, rt_uint32_t num)
{
    if (base < V2M_MIN_SPI)
    {
        LOG_E("Invalid MSI base SPI (base: %u)", base);

        return RT_FALSE;
    }
    else if ((num == 0) || (base + num > V2M_MAX_SPI))
    {
        LOG_E("Number of SPIs (%u) exceed maximum (%u)", num, V2M_MAX_SPI - V2M_MIN_SPI + 1);

        return RT_FALSE;
    }

    return RT_TRUE;
}

static void gicv2m_irq_mask(struct rt_pic_irq *pirq)
{
    rt_pci_msi_mask_irq(pirq);
    rt_pic_irq_parent_mask(pirq);
}

static void gicv2m_irq_unmask(struct rt_pic_irq *pirq)
{
    rt_pci_msi_unmask_irq(pirq);
    rt_pic_irq_parent_unmask(pirq);
}

static void gicv2m_compose_msi_msg(struct rt_pic_irq *pirq, struct rt_pci_msi_msg *msg)
{
    rt_ubase_t addr;
    struct gicv2m *v2m = raw_to_gicv2m(pirq->pic);

    addr = gicv2m_get_msi_addr(v2m, pirq->hwirq);

    msg->address_hi = rt_upper_32_bits(addr);
    msg->address_lo = rt_lower_32_bits(addr);

    if (v2m->flags & GICV2M_GRAVITON_ADDRESS_ONLY)
    {
        msg->data = 0;
    }
    else
    {
        msg->data = pirq->hwirq;
    }

    if (v2m->flags & GICV2M_NEEDS_SPI_OFFSET)
    {
        msg->data -= v2m->spi_offset;
    }
}

static int gicv2m_irq_alloc_msi(struct rt_pic *pic, struct rt_pci_msi_desc *msi_desc)
{
    rt_ubase_t level;
    int irq, parent_irq, hwirq, hwirq_index;
    struct rt_pic_irq *pirq;
    struct gicv2m *v2m = raw_to_gicv2m(pic);
    struct rt_pic *ppic = v2m->gic;

    level = rt_spin_lock_irqsave(&v2m->lock);
    hwirq_index = rt_bitmap_next_clear_bit(v2m->vectors, 0, v2m->spis_nr);

    if (hwirq_index >= v2m->spis_nr)
    {
        irq = -RT_EEMPTY;
        goto _out_lock;
    }

    hwirq = v2m->spi_start + v2m->spi_offset + hwirq_index;

    parent_irq = ppic->ops->irq_map(ppic, hwirq, RT_IRQ_MODE_EDGE_RISING);
    if (parent_irq < 0)
    {
        irq = parent_irq;
        goto _out_lock;
    }

    irq = rt_pic_config_irq(pic, hwirq_index, hwirq);
    if (irq < 0)
    {
        goto _out_lock;
    }
    pirq = rt_pic_find_irq(pic, hwirq_index);

    pirq->mode = RT_IRQ_MODE_EDGE_RISING;
    rt_pic_cascade(pirq, parent_irq);

    rt_bitmap_set_bit(v2m->vectors, hwirq_index);

_out_lock:
    rt_spin_unlock_irqrestore(&v2m->lock, level);

    return irq;
}

static void gicv2m_irq_free_msi(struct rt_pic *pic, int irq)
{
    rt_ubase_t level;
    struct rt_pic_irq *pirq;
    struct gicv2m *v2m = raw_to_gicv2m(pic);

    pirq = rt_pic_find_pirq(pic, irq);

    if (!pirq)
    {
        return;
    }

    rt_pic_uncascade(pirq);

    level = rt_spin_lock_irqsave(&v2m->lock);
    rt_bitmap_clear_bit(v2m->vectors, pirq->hwirq - (v2m->spi_start + v2m->spi_offset));
    rt_spin_unlock_irqrestore(&v2m->lock, level);
}

static rt_err_t gicv2m_irq_set_state(struct rt_pic *pic, int hwirq, int type, rt_bool_t state)
{
    struct gicv2m *v2m = raw_to_gicv2m(pic);
    struct rt_pic *ppic = v2m->gic;

    return ppic->ops->irq_set_state(ppic, hwirq, type, state);
}

static rt_err_t gicv2m_irq_get_state(struct rt_pic *pic, int hwirq, int type, rt_bool_t *out_state)
{
    struct gicv2m *v2m = raw_to_gicv2m(pic);
    struct rt_pic *ppic = v2m->gic;

    return ppic->ops->irq_get_state(ppic, hwirq, type, out_state);
}

const static struct rt_pic_ops gicv2m_ops =
{
    .name = "GICv2m",
    .irq_ack = rt_pic_irq_parent_ack,
    .irq_mask = gicv2m_irq_mask,
    .irq_unmask = gicv2m_irq_unmask,
    .irq_eoi = rt_pic_irq_parent_eoi,
    .irq_set_priority = rt_pic_irq_parent_set_priority,
    .irq_set_affinity = rt_pic_irq_parent_set_affinity,
    .irq_compose_msi_msg = gicv2m_compose_msi_msg,
    .irq_alloc_msi = gicv2m_irq_alloc_msi,
    .irq_free_msi = gicv2m_irq_free_msi,
    .irq_set_state = gicv2m_irq_set_state,
    .irq_get_state = gicv2m_irq_get_state,
    .flags = RT_PIC_F_IRQ_ROUTING,
};

static const struct rt_ofw_node_id gicv2m_ofw_match[] =
{
    { .compatible = "arm,gic-v2m-frame" },
    { /* sentinel */ }
};

rt_err_t gicv2m_ofw_probe(struct rt_ofw_node *np, const struct rt_ofw_node_id *id)
{
    rt_err_t err = RT_EOK;
    struct rt_ofw_node *v2m_np;

    rt_ofw_foreach_available_child_node(np, v2m_np)
    {
        struct gicv2m *v2m;
        rt_size_t bitmap_size;
        rt_uint32_t spi_start = 0, spis_nr = 0;

        if (!rt_ofw_node_match(v2m_np, gicv2m_ofw_match))
        {
            continue;
        }

        if (!rt_ofw_prop_read_bool(v2m_np, "msi-controller"))
        {
            continue;
        }

        if (!(v2m = rt_malloc(sizeof(*v2m))))
        {
            rt_ofw_node_put(v2m_np);

            err = -RT_ENOMEM;
            break;
        }

        v2m->base = rt_ofw_iomap(v2m_np, 0);

        if (!v2m->base)
        {
            LOG_E("%s: IO map failed", rt_ofw_node_full_name(v2m_np));
            continue;
        }

        v2m->base_phy = rt_kmem_v2p(v2m->base);
        v2m->flags = 0;

        if (!rt_ofw_prop_read_u32(v2m_np, "arm,msi-base-spi", &spi_start) &&
            !rt_ofw_prop_read_u32(v2m_np, "arm,msi-num-spis", &spis_nr))
        {
            LOG_I("DT overriding V2M MSI_TYPER (base:%u, num:%u)", spi_start, spis_nr);
        }

        if (spi_start && spis_nr)
        {
            v2m->spi_start = spi_start;
            v2m->spis_nr = spis_nr;
        }
        else
        {
            rt_uint32_t typer;

            /* Graviton should always have explicit spi_start/spis_nr */
            if (v2m->flags & GICV2M_GRAVITON_ADDRESS_ONLY)
            {
                goto _fail;
            }
            typer = HWREG32(v2m->base + V2M_MSI_TYPER);

            v2m->spi_start = V2M_MSI_TYPER_BASE_SPI(typer);
            v2m->spis_nr = V2M_MSI_TYPER_NUM_SPI(typer);
        }

        if (!is_msi_spi_valid(v2m->spi_start, v2m->spis_nr))
        {
            goto _fail;
        }

        /*
         * APM X-Gene GICv2m implementation has an erratum where
         * the MSI data needs to be the offset from the spi_start
         * in order to trigger the correct MSI interrupt. This is
         * different from the standard GICv2m implementation where
         * the MSI data is the absolute value within the range from
         * spi_start to (spi_start + num_spis).
         *
         * Broadcom NS2 GICv2m implementation has an erratum where the MSI data
         * is 'spi_number - 32'
         *
         * Reading that register fails on the Graviton implementation
         */
        if (!(v2m->flags & GICV2M_GRAVITON_ADDRESS_ONLY))
        {
            switch (HWREG32(v2m->base + V2M_MSI_IIDR))
            {
            case XGENE_GICV2M_MSI_IIDR:
                v2m->flags |= GICV2M_NEEDS_SPI_OFFSET;
                v2m->spi_offset = v2m->spi_start;
                break;

            case BCM_NS2_GICV2M_MSI_IIDR:
                v2m->flags |= GICV2M_NEEDS_SPI_OFFSET;
                v2m->spi_offset = 32;
                break;
            }
        }

        bitmap_size = RT_BITMAP_LEN(v2m->spis_nr) * sizeof(rt_bitmap_t);

        if (!(v2m->vectors = rt_calloc(1, bitmap_size)))
        {
            err = -RT_ENOMEM;
            goto _fail;
        }

        rt_spin_lock_init(&v2m->lock);

        v2m->parent.priv_data = v2m;
        v2m->parent.ops = &gicv2m_ops;
        v2m->gic = rt_ofw_data(np);

        rt_pic_linear_irq(&v2m->parent, v2m->spis_nr);
        rt_pic_user_extends(&v2m->parent);

        rt_ofw_data(v2m_np) = &v2m->parent;
        rt_ofw_node_set_flag(v2m_np, RT_OFW_F_READLY);

        continue;

    _fail:
        rt_iounmap(v2m->base);
        rt_free(v2m);

        if (err)
        {
            rt_ofw_node_put(v2m_np);
            break;
        }
    }

    return err;
}