drm_crtc.c 86 KB

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