blob: 120ac76caf37e4a4637f343c4189267c8b3643d2 [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
19#include "lpc_interface.hpp"
20
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>
28#include <utility>
29
30namespace blobs
31{
32
Patrick Venture7b91cbc2018-11-28 14:24:41 -080033std::unique_ptr<LpcMapperInterface>
Patrick Venture28abae72018-12-14 09:44:02 -080034 LpcMapperAspeed::createAspeedMapper(std::size_t regionSize)
Patrick Venturee7728422018-11-14 20:16:33 -080035{
36 /* NOTE: considered using a joint factory to create one or the other, for
37 * now, separate factories.
38 */
Patrick Venture7b91cbc2018-11-28 14:24:41 -080039 return std::make_unique<LpcMapperAspeed>(regionSize);
Patrick Venturee7728422018-11-14 20:16:33 -080040}
41
42std::pair<std::uint32_t, std::uint32_t>
43 LpcMapperAspeed::mapWindow(std::uint32_t address, std::uint32_t length)
44{
Patrick Venture7b91cbc2018-11-28 14:24:41 -080045 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 Venturee7728422018-11-14 20:16:33 -080097}
98
99} // namespace blobs