blob: 8bbe479f73f78a15eb49347cdb0ce317bf05226a [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>
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -080025#include <linux/interrupt.h>
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -080026#include <linux/console.h>
27#include <linux/ioctl.h>
Geert Uytterhoeven1c0c8462007-05-02 14:48:31 +020028#include <linux/kthread.h>
29#include <linux/freezer.h>
Krzysztof Helt84902b72007-10-16 01:29:04 -070030#include <linux/uaccess.h>
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -080031#include <linux/fb.h>
32#include <linux/init.h>
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -080033
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
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +100040
41#define DEVICE_NAME "ps3fb"
42
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -080043#define L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_SYNC 0x101
44#define L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_FLIP 0x102
45#define L1GPU_CONTEXT_ATTRIBUTE_FB_SETUP 0x600
46#define L1GPU_CONTEXT_ATTRIBUTE_FB_BLIT 0x601
47#define L1GPU_CONTEXT_ATTRIBUTE_FB_BLIT_SYNC 0x602
48
49#define L1GPU_FB_BLIT_WAIT_FOR_COMPLETION (1ULL << 32)
50
51#define L1GPU_DISPLAY_SYNC_HSYNC 1
52#define L1GPU_DISPLAY_SYNC_VSYNC 2
53
54#define DDR_SIZE (0) /* used no ddr */
Geert Uytterhoeven2ce32e12007-10-16 01:29:48 -070055#define GPU_CMD_BUF_SIZE (64 * 1024)
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -080056#define GPU_IOIF (0x0d000000UL)
57
58#define PS3FB_FULL_MODE_BIT 0x80
59
60#define GPU_INTR_STATUS_VSYNC_0 0 /* vsync on head A */
61#define GPU_INTR_STATUS_VSYNC_1 1 /* vsync on head B */
62#define GPU_INTR_STATUS_FLIP_0 3 /* flip head A */
63#define GPU_INTR_STATUS_FLIP_1 4 /* flip head B */
64#define GPU_INTR_STATUS_QUEUE_0 5 /* queue head A */
65#define GPU_INTR_STATUS_QUEUE_1 6 /* queue head B */
66
67#define GPU_DRIVER_INFO_VERSION 0x211
68
69/* gpu internals */
70struct display_head {
71 u64 be_time_stamp;
72 u32 status;
73 u32 offset;
74 u32 res1;
75 u32 res2;
76 u32 field;
77 u32 reserved1;
78
79 u64 res3;
80 u32 raster;
81
82 u64 vblank_count;
83 u32 field_vsync;
84 u32 reserved2;
85};
86
87struct gpu_irq {
88 u32 irq_outlet;
89 u32 status;
90 u32 mask;
91 u32 video_cause;
92 u32 graph_cause;
93 u32 user_cause;
94
95 u32 res1;
96 u64 res2;
97
98 u32 reserved[4];
99};
100
101struct gpu_driver_info {
102 u32 version_driver;
103 u32 version_gpu;
104 u32 memory_size;
105 u32 hardware_channel;
106
107 u32 nvcore_frequency;
108 u32 memory_frequency;
109
110 u32 reserved[1063];
111 struct display_head display_head[8];
112 struct gpu_irq irq;
113};
114
115struct ps3fb_priv {
116 unsigned int irq_no;
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800117
118 u64 context_handle, memory_handle;
119 void *xdr_ea;
Geert Uytterhoeven2ce32e12007-10-16 01:29:48 -0700120 size_t xdr_size;
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800121 struct gpu_driver_info *dinfo;
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800122 u32 res_index;
123
124 u64 vblank_count; /* frame count */
125 wait_queue_head_t wait_vsync;
126
127 u32 num_frames; /* num of frame buffers */
128 atomic_t ext_flip; /* on/off flip with vsync */
129 atomic_t f_count; /* fb_open count */
130 int is_blanked;
Geert Uytterhoeven1c0c8462007-05-02 14:48:31 +0200131 int is_kicked;
132 struct task_struct *task;
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800133};
134static struct ps3fb_priv ps3fb;
135
136struct ps3fb_res_table {
137 u32 xres;
138 u32 yres;
139 u32 xoff;
140 u32 yoff;
141 u32 type;
142};
143#define PS3FB_RES_FULL 1
144static const struct ps3fb_res_table ps3fb_res[] = {
145 /* res_x,y margin_x,y full */
146 { 720, 480, 72, 48 , 0},
147 { 720, 576, 72, 58 , 0},
148 { 1280, 720, 78, 38 , 0},
149 { 1920, 1080, 116, 58 , 0},
150 /* full mode */
151 { 720, 480, 0, 0 , PS3FB_RES_FULL},
152 { 720, 576, 0, 0 , PS3FB_RES_FULL},
153 { 1280, 720, 0, 0 , PS3FB_RES_FULL},
154 { 1920, 1080, 0, 0 , PS3FB_RES_FULL},
155 /* vesa: normally full mode */
156 { 1280, 768, 0, 0 , 0},
157 { 1280, 1024, 0, 0 , 0},
158 { 1920, 1200, 0, 0 , 0},
159 { 0, 0, 0, 0 , 0} };
160
161/* default resolution */
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +1000162#define GPU_RES_INDEX 0 /* 720 x 480 */
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800163
164static const struct fb_videomode ps3fb_modedb[] = {
165 /* 60 Hz broadcast modes (modes "1" to "5") */
166 {
167 /* 480i */
168 "480i", 60, 576, 384, 74074, 130, 89, 78, 57, 63, 6,
169 FB_SYNC_BROADCAST, FB_VMODE_INTERLACED
170 }, {
171 /* 480p */
172 "480p", 60, 576, 384, 37037, 130, 89, 78, 57, 63, 6,
173 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
174 }, {
175 /* 720p */
176 "720p", 60, 1124, 644, 13481, 298, 148, 57, 44, 80, 5,
177 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
178 }, {
179 /* 1080i */
180 "1080i", 60, 1688, 964, 13481, 264, 160, 94, 62, 88, 5,
181 FB_SYNC_BROADCAST, FB_VMODE_INTERLACED
182 }, {
183 /* 1080p */
184 "1080p", 60, 1688, 964, 6741, 264, 160, 94, 62, 88, 5,
185 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
186 },
187
188 /* 50 Hz broadcast modes (modes "6" to "10") */
189 {
190 /* 576i */
191 "576i", 50, 576, 460, 74074, 142, 83, 97, 63, 63, 5,
192 FB_SYNC_BROADCAST, FB_VMODE_INTERLACED
193 }, {
194 /* 576p */
195 "576p", 50, 576, 460, 37037, 142, 83, 97, 63, 63, 5,
196 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
197 }, {
198 /* 720p */
199 "720p", 50, 1124, 644, 13468, 298, 478, 57, 44, 80, 5,
200 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
201 }, {
202 /* 1080 */
203 "1080i", 50, 1688, 964, 13468, 264, 600, 94, 62, 88, 5,
204 FB_SYNC_BROADCAST, FB_VMODE_INTERLACED
205 }, {
206 /* 1080p */
207 "1080p", 50, 1688, 964, 6734, 264, 600, 94, 62, 88, 5,
208 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
209 },
210
211 /* VESA modes (modes "11" to "13") */
212 {
213 /* WXGA */
214 "wxga", 60, 1280, 768, 12924, 160, 24, 29, 3, 136, 6,
215 0, FB_VMODE_NONINTERLACED,
216 FB_MODE_IS_VESA
217 }, {
218 /* SXGA */
219 "sxga", 60, 1280, 1024, 9259, 248, 48, 38, 1, 112, 3,
220 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED,
221 FB_MODE_IS_VESA
222 }, {
223 /* WUXGA */
224 "wuxga", 60, 1920, 1200, 6494, 80, 48, 26, 3, 32, 6,
225 FB_SYNC_HOR_HIGH_ACT, FB_VMODE_NONINTERLACED,
226 FB_MODE_IS_VESA
227 },
228
229 /* 60 Hz broadcast modes (full resolution versions of modes "1" to "5") */
230 {
231 /* 480if */
232 "480if", 60, 720, 480, 74074, 58, 17, 30, 9, 63, 6,
233 FB_SYNC_BROADCAST, FB_VMODE_INTERLACED
234 }, {
235 /* 480pf */
236 "480pf", 60, 720, 480, 37037, 58, 17, 30, 9, 63, 6,
237 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
238 }, {
239 /* 720pf */
240 "720pf", 60, 1280, 720, 13481, 220, 70, 19, 6, 80, 5,
241 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
242 }, {
243 /* 1080if */
244 "1080if", 60, 1920, 1080, 13481, 148, 44, 36, 4, 88, 5,
245 FB_SYNC_BROADCAST, FB_VMODE_INTERLACED
246 }, {
247 /* 1080pf */
248 "1080pf", 60, 1920, 1080, 6741, 148, 44, 36, 4, 88, 5,
249 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
250 },
251
252 /* 50 Hz broadcast modes (full resolution versions of modes "6" to "10") */
253 {
254 /* 576if */
255 "576if", 50, 720, 576, 74074, 70, 11, 39, 5, 63, 5,
256 FB_SYNC_BROADCAST, FB_VMODE_INTERLACED
257 }, {
258 /* 576pf */
259 "576pf", 50, 720, 576, 37037, 70, 11, 39, 5, 63, 5,
260 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
261 }, {
262 /* 720pf */
263 "720pf", 50, 1280, 720, 13468, 220, 400, 19, 6, 80, 5,
264 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
265 }, {
266 /* 1080if */
267 "1080f", 50, 1920, 1080, 13468, 148, 484, 36, 4, 88, 5,
268 FB_SYNC_BROADCAST, FB_VMODE_INTERLACED
269 }, {
270 /* 1080pf */
271 "1080pf", 50, 1920, 1080, 6734, 148, 484, 36, 4, 88, 5,
272 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
273 }
274};
275
276
277#define HEAD_A
278#define HEAD_B
279
280#define X_OFF(i) (ps3fb_res[i].xoff) /* left/right margin (pixel) */
281#define Y_OFF(i) (ps3fb_res[i].yoff) /* top/bottom margin (pixel) */
282#define WIDTH(i) (ps3fb_res[i].xres) /* width of FB */
283#define HEIGHT(i) (ps3fb_res[i].yres) /* height of FB */
Geert Uytterhoeven2ce32e12007-10-16 01:29:48 -0700284#define BPP 4 /* number of bytes per pixel */
285
286/* Start of the virtual frame buffer (relative to fullscreen ) */
287#define VP_OFF(i) ((WIDTH(i) * Y_OFF(i) + X_OFF(i)) * BPP)
288
289/*
290 * Start of the virtual frame buffer (relative to start of video memory)
291 * This is PAGE_SIZE aligned for easier mmap()
292 */
293#define VFB_OFF(i) PAGE_ALIGN(VP_OFF(i))
294
295/* Start of the fullscreen frame buffer (relative to start of video memory) */
296#define FB_OFF(i) (-VP_OFF(i) & ~PAGE_MASK)
297
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800298
Geert Uytterhoevenbd685ac2007-05-02 14:48:34 +0200299static int ps3fb_mode;
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +1000300module_param(ps3fb_mode, int, 0);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800301
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +1000302static char *mode_option __devinitdata;
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800303
Geert Uytterhoevenc95344a2007-10-16 01:29:25 -0700304static int ps3fb_get_res_table(u32 xres, u32 yres, int mode)
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800305{
306 int full_mode;
307 unsigned int i;
308 u32 x, y, f;
309
Geert Uytterhoevenc95344a2007-10-16 01:29:25 -0700310 full_mode = (mode & PS3FB_FULL_MODE_BIT) ? PS3FB_RES_FULL : 0;
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800311 for (i = 0;; i++) {
312 x = ps3fb_res[i].xres;
313 y = ps3fb_res[i].yres;
314 f = ps3fb_res[i].type;
315
316 if (!x) {
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -0700317 pr_debug("ERROR: ps3fb_get_res_table()\n");
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800318 return -1;
319 }
320
321 if (full_mode == PS3FB_RES_FULL && f != PS3FB_RES_FULL)
322 continue;
323
324 if (x == xres && (yres == 0 || y == yres))
325 break;
326
327 x = x - 2 * ps3fb_res[i].xoff;
328 y = y - 2 * ps3fb_res[i].yoff;
329 if (x == xres && (yres == 0 || y == yres))
330 break;
331 }
332 return i;
333}
334
335static unsigned int ps3fb_find_mode(const struct fb_var_screeninfo *var,
336 u32 *line_length)
337{
338 unsigned int i, mode;
339
340 for (i = 0; i < ARRAY_SIZE(ps3fb_modedb); i++)
341 if (var->xres == ps3fb_modedb[i].xres &&
342 var->yres == ps3fb_modedb[i].yres &&
343 var->pixclock == ps3fb_modedb[i].pixclock &&
344 var->hsync_len == ps3fb_modedb[i].hsync_len &&
345 var->vsync_len == ps3fb_modedb[i].vsync_len &&
346 var->left_margin == ps3fb_modedb[i].left_margin &&
347 var->right_margin == ps3fb_modedb[i].right_margin &&
348 var->upper_margin == ps3fb_modedb[i].upper_margin &&
349 var->lower_margin == ps3fb_modedb[i].lower_margin &&
350 var->sync == ps3fb_modedb[i].sync &&
351 (var->vmode & FB_VMODE_MASK) == ps3fb_modedb[i].vmode) {
352 /* Cropped broadcast modes use the full line_length */
353 *line_length =
354 ps3fb_modedb[i < 10 ? i + 13 : i].xres * 4;
355 /* Full broadcast modes have the full mode bit set */
356 mode = i > 12 ? (i - 12) | PS3FB_FULL_MODE_BIT : i + 1;
357
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -0700358 pr_debug("ps3fb_find_mode: mode %u\n", mode);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800359 return mode;
360 }
361
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -0700362 pr_debug("ps3fb_find_mode: mode not found\n");
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800363 return 0;
364
365}
366
367static const struct fb_videomode *ps3fb_default_mode(void)
368{
369 u32 mode = ps3fb_mode & PS3AV_MODE_MASK;
370 u32 flags;
371
372 if (mode < 1 || mode > 13)
373 return NULL;
374
375 flags = ps3fb_mode & ~PS3AV_MODE_MASK;
376
377 if (mode <= 10 && flags & PS3FB_FULL_MODE_BIT) {
378 /* Full broadcast mode */
379 return &ps3fb_modedb[mode + 12];
380 }
381
382 return &ps3fb_modedb[mode - 1];
383}
384
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -0700385static int ps3fb_sync(struct fb_info *info, u32 frame)
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800386{
387 int i, status;
388 u32 xres, yres;
389 u64 fb_ioif, offset;
390
391 i = ps3fb.res_index;
392 xres = ps3fb_res[i].xres;
393 yres = ps3fb_res[i].yres;
394
395 if (frame > ps3fb.num_frames - 1) {
Geert Uytterhoeven0138bd82007-10-16 01:29:46 -0700396 dev_dbg(info->device, "%s: invalid frame number (%u)\n",
397 __func__, frame);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800398 return -EINVAL;
399 }
400 offset = xres * yres * BPP * frame;
401
402 fb_ioif = GPU_IOIF + FB_OFF(i) + offset;
403 status = lv1_gpu_context_attribute(ps3fb.context_handle,
404 L1GPU_CONTEXT_ATTRIBUTE_FB_BLIT,
405 offset, fb_ioif,
406 L1GPU_FB_BLIT_WAIT_FOR_COMPLETION |
407 (xres << 16) | yres,
408 xres * BPP); /* line_length */
409 if (status)
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -0700410 dev_err(info->device,
411 "%s: lv1_gpu_context_attribute FB_BLIT failed: %d\n",
412 __func__, status);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800413#ifdef HEAD_A
414 status = lv1_gpu_context_attribute(ps3fb.context_handle,
415 L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_FLIP,
416 0, offset, 0, 0);
417 if (status)
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -0700418 dev_err(info->device,
419 "%s: lv1_gpu_context_attribute FLIP failed: %d\n",
420 __func__, status);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800421#endif
422#ifdef HEAD_B
423 status = lv1_gpu_context_attribute(ps3fb.context_handle,
424 L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_FLIP,
425 1, offset, 0, 0);
426 if (status)
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -0700427 dev_err(info->device,
428 "%s: lv1_gpu_context_attribute FLIP failed: %d\n",
429 __func__, status);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800430#endif
431 return 0;
432}
433
434
435static int ps3fb_open(struct fb_info *info, int user)
436{
437 atomic_inc(&ps3fb.f_count);
438 return 0;
439}
440
441static int ps3fb_release(struct fb_info *info, int user)
442{
443 if (atomic_dec_and_test(&ps3fb.f_count)) {
444 if (atomic_read(&ps3fb.ext_flip)) {
445 atomic_set(&ps3fb.ext_flip, 0);
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -0700446 ps3fb_sync(info, 0); /* single buffer */
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800447 }
448 }
449 return 0;
450}
451
452 /*
453 * Setting the video mode has been split into two parts.
454 * First part, xxxfb_check_var, must not write anything
455 * to hardware, it should only verify and adjust var.
456 * This means it doesn't alter par but it does use hardware
457 * data from it to check this var.
458 */
459
460static int ps3fb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
461{
462 u32 line_length;
463 int mode;
464 int i;
465
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -0700466 dev_dbg(info->device, "var->xres:%u info->var.xres:%u\n", var->xres,
467 info->var.xres);
468 dev_dbg(info->device, "var->yres:%u info->var.yres:%u\n", var->yres,
469 info->var.yres);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800470
471 /* FIXME For now we do exact matches only */
472 mode = ps3fb_find_mode(var, &line_length);
473 if (!mode)
474 return -EINVAL;
475
476 /*
477 * FB_VMODE_CONUPDATE and FB_VMODE_SMOOTH_XPAN are equal!
478 * as FB_VMODE_SMOOTH_XPAN is only used internally
479 */
480
481 if (var->vmode & FB_VMODE_CONUPDATE) {
482 var->vmode |= FB_VMODE_YWRAP;
483 var->xoffset = info->var.xoffset;
484 var->yoffset = info->var.yoffset;
485 }
486
487 /* Virtual screen and panning are not supported */
488 if (var->xres_virtual > var->xres || var->yres_virtual > var->yres ||
489 var->xoffset || var->yoffset) {
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -0700490 dev_dbg(info->device,
491 "Virtual screen and panning are not supported\n");
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800492 return -EINVAL;
493 }
494
495 var->xres_virtual = var->xres;
496 var->yres_virtual = var->yres;
497
498 /* We support ARGB8888 only */
499 if (var->bits_per_pixel > 32 || var->grayscale ||
500 var->red.offset > 16 || var->green.offset > 8 ||
501 var->blue.offset > 0 || var->transp.offset > 24 ||
502 var->red.length > 8 || var->green.length > 8 ||
503 var->blue.length > 8 || var->transp.length > 8 ||
504 var->red.msb_right || var->green.msb_right ||
505 var->blue.msb_right || var->transp.msb_right || var->nonstd) {
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -0700506 dev_dbg(info->device, "We support ARGB8888 only\n");
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800507 return -EINVAL;
508 }
509
510 var->bits_per_pixel = 32;
511 var->red.offset = 16;
512 var->green.offset = 8;
513 var->blue.offset = 0;
514 var->transp.offset = 24;
515 var->red.length = 8;
516 var->green.length = 8;
517 var->blue.length = 8;
518 var->transp.length = 8;
519 var->red.msb_right = 0;
520 var->green.msb_right = 0;
521 var->blue.msb_right = 0;
522 var->transp.msb_right = 0;
523
524 /* Rotation is not supported */
525 if (var->rotate) {
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -0700526 dev_dbg(info->device, "Rotation is not supported\n");
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800527 return -EINVAL;
528 }
529
530 /* Memory limit */
Geert Uytterhoevenc95344a2007-10-16 01:29:25 -0700531 i = ps3fb_get_res_table(var->xres, var->yres, mode);
Geert Uytterhoeven2ce32e12007-10-16 01:29:48 -0700532 if (ps3fb_res[i].xres*ps3fb_res[i].yres*BPP >
533 ps3fb.xdr_size - VFB_OFF(i)) {
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -0700534 dev_dbg(info->device, "Not enough memory\n");
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800535 return -ENOMEM;
536 }
537
538 var->height = -1;
539 var->width = -1;
540
541 return 0;
542}
543
544 /*
545 * This routine actually sets the video mode.
546 */
547
548static int ps3fb_set_par(struct fb_info *info)
549{
550 unsigned int mode;
551 int i;
552 unsigned long offset;
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800553
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -0700554 dev_dbg(info->device, "xres:%d xv:%d yres:%d yv:%d clock:%d\n",
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800555 info->var.xres, info->var.xres_virtual,
556 info->var.yres, info->var.yres_virtual, info->var.pixclock);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800557
558 mode = ps3fb_find_mode(&info->var, &info->fix.line_length);
559 if (!mode)
560 return -EINVAL;
561
Geert Uytterhoevenc95344a2007-10-16 01:29:25 -0700562 i = ps3fb_get_res_table(info->var.xres, info->var.yres, mode);
563 ps3fb.res_index = i;
564
Geert Uytterhoeven2ce32e12007-10-16 01:29:48 -0700565 offset = VFB_OFF(i);
566 info->fix.smem_start = virt_to_abs(ps3fb.xdr_ea) + offset;
567 info->fix.smem_len = ps3fb.xdr_size - offset;
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800568 info->screen_base = (char __iomem *)ps3fb.xdr_ea + offset;
Geert Uytterhoeven2ce32e12007-10-16 01:29:48 -0700569 memset(ps3fb.xdr_ea, 0, ps3fb.xdr_size);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800570
Geert Uytterhoeven2ce32e12007-10-16 01:29:48 -0700571 ps3fb.num_frames = info->fix.smem_len/
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800572 (ps3fb_res[i].xres*ps3fb_res[i].yres*BPP);
573
574 /* Keep the special bits we cannot set using fb_var_screeninfo */
575 ps3fb_mode = (ps3fb_mode & ~PS3AV_MODE_MASK) | mode;
576
Geert Uytterhoevence4c3712007-10-16 01:29:44 -0700577 if (ps3av_set_video_mode(ps3fb_mode))
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800578 return -EINVAL;
579
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800580 return 0;
581}
582
583 /*
584 * Set a single color register. The values supplied are already
585 * rounded down to the hardware's capabilities (according to the
586 * entries in the var structure). Return != 0 for invalid regno.
587 */
588
589static int ps3fb_setcolreg(unsigned int regno, unsigned int red,
590 unsigned int green, unsigned int blue,
591 unsigned int transp, struct fb_info *info)
592{
593 if (regno >= 16)
594 return 1;
595
596 red >>= 8;
597 green >>= 8;
598 blue >>= 8;
599 transp >>= 8;
600
601 ((u32 *)info->pseudo_palette)[regno] = transp << 24 | red << 16 |
602 green << 8 | blue;
603 return 0;
604}
605
606 /*
607 * As we have a virtual frame buffer, we need our own mmap function
608 */
609
610static int ps3fb_mmap(struct fb_info *info, struct vm_area_struct *vma)
611{
612 unsigned long size, offset;
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800613
614 size = vma->vm_end - vma->vm_start;
615 offset = vma->vm_pgoff << PAGE_SHIFT;
616 if (offset + size > info->fix.smem_len)
617 return -EINVAL;
618
Geert Uytterhoeven2ce32e12007-10-16 01:29:48 -0700619 offset += info->fix.smem_start;
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800620 if (remap_pfn_range(vma, vma->vm_start, offset >> PAGE_SHIFT,
621 size, vma->vm_page_prot))
622 return -EAGAIN;
623
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -0700624 dev_dbg(info->device, "ps3fb: mmap framebuffer P(%lx)->V(%lx)\n",
625 offset, vma->vm_start);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800626 return 0;
627}
628
629 /*
630 * Blank the display
631 */
632
633static int ps3fb_blank(int blank, struct fb_info *info)
634{
635 int retval;
636
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -0700637 dev_dbg(info->device, "%s: blank:%d\n", __func__, blank);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800638 switch (blank) {
639 case FB_BLANK_POWERDOWN:
640 case FB_BLANK_HSYNC_SUSPEND:
641 case FB_BLANK_VSYNC_SUSPEND:
642 case FB_BLANK_NORMAL:
643 retval = ps3av_video_mute(1); /* mute on */
644 if (!retval)
645 ps3fb.is_blanked = 1;
646 break;
647
648 default: /* unblank */
649 retval = ps3av_video_mute(0); /* mute off */
650 if (!retval)
651 ps3fb.is_blanked = 0;
652 break;
653 }
654 return retval;
655}
656
657static int ps3fb_get_vblank(struct fb_vblank *vblank)
658{
659 memset(vblank, 0, sizeof(&vblank));
660 vblank->flags = FB_VBLANK_HAVE_VSYNC;
661 return 0;
662}
663
Geert Uytterhoeven15e4d002007-10-16 01:29:47 -0700664static int ps3fb_wait_for_vsync(u32 crtc)
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800665{
666 int ret;
667 u64 count;
668
669 count = ps3fb.vblank_count;
670 ret = wait_event_interruptible_timeout(ps3fb.wait_vsync,
671 count != ps3fb.vblank_count,
672 HZ / 10);
673 if (!ret)
674 return -ETIMEDOUT;
675
676 return 0;
677}
678
Geert Uytterhoeven15e4d002007-10-16 01:29:47 -0700679static void ps3fb_flip_ctl(int on, void *data)
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800680{
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +1000681 struct ps3fb_priv *priv = data;
Geert Uytterhoeveneca28742007-05-02 14:48:32 +0200682 if (on)
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +1000683 atomic_dec_if_positive(&priv->ext_flip);
Geert Uytterhoeveneca28742007-05-02 14:48:32 +0200684 else
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +1000685 atomic_inc(&priv->ext_flip);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800686}
687
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800688
689 /*
690 * ioctl
691 */
692
693static int ps3fb_ioctl(struct fb_info *info, unsigned int cmd,
694 unsigned long arg)
695{
696 void __user *argp = (void __user *)arg;
697 u32 val, old_mode;
698 int retval = -EFAULT;
699
700 switch (cmd) {
701 case FBIOGET_VBLANK:
702 {
703 struct fb_vblank vblank;
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -0700704 dev_dbg(info->device, "FBIOGET_VBLANK:\n");
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800705 retval = ps3fb_get_vblank(&vblank);
706 if (retval)
707 break;
708
709 if (copy_to_user(argp, &vblank, sizeof(vblank)))
710 retval = -EFAULT;
711 break;
712 }
713
714 case FBIO_WAITFORVSYNC:
715 {
716 u32 crt;
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -0700717 dev_dbg(info->device, "FBIO_WAITFORVSYNC:\n");
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800718 if (get_user(crt, (u32 __user *) arg))
719 break;
720
721 retval = ps3fb_wait_for_vsync(crt);
722 break;
723 }
724
725 case PS3FB_IOCTL_SETMODE:
726 {
727 const struct fb_videomode *mode;
728 struct fb_var_screeninfo var;
729
730 if (copy_from_user(&val, argp, sizeof(val)))
731 break;
732
Masashi Kimoto64072902007-05-02 14:48:36 +0200733 if (!(val & PS3AV_MODE_MASK)) {
Geert Uytterhoevence4c3712007-10-16 01:29:44 -0700734 u32 id = ps3av_get_auto_mode();
Masashi Kimoto64072902007-05-02 14:48:36 +0200735 if (id > 0)
736 val = (val & ~PS3AV_MODE_MASK) | id;
737 }
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -0700738 dev_dbg(info->device, "PS3FB_IOCTL_SETMODE:%x\n", val);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800739 retval = -EINVAL;
740 old_mode = ps3fb_mode;
741 ps3fb_mode = val;
742 mode = ps3fb_default_mode();
743 if (mode) {
744 var = info->var;
745 fb_videomode_to_var(&var, mode);
746 acquire_console_sem();
747 info->flags |= FBINFO_MISC_USEREVENT;
748 /* Force, in case only special bits changed */
749 var.activate |= FB_ACTIVATE_FORCE;
750 retval = fb_set_var(info, &var);
751 info->flags &= ~FBINFO_MISC_USEREVENT;
752 release_console_sem();
753 }
754 if (retval)
755 ps3fb_mode = old_mode;
756 break;
757 }
758
759 case PS3FB_IOCTL_GETMODE:
760 val = ps3av_get_mode();
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -0700761 dev_dbg(info->device, "PS3FB_IOCTL_GETMODE:%x\n", val);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800762 if (!copy_to_user(argp, &val, sizeof(val)))
763 retval = 0;
764 break;
765
766 case PS3FB_IOCTL_SCREENINFO:
767 {
768 struct ps3fb_ioctl_res res;
769 int i = ps3fb.res_index;
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -0700770 dev_dbg(info->device, "PS3FB_IOCTL_SCREENINFO:\n");
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800771 res.xres = ps3fb_res[i].xres;
772 res.yres = ps3fb_res[i].yres;
773 res.xoff = ps3fb_res[i].xoff;
774 res.yoff = ps3fb_res[i].yoff;
775 res.num_frames = ps3fb.num_frames;
776 if (!copy_to_user(argp, &res, sizeof(res)))
777 retval = 0;
778 break;
779 }
780
781 case PS3FB_IOCTL_ON:
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -0700782 dev_dbg(info->device, "PS3FB_IOCTL_ON:\n");
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800783 atomic_inc(&ps3fb.ext_flip);
784 retval = 0;
785 break;
786
787 case PS3FB_IOCTL_OFF:
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -0700788 dev_dbg(info->device, "PS3FB_IOCTL_OFF:\n");
Geert Uytterhoeveneca28742007-05-02 14:48:32 +0200789 atomic_dec_if_positive(&ps3fb.ext_flip);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800790 retval = 0;
791 break;
792
793 case PS3FB_IOCTL_FSEL:
794 if (copy_from_user(&val, argp, sizeof(val)))
795 break;
796
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -0700797 dev_dbg(info->device, "PS3FB_IOCTL_FSEL:%d\n", val);
798 retval = ps3fb_sync(info, val);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800799 break;
800
801 default:
802 retval = -ENOIOCTLCMD;
803 break;
804 }
805 return retval;
806}
807
808static int ps3fbd(void *arg)
809{
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -0700810 struct fb_info *info = arg;
811
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700812 set_freezable();
Geert Uytterhoeven1c0c8462007-05-02 14:48:31 +0200813 while (!kthread_should_stop()) {
814 try_to_freeze();
815 set_current_state(TASK_INTERRUPTIBLE);
816 if (ps3fb.is_kicked) {
817 ps3fb.is_kicked = 0;
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -0700818 ps3fb_sync(info, 0); /* single buffer */
Geert Uytterhoeven1c0c8462007-05-02 14:48:31 +0200819 }
820 schedule();
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800821 }
822 return 0;
823}
824
825static irqreturn_t ps3fb_vsync_interrupt(int irq, void *ptr)
826{
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -0700827 struct device *dev = ptr;
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800828 u64 v1;
829 int status;
830 struct display_head *head = &ps3fb.dinfo->display_head[1];
831
832 status = lv1_gpu_context_intr(ps3fb.context_handle, &v1);
833 if (status) {
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -0700834 dev_err(dev, "%s: lv1_gpu_context_intr failed: %d\n", __func__,
835 status);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800836 return IRQ_NONE;
837 }
838
839 if (v1 & (1 << GPU_INTR_STATUS_VSYNC_1)) {
840 /* VSYNC */
841 ps3fb.vblank_count = head->vblank_count;
Geert Uytterhoeven1c0c8462007-05-02 14:48:31 +0200842 if (ps3fb.task && !ps3fb.is_blanked &&
843 !atomic_read(&ps3fb.ext_flip)) {
844 ps3fb.is_kicked = 1;
845 wake_up_process(ps3fb.task);
846 }
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800847 wake_up_interruptible(&ps3fb.wait_vsync);
848 }
849
850 return IRQ_HANDLED;
851}
852
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800853
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +1000854static int ps3fb_vsync_settings(struct gpu_driver_info *dinfo,
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -0700855 struct device *dev)
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800856{
857 int error;
858
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -0700859 dev_dbg(dev, "version_driver:%x\n", dinfo->version_driver);
860 dev_dbg(dev, "irq outlet:%x\n", dinfo->irq.irq_outlet);
861 dev_dbg(dev,
862 "version_gpu: %x memory_size: %x ch: %x core_freq: %d "
863 "mem_freq:%d\n",
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800864 dinfo->version_gpu, dinfo->memory_size, dinfo->hardware_channel,
865 dinfo->nvcore_frequency/1000000, dinfo->memory_frequency/1000000);
866
867 if (dinfo->version_driver != GPU_DRIVER_INFO_VERSION) {
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -0700868 dev_err(dev, "%s: version_driver err:%x\n", __func__,
869 dinfo->version_driver);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800870 return -EINVAL;
871 }
872
Geoff Levanddc4f60c2007-05-01 07:01:01 +1000873 error = ps3_irq_plug_setup(PS3_BINDING_CPU_ANY, dinfo->irq.irq_outlet,
874 &ps3fb.irq_no);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800875 if (error) {
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -0700876 dev_err(dev, "%s: ps3_alloc_irq failed %d\n", __func__, error);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800877 return error;
878 }
879
880 error = request_irq(ps3fb.irq_no, ps3fb_vsync_interrupt, IRQF_DISABLED,
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +1000881 DEVICE_NAME, dev);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800882 if (error) {
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -0700883 dev_err(dev, "%s: request_irq failed %d\n", __func__, error);
Geoff Levanddc4f60c2007-05-01 07:01:01 +1000884 ps3_irq_plug_destroy(ps3fb.irq_no);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800885 return error;
886 }
887
888 dinfo->irq.mask = (1 << GPU_INTR_STATUS_VSYNC_1) |
889 (1 << GPU_INTR_STATUS_FLIP_1);
890 return 0;
891}
892
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -0700893static int ps3fb_xdr_settings(u64 xdr_lpar, struct device *dev)
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800894{
895 int status;
896
897 status = lv1_gpu_context_iomap(ps3fb.context_handle, GPU_IOIF,
898 xdr_lpar, ps3fb_videomemory.size, 0);
899 if (status) {
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -0700900 dev_err(dev, "%s: lv1_gpu_context_iomap failed: %d\n",
901 __func__, status);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800902 return -ENXIO;
903 }
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -0700904 dev_dbg(dev,
905 "video:%p xdr_ea:%p ioif:%lx lpar:%lx phys:%lx size:%lx\n",
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800906 ps3fb_videomemory.address, ps3fb.xdr_ea, GPU_IOIF, xdr_lpar,
907 virt_to_abs(ps3fb.xdr_ea), ps3fb_videomemory.size);
908
909 status = lv1_gpu_context_attribute(ps3fb.context_handle,
910 L1GPU_CONTEXT_ATTRIBUTE_FB_SETUP,
Geert Uytterhoeven2ce32e12007-10-16 01:29:48 -0700911 xdr_lpar + ps3fb.xdr_size,
912 GPU_CMD_BUF_SIZE,
913 GPU_IOIF + ps3fb.xdr_size, 0);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800914 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
Geert Uytterhoeven2ce32e12007-10-16 01:29:48 -07001051 /* XDR frame buffer */
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001052 ps3fb.xdr_ea = ps3fb_videomemory.address;
1053 xdr_lpar = ps3_mm_phys_to_lpar(__pa(ps3fb.xdr_ea));
Geert Uytterhoeven2ce32e12007-10-16 01:29:48 -07001054
1055 /* Clear memory to prevent kernel info leakage into userspace */
1056 memset(ps3fb.xdr_ea, 0, ps3fb_videomemory.size);
1057
1058 /* The GPU command buffer is at the end of video memory */
1059 ps3fb.xdr_size = ps3fb_videomemory.size - GPU_CMD_BUF_SIZE;
1060
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -07001061 retval = ps3fb_xdr_settings(xdr_lpar, &dev->core);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001062 if (retval)
1063 goto err_free_irq;
1064
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +10001065 info = framebuffer_alloc(sizeof(u32) * 16, &dev->core);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001066 if (!info)
1067 goto err_free_irq;
1068
Geert Uytterhoeven2ce32e12007-10-16 01:29:48 -07001069 offset = VFB_OFF(ps3fb.res_index);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001070 info->screen_base = (char __iomem *)ps3fb.xdr_ea + offset;
1071 info->fbops = &ps3fb_ops;
1072
1073 info->fix = ps3fb_fix;
Geert Uytterhoeven2ce32e12007-10-16 01:29:48 -07001074 info->fix.smem_start = virt_to_abs(ps3fb.xdr_ea) + offset;
1075 info->fix.smem_len = ps3fb.xdr_size - offset;
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001076 info->pseudo_palette = info->par;
1077 info->par = NULL;
Geert Uytterhoevencfd13af2007-07-21 04:37:51 -07001078 info->flags = FBINFO_DEFAULT | FBINFO_READS_FAST;
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001079
1080 retval = fb_alloc_cmap(&info->cmap, 256, 0);
1081 if (retval < 0)
1082 goto err_framebuffer_release;
1083
1084 if (!fb_find_mode(&info->var, info, mode_option, ps3fb_modedb,
1085 ARRAY_SIZE(ps3fb_modedb), ps3fb_default_mode(), 32)) {
1086 retval = -EINVAL;
1087 goto err_fb_dealloc;
1088 }
1089
1090 fb_videomode_to_modelist(ps3fb_modedb, ARRAY_SIZE(ps3fb_modedb),
1091 &info->modelist);
1092
1093 retval = register_framebuffer(info);
1094 if (retval < 0)
1095 goto err_fb_dealloc;
1096
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +10001097 dev->core.driver_data = info;
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001098
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -07001099 dev_info(info->device, "%s %s, using %lu KiB of video memory\n",
1100 dev_driver_string(info->dev), info->dev->bus_id,
Geert Uytterhoeven2ce32e12007-10-16 01:29:48 -07001101 ps3fb.xdr_size >> 10);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001102
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +10001103 task = kthread_run(ps3fbd, info, DEVICE_NAME);
Geert Uytterhoeven1c0c8462007-05-02 14:48:31 +02001104 if (IS_ERR(task)) {
1105 retval = PTR_ERR(task);
1106 goto err_unregister_framebuffer;
1107 }
1108
1109 ps3fb.task = task;
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +10001110 ps3av_register_flip_ctl(ps3fb_flip_ctl, &ps3fb);
Geert Uytterhoeven1c0c8462007-05-02 14:48:31 +02001111
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001112 return 0;
1113
Geert Uytterhoeven1c0c8462007-05-02 14:48:31 +02001114err_unregister_framebuffer:
1115 unregister_framebuffer(info);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001116err_fb_dealloc:
1117 fb_dealloc_cmap(&info->cmap);
1118err_framebuffer_release:
1119 framebuffer_release(info);
1120err_free_irq:
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +10001121 free_irq(ps3fb.irq_no, dev);
Geoff Levanddc4f60c2007-05-01 07:01:01 +10001122 ps3_irq_plug_destroy(ps3fb.irq_no);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001123err_iounmap_dinfo:
1124 iounmap((u8 __iomem *)ps3fb.dinfo);
1125err_gpu_context_free:
1126 lv1_gpu_context_free(ps3fb.context_handle);
1127err_gpu_memory_free:
1128 lv1_gpu_memory_free(ps3fb.memory_handle);
1129err:
1130 return retval;
1131}
1132
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +10001133static int ps3fb_shutdown(struct ps3_system_bus_device *dev)
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001134{
1135 int status;
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +10001136 struct fb_info *info = dev->core.driver_data;
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001137
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -07001138 dev_dbg(&dev->core, " -> %s:%d\n", __func__, __LINE__);
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +10001139
1140 ps3fb_flip_ctl(0, &ps3fb); /* flip off */
1141 ps3fb.dinfo->irq.mask = 0;
1142
1143 if (info) {
1144 unregister_framebuffer(info);
1145 fb_dealloc_cmap(&info->cmap);
1146 framebuffer_release(info);
1147 }
1148
1149 ps3av_register_flip_ctl(NULL, NULL);
Geert Uytterhoeven1c0c8462007-05-02 14:48:31 +02001150 if (ps3fb.task) {
1151 struct task_struct *task = ps3fb.task;
1152 ps3fb.task = NULL;
1153 kthread_stop(task);
1154 }
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001155 if (ps3fb.irq_no) {
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +10001156 free_irq(ps3fb.irq_no, dev);
Geoff Levanddc4f60c2007-05-01 07:01:01 +10001157 ps3_irq_plug_destroy(ps3fb.irq_no);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001158 }
1159 iounmap((u8 __iomem *)ps3fb.dinfo);
1160
1161 status = lv1_gpu_context_free(ps3fb.context_handle);
1162 if (status)
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -07001163 dev_dbg(&dev->core, "lv1_gpu_context_free failed: %d\n",
1164 status);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001165
1166 status = lv1_gpu_memory_free(ps3fb.memory_handle);
1167 if (status)
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -07001168 dev_dbg(&dev->core, "lv1_gpu_memory_free failed: %d\n",
1169 status);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001170
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +10001171 ps3_close_hv_device(dev);
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -07001172 dev_dbg(&dev->core, " <- %s:%d\n", __func__, __LINE__);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001173
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001174 return 0;
1175}
1176
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +10001177static struct ps3_system_bus_driver ps3fb_driver = {
1178 .match_id = PS3_MATCH_ID_GRAPHICS,
1179 .core.name = DEVICE_NAME,
1180 .core.owner = THIS_MODULE,
1181 .probe = ps3fb_probe,
1182 .remove = ps3fb_shutdown,
1183 .shutdown = ps3fb_shutdown,
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001184};
1185
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +10001186static int __init ps3fb_setup(void)
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001187{
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +10001188 char *options;
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001189
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +10001190#ifdef MODULE
1191 return 0;
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001192#endif
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001193
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +10001194 if (fb_get_options(DEVICE_NAME, &options))
1195 return -ENXIO;
1196
1197 if (!options || !*options)
1198 return 0;
1199
1200 while (1) {
1201 char *this_opt = strsep(&options, ",");
1202
1203 if (!this_opt)
1204 break;
1205 if (!*this_opt)
1206 continue;
1207 if (!strncmp(this_opt, "mode:", 5))
1208 ps3fb_mode = simple_strtoul(this_opt + 5, NULL, 0);
1209 else
1210 mode_option = this_opt;
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001211 }
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001212 return 0;
1213}
1214
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001215static int __init ps3fb_init(void)
1216{
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +10001217 if (!ps3fb_videomemory.address || ps3fb_setup())
1218 return -ENXIO;
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001219
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +10001220 return ps3_system_bus_driver_register(&ps3fb_driver);
1221}
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001222
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +10001223static void __exit ps3fb_exit(void)
1224{
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -07001225 pr_debug(" -> %s:%d\n", __func__, __LINE__);
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +10001226 ps3_system_bus_driver_unregister(&ps3fb_driver);
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -07001227 pr_debug(" <- %s:%d\n", __func__, __LINE__);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001228}
1229
1230module_init(ps3fb_init);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001231module_exit(ps3fb_exit);
1232
1233MODULE_LICENSE("GPL");
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +10001234MODULE_DESCRIPTION("PS3 GPU Frame Buffer Driver");
1235MODULE_AUTHOR("Sony Computer Entertainment Inc.");
1236MODULE_ALIAS(PS3_MODULE_ALIAS_GRAPHICS);