| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #pragma once | ||
| 2 | /* | ||
| 3 | * This file is part of the Falcon Player (FPP) and is Copyright (C) | ||
| 4 | * 2013-2022 by the Falcon Player Developers. | ||
| 5 | * | ||
| 6 | * The Falcon Player (FPP) is free software, and is covered under | ||
| 7 | * multiple Open Source licenses. Please see the included 'LICENSES' | ||
| 8 | * file for descriptions of what files are covered by each license. | ||
| 9 | * | ||
| 10 | * This source file is covered under the LGPL v2.1 as described in the | ||
| 11 | * included LICENSE.LGPL file. | ||
| 12 | */ | ||
| 13 | |||
| 14 | #include <inttypes.h> | ||
| 15 | #include <stdio.h> | ||
| 16 | #include <string> | ||
| 17 | #include <vector> | ||
| 18 | |||
| 19 | /** | ||
| 20 | * Mapping of the PRU memory spaces. | ||
| 21 | * | ||
| 22 | * The PRU has a 8K, fast local data RAM that is mapped into ARM memory, | ||
| 23 | * as well as a 12K, fast local data RAM that is shared for both PRU's | ||
| 24 | * as well as slower access to the DDR RAM of the ARM. | ||
| 25 | */ | ||
| 26 | class BBBPru { | ||
| 27 | public: | ||
| 28 | BBBPru(int pru, bool mapShared = false, bool mapOther = false); | ||
| 29 | ~BBBPru(); | ||
| 30 | |||
| 31 | // Call between writing a frame into the DDR region and writing the PRU | ||
| 32 | // memory word (address, command) that tells the PRU to read it. The DDR | ||
| 33 | // region is write-combined Normal memory and the PRU memories are Device | ||
| 34 | // memory, and nothing orders stores to the two against each other without | ||
| 35 | // a barrier; the PRU is outside the inner shareable domain, so it has to | ||
| 36 | // be dmb sy, not the dmb ish of __sync_synchronize(). An empty asm with | ||
| 37 | // a "memory" clobber only stops the compiler reordering and emits nothing. | ||
| 38 | 7668 | static inline void frameDataBarrier() { | |
| 39 | #if defined(__arm__) || defined(__aarch64__) | ||
| 40 | 7668 | __asm__ __volatile__("dmb sy" ::: "memory"); | |
| 41 | #else | ||
| 42 | __asm__ __volatile__("" ::: "memory"); | ||
| 43 | #endif | ||
| 44 | 7668 | } | |
| 45 | |||
| 46 | // clearSharedMems: clear the shared RAM and (if mapped) the other | ||
| 47 | // PRU's data RAM after the firmware load. Pass false when another | ||
| 48 | // driver owns the other PRU (a shared panels + strings cape) so a | ||
| 49 | // restart of this output cannot wipe its ring or command state. | ||
| 50 | // clearMems: false skips clearing any PRU memory, before or after the | ||
| 51 | // core starts, for firmware that zeroes its own handshake words before | ||
| 52 | // reading them and only reads data the ARM wrote ahead of the command | ||
| 53 | // that releases it. run() then does not touch PRU memory at all. | ||
| 54 | // Otherwise the memories are cleared once the core runs, and also before | ||
| 55 | // it where the PRUSS takes writes with its cores stopped (the AM62x, not | ||
| 56 | // the AM335x: BBBPruUtils.cpp CLEAR_BEFORE_ENABLE). | ||
| 57 | bool run(const std::string& program, bool clearSharedMems = true, bool clearMems = true); | ||
| 58 | void stop(); | ||
| 59 | |||
| 60 | // Halt/restart the core in place. Unlike stop()/run() these do not go | ||
| 61 | // through remoteproc or reload the firmware image: the program counter | ||
| 62 | // and every register survive, so resume() continues from exactly where | ||
| 63 | // pause() stopped and the data/shared RAM mappings stay usable while the | ||
| 64 | // core is halted. | ||
| 65 | // | ||
| 66 | // Two things to know before pausing: | ||
| 67 | // - the output pins hold whatever the program last drove onto them, so | ||
| 68 | // only pause where the firmware has parked them somewhere safe | ||
| 69 | // - the cycle counter stops, which is usually the point: it saturates | ||
| 70 | // after 2^32 cycles (~17s at 250MHz) and the hardware clears CTR_EN | ||
| 71 | // when it does. RESET_PRU_CLOCK does not recover from that, so any | ||
| 72 | // firmware spinning on GET_PRU_CLOCK would hang forever afterwards. | ||
| 73 | // A core left running but idle for >17s hits this; a paused one does | ||
| 74 | // not. | ||
| 75 | void pause(); | ||
| 76 | void resume(); | ||
| 77 | |||
| 78 | void clearPRUMem(uint8_t* ptr, size_t sz); | ||
| 79 | void memcpyToPRU(uint8_t* dst, uint8_t* src, size_t sz); | ||
| 80 | |||
| 81 | unsigned pru_num; | ||
| 82 | |||
| 83 | uint8_t* data_ram; // PRU data ram in ARM space | ||
| 84 | size_t data_ram_size; // size in bytes of the PRU's data RAM | ||
| 85 | |||
| 86 | uint8_t* shared_ram; // PRU data ram in ARM space | ||
| 87 | size_t shared_ram_size; // size in bytes of the PRU's data RAM | ||
| 88 | |||
| 89 | uint8_t* other_data_ram; // PRU data ram in ARM space | ||
| 90 | size_t other_data_ram_size; // size in bytes of the PRU's data RAM | ||
| 91 | |||
| 92 | uint8_t* ddr; // PRU DMA address (in ARM space) | ||
| 93 | |||
| 94 | // Shared allocator for the PRU reserved DDR region. Multiple outputs | ||
| 95 | // can coexist on it (K8-B-Scroller: BBB48String strings + BBBMatrix | ||
| 96 | // panels + BBBSerial) and historically used hardcoded offsets that | ||
| 97 | // could silently overlap. Allocations are page aligned, first fit | ||
| 98 | // (partial config reloads free and re-request in arbitrary order), and | ||
| 99 | // named so a re-request by the same owner replaces its allocation. | ||
| 100 | // Returns nullptr (and logs) when the region cannot fit the request. | ||
| 101 | static uint8_t* ddrAlloc(const std::string& owner, size_t size, uint32_t& physAddr, size_t minOffset = 0); | ||
| 102 | static void ddrRelease(const std::string& owner); | ||
| 103 | uint32_t ddr_addr; // PRU DMA address (in PRU space) | ||
| 104 | size_t ddr_size; // Size in bytes of the shared space | ||
| 105 | |||
| 106 | uint8_t* sram; // SRAM data ram in ARM space | ||
| 107 | uint32_t sram_addr; // SRAM address (in PRU space) | ||
| 108 | size_t sram_size; // size in bytes of the SRAM | ||
| 109 | |||
| 110 | uint8_t* m4ram; // M4RAM data ram in ARM space | ||
| 111 | uint32_t m4ram_addr; // M4RAM address (in PRU space) | ||
| 112 | size_t m4ram_size; // size in bytes of the M4RAM | ||
| 113 | }; | ||
| 114 | |||
| 115 | /** | ||
| 116 | * ARM-side producer for a ring buffer in the PRU shared RAM, used to stream | ||
| 117 | * output data to a PRU program with short, predictable read latency instead | ||
| 118 | * of having the PRU read DDR (1.1us+ per read on the AM62x, with much larger | ||
| 119 | * spikes). The layout and the data-RAM config handshake are described in | ||
| 120 | * pru/SMEMRing.hp; the firmware polls its data RAM for the ring location, so | ||
| 121 | * attach() must be called after BBBPru::run(). | ||
| 122 | * | ||
| 123 | * Two flow-control conventions are supported, chosen by the firmware: | ||
| 124 | * - counters: the control words hold free-running produced/consumed byte | ||
| 125 | * counts (used when the consumer wants to cache its available window) | ||
| 126 | * - pointers: the control words hold the write/read addresses; equality | ||
| 127 | * means empty, which requires all writes to be multiples of the consumer | ||
| 128 | * block size (64 bytes) | ||
| 129 | * The ring is never filled completely so full and empty stay unambiguous. | ||
| 130 | */ | ||
| 131 | class BBBPruSMEMRing { | ||
| 132 | public: | ||
| 133 | // writes the ring config into the PRU data RAM (the firmware polls for | ||
| 134 | // it) and resets the control words; pru must have been created with | ||
| 135 | // mapShared=true | ||
| 136 | void attach(BBBPru* pru, uint32_t pruBaseAddr, uint32_t ringSize, bool usePointerMode); | ||
| 137 | 705807 | bool attached() const { return ring != nullptr; } | |
| 138 | |||
| 139 | // copies up to len bytes (multiple of 8, from an 8-byte aligned src) | ||
| 140 | // into the ring using aligned 64-bit stores (the mapping is device | ||
| 141 | // memory) and publishes the new write position; returns the number of | ||
| 142 | // bytes written, limited by the available space | ||
| 143 | uint32_t write(const uint8_t* src, uint32_t len); | ||
| 144 | |||
| 145 | // PRU address the next byte will be written at. A producer that hands | ||
| 146 | // its consumer an explicit frame start publishes this one: at a frame | ||
| 147 | // boundary nothing of the new frame has been written yet, so it is that | ||
| 148 | // frame's first byte. | ||
| 149 | 5648 | uint32_t writePos() const { return basePru + writeOff; } | |
| 150 | |||
| 151 | // PRU address the consumer has published as its read position | ||
| 152 | uint32_t readPos() const { return ctrl ? ctrl[1] : basePru; } | ||
| 153 | |||
| 154 | // Re-seat the write position onto the consumer's published read position, | ||
| 155 | // abandoning anything still unread. This is a recovery path, not a | ||
| 156 | // steady-state one: it is for a producer that has given up waiting for a | ||
| 157 | // consumer that will never read what it was handed, where carrying on from | ||
| 158 | // the old write position would leave the ring permanently non-empty and | ||
| 159 | // the output dead. Returns true if it actually had to move. | ||
| 160 | 495 | bool seekToConsumer() { | |
| 161 |
2/4✓ Branch 0 taken 495 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 495 times.
|
495 | if (!ctrl || !pointerMode) { |
| 162 | ✗ | return false; | |
| 163 | } | ||
| 164 | 495 | uint32_t off = (ctrl[1] - basePru) % size; | |
| 165 | // Republish unconditionally. The published write position is the one | ||
| 166 | // the consumer compares against, and it can be wrong on its own - if | ||
| 167 | // this returned early whenever the local offset already agreed with | ||
| 168 | // the consumer, a stale published value would never be corrected and | ||
| 169 | // the ring would read as permanently occupied. | ||
| 170 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 495 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
495 | bool moved = off != writeOff || ctrl[0] != basePru + off; |
| 171 | 495 | writeOff = off; | |
| 172 | 495 | __sync_synchronize(); | |
| 173 | 495 | ctrl[0] = basePru + writeOff; | |
| 174 | 495 | return moved; | |
| 175 | } | ||
| 176 | |||
| 177 | // bytes the consumer has not caught up on, from the published positions | ||
| 178 | 495 | uint32_t usedBytes() const { | |
| 179 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 495 times.
|
495 | if (!ctrl) { |
| 180 | ✗ | return 0; | |
| 181 | } | ||
| 182 | // counters: the difference is exact across the wrap, and no modulo | ||
| 183 | // may be applied to it; pointers: both lie inside the ring | ||
| 184 |
1/2✓ Branch 0 taken 495 times.
✗ Branch 1 not taken.
|
495 | return pointerMode ? (ctrl[0] - ctrl[1] + size) % size : ctrl[0] - ctrl[1]; |
| 185 | } | ||
| 186 | |||
| 187 | // true once the consumer has caught up with every byte written; both | ||
| 188 | // conventions publish positions that compare equal only when empty. | ||
| 189 | // Producers that stop on a frame boundary can use this to know the PRU | ||
| 190 | // has consumed exactly what was produced, so the two stay aligned in the | ||
| 191 | // byte stream across a pause. | ||
| 192 | // Compared modulo the ring size: a consumer that publishes its read | ||
| 193 | // pointer before wrapping reports "one past the end", which is the same | ||
| 194 | // position as the base. Comparing raw would call that not-drained | ||
| 195 | // forever and wedge a producer waiting on it. | ||
| 196 | 17207 | bool drained() const { | |
| 197 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 17207 times.
|
17207 | if (!ctrl) { |
| 198 | ✗ | return false; | |
| 199 | } | ||
| 200 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 17204 times.
|
17207 | if (!pointerMode) { |
| 201 | 3 | return ctrl[0] == ctrl[1]; | |
| 202 | } | ||
| 203 | 17204 | return ((ctrl[0] - basePru) % size) == ((ctrl[1] - basePru) % size); | |
| 204 | } | ||
| 205 | |||
| 206 | uint32_t size = 0; | ||
| 207 | |||
| 208 | private: | ||
| 209 | volatile uint64_t* ring = nullptr; | ||
| 210 | volatile uint32_t* ctrl = nullptr; | ||
| 211 | uint32_t basePru = 0; | ||
| 212 | uint32_t writeOff = 0; | ||
| 213 | uint32_t produced = 0; | ||
| 214 | bool pointerMode = false; | ||
| 215 | }; | ||
| 216 | |||
| 217 | // Build a PRU program: runs "/bin/bash <script> <args...>" and waits for that | ||
| 218 | // child alone. fppd sets SA_NOCLDWAIT on SIGCHLD, so wait() would block until | ||
| 219 | // every child of fppd had exited (ptp4l, a running script...) and no exit | ||
| 220 | // status survives either way. The script therefore has to delete its outputs | ||
| 221 | // before building, so a failed build leaves nothing for BBBPru::run() to load | ||
| 222 | // instead of the previous configuration's program. | ||
| 223 | void CompilePRUProgram(const std::string& script, const std::vector<std::string>& args); | ||
| 224 |