drm_crtc.c 102 KB

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