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 | |
Patrick Venture | 5251da9 | 2019-01-17 11:25:26 -0800 | [diff] [blame] | 19 | #include "window_hw_interface.hpp" |
Patrick Venture | e772842 | 2018-11-14 20:16:33 -0800 | [diff] [blame] | 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> |
Patrick Venture | c9c6088 | 2019-01-17 11:44:47 -0800 | [diff] [blame] | 29 | #include <vector> |
Patrick Venture | e772842 | 2018-11-14 20:16:33 -0800 | [diff] [blame] | 30 | |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame] | 31 | namespace ipmi_flash |
Patrick Venture | e772842 | 2018-11-14 20:16:33 -0800 | [diff] [blame] | 32 | { |
Patrick Venture | 38613e1 | 2018-11-16 20:45:16 -0800 | [diff] [blame] | 33 | using std::uint16_t; |
| 34 | using std::uint32_t; |
| 35 | using std::uint8_t; |
Patrick Venture | e772842 | 2018-11-14 20:16:33 -0800 | [diff] [blame] | 36 | |
Patrick Venture | 78b1a66 | 2019-01-17 12:38:26 -0800 | [diff] [blame] | 37 | std::unique_ptr<HardwareMapperInterface> |
| 38 | LpcMapperNuvoton::createNuvotonMapper(std::uint32_t regionAddress) |
Patrick Venture | e772842 | 2018-11-14 20:16:33 -0800 | [diff] [blame] | 39 | { |
| 40 | /* NOTE: Considered making one factory for both types. */ |
Patrick Venture | 78b1a66 | 2019-01-17 12:38:26 -0800 | [diff] [blame] | 41 | return std::make_unique<LpcMapperNuvoton>(regionAddress); |
Patrick Venture | e772842 | 2018-11-14 20:16:33 -0800 | [diff] [blame] | 42 | } |
| 43 | |
Patrick Venture | 2343cfc | 2019-01-17 13:04:36 -0800 | [diff] [blame] | 44 | void LpcMapperNuvoton::close() |
| 45 | { |
| 46 | } |
| 47 | |
Patrick Venture | 38613e1 | 2018-11-16 20:45:16 -0800 | [diff] [blame] | 48 | /* |
| 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 Venture | e772842 | 2018-11-14 20:16:33 -0800 | [diff] [blame] | 59 | std::pair<std::uint32_t, std::uint32_t> |
| 60 | LpcMapperNuvoton::mapWindow(std::uint32_t address, std::uint32_t length) |
| 61 | { |
Patrick Venture | 38613e1 | 2018-11-16 20:45:16 -0800 | [diff] [blame] | 62 | /* 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 Venture | 8b58856 | 2018-11-18 08:44:33 -0800 | [diff] [blame] | 106 | if ((fd = sys->open("/dev/mem", O_RDWR | O_SYNC)) == -1) |
Patrick Venture | 38613e1 | 2018-11-16 20:45:16 -0800 | [diff] [blame] | 107 | { |
| 108 | std::fprintf(stderr, "Failed to open /dev/mem\n"); |
Patrick Venture | 8b58856 | 2018-11-18 08:44:33 -0800 | [diff] [blame] | 109 | sys->close(fd); |
Patrick Venture | 38613e1 | 2018-11-16 20:45:16 -0800 | [diff] [blame] | 110 | 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 Venture | 8b58856 | 2018-11-18 08:44:33 -0800 | [diff] [blame] | 119 | int pageSize = sys->getpagesize(); |
| 120 | |
Patrick Venture | 38613e1 | 2018-11-16 20:45:16 -0800 | [diff] [blame] | 121 | auto mapBasePtr = reinterpret_cast<uint8_t*>( |
Patrick Venture | 8b58856 | 2018-11-18 08:44:33 -0800 | [diff] [blame] | 122 | sys->mmap(nullptr, pageSize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, |
| 123 | bmcMapConfigBaseAddr)); |
Patrick Venture | 38613e1 | 2018-11-16 20:45:16 -0800 | [diff] [blame] | 124 | |
| 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 Venture | 8b58856 | 2018-11-18 08:44:33 -0800 | [diff] [blame] | 132 | sys->munmap(mapBasePtr, pageSize); |
| 133 | sys->close(fd); |
Patrick Venture | 38613e1 | 2018-11-16 20:45:16 -0800 | [diff] [blame] | 134 | |
| 135 | return std::make_pair(windowOffset, windowSize); |
Patrick Venture | e772842 | 2018-11-14 20:16:33 -0800 | [diff] [blame] | 136 | } |
| 137 | |
Patrick Venture | 517710d | 2019-01-17 11:37:40 -0800 | [diff] [blame] | 138 | std::vector<std::uint8_t> LpcMapperNuvoton::copyFrom(std::uint32_t length) |
| 139 | { |
| 140 | /* TODO: implement. */ |
| 141 | return {}; |
| 142 | } |
| 143 | |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame] | 144 | } // namespace ipmi_flash |