drm_crtc.c 104 KB

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