blob: eaa316f9f1f7d2badf99a3e41b5d67051b476fb4 [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_nuvoton.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 Venture38613e12018-11-16 20:45:16 -080022#include <fcntl.h>
23#include <sys/mman.h>
Patrick Venture38613e12018-11-16 20:45:16 -080024
Patrick Venture36bffb42019-06-24 10:47:47 -070025#include <cerrno>
Patrick Venture38613e12018-11-16 20:45:16 -080026#include <cinttypes>
Patrick Venturee7728422018-11-14 20:16:33 -080027#include <cstdint>
Patrick Venture38613e12018-11-16 20:45:16 -080028#include <cstdio>
Patrick Venturee7728422018-11-14 20:16:33 -080029#include <memory>
30#include <utility>
Patrick Venturec9c60882019-01-17 11:44:47 -080031#include <vector>
Patrick Venturee7728422018-11-14 20:16:33 -080032
Patrick Venture1d5a31c2019-05-20 11:38:22 -070033namespace ipmi_flash
Patrick Venturee7728422018-11-14 20:16:33 -080034{
Patrick Venture38613e12018-11-16 20:45:16 -080035using std::uint16_t;
36using std::uint32_t;
37using std::uint8_t;
Patrick Venturee7728422018-11-14 20:16:33 -080038
Patrick Venture78b1a662019-01-17 12:38:26 -080039std::unique_ptr<HardwareMapperInterface>
Patrick Venture36bffb42019-06-24 10:47:47 -070040 LpcMapperNuvoton::createNuvotonMapper(std::uint32_t regionAddress,
41 std::uint32_t regionSize)
Patrick Venturee7728422018-11-14 20:16:33 -080042{
43 /* NOTE: Considered making one factory for both types. */
Patrick Venture36bffb42019-06-24 10:47:47 -070044 return std::make_unique<LpcMapperNuvoton>(regionAddress, regionSize);
45}
46
47MemorySet LpcMapperNuvoton::open()
48{
49 static constexpr auto devmem = "/dev/mem";
50
51 mappedFd = sys->open(devmem, O_RDWR | O_SYNC);
52 if (mappedFd == -1)
53 {
54 throw MapperException("Unable to open /dev/mem");
55 }
56
57 mapped = reinterpret_cast<uint8_t*>(sys->mmap(
58 0, memoryRegionSize, PROT_READ, MAP_SHARED, mappedFd, regionAddress));
59 if (mapped == MAP_FAILED)
60 {
61 sys->close(mappedFd);
62 mappedFd = -1;
63 mapped = nullptr;
64
65 throw MapperException("Unable to map region");
66 }
67
68 MemorySet output;
69 output.mappedFd = mappedFd;
70 output.mapped = mapped;
71
72 return output;
Patrick Venturee7728422018-11-14 20:16:33 -080073}
74
Patrick Venture2343cfc2019-01-17 13:04:36 -080075void LpcMapperNuvoton::close()
76{
Patrick Venture36bffb42019-06-24 10:47:47 -070077 if (mapped)
78 {
79 sys->munmap(mapped, memoryRegionSize);
80 mapped = nullptr;
81 }
82
83 if (mappedFd != -1)
84 {
85 sys->close(mappedFd);
86 mappedFd = -1;
87 }
Patrick Venture2343cfc2019-01-17 13:04:36 -080088}
89
Patrick Venture38613e12018-11-16 20:45:16 -080090/*
91 * The host buffer address is configured by host through
92 * SuperIO. On BMC side the max memory can be mapped is 4kB with the caveat that
93 * first byte of the buffer is reserved as host/BMC semaphore and not usable as
94 * shared memory.
95 *
96 * Mapper returns success for (addr, len) where (addr & 0x7) == 4 and len <=
97 * (4096 - 4). Otherwise, mapper returns either
98 * - WindowOffset = 4 and WindowSize = len - 4 if (addr & 0x7) == 0
99 * - WindowSize = 0 means that the region cannot be mapped otherwise
100 */
Patrick Venture36bffb42019-06-24 10:47:47 -0700101WindowMapResult LpcMapperNuvoton::mapWindow(std::uint32_t address,
102 std::uint32_t length)
Patrick Venturee7728422018-11-14 20:16:33 -0800103{
Patrick Venture36bffb42019-06-24 10:47:47 -0700104 WindowMapResult result = {};
105
Patrick Venture38613e12018-11-16 20:45:16 -0800106 /* We reserve the first 4 bytes from the mapped region; the first byte
107 * is shared semaphore, and the number of 4 is for alignment.
108 */
109 const uint32_t bmcMapReserveBytes = 4;
110 const uint32_t bmcMapMaxSizeBytes = 4 * 1024 - bmcMapReserveBytes;
111
112 if (length <= bmcMapReserveBytes)
113 {
114 std::fprintf(stderr, "window size %" PRIx32 " too small to map.\n",
115 length);
Patrick Venture36bffb42019-06-24 10:47:47 -0700116 result.response = EINVAL;
117 return result;
Patrick Venture38613e12018-11-16 20:45:16 -0800118 }
119
120 if (length > bmcMapMaxSizeBytes)
121 {
122 std::fprintf(stderr,
123 "window size %" PRIx32 " not supported. Max size 4k.\n",
124 length);
125 length = bmcMapMaxSizeBytes;
126 }
127
128 /* If host requested region starts at an aligned address, return offset of 4
129 * bytes so as to skip the semaphore register.
130 */
131 uint32_t windowOffset = bmcMapReserveBytes;
Patrick Venture36bffb42019-06-24 10:47:47 -0700132 // uint32_t windowSize = length;
133
134 result.response = 0;
135 result.windowOffset = windowOffset;
136 result.windowSize = length;
Patrick Venture38613e12018-11-16 20:45:16 -0800137
138 const uint32_t addressOffset = address & 0x7;
139
140 if (addressOffset == 0)
141 {
142 std::fprintf(stderr, "Map address offset should be 4 for Nuvoton.\n");
Patrick Venture36bffb42019-06-24 10:47:47 -0700143
144 result.response = EFBIG;
145 return result;
Patrick Venture38613e12018-11-16 20:45:16 -0800146 }
147 else if (addressOffset != bmcMapReserveBytes)
148 {
149 std::fprintf(stderr, "Map address offset should be 4 for Nuvoton.\n");
Patrick Venture36bffb42019-06-24 10:47:47 -0700150
151 result.response = EINVAL;
152 return result;
Patrick Venture38613e12018-11-16 20:45:16 -0800153 }
154
155 /* TODO: need a kernel driver to handle mapping configuration.
156 * Until then program the register through /dev/mem.
157 */
158 int fd;
Patrick Venture8b588562018-11-18 08:44:33 -0800159 if ((fd = sys->open("/dev/mem", O_RDWR | O_SYNC)) == -1)
Patrick Venture38613e12018-11-16 20:45:16 -0800160 {
161 std::fprintf(stderr, "Failed to open /dev/mem\n");
Patrick Venture8b588562018-11-18 08:44:33 -0800162 sys->close(fd);
Patrick Venture36bffb42019-06-24 10:47:47 -0700163
164 result.response = EINVAL;
165 return result;
Patrick Venture38613e12018-11-16 20:45:16 -0800166 }
167
168 const uint32_t bmcMapConfigBaseAddr = 0xc0001000;
169 const uint32_t bmcMapConfigWindowSizeOffset = 0x7;
170 const uint32_t bmcMapConfigWindowBaseOffset = 0xa;
171 const uint8_t bmcWindowSizeValue = 0xc; // 4k
172 const uint16_t bmcWindowBaseValue = 0x8000; // BMC phyAddr from 0xc0008000
173
Patrick Venture8b588562018-11-18 08:44:33 -0800174 int pageSize = sys->getpagesize();
175
Patrick Venture38613e12018-11-16 20:45:16 -0800176 auto mapBasePtr = reinterpret_cast<uint8_t*>(
Patrick Venture8b588562018-11-18 08:44:33 -0800177 sys->mmap(nullptr, pageSize, PROT_READ | PROT_WRITE, MAP_SHARED, fd,
178 bmcMapConfigBaseAddr));
Patrick Venture38613e12018-11-16 20:45:16 -0800179
180 uint8_t* bmcWindowSize = mapBasePtr + bmcMapConfigWindowSizeOffset;
181 uint16_t* bmcWindowBase =
182 reinterpret_cast<uint16_t*>(mapBasePtr + bmcMapConfigWindowBaseOffset);
183
184 *bmcWindowSize = bmcWindowSizeValue;
185 *bmcWindowBase = bmcWindowBaseValue;
186
Patrick Venture8b588562018-11-18 08:44:33 -0800187 sys->munmap(mapBasePtr, pageSize);
188 sys->close(fd);
Patrick Venture38613e12018-11-16 20:45:16 -0800189
Patrick Venture36bffb42019-06-24 10:47:47 -0700190 return result;
Patrick Venture517710d2019-01-17 11:37:40 -0800191}
192
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700193} // namespace ipmi_flash