xref: /openbmc/phosphor-mboxd/common.h (revision 90b92fe48ec72b6c15cce24d33ac983548368b98)
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  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  *
15  */
16 
17 #ifndef COMMON_H
18 #define COMMON_H
19 
20 #include <stdarg.h>
21 #include <stdbool.h>
22 #include <stdint.h>
23 
24 #ifndef PREFIX
25 #define PREFIX ""
26 #endif
27 
28 enum verbose {
29    MBOX_LOG_NONE = 0,
30    MBOX_LOG_INFO = 1,
31    MBOX_LOG_DEBUG = 2
32 };
33 
34 extern enum verbose verbosity;
35 
36 /* Error Messages */
37 #define MSG_ERR(f_, ...)	mbox_log(LOG_ERR, f_, ##__VA_ARGS__)
38 /* Informational Messages */
39 #define MSG_INFO(f_, ...)	do { if (verbosity >= MBOX_LOG_INFO) { \
40 					mbox_log(LOG_INFO, f_, ##__VA_ARGS__); \
41 				} } while (0)
42 /* Debug Messages */
43 #define MSG_DBG(f_, ...)	do { if (verbosity >= MBOX_LOG_DEBUG) { \
44 					mbox_log(LOG_DEBUG, f_, ##__VA_ARGS__); \
45 				} } while(0)
46 
47 extern void (*mbox_vlog)(int p, const char *fmt, va_list args);
48 
49 void mbox_log_console(int p, const char *fmt, va_list args);
50 
51 __attribute__((format(printf, 2, 3)))
52 void mbox_log(int p, const char *fmt, ...);
53 
54 uint16_t get_u16(uint8_t *ptr);
55 
56 void put_u16(uint8_t *ptr, uint16_t val);
57 
58 uint32_t get_u32(uint8_t *ptr);
59 
60 void put_u32(uint8_t *ptr, uint32_t val);
61 
62 static inline uint32_t align_up(uint32_t val, uint32_t size)
63 {
64 	return (((val) + (size) - 1) & ~((size) - 1));
65 }
66 
67 static inline uint32_t align_down(uint32_t val, uint32_t size)
68 {
69 	return ((val) & ~(((size) - 1)));
70 }
71 
72 static inline uint32_t min_u32(uint32_t a, uint32_t b)
73 {
74 	if (a <= b) {
75 		return a;
76 	}
77 
78 	return b;
79 }
80 
81 static inline int log_2(int val)
82 {
83 	int ret = 0;
84 
85 	if (val <= 0) {
86 		return -1;
87 	}
88 
89 	while (val >>= 1) {
90 		ret++;
91 	}
92 
93 	return ret;
94 }
95 
96 static inline bool is_power_of_2(unsigned val)
97 {
98 	return __builtin_popcount(val) == 1;
99 }
100 
101 char *get_dev_mtd(void);
102 
103 #endif /* COMMON_H */
104