1 /*
2 * Copyright (C) 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #pragma once
18
19 #include <assert.h>
20 #include <string>
21
22 namespace android {
23 namespace hardware {
24
25 enum MQFlavor : uint32_t {
26 /*
27 * kSynchronizedReadWrite represents the wait-free synchronized flavor of the
28 * FMQ. It is intended to be have a single reader and single writer.
29 * Attempts to overflow/underflow returns a failure.
30 */
31 kSynchronizedReadWrite = 0x01,
32 /*
33 * kUnsynchronizedWrite represents the flavor of FMQ where writes always
34 * succeed. This flavor allows one writer and many readers. A read operation
35 * can detect an overwrite and reset the read counter.
36 */
37 kUnsynchronizedWrite = 0x02
38 };
39
40 struct GrantorDescriptor {
41 uint32_t flags __attribute__((aligned(4)));
42 uint32_t fdIndex __attribute__((aligned(4)));
43 uint32_t offset __attribute__((aligned(4)));
44 uint64_t extent __attribute__((aligned(8)));
45 };
46
47 static_assert(offsetof(GrantorDescriptor, flags) == 0, "wrong offset");
48 static_assert(offsetof(GrantorDescriptor, fdIndex) == 4, "wrong offset");
49 static_assert(offsetof(GrantorDescriptor, offset) == 8, "wrong offset");
50 static_assert(offsetof(GrantorDescriptor, extent) == 16, "wrong offset");
51 static_assert(sizeof(GrantorDescriptor) == 24, "wrong size");
52 static_assert(__alignof(GrantorDescriptor) == 8, "wrong alignment");
53
54 namespace details {
55
56 void logError(const std::string& message);
57 void errorWriteLog(int tag, const char* message);
58
59 typedef uint64_t RingBufferPosition;
60 enum GrantorType : int { READPTRPOS = 0, WRITEPTRPOS, DATAPTRPOS, EVFLAGWORDPOS };
61 /*
62 * There should at least be GrantorDescriptors for the read counter, write
63 * counter and data buffer. A GrantorDescriptor for an EventFlag word is
64 * not required if there is no need for blocking FMQ operations.
65 */
66 static constexpr int32_t kMinGrantorCount = DATAPTRPOS + 1;
67
68 /*
69 * Minimum number of GrantorDescriptors required if EventFlag support is
70 * needed for blocking FMQ operations.
71 */
72 static constexpr int32_t kMinGrantorCountForEvFlagSupport = EVFLAGWORDPOS + 1;
73
alignToWordBoundary(size_t length)74 static inline size_t alignToWordBoundary(size_t length) {
75 constexpr size_t kAlignmentSize = 64;
76 if (kAlignmentSize % __WORDSIZE != 0) {
77 #ifdef __BIONIC__
78 __assert(__FILE__, __LINE__, "Incompatible word size");
79 #endif
80 }
81
82 /*
83 * Check if alignment to word boundary would cause an overflow.
84 */
85 if (length > SIZE_MAX - kAlignmentSize / 8 + 1) {
86 #ifdef __BIONIC__
87 __assert(__FILE__, __LINE__, "Queue size too large");
88 #endif
89 }
90
91 return (length + kAlignmentSize / 8 - 1) & ~(kAlignmentSize / 8 - 1U);
92 }
93
isAlignedToWordBoundary(size_t offset)94 static inline size_t isAlignedToWordBoundary(size_t offset) {
95 constexpr size_t kAlignmentSize = 64;
96 return (offset & (kAlignmentSize / 8 - 1)) == 0;
97 }
98
99 } // namespace details
100 } // namespace hardware
101 } // namespace android
102