drm_fb_helper.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  31. #include <linux/kernel.h>
  32. #include <linux/sysrq.h>
  33. #include <linux/slab.h>
  34. #include <linux/fb.h>
  35. #include <linux/module.h>
  36. #include <drm/drmP.h>
  37. #include <drm/drm_crtc.h>
  38. #include <drm/drm_fb_helper.h>
  39. #include <drm/drm_crtc_helper.h>
  40. static LIST_HEAD(kernel_fb_helper_list);
  41. /**
  42. * DOC: fbdev helpers
  43. *
  44. * The fb helper functions are useful to provide an fbdev on top of a drm kernel
  45. * mode setting driver. They can be used mostly independently from the crtc
  46. * helper functions used by many drivers to implement the kernel mode setting
  47. * interfaces.
  48. *
  49. * Initialization is done as a four-step process with drm_fb_helper_prepare(),
  50. * drm_fb_helper_init(), drm_fb_helper_single_add_all_connectors() and
  51. * drm_fb_helper_initial_config(). Drivers with fancier requirements than the
  52. * default behaviour can override the third step with their own code.
  53. * Teardown is done with drm_fb_helper_fini().
  54. *
  55. * At runtime drivers should restore the fbdev console by calling
  56. * drm_fb_helper_restore_fbdev_mode() from their ->lastclose callback. They
  57. * should also notify the fb helper code from updates to the output
  58. * configuration by calling drm_fb_helper_hotplug_event(). For easier
  59. * integration with the output polling code in drm_crtc_helper.c the modeset
  60. * code provides a ->output_poll_changed callback.
  61. *
  62. * All other functions exported by the fb helper library can be used to
  63. * implement the fbdev driver interface by the driver.
  64. *
  65. * It is possible, though perhaps somewhat tricky, to implement race-free
  66. * hotplug detection using the fbdev helpers. The drm_fb_helper_prepare()
  67. * helper must be called first to initialize the minimum required to make
  68. * hotplug detection work. Drivers also need to make sure to properly set up
  69. * the dev->mode_config.funcs member. After calling drm_kms_helper_poll_init()
  70. * it is safe to enable interrupts and start processing hotplug events. At the
  71. * same time, drivers should initialize all modeset objects such as CRTCs,
  72. * encoders and connectors. To finish up the fbdev helper initialization, the
  73. * drm_fb_helper_init() function is called. To probe for all attached displays
  74. * and set up an initial configuration using the detected hardware, drivers
  75. * should call drm_fb_helper_single_add_all_connectors() followed by
  76. * drm_fb_helper_initial_config().
  77. */
  78. /**
  79. * drm_fb_helper_single_add_all_connectors() - add all connectors to fbdev
  80. * emulation helper
  81. * @fb_helper: fbdev initialized with drm_fb_helper_init
  82. *
  83. * This functions adds all the available connectors for use with the given
  84. * fb_helper. This is a separate step to allow drivers to freely assign
  85. * connectors to the fbdev, e.g. if some are reserved for special purposes or
  86. * not adequate to be used for the fbcon.
  87. *
  88. * Since this is part of the initial setup before the fbdev is published, no
  89. * locking is required.
  90. */
  91. int drm_fb_helper_single_add_all_connectors(struct drm_fb_helper *fb_helper)
  92. {
  93. struct drm_device *dev = fb_helper->dev;
  94. struct drm_connector *connector;
  95. int i;
  96. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  97. struct drm_fb_helper_connector *fb_helper_connector;
  98. fb_helper_connector = kzalloc(sizeof(struct drm_fb_helper_connector), GFP_KERNEL);
  99. if (!fb_helper_connector)
  100. goto fail;
  101. fb_helper_connector->connector = connector;
  102. fb_helper->connector_info[fb_helper->connector_count++] = fb_helper_connector;
  103. }
  104. return 0;
  105. fail:
  106. for (i = 0; i < fb_helper->connector_count; i++) {
  107. kfree(fb_helper->connector_info[i]);
  108. fb_helper->connector_info[i] = NULL;
  109. }
  110. fb_helper->connector_count = 0;
  111. return -ENOMEM;
  112. }
  113. EXPORT_SYMBOL(drm_fb_helper_single_add_all_connectors);
  114. int drm_fb_helper_add_one_connector(struct drm_fb_helper *fb_helper, struct drm_connector *connector)
  115. {
  116. struct drm_fb_helper_connector **temp;
  117. struct drm_fb_helper_connector *fb_helper_connector;
  118. WARN_ON(!mutex_is_locked(&fb_helper->dev->mode_config.mutex));
  119. if (fb_helper->connector_count + 1 > fb_helper->connector_info_alloc_count) {
  120. temp = krealloc(fb_helper->connector_info, sizeof(struct drm_fb_helper_connector) * (fb_helper->connector_count + 1), GFP_KERNEL);
  121. if (!temp)
  122. return -ENOMEM;
  123. fb_helper->connector_info_alloc_count = fb_helper->connector_count + 1;
  124. fb_helper->connector_info = temp;
  125. }
  126. fb_helper_connector = kzalloc(sizeof(struct drm_fb_helper_connector), GFP_KERNEL);
  127. if (!fb_helper_connector)
  128. return -ENOMEM;
  129. fb_helper_connector->connector = connector;
  130. fb_helper->connector_info[fb_helper->connector_count++] = fb_helper_connector;
  131. return 0;
  132. }
  133. EXPORT_SYMBOL(drm_fb_helper_add_one_connector);
  134. int drm_fb_helper_remove_one_connector(struct drm_fb_helper *fb_helper,
  135. struct drm_connector *connector)
  136. {
  137. struct drm_fb_helper_connector *fb_helper_connector;
  138. int i, j;
  139. WARN_ON(!mutex_is_locked(&fb_helper->dev->mode_config.mutex));
  140. for (i = 0; i < fb_helper->connector_count; i++) {
  141. if (fb_helper->connector_info[i]->connector == connector)
  142. break;
  143. }
  144. if (i == fb_helper->connector_count)
  145. return -EINVAL;
  146. fb_helper_connector = fb_helper->connector_info[i];
  147. for (j = i + 1; j < fb_helper->connector_count; j++) {
  148. fb_helper->connector_info[j - 1] = fb_helper->connector_info[j];
  149. }
  150. fb_helper->connector_count--;
  151. kfree(fb_helper_connector);
  152. return 0;
  153. }
  154. EXPORT_SYMBOL(drm_fb_helper_remove_one_connector);
  155. static int drm_fb_helper_parse_command_line(struct drm_fb_helper *fb_helper)
  156. {
  157. struct drm_fb_helper_connector *fb_helper_conn;
  158. int i;
  159. for (i = 0; i < fb_helper->connector_count; i++) {
  160. struct drm_cmdline_mode *mode;
  161. struct drm_connector *connector;
  162. char *option = NULL;
  163. fb_helper_conn = fb_helper->connector_info[i];
  164. connector = fb_helper_conn->connector;
  165. mode = &fb_helper_conn->cmdline_mode;
  166. /* do something on return - turn off connector maybe */
  167. if (fb_get_options(connector->name, &option))
  168. continue;
  169. if (drm_mode_parse_command_line_for_connector(option,
  170. connector,
  171. mode)) {
  172. if (mode->force) {
  173. const char *s;
  174. switch (mode->force) {
  175. case DRM_FORCE_OFF:
  176. s = "OFF";
  177. break;
  178. case DRM_FORCE_ON_DIGITAL:
  179. s = "ON - dig";
  180. break;
  181. default:
  182. case DRM_FORCE_ON:
  183. s = "ON";
  184. break;
  185. }
  186. DRM_INFO("forcing %s connector %s\n",
  187. connector->name, s);
  188. connector->force = mode->force;
  189. }
  190. DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
  191. connector->name,
  192. mode->xres, mode->yres,
  193. mode->refresh_specified ? mode->refresh : 60,
  194. mode->rb ? " reduced blanking" : "",
  195. mode->margins ? " with margins" : "",
  196. mode->interlace ? " interlaced" : "");
  197. }
  198. }
  199. return 0;
  200. }
  201. static void drm_fb_helper_save_lut_atomic(struct drm_crtc *crtc, struct drm_fb_helper *helper)
  202. {
  203. uint16_t *r_base, *g_base, *b_base;
  204. int i;
  205. if (helper->funcs->gamma_get == NULL)
  206. return;
  207. r_base = crtc->gamma_store;
  208. g_base = r_base + crtc->gamma_size;
  209. b_base = g_base + crtc->gamma_size;
  210. for (i = 0; i < crtc->gamma_size; i++)
  211. helper->funcs->gamma_get(crtc, &r_base[i], &g_base[i], &b_base[i], i);
  212. }
  213. static void drm_fb_helper_restore_lut_atomic(struct drm_crtc *crtc)
  214. {
  215. uint16_t *r_base, *g_base, *b_base;
  216. if (crtc->funcs->gamma_set == NULL)
  217. return;
  218. r_base = crtc->gamma_store;
  219. g_base = r_base + crtc->gamma_size;
  220. b_base = g_base + crtc->gamma_size;
  221. crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
  222. }
  223. /**
  224. * drm_fb_helper_debug_enter - implementation for ->fb_debug_enter
  225. * @info: fbdev registered by the helper
  226. */
  227. int drm_fb_helper_debug_enter(struct fb_info *info)
  228. {
  229. struct drm_fb_helper *helper = info->par;
  230. struct drm_crtc_helper_funcs *funcs;
  231. int i;
  232. list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
  233. for (i = 0; i < helper->crtc_count; i++) {
  234. struct drm_mode_set *mode_set =
  235. &helper->crtc_info[i].mode_set;
  236. if (!mode_set->crtc->enabled)
  237. continue;
  238. funcs = mode_set->crtc->helper_private;
  239. drm_fb_helper_save_lut_atomic(mode_set->crtc, helper);
  240. funcs->mode_set_base_atomic(mode_set->crtc,
  241. mode_set->fb,
  242. mode_set->x,
  243. mode_set->y,
  244. ENTER_ATOMIC_MODE_SET);
  245. }
  246. }
  247. return 0;
  248. }
  249. EXPORT_SYMBOL(drm_fb_helper_debug_enter);
  250. /* Find the real fb for a given fb helper CRTC */
  251. static struct drm_framebuffer *drm_mode_config_fb(struct drm_crtc *crtc)
  252. {
  253. struct drm_device *dev = crtc->dev;
  254. struct drm_crtc *c;
  255. list_for_each_entry(c, &dev->mode_config.crtc_list, head) {
  256. if (crtc->base.id == c->base.id)
  257. return c->primary->fb;
  258. }
  259. return NULL;
  260. }
  261. /**
  262. * drm_fb_helper_debug_leave - implementation for ->fb_debug_leave
  263. * @info: fbdev registered by the helper
  264. */
  265. int drm_fb_helper_debug_leave(struct fb_info *info)
  266. {
  267. struct drm_fb_helper *helper = info->par;
  268. struct drm_crtc *crtc;
  269. struct drm_crtc_helper_funcs *funcs;
  270. struct drm_framebuffer *fb;
  271. int i;
  272. for (i = 0; i < helper->crtc_count; i++) {
  273. struct drm_mode_set *mode_set = &helper->crtc_info[i].mode_set;
  274. crtc = mode_set->crtc;
  275. funcs = crtc->helper_private;
  276. fb = drm_mode_config_fb(crtc);
  277. if (!crtc->enabled)
  278. continue;
  279. if (!fb) {
  280. DRM_ERROR("no fb to restore??\n");
  281. continue;
  282. }
  283. drm_fb_helper_restore_lut_atomic(mode_set->crtc);
  284. funcs->mode_set_base_atomic(mode_set->crtc, fb, crtc->x,
  285. crtc->y, LEAVE_ATOMIC_MODE_SET);
  286. }
  287. return 0;
  288. }
  289. EXPORT_SYMBOL(drm_fb_helper_debug_leave);
  290. static bool restore_fbdev_mode(struct drm_fb_helper *fb_helper)
  291. {
  292. struct drm_device *dev = fb_helper->dev;
  293. struct drm_plane *plane;
  294. bool error = false;
  295. int i;
  296. drm_warn_on_modeset_not_all_locked(dev);
  297. list_for_each_entry(plane, &dev->mode_config.plane_list, head)
  298. if (plane->type != DRM_PLANE_TYPE_PRIMARY)
  299. drm_plane_force_disable(plane);
  300. for (i = 0; i < fb_helper->crtc_count; i++) {
  301. struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set;
  302. struct drm_crtc *crtc = mode_set->crtc;
  303. int ret;
  304. if (crtc->funcs->cursor_set) {
  305. ret = crtc->funcs->cursor_set(crtc, NULL, 0, 0, 0);
  306. if (ret)
  307. error = true;
  308. }
  309. ret = drm_mode_set_config_internal(mode_set);
  310. if (ret)
  311. error = true;
  312. }
  313. return error;
  314. }
  315. /**
  316. * drm_fb_helper_restore_fbdev_mode - restore fbdev configuration
  317. * @fb_helper: fbcon to restore
  318. *
  319. * This should be called from driver's drm ->lastclose callback
  320. * when implementing an fbcon on top of kms using this helper. This ensures that
  321. * the user isn't greeted with a black screen when e.g. X dies.
  322. *
  323. * Use this variant if you need to bypass locking (panic), or already
  324. * hold all modeset locks. Otherwise use drm_fb_helper_restore_fbdev_mode_unlocked()
  325. */
  326. static bool drm_fb_helper_restore_fbdev_mode(struct drm_fb_helper *fb_helper)
  327. {
  328. return restore_fbdev_mode(fb_helper);
  329. }
  330. /**
  331. * drm_fb_helper_restore_fbdev_mode_unlocked - restore fbdev configuration
  332. * @fb_helper: fbcon to restore
  333. *
  334. * This should be called from driver's drm ->lastclose callback
  335. * when implementing an fbcon on top of kms using this helper. This ensures that
  336. * the user isn't greeted with a black screen when e.g. X dies.
  337. */
  338. bool drm_fb_helper_restore_fbdev_mode_unlocked(struct drm_fb_helper *fb_helper)
  339. {
  340. struct drm_device *dev = fb_helper->dev;
  341. bool ret;
  342. drm_modeset_lock_all(dev);
  343. ret = restore_fbdev_mode(fb_helper);
  344. drm_modeset_unlock_all(dev);
  345. return ret;
  346. }
  347. EXPORT_SYMBOL(drm_fb_helper_restore_fbdev_mode_unlocked);
  348. /*
  349. * restore fbcon display for all kms driver's using this helper, used for sysrq
  350. * and panic handling.
  351. */
  352. static bool drm_fb_helper_force_kernel_mode(void)
  353. {
  354. bool ret, error = false;
  355. struct drm_fb_helper *helper;
  356. if (list_empty(&kernel_fb_helper_list))
  357. return false;
  358. list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
  359. struct drm_device *dev = helper->dev;
  360. if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
  361. continue;
  362. /* NOTE: we use lockless flag below to avoid grabbing other
  363. * modeset locks. So just trylock the underlying mutex
  364. * directly:
  365. */
  366. if (!mutex_trylock(&dev->mode_config.mutex)) {
  367. error = true;
  368. continue;
  369. }
  370. ret = drm_fb_helper_restore_fbdev_mode(helper);
  371. if (ret)
  372. error = true;
  373. mutex_unlock(&dev->mode_config.mutex);
  374. }
  375. return error;
  376. }
  377. static int drm_fb_helper_panic(struct notifier_block *n, unsigned long ununsed,
  378. void *panic_str)
  379. {
  380. /*
  381. * It's a waste of time and effort to switch back to text console
  382. * if the kernel should reboot before panic messages can be seen.
  383. */
  384. if (panic_timeout < 0)
  385. return 0;
  386. pr_err("panic occurred, switching back to text console\n");
  387. return drm_fb_helper_force_kernel_mode();
  388. }
  389. static struct notifier_block paniced = {
  390. .notifier_call = drm_fb_helper_panic,
  391. };
  392. static bool drm_fb_helper_is_bound(struct drm_fb_helper *fb_helper)
  393. {
  394. struct drm_device *dev = fb_helper->dev;
  395. struct drm_crtc *crtc;
  396. int bound = 0, crtcs_bound = 0;
  397. /* Sometimes user space wants everything disabled, so don't steal the
  398. * display if there's a master. */
  399. if (dev->primary->master)
  400. return false;
  401. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  402. if (crtc->primary->fb)
  403. crtcs_bound++;
  404. if (crtc->primary->fb == fb_helper->fb)
  405. bound++;
  406. }
  407. if (bound < crtcs_bound)
  408. return false;
  409. return true;
  410. }
  411. #ifdef CONFIG_MAGIC_SYSRQ
  412. static void drm_fb_helper_restore_work_fn(struct work_struct *ignored)
  413. {
  414. bool ret;
  415. ret = drm_fb_helper_force_kernel_mode();
  416. if (ret == true)
  417. DRM_ERROR("Failed to restore crtc configuration\n");
  418. }
  419. static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn);
  420. static void drm_fb_helper_sysrq(int dummy1)
  421. {
  422. schedule_work(&drm_fb_helper_restore_work);
  423. }
  424. static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = {
  425. .handler = drm_fb_helper_sysrq,
  426. .help_msg = "force-fb(V)",
  427. .action_msg = "Restore framebuffer console",
  428. };
  429. #else
  430. static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { };
  431. #endif
  432. static void drm_fb_helper_dpms(struct fb_info *info, int dpms_mode)
  433. {
  434. struct drm_fb_helper *fb_helper = info->par;
  435. struct drm_device *dev = fb_helper->dev;
  436. struct drm_crtc *crtc;
  437. struct drm_connector *connector;
  438. int i, j;
  439. /*
  440. * fbdev->blank can be called from irq context in case of a panic.
  441. * Since we already have our own special panic handler which will
  442. * restore the fbdev console mode completely, just bail out early.
  443. */
  444. if (oops_in_progress)
  445. return;
  446. /*
  447. * For each CRTC in this fb, turn the connectors on/off.
  448. */
  449. drm_modeset_lock_all(dev);
  450. if (!drm_fb_helper_is_bound(fb_helper)) {
  451. drm_modeset_unlock_all(dev);
  452. return;
  453. }
  454. for (i = 0; i < fb_helper->crtc_count; i++) {
  455. crtc = fb_helper->crtc_info[i].mode_set.crtc;
  456. if (!crtc->enabled)
  457. continue;
  458. /* Walk the connectors & encoders on this fb turning them on/off */
  459. for (j = 0; j < fb_helper->connector_count; j++) {
  460. connector = fb_helper->connector_info[j]->connector;
  461. connector->funcs->dpms(connector, dpms_mode);
  462. drm_object_property_set_value(&connector->base,
  463. dev->mode_config.dpms_property, dpms_mode);
  464. }
  465. }
  466. drm_modeset_unlock_all(dev);
  467. }
  468. /**
  469. * drm_fb_helper_blank - implementation for ->fb_blank
  470. * @blank: desired blanking state
  471. * @info: fbdev registered by the helper
  472. */
  473. int drm_fb_helper_blank(int blank, struct fb_info *info)
  474. {
  475. switch (blank) {
  476. /* Display: On; HSync: On, VSync: On */
  477. case FB_BLANK_UNBLANK:
  478. drm_fb_helper_dpms(info, DRM_MODE_DPMS_ON);
  479. break;
  480. /* Display: Off; HSync: On, VSync: On */
  481. case FB_BLANK_NORMAL:
  482. drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
  483. break;
  484. /* Display: Off; HSync: Off, VSync: On */
  485. case FB_BLANK_HSYNC_SUSPEND:
  486. drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
  487. break;
  488. /* Display: Off; HSync: On, VSync: Off */
  489. case FB_BLANK_VSYNC_SUSPEND:
  490. drm_fb_helper_dpms(info, DRM_MODE_DPMS_SUSPEND);
  491. break;
  492. /* Display: Off; HSync: Off, VSync: Off */
  493. case FB_BLANK_POWERDOWN:
  494. drm_fb_helper_dpms(info, DRM_MODE_DPMS_OFF);
  495. break;
  496. }
  497. return 0;
  498. }
  499. EXPORT_SYMBOL(drm_fb_helper_blank);
  500. static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper)
  501. {
  502. int i;
  503. for (i = 0; i < helper->connector_count; i++)
  504. kfree(helper->connector_info[i]);
  505. kfree(helper->connector_info);
  506. for (i = 0; i < helper->crtc_count; i++) {
  507. kfree(helper->crtc_info[i].mode_set.connectors);
  508. if (helper->crtc_info[i].mode_set.mode)
  509. drm_mode_destroy(helper->dev, helper->crtc_info[i].mode_set.mode);
  510. }
  511. kfree(helper->crtc_info);
  512. }
  513. /**
  514. * drm_fb_helper_prepare - setup a drm_fb_helper structure
  515. * @dev: DRM device
  516. * @helper: driver-allocated fbdev helper structure to set up
  517. * @funcs: pointer to structure of functions associate with this helper
  518. *
  519. * Sets up the bare minimum to make the framebuffer helper usable. This is
  520. * useful to implement race-free initialization of the polling helpers.
  521. */
  522. void drm_fb_helper_prepare(struct drm_device *dev, struct drm_fb_helper *helper,
  523. const struct drm_fb_helper_funcs *funcs)
  524. {
  525. INIT_LIST_HEAD(&helper->kernel_fb_list);
  526. helper->funcs = funcs;
  527. helper->dev = dev;
  528. }
  529. EXPORT_SYMBOL(drm_fb_helper_prepare);
  530. /**
  531. * drm_fb_helper_init - initialize a drm_fb_helper structure
  532. * @dev: drm device
  533. * @fb_helper: driver-allocated fbdev helper structure to initialize
  534. * @crtc_count: maximum number of crtcs to support in this fbdev emulation
  535. * @max_conn_count: max connector count
  536. *
  537. * This allocates the structures for the fbdev helper with the given limits.
  538. * Note that this won't yet touch the hardware (through the driver interfaces)
  539. * nor register the fbdev. This is only done in drm_fb_helper_initial_config()
  540. * to allow driver writes more control over the exact init sequence.
  541. *
  542. * Drivers must call drm_fb_helper_prepare() before calling this function.
  543. *
  544. * RETURNS:
  545. * Zero if everything went ok, nonzero otherwise.
  546. */
  547. int drm_fb_helper_init(struct drm_device *dev,
  548. struct drm_fb_helper *fb_helper,
  549. int crtc_count, int max_conn_count)
  550. {
  551. struct drm_crtc *crtc;
  552. int i;
  553. if (!max_conn_count)
  554. return -EINVAL;
  555. fb_helper->crtc_info = kcalloc(crtc_count, sizeof(struct drm_fb_helper_crtc), GFP_KERNEL);
  556. if (!fb_helper->crtc_info)
  557. return -ENOMEM;
  558. fb_helper->crtc_count = crtc_count;
  559. fb_helper->connector_info = kcalloc(dev->mode_config.num_connector, sizeof(struct drm_fb_helper_connector *), GFP_KERNEL);
  560. if (!fb_helper->connector_info) {
  561. kfree(fb_helper->crtc_info);
  562. return -ENOMEM;
  563. }
  564. fb_helper->connector_info_alloc_count = dev->mode_config.num_connector;
  565. fb_helper->connector_count = 0;
  566. for (i = 0; i < crtc_count; i++) {
  567. fb_helper->crtc_info[i].mode_set.connectors =
  568. kcalloc(max_conn_count,
  569. sizeof(struct drm_connector *),
  570. GFP_KERNEL);
  571. if (!fb_helper->crtc_info[i].mode_set.connectors)
  572. goto out_free;
  573. fb_helper->crtc_info[i].mode_set.num_connectors = 0;
  574. }
  575. i = 0;
  576. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  577. fb_helper->crtc_info[i].mode_set.crtc = crtc;
  578. i++;
  579. }
  580. return 0;
  581. out_free:
  582. drm_fb_helper_crtc_free(fb_helper);
  583. return -ENOMEM;
  584. }
  585. EXPORT_SYMBOL(drm_fb_helper_init);
  586. void drm_fb_helper_fini(struct drm_fb_helper *fb_helper)
  587. {
  588. if (!list_empty(&fb_helper->kernel_fb_list)) {
  589. list_del(&fb_helper->kernel_fb_list);
  590. if (list_empty(&kernel_fb_helper_list)) {
  591. pr_info("drm: unregistered panic notifier\n");
  592. atomic_notifier_chain_unregister(&panic_notifier_list,
  593. &paniced);
  594. unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
  595. }
  596. }
  597. drm_fb_helper_crtc_free(fb_helper);
  598. }
  599. EXPORT_SYMBOL(drm_fb_helper_fini);
  600. static int setcolreg(struct drm_crtc *crtc, u16 red, u16 green,
  601. u16 blue, u16 regno, struct fb_info *info)
  602. {
  603. struct drm_fb_helper *fb_helper = info->par;
  604. struct drm_framebuffer *fb = fb_helper->fb;
  605. int pindex;
  606. if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
  607. u32 *palette;
  608. u32 value;
  609. /* place color in psuedopalette */
  610. if (regno > 16)
  611. return -EINVAL;
  612. palette = (u32 *)info->pseudo_palette;
  613. red >>= (16 - info->var.red.length);
  614. green >>= (16 - info->var.green.length);
  615. blue >>= (16 - info->var.blue.length);
  616. value = (red << info->var.red.offset) |
  617. (green << info->var.green.offset) |
  618. (blue << info->var.blue.offset);
  619. if (info->var.transp.length > 0) {
  620. u32 mask = (1 << info->var.transp.length) - 1;
  621. mask <<= info->var.transp.offset;
  622. value |= mask;
  623. }
  624. palette[regno] = value;
  625. return 0;
  626. }
  627. /*
  628. * The driver really shouldn't advertise pseudo/directcolor
  629. * visuals if it can't deal with the palette.
  630. */
  631. if (WARN_ON(!fb_helper->funcs->gamma_set ||
  632. !fb_helper->funcs->gamma_get))
  633. return -EINVAL;
  634. pindex = regno;
  635. if (fb->bits_per_pixel == 16) {
  636. pindex = regno << 3;
  637. if (fb->depth == 16 && regno > 63)
  638. return -EINVAL;
  639. if (fb->depth == 15 && regno > 31)
  640. return -EINVAL;
  641. if (fb->depth == 16) {
  642. u16 r, g, b;
  643. int i;
  644. if (regno < 32) {
  645. for (i = 0; i < 8; i++)
  646. fb_helper->funcs->gamma_set(crtc, red,
  647. green, blue, pindex + i);
  648. }
  649. fb_helper->funcs->gamma_get(crtc, &r,
  650. &g, &b,
  651. pindex >> 1);
  652. for (i = 0; i < 4; i++)
  653. fb_helper->funcs->gamma_set(crtc, r,
  654. green, b,
  655. (pindex >> 1) + i);
  656. }
  657. }
  658. if (fb->depth != 16)
  659. fb_helper->funcs->gamma_set(crtc, red, green, blue, pindex);
  660. return 0;
  661. }
  662. /**
  663. * drm_fb_helper_setcmap - implementation for ->fb_setcmap
  664. * @cmap: cmap to set
  665. * @info: fbdev registered by the helper
  666. */
  667. int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info)
  668. {
  669. struct drm_fb_helper *fb_helper = info->par;
  670. struct drm_device *dev = fb_helper->dev;
  671. struct drm_crtc_helper_funcs *crtc_funcs;
  672. u16 *red, *green, *blue, *transp;
  673. struct drm_crtc *crtc;
  674. int i, j, rc = 0;
  675. int start;
  676. drm_modeset_lock_all(dev);
  677. if (!drm_fb_helper_is_bound(fb_helper)) {
  678. drm_modeset_unlock_all(dev);
  679. return -EBUSY;
  680. }
  681. for (i = 0; i < fb_helper->crtc_count; i++) {
  682. crtc = fb_helper->crtc_info[i].mode_set.crtc;
  683. crtc_funcs = crtc->helper_private;
  684. red = cmap->red;
  685. green = cmap->green;
  686. blue = cmap->blue;
  687. transp = cmap->transp;
  688. start = cmap->start;
  689. for (j = 0; j < cmap->len; j++) {
  690. u16 hred, hgreen, hblue, htransp = 0xffff;
  691. hred = *red++;
  692. hgreen = *green++;
  693. hblue = *blue++;
  694. if (transp)
  695. htransp = *transp++;
  696. rc = setcolreg(crtc, hred, hgreen, hblue, start++, info);
  697. if (rc)
  698. goto out;
  699. }
  700. if (crtc_funcs->load_lut)
  701. crtc_funcs->load_lut(crtc);
  702. }
  703. out:
  704. drm_modeset_unlock_all(dev);
  705. return rc;
  706. }
  707. EXPORT_SYMBOL(drm_fb_helper_setcmap);
  708. /**
  709. * drm_fb_helper_check_var - implementation for ->fb_check_var
  710. * @var: screeninfo to check
  711. * @info: fbdev registered by the helper
  712. */
  713. int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
  714. struct fb_info *info)
  715. {
  716. struct drm_fb_helper *fb_helper = info->par;
  717. struct drm_framebuffer *fb = fb_helper->fb;
  718. int depth;
  719. if (var->pixclock != 0 || in_dbg_master())
  720. return -EINVAL;
  721. /* Need to resize the fb object !!! */
  722. if (var->bits_per_pixel > fb->bits_per_pixel ||
  723. var->xres > fb->width || var->yres > fb->height ||
  724. var->xres_virtual > fb->width || var->yres_virtual > fb->height) {
  725. DRM_DEBUG("fb userspace requested width/height/bpp is greater than current fb "
  726. "request %dx%d-%d (virtual %dx%d) > %dx%d-%d\n",
  727. var->xres, var->yres, var->bits_per_pixel,
  728. var->xres_virtual, var->yres_virtual,
  729. fb->width, fb->height, fb->bits_per_pixel);
  730. return -EINVAL;
  731. }
  732. switch (var->bits_per_pixel) {
  733. case 16:
  734. depth = (var->green.length == 6) ? 16 : 15;
  735. break;
  736. case 32:
  737. depth = (var->transp.length > 0) ? 32 : 24;
  738. break;
  739. default:
  740. depth = var->bits_per_pixel;
  741. break;
  742. }
  743. switch (depth) {
  744. case 8:
  745. var->red.offset = 0;
  746. var->green.offset = 0;
  747. var->blue.offset = 0;
  748. var->red.length = 8;
  749. var->green.length = 8;
  750. var->blue.length = 8;
  751. var->transp.length = 0;
  752. var->transp.offset = 0;
  753. break;
  754. case 15:
  755. var->red.offset = 10;
  756. var->green.offset = 5;
  757. var->blue.offset = 0;
  758. var->red.length = 5;
  759. var->green.length = 5;
  760. var->blue.length = 5;
  761. var->transp.length = 1;
  762. var->transp.offset = 15;
  763. break;
  764. case 16:
  765. var->red.offset = 11;
  766. var->green.offset = 5;
  767. var->blue.offset = 0;
  768. var->red.length = 5;
  769. var->green.length = 6;
  770. var->blue.length = 5;
  771. var->transp.length = 0;
  772. var->transp.offset = 0;
  773. break;
  774. case 24:
  775. var->red.offset = 16;
  776. var->green.offset = 8;
  777. var->blue.offset = 0;
  778. var->red.length = 8;
  779. var->green.length = 8;
  780. var->blue.length = 8;
  781. var->transp.length = 0;
  782. var->transp.offset = 0;
  783. break;
  784. case 32:
  785. var->red.offset = 16;
  786. var->green.offset = 8;
  787. var->blue.offset = 0;
  788. var->red.length = 8;
  789. var->green.length = 8;
  790. var->blue.length = 8;
  791. var->transp.length = 8;
  792. var->transp.offset = 24;
  793. break;
  794. default:
  795. return -EINVAL;
  796. }
  797. return 0;
  798. }
  799. EXPORT_SYMBOL(drm_fb_helper_check_var);
  800. /**
  801. * drm_fb_helper_set_par - implementation for ->fb_set_par
  802. * @info: fbdev registered by the helper
  803. *
  804. * This will let fbcon do the mode init and is called at initialization time by
  805. * the fbdev core when registering the driver, and later on through the hotplug
  806. * callback.
  807. */
  808. int drm_fb_helper_set_par(struct fb_info *info)
  809. {
  810. struct drm_fb_helper *fb_helper = info->par;
  811. struct fb_var_screeninfo *var = &info->var;
  812. if (var->pixclock != 0) {
  813. DRM_ERROR("PIXEL CLOCK SET\n");
  814. return -EINVAL;
  815. }
  816. drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper);
  817. if (fb_helper->delayed_hotplug) {
  818. fb_helper->delayed_hotplug = false;
  819. drm_fb_helper_hotplug_event(fb_helper);
  820. }
  821. return 0;
  822. }
  823. EXPORT_SYMBOL(drm_fb_helper_set_par);
  824. /**
  825. * drm_fb_helper_pan_display - implementation for ->fb_pan_display
  826. * @var: updated screen information
  827. * @info: fbdev registered by the helper
  828. */
  829. int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
  830. struct fb_info *info)
  831. {
  832. struct drm_fb_helper *fb_helper = info->par;
  833. struct drm_device *dev = fb_helper->dev;
  834. struct drm_mode_set *modeset;
  835. int ret = 0;
  836. int i;
  837. drm_modeset_lock_all(dev);
  838. if (!drm_fb_helper_is_bound(fb_helper)) {
  839. drm_modeset_unlock_all(dev);
  840. return -EBUSY;
  841. }
  842. for (i = 0; i < fb_helper->crtc_count; i++) {
  843. modeset = &fb_helper->crtc_info[i].mode_set;
  844. modeset->x = var->xoffset;
  845. modeset->y = var->yoffset;
  846. if (modeset->num_connectors) {
  847. ret = drm_mode_set_config_internal(modeset);
  848. if (!ret) {
  849. info->var.xoffset = var->xoffset;
  850. info->var.yoffset = var->yoffset;
  851. }
  852. }
  853. }
  854. drm_modeset_unlock_all(dev);
  855. return ret;
  856. }
  857. EXPORT_SYMBOL(drm_fb_helper_pan_display);
  858. /*
  859. * Allocates the backing storage and sets up the fbdev info structure through
  860. * the ->fb_probe callback and then registers the fbdev and sets up the panic
  861. * notifier.
  862. */
  863. static int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
  864. int preferred_bpp)
  865. {
  866. int ret = 0;
  867. int crtc_count = 0;
  868. int i;
  869. struct fb_info *info;
  870. struct drm_fb_helper_surface_size sizes;
  871. int gamma_size = 0;
  872. memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size));
  873. sizes.surface_depth = 24;
  874. sizes.surface_bpp = 32;
  875. sizes.fb_width = (unsigned)-1;
  876. sizes.fb_height = (unsigned)-1;
  877. /* if driver picks 8 or 16 by default use that
  878. for both depth/bpp */
  879. if (preferred_bpp != sizes.surface_bpp)
  880. sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
  881. /* first up get a count of crtcs now in use and new min/maxes width/heights */
  882. for (i = 0; i < fb_helper->connector_count; i++) {
  883. struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i];
  884. struct drm_cmdline_mode *cmdline_mode;
  885. cmdline_mode = &fb_helper_conn->cmdline_mode;
  886. if (cmdline_mode->bpp_specified) {
  887. switch (cmdline_mode->bpp) {
  888. case 8:
  889. sizes.surface_depth = sizes.surface_bpp = 8;
  890. break;
  891. case 15:
  892. sizes.surface_depth = 15;
  893. sizes.surface_bpp = 16;
  894. break;
  895. case 16:
  896. sizes.surface_depth = sizes.surface_bpp = 16;
  897. break;
  898. case 24:
  899. sizes.surface_depth = sizes.surface_bpp = 24;
  900. break;
  901. case 32:
  902. sizes.surface_depth = 24;
  903. sizes.surface_bpp = 32;
  904. break;
  905. }
  906. break;
  907. }
  908. }
  909. crtc_count = 0;
  910. for (i = 0; i < fb_helper->crtc_count; i++) {
  911. struct drm_display_mode *desired_mode;
  912. desired_mode = fb_helper->crtc_info[i].desired_mode;
  913. if (desired_mode) {
  914. if (gamma_size == 0)
  915. gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size;
  916. if (desired_mode->hdisplay < sizes.fb_width)
  917. sizes.fb_width = desired_mode->hdisplay;
  918. if (desired_mode->vdisplay < sizes.fb_height)
  919. sizes.fb_height = desired_mode->vdisplay;
  920. if (desired_mode->hdisplay > sizes.surface_width)
  921. sizes.surface_width = desired_mode->hdisplay;
  922. if (desired_mode->vdisplay > sizes.surface_height)
  923. sizes.surface_height = desired_mode->vdisplay;
  924. crtc_count++;
  925. }
  926. }
  927. if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) {
  928. /* hmm everyone went away - assume VGA cable just fell out
  929. and will come back later. */
  930. DRM_INFO("Cannot find any crtc or sizes - going 1024x768\n");
  931. sizes.fb_width = sizes.surface_width = 1024;
  932. sizes.fb_height = sizes.surface_height = 768;
  933. }
  934. /* push down into drivers */
  935. ret = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes);
  936. if (ret < 0)
  937. return ret;
  938. info = fb_helper->fbdev;
  939. /*
  940. * Set the fb pointer - usually drm_setup_crtcs does this for hotplug
  941. * events, but at init time drm_setup_crtcs needs to be called before
  942. * the fb is allocated (since we need to figure out the desired size of
  943. * the fb before we can allocate it ...). Hence we need to fix things up
  944. * here again.
  945. */
  946. for (i = 0; i < fb_helper->crtc_count; i++)
  947. if (fb_helper->crtc_info[i].mode_set.num_connectors)
  948. fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb;
  949. info->var.pixclock = 0;
  950. if (register_framebuffer(info) < 0)
  951. return -EINVAL;
  952. dev_info(fb_helper->dev->dev, "fb%d: %s frame buffer device\n",
  953. info->node, info->fix.id);
  954. /* Switch back to kernel console on panic */
  955. /* multi card linked list maybe */
  956. if (list_empty(&kernel_fb_helper_list)) {
  957. dev_info(fb_helper->dev->dev, "registered panic notifier\n");
  958. atomic_notifier_chain_register(&panic_notifier_list,
  959. &paniced);
  960. register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
  961. }
  962. list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
  963. return 0;
  964. }
  965. /**
  966. * drm_fb_helper_fill_fix - initializes fixed fbdev information
  967. * @info: fbdev registered by the helper
  968. * @pitch: desired pitch
  969. * @depth: desired depth
  970. *
  971. * Helper to fill in the fixed fbdev information useful for a non-accelerated
  972. * fbdev emulations. Drivers which support acceleration methods which impose
  973. * additional constraints need to set up their own limits.
  974. *
  975. * Drivers should call this (or their equivalent setup code) from their
  976. * ->fb_probe callback.
  977. */
  978. void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
  979. uint32_t depth)
  980. {
  981. info->fix.type = FB_TYPE_PACKED_PIXELS;
  982. info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR :
  983. FB_VISUAL_TRUECOLOR;
  984. info->fix.mmio_start = 0;
  985. info->fix.mmio_len = 0;
  986. info->fix.type_aux = 0;
  987. info->fix.xpanstep = 1; /* doing it in hw */
  988. info->fix.ypanstep = 1; /* doing it in hw */
  989. info->fix.ywrapstep = 0;
  990. info->fix.accel = FB_ACCEL_NONE;
  991. info->fix.line_length = pitch;
  992. return;
  993. }
  994. EXPORT_SYMBOL(drm_fb_helper_fill_fix);
  995. /**
  996. * drm_fb_helper_fill_var - initalizes variable fbdev information
  997. * @info: fbdev instance to set up
  998. * @fb_helper: fb helper instance to use as template
  999. * @fb_width: desired fb width
  1000. * @fb_height: desired fb height
  1001. *
  1002. * Sets up the variable fbdev metainformation from the given fb helper instance
  1003. * and the drm framebuffer allocated in fb_helper->fb.
  1004. *
  1005. * Drivers should call this (or their equivalent setup code) from their
  1006. * ->fb_probe callback after having allocated the fbdev backing
  1007. * storage framebuffer.
  1008. */
  1009. void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper,
  1010. uint32_t fb_width, uint32_t fb_height)
  1011. {
  1012. struct drm_framebuffer *fb = fb_helper->fb;
  1013. info->pseudo_palette = fb_helper->pseudo_palette;
  1014. info->var.xres_virtual = fb->width;
  1015. info->var.yres_virtual = fb->height;
  1016. info->var.bits_per_pixel = fb->bits_per_pixel;
  1017. info->var.accel_flags = FB_ACCELF_TEXT;
  1018. info->var.xoffset = 0;
  1019. info->var.yoffset = 0;
  1020. info->var.activate = FB_ACTIVATE_NOW;
  1021. info->var.height = -1;
  1022. info->var.width = -1;
  1023. switch (fb->depth) {
  1024. case 8:
  1025. info->var.red.offset = 0;
  1026. info->var.green.offset = 0;
  1027. info->var.blue.offset = 0;
  1028. info->var.red.length = 8; /* 8bit DAC */
  1029. info->var.green.length = 8;
  1030. info->var.blue.length = 8;
  1031. info->var.transp.offset = 0;
  1032. info->var.transp.length = 0;
  1033. break;
  1034. case 15:
  1035. info->var.red.offset = 10;
  1036. info->var.green.offset = 5;
  1037. info->var.blue.offset = 0;
  1038. info->var.red.length = 5;
  1039. info->var.green.length = 5;
  1040. info->var.blue.length = 5;
  1041. info->var.transp.offset = 15;
  1042. info->var.transp.length = 1;
  1043. break;
  1044. case 16:
  1045. info->var.red.offset = 11;
  1046. info->var.green.offset = 5;
  1047. info->var.blue.offset = 0;
  1048. info->var.red.length = 5;
  1049. info->var.green.length = 6;
  1050. info->var.blue.length = 5;
  1051. info->var.transp.offset = 0;
  1052. break;
  1053. case 24:
  1054. info->var.red.offset = 16;
  1055. info->var.green.offset = 8;
  1056. info->var.blue.offset = 0;
  1057. info->var.red.length = 8;
  1058. info->var.green.length = 8;
  1059. info->var.blue.length = 8;
  1060. info->var.transp.offset = 0;
  1061. info->var.transp.length = 0;
  1062. break;
  1063. case 32:
  1064. info->var.red.offset = 16;
  1065. info->var.green.offset = 8;
  1066. info->var.blue.offset = 0;
  1067. info->var.red.length = 8;
  1068. info->var.green.length = 8;
  1069. info->var.blue.length = 8;
  1070. info->var.transp.offset = 24;
  1071. info->var.transp.length = 8;
  1072. break;
  1073. default:
  1074. break;
  1075. }
  1076. info->var.xres = fb_width;
  1077. info->var.yres = fb_height;
  1078. }
  1079. EXPORT_SYMBOL(drm_fb_helper_fill_var);
  1080. static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper,
  1081. uint32_t maxX,
  1082. uint32_t maxY)
  1083. {
  1084. struct drm_connector *connector;
  1085. int count = 0;
  1086. int i;
  1087. for (i = 0; i < fb_helper->connector_count; i++) {
  1088. connector = fb_helper->connector_info[i]->connector;
  1089. count += connector->funcs->fill_modes(connector, maxX, maxY);
  1090. }
  1091. return count;
  1092. }
  1093. struct drm_display_mode *drm_has_preferred_mode(struct drm_fb_helper_connector *fb_connector, int width, int height)
  1094. {
  1095. struct drm_display_mode *mode;
  1096. list_for_each_entry(mode, &fb_connector->connector->modes, head) {
  1097. if (mode->hdisplay > width ||
  1098. mode->vdisplay > height)
  1099. continue;
  1100. if (mode->type & DRM_MODE_TYPE_PREFERRED)
  1101. return mode;
  1102. }
  1103. return NULL;
  1104. }
  1105. EXPORT_SYMBOL(drm_has_preferred_mode);
  1106. static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector)
  1107. {
  1108. struct drm_cmdline_mode *cmdline_mode;
  1109. cmdline_mode = &fb_connector->cmdline_mode;
  1110. return cmdline_mode->specified;
  1111. }
  1112. struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn,
  1113. int width, int height)
  1114. {
  1115. struct drm_cmdline_mode *cmdline_mode;
  1116. struct drm_display_mode *mode = NULL;
  1117. bool prefer_non_interlace;
  1118. cmdline_mode = &fb_helper_conn->cmdline_mode;
  1119. if (cmdline_mode->specified == false)
  1120. return mode;
  1121. /* attempt to find a matching mode in the list of modes
  1122. * we have gotten so far, if not add a CVT mode that conforms
  1123. */
  1124. if (cmdline_mode->rb || cmdline_mode->margins)
  1125. goto create_mode;
  1126. prefer_non_interlace = !cmdline_mode->interlace;
  1127. again:
  1128. list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
  1129. /* check width/height */
  1130. if (mode->hdisplay != cmdline_mode->xres ||
  1131. mode->vdisplay != cmdline_mode->yres)
  1132. continue;
  1133. if (cmdline_mode->refresh_specified) {
  1134. if (mode->vrefresh != cmdline_mode->refresh)
  1135. continue;
  1136. }
  1137. if (cmdline_mode->interlace) {
  1138. if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
  1139. continue;
  1140. } else if (prefer_non_interlace) {
  1141. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  1142. continue;
  1143. }
  1144. return mode;
  1145. }
  1146. if (prefer_non_interlace) {
  1147. prefer_non_interlace = false;
  1148. goto again;
  1149. }
  1150. create_mode:
  1151. mode = drm_mode_create_from_cmdline_mode(fb_helper_conn->connector->dev,
  1152. cmdline_mode);
  1153. list_add(&mode->head, &fb_helper_conn->connector->modes);
  1154. return mode;
  1155. }
  1156. EXPORT_SYMBOL(drm_pick_cmdline_mode);
  1157. static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
  1158. {
  1159. bool enable;
  1160. if (strict)
  1161. enable = connector->status == connector_status_connected;
  1162. else
  1163. enable = connector->status != connector_status_disconnected;
  1164. return enable;
  1165. }
  1166. static void drm_enable_connectors(struct drm_fb_helper *fb_helper,
  1167. bool *enabled)
  1168. {
  1169. bool any_enabled = false;
  1170. struct drm_connector *connector;
  1171. int i = 0;
  1172. for (i = 0; i < fb_helper->connector_count; i++) {
  1173. connector = fb_helper->connector_info[i]->connector;
  1174. enabled[i] = drm_connector_enabled(connector, true);
  1175. DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
  1176. enabled[i] ? "yes" : "no");
  1177. any_enabled |= enabled[i];
  1178. }
  1179. if (any_enabled)
  1180. return;
  1181. for (i = 0; i < fb_helper->connector_count; i++) {
  1182. connector = fb_helper->connector_info[i]->connector;
  1183. enabled[i] = drm_connector_enabled(connector, false);
  1184. }
  1185. }
  1186. static bool drm_target_cloned(struct drm_fb_helper *fb_helper,
  1187. struct drm_display_mode **modes,
  1188. bool *enabled, int width, int height)
  1189. {
  1190. int count, i, j;
  1191. bool can_clone = false;
  1192. struct drm_fb_helper_connector *fb_helper_conn;
  1193. struct drm_display_mode *dmt_mode, *mode;
  1194. /* only contemplate cloning in the single crtc case */
  1195. if (fb_helper->crtc_count > 1)
  1196. return false;
  1197. count = 0;
  1198. for (i = 0; i < fb_helper->connector_count; i++) {
  1199. if (enabled[i])
  1200. count++;
  1201. }
  1202. /* only contemplate cloning if more than one connector is enabled */
  1203. if (count <= 1)
  1204. return false;
  1205. /* check the command line or if nothing common pick 1024x768 */
  1206. can_clone = true;
  1207. for (i = 0; i < fb_helper->connector_count; i++) {
  1208. if (!enabled[i])
  1209. continue;
  1210. fb_helper_conn = fb_helper->connector_info[i];
  1211. modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
  1212. if (!modes[i]) {
  1213. can_clone = false;
  1214. break;
  1215. }
  1216. for (j = 0; j < i; j++) {
  1217. if (!enabled[j])
  1218. continue;
  1219. if (!drm_mode_equal(modes[j], modes[i]))
  1220. can_clone = false;
  1221. }
  1222. }
  1223. if (can_clone) {
  1224. DRM_DEBUG_KMS("can clone using command line\n");
  1225. return true;
  1226. }
  1227. /* try and find a 1024x768 mode on each connector */
  1228. can_clone = true;
  1229. dmt_mode = drm_mode_find_dmt(fb_helper->dev, 1024, 768, 60, false);
  1230. for (i = 0; i < fb_helper->connector_count; i++) {
  1231. if (!enabled[i])
  1232. continue;
  1233. fb_helper_conn = fb_helper->connector_info[i];
  1234. list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
  1235. if (drm_mode_equal(mode, dmt_mode))
  1236. modes[i] = mode;
  1237. }
  1238. if (!modes[i])
  1239. can_clone = false;
  1240. }
  1241. if (can_clone) {
  1242. DRM_DEBUG_KMS("can clone using 1024x768\n");
  1243. return true;
  1244. }
  1245. DRM_INFO("kms: can't enable cloning when we probably wanted to.\n");
  1246. return false;
  1247. }
  1248. static bool drm_target_preferred(struct drm_fb_helper *fb_helper,
  1249. struct drm_display_mode **modes,
  1250. bool *enabled, int width, int height)
  1251. {
  1252. struct drm_fb_helper_connector *fb_helper_conn;
  1253. int i;
  1254. for (i = 0; i < fb_helper->connector_count; i++) {
  1255. fb_helper_conn = fb_helper->connector_info[i];
  1256. if (enabled[i] == false)
  1257. continue;
  1258. DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
  1259. fb_helper_conn->connector->base.id);
  1260. /* got for command line mode first */
  1261. modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
  1262. if (!modes[i]) {
  1263. DRM_DEBUG_KMS("looking for preferred mode on connector %d\n",
  1264. fb_helper_conn->connector->base.id);
  1265. modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height);
  1266. }
  1267. /* No preferred modes, pick one off the list */
  1268. if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) {
  1269. list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head)
  1270. break;
  1271. }
  1272. DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
  1273. "none");
  1274. }
  1275. return true;
  1276. }
  1277. static int drm_pick_crtcs(struct drm_fb_helper *fb_helper,
  1278. struct drm_fb_helper_crtc **best_crtcs,
  1279. struct drm_display_mode **modes,
  1280. int n, int width, int height)
  1281. {
  1282. int c, o;
  1283. struct drm_device *dev = fb_helper->dev;
  1284. struct drm_connector *connector;
  1285. struct drm_connector_helper_funcs *connector_funcs;
  1286. struct drm_encoder *encoder;
  1287. int my_score, best_score, score;
  1288. struct drm_fb_helper_crtc **crtcs, *crtc;
  1289. struct drm_fb_helper_connector *fb_helper_conn;
  1290. if (n == fb_helper->connector_count)
  1291. return 0;
  1292. fb_helper_conn = fb_helper->connector_info[n];
  1293. connector = fb_helper_conn->connector;
  1294. best_crtcs[n] = NULL;
  1295. best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height);
  1296. if (modes[n] == NULL)
  1297. return best_score;
  1298. crtcs = kzalloc(dev->mode_config.num_connector *
  1299. sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
  1300. if (!crtcs)
  1301. return best_score;
  1302. my_score = 1;
  1303. if (connector->status == connector_status_connected)
  1304. my_score++;
  1305. if (drm_has_cmdline_mode(fb_helper_conn))
  1306. my_score++;
  1307. if (drm_has_preferred_mode(fb_helper_conn, width, height))
  1308. my_score++;
  1309. connector_funcs = connector->helper_private;
  1310. encoder = connector_funcs->best_encoder(connector);
  1311. if (!encoder)
  1312. goto out;
  1313. /* select a crtc for this connector and then attempt to configure
  1314. remaining connectors */
  1315. for (c = 0; c < fb_helper->crtc_count; c++) {
  1316. crtc = &fb_helper->crtc_info[c];
  1317. if ((encoder->possible_crtcs & (1 << c)) == 0)
  1318. continue;
  1319. for (o = 0; o < n; o++)
  1320. if (best_crtcs[o] == crtc)
  1321. break;
  1322. if (o < n) {
  1323. /* ignore cloning unless only a single crtc */
  1324. if (fb_helper->crtc_count > 1)
  1325. continue;
  1326. if (!drm_mode_equal(modes[o], modes[n]))
  1327. continue;
  1328. }
  1329. crtcs[n] = crtc;
  1330. memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *));
  1331. score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1,
  1332. width, height);
  1333. if (score > best_score) {
  1334. best_score = score;
  1335. memcpy(best_crtcs, crtcs,
  1336. dev->mode_config.num_connector *
  1337. sizeof(struct drm_fb_helper_crtc *));
  1338. }
  1339. }
  1340. out:
  1341. kfree(crtcs);
  1342. return best_score;
  1343. }
  1344. static void drm_setup_crtcs(struct drm_fb_helper *fb_helper)
  1345. {
  1346. struct drm_device *dev = fb_helper->dev;
  1347. struct drm_fb_helper_crtc **crtcs;
  1348. struct drm_display_mode **modes;
  1349. struct drm_mode_set *modeset;
  1350. bool *enabled;
  1351. int width, height;
  1352. int i;
  1353. DRM_DEBUG_KMS("\n");
  1354. width = dev->mode_config.max_width;
  1355. height = dev->mode_config.max_height;
  1356. crtcs = kcalloc(dev->mode_config.num_connector,
  1357. sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
  1358. modes = kcalloc(dev->mode_config.num_connector,
  1359. sizeof(struct drm_display_mode *), GFP_KERNEL);
  1360. enabled = kcalloc(dev->mode_config.num_connector,
  1361. sizeof(bool), GFP_KERNEL);
  1362. if (!crtcs || !modes || !enabled) {
  1363. DRM_ERROR("Memory allocation failed\n");
  1364. goto out;
  1365. }
  1366. drm_enable_connectors(fb_helper, enabled);
  1367. if (!(fb_helper->funcs->initial_config &&
  1368. fb_helper->funcs->initial_config(fb_helper, crtcs, modes,
  1369. enabled, width, height))) {
  1370. memset(modes, 0, dev->mode_config.num_connector*sizeof(modes[0]));
  1371. memset(crtcs, 0, dev->mode_config.num_connector*sizeof(crtcs[0]));
  1372. if (!drm_target_cloned(fb_helper,
  1373. modes, enabled, width, height) &&
  1374. !drm_target_preferred(fb_helper,
  1375. modes, enabled, width, height))
  1376. DRM_ERROR("Unable to find initial modes\n");
  1377. DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n",
  1378. width, height);
  1379. drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height);
  1380. }
  1381. /* need to set the modesets up here for use later */
  1382. /* fill out the connector<->crtc mappings into the modesets */
  1383. for (i = 0; i < fb_helper->crtc_count; i++) {
  1384. modeset = &fb_helper->crtc_info[i].mode_set;
  1385. modeset->num_connectors = 0;
  1386. modeset->fb = NULL;
  1387. }
  1388. for (i = 0; i < fb_helper->connector_count; i++) {
  1389. struct drm_display_mode *mode = modes[i];
  1390. struct drm_fb_helper_crtc *fb_crtc = crtcs[i];
  1391. modeset = &fb_crtc->mode_set;
  1392. if (mode && fb_crtc) {
  1393. DRM_DEBUG_KMS("desired mode %s set on crtc %d\n",
  1394. mode->name, fb_crtc->mode_set.crtc->base.id);
  1395. fb_crtc->desired_mode = mode;
  1396. if (modeset->mode)
  1397. drm_mode_destroy(dev, modeset->mode);
  1398. modeset->mode = drm_mode_duplicate(dev,
  1399. fb_crtc->desired_mode);
  1400. modeset->connectors[modeset->num_connectors++] = fb_helper->connector_info[i]->connector;
  1401. modeset->fb = fb_helper->fb;
  1402. }
  1403. }
  1404. /* Clear out any old modes if there are no more connected outputs. */
  1405. for (i = 0; i < fb_helper->crtc_count; i++) {
  1406. modeset = &fb_helper->crtc_info[i].mode_set;
  1407. if (modeset->num_connectors == 0) {
  1408. BUG_ON(modeset->fb);
  1409. BUG_ON(modeset->num_connectors);
  1410. if (modeset->mode)
  1411. drm_mode_destroy(dev, modeset->mode);
  1412. modeset->mode = NULL;
  1413. }
  1414. }
  1415. out:
  1416. kfree(crtcs);
  1417. kfree(modes);
  1418. kfree(enabled);
  1419. }
  1420. /**
  1421. * drm_fb_helper_initial_config - setup a sane initial connector configuration
  1422. * @fb_helper: fb_helper device struct
  1423. * @bpp_sel: bpp value to use for the framebuffer configuration
  1424. *
  1425. * Scans the CRTCs and connectors and tries to put together an initial setup.
  1426. * At the moment, this is a cloned configuration across all heads with
  1427. * a new framebuffer object as the backing store.
  1428. *
  1429. * Note that this also registers the fbdev and so allows userspace to call into
  1430. * the driver through the fbdev interfaces.
  1431. *
  1432. * This function will call down into the ->fb_probe callback to let
  1433. * the driver allocate and initialize the fbdev info structure and the drm
  1434. * framebuffer used to back the fbdev. drm_fb_helper_fill_var() and
  1435. * drm_fb_helper_fill_fix() are provided as helpers to setup simple default
  1436. * values for the fbdev info structure.
  1437. *
  1438. * RETURNS:
  1439. * Zero if everything went ok, nonzero otherwise.
  1440. */
  1441. bool drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel)
  1442. {
  1443. struct drm_device *dev = fb_helper->dev;
  1444. int count = 0;
  1445. drm_fb_helper_parse_command_line(fb_helper);
  1446. mutex_lock(&dev->mode_config.mutex);
  1447. count = drm_fb_helper_probe_connector_modes(fb_helper,
  1448. dev->mode_config.max_width,
  1449. dev->mode_config.max_height);
  1450. mutex_unlock(&dev->mode_config.mutex);
  1451. /*
  1452. * we shouldn't end up with no modes here.
  1453. */
  1454. if (count == 0)
  1455. dev_info(fb_helper->dev->dev, "No connectors reported connected with modes\n");
  1456. drm_setup_crtcs(fb_helper);
  1457. return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
  1458. }
  1459. EXPORT_SYMBOL(drm_fb_helper_initial_config);
  1460. /**
  1461. * drm_fb_helper_hotplug_event - respond to a hotplug notification by
  1462. * probing all the outputs attached to the fb
  1463. * @fb_helper: the drm_fb_helper
  1464. *
  1465. * Scan the connectors attached to the fb_helper and try to put together a
  1466. * setup after *notification of a change in output configuration.
  1467. *
  1468. * Called at runtime, takes the mode config locks to be able to check/change the
  1469. * modeset configuration. Must be run from process context (which usually means
  1470. * either the output polling work or a work item launched from the driver's
  1471. * hotplug interrupt).
  1472. *
  1473. * Note that drivers may call this even before calling
  1474. * drm_fb_helper_initial_config but only aftert drm_fb_helper_init. This allows
  1475. * for a race-free fbcon setup and will make sure that the fbdev emulation will
  1476. * not miss any hotplug events.
  1477. *
  1478. * RETURNS:
  1479. * 0 on success and a non-zero error code otherwise.
  1480. */
  1481. int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper)
  1482. {
  1483. struct drm_device *dev = fb_helper->dev;
  1484. u32 max_width, max_height;
  1485. mutex_lock(&fb_helper->dev->mode_config.mutex);
  1486. if (!fb_helper->fb || !drm_fb_helper_is_bound(fb_helper)) {
  1487. fb_helper->delayed_hotplug = true;
  1488. mutex_unlock(&fb_helper->dev->mode_config.mutex);
  1489. return 0;
  1490. }
  1491. DRM_DEBUG_KMS("\n");
  1492. max_width = fb_helper->fb->width;
  1493. max_height = fb_helper->fb->height;
  1494. drm_fb_helper_probe_connector_modes(fb_helper, max_width, max_height);
  1495. mutex_unlock(&fb_helper->dev->mode_config.mutex);
  1496. drm_modeset_lock_all(dev);
  1497. drm_setup_crtcs(fb_helper);
  1498. drm_modeset_unlock_all(dev);
  1499. drm_fb_helper_set_par(fb_helper->fbdev);
  1500. return 0;
  1501. }
  1502. EXPORT_SYMBOL(drm_fb_helper_hotplug_event);
  1503. /* The Kconfig DRM_KMS_HELPER selects FRAMEBUFFER_CONSOLE (if !EXPERT)
  1504. * but the module doesn't depend on any fb console symbols. At least
  1505. * attempt to load fbcon to avoid leaving the system without a usable console.
  1506. */
  1507. #if defined(CONFIG_FRAMEBUFFER_CONSOLE_MODULE) && !defined(CONFIG_EXPERT)
  1508. static int __init drm_fb_helper_modinit(void)
  1509. {
  1510. const char *name = "fbcon";
  1511. struct module *fbcon;
  1512. mutex_lock(&module_mutex);
  1513. fbcon = find_module(name);
  1514. mutex_unlock(&module_mutex);
  1515. if (!fbcon)
  1516. request_module_nowait(name);
  1517. return 0;
  1518. }
  1519. module_init(drm_fb_helper_modinit);
  1520. #endif