ps3fb.c 33.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/*
 *  linux/drivers/video/ps3fb.c -- PS3 GPU frame buffer device
 *
 *	Copyright (C) 2006 Sony Computer Entertainment Inc.
 *	Copyright 2006, 2007 Sony Corporation
 *
 *  This file is based on :
 *
 *  linux/drivers/video/vfb.c -- Virtual frame buffer device
 *
 *	Copyright (C) 2002 James Simmons
 *
 *	Copyright (C) 1997 Geert Uytterhoeven
 *
 *  This file is subject to the terms and conditions of the GNU General Public
 *  License. See the file COPYING in the main directory of this archive for
 *  more details.
 */

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/string.h>
#include <linux/mm.h>
#include <linux/interrupt.h>
#include <linux/console.h>
#include <linux/ioctl.h>
G
Geert Uytterhoeven 已提交
28 29
#include <linux/kthread.h>
#include <linux/freezer.h>
30
#include <linux/uaccess.h>
31 32 33 34 35 36 37 38 39
#include <linux/fb.h>
#include <linux/init.h>

#include <asm/abs_addr.h>
#include <asm/lv1call.h>
#include <asm/ps3av.h>
#include <asm/ps3fb.h>
#include <asm/ps3.h>

40 41 42

#define DEVICE_NAME		"ps3fb"

43 44 45 46 47 48 49 50 51 52 53
#define L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_SYNC	0x101
#define L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_FLIP	0x102
#define L1GPU_CONTEXT_ATTRIBUTE_FB_SETUP	0x600
#define L1GPU_CONTEXT_ATTRIBUTE_FB_BLIT		0x601
#define L1GPU_CONTEXT_ATTRIBUTE_FB_BLIT_SYNC	0x602

#define L1GPU_FB_BLIT_WAIT_FOR_COMPLETION	(1ULL << 32)

#define L1GPU_DISPLAY_SYNC_HSYNC		1
#define L1GPU_DISPLAY_SYNC_VSYNC		2

54 55
#define GPU_CMD_BUF_SIZE			(2 * 1024 * 1024)
#define GPU_FB_START				(64 * 1024)
56
#define GPU_IOIF				(0x0d000000UL)
57
#define GPU_ALIGN_UP(x)				_ALIGN_UP((x), 64)
58
#define GPU_MAX_LINE_LENGTH			(65536 - 64)
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119

#define GPU_INTR_STATUS_VSYNC_0			0	/* vsync on head A */
#define GPU_INTR_STATUS_VSYNC_1			1	/* vsync on head B */
#define GPU_INTR_STATUS_FLIP_0			3	/* flip head A */
#define GPU_INTR_STATUS_FLIP_1			4	/* flip head B */
#define GPU_INTR_STATUS_QUEUE_0			5	/* queue head A */
#define GPU_INTR_STATUS_QUEUE_1			6	/* queue head B */

#define GPU_DRIVER_INFO_VERSION			0x211

/* gpu internals */
struct display_head {
	u64 be_time_stamp;
	u32 status;
	u32 offset;
	u32 res1;
	u32 res2;
	u32 field;
	u32 reserved1;

	u64 res3;
	u32 raster;

	u64 vblank_count;
	u32 field_vsync;
	u32 reserved2;
};

struct gpu_irq {
	u32 irq_outlet;
	u32 status;
	u32 mask;
	u32 video_cause;
	u32 graph_cause;
	u32 user_cause;

	u32 res1;
	u64 res2;

	u32 reserved[4];
};

struct gpu_driver_info {
	u32 version_driver;
	u32 version_gpu;
	u32 memory_size;
	u32 hardware_channel;

	u32 nvcore_frequency;
	u32 memory_frequency;

	u32 reserved[1063];
	struct display_head display_head[8];
	struct gpu_irq irq;
};

struct ps3fb_priv {
	unsigned int irq_no;

	u64 context_handle, memory_handle;
	void *xdr_ea;
120
	size_t xdr_size;
121 122 123 124 125 126 127 128
	struct gpu_driver_info *dinfo;

	u64 vblank_count;	/* frame count */
	wait_queue_head_t wait_vsync;

	atomic_t ext_flip;	/* on/off flip with vsync */
	atomic_t f_count;	/* fb_open count */
	int is_blanked;
G
Geert Uytterhoeven 已提交
129 130
	int is_kicked;
	struct task_struct *task;
131 132 133
};
static struct ps3fb_priv ps3fb;

134 135 136 137 138
struct ps3fb_par {
	u32 pseudo_palette[16];
	int mode_id, new_mode_id;
	int res_index;
	unsigned int num_frames;	/* num of frame buffers */
139 140 141 142
	unsigned int width;
	unsigned int height;
	unsigned long full_offset;	/* start of fullscreen DDR fb */
	unsigned long fb_offset;	/* start of actual DDR fb */
143
	unsigned long pan_offset;
144 145
};

146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
struct ps3fb_res_table {
	u32 xres;
	u32 yres;
	u32 xoff;
	u32 yoff;
	u32 type;
};
#define PS3FB_RES_FULL 1
static const struct ps3fb_res_table ps3fb_res[] = {
	/* res_x,y   margin_x,y  full */
	{  720,  480,  72,  48 , 0},
	{  720,  576,  72,  58 , 0},
	{ 1280,  720,  78,  38 , 0},
	{ 1920, 1080, 116,  58 , 0},
	/* full mode */
	{  720,  480,   0,   0 , PS3FB_RES_FULL},
	{  720,  576,   0,   0 , PS3FB_RES_FULL},
	{ 1280,  720,   0,   0 , PS3FB_RES_FULL},
	{ 1920, 1080,   0,   0 , PS3FB_RES_FULL},
	/* vesa: normally full mode */
	{ 1280,  768,   0,   0 , 0},
	{ 1280, 1024,   0,   0 , 0},
	{ 1920, 1200,   0,   0 , 0},
	{    0,    0,   0,   0 , 0} };

/* default resolution */
172
#define GPU_RES_INDEX	0		/* 720 x 480 */
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289

