blob: 76edb97f01dc8f50aab32e8c3ce6f52a22a00008 [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
19#include "lpc_interface.hpp"
20
Patrick Venture38613e12018-11-16 20:45:16 -080021#include <fcntl.h>
22#include <sys/mman.h>
23#include <unistd.h>
24
25#include <cinttypes>
Patrick Venturee7728422018-11-14 20:16:33 -080026#include <cstdint>
Patrick Venture38613e12018-11-16 20:45:16 -080027#include <cstdio>
Patrick Venturee7728422018-11-14 20:16:33 -080028#include <memory>
29#include <utility>
30
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
37std::unique_ptr<LpcMapperInterface> LpcMapperNuvoton::createNuvotonMapper()
38{
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;
101 if ((fd = open("/dev/mem", O_RDWR | O_SYNC)) == -1)
102 {
103 std::fprintf(stderr, "Failed to open /dev/mem\n");
104 close(fd);
105 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
114 auto mapBasePtr = reinterpret_cast<uint8_t*>(
115 mmap(nullptr, getpagesize(), PROT_READ | PROT_WRITE, MAP_SHARED, fd,
116 bmcMapConfigBaseAddr));
117
118 uint8_t* bmcWindowSize = mapBasePtr + bmcMapConfigWindowSizeOffset;
119 uint16_t* bmcWindowBase =
120 reinterpret_cast<uint16_t*>(mapBasePtr + bmcMapConfigWindowBaseOffset);
121
122 *bmcWindowSize = bmcWindowSizeValue;
123 *bmcWindowBase = bmcWindowBaseValue;
124
125 munmap(mapBasePtr, getpagesize());
126 close(fd);
127
128 return std::make_pair(windowOffset, windowSize);
Patrick Venturee7728422018-11-14 20:16:33 -0800129}
130
131} // namespace blobs