[POWERPC] cell: add missing '\n'
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / video / ps3fb.c
blob9c56c492a6939b6dda69fc454299dbb134771343
1 /*
2 * linux/drivers/video/ps3fb.c -- PS3 GPU frame buffer device
4 * Copyright (C) 2006 Sony Computer Entertainment Inc.
5 * Copyright 2006, 2007 Sony Corporation
7 * This file is based on :
9 * linux/drivers/video/vfb.c -- Virtual frame buffer device
11 * Copyright (C) 2002 James Simmons
13 * Copyright (C) 1997 Geert Uytterhoeven
15 * This file is subject to the terms and conditions of the GNU General Public
16 * License. See the file COPYING in the main directory of this archive for
17 * more details.
20 #include <linux/module.h>
21 #include <linux/kernel.h>
22 #include <linux/errno.h>
23 #include <linux/string.h>
24 #include <linux/mm.h>
25 #include <linux/interrupt.h>
26 #include <linux/console.h>
27 #include <linux/ioctl.h>
28 #include <linux/kthread.h>
29 #include <linux/freezer.h>
30 #include <linux/uaccess.h>
31 #include <linux/fb.h>
32 #include <linux/init.h>
34 #include <asm/abs_addr.h>
35 #include <asm/lv1call.h>
36 #include <asm/ps3av.h>
37 #include <asm/ps3fb.h>
38 #include <asm/ps3.h>
41 #define DEVICE_NAME "ps3fb"
43 #define L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_SYNC 0x101
44 #define L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_FLIP 0x102
45 #define L1GPU_CONTEXT_ATTRIBUTE_FB_SETUP 0x600
46 #define L1GPU_CONTEXT_ATTRIBUTE_FB_BLIT 0x601
47 #define L1GPU_CONTEXT_ATTRIBUTE_FB_BLIT_SYNC 0x602
49 #define L1GPU_FB_BLIT_WAIT_FOR_COMPLETION (1ULL << 32)
51 #define L1GPU_DISPLAY_SYNC_HSYNC 1
52 #define L1GPU_DISPLAY_SYNC_VSYNC 2
54 #define GPU_CMD_BUF_SIZE (64 * 1024)
55 #define GPU_IOIF (0x0d000000UL)
56 #define GPU_ALIGN_UP(x) _ALIGN_UP((x), 64)
57 #define GPU_MAX_LINE_LENGTH (65536 - 64)
59 #define PS3FB_FULL_MODE_BIT 0x80
61 #define GPU_INTR_STATUS_VSYNC_0 0 /* vsync on head A */
62 #define GPU_INTR_STATUS_VSYNC_1 1 /* vsync on head B */
63 #define GPU_INTR_STATUS_FLIP_0 3 /* flip head A */
64 #define GPU_INTR_STATUS_FLIP_1 4 /* flip head B */
65 #define GPU_INTR_STATUS_QUEUE_0 5 /* queue head A */
66 #define GPU_INTR_STATUS_QUEUE_1 6 /* queue head B */
68 #define GPU_DRIVER_INFO_VERSION 0x211
70 /* gpu internals */
71 struct display_head {
72 u64 be_time_stamp;
73 u32 status;
74 u32 offset;
75 u32 res1;
76 u32 res2;
77 u32 field;
78 u32 reserved1;
80 u64 res3;
81 u32 raster;
83 u64 vblank_count;
84 u32 field_vsync;
85 u32 reserved2;
88 struct gpu_irq {
89 u32 irq_outlet;
90 u32 status;
91 u32 mask;
92 u32 video_cause;
93 u32 graph_cause;
94 u32 user_cause;
96 u32 res1;
97 u64 res2;
99 u32 reserved[4];
102 struct gpu_driver_info {
103 u32 version_driver;
104 u32 version_gpu;
105 u32 memory_size;
106 u32 hardware_channel;
108 u32 nvcore_frequency;
109 u32 memory_frequency;
111 u32 reserved[1063];
112 struct display_head display_head[8];
113 struct gpu_irq irq;
116 struct ps3fb_priv {
117 unsigned int irq_no;
119 u64 context_handle, memory_handle;
120 void *xdr_ea;
121 size_t xdr_size;
122 struct gpu_driver_info *dinfo;
124 u64 vblank_count; /* frame count */
125 wait_queue_head_t wait_vsync;
127 atomic_t ext_flip; /* on/off flip with vsync */
128 atomic_t f_count; /* fb_open count */
129 int is_blanked;
130 int is_kicked;
131 struct task_struct *task;
133 static struct ps3fb_priv ps3fb;
135 struct ps3fb_par {
136 u32 pseudo_palette[16];
137 int mode_id, new_mode_id;
138 int res_index;
139 unsigned int num_frames; /* num of frame buffers */
140 unsigned int width;
141 unsigned int height;
142 unsigned long full_offset; /* start of fullscreen DDR fb */
143 unsigned long fb_offset; /* start of actual DDR fb */
144 unsigned long pan_offset;
147 struct ps3fb_res_table {
148 u32 xres;
149 u32 yres;
150 u32 xoff;
151 u32 yoff;
152 u32 type;
154 #define PS3FB_RES_FULL 1
155 static const struct ps3fb_res_table ps3fb_res[] = {
156 /* res_x,y margin_x,y full */
157 { 720, 480, 72, 48 , 0},
158 { 720, 576, 72, 58 , 0},
159 { 1280, 720, 78, 38 , 0},
160 { 1920, 1080, 116, 58 , 0},
161 /* full mode */
162 { 720, 480, 0, 0 , PS3FB_RES_FULL},
163 { 720, 576, 0, 0 , PS3FB_RES_FULL},
164 { 1280, 720, 0, 0 , PS3FB_RES_FULL},
165 { 1920, 1080, 0, 0 , PS3FB_RES_FULL},
166 /* vesa: normally full mode */
167 { 1280, 768, 0, 0 , 0},
168 { 1280, 1024, 0, 0 , 0},
169 { 1920, 1200, 0, 0 , 0},
170 { 0, 0, 0, 0 , 0} };
172 /* default resolution */
173 #define GPU_RES_INDEX 0 /* 720 x 480 */
175 static const struct fb_videomode ps3fb_modedb[] = {
176 /* 60 Hz broadcast modes (modes "1" to "5") */
178 /* 480i */
179 "480i", 60, 576, 384, 74074, 130, 89, 78, 57, 63, 6,
180 FB_SYNC_BROADCAST, FB_VMODE_INTERLACED
181 }, {
182 /* 480p */
183 "480p", 60, 576, 384, 37037, 130, 89, 78, 57, 63, 6,
184 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
185 }, {
186 /* 720p */
187 "720p", 60, 1124, 644, 13481, 298, 148, 57, 44, 80, 5,
188 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
189 }, {
190 /* 1080i */
191 "1080i", 60, 1688, 964, 13481, 264, 160, 94, 62, 88, 5,
192 FB_SYNC_BROADCAST, FB_VMODE_INTERLACED
193 }, {
194 /* 1080p */
195 "1080p", 60, 1688, 964, 6741, 264, 160, 94, 62, 88, 5,
196 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
199 /* 50 Hz broadcast modes (modes "6" to "10") */
201 /* 576i */
202 "576i", 50, 576, 460, 74074, 142, 83, 97, 63, 63, 5,
203 FB_SYNC_BROADCAST, FB_VMODE_INTERLACED
204 }, {
205 /* 576p */
206 "576p", 50, 576, 460, 37037, 142, 83, 97, 63, 63, 5,
207 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
208 }, {
209 /* 720p */
210 "720p", 50, 1124, 644, 13468, 298, 478, 57, 44, 80, 5,
211 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
212 }, {
213 /* 1080 */
214 "1080i", 50, 1688, 964, 13468, 264, 600, 94, 62, 88, 5,
215 FB_SYNC_BROADCAST, FB_VMODE_INTERLACED
216 }, {
217 /* 1080p */
218 "1080p", 50, 1688, 964, 6734, 264, 600, 94, 62, 88, 5,
219 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
222 /* VESA modes (modes "11" to "13") */
224 /* WXGA */
225 "wxga", 60, 1280, 768, 12924, 160, 24, 29, 3, 136, 6,
226 0, FB_VMODE_NONINTERLACED,
227 FB_MODE_IS_VESA
228 }, {
229 /* SXGA */
230 "sxga", 60, 1280, 1024, 9259, 248, 48, 38, 1, 112, 3,
231 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED,
232 FB_MODE_IS_VESA
233 }, {
234 /* WUXGA */
235 "wuxga", 60, 1920, 1200, 6494, 80, 48, 26, 3, 32, 6,
236 FB_SYNC_HOR_HIGH_ACT, FB_VMODE_NONINTERLACED,
237 FB_MODE_IS_VESA
240 /* 60 Hz broadcast modes (full resolution versions of modes "1" to "5") */
242 /* 480if */
243 "480if", 60, 720, 480, 74074, 58, 17, 30, 9, 63, 6,
244 FB_SYNC_BROADCAST, FB_VMODE_INTERLACED
245 }, {
246 /* 480pf */
247 "480pf", 60, 720, 480, 37037, 58, 17, 30, 9, 63, 6,
248 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
249 }, {
250 /* 720pf */
251 "720pf", 60, 1280, 720, 13481, 220, 70, 19, 6, 80, 5,
252 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
253 }, {
254 /* 1080if */
255 "1080if", 60, 1920, 1080, 13481, 148, 44, 36, 4, 88, 5,
256 FB_SYNC_BROADCAST, FB_VMODE_INTERLACED
257 }, {
258 /* 1080pf */
259 "1080pf", 60, 1920, 1080, 6741, 148, 44, 36, 4, 88, 5,
260 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
263 /* 50 Hz broadcast modes (full resolution versions of modes "6" to "10") */
265 /* 576if */
266 "576if", 50, 720, 576, 74074, 70, 11, 39, 5, 63, 5,
267 FB_SYNC_BROADCAST, FB_VMODE_INTERLACED
268 }, {
269 /* 576pf */
270 "576pf", 50, 720, 576, 37037, 70, 11, 39, 5, 63, 5,
271 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
272 }, {
273 /* 720pf */
274 "720pf", 50, 1280, 720, 13468, 220, 400, 19, 6, 80, 5,
275 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
276 }, {
277 /* 1080if */
278 "1080f", 50, 1920, 1080, 13468, 148, 484, 36, 4, 88, 5,
279 FB_SYNC_BROADCAST, FB_VMODE_INTERLACED
280 }, {
281 /* 1080pf */
282 "1080pf", 50, 1920, 1080, 6734, 148, 484, 36, 4, 88, 5,
283 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
288 #define HEAD_A
289 #define HEAD_B
291 #define X_OFF(i) (ps3fb_res[i].xoff) /* left/right margin (pixel) */
292 #define Y_OFF(i) (ps3fb_res[i].yoff) /* top/bottom margin (pixel) */
293 #define WIDTH(i) (ps3fb_res[i].xres) /* width of FB */
294 #define HEIGHT(i) (ps3fb_res[i].yres) /* height of FB */
295 #define BPP 4 /* number of bytes per pixel */
297 /* Start of the virtual frame buffer (relative to fullscreen ) */
298 #define VP_OFF(i) ((WIDTH(i) * Y_OFF(i) + X_OFF(i)) * BPP)
301 static int ps3fb_mode;
302 module_param(ps3fb_mode, int, 0);
304 static char *mode_option __devinitdata;
306 static int ps3fb_get_res_table(u32 xres, u32 yres, int mode)
308 int full_mode;
309 unsigned int i;
310 u32 x, y, f;
312 full_mode = (mode & PS3FB_FULL_MODE_BIT) ? PS3FB_RES_FULL : 0;
313 for (i = 0;; i++) {
314 x = ps3fb_res[i].xres;
315 y = ps3fb_res[i].yres;
316 f = ps3fb_res[i].type;
318 if (!x) {
319 pr_debug("ERROR: ps3fb_get_res_table()\n");
320 return -1;
323 if (full_mode == PS3FB_RES_FULL && f != PS3FB_RES_FULL)
324 continue;
326 if (x == xres && (yres == 0 || y == yres))
327 break;
329 x = x - 2 * ps3fb_res[i].xoff;
330 y = y - 2 * ps3fb_res[i].yoff;
331 if (x == xres && (yres == 0 || y == yres))
332 break;
334 return i;
337 static unsigned int ps3fb_find_mode(const struct fb_var_screeninfo *var,
338 u32 *ddr_line_length, u32 *xdr_line_length)
340 unsigned int i, mode;
342 for (i = 0; i < ARRAY_SIZE(ps3fb_modedb); i++)
343 if (var->xres == ps3fb_modedb[i].xres &&
344 var->yres == ps3fb_modedb[i].yres &&
345 var->pixclock == ps3fb_modedb[i].pixclock &&
346 var->hsync_len == ps3fb_modedb[i].hsync_len &&
347 var->vsync_len == ps3fb_modedb[i].vsync_len &&
348 var->left_margin == ps3fb_modedb[i].left_margin &&
349 var->right_margin == ps3fb_modedb[i].right_margin &&
350 var->upper_margin == ps3fb_modedb[i].upper_margin &&
351 var->lower_margin == ps3fb_modedb[i].lower_margin &&
352 var->sync == ps3fb_modedb[i].sync &&
353 (var->vmode & FB_VMODE_MASK) == ps3fb_modedb[i].vmode)
354 goto found;
356 pr_debug("ps3fb_find_mode: mode not found\n");
357 return 0;
359 found:
360 /* Cropped broadcast modes use the full line length */
361 *ddr_line_length = ps3fb_modedb[i < 10 ? i + 13 : i].xres * BPP;
363 if (ps3_compare_firmware_version(1, 9, 0) >= 0) {
364 *xdr_line_length = GPU_ALIGN_UP(max(var->xres,
365 var->xres_virtual) * BPP);
366 if (*xdr_line_length > GPU_MAX_LINE_LENGTH)
367 *xdr_line_length = GPU_MAX_LINE_LENGTH;
368 } else
369 *xdr_line_length = *ddr_line_length;
371 /* Full broadcast modes have the full mode bit set */
372 mode = i > 12 ? (i - 12) | PS3FB_FULL_MODE_BIT : i + 1;
374 pr_debug("ps3fb_find_mode: mode %u\n", mode);
376 return mode;
379 static const struct fb_videomode *ps3fb_default_mode(int id)
381 u32 mode = id & PS3AV_MODE_MASK;
382 u32 flags;
384 if (mode < 1 || mode > 13)
385 return NULL;
387 flags = id & ~PS3AV_MODE_MASK;
389 if (mode <= 10 && flags & PS3FB_FULL_MODE_BIT) {
390 /* Full broadcast mode */
391 return &ps3fb_modedb[mode + 12];
394 return &ps3fb_modedb[mode - 1];
397 static void ps3fb_sync_image(struct device *dev, u64 frame_offset,
398 u64 dst_offset, u64 src_offset, u32 width,
399 u32 height, u32 dst_line_length,
400 u32 src_line_length)
402 int status;
403 u64 line_length;
405 line_length = dst_line_length;
406 if (src_line_length != dst_line_length)
407 line_length |= (u64)src_line_length << 32;
409 status = lv1_gpu_context_attribute(ps3fb.context_handle,
410 L1GPU_CONTEXT_ATTRIBUTE_FB_BLIT,
411 dst_offset, GPU_IOIF + src_offset,
412 L1GPU_FB_BLIT_WAIT_FOR_COMPLETION |
413 (width << 16) | height,
414 line_length);
415 if (status)
416 dev_err(dev,
417 "%s: lv1_gpu_context_attribute FB_BLIT failed: %d\n",
418 __func__, status);
419 #ifdef HEAD_A
420 status = lv1_gpu_context_attribute(ps3fb.context_handle,
421 L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_FLIP,
422 0, frame_offset, 0, 0);
423 if (status)
424 dev_err(dev, "%s: lv1_gpu_context_attribute FLIP failed: %d\n",
425 __func__, status);
426 #endif
427 #ifdef HEAD_B
428 status = lv1_gpu_context_attribute(ps3fb.context_handle,
429 L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_FLIP,
430 1, frame_offset, 0, 0);
431 if (status)
432 dev_err(dev, "%s: lv1_gpu_context_attribute FLIP failed: %d\n",
433 __func__, status);
434 #endif
437 static int ps3fb_sync(struct fb_info *info, u32 frame)
439 struct ps3fb_par *par = info->par;
440 int i, error = 0;
441 u32 ddr_line_length, xdr_line_length;
442 u64 ddr_base, xdr_base;
444 acquire_console_sem();
446 if (frame > par->num_frames - 1) {
447 dev_dbg(info->device, "%s: invalid frame number (%u)\n",
448 __func__, frame);
449 error = -EINVAL;
450 goto out;
453 i = par->res_index;
454 xdr_line_length = info->fix.line_length;
455 ddr_line_length = ps3fb_res[i].xres * BPP;
456 xdr_base = frame * info->var.yres_virtual * xdr_line_length;
457 ddr_base = frame * ps3fb_res[i].yres * ddr_line_length;
459 ps3fb_sync_image(info->device, ddr_base + par->full_offset,
460 ddr_base + par->fb_offset, xdr_base + par->pan_offset,
461 par->width, par->height, ddr_line_length,
462 xdr_line_length);
464 out:
465 release_console_sem();
466 return error;
469 static int ps3fb_open(struct fb_info *info, int user)
471 atomic_inc(&ps3fb.f_count);
472 return 0;
475 static int ps3fb_release(struct fb_info *info, int user)
477 if (atomic_dec_and_test(&ps3fb.f_count)) {
478 if (atomic_read(&ps3fb.ext_flip)) {
479 atomic_set(&ps3fb.ext_flip, 0);
480 ps3fb_sync(info, 0); /* single buffer */
483 return 0;
487 * Setting the video mode has been split into two parts.
488 * First part, xxxfb_check_var, must not write anything
489 * to hardware, it should only verify and adjust var.
490 * This means it doesn't alter par but it does use hardware
491 * data from it to check this var.
494 static int ps3fb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
496 u32 xdr_line_length, ddr_line_length;
497 int mode;
499 dev_dbg(info->device, "var->xres:%u info->var.xres:%u\n", var->xres,
500 info->var.xres);
501 dev_dbg(info->device, "var->yres:%u info->var.yres:%u\n", var->yres,
502 info->var.yres);
504 /* FIXME For now we do exact matches only */
505 mode = ps3fb_find_mode(var, &ddr_line_length, &xdr_line_length);
506 if (!mode)
507 return -EINVAL;
509 /* Virtual screen */
510 if (var->xres_virtual < var->xres)
511 var->xres_virtual = var->xres;
512 if (var->yres_virtual < var->yres)
513 var->yres_virtual = var->yres;
515 if (var->xres_virtual > xdr_line_length / BPP) {
516 dev_dbg(info->device,
517 "Horizontal virtual screen size too large\n");
518 return -EINVAL;
521 if (var->xoffset + var->xres > var->xres_virtual ||
522 var->yoffset + var->yres > var->yres_virtual) {
523 dev_dbg(info->device, "panning out-of-range\n");
524 return -EINVAL;
527 /* We support ARGB8888 only */
528 if (var->bits_per_pixel > 32 || var->grayscale ||
529 var->red.offset > 16 || var->green.offset > 8 ||
530 var->blue.offset > 0 || var->transp.offset > 24 ||
531 var->red.length > 8 || var->green.length > 8 ||
532 var->blue.length > 8 || var->transp.length > 8 ||
533 var->red.msb_right || var->green.msb_right ||
534 var->blue.msb_right || var->transp.msb_right || var->nonstd) {
535 dev_dbg(info->device, "We support ARGB8888 only\n");
536 return -EINVAL;
539 var->bits_per_pixel = 32;
540 var->red.offset = 16;
541 var->green.offset = 8;
542 var->blue.offset = 0;
543 var->transp.offset = 24;
544 var->red.length = 8;
545 var->green.length = 8;
546 var->blue.length = 8;
547 var->transp.length = 8;
548 var->red.msb_right = 0;
549 var->green.msb_right = 0;
550 var->blue.msb_right = 0;
551 var->transp.msb_right = 0;
553 /* Rotation is not supported */
554 if (var->rotate) {
555 dev_dbg(info->device, "Rotation is not supported\n");
556 return -EINVAL;
559 /* Memory limit */
560 if (var->yres_virtual * xdr_line_length > ps3fb.xdr_size) {
561 dev_dbg(info->device, "Not enough memory\n");
562 return -ENOMEM;
565 var->height = -1;
566 var->width = -1;
568 return 0;
572 * This routine actually sets the video mode.
575 static int ps3fb_set_par(struct fb_info *info)
577 struct ps3fb_par *par = info->par;
578 unsigned int mode, ddr_line_length, xdr_line_length, lines, maxlines;
579 int i;
580 unsigned long offset;
581 u64 dst;
583 dev_dbg(info->device, "xres:%d xv:%d yres:%d yv:%d clock:%d\n",
584 info->var.xres, info->var.xres_virtual,
585 info->var.yres, info->var.yres_virtual, info->var.pixclock);
587 mode = ps3fb_find_mode(&info->var, &ddr_line_length, &xdr_line_length);
588 if (!mode)
589 return -EINVAL;
591 i = ps3fb_get_res_table(info->var.xres, info->var.yres, mode);
592 par->res_index = i;
594 info->fix.smem_start = virt_to_abs(ps3fb.xdr_ea);
595 info->fix.smem_len = ps3fb.xdr_size;
596 info->fix.xpanstep = info->var.xres_virtual > info->var.xres ? 1 : 0;
597 info->fix.ypanstep = info->var.yres_virtual > info->var.yres ? 1 : 0;
598 info->fix.line_length = xdr_line_length;
600 info->screen_base = (char __iomem *)ps3fb.xdr_ea;
602 par->num_frames = ps3fb.xdr_size /
603 max(ps3fb_res[i].yres * ddr_line_length,
604 info->var.yres_virtual * xdr_line_length);
606 /* Keep the special bits we cannot set using fb_var_screeninfo */
607 par->new_mode_id = (par->new_mode_id & ~PS3AV_MODE_MASK) | mode;
609 par->width = info->var.xres;
610 par->height = info->var.yres;
611 offset = VP_OFF(i);
612 par->fb_offset = GPU_ALIGN_UP(offset);
613 par->full_offset = par->fb_offset - offset;
614 par->pan_offset = info->var.yoffset * xdr_line_length +
615 info->var.xoffset * BPP;
617 if (par->new_mode_id != par->mode_id) {
618 if (ps3av_set_video_mode(par->new_mode_id)) {
619 par->new_mode_id = par->mode_id;
620 return -EINVAL;
622 par->mode_id = par->new_mode_id;
625 /* Clear XDR frame buffer memory */
626 memset(ps3fb.xdr_ea, 0, ps3fb.xdr_size);
628 /* Clear DDR frame buffer memory */
629 lines = ps3fb_res[i].yres * par->num_frames;
630 if (par->full_offset)
631 lines++;
632 maxlines = ps3fb.xdr_size / ddr_line_length;
633 for (dst = 0; lines; dst += maxlines * ddr_line_length) {
634 unsigned int l = min(lines, maxlines);
635 ps3fb_sync_image(info->device, 0, dst, 0, ps3fb_res[i].xres, l,
636 ddr_line_length, ddr_line_length);
637 lines -= l;
640 return 0;
644 * Set a single color register. The values supplied are already
645 * rounded down to the hardware's capabilities (according to the
646 * entries in the var structure). Return != 0 for invalid regno.
649 static int ps3fb_setcolreg(unsigned int regno, unsigned int red,
650 unsigned int green, unsigned int blue,
651 unsigned int transp, struct fb_info *info)
653 if (regno >= 16)
654 return 1;
656 red >>= 8;
657 green >>= 8;
658 blue >>= 8;
659 transp >>= 8;
661 ((u32 *)info->pseudo_palette)[regno] = transp << 24 | red << 16 |
662 green << 8 | blue;
663 return 0;
666 static int ps3fb_pan_display(struct fb_var_screeninfo *var,
667 struct fb_info *info)
669 struct ps3fb_par *par = info->par;
671 par->pan_offset = var->yoffset * info->fix.line_length +
672 var->xoffset * BPP;
673 return 0;
677 * As we have a virtual frame buffer, we need our own mmap function
680 static int ps3fb_mmap(struct fb_info *info, struct vm_area_struct *vma)
682 unsigned long size, offset;
684 size = vma->vm_end - vma->vm_start;
685 offset = vma->vm_pgoff << PAGE_SHIFT;
686 if (offset + size > info->fix.smem_len)
687 return -EINVAL;
689 offset += info->fix.smem_start;
690 if (remap_pfn_range(vma, vma->vm_start, offset >> PAGE_SHIFT,
691 size, vma->vm_page_prot))
692 return -EAGAIN;
694 dev_dbg(info->device, "ps3fb: mmap framebuffer P(%lx)->V(%lx)\n",
695 offset, vma->vm_start);
696 return 0;
700 * Blank the display
703 static int ps3fb_blank(int blank, struct fb_info *info)
705 int retval;
707 dev_dbg(info->device, "%s: blank:%d\n", __func__, blank);
708 switch (blank) {
709 case FB_BLANK_POWERDOWN:
710 case FB_BLANK_HSYNC_SUSPEND:
711 case FB_BLANK_VSYNC_SUSPEND:
712 case FB_BLANK_NORMAL:
713 retval = ps3av_video_mute(1); /* mute on */
714 if (!retval)
715 ps3fb.is_blanked = 1;
716 break;
718 default: /* unblank */
719 retval = ps3av_video_mute(0); /* mute off */
720 if (!retval)
721 ps3fb.is_blanked = 0;
722 break;
724 return retval;
727 static int ps3fb_get_vblank(struct fb_vblank *vblank)
729 memset(vblank, 0, sizeof(*vblank));
730 vblank->flags = FB_VBLANK_HAVE_VSYNC;
731 return 0;
734 static int ps3fb_wait_for_vsync(u32 crtc)
736 int ret;
737 u64 count;
739 count = ps3fb.vblank_count;
740 ret = wait_event_interruptible_timeout(ps3fb.wait_vsync,
741 count != ps3fb.vblank_count,
742 HZ / 10);
743 if (!ret)
744 return -ETIMEDOUT;
746 return 0;
749 static void ps3fb_flip_ctl(int on, void *data)
751 struct ps3fb_priv *priv = data;
752 if (on)
753 atomic_dec_if_positive(&priv->ext_flip);
754 else
755 atomic_inc(&priv->ext_flip);
760 * ioctl
763 static int ps3fb_ioctl(struct fb_info *info, unsigned int cmd,
764 unsigned long arg)
766 void __user *argp = (void __user *)arg;
767 u32 val;
768 int retval = -EFAULT;
770 switch (cmd) {
771 case FBIOGET_VBLANK:
773 struct fb_vblank vblank;
774 dev_dbg(info->device, "FBIOGET_VBLANK:\n");
775 retval = ps3fb_get_vblank(&vblank);
776 if (retval)
777 break;
779 if (copy_to_user(argp, &vblank, sizeof(vblank)))
780 retval = -EFAULT;
781 break;
784 case FBIO_WAITFORVSYNC:
786 u32 crt;
787 dev_dbg(info->device, "FBIO_WAITFORVSYNC:\n");
788 if (get_user(crt, (u32 __user *) arg))
789 break;
791 retval = ps3fb_wait_for_vsync(crt);
792 break;
795 case PS3FB_IOCTL_SETMODE:
797 struct ps3fb_par *par = info->par;
798 const struct fb_videomode *mode;
799 struct fb_var_screeninfo var;
801 if (copy_from_user(&val, argp, sizeof(val)))
802 break;
804 if (!(val & PS3AV_MODE_MASK)) {
805 u32 id = ps3av_get_auto_mode();
806 if (id > 0)
807 val = (val & ~PS3AV_MODE_MASK) | id;
809 dev_dbg(info->device, "PS3FB_IOCTL_SETMODE:%x\n", val);
810 retval = -EINVAL;
811 mode = ps3fb_default_mode(val);
812 if (mode) {
813 var = info->var;
814 fb_videomode_to_var(&var, mode);
815 acquire_console_sem();
816 info->flags |= FBINFO_MISC_USEREVENT;
817 /* Force, in case only special bits changed */
818 var.activate |= FB_ACTIVATE_FORCE;
819 par->new_mode_id = val;
820 retval = fb_set_var(info, &var);
821 info->flags &= ~FBINFO_MISC_USEREVENT;
822 release_console_sem();
824 break;
827 case PS3FB_IOCTL_GETMODE:
828 val = ps3av_get_mode();
829 dev_dbg(info->device, "PS3FB_IOCTL_GETMODE:%x\n", val);
830 if (!copy_to_user(argp, &val, sizeof(val)))
831 retval = 0;
832 break;
834 case PS3FB_IOCTL_SCREENINFO:
836 struct ps3fb_par *par = info->par;
837 struct ps3fb_ioctl_res res;
838 dev_dbg(info->device, "PS3FB_IOCTL_SCREENINFO:\n");
839 res.xres = info->fix.line_length / BPP;
840 res.yres = info->var.yres_virtual;
841 res.xoff = (res.xres - info->var.xres) / 2;
842 res.yoff = (res.yres - info->var.yres) / 2;
843 res.num_frames = par->num_frames;
844 if (!copy_to_user(argp, &res, sizeof(res)))
845 retval = 0;
846 break;
849 case PS3FB_IOCTL_ON:
850 dev_dbg(info->device, "PS3FB_IOCTL_ON:\n");
851 atomic_inc(&ps3fb.ext_flip);
852 retval = 0;
853 break;
855 case PS3FB_IOCTL_OFF:
856 dev_dbg(info->device, "PS3FB_IOCTL_OFF:\n");
857 atomic_dec_if_positive(&ps3fb.ext_flip);
858 retval = 0;
859 break;
861 case PS3FB_IOCTL_FSEL:
862 if (copy_from_user(&val, argp, sizeof(val)))
863 break;
865 dev_dbg(info->device, "PS3FB_IOCTL_FSEL:%d\n", val);
866 retval = ps3fb_sync(info, val);
867 break;
869 default:
870 retval = -ENOIOCTLCMD;
871 break;
873 return retval;
876 static int ps3fbd(void *arg)
878 struct fb_info *info = arg;
880 set_freezable();
881 while (!kthread_should_stop()) {
882 try_to_freeze();
883 set_current_state(TASK_INTERRUPTIBLE);
884 if (ps3fb.is_kicked) {
885 ps3fb.is_kicked = 0;
886 ps3fb_sync(info, 0); /* single buffer */
888 schedule();
890 return 0;
893 static irqreturn_t ps3fb_vsync_interrupt(int irq, void *ptr)
895 struct device *dev = ptr;
896 u64 v1;
897 int status;
898 struct display_head *head = &ps3fb.dinfo->display_head[1];
900 status = lv1_gpu_context_intr(ps3fb.context_handle, &v1);
901 if (status) {
902 dev_err(dev, "%s: lv1_gpu_context_intr failed: %d\n", __func__,
903 status);
904 return IRQ_NONE;
907 if (v1 & (1 << GPU_INTR_STATUS_VSYNC_1)) {
908 /* VSYNC */
909 ps3fb.vblank_count = head->vblank_count;
910 if (ps3fb.task && !ps3fb.is_blanked &&
911 !atomic_read(&ps3fb.ext_flip)) {
912 ps3fb.is_kicked = 1;
913 wake_up_process(ps3fb.task);
915 wake_up_interruptible(&ps3fb.wait_vsync);
918 return IRQ_HANDLED;
922 static int ps3fb_vsync_settings(struct gpu_driver_info *dinfo,
923 struct device *dev)
925 int error;
927 dev_dbg(dev, "version_driver:%x\n", dinfo->version_driver);
928 dev_dbg(dev, "irq outlet:%x\n", dinfo->irq.irq_outlet);
929 dev_dbg(dev,
930 "version_gpu: %x memory_size: %x ch: %x core_freq: %d "
931 "mem_freq:%d\n",
932 dinfo->version_gpu, dinfo->memory_size, dinfo->hardware_channel,
933 dinfo->nvcore_frequency/1000000, dinfo->memory_frequency/1000000);
935 if (dinfo->version_driver != GPU_DRIVER_INFO_VERSION) {
936 dev_err(dev, "%s: version_driver err:%x\n", __func__,
937 dinfo->version_driver);
938 return -EINVAL;
941 error = ps3_irq_plug_setup(PS3_BINDING_CPU_ANY, dinfo->irq.irq_outlet,
942 &ps3fb.irq_no);
943 if (error) {
944 dev_err(dev, "%s: ps3_alloc_irq failed %d\n", __func__, error);
945 return error;
948 error = request_irq(ps3fb.irq_no, ps3fb_vsync_interrupt, IRQF_DISABLED,
949 DEVICE_NAME, dev);
950 if (error) {
951 dev_err(dev, "%s: request_irq failed %d\n", __func__, error);
952 ps3_irq_plug_destroy(ps3fb.irq_no);
953 return error;
956 dinfo->irq.mask = (1 << GPU_INTR_STATUS_VSYNC_1) |
957 (1 << GPU_INTR_STATUS_FLIP_1);
958 return 0;
961 static int ps3fb_xdr_settings(u64 xdr_lpar, struct device *dev)
963 int status;
965 status = lv1_gpu_context_iomap(ps3fb.context_handle, GPU_IOIF,
966 xdr_lpar, ps3fb_videomemory.size, 0);
967 if (status) {
968 dev_err(dev, "%s: lv1_gpu_context_iomap failed: %d\n",
969 __func__, status);
970 return -ENXIO;
972 dev_dbg(dev,
973 "video:%p xdr_ea:%p ioif:%lx lpar:%lx phys:%lx size:%lx\n",
974 ps3fb_videomemory.address, ps3fb.xdr_ea, GPU_IOIF, xdr_lpar,
975 virt_to_abs(ps3fb.xdr_ea), ps3fb_videomemory.size);
977 status = lv1_gpu_context_attribute(ps3fb.context_handle,
978 L1GPU_CONTEXT_ATTRIBUTE_FB_SETUP,
979 xdr_lpar + ps3fb.xdr_size,
980 GPU_CMD_BUF_SIZE,
981 GPU_IOIF + ps3fb.xdr_size, 0);
982 if (status) {
983 dev_err(dev,
984 "%s: lv1_gpu_context_attribute FB_SETUP failed: %d\n",
985 __func__, status);
986 return -ENXIO;
988 return 0;
991 static struct fb_ops ps3fb_ops = {
992 .fb_open = ps3fb_open,
993 .fb_release = ps3fb_release,
994 .fb_read = fb_sys_read,
995 .fb_write = fb_sys_write,
996 .fb_check_var = ps3fb_check_var,
997 .fb_set_par = ps3fb_set_par,
998 .fb_setcolreg = ps3fb_setcolreg,
999 .fb_pan_display = ps3fb_pan_display,
1000 .fb_fillrect = sys_fillrect,
1001 .fb_copyarea = sys_copyarea,
1002 .fb_imageblit = sys_imageblit,
1003 .fb_mmap = ps3fb_mmap,
1004 .fb_blank = ps3fb_blank,
1005 .fb_ioctl = ps3fb_ioctl,
1006 .fb_compat_ioctl = ps3fb_ioctl
1009 static struct fb_fix_screeninfo ps3fb_fix __initdata = {
1010 .id = DEVICE_NAME,
1011 .type = FB_TYPE_PACKED_PIXELS,
1012 .visual = FB_VISUAL_TRUECOLOR,
1013 .accel = FB_ACCEL_NONE,
1016 static int ps3fb_set_sync(struct device *dev)
1018 int status;
1020 #ifdef HEAD_A
1021 status = lv1_gpu_context_attribute(0x0,
1022 L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_SYNC,
1023 0, L1GPU_DISPLAY_SYNC_VSYNC, 0, 0);
1024 if (status) {
1025 dev_err(dev,
1026 "%s: lv1_gpu_context_attribute DISPLAY_SYNC failed: "
1027 "%d\n",
1028 __func__, status);
1029 return -1;
1031 #endif
1032 #ifdef HEAD_B
1033 status = lv1_gpu_context_attribute(0x0,
1034 L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_SYNC,
1035 1, L1GPU_DISPLAY_SYNC_VSYNC, 0, 0);
1037 if (status) {
1038 dev_err(dev,
1039 "%s: lv1_gpu_context_attribute DISPLAY_SYNC failed: "
1040 "%d\n",
1041 __func__, status);
1042 return -1;
1044 #endif
1045 return 0;
1048 static int __devinit ps3fb_probe(struct ps3_system_bus_device *dev)
1050 struct fb_info *info;
1051 struct ps3fb_par *par;
1052 int retval = -ENOMEM;
1053 u32 xres, yres;
1054 u64 ddr_lpar = 0;
1055 u64 lpar_dma_control = 0;
1056 u64 lpar_driver_info = 0;
1057 u64 lpar_reports = 0;
1058 u64 lpar_reports_size = 0;
1059 u64 xdr_lpar;
1060 int status, res_index;
1061 struct task_struct *task;
1062 unsigned long max_ps3fb_size;
1064 status = ps3_open_hv_device(dev);
1065 if (status) {
1066 dev_err(&dev->core, "%s: ps3_open_hv_device failed\n",
1067 __func__);
1068 goto err;
1071 if (!ps3fb_mode)
1072 ps3fb_mode = ps3av_get_mode();
1073 dev_dbg(&dev->core, "ps3av_mode:%d\n", ps3fb_mode);
1075 if (ps3fb_mode > 0 &&
1076 !ps3av_video_mode2res(ps3fb_mode, &xres, &yres)) {
1077 res_index = ps3fb_get_res_table(xres, yres, ps3fb_mode);
1078 dev_dbg(&dev->core, "res_index:%d\n", res_index);
1079 } else
1080 res_index = GPU_RES_INDEX;
1082 atomic_set(&ps3fb.f_count, -1); /* fbcon opens ps3fb */
1083 atomic_set(&ps3fb.ext_flip, 0); /* for flip with vsync */
1084 init_waitqueue_head(&ps3fb.wait_vsync);
1086 ps3fb_set_sync(&dev->core);
1088 max_ps3fb_size = _ALIGN_UP(GPU_IOIF, 256*1024*1024) - GPU_IOIF;
1089 if (ps3fb_videomemory.size > max_ps3fb_size) {
1090 dev_info(&dev->core, "Limiting ps3fb mem size to %lu bytes\n",
1091 max_ps3fb_size);
1092 ps3fb_videomemory.size = max_ps3fb_size;
1095 /* get gpu context handle */
1096 status = lv1_gpu_memory_allocate(ps3fb_videomemory.size, 0, 0, 0, 0,
1097 &ps3fb.memory_handle, &ddr_lpar);
1098 if (status) {
1099 dev_err(&dev->core, "%s: lv1_gpu_memory_allocate failed: %d\n",
1100 __func__, status);
1101 goto err;
1103 dev_dbg(&dev->core, "ddr:lpar:0x%lx\n", ddr_lpar);
1105 status = lv1_gpu_context_allocate(ps3fb.memory_handle, 0,
1106 &ps3fb.context_handle,
1107 &lpar_dma_control, &lpar_driver_info,
1108 &lpar_reports, &lpar_reports_size);
1109 if (status) {
1110 dev_err(&dev->core,
1111 "%s: lv1_gpu_context_attribute failed: %d\n", __func__,
1112 status);
1113 goto err_gpu_memory_free;
1116 /* vsync interrupt */
1117 ps3fb.dinfo = ioremap(lpar_driver_info, 128 * 1024);
1118 if (!ps3fb.dinfo) {
1119 dev_err(&dev->core, "%s: ioremap failed\n", __func__);
1120 goto err_gpu_context_free;
1123 retval = ps3fb_vsync_settings(ps3fb.dinfo, &dev->core);
1124 if (retval)
1125 goto err_iounmap_dinfo;
1127 /* XDR frame buffer */
1128 ps3fb.xdr_ea = ps3fb_videomemory.address;
1129 xdr_lpar = ps3_mm_phys_to_lpar(__pa(ps3fb.xdr_ea));
1131 /* Clear memory to prevent kernel info leakage into userspace */
1132 memset(ps3fb.xdr_ea, 0, ps3fb_videomemory.size);
1134 /* The GPU command buffer is at the end of video memory */
1135 ps3fb.xdr_size = ps3fb_videomemory.size - GPU_CMD_BUF_SIZE;
1137 retval = ps3fb_xdr_settings(xdr_lpar, &dev->core);
1138 if (retval)
1139 goto err_free_irq;
1141 info = framebuffer_alloc(sizeof(struct ps3fb_par), &dev->core);
1142 if (!info)
1143 goto err_free_irq;
1145 par = info->par;
1146 par->mode_id = ~ps3fb_mode; /* != ps3fb_mode, to trigger change */
1147 par->new_mode_id = ps3fb_mode;
1148 par->res_index = res_index;
1149 par->num_frames = 1;
1151 info->screen_base = (char __iomem *)ps3fb.xdr_ea;
1152 info->fbops = &ps3fb_ops;
1154 info->fix = ps3fb_fix;
1155 info->fix.smem_start = virt_to_abs(ps3fb.xdr_ea);
1156 info->fix.smem_len = ps3fb.xdr_size;
1157 info->pseudo_palette = par->pseudo_palette;
1158 info->flags = FBINFO_DEFAULT | FBINFO_READS_FAST |
1159 FBINFO_HWACCEL_XPAN | FBINFO_HWACCEL_YPAN;
1161 retval = fb_alloc_cmap(&info->cmap, 256, 0);
1162 if (retval < 0)
1163 goto err_framebuffer_release;
1165 if (!fb_find_mode(&info->var, info, mode_option, ps3fb_modedb,
1166 ARRAY_SIZE(ps3fb_modedb),
1167 ps3fb_default_mode(par->new_mode_id), 32)) {
1168 retval = -EINVAL;
1169 goto err_fb_dealloc;
1172 fb_videomode_to_modelist(ps3fb_modedb, ARRAY_SIZE(ps3fb_modedb),
1173 &info->modelist);
1175 retval = register_framebuffer(info);
1176 if (retval < 0)
1177 goto err_fb_dealloc;
1179 dev->core.driver_data = info;
1181 dev_info(info->device, "%s %s, using %lu KiB of video memory\n",
1182 dev_driver_string(info->dev), info->dev->bus_id,
1183 ps3fb.xdr_size >> 10);
1185 task = kthread_run(ps3fbd, info, DEVICE_NAME);
1186 if (IS_ERR(task)) {
1187 retval = PTR_ERR(task);
1188 goto err_unregister_framebuffer;
1191 ps3fb.task = task;
1192 ps3av_register_flip_ctl(ps3fb_flip_ctl, &ps3fb);
1194 return 0;
1196 err_unregister_framebuffer:
1197 unregister_framebuffer(info);
1198 err_fb_dealloc:
1199 fb_dealloc_cmap(&info->cmap);
1200 err_framebuffer_release:
1201 framebuffer_release(info);
1202 err_free_irq:
1203 free_irq(ps3fb.irq_no, dev);
1204 ps3_irq_plug_destroy(ps3fb.irq_no);
1205 err_iounmap_dinfo:
1206 iounmap((u8 __iomem *)ps3fb.dinfo);
1207 err_gpu_context_free:
1208 lv1_gpu_context_free(ps3fb.context_handle);
1209 err_gpu_memory_free:
1210 lv1_gpu_memory_free(ps3fb.memory_handle);
1211 err:
1212 return retval;
1215 static int ps3fb_shutdown(struct ps3_system_bus_device *dev)
1217 int status;
1218 struct fb_info *info = dev->core.driver_data;
1220 dev_dbg(&dev->core, " -> %s:%d\n", __func__, __LINE__);
1222 ps3fb_flip_ctl(0, &ps3fb); /* flip off */
1223 ps3fb.dinfo->irq.mask = 0;
1225 if (info) {
1226 unregister_framebuffer(info);
1227 fb_dealloc_cmap(&info->cmap);
1228 framebuffer_release(info);
1231 ps3av_register_flip_ctl(NULL, NULL);
1232 if (ps3fb.task) {
1233 struct task_struct *task = ps3fb.task;
1234 ps3fb.task = NULL;
1235 kthread_stop(task);
1237 if (ps3fb.irq_no) {
1238 free_irq(ps3fb.irq_no, dev);
1239 ps3_irq_plug_destroy(ps3fb.irq_no);
1241 iounmap((u8 __iomem *)ps3fb.dinfo);
1243 status = lv1_gpu_context_free(ps3fb.context_handle);
1244 if (status)
1245 dev_dbg(&dev->core, "lv1_gpu_context_free failed: %d\n",
1246 status);
1248 status = lv1_gpu_memory_free(ps3fb.memory_handle);
1249 if (status)
1250 dev_dbg(&dev->core, "lv1_gpu_memory_free failed: %d\n",
1251 status);
1253 ps3_close_hv_device(dev);
1254 dev_dbg(&dev->core, " <- %s:%d\n", __func__, __LINE__);
1256 return 0;
1259 static struct ps3_system_bus_driver ps3fb_driver = {
1260 .match_id = PS3_MATCH_ID_GRAPHICS,
1261 .core.name = DEVICE_NAME,
1262 .core.owner = THIS_MODULE,
1263 .probe = ps3fb_probe,
1264 .remove = ps3fb_shutdown,
1265 .shutdown = ps3fb_shutdown,
1268 static int __init ps3fb_setup(void)
1270 char *options;
1272 #ifdef MODULE
1273 return 0;
1274 #endif
1276 if (fb_get_options(DEVICE_NAME, &options))
1277 return -ENXIO;
1279 if (!options || !*options)
1280 return 0;
1282 while (1) {
1283 char *this_opt = strsep(&options, ",");
1285 if (!this_opt)
1286 break;
1287 if (!*this_opt)
1288 continue;
1289 if (!strncmp(this_opt, "mode:", 5))
1290 ps3fb_mode = simple_strtoul(this_opt + 5, NULL, 0);
1291 else
1292 mode_option = this_opt;
1294 return 0;
1297 static int __init ps3fb_init(void)
1299 if (!ps3fb_videomemory.address || ps3fb_setup())
1300 return -ENXIO;
1302 return ps3_system_bus_driver_register(&ps3fb_driver);
1305 static void __exit ps3fb_exit(void)
1307 pr_debug(" -> %s:%d\n", __func__, __LINE__);
1308 ps3_system_bus_driver_unregister(&ps3fb_driver);
1309 pr_debug(" <- %s:%d\n", __func__, __LINE__);
1312 module_init(ps3fb_init);
1313 module_exit(ps3fb_exit);
1315 MODULE_LICENSE("GPL");
1316 MODULE_DESCRIPTION("PS3 GPU Frame Buffer Driver");
1317 MODULE_AUTHOR("Sony Computer Entertainment Inc.");
1318 MODULE_ALIAS(PS3_MODULE_ALIAS_GRAPHICS);