drm_crtc.c 94 KB

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