drm_crtc.c 104 KB

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