at commit afb1ecfe

1 // SPDX-License-Identifier: MIT
2 /*
3 * Copyright 2015 Advanced Micro Devices, Inc.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21 * OTHER DEALINGS IN THE SOFTWARE.
22 *
23 * Authors: AMD
24 *
25 */
26
27 #include "dm_services_types.h"
28 #include "dc.h"
29 #include "dc/dc_dmub_srv.h"
30 #include "dc/dc_stat.h"
31
32 #include "amdgpu.h"
33 #include "amdgpu_display.h"
34 #include "amdgpu_dm.h"
35 #include "amdgpu_dm_irq.h"
36 #include "dm_helpers.h"
37 #include "amdgpu_dm_crtc.h"
38 #include "amdgpu_dm_hdcp.h"
39 #include "amdgpu_dm_mst_types.h"
40 #include "amdgpu_dm_dmub.h"
41 #include "amdgpu_dm_trace.h"
42 #include "link/protocols/link_dpcd.h"
43 #include "link_service_types.h"
44 #include "ivsrcid/ivsrcid_vislands30.h"
45 #include "ivsrcid/dcn/irqsrcs_dcn_1_0.h"
46 #include "modules/inc/mod_freesync.h"
47 #include <drm/drm_vblank.h>
48
49 /**
50 * DOC: overview
51 *
52 * DM provides another layer of IRQ management on top of what the base driver
53 * already provides. This is something that could be cleaned up, and is a
54 * future TODO item.
55 *
56 * The base driver provides IRQ source registration with DRM, handler
57 * registration into the base driver's IRQ table, and a handler callback
58 * amdgpu_irq_handler(), with which DRM calls on interrupts. This generic
59 * handler looks up the IRQ table, and calls the respective
60 * &amdgpu_irq_src_funcs.process hookups.
61 *
62 * What DM provides on top are two IRQ tables specifically for top-half and
63 * bottom-half IRQ handling, with the bottom-half implementing workqueues:
64 *
65 * - &amdgpu_display_manager.irq_handler_list_high_tab
66 * - &amdgpu_display_manager.irq_handler_list_low_tab
67 *
68 * They override the base driver's IRQ table, and the effect can be seen
69 * in the hooks that DM provides for &amdgpu_irq_src_funcs.process. They
70 * are all set to the DM generic handler amdgpu_dm_irq_handler(), which looks up
71 * DM's IRQ tables. However, in order for base driver to recognize this hook, DM
72 * still needs to register the IRQ with the base driver. See
73 * amdgpu_dm_dce110_register_irq_handlers() and
74 * amdgpu_dm_dcn10_register_irq_handlers().
75 *
76 * To expose DC's hardware interrupt toggle to the base driver, DM implements
77 * &amdgpu_irq_src_funcs.set hooks. Base driver calls it through
78 * amdgpu_irq_update() to enable or disable the interrupt.
79 */
80
81 /******************************************************************************
82 * Private declarations.
83 *****************************************************************************/
84
85 /**
86 * struct amdgpu_dm_irq_handler_data - Data for DM interrupt handlers.
87 *
88 * @list: Linked list entry referencing the next/previous handler
89 * @handler: Handler function
90 * @handler_arg: Argument passed to the handler when triggered
91 * @dm: DM which this handler belongs to
92 * @irq_source: DC interrupt source that this handler is registered for
93 * @work: work struct
94 */
95 struct amdgpu_dm_irq_handler_data {
96 struct list_head list;
97 interrupt_handler handler;
98 void *handler_arg;
99
100 struct amdgpu_display_manager *dm;
101 /* DAL irq source which registered for this interrupt. */
102 enum dc_irq_source irq_source;
103 struct work_struct work;
104 };
105
106 #define DM_IRQ_TABLE_LOCK(adev, flags) \
107 spin_lock_irqsave(&adev->dm.irq_handler_list_table_lock, flags)
108
109 #define DM_IRQ_TABLE_UNLOCK(adev, flags) \
110 spin_unlock_irqrestore(&adev->dm.irq_handler_list_table_lock, flags)
111
112 /******************************************************************************
113 * Private functions.
114 *****************************************************************************/
115
116 static void init_handler_common_data(struct amdgpu_dm_irq_handler_data *hcd,
117 void (*ih)(void *),
118 void *args,
119 struct amdgpu_display_manager *dm)
120 {
121 hcd->handler = ih;
122 hcd->handler_arg = args;
123 hcd->dm = dm;
124 }
125
126 /**
127 * dm_irq_work_func() - Handle an IRQ outside of the interrupt handler proper.
128 *
129 * @work: work struct
130 */
131 static void dm_irq_work_func(struct work_struct *work)
132 {
133 struct amdgpu_dm_irq_handler_data *handler_data =
134 container_of(work, struct amdgpu_dm_irq_handler_data, work);
135
136 handler_data->handler(handler_data->handler_arg);
137
138 /* Call a DAL subcomponent which registered for interrupt notification
139 * at INTERRUPT_LOW_IRQ_CONTEXT.
140 * (The most common use is HPD interrupt)
141 */
142 }
143
144 /*
145 * Remove a handler and return a pointer to handler list from which the
146 * handler was removed.
147 */
148 static struct list_head *remove_irq_handler(struct amdgpu_device *adev,
149 void *ih,
150 const struct dc_interrupt_params *int_params)
151 {
152 struct list_head *hnd_list;
153 struct list_head *entry, *tmp;
154 struct amdgpu_dm_irq_handler_data *handler;
155 unsigned long irq_table_flags;
156 bool handler_removed = false;
157 enum dc_irq_source irq_source;
158
159 DM_IRQ_TABLE_LOCK(adev, irq_table_flags);
160
161 irq_source = int_params->irq_source;
162
163 switch (int_params->int_context) {
164 case INTERRUPT_HIGH_IRQ_CONTEXT:
165 hnd_list = &adev->dm.irq_handler_list_high_tab[irq_source];
166 break;
167 case INTERRUPT_LOW_IRQ_CONTEXT:
168 default:
169 hnd_list = &adev->dm.irq_handler_list_low_tab[irq_source];
170 break;
171 }
172
173 list_for_each_safe(entry, tmp, hnd_list) {
174
175 handler = list_entry(entry, struct amdgpu_dm_irq_handler_data,
176 list);
177
178 if (handler == NULL)
179 continue;
180
181 if (ih == handler->handler) {
182 /* Found our handler. Remove it from the list. */
183 list_del(&handler->list);
184 handler_removed = true;
185 break;
186 }
187 }
188
189 DM_IRQ_TABLE_UNLOCK(adev, irq_table_flags);
190
191 if (!handler_removed) {
192 /* Not necessarily an error - caller may not
193 * know the context.
194 */
195 return NULL;
196 }
197
198 if (int_params->int_context == INTERRUPT_LOW_IRQ_CONTEXT)
199 cancel_work_sync(&handler->work);
200
201 kfree(handler);
202
203 DRM_DEBUG_KMS(
204 "DM_IRQ: removed irq handler: %p for: dal_src=%d, irq context=%d\n",
205 ih, int_params->irq_source, int_params->int_context);
206
207 return hnd_list;
208 }
209
210 static bool
211 validate_irq_registration_params(struct dc_interrupt_params *int_params,
212 void (*ih)(void *))
213 {
214 if (NULL == int_params || NULL == ih) {
215 DRM_ERROR("DM_IRQ: invalid input!\n");
216 return false;
217 }
218
219 if (int_params->int_context >= INTERRUPT_CONTEXT_NUMBER) {
220 DRM_ERROR("DM_IRQ: invalid context: %d!\n",
221 int_params->int_context);
222 return false;
223 }
224
225 if (!DAL_VALID_IRQ_SRC_NUM(int_params->irq_source)) {
226 DRM_ERROR("DM_IRQ: invalid irq_source: %d!\n",
227 int_params->irq_source);
228 return false;
229 }
230
231 return true;
232 }
233
234 static bool validate_irq_unregistration_params(enum dc_irq_source irq_source,
235 irq_handler_idx handler_idx)
236 {
237 if (handler_idx == DAL_INVALID_IRQ_HANDLER_IDX) {
238 DRM_ERROR("DM_IRQ: invalid handler_idx==NULL!\n");
239 return false;
240 }
241
242 if (!DAL_VALID_IRQ_SRC_NUM(irq_source)) {
243 DRM_ERROR("DM_IRQ: invalid irq_source:%d!\n", irq_source);
244 return false;
245 }
246
247 return true;
248 }
249 /******************************************************************************
250 * Public functions.
251 *
252 * Note: caller is responsible for input validation.
253 *****************************************************************************/
254
255 /**
256 * amdgpu_dm_irq_register_interrupt() - Register a handler within DM.
257 * @adev: The base driver device containing the DM device.
258 * @int_params: Interrupt parameters containing the source, and handler context
259 * @ih: Function pointer to the interrupt handler to register
260 * @handler_args: Arguments passed to the handler when the interrupt occurs
261 *
262 * Register an interrupt handler for the given IRQ source, under the given
263 * context. The context can either be high or low. High context handlers are
264 * executed directly within ISR context, while low context is executed within a
265 * workqueue, thereby allowing operations that sleep.
266 *
267 * Registered handlers are called in a FIFO manner, i.e. the most recently
268 * registered handler will be called first.
269 *
270 * Return: Handler data &struct amdgpu_dm_irq_handler_data containing the IRQ
271 * source, handler function, and args
272 */
273 void *amdgpu_dm_irq_register_interrupt(struct amdgpu_device *adev,
274 struct dc_interrupt_params *int_params,
275 void (*ih)(void *),
276 void *handler_args)
277 {
278 struct list_head *hnd_list;
279 struct amdgpu_dm_irq_handler_data *handler_data;
280 unsigned long irq_table_flags;
281 enum dc_irq_source irq_source;
282
283 if (!validate_irq_registration_params(int_params, ih))
284 return DAL_INVALID_IRQ_HANDLER_IDX;
285
286 handler_data = kzalloc_obj(*handler_data);
287 if (!handler_data) {
288 DRM_ERROR("DM_IRQ: failed to allocate irq handler!\n");
289 return DAL_INVALID_IRQ_HANDLER_IDX;
290 }
291
292 init_handler_common_data(handler_data, ih, handler_args, &adev->dm);
293
294 irq_source = int_params->irq_source;
295
296 handler_data->irq_source = irq_source;
297
298 /* Lock the list, add the handler. */
299 DM_IRQ_TABLE_LOCK(adev, irq_table_flags);
300
301 switch (int_params->int_context) {
302 case INTERRUPT_HIGH_IRQ_CONTEXT:
303 hnd_list = &adev->dm.irq_handler_list_high_tab[irq_source];
304 break;
305 case INTERRUPT_LOW_IRQ_CONTEXT:
306 default:
307 hnd_list = &adev->dm.irq_handler_list_low_tab[irq_source];
308 INIT_WORK(&handler_data->work, dm_irq_work_func);
309 break;
310 }
311
312 list_add_tail(&handler_data->list, hnd_list);
313
314 DM_IRQ_TABLE_UNLOCK(adev, irq_table_flags);
315
316 /* This pointer will be stored by code which requested interrupt
317 * registration.
318 * The same pointer will be needed in order to unregister the
319 * interrupt.
320 */
321
322 DRM_DEBUG_KMS(
323 "DM_IRQ: added irq handler: %p for: dal_src=%d, irq context=%d\n",
324 handler_data,
325 irq_source,
326 int_params->int_context);
327
328 return handler_data;
329 }
330 EXPORT_IF_KUNIT(amdgpu_dm_irq_register_interrupt);
331
332 /**
333 * amdgpu_dm_irq_unregister_interrupt() - Remove a handler from the DM IRQ table
334 * @adev: The base driver device containing the DM device
335 * @irq_source: IRQ source to remove the given handler from
336 * @ih: Function pointer to the interrupt handler to unregister
337 *
338 * Go through both low and high context IRQ tables, and find the given handler
339 * for the given irq source. If found, remove it. Otherwise, do nothing.
340 */
341 void amdgpu_dm_irq_unregister_interrupt(struct amdgpu_device *adev,
342 enum dc_irq_source irq_source,
343 void *ih)
344 {
345 struct list_head *handler_list;
346 struct dc_interrupt_params int_params;
347 int i;
348
349 if (!validate_irq_unregistration_params(irq_source, ih))
350 return;
351
352 memset(&int_params, 0, sizeof(int_params));
353
354 int_params.irq_source = irq_source;
355
356 for (i = 0; i < INTERRUPT_CONTEXT_NUMBER; i++) {
357
358 int_params.int_context = i;
359
360 handler_list = remove_irq_handler(adev, ih, &int_params);
361
362 if (handler_list != NULL)
363 break;
364 }
365
366 if (handler_list == NULL) {
367 /* If we got here, it means we searched all irq contexts
368 * for this irq source, but the handler was not found.
369 */
370 DRM_ERROR(
371 "DM_IRQ: failed to find irq handler:%p for irq_source:%d!\n",
372 ih, irq_source);
373 }
374 }
375 EXPORT_IF_KUNIT(amdgpu_dm_irq_unregister_interrupt);
376
377 /**
378 * amdgpu_dm_irq_init() - Initialize DM IRQ management
379 * @adev: The base driver device containing the DM device
380 *
381 * Initialize DM's high and low context IRQ tables.
382 *
383 * The N by M table contains N IRQ sources, with M
384 * &struct amdgpu_dm_irq_handler_data hooked together in a linked list. The
385 * list_heads are initialized here. When an interrupt n is triggered, all m
386 * handlers are called in sequence, FIFO according to registration order.
387 *
388 * The low context table requires special steps to initialize, since handlers
389 * will be deferred to a workqueue. See &struct irq_list_head.
390 */
391 int amdgpu_dm_irq_init(struct amdgpu_device *adev)
392 {
393 int src;
394 struct list_head *lh;
395
396 DRM_DEBUG_KMS("DM_IRQ\n");
397
398 spin_lock_init(&adev->dm.irq_handler_list_table_lock);
399
400 adev->dm.irq_wq = alloc_workqueue("amdgpu_dm_irq",
401 WQ_UNBOUND | WQ_HIGHPRI, 0);
402
403 if (!adev->dm.irq_wq)
404 return -ENOMEM;
405
406 adev->dm.vmin_vmax_wq = alloc_workqueue("amdgpu_dm_vmin_vmax",
407 WQ_UNBOUND, 0);
408
409 if (!adev->dm.vmin_vmax_wq) {
410 destroy_workqueue(adev->dm.irq_wq);
411 adev->dm.irq_wq = NULL;
412 return -ENOMEM;
413 }
414
415 for (src = 0; src < DAL_IRQ_SOURCES_NUMBER; src++) {
416 /* low context handler list init */
417 lh = &adev->dm.irq_handler_list_low_tab[src];
418 INIT_LIST_HEAD(lh);
419 /* high context handler init */
420 INIT_LIST_HEAD(&adev->dm.irq_handler_list_high_tab[src]);
421 }
422
423 return 0;
424 }
425 EXPORT_IF_KUNIT(amdgpu_dm_irq_init);
426
427 /**
428 * amdgpu_dm_irq_fini() - Tear down DM IRQ management
429 * @adev: The base driver device containing the DM device
430 *
431 * Removes all handlers from the IRQ tables under the spinlock, cancels
432 * pending work items, and deallocates all handler data.
433 */
434 void amdgpu_dm_irq_fini(struct amdgpu_device *adev)
435 {
436 int src;
437 LIST_HEAD(low_handlers);
438 LIST_HEAD(high_handlers);
439 struct list_head *entry, *tmp;
440 struct amdgpu_dm_irq_handler_data *handler;
441 unsigned long irq_table_flags;
442
443 DRM_DEBUG_KMS("DM_IRQ: releasing resources.\n");
444
445 for (src = 0; src < DAL_IRQ_SOURCES_NUMBER; src++) {
446 DM_IRQ_TABLE_LOCK(adev, irq_table_flags);
447
448 /*
449 * Move all handlers from the low and high context tables to
450 * temporary lists under the lock. This prevents the ISR from
451 * finding them while we process them outside the lock.
452 */
453 list_splice_init(&adev->dm.irq_handler_list_low_tab[src],
454 &low_handlers);
455 list_splice_init(&adev->dm.irq_handler_list_high_tab[src],
456 &high_handlers);
457
458 DM_IRQ_TABLE_UNLOCK(adev, irq_table_flags);
459
460 /*
461 * Cancel all pending work for the low-context handlers
462 * outside the lock. cancel_work_sync() may sleep and waits
463 * until any running work completes, preventing UAF.
464 */
465 list_for_each_safe(entry, tmp, &low_handlers) {
466 handler = list_entry(entry,
467 struct amdgpu_dm_irq_handler_data,
468 list);
469 cancel_work_sync(&handler->work);
470 }
471
472 /*
473 * High-context handlers are executed synchronously within ISR
474 * context (see amdgpu_dm_irq_immediate_work()) and have no
475 * work_struct, so there is no pending work to cancel here.
476 * They will be freed along with low_handlers after the loop.
477 */
478 }
479
480 /* Deallocate all handlers. */
481 list_for_each_safe(entry, tmp, &low_handlers) {
482 handler = list_entry(entry,
483 struct amdgpu_dm_irq_handler_data,
484 list);
485 list_del(&handler->list);
486 kfree(handler);
487 }
488
489 list_for_each_safe(entry, tmp, &high_handlers) {
490 handler = list_entry(entry,
491 struct amdgpu_dm_irq_handler_data,
492 list);
493 list_del(&handler->list);
494 kfree(handler);
495 }
496
497 if (adev->dm.vmin_vmax_wq) {
498 destroy_workqueue(adev->dm.vmin_vmax_wq);
499 adev->dm.vmin_vmax_wq = NULL;
500 }
501
502 if (adev->dm.irq_wq) {
503 destroy_workqueue(adev->dm.irq_wq);
504 adev->dm.irq_wq = NULL;
505 }
506 }
507 EXPORT_IF_KUNIT(amdgpu_dm_irq_fini);
508
509 void amdgpu_dm_irq_suspend(struct amdgpu_device *adev)
510 {
511 struct drm_device *dev = adev_to_drm(adev);
512 int src;
513 struct list_head *hnd_list_l;
514 unsigned long irq_table_flags;
515 struct list_head *entry, *tmp;
516 struct amdgpu_dm_irq_handler_data *handler;
517
518 DM_IRQ_TABLE_LOCK(adev, irq_table_flags);
519
520 DRM_DEBUG_KMS("DM_IRQ: suspend\n");
521
522 /**
523 * Disable HW interrupt for HPD and HPDRX only since FLIP and VBLANK
524 * will be disabled from manage_dm_interrupts on disable CRTC.
525 *
526 * Disable the HW interrupt first, then flush any pending work. Since
527 * the HW interrupt is disabled under the lock, no new IRQ can be
528 * generated after the disable completes. Any work already queued by an
529 * in-flight ISR will be flushed below.
530 */
531 for (src = DC_IRQ_SOURCE_HPD1; src <= DC_IRQ_SOURCE_HPD6RX; src++) {
532 hnd_list_l = &adev->dm.irq_handler_list_low_tab[src];
533 dc_interrupt_set(adev->dm.dc, src, false);
534
535 DM_IRQ_TABLE_UNLOCK(adev, irq_table_flags);
536
537 if (!list_empty(hnd_list_l)) {
538 list_for_each_safe(entry, tmp, hnd_list_l) {
539 handler = list_entry(
540 entry,
541 struct amdgpu_dm_irq_handler_data,
542 list);
543 flush_work(&handler->work);
544 }
545 }
546 DM_IRQ_TABLE_LOCK(adev, irq_table_flags);
547 }
548
549 DM_IRQ_TABLE_UNLOCK(adev, irq_table_flags);
550
551 if (dev->mode_config.poll_enabled)
552 drm_kms_helper_poll_disable(dev);
553 }
554 EXPORT_IF_KUNIT(amdgpu_dm_irq_suspend);
555
556 void amdgpu_dm_irq_resume_early(struct amdgpu_device *adev)
557 {
558 int src;
559 struct list_head *hnd_list_h, *hnd_list_l;
560 unsigned long irq_table_flags;
561
562 DM_IRQ_TABLE_LOCK(adev, irq_table_flags);
563
564 drm_dbg(adev_to_drm(adev), "DM_IRQ: early resume\n");
565
566 /* re-enable short pulse interrupts HW interrupt */
567 for (src = DC_IRQ_SOURCE_HPD1RX; src <= DC_IRQ_SOURCE_HPD6RX; src++) {
568 hnd_list_l = &adev->dm.irq_handler_list_low_tab[src];
569 hnd_list_h = &adev->dm.irq_handler_list_high_tab[src];
570 if (!list_empty(hnd_list_l) || !list_empty(hnd_list_h))
571 dc_interrupt_set(adev->dm.dc, src, true);
572 }
573
574 DM_IRQ_TABLE_UNLOCK(adev, irq_table_flags);
575 }
576 EXPORT_IF_KUNIT(amdgpu_dm_irq_resume_early);
577
578 void amdgpu_dm_irq_resume_late(struct amdgpu_device *adev)
579 {
580 struct drm_device *dev = adev_to_drm(adev);
581 int src;
582 struct list_head *hnd_list_h, *hnd_list_l;
583 unsigned long irq_table_flags;
584
585 DM_IRQ_TABLE_LOCK(adev, irq_table_flags);
586
587 drm_dbg(adev_to_drm(adev), "DM_IRQ: resume\n");
588
589 /**
590 * Renable HW interrupt for HPD and only since FLIP and VBLANK
591 * will be enabled from manage_dm_interrupts on enable CRTC.
592 */
593 for (src = DC_IRQ_SOURCE_HPD1; src <= DC_IRQ_SOURCE_HPD6; src++) {
594 hnd_list_l = &adev->dm.irq_handler_list_low_tab[src];
595 hnd_list_h = &adev->dm.irq_handler_list_high_tab[src];
596 if (!list_empty(hnd_list_l) || !list_empty(hnd_list_h))
597 dc_interrupt_set(adev->dm.dc, src, true);
598 }
599
600 DM_IRQ_TABLE_UNLOCK(adev, irq_table_flags);
601
602 if (dev->mode_config.poll_enabled)
603 drm_kms_helper_poll_enable(dev);
604 }
605 EXPORT_IF_KUNIT(amdgpu_dm_irq_resume_late);
606
607 /*
608 * amdgpu_dm_irq_schedule_work - schedule all work items registered for the
609 * "irq_source".
610 */
611 STATIC_IFN_KUNIT void amdgpu_dm_irq_schedule_work(struct amdgpu_device *adev,
612 enum dc_irq_source irq_source)
613 {
614 struct list_head *handler_list = &adev->dm.irq_handler_list_low_tab[irq_source];
615 struct amdgpu_dm_irq_handler_data *handler_data;
616 bool work_queued = false;
617 unsigned long irq_table_flags;
618
619 DM_IRQ_TABLE_LOCK(adev, irq_table_flags);
620
621 if (list_empty(handler_list))
622 goto out_unlock;
623
624 list_for_each_entry(handler_data, handler_list, list) {
625 if (queue_work(adev->dm.irq_wq, &handler_data->work)) {
626 work_queued = true;
627 break;
628 }
629 }
630
631 if (!work_queued) {
632 struct amdgpu_dm_irq_handler_data *handler_data_add;
633 /*get the amdgpu_dm_irq_handler_data of first item pointed by handler_list*/
634 handler_data = container_of(handler_list->next, struct amdgpu_dm_irq_handler_data, list);
635
636 /*allocate a new amdgpu_dm_irq_handler_data*/
637 handler_data_add = kzalloc_obj(*handler_data, GFP_ATOMIC);
638 if (!handler_data_add) {
639 DRM_ERROR("DM_IRQ: failed to allocate irq handler!\n");
640 goto out_unlock;
641 }
642
643 /*copy new amdgpu_dm_irq_handler_data members from handler_data*/
644 handler_data_add->handler = handler_data->handler;
645 handler_data_add->handler_arg = handler_data->handler_arg;
646 handler_data_add->dm = handler_data->dm;
647 handler_data_add->irq_source = irq_source;
648
649 list_add_tail(&handler_data_add->list, handler_list);
650
651 INIT_WORK(&handler_data_add->work, dm_irq_work_func);
652
653 if (queue_work(adev->dm.irq_wq, &handler_data_add->work))
654 DRM_DEBUG("Queued work for handling interrupt from "
655 "display for IRQ source %d\n",
656 irq_source);
657 else
658 DRM_ERROR("Failed to queue work for handling interrupt "
659 "from display for IRQ source %d\n",
660 irq_source);
661 }
662
663 out_unlock:
664 DM_IRQ_TABLE_UNLOCK(adev, irq_table_flags);
665 }
666 EXPORT_IF_KUNIT(amdgpu_dm_irq_schedule_work);
667
668 /*
669 * amdgpu_dm_irq_immediate_work
670 * Callback high irq work immediately, don't send to work queue
671 */
672 STATIC_IFN_KUNIT void amdgpu_dm_irq_immediate_work(struct amdgpu_device *adev,
673 enum dc_irq_source irq_source)
674 {
675 struct amdgpu_dm_irq_handler_data *handler_data;
676 unsigned long irq_table_flags;
677
678 DM_IRQ_TABLE_LOCK(adev, irq_table_flags);
679
680 list_for_each_entry(handler_data,
681 &adev->dm.irq_handler_list_high_tab[irq_source],
682 list) {
683 /* Call a subcomponent which registered for immediate
684 * interrupt notification
685 */
686 handler_data->handler(handler_data->handler_arg);
687 }
688
689 DM_IRQ_TABLE_UNLOCK(adev, irq_table_flags);
690 }
691 EXPORT_IF_KUNIT(amdgpu_dm_irq_immediate_work);
692
693 /**
694 * amdgpu_dm_irq_handler - Generic DM IRQ handler
695 * @adev: amdgpu base driver device containing the DM device
696 * @source: Unused
697 * @entry: Data about the triggered interrupt
698 *
699 * Calls all registered high irq work immediately, and schedules work for low
700 * irq. The DM IRQ table is used to find the corresponding handlers.
701 */
702 STATIC_IFN_KUNIT int amdgpu_dm_irq_handler(struct amdgpu_device *adev,
703 struct amdgpu_irq_src *source,
704 struct amdgpu_iv_entry *entry)
705 {
706
707 enum dc_irq_source src =
708 dc_interrupt_to_irq_source(
709 adev->dm.dc,
710 entry->src_id,
711 entry->src_data[0]);
712
713 dc_interrupt_ack(adev->dm.dc, src);
714
715 /* Call high irq work immediately */
716 amdgpu_dm_irq_immediate_work(adev, src);
717 /*Schedule low_irq work */
718 amdgpu_dm_irq_schedule_work(adev, src);
719
720 return 0;
721 }
722 EXPORT_IF_KUNIT(amdgpu_dm_irq_handler);
723
724 STATIC_IFN_KUNIT enum dc_irq_source amdgpu_dm_hpd_to_dal_irq_source(unsigned int type)
725 {
726 switch (type) {
727 case AMDGPU_HPD_1:
728 return DC_IRQ_SOURCE_HPD1;
729 case AMDGPU_HPD_2:
730 return DC_IRQ_SOURCE_HPD2;
731 case AMDGPU_HPD_3:
732 return DC_IRQ_SOURCE_HPD3;
733 case AMDGPU_HPD_4:
734 return DC_IRQ_SOURCE_HPD4;
735 case AMDGPU_HPD_5:
736 return DC_IRQ_SOURCE_HPD5;
737 case AMDGPU_HPD_6:
738 return DC_IRQ_SOURCE_HPD6;
739 default:
740 return DC_IRQ_SOURCE_INVALID;
741 }
742 }
743 EXPORT_IF_KUNIT(amdgpu_dm_hpd_to_dal_irq_source);
744
745 STATIC_IFN_KUNIT int amdgpu_dm_set_hpd_irq_state(struct amdgpu_device *adev,
746 struct amdgpu_irq_src *source,
747 unsigned int type,
748 enum amdgpu_interrupt_state state)
749 {
750 enum dc_irq_source src = amdgpu_dm_hpd_to_dal_irq_source(type);
751 bool st = (state == AMDGPU_IRQ_STATE_ENABLE);
752
753 dc_interrupt_set(adev->dm.dc, src, st);
754 return 0;
755 }
756 EXPORT_IF_KUNIT(amdgpu_dm_set_hpd_irq_state);
757
758 static inline int dm_irq_state(struct amdgpu_device *adev,
759 struct amdgpu_irq_src *source,
760 unsigned int crtc_id,
761 enum amdgpu_interrupt_state state,
762 const enum irq_type dal_irq_type,
763 const char *func)
764 {
765 bool st;
766 enum dc_irq_source irq_source;
767 struct dc *dc = adev->dm.dc;
768 struct amdgpu_crtc *acrtc = adev->mode_info.crtcs[crtc_id];
769
770 if (!acrtc) {
771 DRM_ERROR(
772 "%s: crtc is NULL at id :%d\n",
773 func,
774 crtc_id);
775 return 0;
776 }
777
778 if (acrtc->otg_inst == -1)
779 return 0;
780
781 irq_source = dal_irq_type + acrtc->otg_inst;
782
783 st = (state == AMDGPU_IRQ_STATE_ENABLE);
784
785 if (dc && dc->caps.ips_support && dc->idle_optimizations_allowed)
786 dc_allow_idle_optimizations(dc, false);
787
788 dc_interrupt_set(adev->dm.dc, irq_source, st);
789 return 0;
790 }
791
792 STATIC_IFN_KUNIT int amdgpu_dm_set_pflip_irq_state(struct amdgpu_device *adev,
793 struct amdgpu_irq_src *source,
794 unsigned int crtc_id,
795 enum amdgpu_interrupt_state state)
796 {
797 return dm_irq_state(
798 adev,
799 source,
800 crtc_id,
801 state,
802 IRQ_TYPE_PFLIP,
803 __func__);
804 }
805 EXPORT_IF_KUNIT(amdgpu_dm_set_pflip_irq_state);
806
807 STATIC_IFN_KUNIT int amdgpu_dm_set_crtc_irq_state(struct amdgpu_device *adev,
808 struct amdgpu_irq_src *source,
809 unsigned int crtc_id,
810 enum amdgpu_interrupt_state state)
811 {
812 return dm_irq_state(
813 adev,
814 source,
815 crtc_id,
816 state,
817 IRQ_TYPE_VBLANK,
818 __func__);
819 }
820 EXPORT_IF_KUNIT(amdgpu_dm_set_crtc_irq_state);
821
822 STATIC_IFN_KUNIT int amdgpu_dm_set_vline0_irq_state(struct amdgpu_device *adev,
823 struct amdgpu_irq_src *source,
824 unsigned int crtc_id,
825 enum amdgpu_interrupt_state state)
826 {
827 return dm_irq_state(
828 adev,
829 source,
830 crtc_id,
831 state,
832 IRQ_TYPE_VLINE0,
833 __func__);
834 }
835 EXPORT_IF_KUNIT(amdgpu_dm_set_vline0_irq_state);
836
837 STATIC_IFN_KUNIT int amdgpu_dm_set_dmub_outbox_irq_state(struct amdgpu_device *adev,
838 struct amdgpu_irq_src *source,
839 unsigned int crtc_id,
840 enum amdgpu_interrupt_state state)
841 {
842 enum dc_irq_source irq_source = DC_IRQ_SOURCE_DMCUB_OUTBOX;
843 bool st = (state == AMDGPU_IRQ_STATE_ENABLE);
844
845 dc_interrupt_set(adev->dm.dc, irq_source, st);
846 return 0;
847 }
848 EXPORT_IF_KUNIT(amdgpu_dm_set_dmub_outbox_irq_state);
849
850 STATIC_IFN_KUNIT int amdgpu_dm_set_vupdate_irq_state(struct amdgpu_device *adev,
851 struct amdgpu_irq_src *source,
852 unsigned int crtc_id,
853 enum amdgpu_interrupt_state state)
854 {
855 return dm_irq_state(
856 adev,
857 source,
858 crtc_id,
859 state,
860 IRQ_TYPE_VUPDATE,
861 __func__);
862 }
863 EXPORT_IF_KUNIT(amdgpu_dm_set_vupdate_irq_state);
864
865 STATIC_IFN_KUNIT int amdgpu_dm_set_dmub_trace_irq_state(struct amdgpu_device *adev,
866 struct amdgpu_irq_src *source,
867 unsigned int type,
868 enum amdgpu_interrupt_state state)
869 {
870 enum dc_irq_source irq_source = DC_IRQ_SOURCE_DMCUB_OUTBOX0;
871 bool st = (state == AMDGPU_IRQ_STATE_ENABLE);
872
873 dc_interrupt_set(adev->dm.dc, irq_source, st);
874 return 0;
875 }
876 EXPORT_IF_KUNIT(amdgpu_dm_set_dmub_trace_irq_state);
877
878 static const struct amdgpu_irq_src_funcs dm_crtc_irq_funcs = {
879 .set = amdgpu_dm_set_crtc_irq_state,
880 .process = amdgpu_dm_irq_handler,
881 };
882
883 static const struct amdgpu_irq_src_funcs dm_vline0_irq_funcs = {
884 .set = amdgpu_dm_set_vline0_irq_state,
885 .process = amdgpu_dm_irq_handler,
886 };
887
888 static const struct amdgpu_irq_src_funcs dm_dmub_outbox_irq_funcs = {
889 .set = amdgpu_dm_set_dmub_outbox_irq_state,
890 .process = amdgpu_dm_irq_handler,
891 };
892
893 static const struct amdgpu_irq_src_funcs dm_vupdate_irq_funcs = {
894 .set = amdgpu_dm_set_vupdate_irq_state,
895 .process = amdgpu_dm_irq_handler,
896 };
897
898 static const struct amdgpu_irq_src_funcs dm_dmub_trace_irq_funcs = {
899 .set = amdgpu_dm_set_dmub_trace_irq_state,
900 .process = amdgpu_dm_irq_handler,
901 };
902
903 static const struct amdgpu_irq_src_funcs dm_pageflip_irq_funcs = {
904 .set = amdgpu_dm_set_pflip_irq_state,
905 .process = amdgpu_dm_irq_handler,
906 };
907
908 static const struct amdgpu_irq_src_funcs dm_hpd_irq_funcs = {
909 .set = amdgpu_dm_set_hpd_irq_state,
910 .process = amdgpu_dm_irq_handler,
911 };
912
913 void amdgpu_dm_set_irq_funcs(struct amdgpu_device *adev)
914 {
915 adev->crtc_irq.num_types = adev->mode_info.num_crtc;
916 adev->crtc_irq.funcs = &dm_crtc_irq_funcs;
917
918 adev->vline0_irq.num_types = adev->mode_info.num_crtc;
919 adev->vline0_irq.funcs = &dm_vline0_irq_funcs;
920
921 adev->dmub_outbox_irq.num_types = 1;
922 adev->dmub_outbox_irq.funcs = &dm_dmub_outbox_irq_funcs;
923
924 adev->vupdate_irq.num_types = adev->mode_info.num_crtc;
925 adev->vupdate_irq.funcs = &dm_vupdate_irq_funcs;
926
927 adev->dmub_trace_irq.num_types = 1;
928 adev->dmub_trace_irq.funcs = &dm_dmub_trace_irq_funcs;
929
930 adev->pageflip_irq.num_types = adev->mode_info.num_crtc;
931 adev->pageflip_irq.funcs = &dm_pageflip_irq_funcs;
932
933 adev->hpd_irq.num_types = adev->mode_info.num_hpd;
934 adev->hpd_irq.funcs = &dm_hpd_irq_funcs;
935 }
936 EXPORT_IF_KUNIT(amdgpu_dm_set_irq_funcs);
937
938 void amdgpu_dm_outbox_init(struct amdgpu_device *adev)
939 {
940 dc_interrupt_set(adev->dm.dc,
941 DC_IRQ_SOURCE_DMCUB_OUTBOX,
942 true);
943 }
944 EXPORT_IF_KUNIT(amdgpu_dm_outbox_init);
945
946 /**
947 * amdgpu_dm_hpd_init - hpd setup callback.
948 *
949 * @adev: amdgpu_device pointer
950 *
951 * Setup the hpd pins used by the card (evergreen+).
952 * Enable the pin, set the polarity, and enable the hpd interrupts.
953 */
954 void amdgpu_dm_hpd_init(struct amdgpu_device *adev)
955 {
956 struct drm_device *dev = adev_to_drm(adev);
957 struct drm_connector *connector;
958 struct drm_connector_list_iter iter;
959 int irq_type;
960 int i;
961 bool use_polling = false;
962
963 /* First, clear all hpd and hpdrx interrupts */
964 for (i = DC_IRQ_SOURCE_HPD1; i <= DC_IRQ_SOURCE_HPD6RX; i++) {
965 if (!dc_interrupt_set(adev->dm.dc, i, false))
966 drm_err(dev, "Failed to clear hpd(rx) source=%d on init\n",
967 i);
968 }
969
970 drm_connector_list_iter_begin(dev, &iter);
971 drm_for_each_connector_iter(connector, &iter) {
972 struct amdgpu_dm_connector *amdgpu_dm_connector;
973 const struct dc_link *dc_link;
974
975 if (connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK)
976 continue;
977
978 amdgpu_dm_connector = to_amdgpu_dm_connector(connector);
979 dc_link = amdgpu_dm_connector->dc_link;
980 if (!dc_link)
981 continue;
982
983 /*
984 * Analog connectors may be hot-plugged unlike other connector
985 * types that don't support HPD. Only poll analog connectors.
986 */
987 use_polling |= dc_connector_supports_analog(dc_link->link_id.id);
988
989 /*
990 * Get a base driver irq reference for hpd ints for the lifetime
991 * of dm. Note that only hpd interrupt types are registered with
992 * base driver; hpd_rx types aren't. IOW, amdgpu_irq_get/put on
993 * hpd_rx isn't available. DM currently controls hpd_rx
994 * explicitly with dc_interrupt_set()
995 */
996 if (dc_link->irq_source_hpd != DC_IRQ_SOURCE_INVALID) {
997 irq_type = dc_link->irq_source_hpd - DC_IRQ_SOURCE_HPD1;
998 /*
999 * TODO: There's a mismatch between mode_info.num_hpd
1000 * and what bios reports as the # of connectors with hpd
1001 * sources. Since the # of hpd source types registered
1002 * with base driver == mode_info.num_hpd, we have to
1003 * fallback to dc_interrupt_set for the remaining types.
1004 */
1005 if (irq_type < adev->mode_info.num_hpd) {
1006 if (amdgpu_irq_get(adev, &adev->hpd_irq, irq_type))
1007 drm_err(dev, "DM_IRQ: Failed get HPD for source=%d)!\n",
1008 dc_link->irq_source_hpd);
1009 } else {
1010 dc_interrupt_set(adev->dm.dc,
1011 dc_link->irq_source_hpd,
1012 true);
1013 }
1014 }
1015
1016 if (dc_link->irq_source_hpd_rx != DC_IRQ_SOURCE_INVALID) {
1017 dc_interrupt_set(adev->dm.dc,
1018 dc_link->irq_source_hpd_rx,
1019 true);
1020 }
1021 }
1022 drm_connector_list_iter_end(&iter);
1023
1024 if (use_polling)
1025 drm_kms_helper_poll_init(dev);
1026 }
1027 EXPORT_IF_KUNIT(amdgpu_dm_hpd_init);
1028
1029 /**
1030 * amdgpu_dm_hpd_fini - hpd tear down callback.
1031 *
1032 * @adev: amdgpu_device pointer
1033 *
1034 * Tear down the hpd pins used by the card (evergreen+).
1035 * Disable the hpd interrupts.
1036 */
1037 void amdgpu_dm_hpd_fini(struct amdgpu_device *adev)
1038 {
1039 struct drm_device *dev = adev_to_drm(adev);
1040 struct drm_connector *connector;
1041 struct drm_connector_list_iter iter;
1042 int irq_type;
1043
1044 drm_connector_list_iter_begin(dev, &iter);
1045 drm_for_each_connector_iter(connector, &iter) {
1046 struct amdgpu_dm_connector *amdgpu_dm_connector;
1047 const struct dc_link *dc_link;
1048
1049 if (connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK)
1050 continue;
1051
1052 amdgpu_dm_connector = to_amdgpu_dm_connector(connector);
1053 dc_link = amdgpu_dm_connector->dc_link;
1054
1055 if (dc_link->irq_source_hpd != DC_IRQ_SOURCE_INVALID) {
1056 irq_type = dc_link->irq_source_hpd - DC_IRQ_SOURCE_HPD1;
1057
1058 /* TODO: See same TODO in amdgpu_dm_hpd_init() */
1059 if (irq_type < adev->mode_info.num_hpd) {
1060 if (amdgpu_irq_put(adev, &adev->hpd_irq, irq_type))
1061 drm_err(dev, "DM_IRQ: Failed put HPD for source=%d!\n",
1062 dc_link->irq_source_hpd);
1063 } else {
1064 dc_interrupt_set(adev->dm.dc,
1065 dc_link->irq_source_hpd,
1066 false);
1067 }
1068 }
1069
1070 if (dc_link->irq_source_hpd_rx != DC_IRQ_SOURCE_INVALID) {
1071 dc_interrupt_set(adev->dm.dc,
1072 dc_link->irq_source_hpd_rx,
1073 false);
1074 }
1075 }
1076 drm_connector_list_iter_end(&iter);
1077
1078 if (dev->mode_config.poll_enabled)
1079 drm_kms_helper_poll_fini(dev);
1080 }
1081 EXPORT_IF_KUNIT(amdgpu_dm_hpd_fini);
1082
1083 /* ========== HPD handling ========== */
1084 static void force_connector_state(
1085 struct amdgpu_dm_connector *aconnector,
1086 enum drm_connector_force force_state)
1087 {
1088 struct drm_connector *connector = &aconnector->base;
1089
1090 mutex_lock(&connector->dev->mode_config.mutex);
1091 aconnector->base.force = force_state;
1092 mutex_unlock(&connector->dev->mode_config.mutex);
1093
1094 mutex_lock(&aconnector->hpd_lock);
1095 drm_kms_helper_connector_hotplug_event(connector);
1096 mutex_unlock(&aconnector->hpd_lock);
1097 }
1098
1099 STATIC_IFN_KUNIT void dm_handle_hpd_rx_offload_work(struct work_struct *work)
1100 {
1101 struct hpd_rx_irq_offload_work *offload_work;
1102 struct amdgpu_dm_connector *aconnector;
1103 struct dc_link *dc_link;
1104 struct amdgpu_device *adev;
1105 enum dc_connection_type new_connection_type = dc_connection_none;
1106 unsigned long flags;
1107 union test_response test_response;
1108
1109 memset(&test_response, 0, sizeof(test_response));
1110
1111 offload_work = container_of(work, struct hpd_rx_irq_offload_work, work);
1112 aconnector = offload_work->offload_wq->aconnector;
1113 adev = offload_work->adev;
1114
1115 if (!aconnector) {
1116 drm_err(adev_to_drm(adev), "Can't retrieve aconnector in hpd_rx_irq_offload_work");
1117 goto skip;
1118 }
1119
1120 dc_link = aconnector->dc_link;
1121
1122 mutex_lock(&aconnector->hpd_lock);
1123 if (!dc_link_detect_connection_type(dc_link, &new_connection_type))
1124 drm_err(adev_to_drm(adev), "KMS: Failed to detect connector\n");
1125 mutex_unlock(&aconnector->hpd_lock);
1126
1127 if (new_connection_type == dc_connection_none)
1128 goto skip;
1129
1130 if (amdgpu_in_reset(adev))
1131 goto skip;
1132
1133 if (offload_work->data.bytes.device_service_irq.bits.UP_REQ_MSG_RDY ||
1134 offload_work->data.bytes.device_service_irq.bits.DOWN_REP_MSG_RDY) {
1135 dm_handle_mst_sideband_msg_ready_event(&aconnector->mst_mgr, DOWN_OR_UP_MSG_RDY_EVENT);
1136 spin_lock_irqsave(&offload_work->offload_wq->offload_lock, flags);
1137 offload_work->offload_wq->is_handling_mst_msg_rdy_event = false;
1138 spin_unlock_irqrestore(&offload_work->offload_wq->offload_lock, flags);
1139 goto skip;
1140 }
1141
1142 mutex_lock(&adev->dm.dc_lock);
1143 if (offload_work->data.bytes.device_service_irq.bits.AUTOMATED_TEST) {
1144 dc_link_dp_handle_automated_test(dc_link);
1145
1146 if (aconnector->timing_changed) {
1147 /* force connector disconnect and reconnect */
1148 force_connector_state(aconnector, DRM_FORCE_OFF);
1149 msleep(100);
1150 force_connector_state(aconnector, DRM_FORCE_UNSPECIFIED);
1151 }
1152
1153 test_response.bits.ACK = 1;
1154
1155 core_link_write_dpcd(
1156 dc_link,
1157 DP_TEST_RESPONSE,
1158 &test_response.raw,
1159 sizeof(test_response));
1160 } else if ((dc_link->connector_signal != SIGNAL_TYPE_EDP) &&
1161 dc_link_check_link_loss_status(dc_link, &offload_work->data) &&
1162 dc_link_dp_allow_hpd_rx_irq(dc_link)) {
1163 /* offload_work->data is from handle_hpd_rx_irq->
1164 * schedule_hpd_rx_offload_work.this is defer handle
1165 * for hpd short pulse. upon here, link status may be
1166 * changed, need get latest link status from dpcd
1167 * registers. if link status is good, skip run link
1168 * training again.
1169 */
1170 union hpd_irq_data irq_data;
1171
1172 memset(&irq_data, 0, sizeof(irq_data));
1173
1174 /* before dc_link_dp_handle_link_loss, allow new link lost handle
1175 * request be added to work queue if link lost at end of dc_link_
1176 * dp_handle_link_loss
1177 */
1178 spin_lock_irqsave(&offload_work->offload_wq->offload_lock, flags);
1179 offload_work->offload_wq->is_handling_link_loss = false;
1180 spin_unlock_irqrestore(&offload_work->offload_wq->offload_lock, flags);
1181
1182 if ((dc_link_dp_read_hpd_rx_irq_data(dc_link, &irq_data) == DC_OK) &&
1183 dc_link_check_link_loss_status(dc_link, &irq_data))
1184 dc_link_dp_handle_link_loss(dc_link);
1185 }
1186 mutex_unlock(&adev->dm.dc_lock);
1187
1188 skip:
1189 kfree(offload_work);
1190
1191 }
1192 EXPORT_IF_KUNIT(dm_handle_hpd_rx_offload_work);
1193
1194 struct hpd_rx_irq_offload_work_queue *amdgpu_dm_hpd_rx_irq_create_workqueue(struct amdgpu_device *adev)
1195 {
1196 struct dc *dc = adev->dm.dc;
1197 int max_caps = dc->caps.max_links;
1198 int i = 0;
1199 struct hpd_rx_irq_offload_work_queue *hpd_rx_offload_wq = NULL;
1200
1201 hpd_rx_offload_wq = kzalloc_objs(*hpd_rx_offload_wq, max_caps);
1202
1203 if (!hpd_rx_offload_wq)
1204 return NULL;
1205
1206
1207 for (i = 0; i < max_caps; i++) {
1208 hpd_rx_offload_wq[i].wq =
1209 create_singlethread_workqueue("amdgpu_dm_hpd_rx_offload_wq");
1210
1211 if (hpd_rx_offload_wq[i].wq == NULL) {
1212 drm_err(adev_to_drm(adev), "create amdgpu_dm_hpd_rx_offload_wq fail!");
1213 goto out_err;
1214 }
1215
1216 spin_lock_init(&hpd_rx_offload_wq[i].offload_lock);
1217 }
1218
1219 return hpd_rx_offload_wq;
1220
1221 out_err:
1222 for (i = 0; i < max_caps; i++) {
1223 if (hpd_rx_offload_wq[i].wq)
1224 destroy_workqueue(hpd_rx_offload_wq[i].wq);
1225 }
1226 kfree(hpd_rx_offload_wq);
1227 return NULL;
1228 }
1229 EXPORT_IF_KUNIT(amdgpu_dm_hpd_rx_irq_create_workqueue);
1230
1231 void amdgpu_dm_hpd_rx_irq_work_suspend(struct amdgpu_display_manager *dm)
1232 {
1233 int i;
1234
1235 if (dm->hpd_rx_offload_wq) {
1236 for (i = 0; i < dm->dc->caps.max_links; i++)
1237 flush_workqueue(dm->hpd_rx_offload_wq[i].wq);
1238 }
1239 }
1240 EXPORT_IF_KUNIT(amdgpu_dm_hpd_rx_irq_work_suspend);
1241
1242 STATIC_IFN_KUNIT bool are_sinks_equal(const struct dc_sink *sink1, const struct dc_sink *sink2)
1243 {
1244 if (!sink1 || !sink2)
1245 return false;
1246 if (sink1->sink_signal != sink2->sink_signal)
1247 return false;
1248
1249 if (sink1->dc_edid.length != sink2->dc_edid.length)
1250 return false;
1251
1252 if (memcmp(sink1->dc_edid.raw_edid, sink2->dc_edid.raw_edid,
1253 sink1->dc_edid.length) != 0)
1254 return false;
1255 return true;
1256 }
1257 EXPORT_IF_KUNIT(are_sinks_equal);
1258
1259
1260 /**
1261 * DOC: amdgpu_dm_hdmi_hpd_debounce_work
1262 *
1263 * HDMI HPD debounce delay in milliseconds. When an HDMI display toggles HPD
1264 * (such as during power save transitions), this delay determines how long to
1265 * wait before processing the HPD event. This allows distinguishing between a
1266 * physical unplug (>hdmi_hpd_debounce_delay)
1267 * and a spontaneous RX HPD toggle (<hdmi_hpd_debounce_delay).
1268 *
1269 * If the toggle is less than this delay, the driver compares sink capabilities
1270 * and permits a hotplug event if they changed.
1271 *
1272 * The default value of 1500ms was chosen based on experimental testing with
1273 * various monitors that exhibit spontaneous HPD toggling behavior.
1274 */
1275 void amdgpu_dm_hdmi_hpd_debounce_work(struct work_struct *work)
1276 {
1277 struct amdgpu_dm_connector *aconnector =
1278 container_of(to_delayed_work(work), struct amdgpu_dm_connector,
1279 hdmi_hpd_debounce_work);
1280 struct drm_connector *connector = &aconnector->base;
1281 struct drm_device *dev = connector->dev;
1282 struct amdgpu_device *adev = drm_to_adev(dev);
1283 struct dc *dc = aconnector->dc_link->ctx->dc;
1284 bool fake_reconnect = false;
1285 bool reallow_idle = false;
1286 bool ret = false;
1287
1288 guard(mutex)(&aconnector->hpd_lock);
1289
1290 /* Re-detect the display */
1291 scoped_guard(mutex, &adev->dm.dc_lock) {
1292 if (dc->caps.ips_support && dc->ctx->dmub_srv->idle_allowed) {
1293 dc_allow_idle_optimizations(dc, false);
1294 reallow_idle = true;
1295 }
1296 ret = dc_link_detect(aconnector->dc_link, DETECT_REASON_HPD);
1297 }
1298
1299 if (ret) {
1300 /* Apply workaround delay for certain panels */
1301 amdgpu_dm_apply_delay_after_dpcd_poweroff(adev, aconnector->dc_sink);
1302 /* Compare sinks to determine if this was a spontaneous HPD toggle */
1303 if (are_sinks_equal(aconnector->dc_link->local_sink, aconnector->hdmi_prev_sink)) {
1304 /*
1305 * Sinks match - this was a spontaneous HDMI HPD toggle.
1306 */
1307 drm_dbg_kms(dev, "HDMI HPD: Sink unchanged after debounce, internal re-enable\n");
1308 fake_reconnect = true;
1309 }
1310
1311 /* Update connector state */
1312 amdgpu_dm_update_connector_after_detect(aconnector);
1313
1314 drm_modeset_lock_all(dev);
1315 dm_restore_drm_connector_state(dev, connector);
1316 drm_modeset_unlock_all(dev);
1317
1318 /* Only notify OS if sink actually changed */
1319 if (!fake_reconnect && aconnector->base.force == DRM_FORCE_UNSPECIFIED)
1320 drm_kms_helper_hotplug_event(dev);
1321 }
1322
1323 /* Release the cached sink reference */
1324 if (aconnector->hdmi_prev_sink) {
1325 dc_sink_release(aconnector->hdmi_prev_sink);
1326 aconnector->hdmi_prev_sink = NULL;
1327 }
1328
1329 scoped_guard(mutex, &adev->dm.dc_lock) {
1330 if (reallow_idle && dc->caps.ips_support)
1331 dc_allow_idle_optimizations(dc, true);
1332 }
1333 }
1334 EXPORT_IF_KUNIT(amdgpu_dm_hdmi_hpd_debounce_work);
1335
1336 STATIC_IFN_KUNIT void handle_hpd_irq_helper(struct amdgpu_dm_connector *aconnector,
1337 enum dc_detect_reason reason)
1338 {
1339 struct drm_connector *connector = &aconnector->base;
1340 struct drm_device *dev = connector->dev;
1341 enum dc_connection_type new_connection_type = dc_connection_none;
1342 struct amdgpu_device *adev = drm_to_adev(dev);
1343 struct dm_connector_state *dm_con_state = to_dm_connector_state(connector->state);
1344 struct dc *dc = aconnector->dc_link->ctx->dc;
1345 bool ret = false;
1346 bool debounce_required = false;
1347
1348 if (adev->dm.disable_hpd_irq)
1349 return;
1350
1351 /*
1352 * In case of failure or MST no need to update connector status or notify the OS
1353 * since (for MST case) MST does this in its own context.
1354 */
1355 guard(mutex)(&aconnector->hpd_lock);
1356
1357 if (adev->dm.hdcp_workqueue) {
1358 hdcp_reset_display(adev->dm.hdcp_workqueue, aconnector->dc_link->link_index);
1359 dm_con_state->update_hdcp = true;
1360 }
1361 if (aconnector->fake_enable)
1362 aconnector->fake_enable = false;
1363
1364 aconnector->timing_changed = false;
1365
1366 if (!dc_link_detect_connection_type(aconnector->dc_link, &new_connection_type))
1367 drm_err(adev_to_drm(adev), "KMS: Failed to detect connector\n");
1368
1369 /*
1370 * Check for HDMI disconnect with debounce enabled.
1371 */
1372 debounce_required = (aconnector->hdmi_hpd_debounce_delay_ms > 0 &&
1373 dc_is_hdmi_signal(aconnector->dc_link->connector_signal) &&
1374 new_connection_type == dc_connection_none &&
1375 aconnector->dc_link->local_sink != NULL);
1376
1377 if (aconnector->base.force && new_connection_type == dc_connection_none) {
1378 amdgpu_dm_emulated_link_detect(aconnector->dc_link);
1379
1380 drm_modeset_lock_all(dev);
1381 dm_restore_drm_connector_state(dev, connector);
1382 drm_modeset_unlock_all(dev);
1383
1384 if (aconnector->base.force == DRM_FORCE_UNSPECIFIED ||
1385 reason == DETECT_REASON_HPDRX)
1386 drm_kms_helper_connector_hotplug_event(connector);
1387 } else if (debounce_required) {
1388 /*
1389 * HDMI disconnect detected - schedule delayed work instead of
1390 * processing immediately. This allows us to coalesce spurious
1391 * HDMI signals from physical unplugs.
1392 */
1393 drm_dbg_kms(dev, "HDMI HPD: Disconnect detected, scheduling debounce work (%u ms)\n",
1394 aconnector->hdmi_hpd_debounce_delay_ms);
1395
1396 /* Cache the current sink for later comparison */
1397 if (aconnector->hdmi_prev_sink)
1398 dc_sink_release(aconnector->hdmi_prev_sink);
1399 aconnector->hdmi_prev_sink = aconnector->dc_link->local_sink;
1400 if (aconnector->hdmi_prev_sink)
1401 dc_sink_retain(aconnector->hdmi_prev_sink);
1402
1403 /* Schedule delayed detection. */
1404 if (mod_delayed_work(system_percpu_wq,
1405 &aconnector->hdmi_hpd_debounce_work,
1406 msecs_to_jiffies(aconnector->hdmi_hpd_debounce_delay_ms)))
1407 drm_dbg_kms(dev, "HDMI HPD: Re-scheduled debounce work\n");
1408
1409 } else {
1410
1411 /* If the aconnector->hdmi_hpd_debounce_work is scheduled, exit early */
1412 if (delayed_work_pending(&aconnector->hdmi_hpd_debounce_work))
1413 return;
1414
1415 scoped_guard(mutex, &adev->dm.dc_lock) {
1416 dc_exit_ips_for_hw_access(dc);
1417 ret = dc_link_detect(aconnector->dc_link, reason);
1418 }
1419 if (ret) {
1420 /* w/a delay for certain panels */
1421 amdgpu_dm_apply_delay_after_dpcd_poweroff(adev, aconnector->dc_sink);
1422 amdgpu_dm_update_connector_after_detect(aconnector);
1423
1424 drm_modeset_lock_all(dev);
1425 dm_restore_drm_connector_state(dev, connector);
1426 drm_modeset_unlock_all(dev);
1427
1428 if (aconnector->base.force == DRM_FORCE_UNSPECIFIED ||
1429 reason == DETECT_REASON_HPDRX)
1430 drm_kms_helper_connector_hotplug_event(connector);
1431 }
1432 }
1433 }
1434 EXPORT_IF_KUNIT(handle_hpd_irq_helper);
1435
1436 STATIC_IFN_KUNIT void handle_hpd_irq(void *param)
1437 {
1438 struct amdgpu_dm_connector *aconnector = (struct amdgpu_dm_connector *)param;
1439
1440 handle_hpd_irq_helper(aconnector, DETECT_REASON_HPD);
1441
1442 }
1443 EXPORT_IF_KUNIT(handle_hpd_irq);
1444
1445 STATIC_IFN_KUNIT void schedule_hpd_rx_offload_work(struct amdgpu_device *adev, struct hpd_rx_irq_offload_work_queue *offload_wq,
1446 union hpd_irq_data hpd_irq_data)
1447 {
1448 struct hpd_rx_irq_offload_work *offload_work = kzalloc_obj(*offload_work);
1449
1450 if (!offload_work) {
1451 drm_err(adev_to_drm(adev), "Failed to allocate hpd_rx_irq_offload_work.\n");
1452 return;
1453 }
1454
1455 INIT_WORK(&offload_work->work, dm_handle_hpd_rx_offload_work);
1456 offload_work->data = hpd_irq_data;
1457 offload_work->offload_wq = offload_wq;
1458 offload_work->adev = adev;
1459
1460 queue_work(offload_wq->wq, &offload_work->work);
1461 drm_dbg_kms(adev_to_drm(adev), "queue work to handle hpd_rx offload work");
1462 }
1463 EXPORT_IF_KUNIT(schedule_hpd_rx_offload_work);
1464
1465 STATIC_IFN_KUNIT void handle_hpd_rx_irq(void *param)
1466 {
1467 struct amdgpu_dm_connector *aconnector = (struct amdgpu_dm_connector *)param;
1468 struct drm_connector *connector = &aconnector->base;
1469 struct drm_device *dev = connector->dev;
1470 struct dc_link *dc_link = aconnector->dc_link;
1471 bool is_mst_root_connector = aconnector->mst_mgr.mst_state;
1472 bool result = false;
1473 enum dc_connection_type new_connection_type = dc_connection_none;
1474 struct amdgpu_device *adev = drm_to_adev(dev);
1475 union hpd_irq_data hpd_irq_data;
1476 bool link_loss = false;
1477 bool has_left_work = false;
1478 int idx = dc_link->link_index;
1479 struct hpd_rx_irq_offload_work_queue *offload_wq = &adev->dm.hpd_rx_offload_wq[idx];
1480 struct dc *dc = aconnector->dc_link->ctx->dc;
1481
1482 memset(&hpd_irq_data, 0, sizeof(hpd_irq_data));
1483
1484 if (adev->dm.disable_hpd_irq)
1485 return;
1486
1487 /*
1488 * TODO:Temporary add mutex to protect hpd interrupt not have a gpio
1489 * conflict, after implement i2c helper, this mutex should be
1490 * retired.
1491 */
1492 mutex_lock(&aconnector->hpd_lock);
1493
1494 result = dc_link_handle_hpd_rx_irq(dc_link, &hpd_irq_data,
1495 &link_loss, true, &has_left_work);
1496
1497 if (!has_left_work)
1498 goto out;
1499
1500 if (hpd_irq_data.bytes.device_service_irq.bits.AUTOMATED_TEST) {
1501 schedule_hpd_rx_offload_work(adev, offload_wq, hpd_irq_data);
1502 goto out;
1503 }
1504
1505 if (dc_link_dp_allow_hpd_rx_irq(dc_link)) {
1506 if (hpd_irq_data.bytes.device_service_irq.bits.UP_REQ_MSG_RDY ||
1507 hpd_irq_data.bytes.device_service_irq.bits.DOWN_REP_MSG_RDY) {
1508 bool skip = false;
1509
1510 /*
1511 * DOWN_REP_MSG_RDY is also handled by polling method
1512 * mgr->cbs->poll_hpd_irq()
1513 */
1514 spin_lock(&offload_wq->offload_lock);
1515 skip = offload_wq->is_handling_mst_msg_rdy_event;
1516
1517 if (!skip)
1518 offload_wq->is_handling_mst_msg_rdy_event = true;
1519
1520 spin_unlock(&offload_wq->offload_lock);
1521
1522 if (!skip)
1523 schedule_hpd_rx_offload_work(adev, offload_wq, hpd_irq_data);
1524
1525 goto out;
1526 }
1527
1528 if (link_loss) {
1529 bool skip = false;
1530
1531 spin_lock(&offload_wq->offload_lock);
1532 skip = offload_wq->is_handling_link_loss;
1533
1534 if (!skip)
1535 offload_wq->is_handling_link_loss = true;
1536
1537 spin_unlock(&offload_wq->offload_lock);
1538
1539 if (!skip)
1540 schedule_hpd_rx_offload_work(adev, offload_wq, hpd_irq_data);
1541
1542 goto out;
1543 }
1544 }
1545
1546 out:
1547 if (result && !is_mst_root_connector) {
1548 /* Downstream Port status changed. */
1549 if (!dc_link_detect_connection_type(dc_link, &new_connection_type))
1550 drm_err(adev_to_drm(adev), "KMS: Failed to detect connector\n");
1551
1552 if (aconnector->base.force && new_connection_type == dc_connection_none) {
1553 amdgpu_dm_emulated_link_detect(dc_link);
1554
1555 if (aconnector->fake_enable)
1556 aconnector->fake_enable = false;
1557
1558 amdgpu_dm_update_connector_after_detect(aconnector);
1559
1560
1561 drm_modeset_lock_all(dev);
1562 dm_restore_drm_connector_state(dev, connector);
1563 drm_modeset_unlock_all(dev);
1564
1565 drm_kms_helper_connector_hotplug_event(connector);
1566 } else {
1567 bool ret = false;
1568
1569 mutex_lock(&adev->dm.dc_lock);
1570 dc_exit_ips_for_hw_access(dc);
1571 ret = dc_link_detect(dc_link, DETECT_REASON_HPDRX);
1572 mutex_unlock(&adev->dm.dc_lock);
1573
1574 if (ret) {
1575 if (aconnector->fake_enable)
1576 aconnector->fake_enable = false;
1577
1578 amdgpu_dm_update_connector_after_detect(aconnector);
1579
1580 drm_modeset_lock_all(dev);
1581 dm_restore_drm_connector_state(dev, connector);
1582 drm_modeset_unlock_all(dev);
1583
1584 drm_kms_helper_connector_hotplug_event(connector);
1585 }
1586 }
1587 }
1588 if (hpd_irq_data.bytes.device_service_irq.bits.CP_IRQ) {
1589 if (adev->dm.hdcp_workqueue)
1590 hdcp_handle_cpirq(adev->dm.hdcp_workqueue, aconnector->base.index);
1591 }
1592
1593 if (dc_link->type != dc_connection_mst_branch)
1594 drm_dp_cec_irq(&aconnector->dm_dp_aux.aux);
1595
1596 mutex_unlock(&aconnector->hpd_lock);
1597 }
1598 EXPORT_IF_KUNIT(handle_hpd_rx_irq);
1599
1600 /**
1601 * dmub_hpd_callback - DMUB HPD interrupt processing callback.
1602 * @adev: amdgpu_device pointer
1603 * @notify: dmub notification structure
1604 *
1605 * Dmub Hpd interrupt processing callback. Gets displayindex through the
1606 * ink index and calls helper to do the processing.
1607 */
1608 STATIC_IFN_KUNIT void dmub_hpd_callback(struct amdgpu_device *adev,
1609 struct dmub_notification *notify)
1610 {
1611 struct amdgpu_dm_connector *aconnector;
1612 struct amdgpu_dm_connector *hpd_aconnector = NULL;
1613 struct drm_connector *connector;
1614 struct drm_connector_list_iter iter;
1615 struct dc_link *link;
1616 u8 link_index = 0;
1617 struct drm_device *dev;
1618
1619 if (adev == NULL)
1620 return;
1621
1622 if (notify == NULL) {
1623 drm_err(adev_to_drm(adev), "DMUB HPD callback notification was NULL");
1624 return;
1625 }
1626
1627 if (notify->link_index > adev->dm.dc->link_count) {
1628 drm_err(adev_to_drm(adev), "DMUB HPD index (%u)is abnormal", notify->link_index);
1629 return;
1630 }
1631
1632 /* Skip DMUB HPD IRQ in suspend/resume. We will probe them later. */
1633 if (notify->type == DMUB_NOTIFICATION_HPD && adev->in_suspend) {
1634 drm_info(adev_to_drm(adev), "Skip DMUB HPD IRQ callback in suspend/resume\n");
1635 return;
1636 }
1637
1638 link_index = notify->link_index;
1639 link = adev->dm.dc->links[link_index];
1640 dev = adev->dm.ddev;
1641
1642 drm_connector_list_iter_begin(dev, &iter);
1643 drm_for_each_connector_iter(connector, &iter) {
1644
1645 if (connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK)
1646 continue;
1647
1648 aconnector = to_amdgpu_dm_connector(connector);
1649 if (link && aconnector->dc_link == link) {
1650 if (notify->type == DMUB_NOTIFICATION_HPD)
1651 drm_info(adev_to_drm(adev), "DMUB HPD IRQ callback: link_index=%u\n", link_index);
1652 else if (notify->type == DMUB_NOTIFICATION_HPD_IRQ)
1653 drm_info(adev_to_drm(adev), "DMUB HPD RX IRQ callback: link_index=%u\n", link_index);
1654 else
1655 drm_warn(adev_to_drm(adev), "DMUB Unknown HPD callback type %d, link_index=%u\n",
1656 notify->type, link_index);
1657
1658 hpd_aconnector = aconnector;
1659 break;
1660 }
1661 }
1662 drm_connector_list_iter_end(&iter);
1663
1664 if (hpd_aconnector) {
1665 if (notify->type == DMUB_NOTIFICATION_HPD) {
1666 if (hpd_aconnector->dc_link->hpd_status == (notify->hpd_status == DP_HPD_PLUG))
1667 drm_warn(adev_to_drm(adev), "DMUB reported hpd status unchanged. link_index=%u\n", link_index);
1668 handle_hpd_irq_helper(hpd_aconnector, DETECT_REASON_HPD);
1669 } else if (notify->type == DMUB_NOTIFICATION_HPD_IRQ) {
1670 handle_hpd_rx_irq(hpd_aconnector);
1671 }
1672 }
1673 }
1674 EXPORT_IF_KUNIT(dmub_hpd_callback);
1675
1676 /**
1677 * dmub_hpd_sense_callback - DMUB HPD sense processing callback.
1678 * @adev: amdgpu_device pointer
1679 * @notify: dmub notification structure
1680 *
1681 * HPD sense changes can occur during low power states and need to be
1682 * notified from firmware to driver.
1683 */
1684 STATIC_IFN_KUNIT void dmub_hpd_sense_callback(struct amdgpu_device *adev,
1685 struct dmub_notification *notify)
1686 {
1687 drm_dbg_driver(adev_to_drm(adev), "DMUB HPD SENSE callback.\n");
1688 }
1689 EXPORT_IF_KUNIT(dmub_hpd_sense_callback);
1690
1691 int amdgpu_dm_register_hpd_handlers(struct amdgpu_device *adev)
1692 {
1693 struct drm_device *dev = adev_to_drm(adev);
1694 struct drm_connector *connector;
1695 struct amdgpu_dm_connector *aconnector;
1696 const struct dc_link *dc_link;
1697 struct dc_interrupt_params int_params = {0};
1698
1699 int_params.requested_polarity = INTERRUPT_POLARITY_DEFAULT;
1700 int_params.current_polarity = INTERRUPT_POLARITY_DEFAULT;
1701
1702 if (dc_is_dmub_outbox_supported(adev->dm.dc)) {
1703 if (!dm_register_dmub_notify_callback(adev, DMUB_NOTIFICATION_HPD,
1704 dmub_hpd_callback, true)) {
1705 drm_err(adev_to_drm(adev), "fail to register dmub hpd callback");
1706 return -EINVAL;
1707 }
1708
1709 if (!dm_register_dmub_notify_callback(adev, DMUB_NOTIFICATION_HPD_IRQ,
1710 dmub_hpd_callback, true)) {
1711 drm_err(adev_to_drm(adev), "fail to register dmub hpd callback");
1712 return -EINVAL;
1713 }
1714
1715 if (!dm_register_dmub_notify_callback(adev, DMUB_NOTIFICATION_HPD_SENSE_NOTIFY,
1716 dmub_hpd_sense_callback, true)) {
1717 drm_err(adev_to_drm(adev), "fail to register dmub hpd sense callback");
1718 return -EINVAL;
1719 }
1720 }
1721
1722 list_for_each_entry(connector,
1723 &dev->mode_config.connector_list, head) {
1724
1725 if (connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK)
1726 continue;
1727
1728 aconnector = to_amdgpu_dm_connector(connector);
1729 dc_link = aconnector->dc_link;
1730
1731 if (dc_link->irq_source_hpd != DC_IRQ_SOURCE_INVALID) {
1732 int_params.int_context = INTERRUPT_LOW_IRQ_CONTEXT;
1733 int_params.irq_source = dc_link->irq_source_hpd;
1734
1735 if (int_params.irq_source == DC_IRQ_SOURCE_INVALID ||
1736 int_params.irq_source < DC_IRQ_SOURCE_HPD1 ||
1737 int_params.irq_source > DC_IRQ_SOURCE_HPD6) {
1738 drm_err(adev_to_drm(adev),
1739 "Failed to register hpd irq %u for %s!\n",
1740 int_params.irq_source, connector->name);
1741 return -EINVAL;
1742 }
1743
1744 if (!amdgpu_dm_irq_register_interrupt(adev, &int_params,
1745 handle_hpd_irq, (void *) aconnector))
1746 return -ENOMEM;
1747 }
1748
1749 if (dc_link->irq_source_hpd_rx != DC_IRQ_SOURCE_INVALID) {
1750
1751 /* Also register for DP short pulse (hpd_rx). */
1752 int_params.int_context = INTERRUPT_LOW_IRQ_CONTEXT;
1753 int_params.irq_source = dc_link->irq_source_hpd_rx;
1754
1755 if (int_params.irq_source == DC_IRQ_SOURCE_INVALID ||
1756 int_params.irq_source < DC_IRQ_SOURCE_HPD1RX ||
1757 int_params.irq_source > DC_IRQ_SOURCE_HPD6RX) {
1758 drm_err(adev_to_drm(adev),
1759 "Failed to register hpd rx irq %u for %s!\n",
1760 int_params.irq_source, connector->name);
1761 return -EINVAL;
1762 }
1763
1764 if (!amdgpu_dm_irq_register_interrupt(adev, &int_params,
1765 handle_hpd_rx_irq, (void *) aconnector))
1766 return -ENOMEM;
1767 }
1768 }
1769 return 0;
1770 }
1771 EXPORT_IF_KUNIT(amdgpu_dm_register_hpd_handlers);
1772
1773 /* ========== IRQ handlers ========== */
1774 struct amdgpu_crtc *
1775 amdgpu_dm_get_crtc_by_otg_inst(struct amdgpu_device *adev,
1776 int otg_inst)
1777 {
1778 struct drm_device *dev = adev_to_drm(adev);
1779 struct drm_crtc *crtc;
1780 struct amdgpu_crtc *amdgpu_crtc;
1781
1782 if (WARN_ON(otg_inst == -1))
1783 return adev->mode_info.crtcs[0];
1784
1785 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1786 amdgpu_crtc = to_amdgpu_crtc(crtc);
1787
1788 if (amdgpu_crtc->otg_inst == otg_inst)
1789 return amdgpu_crtc;
1790 }
1791
1792 return NULL;
1793 }
1794 EXPORT_IF_KUNIT(amdgpu_dm_get_crtc_by_otg_inst);
1795
1796 /**
1797 * dm_pflip_high_irq() - Handle pageflip interrupt
1798 * @interrupt_params: ignored
1799 *
1800 * Handles the pageflip interrupt by notifying all interested parties
1801 * that the pageflip has been completed.
1802 */
1803 STATIC_IFN_KUNIT void dm_pflip_high_irq(void *interrupt_params)
1804 {
1805 struct amdgpu_crtc *amdgpu_crtc;
1806 struct common_irq_params *irq_params = interrupt_params;
1807 struct amdgpu_device *adev = irq_params->adev;
1808 struct drm_device *dev = adev_to_drm(adev);
1809 unsigned long flags;
1810 struct drm_pending_vblank_event *e;
1811 u32 vpos, hpos, v_blank_start, v_blank_end;
1812 bool vrr_active;
1813
1814 amdgpu_crtc = amdgpu_dm_get_crtc_by_otg_inst(adev, irq_params->irq_src - IRQ_TYPE_PFLIP);
1815
1816 /* IRQ could occur when in initial stage */
1817 /* TODO work and BO cleanup */
1818 if (amdgpu_crtc == NULL) {
1819 drm_dbg_state(dev, "CRTC is null, returning.\n");
1820 return;
1821 }
1822
1823 spin_lock_irqsave(&adev_to_drm(adev)->event_lock, flags);
1824
1825 if (amdgpu_crtc->pflip_status != AMDGPU_FLIP_SUBMITTED) {
1826 drm_dbg_state(dev,
1827 "amdgpu_crtc->pflip_status = %d != AMDGPU_FLIP_SUBMITTED(%d) on crtc:%d[%p]\n",
1828 amdgpu_crtc->pflip_status, AMDGPU_FLIP_SUBMITTED,
1829 amdgpu_crtc->crtc_id, amdgpu_crtc);
1830 spin_unlock_irqrestore(&adev_to_drm(adev)->event_lock, flags);
1831 return;
1832 }
1833
1834 /* page flip completed. */
1835 e = amdgpu_crtc->event;
1836 amdgpu_crtc->event = NULL;
1837
1838 WARN_ON(!e);
1839
1840 vrr_active = amdgpu_dm_crtc_vrr_active_irq(amdgpu_crtc);
1841
1842 /* Fixed refresh rate, or VRR scanout position outside front-porch? */
1843 if (!vrr_active ||
1844 !dc_stream_get_scanoutpos(amdgpu_crtc->dm_irq_params.stream, &v_blank_start,
1845 &v_blank_end, &hpos, &vpos) ||
1846 (vpos < v_blank_start)) {
1847 /* Update to correct count and vblank timestamp if racing with
1848 * vblank irq. This also updates to the correct vblank timestamp
1849 * even in VRR mode, as scanout is past the front-porch atm.
1850 */
1851 drm_crtc_accurate_vblank_count(&amdgpu_crtc->base);
1852
1853 /* Wake up userspace by sending the pageflip event with proper
1854 * count and timestamp of vblank of flip completion.
1855 */
1856 if (e) {
1857 drm_crtc_send_vblank_event(&amdgpu_crtc->base, e);
1858
1859 /* Event sent, so done with vblank for this flip */
1860 drm_crtc_vblank_put(&amdgpu_crtc->base);
1861 }
1862 } else if (e) {
1863 /* VRR active and inside front-porch: vblank count and
1864 * timestamp for pageflip event will only be up to date after
1865 * drm_crtc_handle_vblank() has been executed from late vblank
1866 * irq handler after start of back-porch (vline 0). We queue the
1867 * pageflip event for send-out by drm_crtc_handle_vblank() with
1868 * updated timestamp and count, once it runs after us.
1869 *
1870 * We need to open-code this instead of using the helper
1871 * drm_crtc_arm_vblank_event(), as that helper would
1872 * call drm_crtc_accurate_vblank_count(), which we must
1873 * not call in VRR mode while we are in front-porch!
1874 */
1875
1876 /* sequence will be replaced by real count during send-out. */
1877 e->sequence = drm_crtc_vblank_count(&amdgpu_crtc->base);
1878 e->pipe = amdgpu_crtc->crtc_id;
1879
1880 list_add_tail(&e->base.link, &adev_to_drm(adev)->vblank_event_list);
1881 e = NULL;
1882 }
1883
1884 /* Keep track of vblank of this flip for flip throttling. We use the
1885 * cooked hw counter, as that one incremented at start of this vblank
1886 * of pageflip completion, so last_flip_vblank is the forbidden count
1887 * for queueing new pageflips if vsync + VRR is enabled.
1888 */
1889 amdgpu_crtc->dm_irq_params.last_flip_vblank =
1890 amdgpu_get_vblank_counter_kms(&amdgpu_crtc->base);
1891
1892 amdgpu_crtc->pflip_status = AMDGPU_FLIP_NONE;
1893 spin_unlock_irqrestore(&adev_to_drm(adev)->event_lock, flags);
1894
1895 drm_dbg_state(dev,
1896 "crtc:%d[%p], pflip_stat:AMDGPU_FLIP_NONE, vrr[%d]-fp %d\n",
1897 amdgpu_crtc->crtc_id, amdgpu_crtc, vrr_active, (int)!e);
1898 }
1899 EXPORT_IF_KUNIT(dm_pflip_high_irq);
1900
1901 STATIC_IFN_KUNIT void dm_handle_vmin_vmax_update(struct work_struct *offload_work)
1902 {
1903 struct vupdate_offload_work *work = container_of(offload_work, struct vupdate_offload_work, work);
1904 struct amdgpu_device *adev = work->adev;
1905 struct dc_stream_state *stream = work->stream;
1906 struct dc_crtc_timing_adjust *adjust = work->adjust;
1907
1908 mutex_lock(&adev->dm.dc_lock);
1909 dc_stream_adjust_vmin_vmax(adev->dm.dc, stream, adjust);
1910 mutex_unlock(&adev->dm.dc_lock);
1911
1912 dc_stream_release(stream);
1913 kfree(work->adjust);
1914 kfree(work);
1915 }
1916 EXPORT_IF_KUNIT(dm_handle_vmin_vmax_update);
1917
1918 static void schedule_dc_vmin_vmax(struct amdgpu_device *adev,
1919 struct dc_stream_state *stream,
1920 struct dc_crtc_timing_adjust *adjust)
1921 {
1922 struct vupdate_offload_work *offload_work = kzalloc_obj(*offload_work,
1923 GFP_NOWAIT);
1924 if (!offload_work) {
1925 drm_dbg_driver(adev_to_drm(adev), "Failed to allocate vupdate_offload_work\n");
1926 return;
1927 }
1928
1929 struct dc_crtc_timing_adjust *adjust_copy = kzalloc_obj(*adjust_copy,
1930 GFP_NOWAIT);
1931 if (!adjust_copy) {
1932 drm_dbg_driver(adev_to_drm(adev), "Failed to allocate adjust_copy\n");
1933 kfree(offload_work);
1934 return;
1935 }
1936
1937 dc_stream_retain(stream);
1938 memcpy(adjust_copy, adjust, sizeof(*adjust_copy));
1939
1940 INIT_WORK(&offload_work->work, dm_handle_vmin_vmax_update);
1941 offload_work->adev = adev;
1942 offload_work->stream = stream;
1943 offload_work->adjust = adjust_copy;
1944
1945 queue_work(adev->dm.vmin_vmax_wq, &offload_work->work);
1946 }
1947
1948 /**
1949 * dm_crtc_high_irq_handler() - Common OTG vblank/flip event handling
1950 * @adev: amdgpu device
1951 * @acrtc: the CRTC to service
1952 * Performs writeback completion, vblank event handling, CRC processing, VRR BTR
1953 * updates and pageflip completion delivery.
1954 *
1955 * On DCN this is driven by VUPDATE_NO_LOCK (the register latch point) from
1956 * dm_vupdate_high_irq(); on DCE it is driven by VLINE0 at the start of vblank
1957 * from dm_crtc_high_irq().
1958 */
1959 static void dm_crtc_high_irq_handler(struct amdgpu_device *adev,
1960 struct amdgpu_crtc *acrtc)
1961 {
1962 unsigned long flags;
1963 int vrr_active;
1964 bool is_dcn = amdgpu_ip_version(adev, DCE_HWIP, 0) != 0;
1965
1966 if (acrtc->wb_conn && acrtc->wb_pending) {
1967 if (acrtc->wb_frame_done) {
1968 /*
1969 * Second vblank: the DMA for the captured frame has
1970 * had a full frame period to flush to memory. Signal
1971 * the out fence now.
1972 */
1973 amdgpu_dm_crtc_complete_writeback(acrtc);
1974 } else {
1975 /*
1976 * First vblank after arming: the frame has been
1977 * scanned out and the DMA is finishing. Disable
1978 * writeback immediately to prevent the hardware from
1979 * starting a new capture that would overwrite the
1980 * buffer. Signal completion on the next vblank to
1981 * ensure the DMA is fully flushed to memory.
1982 */
1983 dc_stream_fc_disable_writeback(adev->dm.dc,
1984 acrtc->dm_irq_params.stream, 0);
1985 acrtc->wb_frame_done = true;
1986 }
1987 }
1988
1989 vrr_active = amdgpu_dm_crtc_vrr_active_irq(acrtc);
1990
1991 drm_dbg_vbl(adev_to_drm(adev),
1992 "crtc:%d, vupdate-vrr:%d, planes:%d\n", acrtc->crtc_id,
1993 vrr_active, acrtc->dm_irq_params.active_planes);
1994
1995 /**
1996 * Core vblank handling.
1997 *
1998 * On DCN this handler runs at VUPDATE_NO_LOCK, the register latch
1999 * point, which is the correct place to timestamp both VRR and non-VRR
2000 * vblanks.
2001 *
2002 * On DCE this handler runs at the start of front-porch, where only
2003 * non-VRR timestamping is valid; VRR vblank is deferred to
2004 * dm_vupdate_high_irq() after end of front-porch.
2005 */
2006 if (is_dcn || !vrr_active)
2007 amdgpu_dm_crtc_handle_vblank(acrtc);
2008
2009 /**
2010 * Following stuff must happen at start of vblank, for crc
2011 * computation and below-the-range btr support in vrr mode.
2012 */
2013 amdgpu_dm_crtc_handle_crc_irq(&acrtc->base);
2014
2015 /* BTR updates need to happen before VUPDATE on Vega and above. */
2016 if (adev->family < AMDGPU_FAMILY_AI)
2017 return;
2018
2019 spin_lock_irqsave(&adev_to_drm(adev)->event_lock, flags);
2020
2021 if (acrtc->dm_irq_params.stream &&
2022 acrtc->dm_irq_params.vrr_params.supported) {
2023 bool replay_en = acrtc->dm_irq_params.stream->link->replay_settings.replay_feature_enabled;
2024 bool psr_en = acrtc->dm_irq_params.stream->link->psr_settings.psr_feature_enabled;
2025 bool fs_active_var_en = acrtc->dm_irq_params.freesync_config.state == VRR_STATE_ACTIVE_VARIABLE;
2026
2027 mod_freesync_handle_v_update(adev->dm.freesync_module,
2028 acrtc->dm_irq_params.stream,
2029 &acrtc->dm_irq_params.vrr_params);
2030
2031 /* update vmin_vmax only if freesync is enabled, or only if PSR and REPLAY are disabled */
2032 if (fs_active_var_en || (!fs_active_var_en && !replay_en && !psr_en)) {
2033 schedule_dc_vmin_vmax(adev, acrtc->dm_irq_params.stream,
2034 &acrtc->dm_irq_params.vrr_params.adjust);
2035 }
2036 }
2037
2038 /*
2039 * Deliver pageflip completion events (DCN only).
2040 *
2041 * Since GRPH_PFLIP is not used, VUPDATE_NO_LOCK is the flip latch
2042 * point. Deliver any pending pageflip completion event from here,
2043 * once HW has consumed the new address (the OTG no longer reports a
2044 * pending flip).
2045 *
2046 * Also handle the case here where there aren't any active planes and
2047 * DCN HUBP may be clock-gated, so the flip-pending status may be
2048 * undefined.
2049 */
2050 if (is_dcn && acrtc->pflip_status == AMDGPU_FLIP_SUBMITTED &&
2051 acrtc->event) {
2052
2053 if (!dc_get_flip_pending_on_otg(adev->dm.dc, acrtc->otg_inst)) {
2054 drm_crtc_send_vblank_event(&acrtc->base, acrtc->event);
2055 acrtc->event = NULL;
2056 drm_crtc_vblank_put(&acrtc->base);
2057 acrtc->pflip_status = AMDGPU_FLIP_NONE;
2058 }
2059 /*
2060 * If the flip is still pending, leave it armed and
2061 * retry on the next vupdate.
2062 */
2063 } else if (is_dcn && acrtc->pflip_status == AMDGPU_FLIP_SUBMITTED &&
2064 acrtc->dm_irq_params.active_planes == 0) {
2065
2066 if (acrtc->event) {
2067 drm_crtc_send_vblank_event(&acrtc->base, acrtc->event);
2068 acrtc->event = NULL;
2069 drm_crtc_vblank_put(&acrtc->base);
2070 }
2071 acrtc->pflip_status = AMDGPU_FLIP_NONE;
2072 }
2073
2074 spin_unlock_irqrestore(&adev_to_drm(adev)->event_lock, flags);
2075 }
2076
2077 STATIC_IFN_KUNIT void dm_vupdate_high_irq(void *interrupt_params)
2078 {
2079 struct common_irq_params *irq_params = interrupt_params;
2080 struct amdgpu_device *adev = irq_params->adev;
2081 struct amdgpu_crtc *acrtc;
2082 struct drm_device *drm_dev;
2083 struct drm_vblank_crtc *vblank;
2084 ktime_t frame_duration_ns, previous_timestamp;
2085 unsigned long flags;
2086 int vrr_active;
2087
2088 acrtc = amdgpu_dm_get_crtc_by_otg_inst(adev, irq_params->irq_src - IRQ_TYPE_VUPDATE);
2089 if (!acrtc)
2090 return;
2091
2092 vrr_active = amdgpu_dm_crtc_vrr_active_irq(acrtc);
2093 drm_dev = acrtc->base.dev;
2094 vblank = drm_crtc_vblank_crtc(&acrtc->base);
2095 previous_timestamp = atomic64_read(&irq_params->previous_timestamp);
2096 frame_duration_ns = vblank->time - previous_timestamp;
2097
2098 if (frame_duration_ns > 0) {
2099 trace_amdgpu_refresh_rate_track(acrtc->base.index,
2100 frame_duration_ns,
2101 ktime_divns(NSEC_PER_SEC, frame_duration_ns));
2102 atomic64_set(&irq_params->previous_timestamp, vblank->time);
2103 }
2104
2105 drm_dbg_vbl(drm_dev,
2106 "crtc:%d, vupdate-vrr:%d\n", acrtc->crtc_id,
2107 vrr_active);
2108
2109 /*
2110 * On DCN, VUPDATE_NO_LOCK is the single OTG interrupt used to deliver
2111 * vblank and pageflip completion events; VSTARTUP and GRPH_PFLIP are
2112 * not used. Run the full handler here.
2113 */
2114 if (amdgpu_ip_version(adev, DCE_HWIP, 0) != 0) {
2115 dm_crtc_high_irq_handler(adev, acrtc);
2116 return;
2117 }
2118
2119 /* DCE only below. */
2120
2121 /* Core vblank handling is done here after end of front-porch in
2122 * vrr mode, as vblank timestamping will give valid results
2123 * while now done after front-porch. This will also deliver
2124 * page-flip completion events that have been queued to us
2125 * if a pageflip happened inside front-porch.
2126 */
2127 if (vrr_active && acrtc->dm_irq_params.stream) {
2128 bool replay_en = acrtc->dm_irq_params.stream->link->replay_settings.replay_feature_enabled;
2129 bool psr_en = acrtc->dm_irq_params.stream->link->psr_settings.psr_feature_enabled;
2130 bool fs_active_var_en = acrtc->dm_irq_params.freesync_config.state
2131 == VRR_STATE_ACTIVE_VARIABLE;
2132
2133 amdgpu_dm_crtc_handle_vblank(acrtc);
2134
2135 /* BTR processing for pre-DCE12 ASICs */
2136 if (adev->family < AMDGPU_FAMILY_AI) {
2137 spin_lock_irqsave(&adev_to_drm(adev)->event_lock, flags);
2138 mod_freesync_handle_v_update(
2139 adev->dm.freesync_module,
2140 acrtc->dm_irq_params.stream,
2141 &acrtc->dm_irq_params.vrr_params);
2142
2143 if (fs_active_var_en || (!fs_active_var_en && !replay_en && !psr_en)) {
2144 schedule_dc_vmin_vmax(adev,
2145 acrtc->dm_irq_params.stream,
2146 &acrtc->dm_irq_params.vrr_params.adjust);
2147 }
2148 spin_unlock_irqrestore(&adev_to_drm(adev)->event_lock, flags);
2149 }
2150 }
2151 }
2152 EXPORT_IF_KUNIT(dm_vupdate_high_irq);
2153
2154 /**
2155 * dm_crtc_high_irq() - Handles CRTC interrupt
2156 * @interrupt_params: used for determining the CRTC instance
2157 *
2158 * Handles the CRTC/VSYNC interrupt by notifying DRM's VBLANK event handler.
2159 *
2160 * Used on DCE (VLINE0, set to vblank start). On DCN the equivalent handling is
2161 * driven by VUPDATE_NO_LOCK in dm_vupdate_high_irq().
2162 */
2163 STATIC_IFN_KUNIT void dm_crtc_high_irq(void *interrupt_params)
2164 {
2165 struct common_irq_params *irq_params = interrupt_params;
2166 struct amdgpu_device *adev = irq_params->adev;
2167 struct amdgpu_crtc *acrtc;
2168
2169 acrtc = amdgpu_dm_get_crtc_by_otg_inst(adev, irq_params->irq_src - IRQ_TYPE_VBLANK);
2170 if (!acrtc)
2171 return;
2172
2173 dm_crtc_high_irq_handler(adev, acrtc);
2174 }
2175 EXPORT_IF_KUNIT(dm_crtc_high_irq);
2176
2177 #if defined(CONFIG_DRM_AMD_SECURE_DISPLAY)
2178 /**
2179 * dm_dcn_vertical_interrupt0_high_irq() - Handles OTG Vertical interrupt0 for
2180 * DCN generation ASICs
2181 * @interrupt_params: interrupt parameters
2182 *
2183 * Used to set crc window/read out crc value at vertical line 0 position
2184 */
2185 static void dm_dcn_vertical_interrupt0_high_irq(void *interrupt_params)
2186 {
2187 struct common_irq_params *irq_params = interrupt_params;
2188 struct amdgpu_device *adev = irq_params->adev;
2189 struct amdgpu_crtc *acrtc;
2190
2191 acrtc = amdgpu_dm_get_crtc_by_otg_inst(adev, irq_params->irq_src - IRQ_TYPE_VLINE0);
2192
2193 if (!acrtc)
2194 return;
2195
2196 amdgpu_dm_crtc_handle_crc_window_irq(&acrtc->base);
2197 }
2198 #endif /* CONFIG_DRM_AMD_SECURE_DISPLAY */
2199
2200 STATIC_IFN_KUNIT void dm_handle_hpd_work(struct work_struct *work)
2201 {
2202 struct dmub_hpd_work *dmub_hpd_wrk;
2203
2204 dmub_hpd_wrk = container_of(work, struct dmub_hpd_work, handle_hpd_work);
2205
2206 if (!dmub_hpd_wrk->dmub_notify) {
2207 drm_err(adev_to_drm(dmub_hpd_wrk->adev), "dmub_hpd_wrk dmub_notify is NULL");
2208 return;
2209 }
2210
2211 if (dmub_hpd_wrk->dmub_notify->type < ARRAY_SIZE(dmub_hpd_wrk->adev->dm.dmub_callback)) {
2212 dmub_hpd_wrk->adev->dm.dmub_callback[dmub_hpd_wrk->dmub_notify->type](dmub_hpd_wrk->adev,
2213 dmub_hpd_wrk->dmub_notify);
2214 }
2215
2216 kfree(dmub_hpd_wrk->dmub_notify);
2217 kfree(dmub_hpd_wrk);
2218
2219 }
2220 EXPORT_IF_KUNIT(dm_handle_hpd_work);
2221
2222 STATIC_IFN_KUNIT const char *dmub_notification_type_str(enum dmub_notification_type e)
2223 {
2224 switch (e) {
2225 case DMUB_NOTIFICATION_NO_DATA:
2226 return "NO_DATA";
2227 case DMUB_NOTIFICATION_AUX_REPLY:
2228 return "AUX_REPLY";
2229 case DMUB_NOTIFICATION_HPD:
2230 return "HPD";
2231 case DMUB_NOTIFICATION_HPD_IRQ:
2232 return "HPD_IRQ";
2233 case DMUB_NOTIFICATION_SET_CONFIG_REPLY:
2234 return "SET_CONFIG_REPLY";
2235 case DMUB_NOTIFICATION_DPIA_NOTIFICATION:
2236 return "DPIA_NOTIFICATION";
2237 case DMUB_NOTIFICATION_HPD_SENSE_NOTIFY:
2238 return "HPD_SENSE_NOTIFY";
2239 case DMUB_NOTIFICATION_FUSED_IO:
2240 return "FUSED_IO";
2241 default:
2242 return "<unknown>";
2243 }
2244 }
2245 EXPORT_IF_KUNIT(dmub_notification_type_str);
2246
2247 #define DMUB_TRACE_MAX_READ 64
2248 /**
2249 * dm_dmub_outbox1_low_irq() - Handles Outbox interrupt
2250 * @interrupt_params: used for determining the Outbox instance
2251 *
2252 * Handles the Outbox Interrupt
2253 * event handler.
2254 */
2255 STATIC_IFN_KUNIT void dm_dmub_outbox1_low_irq(void *interrupt_params)
2256 {
2257 struct dmub_notification notify = {0};
2258 struct common_irq_params *irq_params = interrupt_params;
2259 struct amdgpu_device *adev = irq_params->adev;
2260 struct amdgpu_display_manager *dm = &adev->dm;
2261 struct dmcub_trace_buf_entry entry = { 0 };
2262 u32 count = 0;
2263 struct dmub_hpd_work *dmub_hpd_wrk;
2264
2265 do {
2266 if (dc_dmub_srv_get_dmub_outbox0_msg(dm->dc, &entry)) {
2267 trace_amdgpu_dmub_trace_high_irq(entry.trace_code, entry.tick_count,
2268 entry.param0, entry.param1);
2269
2270 drm_dbg_driver(adev_to_drm(adev), "trace_code:%u, tick_count:%u, param0:%u, param1:%u\n",
2271 entry.trace_code, entry.tick_count, entry.param0, entry.param1);
2272 } else
2273 break;
2274
2275 count++;
2276
2277 } while (count <= DMUB_TRACE_MAX_READ);
2278
2279 if (count > DMUB_TRACE_MAX_READ)
2280 drm_dbg_driver(adev_to_drm(adev), "Warning : count > DMUB_TRACE_MAX_READ");
2281
2282 if (dc_enable_dmub_notifications(adev->dm.dc) &&
2283 irq_params->irq_src == DC_IRQ_SOURCE_DMCUB_OUTBOX) {
2284
2285 do {
2286 dc_stat_get_dmub_notification(adev->dm.dc, &notify);
2287 if (notify.type >= ARRAY_SIZE(dm->dmub_thread_offload)) {
2288 drm_err(adev_to_drm(adev), "DM: notify type %d invalid!", notify.type);
2289 continue;
2290 }
2291 if (!dm->dmub_callback[notify.type]) {
2292 drm_warn(adev_to_drm(adev), "DMUB notification skipped due to no handler: type=%s\n",
2293 dmub_notification_type_str(notify.type));
2294 continue;
2295 }
2296 if (dm->dmub_thread_offload[notify.type]) {
2297 dmub_hpd_wrk = kzalloc_obj(*dmub_hpd_wrk,
2298 GFP_ATOMIC);
2299 if (!dmub_hpd_wrk) {
2300 drm_err(adev_to_drm(adev), "Failed to allocate dmub_hpd_wrk");
2301 return;
2302 }
2303 dmub_hpd_wrk->dmub_notify = kmemdup(&notify, sizeof(struct dmub_notification),
2304 GFP_ATOMIC);
2305 if (!dmub_hpd_wrk->dmub_notify) {
2306 kfree(dmub_hpd_wrk);
2307 drm_err(adev_to_drm(adev), "Failed to allocate dmub_hpd_wrk->dmub_notify");
2308 return;
2309 }
2310 INIT_WORK(&dmub_hpd_wrk->handle_hpd_work, dm_handle_hpd_work);
2311 dmub_hpd_wrk->adev = adev;
2312 queue_work(adev->dm.delayed_hpd_wq, &dmub_hpd_wrk->handle_hpd_work);
2313 } else {
2314 dm->dmub_callback[notify.type](adev, &notify);
2315 }
2316 } while (notify.pending_notification);
2317 }
2318 }
2319 EXPORT_IF_KUNIT(dm_dmub_outbox1_low_irq);
2320
2321 /* Register IRQ sources and initialize IRQ callbacks */
2322 int amdgpu_dm_dce110_register_irq_handlers(struct amdgpu_device *adev)
2323 {
2324 struct dc *dc = adev->dm.dc;
2325 struct common_irq_params *c_irq_params;
2326 struct dc_interrupt_params int_params = {0};
2327 int r;
2328 int i;
2329 unsigned int src_id;
2330 unsigned int client_id = AMDGPU_IRQ_CLIENTID_LEGACY;
2331 /* Use different interrupts for VBLANK on DCE 6 vs. newer. */
2332 const unsigned int vblank_d1 =
2333 adev->dm.dc->ctx->dce_version >= DCE_VERSION_8_0
2334 ? VISLANDS30_IV_SRCID_D1_VERTICAL_INTERRUPT0 : 1;
2335
2336 if (adev->family >= AMDGPU_FAMILY_AI)
2337 client_id = SOC15_IH_CLIENTID_DCE;
2338
2339 int_params.requested_polarity = INTERRUPT_POLARITY_DEFAULT;
2340 int_params.current_polarity = INTERRUPT_POLARITY_DEFAULT;
2341
2342 /*
2343 * Actions of amdgpu_irq_add_id():
2344 * 1. Register a set() function with base driver.
2345 * Base driver will call set() function to enable/disable an
2346 * interrupt in DC hardware.
2347 * 2. Register amdgpu_dm_irq_handler().
2348 * Base driver will call amdgpu_dm_irq_handler() for ALL interrupts
2349 * coming from DC hardware.
2350 * amdgpu_dm_irq_handler() will re-direct the interrupt to DC
2351 * for acknowledging and handling.
2352 */
2353
2354 /* Use VBLANK interrupt */
2355 for (i = 0; i < adev->mode_info.num_crtc; i++) {
2356 src_id = vblank_d1 + i;
2357 r = amdgpu_irq_add_id(adev, client_id, src_id, &adev->crtc_irq);
2358 if (r) {
2359 drm_err(adev_to_drm(adev), "Failed to add crtc irq id!\n");
2360 return r;
2361 }
2362
2363 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
2364 int_params.irq_source =
2365 dc_interrupt_to_irq_source(dc, src_id, 0);
2366
2367 if (int_params.irq_source == DC_IRQ_SOURCE_INVALID ||
2368 int_params.irq_source < DC_IRQ_SOURCE_VBLANK1 ||
2369 int_params.irq_source > DC_IRQ_SOURCE_VBLANK6) {
2370 drm_err(adev_to_drm(adev), "Failed to register vblank irq!\n");
2371 return -EINVAL;
2372 }
2373
2374 c_irq_params = &adev->dm.vblank_params[int_params.irq_source - DC_IRQ_SOURCE_VBLANK1];
2375
2376 c_irq_params->adev = adev;
2377 c_irq_params->irq_src = int_params.irq_source;
2378
2379 if (!amdgpu_dm_irq_register_interrupt(adev, &int_params,
2380 dm_crtc_high_irq, c_irq_params))
2381 return -ENOMEM;
2382 }
2383
2384 if (dc_supports_vrr(adev->dm.dc->ctx->dce_version)) {
2385 /* Use VUPDATE interrupt */
2386 for (i = 0; i < adev->mode_info.num_crtc; i++) {
2387 src_id = VISLANDS30_IV_SRCID_D1_V_UPDATE_INT + i * 2;
2388 r = amdgpu_irq_add_id(adev, client_id, src_id, &adev->vupdate_irq);
2389 if (r) {
2390 drm_err(adev_to_drm(adev), "Failed to add vupdate irq id!\n");
2391 return r;
2392 }
2393
2394 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
2395 int_params.irq_source =
2396 dc_interrupt_to_irq_source(dc, src_id, 0);
2397
2398 if (int_params.irq_source == DC_IRQ_SOURCE_INVALID ||
2399 int_params.irq_source < DC_IRQ_SOURCE_VUPDATE1 ||
2400 int_params.irq_source > DC_IRQ_SOURCE_VUPDATE6) {
2401 drm_err(adev_to_drm(adev), "Failed to register vupdate irq!\n");
2402 return -EINVAL;
2403 }
2404
2405 c_irq_params = &adev->dm.vupdate_params[
2406 int_params.irq_source - DC_IRQ_SOURCE_VUPDATE1];
2407 c_irq_params->adev = adev;
2408 c_irq_params->irq_src = int_params.irq_source;
2409
2410 if (!amdgpu_dm_irq_register_interrupt(adev, &int_params,
2411 dm_vupdate_high_irq, c_irq_params))
2412 return -ENOMEM;
2413 }
2414 }
2415
2416 /* Use GRPH_PFLIP interrupt */
2417 for (i = VISLANDS30_IV_SRCID_D1_GRPH_PFLIP;
2418 i <= VISLANDS30_IV_SRCID_D6_GRPH_PFLIP; i += 2) {
2419 r = amdgpu_irq_add_id(adev, client_id, i, &adev->pageflip_irq);
2420 if (r) {
2421 drm_err(adev_to_drm(adev), "Failed to add page flip irq id!\n");
2422 return r;
2423 }
2424
2425 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
2426 int_params.irq_source =
2427 dc_interrupt_to_irq_source(dc, i, 0);
2428
2429 if (int_params.irq_source == DC_IRQ_SOURCE_INVALID ||
2430 int_params.irq_source < DC_IRQ_SOURCE_PFLIP_FIRST ||
2431 int_params.irq_source > DC_IRQ_SOURCE_PFLIP_LAST) {
2432 drm_err(adev_to_drm(adev), "Failed to register pflip irq!\n");
2433 return -EINVAL;
2434 }
2435
2436 c_irq_params = &adev->dm.pflip_params[int_params.irq_source - DC_IRQ_SOURCE_PFLIP_FIRST];
2437
2438 c_irq_params->adev = adev;
2439 c_irq_params->irq_src = int_params.irq_source;
2440
2441 if (!amdgpu_dm_irq_register_interrupt(adev, &int_params,
2442 dm_pflip_high_irq, c_irq_params))
2443 return -ENOMEM;
2444 }
2445
2446 /* HPD */
2447 r = amdgpu_irq_add_id(adev, client_id,
2448 VISLANDS30_IV_SRCID_HOTPLUG_DETECT_A, &adev->hpd_irq);
2449 if (r) {
2450 drm_err(adev_to_drm(adev), "Failed to add hpd irq id!\n");
2451 return r;
2452 }
2453
2454 r = amdgpu_dm_register_hpd_handlers(adev);
2455
2456 return r;
2457 }
2458 EXPORT_IF_KUNIT(amdgpu_dm_dce110_register_irq_handlers);
2459
2460 /* Register IRQ sources and initialize IRQ callbacks */
2461 int amdgpu_dm_dcn10_register_irq_handlers(struct amdgpu_device *adev)
2462 {
2463 struct dc *dc = adev->dm.dc;
2464 struct common_irq_params *c_irq_params;
2465 struct dc_interrupt_params int_params = {0};
2466 int r;
2467 int i;
2468 #if defined(CONFIG_DRM_AMD_SECURE_DISPLAY)
2469 static const unsigned int vrtl_int_srcid[] = {
2470 DCN_1_0__SRCID__OTG1_VERTICAL_INTERRUPT0_CONTROL,
2471 DCN_1_0__SRCID__OTG2_VERTICAL_INTERRUPT0_CONTROL,
2472 DCN_1_0__SRCID__OTG3_VERTICAL_INTERRUPT0_CONTROL,
2473 DCN_1_0__SRCID__OTG4_VERTICAL_INTERRUPT0_CONTROL,
2474 DCN_1_0__SRCID__OTG5_VERTICAL_INTERRUPT0_CONTROL,
2475 DCN_1_0__SRCID__OTG6_VERTICAL_INTERRUPT0_CONTROL
2476 };
2477 #endif
2478
2479 int_params.requested_polarity = INTERRUPT_POLARITY_DEFAULT;
2480 int_params.current_polarity = INTERRUPT_POLARITY_DEFAULT;
2481
2482 /*
2483 * Actions of amdgpu_irq_add_id():
2484 * 1. Register a set() function with base driver.
2485 * Base driver will call set() function to enable/disable an
2486 * interrupt in DC hardware.
2487 * 2. Register amdgpu_dm_irq_handler().
2488 * Base driver will call amdgpu_dm_irq_handler() for ALL interrupts
2489 * coming from DC hardware.
2490 * amdgpu_dm_irq_handler() will re-direct the interrupt to DC
2491 * for acknowledging and handling.
2492 */
2493
2494 /* Use otg vertical line interrupt */
2495 #if defined(CONFIG_DRM_AMD_SECURE_DISPLAY)
2496 for (i = 0; i <= adev->mode_info.num_crtc - 1; i++) {
2497 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_DCE,
2498 vrtl_int_srcid[i], &adev->vline0_irq);
2499
2500 if (r) {
2501 drm_err(adev_to_drm(adev), "Failed to add vline0 irq id!\n");
2502 return r;
2503 }
2504
2505 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
2506 int_params.irq_source =
2507 dc_interrupt_to_irq_source(dc, vrtl_int_srcid[i], 0);
2508
2509 if (int_params.irq_source == DC_IRQ_SOURCE_INVALID ||
2510 int_params.irq_source < DC_IRQ_SOURCE_DC1_VLINE0 ||
2511 int_params.irq_source > DC_IRQ_SOURCE_DC6_VLINE0) {
2512 drm_err(adev_to_drm(adev), "Failed to register vline0 irq!\n");
2513 return -EINVAL;
2514 }
2515
2516 c_irq_params = &adev->dm.vline0_params[int_params.irq_source
2517 - DC_IRQ_SOURCE_DC1_VLINE0];
2518
2519 c_irq_params->adev = adev;
2520 c_irq_params->irq_src = int_params.irq_source;
2521
2522 if (!amdgpu_dm_irq_register_interrupt(adev, &int_params,
2523 dm_dcn_vertical_interrupt0_high_irq,
2524 c_irq_params))
2525 return -ENOMEM;
2526 }
2527 #endif
2528
2529 /* Use VUPDATE_NO_LOCK interrupt on DCN, which seems to correspond to
2530 * the regular VUPDATE interrupt on DCE. We want DC_IRQ_SOURCE_VUPDATEx
2531 * to trigger at end of each vblank, regardless of state of the lock,
2532 * matching DCE behaviour.
2533 */
2534 for (i = DCN_1_0__SRCID__OTG0_IHC_V_UPDATE_NO_LOCK_INTERRUPT;
2535 i <= DCN_1_0__SRCID__OTG0_IHC_V_UPDATE_NO_LOCK_INTERRUPT + adev->mode_info.num_crtc - 1;
2536 i++) {
2537 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_DCE, i, &adev->vupdate_irq);
2538
2539 if (r) {
2540 drm_err(adev_to_drm(adev), "Failed to add vupdate irq id!\n");
2541 return r;
2542 }
2543
2544 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
2545 int_params.irq_source =
2546 dc_interrupt_to_irq_source(dc, i, 0);
2547
2548 if (int_params.irq_source == DC_IRQ_SOURCE_INVALID ||
2549 int_params.irq_source < DC_IRQ_SOURCE_VUPDATE1 ||
2550 int_params.irq_source > DC_IRQ_SOURCE_VUPDATE6) {
2551 drm_err(adev_to_drm(adev), "Failed to register vupdate irq!\n");
2552 return -EINVAL;
2553 }
2554
2555 c_irq_params = &adev->dm.vupdate_params[int_params.irq_source - DC_IRQ_SOURCE_VUPDATE1];
2556
2557 c_irq_params->adev = adev;
2558 c_irq_params->irq_src = int_params.irq_source;
2559
2560 if (!amdgpu_dm_irq_register_interrupt(adev, &int_params,
2561 dm_vupdate_high_irq, c_irq_params))
2562 return -ENOMEM;
2563 }
2564
2565 /* HPD */
2566 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_DCE, DCN_1_0__SRCID__DC_HPD1_INT,
2567 &adev->hpd_irq);
2568 if (r) {
2569 drm_err(adev_to_drm(adev), "Failed to add hpd irq id!\n");
2570 return r;
2571 }
2572
2573 r = amdgpu_dm_register_hpd_handlers(adev);
2574
2575 return r;
2576 }
2577 EXPORT_IF_KUNIT(amdgpu_dm_dcn10_register_irq_handlers);
2578
2579 /* Register Outbox IRQ sources and initialize IRQ callbacks */
2580 int amdgpu_dm_register_outbox_irq_handlers(struct amdgpu_device *adev)
2581 {
2582 struct dc *dc = adev->dm.dc;
2583 struct common_irq_params *c_irq_params;
2584 struct dc_interrupt_params int_params = {0};
2585 int r, i;
2586
2587 int_params.requested_polarity = INTERRUPT_POLARITY_DEFAULT;
2588 int_params.current_polarity = INTERRUPT_POLARITY_DEFAULT;
2589
2590 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_DCE, DCN_1_0__SRCID__DMCUB_OUTBOX_LOW_PRIORITY_READY_INT,
2591 &adev->dmub_outbox_irq);
2592 if (r) {
2593 drm_err(adev_to_drm(adev), "Failed to add outbox irq id!\n");
2594 return r;
2595 }
2596
2597 if (dc->ctx->dmub_srv) {
2598 i = DCN_1_0__SRCID__DMCUB_OUTBOX_LOW_PRIORITY_READY_INT;
2599 int_params.int_context = INTERRUPT_LOW_IRQ_CONTEXT;
2600 int_params.irq_source =
2601 dc_interrupt_to_irq_source(dc, i, 0);
2602
2603 c_irq_params = &adev->dm.dmub_outbox_params[0];
2604
2605 c_irq_params->adev = adev;
2606 c_irq_params->irq_src = int_params.irq_source;
2607
2608 if (!amdgpu_dm_irq_register_interrupt(adev, &int_params,
2609 dm_dmub_outbox1_low_irq, c_irq_params))
2610 return -ENOMEM;
2611 }
2612
2613 return 0;
2614 }
2615 EXPORT_IF_KUNIT(amdgpu_dm_register_outbox_irq_handlers);