static const struct fb_videomode ps3fb_modedb[] = {
    /* 60 Hz broadcast modes (modes "1" to "5") */
    {
        /* 480i */
        "480i", 60, 576, 384, 74074, 130, 89, 78, 57, 63, 6,
        FB_SYNC_BROADCAST, FB_VMODE_INTERLACED
    },    {
        /* 480p */
        "480p", 60, 576, 384, 37037, 130, 89, 78, 57, 63, 6,
        FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
    },    {
        /* 720p */
        "720p", 60, 1124, 644, 13481, 298, 148, 57, 44, 80, 5,
        FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
    },    {
        /* 1080i */
        "1080i", 60, 1688, 964, 13481, 264, 160, 94, 62, 88, 5,
        FB_SYNC_BROADCAST, FB_VMODE_INTERLACED
    },    {
        /* 1080p */
        "1080p", 60, 1688, 964, 6741, 264, 160, 94, 62, 88, 5,
        FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
    },

    /* 50 Hz broadcast modes (modes "6" to "10") */
    {
        /* 576i */
        "576i", 50, 576, 460, 74074, 142, 83, 97, 63, 63, 5,
        FB_SYNC_BROADCAST, FB_VMODE_INTERLACED
    },    {
        /* 576p */
        "576p", 50, 576, 460, 37037, 142, 83, 97, 63, 63, 5,
        FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
    },    {
        /* 720p */
        "720p", 50, 1124, 644, 13468, 298, 478, 57, 44, 80, 5,
        FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
    },    {
        /* 1080 */
        "1080i", 50, 1688, 964, 13468, 264, 600, 94, 62, 88, 5,
        FB_SYNC_BROADCAST, FB_VMODE_INTERLACED
    },    {
        /* 1080p */
        "1080p", 50, 1688, 964, 6734, 264, 600, 94, 62, 88, 5,
        FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
    },

    /* VESA modes (modes "11" to "13") */
    {
	/* WXGA */
	"wxga", 60, 1280, 768, 12924, 160, 24, 29, 3, 136, 6,
	0, FB_VMODE_NONINTERLACED,
	FB_MODE_IS_VESA
    }, {
	/* SXGA */
	"sxga", 60, 1280, 1024, 9259, 248, 48, 38, 1, 112, 3,
	FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED,
	FB_MODE_IS_VESA
    }, {
	/* WUXGA */
	"wuxga", 60, 1920, 1200, 6494, 80, 48, 26, 3, 32, 6,
	FB_SYNC_HOR_HIGH_ACT, FB_VMODE_NONINTERLACED,
	FB_MODE_IS_VESA
    },

    /* 60 Hz broadcast modes (full resolution versions of modes "1" to "5") */
    {
	/* 480if */
	"480if", 60, 720, 480, 74074, 58, 17, 30, 9, 63, 6,
	FB_SYNC_BROADCAST, FB_VMODE_INTERLACED
    }, {
	/* 480pf */
	"480pf", 60, 720, 480, 37037, 58, 17, 30, 9, 63, 6,
	FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
    }, {
	/* 720pf */
	"720pf", 60, 1280, 720, 13481, 220, 70, 19, 6, 80, 5,
	FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
    }, {
	/* 1080if */
	"1080if", 60, 1920, 1080, 13481, 148, 44, 36, 4, 88, 5,
	FB_SYNC_BROADCAST, FB_VMODE_INTERLACED
    }, {
	/* 1080pf */
	"1080pf", 60, 1920, 1080, 6741, 148, 44, 36, 4, 88, 5,
	FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
    },

    /* 50 Hz broadcast modes (full resolution versions of modes "6" to "10") */
    {
	/* 576if */
	"576if", 50, 720, 576, 74074, 70, 11, 39, 5, 63, 5,
	FB_SYNC_BROADCAST, FB_VMODE_INTERLACED
    }, {
	/* 576pf */
	"576pf", 50, 720, 576, 37037, 70, 11, 39, 5, 63, 5,
	FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
    }, {
	/* 720pf */
	"720pf", 50, 1280, 720, 13468, 220, 400, 19, 6, 80, 5,
	FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
    }, {
	/* 1080if */
	"1080f", 50, 1920, 1080, 13468, 148, 484, 36, 4, 88, 5,
	FB_SYNC_BROADCAST, FB_VMODE_INTERLACED
    }, {
	/* 1080pf */
	"1080pf", 50, 1920, 1080, 6734, 148, 484, 36, 4, 88, 5,
	FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
    }
};


#define HEAD_A
#define HEAD_B

290 291
#define BPP		4			/* number of bytes per pixel */

292

293
static int ps3fb_mode;
294
module_param(ps3fb_mode, int, 0);
295

296
static char *mode_option __devinitdata;
297

298
static int ps3fb_get_res_table(u32 xres, u32 yres, int mode)
299 300 301 302 303
{
	int full_mode;
	unsigned int i;
	u32 x, y, f;

304
	full_mode = (mode & PS3AV_MODE_FULL) ? PS3FB_RES_FULL : 0;
305 306 307 308 309 310
	for (i = 0;; i++) {
		x = ps3fb_res[i].xres;
		y = ps3fb_res[i].yres;
		f = ps3fb_res[i].type;

		if (!x) {
311
			pr_debug("ERROR: ps3fb_get_res_table()\n");
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
			return -1;
		}

		if (full_mode == PS3FB_RES_FULL && f != PS3FB_RES_FULL)
			continue;

		if (x == xres && (yres == 0 || y == yres))
			break;

		x = x - 2 * ps3fb_res[i].xoff;
		y = y - 2 * ps3fb_res[i].yoff;
		if (x == xres && (yres == 0 || y == yres))
			break;
	}
	return i;
}

static unsigned int ps3fb_find_mode(const struct fb_var_screeninfo *var,
330
				    u32 *ddr_line_length, u32 *xdr_line_length)
331
{
332
	unsigned int i, fi, mode;
333 334 335 336 337 338 339 340 341 342 343 344

	for (i = 0; i < ARRAY_SIZE(ps3fb_modedb); i++)
		if (var->xres == ps3fb_modedb[i].xres &&
		    var->yres == ps3fb_modedb[i].yres &&
		    var->pixclock == ps3fb_modedb[i].pixclock &&
		    var->hsync_len == ps3fb_modedb[i].hsync_len &&
		    var->vsync_len == ps3fb_modedb[i].vsync_len &&
		    var->left_margin == ps3fb_modedb[i].left_margin &&
		    var->right_margin == ps3fb_modedb[i].right_margin &&
		    var->upper_margin == ps3fb_modedb[i].upper_margin &&
		    var->lower_margin == ps3fb_modedb[i].lower_margin &&
		    var->sync == ps3fb_modedb[i].sync &&
345 346
		    (var->vmode & FB_VMODE_MASK) == ps3fb_modedb[i].vmode)
			goto found;
347

348
	pr_debug("ps3fb_find_mode: mode not found\n");
349
	return 0;
350 351 352

found:
	/* Cropped broadcast modes use the full line length */
353 354
	fi = i < PS3AV_MODE_1080P50 ? i + PS3AV_MODE_WUXGA : i;
	*ddr_line_length = ps3fb_modedb[fi].xres * BPP;
355 356 357 358 359 360 361 362 363 364

	if (ps3_compare_firmware_version(1, 9, 0) >= 0) {
		*xdr_line_length = GPU_ALIGN_UP(max(var->xres,
						    var->xres_virtual) * BPP);
		if (*xdr_line_length > GPU_MAX_LINE_LENGTH)
			*xdr_line_length = GPU_MAX_LINE_LENGTH;
	} else
		*xdr_line_length = *ddr_line_length;

	/* Full broadcast modes have the full mode bit set */
365 366
	mode = i+1;
	if (mode > PS3AV_MODE_WUXGA)
367
		mode = (mode - PS3AV_MODE_WUXGA) | PS3AV_MODE_FULL;
368 369 370 371

	pr_debug("ps3fb_find_mode: mode %u\n", mode);

	return mode;
372 373
}

