drm_crtc.c 160 KB

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