drm_crtc.c 162 KB

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