drm_crtc.c 94 KB

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