1 /* SPDX-License-Identifier: Apache-2.0 */ 2 /* Copyright (C) 2018 IBM Corp. */ 3 #ifndef COMMON_H 4 #define COMMON_H 5 6 #include <stdarg.h> 7 #include <stdbool.h> 8 #include <stdint.h> 9 10 #ifndef PREFIX 11 #define PREFIX "" 12 #endif 13 14 enum verbose { 15 MBOX_LOG_NONE = 0, 16 MBOX_LOG_INFO = 1, 17 MBOX_LOG_DEBUG = 2 18 }; 19 20 extern enum verbose verbosity; 21 22 /* Error Messages */ 23 #define MSG_ERR(f_, ...) \ 24 do { \ 25 mbox_log(LOG_ERR, f_, ##__VA_ARGS__); \ 26 } while (0) 27 28 /* Informational Messages */ 29 #define MSG_INFO(f_, ...) \ 30 do { \ 31 if (verbosity >= MBOX_LOG_INFO) { \ 32 mbox_log(LOG_INFO, f_, ##__VA_ARGS__); \ 33 } \ 34 } while (0) 35 36 /* Debug Messages */ 37 #define MSG_DBG(f_, ...) \ 38 do { \ 39 if (verbosity >= MBOX_LOG_DEBUG) { \ 40 mbox_log(LOG_DEBUG, f_, ##__VA_ARGS__); \ 41 } \ 42 } while(0) 43 44 extern void (*mbox_vlog)(int p, const char *fmt, va_list args); 45 46 #ifdef __cplusplus 47 extern "C" { 48 #endif 49 50 void mbox_log_console(int p, const char *fmt, va_list args); 51 52 __attribute__((format(printf, 2, 3))) 53 void mbox_log(int p, const char *fmt, ...); 54 55 uint16_t get_u16(uint8_t *ptr); 56 57 void put_u16(uint8_t *ptr, uint16_t val); 58 59 uint32_t get_u32(uint8_t *ptr); 60 61 void put_u32(uint8_t *ptr, uint32_t val); 62 63 static inline uint32_t align_up(uint32_t val, uint32_t size) 64 { 65 return (((val) + (size) - 1) & ~((size) - 1)); 66 } 67 68 static inline uint32_t align_down(uint32_t val, uint32_t size) 69 { 70 return ((val) & ~(((size) - 1))); 71 } 72 73 static inline uint32_t min_u32(uint32_t a, uint32_t b) 74 { 75 if (a <= b) { 76 return a; 77 } 78 79 return b; 80 } 81 82 static inline int log_2(int val) 83 { 84 int ret = 0; 85 86 if (val <= 0) { 87 return -1; 88 } 89 90 while (val >>= 1) { 91 ret++; 92 } 93 94 return ret; 95 } 96 97 static inline bool is_power_of_2(unsigned val) 98 { 99 return __builtin_popcount(val) == 1; 100 } 101 102 char *get_dev_mtd(void); 103 104 #ifdef __cplusplus 105 } 106 #endif 107 108 #endif /* COMMON_H */ 109