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 <drm/drmP.h>
27 
28 #include "dm_services_types.h"
29 #include "dc.h"
30 
31 #include "amdgpu.h"
32 #include "amdgpu_dm.h"
33 #include "amdgpu_dm_irq.h"
34 
35 /**
36  * DOC: overview
37  *
38  * DM provides another layer of IRQ management on top of what the base driver
39  * already provides. This is something that could be cleaned up, and is a
40  * future TODO item.
41  *
42  * The base driver provides IRQ source registration with DRM, handler
43  * registration into the base driver's IRQ table, and a handler callback
44  * amdgpu_irq_handler(), with which DRM calls on interrupts. This generic
45  * handler looks up the IRQ table, and calls the respective
46  * &amdgpu_irq_src_funcs.process hookups.
47  *
48  * What DM provides on top are two IRQ tables specifically for top-half and
49  * bottom-half IRQ handling, with the bottom-half implementing workqueues:
50  *
51  * - &amdgpu_display_manager.irq_handler_list_high_tab
52  * - &amdgpu_display_manager.irq_handler_list_low_tab
53  *
54  * They override the base driver's IRQ table, and the effect can be seen
55  * in the hooks that DM provides for &amdgpu_irq_src_funcs.process. They
56  * are all set to the DM generic handler amdgpu_dm_irq_handler(), which looks up
57  * DM's IRQ tables. However, in order for base driver to recognize this hook, DM
58  * still needs to register the IRQ with the base driver. See
59  * dce110_register_irq_handlers() and dcn10_register_irq_handlers().
60  *
61  * To expose DC's hardware interrupt toggle to the base driver, DM implements
62  * &amdgpu_irq_src_funcs.set hooks. Base driver calls it through
63  * amdgpu_irq_update() to enable or disable the interrupt.
64  */
65 
66 /******************************************************************************
67  * Private declarations.
68  *****************************************************************************/
69 
70 /**
71  * struct amdgpu_dm_irq_handler_data - Data for DM interrupt handlers.
72  *
73  * @list: Linked list entry referencing the next/previous handler
74  * @handler: Handler function
75  * @handler_arg: Argument passed to the handler when triggered
76  * @dm: DM which this handler belongs to
77  * @irq_source: DC interrupt source that this handler is registered for
78  */
79 struct amdgpu_dm_irq_handler_data {
80 	struct list_head list;
81 	interrupt_handler handler;
82 	void *handler_arg;
83 
84 	struct amdgpu_display_manager *dm;
85 	/* DAL irq source which registered for this interrupt. */
86 	enum dc_irq_source irq_source;
87 };
88 
89 #define DM_IRQ_TABLE_LOCK(adev, flags) \
90 	spin_lock_irqsave(&adev->dm.irq_handler_list_table_lock, flags)
91 
92 #define DM_IRQ_TABLE_UNLOCK(adev, flags) \
93 	spin_unlock_irqrestore(&adev->dm.irq_handler_list_table_lock, flags)
94 
95 /******************************************************************************
96  * Private functions.
97  *****************************************************************************/
98 
99 static void init_handler_common_data(struct amdgpu_dm_irq_handler_data *hcd,
100 				     void (*ih)(void *),
101 				     void *args,
102 				     struct amdgpu_display_manager *dm)
103 {
104 	hcd->handler = ih;
105 	hcd->handler_arg = args;
106 	hcd->dm = dm;
107 }
108 
109 /**
110  * dm_irq_work_func() - Handle an IRQ outside of the interrupt handler proper.
111  *
112  * @work: work struct
113  */
114 static void dm_irq_work_func(struct work_struct *work)
115 {
116 	struct list_head *entry;
117 	struct irq_list_head *irq_list_head =
118 		container_of(work, struct irq_list_head, work);
119 	struct list_head *handler_list = &irq_list_head->head;
120 	struct amdgpu_dm_irq_handler_data *handler_data;
121 
122 	list_for_each(entry, handler_list) {
123 		handler_data = list_entry(entry,
124 					  struct amdgpu_dm_irq_handler_data,
125 					  list);
126 
127 		DRM_DEBUG_KMS("DM_IRQ: work_func: for dal_src=%d\n",
128 				handler_data->irq_source);
129 
130 		DRM_DEBUG_KMS("DM_IRQ: schedule_work: for dal_src=%d\n",
131 			handler_data->irq_source);
132 
133 		handler_data->handler(handler_data->handler_arg);
134 	}
135 
136 	/* Call a DAL subcomponent which registered for interrupt notification
137 	 * at INTERRUPT_LOW_IRQ_CONTEXT.
138 	 * (The most common use is HPD interrupt) */
139 }
140 
141 /*
142  * Remove a handler and return a pointer to handler list from which the
143  * handler was removed.
144  */
145 static struct list_head *remove_irq_handler(struct amdgpu_device *adev,
146 					    void *ih,
147 					    const struct dc_interrupt_params *int_params)
148 {
149 	struct list_head *hnd_list;
150 	struct list_head *entry, *tmp;
151 	struct amdgpu_dm_irq_handler_data *handler;
152 	unsigned long irq_table_flags;
153 	bool handler_removed = false;
154 	enum dc_irq_source irq_source;
155 
156 	DM_IRQ_TABLE_LOCK(adev, irq_table_flags);
157 
158 	irq_source = int_params->irq_source;
159 
160 	switch (int_params->int_context) {
161 	case INTERRUPT_HIGH_IRQ_CONTEXT:
162 		hnd_list = &adev->dm.irq_handler_list_high_tab[irq_source];
163 		break;
164 	case INTERRUPT_LOW_IRQ_CONTEXT:
165 	default:
166 		hnd_list = &adev->dm.irq_handler_list_low_tab[irq_source].head;
167 		break;
168 	}
169 
170 	list_for_each_safe(entry, tmp, hnd_list) {
171 
172 		handler = list_entry(entry, struct amdgpu_dm_irq_handler_data,
173 				     list);
174 
175 		if (ih == handler) {
176 			/* Found our handler. Remove it from the list. */
177 			list_del(&handler->list);
178 			handler_removed = true;
179 			break;
180 		}
181 	}
182 
183 	DM_IRQ_TABLE_UNLOCK(adev, irq_table_flags);
184 
185 	if (handler_removed == false) {
186 		/* Not necessarily an error - caller may not
187 		 * know the context. */
188 		return NULL;
189 	}
190 
191 	kfree(handler);
192 
193 	DRM_DEBUG_KMS(
194 	"DM_IRQ: removed irq handler: %p for: dal_src=%d, irq context=%d\n",
195 		ih, int_params->irq_source, int_params->int_context);
196 
197 	return hnd_list;
198 }
199 
200 static bool
201 validate_irq_registration_params(struct dc_interrupt_params *int_params,
202 				 void (*ih)(void *))
203 {
204 	if (NULL == int_params || NULL == ih) {
205 		DRM_ERROR("DM_IRQ: invalid input!\n");
206 		return false;
207 	}
208 
209 	if (int_params->int_context >= INTERRUPT_CONTEXT_NUMBER) {
210 		DRM_ERROR("DM_IRQ: invalid context: %d!\n",
211 				int_params->int_context);
212 		return false;
213 	}
214 
215 	if (!DAL_VALID_IRQ_SRC_NUM(int_params->irq_source)) {
216 		DRM_ERROR("DM_IRQ: invalid irq_source: %d!\n",
217 				int_params->irq_source);
218 		return false;
219 	}
220 
221 	return true;
222 }
223 
224 static bool validate_irq_unregistration_params(enum dc_irq_source irq_source,
225 					       irq_handler_idx handler_idx)
226 {
227 	if (DAL_INVALID_IRQ_HANDLER_IDX == handler_idx) {
228 		DRM_ERROR("DM_IRQ: invalid handler_idx==NULL!\n");
229 		return false;
230 	}
231 
232 	if (!DAL_VALID_IRQ_SRC_NUM(irq_source)) {
233 		DRM_ERROR("DM_IRQ: invalid irq_source:%d!\n", irq_source);
234 		return false;
235 	}
236 
237 	return true;
238 }
239 /******************************************************************************
240  * Public functions.
241  *
242  * Note: caller is responsible for input validation.
243  *****************************************************************************/
244 
245 /**
246  * amdgpu_dm_irq_register_interrupt() - Register a handler within DM.
247  * @adev: The base driver device containing the DM device.
248  * @int_params: Interrupt parameters containing the source, and handler context
249  * @ih: Function pointer to the interrupt handler to register
250  * @handler_args: Arguments passed to the handler when the interrupt occurs
251  *
252  * Register an interrupt handler for the given IRQ source, under the given
253  * context. The context can either be high or low. High context handlers are
254  * executed directly within ISR context, while low context is executed within a
255  * workqueue, thereby allowing operations that sleep.
256  *
257  * Registered handlers are called in a FIFO manner, i.e. the most recently
258  * registered handler will be called first.
259  *
260  * Return: Handler data &struct amdgpu_dm_irq_handler_data containing the IRQ
261  *         source, handler function, and args
262  */
263 void *amdgpu_dm_irq_register_interrupt(struct amdgpu_device *adev,
264 				       struct dc_interrupt_params *int_params,
265 				       void (*ih)(void *),
266 				       void *handler_args)
267 {
268 	struct list_head *hnd_list;
269 	struct amdgpu_dm_irq_handler_data *handler_data;
270 	unsigned long irq_table_flags;
271 	enum dc_irq_source irq_source;
272 
273 	if (false == validate_irq_registration_params(int_params, ih))
274 		return DAL_INVALID_IRQ_HANDLER_IDX;
275 
276 	handler_data = kzalloc(sizeof(*handler_data), GFP_KERNEL);
277 	if (!handler_data) {
278 		DRM_ERROR("DM_IRQ: failed to allocate irq handler!\n");
279 		return DAL_INVALID_IRQ_HANDLER_IDX;
280 	}
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