| 1 | /* |
| 2 | * SPU processing offload to TI C64x DSP using bsp's c64_tools |
| 3 | * (C) GraÅžvydas "notaz" Ignotas, 2015 |
| 4 | * |
| 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 6 | * this software and associated documentation files (the "Software"), to deal in |
| 7 | * the Software without restriction, including without limitation the rights to |
| 8 | * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies |
| 9 | * of the Software, and to permit persons to whom the Software is furnished to do |
| 10 | * so, subject to the following conditions: |
| 11 | * |
| 12 | * The above copyright notice and this permission notice shall be included in all |
| 13 | * copies or substantial portions of the Software. |
| 14 | * |
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | * SOFTWARE. |
| 22 | */ |
| 23 | |
| 24 | #include <dlfcn.h> |
| 25 | #include <stddef.h> |
| 26 | #include <unistd.h> |
| 27 | |
| 28 | #include <inc_libc64_mini.h> |
| 29 | #include "spu_c64x.h" |
| 30 | |
| 31 | static struct { |
| 32 | void *handle; |
| 33 | int (*dsp_open)(void); |
| 34 | dsp_mem_region_t (*dsp_shm_alloc)(dsp_cache_t _type, sU32 _numBytes); |
| 35 | int (*dsp_shm_free)(dsp_mem_region_t _mem); |
| 36 | void (*dsp_close)(void); |
| 37 | int (*dsp_component_load)(const char *_path, const char *_name, dsp_component_id_t *_id); |
| 38 | int (*dsp_cache_inv_virt)(void *_virtAddr, sU32 _size); |
| 39 | int (*dsp_rpc_send)(const dsp_msg_t *_msgTo); |
| 40 | int (*dsp_rpc_recv)(dsp_msg_t *_msgFrom); |
| 41 | int (*dsp_rpc)(const dsp_msg_t *_msgTo, dsp_msg_t *_msgFrom); |
| 42 | void (*dsp_logbuf_print)(void); |
| 43 | |
| 44 | dsp_mem_region_t region; |
| 45 | dsp_component_id_t compid; |
| 46 | } f; |
| 47 | |
| 48 | static void thread_work_start(void) |
| 49 | { |
| 50 | struct region_mem *mem; |
| 51 | dsp_msg_t msg; |
| 52 | int ret; |
| 53 | |
| 54 | // make sure new work is written out |
| 55 | __sync_synchronize(); |
| 56 | |
| 57 | // this should be safe, as dsp checks for new work even |
| 58 | // after it decrements ->active |
| 59 | // cacheline: i_done, active |
| 60 | f.dsp_cache_inv_virt(&worker->i_done, 64); |
| 61 | if (worker->active == ACTIVE_CNT) |
| 62 | return; |
| 63 | |
| 64 | // to start the DSP, dsp_rpc_send() must be used, |
| 65 | // but before that, previous request must be finished |
| 66 | if (worker->req_sent) { |
| 67 | if (worker->boot_cnt == worker->last_boot_cnt) { |
| 68 | // hopefully still booting |
| 69 | //printf("booting?\n"); |
| 70 | return; |
| 71 | } |
| 72 | |
| 73 | ret = f.dsp_rpc_recv(&msg); |
| 74 | if (ret != 0) { |
| 75 | fprintf(stderr, "dsp_rpc_recv failed: %d\n", ret); |
| 76 | f.dsp_logbuf_print(); |
| 77 | worker->req_sent = 0; |
| 78 | spu_config.iUseThread = 0; |
| 79 | return; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | f.dsp_cache_inv_virt(&worker->i_done, 64); |
| 84 | worker->last_boot_cnt = worker->boot_cnt; |
| 85 | |
| 86 | mem = (void *)f.region.virt_addr; |
| 87 | memcpy(&mem->spu_config, &spu_config, sizeof(mem->spu_config)); |
| 88 | |
| 89 | DSP_MSG_INIT(&msg, f.compid, CCMD_DOIT, f.region.phys_addr, 0); |
| 90 | ret = f.dsp_rpc_send(&msg); |
| 91 | if (ret != 0) { |
| 92 | fprintf(stderr, "dsp_rpc_send failed: %d\n", ret); |
| 93 | f.dsp_logbuf_print(); |
| 94 | spu_config.iUseThread = 0; |
| 95 | return; |
| 96 | } |
| 97 | worker->req_sent = 1; |
| 98 | } |
| 99 | |
| 100 | static int thread_get_i_done(void) |
| 101 | { |
| 102 | f.dsp_cache_inv_virt(&worker->i_done, sizeof(worker->i_done)); |
| 103 | return worker->i_done; |
| 104 | } |
| 105 | |
| 106 | static void thread_work_wait_sync(struct work_item *work, int force) |
| 107 | { |
| 108 | int limit = 1000; |
| 109 | int ns_to; |
| 110 | |
| 111 | ns_to = work->ns_to; |
| 112 | f.dsp_cache_inv_virt(work->RVB, sizeof(work->RVB[0]) * 2 * ns_to); |
| 113 | f.dsp_cache_inv_virt(work->SSumLR, sizeof(work->SSumLR[0]) * 2 * ns_to); |
| 114 | __builtin_prefetch(work->RVB); |
| 115 | __builtin_prefetch(work->SSumLR); |
| 116 | |
| 117 | while (worker->i_done == worker->i_reaped && limit-- > 0) { |
| 118 | if (!worker->active) { |
| 119 | printf("dsp: broken sync\n"); |
| 120 | worker->last_boot_cnt = ~0; |
| 121 | break; |
| 122 | } |
| 123 | |
| 124 | usleep(500); |
| 125 | f.dsp_cache_inv_virt(&worker->i_done, 64); |
| 126 | } |
| 127 | |
| 128 | if (limit == 0) |
| 129 | printf("dsp: wait timeout\n"); |
| 130 | |
| 131 | // still in results loop? |
| 132 | if (worker->i_reaped != worker->i_done - 1) |
| 133 | return; |
| 134 | |
| 135 | if (worker->req_sent && (force || worker->i_done == worker->i_ready)) { |
| 136 | dsp_msg_t msg; |
| 137 | int ret; |
| 138 | |
| 139 | ret = f.dsp_rpc_recv(&msg); |
| 140 | if (ret != 0) { |
| 141 | fprintf(stderr, "dsp_rpc_recv failed: %d\n", ret); |
| 142 | f.dsp_logbuf_print(); |
| 143 | spu_config.iUseThread = 0; |
| 144 | } |
| 145 | worker->req_sent = 0; |
| 146 | } |
| 147 | |
| 148 | if (force) { |
| 149 | f.dsp_cache_inv_virt(spu.SB, sizeof(spu.SB[0]) * SB_SIZE * 24); |
| 150 | f.dsp_cache_inv_virt(spu.spuMemC + 0x800, 0x800); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | static void init_spu_thread(void) |
| 155 | { |
| 156 | dsp_msg_t init_msg, msg_in; |
| 157 | struct region_mem *mem; |
| 158 | int ret; |
| 159 | |
| 160 | if (f.handle == NULL) { |
| 161 | const char lib[] = "libc64.so.1"; |
| 162 | int failed = 0; |
| 163 | |
| 164 | f.handle = dlopen(lib, RTLD_NOW); |
| 165 | if (f.handle == NULL) { |
| 166 | fprintf(stderr, "can't load %s: %s\n", lib, dlerror()); |
| 167 | goto fail_open; |
| 168 | } |
| 169 | #define LDS(name) \ |
| 170 | failed |= (f.name = dlsym(f.handle, #name)) == NULL |
| 171 | LDS(dsp_open); |
| 172 | LDS(dsp_close); |
| 173 | LDS(dsp_shm_alloc); |
| 174 | LDS(dsp_shm_free); |
| 175 | LDS(dsp_cache_inv_virt); |
| 176 | LDS(dsp_component_load); |
| 177 | LDS(dsp_rpc_send); |
| 178 | LDS(dsp_rpc_recv); |
| 179 | LDS(dsp_rpc); |
| 180 | LDS(dsp_logbuf_print); |
| 181 | #undef LDS |
| 182 | if (failed) { |
| 183 | fprintf(stderr, "missing symbol(s) in %s\n", lib); |
| 184 | dlclose(f.handle); |
| 185 | f.handle = NULL; |
| 186 | goto fail_open; |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | ret = f.dsp_open(); |
| 191 | if (ret != 0) { |
| 192 | fprintf(stderr, "dsp_open failed: %d\n", ret); |
| 193 | goto fail_open; |
| 194 | } |
| 195 | |
| 196 | ret = f.dsp_component_load(NULL, COMPONENT_NAME, &f.compid); |
| 197 | if (ret != 0) { |
| 198 | fprintf(stderr, "dsp_component_load failed: %d\n", ret); |
| 199 | goto fail_cload; |
| 200 | } |
| 201 | |
| 202 | f.region = f.dsp_shm_alloc(DSP_CACHE_R, sizeof(*mem)); // writethrough |
| 203 | if (f.region.size < sizeof(*mem) || f.region.virt_addr == 0) { |
| 204 | fprintf(stderr, "dsp_shm_alloc failed\n"); |
| 205 | goto fail_mem; |
| 206 | } |
| 207 | mem = (void *)f.region.virt_addr; |
| 208 | |
| 209 | memcpy(&mem->spu_config, &spu_config, sizeof(mem->spu_config)); |
| 210 | |
| 211 | DSP_MSG_INIT(&init_msg, f.compid, CCMD_INIT, f.region.phys_addr, 0); |
| 212 | ret = f.dsp_rpc(&init_msg, &msg_in); |
| 213 | if (ret != 0) { |
| 214 | fprintf(stderr, "dsp_rpc failed: %d\n", ret); |
| 215 | goto fail_init; |
| 216 | } |
| 217 | |
| 218 | if (mem->sizeof_region_mem != sizeof(*mem)) { |
| 219 | fprintf(stderr, "error: size mismatch 1: %d vs %zd\n", |
| 220 | mem->sizeof_region_mem, sizeof(*mem)); |
| 221 | goto fail_init; |
| 222 | } |
| 223 | if (mem->offsetof_s_chan1 != offsetof(typeof(*mem), s_chan[1])) { |
| 224 | fprintf(stderr, "error: size mismatch 2: %d vs %zd\n", |
| 225 | mem->offsetof_s_chan1, offsetof(typeof(*mem), s_chan[1])); |
| 226 | goto fail_init; |
| 227 | } |
| 228 | if (mem->offsetof_spos_3_20 != offsetof(typeof(*mem), worker.i[3].ch[20])) { |
| 229 | fprintf(stderr, "error: size mismatch 3: %d vs %zd\n", |
| 230 | mem->offsetof_spos_3_20, offsetof(typeof(*mem), worker.i[3].ch[20])); |
| 231 | goto fail_init; |
| 232 | } |
| 233 | |
| 234 | // override default allocations |
| 235 | free(spu.spuMemC); |
| 236 | spu.spuMemC = mem->spu_ram; |
| 237 | free(spu.SB); |
| 238 | spu.SB = mem->SB; |
| 239 | free(spu.s_chan); |
| 240 | spu.s_chan = mem->s_chan; |
| 241 | worker = &mem->worker; |
| 242 | |
| 243 | printf("spu: C64x DSP ready (id=%d).\n", (int)f.compid); |
| 244 | f.dsp_logbuf_print(); |
| 245 | |
| 246 | spu_config.iThreadAvail = 1; |
| 247 | (void)do_channel_work; // used by DSP instead |
| 248 | return; |
| 249 | |
| 250 | fail_init: |
| 251 | f.dsp_shm_free(f.region); |
| 252 | fail_mem: |
| 253 | // no component unload func? |
| 254 | fail_cload: |
| 255 | f.dsp_logbuf_print(); |
| 256 | f.dsp_close(); |
| 257 | fail_open: |
| 258 | printf("spu: C64x DSP init failed.\n"); |
| 259 | spu_config.iUseThread = spu_config.iThreadAvail = 0; |
| 260 | worker = NULL; |
| 261 | } |
| 262 | |
| 263 | static void exit_spu_thread(void) |
| 264 | { |
| 265 | dsp_msg_t msg; |
| 266 | |
| 267 | if (worker == NULL) |
| 268 | return; |
| 269 | |
| 270 | if (worker->req_sent) |
| 271 | f.dsp_rpc_recv(&msg); |
| 272 | |
| 273 | f.dsp_logbuf_print(); |
| 274 | f.dsp_shm_free(f.region); |
| 275 | f.dsp_close(); |
| 276 | |
| 277 | spu.spuMemC = NULL; |
| 278 | spu.SB = NULL; |
| 279 | spu.s_chan = NULL; |
| 280 | worker = NULL; |
| 281 | } |
| 282 | |
| 283 | // vim:shiftwidth=1:expandtab |