Andreas Huber | 0a45128 | 2016-08-30 11:27:24 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 20 | #include <android-base/macros.h> |
| 21 | #include <cutils/native_handle.h> |
| 22 | #include <hidl/HidlSupport.h> |
Hridya Valsaraju | 4fe70f0 | 2016-09-20 21:02:51 -0700 | [diff] [blame] | 23 | #include <utils/NativeHandle.h> |
Andreas Huber | 0a45128 | 2016-08-30 11:27:24 -0700 | [diff] [blame] | 24 | |
| 25 | namespace android { |
| 26 | namespace hardware { |
| 27 | |
Hridya Valsaraju | 62bb7a0 | 2016-09-23 10:44:04 -0700 | [diff] [blame] | 28 | typedef uint64_t RingBufferPosition; |
| 29 | |
Andreas Huber | 0a45128 | 2016-08-30 11:27:24 -0700 | [diff] [blame] | 30 | struct GrantorDescriptor { |
| 31 | uint32_t flags; |
| 32 | uint32_t fdIndex; |
| 33 | uint32_t offset; |
Hridya Valsaraju | 62bb7a0 | 2016-09-23 10:44:04 -0700 | [diff] [blame] | 34 | size_t extent; |
Andreas Huber | 0a45128 | 2016-08-30 11:27:24 -0700 | [diff] [blame] | 35 | }; |
| 36 | |
Hridya Valsaraju | 24fb4e2 | 2016-09-26 10:37:01 -0700 | [diff] [blame] | 37 | enum MQFlavor : uint32_t { |
| 38 | /* |
| 39 | * kSynchronizedReadWrite represents the wait-free synchronized flavor of the |
| 40 | * FMQ. It is intended to be have a single reader and single writer. |
| 41 | * Attempts to overflow/underflow returns a failure. |
| 42 | */ |
Hridya Valsaraju | 5657578 | 2016-10-13 21:44:55 -0700 | [diff] [blame] | 43 | kSynchronizedReadWrite = 0x01, |
| 44 | /* |
| 45 | * kUnsynchronizedWrite represents the flavor of FMQ where writes always |
| 46 | * succeed. This flavor allows one writer and many readers. A read operation |
| 47 | * can detect an overwrite and reset the read counter. |
| 48 | */ |
| 49 | kUnsynchronizedWrite = 0x02 |
Hridya Valsaraju | 24fb4e2 | 2016-09-26 10:37:01 -0700 | [diff] [blame] | 50 | }; |
| 51 | |
| 52 | template <MQFlavor flavor> |
Andreas Huber | 0a45128 | 2016-08-30 11:27:24 -0700 | [diff] [blame] | 53 | struct MQDescriptor { |
| 54 | MQDescriptor( |
| 55 | const std::vector<GrantorDescriptor>& grantors, |
Hridya Valsaraju | 24fb4e2 | 2016-09-26 10:37:01 -0700 | [diff] [blame] | 56 | native_handle_t* nHandle, size_t size); |
Hridya Valsaraju | 62bb7a0 | 2016-09-23 10:44:04 -0700 | [diff] [blame] | 57 | |
Hridya Valsaraju | 24fb4e2 | 2016-09-26 10:37:01 -0700 | [diff] [blame] | 58 | MQDescriptor(size_t bufferSize, native_handle_t* nHandle, |
Hridya Valsaraju | 9ec5730 | 2016-12-14 09:00:31 -0800 | [diff] [blame^] | 59 | size_t messageSize, bool configureEventFlag = false); |
Andreas Huber | 0a45128 | 2016-08-30 11:27:24 -0700 | [diff] [blame] | 60 | |
| 61 | ~MQDescriptor(); |
| 62 | |
| 63 | explicit MQDescriptor(const MQDescriptor &other); |
| 64 | MQDescriptor &operator=(const MQDescriptor &other) = delete; |
| 65 | |
| 66 | size_t getSize() const; |
| 67 | |
| 68 | size_t getQuantum() const; |
| 69 | |
| 70 | int32_t getFlags() const; |
Andreas Huber | 0a45128 | 2016-08-30 11:27:24 -0700 | [diff] [blame] | 71 | |
Andreas Huber | 0a45128 | 2016-08-30 11:27:24 -0700 | [diff] [blame] | 72 | bool isHandleValid() const { return mHandle != nullptr; } |
| 73 | size_t countGrantors() const { return mGrantors.size(); } |
Hridya Valsaraju | 4fe70f0 | 2016-09-20 21:02:51 -0700 | [diff] [blame] | 74 | std::vector<GrantorDescriptor> getGrantors() const; |
| 75 | const sp<NativeHandle> getNativeHandle() const; |
Yifan Hong | 089ae13 | 2016-11-11 11:32:52 -0800 | [diff] [blame] | 76 | |
| 77 | inline const ::android::hardware::hidl_vec<GrantorDescriptor> &grantors() const { |
| 78 | return mGrantors; |
| 79 | } |
| 80 | |
| 81 | inline ::android::hardware::hidl_vec<GrantorDescriptor> &grantors() { |
| 82 | return mGrantors; |
| 83 | } |
| 84 | |
| 85 | inline const ::native_handle_t *handle() const { |
| 86 | return mHandle; |
| 87 | } |
| 88 | |
| 89 | inline ::native_handle_t *handle() { |
| 90 | return mHandle; |
| 91 | } |
| 92 | |
| 93 | static const size_t kOffsetOfGrantors; |
| 94 | static const size_t kOffsetOfHandle; |
Hridya Valsaraju | 9ec5730 | 2016-12-14 09:00:31 -0800 | [diff] [blame^] | 95 | enum GrantorType : int { READPTRPOS = 0, WRITEPTRPOS, DATAPTRPOS, EVFLAGWORDPOS }; |
| 96 | |
Hridya Valsaraju | 62bb7a0 | 2016-09-23 10:44:04 -0700 | [diff] [blame] | 97 | /* |
Hridya Valsaraju | 9ec5730 | 2016-12-14 09:00:31 -0800 | [diff] [blame^] | 98 | * There should at least be GrantorDescriptors for the read counter, write |
| 99 | * counter and data buffer. A GrantorDescriptor for an EventFlag word is |
| 100 | * not required if there is no need for blocking FMQ operations. |
Hridya Valsaraju | 62bb7a0 | 2016-09-23 10:44:04 -0700 | [diff] [blame] | 101 | */ |
Hridya Valsaraju | 9ec5730 | 2016-12-14 09:00:31 -0800 | [diff] [blame^] | 102 | static constexpr int32_t kMinGrantorCount = DATAPTRPOS + 1; |
| 103 | |
| 104 | /* |
| 105 | * Minimum number of GrantorDescriptors required if EventFlag support is |
| 106 | * needed for blocking FMQ operations. |
| 107 | */ |
| 108 | static constexpr int32_t kMinGrantorCountForEvFlagSupport = EVFLAGWORDPOS + 1; |
Andreas Huber | 0a45128 | 2016-08-30 11:27:24 -0700 | [diff] [blame] | 109 | private: |
| 110 | ::android::hardware::hidl_vec<GrantorDescriptor> mGrantors; |
Hridya Valsaraju | 4484344 | 2016-12-09 13:38:55 -0800 | [diff] [blame] | 111 | ::android::hardware::details::hidl_pointer<native_handle_t> mHandle; |
Andreas Huber | 0a45128 | 2016-08-30 11:27:24 -0700 | [diff] [blame] | 112 | uint32_t mQuantum; |
| 113 | uint32_t mFlags; |
| 114 | }; |
Hridya Valsaraju | 24fb4e2 | 2016-09-26 10:37:01 -0700 | [diff] [blame] | 115 | |
Yifan Hong | 089ae13 | 2016-11-11 11:32:52 -0800 | [diff] [blame] | 116 | template<MQFlavor flavor> |
| 117 | const size_t MQDescriptor<flavor>::kOffsetOfGrantors = offsetof(MQDescriptor<flavor>, mGrantors); |
| 118 | |
| 119 | template<MQFlavor flavor> |
| 120 | const size_t MQDescriptor<flavor>::kOffsetOfHandle = offsetof(MQDescriptor<flavor>, mHandle); |
| 121 | |
Hridya Valsaraju | 24fb4e2 | 2016-09-26 10:37:01 -0700 | [diff] [blame] | 122 | /* |
| 123 | * MQDescriptorSync will describe the wait-free synchronized |
| 124 | * flavor of FMQ. |
| 125 | */ |
| 126 | using MQDescriptorSync = MQDescriptor<kSynchronizedReadWrite>; |
| 127 | |
Hridya Valsaraju | 5657578 | 2016-10-13 21:44:55 -0700 | [diff] [blame] | 128 | /* |
| 129 | * MQDescriptorUnsync will describe the unsynchronized write |
| 130 | * flavor of FMQ. |
| 131 | */ |
| 132 | using MQDescriptorUnsync = MQDescriptor<kUnsynchronizedWrite>; |
| 133 | |
Hridya Valsaraju | 24fb4e2 | 2016-09-26 10:37:01 -0700 | [diff] [blame] | 134 | template<MQFlavor flavor> |
| 135 | MQDescriptor<flavor>::MQDescriptor( |
| 136 | const std::vector<GrantorDescriptor>& grantors, |
| 137 | native_handle_t* nhandle, |
| 138 | size_t size) |
| 139 | : mHandle(nhandle), |
| 140 | mQuantum(size), |
| 141 | mFlags(flavor) { |
| 142 | mGrantors.resize(grantors.size()); |
| 143 | for (size_t i = 0; i < grantors.size(); ++i) { |
| 144 | mGrantors[i] = grantors[i]; |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | template<MQFlavor flavor> |
| 149 | MQDescriptor<flavor>::MQDescriptor(size_t bufferSize, native_handle_t *nHandle, |
Hridya Valsaraju | 9ec5730 | 2016-12-14 09:00:31 -0800 | [diff] [blame^] | 150 | size_t messageSize, bool configureEventFlag) |
Hridya Valsaraju | 24fb4e2 | 2016-09-26 10:37:01 -0700 | [diff] [blame] | 151 | : mHandle(nHandle), mQuantum(messageSize), mFlags(flavor) { |
Hridya Valsaraju | 9ec5730 | 2016-12-14 09:00:31 -0800 | [diff] [blame^] | 152 | /* |
| 153 | * If configureEventFlag is true, allocate an additional spot in mGrantor |
| 154 | * for containing the fd and offset for mmapping the EventFlag word. |
| 155 | */ |
| 156 | mGrantors.resize(configureEventFlag? kMinGrantorCountForEvFlagSupport : kMinGrantorCount); |
| 157 | |
| 158 | size_t memSize[] = { |
| 159 | sizeof(RingBufferPosition), /* memory to be allocated for read pointer counter */ |
| 160 | sizeof(RingBufferPosition), /* memory to be allocated for write pointer counter */ |
| 161 | bufferSize, /* memory to be allocated for data buffer */ |
| 162 | sizeof(std::atomic<uint32_t>)/* memory to be allocated for EventFlag word */ |
| 163 | }; |
| 164 | |
Hridya Valsaraju | 24fb4e2 | 2016-09-26 10:37:01 -0700 | [diff] [blame] | 165 | /* |
| 166 | * Create a default grantor descriptor for read, write pointers and |
| 167 | * the data buffer. fdIndex parameter is set to 0 by default and |
| 168 | * the offset for each grantor is contiguous. |
| 169 | */ |
Hridya Valsaraju | 9ec5730 | 2016-12-14 09:00:31 -0800 | [diff] [blame^] | 170 | for (size_t grantorPos = 0, offset = 0; |
| 171 | grantorPos < mGrantors.size(); |
| 172 | offset += memSize[grantorPos++]) { |
| 173 | mGrantors[grantorPos] = { |
| 174 | 0 /* grantor flags */, |
| 175 | 0 /* fdIndex */, |
| 176 | static_cast<uint32_t>(offset), |
| 177 | memSize[grantorPos] |
| 178 | }; |
| 179 | } |
Hridya Valsaraju | 24fb4e2 | 2016-09-26 10:37:01 -0700 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | template<MQFlavor flavor> |
| 183 | MQDescriptor<flavor>::MQDescriptor(const MQDescriptor<flavor> &other) |
| 184 | : mGrantors(other.mGrantors), |
| 185 | mHandle(nullptr), |
| 186 | mQuantum(other.mQuantum), |
| 187 | mFlags(other.mFlags) { |
| 188 | if (other.mHandle != nullptr) { |
| 189 | mHandle = native_handle_create( |
| 190 | other.mHandle->numFds, other.mHandle->numInts); |
| 191 | |
| 192 | for (int i = 0; i < other.mHandle->numFds; ++i) { |
Hridya Valsaraju | 4484344 | 2016-12-09 13:38:55 -0800 | [diff] [blame] | 193 | mHandle->data[i] = dup(other.mHandle->data[i]); |
Hridya Valsaraju | 24fb4e2 | 2016-09-26 10:37:01 -0700 | [diff] [blame] | 194 | } |
| 195 | |
Hridya Valsaraju | 4484344 | 2016-12-09 13:38:55 -0800 | [diff] [blame] | 196 | memcpy(&mHandle->data[other.mHandle->numFds], |
Hridya Valsaraju | 24fb4e2 | 2016-09-26 10:37:01 -0700 | [diff] [blame] | 197 | &other.mHandle->data[other.mHandle->numFds], |
| 198 | other.mHandle->numInts * sizeof(int)); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | template<MQFlavor flavor> |
| 203 | MQDescriptor<flavor>::~MQDescriptor() { |
| 204 | if (mHandle != nullptr) { |
Hridya Valsaraju | 4484344 | 2016-12-09 13:38:55 -0800 | [diff] [blame] | 205 | native_handle_close(mHandle); |
| 206 | native_handle_delete(mHandle); |
Hridya Valsaraju | 24fb4e2 | 2016-09-26 10:37:01 -0700 | [diff] [blame] | 207 | } |
| 208 | } |
| 209 | |
| 210 | template<MQFlavor flavor> |
Hridya Valsaraju | 24fb4e2 | 2016-09-26 10:37:01 -0700 | [diff] [blame] | 211 | size_t MQDescriptor<flavor>::getSize() const { |
| 212 | return mGrantors[DATAPTRPOS].extent; |
| 213 | } |
| 214 | |
| 215 | template<MQFlavor flavor> |
| 216 | size_t MQDescriptor<flavor>::getQuantum() const { return mQuantum; } |
| 217 | |
| 218 | template<MQFlavor flavor> |
| 219 | int32_t MQDescriptor<flavor>::getFlags() const { return mFlags; } |
| 220 | |
| 221 | template<MQFlavor flavor> |
| 222 | std::vector<GrantorDescriptor> MQDescriptor<flavor>::getGrantors() const { |
| 223 | size_t grantor_count = mGrantors.size(); |
| 224 | std::vector<GrantorDescriptor> grantors(grantor_count); |
| 225 | for (size_t i = 0; i < grantor_count; i++) { |
| 226 | grantors[i] = mGrantors[i]; |
| 227 | } |
| 228 | return grantors; |
| 229 | } |
| 230 | |
| 231 | template<MQFlavor flavor> |
| 232 | const sp<NativeHandle> MQDescriptor<flavor>::getNativeHandle() const { |
| 233 | /* |
| 234 | * Create an sp<NativeHandle> from mHandle. |
| 235 | */ |
| 236 | return NativeHandle::create(mHandle, false /* ownsHandle */); |
| 237 | } |
Hridya Valsaraju | 4fe70f0 | 2016-09-20 21:02:51 -0700 | [diff] [blame] | 238 | } // namespace hardware |
| 239 | } // namespace android |
Andreas Huber | 0a45128 | 2016-08-30 11:27:24 -0700 | [diff] [blame] | 240 | |
| 241 | #endif // FMSGQ_DESCRIPTOR_H |