blob: 38ac805db97d4bcb5cdca4866f0fd3cfb430dce3 [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
Geert Uytterhoeven9ac67a32007-12-19 11:16:41 +010054#define GPU_CMD_BUF_SIZE (2 * 1024 * 1024)
55#define GPU_FB_START (64 * 1024)
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -080056#define GPU_IOIF (0x0d000000UL)
Geert Uytterhoevenf1664ed2007-10-16 01:29:49 -070057#define GPU_ALIGN_UP(x) _ALIGN_UP((x), 64)
Geert Uytterhoeven61e0b282007-10-16 01:29:51 -070058#define GPU_MAX_LINE_LENGTH (65536 - 64)
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -080059
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -080060#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;
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800119 struct gpu_driver_info *dinfo;
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800120
121 u64 vblank_count; /* frame count */
122 wait_queue_head_t wait_vsync;
123
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800124 atomic_t ext_flip; /* on/off flip with vsync */
125 atomic_t f_count; /* fb_open count */
126 int is_blanked;
Geert Uytterhoeven1c0c8462007-05-02 14:48:31 +0200127 int is_kicked;
128 struct task_struct *task;
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800129};
130static struct ps3fb_priv ps3fb;
131
Geert Uytterhoeven0333d832007-10-16 01:29:48 -0700132struct ps3fb_par {
133 u32 pseudo_palette[16];
134 int mode_id, new_mode_id;
Geert Uytterhoeven0333d832007-10-16 01:29:48 -0700135 unsigned int num_frames; /* num of frame buffers */
Geert Uytterhoevenf1664ed2007-10-16 01:29:49 -0700136 unsigned int width;
137 unsigned int height;
Geert Uytterhoeven9f4f21b2008-02-06 01:39:31 -0800138 unsigned int ddr_line_length;
139 unsigned int ddr_frame_size;
140 unsigned int xdr_frame_size;
Geert Uytterhoeven7974f722008-02-06 01:39:32 -0800141 unsigned int full_offset; /* start of fullscreen DDR fb */
142 unsigned int fb_offset; /* start of actual DDR fb */
143 unsigned int pan_offset;
Geert Uytterhoeven0333d832007-10-16 01:29:48 -0700144};
145
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800146
Geert Uytterhoeven34c422f2008-02-06 01:39:34 -0800147#define FIRST_NATIVE_MODE_INDEX 10
148
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800149static const struct fb_videomode ps3fb_modedb[] = {
150 /* 60 Hz broadcast modes (modes "1" to "5") */
151 {
152 /* 480i */
153 "480i", 60, 576, 384, 74074, 130, 89, 78, 57, 63, 6,
154 FB_SYNC_BROADCAST, FB_VMODE_INTERLACED
155 }, {
156 /* 480p */
157 "480p", 60, 576, 384, 37037, 130, 89, 78, 57, 63, 6,
158 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
159 }, {
160 /* 720p */
161 "720p", 60, 1124, 644, 13481, 298, 148, 57, 44, 80, 5,
162 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
163 }, {
164 /* 1080i */
165 "1080i", 60, 1688, 964, 13481, 264, 160, 94, 62, 88, 5,
166 FB_SYNC_BROADCAST, FB_VMODE_INTERLACED
167 }, {
168 /* 1080p */
169 "1080p", 60, 1688, 964, 6741, 264, 160, 94, 62, 88, 5,
170 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
171 },
172
173 /* 50 Hz broadcast modes (modes "6" to "10") */
174 {
175 /* 576i */
176 "576i", 50, 576, 460, 74074, 142, 83, 97, 63, 63, 5,
177 FB_SYNC_BROADCAST, FB_VMODE_INTERLACED
178 }, {
179 /* 576p */
180 "576p", 50, 576, 460, 37037, 142, 83, 97, 63, 63, 5,
181 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
182 }, {
183 /* 720p */
184 "720p", 50, 1124, 644, 13468, 298, 478, 57, 44, 80, 5,
185 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
186 }, {
Geoff Levanda782eed2008-02-06 01:39:36 -0800187 /* 1080i */
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800188 "1080i", 50, 1688, 964, 13468, 264, 600, 94, 62, 88, 5,
189 FB_SYNC_BROADCAST, FB_VMODE_INTERLACED
190 }, {
191 /* 1080p */
192 "1080p", 50, 1688, 964, 6734, 264, 600, 94, 62, 88, 5,
193 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
194 },
195
Geert Uytterhoeven34c422f2008-02-06 01:39:34 -0800196 [FIRST_NATIVE_MODE_INDEX] =
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800197 /* 60 Hz broadcast modes (full resolution versions of modes "1" to "5") */
198 {
199 /* 480if */
200 "480if", 60, 720, 480, 74074, 58, 17, 30, 9, 63, 6,
201 FB_SYNC_BROADCAST, FB_VMODE_INTERLACED
202 }, {
203 /* 480pf */
204 "480pf", 60, 720, 480, 37037, 58, 17, 30, 9, 63, 6,
205 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
206 }, {
207 /* 720pf */
208 "720pf", 60, 1280, 720, 13481, 220, 70, 19, 6, 80, 5,
209 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
210 }, {
211 /* 1080if */
212 "1080if", 60, 1920, 1080, 13481, 148, 44, 36, 4, 88, 5,
213 FB_SYNC_BROADCAST, FB_VMODE_INTERLACED
214 }, {
215 /* 1080pf */
216 "1080pf", 60, 1920, 1080, 6741, 148, 44, 36, 4, 88, 5,
217 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
218 },
219
220 /* 50 Hz broadcast modes (full resolution versions of modes "6" to "10") */
221 {
222 /* 576if */
223 "576if", 50, 720, 576, 74074, 70, 11, 39, 5, 63, 5,
224 FB_SYNC_BROADCAST, FB_VMODE_INTERLACED
225 }, {
226 /* 576pf */
227 "576pf", 50, 720, 576, 37037, 70, 11, 39, 5, 63, 5,
228 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
229 }, {
230 /* 720pf */
231 "720pf", 50, 1280, 720, 13468, 220, 400, 19, 6, 80, 5,
232 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
233 }, {
234 /* 1080if */
Geoff Levanda782eed2008-02-06 01:39:36 -0800235 "1080if", 50, 1920, 1080, 13468, 148, 484, 36, 4, 88, 5,
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800236 FB_SYNC_BROADCAST, FB_VMODE_INTERLACED
237 }, {
238 /* 1080pf */
239 "1080pf", 50, 1920, 1080, 6734, 148, 484, 36, 4, 88, 5,
240 FB_SYNC_BROADCAST, FB_VMODE_NONINTERLACED
Geert Uytterhoeven34c422f2008-02-06 01:39:34 -0800241 },
242
243 /* VESA modes (modes "11" to "13") */
244 {
245 /* WXGA */
246 "wxga", 60, 1280, 768, 12924, 160, 24, 29, 3, 136, 6,
247 0, FB_VMODE_NONINTERLACED,
248 FB_MODE_IS_VESA
249 }, {
250 /* SXGA */
251 "sxga", 60, 1280, 1024, 9259, 248, 48, 38, 1, 112, 3,
252 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED,
253 FB_MODE_IS_VESA
254 }, {
255 /* WUXGA */
256 "wuxga", 60, 1920, 1200, 6494, 80, 48, 26, 3, 32, 6,
257 FB_SYNC_HOR_HIGH_ACT, FB_VMODE_NONINTERLACED,
258 FB_MODE_IS_VESA
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800259 }
260};
261
262
263#define HEAD_A
264#define HEAD_B
265
Geert Uytterhoeven2ce32e12007-10-16 01:29:48 -0700266#define BPP 4 /* number of bytes per pixel */
267
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800268
Geert Uytterhoevenbd685ac2007-05-02 14:48:34 +0200269static int ps3fb_mode;
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +1000270module_param(ps3fb_mode, int, 0);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800271
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +1000272static char *mode_option __devinitdata;
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800273
Geert Uytterhoeven633bd112008-02-06 01:39:33 -0800274static int ps3fb_cmp_mode(const struct fb_videomode *vmode,
275 const struct fb_var_screeninfo *var)
276{
Geert Uytterhoevena3665362008-02-06 01:39:35 -0800277 long xres, yres, left_margin, right_margin, upper_margin, lower_margin;
278 long dx, dy;
279
280 /* maximum values */
281 if (var->xres > vmode->xres || var->yres > vmode->yres ||
282 var->pixclock > vmode->pixclock ||
283 var->hsync_len > vmode->hsync_len ||
284 var->vsync_len > vmode->vsync_len)
Geert Uytterhoeven633bd112008-02-06 01:39:33 -0800285 return -1;
286
Geert Uytterhoevena3665362008-02-06 01:39:35 -0800287 /* progressive/interlaced must match */
288 if ((var->vmode & FB_VMODE_MASK) != vmode->vmode)
Geert Uytterhoeven633bd112008-02-06 01:39:33 -0800289 return -1;
290
Geert Uytterhoevena3665362008-02-06 01:39:35 -0800291 /* minimum resolution */
292 xres = max(var->xres, 1U);
293 yres = max(var->yres, 1U);
294
295 /* minimum margins */
296 left_margin = max(var->left_margin, vmode->left_margin);
297 right_margin = max(var->right_margin, vmode->right_margin);
298 upper_margin = max(var->upper_margin, vmode->upper_margin);
299 lower_margin = max(var->lower_margin, vmode->lower_margin);
300
301 /* resolution + margins may not exceed native parameters */
302 dx = ((long)vmode->left_margin + (long)vmode->xres +
303 (long)vmode->right_margin) -
304 (left_margin + xres + right_margin);
305 if (dx < 0)
Geert Uytterhoeven633bd112008-02-06 01:39:33 -0800306 return -1;
307
Geert Uytterhoevena3665362008-02-06 01:39:35 -0800308 dy = ((long)vmode->upper_margin + (long)vmode->yres +
309 (long)vmode->lower_margin) -
310 (upper_margin + yres + lower_margin);
311 if (dy < 0)
312 return -1;
313
314 /* exact match */
315 if (!dx && !dy)
316 return 0;
317
318 /* resolution difference */
319 return (vmode->xres - xres) * (vmode->yres - yres);
Geert Uytterhoeven633bd112008-02-06 01:39:33 -0800320}
321
Geert Uytterhoeven34c422f2008-02-06 01:39:34 -0800322static const struct fb_videomode *ps3fb_native_vmode(enum ps3av_mode_num id)
323{
324 return &ps3fb_modedb[FIRST_NATIVE_MODE_INDEX + id - 1];
325}
326
327static const struct fb_videomode *ps3fb_vmode(int id)
328{
329 u32 mode = id & PS3AV_MODE_MASK;
330
331 if (mode < PS3AV_MODE_480I || mode > PS3AV_MODE_WUXGA)
332 return NULL;
333
334 if (mode <= PS3AV_MODE_1080P50 && !(id & PS3AV_MODE_FULL)) {
335 /* Non-fullscreen broadcast mode */
336 return &ps3fb_modedb[mode - 1];
337 }
338
339 return ps3fb_native_vmode(mode);
340}
341
Geert Uytterhoeven633bd112008-02-06 01:39:33 -0800342static unsigned int ps3fb_find_mode(struct fb_var_screeninfo *var,
Geert Uytterhoeven61e0b282007-10-16 01:29:51 -0700343 u32 *ddr_line_length, u32 *xdr_line_length)
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800344{
Geert Uytterhoevena3665362008-02-06 01:39:35 -0800345 unsigned int id, best_id;
346 int diff, best_diff;
Geert Uytterhoeven34c422f2008-02-06 01:39:34 -0800347 const struct fb_videomode *vmode;
Geert Uytterhoevena3665362008-02-06 01:39:35 -0800348 long gap;
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800349
Geert Uytterhoevena3665362008-02-06 01:39:35 -0800350 best_id = 0;
351 best_diff = INT_MAX;
352 pr_debug("%s: wanted %u [%u] %u x %u [%u] %u\n", __func__,
353 var->left_margin, var->xres, var->right_margin,
354 var->upper_margin, var->yres, var->lower_margin);
Geert Uytterhoeven34c422f2008-02-06 01:39:34 -0800355 for (id = PS3AV_MODE_480I; id <= PS3AV_MODE_WUXGA; id++) {
356 vmode = ps3fb_native_vmode(id);
Geert Uytterhoevena3665362008-02-06 01:39:35 -0800357 diff = ps3fb_cmp_mode(vmode, var);
358 pr_debug("%s: mode %u: %u [%u] %u x %u [%u] %u: diff = %d\n",
359 __func__, id, vmode->left_margin, vmode->xres,
360 vmode->right_margin, vmode->upper_margin,
361 vmode->yres, vmode->lower_margin, diff);
362 if (diff < 0)
363 continue;
364 if (diff < best_diff) {
365 best_id = id;
366 if (!diff)
367 break;
368 best_diff = diff;
369 }
Geert Uytterhoeven34c422f2008-02-06 01:39:34 -0800370 }
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800371
Geert Uytterhoevena3665362008-02-06 01:39:35 -0800372 if (!best_id) {
373 pr_debug("%s: no suitable mode found\n", __func__);
374 return 0;
375 }
Geert Uytterhoeven61e0b282007-10-16 01:29:51 -0700376
Geert Uytterhoevena3665362008-02-06 01:39:35 -0800377 id = best_id;
378 vmode = ps3fb_native_vmode(id);
379
Geert Uytterhoeven34c422f2008-02-06 01:39:34 -0800380 *ddr_line_length = vmode->xres * BPP;
Geert Uytterhoeven633bd112008-02-06 01:39:33 -0800381
Geert Uytterhoevena3665362008-02-06 01:39:35 -0800382 /* minimum resolution */
383 if (!var->xres)
Geert Uytterhoeven633bd112008-02-06 01:39:33 -0800384 var->xres = 1;
Geert Uytterhoevena3665362008-02-06 01:39:35 -0800385 if (!var->yres)
Geert Uytterhoeven633bd112008-02-06 01:39:33 -0800386 var->yres = 1;
Geert Uytterhoevena3665362008-02-06 01:39:35 -0800387
388 /* minimum virtual resolution */
389 if (var->xres_virtual < var->xres)
390 var->xres_virtual = var->xres;
391 if (var->yres_virtual < var->yres)
392 var->yres_virtual = var->yres;
393
394 /* minimum margins */
395 if (var->left_margin < vmode->left_margin)
396 var->left_margin = vmode->left_margin;
397 if (var->right_margin < vmode->right_margin)
398 var->right_margin = vmode->right_margin;
399 if (var->upper_margin < vmode->upper_margin)
400 var->upper_margin = vmode->upper_margin;
401 if (var->lower_margin < vmode->lower_margin)
402 var->lower_margin = vmode->lower_margin;
403
404 /* extra margins */
405 gap = ((long)vmode->left_margin + (long)vmode->xres +
406 (long)vmode->right_margin) -
407 ((long)var->left_margin + (long)var->xres +
408 (long)var->right_margin);
409 if (gap > 0) {
410 var->left_margin += gap/2;
411 var->right_margin += (gap+1)/2;
412 pr_debug("%s: rounded up H to %u [%u] %u\n", __func__,
413 var->left_margin, var->xres, var->right_margin);
Geert Uytterhoeven633bd112008-02-06 01:39:33 -0800414 }
Geert Uytterhoeven61e0b282007-10-16 01:29:51 -0700415
Geert Uytterhoevena3665362008-02-06 01:39:35 -0800416 gap = ((long)vmode->upper_margin + (long)vmode->yres +
417 (long)vmode->lower_margin) -
418 ((long)var->upper_margin + (long)var->yres +
419 (long)var->lower_margin);
420 if (gap > 0) {
421 var->upper_margin += gap/2;
422 var->lower_margin += (gap+1)/2;
423 pr_debug("%s: rounded up V to %u [%u] %u\n", __func__,
424 var->upper_margin, var->yres, var->lower_margin);
425 }
426
427 /* fixed fields */
428 var->pixclock = vmode->pixclock;
429 var->hsync_len = vmode->hsync_len;
430 var->vsync_len = vmode->vsync_len;
431 var->sync = vmode->sync;
432
Geert Uytterhoeven61e0b282007-10-16 01:29:51 -0700433 if (ps3_compare_firmware_version(1, 9, 0) >= 0) {
Geert Uytterhoevena3665362008-02-06 01:39:35 -0800434 *xdr_line_length = GPU_ALIGN_UP(var->xres_virtual * BPP);
Geert Uytterhoeven61e0b282007-10-16 01:29:51 -0700435 if (*xdr_line_length > GPU_MAX_LINE_LENGTH)
436 *xdr_line_length = GPU_MAX_LINE_LENGTH;
437 } else
438 *xdr_line_length = *ddr_line_length;
439
Geert Uytterhoeven34c422f2008-02-06 01:39:34 -0800440 if (vmode->sync & FB_SYNC_BROADCAST) {
Geert Uytterhoeven633bd112008-02-06 01:39:33 -0800441 /* Full broadcast modes have the full mode bit set */
Geert Uytterhoeven34c422f2008-02-06 01:39:34 -0800442 if (vmode->xres == var->xres && vmode->yres == var->yres)
443 id |= PS3AV_MODE_FULL;
Geert Uytterhoeven633bd112008-02-06 01:39:33 -0800444 }
Geert Uytterhoeven61e0b282007-10-16 01:29:51 -0700445
Geert Uytterhoeven34c422f2008-02-06 01:39:34 -0800446 pr_debug("%s: mode %u\n", __func__, id);
447 return id;
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800448}
449
Geert Uytterhoevenf1664ed2007-10-16 01:29:49 -0700450static void ps3fb_sync_image(struct device *dev, u64 frame_offset,
451 u64 dst_offset, u64 src_offset, u32 width,
Geert Uytterhoeven61e0b282007-10-16 01:29:51 -0700452 u32 height, u32 dst_line_length,
453 u32 src_line_length)
Geert Uytterhoevenf1664ed2007-10-16 01:29:49 -0700454{
455 int status;
Geert Uytterhoeven61e0b282007-10-16 01:29:51 -0700456 u64 line_length;
457
458 line_length = dst_line_length;
459 if (src_line_length != dst_line_length)
460 line_length |= (u64)src_line_length << 32;
Geert Uytterhoevenf1664ed2007-10-16 01:29:49 -0700461
Geert Uytterhoeven9ac67a32007-12-19 11:16:41 +0100462 src_offset += GPU_FB_START;
Geert Uytterhoeven9b82f3e2008-10-30 08:12:58 +0000463
464 mutex_lock(&ps3_gpu_mutex);
Geert Uytterhoevenf1664ed2007-10-16 01:29:49 -0700465 status = lv1_gpu_context_attribute(ps3fb.context_handle,
466 L1GPU_CONTEXT_ATTRIBUTE_FB_BLIT,
467 dst_offset, GPU_IOIF + src_offset,
468 L1GPU_FB_BLIT_WAIT_FOR_COMPLETION |
469 (width << 16) | height,
470 line_length);
Geert Uytterhoeven9b82f3e2008-10-30 08:12:58 +0000471 mutex_unlock(&ps3_gpu_mutex);
472
Geert Uytterhoevenf1664ed2007-10-16 01:29:49 -0700473 if (status)
474 dev_err(dev,
475 "%s: lv1_gpu_context_attribute FB_BLIT failed: %d\n",
476 __func__, status);
477#ifdef HEAD_A
478 status = lv1_gpu_context_attribute(ps3fb.context_handle,
479 L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_FLIP,
480 0, frame_offset, 0, 0);
481 if (status)
482 dev_err(dev, "%s: lv1_gpu_context_attribute FLIP failed: %d\n",
483 __func__, status);
484#endif
485#ifdef HEAD_B
486 status = lv1_gpu_context_attribute(ps3fb.context_handle,
487 L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_FLIP,
488 1, frame_offset, 0, 0);
489 if (status)
490 dev_err(dev, "%s: lv1_gpu_context_attribute FLIP failed: %d\n",
491 __func__, status);
492#endif
493}
494
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -0700495static int ps3fb_sync(struct fb_info *info, u32 frame)
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800496{
Geert Uytterhoeven0333d832007-10-16 01:29:48 -0700497 struct ps3fb_par *par = info->par;
Geert Uytterhoeven9f4f21b2008-02-06 01:39:31 -0800498 int error = 0;
Geert Uytterhoeven61e0b282007-10-16 01:29:51 -0700499 u64 ddr_base, xdr_base;
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800500
Geert Uytterhoeven0333d832007-10-16 01:29:48 -0700501 if (frame > par->num_frames - 1) {
Geert Uytterhoeven0138bd82007-10-16 01:29:46 -0700502 dev_dbg(info->device, "%s: invalid frame number (%u)\n",
503 __func__, frame);
Geert Uytterhoeven0333d832007-10-16 01:29:48 -0700504 error = -EINVAL;
505 goto out;
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800506 }
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800507
Geert Uytterhoeven9f4f21b2008-02-06 01:39:31 -0800508 xdr_base = frame * par->xdr_frame_size;
509 ddr_base = frame * par->ddr_frame_size;
Geert Uytterhoevenf1664ed2007-10-16 01:29:49 -0700510
Geert Uytterhoeven61e0b282007-10-16 01:29:51 -0700511 ps3fb_sync_image(info->device, ddr_base + par->full_offset,
512 ddr_base + par->fb_offset, xdr_base + par->pan_offset,
Geert Uytterhoeven9f4f21b2008-02-06 01:39:31 -0800513 par->width, par->height, par->ddr_line_length,
514 info->fix.line_length);
Geert Uytterhoeven0333d832007-10-16 01:29:48 -0700515
516out:
Geert Uytterhoeven0333d832007-10-16 01:29:48 -0700517 return error;
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800518}
519
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800520static int ps3fb_open(struct fb_info *info, int user)
521{
522 atomic_inc(&ps3fb.f_count);
523 return 0;
524}
525
526static int ps3fb_release(struct fb_info *info, int user)
527{
528 if (atomic_dec_and_test(&ps3fb.f_count)) {
529 if (atomic_read(&ps3fb.ext_flip)) {
530 atomic_set(&ps3fb.ext_flip, 0);
Jeremy Kerr8dab6372008-01-11 14:28:04 +0100531 if (!try_acquire_console_sem()) {
532 ps3fb_sync(info, 0); /* single buffer */
533 release_console_sem();
534 }
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800535 }
536 }
537 return 0;
538}
539
540 /*
541 * Setting the video mode has been split into two parts.
542 * First part, xxxfb_check_var, must not write anything
543 * to hardware, it should only verify and adjust var.
544 * This means it doesn't alter par but it does use hardware
545 * data from it to check this var.
546 */
547
548static int ps3fb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
549{
Geert Uytterhoeven61e0b282007-10-16 01:29:51 -0700550 u32 xdr_line_length, ddr_line_length;
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800551 int mode;
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800552
Geert Uytterhoeven61e0b282007-10-16 01:29:51 -0700553 mode = ps3fb_find_mode(var, &ddr_line_length, &xdr_line_length);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800554 if (!mode)
555 return -EINVAL;
556
Geert Uytterhoevenfc7028b72007-10-16 01:29:50 -0700557 /* Virtual screen */
Geert Uytterhoeven61e0b282007-10-16 01:29:51 -0700558 if (var->xres_virtual > xdr_line_length / BPP) {
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -0700559 dev_dbg(info->device,
Geert Uytterhoevenfc7028b72007-10-16 01:29:50 -0700560 "Horizontal virtual screen size too large\n");
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800561 return -EINVAL;
562 }
563
Geert Uytterhoevenfc7028b72007-10-16 01:29:50 -0700564 if (var->xoffset + var->xres > var->xres_virtual ||
565 var->yoffset + var->yres > var->yres_virtual) {
566 dev_dbg(info->device, "panning out-of-range\n");
567 return -EINVAL;
568 }
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800569
570 /* We support ARGB8888 only */
571 if (var->bits_per_pixel > 32 || var->grayscale ||
572 var->red.offset > 16 || var->green.offset > 8 ||
573 var->blue.offset > 0 || var->transp.offset > 24 ||
574 var->red.length > 8 || var->green.length > 8 ||
575 var->blue.length > 8 || var->transp.length > 8 ||
576 var->red.msb_right || var->green.msb_right ||
577 var->blue.msb_right || var->transp.msb_right || var->nonstd) {
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -0700578 dev_dbg(info->device, "We support ARGB8888 only\n");
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800579 return -EINVAL;
580 }
581
582 var->bits_per_pixel = 32;
583 var->red.offset = 16;
584 var->green.offset = 8;
585 var->blue.offset = 0;
586 var->transp.offset = 24;
587 var->red.length = 8;
588 var->green.length = 8;
589 var->blue.length = 8;
590 var->transp.length = 8;
591 var->red.msb_right = 0;
592 var->green.msb_right = 0;
593 var->blue.msb_right = 0;
594 var->transp.msb_right = 0;
595
596 /* Rotation is not supported */
597 if (var->rotate) {
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -0700598 dev_dbg(info->device, "Rotation is not supported\n");
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800599 return -EINVAL;
600 }
601
602 /* Memory limit */
Geert Uytterhoevena2864082008-02-06 01:39:35 -0800603 if (var->yres_virtual * xdr_line_length > info->fix.smem_len) {
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -0700604 dev_dbg(info->device, "Not enough memory\n");
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800605 return -ENOMEM;
606 }
607
608 var->height = -1;
609 var->width = -1;
610
611 return 0;
612}
613
614 /*
615 * This routine actually sets the video mode.
616 */
617
618static int ps3fb_set_par(struct fb_info *info)
619{
Geert Uytterhoeven0333d832007-10-16 01:29:48 -0700620 struct ps3fb_par *par = info->par;
Geert Uytterhoeven61e0b282007-10-16 01:29:51 -0700621 unsigned int mode, ddr_line_length, xdr_line_length, lines, maxlines;
Geert Uytterhoeven7974f722008-02-06 01:39:32 -0800622 unsigned int ddr_xoff, ddr_yoff, offset;
Geert Uytterhoeven9f4f21b2008-02-06 01:39:31 -0800623 const struct fb_videomode *vmode;
Geert Uytterhoeven61e0b282007-10-16 01:29:51 -0700624 u64 dst;
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800625
Geert Uytterhoeven61e0b282007-10-16 01:29:51 -0700626 mode = ps3fb_find_mode(&info->var, &ddr_line_length, &xdr_line_length);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800627 if (!mode)
628 return -EINVAL;
629
Geert Uytterhoeven34c422f2008-02-06 01:39:34 -0800630 vmode = ps3fb_native_vmode(mode & PS3AV_MODE_MASK);
Geert Uytterhoevenc95344a2007-10-16 01:29:25 -0700631
Geert Uytterhoevenfc7028b72007-10-16 01:29:50 -0700632 info->fix.xpanstep = info->var.xres_virtual > info->var.xres ? 1 : 0;
633 info->fix.ypanstep = info->var.yres_virtual > info->var.yres ? 1 : 0;
Geert Uytterhoeven61e0b282007-10-16 01:29:51 -0700634 info->fix.line_length = xdr_line_length;
Geert Uytterhoevenfc7028b72007-10-16 01:29:50 -0700635
Geert Uytterhoeven9f4f21b2008-02-06 01:39:31 -0800636 par->ddr_line_length = ddr_line_length;
637 par->ddr_frame_size = vmode->yres * ddr_line_length;
638 par->xdr_frame_size = info->var.yres_virtual * xdr_line_length;
639
Geert Uytterhoevena2864082008-02-06 01:39:35 -0800640 par->num_frames = info->fix.smem_len /
Geert Uytterhoeven9f4f21b2008-02-06 01:39:31 -0800641 max(par->ddr_frame_size, par->xdr_frame_size);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800642
643 /* Keep the special bits we cannot set using fb_var_screeninfo */
Geert Uytterhoeven0333d832007-10-16 01:29:48 -0700644 par->new_mode_id = (par->new_mode_id & ~PS3AV_MODE_MASK) | mode;
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800645
Geert Uytterhoevenf1664ed2007-10-16 01:29:49 -0700646 par->width = info->var.xres;
647 par->height = info->var.yres;
Geert Uytterhoevend9a4ba62008-02-06 01:39:31 -0800648
649 /* Start of the virtual frame buffer (relative to fullscreen) */
Geert Uytterhoeven9f4f21b2008-02-06 01:39:31 -0800650 ddr_xoff = info->var.left_margin - vmode->left_margin;
651 ddr_yoff = info->var.upper_margin - vmode->upper_margin;
652 offset = ddr_yoff * ddr_line_length + ddr_xoff * BPP;
Geert Uytterhoevend9a4ba62008-02-06 01:39:31 -0800653
Geert Uytterhoevenf1664ed2007-10-16 01:29:49 -0700654 par->fb_offset = GPU_ALIGN_UP(offset);
655 par->full_offset = par->fb_offset - offset;
Geert Uytterhoeven61e0b282007-10-16 01:29:51 -0700656 par->pan_offset = info->var.yoffset * xdr_line_length +
Geert Uytterhoevenfc7028b72007-10-16 01:29:50 -0700657 info->var.xoffset * BPP;
Geert Uytterhoevenf1664ed2007-10-16 01:29:49 -0700658
Geert Uytterhoeven0333d832007-10-16 01:29:48 -0700659 if (par->new_mode_id != par->mode_id) {
660 if (ps3av_set_video_mode(par->new_mode_id)) {
661 par->new_mode_id = par->mode_id;
662 return -EINVAL;
663 }
664 par->mode_id = par->new_mode_id;
665 }
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800666
Geert Uytterhoevenf1664ed2007-10-16 01:29:49 -0700667 /* Clear XDR frame buffer memory */
Geert Uytterhoevena2864082008-02-06 01:39:35 -0800668 memset((void __force *)info->screen_base, 0, info->fix.smem_len);
Geert Uytterhoevenf1664ed2007-10-16 01:29:49 -0700669
670 /* Clear DDR frame buffer memory */
Geert Uytterhoeven9f4f21b2008-02-06 01:39:31 -0800671 lines = vmode->yres * par->num_frames;
Geert Uytterhoevenf1664ed2007-10-16 01:29:49 -0700672 if (par->full_offset)
673 lines++;
Geert Uytterhoevena2864082008-02-06 01:39:35 -0800674 maxlines = info->fix.smem_len / ddr_line_length;
Geert Uytterhoeven61e0b282007-10-16 01:29:51 -0700675 for (dst = 0; lines; dst += maxlines * ddr_line_length) {
Geert Uytterhoevenf1664ed2007-10-16 01:29:49 -0700676 unsigned int l = min(lines, maxlines);
Geert Uytterhoeven9f4f21b2008-02-06 01:39:31 -0800677 ps3fb_sync_image(info->device, 0, dst, 0, vmode->xres, l,
Geert Uytterhoeven61e0b282007-10-16 01:29:51 -0700678 ddr_line_length, ddr_line_length);
Geert Uytterhoevenf1664ed2007-10-16 01:29:49 -0700679 lines -= l;
680 }
681
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800682 return 0;
683}
684
685 /*
686 * Set a single color register. The values supplied are already
687 * rounded down to the hardware's capabilities (according to the
688 * entries in the var structure). Return != 0 for invalid regno.
689 */
690
691static int ps3fb_setcolreg(unsigned int regno, unsigned int red,
692 unsigned int green, unsigned int blue,
693 unsigned int transp, struct fb_info *info)
694{
695 if (regno >= 16)
696 return 1;
697
698 red >>= 8;
699 green >>= 8;
700 blue >>= 8;
701 transp >>= 8;
702
703 ((u32 *)info->pseudo_palette)[regno] = transp << 24 | red << 16 |
704 green << 8 | blue;
705 return 0;
706}
707
Geert Uytterhoevenfc7028b72007-10-16 01:29:50 -0700708static int ps3fb_pan_display(struct fb_var_screeninfo *var,
709 struct fb_info *info)
710{
711 struct ps3fb_par *par = info->par;
712
713 par->pan_offset = var->yoffset * info->fix.line_length +
714 var->xoffset * BPP;
715 return 0;
716}
717
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800718 /*
719 * As we have a virtual frame buffer, we need our own mmap function
720 */
721
722static int ps3fb_mmap(struct fb_info *info, struct vm_area_struct *vma)
723{
724 unsigned long size, offset;
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800725
726 size = vma->vm_end - vma->vm_start;
727 offset = vma->vm_pgoff << PAGE_SHIFT;
728 if (offset + size > info->fix.smem_len)
729 return -EINVAL;
730
Geert Uytterhoeven2ce32e12007-10-16 01:29:48 -0700731 offset += info->fix.smem_start;
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800732 if (remap_pfn_range(vma, vma->vm_start, offset >> PAGE_SHIFT,
733 size, vma->vm_page_prot))
734 return -EAGAIN;
735
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -0700736 dev_dbg(info->device, "ps3fb: mmap framebuffer P(%lx)->V(%lx)\n",
737 offset, vma->vm_start);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800738 return 0;
739}
740
741 /*
742 * Blank the display
743 */
744
745static int ps3fb_blank(int blank, struct fb_info *info)
746{
747 int retval;
748
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -0700749 dev_dbg(info->device, "%s: blank:%d\n", __func__, blank);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800750 switch (blank) {
751 case FB_BLANK_POWERDOWN:
752 case FB_BLANK_HSYNC_SUSPEND:
753 case FB_BLANK_VSYNC_SUSPEND:
754 case FB_BLANK_NORMAL:
755 retval = ps3av_video_mute(1); /* mute on */
756 if (!retval)
757 ps3fb.is_blanked = 1;
758 break;
759
760 default: /* unblank */
761 retval = ps3av_video_mute(0); /* mute off */
762 if (!retval)
763 ps3fb.is_blanked = 0;
764 break;
765 }
766 return retval;
767}
768
769static int ps3fb_get_vblank(struct fb_vblank *vblank)
770{
Li Zefan3cc2c172007-11-14 16:58:33 -0800771 memset(vblank, 0, sizeof(*vblank));
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800772 vblank->flags = FB_VBLANK_HAVE_VSYNC;
773 return 0;
774}
775
Geert Uytterhoeven15e4d002007-10-16 01:29:47 -0700776static int ps3fb_wait_for_vsync(u32 crtc)
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800777{
778 int ret;
779 u64 count;
780
781 count = ps3fb.vblank_count;
782 ret = wait_event_interruptible_timeout(ps3fb.wait_vsync,
783 count != ps3fb.vblank_count,
784 HZ / 10);
785 if (!ret)
786 return -ETIMEDOUT;
787
788 return 0;
789}
790
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800791
792 /*
793 * ioctl
794 */
795
796static int ps3fb_ioctl(struct fb_info *info, unsigned int cmd,
797 unsigned long arg)
798{
799 void __user *argp = (void __user *)arg;
Geert Uytterhoeven0333d832007-10-16 01:29:48 -0700800 u32 val;
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800801 int retval = -EFAULT;
802
803 switch (cmd) {
804 case FBIOGET_VBLANK:
805 {
806 struct fb_vblank vblank;
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -0700807 dev_dbg(info->device, "FBIOGET_VBLANK:\n");
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800808 retval = ps3fb_get_vblank(&vblank);
809 if (retval)
810 break;
811
812 if (copy_to_user(argp, &vblank, sizeof(vblank)))
813 retval = -EFAULT;
814 break;
815 }
816
817 case FBIO_WAITFORVSYNC:
818 {
819 u32 crt;
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -0700820 dev_dbg(info->device, "FBIO_WAITFORVSYNC:\n");
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800821 if (get_user(crt, (u32 __user *) arg))
822 break;
823
824 retval = ps3fb_wait_for_vsync(crt);
825 break;
826 }
827
828 case PS3FB_IOCTL_SETMODE:
829 {
Geert Uytterhoeven0333d832007-10-16 01:29:48 -0700830 struct ps3fb_par *par = info->par;
Geert Uytterhoeven34c422f2008-02-06 01:39:34 -0800831 const struct fb_videomode *vmode;
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800832 struct fb_var_screeninfo var;
833
834 if (copy_from_user(&val, argp, sizeof(val)))
835 break;
836
Masashi Kimoto64072902007-05-02 14:48:36 +0200837 if (!(val & PS3AV_MODE_MASK)) {
Geert Uytterhoevence4c3712007-10-16 01:29:44 -0700838 u32 id = ps3av_get_auto_mode();
Masashi Kimoto64072902007-05-02 14:48:36 +0200839 if (id > 0)
840 val = (val & ~PS3AV_MODE_MASK) | id;
841 }
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -0700842 dev_dbg(info->device, "PS3FB_IOCTL_SETMODE:%x\n", val);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800843 retval = -EINVAL;
Geert Uytterhoeven34c422f2008-02-06 01:39:34 -0800844 vmode = ps3fb_vmode(val);
845 if (vmode) {
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800846 var = info->var;
Geert Uytterhoeven34c422f2008-02-06 01:39:34 -0800847 fb_videomode_to_var(&var, vmode);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800848 acquire_console_sem();
849 info->flags |= FBINFO_MISC_USEREVENT;
850 /* Force, in case only special bits changed */
851 var.activate |= FB_ACTIVATE_FORCE;
Geert Uytterhoeven0333d832007-10-16 01:29:48 -0700852 par->new_mode_id = val;
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800853 retval = fb_set_var(info, &var);
854 info->flags &= ~FBINFO_MISC_USEREVENT;
855 release_console_sem();
856 }
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800857 break;
858 }
859
860 case PS3FB_IOCTL_GETMODE:
861 val = ps3av_get_mode();
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -0700862 dev_dbg(info->device, "PS3FB_IOCTL_GETMODE:%x\n", val);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800863 if (!copy_to_user(argp, &val, sizeof(val)))
864 retval = 0;
865 break;
866
867 case PS3FB_IOCTL_SCREENINFO:
868 {
Geert Uytterhoeven0333d832007-10-16 01:29:48 -0700869 struct ps3fb_par *par = info->par;
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800870 struct ps3fb_ioctl_res res;
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -0700871 dev_dbg(info->device, "PS3FB_IOCTL_SCREENINFO:\n");
Geert Uytterhoeven61e0b282007-10-16 01:29:51 -0700872 res.xres = info->fix.line_length / BPP;
873 res.yres = info->var.yres_virtual;
874 res.xoff = (res.xres - info->var.xres) / 2;
875 res.yoff = (res.yres - info->var.yres) / 2;
Geert Uytterhoeven0333d832007-10-16 01:29:48 -0700876 res.num_frames = par->num_frames;
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800877 if (!copy_to_user(argp, &res, sizeof(res)))
878 retval = 0;
879 break;
880 }
881
882 case PS3FB_IOCTL_ON:
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -0700883 dev_dbg(info->device, "PS3FB_IOCTL_ON:\n");
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800884 atomic_inc(&ps3fb.ext_flip);
885 retval = 0;
886 break;
887
888 case PS3FB_IOCTL_OFF:
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -0700889 dev_dbg(info->device, "PS3FB_IOCTL_OFF:\n");
Geert Uytterhoeveneca28742007-05-02 14:48:32 +0200890 atomic_dec_if_positive(&ps3fb.ext_flip);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800891 retval = 0;
892 break;
893
894 case PS3FB_IOCTL_FSEL:
895 if (copy_from_user(&val, argp, sizeof(val)))
896 break;
897
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -0700898 dev_dbg(info->device, "PS3FB_IOCTL_FSEL:%d\n", val);
Jeremy Kerr8dab6372008-01-11 14:28:04 +0100899 acquire_console_sem();
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -0700900 retval = ps3fb_sync(info, val);
Jeremy Kerr8dab6372008-01-11 14:28:04 +0100901 release_console_sem();
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800902 break;
903
904 default:
905 retval = -ENOIOCTLCMD;
906 break;
907 }
908 return retval;
909}
910
911static int ps3fbd(void *arg)
912{
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -0700913 struct fb_info *info = arg;
914
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700915 set_freezable();
Geert Uytterhoeven1c0c8462007-05-02 14:48:31 +0200916 while (!kthread_should_stop()) {
917 try_to_freeze();
918 set_current_state(TASK_INTERRUPTIBLE);
919 if (ps3fb.is_kicked) {
920 ps3fb.is_kicked = 0;
Jeremy Kerr8dab6372008-01-11 14:28:04 +0100921 acquire_console_sem();
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -0700922 ps3fb_sync(info, 0); /* single buffer */
Jeremy Kerr8dab6372008-01-11 14:28:04 +0100923 release_console_sem();
Geert Uytterhoeven1c0c8462007-05-02 14:48:31 +0200924 }
925 schedule();
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800926 }
927 return 0;
928}
929
930static irqreturn_t ps3fb_vsync_interrupt(int irq, void *ptr)
931{
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -0700932 struct device *dev = ptr;
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800933 u64 v1;
934 int status;
935 struct display_head *head = &ps3fb.dinfo->display_head[1];
936
937 status = lv1_gpu_context_intr(ps3fb.context_handle, &v1);
938 if (status) {
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -0700939 dev_err(dev, "%s: lv1_gpu_context_intr failed: %d\n", __func__,
940 status);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800941 return IRQ_NONE;
942 }
943
944 if (v1 & (1 << GPU_INTR_STATUS_VSYNC_1)) {
945 /* VSYNC */
946 ps3fb.vblank_count = head->vblank_count;
Geert Uytterhoeven1c0c8462007-05-02 14:48:31 +0200947 if (ps3fb.task && !ps3fb.is_blanked &&
948 !atomic_read(&ps3fb.ext_flip)) {
949 ps3fb.is_kicked = 1;
950 wake_up_process(ps3fb.task);
951 }
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800952 wake_up_interruptible(&ps3fb.wait_vsync);
953 }
954
955 return IRQ_HANDLED;
956}
957
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800958
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +1000959static int ps3fb_vsync_settings(struct gpu_driver_info *dinfo,
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -0700960 struct device *dev)
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800961{
962 int error;
963
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -0700964 dev_dbg(dev, "version_driver:%x\n", dinfo->version_driver);
965 dev_dbg(dev, "irq outlet:%x\n", dinfo->irq.irq_outlet);
966 dev_dbg(dev,
967 "version_gpu: %x memory_size: %x ch: %x core_freq: %d "
968 "mem_freq:%d\n",
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800969 dinfo->version_gpu, dinfo->memory_size, dinfo->hardware_channel,
970 dinfo->nvcore_frequency/1000000, dinfo->memory_frequency/1000000);
971
972 if (dinfo->version_driver != GPU_DRIVER_INFO_VERSION) {
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -0700973 dev_err(dev, "%s: version_driver err:%x\n", __func__,
974 dinfo->version_driver);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800975 return -EINVAL;
976 }
977
Geoff Levanddc4f60c2007-05-01 07:01:01 +1000978 error = ps3_irq_plug_setup(PS3_BINDING_CPU_ANY, dinfo->irq.irq_outlet,
979 &ps3fb.irq_no);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800980 if (error) {
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -0700981 dev_err(dev, "%s: ps3_alloc_irq failed %d\n", __func__, error);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800982 return error;
983 }
984
985 error = request_irq(ps3fb.irq_no, ps3fb_vsync_interrupt, IRQF_DISABLED,
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +1000986 DEVICE_NAME, dev);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800987 if (error) {
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -0700988 dev_err(dev, "%s: request_irq failed %d\n", __func__, error);
Geoff Levanddc4f60c2007-05-01 07:01:01 +1000989 ps3_irq_plug_destroy(ps3fb.irq_no);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800990 return error;
991 }
992
993 dinfo->irq.mask = (1 << GPU_INTR_STATUS_VSYNC_1) |
994 (1 << GPU_INTR_STATUS_FLIP_1);
995 return 0;
996}
997
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -0700998static int ps3fb_xdr_settings(u64 xdr_lpar, struct device *dev)
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -0800999{
1000 int status;
1001
1002 status = lv1_gpu_context_iomap(ps3fb.context_handle, GPU_IOIF,
1003 xdr_lpar, ps3fb_videomemory.size, 0);
1004 if (status) {
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -07001005 dev_err(dev, "%s: lv1_gpu_context_iomap failed: %d\n",
1006 __func__, status);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001007 return -ENXIO;
1008 }
Geert Uytterhoevena2864082008-02-06 01:39:35 -08001009 dev_dbg(dev, "video:%p ioif:%lx lpar:%lx size:%lx\n",
1010 ps3fb_videomemory.address, GPU_IOIF, xdr_lpar,
1011 ps3fb_videomemory.size);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001012
1013 status = lv1_gpu_context_attribute(ps3fb.context_handle,
1014 L1GPU_CONTEXT_ATTRIBUTE_FB_SETUP,
Geert Uytterhoeven9ac67a32007-12-19 11:16:41 +01001015 xdr_lpar, GPU_CMD_BUF_SIZE,
1016 GPU_IOIF, 0);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001017 if (status) {
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -07001018 dev_err(dev,
1019 "%s: lv1_gpu_context_attribute FB_SETUP failed: %d\n",
1020 __func__, status);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001021 return -ENXIO;
1022 }
1023 return 0;
1024}
1025
1026static struct fb_ops ps3fb_ops = {
1027 .fb_open = ps3fb_open,
1028 .fb_release = ps3fb_release,
Geert Uytterhoeven92c45792007-05-23 13:57:50 -07001029 .fb_read = fb_sys_read,
1030 .fb_write = fb_sys_write,
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001031 .fb_check_var = ps3fb_check_var,
1032 .fb_set_par = ps3fb_set_par,
1033 .fb_setcolreg = ps3fb_setcolreg,
Geert Uytterhoevenfc7028b72007-10-16 01:29:50 -07001034 .fb_pan_display = ps3fb_pan_display,
Geert Uytterhoeven92c45792007-05-23 13:57:50 -07001035 .fb_fillrect = sys_fillrect,
1036 .fb_copyarea = sys_copyarea,
1037 .fb_imageblit = sys_imageblit,
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001038 .fb_mmap = ps3fb_mmap,
1039 .fb_blank = ps3fb_blank,
1040 .fb_ioctl = ps3fb_ioctl,
1041 .fb_compat_ioctl = ps3fb_ioctl
1042};
1043
1044static struct fb_fix_screeninfo ps3fb_fix __initdata = {
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +10001045 .id = DEVICE_NAME,
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001046 .type = FB_TYPE_PACKED_PIXELS,
1047 .visual = FB_VISUAL_TRUECOLOR,
1048 .accel = FB_ACCEL_NONE,
1049};
1050
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -07001051static int ps3fb_set_sync(struct device *dev)
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +10001052{
1053 int status;
1054
1055#ifdef HEAD_A
1056 status = lv1_gpu_context_attribute(0x0,
1057 L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_SYNC,
1058 0, L1GPU_DISPLAY_SYNC_VSYNC, 0, 0);
1059 if (status) {
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -07001060 dev_err(dev,
1061 "%s: lv1_gpu_context_attribute DISPLAY_SYNC failed: "
1062 "%d\n",
1063 __func__, status);
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +10001064 return -1;
1065 }
1066#endif
1067#ifdef HEAD_B
1068 status = lv1_gpu_context_attribute(0x0,
1069 L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_SYNC,
1070 1, L1GPU_DISPLAY_SYNC_VSYNC, 0, 0);
1071
1072 if (status) {
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -07001073 dev_err(dev,
1074 "%s: lv1_gpu_context_attribute DISPLAY_SYNC failed: "
1075 "%d\n",
1076 __func__, status);
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +10001077 return -1;
1078 }
1079#endif
1080 return 0;
1081}
1082
1083static int __devinit ps3fb_probe(struct ps3_system_bus_device *dev)
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001084{
1085 struct fb_info *info;
Geert Uytterhoeven0333d832007-10-16 01:29:48 -07001086 struct ps3fb_par *par;
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001087 int retval = -ENOMEM;
1088 u64 ddr_lpar = 0;
1089 u64 lpar_dma_control = 0;
1090 u64 lpar_driver_info = 0;
1091 u64 lpar_reports = 0;
1092 u64 lpar_reports_size = 0;
1093 u64 xdr_lpar;
Geert Uytterhoevena2864082008-02-06 01:39:35 -08001094 void *fb_start;
Geert Uytterhoeven9f4f21b2008-02-06 01:39:31 -08001095 int status;
Geert Uytterhoeven1c0c8462007-05-02 14:48:31 +02001096 struct task_struct *task;
Geert Uytterhoevenee592a52007-11-28 16:21:11 -08001097 unsigned long max_ps3fb_size;
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001098
Geert Uytterhoeven9ac67a32007-12-19 11:16:41 +01001099 if (ps3fb_videomemory.size < GPU_CMD_BUF_SIZE) {
1100 dev_err(&dev->core, "%s: Not enough video memory\n", __func__);
1101 return -ENOMEM;
1102 }
1103
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +10001104 status = ps3_open_hv_device(dev);
1105 if (status) {
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -07001106 dev_err(&dev->core, "%s: ps3_open_hv_device failed\n",
1107 __func__);
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +10001108 goto err;
1109 }
1110
1111 if (!ps3fb_mode)
1112 ps3fb_mode = ps3av_get_mode();
Geert Uytterhoeven084ffff2008-02-06 01:39:29 -08001113 dev_dbg(&dev->core, "ps3fb_mode: %d\n", ps3fb_mode);
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +10001114
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +10001115 atomic_set(&ps3fb.f_count, -1); /* fbcon opens ps3fb */
1116 atomic_set(&ps3fb.ext_flip, 0); /* for flip with vsync */
1117 init_waitqueue_head(&ps3fb.wait_vsync);
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +10001118
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -07001119 ps3fb_set_sync(&dev->core);
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +10001120
Geert Uytterhoevenee592a52007-11-28 16:21:11 -08001121 max_ps3fb_size = _ALIGN_UP(GPU_IOIF, 256*1024*1024) - GPU_IOIF;
1122 if (ps3fb_videomemory.size > max_ps3fb_size) {
1123 dev_info(&dev->core, "Limiting ps3fb mem size to %lu bytes\n",
1124 max_ps3fb_size);
1125 ps3fb_videomemory.size = max_ps3fb_size;
1126 }
1127
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001128 /* get gpu context handle */
Geert Uytterhoevenee592a52007-11-28 16:21:11 -08001129 status = lv1_gpu_memory_allocate(ps3fb_videomemory.size, 0, 0, 0, 0,
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001130 &ps3fb.memory_handle, &ddr_lpar);
1131 if (status) {
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -07001132 dev_err(&dev->core, "%s: lv1_gpu_memory_allocate failed: %d\n",
1133 __func__, status);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001134 goto err;
1135 }
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -07001136 dev_dbg(&dev->core, "ddr:lpar:0x%lx\n", ddr_lpar);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001137
1138 status = lv1_gpu_context_allocate(ps3fb.memory_handle, 0,
1139 &ps3fb.context_handle,
1140 &lpar_dma_control, &lpar_driver_info,
1141 &lpar_reports, &lpar_reports_size);
1142 if (status) {
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -07001143 dev_err(&dev->core,
1144 "%s: lv1_gpu_context_attribute failed: %d\n", __func__,
1145 status);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001146 goto err_gpu_memory_free;
1147 }
1148
1149 /* vsync interrupt */
Geert Uytterhoevena2864082008-02-06 01:39:35 -08001150 ps3fb.dinfo = (void __force *)ioremap(lpar_driver_info, 128 * 1024);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001151 if (!ps3fb.dinfo) {
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -07001152 dev_err(&dev->core, "%s: ioremap failed\n", __func__);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001153 goto err_gpu_context_free;
1154 }
1155
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -07001156 retval = ps3fb_vsync_settings(ps3fb.dinfo, &dev->core);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001157 if (retval)
1158 goto err_iounmap_dinfo;
1159
Geert Uytterhoeven2ce32e12007-10-16 01:29:48 -07001160 /* Clear memory to prevent kernel info leakage into userspace */
Geert Uytterhoevena2864082008-02-06 01:39:35 -08001161 memset(ps3fb_videomemory.address, 0, ps3fb_videomemory.size);
Geert Uytterhoeven2ce32e12007-10-16 01:29:48 -07001162
Geert Uytterhoevena2864082008-02-06 01:39:35 -08001163 xdr_lpar = ps3_mm_phys_to_lpar(__pa(ps3fb_videomemory.address));
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -07001164 retval = ps3fb_xdr_settings(xdr_lpar, &dev->core);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001165 if (retval)
1166 goto err_free_irq;
1167
Geert Uytterhoeven0333d832007-10-16 01:29:48 -07001168 info = framebuffer_alloc(sizeof(struct ps3fb_par), &dev->core);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001169 if (!info)
1170 goto err_free_irq;
1171
Geert Uytterhoeven0333d832007-10-16 01:29:48 -07001172 par = info->par;
1173 par->mode_id = ~ps3fb_mode; /* != ps3fb_mode, to trigger change */
1174 par->new_mode_id = ps3fb_mode;
Geert Uytterhoeven0333d832007-10-16 01:29:48 -07001175 par->num_frames = 1;
1176
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001177 info->fbops = &ps3fb_ops;
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001178 info->fix = ps3fb_fix;
Geert Uytterhoevena2864082008-02-06 01:39:35 -08001179
1180 /*
1181 * The GPU command buffer is at the start of video memory
1182 * As we don't use the full command buffer, we can put the actual
1183 * frame buffer at offset GPU_FB_START and save some precious XDR
1184 * memory
1185 */
1186 fb_start = ps3fb_videomemory.address + GPU_FB_START;
1187 info->screen_base = (char __force __iomem *)fb_start;
1188 info->fix.smem_start = virt_to_abs(fb_start);
1189 info->fix.smem_len = ps3fb_videomemory.size - GPU_FB_START;
1190
Geert Uytterhoeven0333d832007-10-16 01:29:48 -07001191 info->pseudo_palette = par->pseudo_palette;
Geert Uytterhoevenfc7028b72007-10-16 01:29:50 -07001192 info->flags = FBINFO_DEFAULT | FBINFO_READS_FAST |
1193 FBINFO_HWACCEL_XPAN | FBINFO_HWACCEL_YPAN;
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001194
1195 retval = fb_alloc_cmap(&info->cmap, 256, 0);
1196 if (retval < 0)
1197 goto err_framebuffer_release;
1198
1199 if (!fb_find_mode(&info->var, info, mode_option, ps3fb_modedb,
Geert Uytterhoeven0333d832007-10-16 01:29:48 -07001200 ARRAY_SIZE(ps3fb_modedb),
Geert Uytterhoeven34c422f2008-02-06 01:39:34 -08001201 ps3fb_vmode(par->new_mode_id), 32)) {
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001202 retval = -EINVAL;
1203 goto err_fb_dealloc;
1204 }
1205
1206 fb_videomode_to_modelist(ps3fb_modedb, ARRAY_SIZE(ps3fb_modedb),
1207 &info->modelist);
1208
1209 retval = register_framebuffer(info);
1210 if (retval < 0)
1211 goto err_fb_dealloc;
1212
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +10001213 dev->core.driver_data = info;
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001214
Geert Uytterhoevena2864082008-02-06 01:39:35 -08001215 dev_info(info->device, "%s %s, using %u KiB of video memory\n",
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -07001216 dev_driver_string(info->dev), info->dev->bus_id,
Geert Uytterhoevena2864082008-02-06 01:39:35 -08001217 info->fix.smem_len >> 10);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001218
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +10001219 task = kthread_run(ps3fbd, info, DEVICE_NAME);
Geert Uytterhoeven1c0c8462007-05-02 14:48:31 +02001220 if (IS_ERR(task)) {
1221 retval = PTR_ERR(task);
1222 goto err_unregister_framebuffer;
1223 }
1224
1225 ps3fb.task = task;
1226
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001227 return 0;
1228
Geert Uytterhoeven1c0c8462007-05-02 14:48:31 +02001229err_unregister_framebuffer:
1230 unregister_framebuffer(info);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001231err_fb_dealloc:
1232 fb_dealloc_cmap(&info->cmap);
1233err_framebuffer_release:
1234 framebuffer_release(info);
1235err_free_irq:
Geoff Levandfcbe6e92007-12-19 11:17:31 +01001236 free_irq(ps3fb.irq_no, &dev->core);
Geoff Levanddc4f60c2007-05-01 07:01:01 +10001237 ps3_irq_plug_destroy(ps3fb.irq_no);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001238err_iounmap_dinfo:
Geert Uytterhoevena2864082008-02-06 01:39:35 -08001239 iounmap((u8 __force __iomem *)ps3fb.dinfo);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001240err_gpu_context_free:
1241 lv1_gpu_context_free(ps3fb.context_handle);
1242err_gpu_memory_free:
1243 lv1_gpu_memory_free(ps3fb.memory_handle);
1244err:
1245 return retval;
1246}
1247
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +10001248static int ps3fb_shutdown(struct ps3_system_bus_device *dev)
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001249{
1250 int status;
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +10001251 struct fb_info *info = dev->core.driver_data;
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001252
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -07001253 dev_dbg(&dev->core, " -> %s:%d\n", __func__, __LINE__);
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +10001254
Geert Uytterhoeven9b82f3e2008-10-30 08:12:58 +00001255 atomic_inc(&ps3fb.ext_flip); /* flip off */
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +10001256 ps3fb.dinfo->irq.mask = 0;
1257
Geert Uytterhoeven1c0c8462007-05-02 14:48:31 +02001258 if (ps3fb.task) {
1259 struct task_struct *task = ps3fb.task;
1260 ps3fb.task = NULL;
1261 kthread_stop(task);
1262 }
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001263 if (ps3fb.irq_no) {
Geoff Levandfcbe6e92007-12-19 11:17:31 +01001264 free_irq(ps3fb.irq_no, &dev->core);
Geoff Levanddc4f60c2007-05-01 07:01:01 +10001265 ps3_irq_plug_destroy(ps3fb.irq_no);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001266 }
Jeremy Kerrba216112008-01-11 14:27:10 +01001267 if (info) {
1268 unregister_framebuffer(info);
1269 fb_dealloc_cmap(&info->cmap);
1270 framebuffer_release(info);
1271 info = dev->core.driver_data = NULL;
1272 }
Geert Uytterhoevena2864082008-02-06 01:39:35 -08001273 iounmap((u8 __force __iomem *)ps3fb.dinfo);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001274
1275 status = lv1_gpu_context_free(ps3fb.context_handle);
1276 if (status)
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -07001277 dev_dbg(&dev->core, "lv1_gpu_context_free failed: %d\n",
1278 status);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001279
1280 status = lv1_gpu_memory_free(ps3fb.memory_handle);
1281 if (status)
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -07001282 dev_dbg(&dev->core, "lv1_gpu_memory_free failed: %d\n",
1283 status);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001284
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +10001285 ps3_close_hv_device(dev);
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -07001286 dev_dbg(&dev->core, " <- %s:%d\n", __func__, __LINE__);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001287
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001288 return 0;
1289}
1290
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +10001291static struct ps3_system_bus_driver ps3fb_driver = {
Geert Uytterhoeven46d01492008-12-03 13:52:21 +00001292 .match_id = PS3_MATCH_ID_GPU,
1293 .match_sub_id = PS3_MATCH_SUB_ID_GPU_FB,
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +10001294 .core.name = DEVICE_NAME,
1295 .core.owner = THIS_MODULE,
1296 .probe = ps3fb_probe,
1297 .remove = ps3fb_shutdown,
1298 .shutdown = ps3fb_shutdown,
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001299};
1300
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +10001301static int __init ps3fb_setup(void)
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001302{
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +10001303 char *options;
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001304
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +10001305#ifdef MODULE
1306 return 0;
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001307#endif
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001308
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +10001309 if (fb_get_options(DEVICE_NAME, &options))
1310 return -ENXIO;
1311
1312 if (!options || !*options)
1313 return 0;
1314
1315 while (1) {
1316 char *this_opt = strsep(&options, ",");
1317
1318 if (!this_opt)
1319 break;
1320 if (!*this_opt)
1321 continue;
1322 if (!strncmp(this_opt, "mode:", 5))
1323 ps3fb_mode = simple_strtoul(this_opt + 5, NULL, 0);
1324 else
1325 mode_option = this_opt;
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001326 }
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001327 return 0;
1328}
1329
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001330static int __init ps3fb_init(void)
1331{
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +10001332 if (!ps3fb_videomemory.address || ps3fb_setup())
1333 return -ENXIO;
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001334
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +10001335 return ps3_system_bus_driver_register(&ps3fb_driver);
1336}
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001337
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +10001338static void __exit ps3fb_exit(void)
1339{
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -07001340 pr_debug(" -> %s:%d\n", __func__, __LINE__);
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +10001341 ps3_system_bus_driver_unregister(&ps3fb_driver);
Geert Uytterhoeven535da7f2007-10-16 01:29:45 -07001342 pr_debug(" <- %s:%d\n", __func__, __LINE__);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001343}
1344
1345module_init(ps3fb_init);
Geert Uytterhoeven310d8c12007-02-12 00:55:23 -08001346module_exit(ps3fb_exit);
1347
1348MODULE_LICENSE("GPL");
Geert Uytterhoeven9e6b99b2007-06-16 08:05:38 +10001349MODULE_DESCRIPTION("PS3 GPU Frame Buffer Driver");
1350MODULE_AUTHOR("Sony Computer Entertainment Inc.");
Geert Uytterhoeven46d01492008-12-03 13:52:21 +00001351MODULE_ALIAS(PS3_MODULE_ALIAS_GPU_FB);