drm_fb_helper.c 91 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263
  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/console.h>
  32. #include <linux/dma-buf.h>
  33. #include <linux/kernel.h>
  34. #include <linux/sysrq.h>
  35. #include <linux/slab.h>
  36. #include <linux/module.h>
  37. #include <drm/drmP.h>
  38. #include <drm/drm_crtc.h>
  39. #include <drm/drm_fb_helper.h>
  40. #include <drm/drm_crtc_helper.h>
  41. #include <drm/drm_atomic.h>
  42. #include <drm/drm_atomic_helper.h>
  43. #include "drm_crtc_internal.h"
  44. #include "drm_crtc_helper_internal.h"
  45. static bool drm_fbdev_emulation = true;
  46. module_param_named(fbdev_emulation, drm_fbdev_emulation, bool, 0600);
  47. MODULE_PARM_DESC(fbdev_emulation,
  48. "Enable legacy fbdev emulation [default=true]");
  49. static int drm_fbdev_overalloc = CONFIG_DRM_FBDEV_OVERALLOC;
  50. module_param(drm_fbdev_overalloc, int, 0444);
  51. MODULE_PARM_DESC(drm_fbdev_overalloc,
  52. "Overallocation of the fbdev buffer (%) [default="
  53. __MODULE_STRING(CONFIG_DRM_FBDEV_OVERALLOC) "]");
  54. /*
  55. * In order to keep user-space compatibility, we want in certain use-cases
  56. * to keep leaking the fbdev physical address to the user-space program
  57. * handling the fbdev buffer.
  58. * This is a bad habit essentially kept into closed source opengl driver
  59. * that should really be moved into open-source upstream projects instead
  60. * of using legacy physical addresses in user space to communicate with
  61. * other out-of-tree kernel modules.
  62. *
  63. * This module_param *should* be removed as soon as possible and be
  64. * considered as a broken and legacy behaviour from a modern fbdev device.
  65. */
  66. #if IS_ENABLED(CONFIG_DRM_FBDEV_LEAK_PHYS_SMEM)
  67. static bool drm_leak_fbdev_smem = false;
  68. module_param_unsafe(drm_leak_fbdev_smem, bool, 0600);
  69. MODULE_PARM_DESC(drm_leak_fbdev_smem,
  70. "Allow unsafe leaking fbdev physical smem address [default=false]");
  71. #endif
  72. static LIST_HEAD(kernel_fb_helper_list);
  73. static DEFINE_MUTEX(kernel_fb_helper_lock);
  74. /**
  75. * DOC: fbdev helpers
  76. *
  77. * The fb helper functions are useful to provide an fbdev on top of a drm kernel
  78. * mode setting driver. They can be used mostly independently from the crtc
  79. * helper functions used by many drivers to implement the kernel mode setting
  80. * interfaces.
  81. *
  82. * Drivers that support a dumb buffer with a virtual address and mmap support,
  83. * should try out the generic fbdev emulation using drm_fbdev_generic_setup().
  84. *
  85. * Setup fbdev emulation by calling drm_fb_helper_fbdev_setup() and tear it
  86. * down by calling drm_fb_helper_fbdev_teardown().
  87. *
  88. * Drivers that need to handle connector hotplugging (e.g. dp mst) can't use
  89. * the setup helper and will need to do the whole four-step setup process with
  90. * drm_fb_helper_prepare(), drm_fb_helper_init(),
  91. * drm_fb_helper_single_add_all_connectors(), enable hotplugging and
  92. * drm_fb_helper_initial_config() to avoid a possible race window.
  93. *
  94. * At runtime drivers should restore the fbdev console by using
  95. * drm_fb_helper_lastclose() as their &drm_driver.lastclose callback.
  96. * They should also notify the fb helper code from updates to the output
  97. * configuration by using drm_fb_helper_output_poll_changed() as their
  98. * &drm_mode_config_funcs.output_poll_changed callback.
  99. *
  100. * For suspend/resume consider using drm_mode_config_helper_suspend() and
  101. * drm_mode_config_helper_resume() which takes care of fbdev as well.
  102. *
  103. * All other functions exported by the fb helper library can be used to
  104. * implement the fbdev driver interface by the driver.
  105. *
  106. * It is possible, though perhaps somewhat tricky, to implement race-free
  107. * hotplug detection using the fbdev helpers. The drm_fb_helper_prepare()
  108. * helper must be called first to initialize the minimum required to make
  109. * hotplug detection work. Drivers also need to make sure to properly set up
  110. * the &drm_mode_config.funcs member. After calling drm_kms_helper_poll_init()
  111. * it is safe to enable interrupts and start processing hotplug events. At the
  112. * same time, drivers should initialize all modeset objects such as CRTCs,
  113. * encoders and connectors. To finish up the fbdev helper initialization, the
  114. * drm_fb_helper_init() function is called. To probe for all attached displays
  115. * and set up an initial configuration using the detected hardware, drivers
  116. * should call drm_fb_helper_single_add_all_connectors() followed by
  117. * drm_fb_helper_initial_config().
  118. *
  119. * If &drm_framebuffer_funcs.dirty is set, the
  120. * drm_fb_helper_{cfb,sys}_{write,fillrect,copyarea,imageblit} functions will
  121. * accumulate changes and schedule &drm_fb_helper.dirty_work to run right
  122. * away. This worker then calls the dirty() function ensuring that it will
  123. * always run in process context since the fb_*() function could be running in
  124. * atomic context. If drm_fb_helper_deferred_io() is used as the deferred_io
  125. * callback it will also schedule dirty_work with the damage collected from the
  126. * mmap page writes. Drivers can use drm_fb_helper_defio_init() to setup
  127. * deferred I/O (coupled with drm_fb_helper_fbdev_teardown()).
  128. */
  129. #define drm_fb_helper_for_each_connector(fbh, i__) \
  130. for (({ lockdep_assert_held(&(fbh)->lock); }), \
  131. i__ = 0; i__ < (fbh)->connector_count; i__++)
  132. static int __drm_fb_helper_add_one_connector(struct drm_fb_helper *fb_helper,
  133. struct drm_connector *connector)
  134. {
  135. struct drm_fb_helper_connector *fb_conn;
  136. struct drm_fb_helper_connector **temp;
  137. unsigned int count;
  138. if (!drm_fbdev_emulation)
  139. return 0;
  140. lockdep_assert_held(&fb_helper->lock);
  141. count = fb_helper->connector_count + 1;
  142. if (count > fb_helper->connector_info_alloc_count) {
  143. size_t size = count * sizeof(fb_conn);
  144. temp = krealloc(fb_helper->connector_info, size, GFP_KERNEL);
  145. if (!temp)
  146. return -ENOMEM;
  147. fb_helper->connector_info_alloc_count = count;
  148. fb_helper->connector_info = temp;
  149. }
  150. fb_conn = kzalloc(sizeof(*fb_conn), GFP_KERNEL);
  151. if (!fb_conn)
  152. return -ENOMEM;
  153. drm_connector_get(connector);
  154. fb_conn->connector = connector;
  155. fb_helper->connector_info[fb_helper->connector_count++] = fb_conn;
  156. return 0;
  157. }
  158. int drm_fb_helper_add_one_connector(struct drm_fb_helper *fb_helper,
  159. struct drm_connector *connector)
  160. {
  161. int err;
  162. if (!fb_helper)
  163. return 0;
  164. mutex_lock(&fb_helper->lock);
  165. err = __drm_fb_helper_add_one_connector(fb_helper, connector);
  166. mutex_unlock(&fb_helper->lock);
  167. return err;
  168. }
  169. EXPORT_SYMBOL(drm_fb_helper_add_one_connector);
  170. /**
  171. * drm_fb_helper_single_add_all_connectors() - add all connectors to fbdev
  172. * emulation helper
  173. * @fb_helper: fbdev initialized with drm_fb_helper_init, can be NULL
  174. *
  175. * This functions adds all the available connectors for use with the given
  176. * fb_helper. This is a separate step to allow drivers to freely assign
  177. * connectors to the fbdev, e.g. if some are reserved for special purposes or
  178. * not adequate to be used for the fbcon.
  179. *
  180. * This function is protected against concurrent connector hotadds/removals
  181. * using drm_fb_helper_add_one_connector() and
  182. * drm_fb_helper_remove_one_connector().
  183. */
  184. int drm_fb_helper_single_add_all_connectors(struct drm_fb_helper *fb_helper)
  185. {
  186. struct drm_device *dev;
  187. struct drm_connector *connector;
  188. struct drm_connector_list_iter conn_iter;
  189. int i, ret = 0;
  190. if (!drm_fbdev_emulation || !fb_helper)
  191. return 0;
  192. dev = fb_helper->dev;
  193. mutex_lock(&fb_helper->lock);
  194. drm_connector_list_iter_begin(dev, &conn_iter);
  195. drm_for_each_connector_iter(connector, &conn_iter) {
  196. if (connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK)
  197. continue;
  198. ret = __drm_fb_helper_add_one_connector(fb_helper, connector);
  199. if (ret)
  200. goto fail;
  201. }
  202. goto out;
  203. fail:
  204. drm_fb_helper_for_each_connector(fb_helper, i) {
  205. struct drm_fb_helper_connector *fb_helper_connector =
  206. fb_helper->connector_info[i];
  207. drm_connector_put(fb_helper_connector->connector);
  208. kfree(fb_helper_connector);
  209. fb_helper->connector_info[i] = NULL;
  210. }
  211. fb_helper->connector_count = 0;
  212. out:
  213. drm_connector_list_iter_end(&conn_iter);
  214. mutex_unlock(&fb_helper->lock);
  215. return ret;
  216. }
  217. EXPORT_SYMBOL(drm_fb_helper_single_add_all_connectors);
  218. static int __drm_fb_helper_remove_one_connector(struct drm_fb_helper *fb_helper,
  219. struct drm_connector *connector)
  220. {
  221. struct drm_fb_helper_connector *fb_helper_connector;
  222. int i, j;
  223. if (!drm_fbdev_emulation)
  224. return 0;
  225. lockdep_assert_held(&fb_helper->lock);
  226. drm_fb_helper_for_each_connector(fb_helper, i) {
  227. if (fb_helper->connector_info[i]->connector == connector)
  228. break;
  229. }
  230. if (i == fb_helper->connector_count)
  231. return -EINVAL;
  232. fb_helper_connector = fb_helper->connector_info[i];
  233. drm_connector_put(fb_helper_connector->connector);
  234. for (j = i + 1; j < fb_helper->connector_count; j++)
  235. fb_helper->connector_info[j - 1] = fb_helper->connector_info[j];
  236. fb_helper->connector_count--;
  237. kfree(fb_helper_connector);
  238. return 0;
  239. }
  240. int drm_fb_helper_remove_one_connector(struct drm_fb_helper *fb_helper,
  241. struct drm_connector *connector)
  242. {
  243. int err;
  244. if (!fb_helper)
  245. return 0;
  246. mutex_lock(&fb_helper->lock);
  247. err = __drm_fb_helper_remove_one_connector(fb_helper, connector);
  248. mutex_unlock(&fb_helper->lock);
  249. return err;
  250. }
  251. EXPORT_SYMBOL(drm_fb_helper_remove_one_connector);
  252. static void drm_fb_helper_restore_lut_atomic(struct drm_crtc *crtc)
  253. {
  254. uint16_t *r_base, *g_base, *b_base;
  255. if (crtc->funcs->gamma_set == NULL)
  256. return;
  257. r_base = crtc->gamma_store;
  258. g_base = r_base + crtc->gamma_size;
  259. b_base = g_base + crtc->gamma_size;
  260. crtc->funcs->gamma_set(crtc, r_base, g_base, b_base,
  261. crtc->gamma_size, NULL);
  262. }
  263. /**
  264. * drm_fb_helper_debug_enter - implementation for &fb_ops.fb_debug_enter
  265. * @info: fbdev registered by the helper
  266. */
  267. int drm_fb_helper_debug_enter(struct fb_info *info)
  268. {
  269. struct drm_fb_helper *helper = info->par;
  270. const struct drm_crtc_helper_funcs *funcs;
  271. int i;
  272. list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
  273. for (i = 0; i < helper->crtc_count; i++) {
  274. struct drm_mode_set *mode_set =
  275. &helper->crtc_info[i].mode_set;
  276. if (!mode_set->crtc->enabled)
  277. continue;
  278. funcs = mode_set->crtc->helper_private;
  279. if (funcs->mode_set_base_atomic == NULL)
  280. continue;
  281. if (drm_drv_uses_atomic_modeset(mode_set->crtc->dev))
  282. continue;
  283. funcs->mode_set_base_atomic(mode_set->crtc,
  284. mode_set->fb,
  285. mode_set->x,
  286. mode_set->y,
  287. ENTER_ATOMIC_MODE_SET);
  288. }
  289. }
  290. return 0;
  291. }
  292. EXPORT_SYMBOL(drm_fb_helper_debug_enter);
  293. /**
  294. * drm_fb_helper_debug_leave - implementation for &fb_ops.fb_debug_leave
  295. * @info: fbdev registered by the helper
  296. */
  297. int drm_fb_helper_debug_leave(struct fb_info *info)
  298. {
  299. struct drm_fb_helper *helper = info->par;
  300. struct drm_crtc *crtc;
  301. const struct drm_crtc_helper_funcs *funcs;
  302. struct drm_framebuffer *fb;
  303. int i;
  304. for (i = 0; i < helper->crtc_count; i++) {
  305. struct drm_mode_set *mode_set = &helper->crtc_info[i].mode_set;
  306. crtc = mode_set->crtc;
  307. if (drm_drv_uses_atomic_modeset(crtc->dev))
  308. continue;
  309. funcs = crtc->helper_private;
  310. fb = crtc->primary->fb;
  311. if (!crtc->enabled)
  312. continue;
  313. if (!fb) {
  314. DRM_ERROR("no fb to restore??\n");
  315. continue;
  316. }
  317. if (funcs->mode_set_base_atomic == NULL)
  318. continue;
  319. drm_fb_helper_restore_lut_atomic(mode_set->crtc);
  320. funcs->mode_set_base_atomic(mode_set->crtc, fb, crtc->x,
  321. crtc->y, LEAVE_ATOMIC_MODE_SET);
  322. }
  323. return 0;
  324. }
  325. EXPORT_SYMBOL(drm_fb_helper_debug_leave);
  326. static int restore_fbdev_mode_atomic(struct drm_fb_helper *fb_helper, bool active)
  327. {
  328. struct drm_device *dev = fb_helper->dev;
  329. struct drm_plane_state *plane_state;
  330. struct drm_plane *plane;
  331. struct drm_atomic_state *state;
  332. int i, ret;
  333. struct drm_modeset_acquire_ctx ctx;
  334. drm_modeset_acquire_init(&ctx, 0);
  335. state = drm_atomic_state_alloc(dev);
  336. if (!state) {
  337. ret = -ENOMEM;
  338. goto out_ctx;
  339. }
  340. state->acquire_ctx = &ctx;
  341. retry:
  342. drm_for_each_plane(plane, dev) {
  343. plane_state = drm_atomic_get_plane_state(state, plane);
  344. if (IS_ERR(plane_state)) {
  345. ret = PTR_ERR(plane_state);
  346. goto out_state;
  347. }
  348. plane_state->rotation = DRM_MODE_ROTATE_0;
  349. /* disable non-primary: */
  350. if (plane->type == DRM_PLANE_TYPE_PRIMARY)
  351. continue;
  352. ret = __drm_atomic_helper_disable_plane(plane, plane_state);
  353. if (ret != 0)
  354. goto out_state;
  355. }
  356. for (i = 0; i < fb_helper->crtc_count; i++) {
  357. struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set;
  358. struct drm_plane *primary = mode_set->crtc->primary;
  359. /* Cannot fail as we've already gotten the plane state above */
  360. plane_state = drm_atomic_get_new_plane_state(state, primary);
  361. plane_state->rotation = fb_helper->crtc_info[i].rotation;
  362. ret = __drm_atomic_helper_set_config(mode_set, state);
  363. if (ret != 0)
  364. goto out_state;
  365. /*
  366. * __drm_atomic_helper_set_config() sets active when a
  367. * mode is set, unconditionally clear it if we force DPMS off
  368. */
  369. if (!active) {
  370. struct drm_crtc *crtc = mode_set->crtc;
  371. struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
  372. crtc_state->active = false;
  373. }
  374. }
  375. ret = drm_atomic_commit(state);
  376. out_state:
  377. if (ret == -EDEADLK)
  378. goto backoff;
  379. drm_atomic_state_put(state);
  380. out_ctx:
  381. drm_modeset_drop_locks(&ctx);
  382. drm_modeset_acquire_fini(&ctx);
  383. return ret;
  384. backoff:
  385. drm_atomic_state_clear(state);
  386. drm_modeset_backoff(&ctx);
  387. goto retry;
  388. }
  389. static int restore_fbdev_mode_legacy(struct drm_fb_helper *fb_helper)
  390. {
  391. struct drm_device *dev = fb_helper->dev;
  392. struct drm_plane *plane;
  393. int i, ret = 0;
  394. drm_modeset_lock_all(fb_helper->dev);
  395. drm_for_each_plane(plane, dev) {
  396. if (plane->type != DRM_PLANE_TYPE_PRIMARY)
  397. drm_plane_force_disable(plane);
  398. if (plane->rotation_property)
  399. drm_mode_plane_set_obj_prop(plane,
  400. plane->rotation_property,
  401. DRM_MODE_ROTATE_0);
  402. }
  403. for (i = 0; i < fb_helper->crtc_count; i++) {
  404. struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set;
  405. struct drm_crtc *crtc = mode_set->crtc;
  406. if (crtc->funcs->cursor_set2) {
  407. ret = crtc->funcs->cursor_set2(crtc, NULL, 0, 0, 0, 0, 0);
  408. if (ret)
  409. goto out;
  410. } else if (crtc->funcs->cursor_set) {
  411. ret = crtc->funcs->cursor_set(crtc, NULL, 0, 0, 0);
  412. if (ret)
  413. goto out;
  414. }
  415. ret = drm_mode_set_config_internal(mode_set);
  416. if (ret)
  417. goto out;
  418. }
  419. out:
  420. drm_modeset_unlock_all(fb_helper->dev);
  421. return ret;
  422. }
  423. static int restore_fbdev_mode(struct drm_fb_helper *fb_helper)
  424. {
  425. struct drm_device *dev = fb_helper->dev;
  426. if (drm_drv_uses_atomic_modeset(dev))
  427. return restore_fbdev_mode_atomic(fb_helper, true);
  428. else
  429. return restore_fbdev_mode_legacy(fb_helper);
  430. }
  431. /**
  432. * drm_fb_helper_restore_fbdev_mode_unlocked - restore fbdev configuration
  433. * @fb_helper: driver-allocated fbdev helper, can be NULL
  434. *
  435. * This should be called from driver's drm &drm_driver.lastclose callback
  436. * when implementing an fbcon on top of kms using this helper. This ensures that
  437. * the user isn't greeted with a black screen when e.g. X dies.
  438. *
  439. * RETURNS:
  440. * Zero if everything went ok, negative error code otherwise.
  441. */
  442. int drm_fb_helper_restore_fbdev_mode_unlocked(struct drm_fb_helper *fb_helper)
  443. {
  444. bool do_delayed;
  445. int ret;
  446. if (!drm_fbdev_emulation || !fb_helper)
  447. return -ENODEV;
  448. if (READ_ONCE(fb_helper->deferred_setup))
  449. return 0;
  450. mutex_lock(&fb_helper->lock);
  451. ret = restore_fbdev_mode(fb_helper);
  452. do_delayed = fb_helper->delayed_hotplug;
  453. if (do_delayed)
  454. fb_helper->delayed_hotplug = false;
  455. mutex_unlock(&fb_helper->lock);
  456. if (do_delayed)
  457. drm_fb_helper_hotplug_event(fb_helper);
  458. return ret;
  459. }
  460. EXPORT_SYMBOL(drm_fb_helper_restore_fbdev_mode_unlocked);
  461. static bool drm_fb_helper_is_bound(struct drm_fb_helper *fb_helper)
  462. {
  463. struct drm_device *dev = fb_helper->dev;
  464. struct drm_crtc *crtc;
  465. int bound = 0, crtcs_bound = 0;
  466. /*
  467. * Sometimes user space wants everything disabled, so don't steal the
  468. * display if there's a master.
  469. */
  470. if (READ_ONCE(dev->master))
  471. return false;
  472. drm_for_each_crtc(crtc, dev) {
  473. drm_modeset_lock(&crtc->mutex, NULL);
  474. if (crtc->primary->fb)
  475. crtcs_bound++;
  476. if (crtc->primary->fb == fb_helper->fb)
  477. bound++;
  478. drm_modeset_unlock(&crtc->mutex);
  479. }
  480. if (bound < crtcs_bound)
  481. return false;
  482. return true;
  483. }
  484. #ifdef CONFIG_MAGIC_SYSRQ
  485. /*
  486. * restore fbcon display for all kms driver's using this helper, used for sysrq
  487. * and panic handling.
  488. */
  489. static bool drm_fb_helper_force_kernel_mode(void)
  490. {
  491. bool ret, error = false;
  492. struct drm_fb_helper *helper;
  493. if (list_empty(&kernel_fb_helper_list))
  494. return false;
  495. list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
  496. struct drm_device *dev = helper->dev;
  497. if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
  498. continue;
  499. mutex_lock(&helper->lock);
  500. ret = restore_fbdev_mode(helper);
  501. if (ret)
  502. error = true;
  503. mutex_unlock(&helper->lock);
  504. }
  505. return error;
  506. }
  507. static void drm_fb_helper_restore_work_fn(struct work_struct *ignored)
  508. {
  509. bool ret;
  510. ret = drm_fb_helper_force_kernel_mode();
  511. if (ret == true)
  512. DRM_ERROR("Failed to restore crtc configuration\n");
  513. }
  514. static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn);
  515. static void drm_fb_helper_sysrq(int dummy1)
  516. {
  517. schedule_work(&drm_fb_helper_restore_work);
  518. }
  519. static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = {
  520. .handler = drm_fb_helper_sysrq,
  521. .help_msg = "force-fb(V)",
  522. .action_msg = "Restore framebuffer console",
  523. };
  524. #else
  525. static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { };
  526. #endif
  527. static void dpms_legacy(struct drm_fb_helper *fb_helper, int dpms_mode)
  528. {
  529. struct drm_device *dev = fb_helper->dev;
  530. struct drm_crtc *crtc;
  531. struct drm_connector *connector;
  532. int i, j;
  533. drm_modeset_lock_all(dev);
  534. for (i = 0; i < fb_helper->crtc_count; i++) {
  535. crtc = fb_helper->crtc_info[i].mode_set.crtc;
  536. if (!crtc->enabled)
  537. continue;
  538. /* Walk the connectors & encoders on this fb turning them on/off */
  539. drm_fb_helper_for_each_connector(fb_helper, j) {
  540. connector = fb_helper->connector_info[j]->connector;
  541. connector->funcs->dpms(connector, dpms_mode);
  542. drm_object_property_set_value(&connector->base,
  543. dev->mode_config.dpms_property, dpms_mode);
  544. }
  545. }
  546. drm_modeset_unlock_all(dev);
  547. }
  548. static void drm_fb_helper_dpms(struct fb_info *info, int dpms_mode)
  549. {
  550. struct drm_fb_helper *fb_helper = info->par;
  551. /*
  552. * For each CRTC in this fb, turn the connectors on/off.
  553. */
  554. mutex_lock(&fb_helper->lock);
  555. if (!drm_fb_helper_is_bound(fb_helper)) {
  556. mutex_unlock(&fb_helper->lock);
  557. return;
  558. }
  559. if (drm_drv_uses_atomic_modeset(fb_helper->dev))
  560. restore_fbdev_mode_atomic(fb_helper, dpms_mode == DRM_MODE_DPMS_ON);
  561. else
  562. dpms_legacy(fb_helper, dpms_mode);
  563. mutex_unlock(&fb_helper->lock);
  564. }
  565. /**
  566. * drm_fb_helper_blank - implementation for &fb_ops.fb_blank
  567. * @blank: desired blanking state
  568. * @info: fbdev registered by the helper
  569. */
  570. int drm_fb_helper_blank(int blank, struct fb_info *info)
  571. {
  572. if (oops_in_progress)
  573. return -EBUSY;
  574. switch (blank) {
  575. /* Display: On; HSync: On, VSync: On */
  576. case FB_BLANK_UNBLANK:
  577. drm_fb_helper_dpms(info, DRM_MODE_DPMS_ON);
  578. break;
  579. /* Display: Off; HSync: On, VSync: On */
  580. case FB_BLANK_NORMAL:
  581. drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
  582. break;
  583. /* Display: Off; HSync: Off, VSync: On */
  584. case FB_BLANK_HSYNC_SUSPEND:
  585. drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
  586. break;
  587. /* Display: Off; HSync: On, VSync: Off */
  588. case FB_BLANK_VSYNC_SUSPEND:
  589. drm_fb_helper_dpms(info, DRM_MODE_DPMS_SUSPEND);
  590. break;
  591. /* Display: Off; HSync: Off, VSync: Off */
  592. case FB_BLANK_POWERDOWN:
  593. drm_fb_helper_dpms(info, DRM_MODE_DPMS_OFF);
  594. break;
  595. }
  596. return 0;
  597. }
  598. EXPORT_SYMBOL(drm_fb_helper_blank);
  599. static void drm_fb_helper_modeset_release(struct drm_fb_helper *helper,
  600. struct drm_mode_set *modeset)
  601. {
  602. int i;
  603. for (i = 0; i < modeset->num_connectors; i++) {
  604. drm_connector_put(modeset->connectors[i]);
  605. modeset->connectors[i] = NULL;
  606. }
  607. modeset->num_connectors = 0;
  608. drm_mode_destroy(helper->dev, modeset->mode);
  609. modeset->mode = NULL;
  610. /* FIXME should hold a ref? */
  611. modeset->fb = NULL;
  612. }
  613. static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper)
  614. {
  615. int i;
  616. for (i = 0; i < helper->connector_count; i++) {
  617. drm_connector_put(helper->connector_info[i]->connector);
  618. kfree(helper->connector_info[i]);
  619. }
  620. kfree(helper->connector_info);
  621. for (i = 0; i < helper->crtc_count; i++) {
  622. struct drm_mode_set *modeset = &helper->crtc_info[i].mode_set;
  623. drm_fb_helper_modeset_release(helper, modeset);
  624. kfree(modeset->connectors);
  625. }
  626. kfree(helper->crtc_info);
  627. }
  628. static void drm_fb_helper_resume_worker(struct work_struct *work)
  629. {
  630. struct drm_fb_helper *helper = container_of(work, struct drm_fb_helper,
  631. resume_work);
  632. console_lock();
  633. fb_set_suspend(helper->fbdev, 0);
  634. console_unlock();
  635. }
  636. static void drm_fb_helper_dirty_blit_real(struct drm_fb_helper *fb_helper,
  637. struct drm_clip_rect *clip)
  638. {
  639. struct drm_framebuffer *fb = fb_helper->fb;
  640. unsigned int cpp = drm_format_plane_cpp(fb->format->format, 0);
  641. size_t offset = clip->y1 * fb->pitches[0] + clip->x1 * cpp;
  642. void *src = fb_helper->fbdev->screen_buffer + offset;
  643. void *dst = fb_helper->buffer->vaddr + offset;
  644. size_t len = (clip->x2 - clip->x1) * cpp;
  645. unsigned int y;
  646. for (y = clip->y1; y < clip->y2; y++) {
  647. memcpy(dst, src, len);
  648. src += fb->pitches[0];
  649. dst += fb->pitches[0];
  650. }
  651. }
  652. static void drm_fb_helper_dirty_work(struct work_struct *work)
  653. {
  654. struct drm_fb_helper *helper = container_of(work, struct drm_fb_helper,
  655. dirty_work);
  656. struct drm_clip_rect *clip = &helper->dirty_clip;
  657. struct drm_clip_rect clip_copy;
  658. unsigned long flags;
  659. spin_lock_irqsave(&helper->dirty_lock, flags);
  660. clip_copy = *clip;
  661. clip->x1 = clip->y1 = ~0;
  662. clip->x2 = clip->y2 = 0;
  663. spin_unlock_irqrestore(&helper->dirty_lock, flags);
  664. /* call dirty callback only when it has been really touched */
  665. if (clip_copy.x1 < clip_copy.x2 && clip_copy.y1 < clip_copy.y2) {
  666. /* Generic fbdev uses a shadow buffer */
  667. if (helper->buffer)
  668. drm_fb_helper_dirty_blit_real(helper, &clip_copy);
  669. helper->fb->funcs->dirty(helper->fb, NULL, 0, 0, &clip_copy, 1);
  670. }
  671. }
  672. /**
  673. * drm_fb_helper_prepare - setup a drm_fb_helper structure
  674. * @dev: DRM device
  675. * @helper: driver-allocated fbdev helper structure to set up
  676. * @funcs: pointer to structure of functions associate with this helper
  677. *
  678. * Sets up the bare minimum to make the framebuffer helper usable. This is
  679. * useful to implement race-free initialization of the polling helpers.
  680. */
  681. void drm_fb_helper_prepare(struct drm_device *dev, struct drm_fb_helper *helper,
  682. const struct drm_fb_helper_funcs *funcs)
  683. {
  684. INIT_LIST_HEAD(&helper->kernel_fb_list);
  685. spin_lock_init(&helper->dirty_lock);
  686. INIT_WORK(&helper->resume_work, drm_fb_helper_resume_worker);
  687. INIT_WORK(&helper->dirty_work, drm_fb_helper_dirty_work);
  688. helper->dirty_clip.x1 = helper->dirty_clip.y1 = ~0;
  689. mutex_init(&helper->lock);
  690. helper->funcs = funcs;
  691. helper->dev = dev;
  692. }
  693. EXPORT_SYMBOL(drm_fb_helper_prepare);
  694. /**
  695. * drm_fb_helper_init - initialize a &struct drm_fb_helper
  696. * @dev: drm device
  697. * @fb_helper: driver-allocated fbdev helper structure to initialize
  698. * @max_conn_count: max connector count
  699. *
  700. * This allocates the structures for the fbdev helper with the given limits.
  701. * Note that this won't yet touch the hardware (through the driver interfaces)
  702. * nor register the fbdev. This is only done in drm_fb_helper_initial_config()
  703. * to allow driver writes more control over the exact init sequence.
  704. *
  705. * Drivers must call drm_fb_helper_prepare() before calling this function.
  706. *
  707. * RETURNS:
  708. * Zero if everything went ok, nonzero otherwise.
  709. */
  710. int drm_fb_helper_init(struct drm_device *dev,
  711. struct drm_fb_helper *fb_helper,
  712. int max_conn_count)
  713. {
  714. struct drm_crtc *crtc;
  715. struct drm_mode_config *config = &dev->mode_config;
  716. int i;
  717. if (!drm_fbdev_emulation) {
  718. dev->fb_helper = fb_helper;
  719. return 0;
  720. }
  721. if (!max_conn_count)
  722. return -EINVAL;
  723. fb_helper->crtc_info = kcalloc(config->num_crtc, sizeof(struct drm_fb_helper_crtc), GFP_KERNEL);
  724. if (!fb_helper->crtc_info)
  725. return -ENOMEM;
  726. fb_helper->crtc_count = config->num_crtc;
  727. fb_helper->connector_info = kcalloc(dev->mode_config.num_connector, sizeof(struct drm_fb_helper_connector *), GFP_KERNEL);
  728. if (!fb_helper->connector_info) {
  729. kfree(fb_helper->crtc_info);
  730. return -ENOMEM;
  731. }
  732. fb_helper->connector_info_alloc_count = dev->mode_config.num_connector;
  733. fb_helper->connector_count = 0;
  734. for (i = 0; i < fb_helper->crtc_count; i++) {
  735. fb_helper->crtc_info[i].mode_set.connectors =
  736. kcalloc(max_conn_count,
  737. sizeof(struct drm_connector *),
  738. GFP_KERNEL);
  739. if (!fb_helper->crtc_info[i].mode_set.connectors)
  740. goto out_free;
  741. fb_helper->crtc_info[i].mode_set.num_connectors = 0;
  742. fb_helper->crtc_info[i].rotation = DRM_MODE_ROTATE_0;
  743. }
  744. i = 0;
  745. drm_for_each_crtc(crtc, dev) {
  746. fb_helper->crtc_info[i].mode_set.crtc = crtc;
  747. i++;
  748. }
  749. dev->fb_helper = fb_helper;
  750. return 0;
  751. out_free:
  752. drm_fb_helper_crtc_free(fb_helper);
  753. return -ENOMEM;
  754. }
  755. EXPORT_SYMBOL(drm_fb_helper_init);
  756. /**
  757. * drm_fb_helper_alloc_fbi - allocate fb_info and some of its members
  758. * @fb_helper: driver-allocated fbdev helper
  759. *
  760. * A helper to alloc fb_info and the members cmap and apertures. Called
  761. * by the driver within the fb_probe fb_helper callback function. Drivers do not
  762. * need to release the allocated fb_info structure themselves, this is
  763. * automatically done when calling drm_fb_helper_fini().
  764. *
  765. * RETURNS:
  766. * fb_info pointer if things went okay, pointer containing error code
  767. * otherwise
  768. */
  769. struct fb_info *drm_fb_helper_alloc_fbi(struct drm_fb_helper *fb_helper)
  770. {
  771. struct device *dev = fb_helper->dev->dev;
  772. struct fb_info *info;
  773. int ret;
  774. info = framebuffer_alloc(0, dev);
  775. if (!info)
  776. return ERR_PTR(-ENOMEM);
  777. ret = fb_alloc_cmap(&info->cmap, 256, 0);
  778. if (ret)
  779. goto err_release;
  780. info->apertures = alloc_apertures(1);
  781. if (!info->apertures) {
  782. ret = -ENOMEM;
  783. goto err_free_cmap;
  784. }
  785. fb_helper->fbdev = info;
  786. return info;
  787. err_free_cmap:
  788. fb_dealloc_cmap(&info->cmap);
  789. err_release:
  790. framebuffer_release(info);
  791. return ERR_PTR(ret);
  792. }
  793. EXPORT_SYMBOL(drm_fb_helper_alloc_fbi);
  794. /**
  795. * drm_fb_helper_unregister_fbi - unregister fb_info framebuffer device
  796. * @fb_helper: driver-allocated fbdev helper, can be NULL
  797. *
  798. * A wrapper around unregister_framebuffer, to release the fb_info
  799. * framebuffer device. This must be called before releasing all resources for
  800. * @fb_helper by calling drm_fb_helper_fini().
  801. */
  802. void drm_fb_helper_unregister_fbi(struct drm_fb_helper *fb_helper)
  803. {
  804. if (fb_helper && fb_helper->fbdev)
  805. unregister_framebuffer(fb_helper->fbdev);
  806. }
  807. EXPORT_SYMBOL(drm_fb_helper_unregister_fbi);
  808. /**
  809. * drm_fb_helper_fini - finialize a &struct drm_fb_helper
  810. * @fb_helper: driver-allocated fbdev helper, can be NULL
  811. *
  812. * This cleans up all remaining resources associated with @fb_helper. Must be
  813. * called after drm_fb_helper_unlink_fbi() was called.
  814. */
  815. void drm_fb_helper_fini(struct drm_fb_helper *fb_helper)
  816. {
  817. struct fb_info *info;
  818. if (!fb_helper)
  819. return;
  820. fb_helper->dev->fb_helper = NULL;
  821. if (!drm_fbdev_emulation)
  822. return;
  823. cancel_work_sync(&fb_helper->resume_work);
  824. cancel_work_sync(&fb_helper->dirty_work);
  825. info = fb_helper->fbdev;
  826. if (info) {
  827. if (info->cmap.len)
  828. fb_dealloc_cmap(&info->cmap);
  829. framebuffer_release(info);
  830. }
  831. fb_helper->fbdev = NULL;
  832. mutex_lock(&kernel_fb_helper_lock);
  833. if (!list_empty(&fb_helper->kernel_fb_list)) {
  834. list_del(&fb_helper->kernel_fb_list);
  835. if (list_empty(&kernel_fb_helper_list))
  836. unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
  837. }
  838. mutex_unlock(&kernel_fb_helper_lock);
  839. mutex_destroy(&fb_helper->lock);
  840. drm_fb_helper_crtc_free(fb_helper);
  841. }
  842. EXPORT_SYMBOL(drm_fb_helper_fini);
  843. /**
  844. * drm_fb_helper_unlink_fbi - wrapper around unlink_framebuffer
  845. * @fb_helper: driver-allocated fbdev helper, can be NULL
  846. *
  847. * A wrapper around unlink_framebuffer implemented by fbdev core
  848. */
  849. void drm_fb_helper_unlink_fbi(struct drm_fb_helper *fb_helper)
  850. {
  851. if (fb_helper && fb_helper->fbdev)
  852. unlink_framebuffer(fb_helper->fbdev);
  853. }
  854. EXPORT_SYMBOL(drm_fb_helper_unlink_fbi);
  855. static void drm_fb_helper_dirty(struct fb_info *info, u32 x, u32 y,
  856. u32 width, u32 height)
  857. {
  858. struct drm_fb_helper *helper = info->par;
  859. struct drm_clip_rect *clip = &helper->dirty_clip;
  860. unsigned long flags;
  861. if (!helper->fb->funcs->dirty)
  862. return;
  863. spin_lock_irqsave(&helper->dirty_lock, flags);
  864. clip->x1 = min_t(u32, clip->x1, x);
  865. clip->y1 = min_t(u32, clip->y1, y);
  866. clip->x2 = max_t(u32, clip->x2, x + width);
  867. clip->y2 = max_t(u32, clip->y2, y + height);
  868. spin_unlock_irqrestore(&helper->dirty_lock, flags);
  869. schedule_work(&helper->dirty_work);
  870. }
  871. /**
  872. * drm_fb_helper_deferred_io() - fbdev deferred_io callback function
  873. * @info: fb_info struct pointer
  874. * @pagelist: list of dirty mmap framebuffer pages
  875. *
  876. * This function is used as the &fb_deferred_io.deferred_io
  877. * callback function for flushing the fbdev mmap writes.
  878. */
  879. void drm_fb_helper_deferred_io(struct fb_info *info,
  880. struct list_head *pagelist)
  881. {
  882. unsigned long start, end, min, max;
  883. struct page *page;
  884. u32 y1, y2;
  885. min = ULONG_MAX;
  886. max = 0;
  887. list_for_each_entry(page, pagelist, lru) {
  888. start = page->index << PAGE_SHIFT;
  889. end = start + PAGE_SIZE - 1;
  890. min = min(min, start);
  891. max = max(max, end);
  892. }
  893. if (min < max) {
  894. y1 = min / info->fix.line_length;
  895. y2 = min_t(u32, DIV_ROUND_UP(max, info->fix.line_length),
  896. info->var.yres);
  897. drm_fb_helper_dirty(info, 0, y1, info->var.xres, y2 - y1);
  898. }
  899. }
  900. EXPORT_SYMBOL(drm_fb_helper_deferred_io);
  901. /**
  902. * drm_fb_helper_defio_init - fbdev deferred I/O initialization
  903. * @fb_helper: driver-allocated fbdev helper
  904. *
  905. * This function allocates &fb_deferred_io, sets callback to
  906. * drm_fb_helper_deferred_io(), delay to 50ms and calls fb_deferred_io_init().
  907. * It should be called from the &drm_fb_helper_funcs->fb_probe callback.
  908. * drm_fb_helper_fbdev_teardown() cleans up deferred I/O.
  909. *
  910. * NOTE: A copy of &fb_ops is made and assigned to &info->fbops. This is done
  911. * because fb_deferred_io_cleanup() clears &fbops->fb_mmap and would thereby
  912. * affect other instances of that &fb_ops.
  913. *
  914. * Returns:
  915. * 0 on success or a negative error code on failure.
  916. */
  917. int drm_fb_helper_defio_init(struct drm_fb_helper *fb_helper)
  918. {
  919. struct fb_info *info = fb_helper->fbdev;
  920. struct fb_deferred_io *fbdefio;
  921. struct fb_ops *fbops;
  922. fbdefio = kzalloc(sizeof(*fbdefio), GFP_KERNEL);
  923. fbops = kzalloc(sizeof(*fbops), GFP_KERNEL);
  924. if (!fbdefio || !fbops) {
  925. kfree(fbdefio);
  926. kfree(fbops);
  927. return -ENOMEM;
  928. }
  929. info->fbdefio = fbdefio;
  930. fbdefio->delay = msecs_to_jiffies(50);
  931. fbdefio->deferred_io = drm_fb_helper_deferred_io;
  932. *fbops = *info->fbops;
  933. info->fbops = fbops;
  934. fb_deferred_io_init(info);
  935. return 0;
  936. }
  937. EXPORT_SYMBOL(drm_fb_helper_defio_init);
  938. /**
  939. * drm_fb_helper_sys_read - wrapper around fb_sys_read
  940. * @info: fb_info struct pointer
  941. * @buf: userspace buffer to read from framebuffer memory
  942. * @count: number of bytes to read from framebuffer memory
  943. * @ppos: read offset within framebuffer memory
  944. *
  945. * A wrapper around fb_sys_read implemented by fbdev core
  946. */
  947. ssize_t drm_fb_helper_sys_read(struct fb_info *info, char __user *buf,
  948. size_t count, loff_t *ppos)
  949. {
  950. return fb_sys_read(info, buf, count, ppos);
  951. }
  952. EXPORT_SYMBOL(drm_fb_helper_sys_read);
  953. /**
  954. * drm_fb_helper_sys_write - wrapper around fb_sys_write
  955. * @info: fb_info struct pointer
  956. * @buf: userspace buffer to write to framebuffer memory
  957. * @count: number of bytes to write to framebuffer memory
  958. * @ppos: write offset within framebuffer memory
  959. *
  960. * A wrapper around fb_sys_write implemented by fbdev core
  961. */
  962. ssize_t drm_fb_helper_sys_write(struct fb_info *info, const char __user *buf,
  963. size_t count, loff_t *ppos)
  964. {
  965. ssize_t ret;
  966. ret = fb_sys_write(info, buf, count, ppos);
  967. if (ret > 0)
  968. drm_fb_helper_dirty(info, 0, 0, info->var.xres,
  969. info->var.yres);
  970. return ret;
  971. }
  972. EXPORT_SYMBOL(drm_fb_helper_sys_write);
  973. /**
  974. * drm_fb_helper_sys_fillrect - wrapper around sys_fillrect
  975. * @info: fbdev registered by the helper
  976. * @rect: info about rectangle to fill
  977. *
  978. * A wrapper around sys_fillrect implemented by fbdev core
  979. */
  980. void drm_fb_helper_sys_fillrect(struct fb_info *info,
  981. const struct fb_fillrect *rect)
  982. {
  983. sys_fillrect(info, rect);
  984. drm_fb_helper_dirty(info, rect->dx, rect->dy,
  985. rect->width, rect->height);
  986. }
  987. EXPORT_SYMBOL(drm_fb_helper_sys_fillrect);
  988. /**
  989. * drm_fb_helper_sys_copyarea - wrapper around sys_copyarea
  990. * @info: fbdev registered by the helper
  991. * @area: info about area to copy
  992. *
  993. * A wrapper around sys_copyarea implemented by fbdev core
  994. */
  995. void drm_fb_helper_sys_copyarea(struct fb_info *info,
  996. const struct fb_copyarea *area)
  997. {
  998. sys_copyarea(info, area);
  999. drm_fb_helper_dirty(info, area->dx, area->dy,
  1000. area->width, area->height);
  1001. }
  1002. EXPORT_SYMBOL(drm_fb_helper_sys_copyarea);
  1003. /**
  1004. * drm_fb_helper_sys_imageblit - wrapper around sys_imageblit
  1005. * @info: fbdev registered by the helper
  1006. * @image: info about image to blit
  1007. *
  1008. * A wrapper around sys_imageblit implemented by fbdev core
  1009. */
  1010. void drm_fb_helper_sys_imageblit(struct fb_info *info,
  1011. const struct fb_image *image)
  1012. {
  1013. sys_imageblit(info, image);
  1014. drm_fb_helper_dirty(info, image->dx, image->dy,
  1015. image->width, image->height);
  1016. }
  1017. EXPORT_SYMBOL(drm_fb_helper_sys_imageblit);
  1018. /**
  1019. * drm_fb_helper_cfb_fillrect - wrapper around cfb_fillrect
  1020. * @info: fbdev registered by the helper
  1021. * @rect: info about rectangle to fill
  1022. *
  1023. * A wrapper around cfb_fillrect implemented by fbdev core
  1024. */
  1025. void drm_fb_helper_cfb_fillrect(struct fb_info *info,
  1026. const struct fb_fillrect *rect)
  1027. {
  1028. cfb_fillrect(info, rect);
  1029. drm_fb_helper_dirty(info, rect->dx, rect->dy,
  1030. rect->width, rect->height);
  1031. }
  1032. EXPORT_SYMBOL(drm_fb_helper_cfb_fillrect);
  1033. /**
  1034. * drm_fb_helper_cfb_copyarea - wrapper around cfb_copyarea
  1035. * @info: fbdev registered by the helper
  1036. * @area: info about area to copy
  1037. *
  1038. * A wrapper around cfb_copyarea implemented by fbdev core
  1039. */
  1040. void drm_fb_helper_cfb_copyarea(struct fb_info *info,
  1041. const struct fb_copyarea *area)
  1042. {
  1043. cfb_copyarea(info, area);
  1044. drm_fb_helper_dirty(info, area->dx, area->dy,
  1045. area->width, area->height);
  1046. }
  1047. EXPORT_SYMBOL(drm_fb_helper_cfb_copyarea);
  1048. /**
  1049. * drm_fb_helper_cfb_imageblit - wrapper around cfb_imageblit
  1050. * @info: fbdev registered by the helper
  1051. * @image: info about image to blit
  1052. *
  1053. * A wrapper around cfb_imageblit implemented by fbdev core
  1054. */
  1055. void drm_fb_helper_cfb_imageblit(struct fb_info *info,
  1056. const struct fb_image *image)
  1057. {
  1058. cfb_imageblit(info, image);
  1059. drm_fb_helper_dirty(info, image->dx, image->dy,
  1060. image->width, image->height);
  1061. }
  1062. EXPORT_SYMBOL(drm_fb_helper_cfb_imageblit);
  1063. /**
  1064. * drm_fb_helper_set_suspend - wrapper around fb_set_suspend
  1065. * @fb_helper: driver-allocated fbdev helper, can be NULL
  1066. * @suspend: whether to suspend or resume
  1067. *
  1068. * A wrapper around fb_set_suspend implemented by fbdev core.
  1069. * Use drm_fb_helper_set_suspend_unlocked() if you don't need to take
  1070. * the lock yourself
  1071. */
  1072. void drm_fb_helper_set_suspend(struct drm_fb_helper *fb_helper, bool suspend)
  1073. {
  1074. if (fb_helper && fb_helper->fbdev)
  1075. fb_set_suspend(fb_helper->fbdev, suspend);
  1076. }
  1077. EXPORT_SYMBOL(drm_fb_helper_set_suspend);
  1078. /**
  1079. * drm_fb_helper_set_suspend_unlocked - wrapper around fb_set_suspend that also
  1080. * takes the console lock
  1081. * @fb_helper: driver-allocated fbdev helper, can be NULL
  1082. * @suspend: whether to suspend or resume
  1083. *
  1084. * A wrapper around fb_set_suspend() that takes the console lock. If the lock
  1085. * isn't available on resume, a worker is tasked with waiting for the lock
  1086. * to become available. The console lock can be pretty contented on resume
  1087. * due to all the printk activity.
  1088. *
  1089. * This function can be called multiple times with the same state since
  1090. * &fb_info.state is checked to see if fbdev is running or not before locking.
  1091. *
  1092. * Use drm_fb_helper_set_suspend() if you need to take the lock yourself.
  1093. */
  1094. void drm_fb_helper_set_suspend_unlocked(struct drm_fb_helper *fb_helper,
  1095. bool suspend)
  1096. {
  1097. if (!fb_helper || !fb_helper->fbdev)
  1098. return;
  1099. /* make sure there's no pending/ongoing resume */
  1100. flush_work(&fb_helper->resume_work);
  1101. if (suspend) {
  1102. if (fb_helper->fbdev->state != FBINFO_STATE_RUNNING)
  1103. return;
  1104. console_lock();
  1105. } else {
  1106. if (fb_helper->fbdev->state == FBINFO_STATE_RUNNING)
  1107. return;
  1108. if (!console_trylock()) {
  1109. schedule_work(&fb_helper->resume_work);
  1110. return;
  1111. }
  1112. }
  1113. fb_set_suspend(fb_helper->fbdev, suspend);
  1114. console_unlock();
  1115. }
  1116. EXPORT_SYMBOL(drm_fb_helper_set_suspend_unlocked);
  1117. static int setcmap_pseudo_palette(struct fb_cmap *cmap, struct fb_info *info)
  1118. {
  1119. u32 *palette = (u32 *)info->pseudo_palette;
  1120. int i;
  1121. if (cmap->start + cmap->len > 16)
  1122. return -EINVAL;
  1123. for (i = 0; i < cmap->len; ++i) {
  1124. u16 red = cmap->red[i];
  1125. u16 green = cmap->green[i];
  1126. u16 blue = cmap->blue[i];
  1127. u32 value;
  1128. red >>= 16 - info->var.red.length;
  1129. green >>= 16 - info->var.green.length;
  1130. blue >>= 16 - info->var.blue.length;
  1131. value = (red << info->var.red.offset) |
  1132. (green << info->var.green.offset) |
  1133. (blue << info->var.blue.offset);
  1134. if (info->var.transp.length > 0) {
  1135. u32 mask = (1 << info->var.transp.length) - 1;
  1136. mask <<= info->var.transp.offset;
  1137. value |= mask;
  1138. }
  1139. palette[cmap->start + i] = value;
  1140. }
  1141. return 0;
  1142. }
  1143. static int setcmap_legacy(struct fb_cmap *cmap, struct fb_info *info)
  1144. {
  1145. struct drm_fb_helper *fb_helper = info->par;
  1146. struct drm_crtc *crtc;
  1147. u16 *r, *g, *b;
  1148. int i, ret = 0;
  1149. drm_modeset_lock_all(fb_helper->dev);
  1150. for (i = 0; i < fb_helper->crtc_count; i++) {
  1151. crtc = fb_helper->crtc_info[i].mode_set.crtc;
  1152. if (!crtc->funcs->gamma_set || !crtc->gamma_size)
  1153. return -EINVAL;
  1154. if (cmap->start + cmap->len > crtc->gamma_size)
  1155. return -EINVAL;
  1156. r = crtc->gamma_store;
  1157. g = r + crtc->gamma_size;
  1158. b = g + crtc->gamma_size;
  1159. memcpy(r + cmap->start, cmap->red, cmap->len * sizeof(*r));
  1160. memcpy(g + cmap->start, cmap->green, cmap->len * sizeof(*g));
  1161. memcpy(b + cmap->start, cmap->blue, cmap->len * sizeof(*b));
  1162. ret = crtc->funcs->gamma_set(crtc, r, g, b,
  1163. crtc->gamma_size, NULL);
  1164. if (ret)
  1165. return ret;
  1166. }
  1167. drm_modeset_unlock_all(fb_helper->dev);
  1168. return ret;
  1169. }
  1170. static struct drm_property_blob *setcmap_new_gamma_lut(struct drm_crtc *crtc,
  1171. struct fb_cmap *cmap)
  1172. {
  1173. struct drm_device *dev = crtc->dev;
  1174. struct drm_property_blob *gamma_lut;
  1175. struct drm_color_lut *lut;
  1176. int size = crtc->gamma_size;
  1177. int i;
  1178. if (!size || cmap->start + cmap->len > size)
  1179. return ERR_PTR(-EINVAL);
  1180. gamma_lut = drm_property_create_blob(dev, sizeof(*lut) * size, NULL);
  1181. if (IS_ERR(gamma_lut))
  1182. return gamma_lut;
  1183. lut = gamma_lut->data;
  1184. if (cmap->start || cmap->len != size) {
  1185. u16 *r = crtc->gamma_store;
  1186. u16 *g = r + crtc->gamma_size;
  1187. u16 *b = g + crtc->gamma_size;
  1188. for (i = 0; i < cmap->start; i++) {
  1189. lut[i].red = r[i];
  1190. lut[i].green = g[i];
  1191. lut[i].blue = b[i];
  1192. }
  1193. for (i = cmap->start + cmap->len; i < size; i++) {
  1194. lut[i].red = r[i];
  1195. lut[i].green = g[i];
  1196. lut[i].blue = b[i];
  1197. }
  1198. }
  1199. for (i = 0; i < cmap->len; i++) {
  1200. lut[cmap->start + i].red = cmap->red[i];
  1201. lut[cmap->start + i].green = cmap->green[i];
  1202. lut[cmap->start + i].blue = cmap->blue[i];
  1203. }
  1204. return gamma_lut;
  1205. }
  1206. static int setcmap_atomic(struct fb_cmap *cmap, struct fb_info *info)
  1207. {
  1208. struct drm_fb_helper *fb_helper = info->par;
  1209. struct drm_device *dev = fb_helper->dev;
  1210. struct drm_property_blob *gamma_lut = NULL;
  1211. struct drm_modeset_acquire_ctx ctx;
  1212. struct drm_crtc_state *crtc_state;
  1213. struct drm_atomic_state *state;
  1214. struct drm_crtc *crtc;
  1215. u16 *r, *g, *b;
  1216. int i, ret = 0;
  1217. bool replaced;
  1218. drm_modeset_acquire_init(&ctx, 0);
  1219. state = drm_atomic_state_alloc(dev);
  1220. if (!state) {
  1221. ret = -ENOMEM;
  1222. goto out_ctx;
  1223. }
  1224. state->acquire_ctx = &ctx;
  1225. retry:
  1226. for (i = 0; i < fb_helper->crtc_count; i++) {
  1227. crtc = fb_helper->crtc_info[i].mode_set.crtc;
  1228. if (!gamma_lut)
  1229. gamma_lut = setcmap_new_gamma_lut(crtc, cmap);
  1230. if (IS_ERR(gamma_lut)) {
  1231. ret = PTR_ERR(gamma_lut);
  1232. gamma_lut = NULL;
  1233. goto out_state;
  1234. }
  1235. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  1236. if (IS_ERR(crtc_state)) {
  1237. ret = PTR_ERR(crtc_state);
  1238. goto out_state;
  1239. }
  1240. replaced = drm_property_replace_blob(&crtc_state->degamma_lut,
  1241. NULL);
  1242. replaced |= drm_property_replace_blob(&crtc_state->ctm, NULL);
  1243. replaced |= drm_property_replace_blob(&crtc_state->gamma_lut,
  1244. gamma_lut);
  1245. crtc_state->color_mgmt_changed |= replaced;
  1246. }
  1247. ret = drm_atomic_commit(state);
  1248. if (ret)
  1249. goto out_state;
  1250. for (i = 0; i < fb_helper->crtc_count; i++) {
  1251. crtc = fb_helper->crtc_info[i].mode_set.crtc;
  1252. r = crtc->gamma_store;
  1253. g = r + crtc->gamma_size;
  1254. b = g + crtc->gamma_size;
  1255. memcpy(r + cmap->start, cmap->red, cmap->len * sizeof(*r));
  1256. memcpy(g + cmap->start, cmap->green, cmap->len * sizeof(*g));
  1257. memcpy(b + cmap->start, cmap->blue, cmap->len * sizeof(*b));
  1258. }
  1259. out_state:
  1260. if (ret == -EDEADLK)
  1261. goto backoff;
  1262. drm_property_blob_put(gamma_lut);
  1263. drm_atomic_state_put(state);
  1264. out_ctx:
  1265. drm_modeset_drop_locks(&ctx);
  1266. drm_modeset_acquire_fini(&ctx);
  1267. return ret;
  1268. backoff:
  1269. drm_atomic_state_clear(state);
  1270. drm_modeset_backoff(&ctx);
  1271. goto retry;
  1272. }
  1273. /**
  1274. * drm_fb_helper_setcmap - implementation for &fb_ops.fb_setcmap
  1275. * @cmap: cmap to set
  1276. * @info: fbdev registered by the helper
  1277. */
  1278. int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info)
  1279. {
  1280. struct drm_fb_helper *fb_helper = info->par;
  1281. int ret;
  1282. if (oops_in_progress)
  1283. return -EBUSY;
  1284. mutex_lock(&fb_helper->lock);
  1285. if (!drm_fb_helper_is_bound(fb_helper)) {
  1286. ret = -EBUSY;
  1287. goto out;
  1288. }
  1289. if (info->fix.visual == FB_VISUAL_TRUECOLOR)
  1290. ret = setcmap_pseudo_palette(cmap, info);
  1291. else if (drm_drv_uses_atomic_modeset(fb_helper->dev))
  1292. ret = setcmap_atomic(cmap, info);
  1293. else
  1294. ret = setcmap_legacy(cmap, info);
  1295. out:
  1296. mutex_unlock(&fb_helper->lock);
  1297. return ret;
  1298. }
  1299. EXPORT_SYMBOL(drm_fb_helper_setcmap);
  1300. /**
  1301. * drm_fb_helper_ioctl - legacy ioctl implementation
  1302. * @info: fbdev registered by the helper
  1303. * @cmd: ioctl command
  1304. * @arg: ioctl argument
  1305. *
  1306. * A helper to implement the standard fbdev ioctl. Only
  1307. * FBIO_WAITFORVSYNC is implemented for now.
  1308. */
  1309. int drm_fb_helper_ioctl(struct fb_info *info, unsigned int cmd,
  1310. unsigned long arg)
  1311. {
  1312. struct drm_fb_helper *fb_helper = info->par;
  1313. struct drm_mode_set *mode_set;
  1314. struct drm_crtc *crtc;
  1315. int ret = 0;
  1316. mutex_lock(&fb_helper->lock);
  1317. if (!drm_fb_helper_is_bound(fb_helper)) {
  1318. ret = -EBUSY;
  1319. goto unlock;
  1320. }
  1321. switch (cmd) {
  1322. case FBIO_WAITFORVSYNC:
  1323. /*
  1324. * Only consider the first CRTC.
  1325. *
  1326. * This ioctl is supposed to take the CRTC number as
  1327. * an argument, but in fbdev times, what that number
  1328. * was supposed to be was quite unclear, different
  1329. * drivers were passing that argument differently
  1330. * (some by reference, some by value), and most of the
  1331. * userspace applications were just hardcoding 0 as an
  1332. * argument.
  1333. *
  1334. * The first CRTC should be the integrated panel on
  1335. * most drivers, so this is the best choice we can
  1336. * make. If we're not smart enough here, one should
  1337. * just consider switch the userspace to KMS.
  1338. */
  1339. mode_set = &fb_helper->crtc_info[0].mode_set;
  1340. crtc = mode_set->crtc;
  1341. /*
  1342. * Only wait for a vblank event if the CRTC is
  1343. * enabled, otherwise just don't do anythintg,
  1344. * not even report an error.
  1345. */
  1346. ret = drm_crtc_vblank_get(crtc);
  1347. if (!ret) {
  1348. drm_crtc_wait_one_vblank(crtc);
  1349. drm_crtc_vblank_put(crtc);
  1350. }
  1351. ret = 0;
  1352. goto unlock;
  1353. default:
  1354. ret = -ENOTTY;
  1355. }
  1356. unlock:
  1357. mutex_unlock(&fb_helper->lock);
  1358. return ret;
  1359. }
  1360. EXPORT_SYMBOL(drm_fb_helper_ioctl);
  1361. static bool drm_fb_pixel_format_equal(const struct fb_var_screeninfo *var_1,
  1362. const struct fb_var_screeninfo *var_2)
  1363. {
  1364. return var_1->bits_per_pixel == var_2->bits_per_pixel &&
  1365. var_1->grayscale == var_2->grayscale &&
  1366. var_1->red.offset == var_2->red.offset &&
  1367. var_1->red.length == var_2->red.length &&
  1368. var_1->red.msb_right == var_2->red.msb_right &&
  1369. var_1->green.offset == var_2->green.offset &&
  1370. var_1->green.length == var_2->green.length &&
  1371. var_1->green.msb_right == var_2->green.msb_right &&
  1372. var_1->blue.offset == var_2->blue.offset &&
  1373. var_1->blue.length == var_2->blue.length &&
  1374. var_1->blue.msb_right == var_2->blue.msb_right &&
  1375. var_1->transp.offset == var_2->transp.offset &&
  1376. var_1->transp.length == var_2->transp.length &&
  1377. var_1->transp.msb_right == var_2->transp.msb_right;
  1378. }
  1379. /**
  1380. * drm_fb_helper_check_var - implementation for &fb_ops.fb_check_var
  1381. * @var: screeninfo to check
  1382. * @info: fbdev registered by the helper
  1383. */
  1384. int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
  1385. struct fb_info *info)
  1386. {
  1387. struct drm_fb_helper *fb_helper = info->par;
  1388. struct drm_framebuffer *fb = fb_helper->fb;
  1389. if (var->pixclock != 0 || in_dbg_master())
  1390. return -EINVAL;
  1391. /*
  1392. * Changes struct fb_var_screeninfo are currently not pushed back
  1393. * to KMS, hence fail if different settings are requested.
  1394. */
  1395. if (var->bits_per_pixel != fb->format->cpp[0] * 8 ||
  1396. var->xres > fb->width || var->yres > fb->height ||
  1397. var->xres_virtual > fb->width || var->yres_virtual > fb->height) {
  1398. DRM_DEBUG("fb requested width/height/bpp can't fit in current fb "
  1399. "request %dx%d-%d (virtual %dx%d) > %dx%d-%d\n",
  1400. var->xres, var->yres, var->bits_per_pixel,
  1401. var->xres_virtual, var->yres_virtual,
  1402. fb->width, fb->height, fb->format->cpp[0] * 8);
  1403. return -EINVAL;
  1404. }
  1405. /*
  1406. * drm fbdev emulation doesn't support changing the pixel format at all,
  1407. * so reject all pixel format changing requests.
  1408. */
  1409. if (!drm_fb_pixel_format_equal(var, &info->var)) {
  1410. DRM_DEBUG("fbdev emulation doesn't support changing the pixel format\n");
  1411. return -EINVAL;
  1412. }
  1413. return 0;
  1414. }
  1415. EXPORT_SYMBOL(drm_fb_helper_check_var);
  1416. /**
  1417. * drm_fb_helper_set_par - implementation for &fb_ops.fb_set_par
  1418. * @info: fbdev registered by the helper
  1419. *
  1420. * This will let fbcon do the mode init and is called at initialization time by
  1421. * the fbdev core when registering the driver, and later on through the hotplug
  1422. * callback.
  1423. */
  1424. int drm_fb_helper_set_par(struct fb_info *info)
  1425. {
  1426. struct drm_fb_helper *fb_helper = info->par;
  1427. struct fb_var_screeninfo *var = &info->var;
  1428. if (oops_in_progress)
  1429. return -EBUSY;
  1430. if (var->pixclock != 0) {
  1431. DRM_ERROR("PIXEL CLOCK SET\n");
  1432. return -EINVAL;
  1433. }
  1434. drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper);
  1435. return 0;
  1436. }
  1437. EXPORT_SYMBOL(drm_fb_helper_set_par);
  1438. static void pan_set(struct drm_fb_helper *fb_helper, int x, int y)
  1439. {
  1440. int i;
  1441. for (i = 0; i < fb_helper->crtc_count; i++) {
  1442. struct drm_mode_set *mode_set;
  1443. mode_set = &fb_helper->crtc_info[i].mode_set;
  1444. mode_set->x = x;
  1445. mode_set->y = y;
  1446. }
  1447. }
  1448. static int pan_display_atomic(struct fb_var_screeninfo *var,
  1449. struct fb_info *info)
  1450. {
  1451. struct drm_fb_helper *fb_helper = info->par;
  1452. int ret;
  1453. pan_set(fb_helper, var->xoffset, var->yoffset);
  1454. ret = restore_fbdev_mode_atomic(fb_helper, true);
  1455. if (!ret) {
  1456. info->var.xoffset = var->xoffset;
  1457. info->var.yoffset = var->yoffset;
  1458. } else
  1459. pan_set(fb_helper, info->var.xoffset, info->var.yoffset);
  1460. return ret;
  1461. }
  1462. static int pan_display_legacy(struct fb_var_screeninfo *var,
  1463. struct fb_info *info)
  1464. {
  1465. struct drm_fb_helper *fb_helper = info->par;
  1466. struct drm_mode_set *modeset;
  1467. int ret = 0;
  1468. int i;
  1469. drm_modeset_lock_all(fb_helper->dev);
  1470. for (i = 0; i < fb_helper->crtc_count; i++) {
  1471. modeset = &fb_helper->crtc_info[i].mode_set;
  1472. modeset->x = var->xoffset;
  1473. modeset->y = var->yoffset;
  1474. if (modeset->num_connectors) {
  1475. ret = drm_mode_set_config_internal(modeset);
  1476. if (!ret) {
  1477. info->var.xoffset = var->xoffset;
  1478. info->var.yoffset = var->yoffset;
  1479. }
  1480. }
  1481. }
  1482. drm_modeset_unlock_all(fb_helper->dev);
  1483. return ret;
  1484. }
  1485. /**
  1486. * drm_fb_helper_pan_display - implementation for &fb_ops.fb_pan_display
  1487. * @var: updated screen information
  1488. * @info: fbdev registered by the helper
  1489. */
  1490. int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
  1491. struct fb_info *info)
  1492. {
  1493. struct drm_fb_helper *fb_helper = info->par;
  1494. struct drm_device *dev = fb_helper->dev;
  1495. int ret;
  1496. if (oops_in_progress)
  1497. return -EBUSY;
  1498. mutex_lock(&fb_helper->lock);
  1499. if (!drm_fb_helper_is_bound(fb_helper)) {
  1500. mutex_unlock(&fb_helper->lock);
  1501. return -EBUSY;
  1502. }
  1503. if (drm_drv_uses_atomic_modeset(dev))
  1504. ret = pan_display_atomic(var, info);
  1505. else
  1506. ret = pan_display_legacy(var, info);
  1507. mutex_unlock(&fb_helper->lock);
  1508. return ret;
  1509. }
  1510. EXPORT_SYMBOL(drm_fb_helper_pan_display);
  1511. /*
  1512. * Allocates the backing storage and sets up the fbdev info structure through
  1513. * the ->fb_probe callback.
  1514. */
  1515. static int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
  1516. int preferred_bpp)
  1517. {
  1518. int ret = 0;
  1519. int crtc_count = 0;
  1520. int i;
  1521. struct drm_fb_helper_surface_size sizes;
  1522. int gamma_size = 0;
  1523. memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size));
  1524. sizes.surface_depth = 24;
  1525. sizes.surface_bpp = 32;
  1526. sizes.fb_width = (u32)-1;
  1527. sizes.fb_height = (u32)-1;
  1528. /* if driver picks 8 or 16 by default use that for both depth/bpp */
  1529. if (preferred_bpp != sizes.surface_bpp)
  1530. sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
  1531. /* first up get a count of crtcs now in use and new min/maxes width/heights */
  1532. drm_fb_helper_for_each_connector(fb_helper, i) {
  1533. struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i];
  1534. struct drm_cmdline_mode *cmdline_mode;
  1535. cmdline_mode = &fb_helper_conn->connector->cmdline_mode;
  1536. if (cmdline_mode->bpp_specified) {
  1537. switch (cmdline_mode->bpp) {
  1538. case 8:
  1539. sizes.surface_depth = sizes.surface_bpp = 8;
  1540. break;
  1541. case 15:
  1542. sizes.surface_depth = 15;
  1543. sizes.surface_bpp = 16;
  1544. break;
  1545. case 16:
  1546. sizes.surface_depth = sizes.surface_bpp = 16;
  1547. break;
  1548. case 24:
  1549. sizes.surface_depth = sizes.surface_bpp = 24;
  1550. break;
  1551. case 32:
  1552. sizes.surface_depth = 24;
  1553. sizes.surface_bpp = 32;
  1554. break;
  1555. }
  1556. break;
  1557. }
  1558. }
  1559. crtc_count = 0;
  1560. for (i = 0; i < fb_helper->crtc_count; i++) {
  1561. struct drm_display_mode *desired_mode;
  1562. struct drm_mode_set *mode_set;
  1563. int x, y, j;
  1564. /* in case of tile group, are we the last tile vert or horiz?
  1565. * If no tile group you are always the last one both vertically
  1566. * and horizontally
  1567. */
  1568. bool lastv = true, lasth = true;
  1569. desired_mode = fb_helper->crtc_info[i].desired_mode;
  1570. mode_set = &fb_helper->crtc_info[i].mode_set;
  1571. if (!desired_mode)
  1572. continue;
  1573. crtc_count++;
  1574. x = fb_helper->crtc_info[i].x;
  1575. y = fb_helper->crtc_info[i].y;
  1576. if (gamma_size == 0)
  1577. gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size;
  1578. sizes.surface_width = max_t(u32, desired_mode->hdisplay + x, sizes.surface_width);
  1579. sizes.surface_height = max_t(u32, desired_mode->vdisplay + y, sizes.surface_height);
  1580. for (j = 0; j < mode_set->num_connectors; j++) {
  1581. struct drm_connector *connector = mode_set->connectors[j];
  1582. if (connector->has_tile) {
  1583. lasth = (connector->tile_h_loc == (connector->num_h_tile - 1));
  1584. lastv = (connector->tile_v_loc == (connector->num_v_tile - 1));
  1585. /* cloning to multiple tiles is just crazy-talk, so: */
  1586. break;
  1587. }
  1588. }
  1589. if (lasth)
  1590. sizes.fb_width = min_t(u32, desired_mode->hdisplay + x, sizes.fb_width);
  1591. if (lastv)
  1592. sizes.fb_height = min_t(u32, desired_mode->vdisplay + y, sizes.fb_height);
  1593. }
  1594. if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) {
  1595. DRM_INFO("Cannot find any crtc or sizes\n");
  1596. /* First time: disable all crtc's.. */
  1597. if (!fb_helper->deferred_setup && !READ_ONCE(fb_helper->dev->master))
  1598. restore_fbdev_mode(fb_helper);
  1599. return -EAGAIN;
  1600. }
  1601. /* Handle our overallocation */
  1602. sizes.surface_height *= drm_fbdev_overalloc;
  1603. sizes.surface_height /= 100;
  1604. /* push down into drivers */
  1605. ret = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes);
  1606. if (ret < 0)
  1607. return ret;
  1608. strcpy(fb_helper->fb->comm, "[fbcon]");
  1609. return 0;
  1610. }
  1611. /**
  1612. * drm_fb_helper_fill_fix - initializes fixed fbdev information
  1613. * @info: fbdev registered by the helper
  1614. * @pitch: desired pitch
  1615. * @depth: desired depth
  1616. *
  1617. * Helper to fill in the fixed fbdev information useful for a non-accelerated
  1618. * fbdev emulations. Drivers which support acceleration methods which impose
  1619. * additional constraints need to set up their own limits.
  1620. *
  1621. * Drivers should call this (or their equivalent setup code) from their
  1622. * &drm_fb_helper_funcs.fb_probe callback.
  1623. */
  1624. void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
  1625. uint32_t depth)
  1626. {
  1627. info->fix.type = FB_TYPE_PACKED_PIXELS;
  1628. info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR :
  1629. FB_VISUAL_TRUECOLOR;
  1630. info->fix.mmio_start = 0;
  1631. info->fix.mmio_len = 0;
  1632. info->fix.type_aux = 0;
  1633. info->fix.xpanstep = 1; /* doing it in hw */
  1634. info->fix.ypanstep = 1; /* doing it in hw */
  1635. info->fix.ywrapstep = 0;
  1636. info->fix.accel = FB_ACCEL_NONE;
  1637. info->fix.line_length = pitch;
  1638. }
  1639. EXPORT_SYMBOL(drm_fb_helper_fill_fix);
  1640. /**
  1641. * drm_fb_helper_fill_var - initalizes variable fbdev information
  1642. * @info: fbdev instance to set up
  1643. * @fb_helper: fb helper instance to use as template
  1644. * @fb_width: desired fb width
  1645. * @fb_height: desired fb height
  1646. *
  1647. * Sets up the variable fbdev metainformation from the given fb helper instance
  1648. * and the drm framebuffer allocated in &drm_fb_helper.fb.
  1649. *
  1650. * Drivers should call this (or their equivalent setup code) from their
  1651. * &drm_fb_helper_funcs.fb_probe callback after having allocated the fbdev
  1652. * backing storage framebuffer.
  1653. */
  1654. void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper,
  1655. uint32_t fb_width, uint32_t fb_height)
  1656. {
  1657. struct drm_framebuffer *fb = fb_helper->fb;
  1658. info->pseudo_palette = fb_helper->pseudo_palette;
  1659. info->var.xres_virtual = fb->width;
  1660. info->var.yres_virtual = fb->height;
  1661. info->var.bits_per_pixel = fb->format->cpp[0] * 8;
  1662. info->var.accel_flags = FB_ACCELF_TEXT;
  1663. info->var.xoffset = 0;
  1664. info->var.yoffset = 0;
  1665. info->var.activate = FB_ACTIVATE_NOW;
  1666. switch (fb->format->depth) {
  1667. case 8:
  1668. info->var.red.offset = 0;
  1669. info->var.green.offset = 0;
  1670. info->var.blue.offset = 0;
  1671. info->var.red.length = 8; /* 8bit DAC */
  1672. info->var.green.length = 8;
  1673. info->var.blue.length = 8;
  1674. info->var.transp.offset = 0;
  1675. info->var.transp.length = 0;
  1676. break;
  1677. case 15:
  1678. info->var.red.offset = 10;
  1679. info->var.green.offset = 5;
  1680. info->var.blue.offset = 0;
  1681. info->var.red.length = 5;
  1682. info->var.green.length = 5;
  1683. info->var.blue.length = 5;
  1684. info->var.transp.offset = 15;
  1685. info->var.transp.length = 1;
  1686. break;
  1687. case 16:
  1688. info->var.red.offset = 11;
  1689. info->var.green.offset = 5;
  1690. info->var.blue.offset = 0;
  1691. info->var.red.length = 5;
  1692. info->var.green.length = 6;
  1693. info->var.blue.length = 5;
  1694. info->var.transp.offset = 0;
  1695. break;
  1696. case 24:
  1697. info->var.red.offset = 16;
  1698. info->var.green.offset = 8;
  1699. info->var.blue.offset = 0;
  1700. info->var.red.length = 8;
  1701. info->var.green.length = 8;
  1702. info->var.blue.length = 8;
  1703. info->var.transp.offset = 0;
  1704. info->var.transp.length = 0;
  1705. break;
  1706. case 32:
  1707. info->var.red.offset = 16;
  1708. info->var.green.offset = 8;
  1709. info->var.blue.offset = 0;
  1710. info->var.red.length = 8;
  1711. info->var.green.length = 8;
  1712. info->var.blue.length = 8;
  1713. info->var.transp.offset = 24;
  1714. info->var.transp.length = 8;
  1715. break;
  1716. default:
  1717. break;
  1718. }
  1719. info->var.xres = fb_width;
  1720. info->var.yres = fb_height;
  1721. }
  1722. EXPORT_SYMBOL(drm_fb_helper_fill_var);
  1723. static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper,
  1724. uint32_t maxX,
  1725. uint32_t maxY)
  1726. {
  1727. struct drm_connector *connector;
  1728. int i, count = 0;
  1729. drm_fb_helper_for_each_connector(fb_helper, i) {
  1730. connector = fb_helper->connector_info[i]->connector;
  1731. count += connector->funcs->fill_modes(connector, maxX, maxY);
  1732. }
  1733. return count;
  1734. }
  1735. struct drm_display_mode *drm_has_preferred_mode(struct drm_fb_helper_connector *fb_connector, int width, int height)
  1736. {
  1737. struct drm_display_mode *mode;
  1738. list_for_each_entry(mode, &fb_connector->connector->modes, head) {
  1739. if (mode->hdisplay > width ||
  1740. mode->vdisplay > height)
  1741. continue;
  1742. if (mode->type & DRM_MODE_TYPE_PREFERRED)
  1743. return mode;
  1744. }
  1745. return NULL;
  1746. }
  1747. EXPORT_SYMBOL(drm_has_preferred_mode);
  1748. static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector)
  1749. {
  1750. return fb_connector->connector->cmdline_mode.specified;
  1751. }
  1752. struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn)
  1753. {
  1754. struct drm_cmdline_mode *cmdline_mode;
  1755. struct drm_display_mode *mode;
  1756. bool prefer_non_interlace;
  1757. cmdline_mode = &fb_helper_conn->connector->cmdline_mode;
  1758. if (cmdline_mode->specified == false)
  1759. return NULL;
  1760. /* attempt to find a matching mode in the list of modes
  1761. * we have gotten so far, if not add a CVT mode that conforms
  1762. */
  1763. if (cmdline_mode->rb || cmdline_mode->margins)
  1764. goto create_mode;
  1765. prefer_non_interlace = !cmdline_mode->interlace;
  1766. again:
  1767. list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
  1768. /* check width/height */
  1769. if (mode->hdisplay != cmdline_mode->xres ||
  1770. mode->vdisplay != cmdline_mode->yres)
  1771. continue;
  1772. if (cmdline_mode->refresh_specified) {
  1773. if (mode->vrefresh != cmdline_mode->refresh)
  1774. continue;
  1775. }
  1776. if (cmdline_mode->interlace) {
  1777. if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
  1778. continue;
  1779. } else if (prefer_non_interlace) {
  1780. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  1781. continue;
  1782. }
  1783. return mode;
  1784. }
  1785. if (prefer_non_interlace) {
  1786. prefer_non_interlace = false;
  1787. goto again;
  1788. }
  1789. create_mode:
  1790. mode = drm_mode_create_from_cmdline_mode(fb_helper_conn->connector->dev,
  1791. cmdline_mode);
  1792. list_add(&mode->head, &fb_helper_conn->connector->modes);
  1793. return mode;
  1794. }
  1795. EXPORT_SYMBOL(drm_pick_cmdline_mode);
  1796. static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
  1797. {
  1798. bool enable;
  1799. if (connector->display_info.non_desktop)
  1800. return false;
  1801. if (strict)
  1802. enable = connector->status == connector_status_connected;
  1803. else
  1804. enable = connector->status != connector_status_disconnected;
  1805. return enable;
  1806. }
  1807. static void drm_enable_connectors(struct drm_fb_helper *fb_helper,
  1808. bool *enabled)
  1809. {
  1810. bool any_enabled = false;
  1811. struct drm_connector *connector;
  1812. int i = 0;
  1813. drm_fb_helper_for_each_connector(fb_helper, i) {
  1814. connector = fb_helper->connector_info[i]->connector;
  1815. enabled[i] = drm_connector_enabled(connector, true);
  1816. DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
  1817. connector->display_info.non_desktop ? "non desktop" : enabled[i] ? "yes" : "no");
  1818. any_enabled |= enabled[i];
  1819. }
  1820. if (any_enabled)
  1821. return;
  1822. drm_fb_helper_for_each_connector(fb_helper, i) {
  1823. connector = fb_helper->connector_info[i]->connector;
  1824. enabled[i] = drm_connector_enabled(connector, false);
  1825. }
  1826. }
  1827. static bool drm_target_cloned(struct drm_fb_helper *fb_helper,
  1828. struct drm_display_mode **modes,
  1829. struct drm_fb_offset *offsets,
  1830. bool *enabled, int width, int height)
  1831. {
  1832. int count, i, j;
  1833. bool can_clone = false;
  1834. struct drm_fb_helper_connector *fb_helper_conn;
  1835. struct drm_display_mode *dmt_mode, *mode;
  1836. /* only contemplate cloning in the single crtc case */
  1837. if (fb_helper->crtc_count > 1)
  1838. return false;
  1839. count = 0;
  1840. drm_fb_helper_for_each_connector(fb_helper, i) {
  1841. if (enabled[i])
  1842. count++;
  1843. }
  1844. /* only contemplate cloning if more than one connector is enabled */
  1845. if (count <= 1)
  1846. return false;
  1847. /* check the command line or if nothing common pick 1024x768 */
  1848. can_clone = true;
  1849. drm_fb_helper_for_each_connector(fb_helper, i) {
  1850. if (!enabled[i])
  1851. continue;
  1852. fb_helper_conn = fb_helper->connector_info[i];
  1853. modes[i] = drm_pick_cmdline_mode(fb_helper_conn);
  1854. if (!modes[i]) {
  1855. can_clone = false;
  1856. break;
  1857. }
  1858. for (j = 0; j < i; j++) {
  1859. if (!enabled[j])
  1860. continue;
  1861. if (!drm_mode_match(modes[j], modes[i],
  1862. DRM_MODE_MATCH_TIMINGS |
  1863. DRM_MODE_MATCH_CLOCK |
  1864. DRM_MODE_MATCH_FLAGS |
  1865. DRM_MODE_MATCH_3D_FLAGS))
  1866. can_clone = false;
  1867. }
  1868. }
  1869. if (can_clone) {
  1870. DRM_DEBUG_KMS("can clone using command line\n");
  1871. return true;
  1872. }
  1873. /* try and find a 1024x768 mode on each connector */
  1874. can_clone = true;
  1875. dmt_mode = drm_mode_find_dmt(fb_helper->dev, 1024, 768, 60, false);
  1876. drm_fb_helper_for_each_connector(fb_helper, i) {
  1877. if (!enabled[i])
  1878. continue;
  1879. fb_helper_conn = fb_helper->connector_info[i];
  1880. list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
  1881. if (drm_mode_match(mode, dmt_mode,
  1882. DRM_MODE_MATCH_TIMINGS |
  1883. DRM_MODE_MATCH_CLOCK |
  1884. DRM_MODE_MATCH_FLAGS |
  1885. DRM_MODE_MATCH_3D_FLAGS))
  1886. modes[i] = mode;
  1887. }
  1888. if (!modes[i])
  1889. can_clone = false;
  1890. }
  1891. if (can_clone) {
  1892. DRM_DEBUG_KMS("can clone using 1024x768\n");
  1893. return true;
  1894. }
  1895. DRM_INFO("kms: can't enable cloning when we probably wanted to.\n");
  1896. return false;
  1897. }
  1898. static int drm_get_tile_offsets(struct drm_fb_helper *fb_helper,
  1899. struct drm_display_mode **modes,
  1900. struct drm_fb_offset *offsets,
  1901. int idx,
  1902. int h_idx, int v_idx)
  1903. {
  1904. struct drm_fb_helper_connector *fb_helper_conn;
  1905. int i;
  1906. int hoffset = 0, voffset = 0;
  1907. drm_fb_helper_for_each_connector(fb_helper, i) {
  1908. fb_helper_conn = fb_helper->connector_info[i];
  1909. if (!fb_helper_conn->connector->has_tile)
  1910. continue;
  1911. if (!modes[i] && (h_idx || v_idx)) {
  1912. DRM_DEBUG_KMS("no modes for connector tiled %d %d\n", i,
  1913. fb_helper_conn->connector->base.id);
  1914. continue;
  1915. }
  1916. if (fb_helper_conn->connector->tile_h_loc < h_idx)
  1917. hoffset += modes[i]->hdisplay;
  1918. if (fb_helper_conn->connector->tile_v_loc < v_idx)
  1919. voffset += modes[i]->vdisplay;
  1920. }
  1921. offsets[idx].x = hoffset;
  1922. offsets[idx].y = voffset;
  1923. DRM_DEBUG_KMS("returned %d %d for %d %d\n", hoffset, voffset, h_idx, v_idx);
  1924. return 0;
  1925. }
  1926. static bool drm_target_preferred(struct drm_fb_helper *fb_helper,
  1927. struct drm_display_mode **modes,
  1928. struct drm_fb_offset *offsets,
  1929. bool *enabled, int width, int height)
  1930. {
  1931. struct drm_fb_helper_connector *fb_helper_conn;
  1932. const u64 mask = BIT_ULL(fb_helper->connector_count) - 1;
  1933. u64 conn_configured = 0;
  1934. int tile_pass = 0;
  1935. int i;
  1936. retry:
  1937. drm_fb_helper_for_each_connector(fb_helper, i) {
  1938. fb_helper_conn = fb_helper->connector_info[i];
  1939. if (conn_configured & BIT_ULL(i))
  1940. continue;
  1941. if (enabled[i] == false) {
  1942. conn_configured |= BIT_ULL(i);
  1943. continue;
  1944. }
  1945. /* first pass over all the untiled connectors */
  1946. if (tile_pass == 0 && fb_helper_conn->connector->has_tile)
  1947. continue;
  1948. if (tile_pass == 1) {
  1949. if (fb_helper_conn->connector->tile_h_loc != 0 ||
  1950. fb_helper_conn->connector->tile_v_loc != 0)
  1951. continue;
  1952. } else {
  1953. if (fb_helper_conn->connector->tile_h_loc != tile_pass - 1 &&
  1954. fb_helper_conn->connector->tile_v_loc != tile_pass - 1)
  1955. /* if this tile_pass doesn't cover any of the tiles - keep going */
  1956. continue;
  1957. /*
  1958. * find the tile offsets for this pass - need to find
  1959. * all tiles left and above
  1960. */
  1961. drm_get_tile_offsets(fb_helper, modes, offsets,
  1962. i, fb_helper_conn->connector->tile_h_loc, fb_helper_conn->connector->tile_v_loc);
  1963. }
  1964. DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
  1965. fb_helper_conn->connector->base.id);
  1966. /* got for command line mode first */
  1967. modes[i] = drm_pick_cmdline_mode(fb_helper_conn);
  1968. if (!modes[i]) {
  1969. DRM_DEBUG_KMS("looking for preferred mode on connector %d %d\n",
  1970. fb_helper_conn->connector->base.id, fb_helper_conn->connector->tile_group ? fb_helper_conn->connector->tile_group->id : 0);
  1971. modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height);
  1972. }
  1973. /* No preferred modes, pick one off the list */
  1974. if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) {
  1975. list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head)
  1976. break;
  1977. }
  1978. DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
  1979. "none");
  1980. conn_configured |= BIT_ULL(i);
  1981. }
  1982. if ((conn_configured & mask) != mask) {
  1983. tile_pass++;
  1984. goto retry;
  1985. }
  1986. return true;
  1987. }
  1988. static bool connector_has_possible_crtc(struct drm_connector *connector,
  1989. struct drm_crtc *crtc)
  1990. {
  1991. struct drm_encoder *encoder;
  1992. int i;
  1993. drm_connector_for_each_possible_encoder(connector, encoder, i) {
  1994. if (encoder->possible_crtcs & drm_crtc_mask(crtc))
  1995. return true;
  1996. }
  1997. return false;
  1998. }
  1999. static int drm_pick_crtcs(struct drm_fb_helper *fb_helper,
  2000. struct drm_fb_helper_crtc **best_crtcs,
  2001. struct drm_display_mode **modes,
  2002. int n, int width, int height)
  2003. {
  2004. int c, o;
  2005. struct drm_connector *connector;
  2006. int my_score, best_score, score;
  2007. struct drm_fb_helper_crtc **crtcs, *crtc;
  2008. struct drm_fb_helper_connector *fb_helper_conn;
  2009. if (n == fb_helper->connector_count)
  2010. return 0;
  2011. fb_helper_conn = fb_helper->connector_info[n];
  2012. connector = fb_helper_conn->connector;
  2013. best_crtcs[n] = NULL;
  2014. best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height);
  2015. if (modes[n] == NULL)
  2016. return best_score;
  2017. crtcs = kcalloc(fb_helper->connector_count,
  2018. sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
  2019. if (!crtcs)
  2020. return best_score;
  2021. my_score = 1;
  2022. if (connector->status == connector_status_connected)
  2023. my_score++;
  2024. if (drm_has_cmdline_mode(fb_helper_conn))
  2025. my_score++;
  2026. if (drm_has_preferred_mode(fb_helper_conn, width, height))
  2027. my_score++;
  2028. /*
  2029. * select a crtc for this connector and then attempt to configure
  2030. * remaining connectors
  2031. */
  2032. for (c = 0; c < fb_helper->crtc_count; c++) {
  2033. crtc = &fb_helper->crtc_info[c];
  2034. if (!connector_has_possible_crtc(connector,
  2035. crtc->mode_set.crtc))
  2036. continue;
  2037. for (o = 0; o < n; o++)
  2038. if (best_crtcs[o] == crtc)
  2039. break;
  2040. if (o < n) {
  2041. /* ignore cloning unless only a single crtc */
  2042. if (fb_helper->crtc_count > 1)
  2043. continue;
  2044. if (!drm_mode_equal(modes[o], modes[n]))
  2045. continue;
  2046. }
  2047. crtcs[n] = crtc;
  2048. memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *));
  2049. score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1,
  2050. width, height);
  2051. if (score > best_score) {
  2052. best_score = score;
  2053. memcpy(best_crtcs, crtcs,
  2054. fb_helper->connector_count *
  2055. sizeof(struct drm_fb_helper_crtc *));
  2056. }
  2057. }
  2058. kfree(crtcs);
  2059. return best_score;
  2060. }
  2061. /*
  2062. * This function checks if rotation is necessary because of panel orientation
  2063. * and if it is, if it is supported.
  2064. * If rotation is necessary and supported, its gets set in fb_crtc.rotation.
  2065. * If rotation is necessary but not supported, a DRM_MODE_ROTATE_* flag gets
  2066. * or-ed into fb_helper->sw_rotations. In drm_setup_crtcs_fb() we check if only
  2067. * one bit is set and then we set fb_info.fbcon_rotate_hint to make fbcon do
  2068. * the unsupported rotation.
  2069. */
  2070. static void drm_setup_crtc_rotation(struct drm_fb_helper *fb_helper,
  2071. struct drm_fb_helper_crtc *fb_crtc,
  2072. struct drm_connector *connector)
  2073. {
  2074. struct drm_plane *plane = fb_crtc->mode_set.crtc->primary;
  2075. uint64_t valid_mask = 0;
  2076. int i, rotation;
  2077. fb_crtc->rotation = DRM_MODE_ROTATE_0;
  2078. switch (connector->display_info.panel_orientation) {
  2079. case DRM_MODE_PANEL_ORIENTATION_BOTTOM_UP:
  2080. rotation = DRM_MODE_ROTATE_180;
  2081. break;
  2082. case DRM_MODE_PANEL_ORIENTATION_LEFT_UP:
  2083. rotation = DRM_MODE_ROTATE_90;
  2084. break;
  2085. case DRM_MODE_PANEL_ORIENTATION_RIGHT_UP:
  2086. rotation = DRM_MODE_ROTATE_270;
  2087. break;
  2088. default:
  2089. rotation = DRM_MODE_ROTATE_0;
  2090. }
  2091. /*
  2092. * TODO: support 90 / 270 degree hardware rotation,
  2093. * depending on the hardware this may require the framebuffer
  2094. * to be in a specific tiling format.
  2095. */
  2096. if (rotation != DRM_MODE_ROTATE_180 || !plane->rotation_property) {
  2097. fb_helper->sw_rotations |= rotation;
  2098. return;
  2099. }
  2100. for (i = 0; i < plane->rotation_property->num_values; i++)
  2101. valid_mask |= (1ULL << plane->rotation_property->values[i]);
  2102. if (!(rotation & valid_mask)) {
  2103. fb_helper->sw_rotations |= rotation;
  2104. return;
  2105. }
  2106. fb_crtc->rotation = rotation;
  2107. /* Rotating in hardware, fbcon should not rotate */
  2108. fb_helper->sw_rotations |= DRM_MODE_ROTATE_0;
  2109. }
  2110. static void drm_setup_crtcs(struct drm_fb_helper *fb_helper,
  2111. u32 width, u32 height)
  2112. {
  2113. struct drm_device *dev = fb_helper->dev;
  2114. struct drm_fb_helper_crtc **crtcs;
  2115. struct drm_display_mode **modes;
  2116. struct drm_fb_offset *offsets;
  2117. bool *enabled;
  2118. int i;
  2119. DRM_DEBUG_KMS("\n");
  2120. /* prevent concurrent modification of connector_count by hotplug */
  2121. lockdep_assert_held(&fb_helper->lock);
  2122. crtcs = kcalloc(fb_helper->connector_count,
  2123. sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
  2124. modes = kcalloc(fb_helper->connector_count,
  2125. sizeof(struct drm_display_mode *), GFP_KERNEL);
  2126. offsets = kcalloc(fb_helper->connector_count,
  2127. sizeof(struct drm_fb_offset), GFP_KERNEL);
  2128. enabled = kcalloc(fb_helper->connector_count,
  2129. sizeof(bool), GFP_KERNEL);
  2130. if (!crtcs || !modes || !enabled || !offsets) {
  2131. DRM_ERROR("Memory allocation failed\n");
  2132. goto out;
  2133. }
  2134. mutex_lock(&fb_helper->dev->mode_config.mutex);
  2135. if (drm_fb_helper_probe_connector_modes(fb_helper, width, height) == 0)
  2136. DRM_DEBUG_KMS("No connectors reported connected with modes\n");
  2137. drm_enable_connectors(fb_helper, enabled);
  2138. if (!(fb_helper->funcs->initial_config &&
  2139. fb_helper->funcs->initial_config(fb_helper, crtcs, modes,
  2140. offsets,
  2141. enabled, width, height))) {
  2142. memset(modes, 0, fb_helper->connector_count*sizeof(modes[0]));
  2143. memset(crtcs, 0, fb_helper->connector_count*sizeof(crtcs[0]));
  2144. memset(offsets, 0, fb_helper->connector_count*sizeof(offsets[0]));
  2145. if (!drm_target_cloned(fb_helper, modes, offsets,
  2146. enabled, width, height) &&
  2147. !drm_target_preferred(fb_helper, modes, offsets,
  2148. enabled, width, height))
  2149. DRM_ERROR("Unable to find initial modes\n");
  2150. DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n",
  2151. width, height);
  2152. drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height);
  2153. }
  2154. mutex_unlock(&fb_helper->dev->mode_config.mutex);
  2155. /* need to set the modesets up here for use later */
  2156. /* fill out the connector<->crtc mappings into the modesets */
  2157. for (i = 0; i < fb_helper->crtc_count; i++)
  2158. drm_fb_helper_modeset_release(fb_helper,
  2159. &fb_helper->crtc_info[i].mode_set);
  2160. fb_helper->sw_rotations = 0;
  2161. drm_fb_helper_for_each_connector(fb_helper, i) {
  2162. struct drm_display_mode *mode = modes[i];
  2163. struct drm_fb_helper_crtc *fb_crtc = crtcs[i];
  2164. struct drm_fb_offset *offset = &offsets[i];
  2165. if (mode && fb_crtc) {
  2166. struct drm_mode_set *modeset = &fb_crtc->mode_set;
  2167. struct drm_connector *connector =
  2168. fb_helper->connector_info[i]->connector;
  2169. DRM_DEBUG_KMS("desired mode %s set on crtc %d (%d,%d)\n",
  2170. mode->name, fb_crtc->mode_set.crtc->base.id, offset->x, offset->y);
  2171. fb_crtc->desired_mode = mode;
  2172. fb_crtc->x = offset->x;
  2173. fb_crtc->y = offset->y;
  2174. modeset->mode = drm_mode_duplicate(dev,
  2175. fb_crtc->desired_mode);
  2176. drm_connector_get(connector);
  2177. drm_setup_crtc_rotation(fb_helper, fb_crtc, connector);
  2178. modeset->connectors[modeset->num_connectors++] = connector;
  2179. modeset->x = offset->x;
  2180. modeset->y = offset->y;
  2181. }
  2182. }
  2183. out:
  2184. kfree(crtcs);
  2185. kfree(modes);
  2186. kfree(offsets);
  2187. kfree(enabled);
  2188. }
  2189. /*
  2190. * This is a continuation of drm_setup_crtcs() that sets up anything related
  2191. * to the framebuffer. During initialization, drm_setup_crtcs() is called before
  2192. * the framebuffer has been allocated (fb_helper->fb and fb_helper->fbdev).
  2193. * So, any setup that touches those fields needs to be done here instead of in
  2194. * drm_setup_crtcs().
  2195. */
  2196. static void drm_setup_crtcs_fb(struct drm_fb_helper *fb_helper)
  2197. {
  2198. struct fb_info *info = fb_helper->fbdev;
  2199. int i;
  2200. for (i = 0; i < fb_helper->crtc_count; i++)
  2201. if (fb_helper->crtc_info[i].mode_set.num_connectors)
  2202. fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb;
  2203. mutex_lock(&fb_helper->dev->mode_config.mutex);
  2204. drm_fb_helper_for_each_connector(fb_helper, i) {
  2205. struct drm_connector *connector =
  2206. fb_helper->connector_info[i]->connector;
  2207. /* use first connected connector for the physical dimensions */
  2208. if (connector->status == connector_status_connected) {
  2209. info->var.width = connector->display_info.width_mm;
  2210. info->var.height = connector->display_info.height_mm;
  2211. break;
  2212. }
  2213. }
  2214. mutex_unlock(&fb_helper->dev->mode_config.mutex);
  2215. switch (fb_helper->sw_rotations) {
  2216. case DRM_MODE_ROTATE_0:
  2217. info->fbcon_rotate_hint = FB_ROTATE_UR;
  2218. break;
  2219. case DRM_MODE_ROTATE_90:
  2220. info->fbcon_rotate_hint = FB_ROTATE_CCW;
  2221. break;
  2222. case DRM_MODE_ROTATE_180:
  2223. info->fbcon_rotate_hint = FB_ROTATE_UD;
  2224. break;
  2225. case DRM_MODE_ROTATE_270:
  2226. info->fbcon_rotate_hint = FB_ROTATE_CW;
  2227. break;
  2228. default:
  2229. /*
  2230. * Multiple bits are set / multiple rotations requested
  2231. * fbcon cannot handle separate rotation settings per
  2232. * output, so fallback to unrotated.
  2233. */
  2234. info->fbcon_rotate_hint = FB_ROTATE_UR;
  2235. }
  2236. }
  2237. /* Note: Drops fb_helper->lock before returning. */
  2238. static int
  2239. __drm_fb_helper_initial_config_and_unlock(struct drm_fb_helper *fb_helper,
  2240. int bpp_sel)
  2241. {
  2242. struct drm_device *dev = fb_helper->dev;
  2243. struct fb_info *info;
  2244. unsigned int width, height;
  2245. int ret;
  2246. width = dev->mode_config.max_width;
  2247. height = dev->mode_config.max_height;
  2248. drm_setup_crtcs(fb_helper, width, height);
  2249. ret = drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
  2250. if (ret < 0) {
  2251. if (ret == -EAGAIN) {
  2252. fb_helper->preferred_bpp = bpp_sel;
  2253. fb_helper->deferred_setup = true;
  2254. ret = 0;
  2255. }
  2256. mutex_unlock(&fb_helper->lock);
  2257. return ret;
  2258. }
  2259. drm_setup_crtcs_fb(fb_helper);
  2260. fb_helper->deferred_setup = false;
  2261. info = fb_helper->fbdev;
  2262. info->var.pixclock = 0;
  2263. /* Shamelessly allow physical address leaking to userspace */
  2264. #if IS_ENABLED(CONFIG_DRM_FBDEV_LEAK_PHYS_SMEM)
  2265. if (!drm_leak_fbdev_smem)
  2266. #endif
  2267. /* don't leak any physical addresses to userspace */
  2268. info->flags |= FBINFO_HIDE_SMEM_START;
  2269. /* Need to drop locks to avoid recursive deadlock in
  2270. * register_framebuffer. This is ok because the only thing left to do is
  2271. * register the fbdev emulation instance in kernel_fb_helper_list. */
  2272. mutex_unlock(&fb_helper->lock);
  2273. ret = register_framebuffer(info);
  2274. if (ret < 0)
  2275. return ret;
  2276. dev_info(dev->dev, "fb%d: %s frame buffer device\n",
  2277. info->node, info->fix.id);
  2278. mutex_lock(&kernel_fb_helper_lock);
  2279. if (list_empty(&kernel_fb_helper_list))
  2280. register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
  2281. list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
  2282. mutex_unlock(&kernel_fb_helper_lock);
  2283. return 0;
  2284. }
  2285. /**
  2286. * drm_fb_helper_initial_config - setup a sane initial connector configuration
  2287. * @fb_helper: fb_helper device struct
  2288. * @bpp_sel: bpp value to use for the framebuffer configuration
  2289. *
  2290. * Scans the CRTCs and connectors and tries to put together an initial setup.
  2291. * At the moment, this is a cloned configuration across all heads with
  2292. * a new framebuffer object as the backing store.
  2293. *
  2294. * Note that this also registers the fbdev and so allows userspace to call into
  2295. * the driver through the fbdev interfaces.
  2296. *
  2297. * This function will call down into the &drm_fb_helper_funcs.fb_probe callback
  2298. * to let the driver allocate and initialize the fbdev info structure and the
  2299. * drm framebuffer used to back the fbdev. drm_fb_helper_fill_var() and
  2300. * drm_fb_helper_fill_fix() are provided as helpers to setup simple default
  2301. * values for the fbdev info structure.
  2302. *
  2303. * HANG DEBUGGING:
  2304. *
  2305. * When you have fbcon support built-in or already loaded, this function will do
  2306. * a full modeset to setup the fbdev console. Due to locking misdesign in the
  2307. * VT/fbdev subsystem that entire modeset sequence has to be done while holding
  2308. * console_lock. Until console_unlock is called no dmesg lines will be sent out
  2309. * to consoles, not even serial console. This means when your driver crashes,
  2310. * you will see absolutely nothing else but a system stuck in this function,
  2311. * with no further output. Any kind of printk() you place within your own driver
  2312. * or in the drm core modeset code will also never show up.
  2313. *
  2314. * Standard debug practice is to run the fbcon setup without taking the
  2315. * console_lock as a hack, to be able to see backtraces and crashes on the
  2316. * serial line. This can be done by setting the fb.lockless_register_fb=1 kernel
  2317. * cmdline option.
  2318. *
  2319. * The other option is to just disable fbdev emulation since very likely the
  2320. * first modeset from userspace will crash in the same way, and is even easier
  2321. * to debug. This can be done by setting the drm_kms_helper.fbdev_emulation=0
  2322. * kernel cmdline option.
  2323. *
  2324. * RETURNS:
  2325. * Zero if everything went ok, nonzero otherwise.
  2326. */
  2327. int drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel)
  2328. {
  2329. int ret;
  2330. if (!drm_fbdev_emulation)
  2331. return 0;
  2332. mutex_lock(&fb_helper->lock);
  2333. ret = __drm_fb_helper_initial_config_and_unlock(fb_helper, bpp_sel);
  2334. return ret;
  2335. }
  2336. EXPORT_SYMBOL(drm_fb_helper_initial_config);
  2337. /**
  2338. * drm_fb_helper_hotplug_event - respond to a hotplug notification by
  2339. * probing all the outputs attached to the fb
  2340. * @fb_helper: driver-allocated fbdev helper, can be NULL
  2341. *
  2342. * Scan the connectors attached to the fb_helper and try to put together a
  2343. * setup after notification of a change in output configuration.
  2344. *
  2345. * Called at runtime, takes the mode config locks to be able to check/change the
  2346. * modeset configuration. Must be run from process context (which usually means
  2347. * either the output polling work or a work item launched from the driver's
  2348. * hotplug interrupt).
  2349. *
  2350. * Note that drivers may call this even before calling
  2351. * drm_fb_helper_initial_config but only after drm_fb_helper_init. This allows
  2352. * for a race-free fbcon setup and will make sure that the fbdev emulation will
  2353. * not miss any hotplug events.
  2354. *
  2355. * RETURNS:
  2356. * 0 on success and a non-zero error code otherwise.
  2357. */
  2358. int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper)
  2359. {
  2360. int err = 0;
  2361. if (!drm_fbdev_emulation || !fb_helper)
  2362. return 0;
  2363. mutex_lock(&fb_helper->lock);
  2364. if (fb_helper->deferred_setup) {
  2365. err = __drm_fb_helper_initial_config_and_unlock(fb_helper,
  2366. fb_helper->preferred_bpp);
  2367. return err;
  2368. }
  2369. if (!fb_helper->fb || !drm_fb_helper_is_bound(fb_helper)) {
  2370. fb_helper->delayed_hotplug = true;
  2371. mutex_unlock(&fb_helper->lock);
  2372. return err;
  2373. }
  2374. DRM_DEBUG_KMS("\n");
  2375. drm_setup_crtcs(fb_helper, fb_helper->fb->width, fb_helper->fb->height);
  2376. drm_setup_crtcs_fb(fb_helper);
  2377. mutex_unlock(&fb_helper->lock);
  2378. drm_fb_helper_set_par(fb_helper->fbdev);
  2379. return 0;
  2380. }
  2381. EXPORT_SYMBOL(drm_fb_helper_hotplug_event);
  2382. /**
  2383. * drm_fb_helper_fbdev_setup() - Setup fbdev emulation
  2384. * @dev: DRM device
  2385. * @fb_helper: fbdev helper structure to set up
  2386. * @funcs: fbdev helper functions
  2387. * @preferred_bpp: Preferred bits per pixel for the device.
  2388. * @dev->mode_config.preferred_depth is used if this is zero.
  2389. * @max_conn_count: Maximum number of connectors.
  2390. * @dev->mode_config.num_connector is used if this is zero.
  2391. *
  2392. * This function sets up fbdev emulation and registers fbdev for access by
  2393. * userspace. If all connectors are disconnected, setup is deferred to the next
  2394. * time drm_fb_helper_hotplug_event() is called.
  2395. * The caller must to provide a &drm_fb_helper_funcs->fb_probe callback
  2396. * function.
  2397. *
  2398. * Use drm_fb_helper_fbdev_teardown() to destroy the fbdev.
  2399. *
  2400. * See also: drm_fb_helper_initial_config(), drm_fbdev_generic_setup().
  2401. *
  2402. * Returns:
  2403. * Zero on success or negative error code on failure.
  2404. */
  2405. int drm_fb_helper_fbdev_setup(struct drm_device *dev,
  2406. struct drm_fb_helper *fb_helper,
  2407. const struct drm_fb_helper_funcs *funcs,
  2408. unsigned int preferred_bpp,
  2409. unsigned int max_conn_count)
  2410. {
  2411. int ret;
  2412. if (!preferred_bpp)
  2413. preferred_bpp = dev->mode_config.preferred_depth;
  2414. if (!preferred_bpp)
  2415. preferred_bpp = 32;
  2416. if (!max_conn_count)
  2417. max_conn_count = dev->mode_config.num_connector;
  2418. if (!max_conn_count) {
  2419. DRM_DEV_ERROR(dev->dev, "fbdev: No connectors\n");
  2420. return -EINVAL;
  2421. }
  2422. drm_fb_helper_prepare(dev, fb_helper, funcs);
  2423. ret = drm_fb_helper_init(dev, fb_helper, max_conn_count);
  2424. if (ret < 0) {
  2425. DRM_DEV_ERROR(dev->dev, "fbdev: Failed to initialize (ret=%d)\n", ret);
  2426. return ret;
  2427. }
  2428. ret = drm_fb_helper_single_add_all_connectors(fb_helper);
  2429. if (ret < 0) {
  2430. DRM_DEV_ERROR(dev->dev, "fbdev: Failed to add connectors (ret=%d)\n", ret);
  2431. goto err_drm_fb_helper_fini;
  2432. }
  2433. if (!drm_drv_uses_atomic_modeset(dev))
  2434. drm_helper_disable_unused_functions(dev);
  2435. ret = drm_fb_helper_initial_config(fb_helper, preferred_bpp);
  2436. if (ret < 0) {
  2437. DRM_DEV_ERROR(dev->dev, "fbdev: Failed to set configuration (ret=%d)\n", ret);
  2438. goto err_drm_fb_helper_fini;
  2439. }
  2440. return 0;
  2441. err_drm_fb_helper_fini:
  2442. drm_fb_helper_fini(fb_helper);
  2443. return ret;
  2444. }
  2445. EXPORT_SYMBOL(drm_fb_helper_fbdev_setup);
  2446. /**
  2447. * drm_fb_helper_fbdev_teardown - Tear down fbdev emulation
  2448. * @dev: DRM device
  2449. *
  2450. * This function unregisters fbdev if not already done and cleans up the
  2451. * associated resources including the &drm_framebuffer.
  2452. * The driver is responsible for freeing the &drm_fb_helper structure which is
  2453. * stored in &drm_device->fb_helper. Do note that this pointer has been cleared
  2454. * when this function returns.
  2455. *
  2456. * In order to support device removal/unplug while file handles are still open,
  2457. * drm_fb_helper_unregister_fbi() should be called on device removal and
  2458. * drm_fb_helper_fbdev_teardown() in the &drm_driver->release callback when
  2459. * file handles are closed.
  2460. */
  2461. void drm_fb_helper_fbdev_teardown(struct drm_device *dev)
  2462. {
  2463. struct drm_fb_helper *fb_helper = dev->fb_helper;
  2464. struct fb_ops *fbops = NULL;
  2465. if (!fb_helper)
  2466. return;
  2467. /* Unregister if it hasn't been done already */
  2468. if (fb_helper->fbdev && fb_helper->fbdev->dev)
  2469. drm_fb_helper_unregister_fbi(fb_helper);
  2470. if (fb_helper->fbdev && fb_helper->fbdev->fbdefio) {
  2471. fb_deferred_io_cleanup(fb_helper->fbdev);
  2472. kfree(fb_helper->fbdev->fbdefio);
  2473. fbops = fb_helper->fbdev->fbops;
  2474. }
  2475. drm_fb_helper_fini(fb_helper);
  2476. kfree(fbops);
  2477. if (fb_helper->fb)
  2478. drm_framebuffer_remove(fb_helper->fb);
  2479. }
  2480. EXPORT_SYMBOL(drm_fb_helper_fbdev_teardown);
  2481. /**
  2482. * drm_fb_helper_lastclose - DRM driver lastclose helper for fbdev emulation
  2483. * @dev: DRM device
  2484. *
  2485. * This function can be used as the &drm_driver->lastclose callback for drivers
  2486. * that only need to call drm_fb_helper_restore_fbdev_mode_unlocked().
  2487. */
  2488. void drm_fb_helper_lastclose(struct drm_device *dev)
  2489. {
  2490. drm_fb_helper_restore_fbdev_mode_unlocked(dev->fb_helper);
  2491. }
  2492. EXPORT_SYMBOL(drm_fb_helper_lastclose);
  2493. /**
  2494. * drm_fb_helper_output_poll_changed - DRM mode config \.output_poll_changed
  2495. * helper for fbdev emulation
  2496. * @dev: DRM device
  2497. *
  2498. * This function can be used as the
  2499. * &drm_mode_config_funcs.output_poll_changed callback for drivers that only
  2500. * need to call drm_fb_helper_hotplug_event().
  2501. */
  2502. void drm_fb_helper_output_poll_changed(struct drm_device *dev)
  2503. {
  2504. drm_fb_helper_hotplug_event(dev->fb_helper);
  2505. }
  2506. EXPORT_SYMBOL(drm_fb_helper_output_poll_changed);
  2507. /* @user: 1=userspace, 0=fbcon */
  2508. static int drm_fbdev_fb_open(struct fb_info *info, int user)
  2509. {
  2510. struct drm_fb_helper *fb_helper = info->par;
  2511. if (!try_module_get(fb_helper->dev->driver->fops->owner))
  2512. return -ENODEV;
  2513. return 0;
  2514. }
  2515. static int drm_fbdev_fb_release(struct fb_info *info, int user)
  2516. {
  2517. struct drm_fb_helper *fb_helper = info->par;
  2518. module_put(fb_helper->dev->driver->fops->owner);
  2519. return 0;
  2520. }
  2521. /*
  2522. * fb_ops.fb_destroy is called by the last put_fb_info() call at the end of
  2523. * unregister_framebuffer() or fb_release().
  2524. */
  2525. static void drm_fbdev_fb_destroy(struct fb_info *info)
  2526. {
  2527. struct drm_fb_helper *fb_helper = info->par;
  2528. struct fb_info *fbi = fb_helper->fbdev;
  2529. struct fb_ops *fbops = NULL;
  2530. void *shadow = NULL;
  2531. if (fbi->fbdefio) {
  2532. fb_deferred_io_cleanup(fbi);
  2533. shadow = fbi->screen_buffer;
  2534. fbops = fbi->fbops;
  2535. }
  2536. drm_fb_helper_fini(fb_helper);
  2537. if (shadow) {
  2538. vfree(shadow);
  2539. kfree(fbops);
  2540. }
  2541. drm_client_framebuffer_delete(fb_helper->buffer);
  2542. /*
  2543. * FIXME:
  2544. * Remove conditional when all CMA drivers have been moved over to using
  2545. * drm_fbdev_generic_setup().
  2546. */
  2547. if (fb_helper->client.funcs) {
  2548. drm_client_release(&fb_helper->client);
  2549. kfree(fb_helper);
  2550. }
  2551. }
  2552. static int drm_fbdev_fb_mmap(struct fb_info *info, struct vm_area_struct *vma)
  2553. {
  2554. struct drm_fb_helper *fb_helper = info->par;
  2555. if (fb_helper->dev->driver->gem_prime_mmap)
  2556. return fb_helper->dev->driver->gem_prime_mmap(fb_helper->buffer->gem, vma);
  2557. else
  2558. return -ENODEV;
  2559. }
  2560. static struct fb_ops drm_fbdev_fb_ops = {
  2561. .owner = THIS_MODULE,
  2562. DRM_FB_HELPER_DEFAULT_OPS,
  2563. .fb_open = drm_fbdev_fb_open,
  2564. .fb_release = drm_fbdev_fb_release,
  2565. .fb_destroy = drm_fbdev_fb_destroy,
  2566. .fb_mmap = drm_fbdev_fb_mmap,
  2567. .fb_read = drm_fb_helper_sys_read,
  2568. .fb_write = drm_fb_helper_sys_write,
  2569. .fb_fillrect = drm_fb_helper_sys_fillrect,
  2570. .fb_copyarea = drm_fb_helper_sys_copyarea,
  2571. .fb_imageblit = drm_fb_helper_sys_imageblit,
  2572. };
  2573. static struct fb_deferred_io drm_fbdev_defio = {
  2574. .delay = HZ / 20,
  2575. .deferred_io = drm_fb_helper_deferred_io,
  2576. };
  2577. /**
  2578. * drm_fb_helper_generic_probe - Generic fbdev emulation probe helper
  2579. * @fb_helper: fbdev helper structure
  2580. * @sizes: describes fbdev size and scanout surface size
  2581. *
  2582. * This function uses the client API to create a framebuffer backed by a dumb buffer.
  2583. *
  2584. * The _sys_ versions are used for &fb_ops.fb_read, fb_write, fb_fillrect,
  2585. * fb_copyarea, fb_imageblit.
  2586. *
  2587. * Returns:
  2588. * Zero on success or negative error code on failure.
  2589. */
  2590. int drm_fb_helper_generic_probe(struct drm_fb_helper *fb_helper,
  2591. struct drm_fb_helper_surface_size *sizes)
  2592. {
  2593. struct drm_client_dev *client = &fb_helper->client;
  2594. struct drm_client_buffer *buffer;
  2595. struct drm_framebuffer *fb;
  2596. struct fb_info *fbi;
  2597. u32 format;
  2598. int ret;
  2599. DRM_DEBUG_KMS("surface width(%d), height(%d) and bpp(%d)\n",
  2600. sizes->surface_width, sizes->surface_height,
  2601. sizes->surface_bpp);
  2602. format = drm_mode_legacy_fb_format(sizes->surface_bpp, sizes->surface_depth);
  2603. buffer = drm_client_framebuffer_create(client, sizes->surface_width,
  2604. sizes->surface_height, format);
  2605. if (IS_ERR(buffer))
  2606. return PTR_ERR(buffer);
  2607. fb_helper->buffer = buffer;
  2608. fb_helper->fb = buffer->fb;
  2609. fb = buffer->fb;
  2610. fbi = drm_fb_helper_alloc_fbi(fb_helper);
  2611. if (IS_ERR(fbi)) {
  2612. ret = PTR_ERR(fbi);
  2613. goto err_free_buffer;
  2614. }
  2615. fbi->par = fb_helper;
  2616. fbi->fbops = &drm_fbdev_fb_ops;
  2617. fbi->screen_size = fb->height * fb->pitches[0];
  2618. fbi->fix.smem_len = fbi->screen_size;
  2619. fbi->screen_buffer = buffer->vaddr;
  2620. /* Shamelessly leak the physical address to user-space */
  2621. #if IS_ENABLED(CONFIG_DRM_FBDEV_LEAK_PHYS_SMEM)
  2622. if (drm_leak_fbdev_smem && fbi->fix.smem_start == 0)
  2623. fbi->fix.smem_start =
  2624. page_to_phys(virt_to_page(fbi->screen_buffer));
  2625. #endif
  2626. strcpy(fbi->fix.id, "DRM emulated");
  2627. drm_fb_helper_fill_fix(fbi, fb->pitches[0], fb->format->depth);
  2628. drm_fb_helper_fill_var(fbi, fb_helper, sizes->fb_width, sizes->fb_height);
  2629. if (fb->funcs->dirty) {
  2630. struct fb_ops *fbops;
  2631. void *shadow;
  2632. /*
  2633. * fb_deferred_io_cleanup() clears &fbops->fb_mmap so a per
  2634. * instance version is necessary.
  2635. */
  2636. fbops = kzalloc(sizeof(*fbops), GFP_KERNEL);
  2637. shadow = vzalloc(fbi->screen_size);
  2638. if (!fbops || !shadow) {
  2639. kfree(fbops);
  2640. vfree(shadow);
  2641. ret = -ENOMEM;
  2642. goto err_fb_info_destroy;
  2643. }
  2644. *fbops = *fbi->fbops;
  2645. fbi->fbops = fbops;
  2646. fbi->screen_buffer = shadow;
  2647. fbi->fbdefio = &drm_fbdev_defio;
  2648. fb_deferred_io_init(fbi);
  2649. }
  2650. return 0;
  2651. err_fb_info_destroy:
  2652. drm_fb_helper_fini(fb_helper);
  2653. err_free_buffer:
  2654. drm_client_framebuffer_delete(buffer);
  2655. return ret;
  2656. }
  2657. EXPORT_SYMBOL(drm_fb_helper_generic_probe);
  2658. static const struct drm_fb_helper_funcs drm_fb_helper_generic_funcs = {
  2659. .fb_probe = drm_fb_helper_generic_probe,
  2660. };
  2661. static void drm_fbdev_client_unregister(struct drm_client_dev *client)
  2662. {
  2663. struct drm_fb_helper *fb_helper = drm_fb_helper_from_client(client);
  2664. if (fb_helper->fbdev) {
  2665. drm_fb_helper_unregister_fbi(fb_helper);
  2666. /* drm_fbdev_fb_destroy() takes care of cleanup */
  2667. return;
  2668. }
  2669. /* Did drm_fb_helper_fbdev_setup() run? */
  2670. if (fb_helper->dev)
  2671. drm_fb_helper_fini(fb_helper);
  2672. drm_client_release(client);
  2673. kfree(fb_helper);
  2674. }
  2675. static int drm_fbdev_client_restore(struct drm_client_dev *client)
  2676. {
  2677. struct drm_fb_helper *fb_helper = drm_fb_helper_from_client(client);
  2678. drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper);
  2679. return 0;
  2680. }
  2681. static int drm_fbdev_client_hotplug(struct drm_client_dev *client)
  2682. {
  2683. struct drm_fb_helper *fb_helper = drm_fb_helper_from_client(client);
  2684. struct drm_device *dev = client->dev;
  2685. int ret;
  2686. /* If drm_fb_helper_fbdev_setup() failed, we only try once */
  2687. if (!fb_helper->dev && fb_helper->funcs)
  2688. return 0;
  2689. if (dev->fb_helper)
  2690. return drm_fb_helper_hotplug_event(dev->fb_helper);
  2691. if (!dev->mode_config.num_connector) {
  2692. DRM_DEV_DEBUG(dev->dev, "No connectors found, will not create framebuffer!\n");
  2693. return 0;
  2694. }
  2695. ret = drm_fb_helper_fbdev_setup(dev, fb_helper, &drm_fb_helper_generic_funcs,
  2696. fb_helper->preferred_bpp, 0);
  2697. if (ret) {
  2698. fb_helper->dev = NULL;
  2699. fb_helper->fbdev = NULL;
  2700. return ret;
  2701. }
  2702. return 0;
  2703. }
  2704. static const struct drm_client_funcs drm_fbdev_client_funcs = {
  2705. .owner = THIS_MODULE,
  2706. .unregister = drm_fbdev_client_unregister,
  2707. .restore = drm_fbdev_client_restore,
  2708. .hotplug = drm_fbdev_client_hotplug,
  2709. };
  2710. /**
  2711. * drm_fbdev_generic_setup() - Setup generic fbdev emulation
  2712. * @dev: DRM device
  2713. * @preferred_bpp: Preferred bits per pixel for the device.
  2714. * @dev->mode_config.preferred_depth is used if this is zero.
  2715. *
  2716. * This function sets up generic fbdev emulation for drivers that supports
  2717. * dumb buffers with a virtual address and that can be mmap'ed. If the driver
  2718. * does not support these functions, it could use drm_fb_helper_fbdev_setup().
  2719. *
  2720. * Restore, hotplug events and teardown are all taken care of. Drivers that do
  2721. * suspend/resume need to call drm_fb_helper_set_suspend_unlocked() themselves.
  2722. * Simple drivers might use drm_mode_config_helper_suspend().
  2723. *
  2724. * Drivers that set the dirty callback on their framebuffer will get a shadow
  2725. * fbdev buffer that is blitted onto the real buffer. This is done in order to
  2726. * make deferred I/O work with all kinds of buffers.
  2727. *
  2728. * This function is safe to call even when there are no connectors present.
  2729. * Setup will be retried on the next hotplug event.
  2730. *
  2731. * The fbdev is destroyed by drm_dev_unregister().
  2732. *
  2733. * Returns:
  2734. * Zero on success or negative error code on failure.
  2735. */
  2736. int drm_fbdev_generic_setup(struct drm_device *dev, unsigned int preferred_bpp)
  2737. {
  2738. struct drm_fb_helper *fb_helper;
  2739. int ret;
  2740. WARN(dev->fb_helper, "fb_helper is already set!\n");
  2741. if (!drm_fbdev_emulation)
  2742. return 0;
  2743. fb_helper = kzalloc(sizeof(*fb_helper), GFP_KERNEL);
  2744. if (!fb_helper)
  2745. return -ENOMEM;
  2746. ret = drm_client_init(dev, &fb_helper->client, "fbdev", &drm_fbdev_client_funcs);
  2747. if (ret) {
  2748. kfree(fb_helper);
  2749. DRM_DEV_ERROR(dev->dev, "Failed to register client: %d\n", ret);
  2750. return ret;
  2751. }
  2752. drm_client_add(&fb_helper->client);
  2753. fb_helper->preferred_bpp = preferred_bpp;
  2754. ret = drm_fbdev_client_hotplug(&fb_helper->client);
  2755. if (ret)
  2756. DRM_DEV_DEBUG(dev->dev, "client hotplug ret=%d\n", ret);
  2757. return 0;
  2758. }
  2759. EXPORT_SYMBOL(drm_fbdev_generic_setup);
  2760. /* The Kconfig DRM_KMS_HELPER selects FRAMEBUFFER_CONSOLE (if !EXPERT)
  2761. * but the module doesn't depend on any fb console symbols. At least
  2762. * attempt to load fbcon to avoid leaving the system without a usable console.
  2763. */
  2764. int __init drm_fb_helper_modinit(void)
  2765. {
  2766. #if defined(CONFIG_FRAMEBUFFER_CONSOLE_MODULE) && !defined(CONFIG_EXPERT)
  2767. const char name[] = "fbcon";
  2768. struct module *fbcon;
  2769. mutex_lock(&module_mutex);
  2770. fbcon = find_module(name);
  2771. mutex_unlock(&module_mutex);
  2772. if (!fbcon)
  2773. request_module_nowait(name);
  2774. #endif
  2775. return 0;
  2776. }
  2777. EXPORT_SYMBOL(drm_fb_helper_modinit);