gma500: polish for completion of this phase
[linux-2.6.git] / drivers / staging / gma500 / psb_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 "psb_drv.h"
42 #include "psb_fb.h"
43
44 void psb_spank(struct drm_psb_private *dev_priv)
45 {
46         PSB_WSGX32(_PSB_CS_RESET_BIF_RESET | _PSB_CS_RESET_DPM_RESET |
47                 _PSB_CS_RESET_TA_RESET | _PSB_CS_RESET_USE_RESET |
48                 _PSB_CS_RESET_ISP_RESET | _PSB_CS_RESET_TSP_RESET |
49                 _PSB_CS_RESET_TWOD_RESET, PSB_CR_SOFT_RESET);
50         (void) PSB_RSGX32(PSB_CR_SOFT_RESET);
51
52         msleep(1);
53
54         PSB_WSGX32(0, PSB_CR_SOFT_RESET);
55         wmb();
56         PSB_WSGX32(PSB_RSGX32(PSB_CR_BIF_CTRL) | _PSB_CB_CTRL_CLEAR_FAULT,
57                    PSB_CR_BIF_CTRL);
58         wmb();
59         (void) PSB_RSGX32(PSB_CR_BIF_CTRL);
60
61         msleep(1);
62         PSB_WSGX32(PSB_RSGX32(PSB_CR_BIF_CTRL) & ~_PSB_CB_CTRL_CLEAR_FAULT,
63                    PSB_CR_BIF_CTRL);
64         (void) PSB_RSGX32(PSB_CR_BIF_CTRL);
65         PSB_WSGX32(dev_priv->pg->gatt_start, PSB_CR_BIF_TWOD_REQ_BASE);
66 }
67
68 static int psb_2d_wait_available(struct drm_psb_private *dev_priv,
69                           unsigned size)
70 {
71         uint32_t avail = PSB_RSGX32(PSB_CR_2D_SOCIF);
72         unsigned long t = jiffies + HZ;
73
74         while(avail < size) {
75                 avail = PSB_RSGX32(PSB_CR_2D_SOCIF);
76                 if (time_after(jiffies, t)) {
77                         psb_spank(dev_priv);
78                         return -EIO;
79                 }
80         }
81         return 0;
82 }
83
84 /* FIXME: Remember if we expose the 2D engine to the DRM we need to serialize
85    it with console use */
86
87 int psbfb_2d_submit(struct drm_psb_private *dev_priv, uint32_t *cmdbuf,
88                            unsigned size)
89 {
90         int ret = 0;
91         int i;
92         unsigned submit_size;
93
94         while (size > 0) {
95                 submit_size = (size < 0x60) ? size : 0x60;
96                 size -= submit_size;
97                 ret = psb_2d_wait_available(dev_priv, submit_size);
98                 if (ret)
99                         return ret;
100
101                 submit_size <<= 2;
102                 for (i = 0; i < submit_size; i += 4) {
103                         PSB_WSGX32(*cmdbuf++, PSB_SGX_2D_SLAVE_PORT + i);
104                 }
105                 (void)PSB_RSGX32(PSB_SGX_2D_SLAVE_PORT + i - 4);
106         }
107         return 0;
108 }
109
110 static int psb_accel_2d_fillrect(struct drm_psb_private *dev_priv,
111                                  uint32_t dst_offset, uint32_t dst_stride,
112                                  uint32_t dst_format, uint16_t dst_x,
113                                  uint16_t dst_y, uint16_t size_x,
114                                  uint16_t size_y, uint32_t fill)
115 {
116         uint32_t buffer[10];
117         uint32_t *buf;
118
119         buf = buffer;
120
121         *buf++ = PSB_2D_FENCE_BH;
122
123         *buf++ =
124             PSB_2D_DST_SURF_BH | dst_format | (dst_stride <<
125                                                PSB_2D_DST_STRIDE_SHIFT);
126         *buf++ = dst_offset;
127
128         *buf++ =
129             PSB_2D_BLIT_BH |
130             PSB_2D_ROT_NONE |
131             PSB_2D_COPYORDER_TL2BR |
132             PSB_2D_DSTCK_DISABLE |
133             PSB_2D_SRCCK_DISABLE | PSB_2D_USE_FILL | PSB_2D_ROP3_PATCOPY;
134
135         *buf++ = fill << PSB_2D_FILLCOLOUR_SHIFT;
136         *buf++ =
137             (dst_x << PSB_2D_DST_XSTART_SHIFT) | (dst_y <<
138                                                   PSB_2D_DST_YSTART_SHIFT);
139         *buf++ =
140             (size_x << PSB_2D_DST_XSIZE_SHIFT) | (size_y <<
141                                                   PSB_2D_DST_YSIZE_SHIFT);
142         *buf++ = PSB_2D_FLUSH_BH;
143
144         return psbfb_2d_submit(dev_priv, buffer, buf - buffer);
145 }
146
147 static void psbfb_fillrect_accel(struct fb_info *info,
148                                  const struct fb_fillrect *r)
149 {
150         struct psb_fbdev *fbdev = info->par;
151         struct psb_framebuffer *psbfb = &fbdev->pfb;
152         struct drm_device *dev = psbfb->base.dev;
153         struct drm_framebuffer *fb = fbdev->psb_fb_helper.fb;
154         struct drm_psb_private *dev_priv = dev->dev_private;
155
156         uint32_t offset;
157         uint32_t stride;
158         uint32_t format;
159
160         if (!fb)
161                 return;
162
163         offset = psbfb->gtt->offset;
164         stride = fb->pitch;
165
166         switch (fb->depth) {
167         case 8:
168                 format = PSB_2D_DST_332RGB;
169                 break;
170         case 15:
171                 format = PSB_2D_DST_555RGB;
172                 break;
173         case 16:
174                 format = PSB_2D_DST_565RGB;
175                 break;
176         case 24:
177         case 32:
178                 /* this is wrong but since we don't do blending its okay */
179                 format = PSB_2D_DST_8888ARGB;
180                 break;
181         default:
182                 /* software fallback */
183                 cfb_fillrect(info, r);
184                 return;
185         }
186
187         psb_accel_2d_fillrect(dev_priv,
188                               offset, stride, format,
189                               r->dx, r->dy, r->width, r->height, r->color);
190 }
191
192 void psbfb_fillrect(struct fb_info *info,
193                            const struct fb_fillrect *rect)
194 {
195         if (unlikely(info->state != FBINFO_STATE_RUNNING))
196                 return;
197
198         if (1 || (info->flags & FBINFO_HWACCEL_DISABLED))
199                 return cfb_fillrect(info, rect);
200
201         /*psb_check_power_state(dev, PSB_DEVICE_SGX); */
202         psbfb_fillrect_accel(info, rect);
203         /* Drop power again here on MRST FIXMEAC */
204 }
205
206 static u32 psb_accel_2d_copy_direction(int xdir, int ydir)
207 {
208         if (xdir < 0)
209                 return (ydir < 0) ? PSB_2D_COPYORDER_BR2TL :
210                                         PSB_2D_COPYORDER_TR2BL;
211         else
212                 return (ydir < 0) ? PSB_2D_COPYORDER_BL2TR :
213                                         PSB_2D_COPYORDER_TL2BR;
214 }
215
216 /*
217  * @src_offset in bytes
218  * @src_stride in bytes
219  * @src_format psb 2D format defines
220  * @dst_offset in bytes
221  * @dst_stride in bytes
222  * @dst_format psb 2D format defines
223  * @src_x offset in pixels
224  * @src_y offset in pixels
225  * @dst_x offset in pixels
226  * @dst_y offset in pixels
227  * @size_x of the copied area
228  * @size_y of the copied area
229  */
230 static int psb_accel_2d_copy(struct drm_psb_private *dev_priv,
231                              uint32_t src_offset, uint32_t src_stride,
232                              uint32_t src_format, uint32_t dst_offset,
233                              uint32_t dst_stride, uint32_t dst_format,
234                              uint16_t src_x, uint16_t src_y,
235                              uint16_t dst_x, uint16_t dst_y,
236                              uint16_t size_x, uint16_t size_y)
237 {
238         uint32_t blit_cmd;
239         uint32_t buffer[10];
240         uint32_t *buf;
241         uint32_t direction;
242
243         buf = buffer;
244
245         direction =
246             psb_accel_2d_copy_direction(src_x - dst_x, src_y - dst_y);
247
248         if (direction == PSB_2D_COPYORDER_BR2TL ||
249             direction == PSB_2D_COPYORDER_TR2BL) {
250                 src_x += size_x - 1;
251                 dst_x += size_x - 1;
252         }
253         if (direction == PSB_2D_COPYORDER_BR2TL ||
254             direction == PSB_2D_COPYORDER_BL2TR) {
255                 src_y += size_y - 1;
256                 dst_y += size_y - 1;
257         }
258
259         blit_cmd =
260             PSB_2D_BLIT_BH |
261             PSB_2D_ROT_NONE |
262             PSB_2D_DSTCK_DISABLE |
263             PSB_2D_SRCCK_DISABLE |
264             PSB_2D_USE_PAT | PSB_2D_ROP3_SRCCOPY | direction;
265
266         *buf++ = PSB_2D_FENCE_BH;
267         *buf++ =
268             PSB_2D_DST_SURF_BH | dst_format | (dst_stride <<
269                                                PSB_2D_DST_STRIDE_SHIFT);
270         *buf++ = dst_offset;
271         *buf++ =
272             PSB_2D_SRC_SURF_BH | src_format | (src_stride <<
273                                                PSB_2D_SRC_STRIDE_SHIFT);
274         *buf++ = src_offset;
275         *buf++ =
276             PSB_2D_SRC_OFF_BH | (src_x << PSB_2D_SRCOFF_XSTART_SHIFT) |
277             (src_y << PSB_2D_SRCOFF_YSTART_SHIFT);
278         *buf++ = blit_cmd;
279         *buf++ =
280             (dst_x << PSB_2D_DST_XSTART_SHIFT) | (dst_y <<
281                                                   PSB_2D_DST_YSTART_SHIFT);
282         *buf++ =
283             (size_x << PSB_2D_DST_XSIZE_SHIFT) | (size_y <<
284                                                   PSB_2D_DST_YSIZE_SHIFT);
285         *buf++ = PSB_2D_FLUSH_BH;
286
287         return psbfb_2d_submit(dev_priv, buffer, buf - buffer);
288 }
289
290 static void psbfb_copyarea_accel(struct fb_info *info,
291                                  const struct fb_copyarea *a)
292 {
293         struct psb_fbdev *fbdev = info->par;
294         struct psb_framebuffer *psbfb = &fbdev->pfb;
295         struct drm_device *dev = psbfb->base.dev;
296         struct drm_framebuffer *fb = fbdev->psb_fb_helper.fb;
297         struct drm_psb_private *dev_priv = dev->dev_private;
298         uint32_t offset;
299         uint32_t stride;
300         uint32_t src_format;
301         uint32_t dst_format;
302
303         if (!fb)
304                 return;
305
306         offset = psbfb->gtt->offset;
307         stride = fb->pitch;
308
309         switch (fb->depth) {
310         case 8:
311                 src_format = PSB_2D_SRC_332RGB;
312                 dst_format = PSB_2D_DST_332RGB;
313                 break;
314         case 15:
315                 src_format = PSB_2D_SRC_555RGB;
316                 dst_format = PSB_2D_DST_555RGB;
317                 break;
318         case 16:
319                 src_format = PSB_2D_SRC_565RGB;
320                 dst_format = PSB_2D_DST_565RGB;
321                 break;
322         case 24:
323         case 32:
324                 /* this is wrong but since we don't do blending its okay */
325                 src_format = PSB_2D_SRC_8888ARGB;
326                 dst_format = PSB_2D_DST_8888ARGB;
327                 break;
328         default:
329                 /* software fallback */
330                 cfb_copyarea(info, a);
331                 return;
332         }
333
334         psb_accel_2d_copy(dev_priv,
335                           offset, stride, src_format,
336                           offset, stride, dst_format,
337                           a->sx, a->sy, a->dx, a->dy, a->width, a->height);
338 }
339
340 void psbfb_copyarea(struct fb_info *info,
341                            const struct fb_copyarea *region)
342 {
343         if (unlikely(info->state != FBINFO_STATE_RUNNING))
344                 return;
345
346         if (info->flags & FBINFO_HWACCEL_DISABLED)
347                 return cfb_copyarea(info, region);
348
349         /* psb_check_power_state(dev, PSB_DEVICE_SGX); */
350         psbfb_copyarea_accel(info, region);
351         /* Need to power back off here for MRST FIXMEAC */
352 }
353
354 void psbfb_imageblit(struct fb_info *info, const struct fb_image *image)
355 {
356         /* For now */
357         cfb_imageblit(info, image);
358 }
359
360 int psbfb_sync(struct fb_info *info)
361 {
362         struct psb_fbdev *fbdev = info->par;
363         struct psb_framebuffer *psbfb = &fbdev->pfb;
364         struct drm_device *dev = psbfb->base.dev;
365         struct drm_psb_private *dev_priv = dev->dev_private;
366         unsigned long _end = jiffies + DRM_HZ;
367         int busy = 0;
368
369         /*
370          * First idle the 2D engine.
371          */
372
373         if ((PSB_RSGX32(PSB_CR_2D_SOCIF) == _PSB_C2_SOCIF_EMPTY) &&
374             ((PSB_RSGX32(PSB_CR_2D_BLIT_STATUS) & _PSB_C2B_STATUS_BUSY) == 0))
375                 goto out;
376
377         do {
378                 busy = (PSB_RSGX32(PSB_CR_2D_SOCIF) != _PSB_C2_SOCIF_EMPTY);
379                 cpu_relax();
380         } while (busy && !time_after_eq(jiffies, _end));
381
382         if (busy)
383                 busy = (PSB_RSGX32(PSB_CR_2D_SOCIF) != _PSB_C2_SOCIF_EMPTY);
384         if (busy)
385                 goto out;
386
387         do {
388                 busy = ((PSB_RSGX32(PSB_CR_2D_BLIT_STATUS) &
389                                                 _PSB_C2B_STATUS_BUSY) != 0);
390                 cpu_relax();
391         } while (busy && !time_after_eq(jiffies, _end));
392         if (busy)
393                 busy = ((PSB_RSGX32(PSB_CR_2D_BLIT_STATUS) &
394                                         _PSB_C2B_STATUS_BUSY) != 0);
395
396 out:
397         return (busy) ? -EBUSY : 0;
398 }