]> git.proxmox.com Git - ceph.git/blob - ceph/src/spdk/include/spdk/mmio.h
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / spdk / include / spdk / mmio.h
1 /*-
2 * BSD LICENSE
3 *
4 * Copyright (c) Intel Corporation.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 /** \file
35 * Memory-mapped I/O utility functions
36 */
37
38 #ifndef SPDK_MMIO_H
39 #define SPDK_MMIO_H
40
41 #include "spdk/stdinc.h"
42
43 #ifdef __cplusplus
44 extern "C" {
45 #endif
46
47 #include "spdk/barrier.h"
48
49 #ifdef __x86_64__
50 #define SPDK_MMIO_64BIT 1 /* Can do atomic 64-bit memory read/write (over PCIe) */
51 #else
52 #define SPDK_MMIO_64BIT 0
53 #endif
54
55 static inline uint8_t
56 spdk_mmio_read_1(const volatile uint8_t *addr)
57 {
58 spdk_compiler_barrier();
59 return *addr;
60 }
61
62 static inline void
63 spdk_mmio_write_1(volatile uint8_t *addr, uint8_t val)
64 {
65 spdk_compiler_barrier();
66 *addr = val;
67 }
68
69 static inline uint16_t
70 spdk_mmio_read_2(const volatile uint16_t *addr)
71 {
72 spdk_compiler_barrier();
73 return *addr;
74 }
75
76 static inline void
77 spdk_mmio_write_2(volatile uint16_t *addr, uint16_t val)
78 {
79 spdk_compiler_barrier();
80 *addr = val;
81 }
82
83 static inline uint32_t
84 spdk_mmio_read_4(const volatile uint32_t *addr)
85 {
86 spdk_compiler_barrier();
87 return *addr;
88 }
89
90 static inline void
91 spdk_mmio_write_4(volatile uint32_t *addr, uint32_t val)
92 {
93 spdk_compiler_barrier();
94 *addr = val;
95 }
96
97 static inline uint64_t
98 spdk_mmio_read_8(volatile uint64_t *addr)
99 {
100 uint64_t val;
101 volatile uint32_t *addr32 = (volatile uint32_t *)addr;
102
103 spdk_compiler_barrier();
104
105 if (SPDK_MMIO_64BIT) {
106 val = *addr;
107 } else {
108 /*
109 * Read lower 4 bytes before upper 4 bytes.
110 * This particular order is required by I/OAT.
111 * If the other order is required, use a pair of spdk_mmio_read_4() calls.
112 */
113 val = addr32[0];
114 val |= (uint64_t)addr32[1] << 32;
115 }
116
117 return val;
118 }
119
120 static inline void
121 spdk_mmio_write_8(volatile uint64_t *addr, uint64_t val)
122 {
123 volatile uint32_t *addr32 = (volatile uint32_t *)addr;
124
125 spdk_compiler_barrier();
126
127 if (SPDK_MMIO_64BIT) {
128 *addr = val;
129 } else {
130 addr32[0] = (uint32_t)val;
131 addr32[1] = (uint32_t)(val >> 32);
132 }
133 }
134
135 #ifdef __cplusplus
136 }
137 #endif
138
139 #endif