source: mainline/uspace/drv/audio/sb16/dsp.c

Last change on this file was a64970e1, checked in by Jiri Svoboda <jiri@…>, 4 months ago

Implement quiesce in HD Audio and SB16 drivers.

  • Property mode set to 100644
File size: 14.7 KB
Line 
1/*
2 * Copyright (c) 2025 Jiri Svoboda
3 * Copyright (c) 2011 Jan Vesely
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29/** @addtogroup drvaudiosb16
30 * @{
31 */
32/** @file
33 * @brief DSP helper functions implementation
34 */
35
36#include <as.h>
37#include <stdbool.h>
38#include <ddf/driver.h>
39#include <ddi.h>
40#include <device/hw_res.h>
41#include <libarch/ddi.h>
42#include <barrier.h>
43#include <macros.h>
44#include <str_error.h>
45#include <audio_pcm_iface.h>
46
47#include "ddf_log.h"
48#include "dsp_commands.h"
49#include "dsp.h"
50
51/* Maximum allowed transfer size for ISA DMA transfers is 64kB */
52#define MAX_BUFFER_SIZE (64 * 1024)
53
54#ifndef DSP_RETRY_COUNT
55#define DSP_RETRY_COUNT 100
56#endif
57
58#define DSP_RESET_RESPONSE 0xaa
59
60/* These are only for SB16 (DSP4.00+) */
61#define DSP_RATE_UPPER_LIMIT 44100
62#define DSP_RATE_LOWER_LIMIT 5000
63
64#define AUTO_DMA_MODE
65
66static inline const char *dsp_state_to_str(dsp_state_t state)
67{
68 static const char *state_names[] = {
69 [DSP_PLAYBACK_ACTIVE_EVENTS] = "PLAYBACK w/ EVENTS",
70 [DSP_CAPTURE_ACTIVE_EVENTS] = "CAPTURE w/ EVENTS",
71 [DSP_PLAYBACK_NOEVENTS] = "PLAYBACK w/o EVENTS",
72 [DSP_CAPTURE_NOEVENTS] = "CAPTURE w/o EVENTS",
73 [DSP_PLAYBACK_TERMINATE] = "PLAYBACK TERMINATE",
74 [DSP_CAPTURE_TERMINATE] = "CAPTURE TERMINATE",
75 [DSP_READY] = "READY",
76 [DSP_NO_BUFFER] = "NO BUFFER",
77 [DSP_QUIESCED] = "QUIESCED"
78 };
79 if ((size_t)state < ARRAY_SIZE(state_names))
80 return state_names[state];
81 return "UNKNOWN";
82}
83
84static inline void dsp_change_state(sb_dsp_t *dsp, dsp_state_t state)
85{
86 assert(dsp);
87 if (dsp->state == DSP_QUIESCED)
88 return;
89
90 ddf_log_verbose("Changing state from %s to %s",
91 dsp_state_to_str(dsp->state), dsp_state_to_str(state));
92 dsp->state = state;
93}
94
95static inline errno_t dsp_read(sb_dsp_t *dsp, uint8_t *data)
96{
97 assert(data);
98 assert(dsp);
99 uint8_t status;
100 size_t attempts = DSP_RETRY_COUNT;
101 do {
102 status = pio_read_8(&dsp->regs->dsp_read_status);
103 } while (--attempts && ((status & DSP_READ_READY) == 0));
104
105 if ((status & DSP_READ_READY) == 0)
106 return EIO;
107
108 *data = pio_read_8(&dsp->regs->dsp_data_read);
109 return EOK;
110}
111
112static inline errno_t dsp_write(sb_dsp_t *dsp, uint8_t data)
113{
114 assert(dsp);
115 uint8_t status;
116 size_t attempts = DSP_RETRY_COUNT;
117 do {
118 status = pio_read_8(&dsp->regs->dsp_write);
119 } while (--attempts && ((status & DSP_WRITE_BUSY) != 0));
120
121 if ((status & DSP_WRITE_BUSY))
122 return EIO;
123
124 pio_write_8(&dsp->regs->dsp_write, data);
125 return EOK;
126}
127
128static inline void dsp_reset(sb_dsp_t *dsp)
129{
130 assert(dsp);
131 /* Reset DSP, see Chapter 2 of Sound Blaster HW programming guide */
132 pio_write_8(&dsp->regs->dsp_reset, 1);
133 udelay(3); /* Keep reset for 3 us */
134 pio_write_8(&dsp->regs->dsp_reset, 0);
135 /* "DSP takes about 100 microseconds to initialize itself" */
136 udelay(100);
137}
138
139static inline void dsp_start_current_active(sb_dsp_t *dsp, uint8_t command)
140{
141 dsp_write(dsp, command);
142 dsp_write(dsp, dsp->active.mode);
143 dsp_write(dsp, (dsp->active.samples - 1) & 0xff);
144 dsp_write(dsp, (dsp->active.samples - 1) >> 8);
145}
146
147static inline void dsp_set_sampling_rate(sb_dsp_t *dsp, unsigned rate)
148{
149 dsp_write(dsp, SET_SAMPLING_RATE_OUTPUT);
150 uint8_t rate_lo = rate & 0xff;
151 uint8_t rate_hi = rate >> 8;
152 dsp_write(dsp, rate_hi);
153 dsp_write(dsp, rate_lo);
154 ddf_log_verbose("Sampling rate: %hhx:%hhx.", rate_hi, rate_lo);
155}
156
157static inline void dsp_report_event(sb_dsp_t *dsp, pcm_event_t event)
158{
159 assert(dsp);
160 if (!dsp->event_exchange)
161 ddf_log_warning("No one listening for event %u", event);
162 async_msg_1(dsp->event_exchange, event, dsp->active.frame_count);
163}
164
165static inline errno_t setup_dma(sb_dsp_t *dsp, uintptr_t pa, size_t size)
166{
167 async_sess_t *sess = ddf_dev_parent_sess_get(dsp->sb_dev);
168
169 return hw_res_dma_channel_setup(sess,
170 dsp->dma16_channel, pa, size,
171 DMA_MODE_READ | DMA_MODE_AUTO | DMA_MODE_ON_DEMAND);
172}
173
174static inline errno_t setup_buffer(sb_dsp_t *dsp, size_t size)
175{
176 assert(dsp);
177
178 if ((size > MAX_BUFFER_SIZE) || (size == 0) || ((size % 2) == 1))
179 size = MAX_BUFFER_SIZE;
180
181 uintptr_t pa = 0;
182 void *buffer = AS_AREA_ANY;
183
184 errno_t ret = dmamem_map_anonymous(size, DMAMEM_16MiB | 0x0000ffff,
185 AS_AREA_WRITE | AS_AREA_READ, 0, &pa, &buffer);
186 if (ret != EOK) {
187 ddf_log_error("Failed to allocate DMA buffer.");
188 return ENOMEM;
189 }
190
191 ddf_log_verbose("Setup DMA buffer at %p (%zu) %zu.", buffer, pa, size);
192 assert(pa < (1 << 24));
193
194 /* Setup 16 bit channel */
195 ret = setup_dma(dsp, pa, size);
196 if (ret == EOK) {
197 dsp->buffer.data = buffer;
198 dsp->buffer.size = size;
199 } else {
200 ddf_log_error("Failed to setup DMA16 channel: %s.",
201 str_error(ret));
202 dmamem_unmap_anonymous(buffer);
203 }
204
205 return ret;
206}
207
208errno_t sb_dsp_init(sb_dsp_t *dsp, sb16_regs_t *regs, ddf_dev_t *dev,
209 int dma8, int dma16)
210{
211 assert(dsp);
212 dsp->regs = regs;
213 dsp->dma8_channel = dma8;
214 dsp->dma16_channel = dma16;
215 dsp->event_session = NULL;
216 dsp->event_exchange = NULL;
217 dsp->sb_dev = dev;
218 dsp->state = DSP_NO_BUFFER;
219 dsp_reset(dsp);
220 uint8_t response;
221 const errno_t ret = dsp_read(dsp, &response);
222 if (ret != EOK) {
223 ddf_log_error("Failed to read DSP reset response value.");
224 return ret;
225 }
226 if (response != DSP_RESET_RESPONSE) {
227 ddf_log_error("Invalid DSP reset response: %x.", response);
228 return EIO;
229 }
230
231 /* Get DSP version number */
232 dsp_write(dsp, DSP_VERSION);
233 dsp_read(dsp, &dsp->version.major);
234 dsp_read(dsp, &dsp->version.minor);
235
236 return ret;
237}
238
239void sb_dsp_quiesce(sb_dsp_t *dsp)
240{
241 dsp->state = DSP_QUIESCED;
242 dsp_reset(dsp);
243}
244
245void sb_dsp_interrupt(sb_dsp_t *dsp)
246{
247 assert(dsp);
248
249 dsp->active.frame_count +=
250 dsp->active.samples / ((dsp->active.mode & DSP_MODE_STEREO) ? 2 : 1);
251
252 switch (dsp->state) {
253 case DSP_PLAYBACK_ACTIVE_EVENTS:
254 dsp_report_event(dsp, PCM_EVENT_FRAMES_PLAYED);
255 case DSP_PLAYBACK_NOEVENTS:
256#ifndef AUTO_DMA_MODE
257 dsp_start_current_active(dsp, SINGLE_DMA_16B_DA);
258#endif
259 break;
260 case DSP_CAPTURE_ACTIVE_EVENTS:
261 dsp_report_event(dsp, PCM_EVENT_FRAMES_CAPTURED);
262 case DSP_CAPTURE_NOEVENTS:
263#ifndef AUTO_DMA_MODE
264 dsp_start_current_active(dsp, SINGLE_DMA_16B_DA);
265#endif
266 break;
267 case DSP_CAPTURE_TERMINATE:
268 dsp_change_state(dsp, DSP_READY);
269 dsp_report_event(dsp, PCM_EVENT_CAPTURE_TERMINATED);
270 async_exchange_end(dsp->event_exchange);
271 dsp->event_exchange = NULL;
272 break;
273 case DSP_PLAYBACK_TERMINATE:
274 dsp_change_state(dsp, DSP_READY);
275 dsp_report_event(dsp, PCM_EVENT_PLAYBACK_TERMINATED);
276 async_exchange_end(dsp->event_exchange);
277 dsp->event_exchange = NULL;
278 break;
279 default:
280 ddf_log_warning("Interrupt while DSP not active (%s)",
281 dsp_state_to_str(dsp->state));
282 }
283}
284
285unsigned sb_dsp_query_cap(sb_dsp_t *dsp, audio_cap_t cap)
286{
287 ddf_log_verbose("Querying cap %s", audio_pcm_cap_str(cap));
288 switch (cap) {
289 case AUDIO_CAP_CAPTURE:
290 case AUDIO_CAP_PLAYBACK:
291 case AUDIO_CAP_INTERRUPT:
292 case AUDIO_CAP_BUFFER_POS:
293 return 1;
294 case AUDIO_CAP_MAX_BUFFER:
295 return MAX_BUFFER_SIZE;
296 case AUDIO_CAP_INTERRUPT_MIN_FRAMES:
297 return 1;
298 case AUDIO_CAP_INTERRUPT_MAX_FRAMES:
299 return 16535;
300 default:
301 return -1;
302 }
303}
304
305errno_t sb_dsp_get_buffer_position(sb_dsp_t *dsp, size_t *pos)
306{
307 if (dsp->state == DSP_NO_BUFFER)
308 return ENOENT;
309
310 assert(dsp->buffer.data);
311 async_sess_t *sess = ddf_dev_parent_sess_get(dsp->sb_dev);
312
313 // TODO: Assumes DMA 16
314 size_t remain;
315 errno_t rc = hw_res_dma_channel_remain(sess, dsp->dma16_channel, &remain);
316 if (rc == EOK) {
317 *pos = dsp->buffer.size - remain;
318 }
319 return rc;
320}
321
322errno_t sb_dsp_test_format(sb_dsp_t *dsp, unsigned *channels, unsigned *rate,
323 pcm_sample_format_t *format)
324{
325 errno_t ret = EOK;
326 if (*channels == 0 || *channels > 2) {
327 *channels = 2;
328 ret = ELIMIT;
329 }
330 //TODO 8bit DMA supports 8bit formats
331 if (*format != PCM_SAMPLE_SINT16_LE && *format != PCM_SAMPLE_UINT16_LE) {
332 *format = pcm_sample_format_is_signed(*format) ?
333 PCM_SAMPLE_SINT16_LE : PCM_SAMPLE_UINT16_LE;
334 ret = ELIMIT;
335 }
336 if (*rate > DSP_RATE_UPPER_LIMIT) {
337 *rate = DSP_RATE_UPPER_LIMIT;
338 ret = ELIMIT;
339 }
340 if (*rate < DSP_RATE_LOWER_LIMIT) {
341 *rate = DSP_RATE_LOWER_LIMIT;
342 ret = ELIMIT;
343 }
344 return ret;
345}
346
347errno_t sb_dsp_set_event_session(sb_dsp_t *dsp, async_sess_t *session)
348{
349 assert(dsp);
350 if (dsp->event_session && session)
351 return EBUSY;
352 dsp->event_session = session;
353 ddf_log_debug("Set event session to %p.", session);
354 return EOK;
355}
356
357async_sess_t *sb_dsp_get_event_session(sb_dsp_t *dsp)
358{
359 assert(dsp);
360 ddf_log_debug("Get event session: %p.", dsp->event_session);
361 return dsp->event_session;
362}
363
364errno_t sb_dsp_get_buffer(sb_dsp_t *dsp, void **buffer, size_t *size)
365{
366 assert(dsp);
367 assert(size);
368
369 /*
370 * buffer is already setup by for someone, refuse to work until
371 * it's released
372 */
373 if (dsp->state != DSP_NO_BUFFER)
374 return EBUSY;
375 assert(dsp->buffer.data == NULL);
376
377 const errno_t ret = setup_buffer(dsp, *size);
378 if (ret == EOK) {
379 ddf_log_debug("Providing buffer: %p, %zu B.",
380 dsp->buffer.data, dsp->buffer.size);
381
382 if (buffer)
383 *buffer = dsp->buffer.data;
384 if (size)
385 *size = dsp->buffer.size;
386 dsp_change_state(dsp, DSP_READY);
387 }
388 return ret;
389}
390
391errno_t sb_dsp_release_buffer(sb_dsp_t *dsp)
392{
393 assert(dsp);
394 if (dsp->state != DSP_READY)
395 return EINVAL;
396 assert(dsp->buffer.data);
397 dmamem_unmap_anonymous(dsp->buffer.data);
398 dsp->buffer.data = NULL;
399 dsp->buffer.size = 0;
400 ddf_log_debug("DSP buffer released.");
401 dsp_change_state(dsp, DSP_NO_BUFFER);
402 return EOK;
403}
404
405errno_t sb_dsp_start_playback(sb_dsp_t *dsp, unsigned frames,
406 unsigned channels, unsigned sampling_rate, pcm_sample_format_t format)
407{
408 assert(dsp);
409
410 if (!dsp->buffer.data || dsp->state != DSP_READY)
411 return EINVAL;
412
413 /* Check supported parameters */
414 ddf_log_debug("Requested playback: %u frames, %uHz, %s, %u channel(s).",
415 frames, sampling_rate, pcm_sample_format_str(format), channels);
416 if (sb_dsp_test_format(dsp, &channels, &sampling_rate, &format) != EOK)
417 return ENOTSUP;
418
419 /* Client requested regular events */
420 if (frames && !dsp->event_session)
421 return EINVAL;
422
423 if (dsp->event_session) {
424 dsp->event_exchange = async_exchange_begin(dsp->event_session);
425 if (!dsp->event_exchange)
426 return ENOMEM;
427 }
428
429 dsp->active.mode = 0 |
430 (pcm_sample_format_is_signed(format) ? DSP_MODE_SIGNED : 0) |
431 (channels == 2 ? DSP_MODE_STEREO : 0);
432 dsp->active.samples = frames * channels;
433 dsp->active.frame_count = 0;
434
435 dsp_set_sampling_rate(dsp, sampling_rate);
436
437#ifdef AUTO_DMA_MODE
438 dsp_start_current_active(dsp, AUTO_DMA_16B_DA_FIFO);
439#else
440 dsp_start_current_active(dsp, SINGLE_DMA_16B_DA);
441#endif
442
443 ddf_log_verbose("Playback started, event every %u samples",
444 dsp->active.samples);
445
446 dsp_change_state(dsp,
447 frames ? DSP_PLAYBACK_ACTIVE_EVENTS : DSP_PLAYBACK_NOEVENTS);
448 if (dsp->state == DSP_PLAYBACK_ACTIVE_EVENTS)
449 dsp_report_event(dsp, PCM_EVENT_PLAYBACK_STARTED);
450
451 return EOK;
452}
453
454errno_t sb_dsp_stop_playback(sb_dsp_t *dsp, bool immediate)
455{
456 assert(dsp);
457 if ((dsp->state == DSP_PLAYBACK_NOEVENTS ||
458 dsp->state == DSP_PLAYBACK_ACTIVE_EVENTS) &&
459 immediate) {
460 dsp_write(dsp, DMA_16B_PAUSE);
461 dsp_reset(dsp);
462 ddf_log_debug("Stopped playback");
463 dsp_change_state(dsp, DSP_READY);
464 if (dsp->event_exchange) {
465 dsp_report_event(dsp, PCM_EVENT_PLAYBACK_TERMINATED);
466 async_exchange_end(dsp->event_exchange);
467 dsp->event_exchange = NULL;
468 }
469 return EOK;
470 }
471 if (dsp->state == DSP_PLAYBACK_ACTIVE_EVENTS) {
472 /* Stop after current fragment */
473 assert(!immediate);
474 dsp_write(dsp, DMA_16B_EXIT);
475 ddf_log_debug("Last playback fragment");
476 dsp_change_state(dsp, DSP_PLAYBACK_TERMINATE);
477 return EOK;
478 }
479 return EINVAL;
480}
481
482errno_t sb_dsp_start_capture(sb_dsp_t *dsp, unsigned frames,
483 unsigned channels, unsigned sampling_rate, pcm_sample_format_t format)
484{
485 assert(dsp);
486 if (!dsp->buffer.data || dsp->state != DSP_READY)
487 return EINVAL;
488
489 /* Check supported parameters */
490 ddf_log_debug("Requested capture: %u frames, %uHz, %s, %u channel(s).",
491 frames, sampling_rate, pcm_sample_format_str(format), channels);
492 if (sb_dsp_test_format(dsp, &channels, &sampling_rate, &format) != EOK)
493 return ENOTSUP;
494
495 /* Client requested regular events */
496 if (frames && !dsp->event_session)
497 return EINVAL;
498
499 if (dsp->event_session) {
500 dsp->event_exchange = async_exchange_begin(dsp->event_session);
501 if (!dsp->event_exchange)
502 return ENOMEM;
503 }
504
505 dsp->active.mode = 0 |
506 (pcm_sample_format_is_signed(format) ? DSP_MODE_SIGNED : 0) |
507 (channels == 2 ? DSP_MODE_STEREO : 0);
508 dsp->active.samples = frames * channels;
509 dsp->active.frame_count = 0;
510
511 dsp_set_sampling_rate(dsp, sampling_rate);
512
513#ifdef AUTO_DMA_MODE
514 dsp_start_current_active(dsp, AUTO_DMA_16B_AD_FIFO);
515#else
516 dsp_start_current_active(dsp, SINGLE_DMA_16B_AD);
517#endif
518
519 ddf_log_verbose("Capture started started, event every %u samples",
520 dsp->active.samples);
521 dsp_change_state(dsp,
522 frames ? DSP_CAPTURE_ACTIVE_EVENTS : DSP_CAPTURE_NOEVENTS);
523 if (dsp->state == DSP_CAPTURE_ACTIVE_EVENTS)
524 dsp_report_event(dsp, PCM_EVENT_CAPTURE_STARTED);
525 return EOK;
526}
527
528errno_t sb_dsp_stop_capture(sb_dsp_t *dsp, bool immediate)
529{
530 assert(dsp);
531 if ((dsp->state == DSP_CAPTURE_NOEVENTS ||
532 dsp->state == DSP_CAPTURE_ACTIVE_EVENTS) &&
533 immediate) {
534 dsp_write(dsp, DMA_16B_PAUSE);
535 dsp_reset(dsp);
536 ddf_log_debug("Stopped capture fragment");
537 dsp_change_state(dsp, DSP_READY);
538 if (dsp->event_exchange) {
539 dsp_report_event(dsp, PCM_EVENT_CAPTURE_TERMINATED);
540 async_exchange_end(dsp->event_exchange);
541 dsp->event_exchange = NULL;
542 }
543 return EOK;
544 }
545 if (dsp->state == DSP_CAPTURE_ACTIVE_EVENTS) {
546 /* Stop after current fragment */
547 assert(!immediate);
548 dsp_write(dsp, DMA_16B_EXIT);
549 ddf_log_debug("Last capture fragment");
550 dsp_change_state(dsp, DSP_CAPTURE_TERMINATE);
551 return EOK;
552 }
553 return EINVAL;
554}
555/**
556 * @}
557 */
Note: See TracBrowser for help on using the repository browser.