drm_crtc.c 104 KB

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