blob: 0d18d4c75085d29bfc163358b2e22acd1521e822 [file] [log] [blame]
Devin Moore133cb5e2020-07-07 16:31:22 -07001/*
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>
Devin Moorecfb59852020-08-25 15:47:19 -070020#include <string>
Devin Moore133cb5e2020-07-07 16:31:22 -070021
22namespace android {
23namespace hardware {
24
25enum 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
Devin Moore7c04cfe2020-08-17 14:37:13 -070040struct 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
47static_assert(offsetof(GrantorDescriptor, flags) == 0, "wrong offset");
48static_assert(offsetof(GrantorDescriptor, fdIndex) == 4, "wrong offset");
49static_assert(offsetof(GrantorDescriptor, offset) == 8, "wrong offset");
50static_assert(offsetof(GrantorDescriptor, extent) == 16, "wrong offset");
51static_assert(sizeof(GrantorDescriptor) == 24, "wrong size");
52static_assert(__alignof(GrantorDescriptor) == 8, "wrong alignment");
53
Devin Moore133cb5e2020-07-07 16:31:22 -070054namespace details {
55
Devin Moorecfb59852020-08-25 15:47:19 -070056void logError(const std::string& message);
Devin Moore2741a812021-04-29 16:22:51 -070057void errorWriteLog(int tag, const char* message);
Devin Moorecfb59852020-08-25 15:47:19 -070058
Devin Moore133cb5e2020-07-07 16:31:22 -070059typedef uint64_t RingBufferPosition;
60enum 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 */
66static 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 */
72static constexpr int32_t kMinGrantorCountForEvFlagSupport = EVFLAGWORDPOS + 1;
73
74static 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
94static 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