drm_crtc.c 103 KB

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