drm_crtc.c 102 KB

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