Andrew Jeffery | 4fe996c | 2018-02-27 12:16:48 +1030 | [diff] [blame] | 1 | /* SPDX-License-Identifier: Apache-2.0 */ |
| 2 | /* Copyright (C) 2018 IBM Corp. */ |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 3 | #ifndef COMMON_H |
| 4 | #define COMMON_H |
| 5 | |
Andrew Jeffery | 36a39f6 | 2017-04-10 16:25:04 +0930 | [diff] [blame] | 6 | #include <stdarg.h> |
| 7 | #include <stdbool.h> |
| 8 | #include <stdint.h> |
| 9 | |
Cyril Bur | 314929b | 2016-10-14 15:55:16 +1100 | [diff] [blame] | 10 | #ifndef PREFIX |
| 11 | #define PREFIX "" |
| 12 | #endif |
| 13 | |
Ratan Gupta | 90b92fe | 2017-05-05 17:34:00 +0530 | [diff] [blame] | 14 | enum verbose { |
Andrew Jeffery | 44ac078 | 2018-02-26 13:03:40 +1030 | [diff] [blame] | 15 | MBOX_LOG_NONE = 0, |
| 16 | MBOX_LOG_INFO = 1, |
| 17 | MBOX_LOG_DEBUG = 2 |
Ratan Gupta | 90b92fe | 2017-05-05 17:34:00 +0530 | [diff] [blame] | 18 | }; |
| 19 | |
| 20 | extern enum verbose verbosity; |
Cyril Bur | 314929b | 2016-10-14 15:55:16 +1100 | [diff] [blame] | 21 | |
Suraj Jitindar Singh | 2851959 | 2017-04-27 14:48:58 +1000 | [diff] [blame] | 22 | /* Error Messages */ |
Andrew Jeffery | 44ac078 | 2018-02-26 13:03:40 +1030 | [diff] [blame] | 23 | #define MSG_ERR(f_, ...) \ |
| 24 | do { \ |
| 25 | mbox_log(LOG_ERR, f_, ##__VA_ARGS__); \ |
| 26 | } while (0) |
| 27 | |
Suraj Jitindar Singh | 2851959 | 2017-04-27 14:48:58 +1000 | [diff] [blame] | 28 | /* Informational Messages */ |
Andrew Jeffery | 44ac078 | 2018-02-26 13:03:40 +1030 | [diff] [blame] | 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 | |
Suraj Jitindar Singh | 2851959 | 2017-04-27 14:48:58 +1000 | [diff] [blame] | 36 | /* Debug Messages */ |
Andrew Jeffery | 44ac078 | 2018-02-26 13:03:40 +1030 | [diff] [blame] | 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) |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 43 | |
Ratan Gupta | 90b92fe | 2017-05-05 17:34:00 +0530 | [diff] [blame] | 44 | extern void (*mbox_vlog)(int p, const char *fmt, va_list args); |
Cyril Bur | 314929b | 2016-10-14 15:55:16 +1100 | [diff] [blame] | 45 | |
Deepak Kodihalli | 393821d | 2017-04-28 04:44:38 -0500 | [diff] [blame] | 46 | #ifdef __cplusplus |
| 47 | extern "C" { |
| 48 | #endif |
| 49 | |
Cyril Bur | 314929b | 2016-10-14 15:55:16 +1100 | [diff] [blame] | 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 | |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 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 | |
Suraj Jitindar Singh | 0aff80c | 2017-04-12 14:37:24 +1000 | [diff] [blame] | 97 | static inline bool is_power_of_2(unsigned val) |
| 98 | { |
| 99 | return __builtin_popcount(val) == 1; |
| 100 | } |
| 101 | |
Cyril Bur | 314929b | 2016-10-14 15:55:16 +1100 | [diff] [blame] | 102 | char *get_dev_mtd(void); |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 103 | |
Deepak Kodihalli | 393821d | 2017-04-28 04:44:38 -0500 | [diff] [blame] | 104 | #ifdef __cplusplus |
| 105 | } |
| 106 | #endif |
| 107 | |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 108 | #endif /* COMMON_H */ |