drm_crtc.c 80 KB

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