blob: 9e5d93ec304119023789da8d813d027a22b083e6 [file] [log] [blame]
Jens Wiklander60f1ba72023-05-22 14:22:38 +02001/* SPDX-License-Identifier: GPL-2.0 */
Mike Frysinger3adc1fd2010-01-21 05:01:14 -05002#ifndef _GENERIC_UNALIGNED_H
3#define _GENERIC_UNALIGNED_H
4
5#include <asm/byteorder.h>
6
Jens Wiklander60f1ba72023-05-22 14:22:38 +02007#define __get_unaligned_t(type, ptr) ({ \
8 const struct { type x; } __packed * __pptr = (typeof(__pptr))(ptr); \
9 __pptr->x; \
10})
Mike Frysinger3adc1fd2010-01-21 05:01:14 -050011
Jens Wiklander60f1ba72023-05-22 14:22:38 +020012#define __put_unaligned_t(type, val, ptr) do { \
13 struct { type x; } __packed * __pptr = (typeof(__pptr))(ptr); \
14 __pptr->x = (val); \
15} while (0)
16
17#define get_unaligned(ptr) __get_unaligned_t(typeof(*(ptr)), (ptr))
18#define put_unaligned(val, ptr) __put_unaligned_t(typeof(*(ptr)), (val), (ptr))
19
20static inline u16 get_unaligned_le16(const void *p)
21{
22 return le16_to_cpu(__get_unaligned_t(__le16, p));
23}
24
25static inline u32 get_unaligned_le32(const void *p)
26{
27 return le32_to_cpu(__get_unaligned_t(__le32, p));
28}
29
30static inline u64 get_unaligned_le64(const void *p)
31{
32 return le64_to_cpu(__get_unaligned_t(__le64, p));
33}
34
35static inline void put_unaligned_le16(u16 val, void *p)
36{
37 __put_unaligned_t(__le16, cpu_to_le16(val), p);
38}
39
40static inline void put_unaligned_le32(u32 val, void *p)
41{
42 __put_unaligned_t(__le32, cpu_to_le32(val), p);
43}
44
45static inline void put_unaligned_le64(u64 val, void *p)
46{
47 __put_unaligned_t(__le64, cpu_to_le64(val), p);
48}
49
50static inline u16 get_unaligned_be16(const void *p)
51{
52 return be16_to_cpu(__get_unaligned_t(__be16, p));
53}
54
55static inline u32 get_unaligned_be32(const void *p)
56{
57 return be32_to_cpu(__get_unaligned_t(__be32, p));
58}
59
60static inline u64 get_unaligned_be64(const void *p)
61{
62 return be64_to_cpu(__get_unaligned_t(__be64, p));
63}
64
65static inline void put_unaligned_be16(u16 val, void *p)
66{
67 __put_unaligned_t(__be16, cpu_to_be16(val), p);
68}
69
70static inline void put_unaligned_be32(u32 val, void *p)
71{
72 __put_unaligned_t(__be32, cpu_to_be32(val), p);
73}
74
75static inline void put_unaligned_be64(u64 val, void *p)
76{
77 __put_unaligned_t(__be64, cpu_to_be64(val), p);
78}
Mike Frysinger3adc1fd2010-01-21 05:01:14 -050079
Heinrich Schuchardtc3b11de2018-04-03 21:59:32 +020080/* Allow unaligned memory access */
81void allow_unaligned(void);
82
Mike Frysinger3adc1fd2010-01-21 05:01:14 -050083#endif