Kristofer Jonsson | 6d80c42 | 2021-10-14 10:09:08 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2021 Arm Limited. All rights reserved. |
| 3 | * |
| 4 | * SPDX-License-Identifier: Apache-2.0 |
| 5 | * |
| 6 | * Licensed under the Apache License, Version 2.0 (the License); you may |
| 7 | * not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at |
| 9 | * |
| 10 | * www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an AS IS BASIS, WITHOUT |
| 14 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | */ |
| 18 | |
| 19 | #ifndef DEV_MEM_HPP |
| 20 | #define DEV_MEM_HPP |
| 21 | |
| 22 | #include <exception> |
| 23 | #include <string> |
| 24 | |
| 25 | namespace EthosU { |
| 26 | namespace DevMem { |
| 27 | |
| 28 | /**************************************************************************** |
| 29 | * Exception |
| 30 | ****************************************************************************/ |
| 31 | |
| 32 | class Exception : public std::exception { |
| 33 | public: |
| 34 | Exception(const char *msg); |
| 35 | Exception(const std::string &msg); |
| 36 | virtual ~Exception() throw(); |
| 37 | virtual const char *what() const throw(); |
| 38 | |
| 39 | private: |
| 40 | std::string msg; |
| 41 | }; |
| 42 | |
| 43 | /**************************************************************************** |
| 44 | * DevMem |
| 45 | ****************************************************************************/ |
| 46 | |
| 47 | class DevMem { |
| 48 | public: |
| 49 | DevMem(uintptr_t address, size_t size); |
| 50 | virtual ~DevMem(); |
| 51 | |
| 52 | void read(char *dst, size_t length, size_t offset); |
| 53 | |
| 54 | template <typename T> |
| 55 | void read(T &dst, size_t offset) { |
| 56 | read(reinterpret_cast<char *>(&dst), sizeof(dst), offset); |
| 57 | } |
| 58 | |
| 59 | void write(char *src, size_t length, size_t offset); |
| 60 | |
| 61 | template <typename T> |
| 62 | void write(T &src, size_t offset) { |
| 63 | write(reinterpret_cast<char *>(&src), sizeof(src), offset); |
| 64 | } |
| 65 | |
| 66 | private: |
| 67 | char *base; |
| 68 | const uintptr_t pageMask; |
| 69 | const size_t pageOffset; |
| 70 | const size_t size; |
| 71 | }; |
| 72 | |
| 73 | /**************************************************************************** |
| 74 | * Log |
| 75 | ****************************************************************************/ |
| 76 | |
| 77 | class Log : public DevMem { |
| 78 | public: |
| 79 | Log(uintptr_t address, size_t size = LOG_SIZE_MAX); |
| 80 | |
| 81 | void clear(); |
| 82 | void print(); |
| 83 | |
| 84 | private: |
| 85 | struct LogHeader { |
| 86 | uint32_t size; |
| 87 | uint32_t read; |
| 88 | uint32_t pad[6]; |
| 89 | uint32_t write; |
| 90 | }; |
| 91 | |
| 92 | static const size_t LOG_SIZE_MIN = 1024; |
| 93 | static const size_t LOG_SIZE_MAX = 1024 * 1024; |
| 94 | |
| 95 | static uintptr_t getAddress(); |
| 96 | }; |
| 97 | |
| 98 | } // namespace DevMem |
| 99 | } // namespace EthosU |
| 100 | |
| 101 | #endif /* DEV_MEM_HPP */ |