2 * SPU processing offload to TI C64x DSP using bsp's c64_tools
3 * (C) GraÅžvydas "notaz" Ignotas, 2015
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:
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
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
27 #include <inc_libc64_mini.h>
30 static dsp_mem_region_t region;
31 static dsp_component_id_t compid;
35 int (*dsp_open)(void);
36 dsp_mem_region_t (*dsp_shm_alloc)(dsp_cache_t _type, sU32 _numBytes);
37 int (*dsp_shm_free)(dsp_mem_region_t _mem);
38 void (*dsp_close)(void);
39 int (*dsp_component_load)(const char *_path, const char *_name, dsp_component_id_t *_id);
40 int (*dsp_cache_inv_virt)(void *_virtAddr, sU32 _size);
41 int (*dsp_rpc_send)(const dsp_msg_t *_msgTo);
42 int (*dsp_rpc_recv)(dsp_msg_t *_msgFrom);
43 int (*dsp_rpc)(const dsp_msg_t *_msgTo, dsp_msg_t *_msgFrom);
44 void (*dsp_logbuf_print)(void);
47 static void thread_work_start(void)
52 DSP_MSG_INIT(&msg, compid, CCMD_DOIT, 0, 0);
53 ret = f.dsp_rpc_send(&msg);
55 fprintf(stderr, "dsp_rpc_send failed: %d\n", ret);
57 // maybe stop using the DSP?
61 static void thread_work_wait_sync(void)
67 ns_to = worker->ns_to;
68 f.dsp_cache_inv_virt(spu.sRVBStart, sizeof(spu.sRVBStart[0]) * 2 * ns_to);
69 f.dsp_cache_inv_virt(SSumLR, sizeof(SSumLR[0]) * 2 * ns_to);
70 f.dsp_cache_inv_virt(&worker->r, sizeof(worker->r));
71 worker->stale_cache = 1; // SB, ram
73 ret = f.dsp_rpc_recv(&msg);
75 fprintf(stderr, "dsp_rpc_recv failed: %d\n", ret);
78 //f.dsp_logbuf_print();
81 // called before ARM decides to do SPU mixing itself
82 static void thread_sync_caches(void)
84 if (worker->stale_cache) {
85 f.dsp_cache_inv_virt(spu.SB, sizeof(spu.SB[0]) * SB_SIZE * 24);
86 f.dsp_cache_inv_virt(spu.spuMemC + 0x800, 0x800);
87 worker->stale_cache = 0;
91 static void init_spu_thread(void)
93 dsp_msg_t init_msg, msg_in;
94 struct region_mem *mem;
97 if (f.handle == NULL) {
98 const char lib[] = "libc64.so.1";
101 f.handle = dlopen(lib, RTLD_NOW);
102 if (f.handle == NULL) {
103 fprintf(stderr, "can't load %s: %s\n", lib, dlerror());
107 failed |= (f.name = dlsym(f.handle, #name)) == NULL
112 LDS(dsp_cache_inv_virt);
113 LDS(dsp_component_load);
117 LDS(dsp_logbuf_print);
120 fprintf(stderr, "missing symbol(s) in %s\n", lib);
129 fprintf(stderr, "dsp_open failed: %d\n", ret);
133 ret = f.dsp_component_load(NULL, COMPONENT_NAME, &compid);
135 fprintf(stderr, "dsp_component_load failed: %d\n", ret);
139 region = f.dsp_shm_alloc(DSP_CACHE_R, sizeof(*mem)); // writethrough
140 if (region.size < sizeof(*mem) || region.virt_addr == 0) {
141 fprintf(stderr, "dsp_shm_alloc failed\n");
144 mem = (void *)region.virt_addr;
146 memcpy(&mem->spu_config, &spu_config, sizeof(mem->spu_config));
148 DSP_MSG_INIT(&init_msg, compid, CCMD_INIT, region.phys_addr, 0);
149 ret = f.dsp_rpc(&init_msg, &msg_in);
151 fprintf(stderr, "dsp_rpc failed: %d\n", ret);
155 if (mem->sizeof_region_mem != sizeof(*mem)) {
156 fprintf(stderr, "error: size mismatch 1: %d vs %zd\n",
157 mem->sizeof_region_mem, sizeof(*mem));
160 if (mem->offsetof_s_chan1 != offsetof(typeof(*mem), s_chan[1])) {
161 fprintf(stderr, "error: size mismatch 2: %d vs %zd\n",
162 mem->offsetof_s_chan1, offsetof(typeof(*mem), s_chan[1]));
165 if (mem->offsetof_worker_ram != offsetof(typeof(*mem), worker.ch[1])) {
166 fprintf(stderr, "error: size mismatch 3: %d vs %zd\n",
167 mem->offsetof_worker_ram, offsetof(typeof(*mem), worker.ch[1]));
171 // override default allocations
173 spu.spuMemC = mem->spu_ram;
175 spu.sRVBStart = mem->RVB;
177 SSumLR = mem->SSumLR;
181 spu.s_chan = mem->s_chan;
182 worker = &mem->worker;
184 printf("spu: C64x DSP ready (id=%d).\n", (int)compid);
185 f.dsp_logbuf_print();
188 (void)do_channel_work; // used by DSP instead
192 f.dsp_shm_free(region);
194 // no component unload func?
196 printf("spu: C64x DSP init failed.\n");
197 f.dsp_logbuf_print();
202 static void exit_spu_thread(void)
208 thread_work_wait_sync();
209 f.dsp_shm_free(region);
213 spu.sRVBStart = NULL;
220 // vim:shiftwidth=1:expandtab