374
static const struct fb_videomode *ps3fb_default_mode(int id)
375
{
376
	u32 mode = id & PS3AV_MODE_MASK;
377 378
	u32 flags;

379
	if (mode < PS3AV_MODE_480I || mode > PS3AV_MODE_WUXGA)
380 381
		return NULL;

382
	flags = id & ~PS3AV_MODE_MASK;
383

384
	if (mode <= PS3AV_MODE_1080P50 && flags & PS3AV_MODE_FULL) {
385
		/* Full broadcast mode */
386
		return &ps3fb_modedb[mode + PS3AV_MODE_WUXGA - 1];
387 388 389 390 391
	}

	return &ps3fb_modedb[mode - 1];
}

392 393
static void ps3fb_sync_image(struct device *dev, u64 frame_offset,
			     u64 dst_offset, u64 src_offset, u32 width,
394 395
			     u32 height, u32 dst_line_length,
			     u32 src_line_length)
396
{
397
	int status;
398 399 400 401 402
	u64 line_length;

	line_length = dst_line_length;
	if (src_line_length != dst_line_length)
		line_length |= (u64)src_line_length << 32;
403

404
	src_offset += GPU_FB_START;
405 406
	status = lv1_gpu_context_attribute(ps3fb.context_handle,
					   L1GPU_CONTEXT_ATTRIBUTE_FB_BLIT,
407
					   dst_offset, GPU_IOIF + src_offset,
408
					   L1GPU_FB_BLIT_WAIT_FOR_COMPLETION |
409 410
					   (width << 16) | height,
					   line_length);
411
	if (status)
412
		dev_err(dev,
413 414
			"%s: lv1_gpu_context_attribute FB_BLIT failed: %d\n",
			__func__, status);
415 416 417
#ifdef HEAD_A
	status = lv1_gpu_context_attribute(ps3fb.context_handle,
					   L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_FLIP,
418
					   0, frame_offset, 0, 0);
419
	if (status)
420
		dev_err(dev, "%s: lv1_gpu_context_attribute FLIP failed: %d\n",
421
			__func__, status);
422 423 424 425
#endif
#ifdef HEAD_B
	status = lv1_gpu_context_attribute(ps3fb.context_handle,
					   L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_FLIP,
426
					   1, frame_offset, 0, 0);
427
	if (status)
428
		dev_err(dev, "%s: lv1_gpu_context_attribute FLIP failed: %d\n",
429
			__func__, status);
430
#endif
431 432 433 434 435 436
}

static int ps3fb_sync(struct fb_info *info, u32 frame)
{
	struct ps3fb_par *par = info->par;
	int i, error = 0;
437 438
	u32 ddr_line_length, xdr_line_length;
	u64 ddr_base, xdr_base;
439 440 441 442 443 444 445 446 447

	if (frame > par->num_frames - 1) {
		dev_dbg(info->device, "%s: invalid frame number (%u)\n",
			__func__, frame);
		error = -EINVAL;
		goto out;
	}

	i = par->res_index;
448 449 450 451
	xdr_line_length = info->fix.line_length;
	ddr_line_length = ps3fb_res[i].xres * BPP;
	xdr_base = frame * info->var.yres_virtual * xdr_line_length;
	ddr_base = frame * ps3fb_res[i].yres * ddr_line_length;
452

453 454 455 456
	ps3fb_sync_image(info->device, ddr_base + par->full_offset,
			 ddr_base + par->fb_offset, xdr_base + par->pan_offset,
			 par->width, par->height, ddr_line_length,
			 xdr_line_length);
457 458 459

out:
	return error;
460 461 462 463 464 465 466 467 468 469 470 471 472
}

static int ps3fb_open(struct fb_info *info, int user)
{
	atomic_inc(&ps3fb.f_count);
	return 0;
}

static int ps3fb_release(struct fb_info *info, int user)
{
	if (atomic_dec_and_test(&ps3fb.f_count)) {
		if (atomic_read(&ps3fb.ext_flip)) {
			atomic_set(&ps3fb.ext_flip, 0);
J
Jeremy Kerr 已提交
473 474 475 476
			if (!try_acquire_console_sem()) {
				ps3fb_sync(info, 0);	/* single buffer */
				release_console_sem();
			}
477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
		}
	}
	return 0;
}

    /*
     *  Setting the video mode has been split into two parts.
     *  First part, xxxfb_check_var, must not write anything
     *  to hardware, it should only verify and adjust var.
     *  This means it doesn't alter par but it does use hardware
     *  data from it to check this var.
     */

