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