drm_crtc.c 99 KB

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