Cyril Bur | 314929b | 2016-10-14 15:55:16 +1100 | [diff] [blame] | 1 | /* Copyright 2016 IBM |
| 2 | * |
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 5 | * You may obtain a copy of the License at |
| 6 | * |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
Cyril Bur | 314929b | 2016-10-14 15:55:16 +1100 | [diff] [blame] | 8 | * |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | * See the License for the specific language governing permissions and |
| 13 | * limitations under the License. |
Cyril Bur | 314929b | 2016-10-14 15:55:16 +1100 | [diff] [blame] | 14 | * |
| 15 | */ |
| 16 | |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 17 | #ifndef COMMON_H |
| 18 | #define COMMON_H |
| 19 | |
Andrew Jeffery | 36a39f6 | 2017-04-10 16:25:04 +0930 | [diff] [blame] | 20 | #include <stdarg.h> |
| 21 | #include <stdbool.h> |
| 22 | #include <stdint.h> |
| 23 | |
Cyril Bur | 314929b | 2016-10-14 15:55:16 +1100 | [diff] [blame] | 24 | #ifndef PREFIX |
| 25 | #define PREFIX "" |
| 26 | #endif |
| 27 | |
Ratan Gupta | 90b92fe | 2017-05-05 17:34:00 +0530 | [diff] [blame] | 28 | enum verbose { |
Andrew Jeffery | 44ac078 | 2018-02-26 13:03:40 +1030 | [diff] [blame] | 29 | MBOX_LOG_NONE = 0, |
| 30 | MBOX_LOG_INFO = 1, |
| 31 | MBOX_LOG_DEBUG = 2 |
Ratan Gupta | 90b92fe | 2017-05-05 17:34:00 +0530 | [diff] [blame] | 32 | }; |
| 33 | |
| 34 | extern enum verbose verbosity; |
Cyril Bur | 314929b | 2016-10-14 15:55:16 +1100 | [diff] [blame] | 35 | |
Suraj Jitindar Singh | 2851959 | 2017-04-27 14:48:58 +1000 | [diff] [blame] | 36 | /* Error Messages */ |
Andrew Jeffery | 44ac078 | 2018-02-26 13:03:40 +1030 | [diff] [blame] | 37 | #define MSG_ERR(f_, ...) \ |
| 38 | do { \ |
| 39 | mbox_log(LOG_ERR, f_, ##__VA_ARGS__); \ |
| 40 | } while (0) |
| 41 | |
Suraj Jitindar Singh | 2851959 | 2017-04-27 14:48:58 +1000 | [diff] [blame] | 42 | /* Informational Messages */ |
Andrew Jeffery | 44ac078 | 2018-02-26 13:03:40 +1030 | [diff] [blame] | 43 | #define MSG_INFO(f_, ...) \ |
| 44 | do { \ |
| 45 | if (verbosity >= MBOX_LOG_INFO) { \ |
| 46 | mbox_log(LOG_INFO, f_, ##__VA_ARGS__); \ |
| 47 | } \ |
| 48 | } while (0) |
| 49 | |
Suraj Jitindar Singh | 2851959 | 2017-04-27 14:48:58 +1000 | [diff] [blame] | 50 | /* Debug Messages */ |
Andrew Jeffery | 44ac078 | 2018-02-26 13:03:40 +1030 | [diff] [blame] | 51 | #define MSG_DBG(f_, ...) \ |
| 52 | do { \ |
| 53 | if (verbosity >= MBOX_LOG_DEBUG) { \ |
| 54 | mbox_log(LOG_DEBUG, f_, ##__VA_ARGS__); \ |
| 55 | } \ |
| 56 | } while(0) |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 57 | |
Ratan Gupta | 90b92fe | 2017-05-05 17:34:00 +0530 | [diff] [blame] | 58 | extern void (*mbox_vlog)(int p, const char *fmt, va_list args); |
Cyril Bur | 314929b | 2016-10-14 15:55:16 +1100 | [diff] [blame] | 59 | |
Deepak Kodihalli | 393821d | 2017-04-28 04:44:38 -0500 | [diff] [blame] | 60 | #ifdef __cplusplus |
| 61 | extern "C" { |
| 62 | #endif |
| 63 | |
Cyril Bur | 314929b | 2016-10-14 15:55:16 +1100 | [diff] [blame] | 64 | void mbox_log_console(int p, const char *fmt, va_list args); |
| 65 | |
| 66 | __attribute__((format(printf, 2, 3))) |
| 67 | void mbox_log(int p, const char *fmt, ...); |
| 68 | |
| 69 | uint16_t get_u16(uint8_t *ptr); |
| 70 | |
| 71 | void put_u16(uint8_t *ptr, uint16_t val); |
| 72 | |
| 73 | uint32_t get_u32(uint8_t *ptr); |
| 74 | |
| 75 | void put_u32(uint8_t *ptr, uint32_t val); |
| 76 | |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 77 | static inline uint32_t align_up(uint32_t val, uint32_t size) |
| 78 | { |
| 79 | return (((val) + (size) - 1) & ~((size) - 1)); |
| 80 | } |
| 81 | |
| 82 | static inline uint32_t align_down(uint32_t val, uint32_t size) |
| 83 | { |
| 84 | return ((val) & ~(((size) - 1))); |
| 85 | } |
| 86 | |
| 87 | static inline uint32_t min_u32(uint32_t a, uint32_t b) |
| 88 | { |
| 89 | if (a <= b) { |
| 90 | return a; |
| 91 | } |
| 92 | |
| 93 | return b; |
| 94 | } |
| 95 | |
| 96 | static inline int log_2(int val) |
| 97 | { |
| 98 | int ret = 0; |
| 99 | |
| 100 | if (val <= 0) { |
| 101 | return -1; |
| 102 | } |
| 103 | |
| 104 | while (val >>= 1) { |
| 105 | ret++; |
| 106 | } |
| 107 | |
| 108 | return ret; |
| 109 | } |
| 110 | |
Suraj Jitindar Singh | 0aff80c | 2017-04-12 14:37:24 +1000 | [diff] [blame] | 111 | static inline bool is_power_of_2(unsigned val) |
| 112 | { |
| 113 | return __builtin_popcount(val) == 1; |
| 114 | } |
| 115 | |
Cyril Bur | 314929b | 2016-10-14 15:55:16 +1100 | [diff] [blame] | 116 | char *get_dev_mtd(void); |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 117 | |
Deepak Kodihalli | 393821d | 2017-04-28 04:44:38 -0500 | [diff] [blame] | 118 | #ifdef __cplusplus |
| 119 | } |
| 120 | #endif |
| 121 | |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 122 | #endif /* COMMON_H */ |