drm_crtc.c 162 KB

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