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. kfree(crtc->gamma_store);
  419. crtc->gamma_store = NULL;
  420. drm_mode_object_put(dev, &crtc->base);
  421. list_del(&crtc->head);
  422. dev->mode_config.num_crtc--;
  423. }
  424. EXPORT_SYMBOL(drm_crtc_cleanup);
  425. /**
  426. * drm_mode_probed_add - add a mode to a connector's probed mode list
  427. * @connector: connector the new mode
  428. * @mode: mode data
  429. *
  430. * LOCKING:
  431. * Caller must hold mode config lock.
  432. *
  433. * Add @mode to @connector's mode list for later use.
  434. */
  435. void drm_mode_probed_add(struct drm_connector *connector,
  436. struct drm_display_mode *mode)
  437. {
  438. list_add(&mode->head, &connector->probed_modes);
  439. }
  440. EXPORT_SYMBOL(drm_mode_probed_add);
  441. /**
  442. * drm_mode_remove - remove and free a mode
  443. * @connector: connector list to modify
  444. * @mode: mode to remove
  445. *
  446. * LOCKING:
  447. * Caller must hold mode config lock.
  448. *
  449. * Remove @mode from @connector's mode list, then free it.
  450. */
  451. void drm_mode_remove(struct drm_connector *connector,
  452. struct drm_display_mode *mode)
  453. {
  454. list_del(&mode->head);
  455. drm_mode_destroy(connector->dev, mode);
  456. }
  457. EXPORT_SYMBOL(drm_mode_remove);
  458. /**
  459. * drm_connector_init - Init a preallocated connector
  460. * @dev: DRM device
  461. * @connector: the connector to init
  462. * @funcs: callbacks for this connector
  463. * @name: user visible name of the connector
  464. *
  465. * LOCKING:
  466. * Takes mode config lock.
  467. *
  468. * Initialises a preallocated connector. Connectors should be
  469. * subclassed as part of driver connector objects.
  470. *
  471. * RETURNS:
  472. * Zero on success, error code on failure.
  473. */
  474. int drm_connector_init(struct drm_device *dev,
  475. struct drm_connector *connector,
  476. const struct drm_connector_funcs *funcs,
  477. int connector_type)
  478. {
  479. int ret;
  480. mutex_lock(&dev->mode_config.mutex);
  481. ret = drm_mode_object_get(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR);
  482. if (ret)
  483. goto out;
  484. connector->base.properties = &connector->properties;
  485. connector->dev = dev;
  486. connector->funcs = funcs;
  487. connector->connector_type = connector_type;
  488. connector->connector_type_id =
  489. ++drm_connector_enum_list[connector_type].count; /* TODO */
  490. INIT_LIST_HEAD(&connector->user_modes);
  491. INIT_LIST_HEAD(&connector->probed_modes);
  492. INIT_LIST_HEAD(&connector->modes);
  493. connector->edid_blob_ptr = NULL;
  494. list_add_tail(&connector->head, &dev->mode_config.connector_list);
  495. dev->mode_config.num_connector++;
  496. if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
  497. drm_connector_attach_property(connector,
  498. dev->mode_config.edid_property,
  499. 0);
  500. drm_connector_attach_property(connector,
  501. dev->mode_config.dpms_property, 0);
  502. out:
  503. mutex_unlock(&dev->mode_config.mutex);
  504. return ret;
  505. }
  506. EXPORT_SYMBOL(drm_connector_init);
  507. /**
  508. * drm_connector_cleanup - cleans up an initialised connector
  509. * @connector: connector to cleanup
  510. *
  511. * LOCKING:
  512. * Takes mode config lock.
  513. *
  514. * Cleans up the connector but doesn't free the object.
  515. */
  516. void drm_connector_cleanup(struct drm_connector *connector)
  517. {
  518. struct drm_device *dev = connector->dev;
  519. struct drm_display_mode *mode, *t;
  520. list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
  521. drm_mode_remove(connector, mode);
  522. list_for_each_entry_safe(mode, t, &connector->modes, head)
  523. drm_mode_remove(connector, mode);
  524. list_for_each_entry_safe(mode, t, &connector->user_modes, head)
  525. drm_mode_remove(connector, mode);
  526. mutex_lock(&dev->mode_config.mutex);
  527. drm_mode_object_put(dev, &connector->base);
  528. list_del(&connector->head);
  529. dev->mode_config.num_connector--;
  530. mutex_unlock(&dev->mode_config.mutex);
  531. }
  532. EXPORT_SYMBOL(drm_connector_cleanup);
  533. void drm_connector_unplug_all(struct drm_device *dev)
  534. {
  535. struct drm_connector *connector;
  536. /* taking the mode config mutex ends up in a clash with sysfs */
  537. list_for_each_entry(connector, &dev->mode_config.connector_list, head)
  538. drm_sysfs_connector_remove(connector);
  539. }
  540. EXPORT_SYMBOL(drm_connector_unplug_all);
  541. int drm_encoder_init(struct drm_device *dev,
  542. struct drm_encoder *encoder,
  543. const struct drm_encoder_funcs *funcs,
  544. int encoder_type)
  545. {
  546. int ret;
  547. mutex_lock(&dev->mode_config.mutex);
  548. ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
  549. if (ret)
  550. goto out;
  551. encoder->dev = dev;
  552. encoder->encoder_type = encoder_type;
  553. encoder->funcs = funcs;
  554. list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
  555. dev->mode_config.num_encoder++;
  556. out:
  557. mutex_unlock(&dev->mode_config.mutex);
  558. return ret;
  559. }
  560. EXPORT_SYMBOL(drm_encoder_init);
  561. void drm_encoder_cleanup(struct drm_encoder *encoder)
  562. {
  563. struct drm_device *dev = encoder->dev;
  564. mutex_lock(&dev->mode_config.mutex);
  565. drm_mode_object_put(dev, &encoder->base);
  566. list_del(&encoder->head);
  567. dev->mode_config.num_encoder--;
  568. mutex_unlock(&dev->mode_config.mutex);
  569. }
  570. EXPORT_SYMBOL(drm_encoder_cleanup);
  571. int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
  572. unsigned long possible_crtcs,
  573. const struct drm_plane_funcs *funcs,
  574. const uint32_t *formats, uint32_t format_count,
  575. bool priv)
  576. {
  577. int ret;
  578. mutex_lock(&dev->mode_config.mutex);
  579. ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
  580. if (ret)
  581. goto out;
  582. plane->base.properties = &plane->properties;
  583. plane->dev = dev;
  584. plane->funcs = funcs;
  585. plane->format_types = kmalloc(sizeof(uint32_t) * format_count,
  586. GFP_KERNEL);
  587. if (!plane->format_types) {
  588. DRM_DEBUG_KMS("out of memory when allocating plane\n");
  589. drm_mode_object_put(dev, &plane->base);
  590. ret = -ENOMEM;
  591. goto out;
  592. }
  593. memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
  594. plane->format_count = format_count;
  595. plane->possible_crtcs = possible_crtcs;
  596. /* private planes are not exposed to userspace, but depending on
  597. * display hardware, might be convenient to allow sharing programming
  598. * for the scanout engine with the crtc implementation.
  599. */
  600. if (!priv) {
  601. list_add_tail(&plane->head, &dev->mode_config.plane_list);
  602. dev->mode_config.num_plane++;
  603. } else {
  604. INIT_LIST_HEAD(&plane->head);
  605. }
  606. out:
  607. mutex_unlock(&dev->mode_config.mutex);
  608. return ret;
  609. }
  610. EXPORT_SYMBOL(drm_plane_init);
  611. void drm_plane_cleanup(struct drm_plane *plane)
  612. {
  613. struct drm_device *dev = plane->dev;
  614. mutex_lock(&dev->mode_config.mutex);
  615. kfree(plane->format_types);
  616. drm_mode_object_put(dev, &plane->base);
  617. /* if not added to a list, it must be a private plane */
  618. if (!list_empty(&plane->head)) {
  619. list_del(&plane->head);
  620. dev->mode_config.num_plane--;
  621. }
  622. mutex_unlock(&dev->mode_config.mutex);
  623. }
  624. EXPORT_SYMBOL(drm_plane_cleanup);
  625. /**
  626. * drm_mode_create - create a new display mode
  627. * @dev: DRM device
  628. *
  629. * LOCKING:
  630. * Caller must hold DRM mode_config lock.
  631. *
  632. * Create a new drm_display_mode, give it an ID, and return it.
  633. *
  634. * RETURNS:
  635. * Pointer to new mode on success, NULL on error.
  636. */
  637. struct drm_display_mode *drm_mode_create(struct drm_device *dev)
  638. {
  639. struct drm_display_mode *nmode;
  640. nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
  641. if (!nmode)
  642. return NULL;
  643. if (drm_mode_object_get(dev, &nmode->base, DRM_MODE_OBJECT_MODE)) {
  644. kfree(nmode);
  645. return NULL;
  646. }
  647. return nmode;
  648. }
  649. EXPORT_SYMBOL(drm_mode_create);
  650. /**
  651. * drm_mode_destroy - remove a mode
  652. * @dev: DRM device
  653. * @mode: mode to remove
  654. *
  655. * LOCKING:
  656. * Caller must hold mode config lock.
  657. *
  658. * Free @mode's unique identifier, then free it.
  659. */
  660. void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
  661. {
  662. if (!mode)
  663. return;
  664. drm_mode_object_put(dev, &mode->base);
  665. kfree(mode);
  666. }
  667. EXPORT_SYMBOL(drm_mode_destroy);
  668. static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
  669. {
  670. struct drm_property *edid;
  671. struct drm_property *dpms;
  672. /*
  673. * Standard properties (apply to all connectors)
  674. */
  675. edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
  676. DRM_MODE_PROP_IMMUTABLE,
  677. "EDID", 0);
  678. dev->mode_config.edid_property = edid;
  679. dpms = drm_property_create_enum(dev, 0,
  680. "DPMS", drm_dpms_enum_list,
  681. ARRAY_SIZE(drm_dpms_enum_list));
  682. dev->mode_config.dpms_property = dpms;
  683. return 0;
  684. }
  685. /**
  686. * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
  687. * @dev: DRM device
  688. *
  689. * Called by a driver the first time a DVI-I connector is made.
  690. */
  691. int drm_mode_create_dvi_i_properties(struct drm_device *dev)
  692. {
  693. struct drm_property *dvi_i_selector;
  694. struct drm_property *dvi_i_subconnector;
  695. if (dev->mode_config.dvi_i_select_subconnector_property)
  696. return 0;
  697. dvi_i_selector =
  698. drm_property_create_enum(dev, 0,
  699. "select subconnector",
  700. drm_dvi_i_select_enum_list,
  701. ARRAY_SIZE(drm_dvi_i_select_enum_list));
  702. dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
  703. dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
  704. "subconnector",
  705. drm_dvi_i_subconnector_enum_list,
  706. ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
  707. dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
  708. return 0;
  709. }
  710. EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
  711. /**
  712. * drm_create_tv_properties - create TV specific connector properties
  713. * @dev: DRM device
  714. * @num_modes: number of different TV formats (modes) supported
  715. * @modes: array of pointers to strings containing name of each format
  716. *
  717. * Called by a driver's TV initialization routine, this function creates
  718. * the TV specific connector properties for a given device. Caller is
  719. * responsible for allocating a list of format names and passing them to
  720. * this routine.
  721. */
  722. int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
  723. char *modes[])
  724. {
  725. struct drm_property *tv_selector;
  726. struct drm_property *tv_subconnector;
  727. int i;
  728. if (dev->mode_config.tv_select_subconnector_property)
  729. return 0;
  730. /*
  731. * Basic connector properties
  732. */
  733. tv_selector = drm_property_create_enum(dev, 0,
  734. "select subconnector",
  735. drm_tv_select_enum_list,
  736. ARRAY_SIZE(drm_tv_select_enum_list));
  737. dev->mode_config.tv_select_subconnector_property = tv_selector;
  738. tv_subconnector =
  739. drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
  740. "subconnector",
  741. drm_tv_subconnector_enum_list,
  742. ARRAY_SIZE(drm_tv_subconnector_enum_list));
  743. dev->mode_config.tv_subconnector_property = tv_subconnector;
  744. /*
  745. * Other, TV specific properties: margins & TV modes.
  746. */
  747. dev->mode_config.tv_left_margin_property =
  748. drm_property_create_range(dev, 0, "left margin", 0, 100);
  749. dev->mode_config.tv_right_margin_property =
  750. drm_property_create_range(dev, 0, "right margin", 0, 100);
  751. dev->mode_config.tv_top_margin_property =
  752. drm_property_create_range(dev, 0, "top margin", 0, 100);
  753. dev->mode_config.tv_bottom_margin_property =
  754. drm_property_create_range(dev, 0, "bottom margin", 0, 100);
  755. dev->mode_config.tv_mode_property =
  756. drm_property_create(dev, DRM_MODE_PROP_ENUM,
  757. "mode", num_modes);
  758. for (i = 0; i < num_modes; i++)
  759. drm_property_add_enum(dev->mode_config.tv_mode_property, i,
  760. i, modes[i]);
  761. dev->mode_config.tv_brightness_property =
  762. drm_property_create_range(dev, 0, "brightness", 0, 100);
  763. dev->mode_config.tv_contrast_property =
  764. drm_property_create_range(dev, 0, "contrast", 0, 100);
  765. dev->mode_config.tv_flicker_reduction_property =
  766. drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
  767. dev->mode_config.tv_overscan_property =
  768. drm_property_create_range(dev, 0, "overscan", 0, 100);
  769. dev->mode_config.tv_saturation_property =
  770. drm_property_create_range(dev, 0, "saturation", 0, 100);
  771. dev->mode_config.tv_hue_property =
  772. drm_property_create_range(dev, 0, "hue", 0, 100);
  773. return 0;
  774. }
  775. EXPORT_SYMBOL(drm_mode_create_tv_properties);
  776. /**
  777. * drm_mode_create_scaling_mode_property - create scaling mode property
  778. * @dev: DRM device
  779. *
  780. * Called by a driver the first time it's needed, must be attached to desired
  781. * connectors.
  782. */
  783. int drm_mode_create_scaling_mode_property(struct drm_device *dev)
  784. {
  785. struct drm_property *scaling_mode;
  786. if (dev->mode_config.scaling_mode_property)
  787. return 0;
  788. scaling_mode =
  789. drm_property_create_enum(dev, 0, "scaling mode",
  790. drm_scaling_mode_enum_list,
  791. ARRAY_SIZE(drm_scaling_mode_enum_list));
  792. dev->mode_config.scaling_mode_property = scaling_mode;
  793. return 0;
  794. }
  795. EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
  796. /**
  797. * drm_mode_create_dithering_property - create dithering property
  798. * @dev: DRM device
  799. *
  800. * Called by a driver the first time it's needed, must be attached to desired
  801. * connectors.
  802. */
  803. int drm_mode_create_dithering_property(struct drm_device *dev)
  804. {
  805. struct drm_property *dithering_mode;
  806. if (dev->mode_config.dithering_mode_property)
  807. return 0;
  808. dithering_mode =
  809. drm_property_create_enum(dev, 0, "dithering",
  810. drm_dithering_mode_enum_list,
  811. ARRAY_SIZE(drm_dithering_mode_enum_list));
  812. dev->mode_config.dithering_mode_property = dithering_mode;
  813. return 0;
  814. }
  815. EXPORT_SYMBOL(drm_mode_create_dithering_property);
  816. /**
  817. * drm_mode_create_dirty_property - create dirty property
  818. * @dev: DRM device
  819. *
  820. * Called by a driver the first time it's needed, must be attached to desired
  821. * connectors.
  822. */
  823. int drm_mode_create_dirty_info_property(struct drm_device *dev)
  824. {
  825. struct drm_property *dirty_info;
  826. if (dev->mode_config.dirty_info_property)
  827. return 0;
  828. dirty_info =
  829. drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
  830. "dirty",
  831. drm_dirty_info_enum_list,
  832. ARRAY_SIZE(drm_dirty_info_enum_list));
  833. dev->mode_config.dirty_info_property = dirty_info;
  834. return 0;
  835. }
  836. EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
  837. /**
  838. * drm_mode_config_init - initialize DRM mode_configuration structure
  839. * @dev: DRM device
  840. *
  841. * LOCKING:
  842. * None, should happen single threaded at init time.
  843. *
  844. * Initialize @dev's mode_config structure, used for tracking the graphics
  845. * configuration of @dev.
  846. */
  847. void drm_mode_config_init(struct drm_device *dev)
  848. {
  849. mutex_init(&dev->mode_config.mutex);
  850. mutex_init(&dev->mode_config.idr_mutex);
  851. INIT_LIST_HEAD(&dev->mode_config.fb_list);
  852. INIT_LIST_HEAD(&dev->mode_config.crtc_list);
  853. INIT_LIST_HEAD(&dev->mode_config.connector_list);
  854. INIT_LIST_HEAD(&dev->mode_config.encoder_list);
  855. INIT_LIST_HEAD(&dev->mode_config.property_list);
  856. INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
  857. INIT_LIST_HEAD(&dev->mode_config.plane_list);
  858. idr_init(&dev->mode_config.crtc_idr);
  859. mutex_lock(&dev->mode_config.mutex);
  860. drm_mode_create_standard_connector_properties(dev);
  861. mutex_unlock(&dev->mode_config.mutex);
  862. /* Just to be sure */
  863. dev->mode_config.num_fb = 0;
  864. dev->mode_config.num_connector = 0;
  865. dev->mode_config.num_crtc = 0;
  866. dev->mode_config.num_encoder = 0;
  867. }
  868. EXPORT_SYMBOL(drm_mode_config_init);
  869. int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
  870. {
  871. uint32_t total_objects = 0;
  872. total_objects += dev->mode_config.num_crtc;
  873. total_objects += dev->mode_config.num_connector;
  874. total_objects += dev->mode_config.num_encoder;
  875. group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
  876. if (!group->id_list)
  877. return -ENOMEM;
  878. group->num_crtcs = 0;
  879. group->num_connectors = 0;
  880. group->num_encoders = 0;
  881. return 0;
  882. }
  883. int drm_mode_group_init_legacy_group(struct drm_device *dev,
  884. struct drm_mode_group *group)
  885. {
  886. struct drm_crtc *crtc;
  887. struct drm_encoder *encoder;
  888. struct drm_connector *connector;
  889. int ret;
  890. if ((ret = drm_mode_group_init(dev, group)))
  891. return ret;
  892. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
  893. group->id_list[group->num_crtcs++] = crtc->base.id;
  894. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
  895. group->id_list[group->num_crtcs + group->num_encoders++] =
  896. encoder->base.id;
  897. list_for_each_entry(connector, &dev->mode_config.connector_list, head)
  898. group->id_list[group->num_crtcs + group->num_encoders +
  899. group->num_connectors++] = connector->base.id;
  900. return 0;
  901. }
  902. EXPORT_SYMBOL(drm_mode_group_init_legacy_group);
  903. /**
  904. * drm_mode_config_cleanup - free up DRM mode_config info
  905. * @dev: DRM device
  906. *
  907. * LOCKING:
  908. * Caller must hold mode config lock.
  909. *
  910. * Free up all the connectors and CRTCs associated with this DRM device, then
  911. * free up the framebuffers and associated buffer objects.
  912. *
  913. * FIXME: cleanup any dangling user buffer objects too
  914. */
  915. void drm_mode_config_cleanup(struct drm_device *dev)
  916. {
  917. struct drm_connector *connector, *ot;
  918. struct drm_crtc *crtc, *ct;
  919. struct drm_encoder *encoder, *enct;
  920. struct drm_framebuffer *fb, *fbt;
  921. struct drm_property *property, *pt;
  922. struct drm_plane *plane, *plt;
  923. list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
  924. head) {
  925. encoder->funcs->destroy(encoder);
  926. }
  927. list_for_each_entry_safe(connector, ot,
  928. &dev->mode_config.connector_list, head) {
  929. connector->funcs->destroy(connector);
  930. }
  931. list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
  932. head) {
  933. drm_property_destroy(dev, property);
  934. }
  935. list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
  936. drm_framebuffer_remove(fb);
  937. }
  938. list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
  939. head) {
  940. plane->funcs->destroy(plane);
  941. }
  942. list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
  943. crtc->funcs->destroy(crtc);
  944. }
  945. idr_remove_all(&dev->mode_config.crtc_idr);
  946. idr_destroy(&dev->mode_config.crtc_idr);
  947. }
  948. EXPORT_SYMBOL(drm_mode_config_cleanup);
  949. /**
  950. * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
  951. * @out: drm_mode_modeinfo struct to return to the user
  952. * @in: drm_display_mode to use
  953. *
  954. * LOCKING:
  955. * None.
  956. *
  957. * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
  958. * the user.
  959. */
  960. static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
  961. const struct drm_display_mode *in)
  962. {
  963. WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
  964. in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
  965. in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
  966. in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
  967. in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
  968. "timing values too large for mode info\n");
  969. out->clock = in->clock;
  970. out->hdisplay = in->hdisplay;
  971. out->hsync_start = in->hsync_start;
  972. out->hsync_end = in->hsync_end;
  973. out->htotal = in->htotal;
  974. out->hskew = in->hskew;
  975. out->vdisplay = in->vdisplay;
  976. out->vsync_start = in->vsync_start;
  977. out->vsync_end = in->vsync_end;
  978. out->vtotal = in->vtotal;
  979. out->vscan = in->vscan;
  980. out->vrefresh = in->vrefresh;
  981. out->flags = in->flags;
  982. out->type = in->type;
  983. strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
  984. out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
  985. }
  986. /**
  987. * drm_crtc_convert_to_umode - convert a modeinfo into a drm_display_mode
  988. * @out: drm_display_mode to return to the user
  989. * @in: drm_mode_modeinfo to use
  990. *
  991. * LOCKING:
  992. * None.
  993. *
  994. * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
  995. * the caller.
  996. *
  997. * RETURNS:
  998. * Zero on success, errno on failure.
  999. */
  1000. static int drm_crtc_convert_umode(struct drm_display_mode *out,
  1001. const struct drm_mode_modeinfo *in)
  1002. {
  1003. if (in->clock > INT_MAX || in->vrefresh > INT_MAX)
  1004. return -ERANGE;
  1005. out->clock = in->clock;
  1006. out->hdisplay = in->hdisplay;
  1007. out->hsync_start = in->hsync_start;
  1008. out->hsync_end = in->hsync_end;
  1009. out->htotal = in->htotal;
  1010. out->hskew = in->hskew;
  1011. out->vdisplay = in->vdisplay;
  1012. out->vsync_start = in->vsync_start;
  1013. out->vsync_end = in->vsync_end;
  1014. out->vtotal = in->vtotal;
  1015. out->vscan = in->vscan;
  1016. out->vrefresh = in->vrefresh;
  1017. out->flags = in->flags;
  1018. out->type = in->type;
  1019. strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
  1020. out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
  1021. return 0;
  1022. }
  1023. /**
  1024. * drm_mode_getresources - get graphics configuration
  1025. * @inode: inode from the ioctl
  1026. * @filp: file * from the ioctl
  1027. * @cmd: cmd from ioctl
  1028. * @arg: arg from ioctl
  1029. *
  1030. * LOCKING:
  1031. * Takes mode config lock.
  1032. *
  1033. * Construct a set of configuration description structures and return
  1034. * them to the user, including CRTC, connector and framebuffer configuration.
  1035. *
  1036. * Called by the user via ioctl.
  1037. *
  1038. * RETURNS:
  1039. * Zero on success, errno on failure.
  1040. */
  1041. int drm_mode_getresources(struct drm_device *dev, void *data,
  1042. struct drm_file *file_priv)
  1043. {
  1044. struct drm_mode_card_res *card_res = data;
  1045. struct list_head *lh;
  1046. struct drm_framebuffer *fb;
  1047. struct drm_connector *connector;
  1048. struct drm_crtc *crtc;
  1049. struct drm_encoder *encoder;
  1050. int ret = 0;
  1051. int connector_count = 0;
  1052. int crtc_count = 0;
  1053. int fb_count = 0;
  1054. int encoder_count = 0;
  1055. int copied = 0, i;
  1056. uint32_t __user *fb_id;
  1057. uint32_t __user *crtc_id;
  1058. uint32_t __user *connector_id;
  1059. uint32_t __user *encoder_id;
  1060. struct drm_mode_group *mode_group;
  1061. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1062. return -EINVAL;
  1063. mutex_lock(&dev->mode_config.mutex);
  1064. /*
  1065. * For the non-control nodes we need to limit the list of resources
  1066. * by IDs in the group list for this node
  1067. */
  1068. list_for_each(lh, &file_priv->fbs)
  1069. fb_count++;
  1070. mode_group = &file_priv->master->minor->mode_group;
  1071. if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
  1072. list_for_each(lh, &dev->mode_config.crtc_list)
  1073. crtc_count++;
  1074. list_for_each(lh, &dev->mode_config.connector_list)
  1075. connector_count++;
  1076. list_for_each(lh, &dev->mode_config.encoder_list)
  1077. encoder_count++;
  1078. } else {
  1079. crtc_count = mode_group->num_crtcs;
  1080. connector_count = mode_group->num_connectors;
  1081. encoder_count = mode_group->num_encoders;
  1082. }
  1083. card_res->max_height = dev->mode_config.max_height;
  1084. card_res->min_height = dev->mode_config.min_height;
  1085. card_res->max_width = dev->mode_config.max_width;
  1086. card_res->min_width = dev->mode_config.min_width;
  1087. /* handle this in 4 parts */
  1088. /* FBs */
  1089. if (card_res->count_fbs >= fb_count) {
  1090. copied = 0;
  1091. fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
  1092. list_for_each_entry(fb, &file_priv->fbs, filp_head) {
  1093. if (put_user(fb->base.id, fb_id + copied)) {
  1094. ret = -EFAULT;
  1095. goto out;
  1096. }
  1097. copied++;
  1098. }
  1099. }
  1100. card_res->count_fbs = fb_count;
  1101. /* CRTCs */
  1102. if (card_res->count_crtcs >= crtc_count) {
  1103. copied = 0;
  1104. crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
  1105. if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
  1106. list_for_each_entry(crtc, &dev->mode_config.crtc_list,
  1107. head) {
  1108. DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
  1109. if (put_user(crtc->base.id, crtc_id + copied)) {
  1110. ret = -EFAULT;
  1111. goto out;
  1112. }
  1113. copied++;
  1114. }
  1115. } else {
  1116. for (i = 0; i < mode_group->num_crtcs; i++) {
  1117. if (put_user(mode_group->id_list[i],
  1118. crtc_id + copied)) {
  1119. ret = -EFAULT;
  1120. goto out;
  1121. }
  1122. copied++;
  1123. }
  1124. }
  1125. }
  1126. card_res->count_crtcs = crtc_count;
  1127. /* Encoders */
  1128. if (card_res->count_encoders >= encoder_count) {
  1129. copied = 0;
  1130. encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
  1131. if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
  1132. list_for_each_entry(encoder,
  1133. &dev->mode_config.encoder_list,
  1134. head) {
  1135. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
  1136. drm_get_encoder_name(encoder));
  1137. if (put_user(encoder->base.id, encoder_id +
  1138. copied)) {
  1139. ret = -EFAULT;
  1140. goto out;
  1141. }
  1142. copied++;
  1143. }
  1144. } else {
  1145. for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
  1146. if (put_user(mode_group->id_list[i],
  1147. encoder_id + copied)) {
  1148. ret = -EFAULT;
  1149. goto out;
  1150. }
  1151. copied++;
  1152. }
  1153. }
  1154. }
  1155. card_res->count_encoders = encoder_count;
  1156. /* Connectors */
  1157. if (card_res->count_connectors >= connector_count) {
  1158. copied = 0;
  1159. connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
  1160. if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
  1161. list_for_each_entry(connector,
  1162. &dev->mode_config.connector_list,
  1163. head) {
  1164. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  1165. connector->base.id,
  1166. drm_get_connector_name(connector));
  1167. if (put_user(connector->base.id,
  1168. connector_id + copied)) {
  1169. ret = -EFAULT;
  1170. goto out;
  1171. }
  1172. copied++;
  1173. }
  1174. } else {
  1175. int start = mode_group->num_crtcs +
  1176. mode_group->num_encoders;
  1177. for (i = start; i < start + mode_group->num_connectors; i++) {
  1178. if (put_user(mode_group->id_list[i],
  1179. connector_id + copied)) {
  1180. ret = -EFAULT;
  1181. goto out;
  1182. }
  1183. copied++;
  1184. }
  1185. }
  1186. }
  1187. card_res->count_connectors = connector_count;
  1188. DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
  1189. card_res->count_connectors, card_res->count_encoders);
  1190. out:
  1191. mutex_unlock(&dev->mode_config.mutex);
  1192. return ret;
  1193. }
  1194. /**
  1195. * drm_mode_getcrtc - get CRTC configuration
  1196. * @inode: inode from the ioctl
  1197. * @filp: file * from the ioctl
  1198. * @cmd: cmd from ioctl
  1199. * @arg: arg from ioctl
  1200. *
  1201. * LOCKING:
  1202. * Takes mode config lock.
  1203. *
  1204. * Construct a CRTC configuration structure to return to the user.
  1205. *
  1206. * Called by the user via ioctl.
  1207. *
  1208. * RETURNS:
  1209. * Zero on success, errno on failure.
  1210. */
  1211. int drm_mode_getcrtc(struct drm_device *dev,
  1212. void *data, struct drm_file *file_priv)
  1213. {
  1214. struct drm_mode_crtc *crtc_resp = data;
  1215. struct drm_crtc *crtc;
  1216. struct drm_mode_object *obj;
  1217. int ret = 0;
  1218. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1219. return -EINVAL;
  1220. mutex_lock(&dev->mode_config.mutex);
  1221. obj = drm_mode_object_find(dev, crtc_resp->crtc_id,
  1222. DRM_MODE_OBJECT_CRTC);
  1223. if (!obj) {
  1224. ret = -EINVAL;
  1225. goto out;
  1226. }
  1227. crtc = obj_to_crtc(obj);
  1228. crtc_resp->x = crtc->x;
  1229. crtc_resp->y = crtc->y;
  1230. crtc_resp->gamma_size = crtc->gamma_size;
  1231. if (crtc->fb)
  1232. crtc_resp->fb_id = crtc->fb->base.id;
  1233. else
  1234. crtc_resp->fb_id = 0;
  1235. if (crtc->enabled) {
  1236. drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
  1237. crtc_resp->mode_valid = 1;
  1238. } else {
  1239. crtc_resp->mode_valid = 0;
  1240. }
  1241. out:
  1242. mutex_unlock(&dev->mode_config.mutex);
  1243. return ret;
  1244. }
  1245. /**
  1246. * drm_mode_getconnector - get connector configuration
  1247. * @inode: inode from the ioctl
  1248. * @filp: file * from the ioctl
  1249. * @cmd: cmd from ioctl
  1250. * @arg: arg from ioctl
  1251. *
  1252. * LOCKING:
  1253. * Takes mode config lock.
  1254. *
  1255. * Construct a connector configuration structure to return to the user.
  1256. *
  1257. * Called by the user via ioctl.
  1258. *
  1259. * RETURNS:
  1260. * Zero on success, errno on failure.
  1261. */
  1262. int drm_mode_getconnector(struct drm_device *dev, void *data,
  1263. struct drm_file *file_priv)
  1264. {
  1265. struct drm_mode_get_connector *out_resp = data;
  1266. struct drm_mode_object *obj;
  1267. struct drm_connector *connector;
  1268. struct drm_display_mode *mode;
  1269. int mode_count = 0;
  1270. int props_count = 0;
  1271. int encoders_count = 0;
  1272. int ret = 0;
  1273. int copied = 0;
  1274. int i;
  1275. struct drm_mode_modeinfo u_mode;
  1276. struct drm_mode_modeinfo __user *mode_ptr;
  1277. uint32_t __user *prop_ptr;
  1278. uint64_t __user *prop_values;
  1279. uint32_t __user *encoder_ptr;
  1280. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1281. return -EINVAL;
  1282. memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
  1283. DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
  1284. mutex_lock(&dev->mode_config.mutex);
  1285. obj = drm_mode_object_find(dev, out_resp->connector_id,
  1286. DRM_MODE_OBJECT_CONNECTOR);
  1287. if (!obj) {
  1288. ret = -EINVAL;
  1289. goto out;
  1290. }
  1291. connector = obj_to_connector(obj);
  1292. props_count = connector->properties.count;
  1293. for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
  1294. if (connector->encoder_ids[i] != 0) {
  1295. encoders_count++;
  1296. }
  1297. }
  1298. if (out_resp->count_modes == 0) {
  1299. connector->funcs->fill_modes(connector,
  1300. dev->mode_config.max_width,
  1301. dev->mode_config.max_height);
  1302. }
  1303. /* delayed so we get modes regardless of pre-fill_modes state */
  1304. list_for_each_entry(mode, &connector->modes, head)
  1305. mode_count++;
  1306. out_resp->connector_id = connector->base.id;
  1307. out_resp->connector_type = connector->connector_type;
  1308. out_resp->connector_type_id = connector->connector_type_id;
  1309. out_resp->mm_width = connector->display_info.width_mm;
  1310. out_resp->mm_height = connector->display_info.height_mm;
  1311. out_resp->subpixel = connector->display_info.subpixel_order;
  1312. out_resp->connection = connector->status;
  1313. if (connector->encoder)
  1314. out_resp->encoder_id = connector->encoder->base.id;
  1315. else
  1316. out_resp->encoder_id = 0;
  1317. /*
  1318. * This ioctl is called twice, once to determine how much space is
  1319. * needed, and the 2nd time to fill it.
  1320. */
  1321. if ((out_resp->count_modes >= mode_count) && mode_count) {
  1322. copied = 0;
  1323. mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
  1324. list_for_each_entry(mode, &connector->modes, head) {
  1325. drm_crtc_convert_to_umode(&u_mode, mode);
  1326. if (copy_to_user(mode_ptr + copied,
  1327. &u_mode, sizeof(u_mode))) {
  1328. ret = -EFAULT;
  1329. goto out;
  1330. }
  1331. copied++;
  1332. }
  1333. }
  1334. out_resp->count_modes = mode_count;
  1335. if ((out_resp->count_props >= props_count) && props_count) {
  1336. copied = 0;
  1337. prop_ptr = (uint32_t __user *)(unsigned long)(out_resp->props_ptr);
  1338. prop_values = (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr);
  1339. for (i = 0; i < connector->properties.count; i++) {
  1340. if (put_user(connector->properties.ids[i],
  1341. prop_ptr + copied)) {
  1342. ret = -EFAULT;
  1343. goto out;
  1344. }
  1345. if (put_user(connector->properties.values[i],
  1346. prop_values + copied)) {
  1347. ret = -EFAULT;
  1348. goto out;
  1349. }
  1350. copied++;
  1351. }
  1352. }
  1353. out_resp->count_props = props_count;
  1354. if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
  1355. copied = 0;
  1356. encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
  1357. for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
  1358. if (connector->encoder_ids[i] != 0) {
  1359. if (put_user(connector->encoder_ids[i],
  1360. encoder_ptr + copied)) {
  1361. ret = -EFAULT;
  1362. goto out;
  1363. }
  1364. copied++;
  1365. }
  1366. }
  1367. }
  1368. out_resp->count_encoders = encoders_count;
  1369. out:
  1370. mutex_unlock(&dev->mode_config.mutex);
  1371. return ret;
  1372. }
  1373. int drm_mode_getencoder(struct drm_device *dev, void *data,
  1374. struct drm_file *file_priv)
  1375. {
  1376. struct drm_mode_get_encoder *enc_resp = data;
  1377. struct drm_mode_object *obj;
  1378. struct drm_encoder *encoder;
  1379. int ret = 0;
  1380. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1381. return -EINVAL;
  1382. mutex_lock(&dev->mode_config.mutex);
  1383. obj = drm_mode_object_find(dev, enc_resp->encoder_id,
  1384. DRM_MODE_OBJECT_ENCODER);
  1385. if (!obj) {
  1386. ret = -EINVAL;
  1387. goto out;
  1388. }
  1389. encoder = obj_to_encoder(obj);
  1390. if (encoder->crtc)
  1391. enc_resp->crtc_id = encoder->crtc->base.id;
  1392. else
  1393. enc_resp->crtc_id = 0;
  1394. enc_resp->encoder_type = encoder->encoder_type;
  1395. enc_resp->encoder_id = encoder->base.id;
  1396. enc_resp->possible_crtcs = encoder->possible_crtcs;
  1397. enc_resp->possible_clones = encoder->possible_clones;
  1398. out:
  1399. mutex_unlock(&dev->mode_config.mutex);
  1400. return ret;
  1401. }
  1402. /**
  1403. * drm_mode_getplane_res - get plane info
  1404. * @dev: DRM device
  1405. * @data: ioctl data
  1406. * @file_priv: DRM file info
  1407. *
  1408. * LOCKING:
  1409. * Takes mode config lock.
  1410. *
  1411. * Return an plane count and set of IDs.
  1412. */
  1413. int drm_mode_getplane_res(struct drm_device *dev, void *data,
  1414. struct drm_file *file_priv)
  1415. {
  1416. struct drm_mode_get_plane_res *plane_resp = data;
  1417. struct drm_mode_config *config;
  1418. struct drm_plane *plane;
  1419. uint32_t __user *plane_ptr;
  1420. int copied = 0, ret = 0;
  1421. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1422. return -EINVAL;
  1423. mutex_lock(&dev->mode_config.mutex);
  1424. config = &dev->mode_config;
  1425. /*
  1426. * This ioctl is called twice, once to determine how much space is
  1427. * needed, and the 2nd time to fill it.
  1428. */
  1429. if (config->num_plane &&
  1430. (plane_resp->count_planes >= config->num_plane)) {
  1431. plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
  1432. list_for_each_entry(plane, &config->plane_list, head) {
  1433. if (put_user(plane->base.id, plane_ptr + copied)) {
  1434. ret = -EFAULT;
  1435. goto out;
  1436. }
  1437. copied++;
  1438. }
  1439. }
  1440. plane_resp->count_planes = config->num_plane;
  1441. out:
  1442. mutex_unlock(&dev->mode_config.mutex);
  1443. return ret;
  1444. }
  1445. /**
  1446. * drm_mode_getplane - get plane info
  1447. * @dev: DRM device
  1448. * @data: ioctl data
  1449. * @file_priv: DRM file info
  1450. *
  1451. * LOCKING:
  1452. * Takes mode config lock.
  1453. *
  1454. * Return plane info, including formats supported, gamma size, any
  1455. * current fb, etc.
  1456. */
  1457. int drm_mode_getplane(struct drm_device *dev, void *data,
  1458. struct drm_file *file_priv)
  1459. {
  1460. struct drm_mode_get_plane *plane_resp = data;
  1461. struct drm_mode_object *obj;
  1462. struct drm_plane *plane;
  1463. uint32_t __user *format_ptr;
  1464. int ret = 0;
  1465. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1466. return -EINVAL;
  1467. mutex_lock(&dev->mode_config.mutex);
  1468. obj = drm_mode_object_find(dev, plane_resp->plane_id,
  1469. DRM_MODE_OBJECT_PLANE);
  1470. if (!obj) {
  1471. ret = -ENOENT;
  1472. goto out;
  1473. }
  1474. plane = obj_to_plane(obj);
  1475. if (plane->crtc)
  1476. plane_resp->crtc_id = plane->crtc->base.id;
  1477. else
  1478. plane_resp->crtc_id = 0;
  1479. if (plane->fb)
  1480. plane_resp->fb_id = plane->fb->base.id;
  1481. else
  1482. plane_resp->fb_id = 0;
  1483. plane_resp->plane_id = plane->base.id;
  1484. plane_resp->possible_crtcs = plane->possible_crtcs;
  1485. plane_resp->gamma_size = plane->gamma_size;
  1486. /*
  1487. * This ioctl is called twice, once to determine how much space is
  1488. * needed, and the 2nd time to fill it.
  1489. */
  1490. if (plane->format_count &&
  1491. (plane_resp->count_format_types >= plane->format_count)) {
  1492. format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
  1493. if (copy_to_user(format_ptr,
  1494. plane->format_types,
  1495. sizeof(uint32_t) * plane->format_count)) {
  1496. ret = -EFAULT;
  1497. goto out;
  1498. }
  1499. }
  1500. plane_resp->count_format_types = plane->format_count;
  1501. out:
  1502. mutex_unlock(&dev->mode_config.mutex);
  1503. return ret;
  1504. }
  1505. /**
  1506. * drm_mode_setplane - set up or tear down an plane
  1507. * @dev: DRM device
  1508. * @data: ioctl data*
  1509. * @file_prive: DRM file info
  1510. *
  1511. * LOCKING:
  1512. * Takes mode config lock.
  1513. *
  1514. * Set plane info, including placement, fb, scaling, and other factors.
  1515. * Or pass a NULL fb to disable.
  1516. */
  1517. int drm_mode_setplane(struct drm_device *dev, void *data,
  1518. struct drm_file *file_priv)
  1519. {
  1520. struct drm_mode_set_plane *plane_req = data;
  1521. struct drm_mode_object *obj;
  1522. struct drm_plane *plane;
  1523. struct drm_crtc *crtc;
  1524. struct drm_framebuffer *fb;
  1525. int ret = 0;
  1526. unsigned int fb_width, fb_height;
  1527. int i;
  1528. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1529. return -EINVAL;
  1530. mutex_lock(&dev->mode_config.mutex);
  1531. /*
  1532. * First, find the plane, crtc, and fb objects. If not available,
  1533. * we don't bother to call the driver.
  1534. */
  1535. obj = drm_mode_object_find(dev, plane_req->plane_id,
  1536. DRM_MODE_OBJECT_PLANE);
  1537. if (!obj) {
  1538. DRM_DEBUG_KMS("Unknown plane ID %d\n",
  1539. plane_req->plane_id);
  1540. ret = -ENOENT;
  1541. goto out;
  1542. }
  1543. plane = obj_to_plane(obj);
  1544. /* No fb means shut it down */
  1545. if (!plane_req->fb_id) {
  1546. plane->funcs->disable_plane(plane);
  1547. plane->crtc = NULL;
  1548. plane->fb = NULL;
  1549. goto out;
  1550. }
  1551. obj = drm_mode_object_find(dev, plane_req->crtc_id,
  1552. DRM_MODE_OBJECT_CRTC);
  1553. if (!obj) {
  1554. DRM_DEBUG_KMS("Unknown crtc ID %d\n",
  1555. plane_req->crtc_id);
  1556. ret = -ENOENT;
  1557. goto out;
  1558. }
  1559. crtc = obj_to_crtc(obj);
  1560. obj = drm_mode_object_find(dev, plane_req->fb_id,
  1561. DRM_MODE_OBJECT_FB);
  1562. if (!obj) {
  1563. DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
  1564. plane_req->fb_id);
  1565. ret = -ENOENT;
  1566. goto out;
  1567. }
  1568. fb = obj_to_fb(obj);
  1569. /* Check whether this plane supports the fb pixel format. */
  1570. for (i = 0; i < plane->format_count; i++)
  1571. if (fb->pixel_format == plane->format_types[i])
  1572. break;
  1573. if (i == plane->format_count) {
  1574. DRM_DEBUG_KMS("Invalid pixel format 0x%08x\n", fb->pixel_format);
  1575. ret = -EINVAL;
  1576. goto out;
  1577. }
  1578. fb_width = fb->width << 16;
  1579. fb_height = fb->height << 16;
  1580. /* Make sure source coordinates are inside the fb. */
  1581. if (plane_req->src_w > fb_width ||
  1582. plane_req->src_x > fb_width - plane_req->src_w ||
  1583. plane_req->src_h > fb_height ||
  1584. plane_req->src_y > fb_height - plane_req->src_h) {
  1585. DRM_DEBUG_KMS("Invalid source coordinates "
  1586. "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
  1587. plane_req->src_w >> 16,
  1588. ((plane_req->src_w & 0xffff) * 15625) >> 10,
  1589. plane_req->src_h >> 16,
  1590. ((plane_req->src_h & 0xffff) * 15625) >> 10,
  1591. plane_req->src_x >> 16,
  1592. ((plane_req->src_x & 0xffff) * 15625) >> 10,
  1593. plane_req->src_y >> 16,
  1594. ((plane_req->src_y & 0xffff) * 15625) >> 10);
  1595. ret = -ENOSPC;
  1596. goto out;
  1597. }
  1598. /* Give drivers some help against integer overflows */
  1599. if (plane_req->crtc_w > INT_MAX ||
  1600. plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w ||
  1601. plane_req->crtc_h > INT_MAX ||
  1602. plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) {
  1603. DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
  1604. plane_req->crtc_w, plane_req->crtc_h,
  1605. plane_req->crtc_x, plane_req->crtc_y);
  1606. ret = -ERANGE;
  1607. goto out;
  1608. }
  1609. ret = plane->funcs->update_plane(plane, crtc, fb,
  1610. plane_req->crtc_x, plane_req->crtc_y,
  1611. plane_req->crtc_w, plane_req->crtc_h,
  1612. plane_req->src_x, plane_req->src_y,
  1613. plane_req->src_w, plane_req->src_h);
  1614. if (!ret) {
  1615. plane->crtc = crtc;
  1616. plane->fb = fb;
  1617. }
  1618. out:
  1619. mutex_unlock(&dev->mode_config.mutex);
  1620. return ret;
  1621. }
  1622. /**
  1623. * drm_mode_setcrtc - set CRTC configuration
  1624. * @inode: inode from the ioctl
  1625. * @filp: file * from the ioctl
  1626. * @cmd: cmd from ioctl
  1627. * @arg: arg from ioctl
  1628. *
  1629. * LOCKING:
  1630. * Takes mode config lock.
  1631. *
  1632. * Build a new CRTC configuration based on user request.
  1633. *
  1634. * Called by the user via ioctl.
  1635. *
  1636. * RETURNS:
  1637. * Zero on success, errno on failure.
  1638. */
  1639. int drm_mode_setcrtc(struct drm_device *dev, void *data,
  1640. struct drm_file *file_priv)
  1641. {
  1642. struct drm_mode_config *config = &dev->mode_config;
  1643. struct drm_mode_crtc *crtc_req = data;
  1644. struct drm_mode_object *obj;
  1645. struct drm_crtc *crtc;
  1646. struct drm_connector **connector_set = NULL, *connector;
  1647. struct drm_framebuffer *fb = NULL;
  1648. struct drm_display_mode *mode = NULL;
  1649. struct drm_mode_set set;
  1650. uint32_t __user *set_connectors_ptr;
  1651. int ret;
  1652. int i;
  1653. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1654. return -EINVAL;
  1655. /* For some reason crtc x/y offsets are signed internally. */
  1656. if (crtc_req->x > INT_MAX || crtc_req->y > INT_MAX)
  1657. return -ERANGE;
  1658. mutex_lock(&dev->mode_config.mutex);
  1659. obj = drm_mode_object_find(dev, crtc_req->crtc_id,
  1660. DRM_MODE_OBJECT_CRTC);
  1661. if (!obj) {
  1662. DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
  1663. ret = -EINVAL;
  1664. goto out;
  1665. }
  1666. crtc = obj_to_crtc(obj);
  1667. DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
  1668. if (crtc_req->mode_valid) {
  1669. int hdisplay, vdisplay;
  1670. /* If we have a mode we need a framebuffer. */
  1671. /* If we pass -1, set the mode with the currently bound fb */
  1672. if (crtc_req->fb_id == -1) {
  1673. if (!crtc->fb) {
  1674. DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
  1675. ret = -EINVAL;
  1676. goto out;
  1677. }
  1678. fb = crtc->fb;
  1679. } else {
  1680. obj = drm_mode_object_find(dev, crtc_req->fb_id,
  1681. DRM_MODE_OBJECT_FB);
  1682. if (!obj) {
  1683. DRM_DEBUG_KMS("Unknown FB ID%d\n",
  1684. crtc_req->fb_id);
  1685. ret = -EINVAL;
  1686. goto out;
  1687. }
  1688. fb = obj_to_fb(obj);
  1689. }
  1690. mode = drm_mode_create(dev);
  1691. if (!mode) {
  1692. ret = -ENOMEM;
  1693. goto out;
  1694. }
  1695. ret = drm_crtc_convert_umode(mode, &crtc_req->mode);
  1696. if (ret) {
  1697. DRM_DEBUG_KMS("Invalid mode\n");
  1698. goto out;
  1699. }
  1700. drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
  1701. hdisplay = mode->hdisplay;
  1702. vdisplay = mode->vdisplay;
  1703. if (crtc->invert_dimensions)
  1704. swap(hdisplay, vdisplay);
  1705. if (hdisplay > fb->width ||
  1706. vdisplay > fb->height ||
  1707. crtc_req->x > fb->width - hdisplay ||
  1708. crtc_req->y > fb->height - vdisplay) {
  1709. DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
  1710. fb->width, fb->height,
  1711. hdisplay, vdisplay, crtc_req->x, crtc_req->y,
  1712. crtc->invert_dimensions ? " (inverted)" : "");
  1713. ret = -ENOSPC;
  1714. goto out;
  1715. }
  1716. }
  1717. if (crtc_req->count_connectors == 0 && mode) {
  1718. DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
  1719. ret = -EINVAL;
  1720. goto out;
  1721. }
  1722. if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
  1723. DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
  1724. crtc_req->count_connectors);
  1725. ret = -EINVAL;
  1726. goto out;
  1727. }
  1728. if (crtc_req->count_connectors > 0) {
  1729. u32 out_id;
  1730. /* Avoid unbounded kernel memory allocation */
  1731. if (crtc_req->count_connectors > config->num_connector) {
  1732. ret = -EINVAL;
  1733. goto out;
  1734. }
  1735. connector_set = kmalloc(crtc_req->count_connectors *
  1736. sizeof(struct drm_connector *),
  1737. GFP_KERNEL);
  1738. if (!connector_set) {
  1739. ret = -ENOMEM;
  1740. goto out;
  1741. }
  1742. for (i = 0; i < crtc_req->count_connectors; i++) {
  1743. set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
  1744. if (get_user(out_id, &set_connectors_ptr[i])) {
  1745. ret = -EFAULT;
  1746. goto out;
  1747. }
  1748. obj = drm_mode_object_find(dev, out_id,
  1749. DRM_MODE_OBJECT_CONNECTOR);
  1750. if (!obj) {
  1751. DRM_DEBUG_KMS("Connector id %d unknown\n",
  1752. out_id);
  1753. ret = -EINVAL;
  1754. goto out;
  1755. }
  1756. connector = obj_to_connector(obj);
  1757. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  1758. connector->base.id,
  1759. drm_get_connector_name(connector));
  1760. connector_set[i] = connector;
  1761. }
  1762. }
  1763. set.crtc = crtc;
  1764. set.x = crtc_req->x;
  1765. set.y = crtc_req->y;
  1766. set.mode = mode;
  1767. set.connectors = connector_set;
  1768. set.num_connectors = crtc_req->count_connectors;
  1769. set.fb = fb;
  1770. ret = crtc->funcs->set_config(&set);
  1771. out:
  1772. kfree(connector_set);
  1773. drm_mode_destroy(dev, mode);
  1774. mutex_unlock(&dev->mode_config.mutex);
  1775. return ret;
  1776. }
  1777. int drm_mode_cursor_ioctl(struct drm_device *dev,
  1778. void *data, struct drm_file *file_priv)
  1779. {
  1780. struct drm_mode_cursor *req = data;
  1781. struct drm_mode_object *obj;
  1782. struct drm_crtc *crtc;
  1783. int ret = 0;
  1784. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1785. return -EINVAL;
  1786. if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
  1787. return -EINVAL;
  1788. mutex_lock(&dev->mode_config.mutex);
  1789. obj = drm_mode_object_find(dev, req->crtc_id, DRM_MODE_OBJECT_CRTC);
  1790. if (!obj) {
  1791. DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
  1792. ret = -EINVAL;
  1793. goto out;
  1794. }
  1795. crtc = obj_to_crtc(obj);
  1796. if (req->flags & DRM_MODE_CURSOR_BO) {
  1797. if (!crtc->funcs->cursor_set) {
  1798. ret = -ENXIO;
  1799. goto out;
  1800. }
  1801. /* Turns off the cursor if handle is 0 */
  1802. ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
  1803. req->width, req->height);
  1804. }
  1805. if (req->flags & DRM_MODE_CURSOR_MOVE) {
  1806. if (crtc->funcs->cursor_move) {
  1807. ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
  1808. } else {
  1809. ret = -EFAULT;
  1810. goto out;
  1811. }
  1812. }
  1813. out:
  1814. mutex_unlock(&dev->mode_config.mutex);
  1815. return ret;
  1816. }
  1817. /* Original addfb only supported RGB formats, so figure out which one */
  1818. uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
  1819. {
  1820. uint32_t fmt;
  1821. switch (bpp) {
  1822. case 8:
  1823. fmt = DRM_FORMAT_RGB332;
  1824. break;
  1825. case 16:
  1826. if (depth == 15)
  1827. fmt = DRM_FORMAT_XRGB1555;
  1828. else
  1829. fmt = DRM_FORMAT_RGB565;
  1830. break;
  1831. case 24:
  1832. fmt = DRM_FORMAT_RGB888;
  1833. break;
  1834. case 32:
  1835. if (depth == 24)
  1836. fmt = DRM_FORMAT_XRGB8888;
  1837. else if (depth == 30)
  1838. fmt = DRM_FORMAT_XRGB2101010;
  1839. else
  1840. fmt = DRM_FORMAT_ARGB8888;
  1841. break;
  1842. default:
  1843. DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
  1844. fmt = DRM_FORMAT_XRGB8888;
  1845. break;
  1846. }
  1847. return fmt;
  1848. }
  1849. EXPORT_SYMBOL(drm_mode_legacy_fb_format);
  1850. /**
  1851. * drm_mode_addfb - add an FB to the graphics configuration
  1852. * @inode: inode from the ioctl
  1853. * @filp: file * from the ioctl
  1854. * @cmd: cmd from ioctl
  1855. * @arg: arg from ioctl
  1856. *
  1857. * LOCKING:
  1858. * Takes mode config lock.
  1859. *
  1860. * Add a new FB to the specified CRTC, given a user request.
  1861. *
  1862. * Called by the user via ioctl.
  1863. *
  1864. * RETURNS:
  1865. * Zero on success, errno on failure.
  1866. */
  1867. int drm_mode_addfb(struct drm_device *dev,
  1868. void *data, struct drm_file *file_priv)
  1869. {
  1870. struct drm_mode_fb_cmd *or = data;
  1871. struct drm_mode_fb_cmd2 r = {};
  1872. struct drm_mode_config *config = &dev->mode_config;
  1873. struct drm_framebuffer *fb;
  1874. int ret = 0;
  1875. /* Use new struct with format internally */
  1876. r.fb_id = or->fb_id;
  1877. r.width = or->width;
  1878. r.height = or->height;
  1879. r.pitches[0] = or->pitch;
  1880. r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
  1881. r.handles[0] = or->handle;
  1882. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1883. return -EINVAL;
  1884. if ((config->min_width > r.width) || (r.width > config->max_width))
  1885. return -EINVAL;
  1886. if ((config->min_height > r.height) || (r.height > config->max_height))
  1887. return -EINVAL;
  1888. mutex_lock(&dev->mode_config.mutex);
  1889. /* TODO check buffer is sufficiently large */
  1890. /* TODO setup destructor callback */
  1891. fb = dev->mode_config.funcs->fb_create(dev, file_priv, &r);
  1892. if (IS_ERR(fb)) {
  1893. DRM_DEBUG_KMS("could not create framebuffer\n");
  1894. ret = PTR_ERR(fb);
  1895. goto out;
  1896. }
  1897. or->fb_id = fb->base.id;
  1898. list_add(&fb->filp_head, &file_priv->fbs);
  1899. DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
  1900. out:
  1901. mutex_unlock(&dev->mode_config.mutex);
  1902. return ret;
  1903. }
  1904. static int format_check(const struct drm_mode_fb_cmd2 *r)
  1905. {
  1906. uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
  1907. switch (format) {
  1908. case DRM_FORMAT_C8:
  1909. case DRM_FORMAT_RGB332:
  1910. case DRM_FORMAT_BGR233:
  1911. case DRM_FORMAT_XRGB4444:
  1912. case DRM_FORMAT_XBGR4444:
  1913. case DRM_FORMAT_RGBX4444:
  1914. case DRM_FORMAT_BGRX4444:
  1915. case DRM_FORMAT_ARGB4444:
  1916. case DRM_FORMAT_ABGR4444:
  1917. case DRM_FORMAT_RGBA4444:
  1918. case DRM_FORMAT_BGRA4444:
  1919. case DRM_FORMAT_XRGB1555:
  1920. case DRM_FORMAT_XBGR1555:
  1921. case DRM_FORMAT_RGBX5551:
  1922. case DRM_FORMAT_BGRX5551:
  1923. case DRM_FORMAT_ARGB1555:
  1924. case DRM_FORMAT_ABGR1555:
  1925. case DRM_FORMAT_RGBA5551:
  1926. case DRM_FORMAT_BGRA5551:
  1927. case DRM_FORMAT_RGB565:
  1928. case DRM_FORMAT_BGR565:
  1929. case DRM_FORMAT_RGB888:
  1930. case DRM_FORMAT_BGR888:
  1931. case DRM_FORMAT_XRGB8888:
  1932. case DRM_FORMAT_XBGR8888:
  1933. case DRM_FORMAT_RGBX8888:
  1934. case DRM_FORMAT_BGRX8888:
  1935. case DRM_FORMAT_ARGB8888:
  1936. case DRM_FORMAT_ABGR8888:
  1937. case DRM_FORMAT_RGBA8888:
  1938. case DRM_FORMAT_BGRA8888:
  1939. case DRM_FORMAT_XRGB2101010:
  1940. case DRM_FORMAT_XBGR2101010:
  1941. case DRM_FORMAT_RGBX1010102:
  1942. case DRM_FORMAT_BGRX1010102:
  1943. case DRM_FORMAT_ARGB2101010:
  1944. case DRM_FORMAT_ABGR2101010:
  1945. case DRM_FORMAT_RGBA1010102:
  1946. case DRM_FORMAT_BGRA1010102:
  1947. case DRM_FORMAT_YUYV:
  1948. case DRM_FORMAT_YVYU:
  1949. case DRM_FORMAT_UYVY:
  1950. case DRM_FORMAT_VYUY:
  1951. case DRM_FORMAT_AYUV:
  1952. case DRM_FORMAT_NV12:
  1953. case DRM_FORMAT_NV21:
  1954. case DRM_FORMAT_NV16:
  1955. case DRM_FORMAT_NV61:
  1956. case DRM_FORMAT_NV24:
  1957. case DRM_FORMAT_NV42:
  1958. case DRM_FORMAT_YUV410:
  1959. case DRM_FORMAT_YVU410:
  1960. case DRM_FORMAT_YUV411:
  1961. case DRM_FORMAT_YVU411:
  1962. case DRM_FORMAT_YUV420:
  1963. case DRM_FORMAT_YVU420:
  1964. case DRM_FORMAT_YUV422:
  1965. case DRM_FORMAT_YVU422:
  1966. case DRM_FORMAT_YUV444:
  1967. case DRM_FORMAT_YVU444:
  1968. return 0;
  1969. default:
  1970. return -EINVAL;
  1971. }
  1972. }
  1973. static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
  1974. {
  1975. int ret, hsub, vsub, num_planes, i;
  1976. ret = format_check(r);
  1977. if (ret) {
  1978. DRM_DEBUG_KMS("bad framebuffer format 0x%08x\n", r->pixel_format);
  1979. return ret;
  1980. }
  1981. hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
  1982. vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
  1983. num_planes = drm_format_num_planes(r->pixel_format);
  1984. if (r->width == 0 || r->width % hsub) {
  1985. DRM_DEBUG_KMS("bad framebuffer width %u\n", r->height);
  1986. return -EINVAL;
  1987. }
  1988. if (r->height == 0 || r->height % vsub) {
  1989. DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
  1990. return -EINVAL;
  1991. }
  1992. for (i = 0; i < num_planes; i++) {
  1993. unsigned int width = r->width / (i != 0 ? hsub : 1);
  1994. unsigned int height = r->height / (i != 0 ? vsub : 1);
  1995. unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
  1996. if (!r->handles[i]) {
  1997. DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
  1998. return -EINVAL;
  1999. }
  2000. if ((uint64_t) width * cpp > UINT_MAX)
  2001. return -ERANGE;
  2002. if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
  2003. return -ERANGE;
  2004. if (r->pitches[i] < width * cpp) {
  2005. DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
  2006. return -EINVAL;
  2007. }
  2008. }
  2009. return 0;
  2010. }
  2011. /**
  2012. * drm_mode_addfb2 - add an FB to the graphics configuration
  2013. * @inode: inode from the ioctl
  2014. * @filp: file * from the ioctl
  2015. * @cmd: cmd from ioctl
  2016. * @arg: arg from ioctl
  2017. *
  2018. * LOCKING:
  2019. * Takes mode config lock.
  2020. *
  2021. * Add a new FB to the specified CRTC, given a user request with format.
  2022. *
  2023. * Called by the user via ioctl.
  2024. *
  2025. * RETURNS:
  2026. * Zero on success, errno on failure.
  2027. */
  2028. int drm_mode_addfb2(struct drm_device *dev,
  2029. void *data, struct drm_file *file_priv)
  2030. {
  2031. struct drm_mode_fb_cmd2 *r = data;
  2032. struct drm_mode_config *config = &dev->mode_config;
  2033. struct drm_framebuffer *fb;
  2034. int ret;
  2035. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2036. return -EINVAL;
  2037. if (r->flags & ~DRM_MODE_FB_INTERLACED) {
  2038. DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
  2039. return -EINVAL;
  2040. }
  2041. if ((config->min_width > r->width) || (r->width > config->max_width)) {
  2042. DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
  2043. r->width, config->min_width, config->max_width);
  2044. return -EINVAL;
  2045. }
  2046. if ((config->min_height > r->height) || (r->height > config->max_height)) {
  2047. DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
  2048. r->height, config->min_height, config->max_height);
  2049. return -EINVAL;
  2050. }
  2051. ret = framebuffer_check(r);
  2052. if (ret)
  2053. return ret;
  2054. mutex_lock(&dev->mode_config.mutex);
  2055. fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
  2056. if (IS_ERR(fb)) {
  2057. DRM_DEBUG_KMS("could not create framebuffer\n");
  2058. ret = PTR_ERR(fb);
  2059. goto out;
  2060. }
  2061. r->fb_id = fb->base.id;
  2062. list_add(&fb->filp_head, &file_priv->fbs);
  2063. DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
  2064. out:
  2065. mutex_unlock(&dev->mode_config.mutex);
  2066. return ret;
  2067. }
  2068. /**
  2069. * drm_mode_rmfb - remove an FB from the configuration
  2070. * @inode: inode from the ioctl
  2071. * @filp: file * from the ioctl
  2072. * @cmd: cmd from ioctl
  2073. * @arg: arg from ioctl
  2074. *
  2075. * LOCKING:
  2076. * Takes mode config lock.
  2077. *
  2078. * Remove the FB specified by the user.
  2079. *
  2080. * Called by the user via ioctl.
  2081. *
  2082. * RETURNS:
  2083. * Zero on success, errno on failure.
  2084. */
  2085. int drm_mode_rmfb(struct drm_device *dev,
  2086. void *data, struct drm_file *file_priv)
  2087. {
  2088. struct drm_mode_object *obj;
  2089. struct drm_framebuffer *fb = NULL;
  2090. struct drm_framebuffer *fbl = NULL;
  2091. uint32_t *id = data;
  2092. int ret = 0;
  2093. int found = 0;
  2094. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2095. return -EINVAL;
  2096. mutex_lock(&dev->mode_config.mutex);
  2097. obj = drm_mode_object_find(dev, *id, DRM_MODE_OBJECT_FB);
  2098. /* TODO check that we really get a framebuffer back. */
  2099. if (!obj) {
  2100. ret = -EINVAL;
  2101. goto out;
  2102. }
  2103. fb = obj_to_fb(obj);
  2104. list_for_each_entry(fbl, &file_priv->fbs, filp_head)
  2105. if (fb == fbl)
  2106. found = 1;
  2107. if (!found) {
  2108. ret = -EINVAL;
  2109. goto out;
  2110. }
  2111. drm_framebuffer_remove(fb);
  2112. out:
  2113. mutex_unlock(&dev->mode_config.mutex);
  2114. return ret;
  2115. }
  2116. /**
  2117. * drm_mode_getfb - get FB info
  2118. * @inode: inode from the ioctl
  2119. * @filp: file * from the ioctl
  2120. * @cmd: cmd from ioctl
  2121. * @arg: arg from ioctl
  2122. *
  2123. * LOCKING:
  2124. * Takes mode config lock.
  2125. *
  2126. * Lookup the FB given its ID and return info about it.
  2127. *
  2128. * Called by the user via ioctl.
  2129. *
  2130. * RETURNS:
  2131. * Zero on success, errno on failure.
  2132. */
  2133. int drm_mode_getfb(struct drm_device *dev,
  2134. void *data, struct drm_file *file_priv)
  2135. {
  2136. struct drm_mode_fb_cmd *r = data;
  2137. struct drm_mode_object *obj;
  2138. struct drm_framebuffer *fb;
  2139. int ret = 0;
  2140. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2141. return -EINVAL;
  2142. mutex_lock(&dev->mode_config.mutex);
  2143. obj = drm_mode_object_find(dev, r->fb_id, DRM_MODE_OBJECT_FB);
  2144. if (!obj) {
  2145. ret = -EINVAL;
  2146. goto out;
  2147. }
  2148. fb = obj_to_fb(obj);
  2149. r->height = fb->height;
  2150. r->width = fb->width;
  2151. r->depth = fb->depth;
  2152. r->bpp = fb->bits_per_pixel;
  2153. r->pitch = fb->pitches[0];
  2154. fb->funcs->create_handle(fb, file_priv, &r->handle);
  2155. out:
  2156. mutex_unlock(&dev->mode_config.mutex);
  2157. return ret;
  2158. }
  2159. int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
  2160. void *data, struct drm_file *file_priv)
  2161. {
  2162. struct drm_clip_rect __user *clips_ptr;
  2163. struct drm_clip_rect *clips = NULL;
  2164. struct drm_mode_fb_dirty_cmd *r = data;
  2165. struct drm_mode_object *obj;
  2166. struct drm_framebuffer *fb;
  2167. unsigned flags;
  2168. int num_clips;
  2169. int ret;
  2170. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2171. return -EINVAL;
  2172. mutex_lock(&dev->mode_config.mutex);
  2173. obj = drm_mode_object_find(dev, r->fb_id, DRM_MODE_OBJECT_FB);
  2174. if (!obj) {
  2175. ret = -EINVAL;
  2176. goto out_err1;
  2177. }
  2178. fb = obj_to_fb(obj);
  2179. num_clips = r->num_clips;
  2180. clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
  2181. if (!num_clips != !clips_ptr) {
  2182. ret = -EINVAL;
  2183. goto out_err1;
  2184. }
  2185. flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
  2186. /* If userspace annotates copy, clips must come in pairs */
  2187. if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
  2188. ret = -EINVAL;
  2189. goto out_err1;
  2190. }
  2191. if (num_clips && clips_ptr) {
  2192. if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
  2193. ret = -EINVAL;
  2194. goto out_err1;
  2195. }
  2196. clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL);
  2197. if (!clips) {
  2198. ret = -ENOMEM;
  2199. goto out_err1;
  2200. }
  2201. ret = copy_from_user(clips, clips_ptr,
  2202. num_clips * sizeof(*clips));
  2203. if (ret) {
  2204. ret = -EFAULT;
  2205. goto out_err2;
  2206. }
  2207. }
  2208. if (fb->funcs->dirty) {
  2209. ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
  2210. clips, num_clips);
  2211. } else {
  2212. ret = -ENOSYS;
  2213. goto out_err2;
  2214. }
  2215. out_err2:
  2216. kfree(clips);
  2217. out_err1:
  2218. mutex_unlock(&dev->mode_config.mutex);
  2219. return ret;
  2220. }
  2221. /**
  2222. * drm_fb_release - remove and free the FBs on this file
  2223. * @filp: file * from the ioctl
  2224. *
  2225. * LOCKING:
  2226. * Takes mode config lock.
  2227. *
  2228. * Destroy all the FBs associated with @filp.
  2229. *
  2230. * Called by the user via ioctl.
  2231. *
  2232. * RETURNS:
  2233. * Zero on success, errno on failure.
  2234. */
  2235. void drm_fb_release(struct drm_file *priv)
  2236. {
  2237. struct drm_device *dev = priv->minor->dev;
  2238. struct drm_framebuffer *fb, *tfb;
  2239. mutex_lock(&dev->mode_config.mutex);
  2240. list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
  2241. drm_framebuffer_remove(fb);
  2242. }
  2243. mutex_unlock(&dev->mode_config.mutex);
  2244. }
  2245. /**
  2246. * drm_mode_attachmode - add a mode to the user mode list
  2247. * @dev: DRM device
  2248. * @connector: connector to add the mode to
  2249. * @mode: mode to add
  2250. *
  2251. * Add @mode to @connector's user mode list.
  2252. */
  2253. static void drm_mode_attachmode(struct drm_device *dev,
  2254. struct drm_connector *connector,
  2255. struct drm_display_mode *mode)
  2256. {
  2257. list_add_tail(&mode->head, &connector->user_modes);
  2258. }
  2259. int drm_mode_attachmode_crtc(struct drm_device *dev, struct drm_crtc *crtc,
  2260. const struct drm_display_mode *mode)
  2261. {
  2262. struct drm_connector *connector;
  2263. int ret = 0;
  2264. struct drm_display_mode *dup_mode, *next;
  2265. LIST_HEAD(list);
  2266. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  2267. if (!connector->encoder)
  2268. continue;
  2269. if (connector->encoder->crtc == crtc) {
  2270. dup_mode = drm_mode_duplicate(dev, mode);
  2271. if (!dup_mode) {
  2272. ret = -ENOMEM;
  2273. goto out;
  2274. }
  2275. list_add_tail(&dup_mode->head, &list);
  2276. }
  2277. }
  2278. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  2279. if (!connector->encoder)
  2280. continue;
  2281. if (connector->encoder->crtc == crtc)
  2282. list_move_tail(list.next, &connector->user_modes);
  2283. }
  2284. WARN_ON(!list_empty(&list));
  2285. out:
  2286. list_for_each_entry_safe(dup_mode, next, &list, head)
  2287. drm_mode_destroy(dev, dup_mode);
  2288. return ret;
  2289. }
  2290. EXPORT_SYMBOL(drm_mode_attachmode_crtc);
  2291. static int drm_mode_detachmode(struct drm_device *dev,
  2292. struct drm_connector *connector,
  2293. struct drm_display_mode *mode)
  2294. {
  2295. int found = 0;
  2296. int ret = 0;
  2297. struct drm_display_mode *match_mode, *t;
  2298. list_for_each_entry_safe(match_mode, t, &connector->user_modes, head) {
  2299. if (drm_mode_equal(match_mode, mode)) {
  2300. list_del(&match_mode->head);
  2301. drm_mode_destroy(dev, match_mode);
  2302. found = 1;
  2303. break;
  2304. }
  2305. }
  2306. if (!found)
  2307. ret = -EINVAL;
  2308. return ret;
  2309. }
  2310. int drm_mode_detachmode_crtc(struct drm_device *dev, struct drm_display_mode *mode)
  2311. {
  2312. struct drm_connector *connector;
  2313. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  2314. drm_mode_detachmode(dev, connector, mode);
  2315. }
  2316. return 0;
  2317. }
  2318. EXPORT_SYMBOL(drm_mode_detachmode_crtc);
  2319. /**
  2320. * drm_fb_attachmode - Attach a user mode to an connector
  2321. * @inode: inode from the ioctl
  2322. * @filp: file * from the ioctl
  2323. * @cmd: cmd from ioctl
  2324. * @arg: arg from ioctl
  2325. *
  2326. * This attaches a user specified mode to an connector.
  2327. * Called by the user via ioctl.
  2328. *
  2329. * RETURNS:
  2330. * Zero on success, errno on failure.
  2331. */
  2332. int drm_mode_attachmode_ioctl(struct drm_device *dev,
  2333. void *data, struct drm_file *file_priv)
  2334. {
  2335. struct drm_mode_mode_cmd *mode_cmd = data;
  2336. struct drm_connector *connector;
  2337. struct drm_display_mode *mode;
  2338. struct drm_mode_object *obj;
  2339. struct drm_mode_modeinfo *umode = &mode_cmd->mode;
  2340. int ret;
  2341. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2342. return -EINVAL;
  2343. mutex_lock(&dev->mode_config.mutex);
  2344. obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
  2345. if (!obj) {
  2346. ret = -EINVAL;
  2347. goto out;
  2348. }
  2349. connector = obj_to_connector(obj);
  2350. mode = drm_mode_create(dev);
  2351. if (!mode) {
  2352. ret = -ENOMEM;
  2353. goto out;
  2354. }
  2355. ret = drm_crtc_convert_umode(mode, umode);
  2356. if (ret) {
  2357. DRM_DEBUG_KMS("Invalid mode\n");
  2358. drm_mode_destroy(dev, mode);
  2359. goto out;
  2360. }
  2361. drm_mode_attachmode(dev, connector, mode);
  2362. out:
  2363. mutex_unlock(&dev->mode_config.mutex);
  2364. return ret;
  2365. }
  2366. /**
  2367. * drm_fb_detachmode - Detach a user specified mode from an connector
  2368. * @inode: inode from the ioctl
  2369. * @filp: file * from the ioctl
  2370. * @cmd: cmd from ioctl
  2371. * @arg: arg from ioctl
  2372. *
  2373. * Called by the user via ioctl.
  2374. *
  2375. * RETURNS:
  2376. * Zero on success, errno on failure.
  2377. */
  2378. int drm_mode_detachmode_ioctl(struct drm_device *dev,
  2379. void *data, struct drm_file *file_priv)
  2380. {
  2381. struct drm_mode_object *obj;
  2382. struct drm_mode_mode_cmd *mode_cmd = data;
  2383. struct drm_connector *connector;
  2384. struct drm_display_mode mode;
  2385. struct drm_mode_modeinfo *umode = &mode_cmd->mode;
  2386. int ret;
  2387. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2388. return -EINVAL;
  2389. mutex_lock(&dev->mode_config.mutex);
  2390. obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
  2391. if (!obj) {
  2392. ret = -EINVAL;
  2393. goto out;
  2394. }
  2395. connector = obj_to_connector(obj);
  2396. ret = drm_crtc_convert_umode(&mode, umode);
  2397. if (ret) {
  2398. DRM_DEBUG_KMS("Invalid mode\n");
  2399. goto out;
  2400. }
  2401. ret = drm_mode_detachmode(dev, connector, &mode);
  2402. out:
  2403. mutex_unlock(&dev->mode_config.mutex);
  2404. return ret;
  2405. }
  2406. struct drm_property *drm_property_create(struct drm_device *dev, int flags,
  2407. const char *name, int num_values)
  2408. {
  2409. struct drm_property *property = NULL;
  2410. int ret;
  2411. property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
  2412. if (!property)
  2413. return NULL;
  2414. if (num_values) {
  2415. property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
  2416. if (!property->values)
  2417. goto fail;
  2418. }
  2419. ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
  2420. if (ret)
  2421. goto fail;
  2422. property->flags = flags;
  2423. property->num_values = num_values;
  2424. INIT_LIST_HEAD(&property->enum_blob_list);
  2425. if (name) {
  2426. strncpy(property->name, name, DRM_PROP_NAME_LEN);
  2427. property->name[DRM_PROP_NAME_LEN-1] = '\0';
  2428. }
  2429. list_add_tail(&property->head, &dev->mode_config.property_list);
  2430. return property;
  2431. fail:
  2432. kfree(property->values);
  2433. kfree(property);
  2434. return NULL;
  2435. }
  2436. EXPORT_SYMBOL(drm_property_create);
  2437. struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
  2438. const char *name,
  2439. const struct drm_prop_enum_list *props,
  2440. int num_values)
  2441. {
  2442. struct drm_property *property;
  2443. int i, ret;
  2444. flags |= DRM_MODE_PROP_ENUM;
  2445. property = drm_property_create(dev, flags, name, num_values);
  2446. if (!property)
  2447. return NULL;
  2448. for (i = 0; i < num_values; i++) {
  2449. ret = drm_property_add_enum(property, i,
  2450. props[i].type,
  2451. props[i].name);
  2452. if (ret) {
  2453. drm_property_destroy(dev, property);
  2454. return NULL;
  2455. }
  2456. }
  2457. return property;
  2458. }
  2459. EXPORT_SYMBOL(drm_property_create_enum);
  2460. struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
  2461. int flags, const char *name,
  2462. const struct drm_prop_enum_list *props,
  2463. int num_values)
  2464. {
  2465. struct drm_property *property;
  2466. int i, ret;
  2467. flags |= DRM_MODE_PROP_BITMASK;
  2468. property = drm_property_create(dev, flags, name, num_values);
  2469. if (!property)
  2470. return NULL;
  2471. for (i = 0; i < num_values; i++) {
  2472. ret = drm_property_add_enum(property, i,
  2473. props[i].type,
  2474. props[i].name);
  2475. if (ret) {
  2476. drm_property_destroy(dev, property);
  2477. return NULL;
  2478. }
  2479. }
  2480. return property;
  2481. }
  2482. EXPORT_SYMBOL(drm_property_create_bitmask);
  2483. struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
  2484. const char *name,
  2485. uint64_t min, uint64_t max)
  2486. {
  2487. struct drm_property *property;
  2488. flags |= DRM_MODE_PROP_RANGE;
  2489. property = drm_property_create(dev, flags, name, 2);
  2490. if (!property)
  2491. return NULL;
  2492. property->values[0] = min;
  2493. property->values[1] = max;
  2494. return property;
  2495. }
  2496. EXPORT_SYMBOL(drm_property_create_range);
  2497. int drm_property_add_enum(struct drm_property *property, int index,
  2498. uint64_t value, const char *name)
  2499. {
  2500. struct drm_property_enum *prop_enum;
  2501. if (!(property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)))
  2502. return -EINVAL;
  2503. /*
  2504. * Bitmask enum properties have the additional constraint of values
  2505. * from 0 to 63
  2506. */
  2507. if ((property->flags & DRM_MODE_PROP_BITMASK) && (value > 63))
  2508. return -EINVAL;
  2509. if (!list_empty(&property->enum_blob_list)) {
  2510. list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
  2511. if (prop_enum->value == value) {
  2512. strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
  2513. prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
  2514. return 0;
  2515. }
  2516. }
  2517. }
  2518. prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
  2519. if (!prop_enum)
  2520. return -ENOMEM;
  2521. strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
  2522. prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
  2523. prop_enum->value = value;
  2524. property->values[index] = value;
  2525. list_add_tail(&prop_enum->head, &property->enum_blob_list);
  2526. return 0;
  2527. }
  2528. EXPORT_SYMBOL(drm_property_add_enum);
  2529. void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
  2530. {
  2531. struct drm_property_enum *prop_enum, *pt;
  2532. list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
  2533. list_del(&prop_enum->head);
  2534. kfree(prop_enum);
  2535. }
  2536. if (property->num_values)
  2537. kfree(property->values);
  2538. drm_mode_object_put(dev, &property->base);
  2539. list_del(&property->head);
  2540. kfree(property);
  2541. }
  2542. EXPORT_SYMBOL(drm_property_destroy);
  2543. void drm_connector_attach_property(struct drm_connector *connector,
  2544. struct drm_property *property, uint64_t init_val)
  2545. {
  2546. drm_object_attach_property(&connector->base, property, init_val);
  2547. }
  2548. EXPORT_SYMBOL(drm_connector_attach_property);
  2549. int drm_connector_property_set_value(struct drm_connector *connector,
  2550. struct drm_property *property, uint64_t value)
  2551. {
  2552. return drm_object_property_set_value(&connector->base, property, value);
  2553. }
  2554. EXPORT_SYMBOL(drm_connector_property_set_value);
  2555. int drm_connector_property_get_value(struct drm_connector *connector,
  2556. struct drm_property *property, uint64_t *val)
  2557. {
  2558. return drm_object_property_get_value(&connector->base, property, val);
  2559. }
  2560. EXPORT_SYMBOL(drm_connector_property_get_value);
  2561. void drm_object_attach_property(struct drm_mode_object *obj,
  2562. struct drm_property *property,
  2563. uint64_t init_val)
  2564. {
  2565. int count = obj->properties->count;
  2566. if (count == DRM_OBJECT_MAX_PROPERTY) {
  2567. WARN(1, "Failed to attach object property (type: 0x%x). Please "
  2568. "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
  2569. "you see this message on the same object type.\n",
  2570. obj->type);
  2571. return;
  2572. }
  2573. obj->properties->ids[count] = property->base.id;
  2574. obj->properties->values[count] = init_val;
  2575. obj->properties->count++;
  2576. }
  2577. EXPORT_SYMBOL(drm_object_attach_property);
  2578. int drm_object_property_set_value(struct drm_mode_object *obj,
  2579. struct drm_property *property, uint64_t val)
  2580. {
  2581. int i;
  2582. for (i = 0; i < obj->properties->count; i++) {
  2583. if (obj->properties->ids[i] == property->base.id) {
  2584. obj->properties->values[i] = val;
  2585. return 0;
  2586. }
  2587. }
  2588. return -EINVAL;
  2589. }
  2590. EXPORT_SYMBOL(drm_object_property_set_value);
  2591. int drm_object_property_get_value(struct drm_mode_object *obj,
  2592. struct drm_property *property, uint64_t *val)
  2593. {
  2594. int i;
  2595. for (i = 0; i < obj->properties->count; i++) {
  2596. if (obj->properties->ids[i] == property->base.id) {
  2597. *val = obj->properties->values[i];
  2598. return 0;
  2599. }
  2600. }
  2601. return -EINVAL;
  2602. }
  2603. EXPORT_SYMBOL(drm_object_property_get_value);
  2604. int drm_mode_getproperty_ioctl(struct drm_device *dev,
  2605. void *data, struct drm_file *file_priv)
  2606. {
  2607. struct drm_mode_object *obj;
  2608. struct drm_mode_get_property *out_resp = data;
  2609. struct drm_property *property;
  2610. int enum_count = 0;
  2611. int blob_count = 0;
  2612. int value_count = 0;
  2613. int ret = 0, i;
  2614. int copied;
  2615. struct drm_property_enum *prop_enum;
  2616. struct drm_mode_property_enum __user *enum_ptr;
  2617. struct drm_property_blob *prop_blob;
  2618. uint32_t __user *blob_id_ptr;
  2619. uint64_t __user *values_ptr;
  2620. uint32_t __user *blob_length_ptr;
  2621. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2622. return -EINVAL;
  2623. mutex_lock(&dev->mode_config.mutex);
  2624. obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
  2625. if (!obj) {
  2626. ret = -EINVAL;
  2627. goto done;
  2628. }
  2629. property = obj_to_property(obj);
  2630. if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
  2631. list_for_each_entry(prop_enum, &property->enum_blob_list, head)
  2632. enum_count++;
  2633. } else if (property->flags & DRM_MODE_PROP_BLOB) {
  2634. list_for_each_entry(prop_blob, &property->enum_blob_list, head)
  2635. blob_count++;
  2636. }
  2637. value_count = property->num_values;
  2638. strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
  2639. out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
  2640. out_resp->flags = property->flags;
  2641. if ((out_resp->count_values >= value_count) && value_count) {
  2642. values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
  2643. for (i = 0; i < value_count; i++) {
  2644. if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
  2645. ret = -EFAULT;
  2646. goto done;
  2647. }
  2648. }
  2649. }
  2650. out_resp->count_values = value_count;
  2651. if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
  2652. if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
  2653. copied = 0;
  2654. enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
  2655. list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
  2656. if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
  2657. ret = -EFAULT;
  2658. goto done;
  2659. }
  2660. if (copy_to_user(&enum_ptr[copied].name,
  2661. &prop_enum->name, DRM_PROP_NAME_LEN)) {
  2662. ret = -EFAULT;
  2663. goto done;
  2664. }
  2665. copied++;
  2666. }
  2667. }
  2668. out_resp->count_enum_blobs = enum_count;
  2669. }
  2670. if (property->flags & DRM_MODE_PROP_BLOB) {
  2671. if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
  2672. copied = 0;
  2673. blob_id_ptr = (uint32_t __user *)(unsigned long)out_resp->enum_blob_ptr;
  2674. blob_length_ptr = (uint32_t __user *)(unsigned long)out_resp->values_ptr;
  2675. list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
  2676. if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
  2677. ret = -EFAULT;
  2678. goto done;
  2679. }
  2680. if (put_user(prop_blob->length, blob_length_ptr + copied)) {
  2681. ret = -EFAULT;
  2682. goto done;
  2683. }
  2684. copied++;
  2685. }
  2686. }
  2687. out_resp->count_enum_blobs = blob_count;
  2688. }
  2689. done:
  2690. mutex_unlock(&dev->mode_config.mutex);
  2691. return ret;
  2692. }
  2693. static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
  2694. void *data)
  2695. {
  2696. struct drm_property_blob *blob;
  2697. int ret;
  2698. if (!length || !data)
  2699. return NULL;
  2700. blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
  2701. if (!blob)
  2702. return NULL;
  2703. ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
  2704. if (ret) {
  2705. kfree(blob);
  2706. return NULL;
  2707. }
  2708. blob->length = length;
  2709. memcpy(blob->data, data, length);
  2710. list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
  2711. return blob;
  2712. }
  2713. static void drm_property_destroy_blob(struct drm_device *dev,
  2714. struct drm_property_blob *blob)
  2715. {
  2716. drm_mode_object_put(dev, &blob->base);
  2717. list_del(&blob->head);
  2718. kfree(blob);
  2719. }
  2720. int drm_mode_getblob_ioctl(struct drm_device *dev,
  2721. void *data, struct drm_file *file_priv)
  2722. {
  2723. struct drm_mode_object *obj;
  2724. struct drm_mode_get_blob *out_resp = data;
  2725. struct drm_property_blob *blob;
  2726. int ret = 0;
  2727. void __user *blob_ptr;
  2728. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2729. return -EINVAL;
  2730. mutex_lock(&dev->mode_config.mutex);
  2731. obj = drm_mode_object_find(dev, out_resp->blob_id, DRM_MODE_OBJECT_BLOB);
  2732. if (!obj) {
  2733. ret = -EINVAL;
  2734. goto done;
  2735. }
  2736. blob = obj_to_blob(obj);
  2737. if (out_resp->length == blob->length) {
  2738. blob_ptr = (void __user *)(unsigned long)out_resp->data;
  2739. if (copy_to_user(blob_ptr, blob->data, blob->length)){
  2740. ret = -EFAULT;
  2741. goto done;
  2742. }
  2743. }
  2744. out_resp->length = blob->length;
  2745. done:
  2746. mutex_unlock(&dev->mode_config.mutex);
  2747. return ret;
  2748. }
  2749. int drm_mode_connector_update_edid_property(struct drm_connector *connector,
  2750. struct edid *edid)
  2751. {
  2752. struct drm_device *dev = connector->dev;
  2753. int ret, size;
  2754. if (connector->edid_blob_ptr)
  2755. drm_property_destroy_blob(dev, connector->edid_blob_ptr);
  2756. /* Delete edid, when there is none. */
  2757. if (!edid) {
  2758. connector->edid_blob_ptr = NULL;
  2759. ret = drm_connector_property_set_value(connector, dev->mode_config.edid_property, 0);
  2760. return ret;
  2761. }
  2762. size = EDID_LENGTH * (1 + edid->extensions);
  2763. connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
  2764. size, edid);
  2765. if (!connector->edid_blob_ptr)
  2766. return -EINVAL;
  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);