| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * This file is part of the Falcon Player (FPP) and is Copyright (C) | ||
| 3 | * 2013-2022 by the Falcon Player Developers. | ||
| 4 | * | ||
| 5 | * The Falcon Player (FPP) is free software, and is covered under | ||
| 6 | * multiple Open Source licenses. Please see the included 'LICENSES' | ||
| 7 | * file for descriptions of what files are covered by each license. | ||
| 8 | * | ||
| 9 | * This source file is covered under the LGPL v2.1 as described in the | ||
| 10 | * included LICENSE.LGPL file. | ||
| 11 | */ | ||
| 12 | |||
| 13 | #include "fpp-pch.h" | ||
| 14 | |||
| 15 | #include <sys/mman.h> | ||
| 16 | #include <sys/stat.h> | ||
| 17 | #include <sys/wait.h> | ||
| 18 | #include <algorithm> | ||
| 19 | #include <cerrno> | ||
| 20 | #include <cstring> | ||
| 21 | #include <elf.h> | ||
| 22 | #include <fcntl.h> | ||
| 23 | #include <filesystem> | ||
| 24 | #include <inttypes.h> | ||
| 25 | #include <map> | ||
| 26 | #include <mutex> | ||
| 27 | #include <stdexcept> | ||
| 28 | #include <stdint.h> | ||
| 29 | #include <thread> | ||
| 30 | #include <unistd.h> | ||
| 31 | #include <vector> | ||
| 32 | |||
| 33 | #include "../common.h" | ||
| 34 | #include "../log.h" | ||
| 35 | |||
| 36 | #include "BBBPruUtils.h" | ||
| 37 | |||
| 38 | class PRUCoreInfo { | ||
| 39 | public: | ||
| 40 | int pru_num = 0; | ||
| 41 | uint8_t* dataRam = nullptr; | ||
| 42 | uintptr_t dataRamPhyLoc = 0; | ||
| 43 | int dataRamSize = 0; | ||
| 44 | uint8_t* sharedRam = nullptr; | ||
| 45 | uintptr_t sharedRamPhyLoc = 0; | ||
| 46 | int sharedRamSize = 0; | ||
| 47 | |||
| 48 | uint8_t* sram = nullptr; | ||
| 49 | uint32_t sram_addr = 0; | ||
| 50 | size_t sram_size = 0; | ||
| 51 | |||
| 52 | uint8_t* m4ram = nullptr; | ||
| 53 | uint32_t m4ram_addr = 0; | ||
| 54 | size_t m4ram_size = 0; | ||
| 55 | |||
| 56 | 489 | std::string getRemoteProcStateFile() { | |
| 57 | #ifdef PLATFORM_BBB | ||
| 58 |
2/4✓ Branch 0 taken 412 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 412 times.
✗ Branch 3 not taken.
|
412 | return "/sys/class/remoteproc/remoteproc" + std::to_string(pru_num) + "/state"; |
| 59 | #elif defined(PLATFORM_BB64) | ||
| 60 | 77 | std::string base; | |
| 61 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 65 times.
|
77 | if (pru_num == 0) { |
| 62 |
1/2✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
|
12 | base = "/sys/devices/platform/bus@f0000/30040000.pruss/30074000.pru/remoteproc/remoteproc"; |
| 63 |
1/2✓ Branch 0 taken 65 times.
✗ Branch 1 not taken.
|
65 | } else if (pru_num == 1) { |
| 64 |
1/2✓ Branch 0 taken 65 times.
✗ Branch 1 not taken.
|
65 | base = "/sys/devices/platform/bus@f0000/30040000.pruss/30078000.pru/remoteproc/remoteproc"; |
| 65 | } else { | ||
| 66 | // M4 core, not PRU, but startable via remoteproc | ||
| 67 | ✗ | base = "/sys/devices/platform/bus@f0000/bus@f0000:bus@4000000/5000000.m4fss/remoteproc/remoteproc"; | |
| 68 | } | ||
| 69 |
1/2✓ Branch 0 taken 142 times.
✗ Branch 1 not taken.
|
142 | for (int x = 0; x < 10; x++) { |
| 70 |
5/8✓ Branch 0 taken 142 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 142 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 142 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 77 times.
✓ Branch 7 taken 65 times.
|
142 | if (FileExists(base + std::to_string(x) + "/state")) { |
| 71 |
2/4✓ Branch 0 taken 77 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 77 times.
✗ Branch 3 not taken.
|
77 | return base + std::to_string(x) + "/state"; |
| 72 | } | ||
| 73 | } | ||
| 74 | ✗ | return "/sys/class/remoteproc/remoteproc" + std::to_string(pru_num) + "/state"; | |
| 75 | #endif | ||
| 76 | 77 | } | |
| 77 | 325 | void disable() { | |
| 78 |
1/2✓ Branch 0 taken 325 times.
✗ Branch 1 not taken.
|
325 | std::string filename = getRemoteProcStateFile(); |
| 79 |
2/4✓ Branch 0 taken 325 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 325 times.
✗ Branch 3 not taken.
|
325 | if (FileExists(filename)) { |
| 80 |
1/2✓ Branch 0 taken 325 times.
✗ Branch 1 not taken.
|
325 | FILE* rp = fopen(filename.c_str(), "w"); |
| 81 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 324 times.
|
325 | if (rp == nullptr) { |
| 82 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | LogWarn(VB_CHANNELOUT, "BBBPru::Pru::disable() - could not open %s: %s\n", filename.c_str(), strerror(errno)); |
| 83 | 1 | return; | |
| 84 | } | ||
| 85 |
1/2✓ Branch 0 taken 324 times.
✗ Branch 1 not taken.
|
324 | fprintf(rp, "stop"); |
| 86 |
1/2✓ Branch 0 taken 324 times.
✗ Branch 1 not taken.
|
324 | fclose(rp); |
| 87 |
1/2✓ Branch 0 taken 324 times.
✗ Branch 1 not taken.
|
324 | std::this_thread::sleep_for(std::chrono::milliseconds(1)); |
| 88 |
1/2✓ Branch 0 taken 324 times.
✗ Branch 1 not taken.
|
324 | std::string f = GetFileContents(filename); |
| 89 |
1/2✓ Branch 0 taken 324 times.
✗ Branch 1 not taken.
|
324 | TrimWhiteSpace(f); |
| 90 | 324 | int cnt = 0; | |
| 91 |
3/8✓ Branch 0 taken 324 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 324 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 324 times.
|
324 | while (f != "offline" && cnt < 500) { |
| 92 | ✗ | std::this_thread::sleep_for(std::chrono::milliseconds(1)); | |
| 93 | ✗ | f = GetFileContents(filename); | |
| 94 | ✗ | TrimWhiteSpace(f); | |
| 95 | ✗ | cnt++; | |
| 96 | } | ||
| 97 |
2/4✓ Branch 0 taken 324 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 324 times.
|
324 | if (f != "offline") { |
| 98 | ✗ | LogWarn(VB_CHANNELOUT, "BBBPru::Pru::disable() - could not stop PRU core %d State: %s\n", pru_num, f.c_str()); | |
| 99 | } | ||
| 100 | 324 | } | |
| 101 |
2/2✓ Branch 0 taken 324 times.
✓ Branch 1 taken 1 times.
|
325 | } |
| 102 | 164 | bool enable() { | |
| 103 |
1/2✓ Branch 0 taken 164 times.
✗ Branch 1 not taken.
|
164 | std::string filename = getRemoteProcStateFile(); |
| 104 | 164 | int cnt = 0; | |
| 105 |
3/8✓ Branch 0 taken 164 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 164 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 164 times.
|
164 | while (!FileExists(filename) && cnt < 10000) { |
| 106 | ✗ | cnt++; | |
| 107 | ✗ | std::this_thread::sleep_for(std::chrono::milliseconds(1)); | |
| 108 | } | ||
| 109 |
4/8✓ Branch 0 taken 164 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 164 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 164 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 164 times.
✗ Branch 7 not taken.
|
164 | if (FileExists(filename) && cnt < 10000) { |
| 110 |
1/2✓ Branch 0 taken 164 times.
✗ Branch 1 not taken.
|
164 | if (pru_num <= 1) { |
| 111 | // explicitly select the firmware image; the sysfs setting is | ||
| 112 | // sticky so anything else that loaded firmware on this core | ||
| 113 | // (tests, other tools) would otherwise be re-booted instead | ||
| 114 |
2/4✓ Branch 0 taken 164 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 164 times.
✗ Branch 3 not taken.
|
164 | std::string fwFile = filename.substr(0, filename.size() - 5) + "firmware"; |
| 115 |
1/2✓ Branch 0 taken 164 times.
✗ Branch 1 not taken.
|
164 | FILE* fw = fopen(fwFile.c_str(), "w"); |
| 116 |
2/2✓ Branch 0 taken 163 times.
✓ Branch 1 taken 1 times.
|
164 | if (fw) { |
| 117 | #if defined(PLATFORM_BBB) | ||
| 118 |
1/2✓ Branch 0 taken 138 times.
✗ Branch 1 not taken.
|
138 | fprintf(fw, "am335x-pru%d-fw", pru_num); |
| 119 | #else | ||
| 120 |
1/2✓ Branch 0 taken 25 times.
✗ Branch 1 not taken.
|
25 | fprintf(fw, "am62x-pru%d-fw", pru_num); |
| 121 | #endif | ||
| 122 |
1/2✓ Branch 0 taken 163 times.
✗ Branch 1 not taken.
|
163 | fclose(fw); |
| 123 | } | ||
| 124 | 164 | } | |
| 125 |
1/2✓ Branch 0 taken 164 times.
✗ Branch 1 not taken.
|
164 | FILE* rp = fopen(filename.c_str(), "w"); |
| 126 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 163 times.
|
164 | if (rp == nullptr) { |
| 127 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | LogWarn(VB_CHANNELOUT, "BBBPru::Pru::enable() - could not open %s: %s\n", filename.c_str(), strerror(errno)); |
| 128 | 3 | return false; | |
| 129 | } | ||
| 130 |
1/2✓ Branch 0 taken 163 times.
✗ Branch 1 not taken.
|
163 | fprintf(rp, "start"); |
| 131 |
1/2✓ Branch 0 taken 163 times.
✗ Branch 1 not taken.
|
163 | fclose(rp); |
| 132 | 163 | cnt = 0; | |
| 133 |
1/2✓ Branch 0 taken 163 times.
✗ Branch 1 not taken.
|
163 | std::this_thread::sleep_for(std::chrono::milliseconds(1)); |
| 134 |
1/2✓ Branch 0 taken 163 times.
✗ Branch 1 not taken.
|
163 | std::string f = GetFileContents(filename); |
| 135 |
1/2✓ Branch 0 taken 163 times.
✗ Branch 1 not taken.
|
163 | TrimWhiteSpace(f); |
| 136 |
7/8✓ Branch 0 taken 1163 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1002 times.
✓ Branch 3 taken 161 times.
✓ Branch 4 taken 1000 times.
✓ Branch 5 taken 2 times.
✓ Branch 6 taken 1000 times.
✓ Branch 7 taken 163 times.
|
1163 | while (f != "running" && cnt < 500) { |
| 137 |
1/2✓ Branch 0 taken 1000 times.
✗ Branch 1 not taken.
|
1000 | std::this_thread::sleep_for(std::chrono::milliseconds(1)); |
| 138 |
1/2✓ Branch 0 taken 1000 times.
✗ Branch 1 not taken.
|
1000 | f = GetFileContents(filename); |
| 139 |
1/2✓ Branch 0 taken 1000 times.
✗ Branch 1 not taken.
|
1000 | TrimWhiteSpace(f); |
| 140 | 1000 | cnt++; | |
| 141 | } | ||
| 142 |
3/4✓ Branch 0 taken 163 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 161 times.
|
163 | if (f != "running") { |
| 143 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | LogWarn(VB_CHANNELOUT, "BBBPru::Pru::enable() - could not start PRU core %d State: %s\n", pru_num, f.c_str()); |
| 144 | 2 | return false; | |
| 145 | } | ||
| 146 |
2/2✓ Branch 0 taken 161 times.
✓ Branch 1 taken 2 times.
|
163 | } else { |
| 147 | ✗ | LogWarn(VB_CHANNELOUT, "BBBPru::Pru::enable() - could not start PRU core %d\n", pru_num); | |
| 148 | ✗ | return false; | |
| 149 | } | ||
| 150 | 161 | return true; | |
| 151 | 164 | } | |
| 152 | }; | ||
| 153 | |||
| 154 | static int prussUseCount = 0; | ||
| 155 | |||
| 156 | static uint8_t* ddr_mem_loc = nullptr; | ||
| 157 | static uint32_t ddr_phy_mem_loc = 0; | ||
| 158 | static size_t ddr_filelen = 0; | ||
| 159 | static uint8_t* base_memory_location = nullptr; | ||
| 160 | static PRUCoreInfo prus[2]; | ||
| 161 | |||
| 162 | #if defined(PLATFORM_BBB) | ||
| 163 | |||
| 164 | constexpr uintptr_t PRUSS_MMAP_BASE = 0x4a300000; | ||
| 165 | constexpr size_t PRUSS_MMAP_SIZE = 0x40000; | ||
| 166 | |||
| 167 | constexpr uintptr_t PRUSS_DATARAM0_PHYS_BASE = 0x4a300000; | ||
| 168 | constexpr uintptr_t PRUSS_DATARAM1_PHYS_BASE = 0x4a302000; | ||
| 169 | constexpr size_t PRUSS_DATARAM_SIZE = 8 * 1024; // 8K | ||
| 170 | |||
| 171 | constexpr uintptr_t PRUSS_SHAREDRAM_BASE = 0x4a310000; | ||
| 172 | constexpr size_t PRUSS_SHAREDRAM_SIZE = 12 * 1024; // 12K | ||
| 173 | |||
| 174 | constexpr uint32_t DDR_ADDR = 0x8f000000; | ||
| 175 | constexpr size_t DDR_SIZE = 0x00400000; | ||
| 176 | |||
| 177 | constexpr std::string FIRMWARE_PREFIX = "am335x"; | ||
| 178 | constexpr bool FAKE_PRU = false; | ||
| 179 | // run() clears the PRU memories only after it has enabled the core. The | ||
| 180 | // PRUSS is clocked once a core runs, but with both cores stopped it may not | ||
| 181 | // be: a board whose PRU was never brought up, or whose cores an earlier run | ||
| 182 | // stopped, takes a bus abort on the write (crash bucket e80ad9caf7, SIGBUS in | ||
| 183 | // the clear before enable()). | ||
| 184 | constexpr bool CLEAR_BEFORE_ENABLE = false; | ||
| 185 | |||
| 186 | static uint8_t* PRUSS_SRAM_BASE = 0x00000000; | ||
| 187 | constexpr uint32_t PRUSS_SRAM_GLOBAL = 0x00000000; | ||
| 188 | constexpr size_t PRUSS_SRAM_SIZE = 0; | ||
| 189 | |||
| 190 | static uint8_t* PRUSS_M4RAM_BASE = 0x00000000; | ||
| 191 | constexpr uint32_t PRUSS_M4RAM_GLOBAL = 0x00000000; | ||
| 192 | constexpr size_t PRUSS_M4RAM_SIZE = 0; | ||
| 193 | |||
| 194 | #elif defined(PLATFORM_BB64) | ||
| 195 | constexpr uintptr_t PRUSS_MMAP_BASE = 0x30040000; | ||
| 196 | constexpr size_t PRUSS_MMAP_SIZE = 0x80000; | ||
| 197 | |||
| 198 | constexpr size_t PRUSS_DATARAM_SIZE = 8 * 1024; // 8K | ||
| 199 | constexpr uintptr_t PRUSS_DATARAM0_PHYS_BASE = 0x30040000; | ||
| 200 | constexpr uintptr_t PRUSS_DATARAM1_PHYS_BASE = 0x30042000; | ||
| 201 | |||
| 202 | constexpr uintptr_t PRUSS_SHAREDRAM_BASE = 0x30050000; | ||
| 203 | constexpr size_t PRUSS_SHAREDRAM_SIZE = 32 * 1024; // 32K | ||
| 204 | |||
| 205 | constexpr uint32_t DDR_ADDR = 0x8f000000; | ||
| 206 | constexpr size_t DDR_SIZE = 0x00400000; | ||
| 207 | |||
| 208 | // LDI32 r8, 0x70000000 // OCSRAM | ||
| 209 | // LDI32 r8, 0x0005040000 // M4 DRAM | ||
| 210 | |||
| 211 | static uint8_t* PRUSS_SRAM_BASE = nullptr; | ||
| 212 | constexpr uint32_t PRUSS_SRAM_GLOBAL = 0x70000000; | ||
| 213 | constexpr size_t PRUSS_SRAM_SIZE = 64 * 1024; // 64K | ||
| 214 | |||
| 215 | static uint8_t* PRUSS_M4RAM_BASE = nullptr; | ||
| 216 | constexpr uint32_t PRUSS_M4RAM_GLOBAL = 0x05040000; | ||
| 217 | constexpr size_t PRUSS_M4RAM_SIZE = 256 * 1024; // 256K; | ||
| 218 | |||
| 219 | constexpr std::string FIRMWARE_PREFIX = "am62x"; | ||
| 220 | // With both cores stopped the AM62x PRUSS still takes the writes, and the | ||
| 221 | // outputs that use the SMEM ring need a restart to see none of the previous | ||
| 222 | // run's handshake words (see run()). | ||
| 223 | constexpr bool CLEAR_BEFORE_ENABLE = true; | ||
| 224 | |||
| 225 | static bool FAKE_PRU = !FileExists("/sys/class/remoteproc/remoteproc0/state"); | ||
| 226 | #endif | ||
| 227 | |||
| 228 | 162 | static void releasePrus() { | |
| 229 |
1/2✓ Branch 0 taken 162 times.
✗ Branch 1 not taken.
|
162 | if (ddr_mem_loc) { |
| 230 | 162 | munmap(ddr_mem_loc, ddr_filelen); | |
| 231 | 162 | ddr_mem_loc = nullptr; | |
| 232 | } | ||
| 233 |
2/2✓ Branch 0 taken 161 times.
✓ Branch 1 taken 1 times.
|
162 | if (base_memory_location) { |
| 234 | 161 | munmap(base_memory_location, PRUSS_MMAP_SIZE); | |
| 235 | 161 | base_memory_location = nullptr; | |
| 236 | } | ||
| 237 |
2/2✓ Branch 0 taken 23 times.
✓ Branch 1 taken 139 times.
|
162 | if (PRUSS_SRAM_BASE) { |
| 238 | 23 | munmap(PRUSS_SRAM_BASE, PRUSS_SRAM_SIZE); | |
| 239 | 23 | PRUSS_SRAM_BASE = nullptr; | |
| 240 | } | ||
| 241 |
2/2✓ Branch 0 taken 23 times.
✓ Branch 1 taken 139 times.
|
162 | if (PRUSS_M4RAM_BASE) { |
| 242 | 23 | munmap(PRUSS_M4RAM_BASE, PRUSS_M4RAM_SIZE); | |
| 243 | 23 | PRUSS_M4RAM_BASE = nullptr; | |
| 244 | } | ||
| 245 | 162 | } | |
| 246 | |||
| 247 | // mmap() reports failure as MAP_FAILED, not nullptr; normalise so the rest of | ||
| 248 | // this file can keep testing the pointers for null, and keep the first errno. | ||
| 249 | 370 | static uint8_t* mapOrNull(size_t len, int flags, int fd, off_t off, int& err) { | |
| 250 | 370 | void* p = mmap(0, len, PROT_WRITE | PROT_READ, flags, fd, off); | |
| 251 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 369 times.
|
370 | if (p == MAP_FAILED) { |
| 252 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (!err) { |
| 253 | 1 | err = errno; | |
| 254 | } | ||
| 255 | 1 | return nullptr; | |
| 256 | } | ||
| 257 | 369 | return (uint8_t*)p; | |
| 258 | } | ||
| 259 | |||
| 260 | // Returns 0, or the errno of the first thing that failed. On failure nothing | ||
| 261 | // is left mapped. | ||
| 262 | 162 | static int initPrus() { | |
| 263 | 162 | int err = 0; | |
| 264 | 162 | int mem_fd = -1; | |
| 265 |
2/2✓ Branch 0 taken 23 times.
✓ Branch 1 taken 46 times.
|
69 | if (!FAKE_PRU) { |
| 266 |
1/2✓ Branch 0 taken 116 times.
✗ Branch 1 not taken.
|
116 | mem_fd = open("/dev/mem", O_RDWR | O_CLOEXEC); |
| 267 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 116 times.
|
116 | if (mem_fd < 0) { |
| 268 | ✗ | return errno; | |
| 269 | } | ||
| 270 | 116 | base_memory_location = mapOrNull(PRUSS_MMAP_SIZE, MAP_SHARED, mem_fd, PRUSS_MMAP_BASE, err); | |
| 271 | if (PRUSS_SRAM_SIZE > 0) { | ||
| 272 | 23 | PRUSS_SRAM_BASE = mapOrNull(PRUSS_SRAM_SIZE, MAP_SHARED, mem_fd, PRUSS_SRAM_GLOBAL, err); | |
| 273 | } | ||
| 274 | if (PRUSS_M4RAM_SIZE > 0) { | ||
| 275 | 23 | PRUSS_M4RAM_BASE = mapOrNull(PRUSS_M4RAM_SIZE, MAP_SHARED, mem_fd, PRUSS_M4RAM_GLOBAL, err); | |
| 276 | } | ||
| 277 | } else { | ||
| 278 | 46 | base_memory_location = mapOrNull(PRUSS_MMAP_SIZE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0, err); | |
| 279 | } | ||
| 280 | |||
| 281 | 162 | uint32_t ddr_addr = DDR_ADDR; | |
| 282 | 162 | size_t ddr_sizeb = DDR_SIZE; | |
| 283 | |||
| 284 |
3/4✓ Branch 0 taken 162 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 23 times.
✓ Branch 3 taken 46 times.
|
162 | if (ddr_mem_loc == nullptr && !FAKE_PRU) { |
| 285 | 116 | ddr_phy_mem_loc = ddr_addr; | |
| 286 | 116 | ddr_filelen = ddr_sizeb; | |
| 287 | // The reserved region is still System RAM to the kernel (it is not | ||
| 288 | // no-map), so a plain /dev/mem mapping of it is write-back | ||
| 289 | // cacheable, and the PRUs snoop neither the Cortex-A8 nor the A53 | ||
| 290 | // caches. On the A8 a line an output ever read stays cached and its | ||
| 291 | // next writes sit dirty there instead of reaching DDR (GH #3200: | ||
| 292 | // three pixels of every BBB48String port showing a stale colour at | ||
| 293 | // each change); the A53 allocates on write as well, so there even | ||
| 294 | // write-only code left stale bytes. User space cannot clean a line | ||
| 295 | // to DDR on ARMv7 - msync and __builtin___clear_cache do not - so map | ||
| 296 | // the region write-combined instead (O_SYNC on RAM): no cache to go | ||
| 297 | // stale, and writes that reach DDR measured as fast as through the | ||
| 298 | // cached mapping on both. Reads through it are slow, so outputs keep | ||
| 299 | // their frames in ordinary memory and only ever write this region. | ||
| 300 | // Write-combined is Normal memory, so glibc's memcpy/memset are fine | ||
| 301 | // on it (unlike the PRU memories, which map as Device memory). | ||
| 302 |
1/2✓ Branch 0 taken 116 times.
✗ Branch 1 not taken.
|
116 | int ddr_fd = open("/dev/mem", O_RDWR | O_CLOEXEC | O_SYNC); |
| 303 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 116 times.
|
116 | if (ddr_fd < 0) { |
| 304 | ✗ | if (!err) { | |
| 305 | ✗ | err = errno; | |
| 306 | } | ||
| 307 | } else { | ||
| 308 | 116 | ddr_mem_loc = mapOrNull(ddr_filelen, MAP_SHARED, ddr_fd, ddr_addr, err); | |
| 309 |
1/2✓ Branch 0 taken 116 times.
✗ Branch 1 not taken.
|
116 | close(ddr_fd); |
| 310 | } | ||
| 311 |
1/2✓ Branch 0 taken 116 times.
✗ Branch 1 not taken.
|
116 | if (ddr_mem_loc) { |
| 312 | 116 | madvise(ddr_mem_loc, ddr_filelen, MADV_HUGEPAGE); | |
| 313 | } | ||
| 314 |
2/4✓ Branch 0 taken 46 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 46 times.
✗ Branch 3 not taken.
|
69 | } else if (ddr_addr == 0 || FAKE_PRU) { |
| 315 | // just malloc some memory so we don't crash | ||
| 316 | 46 | ddr_phy_mem_loc = ddr_addr; | |
| 317 | 46 | ddr_filelen = ddr_sizeb; | |
| 318 | 46 | ddr_mem_loc = mapOrNull(ddr_filelen, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0, err); | |
| 319 | } | ||
| 320 |
2/2✓ Branch 0 taken 116 times.
✓ Branch 1 taken 46 times.
|
162 | if (mem_fd >= 0) { |
| 321 |
1/2✓ Branch 0 taken 116 times.
✗ Branch 1 not taken.
|
116 | close(mem_fd); |
| 322 | } | ||
| 323 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 161 times.
|
162 | if (err) { |
| 324 | 1 | releasePrus(); | |
| 325 | 1 | return err; | |
| 326 | } | ||
| 327 | |||
| 328 | 161 | prus[0].pru_num = 0; | |
| 329 | 161 | prus[1].pru_num = 1; | |
| 330 | 161 | prus[0].dataRam = base_memory_location + (PRUSS_DATARAM0_PHYS_BASE - PRUSS_MMAP_BASE); | |
| 331 | 161 | prus[0].dataRamSize = PRUSS_DATARAM_SIZE; | |
| 332 | 161 | prus[0].dataRamPhyLoc = PRUSS_DATARAM0_PHYS_BASE; | |
| 333 | 161 | prus[1].dataRam = base_memory_location + (PRUSS_DATARAM1_PHYS_BASE - PRUSS_MMAP_BASE); | |
| 334 | 161 | prus[1].dataRamSize = PRUSS_DATARAM_SIZE; | |
| 335 | 161 | prus[1].dataRamPhyLoc = PRUSS_DATARAM1_PHYS_BASE; | |
| 336 | 161 | prus[0].sharedRam = base_memory_location + (PRUSS_SHAREDRAM_BASE - PRUSS_MMAP_BASE); | |
| 337 | 161 | prus[0].sharedRamSize = PRUSS_SHAREDRAM_SIZE; | |
| 338 | 161 | prus[0].sharedRamPhyLoc = PRUSS_SHAREDRAM_BASE; | |
| 339 | 161 | prus[1].sharedRam = base_memory_location + (PRUSS_SHAREDRAM_BASE - PRUSS_MMAP_BASE); | |
| 340 | 161 | prus[1].sharedRamSize = PRUSS_SHAREDRAM_SIZE; | |
| 341 | 161 | prus[1].sharedRamPhyLoc = PRUSS_SHAREDRAM_BASE; | |
| 342 | |||
| 343 |
2/2✓ Branch 0 taken 322 times.
✓ Branch 1 taken 161 times.
|
483 | for (int x = 0; x < 2; x++) { |
| 344 | 322 | prus[x].sram = PRUSS_SRAM_BASE; | |
| 345 | 322 | prus[x].sram_addr = PRUSS_SRAM_GLOBAL; | |
| 346 | 322 | prus[x].sram_size = PRUSS_SRAM_SIZE; | |
| 347 | |||
| 348 | 322 | prus[x].m4ram = PRUSS_M4RAM_BASE; | |
| 349 | 322 | prus[x].m4ram_addr = PRUSS_M4RAM_GLOBAL; | |
| 350 | 322 | prus[x].m4ram_size = PRUSS_M4RAM_SIZE; | |
| 351 | } | ||
| 352 | |||
| 353 |
1/2✓ Branch 0 taken 161 times.
✗ Branch 1 not taken.
|
161 | if (ddr_sizeb) { |
| 354 | 161 | memset(ddr_mem_loc, 0, ddr_sizeb); | |
| 355 | } | ||
| 356 | 161 | __asm__ __volatile__("" :: | |
| 357 | : "memory"); | ||
| 358 | 161 | return 0; | |
| 359 | } | ||
| 360 | |||
| 361 | 228 | BBBPru::BBBPru(int pru, bool mapShared, bool mapOther) : | |
| 362 | 228 | pru_num(pru) { | |
| 363 |
2/2✓ Branch 0 taken 162 times.
✓ Branch 1 taken 66 times.
|
228 | if (prussUseCount == 0) { |
| 364 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 161 times.
|
162 | if (int err = initPrus()) { |
| 365 | // Nothing is mapped and prussUseCount is untouched, so no | ||
| 366 | // destructor work is owed. Output Init() runs inside the channel | ||
| 367 | // output loader's try/catch, which logs this, raises its own | ||
| 368 | // (reload-cleared) warning and drops the output -- instead of | ||
| 369 | // memset()ing through MAP_FAILED as this used to. | ||
| 370 |
3/6✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
|
1 | std::string msg = std::string("could not map PRU memory: ") + FPPstrerror(err); |
| 371 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | LogErr(VB_CHANNELOUT, "BBBPru: %s\n", msg.c_str()); |
| 372 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | throw std::runtime_error(msg); |
| 373 | 1 | } | |
| 374 | } | ||
| 375 | 227 | prussUseCount++; | |
| 376 | |||
| 377 | 227 | this->data_ram = prus[pru].dataRam; | |
| 378 | 227 | this->data_ram_size = prus[pru].dataRamSize; | |
| 379 | |||
| 380 | 227 | this->ddr = ddr_mem_loc; | |
| 381 | 227 | this->ddr_size = ddr_filelen; | |
| 382 | 227 | this->ddr_addr = ddr_phy_mem_loc; | |
| 383 | |||
| 384 |
2/2✓ Branch 0 taken 129 times.
✓ Branch 1 taken 98 times.
|
227 | if (mapShared) { |
| 385 | 129 | this->shared_ram_size = prus[pru].sharedRamSize; | |
| 386 | 129 | this->shared_ram = prus[pru].sharedRam; | |
| 387 | } else { | ||
| 388 | 98 | this->shared_ram_size = 0; | |
| 389 | 98 | this->shared_ram = 0; | |
| 390 | } | ||
| 391 |
2/2✓ Branch 0 taken 52 times.
✓ Branch 1 taken 175 times.
|
227 | if (mapOther) { |
| 392 | 52 | int p = pru ? 0 : 1; | |
| 393 | 52 | this->other_data_ram = prus[p].dataRam; | |
| 394 | 52 | this->other_data_ram_size = prus[p].dataRamSize; | |
| 395 | } else { | ||
| 396 | 175 | this->other_data_ram_size = 0; | |
| 397 | 175 | this->other_data_ram = 0; | |
| 398 | } | ||
| 399 | |||
| 400 | 227 | this->sram = prus[pru].sram; | |
| 401 | 227 | this->sram_addr = prus[pru].sram_addr; | |
| 402 | 227 | this->sram_size = prus[pru].sram_size; | |
| 403 | |||
| 404 | 227 | this->m4ram = prus[pru].m4ram; | |
| 405 | 227 | this->m4ram_addr = prus[pru].m4ram_addr; | |
| 406 | 227 | this->m4ram_size = prus[pru].m4ram_size; | |
| 407 | 227 | } | |
| 408 | |||
| 409 | 227 | BBBPru::~BBBPru() { | |
| 410 | 227 | prussUseCount--; | |
| 411 |
2/2✓ Branch 0 taken 161 times.
✓ Branch 1 taken 66 times.
|
227 | if (prussUseCount == 0) { |
| 412 | 161 | releasePrus(); | |
| 413 | } | ||
| 414 | 227 | } | |
| 415 | |||
| 416 | // The core control register sits 0x22000 into the PRUSS mapping, one 0x2000 | ||
| 417 | // block per core, on both the AM335x and the AM62x. Bit 1 enables the core. | ||
| 418 | static constexpr uintptr_t PRU_CTRL_OFFSET = 0x22000; | ||
| 419 | static constexpr uintptr_t PRU_CTRL_STRIDE = 0x2000; | ||
| 420 | static constexpr uint32_t PRU_CTRL_ENABLE = 0x2; | ||
| 421 | |||
| 422 | 327 | static volatile uint32_t* pruControlReg(unsigned pru_num) { | |
| 423 | // only the two PRU cores: one block further on is the PRUSS CFG space | ||
| 424 |
2/4✓ Branch 0 taken 327 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 327 times.
|
327 | if (!base_memory_location || pru_num > 1) { |
| 425 | ✗ | return nullptr; | |
| 426 | } | ||
| 427 | 327 | return (volatile uint32_t*)(base_memory_location + PRU_CTRL_OFFSET + (pru_num * PRU_CTRL_STRIDE)); | |
| 428 | } | ||
| 429 | |||
| 430 | // Clear the core's enable bit and wait until it reads back clear with the | ||
| 431 | // core no longer running. A remoteproc stop alone does not guarantee this: | ||
| 432 | // the firmware's RESET_PRU_CLOCK read-modify-writes this same register to | ||
| 433 | // restart the cycle counter, and every FPP program runs it constantly while | ||
| 434 | // idle. A stop that clears EN between the firmware's read and its write is | ||
| 435 | // undone, and the core keeps running its old program while remoteproc | ||
| 436 | // reports it offline - acting on any command word it sees, and executing | ||
| 437 | // whatever lands in its instruction RAM when the next program is loaded. | ||
| 438 | // Our own write can be undone the same way, so it is repeated until it | ||
| 439 | // sticks; once the core is halted nothing can set the bit again. | ||
| 440 | 325 | static bool haltCore(unsigned pru_num) { | |
| 441 | 325 | volatile uint32_t* ctrl = pruControlReg(pru_num); | |
| 442 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 325 times.
|
325 | if (!ctrl) { |
| 443 | ✗ | return true; | |
| 444 | } | ||
| 445 | 325 | constexpr uint32_t PRU_CTRL_RUNSTATE = 0x8000; | |
| 446 |
1/2✓ Branch 0 taken 587 times.
✗ Branch 1 not taken.
|
587 | for (int x = 0; x < 100; x++) { |
| 447 | 587 | uint32_t v = *ctrl; | |
| 448 |
4/4✓ Branch 0 taken 339 times.
✓ Branch 1 taken 248 times.
✓ Branch 2 taken 325 times.
✓ Branch 3 taken 14 times.
|
587 | if (!(v & PRU_CTRL_ENABLE) && !(v & PRU_CTRL_RUNSTATE)) { |
| 449 | 325 | return true; | |
| 450 | } | ||
| 451 | 262 | *ctrl = v & ~PRU_CTRL_ENABLE; | |
| 452 | 262 | __sync_synchronize(); | |
| 453 |
1/2✓ Branch 0 taken 262 times.
✗ Branch 1 not taken.
|
262 | std::this_thread::sleep_for(std::chrono::microseconds(10)); |
| 454 | } | ||
| 455 | ✗ | LogWarn(VB_CHANNELOUT, "BBBPru: PRU core %d did not halt (control 0x%X)\n", pru_num, *ctrl); | |
| 456 | ✗ | return false; | |
| 457 | } | ||
| 458 | |||
| 459 | 751 | static std::string pruFirmwareName(int pru_num) { | |
| 460 |
3/6✓ Branch 0 taken 751 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 751 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 751 times.
✗ Branch 5 not taken.
|
751 | return FIRMWARE_PREFIX + "-pru" + std::to_string(pru_num) + "-fw"; |
| 461 | } | ||
| 462 | |||
| 463 | // /lib/firmware/<prefix>-pruN-fw ships as a symlink to the "-fw.sleep" | ||
| 464 | // parking firmware that the image loads at boot. Copying the program | ||
| 465 | // through that name would overwrite the sleep firmware itself, so install | ||
| 466 | // the program under its own name and atomically repoint the link at it. | ||
| 467 | // Returns false if the program could not be copied in, leaving the | ||
| 468 | // previously installed program in place. | ||
| 469 | 227 | static bool installPRUFirmware(int pru_num, const std::string& program) { | |
| 470 |
2/4✓ Branch 0 taken 227 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 227 times.
✗ Branch 3 not taken.
|
227 | std::string fw = "/lib/firmware/" + pruFirmwareName(pru_num); |
| 471 |
3/6✓ Branch 0 taken 227 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 227 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 227 times.
|
227 | if (!CopyFileContents(program, fw + ".fpp")) { |
| 472 | ✗ | return false; | |
| 473 | } | ||
| 474 |
1/2✓ Branch 0 taken 227 times.
✗ Branch 1 not taken.
|
227 | std::string tmpLink = fw + ".new"; |
| 475 | 227 | unlink(tmpLink.c_str()); | |
| 476 |
3/6✓ Branch 0 taken 227 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 227 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 227 times.
✗ Branch 5 not taken.
|
227 | if (symlink((pruFirmwareName(pru_num) + ".fpp").c_str(), tmpLink.c_str()) == 0) { |
| 477 | 227 | rename(tmpLink.c_str(), fw.c_str()); | |
| 478 | } | ||
| 479 | 227 | return true; | |
| 480 | 227 | } | |
| 481 | |||
| 482 | // Point the firmware name back at the sleep firmware so the next boot parks | ||
| 483 | // the PRU instead of re-running the last output program. FalconSerial in | ||
| 484 | // particular free-runs frames without waiting for a first command, so leaving | ||
| 485 | // it installed as the boot firmware blasts garbage on any muxed pins. | ||
| 486 | 161 | static void restorePRUSleepFirmware(int pru_num) { | |
| 487 |
2/4✓ Branch 0 taken 161 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 161 times.
✗ Branch 3 not taken.
|
161 | std::string sleepName = pruFirmwareName(pru_num) + ".sleep"; |
| 488 |
4/6✓ Branch 0 taken 161 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 161 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 25 times.
✓ Branch 5 taken 136 times.
|
161 | if (!FileExists("/lib/firmware/" + sleepName)) { |
| 489 | 25 | return; | |
| 490 | } | ||
| 491 |
2/4✓ Branch 0 taken 136 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 136 times.
✗ Branch 3 not taken.
|
136 | std::string fw = "/lib/firmware/" + pruFirmwareName(pru_num); |
| 492 |
1/2✓ Branch 0 taken 136 times.
✗ Branch 1 not taken.
|
136 | std::string tmpLink = fw + ".new"; |
| 493 | 136 | unlink(tmpLink.c_str()); | |
| 494 |
1/2✓ Branch 0 taken 136 times.
✗ Branch 1 not taken.
|
136 | if (symlink(sleepName.c_str(), tmpLink.c_str()) == 0) { |
| 495 | 136 | rename(tmpLink.c_str(), fw.c_str()); | |
| 496 | } | ||
| 497 |
2/2✓ Branch 0 taken 136 times.
✓ Branch 1 taken 25 times.
|
161 | } |
| 498 | |||
| 499 | 227 | bool BBBPru::run(const std::string& program, bool clearSharedMems, bool clearMems) { | |
| 500 |
1/2✓ Branch 0 taken 227 times.
✗ Branch 1 not taken.
|
227 | LogDebug(VB_CHANNELOUT, "BBBPru[%d]::run(%s)\n", pru_num, program.c_str()); |
| 501 | |||
| 502 | 250 | auto clearPRUMems = [&]() { | |
| 503 |
2/2✓ Branch 0 taken 53 times.
✓ Branch 1 taken 197 times.
|
250 | if (!clearMems) { |
| 504 | 53 | return; | |
| 505 | } | ||
| 506 | 197 | clearPRUMem(data_ram, data_ram_size); | |
| 507 |
2/2✓ Branch 0 taken 174 times.
✓ Branch 1 taken 23 times.
|
197 | if (clearSharedMems) { |
| 508 |
2/2✓ Branch 0 taken 75 times.
✓ Branch 1 taken 99 times.
|
174 | if (shared_ram) { |
| 509 | 75 | clearPRUMem(shared_ram, shared_ram_size); | |
| 510 | } | ||
| 511 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 172 times.
|
174 | if (other_data_ram) { |
| 512 | 2 | clearPRUMem(other_data_ram, other_data_ram_size); | |
| 513 | } | ||
| 514 | } | ||
| 515 | 227 | }; | |
| 516 | // A program that is missing - a runtime firmware build that failed - must | ||
| 517 | // not start the core: the copy below would fail and the core would boot | ||
| 518 | // whatever an earlier run left installed, possibly another output's | ||
| 519 | // firmware, while this reported success. | ||
| 520 |
2/4✓ Branch 0 taken 227 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 227 times.
|
227 | if (!FileExists(program)) { |
| 521 | ✗ | LogErr(VB_CHANNELOUT, "BBBPru[%d]: PRU program %s does not exist\n", pru_num, program.c_str()); | |
| 522 | ✗ | return false; | |
| 523 | } | ||
| 524 | 227 | bool enabled = true; | |
| 525 |
2/2✓ Branch 0 taken 26 times.
✓ Branch 1 taken 63 times.
|
89 | if (!FAKE_PRU) { |
| 526 | // halted before the program is installed: remoteproc loads it into | ||
| 527 | // the instruction RAM of a core it believes is stopped | ||
| 528 |
1/2✓ Branch 0 taken 164 times.
✗ Branch 1 not taken.
|
164 | haltCore(pru_num); |
| 529 |
1/2✓ Branch 0 taken 164 times.
✗ Branch 1 not taken.
|
164 | prus[pru_num].disable(); |
| 530 |
2/4✓ Branch 0 taken 164 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 164 times.
|
164 | if (!installPRUFirmware(pru_num, program)) { |
| 531 | ✗ | LogErr(VB_CHANNELOUT, "BBBPru[%d]: could not install PRU program %s\n", pru_num, program.c_str()); | |
| 532 | ✗ | return false; | |
| 533 | } | ||
| 534 | // Clear before the core starts as well as after, where the PRUSS | ||
| 535 | // takes writes with its cores stopped (CLEAR_BEFORE_ENABLE). | ||
| 536 | // Firmware loading does not clear data RAM, so on a restart the | ||
| 537 | // previous run's handshake words (the SMEM ring config in | ||
| 538 | // particular) are still there: the new firmware passes its wait on | ||
| 539 | // them at once and reads config the ARM has not written yet. The | ||
| 540 | // clear after enable() stays for a PRUSS that was powered down, | ||
| 541 | // where these writes are dropped. | ||
| 542 | if (CLEAR_BEFORE_ENABLE) { | ||
| 543 |
1/2✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
|
26 | clearPRUMems(); |
| 544 | } | ||
| 545 |
1/2✓ Branch 0 taken 164 times.
✗ Branch 1 not taken.
|
164 | enabled = prus[pru_num].enable(); |
| 546 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 161 times.
|
164 | if (!enabled) { |
| 547 | 3 | return false; | |
| 548 | } | ||
| 549 |
2/4✓ Branch 0 taken 63 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 63 times.
|
63 | } else if (!installPRUFirmware(pru_num, program)) { |
| 550 | ✗ | LogErr(VB_CHANNELOUT, "BBBPru[%d]: could not install PRU program %s\n", pru_num, program.c_str()); | |
| 551 | ✗ | return false; | |
| 552 | } | ||
| 553 |
1/2✓ Branch 0 taken 224 times.
✗ Branch 1 not taken.
|
224 | clearPRUMems(); |
| 554 | /* | ||
| 555 | printf("DL: %p\n", data_ram); | ||
| 556 | printf("OL: %p\n", other_data_ram); | ||
| 557 | printf("SH: %p\n", shared_ram); | ||
| 558 | printf("DDR: %p\n", ddr); | ||
| 559 | */ | ||
| 560 | 224 | return true; | |
| 561 | } | ||
| 562 | |||
| 563 | #ifdef PLATFORM_BBB | ||
| 564 | 94 | void BBBPru::clearPRUMem(uint8_t* ptr, size_t sz) { | |
| 565 | 94 | memset(ptr, 0, sz); | |
| 566 | 94 | } | |
| 567 | 154 | void BBBPru::memcpyToPRU(uint8_t* dst, uint8_t* src, size_t sz) { | |
| 568 | 154 | memcpy(dst, src, sz); | |
| 569 | 154 | } | |
| 570 | #elif defined(PLATFORM_BB64) | ||
| 571 | #pragma GCC push_options | ||
| 572 | #pragma GCC optimize("O0") | ||
| 573 | // The optimized memset and memcpy on Arm64 will segfault | ||
| 574 | // when doing certain sized operations to un-cacheable | ||
| 575 | // ram segments. Need to use ldnp/stnp instructions | ||
| 576 | // | ||
| 577 | // The block loop moves whole 64 byte units and needs 16 byte aligned operands | ||
| 578 | // (the PRU memories map uncached, where an unaligned vector access faults), so | ||
| 579 | // the head/tail and anything that does not qualify go through the byte loop | ||
| 580 | // instead - slower, but correct for every size and alignment. Exactly sztotal | ||
| 581 | // bytes are written and no more. | ||
| 582 | 1218 | void memcpy_ldnp(volatile unsigned char* dst, volatile unsigned char* src, int sztotal) { | |
| 583 | // NB: "sztotal - sztotal & 64" parses as "(sztotal - sztotal) & 64", i.e. | ||
| 584 | // always zero, which left the block loop below writing one fixed 64 byte | ||
| 585 | // store - past the end for anything shorter - and everything else crawling | ||
| 586 | // through the byte loop. Mask off the low bits instead. | ||
| 587 | 1218 | int sz = 0; | |
| 588 |
4/4✓ Branch 0 taken 1140 times.
✓ Branch 1 taken 78 times.
✓ Branch 2 taken 1056 times.
✓ Branch 3 taken 84 times.
|
1218 | if (sztotal >= 64 && ((((uintptr_t)dst | (uintptr_t)src) & 15) == 0)) { |
| 589 | 1056 | sz = sztotal & ~63; | |
| 590 | } | ||
| 591 |
2/2✓ Branch 0 taken 167856 times.
✓ Branch 1 taken 1218 times.
|
169074 | for (int x = sz; x < sztotal; x++) { |
| 592 | 167856 | dst[x] = src[x]; | |
| 593 | } | ||
| 594 |
2/2✓ Branch 0 taken 162 times.
✓ Branch 1 taken 1056 times.
|
1218 | if (sz == 0) { |
| 595 | 162 | return; | |
| 596 | } | ||
| 597 | 1056 | asm volatile( | |
| 598 | "NEONCopyPLD: \n" | ||
| 599 | "sub %[dst], %[dst], #64 \n" | ||
| 600 | "1: \n" | ||
| 601 | "ldnp q0, q1, [%[src]] \n" | ||
| 602 | "ldnp q2, q3, [%[src], #32] \n" | ||
| 603 | "add %[dst], %[dst], #64 \n" | ||
| 604 | "subs %[sz], %[sz], #64 \n" | ||
| 605 | "add %[src], %[src], #64 \n" | ||
| 606 | "stnp q0, q1, [%[dst]] \n" | ||
| 607 | "stnp q2, q3, [%[dst], #32] \n" | ||
| 608 | "b.gt 1b \n" | ||
| 609 | : [dst] "+r"(dst), [src] "+r"(src), [sz] "+r"(sz) : : "q0", "q1", "q2", "q3", "cc", "memory"); | ||
| 610 | } | ||
| 611 | |||
| 612 | 180 | void BBBPru::clearPRUMem(uint8_t* ptr, size_t sz) { | |
| 613 | 180 | __uint128_t z = 0; | |
| 614 | 180 | int c = sz / sizeof(z); | |
| 615 | 180 | __uint128_t* p = (__uint128_t*)ptr; | |
| 616 |
2/2✓ Branch 0 taken 207360 times.
✓ Branch 1 taken 180 times.
|
207540 | for (int x = 0; x < c; ++x) { |
| 617 | 207360 | *p = z; | |
| 618 | 207360 | p++; | |
| 619 | } | ||
| 620 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 180 times.
|
180 | for (int x = c * sizeof(z); x < sz; ++x) { |
| 621 | ✗ | ptr[x] = 0; | |
| 622 | } | ||
| 623 | 180 | } | |
| 624 | 1218 | void BBBPru::memcpyToPRU(uint8_t* dst, uint8_t* src, size_t sz) { | |
| 625 | 1218 | memcpy_ldnp(dst, src, sz); | |
| 626 | 1218 | } | |
| 627 | #pragma GCC pop_options | ||
| 628 | #endif | ||
| 629 | |||
| 630 | static std::mutex ddrAllocLock; | ||
| 631 | // owner -> (offset, size) within the reserved region | ||
| 632 | static std::map<std::string, std::pair<size_t, size_t>> ddrAllocs; | ||
| 633 | |||
| 634 | 112 | uint8_t* BBBPru::ddrAlloc(const std::string& owner, size_t size, uint32_t& physAddr, size_t minOffset) { | |
| 635 |
1/2✓ Branch 0 taken 112 times.
✗ Branch 1 not taken.
|
112 | std::unique_lock<std::mutex> lock(ddrAllocLock); |
| 636 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 112 times.
|
112 | if (ddr_mem_loc == nullptr) { |
| 637 | ✗ | LogErr(VB_CHANNELOUT, "BBBPru::ddrAlloc(%s): DDR region not mapped yet\n", owner.c_str()); | |
| 638 | ✗ | return nullptr; | |
| 639 | } | ||
| 640 | 112 | size = (size + 4095) & ~(size_t)4095; | |
| 641 | 112 | size_t pos = (minOffset + 4095) & ~(size_t)4095; | |
| 642 |
1/2✓ Branch 0 taken 112 times.
✗ Branch 1 not taken.
|
112 | ddrAllocs.erase(owner); |
| 643 | 112 | std::vector<std::pair<size_t, size_t>> used; | |
| 644 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 112 times.
|
120 | for (auto& a : ddrAllocs) { |
| 645 |
1/2✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 | used.push_back(a.second); |
| 646 | } | ||
| 647 |
1/2✓ Branch 0 taken 112 times.
✗ Branch 1 not taken.
|
112 | std::sort(used.begin(), used.end()); |
| 648 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 110 times.
|
118 | for (auto& u : used) { |
| 649 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 6 times.
|
8 | if (pos + size <= u.first) { |
| 650 | 2 | break; | |
| 651 | } | ||
| 652 |
1/2✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 | if (u.first + u.second > pos) { |
| 653 | 6 | pos = u.first + u.second; | |
| 654 | } | ||
| 655 | } | ||
| 656 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 108 times.
|
112 | if (pos + size > ddr_filelen) { |
| 657 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | LogErr(VB_CHANNELOUT, "BBBPru::ddrAlloc(%s): %zuKB does not fit in the PRU DDR region (%zuKB of %zuKB already in use)\n", |
| 658 | owner.c_str(), size / 1024, pos / 1024, ddr_filelen / 1024); | ||
| 659 | 4 | return nullptr; | |
| 660 | } | ||
| 661 |
1/2✓ Branch 0 taken 108 times.
✗ Branch 1 not taken.
|
108 | ddrAllocs[owner] = { pos, size }; |
| 662 | 108 | physAddr = ddr_phy_mem_loc + pos; | |
| 663 |
1/2✓ Branch 0 taken 108 times.
✗ Branch 1 not taken.
|
108 | LogInfo(VB_CHANNELOUT, "BBBPru: DDR region: '%s' at 0x%zX-0x%zX (%zuKB)\n", |
| 664 | owner.c_str(), pos, pos + size, size / 1024); | ||
| 665 | 108 | return ddr_mem_loc + pos; | |
| 666 | 112 | } | |
| 667 | |||
| 668 | 169 | void BBBPru::ddrRelease(const std::string& owner) { | |
| 669 |
1/2✓ Branch 0 taken 169 times.
✗ Branch 1 not taken.
|
169 | std::unique_lock<std::mutex> lock(ddrAllocLock); |
| 670 |
1/2✓ Branch 0 taken 169 times.
✗ Branch 1 not taken.
|
169 | ddrAllocs.erase(owner); |
| 671 | 169 | } | |
| 672 | |||
| 673 | 223 | void BBBPru::stop() { | |
| 674 |
2/2✓ Branch 0 taken 25 times.
✓ Branch 1 taken 62 times.
|
87 | if (!FAKE_PRU) { |
| 675 | 161 | haltCore(pru_num); | |
| 676 | 161 | prus[pru_num].disable(); | |
| 677 | 161 | restorePRUSleepFirmware(pru_num); | |
| 678 | } | ||
| 679 | 223 | } | |
| 680 | |||
| 681 | 1 | void BBBPru::pause() { | |
| 682 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (FAKE_PRU) { |
| 683 | ✗ | return; | |
| 684 | } | ||
| 685 | 1 | volatile uint32_t* ctrl = pruControlReg(pru_num); | |
| 686 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (ctrl) { |
| 687 | 1 | *ctrl = *ctrl & ~PRU_CTRL_ENABLE; | |
| 688 | 1 | __sync_synchronize(); | |
| 689 | } | ||
| 690 | } | ||
| 691 | |||
| 692 | 1 | void BBBPru::resume() { | |
| 693 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (FAKE_PRU) { |
| 694 | ✗ | return; | |
| 695 | } | ||
| 696 | 1 | volatile uint32_t* ctrl = pruControlReg(pru_num); | |
| 697 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (ctrl) { |
| 698 | 1 | *ctrl = *ctrl | PRU_CTRL_ENABLE; | |
| 699 | 1 | __sync_synchronize(); | |
| 700 | } | ||
| 701 | } | ||
| 702 | |||
| 703 | #include "../pru/SMEMRing.hp" | ||
| 704 | |||
| 705 | 57 | void BBBPruSMEMRing::attach(BBBPru* pru, uint32_t pruBaseAddr, uint32_t ringSize, bool usePointerMode) { | |
| 706 | 57 | basePru = pruBaseAddr; | |
| 707 | 57 | size = ringSize; | |
| 708 | 57 | pointerMode = usePointerMode; | |
| 709 | 57 | writeOff = 0; | |
| 710 | 57 | produced = 0; | |
| 711 | 57 | uint8_t* arm = pru->shared_ram + (pruBaseAddr - SMEM_RING_SMEM_PRU_ADDR); | |
| 712 | 57 | ring = (volatile uint64_t*)arm; | |
| 713 | 57 | ctrl = (volatile uint32_t*)(arm + ringSize); | |
| 714 |
2/2✓ Branch 0 taken 56 times.
✓ Branch 1 taken 1 times.
|
57 | ctrl[0] = pointerMode ? basePru : 0; |
| 715 |
2/2✓ Branch 0 taken 56 times.
✓ Branch 1 taken 1 times.
|
57 | ctrl[1] = pointerMode ? basePru : 0; |
| 716 | // the firmware polls for the config; base first, then the nonzero size | ||
| 717 | 57 | volatile uint32_t* rc = (volatile uint32_t*)(pru->data_ram + SMEM_RING_CONFIG_OFFSET); | |
| 718 | 57 | rc[0] = basePru; | |
| 719 | 57 | rc[1] = ringSize; | |
| 720 | 57 | } | |
| 721 | |||
| 722 | 397581 | uint32_t BBBPruSMEMRing::write(const uint8_t* src, uint32_t len) { | |
| 723 | // Counters are free running, so their difference is the exact number of | ||
| 724 | // bytes in flight across the 2^32 wrap. Reducing a raw count modulo the | ||
| 725 | // ring size is not: 2^32 is not a multiple of the size, so each wrap moved | ||
| 726 | // the computed read position (by 4096 bytes on a 30720 byte ring) and, | ||
| 727 | // once that error was large enough, let the producer overwrite data the | ||
| 728 | // consumer had not read yet. | ||
| 729 |
2/2✓ Branch 0 taken 142049 times.
✓ Branch 1 taken 255532 times.
|
397581 | uint32_t used = pointerMode ? (writeOff - (ctrl[1] - basePru) + size) % size |
| 730 | 255532 | : (produced - ctrl[1]); | |
| 731 | 397581 | uint32_t space = size - 64 - used; | |
| 732 |
2/2✓ Branch 0 taken 367114 times.
✓ Branch 1 taken 30467 times.
|
397581 | if (len > space) { |
| 733 | 367114 | len = space; | |
| 734 | } | ||
| 735 | 397581 | uint32_t total = 0; | |
| 736 |
2/2✓ Branch 0 taken 310946 times.
✓ Branch 1 taken 397581 times.
|
708527 | while (total < len) { |
| 737 | 310946 | uint32_t n = len - total; | |
| 738 |
2/2✓ Branch 0 taken 13330 times.
✓ Branch 1 taken 297616 times.
|
310946 | if (n > (size - writeOff)) { |
| 739 | 13330 | n = size - writeOff; | |
| 740 | } | ||
| 741 | 310946 | volatile uint64_t* d = ring + writeOff / 8; | |
| 742 | 310946 | const uint64_t* s = (const uint64_t*)(src + total); | |
| 743 |
2/2✓ Branch 0 taken 37043224 times.
✓ Branch 1 taken 310946 times.
|
37354170 | for (uint32_t i = 0; i < n / 8; i++) { |
| 744 | 37043224 | d[i] = s[i]; | |
| 745 | } | ||
| 746 | 310946 | writeOff += n; | |
| 747 |
2/2✓ Branch 0 taken 14148 times.
✓ Branch 1 taken 296798 times.
|
310946 | if (writeOff == size) { |
| 748 | 14148 | writeOff = 0; | |
| 749 | } | ||
| 750 | 310946 | produced += n; | |
| 751 | 310946 | total += n; | |
| 752 | } | ||
| 753 |
2/2✓ Branch 0 taken 297616 times.
✓ Branch 1 taken 99965 times.
|
397581 | if (total) { |
| 754 | // the data must be observable before the position update | ||
| 755 | 297616 | __sync_synchronize(); | |
| 756 |
2/2✓ Branch 0 taken 72875 times.
✓ Branch 1 taken 224741 times.
|
297616 | ctrl[0] = pointerMode ? (basePru + writeOff) : produced; |
| 757 | } | ||
| 758 | 397581 | return total; | |
| 759 | } | ||
| 760 | |||
| 761 | 114 | void CompilePRUProgram(const std::string& script, const std::vector<std::string>& args) { | |
| 762 | // argv is built before the fork: the child of a threaded process should | ||
| 763 | // do nothing but exec | ||
| 764 | 114 | std::vector<char*> argv; | |
| 765 |
1/2✓ Branch 0 taken 114 times.
✗ Branch 1 not taken.
|
114 | argv.push_back((char*)"/bin/bash"); |
| 766 |
1/2✓ Branch 0 taken 114 times.
✗ Branch 1 not taken.
|
114 | argv.push_back((char*)script.c_str()); |
| 767 |
1/2✓ Branch 0 taken 114 times.
✗ Branch 1 not taken.
|
114 | std::string log = script; |
| 768 |
2/2✓ Branch 0 taken 1604 times.
✓ Branch 1 taken 114 times.
|
1718 | for (auto& a : args) { |
| 769 |
1/2✓ Branch 0 taken 1604 times.
✗ Branch 1 not taken.
|
1604 | argv.push_back((char*)a.c_str()); |
| 770 |
2/4✓ Branch 0 taken 1604 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1604 times.
✗ Branch 3 not taken.
|
1604 | log += " " + a; |
| 771 | } | ||
| 772 |
1/2✓ Branch 0 taken 114 times.
✗ Branch 1 not taken.
|
114 | argv.push_back(nullptr); |
| 773 |
1/2✓ Branch 0 taken 114 times.
✗ Branch 1 not taken.
|
114 | LogDebug(VB_CHANNELOUT, "Compiling PRU program: %s\n", log.c_str()); |
| 774 | |||
| 775 | 114 | pid_t pid = fork(); | |
| 776 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 228 times.
|
228 | if (pid < 0) { |
| 777 | ✗ | LogErr(VB_CHANNELOUT, "Could not fork to compile PRU program %s: %s\n", script.c_str(), strerror(errno)); | |
| 778 | ✗ | return; | |
| 779 | } | ||
| 780 |
2/2✓ Branch 0 taken 114 times.
✓ Branch 1 taken 114 times.
|
228 | if (pid == 0) { |
| 781 | 114 | execv("/bin/bash", argv.data()); | |
| 782 | // never return into the caller: that would be a second fppd | ||
| 783 | 114 | _exit(127); | |
| 784 | } | ||
| 785 | // returns when this child exits (with ECHILD, as it is reaped for us) | ||
| 786 |
6/8✓ Branch 0 taken 124 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 124 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 10 times.
✓ Branch 5 taken 114 times.
✓ Branch 6 taken 10 times.
✓ Branch 7 taken 114 times.
|
124 | while (waitpid(pid, nullptr, 0) < 0 && errno == EINTR) { |
| 787 | } | ||
| 788 |
2/4✓ Branch 0 taken 114 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 114 times.
✗ Branch 3 not taken.
|
114 | } |
| 789 |