1 /*
2  * Copyright (c) 2006-2023, RT-Thread Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Email: opensource_embedded@phytium.com.cn
7  *
8  * Change Logs:
9  * Date        Author       Notes
10  * 2022-10-26  huanghe      first commit
11  *
12  */
13 
14 #ifndef __PHYTIUM_CPU_H__
15 #define __PHYTIUM_CPU_H__
16 
17 #include <rthw.h>
18 #include <rtthread.h>
19 #include <gicv3.h>
20 #include "fparameters.h"
21 #include "fio.h"
22 #include "faarch.h"
23 #include "tlb.h"
24 
25 #ifdef RT_USING_SMART
26 #include"ioremap.h"
27 #endif
28 
29 #define ARM_GIC_MAX_NR 1
30 
31 #if defined(TARGET_FT2000_4) || defined(TARGET_D2000)
32     #define MAX_HANDLERS 160
33 #endif
34 
35 #if defined(TARGET_PE220X)
36     #define MAX_HANDLERS 270
37 #endif
38 
39 #if defined(TARGET_PD2408)
40     #define MAX_HANDLERS 1024
41 #endif
42 
43 #define GIC_IRQ_START 0
44 #define GIC_ACK_INTID_MASK 0x000003ff
45 #define RT_CORE_AFF(x) (CORE##x##_AFF | 0x80000000)
46 
47 rt_uint64_t get_main_cpu_affval(void);
48 
platform_get_gic_dist_base(void)49 rt_inline rt_uint32_t platform_get_gic_dist_base(void)
50 {
51     return GICV3_DISTRIBUTOR_BASE_ADDR;
52 }
53 
54 /* the basic constants and interfaces needed by gic */
platform_get_gic_redist_base(void)55 rt_inline uintptr_t platform_get_gic_redist_base(void)
56 {
57     uintptr_t redis_base, mpidr_aff, gicr_typer_aff;
58     mpidr_aff = (uintptr_t)(GetAffinity() & CORE_AFF_MASK);
59 
60     for (redis_base = GICV3_RD_BASE_ADDR; redis_base < GICV3_RD_BASE_ADDR + GICV3_RD_SIZE; redis_base += GICV3_RD_OFFSET)
61     {
62 #ifdef RT_USING_SMART
63         uintptr_t redis_base_virtual = (uintptr_t)rt_ioremap((void *)redis_base, GICV3_RD_OFFSET);
64         rt_hw_tlb_invalidate_all();
65         if (redis_base_virtual == 0)
66         {
67             continue;
68         }
69 #if defined(TARGET_ARMV8_AARCH64)
70         gicr_typer_aff = GIC_RDIST_TYPER(redis_base_virtual) >> 32;
71 #else
72         gicr_typer_aff = GIC_RDIST_TYPER(redis_base_virtual + 0x4);
73 #endif
74         if (mpidr_aff == gicr_typer_aff)
75         {
76             return redis_base_virtual;
77         }
78         else
79         {
80             rt_iounmap(redis_base_virtual);
81         }
82 #else
83 #if defined(TARGET_ARMV8_AARCH64)
84         gicr_typer_aff = GIC_RDIST_TYPER(redis_base) >> 32;
85 #else
86         gicr_typer_aff = GIC_RDIST_TYPER(redis_base + 0x4);
87 #endif
88         if (mpidr_aff == gicr_typer_aff)
89         {
90             return redis_base;
91         }
92 #endif
93     }
94 
95     return 0;
96 }
97 
98 
99 #if defined(TARGET_ARMV8_AARCH64)
100 
platform_get_gic_cpu_base(void)101 rt_inline rt_uint32_t platform_get_gic_cpu_base(void)
102 {
103     return 0; /* unused in gicv3 */
104 }
105 
106 #endif
107 
108 
109 int phytium_cpu_id_mapping(int cpu_id);
110 
111 #endif // !
112