drm_crtc.c 85 KB

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