-
Notifications
You must be signed in to change notification settings - Fork 720
Expand file tree
/
Copy pathcommon.h
More file actions
130 lines (105 loc) · 2.98 KB
/
Copy pathcommon.h
File metadata and controls
130 lines (105 loc) · 2.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/* SPDX-License-Identifier: GPL-2.0-or-later */
#pragma once
#include <fcntl.h>
#include <string.h>
#include <stdbool.h>
#if defined(_WIN32)
#include <direct.h>
#include <io.h>
#include <sysinfoapi.h>
#endif
#include "ccan/endian/endian.h"
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
#define min(x, y) ((x) > (y) ? (y) : (x))
#define max(x, y) ((x) > (y) ? (x) : (y))
#ifdef __packed
#else /* __packed */
#define __packed __attribute__((__packed__))
#endif /* __packed */
/*
* Some common functionality is implemented or named differently on Windows.
* Provide compatibility wrappers.
*/
#if defined(_WIN32)
#define mkdir(path, mode) _mkdir(path)
#define fsync _commit
static inline int getpagesize(void)
{
SYSTEM_INFO si;
GetSystemInfo(&si);
return si.dwPageSize;
}
#endif
/*
* Open file for reading or writing raw data with no line-ending translations
* or other interpretation or decoding.
*/
#ifdef O_BINARY
#define nvme_open_rawdata(path, flags, ...) open((path), (flags) | O_BINARY, ##__VA_ARGS__)
#else
#define nvme_open_rawdata(path, flags, ...) open((path), (flags), ##__VA_ARGS__)
#endif
/*
* VMs on arm64 can only use a subset of instructions for MMIO that provide
* the hypervisor with a complete instruction decode. Provide assembly MMIO
* accessors to prevent the compiler from using a possibly unsupported
* instruction.
*
* See kernel commit c726200dd106 ("KVM: arm/arm64: Allow reporting non-ISV
* data aborts to userspace") for more details.
*/
#if defined(__aarch64__)
static inline leint32_t __raw_readl(const volatile leint32_t *addr)
{
leint32_t val;
asm volatile("ldr %w0, %1" : "=r" (val) : "Qo" (*addr));
return val;
}
static inline void __raw_writel(volatile leint32_t *addr, leint32_t val)
{
asm volatile("str %w0, %1" : : "r" (val), "Qo" (*addr));
}
static inline void __raw_writeq(volatile leint64_t *addr, leint64_t val)
{
asm volatile("str %0, %1" : : "r" (val), "Qo" (*addr));
}
#else
static inline leint32_t __raw_readl(volatile leint32_t *addr)
{
return *addr;
}
static inline void __raw_writel(volatile leint32_t *addr, leint32_t val)
{
*addr = val;
}
static inline void __raw_writeq(volatile leint64_t *addr, leint64_t val)
{
*addr = val;
}
#endif
static inline uint32_t mmio_read32(void *addr)
{
return le32_to_cpu(__raw_readl(addr));
}
/* Access 64-bit registers as 2 32-bit; Some devices fail 64-bit MMIO. */
static inline uint64_t mmio_read64(void *addr)
{
uint32_t low, high;
low = le32_to_cpu(__raw_readl(addr));
high = le32_to_cpu(__raw_readl(addr + sizeof(leint32_t)));
return ((uint64_t)high << 32) | low;
}
static inline void mmio_write32(void *addr, uint32_t value)
{
__raw_writel(addr, cpu_to_le32(value));
}
/* Access 64-bit registers as 2 32-bit if write32 flag set; Some devices fail 64-bit MMIO. */
static inline void mmio_write64(void *addr, uint64_t value, bool write32)
{
if (write32) {
mmio_write32(addr, value);
mmio_write32((uint32_t *)addr + 1, value >> 32);
return;
}
__raw_writeq(addr, cpu_to_le64(value));
}