static int ps3fb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
{
492
	u32 xdr_line_length, ddr_line_length;
493 494
	int mode;

495 496 497 498
	dev_dbg(info->device, "var->xres:%u info->var.xres:%u\n", var->xres,
		info->var.xres);
	dev_dbg(info->device, "var->yres:%u info->var.yres:%u\n", var->yres,
		info->var.yres);
499 500

	/* FIXME For now we do exact matches only */
501
	mode = ps3fb_find_mode(var, &ddr_line_length, &xdr_line_length);
502 503 504
	if (!mode)
		return -EINVAL;

505 506 507 508 509
	/* Virtual screen */
	if (var->xres_virtual < var->xres)
		var->xres_virtual = var->xres;
	if (var->yres_virtual < var->yres)
		var->yres_virtual = var->yres;
510

511
	if (var->xres_virtual > xdr_line_length / BPP) {
512
		dev_dbg(info->device,
513
			"Horizontal virtual screen size too large\n");
514 515 516
		return -EINVAL;
	}

517 518 519 520 521
	if (var->xoffset + var->xres > var->xres_virtual ||
	    var->yoffset + var->yres > var->yres_virtual) {
		dev_dbg(info->device, "panning out-of-range\n");
		return -EINVAL;
	}
522 523 524 525 526 527 528 529 530

	/* We support ARGB8888 only */
	if (var->bits_per_pixel > 32 || var->grayscale ||
	    var->red.offset > 16 || var->green.offset > 8 ||
	    var->blue.offset > 0 || var->transp.offset > 24 ||
	    var->red.length > 8 || var->green.length > 8 ||
	    var->blue.length > 8 || var->transp.length > 8 ||
	    var->red.msb_right || var->green.msb_right ||
	    var->blue.msb_right || var->transp.msb_right || var->nonstd) {
531
		dev_dbg(info->device, "We support ARGB8888 only\n");
532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
		return -EINVAL;
	}

	var->bits_per_pixel = 32;
	var->red.offset = 16;
	var->green.offset = 8;
	var->blue.offset = 0;
	var->transp.offset = 24;
	var->red.length = 8;
	var->green.length = 8;
	var->blue.length = 8;
	var->transp.length = 8;
	var->red.msb_right = 0;
	var->green.msb_right = 0;
	var->blue.msb_right = 0;
	var->transp.msb_right = 0;

	/* Rotation is not supported */
	if (var->rotate) {
551
		dev_dbg(info->device, "Rotation is not supported\n");
552 553 554 555
		return -EINVAL;
	}

	/* Memory limit */
556
	if (var->yres_virtual * xdr_line_length > ps3fb.xdr_size) {
557
		dev_dbg(info->device, "Not enough memory\n");
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572
		return -ENOMEM;
	}

	var->height = -1;
	var->width = -1;

	return 0;
}

    /*
     * This routine actually sets the video mode.
     */

static int ps3fb_set_par(struct fb_info *info)
{
573
	struct ps3fb_par *par = info->par;
574
	unsigned int mode, ddr_line_length, xdr_line_length, lines, maxlines;
575
	int i;
576 577
	unsigned long offset;
	u64 dst;
578

579
	dev_dbg(info->device, "xres:%d xv:%d yres:%d yv:%d clock:%d\n",
580 581 582
		info->var.xres, info->var.xres_virtual,
		info->var.yres, info->var.yres_virtual, info->var.pixclock);

583
	mode = ps3fb_find_mode(&info->var, &ddr_line_length, &xdr_line_length);
584 585 586
	if (!mode)
		return -EINVAL;

587
	i = ps3fb_get_res_table(info->var.xres, info->var.yres, mode);
588
	par->res_index = i;
589

590 591
	info->fix.smem_start = virt_to_abs(ps3fb.xdr_ea);
	info->fix.smem_len = ps3fb.xdr_size;
592 593
	info->fix.xpanstep = info->var.xres_virtual > info->var.xres ? 1 : 0;
	info->fix.ypanstep = info->var.yres_virtual > info->var.yres ? 1 : 0;
594
	info->fix.line_length = xdr_line_length;
595

596
	info->screen_base = (char __iomem *)ps3fb.xdr_ea;
597

598 599 600
	par->num_frames = ps3fb.xdr_size /
			  max(ps3fb_res[i].yres * ddr_line_length,
			      info->var.yres_virtual * xdr_line_length);
601 602

	/* Keep the special bits we cannot set using fb_var_screeninfo */
603
	par->new_mode_id = (par->new_mode_id & ~PS3AV_MODE_MASK) | mode;
604

605 606
	par->width = info->var.xres;
	par->height = info->var.yres;
607 608 609 610

	/* Start of the virtual frame buffer (relative to fullscreen) */
	offset = ps3fb_res[i].yoff * ddr_line_length + ps3fb_res[i].xoff * BPP;

611 612
	par->fb_offset = GPU_ALIGN_UP(offset);
	par->full_offset = par->fb_offset - offset;
613
	par->pan_offset = info->var.yoffset * xdr_line_length +
614
			  info->var.xoffset * BPP;
615

616 617 618 619 620 621 622
	if (par->new_mode_id != par->mode_id) {
		if (ps3av_set_video_mode(par->new_mode_id)) {
			par->new_mode_id = par->mode_id;
			return -EINVAL;
		}
		par->mode_id = par->new_mode_id;
	}
623

624 625 626 627 628 629 630
	/* Clear XDR frame buffer memory */
	memset(ps3fb.xdr_ea, 0, ps3fb.xdr_size);

	/* Clear DDR frame buffer memory */
	lines = ps3fb_res[i].yres * par->num_frames;
	if (par->full_offset)
		lines++;
631 632
	maxlines = ps3fb.xdr_size / ddr_line_length;
	for (dst = 0; lines; dst += maxlines * ddr_line_length) {
633 634
		unsigned int l = min(lines, maxlines);
		ps3fb_sync_image(info->device, 0, dst, 0, ps3fb_res[i].xres, l,
635
				 ddr_line_length, ddr_line_length);
636 637 638
		lines -= l;
	}

639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664
	return 0;
}

    /*
     *  Set a single color register. The values supplied are already
     *  rounded down to the hardware's capabilities (according to the
     *  entries in the var structure). Return != 0 for invalid regno.
     */

static int ps3fb_setcolreg(unsigned int regno, unsigned int red,
			   unsigned int green, unsigned int blue,
			   unsigned int transp, struct fb_info *info)
{
	if (regno >= 16)
		return 1;

	red >>= 8;
	green >>= 8;
	blue >>= 8;
	transp >>= 8;

	((u32 *)info->pseudo_palette)[regno] = transp << 24 | red << 16 |
					       green << 8 | blue;
	return 0;
}

665 666 667 668 669 670 671 672 673 674
static int ps3fb_pan_display(struct fb_var_screeninfo *var,
			     struct fb_info *info)
{
	struct ps3fb_par *par = info->par;

	par->pan_offset = var->yoffset * info->fix.line_length +
			  var->xoffset * BPP;
	return 0;
}

675 676 677 678 679 680 681 682 683 684 685 686 687
    /*
     *  As we have a virtual frame buffer, we need our own mmap function
     */

static int ps3fb_mmap(struct fb_info *info, struct vm_area_struct *vma)
{
	unsigned long size, offset;

	size = vma->vm_end - vma->vm_start;
	offset = vma->vm_pgoff << PAGE_SHIFT;
	if (offset + size > info->fix.smem_len)
		return -EINVAL;

688
	offset += info->fix.smem_start;
689 690 691 692
	if (remap_pfn_range(vma, vma->vm_start, offset >> PAGE_SHIFT,
			    size, vma->vm_page_prot))
		return -EAGAIN;

693 694
	dev_dbg(info->device, "ps3fb: mmap framebuffer P(%lx)->V(%lx)\n",
		offset, vma->vm_start);
695 696 697 698 699 700 701 702 703 704 705
	return 0;
}

    /*
     * Blank the display
     */

