Patrick Venture | 22e3875 | 2018-11-21 08:52:49 -0800 | [diff] [blame] | 1 | /* |
| 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 Venture | e772842 | 2018-11-14 20:16:33 -0800 | [diff] [blame] | 17 | #include "lpc_nuvoton.hpp" |
| 18 | |
| 19 | #include "lpc_interface.hpp" |
| 20 | |
Patrick Venture | 38613e1 | 2018-11-16 20:45:16 -0800 | [diff] [blame] | 21 | #include <fcntl.h> |
| 22 | #include <sys/mman.h> |
Patrick Venture | 38613e1 | 2018-11-16 20:45:16 -0800 | [diff] [blame] | 23 | |
| 24 | #include <cinttypes> |
Patrick Venture | e772842 | 2018-11-14 20:16:33 -0800 | [diff] [blame] | 25 | #include <cstdint> |
Patrick Venture | 38613e1 | 2018-11-16 20:45:16 -0800 | [diff] [blame] | 26 | #include <cstdio> |
Patrick Venture | e772842 | 2018-11-14 20:16:33 -0800 | [diff] [blame] | 27 | #include <memory> |
| 28 | #include <utility> |
| 29 | |
| 30 | namespace blobs |
| 31 | { |
Patrick Venture | 38613e1 | 2018-11-16 20:45:16 -0800 | [diff] [blame] | 32 | using std::uint16_t; |
| 33 | using std::uint32_t; |
| 34 | using std::uint8_t; |
Patrick Venture | e772842 | 2018-11-14 20:16:33 -0800 | [diff] [blame] | 35 | |
| 36 | std::unique_ptr<LpcMapperInterface> LpcMapperNuvoton::createNuvotonMapper() |
| 37 | { |
| 38 | /* NOTE: Considered making one factory for both types. */ |
| 39 | return std::make_unique<LpcMapperNuvoton>(); |
| 40 | } |
| 41 | |
Patrick Venture | 38613e1 | 2018-11-16 20:45:16 -0800 | [diff] [blame] | 42 | /* |
| 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 Venture | e772842 | 2018-11-14 20:16:33 -0800 | [diff] [blame] | 53 | std::pair<std::uint32_t, std::uint32_t> |
| 54 | LpcMapperNuvoton::mapWindow(std::uint32_t address, std::uint32_t length) |
| 55 | { |
Patrick Venture | 38613e1 | 2018-11-16 20:45:16 -0800 | [diff] [blame] | 56 | /* 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 Venture | 8b58856 | 2018-11-18 08:44:33 -0800 | [diff] [blame] | 100 | if ((fd = sys->open("/dev/mem", O_RDWR | O_SYNC)) == -1) |
Patrick Venture | 38613e1 | 2018-11-16 20:45:16 -0800 | [diff] [blame] | 101 | { |
| 102 | std::fprintf(stderr, "Failed to open /dev/mem\n"); |
Patrick Venture | 8b58856 | 2018-11-18 08:44:33 -0800 | [diff] [blame] | 103 | sys->close(fd); |
Patrick Venture | 38613e1 | 2018-11-16 20:45:16 -0800 | [diff] [blame] | 104 | 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 Venture | 8b58856 | 2018-11-18 08:44:33 -0800 | [diff] [blame] | 113 | int pageSize = sys->getpagesize(); |
| 114 | |
Patrick Venture | 38613e1 | 2018-11-16 20:45:16 -0800 | [diff] [blame] | 115 | auto mapBasePtr = reinterpret_cast<uint8_t*>( |
Patrick Venture | 8b58856 | 2018-11-18 08:44:33 -0800 | [diff] [blame] | 116 | sys->mmap(nullptr, pageSize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, |
| 117 | bmcMapConfigBaseAddr)); |
Patrick Venture | 38613e1 | 2018-11-16 20:45:16 -0800 | [diff] [blame] | 118 | |
| 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 Venture | 8b58856 | 2018-11-18 08:44:33 -0800 | [diff] [blame] | 126 | sys->munmap(mapBasePtr, pageSize); |
| 127 | sys->close(fd); |
Patrick Venture | 38613e1 | 2018-11-16 20:45:16 -0800 | [diff] [blame] | 128 | |
| 129 | return std::make_pair(windowOffset, windowSize); |
Patrick Venture | e772842 | 2018-11-14 20:16:33 -0800 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | } // namespace blobs |