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> |
Andrew Jeffery | 25863af | 2018-08-03 15:36:21 +0930 | [diff] [blame] | 9 | #include <syslog.h> |
Andrew Jeffery | 36a39f6 | 2017-04-10 16:25:04 +0930 | [diff] [blame] | 10 | |
Cyril Bur | 314929b | 2016-10-14 15:55:16 +1100 | [diff] [blame] | 11 | #ifndef PREFIX |
| 12 | #define PREFIX "" |
| 13 | #endif |
| 14 | |
Ratan Gupta | 90b92fe | 2017-05-05 17:34:00 +0530 | [diff] [blame] | 15 | enum verbose { |
Andrew Jeffery | 44ac078 | 2018-02-26 13:03:40 +1030 | [diff] [blame] | 16 | MBOX_LOG_NONE = 0, |
| 17 | MBOX_LOG_INFO = 1, |
| 18 | MBOX_LOG_DEBUG = 2 |
Ratan Gupta | 90b92fe | 2017-05-05 17:34:00 +0530 | [diff] [blame] | 19 | }; |
| 20 | |
| 21 | extern enum verbose verbosity; |
Cyril Bur | 314929b | 2016-10-14 15:55:16 +1100 | [diff] [blame] | 22 | |
Suraj Jitindar Singh | 2851959 | 2017-04-27 14:48:58 +1000 | [diff] [blame] | 23 | /* Error Messages */ |
Andrew Jeffery | 44ac078 | 2018-02-26 13:03:40 +1030 | [diff] [blame] | 24 | #define MSG_ERR(f_, ...) \ |
| 25 | do { \ |
| 26 | mbox_log(LOG_ERR, f_, ##__VA_ARGS__); \ |
| 27 | } while (0) |
| 28 | |
Suraj Jitindar Singh | 2851959 | 2017-04-27 14:48:58 +1000 | [diff] [blame] | 29 | /* Informational Messages */ |
Andrew Jeffery | 44ac078 | 2018-02-26 13:03:40 +1030 | [diff] [blame] | 30 | #define MSG_INFO(f_, ...) \ |
| 31 | do { \ |
| 32 | if (verbosity >= MBOX_LOG_INFO) { \ |
| 33 | mbox_log(LOG_INFO, f_, ##__VA_ARGS__); \ |
| 34 | } \ |
| 35 | } while (0) |
| 36 | |
Suraj Jitindar Singh | 2851959 | 2017-04-27 14:48:58 +1000 | [diff] [blame] | 37 | /* Debug Messages */ |
Andrew Jeffery | 44ac078 | 2018-02-26 13:03:40 +1030 | [diff] [blame] | 38 | #define MSG_DBG(f_, ...) \ |
| 39 | do { \ |
| 40 | if (verbosity >= MBOX_LOG_DEBUG) { \ |
| 41 | mbox_log(LOG_DEBUG, f_, ##__VA_ARGS__); \ |
| 42 | } \ |
| 43 | } while(0) |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 44 | |
Ratan Gupta | 90b92fe | 2017-05-05 17:34:00 +0530 | [diff] [blame] | 45 | extern void (*mbox_vlog)(int p, const char *fmt, va_list args); |
Cyril Bur | 314929b | 2016-10-14 15:55:16 +1100 | [diff] [blame] | 46 | |
Deepak Kodihalli | 393821d | 2017-04-28 04:44:38 -0500 | [diff] [blame] | 47 | #ifdef __cplusplus |
| 48 | extern "C" { |
| 49 | #endif |
| 50 | |
Cyril Bur | 314929b | 2016-10-14 15:55:16 +1100 | [diff] [blame] | 51 | void mbox_log_console(int p, const char *fmt, va_list args); |
| 52 | |
| 53 | __attribute__((format(printf, 2, 3))) |
| 54 | void mbox_log(int p, const char *fmt, ...); |
| 55 | |
| 56 | uint16_t get_u16(uint8_t *ptr); |
| 57 | |
| 58 | void put_u16(uint8_t *ptr, uint16_t val); |
| 59 | |
| 60 | uint32_t get_u32(uint8_t *ptr); |
| 61 | |
| 62 | void put_u32(uint8_t *ptr, uint32_t val); |
| 63 | |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 64 | static inline uint32_t align_up(uint32_t val, uint32_t size) |
| 65 | { |
| 66 | return (((val) + (size) - 1) & ~((size) - 1)); |
| 67 | } |
| 68 | |
| 69 | static inline uint32_t align_down(uint32_t val, uint32_t size) |
| 70 | { |
| 71 | return ((val) & ~(((size) - 1))); |
| 72 | } |
| 73 | |
| 74 | static inline uint32_t min_u32(uint32_t a, uint32_t b) |
| 75 | { |
| 76 | if (a <= b) { |
| 77 | return a; |
| 78 | } |
| 79 | |
| 80 | return b; |
| 81 | } |
| 82 | |
| 83 | static inline int log_2(int val) |
| 84 | { |
| 85 | int ret = 0; |
| 86 | |
| 87 | if (val <= 0) { |
| 88 | return -1; |
| 89 | } |
| 90 | |
| 91 | while (val >>= 1) { |
| 92 | ret++; |
| 93 | } |
| 94 | |
| 95 | return ret; |
| 96 | } |
| 97 | |
Suraj Jitindar Singh | 0aff80c | 2017-04-12 14:37:24 +1000 | [diff] [blame] | 98 | static inline bool is_power_of_2(unsigned val) |
| 99 | { |
| 100 | return __builtin_popcount(val) == 1; |
| 101 | } |
| 102 | |
Cyril Bur | 314929b | 2016-10-14 15:55:16 +1100 | [diff] [blame] | 103 | char *get_dev_mtd(void); |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 104 | |
Deepak Kodihalli | 393821d | 2017-04-28 04:44:38 -0500 | [diff] [blame] | 105 | #ifdef __cplusplus |
| 106 | } |
| 107 | #endif |
| 108 | |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 109 | #endif /* COMMON_H */ |