ps3fb: use fb_info.par properly
[wrt350n-kernel.git] / drivers / video / ps3fb.c
blobd6160e016b33b47eb7e9523b8fa2abd80f64889d
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 DDR_SIZE (0) /* used no ddr */
55 #define GPU_CMD_BUF_SIZE (64 * 1024)
56 #define GPU_IOIF (0x0d000000UL)
58 #define PS3FB_FULL_MODE_BIT 0x80
60 #define GPU_INTR_STATUS_VSYNC_0 0 /* vsync on head A */
61 #define GPU_INTR_STATUS_VSYNC_1 1 /* vsync on head B */
62 #define GPU_INTR_STATUS_FLIP_0 3 /* flip head A */
63 #define GPU_INTR_STATUS_FLIP_1 4 /* flip head B */
64 #define GPU_INTR_STATUS_QUEUE_0 5 /* queue head A */
65 #define GPU_INTR_STATUS_QUEUE_1 6 /* queue head B */
67 #define GPU_DRIVER_INFO_VERSION 0x211
69 /* gpu internals */
70 struct display_head {
71 u64 be_time_stamp;
72 u32 status;
73 u32 offset;
74 u32 res1;
75 u32 res2;
76 u32 field;
77 u32 reserved1;
79 u64 res3;
80 u32 raster;
82 u64 vblank_count;
83 u32 field_vsync;
84 u32 reserved2;
87 struct gpu_irq {
88 u32 irq_outlet;
89 u32 status;
90 u32 mask;
91 u32 video_cause;
92 u32 graph_cause;
93 u32 user_cause;
95 u32 res1;
96 u64 res2;
98 u32 reserved[4];
101 struct gpu_driver_info {
102 u32 version_driver;
103 u32 version_gpu;
104 u32 memory_size;
105 u32 hardware_channel;
107 u32 nvcore_frequency;
108 u32 memory_frequency;
110 u32 reserved[1063];
111 struct display_head display_head[8];
112 struct gpu_irq irq;
115 struct ps3fb_priv {
116 unsigned int irq_no;
118 u64 context_handle, memory_handle;
119 void *xdr_ea;
120 size_t xdr_size;
121 struct gpu_driver_info *dinfo;
123 u64 vblank_count; /* frame count */
124 wait_queue_head_t wait_vsync;
126 atomic_t ext_flip; /* on/off flip with vsync */
127 atomic_t f_count; /* fb_open count */
128 int is_blanked;
129 int is_kicked;
130 struct task_struct *task;
132 static struct ps3fb_priv ps3fb;
134 struct ps3fb_par {
135 u32 pseudo_palette[16];
136 int mode_id, new_mode_id;
137 int res_index;
138 unsigned int num_frames; /* num of frame buffers */
141 struct ps3fb_res_table {
142 u32 xres;
143 u32 yres;
144 u32 xoff;
145 u32 yoff;
146 u32 type;
148 #define PS3FB_RES_FULL 1
149 static const struct ps3fb_res_table ps3fb_res[] = {
150 /* res_x,y margin_x,y full */
151 { 720, 480, 72, 48 , 0},
152 { 720, 576, 72, 58 , 0},
153 { 1280, 720, 78, 38 , 0},
154 { 1920, 1080, 116, 58 , 0},
155 /* full mode */
156 { 720, 480, 0, 0 , PS3FB_RES_FULL},
157 { 720, 576, 0, 0 , PS3FB_RES_FULL},
158 { 1280, 720, 0, 0 , PS3FB_RES_FULL},
159 { 1920, 1080, 0, 0 , PS3FB_RES_FULL},
160 /* vesa: normally full mode */
161 { 1280, 768, 0, 0 , 0},
162 { 1280, 1024, 0, 0 , 0},
163 { 1920, 1200, 0, 0 , 0},
164 { 0, 0, 0, 0 , 0} };
166 /* default resolution */
167 #define GPU_RES_INDEX 0 /* 720 x 480 */
169 static const struct fb_videomode ps3fb_modedb[] = {
170 /* 60 Hz broadcast modes (modes "1" to "5") */
172 /* 480i */
173 "480i", 60, 576, 384, 74074, 130, 89, 78, 57, 63, 6,
174 FB_SYNC_BROADCAST, FB_VMODE_INTERLACED
175 }, {
176 /* 480p */
177 "480p", 60, 576, 384, 37037, 130, 89, 78, 57, 63, 6,
178 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
179 }, {
180 /* 720p */
181 "720p", 60, 1124, 644, 13481, 298, 148, 57, 44, 80, 5,
182 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
183 }, {
184 /* 1080i */
185 "1080i", 60, 1688, 964, 13481, 264, 160, 94, 62, 88, 5,
186 FB_SYNC_BROADCAST, FB_VMODE_INTERLACED
187 }, {
188 /* 1080p */
189 "1080p", 60, 1688, 964, 6741, 264, 160, 94, 62, 88, 5,
190 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
193 /* 50 Hz broadcast modes (modes "6" to "10") */
195 /* 576i */
196 "576i", 50, 576, 460, 74074, 142, 83, 97, 63, 63, 5,
197 FB_SYNC_BROADCAST, FB_VMODE_INTERLACED
198 }, {
199 /* 576p */
200 "576p", 50, 576, 460, 37037, 142, 83, 97, 63, 63, 5,
201 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
202 }, {
203 /* 720p */
204 "720p", 50, 1124, 644, 13468, 298, 478, 57, 44, 80, 5,
205 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
206 }, {
207 /* 1080 */
208 "1080i", 50, 1688, 964, 13468, 264, 600, 94, 62, 88, 5,
209 FB_SYNC_BROADCAST, FB_VMODE_INTERLACED
210 }, {
211 /* 1080p */
212 "1080p", 50, 1688, 964, 6734, 264, 600, 94, 62, 88, 5,
213 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
216 /* VESA modes (modes "11" to "13") */
218 /* WXGA */
219 "wxga", 60, 1280, 768, 12924, 160, 24, 29, 3, 136, 6,
220 0, FB_VMODE_NONINTERLACED,
221 FB_MODE_IS_VESA
222 }, {
223 /* SXGA */
224 "sxga", 60, 1280, 1024, 9259, 248, 48, 38, 1, 112, 3,
225 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED,
226 FB_MODE_IS_VESA
227 }, {
228 /* WUXGA */
229 "wuxga", 60, 1920, 1200, 6494, 80, 48, 26, 3, 32, 6,
230 FB_SYNC_HOR_HIGH_ACT, FB_VMODE_NONINTERLACED,
231 FB_MODE_IS_VESA
234 /* 60 Hz broadcast modes (full resolution versions of modes "1" to "5") */
236 /* 480if */
237 "480if", 60, 720, 480, 74074, 58, 17, 30, 9, 63, 6,
238 FB_SYNC_BROADCAST, FB_VMODE_INTERLACED
239 }, {
240 /* 480pf */
241 "480pf", 60, 720, 480, 37037, 58, 17, 30, 9, 63, 6,
242 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
243 }, {
244 /* 720pf */
245 "720pf", 60, 1280, 720, 13481, 220, 70, 19, 6, 80, 5,
246 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
247 }, {
248 /* 1080if */
249 "1080if", 60, 1920, 1080, 13481, 148, 44, 36, 4, 88, 5,
250 FB_SYNC_BROADCAST, FB_VMODE_INTERLACED
251 }, {
252 /* 1080pf */
253 "1080pf", 60, 1920, 1080, 6741, 148, 44, 36, 4, 88, 5,
254 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
257 /* 50 Hz broadcast modes (full resolution versions of modes "6" to "10") */
259 /* 576if */
260 "576if", 50, 720, 576, 74074, 70, 11, 39, 5, 63, 5,
261 FB_SYNC_BROADCAST, FB_VMODE_INTERLACED
262 }, {
263 /* 576pf */
264 "576pf", 50, 720, 576, 37037, 70, 11, 39, 5, 63, 5,
265 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
266 }, {
267 /* 720pf */
268 "720pf", 50, 1280, 720, 13468, 220, 400, 19, 6, 80, 5,
269 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
270 }, {
271 /* 1080if */
272 "1080f", 50, 1920, 1080, 13468, 148, 484, 36, 4, 88, 5,
273 FB_SYNC_BROADCAST, FB_VMODE_INTERLACED
274 }, {
275 /* 1080pf */
276 "1080pf", 50, 1920, 1080, 6734, 148, 484, 36, 4, 88, 5,
277 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
282 #define HEAD_A
283 #define HEAD_B
285 #define X_OFF(i) (ps3fb_res[i].xoff) /* left/right margin (pixel) */
286 #define Y_OFF(i) (ps3fb_res[i].yoff) /* top/bottom margin (pixel) */
287 #define WIDTH(i) (ps3fb_res[i].xres) /* width of FB */
288 #define HEIGHT(i) (ps3fb_res[i].yres) /* height of FB */
289 #define BPP 4 /* number of bytes per pixel */
291 /* Start of the virtual frame buffer (relative to fullscreen ) */
292 #define VP_OFF(i) ((WIDTH(i) * Y_OFF(i) + X_OFF(i)) * BPP)
295 * Start of the virtual frame buffer (relative to start of video memory)
296 * This is PAGE_SIZE aligned for easier mmap()
298 #define VFB_OFF(i) PAGE_ALIGN(VP_OFF(i))
300 /* Start of the fullscreen frame buffer (relative to start of video memory) */
301 #define FB_OFF(i) (-VP_OFF(i) & ~PAGE_MASK)
304 static int ps3fb_mode;
305 module_param(ps3fb_mode, int, 0);
307 static char *mode_option __devinitdata;
309 static int ps3fb_get_res_table(u32 xres, u32 yres, int mode)
311 int full_mode;
312 unsigned int i;
313 u32 x, y, f;
315 full_mode = (mode & PS3FB_FULL_MODE_BIT) ? PS3FB_RES_FULL : 0;
316 for (i = 0;; i++) {
317 x = ps3fb_res[i].xres;
318 y = ps3fb_res[i].yres;
319 f = ps3fb_res[i].type;
321 if (!x) {
322 pr_debug("ERROR: ps3fb_get_res_table()\n");
323 return -1;
326 if (full_mode == PS3FB_RES_FULL && f != PS3FB_RES_FULL)
327 continue;
329 if (x == xres && (yres == 0 || y == yres))
330 break;
332 x = x - 2 * ps3fb_res[i].xoff;
333 y = y - 2 * ps3fb_res[i].yoff;
334 if (x == xres && (yres == 0 || y == yres))
335 break;
337 return i;
340 static unsigned int ps3fb_find_mode(const struct fb_var_screeninfo *var,
341 u32 *line_length)
343 unsigned int i, mode;
345 for (i = 0; i < ARRAY_SIZE(ps3fb_modedb); i++)
346 if (var->xres == ps3fb_modedb[i].xres &&
347 var->yres == ps3fb_modedb[i].yres &&
348 var->pixclock == ps3fb_modedb[i].pixclock &&
349 var->hsync_len == ps3fb_modedb[i].hsync_len &&
350 var->vsync_len == ps3fb_modedb[i].vsync_len &&
351 var->left_margin == ps3fb_modedb[i].left_margin &&
352 var->right_margin == ps3fb_modedb[i].right_margin &&
353 var->upper_margin == ps3fb_modedb[i].upper_margin &&
354 var->lower_margin == ps3fb_modedb[i].lower_margin &&
355 var->sync == ps3fb_modedb[i].sync &&
356 (var->vmode & FB_VMODE_MASK) == ps3fb_modedb[i].vmode) {
357 /* Cropped broadcast modes use the full line_length */
358 *line_length =
359 ps3fb_modedb[i < 10 ? i + 13 : i].xres * 4;
360 /* Full broadcast modes have the full mode bit set */
361 mode = i > 12 ? (i - 12) | PS3FB_FULL_MODE_BIT : i + 1;
363 pr_debug("ps3fb_find_mode: mode %u\n", mode);
364 return mode;
367 pr_debug("ps3fb_find_mode: mode not found\n");
368 return 0;
371 static const struct fb_videomode *ps3fb_default_mode(int id)
373 u32 mode = id & PS3AV_MODE_MASK;
374 u32 flags;
376 if (mode < 1 || mode > 13)
377 return NULL;
379 flags = id & ~PS3AV_MODE_MASK;
381 if (mode <= 10 && flags & PS3FB_FULL_MODE_BIT) {
382 /* Full broadcast mode */
383 return &ps3fb_modedb[mode + 12];
386 return &ps3fb_modedb[mode - 1];
389 static int ps3fb_sync(struct fb_info *info, u32 frame)
391 struct ps3fb_par *par = info->par;
392 int i, status, error = 0;
393 u32 xres, yres;
394 u64 fb_ioif, offset;
396 acquire_console_sem();
398 i = par->res_index;
399 xres = ps3fb_res[i].xres;
400 yres = ps3fb_res[i].yres;
402 if (frame > par->num_frames - 1) {
403 dev_dbg(info->device, "%s: invalid frame number (%u)\n",
404 __func__, frame);
405 error = -EINVAL;
406 goto out;
408 offset = xres * yres * BPP * frame;
410 fb_ioif = GPU_IOIF + FB_OFF(i) + offset;
411 status = lv1_gpu_context_attribute(ps3fb.context_handle,
412 L1GPU_CONTEXT_ATTRIBUTE_FB_BLIT,
413 offset, fb_ioif,
414 L1GPU_FB_BLIT_WAIT_FOR_COMPLETION |
415 (xres << 16) | yres,
416 xres * BPP); /* line_length */
417 if (status)
418 dev_err(info->device,
419 "%s: lv1_gpu_context_attribute FB_BLIT failed: %d\n",
420 __func__, status);
421 #ifdef HEAD_A
422 status = lv1_gpu_context_attribute(ps3fb.context_handle,
423 L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_FLIP,
424 0, offset, 0, 0);
425 if (status)
426 dev_err(info->device,
427 "%s: lv1_gpu_context_attribute FLIP failed: %d\n",
428 __func__, status);
429 #endif
430 #ifdef HEAD_B
431 status = lv1_gpu_context_attribute(ps3fb.context_handle,
432 L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_FLIP,
433 1, offset, 0, 0);
434 if (status)
435 dev_err(info->device,
436 "%s: lv1_gpu_context_attribute FLIP failed: %d\n",
437 __func__, status);
438 #endif
440 out:
441 release_console_sem();
442 return error;
446 static int ps3fb_open(struct fb_info *info, int user)
448 atomic_inc(&ps3fb.f_count);
449 return 0;
452 static int ps3fb_release(struct fb_info *info, int user)
454 if (atomic_dec_and_test(&ps3fb.f_count)) {
455 if (atomic_read(&ps3fb.ext_flip)) {
456 atomic_set(&ps3fb.ext_flip, 0);
457 ps3fb_sync(info, 0); /* single buffer */
460 return 0;
464 * Setting the video mode has been split into two parts.
465 * First part, xxxfb_check_var, must not write anything
466 * to hardware, it should only verify and adjust var.
467 * This means it doesn't alter par but it does use hardware
468 * data from it to check this var.
471 static int ps3fb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
473 u32 line_length;
474 int mode;
475 int i;
477 dev_dbg(info->device, "var->xres:%u info->var.xres:%u\n", var->xres,
478 info->var.xres);
479 dev_dbg(info->device, "var->yres:%u info->var.yres:%u\n", var->yres,
480 info->var.yres);
482 /* FIXME For now we do exact matches only */
483 mode = ps3fb_find_mode(var, &line_length);
484 if (!mode)
485 return -EINVAL;
488 * FB_VMODE_CONUPDATE and FB_VMODE_SMOOTH_XPAN are equal!
489 * as FB_VMODE_SMOOTH_XPAN is only used internally
492 if (var->vmode & FB_VMODE_CONUPDATE) {
493 var->vmode |= FB_VMODE_YWRAP;
494 var->xoffset = info->var.xoffset;
495 var->yoffset = info->var.yoffset;
498 /* Virtual screen and panning are not supported */
499 if (var->xres_virtual > var->xres || var->yres_virtual > var->yres ||
500 var->xoffset || var->yoffset) {
501 dev_dbg(info->device,
502 "Virtual screen and panning are not supported\n");
503 return -EINVAL;
506 var->xres_virtual = var->xres;
507 var->yres_virtual = var->yres;
509 /* We support ARGB8888 only */
510 if (var->bits_per_pixel > 32 || var->grayscale ||
511 var->red.offset > 16 || var->green.offset > 8 ||
512 var->blue.offset > 0 || var->transp.offset > 24 ||
513 var->red.length > 8 || var->green.length > 8 ||
514 var->blue.length > 8 || var->transp.length > 8 ||
515 var->red.msb_right || var->green.msb_right ||
516 var->blue.msb_right || var->transp.msb_right || var->nonstd) {
517 dev_dbg(info->device, "We support ARGB8888 only\n");
518 return -EINVAL;
521 var->bits_per_pixel = 32;
522 var->red.offset = 16;
523 var->green.offset = 8;
524 var->blue.offset = 0;
525 var->transp.offset = 24;
526 var->red.length = 8;
527 var->green.length = 8;
528 var->blue.length = 8;
529 var->transp.length = 8;
530 var->red.msb_right = 0;
531 var->green.msb_right = 0;
532 var->blue.msb_right = 0;
533 var->transp.msb_right = 0;
535 /* Rotation is not supported */
536 if (var->rotate) {
537 dev_dbg(info->device, "Rotation is not supported\n");
538 return -EINVAL;
541 /* Memory limit */
542 i = ps3fb_get_res_table(var->xres, var->yres, mode);
543 if (ps3fb_res[i].xres*ps3fb_res[i].yres*BPP >
544 ps3fb.xdr_size - VFB_OFF(i)) {
545 dev_dbg(info->device, "Not enough memory\n");
546 return -ENOMEM;
549 var->height = -1;
550 var->width = -1;
552 return 0;
556 * This routine actually sets the video mode.
559 static int ps3fb_set_par(struct fb_info *info)
561 struct ps3fb_par *par = info->par;
562 unsigned int mode;
563 int i;
564 unsigned long offset;
566 dev_dbg(info->device, "xres:%d xv:%d yres:%d yv:%d clock:%d\n",
567 info->var.xres, info->var.xres_virtual,
568 info->var.yres, info->var.yres_virtual, info->var.pixclock);
570 mode = ps3fb_find_mode(&info->var, &info->fix.line_length);
571 if (!mode)
572 return -EINVAL;
574 i = ps3fb_get_res_table(info->var.xres, info->var.yres, mode);
575 par->res_index = i;
577 offset = VFB_OFF(i);
578 info->fix.smem_start = virt_to_abs(ps3fb.xdr_ea) + offset;
579 info->fix.smem_len = ps3fb.xdr_size - offset;
580 info->screen_base = (char __iomem *)ps3fb.xdr_ea + offset;
581 memset(ps3fb.xdr_ea, 0, ps3fb.xdr_size);
583 par->num_frames = info->fix.smem_len/
584 (ps3fb_res[i].xres*ps3fb_res[i].yres*BPP);
586 /* Keep the special bits we cannot set using fb_var_screeninfo */
587 par->new_mode_id = (par->new_mode_id & ~PS3AV_MODE_MASK) | mode;
589 if (par->new_mode_id != par->mode_id) {
590 if (ps3av_set_video_mode(par->new_mode_id)) {
591 par->new_mode_id = par->mode_id;
592 return -EINVAL;
594 par->mode_id = par->new_mode_id;
597 return 0;
601 * Set a single color register. The values supplied are already
602 * rounded down to the hardware's capabilities (according to the
603 * entries in the var structure). Return != 0 for invalid regno.
606 static int ps3fb_setcolreg(unsigned int regno, unsigned int red,
607 unsigned int green, unsigned int blue,
608 unsigned int transp, struct fb_info *info)
610 if (regno >= 16)
611 return 1;
613 red >>= 8;
614 green >>= 8;
615 blue >>= 8;
616 transp >>= 8;
618 ((u32 *)info->pseudo_palette)[regno] = transp << 24 | red << 16 |
619 green << 8 | blue;
620 return 0;
624 * As we have a virtual frame buffer, we need our own mmap function
627 static int ps3fb_mmap(struct fb_info *info, struct vm_area_struct *vma)
629 unsigned long size, offset;
631 size = vma->vm_end - vma->vm_start;
632 offset = vma->vm_pgoff << PAGE_SHIFT;
633 if (offset + size > info->fix.smem_len)
634 return -EINVAL;
636 offset += info->fix.smem_start;
637 if (remap_pfn_range(vma, vma->vm_start, offset >> PAGE_SHIFT,
638 size, vma->vm_page_prot))
639 return -EAGAIN;
641 dev_dbg(info->device, "ps3fb: mmap framebuffer P(%lx)->V(%lx)\n",
642 offset, vma->vm_start);
643 return 0;
647 * Blank the display
650 static int ps3fb_blank(int blank, struct fb_info *info)
652 int retval;
654 dev_dbg(info->device, "%s: blank:%d\n", __func__, blank);
655 switch (blank) {
656 case FB_BLANK_POWERDOWN:
657 case FB_BLANK_HSYNC_SUSPEND:
658 case FB_BLANK_VSYNC_SUSPEND:
659 case FB_BLANK_NORMAL:
660 retval = ps3av_video_mute(1); /* mute on */
661 if (!retval)
662 ps3fb.is_blanked = 1;
663 break;
665 default: /* unblank */
666 retval = ps3av_video_mute(0); /* mute off */
667 if (!retval)
668 ps3fb.is_blanked = 0;
669 break;
671 return retval;
674 static int ps3fb_get_vblank(struct fb_vblank *vblank)
676 memset(vblank, 0, sizeof(&vblank));
677 vblank->flags = FB_VBLANK_HAVE_VSYNC;
678 return 0;
681 static int ps3fb_wait_for_vsync(u32 crtc)
683 int ret;
684 u64 count;
686 count = ps3fb.vblank_count;
687 ret = wait_event_interruptible_timeout(ps3fb.wait_vsync,
688 count != ps3fb.vblank_count,
689 HZ / 10);
690 if (!ret)
691 return -ETIMEDOUT;
693 return 0;
696 static void ps3fb_flip_ctl(int on, void *data)
698 struct ps3fb_priv *priv = data;
699 if (on)
700 atomic_dec_if_positive(&priv->ext_flip);
701 else
702 atomic_inc(&priv->ext_flip);
707 * ioctl
710 static int ps3fb_ioctl(struct fb_info *info, unsigned int cmd,
711 unsigned long arg)
713 void __user *argp = (void __user *)arg;
714 u32 val;
715 int retval = -EFAULT;
717 switch (cmd) {
718 case FBIOGET_VBLANK:
720 struct fb_vblank vblank;
721 dev_dbg(info->device, "FBIOGET_VBLANK:\n");
722 retval = ps3fb_get_vblank(&vblank);
723 if (retval)
724 break;
726 if (copy_to_user(argp, &vblank, sizeof(vblank)))
727 retval = -EFAULT;
728 break;
731 case FBIO_WAITFORVSYNC:
733 u32 crt;
734 dev_dbg(info->device, "FBIO_WAITFORVSYNC:\n");
735 if (get_user(crt, (u32 __user *) arg))
736 break;
738 retval = ps3fb_wait_for_vsync(crt);
739 break;
742 case PS3FB_IOCTL_SETMODE:
744 struct ps3fb_par *par = info->par;
745 const struct fb_videomode *mode;
746 struct fb_var_screeninfo var;
748 if (copy_from_user(&val, argp, sizeof(val)))
749 break;
751 if (!(val & PS3AV_MODE_MASK)) {
752 u32 id = ps3av_get_auto_mode();
753 if (id > 0)
754 val = (val & ~PS3AV_MODE_MASK) | id;
756 dev_dbg(info->device, "PS3FB_IOCTL_SETMODE:%x\n", val);
757 retval = -EINVAL;
758 mode = ps3fb_default_mode(val);
759 if (mode) {
760 var = info->var;
761 fb_videomode_to_var(&var, mode);
762 acquire_console_sem();
763 info->flags |= FBINFO_MISC_USEREVENT;
764 /* Force, in case only special bits changed */
765 var.activate |= FB_ACTIVATE_FORCE;
766 par->new_mode_id = val;
767 retval = fb_set_var(info, &var);
768 info->flags &= ~FBINFO_MISC_USEREVENT;
769 release_console_sem();
771 break;
774 case PS3FB_IOCTL_GETMODE:
775 val = ps3av_get_mode();
776 dev_dbg(info->device, "PS3FB_IOCTL_GETMODE:%x\n", val);
777 if (!copy_to_user(argp, &val, sizeof(val)))
778 retval = 0;
779 break;
781 case PS3FB_IOCTL_SCREENINFO:
783 struct ps3fb_par *par = info->par;
784 struct ps3fb_ioctl_res res;
785 int i = par->res_index;
786 dev_dbg(info->device, "PS3FB_IOCTL_SCREENINFO:\n");
787 res.xres = ps3fb_res[i].xres;
788 res.yres = ps3fb_res[i].yres;
789 res.xoff = ps3fb_res[i].xoff;
790 res.yoff = ps3fb_res[i].yoff;
791 res.num_frames = par->num_frames;
792 if (!copy_to_user(argp, &res, sizeof(res)))
793 retval = 0;
794 break;
797 case PS3FB_IOCTL_ON:
798 dev_dbg(info->device, "PS3FB_IOCTL_ON:\n");
799 atomic_inc(&ps3fb.ext_flip);
800 retval = 0;
801 break;
803 case PS3FB_IOCTL_OFF:
804 dev_dbg(info->device, "PS3FB_IOCTL_OFF:\n");
805 atomic_dec_if_positive(&ps3fb.ext_flip);
806 retval = 0;
807 break;
809 case PS3FB_IOCTL_FSEL:
810 if (copy_from_user(&val, argp, sizeof(val)))
811 break;
813 dev_dbg(info->device, "PS3FB_IOCTL_FSEL:%d\n", val);
814 retval = ps3fb_sync(info, val);
815 break;
817 default:
818 retval = -ENOIOCTLCMD;
819 break;
821 return retval;
824 static int ps3fbd(void *arg)
826 struct fb_info *info = arg;
828 set_freezable();
829 while (!kthread_should_stop()) {
830 try_to_freeze();
831 set_current_state(TASK_INTERRUPTIBLE);
832 if (ps3fb.is_kicked) {
833 ps3fb.is_kicked = 0;
834 ps3fb_sync(info, 0); /* single buffer */
836 schedule();
838 return 0;
841 static irqreturn_t ps3fb_vsync_interrupt(int irq, void *ptr)
843 struct device *dev = ptr;
844 u64 v1;
845 int status;
846 struct display_head *head = &ps3fb.dinfo->display_head[1];
848 status = lv1_gpu_context_intr(ps3fb.context_handle, &v1);
849 if (status) {
850 dev_err(dev, "%s: lv1_gpu_context_intr failed: %d\n", __func__,
851 status);
852 return IRQ_NONE;
855 if (v1 & (1 << GPU_INTR_STATUS_VSYNC_1)) {
856 /* VSYNC */
857 ps3fb.vblank_count = head->vblank_count;
858 if (ps3fb.task && !ps3fb.is_blanked &&
859 !atomic_read(&ps3fb.ext_flip)) {
860 ps3fb.is_kicked = 1;
861 wake_up_process(ps3fb.task);
863 wake_up_interruptible(&ps3fb.wait_vsync);
866 return IRQ_HANDLED;
870 static int ps3fb_vsync_settings(struct gpu_driver_info *dinfo,
871 struct device *dev)
873 int error;
875 dev_dbg(dev, "version_driver:%x\n", dinfo->version_driver);
876 dev_dbg(dev, "irq outlet:%x\n", dinfo->irq.irq_outlet);
877 dev_dbg(dev,
878 "version_gpu: %x memory_size: %x ch: %x core_freq: %d "
879 "mem_freq:%d\n",
880 dinfo->version_gpu, dinfo->memory_size, dinfo->hardware_channel,
881 dinfo->nvcore_frequency/1000000, dinfo->memory_frequency/1000000);
883 if (dinfo->version_driver != GPU_DRIVER_INFO_VERSION) {
884 dev_err(dev, "%s: version_driver err:%x\n", __func__,
885 dinfo->version_driver);
886 return -EINVAL;
889 error = ps3_irq_plug_setup(PS3_BINDING_CPU_ANY, dinfo->irq.irq_outlet,
890 &ps3fb.irq_no);
891 if (error) {
892 dev_err(dev, "%s: ps3_alloc_irq failed %d\n", __func__, error);
893 return error;
896 error = request_irq(ps3fb.irq_no, ps3fb_vsync_interrupt, IRQF_DISABLED,
897 DEVICE_NAME, dev);
898 if (error) {
899 dev_err(dev, "%s: request_irq failed %d\n", __func__, error);
900 ps3_irq_plug_destroy(ps3fb.irq_no);
901 return error;
904 dinfo->irq.mask = (1 << GPU_INTR_STATUS_VSYNC_1) |
905 (1 << GPU_INTR_STATUS_FLIP_1);
906 return 0;
909 static int ps3fb_xdr_settings(u64 xdr_lpar, struct device *dev)
911 int status;
913 status = lv1_gpu_context_iomap(ps3fb.context_handle, GPU_IOIF,
914 xdr_lpar, ps3fb_videomemory.size, 0);
915 if (status) {
916 dev_err(dev, "%s: lv1_gpu_context_iomap failed: %d\n",
917 __func__, status);
918 return -ENXIO;
920 dev_dbg(dev,
921 "video:%p xdr_ea:%p ioif:%lx lpar:%lx phys:%lx size:%lx\n",
922 ps3fb_videomemory.address, ps3fb.xdr_ea, GPU_IOIF, xdr_lpar,
923 virt_to_abs(ps3fb.xdr_ea), ps3fb_videomemory.size);
925 status = lv1_gpu_context_attribute(ps3fb.context_handle,
926 L1GPU_CONTEXT_ATTRIBUTE_FB_SETUP,
927 xdr_lpar + ps3fb.xdr_size,
928 GPU_CMD_BUF_SIZE,
929 GPU_IOIF + ps3fb.xdr_size, 0);
930 if (status) {
931 dev_err(dev,
932 "%s: lv1_gpu_context_attribute FB_SETUP failed: %d\n",
933 __func__, status);
934 return -ENXIO;
936 return 0;
939 static struct fb_ops ps3fb_ops = {
940 .fb_open = ps3fb_open,
941 .fb_release = ps3fb_release,
942 .fb_read = fb_sys_read,
943 .fb_write = fb_sys_write,
944 .fb_check_var = ps3fb_check_var,
945 .fb_set_par = ps3fb_set_par,
946 .fb_setcolreg = ps3fb_setcolreg,
947 .fb_fillrect = sys_fillrect,
948 .fb_copyarea = sys_copyarea,
949 .fb_imageblit = sys_imageblit,
950 .fb_mmap = ps3fb_mmap,
951 .fb_blank = ps3fb_blank,
952 .fb_ioctl = ps3fb_ioctl,
953 .fb_compat_ioctl = ps3fb_ioctl
956 static struct fb_fix_screeninfo ps3fb_fix __initdata = {
957 .id = DEVICE_NAME,
958 .type = FB_TYPE_PACKED_PIXELS,
959 .visual = FB_VISUAL_TRUECOLOR,
960 .accel = FB_ACCEL_NONE,
963 static int ps3fb_set_sync(struct device *dev)
965 int status;
967 #ifdef HEAD_A
968 status = lv1_gpu_context_attribute(0x0,
969 L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_SYNC,
970 0, L1GPU_DISPLAY_SYNC_VSYNC, 0, 0);
971 if (status) {
972 dev_err(dev,
973 "%s: lv1_gpu_context_attribute DISPLAY_SYNC failed: "
974 "%d\n",
975 __func__, status);
976 return -1;
978 #endif
979 #ifdef HEAD_B
980 status = lv1_gpu_context_attribute(0x0,
981 L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_SYNC,
982 1, L1GPU_DISPLAY_SYNC_VSYNC, 0, 0);
984 if (status) {
985 dev_err(dev,
986 "%s: lv1_gpu_context_attribute DISPLAY_SYNC failed: "
987 "%d\n",
988 __func__, status);
989 return -1;
991 #endif
992 return 0;
995 static int __devinit ps3fb_probe(struct ps3_system_bus_device *dev)
997 struct fb_info *info;
998 struct ps3fb_par *par;
999 int retval = -ENOMEM;
1000 u32 xres, yres;
1001 u64 ddr_lpar = 0;
1002 u64 lpar_dma_control = 0;
1003 u64 lpar_driver_info = 0;
1004 u64 lpar_reports = 0;
1005 u64 lpar_reports_size = 0;
1006 u64 xdr_lpar;
1007 int status, res_index;
1008 unsigned long offset;
1009 struct task_struct *task;
1011 status = ps3_open_hv_device(dev);
1012 if (status) {
1013 dev_err(&dev->core, "%s: ps3_open_hv_device failed\n",
1014 __func__);
1015 goto err;
1018 if (!ps3fb_mode)
1019 ps3fb_mode = ps3av_get_mode();
1020 dev_dbg(&dev->core, "ps3av_mode:%d\n", ps3fb_mode);
1022 if (ps3fb_mode > 0 &&
1023 !ps3av_video_mode2res(ps3fb_mode, &xres, &yres)) {
1024 res_index = ps3fb_get_res_table(xres, yres, ps3fb_mode);
1025 dev_dbg(&dev->core, "res_index:%d\n", res_index);
1026 } else
1027 res_index = GPU_RES_INDEX;
1029 atomic_set(&ps3fb.f_count, -1); /* fbcon opens ps3fb */
1030 atomic_set(&ps3fb.ext_flip, 0); /* for flip with vsync */
1031 init_waitqueue_head(&ps3fb.wait_vsync);
1033 ps3fb_set_sync(&dev->core);
1035 /* get gpu context handle */
1036 status = lv1_gpu_memory_allocate(DDR_SIZE, 0, 0, 0, 0,
1037 &ps3fb.memory_handle, &ddr_lpar);
1038 if (status) {
1039 dev_err(&dev->core, "%s: lv1_gpu_memory_allocate failed: %d\n",
1040 __func__, status);
1041 goto err;
1043 dev_dbg(&dev->core, "ddr:lpar:0x%lx\n", ddr_lpar);
1045 status = lv1_gpu_context_allocate(ps3fb.memory_handle, 0,
1046 &ps3fb.context_handle,
1047 &lpar_dma_control, &lpar_driver_info,
1048 &lpar_reports, &lpar_reports_size);
1049 if (status) {
1050 dev_err(&dev->core,
1051 "%s: lv1_gpu_context_attribute failed: %d\n", __func__,
1052 status);
1053 goto err_gpu_memory_free;
1056 /* vsync interrupt */
1057 ps3fb.dinfo = ioremap(lpar_driver_info, 128 * 1024);
1058 if (!ps3fb.dinfo) {
1059 dev_err(&dev->core, "%s: ioremap failed\n", __func__);
1060 goto err_gpu_context_free;
1063 retval = ps3fb_vsync_settings(ps3fb.dinfo, &dev->core);
1064 if (retval)
1065 goto err_iounmap_dinfo;
1067 /* XDR frame buffer */
1068 ps3fb.xdr_ea = ps3fb_videomemory.address;
1069 xdr_lpar = ps3_mm_phys_to_lpar(__pa(ps3fb.xdr_ea));
1071 /* Clear memory to prevent kernel info leakage into userspace */
1072 memset(ps3fb.xdr_ea, 0, ps3fb_videomemory.size);
1074 /* The GPU command buffer is at the end of video memory */
1075 ps3fb.xdr_size = ps3fb_videomemory.size - GPU_CMD_BUF_SIZE;
1077 retval = ps3fb_xdr_settings(xdr_lpar, &dev->core);
1078 if (retval)
1079 goto err_free_irq;
1081 info = framebuffer_alloc(sizeof(struct ps3fb_par), &dev->core);
1082 if (!info)
1083 goto err_free_irq;
1085 par = info->par;
1086 par->mode_id = ~ps3fb_mode; /* != ps3fb_mode, to trigger change */
1087 par->new_mode_id = ps3fb_mode;
1088 par->res_index = res_index;
1089 par->num_frames = 1;
1091 offset = VFB_OFF(res_index);
1092 info->screen_base = (char __iomem *)ps3fb.xdr_ea + offset;
1093 info->fbops = &ps3fb_ops;
1095 info->fix = ps3fb_fix;
1096 info->fix.smem_start = virt_to_abs(ps3fb.xdr_ea) + offset;
1097 info->fix.smem_len = ps3fb.xdr_size - offset;
1098 info->pseudo_palette = par->pseudo_palette;
1099 info->flags = FBINFO_DEFAULT | FBINFO_READS_FAST;
1101 retval = fb_alloc_cmap(&info->cmap, 256, 0);
1102 if (retval < 0)
1103 goto err_framebuffer_release;
1105 if (!fb_find_mode(&info->var, info, mode_option, ps3fb_modedb,
1106 ARRAY_SIZE(ps3fb_modedb),
1107 ps3fb_default_mode(par->new_mode_id), 32)) {
1108 retval = -EINVAL;
1109 goto err_fb_dealloc;
1112 fb_videomode_to_modelist(ps3fb_modedb, ARRAY_SIZE(ps3fb_modedb),
1113 &info->modelist);
1115 retval = register_framebuffer(info);
1116 if (retval < 0)
1117 goto err_fb_dealloc;
1119 dev->core.driver_data = info;
1121 dev_info(info->device, "%s %s, using %lu KiB of video memory\n",
1122 dev_driver_string(info->dev), info->dev->bus_id,
1123 ps3fb.xdr_size >> 10);
1125 task = kthread_run(ps3fbd, info, DEVICE_NAME);
1126 if (IS_ERR(task)) {
1127 retval = PTR_ERR(task);
1128 goto err_unregister_framebuffer;
1131 ps3fb.task = task;
1132 ps3av_register_flip_ctl(ps3fb_flip_ctl, &ps3fb);
1134 return 0;
1136 err_unregister_framebuffer:
1137 unregister_framebuffer(info);
1138 err_fb_dealloc:
1139 fb_dealloc_cmap(&info->cmap);
1140 err_framebuffer_release:
1141 framebuffer_release(info);
1142 err_free_irq:
1143 free_irq(ps3fb.irq_no, dev);
1144 ps3_irq_plug_destroy(ps3fb.irq_no);
1145 err_iounmap_dinfo:
1146 iounmap((u8 __iomem *)ps3fb.dinfo);
1147 err_gpu_context_free:
1148 lv1_gpu_context_free(ps3fb.context_handle);
1149 err_gpu_memory_free:
1150 lv1_gpu_memory_free(ps3fb.memory_handle);
1151 err:
1152 return retval;
1155 static int ps3fb_shutdown(struct ps3_system_bus_device *dev)
1157 int status;
1158 struct fb_info *info = dev->core.driver_data;
1160 dev_dbg(&dev->core, " -> %s:%d\n", __func__, __LINE__);
1162 ps3fb_flip_ctl(0, &ps3fb); /* flip off */
1163 ps3fb.dinfo->irq.mask = 0;
1165 if (info) {
1166 unregister_framebuffer(info);
1167 fb_dealloc_cmap(&info->cmap);
1168 framebuffer_release(info);
1171 ps3av_register_flip_ctl(NULL, NULL);
1172 if (ps3fb.task) {
1173 struct task_struct *task = ps3fb.task;
1174 ps3fb.task = NULL;
1175 kthread_stop(task);
1177 if (ps3fb.irq_no) {
1178 free_irq(ps3fb.irq_no, dev);
1179 ps3_irq_plug_destroy(ps3fb.irq_no);
1181 iounmap((u8 __iomem *)ps3fb.dinfo);
1183 status = lv1_gpu_context_free(ps3fb.context_handle);
1184 if (status)
1185 dev_dbg(&dev->core, "lv1_gpu_context_free failed: %d\n",
1186 status);
1188 status = lv1_gpu_memory_free(ps3fb.memory_handle);
1189 if (status)
1190 dev_dbg(&dev->core, "lv1_gpu_memory_free failed: %d\n",
1191 status);
1193 ps3_close_hv_device(dev);
1194 dev_dbg(&dev->core, " <- %s:%d\n", __func__, __LINE__);
1196 return 0;
1199 static struct ps3_system_bus_driver ps3fb_driver = {
1200 .match_id = PS3_MATCH_ID_GRAPHICS,
1201 .core.name = DEVICE_NAME,
1202 .core.owner = THIS_MODULE,
1203 .probe = ps3fb_probe,
1204 .remove = ps3fb_shutdown,
1205 .shutdown = ps3fb_shutdown,
1208 static int __init ps3fb_setup(void)
1210 char *options;
1212 #ifdef MODULE
1213 return 0;
1214 #endif
1216 if (fb_get_options(DEVICE_NAME, &options))
1217 return -ENXIO;
1219 if (!options || !*options)
1220 return 0;
1222 while (1) {
1223 char *this_opt = strsep(&options, ",");
1225 if (!this_opt)
1226 break;
1227 if (!*this_opt)
1228 continue;
1229 if (!strncmp(this_opt, "mode:", 5))
1230 ps3fb_mode = simple_strtoul(this_opt + 5, NULL, 0);
1231 else
1232 mode_option = this_opt;
1234 return 0;
1237 static int __init ps3fb_init(void)
1239 if (!ps3fb_videomemory.address || ps3fb_setup())
1240 return -ENXIO;
1242 return ps3_system_bus_driver_register(&ps3fb_driver);
1245 static void __exit ps3fb_exit(void)
1247 pr_debug(" -> %s:%d\n", __func__, __LINE__);
1248 ps3_system_bus_driver_unregister(&ps3fb_driver);
1249 pr_debug(" <- %s:%d\n", __func__, __LINE__);
1252 module_init(ps3fb_init);
1253 module_exit(ps3fb_exit);
1255 MODULE_LICENSE("GPL");
1256 MODULE_DESCRIPTION("PS3 GPU Frame Buffer Driver");
1257 MODULE_AUTHOR("Sony Computer Entertainment Inc.");
1258 MODULE_ALIAS(PS3_MODULE_ALIAS_GRAPHICS);