drm_fb_helper.c 38 KB

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