sbi_types.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * SPDX-License-Identifier: BSD-2-Clause
  3. *
  4. * Copyright (c) 2019 Western Digital Corporation or its affiliates.
  5. *
  6. * Authors:
  7. * Anup Patel <anup.patel@wdc.com>
  8. */
  9. #ifndef __SBI_TYPES_H__
  10. #define __SBI_TYPES_H__
  11. #ifndef OPENSBI_EXTERNAL_SBI_TYPES
  12. /* clang-format off */
  13. typedef char s8;
  14. typedef unsigned char u8;
  15. typedef unsigned char uint8_t;
  16. typedef short s16;
  17. typedef unsigned short u16;
  18. typedef short int16_t;
  19. typedef unsigned short uint16_t;
  20. typedef int s32;
  21. typedef unsigned int u32;
  22. typedef int int32_t;
  23. typedef unsigned int uint32_t;
  24. #if __riscv_xlen == 64
  25. typedef long s64;
  26. typedef unsigned long u64;
  27. typedef long int64_t;
  28. typedef unsigned long uint64_t;
  29. #define PRILX "016lx"
  30. #elif __riscv_xlen == 32
  31. typedef long long s64;
  32. typedef unsigned long long u64;
  33. typedef long long int64_t;
  34. typedef unsigned long long uint64_t;
  35. #define PRILX "08lx"
  36. #else
  37. #error "Unexpected __riscv_xlen"
  38. #endif
  39. typedef int bool;
  40. typedef unsigned long ulong;
  41. typedef unsigned long uintptr_t;
  42. typedef unsigned long size_t;
  43. typedef long ssize_t;
  44. typedef unsigned long virtual_addr_t;
  45. typedef unsigned long virtual_size_t;
  46. typedef unsigned long physical_addr_t;
  47. typedef unsigned long physical_size_t;
  48. #define TRUE 1
  49. #define FALSE 0
  50. #define true TRUE
  51. #define false FALSE
  52. #define NULL ((void *)0)
  53. #define __packed __attribute__((packed))
  54. #define __noreturn __attribute__((noreturn))
  55. #define __aligned(x) __attribute__((aligned(x)))
  56. #define likely(x) __builtin_expect((x), 1)
  57. #define unlikely(x) __builtin_expect((x), 0)
  58. #ifndef __has_builtin
  59. #define __has_builtin(...) 0
  60. #endif
  61. #undef offsetof
  62. #if __has_builtin(__builtin_offsetof)
  63. #define offsetof(TYPE, MEMBER) __builtin_offsetof(TYPE,MEMBER)
  64. #elif defined(__compiler_offsetof)
  65. #define offsetof(TYPE, MEMBER) __compiler_offsetof(TYPE,MEMBER)
  66. #else
  67. #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
  68. #endif
  69. #define container_of(ptr, type, member) ({ \
  70. const typeof(((type *)0)->member) * __mptr = (ptr); \
  71. (type *)((char *)__mptr - offsetof(type, member)); })
  72. #define array_size(x) (sizeof(x) / sizeof((x)[0]))
  73. #define MAX(a, b) ((a) > (b) ? (a) : (b))
  74. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  75. #define CLAMP(a, lo, hi) MIN(MAX(a, lo), hi)
  76. #define STR(x) XSTR(x)
  77. #define XSTR(x) #x
  78. #define ROUNDUP(a, b) ((((a)-1) / (b) + 1) * (b))
  79. #define ROUNDDOWN(a, b) ((a) / (b) * (b))
  80. /* clang-format on */
  81. #else
  82. /*
  83. * OPENSBI_EXTERNAL_SBI_TYPES could be defined in CFLAGS for using the
  84. * external definitions of data types and common macros.
  85. * OPENSBI_EXTERNAL_SBI_TYPES is the file name to external header file,
  86. * the external build system should address the additional include
  87. * directory ccordingly.
  88. */
  89. #define XSTR(x) #x
  90. #define STR(x) XSTR(x)
  91. #include STR(OPENSBI_EXTERNAL_SBI_TYPES)
  92. #endif
  93. #endif