Shad Ansari | 2f7f9be | 2017-06-07 13:34:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | <:copyright-BRCM:2016:DUAL/GPL:standard |
| 3 | |
| 4 | Broadcom Proprietary and Confidential.(c) 2016 Broadcom |
| 5 | All Rights Reserved |
| 6 | |
| 7 | Unless you and Broadcom execute a separate written software license |
| 8 | agreement governing use of this software, this software is licensed |
| 9 | to you under the terms of the GNU General Public License version 2 |
| 10 | (the "GPL"), available at http://www.broadcom.com/licenses/GPLv2.php, |
| 11 | with the following added to such license: |
| 12 | |
| 13 | As a special exception, the copyright holders of this software give |
| 14 | you permission to link this software with independent modules, and |
| 15 | to copy and distribute the resulting executable under terms of your |
| 16 | choice, provided that you also meet, for each linked independent |
| 17 | module, the terms and conditions of the license of that module. |
| 18 | An independent module is a module which is not derived from this |
| 19 | software. The special exception does not apply to any modifications |
| 20 | of the software. |
| 21 | |
| 22 | Not withstanding the above, under no circumstances may you combine |
| 23 | this software in any way with any other Broadcom software provided |
| 24 | under a license other than the GPL, without Broadcom's express prior |
| 25 | written consent. |
| 26 | |
| 27 | :> |
| 28 | */ |
| 29 | |
| 30 | /* |
| 31 | * bcmos_system.h |
| 32 | * Maple System Services |
| 33 | * posix port: simulation |
| 34 | */ |
| 35 | |
| 36 | #ifndef BCMOS_SYSTEM_H_ |
| 37 | #define BCMOS_SYSTEM_H_ |
| 38 | |
| 39 | #include <stdlib.h> |
| 40 | #include <string.h> |
| 41 | #include <strings.h> |
| 42 | #include <stdio.h> |
| 43 | #include <assert.h> |
| 44 | #include <stdint.h> |
| 45 | #include <ctype.h> |
| 46 | #include <stddef.h> |
| 47 | #include <unistd.h> |
| 48 | #include <errno.h> |
| 49 | #include <pthread.h> |
| 50 | #include <semaphore.h> |
| 51 | #include <signal.h> |
| 52 | #include <execinfo.h> |
| 53 | #include <limits.h> |
| 54 | #include <stdarg.h> |
| 55 | #include <sys/select.h> |
| 56 | #include <sys/time.h> |
| 57 | #include <sys/types.h> |
| 58 | #include <sys/socket.h> |
| 59 | #include <netinet/in.h> |
| 60 | |
| 61 | /* posix port has only been tested in linux user space */ |
| 62 | #define LINUX_USER_SPACE |
| 63 | |
| 64 | /* Re-define GNU typeof as ISO __typeof__ */ |
| 65 | #define typeof __typeof__ |
| 66 | |
| 67 | void _bcmos_backtrace(void); |
| 68 | |
| 69 | #define BUG_ON_PRINT(condition, fmt, args...) \ |
| 70 | do \ |
| 71 | { \ |
| 72 | if (condition) \ |
| 73 | { \ |
| 74 | BCMOS_TRACE_ERR(fmt, ##args); \ |
| 75 | assert(!(condition)); \ |
| 76 | } \ |
| 77 | } while (0) |
| 78 | |
| 79 | #define BUG_ON(condition) BUG_ON_PRINT((condition), "BUG in %s %d!\n", __FUNCTION__, __LINE__) |
| 80 | #define BUG() BUG_ON(1) |
| 81 | #define BUG_UNLESS(condition) BUG_ON(!(condition)) |
| 82 | |
| 83 | /* If 'err' is not BCM_ERR_OK, there is a bug in the software - halt and print the specified message */ |
| 84 | #define BUG_ON_ERROR(err, fmt, args...) \ |
| 85 | BUG_ON_PRINT((err) != BCM_ERR_OK, \ |
| 86 | "%s:%d: err=%s (%d): " fmt, \ |
| 87 | __FUNCTION__, __LINE__, bcmos_strerror(err), (err), ##args) |
| 88 | |
| 89 | /* Specify here which system functions are inlined */ |
| 90 | |
| 91 | #define BCMOS_FASTLOCK_INLINE |
| 92 | /* #define BCMOS_SEM_WAIT_INLINE */ |
| 93 | #define BCMOS_SEM_POST_INLINE |
| 94 | #define BCMOS_TRACE_PRINTF |
| 95 | /* #define BCMOS_BYTE_POOL_ALLOC_FREE_INLINE */ |
| 96 | /* #define BCMOS_MALLOC_FREE_INLINE */ |
| 97 | /* #define BCMOS_CALLOC_INLINE */ |
| 98 | #define BCMOS_DMA_ALLOC_FREE_INLINE |
| 99 | #define BCMOS_VIRT_TO_PHYS_INLINE |
| 100 | #define BCMOS_CACHE_INLINE |
| 101 | |
| 102 | #include "bcmos_common.h" |
| 103 | |
| 104 | /* posix-specific stuff */ |
| 105 | |
| 106 | /* |
| 107 | * Synchronization |
| 108 | */ |
| 109 | |
| 110 | /** \addtogroup system_mutex |
| 111 | * @{ |
| 112 | */ |
| 113 | |
| 114 | /** Mutex control block */ |
| 115 | struct bcmos_mutex |
| 116 | { |
| 117 | pthread_mutex_t m; /**< pthread mutex */ |
| 118 | pthread_mutexattr_t attr; /**< pthread mutex attribute */ |
| 119 | }; |
| 120 | |
| 121 | /** @} system_mutex */ |
| 122 | |
| 123 | /** \addtogroup system_fastlock |
| 124 | * @{ |
| 125 | */ |
| 126 | |
| 127 | /** Fast lock control block */ |
| 128 | struct bcmos_fastlock |
| 129 | { |
| 130 | bcmos_mutex m; |
| 131 | }; |
| 132 | |
| 133 | /** Fastlock initializer. Can be used instead of calling bcmos_fastlock_init() */ |
| 134 | #define BCMOS_FASTLOCK_INITIALIZER { { PTHREAD_MUTEX_INITIALIZER } } |
| 135 | |
| 136 | /* Init fastlock */ |
| 137 | static inline void bcmos_fastlock_init(bcmos_fastlock *lock, uint32_t flags) |
| 138 | { |
| 139 | bcmos_mutex_create(&lock->m, 0, NULL); |
| 140 | } |
| 141 | |
| 142 | /* Take fast lock */ |
| 143 | static inline long bcmos_fastlock_lock(bcmos_fastlock *lock) |
| 144 | { |
| 145 | bcmos_mutex_lock(&lock->m); |
| 146 | return 0; |
| 147 | } |
| 148 | |
| 149 | /* Release fast lock */ |
| 150 | static inline void bcmos_fastlock_unlock(bcmos_fastlock *lock, long flags) |
| 151 | { |
| 152 | bcmos_mutex_unlock(&lock->m); |
| 153 | } |
| 154 | |
| 155 | /** @} system_fastlock */ |
| 156 | |
| 157 | /** \addtogroup system_sem |
| 158 | * @{ |
| 159 | */ |
| 160 | |
| 161 | /** Semaphore control block */ |
| 162 | struct bcmos_sem |
| 163 | { |
| 164 | sem_t s; /**< pthread semaphore */ |
| 165 | }; |
| 166 | |
| 167 | /* Increment semaphore counter */ |
| 168 | static inline void bcmos_sem_post(bcmos_sem *sem) |
| 169 | { |
| 170 | sem_post(&sem->s); |
| 171 | } |
| 172 | |
| 173 | /** @} system_sem */ |
| 174 | |
| 175 | /** \addtogroup system_timer |
| 176 | * @{ |
| 177 | */ |
| 178 | |
| 179 | /** System timer */ |
| 180 | struct bcmos_sys_timer |
| 181 | { |
| 182 | timer_t t; /**< librt timer */ |
| 183 | bcmos_sys_timer_handler handler; /**< Timer handler */ |
| 184 | void *data; /**< Parameter to be passed to the handler */ |
| 185 | }; |
| 186 | |
| 187 | /** @} system_timer */ |
| 188 | |
| 189 | /** \addtogroup system_task |
| 190 | * @{ |
| 191 | */ |
| 192 | |
| 193 | /** OS-specific task control block extension */ |
| 194 | typedef struct bcmos_sys_task |
| 195 | { |
| 196 | pthread_t t; /**< posix pthread */ |
| 197 | } bcmos_sys_task; |
| 198 | |
| 199 | /** @} system_task */ |
| 200 | |
| 201 | /** \addtogroup byte_pool |
| 202 | * @{ |
| 203 | */ |
| 204 | |
| 205 | /** Memory pool control block */ |
| 206 | struct bcmos_byte_pool |
| 207 | { |
| 208 | bcmos_byte_pool_parm parm; /**< Pool parameters */ |
| 209 | uint32_t allocated; /**< Number of bytes allocated */ |
| 210 | #ifdef BCMOS_MEM_DEBUG |
| 211 | uint32_t magic; /**< magic number */ |
| 212 | #define BCMOS_BYTE_POOL_VALID (('b'<<24) | ('y' << 16) | ('p' << 8) | 'o') |
| 213 | #define BCMOS_BYTE_POOL_DELETED (('b'<<24) | ('y' << 16) | ('p' << 8) | '~') |
| 214 | #endif |
| 215 | }; |
| 216 | |
| 217 | /** @} */ |
| 218 | |
| 219 | /* Print */ |
| 220 | #define bcmos_sys_vprintf(fmt, args) vprintf(fmt, args) |
| 221 | |
| 222 | /* A few macros to enable linux kernel space compilation */ |
| 223 | #define EXPORT_SYMBOL(_sym) |
| 224 | |
| 225 | /* |
| 226 | * The following low-level functions are mostly for simulation |
| 227 | */ |
| 228 | |
| 229 | /* |
| 230 | * DMA-able memory allocation |
| 231 | */ |
| 232 | |
| 233 | /* Allocate DMA-able memory */ |
| 234 | static inline void *bcmos_dma_alloc(uint8_t device, uint32_t size) |
| 235 | { |
| 236 | return bcmos_alloc(size); |
| 237 | } |
| 238 | |
| 239 | /* Release DMA-able memory */ |
| 240 | static inline void bcmos_dma_free(uint8_t device, void *ptr) |
| 241 | { |
| 242 | bcmos_free(ptr); |
| 243 | } |
| 244 | |
| 245 | /* Convert virtual address to physical address */ |
| 246 | static inline unsigned long bcmos_virt_to_phys(void *va) |
| 247 | { |
| 248 | return (unsigned long)(va); |
| 249 | } |
| 250 | |
| 251 | /* Invalidate address area in data cache. Dirty cache lines content is discarded. */ |
| 252 | static inline void bcmos_dcache_inv(void *start, uint32_t size) |
| 253 | { |
| 254 | } |
| 255 | |
| 256 | /* Flush address area in data cache. Dirty cache lines are committed to memory. */ |
| 257 | static inline void bcmos_dcache_flush(void *start, uint32_t size) |
| 258 | { |
| 259 | } |
| 260 | |
| 261 | /* write barrier */ |
| 262 | static inline void bcmos_barrier(void) |
| 263 | { |
| 264 | } |
| 265 | |
| 266 | static inline void bcm_pci_write32(volatile uint32_t *address, uint32_t value) |
| 267 | { |
| 268 | } |
| 269 | |
| 270 | static inline uint32_t bcm_pci_read32(const volatile uint32_t *address) |
| 271 | { |
| 272 | return -1; |
| 273 | } |
| 274 | |
| 275 | /* Check in-irq status */ |
| 276 | static inline bcmos_bool is_irq_mode(void) |
| 277 | { |
| 278 | return BCMOS_FALSE; |
| 279 | } |
| 280 | |
| 281 | /* Check if interrupts are disabled */ |
| 282 | static inline bcmos_bool is_irq_disabled(void) |
| 283 | { |
| 284 | return BCMOS_FALSE; |
| 285 | } |
| 286 | |
| 287 | /* |
| 288 | * Internal (to OS abstraction) functions for messaging over domain and UDP sockets |
| 289 | */ |
| 290 | #ifdef BCMOS_MSG_QUEUE_DOMAIN_SOCKET |
| 291 | bcmos_errno bcmos_sys_msg_queue_domain_socket_open(bcmos_msg_queue *queue); |
| 292 | #endif |
| 293 | |
| 294 | #ifdef BCMOS_MSG_QUEUE_UDP_SOCKET |
| 295 | bcmos_errno bcmos_sys_msg_queue_udp_socket_open(bcmos_msg_queue *queue); |
| 296 | #endif |
| 297 | |
| 298 | /* 2nd part of OS-independent declarations */ |
| 299 | #include "bcmos_common2.h" |
| 300 | |
| 301 | #endif /* BCMOS_SYSTEM_H_ */ |