1 /**************************************************************************
2 * Copyright (c) 2007, Intel Corporation.
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
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>
28 #include <linux/tty.h>
29 #include <linux/slab.h>
30 #include <linux/delay.h>
32 #include <linux/init.h>
33 #include <linux/console.h>
37 #include <drm/drm_crtc.h>
43 void psb_spank(struct drm_psb_private *dev_priv)
45 PSB_WSGX32(_PSB_CS_RESET_BIF_RESET | _PSB_CS_RESET_DPM_RESET |
46 _PSB_CS_RESET_TA_RESET | _PSB_CS_RESET_USE_RESET |
47 _PSB_CS_RESET_ISP_RESET | _PSB_CS_RESET_TSP_RESET |
48 _PSB_CS_RESET_TWOD_RESET, PSB_CR_SOFT_RESET);
49 (void) PSB_RSGX32(PSB_CR_SOFT_RESET);
53 PSB_WSGX32(0, PSB_CR_SOFT_RESET);
55 PSB_WSGX32(PSB_RSGX32(PSB_CR_BIF_CTRL) | _PSB_CB_CTRL_CLEAR_FAULT,
58 (void) PSB_RSGX32(PSB_CR_BIF_CTRL);
61 PSB_WSGX32(PSB_RSGX32(PSB_CR_BIF_CTRL) & ~_PSB_CB_CTRL_CLEAR_FAULT,
63 (void) PSB_RSGX32(PSB_CR_BIF_CTRL);
64 PSB_WSGX32(dev_priv->pg->gatt_start, PSB_CR_BIF_TWOD_REQ_BASE);
67 static int psb_2d_wait_available(struct drm_psb_private *dev_priv,
70 uint32_t avail = PSB_RSGX32(PSB_CR_2D_SOCIF);
71 unsigned long t = jiffies + HZ;
74 avail = PSB_RSGX32(PSB_CR_2D_SOCIF);
75 if (time_after(jiffies, t)) {
83 /* FIXME: Remember if we expose the 2D engine to the DRM we need to serialize
84 it with console use */
86 int psbfb_2d_submit(struct drm_psb_private *dev_priv, uint32_t *cmdbuf,
94 submit_size = (size < 0x60) ? size : 0x60;
96 ret = psb_2d_wait_available(dev_priv, submit_size);
101 for (i = 0; i < submit_size; i += 4) {
102 PSB_WSGX32(*cmdbuf++, PSB_SGX_2D_SLAVE_PORT + i);
104 (void)PSB_RSGX32(PSB_SGX_2D_SLAVE_PORT + i - 4);
109 static int psb_accel_2d_fillrect(struct drm_psb_private *dev_priv,
110 uint32_t dst_offset, uint32_t dst_stride,
111 uint32_t dst_format, uint16_t dst_x,
112 uint16_t dst_y, uint16_t size_x,
113 uint16_t size_y, uint32_t fill)
120 *buf++ = PSB_2D_FENCE_BH;
123 PSB_2D_DST_SURF_BH | dst_format | (dst_stride <<
124 PSB_2D_DST_STRIDE_SHIFT);
130 PSB_2D_COPYORDER_TL2BR |
131 PSB_2D_DSTCK_DISABLE |
132 PSB_2D_SRCCK_DISABLE | PSB_2D_USE_FILL | PSB_2D_ROP3_PATCOPY;
134 *buf++ = fill << PSB_2D_FILLCOLOUR_SHIFT;
136 (dst_x << PSB_2D_DST_XSTART_SHIFT) | (dst_y <<
137 PSB_2D_DST_YSTART_SHIFT);
139 (size_x << PSB_2D_DST_XSIZE_SHIFT) | (size_y <<
140 PSB_2D_DST_YSIZE_SHIFT);
141 *buf++ = PSB_2D_FLUSH_BH;
143 return psbfb_2d_submit(dev_priv, buffer, buf - buffer);
146 static void psbfb_fillrect_accel(struct fb_info *info,
147 const struct fb_fillrect *r)
149 struct psb_fbdev *fbdev = info->par;
150 struct psb_framebuffer *psbfb = fbdev->pfb;
151 struct drm_device *dev = psbfb->base.dev;
152 struct drm_framebuffer *fb = fbdev->psb_fb_helper.fb;
153 struct drm_psb_private *dev_priv = dev->dev_private;
162 offset = psbfb->gtt->offset;
167 format = PSB_2D_DST_332RGB;
170 format = PSB_2D_DST_555RGB;
173 format = PSB_2D_DST_565RGB;
177 /* this is wrong but since we don't do blending its okay */
178 format = PSB_2D_DST_8888ARGB;
181 /* software fallback */
182 cfb_fillrect(info, r);
186 psb_accel_2d_fillrect(dev_priv,
187 offset, stride, format,
188 r->dx, r->dy, r->width, r->height, r->color);
191 void psbfb_fillrect(struct fb_info *info,
192 const struct fb_fillrect *rect)
194 if (unlikely(info->state != FBINFO_STATE_RUNNING))
197 if (1 || (info->flags & FBINFO_HWACCEL_DISABLED))
198 return cfb_fillrect(info, rect);
200 /*psb_check_power_state(dev, PSB_DEVICE_SGX); */
201 psbfb_fillrect_accel(info, rect);
202 /* Drop power again here on MRST FIXMEAC */
205 static u32 psb_accel_2d_copy_direction(int xdir, int ydir)
208 return (ydir < 0) ? PSB_2D_COPYORDER_BR2TL :
209 PSB_2D_COPYORDER_TR2BL;
211 return (ydir < 0) ? PSB_2D_COPYORDER_BL2TR :
212 PSB_2D_COPYORDER_TL2BR;
216 * @src_offset in bytes
217 * @src_stride in bytes
218 * @src_format psb 2D format defines
219 * @dst_offset in bytes
220 * @dst_stride in bytes
221 * @dst_format psb 2D format defines
222 * @src_x offset in pixels
223 * @src_y offset in pixels
224 * @dst_x offset in pixels
225 * @dst_y offset in pixels
226 * @size_x of the copied area
227 * @size_y of the copied area
229 static int psb_accel_2d_copy(struct drm_psb_private *dev_priv,
230 uint32_t src_offset, uint32_t src_stride,
231 uint32_t src_format, uint32_t dst_offset,
232 uint32_t dst_stride, uint32_t dst_format,
233 uint16_t src_x, uint16_t src_y,
234 uint16_t dst_x, uint16_t dst_y,
235 uint16_t size_x, uint16_t size_y)
245 psb_accel_2d_copy_direction(src_x - dst_x, src_y - dst_y);
247 if (direction == PSB_2D_COPYORDER_BR2TL ||
248 direction == PSB_2D_COPYORDER_TR2BL) {
252 if (direction == PSB_2D_COPYORDER_BR2TL ||
253 direction == PSB_2D_COPYORDER_BL2TR) {
261 PSB_2D_DSTCK_DISABLE |
262 PSB_2D_SRCCK_DISABLE |
263 PSB_2D_USE_PAT | PSB_2D_ROP3_SRCCOPY | direction;
265 *buf++ = PSB_2D_FENCE_BH;
267 PSB_2D_DST_SURF_BH | dst_format | (dst_stride <<
268 PSB_2D_DST_STRIDE_SHIFT);
271 PSB_2D_SRC_SURF_BH | src_format | (src_stride <<
272 PSB_2D_SRC_STRIDE_SHIFT);
275 PSB_2D_SRC_OFF_BH | (src_x << PSB_2D_SRCOFF_XSTART_SHIFT) |
276 (src_y << PSB_2D_SRCOFF_YSTART_SHIFT);
279 (dst_x << PSB_2D_DST_XSTART_SHIFT) | (dst_y <<
280 PSB_2D_DST_YSTART_SHIFT);
282 (size_x << PSB_2D_DST_XSIZE_SHIFT) | (size_y <<
283 PSB_2D_DST_YSIZE_SHIFT);
284 *buf++ = PSB_2D_FLUSH_BH;
286 return psbfb_2d_submit(dev_priv, buffer, buf - buffer);
289 static void psbfb_copyarea_accel(struct fb_info *info,
290 const struct fb_copyarea *a)
292 struct psb_fbdev *fbdev = info->par;
293 struct psb_framebuffer *psbfb = fbdev->pfb;
294 struct drm_device *dev = psbfb->base.dev;
295 struct drm_framebuffer *fb = fbdev->psb_fb_helper.fb;
296 struct drm_psb_private *dev_priv = dev->dev_private;
305 offset = psbfb->gtt->offset;
310 src_format = PSB_2D_SRC_332RGB;
311 dst_format = PSB_2D_DST_332RGB;
314 src_format = PSB_2D_SRC_555RGB;
315 dst_format = PSB_2D_DST_555RGB;
318 src_format = PSB_2D_SRC_565RGB;
319 dst_format = PSB_2D_DST_565RGB;
323 /* this is wrong but since we don't do blending its okay */
324 src_format = PSB_2D_SRC_8888ARGB;
325 dst_format = PSB_2D_DST_8888ARGB;
328 /* software fallback */
329 cfb_copyarea(info, a);
333 psb_accel_2d_copy(dev_priv,
334 offset, stride, src_format,
335 offset, stride, dst_format,
336 a->sx, a->sy, a->dx, a->dy, a->width, a->height);
339 void psbfb_copyarea(struct fb_info *info,
340 const struct fb_copyarea *region)
342 if (unlikely(info->state != FBINFO_STATE_RUNNING))
345 if (info->flags & FBINFO_HWACCEL_DISABLED)
346 return cfb_copyarea(info, region);
348 /* psb_check_power_state(dev, PSB_DEVICE_SGX); */
349 psbfb_copyarea_accel(info, region);
350 /* Need to power back off here for MRST FIXMEAC */
353 void psbfb_imageblit(struct fb_info *info, const struct fb_image *image)
356 cfb_imageblit(info, image);
359 int psbfb_sync(struct fb_info *info)
361 struct psb_fbdev *fbdev = info->par;
362 struct psb_framebuffer *psbfb = fbdev->pfb;
363 struct drm_device *dev = psbfb->base.dev;
364 struct drm_psb_private *dev_priv = dev->dev_private;
365 unsigned long _end = jiffies + DRM_HZ;
369 /* Just a way to quickly test if cmd issue explodes */
373 psbfb_2d_submit(dev_priv, test, 1);
376 * First idle the 2D engine.
379 if ((PSB_RSGX32(PSB_CR_2D_SOCIF) == _PSB_C2_SOCIF_EMPTY) &&
380 ((PSB_RSGX32(PSB_CR_2D_BLIT_STATUS) & _PSB_C2B_STATUS_BUSY) == 0))
384 busy = (PSB_RSGX32(PSB_CR_2D_SOCIF) != _PSB_C2_SOCIF_EMPTY);
386 } while (busy && !time_after_eq(jiffies, _end));
389 busy = (PSB_RSGX32(PSB_CR_2D_SOCIF) != _PSB_C2_SOCIF_EMPTY);
394 busy = ((PSB_RSGX32(PSB_CR_2D_BLIT_STATUS) &
395 _PSB_C2B_STATUS_BUSY) != 0);
397 } while (busy && !time_after_eq(jiffies, _end));
399 busy = ((PSB_RSGX32(PSB_CR_2D_BLIT_STATUS) &
400 _PSB_C2B_STATUS_BUSY) != 0);
403 return (busy) ? -EBUSY : 0;
407 info->fix.accel = FB_ACCEL_I830;
408 info->flags = FBINFO_DEFAULT;