drm_crtc.c 86 KB

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