static int ps3fb_blank(int blank, struct fb_info *info)
{
	int retval;

706
	dev_dbg(info->device, "%s: blank:%d\n", __func__, blank);
707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727
	switch (blank) {
	case FB_BLANK_POWERDOWN:
	case FB_BLANK_HSYNC_SUSPEND:
	case FB_BLANK_VSYNC_SUSPEND:
	case FB_BLANK_NORMAL:
		retval = ps3av_video_mute(1);	/* mute on */
		if (!retval)
			ps3fb.is_blanked = 1;
		break;

	default:		/* unblank */
		retval = ps3av_video_mute(0);	/* mute off */
		if (!retval)
			ps3fb.is_blanked = 0;
		break;
	}
	return retval;
}

static int ps3fb_get_vblank(struct fb_vblank *vblank)
{
728
	memset(vblank, 0, sizeof(*vblank));
729 730 731 732
	vblank->flags = FB_VBLANK_HAVE_VSYNC;
	return 0;
}

733
static int ps3fb_wait_for_vsync(u32 crtc)
734 735 736 737 738 739 740 741 742 743 744 745 746 747
{
	int ret;
	u64 count;

	count = ps3fb.vblank_count;
	ret = wait_event_interruptible_timeout(ps3fb.wait_vsync,
					       count != ps3fb.vblank_count,
					       HZ / 10);
	if (!ret)
		return -ETIMEDOUT;

	return 0;
}

748
static void ps3fb_flip_ctl(int on, void *data)
749
{
750
	struct ps3fb_priv *priv = data;
G
Geert Uytterhoeven 已提交
751
	if (on)
752
		atomic_dec_if_positive(&priv->ext_flip);
G
Geert Uytterhoeven 已提交
753
	else
754
		atomic_inc(&priv->ext_flip);
755 756 757 758 759 760 761 762 763 764 765
}


    /*
     * ioctl
     */

static int ps3fb_ioctl(struct fb_info *info, unsigned int cmd,
		       unsigned long arg)
{
	void __user *argp = (void __user *)arg;
766
	u32 val;
767 768 769 770 771 772
	int retval = -EFAULT;

	switch (cmd) {
	case FBIOGET_VBLANK:
		{
			struct fb_vblank vblank;
773
			dev_dbg(info->device, "FBIOGET_VBLANK:\n");
774 775 776 777 778 779 780 781 782 783 784 785
			retval = ps3fb_get_vblank(&vblank);
			if (retval)
				break;

			if (copy_to_user(argp, &vblank, sizeof(vblank)))
				retval = -EFAULT;
			break;
		}

	case FBIO_WAITFORVSYNC:
		{
			u32 crt;
786
			dev_dbg(info->device, "FBIO_WAITFORVSYNC:\n");
787 788 789 790 791 792 793 794 795
			if (get_user(crt, (u32 __user *) arg))
				break;

			retval = ps3fb_wait_for_vsync(crt);
			break;
		}

	case PS3FB_IOCTL_SETMODE:
		{
796
			struct ps3fb_par *par = info->par;
797 798 799 800 801 802
			const struct fb_videomode *mode;
			struct fb_var_screeninfo var;

			if (copy_from_user(&val, argp, sizeof(val)))
				break;

803
			if (!(val & PS3AV_MODE_MASK)) {
804
				u32 id = ps3av_get_auto_mode();
805 806 807
				if (id > 0)
					val = (val & ~PS3AV_MODE_MASK) | id;
			}
808
			dev_dbg(info->device, "PS3FB_IOCTL_SETMODE:%x\n", val);
809
			retval = -EINVAL;
810
			mode = ps3fb_default_mode(val);
811 812 813 814 815 816 817
			if (mode) {
				var = info->var;
				fb_videomode_to_var(&var, mode);
				acquire_console_sem();
				info->flags |= FBINFO_MISC_USEREVENT;
				/* Force, in case only special bits changed */
				var.activate |= FB_ACTIVATE_FORCE;
818
				par->new_mode_id = val;
819 820 821 822 823 824 825 826 827
				retval = fb_set_var(info, &var);
				info->flags &= ~FBINFO_MISC_USEREVENT;
				release_console_sem();
			}
			break;
		}

	case PS3FB_IOCTL_GETMODE:
		val = ps3av_get_mode();
828
		dev_dbg(info->device, "PS3FB_IOCTL_GETMODE:%x\n", val);
829 830 831 832 833 834
		if (!copy_to_user(argp, &val, sizeof(val)))
			retval = 0;
		break;

	case PS3FB_IOCTL_SCREENINFO:
		{
835
			struct ps3fb_par *par = info->par;
836
			struct ps3fb_ioctl_res res;
837
			dev_dbg(info->device, "PS3FB_IOCTL_SCREENINFO:\n");
838 839 840 841
			res.xres = info->fix.line_length / BPP;
			res.yres = info->var.yres_virtual;
			res.xoff = (res.xres - info->var.xres) / 2;
			res.yoff = (res.yres - info->var.yres) / 2;
842
			res.num_frames = par->num_frames;
843 844 845 846 847 848
			if (!copy_to_user(argp, &res, sizeof(res)))
				retval = 0;
			break;
		}

	case PS3FB_IOCTL_ON:
849
		dev_dbg(info->device, "PS3FB_IOCTL_ON:\n");
850 851 852 853 854
		atomic_inc(&ps3fb.ext_flip);
		retval = 0;
		break;

	case PS3FB_IOCTL_OFF:
855
		dev_dbg(info->device, "PS3FB_IOCTL_OFF:\n");
G
Geert Uytterhoeven 已提交
856
		atomic_dec_if_positive(&ps3fb.ext_flip);
857 858 859 860 861 862 863
		retval = 0;
		break;

	case PS3FB_IOCTL_FSEL:
		if (copy_from_user(&val, argp, sizeof(val)))
			break;

864
		dev_dbg(info->device, "PS3FB_IOCTL_FSEL:%d\n", val);
J
Jeremy Kerr 已提交
865
		acquire_console_sem();
866
		retval = ps3fb_sync(info, val);
J
Jeremy Kerr 已提交
867
		release_console_sem();
868 869 870 871 872 873 874 875 876 877 878
		break;

	default:
		retval = -ENOIOCTLCMD;
		break;
	}
	return retval;
}

