drm_crtc.c 85 KB

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