drm_fb_helper.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574
  1. /*
  2. * Copyright (c) 2006-2009 Red Hat Inc.
  3. * Copyright (c) 2006-2008 Intel Corporation
  4. * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
  5. *
  6. * DRM framebuffer helper functions
  7. *
  8. * Permission to use, copy, modify, distribute, and sell this software and its
  9. * documentation for any purpose is hereby granted without fee, provided that
  10. * the above copyright notice appear in all copies and that both that copyright
  11. * notice and this permission notice appear in supporting documentation, and
  12. * that the name of the copyright holders not be used in advertising or
  13. * publicity pertaining to distribution of the software without specific,
  14. * written prior permission. The copyright holders make no representations
  15. * about the suitability of this software for any purpose. It is provided "as
  16. * is" without express or implied warranty.
  17. *
  18. * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  19. * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  20. * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  21. * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  22. * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  23. * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  24. * OF THIS SOFTWARE.
  25. *
  26. * Authors:
  27. * Dave Airlie <airlied@linux.ie>
  28. * Jesse Barnes <jesse.barnes@intel.com>
  29. */
  30. #include <linux/kernel.h>
  31. #include <linux/sysrq.h>
  32. #include <linux/slab.h>
  33. #include <linux/fb.h>
  34. #include "drmP.h"
  35. #include "drm_crtc.h"
  36. #include "drm_fb_helper.h"
  37. #include "drm_crtc_helper.h"
  38. MODULE_AUTHOR("David Airlie, Jesse Barnes");
  39. MODULE_DESCRIPTION("DRM KMS helper");
  40. MODULE_LICENSE("GPL and additional rights");
  41. static LIST_HEAD(kernel_fb_helper_list);
  42. /* simple single crtc case helper function */
  43. int drm_fb_helper_single_add_all_connectors(struct drm_fb_helper *fb_helper)
  44. {
  45. struct drm_device *dev = fb_helper->dev;
  46. struct drm_connector *connector;
  47. int i;
  48. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  49. struct drm_fb_helper_connector *fb_helper_connector;
  50. fb_helper_connector = kzalloc(sizeof(struct drm_fb_helper_connector), GFP_KERNEL);
  51. if (!fb_helper_connector)
  52. goto fail;
  53. fb_helper_connector->connector = connector;
  54. fb_helper->connector_info[fb_helper->connector_count++] = fb_helper_connector;
  55. }
  56. return 0;
  57. fail:
  58. for (i = 0; i < fb_helper->connector_count; i++) {
  59. kfree(fb_helper->connector_info[i]);
  60. fb_helper->connector_info[i] = NULL;
  61. }
  62. fb_helper->connector_count = 0;
  63. return -ENOMEM;
  64. }
  65. EXPORT_SYMBOL(drm_fb_helper_single_add_all_connectors);
  66. /**
  67. * drm_fb_helper_connector_parse_command_line - parse command line for connector
  68. * @connector - connector to parse line for
  69. * @mode_option - per connector mode option
  70. *
  71. * This parses the connector specific then generic command lines for
  72. * modes and options to configure the connector.
  73. *
  74. * This uses the same parameters as the fb modedb.c, except for extra
  75. * <xres>x<yres>[M][R][-<bpp>][@<refresh>][i][m][eDd]
  76. *
  77. * enable/enable Digital/disable bit at the end
  78. */
  79. static bool drm_fb_helper_connector_parse_command_line(struct drm_fb_helper_connector *fb_helper_conn,
  80. const char *mode_option)
  81. {
  82. const char *name;
  83. unsigned int namelen;
  84. int res_specified = 0, bpp_specified = 0, refresh_specified = 0;
  85. unsigned int xres = 0, yres = 0, bpp = 32, refresh = 0;
  86. int yres_specified = 0, cvt = 0, rb = 0, interlace = 0, margins = 0;
  87. int i;
  88. enum drm_connector_force force = DRM_FORCE_UNSPECIFIED;
  89. struct drm_fb_helper_cmdline_mode *cmdline_mode;
  90. struct drm_connector *connector;
  91. if (!fb_helper_conn)
  92. return false;
  93. connector = fb_helper_conn->connector;
  94. cmdline_mode = &fb_helper_conn->cmdline_mode;
  95. if (!mode_option)
  96. mode_option = fb_mode_option;
  97. if (!mode_option) {
  98. cmdline_mode->specified = false;
  99. return false;
  100. }
  101. name = mode_option;
  102. namelen = strlen(name);
  103. for (i = namelen-1; i >= 0; i--) {
  104. switch (name[i]) {
  105. case '@':
  106. namelen = i;
  107. if (!refresh_specified && !bpp_specified &&
  108. !yres_specified) {
  109. refresh = simple_strtol(&name[i+1], NULL, 10);
  110. refresh_specified = 1;
  111. if (cvt || rb)
  112. cvt = 0;
  113. } else
  114. goto done;
  115. break;
  116. case '-':
  117. namelen = i;
  118. if (!bpp_specified && !yres_specified) {
  119. bpp = simple_strtol(&name[i+1], NULL, 10);
  120. bpp_specified = 1;
  121. if (cvt || rb)
  122. cvt = 0;
  123. } else
  124. goto done;
  125. break;
  126. case 'x':
  127. if (!yres_specified) {
  128. yres = simple_strtol(&name[i+1], NULL, 10);
  129. yres_specified = 1;
  130. } else
  131. goto done;
  132. case '0' ... '9':
  133. break;
  134. case 'M':
  135. if (!yres_specified)
  136. cvt = 1;
  137. break;
  138. case 'R':
  139. if (cvt)
  140. rb = 1;
  141. break;
  142. case 'm':
  143. if (!cvt)
  144. margins = 1;
  145. break;
  146. case 'i':
  147. if (!cvt)
  148. interlace = 1;
  149. break;
  150. case 'e':
  151. force = DRM_FORCE_ON;
  152. break;
  153. case 'D':
  154. if ((connector->connector_type != DRM_MODE_CONNECTOR_DVII) &&
  155. (connector->connector_type != DRM_MODE_CONNECTOR_HDMIB))
  156. force = DRM_FORCE_ON;
  157. else
  158. force = DRM_FORCE_ON_DIGITAL;
  159. break;
  160. case 'd':
  161. force = DRM_FORCE_OFF;
  162. break;
  163. default:
  164. goto done;
  165. }
  166. }
  167. if (i < 0 && yres_specified) {
  168. xres = simple_strtol(name, NULL, 10);
  169. res_specified = 1;
  170. }
  171. done:
  172. DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
  173. drm_get_connector_name(connector), xres, yres,
  174. (refresh) ? refresh : 60, (rb) ? " reduced blanking" :
  175. "", (margins) ? " with margins" : "", (interlace) ?
  176. " interlaced" : "");
  177. if (force) {
  178. const char *s;
  179. switch (force) {
  180. case DRM_FORCE_OFF: s = "OFF"; break;
  181. case DRM_FORCE_ON_DIGITAL: s = "ON - dig"; break;
  182. default:
  183. case DRM_FORCE_ON: s = "ON"; break;
  184. }
  185. DRM_INFO("forcing %s connector %s\n",
  186. drm_get_connector_name(connector), s);
  187. connector->force = force;
  188. }
  189. if (res_specified) {
  190. cmdline_mode->specified = true;
  191. cmdline_mode->xres = xres;
  192. cmdline_mode->yres = yres;
  193. }
  194. if (refresh_specified) {
  195. cmdline_mode->refresh_specified = true;
  196. cmdline_mode->refresh = refresh;
  197. }
  198. if (bpp_specified) {
  199. cmdline_mode->bpp_specified = true;
  200. cmdline_mode->bpp = bpp;
  201. }
  202. cmdline_mode->rb = rb ? true : false;
  203. cmdline_mode->cvt = cvt ? true : false;
  204. cmdline_mode->interlace = interlace ? true : false;
  205. return true;
  206. }
  207. static int drm_fb_helper_parse_command_line(struct drm_fb_helper *fb_helper)
  208. {
  209. struct drm_fb_helper_connector *fb_helper_conn;
  210. int i;
  211. for (i = 0; i < fb_helper->connector_count; i++) {
  212. char *option = NULL;
  213. fb_helper_conn = fb_helper->connector_info[i];
  214. /* do something on return - turn off connector maybe */
  215. if (fb_get_options(drm_get_connector_name(fb_helper_conn->connector), &option))
  216. continue;
  217. drm_fb_helper_connector_parse_command_line(fb_helper_conn, option);
  218. }
  219. return 0;
  220. }
  221. static void drm_fb_helper_save_lut_atomic(struct drm_crtc *crtc, struct drm_fb_helper *helper)
  222. {
  223. uint16_t *r_base, *g_base, *b_base;
  224. int i;
  225. r_base = crtc->gamma_store;
  226. g_base = r_base + crtc->gamma_size;
  227. b_base = g_base + crtc->gamma_size;
  228. for (i = 0; i < crtc->gamma_size; i++)
  229. helper->funcs->gamma_get(crtc, &r_base[i], &g_base[i], &b_base[i], i);
  230. }
  231. static void drm_fb_helper_restore_lut_atomic(struct drm_crtc *crtc)
  232. {
  233. uint16_t *r_base, *g_base, *b_base;
  234. r_base = crtc->gamma_store;
  235. g_base = r_base + crtc->gamma_size;
  236. b_base = g_base + crtc->gamma_size;
  237. crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
  238. }
  239. int drm_fb_helper_debug_enter(struct fb_info *info)
  240. {
  241. struct drm_fb_helper *helper = info->par;
  242. struct drm_crtc_helper_funcs *funcs;
  243. int i;
  244. if (list_empty(&kernel_fb_helper_list))
  245. return false;
  246. list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
  247. for (i = 0; i < helper->crtc_count; i++) {
  248. struct drm_mode_set *mode_set =
  249. &helper->crtc_info[i].mode_set;
  250. if (!mode_set->crtc->enabled)
  251. continue;
  252. funcs = mode_set->crtc->helper_private;
  253. drm_fb_helper_save_lut_atomic(mode_set->crtc, helper);
  254. funcs->mode_set_base_atomic(mode_set->crtc,
  255. mode_set->fb,
  256. mode_set->x,
  257. mode_set->y,
  258. ENTER_ATOMIC_MODE_SET);
  259. }
  260. }
  261. return 0;
  262. }
  263. EXPORT_SYMBOL(drm_fb_helper_debug_enter);
  264. /* Find the real fb for a given fb helper CRTC */
  265. static struct drm_framebuffer *drm_mode_config_fb(struct drm_crtc *crtc)
  266. {
  267. struct drm_device *dev = crtc->dev;
  268. struct drm_crtc *c;
  269. list_for_each_entry(c, &dev->mode_config.crtc_list, head) {
  270. if (crtc->base.id == c->base.id)
  271. return c->fb;
  272. }
  273. return NULL;
  274. }
  275. int drm_fb_helper_debug_leave(struct fb_info *info)
  276. {
  277. struct drm_fb_helper *helper = info->par;
  278. struct drm_crtc *crtc;
  279. struct drm_crtc_helper_funcs *funcs;
  280. struct drm_framebuffer *fb;
  281. int i;
  282. for (i = 0; i < helper->crtc_count; i++) {
  283. struct drm_mode_set *mode_set = &helper->crtc_info[i].mode_set;
  284. crtc = mode_set->crtc;
  285. funcs = crtc->helper_private;
  286. fb = drm_mode_config_fb(crtc);
  287. if (!crtc->enabled)
  288. continue;
  289. if (!fb) {
  290. DRM_ERROR("no fb to restore??\n");
  291. continue;
  292. }
  293. drm_fb_helper_restore_lut_atomic(mode_set->crtc);
  294. funcs->mode_set_base_atomic(mode_set->crtc, fb, crtc->x,
  295. crtc->y, LEAVE_ATOMIC_MODE_SET);
  296. }
  297. return 0;
  298. }
  299. EXPORT_SYMBOL(drm_fb_helper_debug_leave);
  300. bool drm_fb_helper_restore_fbdev_mode(struct drm_fb_helper *fb_helper)
  301. {
  302. bool error = false;
  303. int i, ret;
  304. for (i = 0; i < fb_helper->crtc_count; i++) {
  305. struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set;
  306. ret = drm_crtc_helper_set_config(mode_set);
  307. if (ret)
  308. error = true;
  309. }
  310. return error;
  311. }
  312. EXPORT_SYMBOL(drm_fb_helper_restore_fbdev_mode);
  313. bool drm_fb_helper_force_kernel_mode(void)
  314. {
  315. bool ret, error = false;
  316. struct drm_fb_helper *helper;
  317. if (list_empty(&kernel_fb_helper_list))
  318. return false;
  319. list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
  320. if (helper->dev->switch_power_state == DRM_SWITCH_POWER_OFF)
  321. continue;
  322. ret = drm_fb_helper_restore_fbdev_mode(helper);
  323. if (ret)
  324. error = true;
  325. }
  326. return error;
  327. }
  328. int drm_fb_helper_panic(struct notifier_block *n, unsigned long ununsed,
  329. void *panic_str)
  330. {
  331. printk(KERN_ERR "panic occurred, switching back to text console\n");
  332. return drm_fb_helper_force_kernel_mode();
  333. return 0;
  334. }
  335. EXPORT_SYMBOL(drm_fb_helper_panic);
  336. static struct notifier_block paniced = {
  337. .notifier_call = drm_fb_helper_panic,
  338. };
  339. /**
  340. * drm_fb_helper_restore - restore the framebuffer console (kernel) config
  341. *
  342. * Restore's the kernel's fbcon mode, used for lastclose & panic paths.
  343. */
  344. void drm_fb_helper_restore(void)
  345. {
  346. bool ret;
  347. ret = drm_fb_helper_force_kernel_mode();
  348. if (ret == true)
  349. DRM_ERROR("Failed to restore crtc configuration\n");
  350. }
  351. EXPORT_SYMBOL(drm_fb_helper_restore);
  352. #ifdef CONFIG_MAGIC_SYSRQ
  353. static void drm_fb_helper_restore_work_fn(struct work_struct *ignored)
  354. {
  355. drm_fb_helper_restore();
  356. }
  357. static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn);
  358. static void drm_fb_helper_sysrq(int dummy1)
  359. {
  360. schedule_work(&drm_fb_helper_restore_work);
  361. }
  362. static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = {
  363. .handler = drm_fb_helper_sysrq,
  364. .help_msg = "force-fb(V)",
  365. .action_msg = "Restore framebuffer console",
  366. };
  367. #else
  368. static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { };
  369. #endif
  370. static void drm_fb_helper_on(struct fb_info *info)
  371. {
  372. struct drm_fb_helper *fb_helper = info->par;
  373. struct drm_device *dev = fb_helper->dev;
  374. struct drm_crtc *crtc;
  375. struct drm_crtc_helper_funcs *crtc_funcs;
  376. struct drm_connector *connector;
  377. struct drm_encoder *encoder;
  378. int i, j;
  379. /*
  380. * For each CRTC in this fb, turn the crtc on then,
  381. * find all associated encoders and turn them on.
  382. */
  383. mutex_lock(&dev->mode_config.mutex);
  384. for (i = 0; i < fb_helper->crtc_count; i++) {
  385. crtc = fb_helper->crtc_info[i].mode_set.crtc;
  386. crtc_funcs = crtc->helper_private;
  387. if (!crtc->enabled)
  388. continue;
  389. crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
  390. /* Walk the connectors & encoders on this fb turning them on */
  391. for (j = 0; j < fb_helper->connector_count; j++) {
  392. connector = fb_helper->connector_info[j]->connector;
  393. connector->dpms = DRM_MODE_DPMS_ON;
  394. drm_connector_property_set_value(connector,
  395. dev->mode_config.dpms_property,
  396. DRM_MODE_DPMS_ON);
  397. }
  398. /* Found a CRTC on this fb, now find encoders */
  399. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  400. if (encoder->crtc == crtc) {
  401. struct drm_encoder_helper_funcs *encoder_funcs;
  402. encoder_funcs = encoder->helper_private;
  403. encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
  404. }
  405. }
  406. }
  407. mutex_unlock(&dev->mode_config.mutex);
  408. }
  409. static void drm_fb_helper_off(struct fb_info *info, int dpms_mode)
  410. {
  411. struct drm_fb_helper *fb_helper = info->par;
  412. struct drm_device *dev = fb_helper->dev;
  413. struct drm_crtc *crtc;
  414. struct drm_crtc_helper_funcs *crtc_funcs;
  415. struct drm_connector *connector;
  416. struct drm_encoder *encoder;
  417. int i, j;
  418. /*
  419. * For each CRTC in this fb, find all associated encoders
  420. * and turn them off, then turn off the CRTC.
  421. */
  422. mutex_lock(&dev->mode_config.mutex);
  423. for (i = 0; i < fb_helper->crtc_count; i++) {
  424. crtc = fb_helper->crtc_info[i].mode_set.crtc;
  425. crtc_funcs = crtc->helper_private;
  426. if (!crtc->enabled)
  427. continue;
  428. /* Walk the connectors on this fb and mark them off */
  429. for (j = 0; j < fb_helper->connector_count; j++) {
  430. connector = fb_helper->connector_info[j]->connector;
  431. connector->dpms = dpms_mode;
  432. drm_connector_property_set_value(connector,
  433. dev->mode_config.dpms_property,
  434. dpms_mode);
  435. }
  436. /* Found a CRTC on this fb, now find encoders */
  437. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  438. if (encoder->crtc == crtc) {
  439. struct drm_encoder_helper_funcs *encoder_funcs;
  440. encoder_funcs = encoder->helper_private;
  441. encoder_funcs->dpms(encoder, dpms_mode);
  442. }
  443. }
  444. crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
  445. }
  446. mutex_unlock(&dev->mode_config.mutex);
  447. }
  448. int drm_fb_helper_blank(int blank, struct fb_info *info)
  449. {
  450. switch (blank) {
  451. /* Display: On; HSync: On, VSync: On */
  452. case FB_BLANK_UNBLANK:
  453. drm_fb_helper_on(info);
  454. break;
  455. /* Display: Off; HSync: On, VSync: On */
  456. case FB_BLANK_NORMAL:
  457. drm_fb_helper_off(info, DRM_MODE_DPMS_STANDBY);
  458. break;
  459. /* Display: Off; HSync: Off, VSync: On */
  460. case FB_BLANK_HSYNC_SUSPEND:
  461. drm_fb_helper_off(info, DRM_MODE_DPMS_STANDBY);
  462. break;
  463. /* Display: Off; HSync: On, VSync: Off */
  464. case FB_BLANK_VSYNC_SUSPEND:
  465. drm_fb_helper_off(info, DRM_MODE_DPMS_SUSPEND);
  466. break;
  467. /* Display: Off; HSync: Off, VSync: Off */
  468. case FB_BLANK_POWERDOWN:
  469. drm_fb_helper_off(info, DRM_MODE_DPMS_OFF);
  470. break;
  471. }
  472. return 0;
  473. }
  474. EXPORT_SYMBOL(drm_fb_helper_blank);
  475. static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper)
  476. {
  477. int i;
  478. for (i = 0; i < helper->connector_count; i++)
  479. kfree(helper->connector_info[i]);
  480. kfree(helper->connector_info);
  481. for (i = 0; i < helper->crtc_count; i++)
  482. kfree(helper->crtc_info[i].mode_set.connectors);
  483. kfree(helper->crtc_info);
  484. }
  485. int drm_fb_helper_init(struct drm_device *dev,
  486. struct drm_fb_helper *fb_helper,
  487. int crtc_count, int max_conn_count)
  488. {
  489. struct drm_crtc *crtc;
  490. int ret = 0;
  491. int i;
  492. fb_helper->dev = dev;
  493. INIT_LIST_HEAD(&fb_helper->kernel_fb_list);
  494. fb_helper->crtc_info = kcalloc(crtc_count, sizeof(struct drm_fb_helper_crtc), GFP_KERNEL);
  495. if (!fb_helper->crtc_info)
  496. return -ENOMEM;
  497. fb_helper->crtc_count = crtc_count;
  498. fb_helper->connector_info = kcalloc(dev->mode_config.num_connector, sizeof(struct drm_fb_helper_connector *), GFP_KERNEL);
  499. if (!fb_helper->connector_info) {
  500. kfree(fb_helper->crtc_info);
  501. return -ENOMEM;
  502. }
  503. fb_helper->connector_count = 0;
  504. for (i = 0; i < crtc_count; i++) {
  505. fb_helper->crtc_info[i].mode_set.connectors =
  506. kcalloc(max_conn_count,
  507. sizeof(struct drm_connector *),
  508. GFP_KERNEL);
  509. if (!fb_helper->crtc_info[i].mode_set.connectors) {
  510. ret = -ENOMEM;
  511. goto out_free;
  512. }
  513. fb_helper->crtc_info[i].mode_set.num_connectors = 0;
  514. }
  515. i = 0;
  516. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  517. fb_helper->crtc_info[i].crtc_id = crtc->base.id;
  518. fb_helper->crtc_info[i].mode_set.crtc = crtc;
  519. i++;
  520. }
  521. fb_helper->conn_limit = max_conn_count;
  522. return 0;
  523. out_free:
  524. drm_fb_helper_crtc_free(fb_helper);
  525. return -ENOMEM;
  526. }
  527. EXPORT_SYMBOL(drm_fb_helper_init);
  528. void drm_fb_helper_fini(struct drm_fb_helper *fb_helper)
  529. {
  530. if (!list_empty(&fb_helper->kernel_fb_list)) {
  531. list_del(&fb_helper->kernel_fb_list);
  532. if (list_empty(&kernel_fb_helper_list)) {
  533. printk(KERN_INFO "drm: unregistered panic notifier\n");
  534. atomic_notifier_chain_unregister(&panic_notifier_list,
  535. &paniced);
  536. unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
  537. }
  538. }
  539. drm_fb_helper_crtc_free(fb_helper);
  540. }
  541. EXPORT_SYMBOL(drm_fb_helper_fini);
  542. static int setcolreg(struct drm_crtc *crtc, u16 red, u16 green,
  543. u16 blue, u16 regno, struct fb_info *info)
  544. {
  545. struct drm_fb_helper *fb_helper = info->par;
  546. struct drm_framebuffer *fb = fb_helper->fb;
  547. int pindex;
  548. if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
  549. u32 *palette;
  550. u32 value;
  551. /* place color in psuedopalette */
  552. if (regno > 16)
  553. return -EINVAL;
  554. palette = (u32 *)info->pseudo_palette;
  555. red >>= (16 - info->var.red.length);
  556. green >>= (16 - info->var.green.length);
  557. blue >>= (16 - info->var.blue.length);
  558. value = (red << info->var.red.offset) |
  559. (green << info->var.green.offset) |
  560. (blue << info->var.blue.offset);
  561. if (info->var.transp.length > 0) {
  562. u32 mask = (1 << info->var.transp.length) - 1;
  563. mask <<= info->var.transp.offset;
  564. value |= mask;
  565. }
  566. palette[regno] = value;
  567. return 0;
  568. }
  569. pindex = regno;
  570. if (fb->bits_per_pixel == 16) {
  571. pindex = regno << 3;
  572. if (fb->depth == 16 && regno > 63)
  573. return -EINVAL;
  574. if (fb->depth == 15 && regno > 31)
  575. return -EINVAL;
  576. if (fb->depth == 16) {
  577. u16 r, g, b;
  578. int i;
  579. if (regno < 32) {
  580. for (i = 0; i < 8; i++)
  581. fb_helper->funcs->gamma_set(crtc, red,
  582. green, blue, pindex + i);
  583. }
  584. fb_helper->funcs->gamma_get(crtc, &r,
  585. &g, &b,
  586. pindex >> 1);
  587. for (i = 0; i < 4; i++)
  588. fb_helper->funcs->gamma_set(crtc, r,
  589. green, b,
  590. (pindex >> 1) + i);
  591. }
  592. }
  593. if (fb->depth != 16)
  594. fb_helper->funcs->gamma_set(crtc, red, green, blue, pindex);
  595. return 0;
  596. }
  597. int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info)
  598. {
  599. struct drm_fb_helper *fb_helper = info->par;
  600. struct drm_crtc_helper_funcs *crtc_funcs;
  601. u16 *red, *green, *blue, *transp;
  602. struct drm_crtc *crtc;
  603. int i, j, rc = 0;
  604. int start;
  605. for (i = 0; i < fb_helper->crtc_count; i++) {
  606. crtc = fb_helper->crtc_info[i].mode_set.crtc;
  607. crtc_funcs = crtc->helper_private;
  608. red = cmap->red;
  609. green = cmap->green;
  610. blue = cmap->blue;
  611. transp = cmap->transp;
  612. start = cmap->start;
  613. for (j = 0; j < cmap->len; j++) {
  614. u16 hred, hgreen, hblue, htransp = 0xffff;
  615. hred = *red++;
  616. hgreen = *green++;
  617. hblue = *blue++;
  618. if (transp)
  619. htransp = *transp++;
  620. rc = setcolreg(crtc, hred, hgreen, hblue, start++, info);
  621. if (rc)
  622. return rc;
  623. }
  624. crtc_funcs->load_lut(crtc);
  625. }
  626. return rc;
  627. }
  628. EXPORT_SYMBOL(drm_fb_helper_setcmap);
  629. int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
  630. struct fb_info *info)
  631. {
  632. struct drm_fb_helper *fb_helper = info->par;
  633. struct drm_framebuffer *fb = fb_helper->fb;
  634. int depth;
  635. if (var->pixclock != 0 || in_dbg_master())
  636. return -EINVAL;
  637. /* Need to resize the fb object !!! */
  638. if (var->bits_per_pixel > fb->bits_per_pixel || var->xres > fb->width || var->yres > fb->height) {
  639. DRM_DEBUG("fb userspace requested width/height/bpp is greater than current fb "
  640. "object %dx%d-%d > %dx%d-%d\n", var->xres, var->yres, var->bits_per_pixel,
  641. fb->width, fb->height, fb->bits_per_pixel);
  642. return -EINVAL;
  643. }
  644. switch (var->bits_per_pixel) {
  645. case 16:
  646. depth = (var->green.length == 6) ? 16 : 15;
  647. break;
  648. case 32:
  649. depth = (var->transp.length > 0) ? 32 : 24;
  650. break;
  651. default:
  652. depth = var->bits_per_pixel;
  653. break;
  654. }
  655. switch (depth) {
  656. case 8:
  657. var->red.offset = 0;
  658. var->green.offset = 0;
  659. var->blue.offset = 0;
  660. var->red.length = 8;
  661. var->green.length = 8;
  662. var->blue.length = 8;
  663. var->transp.length = 0;
  664. var->transp.offset = 0;
  665. break;
  666. case 15:
  667. var->red.offset = 10;
  668. var->green.offset = 5;
  669. var->blue.offset = 0;
  670. var->red.length = 5;
  671. var->green.length = 5;
  672. var->blue.length = 5;
  673. var->transp.length = 1;
  674. var->transp.offset = 15;
  675. break;
  676. case 16:
  677. var->red.offset = 11;
  678. var->green.offset = 5;
  679. var->blue.offset = 0;
  680. var->red.length = 5;
  681. var->green.length = 6;
  682. var->blue.length = 5;
  683. var->transp.length = 0;
  684. var->transp.offset = 0;
  685. break;
  686. case 24:
  687. var->red.offset = 16;
  688. var->green.offset = 8;
  689. var->blue.offset = 0;
  690. var->red.length = 8;
  691. var->green.length = 8;
  692. var->blue.length = 8;
  693. var->transp.length = 0;
  694. var->transp.offset = 0;
  695. break;
  696. case 32:
  697. var->red.offset = 16;
  698. var->green.offset = 8;
  699. var->blue.offset = 0;
  700. var->red.length = 8;
  701. var->green.length = 8;
  702. var->blue.length = 8;
  703. var->transp.length = 8;
  704. var->transp.offset = 24;
  705. break;
  706. default:
  707. return -EINVAL;
  708. }
  709. return 0;
  710. }
  711. EXPORT_SYMBOL(drm_fb_helper_check_var);
  712. /* this will let fbcon do the mode init */
  713. int drm_fb_helper_set_par(struct fb_info *info)
  714. {
  715. struct drm_fb_helper *fb_helper = info->par;
  716. struct drm_device *dev = fb_helper->dev;
  717. struct fb_var_screeninfo *var = &info->var;
  718. struct drm_crtc *crtc;
  719. int ret;
  720. int i;
  721. if (var->pixclock != 0) {
  722. DRM_ERROR("PIXEL CLOCK SET\n");
  723. return -EINVAL;
  724. }
  725. mutex_lock(&dev->mode_config.mutex);
  726. for (i = 0; i < fb_helper->crtc_count; i++) {
  727. crtc = fb_helper->crtc_info[i].mode_set.crtc;
  728. ret = crtc->funcs->set_config(&fb_helper->crtc_info[i].mode_set);
  729. if (ret) {
  730. mutex_unlock(&dev->mode_config.mutex);
  731. return ret;
  732. }
  733. }
  734. mutex_unlock(&dev->mode_config.mutex);
  735. if (fb_helper->delayed_hotplug) {
  736. fb_helper->delayed_hotplug = false;
  737. drm_fb_helper_hotplug_event(fb_helper);
  738. }
  739. return 0;
  740. }
  741. EXPORT_SYMBOL(drm_fb_helper_set_par);
  742. int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
  743. struct fb_info *info)
  744. {
  745. struct drm_fb_helper *fb_helper = info->par;
  746. struct drm_device *dev = fb_helper->dev;
  747. struct drm_mode_set *modeset;
  748. struct drm_crtc *crtc;
  749. int ret = 0;
  750. int i;
  751. mutex_lock(&dev->mode_config.mutex);
  752. for (i = 0; i < fb_helper->crtc_count; i++) {
  753. crtc = fb_helper->crtc_info[i].mode_set.crtc;
  754. modeset = &fb_helper->crtc_info[i].mode_set;
  755. modeset->x = var->xoffset;
  756. modeset->y = var->yoffset;
  757. if (modeset->num_connectors) {
  758. ret = crtc->funcs->set_config(modeset);
  759. if (!ret) {
  760. info->var.xoffset = var->xoffset;
  761. info->var.yoffset = var->yoffset;
  762. }
  763. }
  764. }
  765. mutex_unlock(&dev->mode_config.mutex);
  766. return ret;
  767. }
  768. EXPORT_SYMBOL(drm_fb_helper_pan_display);
  769. int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
  770. int preferred_bpp)
  771. {
  772. int new_fb = 0;
  773. int crtc_count = 0;
  774. int i;
  775. struct fb_info *info;
  776. struct drm_fb_helper_surface_size sizes;
  777. int gamma_size = 0;
  778. memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size));
  779. sizes.surface_depth = 24;
  780. sizes.surface_bpp = 32;
  781. sizes.fb_width = (unsigned)-1;
  782. sizes.fb_height = (unsigned)-1;
  783. /* if driver picks 8 or 16 by default use that
  784. for both depth/bpp */
  785. if (preferred_bpp != sizes.surface_bpp) {
  786. sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
  787. }
  788. /* first up get a count of crtcs now in use and new min/maxes width/heights */
  789. for (i = 0; i < fb_helper->connector_count; i++) {
  790. struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i];
  791. struct drm_fb_helper_cmdline_mode *cmdline_mode;
  792. cmdline_mode = &fb_helper_conn->cmdline_mode;
  793. if (cmdline_mode->bpp_specified) {
  794. switch (cmdline_mode->bpp) {
  795. case 8:
  796. sizes.surface_depth = sizes.surface_bpp = 8;
  797. break;
  798. case 15:
  799. sizes.surface_depth = 15;
  800. sizes.surface_bpp = 16;
  801. break;
  802. case 16:
  803. sizes.surface_depth = sizes.surface_bpp = 16;
  804. break;
  805. case 24:
  806. sizes.surface_depth = sizes.surface_bpp = 24;
  807. break;
  808. case 32:
  809. sizes.surface_depth = 24;
  810. sizes.surface_bpp = 32;
  811. break;
  812. }
  813. break;
  814. }
  815. }
  816. crtc_count = 0;
  817. for (i = 0; i < fb_helper->crtc_count; i++) {
  818. struct drm_display_mode *desired_mode;
  819. desired_mode = fb_helper->crtc_info[i].desired_mode;
  820. if (desired_mode) {
  821. if (gamma_size == 0)
  822. gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size;
  823. if (desired_mode->hdisplay < sizes.fb_width)
  824. sizes.fb_width = desired_mode->hdisplay;
  825. if (desired_mode->vdisplay < sizes.fb_height)
  826. sizes.fb_height = desired_mode->vdisplay;
  827. if (desired_mode->hdisplay > sizes.surface_width)
  828. sizes.surface_width = desired_mode->hdisplay;
  829. if (desired_mode->vdisplay > sizes.surface_height)
  830. sizes.surface_height = desired_mode->vdisplay;
  831. crtc_count++;
  832. }
  833. }
  834. if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) {
  835. /* hmm everyone went away - assume VGA cable just fell out
  836. and will come back later. */
  837. DRM_INFO("Cannot find any crtc or sizes - going 1024x768\n");
  838. sizes.fb_width = sizes.surface_width = 1024;
  839. sizes.fb_height = sizes.surface_height = 768;
  840. }
  841. /* push down into drivers */
  842. new_fb = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes);
  843. if (new_fb < 0)
  844. return new_fb;
  845. info = fb_helper->fbdev;
  846. /* set the fb pointer */
  847. for (i = 0; i < fb_helper->crtc_count; i++) {
  848. fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb;
  849. }
  850. if (new_fb) {
  851. info->var.pixclock = 0;
  852. if (register_framebuffer(info) < 0) {
  853. return -EINVAL;
  854. }
  855. printk(KERN_INFO "fb%d: %s frame buffer device\n", info->node,
  856. info->fix.id);
  857. } else {
  858. drm_fb_helper_set_par(info);
  859. }
  860. /* Switch back to kernel console on panic */
  861. /* multi card linked list maybe */
  862. if (list_empty(&kernel_fb_helper_list)) {
  863. printk(KERN_INFO "drm: registered panic notifier\n");
  864. atomic_notifier_chain_register(&panic_notifier_list,
  865. &paniced);
  866. register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
  867. }
  868. if (new_fb)
  869. list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
  870. return 0;
  871. }
  872. EXPORT_SYMBOL(drm_fb_helper_single_fb_probe);
  873. void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
  874. uint32_t depth)
  875. {
  876. info->fix.type = FB_TYPE_PACKED_PIXELS;
  877. info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR :
  878. FB_VISUAL_TRUECOLOR;
  879. info->fix.mmio_start = 0;
  880. info->fix.mmio_len = 0;
  881. info->fix.type_aux = 0;
  882. info->fix.xpanstep = 1; /* doing it in hw */
  883. info->fix.ypanstep = 1; /* doing it in hw */
  884. info->fix.ywrapstep = 0;
  885. info->fix.accel = FB_ACCEL_NONE;
  886. info->fix.type_aux = 0;
  887. info->fix.line_length = pitch;
  888. return;
  889. }
  890. EXPORT_SYMBOL(drm_fb_helper_fill_fix);
  891. void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper,
  892. uint32_t fb_width, uint32_t fb_height)
  893. {
  894. struct drm_framebuffer *fb = fb_helper->fb;
  895. info->pseudo_palette = fb_helper->pseudo_palette;
  896. info->var.xres_virtual = fb->width;
  897. info->var.yres_virtual = fb->height;
  898. info->var.bits_per_pixel = fb->bits_per_pixel;
  899. info->var.accel_flags = FB_ACCELF_TEXT;
  900. info->var.xoffset = 0;
  901. info->var.yoffset = 0;
  902. info->var.activate = FB_ACTIVATE_NOW;
  903. info->var.height = -1;
  904. info->var.width = -1;
  905. switch (fb->depth) {
  906. case 8:
  907. info->var.red.offset = 0;
  908. info->var.green.offset = 0;
  909. info->var.blue.offset = 0;
  910. info->var.red.length = 8; /* 8bit DAC */
  911. info->var.green.length = 8;
  912. info->var.blue.length = 8;
  913. info->var.transp.offset = 0;
  914. info->var.transp.length = 0;
  915. break;
  916. case 15:
  917. info->var.red.offset = 10;
  918. info->var.green.offset = 5;
  919. info->var.blue.offset = 0;
  920. info->var.red.length = 5;
  921. info->var.green.length = 5;
  922. info->var.blue.length = 5;
  923. info->var.transp.offset = 15;
  924. info->var.transp.length = 1;
  925. break;
  926. case 16:
  927. info->var.red.offset = 11;
  928. info->var.green.offset = 5;
  929. info->var.blue.offset = 0;
  930. info->var.red.length = 5;
  931. info->var.green.length = 6;
  932. info->var.blue.length = 5;
  933. info->var.transp.offset = 0;
  934. break;
  935. case 24:
  936. info->var.red.offset = 16;
  937. info->var.green.offset = 8;
  938. info->var.blue.offset = 0;
  939. info->var.red.length = 8;
  940. info->var.green.length = 8;
  941. info->var.blue.length = 8;
  942. info->var.transp.offset = 0;
  943. info->var.transp.length = 0;
  944. break;
  945. case 32:
  946. info->var.red.offset = 16;
  947. info->var.green.offset = 8;
  948. info->var.blue.offset = 0;
  949. info->var.red.length = 8;
  950. info->var.green.length = 8;
  951. info->var.blue.length = 8;
  952. info->var.transp.offset = 24;
  953. info->var.transp.length = 8;
  954. break;
  955. default:
  956. break;
  957. }
  958. info->var.xres = fb_width;
  959. info->var.yres = fb_height;
  960. }
  961. EXPORT_SYMBOL(drm_fb_helper_fill_var);
  962. static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper,
  963. uint32_t maxX,
  964. uint32_t maxY)
  965. {
  966. struct drm_connector *connector;
  967. int count = 0;
  968. int i;
  969. for (i = 0; i < fb_helper->connector_count; i++) {
  970. connector = fb_helper->connector_info[i]->connector;
  971. count += connector->funcs->fill_modes(connector, maxX, maxY);
  972. }
  973. return count;
  974. }
  975. static struct drm_display_mode *drm_has_preferred_mode(struct drm_fb_helper_connector *fb_connector, int width, int height)
  976. {
  977. struct drm_display_mode *mode;
  978. list_for_each_entry(mode, &fb_connector->connector->modes, head) {
  979. if (drm_mode_width(mode) > width ||
  980. drm_mode_height(mode) > height)
  981. continue;
  982. if (mode->type & DRM_MODE_TYPE_PREFERRED)
  983. return mode;
  984. }
  985. return NULL;
  986. }
  987. static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector)
  988. {
  989. struct drm_fb_helper_cmdline_mode *cmdline_mode;
  990. cmdline_mode = &fb_connector->cmdline_mode;
  991. return cmdline_mode->specified;
  992. }
  993. static struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn,
  994. int width, int height)
  995. {
  996. struct drm_fb_helper_cmdline_mode *cmdline_mode;
  997. struct drm_display_mode *mode = NULL;
  998. cmdline_mode = &fb_helper_conn->cmdline_mode;
  999. if (cmdline_mode->specified == false)
  1000. return mode;
  1001. /* attempt to find a matching mode in the list of modes
  1002. * we have gotten so far, if not add a CVT mode that conforms
  1003. */
  1004. if (cmdline_mode->rb || cmdline_mode->margins)
  1005. goto create_mode;
  1006. list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
  1007. /* check width/height */
  1008. if (mode->hdisplay != cmdline_mode->xres ||
  1009. mode->vdisplay != cmdline_mode->yres)
  1010. continue;
  1011. if (cmdline_mode->refresh_specified) {
  1012. if (mode->vrefresh != cmdline_mode->refresh)
  1013. continue;
  1014. }
  1015. if (cmdline_mode->interlace) {
  1016. if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
  1017. continue;
  1018. }
  1019. return mode;
  1020. }
  1021. create_mode:
  1022. if (cmdline_mode->cvt)
  1023. mode = drm_cvt_mode(fb_helper_conn->connector->dev,
  1024. cmdline_mode->xres, cmdline_mode->yres,
  1025. cmdline_mode->refresh_specified ? cmdline_mode->refresh : 60,
  1026. cmdline_mode->rb, cmdline_mode->interlace,
  1027. cmdline_mode->margins);
  1028. else
  1029. mode = drm_gtf_mode(fb_helper_conn->connector->dev,
  1030. cmdline_mode->xres, cmdline_mode->yres,
  1031. cmdline_mode->refresh_specified ? cmdline_mode->refresh : 60,
  1032. cmdline_mode->interlace,
  1033. cmdline_mode->margins);
  1034. drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
  1035. list_add(&mode->head, &fb_helper_conn->connector->modes);
  1036. return mode;
  1037. }
  1038. static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
  1039. {
  1040. bool enable;
  1041. if (strict) {
  1042. enable = connector->status == connector_status_connected;
  1043. } else {
  1044. enable = connector->status != connector_status_disconnected;
  1045. }
  1046. return enable;
  1047. }
  1048. static void drm_enable_connectors(struct drm_fb_helper *fb_helper,
  1049. bool *enabled)
  1050. {
  1051. bool any_enabled = false;
  1052. struct drm_connector *connector;
  1053. int i = 0;
  1054. for (i = 0; i < fb_helper->connector_count; i++) {
  1055. connector = fb_helper->connector_info[i]->connector;
  1056. enabled[i] = drm_connector_enabled(connector, true);
  1057. DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
  1058. enabled[i] ? "yes" : "no");
  1059. any_enabled |= enabled[i];
  1060. }
  1061. if (any_enabled)
  1062. return;
  1063. for (i = 0; i < fb_helper->connector_count; i++) {
  1064. connector = fb_helper->connector_info[i]->connector;
  1065. enabled[i] = drm_connector_enabled(connector, false);
  1066. }
  1067. }
  1068. static bool drm_target_cloned(struct drm_fb_helper *fb_helper,
  1069. struct drm_display_mode **modes,
  1070. bool *enabled, int width, int height)
  1071. {
  1072. int count, i, j;
  1073. bool can_clone = false;
  1074. struct drm_fb_helper_connector *fb_helper_conn;
  1075. struct drm_display_mode *dmt_mode, *mode;
  1076. /* only contemplate cloning in the single crtc case */
  1077. if (fb_helper->crtc_count > 1)
  1078. return false;
  1079. count = 0;
  1080. for (i = 0; i < fb_helper->connector_count; i++) {
  1081. if (enabled[i])
  1082. count++;
  1083. }
  1084. /* only contemplate cloning if more than one connector is enabled */
  1085. if (count <= 1)
  1086. return false;
  1087. /* check the command line or if nothing common pick 1024x768 */
  1088. can_clone = true;
  1089. for (i = 0; i < fb_helper->connector_count; i++) {
  1090. if (!enabled[i])
  1091. continue;
  1092. fb_helper_conn = fb_helper->connector_info[i];
  1093. modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
  1094. if (!modes[i]) {
  1095. can_clone = false;
  1096. break;
  1097. }
  1098. for (j = 0; j < i; j++) {
  1099. if (!enabled[j])
  1100. continue;
  1101. if (!drm_mode_equal(modes[j], modes[i]))
  1102. can_clone = false;
  1103. }
  1104. }
  1105. if (can_clone) {
  1106. DRM_DEBUG_KMS("can clone using command line\n");
  1107. return true;
  1108. }
  1109. /* try and find a 1024x768 mode on each connector */
  1110. can_clone = true;
  1111. dmt_mode = drm_mode_find_dmt(fb_helper->dev, 1024, 768, 60);
  1112. for (i = 0; i < fb_helper->connector_count; i++) {
  1113. if (!enabled[i])
  1114. continue;
  1115. fb_helper_conn = fb_helper->connector_info[i];
  1116. list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
  1117. if (drm_mode_equal(mode, dmt_mode))
  1118. modes[i] = mode;
  1119. }
  1120. if (!modes[i])
  1121. can_clone = false;
  1122. }
  1123. if (can_clone) {
  1124. DRM_DEBUG_KMS("can clone using 1024x768\n");
  1125. return true;
  1126. }
  1127. DRM_INFO("kms: can't enable cloning when we probably wanted to.\n");
  1128. return false;
  1129. }
  1130. static bool drm_target_preferred(struct drm_fb_helper *fb_helper,
  1131. struct drm_display_mode **modes,
  1132. bool *enabled, int width, int height)
  1133. {
  1134. struct drm_fb_helper_connector *fb_helper_conn;
  1135. int i;
  1136. for (i = 0; i < fb_helper->connector_count; i++) {
  1137. fb_helper_conn = fb_helper->connector_info[i];
  1138. if (enabled[i] == false)
  1139. continue;
  1140. DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
  1141. fb_helper_conn->connector->base.id);
  1142. /* got for command line mode first */
  1143. modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
  1144. if (!modes[i]) {
  1145. DRM_DEBUG_KMS("looking for preferred mode on connector %d\n",
  1146. fb_helper_conn->connector->base.id);
  1147. modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height);
  1148. }
  1149. /* No preferred modes, pick one off the list */
  1150. if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) {
  1151. list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head)
  1152. break;
  1153. }
  1154. DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
  1155. "none");
  1156. }
  1157. return true;
  1158. }
  1159. static int drm_pick_crtcs(struct drm_fb_helper *fb_helper,
  1160. struct drm_fb_helper_crtc **best_crtcs,
  1161. struct drm_display_mode **modes,
  1162. int n, int width, int height)
  1163. {
  1164. int c, o;
  1165. struct drm_device *dev = fb_helper->dev;
  1166. struct drm_connector *connector;
  1167. struct drm_connector_helper_funcs *connector_funcs;
  1168. struct drm_encoder *encoder;
  1169. struct drm_fb_helper_crtc *best_crtc;
  1170. int my_score, best_score, score;
  1171. struct drm_fb_helper_crtc **crtcs, *crtc;
  1172. struct drm_fb_helper_connector *fb_helper_conn;
  1173. if (n == fb_helper->connector_count)
  1174. return 0;
  1175. fb_helper_conn = fb_helper->connector_info[n];
  1176. connector = fb_helper_conn->connector;
  1177. best_crtcs[n] = NULL;
  1178. best_crtc = NULL;
  1179. best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height);
  1180. if (modes[n] == NULL)
  1181. return best_score;
  1182. crtcs = kzalloc(dev->mode_config.num_connector *
  1183. sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
  1184. if (!crtcs)
  1185. return best_score;
  1186. my_score = 1;
  1187. if (connector->status == connector_status_connected)
  1188. my_score++;
  1189. if (drm_has_cmdline_mode(fb_helper_conn))
  1190. my_score++;
  1191. if (drm_has_preferred_mode(fb_helper_conn, width, height))
  1192. my_score++;
  1193. connector_funcs = connector->helper_private;
  1194. encoder = connector_funcs->best_encoder(connector);
  1195. if (!encoder)
  1196. goto out;
  1197. /* select a crtc for this connector and then attempt to configure
  1198. remaining connectors */
  1199. for (c = 0; c < fb_helper->crtc_count; c++) {
  1200. crtc = &fb_helper->crtc_info[c];
  1201. if ((encoder->possible_crtcs & (1 << c)) == 0) {
  1202. continue;
  1203. }
  1204. for (o = 0; o < n; o++)
  1205. if (best_crtcs[o] == crtc)
  1206. break;
  1207. if (o < n) {
  1208. /* ignore cloning unless only a single crtc */
  1209. if (fb_helper->crtc_count > 1)
  1210. continue;
  1211. if (!drm_mode_equal(modes[o], modes[n]))
  1212. continue;
  1213. }
  1214. crtcs[n] = crtc;
  1215. memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *));
  1216. score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1,
  1217. width, height);
  1218. if (score > best_score) {
  1219. best_crtc = crtc;
  1220. best_score = score;
  1221. memcpy(best_crtcs, crtcs,
  1222. dev->mode_config.num_connector *
  1223. sizeof(struct drm_fb_helper_crtc *));
  1224. }
  1225. }
  1226. out:
  1227. kfree(crtcs);
  1228. return best_score;
  1229. }
  1230. static void drm_setup_crtcs(struct drm_fb_helper *fb_helper)
  1231. {
  1232. struct drm_device *dev = fb_helper->dev;
  1233. struct drm_fb_helper_crtc **crtcs;
  1234. struct drm_display_mode **modes;
  1235. struct drm_encoder *encoder;
  1236. struct drm_mode_set *modeset;
  1237. bool *enabled;
  1238. int width, height;
  1239. int i, ret;
  1240. DRM_DEBUG_KMS("\n");
  1241. width = dev->mode_config.max_width;
  1242. height = dev->mode_config.max_height;
  1243. /* clean out all the encoder/crtc combos */
  1244. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  1245. encoder->crtc = NULL;
  1246. }
  1247. crtcs = kcalloc(dev->mode_config.num_connector,
  1248. sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
  1249. modes = kcalloc(dev->mode_config.num_connector,
  1250. sizeof(struct drm_display_mode *), GFP_KERNEL);
  1251. enabled = kcalloc(dev->mode_config.num_connector,
  1252. sizeof(bool), GFP_KERNEL);
  1253. drm_enable_connectors(fb_helper, enabled);
  1254. ret = drm_target_cloned(fb_helper, modes, enabled, width, height);
  1255. if (!ret) {
  1256. ret = drm_target_preferred(fb_helper, modes, enabled, width, height);
  1257. if (!ret)
  1258. DRM_ERROR("Unable to find initial modes\n");
  1259. }
  1260. DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n", width, height);
  1261. drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height);
  1262. /* need to set the modesets up here for use later */
  1263. /* fill out the connector<->crtc mappings into the modesets */
  1264. for (i = 0; i < fb_helper->crtc_count; i++) {
  1265. modeset = &fb_helper->crtc_info[i].mode_set;
  1266. modeset->num_connectors = 0;
  1267. }
  1268. for (i = 0; i < fb_helper->connector_count; i++) {
  1269. struct drm_display_mode *mode = modes[i];
  1270. struct drm_fb_helper_crtc *fb_crtc = crtcs[i];
  1271. modeset = &fb_crtc->mode_set;
  1272. if (mode && fb_crtc) {
  1273. DRM_DEBUG_KMS("desired mode %s set on crtc %d\n",
  1274. mode->name, fb_crtc->mode_set.crtc->base.id);
  1275. fb_crtc->desired_mode = mode;
  1276. if (modeset->mode)
  1277. drm_mode_destroy(dev, modeset->mode);
  1278. modeset->mode = drm_mode_duplicate(dev,
  1279. fb_crtc->desired_mode);
  1280. modeset->connectors[modeset->num_connectors++] = fb_helper->connector_info[i]->connector;
  1281. }
  1282. }
  1283. kfree(crtcs);
  1284. kfree(modes);
  1285. kfree(enabled);
  1286. }
  1287. /**
  1288. * drm_helper_initial_config - setup a sane initial connector configuration
  1289. * @dev: DRM device
  1290. *
  1291. * LOCKING:
  1292. * Called at init time, must take mode config lock.
  1293. *
  1294. * Scan the CRTCs and connectors and try to put together an initial setup.
  1295. * At the moment, this is a cloned configuration across all heads with
  1296. * a new framebuffer object as the backing store.
  1297. *
  1298. * RETURNS:
  1299. * Zero if everything went ok, nonzero otherwise.
  1300. */
  1301. bool drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel)
  1302. {
  1303. struct drm_device *dev = fb_helper->dev;
  1304. int count = 0;
  1305. /* disable all the possible outputs/crtcs before entering KMS mode */
  1306. drm_helper_disable_unused_functions(fb_helper->dev);
  1307. drm_fb_helper_parse_command_line(fb_helper);
  1308. count = drm_fb_helper_probe_connector_modes(fb_helper,
  1309. dev->mode_config.max_width,
  1310. dev->mode_config.max_height);
  1311. /*
  1312. * we shouldn't end up with no modes here.
  1313. */
  1314. if (count == 0) {
  1315. printk(KERN_INFO "No connectors reported connected with modes\n");
  1316. }
  1317. drm_setup_crtcs(fb_helper);
  1318. return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
  1319. }
  1320. EXPORT_SYMBOL(drm_fb_helper_initial_config);
  1321. bool drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper)
  1322. {
  1323. int count = 0;
  1324. u32 max_width, max_height, bpp_sel;
  1325. bool bound = false, crtcs_bound = false;
  1326. struct drm_crtc *crtc;
  1327. if (!fb_helper->fb)
  1328. return false;
  1329. list_for_each_entry(crtc, &fb_helper->dev->mode_config.crtc_list, head) {
  1330. if (crtc->fb)
  1331. crtcs_bound = true;
  1332. if (crtc->fb == fb_helper->fb)
  1333. bound = true;
  1334. }
  1335. if (!bound && crtcs_bound) {
  1336. fb_helper->delayed_hotplug = true;
  1337. return false;
  1338. }
  1339. DRM_DEBUG_KMS("\n");
  1340. max_width = fb_helper->fb->width;
  1341. max_height = fb_helper->fb->height;
  1342. bpp_sel = fb_helper->fb->bits_per_pixel;
  1343. count = drm_fb_helper_probe_connector_modes(fb_helper, max_width,
  1344. max_height);
  1345. drm_setup_crtcs(fb_helper);
  1346. return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
  1347. }
  1348. EXPORT_SYMBOL(drm_fb_helper_hotplug_event);
  1349. /* The Kconfig DRM_KMS_HELPER selects FRAMEBUFFER_CONSOLE (if !EXPERT)
  1350. * but the module doesn't depend on any fb console symbols. At least
  1351. * attempt to load fbcon to avoid leaving the system without a usable console.
  1352. */
  1353. #if defined(CONFIG_FRAMEBUFFER_CONSOLE_MODULE) && !defined(CONFIG_EXPERT)
  1354. static int __init drm_fb_helper_modinit(void)
  1355. {
  1356. const char *name = "fbcon";
  1357. struct module *fbcon;
  1358. mutex_lock(&module_mutex);
  1359. fbcon = find_module(name);
  1360. mutex_unlock(&module_mutex);
  1361. if (!fbcon)
  1362. request_module_nowait(name);
  1363. return 0;
  1364. }
  1365. module_init(drm_fb_helper_modinit);
  1366. #endif