blob: 364aff57cb4efa8b17bdf3c366b24472785426ab [file] [log] [blame]
Patrick Venture22e38752018-11-21 08:52:49 -08001/*
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 Venturee7728422018-11-14 20:16:33 -080017#include "lpc_aspeed.hpp"
18
Patrick Venture36bffb42019-06-24 10:47:47 -070019#include "mapper_errors.hpp"
Patrick Venture5251da92019-01-17 11:25:26 -080020#include "window_hw_interface.hpp"
Patrick Venturee7728422018-11-14 20:16:33 -080021
Patrick Venture7b91cbc2018-11-28 14:24:41 -080022#include <fcntl.h>
23#include <linux/aspeed-lpc-ctrl.h>
24#include <linux/kernel.h>
25
Patrick Venture36bffb42019-06-24 10:47:47 -070026#include <cerrno>
Patrick Venturee7728422018-11-14 20:16:33 -080027#include <cstdint>
Patrick Venture7b91cbc2018-11-28 14:24:41 -080028#include <cstring>
Patrick Venturee7728422018-11-14 20:16:33 -080029#include <memory>
Patrick Venturebf064632019-01-17 12:20:21 -080030#include <string>
Patrick Venturee7728422018-11-14 20:16:33 -080031#include <utility>
32
Patrick Venture1d5a31c2019-05-20 11:38:22 -070033namespace ipmi_flash
Patrick Venturee7728422018-11-14 20:16:33 -080034{
35
Patrick Venturebf064632019-01-17 12:20:21 -080036const std::string LpcMapperAspeed::lpcControlPath = "/dev/aspeed-lpc-ctrl";
37
Patrick Venture5251da92019-01-17 11:25:26 -080038std::unique_ptr<HardwareMapperInterface>
Patrick Venture78b1a662019-01-17 12:38:26 -080039 LpcMapperAspeed::createAspeedMapper(std::uint32_t regionAddress,
40 std::size_t regionSize)
Patrick Venturee7728422018-11-14 20:16:33 -080041{
42 /* NOTE: considered using a joint factory to create one or the other, for
43 * now, separate factories.
44 */
Patrick Venture78b1a662019-01-17 12:38:26 -080045 return std::make_unique<LpcMapperAspeed>(regionAddress, regionSize);
Patrick Venturee7728422018-11-14 20:16:33 -080046}
47
Patrick Venture2343cfc2019-01-17 13:04:36 -080048void LpcMapperAspeed::close()
49{
Patrick Venture5e022152019-01-17 13:19:00 -080050 if (mappedRegion)
51 {
52 sys->munmap(mappedRegion, regionSize);
53 mappedRegion = nullptr;
54 }
55
56 if (mappedFd != -1)
57 {
58 sys->close(mappedFd);
59 mappedFd = -1;
60 }
Patrick Venture2343cfc2019-01-17 13:04:36 -080061}
62
Patrick Venture36bffb42019-06-24 10:47:47 -070063WindowMapResult LpcMapperAspeed::mapWindow(std::uint32_t address,
64 std::uint32_t length)
Patrick Venturee7728422018-11-14 20:16:33 -080065{
Patrick Venture36bffb42019-06-24 10:47:47 -070066 WindowMapResult result = {};
Patrick Venture7b91cbc2018-11-28 14:24:41 -080067 static const std::uint32_t MASK_64K = 0xFFFFU;
68 const std::uint32_t offset = address & MASK_64K;
69
70 if (offset + length > regionSize)
71 {
72 std::fprintf(stderr,
73 "requested window size %" PRIu32 ", offset %#" PRIx32
74 " is too large for mem region"
75 " of size %zu\n",
76 length, offset, regionSize);
Patrick Venture36bffb42019-06-24 10:47:47 -070077
78 result.response = EFBIG;
79 result.windowSize = regionSize - offset;
80 return result;
Patrick Venture7b91cbc2018-11-28 14:24:41 -080081 }
82
83 struct aspeed_lpc_ctrl_mapping map = {
84 .window_type = ASPEED_LPC_CTRL_WINDOW_MEMORY,
85 .window_id = 0,
86 .flags = 0,
87 .addr = address & ~MASK_64K,
88 .offset = 0,
89 .size = __ALIGN_KERNEL_MASK(offset + length, MASK_64K),
90 };
91
92 std::fprintf(stderr,
93 "requesting Aspeed LPC window at %#" PRIx32 " of size %" PRIu32
94 "\n",
95 map.addr, map.size);
96
Patrick Venturebf064632019-01-17 12:20:21 -080097 const auto lpcControlFd = sys->open(lpcControlPath.c_str(), O_RDWR);
Patrick Venture7b91cbc2018-11-28 14:24:41 -080098 if (lpcControlFd == -1)
99 {
100 std::fprintf(stderr,
101 "cannot open Aspeed LPC kernel control dev \"%s\"\n",
Patrick Venturebf064632019-01-17 12:20:21 -0800102 lpcControlPath.c_str());
Patrick Venture36bffb42019-06-24 10:47:47 -0700103
104 result.response = EINVAL;
105 return result;
Patrick Venture7b91cbc2018-11-28 14:24:41 -0800106 }
107
108 if (sys->ioctl(lpcControlFd, ASPEED_LPC_CTRL_IOCTL_MAP, &map) == -1)
109 {
110 std::fprintf(stderr, "Failed to ioctl Aspeed LPC map with error %s\n",
111 std::strerror(errno));
112 sys->close(lpcControlFd);
Patrick Venture36bffb42019-06-24 10:47:47 -0700113
114 result.response = EINVAL;
115 return result;
Patrick Venture7b91cbc2018-11-28 14:24:41 -0800116 }
117
118 sys->close(lpcControlFd);
Patrick Venture36bffb42019-06-24 10:47:47 -0700119
120 result.response = 0;
121 result.windowOffset = offset;
122 result.windowSize = length;
123 return result;
124}
125
126MemorySet LpcMapperAspeed::open()
127{
128 if (mapRegion())
129 {
130 MemorySet output;
131 output.mappedFd = mappedFd;
132 output.mapped = mappedRegion;
133 return output;
134 }
135
136 throw MapperException("Unable to memory-map region");
Patrick Venturee7728422018-11-14 20:16:33 -0800137}
138
Patrick Venturea9e00052019-01-17 12:56:56 -0800139bool LpcMapperAspeed::mapRegion()
140{
141 /* Open the file to map. */
142 mappedFd = sys->open(lpcControlPath.c_str(), O_RDONLY | O_SYNC);
Patrick Ventureae0941a2019-06-26 11:51:33 -0700143 if (mappedFd == -1)
144 {
145 std::fprintf(stderr, "ipmiflash: unable to open %s\n",
146 lpcControlPath.c_str());
147 return false;
148 }
Patrick Venturea9e00052019-01-17 12:56:56 -0800149
150 mappedRegion = reinterpret_cast<uint8_t*>(
151 sys->mmap(0, regionSize, PROT_READ, MAP_SHARED, mappedFd, 0));
152
153 if (mappedRegion == MAP_FAILED)
154 {
155 sys->close(mappedFd);
156 mappedFd = -1;
157 std::fprintf(stderr, "Mmap failure: '%s'\n", std::strerror(errno));
158 return false;
159 }
160
161 /* TOOD: There is no close() method here, to close mappedFd, or mappedRegion
162 * -- therefore, a good next step will be to evaluate whether or not the
163 * other pieces should go here...
164 */
165 return true;
166}
167
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700168} // namespace ipmi_flash