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