drm_crtc.c 102 KB

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