2 * Copyright 2015 Advanced Micro Devices, Inc.
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
28 #include "dm_services_types.h"
32 #include "amdgpu_dm.h"
33 #include "amdgpu_dm_irq.h"
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
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.
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:
51 * - &amdgpu_display_manager.irq_handler_list_high_tab
52 * - &amdgpu_display_manager.irq_handler_list_low_tab
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().
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.
66 /******************************************************************************
67 * Private declarations.
68 *****************************************************************************/
71 * struct amdgpu_dm_irq_handler_data - Data for DM interrupt handlers.
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
79 struct amdgpu_dm_irq_handler_data {
80 struct list_head list;
81 interrupt_handler handler;
84 struct amdgpu_display_manager *dm;
85 /* DAL irq source which registered for this interrupt. */
86 enum dc_irq_source irq_source;
89 #define DM_IRQ_TABLE_LOCK(adev, flags) \
90 spin_lock_irqsave(&adev->dm.irq_handler_list_table_lock, flags)
92 #define DM_IRQ_TABLE_UNLOCK(adev, flags) \
93 spin_unlock_irqrestore(&adev->dm.irq_handler_list_table_lock, flags)
95 /******************************************************************************
97 *****************************************************************************/
99 static void init_handler_common_data(struct amdgpu_dm_irq_handler_data *hcd,
102 struct amdgpu_display_manager *dm)
105 hcd->handler_arg = args;
110 * dm_irq_work_func() - Handle an IRQ outside of the interrupt handler proper.
114 static void dm_irq_work_func(struct work_struct *work)
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;
122 list_for_each(entry, handler_list) {
123 handler_data = list_entry(entry,
124 struct amdgpu_dm_irq_handler_data,
127 DRM_DEBUG_KMS("DM_IRQ: work_func: for dal_src=%d\n",
128 handler_data->irq_source);
130 DRM_DEBUG_KMS("DM_IRQ: schedule_work: for dal_src=%d\n",
131 handler_data->irq_source);
133 handler_data->handler(handler_data->handler_arg);
136 /* Call a DAL subcomponent which registered for interrupt notification
137 * at INTERRUPT_LOW_IRQ_CONTEXT.
138 * (The most common use is HPD interrupt) */
142 * Remove a handler and return a pointer to handler list from which the
143 * handler was removed.
145 static struct list_head *remove_irq_handler(struct amdgpu_device *adev,
147 const struct dc_interrupt_params *int_params)
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;
156 DM_IRQ_TABLE_LOCK(adev, irq_table_flags);
158 irq_source = int_params->irq_source;
160 switch (int_params->int_context) {
161 case INTERRUPT_HIGH_IRQ_CONTEXT:
162 hnd_list = &adev->dm.irq_handler_list_high_tab[irq_source];
164 case INTERRUPT_LOW_IRQ_CONTEXT:
166 hnd_list = &adev->dm.irq_handler_list_low_tab[irq_source].head;
170 list_for_each_safe(entry, tmp, hnd_list) {
172 handler = list_entry(entry, struct amdgpu_dm_irq_handler_data,
176 /* Found our handler. Remove it from the list. */
177 list_del(&handler->list);
178 handler_removed = true;
183 DM_IRQ_TABLE_UNLOCK(adev, irq_table_flags);
185 if (handler_removed == false) {
186 /* Not necessarily an error - caller may not
187 * know the context. */
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);
201 validate_irq_registration_params(struct dc_interrupt_params *int_params,
204 if (NULL == int_params || NULL == ih) {
205 DRM_ERROR("DM_IRQ: invalid input!\n");
209 if (int_params->int_context >= INTERRUPT_CONTEXT_NUMBER) {
210 DRM_ERROR("DM_IRQ: invalid context: %d!\n",
211 int_params->int_context);
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);
224 static bool validate_irq_unregistration_params(enum dc_irq_source irq_source,
225 irq_handler_idx handler_idx)
227 if (DAL_INVALID_IRQ_HANDLER_IDX == handler_idx) {
228 DRM_ERROR("DM_IRQ: invalid handler_idx==NULL!\n");
232 if (!DAL_VALID_IRQ_SRC_NUM(irq_source)) {
233 DRM_ERROR("DM_IRQ: invalid irq_source:%d!\n", irq_source);
239 /******************************************************************************
242 * Note: caller is responsible for input validation.
243 *****************************************************************************/
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
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.
257 * Registered handlers are called in a FIFO manner, i.e. the most recently
258 * registered handler will be called first.
260 * Return: Handler data &struct amdgpu_dm_irq_handler_data containing the IRQ
261 * source, handler function, and args
263 void *amdgpu_dm_irq_register_interrupt(struct amdgpu_device *adev,
264 struct dc_interrupt_params *int_params,
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;
273 if (false == validate_irq_registration_params(int_params, ih))
274 return DAL_INVALID_IRQ_HANDLER_IDX;
276 handler_data = kzalloc(sizeof(*handler_data), GFP_KERNEL);
278 DRM_ERROR("DM_IRQ: failed to allocate irq handler!\n");
279 return DAL_INVALID_IRQ_HANDLER_IDX;
282 memset(handler_data, 0, sizeof(*handler_data));
284 init_handler_common_data(handler_data, ih, handler_args, &adev->dm);
286 irq_source = int_params->irq_source;
288 handler_data->irq_source = irq_source;
290 /* Lock the list, add the handler. */
291 DM_IRQ_TABLE_LOCK(adev, irq_table_flags);
293 switch (int_params->int_context) {
294 case INTERRUPT_HIGH_IRQ_CONTEXT:
295 hnd_list = &adev->dm.irq_handler_list_high_tab[irq_source];
297 case INTERRUPT_LOW_IRQ_CONTEXT:
299 hnd_list = &adev->dm.irq_handler_list_low_tab[irq_source].head;
303 list_add_tail(&handler_data->list, hnd_list);
305 DM_IRQ_TABLE_UNLOCK(adev, irq_table_flags);
307 /* This pointer will be stored by code which requested interrupt
309 * The same pointer will be needed in order to unregister the
313 "DM_IRQ: added irq handler: %p for: dal_src=%d, irq context=%d\n",
316 int_params->int_context);
322 * amdgpu_dm_irq_unregister_interrupt() - Remove a handler from the DM IRQ table
323 * @adev: The base driver device containing the DM device
324 * @irq_source: IRQ source to remove the given handler from
325 * @ih: Function pointer to the interrupt handler to unregister
327 * Go through both low and high context IRQ tables, and find the given handler
328 * for the given irq source. If found, remove it. Otherwise, do nothing.
330 void amdgpu_dm_irq_unregister_interrupt(struct amdgpu_device *adev,
331 enum dc_irq_source irq_source,
334 struct list_head *handler_list;
335 struct dc_interrupt_params int_params;
338 if (false == validate_irq_unregistration_params(irq_source, ih))
341 memset(&int_params, 0, sizeof(int_params));
343 int_params.irq_source = irq_source;
345 for (i = 0; i < INTERRUPT_CONTEXT_NUMBER; i++) {
347 int_params.int_context = i;
349 handler_list = remove_irq_handler(adev, ih, &int_params);
351 if (handler_list != NULL)
355 if (handler_list == NULL) {
356 /* If we got here, it means we searched all irq contexts
357 * for this irq source, but the handler was not found. */
359 "DM_IRQ: failed to find irq handler:%p for irq_source:%d!\n",
365 * amdgpu_dm_irq_init() - Initialize DM IRQ management
366 * @adev: The base driver device containing the DM device
368 * Initialize DM's high and low context IRQ tables.
370 * The N by M table contains N IRQ sources, with M
371 * &struct amdgpu_dm_irq_handler_data hooked together in a linked list. The
372 * list_heads are initialized here. When an interrupt n is triggered, all m
373 * handlers are called in sequence, FIFO according to registration order.
375 * The low context table requires special steps to initialize, since handlers
376 * will be deferred to a workqueue. See &struct irq_list_head.
378 int amdgpu_dm_irq_init(struct amdgpu_device *adev)
381 struct irq_list_head *lh;
383 DRM_DEBUG_KMS("DM_IRQ\n");
385 spin_lock_init(&adev->dm.irq_handler_list_table_lock);
387 for (src = 0; src < DAL_IRQ_SOURCES_NUMBER; src++) {
388 /* low context handler list init */
389 lh = &adev->dm.irq_handler_list_low_tab[src];
390 INIT_LIST_HEAD(&lh->head);
391 INIT_WORK(&lh->work, dm_irq_work_func);
393 /* high context handler init */
394 INIT_LIST_HEAD(&adev->dm.irq_handler_list_high_tab[src]);
401 * amdgpu_dm_irq_fini() - Tear down DM IRQ management
402 * @adev: The base driver device containing the DM device
404 * Flush all work within the low context IRQ table.
406 void amdgpu_dm_irq_fini(struct amdgpu_device *adev)
409 struct irq_list_head *lh;
410 unsigned long irq_table_flags;
411 DRM_DEBUG_KMS("DM_IRQ: releasing resources.\n");
412 for (src = 0; src < DAL_IRQ_SOURCES_NUMBER; src++) {
413 DM_IRQ_TABLE_LOCK(adev, irq_table_flags);
414 /* The handler was removed from the table,
415 * it means it is safe to flush all the 'work'
416 * (because no code can schedule a new one). */
417 lh = &adev->dm.irq_handler_list_low_tab[src];
418 DM_IRQ_TABLE_UNLOCK(adev, irq_table_flags);
419 flush_work(&lh->work);
423 int amdgpu_dm_irq_suspend(struct amdgpu_device *adev)
426 struct list_head *hnd_list_h;
427 struct list_head *hnd_list_l;
428 unsigned long irq_table_flags;
430 DM_IRQ_TABLE_LOCK(adev, irq_table_flags);
432 DRM_DEBUG_KMS("DM_IRQ: suspend\n");
435 * Disable HW interrupt for HPD and HPDRX only since FLIP and VBLANK
436 * will be disabled from manage_dm_interrupts on disable CRTC.
438 for (src = DC_IRQ_SOURCE_HPD1; src <= DC_IRQ_SOURCE_HPD6RX; src++) {
439 hnd_list_l = &adev->dm.irq_handler_list_low_tab[src].head;
440 hnd_list_h = &adev->dm.irq_handler_list_high_tab[src];
441 if (!list_empty(hnd_list_l) || !list_empty(hnd_list_h))
442 dc_interrupt_set(adev->dm.dc, src, false);
444 DM_IRQ_TABLE_UNLOCK(adev, irq_table_flags);
445 flush_work(&adev->dm.irq_handler_list_low_tab[src].work);
447 DM_IRQ_TABLE_LOCK(adev, irq_table_flags);
450 DM_IRQ_TABLE_UNLOCK(adev, irq_table_flags);
454 int amdgpu_dm_irq_resume_early(struct amdgpu_device *adev)
457 struct list_head *hnd_list_h, *hnd_list_l;
458 unsigned long irq_table_flags;
460 DM_IRQ_TABLE_LOCK(adev, irq_table_flags);
462 DRM_DEBUG_KMS("DM_IRQ: early resume\n");
464 /* re-enable short pulse interrupts HW interrupt */
465 for (src = DC_IRQ_SOURCE_HPD1RX; src <= DC_IRQ_SOURCE_HPD6RX; src++) {
466 hnd_list_l = &adev->dm.irq_handler_list_low_tab[src].head;
467 hnd_list_h = &adev->dm.irq_handler_list_high_tab[src];
468 if (!list_empty(hnd_list_l) || !list_empty(hnd_list_h))
469 dc_interrupt_set(adev->dm.dc, src, true);
472 DM_IRQ_TABLE_UNLOCK(adev, irq_table_flags);
477 int amdgpu_dm_irq_resume_late(struct amdgpu_device *adev)
480 struct list_head *hnd_list_h, *hnd_list_l;
481 unsigned long irq_table_flags;
483 DM_IRQ_TABLE_LOCK(adev, irq_table_flags);
485 DRM_DEBUG_KMS("DM_IRQ: resume\n");
488 * Renable HW interrupt for HPD and only since FLIP and VBLANK
489 * will be enabled from manage_dm_interrupts on enable CRTC.
491 for (src = DC_IRQ_SOURCE_HPD1; src <= DC_IRQ_SOURCE_HPD6; src++) {
492 hnd_list_l = &adev->dm.irq_handler_list_low_tab[src].head;
493 hnd_list_h = &adev->dm.irq_handler_list_high_tab[src];
494 if (!list_empty(hnd_list_l) || !list_empty(hnd_list_h))
495 dc_interrupt_set(adev->dm.dc, src, true);
498 DM_IRQ_TABLE_UNLOCK(adev, irq_table_flags);
503 * amdgpu_dm_irq_schedule_work - schedule all work items registered for the
506 static void amdgpu_dm_irq_schedule_work(struct amdgpu_device *adev,
507 enum dc_irq_source irq_source)
509 unsigned long irq_table_flags;
510 struct work_struct *work = NULL;
512 DM_IRQ_TABLE_LOCK(adev, irq_table_flags);
514 if (!list_empty(&adev->dm.irq_handler_list_low_tab[irq_source].head))
515 work = &adev->dm.irq_handler_list_low_tab[irq_source].work;
517 DM_IRQ_TABLE_UNLOCK(adev, irq_table_flags);
520 if (!schedule_work(work))
521 DRM_INFO("amdgpu_dm_irq_schedule_work FAILED src %d\n",
528 * amdgpu_dm_irq_immediate_work
529 * Callback high irq work immediately, don't send to work queue
531 static void amdgpu_dm_irq_immediate_work(struct amdgpu_device *adev,
532 enum dc_irq_source irq_source)
534 struct amdgpu_dm_irq_handler_data *handler_data;
535 struct list_head *entry;
536 unsigned long irq_table_flags;
538 DM_IRQ_TABLE_LOCK(adev, irq_table_flags);
542 &adev->dm.irq_handler_list_high_tab[irq_source]) {
544 handler_data = list_entry(entry,
545 struct amdgpu_dm_irq_handler_data,
548 /* Call a subcomponent which registered for immediate
549 * interrupt notification */
550 handler_data->handler(handler_data->handler_arg);
553 DM_IRQ_TABLE_UNLOCK(adev, irq_table_flags);
557 * amdgpu_dm_irq_handler - Generic DM IRQ handler
558 * @adev: amdgpu base driver device containing the DM device
560 * @entry: Data about the triggered interrupt
562 * Calls all registered high irq work immediately, and schedules work for low
563 * irq. The DM IRQ table is used to find the corresponding handlers.
565 static int amdgpu_dm_irq_handler(struct amdgpu_device *adev,
566 struct amdgpu_irq_src *source,
567 struct amdgpu_iv_entry *entry)
570 enum dc_irq_source src =
571 dc_interrupt_to_irq_source(
576 dc_interrupt_ack(adev->dm.dc, src);
578 /* Call high irq work immediately */
579 amdgpu_dm_irq_immediate_work(adev, src);
580 /*Schedule low_irq work */
581 amdgpu_dm_irq_schedule_work(adev, src);
586 static enum dc_irq_source amdgpu_dm_hpd_to_dal_irq_source(unsigned type)
590 return DC_IRQ_SOURCE_HPD1;
592 return DC_IRQ_SOURCE_HPD2;
594 return DC_IRQ_SOURCE_HPD3;
596 return DC_IRQ_SOURCE_HPD4;
598 return DC_IRQ_SOURCE_HPD5;
600 return DC_IRQ_SOURCE_HPD6;
602 return DC_IRQ_SOURCE_INVALID;
606 static int amdgpu_dm_set_hpd_irq_state(struct amdgpu_device *adev,
607 struct amdgpu_irq_src *source,
609 enum amdgpu_interrupt_state state)
611 enum dc_irq_source src = amdgpu_dm_hpd_to_dal_irq_source(type);
612 bool st = (state == AMDGPU_IRQ_STATE_ENABLE);
614 dc_interrupt_set(adev->dm.dc, src, st);
618 static inline int dm_irq_state(struct amdgpu_device *adev,
619 struct amdgpu_irq_src *source,
621 enum amdgpu_interrupt_state state,
622 const enum irq_type dal_irq_type,
626 enum dc_irq_source irq_source;
628 struct amdgpu_crtc *acrtc = adev->mode_info.crtcs[crtc_id];
632 "%s: crtc is NULL at id :%d\n",
638 if (acrtc->otg_inst == -1)
641 irq_source = dal_irq_type + acrtc->otg_inst;
643 st = (state == AMDGPU_IRQ_STATE_ENABLE);
645 dc_interrupt_set(adev->dm.dc, irq_source, st);
649 static int amdgpu_dm_set_pflip_irq_state(struct amdgpu_device *adev,
650 struct amdgpu_irq_src *source,
652 enum amdgpu_interrupt_state state)
663 static int amdgpu_dm_set_crtc_irq_state(struct amdgpu_device *adev,
664 struct amdgpu_irq_src *source,
666 enum amdgpu_interrupt_state state)
677 static const struct amdgpu_irq_src_funcs dm_crtc_irq_funcs = {
678 .set = amdgpu_dm_set_crtc_irq_state,
679 .process = amdgpu_dm_irq_handler,
682 static const struct amdgpu_irq_src_funcs dm_pageflip_irq_funcs = {
683 .set = amdgpu_dm_set_pflip_irq_state,
684 .process = amdgpu_dm_irq_handler,
687 static const struct amdgpu_irq_src_funcs dm_hpd_irq_funcs = {
688 .set = amdgpu_dm_set_hpd_irq_state,
689 .process = amdgpu_dm_irq_handler,
692 void amdgpu_dm_set_irq_funcs(struct amdgpu_device *adev)
695 adev->crtc_irq.num_types = adev->mode_info.num_crtc;
696 adev->crtc_irq.funcs = &dm_crtc_irq_funcs;
698 adev->pageflip_irq.num_types = adev->mode_info.num_crtc;
699 adev->pageflip_irq.funcs = &dm_pageflip_irq_funcs;
701 adev->hpd_irq.num_types = adev->mode_info.num_hpd;
702 adev->hpd_irq.funcs = &dm_hpd_irq_funcs;
706 * amdgpu_dm_hpd_init - hpd setup callback.
708 * @adev: amdgpu_device pointer
710 * Setup the hpd pins used by the card (evergreen+).
711 * Enable the pin, set the polarity, and enable the hpd interrupts.
713 void amdgpu_dm_hpd_init(struct amdgpu_device *adev)
715 struct drm_device *dev = adev->ddev;
716 struct drm_connector *connector;
718 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
719 struct amdgpu_dm_connector *amdgpu_dm_connector =
720 to_amdgpu_dm_connector(connector);
722 const struct dc_link *dc_link = amdgpu_dm_connector->dc_link;
724 if (DC_IRQ_SOURCE_INVALID != dc_link->irq_source_hpd) {
725 dc_interrupt_set(adev->dm.dc,
726 dc_link->irq_source_hpd,
730 if (DC_IRQ_SOURCE_INVALID != dc_link->irq_source_hpd_rx) {
731 dc_interrupt_set(adev->dm.dc,
732 dc_link->irq_source_hpd_rx,
739 * amdgpu_dm_hpd_fini - hpd tear down callback.
741 * @adev: amdgpu_device pointer
743 * Tear down the hpd pins used by the card (evergreen+).
744 * Disable the hpd interrupts.
746 void amdgpu_dm_hpd_fini(struct amdgpu_device *adev)
748 struct drm_device *dev = adev->ddev;
749 struct drm_connector *connector;
751 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
752 struct amdgpu_dm_connector *amdgpu_dm_connector =
753 to_amdgpu_dm_connector(connector);
754 const struct dc_link *dc_link = amdgpu_dm_connector->dc_link;
756 dc_interrupt_set(adev->dm.dc, dc_link->irq_source_hpd, false);
758 if (DC_IRQ_SOURCE_INVALID != dc_link->irq_source_hpd_rx) {
759 dc_interrupt_set(adev->dm.dc,
760 dc_link->irq_source_hpd_rx,