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 | 28abae7 | 2018-12-14 09:44:02 -0800 | [diff] [blame] | 37 | LpcMapperAspeed::createAspeedMapper(std::size_t regionSize) |
Patrick Venture | e772842 | 2018-11-14 20:16:33 -0800 | [diff] [blame] | 38 | { |
| 39 | /* NOTE: considered using a joint factory to create one or the other, for |
| 40 | * now, separate factories. |
| 41 | */ |
Patrick Venture | 7b91cbc | 2018-11-28 14:24:41 -0800 | [diff] [blame] | 42 | return std::make_unique<LpcMapperAspeed>(regionSize); |
Patrick Venture | e772842 | 2018-11-14 20:16:33 -0800 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | std::pair<std::uint32_t, std::uint32_t> |
| 46 | LpcMapperAspeed::mapWindow(std::uint32_t address, std::uint32_t length) |
| 47 | { |
Patrick Venture | 7b91cbc | 2018-11-28 14:24:41 -0800 | [diff] [blame] | 48 | static const std::uint32_t MASK_64K = 0xFFFFU; |
| 49 | const std::uint32_t offset = address & MASK_64K; |
| 50 | |
| 51 | if (offset + length > regionSize) |
| 52 | { |
| 53 | std::fprintf(stderr, |
| 54 | "requested window size %" PRIu32 ", offset %#" PRIx32 |
| 55 | " is too large for mem region" |
| 56 | " of size %zu\n", |
| 57 | length, offset, regionSize); |
| 58 | /* TODO: need to throw an exception at this point to store the data to |
| 59 | * provide an EBIG response later. |
| 60 | */ |
| 61 | /* *windowSize = regionSize - offset; */ |
| 62 | return std::make_pair(0, 0); |
| 63 | } |
| 64 | |
| 65 | struct aspeed_lpc_ctrl_mapping map = { |
| 66 | .window_type = ASPEED_LPC_CTRL_WINDOW_MEMORY, |
| 67 | .window_id = 0, |
| 68 | .flags = 0, |
| 69 | .addr = address & ~MASK_64K, |
| 70 | .offset = 0, |
| 71 | .size = __ALIGN_KERNEL_MASK(offset + length, MASK_64K), |
| 72 | }; |
| 73 | |
| 74 | std::fprintf(stderr, |
| 75 | "requesting Aspeed LPC window at %#" PRIx32 " of size %" PRIu32 |
| 76 | "\n", |
| 77 | map.addr, map.size); |
| 78 | |
Patrick Venture | bf06463 | 2019-01-17 12:20:21 -0800 | [diff] [blame^] | 79 | const auto lpcControlFd = sys->open(lpcControlPath.c_str(), O_RDWR); |
Patrick Venture | 7b91cbc | 2018-11-28 14:24:41 -0800 | [diff] [blame] | 80 | if (lpcControlFd == -1) |
| 81 | { |
| 82 | std::fprintf(stderr, |
| 83 | "cannot open Aspeed LPC kernel control dev \"%s\"\n", |
Patrick Venture | bf06463 | 2019-01-17 12:20:21 -0800 | [diff] [blame^] | 84 | lpcControlPath.c_str()); |
Patrick Venture | 7b91cbc | 2018-11-28 14:24:41 -0800 | [diff] [blame] | 85 | return std::make_pair(0, 0); |
| 86 | } |
| 87 | |
| 88 | if (sys->ioctl(lpcControlFd, ASPEED_LPC_CTRL_IOCTL_MAP, &map) == -1) |
| 89 | { |
| 90 | std::fprintf(stderr, "Failed to ioctl Aspeed LPC map with error %s\n", |
| 91 | std::strerror(errno)); |
| 92 | sys->close(lpcControlFd); |
| 93 | return std::make_pair(0, 0); |
| 94 | } |
| 95 | |
| 96 | sys->close(lpcControlFd); |
| 97 | return std::make_pair(offset, length); |
Patrick Venture | e772842 | 2018-11-14 20:16:33 -0800 | [diff] [blame] | 98 | } |
| 99 | |
Patrick Venture | 517710d | 2019-01-17 11:37:40 -0800 | [diff] [blame] | 100 | std::vector<std::uint8_t> LpcMapperAspeed::copyFrom(std::uint32_t length) |
| 101 | { |
| 102 | /* TODO: Implement this. */ |
| 103 | return {}; |
| 104 | } |
| 105 | |
Patrick Venture | e772842 | 2018-11-14 20:16:33 -0800 | [diff] [blame] | 106 | } // namespace blobs |