blob: 3e22dd27b702006e0439bc8e11cae4bec7bc790a [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>
9
Cyril Bur314929b2016-10-14 15:55:16 +110010#ifndef PREFIX
11#define PREFIX ""
12#endif
13
Ratan Gupta90b92fe2017-05-05 17:34:00 +053014enum verbose {
Andrew Jeffery44ac0782018-02-26 13:03:40 +103015 MBOX_LOG_NONE = 0,
16 MBOX_LOG_INFO = 1,
17 MBOX_LOG_DEBUG = 2
Ratan Gupta90b92fe2017-05-05 17:34:00 +053018};
19
20extern enum verbose verbosity;
Cyril Bur314929b2016-10-14 15:55:16 +110021
Suraj Jitindar Singh28519592017-04-27 14:48:58 +100022/* Error Messages */
Andrew Jeffery44ac0782018-02-26 13:03:40 +103023#define MSG_ERR(f_, ...) \
24do { \
25 mbox_log(LOG_ERR, f_, ##__VA_ARGS__); \
26} while (0)
27
Suraj Jitindar Singh28519592017-04-27 14:48:58 +100028/* Informational Messages */
Andrew Jeffery44ac0782018-02-26 13:03:40 +103029#define MSG_INFO(f_, ...) \
30do { \
31 if (verbosity >= MBOX_LOG_INFO) { \
32 mbox_log(LOG_INFO, f_, ##__VA_ARGS__); \
33 } \
34} while (0)
35
Suraj Jitindar Singh28519592017-04-27 14:48:58 +100036/* Debug Messages */
Andrew Jeffery44ac0782018-02-26 13:03:40 +103037#define MSG_DBG(f_, ...) \
38do { \
39 if (verbosity >= MBOX_LOG_DEBUG) { \
40 mbox_log(LOG_DEBUG, f_, ##__VA_ARGS__); \
41 } \
42} while(0)
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +110043
Ratan Gupta90b92fe2017-05-05 17:34:00 +053044extern void (*mbox_vlog)(int p, const char *fmt, va_list args);
Cyril Bur314929b2016-10-14 15:55:16 +110045
Deepak Kodihalli393821d2017-04-28 04:44:38 -050046#ifdef __cplusplus
47extern "C" {
48#endif
49
Cyril Bur314929b2016-10-14 15:55:16 +110050void mbox_log_console(int p, const char *fmt, va_list args);
51
52__attribute__((format(printf, 2, 3)))
53void mbox_log(int p, const char *fmt, ...);
54
55uint16_t get_u16(uint8_t *ptr);
56
57void put_u16(uint8_t *ptr, uint16_t val);
58
59uint32_t get_u32(uint8_t *ptr);
60
61void put_u32(uint8_t *ptr, uint32_t val);
62
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +110063static inline uint32_t align_up(uint32_t val, uint32_t size)
64{
65 return (((val) + (size) - 1) & ~((size) - 1));
66}
67
68static inline uint32_t align_down(uint32_t val, uint32_t size)
69{
70 return ((val) & ~(((size) - 1)));
71}
72
73static 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
82static 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 Singh0aff80c2017-04-12 14:37:24 +100097static inline bool is_power_of_2(unsigned val)
98{
99 return __builtin_popcount(val) == 1;
100}
101
Cyril Bur314929b2016-10-14 15:55:16 +1100102char *get_dev_mtd(void);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100103
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500104#ifdef __cplusplus
105}
106#endif
107
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100108#endif /* COMMON_H */