drm_crtc.c 100 KB

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