drm_crtc.c 90 KB

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