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 	struct work_struct work;
86 };
87 
88 #define DM_IRQ_TABLE_LOCK(adev, flags) \
89 	spin_lock_irqsave(&adev->dm.irq_handler_list_table_lock, flags)
90 
91 #define DM_IRQ_TABLE_UNLOCK(adev, flags) \
92 	spin_unlock_irqrestore(&adev->dm.irq_handler_list_table_lock, flags)
93 
94 /******************************************************************************
95  * Private functions.
96  *****************************************************************************/
97 
98 static void init_handler_common_data(struct amdgpu_dm_irq_handler_data *hcd,
99 				     void (*ih)(void *),
100 				     void *args,
101 				     struct amdgpu_display_manager *dm)
102 {
103 	hcd->handler = ih;
104 	hcd->handler_arg = args;
105 	hcd->dm = dm;
106 }
107 
108 /**
109  * dm_irq_work_func() - Handle an IRQ outside of the interrupt handler proper.
110  *
111  * @work: work struct
112  */
113 static void dm_irq_work_func(struct work_struct *work)
114 {
115 	struct amdgpu_dm_irq_handler_data *handler_data =
116 		container_of(work, struct amdgpu_dm_irq_handler_data, work);
117 
118 	handler_data->handler(handler_data->handler_arg);
119 
120 	/* Call a DAL subcomponent which registered for interrupt notification
121 	 * at INTERRUPT_LOW_IRQ_CONTEXT.
122 	 * (The most common use is HPD interrupt) */
123 }
124 
125 /*
126  * Remove a handler and return a pointer to handler list from which the
127  * handler was removed.
128  */
129 static struct list_head *remove_irq_handler(struct amdgpu_device *adev,
130 					    void *ih,
131 					    const struct dc_interrupt_params *int_params)
132 {
133 	struct list_head *hnd_list;
134 	struct list_head *entry, *tmp;
135 	struct amdgpu_dm_irq_handler_data *handler;
136 	unsigned long irq_table_flags;
137 	bool handler_removed = false;
138 	enum dc_irq_source irq_source;
139 
140 	DM_IRQ_TABLE_LOCK(adev, irq_table_flags);
141 
142 	irq_source = int_params->irq_source;
143 
144 	switch (int_params->int_context) {
145 	case INTERRUPT_HIGH_IRQ_CONTEXT:
146 		hnd_list = &adev->dm.irq_handler_list_high_tab[irq_source];
147 		break;
148 	case INTERRUPT_LOW_IRQ_CONTEXT:
149 	default:
150 		hnd_list = &adev->dm.irq_handler_list_low_tab[irq_source];
151 		break;
152 	}
153 
154 	list_for_each_safe(entry, tmp, hnd_list) {
155 
156 		handler = list_entry(entry, struct amdgpu_dm_irq_handler_data,
157 				     list);
158 
159 		if (handler == NULL)
160 			continue;
161 
162 		if (ih == handler->handler) {
163 			/* Found our handler. Remove it from the list. */
164 			list_del(&handler->list);
165 			handler_removed = true;
166 			break;
167 		}
168 	}
169 
170 	DM_IRQ_TABLE_UNLOCK(adev, irq_table_flags);
171 
172 	if (handler_removed == false) {
173 		/* Not necessarily an error - caller may not
174 		 * know the context. */
175 		return NULL;
176 	}
177 
178 	kfree(handler);
179 
180 	DRM_DEBUG_KMS(
181 	"DM_IRQ: removed irq handler: %p for: dal_src=%d, irq context=%d\n",
182 		ih, int_params->irq_source, int_params->int_context);
183 
184 	return hnd_list;
185 }
186 
187 static bool
188 validate_irq_registration_params(struct dc_interrupt_params *int_params,
189 				 void (*ih)(void *))
190 {
191 	if (NULL == int_params || NULL == ih) {
192 		DRM_ERROR("DM_IRQ: invalid input!\n");
193 		return false;
194 	}
195 
196 	if (int_params->int_context >= INTERRUPT_CONTEXT_NUMBER) {
197 		DRM_ERROR("DM_IRQ: invalid context: %d!\n",
198 				int_params->int_context);
199 		return false;
200 	}
201 
202 	if (!DAL_VALID_IRQ_SRC_NUM(int_params->irq_source)) {
203 		DRM_ERROR("DM_IRQ: invalid irq_source: %d!\n",
204 				int_params->irq_source);
205 		return false;
206 	}
207 
208 	return true;
209 }
210 
211 static bool validate_irq_unregistration_params(enum dc_irq_source irq_source,
212 					       irq_handler_idx handler_idx)
213 {
214 	if (DAL_INVALID_IRQ_HANDLER_IDX == handler_idx) {
215 		DRM_ERROR("DM_IRQ: invalid handler_idx==NULL!\n");
216 		return false;
217 	}
218 
219 	if (!DAL_VALID_IRQ_SRC_NUM(irq_source)) {
220 		DRM_ERROR("DM_IRQ: invalid irq_source:%d!\n", irq_source);
221 		return false;
222 	}
223 
224 	return true;
225 }
226 /******************************************************************************
227  * Public functions.
228  *
229  * Note: caller is responsible for input validation.
230  *****************************************************************************/
231 
232 /**
233  * amdgpu_dm_irq_register_interrupt() - Register a handler within DM.
234  * @adev: The base driver device containing the DM device.
235  * @int_params: Interrupt parameters containing the source, and handler context
236  * @ih: Function pointer to the interrupt handler to register
237  * @handler_args: Arguments passed to the handler when the interrupt occurs
238  *
239  * Register an interrupt handler for the given IRQ source, under the given
240  * context. The context can either be high or low. High context handlers are
241  * executed directly within ISR context, while low context is executed within a
242  * workqueue, thereby allowing operations that sleep.
243  *
244  * Registered handlers are called in a FIFO manner, i.e. the most recently
245  * registered handler will be called first.
246  *
247  * Return: Handler data &struct amdgpu_dm_irq_handler_data containing the IRQ
248  *         source, handler function, and args
249  */
250 void *amdgpu_dm_irq_register_interrupt(struct amdgpu_device *adev,
251 				       struct dc_interrupt_params *int_params,
252 				       void (*ih)(void *),
253 				       void *handler_args)
254 {
255 	struct list_head *hnd_list;
256 	struct amdgpu_dm_irq_handler_data *handler_data;
257 	unsigned long irq_table_flags;
258 	enum dc_irq_source irq_source;
259 
260 	if (false == validate_irq_registration_params(int_params, ih))
261 		return DAL_INVALID_IRQ_HANDLER_IDX;
262 
263 	handler_data = kzalloc(sizeof(*handler_data), GFP_KERNEL);
264 	if (!handler_data) {
265 		DRM_ERROR("DM_IRQ: failed to allocate irq handler!\n");
266 		return DAL_INVALID_IRQ_HANDLER_IDX;
267 	}
268 
269 	init_handler_common_data(handler_data, ih, handler_args, &adev->dm);
270 
271 	irq_source = int_params->irq_source;
272 
273 	handler_data->irq_source = irq_source;
274 
275 	/* Lock the list, add the handler. */
276 	DM_IRQ_TABLE_LOCK(adev, irq_table_flags);
277 
278 	switch (int_params->int_context) {
279 	case INTERRUPT_HIGH_IRQ_CONTEXT:
280 		hnd_list = &adev->dm.irq_handler_list_high_tab[irq_source];
281 		break;
282 	case INTERRUPT_LOW_IRQ_CONTEXT:
283 	default:
284 		hnd_list = &adev->dm.irq_handler_list_low_tab[irq_source];
285 		INIT_WORK(&handler_data->work, dm_irq_work_func);
286 		break;
287 	}
288 
289 	list_add_tail(&handler_data->list, hnd_list);
290 
291 	DM_IRQ_TABLE_UNLOCK(adev, irq_table_flags);
292 
293 	/* This pointer will be stored by code which requested interrupt
294 	 * registration.
295 	 * The same pointer will be needed in order to unregister the
296 	 * interrupt. */
297 
298 	DRM_DEBUG_KMS(
299 		"DM_IRQ: added irq handler: %p for: dal_src=%d, irq context=%d\n",
300 		handler_data,
301 		irq_source,
302 		int_params->int_context);
303 
304 	return handler_data;
305 }
306 
307 /**
308  * amdgpu_dm_irq_unregister_interrupt() - Remove a handler from the DM IRQ table
309  * @adev: The base driver device containing the DM device
310  * @irq_source: IRQ source to remove the given handler from
311  * @ih: Function pointer to the interrupt handler to unregister
312  *
313  * Go through both low and high context IRQ tables, and find the given handler
314  * for the given irq source. If found, remove it. Otherwise, do nothing.
315  */
316 void amdgpu_dm_irq_unregister_interrupt(struct amdgpu_device *adev,
317 					enum dc_irq_source irq_source,
318 					void *ih)
319 {
320 	struct list_head *handler_list;
321 	struct dc_interrupt_params int_params;
322 	int i;
323 
324 	if (false == validate_irq_unregistration_params(irq_source, ih))
325 		return;
326 
327 	memset(&int_params, 0, sizeof(int_params));
328 
329 	int_params.irq_source = irq_source;
330 
331 	for (i = 0; i < INTERRUPT_CONTEXT_NUMBER; i++) {
332 
333 		int_params.int_context = i;
334 
335 		handler_list = remove_irq_handler(adev, ih, &int_params);
336 
337 		if (handler_list != NULL)
338 			break;
339 	}
340 
341 	if (handler_list == NULL) {
342 		/* If we got here, it means we searched all irq contexts
343 		 * for this irq source, but the handler was not found. */
344 		DRM_ERROR(
345 		"DM_IRQ: failed to find irq handler:%p for irq_source:%d!\n",
346 			ih, irq_source);
347 	}
348 }
349 
350 /**
351  * amdgpu_dm_irq_init() - Initialize DM IRQ management
352  * @adev:  The base driver device containing the DM device
353  *
354  * Initialize DM's high and low context IRQ tables.
355  *
356  * The N by M table contains N IRQ sources, with M
357  * &struct amdgpu_dm_irq_handler_data hooked together in a linked list. The
358  * list_heads are initialized here. When an interrupt n is triggered, all m
359  * handlers are called in sequence, FIFO according to registration order.
360  *
361  * The low context table requires special steps to initialize, since handlers
362  * will be deferred to a workqueue. See &struct irq_list_head.
363  */
364 int amdgpu_dm_irq_init(struct amdgpu_device *adev)
365 {
366 	int src;
367 	struct list_head *lh;
368 
369 	DRM_DEBUG_KMS("DM_IRQ\n");
370 
371 	spin_lock_init(&adev->dm.irq_handler_list_table_lock);
372 
373 	for (src = 0; src < DAL_IRQ_SOURCES_NUMBER; src++) {
374 		/* low context handler list init */
375 		lh = &adev->dm.irq_handler_list_low_tab[src];
376 		INIT_LIST_HEAD(lh);
377 		/* high context handler init */
378 		INIT_LIST_HEAD(&adev->dm.irq_handler_list_high_tab[src]);
379 	}
380 
381 	return 0;
382 }
383 
384 /**
385  * amdgpu_dm_irq_fini() - Tear down DM IRQ management
386  * @adev: The base driver device containing the DM device
387  *
388  * Flush all work within the low context IRQ table.
389  */
390 void amdgpu_dm_irq_fini(struct amdgpu_device *adev)
391 {
392 	int src;
393 	struct list_head *lh;
394 	struct list_head *entry, *tmp;
395 	struct amdgpu_dm_irq_handler_data *handler;
396 	unsigned long irq_table_flags;
397 
398 	DRM_DEBUG_KMS("DM_IRQ: releasing resources.\n");
399 	for (src = 0; src < DAL_IRQ_SOURCES_NUMBER; src++) {
400 		DM_IRQ_TABLE_LOCK(adev, irq_table_flags);
401 		/* The handler was removed from the table,
402 		 * it means it is safe to flush all the 'work'
403 		 * (because no code can schedule a new one). */
404 		lh = &adev->dm.irq_handler_list_low_tab[src];
405 		DM_IRQ_TABLE_UNLOCK(adev, irq_table_flags);
406 
407 		if (!list_empty(lh)) {
408 			list_for_each_safe(entry, tmp, lh) {
409 				handler = list_entry(
410 					entry,
411 					struct amdgpu_dm_irq_handler_data,
412 					list);
413 				flush_work(&handler->work);
414 			}
415 		}
416 	}
417 }
418 
419 int amdgpu_dm_irq_suspend(struct amdgpu_device *adev)
420 {
421 	int src;
422 	struct list_head *hnd_list_h;
423 	struct list_head *hnd_list_l;
424 	unsigned long irq_table_flags;
425 	struct list_head *entry, *tmp;
426 	struct amdgpu_dm_irq_handler_data *handler;
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];
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 
444 		if (!list_empty(hnd_list_l)) {
445 			list_for_each_safe (entry, tmp, hnd_list_l) {
446 				handler = list_entry(
447 					entry,
448 					struct amdgpu_dm_irq_handler_data,
449 					list);
450 				flush_work(&handler->work);
451 			}
452 		}
453 		DM_IRQ_TABLE_LOCK(adev, irq_table_flags);
454 	}
455 
456 	DM_IRQ_TABLE_UNLOCK(adev, irq_table_flags);
457 	return 0;
458 }
459 
460 int amdgpu_dm_irq_resume_early(struct amdgpu_device *adev)
461 {
462 	int src;
463 	struct list_head *hnd_list_h, *hnd_list_l;
464 	unsigned long irq_table_flags;
465 
466 	DM_IRQ_TABLE_LOCK(adev, irq_table_flags);
467 
468 	DRM_DEBUG_KMS("DM_IRQ: early resume\n");
469 
470 	/* re-enable short pulse interrupts HW interrupt */
471 	for (src = DC_IRQ_SOURCE_HPD1RX; src <= DC_IRQ_SOURCE_HPD6RX; src++) {
472 		hnd_list_l = &adev->dm.irq_handler_list_low_tab[src];
473 		hnd_list_h = &adev->dm.irq_handler_list_high_tab[src];
474 		if (!list_empty(hnd_list_l) || !list_empty(hnd_list_h))
475 			dc_interrupt_set(adev->dm.dc, src, true);
476 	}
477 
478 	DM_IRQ_TABLE_UNLOCK(adev, irq_table_flags);
479 
480 	return 0;
481 }
482 
483 int amdgpu_dm_irq_resume_late(struct amdgpu_device *adev)
484 {
485 	int src;
486 	struct list_head *hnd_list_h, *hnd_list_l;
487 	unsigned long irq_table_flags;
488 
489 	DM_IRQ_TABLE_LOCK(adev, irq_table_flags);
490 
491 	DRM_DEBUG_KMS("DM_IRQ: resume\n");
492 
493 	/**
494 	 * Renable HW interrupt  for HPD and only since FLIP and VBLANK
495 	 * will be enabled from manage_dm_interrupts on enable CRTC.
496 	 */
497 	for (src = DC_IRQ_SOURCE_HPD1; src <= DC_IRQ_SOURCE_HPD6; src++) {
498 		hnd_list_l = &adev->dm.irq_handler_list_low_tab[src];
499 		hnd_list_h = &adev->dm.irq_handler_list_high_tab[src];
500 		if (!list_empty(hnd_list_l) || !list_empty(hnd_list_h))
501 			dc_interrupt_set(adev->dm.dc, src, true);
502 	}
503 
504 	DM_IRQ_TABLE_UNLOCK(adev, irq_table_flags);
505 	return 0;
506 }
507 
508 /*
509  * amdgpu_dm_irq_schedule_work - schedule all work items registered for the
510  * "irq_source".
511  */
512 static void amdgpu_dm_irq_schedule_work(struct amdgpu_device *adev,
513 					enum dc_irq_source irq_source)
514 {
515 	struct  list_head *handler_list = &adev->dm.irq_handler_list_low_tab[irq_source];
516 	struct  amdgpu_dm_irq_handler_data *handler_data;
517 	bool    work_queued = false;
518 
519 	if (list_empty(handler_list))
520 		return;
521 
522 	list_for_each_entry (handler_data, handler_list, list) {
523 		if (!queue_work(system_highpri_wq, &handler_data->work)) {
524 			continue;
525 		} else {
526 			work_queued = true;
527 			break;
528 		}
529 	}
530 
531 	if (!work_queued) {
532 		struct  amdgpu_dm_irq_handler_data *handler_data_add;
533 		/*get the amdgpu_dm_irq_handler_data of first item pointed by handler_list*/
534 		handler_data = container_of(handler_list->next, struct amdgpu_dm_irq_handler_data, list);
535 
536 		/*allocate a new amdgpu_dm_irq_handler_data*/
537 		handler_data_add = kzalloc(sizeof(*handler_data), GFP_KERNEL);
538 		if (!handler_data_add) {
539 			DRM_ERROR("DM_IRQ: failed to allocate irq handler!\n");
540 			return;
541 		}
542 
543 		/*copy new amdgpu_dm_irq_handler_data members from handler_data*/
544 		handler_data_add->handler       = handler_data->handler;
545 		handler_data_add->handler_arg   = handler_data->handler_arg;
546 		handler_data_add->dm            = handler_data->dm;
547 		handler_data_add->irq_source    = irq_source;
548 
549 		list_add_tail(&handler_data_add->list, handler_list);
550 
551 		INIT_WORK(&handler_data_add->work, dm_irq_work_func);
552 
553 		if (queue_work(system_highpri_wq, &handler_data_add->work))
554 			DRM_DEBUG("Queued work for handling interrupt from "
555 				  "display for IRQ source %d\n",
556 				  irq_source);
557 		else
558 			DRM_ERROR("Failed to queue work for handling interrupt "
559 				  "from display for IRQ source %d\n",
560 				  irq_source);
561 	}
562 }
563 
564 /*
565  * amdgpu_dm_irq_immediate_work
566  * Callback high irq work immediately, don't send to work queue
567  */
568 static void amdgpu_dm_irq_immediate_work(struct amdgpu_device *adev,
569 					 enum dc_irq_source irq_source)
570 {
571 	struct amdgpu_dm_irq_handler_data *handler_data;
572 	unsigned long irq_table_flags;
573 
574 	DM_IRQ_TABLE_LOCK(adev, irq_table_flags);
575 
576 	list_for_each_entry(handler_data,
577 			    &adev->dm.irq_handler_list_high_tab[irq_source],
578 			    list) {
579 		/* Call a subcomponent which registered for immediate
580 		 * interrupt notification */
581 		handler_data->handler(handler_data->handler_arg);
582 	}
583 
584 	DM_IRQ_TABLE_UNLOCK(adev, irq_table_flags);
585 }
586 
587 /**
588  * amdgpu_dm_irq_handler - Generic DM IRQ handler
589  * @adev: amdgpu base driver device containing the DM device
590  * @source: Unused
591  * @entry: Data about the triggered interrupt
592  *
593  * Calls all registered high irq work immediately, and schedules work for low
594  * irq. The DM IRQ table is used to find the corresponding handlers.
595  */
596 static int amdgpu_dm_irq_handler(struct amdgpu_device *adev,
597 				 struct amdgpu_irq_src *source,
598 				 struct amdgpu_iv_entry *entry)
599 {
600 
601 	enum dc_irq_source src =
602 		dc_interrupt_to_irq_source(
603 			adev->dm.dc,
604 			entry->src_id,
605 			entry->src_data[0]);
606 
607 	dc_interrupt_ack(adev->dm.dc, src);
608 
609 	/* Call high irq work immediately */
610 	amdgpu_dm_irq_immediate_work(adev, src);
611 	/*Schedule low_irq work */
612 	amdgpu_dm_irq_schedule_work(adev, src);
613 
614 	return 0;
615 }
616 
617 static enum dc_irq_source amdgpu_dm_hpd_to_dal_irq_source(unsigned type)
618 {
619 	switch (type) {
620 	case AMDGPU_HPD_1:
621 		return DC_IRQ_SOURCE_HPD1;
622 	case AMDGPU_HPD_2:
623 		return DC_IRQ_SOURCE_HPD2;
624 	case AMDGPU_HPD_3:
625 		return DC_IRQ_SOURCE_HPD3;
626 	case AMDGPU_HPD_4:
627 		return DC_IRQ_SOURCE_HPD4;
628 	case AMDGPU_HPD_5:
629 		return DC_IRQ_SOURCE_HPD5;
630 	case AMDGPU_HPD_6:
631 		return DC_IRQ_SOURCE_HPD6;
632 	default:
633 		return DC_IRQ_SOURCE_INVALID;
634 	}
635 }
636 
637 static int amdgpu_dm_set_hpd_irq_state(struct amdgpu_device *adev,
638 				       struct amdgpu_irq_src *source,
639 				       unsigned type,
640 				       enum amdgpu_interrupt_state state)
641 {
642 	enum dc_irq_source src = amdgpu_dm_hpd_to_dal_irq_source(type);
643 	bool st = (state == AMDGPU_IRQ_STATE_ENABLE);
644 
645 	dc_interrupt_set(adev->dm.dc, src, st);
646 	return 0;
647 }
648 
649 static inline int dm_irq_state(struct amdgpu_device *adev,
650 			       struct amdgpu_irq_src *source,
651 			       unsigned crtc_id,
652 			       enum amdgpu_interrupt_state state,
653 			       const enum irq_type dal_irq_type,
654 			       const char *func)
655 {
656 	bool st;
657 	enum dc_irq_source irq_source;
658 
659 	struct amdgpu_crtc *acrtc = adev->mode_info.crtcs[crtc_id];
660 
661 	if (!acrtc) {
662 		DRM_ERROR(
663 			"%s: crtc is NULL at id :%d\n",
664 			func,
665 			crtc_id);
666 		return 0;
667 	}
668 
669 	if (acrtc->otg_inst == -1)
670 		return 0;
671 
672 	irq_source = dal_irq_type + acrtc->otg_inst;
673 
674 	st = (state == AMDGPU_IRQ_STATE_ENABLE);
675 
676 	dc_interrupt_set(adev->dm.dc, irq_source, st);
677 	return 0;
678 }
679 
680 static int amdgpu_dm_set_pflip_irq_state(struct amdgpu_device *adev,
681 					 struct amdgpu_irq_src *source,
682 					 unsigned crtc_id,
683 					 enum amdgpu_interrupt_state state)
684 {
685 	return dm_irq_state(
686 		adev,
687 		source,
688 		crtc_id,
689 		state,
690 		IRQ_TYPE_PFLIP,
691 		__func__);
692 }
693 
694 static int amdgpu_dm_set_crtc_irq_state(struct amdgpu_device *adev,
695 					struct amdgpu_irq_src *source,
696 					unsigned crtc_id,
697 					enum amdgpu_interrupt_state state)
698 {
699 	return dm_irq_state(
700 		adev,
701 		source,
702 		crtc_id,
703 		state,
704 		IRQ_TYPE_VBLANK,
705 		__func__);
706 }
707 
708 static int amdgpu_dm_set_vline0_irq_state(struct amdgpu_device *adev,
709 					struct amdgpu_irq_src *source,
710 					unsigned int crtc_id,
711 					enum amdgpu_interrupt_state state)
712 {
713 	return dm_irq_state(
714 		adev,
715 		source,
716 		crtc_id,
717 		state,
718 		IRQ_TYPE_VLINE0,
719 		__func__);
720 }
721 
722 static int amdgpu_dm_set_vupdate_irq_state(struct amdgpu_device *adev,
723 					   struct amdgpu_irq_src *source,
724 					   unsigned int crtc_id,
725 					   enum amdgpu_interrupt_state state)
726 {
727 	return dm_irq_state(
728 		adev,
729 		source,
730 		crtc_id,
731 		state,
732 		IRQ_TYPE_VUPDATE,
733 		__func__);
734 }
735 
736 static const struct amdgpu_irq_src_funcs dm_crtc_irq_funcs = {
737 	.set = amdgpu_dm_set_crtc_irq_state,
738 	.process = amdgpu_dm_irq_handler,
739 };
740 
741 static const struct amdgpu_irq_src_funcs dm_vline0_irq_funcs = {
742 	.set = amdgpu_dm_set_vline0_irq_state,
743 	.process = amdgpu_dm_irq_handler,
744 };
745 
746 static const struct amdgpu_irq_src_funcs dm_vupdate_irq_funcs = {
747 	.set = amdgpu_dm_set_vupdate_irq_state,
748 	.process = amdgpu_dm_irq_handler,
749 };
750 
751 static const struct amdgpu_irq_src_funcs dm_pageflip_irq_funcs = {
752 	.set = amdgpu_dm_set_pflip_irq_state,
753 	.process = amdgpu_dm_irq_handler,
754 };
755 
756 static const struct amdgpu_irq_src_funcs dm_hpd_irq_funcs = {
757 	.set = amdgpu_dm_set_hpd_irq_state,
758 	.process = amdgpu_dm_irq_handler,
759 };
760 
761 void amdgpu_dm_set_irq_funcs(struct amdgpu_device *adev)
762 {
763 
764 	adev->crtc_irq.num_types = adev->mode_info.num_crtc;
765 	adev->crtc_irq.funcs = &dm_crtc_irq_funcs;
766 
767 	adev->vline0_irq.num_types = adev->mode_info.num_crtc;
768 	adev->vline0_irq.funcs = &dm_vline0_irq_funcs;
769 
770 	adev->vupdate_irq.num_types = adev->mode_info.num_crtc;
771 	adev->vupdate_irq.funcs = &dm_vupdate_irq_funcs;
772 
773 	adev->pageflip_irq.num_types = adev->mode_info.num_crtc;
774 	adev->pageflip_irq.funcs = &dm_pageflip_irq_funcs;
775 
776 	adev->hpd_irq.num_types = adev->mode_info.num_hpd;
777 	adev->hpd_irq.funcs = &dm_hpd_irq_funcs;
778 }
779 
780 /**
781  * amdgpu_dm_hpd_init - hpd setup callback.
782  *
783  * @adev: amdgpu_device pointer
784  *
785  * Setup the hpd pins used by the card (evergreen+).
786  * Enable the pin, set the polarity, and enable the hpd interrupts.
787  */
788 void amdgpu_dm_hpd_init(struct amdgpu_device *adev)
789 {
790 	struct drm_device *dev = adev_to_drm(adev);
791 	struct drm_connector *connector;
792 	struct drm_connector_list_iter iter;
793 
794 	drm_connector_list_iter_begin(dev, &iter);
795 	drm_for_each_connector_iter(connector, &iter) {
796 		struct amdgpu_dm_connector *amdgpu_dm_connector =
797 				to_amdgpu_dm_connector(connector);
798 
799 		const struct dc_link *dc_link = amdgpu_dm_connector->dc_link;
800 
801 		if (DC_IRQ_SOURCE_INVALID != dc_link->irq_source_hpd) {
802 			dc_interrupt_set(adev->dm.dc,
803 					dc_link->irq_source_hpd,
804 					true);
805 		}
806 
807 		if (DC_IRQ_SOURCE_INVALID != dc_link->irq_source_hpd_rx) {
808 			dc_interrupt_set(adev->dm.dc,
809 					dc_link->irq_source_hpd_rx,
810 					true);
811 		}
812 	}
813 	drm_connector_list_iter_end(&iter);
814 }
815 
816 /**
817  * amdgpu_dm_hpd_fini - hpd tear down callback.
818  *
819  * @adev: amdgpu_device pointer
820  *
821  * Tear down the hpd pins used by the card (evergreen+).
822  * Disable the hpd interrupts.
823  */
824 void amdgpu_dm_hpd_fini(struct amdgpu_device *adev)
825 {
826 	struct drm_device *dev = adev_to_drm(adev);
827 	struct drm_connector *connector;
828 	struct drm_connector_list_iter iter;
829 
830 	drm_connector_list_iter_begin(dev, &iter);
831 	drm_for_each_connector_iter(connector, &iter) {
832 		struct amdgpu_dm_connector *amdgpu_dm_connector =
833 				to_amdgpu_dm_connector(connector);
834 		const struct dc_link *dc_link = amdgpu_dm_connector->dc_link;
835 
836 		dc_interrupt_set(adev->dm.dc, dc_link->irq_source_hpd, false);
837 
838 		if (DC_IRQ_SOURCE_INVALID != dc_link->irq_source_hpd_rx) {
839 			dc_interrupt_set(adev->dm.dc,
840 					dc_link->irq_source_hpd_rx,
841 					false);
842 		}
843 	}
844 	drm_connector_list_iter_end(&iter);
845 }
846