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