drm_crtc.c 90 KB

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