hdac_stream.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * HD-audio stream operations
  4. */
  5. #include <linux/kernel.h>
  6. #include <linux/delay.h>
  7. #include <linux/export.h>
  8. #include <linux/clocksource.h>
  9. #include <sound/compress_driver.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. * the hdac_stream library is intended to be used with the following
  17. * transitions. The states are not formally defined in the code but loosely
  18. * inspired by boolean variables. Note that the 'prepared' field is not used
  19. * in this library but by the callers during the hw_params/prepare transitions
  20. *
  21. * |
  22. * stream_init() |
  23. * v
  24. * +--+-------+
  25. * | unused |
  26. * +--+----+--+
  27. * | ^
  28. * stream_assign() | | stream_release()
  29. * v |
  30. * +--+----+--+
  31. * | opened |
  32. * +--+----+--+
  33. * | ^
  34. * stream_reset() | |
  35. * stream_setup() | | stream_cleanup()
  36. * v |
  37. * +--+----+--+
  38. * | prepared |
  39. * +--+----+--+
  40. * | ^
  41. * stream_start() | | stream_stop()
  42. * v |
  43. * +--+----+--+
  44. * | running |
  45. * +----------+
  46. */
  47. /**
  48. * snd_hdac_get_stream_stripe_ctl - get stripe control value
  49. * @bus: HD-audio core bus
  50. * @substream: PCM substream
  51. */
  52. int snd_hdac_get_stream_stripe_ctl(struct hdac_bus *bus,
  53. struct snd_pcm_substream *substream)
  54. {
  55. struct snd_pcm_runtime *runtime = substream->runtime;
  56. unsigned int channels = runtime->channels,
  57. rate = runtime->rate,
  58. bits_per_sample = runtime->sample_bits,
  59. max_sdo_lines, value, sdo_line;
  60. /* T_AZA_GCAP_NSDO is 1:2 bitfields in GCAP */
  61. max_sdo_lines = snd_hdac_chip_readl(bus, GCAP) & AZX_GCAP_NSDO;
  62. /* following is from HD audio spec */
  63. for (sdo_line = max_sdo_lines; sdo_line > 0; sdo_line >>= 1) {
  64. if (rate > 48000)
  65. value = (channels * bits_per_sample *
  66. (rate / 48000)) / sdo_line;
  67. else
  68. value = (channels * bits_per_sample) / sdo_line;
  69. if (value >= bus->sdo_limit)
  70. break;
  71. }
  72. /* stripe value: 0 for 1SDO, 1 for 2SDO, 2 for 4SDO lines */
  73. return sdo_line >> 1;
  74. }
  75. EXPORT_SYMBOL_GPL(snd_hdac_get_stream_stripe_ctl);
  76. /**
  77. * snd_hdac_stream_init - initialize each stream (aka device)
  78. * @bus: HD-audio core bus
  79. * @azx_dev: HD-audio core stream object to initialize
  80. * @idx: stream index number
  81. * @direction: stream direction (SNDRV_PCM_STREAM_PLAYBACK or SNDRV_PCM_STREAM_CAPTURE)
  82. * @tag: the tag id to assign
  83. *
  84. * Assign the starting bdl address to each stream (device) and initialize.
  85. */
  86. void snd_hdac_stream_init(struct hdac_bus *bus, struct hdac_stream *azx_dev,
  87. int idx, int direction, int tag)
  88. {
  89. azx_dev->bus = bus;
  90. /* offset: SDI0=0x80, SDI1=0xa0, ... SDO3=0x160 */
  91. azx_dev->sd_addr = bus->remap_addr + (0x20 * idx + 0x80);
  92. /* int mask: SDI0=0x01, SDI1=0x02, ... SDO3=0x80 */
  93. azx_dev->sd_int_sta_mask = 1 << idx;
  94. azx_dev->index = idx;
  95. azx_dev->direction = direction;
  96. azx_dev->stream_tag = tag;
  97. snd_hdac_dsp_lock_init(azx_dev);
  98. list_add_tail(&azx_dev->list, &bus->stream_list);
  99. if (bus->spbcap) {
  100. azx_dev->spib_addr = bus->spbcap + AZX_SPB_BASE +
  101. AZX_SPB_INTERVAL * idx +
  102. AZX_SPB_SPIB;
  103. azx_dev->fifo_addr = bus->spbcap + AZX_SPB_BASE +
  104. AZX_SPB_INTERVAL * idx +
  105. AZX_SPB_MAXFIFO;
  106. }
  107. if (bus->drsmcap)
  108. azx_dev->dpibr_addr = bus->drsmcap + AZX_DRSM_BASE +
  109. AZX_DRSM_INTERVAL * idx;
  110. }
  111. EXPORT_SYMBOL_GPL(snd_hdac_stream_init);
  112. /**
  113. * snd_hdac_stream_start - start a stream
  114. * @azx_dev: HD-audio core stream to start
  115. *
  116. * Start a stream, set start_wallclk and set the running flag.
  117. */
  118. void snd_hdac_stream_start(struct hdac_stream *azx_dev)
  119. {
  120. struct hdac_bus *bus = azx_dev->bus;
  121. int stripe_ctl;
  122. trace_snd_hdac_stream_start(bus, azx_dev);
  123. azx_dev->start_wallclk = snd_hdac_chip_readl(bus, WALLCLK);
  124. /* enable SIE */
  125. snd_hdac_chip_updatel(bus, INTCTL,
  126. 1 << azx_dev->index,
  127. 1 << azx_dev->index);
  128. /* set stripe control */
  129. if (azx_dev->stripe) {
  130. if (azx_dev->substream)
  131. stripe_ctl = snd_hdac_get_stream_stripe_ctl(bus, azx_dev->substream);
  132. else
  133. stripe_ctl = 0;
  134. snd_hdac_stream_updateb(azx_dev, SD_CTL_3B, SD_CTL_STRIPE_MASK,
  135. stripe_ctl);
  136. }
  137. /* set DMA start and interrupt mask */
  138. if (bus->access_sdnctl_in_dword)
  139. snd_hdac_stream_updatel(azx_dev, SD_CTL,
  140. 0, SD_CTL_DMA_START | SD_INT_MASK);
  141. else
  142. snd_hdac_stream_updateb(azx_dev, SD_CTL,
  143. 0, SD_CTL_DMA_START | SD_INT_MASK);
  144. azx_dev->running = true;
  145. }
  146. EXPORT_SYMBOL_GPL(snd_hdac_stream_start);
  147. /**
  148. * snd_hdac_stream_clear - helper to clear stream registers and stop DMA transfers
  149. * @azx_dev: HD-audio core stream to stop
  150. */
  151. static void snd_hdac_stream_clear(struct hdac_stream *azx_dev)
  152. {
  153. snd_hdac_stream_updateb(azx_dev, SD_CTL,
  154. SD_CTL_DMA_START | SD_INT_MASK, 0);
  155. snd_hdac_stream_writeb(azx_dev, SD_STS, SD_INT_MASK); /* to be sure */
  156. if (azx_dev->stripe)
  157. snd_hdac_stream_updateb(azx_dev, SD_CTL_3B, SD_CTL_STRIPE_MASK, 0);
  158. azx_dev->running = false;
  159. }
  160. /**
  161. * snd_hdac_stream_stop - stop a stream
  162. * @azx_dev: HD-audio core stream to stop
  163. *
  164. * Stop a stream DMA and disable stream interrupt
  165. */
  166. void snd_hdac_stream_stop(struct hdac_stream *azx_dev)
  167. {
  168. trace_snd_hdac_stream_stop(azx_dev->bus, azx_dev);
  169. snd_hdac_stream_clear(azx_dev);
  170. /* disable SIE */
  171. snd_hdac_chip_updatel(azx_dev->bus, INTCTL, 1 << azx_dev->index, 0);
  172. }
  173. EXPORT_SYMBOL_GPL(snd_hdac_stream_stop);
  174. /**
  175. * snd_hdac_stop_streams - stop all streams
  176. * @bus: HD-audio core bus
  177. */
  178. void snd_hdac_stop_streams(struct hdac_bus *bus)
  179. {
  180. struct hdac_stream *stream;
  181. list_for_each_entry(stream, &bus->stream_list, list)
  182. snd_hdac_stream_stop(stream);
  183. }
  184. EXPORT_SYMBOL_GPL(snd_hdac_stop_streams);
  185. /**
  186. * snd_hdac_stop_streams_and_chip - stop all streams and chip if running
  187. * @bus: HD-audio core bus
  188. */
  189. void snd_hdac_stop_streams_and_chip(struct hdac_bus *bus)
  190. {
  191. if (bus->chip_init) {
  192. snd_hdac_stop_streams(bus);
  193. snd_hdac_bus_stop_chip(bus);
  194. }
  195. }
  196. EXPORT_SYMBOL_GPL(snd_hdac_stop_streams_and_chip);
  197. /**
  198. * snd_hdac_stream_reset - reset a stream
  199. * @azx_dev: HD-audio core stream to reset
  200. */
  201. void snd_hdac_stream_reset(struct hdac_stream *azx_dev)
  202. {
  203. unsigned char val;
  204. int dma_run_state;
  205. snd_hdac_stream_clear(azx_dev);
  206. dma_run_state = snd_hdac_stream_readb(azx_dev, SD_CTL) & SD_CTL_DMA_START;
  207. snd_hdac_stream_updateb(azx_dev, SD_CTL, 0, SD_CTL_STREAM_RESET);
  208. /* wait for hardware to report that the stream entered reset */
  209. snd_hdac_stream_readb_poll(azx_dev, SD_CTL, val, (val & SD_CTL_STREAM_RESET), 3, 300);
  210. if (azx_dev->bus->dma_stop_delay && dma_run_state)
  211. udelay(azx_dev->bus->dma_stop_delay);
  212. snd_hdac_stream_updateb(azx_dev, SD_CTL, SD_CTL_STREAM_RESET, 0);
  213. /* wait for hardware to report that the stream is out of reset */
  214. snd_hdac_stream_readb_poll(azx_dev, SD_CTL, val, !(val & SD_CTL_STREAM_RESET), 3, 300);
  215. /* reset first position - may not be synced with hw at this time */
  216. if (azx_dev->posbuf)
  217. *azx_dev->posbuf = 0;
  218. }
  219. EXPORT_SYMBOL_GPL(snd_hdac_stream_reset);
  220. /**
  221. * snd_hdac_stream_setup - set up the SD for streaming
  222. * @azx_dev: HD-audio core stream to set up
  223. * @code_loading: Whether the stream is for PCM or code-loading.
  224. */
  225. int snd_hdac_stream_setup(struct hdac_stream *azx_dev, bool code_loading)
  226. {
  227. struct hdac_bus *bus = azx_dev->bus;
  228. struct snd_pcm_runtime *runtime;
  229. unsigned int val;
  230. u16 reg;
  231. int ret;
  232. if (azx_dev->substream)
  233. runtime = azx_dev->substream->runtime;
  234. else
  235. runtime = NULL;
  236. /* make sure the run bit is zero for SD */
  237. snd_hdac_stream_clear(azx_dev);
  238. /* program the stream_tag */
  239. val = snd_hdac_stream_readl(azx_dev, SD_CTL);
  240. val = (val & ~SD_CTL_STREAM_TAG_MASK) |
  241. (azx_dev->stream_tag << SD_CTL_STREAM_TAG_SHIFT);
  242. if (!bus->snoop)
  243. val |= SD_CTL_TRAFFIC_PRIO;
  244. snd_hdac_stream_writel(azx_dev, SD_CTL, val);
  245. /* program the length of samples in cyclic buffer */
  246. snd_hdac_stream_writel(azx_dev, SD_CBL, azx_dev->bufsize);
  247. /* program the stream format */
  248. /* this value needs to be the same as the one programmed */
  249. snd_hdac_stream_writew(azx_dev, SD_FORMAT, azx_dev->format_val);
  250. /* program the stream LVI (last valid index) of the BDL */
  251. snd_hdac_stream_writew(azx_dev, SD_LVI, azx_dev->frags - 1);
  252. /* program the BDL address */
  253. /* lower BDL address */
  254. snd_hdac_stream_writel(azx_dev, SD_BDLPL, (u32)azx_dev->bdl.addr);
  255. /* upper BDL address */
  256. snd_hdac_stream_writel(azx_dev, SD_BDLPU,
  257. upper_32_bits(azx_dev->bdl.addr));
  258. /* enable the position buffer */
  259. if (bus->use_posbuf && bus->posbuf.addr) {
  260. if (!(snd_hdac_chip_readl(bus, DPLBASE) & AZX_DPLBASE_ENABLE))
  261. snd_hdac_chip_writel(bus, DPLBASE,
  262. (u32)bus->posbuf.addr | AZX_DPLBASE_ENABLE);
  263. }
  264. /* set the interrupt enable bits in the descriptor control register */
  265. snd_hdac_stream_updatel(azx_dev, SD_CTL, 0, SD_INT_MASK);
  266. if (!code_loading) {
  267. /* Once SDxFMT is set, the controller programs SDxFIFOS to non-zero value. */
  268. ret = snd_hdac_stream_readw_poll(azx_dev, SD_FIFOSIZE, reg,
  269. reg & AZX_SD_FIFOSIZE_MASK, 3, 300);
  270. if (ret)
  271. dev_dbg(bus->dev, "polling SD_FIFOSIZE 0x%04x failed: %d\n",
  272. AZX_REG_SD_FIFOSIZE, ret);
  273. azx_dev->fifo_size = reg;
  274. }
  275. /* when LPIB delay correction gives a small negative value,
  276. * we ignore it; currently set the threshold statically to
  277. * 64 frames
  278. */
  279. if (runtime && runtime->period_size > 64)
  280. azx_dev->delay_negative_threshold =
  281. -frames_to_bytes(runtime, 64);
  282. else
  283. azx_dev->delay_negative_threshold = 0;
  284. /* wallclk has 24Mhz clock source */
  285. if (runtime)
  286. azx_dev->period_wallclk = (((runtime->period_size * 24000) /
  287. runtime->rate) * 1000);
  288. return 0;
  289. }
  290. EXPORT_SYMBOL_GPL(snd_hdac_stream_setup);
  291. /**
  292. * snd_hdac_stream_cleanup - cleanup a stream
  293. * @azx_dev: HD-audio core stream to clean up
  294. */
  295. void snd_hdac_stream_cleanup(struct hdac_stream *azx_dev)
  296. {
  297. snd_hdac_stream_writel(azx_dev, SD_BDLPL, 0);
  298. snd_hdac_stream_writel(azx_dev, SD_BDLPU, 0);
  299. snd_hdac_stream_writel(azx_dev, SD_CTL, 0);
  300. azx_dev->bufsize = 0;
  301. azx_dev->period_bytes = 0;
  302. azx_dev->format_val = 0;
  303. }
  304. EXPORT_SYMBOL_GPL(snd_hdac_stream_cleanup);
  305. /**
  306. * snd_hdac_stream_assign - assign a stream for the PCM
  307. * @bus: HD-audio core bus
  308. * @substream: PCM substream to assign
  309. *
  310. * Look for an unused stream for the given PCM substream, assign it
  311. * and return the stream object. If no stream is free, returns NULL.
  312. * The function tries to keep using the same stream object when it's used
  313. * beforehand. Also, when bus->reverse_assign flag is set, the last free
  314. * or matching entry is returned. This is needed for some strange codecs.
  315. */
  316. struct hdac_stream *snd_hdac_stream_assign(struct hdac_bus *bus,
  317. struct snd_pcm_substream *substream)
  318. {
  319. struct hdac_stream *azx_dev;
  320. struct hdac_stream *res = NULL;
  321. /* make a non-zero unique key for the substream */
  322. int key = (substream->number << 2) | (substream->stream + 1);
  323. if (substream->pcm)
  324. key |= (substream->pcm->device << 16);
  325. spin_lock_irq(&bus->reg_lock);
  326. list_for_each_entry(azx_dev, &bus->stream_list, list) {
  327. if (azx_dev->direction != substream->stream)
  328. continue;
  329. if (azx_dev->opened)
  330. continue;
  331. if (azx_dev->assigned_key == key) {
  332. res = azx_dev;
  333. break;
  334. }
  335. if (!res || bus->reverse_assign)
  336. res = azx_dev;
  337. }
  338. if (res) {
  339. res->opened = 1;
  340. res->running = 0;
  341. res->assigned_key = key;
  342. res->substream = substream;
  343. }
  344. spin_unlock_irq(&bus->reg_lock);
  345. return res;
  346. }
  347. EXPORT_SYMBOL_GPL(snd_hdac_stream_assign);
  348. /**
  349. * snd_hdac_stream_release_locked - release the assigned stream
  350. * @azx_dev: HD-audio core stream to release
  351. *
  352. * Release the stream that has been assigned by snd_hdac_stream_assign().
  353. * The bus->reg_lock needs to be taken at a higher level
  354. */
  355. void snd_hdac_stream_release_locked(struct hdac_stream *azx_dev)
  356. {
  357. azx_dev->opened = 0;
  358. azx_dev->running = 0;
  359. azx_dev->substream = NULL;
  360. }
  361. EXPORT_SYMBOL_GPL(snd_hdac_stream_release_locked);
  362. /**
  363. * snd_hdac_stream_release - release the assigned stream
  364. * @azx_dev: HD-audio core stream to release
  365. *
  366. * Release the stream that has been assigned by snd_hdac_stream_assign().
  367. */
  368. void snd_hdac_stream_release(struct hdac_stream *azx_dev)
  369. {
  370. struct hdac_bus *bus = azx_dev->bus;
  371. spin_lock_irq(&bus->reg_lock);
  372. snd_hdac_stream_release_locked(azx_dev);
  373. spin_unlock_irq(&bus->reg_lock);
  374. }
  375. EXPORT_SYMBOL_GPL(snd_hdac_stream_release);
  376. /**
  377. * snd_hdac_get_stream - return hdac_stream based on stream_tag and
  378. * direction
  379. *
  380. * @bus: HD-audio core bus
  381. * @dir: direction for the stream to be found
  382. * @stream_tag: stream tag for stream to be found
  383. */
  384. struct hdac_stream *snd_hdac_get_stream(struct hdac_bus *bus,
  385. int dir, int stream_tag)
  386. {
  387. struct hdac_stream *s;
  388. list_for_each_entry(s, &bus->stream_list, list) {
  389. if (s->direction == dir && s->stream_tag == stream_tag)
  390. return s;
  391. }
  392. return NULL;
  393. }
  394. EXPORT_SYMBOL_GPL(snd_hdac_get_stream);
  395. /*
  396. * set up a BDL entry
  397. */
  398. static int setup_bdle(struct hdac_bus *bus,
  399. struct snd_dma_buffer *dmab,
  400. struct hdac_stream *azx_dev, __le32 **bdlp,
  401. int ofs, int size, int with_ioc)
  402. {
  403. __le32 *bdl = *bdlp;
  404. while (size > 0) {
  405. dma_addr_t addr;
  406. int chunk;
  407. if (azx_dev->frags >= AZX_MAX_BDL_ENTRIES)
  408. return -EINVAL;
  409. addr = snd_sgbuf_get_addr(dmab, ofs);
  410. /* program the address field of the BDL entry */
  411. bdl[0] = cpu_to_le32((u32)addr);
  412. bdl[1] = cpu_to_le32(upper_32_bits(addr));
  413. /* program the size field of the BDL entry */
  414. chunk = snd_sgbuf_get_chunk_size(dmab, ofs, size);
  415. /* one BDLE cannot cross 4K boundary on CTHDA chips */
  416. if (bus->align_bdle_4k) {
  417. u32 remain = 0x1000 - (ofs & 0xfff);
  418. if (chunk > remain)
  419. chunk = remain;
  420. }
  421. bdl[2] = cpu_to_le32(chunk);
  422. /* program the IOC to enable interrupt
  423. * only when the whole fragment is processed
  424. */
  425. size -= chunk;
  426. bdl[3] = (size || !with_ioc) ? 0 : cpu_to_le32(0x01);
  427. bdl += 4;
  428. azx_dev->frags++;
  429. ofs += chunk;
  430. }
  431. *bdlp = bdl;
  432. return ofs;
  433. }
  434. /**
  435. * snd_hdac_stream_setup_periods - set up BDL entries
  436. * @azx_dev: HD-audio core stream to set up
  437. *
  438. * Set up the buffer descriptor table of the given stream based on the
  439. * period and buffer sizes of the assigned PCM substream.
  440. */
  441. int snd_hdac_stream_setup_periods(struct hdac_stream *azx_dev)
  442. {
  443. struct hdac_bus *bus = azx_dev->bus;
  444. struct snd_pcm_substream *substream = azx_dev->substream;
  445. struct snd_compr_stream *cstream = azx_dev->cstream;
  446. struct snd_pcm_runtime *runtime = NULL;
  447. struct snd_dma_buffer *dmab;
  448. __le32 *bdl;
  449. int i, ofs, periods, period_bytes;
  450. int pos_adj, pos_align;
  451. if (substream) {
  452. runtime = substream->runtime;
  453. dmab = snd_pcm_get_dma_buf(substream);
  454. } else if (cstream) {
  455. dmab = snd_pcm_get_dma_buf(cstream);
  456. } else {
  457. WARN(1, "No substream or cstream assigned\n");
  458. return -EINVAL;
  459. }
  460. /* reset BDL address */
  461. snd_hdac_stream_writel(azx_dev, SD_BDLPL, 0);
  462. snd_hdac_stream_writel(azx_dev, SD_BDLPU, 0);
  463. period_bytes = azx_dev->period_bytes;
  464. periods = azx_dev->bufsize / period_bytes;
  465. /* program the initial BDL entries */
  466. bdl = (__le32 *)azx_dev->bdl.area;
  467. ofs = 0;
  468. azx_dev->frags = 0;
  469. pos_adj = bus->bdl_pos_adj;
  470. if (runtime && !azx_dev->no_period_wakeup && pos_adj > 0) {
  471. pos_align = pos_adj;
  472. pos_adj = DIV_ROUND_UP(pos_adj * runtime->rate, 48000);
  473. if (!pos_adj)
  474. pos_adj = pos_align;
  475. else
  476. pos_adj = roundup(pos_adj, pos_align);
  477. pos_adj = frames_to_bytes(runtime, pos_adj);
  478. if (pos_adj >= period_bytes) {
  479. dev_warn(bus->dev, "Too big adjustment %d\n",
  480. pos_adj);
  481. pos_adj = 0;
  482. } else {
  483. ofs = setup_bdle(bus, dmab, azx_dev,
  484. &bdl, ofs, pos_adj, true);
  485. if (ofs < 0)
  486. goto error;
  487. }
  488. } else
  489. pos_adj = 0;
  490. for (i = 0; i < periods; i++) {
  491. if (i == periods - 1 && pos_adj)
  492. ofs = setup_bdle(bus, dmab, azx_dev,
  493. &bdl, ofs, period_bytes - pos_adj, 0);
  494. else
  495. ofs = setup_bdle(bus, dmab, azx_dev,
  496. &bdl, ofs, period_bytes,
  497. !azx_dev->no_period_wakeup);
  498. if (ofs < 0)
  499. goto error;
  500. }
  501. return 0;
  502. error:
  503. dev_dbg(bus->dev, "Too many BDL entries: buffer=%d, period=%d\n",
  504. azx_dev->bufsize, period_bytes);
  505. return -EINVAL;
  506. }
  507. EXPORT_SYMBOL_GPL(snd_hdac_stream_setup_periods);
  508. /**
  509. * snd_hdac_stream_set_params - set stream parameters
  510. * @azx_dev: HD-audio core stream for which parameters are to be set
  511. * @format_val: format value parameter
  512. *
  513. * Setup the HD-audio core stream parameters from substream of the stream
  514. * and passed format value
  515. */
  516. int snd_hdac_stream_set_params(struct hdac_stream *azx_dev,
  517. unsigned int format_val)
  518. {
  519. struct snd_pcm_substream *substream = azx_dev->substream;
  520. struct snd_compr_stream *cstream = azx_dev->cstream;
  521. unsigned int bufsize, period_bytes;
  522. unsigned int no_period_wakeup;
  523. int err;
  524. if (substream) {
  525. bufsize = snd_pcm_lib_buffer_bytes(substream);
  526. period_bytes = snd_pcm_lib_period_bytes(substream);
  527. no_period_wakeup = substream->runtime->no_period_wakeup;
  528. } else if (cstream) {
  529. bufsize = cstream->runtime->buffer_size;
  530. period_bytes = cstream->runtime->fragment_size;
  531. no_period_wakeup = 0;
  532. } else {
  533. return -EINVAL;
  534. }
  535. if (bufsize != azx_dev->bufsize ||
  536. period_bytes != azx_dev->period_bytes ||
  537. format_val != azx_dev->format_val ||
  538. no_period_wakeup != azx_dev->no_period_wakeup) {
  539. azx_dev->bufsize = bufsize;
  540. azx_dev->period_bytes = period_bytes;
  541. azx_dev->format_val = format_val;
  542. azx_dev->no_period_wakeup = no_period_wakeup;
  543. err = snd_hdac_stream_setup_periods(azx_dev);
  544. if (err < 0)
  545. return err;
  546. }
  547. return 0;
  548. }
  549. EXPORT_SYMBOL_GPL(snd_hdac_stream_set_params);
  550. static u64 azx_cc_read(const struct cyclecounter *cc)
  551. {
  552. struct hdac_stream *azx_dev = container_of(cc, struct hdac_stream, cc);
  553. return snd_hdac_chip_readl(azx_dev->bus, WALLCLK);
  554. }
  555. static void azx_timecounter_init(struct hdac_stream *azx_dev,
  556. bool force, u64 last)
  557. {
  558. struct timecounter *tc = &azx_dev->tc;
  559. struct cyclecounter *cc = &azx_dev->cc;
  560. u64 nsec;
  561. cc->read = azx_cc_read;
  562. cc->mask = CLOCKSOURCE_MASK(32);
  563. /*
  564. * Calculate the optimal mult/shift values. The counter wraps
  565. * around after ~178.9 seconds.
  566. */
  567. clocks_calc_mult_shift(&cc->mult, &cc->shift, 24000000,
  568. NSEC_PER_SEC, 178);
  569. nsec = 0; /* audio time is elapsed time since trigger */
  570. timecounter_init(tc, cc, nsec);
  571. if (force) {
  572. /*
  573. * force timecounter to use predefined value,
  574. * used for synchronized starts
  575. */
  576. tc->cycle_last = last;
  577. }
  578. }
  579. /**
  580. * snd_hdac_stream_timecounter_init - initialize time counter
  581. * @azx_dev: HD-audio core stream (master stream)
  582. * @streams: bit flags of streams to set up
  583. * @start: true for PCM trigger start, false for other cases
  584. *
  585. * Initializes the time counter of streams marked by the bit flags (each
  586. * bit corresponds to the stream index).
  587. * The trigger timestamp of PCM substream assigned to the given stream is
  588. * updated accordingly, too.
  589. */
  590. void snd_hdac_stream_timecounter_init(struct hdac_stream *azx_dev,
  591. unsigned int streams, bool start)
  592. {
  593. struct hdac_bus *bus = azx_dev->bus;
  594. struct snd_pcm_runtime *runtime = azx_dev->substream->runtime;
  595. struct hdac_stream *s;
  596. bool inited = false;
  597. u64 cycle_last = 0;
  598. if (!start)
  599. goto skip;
  600. list_for_each_entry(s, &bus->stream_list, list) {
  601. if ((streams & (1 << s->index))) {
  602. azx_timecounter_init(s, inited, cycle_last);
  603. if (!inited) {
  604. inited = true;
  605. cycle_last = s->tc.cycle_last;
  606. }
  607. }
  608. }
  609. skip:
  610. snd_pcm_gettime(runtime, &runtime->trigger_tstamp);
  611. runtime->trigger_tstamp_latched = true;
  612. }
  613. EXPORT_SYMBOL_GPL(snd_hdac_stream_timecounter_init);
  614. /**
  615. * snd_hdac_stream_sync_trigger - turn on/off stream sync register
  616. * @azx_dev: HD-audio core stream (master stream)
  617. * @set: true = set, false = clear
  618. * @streams: bit flags of streams to sync
  619. * @reg: the stream sync register address
  620. */
  621. void snd_hdac_stream_sync_trigger(struct hdac_stream *azx_dev, bool set,
  622. unsigned int streams, unsigned int reg)
  623. {
  624. struct hdac_bus *bus = azx_dev->bus;
  625. unsigned int val;
  626. if (!reg)
  627. reg = AZX_REG_SSYNC;
  628. val = _snd_hdac_chip_readl(bus, reg);
  629. if (set)
  630. val |= streams;
  631. else
  632. val &= ~streams;
  633. _snd_hdac_chip_writel(bus, reg, val);
  634. }
  635. EXPORT_SYMBOL_GPL(snd_hdac_stream_sync_trigger);
  636. /**
  637. * snd_hdac_stream_sync - sync with start/stop trigger operation
  638. * @azx_dev: HD-audio core stream (master stream)
  639. * @start: true = start, false = stop
  640. * @streams: bit flags of streams to sync
  641. *
  642. * For @start = true, wait until all FIFOs get ready.
  643. * For @start = false, wait until all RUN bits are cleared.
  644. */
  645. void snd_hdac_stream_sync(struct hdac_stream *azx_dev, bool start,
  646. unsigned int streams)
  647. {
  648. struct hdac_bus *bus = azx_dev->bus;
  649. int nwait, timeout;
  650. struct hdac_stream *s;
  651. for (timeout = 5000; timeout; timeout--) {
  652. nwait = 0;
  653. list_for_each_entry(s, &bus->stream_list, list) {
  654. if (!(streams & (1 << s->index)))
  655. continue;
  656. if (start) {
  657. /* check FIFO gets ready */
  658. if (!(snd_hdac_stream_readb(s, SD_STS) &
  659. SD_STS_FIFO_READY))
  660. nwait++;
  661. } else {
  662. /* check RUN bit is cleared */
  663. if (snd_hdac_stream_readb(s, SD_CTL) &
  664. SD_CTL_DMA_START) {
  665. nwait++;
  666. /*
  667. * Perform stream reset if DMA RUN
  668. * bit not cleared within given timeout
  669. */
  670. if (timeout == 1)
  671. snd_hdac_stream_reset(s);
  672. }
  673. }
  674. }
  675. if (!nwait)
  676. break;
  677. cpu_relax();
  678. }
  679. }
  680. EXPORT_SYMBOL_GPL(snd_hdac_stream_sync);
  681. /**
  682. * snd_hdac_stream_spbcap_enable - enable SPIB for a stream
  683. * @bus: HD-audio core bus
  684. * @enable: flag to enable/disable SPIB
  685. * @index: stream index for which SPIB need to be enabled
  686. */
  687. void snd_hdac_stream_spbcap_enable(struct hdac_bus *bus,
  688. bool enable, int index)
  689. {
  690. u32 mask = 0;
  691. if (!bus->spbcap) {
  692. dev_err(bus->dev, "Address of SPB capability is NULL\n");
  693. return;
  694. }
  695. mask |= (1 << index);
  696. if (enable)
  697. snd_hdac_updatel(bus->spbcap, AZX_REG_SPB_SPBFCCTL, mask, mask);
  698. else
  699. snd_hdac_updatel(bus->spbcap, AZX_REG_SPB_SPBFCCTL, mask, 0);
  700. }
  701. EXPORT_SYMBOL_GPL(snd_hdac_stream_spbcap_enable);
  702. /**
  703. * snd_hdac_stream_set_spib - sets the spib value of a stream
  704. * @bus: HD-audio core bus
  705. * @azx_dev: hdac_stream
  706. * @value: spib value to set
  707. */
  708. int snd_hdac_stream_set_spib(struct hdac_bus *bus,
  709. struct hdac_stream *azx_dev, u32 value)
  710. {
  711. if (!bus->spbcap) {
  712. dev_err(bus->dev, "Address of SPB capability is NULL\n");
  713. return -EINVAL;
  714. }
  715. writel(value, azx_dev->spib_addr);
  716. return 0;
  717. }
  718. EXPORT_SYMBOL_GPL(snd_hdac_stream_set_spib);
  719. /**
  720. * snd_hdac_stream_get_spbmaxfifo - gets the spib value of a stream
  721. * @bus: HD-audio core bus
  722. * @azx_dev: hdac_stream
  723. *
  724. * Return maxfifo for the stream
  725. */
  726. int snd_hdac_stream_get_spbmaxfifo(struct hdac_bus *bus,
  727. struct hdac_stream *azx_dev)
  728. {
  729. if (!bus->spbcap) {
  730. dev_err(bus->dev, "Address of SPB capability is NULL\n");
  731. return -EINVAL;
  732. }
  733. return readl(azx_dev->fifo_addr);
  734. }
  735. EXPORT_SYMBOL_GPL(snd_hdac_stream_get_spbmaxfifo);
  736. /**
  737. * snd_hdac_stream_drsm_enable - enable DMA resume for a stream
  738. * @bus: HD-audio core bus
  739. * @enable: flag to enable/disable DRSM
  740. * @index: stream index for which DRSM need to be enabled
  741. */
  742. void snd_hdac_stream_drsm_enable(struct hdac_bus *bus,
  743. bool enable, int index)
  744. {
  745. u32 mask = 0;
  746. if (!bus->drsmcap) {
  747. dev_err(bus->dev, "Address of DRSM capability is NULL\n");
  748. return;
  749. }
  750. mask |= (1 << index);
  751. if (enable)
  752. snd_hdac_updatel(bus->drsmcap, AZX_REG_DRSM_CTL, mask, mask);
  753. else
  754. snd_hdac_updatel(bus->drsmcap, AZX_REG_DRSM_CTL, mask, 0);
  755. }
  756. EXPORT_SYMBOL_GPL(snd_hdac_stream_drsm_enable);
  757. /*
  758. * snd_hdac_stream_wait_drsm - wait for HW to clear RSM for a stream
  759. * @azx_dev: HD-audio core stream to await RSM for
  760. *
  761. * Returns 0 on success and -ETIMEDOUT upon a timeout.
  762. */
  763. int snd_hdac_stream_wait_drsm(struct hdac_stream *azx_dev)
  764. {
  765. struct hdac_bus *bus = azx_dev->bus;
  766. u32 mask, reg;
  767. int ret;
  768. mask = 1 << azx_dev->index;
  769. ret = read_poll_timeout(snd_hdac_reg_readl, reg, !(reg & mask), 250, 2000, false, bus,
  770. bus->drsmcap + AZX_REG_DRSM_CTL);
  771. if (ret)
  772. dev_dbg(bus->dev, "polling RSM 0x%08x failed: %d\n", mask, ret);
  773. return ret;
  774. }
  775. EXPORT_SYMBOL_GPL(snd_hdac_stream_wait_drsm);
  776. /**
  777. * snd_hdac_stream_set_dpibr - sets the dpibr value of a stream
  778. * @bus: HD-audio core bus
  779. * @azx_dev: hdac_stream
  780. * @value: dpib value to set
  781. */
  782. int snd_hdac_stream_set_dpibr(struct hdac_bus *bus,
  783. struct hdac_stream *azx_dev, u32 value)
  784. {
  785. if (!bus->drsmcap) {
  786. dev_err(bus->dev, "Address of DRSM capability is NULL\n");
  787. return -EINVAL;
  788. }
  789. writel(value, azx_dev->dpibr_addr);
  790. return 0;
  791. }
  792. EXPORT_SYMBOL_GPL(snd_hdac_stream_set_dpibr);
  793. /**
  794. * snd_hdac_stream_set_lpib - sets the lpib value of a stream
  795. * @azx_dev: hdac_stream
  796. * @value: lpib value to set
  797. */
  798. int snd_hdac_stream_set_lpib(struct hdac_stream *azx_dev, u32 value)
  799. {
  800. snd_hdac_stream_writel(azx_dev, SD_LPIB, value);
  801. return 0;
  802. }
  803. EXPORT_SYMBOL_GPL(snd_hdac_stream_set_lpib);
  804. #ifdef CONFIG_SND_HDA_DSP_LOADER
  805. /**
  806. * snd_hdac_dsp_prepare - prepare for DSP loading
  807. * @azx_dev: HD-audio core stream used for DSP loading
  808. * @format: HD-audio stream format
  809. * @byte_size: data chunk byte size
  810. * @bufp: allocated buffer
  811. *
  812. * Allocate the buffer for the given size and set up the given stream for
  813. * DSP loading. Returns the stream tag (>= 0), or a negative error code.
  814. */
  815. int snd_hdac_dsp_prepare(struct hdac_stream *azx_dev, unsigned int format,
  816. unsigned int byte_size, struct snd_dma_buffer *bufp)
  817. {
  818. struct hdac_bus *bus = azx_dev->bus;
  819. __le32 *bdl;
  820. int err;
  821. snd_hdac_dsp_lock(azx_dev);
  822. spin_lock_irq(&bus->reg_lock);
  823. if (azx_dev->running || azx_dev->locked) {
  824. spin_unlock_irq(&bus->reg_lock);
  825. err = -EBUSY;
  826. goto unlock;
  827. }
  828. azx_dev->locked = true;
  829. spin_unlock_irq(&bus->reg_lock);
  830. err = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV_SG, bus->dev,
  831. byte_size, bufp);
  832. if (err < 0)
  833. goto err_alloc;
  834. azx_dev->substream = NULL;
  835. azx_dev->bufsize = byte_size;
  836. azx_dev->period_bytes = byte_size;
  837. azx_dev->format_val = format;
  838. snd_hdac_stream_reset(azx_dev);
  839. /* reset BDL address */
  840. snd_hdac_stream_writel(azx_dev, SD_BDLPL, 0);
  841. snd_hdac_stream_writel(azx_dev, SD_BDLPU, 0);
  842. azx_dev->frags = 0;
  843. bdl = (__le32 *)azx_dev->bdl.area;
  844. err = setup_bdle(bus, bufp, azx_dev, &bdl, 0, byte_size, 0);
  845. if (err < 0)
  846. goto error;
  847. snd_hdac_stream_setup(azx_dev, true);
  848. snd_hdac_dsp_unlock(azx_dev);
  849. return azx_dev->stream_tag;
  850. error:
  851. snd_dma_free_pages(bufp);
  852. err_alloc:
  853. spin_lock_irq(&bus->reg_lock);
  854. azx_dev->locked = false;
  855. spin_unlock_irq(&bus->reg_lock);
  856. unlock:
  857. snd_hdac_dsp_unlock(azx_dev);
  858. return err;
  859. }
  860. EXPORT_SYMBOL_GPL(snd_hdac_dsp_prepare);
  861. /**
  862. * snd_hdac_dsp_trigger - start / stop DSP loading
  863. * @azx_dev: HD-audio core stream used for DSP loading
  864. * @start: trigger start or stop
  865. */
  866. void snd_hdac_dsp_trigger(struct hdac_stream *azx_dev, bool start)
  867. {
  868. if (start)
  869. snd_hdac_stream_start(azx_dev);
  870. else
  871. snd_hdac_stream_stop(azx_dev);
  872. }
  873. EXPORT_SYMBOL_GPL(snd_hdac_dsp_trigger);
  874. /**
  875. * snd_hdac_dsp_cleanup - clean up the stream from DSP loading to normal
  876. * @azx_dev: HD-audio core stream used for DSP loading
  877. * @dmab: buffer used by DSP loading
  878. */
  879. void snd_hdac_dsp_cleanup(struct hdac_stream *azx_dev,
  880. struct snd_dma_buffer *dmab)
  881. {
  882. struct hdac_bus *bus = azx_dev->bus;
  883. if (!dmab->area || !azx_dev->locked)
  884. return;
  885. snd_hdac_dsp_lock(azx_dev);
  886. /* reset BDL address */
  887. snd_hdac_stream_writel(azx_dev, SD_BDLPL, 0);
  888. snd_hdac_stream_writel(azx_dev, SD_BDLPU, 0);
  889. snd_hdac_stream_writel(azx_dev, SD_CTL, 0);
  890. azx_dev->bufsize = 0;
  891. azx_dev->period_bytes = 0;
  892. azx_dev->format_val = 0;
  893. snd_dma_free_pages(dmab);
  894. dmab->area = NULL;
  895. spin_lock_irq(&bus->reg_lock);
  896. azx_dev->locked = false;
  897. spin_unlock_irq(&bus->reg_lock);
  898. snd_hdac_dsp_unlock(azx_dev);
  899. }
  900. EXPORT_SYMBOL_GPL(snd_hdac_dsp_cleanup);
  901. #endif /* CONFIG_SND_HDA_DSP_LOADER */