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