drm_crtc.c 95 KB

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