blob: 815b3cc78fd3ea417da384ebaa2f53d83f1cb4e7 [file] [log] [blame]
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001/*
2 * linux/drivers/video/ps3fb.c -- PS3 GPU frame buffer device
3 *
4 * Copyright (C) 2006 Sony Computer Entertainment Inc.
5 * Copyright 2006, 2007 Sony Corporation
6 *
7 * This file is based on :
8 *
9 * linux/drivers/video/vfb.c -- Virtual frame buffer device
10 *
11 * Copyright (C) 2002 James Simmons
12 *
13 * Copyright (C) 1997 Geert Uytterhoeven
14 *
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.
18 */
19
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/tty.h>
26#include <linux/slab.h>
27#include <linux/vmalloc.h>
28#include <linux/delay.h>
29#include <linux/interrupt.h>
30#include <linux/platform_device.h>
31#include <linux/console.h>
32#include <linux/ioctl.h>
33#include <linux/notifier.h>
34#include <linux/reboot.h>
Geert Uytterhoeven1c0c8462007-05-02 14:48:31 +020035#include <linux/kthread.h>
36#include <linux/freezer.h>
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -080037
38#include <asm/uaccess.h>
39#include <linux/fb.h>
40#include <linux/init.h>
41#include <asm/time.h>
42
43#include <asm/abs_addr.h>
44#include <asm/lv1call.h>
45#include <asm/ps3av.h>
46#include <asm/ps3fb.h>
47#include <asm/ps3.h>
48
49#ifdef PS3FB_DEBUG
50#define DPRINTK(fmt, args...) printk("%s: " fmt, __FUNCTION__ , ##args)
51#else
52#define DPRINTK(fmt, args...)
53#endif
54
55#define L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_SYNC 0x101
56#define L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_FLIP 0x102
57#define L1GPU_CONTEXT_ATTRIBUTE_FB_SETUP 0x600
58#define L1GPU_CONTEXT_ATTRIBUTE_FB_BLIT 0x601
59#define L1GPU_CONTEXT_ATTRIBUTE_FB_BLIT_SYNC 0x602
60
61#define L1GPU_FB_BLIT_WAIT_FOR_COMPLETION (1ULL << 32)
62
63#define L1GPU_DISPLAY_SYNC_HSYNC 1
64#define L1GPU_DISPLAY_SYNC_VSYNC 2
65
66#define DDR_SIZE (0) /* used no ddr */
67#define GPU_OFFSET (64 * 1024)
68#define GPU_IOIF (0x0d000000UL)
69
70#define PS3FB_FULL_MODE_BIT 0x80
71
72#define GPU_INTR_STATUS_VSYNC_0 0 /* vsync on head A */
73#define GPU_INTR_STATUS_VSYNC_1 1 /* vsync on head B */
74#define GPU_INTR_STATUS_FLIP_0 3 /* flip head A */
75#define GPU_INTR_STATUS_FLIP_1 4 /* flip head B */
76#define GPU_INTR_STATUS_QUEUE_0 5 /* queue head A */
77#define GPU_INTR_STATUS_QUEUE_1 6 /* queue head B */
78
79#define GPU_DRIVER_INFO_VERSION 0x211
80
81/* gpu internals */
82struct display_head {
83 u64 be_time_stamp;
84 u32 status;
85 u32 offset;
86 u32 res1;
87 u32 res2;
88 u32 field;
89 u32 reserved1;
90
91 u64 res3;
92 u32 raster;
93
94 u64 vblank_count;
95 u32 field_vsync;
96 u32 reserved2;
97};
98
99struct gpu_irq {
100 u32 irq_outlet;
101 u32 status;
102 u32 mask;
103 u32 video_cause;
104 u32 graph_cause;
105 u32 user_cause;
106
107 u32 res1;
108 u64 res2;
109
110 u32 reserved[4];
111};
112
113struct gpu_driver_info {
114 u32 version_driver;
115 u32 version_gpu;
116 u32 memory_size;
117 u32 hardware_channel;
118
119 u32 nvcore_frequency;
120 u32 memory_frequency;
121
122 u32 reserved[1063];
123 struct display_head display_head[8];
124 struct gpu_irq irq;
125};
126
127struct ps3fb_priv {
128 unsigned int irq_no;
129 void *dev;
130
131 u64 context_handle, memory_handle;
132 void *xdr_ea;
133 struct gpu_driver_info *dinfo;
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800134 u32 res_index;
135
136 u64 vblank_count; /* frame count */
137 wait_queue_head_t wait_vsync;
138
139 u32 num_frames; /* num of frame buffers */
140 atomic_t ext_flip; /* on/off flip with vsync */
141 atomic_t f_count; /* fb_open count */
142 int is_blanked;
Geert Uytterhoeven1c0c8462007-05-02 14:48:31 +0200143 int is_kicked;
144 struct task_struct *task;
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800145};
146static struct ps3fb_priv ps3fb;
147
148struct ps3fb_res_table {
149 u32 xres;
150 u32 yres;
151 u32 xoff;
152 u32 yoff;
153 u32 type;
154};
155#define PS3FB_RES_FULL 1
156static const struct ps3fb_res_table ps3fb_res[] = {
157 /* res_x,y margin_x,y full */
158 { 720, 480, 72, 48 , 0},
159 { 720, 576, 72, 58 , 0},
160 { 1280, 720, 78, 38 , 0},
161 { 1920, 1080, 116, 58 , 0},
162 /* full mode */
163 { 720, 480, 0, 0 , PS3FB_RES_FULL},
164 { 720, 576, 0, 0 , PS3FB_RES_FULL},
165 { 1280, 720, 0, 0 , PS3FB_RES_FULL},
166 { 1920, 1080, 0, 0 , PS3FB_RES_FULL},
167 /* vesa: normally full mode */
168 { 1280, 768, 0, 0 , 0},
169 { 1280, 1024, 0, 0 , 0},
170 { 1920, 1200, 0, 0 , 0},
171 { 0, 0, 0, 0 , 0} };
172
173/* default resolution */
174#define GPU_RES_INDEX 0 /* 720 x 480 */
175
176static const struct fb_videomode ps3fb_modedb[] = {
177 /* 60 Hz broadcast modes (modes "1" to "5") */
178 {
179 /* 480i */
180 "480i", 60, 576, 384, 74074, 130, 89, 78, 57, 63, 6,
181 FB_SYNC_BROADCAST, FB_VMODE_INTERLACED
182 }, {
183 /* 480p */
184 "480p", 60, 576, 384, 37037, 130, 89, 78, 57, 63, 6,
185 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
186 }, {
187 /* 720p */
188 "720p", 60, 1124, 644, 13481, 298, 148, 57, 44, 80, 5,
189 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
190 }, {
191 /* 1080i */
192 "1080i", 60, 1688, 964, 13481, 264, 160, 94, 62, 88, 5,
193 FB_SYNC_BROADCAST, FB_VMODE_INTERLACED
194 }, {
195 /* 1080p */
196 "1080p", 60, 1688, 964, 6741, 264, 160, 94, 62, 88, 5,
197 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
198 },
199
200 /* 50 Hz broadcast modes (modes "6" to "10") */
201 {
202 /* 576i */
203 "576i", 50, 576, 460, 74074, 142, 83, 97, 63, 63, 5,
204 FB_SYNC_BROADCAST, FB_VMODE_INTERLACED
205 }, {
206 /* 576p */
207 "576p", 50, 576, 460, 37037, 142, 83, 97, 63, 63, 5,
208 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
209 }, {
210 /* 720p */
211 "720p", 50, 1124, 644, 13468, 298, 478, 57, 44, 80, 5,
212 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
213 }, {
214 /* 1080 */
215 "1080i", 50, 1688, 964, 13468, 264, 600, 94, 62, 88, 5,
216 FB_SYNC_BROADCAST, FB_VMODE_INTERLACED
217 }, {
218 /* 1080p */
219 "1080p", 50, 1688, 964, 6734, 264, 600, 94, 62, 88, 5,
220 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
221 },
222
223 /* VESA modes (modes "11" to "13") */
224 {
225 /* WXGA */
226 "wxga", 60, 1280, 768, 12924, 160, 24, 29, 3, 136, 6,
227 0, FB_VMODE_NONINTERLACED,
228 FB_MODE_IS_VESA
229 }, {
230 /* SXGA */
231 "sxga", 60, 1280, 1024, 9259, 248, 48, 38, 1, 112, 3,
232 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED,
233 FB_MODE_IS_VESA
234 }, {
235 /* WUXGA */
236 "wuxga", 60, 1920, 1200, 6494, 80, 48, 26, 3, 32, 6,
237 FB_SYNC_HOR_HIGH_ACT, FB_VMODE_NONINTERLACED,
238 FB_MODE_IS_VESA
239 },
240
241 /* 60 Hz broadcast modes (full resolution versions of modes "1" to "5") */
242 {
243 /* 480if */
244 "480if", 60, 720, 480, 74074, 58, 17, 30, 9, 63, 6,
245 FB_SYNC_BROADCAST, FB_VMODE_INTERLACED
246 }, {
247 /* 480pf */
248 "480pf", 60, 720, 480, 37037, 58, 17, 30, 9, 63, 6,
249 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
250 }, {
251 /* 720pf */
252 "720pf", 60, 1280, 720, 13481, 220, 70, 19, 6, 80, 5,
253 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
254 }, {
255 /* 1080if */
256 "1080if", 60, 1920, 1080, 13481, 148, 44, 36, 4, 88, 5,
257 FB_SYNC_BROADCAST, FB_VMODE_INTERLACED
258 }, {
259 /* 1080pf */
260 "1080pf", 60, 1920, 1080, 6741, 148, 44, 36, 4, 88, 5,
261 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
262 },
263
264 /* 50 Hz broadcast modes (full resolution versions of modes "6" to "10") */
265 {
266 /* 576if */
267 "576if", 50, 720, 576, 74074, 70, 11, 39, 5, 63, 5,
268 FB_SYNC_BROADCAST, FB_VMODE_INTERLACED
269 }, {
270 /* 576pf */
271 "576pf", 50, 720, 576, 37037, 70, 11, 39, 5, 63, 5,
272 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
273 }, {
274 /* 720pf */
275 "720pf", 50, 1280, 720, 13468, 220, 400, 19, 6, 80, 5,
276 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
277 }, {
278 /* 1080if */
279 "1080f", 50, 1920, 1080, 13468, 148, 484, 36, 4, 88, 5,
280 FB_SYNC_BROADCAST, FB_VMODE_INTERLACED
281 }, {
282 /* 1080pf */
283 "1080pf", 50, 1920, 1080, 6734, 148, 484, 36, 4, 88, 5,
284 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
285 }
286};
287
288
289#define HEAD_A
290#define HEAD_B
291
292#define X_OFF(i) (ps3fb_res[i].xoff) /* left/right margin (pixel) */
293#define Y_OFF(i) (ps3fb_res[i].yoff) /* top/bottom margin (pixel) */
294#define WIDTH(i) (ps3fb_res[i].xres) /* width of FB */
295#define HEIGHT(i) (ps3fb_res[i].yres) /* height of FB */
296#define BPP 4 /* number of bytes per pixel */
297#define VP_OFF(i) (WIDTH(i) * Y_OFF(i) * BPP + X_OFF(i) * BPP)
298#define FB_OFF(i) (GPU_OFFSET - VP_OFF(i) % GPU_OFFSET)
299
300static int ps3fb_mode = 0;
301module_param(ps3fb_mode, bool, 0);
302
303static char *mode_option __initdata = NULL;
304
305
306static int ps3fb_get_res_table(u32 xres, u32 yres)
307{
308 int full_mode;
309 unsigned int i;
310 u32 x, y, f;
311
312 full_mode = (ps3fb_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;
317
318 if (!x) {
319 DPRINTK("ERROR: ps3fb_get_res_table()\n");
320 return -1;
321 }
322
323 if (full_mode == PS3FB_RES_FULL && f != PS3FB_RES_FULL)
324 continue;
325
326 if (x == xres && (yres == 0 || y == yres))
327 break;
328
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;
333 }
334 return i;
335}
336
337static unsigned int ps3fb_find_mode(const struct fb_var_screeninfo *var,
338 u32 *line_length)
339{
340 unsigned int i, mode;
341
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 /* Cropped broadcast modes use the full line_length */
355 *line_length =
356 ps3fb_modedb[i < 10 ? i + 13 : i].xres * 4;
357 /* Full broadcast modes have the full mode bit set */
358 mode = i > 12 ? (i - 12) | PS3FB_FULL_MODE_BIT : i + 1;
359
360 DPRINTK("ps3fb_find_mode: mode %u\n", mode);
361 return mode;
362 }
363
364 DPRINTK("ps3fb_find_mode: mode not found\n");
365 return 0;
366
367}
368
369static const struct fb_videomode *ps3fb_default_mode(void)
370{
371 u32 mode = ps3fb_mode & PS3AV_MODE_MASK;
372 u32 flags;
373
374 if (mode < 1 || mode > 13)
375 return NULL;
376
377 flags = ps3fb_mode & ~PS3AV_MODE_MASK;
378
379 if (mode <= 10 && flags & PS3FB_FULL_MODE_BIT) {
380 /* Full broadcast mode */
381 return &ps3fb_modedb[mode + 12];
382 }
383
384 return &ps3fb_modedb[mode - 1];
385}
386
387static int ps3fb_sync(u32 frame)
388{
389 int i, status;
390 u32 xres, yres;
391 u64 fb_ioif, offset;
392
393 i = ps3fb.res_index;
394 xres = ps3fb_res[i].xres;
395 yres = ps3fb_res[i].yres;
396
397 if (frame > ps3fb.num_frames - 1) {
398 printk(KERN_WARNING "%s: invalid frame number (%u)\n",
399 __FUNCTION__, frame);
400 return -EINVAL;
401 }
402 offset = xres * yres * BPP * frame;
403
404 fb_ioif = GPU_IOIF + FB_OFF(i) + offset;
405 status = lv1_gpu_context_attribute(ps3fb.context_handle,
406 L1GPU_CONTEXT_ATTRIBUTE_FB_BLIT,
407 offset, fb_ioif,
408 L1GPU_FB_BLIT_WAIT_FOR_COMPLETION |
409 (xres << 16) | yres,
410 xres * BPP); /* line_length */
411 if (status)
412 printk(KERN_ERR "%s: lv1_gpu_context_attribute FB_BLIT failed: %d\n",
413 __FUNCTION__, status);
414#ifdef HEAD_A
415 status = lv1_gpu_context_attribute(ps3fb.context_handle,
416 L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_FLIP,
417 0, offset, 0, 0);
418 if (status)
419 printk(KERN_ERR "%s: lv1_gpu_context_attribute FLIP failed: %d\n",
420 __FUNCTION__, status);
421#endif
422#ifdef HEAD_B
423 status = lv1_gpu_context_attribute(ps3fb.context_handle,
424 L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_FLIP,
425 1, offset, 0, 0);
426 if (status)
427 printk(KERN_ERR "%s: lv1_gpu_context_attribute FLIP failed: %d\n",
428 __FUNCTION__, status);
429#endif
430 return 0;
431}
432
433
434static int ps3fb_open(struct fb_info *info, int user)
435{
436 atomic_inc(&ps3fb.f_count);
437 return 0;
438}
439
440static int ps3fb_release(struct fb_info *info, int user)
441{
442 if (atomic_dec_and_test(&ps3fb.f_count)) {
443 if (atomic_read(&ps3fb.ext_flip)) {
444 atomic_set(&ps3fb.ext_flip, 0);
445 ps3fb_sync(0); /* single buffer */
446 }
447 }
448 return 0;
449}
450
451 /*
452 * Setting the video mode has been split into two parts.
453 * First part, xxxfb_check_var, must not write anything
454 * to hardware, it should only verify and adjust var.
455 * This means it doesn't alter par but it does use hardware
456 * data from it to check this var.
457 */
458
459static int ps3fb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
460{
461 u32 line_length;
462 int mode;
463 int i;
464
465 DPRINTK("var->xres:%u info->var.xres:%u\n", var->xres, info->var.xres);
466 DPRINTK("var->yres:%u info->var.yres:%u\n", var->yres, info->var.yres);
467
468 /* FIXME For now we do exact matches only */
469 mode = ps3fb_find_mode(var, &line_length);
470 if (!mode)
471 return -EINVAL;
472
473 /*
474 * FB_VMODE_CONUPDATE and FB_VMODE_SMOOTH_XPAN are equal!
475 * as FB_VMODE_SMOOTH_XPAN is only used internally
476 */
477
478 if (var->vmode & FB_VMODE_CONUPDATE) {
479 var->vmode |= FB_VMODE_YWRAP;
480 var->xoffset = info->var.xoffset;
481 var->yoffset = info->var.yoffset;
482 }
483
484 /* Virtual screen and panning are not supported */
485 if (var->xres_virtual > var->xres || var->yres_virtual > var->yres ||
486 var->xoffset || var->yoffset) {
487 DPRINTK("Virtual screen and panning are not supported\n");
488 return -EINVAL;
489 }
490
491 var->xres_virtual = var->xres;
492 var->yres_virtual = var->yres;
493
494 /* We support ARGB8888 only */
495 if (var->bits_per_pixel > 32 || var->grayscale ||
496 var->red.offset > 16 || var->green.offset > 8 ||
497 var->blue.offset > 0 || var->transp.offset > 24 ||
498 var->red.length > 8 || var->green.length > 8 ||
499 var->blue.length > 8 || var->transp.length > 8 ||
500 var->red.msb_right || var->green.msb_right ||
501 var->blue.msb_right || var->transp.msb_right || var->nonstd) {
502 DPRINTK("We support ARGB8888 only\n");
503 return -EINVAL;
504 }
505
506 var->bits_per_pixel = 32;
507 var->red.offset = 16;
508 var->green.offset = 8;
509 var->blue.offset = 0;
510 var->transp.offset = 24;
511 var->red.length = 8;
512 var->green.length = 8;
513 var->blue.length = 8;
514 var->transp.length = 8;
515 var->red.msb_right = 0;
516 var->green.msb_right = 0;
517 var->blue.msb_right = 0;
518 var->transp.msb_right = 0;
519
520 /* Rotation is not supported */
521 if (var->rotate) {
522 DPRINTK("Rotation is not supported\n");
523 return -EINVAL;
524 }
525
526 /* Memory limit */
527 i = ps3fb_get_res_table(var->xres, var->yres);
528 if (ps3fb_res[i].xres*ps3fb_res[i].yres*BPP > ps3fb_videomemory.size) {
529 DPRINTK("Not enough memory\n");
530 return -ENOMEM;
531 }
532
533 var->height = -1;
534 var->width = -1;
535
536 return 0;
537}
538
539 /*
540 * This routine actually sets the video mode.
541 */
542
543static int ps3fb_set_par(struct fb_info *info)
544{
545 unsigned int mode;
546 int i;
547 unsigned long offset;
548 static int first = 1;
549
550 DPRINTK("xres:%d xv:%d yres:%d yv:%d clock:%d\n",
551 info->var.xres, info->var.xres_virtual,
552 info->var.yres, info->var.yres_virtual, info->var.pixclock);
553 i = ps3fb_get_res_table(info->var.xres, info->var.yres);
554 ps3fb.res_index = i;
555
556 mode = ps3fb_find_mode(&info->var, &info->fix.line_length);
557 if (!mode)
558 return -EINVAL;
559
560 offset = FB_OFF(i) + VP_OFF(i);
561 info->fix.smem_len = ps3fb_videomemory.size - offset;
562 info->screen_base = (char __iomem *)ps3fb.xdr_ea + offset;
563 memset(ps3fb.xdr_ea, 0, ps3fb_videomemory.size);
564
565 ps3fb.num_frames = ps3fb_videomemory.size/
566 (ps3fb_res[i].xres*ps3fb_res[i].yres*BPP);
567
568 /* Keep the special bits we cannot set using fb_var_screeninfo */
569 ps3fb_mode = (ps3fb_mode & ~PS3AV_MODE_MASK) | mode;
570
571 if (ps3av_set_video_mode(ps3fb_mode, first))
572 return -EINVAL;
573
574 first = 0;
575 return 0;
576}
577
578 /*
579 * Set a single color register. The values supplied are already
580 * rounded down to the hardware's capabilities (according to the
581 * entries in the var structure). Return != 0 for invalid regno.
582 */
583
584static int ps3fb_setcolreg(unsigned int regno, unsigned int red,
585 unsigned int green, unsigned int blue,
586 unsigned int transp, struct fb_info *info)
587{
588 if (regno >= 16)
589 return 1;
590
591 red >>= 8;
592 green >>= 8;
593 blue >>= 8;
594 transp >>= 8;
595
596 ((u32 *)info->pseudo_palette)[regno] = transp << 24 | red << 16 |
597 green << 8 | blue;
598 return 0;
599}
600
601 /*
602 * As we have a virtual frame buffer, we need our own mmap function
603 */
604
605static int ps3fb_mmap(struct fb_info *info, struct vm_area_struct *vma)
606{
607 unsigned long size, offset;
608 int i;
609
610 i = ps3fb_get_res_table(info->var.xres, info->var.yres);
611 if (i == -1)
612 return -EINVAL;
613
614 size = vma->vm_end - vma->vm_start;
615 offset = vma->vm_pgoff << PAGE_SHIFT;
616 if (offset + size > info->fix.smem_len)
617 return -EINVAL;
618
619 offset += info->fix.smem_start + FB_OFF(i) + VP_OFF(i);
620 if (remap_pfn_range(vma, vma->vm_start, offset >> PAGE_SHIFT,
621 size, vma->vm_page_prot))
622 return -EAGAIN;
623
624 printk(KERN_DEBUG "ps3fb: mmap framebuffer P(%lx)->V(%lx)\n", offset,
625 vma->vm_start);
626 return 0;
627}
628
629 /*
630 * Blank the display
631 */
632
633static int ps3fb_blank(int blank, struct fb_info *info)
634{
635 int retval;
636
637 DPRINTK("%s: blank:%d\n", __FUNCTION__, blank);
638 switch (blank) {
639 case FB_BLANK_POWERDOWN:
640 case FB_BLANK_HSYNC_SUSPEND:
641 case FB_BLANK_VSYNC_SUSPEND:
642 case FB_BLANK_NORMAL:
643 retval = ps3av_video_mute(1); /* mute on */
644 if (!retval)
645 ps3fb.is_blanked = 1;
646 break;
647
648 default: /* unblank */
649 retval = ps3av_video_mute(0); /* mute off */
650 if (!retval)
651 ps3fb.is_blanked = 0;
652 break;
653 }
654 return retval;
655}
656
657static int ps3fb_get_vblank(struct fb_vblank *vblank)
658{
659 memset(vblank, 0, sizeof(&vblank));
660 vblank->flags = FB_VBLANK_HAVE_VSYNC;
661 return 0;
662}
663
664int ps3fb_wait_for_vsync(u32 crtc)
665{
666 int ret;
667 u64 count;
668
669 count = ps3fb.vblank_count;
670 ret = wait_event_interruptible_timeout(ps3fb.wait_vsync,
671 count != ps3fb.vblank_count,
672 HZ / 10);
673 if (!ret)
674 return -ETIMEDOUT;
675
676 return 0;
677}
678
679EXPORT_SYMBOL_GPL(ps3fb_wait_for_vsync);
680
681void ps3fb_flip_ctl(int on)
682{
683 if (on) {
684 if (atomic_read(&ps3fb.ext_flip) > 0) {
685 atomic_dec(&ps3fb.ext_flip);
686 }
687 } else {
688 atomic_inc(&ps3fb.ext_flip);
689 }
690}
691
692EXPORT_SYMBOL_GPL(ps3fb_flip_ctl);
693
694 /*
695 * ioctl
696 */
697
698static int ps3fb_ioctl(struct fb_info *info, unsigned int cmd,
699 unsigned long arg)
700{
701 void __user *argp = (void __user *)arg;
702 u32 val, old_mode;
703 int retval = -EFAULT;
704
705 switch (cmd) {
706 case FBIOGET_VBLANK:
707 {
708 struct fb_vblank vblank;
709 DPRINTK("FBIOGET_VBLANK:\n");
710 retval = ps3fb_get_vblank(&vblank);
711 if (retval)
712 break;
713
714 if (copy_to_user(argp, &vblank, sizeof(vblank)))
715 retval = -EFAULT;
716 break;
717 }
718
719 case FBIO_WAITFORVSYNC:
720 {
721 u32 crt;
722 DPRINTK("FBIO_WAITFORVSYNC:\n");
723 if (get_user(crt, (u32 __user *) arg))
724 break;
725
726 retval = ps3fb_wait_for_vsync(crt);
727 break;
728 }
729
730 case PS3FB_IOCTL_SETMODE:
731 {
732 const struct fb_videomode *mode;
733 struct fb_var_screeninfo var;
734
735 if (copy_from_user(&val, argp, sizeof(val)))
736 break;
737
738 DPRINTK("PS3FB_IOCTL_SETMODE:%x\n", val);
739 retval = -EINVAL;
740 old_mode = ps3fb_mode;
741 ps3fb_mode = val;
742 mode = ps3fb_default_mode();
743 if (mode) {
744 var = info->var;
745 fb_videomode_to_var(&var, mode);
746 acquire_console_sem();
747 info->flags |= FBINFO_MISC_USEREVENT;
748 /* Force, in case only special bits changed */
749 var.activate |= FB_ACTIVATE_FORCE;
750 retval = fb_set_var(info, &var);
751 info->flags &= ~FBINFO_MISC_USEREVENT;
752 release_console_sem();
753 }
754 if (retval)
755 ps3fb_mode = old_mode;
756 break;
757 }
758
759 case PS3FB_IOCTL_GETMODE:
760 val = ps3av_get_mode();
761 DPRINTK("PS3FB_IOCTL_GETMODE:%x\n", val);
762 if (!copy_to_user(argp, &val, sizeof(val)))
763 retval = 0;
764 break;
765
766 case PS3FB_IOCTL_SCREENINFO:
767 {
768 struct ps3fb_ioctl_res res;
769 int i = ps3fb.res_index;
770 DPRINTK("PS3FB_IOCTL_SCREENINFO:\n");
771 res.xres = ps3fb_res[i].xres;
772 res.yres = ps3fb_res[i].yres;
773 res.xoff = ps3fb_res[i].xoff;
774 res.yoff = ps3fb_res[i].yoff;
775 res.num_frames = ps3fb.num_frames;
776 if (!copy_to_user(argp, &res, sizeof(res)))
777 retval = 0;
778 break;
779 }
780
781 case PS3FB_IOCTL_ON:
782 DPRINTK("PS3FB_IOCTL_ON:\n");
783 atomic_inc(&ps3fb.ext_flip);
784 retval = 0;
785 break;
786
787 case PS3FB_IOCTL_OFF:
788 DPRINTK("PS3FB_IOCTL_OFF:\n");
789 if (atomic_read(&ps3fb.ext_flip) > 0)
790 atomic_dec(&ps3fb.ext_flip);
791 retval = 0;
792 break;
793
794 case PS3FB_IOCTL_FSEL:
795 if (copy_from_user(&val, argp, sizeof(val)))
796 break;
797
798 DPRINTK("PS3FB_IOCTL_FSEL:%d\n", val);
799 retval = ps3fb_sync(val);
800 break;
801
802 default:
803 retval = -ENOIOCTLCMD;
804 break;
805 }
806 return retval;
807}
808
809static int ps3fbd(void *arg)
810{
Geert Uytterhoeven1c0c8462007-05-02 14:48:31 +0200811 while (!kthread_should_stop()) {
812 try_to_freeze();
813 set_current_state(TASK_INTERRUPTIBLE);
814 if (ps3fb.is_kicked) {
815 ps3fb.is_kicked = 0;
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800816 ps3fb_sync(0); /* single buffer */
Geert Uytterhoeven1c0c8462007-05-02 14:48:31 +0200817 }
818 schedule();
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800819 }
820 return 0;
821}
822
823static irqreturn_t ps3fb_vsync_interrupt(int irq, void *ptr)
824{
825 u64 v1;
826 int status;
827 struct display_head *head = &ps3fb.dinfo->display_head[1];
828
829 status = lv1_gpu_context_intr(ps3fb.context_handle, &v1);
830 if (status) {
831 printk(KERN_ERR "%s: lv1_gpu_context_intr failed: %d\n",
832 __FUNCTION__, status);
833 return IRQ_NONE;
834 }
835
836 if (v1 & (1 << GPU_INTR_STATUS_VSYNC_1)) {
837 /* VSYNC */
838 ps3fb.vblank_count = head->vblank_count;
Geert Uytterhoeven1c0c8462007-05-02 14:48:31 +0200839 if (ps3fb.task && !ps3fb.is_blanked &&
840 !atomic_read(&ps3fb.ext_flip)) {
841 ps3fb.is_kicked = 1;
842 wake_up_process(ps3fb.task);
843 }
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800844 wake_up_interruptible(&ps3fb.wait_vsync);
845 }
846
847 return IRQ_HANDLED;
848}
849
850#ifndef MODULE
851static int __init ps3fb_setup(char *options)
852{
853 char *this_opt;
854 int mode = 0;
855
856 if (!options || !*options)
857 return 0; /* no options */
858
859 while ((this_opt = strsep(&options, ",")) != NULL) {
860 if (!*this_opt)
861 continue;
862 if (!strncmp(this_opt, "mode:", 5))
863 mode = simple_strtoul(this_opt + 5, NULL, 0);
864 else
865 mode_option = this_opt;
866 }
867 return mode;
868}
869#endif /* MODULE */
870
871 /*
872 * Initialisation
873 */
874
875static void ps3fb_platform_release(struct device *device)
876{
877 /* This is called when the reference count goes to zero. */
878}
879
880static int ps3fb_vsync_settings(struct gpu_driver_info *dinfo, void *dev)
881{
882 int error;
883
884 DPRINTK("version_driver:%x\n", dinfo->version_driver);
885 DPRINTK("irq outlet:%x\n", dinfo->irq.irq_outlet);
886 DPRINTK("version_gpu:%x memory_size:%x ch:%x core_freq:%d mem_freq:%d\n",
887 dinfo->version_gpu, dinfo->memory_size, dinfo->hardware_channel,
888 dinfo->nvcore_frequency/1000000, dinfo->memory_frequency/1000000);
889
890 if (dinfo->version_driver != GPU_DRIVER_INFO_VERSION) {
891 printk(KERN_ERR "%s: version_driver err:%x\n", __FUNCTION__,
892 dinfo->version_driver);
893 return -EINVAL;
894 }
895
896 ps3fb.dev = dev;
897 error = ps3_alloc_irq(PS3_BINDING_CPU_ANY, dinfo->irq.irq_outlet,
898 &ps3fb.irq_no);
899 if (error) {
900 printk(KERN_ERR "%s: ps3_alloc_irq failed %d\n", __FUNCTION__,
901 error);
902 return error;
903 }
904
905 error = request_irq(ps3fb.irq_no, ps3fb_vsync_interrupt, IRQF_DISABLED,
906 "ps3fb vsync", ps3fb.dev);
907 if (error) {
908 printk(KERN_ERR "%s: request_irq failed %d\n", __FUNCTION__,
909 error);
910 ps3_free_irq(ps3fb.irq_no);
911 return error;
912 }
913
914 dinfo->irq.mask = (1 << GPU_INTR_STATUS_VSYNC_1) |
915 (1 << GPU_INTR_STATUS_FLIP_1);
916 return 0;
917}
918
919static int ps3fb_xdr_settings(u64 xdr_lpar)
920{
921 int status;
922
923 status = lv1_gpu_context_iomap(ps3fb.context_handle, GPU_IOIF,
924 xdr_lpar, ps3fb_videomemory.size, 0);
925 if (status) {
926 printk(KERN_ERR "%s: lv1_gpu_context_iomap failed: %d\n",
927 __FUNCTION__, status);
928 return -ENXIO;
929 }
930 DPRINTK("video:%p xdr_ea:%p ioif:%lx lpar:%lx phys:%lx size:%lx\n",
931 ps3fb_videomemory.address, ps3fb.xdr_ea, GPU_IOIF, xdr_lpar,
932 virt_to_abs(ps3fb.xdr_ea), ps3fb_videomemory.size);
933
934 status = lv1_gpu_context_attribute(ps3fb.context_handle,
935 L1GPU_CONTEXT_ATTRIBUTE_FB_SETUP,
936 xdr_lpar, ps3fb_videomemory.size,
937 GPU_IOIF, 0);
938 if (status) {
939 printk(KERN_ERR "%s: lv1_gpu_context_attribute FB_SETUP failed: %d\n",
940 __FUNCTION__, status);
941 return -ENXIO;
942 }
943 return 0;
944}
945
946static struct fb_ops ps3fb_ops = {
947 .fb_open = ps3fb_open,
948 .fb_release = ps3fb_release,
949 .fb_check_var = ps3fb_check_var,
950 .fb_set_par = ps3fb_set_par,
951 .fb_setcolreg = ps3fb_setcolreg,
952 .fb_fillrect = cfb_fillrect,
953 .fb_copyarea = cfb_copyarea,
954 .fb_imageblit = cfb_imageblit,
955 .fb_mmap = ps3fb_mmap,
956 .fb_blank = ps3fb_blank,
957 .fb_ioctl = ps3fb_ioctl,
958 .fb_compat_ioctl = ps3fb_ioctl
959};
960
961static struct fb_fix_screeninfo ps3fb_fix __initdata = {
962 .id = "PS3 FB",
963 .type = FB_TYPE_PACKED_PIXELS,
964 .visual = FB_VISUAL_TRUECOLOR,
965 .accel = FB_ACCEL_NONE,
966};
967
968static int __init ps3fb_probe(struct platform_device *dev)
969{
970 struct fb_info *info;
971 int retval = -ENOMEM;
972 u64 ddr_lpar = 0;
973 u64 lpar_dma_control = 0;
974 u64 lpar_driver_info = 0;
975 u64 lpar_reports = 0;
976 u64 lpar_reports_size = 0;
977 u64 xdr_lpar;
978 int status;
979 unsigned long offset;
Geert Uytterhoeven1c0c8462007-05-02 14:48:31 +0200980 struct task_struct *task;
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800981
982 /* get gpu context handle */
983 status = lv1_gpu_memory_allocate(DDR_SIZE, 0, 0, 0, 0,
984 &ps3fb.memory_handle, &ddr_lpar);
985 if (status) {
986 printk(KERN_ERR "%s: lv1_gpu_memory_allocate failed: %d\n",
987 __FUNCTION__, status);
988 goto err;
989 }
990 DPRINTK("ddr:lpar:0x%lx\n", ddr_lpar);
991
992 status = lv1_gpu_context_allocate(ps3fb.memory_handle, 0,
993 &ps3fb.context_handle,
994 &lpar_dma_control, &lpar_driver_info,
995 &lpar_reports, &lpar_reports_size);
996 if (status) {
997 printk(KERN_ERR "%s: lv1_gpu_context_attribute failed: %d\n",
998 __FUNCTION__, status);
999 goto err_gpu_memory_free;
1000 }
1001
1002 /* vsync interrupt */
1003 ps3fb.dinfo = ioremap(lpar_driver_info, 128 * 1024);
1004 if (!ps3fb.dinfo) {
1005 printk(KERN_ERR "%s: ioremap failed\n", __FUNCTION__);
1006 goto err_gpu_context_free;
1007 }
1008
1009 retval = ps3fb_vsync_settings(ps3fb.dinfo, dev);
1010 if (retval)
1011 goto err_iounmap_dinfo;
1012
1013 /* xdr frame buffer */
1014 ps3fb.xdr_ea = ps3fb_videomemory.address;
1015 xdr_lpar = ps3_mm_phys_to_lpar(__pa(ps3fb.xdr_ea));
1016 retval = ps3fb_xdr_settings(xdr_lpar);
1017 if (retval)
1018 goto err_free_irq;
1019
1020 /*
1021 * ps3fb must clear memory to prevent kernel info
1022 * leakage into userspace
1023 */
1024 memset(ps3fb.xdr_ea, 0, ps3fb_videomemory.size);
1025 info = framebuffer_alloc(sizeof(u32) * 16, &dev->dev);
1026 if (!info)
1027 goto err_free_irq;
1028
1029 offset = FB_OFF(ps3fb.res_index) + VP_OFF(ps3fb.res_index);
1030 info->screen_base = (char __iomem *)ps3fb.xdr_ea + offset;
1031 info->fbops = &ps3fb_ops;
1032
1033 info->fix = ps3fb_fix;
1034 info->fix.smem_start = virt_to_abs(ps3fb.xdr_ea);
1035 info->fix.smem_len = ps3fb_videomemory.size - offset;
1036 info->pseudo_palette = info->par;
1037 info->par = NULL;
1038 info->flags = FBINFO_FLAG_DEFAULT;
1039
1040 retval = fb_alloc_cmap(&info->cmap, 256, 0);
1041 if (retval < 0)
1042 goto err_framebuffer_release;
1043
1044 if (!fb_find_mode(&info->var, info, mode_option, ps3fb_modedb,
1045 ARRAY_SIZE(ps3fb_modedb), ps3fb_default_mode(), 32)) {
1046 retval = -EINVAL;
1047 goto err_fb_dealloc;
1048 }
1049
1050 fb_videomode_to_modelist(ps3fb_modedb, ARRAY_SIZE(ps3fb_modedb),
1051 &info->modelist);
1052
1053 retval = register_framebuffer(info);
1054 if (retval < 0)
1055 goto err_fb_dealloc;
1056
1057 platform_set_drvdata(dev, info);
1058
1059 printk(KERN_INFO
1060 "fb%d: PS3 frame buffer device, using %ld KiB of video memory\n",
1061 info->node, ps3fb_videomemory.size >> 10);
1062
Geert Uytterhoeven1c0c8462007-05-02 14:48:31 +02001063 task = kthread_run(ps3fbd, info, "ps3fbd");
1064 if (IS_ERR(task)) {
1065 retval = PTR_ERR(task);
1066 goto err_unregister_framebuffer;
1067 }
1068
1069 ps3fb.task = task;
1070
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001071 return 0;
1072
Geert Uytterhoeven1c0c8462007-05-02 14:48:31 +02001073err_unregister_framebuffer:
1074 unregister_framebuffer(info);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001075err_fb_dealloc:
1076 fb_dealloc_cmap(&info->cmap);
1077err_framebuffer_release:
1078 framebuffer_release(info);
1079err_free_irq:
1080 free_irq(ps3fb.irq_no, ps3fb.dev);
1081 ps3_free_irq(ps3fb.irq_no);
1082err_iounmap_dinfo:
1083 iounmap((u8 __iomem *)ps3fb.dinfo);
1084err_gpu_context_free:
1085 lv1_gpu_context_free(ps3fb.context_handle);
1086err_gpu_memory_free:
1087 lv1_gpu_memory_free(ps3fb.memory_handle);
1088err:
1089 return retval;
1090}
1091
1092static void ps3fb_shutdown(struct platform_device *dev)
1093{
1094 ps3fb_flip_ctl(0); /* flip off */
1095 ps3fb.dinfo->irq.mask = 0;
1096 free_irq(ps3fb.irq_no, ps3fb.dev);
1097 ps3_free_irq(ps3fb.irq_no);
1098 iounmap((u8 __iomem *)ps3fb.dinfo);
1099}
1100
1101void ps3fb_cleanup(void)
1102{
1103 int status;
1104
Geert Uytterhoeven1c0c8462007-05-02 14:48:31 +02001105 if (ps3fb.task) {
1106 struct task_struct *task = ps3fb.task;
1107 ps3fb.task = NULL;
1108 kthread_stop(task);
1109 }
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001110 if (ps3fb.irq_no) {
1111 free_irq(ps3fb.irq_no, ps3fb.dev);
1112 ps3_free_irq(ps3fb.irq_no);
1113 }
1114 iounmap((u8 __iomem *)ps3fb.dinfo);
1115
1116 status = lv1_gpu_context_free(ps3fb.context_handle);
1117 if (status)
1118 DPRINTK("lv1_gpu_context_free failed: %d\n", status);
1119
1120 status = lv1_gpu_memory_free(ps3fb.memory_handle);
1121 if (status)
1122 DPRINTK("lv1_gpu_memory_free failed: %d\n", status);
1123
1124 ps3av_dev_close();
1125}
1126
1127EXPORT_SYMBOL_GPL(ps3fb_cleanup);
1128
1129static int ps3fb_remove(struct platform_device *dev)
1130{
1131 struct fb_info *info = platform_get_drvdata(dev);
1132
1133 if (info) {
1134 unregister_framebuffer(info);
1135 fb_dealloc_cmap(&info->cmap);
1136 framebuffer_release(info);
1137 }
1138 ps3fb_cleanup();
1139 return 0;
1140}
1141
1142static struct platform_driver ps3fb_driver = {
1143 .probe = ps3fb_probe,
1144 .remove = ps3fb_remove,
1145 .shutdown = ps3fb_shutdown,
1146 .driver = { .name = "ps3fb" }
1147};
1148
1149static struct platform_device ps3fb_device = {
1150 .name = "ps3fb",
1151 .id = 0,
1152 .dev = { .release = ps3fb_platform_release }
1153};
1154
1155int ps3fb_set_sync(void)
1156{
1157 int status;
1158
1159#ifdef HEAD_A
1160 status = lv1_gpu_context_attribute(0x0,
1161 L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_SYNC,
1162 0, L1GPU_DISPLAY_SYNC_VSYNC, 0, 0);
1163 if (status) {
1164 printk(KERN_ERR "%s: lv1_gpu_context_attribute DISPLAY_SYNC failed: %d\n",
1165 __FUNCTION__, status);
1166 return -1;
1167 }
1168#endif
1169#ifdef HEAD_B
1170 status = lv1_gpu_context_attribute(0x0,
1171 L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_SYNC,
1172 1, L1GPU_DISPLAY_SYNC_VSYNC, 0, 0);
1173
1174 if (status) {
1175 printk(KERN_ERR "%s: lv1_gpu_context_attribute DISPLAY_MODE failed: %d\n",
1176 __FUNCTION__, status);
1177 return -1;
1178 }
1179#endif
1180 return 0;
1181}
1182
1183EXPORT_SYMBOL_GPL(ps3fb_set_sync);
1184
1185static int __init ps3fb_init(void)
1186{
1187 int error;
1188#ifndef MODULE
1189 int mode;
1190 char *option = NULL;
1191
1192 if (fb_get_options("ps3fb", &option))
1193 goto err;
1194#endif
1195
1196 if (!ps3fb_videomemory.address)
1197 goto err;
1198
1199 error = ps3av_dev_open();
1200 if (error) {
1201 printk(KERN_ERR "%s: ps3av_dev_open failed\n", __FUNCTION__);
1202 goto err;
1203 }
1204
1205 ps3fb_mode = ps3av_get_mode();
1206 DPRINTK("ps3av_mode:%d\n", ps3fb_mode);
1207#ifndef MODULE
1208 mode = ps3fb_setup(option); /* check boot option */
1209 if (mode)
1210 ps3fb_mode = mode;
1211#endif
1212 if (ps3fb_mode > 0) {
1213 u32 xres, yres;
1214 ps3av_video_mode2res(ps3fb_mode, &xres, &yres);
1215 ps3fb.res_index = ps3fb_get_res_table(xres, yres);
1216 DPRINTK("res_index:%d\n", ps3fb.res_index);
1217 } else
1218 ps3fb.res_index = GPU_RES_INDEX;
1219
1220 atomic_set(&ps3fb.f_count, -1); /* fbcon opens ps3fb */
1221 atomic_set(&ps3fb.ext_flip, 0); /* for flip with vsync */
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001222 init_waitqueue_head(&ps3fb.wait_vsync);
1223 ps3fb.num_frames = 1;
1224
1225 error = platform_driver_register(&ps3fb_driver);
1226 if (!error) {
1227 error = platform_device_register(&ps3fb_device);
1228 if (error)
1229 platform_driver_unregister(&ps3fb_driver);
1230 }
1231
1232 ps3fb_set_sync();
1233
1234 return error;
1235
1236err:
1237 return -ENXIO;
1238}
1239
1240module_init(ps3fb_init);
1241
1242#ifdef MODULE
1243static void __exit ps3fb_exit(void)
1244{
1245 platform_device_unregister(&ps3fb_device);
1246 platform_driver_unregister(&ps3fb_driver);
1247}
1248
1249module_exit(ps3fb_exit);
1250
1251MODULE_LICENSE("GPL");
1252#endif /* MODULE */