]> Pileus Git - ~andy/linux/blob - drivers/staging/gma500/accel_2d.c
gma500: The 2D code is now also device independent
[~andy/linux] / drivers / staging / gma500 / accel_2d.c
1 /**************************************************************************
2  * Copyright (c) 2007-2011, Intel Corporation.
3  * All Rights Reserved.
4  *
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.
8  *
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.
13  *
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.
17  *
18  * Intel funded Tungsten Graphics (http://www.tungstengraphics.com) to
19  * develop this driver.
20  *
21  **************************************************************************/
22
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>
34
35 #include <drm/drmP.h>
36 #include <drm/drm.h>
37 #include <drm/drm_crtc.h>
38
39 #include "psb_drv.h"
40 #include "psb_reg.h"
41 #include "framebuffer.h"
42
43 /**
44  *      psb_spank               -       reset the 2D engine
45  *      @dev_priv: our PSB DRM device
46  *
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
50  */
51 void psb_spank(struct drm_psb_private *dev_priv)
52 {
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);
58
59         msleep(1);
60
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);
67
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->pg->gatt_start, PSB_CR_BIF_TWOD_REQ_BASE);
73 }
74
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
79  *
80  *      Wait until there is room to load the FIFO with our data. If the
81  *      device is not responding then reset it
82  */
83 static int psb_2d_wait_available(struct drm_psb_private *dev_priv,
84                           unsigned size)
85 {
86         uint32_t avail = PSB_RSGX32(PSB_CR_2D_SOCIF);
87         unsigned long t = jiffies + HZ;
88
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;
94                 }
95         }
96         return 0;
97 }
98
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)
104  *
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
107  */
108 int psbfb_2d_submit(struct drm_psb_private *dev_priv, uint32_t *cmdbuf,
109                                                                 unsigned size)
110 {
111         int ret = 0;
112         int i;
113         unsigned submit_size;
114
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;
121
122                 submit_size <<= 2;
123
124                 for (i = 0; i < submit_size; i += 4)
125                         PSB_WSGX32(*cmdbuf++, PSB_SGX_2D_SLAVE_PORT + i);
126
127                 (void)PSB_RSGX32(PSB_SGX_2D_SLAVE_PORT + i - 4);
128         }
129         return 0;
130 }
131
132
133 /**
134  *      psb_accel_2d_copy_direction     -       compute blit order
135  *      @xdir: X direction of move
136  *      @ydir: Y direction of move
137  *
138  *      Compute the correct order setings to ensure that an overlapping blit
139  *      correctly copies all the pixels.
140  */
141 static u32 psb_accel_2d_copy_direction(int xdir, int ydir)
142 {
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;
149 }
150
151 /**
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
166  *
167  *      Format and issue a 2D accelerated copy command.
168  */
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)
176 {
177         uint32_t blit_cmd;
178         uint32_t buffer[10];
179         uint32_t *buf;
180         uint32_t direction;
181
182         buf = buffer;
183
184         direction =
185             psb_accel_2d_copy_direction(src_x - dst_x, src_y - dst_y);
186
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;
191         }
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;
196         }
197
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;
204
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;
225
226         return psbfb_2d_submit(dev_priv, buffer, buf - buffer);
227 }
228
229 /**
230  *      psbfb_copyarea_accel    -       copyarea acceleration for /dev/fb
231  *      @info: our framebuffer
232  *      @a: copyarea parameters from the framebuffer core
233  *
234  *      Perform a 2D copy via the accelerator
235  */
236 static void psbfb_copyarea_accel(struct fb_info *info,
237                                  const struct fb_copyarea *a)
238 {
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;
248
249         if (!fb)
250                 return;
251
252         offset = psbfb->gtt->offset;
253         stride = fb->pitch;
254
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;
278         }
279
280         if (!gma_power_begin(dev, false)) {
281                 cfb_copyarea(info, a);
282                 return;
283         }
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);
289 }
290
291 /**
292  *      psbfb_copyarea  -       2D copy interface
293  *      @info: our framebuffer
294  *      @region: region to copy
295  *
296  *      Copy an area of the framebuffer console either by the accelerator
297  *      or directly using the cfb helpers according to the request
298  */
299 void psbfb_copyarea(struct fb_info *info,
300                            const struct fb_copyarea *region)
301 {
302         if (unlikely(info->state != FBINFO_STATE_RUNNING))
303                 return;
304
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);
309
310         psbfb_copyarea_accel(info, region);
311 }
312
313 /**
314  *      psbfb_sync      -       synchronize 2D
315  *      @info: our framebuffer
316  *
317  *      Wait for the 2D engine to quiesce so that we can do CPU
318  *      access to the framebuffer again
319  */
320 int psbfb_sync(struct fb_info *info)
321 {
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;
328
329         /*
330          * First idle the 2D engine.
331          */
332
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;
336
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));
341
342         if (busy)
343                 busy = (PSB_RSGX32(PSB_CR_2D_SOCIF) != _PSB_C2_SOCIF_EMPTY);
344         if (busy)
345                 goto out;
346
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);
355
356 out:
357         return (busy) ? -EBUSY : 0;
358 }