blob: b4e5477101e17b3e68c46c80a9bca6826294d31a [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 Venture78b1a662019-01-17 12:38:26 -080037std::unique_ptr<HardwareMapperInterface>
38 LpcMapperNuvoton::createNuvotonMapper(std::uint32_t regionAddress)
Patrick Venturee7728422018-11-14 20:16:33 -080039{
40 /* NOTE: Considered making one factory for both types. */
Patrick Venture78b1a662019-01-17 12:38:26 -080041 return std::make_unique<LpcMapperNuvoton>(regionAddress);
Patrick Venturee7728422018-11-14 20:16:33 -080042}
43
Patrick Venture2343cfc2019-01-17 13:04:36 -080044void LpcMapperNuvoton::close()
45{
46}
47
Patrick Venture38613e12018-11-16 20:45:16 -080048/*
49 * The host buffer address is configured by host through
50 * SuperIO. On BMC side the max memory can be mapped is 4kB with the caveat that
51 * first byte of the buffer is reserved as host/BMC semaphore and not usable as
52 * shared memory.
53 *
54 * Mapper returns success for (addr, len) where (addr & 0x7) == 4 and len <=
55 * (4096 - 4). Otherwise, mapper returns either
56 * - WindowOffset = 4 and WindowSize = len - 4 if (addr & 0x7) == 0
57 * - WindowSize = 0 means that the region cannot be mapped otherwise
58 */
Patrick Venturee7728422018-11-14 20:16:33 -080059std::pair<std::uint32_t, std::uint32_t>
60 LpcMapperNuvoton::mapWindow(std::uint32_t address, std::uint32_t length)
61{
Patrick Venture38613e12018-11-16 20:45:16 -080062 /* We reserve the first 4 bytes from the mapped region; the first byte
63 * is shared semaphore, and the number of 4 is for alignment.
64 */
65 const uint32_t bmcMapReserveBytes = 4;
66 const uint32_t bmcMapMaxSizeBytes = 4 * 1024 - bmcMapReserveBytes;
67
68 if (length <= bmcMapReserveBytes)
69 {
70 std::fprintf(stderr, "window size %" PRIx32 " too small to map.\n",
71 length);
72 return std::make_pair(0, 0);
73 }
74
75 if (length > bmcMapMaxSizeBytes)
76 {
77 std::fprintf(stderr,
78 "window size %" PRIx32 " not supported. Max size 4k.\n",
79 length);
80 length = bmcMapMaxSizeBytes;
81 }
82
83 /* If host requested region starts at an aligned address, return offset of 4
84 * bytes so as to skip the semaphore register.
85 */
86 uint32_t windowOffset = bmcMapReserveBytes;
87 uint32_t windowSize = length;
88
89 const uint32_t addressOffset = address & 0x7;
90
91 if (addressOffset == 0)
92 {
93 std::fprintf(stderr, "Map address offset should be 4 for Nuvoton.\n");
94 return std::make_pair(0, 0);
95 }
96 else if (addressOffset != bmcMapReserveBytes)
97 {
98 std::fprintf(stderr, "Map address offset should be 4 for Nuvoton.\n");
99 return std::make_pair(0, 0);
100 }
101
102 /* TODO: need a kernel driver to handle mapping configuration.
103 * Until then program the register through /dev/mem.
104 */
105 int fd;
Patrick Venture8b588562018-11-18 08:44:33 -0800106 if ((fd = sys->open("/dev/mem", O_RDWR | O_SYNC)) == -1)
Patrick Venture38613e12018-11-16 20:45:16 -0800107 {
108 std::fprintf(stderr, "Failed to open /dev/mem\n");
Patrick Venture8b588562018-11-18 08:44:33 -0800109 sys->close(fd);
Patrick Venture38613e12018-11-16 20:45:16 -0800110 return std::make_pair(0, 0);
111 }
112
113 const uint32_t bmcMapConfigBaseAddr = 0xc0001000;
114 const uint32_t bmcMapConfigWindowSizeOffset = 0x7;
115 const uint32_t bmcMapConfigWindowBaseOffset = 0xa;
116 const uint8_t bmcWindowSizeValue = 0xc; // 4k
117 const uint16_t bmcWindowBaseValue = 0x8000; // BMC phyAddr from 0xc0008000
118
Patrick Venture8b588562018-11-18 08:44:33 -0800119 int pageSize = sys->getpagesize();
120
Patrick Venture38613e12018-11-16 20:45:16 -0800121 auto mapBasePtr = reinterpret_cast<uint8_t*>(
Patrick Venture8b588562018-11-18 08:44:33 -0800122 sys->mmap(nullptr, pageSize, PROT_READ | PROT_WRITE, MAP_SHARED, fd,
123 bmcMapConfigBaseAddr));
Patrick Venture38613e12018-11-16 20:45:16 -0800124
125 uint8_t* bmcWindowSize = mapBasePtr + bmcMapConfigWindowSizeOffset;
126 uint16_t* bmcWindowBase =
127 reinterpret_cast<uint16_t*>(mapBasePtr + bmcMapConfigWindowBaseOffset);
128
129 *bmcWindowSize = bmcWindowSizeValue;
130 *bmcWindowBase = bmcWindowBaseValue;
131
Patrick Venture8b588562018-11-18 08:44:33 -0800132 sys->munmap(mapBasePtr, pageSize);
133 sys->close(fd);
Patrick Venture38613e12018-11-16 20:45:16 -0800134
135 return std::make_pair(windowOffset, windowSize);
Patrick Venturee7728422018-11-14 20:16:33 -0800136}
137
Patrick Venture517710d2019-01-17 11:37:40 -0800138std::vector<std::uint8_t> LpcMapperNuvoton::copyFrom(std::uint32_t length)
139{
140 /* TODO: implement. */
141 return {};
142}
143
Patrick Venturee7728422018-11-14 20:16:33 -0800144} // namespace blobs