blob: 49069dba1b5d66171d046688603daf81e56c0947 [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>
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -080030#include <linux/console.h>
31#include <linux/ioctl.h>
32#include <linux/notifier.h>
33#include <linux/reboot.h>
Geert Uytterhoeven1c0c8462007-05-02 14:48:31 +020034#include <linux/kthread.h>
35#include <linux/freezer.h>
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -080036
Krzysztof Helt84902b72007-10-16 01:29:04 -070037#include <linux/uaccess.h>
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -080038#include <linux/fb.h>
39#include <linux/init.h>
40#include <asm/time.h>
41
42#include <asm/abs_addr.h>
43#include <asm/lv1call.h>
44#include <asm/ps3av.h>
45#include <asm/ps3fb.h>
46#include <asm/ps3.h>
47
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +100048
49#define DEVICE_NAME "ps3fb"
50
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -080051#define L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_SYNC 0x101
52#define L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_FLIP 0x102
53#define L1GPU_CONTEXT_ATTRIBUTE_FB_SETUP 0x600
54#define L1GPU_CONTEXT_ATTRIBUTE_FB_BLIT 0x601
55#define L1GPU_CONTEXT_ATTRIBUTE_FB_BLIT_SYNC 0x602
56
57#define L1GPU_FB_BLIT_WAIT_FOR_COMPLETION (1ULL << 32)
58
59#define L1GPU_DISPLAY_SYNC_HSYNC 1
60#define L1GPU_DISPLAY_SYNC_VSYNC 2
61
62#define DDR_SIZE (0) /* used no ddr */
63#define GPU_OFFSET (64 * 1024)
64#define GPU_IOIF (0x0d000000UL)
65
66#define PS3FB_FULL_MODE_BIT 0x80
67
68#define GPU_INTR_STATUS_VSYNC_0 0 /* vsync on head A */
69#define GPU_INTR_STATUS_VSYNC_1 1 /* vsync on head B */
70#define GPU_INTR_STATUS_FLIP_0 3 /* flip head A */
71#define GPU_INTR_STATUS_FLIP_1 4 /* flip head B */
72#define GPU_INTR_STATUS_QUEUE_0 5 /* queue head A */
73#define GPU_INTR_STATUS_QUEUE_1 6 /* queue head B */
74
75#define GPU_DRIVER_INFO_VERSION 0x211
76
77/* gpu internals */
78struct display_head {
79 u64 be_time_stamp;
80 u32 status;
81 u32 offset;
82 u32 res1;
83 u32 res2;
84 u32 field;
85 u32 reserved1;
86
87 u64 res3;
88 u32 raster;
89
90 u64 vblank_count;
91 u32 field_vsync;
92 u32 reserved2;
93};
94
95struct gpu_irq {
96 u32 irq_outlet;
97 u32 status;
98 u32 mask;
99 u32 video_cause;
100 u32 graph_cause;
101 u32 user_cause;
102
103 u32 res1;
104 u64 res2;
105
106 u32 reserved[4];
107};
108
109struct gpu_driver_info {
110 u32 version_driver;
111 u32 version_gpu;
112 u32 memory_size;
113 u32 hardware_channel;
114
115 u32 nvcore_frequency;
116 u32 memory_frequency;
117
118 u32 reserved[1063];
119 struct display_head display_head[8];
120 struct gpu_irq irq;
121};
122
123struct ps3fb_priv {
124 unsigned int irq_no;
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800125
126 u64 context_handle, memory_handle;
127 void *xdr_ea;
128 struct gpu_driver_info *dinfo;
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800129 u32 res_index;
130
131 u64 vblank_count; /* frame count */
132 wait_queue_head_t wait_vsync;
133
134 u32 num_frames; /* num of frame buffers */
135 atomic_t ext_flip; /* on/off flip with vsync */
136 atomic_t f_count; /* fb_open count */
137 int is_blanked;
Geert Uytterhoeven1c0c8462007-05-02 14:48:31 +0200138 int is_kicked;
139 struct task_struct *task;
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800140};
141static struct ps3fb_priv ps3fb;
142
143struct ps3fb_res_table {
144 u32 xres;
145 u32 yres;
146 u32 xoff;
147 u32 yoff;
148 u32 type;
149};
150#define PS3FB_RES_FULL 1
151static const struct ps3fb_res_table ps3fb_res[] = {
152 /* res_x,y margin_x,y full */
153 { 720, 480, 72, 48 , 0},
154 { 720, 576, 72, 58 , 0},
155 { 1280, 720, 78, 38 , 0},
156 { 1920, 1080, 116, 58 , 0},
157 /* full mode */
158 { 720, 480, 0, 0 , PS3FB_RES_FULL},
159 { 720, 576, 0, 0 , PS3FB_RES_FULL},
160 { 1280, 720, 0, 0 , PS3FB_RES_FULL},
161 { 1920, 1080, 0, 0 , PS3FB_RES_FULL},
162 /* vesa: normally full mode */
163 { 1280, 768, 0, 0 , 0},
164 { 1280, 1024, 0, 0 , 0},
165 { 1920, 1200, 0, 0 , 0},
166 { 0, 0, 0, 0 , 0} };
167
168/* default resolution */
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +1000169#define GPU_RES_INDEX 0 /* 720 x 480 */
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800170
171static const struct fb_videomode ps3fb_modedb[] = {
172 /* 60 Hz broadcast modes (modes "1" to "5") */
173 {
174 /* 480i */
175 "480i", 60, 576, 384, 74074, 130, 89, 78, 57, 63, 6,
176 FB_SYNC_BROADCAST, FB_VMODE_INTERLACED
177 }, {
178 /* 480p */
179 "480p", 60, 576, 384, 37037, 130, 89, 78, 57, 63, 6,
180 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
181 }, {
182 /* 720p */
183 "720p", 60, 1124, 644, 13481, 298, 148, 57, 44, 80, 5,
184 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
185 }, {
186 /* 1080i */
187 "1080i", 60, 1688, 964, 13481, 264, 160, 94, 62, 88, 5,
188 FB_SYNC_BROADCAST, FB_VMODE_INTERLACED
189 }, {
190 /* 1080p */
191 "1080p", 60, 1688, 964, 6741, 264, 160, 94, 62, 88, 5,
192 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
193 },
194
195 /* 50 Hz broadcast modes (modes "6" to "10") */
196 {
197 /* 576i */
198 "576i", 50, 576, 460, 74074, 142, 83, 97, 63, 63, 5,
199 FB_SYNC_BROADCAST, FB_VMODE_INTERLACED
200 }, {
201 /* 576p */
202 "576p", 50, 576, 460, 37037, 142, 83, 97, 63, 63, 5,
203 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
204 }, {
205 /* 720p */
206 "720p", 50, 1124, 644, 13468, 298, 478, 57, 44, 80, 5,
207 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
208 }, {
209 /* 1080 */
210 "1080i", 50, 1688, 964, 13468, 264, 600, 94, 62, 88, 5,
211 FB_SYNC_BROADCAST, FB_VMODE_INTERLACED
212 }, {
213 /* 1080p */
214 "1080p", 50, 1688, 964, 6734, 264, 600, 94, 62, 88, 5,
215 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
216 },
217
218 /* VESA modes (modes "11" to "13") */
219 {
220 /* WXGA */
221 "wxga", 60, 1280, 768, 12924, 160, 24, 29, 3, 136, 6,
222 0, FB_VMODE_NONINTERLACED,
223 FB_MODE_IS_VESA
224 }, {
225 /* SXGA */
226 "sxga", 60, 1280, 1024, 9259, 248, 48, 38, 1, 112, 3,
227 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED,
228 FB_MODE_IS_VESA
229 }, {
230 /* WUXGA */
231 "wuxga", 60, 1920, 1200, 6494, 80, 48, 26, 3, 32, 6,
232 FB_SYNC_HOR_HIGH_ACT, FB_VMODE_NONINTERLACED,
233 FB_MODE_IS_VESA
234 },
235
236 /* 60 Hz broadcast modes (full resolution versions of modes "1" to "5") */
237 {
238 /* 480if */
239 "480if", 60, 720, 480, 74074, 58, 17, 30, 9, 63, 6,
240 FB_SYNC_BROADCAST, FB_VMODE_INTERLACED
241 }, {
242 /* 480pf */
243 "480pf", 60, 720, 480, 37037, 58, 17, 30, 9, 63, 6,
244 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
245 }, {
246 /* 720pf */
247 "720pf", 60, 1280, 720, 13481, 220, 70, 19, 6, 80, 5,
248 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
249 }, {
250 /* 1080if */
251 "1080if", 60, 1920, 1080, 13481, 148, 44, 36, 4, 88, 5,
252 FB_SYNC_BROADCAST, FB_VMODE_INTERLACED
253 }, {
254 /* 1080pf */
255 "1080pf", 60, 1920, 1080, 6741, 148, 44, 36, 4, 88, 5,
256 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
257 },
258
259 /* 50 Hz broadcast modes (full resolution versions of modes "6" to "10") */
260 {
261 /* 576if */
262 "576if", 50, 720, 576, 74074, 70, 11, 39, 5, 63, 5,
263 FB_SYNC_BROADCAST, FB_VMODE_INTERLACED
264 }, {
265 /* 576pf */
266 "576pf", 50, 720, 576, 37037, 70, 11, 39, 5, 63, 5,
267 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
268 }, {
269 /* 720pf */
270 "720pf", 50, 1280, 720, 13468, 220, 400, 19, 6, 80, 5,
271 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
272 }, {
273 /* 1080if */
274 "1080f", 50, 1920, 1080, 13468, 148, 484, 36, 4, 88, 5,
275 FB_SYNC_BROADCAST, FB_VMODE_INTERLACED
276 }, {
277 /* 1080pf */
278 "1080pf", 50, 1920, 1080, 6734, 148, 484, 36, 4, 88, 5,
279 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
280 }
281};
282
283
284#define HEAD_A
285#define HEAD_B
286
287#define X_OFF(i) (ps3fb_res[i].xoff) /* left/right margin (pixel) */
288#define Y_OFF(i) (ps3fb_res[i].yoff) /* top/bottom margin (pixel) */
289#define WIDTH(i) (ps3fb_res[i].xres) /* width of FB */
290#define HEIGHT(i) (ps3fb_res[i].yres) /* height of FB */
291#define BPP 4 /* number of bytes per pixel */
292#define VP_OFF(i) (WIDTH(i) * Y_OFF(i) * BPP + X_OFF(i) * BPP)
293#define FB_OFF(i) (GPU_OFFSET - VP_OFF(i) % GPU_OFFSET)
294
Geert Uytterhoevenbd685ac2007-05-02 14:48:34 +0200295static int ps3fb_mode;
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +1000296module_param(ps3fb_mode, int, 0);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800297
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +1000298static char *mode_option __devinitdata;
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800299
Geert Uytterhoevenc95344a2007-10-16 01:29:25 -0700300static int ps3fb_get_res_table(u32 xres, u32 yres, int mode)
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800301{
302 int full_mode;
303 unsigned int i;
304 u32 x, y, f;
305
Geert Uytterhoevenc95344a2007-10-16 01:29:25 -0700306 full_mode = (mode & PS3FB_FULL_MODE_BIT) ? PS3FB_RES_FULL : 0;
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800307 for (i = 0;; i++) {
308 x = ps3fb_res[i].xres;
309 y = ps3fb_res[i].yres;
310 f = ps3fb_res[i].type;
311
312 if (!x) {
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -0700313 pr_debug("ERROR: ps3fb_get_res_table()\n");
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800314 return -1;
315 }
316
317 if (full_mode == PS3FB_RES_FULL && f != PS3FB_RES_FULL)
318 continue;
319
320 if (x == xres && (yres == 0 || y == yres))
321 break;
322
323 x = x - 2 * ps3fb_res[i].xoff;
324 y = y - 2 * ps3fb_res[i].yoff;
325 if (x == xres && (yres == 0 || y == yres))
326 break;
327 }
328 return i;
329}
330
331static unsigned int ps3fb_find_mode(const struct fb_var_screeninfo *var,
332 u32 *line_length)
333{
334 unsigned int i, mode;
335
336 for (i = 0; i < ARRAY_SIZE(ps3fb_modedb); i++)
337 if (var->xres == ps3fb_modedb[i].xres &&
338 var->yres == ps3fb_modedb[i].yres &&
339 var->pixclock == ps3fb_modedb[i].pixclock &&
340 var->hsync_len == ps3fb_modedb[i].hsync_len &&
341 var->vsync_len == ps3fb_modedb[i].vsync_len &&
342 var->left_margin == ps3fb_modedb[i].left_margin &&
343 var->right_margin == ps3fb_modedb[i].right_margin &&
344 var->upper_margin == ps3fb_modedb[i].upper_margin &&
345 var->lower_margin == ps3fb_modedb[i].lower_margin &&
346 var->sync == ps3fb_modedb[i].sync &&
347 (var->vmode & FB_VMODE_MASK) == ps3fb_modedb[i].vmode) {
348 /* Cropped broadcast modes use the full line_length */
349 *line_length =
350 ps3fb_modedb[i < 10 ? i + 13 : i].xres * 4;
351 /* Full broadcast modes have the full mode bit set */
352 mode = i > 12 ? (i - 12) | PS3FB_FULL_MODE_BIT : i + 1;
353
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -0700354 pr_debug("ps3fb_find_mode: mode %u\n", mode);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800355 return mode;
356 }
357
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -0700358 pr_debug("ps3fb_find_mode: mode not found\n");
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800359 return 0;
360
361}
362
363static const struct fb_videomode *ps3fb_default_mode(void)
364{
365 u32 mode = ps3fb_mode & PS3AV_MODE_MASK;
366 u32 flags;
367
368 if (mode < 1 || mode > 13)
369 return NULL;
370
371 flags = ps3fb_mode & ~PS3AV_MODE_MASK;
372
373 if (mode <= 10 && flags & PS3FB_FULL_MODE_BIT) {
374 /* Full broadcast mode */
375 return &ps3fb_modedb[mode + 12];
376 }
377
378 return &ps3fb_modedb[mode - 1];
379}
380
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -0700381static int ps3fb_sync(struct fb_info *info, u32 frame)
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800382{
383 int i, status;
384 u32 xres, yres;
385 u64 fb_ioif, offset;
386
387 i = ps3fb.res_index;
388 xres = ps3fb_res[i].xres;
389 yres = ps3fb_res[i].yres;
390
391 if (frame > ps3fb.num_frames - 1) {
Geert Uytterhoeven0138bd82007-10-16 01:29:46 -0700392 dev_dbg(info->device, "%s: invalid frame number (%u)\n",
393 __func__, frame);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800394 return -EINVAL;
395 }
396 offset = xres * yres * BPP * frame;
397
398 fb_ioif = GPU_IOIF + FB_OFF(i) + offset;
399 status = lv1_gpu_context_attribute(ps3fb.context_handle,
400 L1GPU_CONTEXT_ATTRIBUTE_FB_BLIT,
401 offset, fb_ioif,
402 L1GPU_FB_BLIT_WAIT_FOR_COMPLETION |
403 (xres << 16) | yres,
404 xres * BPP); /* line_length */
405 if (status)
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -0700406 dev_err(info->device,
407 "%s: lv1_gpu_context_attribute FB_BLIT failed: %d\n",
408 __func__, status);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800409#ifdef HEAD_A
410 status = lv1_gpu_context_attribute(ps3fb.context_handle,
411 L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_FLIP,
412 0, offset, 0, 0);
413 if (status)
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -0700414 dev_err(info->device,
415 "%s: lv1_gpu_context_attribute FLIP failed: %d\n",
416 __func__, status);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800417#endif
418#ifdef HEAD_B
419 status = lv1_gpu_context_attribute(ps3fb.context_handle,
420 L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_FLIP,
421 1, offset, 0, 0);
422 if (status)
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -0700423 dev_err(info->device,
424 "%s: lv1_gpu_context_attribute FLIP failed: %d\n",
425 __func__, status);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800426#endif
427 return 0;
428}
429
430
431static int ps3fb_open(struct fb_info *info, int user)
432{
433 atomic_inc(&ps3fb.f_count);
434 return 0;
435}
436
437static int ps3fb_release(struct fb_info *info, int user)
438{
439 if (atomic_dec_and_test(&ps3fb.f_count)) {
440 if (atomic_read(&ps3fb.ext_flip)) {
441 atomic_set(&ps3fb.ext_flip, 0);
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -0700442 ps3fb_sync(info, 0); /* single buffer */
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800443 }
444 }
445 return 0;
446}
447
448 /*
449 * Setting the video mode has been split into two parts.
450 * First part, xxxfb_check_var, must not write anything
451 * to hardware, it should only verify and adjust var.
452 * This means it doesn't alter par but it does use hardware
453 * data from it to check this var.
454 */
455
456static int ps3fb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
457{
458 u32 line_length;
459 int mode;
460 int i;
461
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -0700462 dev_dbg(info->device, "var->xres:%u info->var.xres:%u\n", var->xres,
463 info->var.xres);
464 dev_dbg(info->device, "var->yres:%u info->var.yres:%u\n", var->yres,
465 info->var.yres);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800466
467 /* FIXME For now we do exact matches only */
468 mode = ps3fb_find_mode(var, &line_length);
469 if (!mode)
470 return -EINVAL;
471
472 /*
473 * FB_VMODE_CONUPDATE and FB_VMODE_SMOOTH_XPAN are equal!
474 * as FB_VMODE_SMOOTH_XPAN is only used internally
475 */
476
477 if (var->vmode & FB_VMODE_CONUPDATE) {
478 var->vmode |= FB_VMODE_YWRAP;
479 var->xoffset = info->var.xoffset;
480 var->yoffset = info->var.yoffset;
481 }
482
483 /* Virtual screen and panning are not supported */
484 if (var->xres_virtual > var->xres || var->yres_virtual > var->yres ||
485 var->xoffset || var->yoffset) {
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -0700486 dev_dbg(info->device,
487 "Virtual screen and panning are not supported\n");
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800488 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) {
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -0700502 dev_dbg(info->device, "We support ARGB8888 only\n");
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800503 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) {
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -0700522 dev_dbg(info->device, "Rotation is not supported\n");
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800523 return -EINVAL;
524 }
525
526 /* Memory limit */
Geert Uytterhoevenc95344a2007-10-16 01:29:25 -0700527 i = ps3fb_get_res_table(var->xres, var->yres, mode);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800528 if (ps3fb_res[i].xres*ps3fb_res[i].yres*BPP > ps3fb_videomemory.size) {
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -0700529 dev_dbg(info->device, "Not enough memory\n");
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800530 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;
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800548
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -0700549 dev_dbg(info->device, "xres:%d xv:%d yres:%d yv:%d clock:%d\n",
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800550 info->var.xres, info->var.xres_virtual,
551 info->var.yres, info->var.yres_virtual, info->var.pixclock);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800552
553 mode = ps3fb_find_mode(&info->var, &info->fix.line_length);
554 if (!mode)
555 return -EINVAL;
556
Geert Uytterhoevenc95344a2007-10-16 01:29:25 -0700557 i = ps3fb_get_res_table(info->var.xres, info->var.yres, mode);
558 ps3fb.res_index = i;
559
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800560 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
Geert Uytterhoevence4c3712007-10-16 01:29:44 -0700571 if (ps3av_set_video_mode(ps3fb_mode))
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800572 return -EINVAL;
573
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800574 return 0;
575}
576
577 /*
578 * Set a single color register. The values supplied are already
579 * rounded down to the hardware's capabilities (according to the
580 * entries in the var structure). Return != 0 for invalid regno.
581 */
582
583static int ps3fb_setcolreg(unsigned int regno, unsigned int red,
584 unsigned int green, unsigned int blue,
585 unsigned int transp, struct fb_info *info)
586{
587 if (regno >= 16)
588 return 1;
589
590 red >>= 8;
591 green >>= 8;
592 blue >>= 8;
593 transp >>= 8;
594
595 ((u32 *)info->pseudo_palette)[regno] = transp << 24 | red << 16 |
596 green << 8 | blue;
597 return 0;
598}
599
600 /*
601 * As we have a virtual frame buffer, we need our own mmap function
602 */
603
604static int ps3fb_mmap(struct fb_info *info, struct vm_area_struct *vma)
605{
606 unsigned long size, offset;
607 int i;
608
Geert Uytterhoevenc95344a2007-10-16 01:29:25 -0700609 i = ps3fb_get_res_table(info->var.xres, info->var.yres, ps3fb_mode);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800610 if (i == -1)
611 return -EINVAL;
612
613 size = vma->vm_end - vma->vm_start;
614 offset = vma->vm_pgoff << PAGE_SHIFT;
615 if (offset + size > info->fix.smem_len)
616 return -EINVAL;
617
618 offset += info->fix.smem_start + FB_OFF(i) + VP_OFF(i);
619 if (remap_pfn_range(vma, vma->vm_start, offset >> PAGE_SHIFT,
620 size, vma->vm_page_prot))
621 return -EAGAIN;
622
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -0700623 dev_dbg(info->device, "ps3fb: mmap framebuffer P(%lx)->V(%lx)\n",
624 offset, vma->vm_start);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800625 return 0;
626}
627
628 /*
629 * Blank the display
630 */
631
632static int ps3fb_blank(int blank, struct fb_info *info)
633{
634 int retval;
635
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -0700636 dev_dbg(info->device, "%s: blank:%d\n", __func__, blank);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800637 switch (blank) {
638 case FB_BLANK_POWERDOWN:
639 case FB_BLANK_HSYNC_SUSPEND:
640 case FB_BLANK_VSYNC_SUSPEND:
641 case FB_BLANK_NORMAL:
642 retval = ps3av_video_mute(1); /* mute on */
643 if (!retval)
644 ps3fb.is_blanked = 1;
645 break;
646
647 default: /* unblank */
648 retval = ps3av_video_mute(0); /* mute off */
649 if (!retval)
650 ps3fb.is_blanked = 0;
651 break;
652 }
653 return retval;
654}
655
656static int ps3fb_get_vblank(struct fb_vblank *vblank)
657{
658 memset(vblank, 0, sizeof(&vblank));
659 vblank->flags = FB_VBLANK_HAVE_VSYNC;
660 return 0;
661}
662
663int ps3fb_wait_for_vsync(u32 crtc)
664{
665 int ret;
666 u64 count;
667
668 count = ps3fb.vblank_count;
669 ret = wait_event_interruptible_timeout(ps3fb.wait_vsync,
670 count != ps3fb.vblank_count,
671 HZ / 10);
672 if (!ret)
673 return -ETIMEDOUT;
674
675 return 0;
676}
677
678EXPORT_SYMBOL_GPL(ps3fb_wait_for_vsync);
679
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +1000680void ps3fb_flip_ctl(int on, void *data)
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800681{
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +1000682 struct ps3fb_priv *priv = data;
Geert Uytterhoeveneca28742007-05-02 14:48:32 +0200683 if (on)
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +1000684 atomic_dec_if_positive(&priv->ext_flip);
Geert Uytterhoeveneca28742007-05-02 14:48:32 +0200685 else
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +1000686 atomic_inc(&priv->ext_flip);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800687}
688
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800689
690 /*
691 * ioctl
692 */
693
694static int ps3fb_ioctl(struct fb_info *info, unsigned int cmd,
695 unsigned long arg)
696{
697 void __user *argp = (void __user *)arg;
698 u32 val, old_mode;
699 int retval = -EFAULT;
700
701 switch (cmd) {
702 case FBIOGET_VBLANK:
703 {
704 struct fb_vblank vblank;
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -0700705 dev_dbg(info->device, "FBIOGET_VBLANK:\n");
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800706 retval = ps3fb_get_vblank(&vblank);
707 if (retval)
708 break;
709
710 if (copy_to_user(argp, &vblank, sizeof(vblank)))
711 retval = -EFAULT;
712 break;
713 }
714
715 case FBIO_WAITFORVSYNC:
716 {
717 u32 crt;
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -0700718 dev_dbg(info->device, "FBIO_WAITFORVSYNC:\n");
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800719 if (get_user(crt, (u32 __user *) arg))
720 break;
721
722 retval = ps3fb_wait_for_vsync(crt);
723 break;
724 }
725
726 case PS3FB_IOCTL_SETMODE:
727 {
728 const struct fb_videomode *mode;
729 struct fb_var_screeninfo var;
730
731 if (copy_from_user(&val, argp, sizeof(val)))
732 break;
733
Masashi Kimoto64072902007-05-02 14:48:36 +0200734 if (!(val & PS3AV_MODE_MASK)) {
Geert Uytterhoevence4c3712007-10-16 01:29:44 -0700735 u32 id = ps3av_get_auto_mode();
Masashi Kimoto64072902007-05-02 14:48:36 +0200736 if (id > 0)
737 val = (val & ~PS3AV_MODE_MASK) | id;
738 }
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -0700739 dev_dbg(info->device, "PS3FB_IOCTL_SETMODE:%x\n", val);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800740 retval = -EINVAL;
741 old_mode = ps3fb_mode;
742 ps3fb_mode = val;
743 mode = ps3fb_default_mode();
744 if (mode) {
745 var = info->var;
746 fb_videomode_to_var(&var, mode);
747 acquire_console_sem();
748 info->flags |= FBINFO_MISC_USEREVENT;
749 /* Force, in case only special bits changed */
750 var.activate |= FB_ACTIVATE_FORCE;
751 retval = fb_set_var(info, &var);
752 info->flags &= ~FBINFO_MISC_USEREVENT;
753 release_console_sem();
754 }
755 if (retval)
756 ps3fb_mode = old_mode;
757 break;
758 }
759
760 case PS3FB_IOCTL_GETMODE:
761 val = ps3av_get_mode();
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -0700762 dev_dbg(info->device, "PS3FB_IOCTL_GETMODE:%x\n", val);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800763 if (!copy_to_user(argp, &val, sizeof(val)))
764 retval = 0;
765 break;
766
767 case PS3FB_IOCTL_SCREENINFO:
768 {
769 struct ps3fb_ioctl_res res;
770 int i = ps3fb.res_index;
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -0700771 dev_dbg(info->device, "PS3FB_IOCTL_SCREENINFO:\n");
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800772 res.xres = ps3fb_res[i].xres;
773 res.yres = ps3fb_res[i].yres;
774 res.xoff = ps3fb_res[i].xoff;
775 res.yoff = ps3fb_res[i].yoff;
776 res.num_frames = ps3fb.num_frames;
777 if (!copy_to_user(argp, &res, sizeof(res)))
778 retval = 0;
779 break;
780 }
781
782 case PS3FB_IOCTL_ON:
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -0700783 dev_dbg(info->device, "PS3FB_IOCTL_ON:\n");
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800784 atomic_inc(&ps3fb.ext_flip);
785 retval = 0;
786 break;
787
788 case PS3FB_IOCTL_OFF:
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -0700789 dev_dbg(info->device, "PS3FB_IOCTL_OFF:\n");
Geert Uytterhoeveneca28742007-05-02 14:48:32 +0200790 atomic_dec_if_positive(&ps3fb.ext_flip);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800791 retval = 0;
792 break;
793
794 case PS3FB_IOCTL_FSEL:
795 if (copy_from_user(&val, argp, sizeof(val)))
796 break;
797
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -0700798 dev_dbg(info->device, "PS3FB_IOCTL_FSEL:%d\n", val);
799 retval = ps3fb_sync(info, val);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800800 break;
801
802 default:
803 retval = -ENOIOCTLCMD;
804 break;
805 }
806 return retval;
807}
808
809static int ps3fbd(void *arg)
810{
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -0700811 struct fb_info *info = arg;
812
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700813 set_freezable();
Geert Uytterhoeven1c0c8462007-05-02 14:48:31 +0200814 while (!kthread_should_stop()) {
815 try_to_freeze();
816 set_current_state(TASK_INTERRUPTIBLE);
817 if (ps3fb.is_kicked) {
818 ps3fb.is_kicked = 0;
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -0700819 ps3fb_sync(info, 0); /* single buffer */
Geert Uytterhoeven1c0c8462007-05-02 14:48:31 +0200820 }
821 schedule();
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800822 }
823 return 0;
824}
825
826static irqreturn_t ps3fb_vsync_interrupt(int irq, void *ptr)
827{
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -0700828 struct device *dev = ptr;
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800829 u64 v1;
830 int status;
831 struct display_head *head = &ps3fb.dinfo->display_head[1];
832
833 status = lv1_gpu_context_intr(ps3fb.context_handle, &v1);
834 if (status) {
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -0700835 dev_err(dev, "%s: lv1_gpu_context_intr failed: %d\n", __func__,
836 status);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800837 return IRQ_NONE;
838 }
839
840 if (v1 & (1 << GPU_INTR_STATUS_VSYNC_1)) {
841 /* VSYNC */
842 ps3fb.vblank_count = head->vblank_count;
Geert Uytterhoeven1c0c8462007-05-02 14:48:31 +0200843 if (ps3fb.task && !ps3fb.is_blanked &&
844 !atomic_read(&ps3fb.ext_flip)) {
845 ps3fb.is_kicked = 1;
846 wake_up_process(ps3fb.task);
847 }
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800848 wake_up_interruptible(&ps3fb.wait_vsync);
849 }
850
851 return IRQ_HANDLED;
852}
853
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800854
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +1000855static int ps3fb_vsync_settings(struct gpu_driver_info *dinfo,
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -0700856 struct device *dev)
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800857{
858 int error;
859
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -0700860 dev_dbg(dev, "version_driver:%x\n", dinfo->version_driver);
861 dev_dbg(dev, "irq outlet:%x\n", dinfo->irq.irq_outlet);
862 dev_dbg(dev,
863 "version_gpu: %x memory_size: %x ch: %x core_freq: %d "
864 "mem_freq:%d\n",
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800865 dinfo->version_gpu, dinfo->memory_size, dinfo->hardware_channel,
866 dinfo->nvcore_frequency/1000000, dinfo->memory_frequency/1000000);
867
868 if (dinfo->version_driver != GPU_DRIVER_INFO_VERSION) {
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -0700869 dev_err(dev, "%s: version_driver err:%x\n", __func__,
870 dinfo->version_driver);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800871 return -EINVAL;
872 }
873
Geoff Levanddc4f60c2007-05-01 07:01:01 +1000874 error = ps3_irq_plug_setup(PS3_BINDING_CPU_ANY, dinfo->irq.irq_outlet,
875 &ps3fb.irq_no);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800876 if (error) {
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -0700877 dev_err(dev, "%s: ps3_alloc_irq failed %d\n", __func__, error);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800878 return error;
879 }
880
881 error = request_irq(ps3fb.irq_no, ps3fb_vsync_interrupt, IRQF_DISABLED,
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +1000882 DEVICE_NAME, dev);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800883 if (error) {
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -0700884 dev_err(dev, "%s: request_irq failed %d\n", __func__, error);
Geoff Levanddc4f60c2007-05-01 07:01:01 +1000885 ps3_irq_plug_destroy(ps3fb.irq_no);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800886 return error;
887 }
888
889 dinfo->irq.mask = (1 << GPU_INTR_STATUS_VSYNC_1) |
890 (1 << GPU_INTR_STATUS_FLIP_1);
891 return 0;
892}
893
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -0700894static int ps3fb_xdr_settings(u64 xdr_lpar, struct device *dev)
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800895{
896 int status;
897
898 status = lv1_gpu_context_iomap(ps3fb.context_handle, GPU_IOIF,
899 xdr_lpar, ps3fb_videomemory.size, 0);
900 if (status) {
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -0700901 dev_err(dev, "%s: lv1_gpu_context_iomap failed: %d\n",
902 __func__, status);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800903 return -ENXIO;
904 }
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -0700905 dev_dbg(dev,
906 "video:%p xdr_ea:%p ioif:%lx lpar:%lx phys:%lx size:%lx\n",
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800907 ps3fb_videomemory.address, ps3fb.xdr_ea, GPU_IOIF, xdr_lpar,
908 virt_to_abs(ps3fb.xdr_ea), ps3fb_videomemory.size);
909
910 status = lv1_gpu_context_attribute(ps3fb.context_handle,
911 L1GPU_CONTEXT_ATTRIBUTE_FB_SETUP,
912 xdr_lpar, ps3fb_videomemory.size,
913 GPU_IOIF, 0);
914 if (status) {
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -0700915 dev_err(dev,
916 "%s: lv1_gpu_context_attribute FB_SETUP failed: %d\n",
917 __func__, status);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800918 return -ENXIO;
919 }
920 return 0;
921}
922
923static struct fb_ops ps3fb_ops = {
924 .fb_open = ps3fb_open,
925 .fb_release = ps3fb_release,
Geert Uytterhoeven92c45792007-05-23 13:57:50 -0700926 .fb_read = fb_sys_read,
927 .fb_write = fb_sys_write,
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800928 .fb_check_var = ps3fb_check_var,
929 .fb_set_par = ps3fb_set_par,
930 .fb_setcolreg = ps3fb_setcolreg,
Geert Uytterhoeven92c45792007-05-23 13:57:50 -0700931 .fb_fillrect = sys_fillrect,
932 .fb_copyarea = sys_copyarea,
933 .fb_imageblit = sys_imageblit,
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800934 .fb_mmap = ps3fb_mmap,
935 .fb_blank = ps3fb_blank,
936 .fb_ioctl = ps3fb_ioctl,
937 .fb_compat_ioctl = ps3fb_ioctl
938};
939
940static struct fb_fix_screeninfo ps3fb_fix __initdata = {
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +1000941 .id = DEVICE_NAME,
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800942 .type = FB_TYPE_PACKED_PIXELS,
943 .visual = FB_VISUAL_TRUECOLOR,
944 .accel = FB_ACCEL_NONE,
945};
946
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -0700947static int ps3fb_set_sync(struct device *dev)
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +1000948{
949 int status;
950
951#ifdef HEAD_A
952 status = lv1_gpu_context_attribute(0x0,
953 L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_SYNC,
954 0, L1GPU_DISPLAY_SYNC_VSYNC, 0, 0);
955 if (status) {
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -0700956 dev_err(dev,
957 "%s: lv1_gpu_context_attribute DISPLAY_SYNC failed: "
958 "%d\n",
959 __func__, status);
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +1000960 return -1;
961 }
962#endif
963#ifdef HEAD_B
964 status = lv1_gpu_context_attribute(0x0,
965 L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_SYNC,
966 1, L1GPU_DISPLAY_SYNC_VSYNC, 0, 0);
967
968 if (status) {
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -0700969 dev_err(dev,
970 "%s: lv1_gpu_context_attribute DISPLAY_SYNC failed: "
971 "%d\n",
972 __func__, status);
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +1000973 return -1;
974 }
975#endif
976 return 0;
977}
978
979static int __devinit ps3fb_probe(struct ps3_system_bus_device *dev)
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800980{
981 struct fb_info *info;
982 int retval = -ENOMEM;
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +1000983 u32 xres, yres;
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800984 u64 ddr_lpar = 0;
985 u64 lpar_dma_control = 0;
986 u64 lpar_driver_info = 0;
987 u64 lpar_reports = 0;
988 u64 lpar_reports_size = 0;
989 u64 xdr_lpar;
990 int status;
991 unsigned long offset;
Geert Uytterhoeven1c0c8462007-05-02 14:48:31 +0200992 struct task_struct *task;
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800993
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +1000994 status = ps3_open_hv_device(dev);
995 if (status) {
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -0700996 dev_err(&dev->core, "%s: ps3_open_hv_device failed\n",
997 __func__);
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +1000998 goto err;
999 }
1000
1001 if (!ps3fb_mode)
1002 ps3fb_mode = ps3av_get_mode();
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -07001003 dev_dbg(&dev->core, "ps3av_mode:%d\n", ps3fb_mode);
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +10001004
1005 if (ps3fb_mode > 0 &&
1006 !ps3av_video_mode2res(ps3fb_mode, &xres, &yres)) {
Geert Uytterhoevenc95344a2007-10-16 01:29:25 -07001007 ps3fb.res_index = ps3fb_get_res_table(xres, yres, ps3fb_mode);
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -07001008 dev_dbg(&dev->core, "res_index:%d\n", ps3fb.res_index);
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +10001009 } else
1010 ps3fb.res_index = GPU_RES_INDEX;
1011
1012 atomic_set(&ps3fb.f_count, -1); /* fbcon opens ps3fb */
1013 atomic_set(&ps3fb.ext_flip, 0); /* for flip with vsync */
1014 init_waitqueue_head(&ps3fb.wait_vsync);
1015 ps3fb.num_frames = 1;
1016
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -07001017 ps3fb_set_sync(&dev->core);
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +10001018
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001019 /* get gpu context handle */
1020 status = lv1_gpu_memory_allocate(DDR_SIZE, 0, 0, 0, 0,
1021 &ps3fb.memory_handle, &ddr_lpar);
1022 if (status) {
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -07001023 dev_err(&dev->core, "%s: lv1_gpu_memory_allocate failed: %d\n",
1024 __func__, status);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001025 goto err;
1026 }
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -07001027 dev_dbg(&dev->core, "ddr:lpar:0x%lx\n", ddr_lpar);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001028
1029 status = lv1_gpu_context_allocate(ps3fb.memory_handle, 0,
1030 &ps3fb.context_handle,
1031 &lpar_dma_control, &lpar_driver_info,
1032 &lpar_reports, &lpar_reports_size);
1033 if (status) {
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -07001034 dev_err(&dev->core,
1035 "%s: lv1_gpu_context_attribute failed: %d\n", __func__,
1036 status);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001037 goto err_gpu_memory_free;
1038 }
1039
1040 /* vsync interrupt */
1041 ps3fb.dinfo = ioremap(lpar_driver_info, 128 * 1024);
1042 if (!ps3fb.dinfo) {
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -07001043 dev_err(&dev->core, "%s: ioremap failed\n", __func__);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001044 goto err_gpu_context_free;
1045 }
1046
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -07001047 retval = ps3fb_vsync_settings(ps3fb.dinfo, &dev->core);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001048 if (retval)
1049 goto err_iounmap_dinfo;
1050
1051 /* xdr frame buffer */
1052 ps3fb.xdr_ea = ps3fb_videomemory.address;
1053 xdr_lpar = ps3_mm_phys_to_lpar(__pa(ps3fb.xdr_ea));
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -07001054 retval = ps3fb_xdr_settings(xdr_lpar, &dev->core);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001055 if (retval)
1056 goto err_free_irq;
1057
1058 /*
1059 * ps3fb must clear memory to prevent kernel info
1060 * leakage into userspace
1061 */
1062 memset(ps3fb.xdr_ea, 0, ps3fb_videomemory.size);
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +10001063 info = framebuffer_alloc(sizeof(u32) * 16, &dev->core);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001064 if (!info)
1065 goto err_free_irq;
1066
1067 offset = FB_OFF(ps3fb.res_index) + VP_OFF(ps3fb.res_index);
1068 info->screen_base = (char __iomem *)ps3fb.xdr_ea + offset;
1069 info->fbops = &ps3fb_ops;
1070
1071 info->fix = ps3fb_fix;
1072 info->fix.smem_start = virt_to_abs(ps3fb.xdr_ea);
1073 info->fix.smem_len = ps3fb_videomemory.size - offset;
1074 info->pseudo_palette = info->par;
1075 info->par = NULL;
Geert Uytterhoevencfd13af2007-07-21 04:37:51 -07001076 info->flags = FBINFO_DEFAULT | FBINFO_READS_FAST;
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001077
1078 retval = fb_alloc_cmap(&info->cmap, 256, 0);
1079 if (retval < 0)
1080 goto err_framebuffer_release;
1081
1082 if (!fb_find_mode(&info->var, info, mode_option, ps3fb_modedb,
1083 ARRAY_SIZE(ps3fb_modedb), ps3fb_default_mode(), 32)) {
1084 retval = -EINVAL;
1085 goto err_fb_dealloc;
1086 }
1087
1088 fb_videomode_to_modelist(ps3fb_modedb, ARRAY_SIZE(ps3fb_modedb),
1089 &info->modelist);
1090
1091 retval = register_framebuffer(info);
1092 if (retval < 0)
1093 goto err_fb_dealloc;
1094
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +10001095 dev->core.driver_data = info;
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001096
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -07001097 dev_info(info->device, "%s %s, using %lu KiB of video memory\n",
1098 dev_driver_string(info->dev), info->dev->bus_id,
1099 ps3fb_videomemory.size >> 10);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001100
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +10001101 task = kthread_run(ps3fbd, info, DEVICE_NAME);
Geert Uytterhoeven1c0c8462007-05-02 14:48:31 +02001102 if (IS_ERR(task)) {
1103 retval = PTR_ERR(task);
1104 goto err_unregister_framebuffer;
1105 }
1106
1107 ps3fb.task = task;
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +10001108 ps3av_register_flip_ctl(ps3fb_flip_ctl, &ps3fb);
Geert Uytterhoeven1c0c8462007-05-02 14:48:31 +02001109
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001110 return 0;
1111
Geert Uytterhoeven1c0c8462007-05-02 14:48:31 +02001112err_unregister_framebuffer:
1113 unregister_framebuffer(info);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001114err_fb_dealloc:
1115 fb_dealloc_cmap(&info->cmap);
1116err_framebuffer_release:
1117 framebuffer_release(info);
1118err_free_irq:
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +10001119 free_irq(ps3fb.irq_no, dev);
Geoff Levanddc4f60c2007-05-01 07:01:01 +10001120 ps3_irq_plug_destroy(ps3fb.irq_no);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001121err_iounmap_dinfo:
1122 iounmap((u8 __iomem *)ps3fb.dinfo);
1123err_gpu_context_free:
1124 lv1_gpu_context_free(ps3fb.context_handle);
1125err_gpu_memory_free:
1126 lv1_gpu_memory_free(ps3fb.memory_handle);
1127err:
1128 return retval;
1129}
1130
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +10001131static int ps3fb_shutdown(struct ps3_system_bus_device *dev)
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001132{
1133 int status;
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +10001134 struct fb_info *info = dev->core.driver_data;
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001135
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -07001136 dev_dbg(&dev->core, " -> %s:%d\n", __func__, __LINE__);
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +10001137
1138 ps3fb_flip_ctl(0, &ps3fb); /* flip off */
1139 ps3fb.dinfo->irq.mask = 0;
1140
1141 if (info) {
1142 unregister_framebuffer(info);
1143 fb_dealloc_cmap(&info->cmap);
1144 framebuffer_release(info);
1145 }
1146
1147 ps3av_register_flip_ctl(NULL, NULL);
Geert Uytterhoeven1c0c8462007-05-02 14:48:31 +02001148 if (ps3fb.task) {
1149 struct task_struct *task = ps3fb.task;
1150 ps3fb.task = NULL;
1151 kthread_stop(task);
1152 }
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001153 if (ps3fb.irq_no) {
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +10001154 free_irq(ps3fb.irq_no, dev);
Geoff Levanddc4f60c2007-05-01 07:01:01 +10001155 ps3_irq_plug_destroy(ps3fb.irq_no);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001156 }
1157 iounmap((u8 __iomem *)ps3fb.dinfo);
1158
1159 status = lv1_gpu_context_free(ps3fb.context_handle);
1160 if (status)
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -07001161 dev_dbg(&dev->core, "lv1_gpu_context_free failed: %d\n",
1162 status);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001163
1164 status = lv1_gpu_memory_free(ps3fb.memory_handle);
1165 if (status)
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -07001166 dev_dbg(&dev->core, "lv1_gpu_memory_free failed: %d\n",
1167 status);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001168
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +10001169 ps3_close_hv_device(dev);
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -07001170 dev_dbg(&dev->core, " <- %s:%d\n", __func__, __LINE__);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001171
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001172 return 0;
1173}
1174
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +10001175static struct ps3_system_bus_driver ps3fb_driver = {
1176 .match_id = PS3_MATCH_ID_GRAPHICS,
1177 .core.name = DEVICE_NAME,
1178 .core.owner = THIS_MODULE,
1179 .probe = ps3fb_probe,
1180 .remove = ps3fb_shutdown,
1181 .shutdown = ps3fb_shutdown,
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001182};
1183
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +10001184static int __init ps3fb_setup(void)
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001185{
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +10001186 char *options;
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001187
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +10001188#ifdef MODULE
1189 return 0;
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001190#endif
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001191
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +10001192 if (fb_get_options(DEVICE_NAME, &options))
1193 return -ENXIO;
1194
1195 if (!options || !*options)
1196 return 0;
1197
1198 while (1) {
1199 char *this_opt = strsep(&options, ",");
1200
1201 if (!this_opt)
1202 break;
1203 if (!*this_opt)
1204 continue;
1205 if (!strncmp(this_opt, "mode:", 5))
1206 ps3fb_mode = simple_strtoul(this_opt + 5, NULL, 0);
1207 else
1208 mode_option = this_opt;
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001209 }
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001210 return 0;
1211}
1212
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001213static int __init ps3fb_init(void)
1214{
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +10001215 if (!ps3fb_videomemory.address || ps3fb_setup())
1216 return -ENXIO;
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001217
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +10001218 return ps3_system_bus_driver_register(&ps3fb_driver);
1219}
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001220
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +10001221static void __exit ps3fb_exit(void)
1222{
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -07001223 pr_debug(" -> %s:%d\n", __func__, __LINE__);
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +10001224 ps3_system_bus_driver_unregister(&ps3fb_driver);
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -07001225 pr_debug(" <- %s:%d\n", __func__, __LINE__);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001226}
1227
1228module_init(ps3fb_init);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001229module_exit(ps3fb_exit);
1230
1231MODULE_LICENSE("GPL");
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +10001232MODULE_DESCRIPTION("PS3 GPU Frame Buffer Driver");
1233MODULE_AUTHOR("Sony Computer Entertainment Inc.");
1234MODULE_ALIAS(PS3_MODULE_ALIAS_GRAPHICS);