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