1*4fe996c2SAndrew Jeffery /* SPDX-License-Identifier: Apache-2.0 */ 2*4fe996c2SAndrew Jeffery /* Copyright (C) 2018 IBM Corp. */ 3e39c9163SSuraj Jitindar Singh #ifndef COMMON_H 4e39c9163SSuraj Jitindar Singh #define COMMON_H 5e39c9163SSuraj Jitindar Singh 636a39f6bSAndrew Jeffery #include <stdarg.h> 736a39f6bSAndrew Jeffery #include <stdbool.h> 836a39f6bSAndrew Jeffery #include <stdint.h> 936a39f6bSAndrew Jeffery 10314929b4SCyril Bur #ifndef PREFIX 11314929b4SCyril Bur #define PREFIX "" 12314929b4SCyril Bur #endif 13314929b4SCyril Bur 1490b92fe4SRatan Gupta enum verbose { 15314929b4SCyril Bur MBOX_LOG_NONE = 0, 1628519598SSuraj Jitindar Singh MBOX_LOG_INFO = 1, 17314929b4SCyril Bur MBOX_LOG_DEBUG = 2 1890b92fe4SRatan Gupta }; 1990b92fe4SRatan Gupta 2090b92fe4SRatan Gupta extern enum verbose verbosity; 21314929b4SCyril Bur 2228519598SSuraj Jitindar Singh /* Error Messages */ 2344ac078bSAndrew Jeffery #define MSG_ERR(f_, ...) \ 2444ac078bSAndrew Jeffery do { \ 2544ac078bSAndrew Jeffery mbox_log(LOG_ERR, f_, ##__VA_ARGS__); \ 2644ac078bSAndrew Jeffery } while (0) 2744ac078bSAndrew Jeffery 2828519598SSuraj Jitindar Singh /* Informational Messages */ 2944ac078bSAndrew Jeffery #define MSG_INFO(f_, ...) \ 3044ac078bSAndrew Jeffery do { \ 3144ac078bSAndrew Jeffery if (verbosity >= MBOX_LOG_INFO) { \ 32e39c9163SSuraj Jitindar Singh mbox_log(LOG_INFO, f_, ##__VA_ARGS__); \ 3344ac078bSAndrew Jeffery } \ 3444ac078bSAndrew Jeffery } while (0) 3544ac078bSAndrew Jeffery 3628519598SSuraj Jitindar Singh /* Debug Messages */ 3744ac078bSAndrew Jeffery #define MSG_DBG(f_, ...) \ 3844ac078bSAndrew Jeffery do { \ 3944ac078bSAndrew Jeffery if (verbosity >= MBOX_LOG_DEBUG) { \ 4028519598SSuraj Jitindar Singh mbox_log(LOG_DEBUG, f_, ##__VA_ARGS__); \ 4144ac078bSAndrew Jeffery } \ 4244ac078bSAndrew Jeffery } while(0) 43e39c9163SSuraj Jitindar Singh 4490b92fe4SRatan Gupta extern void (*mbox_vlog)(int p, const char *fmt, va_list args); 45314929b4SCyril Bur 46393821ddSDeepak Kodihalli #ifdef __cplusplus 47393821ddSDeepak Kodihalli extern "C" { 48393821ddSDeepak Kodihalli #endif 49393821ddSDeepak Kodihalli 50314929b4SCyril Bur void mbox_log_console(int p, const char *fmt, va_list args); 51314929b4SCyril Bur 52314929b4SCyril Bur __attribute__((format(printf, 2, 3))) 53314929b4SCyril Bur void mbox_log(int p, const char *fmt, ...); 54314929b4SCyril Bur 55314929b4SCyril Bur uint16_t get_u16(uint8_t *ptr); 56314929b4SCyril Bur 57314929b4SCyril Bur void put_u16(uint8_t *ptr, uint16_t val); 58314929b4SCyril Bur 59314929b4SCyril Bur uint32_t get_u32(uint8_t *ptr); 60314929b4SCyril Bur 61314929b4SCyril Bur void put_u32(uint8_t *ptr, uint32_t val); 62314929b4SCyril Bur 63e39c9163SSuraj Jitindar Singh static inline uint32_t align_up(uint32_t val, uint32_t size) 64e39c9163SSuraj Jitindar Singh { 65e39c9163SSuraj Jitindar Singh return (((val) + (size) - 1) & ~((size) - 1)); 66e39c9163SSuraj Jitindar Singh } 67e39c9163SSuraj Jitindar Singh 68e39c9163SSuraj Jitindar Singh static inline uint32_t align_down(uint32_t val, uint32_t size) 69e39c9163SSuraj Jitindar Singh { 70e39c9163SSuraj Jitindar Singh return ((val) & ~(((size) - 1))); 71e39c9163SSuraj Jitindar Singh } 72e39c9163SSuraj Jitindar Singh 73e39c9163SSuraj Jitindar Singh static inline uint32_t min_u32(uint32_t a, uint32_t b) 74e39c9163SSuraj Jitindar Singh { 75e39c9163SSuraj Jitindar Singh if (a <= b) { 76e39c9163SSuraj Jitindar Singh return a; 77e39c9163SSuraj Jitindar Singh } 78e39c9163SSuraj Jitindar Singh 79e39c9163SSuraj Jitindar Singh return b; 80e39c9163SSuraj Jitindar Singh } 81e39c9163SSuraj Jitindar Singh 82e39c9163SSuraj Jitindar Singh static inline int log_2(int val) 83e39c9163SSuraj Jitindar Singh { 84e39c9163SSuraj Jitindar Singh int ret = 0; 85e39c9163SSuraj Jitindar Singh 86e39c9163SSuraj Jitindar Singh if (val <= 0) { 87e39c9163SSuraj Jitindar Singh return -1; 88e39c9163SSuraj Jitindar Singh } 89e39c9163SSuraj Jitindar Singh 90e39c9163SSuraj Jitindar Singh while (val >>= 1) { 91e39c9163SSuraj Jitindar Singh ret++; 92e39c9163SSuraj Jitindar Singh } 93e39c9163SSuraj Jitindar Singh 94e39c9163SSuraj Jitindar Singh return ret; 95e39c9163SSuraj Jitindar Singh } 96e39c9163SSuraj Jitindar Singh 970aff80c0SSuraj Jitindar Singh static inline bool is_power_of_2(unsigned val) 980aff80c0SSuraj Jitindar Singh { 990aff80c0SSuraj Jitindar Singh return __builtin_popcount(val) == 1; 1000aff80c0SSuraj Jitindar Singh } 1010aff80c0SSuraj Jitindar Singh 102314929b4SCyril Bur char *get_dev_mtd(void); 103e39c9163SSuraj Jitindar Singh 104393821ddSDeepak Kodihalli #ifdef __cplusplus 105393821ddSDeepak Kodihalli } 106393821ddSDeepak Kodihalli #endif 107393821ddSDeepak Kodihalli 108e39c9163SSuraj Jitindar Singh #endif /* COMMON_H */ 109