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, u32 flags)
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");
193 hda_stream->flags = flags;
196 * Prevent DMI Link L1 entry for streams that don't support it.
197 * Workaround to address a known issue with host DMA that results
198 * in xruns during pause/release in capture scenarios.
200 if (!(flags & SOF_HDA_STREAM_DMI_L1_COMPATIBLE))
201 snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR,
203 HDA_VS_INTEL_EM2_L1SEN, 0);
209 int hda_dsp_stream_put(struct snd_sof_dev *sdev, int direction, int stream_tag)
211 struct hdac_bus *bus = sof_to_bus(sdev);
212 struct sof_intel_hda_stream *hda_stream;
213 struct hdac_ext_stream *stream;
214 struct hdac_stream *s;
215 bool dmi_l1_enable = true;
218 spin_lock_irq(&bus->reg_lock);
221 * close stream matching the stream tag and check if there are any open streams
222 * that are DMI L1 incompatible.
224 list_for_each_entry(s, &bus->stream_list, list) {
225 stream = stream_to_hdac_ext_stream(s);
226 hda_stream = container_of(stream, struct sof_intel_hda_stream, hda_stream);
231 if (s->direction == direction && s->stream_tag == stream_tag) {
234 } else if (!(hda_stream->flags & SOF_HDA_STREAM_DMI_L1_COMPATIBLE)) {
235 dmi_l1_enable = false;
239 spin_unlock_irq(&bus->reg_lock);
241 /* Enable DMI L1 if permitted */
243 snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR, HDA_VS_INTEL_EM2,
244 HDA_VS_INTEL_EM2_L1SEN, HDA_VS_INTEL_EM2_L1SEN);
247 dev_dbg(sdev->dev, "stream_tag %d not opened!\n", stream_tag);
254 int hda_dsp_stream_trigger(struct snd_sof_dev *sdev,
255 struct hdac_ext_stream *stream, int cmd)
257 struct hdac_stream *hstream = &stream->hstream;
258 int sd_offset = SOF_STREAM_SD_OFFSET(hstream);
259 u32 dma_start = SOF_HDA_SD_CTL_DMA_START;
263 /* cmd must be for audio stream */
265 case SNDRV_PCM_TRIGGER_RESUME:
266 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
267 case SNDRV_PCM_TRIGGER_START:
268 snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR, SOF_HDA_INTCTL,
270 1 << hstream->index);
272 snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR,
274 SOF_HDA_SD_CTL_DMA_START |
275 SOF_HDA_CL_DMA_SD_INT_MASK,
276 SOF_HDA_SD_CTL_DMA_START |
277 SOF_HDA_CL_DMA_SD_INT_MASK);
279 ret = snd_sof_dsp_read_poll_timeout(sdev,
282 ((run & dma_start) == dma_start),
283 HDA_DSP_REG_POLL_INTERVAL_US,
284 HDA_DSP_STREAM_RUN_TIMEOUT);
288 "error: %s: cmd %d: timeout on STREAM_SD_OFFSET read\n",
293 hstream->running = true;
295 case SNDRV_PCM_TRIGGER_SUSPEND:
296 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
297 case SNDRV_PCM_TRIGGER_STOP:
298 snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR,
300 SOF_HDA_SD_CTL_DMA_START |
301 SOF_HDA_CL_DMA_SD_INT_MASK, 0x0);
303 ret = snd_sof_dsp_read_poll_timeout(sdev, HDA_DSP_HDA_BAR,
306 HDA_DSP_REG_POLL_INTERVAL_US,
307 HDA_DSP_STREAM_RUN_TIMEOUT);
311 "error: %s: cmd %d: timeout on STREAM_SD_OFFSET read\n",
316 snd_sof_dsp_write(sdev, HDA_DSP_HDA_BAR, sd_offset +
317 SOF_HDA_ADSP_REG_CL_SD_STS,
318 SOF_HDA_CL_DMA_SD_INT_MASK);
320 hstream->running = false;
321 snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR, SOF_HDA_INTCTL,
322 1 << hstream->index, 0x0);
325 dev_err(sdev->dev, "error: unknown command: %d\n", cmd);
332 /* minimal recommended programming for ICCMAX stream */
333 int hda_dsp_iccmax_stream_hw_params(struct snd_sof_dev *sdev, struct hdac_ext_stream *stream,
334 struct snd_dma_buffer *dmab,
335 struct snd_pcm_hw_params *params)
337 struct hdac_bus *bus = sof_to_bus(sdev);
338 struct hdac_stream *hstream = &stream->hstream;
339 int sd_offset = SOF_STREAM_SD_OFFSET(hstream);
341 u32 mask = 0x1 << hstream->index;
344 dev_err(sdev->dev, "error: no stream available\n");
349 *hstream->posbuf = 0;
351 /* reset BDL address */
352 snd_sof_dsp_write(sdev, HDA_DSP_HDA_BAR,
353 sd_offset + SOF_HDA_ADSP_REG_CL_SD_BDLPL,
355 snd_sof_dsp_write(sdev, HDA_DSP_HDA_BAR,
356 sd_offset + SOF_HDA_ADSP_REG_CL_SD_BDLPU,
361 ret = hda_dsp_stream_setup_bdl(sdev, dmab, hstream);
363 dev_err(sdev->dev, "error: set up of BDL failed\n");
367 /* program BDL address */
368 snd_sof_dsp_write(sdev, HDA_DSP_HDA_BAR,
369 sd_offset + SOF_HDA_ADSP_REG_CL_SD_BDLPL,
370 (u32)hstream->bdl.addr);
371 snd_sof_dsp_write(sdev, HDA_DSP_HDA_BAR,
372 sd_offset + SOF_HDA_ADSP_REG_CL_SD_BDLPU,
373 upper_32_bits(hstream->bdl.addr));
375 /* program cyclic buffer length */
376 snd_sof_dsp_write(sdev, HDA_DSP_HDA_BAR,
377 sd_offset + SOF_HDA_ADSP_REG_CL_SD_CBL,
380 /* program last valid index */
381 snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR,
382 sd_offset + SOF_HDA_ADSP_REG_CL_SD_LVI,
383 0xffff, (hstream->frags - 1));
385 /* decouple host and link DMA, enable DSP features */
386 snd_sof_dsp_update_bits(sdev, HDA_DSP_PP_BAR, SOF_HDA_REG_PP_PPCTL,
389 /* Follow HW recommendation to set the guardband value to 95us during FW boot */
390 snd_hdac_chip_updateb(bus, VS_LTRP, HDA_VS_INTEL_LTRP_GB_MASK, HDA_LTRP_GB_VALUE_US);
393 snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR, sd_offset,
394 SOF_HDA_SD_CTL_DMA_START, SOF_HDA_SD_CTL_DMA_START);
400 * prepare for common hdac registers settings, for both code loader
403 int hda_dsp_stream_hw_params(struct snd_sof_dev *sdev,
404 struct hdac_ext_stream *stream,
405 struct snd_dma_buffer *dmab,
406 struct snd_pcm_hw_params *params)
408 struct hdac_bus *bus = sof_to_bus(sdev);
409 struct hdac_stream *hstream = &stream->hstream;
410 int sd_offset = SOF_STREAM_SD_OFFSET(hstream);
411 int ret, timeout = HDA_DSP_STREAM_RESET_TIMEOUT;
412 u32 dma_start = SOF_HDA_SD_CTL_DMA_START;
417 dev_err(sdev->dev, "error: no stream available\n");
421 /* decouple host and link DMA */
422 mask = 0x1 << hstream->index;
423 snd_sof_dsp_update_bits(sdev, HDA_DSP_PP_BAR, SOF_HDA_REG_PP_PPCTL,
427 dev_err(sdev->dev, "error: no dma buffer allocated!\n");
431 /* clear stream status */
432 snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR, sd_offset,
433 SOF_HDA_CL_DMA_SD_INT_MASK |
434 SOF_HDA_SD_CTL_DMA_START, 0);
436 ret = snd_sof_dsp_read_poll_timeout(sdev, HDA_DSP_HDA_BAR,
439 HDA_DSP_REG_POLL_INTERVAL_US,
440 HDA_DSP_STREAM_RUN_TIMEOUT);
444 "error: %s: timeout on STREAM_SD_OFFSET read1\n",
449 snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR,
450 sd_offset + SOF_HDA_ADSP_REG_CL_SD_STS,
451 SOF_HDA_CL_DMA_SD_INT_MASK,
452 SOF_HDA_CL_DMA_SD_INT_MASK);
455 snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR, sd_offset, 0x1,
459 val = snd_sof_dsp_read(sdev, HDA_DSP_HDA_BAR,
465 dev_err(sdev->dev, "error: stream reset failed\n");
469 timeout = HDA_DSP_STREAM_RESET_TIMEOUT;
470 snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR, sd_offset, 0x1,
473 /* wait for hardware to report that stream is out of reset */
476 val = snd_sof_dsp_read(sdev, HDA_DSP_HDA_BAR,
478 if ((val & 0x1) == 0)
482 dev_err(sdev->dev, "error: timeout waiting for stream reset\n");
487 *hstream->posbuf = 0;
489 /* reset BDL address */
490 snd_sof_dsp_write(sdev, HDA_DSP_HDA_BAR,
491 sd_offset + SOF_HDA_ADSP_REG_CL_SD_BDLPL,
493 snd_sof_dsp_write(sdev, HDA_DSP_HDA_BAR,
494 sd_offset + SOF_HDA_ADSP_REG_CL_SD_BDLPU,
497 /* clear stream status */
498 snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR, sd_offset,
499 SOF_HDA_CL_DMA_SD_INT_MASK |
500 SOF_HDA_SD_CTL_DMA_START, 0);
502 ret = snd_sof_dsp_read_poll_timeout(sdev, HDA_DSP_HDA_BAR,
505 HDA_DSP_REG_POLL_INTERVAL_US,
506 HDA_DSP_STREAM_RUN_TIMEOUT);
510 "error: %s: timeout on STREAM_SD_OFFSET read2\n",
515 snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR,
516 sd_offset + SOF_HDA_ADSP_REG_CL_SD_STS,
517 SOF_HDA_CL_DMA_SD_INT_MASK,
518 SOF_HDA_CL_DMA_SD_INT_MASK);
522 ret = hda_dsp_stream_setup_bdl(sdev, dmab, hstream);
524 dev_err(sdev->dev, "error: set up of BDL failed\n");
528 /* program stream tag to set up stream descriptor for DMA */
529 snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR, sd_offset,
530 SOF_HDA_CL_SD_CTL_STREAM_TAG_MASK,
531 hstream->stream_tag <<
532 SOF_HDA_CL_SD_CTL_STREAM_TAG_SHIFT);
534 /* program cyclic buffer length */
535 snd_sof_dsp_write(sdev, HDA_DSP_HDA_BAR,
536 sd_offset + SOF_HDA_ADSP_REG_CL_SD_CBL,
540 * Recommended hardware programming sequence for HDAudio DMA format
542 * 1. Put DMA into coupled mode by clearing PPCTL.PROCEN bit
543 * for corresponding stream index before the time of writing
544 * format to SDxFMT register.
546 * 3. Set PPCTL.PROCEN bit for corresponding stream index to
547 * enable decoupled mode
550 /* couple host and link DMA, disable DSP features */
551 snd_sof_dsp_update_bits(sdev, HDA_DSP_PP_BAR, SOF_HDA_REG_PP_PPCTL,
554 /* program stream format */
555 snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR,
557 SOF_HDA_ADSP_REG_CL_SD_FORMAT,
558 0xffff, hstream->format_val);
560 /* decouple host and link DMA, enable DSP features */
561 snd_sof_dsp_update_bits(sdev, HDA_DSP_PP_BAR, SOF_HDA_REG_PP_PPCTL,
564 /* program last valid index */
565 snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR,
566 sd_offset + SOF_HDA_ADSP_REG_CL_SD_LVI,
567 0xffff, (hstream->frags - 1));
569 /* program BDL address */
570 snd_sof_dsp_write(sdev, HDA_DSP_HDA_BAR,
571 sd_offset + SOF_HDA_ADSP_REG_CL_SD_BDLPL,
572 (u32)hstream->bdl.addr);
573 snd_sof_dsp_write(sdev, HDA_DSP_HDA_BAR,
574 sd_offset + SOF_HDA_ADSP_REG_CL_SD_BDLPU,
575 upper_32_bits(hstream->bdl.addr));
577 /* enable position buffer */
578 if (!(snd_sof_dsp_read(sdev, HDA_DSP_HDA_BAR, SOF_HDA_ADSP_DPLBASE)
579 & SOF_HDA_ADSP_DPLBASE_ENABLE)) {
580 snd_sof_dsp_write(sdev, HDA_DSP_HDA_BAR, SOF_HDA_ADSP_DPUBASE,
581 upper_32_bits(bus->posbuf.addr));
582 snd_sof_dsp_write(sdev, HDA_DSP_HDA_BAR, SOF_HDA_ADSP_DPLBASE,
583 (u32)bus->posbuf.addr |
584 SOF_HDA_ADSP_DPLBASE_ENABLE);
587 /* set interrupt enable bits */
588 snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR, sd_offset,
589 SOF_HDA_CL_DMA_SD_INT_MASK,
590 SOF_HDA_CL_DMA_SD_INT_MASK);
593 if (hstream->direction == SNDRV_PCM_STREAM_PLAYBACK) {
595 snd_sof_dsp_read(sdev, HDA_DSP_HDA_BAR,
597 SOF_HDA_ADSP_REG_CL_SD_FIFOSIZE);
598 hstream->fifo_size &= 0xffff;
599 hstream->fifo_size += 1;
601 hstream->fifo_size = 0;
607 int hda_dsp_stream_hw_free(struct snd_sof_dev *sdev,
608 struct snd_pcm_substream *substream)
610 struct hdac_stream *stream = substream->runtime->private_data;
611 struct hdac_ext_stream *link_dev = container_of(stream,
612 struct hdac_ext_stream,
614 struct hdac_bus *bus = sof_to_bus(sdev);
615 u32 mask = 0x1 << stream->index;
617 spin_lock_irq(&bus->reg_lock);
618 /* couple host and link DMA if link DMA channel is idle */
619 if (!link_dev->link_locked)
620 snd_sof_dsp_update_bits(sdev, HDA_DSP_PP_BAR,
621 SOF_HDA_REG_PP_PPCTL, mask, 0);
622 spin_unlock_irq(&bus->reg_lock);
624 stream->substream = NULL;
629 bool hda_dsp_check_stream_irq(struct snd_sof_dev *sdev)
631 struct hdac_bus *bus = sof_to_bus(sdev);
635 /* The function can be called at irq thread, so use spin_lock_irq */
636 spin_lock_irq(&bus->reg_lock);
638 status = snd_hdac_chip_readl(bus, INTSTS);
639 dev_vdbg(bus->dev, "stream irq, INTSTS status: 0x%x\n", status);
641 /* if Register inaccessible, ignore it.*/
642 if (status != 0xffffffff)
645 spin_unlock_irq(&bus->reg_lock);
651 hda_dsp_set_bytes_transferred(struct hdac_stream *hstream, u64 buffer_size)
653 u64 prev_pos, pos, num_bytes;
655 div64_u64_rem(hstream->curr_pos, buffer_size, &prev_pos);
656 pos = snd_hdac_stream_get_pos_posbuf(hstream);
659 num_bytes = (buffer_size - prev_pos) + pos;
661 num_bytes = pos - prev_pos;
663 hstream->curr_pos += num_bytes;
666 static bool hda_dsp_stream_check(struct hdac_bus *bus, u32 status)
668 struct sof_intel_hda_dev *sof_hda = bus_to_sof_hda(bus);
669 struct hdac_stream *s;
673 list_for_each_entry(s, &bus->stream_list, list) {
674 if (status & BIT(s->index) && s->opened) {
675 sd_status = snd_hdac_stream_readb(s, SD_STS);
677 dev_vdbg(bus->dev, "stream %d status 0x%x\n",
678 s->index, sd_status);
680 snd_hdac_stream_writeb(s, SD_STS, sd_status);
683 if ((!s->substream && !s->cstream) ||
685 (sd_status & SOF_HDA_CL_DMA_SD_INT_COMPLETE) == 0)
688 /* Inform ALSA only in case not do that with IPC */
689 if (s->substream && sof_hda->no_ipc_position) {
690 snd_sof_pcm_period_elapsed(s->substream);
691 } else if (s->cstream) {
692 hda_dsp_set_bytes_transferred(s,
693 s->cstream->runtime->buffer_size);
694 snd_compr_fragment_elapsed(s->cstream);
702 irqreturn_t hda_dsp_stream_threaded_handler(int irq, void *context)
704 struct snd_sof_dev *sdev = context;
705 struct hdac_bus *bus = sof_to_bus(sdev);
706 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
714 * Loop 10 times to handle missed interrupts caused by
715 * unsolicited responses from the codec
717 for (i = 0, active = true; i < 10 && active; i++) {
718 spin_lock_irq(&bus->reg_lock);
720 status = snd_hdac_chip_readl(bus, INTSTS);
723 active = hda_dsp_stream_check(bus, status);
725 /* check and clear RIRB interrupt */
726 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
727 if (status & AZX_INT_CTRL_EN) {
728 rirb_status = snd_hdac_chip_readb(bus, RIRBSTS);
729 if (rirb_status & RIRB_INT_MASK) {
731 * Clearing the interrupt status here ensures
732 * that no interrupt gets masked after the RIRB
733 * wp is read in snd_hdac_bus_update_rirb.
735 snd_hdac_chip_writeb(bus, RIRBSTS,
738 if (rirb_status & RIRB_INT_RESPONSE)
739 snd_hdac_bus_update_rirb(bus);
743 spin_unlock_irq(&bus->reg_lock);
749 int hda_dsp_stream_init(struct snd_sof_dev *sdev)
751 struct hdac_bus *bus = sof_to_bus(sdev);
752 struct hdac_ext_stream *stream;
753 struct hdac_stream *hstream;
754 struct pci_dev *pci = to_pci_dev(sdev->dev);
755 struct sof_intel_hda_dev *sof_hda = bus_to_sof_hda(bus);
757 int i, num_playback, num_capture, num_total, ret;
760 gcap = snd_sof_dsp_read(sdev, HDA_DSP_HDA_BAR, SOF_HDA_GCAP);
761 dev_dbg(sdev->dev, "hda global caps = 0x%x\n", gcap);
763 /* get stream count from GCAP */
764 num_capture = (gcap >> 8) & 0x0f;
765 num_playback = (gcap >> 12) & 0x0f;
766 num_total = num_playback + num_capture;
768 dev_dbg(sdev->dev, "detected %d playback and %d capture streams\n",
769 num_playback, num_capture);
771 if (num_playback >= SOF_HDA_PLAYBACK_STREAMS) {
772 dev_err(sdev->dev, "error: too many playback streams %d\n",
777 if (num_capture >= SOF_HDA_CAPTURE_STREAMS) {
778 dev_err(sdev->dev, "error: too many capture streams %d\n",
784 * mem alloc for the position buffer
785 * TODO: check position buffer update
787 ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, &pci->dev,
788 SOF_HDA_DPIB_ENTRY_SIZE * num_total,
791 dev_err(sdev->dev, "error: posbuffer dma alloc failed\n");
795 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
796 /* mem alloc for the CORB/RIRB ringbuffers */
797 ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, &pci->dev,
798 PAGE_SIZE, &bus->rb);
800 dev_err(sdev->dev, "error: RB alloc failed\n");
805 /* create capture streams */
806 for (i = 0; i < num_capture; i++) {
807 struct sof_intel_hda_stream *hda_stream;
809 hda_stream = devm_kzalloc(sdev->dev, sizeof(*hda_stream),
814 hda_stream->sdev = sdev;
816 stream = &hda_stream->hda_stream;
818 stream->pphc_addr = sdev->bar[HDA_DSP_PP_BAR] +
819 SOF_HDA_PPHC_BASE + SOF_HDA_PPHC_INTERVAL * i;
821 stream->pplc_addr = sdev->bar[HDA_DSP_PP_BAR] +
822 SOF_HDA_PPLC_BASE + SOF_HDA_PPLC_MULTI * num_total +
823 SOF_HDA_PPLC_INTERVAL * i;
825 /* do we support SPIB */
826 if (sdev->bar[HDA_DSP_SPIB_BAR]) {
827 stream->spib_addr = sdev->bar[HDA_DSP_SPIB_BAR] +
828 SOF_HDA_SPIB_BASE + SOF_HDA_SPIB_INTERVAL * i +
831 stream->fifo_addr = sdev->bar[HDA_DSP_SPIB_BAR] +
832 SOF_HDA_SPIB_BASE + SOF_HDA_SPIB_INTERVAL * i +
833 SOF_HDA_SPIB_MAXFIFO;
836 hstream = &stream->hstream;
838 hstream->sd_int_sta_mask = 1 << i;
840 sd_offset = SOF_STREAM_SD_OFFSET(hstream);
841 hstream->sd_addr = sdev->bar[HDA_DSP_HDA_BAR] + sd_offset;
842 hstream->stream_tag = i + 1;
843 hstream->opened = false;
844 hstream->running = false;
845 hstream->direction = SNDRV_PCM_STREAM_CAPTURE;
847 /* memory alloc for stream BDL */
848 ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, &pci->dev,
849 HDA_DSP_BDL_SIZE, &hstream->bdl);
851 dev_err(sdev->dev, "error: stream bdl dma alloc failed\n");
854 hstream->posbuf = (__le32 *)(bus->posbuf.area +
855 (hstream->index) * 8);
857 list_add_tail(&hstream->list, &bus->stream_list);
860 /* create playback streams */
861 for (i = num_capture; i < num_total; i++) {
862 struct sof_intel_hda_stream *hda_stream;
864 hda_stream = devm_kzalloc(sdev->dev, sizeof(*hda_stream),
869 hda_stream->sdev = sdev;
871 stream = &hda_stream->hda_stream;
873 /* we always have DSP support */
874 stream->pphc_addr = sdev->bar[HDA_DSP_PP_BAR] +
875 SOF_HDA_PPHC_BASE + SOF_HDA_PPHC_INTERVAL * i;
877 stream->pplc_addr = sdev->bar[HDA_DSP_PP_BAR] +
878 SOF_HDA_PPLC_BASE + SOF_HDA_PPLC_MULTI * num_total +
879 SOF_HDA_PPLC_INTERVAL * i;
881 /* do we support SPIB */
882 if (sdev->bar[HDA_DSP_SPIB_BAR]) {
883 stream->spib_addr = sdev->bar[HDA_DSP_SPIB_BAR] +
884 SOF_HDA_SPIB_BASE + SOF_HDA_SPIB_INTERVAL * i +
887 stream->fifo_addr = sdev->bar[HDA_DSP_SPIB_BAR] +
888 SOF_HDA_SPIB_BASE + SOF_HDA_SPIB_INTERVAL * i +
889 SOF_HDA_SPIB_MAXFIFO;
892 hstream = &stream->hstream;
894 hstream->sd_int_sta_mask = 1 << i;
896 sd_offset = SOF_STREAM_SD_OFFSET(hstream);
897 hstream->sd_addr = sdev->bar[HDA_DSP_HDA_BAR] + sd_offset;
898 hstream->stream_tag = i - num_capture + 1;
899 hstream->opened = false;
900 hstream->running = false;
901 hstream->direction = SNDRV_PCM_STREAM_PLAYBACK;
903 /* mem alloc for stream BDL */
904 ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, &pci->dev,
905 HDA_DSP_BDL_SIZE, &hstream->bdl);
907 dev_err(sdev->dev, "error: stream bdl dma alloc failed\n");
911 hstream->posbuf = (__le32 *)(bus->posbuf.area +
912 (hstream->index) * 8);
914 list_add_tail(&hstream->list, &bus->stream_list);
917 /* store total stream count (playback + capture) from GCAP */
918 sof_hda->stream_max = num_total;
923 void hda_dsp_stream_free(struct snd_sof_dev *sdev)
925 struct hdac_bus *bus = sof_to_bus(sdev);
926 struct hdac_stream *s, *_s;
927 struct hdac_ext_stream *stream;
928 struct sof_intel_hda_stream *hda_stream;
930 /* free position buffer */
931 if (bus->posbuf.area)
932 snd_dma_free_pages(&bus->posbuf);
934 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
935 /* free position buffer */
937 snd_dma_free_pages(&bus->rb);
940 list_for_each_entry_safe(s, _s, &bus->stream_list, list) {
943 /* free bdl buffer */
945 snd_dma_free_pages(&s->bdl);
947 stream = stream_to_hdac_ext_stream(s);
948 hda_stream = container_of(stream, struct sof_intel_hda_stream,
950 devm_kfree(sdev->dev, hda_stream);