drm_crtc.c 137 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191
  1. /*
  2. * Copyright (c) 2006-2008 Intel Corporation
  3. * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
  4. * Copyright (c) 2008 Red Hat Inc.
  5. *
  6. * DRM core CRTC related functions
  7. *
  8. * Permission to use, copy, modify, distribute, and sell this software and its
  9. * documentation for any purpose is hereby granted without fee, provided that
  10. * the above copyright notice appear in all copies and that both that copyright
  11. * notice and this permission notice appear in supporting documentation, and
  12. * that the name of the copyright holders not be used in advertising or
  13. * publicity pertaining to distribution of the software without specific,
  14. * written prior permission. The copyright holders make no representations
  15. * about the suitability of this software for any purpose. It is provided "as
  16. * is" without express or implied warranty.
  17. *
  18. * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  19. * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  20. * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  21. * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  22. * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  23. * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  24. * OF THIS SOFTWARE.
  25. *
  26. * Authors:
  27. * Keith Packard
  28. * Eric Anholt <eric@anholt.net>
  29. * Dave Airlie <airlied@linux.ie>
  30. * Jesse Barnes <jesse.barnes@intel.com>
  31. */
  32. #include <linux/ctype.h>
  33. #include <linux/list.h>
  34. #include <linux/slab.h>
  35. #include <linux/export.h>
  36. #include <drm/drmP.h>
  37. #include <drm/drm_crtc.h>
  38. #include <drm/drm_edid.h>
  39. #include <drm/drm_fourcc.h>
  40. #include <drm/drm_modeset_lock.h>
  41. #include "drm_crtc_internal.h"
  42. #include "drm_internal.h"
  43. static struct drm_framebuffer *add_framebuffer_internal(struct drm_device *dev,
  44. struct drm_mode_fb_cmd2 *r,
  45. struct drm_file *file_priv);
  46. /* Avoid boilerplate. I'm tired of typing. */
  47. #define DRM_ENUM_NAME_FN(fnname, list) \
  48. const char *fnname(int val) \
  49. { \
  50. int i; \
  51. for (i = 0; i < ARRAY_SIZE(list); i++) { \
  52. if (list[i].type == val) \
  53. return list[i].name; \
  54. } \
  55. return "(unknown)"; \
  56. }
  57. /*
  58. * Global properties
  59. */
  60. static const struct drm_prop_enum_list drm_dpms_enum_list[] =
  61. { { DRM_MODE_DPMS_ON, "On" },
  62. { DRM_MODE_DPMS_STANDBY, "Standby" },
  63. { DRM_MODE_DPMS_SUSPEND, "Suspend" },
  64. { DRM_MODE_DPMS_OFF, "Off" }
  65. };
  66. DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
  67. static const struct drm_prop_enum_list drm_plane_type_enum_list[] =
  68. {
  69. { DRM_PLANE_TYPE_OVERLAY, "Overlay" },
  70. { DRM_PLANE_TYPE_PRIMARY, "Primary" },
  71. { DRM_PLANE_TYPE_CURSOR, "Cursor" },
  72. };
  73. /*
  74. * Optional properties
  75. */
  76. static const struct drm_prop_enum_list drm_scaling_mode_enum_list[] =
  77. {
  78. { DRM_MODE_SCALE_NONE, "None" },
  79. { DRM_MODE_SCALE_FULLSCREEN, "Full" },
  80. { DRM_MODE_SCALE_CENTER, "Center" },
  81. { DRM_MODE_SCALE_ASPECT, "Full aspect" },
  82. };
  83. static const struct drm_prop_enum_list drm_aspect_ratio_enum_list[] = {
  84. { DRM_MODE_PICTURE_ASPECT_NONE, "Automatic" },
  85. { DRM_MODE_PICTURE_ASPECT_4_3, "4:3" },
  86. { DRM_MODE_PICTURE_ASPECT_16_9, "16:9" },
  87. };
  88. /*
  89. * Non-global properties, but "required" for certain connectors.
  90. */
  91. static const struct drm_prop_enum_list drm_dvi_i_select_enum_list[] =
  92. {
  93. { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
  94. { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
  95. { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
  96. };
  97. DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
  98. static const struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] =
  99. {
  100. { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
  101. { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
  102. { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
  103. };
  104. DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
  105. drm_dvi_i_subconnector_enum_list)
  106. static const struct drm_prop_enum_list drm_tv_select_enum_list[] =
  107. {
  108. { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
  109. { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
  110. { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
  111. { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
  112. { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
  113. };
  114. DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
  115. static const struct drm_prop_enum_list drm_tv_subconnector_enum_list[] =
  116. {
  117. { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
  118. { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
  119. { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
  120. { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
  121. { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
  122. };
  123. DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
  124. drm_tv_subconnector_enum_list)
  125. static const struct drm_prop_enum_list drm_dirty_info_enum_list[] = {
  126. { DRM_MODE_DIRTY_OFF, "Off" },
  127. { DRM_MODE_DIRTY_ON, "On" },
  128. { DRM_MODE_DIRTY_ANNOTATE, "Annotate" },
  129. };
  130. struct drm_conn_prop_enum_list {
  131. int type;
  132. const char *name;
  133. struct ida ida;
  134. };
  135. /*
  136. * Connector and encoder types.
  137. */
  138. static struct drm_conn_prop_enum_list drm_connector_enum_list[] =
  139. { { DRM_MODE_CONNECTOR_Unknown, "Unknown" },
  140. { DRM_MODE_CONNECTOR_VGA, "VGA" },
  141. { DRM_MODE_CONNECTOR_DVII, "DVI-I" },
  142. { DRM_MODE_CONNECTOR_DVID, "DVI-D" },
  143. { DRM_MODE_CONNECTOR_DVIA, "DVI-A" },
  144. { DRM_MODE_CONNECTOR_Composite, "Composite" },
  145. { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO" },
  146. { DRM_MODE_CONNECTOR_LVDS, "LVDS" },
  147. { DRM_MODE_CONNECTOR_Component, "Component" },
  148. { DRM_MODE_CONNECTOR_9PinDIN, "DIN" },
  149. { DRM_MODE_CONNECTOR_DisplayPort, "DP" },
  150. { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A" },
  151. { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B" },
  152. { DRM_MODE_CONNECTOR_TV, "TV" },
  153. { DRM_MODE_CONNECTOR_eDP, "eDP" },
  154. { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual" },
  155. { DRM_MODE_CONNECTOR_DSI, "DSI" },
  156. };
  157. static const struct drm_prop_enum_list drm_encoder_enum_list[] =
  158. { { DRM_MODE_ENCODER_NONE, "None" },
  159. { DRM_MODE_ENCODER_DAC, "DAC" },
  160. { DRM_MODE_ENCODER_TMDS, "TMDS" },
  161. { DRM_MODE_ENCODER_LVDS, "LVDS" },
  162. { DRM_MODE_ENCODER_TVDAC, "TV" },
  163. { DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
  164. { DRM_MODE_ENCODER_DSI, "DSI" },
  165. { DRM_MODE_ENCODER_DPMST, "DP MST" },
  166. };
  167. static const struct drm_prop_enum_list drm_subpixel_enum_list[] =
  168. {
  169. { SubPixelUnknown, "Unknown" },
  170. { SubPixelHorizontalRGB, "Horizontal RGB" },
  171. { SubPixelHorizontalBGR, "Horizontal BGR" },
  172. { SubPixelVerticalRGB, "Vertical RGB" },
  173. { SubPixelVerticalBGR, "Vertical BGR" },
  174. { SubPixelNone, "None" },
  175. };
  176. void drm_connector_ida_init(void)
  177. {
  178. int i;
  179. for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
  180. ida_init(&drm_connector_enum_list[i].ida);
  181. }
  182. void drm_connector_ida_destroy(void)
  183. {
  184. int i;
  185. for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
  186. ida_destroy(&drm_connector_enum_list[i].ida);
  187. }
  188. /**
  189. * drm_get_connector_status_name - return a string for connector status
  190. * @status: connector status to compute name of
  191. *
  192. * In contrast to the other drm_get_*_name functions this one here returns a
  193. * const pointer and hence is threadsafe.
  194. */
  195. const char *drm_get_connector_status_name(enum drm_connector_status status)
  196. {
  197. if (status == connector_status_connected)
  198. return "connected";
  199. else if (status == connector_status_disconnected)
  200. return "disconnected";
  201. else
  202. return "unknown";
  203. }
  204. EXPORT_SYMBOL(drm_get_connector_status_name);
  205. /**
  206. * drm_get_subpixel_order_name - return a string for a given subpixel enum
  207. * @order: enum of subpixel_order
  208. *
  209. * Note you could abuse this and return something out of bounds, but that
  210. * would be a caller error. No unscrubbed user data should make it here.
  211. */
  212. const char *drm_get_subpixel_order_name(enum subpixel_order order)
  213. {
  214. return drm_subpixel_enum_list[order].name;
  215. }
  216. EXPORT_SYMBOL(drm_get_subpixel_order_name);
  217. static char printable_char(int c)
  218. {
  219. return isascii(c) && isprint(c) ? c : '?';
  220. }
  221. /**
  222. * drm_get_format_name - return a string for drm fourcc format
  223. * @format: format to compute name of
  224. *
  225. * Note that the buffer used by this function is globally shared and owned by
  226. * the function itself.
  227. *
  228. * FIXME: This isn't really multithreading safe.
  229. */
  230. const char *drm_get_format_name(uint32_t format)
  231. {
  232. static char buf[32];
  233. snprintf(buf, sizeof(buf),
  234. "%c%c%c%c %s-endian (0x%08x)",
  235. printable_char(format & 0xff),
  236. printable_char((format >> 8) & 0xff),
  237. printable_char((format >> 16) & 0xff),
  238. printable_char((format >> 24) & 0x7f),
  239. format & DRM_FORMAT_BIG_ENDIAN ? "big" : "little",
  240. format);
  241. return buf;
  242. }
  243. EXPORT_SYMBOL(drm_get_format_name);
  244. /*
  245. * Internal function to assign a slot in the object idr and optionally
  246. * register the object into the idr.
  247. */
  248. static int drm_mode_object_get_reg(struct drm_device *dev,
  249. struct drm_mode_object *obj,
  250. uint32_t obj_type,
  251. bool register_obj)
  252. {
  253. int ret;
  254. mutex_lock(&dev->mode_config.idr_mutex);
  255. ret = idr_alloc(&dev->mode_config.crtc_idr, register_obj ? obj : NULL, 1, 0, GFP_KERNEL);
  256. if (ret >= 0) {
  257. /*
  258. * Set up the object linking under the protection of the idr
  259. * lock so that other users can't see inconsistent state.
  260. */
  261. obj->id = ret;
  262. obj->type = obj_type;
  263. }
  264. mutex_unlock(&dev->mode_config.idr_mutex);
  265. return ret < 0 ? ret : 0;
  266. }
  267. /**
  268. * drm_mode_object_get - allocate a new modeset identifier
  269. * @dev: DRM device
  270. * @obj: object pointer, used to generate unique ID
  271. * @obj_type: object type
  272. *
  273. * Create a unique identifier based on @ptr in @dev's identifier space. Used
  274. * for tracking modes, CRTCs and connectors. Note that despite the _get postfix
  275. * modeset identifiers are _not_ reference counted. Hence don't use this for
  276. * reference counted modeset objects like framebuffers.
  277. *
  278. * Returns:
  279. * New unique (relative to other objects in @dev) integer identifier for the
  280. * object.
  281. */
  282. int drm_mode_object_get(struct drm_device *dev,
  283. struct drm_mode_object *obj, uint32_t obj_type)
  284. {
  285. return drm_mode_object_get_reg(dev, obj, obj_type, true);
  286. }
  287. static void drm_mode_object_register(struct drm_device *dev,
  288. struct drm_mode_object *obj)
  289. {
  290. mutex_lock(&dev->mode_config.idr_mutex);
  291. idr_replace(&dev->mode_config.crtc_idr, obj, obj->id);
  292. mutex_unlock(&dev->mode_config.idr_mutex);
  293. }
  294. /**
  295. * drm_mode_object_put - free a modeset identifer
  296. * @dev: DRM device
  297. * @object: object to free
  298. *
  299. * Free @id from @dev's unique identifier pool. Note that despite the _get
  300. * postfix modeset identifiers are _not_ reference counted. Hence don't use this
  301. * for reference counted modeset objects like framebuffers.
  302. */
  303. void drm_mode_object_put(struct drm_device *dev,
  304. struct drm_mode_object *object)
  305. {
  306. mutex_lock(&dev->mode_config.idr_mutex);
  307. idr_remove(&dev->mode_config.crtc_idr, object->id);
  308. mutex_unlock(&dev->mode_config.idr_mutex);
  309. }
  310. static struct drm_mode_object *_object_find(struct drm_device *dev,
  311. uint32_t id, uint32_t type)
  312. {
  313. struct drm_mode_object *obj = NULL;
  314. mutex_lock(&dev->mode_config.idr_mutex);
  315. obj = idr_find(&dev->mode_config.crtc_idr, id);
  316. if (obj && type != DRM_MODE_OBJECT_ANY && obj->type != type)
  317. obj = NULL;
  318. if (obj && obj->id != id)
  319. obj = NULL;
  320. /* don't leak out unref'd fb's */
  321. if (obj && (obj->type == DRM_MODE_OBJECT_FB))
  322. obj = NULL;
  323. mutex_unlock(&dev->mode_config.idr_mutex);
  324. return obj;
  325. }
  326. /**
  327. * drm_mode_object_find - look up a drm object with static lifetime
  328. * @dev: drm device
  329. * @id: id of the mode object
  330. * @type: type of the mode object
  331. *
  332. * Note that framebuffers cannot be looked up with this functions - since those
  333. * are reference counted, they need special treatment. Even with
  334. * DRM_MODE_OBJECT_ANY (although that will simply return NULL
  335. * rather than WARN_ON()).
  336. */
  337. struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
  338. uint32_t id, uint32_t type)
  339. {
  340. struct drm_mode_object *obj = NULL;
  341. /* Framebuffers are reference counted and need their own lookup
  342. * function.*/
  343. WARN_ON(type == DRM_MODE_OBJECT_FB);
  344. obj = _object_find(dev, id, type);
  345. return obj;
  346. }
  347. EXPORT_SYMBOL(drm_mode_object_find);
  348. /**
  349. * drm_framebuffer_init - initialize a framebuffer
  350. * @dev: DRM device
  351. * @fb: framebuffer to be initialized
  352. * @funcs: ... with these functions
  353. *
  354. * Allocates an ID for the framebuffer's parent mode object, sets its mode
  355. * functions & device file and adds it to the master fd list.
  356. *
  357. * IMPORTANT:
  358. * This functions publishes the fb and makes it available for concurrent access
  359. * by other users. Which means by this point the fb _must_ be fully set up -
  360. * since all the fb attributes are invariant over its lifetime, no further
  361. * locking but only correct reference counting is required.
  362. *
  363. * Returns:
  364. * Zero on success, error code on failure.
  365. */
  366. int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
  367. const struct drm_framebuffer_funcs *funcs)
  368. {
  369. int ret;
  370. mutex_lock(&dev->mode_config.fb_lock);
  371. kref_init(&fb->refcount);
  372. INIT_LIST_HEAD(&fb->filp_head);
  373. fb->dev = dev;
  374. fb->funcs = funcs;
  375. ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
  376. if (ret)
  377. goto out;
  378. dev->mode_config.num_fb++;
  379. list_add(&fb->head, &dev->mode_config.fb_list);
  380. out:
  381. mutex_unlock(&dev->mode_config.fb_lock);
  382. return 0;
  383. }
  384. EXPORT_SYMBOL(drm_framebuffer_init);
  385. /* dev->mode_config.fb_lock must be held! */
  386. static void __drm_framebuffer_unregister(struct drm_device *dev,
  387. struct drm_framebuffer *fb)
  388. {
  389. mutex_lock(&dev->mode_config.idr_mutex);
  390. idr_remove(&dev->mode_config.crtc_idr, fb->base.id);
  391. mutex_unlock(&dev->mode_config.idr_mutex);
  392. fb->base.id = 0;
  393. }
  394. static void drm_framebuffer_free(struct kref *kref)
  395. {
  396. struct drm_framebuffer *fb =
  397. container_of(kref, struct drm_framebuffer, refcount);
  398. struct drm_device *dev = fb->dev;
  399. /*
  400. * The lookup idr holds a weak reference, which has not necessarily been
  401. * removed at this point. Check for that.
  402. */
  403. mutex_lock(&dev->mode_config.fb_lock);
  404. if (fb->base.id) {
  405. /* Mark fb as reaped and drop idr ref. */
  406. __drm_framebuffer_unregister(dev, fb);
  407. }
  408. mutex_unlock(&dev->mode_config.fb_lock);
  409. fb->funcs->destroy(fb);
  410. }
  411. static struct drm_framebuffer *__drm_framebuffer_lookup(struct drm_device *dev,
  412. uint32_t id)
  413. {
  414. struct drm_mode_object *obj = NULL;
  415. struct drm_framebuffer *fb;
  416. mutex_lock(&dev->mode_config.idr_mutex);
  417. obj = idr_find(&dev->mode_config.crtc_idr, id);
  418. if (!obj || (obj->type != DRM_MODE_OBJECT_FB) || (obj->id != id))
  419. fb = NULL;
  420. else
  421. fb = obj_to_fb(obj);
  422. mutex_unlock(&dev->mode_config.idr_mutex);
  423. return fb;
  424. }
  425. /**
  426. * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
  427. * @dev: drm device
  428. * @id: id of the fb object
  429. *
  430. * If successful, this grabs an additional reference to the framebuffer -
  431. * callers need to make sure to eventually unreference the returned framebuffer
  432. * again, using @drm_framebuffer_unreference.
  433. */
  434. struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
  435. uint32_t id)
  436. {
  437. struct drm_framebuffer *fb;
  438. mutex_lock(&dev->mode_config.fb_lock);
  439. fb = __drm_framebuffer_lookup(dev, id);
  440. if (fb) {
  441. if (!kref_get_unless_zero(&fb->refcount))
  442. fb = NULL;
  443. }
  444. mutex_unlock(&dev->mode_config.fb_lock);
  445. return fb;
  446. }
  447. EXPORT_SYMBOL(drm_framebuffer_lookup);
  448. /**
  449. * drm_framebuffer_unreference - unref a framebuffer
  450. * @fb: framebuffer to unref
  451. *
  452. * This functions decrements the fb's refcount and frees it if it drops to zero.
  453. */
  454. void drm_framebuffer_unreference(struct drm_framebuffer *fb)
  455. {
  456. DRM_DEBUG("%p: FB ID: %d (%d)\n", fb, fb->base.id, atomic_read(&fb->refcount.refcount));
  457. kref_put(&fb->refcount, drm_framebuffer_free);
  458. }
  459. EXPORT_SYMBOL(drm_framebuffer_unreference);
  460. /**
  461. * drm_framebuffer_reference - incr the fb refcnt
  462. * @fb: framebuffer
  463. *
  464. * This functions increments the fb's refcount.
  465. */
  466. void drm_framebuffer_reference(struct drm_framebuffer *fb)
  467. {
  468. DRM_DEBUG("%p: FB ID: %d (%d)\n", fb, fb->base.id, atomic_read(&fb->refcount.refcount));
  469. kref_get(&fb->refcount);
  470. }
  471. EXPORT_SYMBOL(drm_framebuffer_reference);
  472. static void drm_framebuffer_free_bug(struct kref *kref)
  473. {
  474. BUG();
  475. }
  476. static void __drm_framebuffer_unreference(struct drm_framebuffer *fb)
  477. {
  478. DRM_DEBUG("%p: FB ID: %d (%d)\n", fb, fb->base.id, atomic_read(&fb->refcount.refcount));
  479. kref_put(&fb->refcount, drm_framebuffer_free_bug);
  480. }
  481. /**
  482. * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
  483. * @fb: fb to unregister
  484. *
  485. * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
  486. * those used for fbdev. Note that the caller must hold a reference of it's own,
  487. * i.e. the object may not be destroyed through this call (since it'll lead to a
  488. * locking inversion).
  489. */
  490. void drm_framebuffer_unregister_private(struct drm_framebuffer *fb)
  491. {
  492. struct drm_device *dev = fb->dev;
  493. mutex_lock(&dev->mode_config.fb_lock);
  494. /* Mark fb as reaped and drop idr ref. */
  495. __drm_framebuffer_unregister(dev, fb);
  496. mutex_unlock(&dev->mode_config.fb_lock);
  497. }
  498. EXPORT_SYMBOL(drm_framebuffer_unregister_private);
  499. /**
  500. * drm_framebuffer_cleanup - remove a framebuffer object
  501. * @fb: framebuffer to remove
  502. *
  503. * Cleanup framebuffer. This function is intended to be used from the drivers
  504. * ->destroy callback. It can also be used to clean up driver private
  505. * framebuffers embedded into a larger structure.
  506. *
  507. * Note that this function does not remove the fb from active usuage - if it is
  508. * still used anywhere, hilarity can ensue since userspace could call getfb on
  509. * the id and get back -EINVAL. Obviously no concern at driver unload time.
  510. *
  511. * Also, the framebuffer will not be removed from the lookup idr - for
  512. * user-created framebuffers this will happen in in the rmfb ioctl. For
  513. * driver-private objects (e.g. for fbdev) drivers need to explicitly call
  514. * drm_framebuffer_unregister_private.
  515. */
  516. void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
  517. {
  518. struct drm_device *dev = fb->dev;
  519. mutex_lock(&dev->mode_config.fb_lock);
  520. list_del(&fb->head);
  521. dev->mode_config.num_fb--;
  522. mutex_unlock(&dev->mode_config.fb_lock);
  523. }
  524. EXPORT_SYMBOL(drm_framebuffer_cleanup);
  525. /**
  526. * drm_framebuffer_remove - remove and unreference a framebuffer object
  527. * @fb: framebuffer to remove
  528. *
  529. * Scans all the CRTCs and planes in @dev's mode_config. If they're
  530. * using @fb, removes it, setting it to NULL. Then drops the reference to the
  531. * passed-in framebuffer. Might take the modeset locks.
  532. *
  533. * Note that this function optimizes the cleanup away if the caller holds the
  534. * last reference to the framebuffer. It is also guaranteed to not take the
  535. * modeset locks in this case.
  536. */
  537. void drm_framebuffer_remove(struct drm_framebuffer *fb)
  538. {
  539. struct drm_device *dev = fb->dev;
  540. struct drm_crtc *crtc;
  541. struct drm_plane *plane;
  542. struct drm_mode_set set;
  543. int ret;
  544. WARN_ON(!list_empty(&fb->filp_head));
  545. /*
  546. * drm ABI mandates that we remove any deleted framebuffers from active
  547. * useage. But since most sane clients only remove framebuffers they no
  548. * longer need, try to optimize this away.
  549. *
  550. * Since we're holding a reference ourselves, observing a refcount of 1
  551. * means that we're the last holder and can skip it. Also, the refcount
  552. * can never increase from 1 again, so we don't need any barriers or
  553. * locks.
  554. *
  555. * Note that userspace could try to race with use and instate a new
  556. * usage _after_ we've cleared all current ones. End result will be an
  557. * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot
  558. * in this manner.
  559. */
  560. if (atomic_read(&fb->refcount.refcount) > 1) {
  561. drm_modeset_lock_all(dev);
  562. /* remove from any CRTC */
  563. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  564. if (crtc->primary->fb == fb) {
  565. /* should turn off the crtc */
  566. memset(&set, 0, sizeof(struct drm_mode_set));
  567. set.crtc = crtc;
  568. set.fb = NULL;
  569. ret = drm_mode_set_config_internal(&set);
  570. if (ret)
  571. DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
  572. }
  573. }
  574. list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
  575. if (plane->fb == fb)
  576. drm_plane_force_disable(plane);
  577. }
  578. drm_modeset_unlock_all(dev);
  579. }
  580. drm_framebuffer_unreference(fb);
  581. }
  582. EXPORT_SYMBOL(drm_framebuffer_remove);
  583. DEFINE_WW_CLASS(crtc_ww_class);
  584. /**
  585. * drm_crtc_init_with_planes - Initialise a new CRTC object with
  586. * specified primary and cursor planes.
  587. * @dev: DRM device
  588. * @crtc: CRTC object to init
  589. * @primary: Primary plane for CRTC
  590. * @cursor: Cursor plane for CRTC
  591. * @funcs: callbacks for the new CRTC
  592. *
  593. * Inits a new object created as base part of a driver crtc object.
  594. *
  595. * Returns:
  596. * Zero on success, error code on failure.
  597. */
  598. int drm_crtc_init_with_planes(struct drm_device *dev, struct drm_crtc *crtc,
  599. struct drm_plane *primary,
  600. struct drm_plane *cursor,
  601. const struct drm_crtc_funcs *funcs)
  602. {
  603. struct drm_mode_config *config = &dev->mode_config;
  604. int ret;
  605. crtc->dev = dev;
  606. crtc->funcs = funcs;
  607. crtc->invert_dimensions = false;
  608. drm_modeset_lock_init(&crtc->mutex);
  609. ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
  610. if (ret)
  611. goto out;
  612. crtc->base.properties = &crtc->properties;
  613. list_add_tail(&crtc->head, &config->crtc_list);
  614. config->num_crtc++;
  615. crtc->primary = primary;
  616. crtc->cursor = cursor;
  617. if (primary)
  618. primary->possible_crtcs = 1 << drm_crtc_index(crtc);
  619. if (cursor)
  620. cursor->possible_crtcs = 1 << drm_crtc_index(crtc);
  621. out:
  622. return ret;
  623. }
  624. EXPORT_SYMBOL(drm_crtc_init_with_planes);
  625. /**
  626. * drm_crtc_cleanup - Clean up the core crtc usage
  627. * @crtc: CRTC to cleanup
  628. *
  629. * This function cleans up @crtc and removes it from the DRM mode setting
  630. * core. Note that the function does *not* free the crtc structure itself,
  631. * this is the responsibility of the caller.
  632. */
  633. void drm_crtc_cleanup(struct drm_crtc *crtc)
  634. {
  635. struct drm_device *dev = crtc->dev;
  636. kfree(crtc->gamma_store);
  637. crtc->gamma_store = NULL;
  638. drm_modeset_lock_fini(&crtc->mutex);
  639. drm_mode_object_put(dev, &crtc->base);
  640. list_del(&crtc->head);
  641. dev->mode_config.num_crtc--;
  642. }
  643. EXPORT_SYMBOL(drm_crtc_cleanup);
  644. /**
  645. * drm_crtc_index - find the index of a registered CRTC
  646. * @crtc: CRTC to find index for
  647. *
  648. * Given a registered CRTC, return the index of that CRTC within a DRM
  649. * device's list of CRTCs.
  650. */
  651. unsigned int drm_crtc_index(struct drm_crtc *crtc)
  652. {
  653. unsigned int index = 0;
  654. struct drm_crtc *tmp;
  655. list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head) {
  656. if (tmp == crtc)
  657. return index;
  658. index++;
  659. }
  660. BUG();
  661. }
  662. EXPORT_SYMBOL(drm_crtc_index);
  663. /*
  664. * drm_mode_remove - remove and free a mode
  665. * @connector: connector list to modify
  666. * @mode: mode to remove
  667. *
  668. * Remove @mode from @connector's mode list, then free it.
  669. */
  670. static void drm_mode_remove(struct drm_connector *connector,
  671. struct drm_display_mode *mode)
  672. {
  673. list_del(&mode->head);
  674. drm_mode_destroy(connector->dev, mode);
  675. }
  676. /**
  677. * drm_connector_get_cmdline_mode - reads the user's cmdline mode
  678. * @connector: connector to quwery
  679. * @mode: returned mode
  680. *
  681. * The kernel supports per-connector configration of its consoles through
  682. * use of the video= parameter. This function parses that option and
  683. * extracts the user's specified mode (or enable/disable status) for a
  684. * particular connector. This is typically only used during the early fbdev
  685. * setup.
  686. */
  687. static void drm_connector_get_cmdline_mode(struct drm_connector *connector)
  688. {
  689. struct drm_cmdline_mode *mode = &connector->cmdline_mode;
  690. char *option = NULL;
  691. if (fb_get_options(connector->name, &option))
  692. return;
  693. if (!drm_mode_parse_command_line_for_connector(option,
  694. connector,
  695. mode))
  696. return;
  697. if (mode->force) {
  698. const char *s;
  699. switch (mode->force) {
  700. case DRM_FORCE_OFF:
  701. s = "OFF";
  702. break;
  703. case DRM_FORCE_ON_DIGITAL:
  704. s = "ON - dig";
  705. break;
  706. default:
  707. case DRM_FORCE_ON:
  708. s = "ON";
  709. break;
  710. }
  711. DRM_INFO("forcing %s connector %s\n", connector->name, s);
  712. connector->force = mode->force;
  713. }
  714. DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
  715. connector->name,
  716. mode->xres, mode->yres,
  717. mode->refresh_specified ? mode->refresh : 60,
  718. mode->rb ? " reduced blanking" : "",
  719. mode->margins ? " with margins" : "",
  720. mode->interlace ? " interlaced" : "");
  721. }
  722. /**
  723. * drm_connector_init - Init a preallocated connector
  724. * @dev: DRM device
  725. * @connector: the connector to init
  726. * @funcs: callbacks for this connector
  727. * @connector_type: user visible type of the connector
  728. *
  729. * Initialises a preallocated connector. Connectors should be
  730. * subclassed as part of driver connector objects.
  731. *
  732. * Returns:
  733. * Zero on success, error code on failure.
  734. */
  735. int drm_connector_init(struct drm_device *dev,
  736. struct drm_connector *connector,
  737. const struct drm_connector_funcs *funcs,
  738. int connector_type)
  739. {
  740. int ret;
  741. struct ida *connector_ida =
  742. &drm_connector_enum_list[connector_type].ida;
  743. drm_modeset_lock_all(dev);
  744. ret = drm_mode_object_get_reg(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR, false);
  745. if (ret)
  746. goto out_unlock;
  747. connector->base.properties = &connector->properties;
  748. connector->dev = dev;
  749. connector->funcs = funcs;
  750. connector->connector_type = connector_type;
  751. connector->connector_type_id =
  752. ida_simple_get(connector_ida, 1, 0, GFP_KERNEL);
  753. if (connector->connector_type_id < 0) {
  754. ret = connector->connector_type_id;
  755. goto out_put;
  756. }
  757. connector->name =
  758. kasprintf(GFP_KERNEL, "%s-%d",
  759. drm_connector_enum_list[connector_type].name,
  760. connector->connector_type_id);
  761. if (!connector->name) {
  762. ret = -ENOMEM;
  763. goto out_put;
  764. }
  765. INIT_LIST_HEAD(&connector->probed_modes);
  766. INIT_LIST_HEAD(&connector->modes);
  767. connector->edid_blob_ptr = NULL;
  768. connector->status = connector_status_unknown;
  769. drm_connector_get_cmdline_mode(connector);
  770. list_add_tail(&connector->head, &dev->mode_config.connector_list);
  771. dev->mode_config.num_connector++;
  772. if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
  773. drm_object_attach_property(&connector->base,
  774. dev->mode_config.edid_property,
  775. 0);
  776. drm_object_attach_property(&connector->base,
  777. dev->mode_config.dpms_property, 0);
  778. connector->debugfs_entry = NULL;
  779. out_put:
  780. if (ret)
  781. drm_mode_object_put(dev, &connector->base);
  782. out_unlock:
  783. drm_modeset_unlock_all(dev);
  784. return ret;
  785. }
  786. EXPORT_SYMBOL(drm_connector_init);
  787. /**
  788. * drm_connector_cleanup - cleans up an initialised connector
  789. * @connector: connector to cleanup
  790. *
  791. * Cleans up the connector but doesn't free the object.
  792. */
  793. void drm_connector_cleanup(struct drm_connector *connector)
  794. {
  795. struct drm_device *dev = connector->dev;
  796. struct drm_display_mode *mode, *t;
  797. list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
  798. drm_mode_remove(connector, mode);
  799. list_for_each_entry_safe(mode, t, &connector->modes, head)
  800. drm_mode_remove(connector, mode);
  801. ida_remove(&drm_connector_enum_list[connector->connector_type].ida,
  802. connector->connector_type_id);
  803. drm_mode_object_put(dev, &connector->base);
  804. kfree(connector->name);
  805. connector->name = NULL;
  806. list_del(&connector->head);
  807. dev->mode_config.num_connector--;
  808. }
  809. EXPORT_SYMBOL(drm_connector_cleanup);
  810. /**
  811. * drm_connector_index - find the index of a registered connector
  812. * @connector: connector to find index for
  813. *
  814. * Given a registered connector, return the index of that connector within a DRM
  815. * device's list of connectors.
  816. */
  817. unsigned int drm_connector_index(struct drm_connector *connector)
  818. {
  819. unsigned int index = 0;
  820. struct drm_connector *tmp;
  821. list_for_each_entry(tmp, &connector->dev->mode_config.connector_list, head) {
  822. if (tmp == connector)
  823. return index;
  824. index++;
  825. }
  826. BUG();
  827. }
  828. EXPORT_SYMBOL(drm_connector_index);
  829. /**
  830. * drm_connector_register - register a connector
  831. * @connector: the connector to register
  832. *
  833. * Register userspace interfaces for a connector
  834. *
  835. * Returns:
  836. * Zero on success, error code on failure.
  837. */
  838. int drm_connector_register(struct drm_connector *connector)
  839. {
  840. int ret;
  841. drm_mode_object_register(connector->dev, &connector->base);
  842. ret = drm_sysfs_connector_add(connector);
  843. if (ret)
  844. return ret;
  845. ret = drm_debugfs_connector_add(connector);
  846. if (ret) {
  847. drm_sysfs_connector_remove(connector);
  848. return ret;
  849. }
  850. return 0;
  851. }
  852. EXPORT_SYMBOL(drm_connector_register);
  853. /**
  854. * drm_connector_unregister - unregister a connector
  855. * @connector: the connector to unregister
  856. *
  857. * Unregister userspace interfaces for a connector
  858. */
  859. void drm_connector_unregister(struct drm_connector *connector)
  860. {
  861. drm_sysfs_connector_remove(connector);
  862. drm_debugfs_connector_remove(connector);
  863. }
  864. EXPORT_SYMBOL(drm_connector_unregister);
  865. /**
  866. * drm_connector_unplug_all - unregister connector userspace interfaces
  867. * @dev: drm device
  868. *
  869. * This function unregisters all connector userspace interfaces in sysfs. Should
  870. * be call when the device is disconnected, e.g. from an usb driver's
  871. * ->disconnect callback.
  872. */
  873. void drm_connector_unplug_all(struct drm_device *dev)
  874. {
  875. struct drm_connector *connector;
  876. /* taking the mode config mutex ends up in a clash with sysfs */
  877. list_for_each_entry(connector, &dev->mode_config.connector_list, head)
  878. drm_connector_unregister(connector);
  879. }
  880. EXPORT_SYMBOL(drm_connector_unplug_all);
  881. /**
  882. * drm_bridge_init - initialize a drm transcoder/bridge
  883. * @dev: drm device
  884. * @bridge: transcoder/bridge to set up
  885. * @funcs: bridge function table
  886. *
  887. * Initialises a preallocated bridge. Bridges should be
  888. * subclassed as part of driver connector objects.
  889. *
  890. * Returns:
  891. * Zero on success, error code on failure.
  892. */
  893. int drm_bridge_init(struct drm_device *dev, struct drm_bridge *bridge,
  894. const struct drm_bridge_funcs *funcs)
  895. {
  896. int ret;
  897. drm_modeset_lock_all(dev);
  898. ret = drm_mode_object_get(dev, &bridge->base, DRM_MODE_OBJECT_BRIDGE);
  899. if (ret)
  900. goto out;
  901. bridge->dev = dev;
  902. bridge->funcs = funcs;
  903. list_add_tail(&bridge->head, &dev->mode_config.bridge_list);
  904. dev->mode_config.num_bridge++;
  905. out:
  906. drm_modeset_unlock_all(dev);
  907. return ret;
  908. }
  909. EXPORT_SYMBOL(drm_bridge_init);
  910. /**
  911. * drm_bridge_cleanup - cleans up an initialised bridge
  912. * @bridge: bridge to cleanup
  913. *
  914. * Cleans up the bridge but doesn't free the object.
  915. */
  916. void drm_bridge_cleanup(struct drm_bridge *bridge)
  917. {
  918. struct drm_device *dev = bridge->dev;
  919. drm_modeset_lock_all(dev);
  920. drm_mode_object_put(dev, &bridge->base);
  921. list_del(&bridge->head);
  922. dev->mode_config.num_bridge--;
  923. drm_modeset_unlock_all(dev);
  924. }
  925. EXPORT_SYMBOL(drm_bridge_cleanup);
  926. /**
  927. * drm_encoder_init - Init a preallocated encoder
  928. * @dev: drm device
  929. * @encoder: the encoder to init
  930. * @funcs: callbacks for this encoder
  931. * @encoder_type: user visible type of the encoder
  932. *
  933. * Initialises a preallocated encoder. Encoder should be
  934. * subclassed as part of driver encoder objects.
  935. *
  936. * Returns:
  937. * Zero on success, error code on failure.
  938. */
  939. int drm_encoder_init(struct drm_device *dev,
  940. struct drm_encoder *encoder,
  941. const struct drm_encoder_funcs *funcs,
  942. int encoder_type)
  943. {
  944. int ret;
  945. drm_modeset_lock_all(dev);
  946. ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
  947. if (ret)
  948. goto out_unlock;
  949. encoder->dev = dev;
  950. encoder->encoder_type = encoder_type;
  951. encoder->funcs = funcs;
  952. encoder->name = kasprintf(GFP_KERNEL, "%s-%d",
  953. drm_encoder_enum_list[encoder_type].name,
  954. encoder->base.id);
  955. if (!encoder->name) {
  956. ret = -ENOMEM;
  957. goto out_put;
  958. }
  959. list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
  960. dev->mode_config.num_encoder++;
  961. out_put:
  962. if (ret)
  963. drm_mode_object_put(dev, &encoder->base);
  964. out_unlock:
  965. drm_modeset_unlock_all(dev);
  966. return ret;
  967. }
  968. EXPORT_SYMBOL(drm_encoder_init);
  969. /**
  970. * drm_encoder_cleanup - cleans up an initialised encoder
  971. * @encoder: encoder to cleanup
  972. *
  973. * Cleans up the encoder but doesn't free the object.
  974. */
  975. void drm_encoder_cleanup(struct drm_encoder *encoder)
  976. {
  977. struct drm_device *dev = encoder->dev;
  978. drm_modeset_lock_all(dev);
  979. drm_mode_object_put(dev, &encoder->base);
  980. kfree(encoder->name);
  981. encoder->name = NULL;
  982. list_del(&encoder->head);
  983. dev->mode_config.num_encoder--;
  984. drm_modeset_unlock_all(dev);
  985. }
  986. EXPORT_SYMBOL(drm_encoder_cleanup);
  987. /**
  988. * drm_universal_plane_init - Initialize a new universal plane object
  989. * @dev: DRM device
  990. * @plane: plane object to init
  991. * @possible_crtcs: bitmask of possible CRTCs
  992. * @funcs: callbacks for the new plane
  993. * @formats: array of supported formats (%DRM_FORMAT_*)
  994. * @format_count: number of elements in @formats
  995. * @type: type of plane (overlay, primary, cursor)
  996. *
  997. * Initializes a plane object of type @type.
  998. *
  999. * Returns:
  1000. * Zero on success, error code on failure.
  1001. */
  1002. int drm_universal_plane_init(struct drm_device *dev, struct drm_plane *plane,
  1003. unsigned long possible_crtcs,
  1004. const struct drm_plane_funcs *funcs,
  1005. const uint32_t *formats, uint32_t format_count,
  1006. enum drm_plane_type type)
  1007. {
  1008. int ret;
  1009. drm_modeset_lock_all(dev);
  1010. ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
  1011. if (ret)
  1012. goto out;
  1013. plane->base.properties = &plane->properties;
  1014. plane->dev = dev;
  1015. plane->funcs = funcs;
  1016. plane->format_types = kmalloc(sizeof(uint32_t) * format_count,
  1017. GFP_KERNEL);
  1018. if (!plane->format_types) {
  1019. DRM_DEBUG_KMS("out of memory when allocating plane\n");
  1020. drm_mode_object_put(dev, &plane->base);
  1021. ret = -ENOMEM;
  1022. goto out;
  1023. }
  1024. memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
  1025. plane->format_count = format_count;
  1026. plane->possible_crtcs = possible_crtcs;
  1027. plane->type = type;
  1028. list_add_tail(&plane->head, &dev->mode_config.plane_list);
  1029. dev->mode_config.num_total_plane++;
  1030. if (plane->type == DRM_PLANE_TYPE_OVERLAY)
  1031. dev->mode_config.num_overlay_plane++;
  1032. drm_object_attach_property(&plane->base,
  1033. dev->mode_config.plane_type_property,
  1034. plane->type);
  1035. out:
  1036. drm_modeset_unlock_all(dev);
  1037. return ret;
  1038. }
  1039. EXPORT_SYMBOL(drm_universal_plane_init);
  1040. /**
  1041. * drm_plane_init - Initialize a legacy plane
  1042. * @dev: DRM device
  1043. * @plane: plane object to init
  1044. * @possible_crtcs: bitmask of possible CRTCs
  1045. * @funcs: callbacks for the new plane
  1046. * @formats: array of supported formats (%DRM_FORMAT_*)
  1047. * @format_count: number of elements in @formats
  1048. * @is_primary: plane type (primary vs overlay)
  1049. *
  1050. * Legacy API to initialize a DRM plane.
  1051. *
  1052. * New drivers should call drm_universal_plane_init() instead.
  1053. *
  1054. * Returns:
  1055. * Zero on success, error code on failure.
  1056. */
  1057. int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
  1058. unsigned long possible_crtcs,
  1059. const struct drm_plane_funcs *funcs,
  1060. const uint32_t *formats, uint32_t format_count,
  1061. bool is_primary)
  1062. {
  1063. enum drm_plane_type type;
  1064. type = is_primary ? DRM_PLANE_TYPE_PRIMARY : DRM_PLANE_TYPE_OVERLAY;
  1065. return drm_universal_plane_init(dev, plane, possible_crtcs, funcs,
  1066. formats, format_count, type);
  1067. }
  1068. EXPORT_SYMBOL(drm_plane_init);
  1069. /**
  1070. * drm_plane_cleanup - Clean up the core plane usage
  1071. * @plane: plane to cleanup
  1072. *
  1073. * This function cleans up @plane and removes it from the DRM mode setting
  1074. * core. Note that the function does *not* free the plane structure itself,
  1075. * this is the responsibility of the caller.
  1076. */
  1077. void drm_plane_cleanup(struct drm_plane *plane)
  1078. {
  1079. struct drm_device *dev = plane->dev;
  1080. drm_modeset_lock_all(dev);
  1081. kfree(plane->format_types);
  1082. drm_mode_object_put(dev, &plane->base);
  1083. BUG_ON(list_empty(&plane->head));
  1084. list_del(&plane->head);
  1085. dev->mode_config.num_total_plane--;
  1086. if (plane->type == DRM_PLANE_TYPE_OVERLAY)
  1087. dev->mode_config.num_overlay_plane--;
  1088. drm_modeset_unlock_all(dev);
  1089. }
  1090. EXPORT_SYMBOL(drm_plane_cleanup);
  1091. /**
  1092. * drm_plane_index - find the index of a registered plane
  1093. * @plane: plane to find index for
  1094. *
  1095. * Given a registered plane, return the index of that CRTC within a DRM
  1096. * device's list of planes.
  1097. */
  1098. unsigned int drm_plane_index(struct drm_plane *plane)
  1099. {
  1100. unsigned int index = 0;
  1101. struct drm_plane *tmp;
  1102. list_for_each_entry(tmp, &plane->dev->mode_config.plane_list, head) {
  1103. if (tmp == plane)
  1104. return index;
  1105. index++;
  1106. }
  1107. BUG();
  1108. }
  1109. EXPORT_SYMBOL(drm_plane_index);
  1110. /**
  1111. * drm_plane_force_disable - Forcibly disable a plane
  1112. * @plane: plane to disable
  1113. *
  1114. * Forces the plane to be disabled.
  1115. *
  1116. * Used when the plane's current framebuffer is destroyed,
  1117. * and when restoring fbdev mode.
  1118. */
  1119. void drm_plane_force_disable(struct drm_plane *plane)
  1120. {
  1121. int ret;
  1122. if (!plane->fb)
  1123. return;
  1124. plane->old_fb = plane->fb;
  1125. ret = plane->funcs->disable_plane(plane);
  1126. if (ret) {
  1127. DRM_ERROR("failed to disable plane with busy fb\n");
  1128. plane->old_fb = NULL;
  1129. return;
  1130. }
  1131. /* disconnect the plane from the fb and crtc: */
  1132. __drm_framebuffer_unreference(plane->old_fb);
  1133. plane->old_fb = NULL;
  1134. plane->fb = NULL;
  1135. plane->crtc = NULL;
  1136. }
  1137. EXPORT_SYMBOL(drm_plane_force_disable);
  1138. static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
  1139. {
  1140. struct drm_property *edid;
  1141. struct drm_property *dpms;
  1142. struct drm_property *dev_path;
  1143. /*
  1144. * Standard properties (apply to all connectors)
  1145. */
  1146. edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
  1147. DRM_MODE_PROP_IMMUTABLE,
  1148. "EDID", 0);
  1149. dev->mode_config.edid_property = edid;
  1150. dpms = drm_property_create_enum(dev, 0,
  1151. "DPMS", drm_dpms_enum_list,
  1152. ARRAY_SIZE(drm_dpms_enum_list));
  1153. dev->mode_config.dpms_property = dpms;
  1154. dev_path = drm_property_create(dev,
  1155. DRM_MODE_PROP_BLOB |
  1156. DRM_MODE_PROP_IMMUTABLE,
  1157. "PATH", 0);
  1158. dev->mode_config.path_property = dev_path;
  1159. return 0;
  1160. }
  1161. static int drm_mode_create_standard_plane_properties(struct drm_device *dev)
  1162. {
  1163. struct drm_property *type;
  1164. /*
  1165. * Standard properties (apply to all planes)
  1166. */
  1167. type = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
  1168. "type", drm_plane_type_enum_list,
  1169. ARRAY_SIZE(drm_plane_type_enum_list));
  1170. dev->mode_config.plane_type_property = type;
  1171. return 0;
  1172. }
  1173. /**
  1174. * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
  1175. * @dev: DRM device
  1176. *
  1177. * Called by a driver the first time a DVI-I connector is made.
  1178. */
  1179. int drm_mode_create_dvi_i_properties(struct drm_device *dev)
  1180. {
  1181. struct drm_property *dvi_i_selector;
  1182. struct drm_property *dvi_i_subconnector;
  1183. if (dev->mode_config.dvi_i_select_subconnector_property)
  1184. return 0;
  1185. dvi_i_selector =
  1186. drm_property_create_enum(dev, 0,
  1187. "select subconnector",
  1188. drm_dvi_i_select_enum_list,
  1189. ARRAY_SIZE(drm_dvi_i_select_enum_list));
  1190. dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
  1191. dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
  1192. "subconnector",
  1193. drm_dvi_i_subconnector_enum_list,
  1194. ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
  1195. dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
  1196. return 0;
  1197. }
  1198. EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
  1199. /**
  1200. * drm_create_tv_properties - create TV specific connector properties
  1201. * @dev: DRM device
  1202. * @num_modes: number of different TV formats (modes) supported
  1203. * @modes: array of pointers to strings containing name of each format
  1204. *
  1205. * Called by a driver's TV initialization routine, this function creates
  1206. * the TV specific connector properties for a given device. Caller is
  1207. * responsible for allocating a list of format names and passing them to
  1208. * this routine.
  1209. */
  1210. int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
  1211. char *modes[])
  1212. {
  1213. struct drm_property *tv_selector;
  1214. struct drm_property *tv_subconnector;
  1215. int i;
  1216. if (dev->mode_config.tv_select_subconnector_property)
  1217. return 0;
  1218. /*
  1219. * Basic connector properties
  1220. */
  1221. tv_selector = drm_property_create_enum(dev, 0,
  1222. "select subconnector",
  1223. drm_tv_select_enum_list,
  1224. ARRAY_SIZE(drm_tv_select_enum_list));
  1225. dev->mode_config.tv_select_subconnector_property = tv_selector;
  1226. tv_subconnector =
  1227. drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
  1228. "subconnector",
  1229. drm_tv_subconnector_enum_list,
  1230. ARRAY_SIZE(drm_tv_subconnector_enum_list));
  1231. dev->mode_config.tv_subconnector_property = tv_subconnector;
  1232. /*
  1233. * Other, TV specific properties: margins & TV modes.
  1234. */
  1235. dev->mode_config.tv_left_margin_property =
  1236. drm_property_create_range(dev, 0, "left margin", 0, 100);
  1237. dev->mode_config.tv_right_margin_property =
  1238. drm_property_create_range(dev, 0, "right margin", 0, 100);
  1239. dev->mode_config.tv_top_margin_property =
  1240. drm_property_create_range(dev, 0, "top margin", 0, 100);
  1241. dev->mode_config.tv_bottom_margin_property =
  1242. drm_property_create_range(dev, 0, "bottom margin", 0, 100);
  1243. dev->mode_config.tv_mode_property =
  1244. drm_property_create(dev, DRM_MODE_PROP_ENUM,
  1245. "mode", num_modes);
  1246. for (i = 0; i < num_modes; i++)
  1247. drm_property_add_enum(dev->mode_config.tv_mode_property, i,
  1248. i, modes[i]);
  1249. dev->mode_config.tv_brightness_property =
  1250. drm_property_create_range(dev, 0, "brightness", 0, 100);
  1251. dev->mode_config.tv_contrast_property =
  1252. drm_property_create_range(dev, 0, "contrast", 0, 100);
  1253. dev->mode_config.tv_flicker_reduction_property =
  1254. drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
  1255. dev->mode_config.tv_overscan_property =
  1256. drm_property_create_range(dev, 0, "overscan", 0, 100);
  1257. dev->mode_config.tv_saturation_property =
  1258. drm_property_create_range(dev, 0, "saturation", 0, 100);
  1259. dev->mode_config.tv_hue_property =
  1260. drm_property_create_range(dev, 0, "hue", 0, 100);
  1261. return 0;
  1262. }
  1263. EXPORT_SYMBOL(drm_mode_create_tv_properties);
  1264. /**
  1265. * drm_mode_create_scaling_mode_property - create scaling mode property
  1266. * @dev: DRM device
  1267. *
  1268. * Called by a driver the first time it's needed, must be attached to desired
  1269. * connectors.
  1270. */
  1271. int drm_mode_create_scaling_mode_property(struct drm_device *dev)
  1272. {
  1273. struct drm_property *scaling_mode;
  1274. if (dev->mode_config.scaling_mode_property)
  1275. return 0;
  1276. scaling_mode =
  1277. drm_property_create_enum(dev, 0, "scaling mode",
  1278. drm_scaling_mode_enum_list,
  1279. ARRAY_SIZE(drm_scaling_mode_enum_list));
  1280. dev->mode_config.scaling_mode_property = scaling_mode;
  1281. return 0;
  1282. }
  1283. EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
  1284. /**
  1285. * drm_mode_create_aspect_ratio_property - create aspect ratio property
  1286. * @dev: DRM device
  1287. *
  1288. * Called by a driver the first time it's needed, must be attached to desired
  1289. * connectors.
  1290. *
  1291. * Returns:
  1292. * Zero on success, errno on failure.
  1293. */
  1294. int drm_mode_create_aspect_ratio_property(struct drm_device *dev)
  1295. {
  1296. if (dev->mode_config.aspect_ratio_property)
  1297. return 0;
  1298. dev->mode_config.aspect_ratio_property =
  1299. drm_property_create_enum(dev, 0, "aspect ratio",
  1300. drm_aspect_ratio_enum_list,
  1301. ARRAY_SIZE(drm_aspect_ratio_enum_list));
  1302. if (dev->mode_config.aspect_ratio_property == NULL)
  1303. return -ENOMEM;
  1304. return 0;
  1305. }
  1306. EXPORT_SYMBOL(drm_mode_create_aspect_ratio_property);
  1307. /**
  1308. * drm_mode_create_dirty_property - create dirty property
  1309. * @dev: DRM device
  1310. *
  1311. * Called by a driver the first time it's needed, must be attached to desired
  1312. * connectors.
  1313. */
  1314. int drm_mode_create_dirty_info_property(struct drm_device *dev)
  1315. {
  1316. struct drm_property *dirty_info;
  1317. if (dev->mode_config.dirty_info_property)
  1318. return 0;
  1319. dirty_info =
  1320. drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
  1321. "dirty",
  1322. drm_dirty_info_enum_list,
  1323. ARRAY_SIZE(drm_dirty_info_enum_list));
  1324. dev->mode_config.dirty_info_property = dirty_info;
  1325. return 0;
  1326. }
  1327. EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
  1328. static int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
  1329. {
  1330. uint32_t total_objects = 0;
  1331. total_objects += dev->mode_config.num_crtc;
  1332. total_objects += dev->mode_config.num_connector;
  1333. total_objects += dev->mode_config.num_encoder;
  1334. total_objects += dev->mode_config.num_bridge;
  1335. group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
  1336. if (!group->id_list)
  1337. return -ENOMEM;
  1338. group->num_crtcs = 0;
  1339. group->num_connectors = 0;
  1340. group->num_encoders = 0;
  1341. group->num_bridges = 0;
  1342. return 0;
  1343. }
  1344. void drm_mode_group_destroy(struct drm_mode_group *group)
  1345. {
  1346. kfree(group->id_list);
  1347. group->id_list = NULL;
  1348. }
  1349. /*
  1350. * NOTE: Driver's shouldn't ever call drm_mode_group_init_legacy_group - it is
  1351. * the drm core's responsibility to set up mode control groups.
  1352. */
  1353. int drm_mode_group_init_legacy_group(struct drm_device *dev,
  1354. struct drm_mode_group *group)
  1355. {
  1356. struct drm_crtc *crtc;
  1357. struct drm_encoder *encoder;
  1358. struct drm_connector *connector;
  1359. struct drm_bridge *bridge;
  1360. int ret;
  1361. if ((ret = drm_mode_group_init(dev, group)))
  1362. return ret;
  1363. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
  1364. group->id_list[group->num_crtcs++] = crtc->base.id;
  1365. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
  1366. group->id_list[group->num_crtcs + group->num_encoders++] =
  1367. encoder->base.id;
  1368. list_for_each_entry(connector, &dev->mode_config.connector_list, head)
  1369. group->id_list[group->num_crtcs + group->num_encoders +
  1370. group->num_connectors++] = connector->base.id;
  1371. list_for_each_entry(bridge, &dev->mode_config.bridge_list, head)
  1372. group->id_list[group->num_crtcs + group->num_encoders +
  1373. group->num_connectors + group->num_bridges++] =
  1374. bridge->base.id;
  1375. return 0;
  1376. }
  1377. EXPORT_SYMBOL(drm_mode_group_init_legacy_group);
  1378. void drm_reinit_primary_mode_group(struct drm_device *dev)
  1379. {
  1380. drm_modeset_lock_all(dev);
  1381. drm_mode_group_destroy(&dev->primary->mode_group);
  1382. drm_mode_group_init_legacy_group(dev, &dev->primary->mode_group);
  1383. drm_modeset_unlock_all(dev);
  1384. }
  1385. EXPORT_SYMBOL(drm_reinit_primary_mode_group);
  1386. /**
  1387. * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
  1388. * @out: drm_mode_modeinfo struct to return to the user
  1389. * @in: drm_display_mode to use
  1390. *
  1391. * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
  1392. * the user.
  1393. */
  1394. static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
  1395. const struct drm_display_mode *in)
  1396. {
  1397. WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
  1398. in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
  1399. in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
  1400. in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
  1401. in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
  1402. "timing values too large for mode info\n");
  1403. out->clock = in->clock;
  1404. out->hdisplay = in->hdisplay;
  1405. out->hsync_start = in->hsync_start;
  1406. out->hsync_end = in->hsync_end;
  1407. out->htotal = in->htotal;
  1408. out->hskew = in->hskew;
  1409. out->vdisplay = in->vdisplay;
  1410. out->vsync_start = in->vsync_start;
  1411. out->vsync_end = in->vsync_end;
  1412. out->vtotal = in->vtotal;
  1413. out->vscan = in->vscan;
  1414. out->vrefresh = in->vrefresh;
  1415. out->flags = in->flags;
  1416. out->type = in->type;
  1417. strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
  1418. out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
  1419. }
  1420. /**
  1421. * drm_crtc_convert_umode - convert a modeinfo into a drm_display_mode
  1422. * @out: drm_display_mode to return to the user
  1423. * @in: drm_mode_modeinfo to use
  1424. *
  1425. * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
  1426. * the caller.
  1427. *
  1428. * Returns:
  1429. * Zero on success, errno on failure.
  1430. */
  1431. static int drm_crtc_convert_umode(struct drm_display_mode *out,
  1432. const struct drm_mode_modeinfo *in)
  1433. {
  1434. if (in->clock > INT_MAX || in->vrefresh > INT_MAX)
  1435. return -ERANGE;
  1436. if ((in->flags & DRM_MODE_FLAG_3D_MASK) > DRM_MODE_FLAG_3D_MAX)
  1437. return -EINVAL;
  1438. out->clock = in->clock;
  1439. out->hdisplay = in->hdisplay;
  1440. out->hsync_start = in->hsync_start;
  1441. out->hsync_end = in->hsync_end;
  1442. out->htotal = in->htotal;
  1443. out->hskew = in->hskew;
  1444. out->vdisplay = in->vdisplay;
  1445. out->vsync_start = in->vsync_start;
  1446. out->vsync_end = in->vsync_end;
  1447. out->vtotal = in->vtotal;
  1448. out->vscan = in->vscan;
  1449. out->vrefresh = in->vrefresh;
  1450. out->flags = in->flags;
  1451. out->type = in->type;
  1452. strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
  1453. out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
  1454. return 0;
  1455. }
  1456. /**
  1457. * drm_mode_getresources - get graphics configuration
  1458. * @dev: drm device for the ioctl
  1459. * @data: data pointer for the ioctl
  1460. * @file_priv: drm file for the ioctl call
  1461. *
  1462. * Construct a set of configuration description structures and return
  1463. * them to the user, including CRTC, connector and framebuffer configuration.
  1464. *
  1465. * Called by the user via ioctl.
  1466. *
  1467. * Returns:
  1468. * Zero on success, errno on failure.
  1469. */
  1470. int drm_mode_getresources(struct drm_device *dev, void *data,
  1471. struct drm_file *file_priv)
  1472. {
  1473. struct drm_mode_card_res *card_res = data;
  1474. struct list_head *lh;
  1475. struct drm_framebuffer *fb;
  1476. struct drm_connector *connector;
  1477. struct drm_crtc *crtc;
  1478. struct drm_encoder *encoder;
  1479. int ret = 0;
  1480. int connector_count = 0;
  1481. int crtc_count = 0;
  1482. int fb_count = 0;
  1483. int encoder_count = 0;
  1484. int copied = 0, i;
  1485. uint32_t __user *fb_id;
  1486. uint32_t __user *crtc_id;
  1487. uint32_t __user *connector_id;
  1488. uint32_t __user *encoder_id;
  1489. struct drm_mode_group *mode_group;
  1490. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1491. return -EINVAL;
  1492. mutex_lock(&file_priv->fbs_lock);
  1493. /*
  1494. * For the non-control nodes we need to limit the list of resources
  1495. * by IDs in the group list for this node
  1496. */
  1497. list_for_each(lh, &file_priv->fbs)
  1498. fb_count++;
  1499. /* handle this in 4 parts */
  1500. /* FBs */
  1501. if (card_res->count_fbs >= fb_count) {
  1502. copied = 0;
  1503. fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
  1504. list_for_each_entry(fb, &file_priv->fbs, filp_head) {
  1505. if (put_user(fb->base.id, fb_id + copied)) {
  1506. mutex_unlock(&file_priv->fbs_lock);
  1507. return -EFAULT;
  1508. }
  1509. copied++;
  1510. }
  1511. }
  1512. card_res->count_fbs = fb_count;
  1513. mutex_unlock(&file_priv->fbs_lock);
  1514. drm_modeset_lock_all(dev);
  1515. if (!drm_is_primary_client(file_priv)) {
  1516. mode_group = NULL;
  1517. list_for_each(lh, &dev->mode_config.crtc_list)
  1518. crtc_count++;
  1519. list_for_each(lh, &dev->mode_config.connector_list)
  1520. connector_count++;
  1521. list_for_each(lh, &dev->mode_config.encoder_list)
  1522. encoder_count++;
  1523. } else {
  1524. mode_group = &file_priv->master->minor->mode_group;
  1525. crtc_count = mode_group->num_crtcs;
  1526. connector_count = mode_group->num_connectors;
  1527. encoder_count = mode_group->num_encoders;
  1528. }
  1529. card_res->max_height = dev->mode_config.max_height;
  1530. card_res->min_height = dev->mode_config.min_height;
  1531. card_res->max_width = dev->mode_config.max_width;
  1532. card_res->min_width = dev->mode_config.min_width;
  1533. /* CRTCs */
  1534. if (card_res->count_crtcs >= crtc_count) {
  1535. copied = 0;
  1536. crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
  1537. if (!mode_group) {
  1538. list_for_each_entry(crtc, &dev->mode_config.crtc_list,
  1539. head) {
  1540. DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
  1541. if (put_user(crtc->base.id, crtc_id + copied)) {
  1542. ret = -EFAULT;
  1543. goto out;
  1544. }
  1545. copied++;
  1546. }
  1547. } else {
  1548. for (i = 0; i < mode_group->num_crtcs; i++) {
  1549. if (put_user(mode_group->id_list[i],
  1550. crtc_id + copied)) {
  1551. ret = -EFAULT;
  1552. goto out;
  1553. }
  1554. copied++;
  1555. }
  1556. }
  1557. }
  1558. card_res->count_crtcs = crtc_count;
  1559. /* Encoders */
  1560. if (card_res->count_encoders >= encoder_count) {
  1561. copied = 0;
  1562. encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
  1563. if (!mode_group) {
  1564. list_for_each_entry(encoder,
  1565. &dev->mode_config.encoder_list,
  1566. head) {
  1567. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
  1568. encoder->name);
  1569. if (put_user(encoder->base.id, encoder_id +
  1570. copied)) {
  1571. ret = -EFAULT;
  1572. goto out;
  1573. }
  1574. copied++;
  1575. }
  1576. } else {
  1577. for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
  1578. if (put_user(mode_group->id_list[i],
  1579. encoder_id + copied)) {
  1580. ret = -EFAULT;
  1581. goto out;
  1582. }
  1583. copied++;
  1584. }
  1585. }
  1586. }
  1587. card_res->count_encoders = encoder_count;
  1588. /* Connectors */
  1589. if (card_res->count_connectors >= connector_count) {
  1590. copied = 0;
  1591. connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
  1592. if (!mode_group) {
  1593. list_for_each_entry(connector,
  1594. &dev->mode_config.connector_list,
  1595. head) {
  1596. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  1597. connector->base.id,
  1598. connector->name);
  1599. if (put_user(connector->base.id,
  1600. connector_id + copied)) {
  1601. ret = -EFAULT;
  1602. goto out;
  1603. }
  1604. copied++;
  1605. }
  1606. } else {
  1607. int start = mode_group->num_crtcs +
  1608. mode_group->num_encoders;
  1609. for (i = start; i < start + mode_group->num_connectors; i++) {
  1610. if (put_user(mode_group->id_list[i],
  1611. connector_id + copied)) {
  1612. ret = -EFAULT;
  1613. goto out;
  1614. }
  1615. copied++;
  1616. }
  1617. }
  1618. }
  1619. card_res->count_connectors = connector_count;
  1620. DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
  1621. card_res->count_connectors, card_res->count_encoders);
  1622. out:
  1623. drm_modeset_unlock_all(dev);
  1624. return ret;
  1625. }
  1626. /**
  1627. * drm_mode_getcrtc - get CRTC configuration
  1628. * @dev: drm device for the ioctl
  1629. * @data: data pointer for the ioctl
  1630. * @file_priv: drm file for the ioctl call
  1631. *
  1632. * Construct a CRTC configuration structure to return to the user.
  1633. *
  1634. * Called by the user via ioctl.
  1635. *
  1636. * Returns:
  1637. * Zero on success, errno on failure.
  1638. */
  1639. int drm_mode_getcrtc(struct drm_device *dev,
  1640. void *data, struct drm_file *file_priv)
  1641. {
  1642. struct drm_mode_crtc *crtc_resp = data;
  1643. struct drm_crtc *crtc;
  1644. int ret = 0;
  1645. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1646. return -EINVAL;
  1647. drm_modeset_lock_all(dev);
  1648. crtc = drm_crtc_find(dev, crtc_resp->crtc_id);
  1649. if (!crtc) {
  1650. ret = -ENOENT;
  1651. goto out;
  1652. }
  1653. crtc_resp->x = crtc->x;
  1654. crtc_resp->y = crtc->y;
  1655. crtc_resp->gamma_size = crtc->gamma_size;
  1656. if (crtc->primary->fb)
  1657. crtc_resp->fb_id = crtc->primary->fb->base.id;
  1658. else
  1659. crtc_resp->fb_id = 0;
  1660. if (crtc->enabled) {
  1661. drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
  1662. crtc_resp->mode_valid = 1;
  1663. } else {
  1664. crtc_resp->mode_valid = 0;
  1665. }
  1666. out:
  1667. drm_modeset_unlock_all(dev);
  1668. return ret;
  1669. }
  1670. static bool drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
  1671. const struct drm_file *file_priv)
  1672. {
  1673. /*
  1674. * If user-space hasn't configured the driver to expose the stereo 3D
  1675. * modes, don't expose them.
  1676. */
  1677. if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
  1678. return false;
  1679. return true;
  1680. }
  1681. /**
  1682. * drm_mode_getconnector - get connector configuration
  1683. * @dev: drm device for the ioctl
  1684. * @data: data pointer for the ioctl
  1685. * @file_priv: drm file for the ioctl call
  1686. *
  1687. * Construct a connector configuration structure to return to the user.
  1688. *
  1689. * Called by the user via ioctl.
  1690. *
  1691. * Returns:
  1692. * Zero on success, errno on failure.
  1693. */
  1694. int drm_mode_getconnector(struct drm_device *dev, void *data,
  1695. struct drm_file *file_priv)
  1696. {
  1697. struct drm_mode_get_connector *out_resp = data;
  1698. struct drm_connector *connector;
  1699. struct drm_display_mode *mode;
  1700. int mode_count = 0;
  1701. int props_count = 0;
  1702. int encoders_count = 0;
  1703. int ret = 0;
  1704. int copied = 0;
  1705. int i;
  1706. struct drm_mode_modeinfo u_mode;
  1707. struct drm_mode_modeinfo __user *mode_ptr;
  1708. uint32_t __user *prop_ptr;
  1709. uint64_t __user *prop_values;
  1710. uint32_t __user *encoder_ptr;
  1711. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1712. return -EINVAL;
  1713. memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
  1714. DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
  1715. mutex_lock(&dev->mode_config.mutex);
  1716. connector = drm_connector_find(dev, out_resp->connector_id);
  1717. if (!connector) {
  1718. ret = -ENOENT;
  1719. goto out;
  1720. }
  1721. props_count = connector->properties.count;
  1722. for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
  1723. if (connector->encoder_ids[i] != 0) {
  1724. encoders_count++;
  1725. }
  1726. }
  1727. if (out_resp->count_modes == 0) {
  1728. connector->funcs->fill_modes(connector,
  1729. dev->mode_config.max_width,
  1730. dev->mode_config.max_height);
  1731. }
  1732. /* delayed so we get modes regardless of pre-fill_modes state */
  1733. list_for_each_entry(mode, &connector->modes, head)
  1734. if (drm_mode_expose_to_userspace(mode, file_priv))
  1735. mode_count++;
  1736. out_resp->connector_id = connector->base.id;
  1737. out_resp->connector_type = connector->connector_type;
  1738. out_resp->connector_type_id = connector->connector_type_id;
  1739. out_resp->mm_width = connector->display_info.width_mm;
  1740. out_resp->mm_height = connector->display_info.height_mm;
  1741. out_resp->subpixel = connector->display_info.subpixel_order;
  1742. out_resp->connection = connector->status;
  1743. drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
  1744. if (connector->encoder)
  1745. out_resp->encoder_id = connector->encoder->base.id;
  1746. else
  1747. out_resp->encoder_id = 0;
  1748. drm_modeset_unlock(&dev->mode_config.connection_mutex);
  1749. /*
  1750. * This ioctl is called twice, once to determine how much space is
  1751. * needed, and the 2nd time to fill it.
  1752. */
  1753. if ((out_resp->count_modes >= mode_count) && mode_count) {
  1754. copied = 0;
  1755. mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
  1756. list_for_each_entry(mode, &connector->modes, head) {
  1757. if (!drm_mode_expose_to_userspace(mode, file_priv))
  1758. continue;
  1759. drm_crtc_convert_to_umode(&u_mode, mode);
  1760. if (copy_to_user(mode_ptr + copied,
  1761. &u_mode, sizeof(u_mode))) {
  1762. ret = -EFAULT;
  1763. goto out;
  1764. }
  1765. copied++;
  1766. }
  1767. }
  1768. out_resp->count_modes = mode_count;
  1769. if ((out_resp->count_props >= props_count) && props_count) {
  1770. copied = 0;
  1771. prop_ptr = (uint32_t __user *)(unsigned long)(out_resp->props_ptr);
  1772. prop_values = (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr);
  1773. for (i = 0; i < connector->properties.count; i++) {
  1774. if (put_user(connector->properties.ids[i],
  1775. prop_ptr + copied)) {
  1776. ret = -EFAULT;
  1777. goto out;
  1778. }
  1779. if (put_user(connector->properties.values[i],
  1780. prop_values + copied)) {
  1781. ret = -EFAULT;
  1782. goto out;
  1783. }
  1784. copied++;
  1785. }
  1786. }
  1787. out_resp->count_props = props_count;
  1788. if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
  1789. copied = 0;
  1790. encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
  1791. for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
  1792. if (connector->encoder_ids[i] != 0) {
  1793. if (put_user(connector->encoder_ids[i],
  1794. encoder_ptr + copied)) {
  1795. ret = -EFAULT;
  1796. goto out;
  1797. }
  1798. copied++;
  1799. }
  1800. }
  1801. }
  1802. out_resp->count_encoders = encoders_count;
  1803. out:
  1804. mutex_unlock(&dev->mode_config.mutex);
  1805. return ret;
  1806. }
  1807. /**
  1808. * drm_mode_getencoder - get encoder configuration
  1809. * @dev: drm device for the ioctl
  1810. * @data: data pointer for the ioctl
  1811. * @file_priv: drm file for the ioctl call
  1812. *
  1813. * Construct a encoder configuration structure to return to the user.
  1814. *
  1815. * Called by the user via ioctl.
  1816. *
  1817. * Returns:
  1818. * Zero on success, errno on failure.
  1819. */
  1820. int drm_mode_getencoder(struct drm_device *dev, void *data,
  1821. struct drm_file *file_priv)
  1822. {
  1823. struct drm_mode_get_encoder *enc_resp = data;
  1824. struct drm_encoder *encoder;
  1825. int ret = 0;
  1826. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1827. return -EINVAL;
  1828. drm_modeset_lock_all(dev);
  1829. encoder = drm_encoder_find(dev, enc_resp->encoder_id);
  1830. if (!encoder) {
  1831. ret = -ENOENT;
  1832. goto out;
  1833. }
  1834. if (encoder->crtc)
  1835. enc_resp->crtc_id = encoder->crtc->base.id;
  1836. else
  1837. enc_resp->crtc_id = 0;
  1838. enc_resp->encoder_type = encoder->encoder_type;
  1839. enc_resp->encoder_id = encoder->base.id;
  1840. enc_resp->possible_crtcs = encoder->possible_crtcs;
  1841. enc_resp->possible_clones = encoder->possible_clones;
  1842. out:
  1843. drm_modeset_unlock_all(dev);
  1844. return ret;
  1845. }
  1846. /**
  1847. * drm_mode_getplane_res - enumerate all plane resources
  1848. * @dev: DRM device
  1849. * @data: ioctl data
  1850. * @file_priv: DRM file info
  1851. *
  1852. * Construct a list of plane ids to return to the user.
  1853. *
  1854. * Called by the user via ioctl.
  1855. *
  1856. * Returns:
  1857. * Zero on success, errno on failure.
  1858. */
  1859. int drm_mode_getplane_res(struct drm_device *dev, void *data,
  1860. struct drm_file *file_priv)
  1861. {
  1862. struct drm_mode_get_plane_res *plane_resp = data;
  1863. struct drm_mode_config *config;
  1864. struct drm_plane *plane;
  1865. uint32_t __user *plane_ptr;
  1866. int copied = 0, ret = 0;
  1867. unsigned num_planes;
  1868. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1869. return -EINVAL;
  1870. drm_modeset_lock_all(dev);
  1871. config = &dev->mode_config;
  1872. if (file_priv->universal_planes)
  1873. num_planes = config->num_total_plane;
  1874. else
  1875. num_planes = config->num_overlay_plane;
  1876. /*
  1877. * This ioctl is called twice, once to determine how much space is
  1878. * needed, and the 2nd time to fill it.
  1879. */
  1880. if (num_planes &&
  1881. (plane_resp->count_planes >= num_planes)) {
  1882. plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
  1883. list_for_each_entry(plane, &config->plane_list, head) {
  1884. /*
  1885. * Unless userspace set the 'universal planes'
  1886. * capability bit, only advertise overlays.
  1887. */
  1888. if (plane->type != DRM_PLANE_TYPE_OVERLAY &&
  1889. !file_priv->universal_planes)
  1890. continue;
  1891. if (put_user(plane->base.id, plane_ptr + copied)) {
  1892. ret = -EFAULT;
  1893. goto out;
  1894. }
  1895. copied++;
  1896. }
  1897. }
  1898. plane_resp->count_planes = num_planes;
  1899. out:
  1900. drm_modeset_unlock_all(dev);
  1901. return ret;
  1902. }
  1903. /**
  1904. * drm_mode_getplane - get plane configuration
  1905. * @dev: DRM device
  1906. * @data: ioctl data
  1907. * @file_priv: DRM file info
  1908. *
  1909. * Construct a plane configuration structure to return to the user.
  1910. *
  1911. * Called by the user via ioctl.
  1912. *
  1913. * Returns:
  1914. * Zero on success, errno on failure.
  1915. */
  1916. int drm_mode_getplane(struct drm_device *dev, void *data,
  1917. struct drm_file *file_priv)
  1918. {
  1919. struct drm_mode_get_plane *plane_resp = data;
  1920. struct drm_plane *plane;
  1921. uint32_t __user *format_ptr;
  1922. int ret = 0;
  1923. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1924. return -EINVAL;
  1925. drm_modeset_lock_all(dev);
  1926. plane = drm_plane_find(dev, plane_resp->plane_id);
  1927. if (!plane) {
  1928. ret = -ENOENT;
  1929. goto out;
  1930. }
  1931. if (plane->crtc)
  1932. plane_resp->crtc_id = plane->crtc->base.id;
  1933. else
  1934. plane_resp->crtc_id = 0;
  1935. if (plane->fb)
  1936. plane_resp->fb_id = plane->fb->base.id;
  1937. else
  1938. plane_resp->fb_id = 0;
  1939. plane_resp->plane_id = plane->base.id;
  1940. plane_resp->possible_crtcs = plane->possible_crtcs;
  1941. plane_resp->gamma_size = 0;
  1942. /*
  1943. * This ioctl is called twice, once to determine how much space is
  1944. * needed, and the 2nd time to fill it.
  1945. */
  1946. if (plane->format_count &&
  1947. (plane_resp->count_format_types >= plane->format_count)) {
  1948. format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
  1949. if (copy_to_user(format_ptr,
  1950. plane->format_types,
  1951. sizeof(uint32_t) * plane->format_count)) {
  1952. ret = -EFAULT;
  1953. goto out;
  1954. }
  1955. }
  1956. plane_resp->count_format_types = plane->format_count;
  1957. out:
  1958. drm_modeset_unlock_all(dev);
  1959. return ret;
  1960. }
  1961. /*
  1962. * setplane_internal - setplane handler for internal callers
  1963. *
  1964. * Note that we assume an extra reference has already been taken on fb. If the
  1965. * update fails, this reference will be dropped before return; if it succeeds,
  1966. * the previous framebuffer (if any) will be unreferenced instead.
  1967. *
  1968. * src_{x,y,w,h} are provided in 16.16 fixed point format
  1969. */
  1970. static int __setplane_internal(struct drm_plane *plane,
  1971. struct drm_crtc *crtc,
  1972. struct drm_framebuffer *fb,
  1973. int32_t crtc_x, int32_t crtc_y,
  1974. uint32_t crtc_w, uint32_t crtc_h,
  1975. /* src_{x,y,w,h} values are 16.16 fixed point */
  1976. uint32_t src_x, uint32_t src_y,
  1977. uint32_t src_w, uint32_t src_h)
  1978. {
  1979. int ret = 0;
  1980. unsigned int fb_width, fb_height;
  1981. int i;
  1982. /* No fb means shut it down */
  1983. if (!fb) {
  1984. plane->old_fb = plane->fb;
  1985. ret = plane->funcs->disable_plane(plane);
  1986. if (!ret) {
  1987. plane->crtc = NULL;
  1988. plane->fb = NULL;
  1989. } else {
  1990. plane->old_fb = NULL;
  1991. }
  1992. goto out;
  1993. }
  1994. /* Check whether this plane is usable on this CRTC */
  1995. if (!(plane->possible_crtcs & drm_crtc_mask(crtc))) {
  1996. DRM_DEBUG_KMS("Invalid crtc for plane\n");
  1997. ret = -EINVAL;
  1998. goto out;
  1999. }
  2000. /* Check whether this plane supports the fb pixel format. */
  2001. for (i = 0; i < plane->format_count; i++)
  2002. if (fb->pixel_format == plane->format_types[i])
  2003. break;
  2004. if (i == plane->format_count) {
  2005. DRM_DEBUG_KMS("Invalid pixel format %s\n",
  2006. drm_get_format_name(fb->pixel_format));
  2007. ret = -EINVAL;
  2008. goto out;
  2009. }
  2010. fb_width = fb->width << 16;
  2011. fb_height = fb->height << 16;
  2012. /* Make sure source coordinates are inside the fb. */
  2013. if (src_w > fb_width ||
  2014. src_x > fb_width - src_w ||
  2015. src_h > fb_height ||
  2016. src_y > fb_height - src_h) {
  2017. DRM_DEBUG_KMS("Invalid source coordinates "
  2018. "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
  2019. src_w >> 16, ((src_w & 0xffff) * 15625) >> 10,
  2020. src_h >> 16, ((src_h & 0xffff) * 15625) >> 10,
  2021. src_x >> 16, ((src_x & 0xffff) * 15625) >> 10,
  2022. src_y >> 16, ((src_y & 0xffff) * 15625) >> 10);
  2023. ret = -ENOSPC;
  2024. goto out;
  2025. }
  2026. plane->old_fb = plane->fb;
  2027. ret = plane->funcs->update_plane(plane, crtc, fb,
  2028. crtc_x, crtc_y, crtc_w, crtc_h,
  2029. src_x, src_y, src_w, src_h);
  2030. if (!ret) {
  2031. plane->crtc = crtc;
  2032. plane->fb = fb;
  2033. fb = NULL;
  2034. } else {
  2035. plane->old_fb = NULL;
  2036. }
  2037. out:
  2038. if (fb)
  2039. drm_framebuffer_unreference(fb);
  2040. if (plane->old_fb)
  2041. drm_framebuffer_unreference(plane->old_fb);
  2042. plane->old_fb = NULL;
  2043. return ret;
  2044. }
  2045. static int setplane_internal(struct drm_plane *plane,
  2046. struct drm_crtc *crtc,
  2047. struct drm_framebuffer *fb,
  2048. int32_t crtc_x, int32_t crtc_y,
  2049. uint32_t crtc_w, uint32_t crtc_h,
  2050. /* src_{x,y,w,h} values are 16.16 fixed point */
  2051. uint32_t src_x, uint32_t src_y,
  2052. uint32_t src_w, uint32_t src_h)
  2053. {
  2054. int ret;
  2055. drm_modeset_lock_all(plane->dev);
  2056. ret = __setplane_internal(plane, crtc, fb,
  2057. crtc_x, crtc_y, crtc_w, crtc_h,
  2058. src_x, src_y, src_w, src_h);
  2059. drm_modeset_unlock_all(plane->dev);
  2060. return ret;
  2061. }
  2062. /**
  2063. * drm_mode_setplane - configure a plane's configuration
  2064. * @dev: DRM device
  2065. * @data: ioctl data*
  2066. * @file_priv: DRM file info
  2067. *
  2068. * Set plane configuration, including placement, fb, scaling, and other factors.
  2069. * Or pass a NULL fb to disable (planes may be disabled without providing a
  2070. * valid crtc).
  2071. *
  2072. * Returns:
  2073. * Zero on success, errno on failure.
  2074. */
  2075. int drm_mode_setplane(struct drm_device *dev, void *data,
  2076. struct drm_file *file_priv)
  2077. {
  2078. struct drm_mode_set_plane *plane_req = data;
  2079. struct drm_mode_object *obj;
  2080. struct drm_plane *plane;
  2081. struct drm_crtc *crtc = NULL;
  2082. struct drm_framebuffer *fb = NULL;
  2083. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2084. return -EINVAL;
  2085. /* Give drivers some help against integer overflows */
  2086. if (plane_req->crtc_w > INT_MAX ||
  2087. plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w ||
  2088. plane_req->crtc_h > INT_MAX ||
  2089. plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) {
  2090. DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
  2091. plane_req->crtc_w, plane_req->crtc_h,
  2092. plane_req->crtc_x, plane_req->crtc_y);
  2093. return -ERANGE;
  2094. }
  2095. /*
  2096. * First, find the plane, crtc, and fb objects. If not available,
  2097. * we don't bother to call the driver.
  2098. */
  2099. obj = drm_mode_object_find(dev, plane_req->plane_id,
  2100. DRM_MODE_OBJECT_PLANE);
  2101. if (!obj) {
  2102. DRM_DEBUG_KMS("Unknown plane ID %d\n",
  2103. plane_req->plane_id);
  2104. return -ENOENT;
  2105. }
  2106. plane = obj_to_plane(obj);
  2107. if (plane_req->fb_id) {
  2108. fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
  2109. if (!fb) {
  2110. DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
  2111. plane_req->fb_id);
  2112. return -ENOENT;
  2113. }
  2114. obj = drm_mode_object_find(dev, plane_req->crtc_id,
  2115. DRM_MODE_OBJECT_CRTC);
  2116. if (!obj) {
  2117. DRM_DEBUG_KMS("Unknown crtc ID %d\n",
  2118. plane_req->crtc_id);
  2119. return -ENOENT;
  2120. }
  2121. crtc = obj_to_crtc(obj);
  2122. }
  2123. /*
  2124. * setplane_internal will take care of deref'ing either the old or new
  2125. * framebuffer depending on success.
  2126. */
  2127. return setplane_internal(plane, crtc, fb,
  2128. plane_req->crtc_x, plane_req->crtc_y,
  2129. plane_req->crtc_w, plane_req->crtc_h,
  2130. plane_req->src_x, plane_req->src_y,
  2131. plane_req->src_w, plane_req->src_h);
  2132. }
  2133. /**
  2134. * drm_mode_set_config_internal - helper to call ->set_config
  2135. * @set: modeset config to set
  2136. *
  2137. * This is a little helper to wrap internal calls to the ->set_config driver
  2138. * interface. The only thing it adds is correct refcounting dance.
  2139. *
  2140. * Returns:
  2141. * Zero on success, errno on failure.
  2142. */
  2143. int drm_mode_set_config_internal(struct drm_mode_set *set)
  2144. {
  2145. struct drm_crtc *crtc = set->crtc;
  2146. struct drm_framebuffer *fb;
  2147. struct drm_crtc *tmp;
  2148. int ret;
  2149. /*
  2150. * NOTE: ->set_config can also disable other crtcs (if we steal all
  2151. * connectors from it), hence we need to refcount the fbs across all
  2152. * crtcs. Atomic modeset will have saner semantics ...
  2153. */
  2154. list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head)
  2155. tmp->primary->old_fb = tmp->primary->fb;
  2156. fb = set->fb;
  2157. ret = crtc->funcs->set_config(set);
  2158. if (ret == 0) {
  2159. crtc->primary->crtc = crtc;
  2160. crtc->primary->fb = fb;
  2161. }
  2162. list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head) {
  2163. if (tmp->primary->fb)
  2164. drm_framebuffer_reference(tmp->primary->fb);
  2165. if (tmp->primary->old_fb)
  2166. drm_framebuffer_unreference(tmp->primary->old_fb);
  2167. tmp->primary->old_fb = NULL;
  2168. }
  2169. return ret;
  2170. }
  2171. EXPORT_SYMBOL(drm_mode_set_config_internal);
  2172. /**
  2173. * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
  2174. * CRTC viewport
  2175. * @crtc: CRTC that framebuffer will be displayed on
  2176. * @x: x panning
  2177. * @y: y panning
  2178. * @mode: mode that framebuffer will be displayed under
  2179. * @fb: framebuffer to check size of
  2180. */
  2181. int drm_crtc_check_viewport(const struct drm_crtc *crtc,
  2182. int x, int y,
  2183. const struct drm_display_mode *mode,
  2184. const struct drm_framebuffer *fb)
  2185. {
  2186. int hdisplay, vdisplay;
  2187. hdisplay = mode->hdisplay;
  2188. vdisplay = mode->vdisplay;
  2189. if (drm_mode_is_stereo(mode)) {
  2190. struct drm_display_mode adjusted = *mode;
  2191. drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE);
  2192. hdisplay = adjusted.crtc_hdisplay;
  2193. vdisplay = adjusted.crtc_vdisplay;
  2194. }
  2195. if (crtc->invert_dimensions)
  2196. swap(hdisplay, vdisplay);
  2197. if (hdisplay > fb->width ||
  2198. vdisplay > fb->height ||
  2199. x > fb->width - hdisplay ||
  2200. y > fb->height - vdisplay) {
  2201. DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
  2202. fb->width, fb->height, hdisplay, vdisplay, x, y,
  2203. crtc->invert_dimensions ? " (inverted)" : "");
  2204. return -ENOSPC;
  2205. }
  2206. return 0;
  2207. }
  2208. EXPORT_SYMBOL(drm_crtc_check_viewport);
  2209. /**
  2210. * drm_mode_setcrtc - set CRTC configuration
  2211. * @dev: drm device for the ioctl
  2212. * @data: data pointer for the ioctl
  2213. * @file_priv: drm file for the ioctl call
  2214. *
  2215. * Build a new CRTC configuration based on user request.
  2216. *
  2217. * Called by the user via ioctl.
  2218. *
  2219. * Returns:
  2220. * Zero on success, errno on failure.
  2221. */
  2222. int drm_mode_setcrtc(struct drm_device *dev, void *data,
  2223. struct drm_file *file_priv)
  2224. {
  2225. struct drm_mode_config *config = &dev->mode_config;
  2226. struct drm_mode_crtc *crtc_req = data;
  2227. struct drm_crtc *crtc;
  2228. struct drm_connector **connector_set = NULL, *connector;
  2229. struct drm_framebuffer *fb = NULL;
  2230. struct drm_display_mode *mode = NULL;
  2231. struct drm_mode_set set;
  2232. uint32_t __user *set_connectors_ptr;
  2233. int ret;
  2234. int i;
  2235. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2236. return -EINVAL;
  2237. /* For some reason crtc x/y offsets are signed internally. */
  2238. if (crtc_req->x > INT_MAX || crtc_req->y > INT_MAX)
  2239. return -ERANGE;
  2240. drm_modeset_lock_all(dev);
  2241. crtc = drm_crtc_find(dev, crtc_req->crtc_id);
  2242. if (!crtc) {
  2243. DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
  2244. ret = -ENOENT;
  2245. goto out;
  2246. }
  2247. DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
  2248. if (crtc_req->mode_valid) {
  2249. /* If we have a mode we need a framebuffer. */
  2250. /* If we pass -1, set the mode with the currently bound fb */
  2251. if (crtc_req->fb_id == -1) {
  2252. if (!crtc->primary->fb) {
  2253. DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
  2254. ret = -EINVAL;
  2255. goto out;
  2256. }
  2257. fb = crtc->primary->fb;
  2258. /* Make refcounting symmetric with the lookup path. */
  2259. drm_framebuffer_reference(fb);
  2260. } else {
  2261. fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
  2262. if (!fb) {
  2263. DRM_DEBUG_KMS("Unknown FB ID%d\n",
  2264. crtc_req->fb_id);
  2265. ret = -ENOENT;
  2266. goto out;
  2267. }
  2268. }
  2269. mode = drm_mode_create(dev);
  2270. if (!mode) {
  2271. ret = -ENOMEM;
  2272. goto out;
  2273. }
  2274. ret = drm_crtc_convert_umode(mode, &crtc_req->mode);
  2275. if (ret) {
  2276. DRM_DEBUG_KMS("Invalid mode\n");
  2277. goto out;
  2278. }
  2279. drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
  2280. ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
  2281. mode, fb);
  2282. if (ret)
  2283. goto out;
  2284. }
  2285. if (crtc_req->count_connectors == 0 && mode) {
  2286. DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
  2287. ret = -EINVAL;
  2288. goto out;
  2289. }
  2290. if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
  2291. DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
  2292. crtc_req->count_connectors);
  2293. ret = -EINVAL;
  2294. goto out;
  2295. }
  2296. if (crtc_req->count_connectors > 0) {
  2297. u32 out_id;
  2298. /* Avoid unbounded kernel memory allocation */
  2299. if (crtc_req->count_connectors > config->num_connector) {
  2300. ret = -EINVAL;
  2301. goto out;
  2302. }
  2303. connector_set = kmalloc(crtc_req->count_connectors *
  2304. sizeof(struct drm_connector *),
  2305. GFP_KERNEL);
  2306. if (!connector_set) {
  2307. ret = -ENOMEM;
  2308. goto out;
  2309. }
  2310. for (i = 0; i < crtc_req->count_connectors; i++) {
  2311. set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
  2312. if (get_user(out_id, &set_connectors_ptr[i])) {
  2313. ret = -EFAULT;
  2314. goto out;
  2315. }
  2316. connector = drm_connector_find(dev, out_id);
  2317. if (!connector) {
  2318. DRM_DEBUG_KMS("Connector id %d unknown\n",
  2319. out_id);
  2320. ret = -ENOENT;
  2321. goto out;
  2322. }
  2323. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  2324. connector->base.id,
  2325. connector->name);
  2326. connector_set[i] = connector;
  2327. }
  2328. }
  2329. set.crtc = crtc;
  2330. set.x = crtc_req->x;
  2331. set.y = crtc_req->y;
  2332. set.mode = mode;
  2333. set.connectors = connector_set;
  2334. set.num_connectors = crtc_req->count_connectors;
  2335. set.fb = fb;
  2336. ret = drm_mode_set_config_internal(&set);
  2337. out:
  2338. if (fb)
  2339. drm_framebuffer_unreference(fb);
  2340. kfree(connector_set);
  2341. drm_mode_destroy(dev, mode);
  2342. drm_modeset_unlock_all(dev);
  2343. return ret;
  2344. }
  2345. /**
  2346. * drm_mode_cursor_universal - translate legacy cursor ioctl call into a
  2347. * universal plane handler call
  2348. * @crtc: crtc to update cursor for
  2349. * @req: data pointer for the ioctl
  2350. * @file_priv: drm file for the ioctl call
  2351. *
  2352. * Legacy cursor ioctl's work directly with driver buffer handles. To
  2353. * translate legacy ioctl calls into universal plane handler calls, we need to
  2354. * wrap the native buffer handle in a drm_framebuffer.
  2355. *
  2356. * Note that we assume any handle passed to the legacy ioctls was a 32-bit ARGB
  2357. * buffer with a pitch of 4*width; the universal plane interface should be used
  2358. * directly in cases where the hardware can support other buffer settings and
  2359. * userspace wants to make use of these capabilities.
  2360. *
  2361. * Returns:
  2362. * Zero on success, errno on failure.
  2363. */
  2364. static int drm_mode_cursor_universal(struct drm_crtc *crtc,
  2365. struct drm_mode_cursor2 *req,
  2366. struct drm_file *file_priv)
  2367. {
  2368. struct drm_device *dev = crtc->dev;
  2369. struct drm_framebuffer *fb = NULL;
  2370. struct drm_mode_fb_cmd2 fbreq = {
  2371. .width = req->width,
  2372. .height = req->height,
  2373. .pixel_format = DRM_FORMAT_ARGB8888,
  2374. .pitches = { req->width * 4 },
  2375. .handles = { req->handle },
  2376. };
  2377. int32_t crtc_x, crtc_y;
  2378. uint32_t crtc_w = 0, crtc_h = 0;
  2379. uint32_t src_w = 0, src_h = 0;
  2380. int ret = 0;
  2381. BUG_ON(!crtc->cursor);
  2382. WARN_ON(crtc->cursor->crtc != crtc && crtc->cursor->crtc != NULL);
  2383. /*
  2384. * Obtain fb we'll be using (either new or existing) and take an extra
  2385. * reference to it if fb != null. setplane will take care of dropping
  2386. * the reference if the plane update fails.
  2387. */
  2388. if (req->flags & DRM_MODE_CURSOR_BO) {
  2389. if (req->handle) {
  2390. fb = add_framebuffer_internal(dev, &fbreq, file_priv);
  2391. if (IS_ERR(fb)) {
  2392. DRM_DEBUG_KMS("failed to wrap cursor buffer in drm framebuffer\n");
  2393. return PTR_ERR(fb);
  2394. }
  2395. drm_framebuffer_reference(fb);
  2396. } else {
  2397. fb = NULL;
  2398. }
  2399. } else {
  2400. fb = crtc->cursor->fb;
  2401. if (fb)
  2402. drm_framebuffer_reference(fb);
  2403. }
  2404. if (req->flags & DRM_MODE_CURSOR_MOVE) {
  2405. crtc_x = req->x;
  2406. crtc_y = req->y;
  2407. } else {
  2408. crtc_x = crtc->cursor_x;
  2409. crtc_y = crtc->cursor_y;
  2410. }
  2411. if (fb) {
  2412. crtc_w = fb->width;
  2413. crtc_h = fb->height;
  2414. src_w = fb->width << 16;
  2415. src_h = fb->height << 16;
  2416. }
  2417. /*
  2418. * setplane_internal will take care of deref'ing either the old or new
  2419. * framebuffer depending on success.
  2420. */
  2421. ret = __setplane_internal(crtc->cursor, crtc, fb,
  2422. crtc_x, crtc_y, crtc_w, crtc_h,
  2423. 0, 0, src_w, src_h);
  2424. /* Update successful; save new cursor position, if necessary */
  2425. if (ret == 0 && req->flags & DRM_MODE_CURSOR_MOVE) {
  2426. crtc->cursor_x = req->x;
  2427. crtc->cursor_y = req->y;
  2428. }
  2429. return ret;
  2430. }
  2431. static int drm_mode_cursor_common(struct drm_device *dev,
  2432. struct drm_mode_cursor2 *req,
  2433. struct drm_file *file_priv)
  2434. {
  2435. struct drm_crtc *crtc;
  2436. int ret = 0;
  2437. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2438. return -EINVAL;
  2439. if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
  2440. return -EINVAL;
  2441. crtc = drm_crtc_find(dev, req->crtc_id);
  2442. if (!crtc) {
  2443. DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
  2444. return -ENOENT;
  2445. }
  2446. /*
  2447. * If this crtc has a universal cursor plane, call that plane's update
  2448. * handler rather than using legacy cursor handlers.
  2449. */
  2450. drm_modeset_lock_crtc(crtc);
  2451. if (crtc->cursor) {
  2452. ret = drm_mode_cursor_universal(crtc, req, file_priv);
  2453. goto out;
  2454. }
  2455. if (req->flags & DRM_MODE_CURSOR_BO) {
  2456. if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
  2457. ret = -ENXIO;
  2458. goto out;
  2459. }
  2460. /* Turns off the cursor if handle is 0 */
  2461. if (crtc->funcs->cursor_set2)
  2462. ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
  2463. req->width, req->height, req->hot_x, req->hot_y);
  2464. else
  2465. ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
  2466. req->width, req->height);
  2467. }
  2468. if (req->flags & DRM_MODE_CURSOR_MOVE) {
  2469. if (crtc->funcs->cursor_move) {
  2470. ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
  2471. } else {
  2472. ret = -EFAULT;
  2473. goto out;
  2474. }
  2475. }
  2476. out:
  2477. drm_modeset_unlock_crtc(crtc);
  2478. return ret;
  2479. }
  2480. /**
  2481. * drm_mode_cursor_ioctl - set CRTC's cursor configuration
  2482. * @dev: drm device for the ioctl
  2483. * @data: data pointer for the ioctl
  2484. * @file_priv: drm file for the ioctl call
  2485. *
  2486. * Set the cursor configuration based on user request.
  2487. *
  2488. * Called by the user via ioctl.
  2489. *
  2490. * Returns:
  2491. * Zero on success, errno on failure.
  2492. */
  2493. int drm_mode_cursor_ioctl(struct drm_device *dev,
  2494. void *data, struct drm_file *file_priv)
  2495. {
  2496. struct drm_mode_cursor *req = data;
  2497. struct drm_mode_cursor2 new_req;
  2498. memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
  2499. new_req.hot_x = new_req.hot_y = 0;
  2500. return drm_mode_cursor_common(dev, &new_req, file_priv);
  2501. }
  2502. /**
  2503. * drm_mode_cursor2_ioctl - set CRTC's cursor configuration
  2504. * @dev: drm device for the ioctl
  2505. * @data: data pointer for the ioctl
  2506. * @file_priv: drm file for the ioctl call
  2507. *
  2508. * Set the cursor configuration based on user request. This implements the 2nd
  2509. * version of the cursor ioctl, which allows userspace to additionally specify
  2510. * the hotspot of the pointer.
  2511. *
  2512. * Called by the user via ioctl.
  2513. *
  2514. * Returns:
  2515. * Zero on success, errno on failure.
  2516. */
  2517. int drm_mode_cursor2_ioctl(struct drm_device *dev,
  2518. void *data, struct drm_file *file_priv)
  2519. {
  2520. struct drm_mode_cursor2 *req = data;
  2521. return drm_mode_cursor_common(dev, req, file_priv);
  2522. }
  2523. /**
  2524. * drm_mode_legacy_fb_format - compute drm fourcc code from legacy description
  2525. * @bpp: bits per pixels
  2526. * @depth: bit depth per pixel
  2527. *
  2528. * Computes a drm fourcc pixel format code for the given @bpp/@depth values.
  2529. * Useful in fbdev emulation code, since that deals in those values.
  2530. */
  2531. uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
  2532. {
  2533. uint32_t fmt;
  2534. switch (bpp) {
  2535. case 8:
  2536. fmt = DRM_FORMAT_C8;
  2537. break;
  2538. case 16:
  2539. if (depth == 15)
  2540. fmt = DRM_FORMAT_XRGB1555;
  2541. else
  2542. fmt = DRM_FORMAT_RGB565;
  2543. break;
  2544. case 24:
  2545. fmt = DRM_FORMAT_RGB888;
  2546. break;
  2547. case 32:
  2548. if (depth == 24)
  2549. fmt = DRM_FORMAT_XRGB8888;
  2550. else if (depth == 30)
  2551. fmt = DRM_FORMAT_XRGB2101010;
  2552. else
  2553. fmt = DRM_FORMAT_ARGB8888;
  2554. break;
  2555. default:
  2556. DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
  2557. fmt = DRM_FORMAT_XRGB8888;
  2558. break;
  2559. }
  2560. return fmt;
  2561. }
  2562. EXPORT_SYMBOL(drm_mode_legacy_fb_format);
  2563. /**
  2564. * drm_mode_addfb - add an FB to the graphics configuration
  2565. * @dev: drm device for the ioctl
  2566. * @data: data pointer for the ioctl
  2567. * @file_priv: drm file for the ioctl call
  2568. *
  2569. * Add a new FB to the specified CRTC, given a user request. This is the
  2570. * original addfb ioctl which only supported RGB formats.
  2571. *
  2572. * Called by the user via ioctl.
  2573. *
  2574. * Returns:
  2575. * Zero on success, errno on failure.
  2576. */
  2577. int drm_mode_addfb(struct drm_device *dev,
  2578. void *data, struct drm_file *file_priv)
  2579. {
  2580. struct drm_mode_fb_cmd *or = data;
  2581. struct drm_mode_fb_cmd2 r = {};
  2582. int ret;
  2583. /* convert to new format and call new ioctl */
  2584. r.fb_id = or->fb_id;
  2585. r.width = or->width;
  2586. r.height = or->height;
  2587. r.pitches[0] = or->pitch;
  2588. r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
  2589. r.handles[0] = or->handle;
  2590. ret = drm_mode_addfb2(dev, &r, file_priv);
  2591. if (ret)
  2592. return ret;
  2593. or->fb_id = r.fb_id;
  2594. return ret;
  2595. }
  2596. static int format_check(const struct drm_mode_fb_cmd2 *r)
  2597. {
  2598. uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
  2599. switch (format) {
  2600. case DRM_FORMAT_C8:
  2601. case DRM_FORMAT_RGB332:
  2602. case DRM_FORMAT_BGR233:
  2603. case DRM_FORMAT_XRGB4444:
  2604. case DRM_FORMAT_XBGR4444:
  2605. case DRM_FORMAT_RGBX4444:
  2606. case DRM_FORMAT_BGRX4444:
  2607. case DRM_FORMAT_ARGB4444:
  2608. case DRM_FORMAT_ABGR4444:
  2609. case DRM_FORMAT_RGBA4444:
  2610. case DRM_FORMAT_BGRA4444:
  2611. case DRM_FORMAT_XRGB1555:
  2612. case DRM_FORMAT_XBGR1555:
  2613. case DRM_FORMAT_RGBX5551:
  2614. case DRM_FORMAT_BGRX5551:
  2615. case DRM_FORMAT_ARGB1555:
  2616. case DRM_FORMAT_ABGR1555:
  2617. case DRM_FORMAT_RGBA5551:
  2618. case DRM_FORMAT_BGRA5551:
  2619. case DRM_FORMAT_RGB565:
  2620. case DRM_FORMAT_BGR565:
  2621. case DRM_FORMAT_RGB888:
  2622. case DRM_FORMAT_BGR888:
  2623. case DRM_FORMAT_XRGB8888:
  2624. case DRM_FORMAT_XBGR8888:
  2625. case DRM_FORMAT_RGBX8888:
  2626. case DRM_FORMAT_BGRX8888:
  2627. case DRM_FORMAT_ARGB8888:
  2628. case DRM_FORMAT_ABGR8888:
  2629. case DRM_FORMAT_RGBA8888:
  2630. case DRM_FORMAT_BGRA8888:
  2631. case DRM_FORMAT_XRGB2101010:
  2632. case DRM_FORMAT_XBGR2101010:
  2633. case DRM_FORMAT_RGBX1010102:
  2634. case DRM_FORMAT_BGRX1010102:
  2635. case DRM_FORMAT_ARGB2101010:
  2636. case DRM_FORMAT_ABGR2101010:
  2637. case DRM_FORMAT_RGBA1010102:
  2638. case DRM_FORMAT_BGRA1010102:
  2639. case DRM_FORMAT_YUYV:
  2640. case DRM_FORMAT_YVYU:
  2641. case DRM_FORMAT_UYVY:
  2642. case DRM_FORMAT_VYUY:
  2643. case DRM_FORMAT_AYUV:
  2644. case DRM_FORMAT_NV12:
  2645. case DRM_FORMAT_NV21:
  2646. case DRM_FORMAT_NV16:
  2647. case DRM_FORMAT_NV61:
  2648. case DRM_FORMAT_NV24:
  2649. case DRM_FORMAT_NV42:
  2650. case DRM_FORMAT_YUV410:
  2651. case DRM_FORMAT_YVU410:
  2652. case DRM_FORMAT_YUV411:
  2653. case DRM_FORMAT_YVU411:
  2654. case DRM_FORMAT_YUV420:
  2655. case DRM_FORMAT_YVU420:
  2656. case DRM_FORMAT_YUV422:
  2657. case DRM_FORMAT_YVU422:
  2658. case DRM_FORMAT_YUV444:
  2659. case DRM_FORMAT_YVU444:
  2660. return 0;
  2661. default:
  2662. DRM_DEBUG_KMS("invalid pixel format %s\n",
  2663. drm_get_format_name(r->pixel_format));
  2664. return -EINVAL;
  2665. }
  2666. }
  2667. static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
  2668. {
  2669. int ret, hsub, vsub, num_planes, i;
  2670. ret = format_check(r);
  2671. if (ret) {
  2672. DRM_DEBUG_KMS("bad framebuffer format %s\n",
  2673. drm_get_format_name(r->pixel_format));
  2674. return ret;
  2675. }
  2676. hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
  2677. vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
  2678. num_planes = drm_format_num_planes(r->pixel_format);
  2679. if (r->width == 0 || r->width % hsub) {
  2680. DRM_DEBUG_KMS("bad framebuffer width %u\n", r->width);
  2681. return -EINVAL;
  2682. }
  2683. if (r->height == 0 || r->height % vsub) {
  2684. DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
  2685. return -EINVAL;
  2686. }
  2687. for (i = 0; i < num_planes; i++) {
  2688. unsigned int width = r->width / (i != 0 ? hsub : 1);
  2689. unsigned int height = r->height / (i != 0 ? vsub : 1);
  2690. unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
  2691. if (!r->handles[i]) {
  2692. DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
  2693. return -EINVAL;
  2694. }
  2695. if ((uint64_t) width * cpp > UINT_MAX)
  2696. return -ERANGE;
  2697. if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
  2698. return -ERANGE;
  2699. if (r->pitches[i] < width * cpp) {
  2700. DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
  2701. return -EINVAL;
  2702. }
  2703. }
  2704. return 0;
  2705. }
  2706. static struct drm_framebuffer *add_framebuffer_internal(struct drm_device *dev,
  2707. struct drm_mode_fb_cmd2 *r,
  2708. struct drm_file *file_priv)
  2709. {
  2710. struct drm_mode_config *config = &dev->mode_config;
  2711. struct drm_framebuffer *fb;
  2712. int ret;
  2713. if (r->flags & ~DRM_MODE_FB_INTERLACED) {
  2714. DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
  2715. return ERR_PTR(-EINVAL);
  2716. }
  2717. if ((config->min_width > r->width) || (r->width > config->max_width)) {
  2718. DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
  2719. r->width, config->min_width, config->max_width);
  2720. return ERR_PTR(-EINVAL);
  2721. }
  2722. if ((config->min_height > r->height) || (r->height > config->max_height)) {
  2723. DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
  2724. r->height, config->min_height, config->max_height);
  2725. return ERR_PTR(-EINVAL);
  2726. }
  2727. ret = framebuffer_check(r);
  2728. if (ret)
  2729. return ERR_PTR(ret);
  2730. fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
  2731. if (IS_ERR(fb)) {
  2732. DRM_DEBUG_KMS("could not create framebuffer\n");
  2733. return fb;
  2734. }
  2735. mutex_lock(&file_priv->fbs_lock);
  2736. r->fb_id = fb->base.id;
  2737. list_add(&fb->filp_head, &file_priv->fbs);
  2738. DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
  2739. mutex_unlock(&file_priv->fbs_lock);
  2740. return fb;
  2741. }
  2742. /**
  2743. * drm_mode_addfb2 - add an FB to the graphics configuration
  2744. * @dev: drm device for the ioctl
  2745. * @data: data pointer for the ioctl
  2746. * @file_priv: drm file for the ioctl call
  2747. *
  2748. * Add a new FB to the specified CRTC, given a user request with format. This is
  2749. * the 2nd version of the addfb ioctl, which supports multi-planar framebuffers
  2750. * and uses fourcc codes as pixel format specifiers.
  2751. *
  2752. * Called by the user via ioctl.
  2753. *
  2754. * Returns:
  2755. * Zero on success, errno on failure.
  2756. */
  2757. int drm_mode_addfb2(struct drm_device *dev,
  2758. void *data, struct drm_file *file_priv)
  2759. {
  2760. struct drm_framebuffer *fb;
  2761. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2762. return -EINVAL;
  2763. fb = add_framebuffer_internal(dev, data, file_priv);
  2764. if (IS_ERR(fb))
  2765. return PTR_ERR(fb);
  2766. return 0;
  2767. }
  2768. /**
  2769. * drm_mode_rmfb - remove an FB from the configuration
  2770. * @dev: drm device for the ioctl
  2771. * @data: data pointer for the ioctl
  2772. * @file_priv: drm file for the ioctl call
  2773. *
  2774. * Remove the FB specified by the user.
  2775. *
  2776. * Called by the user via ioctl.
  2777. *
  2778. * Returns:
  2779. * Zero on success, errno on failure.
  2780. */
  2781. int drm_mode_rmfb(struct drm_device *dev,
  2782. void *data, struct drm_file *file_priv)
  2783. {
  2784. struct drm_framebuffer *fb = NULL;
  2785. struct drm_framebuffer *fbl = NULL;
  2786. uint32_t *id = data;
  2787. int found = 0;
  2788. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2789. return -EINVAL;
  2790. mutex_lock(&file_priv->fbs_lock);
  2791. mutex_lock(&dev->mode_config.fb_lock);
  2792. fb = __drm_framebuffer_lookup(dev, *id);
  2793. if (!fb)
  2794. goto fail_lookup;
  2795. list_for_each_entry(fbl, &file_priv->fbs, filp_head)
  2796. if (fb == fbl)
  2797. found = 1;
  2798. if (!found)
  2799. goto fail_lookup;
  2800. /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
  2801. __drm_framebuffer_unregister(dev, fb);
  2802. list_del_init(&fb->filp_head);
  2803. mutex_unlock(&dev->mode_config.fb_lock);
  2804. mutex_unlock(&file_priv->fbs_lock);
  2805. drm_framebuffer_remove(fb);
  2806. return 0;
  2807. fail_lookup:
  2808. mutex_unlock(&dev->mode_config.fb_lock);
  2809. mutex_unlock(&file_priv->fbs_lock);
  2810. return -ENOENT;
  2811. }
  2812. /**
  2813. * drm_mode_getfb - get FB info
  2814. * @dev: drm device for the ioctl
  2815. * @data: data pointer for the ioctl
  2816. * @file_priv: drm file for the ioctl call
  2817. *
  2818. * Lookup the FB given its ID and return info about it.
  2819. *
  2820. * Called by the user via ioctl.
  2821. *
  2822. * Returns:
  2823. * Zero on success, errno on failure.
  2824. */
  2825. int drm_mode_getfb(struct drm_device *dev,
  2826. void *data, struct drm_file *file_priv)
  2827. {
  2828. struct drm_mode_fb_cmd *r = data;
  2829. struct drm_framebuffer *fb;
  2830. int ret;
  2831. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2832. return -EINVAL;
  2833. fb = drm_framebuffer_lookup(dev, r->fb_id);
  2834. if (!fb)
  2835. return -ENOENT;
  2836. r->height = fb->height;
  2837. r->width = fb->width;
  2838. r->depth = fb->depth;
  2839. r->bpp = fb->bits_per_pixel;
  2840. r->pitch = fb->pitches[0];
  2841. if (fb->funcs->create_handle) {
  2842. if (file_priv->is_master || capable(CAP_SYS_ADMIN) ||
  2843. drm_is_control_client(file_priv)) {
  2844. ret = fb->funcs->create_handle(fb, file_priv,
  2845. &r->handle);
  2846. } else {
  2847. /* GET_FB() is an unprivileged ioctl so we must not
  2848. * return a buffer-handle to non-master processes! For
  2849. * backwards-compatibility reasons, we cannot make
  2850. * GET_FB() privileged, so just return an invalid handle
  2851. * for non-masters. */
  2852. r->handle = 0;
  2853. ret = 0;
  2854. }
  2855. } else {
  2856. ret = -ENODEV;
  2857. }
  2858. drm_framebuffer_unreference(fb);
  2859. return ret;
  2860. }
  2861. /**
  2862. * drm_mode_dirtyfb_ioctl - flush frontbuffer rendering on an FB
  2863. * @dev: drm device for the ioctl
  2864. * @data: data pointer for the ioctl
  2865. * @file_priv: drm file for the ioctl call
  2866. *
  2867. * Lookup the FB and flush out the damaged area supplied by userspace as a clip
  2868. * rectangle list. Generic userspace which does frontbuffer rendering must call
  2869. * this ioctl to flush out the changes on manual-update display outputs, e.g.
  2870. * usb display-link, mipi manual update panels or edp panel self refresh modes.
  2871. *
  2872. * Modesetting drivers which always update the frontbuffer do not need to
  2873. * implement the corresponding ->dirty framebuffer callback.
  2874. *
  2875. * Called by the user via ioctl.
  2876. *
  2877. * Returns:
  2878. * Zero on success, errno on failure.
  2879. */
  2880. int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
  2881. void *data, struct drm_file *file_priv)
  2882. {
  2883. struct drm_clip_rect __user *clips_ptr;
  2884. struct drm_clip_rect *clips = NULL;
  2885. struct drm_mode_fb_dirty_cmd *r = data;
  2886. struct drm_framebuffer *fb;
  2887. unsigned flags;
  2888. int num_clips;
  2889. int ret;
  2890. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2891. return -EINVAL;
  2892. fb = drm_framebuffer_lookup(dev, r->fb_id);
  2893. if (!fb)
  2894. return -ENOENT;
  2895. num_clips = r->num_clips;
  2896. clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
  2897. if (!num_clips != !clips_ptr) {
  2898. ret = -EINVAL;
  2899. goto out_err1;
  2900. }
  2901. flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
  2902. /* If userspace annotates copy, clips must come in pairs */
  2903. if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
  2904. ret = -EINVAL;
  2905. goto out_err1;
  2906. }
  2907. if (num_clips && clips_ptr) {
  2908. if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
  2909. ret = -EINVAL;
  2910. goto out_err1;
  2911. }
  2912. clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL);
  2913. if (!clips) {
  2914. ret = -ENOMEM;
  2915. goto out_err1;
  2916. }
  2917. ret = copy_from_user(clips, clips_ptr,
  2918. num_clips * sizeof(*clips));
  2919. if (ret) {
  2920. ret = -EFAULT;
  2921. goto out_err2;
  2922. }
  2923. }
  2924. if (fb->funcs->dirty) {
  2925. ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
  2926. clips, num_clips);
  2927. } else {
  2928. ret = -ENOSYS;
  2929. }
  2930. out_err2:
  2931. kfree(clips);
  2932. out_err1:
  2933. drm_framebuffer_unreference(fb);
  2934. return ret;
  2935. }
  2936. /**
  2937. * drm_fb_release - remove and free the FBs on this file
  2938. * @priv: drm file for the ioctl
  2939. *
  2940. * Destroy all the FBs associated with @filp.
  2941. *
  2942. * Called by the user via ioctl.
  2943. *
  2944. * Returns:
  2945. * Zero on success, errno on failure.
  2946. */
  2947. void drm_fb_release(struct drm_file *priv)
  2948. {
  2949. struct drm_device *dev = priv->minor->dev;
  2950. struct drm_framebuffer *fb, *tfb;
  2951. /*
  2952. * When the file gets released that means no one else can access the fb
  2953. * list any more, so no need to grab fpriv->fbs_lock. And we need to to
  2954. * avoid upsetting lockdep since the universal cursor code adds a
  2955. * framebuffer while holding mutex locks.
  2956. *
  2957. * Note that a real deadlock between fpriv->fbs_lock and the modeset
  2958. * locks is impossible here since no one else but this function can get
  2959. * at it any more.
  2960. */
  2961. list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
  2962. mutex_lock(&dev->mode_config.fb_lock);
  2963. /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
  2964. __drm_framebuffer_unregister(dev, fb);
  2965. mutex_unlock(&dev->mode_config.fb_lock);
  2966. list_del_init(&fb->filp_head);
  2967. /* This will also drop the fpriv->fbs reference. */
  2968. drm_framebuffer_remove(fb);
  2969. }
  2970. }
  2971. /**
  2972. * drm_property_create - create a new property type
  2973. * @dev: drm device
  2974. * @flags: flags specifying the property type
  2975. * @name: name of the property
  2976. * @num_values: number of pre-defined values
  2977. *
  2978. * This creates a new generic drm property which can then be attached to a drm
  2979. * object with drm_object_attach_property. The returned property object must be
  2980. * freed with drm_property_destroy.
  2981. *
  2982. * Returns:
  2983. * A pointer to the newly created property on success, NULL on failure.
  2984. */
  2985. struct drm_property *drm_property_create(struct drm_device *dev, int flags,
  2986. const char *name, int num_values)
  2987. {
  2988. struct drm_property *property = NULL;
  2989. int ret;
  2990. property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
  2991. if (!property)
  2992. return NULL;
  2993. property->dev = dev;
  2994. if (num_values) {
  2995. property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
  2996. if (!property->values)
  2997. goto fail;
  2998. }
  2999. ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
  3000. if (ret)
  3001. goto fail;
  3002. property->flags = flags;
  3003. property->num_values = num_values;
  3004. INIT_LIST_HEAD(&property->enum_blob_list);
  3005. if (name) {
  3006. strncpy(property->name, name, DRM_PROP_NAME_LEN);
  3007. property->name[DRM_PROP_NAME_LEN-1] = '\0';
  3008. }
  3009. list_add_tail(&property->head, &dev->mode_config.property_list);
  3010. WARN_ON(!drm_property_type_valid(property));
  3011. return property;
  3012. fail:
  3013. kfree(property->values);
  3014. kfree(property);
  3015. return NULL;
  3016. }
  3017. EXPORT_SYMBOL(drm_property_create);
  3018. /**
  3019. * drm_property_create_enum - create a new enumeration property type
  3020. * @dev: drm device
  3021. * @flags: flags specifying the property type
  3022. * @name: name of the property
  3023. * @props: enumeration lists with property values
  3024. * @num_values: number of pre-defined values
  3025. *
  3026. * This creates a new generic drm property which can then be attached to a drm
  3027. * object with drm_object_attach_property. The returned property object must be
  3028. * freed with drm_property_destroy.
  3029. *
  3030. * Userspace is only allowed to set one of the predefined values for enumeration
  3031. * properties.
  3032. *
  3033. * Returns:
  3034. * A pointer to the newly created property on success, NULL on failure.
  3035. */
  3036. struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
  3037. const char *name,
  3038. const struct drm_prop_enum_list *props,
  3039. int num_values)
  3040. {
  3041. struct drm_property *property;
  3042. int i, ret;
  3043. flags |= DRM_MODE_PROP_ENUM;
  3044. property = drm_property_create(dev, flags, name, num_values);
  3045. if (!property)
  3046. return NULL;
  3047. for (i = 0; i < num_values; i++) {
  3048. ret = drm_property_add_enum(property, i,
  3049. props[i].type,
  3050. props[i].name);
  3051. if (ret) {
  3052. drm_property_destroy(dev, property);
  3053. return NULL;
  3054. }
  3055. }
  3056. return property;
  3057. }
  3058. EXPORT_SYMBOL(drm_property_create_enum);
  3059. /**
  3060. * drm_property_create_bitmask - create a new bitmask property type
  3061. * @dev: drm device
  3062. * @flags: flags specifying the property type
  3063. * @name: name of the property
  3064. * @props: enumeration lists with property bitflags
  3065. * @num_props: size of the @props array
  3066. * @supported_bits: bitmask of all supported enumeration values
  3067. *
  3068. * This creates a new bitmask drm property which can then be attached to a drm
  3069. * object with drm_object_attach_property. The returned property object must be
  3070. * freed with drm_property_destroy.
  3071. *
  3072. * Compared to plain enumeration properties userspace is allowed to set any
  3073. * or'ed together combination of the predefined property bitflag values
  3074. *
  3075. * Returns:
  3076. * A pointer to the newly created property on success, NULL on failure.
  3077. */
  3078. struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
  3079. int flags, const char *name,
  3080. const struct drm_prop_enum_list *props,
  3081. int num_props,
  3082. uint64_t supported_bits)
  3083. {
  3084. struct drm_property *property;
  3085. int i, ret, index = 0;
  3086. int num_values = hweight64(supported_bits);
  3087. flags |= DRM_MODE_PROP_BITMASK;
  3088. property = drm_property_create(dev, flags, name, num_values);
  3089. if (!property)
  3090. return NULL;
  3091. for (i = 0; i < num_props; i++) {
  3092. if (!(supported_bits & (1ULL << props[i].type)))
  3093. continue;
  3094. if (WARN_ON(index >= num_values)) {
  3095. drm_property_destroy(dev, property);
  3096. return NULL;
  3097. }
  3098. ret = drm_property_add_enum(property, index++,
  3099. props[i].type,
  3100. props[i].name);
  3101. if (ret) {
  3102. drm_property_destroy(dev, property);
  3103. return NULL;
  3104. }
  3105. }
  3106. return property;
  3107. }
  3108. EXPORT_SYMBOL(drm_property_create_bitmask);
  3109. static struct drm_property *property_create_range(struct drm_device *dev,
  3110. int flags, const char *name,
  3111. uint64_t min, uint64_t max)
  3112. {
  3113. struct drm_property *property;
  3114. property = drm_property_create(dev, flags, name, 2);
  3115. if (!property)
  3116. return NULL;
  3117. property->values[0] = min;
  3118. property->values[1] = max;
  3119. return property;
  3120. }
  3121. /**
  3122. * drm_property_create_range - create a new ranged property type
  3123. * @dev: drm device
  3124. * @flags: flags specifying the property type
  3125. * @name: name of the property
  3126. * @min: minimum value of the property
  3127. * @max: maximum value of the property
  3128. *
  3129. * This creates a new generic drm property which can then be attached to a drm
  3130. * object with drm_object_attach_property. The returned property object must be
  3131. * freed with drm_property_destroy.
  3132. *
  3133. * Userspace is allowed to set any integer value in the (min, max) range
  3134. * inclusive.
  3135. *
  3136. * Returns:
  3137. * A pointer to the newly created property on success, NULL on failure.
  3138. */
  3139. struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
  3140. const char *name,
  3141. uint64_t min, uint64_t max)
  3142. {
  3143. return property_create_range(dev, DRM_MODE_PROP_RANGE | flags,
  3144. name, min, max);
  3145. }
  3146. EXPORT_SYMBOL(drm_property_create_range);
  3147. struct drm_property *drm_property_create_signed_range(struct drm_device *dev,
  3148. int flags, const char *name,
  3149. int64_t min, int64_t max)
  3150. {
  3151. return property_create_range(dev, DRM_MODE_PROP_SIGNED_RANGE | flags,
  3152. name, I642U64(min), I642U64(max));
  3153. }
  3154. EXPORT_SYMBOL(drm_property_create_signed_range);
  3155. struct drm_property *drm_property_create_object(struct drm_device *dev,
  3156. int flags, const char *name, uint32_t type)
  3157. {
  3158. struct drm_property *property;
  3159. flags |= DRM_MODE_PROP_OBJECT;
  3160. property = drm_property_create(dev, flags, name, 1);
  3161. if (!property)
  3162. return NULL;
  3163. property->values[0] = type;
  3164. return property;
  3165. }
  3166. EXPORT_SYMBOL(drm_property_create_object);
  3167. /**
  3168. * drm_property_add_enum - add a possible value to an enumeration property
  3169. * @property: enumeration property to change
  3170. * @index: index of the new enumeration
  3171. * @value: value of the new enumeration
  3172. * @name: symbolic name of the new enumeration
  3173. *
  3174. * This functions adds enumerations to a property.
  3175. *
  3176. * It's use is deprecated, drivers should use one of the more specific helpers
  3177. * to directly create the property with all enumerations already attached.
  3178. *
  3179. * Returns:
  3180. * Zero on success, error code on failure.
  3181. */
  3182. int drm_property_add_enum(struct drm_property *property, int index,
  3183. uint64_t value, const char *name)
  3184. {
  3185. struct drm_property_enum *prop_enum;
  3186. if (!(drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
  3187. drm_property_type_is(property, DRM_MODE_PROP_BITMASK)))
  3188. return -EINVAL;
  3189. /*
  3190. * Bitmask enum properties have the additional constraint of values
  3191. * from 0 to 63
  3192. */
  3193. if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK) &&
  3194. (value > 63))
  3195. return -EINVAL;
  3196. if (!list_empty(&property->enum_blob_list)) {
  3197. list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
  3198. if (prop_enum->value == value) {
  3199. strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
  3200. prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
  3201. return 0;
  3202. }
  3203. }
  3204. }
  3205. prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
  3206. if (!prop_enum)
  3207. return -ENOMEM;
  3208. strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
  3209. prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
  3210. prop_enum->value = value;
  3211. property->values[index] = value;
  3212. list_add_tail(&prop_enum->head, &property->enum_blob_list);
  3213. return 0;
  3214. }
  3215. EXPORT_SYMBOL(drm_property_add_enum);
  3216. /**
  3217. * drm_property_destroy - destroy a drm property
  3218. * @dev: drm device
  3219. * @property: property to destry
  3220. *
  3221. * This function frees a property including any attached resources like
  3222. * enumeration values.
  3223. */
  3224. void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
  3225. {
  3226. struct drm_property_enum *prop_enum, *pt;
  3227. list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
  3228. list_del(&prop_enum->head);
  3229. kfree(prop_enum);
  3230. }
  3231. if (property->num_values)
  3232. kfree(property->values);
  3233. drm_mode_object_put(dev, &property->base);
  3234. list_del(&property->head);
  3235. kfree(property);
  3236. }
  3237. EXPORT_SYMBOL(drm_property_destroy);
  3238. /**
  3239. * drm_object_attach_property - attach a property to a modeset object
  3240. * @obj: drm modeset object
  3241. * @property: property to attach
  3242. * @init_val: initial value of the property
  3243. *
  3244. * This attaches the given property to the modeset object with the given initial
  3245. * value. Currently this function cannot fail since the properties are stored in
  3246. * a statically sized array.
  3247. */
  3248. void drm_object_attach_property(struct drm_mode_object *obj,
  3249. struct drm_property *property,
  3250. uint64_t init_val)
  3251. {
  3252. int count = obj->properties->count;
  3253. if (count == DRM_OBJECT_MAX_PROPERTY) {
  3254. WARN(1, "Failed to attach object property (type: 0x%x). Please "
  3255. "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
  3256. "you see this message on the same object type.\n",
  3257. obj->type);
  3258. return;
  3259. }
  3260. obj->properties->ids[count] = property->base.id;
  3261. obj->properties->values[count] = init_val;
  3262. obj->properties->count++;
  3263. }
  3264. EXPORT_SYMBOL(drm_object_attach_property);
  3265. /**
  3266. * drm_object_property_set_value - set the value of a property
  3267. * @obj: drm mode object to set property value for
  3268. * @property: property to set
  3269. * @val: value the property should be set to
  3270. *
  3271. * This functions sets a given property on a given object. This function only
  3272. * changes the software state of the property, it does not call into the
  3273. * driver's ->set_property callback.
  3274. *
  3275. * Returns:
  3276. * Zero on success, error code on failure.
  3277. */
  3278. int drm_object_property_set_value(struct drm_mode_object *obj,
  3279. struct drm_property *property, uint64_t val)
  3280. {
  3281. int i;
  3282. for (i = 0; i < obj->properties->count; i++) {
  3283. if (obj->properties->ids[i] == property->base.id) {
  3284. obj->properties->values[i] = val;
  3285. return 0;
  3286. }
  3287. }
  3288. return -EINVAL;
  3289. }
  3290. EXPORT_SYMBOL(drm_object_property_set_value);
  3291. /**
  3292. * drm_object_property_get_value - retrieve the value of a property
  3293. * @obj: drm mode object to get property value from
  3294. * @property: property to retrieve
  3295. * @val: storage for the property value
  3296. *
  3297. * This function retrieves the softare state of the given property for the given
  3298. * property. Since there is no driver callback to retrieve the current property
  3299. * value this might be out of sync with the hardware, depending upon the driver
  3300. * and property.
  3301. *
  3302. * Returns:
  3303. * Zero on success, error code on failure.
  3304. */
  3305. int drm_object_property_get_value(struct drm_mode_object *obj,
  3306. struct drm_property *property, uint64_t *val)
  3307. {
  3308. int i;
  3309. for (i = 0; i < obj->properties->count; i++) {
  3310. if (obj->properties->ids[i] == property->base.id) {
  3311. *val = obj->properties->values[i];
  3312. return 0;
  3313. }
  3314. }
  3315. return -EINVAL;
  3316. }
  3317. EXPORT_SYMBOL(drm_object_property_get_value);
  3318. /**
  3319. * drm_mode_getproperty_ioctl - get the current value of a connector's property
  3320. * @dev: DRM device
  3321. * @data: ioctl data
  3322. * @file_priv: DRM file info
  3323. *
  3324. * This function retrieves the current value for an connectors's property.
  3325. *
  3326. * Called by the user via ioctl.
  3327. *
  3328. * Returns:
  3329. * Zero on success, errno on failure.
  3330. */
  3331. int drm_mode_getproperty_ioctl(struct drm_device *dev,
  3332. void *data, struct drm_file *file_priv)
  3333. {
  3334. struct drm_mode_get_property *out_resp = data;
  3335. struct drm_property *property;
  3336. int enum_count = 0;
  3337. int blob_count = 0;
  3338. int value_count = 0;
  3339. int ret = 0, i;
  3340. int copied;
  3341. struct drm_property_enum *prop_enum;
  3342. struct drm_mode_property_enum __user *enum_ptr;
  3343. struct drm_property_blob *prop_blob;
  3344. uint32_t __user *blob_id_ptr;
  3345. uint64_t __user *values_ptr;
  3346. uint32_t __user *blob_length_ptr;
  3347. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  3348. return -EINVAL;
  3349. drm_modeset_lock_all(dev);
  3350. property = drm_property_find(dev, out_resp->prop_id);
  3351. if (!property) {
  3352. ret = -ENOENT;
  3353. goto done;
  3354. }
  3355. if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
  3356. drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
  3357. list_for_each_entry(prop_enum, &property->enum_blob_list, head)
  3358. enum_count++;
  3359. } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
  3360. list_for_each_entry(prop_blob, &property->enum_blob_list, head)
  3361. blob_count++;
  3362. }
  3363. value_count = property->num_values;
  3364. strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
  3365. out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
  3366. out_resp->flags = property->flags;
  3367. if ((out_resp->count_values >= value_count) && value_count) {
  3368. values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
  3369. for (i = 0; i < value_count; i++) {
  3370. if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
  3371. ret = -EFAULT;
  3372. goto done;
  3373. }
  3374. }
  3375. }
  3376. out_resp->count_values = value_count;
  3377. if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
  3378. drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
  3379. if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
  3380. copied = 0;
  3381. enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
  3382. list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
  3383. if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
  3384. ret = -EFAULT;
  3385. goto done;
  3386. }
  3387. if (copy_to_user(&enum_ptr[copied].name,
  3388. &prop_enum->name, DRM_PROP_NAME_LEN)) {
  3389. ret = -EFAULT;
  3390. goto done;
  3391. }
  3392. copied++;
  3393. }
  3394. }
  3395. out_resp->count_enum_blobs = enum_count;
  3396. }
  3397. if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
  3398. if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
  3399. copied = 0;
  3400. blob_id_ptr = (uint32_t __user *)(unsigned long)out_resp->enum_blob_ptr;
  3401. blob_length_ptr = (uint32_t __user *)(unsigned long)out_resp->values_ptr;
  3402. list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
  3403. if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
  3404. ret = -EFAULT;
  3405. goto done;
  3406. }
  3407. if (put_user(prop_blob->length, blob_length_ptr + copied)) {
  3408. ret = -EFAULT;
  3409. goto done;
  3410. }
  3411. copied++;
  3412. }
  3413. }
  3414. out_resp->count_enum_blobs = blob_count;
  3415. }
  3416. done:
  3417. drm_modeset_unlock_all(dev);
  3418. return ret;
  3419. }
  3420. static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
  3421. void *data)
  3422. {
  3423. struct drm_property_blob *blob;
  3424. int ret;
  3425. if (!length || !data)
  3426. return NULL;
  3427. blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
  3428. if (!blob)
  3429. return NULL;
  3430. ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
  3431. if (ret) {
  3432. kfree(blob);
  3433. return NULL;
  3434. }
  3435. blob->length = length;
  3436. memcpy(blob->data, data, length);
  3437. list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
  3438. return blob;
  3439. }
  3440. static void drm_property_destroy_blob(struct drm_device *dev,
  3441. struct drm_property_blob *blob)
  3442. {
  3443. drm_mode_object_put(dev, &blob->base);
  3444. list_del(&blob->head);
  3445. kfree(blob);
  3446. }
  3447. /**
  3448. * drm_mode_getblob_ioctl - get the contents of a blob property value
  3449. * @dev: DRM device
  3450. * @data: ioctl data
  3451. * @file_priv: DRM file info
  3452. *
  3453. * This function retrieves the contents of a blob property. The value stored in
  3454. * an object's blob property is just a normal modeset object id.
  3455. *
  3456. * Called by the user via ioctl.
  3457. *
  3458. * Returns:
  3459. * Zero on success, errno on failure.
  3460. */
  3461. int drm_mode_getblob_ioctl(struct drm_device *dev,
  3462. void *data, struct drm_file *file_priv)
  3463. {
  3464. struct drm_mode_get_blob *out_resp = data;
  3465. struct drm_property_blob *blob;
  3466. int ret = 0;
  3467. void __user *blob_ptr;
  3468. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  3469. return -EINVAL;
  3470. drm_modeset_lock_all(dev);
  3471. blob = drm_property_blob_find(dev, out_resp->blob_id);
  3472. if (!blob) {
  3473. ret = -ENOENT;
  3474. goto done;
  3475. }
  3476. if (out_resp->length == blob->length) {
  3477. blob_ptr = (void __user *)(unsigned long)out_resp->data;
  3478. if (copy_to_user(blob_ptr, blob->data, blob->length)){
  3479. ret = -EFAULT;
  3480. goto done;
  3481. }
  3482. }
  3483. out_resp->length = blob->length;
  3484. done:
  3485. drm_modeset_unlock_all(dev);
  3486. return ret;
  3487. }
  3488. int drm_mode_connector_set_path_property(struct drm_connector *connector,
  3489. char *path)
  3490. {
  3491. struct drm_device *dev = connector->dev;
  3492. int ret, size;
  3493. size = strlen(path) + 1;
  3494. connector->path_blob_ptr = drm_property_create_blob(connector->dev,
  3495. size, path);
  3496. if (!connector->path_blob_ptr)
  3497. return -EINVAL;
  3498. ret = drm_object_property_set_value(&connector->base,
  3499. dev->mode_config.path_property,
  3500. connector->path_blob_ptr->base.id);
  3501. return ret;
  3502. }
  3503. EXPORT_SYMBOL(drm_mode_connector_set_path_property);
  3504. /**
  3505. * drm_mode_connector_update_edid_property - update the edid property of a connector
  3506. * @connector: drm connector
  3507. * @edid: new value of the edid property
  3508. *
  3509. * This function creates a new blob modeset object and assigns its id to the
  3510. * connector's edid property.
  3511. *
  3512. * Returns:
  3513. * Zero on success, errno on failure.
  3514. */
  3515. int drm_mode_connector_update_edid_property(struct drm_connector *connector,
  3516. struct edid *edid)
  3517. {
  3518. struct drm_device *dev = connector->dev;
  3519. int ret, size;
  3520. /* ignore requests to set edid when overridden */
  3521. if (connector->override_edid)
  3522. return 0;
  3523. if (connector->edid_blob_ptr)
  3524. drm_property_destroy_blob(dev, connector->edid_blob_ptr);
  3525. /* Delete edid, when there is none. */
  3526. if (!edid) {
  3527. connector->edid_blob_ptr = NULL;
  3528. ret = drm_object_property_set_value(&connector->base, dev->mode_config.edid_property, 0);
  3529. return ret;
  3530. }
  3531. size = EDID_LENGTH * (1 + edid->extensions);
  3532. connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
  3533. size, edid);
  3534. if (!connector->edid_blob_ptr)
  3535. return -EINVAL;
  3536. ret = drm_object_property_set_value(&connector->base,
  3537. dev->mode_config.edid_property,
  3538. connector->edid_blob_ptr->base.id);
  3539. return ret;
  3540. }
  3541. EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
  3542. static bool drm_property_change_is_valid(struct drm_property *property,
  3543. uint64_t value)
  3544. {
  3545. if (property->flags & DRM_MODE_PROP_IMMUTABLE)
  3546. return false;
  3547. if (drm_property_type_is(property, DRM_MODE_PROP_RANGE)) {
  3548. if (value < property->values[0] || value > property->values[1])
  3549. return false;
  3550. return true;
  3551. } else if (drm_property_type_is(property, DRM_MODE_PROP_SIGNED_RANGE)) {
  3552. int64_t svalue = U642I64(value);
  3553. if (svalue < U642I64(property->values[0]) ||
  3554. svalue > U642I64(property->values[1]))
  3555. return false;
  3556. return true;
  3557. } else if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
  3558. int i;
  3559. uint64_t valid_mask = 0;
  3560. for (i = 0; i < property->num_values; i++)
  3561. valid_mask |= (1ULL << property->values[i]);
  3562. return !(value & ~valid_mask);
  3563. } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
  3564. /* Only the driver knows */
  3565. return true;
  3566. } else if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
  3567. struct drm_mode_object *obj;
  3568. /* a zero value for an object property translates to null: */
  3569. if (value == 0)
  3570. return true;
  3571. /*
  3572. * NOTE: use _object_find() directly to bypass restriction on
  3573. * looking up refcnt'd objects (ie. fb's). For a refcnt'd
  3574. * object this could race against object finalization, so it
  3575. * simply tells us that the object *was* valid. Which is good
  3576. * enough.
  3577. */
  3578. obj = _object_find(property->dev, value, property->values[0]);
  3579. return obj != NULL;
  3580. } else {
  3581. int i;
  3582. for (i = 0; i < property->num_values; i++)
  3583. if (property->values[i] == value)
  3584. return true;
  3585. return false;
  3586. }
  3587. }
  3588. /**
  3589. * drm_mode_connector_property_set_ioctl - set the current value of a connector property
  3590. * @dev: DRM device
  3591. * @data: ioctl data
  3592. * @file_priv: DRM file info
  3593. *
  3594. * This function sets the current value for a connectors's property. It also
  3595. * calls into a driver's ->set_property callback to update the hardware state
  3596. *
  3597. * Called by the user via ioctl.
  3598. *
  3599. * Returns:
  3600. * Zero on success, errno on failure.
  3601. */
  3602. int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
  3603. void *data, struct drm_file *file_priv)
  3604. {
  3605. struct drm_mode_connector_set_property *conn_set_prop = data;
  3606. struct drm_mode_obj_set_property obj_set_prop = {
  3607. .value = conn_set_prop->value,
  3608. .prop_id = conn_set_prop->prop_id,
  3609. .obj_id = conn_set_prop->connector_id,
  3610. .obj_type = DRM_MODE_OBJECT_CONNECTOR
  3611. };
  3612. /* It does all the locking and checking we need */
  3613. return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
  3614. }
  3615. static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
  3616. struct drm_property *property,
  3617. uint64_t value)
  3618. {
  3619. int ret = -EINVAL;
  3620. struct drm_connector *connector = obj_to_connector(obj);
  3621. /* Do DPMS ourselves */
  3622. if (property == connector->dev->mode_config.dpms_property) {
  3623. if (connector->funcs->dpms)
  3624. (*connector->funcs->dpms)(connector, (int)value);
  3625. ret = 0;
  3626. } else if (connector->funcs->set_property)
  3627. ret = connector->funcs->set_property(connector, property, value);
  3628. /* store the property value if successful */
  3629. if (!ret)
  3630. drm_object_property_set_value(&connector->base, property, value);
  3631. return ret;
  3632. }
  3633. static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
  3634. struct drm_property *property,
  3635. uint64_t value)
  3636. {
  3637. int ret = -EINVAL;
  3638. struct drm_crtc *crtc = obj_to_crtc(obj);
  3639. if (crtc->funcs->set_property)
  3640. ret = crtc->funcs->set_property(crtc, property, value);
  3641. if (!ret)
  3642. drm_object_property_set_value(obj, property, value);
  3643. return ret;
  3644. }
  3645. /**
  3646. * drm_mode_plane_set_obj_prop - set the value of a property
  3647. * @plane: drm plane object to set property value for
  3648. * @property: property to set
  3649. * @value: value the property should be set to
  3650. *
  3651. * This functions sets a given property on a given plane object. This function
  3652. * calls the driver's ->set_property callback and changes the software state of
  3653. * the property if the callback succeeds.
  3654. *
  3655. * Returns:
  3656. * Zero on success, error code on failure.
  3657. */
  3658. int drm_mode_plane_set_obj_prop(struct drm_plane *plane,
  3659. struct drm_property *property,
  3660. uint64_t value)
  3661. {
  3662. int ret = -EINVAL;
  3663. struct drm_mode_object *obj = &plane->base;
  3664. if (plane->funcs->set_property)
  3665. ret = plane->funcs->set_property(plane, property, value);
  3666. if (!ret)
  3667. drm_object_property_set_value(obj, property, value);
  3668. return ret;
  3669. }
  3670. EXPORT_SYMBOL(drm_mode_plane_set_obj_prop);
  3671. /**
  3672. * drm_mode_getproperty_ioctl - get the current value of a object's property
  3673. * @dev: DRM device
  3674. * @data: ioctl data
  3675. * @file_priv: DRM file info
  3676. *
  3677. * This function retrieves the current value for an object's property. Compared
  3678. * to the connector specific ioctl this one is extended to also work on crtc and
  3679. * plane objects.
  3680. *
  3681. * Called by the user via ioctl.
  3682. *
  3683. * Returns:
  3684. * Zero on success, errno on failure.
  3685. */
  3686. int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
  3687. struct drm_file *file_priv)
  3688. {
  3689. struct drm_mode_obj_get_properties *arg = data;
  3690. struct drm_mode_object *obj;
  3691. int ret = 0;
  3692. int i;
  3693. int copied = 0;
  3694. int props_count = 0;
  3695. uint32_t __user *props_ptr;
  3696. uint64_t __user *prop_values_ptr;
  3697. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  3698. return -EINVAL;
  3699. drm_modeset_lock_all(dev);
  3700. obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
  3701. if (!obj) {
  3702. ret = -ENOENT;
  3703. goto out;
  3704. }
  3705. if (!obj->properties) {
  3706. ret = -EINVAL;
  3707. goto out;
  3708. }
  3709. props_count = obj->properties->count;
  3710. /* This ioctl is called twice, once to determine how much space is
  3711. * needed, and the 2nd time to fill it. */
  3712. if ((arg->count_props >= props_count) && props_count) {
  3713. copied = 0;
  3714. props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
  3715. prop_values_ptr = (uint64_t __user *)(unsigned long)
  3716. (arg->prop_values_ptr);
  3717. for (i = 0; i < props_count; i++) {
  3718. if (put_user(obj->properties->ids[i],
  3719. props_ptr + copied)) {
  3720. ret = -EFAULT;
  3721. goto out;
  3722. }
  3723. if (put_user(obj->properties->values[i],
  3724. prop_values_ptr + copied)) {
  3725. ret = -EFAULT;
  3726. goto out;
  3727. }
  3728. copied++;
  3729. }
  3730. }
  3731. arg->count_props = props_count;
  3732. out:
  3733. drm_modeset_unlock_all(dev);
  3734. return ret;
  3735. }
  3736. /**
  3737. * drm_mode_obj_set_property_ioctl - set the current value of an object's property
  3738. * @dev: DRM device
  3739. * @data: ioctl data
  3740. * @file_priv: DRM file info
  3741. *
  3742. * This function sets the current value for an object's property. It also calls
  3743. * into a driver's ->set_property callback to update the hardware state.
  3744. * Compared to the connector specific ioctl this one is extended to also work on
  3745. * crtc and plane objects.
  3746. *
  3747. * Called by the user via ioctl.
  3748. *
  3749. * Returns:
  3750. * Zero on success, errno on failure.
  3751. */
  3752. int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
  3753. struct drm_file *file_priv)
  3754. {
  3755. struct drm_mode_obj_set_property *arg = data;
  3756. struct drm_mode_object *arg_obj;
  3757. struct drm_mode_object *prop_obj;
  3758. struct drm_property *property;
  3759. int ret = -EINVAL;
  3760. int i;
  3761. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  3762. return -EINVAL;
  3763. drm_modeset_lock_all(dev);
  3764. arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
  3765. if (!arg_obj) {
  3766. ret = -ENOENT;
  3767. goto out;
  3768. }
  3769. if (!arg_obj->properties)
  3770. goto out;
  3771. for (i = 0; i < arg_obj->properties->count; i++)
  3772. if (arg_obj->properties->ids[i] == arg->prop_id)
  3773. break;
  3774. if (i == arg_obj->properties->count)
  3775. goto out;
  3776. prop_obj = drm_mode_object_find(dev, arg->prop_id,
  3777. DRM_MODE_OBJECT_PROPERTY);
  3778. if (!prop_obj) {
  3779. ret = -ENOENT;
  3780. goto out;
  3781. }
  3782. property = obj_to_property(prop_obj);
  3783. if (!drm_property_change_is_valid(property, arg->value))
  3784. goto out;
  3785. switch (arg_obj->type) {
  3786. case DRM_MODE_OBJECT_CONNECTOR:
  3787. ret = drm_mode_connector_set_obj_prop(arg_obj, property,
  3788. arg->value);
  3789. break;
  3790. case DRM_MODE_OBJECT_CRTC:
  3791. ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
  3792. break;
  3793. case DRM_MODE_OBJECT_PLANE:
  3794. ret = drm_mode_plane_set_obj_prop(obj_to_plane(arg_obj),
  3795. property, arg->value);
  3796. break;
  3797. }
  3798. out:
  3799. drm_modeset_unlock_all(dev);
  3800. return ret;
  3801. }
  3802. /**
  3803. * drm_mode_connector_attach_encoder - attach a connector to an encoder
  3804. * @connector: connector to attach
  3805. * @encoder: encoder to attach @connector to
  3806. *
  3807. * This function links up a connector to an encoder. Note that the routing
  3808. * restrictions between encoders and crtcs are exposed to userspace through the
  3809. * possible_clones and possible_crtcs bitmasks.
  3810. *
  3811. * Returns:
  3812. * Zero on success, errno on failure.
  3813. */
  3814. int drm_mode_connector_attach_encoder(struct drm_connector *connector,
  3815. struct drm_encoder *encoder)
  3816. {
  3817. int i;
  3818. for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
  3819. if (connector->encoder_ids[i] == 0) {
  3820. connector->encoder_ids[i] = encoder->base.id;
  3821. return 0;
  3822. }
  3823. }
  3824. return -ENOMEM;
  3825. }
  3826. EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
  3827. /**
  3828. * drm_mode_crtc_set_gamma_size - set the gamma table size
  3829. * @crtc: CRTC to set the gamma table size for
  3830. * @gamma_size: size of the gamma table
  3831. *
  3832. * Drivers which support gamma tables should set this to the supported gamma
  3833. * table size when initializing the CRTC. Currently the drm core only supports a
  3834. * fixed gamma table size.
  3835. *
  3836. * Returns:
  3837. * Zero on success, errno on failure.
  3838. */
  3839. int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
  3840. int gamma_size)
  3841. {
  3842. crtc->gamma_size = gamma_size;
  3843. crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
  3844. if (!crtc->gamma_store) {
  3845. crtc->gamma_size = 0;
  3846. return -ENOMEM;
  3847. }
  3848. return 0;
  3849. }
  3850. EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
  3851. /**
  3852. * drm_mode_gamma_set_ioctl - set the gamma table
  3853. * @dev: DRM device
  3854. * @data: ioctl data
  3855. * @file_priv: DRM file info
  3856. *
  3857. * Set the gamma table of a CRTC to the one passed in by the user. Userspace can
  3858. * inquire the required gamma table size through drm_mode_gamma_get_ioctl.
  3859. *
  3860. * Called by the user via ioctl.
  3861. *
  3862. * Returns:
  3863. * Zero on success, errno on failure.
  3864. */
  3865. int drm_mode_gamma_set_ioctl(struct drm_device *dev,
  3866. void *data, struct drm_file *file_priv)
  3867. {
  3868. struct drm_mode_crtc_lut *crtc_lut = data;
  3869. struct drm_crtc *crtc;
  3870. void *r_base, *g_base, *b_base;
  3871. int size;
  3872. int ret = 0;
  3873. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  3874. return -EINVAL;
  3875. drm_modeset_lock_all(dev);
  3876. crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
  3877. if (!crtc) {
  3878. ret = -ENOENT;
  3879. goto out;
  3880. }
  3881. if (crtc->funcs->gamma_set == NULL) {
  3882. ret = -ENOSYS;
  3883. goto out;
  3884. }
  3885. /* memcpy into gamma store */
  3886. if (crtc_lut->gamma_size != crtc->gamma_size) {
  3887. ret = -EINVAL;
  3888. goto out;
  3889. }
  3890. size = crtc_lut->gamma_size * (sizeof(uint16_t));
  3891. r_base = crtc->gamma_store;
  3892. if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
  3893. ret = -EFAULT;
  3894. goto out;
  3895. }
  3896. g_base = r_base + size;
  3897. if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
  3898. ret = -EFAULT;
  3899. goto out;
  3900. }
  3901. b_base = g_base + size;
  3902. if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
  3903. ret = -EFAULT;
  3904. goto out;
  3905. }
  3906. crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
  3907. out:
  3908. drm_modeset_unlock_all(dev);
  3909. return ret;
  3910. }
  3911. /**
  3912. * drm_mode_gamma_get_ioctl - get the gamma table
  3913. * @dev: DRM device
  3914. * @data: ioctl data
  3915. * @file_priv: DRM file info
  3916. *
  3917. * Copy the current gamma table into the storage provided. This also provides
  3918. * the gamma table size the driver expects, which can be used to size the
  3919. * allocated storage.
  3920. *
  3921. * Called by the user via ioctl.
  3922. *
  3923. * Returns:
  3924. * Zero on success, errno on failure.
  3925. */
  3926. int drm_mode_gamma_get_ioctl(struct drm_device *dev,
  3927. void *data, struct drm_file *file_priv)
  3928. {
  3929. struct drm_mode_crtc_lut *crtc_lut = data;
  3930. struct drm_crtc *crtc;
  3931. void *r_base, *g_base, *b_base;
  3932. int size;
  3933. int ret = 0;
  3934. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  3935. return -EINVAL;
  3936. drm_modeset_lock_all(dev);
  3937. crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
  3938. if (!crtc) {
  3939. ret = -ENOENT;
  3940. goto out;
  3941. }
  3942. /* memcpy into gamma store */
  3943. if (crtc_lut->gamma_size != crtc->gamma_size) {
  3944. ret = -EINVAL;
  3945. goto out;
  3946. }
  3947. size = crtc_lut->gamma_size * (sizeof(uint16_t));
  3948. r_base = crtc->gamma_store;
  3949. if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
  3950. ret = -EFAULT;
  3951. goto out;
  3952. }
  3953. g_base = r_base + size;
  3954. if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
  3955. ret = -EFAULT;
  3956. goto out;
  3957. }
  3958. b_base = g_base + size;
  3959. if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
  3960. ret = -EFAULT;
  3961. goto out;
  3962. }
  3963. out:
  3964. drm_modeset_unlock_all(dev);
  3965. return ret;
  3966. }
  3967. /**
  3968. * drm_mode_page_flip_ioctl - schedule an asynchronous fb update
  3969. * @dev: DRM device
  3970. * @data: ioctl data
  3971. * @file_priv: DRM file info
  3972. *
  3973. * This schedules an asynchronous update on a given CRTC, called page flip.
  3974. * Optionally a drm event is generated to signal the completion of the event.
  3975. * Generic drivers cannot assume that a pageflip with changed framebuffer
  3976. * properties (including driver specific metadata like tiling layout) will work,
  3977. * but some drivers support e.g. pixel format changes through the pageflip
  3978. * ioctl.
  3979. *
  3980. * Called by the user via ioctl.
  3981. *
  3982. * Returns:
  3983. * Zero on success, errno on failure.
  3984. */
  3985. int drm_mode_page_flip_ioctl(struct drm_device *dev,
  3986. void *data, struct drm_file *file_priv)
  3987. {
  3988. struct drm_mode_crtc_page_flip *page_flip = data;
  3989. struct drm_crtc *crtc;
  3990. struct drm_framebuffer *fb = NULL;
  3991. struct drm_pending_vblank_event *e = NULL;
  3992. unsigned long flags;
  3993. int ret = -EINVAL;
  3994. if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
  3995. page_flip->reserved != 0)
  3996. return -EINVAL;
  3997. if ((page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC) && !dev->mode_config.async_page_flip)
  3998. return -EINVAL;
  3999. crtc = drm_crtc_find(dev, page_flip->crtc_id);
  4000. if (!crtc)
  4001. return -ENOENT;
  4002. drm_modeset_lock_crtc(crtc);
  4003. if (crtc->primary->fb == NULL) {
  4004. /* The framebuffer is currently unbound, presumably
  4005. * due to a hotplug event, that userspace has not
  4006. * yet discovered.
  4007. */
  4008. ret = -EBUSY;
  4009. goto out;
  4010. }
  4011. if (crtc->funcs->page_flip == NULL)
  4012. goto out;
  4013. fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
  4014. if (!fb) {
  4015. ret = -ENOENT;
  4016. goto out;
  4017. }
  4018. ret = drm_crtc_check_viewport(crtc, crtc->x, crtc->y, &crtc->mode, fb);
  4019. if (ret)
  4020. goto out;
  4021. if (crtc->primary->fb->pixel_format != fb->pixel_format) {
  4022. DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
  4023. ret = -EINVAL;
  4024. goto out;
  4025. }
  4026. if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
  4027. ret = -ENOMEM;
  4028. spin_lock_irqsave(&dev->event_lock, flags);
  4029. if (file_priv->event_space < sizeof e->event) {
  4030. spin_unlock_irqrestore(&dev->event_lock, flags);
  4031. goto out;
  4032. }
  4033. file_priv->event_space -= sizeof e->event;
  4034. spin_unlock_irqrestore(&dev->event_lock, flags);
  4035. e = kzalloc(sizeof *e, GFP_KERNEL);
  4036. if (e == NULL) {
  4037. spin_lock_irqsave(&dev->event_lock, flags);
  4038. file_priv->event_space += sizeof e->event;
  4039. spin_unlock_irqrestore(&dev->event_lock, flags);
  4040. goto out;
  4041. }
  4042. e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
  4043. e->event.base.length = sizeof e->event;
  4044. e->event.user_data = page_flip->user_data;
  4045. e->base.event = &e->event.base;
  4046. e->base.file_priv = file_priv;
  4047. e->base.destroy =
  4048. (void (*) (struct drm_pending_event *)) kfree;
  4049. }
  4050. crtc->primary->old_fb = crtc->primary->fb;
  4051. ret = crtc->funcs->page_flip(crtc, fb, e, page_flip->flags);
  4052. if (ret) {
  4053. if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
  4054. spin_lock_irqsave(&dev->event_lock, flags);
  4055. file_priv->event_space += sizeof e->event;
  4056. spin_unlock_irqrestore(&dev->event_lock, flags);
  4057. kfree(e);
  4058. }
  4059. /* Keep the old fb, don't unref it. */
  4060. crtc->primary->old_fb = NULL;
  4061. } else {
  4062. /*
  4063. * Warn if the driver hasn't properly updated the crtc->fb
  4064. * field to reflect that the new framebuffer is now used.
  4065. * Failing to do so will screw with the reference counting
  4066. * on framebuffers.
  4067. */
  4068. WARN_ON(crtc->primary->fb != fb);
  4069. /* Unref only the old framebuffer. */
  4070. fb = NULL;
  4071. }
  4072. out:
  4073. if (fb)
  4074. drm_framebuffer_unreference(fb);
  4075. if (crtc->primary->old_fb)
  4076. drm_framebuffer_unreference(crtc->primary->old_fb);
  4077. crtc->primary->old_fb = NULL;
  4078. drm_modeset_unlock_crtc(crtc);
  4079. return ret;
  4080. }
  4081. /**
  4082. * drm_mode_config_reset - call ->reset callbacks
  4083. * @dev: drm device
  4084. *
  4085. * This functions calls all the crtc's, encoder's and connector's ->reset
  4086. * callback. Drivers can use this in e.g. their driver load or resume code to
  4087. * reset hardware and software state.
  4088. */
  4089. void drm_mode_config_reset(struct drm_device *dev)
  4090. {
  4091. struct drm_crtc *crtc;
  4092. struct drm_plane *plane;
  4093. struct drm_encoder *encoder;
  4094. struct drm_connector *connector;
  4095. list_for_each_entry(plane, &dev->mode_config.plane_list, head)
  4096. if (plane->funcs->reset)
  4097. plane->funcs->reset(plane);
  4098. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
  4099. if (crtc->funcs->reset)
  4100. crtc->funcs->reset(crtc);
  4101. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
  4102. if (encoder->funcs->reset)
  4103. encoder->funcs->reset(encoder);
  4104. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  4105. connector->status = connector_status_unknown;
  4106. if (connector->funcs->reset)
  4107. connector->funcs->reset(connector);
  4108. }
  4109. }
  4110. EXPORT_SYMBOL(drm_mode_config_reset);
  4111. /**
  4112. * drm_mode_create_dumb_ioctl - create a dumb backing storage buffer
  4113. * @dev: DRM device
  4114. * @data: ioctl data
  4115. * @file_priv: DRM file info
  4116. *
  4117. * This creates a new dumb buffer in the driver's backing storage manager (GEM,
  4118. * TTM or something else entirely) and returns the resulting buffer handle. This
  4119. * handle can then be wrapped up into a framebuffer modeset object.
  4120. *
  4121. * Note that userspace is not allowed to use such objects for render
  4122. * acceleration - drivers must create their own private ioctls for such a use
  4123. * case.
  4124. *
  4125. * Called by the user via ioctl.
  4126. *
  4127. * Returns:
  4128. * Zero on success, errno on failure.
  4129. */
  4130. int drm_mode_create_dumb_ioctl(struct drm_device *dev,
  4131. void *data, struct drm_file *file_priv)
  4132. {
  4133. struct drm_mode_create_dumb *args = data;
  4134. u32 cpp, stride, size;
  4135. if (!dev->driver->dumb_create)
  4136. return -ENOSYS;
  4137. if (!args->width || !args->height || !args->bpp)
  4138. return -EINVAL;
  4139. /* overflow checks for 32bit size calculations */
  4140. /* NOTE: DIV_ROUND_UP() can overflow */
  4141. cpp = DIV_ROUND_UP(args->bpp, 8);
  4142. if (!cpp || cpp > 0xffffffffU / args->width)
  4143. return -EINVAL;
  4144. stride = cpp * args->width;
  4145. if (args->height > 0xffffffffU / stride)
  4146. return -EINVAL;
  4147. /* test for wrap-around */
  4148. size = args->height * stride;
  4149. if (PAGE_ALIGN(size) == 0)
  4150. return -EINVAL;
  4151. return dev->driver->dumb_create(file_priv, dev, args);
  4152. }
  4153. /**
  4154. * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer
  4155. * @dev: DRM device
  4156. * @data: ioctl data
  4157. * @file_priv: DRM file info
  4158. *
  4159. * Allocate an offset in the drm device node's address space to be able to
  4160. * memory map a dumb buffer.
  4161. *
  4162. * Called by the user via ioctl.
  4163. *
  4164. * Returns:
  4165. * Zero on success, errno on failure.
  4166. */
  4167. int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
  4168. void *data, struct drm_file *file_priv)
  4169. {
  4170. struct drm_mode_map_dumb *args = data;
  4171. /* call driver ioctl to get mmap offset */
  4172. if (!dev->driver->dumb_map_offset)
  4173. return -ENOSYS;
  4174. return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
  4175. }
  4176. /**
  4177. * drm_mode_destroy_dumb_ioctl - destroy a dumb backing strage buffer
  4178. * @dev: DRM device
  4179. * @data: ioctl data
  4180. * @file_priv: DRM file info
  4181. *
  4182. * This destroys the userspace handle for the given dumb backing storage buffer.
  4183. * Since buffer objects must be reference counted in the kernel a buffer object
  4184. * won't be immediately freed if a framebuffer modeset object still uses it.
  4185. *
  4186. * Called by the user via ioctl.
  4187. *
  4188. * Returns:
  4189. * Zero on success, errno on failure.
  4190. */
  4191. int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
  4192. void *data, struct drm_file *file_priv)
  4193. {
  4194. struct drm_mode_destroy_dumb *args = data;
  4195. if (!dev->driver->dumb_destroy)
  4196. return -ENOSYS;
  4197. return dev->driver->dumb_destroy(file_priv, dev, args->handle);
  4198. }
  4199. /**
  4200. * drm_fb_get_bpp_depth - get the bpp/depth values for format
  4201. * @format: pixel format (DRM_FORMAT_*)
  4202. * @depth: storage for the depth value
  4203. * @bpp: storage for the bpp value
  4204. *
  4205. * This only supports RGB formats here for compat with code that doesn't use
  4206. * pixel formats directly yet.
  4207. */
  4208. void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
  4209. int *bpp)
  4210. {
  4211. switch (format) {
  4212. case DRM_FORMAT_C8:
  4213. case DRM_FORMAT_RGB332:
  4214. case DRM_FORMAT_BGR233:
  4215. *depth = 8;
  4216. *bpp = 8;
  4217. break;
  4218. case DRM_FORMAT_XRGB1555:
  4219. case DRM_FORMAT_XBGR1555:
  4220. case DRM_FORMAT_RGBX5551:
  4221. case DRM_FORMAT_BGRX5551:
  4222. case DRM_FORMAT_ARGB1555:
  4223. case DRM_FORMAT_ABGR1555:
  4224. case DRM_FORMAT_RGBA5551:
  4225. case DRM_FORMAT_BGRA5551:
  4226. *depth = 15;
  4227. *bpp = 16;
  4228. break;
  4229. case DRM_FORMAT_RGB565:
  4230. case DRM_FORMAT_BGR565:
  4231. *depth = 16;
  4232. *bpp = 16;
  4233. break;
  4234. case DRM_FORMAT_RGB888:
  4235. case DRM_FORMAT_BGR888:
  4236. *depth = 24;
  4237. *bpp = 24;
  4238. break;
  4239. case DRM_FORMAT_XRGB8888:
  4240. case DRM_FORMAT_XBGR8888:
  4241. case DRM_FORMAT_RGBX8888:
  4242. case DRM_FORMAT_BGRX8888:
  4243. *depth = 24;
  4244. *bpp = 32;
  4245. break;
  4246. case DRM_FORMAT_XRGB2101010:
  4247. case DRM_FORMAT_XBGR2101010:
  4248. case DRM_FORMAT_RGBX1010102:
  4249. case DRM_FORMAT_BGRX1010102:
  4250. case DRM_FORMAT_ARGB2101010:
  4251. case DRM_FORMAT_ABGR2101010:
  4252. case DRM_FORMAT_RGBA1010102:
  4253. case DRM_FORMAT_BGRA1010102:
  4254. *depth = 30;
  4255. *bpp = 32;
  4256. break;
  4257. case DRM_FORMAT_ARGB8888:
  4258. case DRM_FORMAT_ABGR8888:
  4259. case DRM_FORMAT_RGBA8888:
  4260. case DRM_FORMAT_BGRA8888:
  4261. *depth = 32;
  4262. *bpp = 32;
  4263. break;
  4264. default:
  4265. DRM_DEBUG_KMS("unsupported pixel format %s\n",
  4266. drm_get_format_name(format));
  4267. *depth = 0;
  4268. *bpp = 0;
  4269. break;
  4270. }
  4271. }
  4272. EXPORT_SYMBOL(drm_fb_get_bpp_depth);
  4273. /**
  4274. * drm_format_num_planes - get the number of planes for format
  4275. * @format: pixel format (DRM_FORMAT_*)
  4276. *
  4277. * Returns:
  4278. * The number of planes used by the specified pixel format.
  4279. */
  4280. int drm_format_num_planes(uint32_t format)
  4281. {
  4282. switch (format) {
  4283. case DRM_FORMAT_YUV410:
  4284. case DRM_FORMAT_YVU410:
  4285. case DRM_FORMAT_YUV411:
  4286. case DRM_FORMAT_YVU411:
  4287. case DRM_FORMAT_YUV420:
  4288. case DRM_FORMAT_YVU420:
  4289. case DRM_FORMAT_YUV422:
  4290. case DRM_FORMAT_YVU422:
  4291. case DRM_FORMAT_YUV444:
  4292. case DRM_FORMAT_YVU444:
  4293. return 3;
  4294. case DRM_FORMAT_NV12:
  4295. case DRM_FORMAT_NV21:
  4296. case DRM_FORMAT_NV16:
  4297. case DRM_FORMAT_NV61:
  4298. case DRM_FORMAT_NV24:
  4299. case DRM_FORMAT_NV42:
  4300. return 2;
  4301. default:
  4302. return 1;
  4303. }
  4304. }
  4305. EXPORT_SYMBOL(drm_format_num_planes);
  4306. /**
  4307. * drm_format_plane_cpp - determine the bytes per pixel value
  4308. * @format: pixel format (DRM_FORMAT_*)
  4309. * @plane: plane index
  4310. *
  4311. * Returns:
  4312. * The bytes per pixel value for the specified plane.
  4313. */
  4314. int drm_format_plane_cpp(uint32_t format, int plane)
  4315. {
  4316. unsigned int depth;
  4317. int bpp;
  4318. if (plane >= drm_format_num_planes(format))
  4319. return 0;
  4320. switch (format) {
  4321. case DRM_FORMAT_YUYV:
  4322. case DRM_FORMAT_YVYU:
  4323. case DRM_FORMAT_UYVY:
  4324. case DRM_FORMAT_VYUY:
  4325. return 2;
  4326. case DRM_FORMAT_NV12:
  4327. case DRM_FORMAT_NV21:
  4328. case DRM_FORMAT_NV16:
  4329. case DRM_FORMAT_NV61:
  4330. case DRM_FORMAT_NV24:
  4331. case DRM_FORMAT_NV42:
  4332. return plane ? 2 : 1;
  4333. case DRM_FORMAT_YUV410:
  4334. case DRM_FORMAT_YVU410:
  4335. case DRM_FORMAT_YUV411:
  4336. case DRM_FORMAT_YVU411:
  4337. case DRM_FORMAT_YUV420:
  4338. case DRM_FORMAT_YVU420:
  4339. case DRM_FORMAT_YUV422:
  4340. case DRM_FORMAT_YVU422:
  4341. case DRM_FORMAT_YUV444:
  4342. case DRM_FORMAT_YVU444:
  4343. return 1;
  4344. default:
  4345. drm_fb_get_bpp_depth(format, &depth, &bpp);
  4346. return bpp >> 3;
  4347. }
  4348. }
  4349. EXPORT_SYMBOL(drm_format_plane_cpp);
  4350. /**
  4351. * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
  4352. * @format: pixel format (DRM_FORMAT_*)
  4353. *
  4354. * Returns:
  4355. * The horizontal chroma subsampling factor for the
  4356. * specified pixel format.
  4357. */
  4358. int drm_format_horz_chroma_subsampling(uint32_t format)
  4359. {
  4360. switch (format) {
  4361. case DRM_FORMAT_YUV411:
  4362. case DRM_FORMAT_YVU411:
  4363. case DRM_FORMAT_YUV410:
  4364. case DRM_FORMAT_YVU410:
  4365. return 4;
  4366. case DRM_FORMAT_YUYV:
  4367. case DRM_FORMAT_YVYU:
  4368. case DRM_FORMAT_UYVY:
  4369. case DRM_FORMAT_VYUY:
  4370. case DRM_FORMAT_NV12:
  4371. case DRM_FORMAT_NV21:
  4372. case DRM_FORMAT_NV16:
  4373. case DRM_FORMAT_NV61:
  4374. case DRM_FORMAT_YUV422:
  4375. case DRM_FORMAT_YVU422:
  4376. case DRM_FORMAT_YUV420:
  4377. case DRM_FORMAT_YVU420:
  4378. return 2;
  4379. default:
  4380. return 1;
  4381. }
  4382. }
  4383. EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
  4384. /**
  4385. * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
  4386. * @format: pixel format (DRM_FORMAT_*)
  4387. *
  4388. * Returns:
  4389. * The vertical chroma subsampling factor for the
  4390. * specified pixel format.
  4391. */
  4392. int drm_format_vert_chroma_subsampling(uint32_t format)
  4393. {
  4394. switch (format) {
  4395. case DRM_FORMAT_YUV410:
  4396. case DRM_FORMAT_YVU410:
  4397. return 4;
  4398. case DRM_FORMAT_YUV420:
  4399. case DRM_FORMAT_YVU420:
  4400. case DRM_FORMAT_NV12:
  4401. case DRM_FORMAT_NV21:
  4402. return 2;
  4403. default:
  4404. return 1;
  4405. }
  4406. }
  4407. EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);
  4408. /**
  4409. * drm_rotation_simplify() - Try to simplify the rotation
  4410. * @rotation: Rotation to be simplified
  4411. * @supported_rotations: Supported rotations
  4412. *
  4413. * Attempt to simplify the rotation to a form that is supported.
  4414. * Eg. if the hardware supports everything except DRM_REFLECT_X
  4415. * one could call this function like this:
  4416. *
  4417. * drm_rotation_simplify(rotation, BIT(DRM_ROTATE_0) |
  4418. * BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_180) |
  4419. * BIT(DRM_ROTATE_270) | BIT(DRM_REFLECT_Y));
  4420. *
  4421. * to eliminate the DRM_ROTATE_X flag. Depending on what kind of
  4422. * transforms the hardware supports, this function may not
  4423. * be able to produce a supported transform, so the caller should
  4424. * check the result afterwards.
  4425. */
  4426. unsigned int drm_rotation_simplify(unsigned int rotation,
  4427. unsigned int supported_rotations)
  4428. {
  4429. if (rotation & ~supported_rotations) {
  4430. rotation ^= BIT(DRM_REFLECT_X) | BIT(DRM_REFLECT_Y);
  4431. rotation = (rotation & ~0xf) | BIT((ffs(rotation & 0xf) + 1) % 4);
  4432. }
  4433. return rotation;
  4434. }
  4435. EXPORT_SYMBOL(drm_rotation_simplify);
  4436. /**
  4437. * drm_mode_config_init - initialize DRM mode_configuration structure
  4438. * @dev: DRM device
  4439. *
  4440. * Initialize @dev's mode_config structure, used for tracking the graphics
  4441. * configuration of @dev.
  4442. *
  4443. * Since this initializes the modeset locks, no locking is possible. Which is no
  4444. * problem, since this should happen single threaded at init time. It is the
  4445. * driver's problem to ensure this guarantee.
  4446. *
  4447. */
  4448. void drm_mode_config_init(struct drm_device *dev)
  4449. {
  4450. mutex_init(&dev->mode_config.mutex);
  4451. drm_modeset_lock_init(&dev->mode_config.connection_mutex);
  4452. mutex_init(&dev->mode_config.idr_mutex);
  4453. mutex_init(&dev->mode_config.fb_lock);
  4454. INIT_LIST_HEAD(&dev->mode_config.fb_list);
  4455. INIT_LIST_HEAD(&dev->mode_config.crtc_list);
  4456. INIT_LIST_HEAD(&dev->mode_config.connector_list);
  4457. INIT_LIST_HEAD(&dev->mode_config.bridge_list);
  4458. INIT_LIST_HEAD(&dev->mode_config.encoder_list);
  4459. INIT_LIST_HEAD(&dev->mode_config.property_list);
  4460. INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
  4461. INIT_LIST_HEAD(&dev->mode_config.plane_list);
  4462. idr_init(&dev->mode_config.crtc_idr);
  4463. drm_modeset_lock_all(dev);
  4464. drm_mode_create_standard_connector_properties(dev);
  4465. drm_mode_create_standard_plane_properties(dev);
  4466. drm_modeset_unlock_all(dev);
  4467. /* Just to be sure */
  4468. dev->mode_config.num_fb = 0;
  4469. dev->mode_config.num_connector = 0;
  4470. dev->mode_config.num_crtc = 0;
  4471. dev->mode_config.num_encoder = 0;
  4472. dev->mode_config.num_overlay_plane = 0;
  4473. dev->mode_config.num_total_plane = 0;
  4474. }
  4475. EXPORT_SYMBOL(drm_mode_config_init);
  4476. /**
  4477. * drm_mode_config_cleanup - free up DRM mode_config info
  4478. * @dev: DRM device
  4479. *
  4480. * Free up all the connectors and CRTCs associated with this DRM device, then
  4481. * free up the framebuffers and associated buffer objects.
  4482. *
  4483. * Note that since this /should/ happen single-threaded at driver/device
  4484. * teardown time, no locking is required. It's the driver's job to ensure that
  4485. * this guarantee actually holds true.
  4486. *
  4487. * FIXME: cleanup any dangling user buffer objects too
  4488. */
  4489. void drm_mode_config_cleanup(struct drm_device *dev)
  4490. {
  4491. struct drm_connector *connector, *ot;
  4492. struct drm_crtc *crtc, *ct;
  4493. struct drm_encoder *encoder, *enct;
  4494. struct drm_bridge *bridge, *brt;
  4495. struct drm_framebuffer *fb, *fbt;
  4496. struct drm_property *property, *pt;
  4497. struct drm_property_blob *blob, *bt;
  4498. struct drm_plane *plane, *plt;
  4499. list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
  4500. head) {
  4501. encoder->funcs->destroy(encoder);
  4502. }
  4503. list_for_each_entry_safe(bridge, brt,
  4504. &dev->mode_config.bridge_list, head) {
  4505. bridge->funcs->destroy(bridge);
  4506. }
  4507. list_for_each_entry_safe(connector, ot,
  4508. &dev->mode_config.connector_list, head) {
  4509. connector->funcs->destroy(connector);
  4510. }
  4511. list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
  4512. head) {
  4513. drm_property_destroy(dev, property);
  4514. }
  4515. list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
  4516. head) {
  4517. drm_property_destroy_blob(dev, blob);
  4518. }
  4519. /*
  4520. * Single-threaded teardown context, so it's not required to grab the
  4521. * fb_lock to protect against concurrent fb_list access. Contrary, it
  4522. * would actually deadlock with the drm_framebuffer_cleanup function.
  4523. *
  4524. * Also, if there are any framebuffers left, that's a driver leak now,
  4525. * so politely WARN about this.
  4526. */
  4527. WARN_ON(!list_empty(&dev->mode_config.fb_list));
  4528. list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
  4529. drm_framebuffer_remove(fb);
  4530. }
  4531. list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
  4532. head) {
  4533. plane->funcs->destroy(plane);
  4534. }
  4535. list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
  4536. crtc->funcs->destroy(crtc);
  4537. }
  4538. idr_destroy(&dev->mode_config.crtc_idr);
  4539. drm_modeset_lock_fini(&dev->mode_config.connection_mutex);
  4540. }
  4541. EXPORT_SYMBOL(drm_mode_config_cleanup);
  4542. struct drm_property *drm_mode_create_rotation_property(struct drm_device *dev,
  4543. unsigned int supported_rotations)
  4544. {
  4545. static const struct drm_prop_enum_list props[] = {
  4546. { DRM_ROTATE_0, "rotate-0" },
  4547. { DRM_ROTATE_90, "rotate-90" },
  4548. { DRM_ROTATE_180, "rotate-180" },
  4549. { DRM_ROTATE_270, "rotate-270" },
  4550. { DRM_REFLECT_X, "reflect-x" },
  4551. { DRM_REFLECT_Y, "reflect-y" },
  4552. };
  4553. return drm_property_create_bitmask(dev, 0, "rotation",
  4554. props, ARRAY_SIZE(props),
  4555. supported_rotations);
  4556. }
  4557. EXPORT_SYMBOL(drm_mode_create_rotation_property);