1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2013 Red Hat
4  * Author: Rob Clark <robdclark@gmail.com>
5  */
6 
7 #include <linux/irq.h>
8 
9 #include <drm/drm_print.h>
10 #include <drm/drm_vblank.h>
11 
12 #include "msm_drv.h"
13 #include "mdp5_kms.h"
14 
mdp5_set_irqmask(struct mdp_kms * mdp_kms,uint32_t irqmask,uint32_t old_irqmask)15 void mdp5_set_irqmask(struct mdp_kms *mdp_kms, uint32_t irqmask,
16 		uint32_t old_irqmask)
17 {
18 	mdp5_write(to_mdp5_kms(mdp_kms), REG_MDP5_INTR_CLEAR,
19 		   irqmask ^ (irqmask & old_irqmask));
20 	mdp5_write(to_mdp5_kms(mdp_kms), REG_MDP5_INTR_EN, irqmask);
21 }
22 
mdp5_irq_error_handler(struct mdp_irq * irq,uint32_t irqstatus)23 static void mdp5_irq_error_handler(struct mdp_irq *irq, uint32_t irqstatus)
24 {
25 	struct mdp5_kms *mdp5_kms = container_of(irq, struct mdp5_kms, error_handler);
26 	static DEFINE_RATELIMIT_STATE(rs, 5*HZ, 1);
27 	extern bool dumpstate;
28 
29 	DRM_ERROR_RATELIMITED("errors: %08x\n", irqstatus);
30 
31 	if (dumpstate && __ratelimit(&rs)) {
32 		struct drm_printer p = drm_info_printer(mdp5_kms->dev->dev);
33 		drm_state_dump(mdp5_kms->dev, &p);
34 		if (mdp5_kms->smp)
35 			mdp5_smp_dump(mdp5_kms->smp, &p);
36 	}
37 }
38 
mdp5_irq_preinstall(struct msm_kms * kms)39 void mdp5_irq_preinstall(struct msm_kms *kms)
40 {
41 	struct mdp5_kms *mdp5_kms = to_mdp5_kms(to_mdp_kms(kms));
42 	struct device *dev = &mdp5_kms->pdev->dev;
43 
44 	pm_runtime_get_sync(dev);
45 	mdp5_write(mdp5_kms, REG_MDP5_INTR_CLEAR, 0xffffffff);
46 	mdp5_write(mdp5_kms, REG_MDP5_INTR_EN, 0x00000000);
47 	pm_runtime_put_sync(dev);
48 }
49 
mdp5_irq_postinstall(struct msm_kms * kms)50 int mdp5_irq_postinstall(struct msm_kms *kms)
51 {
52 	struct mdp_kms *mdp_kms = to_mdp_kms(kms);
53 	struct mdp5_kms *mdp5_kms = to_mdp5_kms(mdp_kms);
54 	struct device *dev = &mdp5_kms->pdev->dev;
55 	struct mdp_irq *error_handler = &mdp5_kms->error_handler;
56 
57 	error_handler->irq = mdp5_irq_error_handler;
58 	error_handler->irqmask = MDP5_IRQ_INTF0_UNDER_RUN |
59 			MDP5_IRQ_INTF1_UNDER_RUN |
60 			MDP5_IRQ_INTF2_UNDER_RUN |
61 			MDP5_IRQ_INTF3_UNDER_RUN;
62 
63 	pm_runtime_get_sync(dev);
64 	mdp_irq_register(mdp_kms, error_handler);
65 	pm_runtime_put_sync(dev);
66 
67 	return 0;
68 }
69 
mdp5_irq_uninstall(struct msm_kms * kms)70 void mdp5_irq_uninstall(struct msm_kms *kms)
71 {
72 	struct mdp5_kms *mdp5_kms = to_mdp5_kms(to_mdp_kms(kms));
73 	struct device *dev = &mdp5_kms->pdev->dev;
74 
75 	pm_runtime_get_sync(dev);
76 	mdp5_write(mdp5_kms, REG_MDP5_INTR_EN, 0x00000000);
77 	pm_runtime_put_sync(dev);
78 }
79 
mdp5_irq(struct msm_kms * kms)80 irqreturn_t mdp5_irq(struct msm_kms *kms)
81 {
82 	struct mdp_kms *mdp_kms = to_mdp_kms(kms);
83 	struct mdp5_kms *mdp5_kms = to_mdp5_kms(mdp_kms);
84 	struct drm_device *dev = mdp5_kms->dev;
85 	struct msm_drm_private *priv = dev->dev_private;
86 	unsigned int id;
87 	uint32_t status, enable;
88 
89 	enable = mdp5_read(mdp5_kms, REG_MDP5_INTR_EN);
90 	status = mdp5_read(mdp5_kms, REG_MDP5_INTR_STATUS) & enable;
91 	mdp5_write(mdp5_kms, REG_MDP5_INTR_CLEAR, status);
92 
93 	VERB("status=%08x", status);
94 
95 	mdp_dispatch_irqs(mdp_kms, status);
96 
97 	for (id = 0; id < priv->num_crtcs; id++)
98 		if (status & mdp5_crtc_vblank(priv->crtcs[id]))
99 			drm_handle_vblank(dev, id);
100 
101 	return IRQ_HANDLED;
102 }
103 
mdp5_enable_vblank(struct msm_kms * kms,struct drm_crtc * crtc)104 int mdp5_enable_vblank(struct msm_kms *kms, struct drm_crtc *crtc)
105 {
106 	struct mdp5_kms *mdp5_kms = to_mdp5_kms(to_mdp_kms(kms));
107 	struct device *dev = &mdp5_kms->pdev->dev;
108 
109 	pm_runtime_get_sync(dev);
110 	mdp_update_vblank_mask(to_mdp_kms(kms),
111 			mdp5_crtc_vblank(crtc), true);
112 	pm_runtime_put_sync(dev);
113 
114 	return 0;
115 }
116 
mdp5_disable_vblank(struct msm_kms * kms,struct drm_crtc * crtc)117 void mdp5_disable_vblank(struct msm_kms *kms, struct drm_crtc *crtc)
118 {
119 	struct mdp5_kms *mdp5_kms = to_mdp5_kms(to_mdp_kms(kms));
120 	struct device *dev = &mdp5_kms->pdev->dev;
121 
122 	pm_runtime_get_sync(dev);
123 	mdp_update_vblank_mask(to_mdp_kms(kms),
124 			mdp5_crtc_vblank(crtc), false);
125 	pm_runtime_put_sync(dev);
126 }
127