blob: 0f61cb50e94910e9e9726ade5fdf2246d3dbe9f9 [file] [log] [blame]
Andreas Huber0a451282016-08-30 11:27:24 -07001/*
2 * Copyright (C) 2016 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#ifndef _FMSGQ_DESCRIPTOR_H
18#define _FMSGQ_DESCRIPTOR_H
19
Steven Moreland141a4d32017-04-28 17:05:50 -070020#include <unistd.h>
21
Andreas Huber0a451282016-08-30 11:27:24 -070022#include <cutils/native_handle.h>
Hridya Valsarajub7370302017-03-08 14:39:23 -080023#include <hidl/HidlInternal.h>
Andreas Huber0a451282016-08-30 11:27:24 -070024#include <hidl/HidlSupport.h>
25
26namespace android {
27namespace hardware {
28
Hridya Valsaraju62bb7a02016-09-23 10:44:04 -070029typedef uint64_t RingBufferPosition;
30
Andreas Huber0a451282016-08-30 11:27:24 -070031struct GrantorDescriptor {
Hridya Valsaraju45eb91d2017-01-12 13:20:35 -080032 uint32_t flags __attribute__ ((aligned(4)));
33 uint32_t fdIndex __attribute__ ((aligned(4)));
34 uint32_t offset __attribute__ ((aligned(4)));
35 uint64_t extent __attribute__ ((aligned(8)));
Andreas Huber0a451282016-08-30 11:27:24 -070036};
37
Hridya Valsaraju45eb91d2017-01-12 13:20:35 -080038static_assert(offsetof(GrantorDescriptor, flags) == 0, "wrong offset");
39static_assert(offsetof(GrantorDescriptor, fdIndex) == 4, "wrong offset");
40static_assert(offsetof(GrantorDescriptor, offset) == 8, "wrong offset");
41static_assert(offsetof(GrantorDescriptor, extent) == 16, "wrong offset");
42static_assert(sizeof(GrantorDescriptor) == 24, "wrong size");
43static_assert(__alignof(GrantorDescriptor) == 8, "wrong alignment");
44
Hridya Valsaraju24fb4e22016-09-26 10:37:01 -070045enum MQFlavor : uint32_t {
46 /*
47 * kSynchronizedReadWrite represents the wait-free synchronized flavor of the
48 * FMQ. It is intended to be have a single reader and single writer.
49 * Attempts to overflow/underflow returns a failure.
50 */
Hridya Valsaraju56575782016-10-13 21:44:55 -070051 kSynchronizedReadWrite = 0x01,
52 /*
53 * kUnsynchronizedWrite represents the flavor of FMQ where writes always
54 * succeed. This flavor allows one writer and many readers. A read operation
55 * can detect an overwrite and reset the read counter.
56 */
57 kUnsynchronizedWrite = 0x02
Hridya Valsaraju24fb4e22016-09-26 10:37:01 -070058};
59
Hridya Valsaraju7e6404d2016-12-27 09:13:34 -080060template <typename T, MQFlavor flavor>
Andreas Huber0a451282016-08-30 11:27:24 -070061struct MQDescriptor {
Steven Moreland4cee37c2018-10-09 15:54:27 -070062 // Takes ownership of handle
Andreas Huber0a451282016-08-30 11:27:24 -070063 MQDescriptor(
64 const std::vector<GrantorDescriptor>& grantors,
Hridya Valsaraju24fb4e22016-09-26 10:37:01 -070065 native_handle_t* nHandle, size_t size);
Hridya Valsaraju62bb7a02016-09-23 10:44:04 -070066
Steven Moreland4cee37c2018-10-09 15:54:27 -070067 // Takes ownership of handle
Hridya Valsaraju24fb4e22016-09-26 10:37:01 -070068 MQDescriptor(size_t bufferSize, native_handle_t* nHandle,
Hridya Valsaraju9ec57302016-12-14 09:00:31 -080069 size_t messageSize, bool configureEventFlag = false);
Andreas Huber0a451282016-08-30 11:27:24 -070070
Hridya Valsaraju0e326a02016-12-21 10:52:29 -080071 MQDescriptor();
Andreas Huber0a451282016-08-30 11:27:24 -070072 ~MQDescriptor();
73
Steven Moreland6d503152018-10-09 15:12:20 -070074 explicit MQDescriptor(const MQDescriptor& other) : MQDescriptor() { *this = other; }
75 MQDescriptor& operator=(const MQDescriptor& other);
Andreas Huber0a451282016-08-30 11:27:24 -070076
77 size_t getSize() const;
78
79 size_t getQuantum() const;
80
81 int32_t getFlags() const;
Andreas Huber0a451282016-08-30 11:27:24 -070082
Andreas Huber0a451282016-08-30 11:27:24 -070083 bool isHandleValid() const { return mHandle != nullptr; }
84 size_t countGrantors() const { return mGrantors.size(); }
Yifan Hong089ae132016-11-11 11:32:52 -080085
86 inline const ::android::hardware::hidl_vec<GrantorDescriptor> &grantors() const {
87 return mGrantors;
88 }
89
90 inline ::android::hardware::hidl_vec<GrantorDescriptor> &grantors() {
91 return mGrantors;
92 }
93
94 inline const ::native_handle_t *handle() const {
95 return mHandle;
96 }
97
98 inline ::native_handle_t *handle() {
99 return mHandle;
100 }
101
102 static const size_t kOffsetOfGrantors;
103 static const size_t kOffsetOfHandle;
Hridya Valsaraju9ec57302016-12-14 09:00:31 -0800104 enum GrantorType : int { READPTRPOS = 0, WRITEPTRPOS, DATAPTRPOS, EVFLAGWORDPOS };
105
Hridya Valsaraju62bb7a02016-09-23 10:44:04 -0700106 /*
Hridya Valsaraju9ec57302016-12-14 09:00:31 -0800107 * There should at least be GrantorDescriptors for the read counter, write
108 * counter and data buffer. A GrantorDescriptor for an EventFlag word is
109 * not required if there is no need for blocking FMQ operations.
Hridya Valsaraju62bb7a02016-09-23 10:44:04 -0700110 */
Hridya Valsaraju9ec57302016-12-14 09:00:31 -0800111 static constexpr int32_t kMinGrantorCount = DATAPTRPOS + 1;
112
113 /*
114 * Minimum number of GrantorDescriptors required if EventFlag support is
115 * needed for blocking FMQ operations.
116 */
117 static constexpr int32_t kMinGrantorCountForEvFlagSupport = EVFLAGWORDPOS + 1;
Hridya Valsaraju5d148b32017-01-10 14:38:42 -0800118
119 //TODO(b/34160777) Identify a better solution that supports remoting.
120 static inline size_t alignToWordBoundary(size_t length) {
121 constexpr size_t kAlignmentSize = 64;
Hridya Valsarajub7370302017-03-08 14:39:23 -0800122 if (kAlignmentSize % __WORDSIZE != 0) {
123 details::logAlwaysFatal("Incompatible word size");
124 }
Hridya Valsaraju2143e742017-04-03 13:54:11 -0700125
126 /*
127 * Check if alignment to word boundary would cause an overflow.
128 */
129 if (length > SIZE_MAX - kAlignmentSize/8 + 1) {
130 details::logAlwaysFatal("Queue size too large");
131 }
132
Hridya Valsaraju5d148b32017-01-10 14:38:42 -0800133 return (length + kAlignmentSize/8 - 1) & ~(kAlignmentSize/8 - 1U);
134 }
135
136 static inline size_t isAlignedToWordBoundary(size_t offset) {
137 constexpr size_t kAlignmentSize = 64;
138 return (offset & (kAlignmentSize/8 - 1)) == 0;
139 }
Andreas Huber0a451282016-08-30 11:27:24 -0700140private:
141 ::android::hardware::hidl_vec<GrantorDescriptor> mGrantors;
Hridya Valsaraju44843442016-12-09 13:38:55 -0800142 ::android::hardware::details::hidl_pointer<native_handle_t> mHandle;
Andreas Huber0a451282016-08-30 11:27:24 -0700143 uint32_t mQuantum;
144 uint32_t mFlags;
145};
Hridya Valsaraju24fb4e22016-09-26 10:37:01 -0700146
Hridya Valsaraju7e6404d2016-12-27 09:13:34 -0800147template<typename T, MQFlavor flavor>
148const size_t MQDescriptor<T, flavor>::kOffsetOfGrantors = offsetof(MQDescriptor, mGrantors);
Yifan Hong089ae132016-11-11 11:32:52 -0800149
Hridya Valsaraju7e6404d2016-12-27 09:13:34 -0800150template<typename T, MQFlavor flavor>
151const size_t MQDescriptor<T, flavor>::kOffsetOfHandle = offsetof(MQDescriptor, mHandle);
Yifan Hong089ae132016-11-11 11:32:52 -0800152
Hridya Valsaraju24fb4e22016-09-26 10:37:01 -0700153/*
154 * MQDescriptorSync will describe the wait-free synchronized
155 * flavor of FMQ.
156 */
Hridya Valsaraju7e6404d2016-12-27 09:13:34 -0800157template<typename T>
158using MQDescriptorSync = MQDescriptor<T, kSynchronizedReadWrite>;
Hridya Valsaraju24fb4e22016-09-26 10:37:01 -0700159
Hridya Valsaraju56575782016-10-13 21:44:55 -0700160/*
161 * MQDescriptorUnsync will describe the unsynchronized write
162 * flavor of FMQ.
163 */
Hridya Valsaraju7e6404d2016-12-27 09:13:34 -0800164template<typename T>
165using MQDescriptorUnsync = MQDescriptor<T, kUnsynchronizedWrite>;
Hridya Valsaraju56575782016-10-13 21:44:55 -0700166
Hridya Valsaraju7e6404d2016-12-27 09:13:34 -0800167template<typename T, MQFlavor flavor>
168MQDescriptor<T, flavor>::MQDescriptor(
Hridya Valsaraju24fb4e22016-09-26 10:37:01 -0700169 const std::vector<GrantorDescriptor>& grantors,
170 native_handle_t* nhandle,
171 size_t size)
172 : mHandle(nhandle),
173 mQuantum(size),
174 mFlags(flavor) {
175 mGrantors.resize(grantors.size());
176 for (size_t i = 0; i < grantors.size(); ++i) {
Hridya Valsarajub7370302017-03-08 14:39:23 -0800177 if (isAlignedToWordBoundary(grantors[i].offset) == false) {
178 details::logAlwaysFatal("Grantor offsets need to be aligned");
179 }
Hridya Valsaraju24fb4e22016-09-26 10:37:01 -0700180 mGrantors[i] = grantors[i];
181 }
182}
183
Hridya Valsaraju7e6404d2016-12-27 09:13:34 -0800184template<typename T, MQFlavor flavor>
185MQDescriptor<T, flavor>::MQDescriptor(size_t bufferSize, native_handle_t *nHandle,
Hridya Valsaraju9ec57302016-12-14 09:00:31 -0800186 size_t messageSize, bool configureEventFlag)
Hridya Valsaraju24fb4e22016-09-26 10:37:01 -0700187 : mHandle(nHandle), mQuantum(messageSize), mFlags(flavor) {
Hridya Valsaraju9ec57302016-12-14 09:00:31 -0800188 /*
189 * If configureEventFlag is true, allocate an additional spot in mGrantor
190 * for containing the fd and offset for mmapping the EventFlag word.
191 */
192 mGrantors.resize(configureEventFlag? kMinGrantorCountForEvFlagSupport : kMinGrantorCount);
193
194 size_t memSize[] = {
195 sizeof(RingBufferPosition), /* memory to be allocated for read pointer counter */
196 sizeof(RingBufferPosition), /* memory to be allocated for write pointer counter */
197 bufferSize, /* memory to be allocated for data buffer */
198 sizeof(std::atomic<uint32_t>)/* memory to be allocated for EventFlag word */
199 };
200
Hridya Valsaraju24fb4e22016-09-26 10:37:01 -0700201 /*
202 * Create a default grantor descriptor for read, write pointers and
203 * the data buffer. fdIndex parameter is set to 0 by default and
204 * the offset for each grantor is contiguous.
205 */
Hridya Valsaraju9ec57302016-12-14 09:00:31 -0800206 for (size_t grantorPos = 0, offset = 0;
207 grantorPos < mGrantors.size();
208 offset += memSize[grantorPos++]) {
209 mGrantors[grantorPos] = {
210 0 /* grantor flags */,
211 0 /* fdIndex */,
Hridya Valsaraju5d148b32017-01-10 14:38:42 -0800212 static_cast<uint32_t>(alignToWordBoundary(offset)),
Hridya Valsaraju9ec57302016-12-14 09:00:31 -0800213 memSize[grantorPos]
214 };
215 }
Hridya Valsaraju24fb4e22016-09-26 10:37:01 -0700216}
217
Steven Moreland6d503152018-10-09 15:12:20 -0700218template <typename T, MQFlavor flavor>
219MQDescriptor<T, flavor>& MQDescriptor<T, flavor>::operator=(const MQDescriptor& other) {
220 mGrantors = other.mGrantors;
221 if (mHandle != nullptr) {
222 native_handle_close(mHandle);
223 native_handle_delete(mHandle);
224 mHandle = nullptr;
225 }
226 mQuantum = other.mQuantum;
227 mFlags = other.mFlags;
228
Hridya Valsaraju24fb4e22016-09-26 10:37:01 -0700229 if (other.mHandle != nullptr) {
230 mHandle = native_handle_create(
231 other.mHandle->numFds, other.mHandle->numInts);
232
233 for (int i = 0; i < other.mHandle->numFds; ++i) {
Hridya Valsaraju44843442016-12-09 13:38:55 -0800234 mHandle->data[i] = dup(other.mHandle->data[i]);
Hridya Valsaraju24fb4e22016-09-26 10:37:01 -0700235 }
236
Hridya Valsaraju44843442016-12-09 13:38:55 -0800237 memcpy(&mHandle->data[other.mHandle->numFds],
Hridya Valsaraju24fb4e22016-09-26 10:37:01 -0700238 &other.mHandle->data[other.mHandle->numFds],
239 other.mHandle->numInts * sizeof(int));
240 }
Steven Moreland6d503152018-10-09 15:12:20 -0700241
242 return *this;
Hridya Valsaraju24fb4e22016-09-26 10:37:01 -0700243}
244
Hridya Valsaraju7e6404d2016-12-27 09:13:34 -0800245template<typename T, MQFlavor flavor>
246MQDescriptor<T, flavor>::MQDescriptor() : MQDescriptor(
Hridya Valsaraju0e326a02016-12-21 10:52:29 -0800247 std::vector<android::hardware::GrantorDescriptor>(),
248 nullptr /* nHandle */,
249 0 /* size */) {}
250
Hridya Valsaraju7e6404d2016-12-27 09:13:34 -0800251template<typename T, MQFlavor flavor>
252MQDescriptor<T, flavor>::~MQDescriptor() {
Hridya Valsaraju24fb4e22016-09-26 10:37:01 -0700253 if (mHandle != nullptr) {
Hridya Valsaraju44843442016-12-09 13:38:55 -0800254 native_handle_close(mHandle);
255 native_handle_delete(mHandle);
Hridya Valsaraju24fb4e22016-09-26 10:37:01 -0700256 }
257}
258
Hridya Valsaraju7e6404d2016-12-27 09:13:34 -0800259template<typename T, MQFlavor flavor>
260size_t MQDescriptor<T, flavor>::getSize() const {
Hridya Valsaraju24fb4e22016-09-26 10:37:01 -0700261 return mGrantors[DATAPTRPOS].extent;
262}
263
Hridya Valsaraju7e6404d2016-12-27 09:13:34 -0800264template<typename T, MQFlavor flavor>
265size_t MQDescriptor<T, flavor>::getQuantum() const { return mQuantum; }
Hridya Valsaraju24fb4e22016-09-26 10:37:01 -0700266
Hridya Valsaraju7e6404d2016-12-27 09:13:34 -0800267template<typename T, MQFlavor flavor>
268int32_t MQDescriptor<T, flavor>::getFlags() const { return mFlags; }
Hridya Valsaraju24fb4e22016-09-26 10:37:01 -0700269
Yifan Hongbe7a6882017-01-05 17:30:17 -0800270template<typename T, MQFlavor flavor>
271std::string toString(const MQDescriptor<T, flavor> &q) {
272 std::string os;
273 if (flavor & kSynchronizedReadWrite) {
274 os += "fmq_sync";
275 }
276 if (flavor & kUnsynchronizedWrite) {
277 os += "fmq_unsync";
278 }
279 os += " {"
Hridya Valsarajued4e7142017-02-24 11:00:48 -0800280 + toString(q.grantors().size()) + " grantor(s), "
Yifan Hongbe7a6882017-01-05 17:30:17 -0800281 + "size = " + toString(q.getSize())
Hridya Valsarajued4e7142017-02-24 11:00:48 -0800282 + ", .handle = " + toString(q.handle())
Yifan Hongbe7a6882017-01-05 17:30:17 -0800283 + ", .quantum = " + toString(q.getQuantum()) + "}";
284 return os;
285}
Yifan Hongbe7a6882017-01-05 17:30:17 -0800286
Hridya Valsaraju4fe70f02016-09-20 21:02:51 -0700287} // namespace hardware
288} // namespace android
Andreas Huber0a451282016-08-30 11:27:24 -0700289
290#endif // FMSGQ_DESCRIPTOR_H