blob: 53ba424823b2abc02c082a49752db8b17a530fb7 [file] [log] [blame]
Rob Clarkf5f94542012-12-04 13:59:12 -06001/*
Andrew F. Davisbb5cdf82017-12-05 14:29:31 -06002 * Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/
Rob Clarkf5f94542012-12-04 13:59:12 -06003 * Author: Rob Clark <rob.clark@linaro.org>
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published by
7 * the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include "omap_drv.h"
19
Laurent Pinchart80f91bf2016-04-19 02:47:02 +030020struct omap_irq_wait {
21 struct list_head node;
Laurent Pinchart84e1d452016-04-19 03:07:59 +030022 wait_queue_head_t wq;
Laurent Pinchart80f91bf2016-04-19 02:47:02 +030023 uint32_t irqmask;
24 int count;
25};
26
Laurent Pinchart84e1d452016-04-19 03:07:59 +030027/* call with wait_lock and dispc runtime held */
Rob Clarkf5f94542012-12-04 13:59:12 -060028static void omap_irq_update(struct drm_device *dev)
29{
30 struct omap_drm_private *priv = dev->dev_private;
Laurent Pinchart80f91bf2016-04-19 02:47:02 +030031 struct omap_irq_wait *wait;
Laurent Pinchart728ae8d2015-05-28 00:21:29 +030032 uint32_t irqmask = priv->irq_mask;
Rob Clarkf5f94542012-12-04 13:59:12 -060033
Laurent Pinchart84e1d452016-04-19 03:07:59 +030034 assert_spin_locked(&priv->wait_lock);
Rob Clarkf5f94542012-12-04 13:59:12 -060035
Laurent Pinchart80f91bf2016-04-19 02:47:02 +030036 list_for_each_entry(wait, &priv->wait_list, node)
37 irqmask |= wait->irqmask;
Rob Clarkf5f94542012-12-04 13:59:12 -060038
39 DBG("irqmask=%08x", irqmask);
40
Tomi Valkeinen9f759222015-11-05 18:39:52 +020041 priv->dispc_ops->write_irqenable(irqmask);
Rob Clarkf5f94542012-12-04 13:59:12 -060042}
43
Laurent Pinchart80f91bf2016-04-19 02:47:02 +030044static void omap_irq_wait_handler(struct omap_irq_wait *wait)
Rob Clarkf5f94542012-12-04 13:59:12 -060045{
Rob Clarkf5f94542012-12-04 13:59:12 -060046 wait->count--;
Laurent Pinchart84e1d452016-04-19 03:07:59 +030047 wake_up(&wait->wq);
Rob Clarkf5f94542012-12-04 13:59:12 -060048}
49
50struct omap_irq_wait * omap_irq_wait_init(struct drm_device *dev,
51 uint32_t irqmask, int count)
52{
Laurent Pinchart80f91bf2016-04-19 02:47:02 +030053 struct omap_drm_private *priv = dev->dev_private;
Rob Clarkf5f94542012-12-04 13:59:12 -060054 struct omap_irq_wait *wait = kzalloc(sizeof(*wait), GFP_KERNEL);
Laurent Pinchart80f91bf2016-04-19 02:47:02 +030055 unsigned long flags;
56
Laurent Pinchart84e1d452016-04-19 03:07:59 +030057 init_waitqueue_head(&wait->wq);
Laurent Pinchart80f91bf2016-04-19 02:47:02 +030058 wait->irqmask = irqmask;
Rob Clarkf5f94542012-12-04 13:59:12 -060059 wait->count = count;
Laurent Pinchart80f91bf2016-04-19 02:47:02 +030060
Laurent Pinchart84e1d452016-04-19 03:07:59 +030061 spin_lock_irqsave(&priv->wait_lock, flags);
Laurent Pinchart80f91bf2016-04-19 02:47:02 +030062 list_add(&wait->node, &priv->wait_list);
63 omap_irq_update(dev);
Laurent Pinchart84e1d452016-04-19 03:07:59 +030064 spin_unlock_irqrestore(&priv->wait_lock, flags);
Laurent Pinchart80f91bf2016-04-19 02:47:02 +030065
Rob Clarkf5f94542012-12-04 13:59:12 -060066 return wait;
67}
68
69int omap_irq_wait(struct drm_device *dev, struct omap_irq_wait *wait,
70 unsigned long timeout)
71{
Laurent Pinchart84e1d452016-04-19 03:07:59 +030072 struct omap_drm_private *priv = dev->dev_private;
Laurent Pinchart80f91bf2016-04-19 02:47:02 +030073 unsigned long flags;
Laurent Pinchart84e1d452016-04-19 03:07:59 +030074 int ret;
Laurent Pinchart80f91bf2016-04-19 02:47:02 +030075
Laurent Pinchart84e1d452016-04-19 03:07:59 +030076 ret = wait_event_timeout(wait->wq, (wait->count <= 0), timeout);
77
78 spin_lock_irqsave(&priv->wait_lock, flags);
Laurent Pinchart80f91bf2016-04-19 02:47:02 +030079 list_del(&wait->node);
80 omap_irq_update(dev);
Laurent Pinchart84e1d452016-04-19 03:07:59 +030081 spin_unlock_irqrestore(&priv->wait_lock, flags);
Laurent Pinchart80f91bf2016-04-19 02:47:02 +030082
Rob Clarkf5f94542012-12-04 13:59:12 -060083 kfree(wait);
Laurent Pinchart80f91bf2016-04-19 02:47:02 +030084
85 return ret == 0 ? -1 : 0;
Rob Clarkf5f94542012-12-04 13:59:12 -060086}
87
88/**
89 * enable_vblank - enable vblank interrupt events
90 * @dev: DRM device
Thierry Reding88e72712015-09-24 18:35:31 +020091 * @pipe: which irq to enable
Rob Clarkf5f94542012-12-04 13:59:12 -060092 *
93 * Enable vblank interrupts for @crtc. If the device doesn't have
94 * a hardware vblank counter, this routine should be a no-op, since
95 * interrupts will have to stay on to keep the count accurate.
96 *
97 * RETURNS
98 * Zero on success, appropriate errno if the given @crtc's vblank
99 * interrupt cannot be enabled.
100 */
Tomi Valkeinen03961622017-02-08 13:26:00 +0200101int omap_irq_enable_vblank(struct drm_crtc *crtc)
Rob Clarkf5f94542012-12-04 13:59:12 -0600102{
Tomi Valkeinen03961622017-02-08 13:26:00 +0200103 struct drm_device *dev = crtc->dev;
Rob Clarkf5f94542012-12-04 13:59:12 -0600104 struct omap_drm_private *priv = dev->dev_private;
105 unsigned long flags;
Tomi Valkeinen03961622017-02-08 13:26:00 +0200106 enum omap_channel channel = omap_crtc_channel(crtc);
Rob Clarkf5f94542012-12-04 13:59:12 -0600107
Tomi Valkeinen03961622017-02-08 13:26:00 +0200108 DBG("dev=%p, crtc=%u", dev, channel);
Rob Clarkf5f94542012-12-04 13:59:12 -0600109
Laurent Pinchart84e1d452016-04-19 03:07:59 +0300110 spin_lock_irqsave(&priv->wait_lock, flags);
Tomi Valkeinen9f759222015-11-05 18:39:52 +0200111 priv->irq_mask |= priv->dispc_ops->mgr_get_vsync_irq(channel);
Rob Clarkf5f94542012-12-04 13:59:12 -0600112 omap_irq_update(dev);
Laurent Pinchart84e1d452016-04-19 03:07:59 +0300113 spin_unlock_irqrestore(&priv->wait_lock, flags);
Rob Clarkf5f94542012-12-04 13:59:12 -0600114
115 return 0;
116}
117
118/**
119 * disable_vblank - disable vblank interrupt events
120 * @dev: DRM device
Thierry Reding88e72712015-09-24 18:35:31 +0200121 * @pipe: which irq to enable
Rob Clarkf5f94542012-12-04 13:59:12 -0600122 *
123 * Disable vblank interrupts for @crtc. If the device doesn't have
124 * a hardware vblank counter, this routine should be a no-op, since
125 * interrupts will have to stay on to keep the count accurate.
126 */
Tomi Valkeinen03961622017-02-08 13:26:00 +0200127void omap_irq_disable_vblank(struct drm_crtc *crtc)
Rob Clarkf5f94542012-12-04 13:59:12 -0600128{
Tomi Valkeinen03961622017-02-08 13:26:00 +0200129 struct drm_device *dev = crtc->dev;
Rob Clarkf5f94542012-12-04 13:59:12 -0600130 struct omap_drm_private *priv = dev->dev_private;
131 unsigned long flags;
Tomi Valkeinen03961622017-02-08 13:26:00 +0200132 enum omap_channel channel = omap_crtc_channel(crtc);
Rob Clarkf5f94542012-12-04 13:59:12 -0600133
Tomi Valkeinen03961622017-02-08 13:26:00 +0200134 DBG("dev=%p, crtc=%u", dev, channel);
Rob Clarkf5f94542012-12-04 13:59:12 -0600135
Laurent Pinchart84e1d452016-04-19 03:07:59 +0300136 spin_lock_irqsave(&priv->wait_lock, flags);
Tomi Valkeinen9f759222015-11-05 18:39:52 +0200137 priv->irq_mask &= ~priv->dispc_ops->mgr_get_vsync_irq(channel);
Rob Clarkf5f94542012-12-04 13:59:12 -0600138 omap_irq_update(dev);
Laurent Pinchart84e1d452016-04-19 03:07:59 +0300139 spin_unlock_irqrestore(&priv->wait_lock, flags);
Rob Clarkf5f94542012-12-04 13:59:12 -0600140}
141
Laurent Pinchart728ae8d2015-05-28 00:21:29 +0300142static void omap_irq_fifo_underflow(struct omap_drm_private *priv,
143 u32 irqstatus)
144{
145 static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
146 DEFAULT_RATELIMIT_BURST);
147 static const struct {
148 const char *name;
149 u32 mask;
150 } sources[] = {
151 { "gfx", DISPC_IRQ_GFX_FIFO_UNDERFLOW },
152 { "vid1", DISPC_IRQ_VID1_FIFO_UNDERFLOW },
153 { "vid2", DISPC_IRQ_VID2_FIFO_UNDERFLOW },
154 { "vid3", DISPC_IRQ_VID3_FIFO_UNDERFLOW },
155 };
156
157 const u32 mask = DISPC_IRQ_GFX_FIFO_UNDERFLOW
158 | DISPC_IRQ_VID1_FIFO_UNDERFLOW
159 | DISPC_IRQ_VID2_FIFO_UNDERFLOW
160 | DISPC_IRQ_VID3_FIFO_UNDERFLOW;
161 unsigned int i;
162
Laurent Pinchart84e1d452016-04-19 03:07:59 +0300163 spin_lock(&priv->wait_lock);
Laurent Pinchart728ae8d2015-05-28 00:21:29 +0300164 irqstatus &= priv->irq_mask & mask;
Laurent Pinchart84e1d452016-04-19 03:07:59 +0300165 spin_unlock(&priv->wait_lock);
Laurent Pinchart728ae8d2015-05-28 00:21:29 +0300166
167 if (!irqstatus)
168 return;
169
170 if (!__ratelimit(&_rs))
171 return;
172
173 DRM_ERROR("FIFO underflow on ");
174
175 for (i = 0; i < ARRAY_SIZE(sources); ++i) {
176 if (sources[i].mask & irqstatus)
177 pr_cont("%s ", sources[i].name);
178 }
179
180 pr_cont("(0x%08x)\n", irqstatus);
181}
182
Tomi Valkeinendc50be82017-03-03 12:15:39 +0200183static void omap_irq_ocp_error_handler(struct drm_device *dev,
184 u32 irqstatus)
Laurent Pinchart6b5538d2015-05-28 01:05:20 +0300185{
186 if (!(irqstatus & DISPC_IRQ_OCP_ERR))
187 return;
188
Tomi Valkeinendc50be82017-03-03 12:15:39 +0200189 dev_err_ratelimited(dev->dev, "OCP error\n");
Laurent Pinchart6b5538d2015-05-28 01:05:20 +0300190}
191
Laurent Pinchartf13ab002015-01-25 22:06:45 +0200192static irqreturn_t omap_irq_handler(int irq, void *arg)
Rob Clarkf5f94542012-12-04 13:59:12 -0600193{
194 struct drm_device *dev = (struct drm_device *) arg;
195 struct omap_drm_private *priv = dev->dev_private;
Laurent Pinchart80f91bf2016-04-19 02:47:02 +0300196 struct omap_irq_wait *wait, *n;
Rob Clarkf5f94542012-12-04 13:59:12 -0600197 unsigned long flags;
198 unsigned int id;
199 u32 irqstatus;
200
Tomi Valkeinen9f759222015-11-05 18:39:52 +0200201 irqstatus = priv->dispc_ops->read_irqstatus();
202 priv->dispc_ops->clear_irqstatus(irqstatus);
203 priv->dispc_ops->read_irqstatus(); /* flush posted write */
Rob Clarkf5f94542012-12-04 13:59:12 -0600204
205 VERB("irqs: %08x", irqstatus);
206
Archit Taneja0d8f3712013-03-26 19:15:19 +0530207 for (id = 0; id < priv->num_crtcs; id++) {
208 struct drm_crtc *crtc = priv->crtcs[id];
Laurent Pincharte0519af2015-05-28 00:21:29 +0300209 enum omap_channel channel = omap_crtc_channel(crtc);
Archit Taneja0d8f3712013-03-26 19:15:19 +0530210
Tomi Valkeinen9f759222015-11-05 18:39:52 +0200211 if (irqstatus & priv->dispc_ops->mgr_get_vsync_irq(channel)) {
Rob Clarkf5f94542012-12-04 13:59:12 -0600212 drm_handle_vblank(dev, id);
Laurent Pinchart14389a32016-04-19 01:43:03 +0300213 omap_crtc_vblank_irq(crtc);
214 }
Laurent Pincharte0519af2015-05-28 00:21:29 +0300215
Tomi Valkeinen9f759222015-11-05 18:39:52 +0200216 if (irqstatus & priv->dispc_ops->mgr_get_sync_lost_irq(channel))
Laurent Pincharte0519af2015-05-28 00:21:29 +0300217 omap_crtc_error_irq(crtc, irqstatus);
Archit Taneja0d8f3712013-03-26 19:15:19 +0530218 }
Rob Clarkf5f94542012-12-04 13:59:12 -0600219
Tomi Valkeinendc50be82017-03-03 12:15:39 +0200220 omap_irq_ocp_error_handler(dev, irqstatus);
Laurent Pinchart728ae8d2015-05-28 00:21:29 +0300221 omap_irq_fifo_underflow(priv, irqstatus);
222
Laurent Pinchart84e1d452016-04-19 03:07:59 +0300223 spin_lock_irqsave(&priv->wait_lock, flags);
Laurent Pinchart80f91bf2016-04-19 02:47:02 +0300224 list_for_each_entry_safe(wait, n, &priv->wait_list, node) {
225 if (wait->irqmask & irqstatus)
226 omap_irq_wait_handler(wait);
Rob Clarkf5f94542012-12-04 13:59:12 -0600227 }
Laurent Pinchart84e1d452016-04-19 03:07:59 +0300228 spin_unlock_irqrestore(&priv->wait_lock, flags);
Rob Clarkf5f94542012-12-04 13:59:12 -0600229
230 return IRQ_HANDLED;
231}
232
Laurent Pinchart728ae8d2015-05-28 00:21:29 +0300233static const u32 omap_underflow_irqs[] = {
234 [OMAP_DSS_GFX] = DISPC_IRQ_GFX_FIFO_UNDERFLOW,
235 [OMAP_DSS_VIDEO1] = DISPC_IRQ_VID1_FIFO_UNDERFLOW,
236 [OMAP_DSS_VIDEO2] = DISPC_IRQ_VID2_FIFO_UNDERFLOW,
237 [OMAP_DSS_VIDEO3] = DISPC_IRQ_VID3_FIFO_UNDERFLOW,
238};
239
Laurent Pinchartf13ab002015-01-25 22:06:45 +0200240/*
241 * We need a special version, instead of just using drm_irq_install(),
242 * because we need to register the irq via omapdss. Once omapdss and
243 * omapdrm are merged together we can assign the dispc hwmod data to
244 * ourselves and drop these and just use drm_irq_{install,uninstall}()
245 */
Rob Clarkf5f94542012-12-04 13:59:12 -0600246
Laurent Pinchartf13ab002015-01-25 22:06:45 +0200247int omap_drm_irq_install(struct drm_device *dev)
Rob Clarkf5f94542012-12-04 13:59:12 -0600248{
249 struct omap_drm_private *priv = dev->dev_private;
Tomi Valkeinen9f759222015-11-05 18:39:52 +0200250 unsigned int num_mgrs = priv->dispc_ops->get_num_mgrs();
Laurent Pinchart728ae8d2015-05-28 00:21:29 +0300251 unsigned int max_planes;
252 unsigned int i;
Laurent Pinchartf13ab002015-01-25 22:06:45 +0200253 int ret;
Rob Clarkf5f94542012-12-04 13:59:12 -0600254
Laurent Pinchart84e1d452016-04-19 03:07:59 +0300255 spin_lock_init(&priv->wait_lock);
Laurent Pinchart80f91bf2016-04-19 02:47:02 +0300256 INIT_LIST_HEAD(&priv->wait_list);
Rob Clarkf5f94542012-12-04 13:59:12 -0600257
Laurent Pinchart6b5538d2015-05-28 01:05:20 +0300258 priv->irq_mask = DISPC_IRQ_OCP_ERR;
Laurent Pinchart728ae8d2015-05-28 00:21:29 +0300259
260 max_planes = min(ARRAY_SIZE(priv->planes),
261 ARRAY_SIZE(omap_underflow_irqs));
262 for (i = 0; i < max_planes; ++i) {
263 if (priv->planes[i])
264 priv->irq_mask |= omap_underflow_irqs[i];
265 }
266
Laurent Pincharte0519af2015-05-28 00:21:29 +0300267 for (i = 0; i < num_mgrs; ++i)
Tomi Valkeinen9f759222015-11-05 18:39:52 +0200268 priv->irq_mask |= priv->dispc_ops->mgr_get_sync_lost_irq(i);
Laurent Pincharte0519af2015-05-28 00:21:29 +0300269
Tomi Valkeinen9f759222015-11-05 18:39:52 +0200270 priv->dispc_ops->runtime_get();
271 priv->dispc_ops->clear_irqstatus(0xffffffff);
272 priv->dispc_ops->runtime_put();
Laurent Pinchartf13ab002015-01-25 22:06:45 +0200273
Tomi Valkeinen9f759222015-11-05 18:39:52 +0200274 ret = priv->dispc_ops->request_irq(omap_irq_handler, dev);
Laurent Pinchartf13ab002015-01-25 22:06:45 +0200275 if (ret < 0)
276 return ret;
277
Laurent Pinchartf13ab002015-01-25 22:06:45 +0200278 dev->irq_enabled = true;
279
Rob Clarkf5f94542012-12-04 13:59:12 -0600280 return 0;
281}
282
Laurent Pinchartf13ab002015-01-25 22:06:45 +0200283void omap_drm_irq_uninstall(struct drm_device *dev)
Rob Clarkf5f94542012-12-04 13:59:12 -0600284{
Tomi Valkeinen9f759222015-11-05 18:39:52 +0200285 struct omap_drm_private *priv = dev->dev_private;
Rob Clarkf5f94542012-12-04 13:59:12 -0600286
Laurent Pinchartf13ab002015-01-25 22:06:45 +0200287 if (!dev->irq_enabled)
288 return;
Rob Clarkf5f94542012-12-04 13:59:12 -0600289
Laurent Pinchartf13ab002015-01-25 22:06:45 +0200290 dev->irq_enabled = false;
291
Tomi Valkeinen9f759222015-11-05 18:39:52 +0200292 priv->dispc_ops->free_irq(dev);
Rob Clarkf5f94542012-12-04 13:59:12 -0600293}