blob: 83b0160ad0beb8bb109bf0d0da112d3a55ce205e [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 Venture38613e12018-11-16 20:45:16 -080044/*
45 * The host buffer address is configured by host through
46 * SuperIO. On BMC side the max memory can be mapped is 4kB with the caveat that
47 * first byte of the buffer is reserved as host/BMC semaphore and not usable as
48 * shared memory.
49 *
50 * Mapper returns success for (addr, len) where (addr & 0x7) == 4 and len <=
51 * (4096 - 4). Otherwise, mapper returns either
52 * - WindowOffset = 4 and WindowSize = len - 4 if (addr & 0x7) == 0
53 * - WindowSize = 0 means that the region cannot be mapped otherwise
54 */
Patrick Venturee7728422018-11-14 20:16:33 -080055std::pair<std::uint32_t, std::uint32_t>
56 LpcMapperNuvoton::mapWindow(std::uint32_t address, std::uint32_t length)
57{
Patrick Venture38613e12018-11-16 20:45:16 -080058 /* We reserve the first 4 bytes from the mapped region; the first byte
59 * is shared semaphore, and the number of 4 is for alignment.
60 */
61 const uint32_t bmcMapReserveBytes = 4;
62 const uint32_t bmcMapMaxSizeBytes = 4 * 1024 - bmcMapReserveBytes;
63
64 if (length <= bmcMapReserveBytes)
65 {
66 std::fprintf(stderr, "window size %" PRIx32 " too small to map.\n",
67 length);
68 return std::make_pair(0, 0);
69 }
70
71 if (length > bmcMapMaxSizeBytes)
72 {
73 std::fprintf(stderr,
74 "window size %" PRIx32 " not supported. Max size 4k.\n",
75 length);
76 length = bmcMapMaxSizeBytes;
77 }
78
79 /* If host requested region starts at an aligned address, return offset of 4
80 * bytes so as to skip the semaphore register.
81 */
82 uint32_t windowOffset = bmcMapReserveBytes;
83 uint32_t windowSize = length;
84
85 const uint32_t addressOffset = address & 0x7;
86
87 if (addressOffset == 0)
88 {
89 std::fprintf(stderr, "Map address offset should be 4 for Nuvoton.\n");
90 return std::make_pair(0, 0);
91 }
92 else if (addressOffset != bmcMapReserveBytes)
93 {
94 std::fprintf(stderr, "Map address offset should be 4 for Nuvoton.\n");
95 return std::make_pair(0, 0);
96 }
97
98 /* TODO: need a kernel driver to handle mapping configuration.
99 * Until then program the register through /dev/mem.
100 */
101 int fd;
Patrick Venture8b588562018-11-18 08:44:33 -0800102 if ((fd = sys->open("/dev/mem", O_RDWR | O_SYNC)) == -1)
Patrick Venture38613e12018-11-16 20:45:16 -0800103 {
104 std::fprintf(stderr, "Failed to open /dev/mem\n");
Patrick Venture8b588562018-11-18 08:44:33 -0800105 sys->close(fd);
Patrick Venture38613e12018-11-16 20:45:16 -0800106 return std::make_pair(0, 0);
107 }
108
109 const uint32_t bmcMapConfigBaseAddr = 0xc0001000;
110 const uint32_t bmcMapConfigWindowSizeOffset = 0x7;
111 const uint32_t bmcMapConfigWindowBaseOffset = 0xa;
112 const uint8_t bmcWindowSizeValue = 0xc; // 4k
113 const uint16_t bmcWindowBaseValue = 0x8000; // BMC phyAddr from 0xc0008000
114
Patrick Venture8b588562018-11-18 08:44:33 -0800115 int pageSize = sys->getpagesize();
116
Patrick Venture38613e12018-11-16 20:45:16 -0800117 auto mapBasePtr = reinterpret_cast<uint8_t*>(
Patrick Venture8b588562018-11-18 08:44:33 -0800118 sys->mmap(nullptr, pageSize, PROT_READ | PROT_WRITE, MAP_SHARED, fd,
119 bmcMapConfigBaseAddr));
Patrick Venture38613e12018-11-16 20:45:16 -0800120
121 uint8_t* bmcWindowSize = mapBasePtr + bmcMapConfigWindowSizeOffset;
122 uint16_t* bmcWindowBase =
123 reinterpret_cast<uint16_t*>(mapBasePtr + bmcMapConfigWindowBaseOffset);
124
125 *bmcWindowSize = bmcWindowSizeValue;
126 *bmcWindowBase = bmcWindowBaseValue;
127
Patrick Venture8b588562018-11-18 08:44:33 -0800128 sys->munmap(mapBasePtr, pageSize);
129 sys->close(fd);
Patrick Venture38613e12018-11-16 20:45:16 -0800130
131 return std::make_pair(windowOffset, windowSize);
Patrick Venturee7728422018-11-14 20:16:33 -0800132}
133
Patrick Venture517710d2019-01-17 11:37:40 -0800134std::vector<std::uint8_t> LpcMapperNuvoton::copyFrom(std::uint32_t length)
135{
136 /* TODO: implement. */
137 return {};
138}
139
Patrick Venturee7728422018-11-14 20:16:33 -0800140} // namespace blobs