Xiaohu.Huang | 83a0b70 | 2021-12-28 11:06:24 +0800 | [diff] [blame] | 1 | /* |
yang.li | 5bef2f6 | 2022-01-11 14:08:06 +0800 | [diff] [blame] | 2 | * Copyright (c) 2021-2022 Amlogic, Inc. All rights reserved. |
Xiaohu.Huang | 83a0b70 | 2021-12-28 11:06:24 +0800 | [diff] [blame] | 3 | * |
yang.li | 5bef2f6 | 2022-01-11 14:08:06 +0800 | [diff] [blame] | 4 | * SPDX-License-Identifier: MIT |
Xiaohu.Huang | 83a0b70 | 2021-12-28 11:06:24 +0800 | [diff] [blame] | 5 | */ |
| 6 | |
xiaohu.huang | 797c4c1 | 2024-01-24 18:52:43 +0800 | [diff] [blame] | 7 | #ifndef __COMMON_H__ |
| 8 | #define __COMMON_H__ |
Xiaohu.Huang | 83a0b70 | 2021-12-28 11:06:24 +0800 | [diff] [blame] | 9 | |
| 10 | #ifdef __cplusplus |
| 11 | extern "C" { |
| 12 | #endif |
Xiaohu.Huang | 83a0b70 | 2021-12-28 11:06:24 +0800 | [diff] [blame] | 13 | #include <errno.h> |
shijie.xiong | e89c246 | 2023-08-08 15:46:11 +0800 | [diff] [blame] | 14 | #include <stdint.h> |
Xiaohu.Huang | 83a0b70 | 2021-12-28 11:06:24 +0800 | [diff] [blame] | 15 | |
Xiaohu.Huang | 83a0b70 | 2021-12-28 11:06:24 +0800 | [diff] [blame] | 16 | /* Macros to access registers */ |
| 17 | #define REG32_ADDR(addr) ((volatile uint32_t *)(uintptr_t)(addr)) |
| 18 | #define REG16_ADDR(addr) ((volatile uint16_t *)(uintptr_t)(addr)) |
xiaohu.huang | e7678d1 | 2022-05-10 00:56:48 +0800 | [diff] [blame] | 19 | #define REG8_ADDR(addr) ((volatile uint8_t *)(uintptr_t)(addr)) |
Xiaohu.Huang | 83a0b70 | 2021-12-28 11:06:24 +0800 | [diff] [blame] | 20 | |
| 21 | #define REG32(addr) (*REG32_ADDR(addr)) |
| 22 | #define REG16(addr) (*REG16_ADDR(addr)) |
xiaohu.huang | e7678d1 | 2022-05-10 00:56:48 +0800 | [diff] [blame] | 23 | #define REG8(addr) (*REG8_ADDR(addr)) |
Xiaohu.Huang | 83a0b70 | 2021-12-28 11:06:24 +0800 | [diff] [blame] | 24 | |
xiaohu.huang | 797c4c1 | 2024-01-24 18:52:43 +0800 | [diff] [blame] | 25 | #define REG32_SET_BITS(addr, bits) \ |
| 26 | do { \ |
| 27 | REG32(addr) = REG32(addr) | (bits); \ |
| 28 | } while(0) |
| 29 | |
| 30 | #define REG32_CLR_BITS(addr, bits) \ |
| 31 | do { \ |
| 32 | REG32(addr) = REG32(addr) & (~(bits)); \ |
| 33 | } while(0) \ |
| 34 | |
| 35 | #define REG32_UPDATE_BITS(addr, mask, val) \ |
| 36 | do { \ |
| 37 | uint32_t _v = REG32((unsigned long)addr); \ |
| 38 | _v &= (~(mask)); \ |
| 39 | _v |= ((val) & (mask)); \ |
| 40 | REG32((unsigned long)addr) = _v; \ |
xiaohu.huang | e7678d1 | 2022-05-10 00:56:48 +0800 | [diff] [blame] | 41 | } while (0) |
Xiaohu.Huang | 83a0b70 | 2021-12-28 11:06:24 +0800 | [diff] [blame] | 42 | |
xiaohu.huang | 797c4c1 | 2024-01-24 18:52:43 +0800 | [diff] [blame] | 43 | #define BIT(nr) (1UL << (nr)) |
| 44 | |
xiaohu.huang | 0a90955 | 2022-09-19 14:43:08 +0800 | [diff] [blame] | 45 | static inline int generic_ffs(int x) |
| 46 | { |
| 47 | int r = 1; |
| 48 | |
| 49 | if (!x) |
| 50 | return 0; |
| 51 | if (!(x & 0xffff)) { |
| 52 | x >>= 16; |
| 53 | r += 16; |
| 54 | } |
| 55 | if (!(x & 0xff)) { |
| 56 | x >>= 8; |
| 57 | r += 8; |
| 58 | } |
| 59 | if (!(x & 0xf)) { |
| 60 | x >>= 4; |
| 61 | r += 4; |
| 62 | } |
| 63 | if (!(x & 3)) { |
| 64 | x >>= 2; |
| 65 | r += 2; |
| 66 | } |
| 67 | if (!(x & 1)) { |
| 68 | x >>= 1; |
| 69 | r += 1; |
| 70 | } |
| 71 | return r; |
| 72 | } |
| 73 | #define ffs(x) generic_ffs(x) |
| 74 | |
Xiaohu.Huang | 83a0b70 | 2021-12-28 11:06:24 +0800 | [diff] [blame] | 75 | #ifndef FIELD_PREP |
xiaohu.huang | e7678d1 | 2022-05-10 00:56:48 +0800 | [diff] [blame] | 76 | #define FIELD_PREP(_mask, _val) (((typeof(_mask))(_val) << (ffs(_mask) - 1)) & (_mask)) |
Xiaohu.Huang | 83a0b70 | 2021-12-28 11:06:24 +0800 | [diff] [blame] | 77 | #endif |
| 78 | |
| 79 | #ifndef FIELD_GET |
xiaohu.huang | e7678d1 | 2022-05-10 00:56:48 +0800 | [diff] [blame] | 80 | #define FIELD_GET(_mask, _reg) ((typeof(_mask))(((_reg) & (_mask)) >> (ffs(_mask) - 1))) |
Xiaohu.Huang | 83a0b70 | 2021-12-28 11:06:24 +0800 | [diff] [blame] | 81 | #endif |
| 82 | |
| 83 | #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) |
| 84 | |
| 85 | #define BITS_PER_LONG (sizeof(unsigned long) == 8 ? 64 : 32) |
xiaohu.huang | 797c4c1 | 2024-01-24 18:52:43 +0800 | [diff] [blame] | 86 | |
xiaohu.huang | e7678d1 | 2022-05-10 00:56:48 +0800 | [diff] [blame] | 87 | #define GENMASK(h, l) (((~0UL) << (l)) & (~0UL >> (BITS_PER_LONG - 1 - (h)))) |
xiaohu.huang | 797c4c1 | 2024-01-24 18:52:43 +0800 | [diff] [blame] | 88 | |
xiaohu.huang | e7678d1 | 2022-05-10 00:56:48 +0800 | [diff] [blame] | 89 | #define IS_ALIGNED(x, a) (((unsigned long)(x) & ((unsigned long)(a)-1)) == 0) |
xiaohu.huang | 797c4c1 | 2024-01-24 18:52:43 +0800 | [diff] [blame] | 90 | |
xiaohu.huang | e7678d1 | 2022-05-10 00:56:48 +0800 | [diff] [blame] | 91 | #define _RET_IP_ ((unsigned long)__builtin_return_address(0)) |
xiaohu.huang | 797c4c1 | 2024-01-24 18:52:43 +0800 | [diff] [blame] | 92 | |
shijie.xiong | e89c246 | 2023-08-08 15:46:11 +0800 | [diff] [blame] | 93 | #define _THIS_IP_ \ |
| 94 | ({ \ |
| 95 | __label__ __here; \ |
| 96 | __here: \ |
| 97 | (unsigned long)&&__here; \ |
xiaohu.huang | e7678d1 | 2022-05-10 00:56:48 +0800 | [diff] [blame] | 98 | }) |
xiaohu.huang | 797c4c1 | 2024-01-24 18:52:43 +0800 | [diff] [blame] | 99 | |
Xiaohu.Huang | 83a0b70 | 2021-12-28 11:06:24 +0800 | [diff] [blame] | 100 | #define __round_mask(x, y) ((__typeof__(x))((y)-1)) |
xiaohu.huang | e7678d1 | 2022-05-10 00:56:48 +0800 | [diff] [blame] | 101 | #define round_up(x, y) ((((x)-1) | __round_mask(x, y)) + 1) |
Xiaohu.Huang | 83a0b70 | 2021-12-28 11:06:24 +0800 | [diff] [blame] | 102 | #define round_down(x, y) ((x) & ~__round_mask(x, y)) |
| 103 | |
xiaohu.huang | 797c4c1 | 2024-01-24 18:52:43 +0800 | [diff] [blame] | 104 | #define MAX(a, b) ((a) > (b) ? (a) : (b)) |
| 105 | #define MIN(a, b) ((a) < (b) ? (a) : (b)) |
| 106 | #define CLAMP(a, lo, hi) MIN(MAX(a, lo), hi) |
| 107 | |
Xiaohu.Huang | 83a0b70 | 2021-12-28 11:06:24 +0800 | [diff] [blame] | 108 | typedef uint64_t u64; |
| 109 | typedef uint32_t u32; |
| 110 | typedef uint16_t u16; |
| 111 | typedef uint8_t u8; |
| 112 | typedef int64_t s64; |
| 113 | typedef int32_t s32; |
| 114 | typedef int16_t s16; |
| 115 | typedef int8_t s8; |
| 116 | |
shijie.xiong | e89c246 | 2023-08-08 15:46:11 +0800 | [diff] [blame] | 117 | /* |
| 118 | * This struct define the way the registers are |
| 119 | * stored on the stack during an exception |
| 120 | */ |
| 121 | struct pt_regs { |
shijie.xiong | 1038db0 | 2024-02-01 14:16:05 +0800 | [diff] [blame] | 122 | #ifdef CONFIG_RISCV_WCN |
| 123 | uint32_t mepc; |
| 124 | uint32_t ra; //x1 |
| 125 | uint32_t regs[27]; /* x5-x31 */ |
| 126 | #else |
shijie.xiong | e89c246 | 2023-08-08 15:46:11 +0800 | [diff] [blame] | 127 | uint32_t regs[32]; /* include zero reg */ |
| 128 | uint32_t mstatus; /* machine status register */ |
| 129 | uint32_t mepc; /* machine exception program counter */ |
shijie.xiong | 1038db0 | 2024-02-01 14:16:05 +0800 | [diff] [blame] | 130 | #endif |
shijie.xiong | e89c246 | 2023-08-08 15:46:11 +0800 | [diff] [blame] | 131 | }; |
| 132 | |
xiaohu.huang | 797c4c1 | 2024-01-24 18:52:43 +0800 | [diff] [blame] | 133 | #define UNUSED_PARAM(X) ((void)X) |
| 134 | |
Xiaohu.Huang | 83a0b70 | 2021-12-28 11:06:24 +0800 | [diff] [blame] | 135 | #ifdef __cplusplus |
| 136 | } |
| 137 | #endif |
| 138 | #endif |