drm_crtc.c 98 KB

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