drm_fb_helper.c 56 KB

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