xref: /openbmc/linux/drivers/gpu/drm/i915/i915_irq.c (revision 630681d9a5314e6cf53d144f7f58b7c19862a7d3)
1c0e09200SDave Airlie /* i915_irq.c -- IRQ support for the I915 -*- linux-c -*-
2c0e09200SDave Airlie  */
3c0e09200SDave Airlie /*
4c0e09200SDave Airlie  * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
5c0e09200SDave Airlie  * All Rights Reserved.
6c0e09200SDave Airlie  *
7c0e09200SDave Airlie  * Permission is hereby granted, free of charge, to any person obtaining a
8c0e09200SDave Airlie  * copy of this software and associated documentation files (the
9c0e09200SDave Airlie  * "Software"), to deal in the Software without restriction, including
10c0e09200SDave Airlie  * without limitation the rights to use, copy, modify, merge, publish,
11c0e09200SDave Airlie  * distribute, sub license, and/or sell copies of the Software, and to
12c0e09200SDave Airlie  * permit persons to whom the Software is furnished to do so, subject to
13c0e09200SDave Airlie  * the following conditions:
14c0e09200SDave Airlie  *
15c0e09200SDave Airlie  * The above copyright notice and this permission notice (including the
16c0e09200SDave Airlie  * next paragraph) shall be included in all copies or substantial portions
17c0e09200SDave Airlie  * of the Software.
18c0e09200SDave Airlie  *
19c0e09200SDave Airlie  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20c0e09200SDave Airlie  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21c0e09200SDave Airlie  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22c0e09200SDave Airlie  * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
23c0e09200SDave Airlie  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24c0e09200SDave Airlie  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25c0e09200SDave Airlie  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26c0e09200SDave Airlie  *
27c0e09200SDave Airlie  */
28c0e09200SDave Airlie 
29c0e09200SDave Airlie #include "drmP.h"
30c0e09200SDave Airlie #include "drm.h"
31c0e09200SDave Airlie #include "i915_drm.h"
32c0e09200SDave Airlie #include "i915_drv.h"
33c0e09200SDave Airlie 
34c0e09200SDave Airlie #define MAX_NOPID ((u32)~0)
35c0e09200SDave Airlie 
36ed4cb414SEric Anholt /** These are the interrupts used by the driver */
37ed4cb414SEric Anholt #define I915_INTERRUPT_ENABLE_MASK (I915_USER_INTERRUPT |		\
388ee1c3dbSMatthew Garrett 				    I915_ASLE_INTERRUPT |		\
390a3e67a4SJesse Barnes 				    I915_DISPLAY_PIPE_A_EVENT_INTERRUPT | \
408ee1c3dbSMatthew Garrett 				    I915_DISPLAY_PIPE_B_EVENT_INTERRUPT)
41ed4cb414SEric Anholt 
428ee1c3dbSMatthew Garrett void
43ed4cb414SEric Anholt i915_enable_irq(drm_i915_private_t *dev_priv, u32 mask)
44ed4cb414SEric Anholt {
45ed4cb414SEric Anholt 	if ((dev_priv->irq_mask_reg & mask) != 0) {
46ed4cb414SEric Anholt 		dev_priv->irq_mask_reg &= ~mask;
47ed4cb414SEric Anholt 		I915_WRITE(IMR, dev_priv->irq_mask_reg);
48ed4cb414SEric Anholt 		(void) I915_READ(IMR);
49ed4cb414SEric Anholt 	}
50ed4cb414SEric Anholt }
51ed4cb414SEric Anholt 
52ed4cb414SEric Anholt static inline void
53ed4cb414SEric Anholt i915_disable_irq(drm_i915_private_t *dev_priv, u32 mask)
54ed4cb414SEric Anholt {
55ed4cb414SEric Anholt 	if ((dev_priv->irq_mask_reg & mask) != mask) {
56ed4cb414SEric Anholt 		dev_priv->irq_mask_reg |= mask;
57ed4cb414SEric Anholt 		I915_WRITE(IMR, dev_priv->irq_mask_reg);
58ed4cb414SEric Anholt 		(void) I915_READ(IMR);
59ed4cb414SEric Anholt 	}
60ed4cb414SEric Anholt }
61ed4cb414SEric Anholt 
62c0e09200SDave Airlie /**
630a3e67a4SJesse Barnes  * i915_get_pipe - return the the pipe associated with a given plane
640a3e67a4SJesse Barnes  * @dev: DRM device
650a3e67a4SJesse Barnes  * @plane: plane to look for
660a3e67a4SJesse Barnes  *
670a3e67a4SJesse Barnes  * The Intel Mesa & 2D drivers call the vblank routines with a plane number
680a3e67a4SJesse Barnes  * rather than a pipe number, since they may not always be equal.  This routine
690a3e67a4SJesse Barnes  * maps the given @plane back to a pipe number.
700a3e67a4SJesse Barnes  */
710a3e67a4SJesse Barnes static int
720a3e67a4SJesse Barnes i915_get_pipe(struct drm_device *dev, int plane)
730a3e67a4SJesse Barnes {
740a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
750a3e67a4SJesse Barnes 	u32 dspcntr;
760a3e67a4SJesse Barnes 
770a3e67a4SJesse Barnes 	dspcntr = plane ? I915_READ(DSPBCNTR) : I915_READ(DSPACNTR);
780a3e67a4SJesse Barnes 
790a3e67a4SJesse Barnes 	return dspcntr & DISPPLANE_SEL_PIPE_MASK ? 1 : 0;
800a3e67a4SJesse Barnes }
810a3e67a4SJesse Barnes 
820a3e67a4SJesse Barnes /**
830a3e67a4SJesse Barnes  * i915_get_plane - return the the plane associated with a given pipe
840a3e67a4SJesse Barnes  * @dev: DRM device
850a3e67a4SJesse Barnes  * @pipe: pipe to look for
860a3e67a4SJesse Barnes  *
870a3e67a4SJesse Barnes  * The Intel Mesa & 2D drivers call the vblank routines with a plane number
880a3e67a4SJesse Barnes  * rather than a plane number, since they may not always be equal.  This routine
890a3e67a4SJesse Barnes  * maps the given @pipe back to a plane number.
900a3e67a4SJesse Barnes  */
910a3e67a4SJesse Barnes static int
920a3e67a4SJesse Barnes i915_get_plane(struct drm_device *dev, int pipe)
930a3e67a4SJesse Barnes {
940a3e67a4SJesse Barnes 	if (i915_get_pipe(dev, 0) == pipe)
950a3e67a4SJesse Barnes 		return 0;
960a3e67a4SJesse Barnes 	return 1;
970a3e67a4SJesse Barnes }
980a3e67a4SJesse Barnes 
990a3e67a4SJesse Barnes /**
1000a3e67a4SJesse Barnes  * i915_pipe_enabled - check if a pipe is enabled
1010a3e67a4SJesse Barnes  * @dev: DRM device
1020a3e67a4SJesse Barnes  * @pipe: pipe to check
1030a3e67a4SJesse Barnes  *
1040a3e67a4SJesse Barnes  * Reading certain registers when the pipe is disabled can hang the chip.
1050a3e67a4SJesse Barnes  * Use this routine to make sure the PLL is running and the pipe is active
1060a3e67a4SJesse Barnes  * before reading such registers if unsure.
1070a3e67a4SJesse Barnes  */
1080a3e67a4SJesse Barnes static int
1090a3e67a4SJesse Barnes i915_pipe_enabled(struct drm_device *dev, int pipe)
1100a3e67a4SJesse Barnes {
1110a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1120a3e67a4SJesse Barnes 	unsigned long pipeconf = pipe ? PIPEBCONF : PIPEACONF;
1130a3e67a4SJesse Barnes 
1140a3e67a4SJesse Barnes 	if (I915_READ(pipeconf) & PIPEACONF_ENABLE)
1150a3e67a4SJesse Barnes 		return 1;
1160a3e67a4SJesse Barnes 
1170a3e67a4SJesse Barnes 	return 0;
1180a3e67a4SJesse Barnes }
1190a3e67a4SJesse Barnes 
1200a3e67a4SJesse Barnes /**
121c0e09200SDave Airlie  * Emit blits for scheduled buffer swaps.
122c0e09200SDave Airlie  *
123c0e09200SDave Airlie  * This function will be called with the HW lock held.
124c0e09200SDave Airlie  */
125c0e09200SDave Airlie static void i915_vblank_tasklet(struct drm_device *dev)
126c0e09200SDave Airlie {
127c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
128c0e09200SDave Airlie 	unsigned long irqflags;
129c0e09200SDave Airlie 	struct list_head *list, *tmp, hits, *hit;
130c0e09200SDave Airlie 	int nhits, nrects, slice[2], upper[2], lower[2], i;
1310a3e67a4SJesse Barnes 	unsigned counter[2];
132c0e09200SDave Airlie 	struct drm_drawable_info *drw;
133c0e09200SDave Airlie 	drm_i915_sarea_t *sarea_priv = dev_priv->sarea_priv;
134c0e09200SDave Airlie 	u32 cpp = dev_priv->cpp;
135c0e09200SDave Airlie 	u32 cmd = (cpp == 4) ? (XY_SRC_COPY_BLT_CMD |
136c0e09200SDave Airlie 				XY_SRC_COPY_BLT_WRITE_ALPHA |
137c0e09200SDave Airlie 				XY_SRC_COPY_BLT_WRITE_RGB)
138c0e09200SDave Airlie 			     : XY_SRC_COPY_BLT_CMD;
139c0e09200SDave Airlie 	u32 src_pitch = sarea_priv->pitch * cpp;
140c0e09200SDave Airlie 	u32 dst_pitch = sarea_priv->pitch * cpp;
141c0e09200SDave Airlie 	u32 ropcpp = (0xcc << 16) | ((cpp - 1) << 24);
142c0e09200SDave Airlie 	RING_LOCALS;
143c0e09200SDave Airlie 
144c0e09200SDave Airlie 	if (IS_I965G(dev) && sarea_priv->front_tiled) {
145c0e09200SDave Airlie 		cmd |= XY_SRC_COPY_BLT_DST_TILED;
146c0e09200SDave Airlie 		dst_pitch >>= 2;
147c0e09200SDave Airlie 	}
148c0e09200SDave Airlie 	if (IS_I965G(dev) && sarea_priv->back_tiled) {
149c0e09200SDave Airlie 		cmd |= XY_SRC_COPY_BLT_SRC_TILED;
150c0e09200SDave Airlie 		src_pitch >>= 2;
151c0e09200SDave Airlie 	}
152c0e09200SDave Airlie 
1530a3e67a4SJesse Barnes 	counter[0] = drm_vblank_count(dev, 0);
1540a3e67a4SJesse Barnes 	counter[1] = drm_vblank_count(dev, 1);
1550a3e67a4SJesse Barnes 
156c0e09200SDave Airlie 	DRM_DEBUG("\n");
157c0e09200SDave Airlie 
158c0e09200SDave Airlie 	INIT_LIST_HEAD(&hits);
159c0e09200SDave Airlie 
160c0e09200SDave Airlie 	nhits = nrects = 0;
161c0e09200SDave Airlie 
162c0e09200SDave Airlie 	spin_lock_irqsave(&dev_priv->swaps_lock, irqflags);
163c0e09200SDave Airlie 
164c0e09200SDave Airlie 	/* Find buffer swaps scheduled for this vertical blank */
165c0e09200SDave Airlie 	list_for_each_safe(list, tmp, &dev_priv->vbl_swaps.head) {
166c0e09200SDave Airlie 		drm_i915_vbl_swap_t *vbl_swap =
167c0e09200SDave Airlie 			list_entry(list, drm_i915_vbl_swap_t, head);
1680a3e67a4SJesse Barnes 		int pipe = i915_get_pipe(dev, vbl_swap->plane);
169c0e09200SDave Airlie 
1700a3e67a4SJesse Barnes 		if ((counter[pipe] - vbl_swap->sequence) > (1<<23))
171c0e09200SDave Airlie 			continue;
172c0e09200SDave Airlie 
173c0e09200SDave Airlie 		list_del(list);
174c0e09200SDave Airlie 		dev_priv->swaps_pending--;
1750a3e67a4SJesse Barnes 		drm_vblank_put(dev, pipe);
176c0e09200SDave Airlie 
177c0e09200SDave Airlie 		spin_unlock(&dev_priv->swaps_lock);
178c0e09200SDave Airlie 		spin_lock(&dev->drw_lock);
179c0e09200SDave Airlie 
180c0e09200SDave Airlie 		drw = drm_get_drawable_info(dev, vbl_swap->drw_id);
181c0e09200SDave Airlie 
182c0e09200SDave Airlie 		if (!drw) {
183c0e09200SDave Airlie 			spin_unlock(&dev->drw_lock);
184c0e09200SDave Airlie 			drm_free(vbl_swap, sizeof(*vbl_swap), DRM_MEM_DRIVER);
185c0e09200SDave Airlie 			spin_lock(&dev_priv->swaps_lock);
186c0e09200SDave Airlie 			continue;
187c0e09200SDave Airlie 		}
188c0e09200SDave Airlie 
189c0e09200SDave Airlie 		list_for_each(hit, &hits) {
190c0e09200SDave Airlie 			drm_i915_vbl_swap_t *swap_cmp =
191c0e09200SDave Airlie 				list_entry(hit, drm_i915_vbl_swap_t, head);
192c0e09200SDave Airlie 			struct drm_drawable_info *drw_cmp =
193c0e09200SDave Airlie 				drm_get_drawable_info(dev, swap_cmp->drw_id);
194c0e09200SDave Airlie 
195c0e09200SDave Airlie 			if (drw_cmp &&
196c0e09200SDave Airlie 			    drw_cmp->rects[0].y1 > drw->rects[0].y1) {
197c0e09200SDave Airlie 				list_add_tail(list, hit);
198c0e09200SDave Airlie 				break;
199c0e09200SDave Airlie 			}
200c0e09200SDave Airlie 		}
201c0e09200SDave Airlie 
202c0e09200SDave Airlie 		spin_unlock(&dev->drw_lock);
203c0e09200SDave Airlie 
204c0e09200SDave Airlie 		/* List of hits was empty, or we reached the end of it */
205c0e09200SDave Airlie 		if (hit == &hits)
206c0e09200SDave Airlie 			list_add_tail(list, hits.prev);
207c0e09200SDave Airlie 
208c0e09200SDave Airlie 		nhits++;
209c0e09200SDave Airlie 
210c0e09200SDave Airlie 		spin_lock(&dev_priv->swaps_lock);
211c0e09200SDave Airlie 	}
212c0e09200SDave Airlie 
213c0e09200SDave Airlie 	if (nhits == 0) {
214c0e09200SDave Airlie 		spin_unlock_irqrestore(&dev_priv->swaps_lock, irqflags);
215c0e09200SDave Airlie 		return;
216c0e09200SDave Airlie 	}
217c0e09200SDave Airlie 
218c0e09200SDave Airlie 	spin_unlock(&dev_priv->swaps_lock);
219c0e09200SDave Airlie 
220c0e09200SDave Airlie 	i915_kernel_lost_context(dev);
221c0e09200SDave Airlie 
222c0e09200SDave Airlie 	if (IS_I965G(dev)) {
223c0e09200SDave Airlie 		BEGIN_LP_RING(4);
224c0e09200SDave Airlie 
225c0e09200SDave Airlie 		OUT_RING(GFX_OP_DRAWRECT_INFO_I965);
226c0e09200SDave Airlie 		OUT_RING(0);
227c0e09200SDave Airlie 		OUT_RING(((sarea_priv->width - 1) & 0xffff) | ((sarea_priv->height - 1) << 16));
228c0e09200SDave Airlie 		OUT_RING(0);
229c0e09200SDave Airlie 		ADVANCE_LP_RING();
230c0e09200SDave Airlie 	} else {
231c0e09200SDave Airlie 		BEGIN_LP_RING(6);
232c0e09200SDave Airlie 
233c0e09200SDave Airlie 		OUT_RING(GFX_OP_DRAWRECT_INFO);
234c0e09200SDave Airlie 		OUT_RING(0);
235c0e09200SDave Airlie 		OUT_RING(0);
236c0e09200SDave Airlie 		OUT_RING(sarea_priv->width | sarea_priv->height << 16);
237c0e09200SDave Airlie 		OUT_RING(sarea_priv->width | sarea_priv->height << 16);
238c0e09200SDave Airlie 		OUT_RING(0);
239c0e09200SDave Airlie 
240c0e09200SDave Airlie 		ADVANCE_LP_RING();
241c0e09200SDave Airlie 	}
242c0e09200SDave Airlie 
243c0e09200SDave Airlie 	sarea_priv->ctxOwner = DRM_KERNEL_CONTEXT;
244c0e09200SDave Airlie 
245c0e09200SDave Airlie 	upper[0] = upper[1] = 0;
246c0e09200SDave Airlie 	slice[0] = max(sarea_priv->pipeA_h / nhits, 1);
247c0e09200SDave Airlie 	slice[1] = max(sarea_priv->pipeB_h / nhits, 1);
248c0e09200SDave Airlie 	lower[0] = sarea_priv->pipeA_y + slice[0];
249c0e09200SDave Airlie 	lower[1] = sarea_priv->pipeB_y + slice[0];
250c0e09200SDave Airlie 
251c0e09200SDave Airlie 	spin_lock(&dev->drw_lock);
252c0e09200SDave Airlie 
253c0e09200SDave Airlie 	/* Emit blits for buffer swaps, partitioning both outputs into as many
254c0e09200SDave Airlie 	 * slices as there are buffer swaps scheduled in order to avoid tearing
255c0e09200SDave Airlie 	 * (based on the assumption that a single buffer swap would always
256c0e09200SDave Airlie 	 * complete before scanout starts).
257c0e09200SDave Airlie 	 */
258c0e09200SDave Airlie 	for (i = 0; i++ < nhits;
259c0e09200SDave Airlie 	     upper[0] = lower[0], lower[0] += slice[0],
260c0e09200SDave Airlie 	     upper[1] = lower[1], lower[1] += slice[1]) {
261c0e09200SDave Airlie 		if (i == nhits)
262c0e09200SDave Airlie 			lower[0] = lower[1] = sarea_priv->height;
263c0e09200SDave Airlie 
264c0e09200SDave Airlie 		list_for_each(hit, &hits) {
265c0e09200SDave Airlie 			drm_i915_vbl_swap_t *swap_hit =
266c0e09200SDave Airlie 				list_entry(hit, drm_i915_vbl_swap_t, head);
267c0e09200SDave Airlie 			struct drm_clip_rect *rect;
2680a3e67a4SJesse Barnes 			int num_rects, plane;
269c0e09200SDave Airlie 			unsigned short top, bottom;
270c0e09200SDave Airlie 
271c0e09200SDave Airlie 			drw = drm_get_drawable_info(dev, swap_hit->drw_id);
272c0e09200SDave Airlie 
273c0e09200SDave Airlie 			if (!drw)
274c0e09200SDave Airlie 				continue;
275c0e09200SDave Airlie 
276c0e09200SDave Airlie 			rect = drw->rects;
2770a3e67a4SJesse Barnes 			plane = swap_hit->plane;
2780a3e67a4SJesse Barnes 			top = upper[plane];
2790a3e67a4SJesse Barnes 			bottom = lower[plane];
280c0e09200SDave Airlie 
281c0e09200SDave Airlie 			for (num_rects = drw->num_rects; num_rects--; rect++) {
282c0e09200SDave Airlie 				int y1 = max(rect->y1, top);
283c0e09200SDave Airlie 				int y2 = min(rect->y2, bottom);
284c0e09200SDave Airlie 
285c0e09200SDave Airlie 				if (y1 >= y2)
286c0e09200SDave Airlie 					continue;
287c0e09200SDave Airlie 
288c0e09200SDave Airlie 				BEGIN_LP_RING(8);
289c0e09200SDave Airlie 
290c0e09200SDave Airlie 				OUT_RING(cmd);
291c0e09200SDave Airlie 				OUT_RING(ropcpp | dst_pitch);
292c0e09200SDave Airlie 				OUT_RING((y1 << 16) | rect->x1);
293c0e09200SDave Airlie 				OUT_RING((y2 << 16) | rect->x2);
294c0e09200SDave Airlie 				OUT_RING(sarea_priv->front_offset);
295c0e09200SDave Airlie 				OUT_RING((y1 << 16) | rect->x1);
296c0e09200SDave Airlie 				OUT_RING(src_pitch);
297c0e09200SDave Airlie 				OUT_RING(sarea_priv->back_offset);
298c0e09200SDave Airlie 
299c0e09200SDave Airlie 				ADVANCE_LP_RING();
300c0e09200SDave Airlie 			}
301c0e09200SDave Airlie 		}
302c0e09200SDave Airlie 	}
303c0e09200SDave Airlie 
304c0e09200SDave Airlie 	spin_unlock_irqrestore(&dev->drw_lock, irqflags);
305c0e09200SDave Airlie 
306c0e09200SDave Airlie 	list_for_each_safe(hit, tmp, &hits) {
307c0e09200SDave Airlie 		drm_i915_vbl_swap_t *swap_hit =
308c0e09200SDave Airlie 			list_entry(hit, drm_i915_vbl_swap_t, head);
309c0e09200SDave Airlie 
310c0e09200SDave Airlie 		list_del(hit);
311c0e09200SDave Airlie 
312c0e09200SDave Airlie 		drm_free(swap_hit, sizeof(*swap_hit), DRM_MEM_DRIVER);
313c0e09200SDave Airlie 	}
314c0e09200SDave Airlie }
315c0e09200SDave Airlie 
3160a3e67a4SJesse Barnes u32 i915_get_vblank_counter(struct drm_device *dev, int plane)
3170a3e67a4SJesse Barnes {
3180a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3190a3e67a4SJesse Barnes 	unsigned long high_frame;
3200a3e67a4SJesse Barnes 	unsigned long low_frame;
3210a3e67a4SJesse Barnes 	u32 high1, high2, low, count;
3220a3e67a4SJesse Barnes 	int pipe;
3230a3e67a4SJesse Barnes 
3240a3e67a4SJesse Barnes 	pipe = i915_get_pipe(dev, plane);
3250a3e67a4SJesse Barnes 	high_frame = pipe ? PIPEBFRAMEHIGH : PIPEAFRAMEHIGH;
3260a3e67a4SJesse Barnes 	low_frame = pipe ? PIPEBFRAMEPIXEL : PIPEAFRAMEPIXEL;
3270a3e67a4SJesse Barnes 
3280a3e67a4SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe)) {
3290a3e67a4SJesse Barnes 		DRM_ERROR("trying to get vblank count for disabled pipe %d\n", pipe);
3300a3e67a4SJesse Barnes 		return 0;
3310a3e67a4SJesse Barnes 	}
3320a3e67a4SJesse Barnes 
3330a3e67a4SJesse Barnes 	/*
3340a3e67a4SJesse Barnes 	 * High & low register fields aren't synchronized, so make sure
3350a3e67a4SJesse Barnes 	 * we get a low value that's stable across two reads of the high
3360a3e67a4SJesse Barnes 	 * register.
3370a3e67a4SJesse Barnes 	 */
3380a3e67a4SJesse Barnes 	do {
3390a3e67a4SJesse Barnes 		high1 = ((I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK) >>
3400a3e67a4SJesse Barnes 			 PIPE_FRAME_HIGH_SHIFT);
3410a3e67a4SJesse Barnes 		low =  ((I915_READ(low_frame) & PIPE_FRAME_LOW_MASK) >>
3420a3e67a4SJesse Barnes 			PIPE_FRAME_LOW_SHIFT);
3430a3e67a4SJesse Barnes 		high2 = ((I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK) >>
3440a3e67a4SJesse Barnes 			 PIPE_FRAME_HIGH_SHIFT);
3450a3e67a4SJesse Barnes 	} while (high1 != high2);
3460a3e67a4SJesse Barnes 
3470a3e67a4SJesse Barnes 	count = (high1 << 8) | low;
3480a3e67a4SJesse Barnes 
3490a3e67a4SJesse Barnes 	return count;
3500a3e67a4SJesse Barnes }
3510a3e67a4SJesse Barnes 
352546b0974SEric Anholt void
353546b0974SEric Anholt i915_gem_vblank_work_handler(struct work_struct *work)
354546b0974SEric Anholt {
355546b0974SEric Anholt 	drm_i915_private_t *dev_priv;
356546b0974SEric Anholt 	struct drm_device *dev;
357546b0974SEric Anholt 
358546b0974SEric Anholt 	dev_priv = container_of(work, drm_i915_private_t,
359546b0974SEric Anholt 				mm.vblank_work);
360546b0974SEric Anholt 	dev = dev_priv->dev;
361546b0974SEric Anholt 
362546b0974SEric Anholt 	mutex_lock(&dev->struct_mutex);
363546b0974SEric Anholt 	i915_vblank_tasklet(dev);
364546b0974SEric Anholt 	mutex_unlock(&dev->struct_mutex);
365546b0974SEric Anholt }
366546b0974SEric Anholt 
367c0e09200SDave Airlie irqreturn_t i915_driver_irq_handler(DRM_IRQ_ARGS)
368c0e09200SDave Airlie {
369c0e09200SDave Airlie 	struct drm_device *dev = (struct drm_device *) arg;
370c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
371ed4cb414SEric Anholt 	u32 iir;
3720a3e67a4SJesse Barnes 	u32 pipea_stats, pipeb_stats;
3730a3e67a4SJesse Barnes 	int vblank = 0;
374c0e09200SDave Airlie 
375*630681d9SEric Anholt 	atomic_inc(&dev_priv->irq_received);
376*630681d9SEric Anholt 
377ed4cb414SEric Anholt 	if (dev->pdev->msi_enabled)
378ed4cb414SEric Anholt 		I915_WRITE(IMR, ~0);
379ed4cb414SEric Anholt 	iir = I915_READ(IIR);
380c0e09200SDave Airlie 
381ed4cb414SEric Anholt 	if (iir == 0) {
382ed4cb414SEric Anholt 		if (dev->pdev->msi_enabled) {
383ed4cb414SEric Anholt 			I915_WRITE(IMR, dev_priv->irq_mask_reg);
384ed4cb414SEric Anholt 			(void) I915_READ(IMR);
385ed4cb414SEric Anholt 		}
386c0e09200SDave Airlie 		return IRQ_NONE;
387ed4cb414SEric Anholt 	}
388c0e09200SDave Airlie 
3890a3e67a4SJesse Barnes 	/*
3900a3e67a4SJesse Barnes 	 * Clear the PIPE(A|B)STAT regs before the IIR otherwise
3910a3e67a4SJesse Barnes 	 * we may get extra interrupts.
3920a3e67a4SJesse Barnes 	 */
3930a3e67a4SJesse Barnes 	if (iir & I915_DISPLAY_PIPE_A_EVENT_INTERRUPT) {
3940a3e67a4SJesse Barnes 		pipea_stats = I915_READ(PIPEASTAT);
3950a3e67a4SJesse Barnes 		if (!(dev_priv->vblank_pipe & DRM_I915_VBLANK_PIPE_A))
3960a3e67a4SJesse Barnes 			pipea_stats &= ~(PIPE_START_VBLANK_INTERRUPT_ENABLE |
3970a3e67a4SJesse Barnes 					 PIPE_VBLANK_INTERRUPT_ENABLE);
3980a3e67a4SJesse Barnes 		else if (pipea_stats & (PIPE_START_VBLANK_INTERRUPT_STATUS|
3990a3e67a4SJesse Barnes 					PIPE_VBLANK_INTERRUPT_STATUS)) {
4000a3e67a4SJesse Barnes 			vblank++;
4010a3e67a4SJesse Barnes 			drm_handle_vblank(dev, i915_get_plane(dev, 0));
4020a3e67a4SJesse Barnes 		}
4030a3e67a4SJesse Barnes 
4048ee1c3dbSMatthew Garrett 		I915_WRITE(PIPEASTAT, pipea_stats);
4050a3e67a4SJesse Barnes 	}
4060a3e67a4SJesse Barnes 	if (iir & I915_DISPLAY_PIPE_B_EVENT_INTERRUPT) {
4070a3e67a4SJesse Barnes 		pipeb_stats = I915_READ(PIPEBSTAT);
4080a3e67a4SJesse Barnes 		/* Ack the event */
4098ee1c3dbSMatthew Garrett 		I915_WRITE(PIPEBSTAT, pipeb_stats);
4108ee1c3dbSMatthew Garrett 
4110a3e67a4SJesse Barnes 		/* The vblank interrupt gets enabled even if we didn't ask for
4120a3e67a4SJesse Barnes 		   it, so make sure it's shut down again */
4130a3e67a4SJesse Barnes 		if (!(dev_priv->vblank_pipe & DRM_I915_VBLANK_PIPE_B))
4140a3e67a4SJesse Barnes 			pipeb_stats &= ~(PIPE_START_VBLANK_INTERRUPT_ENABLE |
4150a3e67a4SJesse Barnes 					 PIPE_VBLANK_INTERRUPT_ENABLE);
4160a3e67a4SJesse Barnes 		else if (pipeb_stats & (PIPE_START_VBLANK_INTERRUPT_STATUS|
4170a3e67a4SJesse Barnes 					PIPE_VBLANK_INTERRUPT_STATUS)) {
4180a3e67a4SJesse Barnes 			vblank++;
4190a3e67a4SJesse Barnes 			drm_handle_vblank(dev, i915_get_plane(dev, 1));
4200a3e67a4SJesse Barnes 		}
421c0e09200SDave Airlie 
4220a3e67a4SJesse Barnes 		if (pipeb_stats & I915_LEGACY_BLC_EVENT_STATUS)
4230a3e67a4SJesse Barnes 			opregion_asle_intr(dev);
4240a3e67a4SJesse Barnes 		I915_WRITE(PIPEBSTAT, pipeb_stats);
425c0e09200SDave Airlie 	}
426c0e09200SDave Airlie 
427673a394bSEric Anholt 	I915_WRITE(IIR, iir);
428673a394bSEric Anholt 	if (dev->pdev->msi_enabled)
429673a394bSEric Anholt 		I915_WRITE(IMR, dev_priv->irq_mask_reg);
430673a394bSEric Anholt 	(void) I915_READ(IIR); /* Flush posted writes */
4318ee1c3dbSMatthew Garrett 
432c99b058fSKristian Høgsberg 	if (dev_priv->sarea_priv)
433c99b058fSKristian Høgsberg 		dev_priv->sarea_priv->last_dispatch =
434c99b058fSKristian Høgsberg 			READ_BREADCRUMB(dev_priv);
4350a3e67a4SJesse Barnes 
436673a394bSEric Anholt 	if (iir & I915_USER_INTERRUPT) {
437673a394bSEric Anholt 		dev_priv->mm.irq_gem_seqno = i915_get_gem_seqno(dev);
438673a394bSEric Anholt 		DRM_WAKEUP(&dev_priv->irq_queue);
439673a394bSEric Anholt 	}
440673a394bSEric Anholt 
441673a394bSEric Anholt 	if (iir & I915_ASLE_INTERRUPT)
442673a394bSEric Anholt 		opregion_asle_intr(dev);
4430a3e67a4SJesse Barnes 
444546b0974SEric Anholt 	if (vblank && dev_priv->swaps_pending > 0) {
445546b0974SEric Anholt 		if (dev_priv->ring.ring_obj == NULL)
4460a3e67a4SJesse Barnes 			drm_locked_tasklet(dev, i915_vblank_tasklet);
447546b0974SEric Anholt 		else
448546b0974SEric Anholt 			schedule_work(&dev_priv->mm.vblank_work);
449546b0974SEric Anholt 	}
4508ee1c3dbSMatthew Garrett 
451c0e09200SDave Airlie 	return IRQ_HANDLED;
452c0e09200SDave Airlie }
453c0e09200SDave Airlie 
454c0e09200SDave Airlie static int i915_emit_irq(struct drm_device * dev)
455c0e09200SDave Airlie {
456c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
457c0e09200SDave Airlie 	RING_LOCALS;
458c0e09200SDave Airlie 
459c0e09200SDave Airlie 	i915_kernel_lost_context(dev);
460c0e09200SDave Airlie 
461c0e09200SDave Airlie 	DRM_DEBUG("\n");
462c0e09200SDave Airlie 
463c99b058fSKristian Høgsberg 	dev_priv->counter++;
464c0e09200SDave Airlie 	if (dev_priv->counter > 0x7FFFFFFFUL)
465c99b058fSKristian Høgsberg 		dev_priv->counter = 1;
466c99b058fSKristian Høgsberg 	if (dev_priv->sarea_priv)
467c99b058fSKristian Høgsberg 		dev_priv->sarea_priv->last_enqueue = dev_priv->counter;
468c0e09200SDave Airlie 
469c0e09200SDave Airlie 	BEGIN_LP_RING(6);
470585fb111SJesse Barnes 	OUT_RING(MI_STORE_DWORD_INDEX);
471585fb111SJesse Barnes 	OUT_RING(5 << MI_STORE_DWORD_INDEX_SHIFT);
472c0e09200SDave Airlie 	OUT_RING(dev_priv->counter);
473c0e09200SDave Airlie 	OUT_RING(0);
474c0e09200SDave Airlie 	OUT_RING(0);
475585fb111SJesse Barnes 	OUT_RING(MI_USER_INTERRUPT);
476c0e09200SDave Airlie 	ADVANCE_LP_RING();
477c0e09200SDave Airlie 
478c0e09200SDave Airlie 	return dev_priv->counter;
479c0e09200SDave Airlie }
480c0e09200SDave Airlie 
481673a394bSEric Anholt void i915_user_irq_get(struct drm_device *dev)
482ed4cb414SEric Anholt {
483ed4cb414SEric Anholt 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
484ed4cb414SEric Anholt 
485ed4cb414SEric Anholt 	spin_lock(&dev_priv->user_irq_lock);
486ed4cb414SEric Anholt 	if (dev->irq_enabled && (++dev_priv->user_irq_refcount == 1))
487ed4cb414SEric Anholt 		i915_enable_irq(dev_priv, I915_USER_INTERRUPT);
488ed4cb414SEric Anholt 	spin_unlock(&dev_priv->user_irq_lock);
489ed4cb414SEric Anholt }
490ed4cb414SEric Anholt 
4910a3e67a4SJesse Barnes void i915_user_irq_put(struct drm_device *dev)
492ed4cb414SEric Anholt {
493ed4cb414SEric Anholt 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
494ed4cb414SEric Anholt 
495ed4cb414SEric Anholt 	spin_lock(&dev_priv->user_irq_lock);
496ed4cb414SEric Anholt 	BUG_ON(dev->irq_enabled && dev_priv->user_irq_refcount <= 0);
497ed4cb414SEric Anholt 	if (dev->irq_enabled && (--dev_priv->user_irq_refcount == 0))
498ed4cb414SEric Anholt 		i915_disable_irq(dev_priv, I915_USER_INTERRUPT);
499ed4cb414SEric Anholt 	spin_unlock(&dev_priv->user_irq_lock);
500ed4cb414SEric Anholt }
501ed4cb414SEric Anholt 
502c0e09200SDave Airlie static int i915_wait_irq(struct drm_device * dev, int irq_nr)
503c0e09200SDave Airlie {
504c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
505c0e09200SDave Airlie 	int ret = 0;
506c0e09200SDave Airlie 
507c0e09200SDave Airlie 	DRM_DEBUG("irq_nr=%d breadcrumb=%d\n", irq_nr,
508c0e09200SDave Airlie 		  READ_BREADCRUMB(dev_priv));
509c0e09200SDave Airlie 
510ed4cb414SEric Anholt 	if (READ_BREADCRUMB(dev_priv) >= irq_nr) {
511c99b058fSKristian Høgsberg 		if (dev_priv->sarea_priv) {
512c99b058fSKristian Høgsberg 			dev_priv->sarea_priv->last_dispatch =
513c99b058fSKristian Høgsberg 				READ_BREADCRUMB(dev_priv);
514c99b058fSKristian Høgsberg 		}
515c0e09200SDave Airlie 		return 0;
516ed4cb414SEric Anholt 	}
517c0e09200SDave Airlie 
518c99b058fSKristian Høgsberg 	if (dev_priv->sarea_priv)
519c0e09200SDave Airlie 		dev_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT;
520c0e09200SDave Airlie 
521ed4cb414SEric Anholt 	i915_user_irq_get(dev);
522c0e09200SDave Airlie 	DRM_WAIT_ON(ret, dev_priv->irq_queue, 3 * DRM_HZ,
523c0e09200SDave Airlie 		    READ_BREADCRUMB(dev_priv) >= irq_nr);
524ed4cb414SEric Anholt 	i915_user_irq_put(dev);
525c0e09200SDave Airlie 
526c0e09200SDave Airlie 	if (ret == -EBUSY) {
527c0e09200SDave Airlie 		DRM_ERROR("EBUSY -- rec: %d emitted: %d\n",
528c0e09200SDave Airlie 			  READ_BREADCRUMB(dev_priv), (int)dev_priv->counter);
529c0e09200SDave Airlie 	}
530c0e09200SDave Airlie 
531c99b058fSKristian Høgsberg 	if (dev_priv->sarea_priv)
532c99b058fSKristian Høgsberg 		dev_priv->sarea_priv->last_dispatch =
533c99b058fSKristian Høgsberg 			READ_BREADCRUMB(dev_priv);
534c0e09200SDave Airlie 
535c0e09200SDave Airlie 	return ret;
536c0e09200SDave Airlie }
537c0e09200SDave Airlie 
538c0e09200SDave Airlie /* Needs the lock as it touches the ring.
539c0e09200SDave Airlie  */
540c0e09200SDave Airlie int i915_irq_emit(struct drm_device *dev, void *data,
541c0e09200SDave Airlie 			 struct drm_file *file_priv)
542c0e09200SDave Airlie {
543c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
544c0e09200SDave Airlie 	drm_i915_irq_emit_t *emit = data;
545c0e09200SDave Airlie 	int result;
546c0e09200SDave Airlie 
547546b0974SEric Anholt 	RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
548c0e09200SDave Airlie 
549c0e09200SDave Airlie 	if (!dev_priv) {
550c0e09200SDave Airlie 		DRM_ERROR("called with no initialization\n");
551c0e09200SDave Airlie 		return -EINVAL;
552c0e09200SDave Airlie 	}
553546b0974SEric Anholt 	mutex_lock(&dev->struct_mutex);
554c0e09200SDave Airlie 	result = i915_emit_irq(dev);
555546b0974SEric Anholt 	mutex_unlock(&dev->struct_mutex);
556c0e09200SDave Airlie 
557c0e09200SDave Airlie 	if (DRM_COPY_TO_USER(emit->irq_seq, &result, sizeof(int))) {
558c0e09200SDave Airlie 		DRM_ERROR("copy_to_user\n");
559c0e09200SDave Airlie 		return -EFAULT;
560c0e09200SDave Airlie 	}
561c0e09200SDave Airlie 
562c0e09200SDave Airlie 	return 0;
563c0e09200SDave Airlie }
564c0e09200SDave Airlie 
565c0e09200SDave Airlie /* Doesn't need the hardware lock.
566c0e09200SDave Airlie  */
567c0e09200SDave Airlie int i915_irq_wait(struct drm_device *dev, void *data,
568c0e09200SDave Airlie 			 struct drm_file *file_priv)
569c0e09200SDave Airlie {
570c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
571c0e09200SDave Airlie 	drm_i915_irq_wait_t *irqwait = data;
572c0e09200SDave Airlie 
573c0e09200SDave Airlie 	if (!dev_priv) {
574c0e09200SDave Airlie 		DRM_ERROR("called with no initialization\n");
575c0e09200SDave Airlie 		return -EINVAL;
576c0e09200SDave Airlie 	}
577c0e09200SDave Airlie 
578c0e09200SDave Airlie 	return i915_wait_irq(dev, irqwait->irq_seq);
579c0e09200SDave Airlie }
580c0e09200SDave Airlie 
5810a3e67a4SJesse Barnes int i915_enable_vblank(struct drm_device *dev, int plane)
5820a3e67a4SJesse Barnes {
5830a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
5840a3e67a4SJesse Barnes 	int pipe = i915_get_pipe(dev, plane);
5850a3e67a4SJesse Barnes 	u32	pipestat_reg = 0;
5860a3e67a4SJesse Barnes 	u32	pipestat;
5870a3e67a4SJesse Barnes 
5880a3e67a4SJesse Barnes 	switch (pipe) {
5890a3e67a4SJesse Barnes 	case 0:
5900a3e67a4SJesse Barnes 		pipestat_reg = PIPEASTAT;
5910a3e67a4SJesse Barnes 		i915_enable_irq(dev_priv, I915_DISPLAY_PIPE_A_EVENT_INTERRUPT);
5920a3e67a4SJesse Barnes 		break;
5930a3e67a4SJesse Barnes 	case 1:
5940a3e67a4SJesse Barnes 		pipestat_reg = PIPEBSTAT;
5950a3e67a4SJesse Barnes 		i915_enable_irq(dev_priv, I915_DISPLAY_PIPE_B_EVENT_INTERRUPT);
5960a3e67a4SJesse Barnes 		break;
5970a3e67a4SJesse Barnes 	default:
5980a3e67a4SJesse Barnes 		DRM_ERROR("tried to enable vblank on non-existent pipe %d\n",
5990a3e67a4SJesse Barnes 			  pipe);
6000a3e67a4SJesse Barnes 		break;
6010a3e67a4SJesse Barnes 	}
6020a3e67a4SJesse Barnes 
6030a3e67a4SJesse Barnes 	if (pipestat_reg) {
6040a3e67a4SJesse Barnes 		pipestat = I915_READ(pipestat_reg);
6050a3e67a4SJesse Barnes 		if (IS_I965G(dev))
6060a3e67a4SJesse Barnes 			pipestat |= PIPE_START_VBLANK_INTERRUPT_ENABLE;
6070a3e67a4SJesse Barnes 		else
6080a3e67a4SJesse Barnes 			pipestat |= PIPE_VBLANK_INTERRUPT_ENABLE;
6090a3e67a4SJesse Barnes 		/* Clear any stale interrupt status */
6100a3e67a4SJesse Barnes 		pipestat |= (PIPE_START_VBLANK_INTERRUPT_STATUS |
6110a3e67a4SJesse Barnes 			     PIPE_VBLANK_INTERRUPT_STATUS);
6120a3e67a4SJesse Barnes 		I915_WRITE(pipestat_reg, pipestat);
6130a3e67a4SJesse Barnes 	}
6140a3e67a4SJesse Barnes 
6150a3e67a4SJesse Barnes 	return 0;
6160a3e67a4SJesse Barnes }
6170a3e67a4SJesse Barnes 
6180a3e67a4SJesse Barnes void i915_disable_vblank(struct drm_device *dev, int plane)
6190a3e67a4SJesse Barnes {
6200a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
6210a3e67a4SJesse Barnes 	int pipe = i915_get_pipe(dev, plane);
6220a3e67a4SJesse Barnes 	u32	pipestat_reg = 0;
6230a3e67a4SJesse Barnes 	u32	pipestat;
6240a3e67a4SJesse Barnes 
6250a3e67a4SJesse Barnes 	switch (pipe) {
6260a3e67a4SJesse Barnes 	case 0:
6270a3e67a4SJesse Barnes 		pipestat_reg = PIPEASTAT;
6280a3e67a4SJesse Barnes 		i915_disable_irq(dev_priv, I915_DISPLAY_PIPE_A_EVENT_INTERRUPT);
6290a3e67a4SJesse Barnes 		break;
6300a3e67a4SJesse Barnes 	case 1:
6310a3e67a4SJesse Barnes 		pipestat_reg = PIPEBSTAT;
6320a3e67a4SJesse Barnes 		i915_disable_irq(dev_priv, I915_DISPLAY_PIPE_B_EVENT_INTERRUPT);
6330a3e67a4SJesse Barnes 		break;
6340a3e67a4SJesse Barnes 	default:
6350a3e67a4SJesse Barnes 		DRM_ERROR("tried to disable vblank on non-existent pipe %d\n",
6360a3e67a4SJesse Barnes 			  pipe);
6370a3e67a4SJesse Barnes 		break;
6380a3e67a4SJesse Barnes 	}
6390a3e67a4SJesse Barnes 
6400a3e67a4SJesse Barnes 	if (pipestat_reg) {
6410a3e67a4SJesse Barnes 		pipestat = I915_READ(pipestat_reg);
6420a3e67a4SJesse Barnes 		pipestat &= ~(PIPE_START_VBLANK_INTERRUPT_ENABLE |
6430a3e67a4SJesse Barnes 			      PIPE_VBLANK_INTERRUPT_ENABLE);
6440a3e67a4SJesse Barnes 		/* Clear any stale interrupt status */
6450a3e67a4SJesse Barnes 		pipestat |= (PIPE_START_VBLANK_INTERRUPT_STATUS |
6460a3e67a4SJesse Barnes 			     PIPE_VBLANK_INTERRUPT_STATUS);
6470a3e67a4SJesse Barnes 		I915_WRITE(pipestat_reg, pipestat);
6480a3e67a4SJesse Barnes 	}
6490a3e67a4SJesse Barnes }
6500a3e67a4SJesse Barnes 
651c0e09200SDave Airlie /* Set the vblank monitor pipe
652c0e09200SDave Airlie  */
653c0e09200SDave Airlie int i915_vblank_pipe_set(struct drm_device *dev, void *data,
654c0e09200SDave Airlie 			 struct drm_file *file_priv)
655c0e09200SDave Airlie {
656c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
657c0e09200SDave Airlie 
658c0e09200SDave Airlie 	if (!dev_priv) {
659c0e09200SDave Airlie 		DRM_ERROR("called with no initialization\n");
660c0e09200SDave Airlie 		return -EINVAL;
661c0e09200SDave Airlie 	}
662c0e09200SDave Airlie 
663c0e09200SDave Airlie 	return 0;
664c0e09200SDave Airlie }
665c0e09200SDave Airlie 
666c0e09200SDave Airlie int i915_vblank_pipe_get(struct drm_device *dev, void *data,
667c0e09200SDave Airlie 			 struct drm_file *file_priv)
668c0e09200SDave Airlie {
669c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
670c0e09200SDave Airlie 	drm_i915_vblank_pipe_t *pipe = data;
671c0e09200SDave Airlie 
672c0e09200SDave Airlie 	if (!dev_priv) {
673c0e09200SDave Airlie 		DRM_ERROR("called with no initialization\n");
674c0e09200SDave Airlie 		return -EINVAL;
675c0e09200SDave Airlie 	}
676c0e09200SDave Airlie 
6770a3e67a4SJesse Barnes 	pipe->pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
678c0e09200SDave Airlie 
679c0e09200SDave Airlie 	return 0;
680c0e09200SDave Airlie }
681c0e09200SDave Airlie 
682c0e09200SDave Airlie /**
683c0e09200SDave Airlie  * Schedule buffer swap at given vertical blank.
684c0e09200SDave Airlie  */
685c0e09200SDave Airlie int i915_vblank_swap(struct drm_device *dev, void *data,
686c0e09200SDave Airlie 		     struct drm_file *file_priv)
687c0e09200SDave Airlie {
688c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
689c0e09200SDave Airlie 	drm_i915_vblank_swap_t *swap = data;
690c0e09200SDave Airlie 	drm_i915_vbl_swap_t *vbl_swap;
6910a3e67a4SJesse Barnes 	unsigned int pipe, seqtype, curseq, plane;
692c0e09200SDave Airlie 	unsigned long irqflags;
693c0e09200SDave Airlie 	struct list_head *list;
6940a3e67a4SJesse Barnes 	int ret;
695c0e09200SDave Airlie 
696c99b058fSKristian Høgsberg 	if (!dev_priv || !dev_priv->sarea_priv) {
697c0e09200SDave Airlie 		DRM_ERROR("%s called with no initialization\n", __func__);
698c0e09200SDave Airlie 		return -EINVAL;
699c0e09200SDave Airlie 	}
700c0e09200SDave Airlie 
701c0e09200SDave Airlie 	if (dev_priv->sarea_priv->rotation) {
702c0e09200SDave Airlie 		DRM_DEBUG("Rotation not supported\n");
703c0e09200SDave Airlie 		return -EINVAL;
704c0e09200SDave Airlie 	}
705c0e09200SDave Airlie 
706c0e09200SDave Airlie 	if (swap->seqtype & ~(_DRM_VBLANK_RELATIVE | _DRM_VBLANK_ABSOLUTE |
707c0e09200SDave Airlie 			     _DRM_VBLANK_SECONDARY | _DRM_VBLANK_NEXTONMISS)) {
708c0e09200SDave Airlie 		DRM_ERROR("Invalid sequence type 0x%x\n", swap->seqtype);
709c0e09200SDave Airlie 		return -EINVAL;
710c0e09200SDave Airlie 	}
711c0e09200SDave Airlie 
7120a3e67a4SJesse Barnes 	plane = (swap->seqtype & _DRM_VBLANK_SECONDARY) ? 1 : 0;
7130a3e67a4SJesse Barnes 	pipe = i915_get_pipe(dev, plane);
714c0e09200SDave Airlie 
715c0e09200SDave Airlie 	seqtype = swap->seqtype & (_DRM_VBLANK_RELATIVE | _DRM_VBLANK_ABSOLUTE);
716c0e09200SDave Airlie 
717c0e09200SDave Airlie 	if (!(dev_priv->vblank_pipe & (1 << pipe))) {
718c0e09200SDave Airlie 		DRM_ERROR("Invalid pipe %d\n", pipe);
719c0e09200SDave Airlie 		return -EINVAL;
720c0e09200SDave Airlie 	}
721c0e09200SDave Airlie 
722c0e09200SDave Airlie 	spin_lock_irqsave(&dev->drw_lock, irqflags);
723c0e09200SDave Airlie 
724c0e09200SDave Airlie 	if (!drm_get_drawable_info(dev, swap->drawable)) {
725c0e09200SDave Airlie 		spin_unlock_irqrestore(&dev->drw_lock, irqflags);
726c0e09200SDave Airlie 		DRM_DEBUG("Invalid drawable ID %d\n", swap->drawable);
727c0e09200SDave Airlie 		return -EINVAL;
728c0e09200SDave Airlie 	}
729c0e09200SDave Airlie 
730c0e09200SDave Airlie 	spin_unlock_irqrestore(&dev->drw_lock, irqflags);
731c0e09200SDave Airlie 
7320a3e67a4SJesse Barnes 	/*
7330a3e67a4SJesse Barnes 	 * We take the ref here and put it when the swap actually completes
7340a3e67a4SJesse Barnes 	 * in the tasklet.
7350a3e67a4SJesse Barnes 	 */
7360a3e67a4SJesse Barnes 	ret = drm_vblank_get(dev, pipe);
7370a3e67a4SJesse Barnes 	if (ret)
7380a3e67a4SJesse Barnes 		return ret;
7390a3e67a4SJesse Barnes 	curseq = drm_vblank_count(dev, pipe);
740c0e09200SDave Airlie 
741c0e09200SDave Airlie 	if (seqtype == _DRM_VBLANK_RELATIVE)
742c0e09200SDave Airlie 		swap->sequence += curseq;
743c0e09200SDave Airlie 
744c0e09200SDave Airlie 	if ((curseq - swap->sequence) <= (1<<23)) {
745c0e09200SDave Airlie 		if (swap->seqtype & _DRM_VBLANK_NEXTONMISS) {
746c0e09200SDave Airlie 			swap->sequence = curseq + 1;
747c0e09200SDave Airlie 		} else {
748c0e09200SDave Airlie 			DRM_DEBUG("Missed target sequence\n");
7490a3e67a4SJesse Barnes 			drm_vblank_put(dev, pipe);
750c0e09200SDave Airlie 			return -EINVAL;
751c0e09200SDave Airlie 		}
752c0e09200SDave Airlie 	}
753c0e09200SDave Airlie 
754c0e09200SDave Airlie 	spin_lock_irqsave(&dev_priv->swaps_lock, irqflags);
755c0e09200SDave Airlie 
756c0e09200SDave Airlie 	list_for_each(list, &dev_priv->vbl_swaps.head) {
757c0e09200SDave Airlie 		vbl_swap = list_entry(list, drm_i915_vbl_swap_t, head);
758c0e09200SDave Airlie 
759c0e09200SDave Airlie 		if (vbl_swap->drw_id == swap->drawable &&
7600a3e67a4SJesse Barnes 		    vbl_swap->plane == plane &&
761c0e09200SDave Airlie 		    vbl_swap->sequence == swap->sequence) {
762c0e09200SDave Airlie 			spin_unlock_irqrestore(&dev_priv->swaps_lock, irqflags);
763c0e09200SDave Airlie 			DRM_DEBUG("Already scheduled\n");
764c0e09200SDave Airlie 			return 0;
765c0e09200SDave Airlie 		}
766c0e09200SDave Airlie 	}
767c0e09200SDave Airlie 
768c0e09200SDave Airlie 	spin_unlock_irqrestore(&dev_priv->swaps_lock, irqflags);
769c0e09200SDave Airlie 
770c0e09200SDave Airlie 	if (dev_priv->swaps_pending >= 100) {
771c0e09200SDave Airlie 		DRM_DEBUG("Too many swaps queued\n");
7720a3e67a4SJesse Barnes 		drm_vblank_put(dev, pipe);
773c0e09200SDave Airlie 		return -EBUSY;
774c0e09200SDave Airlie 	}
775c0e09200SDave Airlie 
776c0e09200SDave Airlie 	vbl_swap = drm_calloc(1, sizeof(*vbl_swap), DRM_MEM_DRIVER);
777c0e09200SDave Airlie 
778c0e09200SDave Airlie 	if (!vbl_swap) {
779c0e09200SDave Airlie 		DRM_ERROR("Failed to allocate memory to queue swap\n");
7800a3e67a4SJesse Barnes 		drm_vblank_put(dev, pipe);
781c0e09200SDave Airlie 		return -ENOMEM;
782c0e09200SDave Airlie 	}
783c0e09200SDave Airlie 
784c0e09200SDave Airlie 	DRM_DEBUG("\n");
785c0e09200SDave Airlie 
786c0e09200SDave Airlie 	vbl_swap->drw_id = swap->drawable;
7870a3e67a4SJesse Barnes 	vbl_swap->plane = plane;
788c0e09200SDave Airlie 	vbl_swap->sequence = swap->sequence;
789c0e09200SDave Airlie 
790c0e09200SDave Airlie 	spin_lock_irqsave(&dev_priv->swaps_lock, irqflags);
791c0e09200SDave Airlie 
792c0e09200SDave Airlie 	list_add_tail(&vbl_swap->head, &dev_priv->vbl_swaps.head);
793c0e09200SDave Airlie 	dev_priv->swaps_pending++;
794c0e09200SDave Airlie 
795c0e09200SDave Airlie 	spin_unlock_irqrestore(&dev_priv->swaps_lock, irqflags);
796c0e09200SDave Airlie 
797c0e09200SDave Airlie 	return 0;
798c0e09200SDave Airlie }
799c0e09200SDave Airlie 
800c0e09200SDave Airlie /* drm_dma.h hooks
801c0e09200SDave Airlie */
802c0e09200SDave Airlie void i915_driver_irq_preinstall(struct drm_device * dev)
803c0e09200SDave Airlie {
804c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
805c0e09200SDave Airlie 
8060a3e67a4SJesse Barnes 	I915_WRITE(HWSTAM, 0xeffe);
8070a3e67a4SJesse Barnes 	I915_WRITE(IMR, 0xffffffff);
808ed4cb414SEric Anholt 	I915_WRITE(IER, 0x0);
809c0e09200SDave Airlie }
810c0e09200SDave Airlie 
8110a3e67a4SJesse Barnes int i915_driver_irq_postinstall(struct drm_device *dev)
812c0e09200SDave Airlie {
813c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
8140a3e67a4SJesse Barnes 	int ret, num_pipes = 2;
815c0e09200SDave Airlie 
816c0e09200SDave Airlie 	spin_lock_init(&dev_priv->swaps_lock);
817c0e09200SDave Airlie 	INIT_LIST_HEAD(&dev_priv->vbl_swaps.head);
818c0e09200SDave Airlie 	dev_priv->swaps_pending = 0;
819c0e09200SDave Airlie 
820ed4cb414SEric Anholt 	/* Set initial unmasked IRQs to just the selected vblank pipes. */
821ed4cb414SEric Anholt 	dev_priv->irq_mask_reg = ~0;
8220a3e67a4SJesse Barnes 
8230a3e67a4SJesse Barnes 	ret = drm_vblank_init(dev, num_pipes);
8240a3e67a4SJesse Barnes 	if (ret)
8250a3e67a4SJesse Barnes 		return ret;
8260a3e67a4SJesse Barnes 
8270a3e67a4SJesse Barnes 	dev_priv->vblank_pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
828ed4cb414SEric Anholt 	dev_priv->irq_mask_reg &= ~I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT;
829ed4cb414SEric Anholt 	dev_priv->irq_mask_reg &= ~I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
830ed4cb414SEric Anholt 
8310a3e67a4SJesse Barnes 	dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
8320a3e67a4SJesse Barnes 
8338ee1c3dbSMatthew Garrett 	dev_priv->irq_mask_reg &= I915_INTERRUPT_ENABLE_MASK;
8348ee1c3dbSMatthew Garrett 
835ed4cb414SEric Anholt 	I915_WRITE(IMR, dev_priv->irq_mask_reg);
836ed4cb414SEric Anholt 	I915_WRITE(IER, I915_INTERRUPT_ENABLE_MASK);
837ed4cb414SEric Anholt 	(void) I915_READ(IER);
838ed4cb414SEric Anholt 
8398ee1c3dbSMatthew Garrett 	opregion_enable_asle(dev);
840c0e09200SDave Airlie 	DRM_INIT_WAITQUEUE(&dev_priv->irq_queue);
8410a3e67a4SJesse Barnes 
8420a3e67a4SJesse Barnes 	return 0;
843c0e09200SDave Airlie }
844c0e09200SDave Airlie 
845c0e09200SDave Airlie void i915_driver_irq_uninstall(struct drm_device * dev)
846c0e09200SDave Airlie {
847c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
8480a3e67a4SJesse Barnes 	u32 temp;
849c0e09200SDave Airlie 
850c0e09200SDave Airlie 	if (!dev_priv)
851c0e09200SDave Airlie 		return;
852c0e09200SDave Airlie 
8530a3e67a4SJesse Barnes 	dev_priv->vblank_pipe = 0;
8540a3e67a4SJesse Barnes 
8550a3e67a4SJesse Barnes 	I915_WRITE(HWSTAM, 0xffffffff);
8560a3e67a4SJesse Barnes 	I915_WRITE(IMR, 0xffffffff);
857ed4cb414SEric Anholt 	I915_WRITE(IER, 0x0);
858c0e09200SDave Airlie 
8590a3e67a4SJesse Barnes 	temp = I915_READ(PIPEASTAT);
8600a3e67a4SJesse Barnes 	I915_WRITE(PIPEASTAT, temp);
8610a3e67a4SJesse Barnes 	temp = I915_READ(PIPEBSTAT);
8620a3e67a4SJesse Barnes 	I915_WRITE(PIPEBSTAT, temp);
863ed4cb414SEric Anholt 	temp = I915_READ(IIR);
864ed4cb414SEric Anholt 	I915_WRITE(IIR, temp);
865c0e09200SDave Airlie }
866