intel/skylake: Implement native Cache-as-RAM (CAR)
[coreboot.git] / src / include / endian.h
blob08636f3da61e0aff86b86cce614c8451ff5db4ca
1 /*
2 * Copyright (C) 2013 The Chromium OS Authors. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of
7 * the License, or (at your option) any later version.
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.
15 #ifndef _ENDIAN_H_
16 #define _ENDIAN_H_
18 #include <arch/byteorder.h>
19 #include <stdint.h>
20 #include <swab.h>
22 #if defined(__LITTLE_ENDIAN)
23 #define cpu_to_le64(x) ((uint64_t)(x))
24 #define le64_to_cpu(x) ((uint64_t)(x))
25 #define cpu_to_le32(x) ((uint32_t)(x))
26 #define le32_to_cpu(x) ((uint32_t)(x))
27 #define cpu_to_le16(x) ((uint16_t)(x))
28 #define le16_to_cpu(x) ((uint16_t)(x))
29 #define cpu_to_be64(x) swab64(x)
30 #define be64_to_cpu(x) swab64(x)
31 #define cpu_to_be32(x) swab32(x)
32 #define be32_to_cpu(x) swab32(x)
33 #define cpu_to_be16(x) swab16(x)
34 #define be16_to_cpu(x) swab16(x)
35 #elif defined(__BIG_ENDIAN)
36 #define cpu_to_le64(x) swab64(x)
37 #define le64_to_cpu(x) swab64(x)
38 #define cpu_to_le32(x) swab32(x)
39 #define le32_to_cpu(x) swab32(x)
40 #define cpu_to_le16(x) swab16(x)
41 #define le16_to_cpu(x) swab16(x)
42 #define cpu_to_be64(x) ((uint64_t)(x))
43 #define be64_to_cpu(x) ((uint64_t)(x))
44 #define cpu_to_be32(x) ((uint32_t)(x))
45 #define be32_to_cpu(x) ((uint32_t)(x))
46 #define cpu_to_be16(x) ((uint16_t)(x))
47 #define be16_to_cpu(x) ((uint16_t)(x))
48 #else
49 #error "<arch/byteorder.h> must #define __LITTLE_ENDIAN or __BIG_ENDIAN"
50 #endif
52 #define ntohll(x) be64_to_cpu(x)
53 #define htonll(x) cpu_to_be64(x)
54 #define ntohl(x) be32_to_cpu(x)
55 #define htonl(x) cpu_to_be32(x)
57 #define __clrsetbits(endian, bits, addr, clear, set) \
58 write##bits(addr, cpu_to_##endian##bits((endian##bits##_to_cpu( \
59 read##bits(addr)) & ~((uint##bits##_t)(clear))) | (set)))
61 #define clrbits_le64(addr, clear) __clrsetbits(le, 64, addr, clear, 0)
62 #define clrbits_be64(addr, clear) __clrsetbits(be, 64, addr, clear, 0)
63 #define clrbits_le32(addr, clear) __clrsetbits(le, 32, addr, clear, 0)
64 #define clrbits_be32(addr, clear) __clrsetbits(be, 32, addr, clear, 0)
65 #define clrbits_le16(addr, clear) __clrsetbits(le, 16, addr, clear, 0)
66 #define clrbits_be16(addr, clear) __clrsetbits(be, 16, addr, clear, 0)
68 #define setbits_le64(addr, set) __clrsetbits(le, 64, addr, 0, set)
69 #define setbits_be64(addr, set) __clrsetbits(be, 64, addr, 0, set)
70 #define setbits_le32(addr, set) __clrsetbits(le, 32, addr, 0, set)
71 #define setbits_be32(addr, set) __clrsetbits(be, 32, addr, 0, set)
72 #define setbits_le16(addr, set) __clrsetbits(le, 16, addr, 0, set)
73 #define setbits_be16(addr, set) __clrsetbits(be, 16, addr, 0, set)
75 #define clrsetbits_le64(addr, clear, set) __clrsetbits(le, 64, addr, clear, set)
76 #define clrsetbits_be64(addr, clear, set) __clrsetbits(be, 64, addr, clear, set)
77 #define clrsetbits_le32(addr, clear, set) __clrsetbits(le, 32, addr, clear, set)
78 #define clrsetbits_be32(addr, clear, set) __clrsetbits(be, 32, addr, clear, set)
79 #define clrsetbits_le16(addr, clear, set) __clrsetbits(le, 16, addr, clear, set)
80 #define clrsetbits_be16(addr, clear, set) __clrsetbits(be, 16, addr, clear, set)
82 #define clrsetbits_8(addr, clear, set) \
83 write8(addr, (read8(addr) & ~(clear)) | (set))
84 #define clrbits_8(addr, clear) clrsetbits_8(addr, clear, 0)
85 #define setbits_8(addr, set) setbits_8(addr, 0, set)
87 #ifndef __ROMCC__
89 * Portable (API) endian support that can be used in code that is shared
90 * with userspace (man 3 endian) tools.
92 static inline uint16_t htobe16(uint16_t host_16bits)
94 return cpu_to_be16(host_16bits);
97 static inline uint16_t htole16(uint16_t host_16bits)
99 return cpu_to_le16(host_16bits);
102 static inline uint16_t be16toh(uint16_t big_endian_16bits)
104 return be16_to_cpu(big_endian_16bits);
107 static inline uint16_t le16toh(uint16_t little_endian_16bits)
109 return le16_to_cpu(little_endian_16bits);
112 static inline uint32_t htobe32(uint32_t host_32bits)
114 return cpu_to_be32(host_32bits);
117 static inline uint32_t htole32(uint32_t host_32bits)
119 return cpu_to_le32(host_32bits);
122 static inline uint32_t be32toh(uint32_t big_endian_32bits)
124 return be32_to_cpu(big_endian_32bits);
127 static inline uint32_t le32toh(uint32_t little_endian_32bits)
129 return le32_to_cpu(little_endian_32bits);
132 static inline uint64_t htobe64(uint64_t host_64bits)
134 return cpu_to_be64(host_64bits);
137 static inline uint64_t htole64(uint64_t host_64bits)
139 return cpu_to_le64(host_64bits);
142 static inline uint64_t be64toh(uint64_t big_endian_64bits)
144 return be64_to_cpu(big_endian_64bits);
147 static inline uint64_t le64toh(uint64_t little_endian_64bits)
149 return le64_to_cpu(little_endian_64bits);
151 #endif
153 #endif