drm_fb_helper.c 39 KB

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