drm_crtc.c 85 KB

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