Patrick Venture | 22e3875 | 2018-11-21 08:52:49 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 Google Inc. |
| 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 | |
Patrick Venture | e772842 | 2018-11-14 20:16:33 -0800 | [diff] [blame] | 17 | #include "lpc_aspeed.hpp" |
| 18 | |
Patrick Venture | 5251da9 | 2019-01-17 11:25:26 -0800 | [diff] [blame] | 19 | #include "window_hw_interface.hpp" |
Patrick Venture | e772842 | 2018-11-14 20:16:33 -0800 | [diff] [blame] | 20 | |
Patrick Venture | 7b91cbc | 2018-11-28 14:24:41 -0800 | [diff] [blame] | 21 | #include <fcntl.h> |
| 22 | #include <linux/aspeed-lpc-ctrl.h> |
| 23 | #include <linux/kernel.h> |
| 24 | |
Patrick Venture | e772842 | 2018-11-14 20:16:33 -0800 | [diff] [blame] | 25 | #include <cstdint> |
Patrick Venture | 7b91cbc | 2018-11-28 14:24:41 -0800 | [diff] [blame] | 26 | #include <cstring> |
Patrick Venture | e772842 | 2018-11-14 20:16:33 -0800 | [diff] [blame] | 27 | #include <memory> |
Patrick Venture | bf06463 | 2019-01-17 12:20:21 -0800 | [diff] [blame] | 28 | #include <string> |
Patrick Venture | e772842 | 2018-11-14 20:16:33 -0800 | [diff] [blame] | 29 | #include <utility> |
| 30 | |
| 31 | namespace blobs |
| 32 | { |
| 33 | |
Patrick Venture | bf06463 | 2019-01-17 12:20:21 -0800 | [diff] [blame] | 34 | const std::string LpcMapperAspeed::lpcControlPath = "/dev/aspeed-lpc-ctrl"; |
| 35 | |
Patrick Venture | 5251da9 | 2019-01-17 11:25:26 -0800 | [diff] [blame] | 36 | std::unique_ptr<HardwareMapperInterface> |
Patrick Venture | 78b1a66 | 2019-01-17 12:38:26 -0800 | [diff] [blame] | 37 | LpcMapperAspeed::createAspeedMapper(std::uint32_t regionAddress, |
| 38 | std::size_t regionSize) |
Patrick Venture | e772842 | 2018-11-14 20:16:33 -0800 | [diff] [blame] | 39 | { |
| 40 | /* NOTE: considered using a joint factory to create one or the other, for |
| 41 | * now, separate factories. |
| 42 | */ |
Patrick Venture | 78b1a66 | 2019-01-17 12:38:26 -0800 | [diff] [blame] | 43 | return std::make_unique<LpcMapperAspeed>(regionAddress, regionSize); |
Patrick Venture | e772842 | 2018-11-14 20:16:33 -0800 | [diff] [blame] | 44 | } |
| 45 | |
Patrick Venture | 2343cfc | 2019-01-17 13:04:36 -0800 | [diff] [blame] | 46 | void LpcMapperAspeed::close() |
| 47 | { |
| 48 | } |
| 49 | |
Patrick Venture | e772842 | 2018-11-14 20:16:33 -0800 | [diff] [blame] | 50 | std::pair<std::uint32_t, std::uint32_t> |
| 51 | LpcMapperAspeed::mapWindow(std::uint32_t address, std::uint32_t length) |
| 52 | { |
Patrick Venture | 7b91cbc | 2018-11-28 14:24:41 -0800 | [diff] [blame] | 53 | static const std::uint32_t MASK_64K = 0xFFFFU; |
| 54 | const std::uint32_t offset = address & MASK_64K; |
| 55 | |
| 56 | if (offset + length > regionSize) |
| 57 | { |
| 58 | std::fprintf(stderr, |
| 59 | "requested window size %" PRIu32 ", offset %#" PRIx32 |
| 60 | " is too large for mem region" |
| 61 | " of size %zu\n", |
| 62 | length, offset, regionSize); |
| 63 | /* TODO: need to throw an exception at this point to store the data to |
| 64 | * provide an EBIG response later. |
| 65 | */ |
| 66 | /* *windowSize = regionSize - offset; */ |
| 67 | return std::make_pair(0, 0); |
| 68 | } |
| 69 | |
| 70 | struct aspeed_lpc_ctrl_mapping map = { |
| 71 | .window_type = ASPEED_LPC_CTRL_WINDOW_MEMORY, |
| 72 | .window_id = 0, |
| 73 | .flags = 0, |
| 74 | .addr = address & ~MASK_64K, |
| 75 | .offset = 0, |
| 76 | .size = __ALIGN_KERNEL_MASK(offset + length, MASK_64K), |
| 77 | }; |
| 78 | |
| 79 | std::fprintf(stderr, |
| 80 | "requesting Aspeed LPC window at %#" PRIx32 " of size %" PRIu32 |
| 81 | "\n", |
| 82 | map.addr, map.size); |
| 83 | |
Patrick Venture | bf06463 | 2019-01-17 12:20:21 -0800 | [diff] [blame] | 84 | const auto lpcControlFd = sys->open(lpcControlPath.c_str(), O_RDWR); |
Patrick Venture | 7b91cbc | 2018-11-28 14:24:41 -0800 | [diff] [blame] | 85 | if (lpcControlFd == -1) |
| 86 | { |
| 87 | std::fprintf(stderr, |
| 88 | "cannot open Aspeed LPC kernel control dev \"%s\"\n", |
Patrick Venture | bf06463 | 2019-01-17 12:20:21 -0800 | [diff] [blame] | 89 | lpcControlPath.c_str()); |
Patrick Venture | 7b91cbc | 2018-11-28 14:24:41 -0800 | [diff] [blame] | 90 | return std::make_pair(0, 0); |
| 91 | } |
| 92 | |
| 93 | if (sys->ioctl(lpcControlFd, ASPEED_LPC_CTRL_IOCTL_MAP, &map) == -1) |
| 94 | { |
| 95 | std::fprintf(stderr, "Failed to ioctl Aspeed LPC map with error %s\n", |
| 96 | std::strerror(errno)); |
| 97 | sys->close(lpcControlFd); |
| 98 | return std::make_pair(0, 0); |
| 99 | } |
| 100 | |
| 101 | sys->close(lpcControlFd); |
| 102 | return std::make_pair(offset, length); |
Patrick Venture | e772842 | 2018-11-14 20:16:33 -0800 | [diff] [blame] | 103 | } |
| 104 | |
Patrick Venture | a9e0005 | 2019-01-17 12:56:56 -0800 | [diff] [blame^] | 105 | bool LpcMapperAspeed::mapRegion() |
| 106 | { |
| 107 | /* Open the file to map. */ |
| 108 | mappedFd = sys->open(lpcControlPath.c_str(), O_RDONLY | O_SYNC); |
| 109 | |
| 110 | mappedRegion = reinterpret_cast<uint8_t*>( |
| 111 | sys->mmap(0, regionSize, PROT_READ, MAP_SHARED, mappedFd, 0)); |
| 112 | |
| 113 | if (mappedRegion == MAP_FAILED) |
| 114 | { |
| 115 | sys->close(mappedFd); |
| 116 | mappedFd = -1; |
| 117 | std::fprintf(stderr, "Mmap failure: '%s'\n", std::strerror(errno)); |
| 118 | return false; |
| 119 | } |
| 120 | |
| 121 | /* TOOD: There is no close() method here, to close mappedFd, or mappedRegion |
| 122 | * -- therefore, a good next step will be to evaluate whether or not the |
| 123 | * other pieces should go here... |
| 124 | */ |
| 125 | return true; |
| 126 | } |
| 127 | |
Patrick Venture | 517710d | 2019-01-17 11:37:40 -0800 | [diff] [blame] | 128 | std::vector<std::uint8_t> LpcMapperAspeed::copyFrom(std::uint32_t length) |
| 129 | { |
Patrick Venture | a9e0005 | 2019-01-17 12:56:56 -0800 | [diff] [blame^] | 130 | if (mappedFd < 0) |
| 131 | { |
| 132 | /* NOTE: may make more sense to do this in the open() */ |
| 133 | if (!mapRegion()) |
| 134 | { |
| 135 | /* Was unable to map region -- this call only required if using mmap |
| 136 | * and not ioctl. |
| 137 | */ |
| 138 | /* TODO: have a better failure. */ |
| 139 | return {}; |
| 140 | } |
| 141 | } |
| 142 | |
Patrick Venture | 517710d | 2019-01-17 11:37:40 -0800 | [diff] [blame] | 143 | /* TODO: Implement this. */ |
| 144 | return {}; |
| 145 | } |
| 146 | |
Patrick Venture | e772842 | 2018-11-14 20:16:33 -0800 | [diff] [blame] | 147 | } // namespace blobs |