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 | 517710d | 2019-01-17 11:37:40 -0800 | [diff] [blame] | 105 | std::vector<std::uint8_t> LpcMapperAspeed::copyFrom(std::uint32_t length) |
| 106 | { |
| 107 | /* TODO: Implement this. */ |
| 108 | return {}; |
| 109 | } |
| 110 | |
Patrick Venture | e772842 | 2018-11-14 20:16:33 -0800 | [diff] [blame] | 111 | } // namespace blobs |