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