blob: 184f18a5763eb900c7304ddd6cbcf376a4dc50a9 [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 Venture5251da92019-01-17 11:25:26 -080019#include "window_hw_interface.hpp"
Patrick Venturee7728422018-11-14 20:16:33 -080020
Patrick Venture7b91cbc2018-11-28 14:24:41 -080021#include <fcntl.h>
22#include <linux/aspeed-lpc-ctrl.h>
23#include <linux/kernel.h>
24
Patrick Venturee7728422018-11-14 20:16:33 -080025#include <cstdint>
Patrick Venture7b91cbc2018-11-28 14:24:41 -080026#include <cstring>
Patrick Venturee7728422018-11-14 20:16:33 -080027#include <memory>
Patrick Venturebf064632019-01-17 12:20:21 -080028#include <string>
Patrick Venturee7728422018-11-14 20:16:33 -080029#include <utility>
30
Patrick Venture1d5a31c2019-05-20 11:38:22 -070031namespace ipmi_flash
Patrick Venturee7728422018-11-14 20:16:33 -080032{
33
Patrick Venturebf064632019-01-17 12:20:21 -080034const std::string LpcMapperAspeed::lpcControlPath = "/dev/aspeed-lpc-ctrl";
35
Patrick Venture5251da92019-01-17 11:25:26 -080036std::unique_ptr<HardwareMapperInterface>
Patrick Venture78b1a662019-01-17 12:38:26 -080037 LpcMapperAspeed::createAspeedMapper(std::uint32_t regionAddress,
38 std::size_t regionSize)
Patrick Venturee7728422018-11-14 20:16:33 -080039{
40 /* NOTE: considered using a joint factory to create one or the other, for
41 * now, separate factories.
42 */
Patrick Venture78b1a662019-01-17 12:38:26 -080043 return std::make_unique<LpcMapperAspeed>(regionAddress, regionSize);
Patrick Venturee7728422018-11-14 20:16:33 -080044}
45
Patrick Venture2343cfc2019-01-17 13:04:36 -080046void LpcMapperAspeed::close()
47{
Patrick Venture5e022152019-01-17 13:19:00 -080048 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 Venture2343cfc2019-01-17 13:04:36 -080059}
60
Patrick Venturee7728422018-11-14 20:16:33 -080061std::pair<std::uint32_t, std::uint32_t>
62 LpcMapperAspeed::mapWindow(std::uint32_t address, std::uint32_t length)
63{
Patrick Venture7b91cbc2018-11-28 14:24:41 -080064 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 Venturebf064632019-01-17 12:20:21 -080095 const auto lpcControlFd = sys->open(lpcControlPath.c_str(), O_RDWR);
Patrick Venture7b91cbc2018-11-28 14:24:41 -080096 if (lpcControlFd == -1)
97 {
98 std::fprintf(stderr,
99 "cannot open Aspeed LPC kernel control dev \"%s\"\n",
Patrick Venturebf064632019-01-17 12:20:21 -0800100 lpcControlPath.c_str());
Patrick Venture7b91cbc2018-11-28 14:24:41 -0800101 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 Venturee7728422018-11-14 20:16:33 -0800114}
115
Patrick Venturea9e00052019-01-17 12:56:56 -0800116bool LpcMapperAspeed::mapRegion()
117{
118 /* Open the file to map. */
119 mappedFd = sys->open(lpcControlPath.c_str(), O_RDONLY | O_SYNC);
120
Patrick Venture98af2d12019-02-26 09:41:28 -0800121 /* 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 Venturea9e00052019-01-17 12:56:56 -0800124 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 Venture517710d2019-01-17 11:37:40 -0800142std::vector<std::uint8_t> LpcMapperAspeed::copyFrom(std::uint32_t length)
143{
Patrick Venturea9e00052019-01-17 12:56:56 -0800144 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 Venture439e23f2019-01-17 13:30:41 -0800157 return std::vector<std::uint8_t>(mappedRegion, mappedRegion + length);
Patrick Venture517710d2019-01-17 11:37:40 -0800158}
159
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700160} // namespace ipmi_flash