sbi_types.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. #undef offsetof
  59. #ifdef __compiler_offsetof
  60. #define offsetof(TYPE, MEMBER) __compiler_offsetof(TYPE,MEMBER)
  61. #else
  62. #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
  63. #endif
  64. #define container_of(ptr, type, member) ({ \
  65. const typeof(((type *)0)->member) * __mptr = (ptr); \
  66. (type *)((char *)__mptr - offsetof(type, member)); })
  67. #define array_size(x) (sizeof(x) / sizeof((x)[0]))
  68. #define MAX(a, b) ((a) > (b) ? (a) : (b))
  69. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  70. #define CLAMP(a, lo, hi) MIN(MAX(a, lo), hi)
  71. #define STR(x) XSTR(x)
  72. #define XSTR(x) #x
  73. #define ROUNDUP(a, b) ((((a)-1) / (b) + 1) * (b))
  74. #define ROUNDDOWN(a, b) ((a) / (b) * (b))
  75. /* clang-format on */
  76. #else
  77. /*
  78. * OPENSBI_EXTERNAL_SBI_TYPES could be defined in CFLAGS for using the
  79. * external definitions of data types and common macros.
  80. * OPENSBI_EXTERNAL_SBI_TYPES is the file name to external header file,
  81. * the external build system should address the additional include
  82. * directory ccordingly.
  83. */
  84. #define XSTR(x) #x
  85. #define STR(x) XSTR(x)
  86. #include STR(OPENSBI_EXTERNAL_SBI_TYPES)
  87. #endif
  88. #endif