static int ps3fbd(void *arg)
{
879 880
	struct fb_info *info = arg;

881
	set_freezable();
G
Geert Uytterhoeven 已提交
882 883 884 885 886
	while (!kthread_should_stop()) {
		try_to_freeze();
		set_current_state(TASK_INTERRUPTIBLE);
		if (ps3fb.is_kicked) {
			ps3fb.is_kicked = 0;
J
Jeremy Kerr 已提交
887
			acquire_console_sem();
888
			ps3fb_sync(info, 0);	/* single buffer */
J
Jeremy Kerr 已提交
889
			release_console_sem();
G
Geert Uytterhoeven 已提交
890 891
		}
		schedule();
892 893 894 895 896 897
	}
	return 0;
}

static irqreturn_t ps3fb_vsync_interrupt(int irq, void *ptr)
{
898
	struct device *dev = ptr;
899 900 901 902 903 904
	u64 v1;
	int status;
	struct display_head *head = &ps3fb.dinfo->display_head[1];

	status = lv1_gpu_context_intr(ps3fb.context_handle, &v1);
	if (status) {
905 906
		dev_err(dev, "%s: lv1_gpu_context_intr failed: %d\n", __func__,
			status);
907 908 909 910 911 912
		return IRQ_NONE;
	}

	if (v1 & (1 << GPU_INTR_STATUS_VSYNC_1)) {
		/* VSYNC */
		ps3fb.vblank_count = head->vblank_count;
G
Geert Uytterhoeven 已提交
913 914 915 916 917
		if (ps3fb.task && !ps3fb.is_blanked &&
		    !atomic_read(&ps3fb.ext_flip)) {
			ps3fb.is_kicked = 1;
			wake_up_process(ps3fb.task);
		}
918 919 920 921 922 923 924
		wake_up_interruptible(&ps3fb.wait_vsync);
	}

	return IRQ_HANDLED;
}


925
static int ps3fb_vsync_settings(struct gpu_driver_info *dinfo,
926
				struct device *dev)
927 928 929
{
	int error;

930 931 932 933 934
	dev_dbg(dev, "version_driver:%x\n", dinfo->version_driver);
	dev_dbg(dev, "irq outlet:%x\n", dinfo->irq.irq_outlet);
	dev_dbg(dev,
		"version_gpu: %x memory_size: %x ch: %x core_freq: %d "
		"mem_freq:%d\n",
935 936 937 938
		dinfo->version_gpu, dinfo->memory_size, dinfo->hardware_channel,
		dinfo->nvcore_frequency/1000000, dinfo->memory_frequency/1000000);

	if (dinfo->version_driver != GPU_DRIVER_INFO_VERSION) {
939 940
		dev_err(dev, "%s: version_driver err:%x\n", __func__,
			dinfo->version_driver);
941 942 943
		return -EINVAL;
	}

944 945
	error = ps3_irq_plug_setup(PS3_BINDING_CPU_ANY, dinfo->irq.irq_outlet,
				   &ps3fb.irq_no);
946
	if (error) {
947
		dev_err(dev, "%s: ps3_alloc_irq failed %d\n", __func__, error);
948 949 950 951
		return error;
	}

	error = request_irq(ps3fb.irq_no, ps3fb_vsync_interrupt, IRQF_DISABLED,
952
			    DEVICE_NAME, dev);
953
	if (error) {
954
		dev_err(dev, "%s: request_irq failed %d\n", __func__, error);
955
		ps3_irq_plug_destroy(ps3fb.irq_no);
956 957 958 959 960 961 962 963
		return error;
	}

	dinfo->irq.mask = (1 << GPU_INTR_STATUS_VSYNC_1) |
			  (1 << GPU_INTR_STATUS_FLIP_1);
	return 0;
}

964
static int ps3fb_xdr_settings(u64 xdr_lpar, struct device *dev)
965 966 967 968 969 970
{
	int status;

	status = lv1_gpu_context_iomap(ps3fb.context_handle, GPU_IOIF,
				       xdr_lpar, ps3fb_videomemory.size, 0);
	if (status) {
971 972
		dev_err(dev, "%s: lv1_gpu_context_iomap failed: %d\n",
			__func__, status);
973 974
		return -ENXIO;
	}
975 976
	dev_dbg(dev,
		"video:%p xdr_ea:%p ioif:%lx lpar:%lx phys:%lx size:%lx\n",
977 978 979 980 981
		ps3fb_videomemory.address, ps3fb.xdr_ea, GPU_IOIF, xdr_lpar,
		virt_to_abs(ps3fb.xdr_ea), ps3fb_videomemory.size);

	status = lv1_gpu_context_attribute(ps3fb.context_handle,
					   L1GPU_CONTEXT_ATTRIBUTE_FB_SETUP,
982 983
					   xdr_lpar, GPU_CMD_BUF_SIZE,
					   GPU_IOIF, 0);
984
	if (status) {
985 986 987
		dev_err(dev,
			"%s: lv1_gpu_context_attribute FB_SETUP failed: %d\n",
			__func__, status);
988 989 990 991 992 993 994 995
		return -ENXIO;
	}
	return 0;
}

static struct fb_ops ps3fb_ops = {
	.fb_open	= ps3fb_open,
	.fb_release	= ps3fb_release,
996 997
	.fb_read        = fb_sys_read,
	.fb_write       = fb_sys_write,
998 999 1000
	.fb_check_var	= ps3fb_check_var,
	.fb_set_par	= ps3fb_set_par,
	.fb_setcolreg	= ps3fb_setcolreg,
1001
	.fb_pan_display	= ps3fb_pan_display,
1002 1003 1004
	.fb_fillrect	= sys_fillrect,
	.fb_copyarea	= sys_copyarea,
	.fb_imageblit	= sys_imageblit,
1005 1006 1007 1008 1009 1010 1011
	.fb_mmap	= ps3fb_mmap,
	.fb_blank	= ps3fb_blank,
	.fb_ioctl	= ps3fb_ioctl,
	.fb_compat_ioctl = ps3fb_ioctl
};

static struct fb_fix_screeninfo ps3fb_fix __initdata = {
1012
	.id =		DEVICE_NAME,
1013 1014 1015 1016 1017
	.type =		FB_TYPE_PACKED_PIXELS,
	.visual =	FB_VISUAL_TRUECOLOR,
	.accel =	FB_ACCEL_NONE,
};

