drm_crtc.c 97 KB

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