hda-stream.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187
  1. // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
  2. //
  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.
  5. //
  6. // Copyright(c) 2018 Intel Corporation
  7. //
  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>
  12. //
  13. /*
  14. * Hardware interface for generic Intel audio DSP HDA IP
  15. */
  16. #include <sound/hdaudio_ext.h>
  17. #include <sound/hda_register.h>
  18. #include <sound/sof.h>
  19. #include <trace/events/sof_intel.h>
  20. #include "../ops.h"
  21. #include "../sof-audio.h"
  22. #include "../ipc4-priv.h"
  23. #include "hda.h"
  24. int sof_hda_position_quirk = SOF_HDA_POSITION_QUIRK_USE_DPIB_REGISTERS;
  25. module_param_named(position_quirk, sof_hda_position_quirk, int, 0444);
  26. MODULE_PARM_DESC(position_quirk, "SOF HDaudio position quirk");
  27. EXPORT_SYMBOL_NS(sof_hda_position_quirk, SND_SOC_SOF_INTEL_HDA_COMMON);
  28. #define HDA_LTRP_GB_VALUE_US 95
  29. static inline const char *hda_hstream_direction_str(struct hdac_stream *hstream)
  30. {
  31. if (hstream->direction == SNDRV_PCM_STREAM_PLAYBACK)
  32. return "Playback";
  33. else
  34. return "Capture";
  35. }
  36. static char *hda_hstream_dbg_get_stream_info_str(struct hdac_stream *hstream)
  37. {
  38. struct snd_soc_pcm_runtime *rtd;
  39. if (hstream->substream)
  40. rtd = snd_soc_substream_to_rtd(hstream->substream);
  41. else if (hstream->cstream)
  42. rtd = hstream->cstream->private_data;
  43. else
  44. /* Non audio DMA user, like dma-trace */
  45. return kasprintf(GFP_KERNEL, "-- (%s, stream_tag: %u)",
  46. hda_hstream_direction_str(hstream),
  47. hstream->stream_tag);
  48. return kasprintf(GFP_KERNEL, "dai_link \"%s\" (%s, stream_tag: %u)",
  49. rtd->dai_link->name, hda_hstream_direction_str(hstream),
  50. hstream->stream_tag);
  51. }
  52. /*
  53. * set up one of BDL entries for a stream
  54. */
  55. static int hda_setup_bdle(struct snd_sof_dev *sdev,
  56. struct snd_dma_buffer *dmab,
  57. struct hdac_stream *hstream,
  58. struct sof_intel_dsp_bdl **bdlp,
  59. int offset, int size, int ioc)
  60. {
  61. struct hdac_bus *bus = sof_to_bus(sdev);
  62. struct sof_intel_dsp_bdl *bdl = *bdlp;
  63. while (size > 0) {
  64. dma_addr_t addr;
  65. int chunk;
  66. if (hstream->frags >= HDA_DSP_MAX_BDL_ENTRIES) {
  67. dev_err(sdev->dev, "error: stream frags exceeded\n");
  68. return -EINVAL;
  69. }
  70. addr = snd_sgbuf_get_addr(dmab, offset);
  71. /* program BDL addr */
  72. bdl->addr_l = cpu_to_le32(lower_32_bits(addr));
  73. bdl->addr_h = cpu_to_le32(upper_32_bits(addr));
  74. /* program BDL size */
  75. chunk = snd_sgbuf_get_chunk_size(dmab, offset, size);
  76. /* one BDLE should not cross 4K boundary */
  77. if (bus->align_bdle_4k) {
  78. u32 remain = 0x1000 - (offset & 0xfff);
  79. if (chunk > remain)
  80. chunk = remain;
  81. }
  82. bdl->size = cpu_to_le32(chunk);
  83. /* only program IOC when the whole segment is processed */
  84. size -= chunk;
  85. bdl->ioc = (size || !ioc) ? 0 : cpu_to_le32(0x01);
  86. bdl++;
  87. hstream->frags++;
  88. offset += chunk;
  89. }
  90. *bdlp = bdl;
  91. return offset;
  92. }
  93. /*
  94. * set up Buffer Descriptor List (BDL) for host memory transfer
  95. * BDL describes the location of the individual buffers and is little endian.
  96. */
  97. int hda_dsp_stream_setup_bdl(struct snd_sof_dev *sdev,
  98. struct snd_dma_buffer *dmab,
  99. struct hdac_stream *hstream)
  100. {
  101. struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata;
  102. struct sof_intel_dsp_bdl *bdl;
  103. int i, offset, period_bytes, periods;
  104. int remain, ioc;
  105. period_bytes = hstream->period_bytes;
  106. dev_dbg(sdev->dev, "period_bytes:0x%x\n", period_bytes);
  107. if (!period_bytes)
  108. period_bytes = hstream->bufsize;
  109. periods = hstream->bufsize / period_bytes;
  110. dev_dbg(sdev->dev, "periods:%d\n", periods);
  111. remain = hstream->bufsize % period_bytes;
  112. if (remain)
  113. periods++;
  114. /* program the initial BDL entries */
  115. bdl = (struct sof_intel_dsp_bdl *)hstream->bdl.area;
  116. offset = 0;
  117. hstream->frags = 0;
  118. /*
  119. * set IOC if don't use position IPC
  120. * and period_wakeup needed.
  121. */
  122. ioc = hda->no_ipc_position ?
  123. !hstream->no_period_wakeup : 0;
  124. for (i = 0; i < periods; i++) {
  125. if (i == (periods - 1) && remain)
  126. /* set the last small entry */
  127. offset = hda_setup_bdle(sdev, dmab,
  128. hstream, &bdl, offset,
  129. remain, 0);
  130. else
  131. offset = hda_setup_bdle(sdev, dmab,
  132. hstream, &bdl, offset,
  133. period_bytes, ioc);
  134. }
  135. return offset;
  136. }
  137. int hda_dsp_stream_spib_config(struct snd_sof_dev *sdev,
  138. struct hdac_ext_stream *hext_stream,
  139. int enable, u32 size)
  140. {
  141. struct hdac_stream *hstream = &hext_stream->hstream;
  142. u32 mask;
  143. if (!sdev->bar[HDA_DSP_SPIB_BAR]) {
  144. dev_err(sdev->dev, "error: address of spib capability is NULL\n");
  145. return -EINVAL;
  146. }
  147. mask = (1 << hstream->index);
  148. /* enable/disable SPIB for the stream */
  149. snd_sof_dsp_update_bits(sdev, HDA_DSP_SPIB_BAR,
  150. SOF_HDA_ADSP_REG_CL_SPBFIFO_SPBFCCTL, mask,
  151. enable << hstream->index);
  152. /* set the SPIB value */
  153. sof_io_write(sdev, hstream->spib_addr, size);
  154. return 0;
  155. }
  156. /* get next unused stream */
  157. struct hdac_ext_stream *
  158. hda_dsp_stream_get(struct snd_sof_dev *sdev, int direction, u32 flags)
  159. {
  160. const struct sof_intel_dsp_desc *chip_info = get_chip_info(sdev->pdata);
  161. struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata;
  162. struct hdac_bus *bus = sof_to_bus(sdev);
  163. struct sof_intel_hda_stream *hda_stream;
  164. struct hdac_ext_stream *hext_stream = NULL;
  165. struct hdac_stream *s;
  166. 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. hext_stream = stream_to_hdac_ext_stream(s);
  171. hda_stream = container_of(hext_stream,
  172. struct sof_intel_hda_stream,
  173. hext_stream);
  174. /* check if the host DMA channel is reserved */
  175. if (hda_stream->host_reserved)
  176. continue;
  177. s->opened = true;
  178. break;
  179. }
  180. }
  181. spin_unlock_irq(&bus->reg_lock);
  182. /* stream found ? */
  183. if (!hext_stream) {
  184. dev_err(sdev->dev, "error: no free %s streams\n", snd_pcm_direction_name(direction));
  185. return hext_stream;
  186. }
  187. hda_stream->flags = flags;
  188. /*
  189. * Prevent DMI Link L1 entry for streams that don't support it.
  190. * Workaround to address a known issue with host DMA that results
  191. * in xruns during pause/release in capture scenarios. This is not needed for the ACE IP.
  192. */
  193. if (chip_info->hw_ip_version < SOF_INTEL_ACE_1_0 &&
  194. !(flags & SOF_HDA_STREAM_DMI_L1_COMPATIBLE)) {
  195. snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR,
  196. HDA_VS_INTEL_EM2,
  197. HDA_VS_INTEL_EM2_L1SEN, 0);
  198. hda->l1_disabled = true;
  199. }
  200. return hext_stream;
  201. }
  202. /* free a stream */
  203. int hda_dsp_stream_put(struct snd_sof_dev *sdev, int direction, int stream_tag)
  204. {
  205. const struct sof_intel_dsp_desc *chip_info = get_chip_info(sdev->pdata);
  206. struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata;
  207. struct hdac_bus *bus = sof_to_bus(sdev);
  208. struct sof_intel_hda_stream *hda_stream;
  209. struct hdac_ext_stream *hext_stream;
  210. struct hdac_stream *s;
  211. bool dmi_l1_enable = true;
  212. bool found = false;
  213. spin_lock_irq(&bus->reg_lock);
  214. /*
  215. * close stream matching the stream tag and check if there are any open streams
  216. * that are DMI L1 incompatible.
  217. */
  218. list_for_each_entry(s, &bus->stream_list, list) {
  219. hext_stream = stream_to_hdac_ext_stream(s);
  220. hda_stream = container_of(hext_stream, struct sof_intel_hda_stream, hext_stream);
  221. if (!s->opened)
  222. continue;
  223. if (s->direction == direction && s->stream_tag == stream_tag) {
  224. s->opened = false;
  225. found = true;
  226. } else if (!(hda_stream->flags & SOF_HDA_STREAM_DMI_L1_COMPATIBLE)) {
  227. dmi_l1_enable = false;
  228. }
  229. }
  230. spin_unlock_irq(&bus->reg_lock);
  231. /* Enable DMI L1 if permitted */
  232. if (chip_info->hw_ip_version < SOF_INTEL_ACE_1_0 && dmi_l1_enable) {
  233. snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR, HDA_VS_INTEL_EM2,
  234. HDA_VS_INTEL_EM2_L1SEN, HDA_VS_INTEL_EM2_L1SEN);
  235. hda->l1_disabled = false;
  236. }
  237. if (!found) {
  238. dev_err(sdev->dev, "%s: stream_tag %d not opened!\n",
  239. __func__, stream_tag);
  240. return -ENODEV;
  241. }
  242. return 0;
  243. }
  244. static int hda_dsp_stream_reset(struct snd_sof_dev *sdev, struct hdac_stream *hstream)
  245. {
  246. int sd_offset = SOF_STREAM_SD_OFFSET(hstream);
  247. int timeout = HDA_DSP_STREAM_RESET_TIMEOUT;
  248. u32 val;
  249. /* enter stream reset */
  250. snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR, sd_offset, SOF_STREAM_SD_OFFSET_CRST,
  251. SOF_STREAM_SD_OFFSET_CRST);
  252. do {
  253. val = snd_sof_dsp_read(sdev, HDA_DSP_HDA_BAR, sd_offset);
  254. if (val & SOF_STREAM_SD_OFFSET_CRST)
  255. break;
  256. } while (--timeout);
  257. if (timeout == 0) {
  258. dev_err(sdev->dev, "timeout waiting for stream reset\n");
  259. return -ETIMEDOUT;
  260. }
  261. timeout = HDA_DSP_STREAM_RESET_TIMEOUT;
  262. /* exit stream reset and wait to read a zero before reading any other register */
  263. snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR, sd_offset, SOF_STREAM_SD_OFFSET_CRST, 0x0);
  264. /* wait for hardware to report that stream is out of reset */
  265. udelay(3);
  266. do {
  267. val = snd_sof_dsp_read(sdev, HDA_DSP_HDA_BAR, sd_offset);
  268. if ((val & SOF_STREAM_SD_OFFSET_CRST) == 0)
  269. break;
  270. } while (--timeout);
  271. if (timeout == 0) {
  272. dev_err(sdev->dev, "timeout waiting for stream to exit reset\n");
  273. return -ETIMEDOUT;
  274. }
  275. return 0;
  276. }
  277. int hda_dsp_stream_trigger(struct snd_sof_dev *sdev,
  278. struct hdac_ext_stream *hext_stream, int cmd)
  279. {
  280. struct hdac_stream *hstream = &hext_stream->hstream;
  281. int sd_offset = SOF_STREAM_SD_OFFSET(hstream);
  282. u32 dma_start = SOF_HDA_SD_CTL_DMA_START;
  283. int ret = 0;
  284. u32 run;
  285. /* cmd must be for audio stream */
  286. switch (cmd) {
  287. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  288. if (!sdev->dspless_mode_selected)
  289. break;
  290. fallthrough;
  291. case SNDRV_PCM_TRIGGER_START:
  292. if (hstream->running)
  293. break;
  294. snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR, SOF_HDA_INTCTL,
  295. 1 << hstream->index,
  296. 1 << hstream->index);
  297. snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR,
  298. sd_offset,
  299. SOF_HDA_SD_CTL_DMA_START |
  300. SOF_HDA_CL_DMA_SD_INT_MASK,
  301. SOF_HDA_SD_CTL_DMA_START |
  302. SOF_HDA_CL_DMA_SD_INT_MASK);
  303. ret = snd_sof_dsp_read_poll_timeout(sdev,
  304. HDA_DSP_HDA_BAR,
  305. sd_offset, run,
  306. ((run & dma_start) == dma_start),
  307. HDA_DSP_REG_POLL_INTERVAL_US,
  308. HDA_DSP_STREAM_RUN_TIMEOUT);
  309. if (ret >= 0)
  310. hstream->running = true;
  311. break;
  312. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  313. if (!sdev->dspless_mode_selected)
  314. break;
  315. fallthrough;
  316. case SNDRV_PCM_TRIGGER_SUSPEND:
  317. case SNDRV_PCM_TRIGGER_STOP:
  318. snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR,
  319. sd_offset,
  320. SOF_HDA_SD_CTL_DMA_START |
  321. SOF_HDA_CL_DMA_SD_INT_MASK, 0x0);
  322. ret = snd_sof_dsp_read_poll_timeout(sdev, HDA_DSP_HDA_BAR,
  323. sd_offset, run,
  324. !(run & dma_start),
  325. HDA_DSP_REG_POLL_INTERVAL_US,
  326. HDA_DSP_STREAM_RUN_TIMEOUT);
  327. if (ret >= 0) {
  328. snd_sof_dsp_write(sdev, HDA_DSP_HDA_BAR,
  329. sd_offset + SOF_HDA_ADSP_REG_SD_STS,
  330. SOF_HDA_CL_DMA_SD_INT_MASK);
  331. hstream->running = false;
  332. snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR,
  333. SOF_HDA_INTCTL,
  334. 1 << hstream->index, 0x0);
  335. }
  336. break;
  337. default:
  338. dev_err(sdev->dev, "error: unknown command: %d\n", cmd);
  339. return -EINVAL;
  340. }
  341. if (ret < 0) {
  342. char *stream_name = hda_hstream_dbg_get_stream_info_str(hstream);
  343. dev_err(sdev->dev,
  344. "%s: cmd %d on %s: timeout on STREAM_SD_OFFSET read\n",
  345. __func__, cmd, stream_name ? stream_name : "unknown stream");
  346. kfree(stream_name);
  347. }
  348. return ret;
  349. }
  350. /* minimal recommended programming for ICCMAX stream */
  351. int hda_dsp_iccmax_stream_hw_params(struct snd_sof_dev *sdev, struct hdac_ext_stream *hext_stream,
  352. struct snd_dma_buffer *dmab,
  353. struct snd_pcm_hw_params *params)
  354. {
  355. struct hdac_stream *hstream = &hext_stream->hstream;
  356. int sd_offset = SOF_STREAM_SD_OFFSET(hstream);
  357. int ret;
  358. u32 mask = 0x1 << hstream->index;
  359. if (!hext_stream) {
  360. dev_err(sdev->dev, "error: no stream available\n");
  361. return -ENODEV;
  362. }
  363. if (!dmab) {
  364. dev_err(sdev->dev, "error: no dma buffer allocated!\n");
  365. return -ENODEV;
  366. }
  367. if (hstream->posbuf)
  368. *hstream->posbuf = 0;
  369. /* reset BDL address */
  370. snd_sof_dsp_write(sdev, HDA_DSP_HDA_BAR,
  371. sd_offset + SOF_HDA_ADSP_REG_SD_BDLPL,
  372. 0x0);
  373. snd_sof_dsp_write(sdev, HDA_DSP_HDA_BAR,
  374. sd_offset + SOF_HDA_ADSP_REG_SD_BDLPU,
  375. 0x0);
  376. hstream->frags = 0;
  377. ret = hda_dsp_stream_setup_bdl(sdev, dmab, hstream);
  378. if (ret < 0) {
  379. dev_err(sdev->dev, "error: set up of BDL failed\n");
  380. return ret;
  381. }
  382. /* program BDL address */
  383. snd_sof_dsp_write(sdev, HDA_DSP_HDA_BAR,
  384. sd_offset + SOF_HDA_ADSP_REG_SD_BDLPL,
  385. (u32)hstream->bdl.addr);
  386. snd_sof_dsp_write(sdev, HDA_DSP_HDA_BAR,
  387. sd_offset + SOF_HDA_ADSP_REG_SD_BDLPU,
  388. upper_32_bits(hstream->bdl.addr));
  389. /* program cyclic buffer length */
  390. snd_sof_dsp_write(sdev, HDA_DSP_HDA_BAR,
  391. sd_offset + SOF_HDA_ADSP_REG_SD_CBL,
  392. hstream->bufsize);
  393. /* program last valid index */
  394. snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR,
  395. sd_offset + SOF_HDA_ADSP_REG_SD_LVI,
  396. 0xffff, (hstream->frags - 1));
  397. /* decouple host and link DMA, enable DSP features */
  398. snd_sof_dsp_update_bits(sdev, HDA_DSP_PP_BAR, SOF_HDA_REG_PP_PPCTL,
  399. mask, mask);
  400. /* Follow HW recommendation to set the guardband value to 95us during FW boot */
  401. snd_sof_dsp_update8(sdev, HDA_DSP_HDA_BAR, HDA_VS_INTEL_LTRP,
  402. HDA_VS_INTEL_LTRP_GB_MASK, HDA_LTRP_GB_VALUE_US);
  403. /* start DMA */
  404. snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR, sd_offset,
  405. SOF_HDA_SD_CTL_DMA_START, SOF_HDA_SD_CTL_DMA_START);
  406. return 0;
  407. }
  408. /*
  409. * prepare for common hdac registers settings, for both code loader
  410. * and normal stream.
  411. */
  412. int hda_dsp_stream_hw_params(struct snd_sof_dev *sdev,
  413. struct hdac_ext_stream *hext_stream,
  414. struct snd_dma_buffer *dmab,
  415. struct snd_pcm_hw_params *params)
  416. {
  417. const struct sof_intel_dsp_desc *chip = get_chip_info(sdev->pdata);
  418. struct hdac_bus *bus = sof_to_bus(sdev);
  419. struct hdac_stream *hstream;
  420. int sd_offset, ret;
  421. u32 dma_start = SOF_HDA_SD_CTL_DMA_START;
  422. u32 mask;
  423. u32 run;
  424. if (!hext_stream) {
  425. dev_err(sdev->dev, "error: no stream available\n");
  426. return -ENODEV;
  427. }
  428. if (!dmab) {
  429. dev_err(sdev->dev, "error: no dma buffer allocated!\n");
  430. return -ENODEV;
  431. }
  432. hstream = &hext_stream->hstream;
  433. sd_offset = SOF_STREAM_SD_OFFSET(hstream);
  434. mask = BIT(hstream->index);
  435. /* decouple host and link DMA if the DSP is used */
  436. if (!sdev->dspless_mode_selected)
  437. snd_sof_dsp_update_bits(sdev, HDA_DSP_PP_BAR, SOF_HDA_REG_PP_PPCTL,
  438. mask, mask);
  439. /* clear stream status */
  440. snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR, sd_offset,
  441. SOF_HDA_CL_DMA_SD_INT_MASK |
  442. SOF_HDA_SD_CTL_DMA_START, 0);
  443. ret = snd_sof_dsp_read_poll_timeout(sdev, HDA_DSP_HDA_BAR,
  444. sd_offset, run,
  445. !(run & dma_start),
  446. HDA_DSP_REG_POLL_INTERVAL_US,
  447. HDA_DSP_STREAM_RUN_TIMEOUT);
  448. if (ret < 0) {
  449. char *stream_name = hda_hstream_dbg_get_stream_info_str(hstream);
  450. dev_err(sdev->dev,
  451. "%s: on %s: timeout on STREAM_SD_OFFSET read1\n",
  452. __func__, stream_name ? stream_name : "unknown stream");
  453. kfree(stream_name);
  454. return ret;
  455. }
  456. snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR,
  457. sd_offset + SOF_HDA_ADSP_REG_SD_STS,
  458. SOF_HDA_CL_DMA_SD_INT_MASK,
  459. SOF_HDA_CL_DMA_SD_INT_MASK);
  460. /* stream reset */
  461. ret = hda_dsp_stream_reset(sdev, hstream);
  462. if (ret < 0)
  463. return ret;
  464. if (hstream->posbuf)
  465. *hstream->posbuf = 0;
  466. /* reset BDL address */
  467. snd_sof_dsp_write(sdev, HDA_DSP_HDA_BAR,
  468. sd_offset + SOF_HDA_ADSP_REG_SD_BDLPL,
  469. 0x0);
  470. snd_sof_dsp_write(sdev, HDA_DSP_HDA_BAR,
  471. sd_offset + SOF_HDA_ADSP_REG_SD_BDLPU,
  472. 0x0);
  473. /* clear stream status */
  474. snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR, sd_offset,
  475. SOF_HDA_CL_DMA_SD_INT_MASK |
  476. SOF_HDA_SD_CTL_DMA_START, 0);
  477. ret = snd_sof_dsp_read_poll_timeout(sdev, HDA_DSP_HDA_BAR,
  478. sd_offset, run,
  479. !(run & dma_start),
  480. HDA_DSP_REG_POLL_INTERVAL_US,
  481. HDA_DSP_STREAM_RUN_TIMEOUT);
  482. if (ret < 0) {
  483. char *stream_name = hda_hstream_dbg_get_stream_info_str(hstream);
  484. dev_err(sdev->dev,
  485. "%s: on %s: timeout on STREAM_SD_OFFSET read1\n",
  486. __func__, stream_name ? stream_name : "unknown stream");
  487. kfree(stream_name);
  488. return ret;
  489. }
  490. snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR,
  491. sd_offset + SOF_HDA_ADSP_REG_SD_STS,
  492. SOF_HDA_CL_DMA_SD_INT_MASK,
  493. SOF_HDA_CL_DMA_SD_INT_MASK);
  494. hstream->frags = 0;
  495. ret = hda_dsp_stream_setup_bdl(sdev, dmab, hstream);
  496. if (ret < 0) {
  497. dev_err(sdev->dev, "error: set up of BDL failed\n");
  498. return ret;
  499. }
  500. /* program stream tag to set up stream descriptor for DMA */
  501. snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR, sd_offset,
  502. SOF_HDA_CL_SD_CTL_STREAM_TAG_MASK,
  503. hstream->stream_tag <<
  504. SOF_HDA_CL_SD_CTL_STREAM_TAG_SHIFT);
  505. /* program cyclic buffer length */
  506. snd_sof_dsp_write(sdev, HDA_DSP_HDA_BAR,
  507. sd_offset + SOF_HDA_ADSP_REG_SD_CBL,
  508. hstream->bufsize);
  509. /*
  510. * Recommended hardware programming sequence for HDAudio DMA format
  511. * on earlier platforms - this is not needed on newer platforms
  512. *
  513. * 1. Put DMA into coupled mode by clearing PPCTL.PROCEN bit
  514. * for corresponding stream index before the time of writing
  515. * format to SDxFMT register.
  516. * 2. Write SDxFMT
  517. * 3. Set PPCTL.PROCEN bit for corresponding stream index to
  518. * enable decoupled mode
  519. */
  520. if (!sdev->dspless_mode_selected && (chip->quirks & SOF_INTEL_PROCEN_FMT_QUIRK))
  521. /* couple host and link DMA, disable DSP features */
  522. snd_sof_dsp_update_bits(sdev, HDA_DSP_PP_BAR, SOF_HDA_REG_PP_PPCTL,
  523. mask, 0);
  524. /* program stream format */
  525. snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR,
  526. sd_offset +
  527. SOF_HDA_ADSP_REG_SD_FORMAT,
  528. 0xffff, hstream->format_val);
  529. if (!sdev->dspless_mode_selected && (chip->quirks & SOF_INTEL_PROCEN_FMT_QUIRK))
  530. /* decouple host and link DMA, enable DSP features */
  531. snd_sof_dsp_update_bits(sdev, HDA_DSP_PP_BAR, SOF_HDA_REG_PP_PPCTL,
  532. mask, mask);
  533. /* program last valid index */
  534. snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR,
  535. sd_offset + SOF_HDA_ADSP_REG_SD_LVI,
  536. 0xffff, (hstream->frags - 1));
  537. /* program BDL address */
  538. snd_sof_dsp_write(sdev, HDA_DSP_HDA_BAR,
  539. sd_offset + SOF_HDA_ADSP_REG_SD_BDLPL,
  540. (u32)hstream->bdl.addr);
  541. snd_sof_dsp_write(sdev, HDA_DSP_HDA_BAR,
  542. sd_offset + SOF_HDA_ADSP_REG_SD_BDLPU,
  543. upper_32_bits(hstream->bdl.addr));
  544. /* enable position buffer, if needed */
  545. if (bus->use_posbuf && bus->posbuf.addr &&
  546. !(snd_sof_dsp_read(sdev, HDA_DSP_HDA_BAR, SOF_HDA_ADSP_DPLBASE)
  547. & SOF_HDA_ADSP_DPLBASE_ENABLE)) {
  548. snd_sof_dsp_write(sdev, HDA_DSP_HDA_BAR, SOF_HDA_ADSP_DPUBASE,
  549. upper_32_bits(bus->posbuf.addr));
  550. snd_sof_dsp_write(sdev, HDA_DSP_HDA_BAR, SOF_HDA_ADSP_DPLBASE,
  551. (u32)bus->posbuf.addr |
  552. SOF_HDA_ADSP_DPLBASE_ENABLE);
  553. }
  554. /* set interrupt enable bits */
  555. snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR, sd_offset,
  556. SOF_HDA_CL_DMA_SD_INT_MASK,
  557. SOF_HDA_CL_DMA_SD_INT_MASK);
  558. /* read FIFO size */
  559. if (hstream->direction == SNDRV_PCM_STREAM_PLAYBACK) {
  560. hstream->fifo_size =
  561. snd_sof_dsp_read(sdev, HDA_DSP_HDA_BAR,
  562. sd_offset +
  563. SOF_HDA_ADSP_REG_SD_FIFOSIZE);
  564. hstream->fifo_size &= SOF_HDA_SD_FIFOSIZE_FIFOS_MASK;
  565. hstream->fifo_size += 1;
  566. } else {
  567. hstream->fifo_size = 0;
  568. }
  569. return ret;
  570. }
  571. int hda_dsp_stream_hw_free(struct snd_sof_dev *sdev,
  572. struct snd_pcm_substream *substream)
  573. {
  574. struct hdac_stream *hstream = substream->runtime->private_data;
  575. struct hdac_ext_stream *hext_stream = container_of(hstream,
  576. struct hdac_ext_stream,
  577. hstream);
  578. int ret;
  579. ret = hda_dsp_stream_reset(sdev, hstream);
  580. if (ret < 0)
  581. return ret;
  582. if (!sdev->dspless_mode_selected) {
  583. struct hdac_bus *bus = sof_to_bus(sdev);
  584. u32 mask = BIT(hstream->index);
  585. spin_lock_irq(&bus->reg_lock);
  586. /* couple host and link DMA if link DMA channel is idle */
  587. if (!hext_stream->link_locked)
  588. snd_sof_dsp_update_bits(sdev, HDA_DSP_PP_BAR,
  589. SOF_HDA_REG_PP_PPCTL, mask, 0);
  590. spin_unlock_irq(&bus->reg_lock);
  591. }
  592. hda_dsp_stream_spib_config(sdev, hext_stream, HDA_DSP_SPIB_DISABLE, 0);
  593. hstream->substream = NULL;
  594. return 0;
  595. }
  596. EXPORT_SYMBOL_NS(hda_dsp_stream_hw_free, SND_SOC_SOF_INTEL_HDA_COMMON);
  597. bool hda_dsp_check_stream_irq(struct snd_sof_dev *sdev)
  598. {
  599. struct hdac_bus *bus = sof_to_bus(sdev);
  600. bool ret = false;
  601. u32 status;
  602. /* The function can be called at irq thread, so use spin_lock_irq */
  603. spin_lock_irq(&bus->reg_lock);
  604. status = snd_sof_dsp_read(sdev, HDA_DSP_HDA_BAR, SOF_HDA_INTSTS);
  605. trace_sof_intel_hda_dsp_check_stream_irq(sdev, status);
  606. /* if Register inaccessible, ignore it.*/
  607. if (status != 0xffffffff)
  608. ret = true;
  609. spin_unlock_irq(&bus->reg_lock);
  610. return ret;
  611. }
  612. EXPORT_SYMBOL_NS(hda_dsp_check_stream_irq, SND_SOC_SOF_INTEL_HDA_COMMON);
  613. static void
  614. hda_dsp_compr_bytes_transferred(struct hdac_stream *hstream, int direction)
  615. {
  616. u64 buffer_size = hstream->bufsize;
  617. u64 prev_pos, pos, num_bytes;
  618. div64_u64_rem(hstream->curr_pos, buffer_size, &prev_pos);
  619. pos = hda_dsp_stream_get_position(hstream, direction, false);
  620. if (pos < prev_pos)
  621. num_bytes = (buffer_size - prev_pos) + pos;
  622. else
  623. num_bytes = pos - prev_pos;
  624. hstream->curr_pos += num_bytes;
  625. }
  626. static bool hda_dsp_stream_check(struct hdac_bus *bus, u32 status)
  627. {
  628. struct sof_intel_hda_dev *sof_hda = bus_to_sof_hda(bus);
  629. struct hdac_stream *s;
  630. bool active = false;
  631. u32 sd_status;
  632. list_for_each_entry(s, &bus->stream_list, list) {
  633. if (status & BIT(s->index) && s->opened) {
  634. sd_status = readb(s->sd_addr + SOF_HDA_ADSP_REG_SD_STS);
  635. trace_sof_intel_hda_dsp_stream_status(bus->dev, s, sd_status);
  636. writeb(sd_status, s->sd_addr + SOF_HDA_ADSP_REG_SD_STS);
  637. active = true;
  638. if (!s->running)
  639. continue;
  640. if ((sd_status & SOF_HDA_CL_DMA_SD_INT_COMPLETE) == 0)
  641. continue;
  642. if (!s->substream && !s->cstream) {
  643. /*
  644. * when no substream is found, the DMA may used for code loading
  645. * or data transfers which can rely on wait_for_completion()
  646. */
  647. struct sof_intel_hda_stream *hda_stream;
  648. struct hdac_ext_stream *hext_stream;
  649. hext_stream = stream_to_hdac_ext_stream(s);
  650. hda_stream = container_of(hext_stream, struct sof_intel_hda_stream,
  651. hext_stream);
  652. complete(&hda_stream->ioc);
  653. continue;
  654. }
  655. /* Inform ALSA only if the IPC position is not used */
  656. if (s->substream && sof_hda->no_ipc_position) {
  657. snd_sof_pcm_period_elapsed(s->substream);
  658. } else if (s->cstream) {
  659. hda_dsp_compr_bytes_transferred(s, s->cstream->direction);
  660. snd_compr_fragment_elapsed(s->cstream);
  661. }
  662. }
  663. }
  664. return active;
  665. }
  666. irqreturn_t hda_dsp_stream_threaded_handler(int irq, void *context)
  667. {
  668. struct snd_sof_dev *sdev = context;
  669. struct hdac_bus *bus = sof_to_bus(sdev);
  670. bool active;
  671. u32 status;
  672. int i;
  673. /*
  674. * Loop 10 times to handle missed interrupts caused by
  675. * unsolicited responses from the codec
  676. */
  677. for (i = 0, active = true; i < 10 && active; i++) {
  678. spin_lock_irq(&bus->reg_lock);
  679. status = snd_sof_dsp_read(sdev, HDA_DSP_HDA_BAR, SOF_HDA_INTSTS);
  680. /* check streams */
  681. active = hda_dsp_stream_check(bus, status);
  682. /* check and clear RIRB interrupt */
  683. if (status & AZX_INT_CTRL_EN) {
  684. active |= hda_codec_check_rirb_status(sdev);
  685. }
  686. spin_unlock_irq(&bus->reg_lock);
  687. }
  688. return IRQ_HANDLED;
  689. }
  690. EXPORT_SYMBOL_NS(hda_dsp_stream_threaded_handler, SND_SOC_SOF_INTEL_HDA_COMMON);
  691. int hda_dsp_stream_init(struct snd_sof_dev *sdev)
  692. {
  693. struct hdac_bus *bus = sof_to_bus(sdev);
  694. struct hdac_ext_stream *hext_stream;
  695. struct hdac_stream *hstream;
  696. struct pci_dev *pci = to_pci_dev(sdev->dev);
  697. struct sof_intel_hda_dev *sof_hda = bus_to_sof_hda(bus);
  698. int sd_offset;
  699. int i, num_playback, num_capture, num_total, ret;
  700. u32 gcap;
  701. gcap = snd_sof_dsp_read(sdev, HDA_DSP_HDA_BAR, SOF_HDA_GCAP);
  702. dev_dbg(sdev->dev, "hda global caps = 0x%x\n", gcap);
  703. /* get stream count from GCAP */
  704. num_capture = (gcap >> 8) & 0x0f;
  705. num_playback = (gcap >> 12) & 0x0f;
  706. num_total = num_playback + num_capture;
  707. dev_dbg(sdev->dev, "detected %d playback and %d capture streams\n",
  708. num_playback, num_capture);
  709. if (num_playback >= SOF_HDA_PLAYBACK_STREAMS) {
  710. dev_err(sdev->dev, "error: too many playback streams %d\n",
  711. num_playback);
  712. return -EINVAL;
  713. }
  714. if (num_capture >= SOF_HDA_CAPTURE_STREAMS) {
  715. dev_err(sdev->dev, "error: too many capture streams %d\n",
  716. num_capture);
  717. return -EINVAL;
  718. }
  719. /*
  720. * mem alloc for the position buffer
  721. * TODO: check position buffer update
  722. */
  723. ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, &pci->dev,
  724. SOF_HDA_DPIB_ENTRY_SIZE * num_total,
  725. &bus->posbuf);
  726. if (ret < 0) {
  727. dev_err(sdev->dev, "error: posbuffer dma alloc failed\n");
  728. return -ENOMEM;
  729. }
  730. /*
  731. * mem alloc for the CORB/RIRB ringbuffers - this will be used only for
  732. * HDAudio codecs
  733. */
  734. ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, &pci->dev,
  735. PAGE_SIZE, &bus->rb);
  736. if (ret < 0) {
  737. dev_err(sdev->dev, "error: RB alloc failed\n");
  738. return -ENOMEM;
  739. }
  740. /* create capture and playback streams */
  741. for (i = 0; i < num_total; i++) {
  742. struct sof_intel_hda_stream *hda_stream;
  743. hda_stream = devm_kzalloc(sdev->dev, sizeof(*hda_stream),
  744. GFP_KERNEL);
  745. if (!hda_stream)
  746. return -ENOMEM;
  747. hda_stream->sdev = sdev;
  748. init_completion(&hda_stream->ioc);
  749. hext_stream = &hda_stream->hext_stream;
  750. if (sdev->bar[HDA_DSP_PP_BAR]) {
  751. hext_stream->pphc_addr = sdev->bar[HDA_DSP_PP_BAR] +
  752. SOF_HDA_PPHC_BASE + SOF_HDA_PPHC_INTERVAL * i;
  753. hext_stream->pplc_addr = sdev->bar[HDA_DSP_PP_BAR] +
  754. SOF_HDA_PPLC_BASE + SOF_HDA_PPLC_MULTI * num_total +
  755. SOF_HDA_PPLC_INTERVAL * i;
  756. }
  757. hstream = &hext_stream->hstream;
  758. /* do we support SPIB */
  759. if (sdev->bar[HDA_DSP_SPIB_BAR]) {
  760. hstream->spib_addr = sdev->bar[HDA_DSP_SPIB_BAR] +
  761. SOF_HDA_SPIB_BASE + SOF_HDA_SPIB_INTERVAL * i +
  762. SOF_HDA_SPIB_SPIB;
  763. hstream->fifo_addr = sdev->bar[HDA_DSP_SPIB_BAR] +
  764. SOF_HDA_SPIB_BASE + SOF_HDA_SPIB_INTERVAL * i +
  765. SOF_HDA_SPIB_MAXFIFO;
  766. }
  767. hstream->bus = bus;
  768. hstream->sd_int_sta_mask = 1 << i;
  769. hstream->index = i;
  770. sd_offset = SOF_STREAM_SD_OFFSET(hstream);
  771. hstream->sd_addr = sdev->bar[HDA_DSP_HDA_BAR] + sd_offset;
  772. hstream->opened = false;
  773. hstream->running = false;
  774. if (i < num_capture) {
  775. hstream->stream_tag = i + 1;
  776. hstream->direction = SNDRV_PCM_STREAM_CAPTURE;
  777. } else {
  778. hstream->stream_tag = i - num_capture + 1;
  779. hstream->direction = SNDRV_PCM_STREAM_PLAYBACK;
  780. }
  781. /* mem alloc for stream BDL */
  782. ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, &pci->dev,
  783. HDA_DSP_BDL_SIZE, &hstream->bdl);
  784. if (ret < 0) {
  785. dev_err(sdev->dev, "error: stream bdl dma alloc failed\n");
  786. return -ENOMEM;
  787. }
  788. hstream->posbuf = (__le32 *)(bus->posbuf.area +
  789. (hstream->index) * 8);
  790. list_add_tail(&hstream->list, &bus->stream_list);
  791. }
  792. /* store total stream count (playback + capture) from GCAP */
  793. sof_hda->stream_max = num_total;
  794. /* store stream count from GCAP required for CHAIN_DMA */
  795. if (sdev->pdata->ipc_type == SOF_IPC_TYPE_4) {
  796. struct sof_ipc4_fw_data *ipc4_data = sdev->private;
  797. ipc4_data->num_playback_streams = num_playback;
  798. ipc4_data->num_capture_streams = num_capture;
  799. }
  800. return 0;
  801. }
  802. EXPORT_SYMBOL_NS(hda_dsp_stream_init, SND_SOC_SOF_INTEL_HDA_COMMON);
  803. void hda_dsp_stream_free(struct snd_sof_dev *sdev)
  804. {
  805. struct hdac_bus *bus = sof_to_bus(sdev);
  806. struct hdac_stream *s, *_s;
  807. struct hdac_ext_stream *hext_stream;
  808. struct sof_intel_hda_stream *hda_stream;
  809. /* free position buffer */
  810. if (bus->posbuf.area)
  811. snd_dma_free_pages(&bus->posbuf);
  812. /* free CORB/RIRB buffer - only used for HDaudio codecs */
  813. if (bus->rb.area)
  814. snd_dma_free_pages(&bus->rb);
  815. list_for_each_entry_safe(s, _s, &bus->stream_list, list) {
  816. /* TODO: decouple */
  817. /* free bdl buffer */
  818. if (s->bdl.area)
  819. snd_dma_free_pages(&s->bdl);
  820. list_del(&s->list);
  821. hext_stream = stream_to_hdac_ext_stream(s);
  822. hda_stream = container_of(hext_stream, struct sof_intel_hda_stream,
  823. hext_stream);
  824. devm_kfree(sdev->dev, hda_stream);
  825. }
  826. }
  827. EXPORT_SYMBOL_NS(hda_dsp_stream_free, SND_SOC_SOF_INTEL_HDA_COMMON);
  828. snd_pcm_uframes_t hda_dsp_stream_get_position(struct hdac_stream *hstream,
  829. int direction, bool can_sleep)
  830. {
  831. struct hdac_ext_stream *hext_stream = stream_to_hdac_ext_stream(hstream);
  832. struct sof_intel_hda_stream *hda_stream = hstream_to_sof_hda_stream(hext_stream);
  833. struct snd_sof_dev *sdev = hda_stream->sdev;
  834. snd_pcm_uframes_t pos;
  835. switch (sof_hda_position_quirk) {
  836. case SOF_HDA_POSITION_QUIRK_USE_SKYLAKE_LEGACY:
  837. /*
  838. * This legacy code, inherited from the Skylake driver,
  839. * mixes DPIB registers and DPIB DDR updates and
  840. * does not seem to follow any known hardware recommendations.
  841. * It's not clear e.g. why there is a different flow
  842. * for capture and playback, the only information that matters is
  843. * what traffic class is used, and on all SOF-enabled platforms
  844. * only VC0 is supported so the work-around was likely not necessary
  845. * and quite possibly wrong.
  846. */
  847. /* DPIB/posbuf position mode:
  848. * For Playback, Use DPIB register from HDA space which
  849. * reflects the actual data transferred.
  850. * For Capture, Use the position buffer for pointer, as DPIB
  851. * is not accurate enough, its update may be completed
  852. * earlier than the data written to DDR.
  853. */
  854. if (direction == SNDRV_PCM_STREAM_PLAYBACK) {
  855. pos = snd_sof_dsp_read(sdev, HDA_DSP_HDA_BAR,
  856. AZX_REG_VS_SDXDPIB_XBASE +
  857. (AZX_REG_VS_SDXDPIB_XINTERVAL *
  858. hstream->index));
  859. } else {
  860. /*
  861. * For capture stream, we need more workaround to fix the
  862. * position incorrect issue:
  863. *
  864. * 1. Wait at least 20us before reading position buffer after
  865. * the interrupt generated(IOC), to make sure position update
  866. * happens on frame boundary i.e. 20.833uSec for 48KHz.
  867. * 2. Perform a dummy Read to DPIB register to flush DMA
  868. * position value.
  869. * 3. Read the DMA Position from posbuf. Now the readback
  870. * value should be >= period boundary.
  871. */
  872. if (can_sleep)
  873. usleep_range(20, 21);
  874. snd_sof_dsp_read(sdev, HDA_DSP_HDA_BAR,
  875. AZX_REG_VS_SDXDPIB_XBASE +
  876. (AZX_REG_VS_SDXDPIB_XINTERVAL *
  877. hstream->index));
  878. pos = snd_hdac_stream_get_pos_posbuf(hstream);
  879. }
  880. break;
  881. case SOF_HDA_POSITION_QUIRK_USE_DPIB_REGISTERS:
  882. /*
  883. * In case VC1 traffic is disabled this is the recommended option
  884. */
  885. pos = snd_sof_dsp_read(sdev, HDA_DSP_HDA_BAR,
  886. AZX_REG_VS_SDXDPIB_XBASE +
  887. (AZX_REG_VS_SDXDPIB_XINTERVAL *
  888. hstream->index));
  889. break;
  890. case SOF_HDA_POSITION_QUIRK_USE_DPIB_DDR_UPDATE:
  891. /*
  892. * This is the recommended option when VC1 is enabled.
  893. * While this isn't needed for SOF platforms it's added for
  894. * consistency and debug.
  895. */
  896. pos = snd_hdac_stream_get_pos_posbuf(hstream);
  897. break;
  898. default:
  899. dev_err_once(sdev->dev, "hda_position_quirk value %d not supported\n",
  900. sof_hda_position_quirk);
  901. pos = 0;
  902. break;
  903. }
  904. if (pos >= hstream->bufsize)
  905. pos = 0;
  906. return pos;
  907. }
  908. EXPORT_SYMBOL_NS(hda_dsp_stream_get_position, SND_SOC_SOF_INTEL_HDA_COMMON);
  909. #define merge_u64(u32_u, u32_l) (((u64)(u32_u) << 32) | (u32_l))
  910. /**
  911. * hda_dsp_get_stream_llp - Retrieve the LLP (Linear Link Position) of the stream
  912. * @sdev: SOF device
  913. * @component: ASoC component
  914. * @substream: PCM substream
  915. *
  916. * Returns the raw Linear Link Position value
  917. */
  918. u64 hda_dsp_get_stream_llp(struct snd_sof_dev *sdev,
  919. struct snd_soc_component *component,
  920. struct snd_pcm_substream *substream)
  921. {
  922. struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
  923. struct snd_soc_pcm_runtime *be_rtd = NULL;
  924. struct hdac_ext_stream *hext_stream;
  925. struct snd_soc_dai *cpu_dai;
  926. struct snd_soc_dpcm *dpcm;
  927. u32 llp_l, llp_u;
  928. /*
  929. * The LLP needs to be read from the Link DMA used for this FE as it is
  930. * allowed to use any combination of Link and Host channels
  931. */
  932. for_each_dpcm_be(rtd, substream->stream, dpcm) {
  933. if (dpcm->fe != rtd)
  934. continue;
  935. be_rtd = dpcm->be;
  936. }
  937. if (!be_rtd)
  938. return 0;
  939. cpu_dai = snd_soc_rtd_to_cpu(be_rtd, 0);
  940. if (!cpu_dai)
  941. return 0;
  942. hext_stream = snd_soc_dai_get_dma_data(cpu_dai, substream);
  943. if (!hext_stream)
  944. return 0;
  945. /*
  946. * The pplc_addr have been calculated during probe in
  947. * hda_dsp_stream_init():
  948. * pplc_addr = sdev->bar[HDA_DSP_PP_BAR] +
  949. * SOF_HDA_PPLC_BASE +
  950. * SOF_HDA_PPLC_MULTI * total_stream +
  951. * SOF_HDA_PPLC_INTERVAL * stream_index
  952. *
  953. * Use this pre-calculated address to avoid repeated re-calculation.
  954. */
  955. llp_l = readl(hext_stream->pplc_addr + AZX_REG_PPLCLLPL);
  956. llp_u = readl(hext_stream->pplc_addr + AZX_REG_PPLCLLPU);
  957. /* Compensate the LLP counter with the saved offset */
  958. if (hext_stream->pplcllpl || hext_stream->pplcllpu)
  959. return merge_u64(llp_u, llp_l) -
  960. merge_u64(hext_stream->pplcllpu, hext_stream->pplcllpl);
  961. return merge_u64(llp_u, llp_l);
  962. }
  963. EXPORT_SYMBOL_NS(hda_dsp_get_stream_llp, SND_SOC_SOF_INTEL_HDA_COMMON);
  964. /**
  965. * hda_dsp_get_stream_ldp - Retrieve the LDP (Linear DMA Position) of the stream
  966. * @sdev: SOF device
  967. * @component: ASoC component
  968. * @substream: PCM substream
  969. *
  970. * Returns the raw Linear Link Position value
  971. */
  972. u64 hda_dsp_get_stream_ldp(struct snd_sof_dev *sdev,
  973. struct snd_soc_component *component,
  974. struct snd_pcm_substream *substream)
  975. {
  976. struct hdac_stream *hstream = substream->runtime->private_data;
  977. struct hdac_ext_stream *hext_stream = stream_to_hdac_ext_stream(hstream);
  978. u32 ldp_l, ldp_u;
  979. /*
  980. * The pphc_addr have been calculated during probe in
  981. * hda_dsp_stream_init():
  982. * pphc_addr = sdev->bar[HDA_DSP_PP_BAR] +
  983. * SOF_HDA_PPHC_BASE +
  984. * SOF_HDA_PPHC_INTERVAL * stream_index
  985. *
  986. * Use this pre-calculated address to avoid repeated re-calculation.
  987. */
  988. ldp_l = readl(hext_stream->pphc_addr + AZX_REG_PPHCLDPL);
  989. ldp_u = readl(hext_stream->pphc_addr + AZX_REG_PPHCLDPU);
  990. return ((u64)ldp_u << 32) | ldp_l;
  991. }
  992. EXPORT_SYMBOL_NS(hda_dsp_get_stream_ldp, SND_SOC_SOF_INTEL_HDA_COMMON);