ps3fb.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358
  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. status = lv1_gpu_context_attribute(ps3fb.context_handle,
  396. L1GPU_CONTEXT_ATTRIBUTE_FB_BLIT,
  397. dst_offset, GPU_IOIF + src_offset,
  398. L1GPU_FB_BLIT_WAIT_FOR_COMPLETION |
  399. (width << 16) | height,
  400. line_length);
  401. if (status)
  402. dev_err(dev,
  403. "%s: lv1_gpu_context_attribute FB_BLIT failed: %d\n",
  404. __func__, status);
  405. #ifdef HEAD_A
  406. status = lv1_gpu_context_attribute(ps3fb.context_handle,
  407. L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_FLIP,
  408. 0, frame_offset, 0, 0);
  409. if (status)
  410. dev_err(dev, "%s: lv1_gpu_context_attribute FLIP failed: %d\n",
  411. __func__, status);
  412. #endif
  413. #ifdef HEAD_B
  414. status = lv1_gpu_context_attribute(ps3fb.context_handle,
  415. L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_FLIP,
  416. 1, frame_offset, 0, 0);
  417. if (status)
  418. dev_err(dev, "%s: lv1_gpu_context_attribute FLIP failed: %d\n",
  419. __func__, status);
  420. #endif
  421. }
  422. static int ps3fb_sync(struct fb_info *info, u32 frame)
  423. {
  424. struct ps3fb_par *par = info->par;
  425. int error = 0;
  426. u64 ddr_base, xdr_base;
  427. if (frame > par->num_frames - 1) {
  428. dev_dbg(info->device, "%s: invalid frame number (%u)\n",
  429. __func__, frame);
  430. error = -EINVAL;
  431. goto out;
  432. }
  433. xdr_base = frame * par->xdr_frame_size;
  434. ddr_base = frame * par->ddr_frame_size;
  435. ps3fb_sync_image(info->device, ddr_base + par->full_offset,
  436. ddr_base + par->fb_offset, xdr_base + par->pan_offset,
  437. par->width, par->height, par->ddr_line_length,
  438. info->fix.line_length);
  439. out:
  440. return error;
  441. }
  442. static int ps3fb_open(struct fb_info *info, int user)
  443. {
  444. atomic_inc(&ps3fb.f_count);
  445. return 0;
  446. }
  447. static int ps3fb_release(struct fb_info *info, int user)
  448. {
  449. if (atomic_dec_and_test(&ps3fb.f_count)) {
  450. if (atomic_read(&ps3fb.ext_flip)) {
  451. atomic_set(&ps3fb.ext_flip, 0);
  452. if (!try_acquire_console_sem()) {
  453. ps3fb_sync(info, 0); /* single buffer */
  454. release_console_sem();
  455. }
  456. }
  457. }
  458. return 0;
  459. }
  460. /*
  461. * Setting the video mode has been split into two parts.
  462. * First part, xxxfb_check_var, must not write anything
  463. * to hardware, it should only verify and adjust var.
  464. * This means it doesn't alter par but it does use hardware
  465. * data from it to check this var.
  466. */
  467. static int ps3fb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
  468. {
  469. u32 xdr_line_length, ddr_line_length;
  470. int mode;
  471. mode = ps3fb_find_mode(var, &ddr_line_length, &xdr_line_length);
  472. if (!mode)
  473. return -EINVAL;
  474. /* Virtual screen */
  475. if (var->xres_virtual > xdr_line_length / BPP) {
  476. dev_dbg(info->device,
  477. "Horizontal virtual screen size too large\n");
  478. return -EINVAL;
  479. }
  480. if (var->xoffset + var->xres > var->xres_virtual ||
  481. var->yoffset + var->yres > var->yres_virtual) {
  482. dev_dbg(info->device, "panning out-of-range\n");
  483. return -EINVAL;
  484. }
  485. /* We support ARGB8888 only */
  486. if (var->bits_per_pixel > 32 || var->grayscale ||
  487. var->red.offset > 16 || var->green.offset > 8 ||
  488. var->blue.offset > 0 || var->transp.offset > 24 ||
  489. var->red.length > 8 || var->green.length > 8 ||
  490. var->blue.length > 8 || var->transp.length > 8 ||
  491. var->red.msb_right || var->green.msb_right ||
  492. var->blue.msb_right || var->transp.msb_right || var->nonstd) {
  493. dev_dbg(info->device, "We support ARGB8888 only\n");
  494. return -EINVAL;
  495. }
  496. var->bits_per_pixel = 32;
  497. var->red.offset = 16;
  498. var->green.offset = 8;
  499. var->blue.offset = 0;
  500. var->transp.offset = 24;
  501. var->red.length = 8;
  502. var->green.length = 8;
  503. var->blue.length = 8;
  504. var->transp.length = 8;
  505. var->red.msb_right = 0;
  506. var->green.msb_right = 0;
  507. var->blue.msb_right = 0;
  508. var->transp.msb_right = 0;
  509. /* Rotation is not supported */
  510. if (var->rotate) {
  511. dev_dbg(info->device, "Rotation is not supported\n");
  512. return -EINVAL;
  513. }
  514. /* Memory limit */
  515. if (var->yres_virtual * xdr_line_length > info->fix.smem_len) {
  516. dev_dbg(info->device, "Not enough memory\n");
  517. return -ENOMEM;
  518. }
  519. var->height = -1;
  520. var->width = -1;
  521. return 0;
  522. }
  523. /*
  524. * This routine actually sets the video mode.
  525. */
  526. static int ps3fb_set_par(struct fb_info *info)
  527. {
  528. struct ps3fb_par *par = info->par;
  529. unsigned int mode, ddr_line_length, xdr_line_length, lines, maxlines;
  530. unsigned int ddr_xoff, ddr_yoff, offset;
  531. const struct fb_videomode *vmode;
  532. u64 dst;
  533. mode = ps3fb_find_mode(&info->var, &ddr_line_length, &xdr_line_length);
  534. if (!mode)
  535. return -EINVAL;
  536. vmode = ps3fb_native_vmode(mode & PS3AV_MODE_MASK);
  537. info->fix.xpanstep = info->var.xres_virtual > info->var.xres ? 1 : 0;
  538. info->fix.ypanstep = info->var.yres_virtual > info->var.yres ? 1 : 0;
  539. info->fix.line_length = xdr_line_length;
  540. par->ddr_line_length = ddr_line_length;
  541. par->ddr_frame_size = vmode->yres * ddr_line_length;
  542. par->xdr_frame_size = info->var.yres_virtual * xdr_line_length;
  543. par->num_frames = info->fix.smem_len /
  544. max(par->ddr_frame_size, par->xdr_frame_size);
  545. /* Keep the special bits we cannot set using fb_var_screeninfo */
  546. par->new_mode_id = (par->new_mode_id & ~PS3AV_MODE_MASK) | mode;
  547. par->width = info->var.xres;
  548. par->height = info->var.yres;
  549. /* Start of the virtual frame buffer (relative to fullscreen) */
  550. ddr_xoff = info->var.left_margin - vmode->left_margin;
  551. ddr_yoff = info->var.upper_margin - vmode->upper_margin;
  552. offset = ddr_yoff * ddr_line_length + ddr_xoff * BPP;
  553. par->fb_offset = GPU_ALIGN_UP(offset);
  554. par->full_offset = par->fb_offset - offset;
  555. par->pan_offset = info->var.yoffset * xdr_line_length +
  556. info->var.xoffset * BPP;
  557. if (par->new_mode_id != par->mode_id) {
  558. if (ps3av_set_video_mode(par->new_mode_id)) {
  559. par->new_mode_id = par->mode_id;
  560. return -EINVAL;
  561. }
  562. par->mode_id = par->new_mode_id;
  563. }
  564. /* Clear XDR frame buffer memory */
  565. memset((void __force *)info->screen_base, 0, info->fix.smem_len);
  566. /* Clear DDR frame buffer memory */
  567. lines = vmode->yres * par->num_frames;
  568. if (par->full_offset)
  569. lines++;
  570. maxlines = info->fix.smem_len / ddr_line_length;
  571. for (dst = 0; lines; dst += maxlines * ddr_line_length) {
  572. unsigned int l = min(lines, maxlines);
  573. ps3fb_sync_image(info->device, 0, dst, 0, vmode->xres, l,
  574. ddr_line_length, ddr_line_length);
  575. lines -= l;
  576. }
  577. return 0;
  578. }
  579. /*
  580. * Set a single color register. The values supplied are already
  581. * rounded down to the hardware's capabilities (according to the
  582. * entries in the var structure). Return != 0 for invalid regno.
  583. */
  584. static int ps3fb_setcolreg(unsigned int regno, unsigned int red,
  585. unsigned int green, unsigned int blue,
  586. unsigned int transp, struct fb_info *info)
  587. {
  588. if (regno >= 16)
  589. return 1;
  590. red >>= 8;
  591. green >>= 8;
  592. blue >>= 8;
  593. transp >>= 8;
  594. ((u32 *)info->pseudo_palette)[regno] = transp << 24 | red << 16 |
  595. green << 8 | blue;
  596. return 0;
  597. }
  598. static int ps3fb_pan_display(struct fb_var_screeninfo *var,
  599. struct fb_info *info)
  600. {
  601. struct ps3fb_par *par = info->par;
  602. par->pan_offset = var->yoffset * info->fix.line_length +
  603. var->xoffset * BPP;
  604. return 0;
  605. }
  606. /*
  607. * As we have a virtual frame buffer, we need our own mmap function
  608. */
  609. static int ps3fb_mmap(struct fb_info *info, struct vm_area_struct *vma)
  610. {
  611. unsigned long size, offset;
  612. size = vma->vm_end - vma->vm_start;
  613. offset = vma->vm_pgoff << PAGE_SHIFT;
  614. if (offset + size > info->fix.smem_len)
  615. return -EINVAL;
  616. offset += info->fix.smem_start;
  617. if (remap_pfn_range(vma, vma->vm_start, offset >> PAGE_SHIFT,
  618. size, vma->vm_page_prot))
  619. return -EAGAIN;
  620. dev_dbg(info->device, "ps3fb: mmap framebuffer P(%lx)->V(%lx)\n",
  621. offset, vma->vm_start);
  622. return 0;
  623. }
  624. /*
  625. * Blank the display
  626. */
  627. static int ps3fb_blank(int blank, struct fb_info *info)
  628. {
  629. int retval;
  630. dev_dbg(info->device, "%s: blank:%d\n", __func__, blank);
  631. switch (blank) {
  632. case FB_BLANK_POWERDOWN:
  633. case FB_BLANK_HSYNC_SUSPEND:
  634. case FB_BLANK_VSYNC_SUSPEND:
  635. case FB_BLANK_NORMAL:
  636. retval = ps3av_video_mute(1); /* mute on */
  637. if (!retval)
  638. ps3fb.is_blanked = 1;
  639. break;
  640. default: /* unblank */
  641. retval = ps3av_video_mute(0); /* mute off */
  642. if (!retval)
  643. ps3fb.is_blanked = 0;
  644. break;
  645. }
  646. return retval;
  647. }
  648. static int ps3fb_get_vblank(struct fb_vblank *vblank)
  649. {
  650. memset(vblank, 0, sizeof(*vblank));
  651. vblank->flags = FB_VBLANK_HAVE_VSYNC;
  652. return 0;
  653. }
  654. static int ps3fb_wait_for_vsync(u32 crtc)
  655. {
  656. int ret;
  657. u64 count;
  658. count = ps3fb.vblank_count;
  659. ret = wait_event_interruptible_timeout(ps3fb.wait_vsync,
  660. count != ps3fb.vblank_count,
  661. HZ / 10);
  662. if (!ret)
  663. return -ETIMEDOUT;
  664. return 0;
  665. }
  666. static void ps3fb_flip_ctl(int on, void *data)
  667. {
  668. struct ps3fb_priv *priv = data;
  669. if (on)
  670. atomic_dec_if_positive(&priv->ext_flip);
  671. else
  672. atomic_inc(&priv->ext_flip);
  673. }
  674. /*
  675. * ioctl
  676. */
  677. static int ps3fb_ioctl(struct fb_info *info, unsigned int cmd,
  678. unsigned long arg)
  679. {
  680. void __user *argp = (void __user *)arg;
  681. u32 val;
  682. int retval = -EFAULT;
  683. switch (cmd) {
  684. case FBIOGET_VBLANK:
  685. {
  686. struct fb_vblank vblank;
  687. dev_dbg(info->device, "FBIOGET_VBLANK:\n");
  688. retval = ps3fb_get_vblank(&vblank);
  689. if (retval)
  690. break;
  691. if (copy_to_user(argp, &vblank, sizeof(vblank)))
  692. retval = -EFAULT;
  693. break;
  694. }
  695. case FBIO_WAITFORVSYNC:
  696. {
  697. u32 crt;
  698. dev_dbg(info->device, "FBIO_WAITFORVSYNC:\n");
  699. if (get_user(crt, (u32 __user *) arg))
  700. break;
  701. retval = ps3fb_wait_for_vsync(crt);
  702. break;
  703. }
  704. case PS3FB_IOCTL_SETMODE:
  705. {
  706. struct ps3fb_par *par = info->par;
  707. const struct fb_videomode *vmode;
  708. struct fb_var_screeninfo var;
  709. if (copy_from_user(&val, argp, sizeof(val)))
  710. break;
  711. if (!(val & PS3AV_MODE_MASK)) {
  712. u32 id = ps3av_get_auto_mode();
  713. if (id > 0)
  714. val = (val & ~PS3AV_MODE_MASK) | id;
  715. }
  716. dev_dbg(info->device, "PS3FB_IOCTL_SETMODE:%x\n", val);
  717. retval = -EINVAL;
  718. vmode = ps3fb_vmode(val);
  719. if (vmode) {
  720. var = info->var;
  721. fb_videomode_to_var(&var, vmode);
  722. acquire_console_sem();
  723. info->flags |= FBINFO_MISC_USEREVENT;
  724. /* Force, in case only special bits changed */
  725. var.activate |= FB_ACTIVATE_FORCE;
  726. par->new_mode_id = val;
  727. retval = fb_set_var(info, &var);
  728. info->flags &= ~FBINFO_MISC_USEREVENT;
  729. release_console_sem();
  730. }
  731. break;
  732. }
  733. case PS3FB_IOCTL_GETMODE:
  734. val = ps3av_get_mode();
  735. dev_dbg(info->device, "PS3FB_IOCTL_GETMODE:%x\n", val);
  736. if (!copy_to_user(argp, &val, sizeof(val)))
  737. retval = 0;
  738. break;
  739. case PS3FB_IOCTL_SCREENINFO:
  740. {
  741. struct ps3fb_par *par = info->par;
  742. struct ps3fb_ioctl_res res;
  743. dev_dbg(info->device, "PS3FB_IOCTL_SCREENINFO:\n");
  744. res.xres = info->fix.line_length / BPP;
  745. res.yres = info->var.yres_virtual;
  746. res.xoff = (res.xres - info->var.xres) / 2;
  747. res.yoff = (res.yres - info->var.yres) / 2;
  748. res.num_frames = par->num_frames;
  749. if (!copy_to_user(argp, &res, sizeof(res)))
  750. retval = 0;
  751. break;
  752. }
  753. case PS3FB_IOCTL_ON:
  754. dev_dbg(info->device, "PS3FB_IOCTL_ON:\n");
  755. atomic_inc(&ps3fb.ext_flip);
  756. retval = 0;
  757. break;
  758. case PS3FB_IOCTL_OFF:
  759. dev_dbg(info->device, "PS3FB_IOCTL_OFF:\n");
  760. atomic_dec_if_positive(&ps3fb.ext_flip);
  761. retval = 0;
  762. break;
  763. case PS3FB_IOCTL_FSEL:
  764. if (copy_from_user(&val, argp, sizeof(val)))
  765. break;
  766. dev_dbg(info->device, "PS3FB_IOCTL_FSEL:%d\n", val);
  767. acquire_console_sem();
  768. retval = ps3fb_sync(info, val);
  769. release_console_sem();
  770. break;
  771. default:
  772. retval = -ENOIOCTLCMD;
  773. break;
  774. }
  775. return retval;
  776. }
  777. static int ps3fbd(void *arg)
  778. {
  779. struct fb_info *info = arg;
  780. set_freezable();
  781. while (!kthread_should_stop()) {
  782. try_to_freeze();
  783. set_current_state(TASK_INTERRUPTIBLE);
  784. if (ps3fb.is_kicked) {
  785. ps3fb.is_kicked = 0;
  786. acquire_console_sem();
  787. ps3fb_sync(info, 0); /* single buffer */
  788. release_console_sem();
  789. }
  790. schedule();
  791. }
  792. return 0;
  793. }
  794. static irqreturn_t ps3fb_vsync_interrupt(int irq, void *ptr)
  795. {
  796. struct device *dev = ptr;
  797. u64 v1;
  798. int status;
  799. struct display_head *head = &ps3fb.dinfo->display_head[1];
  800. status = lv1_gpu_context_intr(ps3fb.context_handle, &v1);
  801. if (status) {
  802. dev_err(dev, "%s: lv1_gpu_context_intr failed: %d\n", __func__,
  803. status);
  804. return IRQ_NONE;
  805. }
  806. if (v1 & (1 << GPU_INTR_STATUS_VSYNC_1)) {
  807. /* VSYNC */
  808. ps3fb.vblank_count = head->vblank_count;
  809. if (ps3fb.task && !ps3fb.is_blanked &&
  810. !atomic_read(&ps3fb.ext_flip)) {
  811. ps3fb.is_kicked = 1;
  812. wake_up_process(ps3fb.task);
  813. }
  814. wake_up_interruptible(&ps3fb.wait_vsync);
  815. }
  816. return IRQ_HANDLED;
  817. }
  818. static int ps3fb_vsync_settings(struct gpu_driver_info *dinfo,
  819. struct device *dev)
  820. {
  821. int error;
  822. dev_dbg(dev, "version_driver:%x\n", dinfo->version_driver);
  823. dev_dbg(dev, "irq outlet:%x\n", dinfo->irq.irq_outlet);
  824. dev_dbg(dev,
  825. "version_gpu: %x memory_size: %x ch: %x core_freq: %d "
  826. "mem_freq:%d\n",
  827. dinfo->version_gpu, dinfo->memory_size, dinfo->hardware_channel,
  828. dinfo->nvcore_frequency/1000000, dinfo->memory_frequency/1000000);
  829. if (dinfo->version_driver != GPU_DRIVER_INFO_VERSION) {
  830. dev_err(dev, "%s: version_driver err:%x\n", __func__,
  831. dinfo->version_driver);
  832. return -EINVAL;
  833. }
  834. error = ps3_irq_plug_setup(PS3_BINDING_CPU_ANY, dinfo->irq.irq_outlet,
  835. &ps3fb.irq_no);
  836. if (error) {
  837. dev_err(dev, "%s: ps3_alloc_irq failed %d\n", __func__, error);
  838. return error;
  839. }
  840. error = request_irq(ps3fb.irq_no, ps3fb_vsync_interrupt, IRQF_DISABLED,
  841. DEVICE_NAME, dev);
  842. if (error) {
  843. dev_err(dev, "%s: request_irq failed %d\n", __func__, error);
  844. ps3_irq_plug_destroy(ps3fb.irq_no);
  845. return error;
  846. }
  847. dinfo->irq.mask = (1 << GPU_INTR_STATUS_VSYNC_1) |
  848. (1 << GPU_INTR_STATUS_FLIP_1);
  849. return 0;
  850. }
  851. static int ps3fb_xdr_settings(u64 xdr_lpar, struct device *dev)
  852. {
  853. int status;
  854. status = lv1_gpu_context_iomap(ps3fb.context_handle, GPU_IOIF,
  855. xdr_lpar, ps3fb_videomemory.size, 0);
  856. if (status) {
  857. dev_err(dev, "%s: lv1_gpu_context_iomap failed: %d\n",
  858. __func__, status);
  859. return -ENXIO;
  860. }
  861. dev_dbg(dev, "video:%p ioif:%lx lpar:%lx size:%lx\n",
  862. ps3fb_videomemory.address, GPU_IOIF, xdr_lpar,
  863. ps3fb_videomemory.size);
  864. status = lv1_gpu_context_attribute(ps3fb.context_handle,
  865. L1GPU_CONTEXT_ATTRIBUTE_FB_SETUP,
  866. xdr_lpar, GPU_CMD_BUF_SIZE,
  867. GPU_IOIF, 0);
  868. if (status) {
  869. dev_err(dev,
  870. "%s: lv1_gpu_context_attribute FB_SETUP failed: %d\n",
  871. __func__, status);
  872. return -ENXIO;
  873. }
  874. return 0;
  875. }
  876. static struct fb_ops ps3fb_ops = {
  877. .fb_open = ps3fb_open,
  878. .fb_release = ps3fb_release,
  879. .fb_read = fb_sys_read,
  880. .fb_write = fb_sys_write,
  881. .fb_check_var = ps3fb_check_var,
  882. .fb_set_par = ps3fb_set_par,
  883. .fb_setcolreg = ps3fb_setcolreg,
  884. .fb_pan_display = ps3fb_pan_display,
  885. .fb_fillrect = sys_fillrect,
  886. .fb_copyarea = sys_copyarea,
  887. .fb_imageblit = sys_imageblit,
  888. .fb_mmap = ps3fb_mmap,
  889. .fb_blank = ps3fb_blank,
  890. .fb_ioctl = ps3fb_ioctl,
  891. .fb_compat_ioctl = ps3fb_ioctl
  892. };
  893. static struct fb_fix_screeninfo ps3fb_fix __initdata = {
  894. .id = DEVICE_NAME,
  895. .type = FB_TYPE_PACKED_PIXELS,
  896. .visual = FB_VISUAL_TRUECOLOR,
  897. .accel = FB_ACCEL_NONE,
  898. };
  899. static int ps3fb_set_sync(struct device *dev)
  900. {
  901. int status;
  902. #ifdef HEAD_A
  903. status = lv1_gpu_context_attribute(0x0,
  904. L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_SYNC,
  905. 0, L1GPU_DISPLAY_SYNC_VSYNC, 0, 0);
  906. if (status) {
  907. dev_err(dev,
  908. "%s: lv1_gpu_context_attribute DISPLAY_SYNC failed: "
  909. "%d\n",
  910. __func__, status);
  911. return -1;
  912. }
  913. #endif
  914. #ifdef HEAD_B
  915. status = lv1_gpu_context_attribute(0x0,
  916. L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_SYNC,
  917. 1, L1GPU_DISPLAY_SYNC_VSYNC, 0, 0);
  918. if (status) {
  919. dev_err(dev,
  920. "%s: lv1_gpu_context_attribute DISPLAY_SYNC failed: "
  921. "%d\n",
  922. __func__, status);
  923. return -1;
  924. }
  925. #endif
  926. return 0;
  927. }
  928. static int __devinit ps3fb_probe(struct ps3_system_bus_device *dev)
  929. {
  930. struct fb_info *info;
  931. struct ps3fb_par *par;
  932. int retval = -ENOMEM;
  933. u64 ddr_lpar = 0;
  934. u64 lpar_dma_control = 0;
  935. u64 lpar_driver_info = 0;
  936. u64 lpar_reports = 0;
  937. u64 lpar_reports_size = 0;
  938. u64 xdr_lpar;
  939. void *fb_start;
  940. int status;
  941. struct task_struct *task;
  942. unsigned long max_ps3fb_size;
  943. if (ps3fb_videomemory.size < GPU_CMD_BUF_SIZE) {
  944. dev_err(&dev->core, "%s: Not enough video memory\n", __func__);
  945. return -ENOMEM;
  946. }
  947. status = ps3_open_hv_device(dev);
  948. if (status) {
  949. dev_err(&dev->core, "%s: ps3_open_hv_device failed\n",
  950. __func__);
  951. goto err;
  952. }
  953. if (!ps3fb_mode)
  954. ps3fb_mode = ps3av_get_mode();
  955. dev_dbg(&dev->core, "ps3fb_mode: %d\n", ps3fb_mode);
  956. atomic_set(&ps3fb.f_count, -1); /* fbcon opens ps3fb */
  957. atomic_set(&ps3fb.ext_flip, 0); /* for flip with vsync */
  958. init_waitqueue_head(&ps3fb.wait_vsync);
  959. ps3fb_set_sync(&dev->core);
  960. max_ps3fb_size = _ALIGN_UP(GPU_IOIF, 256*1024*1024) - GPU_IOIF;
  961. if (ps3fb_videomemory.size > max_ps3fb_size) {
  962. dev_info(&dev->core, "Limiting ps3fb mem size to %lu bytes\n",
  963. max_ps3fb_size);
  964. ps3fb_videomemory.size = max_ps3fb_size;
  965. }
  966. /* get gpu context handle */
  967. status = lv1_gpu_memory_allocate(ps3fb_videomemory.size, 0, 0, 0, 0,
  968. &ps3fb.memory_handle, &ddr_lpar);
  969. if (status) {
  970. dev_err(&dev->core, "%s: lv1_gpu_memory_allocate failed: %d\n",
  971. __func__, status);
  972. goto err;
  973. }
  974. dev_dbg(&dev->core, "ddr:lpar:0x%lx\n", ddr_lpar);
  975. status = lv1_gpu_context_allocate(ps3fb.memory_handle, 0,
  976. &ps3fb.context_handle,
  977. &lpar_dma_control, &lpar_driver_info,
  978. &lpar_reports, &lpar_reports_size);
  979. if (status) {
  980. dev_err(&dev->core,
  981. "%s: lv1_gpu_context_attribute failed: %d\n", __func__,
  982. status);
  983. goto err_gpu_memory_free;
  984. }
  985. /* vsync interrupt */
  986. ps3fb.dinfo = (void __force *)ioremap(lpar_driver_info, 128 * 1024);
  987. if (!ps3fb.dinfo) {
  988. dev_err(&dev->core, "%s: ioremap failed\n", __func__);
  989. goto err_gpu_context_free;
  990. }
  991. retval = ps3fb_vsync_settings(ps3fb.dinfo, &dev->core);
  992. if (retval)
  993. goto err_iounmap_dinfo;
  994. /* Clear memory to prevent kernel info leakage into userspace */
  995. memset(ps3fb_videomemory.address, 0, ps3fb_videomemory.size);
  996. xdr_lpar = ps3_mm_phys_to_lpar(__pa(ps3fb_videomemory.address));
  997. retval = ps3fb_xdr_settings(xdr_lpar, &dev->core);
  998. if (retval)
  999. goto err_free_irq;
  1000. info = framebuffer_alloc(sizeof(struct ps3fb_par), &dev->core);
  1001. if (!info)
  1002. goto err_free_irq;
  1003. par = info->par;
  1004. par->mode_id = ~ps3fb_mode; /* != ps3fb_mode, to trigger change */
  1005. par->new_mode_id = ps3fb_mode;
  1006. par->num_frames = 1;
  1007. info->fbops = &ps3fb_ops;
  1008. info->fix = ps3fb_fix;
  1009. /*
  1010. * The GPU command buffer is at the start of video memory
  1011. * As we don't use the full command buffer, we can put the actual
  1012. * frame buffer at offset GPU_FB_START and save some precious XDR
  1013. * memory
  1014. */
  1015. fb_start = ps3fb_videomemory.address + GPU_FB_START;
  1016. info->screen_base = (char __force __iomem *)fb_start;
  1017. info->fix.smem_start = virt_to_abs(fb_start);
  1018. info->fix.smem_len = ps3fb_videomemory.size - GPU_FB_START;
  1019. info->pseudo_palette = par->pseudo_palette;
  1020. info->flags = FBINFO_DEFAULT | FBINFO_READS_FAST |
  1021. FBINFO_HWACCEL_XPAN | FBINFO_HWACCEL_YPAN;
  1022. retval = fb_alloc_cmap(&info->cmap, 256, 0);
  1023. if (retval < 0)
  1024. goto err_framebuffer_release;
  1025. if (!fb_find_mode(&info->var, info, mode_option, ps3fb_modedb,
  1026. ARRAY_SIZE(ps3fb_modedb),
  1027. ps3fb_vmode(par->new_mode_id), 32)) {
  1028. retval = -EINVAL;
  1029. goto err_fb_dealloc;
  1030. }
  1031. fb_videomode_to_modelist(ps3fb_modedb, ARRAY_SIZE(ps3fb_modedb),
  1032. &info->modelist);
  1033. retval = register_framebuffer(info);
  1034. if (retval < 0)
  1035. goto err_fb_dealloc;
  1036. dev->core.driver_data = info;
  1037. dev_info(info->device, "%s %s, using %u KiB of video memory\n",
  1038. dev_driver_string(info->dev), info->dev->bus_id,
  1039. info->fix.smem_len >> 10);
  1040. task = kthread_run(ps3fbd, info, DEVICE_NAME);
  1041. if (IS_ERR(task)) {
  1042. retval = PTR_ERR(task);
  1043. goto err_unregister_framebuffer;
  1044. }
  1045. ps3fb.task = task;
  1046. ps3av_register_flip_ctl(ps3fb_flip_ctl, &ps3fb);
  1047. return 0;
  1048. err_unregister_framebuffer:
  1049. unregister_framebuffer(info);
  1050. err_fb_dealloc:
  1051. fb_dealloc_cmap(&info->cmap);
  1052. err_framebuffer_release:
  1053. framebuffer_release(info);
  1054. err_free_irq:
  1055. free_irq(ps3fb.irq_no, &dev->core);
  1056. ps3_irq_plug_destroy(ps3fb.irq_no);
  1057. err_iounmap_dinfo:
  1058. iounmap((u8 __force __iomem *)ps3fb.dinfo);
  1059. err_gpu_context_free:
  1060. lv1_gpu_context_free(ps3fb.context_handle);
  1061. err_gpu_memory_free:
  1062. lv1_gpu_memory_free(ps3fb.memory_handle);
  1063. err:
  1064. return retval;
  1065. }
  1066. static int ps3fb_shutdown(struct ps3_system_bus_device *dev)
  1067. {
  1068. int status;
  1069. struct fb_info *info = dev->core.driver_data;
  1070. dev_dbg(&dev->core, " -> %s:%d\n", __func__, __LINE__);
  1071. ps3fb_flip_ctl(0, &ps3fb); /* flip off */
  1072. ps3fb.dinfo->irq.mask = 0;
  1073. ps3av_register_flip_ctl(NULL, NULL);
  1074. if (ps3fb.task) {
  1075. struct task_struct *task = ps3fb.task;
  1076. ps3fb.task = NULL;
  1077. kthread_stop(task);
  1078. }
  1079. if (ps3fb.irq_no) {
  1080. free_irq(ps3fb.irq_no, &dev->core);
  1081. ps3_irq_plug_destroy(ps3fb.irq_no);
  1082. }
  1083. if (info) {
  1084. unregister_framebuffer(info);
  1085. fb_dealloc_cmap(&info->cmap);
  1086. framebuffer_release(info);
  1087. info = dev->core.driver_data = NULL;
  1088. }
  1089. iounmap((u8 __force __iomem *)ps3fb.dinfo);
  1090. status = lv1_gpu_context_free(ps3fb.context_handle);
  1091. if (status)
  1092. dev_dbg(&dev->core, "lv1_gpu_context_free failed: %d\n",
  1093. status);
  1094. status = lv1_gpu_memory_free(ps3fb.memory_handle);
  1095. if (status)
  1096. dev_dbg(&dev->core, "lv1_gpu_memory_free failed: %d\n",
  1097. status);
  1098. ps3_close_hv_device(dev);
  1099. dev_dbg(&dev->core, " <- %s:%d\n", __func__, __LINE__);
  1100. return 0;
  1101. }
  1102. static struct ps3_system_bus_driver ps3fb_driver = {
  1103. .match_id = PS3_MATCH_ID_GRAPHICS,
  1104. .match_sub_id = PS3_MATCH_SUB_ID_FB,
  1105. .core.name = DEVICE_NAME,
  1106. .core.owner = THIS_MODULE,
  1107. .probe = ps3fb_probe,
  1108. .remove = ps3fb_shutdown,
  1109. .shutdown = ps3fb_shutdown,
  1110. };
  1111. static int __init ps3fb_setup(void)
  1112. {
  1113. char *options;
  1114. #ifdef MODULE
  1115. return 0;
  1116. #endif
  1117. if (fb_get_options(DEVICE_NAME, &options))
  1118. return -ENXIO;
  1119. if (!options || !*options)
  1120. return 0;
  1121. while (1) {
  1122. char *this_opt = strsep(&options, ",");
  1123. if (!this_opt)
  1124. break;
  1125. if (!*this_opt)
  1126. continue;
  1127. if (!strncmp(this_opt, "mode:", 5))
  1128. ps3fb_mode = simple_strtoul(this_opt + 5, NULL, 0);
  1129. else
  1130. mode_option = this_opt;
  1131. }
  1132. return 0;
  1133. }
  1134. static int __init ps3fb_init(void)
  1135. {
  1136. if (!ps3fb_videomemory.address || ps3fb_setup())
  1137. return -ENXIO;
  1138. return ps3_system_bus_driver_register(&ps3fb_driver);
  1139. }
  1140. static void __exit ps3fb_exit(void)
  1141. {
  1142. pr_debug(" -> %s:%d\n", __func__, __LINE__);
  1143. ps3_system_bus_driver_unregister(&ps3fb_driver);
  1144. pr_debug(" <- %s:%d\n", __func__, __LINE__);
  1145. }
  1146. module_init(ps3fb_init);
  1147. module_exit(ps3fb_exit);
  1148. MODULE_LICENSE("GPL");
  1149. MODULE_DESCRIPTION("PS3 GPU Frame Buffer Driver");
  1150. MODULE_AUTHOR("Sony Computer Entertainment Inc.");
  1151. MODULE_ALIAS(PS3_MODULE_ALIAS_GRAPHICS);