drm_crtc.c 106 KB

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