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