xref: /linux/sound/hda/core/controller.c (revision f65c917397c77daeb7e6d0893ebe89107ddd5692)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * HD-audio controller helpers
4  */
5 
6 #include <linux/kernel.h>
7 #include <linux/delay.h>
8 #include <linux/export.h>
9 #include <sound/core.h>
10 #include <sound/hdaudio.h>
11 #include <sound/hda_register.h>
12 #include "local.h"
13 
14 /* clear CORB read pointer properly */
15 static void azx_clear_corbrp(struct hdac_bus *bus)
16 {
17 	int timeout;
18 
19 	for (timeout = 1000; timeout > 0; timeout--) {
20 		if (snd_hdac_chip_readw(bus, CORBRP) & AZX_CORBRP_RST)
21 			break;
22 		udelay(1);
23 	}
24 	if (timeout <= 0)
25 		dev_err(bus->dev, "CORB reset timeout#1, CORBRP = %d\n",
26 			snd_hdac_chip_readw(bus, CORBRP));
27 
28 	snd_hdac_chip_writew(bus, CORBRP, 0);
29 	for (timeout = 1000; timeout > 0; timeout--) {
30 		if (snd_hdac_chip_readw(bus, CORBRP) == 0)
31 			break;
32 		udelay(1);
33 	}
34 	if (timeout <= 0)
35 		dev_err(bus->dev, "CORB reset timeout#2, CORBRP = %d\n",
36 			snd_hdac_chip_readw(bus, CORBRP));
37 }
38 
39 /**
40  * snd_hdac_bus_init_cmd_io - set up CORB/RIRB buffers
41  * @bus: HD-audio core bus
42  */
43 void snd_hdac_bus_init_cmd_io(struct hdac_bus *bus)
44 {
45 	WARN_ON_ONCE(!bus->rb.area);
46 
47 	guard(spinlock_irq)(&bus->reg_lock);
48 	/* CORB set up */
49 	bus->corb.addr = bus->rb.addr;
50 	bus->corb.buf = (__le32 *)bus->rb.area;
51 	snd_hdac_chip_writel(bus, CORBLBASE, (u32)(bus->corb.addr + bus->addr_offset));
52 	snd_hdac_chip_writel(bus, CORBUBASE, upper_32_bits(bus->corb.addr + bus->addr_offset));
53 
54 	/* set the corb size to 256 entries (ULI requires explicitly) */
55 	snd_hdac_chip_writeb(bus, CORBSIZE, 0x02);
56 	/* set the corb write pointer to 0 */
57 	snd_hdac_chip_writew(bus, CORBWP, 0);
58 
59 	/* reset the corb hw read pointer */
60 	snd_hdac_chip_writew(bus, CORBRP, AZX_CORBRP_RST);
61 	if (!bus->corbrp_self_clear)
62 		azx_clear_corbrp(bus);
63 
64 	/* enable corb dma */
65 	if (!bus->use_pio_for_commands)
66 		snd_hdac_chip_writeb(bus, CORBCTL, AZX_CORBCTL_RUN);
67 
68 	/* RIRB set up */
69 	bus->rirb.addr = bus->rb.addr + 2048;
70 	bus->rirb.buf = (__le32 *)(bus->rb.area + 2048);
71 	bus->rirb.wp = bus->rirb.rp = 0;
72 	memset(bus->rirb.cmds, 0, sizeof(bus->rirb.cmds));
73 	snd_hdac_chip_writel(bus, RIRBLBASE, (u32)(bus->rirb.addr + bus->addr_offset));
74 	snd_hdac_chip_writel(bus, RIRBUBASE, upper_32_bits(bus->rirb.addr + bus->addr_offset));
75 
76 	/* set the rirb size to 256 entries (ULI requires explicitly) */
77 	snd_hdac_chip_writeb(bus, RIRBSIZE, 0x02);
78 	/* reset the rirb hw write pointer */
79 	snd_hdac_chip_writew(bus, RIRBWP, AZX_RIRBWP_RST);
80 	/* set N=1, get RIRB response interrupt for new entry */
81 	snd_hdac_chip_writew(bus, RINTCNT, 1);
82 	/* enable rirb dma and response irq */
83 	if (bus->not_use_interrupts)
84 		snd_hdac_chip_writeb(bus, RIRBCTL, AZX_RBCTL_DMA_EN);
85 	else
86 		snd_hdac_chip_writeb(bus, RIRBCTL, AZX_RBCTL_DMA_EN | AZX_RBCTL_IRQ_EN);
87 	/* Accept unsolicited responses */
88 	snd_hdac_chip_updatel(bus, GCTL, AZX_GCTL_UNSOL, AZX_GCTL_UNSOL);
89 }
90 EXPORT_SYMBOL_GPL(snd_hdac_bus_init_cmd_io);
91 
92 /* wait for cmd dmas till they are stopped */
93 static void hdac_wait_for_cmd_dmas(struct hdac_bus *bus)
94 {
95 	unsigned long timeout;
96 
97 	timeout = jiffies + msecs_to_jiffies(100);
98 	while ((snd_hdac_chip_readb(bus, RIRBCTL) & AZX_RBCTL_DMA_EN)
99 		&& time_before(jiffies, timeout))
100 		udelay(10);
101 
102 	timeout = jiffies + msecs_to_jiffies(100);
103 	while ((snd_hdac_chip_readb(bus, CORBCTL) & AZX_CORBCTL_RUN)
104 		&& time_before(jiffies, timeout))
105 		udelay(10);
106 }
107 
108 /**
109  * snd_hdac_bus_stop_cmd_io - clean up CORB/RIRB buffers
110  * @bus: HD-audio core bus
111  */
112 void snd_hdac_bus_stop_cmd_io(struct hdac_bus *bus)
113 {
114 	scoped_guard(spinlock_irq, &bus->reg_lock) {
115 		/* disable ringbuffer DMAs */
116 		snd_hdac_chip_writeb(bus, RIRBCTL, 0);
117 		snd_hdac_chip_writeb(bus, CORBCTL, 0);
118 	}
119 
120 	hdac_wait_for_cmd_dmas(bus);
121 
122 	guard(spinlock_irq)(&bus->reg_lock);
123 	/* disable unsolicited responses */
124 	snd_hdac_chip_updatel(bus, GCTL, AZX_GCTL_UNSOL, 0);
125 }
126 EXPORT_SYMBOL_GPL(snd_hdac_bus_stop_cmd_io);
127 
128 static unsigned int azx_command_addr(u32 cmd)
129 {
130 	unsigned int addr = cmd >> 28;
131 
132 	if (snd_BUG_ON(addr >= HDA_MAX_CODECS))
133 		addr = 0;
134 	return addr;
135 }
136 
137 /* receive an Immediate Response with PIO */
138 static int snd_hdac_bus_wait_for_pio_response(struct hdac_bus *bus,
139 					      unsigned int addr)
140 {
141 	int timeout = 50;
142 
143 	while (timeout--) {
144 		/* check IRV bit */
145 		if (snd_hdac_chip_readw(bus, IRS) & AZX_IRS_VALID) {
146 			/* reuse rirb.res as the response return value */
147 			bus->rirb.res[addr] = snd_hdac_chip_readl(bus, IR);
148 			return 0;
149 		}
150 		udelay(1);
151 	}
152 
153 	dev_dbg_ratelimited(bus->dev, "get_response_pio timeout: IRS=%#x\n",
154 			    snd_hdac_chip_readw(bus, IRS));
155 
156 	bus->rirb.res[addr] = -1;
157 
158 	return -EIO;
159 }
160 
161 /**
162  * snd_hdac_bus_send_cmd_pio - send a command verb via Immediate Command
163  * @bus: HD-audio core bus
164  * @val: encoded verb value to send
165  *
166  * Returns zero for success or a negative error code.
167  */
168 static int snd_hdac_bus_send_cmd_pio(struct hdac_bus *bus, unsigned int val)
169 {
170 	unsigned int addr = azx_command_addr(val);
171 	int timeout = 50;
172 
173 	guard(spinlock_irq)(&bus->reg_lock);
174 
175 	while (timeout--) {
176 		/* check ICB bit */
177 		if (!((snd_hdac_chip_readw(bus, IRS) & AZX_IRS_BUSY))) {
178 			/* Clear IRV bit */
179 			snd_hdac_chip_updatew(bus, IRS, AZX_IRS_VALID, AZX_IRS_VALID);
180 			snd_hdac_chip_writel(bus, IC, val);
181 			/* Set ICB bit */
182 			snd_hdac_chip_updatew(bus, IRS, AZX_IRS_BUSY, AZX_IRS_BUSY);
183 
184 			return snd_hdac_bus_wait_for_pio_response(bus, addr);
185 		}
186 		udelay(1);
187 	}
188 
189 	dev_dbg_ratelimited(bus->dev, "send_cmd_pio timeout: IRS=%#x, val=%#x\n",
190 			    snd_hdac_chip_readw(bus, IRS), val);
191 
192 	return -EIO;
193 }
194 
195 /**
196  * snd_hdac_bus_get_response_pio - receive a response via Immediate Response
197  * @bus: HD-audio core bus
198  * @addr: codec address
199  * @res: pointer to store the value, NULL when not needed
200  *
201  * Returns zero if a value is read, or a negative error code.
202  */
203 static int snd_hdac_bus_get_response_pio(struct hdac_bus *bus,
204 					 unsigned int addr, unsigned int *res)
205 {
206 	if (res)
207 		*res = bus->rirb.res[addr];
208 
209 	return 0;
210 }
211 
212 /**
213  * snd_hdac_bus_send_cmd_corb - send a command verb via CORB
214  * @bus: HD-audio core bus
215  * @val: encoded verb value to send
216  *
217  * Returns zero for success or a negative error code.
218  */
219 static int snd_hdac_bus_send_cmd_corb(struct hdac_bus *bus, unsigned int val)
220 {
221 	unsigned int addr = azx_command_addr(val);
222 	unsigned int wp, rp;
223 
224 	guard(spinlock_irq)(&bus->reg_lock);
225 
226 	bus->last_cmd[azx_command_addr(val)] = val;
227 
228 	/* add command to corb */
229 	wp = snd_hdac_chip_readw(bus, CORBWP);
230 	if (wp == 0xffff) {
231 		/* something wrong, controller likely turned to D3 */
232 		return -EIO;
233 	}
234 	wp++;
235 	wp %= AZX_MAX_CORB_ENTRIES;
236 
237 	rp = snd_hdac_chip_readw(bus, CORBRP);
238 	if (wp == rp) {
239 		/* oops, it's full */
240 		return -EAGAIN;
241 	}
242 
243 	bus->rirb.cmds[addr]++;
244 	bus->corb.buf[wp] = cpu_to_le32(val);
245 	snd_hdac_chip_writew(bus, CORBWP, wp);
246 
247 	return 0;
248 }
249 
250 #define AZX_RIRB_EX_UNSOL_EV	(1<<4)
251 
252 /**
253  * snd_hdac_bus_update_rirb - retrieve RIRB entries
254  * @bus: HD-audio core bus
255  *
256  * Usually called from interrupt handler.
257  * The caller needs bus->reg_lock spinlock before calling this.
258  */
259 void snd_hdac_bus_update_rirb(struct hdac_bus *bus)
260 {
261 	unsigned int rp, wp;
262 	unsigned int addr;
263 	u32 res, res_ex;
264 
265 	wp = snd_hdac_chip_readw(bus, RIRBWP);
266 	if (wp == 0xffff) {
267 		/* something wrong, controller likely turned to D3 */
268 		return;
269 	}
270 
271 	if (wp == bus->rirb.wp)
272 		return;
273 	bus->rirb.wp = wp;
274 
275 	while (bus->rirb.rp != wp) {
276 		bus->rirb.rp++;
277 		bus->rirb.rp %= AZX_MAX_RIRB_ENTRIES;
278 
279 		rp = bus->rirb.rp << 1; /* an RIRB entry is 8-bytes */
280 		res_ex = le32_to_cpu(bus->rirb.buf[rp + 1]);
281 		res = le32_to_cpu(bus->rirb.buf[rp]);
282 		addr = res_ex & 0xf;
283 		if (addr >= HDA_MAX_CODECS) {
284 			dev_err(bus->dev,
285 				"spurious response %#x:%#x, rp = %d, wp = %d",
286 				res, res_ex, bus->rirb.rp, wp);
287 			snd_BUG();
288 		} else if (res_ex & AZX_RIRB_EX_UNSOL_EV)
289 			snd_hdac_bus_queue_event(bus, res, res_ex);
290 		else if (bus->rirb.cmds[addr]) {
291 			bus->rirb.res[addr] = res;
292 			bus->rirb.cmds[addr]--;
293 			if (!bus->rirb.cmds[addr] &&
294 			    waitqueue_active(&bus->rirb_wq))
295 				wake_up(&bus->rirb_wq);
296 		} else {
297 			dev_err_ratelimited(bus->dev,
298 				"spurious response %#x:%#x, last cmd=%#08x\n",
299 				res, res_ex, bus->last_cmd[addr]);
300 		}
301 	}
302 }
303 EXPORT_SYMBOL_GPL(snd_hdac_bus_update_rirb);
304 
305 /**
306  * snd_hdac_bus_get_response_rirb - receive a response via RIRB
307  * @bus: HD-audio core bus
308  * @addr: codec address
309  * @res: pointer to store the value, NULL when not needed
310  *
311  * Returns zero if a value is read, or a negative error code.
312  */
313 static int snd_hdac_bus_get_response_rirb(struct hdac_bus *bus,
314 					  unsigned int addr, unsigned int *res)
315 {
316 	unsigned long timeout;
317 	unsigned long loopcounter;
318 	wait_queue_entry_t wait;
319 	bool warned = false;
320 
321 	init_wait_entry(&wait, 0);
322 	timeout = jiffies + msecs_to_jiffies(1000);
323 
324 	for (loopcounter = 0;; loopcounter++) {
325 		scoped_guard(spinlock_irq, &bus->reg_lock) {
326 			if (!bus->polling_mode)
327 				prepare_to_wait(&bus->rirb_wq, &wait,
328 						TASK_UNINTERRUPTIBLE);
329 			if (bus->polling_mode)
330 				snd_hdac_bus_update_rirb(bus);
331 			if (!bus->rirb.cmds[addr]) {
332 				if (res)
333 					*res = bus->rirb.res[addr]; /* the last value */
334 				if (!bus->polling_mode)
335 					finish_wait(&bus->rirb_wq, &wait);
336 				return 0;
337 			}
338 		}
339 		if (time_after(jiffies, timeout))
340 			break;
341 #define LOOP_COUNT_MAX	3000
342 		if (!bus->polling_mode) {
343 			schedule_timeout(msecs_to_jiffies(2));
344 		} else if (bus->needs_damn_long_delay ||
345 			   loopcounter > LOOP_COUNT_MAX) {
346 			if (loopcounter > LOOP_COUNT_MAX && !warned) {
347 				dev_dbg_ratelimited(bus->dev,
348 						    "too slow response, last cmd=%#08x\n",
349 						    bus->last_cmd[addr]);
350 				warned = true;
351 			}
352 			msleep(2); /* temporary workaround */
353 		} else {
354 			udelay(10);
355 			cond_resched();
356 		}
357 	}
358 
359 	if (!bus->polling_mode)
360 		finish_wait(&bus->rirb_wq, &wait);
361 
362 	return -EIO;
363 }
364 
365 /**
366  * snd_hdac_bus_send_cmd - send a command verb via CORB or PIO
367  * @bus: HD-audio core bus
368  * @val: encoded verb value to send
369  *
370  * Returns zero for success or a negative error code.
371  */
372 int snd_hdac_bus_send_cmd(struct hdac_bus *bus, unsigned int val)
373 {
374 	if (bus->use_pio_for_commands)
375 		return snd_hdac_bus_send_cmd_pio(bus, val);
376 
377 	return snd_hdac_bus_send_cmd_corb(bus, val);
378 }
379 EXPORT_SYMBOL_GPL(snd_hdac_bus_send_cmd);
380 
381 /**
382  * snd_hdac_bus_get_response - receive a response via RIRB or PIO
383  * @bus: HD-audio core bus
384  * @addr: codec address
385  * @res: pointer to store the value, NULL when not needed
386  *
387  * Returns zero if a value is read, or a negative error code.
388  */
389 int snd_hdac_bus_get_response(struct hdac_bus *bus, unsigned int addr,
390 			      unsigned int *res)
391 {
392 	if (bus->use_pio_for_commands)
393 		return snd_hdac_bus_get_response_pio(bus, addr, res);
394 
395 	return snd_hdac_bus_get_response_rirb(bus, addr, res);
396 }
397 EXPORT_SYMBOL_GPL(snd_hdac_bus_get_response);
398 
399 /**
400  * snd_hdac_bus_reset_response_counter - reset the pending response count
401  * @bus: HD-audio core bus
402  * @addr: codec address
403  *
404  * Drop the number of commands that are still waiting for a response from
405  * the given codec address.  This is called when a verb is known to be dead,
406  * e.g. after a response timeout, so that a lost response won't leave the
407  * counter non-zero forever and block all later verbs to the same codec.
408  */
409 void snd_hdac_bus_reset_response_counter(struct hdac_bus *bus, unsigned int addr)
410 {
411 	if (addr >= HDA_MAX_CODECS)
412 		return;
413 
414 	guard(spinlock_irq)(&bus->reg_lock);
415 	bus->rirb.cmds[addr] = 0;
416 }
417 
418 #define HDAC_MAX_CAPS 10
419 /**
420  * snd_hdac_bus_parse_capabilities - parse capability structure
421  * @bus: the pointer to bus object
422  *
423  * Returns 0 if successful, or a negative error code.
424  */
425 int snd_hdac_bus_parse_capabilities(struct hdac_bus *bus)
426 {
427 	unsigned int cur_cap;
428 	unsigned int offset;
429 	unsigned int counter = 0;
430 
431 	offset = snd_hdac_chip_readw(bus, LLCH);
432 
433 	/* Lets walk the linked capabilities list */
434 	do {
435 		cur_cap = _snd_hdac_chip_readl(bus, offset);
436 
437 		dev_dbg(bus->dev, "Capability version: 0x%x\n",
438 			(cur_cap & AZX_CAP_HDR_VER_MASK) >> AZX_CAP_HDR_VER_OFF);
439 
440 		dev_dbg(bus->dev, "HDA capability ID: 0x%x\n",
441 			(cur_cap & AZX_CAP_HDR_ID_MASK) >> AZX_CAP_HDR_ID_OFF);
442 
443 		if (cur_cap == -1) {
444 			dev_dbg(bus->dev, "Invalid capability reg read\n");
445 			break;
446 		}
447 
448 		/* An all-zero header means no capability is present here */
449 		if (!cur_cap) {
450 			dev_dbg(bus->dev, "Empty capability header at offset 0x%x\n",
451 				offset);
452 			break;
453 		}
454 
455 		switch ((cur_cap & AZX_CAP_HDR_ID_MASK) >> AZX_CAP_HDR_ID_OFF) {
456 		case AZX_ML_CAP_ID:
457 			dev_dbg(bus->dev, "Found ML capability\n");
458 			bus->mlcap = bus->remap_addr + offset;
459 			break;
460 
461 		case AZX_GTS_CAP_ID:
462 			dev_dbg(bus->dev, "Found GTS capability offset=%x\n", offset);
463 			bus->gtscap = bus->remap_addr + offset;
464 			break;
465 
466 		case AZX_PP_CAP_ID:
467 			/* PP capability found, the Audio DSP is present */
468 			dev_dbg(bus->dev, "Found PP capability offset=%x\n", offset);
469 			bus->ppcap = bus->remap_addr + offset;
470 			break;
471 
472 		case AZX_SPB_CAP_ID:
473 			/* SPIB capability found, handler function */
474 			dev_dbg(bus->dev, "Found SPB capability\n");
475 			bus->spbcap = bus->remap_addr + offset;
476 			break;
477 
478 		case AZX_DRSM_CAP_ID:
479 			/* DMA resume  capability found, handler function */
480 			dev_dbg(bus->dev, "Found DRSM capability\n");
481 			bus->drsmcap = bus->remap_addr + offset;
482 			break;
483 
484 		default:
485 			dev_err(bus->dev, "Unknown capability ID 0x%x\n",
486 				(cur_cap & AZX_CAP_HDR_ID_MASK) >> AZX_CAP_HDR_ID_OFF);
487 			cur_cap = 0;
488 			break;
489 		}
490 
491 		counter++;
492 
493 		if (counter > HDAC_MAX_CAPS) {
494 			dev_err(bus->dev, "We exceeded HDAC capabilities!!!\n");
495 			break;
496 		}
497 
498 		/* read the offset of next capability */
499 		offset = cur_cap & AZX_CAP_HDR_NXT_PTR_MASK;
500 
501 	} while (offset);
502 
503 	return 0;
504 }
505 EXPORT_SYMBOL_GPL(snd_hdac_bus_parse_capabilities);
506 
507 /*
508  * Lowlevel interface
509  */
510 
511 /**
512  * snd_hdac_bus_enter_link_reset - enter link reset
513  * @bus: HD-audio core bus
514  *
515  * Enter to the link reset state.
516  */
517 void snd_hdac_bus_enter_link_reset(struct hdac_bus *bus)
518 {
519 	unsigned long timeout;
520 
521 	/* reset controller */
522 	snd_hdac_chip_updatel(bus, GCTL, AZX_GCTL_RESET, 0);
523 
524 	timeout = jiffies + msecs_to_jiffies(100);
525 	while ((snd_hdac_chip_readb(bus, GCTL) & AZX_GCTL_RESET) &&
526 	       time_before(jiffies, timeout))
527 		usleep_range(500, 1000);
528 }
529 EXPORT_SYMBOL_GPL(snd_hdac_bus_enter_link_reset);
530 
531 /**
532  * snd_hdac_bus_exit_link_reset - exit link reset
533  * @bus: HD-audio core bus
534  *
535  * Exit from the link reset state.
536  */
537 void snd_hdac_bus_exit_link_reset(struct hdac_bus *bus)
538 {
539 	unsigned long timeout;
540 
541 	if (bus->access_sdnctl_in_dword)
542 		snd_hdac_chip_updatel(bus, GCTL, AZX_GCTL_RESET, AZX_GCTL_RESET);
543 	else
544 		snd_hdac_chip_updateb(bus, GCTL, AZX_GCTL_RESET, AZX_GCTL_RESET);
545 
546 	timeout = jiffies + msecs_to_jiffies(100);
547 	while (!snd_hdac_chip_readb(bus, GCTL) && time_before(jiffies, timeout))
548 		usleep_range(500, 1000);
549 }
550 EXPORT_SYMBOL_GPL(snd_hdac_bus_exit_link_reset);
551 
552 /* reset codec link */
553 int snd_hdac_bus_reset_link(struct hdac_bus *bus, bool full_reset)
554 {
555 	if (!full_reset)
556 		goto skip_reset;
557 
558 	/* clear STATESTS if not in reset */
559 	if (snd_hdac_chip_readb(bus, GCTL) & AZX_GCTL_RESET)
560 		snd_hdac_chip_writew(bus, STATESTS, STATESTS_INT_MASK);
561 
562 	/* reset controller */
563 	snd_hdac_bus_enter_link_reset(bus);
564 
565 	/* delay for >= 100us for codec PLL to settle per spec
566 	 * Rev 0.9 section 5.5.1
567 	 */
568 	usleep_range(500, 1000);
569 
570 	/* Bring controller out of reset */
571 	snd_hdac_bus_exit_link_reset(bus);
572 
573 	/* Brent Chartrand said to wait >= 540us for codecs to initialize */
574 	usleep_range(1000, 1200);
575 
576  skip_reset:
577 	/* check to see if controller is ready */
578 	if (!snd_hdac_chip_readb(bus, GCTL)) {
579 		dev_dbg(bus->dev, "controller not ready!\n");
580 		return -EBUSY;
581 	}
582 
583 	/* detect codecs */
584 	if (!bus->codec_mask) {
585 		bus->codec_mask = snd_hdac_chip_readw(bus, STATESTS);
586 		dev_dbg(bus->dev, "codec_mask = 0x%lx\n", bus->codec_mask);
587 	}
588 
589 	return 0;
590 }
591 EXPORT_SYMBOL_GPL(snd_hdac_bus_reset_link);
592 
593 /* enable interrupts */
594 static void azx_int_enable(struct hdac_bus *bus)
595 {
596 	/* enable controller CIE and GIE */
597 	snd_hdac_chip_updatel(bus, INTCTL,
598 			      AZX_INT_CTRL_EN | AZX_INT_GLOBAL_EN,
599 			      AZX_INT_CTRL_EN | AZX_INT_GLOBAL_EN);
600 }
601 
602 /* disable interrupts */
603 static void azx_int_disable(struct hdac_bus *bus)
604 {
605 	struct hdac_stream *azx_dev;
606 
607 	/* disable interrupts in stream descriptor */
608 	list_for_each_entry(azx_dev, &bus->stream_list, list)
609 		if (bus->access_sdnctl_in_dword)
610 			snd_hdac_stream_updatel(azx_dev, SD_CTL, SD_INT_MASK, 0);
611 		else
612 			snd_hdac_stream_updateb(azx_dev, SD_CTL, SD_INT_MASK, 0);
613 
614 	/* disable SIE for all streams & disable controller CIE and GIE */
615 	snd_hdac_chip_writel(bus, INTCTL, 0);
616 }
617 
618 /* clear interrupts */
619 static void azx_int_clear(struct hdac_bus *bus)
620 {
621 	struct hdac_stream *azx_dev;
622 
623 	/* clear stream status */
624 	list_for_each_entry(azx_dev, &bus->stream_list, list)
625 		snd_hdac_stream_writeb(azx_dev, SD_STS, SD_INT_MASK);
626 
627 	/* clear STATESTS */
628 	snd_hdac_chip_writew(bus, STATESTS, STATESTS_INT_MASK);
629 
630 	/* clear rirb status */
631 	snd_hdac_chip_writeb(bus, RIRBSTS, RIRB_INT_MASK);
632 
633 	/* clear int status */
634 	snd_hdac_chip_writel(bus, INTSTS, AZX_INT_CTRL_EN | AZX_INT_ALL_STREAM);
635 }
636 
637 /**
638  * snd_hdac_bus_init_chip - reset and start the controller registers
639  * @bus: HD-audio core bus
640  * @full_reset: Do full reset
641  */
642 bool snd_hdac_bus_init_chip(struct hdac_bus *bus, bool full_reset)
643 {
644 	if (bus->chip_init)
645 		return false;
646 
647 	/* reset controller */
648 	snd_hdac_bus_reset_link(bus, full_reset);
649 
650 	/* clear interrupts */
651 	azx_int_clear(bus);
652 
653 	/* initialize the codec command I/O */
654 	snd_hdac_bus_init_cmd_io(bus);
655 
656 	/* enable interrupts after CORB/RIRB buffers are initialized above */
657 	azx_int_enable(bus);
658 
659 	/* program the position buffer */
660 	if (bus->use_posbuf && bus->posbuf.addr) {
661 		snd_hdac_chip_writel(bus, DPLBASE, (u32)(bus->posbuf.addr + bus->addr_offset));
662 		snd_hdac_chip_writel(bus, DPUBASE, upper_32_bits(bus->posbuf.addr + bus->addr_offset));
663 	}
664 
665 	bus->chip_init = true;
666 
667 	return true;
668 }
669 EXPORT_SYMBOL_GPL(snd_hdac_bus_init_chip);
670 
671 /**
672  * snd_hdac_bus_stop_chip - disable the whole IRQ and I/Os
673  * @bus: HD-audio core bus
674  */
675 void snd_hdac_bus_stop_chip(struct hdac_bus *bus)
676 {
677 	if (!bus->chip_init)
678 		return;
679 
680 	/* disable interrupts */
681 	azx_int_disable(bus);
682 	azx_int_clear(bus);
683 
684 	/* disable CORB/RIRB */
685 	snd_hdac_bus_stop_cmd_io(bus);
686 
687 	/* disable position buffer */
688 	if (bus->posbuf.addr) {
689 		snd_hdac_chip_writel(bus, DPLBASE, 0);
690 		snd_hdac_chip_writel(bus, DPUBASE, 0);
691 	}
692 
693 	bus->chip_init = false;
694 }
695 EXPORT_SYMBOL_GPL(snd_hdac_bus_stop_chip);
696 
697 /**
698  * snd_hdac_bus_handle_stream_irq - interrupt handler for streams
699  * @bus: HD-audio core bus
700  * @status: INTSTS register value
701  * @ack: callback to be called for woken streams
702  *
703  * Returns the bits of handled streams, or zero if no stream is handled.
704  */
705 int snd_hdac_bus_handle_stream_irq(struct hdac_bus *bus, unsigned int status,
706 				    void (*ack)(struct hdac_bus *,
707 						struct hdac_stream *))
708 {
709 	struct hdac_stream *azx_dev;
710 	u8 sd_status;
711 	int handled = 0;
712 
713 	list_for_each_entry(azx_dev, &bus->stream_list, list) {
714 		if (status & azx_dev->sd_int_sta_mask) {
715 			sd_status = snd_hdac_stream_readb(azx_dev, SD_STS);
716 			snd_hdac_stream_writeb(azx_dev, SD_STS, SD_INT_MASK);
717 			handled |= 1 << azx_dev->index;
718 			if (sd_status & (SD_INT_FIFO_ERR | SD_INT_DESC_ERR)) {
719 				dev_warn_ratelimited(bus->dev,
720 						"stream %u dma error: 0x%02x\n",
721 						azx_dev->index, sd_status);
722 			}
723 			if ((!azx_dev->substream && !azx_dev->cstream) ||
724 			    !azx_dev->running || !(sd_status & SD_INT_COMPLETE))
725 				continue;
726 			if (ack)
727 				ack(bus, azx_dev);
728 		}
729 	}
730 	return handled;
731 }
732 EXPORT_SYMBOL_GPL(snd_hdac_bus_handle_stream_irq);
733 
734 /**
735  * snd_hdac_bus_alloc_stream_pages - allocate BDL and other buffers
736  * @bus: HD-audio core bus
737  *
738  * Call this after assigning the all streams.
739  * Returns zero for success, or a negative error code.
740  */
741 int snd_hdac_bus_alloc_stream_pages(struct hdac_bus *bus)
742 {
743 	struct hdac_stream *s;
744 	int num_streams = 0;
745 	int dma_type = bus->dma_type ? bus->dma_type : SNDRV_DMA_TYPE_DEV;
746 	int err;
747 
748 	list_for_each_entry(s, &bus->stream_list, list) {
749 		/* allocate memory for the BDL for each stream */
750 		err = snd_dma_alloc_pages(dma_type, bus->dev,
751 					  BDL_SIZE, &s->bdl);
752 		num_streams++;
753 		if (err < 0)
754 			goto error_bdl;
755 	}
756 
757 	if (WARN_ON(!num_streams))
758 		return -EINVAL;
759 	/* allocate memory for the position buffer */
760 	err = snd_dma_alloc_pages(dma_type, bus->dev,
761 				  num_streams * 8, &bus->posbuf);
762 	if (err < 0)
763 		goto error_bdl;
764 	list_for_each_entry(s, &bus->stream_list, list)
765 		s->posbuf = (__le32 *)(bus->posbuf.area + s->index * 8);
766 
767 	/* single page (at least 4096 bytes) must suffice for both ringbuffes */
768 	err = snd_dma_alloc_pages(dma_type, bus->dev, PAGE_SIZE, &bus->rb);
769 	if (err < 0)
770 		goto error_posbuf;
771 	return 0;
772 
773 error_posbuf:
774 	snd_dma_free_pages(&bus->posbuf);
775 	bus->posbuf.area = NULL;
776 error_bdl:
777 	list_for_each_entry(s, &bus->stream_list, list) {
778 		if (s->bdl.area) {
779 			snd_dma_free_pages(&s->bdl);
780 			s->bdl.area = NULL;
781 		}
782 	}
783 	return -ENOMEM;
784 }
785 EXPORT_SYMBOL_GPL(snd_hdac_bus_alloc_stream_pages);
786 
787 /**
788  * snd_hdac_bus_free_stream_pages - release BDL and other buffers
789  * @bus: HD-audio core bus
790  */
791 void snd_hdac_bus_free_stream_pages(struct hdac_bus *bus)
792 {
793 	struct hdac_stream *s;
794 
795 	list_for_each_entry(s, &bus->stream_list, list) {
796 		if (s->bdl.area)
797 			snd_dma_free_pages(&s->bdl);
798 	}
799 
800 	if (bus->rb.area)
801 		snd_dma_free_pages(&bus->rb);
802 	if (bus->posbuf.area)
803 		snd_dma_free_pages(&bus->posbuf);
804 }
805 EXPORT_SYMBOL_GPL(snd_hdac_bus_free_stream_pages);
806 
807 /**
808  * snd_hdac_bus_link_power - power up/down codec link
809  * @codec: HD-audio device
810  * @enable: whether to power-up the link
811  */
812 void snd_hdac_bus_link_power(struct hdac_device *codec, bool enable)
813 {
814 	assign_bit(codec->addr, &codec->bus->codec_powered, enable);
815 }
816 EXPORT_SYMBOL_GPL(snd_hdac_bus_link_power);
817