1
2
3
4
5
6#include <linux/kernel.h>
7#include <linux/delay.h>
8#include <linux/export.h>
9#include <linux/clocksource.h>
10#include <sound/core.h>
11#include <sound/pcm.h>
12#include <sound/hdaudio.h>
13#include <sound/hda_register.h>
14#include "trace.h"
15
16
17
18
19
20
21int snd_hdac_get_stream_stripe_ctl(struct hdac_bus *bus,
22 struct snd_pcm_substream *substream)
23{
24 struct snd_pcm_runtime *runtime = substream->runtime;
25 unsigned int channels = runtime->channels,
26 rate = runtime->rate,
27 bits_per_sample = runtime->sample_bits,
28 max_sdo_lines, value, sdo_line;
29
30
31 max_sdo_lines = snd_hdac_chip_readl(bus, GCAP) & AZX_GCAP_NSDO;
32
33
34 for (sdo_line = max_sdo_lines; sdo_line > 0; sdo_line >>= 1) {
35 if (rate > 48000)
36 value = (channels * bits_per_sample *
37 (rate / 48000)) / sdo_line;
38 else
39 value = (channels * bits_per_sample) / sdo_line;
40
41 if (value >= 8)
42 break;
43 }
44
45
46 return sdo_line >> 1;
47}
48EXPORT_SYMBOL_GPL(snd_hdac_get_stream_stripe_ctl);
49
50
51
52
53
54
55
56
57
58
59
60void snd_hdac_stream_init(struct hdac_bus *bus, struct hdac_stream *azx_dev,
61 int idx, int direction, int tag)
62{
63 azx_dev->bus = bus;
64
65 azx_dev->sd_addr = bus->remap_addr + (0x20 * idx + 0x80);
66
67 azx_dev->sd_int_sta_mask = 1 << idx;
68 azx_dev->index = idx;
69 azx_dev->direction = direction;
70 azx_dev->stream_tag = tag;
71 snd_hdac_dsp_lock_init(azx_dev);
72 list_add_tail(&azx_dev->list, &bus->stream_list);
73}
74EXPORT_SYMBOL_GPL(snd_hdac_stream_init);
75
76
77
78
79
80
81
82
83void snd_hdac_stream_start(struct hdac_stream *azx_dev, bool fresh_start)
84{
85 struct hdac_bus *bus = azx_dev->bus;
86 int stripe_ctl;
87
88 trace_snd_hdac_stream_start(bus, azx_dev);
89
90 azx_dev->start_wallclk = snd_hdac_chip_readl(bus, WALLCLK);
91 if (!fresh_start)
92 azx_dev->start_wallclk -= azx_dev->period_wallclk;
93
94
95 snd_hdac_chip_updatel(bus, INTCTL,
96 1 << azx_dev->index,
97 1 << azx_dev->index);
98
99 if (azx_dev->stripe) {
100 if (azx_dev->substream)
101 stripe_ctl = snd_hdac_get_stream_stripe_ctl(bus, azx_dev->substream);
102 else
103 stripe_ctl = 0;
104 snd_hdac_stream_updateb(azx_dev, SD_CTL_3B, SD_CTL_STRIPE_MASK,
105 stripe_ctl);
106 }
107
108 snd_hdac_stream_updateb(azx_dev, SD_CTL,
109 0, SD_CTL_DMA_START | SD_INT_MASK);
110 azx_dev->running = true;
111}
112EXPORT_SYMBOL_GPL(snd_hdac_stream_start);
113
114
115
116
117
118void snd_hdac_stream_clear(struct hdac_stream *azx_dev)
119{
120 snd_hdac_stream_updateb(azx_dev, SD_CTL,
121 SD_CTL_DMA_START | SD_INT_MASK, 0);
122 snd_hdac_stream_writeb(azx_dev, SD_STS, SD_INT_MASK);
123 if (azx_dev->stripe)
124 snd_hdac_stream_updateb(azx_dev, SD_CTL_3B, SD_CTL_STRIPE_MASK, 0);
125 azx_dev->running = false;
126}
127EXPORT_SYMBOL_GPL(snd_hdac_stream_clear);
128
129
130
131
132
133
134
135void snd_hdac_stream_stop(struct hdac_stream *azx_dev)
136{
137 trace_snd_hdac_stream_stop(azx_dev->bus, azx_dev);
138
139 snd_hdac_stream_clear(azx_dev);
140
141 snd_hdac_chip_updatel(azx_dev->bus, INTCTL, 1 << azx_dev->index, 0);
142}
143EXPORT_SYMBOL_GPL(snd_hdac_stream_stop);
144
145
146
147
148
149void snd_hdac_stream_reset(struct hdac_stream *azx_dev)
150{
151 unsigned char val;
152 int timeout;
153
154 snd_hdac_stream_clear(azx_dev);
155
156 snd_hdac_stream_updateb(azx_dev, SD_CTL, 0, SD_CTL_STREAM_RESET);
157 udelay(3);
158 timeout = 300;
159 do {
160 val = snd_hdac_stream_readb(azx_dev, SD_CTL) &
161 SD_CTL_STREAM_RESET;
162 if (val)
163 break;
164 } while (--timeout);
165 val &= ~SD_CTL_STREAM_RESET;
166 snd_hdac_stream_writeb(azx_dev, SD_CTL, val);
167 udelay(3);
168
169 timeout = 300;
170
171 do {
172 val = snd_hdac_stream_readb(azx_dev, SD_CTL) &
173 SD_CTL_STREAM_RESET;
174 if (!val)
175 break;
176 } while (--timeout);
177
178
179 if (azx_dev->posbuf)
180 *azx_dev->posbuf = 0;
181}
182EXPORT_SYMBOL_GPL(snd_hdac_stream_reset);
183
184
185
186
187
188int snd_hdac_stream_setup(struct hdac_stream *azx_dev)
189{
190 struct hdac_bus *bus = azx_dev->bus;
191 struct snd_pcm_runtime *runtime;
192 unsigned int val;
193
194 if (azx_dev->substream)
195 runtime = azx_dev->substream->runtime;
196 else
197 runtime = NULL;
198
199 snd_hdac_stream_clear(azx_dev);
200
201 val = snd_hdac_stream_readl(azx_dev, SD_CTL);
202 val = (val & ~SD_CTL_STREAM_TAG_MASK) |
203 (azx_dev->stream_tag << SD_CTL_STREAM_TAG_SHIFT);
204 if (!bus->snoop)
205 val |= SD_CTL_TRAFFIC_PRIO;
206 snd_hdac_stream_writel(azx_dev, SD_CTL, val);
207
208
209 snd_hdac_stream_writel(azx_dev, SD_CBL, azx_dev->bufsize);
210
211
212
213 snd_hdac_stream_writew(azx_dev, SD_FORMAT, azx_dev->format_val);
214
215
216 snd_hdac_stream_writew(azx_dev, SD_LVI, azx_dev->frags - 1);
217
218
219
220 snd_hdac_stream_writel(azx_dev, SD_BDLPL, (u32)azx_dev->bdl.addr);
221
222 snd_hdac_stream_writel(azx_dev, SD_BDLPU,
223 upper_32_bits(azx_dev->bdl.addr));
224
225
226 if (bus->use_posbuf && bus->posbuf.addr) {
227 if (!(snd_hdac_chip_readl(bus, DPLBASE) & AZX_DPLBASE_ENABLE))
228 snd_hdac_chip_writel(bus, DPLBASE,
229 (u32)bus->posbuf.addr | AZX_DPLBASE_ENABLE);
230 }
231
232
233 snd_hdac_stream_updatel(azx_dev, SD_CTL, 0, SD_INT_MASK);
234
235 azx_dev->fifo_size = snd_hdac_stream_readw(azx_dev, SD_FIFOSIZE) + 1;
236
237
238
239
240
241 if (runtime && runtime->period_size > 64)
242 azx_dev->delay_negative_threshold =
243 -frames_to_bytes(runtime, 64);
244 else
245 azx_dev->delay_negative_threshold = 0;
246
247
248 if (runtime)
249 azx_dev->period_wallclk = (((runtime->period_size * 24000) /
250 runtime->rate) * 1000);
251
252 return 0;
253}
254EXPORT_SYMBOL_GPL(snd_hdac_stream_setup);
255
256
257
258
259
260void snd_hdac_stream_cleanup(struct hdac_stream *azx_dev)
261{
262 snd_hdac_stream_writel(azx_dev, SD_BDLPL, 0);
263 snd_hdac_stream_writel(azx_dev, SD_BDLPU, 0);
264 snd_hdac_stream_writel(azx_dev, SD_CTL, 0);
265 azx_dev->bufsize = 0;
266 azx_dev->period_bytes = 0;
267 azx_dev->format_val = 0;
268}
269EXPORT_SYMBOL_GPL(snd_hdac_stream_cleanup);
270
271
272
273
274
275
276
277
278
279
280
281
282struct hdac_stream *snd_hdac_stream_assign(struct hdac_bus *bus,
283 struct snd_pcm_substream *substream)
284{
285 struct hdac_stream *azx_dev;
286 struct hdac_stream *res = NULL;
287
288
289 int key = (substream->pcm->device << 16) | (substream->number << 2) |
290 (substream->stream + 1);
291
292 list_for_each_entry(azx_dev, &bus->stream_list, list) {
293 if (azx_dev->direction != substream->stream)
294 continue;
295 if (azx_dev->opened)
296 continue;
297 if (azx_dev->assigned_key == key) {
298 res = azx_dev;
299 break;
300 }
301 if (!res || bus->reverse_assign)
302 res = azx_dev;
303 }
304 if (res) {
305 spin_lock_irq(&bus->reg_lock);
306 res->opened = 1;
307 res->running = 0;
308 res->assigned_key = key;
309 res->substream = substream;
310 spin_unlock_irq(&bus->reg_lock);
311 }
312 return res;
313}
314EXPORT_SYMBOL_GPL(snd_hdac_stream_assign);
315
316
317
318
319
320
321
322void snd_hdac_stream_release(struct hdac_stream *azx_dev)
323{
324 struct hdac_bus *bus = azx_dev->bus;
325
326 spin_lock_irq(&bus->reg_lock);
327 azx_dev->opened = 0;
328 azx_dev->running = 0;
329 azx_dev->substream = NULL;
330 spin_unlock_irq(&bus->reg_lock);
331}
332EXPORT_SYMBOL_GPL(snd_hdac_stream_release);
333
334
335
336
337
338
339
340
341
342struct hdac_stream *snd_hdac_get_stream(struct hdac_bus *bus,
343 int dir, int stream_tag)
344{
345 struct hdac_stream *s;
346
347 list_for_each_entry(s, &bus->stream_list, list) {
348 if (s->direction == dir && s->stream_tag == stream_tag)
349 return s;
350 }
351
352 return NULL;
353}
354EXPORT_SYMBOL_GPL(snd_hdac_get_stream);
355
356
357
358
359static int setup_bdle(struct hdac_bus *bus,
360 struct snd_dma_buffer *dmab,
361 struct hdac_stream *azx_dev, __le32 **bdlp,
362 int ofs, int size, int with_ioc)
363{
364 __le32 *bdl = *bdlp;
365
366 while (size > 0) {
367 dma_addr_t addr;
368 int chunk;
369
370 if (azx_dev->frags >= AZX_MAX_BDL_ENTRIES)
371 return -EINVAL;
372
373 addr = snd_sgbuf_get_addr(dmab, ofs);
374
375 bdl[0] = cpu_to_le32((u32)addr);
376 bdl[1] = cpu_to_le32(upper_32_bits(addr));
377
378 chunk = snd_sgbuf_get_chunk_size(dmab, ofs, size);
379
380 if (bus->align_bdle_4k) {
381 u32 remain = 0x1000 - (ofs & 0xfff);
382
383 if (chunk > remain)
384 chunk = remain;
385 }
386 bdl[2] = cpu_to_le32(chunk);
387
388
389
390 size -= chunk;
391 bdl[3] = (size || !with_ioc) ? 0 : cpu_to_le32(0x01);
392 bdl += 4;
393 azx_dev->frags++;
394 ofs += chunk;
395 }
396 *bdlp = bdl;
397 return ofs;
398}
399
400
401
402
403
404
405
406
407int snd_hdac_stream_setup_periods(struct hdac_stream *azx_dev)
408{
409 struct hdac_bus *bus = azx_dev->bus;
410 struct snd_pcm_substream *substream = azx_dev->substream;
411 struct snd_pcm_runtime *runtime = substream->runtime;
412 __le32 *bdl;
413 int i, ofs, periods, period_bytes;
414 int pos_adj, pos_align;
415
416
417 snd_hdac_stream_writel(azx_dev, SD_BDLPL, 0);
418 snd_hdac_stream_writel(azx_dev, SD_BDLPU, 0);
419
420 period_bytes = azx_dev->period_bytes;
421 periods = azx_dev->bufsize / period_bytes;
422
423
424 bdl = (__le32 *)azx_dev->bdl.area;
425 ofs = 0;
426 azx_dev->frags = 0;
427
428 pos_adj = bus->bdl_pos_adj;
429 if (!azx_dev->no_period_wakeup && pos_adj > 0) {
430 pos_align = pos_adj;
431 pos_adj = (pos_adj * runtime->rate + 47999) / 48000;
432 if (!pos_adj)
433 pos_adj = pos_align;
434 else
435 pos_adj = ((pos_adj + pos_align - 1) / pos_align) *
436 pos_align;
437 pos_adj = frames_to_bytes(runtime, pos_adj);
438 if (pos_adj >= period_bytes) {
439 dev_warn(bus->dev, "Too big adjustment %d\n",
440 pos_adj);
441 pos_adj = 0;
442 } else {
443 ofs = setup_bdle(bus, snd_pcm_get_dma_buf(substream),
444 azx_dev,
445 &bdl, ofs, pos_adj, true);
446 if (ofs < 0)
447 goto error;
448 }
449 } else
450 pos_adj = 0;
451
452 for (i = 0; i < periods; i++) {
453 if (i == periods - 1 && pos_adj)
454 ofs = setup_bdle(bus, snd_pcm_get_dma_buf(substream),
455 azx_dev, &bdl, ofs,
456 period_bytes - pos_adj, 0);
457 else
458 ofs = setup_bdle(bus, snd_pcm_get_dma_buf(substream),
459 azx_dev, &bdl, ofs,
460 period_bytes,
461 !azx_dev->no_period_wakeup);
462 if (ofs < 0)
463 goto error;
464 }
465 return 0;
466
467 error:
468 dev_err(bus->dev, "Too many BDL entries: buffer=%d, period=%d\n",
469 azx_dev->bufsize, period_bytes);
470 return -EINVAL;
471}
472EXPORT_SYMBOL_GPL(snd_hdac_stream_setup_periods);
473
474
475
476
477
478
479
480
481
482int snd_hdac_stream_set_params(struct hdac_stream *azx_dev,
483 unsigned int format_val)
484{
485
486 unsigned int bufsize, period_bytes;
487 struct snd_pcm_substream *substream = azx_dev->substream;
488 struct snd_pcm_runtime *runtime;
489 int err;
490
491 if (!substream)
492 return -EINVAL;
493 runtime = substream->runtime;
494 bufsize = snd_pcm_lib_buffer_bytes(substream);
495 period_bytes = snd_pcm_lib_period_bytes(substream);
496
497 if (bufsize != azx_dev->bufsize ||
498 period_bytes != azx_dev->period_bytes ||
499 format_val != azx_dev->format_val ||
500 runtime->no_period_wakeup != azx_dev->no_period_wakeup) {
501 azx_dev->bufsize = bufsize;
502 azx_dev->period_bytes = period_bytes;
503 azx_dev->format_val = format_val;
504 azx_dev->no_period_wakeup = runtime->no_period_wakeup;
505 err = snd_hdac_stream_setup_periods(azx_dev);
506 if (err < 0)
507 return err;
508 }
509 return 0;
510}
511EXPORT_SYMBOL_GPL(snd_hdac_stream_set_params);
512
513static u64 azx_cc_read(const struct cyclecounter *cc)
514{
515 struct hdac_stream *azx_dev = container_of(cc, struct hdac_stream, cc);
516
517 return snd_hdac_chip_readl(azx_dev->bus, WALLCLK);
518}
519
520static void azx_timecounter_init(struct hdac_stream *azx_dev,
521 bool force, u64 last)
522{
523 struct timecounter *tc = &azx_dev->tc;
524 struct cyclecounter *cc = &azx_dev->cc;
525 u64 nsec;
526
527 cc->read = azx_cc_read;
528 cc->mask = CLOCKSOURCE_MASK(32);
529
530
531
532
533
534
535
536
537
538
539
540 cc->mult = 125;
541 cc->shift = 0;
542
543 nsec = 0;
544 timecounter_init(tc, cc, nsec);
545 if (force) {
546
547
548
549
550 tc->cycle_last = last;
551 }
552}
553
554
555
556
557
558
559
560
561
562
563
564void snd_hdac_stream_timecounter_init(struct hdac_stream *azx_dev,
565 unsigned int streams)
566{
567 struct hdac_bus *bus = azx_dev->bus;
568 struct snd_pcm_runtime *runtime = azx_dev->substream->runtime;
569 struct hdac_stream *s;
570 bool inited = false;
571 u64 cycle_last = 0;
572 int i = 0;
573
574 list_for_each_entry(s, &bus->stream_list, list) {
575 if (streams & (1 << i)) {
576 azx_timecounter_init(s, inited, cycle_last);
577 if (!inited) {
578 inited = true;
579 cycle_last = s->tc.cycle_last;
580 }
581 }
582 i++;
583 }
584
585 snd_pcm_gettime(runtime, &runtime->trigger_tstamp);
586 runtime->trigger_tstamp_latched = true;
587}
588EXPORT_SYMBOL_GPL(snd_hdac_stream_timecounter_init);
589
590
591
592
593
594
595
596
597void snd_hdac_stream_sync_trigger(struct hdac_stream *azx_dev, bool set,
598 unsigned int streams, unsigned int reg)
599{
600 struct hdac_bus *bus = azx_dev->bus;
601 unsigned int val;
602
603 if (!reg)
604 reg = AZX_REG_SSYNC;
605 val = _snd_hdac_chip_readl(bus, reg);
606 if (set)
607 val |= streams;
608 else
609 val &= ~streams;
610 _snd_hdac_chip_writel(bus, reg, val);
611}
612EXPORT_SYMBOL_GPL(snd_hdac_stream_sync_trigger);
613
614
615
616
617
618
619
620
621
622
623void snd_hdac_stream_sync(struct hdac_stream *azx_dev, bool start,
624 unsigned int streams)
625{
626 struct hdac_bus *bus = azx_dev->bus;
627 int i, nwait, timeout;
628 struct hdac_stream *s;
629
630 for (timeout = 5000; timeout; timeout--) {
631 nwait = 0;
632 i = 0;
633 list_for_each_entry(s, &bus->stream_list, list) {
634 if (!(streams & (1 << i++)))
635 continue;
636
637 if (start) {
638
639 if (!(snd_hdac_stream_readb(s, SD_STS) &
640 SD_STS_FIFO_READY))
641 nwait++;
642 } else {
643
644 if (snd_hdac_stream_readb(s, SD_CTL) &
645 SD_CTL_DMA_START) {
646 nwait++;
647
648
649
650
651 if (timeout == 1)
652 snd_hdac_stream_reset(s);
653 }
654 }
655 }
656 if (!nwait)
657 break;
658 cpu_relax();
659 }
660}
661EXPORT_SYMBOL_GPL(snd_hdac_stream_sync);
662
663#ifdef CONFIG_SND_HDA_DSP_LOADER
664
665
666
667
668
669
670
671
672
673
674int snd_hdac_dsp_prepare(struct hdac_stream *azx_dev, unsigned int format,
675 unsigned int byte_size, struct snd_dma_buffer *bufp)
676{
677 struct hdac_bus *bus = azx_dev->bus;
678 __le32 *bdl;
679 int err;
680
681 snd_hdac_dsp_lock(azx_dev);
682 spin_lock_irq(&bus->reg_lock);
683 if (azx_dev->running || azx_dev->locked) {
684 spin_unlock_irq(&bus->reg_lock);
685 err = -EBUSY;
686 goto unlock;
687 }
688 azx_dev->locked = true;
689 spin_unlock_irq(&bus->reg_lock);
690
691 err = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV_SG, bus->dev,
692 byte_size, bufp);
693 if (err < 0)
694 goto err_alloc;
695
696 azx_dev->substream = NULL;
697 azx_dev->bufsize = byte_size;
698 azx_dev->period_bytes = byte_size;
699 azx_dev->format_val = format;
700
701 snd_hdac_stream_reset(azx_dev);
702
703
704 snd_hdac_stream_writel(azx_dev, SD_BDLPL, 0);
705 snd_hdac_stream_writel(azx_dev, SD_BDLPU, 0);
706
707 azx_dev->frags = 0;
708 bdl = (__le32 *)azx_dev->bdl.area;
709 err = setup_bdle(bus, bufp, azx_dev, &bdl, 0, byte_size, 0);
710 if (err < 0)
711 goto error;
712
713 snd_hdac_stream_setup(azx_dev);
714 snd_hdac_dsp_unlock(azx_dev);
715 return azx_dev->stream_tag;
716
717 error:
718 snd_dma_free_pages(bufp);
719 err_alloc:
720 spin_lock_irq(&bus->reg_lock);
721 azx_dev->locked = false;
722 spin_unlock_irq(&bus->reg_lock);
723 unlock:
724 snd_hdac_dsp_unlock(azx_dev);
725 return err;
726}
727EXPORT_SYMBOL_GPL(snd_hdac_dsp_prepare);
728
729
730
731
732
733
734void snd_hdac_dsp_trigger(struct hdac_stream *azx_dev, bool start)
735{
736 if (start)
737 snd_hdac_stream_start(azx_dev, true);
738 else
739 snd_hdac_stream_stop(azx_dev);
740}
741EXPORT_SYMBOL_GPL(snd_hdac_dsp_trigger);
742
743
744
745
746
747
748void snd_hdac_dsp_cleanup(struct hdac_stream *azx_dev,
749 struct snd_dma_buffer *dmab)
750{
751 struct hdac_bus *bus = azx_dev->bus;
752
753 if (!dmab->area || !azx_dev->locked)
754 return;
755
756 snd_hdac_dsp_lock(azx_dev);
757
758 snd_hdac_stream_writel(azx_dev, SD_BDLPL, 0);
759 snd_hdac_stream_writel(azx_dev, SD_BDLPU, 0);
760 snd_hdac_stream_writel(azx_dev, SD_CTL, 0);
761 azx_dev->bufsize = 0;
762 azx_dev->period_bytes = 0;
763 azx_dev->format_val = 0;
764
765 snd_dma_free_pages(dmab);
766 dmab->area = NULL;
767
768 spin_lock_irq(&bus->reg_lock);
769 azx_dev->locked = false;
770 spin_unlock_irq(&bus->reg_lock);
771 snd_hdac_dsp_unlock(azx_dev);
772}
773EXPORT_SYMBOL_GPL(snd_hdac_dsp_cleanup);
774#endif
775