drm_crtc.c 99 KB

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