drm_crtc.c 104 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086
  1. /*
  2. * Copyright (c) 2006-2008 Intel Corporation
  3. * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
  4. * Copyright (c) 2008 Red Hat Inc.
  5. *
  6. * DRM core CRTC related 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. * Keith Packard
  28. * Eric Anholt <eric@anholt.net>
  29. * Dave Airlie <airlied@linux.ie>
  30. * Jesse Barnes <jesse.barnes@intel.com>
  31. */
  32. #include <linux/ctype.h>
  33. #include <linux/list.h>
  34. #include <linux/slab.h>
  35. #include <linux/export.h>
  36. #include <drm/drmP.h>
  37. #include <drm/drm_crtc.h>
  38. #include <drm/drm_edid.h>
  39. #include <drm/drm_fourcc.h>
  40. /**
  41. * drm_modeset_lock_all - take all modeset locks
  42. * @dev: drm device
  43. *
  44. * This function takes all modeset locks, suitable where a more fine-grained
  45. * scheme isn't (yet) implemented.
  46. */
  47. void drm_modeset_lock_all(struct drm_device *dev)
  48. {
  49. struct drm_crtc *crtc;
  50. mutex_lock(&dev->mode_config.mutex);
  51. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
  52. mutex_lock_nest_lock(&crtc->mutex, &dev->mode_config.mutex);
  53. }
  54. EXPORT_SYMBOL(drm_modeset_lock_all);
  55. /**
  56. * drm_modeset_unlock_all - drop all modeset locks
  57. * @dev: device
  58. */
  59. void drm_modeset_unlock_all(struct drm_device *dev)
  60. {
  61. struct drm_crtc *crtc;
  62. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
  63. mutex_unlock(&crtc->mutex);
  64. mutex_unlock(&dev->mode_config.mutex);
  65. }
  66. EXPORT_SYMBOL(drm_modeset_unlock_all);
  67. /**
  68. * drm_warn_on_modeset_not_all_locked - check that all modeset locks are locked
  69. * @dev: device
  70. */
  71. void drm_warn_on_modeset_not_all_locked(struct drm_device *dev)
  72. {
  73. struct drm_crtc *crtc;
  74. /* Locking is currently fubar in the panic handler. */
  75. if (oops_in_progress)
  76. return;
  77. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
  78. WARN_ON(!mutex_is_locked(&crtc->mutex));
  79. WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
  80. }
  81. EXPORT_SYMBOL(drm_warn_on_modeset_not_all_locked);
  82. /* Avoid boilerplate. I'm tired of typing. */
  83. #define DRM_ENUM_NAME_FN(fnname, list) \
  84. const char *fnname(int val) \
  85. { \
  86. int i; \
  87. for (i = 0; i < ARRAY_SIZE(list); i++) { \
  88. if (list[i].type == val) \
  89. return list[i].name; \
  90. } \
  91. return "(unknown)"; \
  92. }
  93. /*
  94. * Global properties
  95. */
  96. static const struct drm_prop_enum_list drm_dpms_enum_list[] =
  97. { { DRM_MODE_DPMS_ON, "On" },
  98. { DRM_MODE_DPMS_STANDBY, "Standby" },
  99. { DRM_MODE_DPMS_SUSPEND, "Suspend" },
  100. { DRM_MODE_DPMS_OFF, "Off" }
  101. };
  102. DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
  103. /*
  104. * Optional properties
  105. */
  106. static const struct drm_prop_enum_list drm_scaling_mode_enum_list[] =
  107. {
  108. { DRM_MODE_SCALE_NONE, "None" },
  109. { DRM_MODE_SCALE_FULLSCREEN, "Full" },
  110. { DRM_MODE_SCALE_CENTER, "Center" },
  111. { DRM_MODE_SCALE_ASPECT, "Full aspect" },
  112. };
  113. /*
  114. * Non-global properties, but "required" for certain connectors.
  115. */
  116. static const struct drm_prop_enum_list drm_dvi_i_select_enum_list[] =
  117. {
  118. { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
  119. { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
  120. { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
  121. };
  122. DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
  123. static const struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] =
  124. {
  125. { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
  126. { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
  127. { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
  128. };
  129. DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
  130. drm_dvi_i_subconnector_enum_list)
  131. static const struct drm_prop_enum_list drm_tv_select_enum_list[] =
  132. {
  133. { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
  134. { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
  135. { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
  136. { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
  137. { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
  138. };
  139. DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
  140. static const struct drm_prop_enum_list drm_tv_subconnector_enum_list[] =
  141. {
  142. { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
  143. { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
  144. { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
  145. { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
  146. { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
  147. };
  148. DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
  149. drm_tv_subconnector_enum_list)
  150. static const struct drm_prop_enum_list drm_dirty_info_enum_list[] = {
  151. { DRM_MODE_DIRTY_OFF, "Off" },
  152. { DRM_MODE_DIRTY_ON, "On" },
  153. { DRM_MODE_DIRTY_ANNOTATE, "Annotate" },
  154. };
  155. struct drm_conn_prop_enum_list {
  156. int type;
  157. const char *name;
  158. struct ida ida;
  159. };
  160. /*
  161. * Connector and encoder types.
  162. */
  163. static struct drm_conn_prop_enum_list drm_connector_enum_list[] =
  164. { { DRM_MODE_CONNECTOR_Unknown, "Unknown" },
  165. { DRM_MODE_CONNECTOR_VGA, "VGA" },
  166. { DRM_MODE_CONNECTOR_DVII, "DVI-I" },
  167. { DRM_MODE_CONNECTOR_DVID, "DVI-D" },
  168. { DRM_MODE_CONNECTOR_DVIA, "DVI-A" },
  169. { DRM_MODE_CONNECTOR_Composite, "Composite" },
  170. { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO" },
  171. { DRM_MODE_CONNECTOR_LVDS, "LVDS" },
  172. { DRM_MODE_CONNECTOR_Component, "Component" },
  173. { DRM_MODE_CONNECTOR_9PinDIN, "DIN" },
  174. { DRM_MODE_CONNECTOR_DisplayPort, "DP" },
  175. { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A" },
  176. { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B" },
  177. { DRM_MODE_CONNECTOR_TV, "TV" },
  178. { DRM_MODE_CONNECTOR_eDP, "eDP" },
  179. { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual" },
  180. { DRM_MODE_CONNECTOR_DSI, "DSI" },
  181. };
  182. static const struct drm_prop_enum_list drm_encoder_enum_list[] =
  183. { { DRM_MODE_ENCODER_NONE, "None" },
  184. { DRM_MODE_ENCODER_DAC, "DAC" },
  185. { DRM_MODE_ENCODER_TMDS, "TMDS" },
  186. { DRM_MODE_ENCODER_LVDS, "LVDS" },
  187. { DRM_MODE_ENCODER_TVDAC, "TV" },
  188. { DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
  189. { DRM_MODE_ENCODER_DSI, "DSI" },
  190. };
  191. void drm_connector_ida_init(void)
  192. {
  193. int i;
  194. for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
  195. ida_init(&drm_connector_enum_list[i].ida);
  196. }
  197. void drm_connector_ida_destroy(void)
  198. {
  199. int i;
  200. for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
  201. ida_destroy(&drm_connector_enum_list[i].ida);
  202. }
  203. const char *drm_get_encoder_name(const struct drm_encoder *encoder)
  204. {
  205. static char buf[32];
  206. snprintf(buf, 32, "%s-%d",
  207. drm_encoder_enum_list[encoder->encoder_type].name,
  208. encoder->base.id);
  209. return buf;
  210. }
  211. EXPORT_SYMBOL(drm_get_encoder_name);
  212. const char *drm_get_connector_name(const struct drm_connector *connector)
  213. {
  214. static char buf[32];
  215. snprintf(buf, 32, "%s-%d",
  216. drm_connector_enum_list[connector->connector_type].name,
  217. connector->connector_type_id);
  218. return buf;
  219. }
  220. EXPORT_SYMBOL(drm_get_connector_name);
  221. const char *drm_get_connector_status_name(enum drm_connector_status status)
  222. {
  223. if (status == connector_status_connected)
  224. return "connected";
  225. else if (status == connector_status_disconnected)
  226. return "disconnected";
  227. else
  228. return "unknown";
  229. }
  230. EXPORT_SYMBOL(drm_get_connector_status_name);
  231. static char printable_char(int c)
  232. {
  233. return isascii(c) && isprint(c) ? c : '?';
  234. }
  235. const char *drm_get_format_name(uint32_t format)
  236. {
  237. static char buf[32];
  238. snprintf(buf, sizeof(buf),
  239. "%c%c%c%c %s-endian (0x%08x)",
  240. printable_char(format & 0xff),
  241. printable_char((format >> 8) & 0xff),
  242. printable_char((format >> 16) & 0xff),
  243. printable_char((format >> 24) & 0x7f),
  244. format & DRM_FORMAT_BIG_ENDIAN ? "big" : "little",
  245. format);
  246. return buf;
  247. }
  248. EXPORT_SYMBOL(drm_get_format_name);
  249. /**
  250. * drm_mode_object_get - allocate a new modeset identifier
  251. * @dev: DRM device
  252. * @obj: object pointer, used to generate unique ID
  253. * @obj_type: object type
  254. *
  255. * Create a unique identifier based on @ptr in @dev's identifier space. Used
  256. * for tracking modes, CRTCs and connectors.
  257. *
  258. * RETURNS:
  259. * New unique (relative to other objects in @dev) integer identifier for the
  260. * object.
  261. */
  262. static int drm_mode_object_get(struct drm_device *dev,
  263. struct drm_mode_object *obj, uint32_t obj_type)
  264. {
  265. int ret;
  266. mutex_lock(&dev->mode_config.idr_mutex);
  267. ret = idr_alloc(&dev->mode_config.crtc_idr, obj, 1, 0, GFP_KERNEL);
  268. if (ret >= 0) {
  269. /*
  270. * Set up the object linking under the protection of the idr
  271. * lock so that other users can't see inconsistent state.
  272. */
  273. obj->id = ret;
  274. obj->type = obj_type;
  275. }
  276. mutex_unlock(&dev->mode_config.idr_mutex);
  277. return ret < 0 ? ret : 0;
  278. }
  279. /**
  280. * drm_mode_object_put - free a modeset identifer
  281. * @dev: DRM device
  282. * @object: object to free
  283. *
  284. * Free @id from @dev's unique identifier pool.
  285. */
  286. static void drm_mode_object_put(struct drm_device *dev,
  287. struct drm_mode_object *object)
  288. {
  289. mutex_lock(&dev->mode_config.idr_mutex);
  290. idr_remove(&dev->mode_config.crtc_idr, object->id);
  291. mutex_unlock(&dev->mode_config.idr_mutex);
  292. }
  293. /**
  294. * drm_mode_object_find - look up a drm object with static lifetime
  295. * @dev: drm device
  296. * @id: id of the mode object
  297. * @type: type of the mode object
  298. *
  299. * Note that framebuffers cannot be looked up with this functions - since those
  300. * are reference counted, they need special treatment.
  301. */
  302. struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
  303. uint32_t id, uint32_t type)
  304. {
  305. struct drm_mode_object *obj = NULL;
  306. /* Framebuffers are reference counted and need their own lookup
  307. * function.*/
  308. WARN_ON(type == DRM_MODE_OBJECT_FB);
  309. mutex_lock(&dev->mode_config.idr_mutex);
  310. obj = idr_find(&dev->mode_config.crtc_idr, id);
  311. if (!obj || (obj->type != type) || (obj->id != id))
  312. obj = NULL;
  313. mutex_unlock(&dev->mode_config.idr_mutex);
  314. return obj;
  315. }
  316. EXPORT_SYMBOL(drm_mode_object_find);
  317. /**
  318. * drm_framebuffer_init - initialize a framebuffer
  319. * @dev: DRM device
  320. * @fb: framebuffer to be initialized
  321. * @funcs: ... with these functions
  322. *
  323. * Allocates an ID for the framebuffer's parent mode object, sets its mode
  324. * functions & device file and adds it to the master fd list.
  325. *
  326. * IMPORTANT:
  327. * This functions publishes the fb and makes it available for concurrent access
  328. * by other users. Which means by this point the fb _must_ be fully set up -
  329. * since all the fb attributes are invariant over its lifetime, no further
  330. * locking but only correct reference counting is required.
  331. *
  332. * RETURNS:
  333. * Zero on success, error code on failure.
  334. */
  335. int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
  336. const struct drm_framebuffer_funcs *funcs)
  337. {
  338. int ret;
  339. mutex_lock(&dev->mode_config.fb_lock);
  340. kref_init(&fb->refcount);
  341. INIT_LIST_HEAD(&fb->filp_head);
  342. fb->dev = dev;
  343. fb->funcs = funcs;
  344. ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
  345. if (ret)
  346. goto out;
  347. /* Grab the idr reference. */
  348. drm_framebuffer_reference(fb);
  349. dev->mode_config.num_fb++;
  350. list_add(&fb->head, &dev->mode_config.fb_list);
  351. out:
  352. mutex_unlock(&dev->mode_config.fb_lock);
  353. return 0;
  354. }
  355. EXPORT_SYMBOL(drm_framebuffer_init);
  356. static void drm_framebuffer_free(struct kref *kref)
  357. {
  358. struct drm_framebuffer *fb =
  359. container_of(kref, struct drm_framebuffer, refcount);
  360. fb->funcs->destroy(fb);
  361. }
  362. static struct drm_framebuffer *__drm_framebuffer_lookup(struct drm_device *dev,
  363. uint32_t id)
  364. {
  365. struct drm_mode_object *obj = NULL;
  366. struct drm_framebuffer *fb;
  367. mutex_lock(&dev->mode_config.idr_mutex);
  368. obj = idr_find(&dev->mode_config.crtc_idr, id);
  369. if (!obj || (obj->type != DRM_MODE_OBJECT_FB) || (obj->id != id))
  370. fb = NULL;
  371. else
  372. fb = obj_to_fb(obj);
  373. mutex_unlock(&dev->mode_config.idr_mutex);
  374. return fb;
  375. }
  376. /**
  377. * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
  378. * @dev: drm device
  379. * @id: id of the fb object
  380. *
  381. * If successful, this grabs an additional reference to the framebuffer -
  382. * callers need to make sure to eventually unreference the returned framebuffer
  383. * again.
  384. */
  385. struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
  386. uint32_t id)
  387. {
  388. struct drm_framebuffer *fb;
  389. mutex_lock(&dev->mode_config.fb_lock);
  390. fb = __drm_framebuffer_lookup(dev, id);
  391. if (fb)
  392. drm_framebuffer_reference(fb);
  393. mutex_unlock(&dev->mode_config.fb_lock);
  394. return fb;
  395. }
  396. EXPORT_SYMBOL(drm_framebuffer_lookup);
  397. /**
  398. * drm_framebuffer_unreference - unref a framebuffer
  399. * @fb: framebuffer to unref
  400. *
  401. * This functions decrements the fb's refcount and frees it if it drops to zero.
  402. */
  403. void drm_framebuffer_unreference(struct drm_framebuffer *fb)
  404. {
  405. DRM_DEBUG("FB ID: %d\n", fb->base.id);
  406. kref_put(&fb->refcount, drm_framebuffer_free);
  407. }
  408. EXPORT_SYMBOL(drm_framebuffer_unreference);
  409. /**
  410. * drm_framebuffer_reference - incr the fb refcnt
  411. * @fb: framebuffer
  412. */
  413. void drm_framebuffer_reference(struct drm_framebuffer *fb)
  414. {
  415. DRM_DEBUG("FB ID: %d\n", fb->base.id);
  416. kref_get(&fb->refcount);
  417. }
  418. EXPORT_SYMBOL(drm_framebuffer_reference);
  419. static void drm_framebuffer_free_bug(struct kref *kref)
  420. {
  421. BUG();
  422. }
  423. static void __drm_framebuffer_unreference(struct drm_framebuffer *fb)
  424. {
  425. DRM_DEBUG("FB ID: %d\n", fb->base.id);
  426. kref_put(&fb->refcount, drm_framebuffer_free_bug);
  427. }
  428. /* dev->mode_config.fb_lock must be held! */
  429. static void __drm_framebuffer_unregister(struct drm_device *dev,
  430. struct drm_framebuffer *fb)
  431. {
  432. mutex_lock(&dev->mode_config.idr_mutex);
  433. idr_remove(&dev->mode_config.crtc_idr, fb->base.id);
  434. mutex_unlock(&dev->mode_config.idr_mutex);
  435. fb->base.id = 0;
  436. __drm_framebuffer_unreference(fb);
  437. }
  438. /**
  439. * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
  440. * @fb: fb to unregister
  441. *
  442. * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
  443. * those used for fbdev. Note that the caller must hold a reference of it's own,
  444. * i.e. the object may not be destroyed through this call (since it'll lead to a
  445. * locking inversion).
  446. */
  447. void drm_framebuffer_unregister_private(struct drm_framebuffer *fb)
  448. {
  449. struct drm_device *dev = fb->dev;
  450. mutex_lock(&dev->mode_config.fb_lock);
  451. /* Mark fb as reaped and drop idr ref. */
  452. __drm_framebuffer_unregister(dev, fb);
  453. mutex_unlock(&dev->mode_config.fb_lock);
  454. }
  455. EXPORT_SYMBOL(drm_framebuffer_unregister_private);
  456. /**
  457. * drm_framebuffer_cleanup - remove a framebuffer object
  458. * @fb: framebuffer to remove
  459. *
  460. * Cleanup references to a user-created framebuffer. This function is intended
  461. * to be used from the drivers ->destroy callback.
  462. *
  463. * Note that this function does not remove the fb from active usuage - if it is
  464. * still used anywhere, hilarity can ensue since userspace could call getfb on
  465. * the id and get back -EINVAL. Obviously no concern at driver unload time.
  466. *
  467. * Also, the framebuffer will not be removed from the lookup idr - for
  468. * user-created framebuffers this will happen in in the rmfb ioctl. For
  469. * driver-private objects (e.g. for fbdev) drivers need to explicitly call
  470. * drm_framebuffer_unregister_private.
  471. */
  472. void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
  473. {
  474. struct drm_device *dev = fb->dev;
  475. mutex_lock(&dev->mode_config.fb_lock);
  476. list_del(&fb->head);
  477. dev->mode_config.num_fb--;
  478. mutex_unlock(&dev->mode_config.fb_lock);
  479. }
  480. EXPORT_SYMBOL(drm_framebuffer_cleanup);
  481. /**
  482. * drm_framebuffer_remove - remove and unreference a framebuffer object
  483. * @fb: framebuffer to remove
  484. *
  485. * Scans all the CRTCs and planes in @dev's mode_config. If they're
  486. * using @fb, removes it, setting it to NULL. Then drops the reference to the
  487. * passed-in framebuffer. Might take the modeset locks.
  488. *
  489. * Note that this function optimizes the cleanup away if the caller holds the
  490. * last reference to the framebuffer. It is also guaranteed to not take the
  491. * modeset locks in this case.
  492. */
  493. void drm_framebuffer_remove(struct drm_framebuffer *fb)
  494. {
  495. struct drm_device *dev = fb->dev;
  496. struct drm_crtc *crtc;
  497. struct drm_plane *plane;
  498. struct drm_mode_set set;
  499. int ret;
  500. WARN_ON(!list_empty(&fb->filp_head));
  501. /*
  502. * drm ABI mandates that we remove any deleted framebuffers from active
  503. * useage. But since most sane clients only remove framebuffers they no
  504. * longer need, try to optimize this away.
  505. *
  506. * Since we're holding a reference ourselves, observing a refcount of 1
  507. * means that we're the last holder and can skip it. Also, the refcount
  508. * can never increase from 1 again, so we don't need any barriers or
  509. * locks.
  510. *
  511. * Note that userspace could try to race with use and instate a new
  512. * usage _after_ we've cleared all current ones. End result will be an
  513. * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot
  514. * in this manner.
  515. */
  516. if (atomic_read(&fb->refcount.refcount) > 1) {
  517. drm_modeset_lock_all(dev);
  518. /* remove from any CRTC */
  519. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  520. if (crtc->fb == fb) {
  521. /* should turn off the crtc */
  522. memset(&set, 0, sizeof(struct drm_mode_set));
  523. set.crtc = crtc;
  524. set.fb = NULL;
  525. ret = drm_mode_set_config_internal(&set);
  526. if (ret)
  527. DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
  528. }
  529. }
  530. list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
  531. if (plane->fb == fb)
  532. drm_plane_force_disable(plane);
  533. }
  534. drm_modeset_unlock_all(dev);
  535. }
  536. drm_framebuffer_unreference(fb);
  537. }
  538. EXPORT_SYMBOL(drm_framebuffer_remove);
  539. /**
  540. * drm_crtc_init - Initialise a new CRTC object
  541. * @dev: DRM device
  542. * @crtc: CRTC object to init
  543. * @funcs: callbacks for the new CRTC
  544. *
  545. * Inits a new object created as base part of a driver crtc object.
  546. *
  547. * RETURNS:
  548. * Zero on success, error code on failure.
  549. */
  550. int drm_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
  551. const struct drm_crtc_funcs *funcs)
  552. {
  553. int ret;
  554. crtc->dev = dev;
  555. crtc->funcs = funcs;
  556. crtc->invert_dimensions = false;
  557. drm_modeset_lock_all(dev);
  558. mutex_init(&crtc->mutex);
  559. mutex_lock_nest_lock(&crtc->mutex, &dev->mode_config.mutex);
  560. ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
  561. if (ret)
  562. goto out;
  563. crtc->base.properties = &crtc->properties;
  564. list_add_tail(&crtc->head, &dev->mode_config.crtc_list);
  565. dev->mode_config.num_crtc++;
  566. out:
  567. drm_modeset_unlock_all(dev);
  568. return ret;
  569. }
  570. EXPORT_SYMBOL(drm_crtc_init);
  571. /**
  572. * drm_crtc_cleanup - Clean up the core crtc usage
  573. * @crtc: CRTC to cleanup
  574. *
  575. * This function cleans up @crtc and removes it from the DRM mode setting
  576. * core. Note that the function does *not* free the crtc structure itself,
  577. * this is the responsibility of the caller.
  578. */
  579. void drm_crtc_cleanup(struct drm_crtc *crtc)
  580. {
  581. struct drm_device *dev = crtc->dev;
  582. kfree(crtc->gamma_store);
  583. crtc->gamma_store = NULL;
  584. drm_mode_object_put(dev, &crtc->base);
  585. list_del(&crtc->head);
  586. dev->mode_config.num_crtc--;
  587. }
  588. EXPORT_SYMBOL(drm_crtc_cleanup);
  589. /**
  590. * drm_mode_probed_add - add a mode to a connector's probed mode list
  591. * @connector: connector the new mode
  592. * @mode: mode data
  593. *
  594. * Add @mode to @connector's mode list for later use.
  595. */
  596. void drm_mode_probed_add(struct drm_connector *connector,
  597. struct drm_display_mode *mode)
  598. {
  599. list_add_tail(&mode->head, &connector->probed_modes);
  600. }
  601. EXPORT_SYMBOL(drm_mode_probed_add);
  602. /*
  603. * drm_mode_remove - remove and free a mode
  604. * @connector: connector list to modify
  605. * @mode: mode to remove
  606. *
  607. * Remove @mode from @connector's mode list, then free it.
  608. */
  609. static void drm_mode_remove(struct drm_connector *connector,
  610. struct drm_display_mode *mode)
  611. {
  612. list_del(&mode->head);
  613. drm_mode_destroy(connector->dev, mode);
  614. }
  615. /**
  616. * drm_connector_init - Init a preallocated connector
  617. * @dev: DRM device
  618. * @connector: the connector to init
  619. * @funcs: callbacks for this connector
  620. * @connector_type: user visible type of the connector
  621. *
  622. * Initialises a preallocated connector. Connectors should be
  623. * subclassed as part of driver connector objects.
  624. *
  625. * RETURNS:
  626. * Zero on success, error code on failure.
  627. */
  628. int drm_connector_init(struct drm_device *dev,
  629. struct drm_connector *connector,
  630. const struct drm_connector_funcs *funcs,
  631. int connector_type)
  632. {
  633. int ret;
  634. struct ida *connector_ida =
  635. &drm_connector_enum_list[connector_type].ida;
  636. drm_modeset_lock_all(dev);
  637. ret = drm_mode_object_get(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR);
  638. if (ret)
  639. goto out;
  640. connector->base.properties = &connector->properties;
  641. connector->dev = dev;
  642. connector->funcs = funcs;
  643. connector->connector_type = connector_type;
  644. connector->connector_type_id =
  645. ida_simple_get(connector_ida, 1, 0, GFP_KERNEL);
  646. if (connector->connector_type_id < 0) {
  647. ret = connector->connector_type_id;
  648. drm_mode_object_put(dev, &connector->base);
  649. goto out;
  650. }
  651. INIT_LIST_HEAD(&connector->probed_modes);
  652. INIT_LIST_HEAD(&connector->modes);
  653. connector->edid_blob_ptr = NULL;
  654. connector->status = connector_status_unknown;
  655. list_add_tail(&connector->head, &dev->mode_config.connector_list);
  656. dev->mode_config.num_connector++;
  657. if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
  658. drm_object_attach_property(&connector->base,
  659. dev->mode_config.edid_property,
  660. 0);
  661. drm_object_attach_property(&connector->base,
  662. dev->mode_config.dpms_property, 0);
  663. out:
  664. drm_modeset_unlock_all(dev);
  665. return ret;
  666. }
  667. EXPORT_SYMBOL(drm_connector_init);
  668. /**
  669. * drm_connector_cleanup - cleans up an initialised connector
  670. * @connector: connector to cleanup
  671. *
  672. * Cleans up the connector but doesn't free the object.
  673. */
  674. void drm_connector_cleanup(struct drm_connector *connector)
  675. {
  676. struct drm_device *dev = connector->dev;
  677. struct drm_display_mode *mode, *t;
  678. list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
  679. drm_mode_remove(connector, mode);
  680. list_for_each_entry_safe(mode, t, &connector->modes, head)
  681. drm_mode_remove(connector, mode);
  682. ida_remove(&drm_connector_enum_list[connector->connector_type].ida,
  683. connector->connector_type_id);
  684. drm_mode_object_put(dev, &connector->base);
  685. list_del(&connector->head);
  686. dev->mode_config.num_connector--;
  687. }
  688. EXPORT_SYMBOL(drm_connector_cleanup);
  689. void drm_connector_unplug_all(struct drm_device *dev)
  690. {
  691. struct drm_connector *connector;
  692. /* taking the mode config mutex ends up in a clash with sysfs */
  693. list_for_each_entry(connector, &dev->mode_config.connector_list, head)
  694. drm_sysfs_connector_remove(connector);
  695. }
  696. EXPORT_SYMBOL(drm_connector_unplug_all);
  697. int drm_bridge_init(struct drm_device *dev, struct drm_bridge *bridge,
  698. const struct drm_bridge_funcs *funcs)
  699. {
  700. int ret;
  701. drm_modeset_lock_all(dev);
  702. ret = drm_mode_object_get(dev, &bridge->base, DRM_MODE_OBJECT_BRIDGE);
  703. if (ret)
  704. goto out;
  705. bridge->dev = dev;
  706. bridge->funcs = funcs;
  707. list_add_tail(&bridge->head, &dev->mode_config.bridge_list);
  708. dev->mode_config.num_bridge++;
  709. out:
  710. drm_modeset_unlock_all(dev);
  711. return ret;
  712. }
  713. EXPORT_SYMBOL(drm_bridge_init);
  714. void drm_bridge_cleanup(struct drm_bridge *bridge)
  715. {
  716. struct drm_device *dev = bridge->dev;
  717. drm_modeset_lock_all(dev);
  718. drm_mode_object_put(dev, &bridge->base);
  719. list_del(&bridge->head);
  720. dev->mode_config.num_bridge--;
  721. drm_modeset_unlock_all(dev);
  722. }
  723. EXPORT_SYMBOL(drm_bridge_cleanup);
  724. int drm_encoder_init(struct drm_device *dev,
  725. struct drm_encoder *encoder,
  726. const struct drm_encoder_funcs *funcs,
  727. int encoder_type)
  728. {
  729. int ret;
  730. drm_modeset_lock_all(dev);
  731. ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
  732. if (ret)
  733. goto out;
  734. encoder->dev = dev;
  735. encoder->encoder_type = encoder_type;
  736. encoder->funcs = funcs;
  737. list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
  738. dev->mode_config.num_encoder++;
  739. out:
  740. drm_modeset_unlock_all(dev);
  741. return ret;
  742. }
  743. EXPORT_SYMBOL(drm_encoder_init);
  744. void drm_encoder_cleanup(struct drm_encoder *encoder)
  745. {
  746. struct drm_device *dev = encoder->dev;
  747. drm_modeset_lock_all(dev);
  748. drm_mode_object_put(dev, &encoder->base);
  749. list_del(&encoder->head);
  750. dev->mode_config.num_encoder--;
  751. drm_modeset_unlock_all(dev);
  752. }
  753. EXPORT_SYMBOL(drm_encoder_cleanup);
  754. /**
  755. * drm_plane_init - Initialise a new plane object
  756. * @dev: DRM device
  757. * @plane: plane object to init
  758. * @possible_crtcs: bitmask of possible CRTCs
  759. * @funcs: callbacks for the new plane
  760. * @formats: array of supported formats (%DRM_FORMAT_*)
  761. * @format_count: number of elements in @formats
  762. * @priv: plane is private (hidden from userspace)?
  763. *
  764. * Inits a new object created as base part of a driver plane object.
  765. *
  766. * RETURNS:
  767. * Zero on success, error code on failure.
  768. */
  769. int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
  770. unsigned long possible_crtcs,
  771. const struct drm_plane_funcs *funcs,
  772. const uint32_t *formats, uint32_t format_count,
  773. bool priv)
  774. {
  775. int ret;
  776. drm_modeset_lock_all(dev);
  777. ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
  778. if (ret)
  779. goto out;
  780. plane->base.properties = &plane->properties;
  781. plane->dev = dev;
  782. plane->funcs = funcs;
  783. plane->format_types = kmalloc(sizeof(uint32_t) * format_count,
  784. GFP_KERNEL);
  785. if (!plane->format_types) {
  786. DRM_DEBUG_KMS("out of memory when allocating plane\n");
  787. drm_mode_object_put(dev, &plane->base);
  788. ret = -ENOMEM;
  789. goto out;
  790. }
  791. memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
  792. plane->format_count = format_count;
  793. plane->possible_crtcs = possible_crtcs;
  794. /* private planes are not exposed to userspace, but depending on
  795. * display hardware, might be convenient to allow sharing programming
  796. * for the scanout engine with the crtc implementation.
  797. */
  798. if (!priv) {
  799. list_add_tail(&plane->head, &dev->mode_config.plane_list);
  800. dev->mode_config.num_plane++;
  801. } else {
  802. INIT_LIST_HEAD(&plane->head);
  803. }
  804. out:
  805. drm_modeset_unlock_all(dev);
  806. return ret;
  807. }
  808. EXPORT_SYMBOL(drm_plane_init);
  809. /**
  810. * drm_plane_cleanup - Clean up the core plane usage
  811. * @plane: plane to cleanup
  812. *
  813. * This function cleans up @plane and removes it from the DRM mode setting
  814. * core. Note that the function does *not* free the plane structure itself,
  815. * this is the responsibility of the caller.
  816. */
  817. void drm_plane_cleanup(struct drm_plane *plane)
  818. {
  819. struct drm_device *dev = plane->dev;
  820. drm_modeset_lock_all(dev);
  821. kfree(plane->format_types);
  822. drm_mode_object_put(dev, &plane->base);
  823. /* if not added to a list, it must be a private plane */
  824. if (!list_empty(&plane->head)) {
  825. list_del(&plane->head);
  826. dev->mode_config.num_plane--;
  827. }
  828. drm_modeset_unlock_all(dev);
  829. }
  830. EXPORT_SYMBOL(drm_plane_cleanup);
  831. /**
  832. * drm_plane_force_disable - Forcibly disable a plane
  833. * @plane: plane to disable
  834. *
  835. * Forces the plane to be disabled.
  836. *
  837. * Used when the plane's current framebuffer is destroyed,
  838. * and when restoring fbdev mode.
  839. */
  840. void drm_plane_force_disable(struct drm_plane *plane)
  841. {
  842. int ret;
  843. if (!plane->fb)
  844. return;
  845. ret = plane->funcs->disable_plane(plane);
  846. if (ret)
  847. DRM_ERROR("failed to disable plane with busy fb\n");
  848. /* disconnect the plane from the fb and crtc: */
  849. __drm_framebuffer_unreference(plane->fb);
  850. plane->fb = NULL;
  851. plane->crtc = NULL;
  852. }
  853. EXPORT_SYMBOL(drm_plane_force_disable);
  854. /**
  855. * drm_mode_create - create a new display mode
  856. * @dev: DRM device
  857. *
  858. * Create a new drm_display_mode, give it an ID, and return it.
  859. *
  860. * RETURNS:
  861. * Pointer to new mode on success, NULL on error.
  862. */
  863. struct drm_display_mode *drm_mode_create(struct drm_device *dev)
  864. {
  865. struct drm_display_mode *nmode;
  866. nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
  867. if (!nmode)
  868. return NULL;
  869. if (drm_mode_object_get(dev, &nmode->base, DRM_MODE_OBJECT_MODE)) {
  870. kfree(nmode);
  871. return NULL;
  872. }
  873. return nmode;
  874. }
  875. EXPORT_SYMBOL(drm_mode_create);
  876. /**
  877. * drm_mode_destroy - remove a mode
  878. * @dev: DRM device
  879. * @mode: mode to remove
  880. *
  881. * Free @mode's unique identifier, then free it.
  882. */
  883. void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
  884. {
  885. if (!mode)
  886. return;
  887. drm_mode_object_put(dev, &mode->base);
  888. kfree(mode);
  889. }
  890. EXPORT_SYMBOL(drm_mode_destroy);
  891. static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
  892. {
  893. struct drm_property *edid;
  894. struct drm_property *dpms;
  895. /*
  896. * Standard properties (apply to all connectors)
  897. */
  898. edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
  899. DRM_MODE_PROP_IMMUTABLE,
  900. "EDID", 0);
  901. dev->mode_config.edid_property = edid;
  902. dpms = drm_property_create_enum(dev, 0,
  903. "DPMS", drm_dpms_enum_list,
  904. ARRAY_SIZE(drm_dpms_enum_list));
  905. dev->mode_config.dpms_property = dpms;
  906. return 0;
  907. }
  908. /**
  909. * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
  910. * @dev: DRM device
  911. *
  912. * Called by a driver the first time a DVI-I connector is made.
  913. */
  914. int drm_mode_create_dvi_i_properties(struct drm_device *dev)
  915. {
  916. struct drm_property *dvi_i_selector;
  917. struct drm_property *dvi_i_subconnector;
  918. if (dev->mode_config.dvi_i_select_subconnector_property)
  919. return 0;
  920. dvi_i_selector =
  921. drm_property_create_enum(dev, 0,
  922. "select subconnector",
  923. drm_dvi_i_select_enum_list,
  924. ARRAY_SIZE(drm_dvi_i_select_enum_list));
  925. dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
  926. dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
  927. "subconnector",
  928. drm_dvi_i_subconnector_enum_list,
  929. ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
  930. dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
  931. return 0;
  932. }
  933. EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
  934. /**
  935. * drm_create_tv_properties - create TV specific connector properties
  936. * @dev: DRM device
  937. * @num_modes: number of different TV formats (modes) supported
  938. * @modes: array of pointers to strings containing name of each format
  939. *
  940. * Called by a driver's TV initialization routine, this function creates
  941. * the TV specific connector properties for a given device. Caller is
  942. * responsible for allocating a list of format names and passing them to
  943. * this routine.
  944. */
  945. int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
  946. char *modes[])
  947. {
  948. struct drm_property *tv_selector;
  949. struct drm_property *tv_subconnector;
  950. int i;
  951. if (dev->mode_config.tv_select_subconnector_property)
  952. return 0;
  953. /*
  954. * Basic connector properties
  955. */
  956. tv_selector = drm_property_create_enum(dev, 0,
  957. "select subconnector",
  958. drm_tv_select_enum_list,
  959. ARRAY_SIZE(drm_tv_select_enum_list));
  960. dev->mode_config.tv_select_subconnector_property = tv_selector;
  961. tv_subconnector =
  962. drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
  963. "subconnector",
  964. drm_tv_subconnector_enum_list,
  965. ARRAY_SIZE(drm_tv_subconnector_enum_list));
  966. dev->mode_config.tv_subconnector_property = tv_subconnector;
  967. /*
  968. * Other, TV specific properties: margins & TV modes.
  969. */
  970. dev->mode_config.tv_left_margin_property =
  971. drm_property_create_range(dev, 0, "left margin", 0, 100);
  972. dev->mode_config.tv_right_margin_property =
  973. drm_property_create_range(dev, 0, "right margin", 0, 100);
  974. dev->mode_config.tv_top_margin_property =
  975. drm_property_create_range(dev, 0, "top margin", 0, 100);
  976. dev->mode_config.tv_bottom_margin_property =
  977. drm_property_create_range(dev, 0, "bottom margin", 0, 100);
  978. dev->mode_config.tv_mode_property =
  979. drm_property_create(dev, DRM_MODE_PROP_ENUM,
  980. "mode", num_modes);
  981. for (i = 0; i < num_modes; i++)
  982. drm_property_add_enum(dev->mode_config.tv_mode_property, i,
  983. i, modes[i]);
  984. dev->mode_config.tv_brightness_property =
  985. drm_property_create_range(dev, 0, "brightness", 0, 100);
  986. dev->mode_config.tv_contrast_property =
  987. drm_property_create_range(dev, 0, "contrast", 0, 100);
  988. dev->mode_config.tv_flicker_reduction_property =
  989. drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
  990. dev->mode_config.tv_overscan_property =
  991. drm_property_create_range(dev, 0, "overscan", 0, 100);
  992. dev->mode_config.tv_saturation_property =
  993. drm_property_create_range(dev, 0, "saturation", 0, 100);
  994. dev->mode_config.tv_hue_property =
  995. drm_property_create_range(dev, 0, "hue", 0, 100);
  996. return 0;
  997. }
  998. EXPORT_SYMBOL(drm_mode_create_tv_properties);
  999. /**
  1000. * drm_mode_create_scaling_mode_property - create scaling mode property
  1001. * @dev: DRM device
  1002. *
  1003. * Called by a driver the first time it's needed, must be attached to desired
  1004. * connectors.
  1005. */
  1006. int drm_mode_create_scaling_mode_property(struct drm_device *dev)
  1007. {
  1008. struct drm_property *scaling_mode;
  1009. if (dev->mode_config.scaling_mode_property)
  1010. return 0;
  1011. scaling_mode =
  1012. drm_property_create_enum(dev, 0, "scaling mode",
  1013. drm_scaling_mode_enum_list,
  1014. ARRAY_SIZE(drm_scaling_mode_enum_list));
  1015. dev->mode_config.scaling_mode_property = scaling_mode;
  1016. return 0;
  1017. }
  1018. EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
  1019. /**
  1020. * drm_mode_create_dirty_property - create dirty property
  1021. * @dev: DRM device
  1022. *
  1023. * Called by a driver the first time it's needed, must be attached to desired
  1024. * connectors.
  1025. */
  1026. int drm_mode_create_dirty_info_property(struct drm_device *dev)
  1027. {
  1028. struct drm_property *dirty_info;
  1029. if (dev->mode_config.dirty_info_property)
  1030. return 0;
  1031. dirty_info =
  1032. drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
  1033. "dirty",
  1034. drm_dirty_info_enum_list,
  1035. ARRAY_SIZE(drm_dirty_info_enum_list));
  1036. dev->mode_config.dirty_info_property = dirty_info;
  1037. return 0;
  1038. }
  1039. EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
  1040. static int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
  1041. {
  1042. uint32_t total_objects = 0;
  1043. total_objects += dev->mode_config.num_crtc;
  1044. total_objects += dev->mode_config.num_connector;
  1045. total_objects += dev->mode_config.num_encoder;
  1046. total_objects += dev->mode_config.num_bridge;
  1047. group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
  1048. if (!group->id_list)
  1049. return -ENOMEM;
  1050. group->num_crtcs = 0;
  1051. group->num_connectors = 0;
  1052. group->num_encoders = 0;
  1053. group->num_bridges = 0;
  1054. return 0;
  1055. }
  1056. int drm_mode_group_init_legacy_group(struct drm_device *dev,
  1057. struct drm_mode_group *group)
  1058. {
  1059. struct drm_crtc *crtc;
  1060. struct drm_encoder *encoder;
  1061. struct drm_connector *connector;
  1062. struct drm_bridge *bridge;
  1063. int ret;
  1064. if ((ret = drm_mode_group_init(dev, group)))
  1065. return ret;
  1066. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
  1067. group->id_list[group->num_crtcs++] = crtc->base.id;
  1068. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
  1069. group->id_list[group->num_crtcs + group->num_encoders++] =
  1070. encoder->base.id;
  1071. list_for_each_entry(connector, &dev->mode_config.connector_list, head)
  1072. group->id_list[group->num_crtcs + group->num_encoders +
  1073. group->num_connectors++] = connector->base.id;
  1074. list_for_each_entry(bridge, &dev->mode_config.bridge_list, head)
  1075. group->id_list[group->num_crtcs + group->num_encoders +
  1076. group->num_connectors + group->num_bridges++] =
  1077. bridge->base.id;
  1078. return 0;
  1079. }
  1080. EXPORT_SYMBOL(drm_mode_group_init_legacy_group);
  1081. /**
  1082. * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
  1083. * @out: drm_mode_modeinfo struct to return to the user
  1084. * @in: drm_display_mode to use
  1085. *
  1086. * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
  1087. * the user.
  1088. */
  1089. static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
  1090. const struct drm_display_mode *in)
  1091. {
  1092. WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
  1093. in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
  1094. in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
  1095. in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
  1096. in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
  1097. "timing values too large for mode info\n");
  1098. out->clock = in->clock;
  1099. out->hdisplay = in->hdisplay;
  1100. out->hsync_start = in->hsync_start;
  1101. out->hsync_end = in->hsync_end;
  1102. out->htotal = in->htotal;
  1103. out->hskew = in->hskew;
  1104. out->vdisplay = in->vdisplay;
  1105. out->vsync_start = in->vsync_start;
  1106. out->vsync_end = in->vsync_end;
  1107. out->vtotal = in->vtotal;
  1108. out->vscan = in->vscan;
  1109. out->vrefresh = in->vrefresh;
  1110. out->flags = in->flags;
  1111. out->type = in->type;
  1112. strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
  1113. out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
  1114. }
  1115. /**
  1116. * drm_crtc_convert_to_umode - convert a modeinfo into a drm_display_mode
  1117. * @out: drm_display_mode to return to the user
  1118. * @in: drm_mode_modeinfo to use
  1119. *
  1120. * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
  1121. * the caller.
  1122. *
  1123. * RETURNS:
  1124. * Zero on success, errno on failure.
  1125. */
  1126. static int drm_crtc_convert_umode(struct drm_display_mode *out,
  1127. const struct drm_mode_modeinfo *in)
  1128. {
  1129. if (in->clock > INT_MAX || in->vrefresh > INT_MAX)
  1130. return -ERANGE;
  1131. if ((in->flags & DRM_MODE_FLAG_3D_MASK) > DRM_MODE_FLAG_3D_MAX)
  1132. return -EINVAL;
  1133. out->clock = in->clock;
  1134. out->hdisplay = in->hdisplay;
  1135. out->hsync_start = in->hsync_start;
  1136. out->hsync_end = in->hsync_end;
  1137. out->htotal = in->htotal;
  1138. out->hskew = in->hskew;
  1139. out->vdisplay = in->vdisplay;
  1140. out->vsync_start = in->vsync_start;
  1141. out->vsync_end = in->vsync_end;
  1142. out->vtotal = in->vtotal;
  1143. out->vscan = in->vscan;
  1144. out->vrefresh = in->vrefresh;
  1145. out->flags = in->flags;
  1146. out->type = in->type;
  1147. strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
  1148. out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
  1149. return 0;
  1150. }
  1151. /**
  1152. * drm_mode_getresources - get graphics configuration
  1153. * @dev: drm device for the ioctl
  1154. * @data: data pointer for the ioctl
  1155. * @file_priv: drm file for the ioctl call
  1156. *
  1157. * Construct a set of configuration description structures and return
  1158. * them to the user, including CRTC, connector and framebuffer configuration.
  1159. *
  1160. * Called by the user via ioctl.
  1161. *
  1162. * RETURNS:
  1163. * Zero on success, errno on failure.
  1164. */
  1165. int drm_mode_getresources(struct drm_device *dev, void *data,
  1166. struct drm_file *file_priv)
  1167. {
  1168. struct drm_mode_card_res *card_res = data;
  1169. struct list_head *lh;
  1170. struct drm_framebuffer *fb;
  1171. struct drm_connector *connector;
  1172. struct drm_crtc *crtc;
  1173. struct drm_encoder *encoder;
  1174. int ret = 0;
  1175. int connector_count = 0;
  1176. int crtc_count = 0;
  1177. int fb_count = 0;
  1178. int encoder_count = 0;
  1179. int copied = 0, i;
  1180. uint32_t __user *fb_id;
  1181. uint32_t __user *crtc_id;
  1182. uint32_t __user *connector_id;
  1183. uint32_t __user *encoder_id;
  1184. struct drm_mode_group *mode_group;
  1185. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1186. return -EINVAL;
  1187. mutex_lock(&file_priv->fbs_lock);
  1188. /*
  1189. * For the non-control nodes we need to limit the list of resources
  1190. * by IDs in the group list for this node
  1191. */
  1192. list_for_each(lh, &file_priv->fbs)
  1193. fb_count++;
  1194. /* handle this in 4 parts */
  1195. /* FBs */
  1196. if (card_res->count_fbs >= fb_count) {
  1197. copied = 0;
  1198. fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
  1199. list_for_each_entry(fb, &file_priv->fbs, filp_head) {
  1200. if (put_user(fb->base.id, fb_id + copied)) {
  1201. mutex_unlock(&file_priv->fbs_lock);
  1202. return -EFAULT;
  1203. }
  1204. copied++;
  1205. }
  1206. }
  1207. card_res->count_fbs = fb_count;
  1208. mutex_unlock(&file_priv->fbs_lock);
  1209. drm_modeset_lock_all(dev);
  1210. mode_group = &file_priv->master->minor->mode_group;
  1211. if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
  1212. list_for_each(lh, &dev->mode_config.crtc_list)
  1213. crtc_count++;
  1214. list_for_each(lh, &dev->mode_config.connector_list)
  1215. connector_count++;
  1216. list_for_each(lh, &dev->mode_config.encoder_list)
  1217. encoder_count++;
  1218. } else {
  1219. crtc_count = mode_group->num_crtcs;
  1220. connector_count = mode_group->num_connectors;
  1221. encoder_count = mode_group->num_encoders;
  1222. }
  1223. card_res->max_height = dev->mode_config.max_height;
  1224. card_res->min_height = dev->mode_config.min_height;
  1225. card_res->max_width = dev->mode_config.max_width;
  1226. card_res->min_width = dev->mode_config.min_width;
  1227. /* CRTCs */
  1228. if (card_res->count_crtcs >= crtc_count) {
  1229. copied = 0;
  1230. crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
  1231. if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
  1232. list_for_each_entry(crtc, &dev->mode_config.crtc_list,
  1233. head) {
  1234. DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
  1235. if (put_user(crtc->base.id, crtc_id + copied)) {
  1236. ret = -EFAULT;
  1237. goto out;
  1238. }
  1239. copied++;
  1240. }
  1241. } else {
  1242. for (i = 0; i < mode_group->num_crtcs; i++) {
  1243. if (put_user(mode_group->id_list[i],
  1244. crtc_id + copied)) {
  1245. ret = -EFAULT;
  1246. goto out;
  1247. }
  1248. copied++;
  1249. }
  1250. }
  1251. }
  1252. card_res->count_crtcs = crtc_count;
  1253. /* Encoders */
  1254. if (card_res->count_encoders >= encoder_count) {
  1255. copied = 0;
  1256. encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
  1257. if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
  1258. list_for_each_entry(encoder,
  1259. &dev->mode_config.encoder_list,
  1260. head) {
  1261. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
  1262. drm_get_encoder_name(encoder));
  1263. if (put_user(encoder->base.id, encoder_id +
  1264. copied)) {
  1265. ret = -EFAULT;
  1266. goto out;
  1267. }
  1268. copied++;
  1269. }
  1270. } else {
  1271. for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
  1272. if (put_user(mode_group->id_list[i],
  1273. encoder_id + copied)) {
  1274. ret = -EFAULT;
  1275. goto out;
  1276. }
  1277. copied++;
  1278. }
  1279. }
  1280. }
  1281. card_res->count_encoders = encoder_count;
  1282. /* Connectors */
  1283. if (card_res->count_connectors >= connector_count) {
  1284. copied = 0;
  1285. connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
  1286. if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
  1287. list_for_each_entry(connector,
  1288. &dev->mode_config.connector_list,
  1289. head) {
  1290. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  1291. connector->base.id,
  1292. drm_get_connector_name(connector));
  1293. if (put_user(connector->base.id,
  1294. connector_id + copied)) {
  1295. ret = -EFAULT;
  1296. goto out;
  1297. }
  1298. copied++;
  1299. }
  1300. } else {
  1301. int start = mode_group->num_crtcs +
  1302. mode_group->num_encoders;
  1303. for (i = start; i < start + mode_group->num_connectors; i++) {
  1304. if (put_user(mode_group->id_list[i],
  1305. connector_id + copied)) {
  1306. ret = -EFAULT;
  1307. goto out;
  1308. }
  1309. copied++;
  1310. }
  1311. }
  1312. }
  1313. card_res->count_connectors = connector_count;
  1314. DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
  1315. card_res->count_connectors, card_res->count_encoders);
  1316. out:
  1317. drm_modeset_unlock_all(dev);
  1318. return ret;
  1319. }
  1320. /**
  1321. * drm_mode_getcrtc - get CRTC configuration
  1322. * @dev: drm device for the ioctl
  1323. * @data: data pointer for the ioctl
  1324. * @file_priv: drm file for the ioctl call
  1325. *
  1326. * Construct a CRTC configuration structure to return to the user.
  1327. *
  1328. * Called by the user via ioctl.
  1329. *
  1330. * RETURNS:
  1331. * Zero on success, errno on failure.
  1332. */
  1333. int drm_mode_getcrtc(struct drm_device *dev,
  1334. void *data, struct drm_file *file_priv)
  1335. {
  1336. struct drm_mode_crtc *crtc_resp = data;
  1337. struct drm_crtc *crtc;
  1338. struct drm_mode_object *obj;
  1339. int ret = 0;
  1340. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1341. return -EINVAL;
  1342. drm_modeset_lock_all(dev);
  1343. obj = drm_mode_object_find(dev, crtc_resp->crtc_id,
  1344. DRM_MODE_OBJECT_CRTC);
  1345. if (!obj) {
  1346. ret = -EINVAL;
  1347. goto out;
  1348. }
  1349. crtc = obj_to_crtc(obj);
  1350. crtc_resp->x = crtc->x;
  1351. crtc_resp->y = crtc->y;
  1352. crtc_resp->gamma_size = crtc->gamma_size;
  1353. if (crtc->fb)
  1354. crtc_resp->fb_id = crtc->fb->base.id;
  1355. else
  1356. crtc_resp->fb_id = 0;
  1357. if (crtc->enabled) {
  1358. drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
  1359. crtc_resp->mode_valid = 1;
  1360. } else {
  1361. crtc_resp->mode_valid = 0;
  1362. }
  1363. out:
  1364. drm_modeset_unlock_all(dev);
  1365. return ret;
  1366. }
  1367. static bool drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
  1368. const struct drm_file *file_priv)
  1369. {
  1370. /*
  1371. * If user-space hasn't configured the driver to expose the stereo 3D
  1372. * modes, don't expose them.
  1373. */
  1374. if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
  1375. return false;
  1376. return true;
  1377. }
  1378. /**
  1379. * drm_mode_getconnector - get connector configuration
  1380. * @dev: drm device for the ioctl
  1381. * @data: data pointer for the ioctl
  1382. * @file_priv: drm file for the ioctl call
  1383. *
  1384. * Construct a connector configuration structure to return to the user.
  1385. *
  1386. * Called by the user via ioctl.
  1387. *
  1388. * RETURNS:
  1389. * Zero on success, errno on failure.
  1390. */
  1391. int drm_mode_getconnector(struct drm_device *dev, void *data,
  1392. struct drm_file *file_priv)
  1393. {
  1394. struct drm_mode_get_connector *out_resp = data;
  1395. struct drm_mode_object *obj;
  1396. struct drm_connector *connector;
  1397. struct drm_display_mode *mode;
  1398. int mode_count = 0;
  1399. int props_count = 0;
  1400. int encoders_count = 0;
  1401. int ret = 0;
  1402. int copied = 0;
  1403. int i;
  1404. struct drm_mode_modeinfo u_mode;
  1405. struct drm_mode_modeinfo __user *mode_ptr;
  1406. uint32_t __user *prop_ptr;
  1407. uint64_t __user *prop_values;
  1408. uint32_t __user *encoder_ptr;
  1409. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1410. return -EINVAL;
  1411. memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
  1412. DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
  1413. mutex_lock(&dev->mode_config.mutex);
  1414. obj = drm_mode_object_find(dev, out_resp->connector_id,
  1415. DRM_MODE_OBJECT_CONNECTOR);
  1416. if (!obj) {
  1417. ret = -EINVAL;
  1418. goto out;
  1419. }
  1420. connector = obj_to_connector(obj);
  1421. props_count = connector->properties.count;
  1422. for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
  1423. if (connector->encoder_ids[i] != 0) {
  1424. encoders_count++;
  1425. }
  1426. }
  1427. if (out_resp->count_modes == 0) {
  1428. connector->funcs->fill_modes(connector,
  1429. dev->mode_config.max_width,
  1430. dev->mode_config.max_height);
  1431. }
  1432. /* delayed so we get modes regardless of pre-fill_modes state */
  1433. list_for_each_entry(mode, &connector->modes, head)
  1434. if (drm_mode_expose_to_userspace(mode, file_priv))
  1435. mode_count++;
  1436. out_resp->connector_id = connector->base.id;
  1437. out_resp->connector_type = connector->connector_type;
  1438. out_resp->connector_type_id = connector->connector_type_id;
  1439. out_resp->mm_width = connector->display_info.width_mm;
  1440. out_resp->mm_height = connector->display_info.height_mm;
  1441. out_resp->subpixel = connector->display_info.subpixel_order;
  1442. out_resp->connection = connector->status;
  1443. if (connector->encoder)
  1444. out_resp->encoder_id = connector->encoder->base.id;
  1445. else
  1446. out_resp->encoder_id = 0;
  1447. /*
  1448. * This ioctl is called twice, once to determine how much space is
  1449. * needed, and the 2nd time to fill it.
  1450. */
  1451. if ((out_resp->count_modes >= mode_count) && mode_count) {
  1452. copied = 0;
  1453. mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
  1454. list_for_each_entry(mode, &connector->modes, head) {
  1455. if (!drm_mode_expose_to_userspace(mode, file_priv))
  1456. continue;
  1457. drm_crtc_convert_to_umode(&u_mode, mode);
  1458. if (copy_to_user(mode_ptr + copied,
  1459. &u_mode, sizeof(u_mode))) {
  1460. ret = -EFAULT;
  1461. goto out;
  1462. }
  1463. copied++;
  1464. }
  1465. }
  1466. out_resp->count_modes = mode_count;
  1467. if ((out_resp->count_props >= props_count) && props_count) {
  1468. copied = 0;
  1469. prop_ptr = (uint32_t __user *)(unsigned long)(out_resp->props_ptr);
  1470. prop_values = (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr);
  1471. for (i = 0; i < connector->properties.count; i++) {
  1472. if (put_user(connector->properties.ids[i],
  1473. prop_ptr + copied)) {
  1474. ret = -EFAULT;
  1475. goto out;
  1476. }
  1477. if (put_user(connector->properties.values[i],
  1478. prop_values + copied)) {
  1479. ret = -EFAULT;
  1480. goto out;
  1481. }
  1482. copied++;
  1483. }
  1484. }
  1485. out_resp->count_props = props_count;
  1486. if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
  1487. copied = 0;
  1488. encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
  1489. for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
  1490. if (connector->encoder_ids[i] != 0) {
  1491. if (put_user(connector->encoder_ids[i],
  1492. encoder_ptr + copied)) {
  1493. ret = -EFAULT;
  1494. goto out;
  1495. }
  1496. copied++;
  1497. }
  1498. }
  1499. }
  1500. out_resp->count_encoders = encoders_count;
  1501. out:
  1502. mutex_unlock(&dev->mode_config.mutex);
  1503. return ret;
  1504. }
  1505. int drm_mode_getencoder(struct drm_device *dev, void *data,
  1506. struct drm_file *file_priv)
  1507. {
  1508. struct drm_mode_get_encoder *enc_resp = data;
  1509. struct drm_mode_object *obj;
  1510. struct drm_encoder *encoder;
  1511. int ret = 0;
  1512. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1513. return -EINVAL;
  1514. drm_modeset_lock_all(dev);
  1515. obj = drm_mode_object_find(dev, enc_resp->encoder_id,
  1516. DRM_MODE_OBJECT_ENCODER);
  1517. if (!obj) {
  1518. ret = -EINVAL;
  1519. goto out;
  1520. }
  1521. encoder = obj_to_encoder(obj);
  1522. if (encoder->crtc)
  1523. enc_resp->crtc_id = encoder->crtc->base.id;
  1524. else
  1525. enc_resp->crtc_id = 0;
  1526. enc_resp->encoder_type = encoder->encoder_type;
  1527. enc_resp->encoder_id = encoder->base.id;
  1528. enc_resp->possible_crtcs = encoder->possible_crtcs;
  1529. enc_resp->possible_clones = encoder->possible_clones;
  1530. out:
  1531. drm_modeset_unlock_all(dev);
  1532. return ret;
  1533. }
  1534. /**
  1535. * drm_mode_getplane_res - get plane info
  1536. * @dev: DRM device
  1537. * @data: ioctl data
  1538. * @file_priv: DRM file info
  1539. *
  1540. * Return an plane count and set of IDs.
  1541. */
  1542. int drm_mode_getplane_res(struct drm_device *dev, void *data,
  1543. struct drm_file *file_priv)
  1544. {
  1545. struct drm_mode_get_plane_res *plane_resp = data;
  1546. struct drm_mode_config *config;
  1547. struct drm_plane *plane;
  1548. uint32_t __user *plane_ptr;
  1549. int copied = 0, ret = 0;
  1550. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1551. return -EINVAL;
  1552. drm_modeset_lock_all(dev);
  1553. config = &dev->mode_config;
  1554. /*
  1555. * This ioctl is called twice, once to determine how much space is
  1556. * needed, and the 2nd time to fill it.
  1557. */
  1558. if (config->num_plane &&
  1559. (plane_resp->count_planes >= config->num_plane)) {
  1560. plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
  1561. list_for_each_entry(plane, &config->plane_list, head) {
  1562. if (put_user(plane->base.id, plane_ptr + copied)) {
  1563. ret = -EFAULT;
  1564. goto out;
  1565. }
  1566. copied++;
  1567. }
  1568. }
  1569. plane_resp->count_planes = config->num_plane;
  1570. out:
  1571. drm_modeset_unlock_all(dev);
  1572. return ret;
  1573. }
  1574. /**
  1575. * drm_mode_getplane - get plane info
  1576. * @dev: DRM device
  1577. * @data: ioctl data
  1578. * @file_priv: DRM file info
  1579. *
  1580. * Return plane info, including formats supported, gamma size, any
  1581. * current fb, etc.
  1582. */
  1583. int drm_mode_getplane(struct drm_device *dev, void *data,
  1584. struct drm_file *file_priv)
  1585. {
  1586. struct drm_mode_get_plane *plane_resp = data;
  1587. struct drm_mode_object *obj;
  1588. struct drm_plane *plane;
  1589. uint32_t __user *format_ptr;
  1590. int ret = 0;
  1591. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1592. return -EINVAL;
  1593. drm_modeset_lock_all(dev);
  1594. obj = drm_mode_object_find(dev, plane_resp->plane_id,
  1595. DRM_MODE_OBJECT_PLANE);
  1596. if (!obj) {
  1597. ret = -ENOENT;
  1598. goto out;
  1599. }
  1600. plane = obj_to_plane(obj);
  1601. if (plane->crtc)
  1602. plane_resp->crtc_id = plane->crtc->base.id;
  1603. else
  1604. plane_resp->crtc_id = 0;
  1605. if (plane->fb)
  1606. plane_resp->fb_id = plane->fb->base.id;
  1607. else
  1608. plane_resp->fb_id = 0;
  1609. plane_resp->plane_id = plane->base.id;
  1610. plane_resp->possible_crtcs = plane->possible_crtcs;
  1611. plane_resp->gamma_size = 0;
  1612. /*
  1613. * This ioctl is called twice, once to determine how much space is
  1614. * needed, and the 2nd time to fill it.
  1615. */
  1616. if (plane->format_count &&
  1617. (plane_resp->count_format_types >= plane->format_count)) {
  1618. format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
  1619. if (copy_to_user(format_ptr,
  1620. plane->format_types,
  1621. sizeof(uint32_t) * plane->format_count)) {
  1622. ret = -EFAULT;
  1623. goto out;
  1624. }
  1625. }
  1626. plane_resp->count_format_types = plane->format_count;
  1627. out:
  1628. drm_modeset_unlock_all(dev);
  1629. return ret;
  1630. }
  1631. /**
  1632. * drm_mode_setplane - set up or tear down an plane
  1633. * @dev: DRM device
  1634. * @data: ioctl data*
  1635. * @file_priv: DRM file info
  1636. *
  1637. * Set plane info, including placement, fb, scaling, and other factors.
  1638. * Or pass a NULL fb to disable.
  1639. */
  1640. int drm_mode_setplane(struct drm_device *dev, void *data,
  1641. struct drm_file *file_priv)
  1642. {
  1643. struct drm_mode_set_plane *plane_req = data;
  1644. struct drm_mode_object *obj;
  1645. struct drm_plane *plane;
  1646. struct drm_crtc *crtc;
  1647. struct drm_framebuffer *fb = NULL, *old_fb = NULL;
  1648. int ret = 0;
  1649. unsigned int fb_width, fb_height;
  1650. int i;
  1651. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1652. return -EINVAL;
  1653. /*
  1654. * First, find the plane, crtc, and fb objects. If not available,
  1655. * we don't bother to call the driver.
  1656. */
  1657. obj = drm_mode_object_find(dev, plane_req->plane_id,
  1658. DRM_MODE_OBJECT_PLANE);
  1659. if (!obj) {
  1660. DRM_DEBUG_KMS("Unknown plane ID %d\n",
  1661. plane_req->plane_id);
  1662. return -ENOENT;
  1663. }
  1664. plane = obj_to_plane(obj);
  1665. /* No fb means shut it down */
  1666. if (!plane_req->fb_id) {
  1667. drm_modeset_lock_all(dev);
  1668. old_fb = plane->fb;
  1669. plane->funcs->disable_plane(plane);
  1670. plane->crtc = NULL;
  1671. plane->fb = NULL;
  1672. drm_modeset_unlock_all(dev);
  1673. goto out;
  1674. }
  1675. obj = drm_mode_object_find(dev, plane_req->crtc_id,
  1676. DRM_MODE_OBJECT_CRTC);
  1677. if (!obj) {
  1678. DRM_DEBUG_KMS("Unknown crtc ID %d\n",
  1679. plane_req->crtc_id);
  1680. ret = -ENOENT;
  1681. goto out;
  1682. }
  1683. crtc = obj_to_crtc(obj);
  1684. fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
  1685. if (!fb) {
  1686. DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
  1687. plane_req->fb_id);
  1688. ret = -ENOENT;
  1689. goto out;
  1690. }
  1691. /* Check whether this plane supports the fb pixel format. */
  1692. for (i = 0; i < plane->format_count; i++)
  1693. if (fb->pixel_format == plane->format_types[i])
  1694. break;
  1695. if (i == plane->format_count) {
  1696. DRM_DEBUG_KMS("Invalid pixel format %s\n",
  1697. drm_get_format_name(fb->pixel_format));
  1698. ret = -EINVAL;
  1699. goto out;
  1700. }
  1701. fb_width = fb->width << 16;
  1702. fb_height = fb->height << 16;
  1703. /* Make sure source coordinates are inside the fb. */
  1704. if (plane_req->src_w > fb_width ||
  1705. plane_req->src_x > fb_width - plane_req->src_w ||
  1706. plane_req->src_h > fb_height ||
  1707. plane_req->src_y > fb_height - plane_req->src_h) {
  1708. DRM_DEBUG_KMS("Invalid source coordinates "
  1709. "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
  1710. plane_req->src_w >> 16,
  1711. ((plane_req->src_w & 0xffff) * 15625) >> 10,
  1712. plane_req->src_h >> 16,
  1713. ((plane_req->src_h & 0xffff) * 15625) >> 10,
  1714. plane_req->src_x >> 16,
  1715. ((plane_req->src_x & 0xffff) * 15625) >> 10,
  1716. plane_req->src_y >> 16,
  1717. ((plane_req->src_y & 0xffff) * 15625) >> 10);
  1718. ret = -ENOSPC;
  1719. goto out;
  1720. }
  1721. /* Give drivers some help against integer overflows */
  1722. if (plane_req->crtc_w > INT_MAX ||
  1723. plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w ||
  1724. plane_req->crtc_h > INT_MAX ||
  1725. plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) {
  1726. DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
  1727. plane_req->crtc_w, plane_req->crtc_h,
  1728. plane_req->crtc_x, plane_req->crtc_y);
  1729. ret = -ERANGE;
  1730. goto out;
  1731. }
  1732. drm_modeset_lock_all(dev);
  1733. ret = plane->funcs->update_plane(plane, crtc, fb,
  1734. plane_req->crtc_x, plane_req->crtc_y,
  1735. plane_req->crtc_w, plane_req->crtc_h,
  1736. plane_req->src_x, plane_req->src_y,
  1737. plane_req->src_w, plane_req->src_h);
  1738. if (!ret) {
  1739. old_fb = plane->fb;
  1740. plane->crtc = crtc;
  1741. plane->fb = fb;
  1742. fb = NULL;
  1743. }
  1744. drm_modeset_unlock_all(dev);
  1745. out:
  1746. if (fb)
  1747. drm_framebuffer_unreference(fb);
  1748. if (old_fb)
  1749. drm_framebuffer_unreference(old_fb);
  1750. return ret;
  1751. }
  1752. /**
  1753. * drm_mode_set_config_internal - helper to call ->set_config
  1754. * @set: modeset config to set
  1755. *
  1756. * This is a little helper to wrap internal calls to the ->set_config driver
  1757. * interface. The only thing it adds is correct refcounting dance.
  1758. */
  1759. int drm_mode_set_config_internal(struct drm_mode_set *set)
  1760. {
  1761. struct drm_crtc *crtc = set->crtc;
  1762. struct drm_framebuffer *fb;
  1763. struct drm_crtc *tmp;
  1764. int ret;
  1765. /*
  1766. * NOTE: ->set_config can also disable other crtcs (if we steal all
  1767. * connectors from it), hence we need to refcount the fbs across all
  1768. * crtcs. Atomic modeset will have saner semantics ...
  1769. */
  1770. list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head)
  1771. tmp->old_fb = tmp->fb;
  1772. fb = set->fb;
  1773. ret = crtc->funcs->set_config(set);
  1774. if (ret == 0) {
  1775. /* crtc->fb must be updated by ->set_config, enforces this. */
  1776. WARN_ON(fb != crtc->fb);
  1777. }
  1778. list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head) {
  1779. if (tmp->fb)
  1780. drm_framebuffer_reference(tmp->fb);
  1781. if (tmp->old_fb)
  1782. drm_framebuffer_unreference(tmp->old_fb);
  1783. }
  1784. return ret;
  1785. }
  1786. EXPORT_SYMBOL(drm_mode_set_config_internal);
  1787. /*
  1788. * Checks that the framebuffer is big enough for the CRTC viewport
  1789. * (x, y, hdisplay, vdisplay)
  1790. */
  1791. static int drm_crtc_check_viewport(const struct drm_crtc *crtc,
  1792. int x, int y,
  1793. const struct drm_display_mode *mode,
  1794. const struct drm_framebuffer *fb)
  1795. {
  1796. int hdisplay, vdisplay;
  1797. hdisplay = mode->hdisplay;
  1798. vdisplay = mode->vdisplay;
  1799. if (drm_mode_is_stereo(mode)) {
  1800. struct drm_display_mode adjusted = *mode;
  1801. drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE);
  1802. hdisplay = adjusted.crtc_hdisplay;
  1803. vdisplay = adjusted.crtc_vdisplay;
  1804. }
  1805. if (crtc->invert_dimensions)
  1806. swap(hdisplay, vdisplay);
  1807. if (hdisplay > fb->width ||
  1808. vdisplay > fb->height ||
  1809. x > fb->width - hdisplay ||
  1810. y > fb->height - vdisplay) {
  1811. DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
  1812. fb->width, fb->height, hdisplay, vdisplay, x, y,
  1813. crtc->invert_dimensions ? " (inverted)" : "");
  1814. return -ENOSPC;
  1815. }
  1816. return 0;
  1817. }
  1818. /**
  1819. * drm_mode_setcrtc - set CRTC configuration
  1820. * @dev: drm device for the ioctl
  1821. * @data: data pointer for the ioctl
  1822. * @file_priv: drm file for the ioctl call
  1823. *
  1824. * Build a new CRTC configuration based on user request.
  1825. *
  1826. * Called by the user via ioctl.
  1827. *
  1828. * RETURNS:
  1829. * Zero on success, errno on failure.
  1830. */
  1831. int drm_mode_setcrtc(struct drm_device *dev, void *data,
  1832. struct drm_file *file_priv)
  1833. {
  1834. struct drm_mode_config *config = &dev->mode_config;
  1835. struct drm_mode_crtc *crtc_req = data;
  1836. struct drm_mode_object *obj;
  1837. struct drm_crtc *crtc;
  1838. struct drm_connector **connector_set = NULL, *connector;
  1839. struct drm_framebuffer *fb = NULL;
  1840. struct drm_display_mode *mode = NULL;
  1841. struct drm_mode_set set;
  1842. uint32_t __user *set_connectors_ptr;
  1843. int ret;
  1844. int i;
  1845. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1846. return -EINVAL;
  1847. /* For some reason crtc x/y offsets are signed internally. */
  1848. if (crtc_req->x > INT_MAX || crtc_req->y > INT_MAX)
  1849. return -ERANGE;
  1850. drm_modeset_lock_all(dev);
  1851. obj = drm_mode_object_find(dev, crtc_req->crtc_id,
  1852. DRM_MODE_OBJECT_CRTC);
  1853. if (!obj) {
  1854. DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
  1855. ret = -EINVAL;
  1856. goto out;
  1857. }
  1858. crtc = obj_to_crtc(obj);
  1859. DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
  1860. if (crtc_req->mode_valid) {
  1861. /* If we have a mode we need a framebuffer. */
  1862. /* If we pass -1, set the mode with the currently bound fb */
  1863. if (crtc_req->fb_id == -1) {
  1864. if (!crtc->fb) {
  1865. DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
  1866. ret = -EINVAL;
  1867. goto out;
  1868. }
  1869. fb = crtc->fb;
  1870. /* Make refcounting symmetric with the lookup path. */
  1871. drm_framebuffer_reference(fb);
  1872. } else {
  1873. fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
  1874. if (!fb) {
  1875. DRM_DEBUG_KMS("Unknown FB ID%d\n",
  1876. crtc_req->fb_id);
  1877. ret = -EINVAL;
  1878. goto out;
  1879. }
  1880. }
  1881. mode = drm_mode_create(dev);
  1882. if (!mode) {
  1883. ret = -ENOMEM;
  1884. goto out;
  1885. }
  1886. ret = drm_crtc_convert_umode(mode, &crtc_req->mode);
  1887. if (ret) {
  1888. DRM_DEBUG_KMS("Invalid mode\n");
  1889. goto out;
  1890. }
  1891. drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
  1892. ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
  1893. mode, fb);
  1894. if (ret)
  1895. goto out;
  1896. }
  1897. if (crtc_req->count_connectors == 0 && mode) {
  1898. DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
  1899. ret = -EINVAL;
  1900. goto out;
  1901. }
  1902. if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
  1903. DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
  1904. crtc_req->count_connectors);
  1905. ret = -EINVAL;
  1906. goto out;
  1907. }
  1908. if (crtc_req->count_connectors > 0) {
  1909. u32 out_id;
  1910. /* Avoid unbounded kernel memory allocation */
  1911. if (crtc_req->count_connectors > config->num_connector) {
  1912. ret = -EINVAL;
  1913. goto out;
  1914. }
  1915. connector_set = kmalloc(crtc_req->count_connectors *
  1916. sizeof(struct drm_connector *),
  1917. GFP_KERNEL);
  1918. if (!connector_set) {
  1919. ret = -ENOMEM;
  1920. goto out;
  1921. }
  1922. for (i = 0; i < crtc_req->count_connectors; i++) {
  1923. set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
  1924. if (get_user(out_id, &set_connectors_ptr[i])) {
  1925. ret = -EFAULT;
  1926. goto out;
  1927. }
  1928. obj = drm_mode_object_find(dev, out_id,
  1929. DRM_MODE_OBJECT_CONNECTOR);
  1930. if (!obj) {
  1931. DRM_DEBUG_KMS("Connector id %d unknown\n",
  1932. out_id);
  1933. ret = -EINVAL;
  1934. goto out;
  1935. }
  1936. connector = obj_to_connector(obj);
  1937. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  1938. connector->base.id,
  1939. drm_get_connector_name(connector));
  1940. connector_set[i] = connector;
  1941. }
  1942. }
  1943. set.crtc = crtc;
  1944. set.x = crtc_req->x;
  1945. set.y = crtc_req->y;
  1946. set.mode = mode;
  1947. set.connectors = connector_set;
  1948. set.num_connectors = crtc_req->count_connectors;
  1949. set.fb = fb;
  1950. ret = drm_mode_set_config_internal(&set);
  1951. out:
  1952. if (fb)
  1953. drm_framebuffer_unreference(fb);
  1954. kfree(connector_set);
  1955. drm_mode_destroy(dev, mode);
  1956. drm_modeset_unlock_all(dev);
  1957. return ret;
  1958. }
  1959. static int drm_mode_cursor_common(struct drm_device *dev,
  1960. struct drm_mode_cursor2 *req,
  1961. struct drm_file *file_priv)
  1962. {
  1963. struct drm_mode_object *obj;
  1964. struct drm_crtc *crtc;
  1965. int ret = 0;
  1966. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1967. return -EINVAL;
  1968. if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
  1969. return -EINVAL;
  1970. obj = drm_mode_object_find(dev, req->crtc_id, DRM_MODE_OBJECT_CRTC);
  1971. if (!obj) {
  1972. DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
  1973. return -EINVAL;
  1974. }
  1975. crtc = obj_to_crtc(obj);
  1976. mutex_lock(&crtc->mutex);
  1977. if (req->flags & DRM_MODE_CURSOR_BO) {
  1978. if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
  1979. ret = -ENXIO;
  1980. goto out;
  1981. }
  1982. /* Turns off the cursor if handle is 0 */
  1983. if (crtc->funcs->cursor_set2)
  1984. ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
  1985. req->width, req->height, req->hot_x, req->hot_y);
  1986. else
  1987. ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
  1988. req->width, req->height);
  1989. }
  1990. if (req->flags & DRM_MODE_CURSOR_MOVE) {
  1991. if (crtc->funcs->cursor_move) {
  1992. ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
  1993. } else {
  1994. ret = -EFAULT;
  1995. goto out;
  1996. }
  1997. }
  1998. out:
  1999. mutex_unlock(&crtc->mutex);
  2000. return ret;
  2001. }
  2002. int drm_mode_cursor_ioctl(struct drm_device *dev,
  2003. void *data, struct drm_file *file_priv)
  2004. {
  2005. struct drm_mode_cursor *req = data;
  2006. struct drm_mode_cursor2 new_req;
  2007. memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
  2008. new_req.hot_x = new_req.hot_y = 0;
  2009. return drm_mode_cursor_common(dev, &new_req, file_priv);
  2010. }
  2011. int drm_mode_cursor2_ioctl(struct drm_device *dev,
  2012. void *data, struct drm_file *file_priv)
  2013. {
  2014. struct drm_mode_cursor2 *req = data;
  2015. return drm_mode_cursor_common(dev, req, file_priv);
  2016. }
  2017. /* Original addfb only supported RGB formats, so figure out which one */
  2018. uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
  2019. {
  2020. uint32_t fmt;
  2021. switch (bpp) {
  2022. case 8:
  2023. fmt = DRM_FORMAT_C8;
  2024. break;
  2025. case 16:
  2026. if (depth == 15)
  2027. fmt = DRM_FORMAT_XRGB1555;
  2028. else
  2029. fmt = DRM_FORMAT_RGB565;
  2030. break;
  2031. case 24:
  2032. fmt = DRM_FORMAT_RGB888;
  2033. break;
  2034. case 32:
  2035. if (depth == 24)
  2036. fmt = DRM_FORMAT_XRGB8888;
  2037. else if (depth == 30)
  2038. fmt = DRM_FORMAT_XRGB2101010;
  2039. else
  2040. fmt = DRM_FORMAT_ARGB8888;
  2041. break;
  2042. default:
  2043. DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
  2044. fmt = DRM_FORMAT_XRGB8888;
  2045. break;
  2046. }
  2047. return fmt;
  2048. }
  2049. EXPORT_SYMBOL(drm_mode_legacy_fb_format);
  2050. /**
  2051. * drm_mode_addfb - add an FB to the graphics configuration
  2052. * @dev: drm device for the ioctl
  2053. * @data: data pointer for the ioctl
  2054. * @file_priv: drm file for the ioctl call
  2055. *
  2056. * Add a new FB to the specified CRTC, given a user request.
  2057. *
  2058. * Called by the user via ioctl.
  2059. *
  2060. * RETURNS:
  2061. * Zero on success, errno on failure.
  2062. */
  2063. int drm_mode_addfb(struct drm_device *dev,
  2064. void *data, struct drm_file *file_priv)
  2065. {
  2066. struct drm_mode_fb_cmd *or = data;
  2067. struct drm_mode_fb_cmd2 r = {};
  2068. struct drm_mode_config *config = &dev->mode_config;
  2069. struct drm_framebuffer *fb;
  2070. int ret = 0;
  2071. /* Use new struct with format internally */
  2072. r.fb_id = or->fb_id;
  2073. r.width = or->width;
  2074. r.height = or->height;
  2075. r.pitches[0] = or->pitch;
  2076. r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
  2077. r.handles[0] = or->handle;
  2078. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2079. return -EINVAL;
  2080. if ((config->min_width > r.width) || (r.width > config->max_width))
  2081. return -EINVAL;
  2082. if ((config->min_height > r.height) || (r.height > config->max_height))
  2083. return -EINVAL;
  2084. fb = dev->mode_config.funcs->fb_create(dev, file_priv, &r);
  2085. if (IS_ERR(fb)) {
  2086. DRM_DEBUG_KMS("could not create framebuffer\n");
  2087. return PTR_ERR(fb);
  2088. }
  2089. mutex_lock(&file_priv->fbs_lock);
  2090. or->fb_id = fb->base.id;
  2091. list_add(&fb->filp_head, &file_priv->fbs);
  2092. DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
  2093. mutex_unlock(&file_priv->fbs_lock);
  2094. return ret;
  2095. }
  2096. static int format_check(const struct drm_mode_fb_cmd2 *r)
  2097. {
  2098. uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
  2099. switch (format) {
  2100. case DRM_FORMAT_C8:
  2101. case DRM_FORMAT_RGB332:
  2102. case DRM_FORMAT_BGR233:
  2103. case DRM_FORMAT_XRGB4444:
  2104. case DRM_FORMAT_XBGR4444:
  2105. case DRM_FORMAT_RGBX4444:
  2106. case DRM_FORMAT_BGRX4444:
  2107. case DRM_FORMAT_ARGB4444:
  2108. case DRM_FORMAT_ABGR4444:
  2109. case DRM_FORMAT_RGBA4444:
  2110. case DRM_FORMAT_BGRA4444:
  2111. case DRM_FORMAT_XRGB1555:
  2112. case DRM_FORMAT_XBGR1555:
  2113. case DRM_FORMAT_RGBX5551:
  2114. case DRM_FORMAT_BGRX5551:
  2115. case DRM_FORMAT_ARGB1555:
  2116. case DRM_FORMAT_ABGR1555:
  2117. case DRM_FORMAT_RGBA5551:
  2118. case DRM_FORMAT_BGRA5551:
  2119. case DRM_FORMAT_RGB565:
  2120. case DRM_FORMAT_BGR565:
  2121. case DRM_FORMAT_RGB888:
  2122. case DRM_FORMAT_BGR888:
  2123. case DRM_FORMAT_XRGB8888:
  2124. case DRM_FORMAT_XBGR8888:
  2125. case DRM_FORMAT_RGBX8888:
  2126. case DRM_FORMAT_BGRX8888:
  2127. case DRM_FORMAT_ARGB8888:
  2128. case DRM_FORMAT_ABGR8888:
  2129. case DRM_FORMAT_RGBA8888:
  2130. case DRM_FORMAT_BGRA8888:
  2131. case DRM_FORMAT_XRGB2101010:
  2132. case DRM_FORMAT_XBGR2101010:
  2133. case DRM_FORMAT_RGBX1010102:
  2134. case DRM_FORMAT_BGRX1010102:
  2135. case DRM_FORMAT_ARGB2101010:
  2136. case DRM_FORMAT_ABGR2101010:
  2137. case DRM_FORMAT_RGBA1010102:
  2138. case DRM_FORMAT_BGRA1010102:
  2139. case DRM_FORMAT_YUYV:
  2140. case DRM_FORMAT_YVYU:
  2141. case DRM_FORMAT_UYVY:
  2142. case DRM_FORMAT_VYUY:
  2143. case DRM_FORMAT_AYUV:
  2144. case DRM_FORMAT_NV12:
  2145. case DRM_FORMAT_NV21:
  2146. case DRM_FORMAT_NV16:
  2147. case DRM_FORMAT_NV61:
  2148. case DRM_FORMAT_NV24:
  2149. case DRM_FORMAT_NV42:
  2150. case DRM_FORMAT_YUV410:
  2151. case DRM_FORMAT_YVU410:
  2152. case DRM_FORMAT_YUV411:
  2153. case DRM_FORMAT_YVU411:
  2154. case DRM_FORMAT_YUV420:
  2155. case DRM_FORMAT_YVU420:
  2156. case DRM_FORMAT_YUV422:
  2157. case DRM_FORMAT_YVU422:
  2158. case DRM_FORMAT_YUV444:
  2159. case DRM_FORMAT_YVU444:
  2160. return 0;
  2161. default:
  2162. return -EINVAL;
  2163. }
  2164. }
  2165. static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
  2166. {
  2167. int ret, hsub, vsub, num_planes, i;
  2168. ret = format_check(r);
  2169. if (ret) {
  2170. DRM_DEBUG_KMS("bad framebuffer format %s\n",
  2171. drm_get_format_name(r->pixel_format));
  2172. return ret;
  2173. }
  2174. hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
  2175. vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
  2176. num_planes = drm_format_num_planes(r->pixel_format);
  2177. if (r->width == 0 || r->width % hsub) {
  2178. DRM_DEBUG_KMS("bad framebuffer width %u\n", r->height);
  2179. return -EINVAL;
  2180. }
  2181. if (r->height == 0 || r->height % vsub) {
  2182. DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
  2183. return -EINVAL;
  2184. }
  2185. for (i = 0; i < num_planes; i++) {
  2186. unsigned int width = r->width / (i != 0 ? hsub : 1);
  2187. unsigned int height = r->height / (i != 0 ? vsub : 1);
  2188. unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
  2189. if (!r->handles[i]) {
  2190. DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
  2191. return -EINVAL;
  2192. }
  2193. if ((uint64_t) width * cpp > UINT_MAX)
  2194. return -ERANGE;
  2195. if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
  2196. return -ERANGE;
  2197. if (r->pitches[i] < width * cpp) {
  2198. DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
  2199. return -EINVAL;
  2200. }
  2201. }
  2202. return 0;
  2203. }
  2204. /**
  2205. * drm_mode_addfb2 - add an FB to the graphics configuration
  2206. * @dev: drm device for the ioctl
  2207. * @data: data pointer for the ioctl
  2208. * @file_priv: drm file for the ioctl call
  2209. *
  2210. * Add a new FB to the specified CRTC, given a user request with format.
  2211. *
  2212. * Called by the user via ioctl.
  2213. *
  2214. * RETURNS:
  2215. * Zero on success, errno on failure.
  2216. */
  2217. int drm_mode_addfb2(struct drm_device *dev,
  2218. void *data, struct drm_file *file_priv)
  2219. {
  2220. struct drm_mode_fb_cmd2 *r = data;
  2221. struct drm_mode_config *config = &dev->mode_config;
  2222. struct drm_framebuffer *fb;
  2223. int ret;
  2224. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2225. return -EINVAL;
  2226. if (r->flags & ~DRM_MODE_FB_INTERLACED) {
  2227. DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
  2228. return -EINVAL;
  2229. }
  2230. if ((config->min_width > r->width) || (r->width > config->max_width)) {
  2231. DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
  2232. r->width, config->min_width, config->max_width);
  2233. return -EINVAL;
  2234. }
  2235. if ((config->min_height > r->height) || (r->height > config->max_height)) {
  2236. DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
  2237. r->height, config->min_height, config->max_height);
  2238. return -EINVAL;
  2239. }
  2240. ret = framebuffer_check(r);
  2241. if (ret)
  2242. return ret;
  2243. fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
  2244. if (IS_ERR(fb)) {
  2245. DRM_DEBUG_KMS("could not create framebuffer\n");
  2246. return PTR_ERR(fb);
  2247. }
  2248. mutex_lock(&file_priv->fbs_lock);
  2249. r->fb_id = fb->base.id;
  2250. list_add(&fb->filp_head, &file_priv->fbs);
  2251. DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
  2252. mutex_unlock(&file_priv->fbs_lock);
  2253. return ret;
  2254. }
  2255. /**
  2256. * drm_mode_rmfb - remove an FB from the configuration
  2257. * @dev: drm device for the ioctl
  2258. * @data: data pointer for the ioctl
  2259. * @file_priv: drm file for the ioctl call
  2260. *
  2261. * Remove the FB specified by the user.
  2262. *
  2263. * Called by the user via ioctl.
  2264. *
  2265. * RETURNS:
  2266. * Zero on success, errno on failure.
  2267. */
  2268. int drm_mode_rmfb(struct drm_device *dev,
  2269. void *data, struct drm_file *file_priv)
  2270. {
  2271. struct drm_framebuffer *fb = NULL;
  2272. struct drm_framebuffer *fbl = NULL;
  2273. uint32_t *id = data;
  2274. int found = 0;
  2275. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2276. return -EINVAL;
  2277. mutex_lock(&file_priv->fbs_lock);
  2278. mutex_lock(&dev->mode_config.fb_lock);
  2279. fb = __drm_framebuffer_lookup(dev, *id);
  2280. if (!fb)
  2281. goto fail_lookup;
  2282. list_for_each_entry(fbl, &file_priv->fbs, filp_head)
  2283. if (fb == fbl)
  2284. found = 1;
  2285. if (!found)
  2286. goto fail_lookup;
  2287. /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
  2288. __drm_framebuffer_unregister(dev, fb);
  2289. list_del_init(&fb->filp_head);
  2290. mutex_unlock(&dev->mode_config.fb_lock);
  2291. mutex_unlock(&file_priv->fbs_lock);
  2292. drm_framebuffer_remove(fb);
  2293. return 0;
  2294. fail_lookup:
  2295. mutex_unlock(&dev->mode_config.fb_lock);
  2296. mutex_unlock(&file_priv->fbs_lock);
  2297. return -EINVAL;
  2298. }
  2299. /**
  2300. * drm_mode_getfb - get FB info
  2301. * @dev: drm device for the ioctl
  2302. * @data: data pointer for the ioctl
  2303. * @file_priv: drm file for the ioctl call
  2304. *
  2305. * Lookup the FB given its ID and return info about it.
  2306. *
  2307. * Called by the user via ioctl.
  2308. *
  2309. * RETURNS:
  2310. * Zero on success, errno on failure.
  2311. */
  2312. int drm_mode_getfb(struct drm_device *dev,
  2313. void *data, struct drm_file *file_priv)
  2314. {
  2315. struct drm_mode_fb_cmd *r = data;
  2316. struct drm_framebuffer *fb;
  2317. int ret;
  2318. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2319. return -EINVAL;
  2320. fb = drm_framebuffer_lookup(dev, r->fb_id);
  2321. if (!fb)
  2322. return -EINVAL;
  2323. r->height = fb->height;
  2324. r->width = fb->width;
  2325. r->depth = fb->depth;
  2326. r->bpp = fb->bits_per_pixel;
  2327. r->pitch = fb->pitches[0];
  2328. if (fb->funcs->create_handle) {
  2329. if (file_priv->is_master || capable(CAP_SYS_ADMIN)) {
  2330. ret = fb->funcs->create_handle(fb, file_priv,
  2331. &r->handle);
  2332. } else {
  2333. /* GET_FB() is an unprivileged ioctl so we must not
  2334. * return a buffer-handle to non-master processes! For
  2335. * backwards-compatibility reasons, we cannot make
  2336. * GET_FB() privileged, so just return an invalid handle
  2337. * for non-masters. */
  2338. r->handle = 0;
  2339. ret = 0;
  2340. }
  2341. } else {
  2342. ret = -ENODEV;
  2343. }
  2344. drm_framebuffer_unreference(fb);
  2345. return ret;
  2346. }
  2347. int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
  2348. void *data, struct drm_file *file_priv)
  2349. {
  2350. struct drm_clip_rect __user *clips_ptr;
  2351. struct drm_clip_rect *clips = NULL;
  2352. struct drm_mode_fb_dirty_cmd *r = data;
  2353. struct drm_framebuffer *fb;
  2354. unsigned flags;
  2355. int num_clips;
  2356. int ret;
  2357. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2358. return -EINVAL;
  2359. fb = drm_framebuffer_lookup(dev, r->fb_id);
  2360. if (!fb)
  2361. return -EINVAL;
  2362. num_clips = r->num_clips;
  2363. clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
  2364. if (!num_clips != !clips_ptr) {
  2365. ret = -EINVAL;
  2366. goto out_err1;
  2367. }
  2368. flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
  2369. /* If userspace annotates copy, clips must come in pairs */
  2370. if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
  2371. ret = -EINVAL;
  2372. goto out_err1;
  2373. }
  2374. if (num_clips && clips_ptr) {
  2375. if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
  2376. ret = -EINVAL;
  2377. goto out_err1;
  2378. }
  2379. clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL);
  2380. if (!clips) {
  2381. ret = -ENOMEM;
  2382. goto out_err1;
  2383. }
  2384. ret = copy_from_user(clips, clips_ptr,
  2385. num_clips * sizeof(*clips));
  2386. if (ret) {
  2387. ret = -EFAULT;
  2388. goto out_err2;
  2389. }
  2390. }
  2391. if (fb->funcs->dirty) {
  2392. drm_modeset_lock_all(dev);
  2393. ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
  2394. clips, num_clips);
  2395. drm_modeset_unlock_all(dev);
  2396. } else {
  2397. ret = -ENOSYS;
  2398. }
  2399. out_err2:
  2400. kfree(clips);
  2401. out_err1:
  2402. drm_framebuffer_unreference(fb);
  2403. return ret;
  2404. }
  2405. /**
  2406. * drm_fb_release - remove and free the FBs on this file
  2407. * @priv: drm file for the ioctl
  2408. *
  2409. * Destroy all the FBs associated with @filp.
  2410. *
  2411. * Called by the user via ioctl.
  2412. *
  2413. * RETURNS:
  2414. * Zero on success, errno on failure.
  2415. */
  2416. void drm_fb_release(struct drm_file *priv)
  2417. {
  2418. struct drm_device *dev = priv->minor->dev;
  2419. struct drm_framebuffer *fb, *tfb;
  2420. mutex_lock(&priv->fbs_lock);
  2421. list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
  2422. mutex_lock(&dev->mode_config.fb_lock);
  2423. /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
  2424. __drm_framebuffer_unregister(dev, fb);
  2425. mutex_unlock(&dev->mode_config.fb_lock);
  2426. list_del_init(&fb->filp_head);
  2427. /* This will also drop the fpriv->fbs reference. */
  2428. drm_framebuffer_remove(fb);
  2429. }
  2430. mutex_unlock(&priv->fbs_lock);
  2431. }
  2432. struct drm_property *drm_property_create(struct drm_device *dev, int flags,
  2433. const char *name, int num_values)
  2434. {
  2435. struct drm_property *property = NULL;
  2436. int ret;
  2437. property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
  2438. if (!property)
  2439. return NULL;
  2440. if (num_values) {
  2441. property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
  2442. if (!property->values)
  2443. goto fail;
  2444. }
  2445. ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
  2446. if (ret)
  2447. goto fail;
  2448. property->flags = flags;
  2449. property->num_values = num_values;
  2450. INIT_LIST_HEAD(&property->enum_blob_list);
  2451. if (name) {
  2452. strncpy(property->name, name, DRM_PROP_NAME_LEN);
  2453. property->name[DRM_PROP_NAME_LEN-1] = '\0';
  2454. }
  2455. list_add_tail(&property->head, &dev->mode_config.property_list);
  2456. return property;
  2457. fail:
  2458. kfree(property->values);
  2459. kfree(property);
  2460. return NULL;
  2461. }
  2462. EXPORT_SYMBOL(drm_property_create);
  2463. struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
  2464. const char *name,
  2465. const struct drm_prop_enum_list *props,
  2466. int num_values)
  2467. {
  2468. struct drm_property *property;
  2469. int i, ret;
  2470. flags |= DRM_MODE_PROP_ENUM;
  2471. property = drm_property_create(dev, flags, name, num_values);
  2472. if (!property)
  2473. return NULL;
  2474. for (i = 0; i < num_values; i++) {
  2475. ret = drm_property_add_enum(property, i,
  2476. props[i].type,
  2477. props[i].name);
  2478. if (ret) {
  2479. drm_property_destroy(dev, property);
  2480. return NULL;
  2481. }
  2482. }
  2483. return property;
  2484. }
  2485. EXPORT_SYMBOL(drm_property_create_enum);
  2486. struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
  2487. int flags, const char *name,
  2488. const struct drm_prop_enum_list *props,
  2489. int num_values)
  2490. {
  2491. struct drm_property *property;
  2492. int i, ret;
  2493. flags |= DRM_MODE_PROP_BITMASK;
  2494. property = drm_property_create(dev, flags, name, num_values);
  2495. if (!property)
  2496. return NULL;
  2497. for (i = 0; i < num_values; i++) {
  2498. ret = drm_property_add_enum(property, i,
  2499. props[i].type,
  2500. props[i].name);
  2501. if (ret) {
  2502. drm_property_destroy(dev, property);
  2503. return NULL;
  2504. }
  2505. }
  2506. return property;
  2507. }
  2508. EXPORT_SYMBOL(drm_property_create_bitmask);
  2509. struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
  2510. const char *name,
  2511. uint64_t min, uint64_t max)
  2512. {
  2513. struct drm_property *property;
  2514. flags |= DRM_MODE_PROP_RANGE;
  2515. property = drm_property_create(dev, flags, name, 2);
  2516. if (!property)
  2517. return NULL;
  2518. property->values[0] = min;
  2519. property->values[1] = max;
  2520. return property;
  2521. }
  2522. EXPORT_SYMBOL(drm_property_create_range);
  2523. int drm_property_add_enum(struct drm_property *property, int index,
  2524. uint64_t value, const char *name)
  2525. {
  2526. struct drm_property_enum *prop_enum;
  2527. if (!(property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)))
  2528. return -EINVAL;
  2529. /*
  2530. * Bitmask enum properties have the additional constraint of values
  2531. * from 0 to 63
  2532. */
  2533. if ((property->flags & DRM_MODE_PROP_BITMASK) && (value > 63))
  2534. return -EINVAL;
  2535. if (!list_empty(&property->enum_blob_list)) {
  2536. list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
  2537. if (prop_enum->value == value) {
  2538. strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
  2539. prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
  2540. return 0;
  2541. }
  2542. }
  2543. }
  2544. prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
  2545. if (!prop_enum)
  2546. return -ENOMEM;
  2547. strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
  2548. prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
  2549. prop_enum->value = value;
  2550. property->values[index] = value;
  2551. list_add_tail(&prop_enum->head, &property->enum_blob_list);
  2552. return 0;
  2553. }
  2554. EXPORT_SYMBOL(drm_property_add_enum);
  2555. void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
  2556. {
  2557. struct drm_property_enum *prop_enum, *pt;
  2558. list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
  2559. list_del(&prop_enum->head);
  2560. kfree(prop_enum);
  2561. }
  2562. if (property->num_values)
  2563. kfree(property->values);
  2564. drm_mode_object_put(dev, &property->base);
  2565. list_del(&property->head);
  2566. kfree(property);
  2567. }
  2568. EXPORT_SYMBOL(drm_property_destroy);
  2569. void drm_object_attach_property(struct drm_mode_object *obj,
  2570. struct drm_property *property,
  2571. uint64_t init_val)
  2572. {
  2573. int count = obj->properties->count;
  2574. if (count == DRM_OBJECT_MAX_PROPERTY) {
  2575. WARN(1, "Failed to attach object property (type: 0x%x). Please "
  2576. "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
  2577. "you see this message on the same object type.\n",
  2578. obj->type);
  2579. return;
  2580. }
  2581. obj->properties->ids[count] = property->base.id;
  2582. obj->properties->values[count] = init_val;
  2583. obj->properties->count++;
  2584. }
  2585. EXPORT_SYMBOL(drm_object_attach_property);
  2586. int drm_object_property_set_value(struct drm_mode_object *obj,
  2587. struct drm_property *property, uint64_t val)
  2588. {
  2589. int i;
  2590. for (i = 0; i < obj->properties->count; i++) {
  2591. if (obj->properties->ids[i] == property->base.id) {
  2592. obj->properties->values[i] = val;
  2593. return 0;
  2594. }
  2595. }
  2596. return -EINVAL;
  2597. }
  2598. EXPORT_SYMBOL(drm_object_property_set_value);
  2599. int drm_object_property_get_value(struct drm_mode_object *obj,
  2600. struct drm_property *property, uint64_t *val)
  2601. {
  2602. int i;
  2603. for (i = 0; i < obj->properties->count; i++) {
  2604. if (obj->properties->ids[i] == property->base.id) {
  2605. *val = obj->properties->values[i];
  2606. return 0;
  2607. }
  2608. }
  2609. return -EINVAL;
  2610. }
  2611. EXPORT_SYMBOL(drm_object_property_get_value);
  2612. int drm_mode_getproperty_ioctl(struct drm_device *dev,
  2613. void *data, struct drm_file *file_priv)
  2614. {
  2615. struct drm_mode_object *obj;
  2616. struct drm_mode_get_property *out_resp = data;
  2617. struct drm_property *property;
  2618. int enum_count = 0;
  2619. int blob_count = 0;
  2620. int value_count = 0;
  2621. int ret = 0, i;
  2622. int copied;
  2623. struct drm_property_enum *prop_enum;
  2624. struct drm_mode_property_enum __user *enum_ptr;
  2625. struct drm_property_blob *prop_blob;
  2626. uint32_t __user *blob_id_ptr;
  2627. uint64_t __user *values_ptr;
  2628. uint32_t __user *blob_length_ptr;
  2629. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2630. return -EINVAL;
  2631. drm_modeset_lock_all(dev);
  2632. obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
  2633. if (!obj) {
  2634. ret = -EINVAL;
  2635. goto done;
  2636. }
  2637. property = obj_to_property(obj);
  2638. if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
  2639. list_for_each_entry(prop_enum, &property->enum_blob_list, head)
  2640. enum_count++;
  2641. } else if (property->flags & DRM_MODE_PROP_BLOB) {
  2642. list_for_each_entry(prop_blob, &property->enum_blob_list, head)
  2643. blob_count++;
  2644. }
  2645. value_count = property->num_values;
  2646. strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
  2647. out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
  2648. out_resp->flags = property->flags;
  2649. if ((out_resp->count_values >= value_count) && value_count) {
  2650. values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
  2651. for (i = 0; i < value_count; i++) {
  2652. if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
  2653. ret = -EFAULT;
  2654. goto done;
  2655. }
  2656. }
  2657. }
  2658. out_resp->count_values = value_count;
  2659. if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
  2660. if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
  2661. copied = 0;
  2662. enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
  2663. list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
  2664. if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
  2665. ret = -EFAULT;
  2666. goto done;
  2667. }
  2668. if (copy_to_user(&enum_ptr[copied].name,
  2669. &prop_enum->name, DRM_PROP_NAME_LEN)) {
  2670. ret = -EFAULT;
  2671. goto done;
  2672. }
  2673. copied++;
  2674. }
  2675. }
  2676. out_resp->count_enum_blobs = enum_count;
  2677. }
  2678. if (property->flags & DRM_MODE_PROP_BLOB) {
  2679. if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
  2680. copied = 0;
  2681. blob_id_ptr = (uint32_t __user *)(unsigned long)out_resp->enum_blob_ptr;
  2682. blob_length_ptr = (uint32_t __user *)(unsigned long)out_resp->values_ptr;
  2683. list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
  2684. if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
  2685. ret = -EFAULT;
  2686. goto done;
  2687. }
  2688. if (put_user(prop_blob->length, blob_length_ptr + copied)) {
  2689. ret = -EFAULT;
  2690. goto done;
  2691. }
  2692. copied++;
  2693. }
  2694. }
  2695. out_resp->count_enum_blobs = blob_count;
  2696. }
  2697. done:
  2698. drm_modeset_unlock_all(dev);
  2699. return ret;
  2700. }
  2701. static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
  2702. void *data)
  2703. {
  2704. struct drm_property_blob *blob;
  2705. int ret;
  2706. if (!length || !data)
  2707. return NULL;
  2708. blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
  2709. if (!blob)
  2710. return NULL;
  2711. ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
  2712. if (ret) {
  2713. kfree(blob);
  2714. return NULL;
  2715. }
  2716. blob->length = length;
  2717. memcpy(blob->data, data, length);
  2718. list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
  2719. return blob;
  2720. }
  2721. static void drm_property_destroy_blob(struct drm_device *dev,
  2722. struct drm_property_blob *blob)
  2723. {
  2724. drm_mode_object_put(dev, &blob->base);
  2725. list_del(&blob->head);
  2726. kfree(blob);
  2727. }
  2728. int drm_mode_getblob_ioctl(struct drm_device *dev,
  2729. void *data, struct drm_file *file_priv)
  2730. {
  2731. struct drm_mode_object *obj;
  2732. struct drm_mode_get_blob *out_resp = data;
  2733. struct drm_property_blob *blob;
  2734. int ret = 0;
  2735. void __user *blob_ptr;
  2736. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2737. return -EINVAL;
  2738. drm_modeset_lock_all(dev);
  2739. obj = drm_mode_object_find(dev, out_resp->blob_id, DRM_MODE_OBJECT_BLOB);
  2740. if (!obj) {
  2741. ret = -EINVAL;
  2742. goto done;
  2743. }
  2744. blob = obj_to_blob(obj);
  2745. if (out_resp->length == blob->length) {
  2746. blob_ptr = (void __user *)(unsigned long)out_resp->data;
  2747. if (copy_to_user(blob_ptr, blob->data, blob->length)){
  2748. ret = -EFAULT;
  2749. goto done;
  2750. }
  2751. }
  2752. out_resp->length = blob->length;
  2753. done:
  2754. drm_modeset_unlock_all(dev);
  2755. return ret;
  2756. }
  2757. int drm_mode_connector_update_edid_property(struct drm_connector *connector,
  2758. struct edid *edid)
  2759. {
  2760. struct drm_device *dev = connector->dev;
  2761. int ret, size;
  2762. if (connector->edid_blob_ptr)
  2763. drm_property_destroy_blob(dev, connector->edid_blob_ptr);
  2764. /* Delete edid, when there is none. */
  2765. if (!edid) {
  2766. connector->edid_blob_ptr = NULL;
  2767. ret = drm_object_property_set_value(&connector->base, dev->mode_config.edid_property, 0);
  2768. return ret;
  2769. }
  2770. size = EDID_LENGTH * (1 + edid->extensions);
  2771. connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
  2772. size, edid);
  2773. if (!connector->edid_blob_ptr)
  2774. return -EINVAL;
  2775. ret = drm_object_property_set_value(&connector->base,
  2776. dev->mode_config.edid_property,
  2777. connector->edid_blob_ptr->base.id);
  2778. return ret;
  2779. }
  2780. EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
  2781. static bool drm_property_change_is_valid(struct drm_property *property,
  2782. uint64_t value)
  2783. {
  2784. if (property->flags & DRM_MODE_PROP_IMMUTABLE)
  2785. return false;
  2786. if (property->flags & DRM_MODE_PROP_RANGE) {
  2787. if (value < property->values[0] || value > property->values[1])
  2788. return false;
  2789. return true;
  2790. } else if (property->flags & DRM_MODE_PROP_BITMASK) {
  2791. int i;
  2792. uint64_t valid_mask = 0;
  2793. for (i = 0; i < property->num_values; i++)
  2794. valid_mask |= (1ULL << property->values[i]);
  2795. return !(value & ~valid_mask);
  2796. } else if (property->flags & DRM_MODE_PROP_BLOB) {
  2797. /* Only the driver knows */
  2798. return true;
  2799. } else {
  2800. int i;
  2801. for (i = 0; i < property->num_values; i++)
  2802. if (property->values[i] == value)
  2803. return true;
  2804. return false;
  2805. }
  2806. }
  2807. int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
  2808. void *data, struct drm_file *file_priv)
  2809. {
  2810. struct drm_mode_connector_set_property *conn_set_prop = data;
  2811. struct drm_mode_obj_set_property obj_set_prop = {
  2812. .value = conn_set_prop->value,
  2813. .prop_id = conn_set_prop->prop_id,
  2814. .obj_id = conn_set_prop->connector_id,
  2815. .obj_type = DRM_MODE_OBJECT_CONNECTOR
  2816. };
  2817. /* It does all the locking and checking we need */
  2818. return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
  2819. }
  2820. static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
  2821. struct drm_property *property,
  2822. uint64_t value)
  2823. {
  2824. int ret = -EINVAL;
  2825. struct drm_connector *connector = obj_to_connector(obj);
  2826. /* Do DPMS ourselves */
  2827. if (property == connector->dev->mode_config.dpms_property) {
  2828. if (connector->funcs->dpms)
  2829. (*connector->funcs->dpms)(connector, (int)value);
  2830. ret = 0;
  2831. } else if (connector->funcs->set_property)
  2832. ret = connector->funcs->set_property(connector, property, value);
  2833. /* store the property value if successful */
  2834. if (!ret)
  2835. drm_object_property_set_value(&connector->base, property, value);
  2836. return ret;
  2837. }
  2838. static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
  2839. struct drm_property *property,
  2840. uint64_t value)
  2841. {
  2842. int ret = -EINVAL;
  2843. struct drm_crtc *crtc = obj_to_crtc(obj);
  2844. if (crtc->funcs->set_property)
  2845. ret = crtc->funcs->set_property(crtc, property, value);
  2846. if (!ret)
  2847. drm_object_property_set_value(obj, property, value);
  2848. return ret;
  2849. }
  2850. static int drm_mode_plane_set_obj_prop(struct drm_mode_object *obj,
  2851. struct drm_property *property,
  2852. uint64_t value)
  2853. {
  2854. int ret = -EINVAL;
  2855. struct drm_plane *plane = obj_to_plane(obj);
  2856. if (plane->funcs->set_property)
  2857. ret = plane->funcs->set_property(plane, property, value);
  2858. if (!ret)
  2859. drm_object_property_set_value(obj, property, value);
  2860. return ret;
  2861. }
  2862. int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
  2863. struct drm_file *file_priv)
  2864. {
  2865. struct drm_mode_obj_get_properties *arg = data;
  2866. struct drm_mode_object *obj;
  2867. int ret = 0;
  2868. int i;
  2869. int copied = 0;
  2870. int props_count = 0;
  2871. uint32_t __user *props_ptr;
  2872. uint64_t __user *prop_values_ptr;
  2873. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2874. return -EINVAL;
  2875. drm_modeset_lock_all(dev);
  2876. obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
  2877. if (!obj) {
  2878. ret = -EINVAL;
  2879. goto out;
  2880. }
  2881. if (!obj->properties) {
  2882. ret = -EINVAL;
  2883. goto out;
  2884. }
  2885. props_count = obj->properties->count;
  2886. /* This ioctl is called twice, once to determine how much space is
  2887. * needed, and the 2nd time to fill it. */
  2888. if ((arg->count_props >= props_count) && props_count) {
  2889. copied = 0;
  2890. props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
  2891. prop_values_ptr = (uint64_t __user *)(unsigned long)
  2892. (arg->prop_values_ptr);
  2893. for (i = 0; i < props_count; i++) {
  2894. if (put_user(obj->properties->ids[i],
  2895. props_ptr + copied)) {
  2896. ret = -EFAULT;
  2897. goto out;
  2898. }
  2899. if (put_user(obj->properties->values[i],
  2900. prop_values_ptr + copied)) {
  2901. ret = -EFAULT;
  2902. goto out;
  2903. }
  2904. copied++;
  2905. }
  2906. }
  2907. arg->count_props = props_count;
  2908. out:
  2909. drm_modeset_unlock_all(dev);
  2910. return ret;
  2911. }
  2912. int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
  2913. struct drm_file *file_priv)
  2914. {
  2915. struct drm_mode_obj_set_property *arg = data;
  2916. struct drm_mode_object *arg_obj;
  2917. struct drm_mode_object *prop_obj;
  2918. struct drm_property *property;
  2919. int ret = -EINVAL;
  2920. int i;
  2921. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2922. return -EINVAL;
  2923. drm_modeset_lock_all(dev);
  2924. arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
  2925. if (!arg_obj)
  2926. goto out;
  2927. if (!arg_obj->properties)
  2928. goto out;
  2929. for (i = 0; i < arg_obj->properties->count; i++)
  2930. if (arg_obj->properties->ids[i] == arg->prop_id)
  2931. break;
  2932. if (i == arg_obj->properties->count)
  2933. goto out;
  2934. prop_obj = drm_mode_object_find(dev, arg->prop_id,
  2935. DRM_MODE_OBJECT_PROPERTY);
  2936. if (!prop_obj)
  2937. goto out;
  2938. property = obj_to_property(prop_obj);
  2939. if (!drm_property_change_is_valid(property, arg->value))
  2940. goto out;
  2941. switch (arg_obj->type) {
  2942. case DRM_MODE_OBJECT_CONNECTOR:
  2943. ret = drm_mode_connector_set_obj_prop(arg_obj, property,
  2944. arg->value);
  2945. break;
  2946. case DRM_MODE_OBJECT_CRTC:
  2947. ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
  2948. break;
  2949. case DRM_MODE_OBJECT_PLANE:
  2950. ret = drm_mode_plane_set_obj_prop(arg_obj, property, arg->value);
  2951. break;
  2952. }
  2953. out:
  2954. drm_modeset_unlock_all(dev);
  2955. return ret;
  2956. }
  2957. int drm_mode_connector_attach_encoder(struct drm_connector *connector,
  2958. struct drm_encoder *encoder)
  2959. {
  2960. int i;
  2961. for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
  2962. if (connector->encoder_ids[i] == 0) {
  2963. connector->encoder_ids[i] = encoder->base.id;
  2964. return 0;
  2965. }
  2966. }
  2967. return -ENOMEM;
  2968. }
  2969. EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
  2970. void drm_mode_connector_detach_encoder(struct drm_connector *connector,
  2971. struct drm_encoder *encoder)
  2972. {
  2973. int i;
  2974. for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
  2975. if (connector->encoder_ids[i] == encoder->base.id) {
  2976. connector->encoder_ids[i] = 0;
  2977. if (connector->encoder == encoder)
  2978. connector->encoder = NULL;
  2979. break;
  2980. }
  2981. }
  2982. }
  2983. EXPORT_SYMBOL(drm_mode_connector_detach_encoder);
  2984. int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
  2985. int gamma_size)
  2986. {
  2987. crtc->gamma_size = gamma_size;
  2988. crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
  2989. if (!crtc->gamma_store) {
  2990. crtc->gamma_size = 0;
  2991. return -ENOMEM;
  2992. }
  2993. return 0;
  2994. }
  2995. EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
  2996. int drm_mode_gamma_set_ioctl(struct drm_device *dev,
  2997. void *data, struct drm_file *file_priv)
  2998. {
  2999. struct drm_mode_crtc_lut *crtc_lut = data;
  3000. struct drm_mode_object *obj;
  3001. struct drm_crtc *crtc;
  3002. void *r_base, *g_base, *b_base;
  3003. int size;
  3004. int ret = 0;
  3005. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  3006. return -EINVAL;
  3007. drm_modeset_lock_all(dev);
  3008. obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
  3009. if (!obj) {
  3010. ret = -EINVAL;
  3011. goto out;
  3012. }
  3013. crtc = obj_to_crtc(obj);
  3014. if (crtc->funcs->gamma_set == NULL) {
  3015. ret = -ENOSYS;
  3016. goto out;
  3017. }
  3018. /* memcpy into gamma store */
  3019. if (crtc_lut->gamma_size != crtc->gamma_size) {
  3020. ret = -EINVAL;
  3021. goto out;
  3022. }
  3023. size = crtc_lut->gamma_size * (sizeof(uint16_t));
  3024. r_base = crtc->gamma_store;
  3025. if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
  3026. ret = -EFAULT;
  3027. goto out;
  3028. }
  3029. g_base = r_base + size;
  3030. if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
  3031. ret = -EFAULT;
  3032. goto out;
  3033. }
  3034. b_base = g_base + size;
  3035. if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
  3036. ret = -EFAULT;
  3037. goto out;
  3038. }
  3039. crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
  3040. out:
  3041. drm_modeset_unlock_all(dev);
  3042. return ret;
  3043. }
  3044. int drm_mode_gamma_get_ioctl(struct drm_device *dev,
  3045. void *data, struct drm_file *file_priv)
  3046. {
  3047. struct drm_mode_crtc_lut *crtc_lut = data;
  3048. struct drm_mode_object *obj;
  3049. struct drm_crtc *crtc;
  3050. void *r_base, *g_base, *b_base;
  3051. int size;
  3052. int ret = 0;
  3053. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  3054. return -EINVAL;
  3055. drm_modeset_lock_all(dev);
  3056. obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
  3057. if (!obj) {
  3058. ret = -EINVAL;
  3059. goto out;
  3060. }
  3061. crtc = obj_to_crtc(obj);
  3062. /* memcpy into gamma store */
  3063. if (crtc_lut->gamma_size != crtc->gamma_size) {
  3064. ret = -EINVAL;
  3065. goto out;
  3066. }
  3067. size = crtc_lut->gamma_size * (sizeof(uint16_t));
  3068. r_base = crtc->gamma_store;
  3069. if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
  3070. ret = -EFAULT;
  3071. goto out;
  3072. }
  3073. g_base = r_base + size;
  3074. if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
  3075. ret = -EFAULT;
  3076. goto out;
  3077. }
  3078. b_base = g_base + size;
  3079. if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
  3080. ret = -EFAULT;
  3081. goto out;
  3082. }
  3083. out:
  3084. drm_modeset_unlock_all(dev);
  3085. return ret;
  3086. }
  3087. int drm_mode_page_flip_ioctl(struct drm_device *dev,
  3088. void *data, struct drm_file *file_priv)
  3089. {
  3090. struct drm_mode_crtc_page_flip *page_flip = data;
  3091. struct drm_mode_object *obj;
  3092. struct drm_crtc *crtc;
  3093. struct drm_framebuffer *fb = NULL, *old_fb = NULL;
  3094. struct drm_pending_vblank_event *e = NULL;
  3095. unsigned long flags;
  3096. int ret = -EINVAL;
  3097. if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
  3098. page_flip->reserved != 0)
  3099. return -EINVAL;
  3100. if ((page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC) && !dev->mode_config.async_page_flip)
  3101. return -EINVAL;
  3102. obj = drm_mode_object_find(dev, page_flip->crtc_id, DRM_MODE_OBJECT_CRTC);
  3103. if (!obj)
  3104. return -EINVAL;
  3105. crtc = obj_to_crtc(obj);
  3106. mutex_lock(&crtc->mutex);
  3107. if (crtc->fb == NULL) {
  3108. /* The framebuffer is currently unbound, presumably
  3109. * due to a hotplug event, that userspace has not
  3110. * yet discovered.
  3111. */
  3112. ret = -EBUSY;
  3113. goto out;
  3114. }
  3115. if (crtc->funcs->page_flip == NULL)
  3116. goto out;
  3117. fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
  3118. if (!fb)
  3119. goto out;
  3120. ret = drm_crtc_check_viewport(crtc, crtc->x, crtc->y, &crtc->mode, fb);
  3121. if (ret)
  3122. goto out;
  3123. if (crtc->fb->pixel_format != fb->pixel_format) {
  3124. DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
  3125. ret = -EINVAL;
  3126. goto out;
  3127. }
  3128. if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
  3129. ret = -ENOMEM;
  3130. spin_lock_irqsave(&dev->event_lock, flags);
  3131. if (file_priv->event_space < sizeof e->event) {
  3132. spin_unlock_irqrestore(&dev->event_lock, flags);
  3133. goto out;
  3134. }
  3135. file_priv->event_space -= sizeof e->event;
  3136. spin_unlock_irqrestore(&dev->event_lock, flags);
  3137. e = kzalloc(sizeof *e, GFP_KERNEL);
  3138. if (e == NULL) {
  3139. spin_lock_irqsave(&dev->event_lock, flags);
  3140. file_priv->event_space += sizeof e->event;
  3141. spin_unlock_irqrestore(&dev->event_lock, flags);
  3142. goto out;
  3143. }
  3144. e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
  3145. e->event.base.length = sizeof e->event;
  3146. e->event.user_data = page_flip->user_data;
  3147. e->base.event = &e->event.base;
  3148. e->base.file_priv = file_priv;
  3149. e->base.destroy =
  3150. (void (*) (struct drm_pending_event *)) kfree;
  3151. }
  3152. old_fb = crtc->fb;
  3153. ret = crtc->funcs->page_flip(crtc, fb, e, page_flip->flags);
  3154. if (ret) {
  3155. if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
  3156. spin_lock_irqsave(&dev->event_lock, flags);
  3157. file_priv->event_space += sizeof e->event;
  3158. spin_unlock_irqrestore(&dev->event_lock, flags);
  3159. kfree(e);
  3160. }
  3161. /* Keep the old fb, don't unref it. */
  3162. old_fb = NULL;
  3163. } else {
  3164. /*
  3165. * Warn if the driver hasn't properly updated the crtc->fb
  3166. * field to reflect that the new framebuffer is now used.
  3167. * Failing to do so will screw with the reference counting
  3168. * on framebuffers.
  3169. */
  3170. WARN_ON(crtc->fb != fb);
  3171. /* Unref only the old framebuffer. */
  3172. fb = NULL;
  3173. }
  3174. out:
  3175. if (fb)
  3176. drm_framebuffer_unreference(fb);
  3177. if (old_fb)
  3178. drm_framebuffer_unreference(old_fb);
  3179. mutex_unlock(&crtc->mutex);
  3180. return ret;
  3181. }
  3182. void drm_mode_config_reset(struct drm_device *dev)
  3183. {
  3184. struct drm_crtc *crtc;
  3185. struct drm_encoder *encoder;
  3186. struct drm_connector *connector;
  3187. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
  3188. if (crtc->funcs->reset)
  3189. crtc->funcs->reset(crtc);
  3190. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
  3191. if (encoder->funcs->reset)
  3192. encoder->funcs->reset(encoder);
  3193. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  3194. connector->status = connector_status_unknown;
  3195. if (connector->funcs->reset)
  3196. connector->funcs->reset(connector);
  3197. }
  3198. }
  3199. EXPORT_SYMBOL(drm_mode_config_reset);
  3200. int drm_mode_create_dumb_ioctl(struct drm_device *dev,
  3201. void *data, struct drm_file *file_priv)
  3202. {
  3203. struct drm_mode_create_dumb *args = data;
  3204. if (!dev->driver->dumb_create)
  3205. return -ENOSYS;
  3206. return dev->driver->dumb_create(file_priv, dev, args);
  3207. }
  3208. int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
  3209. void *data, struct drm_file *file_priv)
  3210. {
  3211. struct drm_mode_map_dumb *args = data;
  3212. /* call driver ioctl to get mmap offset */
  3213. if (!dev->driver->dumb_map_offset)
  3214. return -ENOSYS;
  3215. return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
  3216. }
  3217. int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
  3218. void *data, struct drm_file *file_priv)
  3219. {
  3220. struct drm_mode_destroy_dumb *args = data;
  3221. if (!dev->driver->dumb_destroy)
  3222. return -ENOSYS;
  3223. return dev->driver->dumb_destroy(file_priv, dev, args->handle);
  3224. }
  3225. /*
  3226. * Just need to support RGB formats here for compat with code that doesn't
  3227. * use pixel formats directly yet.
  3228. */
  3229. void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
  3230. int *bpp)
  3231. {
  3232. switch (format) {
  3233. case DRM_FORMAT_C8:
  3234. case DRM_FORMAT_RGB332:
  3235. case DRM_FORMAT_BGR233:
  3236. *depth = 8;
  3237. *bpp = 8;
  3238. break;
  3239. case DRM_FORMAT_XRGB1555:
  3240. case DRM_FORMAT_XBGR1555:
  3241. case DRM_FORMAT_RGBX5551:
  3242. case DRM_FORMAT_BGRX5551:
  3243. case DRM_FORMAT_ARGB1555:
  3244. case DRM_FORMAT_ABGR1555:
  3245. case DRM_FORMAT_RGBA5551:
  3246. case DRM_FORMAT_BGRA5551:
  3247. *depth = 15;
  3248. *bpp = 16;
  3249. break;
  3250. case DRM_FORMAT_RGB565:
  3251. case DRM_FORMAT_BGR565:
  3252. *depth = 16;
  3253. *bpp = 16;
  3254. break;
  3255. case DRM_FORMAT_RGB888:
  3256. case DRM_FORMAT_BGR888:
  3257. *depth = 24;
  3258. *bpp = 24;
  3259. break;
  3260. case DRM_FORMAT_XRGB8888:
  3261. case DRM_FORMAT_XBGR8888:
  3262. case DRM_FORMAT_RGBX8888:
  3263. case DRM_FORMAT_BGRX8888:
  3264. *depth = 24;
  3265. *bpp = 32;
  3266. break;
  3267. case DRM_FORMAT_XRGB2101010:
  3268. case DRM_FORMAT_XBGR2101010:
  3269. case DRM_FORMAT_RGBX1010102:
  3270. case DRM_FORMAT_BGRX1010102:
  3271. case DRM_FORMAT_ARGB2101010:
  3272. case DRM_FORMAT_ABGR2101010:
  3273. case DRM_FORMAT_RGBA1010102:
  3274. case DRM_FORMAT_BGRA1010102:
  3275. *depth = 30;
  3276. *bpp = 32;
  3277. break;
  3278. case DRM_FORMAT_ARGB8888:
  3279. case DRM_FORMAT_ABGR8888:
  3280. case DRM_FORMAT_RGBA8888:
  3281. case DRM_FORMAT_BGRA8888:
  3282. *depth = 32;
  3283. *bpp = 32;
  3284. break;
  3285. default:
  3286. DRM_DEBUG_KMS("unsupported pixel format\n");
  3287. *depth = 0;
  3288. *bpp = 0;
  3289. break;
  3290. }
  3291. }
  3292. EXPORT_SYMBOL(drm_fb_get_bpp_depth);
  3293. /**
  3294. * drm_format_num_planes - get the number of planes for format
  3295. * @format: pixel format (DRM_FORMAT_*)
  3296. *
  3297. * RETURNS:
  3298. * The number of planes used by the specified pixel format.
  3299. */
  3300. int drm_format_num_planes(uint32_t format)
  3301. {
  3302. switch (format) {
  3303. case DRM_FORMAT_YUV410:
  3304. case DRM_FORMAT_YVU410:
  3305. case DRM_FORMAT_YUV411:
  3306. case DRM_FORMAT_YVU411:
  3307. case DRM_FORMAT_YUV420:
  3308. case DRM_FORMAT_YVU420:
  3309. case DRM_FORMAT_YUV422:
  3310. case DRM_FORMAT_YVU422:
  3311. case DRM_FORMAT_YUV444:
  3312. case DRM_FORMAT_YVU444:
  3313. return 3;
  3314. case DRM_FORMAT_NV12:
  3315. case DRM_FORMAT_NV21:
  3316. case DRM_FORMAT_NV16:
  3317. case DRM_FORMAT_NV61:
  3318. case DRM_FORMAT_NV24:
  3319. case DRM_FORMAT_NV42:
  3320. return 2;
  3321. default:
  3322. return 1;
  3323. }
  3324. }
  3325. EXPORT_SYMBOL(drm_format_num_planes);
  3326. /**
  3327. * drm_format_plane_cpp - determine the bytes per pixel value
  3328. * @format: pixel format (DRM_FORMAT_*)
  3329. * @plane: plane index
  3330. *
  3331. * RETURNS:
  3332. * The bytes per pixel value for the specified plane.
  3333. */
  3334. int drm_format_plane_cpp(uint32_t format, int plane)
  3335. {
  3336. unsigned int depth;
  3337. int bpp;
  3338. if (plane >= drm_format_num_planes(format))
  3339. return 0;
  3340. switch (format) {
  3341. case DRM_FORMAT_YUYV:
  3342. case DRM_FORMAT_YVYU:
  3343. case DRM_FORMAT_UYVY:
  3344. case DRM_FORMAT_VYUY:
  3345. return 2;
  3346. case DRM_FORMAT_NV12:
  3347. case DRM_FORMAT_NV21:
  3348. case DRM_FORMAT_NV16:
  3349. case DRM_FORMAT_NV61:
  3350. case DRM_FORMAT_NV24:
  3351. case DRM_FORMAT_NV42:
  3352. return plane ? 2 : 1;
  3353. case DRM_FORMAT_YUV410:
  3354. case DRM_FORMAT_YVU410:
  3355. case DRM_FORMAT_YUV411:
  3356. case DRM_FORMAT_YVU411:
  3357. case DRM_FORMAT_YUV420:
  3358. case DRM_FORMAT_YVU420:
  3359. case DRM_FORMAT_YUV422:
  3360. case DRM_FORMAT_YVU422:
  3361. case DRM_FORMAT_YUV444:
  3362. case DRM_FORMAT_YVU444:
  3363. return 1;
  3364. default:
  3365. drm_fb_get_bpp_depth(format, &depth, &bpp);
  3366. return bpp >> 3;
  3367. }
  3368. }
  3369. EXPORT_SYMBOL(drm_format_plane_cpp);
  3370. /**
  3371. * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
  3372. * @format: pixel format (DRM_FORMAT_*)
  3373. *
  3374. * RETURNS:
  3375. * The horizontal chroma subsampling factor for the
  3376. * specified pixel format.
  3377. */
  3378. int drm_format_horz_chroma_subsampling(uint32_t format)
  3379. {
  3380. switch (format) {
  3381. case DRM_FORMAT_YUV411:
  3382. case DRM_FORMAT_YVU411:
  3383. case DRM_FORMAT_YUV410:
  3384. case DRM_FORMAT_YVU410:
  3385. return 4;
  3386. case DRM_FORMAT_YUYV:
  3387. case DRM_FORMAT_YVYU:
  3388. case DRM_FORMAT_UYVY:
  3389. case DRM_FORMAT_VYUY:
  3390. case DRM_FORMAT_NV12:
  3391. case DRM_FORMAT_NV21:
  3392. case DRM_FORMAT_NV16:
  3393. case DRM_FORMAT_NV61:
  3394. case DRM_FORMAT_YUV422:
  3395. case DRM_FORMAT_YVU422:
  3396. case DRM_FORMAT_YUV420:
  3397. case DRM_FORMAT_YVU420:
  3398. return 2;
  3399. default:
  3400. return 1;
  3401. }
  3402. }
  3403. EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
  3404. /**
  3405. * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
  3406. * @format: pixel format (DRM_FORMAT_*)
  3407. *
  3408. * RETURNS:
  3409. * The vertical chroma subsampling factor for the
  3410. * specified pixel format.
  3411. */
  3412. int drm_format_vert_chroma_subsampling(uint32_t format)
  3413. {
  3414. switch (format) {
  3415. case DRM_FORMAT_YUV410:
  3416. case DRM_FORMAT_YVU410:
  3417. return 4;
  3418. case DRM_FORMAT_YUV420:
  3419. case DRM_FORMAT_YVU420:
  3420. case DRM_FORMAT_NV12:
  3421. case DRM_FORMAT_NV21:
  3422. return 2;
  3423. default:
  3424. return 1;
  3425. }
  3426. }
  3427. EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);
  3428. /**
  3429. * drm_mode_config_init - initialize DRM mode_configuration structure
  3430. * @dev: DRM device
  3431. *
  3432. * Initialize @dev's mode_config structure, used for tracking the graphics
  3433. * configuration of @dev.
  3434. *
  3435. * Since this initializes the modeset locks, no locking is possible. Which is no
  3436. * problem, since this should happen single threaded at init time. It is the
  3437. * driver's problem to ensure this guarantee.
  3438. *
  3439. */
  3440. void drm_mode_config_init(struct drm_device *dev)
  3441. {
  3442. mutex_init(&dev->mode_config.mutex);
  3443. mutex_init(&dev->mode_config.idr_mutex);
  3444. mutex_init(&dev->mode_config.fb_lock);
  3445. INIT_LIST_HEAD(&dev->mode_config.fb_list);
  3446. INIT_LIST_HEAD(&dev->mode_config.crtc_list);
  3447. INIT_LIST_HEAD(&dev->mode_config.connector_list);
  3448. INIT_LIST_HEAD(&dev->mode_config.bridge_list);
  3449. INIT_LIST_HEAD(&dev->mode_config.encoder_list);
  3450. INIT_LIST_HEAD(&dev->mode_config.property_list);
  3451. INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
  3452. INIT_LIST_HEAD(&dev->mode_config.plane_list);
  3453. idr_init(&dev->mode_config.crtc_idr);
  3454. drm_modeset_lock_all(dev);
  3455. drm_mode_create_standard_connector_properties(dev);
  3456. drm_modeset_unlock_all(dev);
  3457. /* Just to be sure */
  3458. dev->mode_config.num_fb = 0;
  3459. dev->mode_config.num_connector = 0;
  3460. dev->mode_config.num_crtc = 0;
  3461. dev->mode_config.num_encoder = 0;
  3462. }
  3463. EXPORT_SYMBOL(drm_mode_config_init);
  3464. /**
  3465. * drm_mode_config_cleanup - free up DRM mode_config info
  3466. * @dev: DRM device
  3467. *
  3468. * Free up all the connectors and CRTCs associated with this DRM device, then
  3469. * free up the framebuffers and associated buffer objects.
  3470. *
  3471. * Note that since this /should/ happen single-threaded at driver/device
  3472. * teardown time, no locking is required. It's the driver's job to ensure that
  3473. * this guarantee actually holds true.
  3474. *
  3475. * FIXME: cleanup any dangling user buffer objects too
  3476. */
  3477. void drm_mode_config_cleanup(struct drm_device *dev)
  3478. {
  3479. struct drm_connector *connector, *ot;
  3480. struct drm_crtc *crtc, *ct;
  3481. struct drm_encoder *encoder, *enct;
  3482. struct drm_bridge *bridge, *brt;
  3483. struct drm_framebuffer *fb, *fbt;
  3484. struct drm_property *property, *pt;
  3485. struct drm_property_blob *blob, *bt;
  3486. struct drm_plane *plane, *plt;
  3487. list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
  3488. head) {
  3489. encoder->funcs->destroy(encoder);
  3490. }
  3491. list_for_each_entry_safe(bridge, brt,
  3492. &dev->mode_config.bridge_list, head) {
  3493. bridge->funcs->destroy(bridge);
  3494. }
  3495. list_for_each_entry_safe(connector, ot,
  3496. &dev->mode_config.connector_list, head) {
  3497. connector->funcs->destroy(connector);
  3498. }
  3499. list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
  3500. head) {
  3501. drm_property_destroy(dev, property);
  3502. }
  3503. list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
  3504. head) {
  3505. drm_property_destroy_blob(dev, blob);
  3506. }
  3507. /*
  3508. * Single-threaded teardown context, so it's not required to grab the
  3509. * fb_lock to protect against concurrent fb_list access. Contrary, it
  3510. * would actually deadlock with the drm_framebuffer_cleanup function.
  3511. *
  3512. * Also, if there are any framebuffers left, that's a driver leak now,
  3513. * so politely WARN about this.
  3514. */
  3515. WARN_ON(!list_empty(&dev->mode_config.fb_list));
  3516. list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
  3517. drm_framebuffer_remove(fb);
  3518. }
  3519. list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
  3520. head) {
  3521. plane->funcs->destroy(plane);
  3522. }
  3523. list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
  3524. crtc->funcs->destroy(crtc);
  3525. }
  3526. idr_destroy(&dev->mode_config.crtc_idr);
  3527. }
  3528. EXPORT_SYMBOL(drm_mode_config_cleanup);