1 /* 2 * Copyright (C) 2022 HiSilicon (Shanghai) Technologies CO., LIMITED. 3 * 4 * This program is free software; you can redistribute it and/or 5 * modify it under the terms of the GNU General Public License 6 * as published by the Free Software Foundation; either version 2 7 * of the License, or (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program; if not, write to the Free Software 16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 17 */ 18 19 #ifndef __HI_OSAL_H__ 20 #define __HI_OSAL_H__ 21 22 #define HI_OSAL_VERSION "2.0" 23 24 #include "osal_list.h" 25 26 #define OSAL_GFP_KERNEL 0x24000c0 27 #define OSAL_GFP_ATOMIC 0x2080020 28 #define OSAL_GFP_DMA 0x1 29 30 void *osal_kmalloc(unsigned int module_id, unsigned long size, unsigned int osal_gfp_flag); 31 void osal_kfree(unsigned int module_id, const void *addr); 32 void *osal_vmalloc(unsigned int module_id, unsigned long size); 33 void osal_vfree(unsigned int module_id, const void *addr); 34 35 // atomic api 36 typedef struct osal_atomic_ { 37 void *atomic; 38 } osal_atomic; 39 40 int osal_atomic_init(osal_atomic *atomic); 41 void osal_atomic_destory(osal_atomic *atomic); 42 int osal_atomic_read(osal_atomic *v); 43 void osal_atomic_set(osal_atomic *v, int i); 44 int osal_atomic_inc_return(osal_atomic *v); 45 int osal_atomic_dec_return(osal_atomic *v); 46 47 // semaphore api 48 typedef struct osal_semaphore_ { 49 void *sem; 50 } osal_semaphore; 51 52 int osal_sem_init(osal_semaphore *sem, int val); 53 int osal_sem_down(osal_semaphore *sem); 54 int osal_sem_down_interruptible(osal_semaphore *sem); 55 int osal_sem_trydown(osal_semaphore *sem); 56 void osal_sem_up(osal_semaphore *sem); 57 // notice:must be called when kmod exit, other wise will lead to memory leak; 58 void osal_sem_destory(osal_semaphore *sem); 59 60 // mutex api 61 typedef struct osal_mutex_ { 62 void *mutex; 63 } osal_mutex; 64 65 int osal_mutex_init(osal_mutex *mutex); 66 int osal_mutex_lock(osal_mutex *mutex); 67 int osal_mutex_lock_interruptible(osal_mutex *mutex); 68 int osal_mutex_trylock(osal_mutex *mutex); 69 void osal_mutex_unlock(osal_mutex *mutex); 70 // notice:must be called when kmod exit, other wise will lead to memory leak; 71 void osal_mutex_destory(osal_mutex *mutex); 72 73 // spin lock api 74 typedef struct osal_spinlock_ { 75 void *lock; 76 } osal_spinlock; 77 78 int osal_spin_lock_init(osal_spinlock *lock); 79 void osal_spin_lock(osal_spinlock *lock); 80 int osal_spin_trylock(osal_spinlock *lock); 81 void osal_spin_unlock(osal_spinlock *lock); 82 void osal_spin_lock_irqsave(osal_spinlock *lock, unsigned long *flags); 83 void osal_spin_unlock_irqrestore(osal_spinlock *lock, unsigned long *flags); 84 // notice:must be called when kmod exit, other wise will lead to memory leak; 85 void osal_spin_lock_destory(osal_spinlock *lock); 86 87 // wait api 88 typedef int (*osal_wait_condition_func)(const void *param); 89 90 typedef struct osal_wait_ { 91 void *wait; 92 } osal_wait; 93 #define ERESTARTSYS 512 94 95 int osal_wait_init(osal_wait *wait); 96 int osal_wait_interruptible(osal_wait *wait, osal_wait_condition_func func, const void *param); 97 int osal_wait_uninterruptible(osal_wait *wait, osal_wait_condition_func func, const void *param); 98 int osal_wait_timeout_interruptible(osal_wait *wait, 99 osal_wait_condition_func func, const void *param, unsigned long ms); 100 int osal_wait_timeout_uninterruptible(osal_wait *wait, 101 osal_wait_condition_func func, const void *param, unsigned long ms); 102 void osal_wait_wakeup(osal_wait *wait); // same as wake_up_all 103 void osal_wait_destroy(osal_wait *wait); 104 unsigned long osal_msecs_to_jiffies(const unsigned int m); 105 106 // workqueue api 107 typedef struct osal_workqueue_ { 108 int queue_flag; 109 void *work; 110 void (*handler)(struct osal_workqueue_ *workqueue); 111 } osal_workqueue; 112 typedef void (*osal_workqueue_handler)(osal_workqueue *workqueue); 113 114 int osal_workqueue_init(osal_workqueue *work, osal_workqueue_handler handler); 115 int osal_workqueue_schedule(osal_workqueue *work); 116 void osal_workqueue_destroy(osal_workqueue *work); 117 int osal_workqueue_flush(osal_workqueue *work); 118 119 // timer 120 typedef struct osal_timer_ { 121 void *timer; 122 void (*handler)(unsigned long); 123 unsigned long data; 124 } osal_timer; 125 126 typedef struct osal_timeval_ { 127 long tv_sec; 128 long tv_usec; 129 } osal_timeval; 130 131 int osal_timer_init(osal_timer *timer); 132 int osal_timer_set(osal_timer *timer, unsigned long interval); // ms 133 int osal_timer_del(osal_timer *timer); 134 int osal_timer_destory(osal_timer *timer); 135 136 unsigned long osal_msleep(unsigned int msecs); 137 void osal_msleep_uninterruptible(unsigned int msecs); 138 void osal_udelay(unsigned int usecs); 139 void osal_mdelay(unsigned int msecs); 140 unsigned long long osal_sched_clock(void); 141 unsigned int osal_get_tickcount(void); 142 unsigned long long osal_get_jiffies(void); 143 void osal_get_timeofday(osal_timeval *tv); 144 int osal_time_after(unsigned long a, unsigned long b); // if a after b, return true 145 int osal_time_before(unsigned long a, unsigned long b); 146 int osal_get_pid(void); 147 148 typedef struct osal_rtc_time_ { 149 int tm_sec; 150 int tm_min; 151 int tm_hour; 152 int tm_mday; 153 int tm_mon; 154 int tm_year; 155 int tm_wday; 156 int tm_yday; 157 int tm_isdst; 158 } osal_rtc_time; 159 160 /* Return values for the timer callback function */ 161 typedef enum hiOSAL_HRTIMER_RESTART_E { 162 OSAL_HRTIMER_NORESTART, /* < The timer will not be restarted. */ 163 OSAL_HRTIMER_RESTART /* < The timer must be restarted. */ 164 } OSAL_HRTIMER_RESTART_E; 165 166 /* hrtimer struct */ 167 typedef struct osal_hrtimer { 168 void *timer; 169 OSAL_HRTIMER_RESTART_E (*handler)(void *timer); 170 unsigned long interval; /* Unit ms */ 171 } osal_hrtimer; 172 173 int osal_hrtimer_create(osal_hrtimer *hrtimer); 174 int osal_hrtimer_start(osal_hrtimer *hrtimer); 175 int osal_hrtimer_destory(osal_hrtimer *hrtimer); 176 void osal_rtc_time_to_tm(unsigned long time, osal_rtc_time *tm); 177 void osal_rtc_tm_to_time(osal_rtc_time *tm, unsigned long *time); 178 int osal_rtc_valid_tm(osal_rtc_time *tm); 179 180 // task api 181 typedef enum osal_task_priority_ { 182 OSAL_TASK_PRIORITY_HIGH, /* Highest */ 183 OSAL_TASK_PRIORITY_MIDDLE, 184 OSAL_TASK_PRIORITY_LOW, 185 OSAL_TASK_PRIORITY_MAX, 186 }osal_task_priority; 187 188 typedef struct osal_task_ { 189 void *task; 190 } osal_task; 191 typedef int (*osal_kthread_handler)(void *data); 192 193 osal_task *osal_kthread_create(osal_kthread_handler handler, 194 void *data, 195 const char *name, 196 unsigned int stack_size); // 0 mean default 197 void osal_kthread_set_priority(osal_task *task, osal_task_priority priority); 198 void osal_kthread_set_affinity(osal_task *task, int cpu_mask); 199 int osal_kthread_should_stop(void); 200 void osal_kthread_destroy(osal_task *task, unsigned int stop_flag); 201 202 // fence 203 int osal_fence_create(unsigned int module_id, int *fd, unsigned int private_size); 204 int osal_fence_destroy(unsigned int module_id, int fd); 205 int osal_fence_acquire_private(unsigned int module_id, int fd, void **private); 206 int osal_fence_release_private(unsigned int module_id, int fd, void *private); 207 int osal_fence_signal(int fd); 208 int osal_fence_wait(int fd, unsigned int ms); 209 int osal_fence_trywait(int fd); 210 211 // interrupt api 212 #define OSAL_CPU_0 (1<<1) 213 #define OSAL_CPU_1 (1<<2) 214 #define OSAL_CPU_2 (1<<3) 215 #define OSAL_CPU_3 (1<<4) 216 217 enum osal_irqreturn { 218 OSAL_IRQ_NONE = (0 << 0), 219 OSAL_IRQ_HANDLED = (1 << 0), 220 OSAL_IRQ_WAKE_THREAD = (1 << 1), 221 }; 222 223 typedef int (*osal_irq_handler)(int, void *); 224 int osal_irq_request(unsigned int irq, 225 osal_irq_handler irq_handler, 226 osal_irq_handler thread_handler, 227 const char *name, 228 void *dev); 229 void osal_irq_free(unsigned int irq, void *dev); 230 int osal_irq_set_affinity(unsigned int irq, const char *name, int cpu_mask); 231 void osal_irq_enable(unsigned int irq); 232 void osal_irq_disable(unsigned int irq); 233 int osal_in_interrupt(void); 234 int osal_irq_init(void); 235 void osal_irq_exit(void); 236 237 // tasklet api 238 typedef struct osal_tasklet_ { 239 void *tasklet; 240 void (*handler)(unsigned long data); 241 unsigned long data; 242 } osal_tasklet; 243 typedef void (*osal_tasklet_handler)(unsigned long data); 244 245 int osal_tasklet_init(osal_tasklet *tasklet); 246 int osal_tasklet_schedule(osal_tasklet *tasklet); 247 int osal_tasklet_kill(osal_tasklet *tasklet); 248 int osal_tasklet_update(osal_tasklet *tasklet); 249 250 // addr translate 251 void *osal_ioremap(unsigned long phys_addr, unsigned long size); 252 void *osal_ioremap_nocache(unsigned long phys_addr, unsigned long size); 253 void *osal_ioremap_cached(unsigned long phys_addr, unsigned long size); 254 void osal_iounmap(volatile void *addr); 255 256 void *osal_phys_to_virt(unsigned long addr); 257 unsigned long osal_virt_to_phys(const void *virt_addr); 258 259 void *osal_blockmem_vmap(unsigned long phys_addr, unsigned long size); 260 void osal_blockmem_vunmap(const void *virt_addr); 261 void osal_blockmem_free(unsigned long phys_addr, unsigned long size); 262 263 #define osal_readl(x) (*((volatile unsigned int *)(x))) 264 #define osal_writel(v, x) (*((volatile unsigned int *)(x)) = (v)) 265 266 unsigned long osal_copy_from_user(void *to, const void *from, unsigned long n); 267 unsigned long osal_copy_to_user(void *to, const void *from, unsigned long n); 268 269 #define OSAL_VERIFY_READ 0 270 #define OSAL_VERIFY_WRITE 1 271 int osal_access_ok(int type, const void *addr, unsigned long size); 272 273 // cache 274 void osal_flush_dcache_area(const void *kvirt, unsigned long phys_addr, unsigned long size); 275 276 // math 277 unsigned long long osal_div_u64(unsigned long long dividend, unsigned int divisor); 278 long long osal_div_s64(long long dividend, int divisor); 279 unsigned long long osal_div64_u64(unsigned long long dividend, unsigned long long divisor); 280 long long osal_div64_s64(long long dividend, long long divisor); 281 unsigned long long osal_div_u64_rem(unsigned long long dividend, unsigned int divisor); 282 long long osal_div_s64_rem(long long dividend, int divisor); 283 unsigned long long osal_div64_u64_rem(unsigned long long dividend, unsigned long long divisor); 284 285 #define osal_max(x, y) ({ \ 286 __typeof__(x) _max1 = (x); \ 287 __typeof__(y) _max2 = (y); \ 288 (void) (&_max1 == &_max2); \ 289 _max1 > _max2 ? _max1 : _max2; }) 290 291 #define osal_min(x, y) ({ \ 292 __typeof__(x) _min1 = (x); \ 293 __typeof__(y) _min2 = (y); \ 294 (void) (&_min1 == &_min2); \ 295 _min1 < _min2 ? _min1 : _min2; }) 296 297 #define osal_abs(x) ({ \ 298 long ret; \ 299 if (sizeof(x) == sizeof(long)) { \ 300 long __x = (x); \ 301 ret = (__x < 0) ? -__x : __x; \ 302 } else { \ 303 int __x = (x); \ 304 ret = (__x < 0) ? - __x : __x; \ 305 } \ 306 ret; \ 307 }) 308 309 // barrier 310 void osal_mb(void); 311 void osal_rmb(void); 312 void osal_wmb(void); 313 void osal_smp_mb(void); 314 void osal_smp_rmb(void); 315 void osal_smp_wmb(void); 316 void osal_isb(void); 317 void osal_dsb(void); 318 void osal_dmb(void); 319 320 // proc 321 #define OSAL_PROC_NAME_LEN 16 322 #define PROC_CMD_ALL_LENGTH_MAX 128 323 #define PROC_CMD_NUM_MAX 16 324 #define PROC_CMD_SINGEL_LENGTH_MAX 16 325 #define OSAL_BUILDVARIANT_NONE 0x0 326 #define OSAL_BUILDVARIANT_USER 0x1 327 #define OSAL_BUILDVARIANT_ENG 0x2 328 typedef struct osal_proc_cmd_ { 329 char name[OSAL_PROC_NAME_LEN]; 330 int (*handler)(unsigned int argc, char (*argv)[PROC_CMD_SINGEL_LENGTH_MAX], void *private); 331 } osal_proc_cmd; 332 333 typedef struct osal_proc_entry_ { 334 char name[OSAL_PROC_NAME_LEN]; 335 int (*open)(void *private); 336 int (*read)(void *seqfile, void *private); 337 int (*write)(struct osal_proc_entry_ *entry, const char *buf, int count, long long *); 338 osal_proc_cmd *cmd_list; 339 unsigned int cmd_cnt; 340 void *private; 341 void *proc_dir_entry; 342 void *seqfile; 343 struct osal_list_head node; 344 } osal_proc_entry; 345 346 osal_proc_entry *osal_proc_add(const char *name, unsigned long name_size); 347 void osal_proc_remove(const char *name, unsigned long name_size); 348 int osal_proc_print(void *seqfile, const char *fmt, ...) __attribute__((format(printf, 2, 3))); 349 int osal_printk(const char *fmt, ...); 350 int osal_get_buildvariant(void); 351 352 // device api 353 #ifndef _IOC_TYPECHECK 354 #ifdef __LITEOS__ 355 #include "asm/ioctl.h" 356 #else 357 #include "osal_ioctl.h" 358 #endif 359 #endif 360 361 #define OSAL_POLLIN 0x0001 362 #define OSAL_POLLPRI 0x0002 363 #define OSAL_POLLOUT 0x0004 364 #define OSAL_POLLERR 0x0008 365 #define OSAL_POLLHUP 0x0010 366 #define OSAL_POLLNVAL 0x0020 367 #define OSAL_POLLRDNORM 0x0040 368 #define OSAL_POLLRDBAND 0x0080 369 #define OSAL_POLLWRNORM 0x0100 370 371 #define OSAL_SEEK_SET 0 372 #define OSAL_SEEK_CUR 1 373 #define OSAL_SEEK_END 2 374 375 typedef struct osal_poll_ { 376 void *poll_table; 377 void *data; 378 } osal_poll; 379 380 typedef struct osal_ioctl_cmd_ { 381 unsigned int cmd; 382 int (*handler)(unsigned int cmd, void *arg, void *private_data); 383 } osal_ioctl_cmd; 384 385 typedef struct osal_vm_ { 386 void *vm; 387 } osal_vm; 388 389 typedef struct osal_fileops_ { 390 int (*open)(void *private_data); 391 int (*read)(char *buf, int size, long *offset, void *private_data); 392 int (*write)(const char *buf, int size, long *offset, void *private_data); 393 long (*llseek)(long offset, int whence, void *private_data); 394 int (*release)(void *private_data); 395 long (*unlocked_ioctl)(unsigned int cmd, unsigned long arg, void *private_data); 396 int (*poll)(osal_poll *osal_poll, void *private_data); 397 int (*mmap)(osal_vm *vm, unsigned long start, unsigned long end, unsigned long vm_pgoff, void *private_data); 398 #ifdef CONFIG_COMPAT 399 long (*compat_ioctl)(unsigned int cmd, unsigned long arg, void *private_data); 400 #endif 401 osal_ioctl_cmd *cmd_list; 402 unsigned int cmd_cnt; 403 } osal_fileops; 404 405 typedef struct osal_pmops_ { 406 int (*pm_suspend)(void *private_data); 407 int (*pm_resume)(void *private_data); 408 int (*pm_lowpower_enter)(void *private_data); 409 int (*pm_lowpower_exit)(void *private_data); 410 int (*pm_poweroff)(void *private_data); 411 void *private_data; 412 } osal_pmops; 413 414 #define OSAL_DEV_NAME_LEN 16 415 typedef struct osal_dev_ { 416 struct osal_list_head list; 417 char name[OSAL_DEV_NAME_LEN]; 418 int minor; 419 unsigned int count; 420 osal_fileops *fops; 421 osal_pmops *pmops; 422 void *dev; 423 } osal_dev; 424 425 #define OSAL_NOCACHE 0 426 #define OSAL_CACHE 1 427 428 int osal_dev_register(osal_dev *dev); 429 void osal_dev_unregister(osal_dev *dev); 430 void osal_poll_wait(osal_poll *table, osal_wait *wait); 431 int osal_remap_pfn_range(osal_vm *vm, unsigned long addr, unsigned long pfn, unsigned long size, unsigned int cached); 432 int osal_try_to_freeze(void); 433 int osal_set_freezable(void); 434 extern int osal_irq_mmap(osal_vm *vm, void *ptr, unsigned long start, unsigned long sz); 435 436 437 // export function 438 int osal_exportfunc_register(unsigned int module_id, const char *name, void *func); 439 int osal_exportfunc_unregister(unsigned int module_id); 440 int osal_exportfunc_get(unsigned int module_id, void **func); 441 442 443 // kfile 444 #define OSAL_O_ACCMODE 00000003 445 #define OSAL_O_RDONLY 00000000 446 #define OSAL_O_WRONLY 00000001 447 #define OSAL_O_RDWR 00000002 448 #define OSAL_O_CREAT 00000100 449 #define OSAL_O_APPEND 00002000 450 451 void *osal_klib_fopen(const char *filename, int flags, int mode); 452 void osal_klib_fclose(void *filp); 453 int osal_klib_fwrite(const char *buf, unsigned long size, void *filp); 454 int osal_klib_fread(char *buf, unsigned long size, void *filp); 455 int osal_klib_user_fwrite(const char *buf, unsigned long size, void *filp); 456 int osal_klib_user_fread(char *buf, unsigned long size, void *filp); 457 void osal_klib_fsync(void *filp); 458 int osal_klib_fseek(long long offset, int whence, void *filp); 459 int osal_klib_get_store_path(char *path, unsigned int path_size); 460 void osal_klib_set_store_path(char *path); 461 462 463 // string 464 int osal_memncmp(const void *buf1, unsigned long size1, 465 const void *buf2, unsigned long size2); 466 int osal_strncmp(const char *str1, unsigned long size1, 467 const char *str2, unsigned long size2); 468 int osal_strncasecmp(const char *str1, unsigned long size1, 469 const char *str2, unsigned long size2); 470 471 #define OSAL_BASE_DEC 10 472 #define OSAL_BASE_HEX 16 473 474 long osal_strtol(const char *, char **end, unsigned int base); 475 unsigned long osal_strtoul(const char *, char **end, unsigned int base); 476 void osal_get_random_bytes(void *buf, int nbytes); 477 478 479 // debug 480 void osal_dump_stack(void); 481 482 // gpio 483 int osal_gpio_get_direction(unsigned int gpio); 484 int osal_gpio_set_direction(unsigned int gpio, unsigned int dir); 485 void osal_gpio_set_value(unsigned int gpio, int value); 486 int osal_gpio_get_value(unsigned int gpio); 487 int osal_gpio_irq_request(unsigned int gpio, osal_irq_handler handler, 488 unsigned long flags, const char *name, void *dev); 489 void osal_gpio_irq_free(unsigned int gpio, void *dev); 490 491 #endif /* __HI_OSAL_H__ */ 492 493