drm_fb_helper.c 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023
  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_unlocked() from their ->lastclose callback.
  57. * They 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. * This function is protected against concurrent connector hotadds/removals
  89. * using drm_fb_helper_add_one_connector() and
  90. * drm_fb_helper_remove_one_connector().
  91. */
  92. int drm_fb_helper_single_add_all_connectors(struct drm_fb_helper *fb_helper)
  93. {
  94. struct drm_device *dev = fb_helper->dev;
  95. struct drm_connector *connector;
  96. int i;
  97. mutex_lock(&dev->mode_config.mutex);
  98. drm_for_each_connector(connector, dev) {
  99. struct drm_fb_helper_connector *fb_helper_connector;
  100. fb_helper_connector = kzalloc(sizeof(struct drm_fb_helper_connector), GFP_KERNEL);
  101. if (!fb_helper_connector)
  102. goto fail;
  103. fb_helper_connector->connector = connector;
  104. fb_helper->connector_info[fb_helper->connector_count++] = fb_helper_connector;
  105. }
  106. mutex_unlock(&dev->mode_config.mutex);
  107. return 0;
  108. fail:
  109. for (i = 0; i < fb_helper->connector_count; i++) {
  110. kfree(fb_helper->connector_info[i]);
  111. fb_helper->connector_info[i] = NULL;
  112. }
  113. fb_helper->connector_count = 0;
  114. mutex_unlock(&dev->mode_config.mutex);
  115. return -ENOMEM;
  116. }
  117. EXPORT_SYMBOL(drm_fb_helper_single_add_all_connectors);
  118. int drm_fb_helper_add_one_connector(struct drm_fb_helper *fb_helper, struct drm_connector *connector)
  119. {
  120. struct drm_fb_helper_connector **temp;
  121. struct drm_fb_helper_connector *fb_helper_connector;
  122. WARN_ON(!mutex_is_locked(&fb_helper->dev->mode_config.mutex));
  123. if (fb_helper->connector_count + 1 > fb_helper->connector_info_alloc_count) {
  124. temp = krealloc(fb_helper->connector_info, sizeof(struct drm_fb_helper_connector *) * (fb_helper->connector_count + 1), GFP_KERNEL);
  125. if (!temp)
  126. return -ENOMEM;
  127. fb_helper->connector_info_alloc_count = fb_helper->connector_count + 1;
  128. fb_helper->connector_info = temp;
  129. }
  130. fb_helper_connector = kzalloc(sizeof(struct drm_fb_helper_connector), GFP_KERNEL);
  131. if (!fb_helper_connector)
  132. return -ENOMEM;
  133. fb_helper_connector->connector = connector;
  134. fb_helper->connector_info[fb_helper->connector_count++] = fb_helper_connector;
  135. return 0;
  136. }
  137. EXPORT_SYMBOL(drm_fb_helper_add_one_connector);
  138. static void remove_from_modeset(struct drm_mode_set *set,
  139. struct drm_connector *connector)
  140. {
  141. int i, j;
  142. for (i = 0; i < set->num_connectors; i++) {
  143. if (set->connectors[i] == connector)
  144. break;
  145. }
  146. if (i == set->num_connectors)
  147. return;
  148. for (j = i + 1; j < set->num_connectors; j++) {
  149. set->connectors[j - 1] = set->connectors[j];
  150. }
  151. set->num_connectors--;
  152. /*
  153. * TODO maybe need to makes sure we set it back to !=NULL somewhere?
  154. */
  155. if (set->num_connectors == 0) {
  156. set->fb = NULL;
  157. drm_mode_destroy(connector->dev, set->mode);
  158. set->mode = NULL;
  159. }
  160. }
  161. int drm_fb_helper_remove_one_connector(struct drm_fb_helper *fb_helper,
  162. struct drm_connector *connector)
  163. {
  164. struct drm_fb_helper_connector *fb_helper_connector;
  165. int i, j;
  166. WARN_ON(!mutex_is_locked(&fb_helper->dev->mode_config.mutex));
  167. for (i = 0; i < fb_helper->connector_count; i++) {
  168. if (fb_helper->connector_info[i]->connector == connector)
  169. break;
  170. }
  171. if (i == fb_helper->connector_count)
  172. return -EINVAL;
  173. fb_helper_connector = fb_helper->connector_info[i];
  174. for (j = i + 1; j < fb_helper->connector_count; j++) {
  175. fb_helper->connector_info[j - 1] = fb_helper->connector_info[j];
  176. }
  177. fb_helper->connector_count--;
  178. kfree(fb_helper_connector);
  179. /* also cleanup dangling references to the connector: */
  180. for (i = 0; i < fb_helper->crtc_count; i++)
  181. remove_from_modeset(&fb_helper->crtc_info[i].mode_set, connector);
  182. return 0;
  183. }
  184. EXPORT_SYMBOL(drm_fb_helper_remove_one_connector);
  185. static void drm_fb_helper_save_lut_atomic(struct drm_crtc *crtc, struct drm_fb_helper *helper)
  186. {
  187. uint16_t *r_base, *g_base, *b_base;
  188. int i;
  189. if (helper->funcs->gamma_get == NULL)
  190. return;
  191. r_base = crtc->gamma_store;
  192. g_base = r_base + crtc->gamma_size;
  193. b_base = g_base + crtc->gamma_size;
  194. for (i = 0; i < crtc->gamma_size; i++)
  195. helper->funcs->gamma_get(crtc, &r_base[i], &g_base[i], &b_base[i], i);
  196. }
  197. static void drm_fb_helper_restore_lut_atomic(struct drm_crtc *crtc)
  198. {
  199. uint16_t *r_base, *g_base, *b_base;
  200. if (crtc->funcs->gamma_set == NULL)
  201. return;
  202. r_base = crtc->gamma_store;
  203. g_base = r_base + crtc->gamma_size;
  204. b_base = g_base + crtc->gamma_size;
  205. crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
  206. }
  207. /**
  208. * drm_fb_helper_debug_enter - implementation for ->fb_debug_enter
  209. * @info: fbdev registered by the helper
  210. */
  211. int drm_fb_helper_debug_enter(struct fb_info *info)
  212. {
  213. struct drm_fb_helper *helper = info->par;
  214. const struct drm_crtc_helper_funcs *funcs;
  215. int i;
  216. list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
  217. for (i = 0; i < helper->crtc_count; i++) {
  218. struct drm_mode_set *mode_set =
  219. &helper->crtc_info[i].mode_set;
  220. if (!mode_set->crtc->enabled)
  221. continue;
  222. funcs = mode_set->crtc->helper_private;
  223. drm_fb_helper_save_lut_atomic(mode_set->crtc, helper);
  224. funcs->mode_set_base_atomic(mode_set->crtc,
  225. mode_set->fb,
  226. mode_set->x,
  227. mode_set->y,
  228. ENTER_ATOMIC_MODE_SET);
  229. }
  230. }
  231. return 0;
  232. }
  233. EXPORT_SYMBOL(drm_fb_helper_debug_enter);
  234. /* Find the real fb for a given fb helper CRTC */
  235. static struct drm_framebuffer *drm_mode_config_fb(struct drm_crtc *crtc)
  236. {
  237. struct drm_device *dev = crtc->dev;
  238. struct drm_crtc *c;
  239. drm_for_each_crtc(c, dev) {
  240. if (crtc->base.id == c->base.id)
  241. return c->primary->fb;
  242. }
  243. return NULL;
  244. }
  245. /**
  246. * drm_fb_helper_debug_leave - implementation for ->fb_debug_leave
  247. * @info: fbdev registered by the helper
  248. */
  249. int drm_fb_helper_debug_leave(struct fb_info *info)
  250. {
  251. struct drm_fb_helper *helper = info->par;
  252. struct drm_crtc *crtc;
  253. const struct drm_crtc_helper_funcs *funcs;
  254. struct drm_framebuffer *fb;
  255. int i;
  256. for (i = 0; i < helper->crtc_count; i++) {
  257. struct drm_mode_set *mode_set = &helper->crtc_info[i].mode_set;
  258. crtc = mode_set->crtc;
  259. funcs = crtc->helper_private;
  260. fb = drm_mode_config_fb(crtc);
  261. if (!crtc->enabled)
  262. continue;
  263. if (!fb) {
  264. DRM_ERROR("no fb to restore??\n");
  265. continue;
  266. }
  267. drm_fb_helper_restore_lut_atomic(mode_set->crtc);
  268. funcs->mode_set_base_atomic(mode_set->crtc, fb, crtc->x,
  269. crtc->y, LEAVE_ATOMIC_MODE_SET);
  270. }
  271. return 0;
  272. }
  273. EXPORT_SYMBOL(drm_fb_helper_debug_leave);
  274. static bool restore_fbdev_mode(struct drm_fb_helper *fb_helper)
  275. {
  276. struct drm_device *dev = fb_helper->dev;
  277. struct drm_plane *plane;
  278. bool error = false;
  279. int i;
  280. drm_warn_on_modeset_not_all_locked(dev);
  281. drm_for_each_plane(plane, dev) {
  282. if (plane->type != DRM_PLANE_TYPE_PRIMARY)
  283. drm_plane_force_disable(plane);
  284. if (dev->mode_config.rotation_property) {
  285. drm_mode_plane_set_obj_prop(plane,
  286. dev->mode_config.rotation_property,
  287. BIT(DRM_ROTATE_0));
  288. }
  289. }
  290. for (i = 0; i < fb_helper->crtc_count; i++) {
  291. struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set;
  292. struct drm_crtc *crtc = mode_set->crtc;
  293. int ret;
  294. if (crtc->funcs->cursor_set2) {
  295. ret = crtc->funcs->cursor_set2(crtc, NULL, 0, 0, 0, 0, 0);
  296. if (ret)
  297. error = true;
  298. } else if (crtc->funcs->cursor_set) {
  299. ret = crtc->funcs->cursor_set(crtc, NULL, 0, 0, 0);
  300. if (ret)
  301. error = true;
  302. }
  303. ret = drm_mode_set_config_internal(mode_set);
  304. if (ret)
  305. error = true;
  306. }
  307. return error;
  308. }
  309. /**
  310. * drm_fb_helper_restore_fbdev_mode_unlocked - restore fbdev configuration
  311. * @fb_helper: fbcon to restore
  312. *
  313. * This should be called from driver's drm ->lastclose callback
  314. * when implementing an fbcon on top of kms using this helper. This ensures that
  315. * the user isn't greeted with a black screen when e.g. X dies.
  316. */
  317. bool drm_fb_helper_restore_fbdev_mode_unlocked(struct drm_fb_helper *fb_helper)
  318. {
  319. struct drm_device *dev = fb_helper->dev;
  320. bool ret;
  321. bool do_delayed = false;
  322. drm_modeset_lock_all(dev);
  323. ret = restore_fbdev_mode(fb_helper);
  324. do_delayed = fb_helper->delayed_hotplug;
  325. if (do_delayed)
  326. fb_helper->delayed_hotplug = false;
  327. drm_modeset_unlock_all(dev);
  328. if (do_delayed)
  329. drm_fb_helper_hotplug_event(fb_helper);
  330. return ret;
  331. }
  332. EXPORT_SYMBOL(drm_fb_helper_restore_fbdev_mode_unlocked);
  333. static bool drm_fb_helper_is_bound(struct drm_fb_helper *fb_helper)
  334. {
  335. struct drm_device *dev = fb_helper->dev;
  336. struct drm_crtc *crtc;
  337. int bound = 0, crtcs_bound = 0;
  338. /* Sometimes user space wants everything disabled, so don't steal the
  339. * display if there's a master. */
  340. if (dev->primary->master)
  341. return false;
  342. drm_for_each_crtc(crtc, dev) {
  343. if (crtc->primary->fb)
  344. crtcs_bound++;
  345. if (crtc->primary->fb == fb_helper->fb)
  346. bound++;
  347. }
  348. if (bound < crtcs_bound)
  349. return false;
  350. return true;
  351. }
  352. #ifdef CONFIG_MAGIC_SYSRQ
  353. /*
  354. * restore fbcon display for all kms driver's using this helper, used for sysrq
  355. * and panic handling.
  356. */
  357. static bool drm_fb_helper_force_kernel_mode(void)
  358. {
  359. bool ret, error = false;
  360. struct drm_fb_helper *helper;
  361. if (list_empty(&kernel_fb_helper_list))
  362. return false;
  363. list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
  364. struct drm_device *dev = helper->dev;
  365. if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
  366. continue;
  367. drm_modeset_lock_all(dev);
  368. ret = restore_fbdev_mode(helper);
  369. if (ret)
  370. error = true;
  371. drm_modeset_unlock_all(dev);
  372. }
  373. return error;
  374. }
  375. static void drm_fb_helper_restore_work_fn(struct work_struct *ignored)
  376. {
  377. bool ret;
  378. ret = drm_fb_helper_force_kernel_mode();
  379. if (ret == true)
  380. DRM_ERROR("Failed to restore crtc configuration\n");
  381. }
  382. static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn);
  383. static void drm_fb_helper_sysrq(int dummy1)
  384. {
  385. schedule_work(&drm_fb_helper_restore_work);
  386. }
  387. static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = {
  388. .handler = drm_fb_helper_sysrq,
  389. .help_msg = "force-fb(V)",
  390. .action_msg = "Restore framebuffer console",
  391. };
  392. #else
  393. static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { };
  394. #endif
  395. static void drm_fb_helper_dpms(struct fb_info *info, int dpms_mode)
  396. {
  397. struct drm_fb_helper *fb_helper = info->par;
  398. struct drm_device *dev = fb_helper->dev;
  399. struct drm_crtc *crtc;
  400. struct drm_connector *connector;
  401. int i, j;
  402. /*
  403. * For each CRTC in this fb, turn the connectors on/off.
  404. */
  405. drm_modeset_lock_all(dev);
  406. if (!drm_fb_helper_is_bound(fb_helper)) {
  407. drm_modeset_unlock_all(dev);
  408. return;
  409. }
  410. for (i = 0; i < fb_helper->crtc_count; i++) {
  411. crtc = fb_helper->crtc_info[i].mode_set.crtc;
  412. if (!crtc->enabled)
  413. continue;
  414. /* Walk the connectors & encoders on this fb turning them on/off */
  415. for (j = 0; j < fb_helper->connector_count; j++) {
  416. connector = fb_helper->connector_info[j]->connector;
  417. connector->funcs->dpms(connector, dpms_mode);
  418. drm_object_property_set_value(&connector->base,
  419. dev->mode_config.dpms_property, dpms_mode);
  420. }
  421. }
  422. drm_modeset_unlock_all(dev);
  423. }
  424. /**
  425. * drm_fb_helper_blank - implementation for ->fb_blank
  426. * @blank: desired blanking state
  427. * @info: fbdev registered by the helper
  428. */
  429. int drm_fb_helper_blank(int blank, struct fb_info *info)
  430. {
  431. if (oops_in_progress)
  432. return -EBUSY;
  433. switch (blank) {
  434. /* Display: On; HSync: On, VSync: On */
  435. case FB_BLANK_UNBLANK:
  436. drm_fb_helper_dpms(info, DRM_MODE_DPMS_ON);
  437. break;
  438. /* Display: Off; HSync: On, VSync: On */
  439. case FB_BLANK_NORMAL:
  440. drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
  441. break;
  442. /* Display: Off; HSync: Off, VSync: On */
  443. case FB_BLANK_HSYNC_SUSPEND:
  444. drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
  445. break;
  446. /* Display: Off; HSync: On, VSync: Off */
  447. case FB_BLANK_VSYNC_SUSPEND:
  448. drm_fb_helper_dpms(info, DRM_MODE_DPMS_SUSPEND);
  449. break;
  450. /* Display: Off; HSync: Off, VSync: Off */
  451. case FB_BLANK_POWERDOWN:
  452. drm_fb_helper_dpms(info, DRM_MODE_DPMS_OFF);
  453. break;
  454. }
  455. return 0;
  456. }
  457. EXPORT_SYMBOL(drm_fb_helper_blank);
  458. static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper)
  459. {
  460. int i;
  461. for (i = 0; i < helper->connector_count; i++)
  462. kfree(helper->connector_info[i]);
  463. kfree(helper->connector_info);
  464. for (i = 0; i < helper->crtc_count; i++) {
  465. kfree(helper->crtc_info[i].mode_set.connectors);
  466. if (helper->crtc_info[i].mode_set.mode)
  467. drm_mode_destroy(helper->dev, helper->crtc_info[i].mode_set.mode);
  468. }
  469. kfree(helper->crtc_info);
  470. }
  471. /**
  472. * drm_fb_helper_prepare - setup a drm_fb_helper structure
  473. * @dev: DRM device
  474. * @helper: driver-allocated fbdev helper structure to set up
  475. * @funcs: pointer to structure of functions associate with this helper
  476. *
  477. * Sets up the bare minimum to make the framebuffer helper usable. This is
  478. * useful to implement race-free initialization of the polling helpers.
  479. */
  480. void drm_fb_helper_prepare(struct drm_device *dev, struct drm_fb_helper *helper,
  481. const struct drm_fb_helper_funcs *funcs)
  482. {
  483. INIT_LIST_HEAD(&helper->kernel_fb_list);
  484. helper->funcs = funcs;
  485. helper->dev = dev;
  486. }
  487. EXPORT_SYMBOL(drm_fb_helper_prepare);
  488. /**
  489. * drm_fb_helper_init - initialize a drm_fb_helper structure
  490. * @dev: drm device
  491. * @fb_helper: driver-allocated fbdev helper structure to initialize
  492. * @crtc_count: maximum number of crtcs to support in this fbdev emulation
  493. * @max_conn_count: max connector count
  494. *
  495. * This allocates the structures for the fbdev helper with the given limits.
  496. * Note that this won't yet touch the hardware (through the driver interfaces)
  497. * nor register the fbdev. This is only done in drm_fb_helper_initial_config()
  498. * to allow driver writes more control over the exact init sequence.
  499. *
  500. * Drivers must call drm_fb_helper_prepare() before calling this function.
  501. *
  502. * RETURNS:
  503. * Zero if everything went ok, nonzero otherwise.
  504. */
  505. int drm_fb_helper_init(struct drm_device *dev,
  506. struct drm_fb_helper *fb_helper,
  507. int crtc_count, int max_conn_count)
  508. {
  509. struct drm_crtc *crtc;
  510. int i;
  511. if (!max_conn_count)
  512. return -EINVAL;
  513. fb_helper->crtc_info = kcalloc(crtc_count, sizeof(struct drm_fb_helper_crtc), GFP_KERNEL);
  514. if (!fb_helper->crtc_info)
  515. return -ENOMEM;
  516. fb_helper->crtc_count = crtc_count;
  517. fb_helper->connector_info = kcalloc(dev->mode_config.num_connector, sizeof(struct drm_fb_helper_connector *), GFP_KERNEL);
  518. if (!fb_helper->connector_info) {
  519. kfree(fb_helper->crtc_info);
  520. return -ENOMEM;
  521. }
  522. fb_helper->connector_info_alloc_count = dev->mode_config.num_connector;
  523. fb_helper->connector_count = 0;
  524. for (i = 0; i < crtc_count; i++) {
  525. fb_helper->crtc_info[i].mode_set.connectors =
  526. kcalloc(max_conn_count,
  527. sizeof(struct drm_connector *),
  528. GFP_KERNEL);
  529. if (!fb_helper->crtc_info[i].mode_set.connectors)
  530. goto out_free;
  531. fb_helper->crtc_info[i].mode_set.num_connectors = 0;
  532. }
  533. i = 0;
  534. drm_for_each_crtc(crtc, dev) {
  535. fb_helper->crtc_info[i].mode_set.crtc = crtc;
  536. i++;
  537. }
  538. return 0;
  539. out_free:
  540. drm_fb_helper_crtc_free(fb_helper);
  541. return -ENOMEM;
  542. }
  543. EXPORT_SYMBOL(drm_fb_helper_init);
  544. /**
  545. * drm_fb_helper_alloc_fbi - allocate fb_info and some of its members
  546. * @fb_helper: driver-allocated fbdev helper
  547. *
  548. * A helper to alloc fb_info and the members cmap and apertures. Called
  549. * by the driver within the fb_probe fb_helper callback function.
  550. *
  551. * RETURNS:
  552. * fb_info pointer if things went okay, pointer containing error code
  553. * otherwise
  554. */
  555. struct fb_info *drm_fb_helper_alloc_fbi(struct drm_fb_helper *fb_helper)
  556. {
  557. struct device *dev = fb_helper->dev->dev;
  558. struct fb_info *info;
  559. int ret;
  560. info = framebuffer_alloc(0, dev);
  561. if (!info)
  562. return ERR_PTR(-ENOMEM);
  563. ret = fb_alloc_cmap(&info->cmap, 256, 0);
  564. if (ret)
  565. goto err_release;
  566. info->apertures = alloc_apertures(1);
  567. if (!info->apertures) {
  568. ret = -ENOMEM;
  569. goto err_free_cmap;
  570. }
  571. fb_helper->fbdev = info;
  572. return info;
  573. err_free_cmap:
  574. fb_dealloc_cmap(&info->cmap);
  575. err_release:
  576. framebuffer_release(info);
  577. return ERR_PTR(ret);
  578. }
  579. EXPORT_SYMBOL(drm_fb_helper_alloc_fbi);
  580. /**
  581. * drm_fb_helper_unregister_fbi - unregister fb_info framebuffer device
  582. * @fb_helper: driver-allocated fbdev helper
  583. *
  584. * A wrapper around unregister_framebuffer, to release the fb_info
  585. * framebuffer device
  586. */
  587. void drm_fb_helper_unregister_fbi(struct drm_fb_helper *fb_helper)
  588. {
  589. if (fb_helper && fb_helper->fbdev)
  590. unregister_framebuffer(fb_helper->fbdev);
  591. }
  592. EXPORT_SYMBOL(drm_fb_helper_unregister_fbi);
  593. /**
  594. * drm_fb_helper_release_fbi - dealloc fb_info and its members
  595. * @fb_helper: driver-allocated fbdev helper
  596. *
  597. * A helper to free memory taken by fb_info and the members cmap and
  598. * apertures
  599. */
  600. void drm_fb_helper_release_fbi(struct drm_fb_helper *fb_helper)
  601. {
  602. if (fb_helper) {
  603. struct fb_info *info = fb_helper->fbdev;
  604. if (info) {
  605. if (info->cmap.len)
  606. fb_dealloc_cmap(&info->cmap);
  607. framebuffer_release(info);
  608. }
  609. fb_helper->fbdev = NULL;
  610. }
  611. }
  612. EXPORT_SYMBOL(drm_fb_helper_release_fbi);
  613. void drm_fb_helper_fini(struct drm_fb_helper *fb_helper)
  614. {
  615. if (!list_empty(&fb_helper->kernel_fb_list)) {
  616. list_del(&fb_helper->kernel_fb_list);
  617. if (list_empty(&kernel_fb_helper_list)) {
  618. unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
  619. }
  620. }
  621. drm_fb_helper_crtc_free(fb_helper);
  622. }
  623. EXPORT_SYMBOL(drm_fb_helper_fini);
  624. /**
  625. * drm_fb_helper_unlink_fbi - wrapper around unlink_framebuffer
  626. * @fb_helper: driver-allocated fbdev helper
  627. *
  628. * A wrapper around unlink_framebuffer implemented by fbdev core
  629. */
  630. void drm_fb_helper_unlink_fbi(struct drm_fb_helper *fb_helper)
  631. {
  632. if (fb_helper && fb_helper->fbdev)
  633. unlink_framebuffer(fb_helper->fbdev);
  634. }
  635. EXPORT_SYMBOL(drm_fb_helper_unlink_fbi);
  636. /**
  637. * drm_fb_helper_sys_read - wrapper around fb_sys_read
  638. * @info: fb_info struct pointer
  639. * @buf: userspace buffer to read from framebuffer memory
  640. * @count: number of bytes to read from framebuffer memory
  641. * @ppos: read offset within framebuffer memory
  642. *
  643. * A wrapper around fb_sys_read implemented by fbdev core
  644. */
  645. ssize_t drm_fb_helper_sys_read(struct fb_info *info, char __user *buf,
  646. size_t count, loff_t *ppos)
  647. {
  648. return fb_sys_read(info, buf, count, ppos);
  649. }
  650. EXPORT_SYMBOL(drm_fb_helper_sys_read);
  651. /**
  652. * drm_fb_helper_sys_write - wrapper around fb_sys_write
  653. * @info: fb_info struct pointer
  654. * @buf: userspace buffer to write to framebuffer memory
  655. * @count: number of bytes to write to framebuffer memory
  656. * @ppos: write offset within framebuffer memory
  657. *
  658. * A wrapper around fb_sys_write implemented by fbdev core
  659. */
  660. ssize_t drm_fb_helper_sys_write(struct fb_info *info, const char __user *buf,
  661. size_t count, loff_t *ppos)
  662. {
  663. return fb_sys_write(info, buf, count, ppos);
  664. }
  665. EXPORT_SYMBOL(drm_fb_helper_sys_write);
  666. /**
  667. * drm_fb_helper_sys_fillrect - wrapper around sys_fillrect
  668. * @info: fbdev registered by the helper
  669. * @rect: info about rectangle to fill
  670. *
  671. * A wrapper around sys_fillrect implemented by fbdev core
  672. */
  673. void drm_fb_helper_sys_fillrect(struct fb_info *info,
  674. const struct fb_fillrect *rect)
  675. {
  676. sys_fillrect(info, rect);
  677. }
  678. EXPORT_SYMBOL(drm_fb_helper_sys_fillrect);
  679. /**
  680. * drm_fb_helper_sys_copyarea - wrapper around sys_copyarea
  681. * @info: fbdev registered by the helper
  682. * @area: info about area to copy
  683. *
  684. * A wrapper around sys_copyarea implemented by fbdev core
  685. */
  686. void drm_fb_helper_sys_copyarea(struct fb_info *info,
  687. const struct fb_copyarea *area)
  688. {
  689. sys_copyarea(info, area);
  690. }
  691. EXPORT_SYMBOL(drm_fb_helper_sys_copyarea);
  692. /**
  693. * drm_fb_helper_sys_imageblit - wrapper around sys_imageblit
  694. * @info: fbdev registered by the helper
  695. * @image: info about image to blit
  696. *
  697. * A wrapper around sys_imageblit implemented by fbdev core
  698. */
  699. void drm_fb_helper_sys_imageblit(struct fb_info *info,
  700. const struct fb_image *image)
  701. {
  702. sys_imageblit(info, image);
  703. }
  704. EXPORT_SYMBOL(drm_fb_helper_sys_imageblit);
  705. /**
  706. * drm_fb_helper_cfb_fillrect - wrapper around cfb_fillrect
  707. * @info: fbdev registered by the helper
  708. * @rect: info about rectangle to fill
  709. *
  710. * A wrapper around cfb_imageblit implemented by fbdev core
  711. */
  712. void drm_fb_helper_cfb_fillrect(struct fb_info *info,
  713. const struct fb_fillrect *rect)
  714. {
  715. cfb_fillrect(info, rect);
  716. }
  717. EXPORT_SYMBOL(drm_fb_helper_cfb_fillrect);
  718. /**
  719. * drm_fb_helper_cfb_copyarea - wrapper around cfb_copyarea
  720. * @info: fbdev registered by the helper
  721. * @area: info about area to copy
  722. *
  723. * A wrapper around cfb_copyarea implemented by fbdev core
  724. */
  725. void drm_fb_helper_cfb_copyarea(struct fb_info *info,
  726. const struct fb_copyarea *area)
  727. {
  728. cfb_copyarea(info, area);
  729. }
  730. EXPORT_SYMBOL(drm_fb_helper_cfb_copyarea);
  731. /**
  732. * drm_fb_helper_cfb_imageblit - wrapper around cfb_imageblit
  733. * @info: fbdev registered by the helper
  734. * @image: info about image to blit
  735. *
  736. * A wrapper around cfb_imageblit implemented by fbdev core
  737. */
  738. void drm_fb_helper_cfb_imageblit(struct fb_info *info,
  739. const struct fb_image *image)
  740. {
  741. cfb_imageblit(info, image);
  742. }
  743. EXPORT_SYMBOL(drm_fb_helper_cfb_imageblit);
  744. /**
  745. * drm_fb_helper_set_suspend - wrapper around fb_set_suspend
  746. * @fb_helper: driver-allocated fbdev helper
  747. * @state: desired state, zero to resume, non-zero to suspend
  748. *
  749. * A wrapper around fb_set_suspend implemented by fbdev core
  750. */
  751. void drm_fb_helper_set_suspend(struct drm_fb_helper *fb_helper, int state)
  752. {
  753. if (fb_helper && fb_helper->fbdev)
  754. fb_set_suspend(fb_helper->fbdev, state);
  755. }
  756. EXPORT_SYMBOL(drm_fb_helper_set_suspend);
  757. static int setcolreg(struct drm_crtc *crtc, u16 red, u16 green,
  758. u16 blue, u16 regno, struct fb_info *info)
  759. {
  760. struct drm_fb_helper *fb_helper = info->par;
  761. struct drm_framebuffer *fb = fb_helper->fb;
  762. int pindex;
  763. if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
  764. u32 *palette;
  765. u32 value;
  766. /* place color in psuedopalette */
  767. if (regno > 16)
  768. return -EINVAL;
  769. palette = (u32 *)info->pseudo_palette;
  770. red >>= (16 - info->var.red.length);
  771. green >>= (16 - info->var.green.length);
  772. blue >>= (16 - info->var.blue.length);
  773. value = (red << info->var.red.offset) |
  774. (green << info->var.green.offset) |
  775. (blue << info->var.blue.offset);
  776. if (info->var.transp.length > 0) {
  777. u32 mask = (1 << info->var.transp.length) - 1;
  778. mask <<= info->var.transp.offset;
  779. value |= mask;
  780. }
  781. palette[regno] = value;
  782. return 0;
  783. }
  784. /*
  785. * The driver really shouldn't advertise pseudo/directcolor
  786. * visuals if it can't deal with the palette.
  787. */
  788. if (WARN_ON(!fb_helper->funcs->gamma_set ||
  789. !fb_helper->funcs->gamma_get))
  790. return -EINVAL;
  791. pindex = regno;
  792. if (fb->bits_per_pixel == 16) {
  793. pindex = regno << 3;
  794. if (fb->depth == 16 && regno > 63)
  795. return -EINVAL;
  796. if (fb->depth == 15 && regno > 31)
  797. return -EINVAL;
  798. if (fb->depth == 16) {
  799. u16 r, g, b;
  800. int i;
  801. if (regno < 32) {
  802. for (i = 0; i < 8; i++)
  803. fb_helper->funcs->gamma_set(crtc, red,
  804. green, blue, pindex + i);
  805. }
  806. fb_helper->funcs->gamma_get(crtc, &r,
  807. &g, &b,
  808. pindex >> 1);
  809. for (i = 0; i < 4; i++)
  810. fb_helper->funcs->gamma_set(crtc, r,
  811. green, b,
  812. (pindex >> 1) + i);
  813. }
  814. }
  815. if (fb->depth != 16)
  816. fb_helper->funcs->gamma_set(crtc, red, green, blue, pindex);
  817. return 0;
  818. }
  819. /**
  820. * drm_fb_helper_setcmap - implementation for ->fb_setcmap
  821. * @cmap: cmap to set
  822. * @info: fbdev registered by the helper
  823. */
  824. int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info)
  825. {
  826. struct drm_fb_helper *fb_helper = info->par;
  827. struct drm_device *dev = fb_helper->dev;
  828. const struct drm_crtc_helper_funcs *crtc_funcs;
  829. u16 *red, *green, *blue, *transp;
  830. struct drm_crtc *crtc;
  831. int i, j, rc = 0;
  832. int start;
  833. if (oops_in_progress)
  834. return -EBUSY;
  835. drm_modeset_lock_all(dev);
  836. if (!drm_fb_helper_is_bound(fb_helper)) {
  837. drm_modeset_unlock_all(dev);
  838. return -EBUSY;
  839. }
  840. for (i = 0; i < fb_helper->crtc_count; i++) {
  841. crtc = fb_helper->crtc_info[i].mode_set.crtc;
  842. crtc_funcs = crtc->helper_private;
  843. red = cmap->red;
  844. green = cmap->green;
  845. blue = cmap->blue;
  846. transp = cmap->transp;
  847. start = cmap->start;
  848. for (j = 0; j < cmap->len; j++) {
  849. u16 hred, hgreen, hblue, htransp = 0xffff;
  850. hred = *red++;
  851. hgreen = *green++;
  852. hblue = *blue++;
  853. if (transp)
  854. htransp = *transp++;
  855. rc = setcolreg(crtc, hred, hgreen, hblue, start++, info);
  856. if (rc)
  857. goto out;
  858. }
  859. if (crtc_funcs->load_lut)
  860. crtc_funcs->load_lut(crtc);
  861. }
  862. out:
  863. drm_modeset_unlock_all(dev);
  864. return rc;
  865. }
  866. EXPORT_SYMBOL(drm_fb_helper_setcmap);
  867. /**
  868. * drm_fb_helper_check_var - implementation for ->fb_check_var
  869. * @var: screeninfo to check
  870. * @info: fbdev registered by the helper
  871. */
  872. int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
  873. struct fb_info *info)
  874. {
  875. struct drm_fb_helper *fb_helper = info->par;
  876. struct drm_framebuffer *fb = fb_helper->fb;
  877. int depth;
  878. if (var->pixclock != 0 || in_dbg_master())
  879. return -EINVAL;
  880. /* Need to resize the fb object !!! */
  881. if (var->bits_per_pixel > fb->bits_per_pixel ||
  882. var->xres > fb->width || var->yres > fb->height ||
  883. var->xres_virtual > fb->width || var->yres_virtual > fb->height) {
  884. DRM_DEBUG("fb userspace requested width/height/bpp is greater than current fb "
  885. "request %dx%d-%d (virtual %dx%d) > %dx%d-%d\n",
  886. var->xres, var->yres, var->bits_per_pixel,
  887. var->xres_virtual, var->yres_virtual,
  888. fb->width, fb->height, fb->bits_per_pixel);
  889. return -EINVAL;
  890. }
  891. switch (var->bits_per_pixel) {
  892. case 16:
  893. depth = (var->green.length == 6) ? 16 : 15;
  894. break;
  895. case 32:
  896. depth = (var->transp.length > 0) ? 32 : 24;
  897. break;
  898. default:
  899. depth = var->bits_per_pixel;
  900. break;
  901. }
  902. switch (depth) {
  903. case 8:
  904. var->red.offset = 0;
  905. var->green.offset = 0;
  906. var->blue.offset = 0;
  907. var->red.length = 8;
  908. var->green.length = 8;
  909. var->blue.length = 8;
  910. var->transp.length = 0;
  911. var->transp.offset = 0;
  912. break;
  913. case 15:
  914. var->red.offset = 10;
  915. var->green.offset = 5;
  916. var->blue.offset = 0;
  917. var->red.length = 5;
  918. var->green.length = 5;
  919. var->blue.length = 5;
  920. var->transp.length = 1;
  921. var->transp.offset = 15;
  922. break;
  923. case 16:
  924. var->red.offset = 11;
  925. var->green.offset = 5;
  926. var->blue.offset = 0;
  927. var->red.length = 5;
  928. var->green.length = 6;
  929. var->blue.length = 5;
  930. var->transp.length = 0;
  931. var->transp.offset = 0;
  932. break;
  933. case 24:
  934. var->red.offset = 16;
  935. var->green.offset = 8;
  936. var->blue.offset = 0;
  937. var->red.length = 8;
  938. var->green.length = 8;
  939. var->blue.length = 8;
  940. var->transp.length = 0;
  941. var->transp.offset = 0;
  942. break;
  943. case 32:
  944. var->red.offset = 16;
  945. var->green.offset = 8;
  946. var->blue.offset = 0;
  947. var->red.length = 8;
  948. var->green.length = 8;
  949. var->blue.length = 8;
  950. var->transp.length = 8;
  951. var->transp.offset = 24;
  952. break;
  953. default:
  954. return -EINVAL;
  955. }
  956. return 0;
  957. }
  958. EXPORT_SYMBOL(drm_fb_helper_check_var);
  959. /**
  960. * drm_fb_helper_set_par - implementation for ->fb_set_par
  961. * @info: fbdev registered by the helper
  962. *
  963. * This will let fbcon do the mode init and is called at initialization time by
  964. * the fbdev core when registering the driver, and later on through the hotplug
  965. * callback.
  966. */
  967. int drm_fb_helper_set_par(struct fb_info *info)
  968. {
  969. struct drm_fb_helper *fb_helper = info->par;
  970. struct fb_var_screeninfo *var = &info->var;
  971. if (oops_in_progress)
  972. return -EBUSY;
  973. if (var->pixclock != 0) {
  974. DRM_ERROR("PIXEL CLOCK SET\n");
  975. return -EINVAL;
  976. }
  977. drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper);
  978. return 0;
  979. }
  980. EXPORT_SYMBOL(drm_fb_helper_set_par);
  981. /**
  982. * drm_fb_helper_pan_display - implementation for ->fb_pan_display
  983. * @var: updated screen information
  984. * @info: fbdev registered by the helper
  985. */
  986. int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
  987. struct fb_info *info)
  988. {
  989. struct drm_fb_helper *fb_helper = info->par;
  990. struct drm_device *dev = fb_helper->dev;
  991. struct drm_mode_set *modeset;
  992. int ret = 0;
  993. int i;
  994. if (oops_in_progress)
  995. return -EBUSY;
  996. drm_modeset_lock_all(dev);
  997. if (!drm_fb_helper_is_bound(fb_helper)) {
  998. drm_modeset_unlock_all(dev);
  999. return -EBUSY;
  1000. }
  1001. for (i = 0; i < fb_helper->crtc_count; i++) {
  1002. modeset = &fb_helper->crtc_info[i].mode_set;
  1003. modeset->x = var->xoffset;
  1004. modeset->y = var->yoffset;
  1005. if (modeset->num_connectors) {
  1006. ret = drm_mode_set_config_internal(modeset);
  1007. if (!ret) {
  1008. info->var.xoffset = var->xoffset;
  1009. info->var.yoffset = var->yoffset;
  1010. }
  1011. }
  1012. }
  1013. drm_modeset_unlock_all(dev);
  1014. return ret;
  1015. }
  1016. EXPORT_SYMBOL(drm_fb_helper_pan_display);
  1017. /*
  1018. * Allocates the backing storage and sets up the fbdev info structure through
  1019. * the ->fb_probe callback and then registers the fbdev and sets up the panic
  1020. * notifier.
  1021. */
  1022. static int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
  1023. int preferred_bpp)
  1024. {
  1025. int ret = 0;
  1026. int crtc_count = 0;
  1027. int i;
  1028. struct fb_info *info;
  1029. struct drm_fb_helper_surface_size sizes;
  1030. int gamma_size = 0;
  1031. memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size));
  1032. sizes.surface_depth = 24;
  1033. sizes.surface_bpp = 32;
  1034. sizes.fb_width = (unsigned)-1;
  1035. sizes.fb_height = (unsigned)-1;
  1036. /* if driver picks 8 or 16 by default use that
  1037. for both depth/bpp */
  1038. if (preferred_bpp != sizes.surface_bpp)
  1039. sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
  1040. /* first up get a count of crtcs now in use and new min/maxes width/heights */
  1041. for (i = 0; i < fb_helper->connector_count; i++) {
  1042. struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i];
  1043. struct drm_cmdline_mode *cmdline_mode;
  1044. cmdline_mode = &fb_helper_conn->connector->cmdline_mode;
  1045. if (cmdline_mode->bpp_specified) {
  1046. switch (cmdline_mode->bpp) {
  1047. case 8:
  1048. sizes.surface_depth = sizes.surface_bpp = 8;
  1049. break;
  1050. case 15:
  1051. sizes.surface_depth = 15;
  1052. sizes.surface_bpp = 16;
  1053. break;
  1054. case 16:
  1055. sizes.surface_depth = sizes.surface_bpp = 16;
  1056. break;
  1057. case 24:
  1058. sizes.surface_depth = sizes.surface_bpp = 24;
  1059. break;
  1060. case 32:
  1061. sizes.surface_depth = 24;
  1062. sizes.surface_bpp = 32;
  1063. break;
  1064. }
  1065. break;
  1066. }
  1067. }
  1068. crtc_count = 0;
  1069. for (i = 0; i < fb_helper->crtc_count; i++) {
  1070. struct drm_display_mode *desired_mode;
  1071. struct drm_mode_set *mode_set;
  1072. int x, y, j;
  1073. /* in case of tile group, are we the last tile vert or horiz?
  1074. * If no tile group you are always the last one both vertically
  1075. * and horizontally
  1076. */
  1077. bool lastv = true, lasth = true;
  1078. desired_mode = fb_helper->crtc_info[i].desired_mode;
  1079. mode_set = &fb_helper->crtc_info[i].mode_set;
  1080. if (!desired_mode)
  1081. continue;
  1082. crtc_count++;
  1083. x = fb_helper->crtc_info[i].x;
  1084. y = fb_helper->crtc_info[i].y;
  1085. if (gamma_size == 0)
  1086. gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size;
  1087. sizes.surface_width = max_t(u32, desired_mode->hdisplay + x, sizes.surface_width);
  1088. sizes.surface_height = max_t(u32, desired_mode->vdisplay + y, sizes.surface_height);
  1089. for (j = 0; j < mode_set->num_connectors; j++) {
  1090. struct drm_connector *connector = mode_set->connectors[j];
  1091. if (connector->has_tile) {
  1092. lasth = (connector->tile_h_loc == (connector->num_h_tile - 1));
  1093. lastv = (connector->tile_v_loc == (connector->num_v_tile - 1));
  1094. /* cloning to multiple tiles is just crazy-talk, so: */
  1095. break;
  1096. }
  1097. }
  1098. if (lasth)
  1099. sizes.fb_width = min_t(u32, desired_mode->hdisplay + x, sizes.fb_width);
  1100. if (lastv)
  1101. sizes.fb_height = min_t(u32, desired_mode->vdisplay + y, sizes.fb_height);
  1102. }
  1103. if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) {
  1104. /* hmm everyone went away - assume VGA cable just fell out
  1105. and will come back later. */
  1106. DRM_INFO("Cannot find any crtc or sizes - going 1024x768\n");
  1107. sizes.fb_width = sizes.surface_width = 1024;
  1108. sizes.fb_height = sizes.surface_height = 768;
  1109. }
  1110. /* push down into drivers */
  1111. ret = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes);
  1112. if (ret < 0)
  1113. return ret;
  1114. info = fb_helper->fbdev;
  1115. /*
  1116. * Set the fb pointer - usually drm_setup_crtcs does this for hotplug
  1117. * events, but at init time drm_setup_crtcs needs to be called before
  1118. * the fb is allocated (since we need to figure out the desired size of
  1119. * the fb before we can allocate it ...). Hence we need to fix things up
  1120. * here again.
  1121. */
  1122. for (i = 0; i < fb_helper->crtc_count; i++)
  1123. if (fb_helper->crtc_info[i].mode_set.num_connectors)
  1124. fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb;
  1125. info->var.pixclock = 0;
  1126. if (register_framebuffer(info) < 0)
  1127. return -EINVAL;
  1128. dev_info(fb_helper->dev->dev, "fb%d: %s frame buffer device\n",
  1129. info->node, info->fix.id);
  1130. if (list_empty(&kernel_fb_helper_list)) {
  1131. register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
  1132. }
  1133. list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
  1134. return 0;
  1135. }
  1136. /**
  1137. * drm_fb_helper_fill_fix - initializes fixed fbdev information
  1138. * @info: fbdev registered by the helper
  1139. * @pitch: desired pitch
  1140. * @depth: desired depth
  1141. *
  1142. * Helper to fill in the fixed fbdev information useful for a non-accelerated
  1143. * fbdev emulations. Drivers which support acceleration methods which impose
  1144. * additional constraints need to set up their own limits.
  1145. *
  1146. * Drivers should call this (or their equivalent setup code) from their
  1147. * ->fb_probe callback.
  1148. */
  1149. void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
  1150. uint32_t depth)
  1151. {
  1152. info->fix.type = FB_TYPE_PACKED_PIXELS;
  1153. info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR :
  1154. FB_VISUAL_TRUECOLOR;
  1155. info->fix.mmio_start = 0;
  1156. info->fix.mmio_len = 0;
  1157. info->fix.type_aux = 0;
  1158. info->fix.xpanstep = 1; /* doing it in hw */
  1159. info->fix.ypanstep = 1; /* doing it in hw */
  1160. info->fix.ywrapstep = 0;
  1161. info->fix.accel = FB_ACCEL_NONE;
  1162. info->fix.line_length = pitch;
  1163. return;
  1164. }
  1165. EXPORT_SYMBOL(drm_fb_helper_fill_fix);
  1166. /**
  1167. * drm_fb_helper_fill_var - initalizes variable fbdev information
  1168. * @info: fbdev instance to set up
  1169. * @fb_helper: fb helper instance to use as template
  1170. * @fb_width: desired fb width
  1171. * @fb_height: desired fb height
  1172. *
  1173. * Sets up the variable fbdev metainformation from the given fb helper instance
  1174. * and the drm framebuffer allocated in fb_helper->fb.
  1175. *
  1176. * Drivers should call this (or their equivalent setup code) from their
  1177. * ->fb_probe callback after having allocated the fbdev backing
  1178. * storage framebuffer.
  1179. */
  1180. void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper,
  1181. uint32_t fb_width, uint32_t fb_height)
  1182. {
  1183. struct drm_framebuffer *fb = fb_helper->fb;
  1184. info->pseudo_palette = fb_helper->pseudo_palette;
  1185. info->var.xres_virtual = fb->width;
  1186. info->var.yres_virtual = fb->height;
  1187. info->var.bits_per_pixel = fb->bits_per_pixel;
  1188. info->var.accel_flags = FB_ACCELF_TEXT;
  1189. info->var.xoffset = 0;
  1190. info->var.yoffset = 0;
  1191. info->var.activate = FB_ACTIVATE_NOW;
  1192. info->var.height = -1;
  1193. info->var.width = -1;
  1194. switch (fb->depth) {
  1195. case 8:
  1196. info->var.red.offset = 0;
  1197. info->var.green.offset = 0;
  1198. info->var.blue.offset = 0;
  1199. info->var.red.length = 8; /* 8bit DAC */
  1200. info->var.green.length = 8;
  1201. info->var.blue.length = 8;
  1202. info->var.transp.offset = 0;
  1203. info->var.transp.length = 0;
  1204. break;
  1205. case 15:
  1206. info->var.red.offset = 10;
  1207. info->var.green.offset = 5;
  1208. info->var.blue.offset = 0;
  1209. info->var.red.length = 5;
  1210. info->var.green.length = 5;
  1211. info->var.blue.length = 5;
  1212. info->var.transp.offset = 15;
  1213. info->var.transp.length = 1;
  1214. break;
  1215. case 16:
  1216. info->var.red.offset = 11;
  1217. info->var.green.offset = 5;
  1218. info->var.blue.offset = 0;
  1219. info->var.red.length = 5;
  1220. info->var.green.length = 6;
  1221. info->var.blue.length = 5;
  1222. info->var.transp.offset = 0;
  1223. break;
  1224. case 24:
  1225. info->var.red.offset = 16;
  1226. info->var.green.offset = 8;
  1227. info->var.blue.offset = 0;
  1228. info->var.red.length = 8;
  1229. info->var.green.length = 8;
  1230. info->var.blue.length = 8;
  1231. info->var.transp.offset = 0;
  1232. info->var.transp.length = 0;
  1233. break;
  1234. case 32:
  1235. info->var.red.offset = 16;
  1236. info->var.green.offset = 8;
  1237. info->var.blue.offset = 0;
  1238. info->var.red.length = 8;
  1239. info->var.green.length = 8;
  1240. info->var.blue.length = 8;
  1241. info->var.transp.offset = 24;
  1242. info->var.transp.length = 8;
  1243. break;
  1244. default:
  1245. break;
  1246. }
  1247. info->var.xres = fb_width;
  1248. info->var.yres = fb_height;
  1249. }
  1250. EXPORT_SYMBOL(drm_fb_helper_fill_var);
  1251. static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper,
  1252. uint32_t maxX,
  1253. uint32_t maxY)
  1254. {
  1255. struct drm_connector *connector;
  1256. int count = 0;
  1257. int i;
  1258. for (i = 0; i < fb_helper->connector_count; i++) {
  1259. connector = fb_helper->connector_info[i]->connector;
  1260. count += connector->funcs->fill_modes(connector, maxX, maxY);
  1261. }
  1262. return count;
  1263. }
  1264. struct drm_display_mode *drm_has_preferred_mode(struct drm_fb_helper_connector *fb_connector, int width, int height)
  1265. {
  1266. struct drm_display_mode *mode;
  1267. list_for_each_entry(mode, &fb_connector->connector->modes, head) {
  1268. if (mode->hdisplay > width ||
  1269. mode->vdisplay > height)
  1270. continue;
  1271. if (mode->type & DRM_MODE_TYPE_PREFERRED)
  1272. return mode;
  1273. }
  1274. return NULL;
  1275. }
  1276. EXPORT_SYMBOL(drm_has_preferred_mode);
  1277. static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector)
  1278. {
  1279. return fb_connector->connector->cmdline_mode.specified;
  1280. }
  1281. struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn,
  1282. int width, int height)
  1283. {
  1284. struct drm_cmdline_mode *cmdline_mode;
  1285. struct drm_display_mode *mode;
  1286. bool prefer_non_interlace;
  1287. cmdline_mode = &fb_helper_conn->connector->cmdline_mode;
  1288. if (cmdline_mode->specified == false)
  1289. return NULL;
  1290. /* attempt to find a matching mode in the list of modes
  1291. * we have gotten so far, if not add a CVT mode that conforms
  1292. */
  1293. if (cmdline_mode->rb || cmdline_mode->margins)
  1294. goto create_mode;
  1295. prefer_non_interlace = !cmdline_mode->interlace;
  1296. again:
  1297. list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
  1298. /* check width/height */
  1299. if (mode->hdisplay != cmdline_mode->xres ||
  1300. mode->vdisplay != cmdline_mode->yres)
  1301. continue;
  1302. if (cmdline_mode->refresh_specified) {
  1303. if (mode->vrefresh != cmdline_mode->refresh)
  1304. continue;
  1305. }
  1306. if (cmdline_mode->interlace) {
  1307. if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
  1308. continue;
  1309. } else if (prefer_non_interlace) {
  1310. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  1311. continue;
  1312. }
  1313. return mode;
  1314. }
  1315. if (prefer_non_interlace) {
  1316. prefer_non_interlace = false;
  1317. goto again;
  1318. }
  1319. create_mode:
  1320. mode = drm_mode_create_from_cmdline_mode(fb_helper_conn->connector->dev,
  1321. cmdline_mode);
  1322. list_add(&mode->head, &fb_helper_conn->connector->modes);
  1323. return mode;
  1324. }
  1325. EXPORT_SYMBOL(drm_pick_cmdline_mode);
  1326. static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
  1327. {
  1328. bool enable;
  1329. if (strict)
  1330. enable = connector->status == connector_status_connected;
  1331. else
  1332. enable = connector->status != connector_status_disconnected;
  1333. return enable;
  1334. }
  1335. static void drm_enable_connectors(struct drm_fb_helper *fb_helper,
  1336. bool *enabled)
  1337. {
  1338. bool any_enabled = false;
  1339. struct drm_connector *connector;
  1340. int i = 0;
  1341. for (i = 0; i < fb_helper->connector_count; i++) {
  1342. connector = fb_helper->connector_info[i]->connector;
  1343. enabled[i] = drm_connector_enabled(connector, true);
  1344. DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
  1345. enabled[i] ? "yes" : "no");
  1346. any_enabled |= enabled[i];
  1347. }
  1348. if (any_enabled)
  1349. return;
  1350. for (i = 0; i < fb_helper->connector_count; i++) {
  1351. connector = fb_helper->connector_info[i]->connector;
  1352. enabled[i] = drm_connector_enabled(connector, false);
  1353. }
  1354. }
  1355. static bool drm_target_cloned(struct drm_fb_helper *fb_helper,
  1356. struct drm_display_mode **modes,
  1357. struct drm_fb_offset *offsets,
  1358. bool *enabled, int width, int height)
  1359. {
  1360. int count, i, j;
  1361. bool can_clone = false;
  1362. struct drm_fb_helper_connector *fb_helper_conn;
  1363. struct drm_display_mode *dmt_mode, *mode;
  1364. /* only contemplate cloning in the single crtc case */
  1365. if (fb_helper->crtc_count > 1)
  1366. return false;
  1367. count = 0;
  1368. for (i = 0; i < fb_helper->connector_count; i++) {
  1369. if (enabled[i])
  1370. count++;
  1371. }
  1372. /* only contemplate cloning if more than one connector is enabled */
  1373. if (count <= 1)
  1374. return false;
  1375. /* check the command line or if nothing common pick 1024x768 */
  1376. can_clone = true;
  1377. for (i = 0; i < fb_helper->connector_count; i++) {
  1378. if (!enabled[i])
  1379. continue;
  1380. fb_helper_conn = fb_helper->connector_info[i];
  1381. modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
  1382. if (!modes[i]) {
  1383. can_clone = false;
  1384. break;
  1385. }
  1386. for (j = 0; j < i; j++) {
  1387. if (!enabled[j])
  1388. continue;
  1389. if (!drm_mode_equal(modes[j], modes[i]))
  1390. can_clone = false;
  1391. }
  1392. }
  1393. if (can_clone) {
  1394. DRM_DEBUG_KMS("can clone using command line\n");
  1395. return true;
  1396. }
  1397. /* try and find a 1024x768 mode on each connector */
  1398. can_clone = true;
  1399. dmt_mode = drm_mode_find_dmt(fb_helper->dev, 1024, 768, 60, false);
  1400. for (i = 0; i < fb_helper->connector_count; i++) {
  1401. if (!enabled[i])
  1402. continue;
  1403. fb_helper_conn = fb_helper->connector_info[i];
  1404. list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
  1405. if (drm_mode_equal(mode, dmt_mode))
  1406. modes[i] = mode;
  1407. }
  1408. if (!modes[i])
  1409. can_clone = false;
  1410. }
  1411. if (can_clone) {
  1412. DRM_DEBUG_KMS("can clone using 1024x768\n");
  1413. return true;
  1414. }
  1415. DRM_INFO("kms: can't enable cloning when we probably wanted to.\n");
  1416. return false;
  1417. }
  1418. static int drm_get_tile_offsets(struct drm_fb_helper *fb_helper,
  1419. struct drm_display_mode **modes,
  1420. struct drm_fb_offset *offsets,
  1421. int idx,
  1422. int h_idx, int v_idx)
  1423. {
  1424. struct drm_fb_helper_connector *fb_helper_conn;
  1425. int i;
  1426. int hoffset = 0, voffset = 0;
  1427. for (i = 0; i < fb_helper->connector_count; i++) {
  1428. fb_helper_conn = fb_helper->connector_info[i];
  1429. if (!fb_helper_conn->connector->has_tile)
  1430. continue;
  1431. if (!modes[i] && (h_idx || v_idx)) {
  1432. DRM_DEBUG_KMS("no modes for connector tiled %d %d\n", i,
  1433. fb_helper_conn->connector->base.id);
  1434. continue;
  1435. }
  1436. if (fb_helper_conn->connector->tile_h_loc < h_idx)
  1437. hoffset += modes[i]->hdisplay;
  1438. if (fb_helper_conn->connector->tile_v_loc < v_idx)
  1439. voffset += modes[i]->vdisplay;
  1440. }
  1441. offsets[idx].x = hoffset;
  1442. offsets[idx].y = voffset;
  1443. DRM_DEBUG_KMS("returned %d %d for %d %d\n", hoffset, voffset, h_idx, v_idx);
  1444. return 0;
  1445. }
  1446. static bool drm_target_preferred(struct drm_fb_helper *fb_helper,
  1447. struct drm_display_mode **modes,
  1448. struct drm_fb_offset *offsets,
  1449. bool *enabled, int width, int height)
  1450. {
  1451. struct drm_fb_helper_connector *fb_helper_conn;
  1452. int i;
  1453. uint64_t conn_configured = 0, mask;
  1454. int tile_pass = 0;
  1455. mask = (1 << fb_helper->connector_count) - 1;
  1456. retry:
  1457. for (i = 0; i < fb_helper->connector_count; i++) {
  1458. fb_helper_conn = fb_helper->connector_info[i];
  1459. if (conn_configured & (1 << i))
  1460. continue;
  1461. if (enabled[i] == false) {
  1462. conn_configured |= (1 << i);
  1463. continue;
  1464. }
  1465. /* first pass over all the untiled connectors */
  1466. if (tile_pass == 0 && fb_helper_conn->connector->has_tile)
  1467. continue;
  1468. if (tile_pass == 1) {
  1469. if (fb_helper_conn->connector->tile_h_loc != 0 ||
  1470. fb_helper_conn->connector->tile_v_loc != 0)
  1471. continue;
  1472. } else {
  1473. if (fb_helper_conn->connector->tile_h_loc != tile_pass -1 &&
  1474. fb_helper_conn->connector->tile_v_loc != tile_pass - 1)
  1475. /* if this tile_pass doesn't cover any of the tiles - keep going */
  1476. continue;
  1477. /* find the tile offsets for this pass - need
  1478. to find all tiles left and above */
  1479. drm_get_tile_offsets(fb_helper, modes, offsets,
  1480. i, fb_helper_conn->connector->tile_h_loc, fb_helper_conn->connector->tile_v_loc);
  1481. }
  1482. DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
  1483. fb_helper_conn->connector->base.id);
  1484. /* got for command line mode first */
  1485. modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
  1486. if (!modes[i]) {
  1487. DRM_DEBUG_KMS("looking for preferred mode on connector %d %d\n",
  1488. fb_helper_conn->connector->base.id, fb_helper_conn->connector->tile_group ? fb_helper_conn->connector->tile_group->id : 0);
  1489. modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height);
  1490. }
  1491. /* No preferred modes, pick one off the list */
  1492. if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) {
  1493. list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head)
  1494. break;
  1495. }
  1496. DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
  1497. "none");
  1498. conn_configured |= (1 << i);
  1499. }
  1500. if ((conn_configured & mask) != mask) {
  1501. tile_pass++;
  1502. goto retry;
  1503. }
  1504. return true;
  1505. }
  1506. static int drm_pick_crtcs(struct drm_fb_helper *fb_helper,
  1507. struct drm_fb_helper_crtc **best_crtcs,
  1508. struct drm_display_mode **modes,
  1509. int n, int width, int height)
  1510. {
  1511. int c, o;
  1512. struct drm_device *dev = fb_helper->dev;
  1513. struct drm_connector *connector;
  1514. const struct drm_connector_helper_funcs *connector_funcs;
  1515. struct drm_encoder *encoder;
  1516. int my_score, best_score, score;
  1517. struct drm_fb_helper_crtc **crtcs, *crtc;
  1518. struct drm_fb_helper_connector *fb_helper_conn;
  1519. if (n == fb_helper->connector_count)
  1520. return 0;
  1521. fb_helper_conn = fb_helper->connector_info[n];
  1522. connector = fb_helper_conn->connector;
  1523. best_crtcs[n] = NULL;
  1524. best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height);
  1525. if (modes[n] == NULL)
  1526. return best_score;
  1527. crtcs = kzalloc(dev->mode_config.num_connector *
  1528. sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
  1529. if (!crtcs)
  1530. return best_score;
  1531. my_score = 1;
  1532. if (connector->status == connector_status_connected)
  1533. my_score++;
  1534. if (drm_has_cmdline_mode(fb_helper_conn))
  1535. my_score++;
  1536. if (drm_has_preferred_mode(fb_helper_conn, width, height))
  1537. my_score++;
  1538. connector_funcs = connector->helper_private;
  1539. encoder = connector_funcs->best_encoder(connector);
  1540. if (!encoder)
  1541. goto out;
  1542. /* select a crtc for this connector and then attempt to configure
  1543. remaining connectors */
  1544. for (c = 0; c < fb_helper->crtc_count; c++) {
  1545. crtc = &fb_helper->crtc_info[c];
  1546. if ((encoder->possible_crtcs & (1 << c)) == 0)
  1547. continue;
  1548. for (o = 0; o < n; o++)
  1549. if (best_crtcs[o] == crtc)
  1550. break;
  1551. if (o < n) {
  1552. /* ignore cloning unless only a single crtc */
  1553. if (fb_helper->crtc_count > 1)
  1554. continue;
  1555. if (!drm_mode_equal(modes[o], modes[n]))
  1556. continue;
  1557. }
  1558. crtcs[n] = crtc;
  1559. memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *));
  1560. score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1,
  1561. width, height);
  1562. if (score > best_score) {
  1563. best_score = score;
  1564. memcpy(best_crtcs, crtcs,
  1565. dev->mode_config.num_connector *
  1566. sizeof(struct drm_fb_helper_crtc *));
  1567. }
  1568. }
  1569. out:
  1570. kfree(crtcs);
  1571. return best_score;
  1572. }
  1573. static void drm_setup_crtcs(struct drm_fb_helper *fb_helper)
  1574. {
  1575. struct drm_device *dev = fb_helper->dev;
  1576. struct drm_fb_helper_crtc **crtcs;
  1577. struct drm_display_mode **modes;
  1578. struct drm_fb_offset *offsets;
  1579. struct drm_mode_set *modeset;
  1580. bool *enabled;
  1581. int width, height;
  1582. int i;
  1583. DRM_DEBUG_KMS("\n");
  1584. width = dev->mode_config.max_width;
  1585. height = dev->mode_config.max_height;
  1586. crtcs = kcalloc(dev->mode_config.num_connector,
  1587. sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
  1588. modes = kcalloc(dev->mode_config.num_connector,
  1589. sizeof(struct drm_display_mode *), GFP_KERNEL);
  1590. offsets = kcalloc(dev->mode_config.num_connector,
  1591. sizeof(struct drm_fb_offset), GFP_KERNEL);
  1592. enabled = kcalloc(dev->mode_config.num_connector,
  1593. sizeof(bool), GFP_KERNEL);
  1594. if (!crtcs || !modes || !enabled || !offsets) {
  1595. DRM_ERROR("Memory allocation failed\n");
  1596. goto out;
  1597. }
  1598. drm_enable_connectors(fb_helper, enabled);
  1599. if (!(fb_helper->funcs->initial_config &&
  1600. fb_helper->funcs->initial_config(fb_helper, crtcs, modes,
  1601. offsets,
  1602. enabled, width, height))) {
  1603. memset(modes, 0, dev->mode_config.num_connector*sizeof(modes[0]));
  1604. memset(crtcs, 0, dev->mode_config.num_connector*sizeof(crtcs[0]));
  1605. memset(offsets, 0, dev->mode_config.num_connector*sizeof(offsets[0]));
  1606. if (!drm_target_cloned(fb_helper, modes, offsets,
  1607. enabled, width, height) &&
  1608. !drm_target_preferred(fb_helper, modes, offsets,
  1609. enabled, width, height))
  1610. DRM_ERROR("Unable to find initial modes\n");
  1611. DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n",
  1612. width, height);
  1613. drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height);
  1614. }
  1615. /* need to set the modesets up here for use later */
  1616. /* fill out the connector<->crtc mappings into the modesets */
  1617. for (i = 0; i < fb_helper->crtc_count; i++) {
  1618. modeset = &fb_helper->crtc_info[i].mode_set;
  1619. modeset->num_connectors = 0;
  1620. modeset->fb = NULL;
  1621. }
  1622. for (i = 0; i < fb_helper->connector_count; i++) {
  1623. struct drm_display_mode *mode = modes[i];
  1624. struct drm_fb_helper_crtc *fb_crtc = crtcs[i];
  1625. struct drm_fb_offset *offset = &offsets[i];
  1626. modeset = &fb_crtc->mode_set;
  1627. if (mode && fb_crtc) {
  1628. DRM_DEBUG_KMS("desired mode %s set on crtc %d (%d,%d)\n",
  1629. mode->name, fb_crtc->mode_set.crtc->base.id, offset->x, offset->y);
  1630. fb_crtc->desired_mode = mode;
  1631. fb_crtc->x = offset->x;
  1632. fb_crtc->y = offset->y;
  1633. if (modeset->mode)
  1634. drm_mode_destroy(dev, modeset->mode);
  1635. modeset->mode = drm_mode_duplicate(dev,
  1636. fb_crtc->desired_mode);
  1637. modeset->connectors[modeset->num_connectors++] = fb_helper->connector_info[i]->connector;
  1638. modeset->fb = fb_helper->fb;
  1639. modeset->x = offset->x;
  1640. modeset->y = offset->y;
  1641. }
  1642. }
  1643. /* Clear out any old modes if there are no more connected outputs. */
  1644. for (i = 0; i < fb_helper->crtc_count; i++) {
  1645. modeset = &fb_helper->crtc_info[i].mode_set;
  1646. if (modeset->num_connectors == 0) {
  1647. BUG_ON(modeset->fb);
  1648. if (modeset->mode)
  1649. drm_mode_destroy(dev, modeset->mode);
  1650. modeset->mode = NULL;
  1651. }
  1652. }
  1653. out:
  1654. kfree(crtcs);
  1655. kfree(modes);
  1656. kfree(offsets);
  1657. kfree(enabled);
  1658. }
  1659. /**
  1660. * drm_fb_helper_initial_config - setup a sane initial connector configuration
  1661. * @fb_helper: fb_helper device struct
  1662. * @bpp_sel: bpp value to use for the framebuffer configuration
  1663. *
  1664. * Scans the CRTCs and connectors and tries to put together an initial setup.
  1665. * At the moment, this is a cloned configuration across all heads with
  1666. * a new framebuffer object as the backing store.
  1667. *
  1668. * Note that this also registers the fbdev and so allows userspace to call into
  1669. * the driver through the fbdev interfaces.
  1670. *
  1671. * This function will call down into the ->fb_probe callback to let
  1672. * the driver allocate and initialize the fbdev info structure and the drm
  1673. * framebuffer used to back the fbdev. drm_fb_helper_fill_var() and
  1674. * drm_fb_helper_fill_fix() are provided as helpers to setup simple default
  1675. * values for the fbdev info structure.
  1676. *
  1677. * RETURNS:
  1678. * Zero if everything went ok, nonzero otherwise.
  1679. */
  1680. int drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel)
  1681. {
  1682. struct drm_device *dev = fb_helper->dev;
  1683. int count = 0;
  1684. mutex_lock(&dev->mode_config.mutex);
  1685. count = drm_fb_helper_probe_connector_modes(fb_helper,
  1686. dev->mode_config.max_width,
  1687. dev->mode_config.max_height);
  1688. mutex_unlock(&dev->mode_config.mutex);
  1689. /*
  1690. * we shouldn't end up with no modes here.
  1691. */
  1692. if (count == 0)
  1693. dev_info(fb_helper->dev->dev, "No connectors reported connected with modes\n");
  1694. drm_setup_crtcs(fb_helper);
  1695. return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
  1696. }
  1697. EXPORT_SYMBOL(drm_fb_helper_initial_config);
  1698. /**
  1699. * drm_fb_helper_hotplug_event - respond to a hotplug notification by
  1700. * probing all the outputs attached to the fb
  1701. * @fb_helper: the drm_fb_helper
  1702. *
  1703. * Scan the connectors attached to the fb_helper and try to put together a
  1704. * setup after *notification of a change in output configuration.
  1705. *
  1706. * Called at runtime, takes the mode config locks to be able to check/change the
  1707. * modeset configuration. Must be run from process context (which usually means
  1708. * either the output polling work or a work item launched from the driver's
  1709. * hotplug interrupt).
  1710. *
  1711. * Note that drivers may call this even before calling
  1712. * drm_fb_helper_initial_config but only aftert drm_fb_helper_init. This allows
  1713. * for a race-free fbcon setup and will make sure that the fbdev emulation will
  1714. * not miss any hotplug events.
  1715. *
  1716. * RETURNS:
  1717. * 0 on success and a non-zero error code otherwise.
  1718. */
  1719. int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper)
  1720. {
  1721. struct drm_device *dev = fb_helper->dev;
  1722. u32 max_width, max_height;
  1723. mutex_lock(&fb_helper->dev->mode_config.mutex);
  1724. if (!fb_helper->fb || !drm_fb_helper_is_bound(fb_helper)) {
  1725. fb_helper->delayed_hotplug = true;
  1726. mutex_unlock(&fb_helper->dev->mode_config.mutex);
  1727. return 0;
  1728. }
  1729. DRM_DEBUG_KMS("\n");
  1730. max_width = fb_helper->fb->width;
  1731. max_height = fb_helper->fb->height;
  1732. drm_fb_helper_probe_connector_modes(fb_helper, max_width, max_height);
  1733. mutex_unlock(&fb_helper->dev->mode_config.mutex);
  1734. drm_modeset_lock_all(dev);
  1735. drm_setup_crtcs(fb_helper);
  1736. drm_modeset_unlock_all(dev);
  1737. drm_fb_helper_set_par(fb_helper->fbdev);
  1738. return 0;
  1739. }
  1740. EXPORT_SYMBOL(drm_fb_helper_hotplug_event);
  1741. /* The Kconfig DRM_KMS_HELPER selects FRAMEBUFFER_CONSOLE (if !EXPERT)
  1742. * but the module doesn't depend on any fb console symbols. At least
  1743. * attempt to load fbcon to avoid leaving the system without a usable console.
  1744. */
  1745. #if defined(CONFIG_FRAMEBUFFER_CONSOLE_MODULE) && !defined(CONFIG_EXPERT)
  1746. static int __init drm_fb_helper_modinit(void)
  1747. {
  1748. const char *name = "fbcon";
  1749. struct module *fbcon;
  1750. mutex_lock(&module_mutex);
  1751. fbcon = find_module(name);
  1752. mutex_unlock(&module_mutex);
  1753. if (!fbcon)
  1754. request_module_nowait(name);
  1755. return 0;
  1756. }
  1757. module_init(drm_fb_helper_modinit);
  1758. #endif