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