drm_crtc.c 104 KB

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