]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/video/ps3fb.c
4885978dd5be3f9ba8555ef2a1fe54059dc94e5e
[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 static 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 static void ps3fb_flip_ctl(int on, void *data)
671 {
672         struct ps3fb_priv *priv = data;
673         if (on)
674                 atomic_dec_if_positive(&priv->ext_flip);
675         else
676                 atomic_inc(&priv->ext_flip);
677 }
678
679
680     /*
681      * ioctl
682      */
683
684 static int ps3fb_ioctl(struct fb_info *info, unsigned int cmd,
685                        unsigned long arg)
686 {
687         void __user *argp = (void __user *)arg;
688         u32 val, old_mode;
689         int retval = -EFAULT;
690
691         switch (cmd) {
692         case FBIOGET_VBLANK:
693                 {
694                         struct fb_vblank vblank;
695                         dev_dbg(info->device, "FBIOGET_VBLANK:\n");
696                         retval = ps3fb_get_vblank(&vblank);
697                         if (retval)
698                                 break;
699
700                         if (copy_to_user(argp, &vblank, sizeof(vblank)))
701                                 retval = -EFAULT;
702                         break;
703                 }
704
705         case FBIO_WAITFORVSYNC:
706                 {
707                         u32 crt;
708                         dev_dbg(info->device, "FBIO_WAITFORVSYNC:\n");
709                         if (get_user(crt, (u32 __user *) arg))
710                                 break;
711
712                         retval = ps3fb_wait_for_vsync(crt);
713                         break;
714                 }
715
716         case PS3FB_IOCTL_SETMODE:
717                 {
718                         const struct fb_videomode *mode;
719                         struct fb_var_screeninfo var;
720
721                         if (copy_from_user(&val, argp, sizeof(val)))
722                                 break;
723
724                         if (!(val & PS3AV_MODE_MASK)) {
725                                 u32 id = ps3av_get_auto_mode();
726                                 if (id > 0)
727                                         val = (val & ~PS3AV_MODE_MASK) | id;
728                         }
729                         dev_dbg(info->device, "PS3FB_IOCTL_SETMODE:%x\n", val);
730                         retval = -EINVAL;
731                         old_mode = ps3fb_mode;
732                         ps3fb_mode = val;
733                         mode = ps3fb_default_mode();
734                         if (mode) {
735                                 var = info->var;
736                                 fb_videomode_to_var(&var, mode);
737                                 acquire_console_sem();
738                                 info->flags |= FBINFO_MISC_USEREVENT;
739                                 /* Force, in case only special bits changed */
740                                 var.activate |= FB_ACTIVATE_FORCE;
741                                 retval = fb_set_var(info, &var);
742                                 info->flags &= ~FBINFO_MISC_USEREVENT;
743                                 release_console_sem();
744                         }
745                         if (retval)
746                                 ps3fb_mode = old_mode;
747                         break;
748                 }
749
750         case PS3FB_IOCTL_GETMODE:
751                 val = ps3av_get_mode();
752                 dev_dbg(info->device, "PS3FB_IOCTL_GETMODE:%x\n", val);
753                 if (!copy_to_user(argp, &val, sizeof(val)))
754                         retval = 0;
755                 break;
756
757         case PS3FB_IOCTL_SCREENINFO:
758                 {
759                         struct ps3fb_ioctl_res res;
760                         int i = ps3fb.res_index;
761                         dev_dbg(info->device, "PS3FB_IOCTL_SCREENINFO:\n");
762                         res.xres = ps3fb_res[i].xres;
763                         res.yres = ps3fb_res[i].yres;
764                         res.xoff = ps3fb_res[i].xoff;
765                         res.yoff = ps3fb_res[i].yoff;
766                         res.num_frames = ps3fb.num_frames;
767                         if (!copy_to_user(argp, &res, sizeof(res)))
768                                 retval = 0;
769                         break;
770                 }
771
772         case PS3FB_IOCTL_ON:
773                 dev_dbg(info->device, "PS3FB_IOCTL_ON:\n");
774                 atomic_inc(&ps3fb.ext_flip);
775                 retval = 0;
776                 break;
777
778         case PS3FB_IOCTL_OFF:
779                 dev_dbg(info->device, "PS3FB_IOCTL_OFF:\n");
780                 atomic_dec_if_positive(&ps3fb.ext_flip);
781                 retval = 0;
782                 break;
783
784         case PS3FB_IOCTL_FSEL:
785                 if (copy_from_user(&val, argp, sizeof(val)))
786                         break;
787
788                 dev_dbg(info->device, "PS3FB_IOCTL_FSEL:%d\n", val);
789                 retval = ps3fb_sync(info, val);
790                 break;
791
792         default:
793                 retval = -ENOIOCTLCMD;
794                 break;
795         }
796         return retval;
797 }
798
799 static int ps3fbd(void *arg)
800 {
801         struct fb_info *info = arg;
802
803         set_freezable();
804         while (!kthread_should_stop()) {
805                 try_to_freeze();
806                 set_current_state(TASK_INTERRUPTIBLE);
807                 if (ps3fb.is_kicked) {
808                         ps3fb.is_kicked = 0;
809                         ps3fb_sync(info, 0);    /* single buffer */
810                 }
811                 schedule();
812         }
813         return 0;
814 }
815
816 static irqreturn_t ps3fb_vsync_interrupt(int irq, void *ptr)
817 {
818         struct device *dev = ptr;
819         u64 v1;
820         int status;
821         struct display_head *head = &ps3fb.dinfo->display_head[1];
822
823         status = lv1_gpu_context_intr(ps3fb.context_handle, &v1);
824         if (status) {
825                 dev_err(dev, "%s: lv1_gpu_context_intr failed: %d\n", __func__,
826                         status);
827                 return IRQ_NONE;
828         }
829
830         if (v1 & (1 << GPU_INTR_STATUS_VSYNC_1)) {
831                 /* VSYNC */
832                 ps3fb.vblank_count = head->vblank_count;
833                 if (ps3fb.task && !ps3fb.is_blanked &&
834                     !atomic_read(&ps3fb.ext_flip)) {
835                         ps3fb.is_kicked = 1;
836                         wake_up_process(ps3fb.task);
837                 }
838                 wake_up_interruptible(&ps3fb.wait_vsync);
839         }
840
841         return IRQ_HANDLED;
842 }
843
844
845 static int ps3fb_vsync_settings(struct gpu_driver_info *dinfo,
846                                 struct device *dev)
847 {
848         int error;
849
850         dev_dbg(dev, "version_driver:%x\n", dinfo->version_driver);
851         dev_dbg(dev, "irq outlet:%x\n", dinfo->irq.irq_outlet);
852         dev_dbg(dev,
853                 "version_gpu: %x memory_size: %x ch: %x core_freq: %d "
854                 "mem_freq:%d\n",
855                 dinfo->version_gpu, dinfo->memory_size, dinfo->hardware_channel,
856                 dinfo->nvcore_frequency/1000000, dinfo->memory_frequency/1000000);
857
858         if (dinfo->version_driver != GPU_DRIVER_INFO_VERSION) {
859                 dev_err(dev, "%s: version_driver err:%x\n", __func__,
860                         dinfo->version_driver);
861                 return -EINVAL;
862         }
863
864         error = ps3_irq_plug_setup(PS3_BINDING_CPU_ANY, dinfo->irq.irq_outlet,
865                                    &ps3fb.irq_no);
866         if (error) {
867                 dev_err(dev, "%s: ps3_alloc_irq failed %d\n", __func__, error);
868                 return error;
869         }
870
871         error = request_irq(ps3fb.irq_no, ps3fb_vsync_interrupt, IRQF_DISABLED,
872                             DEVICE_NAME, dev);
873         if (error) {
874                 dev_err(dev, "%s: request_irq failed %d\n", __func__, error);
875                 ps3_irq_plug_destroy(ps3fb.irq_no);
876                 return error;
877         }
878
879         dinfo->irq.mask = (1 << GPU_INTR_STATUS_VSYNC_1) |
880                           (1 << GPU_INTR_STATUS_FLIP_1);
881         return 0;
882 }
883
884 static int ps3fb_xdr_settings(u64 xdr_lpar, struct device *dev)
885 {
886         int status;
887
888         status = lv1_gpu_context_iomap(ps3fb.context_handle, GPU_IOIF,
889                                        xdr_lpar, ps3fb_videomemory.size, 0);
890         if (status) {
891                 dev_err(dev, "%s: lv1_gpu_context_iomap failed: %d\n",
892                         __func__, status);
893                 return -ENXIO;
894         }
895         dev_dbg(dev,
896                 "video:%p xdr_ea:%p ioif:%lx lpar:%lx phys:%lx size:%lx\n",
897                 ps3fb_videomemory.address, ps3fb.xdr_ea, GPU_IOIF, xdr_lpar,
898                 virt_to_abs(ps3fb.xdr_ea), ps3fb_videomemory.size);
899
900         status = lv1_gpu_context_attribute(ps3fb.context_handle,
901                                            L1GPU_CONTEXT_ATTRIBUTE_FB_SETUP,
902                                            xdr_lpar, ps3fb_videomemory.size,
903                                            GPU_IOIF, 0);
904         if (status) {
905                 dev_err(dev,
906                         "%s: lv1_gpu_context_attribute FB_SETUP failed: %d\n",
907                         __func__, status);
908                 return -ENXIO;
909         }
910         return 0;
911 }
912
913 static struct fb_ops ps3fb_ops = {
914         .fb_open        = ps3fb_open,
915         .fb_release     = ps3fb_release,
916         .fb_read        = fb_sys_read,
917         .fb_write       = fb_sys_write,
918         .fb_check_var   = ps3fb_check_var,
919         .fb_set_par     = ps3fb_set_par,
920         .fb_setcolreg   = ps3fb_setcolreg,
921         .fb_fillrect    = sys_fillrect,
922         .fb_copyarea    = sys_copyarea,
923         .fb_imageblit   = sys_imageblit,
924         .fb_mmap        = ps3fb_mmap,
925         .fb_blank       = ps3fb_blank,
926         .fb_ioctl       = ps3fb_ioctl,
927         .fb_compat_ioctl = ps3fb_ioctl
928 };
929
930 static struct fb_fix_screeninfo ps3fb_fix __initdata = {
931         .id =           DEVICE_NAME,
932         .type =         FB_TYPE_PACKED_PIXELS,
933         .visual =       FB_VISUAL_TRUECOLOR,
934         .accel =        FB_ACCEL_NONE,
935 };
936
937 static int ps3fb_set_sync(struct device *dev)
938 {
939         int status;
940
941 #ifdef HEAD_A
942         status = lv1_gpu_context_attribute(0x0,
943                                            L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_SYNC,
944                                            0, L1GPU_DISPLAY_SYNC_VSYNC, 0, 0);
945         if (status) {
946                 dev_err(dev,
947                         "%s: lv1_gpu_context_attribute DISPLAY_SYNC failed: "
948                         "%d\n",
949                         __func__, status);
950                 return -1;
951         }
952 #endif
953 #ifdef HEAD_B
954         status = lv1_gpu_context_attribute(0x0,
955                                            L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_SYNC,
956                                            1, L1GPU_DISPLAY_SYNC_VSYNC, 0, 0);
957
958         if (status) {
959                 dev_err(dev,
960                         "%s: lv1_gpu_context_attribute DISPLAY_SYNC failed: "
961                         "%d\n",
962                         __func__, status);
963                 return -1;
964         }
965 #endif
966         return 0;
967 }
968
969 static int __devinit ps3fb_probe(struct ps3_system_bus_device *dev)
970 {
971         struct fb_info *info;
972         int retval = -ENOMEM;
973         u32 xres, yres;
974         u64 ddr_lpar = 0;
975         u64 lpar_dma_control = 0;
976         u64 lpar_driver_info = 0;
977         u64 lpar_reports = 0;
978         u64 lpar_reports_size = 0;
979         u64 xdr_lpar;
980         int status;
981         unsigned long offset;
982         struct task_struct *task;
983
984         status = ps3_open_hv_device(dev);
985         if (status) {
986                 dev_err(&dev->core, "%s: ps3_open_hv_device failed\n",
987                         __func__);
988                 goto err;
989         }
990
991         if (!ps3fb_mode)
992                 ps3fb_mode = ps3av_get_mode();
993         dev_dbg(&dev->core, "ps3av_mode:%d\n", ps3fb_mode);
994
995         if (ps3fb_mode > 0 &&
996             !ps3av_video_mode2res(ps3fb_mode, &xres, &yres)) {
997                 ps3fb.res_index = ps3fb_get_res_table(xres, yres, ps3fb_mode);
998                 dev_dbg(&dev->core, "res_index:%d\n", ps3fb.res_index);
999         } else
1000                 ps3fb.res_index = GPU_RES_INDEX;
1001
1002         atomic_set(&ps3fb.f_count, -1); /* fbcon opens ps3fb */
1003         atomic_set(&ps3fb.ext_flip, 0); /* for flip with vsync */
1004         init_waitqueue_head(&ps3fb.wait_vsync);
1005         ps3fb.num_frames = 1;
1006
1007         ps3fb_set_sync(&dev->core);
1008
1009         /* get gpu context handle */
1010         status = lv1_gpu_memory_allocate(DDR_SIZE, 0, 0, 0, 0,
1011                                          &ps3fb.memory_handle, &ddr_lpar);
1012         if (status) {
1013                 dev_err(&dev->core, "%s: lv1_gpu_memory_allocate failed: %d\n",
1014                         __func__, status);
1015                 goto err;
1016         }
1017         dev_dbg(&dev->core, "ddr:lpar:0x%lx\n", ddr_lpar);
1018
1019         status = lv1_gpu_context_allocate(ps3fb.memory_handle, 0,
1020                                           &ps3fb.context_handle,
1021                                           &lpar_dma_control, &lpar_driver_info,
1022                                           &lpar_reports, &lpar_reports_size);
1023         if (status) {
1024                 dev_err(&dev->core,
1025                         "%s: lv1_gpu_context_attribute failed: %d\n", __func__,
1026                         status);
1027                 goto err_gpu_memory_free;
1028         }
1029
1030         /* vsync interrupt */
1031         ps3fb.dinfo = ioremap(lpar_driver_info, 128 * 1024);
1032         if (!ps3fb.dinfo) {
1033                 dev_err(&dev->core, "%s: ioremap failed\n", __func__);
1034                 goto err_gpu_context_free;
1035         }
1036
1037         retval = ps3fb_vsync_settings(ps3fb.dinfo, &dev->core);
1038         if (retval)
1039                 goto err_iounmap_dinfo;
1040
1041         /* xdr frame buffer */
1042         ps3fb.xdr_ea = ps3fb_videomemory.address;
1043         xdr_lpar = ps3_mm_phys_to_lpar(__pa(ps3fb.xdr_ea));
1044         retval = ps3fb_xdr_settings(xdr_lpar, &dev->core);
1045         if (retval)
1046                 goto err_free_irq;
1047
1048         /*
1049          * ps3fb must clear memory to prevent kernel info
1050          * leakage into userspace
1051          */
1052         memset(ps3fb.xdr_ea, 0, ps3fb_videomemory.size);
1053         info = framebuffer_alloc(sizeof(u32) * 16, &dev->core);
1054         if (!info)
1055                 goto err_free_irq;
1056
1057         offset = FB_OFF(ps3fb.res_index) + VP_OFF(ps3fb.res_index);
1058         info->screen_base = (char __iomem *)ps3fb.xdr_ea + offset;
1059         info->fbops = &ps3fb_ops;
1060
1061         info->fix = ps3fb_fix;
1062         info->fix.smem_start = virt_to_abs(ps3fb.xdr_ea);
1063         info->fix.smem_len = ps3fb_videomemory.size - offset;
1064         info->pseudo_palette = info->par;
1065         info->par = NULL;
1066         info->flags = FBINFO_DEFAULT | FBINFO_READS_FAST;
1067
1068         retval = fb_alloc_cmap(&info->cmap, 256, 0);
1069         if (retval < 0)
1070                 goto err_framebuffer_release;
1071
1072         if (!fb_find_mode(&info->var, info, mode_option, ps3fb_modedb,
1073                           ARRAY_SIZE(ps3fb_modedb), ps3fb_default_mode(), 32)) {
1074                 retval = -EINVAL;
1075                 goto err_fb_dealloc;
1076         }
1077
1078         fb_videomode_to_modelist(ps3fb_modedb, ARRAY_SIZE(ps3fb_modedb),
1079                                  &info->modelist);
1080
1081         retval = register_framebuffer(info);
1082         if (retval < 0)
1083                 goto err_fb_dealloc;
1084
1085         dev->core.driver_data = info;
1086
1087         dev_info(info->device, "%s %s, using %lu KiB of video memory\n",
1088                  dev_driver_string(info->dev), info->dev->bus_id,
1089                  ps3fb_videomemory.size >> 10);
1090
1091         task = kthread_run(ps3fbd, info, DEVICE_NAME);
1092         if (IS_ERR(task)) {
1093                 retval = PTR_ERR(task);
1094                 goto err_unregister_framebuffer;
1095         }
1096
1097         ps3fb.task = task;
1098         ps3av_register_flip_ctl(ps3fb_flip_ctl, &ps3fb);
1099
1100         return 0;
1101
1102 err_unregister_framebuffer:
1103         unregister_framebuffer(info);
1104 err_fb_dealloc:
1105         fb_dealloc_cmap(&info->cmap);
1106 err_framebuffer_release:
1107         framebuffer_release(info);
1108 err_free_irq:
1109         free_irq(ps3fb.irq_no, dev);
1110         ps3_irq_plug_destroy(ps3fb.irq_no);
1111 err_iounmap_dinfo:
1112         iounmap((u8 __iomem *)ps3fb.dinfo);
1113 err_gpu_context_free:
1114         lv1_gpu_context_free(ps3fb.context_handle);
1115 err_gpu_memory_free:
1116         lv1_gpu_memory_free(ps3fb.memory_handle);
1117 err:
1118         return retval;
1119 }
1120
1121 static int ps3fb_shutdown(struct ps3_system_bus_device *dev)
1122 {
1123         int status;
1124         struct fb_info *info = dev->core.driver_data;
1125
1126         dev_dbg(&dev->core, " -> %s:%d\n", __func__, __LINE__);
1127
1128         ps3fb_flip_ctl(0, &ps3fb);      /* flip off */
1129         ps3fb.dinfo->irq.mask = 0;
1130
1131         if (info) {
1132                 unregister_framebuffer(info);
1133                 fb_dealloc_cmap(&info->cmap);
1134                 framebuffer_release(info);
1135         }
1136
1137         ps3av_register_flip_ctl(NULL, NULL);
1138         if (ps3fb.task) {
1139                 struct task_struct *task = ps3fb.task;
1140                 ps3fb.task = NULL;
1141                 kthread_stop(task);
1142         }
1143         if (ps3fb.irq_no) {
1144                 free_irq(ps3fb.irq_no, dev);
1145                 ps3_irq_plug_destroy(ps3fb.irq_no);
1146         }
1147         iounmap((u8 __iomem *)ps3fb.dinfo);
1148
1149         status = lv1_gpu_context_free(ps3fb.context_handle);
1150         if (status)
1151                 dev_dbg(&dev->core, "lv1_gpu_context_free failed: %d\n",
1152                         status);
1153
1154         status = lv1_gpu_memory_free(ps3fb.memory_handle);
1155         if (status)
1156                 dev_dbg(&dev->core, "lv1_gpu_memory_free failed: %d\n",
1157                         status);
1158
1159         ps3_close_hv_device(dev);
1160         dev_dbg(&dev->core, " <- %s:%d\n", __func__, __LINE__);
1161
1162         return 0;
1163 }
1164
1165 static struct ps3_system_bus_driver ps3fb_driver = {
1166         .match_id       = PS3_MATCH_ID_GRAPHICS,
1167         .core.name      = DEVICE_NAME,
1168         .core.owner     = THIS_MODULE,
1169         .probe          = ps3fb_probe,
1170         .remove         = ps3fb_shutdown,
1171         .shutdown       = ps3fb_shutdown,
1172 };
1173
1174 static int __init ps3fb_setup(void)
1175 {
1176         char *options;
1177
1178 #ifdef MODULE
1179         return 0;
1180 #endif
1181
1182         if (fb_get_options(DEVICE_NAME, &options))
1183                 return -ENXIO;
1184
1185         if (!options || !*options)
1186                 return 0;
1187
1188         while (1) {
1189                 char *this_opt = strsep(&options, ",");
1190
1191                 if (!this_opt)
1192                         break;
1193                 if (!*this_opt)
1194                         continue;
1195                 if (!strncmp(this_opt, "mode:", 5))
1196                         ps3fb_mode = simple_strtoul(this_opt + 5, NULL, 0);
1197                 else
1198                         mode_option = this_opt;
1199         }
1200         return 0;
1201 }
1202
1203 static int __init ps3fb_init(void)
1204 {
1205         if (!ps3fb_videomemory.address ||  ps3fb_setup())
1206                 return -ENXIO;
1207
1208         return ps3_system_bus_driver_register(&ps3fb_driver);
1209 }
1210
1211 static void __exit ps3fb_exit(void)
1212 {
1213         pr_debug(" -> %s:%d\n", __func__, __LINE__);
1214         ps3_system_bus_driver_unregister(&ps3fb_driver);
1215         pr_debug(" <- %s:%d\n", __func__, __LINE__);
1216 }
1217
1218 module_init(ps3fb_init);
1219 module_exit(ps3fb_exit);
1220
1221 MODULE_LICENSE("GPL");
1222 MODULE_DESCRIPTION("PS3 GPU Frame Buffer Driver");
1223 MODULE_AUTHOR("Sony Computer Entertainment Inc.");
1224 MODULE_ALIAS(PS3_MODULE_ALIAS_GRAPHICS);