1 /*
2  * dice_stream.c - a part of driver for DICE based devices
3  *
4  * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
5  * Copyright (c) 2014 Takashi Sakamoto <o-takashi@sakamocchi.jp>
6  *
7  * Licensed under the terms of the GNU General Public License, version 2.
8  */
9 
10 #include "dice.h"
11 
12 #define	CALLBACK_TIMEOUT	200
13 #define NOTIFICATION_TIMEOUT_MS	(2 * MSEC_PER_SEC)
14 
15 struct reg_params {
16 	unsigned int count;
17 	unsigned int size;
18 };
19 
20 const unsigned int snd_dice_rates[SND_DICE_RATES_COUNT] = {
21 	/* mode 0 */
22 	[0] =  32000,
23 	[1] =  44100,
24 	[2] =  48000,
25 	/* mode 1 */
26 	[3] =  88200,
27 	[4] =  96000,
28 	/* mode 2 */
29 	[5] = 176400,
30 	[6] = 192000,
31 };
32 
33 int snd_dice_stream_get_rate_mode(struct snd_dice *dice, unsigned int rate,
34 				  enum snd_dice_rate_mode *mode)
35 {
36 	/* Corresponding to each entry in snd_dice_rates. */
37 	static const enum snd_dice_rate_mode modes[] = {
38 		[0] = SND_DICE_RATE_MODE_LOW,
39 		[1] = SND_DICE_RATE_MODE_LOW,
40 		[2] = SND_DICE_RATE_MODE_LOW,
41 		[3] = SND_DICE_RATE_MODE_MIDDLE,
42 		[4] = SND_DICE_RATE_MODE_MIDDLE,
43 		[5] = SND_DICE_RATE_MODE_HIGH,
44 		[6] = SND_DICE_RATE_MODE_HIGH,
45 	};
46 	int i;
47 
48 	for (i = 0; i < ARRAY_SIZE(snd_dice_rates); i++) {
49 		if (!(dice->clock_caps & BIT(i)))
50 			continue;
51 		if (snd_dice_rates[i] != rate)
52 			continue;
53 
54 		*mode = modes[i];
55 		return 0;
56 	}
57 
58 	return -EINVAL;
59 }
60 
61 /*
62  * This operation has an effect to synchronize GLOBAL_STATUS/GLOBAL_SAMPLE_RATE
63  * to GLOBAL_STATUS. Especially, just after powering on, these are different.
64  */
65 static int ensure_phase_lock(struct snd_dice *dice, unsigned int rate)
66 {
67 	__be32 reg, nominal;
68 	u32 data;
69 	int i;
70 	int err;
71 
72 	err = snd_dice_transaction_read_global(dice, GLOBAL_CLOCK_SELECT,
73 					       &reg, sizeof(reg));
74 	if (err < 0)
75 		return err;
76 
77 	data = be32_to_cpu(reg);
78 
79 	data &= ~CLOCK_RATE_MASK;
80 	for (i = 0; i < ARRAY_SIZE(snd_dice_rates); ++i) {
81 		if (snd_dice_rates[i] == rate)
82 			break;
83 	}
84 	if (i == ARRAY_SIZE(snd_dice_rates))
85 		return -EINVAL;
86 	data |= i << CLOCK_RATE_SHIFT;
87 
88 	if (completion_done(&dice->clock_accepted))
89 		reinit_completion(&dice->clock_accepted);
90 
91 	reg = cpu_to_be32(data);
92 	err = snd_dice_transaction_write_global(dice, GLOBAL_CLOCK_SELECT,
93 						&reg, sizeof(reg));
94 	if (err < 0)
95 		return err;
96 
97 	if (wait_for_completion_timeout(&dice->clock_accepted,
98 			msecs_to_jiffies(NOTIFICATION_TIMEOUT_MS)) == 0) {
99 		/*
100 		 * Old versions of Dice firmware transfer no notification when
101 		 * the same clock status as current one is set. In this case,
102 		 * just check current clock status.
103 		 */
104 		err = snd_dice_transaction_read_global(dice, GLOBAL_STATUS,
105 						&nominal, sizeof(nominal));
106 		if (err < 0)
107 			return err;
108 		if (!(be32_to_cpu(nominal) & STATUS_SOURCE_LOCKED))
109 			return -ETIMEDOUT;
110 	}
111 
112 	return 0;
113 }
114 
115 static int get_register_params(struct snd_dice *dice,
116 			       struct reg_params *tx_params,
117 			       struct reg_params *rx_params)
118 {
119 	__be32 reg[2];
120 	int err;
121 
122 	err = snd_dice_transaction_read_tx(dice, TX_NUMBER, reg, sizeof(reg));
123 	if (err < 0)
124 		return err;
125 	tx_params->count =
126 			min_t(unsigned int, be32_to_cpu(reg[0]), MAX_STREAMS);
127 	tx_params->size = be32_to_cpu(reg[1]) * 4;
128 
129 	err = snd_dice_transaction_read_rx(dice, RX_NUMBER, reg, sizeof(reg));
130 	if (err < 0)
131 		return err;
132 	rx_params->count =
133 			min_t(unsigned int, be32_to_cpu(reg[0]), MAX_STREAMS);
134 	rx_params->size = be32_to_cpu(reg[1]) * 4;
135 
136 	return 0;
137 }
138 
139 static void release_resources(struct snd_dice *dice)
140 {
141 	int i;
142 
143 	for (i = 0; i < MAX_STREAMS; ++i) {
144 		fw_iso_resources_free(&dice->tx_resources[i]);
145 		fw_iso_resources_free(&dice->rx_resources[i]);
146 	}
147 }
148 
149 static void stop_streams(struct snd_dice *dice, enum amdtp_stream_direction dir,
150 			 struct reg_params *params)
151 {
152 	__be32 reg;
153 	unsigned int i;
154 
155 	for (i = 0; i < params->count; i++) {
156 		reg = cpu_to_be32((u32)-1);
157 		if (dir == AMDTP_IN_STREAM) {
158 			amdtp_stream_stop(&dice->tx_stream[i]);
159 
160 			snd_dice_transaction_write_tx(dice,
161 					params->size * i + TX_ISOCHRONOUS,
162 					&reg, sizeof(reg));
163 		} else {
164 			amdtp_stream_stop(&dice->rx_stream[i]);
165 
166 			snd_dice_transaction_write_rx(dice,
167 					params->size * i + RX_ISOCHRONOUS,
168 					&reg, sizeof(reg));
169 		}
170 	}
171 }
172 
173 static int keep_resources(struct snd_dice *dice, struct amdtp_stream *stream,
174 			  struct fw_iso_resources *resources, unsigned int rate,
175 			  unsigned int pcm_chs, unsigned int midi_ports)
176 {
177 	bool double_pcm_frames;
178 	unsigned int i;
179 	int err;
180 
181 	// At 176.4/192.0 kHz, Dice has a quirk to transfer two PCM frames in
182 	// one data block of AMDTP packet. Thus sampling transfer frequency is
183 	// a half of PCM sampling frequency, i.e. PCM frames at 192.0 kHz are
184 	// transferred on AMDTP packets at 96 kHz. Two successive samples of a
185 	// channel are stored consecutively in the packet. This quirk is called
186 	// as 'Dual Wire'.
187 	// For this quirk, blocking mode is required and PCM buffer size should
188 	// be aligned to SYT_INTERVAL.
189 	double_pcm_frames = rate > 96000;
190 	if (double_pcm_frames) {
191 		rate /= 2;
192 		pcm_chs *= 2;
193 	}
194 
195 	err = amdtp_am824_set_parameters(stream, rate, pcm_chs, midi_ports,
196 					 double_pcm_frames);
197 	if (err < 0)
198 		return err;
199 
200 	if (double_pcm_frames) {
201 		pcm_chs /= 2;
202 
203 		for (i = 0; i < pcm_chs; i++) {
204 			amdtp_am824_set_pcm_position(stream, i, i * 2);
205 			amdtp_am824_set_pcm_position(stream, i + pcm_chs,
206 						     i * 2 + 1);
207 		}
208 	}
209 
210 	return fw_iso_resources_allocate(resources,
211 				amdtp_stream_get_max_payload(stream),
212 				fw_parent_device(dice->unit)->max_speed);
213 }
214 
215 static int keep_dual_resources(struct snd_dice *dice, unsigned int rate,
216 			       enum amdtp_stream_direction dir,
217 			       struct reg_params *params)
218 {
219 	enum snd_dice_rate_mode mode;
220 	int i;
221 	int err;
222 
223 	err = snd_dice_stream_get_rate_mode(dice, rate, &mode);
224 	if (err < 0)
225 		return err;
226 
227 	for (i = 0; i < params->count; ++i) {
228 		__be32 reg[2];
229 		struct amdtp_stream *stream;
230 		struct fw_iso_resources *resources;
231 		unsigned int pcm_cache;
232 		unsigned int midi_cache;
233 		unsigned int pcm_chs;
234 		unsigned int midi_ports;
235 
236 		if (dir == AMDTP_IN_STREAM) {
237 			stream = &dice->tx_stream[i];
238 			resources = &dice->tx_resources[i];
239 
240 			pcm_cache = dice->tx_pcm_chs[i][mode];
241 			midi_cache = dice->tx_midi_ports[i];
242 			err = snd_dice_transaction_read_tx(dice,
243 					params->size * i + TX_NUMBER_AUDIO,
244 					reg, sizeof(reg));
245 		} else {
246 			stream = &dice->rx_stream[i];
247 			resources = &dice->rx_resources[i];
248 
249 			pcm_cache = dice->rx_pcm_chs[i][mode];
250 			midi_cache = dice->rx_midi_ports[i];
251 			err = snd_dice_transaction_read_rx(dice,
252 					params->size * i + RX_NUMBER_AUDIO,
253 					reg, sizeof(reg));
254 		}
255 		if (err < 0)
256 			return err;
257 		pcm_chs = be32_to_cpu(reg[0]);
258 		midi_ports = be32_to_cpu(reg[1]);
259 
260 		// These are important for developer of this driver.
261 		if (pcm_chs != pcm_cache || midi_ports != midi_cache) {
262 			dev_info(&dice->unit->device,
263 				 "cache mismatch: pcm: %u:%u, midi: %u:%u\n",
264 				 pcm_chs, pcm_cache, midi_ports, midi_cache);
265 			return -EPROTO;
266 		}
267 
268 		err = keep_resources(dice, stream, resources, rate, pcm_chs,
269 				     midi_ports);
270 		if (err < 0)
271 			return err;
272 	}
273 
274 	return 0;
275 }
276 
277 static void finish_session(struct snd_dice *dice, struct reg_params *tx_params,
278 			   struct reg_params *rx_params)
279 {
280 	stop_streams(dice, AMDTP_IN_STREAM, tx_params);
281 	stop_streams(dice, AMDTP_OUT_STREAM, rx_params);
282 
283 	snd_dice_transaction_clear_enable(dice);
284 }
285 
286 int snd_dice_stream_reserve_duplex(struct snd_dice *dice, unsigned int rate)
287 {
288 	unsigned int curr_rate;
289 	int err;
290 
291 	// Check sampling transmission frequency.
292 	err = snd_dice_transaction_get_rate(dice, &curr_rate);
293 	if (err < 0)
294 		return err;
295 	if (rate == 0)
296 		rate = curr_rate;
297 
298 	if (dice->substreams_counter == 0 || curr_rate != rate) {
299 		struct reg_params tx_params, rx_params;
300 
301 		err = get_register_params(dice, &tx_params, &rx_params);
302 		if (err < 0)
303 			return err;
304 
305 		finish_session(dice, &tx_params, &rx_params);
306 
307 		release_resources(dice);
308 
309 		// Just after owning the unit (GLOBAL_OWNER), the unit can
310 		// return invalid stream formats. Selecting clock parameters
311 		// have an effect for the unit to refine it.
312 		err = ensure_phase_lock(dice, rate);
313 		if (err < 0)
314 			return err;
315 
316 		// After changing sampling transfer frequency, the value of
317 		// register can be changed.
318 		err = get_register_params(dice, &tx_params, &rx_params);
319 		if (err < 0)
320 			return err;
321 
322 		err = keep_dual_resources(dice, rate, AMDTP_IN_STREAM,
323 					  &tx_params);
324 		if (err < 0)
325 			goto error;
326 
327 		err = keep_dual_resources(dice, rate, AMDTP_OUT_STREAM,
328 					  &rx_params);
329 		if (err < 0)
330 			goto error;
331 	}
332 
333 	return 0;
334 error:
335 	release_resources(dice);
336 	return err;
337 }
338 
339 static int start_streams(struct snd_dice *dice, enum amdtp_stream_direction dir,
340 			 unsigned int rate, struct reg_params *params)
341 {
342 	unsigned int max_speed = fw_parent_device(dice->unit)->max_speed;
343 	int i;
344 	int err;
345 
346 	for (i = 0; i < params->count; i++) {
347 		struct amdtp_stream *stream;
348 		struct fw_iso_resources *resources;
349 		__be32 reg;
350 
351 		if (dir == AMDTP_IN_STREAM) {
352 			stream = dice->tx_stream + i;
353 			resources = dice->tx_resources + i;
354 		} else {
355 			stream = dice->rx_stream + i;
356 			resources = dice->rx_resources + i;
357 		}
358 
359 		reg = cpu_to_be32(resources->channel);
360 		if (dir == AMDTP_IN_STREAM) {
361 			err = snd_dice_transaction_write_tx(dice,
362 					params->size * i + TX_ISOCHRONOUS,
363 					&reg, sizeof(reg));
364 		} else {
365 			err = snd_dice_transaction_write_rx(dice,
366 					params->size * i + RX_ISOCHRONOUS,
367 					&reg, sizeof(reg));
368 		}
369 		if (err < 0)
370 			return err;
371 
372 		if (dir == AMDTP_IN_STREAM) {
373 			reg = cpu_to_be32(max_speed);
374 			err = snd_dice_transaction_write_tx(dice,
375 					params->size * i + TX_SPEED,
376 					&reg, sizeof(reg));
377 			if (err < 0)
378 				return err;
379 		}
380 
381 		err = amdtp_stream_start(stream, resources->channel, max_speed);
382 		if (err < 0)
383 			return err;
384 	}
385 
386 	return 0;
387 }
388 
389 /*
390  * MEMO: After this function, there're two states of streams:
391  *  - None streams are running.
392  *  - All streams are running.
393  */
394 int snd_dice_stream_start_duplex(struct snd_dice *dice)
395 {
396 	unsigned int generation = dice->rx_resources[0].generation;
397 	struct reg_params tx_params, rx_params;
398 	unsigned int i;
399 	unsigned int rate;
400 	enum snd_dice_rate_mode mode;
401 	int err;
402 
403 	if (dice->substreams_counter == 0)
404 		return -EIO;
405 
406 	err = get_register_params(dice, &tx_params, &rx_params);
407 	if (err < 0)
408 		return err;
409 
410 	// Check error of packet streaming.
411 	for (i = 0; i < MAX_STREAMS; ++i) {
412 		if (amdtp_streaming_error(&dice->tx_stream[i]) ||
413 		    amdtp_streaming_error(&dice->rx_stream[i])) {
414 			finish_session(dice, &tx_params, &rx_params);
415 			break;
416 		}
417 	}
418 
419 	if (generation != fw_parent_device(dice->unit)->card->generation) {
420 		for (i = 0; i < MAX_STREAMS; ++i) {
421 			if (i < tx_params.count)
422 				fw_iso_resources_update(dice->tx_resources + i);
423 			if (i < rx_params.count)
424 				fw_iso_resources_update(dice->rx_resources + i);
425 		}
426 	}
427 
428 	// Check required streams are running or not.
429 	err = snd_dice_transaction_get_rate(dice, &rate);
430 	if (err < 0)
431 		return err;
432 	err = snd_dice_stream_get_rate_mode(dice, rate, &mode);
433 	if (err < 0)
434 		return err;
435 	for (i = 0; i < MAX_STREAMS; ++i) {
436 		if (dice->tx_pcm_chs[i][mode] > 0 &&
437 		    !amdtp_stream_running(&dice->tx_stream[i]))
438 			break;
439 		if (dice->rx_pcm_chs[i][mode] > 0 &&
440 		    !amdtp_stream_running(&dice->rx_stream[i]))
441 			break;
442 	}
443 	if (i < MAX_STREAMS) {
444 		// Start both streams.
445 		err = start_streams(dice, AMDTP_IN_STREAM, rate, &tx_params);
446 		if (err < 0)
447 			goto error;
448 
449 		err = start_streams(dice, AMDTP_OUT_STREAM, rate, &rx_params);
450 		if (err < 0)
451 			goto error;
452 
453 		err = snd_dice_transaction_set_enable(dice);
454 		if (err < 0) {
455 			dev_err(&dice->unit->device,
456 				"fail to enable interface\n");
457 			goto error;
458 		}
459 
460 		for (i = 0; i < MAX_STREAMS; i++) {
461 			if ((i < tx_params.count &&
462 			    !amdtp_stream_wait_callback(&dice->tx_stream[i],
463 							CALLBACK_TIMEOUT)) ||
464 			    (i < rx_params.count &&
465 			     !amdtp_stream_wait_callback(&dice->rx_stream[i],
466 							 CALLBACK_TIMEOUT))) {
467 				err = -ETIMEDOUT;
468 				goto error;
469 			}
470 		}
471 	}
472 
473 	return 0;
474 error:
475 	finish_session(dice, &tx_params, &rx_params);
476 	return err;
477 }
478 
479 /*
480  * MEMO: After this function, there're two states of streams:
481  *  - None streams are running.
482  *  - All streams are running.
483  */
484 void snd_dice_stream_stop_duplex(struct snd_dice *dice)
485 {
486 	struct reg_params tx_params, rx_params;
487 
488 	if (dice->substreams_counter == 0) {
489 		if (get_register_params(dice, &tx_params, &rx_params) >= 0)
490 			finish_session(dice, &tx_params, &rx_params);
491 
492 		release_resources(dice);
493 	}
494 }
495 
496 static int init_stream(struct snd_dice *dice, enum amdtp_stream_direction dir,
497 		       unsigned int index)
498 {
499 	struct amdtp_stream *stream;
500 	struct fw_iso_resources *resources;
501 	int err;
502 
503 	if (dir == AMDTP_IN_STREAM) {
504 		stream = &dice->tx_stream[index];
505 		resources = &dice->tx_resources[index];
506 	} else {
507 		stream = &dice->rx_stream[index];
508 		resources = &dice->rx_resources[index];
509 	}
510 
511 	err = fw_iso_resources_init(resources, dice->unit);
512 	if (err < 0)
513 		goto end;
514 	resources->channels_mask = 0x00000000ffffffffuLL;
515 
516 	err = amdtp_am824_init(stream, dice->unit, dir, CIP_BLOCKING);
517 	if (err < 0) {
518 		amdtp_stream_destroy(stream);
519 		fw_iso_resources_destroy(resources);
520 	}
521 end:
522 	return err;
523 }
524 
525 /*
526  * This function should be called before starting streams or after stopping
527  * streams.
528  */
529 static void destroy_stream(struct snd_dice *dice,
530 			   enum amdtp_stream_direction dir,
531 			   unsigned int index)
532 {
533 	struct amdtp_stream *stream;
534 	struct fw_iso_resources *resources;
535 
536 	if (dir == AMDTP_IN_STREAM) {
537 		stream = &dice->tx_stream[index];
538 		resources = &dice->tx_resources[index];
539 	} else {
540 		stream = &dice->rx_stream[index];
541 		resources = &dice->rx_resources[index];
542 	}
543 
544 	amdtp_stream_destroy(stream);
545 	fw_iso_resources_destroy(resources);
546 }
547 
548 int snd_dice_stream_init_duplex(struct snd_dice *dice)
549 {
550 	int i, err;
551 
552 	for (i = 0; i < MAX_STREAMS; i++) {
553 		err = init_stream(dice, AMDTP_IN_STREAM, i);
554 		if (err < 0) {
555 			for (; i >= 0; i--)
556 				destroy_stream(dice, AMDTP_IN_STREAM, i);
557 			goto end;
558 		}
559 	}
560 
561 	for (i = 0; i < MAX_STREAMS; i++) {
562 		err = init_stream(dice, AMDTP_OUT_STREAM, i);
563 		if (err < 0) {
564 			for (; i >= 0; i--)
565 				destroy_stream(dice, AMDTP_OUT_STREAM, i);
566 			for (i = 0; i < MAX_STREAMS; i++)
567 				destroy_stream(dice, AMDTP_IN_STREAM, i);
568 			break;
569 		}
570 	}
571 end:
572 	return err;
573 }
574 
575 void snd_dice_stream_destroy_duplex(struct snd_dice *dice)
576 {
577 	unsigned int i;
578 
579 	for (i = 0; i < MAX_STREAMS; i++) {
580 		destroy_stream(dice, AMDTP_IN_STREAM, i);
581 		destroy_stream(dice, AMDTP_OUT_STREAM, i);
582 	}
583 }
584 
585 void snd_dice_stream_update_duplex(struct snd_dice *dice)
586 {
587 	struct reg_params tx_params, rx_params;
588 
589 	/*
590 	 * On a bus reset, the DICE firmware disables streaming and then goes
591 	 * off contemplating its own navel for hundreds of milliseconds before
592 	 * it can react to any of our attempts to reenable streaming.  This
593 	 * means that we lose synchronization anyway, so we force our streams
594 	 * to stop so that the application can restart them in an orderly
595 	 * manner.
596 	 */
597 	dice->global_enabled = false;
598 
599 	if (get_register_params(dice, &tx_params, &rx_params) == 0) {
600 		stop_streams(dice, AMDTP_IN_STREAM, &tx_params);
601 		stop_streams(dice, AMDTP_OUT_STREAM, &rx_params);
602 	}
603 }
604 
605 int snd_dice_stream_detect_current_formats(struct snd_dice *dice)
606 {
607 	unsigned int rate;
608 	enum snd_dice_rate_mode mode;
609 	__be32 reg[2];
610 	struct reg_params tx_params, rx_params;
611 	int i;
612 	int err;
613 
614 	/* If extended protocol is available, detect detail spec. */
615 	err = snd_dice_detect_extension_formats(dice);
616 	if (err >= 0)
617 		return err;
618 
619 	/*
620 	 * Available stream format is restricted at current mode of sampling
621 	 * clock.
622 	 */
623 	err = snd_dice_transaction_get_rate(dice, &rate);
624 	if (err < 0)
625 		return err;
626 
627 	err = snd_dice_stream_get_rate_mode(dice, rate, &mode);
628 	if (err < 0)
629 		return err;
630 
631 	/*
632 	 * Just after owning the unit (GLOBAL_OWNER), the unit can return
633 	 * invalid stream formats. Selecting clock parameters have an effect
634 	 * for the unit to refine it.
635 	 */
636 	err = ensure_phase_lock(dice, rate);
637 	if (err < 0)
638 		return err;
639 
640 	err = get_register_params(dice, &tx_params, &rx_params);
641 	if (err < 0)
642 		return err;
643 
644 	for (i = 0; i < tx_params.count; ++i) {
645 		err = snd_dice_transaction_read_tx(dice,
646 				tx_params.size * i + TX_NUMBER_AUDIO,
647 				reg, sizeof(reg));
648 		if (err < 0)
649 			return err;
650 		dice->tx_pcm_chs[i][mode] = be32_to_cpu(reg[0]);
651 		dice->tx_midi_ports[i] = max_t(unsigned int,
652 				be32_to_cpu(reg[1]), dice->tx_midi_ports[i]);
653 	}
654 	for (i = 0; i < rx_params.count; ++i) {
655 		err = snd_dice_transaction_read_rx(dice,
656 				rx_params.size * i + RX_NUMBER_AUDIO,
657 				reg, sizeof(reg));
658 		if (err < 0)
659 			return err;
660 		dice->rx_pcm_chs[i][mode] = be32_to_cpu(reg[0]);
661 		dice->rx_midi_ports[i] = max_t(unsigned int,
662 				be32_to_cpu(reg[1]), dice->rx_midi_ports[i]);
663 	}
664 
665 	return 0;
666 }
667 
668 static void dice_lock_changed(struct snd_dice *dice)
669 {
670 	dice->dev_lock_changed = true;
671 	wake_up(&dice->hwdep_wait);
672 }
673 
674 int snd_dice_stream_lock_try(struct snd_dice *dice)
675 {
676 	int err;
677 
678 	spin_lock_irq(&dice->lock);
679 
680 	if (dice->dev_lock_count < 0) {
681 		err = -EBUSY;
682 		goto out;
683 	}
684 
685 	if (dice->dev_lock_count++ == 0)
686 		dice_lock_changed(dice);
687 	err = 0;
688 out:
689 	spin_unlock_irq(&dice->lock);
690 	return err;
691 }
692 
693 void snd_dice_stream_lock_release(struct snd_dice *dice)
694 {
695 	spin_lock_irq(&dice->lock);
696 
697 	if (WARN_ON(dice->dev_lock_count <= 0))
698 		goto out;
699 
700 	if (--dice->dev_lock_count == 0)
701 		dice_lock_changed(dice);
702 out:
703 	spin_unlock_irq(&dice->lock);
704 }
705