drm_crtc.c 104 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083
  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. out->clock = in->clock;
  1132. out->hdisplay = in->hdisplay;
  1133. out->hsync_start = in->hsync_start;
  1134. out->hsync_end = in->hsync_end;
  1135. out->htotal = in->htotal;
  1136. out->hskew = in->hskew;
  1137. out->vdisplay = in->vdisplay;
  1138. out->vsync_start = in->vsync_start;
  1139. out->vsync_end = in->vsync_end;
  1140. out->vtotal = in->vtotal;
  1141. out->vscan = in->vscan;
  1142. out->vrefresh = in->vrefresh;
  1143. out->flags = in->flags;
  1144. out->type = in->type;
  1145. strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
  1146. out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
  1147. return 0;
  1148. }
  1149. /**
  1150. * drm_mode_getresources - get graphics configuration
  1151. * @dev: drm device for the ioctl
  1152. * @data: data pointer for the ioctl
  1153. * @file_priv: drm file for the ioctl call
  1154. *
  1155. * Construct a set of configuration description structures and return
  1156. * them to the user, including CRTC, connector and framebuffer configuration.
  1157. *
  1158. * Called by the user via ioctl.
  1159. *
  1160. * RETURNS:
  1161. * Zero on success, errno on failure.
  1162. */
  1163. int drm_mode_getresources(struct drm_device *dev, void *data,
  1164. struct drm_file *file_priv)
  1165. {
  1166. struct drm_mode_card_res *card_res = data;
  1167. struct list_head *lh;
  1168. struct drm_framebuffer *fb;
  1169. struct drm_connector *connector;
  1170. struct drm_crtc *crtc;
  1171. struct drm_encoder *encoder;
  1172. int ret = 0;
  1173. int connector_count = 0;
  1174. int crtc_count = 0;
  1175. int fb_count = 0;
  1176. int encoder_count = 0;
  1177. int copied = 0, i;
  1178. uint32_t __user *fb_id;
  1179. uint32_t __user *crtc_id;
  1180. uint32_t __user *connector_id;
  1181. uint32_t __user *encoder_id;
  1182. struct drm_mode_group *mode_group;
  1183. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1184. return -EINVAL;
  1185. mutex_lock(&file_priv->fbs_lock);
  1186. /*
  1187. * For the non-control nodes we need to limit the list of resources
  1188. * by IDs in the group list for this node
  1189. */
  1190. list_for_each(lh, &file_priv->fbs)
  1191. fb_count++;
  1192. /* handle this in 4 parts */
  1193. /* FBs */
  1194. if (card_res->count_fbs >= fb_count) {
  1195. copied = 0;
  1196. fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
  1197. list_for_each_entry(fb, &file_priv->fbs, filp_head) {
  1198. if (put_user(fb->base.id, fb_id + copied)) {
  1199. mutex_unlock(&file_priv->fbs_lock);
  1200. return -EFAULT;
  1201. }
  1202. copied++;
  1203. }
  1204. }
  1205. card_res->count_fbs = fb_count;
  1206. mutex_unlock(&file_priv->fbs_lock);
  1207. drm_modeset_lock_all(dev);
  1208. mode_group = &file_priv->master->minor->mode_group;
  1209. if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
  1210. list_for_each(lh, &dev->mode_config.crtc_list)
  1211. crtc_count++;
  1212. list_for_each(lh, &dev->mode_config.connector_list)
  1213. connector_count++;
  1214. list_for_each(lh, &dev->mode_config.encoder_list)
  1215. encoder_count++;
  1216. } else {
  1217. crtc_count = mode_group->num_crtcs;
  1218. connector_count = mode_group->num_connectors;
  1219. encoder_count = mode_group->num_encoders;
  1220. }
  1221. card_res->max_height = dev->mode_config.max_height;
  1222. card_res->min_height = dev->mode_config.min_height;
  1223. card_res->max_width = dev->mode_config.max_width;
  1224. card_res->min_width = dev->mode_config.min_width;
  1225. /* CRTCs */
  1226. if (card_res->count_crtcs >= crtc_count) {
  1227. copied = 0;
  1228. crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
  1229. if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
  1230. list_for_each_entry(crtc, &dev->mode_config.crtc_list,
  1231. head) {
  1232. DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
  1233. if (put_user(crtc->base.id, crtc_id + copied)) {
  1234. ret = -EFAULT;
  1235. goto out;
  1236. }
  1237. copied++;
  1238. }
  1239. } else {
  1240. for (i = 0; i < mode_group->num_crtcs; i++) {
  1241. if (put_user(mode_group->id_list[i],
  1242. crtc_id + copied)) {
  1243. ret = -EFAULT;
  1244. goto out;
  1245. }
  1246. copied++;
  1247. }
  1248. }
  1249. }
  1250. card_res->count_crtcs = crtc_count;
  1251. /* Encoders */
  1252. if (card_res->count_encoders >= encoder_count) {
  1253. copied = 0;
  1254. encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
  1255. if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
  1256. list_for_each_entry(encoder,
  1257. &dev->mode_config.encoder_list,
  1258. head) {
  1259. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
  1260. drm_get_encoder_name(encoder));
  1261. if (put_user(encoder->base.id, encoder_id +
  1262. copied)) {
  1263. ret = -EFAULT;
  1264. goto out;
  1265. }
  1266. copied++;
  1267. }
  1268. } else {
  1269. for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
  1270. if (put_user(mode_group->id_list[i],
  1271. encoder_id + copied)) {
  1272. ret = -EFAULT;
  1273. goto out;
  1274. }
  1275. copied++;
  1276. }
  1277. }
  1278. }
  1279. card_res->count_encoders = encoder_count;
  1280. /* Connectors */
  1281. if (card_res->count_connectors >= connector_count) {
  1282. copied = 0;
  1283. connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
  1284. if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
  1285. list_for_each_entry(connector,
  1286. &dev->mode_config.connector_list,
  1287. head) {
  1288. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  1289. connector->base.id,
  1290. drm_get_connector_name(connector));
  1291. if (put_user(connector->base.id,
  1292. connector_id + copied)) {
  1293. ret = -EFAULT;
  1294. goto out;
  1295. }
  1296. copied++;
  1297. }
  1298. } else {
  1299. int start = mode_group->num_crtcs +
  1300. mode_group->num_encoders;
  1301. for (i = start; i < start + mode_group->num_connectors; i++) {
  1302. if (put_user(mode_group->id_list[i],
  1303. connector_id + copied)) {
  1304. ret = -EFAULT;
  1305. goto out;
  1306. }
  1307. copied++;
  1308. }
  1309. }
  1310. }
  1311. card_res->count_connectors = connector_count;
  1312. DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
  1313. card_res->count_connectors, card_res->count_encoders);
  1314. out:
  1315. drm_modeset_unlock_all(dev);
  1316. return ret;
  1317. }
  1318. /**
  1319. * drm_mode_getcrtc - get CRTC configuration
  1320. * @dev: drm device for the ioctl
  1321. * @data: data pointer for the ioctl
  1322. * @file_priv: drm file for the ioctl call
  1323. *
  1324. * Construct a CRTC configuration structure to return to the user.
  1325. *
  1326. * Called by the user via ioctl.
  1327. *
  1328. * RETURNS:
  1329. * Zero on success, errno on failure.
  1330. */
  1331. int drm_mode_getcrtc(struct drm_device *dev,
  1332. void *data, struct drm_file *file_priv)
  1333. {
  1334. struct drm_mode_crtc *crtc_resp = data;
  1335. struct drm_crtc *crtc;
  1336. struct drm_mode_object *obj;
  1337. int ret = 0;
  1338. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1339. return -EINVAL;
  1340. drm_modeset_lock_all(dev);
  1341. obj = drm_mode_object_find(dev, crtc_resp->crtc_id,
  1342. DRM_MODE_OBJECT_CRTC);
  1343. if (!obj) {
  1344. ret = -EINVAL;
  1345. goto out;
  1346. }
  1347. crtc = obj_to_crtc(obj);
  1348. crtc_resp->x = crtc->x;
  1349. crtc_resp->y = crtc->y;
  1350. crtc_resp->gamma_size = crtc->gamma_size;
  1351. if (crtc->fb)
  1352. crtc_resp->fb_id = crtc->fb->base.id;
  1353. else
  1354. crtc_resp->fb_id = 0;
  1355. if (crtc->enabled) {
  1356. drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
  1357. crtc_resp->mode_valid = 1;
  1358. } else {
  1359. crtc_resp->mode_valid = 0;
  1360. }
  1361. out:
  1362. drm_modeset_unlock_all(dev);
  1363. return ret;
  1364. }
  1365. static bool drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
  1366. const struct drm_file *file_priv)
  1367. {
  1368. /*
  1369. * If user-space hasn't configured the driver to expose the stereo 3D
  1370. * modes, don't expose them.
  1371. */
  1372. if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
  1373. return false;
  1374. return true;
  1375. }
  1376. /**
  1377. * drm_mode_getconnector - get connector configuration
  1378. * @dev: drm device for the ioctl
  1379. * @data: data pointer for the ioctl
  1380. * @file_priv: drm file for the ioctl call
  1381. *
  1382. * Construct a connector configuration structure to return to the user.
  1383. *
  1384. * Called by the user via ioctl.
  1385. *
  1386. * RETURNS:
  1387. * Zero on success, errno on failure.
  1388. */
  1389. int drm_mode_getconnector(struct drm_device *dev, void *data,
  1390. struct drm_file *file_priv)
  1391. {
  1392. struct drm_mode_get_connector *out_resp = data;
  1393. struct drm_mode_object *obj;
  1394. struct drm_connector *connector;
  1395. struct drm_display_mode *mode;
  1396. int mode_count = 0;
  1397. int props_count = 0;
  1398. int encoders_count = 0;
  1399. int ret = 0;
  1400. int copied = 0;
  1401. int i;
  1402. struct drm_mode_modeinfo u_mode;
  1403. struct drm_mode_modeinfo __user *mode_ptr;
  1404. uint32_t __user *prop_ptr;
  1405. uint64_t __user *prop_values;
  1406. uint32_t __user *encoder_ptr;
  1407. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1408. return -EINVAL;
  1409. memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
  1410. DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
  1411. mutex_lock(&dev->mode_config.mutex);
  1412. obj = drm_mode_object_find(dev, out_resp->connector_id,
  1413. DRM_MODE_OBJECT_CONNECTOR);
  1414. if (!obj) {
  1415. ret = -EINVAL;
  1416. goto out;
  1417. }
  1418. connector = obj_to_connector(obj);
  1419. props_count = connector->properties.count;
  1420. for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
  1421. if (connector->encoder_ids[i] != 0) {
  1422. encoders_count++;
  1423. }
  1424. }
  1425. if (out_resp->count_modes == 0) {
  1426. connector->funcs->fill_modes(connector,
  1427. dev->mode_config.max_width,
  1428. dev->mode_config.max_height);
  1429. }
  1430. /* delayed so we get modes regardless of pre-fill_modes state */
  1431. list_for_each_entry(mode, &connector->modes, head)
  1432. if (drm_mode_expose_to_userspace(mode, file_priv))
  1433. mode_count++;
  1434. out_resp->connector_id = connector->base.id;
  1435. out_resp->connector_type = connector->connector_type;
  1436. out_resp->connector_type_id = connector->connector_type_id;
  1437. out_resp->mm_width = connector->display_info.width_mm;
  1438. out_resp->mm_height = connector->display_info.height_mm;
  1439. out_resp->subpixel = connector->display_info.subpixel_order;
  1440. out_resp->connection = connector->status;
  1441. if (connector->encoder)
  1442. out_resp->encoder_id = connector->encoder->base.id;
  1443. else
  1444. out_resp->encoder_id = 0;
  1445. /*
  1446. * This ioctl is called twice, once to determine how much space is
  1447. * needed, and the 2nd time to fill it.
  1448. */
  1449. if ((out_resp->count_modes >= mode_count) && mode_count) {
  1450. copied = 0;
  1451. mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
  1452. list_for_each_entry(mode, &connector->modes, head) {
  1453. if (!drm_mode_expose_to_userspace(mode, file_priv))
  1454. continue;
  1455. drm_crtc_convert_to_umode(&u_mode, mode);
  1456. if (copy_to_user(mode_ptr + copied,
  1457. &u_mode, sizeof(u_mode))) {
  1458. ret = -EFAULT;
  1459. goto out;
  1460. }
  1461. copied++;
  1462. }
  1463. }
  1464. out_resp->count_modes = mode_count;
  1465. if ((out_resp->count_props >= props_count) && props_count) {
  1466. copied = 0;
  1467. prop_ptr = (uint32_t __user *)(unsigned long)(out_resp->props_ptr);
  1468. prop_values = (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr);
  1469. for (i = 0; i < connector->properties.count; i++) {
  1470. if (put_user(connector->properties.ids[i],
  1471. prop_ptr + copied)) {
  1472. ret = -EFAULT;
  1473. goto out;
  1474. }
  1475. if (put_user(connector->properties.values[i],
  1476. prop_values + copied)) {
  1477. ret = -EFAULT;
  1478. goto out;
  1479. }
  1480. copied++;
  1481. }
  1482. }
  1483. out_resp->count_props = props_count;
  1484. if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
  1485. copied = 0;
  1486. encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
  1487. for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
  1488. if (connector->encoder_ids[i] != 0) {
  1489. if (put_user(connector->encoder_ids[i],
  1490. encoder_ptr + copied)) {
  1491. ret = -EFAULT;
  1492. goto out;
  1493. }
  1494. copied++;
  1495. }
  1496. }
  1497. }
  1498. out_resp->count_encoders = encoders_count;
  1499. out:
  1500. mutex_unlock(&dev->mode_config.mutex);
  1501. return ret;
  1502. }
  1503. int drm_mode_getencoder(struct drm_device *dev, void *data,
  1504. struct drm_file *file_priv)
  1505. {
  1506. struct drm_mode_get_encoder *enc_resp = data;
  1507. struct drm_mode_object *obj;
  1508. struct drm_encoder *encoder;
  1509. int ret = 0;
  1510. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1511. return -EINVAL;
  1512. drm_modeset_lock_all(dev);
  1513. obj = drm_mode_object_find(dev, enc_resp->encoder_id,
  1514. DRM_MODE_OBJECT_ENCODER);
  1515. if (!obj) {
  1516. ret = -EINVAL;
  1517. goto out;
  1518. }
  1519. encoder = obj_to_encoder(obj);
  1520. if (encoder->crtc)
  1521. enc_resp->crtc_id = encoder->crtc->base.id;
  1522. else
  1523. enc_resp->crtc_id = 0;
  1524. enc_resp->encoder_type = encoder->encoder_type;
  1525. enc_resp->encoder_id = encoder->base.id;
  1526. enc_resp->possible_crtcs = encoder->possible_crtcs;
  1527. enc_resp->possible_clones = encoder->possible_clones;
  1528. out:
  1529. drm_modeset_unlock_all(dev);
  1530. return ret;
  1531. }
  1532. /**
  1533. * drm_mode_getplane_res - get plane info
  1534. * @dev: DRM device
  1535. * @data: ioctl data
  1536. * @file_priv: DRM file info
  1537. *
  1538. * Return an plane count and set of IDs.
  1539. */
  1540. int drm_mode_getplane_res(struct drm_device *dev, void *data,
  1541. struct drm_file *file_priv)
  1542. {
  1543. struct drm_mode_get_plane_res *plane_resp = data;
  1544. struct drm_mode_config *config;
  1545. struct drm_plane *plane;
  1546. uint32_t __user *plane_ptr;
  1547. int copied = 0, ret = 0;
  1548. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1549. return -EINVAL;
  1550. drm_modeset_lock_all(dev);
  1551. config = &dev->mode_config;
  1552. /*
  1553. * This ioctl is called twice, once to determine how much space is
  1554. * needed, and the 2nd time to fill it.
  1555. */
  1556. if (config->num_plane &&
  1557. (plane_resp->count_planes >= config->num_plane)) {
  1558. plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
  1559. list_for_each_entry(plane, &config->plane_list, head) {
  1560. if (put_user(plane->base.id, plane_ptr + copied)) {
  1561. ret = -EFAULT;
  1562. goto out;
  1563. }
  1564. copied++;
  1565. }
  1566. }
  1567. plane_resp->count_planes = config->num_plane;
  1568. out:
  1569. drm_modeset_unlock_all(dev);
  1570. return ret;
  1571. }
  1572. /**
  1573. * drm_mode_getplane - get plane info
  1574. * @dev: DRM device
  1575. * @data: ioctl data
  1576. * @file_priv: DRM file info
  1577. *
  1578. * Return plane info, including formats supported, gamma size, any
  1579. * current fb, etc.
  1580. */
  1581. int drm_mode_getplane(struct drm_device *dev, void *data,
  1582. struct drm_file *file_priv)
  1583. {
  1584. struct drm_mode_get_plane *plane_resp = data;
  1585. struct drm_mode_object *obj;
  1586. struct drm_plane *plane;
  1587. uint32_t __user *format_ptr;
  1588. int ret = 0;
  1589. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1590. return -EINVAL;
  1591. drm_modeset_lock_all(dev);
  1592. obj = drm_mode_object_find(dev, plane_resp->plane_id,
  1593. DRM_MODE_OBJECT_PLANE);
  1594. if (!obj) {
  1595. ret = -ENOENT;
  1596. goto out;
  1597. }
  1598. plane = obj_to_plane(obj);
  1599. if (plane->crtc)
  1600. plane_resp->crtc_id = plane->crtc->base.id;
  1601. else
  1602. plane_resp->crtc_id = 0;
  1603. if (plane->fb)
  1604. plane_resp->fb_id = plane->fb->base.id;
  1605. else
  1606. plane_resp->fb_id = 0;
  1607. plane_resp->plane_id = plane->base.id;
  1608. plane_resp->possible_crtcs = plane->possible_crtcs;
  1609. plane_resp->gamma_size = 0;
  1610. /*
  1611. * This ioctl is called twice, once to determine how much space is
  1612. * needed, and the 2nd time to fill it.
  1613. */
  1614. if (plane->format_count &&
  1615. (plane_resp->count_format_types >= plane->format_count)) {
  1616. format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
  1617. if (copy_to_user(format_ptr,
  1618. plane->format_types,
  1619. sizeof(uint32_t) * plane->format_count)) {
  1620. ret = -EFAULT;
  1621. goto out;
  1622. }
  1623. }
  1624. plane_resp->count_format_types = plane->format_count;
  1625. out:
  1626. drm_modeset_unlock_all(dev);
  1627. return ret;
  1628. }
  1629. /**
  1630. * drm_mode_setplane - set up or tear down an plane
  1631. * @dev: DRM device
  1632. * @data: ioctl data*
  1633. * @file_priv: DRM file info
  1634. *
  1635. * Set plane info, including placement, fb, scaling, and other factors.
  1636. * Or pass a NULL fb to disable.
  1637. */
  1638. int drm_mode_setplane(struct drm_device *dev, void *data,
  1639. struct drm_file *file_priv)
  1640. {
  1641. struct drm_mode_set_plane *plane_req = data;
  1642. struct drm_mode_object *obj;
  1643. struct drm_plane *plane;
  1644. struct drm_crtc *crtc;
  1645. struct drm_framebuffer *fb = NULL, *old_fb = NULL;
  1646. int ret = 0;
  1647. unsigned int fb_width, fb_height;
  1648. int i;
  1649. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1650. return -EINVAL;
  1651. /*
  1652. * First, find the plane, crtc, and fb objects. If not available,
  1653. * we don't bother to call the driver.
  1654. */
  1655. obj = drm_mode_object_find(dev, plane_req->plane_id,
  1656. DRM_MODE_OBJECT_PLANE);
  1657. if (!obj) {
  1658. DRM_DEBUG_KMS("Unknown plane ID %d\n",
  1659. plane_req->plane_id);
  1660. return -ENOENT;
  1661. }
  1662. plane = obj_to_plane(obj);
  1663. /* No fb means shut it down */
  1664. if (!plane_req->fb_id) {
  1665. drm_modeset_lock_all(dev);
  1666. old_fb = plane->fb;
  1667. plane->funcs->disable_plane(plane);
  1668. plane->crtc = NULL;
  1669. plane->fb = NULL;
  1670. drm_modeset_unlock_all(dev);
  1671. goto out;
  1672. }
  1673. obj = drm_mode_object_find(dev, plane_req->crtc_id,
  1674. DRM_MODE_OBJECT_CRTC);
  1675. if (!obj) {
  1676. DRM_DEBUG_KMS("Unknown crtc ID %d\n",
  1677. plane_req->crtc_id);
  1678. ret = -ENOENT;
  1679. goto out;
  1680. }
  1681. crtc = obj_to_crtc(obj);
  1682. fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
  1683. if (!fb) {
  1684. DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
  1685. plane_req->fb_id);
  1686. ret = -ENOENT;
  1687. goto out;
  1688. }
  1689. /* Check whether this plane supports the fb pixel format. */
  1690. for (i = 0; i < plane->format_count; i++)
  1691. if (fb->pixel_format == plane->format_types[i])
  1692. break;
  1693. if (i == plane->format_count) {
  1694. DRM_DEBUG_KMS("Invalid pixel format %s\n",
  1695. drm_get_format_name(fb->pixel_format));
  1696. ret = -EINVAL;
  1697. goto out;
  1698. }
  1699. fb_width = fb->width << 16;
  1700. fb_height = fb->height << 16;
  1701. /* Make sure source coordinates are inside the fb. */
  1702. if (plane_req->src_w > fb_width ||
  1703. plane_req->src_x > fb_width - plane_req->src_w ||
  1704. plane_req->src_h > fb_height ||
  1705. plane_req->src_y > fb_height - plane_req->src_h) {
  1706. DRM_DEBUG_KMS("Invalid source coordinates "
  1707. "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
  1708. plane_req->src_w >> 16,
  1709. ((plane_req->src_w & 0xffff) * 15625) >> 10,
  1710. plane_req->src_h >> 16,
  1711. ((plane_req->src_h & 0xffff) * 15625) >> 10,
  1712. plane_req->src_x >> 16,
  1713. ((plane_req->src_x & 0xffff) * 15625) >> 10,
  1714. plane_req->src_y >> 16,
  1715. ((plane_req->src_y & 0xffff) * 15625) >> 10);
  1716. ret = -ENOSPC;
  1717. goto out;
  1718. }
  1719. /* Give drivers some help against integer overflows */
  1720. if (plane_req->crtc_w > INT_MAX ||
  1721. plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w ||
  1722. plane_req->crtc_h > INT_MAX ||
  1723. plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) {
  1724. DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
  1725. plane_req->crtc_w, plane_req->crtc_h,
  1726. plane_req->crtc_x, plane_req->crtc_y);
  1727. ret = -ERANGE;
  1728. goto out;
  1729. }
  1730. drm_modeset_lock_all(dev);
  1731. ret = plane->funcs->update_plane(plane, crtc, fb,
  1732. plane_req->crtc_x, plane_req->crtc_y,
  1733. plane_req->crtc_w, plane_req->crtc_h,
  1734. plane_req->src_x, plane_req->src_y,
  1735. plane_req->src_w, plane_req->src_h);
  1736. if (!ret) {
  1737. old_fb = plane->fb;
  1738. plane->crtc = crtc;
  1739. plane->fb = fb;
  1740. fb = NULL;
  1741. }
  1742. drm_modeset_unlock_all(dev);
  1743. out:
  1744. if (fb)
  1745. drm_framebuffer_unreference(fb);
  1746. if (old_fb)
  1747. drm_framebuffer_unreference(old_fb);
  1748. return ret;
  1749. }
  1750. /**
  1751. * drm_mode_set_config_internal - helper to call ->set_config
  1752. * @set: modeset config to set
  1753. *
  1754. * This is a little helper to wrap internal calls to the ->set_config driver
  1755. * interface. The only thing it adds is correct refcounting dance.
  1756. */
  1757. int drm_mode_set_config_internal(struct drm_mode_set *set)
  1758. {
  1759. struct drm_crtc *crtc = set->crtc;
  1760. struct drm_framebuffer *fb;
  1761. struct drm_crtc *tmp;
  1762. int ret;
  1763. /*
  1764. * NOTE: ->set_config can also disable other crtcs (if we steal all
  1765. * connectors from it), hence we need to refcount the fbs across all
  1766. * crtcs. Atomic modeset will have saner semantics ...
  1767. */
  1768. list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head)
  1769. tmp->old_fb = tmp->fb;
  1770. fb = set->fb;
  1771. ret = crtc->funcs->set_config(set);
  1772. if (ret == 0) {
  1773. /* crtc->fb must be updated by ->set_config, enforces this. */
  1774. WARN_ON(fb != crtc->fb);
  1775. }
  1776. list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head) {
  1777. if (tmp->fb)
  1778. drm_framebuffer_reference(tmp->fb);
  1779. if (tmp->old_fb)
  1780. drm_framebuffer_unreference(tmp->old_fb);
  1781. }
  1782. return ret;
  1783. }
  1784. EXPORT_SYMBOL(drm_mode_set_config_internal);
  1785. /*
  1786. * Checks that the framebuffer is big enough for the CRTC viewport
  1787. * (x, y, hdisplay, vdisplay)
  1788. */
  1789. static int drm_crtc_check_viewport(const struct drm_crtc *crtc,
  1790. int x, int y,
  1791. const struct drm_display_mode *mode,
  1792. const struct drm_framebuffer *fb)
  1793. {
  1794. int hdisplay, vdisplay;
  1795. hdisplay = mode->hdisplay;
  1796. vdisplay = mode->vdisplay;
  1797. if (drm_mode_is_stereo(mode)) {
  1798. struct drm_display_mode adjusted = *mode;
  1799. drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE);
  1800. hdisplay = adjusted.crtc_hdisplay;
  1801. vdisplay = adjusted.crtc_vdisplay;
  1802. }
  1803. if (crtc->invert_dimensions)
  1804. swap(hdisplay, vdisplay);
  1805. if (hdisplay > fb->width ||
  1806. vdisplay > fb->height ||
  1807. x > fb->width - hdisplay ||
  1808. y > fb->height - vdisplay) {
  1809. DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
  1810. fb->width, fb->height, hdisplay, vdisplay, x, y,
  1811. crtc->invert_dimensions ? " (inverted)" : "");
  1812. return -ENOSPC;
  1813. }
  1814. return 0;
  1815. }
  1816. /**
  1817. * drm_mode_setcrtc - set CRTC configuration
  1818. * @dev: drm device for the ioctl
  1819. * @data: data pointer for the ioctl
  1820. * @file_priv: drm file for the ioctl call
  1821. *
  1822. * Build a new CRTC configuration based on user request.
  1823. *
  1824. * Called by the user via ioctl.
  1825. *
  1826. * RETURNS:
  1827. * Zero on success, errno on failure.
  1828. */
  1829. int drm_mode_setcrtc(struct drm_device *dev, void *data,
  1830. struct drm_file *file_priv)
  1831. {
  1832. struct drm_mode_config *config = &dev->mode_config;
  1833. struct drm_mode_crtc *crtc_req = data;
  1834. struct drm_mode_object *obj;
  1835. struct drm_crtc *crtc;
  1836. struct drm_connector **connector_set = NULL, *connector;
  1837. struct drm_framebuffer *fb = NULL;
  1838. struct drm_display_mode *mode = NULL;
  1839. struct drm_mode_set set;
  1840. uint32_t __user *set_connectors_ptr;
  1841. int ret;
  1842. int i;
  1843. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1844. return -EINVAL;
  1845. /* For some reason crtc x/y offsets are signed internally. */
  1846. if (crtc_req->x > INT_MAX || crtc_req->y > INT_MAX)
  1847. return -ERANGE;
  1848. drm_modeset_lock_all(dev);
  1849. obj = drm_mode_object_find(dev, crtc_req->crtc_id,
  1850. DRM_MODE_OBJECT_CRTC);
  1851. if (!obj) {
  1852. DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
  1853. ret = -EINVAL;
  1854. goto out;
  1855. }
  1856. crtc = obj_to_crtc(obj);
  1857. DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
  1858. if (crtc_req->mode_valid) {
  1859. /* If we have a mode we need a framebuffer. */
  1860. /* If we pass -1, set the mode with the currently bound fb */
  1861. if (crtc_req->fb_id == -1) {
  1862. if (!crtc->fb) {
  1863. DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
  1864. ret = -EINVAL;
  1865. goto out;
  1866. }
  1867. fb = crtc->fb;
  1868. /* Make refcounting symmetric with the lookup path. */
  1869. drm_framebuffer_reference(fb);
  1870. } else {
  1871. fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
  1872. if (!fb) {
  1873. DRM_DEBUG_KMS("Unknown FB ID%d\n",
  1874. crtc_req->fb_id);
  1875. ret = -EINVAL;
  1876. goto out;
  1877. }
  1878. }
  1879. mode = drm_mode_create(dev);
  1880. if (!mode) {
  1881. ret = -ENOMEM;
  1882. goto out;
  1883. }
  1884. ret = drm_crtc_convert_umode(mode, &crtc_req->mode);
  1885. if (ret) {
  1886. DRM_DEBUG_KMS("Invalid mode\n");
  1887. goto out;
  1888. }
  1889. drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
  1890. ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
  1891. mode, fb);
  1892. if (ret)
  1893. goto out;
  1894. }
  1895. if (crtc_req->count_connectors == 0 && mode) {
  1896. DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
  1897. ret = -EINVAL;
  1898. goto out;
  1899. }
  1900. if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
  1901. DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
  1902. crtc_req->count_connectors);
  1903. ret = -EINVAL;
  1904. goto out;
  1905. }
  1906. if (crtc_req->count_connectors > 0) {
  1907. u32 out_id;
  1908. /* Avoid unbounded kernel memory allocation */
  1909. if (crtc_req->count_connectors > config->num_connector) {
  1910. ret = -EINVAL;
  1911. goto out;
  1912. }
  1913. connector_set = kmalloc(crtc_req->count_connectors *
  1914. sizeof(struct drm_connector *),
  1915. GFP_KERNEL);
  1916. if (!connector_set) {
  1917. ret = -ENOMEM;
  1918. goto out;
  1919. }
  1920. for (i = 0; i < crtc_req->count_connectors; i++) {
  1921. set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
  1922. if (get_user(out_id, &set_connectors_ptr[i])) {
  1923. ret = -EFAULT;
  1924. goto out;
  1925. }
  1926. obj = drm_mode_object_find(dev, out_id,
  1927. DRM_MODE_OBJECT_CONNECTOR);
  1928. if (!obj) {
  1929. DRM_DEBUG_KMS("Connector id %d unknown\n",
  1930. out_id);
  1931. ret = -EINVAL;
  1932. goto out;
  1933. }
  1934. connector = obj_to_connector(obj);
  1935. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  1936. connector->base.id,
  1937. drm_get_connector_name(connector));
  1938. connector_set[i] = connector;
  1939. }
  1940. }
  1941. set.crtc = crtc;
  1942. set.x = crtc_req->x;
  1943. set.y = crtc_req->y;
  1944. set.mode = mode;
  1945. set.connectors = connector_set;
  1946. set.num_connectors = crtc_req->count_connectors;
  1947. set.fb = fb;
  1948. ret = drm_mode_set_config_internal(&set);
  1949. out:
  1950. if (fb)
  1951. drm_framebuffer_unreference(fb);
  1952. kfree(connector_set);
  1953. drm_mode_destroy(dev, mode);
  1954. drm_modeset_unlock_all(dev);
  1955. return ret;
  1956. }
  1957. static int drm_mode_cursor_common(struct drm_device *dev,
  1958. struct drm_mode_cursor2 *req,
  1959. struct drm_file *file_priv)
  1960. {
  1961. struct drm_mode_object *obj;
  1962. struct drm_crtc *crtc;
  1963. int ret = 0;
  1964. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1965. return -EINVAL;
  1966. if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
  1967. return -EINVAL;
  1968. obj = drm_mode_object_find(dev, req->crtc_id, DRM_MODE_OBJECT_CRTC);
  1969. if (!obj) {
  1970. DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
  1971. return -EINVAL;
  1972. }
  1973. crtc = obj_to_crtc(obj);
  1974. mutex_lock(&crtc->mutex);
  1975. if (req->flags & DRM_MODE_CURSOR_BO) {
  1976. if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
  1977. ret = -ENXIO;
  1978. goto out;
  1979. }
  1980. /* Turns off the cursor if handle is 0 */
  1981. if (crtc->funcs->cursor_set2)
  1982. ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
  1983. req->width, req->height, req->hot_x, req->hot_y);
  1984. else
  1985. ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
  1986. req->width, req->height);
  1987. }
  1988. if (req->flags & DRM_MODE_CURSOR_MOVE) {
  1989. if (crtc->funcs->cursor_move) {
  1990. ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
  1991. } else {
  1992. ret = -EFAULT;
  1993. goto out;
  1994. }
  1995. }
  1996. out:
  1997. mutex_unlock(&crtc->mutex);
  1998. return ret;
  1999. }
  2000. int drm_mode_cursor_ioctl(struct drm_device *dev,
  2001. void *data, struct drm_file *file_priv)
  2002. {
  2003. struct drm_mode_cursor *req = data;
  2004. struct drm_mode_cursor2 new_req;
  2005. memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
  2006. new_req.hot_x = new_req.hot_y = 0;
  2007. return drm_mode_cursor_common(dev, &new_req, file_priv);
  2008. }
  2009. int drm_mode_cursor2_ioctl(struct drm_device *dev,
  2010. void *data, struct drm_file *file_priv)
  2011. {
  2012. struct drm_mode_cursor2 *req = data;
  2013. return drm_mode_cursor_common(dev, req, file_priv);
  2014. }
  2015. /* Original addfb only supported RGB formats, so figure out which one */
  2016. uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
  2017. {
  2018. uint32_t fmt;
  2019. switch (bpp) {
  2020. case 8:
  2021. fmt = DRM_FORMAT_C8;
  2022. break;
  2023. case 16:
  2024. if (depth == 15)
  2025. fmt = DRM_FORMAT_XRGB1555;
  2026. else
  2027. fmt = DRM_FORMAT_RGB565;
  2028. break;
  2029. case 24:
  2030. fmt = DRM_FORMAT_RGB888;
  2031. break;
  2032. case 32:
  2033. if (depth == 24)
  2034. fmt = DRM_FORMAT_XRGB8888;
  2035. else if (depth == 30)
  2036. fmt = DRM_FORMAT_XRGB2101010;
  2037. else
  2038. fmt = DRM_FORMAT_ARGB8888;
  2039. break;
  2040. default:
  2041. DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
  2042. fmt = DRM_FORMAT_XRGB8888;
  2043. break;
  2044. }
  2045. return fmt;
  2046. }
  2047. EXPORT_SYMBOL(drm_mode_legacy_fb_format);
  2048. /**
  2049. * drm_mode_addfb - add an FB to the graphics configuration
  2050. * @dev: drm device for the ioctl
  2051. * @data: data pointer for the ioctl
  2052. * @file_priv: drm file for the ioctl call
  2053. *
  2054. * Add a new FB to the specified CRTC, given a user request.
  2055. *
  2056. * Called by the user via ioctl.
  2057. *
  2058. * RETURNS:
  2059. * Zero on success, errno on failure.
  2060. */
  2061. int drm_mode_addfb(struct drm_device *dev,
  2062. void *data, struct drm_file *file_priv)
  2063. {
  2064. struct drm_mode_fb_cmd *or = data;
  2065. struct drm_mode_fb_cmd2 r = {};
  2066. struct drm_mode_config *config = &dev->mode_config;
  2067. struct drm_framebuffer *fb;
  2068. int ret = 0;
  2069. /* Use new struct with format internally */
  2070. r.fb_id = or->fb_id;
  2071. r.width = or->width;
  2072. r.height = or->height;
  2073. r.pitches[0] = or->pitch;
  2074. r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
  2075. r.handles[0] = or->handle;
  2076. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2077. return -EINVAL;
  2078. if ((config->min_width > r.width) || (r.width > config->max_width))
  2079. return -EINVAL;
  2080. if ((config->min_height > r.height) || (r.height > config->max_height))
  2081. return -EINVAL;
  2082. fb = dev->mode_config.funcs->fb_create(dev, file_priv, &r);
  2083. if (IS_ERR(fb)) {
  2084. DRM_DEBUG_KMS("could not create framebuffer\n");
  2085. return PTR_ERR(fb);
  2086. }
  2087. mutex_lock(&file_priv->fbs_lock);
  2088. or->fb_id = fb->base.id;
  2089. list_add(&fb->filp_head, &file_priv->fbs);
  2090. DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
  2091. mutex_unlock(&file_priv->fbs_lock);
  2092. return ret;
  2093. }
  2094. static int format_check(const struct drm_mode_fb_cmd2 *r)
  2095. {
  2096. uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
  2097. switch (format) {
  2098. case DRM_FORMAT_C8:
  2099. case DRM_FORMAT_RGB332:
  2100. case DRM_FORMAT_BGR233:
  2101. case DRM_FORMAT_XRGB4444:
  2102. case DRM_FORMAT_XBGR4444:
  2103. case DRM_FORMAT_RGBX4444:
  2104. case DRM_FORMAT_BGRX4444:
  2105. case DRM_FORMAT_ARGB4444:
  2106. case DRM_FORMAT_ABGR4444:
  2107. case DRM_FORMAT_RGBA4444:
  2108. case DRM_FORMAT_BGRA4444:
  2109. case DRM_FORMAT_XRGB1555:
  2110. case DRM_FORMAT_XBGR1555:
  2111. case DRM_FORMAT_RGBX5551:
  2112. case DRM_FORMAT_BGRX5551:
  2113. case DRM_FORMAT_ARGB1555:
  2114. case DRM_FORMAT_ABGR1555:
  2115. case DRM_FORMAT_RGBA5551:
  2116. case DRM_FORMAT_BGRA5551:
  2117. case DRM_FORMAT_RGB565:
  2118. case DRM_FORMAT_BGR565:
  2119. case DRM_FORMAT_RGB888:
  2120. case DRM_FORMAT_BGR888:
  2121. case DRM_FORMAT_XRGB8888:
  2122. case DRM_FORMAT_XBGR8888:
  2123. case DRM_FORMAT_RGBX8888:
  2124. case DRM_FORMAT_BGRX8888:
  2125. case DRM_FORMAT_ARGB8888:
  2126. case DRM_FORMAT_ABGR8888:
  2127. case DRM_FORMAT_RGBA8888:
  2128. case DRM_FORMAT_BGRA8888:
  2129. case DRM_FORMAT_XRGB2101010:
  2130. case DRM_FORMAT_XBGR2101010:
  2131. case DRM_FORMAT_RGBX1010102:
  2132. case DRM_FORMAT_BGRX1010102:
  2133. case DRM_FORMAT_ARGB2101010:
  2134. case DRM_FORMAT_ABGR2101010:
  2135. case DRM_FORMAT_RGBA1010102:
  2136. case DRM_FORMAT_BGRA1010102:
  2137. case DRM_FORMAT_YUYV:
  2138. case DRM_FORMAT_YVYU:
  2139. case DRM_FORMAT_UYVY:
  2140. case DRM_FORMAT_VYUY:
  2141. case DRM_FORMAT_AYUV:
  2142. case DRM_FORMAT_NV12:
  2143. case DRM_FORMAT_NV21:
  2144. case DRM_FORMAT_NV16:
  2145. case DRM_FORMAT_NV61:
  2146. case DRM_FORMAT_NV24:
  2147. case DRM_FORMAT_NV42:
  2148. case DRM_FORMAT_YUV410:
  2149. case DRM_FORMAT_YVU410:
  2150. case DRM_FORMAT_YUV411:
  2151. case DRM_FORMAT_YVU411:
  2152. case DRM_FORMAT_YUV420:
  2153. case DRM_FORMAT_YVU420:
  2154. case DRM_FORMAT_YUV422:
  2155. case DRM_FORMAT_YVU422:
  2156. case DRM_FORMAT_YUV444:
  2157. case DRM_FORMAT_YVU444:
  2158. return 0;
  2159. default:
  2160. return -EINVAL;
  2161. }
  2162. }
  2163. static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
  2164. {
  2165. int ret, hsub, vsub, num_planes, i;
  2166. ret = format_check(r);
  2167. if (ret) {
  2168. DRM_DEBUG_KMS("bad framebuffer format %s\n",
  2169. drm_get_format_name(r->pixel_format));
  2170. return ret;
  2171. }
  2172. hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
  2173. vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
  2174. num_planes = drm_format_num_planes(r->pixel_format);
  2175. if (r->width == 0 || r->width % hsub) {
  2176. DRM_DEBUG_KMS("bad framebuffer width %u\n", r->height);
  2177. return -EINVAL;
  2178. }
  2179. if (r->height == 0 || r->height % vsub) {
  2180. DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
  2181. return -EINVAL;
  2182. }
  2183. for (i = 0; i < num_planes; i++) {
  2184. unsigned int width = r->width / (i != 0 ? hsub : 1);
  2185. unsigned int height = r->height / (i != 0 ? vsub : 1);
  2186. unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
  2187. if (!r->handles[i]) {
  2188. DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
  2189. return -EINVAL;
  2190. }
  2191. if ((uint64_t) width * cpp > UINT_MAX)
  2192. return -ERANGE;
  2193. if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
  2194. return -ERANGE;
  2195. if (r->pitches[i] < width * cpp) {
  2196. DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
  2197. return -EINVAL;
  2198. }
  2199. }
  2200. return 0;
  2201. }
  2202. /**
  2203. * drm_mode_addfb2 - add an FB to the graphics configuration
  2204. * @dev: drm device for the ioctl
  2205. * @data: data pointer for the ioctl
  2206. * @file_priv: drm file for the ioctl call
  2207. *
  2208. * Add a new FB to the specified CRTC, given a user request with format.
  2209. *
  2210. * Called by the user via ioctl.
  2211. *
  2212. * RETURNS:
  2213. * Zero on success, errno on failure.
  2214. */
  2215. int drm_mode_addfb2(struct drm_device *dev,
  2216. void *data, struct drm_file *file_priv)
  2217. {
  2218. struct drm_mode_fb_cmd2 *r = data;
  2219. struct drm_mode_config *config = &dev->mode_config;
  2220. struct drm_framebuffer *fb;
  2221. int ret;
  2222. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2223. return -EINVAL;
  2224. if (r->flags & ~DRM_MODE_FB_INTERLACED) {
  2225. DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
  2226. return -EINVAL;
  2227. }
  2228. if ((config->min_width > r->width) || (r->width > config->max_width)) {
  2229. DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
  2230. r->width, config->min_width, config->max_width);
  2231. return -EINVAL;
  2232. }
  2233. if ((config->min_height > r->height) || (r->height > config->max_height)) {
  2234. DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
  2235. r->height, config->min_height, config->max_height);
  2236. return -EINVAL;
  2237. }
  2238. ret = framebuffer_check(r);
  2239. if (ret)
  2240. return ret;
  2241. fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
  2242. if (IS_ERR(fb)) {
  2243. DRM_DEBUG_KMS("could not create framebuffer\n");
  2244. return PTR_ERR(fb);
  2245. }
  2246. mutex_lock(&file_priv->fbs_lock);
  2247. r->fb_id = fb->base.id;
  2248. list_add(&fb->filp_head, &file_priv->fbs);
  2249. DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
  2250. mutex_unlock(&file_priv->fbs_lock);
  2251. return ret;
  2252. }
  2253. /**
  2254. * drm_mode_rmfb - remove an FB from the configuration
  2255. * @dev: drm device for the ioctl
  2256. * @data: data pointer for the ioctl
  2257. * @file_priv: drm file for the ioctl call
  2258. *
  2259. * Remove the FB specified by the user.
  2260. *
  2261. * Called by the user via ioctl.
  2262. *
  2263. * RETURNS:
  2264. * Zero on success, errno on failure.
  2265. */
  2266. int drm_mode_rmfb(struct drm_device *dev,
  2267. void *data, struct drm_file *file_priv)
  2268. {
  2269. struct drm_framebuffer *fb = NULL;
  2270. struct drm_framebuffer *fbl = NULL;
  2271. uint32_t *id = data;
  2272. int found = 0;
  2273. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2274. return -EINVAL;
  2275. mutex_lock(&file_priv->fbs_lock);
  2276. mutex_lock(&dev->mode_config.fb_lock);
  2277. fb = __drm_framebuffer_lookup(dev, *id);
  2278. if (!fb)
  2279. goto fail_lookup;
  2280. list_for_each_entry(fbl, &file_priv->fbs, filp_head)
  2281. if (fb == fbl)
  2282. found = 1;
  2283. if (!found)
  2284. goto fail_lookup;
  2285. /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
  2286. __drm_framebuffer_unregister(dev, fb);
  2287. list_del_init(&fb->filp_head);
  2288. mutex_unlock(&dev->mode_config.fb_lock);
  2289. mutex_unlock(&file_priv->fbs_lock);
  2290. drm_framebuffer_remove(fb);
  2291. return 0;
  2292. fail_lookup:
  2293. mutex_unlock(&dev->mode_config.fb_lock);
  2294. mutex_unlock(&file_priv->fbs_lock);
  2295. return -EINVAL;
  2296. }
  2297. /**
  2298. * drm_mode_getfb - get FB info
  2299. * @dev: drm device for the ioctl
  2300. * @data: data pointer for the ioctl
  2301. * @file_priv: drm file for the ioctl call
  2302. *
  2303. * Lookup the FB given its ID and return info about it.
  2304. *
  2305. * Called by the user via ioctl.
  2306. *
  2307. * RETURNS:
  2308. * Zero on success, errno on failure.
  2309. */
  2310. int drm_mode_getfb(struct drm_device *dev,
  2311. void *data, struct drm_file *file_priv)
  2312. {
  2313. struct drm_mode_fb_cmd *r = data;
  2314. struct drm_framebuffer *fb;
  2315. int ret;
  2316. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2317. return -EINVAL;
  2318. fb = drm_framebuffer_lookup(dev, r->fb_id);
  2319. if (!fb)
  2320. return -EINVAL;
  2321. r->height = fb->height;
  2322. r->width = fb->width;
  2323. r->depth = fb->depth;
  2324. r->bpp = fb->bits_per_pixel;
  2325. r->pitch = fb->pitches[0];
  2326. if (fb->funcs->create_handle) {
  2327. if (file_priv->is_master || capable(CAP_SYS_ADMIN)) {
  2328. ret = fb->funcs->create_handle(fb, file_priv,
  2329. &r->handle);
  2330. } else {
  2331. /* GET_FB() is an unprivileged ioctl so we must not
  2332. * return a buffer-handle to non-master processes! For
  2333. * backwards-compatibility reasons, we cannot make
  2334. * GET_FB() privileged, so just return an invalid handle
  2335. * for non-masters. */
  2336. r->handle = 0;
  2337. ret = 0;
  2338. }
  2339. } else {
  2340. ret = -ENODEV;
  2341. }
  2342. drm_framebuffer_unreference(fb);
  2343. return ret;
  2344. }
  2345. int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
  2346. void *data, struct drm_file *file_priv)
  2347. {
  2348. struct drm_clip_rect __user *clips_ptr;
  2349. struct drm_clip_rect *clips = NULL;
  2350. struct drm_mode_fb_dirty_cmd *r = data;
  2351. struct drm_framebuffer *fb;
  2352. unsigned flags;
  2353. int num_clips;
  2354. int ret;
  2355. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2356. return -EINVAL;
  2357. fb = drm_framebuffer_lookup(dev, r->fb_id);
  2358. if (!fb)
  2359. return -EINVAL;
  2360. num_clips = r->num_clips;
  2361. clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
  2362. if (!num_clips != !clips_ptr) {
  2363. ret = -EINVAL;
  2364. goto out_err1;
  2365. }
  2366. flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
  2367. /* If userspace annotates copy, clips must come in pairs */
  2368. if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
  2369. ret = -EINVAL;
  2370. goto out_err1;
  2371. }
  2372. if (num_clips && clips_ptr) {
  2373. if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
  2374. ret = -EINVAL;
  2375. goto out_err1;
  2376. }
  2377. clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL);
  2378. if (!clips) {
  2379. ret = -ENOMEM;
  2380. goto out_err1;
  2381. }
  2382. ret = copy_from_user(clips, clips_ptr,
  2383. num_clips * sizeof(*clips));
  2384. if (ret) {
  2385. ret = -EFAULT;
  2386. goto out_err2;
  2387. }
  2388. }
  2389. if (fb->funcs->dirty) {
  2390. drm_modeset_lock_all(dev);
  2391. ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
  2392. clips, num_clips);
  2393. drm_modeset_unlock_all(dev);
  2394. } else {
  2395. ret = -ENOSYS;
  2396. }
  2397. out_err2:
  2398. kfree(clips);
  2399. out_err1:
  2400. drm_framebuffer_unreference(fb);
  2401. return ret;
  2402. }
  2403. /**
  2404. * drm_fb_release - remove and free the FBs on this file
  2405. * @priv: drm file for the ioctl
  2406. *
  2407. * Destroy all the FBs associated with @filp.
  2408. *
  2409. * Called by the user via ioctl.
  2410. *
  2411. * RETURNS:
  2412. * Zero on success, errno on failure.
  2413. */
  2414. void drm_fb_release(struct drm_file *priv)
  2415. {
  2416. struct drm_device *dev = priv->minor->dev;
  2417. struct drm_framebuffer *fb, *tfb;
  2418. mutex_lock(&priv->fbs_lock);
  2419. list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
  2420. mutex_lock(&dev->mode_config.fb_lock);
  2421. /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
  2422. __drm_framebuffer_unregister(dev, fb);
  2423. mutex_unlock(&dev->mode_config.fb_lock);
  2424. list_del_init(&fb->filp_head);
  2425. /* This will also drop the fpriv->fbs reference. */
  2426. drm_framebuffer_remove(fb);
  2427. }
  2428. mutex_unlock(&priv->fbs_lock);
  2429. }
  2430. struct drm_property *drm_property_create(struct drm_device *dev, int flags,
  2431. const char *name, int num_values)
  2432. {
  2433. struct drm_property *property = NULL;
  2434. int ret;
  2435. property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
  2436. if (!property)
  2437. return NULL;
  2438. if (num_values) {
  2439. property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
  2440. if (!property->values)
  2441. goto fail;
  2442. }
  2443. ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
  2444. if (ret)
  2445. goto fail;
  2446. property->flags = flags;
  2447. property->num_values = num_values;
  2448. INIT_LIST_HEAD(&property->enum_blob_list);
  2449. if (name) {
  2450. strncpy(property->name, name, DRM_PROP_NAME_LEN);
  2451. property->name[DRM_PROP_NAME_LEN-1] = '\0';
  2452. }
  2453. list_add_tail(&property->head, &dev->mode_config.property_list);
  2454. return property;
  2455. fail:
  2456. kfree(property->values);
  2457. kfree(property);
  2458. return NULL;
  2459. }
  2460. EXPORT_SYMBOL(drm_property_create);
  2461. struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
  2462. const char *name,
  2463. const struct drm_prop_enum_list *props,
  2464. int num_values)
  2465. {
  2466. struct drm_property *property;
  2467. int i, ret;
  2468. flags |= DRM_MODE_PROP_ENUM;
  2469. property = drm_property_create(dev, flags, name, num_values);
  2470. if (!property)
  2471. return NULL;
  2472. for (i = 0; i < num_values; i++) {
  2473. ret = drm_property_add_enum(property, i,
  2474. props[i].type,
  2475. props[i].name);
  2476. if (ret) {
  2477. drm_property_destroy(dev, property);
  2478. return NULL;
  2479. }
  2480. }
  2481. return property;
  2482. }
  2483. EXPORT_SYMBOL(drm_property_create_enum);
  2484. struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
  2485. int flags, const char *name,
  2486. const struct drm_prop_enum_list *props,
  2487. int num_values)
  2488. {
  2489. struct drm_property *property;
  2490. int i, ret;
  2491. flags |= DRM_MODE_PROP_BITMASK;
  2492. property = drm_property_create(dev, flags, name, num_values);
  2493. if (!property)
  2494. return NULL;
  2495. for (i = 0; i < num_values; i++) {
  2496. ret = drm_property_add_enum(property, i,
  2497. props[i].type,
  2498. props[i].name);
  2499. if (ret) {
  2500. drm_property_destroy(dev, property);
  2501. return NULL;
  2502. }
  2503. }
  2504. return property;
  2505. }
  2506. EXPORT_SYMBOL(drm_property_create_bitmask);
  2507. struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
  2508. const char *name,
  2509. uint64_t min, uint64_t max)
  2510. {
  2511. struct drm_property *property;
  2512. flags |= DRM_MODE_PROP_RANGE;
  2513. property = drm_property_create(dev, flags, name, 2);
  2514. if (!property)
  2515. return NULL;
  2516. property->values[0] = min;
  2517. property->values[1] = max;
  2518. return property;
  2519. }
  2520. EXPORT_SYMBOL(drm_property_create_range);
  2521. int drm_property_add_enum(struct drm_property *property, int index,
  2522. uint64_t value, const char *name)
  2523. {
  2524. struct drm_property_enum *prop_enum;
  2525. if (!(property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)))
  2526. return -EINVAL;
  2527. /*
  2528. * Bitmask enum properties have the additional constraint of values
  2529. * from 0 to 63
  2530. */
  2531. if ((property->flags & DRM_MODE_PROP_BITMASK) && (value > 63))
  2532. return -EINVAL;
  2533. if (!list_empty(&property->enum_blob_list)) {
  2534. list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
  2535. if (prop_enum->value == value) {
  2536. strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
  2537. prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
  2538. return 0;
  2539. }
  2540. }
  2541. }
  2542. prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
  2543. if (!prop_enum)
  2544. return -ENOMEM;
  2545. strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
  2546. prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
  2547. prop_enum->value = value;
  2548. property->values[index] = value;
  2549. list_add_tail(&prop_enum->head, &property->enum_blob_list);
  2550. return 0;
  2551. }
  2552. EXPORT_SYMBOL(drm_property_add_enum);
  2553. void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
  2554. {
  2555. struct drm_property_enum *prop_enum, *pt;
  2556. list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
  2557. list_del(&prop_enum->head);
  2558. kfree(prop_enum);
  2559. }
  2560. if (property->num_values)
  2561. kfree(property->values);
  2562. drm_mode_object_put(dev, &property->base);
  2563. list_del(&property->head);
  2564. kfree(property);
  2565. }
  2566. EXPORT_SYMBOL(drm_property_destroy);
  2567. void drm_object_attach_property(struct drm_mode_object *obj,
  2568. struct drm_property *property,
  2569. uint64_t init_val)
  2570. {
  2571. int count = obj->properties->count;
  2572. if (count == DRM_OBJECT_MAX_PROPERTY) {
  2573. WARN(1, "Failed to attach object property (type: 0x%x). Please "
  2574. "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
  2575. "you see this message on the same object type.\n",
  2576. obj->type);
  2577. return;
  2578. }
  2579. obj->properties->ids[count] = property->base.id;
  2580. obj->properties->values[count] = init_val;
  2581. obj->properties->count++;
  2582. }
  2583. EXPORT_SYMBOL(drm_object_attach_property);
  2584. int drm_object_property_set_value(struct drm_mode_object *obj,
  2585. struct drm_property *property, uint64_t val)
  2586. {
  2587. int i;
  2588. for (i = 0; i < obj->properties->count; i++) {
  2589. if (obj->properties->ids[i] == property->base.id) {
  2590. obj->properties->values[i] = val;
  2591. return 0;
  2592. }
  2593. }
  2594. return -EINVAL;
  2595. }
  2596. EXPORT_SYMBOL(drm_object_property_set_value);
  2597. int drm_object_property_get_value(struct drm_mode_object *obj,
  2598. struct drm_property *property, uint64_t *val)
  2599. {
  2600. int i;
  2601. for (i = 0; i < obj->properties->count; i++) {
  2602. if (obj->properties->ids[i] == property->base.id) {
  2603. *val = obj->properties->values[i];
  2604. return 0;
  2605. }
  2606. }
  2607. return -EINVAL;
  2608. }
  2609. EXPORT_SYMBOL(drm_object_property_get_value);
  2610. int drm_mode_getproperty_ioctl(struct drm_device *dev,
  2611. void *data, struct drm_file *file_priv)
  2612. {
  2613. struct drm_mode_object *obj;
  2614. struct drm_mode_get_property *out_resp = data;
  2615. struct drm_property *property;
  2616. int enum_count = 0;
  2617. int blob_count = 0;
  2618. int value_count = 0;
  2619. int ret = 0, i;
  2620. int copied;
  2621. struct drm_property_enum *prop_enum;
  2622. struct drm_mode_property_enum __user *enum_ptr;
  2623. struct drm_property_blob *prop_blob;
  2624. uint32_t __user *blob_id_ptr;
  2625. uint64_t __user *values_ptr;
  2626. uint32_t __user *blob_length_ptr;
  2627. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2628. return -EINVAL;
  2629. drm_modeset_lock_all(dev);
  2630. obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
  2631. if (!obj) {
  2632. ret = -EINVAL;
  2633. goto done;
  2634. }
  2635. property = obj_to_property(obj);
  2636. if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
  2637. list_for_each_entry(prop_enum, &property->enum_blob_list, head)
  2638. enum_count++;
  2639. } else if (property->flags & DRM_MODE_PROP_BLOB) {
  2640. list_for_each_entry(prop_blob, &property->enum_blob_list, head)
  2641. blob_count++;
  2642. }
  2643. value_count = property->num_values;
  2644. strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
  2645. out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
  2646. out_resp->flags = property->flags;
  2647. if ((out_resp->count_values >= value_count) && value_count) {
  2648. values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
  2649. for (i = 0; i < value_count; i++) {
  2650. if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
  2651. ret = -EFAULT;
  2652. goto done;
  2653. }
  2654. }
  2655. }
  2656. out_resp->count_values = value_count;
  2657. if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
  2658. if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
  2659. copied = 0;
  2660. enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
  2661. list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
  2662. if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
  2663. ret = -EFAULT;
  2664. goto done;
  2665. }
  2666. if (copy_to_user(&enum_ptr[copied].name,
  2667. &prop_enum->name, DRM_PROP_NAME_LEN)) {
  2668. ret = -EFAULT;
  2669. goto done;
  2670. }
  2671. copied++;
  2672. }
  2673. }
  2674. out_resp->count_enum_blobs = enum_count;
  2675. }
  2676. if (property->flags & DRM_MODE_PROP_BLOB) {
  2677. if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
  2678. copied = 0;
  2679. blob_id_ptr = (uint32_t __user *)(unsigned long)out_resp->enum_blob_ptr;
  2680. blob_length_ptr = (uint32_t __user *)(unsigned long)out_resp->values_ptr;
  2681. list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
  2682. if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
  2683. ret = -EFAULT;
  2684. goto done;
  2685. }
  2686. if (put_user(prop_blob->length, blob_length_ptr + copied)) {
  2687. ret = -EFAULT;
  2688. goto done;
  2689. }
  2690. copied++;
  2691. }
  2692. }
  2693. out_resp->count_enum_blobs = blob_count;
  2694. }
  2695. done:
  2696. drm_modeset_unlock_all(dev);
  2697. return ret;
  2698. }
  2699. static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
  2700. void *data)
  2701. {
  2702. struct drm_property_blob *blob;
  2703. int ret;
  2704. if (!length || !data)
  2705. return NULL;
  2706. blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
  2707. if (!blob)
  2708. return NULL;
  2709. ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
  2710. if (ret) {
  2711. kfree(blob);
  2712. return NULL;
  2713. }
  2714. blob->length = length;
  2715. memcpy(blob->data, data, length);
  2716. list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
  2717. return blob;
  2718. }
  2719. static void drm_property_destroy_blob(struct drm_device *dev,
  2720. struct drm_property_blob *blob)
  2721. {
  2722. drm_mode_object_put(dev, &blob->base);
  2723. list_del(&blob->head);
  2724. kfree(blob);
  2725. }
  2726. int drm_mode_getblob_ioctl(struct drm_device *dev,
  2727. void *data, struct drm_file *file_priv)
  2728. {
  2729. struct drm_mode_object *obj;
  2730. struct drm_mode_get_blob *out_resp = data;
  2731. struct drm_property_blob *blob;
  2732. int ret = 0;
  2733. void __user *blob_ptr;
  2734. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2735. return -EINVAL;
  2736. drm_modeset_lock_all(dev);
  2737. obj = drm_mode_object_find(dev, out_resp->blob_id, DRM_MODE_OBJECT_BLOB);
  2738. if (!obj) {
  2739. ret = -EINVAL;
  2740. goto done;
  2741. }
  2742. blob = obj_to_blob(obj);
  2743. if (out_resp->length == blob->length) {
  2744. blob_ptr = (void __user *)(unsigned long)out_resp->data;
  2745. if (copy_to_user(blob_ptr, blob->data, blob->length)){
  2746. ret = -EFAULT;
  2747. goto done;
  2748. }
  2749. }
  2750. out_resp->length = blob->length;
  2751. done:
  2752. drm_modeset_unlock_all(dev);
  2753. return ret;
  2754. }
  2755. int drm_mode_connector_update_edid_property(struct drm_connector *connector,
  2756. struct edid *edid)
  2757. {
  2758. struct drm_device *dev = connector->dev;
  2759. int ret, size;
  2760. if (connector->edid_blob_ptr)
  2761. drm_property_destroy_blob(dev, connector->edid_blob_ptr);
  2762. /* Delete edid, when there is none. */
  2763. if (!edid) {
  2764. connector->edid_blob_ptr = NULL;
  2765. ret = drm_object_property_set_value(&connector->base, dev->mode_config.edid_property, 0);
  2766. return ret;
  2767. }
  2768. size = EDID_LENGTH * (1 + edid->extensions);
  2769. connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
  2770. size, edid);
  2771. if (!connector->edid_blob_ptr)
  2772. return -EINVAL;
  2773. ret = drm_object_property_set_value(&connector->base,
  2774. dev->mode_config.edid_property,
  2775. connector->edid_blob_ptr->base.id);
  2776. return ret;
  2777. }
  2778. EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
  2779. static bool drm_property_change_is_valid(struct drm_property *property,
  2780. uint64_t value)
  2781. {
  2782. if (property->flags & DRM_MODE_PROP_IMMUTABLE)
  2783. return false;
  2784. if (property->flags & DRM_MODE_PROP_RANGE) {
  2785. if (value < property->values[0] || value > property->values[1])
  2786. return false;
  2787. return true;
  2788. } else if (property->flags & DRM_MODE_PROP_BITMASK) {
  2789. int i;
  2790. uint64_t valid_mask = 0;
  2791. for (i = 0; i < property->num_values; i++)
  2792. valid_mask |= (1ULL << property->values[i]);
  2793. return !(value & ~valid_mask);
  2794. } else if (property->flags & DRM_MODE_PROP_BLOB) {
  2795. /* Only the driver knows */
  2796. return true;
  2797. } else {
  2798. int i;
  2799. for (i = 0; i < property->num_values; i++)
  2800. if (property->values[i] == value)
  2801. return true;
  2802. return false;
  2803. }
  2804. }
  2805. int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
  2806. void *data, struct drm_file *file_priv)
  2807. {
  2808. struct drm_mode_connector_set_property *conn_set_prop = data;
  2809. struct drm_mode_obj_set_property obj_set_prop = {
  2810. .value = conn_set_prop->value,
  2811. .prop_id = conn_set_prop->prop_id,
  2812. .obj_id = conn_set_prop->connector_id,
  2813. .obj_type = DRM_MODE_OBJECT_CONNECTOR
  2814. };
  2815. /* It does all the locking and checking we need */
  2816. return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
  2817. }
  2818. static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
  2819. struct drm_property *property,
  2820. uint64_t value)
  2821. {
  2822. int ret = -EINVAL;
  2823. struct drm_connector *connector = obj_to_connector(obj);
  2824. /* Do DPMS ourselves */
  2825. if (property == connector->dev->mode_config.dpms_property) {
  2826. if (connector->funcs->dpms)
  2827. (*connector->funcs->dpms)(connector, (int)value);
  2828. ret = 0;
  2829. } else if (connector->funcs->set_property)
  2830. ret = connector->funcs->set_property(connector, property, value);
  2831. /* store the property value if successful */
  2832. if (!ret)
  2833. drm_object_property_set_value(&connector->base, property, value);
  2834. return ret;
  2835. }
  2836. static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
  2837. struct drm_property *property,
  2838. uint64_t value)
  2839. {
  2840. int ret = -EINVAL;
  2841. struct drm_crtc *crtc = obj_to_crtc(obj);
  2842. if (crtc->funcs->set_property)
  2843. ret = crtc->funcs->set_property(crtc, property, value);
  2844. if (!ret)
  2845. drm_object_property_set_value(obj, property, value);
  2846. return ret;
  2847. }
  2848. static int drm_mode_plane_set_obj_prop(struct drm_mode_object *obj,
  2849. struct drm_property *property,
  2850. uint64_t value)
  2851. {
  2852. int ret = -EINVAL;
  2853. struct drm_plane *plane = obj_to_plane(obj);
  2854. if (plane->funcs->set_property)
  2855. ret = plane->funcs->set_property(plane, property, value);
  2856. if (!ret)
  2857. drm_object_property_set_value(obj, property, value);
  2858. return ret;
  2859. }
  2860. int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
  2861. struct drm_file *file_priv)
  2862. {
  2863. struct drm_mode_obj_get_properties *arg = data;
  2864. struct drm_mode_object *obj;
  2865. int ret = 0;
  2866. int i;
  2867. int copied = 0;
  2868. int props_count = 0;
  2869. uint32_t __user *props_ptr;
  2870. uint64_t __user *prop_values_ptr;
  2871. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2872. return -EINVAL;
  2873. drm_modeset_lock_all(dev);
  2874. obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
  2875. if (!obj) {
  2876. ret = -EINVAL;
  2877. goto out;
  2878. }
  2879. if (!obj->properties) {
  2880. ret = -EINVAL;
  2881. goto out;
  2882. }
  2883. props_count = obj->properties->count;
  2884. /* This ioctl is called twice, once to determine how much space is
  2885. * needed, and the 2nd time to fill it. */
  2886. if ((arg->count_props >= props_count) && props_count) {
  2887. copied = 0;
  2888. props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
  2889. prop_values_ptr = (uint64_t __user *)(unsigned long)
  2890. (arg->prop_values_ptr);
  2891. for (i = 0; i < props_count; i++) {
  2892. if (put_user(obj->properties->ids[i],
  2893. props_ptr + copied)) {
  2894. ret = -EFAULT;
  2895. goto out;
  2896. }
  2897. if (put_user(obj->properties->values[i],
  2898. prop_values_ptr + copied)) {
  2899. ret = -EFAULT;
  2900. goto out;
  2901. }
  2902. copied++;
  2903. }
  2904. }
  2905. arg->count_props = props_count;
  2906. out:
  2907. drm_modeset_unlock_all(dev);
  2908. return ret;
  2909. }
  2910. int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
  2911. struct drm_file *file_priv)
  2912. {
  2913. struct drm_mode_obj_set_property *arg = data;
  2914. struct drm_mode_object *arg_obj;
  2915. struct drm_mode_object *prop_obj;
  2916. struct drm_property *property;
  2917. int ret = -EINVAL;
  2918. int i;
  2919. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2920. return -EINVAL;
  2921. drm_modeset_lock_all(dev);
  2922. arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
  2923. if (!arg_obj)
  2924. goto out;
  2925. if (!arg_obj->properties)
  2926. goto out;
  2927. for (i = 0; i < arg_obj->properties->count; i++)
  2928. if (arg_obj->properties->ids[i] == arg->prop_id)
  2929. break;
  2930. if (i == arg_obj->properties->count)
  2931. goto out;
  2932. prop_obj = drm_mode_object_find(dev, arg->prop_id,
  2933. DRM_MODE_OBJECT_PROPERTY);
  2934. if (!prop_obj)
  2935. goto out;
  2936. property = obj_to_property(prop_obj);
  2937. if (!drm_property_change_is_valid(property, arg->value))
  2938. goto out;
  2939. switch (arg_obj->type) {
  2940. case DRM_MODE_OBJECT_CONNECTOR:
  2941. ret = drm_mode_connector_set_obj_prop(arg_obj, property,
  2942. arg->value);
  2943. break;
  2944. case DRM_MODE_OBJECT_CRTC:
  2945. ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
  2946. break;
  2947. case DRM_MODE_OBJECT_PLANE:
  2948. ret = drm_mode_plane_set_obj_prop(arg_obj, property, arg->value);
  2949. break;
  2950. }
  2951. out:
  2952. drm_modeset_unlock_all(dev);
  2953. return ret;
  2954. }
  2955. int drm_mode_connector_attach_encoder(struct drm_connector *connector,
  2956. struct drm_encoder *encoder)
  2957. {
  2958. int i;
  2959. for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
  2960. if (connector->encoder_ids[i] == 0) {
  2961. connector->encoder_ids[i] = encoder->base.id;
  2962. return 0;
  2963. }
  2964. }
  2965. return -ENOMEM;
  2966. }
  2967. EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
  2968. void drm_mode_connector_detach_encoder(struct drm_connector *connector,
  2969. struct drm_encoder *encoder)
  2970. {
  2971. int i;
  2972. for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
  2973. if (connector->encoder_ids[i] == encoder->base.id) {
  2974. connector->encoder_ids[i] = 0;
  2975. if (connector->encoder == encoder)
  2976. connector->encoder = NULL;
  2977. break;
  2978. }
  2979. }
  2980. }
  2981. EXPORT_SYMBOL(drm_mode_connector_detach_encoder);
  2982. int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
  2983. int gamma_size)
  2984. {
  2985. crtc->gamma_size = gamma_size;
  2986. crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
  2987. if (!crtc->gamma_store) {
  2988. crtc->gamma_size = 0;
  2989. return -ENOMEM;
  2990. }
  2991. return 0;
  2992. }
  2993. EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
  2994. int drm_mode_gamma_set_ioctl(struct drm_device *dev,
  2995. void *data, struct drm_file *file_priv)
  2996. {
  2997. struct drm_mode_crtc_lut *crtc_lut = data;
  2998. struct drm_mode_object *obj;
  2999. struct drm_crtc *crtc;
  3000. void *r_base, *g_base, *b_base;
  3001. int size;
  3002. int ret = 0;
  3003. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  3004. return -EINVAL;
  3005. drm_modeset_lock_all(dev);
  3006. obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
  3007. if (!obj) {
  3008. ret = -EINVAL;
  3009. goto out;
  3010. }
  3011. crtc = obj_to_crtc(obj);
  3012. if (crtc->funcs->gamma_set == NULL) {
  3013. ret = -ENOSYS;
  3014. goto out;
  3015. }
  3016. /* memcpy into gamma store */
  3017. if (crtc_lut->gamma_size != crtc->gamma_size) {
  3018. ret = -EINVAL;
  3019. goto out;
  3020. }
  3021. size = crtc_lut->gamma_size * (sizeof(uint16_t));
  3022. r_base = crtc->gamma_store;
  3023. if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
  3024. ret = -EFAULT;
  3025. goto out;
  3026. }
  3027. g_base = r_base + size;
  3028. if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
  3029. ret = -EFAULT;
  3030. goto out;
  3031. }
  3032. b_base = g_base + size;
  3033. if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
  3034. ret = -EFAULT;
  3035. goto out;
  3036. }
  3037. crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
  3038. out:
  3039. drm_modeset_unlock_all(dev);
  3040. return ret;
  3041. }
  3042. int drm_mode_gamma_get_ioctl(struct drm_device *dev,
  3043. void *data, struct drm_file *file_priv)
  3044. {
  3045. struct drm_mode_crtc_lut *crtc_lut = data;
  3046. struct drm_mode_object *obj;
  3047. struct drm_crtc *crtc;
  3048. void *r_base, *g_base, *b_base;
  3049. int size;
  3050. int ret = 0;
  3051. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  3052. return -EINVAL;
  3053. drm_modeset_lock_all(dev);
  3054. obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
  3055. if (!obj) {
  3056. ret = -EINVAL;
  3057. goto out;
  3058. }
  3059. crtc = obj_to_crtc(obj);
  3060. /* memcpy into gamma store */
  3061. if (crtc_lut->gamma_size != crtc->gamma_size) {
  3062. ret = -EINVAL;
  3063. goto out;
  3064. }
  3065. size = crtc_lut->gamma_size * (sizeof(uint16_t));
  3066. r_base = crtc->gamma_store;
  3067. if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
  3068. ret = -EFAULT;
  3069. goto out;
  3070. }
  3071. g_base = r_base + size;
  3072. if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
  3073. ret = -EFAULT;
  3074. goto out;
  3075. }
  3076. b_base = g_base + size;
  3077. if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
  3078. ret = -EFAULT;
  3079. goto out;
  3080. }
  3081. out:
  3082. drm_modeset_unlock_all(dev);
  3083. return ret;
  3084. }
  3085. int drm_mode_page_flip_ioctl(struct drm_device *dev,
  3086. void *data, struct drm_file *file_priv)
  3087. {
  3088. struct drm_mode_crtc_page_flip *page_flip = data;
  3089. struct drm_mode_object *obj;
  3090. struct drm_crtc *crtc;
  3091. struct drm_framebuffer *fb = NULL, *old_fb = NULL;
  3092. struct drm_pending_vblank_event *e = NULL;
  3093. unsigned long flags;
  3094. int ret = -EINVAL;
  3095. if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
  3096. page_flip->reserved != 0)
  3097. return -EINVAL;
  3098. if ((page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC) && !dev->mode_config.async_page_flip)
  3099. return -EINVAL;
  3100. obj = drm_mode_object_find(dev, page_flip->crtc_id, DRM_MODE_OBJECT_CRTC);
  3101. if (!obj)
  3102. return -EINVAL;
  3103. crtc = obj_to_crtc(obj);
  3104. mutex_lock(&crtc->mutex);
  3105. if (crtc->fb == NULL) {
  3106. /* The framebuffer is currently unbound, presumably
  3107. * due to a hotplug event, that userspace has not
  3108. * yet discovered.
  3109. */
  3110. ret = -EBUSY;
  3111. goto out;
  3112. }
  3113. if (crtc->funcs->page_flip == NULL)
  3114. goto out;
  3115. fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
  3116. if (!fb)
  3117. goto out;
  3118. ret = drm_crtc_check_viewport(crtc, crtc->x, crtc->y, &crtc->mode, fb);
  3119. if (ret)
  3120. goto out;
  3121. if (crtc->fb->pixel_format != fb->pixel_format) {
  3122. DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
  3123. ret = -EINVAL;
  3124. goto out;
  3125. }
  3126. if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
  3127. ret = -ENOMEM;
  3128. spin_lock_irqsave(&dev->event_lock, flags);
  3129. if (file_priv->event_space < sizeof e->event) {
  3130. spin_unlock_irqrestore(&dev->event_lock, flags);
  3131. goto out;
  3132. }
  3133. file_priv->event_space -= sizeof e->event;
  3134. spin_unlock_irqrestore(&dev->event_lock, flags);
  3135. e = kzalloc(sizeof *e, GFP_KERNEL);
  3136. if (e == NULL) {
  3137. spin_lock_irqsave(&dev->event_lock, flags);
  3138. file_priv->event_space += sizeof e->event;
  3139. spin_unlock_irqrestore(&dev->event_lock, flags);
  3140. goto out;
  3141. }
  3142. e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
  3143. e->event.base.length = sizeof e->event;
  3144. e->event.user_data = page_flip->user_data;
  3145. e->base.event = &e->event.base;
  3146. e->base.file_priv = file_priv;
  3147. e->base.destroy =
  3148. (void (*) (struct drm_pending_event *)) kfree;
  3149. }
  3150. old_fb = crtc->fb;
  3151. ret = crtc->funcs->page_flip(crtc, fb, e, page_flip->flags);
  3152. if (ret) {
  3153. if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
  3154. spin_lock_irqsave(&dev->event_lock, flags);
  3155. file_priv->event_space += sizeof e->event;
  3156. spin_unlock_irqrestore(&dev->event_lock, flags);
  3157. kfree(e);
  3158. }
  3159. /* Keep the old fb, don't unref it. */
  3160. old_fb = NULL;
  3161. } else {
  3162. /*
  3163. * Warn if the driver hasn't properly updated the crtc->fb
  3164. * field to reflect that the new framebuffer is now used.
  3165. * Failing to do so will screw with the reference counting
  3166. * on framebuffers.
  3167. */
  3168. WARN_ON(crtc->fb != fb);
  3169. /* Unref only the old framebuffer. */
  3170. fb = NULL;
  3171. }
  3172. out:
  3173. if (fb)
  3174. drm_framebuffer_unreference(fb);
  3175. if (old_fb)
  3176. drm_framebuffer_unreference(old_fb);
  3177. mutex_unlock(&crtc->mutex);
  3178. return ret;
  3179. }
  3180. void drm_mode_config_reset(struct drm_device *dev)
  3181. {
  3182. struct drm_crtc *crtc;
  3183. struct drm_encoder *encoder;
  3184. struct drm_connector *connector;
  3185. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
  3186. if (crtc->funcs->reset)
  3187. crtc->funcs->reset(crtc);
  3188. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
  3189. if (encoder->funcs->reset)
  3190. encoder->funcs->reset(encoder);
  3191. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  3192. connector->status = connector_status_unknown;
  3193. if (connector->funcs->reset)
  3194. connector->funcs->reset(connector);
  3195. }
  3196. }
  3197. EXPORT_SYMBOL(drm_mode_config_reset);
  3198. int drm_mode_create_dumb_ioctl(struct drm_device *dev,
  3199. void *data, struct drm_file *file_priv)
  3200. {
  3201. struct drm_mode_create_dumb *args = data;
  3202. if (!dev->driver->dumb_create)
  3203. return -ENOSYS;
  3204. return dev->driver->dumb_create(file_priv, dev, args);
  3205. }
  3206. int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
  3207. void *data, struct drm_file *file_priv)
  3208. {
  3209. struct drm_mode_map_dumb *args = data;
  3210. /* call driver ioctl to get mmap offset */
  3211. if (!dev->driver->dumb_map_offset)
  3212. return -ENOSYS;
  3213. return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
  3214. }
  3215. int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
  3216. void *data, struct drm_file *file_priv)
  3217. {
  3218. struct drm_mode_destroy_dumb *args = data;
  3219. if (!dev->driver->dumb_destroy)
  3220. return -ENOSYS;
  3221. return dev->driver->dumb_destroy(file_priv, dev, args->handle);
  3222. }
  3223. /*
  3224. * Just need to support RGB formats here for compat with code that doesn't
  3225. * use pixel formats directly yet.
  3226. */
  3227. void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
  3228. int *bpp)
  3229. {
  3230. switch (format) {
  3231. case DRM_FORMAT_C8:
  3232. case DRM_FORMAT_RGB332:
  3233. case DRM_FORMAT_BGR233:
  3234. *depth = 8;
  3235. *bpp = 8;
  3236. break;
  3237. case DRM_FORMAT_XRGB1555:
  3238. case DRM_FORMAT_XBGR1555:
  3239. case DRM_FORMAT_RGBX5551:
  3240. case DRM_FORMAT_BGRX5551:
  3241. case DRM_FORMAT_ARGB1555:
  3242. case DRM_FORMAT_ABGR1555:
  3243. case DRM_FORMAT_RGBA5551:
  3244. case DRM_FORMAT_BGRA5551:
  3245. *depth = 15;
  3246. *bpp = 16;
  3247. break;
  3248. case DRM_FORMAT_RGB565:
  3249. case DRM_FORMAT_BGR565:
  3250. *depth = 16;
  3251. *bpp = 16;
  3252. break;
  3253. case DRM_FORMAT_RGB888:
  3254. case DRM_FORMAT_BGR888:
  3255. *depth = 24;
  3256. *bpp = 24;
  3257. break;
  3258. case DRM_FORMAT_XRGB8888:
  3259. case DRM_FORMAT_XBGR8888:
  3260. case DRM_FORMAT_RGBX8888:
  3261. case DRM_FORMAT_BGRX8888:
  3262. *depth = 24;
  3263. *bpp = 32;
  3264. break;
  3265. case DRM_FORMAT_XRGB2101010:
  3266. case DRM_FORMAT_XBGR2101010:
  3267. case DRM_FORMAT_RGBX1010102:
  3268. case DRM_FORMAT_BGRX1010102:
  3269. case DRM_FORMAT_ARGB2101010:
  3270. case DRM_FORMAT_ABGR2101010:
  3271. case DRM_FORMAT_RGBA1010102:
  3272. case DRM_FORMAT_BGRA1010102:
  3273. *depth = 30;
  3274. *bpp = 32;
  3275. break;
  3276. case DRM_FORMAT_ARGB8888:
  3277. case DRM_FORMAT_ABGR8888:
  3278. case DRM_FORMAT_RGBA8888:
  3279. case DRM_FORMAT_BGRA8888:
  3280. *depth = 32;
  3281. *bpp = 32;
  3282. break;
  3283. default:
  3284. DRM_DEBUG_KMS("unsupported pixel format\n");
  3285. *depth = 0;
  3286. *bpp = 0;
  3287. break;
  3288. }
  3289. }
  3290. EXPORT_SYMBOL(drm_fb_get_bpp_depth);
  3291. /**
  3292. * drm_format_num_planes - get the number of planes for format
  3293. * @format: pixel format (DRM_FORMAT_*)
  3294. *
  3295. * RETURNS:
  3296. * The number of planes used by the specified pixel format.
  3297. */
  3298. int drm_format_num_planes(uint32_t format)
  3299. {
  3300. switch (format) {
  3301. case DRM_FORMAT_YUV410:
  3302. case DRM_FORMAT_YVU410:
  3303. case DRM_FORMAT_YUV411:
  3304. case DRM_FORMAT_YVU411:
  3305. case DRM_FORMAT_YUV420:
  3306. case DRM_FORMAT_YVU420:
  3307. case DRM_FORMAT_YUV422:
  3308. case DRM_FORMAT_YVU422:
  3309. case DRM_FORMAT_YUV444:
  3310. case DRM_FORMAT_YVU444:
  3311. return 3;
  3312. case DRM_FORMAT_NV12:
  3313. case DRM_FORMAT_NV21:
  3314. case DRM_FORMAT_NV16:
  3315. case DRM_FORMAT_NV61:
  3316. case DRM_FORMAT_NV24:
  3317. case DRM_FORMAT_NV42:
  3318. return 2;
  3319. default:
  3320. return 1;
  3321. }
  3322. }
  3323. EXPORT_SYMBOL(drm_format_num_planes);
  3324. /**
  3325. * drm_format_plane_cpp - determine the bytes per pixel value
  3326. * @format: pixel format (DRM_FORMAT_*)
  3327. * @plane: plane index
  3328. *
  3329. * RETURNS:
  3330. * The bytes per pixel value for the specified plane.
  3331. */
  3332. int drm_format_plane_cpp(uint32_t format, int plane)
  3333. {
  3334. unsigned int depth;
  3335. int bpp;
  3336. if (plane >= drm_format_num_planes(format))
  3337. return 0;
  3338. switch (format) {
  3339. case DRM_FORMAT_YUYV:
  3340. case DRM_FORMAT_YVYU:
  3341. case DRM_FORMAT_UYVY:
  3342. case DRM_FORMAT_VYUY:
  3343. return 2;
  3344. case DRM_FORMAT_NV12:
  3345. case DRM_FORMAT_NV21:
  3346. case DRM_FORMAT_NV16:
  3347. case DRM_FORMAT_NV61:
  3348. case DRM_FORMAT_NV24:
  3349. case DRM_FORMAT_NV42:
  3350. return plane ? 2 : 1;
  3351. case DRM_FORMAT_YUV410:
  3352. case DRM_FORMAT_YVU410:
  3353. case DRM_FORMAT_YUV411:
  3354. case DRM_FORMAT_YVU411:
  3355. case DRM_FORMAT_YUV420:
  3356. case DRM_FORMAT_YVU420:
  3357. case DRM_FORMAT_YUV422:
  3358. case DRM_FORMAT_YVU422:
  3359. case DRM_FORMAT_YUV444:
  3360. case DRM_FORMAT_YVU444:
  3361. return 1;
  3362. default:
  3363. drm_fb_get_bpp_depth(format, &depth, &bpp);
  3364. return bpp >> 3;
  3365. }
  3366. }
  3367. EXPORT_SYMBOL(drm_format_plane_cpp);
  3368. /**
  3369. * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
  3370. * @format: pixel format (DRM_FORMAT_*)
  3371. *
  3372. * RETURNS:
  3373. * The horizontal chroma subsampling factor for the
  3374. * specified pixel format.
  3375. */
  3376. int drm_format_horz_chroma_subsampling(uint32_t format)
  3377. {
  3378. switch (format) {
  3379. case DRM_FORMAT_YUV411:
  3380. case DRM_FORMAT_YVU411:
  3381. case DRM_FORMAT_YUV410:
  3382. case DRM_FORMAT_YVU410:
  3383. return 4;
  3384. case DRM_FORMAT_YUYV:
  3385. case DRM_FORMAT_YVYU:
  3386. case DRM_FORMAT_UYVY:
  3387. case DRM_FORMAT_VYUY:
  3388. case DRM_FORMAT_NV12:
  3389. case DRM_FORMAT_NV21:
  3390. case DRM_FORMAT_NV16:
  3391. case DRM_FORMAT_NV61:
  3392. case DRM_FORMAT_YUV422:
  3393. case DRM_FORMAT_YVU422:
  3394. case DRM_FORMAT_YUV420:
  3395. case DRM_FORMAT_YVU420:
  3396. return 2;
  3397. default:
  3398. return 1;
  3399. }
  3400. }
  3401. EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
  3402. /**
  3403. * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
  3404. * @format: pixel format (DRM_FORMAT_*)
  3405. *
  3406. * RETURNS:
  3407. * The vertical chroma subsampling factor for the
  3408. * specified pixel format.
  3409. */
  3410. int drm_format_vert_chroma_subsampling(uint32_t format)
  3411. {
  3412. switch (format) {
  3413. case DRM_FORMAT_YUV410:
  3414. case DRM_FORMAT_YVU410:
  3415. return 4;
  3416. case DRM_FORMAT_YUV420:
  3417. case DRM_FORMAT_YVU420:
  3418. case DRM_FORMAT_NV12:
  3419. case DRM_FORMAT_NV21:
  3420. return 2;
  3421. default:
  3422. return 1;
  3423. }
  3424. }
  3425. EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);
  3426. /**
  3427. * drm_mode_config_init - initialize DRM mode_configuration structure
  3428. * @dev: DRM device
  3429. *
  3430. * Initialize @dev's mode_config structure, used for tracking the graphics
  3431. * configuration of @dev.
  3432. *
  3433. * Since this initializes the modeset locks, no locking is possible. Which is no
  3434. * problem, since this should happen single threaded at init time. It is the
  3435. * driver's problem to ensure this guarantee.
  3436. *
  3437. */
  3438. void drm_mode_config_init(struct drm_device *dev)
  3439. {
  3440. mutex_init(&dev->mode_config.mutex);
  3441. mutex_init(&dev->mode_config.idr_mutex);
  3442. mutex_init(&dev->mode_config.fb_lock);
  3443. INIT_LIST_HEAD(&dev->mode_config.fb_list);
  3444. INIT_LIST_HEAD(&dev->mode_config.crtc_list);
  3445. INIT_LIST_HEAD(&dev->mode_config.connector_list);
  3446. INIT_LIST_HEAD(&dev->mode_config.bridge_list);
  3447. INIT_LIST_HEAD(&dev->mode_config.encoder_list);
  3448. INIT_LIST_HEAD(&dev->mode_config.property_list);
  3449. INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
  3450. INIT_LIST_HEAD(&dev->mode_config.plane_list);
  3451. idr_init(&dev->mode_config.crtc_idr);
  3452. drm_modeset_lock_all(dev);
  3453. drm_mode_create_standard_connector_properties(dev);
  3454. drm_modeset_unlock_all(dev);
  3455. /* Just to be sure */
  3456. dev->mode_config.num_fb = 0;
  3457. dev->mode_config.num_connector = 0;
  3458. dev->mode_config.num_crtc = 0;
  3459. dev->mode_config.num_encoder = 0;
  3460. }
  3461. EXPORT_SYMBOL(drm_mode_config_init);
  3462. /**
  3463. * drm_mode_config_cleanup - free up DRM mode_config info
  3464. * @dev: DRM device
  3465. *
  3466. * Free up all the connectors and CRTCs associated with this DRM device, then
  3467. * free up the framebuffers and associated buffer objects.
  3468. *
  3469. * Note that since this /should/ happen single-threaded at driver/device
  3470. * teardown time, no locking is required. It's the driver's job to ensure that
  3471. * this guarantee actually holds true.
  3472. *
  3473. * FIXME: cleanup any dangling user buffer objects too
  3474. */
  3475. void drm_mode_config_cleanup(struct drm_device *dev)
  3476. {
  3477. struct drm_connector *connector, *ot;
  3478. struct drm_crtc *crtc, *ct;
  3479. struct drm_encoder *encoder, *enct;
  3480. struct drm_bridge *bridge, *brt;
  3481. struct drm_framebuffer *fb, *fbt;
  3482. struct drm_property *property, *pt;
  3483. struct drm_property_blob *blob, *bt;
  3484. struct drm_plane *plane, *plt;
  3485. list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
  3486. head) {
  3487. encoder->funcs->destroy(encoder);
  3488. }
  3489. list_for_each_entry_safe(bridge, brt,
  3490. &dev->mode_config.bridge_list, head) {
  3491. bridge->funcs->destroy(bridge);
  3492. }
  3493. list_for_each_entry_safe(connector, ot,
  3494. &dev->mode_config.connector_list, head) {
  3495. connector->funcs->destroy(connector);
  3496. }
  3497. list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
  3498. head) {
  3499. drm_property_destroy(dev, property);
  3500. }
  3501. list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
  3502. head) {
  3503. drm_property_destroy_blob(dev, blob);
  3504. }
  3505. /*
  3506. * Single-threaded teardown context, so it's not required to grab the
  3507. * fb_lock to protect against concurrent fb_list access. Contrary, it
  3508. * would actually deadlock with the drm_framebuffer_cleanup function.
  3509. *
  3510. * Also, if there are any framebuffers left, that's a driver leak now,
  3511. * so politely WARN about this.
  3512. */
  3513. WARN_ON(!list_empty(&dev->mode_config.fb_list));
  3514. list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
  3515. drm_framebuffer_remove(fb);
  3516. }
  3517. list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
  3518. head) {
  3519. plane->funcs->destroy(plane);
  3520. }
  3521. list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
  3522. crtc->funcs->destroy(crtc);
  3523. }
  3524. idr_destroy(&dev->mode_config.crtc_idr);
  3525. }
  3526. EXPORT_SYMBOL(drm_mode_config_cleanup);