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