summaryrefslogtreecommitdiff
path: root/components/drivers/ofw/irq.c
blob: bd88572c74342e8600553856652c9236da2ce89c (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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
/*
 * Copyright (c) 2006-2022, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2022-08-25     GuEe-GUI     first version
 */

#include <rtthread.h>

#include <drivers/pic.h>
#include <drivers/ofw.h>
#include <drivers/ofw_io.h>
#include <drivers/ofw_irq.h>
#include <drivers/platform.h>

#define DBG_TAG "rtdm.ofw"
#define DBG_LVL DBG_INFO
#include <rtdbg.h>

#include "ofw_internal.h"

static int ofw_interrupt_cells(struct rt_ofw_node *np)
{
    int interrupt_cells = -RT_EEMPTY;

    rt_ofw_prop_read_u32(np, "#interrupt-cells", (rt_uint32_t *)&interrupt_cells);

    return interrupt_cells;
}

int rt_ofw_irq_cells(struct rt_ofw_node *np)
{
    return np ? ofw_interrupt_cells(np) : -RT_EINVAL;
}

static rt_err_t ofw_parse_irq_map(struct rt_ofw_node *np, struct rt_ofw_cell_args *irq_args)
{
    rt_err_t err = RT_EOK;
    rt_phandle ic_phandle = 0;
    rt_ssize_t map_len, map_mask_len;
    struct rt_ofw_node *ic_np = RT_NULL;
    const fdt32_t *addr, *map, *map_mask;
    int child_address_cells, child_interrupt_cells;
    int parent_address_cells = 0, parent_interrupt_cells = 0;
    int addr_cells, pin_cells, icaddr_cells, idx1, idx2, limit;

    /*
     * interrupt-map:
     *  An interrupt-map is a property on a nexus node that bridges one
     *  interrupt domain with a set of parent interrupt domains and specifies
     *  how interrupt specifiers in the child domain are mapped to
     *  their respective parent domains.
     *
     *  The interrupt map is a table where each row is a mapping entry
     *  consisting of five components: child unit address, child interrupt
     *  specifier, interrupt-parent, parent unit address, parent interrupt
     *  specifier.
     *
     *  child unit address
     *      The unit address of the child node being mapped. The number of
     *      32-bit cells required to specify this is described by the
     *      #address-cells property of the bus node on which the child is
     *      located.
     *
     *  child interrupt specifier
     *      The interrupt specifier of the child node being mapped. The number
     *      of 32-bit cells required to specify this component is described by
     *      the #interrupt-cells property of this node-the nexus node containing
     *      the interrupt-map property.
     *
     *  interrupt-parent
     *      A single <phandle> value that points to the interrupt parent to
     *      which the child domain is being mapped.
     *
     *  parent unit address
     *      The unit address in the domain of the interrupt parent. The number
     *      of 32-bit cells required to specify this address is described by the
     *      #address-cells property of the node pointed to by the
     *      interrupt-parent field.
     *
     *  parent interrupt specifier
     *      The interrupt specifier in the parent domain. The number of 32-bit
     *      cells required to specify this component is described by the
     *      #interrupt-cells property of the node pointed to by the
     *      interrupt-parent field.
     *
     *  Lookups are performed on the interrupt mapping table by matching a
     *  unit-address/interrupt specifier pair against the child components in
     *  the interrupt-map. Because some fields in the unit interrupt specifier
     *  may not be relevant, a mask is applied before the lookup is done.
     *  Example:
     *
     *      pic: interrupt-controller@0 {
     *          interrupt-controller;
     *          #address-cells = <0>;   // icaddr (parent unit address)
     *          #interrupt-cells = <1>; // icintr (parent interrupt specifier)
     *      };
     *
     *      gic: interrupt-controller@1 {
     *          interrupt-controller;
     *          #address-cells = <2>;   // icaddr (parent unit address)
     *          #interrupt-cells = <3>; // icintr (parent interrupt specifier)
     *      };
     *
     *      pcie {
     *          #address-cells = <3>;   // addr (child unit address)
     *          #interrupt-cells = <1>; // pin (child interrupt specifier)
     *          interrupt-parent = <&gic>;
     *          interrupt-map-mask = <0x1800 0 0 7>;
     *          interrupt-map =
     *              //     addr pin   ic icintr
     *              <0x0000 0 0   1 &pic      1>, // INTA SOLT 0
     *              <0x0000 0 0   2 &pic      2>, // INTB
     *              <0x0000 0 0   3 &pic      3>, // INTC
     *              <0x0000 0 0   4 &pic      4>, // INTD
     *              <0x0800 0 0   1 &pic      2>, // INTA SOLT 1
     *              <0x0800 0 0   2 &pic      3>, // INTB
     *              <0x0800 0 0   3 &pic      4>, // INTC
     *              <0x0800 0 0   4 &pic      1>, // INTD
     *              //     addr pin   ic icaddr                        icintr
     *              <0x1000 0 0   1 &gic    0 0 GIC_SPI 3 IRQ_TYPE_LEVEL_HIGH>, // INTA SOLT 2
     *              <0x1000 0 0   2 &gic    0 0 GIC_SPI 4 IRQ_TYPE_LEVEL_HIGH>, // INTB
     *              <0x1000 0 0   3 &gic    0 0 GIC_SPI 1 IRQ_TYPE_LEVEL_HIGH>, // INTC
     *              <0x1000 0 0   4 &gic    0 0 GIC_SPI 2 IRQ_TYPE_LEVEL_HIGH>, // INTD
     *              <0x1800 0 0   1 &gic    0 0 GIC_SPI 4 IRQ_TYPE_LEVEL_HIGH>, // INTA SOLT 3
     *              <0x1800 0 0   2 &gic    0 0 GIC_SPI 1 IRQ_TYPE_LEVEL_HIGH>, // INTB
     *              <0x1800 0 0   3 &gic    0 0 GIC_SPI 2 IRQ_TYPE_LEVEL_HIGH>, // INTC
     *              <0x1800 0 0   4 &gic    0 0 GIC_SPI 3 IRQ_TYPE_LEVEL_HIGH>; // INTD
     *      };
     *
     * In fact, almost no SoC will be use multi IC to implement INTx.
     * before call ofw_parse_irq_map(np, &args):
     *
     *      args.data = addr;
     *      args.args_count = 2 or 3;
     *      args.args[0] = (addr cells);
     *      args.args[1] = (pin cells);
     *      args.args[2] = (icaddr cells);
     *
     * if call with `pcie` in ofw_parse_irq_map(np, &args):
     *
     *      np = &pcie;
     *      args.data = addr = fdt32_t({ (bus << 16) | (device << 11) | (function << 8), 0, 0, pin });
     *      args.args_count = 2;
     *      args.args[0] = 3;
     *      args.args[1] = 1;
     *
     * To perform a lookup of the gic interrupt source number for INTB for IDSEL
     * 0x12 (slot 2), function 0x3, the following steps would be performed:
     *
     *  1.The user addr is value <0x9300 0 0 2>.
     *
     *  2.The encoding of the address includes the bus number (0x0 << 16),
     *    device number (0x12 << 11), and function number (0x3 << 8).
     *
     *  3.The interrupt specifier is 2, which is the encoding for INTB as per
     *    the PCI binding.
     *
     *  4.The interrupt-map-mask value <0x1800 0 0 7> is applied, giving a
     *    result of <0x1000 0 0 2>.
     *
     *  5.That result is looked up in the interrupt-map table, which maps to the
     *    parent interrupt specifier <GIC_SPI 4 IRQ_TYPE_LEVEL_HIGH>.
     */

    do {
        err = -RT_EEMPTY;

        if ((child_address_cells = rt_ofw_bus_addr_cells(np)) < 0)
        {
            LOG_D("%s property %s is undefined", np->full_name, "#address-cells");

            break;
        }

        if ((child_interrupt_cells = ofw_interrupt_cells(np)) < 0)
        {
            LOG_D("%s property %s is undefined", np->full_name, "#interrupt-cells");

            break;
        }

        if (!(map = rt_ofw_prop_read_raw(np, "interrupt-map", &map_len)))
        {
            LOG_D("%s property %s is undefined", np->full_name, "interrupt-map");

            break;
        }

        if (!(map_mask = rt_ofw_prop_read_raw(np, "interrupt-map-mask", &map_mask_len)))
        {
            LOG_D("%s property %s is undefined", np->full_name, "interrupt-map-mask");

            break;
        }

        map_len /= sizeof(fdt32_t);
        map_mask_len /= sizeof(fdt32_t);

        err = -RT_EINVAL;

        addr = irq_args->data;
        addr_cells = irq_args->args[0];
        pin_cells = irq_args->args[1];
        icaddr_cells = irq_args->args_count == 3 ? irq_args->args[2] : 0;

        if (addr_cells > child_address_cells)
        {
            LOG_D("%s(%d) > %s(%d)", "addr_cells", addr_cells, "child_address_cells", child_address_cells);

            break;
        }

        if (pin_cells > child_interrupt_cells)
        {
            LOG_D("%s(%d) > %s(%d)", "pin_cells", pin_cells, "child_interrupt_cells", child_interrupt_cells);

            break;
        }

        err = -RT_ENOENT;

#define _map_walk_range(_idx, _idx2, _count, ...) \
        for (idx1 = _idx, idx2 = _idx2, limit = idx1 + _count; idx1 < limit __VA_ARGS__; ++idx1, ++idx2)

        _map_walk_range(0, 0, addr_cells)
        {
            /* Applied addr mask */
            ((fdt32_t *)addr)[idx1] &= map_mask[idx2];
        }

        _map_walk_range(addr_cells, child_address_cells, pin_cells)
        {
            /* Applied pin mask */
            ((fdt32_t *)addr)[idx1] &= map_mask[idx2];
        }

        while (map_len > 0)
        {
            rt_bool_t match = RT_TRUE;

            _map_walk_range(0, 0, addr_cells)
            {
                /* Applied mask */
                if (addr[idx1] != map[idx2])
                {
                    match = RT_FALSE;
                    break;
                }
            }

            _map_walk_range(addr_cells, child_address_cells, pin_cells, && match)
            {
                /* Applied mask */
                if (addr[idx1] != map[idx2])
                {
                    match = RT_FALSE;
                    break;
                }
            }

            /* Skip addr, pin */
            map += map_mask_len;

            /* IC is different? */
            if (ic_phandle != fdt32_to_cpu(*map))
            {
                rt_ofw_node_put(ic_np);

                ic_phandle = fdt32_to_cpu(*map);
                ic_np = rt_ofw_find_node_by_phandle(ic_phandle);

                if (!ic_np)
                {
                    LOG_D("%s irq parent phandle = %d is not found", np->full_name, ic_phandle);

                    break;
                }

                if ((parent_address_cells = rt_ofw_bus_addr_cells(ic_np)) < 0)
                {
                    LOG_D("%s property %s is undefined", ic_np->full_name, "#address-cells");

                    break;
                }

                if (icaddr_cells > parent_address_cells)
                {
                    LOG_D("%s(%d) > %s(%d)", "icaddr_cells", icaddr_cells, "parent_address_cells", parent_address_cells);

                    break;
                }

                if ((parent_interrupt_cells = ofw_interrupt_cells(ic_np)) < 0)
                {
                    LOG_D("%s property %s is undefined", ic_np->full_name, "#interrupt-cells");

                    break;
                }

                RT_ASSERT(parent_interrupt_cells <= RT_OFW_MAX_CELL_ARGS);
            }

            /* Skip ic phandle */
            ++map;

            _map_walk_range(addr_cells + pin_cells, 0, icaddr_cells, && match)
            {
                /* Applied ic_addr mask */
                if (addr[idx1] != map[idx2])
                {
                    match = RT_FALSE;
                    break;
                }
            }

            /* Skip icaddr */
            map += parent_address_cells;

            if (match)
            {
                irq_args->data = ic_np;
                irq_args->args_count = parent_interrupt_cells;

                for (int i = 0; i < irq_args->args_count; ++i)
                {
                    irq_args->args[i] = fdt32_to_cpu(*map++);
                }

                err = RT_EOK;

                break;
            }

            /* Skip icintr */
            map += parent_interrupt_cells;

            map_len -= map_mask_len + 1 + parent_address_cells + parent_interrupt_cells;
        }

#undef _map_walk_range
    } while (0);

    return err;
}

rt_err_t rt_ofw_parse_irq_map(struct rt_ofw_node *np, struct rt_ofw_cell_args *irq_args)
{
    rt_err_t err;

    if (np && irq_args && irq_args->data)
    {
        err = ofw_parse_irq_map(np, irq_args);
    }
    else
    {
        err = -RT_EINVAL;
    }

    return err;
}

static rt_err_t ofw_parse_irq_cells(struct rt_ofw_node *np, int index, struct rt_ofw_cell_args *out_irq_args)
{
    rt_err_t err;

    /*
     * interrupts-extended:
     *
     *  The interrupts-extended property lists the interrupt(s) generated by a
     *  device. interrupts-extended should be used instead of interrupts when a
     *  device is connected to multiple interrupt controllers as it encodes a
     *  parent phandle with each interrupt specifier. Example:
     *
     *      pic: interrupt-controller@0 {
     *          interrupt-controller;
     *          #interrupt-cells = <1>;
     *      };
     *
     *      gic: interrupt-controller@1 {
     *          interrupt-controller;
     *          #interrupt-cells = <3>;
     *      };
     *
     *      node: node {
     *          interrupts-extended = <&pic 9>, <&gic GIC_SPI 1 IRQ_TYPE_LEVEL_HIGH>;
     *      };
     *
     *  call `rt_ofw_parse_phandle_cells` to get irq info;
     */

    err = rt_ofw_parse_phandle_cells(np, "interrupts-extended", "#interrupt-cells", index, out_irq_args);

    do {
        int interrupt_cells;
        const fdt32_t *cell;
        rt_ssize_t interrupt_len;
        struct rt_ofw_node *ic_np;

        if (!err)
        {
            break;
        }

        /*
         * interrupts (old style):
         *
         *  The interrupts property of a device node defines the interrupt or
         *  interrupts that are generated by the device. The value of the
         *  interrupts property consists of an arbitrary number of interrupt
         *  specifiers. The format of an interrupt specifier is defined by the
         *  binding of the interrupt domain root.
         *  interrupts is overridden by the interrupts-extended property and
         *  normally only one or the other should be used. Example:
         *
         *      pic: interrupt-controller@0 {
         *          interrupt-controller;
         *          #interrupt-cells = <1>;
         *      };
         *
         *      gic: interrupt-controller@1 {
         *          interrupt-controller;
         *          #interrupt-cells = <3>;
         *      };
         *
         *      node0: node0 {
         *          interrupt-parent = <&pic>;
         *          interrupts = <9>;
         *      };
         *
         *      node1: node1 {
         *          interrupt-parent = <&gic>;
         *          interrupts = <GIC_SPI 1 IRQ_TYPE_LEVEL_HIGH>;
         *      };
         */

        cell = rt_ofw_prop_read_raw(np, "interrupts", &interrupt_len);

        if (!cell)
        {
            err = -RT_ERROR;
            break;
        }

        ic_np = rt_ofw_find_irq_parent(np, &interrupt_cells);

        if (!ic_np)
        {
            err = -RT_ERROR;
            break;
        }

        RT_ASSERT(interrupt_cells <= RT_OFW_MAX_CELL_ARGS);

        if (index >= interrupt_len / (interrupt_cells * sizeof(*cell)))
        {
            err = -RT_EINVAL;
            break;
        }

        cell += index * interrupt_cells;

        out_irq_args->data = ic_np;
        out_irq_args->args_count = interrupt_cells;

        for (int idx = 0; idx < interrupt_cells; ++idx, ++cell)
        {
            out_irq_args->args[idx] = fdt32_to_cpu(*cell);
        }

        err = RT_EOK;
    } while (0);

    return err;
}

rt_err_t rt_ofw_parse_irq_cells(struct rt_ofw_node *np, int index, struct rt_ofw_cell_args *out_irq_args)
{
    rt_err_t err;

    if (np && index >= 0 && out_irq_args)
    {
        err = ofw_parse_irq_cells(np, index, out_irq_args);
    }
    else
    {
        err = -RT_EINVAL;
    }

    return err;
}

struct rt_ofw_node *rt_ofw_find_irq_parent(struct rt_ofw_node *np, int *out_interrupt_cells)
{
    for (np = rt_ofw_node_get(np); np; np = rt_ofw_get_next_parent(np))
    {
        rt_phandle ic_phandle;

        if (!rt_ofw_prop_read_u32(np, "interrupt-parent", (rt_uint32_t *)&ic_phandle))
        {
            int interrupt_cells;
            struct rt_ofw_node *ic_np = rt_ofw_find_node_by_phandle(ic_phandle);

            if (ic_np && (interrupt_cells = ofw_interrupt_cells(ic_np)) >= 0)
            {
                np = ic_np;

                if (out_interrupt_cells)
                {
                    *out_interrupt_cells = interrupt_cells;
                }

                break;
            }

            rt_ofw_node_put(ic_np);
        }
    }

    return np;
}

static int ofw_map_irq(struct rt_ofw_cell_args *irq_args)
{
    int irq;
    struct rt_pic *pic;
    struct rt_ofw_node *ic_np = irq_args->data;

    if (!rt_ofw_data(ic_np))
    {
        rt_platform_ofw_request(ic_np);
    }

    pic = rt_pic_dynamic_cast(rt_ofw_data(ic_np));

    /* args.data is "interrupt-controller" */
    if (pic)
    {
        struct rt_pic_irq pirq;

        if (!pic->ops->irq_parse)
        {
            LOG_E("Master pic MUST implemented irq_parse");
            RT_ASSERT(0);
        }

        if (!pic->ops->irq_map)
        {
            LOG_E("Master pic MUST implemented irq_map");
            RT_ASSERT(0);
        }

        irq = pic->ops->irq_parse(pic, irq_args, &pirq);

        if (!irq)
        {
            irq = pic->ops->irq_map(pic, pirq.hwirq, pirq.mode);
        }
    }
    else
    {
        LOG_E("Master pic %s not support", ic_np->full_name);
        irq = -RT_EIO;
    }

    rt_ofw_node_put(ic_np);

    return irq;
}

int rt_ofw_map_irq(struct rt_ofw_cell_args *irq_args)
{
    int irq;

    if (irq_args && irq_args->data && irq_args->args_count > 0)
    {
        irq = ofw_map_irq(irq_args);
    }
    else
    {
        irq = -RT_EINVAL;
    }

    return irq;
}

int rt_ofw_get_irq_count(struct rt_ofw_node *np)
{
    int count;

    if (np)
    {
        struct rt_ofw_cell_args irq_args;

        count = 0;

        while (!ofw_parse_irq_cells(np, count, &irq_args))
        {
            ++count;
        }
    }
    else
    {
        count = -RT_EINVAL;
    }

    return count;
}

int rt_ofw_get_irq(struct rt_ofw_node *np, int index)
{
    int irq;

    if (np && index >= 0)
    {
        struct rt_ofw_cell_args irq_args;

        irq = ofw_parse_irq_cells(np, index, &irq_args);

        if (irq >= 0)
        {
            rt_phandle cpu_phandle;

            irq = ofw_map_irq(&irq_args);

            if (irq >= 0 && !rt_ofw_prop_read_u32_index(np, "interrupt-affinity", index, &cpu_phandle))
            {
                rt_uint64_t cpuid = rt_ofw_get_cpu_id(rt_ofw_find_node_by_phandle(cpu_phandle));

                if ((rt_int64_t)cpuid >= 0)
                {
                    RT_BITMAP_DECLARE(affinity, RT_CPUS_NR) = { 0 };

                    rt_bitmap_set_bit(affinity, cpuid);

                    if (rt_pic_irq_set_affinity(irq, affinity) == -RT_ENOSYS)
                    {
                        LOG_W("%s irq affinity init fail", np->full_name);
                    }
                }
            }
        }
    }
    else
    {
        irq = -RT_EINVAL;
    }

    return irq;
}

int rt_ofw_get_irq_by_name(struct rt_ofw_node *np, const char *name)
{
    int irq;

    if (np && name)
    {
        int index = rt_ofw_prop_index_of_string(np, "interrupt-names", name);

        if (index >= 0)
        {
            irq = rt_ofw_get_irq(np, index);
        }
        else
        {
            irq = -1;
        }
    }
    else
    {
        irq = -RT_EINVAL;
    }

    return irq;
}