blob: 58c5efb267665c966c76ab5edfb74970382daf5f [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
22static DEFINE_SPINLOCK(list_lock);
23
24static void omap_irq_error_handler(struct omap_drm_irq *irq,
25 uint32_t irqstatus)
26{
27 DRM_ERROR("errors: %08x\n", irqstatus);
28}
29
30/* call with list_lock and dispc runtime held */
31static void omap_irq_update(struct drm_device *dev)
32{
33 struct omap_drm_private *priv = dev->dev_private;
34 struct omap_drm_irq *irq;
Laurent Pinchart728ae8d2015-05-28 00:21:29 +030035 uint32_t irqmask = priv->irq_mask;
Rob Clarkf5f94542012-12-04 13:59:12 -060036
Tomi Valkeinen8519c622014-11-28 14:34:16 +020037 assert_spin_locked(&list_lock);
Rob Clarkf5f94542012-12-04 13:59:12 -060038
39 list_for_each_entry(irq, &priv->irq_list, node)
40 irqmask |= irq->irqmask;
41
42 DBG("irqmask=%08x", irqmask);
43
44 dispc_write_irqenable(irqmask);
45 dispc_read_irqenable(); /* flush posted write */
46}
47
Tomi Valkeinen6da9f892013-10-24 09:50:50 +030048void __omap_irq_register(struct drm_device *dev, struct omap_drm_irq *irq)
Rob Clarkf5f94542012-12-04 13:59:12 -060049{
50 struct omap_drm_private *priv = dev->dev_private;
51 unsigned long flags;
52
Rob Clarkf5f94542012-12-04 13:59:12 -060053 spin_lock_irqsave(&list_lock, flags);
54
55 if (!WARN_ON(irq->registered)) {
56 irq->registered = true;
57 list_add(&irq->node, &priv->irq_list);
58 omap_irq_update(dev);
59 }
60
61 spin_unlock_irqrestore(&list_lock, flags);
Tomi Valkeinen6da9f892013-10-24 09:50:50 +030062}
63
64void omap_irq_register(struct drm_device *dev, struct omap_drm_irq *irq)
65{
66 dispc_runtime_get();
67
68 __omap_irq_register(dev, irq);
69
Rob Clarkf5f94542012-12-04 13:59:12 -060070 dispc_runtime_put();
71}
72
Tomi Valkeinen6da9f892013-10-24 09:50:50 +030073void __omap_irq_unregister(struct drm_device *dev, struct omap_drm_irq *irq)
Rob Clarkf5f94542012-12-04 13:59:12 -060074{
75 unsigned long flags;
76
Rob Clarkf5f94542012-12-04 13:59:12 -060077 spin_lock_irqsave(&list_lock, flags);
78
79 if (!WARN_ON(!irq->registered)) {
80 irq->registered = false;
81 list_del(&irq->node);
82 omap_irq_update(dev);
83 }
84
85 spin_unlock_irqrestore(&list_lock, flags);
Tomi Valkeinen6da9f892013-10-24 09:50:50 +030086}
87
88void omap_irq_unregister(struct drm_device *dev, struct omap_drm_irq *irq)
89{
90 dispc_runtime_get();
91
92 __omap_irq_unregister(dev, irq);
93
Rob Clarkf5f94542012-12-04 13:59:12 -060094 dispc_runtime_put();
95}
96
97struct omap_irq_wait {
98 struct omap_drm_irq irq;
99 int count;
100};
101
102static DECLARE_WAIT_QUEUE_HEAD(wait_event);
103
104static void wait_irq(struct omap_drm_irq *irq, uint32_t irqstatus)
105{
106 struct omap_irq_wait *wait =
107 container_of(irq, struct omap_irq_wait, irq);
108 wait->count--;
109 wake_up_all(&wait_event);
110}
111
112struct omap_irq_wait * omap_irq_wait_init(struct drm_device *dev,
113 uint32_t irqmask, int count)
114{
115 struct omap_irq_wait *wait = kzalloc(sizeof(*wait), GFP_KERNEL);
116 wait->irq.irq = wait_irq;
117 wait->irq.irqmask = irqmask;
118 wait->count = count;
119 omap_irq_register(dev, &wait->irq);
120 return wait;
121}
122
123int omap_irq_wait(struct drm_device *dev, struct omap_irq_wait *wait,
124 unsigned long timeout)
125{
126 int ret = wait_event_timeout(wait_event, (wait->count <= 0), timeout);
127 omap_irq_unregister(dev, &wait->irq);
128 kfree(wait);
129 if (ret == 0)
130 return -1;
131 return 0;
132}
133
134/**
135 * enable_vblank - enable vblank interrupt events
136 * @dev: DRM device
Thierry Reding88e72712015-09-24 18:35:31 +0200137 * @pipe: which irq to enable
Rob Clarkf5f94542012-12-04 13:59:12 -0600138 *
139 * Enable vblank interrupts for @crtc. If the device doesn't have
140 * a hardware vblank counter, this routine should be a no-op, since
141 * interrupts will have to stay on to keep the count accurate.
142 *
143 * RETURNS
144 * Zero on success, appropriate errno if the given @crtc's vblank
145 * interrupt cannot be enabled.
146 */
Thierry Reding88e72712015-09-24 18:35:31 +0200147int omap_irq_enable_vblank(struct drm_device *dev, unsigned int pipe)
Rob Clarkf5f94542012-12-04 13:59:12 -0600148{
149 struct omap_drm_private *priv = dev->dev_private;
Thierry Reding88e72712015-09-24 18:35:31 +0200150 struct drm_crtc *crtc = priv->crtcs[pipe];
Rob Clarkf5f94542012-12-04 13:59:12 -0600151 unsigned long flags;
152
Thierry Reding88e72712015-09-24 18:35:31 +0200153 DBG("dev=%p, crtc=%u", dev, pipe);
Rob Clarkf5f94542012-12-04 13:59:12 -0600154
Rob Clarkf5f94542012-12-04 13:59:12 -0600155 spin_lock_irqsave(&list_lock, flags);
Laurent Pinchart728ae8d2015-05-28 00:21:29 +0300156 priv->irq_mask |= pipe2vbl(crtc);
Rob Clarkf5f94542012-12-04 13:59:12 -0600157 omap_irq_update(dev);
158 spin_unlock_irqrestore(&list_lock, flags);
Rob Clarkf5f94542012-12-04 13:59:12 -0600159
160 return 0;
161}
162
163/**
164 * disable_vblank - disable vblank interrupt events
165 * @dev: DRM device
Thierry Reding88e72712015-09-24 18:35:31 +0200166 * @pipe: which irq to enable
Rob Clarkf5f94542012-12-04 13:59:12 -0600167 *
168 * Disable vblank interrupts for @crtc. If the device doesn't have
169 * a hardware vblank counter, this routine should be a no-op, since
170 * interrupts will have to stay on to keep the count accurate.
171 */
Thierry Reding88e72712015-09-24 18:35:31 +0200172void omap_irq_disable_vblank(struct drm_device *dev, unsigned int pipe)
Rob Clarkf5f94542012-12-04 13:59:12 -0600173{
174 struct omap_drm_private *priv = dev->dev_private;
Thierry Reding88e72712015-09-24 18:35:31 +0200175 struct drm_crtc *crtc = priv->crtcs[pipe];
Rob Clarkf5f94542012-12-04 13:59:12 -0600176 unsigned long flags;
177
Thierry Reding88e72712015-09-24 18:35:31 +0200178 DBG("dev=%p, crtc=%u", dev, pipe);
Rob Clarkf5f94542012-12-04 13:59:12 -0600179
Rob Clarkf5f94542012-12-04 13:59:12 -0600180 spin_lock_irqsave(&list_lock, flags);
Laurent Pinchart728ae8d2015-05-28 00:21:29 +0300181 priv->irq_mask &= ~pipe2vbl(crtc);
Rob Clarkf5f94542012-12-04 13:59:12 -0600182 omap_irq_update(dev);
183 spin_unlock_irqrestore(&list_lock, flags);
Rob Clarkf5f94542012-12-04 13:59:12 -0600184}
185
Laurent Pinchart728ae8d2015-05-28 00:21:29 +0300186static void omap_irq_fifo_underflow(struct omap_drm_private *priv,
187 u32 irqstatus)
188{
189 static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
190 DEFAULT_RATELIMIT_BURST);
191 static const struct {
192 const char *name;
193 u32 mask;
194 } sources[] = {
195 { "gfx", DISPC_IRQ_GFX_FIFO_UNDERFLOW },
196 { "vid1", DISPC_IRQ_VID1_FIFO_UNDERFLOW },
197 { "vid2", DISPC_IRQ_VID2_FIFO_UNDERFLOW },
198 { "vid3", DISPC_IRQ_VID3_FIFO_UNDERFLOW },
199 };
200
201 const u32 mask = DISPC_IRQ_GFX_FIFO_UNDERFLOW
202 | DISPC_IRQ_VID1_FIFO_UNDERFLOW
203 | DISPC_IRQ_VID2_FIFO_UNDERFLOW
204 | DISPC_IRQ_VID3_FIFO_UNDERFLOW;
205 unsigned int i;
206
207 spin_lock(&list_lock);
208 irqstatus &= priv->irq_mask & mask;
209 spin_unlock(&list_lock);
210
211 if (!irqstatus)
212 return;
213
214 if (!__ratelimit(&_rs))
215 return;
216
217 DRM_ERROR("FIFO underflow on ");
218
219 for (i = 0; i < ARRAY_SIZE(sources); ++i) {
220 if (sources[i].mask & irqstatus)
221 pr_cont("%s ", sources[i].name);
222 }
223
224 pr_cont("(0x%08x)\n", irqstatus);
225}
226
Laurent Pinchartf13ab002015-01-25 22:06:45 +0200227static irqreturn_t omap_irq_handler(int irq, void *arg)
Rob Clarkf5f94542012-12-04 13:59:12 -0600228{
229 struct drm_device *dev = (struct drm_device *) arg;
230 struct omap_drm_private *priv = dev->dev_private;
231 struct omap_drm_irq *handler, *n;
232 unsigned long flags;
233 unsigned int id;
234 u32 irqstatus;
235
236 irqstatus = dispc_read_irqstatus();
237 dispc_clear_irqstatus(irqstatus);
238 dispc_read_irqstatus(); /* flush posted write */
239
240 VERB("irqs: %08x", irqstatus);
241
Archit Taneja0d8f3712013-03-26 19:15:19 +0530242 for (id = 0; id < priv->num_crtcs; id++) {
243 struct drm_crtc *crtc = priv->crtcs[id];
Laurent Pincharte0519af2015-05-28 00:21:29 +0300244 enum omap_channel channel = omap_crtc_channel(crtc);
Archit Taneja0d8f3712013-03-26 19:15:19 +0530245
246 if (irqstatus & pipe2vbl(crtc))
Rob Clarkf5f94542012-12-04 13:59:12 -0600247 drm_handle_vblank(dev, id);
Laurent Pincharte0519af2015-05-28 00:21:29 +0300248
249 if (irqstatus & dispc_mgr_get_sync_lost_irq(channel))
250 omap_crtc_error_irq(crtc, irqstatus);
Archit Taneja0d8f3712013-03-26 19:15:19 +0530251 }
Rob Clarkf5f94542012-12-04 13:59:12 -0600252
Laurent Pinchart728ae8d2015-05-28 00:21:29 +0300253 omap_irq_fifo_underflow(priv, irqstatus);
254
Rob Clarkf5f94542012-12-04 13:59:12 -0600255 spin_lock_irqsave(&list_lock, flags);
256 list_for_each_entry_safe(handler, n, &priv->irq_list, node) {
257 if (handler->irqmask & irqstatus) {
258 spin_unlock_irqrestore(&list_lock, flags);
259 handler->irq(handler, handler->irqmask & irqstatus);
260 spin_lock_irqsave(&list_lock, flags);
261 }
262 }
263 spin_unlock_irqrestore(&list_lock, flags);
264
265 return IRQ_HANDLED;
266}
267
Laurent Pinchart728ae8d2015-05-28 00:21:29 +0300268static const u32 omap_underflow_irqs[] = {
269 [OMAP_DSS_GFX] = DISPC_IRQ_GFX_FIFO_UNDERFLOW,
270 [OMAP_DSS_VIDEO1] = DISPC_IRQ_VID1_FIFO_UNDERFLOW,
271 [OMAP_DSS_VIDEO2] = DISPC_IRQ_VID2_FIFO_UNDERFLOW,
272 [OMAP_DSS_VIDEO3] = DISPC_IRQ_VID3_FIFO_UNDERFLOW,
273};
274
Laurent Pinchartf13ab002015-01-25 22:06:45 +0200275/*
276 * We need a special version, instead of just using drm_irq_install(),
277 * because we need to register the irq via omapdss. Once omapdss and
278 * omapdrm are merged together we can assign the dispc hwmod data to
279 * ourselves and drop these and just use drm_irq_{install,uninstall}()
280 */
Rob Clarkf5f94542012-12-04 13:59:12 -0600281
Laurent Pinchartf13ab002015-01-25 22:06:45 +0200282int omap_drm_irq_install(struct drm_device *dev)
Rob Clarkf5f94542012-12-04 13:59:12 -0600283{
284 struct omap_drm_private *priv = dev->dev_private;
285 struct omap_drm_irq *error_handler = &priv->error_handler;
Laurent Pincharte0519af2015-05-28 00:21:29 +0300286 unsigned int num_mgrs = dss_feat_get_num_mgrs();
Laurent Pinchart728ae8d2015-05-28 00:21:29 +0300287 unsigned int max_planes;
288 unsigned int i;
Laurent Pinchartf13ab002015-01-25 22:06:45 +0200289 int ret;
Rob Clarkf5f94542012-12-04 13:59:12 -0600290
291 INIT_LIST_HEAD(&priv->irq_list);
292
Laurent Pinchart728ae8d2015-05-28 00:21:29 +0300293 priv->irq_mask = 0;
294
295 max_planes = min(ARRAY_SIZE(priv->planes),
296 ARRAY_SIZE(omap_underflow_irqs));
297 for (i = 0; i < max_planes; ++i) {
298 if (priv->planes[i])
299 priv->irq_mask |= omap_underflow_irqs[i];
300 }
301
Laurent Pincharte0519af2015-05-28 00:21:29 +0300302 for (i = 0; i < num_mgrs; ++i)
303 priv->irq_mask |= dispc_mgr_get_sync_lost_irq(i);
304
Laurent Pinchartf13ab002015-01-25 22:06:45 +0200305 dispc_runtime_get();
306 dispc_clear_irqstatus(0xffffffff);
307 dispc_runtime_put();
308
309 ret = dispc_request_irq(omap_irq_handler, dev);
310 if (ret < 0)
311 return ret;
312
Rob Clarkf5f94542012-12-04 13:59:12 -0600313 error_handler->irq = omap_irq_error_handler;
314 error_handler->irqmask = DISPC_IRQ_OCP_ERR;
315
316 /* for now ignore DISPC_IRQ_SYNC_LOST_DIGIT.. really I think
317 * we just need to ignore it while enabling tv-out
318 */
319 error_handler->irqmask &= ~DISPC_IRQ_SYNC_LOST_DIGIT;
320
321 omap_irq_register(dev, error_handler);
322
Laurent Pinchartf13ab002015-01-25 22:06:45 +0200323 dev->irq_enabled = true;
324
Rob Clarkf5f94542012-12-04 13:59:12 -0600325 return 0;
326}
327
Laurent Pinchartf13ab002015-01-25 22:06:45 +0200328void omap_drm_irq_uninstall(struct drm_device *dev)
Rob Clarkf5f94542012-12-04 13:59:12 -0600329{
330 unsigned long irqflags;
Ville Syrjälä44238432013-10-04 14:53:37 +0300331 int i;
Rob Clarkf5f94542012-12-04 13:59:12 -0600332
Laurent Pinchartf13ab002015-01-25 22:06:45 +0200333 if (!dev->irq_enabled)
334 return;
Rob Clarkf5f94542012-12-04 13:59:12 -0600335
Laurent Pinchartf13ab002015-01-25 22:06:45 +0200336 dev->irq_enabled = false;
337
338 /* Wake up any waiters so they don't hang. */
Rob Clarkf5f94542012-12-04 13:59:12 -0600339 if (dev->num_crtcs) {
340 spin_lock_irqsave(&dev->vbl_lock, irqflags);
341 for (i = 0; i < dev->num_crtcs; i++) {
Daniel Vetter57ed0f72013-12-11 11:34:43 +0100342 wake_up(&dev->vblank[i].queue);
Ville Syrjälä5380e922013-10-04 14:53:36 +0300343 dev->vblank[i].enabled = false;
344 dev->vblank[i].last =
Rob Clarkf5f94542012-12-04 13:59:12 -0600345 dev->driver->get_vblank_counter(dev, i);
346 }
347 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
348 }
349
Rob Clarkf5f94542012-12-04 13:59:12 -0600350 dispc_free_irq(dev);
Rob Clarkf5f94542012-12-04 13:59:12 -0600351}