blob: ebc210ce00caddf4c17ae4e3d37e833314dcce34 [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 Venture5251da92019-01-17 11:25:26 -080019#include "window_hw_interface.hpp"
Patrick Venturee7728422018-11-14 20:16:33 -080020
Patrick Venture38613e12018-11-16 20:45:16 -080021#include <fcntl.h>
22#include <sys/mman.h>
Patrick Venture38613e12018-11-16 20:45:16 -080023
24#include <cinttypes>
Patrick Venturee7728422018-11-14 20:16:33 -080025#include <cstdint>
Patrick Venture38613e12018-11-16 20:45:16 -080026#include <cstdio>
Patrick Venturee7728422018-11-14 20:16:33 -080027#include <memory>
28#include <utility>
29
30namespace blobs
31{
Patrick Venture38613e12018-11-16 20:45:16 -080032using std::uint16_t;
33using std::uint32_t;
34using std::uint8_t;
Patrick Venturee7728422018-11-14 20:16:33 -080035
Patrick Venture5251da92019-01-17 11:25:26 -080036std::unique_ptr<HardwareMapperInterface> LpcMapperNuvoton::createNuvotonMapper()
Patrick Venturee7728422018-11-14 20:16:33 -080037{
38 /* NOTE: Considered making one factory for both types. */
39 return std::make_unique<LpcMapperNuvoton>();
40}
41
Patrick Venture38613e12018-11-16 20:45:16 -080042/*
43 * The host buffer address is configured by host through
44 * SuperIO. On BMC side the max memory can be mapped is 4kB with the caveat that
45 * first byte of the buffer is reserved as host/BMC semaphore and not usable as
46 * shared memory.
47 *
48 * Mapper returns success for (addr, len) where (addr & 0x7) == 4 and len <=
49 * (4096 - 4). Otherwise, mapper returns either
50 * - WindowOffset = 4 and WindowSize = len - 4 if (addr & 0x7) == 0
51 * - WindowSize = 0 means that the region cannot be mapped otherwise
52 */
Patrick Venturee7728422018-11-14 20:16:33 -080053std::pair<std::uint32_t, std::uint32_t>
54 LpcMapperNuvoton::mapWindow(std::uint32_t address, std::uint32_t length)
55{
Patrick Venture38613e12018-11-16 20:45:16 -080056 /* We reserve the first 4 bytes from the mapped region; the first byte
57 * is shared semaphore, and the number of 4 is for alignment.
58 */
59 const uint32_t bmcMapReserveBytes = 4;
60 const uint32_t bmcMapMaxSizeBytes = 4 * 1024 - bmcMapReserveBytes;
61
62 if (length <= bmcMapReserveBytes)
63 {
64 std::fprintf(stderr, "window size %" PRIx32 " too small to map.\n",
65 length);
66 return std::make_pair(0, 0);
67 }
68
69 if (length > bmcMapMaxSizeBytes)
70 {
71 std::fprintf(stderr,
72 "window size %" PRIx32 " not supported. Max size 4k.\n",
73 length);
74 length = bmcMapMaxSizeBytes;
75 }
76
77 /* If host requested region starts at an aligned address, return offset of 4
78 * bytes so as to skip the semaphore register.
79 */
80 uint32_t windowOffset = bmcMapReserveBytes;
81 uint32_t windowSize = length;
82
83 const uint32_t addressOffset = address & 0x7;
84
85 if (addressOffset == 0)
86 {
87 std::fprintf(stderr, "Map address offset should be 4 for Nuvoton.\n");
88 return std::make_pair(0, 0);
89 }
90 else if (addressOffset != bmcMapReserveBytes)
91 {
92 std::fprintf(stderr, "Map address offset should be 4 for Nuvoton.\n");
93 return std::make_pair(0, 0);
94 }
95
96 /* TODO: need a kernel driver to handle mapping configuration.
97 * Until then program the register through /dev/mem.
98 */
99 int fd;
Patrick Venture8b588562018-11-18 08:44:33 -0800100 if ((fd = sys->open("/dev/mem", O_RDWR | O_SYNC)) == -1)
Patrick Venture38613e12018-11-16 20:45:16 -0800101 {
102 std::fprintf(stderr, "Failed to open /dev/mem\n");
Patrick Venture8b588562018-11-18 08:44:33 -0800103 sys->close(fd);
Patrick Venture38613e12018-11-16 20:45:16 -0800104 return std::make_pair(0, 0);
105 }
106
107 const uint32_t bmcMapConfigBaseAddr = 0xc0001000;
108 const uint32_t bmcMapConfigWindowSizeOffset = 0x7;
109 const uint32_t bmcMapConfigWindowBaseOffset = 0xa;
110 const uint8_t bmcWindowSizeValue = 0xc; // 4k
111 const uint16_t bmcWindowBaseValue = 0x8000; // BMC phyAddr from 0xc0008000
112
Patrick Venture8b588562018-11-18 08:44:33 -0800113 int pageSize = sys->getpagesize();
114
Patrick Venture38613e12018-11-16 20:45:16 -0800115 auto mapBasePtr = reinterpret_cast<uint8_t*>(
Patrick Venture8b588562018-11-18 08:44:33 -0800116 sys->mmap(nullptr, pageSize, PROT_READ | PROT_WRITE, MAP_SHARED, fd,
117 bmcMapConfigBaseAddr));
Patrick Venture38613e12018-11-16 20:45:16 -0800118
119 uint8_t* bmcWindowSize = mapBasePtr + bmcMapConfigWindowSizeOffset;
120 uint16_t* bmcWindowBase =
121 reinterpret_cast<uint16_t*>(mapBasePtr + bmcMapConfigWindowBaseOffset);
122
123 *bmcWindowSize = bmcWindowSizeValue;
124 *bmcWindowBase = bmcWindowBaseValue;
125
Patrick Venture8b588562018-11-18 08:44:33 -0800126 sys->munmap(mapBasePtr, pageSize);
127 sys->close(fd);
Patrick Venture38613e12018-11-16 20:45:16 -0800128
129 return std::make_pair(windowOffset, windowSize);
Patrick Venturee7728422018-11-14 20:16:33 -0800130}
131
Patrick Venture517710d2019-01-17 11:37:40 -0800132std::vector<std::uint8_t> LpcMapperNuvoton::copyFrom(std::uint32_t length)
133{
134 /* TODO: implement. */
135 return {};
136}
137
Patrick Venturee7728422018-11-14 20:16:33 -0800138} // namespace blobs