lib: utils: Add fdt_parse_clint_node() function
authorAnup Patel <anup.patel@wdc.com>
Wed, 13 May 2020 04:25:28 +0000 (09:55 +0530)
committerAnup Patel <anup@brainfault.org>
Sat, 23 May 2020 05:06:46 +0000 (10:36 +0530)
We add fdt_parse_clint_node() function which will be used by
fdt_ipi_clint and fdt_timer_clint drivers to parse CLINT details
from DT node.

Signed-off-by: Anup Patel <anup.patel@wdc.com>
Reviewed-by: Atish Patra <atish.patra@wdc.com>
include/sbi_utils/fdt/fdt_helper.h
lib/utils/fdt/fdt_helper.c
lib/utils/ipi/fdt_ipi_clint.c
lib/utils/timer/fdt_timer_clint.c

index 72fb167..1b780fb 100644 (file)
@@ -54,6 +54,11 @@ int fdt_parse_plic_node(void *fdt, int nodeoffset, struct plic_data *plic);
 
 int fdt_parse_plic(void *fdt, struct plic_data *plic, const char *compat);
 
+struct clint_data;
+
+int fdt_parse_clint_node(void *fdt, int nodeoffset, bool for_timer,
+                        struct clint_data *clint);
+
 int fdt_parse_compat_addr(void *fdt, unsigned long *addr,
                          const char *compatible);
 
index 58f8951..887d6ed 100644 (file)
@@ -9,10 +9,12 @@
 #include <libfdt.h>
 #include <sbi/riscv_asm.h>
 #include <sbi/sbi_console.h>
+#include <sbi/sbi_hartmask.h>
 #include <sbi/sbi_platform.h>
 #include <sbi/sbi_scratch.h>
 #include <sbi_utils/fdt/fdt_helper.h>
 #include <sbi_utils/irqchip/plic.h>
+#include <sbi_utils/sys/clint.h>
 
 #define DEFAULT_UART_FREQ              0
 #define DEFAULT_UART_BAUD              115200
@@ -296,6 +298,73 @@ int fdt_parse_plic(void *fdt, struct plic_data *plic, const char *compat)
        return fdt_parse_plic_node(fdt, nodeoffset, plic);
 }
 
+int fdt_parse_clint_node(void *fdt, int nodeoffset, bool for_timer,
+                        struct clint_data *clint)
+{
+       const fdt32_t *val;
+       unsigned long reg_addr, reg_size;
+       int i, rc, count, cpu_offset, cpu_intc_offset;
+       u32 phandle, hwirq, hartid, first_hartid, last_hartid;
+       u32 match_hwirq = (for_timer) ? IRQ_M_TIMER : IRQ_M_SOFT;
+
+       if (nodeoffset < 0 || !clint || !fdt)
+               return SBI_ENODEV;
+
+       rc = fdt_get_node_addr_size(fdt, nodeoffset, &reg_addr, &reg_size);
+       if (rc < 0 || !reg_addr || !reg_size)
+               return SBI_ENODEV;
+       clint->addr = reg_addr;
+
+       val = fdt_getprop(fdt, nodeoffset, "interrupts-extended", &count);
+       if (!val || count < sizeof(fdt32_t))
+               return SBI_EINVAL;
+       count = count / sizeof(fdt32_t);
+
+       first_hartid = -1U;
+       last_hartid = 0;
+       clint->hart_count = 0;
+       for (i = 0; i < count; i += 2) {
+               phandle = fdt32_to_cpu(val[i]);
+               hwirq = fdt32_to_cpu(val[i + 1]);
+
+               cpu_intc_offset = fdt_node_offset_by_phandle(fdt, phandle);
+               if (cpu_intc_offset < 0)
+                       continue;
+
+               cpu_offset = fdt_parent_offset(fdt, cpu_intc_offset);
+               if (cpu_intc_offset < 0)
+                       continue;
+
+               rc = fdt_parse_hart_id(fdt, cpu_offset, &hartid);
+               if (rc)
+                       continue;
+
+               if (SBI_HARTMASK_MAX_BITS <= hartid)
+                       continue;
+
+               if (match_hwirq == hwirq) {
+                       if (hartid < first_hartid)
+                               first_hartid = hartid;
+                       if (hartid > last_hartid)
+                               last_hartid = hartid;
+                       clint->hart_count++;
+               }
+       }
+
+       if ((last_hartid < first_hartid) || first_hartid == -1U)
+               return SBI_ENODEV;
+
+       clint->first_hartid = first_hartid;
+       count = last_hartid - first_hartid + 1;
+       if (clint->hart_count < count)
+               clint->hart_count = count;
+
+       /* TODO: We should figure-out CLINT has_64bit_mmio from DT node */
+       clint->has_64bit_mmio = TRUE;
+
+       return 0;
+}
+
 int fdt_parse_compat_addr(void *fdt, unsigned long *addr,
                          const char *compatible)
 {
index c4ac0df..eead8b3 100644 (file)
@@ -17,23 +17,11 @@ static int ipi_clint_cold_init(void *fdt, int nodeoff,
                               const struct fdt_match *match)
 {
        int rc;
-       u32 max_hartid;
-       unsigned long addr;
 
-       rc = fdt_parse_max_hart_id(fdt, &max_hartid);
+       rc = fdt_parse_clint_node(fdt, nodeoff, FALSE, &clint_ipi);
        if (rc)
                return rc;
 
-       rc = fdt_get_node_addr_size(fdt, nodeoff, &addr, NULL);
-       if (rc)
-               return rc;
-
-       /* TODO: We should figure-out CLINT has_64bit_mmio from DT node */
-       clint_ipi.addr = addr;
-       clint_ipi.first_hartid = 0;
-       clint_ipi.hart_count = max_hartid + 1;
-       clint_ipi.has_64bit_mmio = TRUE;
-
        return clint_cold_ipi_init(&clint_ipi);
 }
 
index 2f5f283..85779b7 100644 (file)
@@ -17,23 +17,11 @@ static int timer_clint_cold_init(void *fdt, int nodeoff,
                                  const struct fdt_match *match)
 {
        int rc;
-       u32 max_hartid;
-       unsigned long addr;
 
-       rc = fdt_parse_max_hart_id(fdt, &max_hartid);
+       rc = fdt_parse_clint_node(fdt, nodeoff, TRUE, &clint_timer);
        if (rc)
                return rc;
 
-       rc = fdt_get_node_addr_size(fdt, nodeoff, &addr, NULL);
-       if (rc)
-               return rc;
-
-       /* TODO: We should figure-out CLINT has_64bit_mmio from DT node */
-       clint_timer.addr = addr;
-       clint_timer.first_hartid = 0;
-       clint_timer.hart_count = max_hartid + 1;
-       clint_timer.has_64bit_mmio = TRUE;
-
        return clint_cold_timer_init(&clint_timer, NULL);
 }