blob: 7afe4b90befdec5c12fa965c7485af795d264d0f [file] [log] [blame]
Rob Clarkf5f94542012-12-04 13:59:12 -06001/*
Rob Clark8bb0daf2013-02-11 12:43:09 -05002 * drivers/gpu/drm/omapdrm/omap_irq.c
Rob Clarkf5f94542012-12-04 13:59:12 -06003 *
4 * Copyright (C) 2012 Texas Instruments
5 * Author: Rob Clark <rob.clark@linaro.org>
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License version 2 as published by
9 * the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include "omap_drv.h"
21
Laurent Pinchart80f91bf2016-04-19 02:47:02 +030022struct omap_irq_wait {
23 struct list_head node;
Laurent Pinchart84e1d452016-04-19 03:07:59 +030024 wait_queue_head_t wq;
Laurent Pinchart80f91bf2016-04-19 02:47:02 +030025 uint32_t irqmask;
26 int count;
27};
28
Laurent Pinchart84e1d452016-04-19 03:07:59 +030029/* call with wait_lock and dispc runtime held */
Rob Clarkf5f94542012-12-04 13:59:12 -060030static void omap_irq_update(struct drm_device *dev)
31{
32 struct omap_drm_private *priv = dev->dev_private;
Laurent Pinchart80f91bf2016-04-19 02:47:02 +030033 struct omap_irq_wait *wait;
Laurent Pinchart728ae8d2015-05-28 00:21:29 +030034 uint32_t irqmask = priv->irq_mask;
Rob Clarkf5f94542012-12-04 13:59:12 -060035
Laurent Pinchart84e1d452016-04-19 03:07:59 +030036 assert_spin_locked(&priv->wait_lock);
Rob Clarkf5f94542012-12-04 13:59:12 -060037
Laurent Pinchart80f91bf2016-04-19 02:47:02 +030038 list_for_each_entry(wait, &priv->wait_list, node)
39 irqmask |= wait->irqmask;
Rob Clarkf5f94542012-12-04 13:59:12 -060040
41 DBG("irqmask=%08x", irqmask);
42
Tomi Valkeinen9f759222015-11-05 18:39:52 +020043 priv->dispc_ops->write_irqenable(irqmask);
Rob Clarkf5f94542012-12-04 13:59:12 -060044}
45
Laurent Pinchart80f91bf2016-04-19 02:47:02 +030046static void omap_irq_wait_handler(struct omap_irq_wait *wait)
Rob Clarkf5f94542012-12-04 13:59:12 -060047{
Rob Clarkf5f94542012-12-04 13:59:12 -060048 wait->count--;
Laurent Pinchart84e1d452016-04-19 03:07:59 +030049 wake_up(&wait->wq);
Rob Clarkf5f94542012-12-04 13:59:12 -060050}
51
52struct omap_irq_wait * omap_irq_wait_init(struct drm_device *dev,
53 uint32_t irqmask, int count)
54{
Laurent Pinchart80f91bf2016-04-19 02:47:02 +030055 struct omap_drm_private *priv = dev->dev_private;
Rob Clarkf5f94542012-12-04 13:59:12 -060056 struct omap_irq_wait *wait = kzalloc(sizeof(*wait), GFP_KERNEL);
Laurent Pinchart80f91bf2016-04-19 02:47:02 +030057 unsigned long flags;
58
Laurent Pinchart84e1d452016-04-19 03:07:59 +030059 init_waitqueue_head(&wait->wq);
Laurent Pinchart80f91bf2016-04-19 02:47:02 +030060 wait->irqmask = irqmask;
Rob Clarkf5f94542012-12-04 13:59:12 -060061 wait->count = count;
Laurent Pinchart80f91bf2016-04-19 02:47:02 +030062
Laurent Pinchart84e1d452016-04-19 03:07:59 +030063 spin_lock_irqsave(&priv->wait_lock, flags);
Laurent Pinchart80f91bf2016-04-19 02:47:02 +030064 list_add(&wait->node, &priv->wait_list);
65 omap_irq_update(dev);
Laurent Pinchart84e1d452016-04-19 03:07:59 +030066 spin_unlock_irqrestore(&priv->wait_lock, flags);
Laurent Pinchart80f91bf2016-04-19 02:47:02 +030067
Rob Clarkf5f94542012-12-04 13:59:12 -060068 return wait;
69}
70
71int omap_irq_wait(struct drm_device *dev, struct omap_irq_wait *wait,
72 unsigned long timeout)
73{
Laurent Pinchart84e1d452016-04-19 03:07:59 +030074 struct omap_drm_private *priv = dev->dev_private;
Laurent Pinchart80f91bf2016-04-19 02:47:02 +030075 unsigned long flags;
Laurent Pinchart84e1d452016-04-19 03:07:59 +030076 int ret;
Laurent Pinchart80f91bf2016-04-19 02:47:02 +030077
Laurent Pinchart84e1d452016-04-19 03:07:59 +030078 ret = wait_event_timeout(wait->wq, (wait->count <= 0), timeout);
79
80 spin_lock_irqsave(&priv->wait_lock, flags);
Laurent Pinchart80f91bf2016-04-19 02:47:02 +030081 list_del(&wait->node);
82 omap_irq_update(dev);
Laurent Pinchart84e1d452016-04-19 03:07:59 +030083 spin_unlock_irqrestore(&priv->wait_lock, flags);
Laurent Pinchart80f91bf2016-04-19 02:47:02 +030084
Rob Clarkf5f94542012-12-04 13:59:12 -060085 kfree(wait);
Laurent Pinchart80f91bf2016-04-19 02:47:02 +030086
87 return ret == 0 ? -1 : 0;
Rob Clarkf5f94542012-12-04 13:59:12 -060088}
89
90/**
91 * enable_vblank - enable vblank interrupt events
92 * @dev: DRM device
Thierry Reding88e72712015-09-24 18:35:31 +020093 * @pipe: which irq to enable
Rob Clarkf5f94542012-12-04 13:59:12 -060094 *
95 * Enable vblank interrupts for @crtc. If the device doesn't have
96 * a hardware vblank counter, this routine should be a no-op, since
97 * interrupts will have to stay on to keep the count accurate.
98 *
99 * RETURNS
100 * Zero on success, appropriate errno if the given @crtc's vblank
101 * interrupt cannot be enabled.
102 */
Tomi Valkeinen03961622017-02-08 13:26:00 +0200103int omap_irq_enable_vblank(struct drm_crtc *crtc)
Rob Clarkf5f94542012-12-04 13:59:12 -0600104{
Tomi Valkeinen03961622017-02-08 13:26:00 +0200105 struct drm_device *dev = crtc->dev;
Rob Clarkf5f94542012-12-04 13:59:12 -0600106 struct omap_drm_private *priv = dev->dev_private;
107 unsigned long flags;
Tomi Valkeinen03961622017-02-08 13:26:00 +0200108 enum omap_channel channel = omap_crtc_channel(crtc);
Rob Clarkf5f94542012-12-04 13:59:12 -0600109
Tomi Valkeinen03961622017-02-08 13:26:00 +0200110 DBG("dev=%p, crtc=%u", dev, channel);
Rob Clarkf5f94542012-12-04 13:59:12 -0600111
Laurent Pinchart84e1d452016-04-19 03:07:59 +0300112 spin_lock_irqsave(&priv->wait_lock, flags);
Tomi Valkeinen9f759222015-11-05 18:39:52 +0200113 priv->irq_mask |= priv->dispc_ops->mgr_get_vsync_irq(channel);
Rob Clarkf5f94542012-12-04 13:59:12 -0600114 omap_irq_update(dev);
Laurent Pinchart84e1d452016-04-19 03:07:59 +0300115 spin_unlock_irqrestore(&priv->wait_lock, flags);
Rob Clarkf5f94542012-12-04 13:59:12 -0600116
117 return 0;
118}
119
120/**
121 * disable_vblank - disable vblank interrupt events
122 * @dev: DRM device
Thierry Reding88e72712015-09-24 18:35:31 +0200123 * @pipe: which irq to enable
Rob Clarkf5f94542012-12-04 13:59:12 -0600124 *
125 * Disable vblank interrupts for @crtc. If the device doesn't have
126 * a hardware vblank counter, this routine should be a no-op, since
127 * interrupts will have to stay on to keep the count accurate.
128 */
Tomi Valkeinen03961622017-02-08 13:26:00 +0200129void omap_irq_disable_vblank(struct drm_crtc *crtc)
Rob Clarkf5f94542012-12-04 13:59:12 -0600130{
Tomi Valkeinen03961622017-02-08 13:26:00 +0200131 struct drm_device *dev = crtc->dev;
Rob Clarkf5f94542012-12-04 13:59:12 -0600132 struct omap_drm_private *priv = dev->dev_private;
133 unsigned long flags;
Tomi Valkeinen03961622017-02-08 13:26:00 +0200134 enum omap_channel channel = omap_crtc_channel(crtc);
Rob Clarkf5f94542012-12-04 13:59:12 -0600135
Tomi Valkeinen03961622017-02-08 13:26:00 +0200136 DBG("dev=%p, crtc=%u", dev, channel);
Rob Clarkf5f94542012-12-04 13:59:12 -0600137
Laurent Pinchart84e1d452016-04-19 03:07:59 +0300138 spin_lock_irqsave(&priv->wait_lock, flags);
Tomi Valkeinen9f759222015-11-05 18:39:52 +0200139 priv->irq_mask &= ~priv->dispc_ops->mgr_get_vsync_irq(channel);
Rob Clarkf5f94542012-12-04 13:59:12 -0600140 omap_irq_update(dev);
Laurent Pinchart84e1d452016-04-19 03:07:59 +0300141 spin_unlock_irqrestore(&priv->wait_lock, flags);
Rob Clarkf5f94542012-12-04 13:59:12 -0600142}
143
Laurent Pinchart728ae8d2015-05-28 00:21:29 +0300144static void omap_irq_fifo_underflow(struct omap_drm_private *priv,
145 u32 irqstatus)
146{
147 static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
148 DEFAULT_RATELIMIT_BURST);
149 static const struct {
150 const char *name;
151 u32 mask;
152 } sources[] = {
153 { "gfx", DISPC_IRQ_GFX_FIFO_UNDERFLOW },
154 { "vid1", DISPC_IRQ_VID1_FIFO_UNDERFLOW },
155 { "vid2", DISPC_IRQ_VID2_FIFO_UNDERFLOW },
156 { "vid3", DISPC_IRQ_VID3_FIFO_UNDERFLOW },
157 };
158
159 const u32 mask = DISPC_IRQ_GFX_FIFO_UNDERFLOW
160 | DISPC_IRQ_VID1_FIFO_UNDERFLOW
161 | DISPC_IRQ_VID2_FIFO_UNDERFLOW
162 | DISPC_IRQ_VID3_FIFO_UNDERFLOW;
163 unsigned int i;
164
Laurent Pinchart84e1d452016-04-19 03:07:59 +0300165 spin_lock(&priv->wait_lock);
Laurent Pinchart728ae8d2015-05-28 00:21:29 +0300166 irqstatus &= priv->irq_mask & mask;
Laurent Pinchart84e1d452016-04-19 03:07:59 +0300167 spin_unlock(&priv->wait_lock);
Laurent Pinchart728ae8d2015-05-28 00:21:29 +0300168
169 if (!irqstatus)
170 return;
171
172 if (!__ratelimit(&_rs))
173 return;
174
175 DRM_ERROR("FIFO underflow on ");
176
177 for (i = 0; i < ARRAY_SIZE(sources); ++i) {
178 if (sources[i].mask & irqstatus)
179 pr_cont("%s ", sources[i].name);
180 }
181
182 pr_cont("(0x%08x)\n", irqstatus);
183}
184
Laurent Pinchart6b5538d2015-05-28 01:05:20 +0300185static void omap_irq_ocp_error_handler(u32 irqstatus)
186{
187 if (!(irqstatus & DISPC_IRQ_OCP_ERR))
188 return;
189
190 DRM_ERROR("OCP error\n");
191}
192
Laurent Pinchartf13ab002015-01-25 22:06:45 +0200193static irqreturn_t omap_irq_handler(int irq, void *arg)
Rob Clarkf5f94542012-12-04 13:59:12 -0600194{
195 struct drm_device *dev = (struct drm_device *) arg;
196 struct omap_drm_private *priv = dev->dev_private;
Laurent Pinchart80f91bf2016-04-19 02:47:02 +0300197 struct omap_irq_wait *wait, *n;
Rob Clarkf5f94542012-12-04 13:59:12 -0600198 unsigned long flags;
199 unsigned int id;
200 u32 irqstatus;
201
Tomi Valkeinen9f759222015-11-05 18:39:52 +0200202 irqstatus = priv->dispc_ops->read_irqstatus();
203 priv->dispc_ops->clear_irqstatus(irqstatus);
204 priv->dispc_ops->read_irqstatus(); /* flush posted write */
Rob Clarkf5f94542012-12-04 13:59:12 -0600205
206 VERB("irqs: %08x", irqstatus);
207
Archit Taneja0d8f3712013-03-26 19:15:19 +0530208 for (id = 0; id < priv->num_crtcs; id++) {
209 struct drm_crtc *crtc = priv->crtcs[id];
Laurent Pincharte0519af2015-05-28 00:21:29 +0300210 enum omap_channel channel = omap_crtc_channel(crtc);
Archit Taneja0d8f3712013-03-26 19:15:19 +0530211
Tomi Valkeinen9f759222015-11-05 18:39:52 +0200212 if (irqstatus & priv->dispc_ops->mgr_get_vsync_irq(channel)) {
Rob Clarkf5f94542012-12-04 13:59:12 -0600213 drm_handle_vblank(dev, id);
Laurent Pinchart14389a32016-04-19 01:43:03 +0300214 omap_crtc_vblank_irq(crtc);
215 }
Laurent Pincharte0519af2015-05-28 00:21:29 +0300216
Tomi Valkeinen9f759222015-11-05 18:39:52 +0200217 if (irqstatus & priv->dispc_ops->mgr_get_sync_lost_irq(channel))
Laurent Pincharte0519af2015-05-28 00:21:29 +0300218 omap_crtc_error_irq(crtc, irqstatus);
Archit Taneja0d8f3712013-03-26 19:15:19 +0530219 }
Rob Clarkf5f94542012-12-04 13:59:12 -0600220
Laurent Pinchart6b5538d2015-05-28 01:05:20 +0300221 omap_irq_ocp_error_handler(irqstatus);
Laurent Pinchart728ae8d2015-05-28 00:21:29 +0300222 omap_irq_fifo_underflow(priv, irqstatus);
223
Laurent Pinchart84e1d452016-04-19 03:07:59 +0300224 spin_lock_irqsave(&priv->wait_lock, flags);
Laurent Pinchart80f91bf2016-04-19 02:47:02 +0300225 list_for_each_entry_safe(wait, n, &priv->wait_list, node) {
226 if (wait->irqmask & irqstatus)
227 omap_irq_wait_handler(wait);
Rob Clarkf5f94542012-12-04 13:59:12 -0600228 }
Laurent Pinchart84e1d452016-04-19 03:07:59 +0300229 spin_unlock_irqrestore(&priv->wait_lock, flags);
Rob Clarkf5f94542012-12-04 13:59:12 -0600230
231 return IRQ_HANDLED;
232}
233
Laurent Pinchart728ae8d2015-05-28 00:21:29 +0300234static const u32 omap_underflow_irqs[] = {
235 [OMAP_DSS_GFX] = DISPC_IRQ_GFX_FIFO_UNDERFLOW,
236 [OMAP_DSS_VIDEO1] = DISPC_IRQ_VID1_FIFO_UNDERFLOW,
237 [OMAP_DSS_VIDEO2] = DISPC_IRQ_VID2_FIFO_UNDERFLOW,
238 [OMAP_DSS_VIDEO3] = DISPC_IRQ_VID3_FIFO_UNDERFLOW,
239};
240
Laurent Pinchartf13ab002015-01-25 22:06:45 +0200241/*
242 * We need a special version, instead of just using drm_irq_install(),
243 * because we need to register the irq via omapdss. Once omapdss and
244 * omapdrm are merged together we can assign the dispc hwmod data to
245 * ourselves and drop these and just use drm_irq_{install,uninstall}()
246 */
Rob Clarkf5f94542012-12-04 13:59:12 -0600247
Laurent Pinchartf13ab002015-01-25 22:06:45 +0200248int omap_drm_irq_install(struct drm_device *dev)
Rob Clarkf5f94542012-12-04 13:59:12 -0600249{
250 struct omap_drm_private *priv = dev->dev_private;
Tomi Valkeinen9f759222015-11-05 18:39:52 +0200251 unsigned int num_mgrs = priv->dispc_ops->get_num_mgrs();
Laurent Pinchart728ae8d2015-05-28 00:21:29 +0300252 unsigned int max_planes;
253 unsigned int i;
Laurent Pinchartf13ab002015-01-25 22:06:45 +0200254 int ret;
Rob Clarkf5f94542012-12-04 13:59:12 -0600255
Laurent Pinchart84e1d452016-04-19 03:07:59 +0300256 spin_lock_init(&priv->wait_lock);
Laurent Pinchart80f91bf2016-04-19 02:47:02 +0300257 INIT_LIST_HEAD(&priv->wait_list);
Rob Clarkf5f94542012-12-04 13:59:12 -0600258
Laurent Pinchart6b5538d2015-05-28 01:05:20 +0300259 priv->irq_mask = DISPC_IRQ_OCP_ERR;
Laurent Pinchart728ae8d2015-05-28 00:21:29 +0300260
261 max_planes = min(ARRAY_SIZE(priv->planes),
262 ARRAY_SIZE(omap_underflow_irqs));
263 for (i = 0; i < max_planes; ++i) {
264 if (priv->planes[i])
265 priv->irq_mask |= omap_underflow_irqs[i];
266 }
267
Laurent Pincharte0519af2015-05-28 00:21:29 +0300268 for (i = 0; i < num_mgrs; ++i)
Tomi Valkeinen9f759222015-11-05 18:39:52 +0200269 priv->irq_mask |= priv->dispc_ops->mgr_get_sync_lost_irq(i);
Laurent Pincharte0519af2015-05-28 00:21:29 +0300270
Tomi Valkeinen9f759222015-11-05 18:39:52 +0200271 priv->dispc_ops->runtime_get();
272 priv->dispc_ops->clear_irqstatus(0xffffffff);
273 priv->dispc_ops->runtime_put();
Laurent Pinchartf13ab002015-01-25 22:06:45 +0200274
Tomi Valkeinen9f759222015-11-05 18:39:52 +0200275 ret = priv->dispc_ops->request_irq(omap_irq_handler, dev);
Laurent Pinchartf13ab002015-01-25 22:06:45 +0200276 if (ret < 0)
277 return ret;
278
Laurent Pinchartf13ab002015-01-25 22:06:45 +0200279 dev->irq_enabled = true;
280
Rob Clarkf5f94542012-12-04 13:59:12 -0600281 return 0;
282}
283
Laurent Pinchartf13ab002015-01-25 22:06:45 +0200284void omap_drm_irq_uninstall(struct drm_device *dev)
Rob Clarkf5f94542012-12-04 13:59:12 -0600285{
Tomi Valkeinen9f759222015-11-05 18:39:52 +0200286 struct omap_drm_private *priv = dev->dev_private;
Rob Clarkf5f94542012-12-04 13:59:12 -0600287
Laurent Pinchartf13ab002015-01-25 22:06:45 +0200288 if (!dev->irq_enabled)
289 return;
Rob Clarkf5f94542012-12-04 13:59:12 -0600290
Laurent Pinchartf13ab002015-01-25 22:06:45 +0200291 dev->irq_enabled = false;
292
Tomi Valkeinen9f759222015-11-05 18:39:52 +0200293 priv->dispc_ops->free_irq(dev);
Rob Clarkf5f94542012-12-04 13:59:12 -0600294}