1018
static int ps3fb_set_sync(struct device *dev)
1019 1020 1021 1022 1023 1024 1025 1026
{
	int status;

#ifdef HEAD_A
	status = lv1_gpu_context_attribute(0x0,
					   L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_SYNC,
					   0, L1GPU_DISPLAY_SYNC_VSYNC, 0, 0);
	if (status) {
1027 1028 1029 1030
		dev_err(dev,
			"%s: lv1_gpu_context_attribute DISPLAY_SYNC failed: "
			"%d\n",
			__func__, status);
1031 1032 1033 1034 1035 1036 1037 1038 1039
		return -1;
	}
#endif
#ifdef HEAD_B
	status = lv1_gpu_context_attribute(0x0,
					   L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_SYNC,
					   1, L1GPU_DISPLAY_SYNC_VSYNC, 0, 0);

	if (status) {
1040 1041 1042 1043
		dev_err(dev,
			"%s: lv1_gpu_context_attribute DISPLAY_SYNC failed: "
			"%d\n",
			__func__, status);
1044 1045 1046 1047 1048 1049 1050
		return -1;
	}
#endif
	return 0;
}

static int __devinit ps3fb_probe(struct ps3_system_bus_device *dev)
1051 1052
{
	struct fb_info *info;
1053
	struct ps3fb_par *par;
1054
	int retval = -ENOMEM;
1055
	u32 xres, yres;
1056 1057 1058 1059 1060 1061
	u64 ddr_lpar = 0;
	u64 lpar_dma_control = 0;
	u64 lpar_driver_info = 0;
	u64 lpar_reports = 0;
	u64 lpar_reports_size = 0;
	u64 xdr_lpar;
1062
	int status, res_index;
G
Geert Uytterhoeven 已提交
1063
	struct task_struct *task;
1064
	unsigned long max_ps3fb_size;
1065

1066 1067 1068 1069 1070
	if (ps3fb_videomemory.size < GPU_CMD_BUF_SIZE) {
		dev_err(&dev->core, "%s: Not enough video memory\n", __func__);
		return -ENOMEM;
	}

1071 1072
	status = ps3_open_hv_device(dev);
	if (status) {
1073 1074
		dev_err(&dev->core, "%s: ps3_open_hv_device failed\n",
			__func__);
1075 1076 1077 1078 1079
		goto err;
	}

	if (!ps3fb_mode)
		ps3fb_mode = ps3av_get_mode();
1080
	dev_dbg(&dev->core, "ps3fb_mode: %d\n", ps3fb_mode);
1081 1082 1083

	if (ps3fb_mode > 0 &&
	    !ps3av_video_mode2res(ps3fb_mode, &xres, &yres)) {
1084 1085
		res_index = ps3fb_get_res_table(xres, yres, ps3fb_mode);
		dev_dbg(&dev->core, "res_index:%d\n", res_index);
1086
	} else
1087
		res_index = GPU_RES_INDEX;
1088 1089 1090 1091 1092

	atomic_set(&ps3fb.f_count, -1);	/* fbcon opens ps3fb */
	atomic_set(&ps3fb.ext_flip, 0);	/* for flip with vsync */
	init_waitqueue_head(&ps3fb.wait_vsync);

1093
	ps3fb_set_sync(&dev->core);
1094

1095 1096 1097 1098 1099 1100 1101
	max_ps3fb_size = _ALIGN_UP(GPU_IOIF, 256*1024*1024) - GPU_IOIF;
	if (ps3fb_videomemory.size > max_ps3fb_size) {
		dev_info(&dev->core, "Limiting ps3fb mem size to %lu bytes\n",
			 max_ps3fb_size);
		ps3fb_videomemory.size = max_ps3fb_size;
	}

1102
	/* get gpu context handle */
1103
	status = lv1_gpu_memory_allocate(ps3fb_videomemory.size, 0, 0, 0, 0,
1104 1105
					 &ps3fb.memory_handle, &ddr_lpar);
	if (status) {
1106 1107
		dev_err(&dev->core, "%s: lv1_gpu_memory_allocate failed: %d\n",
			__func__, status);
1108 1109
		goto err;
	}
1110
	dev_dbg(&dev->core, "ddr:lpar:0x%lx\n", ddr_lpar);
1111 1112 1113 1114 1115 1116

	status = lv1_gpu_context_allocate(ps3fb.memory_handle, 0,
					  &ps3fb.context_handle,
					  &lpar_dma_control, &lpar_driver_info,
					  &lpar_reports, &lpar_reports_size);
	if (status) {
1117 1118 1119
		dev_err(&dev->core,
			"%s: lv1_gpu_context_attribute failed: %d\n", __func__,
			status);
1120 1121 1122 1123 1124 1125
		goto err_gpu_memory_free;
	}

	/* vsync interrupt */
	ps3fb.dinfo = ioremap(lpar_driver_info, 128 * 1024);
	if (!ps3fb.dinfo) {
1126
		dev_err(&dev->core, "%s: ioremap failed\n", __func__);
1127 1128 1129
		goto err_gpu_context_free;
	}

1130
	retval = ps3fb_vsync_settings(ps3fb.dinfo, &dev->core);
1131 1132 1133
	if (retval)
		goto err_iounmap_dinfo;

1134
	/* XDR frame buffer */
1135 1136
	ps3fb.xdr_ea = ps3fb_videomemory.address;
	xdr_lpar = ps3_mm_phys_to_lpar(__pa(ps3fb.xdr_ea));
1137 1138 1139 1140

	/* Clear memory to prevent kernel info leakage into userspace */
	memset(ps3fb.xdr_ea, 0, ps3fb_videomemory.size);

1141 1142 1143 1144 1145 1146 1147 1148
	/*
	 * The GPU command buffer is at the start of video memory
	 * As we don't use the full command buffer, we can put the actual
	 * frame buffer at offset GPU_FB_START and save some precious XDR
	 * memory
	 */
	ps3fb.xdr_ea += GPU_FB_START;
	ps3fb.xdr_size = ps3fb_videomemory.size - GPU_FB_START;
1149

1150
	retval = ps3fb_xdr_settings(xdr_lpar, &dev->core);
1151 1152 1153
	if (retval)
		goto err_free_irq;

1154
	info = framebuffer_alloc(sizeof(struct ps3fb_par), &dev->core);
1155 1156 1157
	if (!info)
		goto err_free_irq;

1158 1159 1160 1161 1162 1163
	par = info->par;
	par->mode_id = ~ps3fb_mode;	/* != ps3fb_mode, to trigger change */
	par->new_mode_id = ps3fb_mode;
	par->res_index = res_index;
	par->num_frames = 1;

1164
	info->screen_base = (char __iomem *)ps3fb.xdr_ea;
1165 1166 1167
	info->fbops = &ps3fb_ops;

	info->fix = ps3fb_fix;
1168 1169
	info->fix.smem_start = virt_to_abs(ps3fb.xdr_ea);
	info->fix.smem_len = ps3fb.xdr_size;
1170
	info->pseudo_palette = par->pseudo_palette;
1171 1172
	info->flags = FBINFO_DEFAULT | FBINFO_READS_FAST |
		      FBINFO_HWACCEL_XPAN | FBINFO_HWACCEL_YPAN;
1173 1174 1175 1176 1177 1178

	retval = fb_alloc_cmap(&info->cmap, 256, 0);
	if (retval < 0)
		goto err_framebuffer_release;

	if (!fb_find_mode(&info->var, info, mode_option, ps3fb_modedb,
1179 1180
			  ARRAY_SIZE(ps3fb_modedb),
			  ps3fb_default_mode(par->new_mode_id), 32)) {
1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191
		retval = -EINVAL;
		goto err_fb_dealloc;
	}

	fb_videomode_to_modelist(ps3fb_modedb, ARRAY_SIZE(ps3fb_modedb),
				 &info->modelist);

	retval = register_framebuffer(info);
	if (retval < 0)
		goto err_fb_dealloc;

1192
	dev->core.driver_data = info;
1193

1194 1195
	dev_info(info->device, "%s %s, using %lu KiB of video memory\n",
		 dev_driver_string(info->dev), info->dev->bus_id,
1196
		 ps3fb.xdr_size >> 10);
1197

1198
	task = kthread_run(ps3fbd, info, DEVICE_NAME);
G
Geert Uytterhoeven 已提交
1199 1200 1201 1202 1203 1204
	if (IS_ERR(task)) {
		retval = PTR_ERR(task);
		goto err_unregister_framebuffer;
	}

	ps3fb.task = task;
1205
	ps3av_register_flip_ctl(ps3fb_flip_ctl, &ps3fb);
G
Geert Uytterhoeven 已提交
1206

1207 1208
	return 0;

G
Geert Uytterhoeven 已提交
1209 1210
err_unregister_framebuffer:
	unregister_framebuffer(info);
1211 1212 1213 1214 1215
err_fb_dealloc:
	fb_dealloc_cmap(&info->cmap);
err_framebuffer_release:
	framebuffer_release(info);
err_free_irq:
G
Geoff Levand 已提交
1216
	free_irq(ps3fb.irq_no, &dev->core);
1217
	ps3_irq_plug_destroy(ps3fb.irq_no);
1218 1219 1220 1221 1222 1223 1224 1225 1226 1227
err_iounmap_dinfo:
	iounmap((u8 __iomem *)ps3fb.dinfo);
err_gpu_context_free:
	lv1_gpu_context_free(ps3fb.context_handle);
err_gpu_memory_free:
	lv1_gpu_memory_free(ps3fb.memory_handle);
err:
	return retval;
}

