gma500: don't dynamically allocate the psb_gtt struct
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / staging / gma500 / accel_2d.c
blobc719017e9db3f0f6c4abdabf6c1b92dd0d8c23a0
1 /**************************************************************************
2 * Copyright (c) 2007-2011, Intel Corporation.
3 * All Rights Reserved.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
9 * This program is distributed in the hope 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.
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
18 * Intel funded Tungsten Graphics (http://www.tungstengraphics.com) to
19 * develop this driver.
21 **************************************************************************/
23 #include <linux/module.h>
24 #include <linux/kernel.h>
25 #include <linux/errno.h>
26 #include <linux/string.h>
27 #include <linux/mm.h>
28 #include <linux/tty.h>
29 #include <linux/slab.h>
30 #include <linux/delay.h>
31 #include <linux/fb.h>
32 #include <linux/init.h>
33 #include <linux/console.h>
35 #include <drm/drmP.h>
36 #include <drm/drm.h>
37 #include <drm/drm_crtc.h>
39 #include "psb_drv.h"
40 #include "psb_reg.h"
41 #include "framebuffer.h"
43 /**
44 * psb_spank - reset the 2D engine
45 * @dev_priv: our PSB DRM device
47 * Soft reset the graphics engine and then reload the necessary registers.
48 * We use this at initialisation time but it will become relevant for
49 * accelerated X later
51 void psb_spank(struct drm_psb_private *dev_priv)
53 PSB_WSGX32(_PSB_CS_RESET_BIF_RESET | _PSB_CS_RESET_DPM_RESET |
54 _PSB_CS_RESET_TA_RESET | _PSB_CS_RESET_USE_RESET |
55 _PSB_CS_RESET_ISP_RESET | _PSB_CS_RESET_TSP_RESET |
56 _PSB_CS_RESET_TWOD_RESET, PSB_CR_SOFT_RESET);
57 PSB_RSGX32(PSB_CR_SOFT_RESET);
59 msleep(1);
61 PSB_WSGX32(0, PSB_CR_SOFT_RESET);
62 wmb();
63 PSB_WSGX32(PSB_RSGX32(PSB_CR_BIF_CTRL) | _PSB_CB_CTRL_CLEAR_FAULT,
64 PSB_CR_BIF_CTRL);
65 wmb();
66 (void) PSB_RSGX32(PSB_CR_BIF_CTRL);
68 msleep(1);
69 PSB_WSGX32(PSB_RSGX32(PSB_CR_BIF_CTRL) & ~_PSB_CB_CTRL_CLEAR_FAULT,
70 PSB_CR_BIF_CTRL);
71 (void) PSB_RSGX32(PSB_CR_BIF_CTRL);
72 PSB_WSGX32(dev_priv->gtt.gatt_start, PSB_CR_BIF_TWOD_REQ_BASE);
75 /**
76 * psb2_2d_wait_available - wait for FIFO room
77 * @dev_priv: our DRM device
78 * @size: size (in dwords) of the command we want to issue
80 * Wait until there is room to load the FIFO with our data. If the
81 * device is not responding then reset it
83 static int psb_2d_wait_available(struct drm_psb_private *dev_priv,
84 unsigned size)
86 uint32_t avail = PSB_RSGX32(PSB_CR_2D_SOCIF);
87 unsigned long t = jiffies + HZ;
89 while (avail < size) {
90 avail = PSB_RSGX32(PSB_CR_2D_SOCIF);
91 if (time_after(jiffies, t)) {
92 psb_spank(dev_priv);
93 return -EIO;
96 return 0;
99 /**
100 * psb_2d_submit - submit a 2D command
101 * @dev_priv: our DRM device
102 * @cmdbuf: command to issue
103 * @size: length (in dwords)
105 * Issue one or more 2D commands to the accelerator. This needs to be
106 * serialized later when we add the GEM interfaces for acceleration
108 int psbfb_2d_submit(struct drm_psb_private *dev_priv, uint32_t *cmdbuf,
109 unsigned size)
111 int ret = 0;
112 int i;
113 unsigned submit_size;
115 while (size > 0) {
116 submit_size = (size < 0x60) ? size : 0x60;
117 size -= submit_size;
118 ret = psb_2d_wait_available(dev_priv, submit_size);
119 if (ret)
120 return ret;
122 submit_size <<= 2;
124 for (i = 0; i < submit_size; i += 4)
125 PSB_WSGX32(*cmdbuf++, PSB_SGX_2D_SLAVE_PORT + i);
127 (void)PSB_RSGX32(PSB_SGX_2D_SLAVE_PORT + i - 4);
129 return 0;
134 * psb_accel_2d_copy_direction - compute blit order
135 * @xdir: X direction of move
136 * @ydir: Y direction of move
138 * Compute the correct order setings to ensure that an overlapping blit
139 * correctly copies all the pixels.
141 static u32 psb_accel_2d_copy_direction(int xdir, int ydir)
143 if (xdir < 0)
144 return (ydir < 0) ? PSB_2D_COPYORDER_BR2TL :
145 PSB_2D_COPYORDER_TR2BL;
146 else
147 return (ydir < 0) ? PSB_2D_COPYORDER_BL2TR :
148 PSB_2D_COPYORDER_TL2BR;
152 * psb_accel_2d_copy - accelerated 2D copy
153 * @dev_priv: our DRM device
154 * @src_offset in bytes
155 * @src_stride in bytes
156 * @src_format psb 2D format defines
157 * @dst_offset in bytes
158 * @dst_stride in bytes
159 * @dst_format psb 2D format defines
160 * @src_x offset in pixels
161 * @src_y offset in pixels
162 * @dst_x offset in pixels
163 * @dst_y offset in pixels
164 * @size_x of the copied area
165 * @size_y of the copied area
167 * Format and issue a 2D accelerated copy command.
169 static int psb_accel_2d_copy(struct drm_psb_private *dev_priv,
170 uint32_t src_offset, uint32_t src_stride,
171 uint32_t src_format, uint32_t dst_offset,
172 uint32_t dst_stride, uint32_t dst_format,
173 uint16_t src_x, uint16_t src_y,
174 uint16_t dst_x, uint16_t dst_y,
175 uint16_t size_x, uint16_t size_y)
177 uint32_t blit_cmd;
178 uint32_t buffer[10];
179 uint32_t *buf;
180 uint32_t direction;
182 buf = buffer;
184 direction =
185 psb_accel_2d_copy_direction(src_x - dst_x, src_y - dst_y);
187 if (direction == PSB_2D_COPYORDER_BR2TL ||
188 direction == PSB_2D_COPYORDER_TR2BL) {
189 src_x += size_x - 1;
190 dst_x += size_x - 1;
192 if (direction == PSB_2D_COPYORDER_BR2TL ||
193 direction == PSB_2D_COPYORDER_BL2TR) {
194 src_y += size_y - 1;
195 dst_y += size_y - 1;
198 blit_cmd =
199 PSB_2D_BLIT_BH |
200 PSB_2D_ROT_NONE |
201 PSB_2D_DSTCK_DISABLE |
202 PSB_2D_SRCCK_DISABLE |
203 PSB_2D_USE_PAT | PSB_2D_ROP3_SRCCOPY | direction;
205 *buf++ = PSB_2D_FENCE_BH;
206 *buf++ =
207 PSB_2D_DST_SURF_BH | dst_format | (dst_stride <<
208 PSB_2D_DST_STRIDE_SHIFT);
209 *buf++ = dst_offset;
210 *buf++ =
211 PSB_2D_SRC_SURF_BH | src_format | (src_stride <<
212 PSB_2D_SRC_STRIDE_SHIFT);
213 *buf++ = src_offset;
214 *buf++ =
215 PSB_2D_SRC_OFF_BH | (src_x << PSB_2D_SRCOFF_XSTART_SHIFT) |
216 (src_y << PSB_2D_SRCOFF_YSTART_SHIFT);
217 *buf++ = blit_cmd;
218 *buf++ =
219 (dst_x << PSB_2D_DST_XSTART_SHIFT) | (dst_y <<
220 PSB_2D_DST_YSTART_SHIFT);
221 *buf++ =
222 (size_x << PSB_2D_DST_XSIZE_SHIFT) | (size_y <<
223 PSB_2D_DST_YSIZE_SHIFT);
224 *buf++ = PSB_2D_FLUSH_BH;
226 return psbfb_2d_submit(dev_priv, buffer, buf - buffer);
230 * psbfb_copyarea_accel - copyarea acceleration for /dev/fb
231 * @info: our framebuffer
232 * @a: copyarea parameters from the framebuffer core
234 * Perform a 2D copy via the accelerator
236 static void psbfb_copyarea_accel(struct fb_info *info,
237 const struct fb_copyarea *a)
239 struct psb_fbdev *fbdev = info->par;
240 struct psb_framebuffer *psbfb = &fbdev->pfb;
241 struct drm_device *dev = psbfb->base.dev;
242 struct drm_framebuffer *fb = fbdev->psb_fb_helper.fb;
243 struct drm_psb_private *dev_priv = dev->dev_private;
244 uint32_t offset;
245 uint32_t stride;
246 uint32_t src_format;
247 uint32_t dst_format;
249 if (!fb)
250 return;
252 offset = psbfb->gtt->offset;
253 stride = fb->pitch;
255 switch (fb->depth) {
256 case 8:
257 src_format = PSB_2D_SRC_332RGB;
258 dst_format = PSB_2D_DST_332RGB;
259 break;
260 case 15:
261 src_format = PSB_2D_SRC_555RGB;
262 dst_format = PSB_2D_DST_555RGB;
263 break;
264 case 16:
265 src_format = PSB_2D_SRC_565RGB;
266 dst_format = PSB_2D_DST_565RGB;
267 break;
268 case 24:
269 case 32:
270 /* this is wrong but since we don't do blending its okay */
271 src_format = PSB_2D_SRC_8888ARGB;
272 dst_format = PSB_2D_DST_8888ARGB;
273 break;
274 default:
275 /* software fallback */
276 cfb_copyarea(info, a);
277 return;
280 if (!gma_power_begin(dev, false)) {
281 cfb_copyarea(info, a);
282 return;
284 psb_accel_2d_copy(dev_priv,
285 offset, stride, src_format,
286 offset, stride, dst_format,
287 a->sx, a->sy, a->dx, a->dy, a->width, a->height);
288 gma_power_end(dev);
292 * psbfb_copyarea - 2D copy interface
293 * @info: our framebuffer
294 * @region: region to copy
296 * Copy an area of the framebuffer console either by the accelerator
297 * or directly using the cfb helpers according to the request
299 void psbfb_copyarea(struct fb_info *info,
300 const struct fb_copyarea *region)
302 if (unlikely(info->state != FBINFO_STATE_RUNNING))
303 return;
305 /* Avoid the 8 pixel erratum */
306 if (region->width == 8 || region->height == 8 ||
307 (info->flags & FBINFO_HWACCEL_DISABLED))
308 return cfb_copyarea(info, region);
310 psbfb_copyarea_accel(info, region);
314 * psbfb_sync - synchronize 2D
315 * @info: our framebuffer
317 * Wait for the 2D engine to quiesce so that we can do CPU
318 * access to the framebuffer again
320 int psbfb_sync(struct fb_info *info)
322 struct psb_fbdev *fbdev = info->par;
323 struct psb_framebuffer *psbfb = &fbdev->pfb;
324 struct drm_device *dev = psbfb->base.dev;
325 struct drm_psb_private *dev_priv = dev->dev_private;
326 unsigned long _end = jiffies + DRM_HZ;
327 int busy = 0;
330 * First idle the 2D engine.
333 if ((PSB_RSGX32(PSB_CR_2D_SOCIF) == _PSB_C2_SOCIF_EMPTY) &&
334 ((PSB_RSGX32(PSB_CR_2D_BLIT_STATUS) & _PSB_C2B_STATUS_BUSY) == 0))
335 goto out;
337 do {
338 busy = (PSB_RSGX32(PSB_CR_2D_SOCIF) != _PSB_C2_SOCIF_EMPTY);
339 cpu_relax();
340 } while (busy && !time_after_eq(jiffies, _end));
342 if (busy)
343 busy = (PSB_RSGX32(PSB_CR_2D_SOCIF) != _PSB_C2_SOCIF_EMPTY);
344 if (busy)
345 goto out;
347 do {
348 busy = ((PSB_RSGX32(PSB_CR_2D_BLIT_STATUS) &
349 _PSB_C2B_STATUS_BUSY) != 0);
350 cpu_relax();
351 } while (busy && !time_after_eq(jiffies, _end));
352 if (busy)
353 busy = ((PSB_RSGX32(PSB_CR_2D_BLIT_STATUS) &
354 _PSB_C2B_STATUS_BUSY) != 0);
356 out:
357 return (busy) ? -EBUSY : 0;