blob: 0bd99de08e95d1038c3108a7682e36b45cd3fa78 [file] [log] [blame]
Andrew Jeffery4fe996c2018-02-27 12:16:48 +10301/* SPDX-License-Identifier: Apache-2.0 */
2/* Copyright (C) 2018 IBM Corp. */
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +11003#ifndef COMMON_H
4#define COMMON_H
5
Andrew Jeffery36a39f62017-04-10 16:25:04 +09306#include <stdarg.h>
7#include <stdbool.h>
8#include <stdint.h>
Andrew Jeffery25863af2018-08-03 15:36:21 +09309#include <syslog.h>
Andrew Jeffery36a39f62017-04-10 16:25:04 +093010
Cyril Bur314929b2016-10-14 15:55:16 +110011#ifndef PREFIX
12#define PREFIX ""
13#endif
14
Ratan Gupta90b92fe2017-05-05 17:34:00 +053015enum verbose {
Andrew Jeffery44ac0782018-02-26 13:03:40 +103016 MBOX_LOG_NONE = 0,
17 MBOX_LOG_INFO = 1,
18 MBOX_LOG_DEBUG = 2
Ratan Gupta90b92fe2017-05-05 17:34:00 +053019};
20
21extern enum verbose verbosity;
Cyril Bur314929b2016-10-14 15:55:16 +110022
Suraj Jitindar Singh28519592017-04-27 14:48:58 +100023/* Error Messages */
Andrew Jeffery44ac0782018-02-26 13:03:40 +103024#define MSG_ERR(f_, ...) \
25do { \
26 mbox_log(LOG_ERR, f_, ##__VA_ARGS__); \
27} while (0)
28
Suraj Jitindar Singh28519592017-04-27 14:48:58 +100029/* Informational Messages */
Andrew Jeffery44ac0782018-02-26 13:03:40 +103030#define MSG_INFO(f_, ...) \
31do { \
32 if (verbosity >= MBOX_LOG_INFO) { \
33 mbox_log(LOG_INFO, f_, ##__VA_ARGS__); \
34 } \
35} while (0)
36
Suraj Jitindar Singh28519592017-04-27 14:48:58 +100037/* Debug Messages */
Andrew Jeffery44ac0782018-02-26 13:03:40 +103038#define MSG_DBG(f_, ...) \
39do { \
40 if (verbosity >= MBOX_LOG_DEBUG) { \
41 mbox_log(LOG_DEBUG, f_, ##__VA_ARGS__); \
42 } \
43} while(0)
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +110044
Ratan Gupta90b92fe2017-05-05 17:34:00 +053045extern void (*mbox_vlog)(int p, const char *fmt, va_list args);
Cyril Bur314929b2016-10-14 15:55:16 +110046
Deepak Kodihalli393821d2017-04-28 04:44:38 -050047#ifdef __cplusplus
48extern "C" {
49#endif
50
Cyril Bur314929b2016-10-14 15:55:16 +110051void mbox_log_console(int p, const char *fmt, va_list args);
52
53__attribute__((format(printf, 2, 3)))
54void mbox_log(int p, const char *fmt, ...);
55
56uint16_t get_u16(uint8_t *ptr);
57
58void put_u16(uint8_t *ptr, uint16_t val);
59
60uint32_t get_u32(uint8_t *ptr);
61
62void put_u32(uint8_t *ptr, uint32_t val);
63
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +110064static inline uint32_t align_up(uint32_t val, uint32_t size)
65{
66 return (((val) + (size) - 1) & ~((size) - 1));
67}
68
69static inline uint32_t align_down(uint32_t val, uint32_t size)
70{
71 return ((val) & ~(((size) - 1)));
72}
73
74static 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
83static 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 Singh0aff80c2017-04-12 14:37:24 +100098static inline bool is_power_of_2(unsigned val)
99{
100 return __builtin_popcount(val) == 1;
101}
102
Cyril Bur314929b2016-10-14 15:55:16 +1100103char *get_dev_mtd(void);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100104
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500105#ifdef __cplusplus
106}
107#endif
108
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100109#endif /* COMMON_H */