blob: 75313824ecf2edc3a5e7b817768198c3b375b7d0 [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
31namespace blobs
32{
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{
48}
49
Patrick Venturee7728422018-11-14 20:16:33 -080050std::pair<std::uint32_t, std::uint32_t>
51 LpcMapperAspeed::mapWindow(std::uint32_t address, std::uint32_t length)
52{
Patrick Venture7b91cbc2018-11-28 14:24:41 -080053 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 Venturebf064632019-01-17 12:20:21 -080084 const auto lpcControlFd = sys->open(lpcControlPath.c_str(), O_RDWR);
Patrick Venture7b91cbc2018-11-28 14:24:41 -080085 if (lpcControlFd == -1)
86 {
87 std::fprintf(stderr,
88 "cannot open Aspeed LPC kernel control dev \"%s\"\n",
Patrick Venturebf064632019-01-17 12:20:21 -080089 lpcControlPath.c_str());
Patrick Venture7b91cbc2018-11-28 14:24:41 -080090 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 Venturee7728422018-11-14 20:16:33 -0800103}
104
Patrick Venturea9e00052019-01-17 12:56:56 -0800105bool LpcMapperAspeed::mapRegion()
106{
107 /* Open the file to map. */
108 mappedFd = sys->open(lpcControlPath.c_str(), O_RDONLY | O_SYNC);
109
110 mappedRegion = reinterpret_cast<uint8_t*>(
111 sys->mmap(0, regionSize, PROT_READ, MAP_SHARED, mappedFd, 0));
112
113 if (mappedRegion == MAP_FAILED)
114 {
115 sys->close(mappedFd);
116 mappedFd = -1;
117 std::fprintf(stderr, "Mmap failure: '%s'\n", std::strerror(errno));
118 return false;
119 }
120
121 /* TOOD: There is no close() method here, to close mappedFd, or mappedRegion
122 * -- therefore, a good next step will be to evaluate whether or not the
123 * other pieces should go here...
124 */
125 return true;
126}
127
Patrick Venture517710d2019-01-17 11:37:40 -0800128std::vector<std::uint8_t> LpcMapperAspeed::copyFrom(std::uint32_t length)
129{
Patrick Venturea9e00052019-01-17 12:56:56 -0800130 if (mappedFd < 0)
131 {
132 /* NOTE: may make more sense to do this in the open() */
133 if (!mapRegion())
134 {
135 /* Was unable to map region -- this call only required if using mmap
136 * and not ioctl.
137 */
138 /* TODO: have a better failure. */
139 return {};
140 }
141 }
142
Patrick Venture517710d2019-01-17 11:37:40 -0800143 /* TODO: Implement this. */
144 return {};
145}
146
Patrick Venturee7728422018-11-14 20:16:33 -0800147} // namespace blobs