1228
static int ps3fb_shutdown(struct ps3_system_bus_device *dev)
1229
{
1230 1231 1232
	int status;
	struct fb_info *info = dev->core.driver_data;

1233
	dev_dbg(&dev->core, " -> %s:%d\n", __func__, __LINE__);
1234 1235

	ps3fb_flip_ctl(0, &ps3fb);	/* flip off */
1236 1237
	ps3fb.dinfo->irq.mask = 0;

1238
	ps3av_register_flip_ctl(NULL, NULL);
G
Geert Uytterhoeven 已提交
1239 1240 1241 1242 1243
	if (ps3fb.task) {
		struct task_struct *task = ps3fb.task;
		ps3fb.task = NULL;
		kthread_stop(task);
	}
1244
	if (ps3fb.irq_no) {
G
Geoff Levand 已提交
1245
		free_irq(ps3fb.irq_no, &dev->core);
1246
		ps3_irq_plug_destroy(ps3fb.irq_no);
1247
	}
1248 1249 1250 1251 1252 1253
	if (info) {
		unregister_framebuffer(info);
		fb_dealloc_cmap(&info->cmap);
		framebuffer_release(info);
		info = dev->core.driver_data = NULL;
	}
1254 1255 1256 1257
	iounmap((u8 __iomem *)ps3fb.dinfo);

	status = lv1_gpu_context_free(ps3fb.context_handle);
	if (status)
1258 1259
		dev_dbg(&dev->core, "lv1_gpu_context_free failed: %d\n",
			status);
1260 1261 1262

	status = lv1_gpu_memory_free(ps3fb.memory_handle);
	if (status)
1263 1264
		dev_dbg(&dev->core, "lv1_gpu_memory_free failed: %d\n",
			status);
1265

1266
	ps3_close_hv_device(dev);
1267
	dev_dbg(&dev->core, " <- %s:%d\n", __func__, __LINE__);
1268 1269 1270 1271

	return 0;
}

1272 1273 1274 1275 1276 1277 1278
static struct ps3_system_bus_driver ps3fb_driver = {
	.match_id	= PS3_MATCH_ID_GRAPHICS,
	.core.name	= DEVICE_NAME,
	.core.owner	= THIS_MODULE,
	.probe		= ps3fb_probe,
	.remove		= ps3fb_shutdown,
	.shutdown	= ps3fb_shutdown,
1279 1280
};

1281
static int __init ps3fb_setup(void)
1282
{
1283
	char *options;
1284

1285
#ifdef MODULE
1286 1287 1288
	return 0;
#endif

1289 1290
	if (fb_get_options(DEVICE_NAME, &options))
		return -ENXIO;
1291

1292 1293
	if (!options || !*options)
		return 0;
1294

1295 1296
	while (1) {
		char *this_opt = strsep(&options, ",");
1297

1298 1299 1300 1301 1302 1303 1304 1305
		if (!this_opt)
			break;
		if (!*this_opt)
			continue;
		if (!strncmp(this_opt, "mode:", 5))
			ps3fb_mode = simple_strtoul(this_opt + 5, NULL, 0);
		else
			mode_option = this_opt;
1306
	}
1307 1308
	return 0;
}
1309

1310 1311 1312 1313
static int __init ps3fb_init(void)
{
	if (!ps3fb_videomemory.address ||  ps3fb_setup())
		return -ENXIO;
1314

1315
	return ps3_system_bus_driver_register(&ps3fb_driver);
1316 1317 1318 1319
}

static void __exit ps3fb_exit(void)
{
1320
	pr_debug(" -> %s:%d\n", __func__, __LINE__);
1321
	ps3_system_bus_driver_unregister(&ps3fb_driver);
1322
	pr_debug(" <- %s:%d\n", __func__, __LINE__);
1323 1324
}

1325
module_init(ps3fb_init);
1326 1327 1328
module_exit(ps3fb_exit);

MODULE_LICENSE("GPL");
1329 1330 1331
MODULE_DESCRIPTION("PS3 GPU Frame Buffer Driver");
MODULE_AUTHOR("Sony Computer Entertainment Inc.");
MODULE_ALIAS(PS3_MODULE_ALIAS_GRAPHICS);