1 /* 2 * Copyright (C) 2024 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 <fcntl.h> 20 #include <unistd.h> 21 22 #include <memory> 23 #include <mutex> 24 #include <optional> 25 #include <string> 26 #include <string_view> 27 #include <utility> 28 29 #include "PhysicalQuantityTypes.h" 30 31 namespace aidl { 32 namespace google { 33 namespace hardware { 34 namespace power { 35 namespace impl { 36 namespace pixel { 37 38 struct FdInterface { 39 virtual int open(const char *, int) const = 0; 40 virtual int write(int, const char *, size_t) const = 0; 41 virtual ssize_t read(int, void *, size_t) const = 0; 42 virtual off_t lseek(int, off_t, int) const = 0; 43 virtual int close(int) const = 0; 44 45 FdInterface() = default; 46 virtual ~FdInterface() = default; 47 FdInterface(FdInterface const &) = delete; 48 FdInterface &operator=(FdInterface const &) = delete; 49 }; 50 51 struct FdWriter : FdInterface { openFdWriter52 int open(const char *path, int flags) const override { return ::open(path, flags); } writeFdWriter53 int write(int fd, const char *data, size_t len) const override { 54 return ::write(fd, data, len); 55 } readFdWriter56 ssize_t read(int fd, void *data, size_t len) const final { return ::read(fd, data, len); } lseekFdWriter57 off_t lseek(int fd, off_t offset, int whence) const final { 58 return ::lseek(fd, offset, whence); 59 } closeFdWriter60 int close(int fd) const override { return ::close(fd); } 61 }; 62 63 struct GpuCapacityNode final { 64 // Exceptions should really be allowed, use exploded constructor pattern and provide 65 // helper construction function. 66 GpuCapacityNode(std::unique_ptr<FdInterface> fd_interface, int validated_capacity_headroom_fd, 67 int validated_frequency_fd, std::string_view gpu_node_dir); 68 static std::unique_ptr<GpuCapacityNode> init_gpu_capacity_node( 69 std::unique_ptr<FdInterface> fd_interface, std::string_view gpu_node_dir); 70 71 ~GpuCapacityNode() noexcept; 72 73 bool set_gpu_capacity(Cycles capacity) const; 74 std::optional<Frequency> gpu_frequency() const; 75 76 private: 77 std::unique_ptr<FdInterface> const fd_interface_; 78 std::string const capacity_node_path_; 79 int const capacity_headroom_fd_; 80 int const frequency_fd_; 81 std::mutex mutable freq_mutex_; 82 std::mutex mutable capacity_mutex_; 83 }; 84 85 // There's not a global object factory or context in PowerHal, maybe introducing one would simplify 86 // resource management. 87 std::optional<std::unique_ptr<GpuCapacityNode>> createGpuCapacityNode(); 88 89 } // namespace pixel 90 } // namespace impl 91 } // namespace power 92 } // namespace hardware 93 } // namespace google 94 } // namespace aidl 95