spu: put reverb on the thread too
[pcsx_rearmed.git] / plugins / dfsound / spu_c64x.c
CommitLineData
5514a050 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>
de4a0279 25#include <stddef.h>
3bd31caf 26#include <unistd.h>
de4a0279 27
5514a050 28#include <inc_libc64_mini.h>
29#include "spu_c64x.h"
30
5514a050 31static 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);
de4a0279 41 int (*dsp_rpc)(const dsp_msg_t *_msgTo, dsp_msg_t *_msgFrom);
5514a050 42 void (*dsp_logbuf_print)(void);
3bd31caf 43
44 dsp_mem_region_t region;
45 dsp_component_id_t compid;
05c7cec7 46 unsigned int stale_caches:1;
47 unsigned int req_sent:1;
5514a050 48} f;
49
50static void thread_work_start(void)
51{
3bd31caf 52 struct region_mem *mem;
de4a0279 53 dsp_msg_t msg;
54 int ret;
55
3bd31caf 56 // make sure new work is written out
57 __sync_synchronize();
58
59 // this should be safe, as dsp checks for new work even
60 // after it decrements ->active
61 // cacheline: i_done, active
62 f.dsp_cache_inv_virt(&worker->i_done, 64);
63 if (worker->active == ACTIVE_CNT)
64 return;
65
66 // to start the DSP, dsp_rpc_send() must be used,
67 // but before that, previous request must be finished
05c7cec7 68 if (f.req_sent) {
3bd31caf 69 if (worker->boot_cnt == worker->last_boot_cnt) {
70 // hopefully still booting
71 //printf("booting?\n");
72 return;
73 }
74
75 ret = f.dsp_rpc_recv(&msg);
76 if (ret != 0) {
77 fprintf(stderr, "dsp_rpc_recv failed: %d\n", ret);
78 f.dsp_logbuf_print();
05c7cec7 79 f.req_sent = 0;
3bd31caf 80 spu_config.iUseThread = 0;
81 return;
82 }
83 }
84
85 f.dsp_cache_inv_virt(&worker->i_done, 64);
86 worker->last_boot_cnt = worker->boot_cnt;
87
88 mem = (void *)f.region.virt_addr;
89 memcpy(&mem->spu_config, &spu_config, sizeof(mem->spu_config));
90
91 DSP_MSG_INIT(&msg, f.compid, CCMD_DOIT, f.region.phys_addr, 0);
de4a0279 92 ret = f.dsp_rpc_send(&msg);
93 if (ret != 0) {
94 fprintf(stderr, "dsp_rpc_send failed: %d\n", ret);
95 f.dsp_logbuf_print();
3bd31caf 96 spu_config.iUseThread = 0;
97 return;
de4a0279 98 }
05c7cec7 99 f.req_sent = 1;
5514a050 100}
101
3bd31caf 102static int thread_get_i_done(void)
5514a050 103{
3bd31caf 104 f.dsp_cache_inv_virt(&worker->i_done, sizeof(worker->i_done));
105 return worker->i_done;
106}
107
108static void thread_work_wait_sync(struct work_item *work, int force)
109{
110 int limit = 1000;
de4a0279 111 int ns_to;
de4a0279 112
3bd31caf 113 while (worker->i_done == worker->i_reaped && limit-- > 0) {
05c7cec7 114 if (!f.req_sent) {
115 printf("dsp: req not sent?\n");
116 break;
117 }
118
119 if (worker->boot_cnt != worker->last_boot_cnt && !worker->active) {
3bd31caf 120 printf("dsp: broken sync\n");
121 worker->last_boot_cnt = ~0;
122 break;
123 }
124
125 usleep(500);
126 f.dsp_cache_inv_virt(&worker->i_done, 64);
de4a0279 127 }
de4a0279 128
05c7cec7 129 ns_to = work->ns_to;
130 f.dsp_cache_inv_virt(work->SSumLR, sizeof(work->SSumLR[0]) * 2 * ns_to);
131 preload(work->SSumLR);
132 preload(work->SSumLR + 64/4);
133
134 f.stale_caches = 1; // SB, spuMem
135
3bd31caf 136 if (limit == 0)
137 printf("dsp: wait timeout\n");
138
139 // still in results loop?
140 if (worker->i_reaped != worker->i_done - 1)
141 return;
142
05c7cec7 143 if (f.req_sent && (force || worker->i_done == worker->i_ready)) {
3bd31caf 144 dsp_msg_t msg;
145 int ret;
146
147 ret = f.dsp_rpc_recv(&msg);
148 if (ret != 0) {
149 fprintf(stderr, "dsp_rpc_recv failed: %d\n", ret);
150 f.dsp_logbuf_print();
151 spu_config.iUseThread = 0;
152 }
05c7cec7 153 f.req_sent = 0;
3bd31caf 154 }
05c7cec7 155}
3bd31caf 156
05c7cec7 157static void thread_sync_caches(void)
158{
159 if (f.stale_caches) {
de4a0279 160 f.dsp_cache_inv_virt(spu.SB, sizeof(spu.SB[0]) * SB_SIZE * 24);
161 f.dsp_cache_inv_virt(spu.spuMemC + 0x800, 0x800);
05c7cec7 162 if (spu.rvb->StartAddr) {
163 int left = 0x40000 - spu.rvb->StartAddr;
164 f.dsp_cache_inv_virt(spu.spuMem + spu.rvb->StartAddr, left * 2);
165 }
166 f.stale_caches = 0;
de4a0279 167 }
5514a050 168}
169
170static void init_spu_thread(void)
171{
de4a0279 172 dsp_msg_t init_msg, msg_in;
5514a050 173 struct region_mem *mem;
174 int ret;
175
176 if (f.handle == NULL) {
177 const char lib[] = "libc64.so.1";
178 int failed = 0;
179
180 f.handle = dlopen(lib, RTLD_NOW);
181 if (f.handle == NULL) {
182 fprintf(stderr, "can't load %s: %s\n", lib, dlerror());
3bd31caf 183 goto fail_open;
5514a050 184 }
185 #define LDS(name) \
186 failed |= (f.name = dlsym(f.handle, #name)) == NULL
187 LDS(dsp_open);
188 LDS(dsp_close);
189 LDS(dsp_shm_alloc);
190 LDS(dsp_shm_free);
191 LDS(dsp_cache_inv_virt);
192 LDS(dsp_component_load);
193 LDS(dsp_rpc_send);
194 LDS(dsp_rpc_recv);
de4a0279 195 LDS(dsp_rpc);
5514a050 196 LDS(dsp_logbuf_print);
197 #undef LDS
198 if (failed) {
199 fprintf(stderr, "missing symbol(s) in %s\n", lib);
200 dlclose(f.handle);
201 f.handle = NULL;
3bd31caf 202 goto fail_open;
5514a050 203 }
204 }
205
206 ret = f.dsp_open();
207 if (ret != 0) {
208 fprintf(stderr, "dsp_open failed: %d\n", ret);
3bd31caf 209 goto fail_open;
5514a050 210 }
211
3bd31caf 212 ret = f.dsp_component_load(NULL, COMPONENT_NAME, &f.compid);
de4a0279 213 if (ret != 0) {
214 fprintf(stderr, "dsp_component_load failed: %d\n", ret);
215 goto fail_cload;
216 }
217
3bd31caf 218 f.region = f.dsp_shm_alloc(DSP_CACHE_R, sizeof(*mem)); // writethrough
219 if (f.region.size < sizeof(*mem) || f.region.virt_addr == 0) {
5514a050 220 fprintf(stderr, "dsp_shm_alloc failed\n");
221 goto fail_mem;
222 }
3bd31caf 223 mem = (void *)f.region.virt_addr;
5514a050 224
de4a0279 225 memcpy(&mem->spu_config, &spu_config, sizeof(mem->spu_config));
226
3bd31caf 227 DSP_MSG_INIT(&init_msg, f.compid, CCMD_INIT, f.region.phys_addr, 0);
de4a0279 228 ret = f.dsp_rpc(&init_msg, &msg_in);
229 if (ret != 0) {
230 fprintf(stderr, "dsp_rpc failed: %d\n", ret);
231 goto fail_init;
232 }
233
234 if (mem->sizeof_region_mem != sizeof(*mem)) {
235 fprintf(stderr, "error: size mismatch 1: %d vs %zd\n",
236 mem->sizeof_region_mem, sizeof(*mem));
237 goto fail_init;
238 }
05c7cec7 239 if (mem->offsetof_s_chan1 != offsetof(typeof(*mem), in.s_chan[1])) {
de4a0279 240 fprintf(stderr, "error: size mismatch 2: %d vs %zd\n",
05c7cec7 241 mem->offsetof_s_chan1, offsetof(typeof(*mem), in.s_chan[1]));
de4a0279 242 goto fail_init;
243 }
3bd31caf 244 if (mem->offsetof_spos_3_20 != offsetof(typeof(*mem), worker.i[3].ch[20])) {
de4a0279 245 fprintf(stderr, "error: size mismatch 3: %d vs %zd\n",
3bd31caf 246 mem->offsetof_spos_3_20, offsetof(typeof(*mem), worker.i[3].ch[20]));
de4a0279 247 goto fail_init;
248 }
249
5514a050 250 // override default allocations
251 free(spu.spuMemC);
252 spu.spuMemC = mem->spu_ram;
de4a0279 253 free(spu.SB);
254 spu.SB = mem->SB;
5514a050 255 free(spu.s_chan);
05c7cec7 256 spu.s_chan = mem->in.s_chan;
257 free(spu.rvb);
258 spu.rvb = &mem->in.rvb;
5514a050 259 worker = &mem->worker;
260
3bd31caf 261 printf("spu: C64x DSP ready (id=%d).\n", (int)f.compid);
de4a0279 262 f.dsp_logbuf_print();
263
3bd31caf 264 spu_config.iThreadAvail = 1;
de4a0279 265 (void)do_channel_work; // used by DSP instead
5514a050 266 return;
267
de4a0279 268fail_init:
3bd31caf 269 f.dsp_shm_free(f.region);
5514a050 270fail_mem:
de4a0279 271 // no component unload func?
272fail_cload:
de4a0279 273 f.dsp_logbuf_print();
5514a050 274 f.dsp_close();
3bd31caf 275fail_open:
276 printf("spu: C64x DSP init failed.\n");
277 spu_config.iUseThread = spu_config.iThreadAvail = 0;
5514a050 278 worker = NULL;
279}
280
281static void exit_spu_thread(void)
282{
3bd31caf 283 dsp_msg_t msg;
284
5514a050 285 if (worker == NULL)
286 return;
287
05c7cec7 288 if (f.req_sent) {
3bd31caf 289 f.dsp_rpc_recv(&msg);
05c7cec7 290 f.req_sent = 0;
291 }
3bd31caf 292
293 f.dsp_logbuf_print();
294 f.dsp_shm_free(f.region);
5514a050 295 f.dsp_close();
296
297 spu.spuMemC = NULL;
de4a0279 298 spu.SB = NULL;
5514a050 299 spu.s_chan = NULL;
05c7cec7 300 spu.rvb = NULL;
5514a050 301 worker = NULL;
302}
303
304// vim:shiftwidth=1:expandtab