blob: 80c3245b295f2d19141908b7eef0799c16f55a42 [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>
Patrick Venturec9c60882019-01-17 11:44:47 -080029#include <vector>
Patrick Venturee7728422018-11-14 20:16:33 -080030
31namespace blobs
32{
Patrick Venture38613e12018-11-16 20:45:16 -080033using std::uint16_t;
34using std::uint32_t;
35using std::uint8_t;
Patrick Venturee7728422018-11-14 20:16:33 -080036
Patrick Venture5251da92019-01-17 11:25:26 -080037std::unique_ptr<HardwareMapperInterface> LpcMapperNuvoton::createNuvotonMapper()
Patrick Venturee7728422018-11-14 20:16:33 -080038{
39 /* NOTE: Considered making one factory for both types. */
40 return std::make_unique<LpcMapperNuvoton>();
41}
42
Patrick Venture38613e12018-11-16 20:45:16 -080043/*
44 * The host buffer address is configured by host through
45 * SuperIO. On BMC side the max memory can be mapped is 4kB with the caveat that
46 * first byte of the buffer is reserved as host/BMC semaphore and not usable as
47 * shared memory.
48 *
49 * Mapper returns success for (addr, len) where (addr & 0x7) == 4 and len <=
50 * (4096 - 4). Otherwise, mapper returns either
51 * - WindowOffset = 4 and WindowSize = len - 4 if (addr & 0x7) == 0
52 * - WindowSize = 0 means that the region cannot be mapped otherwise
53 */
Patrick Venturee7728422018-11-14 20:16:33 -080054std::pair<std::uint32_t, std::uint32_t>
55 LpcMapperNuvoton::mapWindow(std::uint32_t address, std::uint32_t length)
56{
Patrick Venture38613e12018-11-16 20:45:16 -080057 /* We reserve the first 4 bytes from the mapped region; the first byte
58 * is shared semaphore, and the number of 4 is for alignment.
59 */
60 const uint32_t bmcMapReserveBytes = 4;
61 const uint32_t bmcMapMaxSizeBytes = 4 * 1024 - bmcMapReserveBytes;
62
63 if (length <= bmcMapReserveBytes)
64 {
65 std::fprintf(stderr, "window size %" PRIx32 " too small to map.\n",
66 length);
67 return std::make_pair(0, 0);
68 }
69
70 if (length > bmcMapMaxSizeBytes)
71 {
72 std::fprintf(stderr,
73 "window size %" PRIx32 " not supported. Max size 4k.\n",
74 length);
75 length = bmcMapMaxSizeBytes;
76 }
77
78 /* If host requested region starts at an aligned address, return offset of 4
79 * bytes so as to skip the semaphore register.
80 */
81 uint32_t windowOffset = bmcMapReserveBytes;
82 uint32_t windowSize = length;
83
84 const uint32_t addressOffset = address & 0x7;
85
86 if (addressOffset == 0)
87 {
88 std::fprintf(stderr, "Map address offset should be 4 for Nuvoton.\n");
89 return std::make_pair(0, 0);
90 }
91 else if (addressOffset != bmcMapReserveBytes)
92 {
93 std::fprintf(stderr, "Map address offset should be 4 for Nuvoton.\n");
94 return std::make_pair(0, 0);
95 }
96
97 /* TODO: need a kernel driver to handle mapping configuration.
98 * Until then program the register through /dev/mem.
99 */
100 int fd;
Patrick Venture8b588562018-11-18 08:44:33 -0800101 if ((fd = sys->open("/dev/mem", O_RDWR | O_SYNC)) == -1)
Patrick Venture38613e12018-11-16 20:45:16 -0800102 {
103 std::fprintf(stderr, "Failed to open /dev/mem\n");
Patrick Venture8b588562018-11-18 08:44:33 -0800104 sys->close(fd);
Patrick Venture38613e12018-11-16 20:45:16 -0800105 return std::make_pair(0, 0);
106 }
107
108 const uint32_t bmcMapConfigBaseAddr = 0xc0001000;
109 const uint32_t bmcMapConfigWindowSizeOffset = 0x7;
110 const uint32_t bmcMapConfigWindowBaseOffset = 0xa;
111 const uint8_t bmcWindowSizeValue = 0xc; // 4k
112 const uint16_t bmcWindowBaseValue = 0x8000; // BMC phyAddr from 0xc0008000
113
Patrick Venture8b588562018-11-18 08:44:33 -0800114 int pageSize = sys->getpagesize();
115
Patrick Venture38613e12018-11-16 20:45:16 -0800116 auto mapBasePtr = reinterpret_cast<uint8_t*>(
Patrick Venture8b588562018-11-18 08:44:33 -0800117 sys->mmap(nullptr, pageSize, PROT_READ | PROT_WRITE, MAP_SHARED, fd,
118 bmcMapConfigBaseAddr));
Patrick Venture38613e12018-11-16 20:45:16 -0800119
120 uint8_t* bmcWindowSize = mapBasePtr + bmcMapConfigWindowSizeOffset;
121 uint16_t* bmcWindowBase =
122 reinterpret_cast<uint16_t*>(mapBasePtr + bmcMapConfigWindowBaseOffset);
123
124 *bmcWindowSize = bmcWindowSizeValue;
125 *bmcWindowBase = bmcWindowBaseValue;
126
Patrick Venture8b588562018-11-18 08:44:33 -0800127 sys->munmap(mapBasePtr, pageSize);
128 sys->close(fd);
Patrick Venture38613e12018-11-16 20:45:16 -0800129
130 return std::make_pair(windowOffset, windowSize);
Patrick Venturee7728422018-11-14 20:16:33 -0800131}
132
Patrick Venture517710d2019-01-17 11:37:40 -0800133std::vector<std::uint8_t> LpcMapperNuvoton::copyFrom(std::uint32_t length)
134{
135 /* TODO: implement. */
136 return {};
137}
138
Patrick Venturee7728422018-11-14 20:16:33 -0800139} // namespace blobs