1 /*
2  * Copyright 2015 Advanced Micro Devices, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * Authors: AMD
23  *
24  */
25 
26 #include "dm_services_types.h"
27 #include "dc.h"
28 
29 #include "amdgpu.h"
30 #include "amdgpu_dm.h"
31 #include "amdgpu_dm_irq.h"
32 
33 /**
34  * DOC: overview
35  *
36  * DM provides another layer of IRQ management on top of what the base driver
37  * already provides. This is something that could be cleaned up, and is a
38  * future TODO item.
39  *
40  * The base driver provides IRQ source registration with DRM, handler
41  * registration into the base driver's IRQ table, and a handler callback
42  * amdgpu_irq_handler(), with which DRM calls on interrupts. This generic
43  * handler looks up the IRQ table, and calls the respective
44  * &amdgpu_irq_src_funcs.process hookups.
45  *
46  * What DM provides on top are two IRQ tables specifically for top-half and
47  * bottom-half IRQ handling, with the bottom-half implementing workqueues:
48  *
49  * - &amdgpu_display_manager.irq_handler_list_high_tab
50  * - &amdgpu_display_manager.irq_handler_list_low_tab
51  *
52  * They override the base driver's IRQ table, and the effect can be seen
53  * in the hooks that DM provides for &amdgpu_irq_src_funcs.process. They
54  * are all set to the DM generic handler amdgpu_dm_irq_handler(), which looks up
55  * DM's IRQ tables. However, in order for base driver to recognize this hook, DM
56  * still needs to register the IRQ with the base driver. See
57  * dce110_register_irq_handlers() and dcn10_register_irq_handlers().
58  *
59  * To expose DC's hardware interrupt toggle to the base driver, DM implements
60  * &amdgpu_irq_src_funcs.set hooks. Base driver calls it through
61  * amdgpu_irq_update() to enable or disable the interrupt.
62  */
63 
64 /******************************************************************************
65  * Private declarations.
66  *****************************************************************************/
67 
68 /**
69  * struct amdgpu_dm_irq_handler_data - Data for DM interrupt handlers.
70  *
71  * @list: Linked list entry referencing the next/previous handler
72  * @handler: Handler function
73  * @handler_arg: Argument passed to the handler when triggered
74  * @dm: DM which this handler belongs to
75  * @irq_source: DC interrupt source that this handler is registered for
76  */
77 struct amdgpu_dm_irq_handler_data {
78 	struct list_head list;
79 	interrupt_handler handler;
80 	void *handler_arg;
81 
82 	struct amdgpu_display_manager *dm;
83 	/* DAL irq source which registered for this interrupt. */
84 	enum dc_irq_source irq_source;
85 };
86 
87 #define DM_IRQ_TABLE_LOCK(adev, flags) \
88 	spin_lock_irqsave(&adev->dm.irq_handler_list_table_lock, flags)
89 
90 #define DM_IRQ_TABLE_UNLOCK(adev, flags) \
91 	spin_unlock_irqrestore(&adev->dm.irq_handler_list_table_lock, flags)
92 
93 /******************************************************************************
94  * Private functions.
95  *****************************************************************************/
96 
97 static void init_handler_common_data(struct amdgpu_dm_irq_handler_data *hcd,
98 				     void (*ih)(void *),
99 				     void *args,
100 				     struct amdgpu_display_manager *dm)
101 {
102 	hcd->handler = ih;
103 	hcd->handler_arg = args;
104 	hcd->dm = dm;
105 }
106 
107 /**
108  * dm_irq_work_func() - Handle an IRQ outside of the interrupt handler proper.
109  *
110  * @work: work struct
111  */
112 static void dm_irq_work_func(struct work_struct *work)
113 {
114 	struct list_head *entry;
115 	struct irq_list_head *irq_list_head =
116 		container_of(work, struct irq_list_head, work);
117 	struct list_head *handler_list = &irq_list_head->head;
118 	struct amdgpu_dm_irq_handler_data *handler_data;
119 
120 	list_for_each(entry, handler_list) {
121 		handler_data = list_entry(entry,
122 					  struct amdgpu_dm_irq_handler_data,
123 					  list);
124 
125 		DRM_DEBUG_KMS("DM_IRQ: work_func: for dal_src=%d\n",
126 				handler_data->irq_source);
127 
128 		DRM_DEBUG_KMS("DM_IRQ: schedule_work: for dal_src=%d\n",
129 			handler_data->irq_source);
130 
131 		handler_data->handler(handler_data->handler_arg);
132 	}
133 
134 	/* Call a DAL subcomponent which registered for interrupt notification
135 	 * at INTERRUPT_LOW_IRQ_CONTEXT.
136 	 * (The most common use is HPD interrupt) */
137 }
138 
139 /*
140  * Remove a handler and return a pointer to handler list from which the
141  * handler was removed.
142  */
143 static struct list_head *remove_irq_handler(struct amdgpu_device *adev,
144 					    void *ih,
145 					    const struct dc_interrupt_params *int_params)
146 {
147 	struct list_head *hnd_list;
148 	struct list_head *entry, *tmp;
149 	struct amdgpu_dm_irq_handler_data *handler;
150 	unsigned long irq_table_flags;
151 	bool handler_removed = false;
152 	enum dc_irq_source irq_source;
153 
154 	DM_IRQ_TABLE_LOCK(adev, irq_table_flags);
155 
156 	irq_source = int_params->irq_source;
157 
158 	switch (int_params->int_context) {
159 	case INTERRUPT_HIGH_IRQ_CONTEXT:
160 		hnd_list = &adev->dm.irq_handler_list_high_tab[irq_source];
161 		break;
162 	case INTERRUPT_LOW_IRQ_CONTEXT:
163 	default:
164 		hnd_list = &adev->dm.irq_handler_list_low_tab[irq_source].head;
165 		break;
166 	}
167 
168 	list_for_each_safe(entry, tmp, hnd_list) {
169 
170 		handler = list_entry(entry, struct amdgpu_dm_irq_handler_data,
171 				     list);
172 
173 		if (ih == handler) {
174 			/* Found our handler. Remove it from the list. */
175 			list_del(&handler->list);
176 			handler_removed = true;
177 			break;
178 		}
179 	}
180 
181 	DM_IRQ_TABLE_UNLOCK(adev, irq_table_flags);
182 
183 	if (handler_removed == false) {
184 		/* Not necessarily an error - caller may not
185 		 * know the context. */
186 		return NULL;
187 	}
188 
189 	kfree(handler);
190 
191 	DRM_DEBUG_KMS(
192 	"DM_IRQ: removed irq handler: %p for: dal_src=%d, irq context=%d\n",
193 		ih, int_params->irq_source, int_params->int_context);
194 
195 	return hnd_list;
196 }
197 
198 static bool
199 validate_irq_registration_params(struct dc_interrupt_params *int_params,
200 				 void (*ih)(void *))
201 {
202 	if (NULL == int_params || NULL == ih) {
203 		DRM_ERROR("DM_IRQ: invalid input!\n");
204 		return false;
205 	}
206 
207 	if (int_params->int_context >= INTERRUPT_CONTEXT_NUMBER) {
208 		DRM_ERROR("DM_IRQ: invalid context: %d!\n",
209 				int_params->int_context);
210 		return false;
211 	}
212 
213 	if (!DAL_VALID_IRQ_SRC_NUM(int_params->irq_source)) {
214 		DRM_ERROR("DM_IRQ: invalid irq_source: %d!\n",
215 				int_params->irq_source);
216 		return false;
217 	}
218 
219 	return true;
220 }
221 
222 static bool validate_irq_unregistration_params(enum dc_irq_source irq_source,
223 					       irq_handler_idx handler_idx)
224 {
225 	if (DAL_INVALID_IRQ_HANDLER_IDX == handler_idx) {
226 		DRM_ERROR("DM_IRQ: invalid handler_idx==NULL!\n");
227 		return false;
228 	}
229 
230 	if (!DAL_VALID_IRQ_SRC_NUM(irq_source)) {
231 		DRM_ERROR("DM_IRQ: invalid irq_source:%d!\n", irq_source);
232 		return false;
233 	}
234 
235 	return true;
236 }
237 /******************************************************************************
238  * Public functions.
239  *
240  * Note: caller is responsible for input validation.
241  *****************************************************************************/
242 
243 /**
244  * amdgpu_dm_irq_register_interrupt() - Register a handler within DM.
245  * @adev: The base driver device containing the DM device.
246  * @int_params: Interrupt parameters containing the source, and handler context
247  * @ih: Function pointer to the interrupt handler to register
248  * @handler_args: Arguments passed to the handler when the interrupt occurs
249  *
250  * Register an interrupt handler for the given IRQ source, under the given
251  * context. The context can either be high or low. High context handlers are
252  * executed directly within ISR context, while low context is executed within a
253  * workqueue, thereby allowing operations that sleep.
254  *
255  * Registered handlers are called in a FIFO manner, i.e. the most recently
256  * registered handler will be called first.
257  *
258  * Return: Handler data &struct amdgpu_dm_irq_handler_data containing the IRQ
259  *         source, handler function, and args
260  */
261 void *amdgpu_dm_irq_register_interrupt(struct amdgpu_device *adev,
262 				       struct dc_interrupt_params *int_params,
263 				       void (*ih)(void *),
264 				       void *handler_args)
265 {
266 	struct list_head *hnd_list;
267 	struct amdgpu_dm_irq_handler_data *handler_data;
268 	unsigned long irq_table_flags;
269 	enum dc_irq_source irq_source;
270 
271 	if (false == validate_irq_registration_params(int_params, ih))
272 		return DAL_INVALID_IRQ_HANDLER_IDX;
273 
274 	handler_data = kzalloc(sizeof(*handler_data), GFP_KERNEL);
275 	if (!handler_data) {
276 		DRM_ERROR("DM_IRQ: failed to allocate irq handler!\n");
277 		return DAL_INVALID_IRQ_HANDLER_IDX;
278 	}
279 
280 	memset(handler_data, 0, sizeof(*handler_data));
281 
282 	init_handler_common_data(handler_data, ih, handler_args, &adev->dm);
283 
284 	irq_source = int_params->irq_source;
285 
286 	handler_data->irq_source = irq_source;
287 
288 	/* Lock the list, add the handler. */
289 	DM_IRQ_TABLE_LOCK(adev, irq_table_flags);
290 
291 	switch (int_params->int_context) {
292 	case INTERRUPT_HIGH_IRQ_CONTEXT:
293 		hnd_list = &adev->dm.irq_handler_list_high_tab[irq_source];
294 		break;
295 	case INTERRUPT_LOW_IRQ_CONTEXT:
296 	default:
297 		hnd_list = &adev->dm.irq_handler_list_low_tab[irq_source].head;
298 		break;
299 	}
300 
301 	list_add_tail(&handler_data->list, hnd_list);
302 
303 	DM_IRQ_TABLE_UNLOCK(adev, irq_table_flags);
304 
305 	/* This pointer will be stored by code which requested interrupt
306 	 * registration.
307 	 * The same pointer will be needed in order to unregister the
308 	 * interrupt. */
309 
310 	DRM_DEBUG_KMS(
311 		"DM_IRQ: added irq handler: %p for: dal_src=%d, irq context=%d\n",
312 		handler_data,
313 		irq_source,
314 		int_params->int_context);
315 
316 	return handler_data;
317 }
318 
319 /**
320  * amdgpu_dm_irq_unregister_interrupt() - Remove a handler from the DM IRQ table
321  * @adev: The base driver device containing the DM device
322  * @irq_source: IRQ source to remove the given handler from
323  * @ih: Function pointer to the interrupt handler to unregister
324  *
325  * Go through both low and high context IRQ tables, and find the given handler
326  * for the given irq source. If found, remove it. Otherwise, do nothing.
327  */
328 void amdgpu_dm_irq_unregister_interrupt(struct amdgpu_device *adev,
329 					enum dc_irq_source irq_source,
330 					void *ih)
331 {
332 	struct list_head *handler_list;
333 	struct dc_interrupt_params int_params;
334 	int i;
335 
336 	if (false == validate_irq_unregistration_params(irq_source, ih))
337 		return;
338 
339 	memset(&int_params, 0, sizeof(int_params));
340 
341 	int_params.irq_source = irq_source;
342 
343 	for (i = 0; i < INTERRUPT_CONTEXT_NUMBER; i++) {
344 
345 		int_params.int_context = i;
346 
347 		handler_list = remove_irq_handler(adev, ih, &int_params);
348 
349 		if (handler_list != NULL)
350 			break;
351 	}
352 
353 	if (handler_list == NULL) {
354 		/* If we got here, it means we searched all irq contexts
355 		 * for this irq source, but the handler was not found. */
356 		DRM_ERROR(
357 		"DM_IRQ: failed to find irq handler:%p for irq_source:%d!\n",
358 			ih, irq_source);
359 	}
360 }
361 
362 /**
363  * amdgpu_dm_irq_init() - Initialize DM IRQ management
364  * @adev:  The base driver device containing the DM device
365  *
366  * Initialize DM's high and low context IRQ tables.
367  *
368  * The N by M table contains N IRQ sources, with M
369  * &struct amdgpu_dm_irq_handler_data hooked together in a linked list. The
370  * list_heads are initialized here. When an interrupt n is triggered, all m
371  * handlers are called in sequence, FIFO according to registration order.
372  *
373  * The low context table requires special steps to initialize, since handlers
374  * will be deferred to a workqueue. See &struct irq_list_head.
375  */
376 int amdgpu_dm_irq_init(struct amdgpu_device *adev)
377 {
378 	int src;
379 	struct irq_list_head *lh;
380 
381 	DRM_DEBUG_KMS("DM_IRQ\n");
382 
383 	spin_lock_init(&adev->dm.irq_handler_list_table_lock);
384 
385 	for (src = 0; src < DAL_IRQ_SOURCES_NUMBER; src++) {
386 		/* low context handler list init */
387 		lh = &adev->dm.irq_handler_list_low_tab[src];
388 		INIT_LIST_HEAD(&lh->head);
389 		INIT_WORK(&lh->work, dm_irq_work_func);
390 
391 		/* high context handler init */
392 		INIT_LIST_HEAD(&adev->dm.irq_handler_list_high_tab[src]);
393 	}
394 
395 	return 0;
396 }
397 
398 /**
399  * amdgpu_dm_irq_fini() - Tear down DM IRQ management
400  * @adev: The base driver device containing the DM device
401  *
402  * Flush all work within the low context IRQ table.
403  */
404 void amdgpu_dm_irq_fini(struct amdgpu_device *adev)
405 {
406 	int src;
407 	struct irq_list_head *lh;
408 	unsigned long irq_table_flags;
409 	DRM_DEBUG_KMS("DM_IRQ: releasing resources.\n");
410 	for (src = 0; src < DAL_IRQ_SOURCES_NUMBER; src++) {
411 		DM_IRQ_TABLE_LOCK(adev, irq_table_flags);
412 		/* The handler was removed from the table,
413 		 * it means it is safe to flush all the 'work'
414 		 * (because no code can schedule a new one). */
415 		lh = &adev->dm.irq_handler_list_low_tab[src];
416 		DM_IRQ_TABLE_UNLOCK(adev, irq_table_flags);
417 		flush_work(&lh->work);
418 	}
419 }
420 
421 int amdgpu_dm_irq_suspend(struct amdgpu_device *adev)
422 {
423 	int src;
424 	struct list_head *hnd_list_h;
425 	struct list_head *hnd_list_l;
426 	unsigned long irq_table_flags;
427 
428 	DM_IRQ_TABLE_LOCK(adev, irq_table_flags);
429 
430 	DRM_DEBUG_KMS("DM_IRQ: suspend\n");
431 
432 	/**
433 	 * Disable HW interrupt  for HPD and HPDRX only since FLIP and VBLANK
434 	 * will be disabled from manage_dm_interrupts on disable CRTC.
435 	 */
436 	for (src = DC_IRQ_SOURCE_HPD1; src <= DC_IRQ_SOURCE_HPD6RX; src++) {
437 		hnd_list_l = &adev->dm.irq_handler_list_low_tab[src].head;
438 		hnd_list_h = &adev->dm.irq_handler_list_high_tab[src];
439 		if (!list_empty(hnd_list_l) || !list_empty(hnd_list_h))
440 			dc_interrupt_set(adev->dm.dc, src, false);
441 
442 		DM_IRQ_TABLE_UNLOCK(adev, irq_table_flags);
443 		flush_work(&adev->dm.irq_handler_list_low_tab[src].work);
444 
445 		DM_IRQ_TABLE_LOCK(adev, irq_table_flags);
446 	}
447 
448 	DM_IRQ_TABLE_UNLOCK(adev, irq_table_flags);
449 	return 0;
450 }
451 
452 int amdgpu_dm_irq_resume_early(struct amdgpu_device *adev)
453 {
454 	int src;
455 	struct list_head *hnd_list_h, *hnd_list_l;
456 	unsigned long irq_table_flags;
457 
458 	DM_IRQ_TABLE_LOCK(adev, irq_table_flags);
459 
460 	DRM_DEBUG_KMS("DM_IRQ: early resume\n");
461 
462 	/* re-enable short pulse interrupts HW interrupt */
463 	for (src = DC_IRQ_SOURCE_HPD1RX; src <= DC_IRQ_SOURCE_HPD6RX; src++) {
464 		hnd_list_l = &adev->dm.irq_handler_list_low_tab[src].head;
465 		hnd_list_h = &adev->dm.irq_handler_list_high_tab[src];
466 		if (!list_empty(hnd_list_l) || !list_empty(hnd_list_h))
467 			dc_interrupt_set(adev->dm.dc, src, true);
468 	}
469 
470 	DM_IRQ_TABLE_UNLOCK(adev, irq_table_flags);
471 
472 	return 0;
473 }
474 
475 int amdgpu_dm_irq_resume_late(struct amdgpu_device *adev)
476 {
477 	int src;
478 	struct list_head *hnd_list_h, *hnd_list_l;
479 	unsigned long irq_table_flags;
480 
481 	DM_IRQ_TABLE_LOCK(adev, irq_table_flags);
482 
483 	DRM_DEBUG_KMS("DM_IRQ: resume\n");
484 
485 	/**
486 	 * Renable HW interrupt  for HPD and only since FLIP and VBLANK
487 	 * will be enabled from manage_dm_interrupts on enable CRTC.
488 	 */
489 	for (src = DC_IRQ_SOURCE_HPD1; src <= DC_IRQ_SOURCE_HPD6; src++) {
490 		hnd_list_l = &adev->dm.irq_handler_list_low_tab[src].head;
491 		hnd_list_h = &adev->dm.irq_handler_list_high_tab[src];
492 		if (!list_empty(hnd_list_l) || !list_empty(hnd_list_h))
493 			dc_interrupt_set(adev->dm.dc, src, true);
494 	}
495 
496 	DM_IRQ_TABLE_UNLOCK(adev, irq_table_flags);
497 	return 0;
498 }
499 
500 /*
501  * amdgpu_dm_irq_schedule_work - schedule all work items registered for the
502  * "irq_source".
503  */
504 static void amdgpu_dm_irq_schedule_work(struct amdgpu_device *adev,
505 					enum dc_irq_source irq_source)
506 {
507 	unsigned long irq_table_flags;
508 	struct work_struct *work = NULL;
509 
510 	DM_IRQ_TABLE_LOCK(adev, irq_table_flags);
511 
512 	if (!list_empty(&adev->dm.irq_handler_list_low_tab[irq_source].head))
513 		work = &adev->dm.irq_handler_list_low_tab[irq_source].work;
514 
515 	DM_IRQ_TABLE_UNLOCK(adev, irq_table_flags);
516 
517 	if (work) {
518 		if (!schedule_work(work))
519 			DRM_INFO("amdgpu_dm_irq_schedule_work FAILED src %d\n",
520 						irq_source);
521 	}
522 
523 }
524 
525 /*
526  * amdgpu_dm_irq_immediate_work
527  * Callback high irq work immediately, don't send to work queue
528  */
529 static void amdgpu_dm_irq_immediate_work(struct amdgpu_device *adev,
530 					 enum dc_irq_source irq_source)
531 {
532 	struct amdgpu_dm_irq_handler_data *handler_data;
533 	struct list_head *entry;
534 	unsigned long irq_table_flags;
535 
536 	DM_IRQ_TABLE_LOCK(adev, irq_table_flags);
537 
538 	list_for_each(
539 		entry,
540 		&adev->dm.irq_handler_list_high_tab[irq_source]) {
541 
542 		handler_data = list_entry(entry,
543 					  struct amdgpu_dm_irq_handler_data,
544 					  list);
545 
546 		/* Call a subcomponent which registered for immediate
547 		 * interrupt notification */
548 		handler_data->handler(handler_data->handler_arg);
549 	}
550 
551 	DM_IRQ_TABLE_UNLOCK(adev, irq_table_flags);
552 }
553 
554 /**
555  * amdgpu_dm_irq_handler - Generic DM IRQ handler
556  * @adev: amdgpu base driver device containing the DM device
557  * @source: Unused
558  * @entry: Data about the triggered interrupt
559  *
560  * Calls all registered high irq work immediately, and schedules work for low
561  * irq. The DM IRQ table is used to find the corresponding handlers.
562  */
563 static int amdgpu_dm_irq_handler(struct amdgpu_device *adev,
564 				 struct amdgpu_irq_src *source,
565 				 struct amdgpu_iv_entry *entry)
566 {
567 
568 	enum dc_irq_source src =
569 		dc_interrupt_to_irq_source(
570 			adev->dm.dc,
571 			entry->src_id,
572 			entry->src_data[0]);
573 
574 	dc_interrupt_ack(adev->dm.dc, src);
575 
576 	/* Call high irq work immediately */
577 	amdgpu_dm_irq_immediate_work(adev, src);
578 	/*Schedule low_irq work */
579 	amdgpu_dm_irq_schedule_work(adev, src);
580 
581 	return 0;
582 }
583 
584 static enum dc_irq_source amdgpu_dm_hpd_to_dal_irq_source(unsigned type)
585 {
586 	switch (type) {
587 	case AMDGPU_HPD_1:
588 		return DC_IRQ_SOURCE_HPD1;
589 	case AMDGPU_HPD_2:
590 		return DC_IRQ_SOURCE_HPD2;
591 	case AMDGPU_HPD_3:
592 		return DC_IRQ_SOURCE_HPD3;
593 	case AMDGPU_HPD_4:
594 		return DC_IRQ_SOURCE_HPD4;
595 	case AMDGPU_HPD_5:
596 		return DC_IRQ_SOURCE_HPD5;
597 	case AMDGPU_HPD_6:
598 		return DC_IRQ_SOURCE_HPD6;
599 	default:
600 		return DC_IRQ_SOURCE_INVALID;
601 	}
602 }
603 
604 static int amdgpu_dm_set_hpd_irq_state(struct amdgpu_device *adev,
605 				       struct amdgpu_irq_src *source,
606 				       unsigned type,
607 				       enum amdgpu_interrupt_state state)
608 {
609 	enum dc_irq_source src = amdgpu_dm_hpd_to_dal_irq_source(type);
610 	bool st = (state == AMDGPU_IRQ_STATE_ENABLE);
611 
612 	dc_interrupt_set(adev->dm.dc, src, st);
613 	return 0;
614 }
615 
616 static inline int dm_irq_state(struct amdgpu_device *adev,
617 			       struct amdgpu_irq_src *source,
618 			       unsigned crtc_id,
619 			       enum amdgpu_interrupt_state state,
620 			       const enum irq_type dal_irq_type,
621 			       const char *func)
622 {
623 	bool st;
624 	enum dc_irq_source irq_source;
625 
626 	struct amdgpu_crtc *acrtc = adev->mode_info.crtcs[crtc_id];
627 
628 	if (!acrtc) {
629 		DRM_ERROR(
630 			"%s: crtc is NULL at id :%d\n",
631 			func,
632 			crtc_id);
633 		return 0;
634 	}
635 
636 	if (acrtc->otg_inst == -1)
637 		return 0;
638 
639 	irq_source = dal_irq_type + acrtc->otg_inst;
640 
641 	st = (state == AMDGPU_IRQ_STATE_ENABLE);
642 
643 	dc_interrupt_set(adev->dm.dc, irq_source, st);
644 	return 0;
645 }
646 
647 static int amdgpu_dm_set_pflip_irq_state(struct amdgpu_device *adev,
648 					 struct amdgpu_irq_src *source,
649 					 unsigned crtc_id,
650 					 enum amdgpu_interrupt_state state)
651 {
652 	return dm_irq_state(
653 		adev,
654 		source,
655 		crtc_id,
656 		state,
657 		IRQ_TYPE_PFLIP,
658 		__func__);
659 }
660 
661 static int amdgpu_dm_set_crtc_irq_state(struct amdgpu_device *adev,
662 					struct amdgpu_irq_src *source,
663 					unsigned crtc_id,
664 					enum amdgpu_interrupt_state state)
665 {
666 	return dm_irq_state(
667 		adev,
668 		source,
669 		crtc_id,
670 		state,
671 		IRQ_TYPE_VBLANK,
672 		__func__);
673 }
674 
675 static int amdgpu_dm_set_vupdate_irq_state(struct amdgpu_device *adev,
676 					   struct amdgpu_irq_src *source,
677 					   unsigned int crtc_id,
678 					   enum amdgpu_interrupt_state state)
679 {
680 	return dm_irq_state(
681 		adev,
682 		source,
683 		crtc_id,
684 		state,
685 		IRQ_TYPE_VUPDATE,
686 		__func__);
687 }
688 
689 static const struct amdgpu_irq_src_funcs dm_crtc_irq_funcs = {
690 	.set = amdgpu_dm_set_crtc_irq_state,
691 	.process = amdgpu_dm_irq_handler,
692 };
693 
694 static const struct amdgpu_irq_src_funcs dm_vupdate_irq_funcs = {
695 	.set = amdgpu_dm_set_vupdate_irq_state,
696 	.process = amdgpu_dm_irq_handler,
697 };
698 
699 static const struct amdgpu_irq_src_funcs dm_pageflip_irq_funcs = {
700 	.set = amdgpu_dm_set_pflip_irq_state,
701 	.process = amdgpu_dm_irq_handler,
702 };
703 
704 static const struct amdgpu_irq_src_funcs dm_hpd_irq_funcs = {
705 	.set = amdgpu_dm_set_hpd_irq_state,
706 	.process = amdgpu_dm_irq_handler,
707 };
708 
709 void amdgpu_dm_set_irq_funcs(struct amdgpu_device *adev)
710 {
711 
712 	adev->crtc_irq.num_types = adev->mode_info.num_crtc;
713 	adev->crtc_irq.funcs = &dm_crtc_irq_funcs;
714 
715 	adev->vupdate_irq.num_types = adev->mode_info.num_crtc;
716 	adev->vupdate_irq.funcs = &dm_vupdate_irq_funcs;
717 
718 	adev->pageflip_irq.num_types = adev->mode_info.num_crtc;
719 	adev->pageflip_irq.funcs = &dm_pageflip_irq_funcs;
720 
721 	adev->hpd_irq.num_types = adev->mode_info.num_hpd;
722 	adev->hpd_irq.funcs = &dm_hpd_irq_funcs;
723 }
724 
725 /**
726  * amdgpu_dm_hpd_init - hpd setup callback.
727  *
728  * @adev: amdgpu_device pointer
729  *
730  * Setup the hpd pins used by the card (evergreen+).
731  * Enable the pin, set the polarity, and enable the hpd interrupts.
732  */
733 void amdgpu_dm_hpd_init(struct amdgpu_device *adev)
734 {
735 	struct drm_device *dev = adev->ddev;
736 	struct drm_connector *connector;
737 
738 	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
739 		struct amdgpu_dm_connector *amdgpu_dm_connector =
740 				to_amdgpu_dm_connector(connector);
741 
742 		const struct dc_link *dc_link = amdgpu_dm_connector->dc_link;
743 
744 		if (DC_IRQ_SOURCE_INVALID != dc_link->irq_source_hpd) {
745 			dc_interrupt_set(adev->dm.dc,
746 					dc_link->irq_source_hpd,
747 					true);
748 		}
749 
750 		if (DC_IRQ_SOURCE_INVALID != dc_link->irq_source_hpd_rx) {
751 			dc_interrupt_set(adev->dm.dc,
752 					dc_link->irq_source_hpd_rx,
753 					true);
754 		}
755 	}
756 }
757 
758 /**
759  * amdgpu_dm_hpd_fini - hpd tear down callback.
760  *
761  * @adev: amdgpu_device pointer
762  *
763  * Tear down the hpd pins used by the card (evergreen+).
764  * Disable the hpd interrupts.
765  */
766 void amdgpu_dm_hpd_fini(struct amdgpu_device *adev)
767 {
768 	struct drm_device *dev = adev->ddev;
769 	struct drm_connector *connector;
770 
771 	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
772 		struct amdgpu_dm_connector *amdgpu_dm_connector =
773 				to_amdgpu_dm_connector(connector);
774 		const struct dc_link *dc_link = amdgpu_dm_connector->dc_link;
775 
776 		dc_interrupt_set(adev->dm.dc, dc_link->irq_source_hpd, false);
777 
778 		if (DC_IRQ_SOURCE_INVALID != dc_link->irq_source_hpd_rx) {
779 			dc_interrupt_set(adev->dm.dc,
780 					dc_link->irq_source_hpd_rx,
781 					false);
782 		}
783 	}
784 }
785