1 /*
2  * Copyright (c) 2021 HPMicro
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  */
7 
8 #ifndef HPM_MISC_H
9 #define HPM_MISC_H
10 
11 #define ILM_LOCAL_BASE (0x0U)
12 #define ILM_SIZE_IN_BYTE (0x40000U)
13 #define DLM_LOCAL_BASE (0x80000U)
14 #define DLM_SIZE_IN_BYTE (0x40000U)
15 #define CORE0_ILM_SYSTEM_BASE (0x1000000U)
16 #define CORE0_DLM_SYSTEM_BASE (0x1040000U)
17 #define CORE1_ILM_SYSTEM_BASE (0x1180000U)
18 #define CORE1_DLM_SYSTEM_BASE (0x11C0000U)
19 
20 #define ADDRESS_IN_ILM(address) \
21     ((ILM_LOCAL_BASE + ILM_SIZE_IN_BYTE) > (address))
22 #define ADDRESS_IN_DLM(address) \
23     (((DLM_LOCAL_BASE) <= (address)) && \
24      ((DLM_LOCAL_BASE + DLM_SIZE_IN_BYTE) > (address)))
25 
26 #define ADDRESS_IN_CORE0_ILM_SYSTEM(address) \
27     (((CORE0_ILM_SYSTEM_BASE) <= (address)) && \
28      ((CORE0_ILM_SYSTEM_BASE + ILM_SIZE_IN_BYTE) > (address)))
29 #define ADDRESS_IN_CORE0_DLM_SYSTEM(address) \
30     (((CORE0_DLM_SYSTEM_BASE) <= (address)) && \
31      ((CORE0_DLM_SYSTEM_BASE + DLM_SIZE_IN_BYTE) > (address)))
32 
33 #define ADDRESS_IN_CORE1_ILM_SYSTEM(address) \
34     (((CORE1_ILM_SYSTEM_BASE) <= (address)) && \
35      ((CORE1_ILM_SYSTEM_BASE + ILM_SIZE_IN_BYTE) > (address)))
36 #define ADDRESS_IN_CORE1_DLM_SYSTEM(address) \
37     (((CORE1_DLM_SYSTEM_BASE) <= (address)) && \
38      ((CORE1_DLM_SYSTEM_BASE + DLM_SIZE_IN_BYTE) > (address)))
39 
40 #define DLM_TO_SYSTEM(address) \
41     (CORE0_DLM_SYSTEM_BASE + (address) - (DLM_LOCAL_BASE))
42 #define ILM_TO_SYSTEM(address) \
43     (CORE0_ILM_SYSTEM_BASE + (address) - (ILM_LOCAL_BASE))
44 #define SYSTEM_TO_DLM(address) \
45     ((address) - CORE0_DLM_SYSTEM_BASE + (DLM_LOCAL_BASE))
46 
47 #define HPM_CORE0 (0U)
48 #define HPM_CORE1 (1U)
49 
50 /* map core local memory(DLM/ILM) to system address */
core_local_mem_to_sys_address(uint8_t core_id,uint32_t addr)51 static inline uint32_t core_local_mem_to_sys_address(uint8_t core_id, uint32_t addr)
52 {
53     uint32_t sys_addr;
54     if (ADDRESS_IN_ILM(addr)) {
55         sys_addr = ILM_TO_SYSTEM(addr);
56     } else if (ADDRESS_IN_DLM(addr)) {
57         sys_addr = DLM_TO_SYSTEM(addr);
58     } else {
59         return addr;
60     }
61     if (core_id == HPM_CORE1) {
62         sys_addr += CORE1_ILM_SYSTEM_BASE - CORE0_ILM_SYSTEM_BASE;
63     }
64 
65     return sys_addr;
66 }
67 
68 /* map system address to core local memory(DLM/ILM) */
sys_address_to_core_local_mem(uint8_t core_id,uint32_t addr)69 static inline uint32_t sys_address_to_core_local_mem(uint8_t core_id, uint32_t addr)
70 {
71     if (core_id == HPM_CORE1) {
72         if (ADDRESS_IN_CORE1_DLM_SYSTEM(addr)) {
73             addr = (addr - CORE1_DLM_SYSTEM_BASE) + DLM_LOCAL_BASE;
74         } else if (ADDRESS_IN_CORE1_ILM_SYSTEM(addr)) {
75             addr = (addr - CORE1_ILM_SYSTEM_BASE) + ILM_LOCAL_BASE;
76         }
77     } else {
78         if (ADDRESS_IN_CORE0_DLM_SYSTEM(addr)) {
79             addr = (addr - CORE0_DLM_SYSTEM_BASE) + DLM_LOCAL_BASE;
80         } else if (ADDRESS_IN_CORE0_ILM_SYSTEM(addr)) {
81             addr = (addr - CORE0_ILM_SYSTEM_BASE) + ILM_LOCAL_BASE;
82         }
83     }
84 
85     return addr;
86 }
87 #endif /* HPM_MISC_H */
88