drm_crtc.c 163 KB

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