1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
3 // This file is provided under a dual BSD/GPLv2 license. When using or
4 // redistributing this file, you may do so under either license.
6 // Copyright(c) 2018 Intel Corporation. All rights reserved.
8 // Authors: Liam Girdwood <liam.r.girdwood@linux.intel.com>
9 // Ranjani Sridharan <ranjani.sridharan@linux.intel.com>
10 // Rander Wang <rander.wang@intel.com>
11 // Keyon Jie <yang.jie@linux.intel.com>
15 * Hardware interface for generic Intel audio DSP HDA IP
18 #include <linux/pm_runtime.h>
19 #include <sound/hdaudio_ext.h>
20 #include <sound/hda_register.h>
21 #include <sound/sof.h>
23 #include "../sof-audio.h"
26 #define HDA_LTRP_GB_VALUE_US 95
29 * set up one of BDL entries for a stream
31 static int hda_setup_bdle(struct snd_sof_dev
*sdev
,
32 struct snd_dma_buffer
*dmab
,
33 struct hdac_stream
*stream
,
34 struct sof_intel_dsp_bdl
**bdlp
,
35 int offset
, int size
, int ioc
)
37 struct hdac_bus
*bus
= sof_to_bus(sdev
);
38 struct sof_intel_dsp_bdl
*bdl
= *bdlp
;
44 if (stream
->frags
>= HDA_DSP_MAX_BDL_ENTRIES
) {
45 dev_err(sdev
->dev
, "error: stream frags exceeded\n");
49 addr
= snd_sgbuf_get_addr(dmab
, offset
);
50 /* program BDL addr */
51 bdl
->addr_l
= cpu_to_le32(lower_32_bits(addr
));
52 bdl
->addr_h
= cpu_to_le32(upper_32_bits(addr
));
53 /* program BDL size */
54 chunk
= snd_sgbuf_get_chunk_size(dmab
, offset
, size
);
55 /* one BDLE should not cross 4K boundary */
56 if (bus
->align_bdle_4k
) {
57 u32 remain
= 0x1000 - (offset
& 0xfff);
62 bdl
->size
= cpu_to_le32(chunk
);
63 /* only program IOC when the whole segment is processed */
65 bdl
->ioc
= (size
|| !ioc
) ? 0 : cpu_to_le32(0x01);
70 dev_vdbg(sdev
->dev
, "bdl, frags:%d, chunk size:0x%x;\n",
71 stream
->frags
, chunk
);
79 * set up Buffer Descriptor List (BDL) for host memory transfer
80 * BDL describes the location of the individual buffers and is little endian.
82 int hda_dsp_stream_setup_bdl(struct snd_sof_dev
*sdev
,
83 struct snd_dma_buffer
*dmab
,
84 struct hdac_stream
*stream
)
86 struct sof_intel_hda_dev
*hda
= sdev
->pdata
->hw_pdata
;
87 struct sof_intel_dsp_bdl
*bdl
;
88 int i
, offset
, period_bytes
, periods
;
91 period_bytes
= stream
->period_bytes
;
92 dev_dbg(sdev
->dev
, "period_bytes:0x%x\n", period_bytes
);
94 period_bytes
= stream
->bufsize
;
96 periods
= stream
->bufsize
/ period_bytes
;
98 dev_dbg(sdev
->dev
, "periods:%d\n", periods
);
100 remain
= stream
->bufsize
% period_bytes
;
104 /* program the initial BDL entries */
105 bdl
= (struct sof_intel_dsp_bdl
*)stream
->bdl
.area
;
110 * set IOC if don't use position IPC
111 * and period_wakeup needed.
113 ioc
= hda
->no_ipc_position
?
114 !stream
->no_period_wakeup
: 0;
116 for (i
= 0; i
< periods
; i
++) {
117 if (i
== (periods
- 1) && remain
)
118 /* set the last small entry */
119 offset
= hda_setup_bdle(sdev
, dmab
,
120 stream
, &bdl
, offset
,
123 offset
= hda_setup_bdle(sdev
, dmab
,
124 stream
, &bdl
, offset
,
131 int hda_dsp_stream_spib_config(struct snd_sof_dev
*sdev
,
132 struct hdac_ext_stream
*stream
,
133 int enable
, u32 size
)
135 struct hdac_stream
*hstream
= &stream
->hstream
;
138 if (!sdev
->bar
[HDA_DSP_SPIB_BAR
]) {
139 dev_err(sdev
->dev
, "error: address of spib capability is NULL\n");
143 mask
= (1 << hstream
->index
);
145 /* enable/disable SPIB for the stream */
146 snd_sof_dsp_update_bits(sdev
, HDA_DSP_SPIB_BAR
,
147 SOF_HDA_ADSP_REG_CL_SPBFIFO_SPBFCCTL
, mask
,
148 enable
<< hstream
->index
);
150 /* set the SPIB value */
151 sof_io_write(sdev
, stream
->spib_addr
, size
);
156 /* get next unused stream */
157 struct hdac_ext_stream
*
158 hda_dsp_stream_get(struct snd_sof_dev
*sdev
, int direction
)
160 struct hdac_bus
*bus
= sof_to_bus(sdev
);
161 struct sof_intel_hda_stream
*hda_stream
;
162 struct hdac_ext_stream
*stream
= NULL
;
163 struct hdac_stream
*s
;
165 spin_lock_irq(&bus
->reg_lock
);
167 /* get an unused stream */
168 list_for_each_entry(s
, &bus
->stream_list
, list
) {
169 if (s
->direction
== direction
&& !s
->opened
) {
170 stream
= stream_to_hdac_ext_stream(s
);
171 hda_stream
= container_of(stream
,
172 struct sof_intel_hda_stream
,
174 /* check if the host DMA channel is reserved */
175 if (hda_stream
->host_reserved
)
183 spin_unlock_irq(&bus
->reg_lock
);
187 dev_err(sdev
->dev
, "error: no free %s streams\n",
188 direction
== SNDRV_PCM_STREAM_PLAYBACK
?
189 "playback" : "capture");
192 * Disable DMI Link L1 entry when capture stream is opened.
193 * Workaround to address a known issue with host DMA that results
194 * in xruns during pause/release in capture scenarios.
196 if (!IS_ENABLED(CONFIG_SND_SOC_SOF_HDA_ALWAYS_ENABLE_DMI_L1
))
197 if (stream
&& direction
== SNDRV_PCM_STREAM_CAPTURE
)
198 snd_sof_dsp_update_bits(sdev
, HDA_DSP_HDA_BAR
,
200 HDA_VS_INTEL_EM2_L1SEN
, 0);
206 int hda_dsp_stream_put(struct snd_sof_dev
*sdev
, int direction
, int stream_tag
)
208 struct hdac_bus
*bus
= sof_to_bus(sdev
);
209 struct hdac_stream
*s
;
210 bool active_capture_stream
= false;
213 spin_lock_irq(&bus
->reg_lock
);
216 * close stream matching the stream tag
217 * and check if there are any open capture streams.
219 list_for_each_entry(s
, &bus
->stream_list
, list
) {
223 if (s
->direction
== direction
&& s
->stream_tag
== stream_tag
) {
226 } else if (s
->direction
== SNDRV_PCM_STREAM_CAPTURE
) {
227 active_capture_stream
= true;
231 spin_unlock_irq(&bus
->reg_lock
);
233 /* Enable DMI L1 entry if there are no capture streams open */
234 if (!IS_ENABLED(CONFIG_SND_SOC_SOF_HDA_ALWAYS_ENABLE_DMI_L1
))
235 if (!active_capture_stream
)
236 snd_sof_dsp_update_bits(sdev
, HDA_DSP_HDA_BAR
,
238 HDA_VS_INTEL_EM2_L1SEN
,
239 HDA_VS_INTEL_EM2_L1SEN
);
242 dev_dbg(sdev
->dev
, "stream_tag %d not opened!\n", stream_tag
);
249 int hda_dsp_stream_trigger(struct snd_sof_dev
*sdev
,
250 struct hdac_ext_stream
*stream
, int cmd
)
252 struct hdac_stream
*hstream
= &stream
->hstream
;
253 int sd_offset
= SOF_STREAM_SD_OFFSET(hstream
);
254 u32 dma_start
= SOF_HDA_SD_CTL_DMA_START
;
258 /* cmd must be for audio stream */
260 case SNDRV_PCM_TRIGGER_RESUME
:
261 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
262 case SNDRV_PCM_TRIGGER_START
:
263 snd_sof_dsp_update_bits(sdev
, HDA_DSP_HDA_BAR
, SOF_HDA_INTCTL
,
265 1 << hstream
->index
);
267 snd_sof_dsp_update_bits(sdev
, HDA_DSP_HDA_BAR
,
269 SOF_HDA_SD_CTL_DMA_START
|
270 SOF_HDA_CL_DMA_SD_INT_MASK
,
271 SOF_HDA_SD_CTL_DMA_START
|
272 SOF_HDA_CL_DMA_SD_INT_MASK
);
274 ret
= snd_sof_dsp_read_poll_timeout(sdev
,
277 ((run
& dma_start
) == dma_start
),
278 HDA_DSP_REG_POLL_INTERVAL_US
,
279 HDA_DSP_STREAM_RUN_TIMEOUT
);
283 "error: %s: cmd %d: timeout on STREAM_SD_OFFSET read\n",
288 hstream
->running
= true;
290 case SNDRV_PCM_TRIGGER_SUSPEND
:
291 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
292 case SNDRV_PCM_TRIGGER_STOP
:
293 snd_sof_dsp_update_bits(sdev
, HDA_DSP_HDA_BAR
,
295 SOF_HDA_SD_CTL_DMA_START
|
296 SOF_HDA_CL_DMA_SD_INT_MASK
, 0x0);
298 ret
= snd_sof_dsp_read_poll_timeout(sdev
, HDA_DSP_HDA_BAR
,
301 HDA_DSP_REG_POLL_INTERVAL_US
,
302 HDA_DSP_STREAM_RUN_TIMEOUT
);
306 "error: %s: cmd %d: timeout on STREAM_SD_OFFSET read\n",
311 snd_sof_dsp_write(sdev
, HDA_DSP_HDA_BAR
, sd_offset
+
312 SOF_HDA_ADSP_REG_CL_SD_STS
,
313 SOF_HDA_CL_DMA_SD_INT_MASK
);
315 hstream
->running
= false;
316 snd_sof_dsp_update_bits(sdev
, HDA_DSP_HDA_BAR
, SOF_HDA_INTCTL
,
317 1 << hstream
->index
, 0x0);
320 dev_err(sdev
->dev
, "error: unknown command: %d\n", cmd
);
327 /* minimal recommended programming for ICCMAX stream */
328 int hda_dsp_iccmax_stream_hw_params(struct snd_sof_dev
*sdev
, struct hdac_ext_stream
*stream
,
329 struct snd_dma_buffer
*dmab
,
330 struct snd_pcm_hw_params
*params
)
332 struct hdac_bus
*bus
= sof_to_bus(sdev
);
333 struct hdac_stream
*hstream
= &stream
->hstream
;
334 int sd_offset
= SOF_STREAM_SD_OFFSET(hstream
);
336 u32 mask
= 0x1 << hstream
->index
;
339 dev_err(sdev
->dev
, "error: no stream available\n");
344 *hstream
->posbuf
= 0;
346 /* reset BDL address */
347 snd_sof_dsp_write(sdev
, HDA_DSP_HDA_BAR
,
348 sd_offset
+ SOF_HDA_ADSP_REG_CL_SD_BDLPL
,
350 snd_sof_dsp_write(sdev
, HDA_DSP_HDA_BAR
,
351 sd_offset
+ SOF_HDA_ADSP_REG_CL_SD_BDLPU
,
356 ret
= hda_dsp_stream_setup_bdl(sdev
, dmab
, hstream
);
358 dev_err(sdev
->dev
, "error: set up of BDL failed\n");
362 /* program BDL address */
363 snd_sof_dsp_write(sdev
, HDA_DSP_HDA_BAR
,
364 sd_offset
+ SOF_HDA_ADSP_REG_CL_SD_BDLPL
,
365 (u32
)hstream
->bdl
.addr
);
366 snd_sof_dsp_write(sdev
, HDA_DSP_HDA_BAR
,
367 sd_offset
+ SOF_HDA_ADSP_REG_CL_SD_BDLPU
,
368 upper_32_bits(hstream
->bdl
.addr
));
370 /* program cyclic buffer length */
371 snd_sof_dsp_write(sdev
, HDA_DSP_HDA_BAR
,
372 sd_offset
+ SOF_HDA_ADSP_REG_CL_SD_CBL
,
375 /* program last valid index */
376 snd_sof_dsp_update_bits(sdev
, HDA_DSP_HDA_BAR
,
377 sd_offset
+ SOF_HDA_ADSP_REG_CL_SD_LVI
,
378 0xffff, (hstream
->frags
- 1));
380 /* decouple host and link DMA, enable DSP features */
381 snd_sof_dsp_update_bits(sdev
, HDA_DSP_PP_BAR
, SOF_HDA_REG_PP_PPCTL
,
384 /* Follow HW recommendation to set the guardband value to 95us during FW boot */
385 snd_hdac_chip_updateb(bus
, VS_LTRP
, HDA_VS_INTEL_LTRP_GB_MASK
, HDA_LTRP_GB_VALUE_US
);
388 snd_sof_dsp_update_bits(sdev
, HDA_DSP_HDA_BAR
, sd_offset
,
389 SOF_HDA_SD_CTL_DMA_START
, SOF_HDA_SD_CTL_DMA_START
);
395 * prepare for common hdac registers settings, for both code loader
398 int hda_dsp_stream_hw_params(struct snd_sof_dev
*sdev
,
399 struct hdac_ext_stream
*stream
,
400 struct snd_dma_buffer
*dmab
,
401 struct snd_pcm_hw_params
*params
)
403 struct hdac_bus
*bus
= sof_to_bus(sdev
);
404 struct hdac_stream
*hstream
= &stream
->hstream
;
405 int sd_offset
= SOF_STREAM_SD_OFFSET(hstream
);
406 int ret
, timeout
= HDA_DSP_STREAM_RESET_TIMEOUT
;
407 u32 dma_start
= SOF_HDA_SD_CTL_DMA_START
;
412 dev_err(sdev
->dev
, "error: no stream available\n");
416 /* decouple host and link DMA */
417 mask
= 0x1 << hstream
->index
;
418 snd_sof_dsp_update_bits(sdev
, HDA_DSP_PP_BAR
, SOF_HDA_REG_PP_PPCTL
,
422 dev_err(sdev
->dev
, "error: no dma buffer allocated!\n");
426 /* clear stream status */
427 snd_sof_dsp_update_bits(sdev
, HDA_DSP_HDA_BAR
, sd_offset
,
428 SOF_HDA_CL_DMA_SD_INT_MASK
|
429 SOF_HDA_SD_CTL_DMA_START
, 0);
431 ret
= snd_sof_dsp_read_poll_timeout(sdev
, HDA_DSP_HDA_BAR
,
434 HDA_DSP_REG_POLL_INTERVAL_US
,
435 HDA_DSP_STREAM_RUN_TIMEOUT
);
439 "error: %s: timeout on STREAM_SD_OFFSET read1\n",
444 snd_sof_dsp_update_bits(sdev
, HDA_DSP_HDA_BAR
,
445 sd_offset
+ SOF_HDA_ADSP_REG_CL_SD_STS
,
446 SOF_HDA_CL_DMA_SD_INT_MASK
,
447 SOF_HDA_CL_DMA_SD_INT_MASK
);
450 snd_sof_dsp_update_bits(sdev
, HDA_DSP_HDA_BAR
, sd_offset
, 0x1,
454 val
= snd_sof_dsp_read(sdev
, HDA_DSP_HDA_BAR
,
460 dev_err(sdev
->dev
, "error: stream reset failed\n");
464 timeout
= HDA_DSP_STREAM_RESET_TIMEOUT
;
465 snd_sof_dsp_update_bits(sdev
, HDA_DSP_HDA_BAR
, sd_offset
, 0x1,
468 /* wait for hardware to report that stream is out of reset */
471 val
= snd_sof_dsp_read(sdev
, HDA_DSP_HDA_BAR
,
473 if ((val
& 0x1) == 0)
477 dev_err(sdev
->dev
, "error: timeout waiting for stream reset\n");
482 *hstream
->posbuf
= 0;
484 /* reset BDL address */
485 snd_sof_dsp_write(sdev
, HDA_DSP_HDA_BAR
,
486 sd_offset
+ SOF_HDA_ADSP_REG_CL_SD_BDLPL
,
488 snd_sof_dsp_write(sdev
, HDA_DSP_HDA_BAR
,
489 sd_offset
+ SOF_HDA_ADSP_REG_CL_SD_BDLPU
,
492 /* clear stream status */
493 snd_sof_dsp_update_bits(sdev
, HDA_DSP_HDA_BAR
, sd_offset
,
494 SOF_HDA_CL_DMA_SD_INT_MASK
|
495 SOF_HDA_SD_CTL_DMA_START
, 0);
497 ret
= snd_sof_dsp_read_poll_timeout(sdev
, HDA_DSP_HDA_BAR
,
500 HDA_DSP_REG_POLL_INTERVAL_US
,
501 HDA_DSP_STREAM_RUN_TIMEOUT
);
505 "error: %s: timeout on STREAM_SD_OFFSET read2\n",
510 snd_sof_dsp_update_bits(sdev
, HDA_DSP_HDA_BAR
,
511 sd_offset
+ SOF_HDA_ADSP_REG_CL_SD_STS
,
512 SOF_HDA_CL_DMA_SD_INT_MASK
,
513 SOF_HDA_CL_DMA_SD_INT_MASK
);
517 ret
= hda_dsp_stream_setup_bdl(sdev
, dmab
, hstream
);
519 dev_err(sdev
->dev
, "error: set up of BDL failed\n");
523 /* program stream tag to set up stream descriptor for DMA */
524 snd_sof_dsp_update_bits(sdev
, HDA_DSP_HDA_BAR
, sd_offset
,
525 SOF_HDA_CL_SD_CTL_STREAM_TAG_MASK
,
526 hstream
->stream_tag
<<
527 SOF_HDA_CL_SD_CTL_STREAM_TAG_SHIFT
);
529 /* program cyclic buffer length */
530 snd_sof_dsp_write(sdev
, HDA_DSP_HDA_BAR
,
531 sd_offset
+ SOF_HDA_ADSP_REG_CL_SD_CBL
,
535 * Recommended hardware programming sequence for HDAudio DMA format
537 * 1. Put DMA into coupled mode by clearing PPCTL.PROCEN bit
538 * for corresponding stream index before the time of writing
539 * format to SDxFMT register.
541 * 3. Set PPCTL.PROCEN bit for corresponding stream index to
542 * enable decoupled mode
545 /* couple host and link DMA, disable DSP features */
546 snd_sof_dsp_update_bits(sdev
, HDA_DSP_PP_BAR
, SOF_HDA_REG_PP_PPCTL
,
549 /* program stream format */
550 snd_sof_dsp_update_bits(sdev
, HDA_DSP_HDA_BAR
,
552 SOF_HDA_ADSP_REG_CL_SD_FORMAT
,
553 0xffff, hstream
->format_val
);
555 /* decouple host and link DMA, enable DSP features */
556 snd_sof_dsp_update_bits(sdev
, HDA_DSP_PP_BAR
, SOF_HDA_REG_PP_PPCTL
,
559 /* program last valid index */
560 snd_sof_dsp_update_bits(sdev
, HDA_DSP_HDA_BAR
,
561 sd_offset
+ SOF_HDA_ADSP_REG_CL_SD_LVI
,
562 0xffff, (hstream
->frags
- 1));
564 /* program BDL address */
565 snd_sof_dsp_write(sdev
, HDA_DSP_HDA_BAR
,
566 sd_offset
+ SOF_HDA_ADSP_REG_CL_SD_BDLPL
,
567 (u32
)hstream
->bdl
.addr
);
568 snd_sof_dsp_write(sdev
, HDA_DSP_HDA_BAR
,
569 sd_offset
+ SOF_HDA_ADSP_REG_CL_SD_BDLPU
,
570 upper_32_bits(hstream
->bdl
.addr
));
572 /* enable position buffer */
573 if (!(snd_sof_dsp_read(sdev
, HDA_DSP_HDA_BAR
, SOF_HDA_ADSP_DPLBASE
)
574 & SOF_HDA_ADSP_DPLBASE_ENABLE
)) {
575 snd_sof_dsp_write(sdev
, HDA_DSP_HDA_BAR
, SOF_HDA_ADSP_DPUBASE
,
576 upper_32_bits(bus
->posbuf
.addr
));
577 snd_sof_dsp_write(sdev
, HDA_DSP_HDA_BAR
, SOF_HDA_ADSP_DPLBASE
,
578 (u32
)bus
->posbuf
.addr
|
579 SOF_HDA_ADSP_DPLBASE_ENABLE
);
582 /* set interrupt enable bits */
583 snd_sof_dsp_update_bits(sdev
, HDA_DSP_HDA_BAR
, sd_offset
,
584 SOF_HDA_CL_DMA_SD_INT_MASK
,
585 SOF_HDA_CL_DMA_SD_INT_MASK
);
588 if (hstream
->direction
== SNDRV_PCM_STREAM_PLAYBACK
) {
590 snd_sof_dsp_read(sdev
, HDA_DSP_HDA_BAR
,
592 SOF_HDA_ADSP_REG_CL_SD_FIFOSIZE
);
593 hstream
->fifo_size
&= 0xffff;
594 hstream
->fifo_size
+= 1;
596 hstream
->fifo_size
= 0;
602 int hda_dsp_stream_hw_free(struct snd_sof_dev
*sdev
,
603 struct snd_pcm_substream
*substream
)
605 struct hdac_stream
*stream
= substream
->runtime
->private_data
;
606 struct hdac_ext_stream
*link_dev
= container_of(stream
,
607 struct hdac_ext_stream
,
609 struct hdac_bus
*bus
= sof_to_bus(sdev
);
610 u32 mask
= 0x1 << stream
->index
;
612 spin_lock_irq(&bus
->reg_lock
);
613 /* couple host and link DMA if link DMA channel is idle */
614 if (!link_dev
->link_locked
)
615 snd_sof_dsp_update_bits(sdev
, HDA_DSP_PP_BAR
,
616 SOF_HDA_REG_PP_PPCTL
, mask
, 0);
617 spin_unlock_irq(&bus
->reg_lock
);
619 stream
->substream
= NULL
;
624 bool hda_dsp_check_stream_irq(struct snd_sof_dev
*sdev
)
626 struct hdac_bus
*bus
= sof_to_bus(sdev
);
630 /* The function can be called at irq thread, so use spin_lock_irq */
631 spin_lock_irq(&bus
->reg_lock
);
633 status
= snd_hdac_chip_readl(bus
, INTSTS
);
634 dev_vdbg(bus
->dev
, "stream irq, INTSTS status: 0x%x\n", status
);
636 /* if Register inaccessible, ignore it.*/
637 if (status
!= 0xffffffff)
640 spin_unlock_irq(&bus
->reg_lock
);
646 hda_dsp_set_bytes_transferred(struct hdac_stream
*hstream
, u64 buffer_size
)
648 u64 prev_pos
, pos
, num_bytes
;
650 div64_u64_rem(hstream
->curr_pos
, buffer_size
, &prev_pos
);
651 pos
= snd_hdac_stream_get_pos_posbuf(hstream
);
654 num_bytes
= (buffer_size
- prev_pos
) + pos
;
656 num_bytes
= pos
- prev_pos
;
658 hstream
->curr_pos
+= num_bytes
;
661 static bool hda_dsp_stream_check(struct hdac_bus
*bus
, u32 status
)
663 struct sof_intel_hda_dev
*sof_hda
= bus_to_sof_hda(bus
);
664 struct hdac_stream
*s
;
668 list_for_each_entry(s
, &bus
->stream_list
, list
) {
669 if (status
& BIT(s
->index
) && s
->opened
) {
670 sd_status
= snd_hdac_stream_readb(s
, SD_STS
);
672 dev_vdbg(bus
->dev
, "stream %d status 0x%x\n",
673 s
->index
, sd_status
);
675 snd_hdac_stream_writeb(s
, SD_STS
, sd_status
);
678 if ((!s
->substream
&& !s
->cstream
) ||
680 (sd_status
& SOF_HDA_CL_DMA_SD_INT_COMPLETE
) == 0)
683 /* Inform ALSA only in case not do that with IPC */
684 if (s
->substream
&& sof_hda
->no_ipc_position
) {
685 snd_sof_pcm_period_elapsed(s
->substream
);
686 } else if (s
->cstream
) {
687 hda_dsp_set_bytes_transferred(s
,
688 s
->cstream
->runtime
->buffer_size
);
689 snd_compr_fragment_elapsed(s
->cstream
);
697 irqreturn_t
hda_dsp_stream_threaded_handler(int irq
, void *context
)
699 struct snd_sof_dev
*sdev
= context
;
700 struct hdac_bus
*bus
= sof_to_bus(sdev
);
701 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
709 * Loop 10 times to handle missed interrupts caused by
710 * unsolicited responses from the codec
712 for (i
= 0, active
= true; i
< 10 && active
; i
++) {
713 spin_lock_irq(&bus
->reg_lock
);
715 status
= snd_hdac_chip_readl(bus
, INTSTS
);
718 active
= hda_dsp_stream_check(bus
, status
);
720 /* check and clear RIRB interrupt */
721 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
722 if (status
& AZX_INT_CTRL_EN
) {
723 rirb_status
= snd_hdac_chip_readb(bus
, RIRBSTS
);
724 if (rirb_status
& RIRB_INT_MASK
) {
726 * Clearing the interrupt status here ensures
727 * that no interrupt gets masked after the RIRB
728 * wp is read in snd_hdac_bus_update_rirb.
730 snd_hdac_chip_writeb(bus
, RIRBSTS
,
733 if (rirb_status
& RIRB_INT_RESPONSE
)
734 snd_hdac_bus_update_rirb(bus
);
738 spin_unlock_irq(&bus
->reg_lock
);
744 int hda_dsp_stream_init(struct snd_sof_dev
*sdev
)
746 struct hdac_bus
*bus
= sof_to_bus(sdev
);
747 struct hdac_ext_stream
*stream
;
748 struct hdac_stream
*hstream
;
749 struct pci_dev
*pci
= to_pci_dev(sdev
->dev
);
750 struct sof_intel_hda_dev
*sof_hda
= bus_to_sof_hda(bus
);
752 int i
, num_playback
, num_capture
, num_total
, ret
;
755 gcap
= snd_sof_dsp_read(sdev
, HDA_DSP_HDA_BAR
, SOF_HDA_GCAP
);
756 dev_dbg(sdev
->dev
, "hda global caps = 0x%x\n", gcap
);
758 /* get stream count from GCAP */
759 num_capture
= (gcap
>> 8) & 0x0f;
760 num_playback
= (gcap
>> 12) & 0x0f;
761 num_total
= num_playback
+ num_capture
;
763 dev_dbg(sdev
->dev
, "detected %d playback and %d capture streams\n",
764 num_playback
, num_capture
);
766 if (num_playback
>= SOF_HDA_PLAYBACK_STREAMS
) {
767 dev_err(sdev
->dev
, "error: too many playback streams %d\n",
772 if (num_capture
>= SOF_HDA_CAPTURE_STREAMS
) {
773 dev_err(sdev
->dev
, "error: too many capture streams %d\n",
779 * mem alloc for the position buffer
780 * TODO: check position buffer update
782 ret
= snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV
, &pci
->dev
,
783 SOF_HDA_DPIB_ENTRY_SIZE
* num_total
,
786 dev_err(sdev
->dev
, "error: posbuffer dma alloc failed\n");
790 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
791 /* mem alloc for the CORB/RIRB ringbuffers */
792 ret
= snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV
, &pci
->dev
,
793 PAGE_SIZE
, &bus
->rb
);
795 dev_err(sdev
->dev
, "error: RB alloc failed\n");
800 /* create capture streams */
801 for (i
= 0; i
< num_capture
; i
++) {
802 struct sof_intel_hda_stream
*hda_stream
;
804 hda_stream
= devm_kzalloc(sdev
->dev
, sizeof(*hda_stream
),
809 hda_stream
->sdev
= sdev
;
811 stream
= &hda_stream
->hda_stream
;
813 stream
->pphc_addr
= sdev
->bar
[HDA_DSP_PP_BAR
] +
814 SOF_HDA_PPHC_BASE
+ SOF_HDA_PPHC_INTERVAL
* i
;
816 stream
->pplc_addr
= sdev
->bar
[HDA_DSP_PP_BAR
] +
817 SOF_HDA_PPLC_BASE
+ SOF_HDA_PPLC_MULTI
* num_total
+
818 SOF_HDA_PPLC_INTERVAL
* i
;
820 /* do we support SPIB */
821 if (sdev
->bar
[HDA_DSP_SPIB_BAR
]) {
822 stream
->spib_addr
= sdev
->bar
[HDA_DSP_SPIB_BAR
] +
823 SOF_HDA_SPIB_BASE
+ SOF_HDA_SPIB_INTERVAL
* i
+
826 stream
->fifo_addr
= sdev
->bar
[HDA_DSP_SPIB_BAR
] +
827 SOF_HDA_SPIB_BASE
+ SOF_HDA_SPIB_INTERVAL
* i
+
828 SOF_HDA_SPIB_MAXFIFO
;
831 hstream
= &stream
->hstream
;
833 hstream
->sd_int_sta_mask
= 1 << i
;
835 sd_offset
= SOF_STREAM_SD_OFFSET(hstream
);
836 hstream
->sd_addr
= sdev
->bar
[HDA_DSP_HDA_BAR
] + sd_offset
;
837 hstream
->stream_tag
= i
+ 1;
838 hstream
->opened
= false;
839 hstream
->running
= false;
840 hstream
->direction
= SNDRV_PCM_STREAM_CAPTURE
;
842 /* memory alloc for stream BDL */
843 ret
= snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV
, &pci
->dev
,
844 HDA_DSP_BDL_SIZE
, &hstream
->bdl
);
846 dev_err(sdev
->dev
, "error: stream bdl dma alloc failed\n");
849 hstream
->posbuf
= (__le32
*)(bus
->posbuf
.area
+
850 (hstream
->index
) * 8);
852 list_add_tail(&hstream
->list
, &bus
->stream_list
);
855 /* create playback streams */
856 for (i
= num_capture
; i
< num_total
; i
++) {
857 struct sof_intel_hda_stream
*hda_stream
;
859 hda_stream
= devm_kzalloc(sdev
->dev
, sizeof(*hda_stream
),
864 hda_stream
->sdev
= sdev
;
866 stream
= &hda_stream
->hda_stream
;
868 /* we always have DSP support */
869 stream
->pphc_addr
= sdev
->bar
[HDA_DSP_PP_BAR
] +
870 SOF_HDA_PPHC_BASE
+ SOF_HDA_PPHC_INTERVAL
* i
;
872 stream
->pplc_addr
= sdev
->bar
[HDA_DSP_PP_BAR
] +
873 SOF_HDA_PPLC_BASE
+ SOF_HDA_PPLC_MULTI
* num_total
+
874 SOF_HDA_PPLC_INTERVAL
* i
;
876 /* do we support SPIB */
877 if (sdev
->bar
[HDA_DSP_SPIB_BAR
]) {
878 stream
->spib_addr
= sdev
->bar
[HDA_DSP_SPIB_BAR
] +
879 SOF_HDA_SPIB_BASE
+ SOF_HDA_SPIB_INTERVAL
* i
+
882 stream
->fifo_addr
= sdev
->bar
[HDA_DSP_SPIB_BAR
] +
883 SOF_HDA_SPIB_BASE
+ SOF_HDA_SPIB_INTERVAL
* i
+
884 SOF_HDA_SPIB_MAXFIFO
;
887 hstream
= &stream
->hstream
;
889 hstream
->sd_int_sta_mask
= 1 << i
;
891 sd_offset
= SOF_STREAM_SD_OFFSET(hstream
);
892 hstream
->sd_addr
= sdev
->bar
[HDA_DSP_HDA_BAR
] + sd_offset
;
893 hstream
->stream_tag
= i
- num_capture
+ 1;
894 hstream
->opened
= false;
895 hstream
->running
= false;
896 hstream
->direction
= SNDRV_PCM_STREAM_PLAYBACK
;
898 /* mem alloc for stream BDL */
899 ret
= snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV
, &pci
->dev
,
900 HDA_DSP_BDL_SIZE
, &hstream
->bdl
);
902 dev_err(sdev
->dev
, "error: stream bdl dma alloc failed\n");
906 hstream
->posbuf
= (__le32
*)(bus
->posbuf
.area
+
907 (hstream
->index
) * 8);
909 list_add_tail(&hstream
->list
, &bus
->stream_list
);
912 /* store total stream count (playback + capture) from GCAP */
913 sof_hda
->stream_max
= num_total
;
918 void hda_dsp_stream_free(struct snd_sof_dev
*sdev
)
920 struct hdac_bus
*bus
= sof_to_bus(sdev
);
921 struct hdac_stream
*s
, *_s
;
922 struct hdac_ext_stream
*stream
;
923 struct sof_intel_hda_stream
*hda_stream
;
925 /* free position buffer */
926 if (bus
->posbuf
.area
)
927 snd_dma_free_pages(&bus
->posbuf
);
929 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
930 /* free position buffer */
932 snd_dma_free_pages(&bus
->rb
);
935 list_for_each_entry_safe(s
, _s
, &bus
->stream_list
, list
) {
938 /* free bdl buffer */
940 snd_dma_free_pages(&s
->bdl
);
942 stream
= stream_to_hdac_ext_stream(s
);
943 hda_stream
= container_of(stream
, struct sof_intel_hda_stream
,
945 devm_kfree(sdev
->dev
, hda_stream
);