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 | |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame^] | 31 | namespace ipmi_flash |
Patrick Venture | e772842 | 2018-11-14 20:16:33 -0800 | [diff] [blame] | 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 | { |
Patrick Venture | 5e02215 | 2019-01-17 13:19:00 -0800 | [diff] [blame] | 48 | if (mappedRegion) |
| 49 | { |
| 50 | sys->munmap(mappedRegion, regionSize); |
| 51 | mappedRegion = nullptr; |
| 52 | } |
| 53 | |
| 54 | if (mappedFd != -1) |
| 55 | { |
| 56 | sys->close(mappedFd); |
| 57 | mappedFd = -1; |
| 58 | } |
Patrick Venture | 2343cfc | 2019-01-17 13:04:36 -0800 | [diff] [blame] | 59 | } |
| 60 | |
Patrick Venture | e772842 | 2018-11-14 20:16:33 -0800 | [diff] [blame] | 61 | std::pair<std::uint32_t, std::uint32_t> |
| 62 | LpcMapperAspeed::mapWindow(std::uint32_t address, std::uint32_t length) |
| 63 | { |
Patrick Venture | 7b91cbc | 2018-11-28 14:24:41 -0800 | [diff] [blame] | 64 | static const std::uint32_t MASK_64K = 0xFFFFU; |
| 65 | const std::uint32_t offset = address & MASK_64K; |
| 66 | |
| 67 | if (offset + length > regionSize) |
| 68 | { |
| 69 | std::fprintf(stderr, |
| 70 | "requested window size %" PRIu32 ", offset %#" PRIx32 |
| 71 | " is too large for mem region" |
| 72 | " of size %zu\n", |
| 73 | length, offset, regionSize); |
| 74 | /* TODO: need to throw an exception at this point to store the data to |
| 75 | * provide an EBIG response later. |
| 76 | */ |
| 77 | /* *windowSize = regionSize - offset; */ |
| 78 | return std::make_pair(0, 0); |
| 79 | } |
| 80 | |
| 81 | struct aspeed_lpc_ctrl_mapping map = { |
| 82 | .window_type = ASPEED_LPC_CTRL_WINDOW_MEMORY, |
| 83 | .window_id = 0, |
| 84 | .flags = 0, |
| 85 | .addr = address & ~MASK_64K, |
| 86 | .offset = 0, |
| 87 | .size = __ALIGN_KERNEL_MASK(offset + length, MASK_64K), |
| 88 | }; |
| 89 | |
| 90 | std::fprintf(stderr, |
| 91 | "requesting Aspeed LPC window at %#" PRIx32 " of size %" PRIu32 |
| 92 | "\n", |
| 93 | map.addr, map.size); |
| 94 | |
Patrick Venture | bf06463 | 2019-01-17 12:20:21 -0800 | [diff] [blame] | 95 | const auto lpcControlFd = sys->open(lpcControlPath.c_str(), O_RDWR); |
Patrick Venture | 7b91cbc | 2018-11-28 14:24:41 -0800 | [diff] [blame] | 96 | if (lpcControlFd == -1) |
| 97 | { |
| 98 | std::fprintf(stderr, |
| 99 | "cannot open Aspeed LPC kernel control dev \"%s\"\n", |
Patrick Venture | bf06463 | 2019-01-17 12:20:21 -0800 | [diff] [blame] | 100 | lpcControlPath.c_str()); |
Patrick Venture | 7b91cbc | 2018-11-28 14:24:41 -0800 | [diff] [blame] | 101 | return std::make_pair(0, 0); |
| 102 | } |
| 103 | |
| 104 | if (sys->ioctl(lpcControlFd, ASPEED_LPC_CTRL_IOCTL_MAP, &map) == -1) |
| 105 | { |
| 106 | std::fprintf(stderr, "Failed to ioctl Aspeed LPC map with error %s\n", |
| 107 | std::strerror(errno)); |
| 108 | sys->close(lpcControlFd); |
| 109 | return std::make_pair(0, 0); |
| 110 | } |
| 111 | |
| 112 | sys->close(lpcControlFd); |
| 113 | return std::make_pair(offset, length); |
Patrick Venture | e772842 | 2018-11-14 20:16:33 -0800 | [diff] [blame] | 114 | } |
| 115 | |
Patrick Venture | a9e0005 | 2019-01-17 12:56:56 -0800 | [diff] [blame] | 116 | bool LpcMapperAspeed::mapRegion() |
| 117 | { |
| 118 | /* Open the file to map. */ |
| 119 | mappedFd = sys->open(lpcControlPath.c_str(), O_RDONLY | O_SYNC); |
| 120 | |
Patrick Venture | 98af2d1 | 2019-02-26 09:41:28 -0800 | [diff] [blame] | 121 | /* TODO: The offset to use is the address we use for the map - the base |
| 122 | * address of the memory region we reserved in the device-tree. |
| 123 | */ |
Patrick Venture | a9e0005 | 2019-01-17 12:56:56 -0800 | [diff] [blame] | 124 | mappedRegion = reinterpret_cast<uint8_t*>( |
| 125 | sys->mmap(0, regionSize, PROT_READ, MAP_SHARED, mappedFd, 0)); |
| 126 | |
| 127 | if (mappedRegion == MAP_FAILED) |
| 128 | { |
| 129 | sys->close(mappedFd); |
| 130 | mappedFd = -1; |
| 131 | std::fprintf(stderr, "Mmap failure: '%s'\n", std::strerror(errno)); |
| 132 | return false; |
| 133 | } |
| 134 | |
| 135 | /* TOOD: There is no close() method here, to close mappedFd, or mappedRegion |
| 136 | * -- therefore, a good next step will be to evaluate whether or not the |
| 137 | * other pieces should go here... |
| 138 | */ |
| 139 | return true; |
| 140 | } |
| 141 | |
Patrick Venture | 517710d | 2019-01-17 11:37:40 -0800 | [diff] [blame] | 142 | std::vector<std::uint8_t> LpcMapperAspeed::copyFrom(std::uint32_t length) |
| 143 | { |
Patrick Venture | a9e0005 | 2019-01-17 12:56:56 -0800 | [diff] [blame] | 144 | if (mappedFd < 0) |
| 145 | { |
| 146 | /* NOTE: may make more sense to do this in the open() */ |
| 147 | if (!mapRegion()) |
| 148 | { |
| 149 | /* Was unable to map region -- this call only required if using mmap |
| 150 | * and not ioctl. |
| 151 | */ |
| 152 | /* TODO: have a better failure. */ |
| 153 | return {}; |
| 154 | } |
| 155 | } |
| 156 | |
Patrick Venture | 439e23f | 2019-01-17 13:30:41 -0800 | [diff] [blame] | 157 | return std::vector<std::uint8_t>(mappedRegion, mappedRegion + length); |
Patrick Venture | 517710d | 2019-01-17 11:37:40 -0800 | [diff] [blame] | 158 | } |
| 159 | |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame^] | 160 | } // namespace ipmi_flash |