ps3fb.c 34 KB

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