drm/panthor: Don't add write fences to the shared BOs
[drm/drm-misc.git] / sound / firewire / dice / dice-stream.c
blob4c677c8546c71a8bebc867abb423239d49906108
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * dice_stream.c - a part of driver for DICE based devices
5 * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
6 * Copyright (c) 2014 Takashi Sakamoto <o-takashi@sakamocchi.jp>
7 */
9 #include "dice.h"
11 #define READY_TIMEOUT_MS 200
12 #define NOTIFICATION_TIMEOUT_MS 100
14 struct reg_params {
15 unsigned int count;
16 unsigned int size;
19 const unsigned int snd_dice_rates[SND_DICE_RATES_COUNT] = {
20 /* mode 0 */
21 [0] = 32000,
22 [1] = 44100,
23 [2] = 48000,
24 /* mode 1 */
25 [3] = 88200,
26 [4] = 96000,
27 /* mode 2 */
28 [5] = 176400,
29 [6] = 192000,
32 int snd_dice_stream_get_rate_mode(struct snd_dice *dice, unsigned int rate,
33 enum snd_dice_rate_mode *mode)
35 /* Corresponding to each entry in snd_dice_rates. */
36 static const enum snd_dice_rate_mode modes[] = {
37 [0] = SND_DICE_RATE_MODE_LOW,
38 [1] = SND_DICE_RATE_MODE_LOW,
39 [2] = SND_DICE_RATE_MODE_LOW,
40 [3] = SND_DICE_RATE_MODE_MIDDLE,
41 [4] = SND_DICE_RATE_MODE_MIDDLE,
42 [5] = SND_DICE_RATE_MODE_HIGH,
43 [6] = SND_DICE_RATE_MODE_HIGH,
45 int i;
47 for (i = 0; i < ARRAY_SIZE(snd_dice_rates); i++) {
48 if (!(dice->clock_caps & BIT(i)))
49 continue;
50 if (snd_dice_rates[i] != rate)
51 continue;
53 *mode = modes[i];
54 return 0;
57 return -EINVAL;
60 static int select_clock(struct snd_dice *dice, unsigned int rate)
62 __be32 reg, new;
63 u32 data;
64 int i;
65 int err;
67 err = snd_dice_transaction_read_global(dice, GLOBAL_CLOCK_SELECT,
68 &reg, sizeof(reg));
69 if (err < 0)
70 return err;
72 data = be32_to_cpu(reg);
74 data &= ~CLOCK_RATE_MASK;
75 for (i = 0; i < ARRAY_SIZE(snd_dice_rates); ++i) {
76 if (snd_dice_rates[i] == rate)
77 break;
79 if (i == ARRAY_SIZE(snd_dice_rates))
80 return -EINVAL;
81 data |= i << CLOCK_RATE_SHIFT;
83 if (completion_done(&dice->clock_accepted))
84 reinit_completion(&dice->clock_accepted);
86 new = cpu_to_be32(data);
87 err = snd_dice_transaction_write_global(dice, GLOBAL_CLOCK_SELECT,
88 &new, sizeof(new));
89 if (err < 0)
90 return err;
92 if (wait_for_completion_timeout(&dice->clock_accepted,
93 msecs_to_jiffies(NOTIFICATION_TIMEOUT_MS)) == 0) {
94 if (reg != new)
95 return -ETIMEDOUT;
98 return 0;
101 static int get_register_params(struct snd_dice *dice,
102 struct reg_params *tx_params,
103 struct reg_params *rx_params)
105 __be32 reg[2];
106 int err;
108 err = snd_dice_transaction_read_tx(dice, TX_NUMBER, reg, sizeof(reg));
109 if (err < 0)
110 return err;
111 tx_params->count =
112 min_t(unsigned int, be32_to_cpu(reg[0]), MAX_STREAMS);
113 tx_params->size = be32_to_cpu(reg[1]) * 4;
115 err = snd_dice_transaction_read_rx(dice, RX_NUMBER, reg, sizeof(reg));
116 if (err < 0)
117 return err;
118 rx_params->count =
119 min_t(unsigned int, be32_to_cpu(reg[0]), MAX_STREAMS);
120 rx_params->size = be32_to_cpu(reg[1]) * 4;
122 return 0;
125 static void release_resources(struct snd_dice *dice)
127 int i;
129 for (i = 0; i < MAX_STREAMS; ++i) {
130 fw_iso_resources_free(&dice->tx_resources[i]);
131 fw_iso_resources_free(&dice->rx_resources[i]);
135 static void stop_streams(struct snd_dice *dice, enum amdtp_stream_direction dir,
136 struct reg_params *params)
138 __be32 reg;
139 unsigned int i;
141 for (i = 0; i < params->count; i++) {
142 reg = cpu_to_be32((u32)-1);
143 if (dir == AMDTP_IN_STREAM) {
144 snd_dice_transaction_write_tx(dice,
145 params->size * i + TX_ISOCHRONOUS,
146 &reg, sizeof(reg));
147 } else {
148 snd_dice_transaction_write_rx(dice,
149 params->size * i + RX_ISOCHRONOUS,
150 &reg, sizeof(reg));
155 static int keep_resources(struct snd_dice *dice, struct amdtp_stream *stream,
156 struct fw_iso_resources *resources, unsigned int rate,
157 unsigned int pcm_chs, unsigned int midi_ports)
159 bool double_pcm_frames;
160 unsigned int i;
161 int err;
163 // At 176.4/192.0 kHz, Dice has a quirk to transfer two PCM frames in
164 // one data block of AMDTP packet. Thus sampling transfer frequency is
165 // a half of PCM sampling frequency, i.e. PCM frames at 192.0 kHz are
166 // transferred on AMDTP packets at 96 kHz. Two successive samples of a
167 // channel are stored consecutively in the packet. This quirk is called
168 // as 'Dual Wire'.
169 // For this quirk, blocking mode is required and PCM buffer size should
170 // be aligned to SYT_INTERVAL.
171 double_pcm_frames = (rate > 96000 && !dice->disable_double_pcm_frames);
172 if (double_pcm_frames) {
173 rate /= 2;
174 pcm_chs *= 2;
177 err = amdtp_am824_set_parameters(stream, rate, pcm_chs, midi_ports,
178 double_pcm_frames);
179 if (err < 0)
180 return err;
182 if (double_pcm_frames) {
183 pcm_chs /= 2;
185 for (i = 0; i < pcm_chs; i++) {
186 amdtp_am824_set_pcm_position(stream, i, i * 2);
187 amdtp_am824_set_pcm_position(stream, i + pcm_chs,
188 i * 2 + 1);
192 return fw_iso_resources_allocate(resources,
193 amdtp_stream_get_max_payload(stream),
194 fw_parent_device(dice->unit)->max_speed);
197 static int keep_dual_resources(struct snd_dice *dice, unsigned int rate,
198 enum amdtp_stream_direction dir,
199 struct reg_params *params)
201 enum snd_dice_rate_mode mode;
202 int i;
203 int err;
205 err = snd_dice_stream_get_rate_mode(dice, rate, &mode);
206 if (err < 0)
207 return err;
209 for (i = 0; i < params->count; ++i) {
210 __be32 reg[2];
211 struct amdtp_stream *stream;
212 struct fw_iso_resources *resources;
213 unsigned int pcm_cache;
214 unsigned int pcm_chs;
215 unsigned int midi_ports;
217 if (dir == AMDTP_IN_STREAM) {
218 stream = &dice->tx_stream[i];
219 resources = &dice->tx_resources[i];
221 pcm_cache = dice->tx_pcm_chs[i][mode];
222 err = snd_dice_transaction_read_tx(dice,
223 params->size * i + TX_NUMBER_AUDIO,
224 reg, sizeof(reg));
225 } else {
226 stream = &dice->rx_stream[i];
227 resources = &dice->rx_resources[i];
229 pcm_cache = dice->rx_pcm_chs[i][mode];
230 err = snd_dice_transaction_read_rx(dice,
231 params->size * i + RX_NUMBER_AUDIO,
232 reg, sizeof(reg));
234 if (err < 0)
235 return err;
236 pcm_chs = be32_to_cpu(reg[0]);
237 midi_ports = be32_to_cpu(reg[1]);
239 // These are important for developer of this driver.
240 if (pcm_chs != pcm_cache) {
241 dev_info(&dice->unit->device,
242 "cache mismatch: pcm: %u:%u, midi: %u\n",
243 pcm_chs, pcm_cache, midi_ports);
244 return -EPROTO;
247 err = keep_resources(dice, stream, resources, rate, pcm_chs,
248 midi_ports);
249 if (err < 0)
250 return err;
253 return 0;
256 static void finish_session(struct snd_dice *dice, struct reg_params *tx_params,
257 struct reg_params *rx_params)
259 stop_streams(dice, AMDTP_IN_STREAM, tx_params);
260 stop_streams(dice, AMDTP_OUT_STREAM, rx_params);
262 snd_dice_transaction_clear_enable(dice);
265 int snd_dice_stream_reserve_duplex(struct snd_dice *dice, unsigned int rate,
266 unsigned int events_per_period,
267 unsigned int events_per_buffer)
269 unsigned int curr_rate;
270 int err;
272 // Check sampling transmission frequency.
273 err = snd_dice_transaction_get_rate(dice, &curr_rate);
274 if (err < 0)
275 return err;
276 if (rate == 0)
277 rate = curr_rate;
279 if (dice->substreams_counter == 0 || curr_rate != rate) {
280 struct reg_params tx_params, rx_params;
282 amdtp_domain_stop(&dice->domain);
284 err = get_register_params(dice, &tx_params, &rx_params);
285 if (err < 0)
286 return err;
287 finish_session(dice, &tx_params, &rx_params);
289 release_resources(dice);
291 // Just after owning the unit (GLOBAL_OWNER), the unit can
292 // return invalid stream formats. Selecting clock parameters
293 // have an effect for the unit to refine it.
294 err = select_clock(dice, rate);
295 if (err < 0)
296 return err;
298 // After changing sampling transfer frequency, the value of
299 // register can be changed.
300 err = get_register_params(dice, &tx_params, &rx_params);
301 if (err < 0)
302 return err;
304 err = keep_dual_resources(dice, rate, AMDTP_IN_STREAM,
305 &tx_params);
306 if (err < 0)
307 goto error;
309 err = keep_dual_resources(dice, rate, AMDTP_OUT_STREAM,
310 &rx_params);
311 if (err < 0)
312 goto error;
314 err = amdtp_domain_set_events_per_period(&dice->domain,
315 events_per_period, events_per_buffer);
316 if (err < 0)
317 goto error;
320 return 0;
321 error:
322 release_resources(dice);
323 return err;
326 static int start_streams(struct snd_dice *dice, enum amdtp_stream_direction dir,
327 unsigned int rate, struct reg_params *params)
329 unsigned int max_speed = fw_parent_device(dice->unit)->max_speed;
330 int i;
331 int err;
333 for (i = 0; i < params->count; i++) {
334 struct amdtp_stream *stream;
335 struct fw_iso_resources *resources;
336 __be32 reg;
338 if (dir == AMDTP_IN_STREAM) {
339 stream = dice->tx_stream + i;
340 resources = dice->tx_resources + i;
341 } else {
342 stream = dice->rx_stream + i;
343 resources = dice->rx_resources + i;
346 reg = cpu_to_be32(resources->channel);
347 if (dir == AMDTP_IN_STREAM) {
348 err = snd_dice_transaction_write_tx(dice,
349 params->size * i + TX_ISOCHRONOUS,
350 &reg, sizeof(reg));
351 } else {
352 err = snd_dice_transaction_write_rx(dice,
353 params->size * i + RX_ISOCHRONOUS,
354 &reg, sizeof(reg));
356 if (err < 0)
357 return err;
359 if (dir == AMDTP_IN_STREAM) {
360 reg = cpu_to_be32(max_speed);
361 err = snd_dice_transaction_write_tx(dice,
362 params->size * i + TX_SPEED,
363 &reg, sizeof(reg));
364 if (err < 0)
365 return err;
368 err = amdtp_domain_add_stream(&dice->domain, stream,
369 resources->channel, max_speed);
370 if (err < 0)
371 return err;
374 return 0;
378 * MEMO: After this function, there're two states of streams:
379 * - None streams are running.
380 * - All streams are running.
382 int snd_dice_stream_start_duplex(struct snd_dice *dice)
384 unsigned int generation = dice->rx_resources[0].generation;
385 struct reg_params tx_params, rx_params;
386 unsigned int i;
387 unsigned int rate;
388 enum snd_dice_rate_mode mode;
389 int err;
391 if (dice->substreams_counter == 0)
392 return -EIO;
394 err = get_register_params(dice, &tx_params, &rx_params);
395 if (err < 0)
396 return err;
398 // Check error of packet streaming.
399 for (i = 0; i < MAX_STREAMS; ++i) {
400 if (amdtp_streaming_error(&dice->tx_stream[i]) ||
401 amdtp_streaming_error(&dice->rx_stream[i])) {
402 amdtp_domain_stop(&dice->domain);
403 finish_session(dice, &tx_params, &rx_params);
404 break;
408 if (generation != fw_parent_device(dice->unit)->card->generation) {
409 for (i = 0; i < MAX_STREAMS; ++i) {
410 if (i < tx_params.count)
411 fw_iso_resources_update(dice->tx_resources + i);
412 if (i < rx_params.count)
413 fw_iso_resources_update(dice->rx_resources + i);
417 // Check required streams are running or not.
418 err = snd_dice_transaction_get_rate(dice, &rate);
419 if (err < 0)
420 return err;
421 err = snd_dice_stream_get_rate_mode(dice, rate, &mode);
422 if (err < 0)
423 return err;
424 for (i = 0; i < MAX_STREAMS; ++i) {
425 if (dice->tx_pcm_chs[i][mode] > 0 &&
426 !amdtp_stream_running(&dice->tx_stream[i]))
427 break;
428 if (dice->rx_pcm_chs[i][mode] > 0 &&
429 !amdtp_stream_running(&dice->rx_stream[i]))
430 break;
432 if (i < MAX_STREAMS) {
433 // Start both streams.
434 err = start_streams(dice, AMDTP_IN_STREAM, rate, &tx_params);
435 if (err < 0)
436 goto error;
438 err = start_streams(dice, AMDTP_OUT_STREAM, rate, &rx_params);
439 if (err < 0)
440 goto error;
442 err = snd_dice_transaction_set_enable(dice);
443 if (err < 0) {
444 dev_err(&dice->unit->device,
445 "fail to enable interface\n");
446 goto error;
449 // MEMO: The device immediately starts packet transmission when enabled. Some
450 // devices are strictly to generate any discontinuity in the sequence of tx packet
451 // when they receives invalid sequence of presentation time in CIP header. The
452 // sequence replay for media clock recovery can suppress the behaviour.
453 err = amdtp_domain_start(&dice->domain, 0, true, false);
454 if (err < 0)
455 goto error;
457 if (!amdtp_domain_wait_ready(&dice->domain, READY_TIMEOUT_MS)) {
458 err = -ETIMEDOUT;
459 goto error;
463 return 0;
464 error:
465 amdtp_domain_stop(&dice->domain);
466 finish_session(dice, &tx_params, &rx_params);
467 return err;
471 * MEMO: After this function, there're two states of streams:
472 * - None streams are running.
473 * - All streams are running.
475 void snd_dice_stream_stop_duplex(struct snd_dice *dice)
477 struct reg_params tx_params, rx_params;
479 if (dice->substreams_counter == 0) {
480 if (get_register_params(dice, &tx_params, &rx_params) >= 0)
481 finish_session(dice, &tx_params, &rx_params);
483 amdtp_domain_stop(&dice->domain);
484 release_resources(dice);
488 static int init_stream(struct snd_dice *dice, enum amdtp_stream_direction dir,
489 unsigned int index)
491 struct amdtp_stream *stream;
492 struct fw_iso_resources *resources;
493 int err;
495 if (dir == AMDTP_IN_STREAM) {
496 stream = &dice->tx_stream[index];
497 resources = &dice->tx_resources[index];
498 } else {
499 stream = &dice->rx_stream[index];
500 resources = &dice->rx_resources[index];
503 err = fw_iso_resources_init(resources, dice->unit);
504 if (err < 0)
505 goto end;
506 resources->channels_mask = 0x00000000ffffffffuLL;
508 err = amdtp_am824_init(stream, dice->unit, dir, CIP_BLOCKING);
509 if (err < 0) {
510 amdtp_stream_destroy(stream);
511 fw_iso_resources_destroy(resources);
513 end:
514 return err;
518 * This function should be called before starting streams or after stopping
519 * streams.
521 static void destroy_stream(struct snd_dice *dice,
522 enum amdtp_stream_direction dir,
523 unsigned int index)
525 struct amdtp_stream *stream;
526 struct fw_iso_resources *resources;
528 if (dir == AMDTP_IN_STREAM) {
529 stream = &dice->tx_stream[index];
530 resources = &dice->tx_resources[index];
531 } else {
532 stream = &dice->rx_stream[index];
533 resources = &dice->rx_resources[index];
536 amdtp_stream_destroy(stream);
537 fw_iso_resources_destroy(resources);
540 int snd_dice_stream_init_duplex(struct snd_dice *dice)
542 int i, err;
544 for (i = 0; i < MAX_STREAMS; i++) {
545 err = init_stream(dice, AMDTP_IN_STREAM, i);
546 if (err < 0) {
547 for (; i >= 0; i--)
548 destroy_stream(dice, AMDTP_IN_STREAM, i);
549 goto end;
553 for (i = 0; i < MAX_STREAMS; i++) {
554 err = init_stream(dice, AMDTP_OUT_STREAM, i);
555 if (err < 0) {
556 for (; i >= 0; i--)
557 destroy_stream(dice, AMDTP_OUT_STREAM, i);
558 for (i = 0; i < MAX_STREAMS; i++)
559 destroy_stream(dice, AMDTP_IN_STREAM, i);
560 goto end;
564 err = amdtp_domain_init(&dice->domain);
565 if (err < 0) {
566 for (i = 0; i < MAX_STREAMS; ++i) {
567 destroy_stream(dice, AMDTP_OUT_STREAM, i);
568 destroy_stream(dice, AMDTP_IN_STREAM, i);
571 end:
572 return err;
575 void snd_dice_stream_destroy_duplex(struct snd_dice *dice)
577 unsigned int i;
579 for (i = 0; i < MAX_STREAMS; i++) {
580 destroy_stream(dice, AMDTP_IN_STREAM, i);
581 destroy_stream(dice, AMDTP_OUT_STREAM, i);
584 amdtp_domain_destroy(&dice->domain);
587 void snd_dice_stream_update_duplex(struct snd_dice *dice)
589 struct reg_params tx_params, rx_params;
592 * On a bus reset, the DICE firmware disables streaming and then goes
593 * off contemplating its own navel for hundreds of milliseconds before
594 * it can react to any of our attempts to reenable streaming. This
595 * means that we lose synchronization anyway, so we force our streams
596 * to stop so that the application can restart them in an orderly
597 * manner.
599 dice->global_enabled = false;
601 if (get_register_params(dice, &tx_params, &rx_params) == 0) {
602 amdtp_domain_stop(&dice->domain);
604 stop_streams(dice, AMDTP_IN_STREAM, &tx_params);
605 stop_streams(dice, AMDTP_OUT_STREAM, &rx_params);
609 int snd_dice_stream_detect_current_formats(struct snd_dice *dice)
611 unsigned int rate;
612 enum snd_dice_rate_mode mode;
613 __be32 reg[2];
614 struct reg_params tx_params, rx_params;
615 int i;
616 int err;
618 /* If extended protocol is available, detect detail spec. */
619 err = snd_dice_detect_extension_formats(dice);
620 if (err >= 0)
621 return err;
624 * Available stream format is restricted at current mode of sampling
625 * clock.
627 err = snd_dice_transaction_get_rate(dice, &rate);
628 if (err < 0)
629 return err;
631 err = snd_dice_stream_get_rate_mode(dice, rate, &mode);
632 if (err < 0)
633 return err;
636 * Just after owning the unit (GLOBAL_OWNER), the unit can return
637 * invalid stream formats. Selecting clock parameters have an effect
638 * for the unit to refine it.
640 err = select_clock(dice, rate);
641 if (err < 0)
642 return err;
644 err = get_register_params(dice, &tx_params, &rx_params);
645 if (err < 0)
646 return err;
648 for (i = 0; i < tx_params.count; ++i) {
649 err = snd_dice_transaction_read_tx(dice,
650 tx_params.size * i + TX_NUMBER_AUDIO,
651 reg, sizeof(reg));
652 if (err < 0)
653 return err;
654 dice->tx_pcm_chs[i][mode] = be32_to_cpu(reg[0]);
655 dice->tx_midi_ports[i] = max_t(unsigned int,
656 be32_to_cpu(reg[1]), dice->tx_midi_ports[i]);
658 for (i = 0; i < rx_params.count; ++i) {
659 err = snd_dice_transaction_read_rx(dice,
660 rx_params.size * i + RX_NUMBER_AUDIO,
661 reg, sizeof(reg));
662 if (err < 0)
663 return err;
664 dice->rx_pcm_chs[i][mode] = be32_to_cpu(reg[0]);
665 dice->rx_midi_ports[i] = max_t(unsigned int,
666 be32_to_cpu(reg[1]), dice->rx_midi_ports[i]);
669 return 0;
672 static void dice_lock_changed(struct snd_dice *dice)
674 dice->dev_lock_changed = true;
675 wake_up(&dice->hwdep_wait);
678 int snd_dice_stream_lock_try(struct snd_dice *dice)
680 int err;
682 spin_lock_irq(&dice->lock);
684 if (dice->dev_lock_count < 0) {
685 err = -EBUSY;
686 goto out;
689 if (dice->dev_lock_count++ == 0)
690 dice_lock_changed(dice);
691 err = 0;
692 out:
693 spin_unlock_irq(&dice->lock);
694 return err;
697 void snd_dice_stream_lock_release(struct snd_dice *dice)
699 spin_lock_irq(&dice->lock);
701 if (WARN_ON(dice->dev_lock_count <= 0))
702 goto out;
704 if (--dice->dev_lock_count == 0)
705 dice_lock_changed(dice);
706 out:
707 spin_unlock_irq(&dice->lock);