drm_fb_helper.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532
  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_force_kernel_mode(void)
  301. {
  302. int i = 0;
  303. bool ret, error = false;
  304. struct drm_fb_helper *helper;
  305. if (list_empty(&kernel_fb_helper_list))
  306. return false;
  307. list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
  308. for (i = 0; i < helper->crtc_count; i++) {
  309. struct drm_mode_set *mode_set = &helper->crtc_info[i].mode_set;
  310. ret = drm_crtc_helper_set_config(mode_set);
  311. if (ret)
  312. error = true;
  313. }
  314. }
  315. return error;
  316. }
  317. int drm_fb_helper_panic(struct notifier_block *n, unsigned long ununsed,
  318. void *panic_str)
  319. {
  320. printk(KERN_ERR "panic occurred, switching back to text console\n");
  321. return drm_fb_helper_force_kernel_mode();
  322. return 0;
  323. }
  324. EXPORT_SYMBOL(drm_fb_helper_panic);
  325. static struct notifier_block paniced = {
  326. .notifier_call = drm_fb_helper_panic,
  327. };
  328. /**
  329. * drm_fb_helper_restore - restore the framebuffer console (kernel) config
  330. *
  331. * Restore's the kernel's fbcon mode, used for lastclose & panic paths.
  332. */
  333. void drm_fb_helper_restore(void)
  334. {
  335. bool ret;
  336. ret = drm_fb_helper_force_kernel_mode();
  337. if (ret == true)
  338. DRM_ERROR("Failed to restore crtc configuration\n");
  339. }
  340. EXPORT_SYMBOL(drm_fb_helper_restore);
  341. #ifdef CONFIG_MAGIC_SYSRQ
  342. static void drm_fb_helper_restore_work_fn(struct work_struct *ignored)
  343. {
  344. drm_fb_helper_restore();
  345. }
  346. static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn);
  347. static void drm_fb_helper_sysrq(int dummy1)
  348. {
  349. schedule_work(&drm_fb_helper_restore_work);
  350. }
  351. static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = {
  352. .handler = drm_fb_helper_sysrq,
  353. .help_msg = "force-fb(V)",
  354. .action_msg = "Restore framebuffer console",
  355. };
  356. #else
  357. static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { };
  358. #endif
  359. static void drm_fb_helper_on(struct fb_info *info)
  360. {
  361. struct drm_fb_helper *fb_helper = info->par;
  362. struct drm_device *dev = fb_helper->dev;
  363. struct drm_crtc *crtc;
  364. struct drm_crtc_helper_funcs *crtc_funcs;
  365. struct drm_connector *connector;
  366. struct drm_encoder *encoder;
  367. int i, j;
  368. /*
  369. * For each CRTC in this fb, turn the crtc on then,
  370. * find all associated encoders and turn them on.
  371. */
  372. mutex_lock(&dev->mode_config.mutex);
  373. for (i = 0; i < fb_helper->crtc_count; i++) {
  374. crtc = fb_helper->crtc_info[i].mode_set.crtc;
  375. crtc_funcs = crtc->helper_private;
  376. if (!crtc->enabled)
  377. continue;
  378. crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
  379. /* Walk the connectors & encoders on this fb turning them on */
  380. for (j = 0; j < fb_helper->connector_count; j++) {
  381. connector = fb_helper->connector_info[j]->connector;
  382. connector->dpms = DRM_MODE_DPMS_ON;
  383. drm_connector_property_set_value(connector,
  384. dev->mode_config.dpms_property,
  385. DRM_MODE_DPMS_ON);
  386. }
  387. /* Found a CRTC on this fb, now find encoders */
  388. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  389. if (encoder->crtc == crtc) {
  390. struct drm_encoder_helper_funcs *encoder_funcs;
  391. encoder_funcs = encoder->helper_private;
  392. encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
  393. }
  394. }
  395. }
  396. mutex_unlock(&dev->mode_config.mutex);
  397. }
  398. static void drm_fb_helper_off(struct fb_info *info, int dpms_mode)
  399. {
  400. struct drm_fb_helper *fb_helper = info->par;
  401. struct drm_device *dev = fb_helper->dev;
  402. struct drm_crtc *crtc;
  403. struct drm_crtc_helper_funcs *crtc_funcs;
  404. struct drm_connector *connector;
  405. struct drm_encoder *encoder;
  406. int i, j;
  407. /*
  408. * For each CRTC in this fb, find all associated encoders
  409. * and turn them off, then turn off the CRTC.
  410. */
  411. mutex_lock(&dev->mode_config.mutex);
  412. for (i = 0; i < fb_helper->crtc_count; i++) {
  413. crtc = fb_helper->crtc_info[i].mode_set.crtc;
  414. crtc_funcs = crtc->helper_private;
  415. if (!crtc->enabled)
  416. continue;
  417. /* Walk the connectors on this fb and mark them off */
  418. for (j = 0; j < fb_helper->connector_count; j++) {
  419. connector = fb_helper->connector_info[j]->connector;
  420. connector->dpms = dpms_mode;
  421. drm_connector_property_set_value(connector,
  422. dev->mode_config.dpms_property,
  423. dpms_mode);
  424. }
  425. /* Found a CRTC on this fb, now find encoders */
  426. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  427. if (encoder->crtc == crtc) {
  428. struct drm_encoder_helper_funcs *encoder_funcs;
  429. encoder_funcs = encoder->helper_private;
  430. encoder_funcs->dpms(encoder, dpms_mode);
  431. }
  432. }
  433. crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
  434. }
  435. mutex_unlock(&dev->mode_config.mutex);
  436. }
  437. int drm_fb_helper_blank(int blank, struct fb_info *info)
  438. {
  439. switch (blank) {
  440. /* Display: On; HSync: On, VSync: On */
  441. case FB_BLANK_UNBLANK:
  442. drm_fb_helper_on(info);
  443. break;
  444. /* Display: Off; HSync: On, VSync: On */
  445. case FB_BLANK_NORMAL:
  446. drm_fb_helper_off(info, DRM_MODE_DPMS_STANDBY);
  447. break;
  448. /* Display: Off; HSync: Off, VSync: On */
  449. case FB_BLANK_HSYNC_SUSPEND:
  450. drm_fb_helper_off(info, DRM_MODE_DPMS_STANDBY);
  451. break;
  452. /* Display: Off; HSync: On, VSync: Off */
  453. case FB_BLANK_VSYNC_SUSPEND:
  454. drm_fb_helper_off(info, DRM_MODE_DPMS_SUSPEND);
  455. break;
  456. /* Display: Off; HSync: Off, VSync: Off */
  457. case FB_BLANK_POWERDOWN:
  458. drm_fb_helper_off(info, DRM_MODE_DPMS_OFF);
  459. break;
  460. }
  461. return 0;
  462. }
  463. EXPORT_SYMBOL(drm_fb_helper_blank);
  464. static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper)
  465. {
  466. int i;
  467. for (i = 0; i < helper->connector_count; i++)
  468. kfree(helper->connector_info[i]);
  469. kfree(helper->connector_info);
  470. for (i = 0; i < helper->crtc_count; i++)
  471. kfree(helper->crtc_info[i].mode_set.connectors);
  472. kfree(helper->crtc_info);
  473. }
  474. int drm_fb_helper_init(struct drm_device *dev,
  475. struct drm_fb_helper *fb_helper,
  476. int crtc_count, int max_conn_count)
  477. {
  478. struct drm_crtc *crtc;
  479. int ret = 0;
  480. int i;
  481. fb_helper->dev = dev;
  482. INIT_LIST_HEAD(&fb_helper->kernel_fb_list);
  483. fb_helper->crtc_info = kcalloc(crtc_count, sizeof(struct drm_fb_helper_crtc), GFP_KERNEL);
  484. if (!fb_helper->crtc_info)
  485. return -ENOMEM;
  486. fb_helper->crtc_count = crtc_count;
  487. fb_helper->connector_info = kcalloc(dev->mode_config.num_connector, sizeof(struct drm_fb_helper_connector *), GFP_KERNEL);
  488. if (!fb_helper->connector_info) {
  489. kfree(fb_helper->crtc_info);
  490. return -ENOMEM;
  491. }
  492. fb_helper->connector_count = 0;
  493. for (i = 0; i < crtc_count; i++) {
  494. fb_helper->crtc_info[i].mode_set.connectors =
  495. kcalloc(max_conn_count,
  496. sizeof(struct drm_connector *),
  497. GFP_KERNEL);
  498. if (!fb_helper->crtc_info[i].mode_set.connectors) {
  499. ret = -ENOMEM;
  500. goto out_free;
  501. }
  502. fb_helper->crtc_info[i].mode_set.num_connectors = 0;
  503. }
  504. i = 0;
  505. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  506. fb_helper->crtc_info[i].crtc_id = crtc->base.id;
  507. fb_helper->crtc_info[i].mode_set.crtc = crtc;
  508. i++;
  509. }
  510. fb_helper->conn_limit = max_conn_count;
  511. return 0;
  512. out_free:
  513. drm_fb_helper_crtc_free(fb_helper);
  514. return -ENOMEM;
  515. }
  516. EXPORT_SYMBOL(drm_fb_helper_init);
  517. void drm_fb_helper_fini(struct drm_fb_helper *fb_helper)
  518. {
  519. if (!list_empty(&fb_helper->kernel_fb_list)) {
  520. list_del(&fb_helper->kernel_fb_list);
  521. if (list_empty(&kernel_fb_helper_list)) {
  522. printk(KERN_INFO "drm: unregistered panic notifier\n");
  523. atomic_notifier_chain_unregister(&panic_notifier_list,
  524. &paniced);
  525. unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
  526. }
  527. }
  528. drm_fb_helper_crtc_free(fb_helper);
  529. }
  530. EXPORT_SYMBOL(drm_fb_helper_fini);
  531. static int setcolreg(struct drm_crtc *crtc, u16 red, u16 green,
  532. u16 blue, u16 regno, struct fb_info *info)
  533. {
  534. struct drm_fb_helper *fb_helper = info->par;
  535. struct drm_framebuffer *fb = fb_helper->fb;
  536. int pindex;
  537. if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
  538. u32 *palette;
  539. u32 value;
  540. /* place color in psuedopalette */
  541. if (regno > 16)
  542. return -EINVAL;
  543. palette = (u32 *)info->pseudo_palette;
  544. red >>= (16 - info->var.red.length);
  545. green >>= (16 - info->var.green.length);
  546. blue >>= (16 - info->var.blue.length);
  547. value = (red << info->var.red.offset) |
  548. (green << info->var.green.offset) |
  549. (blue << info->var.blue.offset);
  550. palette[regno] = value;
  551. return 0;
  552. }
  553. pindex = regno;
  554. if (fb->bits_per_pixel == 16) {
  555. pindex = regno << 3;
  556. if (fb->depth == 16 && regno > 63)
  557. return -EINVAL;
  558. if (fb->depth == 15 && regno > 31)
  559. return -EINVAL;
  560. if (fb->depth == 16) {
  561. u16 r, g, b;
  562. int i;
  563. if (regno < 32) {
  564. for (i = 0; i < 8; i++)
  565. fb_helper->funcs->gamma_set(crtc, red,
  566. green, blue, pindex + i);
  567. }
  568. fb_helper->funcs->gamma_get(crtc, &r,
  569. &g, &b,
  570. pindex >> 1);
  571. for (i = 0; i < 4; i++)
  572. fb_helper->funcs->gamma_set(crtc, r,
  573. green, b,
  574. (pindex >> 1) + i);
  575. }
  576. }
  577. if (fb->depth != 16)
  578. fb_helper->funcs->gamma_set(crtc, red, green, blue, pindex);
  579. return 0;
  580. }
  581. int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info)
  582. {
  583. struct drm_fb_helper *fb_helper = info->par;
  584. struct drm_crtc_helper_funcs *crtc_funcs;
  585. u16 *red, *green, *blue, *transp;
  586. struct drm_crtc *crtc;
  587. int i, rc = 0;
  588. int start;
  589. for (i = 0; i < fb_helper->crtc_count; i++) {
  590. crtc = fb_helper->crtc_info[i].mode_set.crtc;
  591. crtc_funcs = crtc->helper_private;
  592. red = cmap->red;
  593. green = cmap->green;
  594. blue = cmap->blue;
  595. transp = cmap->transp;
  596. start = cmap->start;
  597. for (i = 0; i < cmap->len; i++) {
  598. u16 hred, hgreen, hblue, htransp = 0xffff;
  599. hred = *red++;
  600. hgreen = *green++;
  601. hblue = *blue++;
  602. if (transp)
  603. htransp = *transp++;
  604. rc = setcolreg(crtc, hred, hgreen, hblue, start++, info);
  605. if (rc)
  606. return rc;
  607. }
  608. crtc_funcs->load_lut(crtc);
  609. }
  610. return rc;
  611. }
  612. EXPORT_SYMBOL(drm_fb_helper_setcmap);
  613. int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
  614. struct fb_info *info)
  615. {
  616. struct drm_fb_helper *fb_helper = info->par;
  617. struct drm_framebuffer *fb = fb_helper->fb;
  618. int depth;
  619. if (var->pixclock != 0 || in_dbg_master())
  620. return -EINVAL;
  621. /* Need to resize the fb object !!! */
  622. if (var->bits_per_pixel > fb->bits_per_pixel || var->xres > fb->width || var->yres > fb->height) {
  623. DRM_DEBUG("fb userspace requested width/height/bpp is greater than current fb "
  624. "object %dx%d-%d > %dx%d-%d\n", var->xres, var->yres, var->bits_per_pixel,
  625. fb->width, fb->height, fb->bits_per_pixel);
  626. return -EINVAL;
  627. }
  628. switch (var->bits_per_pixel) {
  629. case 16:
  630. depth = (var->green.length == 6) ? 16 : 15;
  631. break;
  632. case 32:
  633. depth = (var->transp.length > 0) ? 32 : 24;
  634. break;
  635. default:
  636. depth = var->bits_per_pixel;
  637. break;
  638. }
  639. switch (depth) {
  640. case 8:
  641. var->red.offset = 0;
  642. var->green.offset = 0;
  643. var->blue.offset = 0;
  644. var->red.length = 8;
  645. var->green.length = 8;
  646. var->blue.length = 8;
  647. var->transp.length = 0;
  648. var->transp.offset = 0;
  649. break;
  650. case 15:
  651. var->red.offset = 10;
  652. var->green.offset = 5;
  653. var->blue.offset = 0;
  654. var->red.length = 5;
  655. var->green.length = 5;
  656. var->blue.length = 5;
  657. var->transp.length = 1;
  658. var->transp.offset = 15;
  659. break;
  660. case 16:
  661. var->red.offset = 11;
  662. var->green.offset = 5;
  663. var->blue.offset = 0;
  664. var->red.length = 5;
  665. var->green.length = 6;
  666. var->blue.length = 5;
  667. var->transp.length = 0;
  668. var->transp.offset = 0;
  669. break;
  670. case 24:
  671. var->red.offset = 16;
  672. var->green.offset = 8;
  673. var->blue.offset = 0;
  674. var->red.length = 8;
  675. var->green.length = 8;
  676. var->blue.length = 8;
  677. var->transp.length = 0;
  678. var->transp.offset = 0;
  679. break;
  680. case 32:
  681. var->red.offset = 16;
  682. var->green.offset = 8;
  683. var->blue.offset = 0;
  684. var->red.length = 8;
  685. var->green.length = 8;
  686. var->blue.length = 8;
  687. var->transp.length = 8;
  688. var->transp.offset = 24;
  689. break;
  690. default:
  691. return -EINVAL;
  692. }
  693. return 0;
  694. }
  695. EXPORT_SYMBOL(drm_fb_helper_check_var);
  696. /* this will let fbcon do the mode init */
  697. int drm_fb_helper_set_par(struct fb_info *info)
  698. {
  699. struct drm_fb_helper *fb_helper = info->par;
  700. struct drm_device *dev = fb_helper->dev;
  701. struct fb_var_screeninfo *var = &info->var;
  702. struct drm_crtc *crtc;
  703. int ret;
  704. int i;
  705. if (var->pixclock != 0) {
  706. DRM_ERROR("PIXEL CLOCK SET\n");
  707. return -EINVAL;
  708. }
  709. mutex_lock(&dev->mode_config.mutex);
  710. for (i = 0; i < fb_helper->crtc_count; i++) {
  711. crtc = fb_helper->crtc_info[i].mode_set.crtc;
  712. ret = crtc->funcs->set_config(&fb_helper->crtc_info[i].mode_set);
  713. if (ret) {
  714. mutex_unlock(&dev->mode_config.mutex);
  715. return ret;
  716. }
  717. }
  718. mutex_unlock(&dev->mode_config.mutex);
  719. if (fb_helper->delayed_hotplug) {
  720. fb_helper->delayed_hotplug = false;
  721. drm_fb_helper_hotplug_event(fb_helper);
  722. }
  723. return 0;
  724. }
  725. EXPORT_SYMBOL(drm_fb_helper_set_par);
  726. int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
  727. struct fb_info *info)
  728. {
  729. struct drm_fb_helper *fb_helper = info->par;
  730. struct drm_device *dev = fb_helper->dev;
  731. struct drm_mode_set *modeset;
  732. struct drm_crtc *crtc;
  733. int ret = 0;
  734. int i;
  735. mutex_lock(&dev->mode_config.mutex);
  736. for (i = 0; i < fb_helper->crtc_count; i++) {
  737. crtc = fb_helper->crtc_info[i].mode_set.crtc;
  738. modeset = &fb_helper->crtc_info[i].mode_set;
  739. modeset->x = var->xoffset;
  740. modeset->y = var->yoffset;
  741. if (modeset->num_connectors) {
  742. ret = crtc->funcs->set_config(modeset);
  743. if (!ret) {
  744. info->var.xoffset = var->xoffset;
  745. info->var.yoffset = var->yoffset;
  746. }
  747. }
  748. }
  749. mutex_unlock(&dev->mode_config.mutex);
  750. return ret;
  751. }
  752. EXPORT_SYMBOL(drm_fb_helper_pan_display);
  753. int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
  754. int preferred_bpp)
  755. {
  756. int new_fb = 0;
  757. int crtc_count = 0;
  758. int i;
  759. struct fb_info *info;
  760. struct drm_fb_helper_surface_size sizes;
  761. int gamma_size = 0;
  762. memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size));
  763. sizes.surface_depth = 24;
  764. sizes.surface_bpp = 32;
  765. sizes.fb_width = (unsigned)-1;
  766. sizes.fb_height = (unsigned)-1;
  767. /* if driver picks 8 or 16 by default use that
  768. for both depth/bpp */
  769. if (preferred_bpp != sizes.surface_bpp) {
  770. sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
  771. }
  772. /* first up get a count of crtcs now in use and new min/maxes width/heights */
  773. for (i = 0; i < fb_helper->connector_count; i++) {
  774. struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i];
  775. struct drm_fb_helper_cmdline_mode *cmdline_mode;
  776. cmdline_mode = &fb_helper_conn->cmdline_mode;
  777. if (cmdline_mode->bpp_specified) {
  778. switch (cmdline_mode->bpp) {
  779. case 8:
  780. sizes.surface_depth = sizes.surface_bpp = 8;
  781. break;
  782. case 15:
  783. sizes.surface_depth = 15;
  784. sizes.surface_bpp = 16;
  785. break;
  786. case 16:
  787. sizes.surface_depth = sizes.surface_bpp = 16;
  788. break;
  789. case 24:
  790. sizes.surface_depth = sizes.surface_bpp = 24;
  791. break;
  792. case 32:
  793. sizes.surface_depth = 24;
  794. sizes.surface_bpp = 32;
  795. break;
  796. }
  797. break;
  798. }
  799. }
  800. crtc_count = 0;
  801. for (i = 0; i < fb_helper->crtc_count; i++) {
  802. struct drm_display_mode *desired_mode;
  803. desired_mode = fb_helper->crtc_info[i].desired_mode;
  804. if (desired_mode) {
  805. if (gamma_size == 0)
  806. gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size;
  807. if (desired_mode->hdisplay < sizes.fb_width)
  808. sizes.fb_width = desired_mode->hdisplay;
  809. if (desired_mode->vdisplay < sizes.fb_height)
  810. sizes.fb_height = desired_mode->vdisplay;
  811. if (desired_mode->hdisplay > sizes.surface_width)
  812. sizes.surface_width = desired_mode->hdisplay;
  813. if (desired_mode->vdisplay > sizes.surface_height)
  814. sizes.surface_height = desired_mode->vdisplay;
  815. crtc_count++;
  816. }
  817. }
  818. if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) {
  819. /* hmm everyone went away - assume VGA cable just fell out
  820. and will come back later. */
  821. DRM_INFO("Cannot find any crtc or sizes - going 1024x768\n");
  822. sizes.fb_width = sizes.surface_width = 1024;
  823. sizes.fb_height = sizes.surface_height = 768;
  824. }
  825. /* push down into drivers */
  826. new_fb = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes);
  827. if (new_fb < 0)
  828. return new_fb;
  829. info = fb_helper->fbdev;
  830. /* set the fb pointer */
  831. for (i = 0; i < fb_helper->crtc_count; i++) {
  832. fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb;
  833. }
  834. if (new_fb) {
  835. info->var.pixclock = 0;
  836. if (register_framebuffer(info) < 0) {
  837. return -EINVAL;
  838. }
  839. printk(KERN_INFO "fb%d: %s frame buffer device\n", info->node,
  840. info->fix.id);
  841. } else {
  842. drm_fb_helper_set_par(info);
  843. }
  844. /* Switch back to kernel console on panic */
  845. /* multi card linked list maybe */
  846. if (list_empty(&kernel_fb_helper_list)) {
  847. printk(KERN_INFO "drm: registered panic notifier\n");
  848. atomic_notifier_chain_register(&panic_notifier_list,
  849. &paniced);
  850. register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
  851. }
  852. if (new_fb)
  853. list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
  854. return 0;
  855. }
  856. EXPORT_SYMBOL(drm_fb_helper_single_fb_probe);
  857. void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
  858. uint32_t depth)
  859. {
  860. info->fix.type = FB_TYPE_PACKED_PIXELS;
  861. info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR :
  862. FB_VISUAL_TRUECOLOR;
  863. info->fix.type_aux = 0;
  864. info->fix.xpanstep = 1; /* doing it in hw */
  865. info->fix.ypanstep = 1; /* doing it in hw */
  866. info->fix.ywrapstep = 0;
  867. info->fix.accel = FB_ACCEL_NONE;
  868. info->fix.type_aux = 0;
  869. info->fix.line_length = pitch;
  870. return;
  871. }
  872. EXPORT_SYMBOL(drm_fb_helper_fill_fix);
  873. void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper,
  874. uint32_t fb_width, uint32_t fb_height)
  875. {
  876. struct drm_framebuffer *fb = fb_helper->fb;
  877. info->pseudo_palette = fb_helper->pseudo_palette;
  878. info->var.xres_virtual = fb->width;
  879. info->var.yres_virtual = fb->height;
  880. info->var.bits_per_pixel = fb->bits_per_pixel;
  881. info->var.xoffset = 0;
  882. info->var.yoffset = 0;
  883. info->var.activate = FB_ACTIVATE_NOW;
  884. info->var.height = -1;
  885. info->var.width = -1;
  886. switch (fb->depth) {
  887. case 8:
  888. info->var.red.offset = 0;
  889. info->var.green.offset = 0;
  890. info->var.blue.offset = 0;
  891. info->var.red.length = 8; /* 8bit DAC */
  892. info->var.green.length = 8;
  893. info->var.blue.length = 8;
  894. info->var.transp.offset = 0;
  895. info->var.transp.length = 0;
  896. break;
  897. case 15:
  898. info->var.red.offset = 10;
  899. info->var.green.offset = 5;
  900. info->var.blue.offset = 0;
  901. info->var.red.length = 5;
  902. info->var.green.length = 5;
  903. info->var.blue.length = 5;
  904. info->var.transp.offset = 15;
  905. info->var.transp.length = 1;
  906. break;
  907. case 16:
  908. info->var.red.offset = 11;
  909. info->var.green.offset = 5;
  910. info->var.blue.offset = 0;
  911. info->var.red.length = 5;
  912. info->var.green.length = 6;
  913. info->var.blue.length = 5;
  914. info->var.transp.offset = 0;
  915. break;
  916. case 24:
  917. info->var.red.offset = 16;
  918. info->var.green.offset = 8;
  919. info->var.blue.offset = 0;
  920. info->var.red.length = 8;
  921. info->var.green.length = 8;
  922. info->var.blue.length = 8;
  923. info->var.transp.offset = 0;
  924. info->var.transp.length = 0;
  925. break;
  926. case 32:
  927. info->var.red.offset = 16;
  928. info->var.green.offset = 8;
  929. info->var.blue.offset = 0;
  930. info->var.red.length = 8;
  931. info->var.green.length = 8;
  932. info->var.blue.length = 8;
  933. info->var.transp.offset = 24;
  934. info->var.transp.length = 8;
  935. break;
  936. default:
  937. break;
  938. }
  939. info->var.xres = fb_width;
  940. info->var.yres = fb_height;
  941. }
  942. EXPORT_SYMBOL(drm_fb_helper_fill_var);
  943. static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper,
  944. uint32_t maxX,
  945. uint32_t maxY)
  946. {
  947. struct drm_connector *connector;
  948. int count = 0;
  949. int i;
  950. for (i = 0; i < fb_helper->connector_count; i++) {
  951. connector = fb_helper->connector_info[i]->connector;
  952. count += connector->funcs->fill_modes(connector, maxX, maxY);
  953. }
  954. return count;
  955. }
  956. static struct drm_display_mode *drm_has_preferred_mode(struct drm_fb_helper_connector *fb_connector, int width, int height)
  957. {
  958. struct drm_display_mode *mode;
  959. list_for_each_entry(mode, &fb_connector->connector->modes, head) {
  960. if (drm_mode_width(mode) > width ||
  961. drm_mode_height(mode) > height)
  962. continue;
  963. if (mode->type & DRM_MODE_TYPE_PREFERRED)
  964. return mode;
  965. }
  966. return NULL;
  967. }
  968. static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector)
  969. {
  970. struct drm_fb_helper_cmdline_mode *cmdline_mode;
  971. cmdline_mode = &fb_connector->cmdline_mode;
  972. return cmdline_mode->specified;
  973. }
  974. static struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn,
  975. int width, int height)
  976. {
  977. struct drm_fb_helper_cmdline_mode *cmdline_mode;
  978. struct drm_display_mode *mode = NULL;
  979. cmdline_mode = &fb_helper_conn->cmdline_mode;
  980. if (cmdline_mode->specified == false)
  981. return mode;
  982. /* attempt to find a matching mode in the list of modes
  983. * we have gotten so far, if not add a CVT mode that conforms
  984. */
  985. if (cmdline_mode->rb || cmdline_mode->margins)
  986. goto create_mode;
  987. list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
  988. /* check width/height */
  989. if (mode->hdisplay != cmdline_mode->xres ||
  990. mode->vdisplay != cmdline_mode->yres)
  991. continue;
  992. if (cmdline_mode->refresh_specified) {
  993. if (mode->vrefresh != cmdline_mode->refresh)
  994. continue;
  995. }
  996. if (cmdline_mode->interlace) {
  997. if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
  998. continue;
  999. }
  1000. return mode;
  1001. }
  1002. create_mode:
  1003. if (cmdline_mode->cvt)
  1004. mode = drm_cvt_mode(fb_helper_conn->connector->dev,
  1005. cmdline_mode->xres, cmdline_mode->yres,
  1006. cmdline_mode->refresh_specified ? cmdline_mode->refresh : 60,
  1007. cmdline_mode->rb, cmdline_mode->interlace,
  1008. cmdline_mode->margins);
  1009. else
  1010. mode = drm_gtf_mode(fb_helper_conn->connector->dev,
  1011. cmdline_mode->xres, cmdline_mode->yres,
  1012. cmdline_mode->refresh_specified ? cmdline_mode->refresh : 60,
  1013. cmdline_mode->interlace,
  1014. cmdline_mode->margins);
  1015. drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
  1016. list_add(&mode->head, &fb_helper_conn->connector->modes);
  1017. return mode;
  1018. }
  1019. static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
  1020. {
  1021. bool enable;
  1022. if (strict) {
  1023. enable = connector->status == connector_status_connected;
  1024. } else {
  1025. enable = connector->status != connector_status_disconnected;
  1026. }
  1027. return enable;
  1028. }
  1029. static void drm_enable_connectors(struct drm_fb_helper *fb_helper,
  1030. bool *enabled)
  1031. {
  1032. bool any_enabled = false;
  1033. struct drm_connector *connector;
  1034. int i = 0;
  1035. for (i = 0; i < fb_helper->connector_count; i++) {
  1036. connector = fb_helper->connector_info[i]->connector;
  1037. enabled[i] = drm_connector_enabled(connector, true);
  1038. DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
  1039. enabled[i] ? "yes" : "no");
  1040. any_enabled |= enabled[i];
  1041. }
  1042. if (any_enabled)
  1043. return;
  1044. for (i = 0; i < fb_helper->connector_count; i++) {
  1045. connector = fb_helper->connector_info[i]->connector;
  1046. enabled[i] = drm_connector_enabled(connector, false);
  1047. }
  1048. }
  1049. static bool drm_target_cloned(struct drm_fb_helper *fb_helper,
  1050. struct drm_display_mode **modes,
  1051. bool *enabled, int width, int height)
  1052. {
  1053. int count, i, j;
  1054. bool can_clone = false;
  1055. struct drm_fb_helper_connector *fb_helper_conn;
  1056. struct drm_display_mode *dmt_mode, *mode;
  1057. /* only contemplate cloning in the single crtc case */
  1058. if (fb_helper->crtc_count > 1)
  1059. return false;
  1060. count = 0;
  1061. for (i = 0; i < fb_helper->connector_count; i++) {
  1062. if (enabled[i])
  1063. count++;
  1064. }
  1065. /* only contemplate cloning if more than one connector is enabled */
  1066. if (count <= 1)
  1067. return false;
  1068. /* check the command line or if nothing common pick 1024x768 */
  1069. can_clone = true;
  1070. for (i = 0; i < fb_helper->connector_count; i++) {
  1071. if (!enabled[i])
  1072. continue;
  1073. fb_helper_conn = fb_helper->connector_info[i];
  1074. modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
  1075. if (!modes[i]) {
  1076. can_clone = false;
  1077. break;
  1078. }
  1079. for (j = 0; j < i; j++) {
  1080. if (!enabled[j])
  1081. continue;
  1082. if (!drm_mode_equal(modes[j], modes[i]))
  1083. can_clone = false;
  1084. }
  1085. }
  1086. if (can_clone) {
  1087. DRM_DEBUG_KMS("can clone using command line\n");
  1088. return true;
  1089. }
  1090. /* try and find a 1024x768 mode on each connector */
  1091. can_clone = true;
  1092. dmt_mode = drm_mode_find_dmt(fb_helper->dev, 1024, 768, 60);
  1093. for (i = 0; i < fb_helper->connector_count; i++) {
  1094. if (!enabled[i])
  1095. continue;
  1096. fb_helper_conn = fb_helper->connector_info[i];
  1097. list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
  1098. if (drm_mode_equal(mode, dmt_mode))
  1099. modes[i] = mode;
  1100. }
  1101. if (!modes[i])
  1102. can_clone = false;
  1103. }
  1104. if (can_clone) {
  1105. DRM_DEBUG_KMS("can clone using 1024x768\n");
  1106. return true;
  1107. }
  1108. DRM_INFO("kms: can't enable cloning when we probably wanted to.\n");
  1109. return false;
  1110. }
  1111. static bool drm_target_preferred(struct drm_fb_helper *fb_helper,
  1112. struct drm_display_mode **modes,
  1113. bool *enabled, int width, int height)
  1114. {
  1115. struct drm_fb_helper_connector *fb_helper_conn;
  1116. int i;
  1117. for (i = 0; i < fb_helper->connector_count; i++) {
  1118. fb_helper_conn = fb_helper->connector_info[i];
  1119. if (enabled[i] == false)
  1120. continue;
  1121. DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
  1122. fb_helper_conn->connector->base.id);
  1123. /* got for command line mode first */
  1124. modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
  1125. if (!modes[i]) {
  1126. DRM_DEBUG_KMS("looking for preferred mode on connector %d\n",
  1127. fb_helper_conn->connector->base.id);
  1128. modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height);
  1129. }
  1130. /* No preferred modes, pick one off the list */
  1131. if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) {
  1132. list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head)
  1133. break;
  1134. }
  1135. DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
  1136. "none");
  1137. }
  1138. return true;
  1139. }
  1140. static int drm_pick_crtcs(struct drm_fb_helper *fb_helper,
  1141. struct drm_fb_helper_crtc **best_crtcs,
  1142. struct drm_display_mode **modes,
  1143. int n, int width, int height)
  1144. {
  1145. int c, o;
  1146. struct drm_device *dev = fb_helper->dev;
  1147. struct drm_connector *connector;
  1148. struct drm_connector_helper_funcs *connector_funcs;
  1149. struct drm_encoder *encoder;
  1150. struct drm_fb_helper_crtc *best_crtc;
  1151. int my_score, best_score, score;
  1152. struct drm_fb_helper_crtc **crtcs, *crtc;
  1153. struct drm_fb_helper_connector *fb_helper_conn;
  1154. if (n == fb_helper->connector_count)
  1155. return 0;
  1156. fb_helper_conn = fb_helper->connector_info[n];
  1157. connector = fb_helper_conn->connector;
  1158. best_crtcs[n] = NULL;
  1159. best_crtc = NULL;
  1160. best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height);
  1161. if (modes[n] == NULL)
  1162. return best_score;
  1163. crtcs = kzalloc(dev->mode_config.num_connector *
  1164. sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
  1165. if (!crtcs)
  1166. return best_score;
  1167. my_score = 1;
  1168. if (connector->status == connector_status_connected)
  1169. my_score++;
  1170. if (drm_has_cmdline_mode(fb_helper_conn))
  1171. my_score++;
  1172. if (drm_has_preferred_mode(fb_helper_conn, width, height))
  1173. my_score++;
  1174. connector_funcs = connector->helper_private;
  1175. encoder = connector_funcs->best_encoder(connector);
  1176. if (!encoder)
  1177. goto out;
  1178. /* select a crtc for this connector and then attempt to configure
  1179. remaining connectors */
  1180. for (c = 0; c < fb_helper->crtc_count; c++) {
  1181. crtc = &fb_helper->crtc_info[c];
  1182. if ((encoder->possible_crtcs & (1 << c)) == 0) {
  1183. continue;
  1184. }
  1185. for (o = 0; o < n; o++)
  1186. if (best_crtcs[o] == crtc)
  1187. break;
  1188. if (o < n) {
  1189. /* ignore cloning unless only a single crtc */
  1190. if (fb_helper->crtc_count > 1)
  1191. continue;
  1192. if (!drm_mode_equal(modes[o], modes[n]))
  1193. continue;
  1194. }
  1195. crtcs[n] = crtc;
  1196. memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *));
  1197. score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1,
  1198. width, height);
  1199. if (score > best_score) {
  1200. best_crtc = crtc;
  1201. best_score = score;
  1202. memcpy(best_crtcs, crtcs,
  1203. dev->mode_config.num_connector *
  1204. sizeof(struct drm_fb_helper_crtc *));
  1205. }
  1206. }
  1207. out:
  1208. kfree(crtcs);
  1209. return best_score;
  1210. }
  1211. static void drm_setup_crtcs(struct drm_fb_helper *fb_helper)
  1212. {
  1213. struct drm_device *dev = fb_helper->dev;
  1214. struct drm_fb_helper_crtc **crtcs;
  1215. struct drm_display_mode **modes;
  1216. struct drm_encoder *encoder;
  1217. struct drm_mode_set *modeset;
  1218. bool *enabled;
  1219. int width, height;
  1220. int i, ret;
  1221. DRM_DEBUG_KMS("\n");
  1222. width = dev->mode_config.max_width;
  1223. height = dev->mode_config.max_height;
  1224. /* clean out all the encoder/crtc combos */
  1225. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  1226. encoder->crtc = NULL;
  1227. }
  1228. crtcs = kcalloc(dev->mode_config.num_connector,
  1229. sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
  1230. modes = kcalloc(dev->mode_config.num_connector,
  1231. sizeof(struct drm_display_mode *), GFP_KERNEL);
  1232. enabled = kcalloc(dev->mode_config.num_connector,
  1233. sizeof(bool), GFP_KERNEL);
  1234. drm_enable_connectors(fb_helper, enabled);
  1235. ret = drm_target_cloned(fb_helper, modes, enabled, width, height);
  1236. if (!ret) {
  1237. ret = drm_target_preferred(fb_helper, modes, enabled, width, height);
  1238. if (!ret)
  1239. DRM_ERROR("Unable to find initial modes\n");
  1240. }
  1241. DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n", width, height);
  1242. drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height);
  1243. /* need to set the modesets up here for use later */
  1244. /* fill out the connector<->crtc mappings into the modesets */
  1245. for (i = 0; i < fb_helper->crtc_count; i++) {
  1246. modeset = &fb_helper->crtc_info[i].mode_set;
  1247. modeset->num_connectors = 0;
  1248. }
  1249. for (i = 0; i < fb_helper->connector_count; i++) {
  1250. struct drm_display_mode *mode = modes[i];
  1251. struct drm_fb_helper_crtc *fb_crtc = crtcs[i];
  1252. modeset = &fb_crtc->mode_set;
  1253. if (mode && fb_crtc) {
  1254. DRM_DEBUG_KMS("desired mode %s set on crtc %d\n",
  1255. mode->name, fb_crtc->mode_set.crtc->base.id);
  1256. fb_crtc->desired_mode = mode;
  1257. if (modeset->mode)
  1258. drm_mode_destroy(dev, modeset->mode);
  1259. modeset->mode = drm_mode_duplicate(dev,
  1260. fb_crtc->desired_mode);
  1261. modeset->connectors[modeset->num_connectors++] = fb_helper->connector_info[i]->connector;
  1262. }
  1263. }
  1264. kfree(crtcs);
  1265. kfree(modes);
  1266. kfree(enabled);
  1267. }
  1268. /**
  1269. * drm_helper_initial_config - setup a sane initial connector configuration
  1270. * @dev: DRM device
  1271. *
  1272. * LOCKING:
  1273. * Called at init time, must take mode config lock.
  1274. *
  1275. * Scan the CRTCs and connectors and try to put together an initial setup.
  1276. * At the moment, this is a cloned configuration across all heads with
  1277. * a new framebuffer object as the backing store.
  1278. *
  1279. * RETURNS:
  1280. * Zero if everything went ok, nonzero otherwise.
  1281. */
  1282. bool drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel)
  1283. {
  1284. struct drm_device *dev = fb_helper->dev;
  1285. int count = 0;
  1286. /* disable all the possible outputs/crtcs before entering KMS mode */
  1287. drm_helper_disable_unused_functions(fb_helper->dev);
  1288. drm_fb_helper_parse_command_line(fb_helper);
  1289. count = drm_fb_helper_probe_connector_modes(fb_helper,
  1290. dev->mode_config.max_width,
  1291. dev->mode_config.max_height);
  1292. /*
  1293. * we shouldn't end up with no modes here.
  1294. */
  1295. if (count == 0) {
  1296. printk(KERN_INFO "No connectors reported connected with modes\n");
  1297. }
  1298. drm_setup_crtcs(fb_helper);
  1299. return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
  1300. }
  1301. EXPORT_SYMBOL(drm_fb_helper_initial_config);
  1302. bool drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper)
  1303. {
  1304. int count = 0;
  1305. u32 max_width, max_height, bpp_sel;
  1306. bool bound = false, crtcs_bound = false;
  1307. struct drm_crtc *crtc;
  1308. if (!fb_helper->fb)
  1309. return false;
  1310. list_for_each_entry(crtc, &fb_helper->dev->mode_config.crtc_list, head) {
  1311. if (crtc->fb)
  1312. crtcs_bound = true;
  1313. if (crtc->fb == fb_helper->fb)
  1314. bound = true;
  1315. }
  1316. if (!bound && crtcs_bound) {
  1317. fb_helper->delayed_hotplug = true;
  1318. return false;
  1319. }
  1320. DRM_DEBUG_KMS("\n");
  1321. max_width = fb_helper->fb->width;
  1322. max_height = fb_helper->fb->height;
  1323. bpp_sel = fb_helper->fb->bits_per_pixel;
  1324. count = drm_fb_helper_probe_connector_modes(fb_helper, max_width,
  1325. max_height);
  1326. drm_setup_crtcs(fb_helper);
  1327. return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
  1328. }
  1329. EXPORT_SYMBOL(drm_fb_helper_hotplug_event);