drm_crtc.c 97 KB

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