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