drm_crtc.c 86 KB

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