drm_fb_helper.c 56 KB

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