drm_crtc.c 158 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868
  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. drm_for_each_crtc(crtc, dev) {
  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. drm_for_each_plane(plane, dev) {
  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. drm_for_each_crtc(tmp, crtc->dev) {
  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. drm_for_each_connector(tmp, connector->dev) {
  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. /* FIXME: 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, unsigned int 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, unsigned int 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. drm_for_each_plane(tmp, plane->dev) {
  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. drm_for_each_plane(plane, dev) {
  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. /**
  1449. * drm_mode_getresources - get graphics configuration
  1450. * @dev: drm device for the ioctl
  1451. * @data: data pointer for the ioctl
  1452. * @file_priv: drm file for the ioctl call
  1453. *
  1454. * Construct a set of configuration description structures and return
  1455. * them to the user, including CRTC, connector and framebuffer configuration.
  1456. *
  1457. * Called by the user via ioctl.
  1458. *
  1459. * Returns:
  1460. * Zero on success, negative errno on failure.
  1461. */
  1462. int drm_mode_getresources(struct drm_device *dev, void *data,
  1463. struct drm_file *file_priv)
  1464. {
  1465. struct drm_mode_card_res *card_res = data;
  1466. struct list_head *lh;
  1467. struct drm_framebuffer *fb;
  1468. struct drm_connector *connector;
  1469. struct drm_crtc *crtc;
  1470. struct drm_encoder *encoder;
  1471. int ret = 0;
  1472. int connector_count = 0;
  1473. int crtc_count = 0;
  1474. int fb_count = 0;
  1475. int encoder_count = 0;
  1476. int copied = 0;
  1477. uint32_t __user *fb_id;
  1478. uint32_t __user *crtc_id;
  1479. uint32_t __user *connector_id;
  1480. uint32_t __user *encoder_id;
  1481. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1482. return -EINVAL;
  1483. mutex_lock(&file_priv->fbs_lock);
  1484. /*
  1485. * For the non-control nodes we need to limit the list of resources
  1486. * by IDs in the group list for this node
  1487. */
  1488. list_for_each(lh, &file_priv->fbs)
  1489. fb_count++;
  1490. /* handle this in 4 parts */
  1491. /* FBs */
  1492. if (card_res->count_fbs >= fb_count) {
  1493. copied = 0;
  1494. fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
  1495. list_for_each_entry(fb, &file_priv->fbs, filp_head) {
  1496. if (put_user(fb->base.id, fb_id + copied)) {
  1497. mutex_unlock(&file_priv->fbs_lock);
  1498. return -EFAULT;
  1499. }
  1500. copied++;
  1501. }
  1502. }
  1503. card_res->count_fbs = fb_count;
  1504. mutex_unlock(&file_priv->fbs_lock);
  1505. /* mode_config.mutex protects the connector list against e.g. DP MST
  1506. * connector hot-adding. CRTC/Plane lists are invariant. */
  1507. mutex_lock(&dev->mode_config.mutex);
  1508. drm_for_each_crtc(crtc, dev)
  1509. crtc_count++;
  1510. drm_for_each_connector(connector, dev)
  1511. connector_count++;
  1512. drm_for_each_encoder(encoder, dev)
  1513. encoder_count++;
  1514. card_res->max_height = dev->mode_config.max_height;
  1515. card_res->min_height = dev->mode_config.min_height;
  1516. card_res->max_width = dev->mode_config.max_width;
  1517. card_res->min_width = dev->mode_config.min_width;
  1518. /* CRTCs */
  1519. if (card_res->count_crtcs >= crtc_count) {
  1520. copied = 0;
  1521. crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
  1522. drm_for_each_crtc(crtc, dev) {
  1523. DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
  1524. if (put_user(crtc->base.id, crtc_id + copied)) {
  1525. ret = -EFAULT;
  1526. goto out;
  1527. }
  1528. copied++;
  1529. }
  1530. }
  1531. card_res->count_crtcs = crtc_count;
  1532. /* Encoders */
  1533. if (card_res->count_encoders >= encoder_count) {
  1534. copied = 0;
  1535. encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
  1536. drm_for_each_encoder(encoder, dev) {
  1537. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
  1538. encoder->name);
  1539. if (put_user(encoder->base.id, encoder_id +
  1540. copied)) {
  1541. ret = -EFAULT;
  1542. goto out;
  1543. }
  1544. copied++;
  1545. }
  1546. }
  1547. card_res->count_encoders = encoder_count;
  1548. /* Connectors */
  1549. if (card_res->count_connectors >= connector_count) {
  1550. copied = 0;
  1551. connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
  1552. drm_for_each_connector(connector, dev) {
  1553. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  1554. connector->base.id,
  1555. connector->name);
  1556. if (put_user(connector->base.id,
  1557. connector_id + copied)) {
  1558. ret = -EFAULT;
  1559. goto out;
  1560. }
  1561. copied++;
  1562. }
  1563. }
  1564. card_res->count_connectors = connector_count;
  1565. DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
  1566. card_res->count_connectors, card_res->count_encoders);
  1567. out:
  1568. mutex_unlock(&dev->mode_config.mutex);
  1569. return ret;
  1570. }
  1571. /**
  1572. * drm_mode_getcrtc - get CRTC configuration
  1573. * @dev: drm device for the ioctl
  1574. * @data: data pointer for the ioctl
  1575. * @file_priv: drm file for the ioctl call
  1576. *
  1577. * Construct a CRTC configuration structure to return to the user.
  1578. *
  1579. * Called by the user via ioctl.
  1580. *
  1581. * Returns:
  1582. * Zero on success, negative errno on failure.
  1583. */
  1584. int drm_mode_getcrtc(struct drm_device *dev,
  1585. void *data, struct drm_file *file_priv)
  1586. {
  1587. struct drm_mode_crtc *crtc_resp = data;
  1588. struct drm_crtc *crtc;
  1589. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1590. return -EINVAL;
  1591. crtc = drm_crtc_find(dev, crtc_resp->crtc_id);
  1592. if (!crtc)
  1593. return -ENOENT;
  1594. drm_modeset_lock_crtc(crtc, crtc->primary);
  1595. crtc_resp->gamma_size = crtc->gamma_size;
  1596. if (crtc->primary->fb)
  1597. crtc_resp->fb_id = crtc->primary->fb->base.id;
  1598. else
  1599. crtc_resp->fb_id = 0;
  1600. if (crtc->state) {
  1601. crtc_resp->x = crtc->primary->state->src_x >> 16;
  1602. crtc_resp->y = crtc->primary->state->src_y >> 16;
  1603. if (crtc->state->enable) {
  1604. drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->state->mode);
  1605. crtc_resp->mode_valid = 1;
  1606. } else {
  1607. crtc_resp->mode_valid = 0;
  1608. }
  1609. } else {
  1610. crtc_resp->x = crtc->x;
  1611. crtc_resp->y = crtc->y;
  1612. if (crtc->enabled) {
  1613. drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->mode);
  1614. crtc_resp->mode_valid = 1;
  1615. } else {
  1616. crtc_resp->mode_valid = 0;
  1617. }
  1618. }
  1619. drm_modeset_unlock_crtc(crtc);
  1620. return 0;
  1621. }
  1622. static bool drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
  1623. const struct drm_file *file_priv)
  1624. {
  1625. /*
  1626. * If user-space hasn't configured the driver to expose the stereo 3D
  1627. * modes, don't expose them.
  1628. */
  1629. if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
  1630. return false;
  1631. return true;
  1632. }
  1633. static struct drm_encoder *drm_connector_get_encoder(struct drm_connector *connector)
  1634. {
  1635. /* For atomic drivers only state objects are synchronously updated and
  1636. * protected by modeset locks, so check those first. */
  1637. if (connector->state)
  1638. return connector->state->best_encoder;
  1639. return connector->encoder;
  1640. }
  1641. /* helper for getconnector and getproperties ioctls */
  1642. static int get_properties(struct drm_mode_object *obj, bool atomic,
  1643. uint32_t __user *prop_ptr, uint64_t __user *prop_values,
  1644. uint32_t *arg_count_props)
  1645. {
  1646. int props_count;
  1647. int i, ret, copied;
  1648. props_count = obj->properties->count;
  1649. if (!atomic)
  1650. props_count -= obj->properties->atomic_count;
  1651. if ((*arg_count_props >= props_count) && props_count) {
  1652. for (i = 0, copied = 0; copied < props_count; i++) {
  1653. struct drm_property *prop = obj->properties->properties[i];
  1654. uint64_t val;
  1655. if ((prop->flags & DRM_MODE_PROP_ATOMIC) && !atomic)
  1656. continue;
  1657. ret = drm_object_property_get_value(obj, prop, &val);
  1658. if (ret)
  1659. return ret;
  1660. if (put_user(prop->base.id, prop_ptr + copied))
  1661. return -EFAULT;
  1662. if (put_user(val, prop_values + copied))
  1663. return -EFAULT;
  1664. copied++;
  1665. }
  1666. }
  1667. *arg_count_props = props_count;
  1668. return 0;
  1669. }
  1670. /**
  1671. * drm_mode_getconnector - get connector configuration
  1672. * @dev: drm device for the ioctl
  1673. * @data: data pointer for the ioctl
  1674. * @file_priv: drm file for the ioctl call
  1675. *
  1676. * Construct a connector configuration structure to return to the user.
  1677. *
  1678. * Called by the user via ioctl.
  1679. *
  1680. * Returns:
  1681. * Zero on success, negative errno on failure.
  1682. */
  1683. int drm_mode_getconnector(struct drm_device *dev, void *data,
  1684. struct drm_file *file_priv)
  1685. {
  1686. struct drm_mode_get_connector *out_resp = data;
  1687. struct drm_connector *connector;
  1688. struct drm_encoder *encoder;
  1689. struct drm_display_mode *mode;
  1690. int mode_count = 0;
  1691. int encoders_count = 0;
  1692. int ret = 0;
  1693. int copied = 0;
  1694. int i;
  1695. struct drm_mode_modeinfo u_mode;
  1696. struct drm_mode_modeinfo __user *mode_ptr;
  1697. uint32_t __user *encoder_ptr;
  1698. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1699. return -EINVAL;
  1700. memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
  1701. DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
  1702. mutex_lock(&dev->mode_config.mutex);
  1703. connector = drm_connector_find(dev, out_resp->connector_id);
  1704. if (!connector) {
  1705. ret = -ENOENT;
  1706. goto out_unlock;
  1707. }
  1708. for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++)
  1709. if (connector->encoder_ids[i] != 0)
  1710. encoders_count++;
  1711. if (out_resp->count_modes == 0) {
  1712. connector->funcs->fill_modes(connector,
  1713. dev->mode_config.max_width,
  1714. dev->mode_config.max_height);
  1715. }
  1716. /* delayed so we get modes regardless of pre-fill_modes state */
  1717. list_for_each_entry(mode, &connector->modes, head)
  1718. if (drm_mode_expose_to_userspace(mode, file_priv))
  1719. mode_count++;
  1720. out_resp->connector_id = connector->base.id;
  1721. out_resp->connector_type = connector->connector_type;
  1722. out_resp->connector_type_id = connector->connector_type_id;
  1723. out_resp->mm_width = connector->display_info.width_mm;
  1724. out_resp->mm_height = connector->display_info.height_mm;
  1725. out_resp->subpixel = connector->display_info.subpixel_order;
  1726. out_resp->connection = connector->status;
  1727. drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
  1728. encoder = drm_connector_get_encoder(connector);
  1729. if (encoder)
  1730. out_resp->encoder_id = encoder->base.id;
  1731. else
  1732. out_resp->encoder_id = 0;
  1733. /*
  1734. * This ioctl is called twice, once to determine how much space is
  1735. * needed, and the 2nd time to fill it.
  1736. */
  1737. if ((out_resp->count_modes >= mode_count) && mode_count) {
  1738. copied = 0;
  1739. mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
  1740. list_for_each_entry(mode, &connector->modes, head) {
  1741. if (!drm_mode_expose_to_userspace(mode, file_priv))
  1742. continue;
  1743. drm_mode_convert_to_umode(&u_mode, mode);
  1744. if (copy_to_user(mode_ptr + copied,
  1745. &u_mode, sizeof(u_mode))) {
  1746. ret = -EFAULT;
  1747. goto out;
  1748. }
  1749. copied++;
  1750. }
  1751. }
  1752. out_resp->count_modes = mode_count;
  1753. ret = get_properties(&connector->base, file_priv->atomic,
  1754. (uint32_t __user *)(unsigned long)(out_resp->props_ptr),
  1755. (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr),
  1756. &out_resp->count_props);
  1757. if (ret)
  1758. goto out;
  1759. if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
  1760. copied = 0;
  1761. encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
  1762. for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
  1763. if (connector->encoder_ids[i] != 0) {
  1764. if (put_user(connector->encoder_ids[i],
  1765. encoder_ptr + copied)) {
  1766. ret = -EFAULT;
  1767. goto out;
  1768. }
  1769. copied++;
  1770. }
  1771. }
  1772. }
  1773. out_resp->count_encoders = encoders_count;
  1774. out:
  1775. drm_modeset_unlock(&dev->mode_config.connection_mutex);
  1776. out_unlock:
  1777. mutex_unlock(&dev->mode_config.mutex);
  1778. return ret;
  1779. }
  1780. static struct drm_crtc *drm_encoder_get_crtc(struct drm_encoder *encoder)
  1781. {
  1782. struct drm_connector *connector;
  1783. struct drm_device *dev = encoder->dev;
  1784. bool uses_atomic = false;
  1785. /* For atomic drivers only state objects are synchronously updated and
  1786. * protected by modeset locks, so check those first. */
  1787. drm_for_each_connector(connector, dev) {
  1788. if (!connector->state)
  1789. continue;
  1790. uses_atomic = true;
  1791. if (connector->state->best_encoder != encoder)
  1792. continue;
  1793. return connector->state->crtc;
  1794. }
  1795. /* Don't return stale data (e.g. pending async disable). */
  1796. if (uses_atomic)
  1797. return NULL;
  1798. return encoder->crtc;
  1799. }
  1800. /**
  1801. * drm_mode_getencoder - get encoder configuration
  1802. * @dev: drm device for the ioctl
  1803. * @data: data pointer for the ioctl
  1804. * @file_priv: drm file for the ioctl call
  1805. *
  1806. * Construct a encoder configuration structure to return to the user.
  1807. *
  1808. * Called by the user via ioctl.
  1809. *
  1810. * Returns:
  1811. * Zero on success, negative errno on failure.
  1812. */
  1813. int drm_mode_getencoder(struct drm_device *dev, void *data,
  1814. struct drm_file *file_priv)
  1815. {
  1816. struct drm_mode_get_encoder *enc_resp = data;
  1817. struct drm_encoder *encoder;
  1818. struct drm_crtc *crtc;
  1819. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1820. return -EINVAL;
  1821. encoder = drm_encoder_find(dev, enc_resp->encoder_id);
  1822. if (!encoder)
  1823. return -ENOENT;
  1824. drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
  1825. crtc = drm_encoder_get_crtc(encoder);
  1826. if (crtc)
  1827. enc_resp->crtc_id = crtc->base.id;
  1828. else
  1829. enc_resp->crtc_id = 0;
  1830. drm_modeset_unlock(&dev->mode_config.connection_mutex);
  1831. enc_resp->encoder_type = encoder->encoder_type;
  1832. enc_resp->encoder_id = encoder->base.id;
  1833. enc_resp->possible_crtcs = encoder->possible_crtcs;
  1834. enc_resp->possible_clones = encoder->possible_clones;
  1835. return 0;
  1836. }
  1837. /**
  1838. * drm_mode_getplane_res - enumerate all plane resources
  1839. * @dev: DRM device
  1840. * @data: ioctl data
  1841. * @file_priv: DRM file info
  1842. *
  1843. * Construct a list of plane ids to return to the user.
  1844. *
  1845. * Called by the user via ioctl.
  1846. *
  1847. * Returns:
  1848. * Zero on success, negative errno on failure.
  1849. */
  1850. int drm_mode_getplane_res(struct drm_device *dev, void *data,
  1851. struct drm_file *file_priv)
  1852. {
  1853. struct drm_mode_get_plane_res *plane_resp = data;
  1854. struct drm_mode_config *config;
  1855. struct drm_plane *plane;
  1856. uint32_t __user *plane_ptr;
  1857. int copied = 0;
  1858. unsigned num_planes;
  1859. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1860. return -EINVAL;
  1861. config = &dev->mode_config;
  1862. if (file_priv->universal_planes)
  1863. num_planes = config->num_total_plane;
  1864. else
  1865. num_planes = config->num_overlay_plane;
  1866. /*
  1867. * This ioctl is called twice, once to determine how much space is
  1868. * needed, and the 2nd time to fill it.
  1869. */
  1870. if (num_planes &&
  1871. (plane_resp->count_planes >= num_planes)) {
  1872. plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
  1873. /* Plane lists are invariant, no locking needed. */
  1874. drm_for_each_plane(plane, dev) {
  1875. /*
  1876. * Unless userspace set the 'universal planes'
  1877. * capability bit, only advertise overlays.
  1878. */
  1879. if (plane->type != DRM_PLANE_TYPE_OVERLAY &&
  1880. !file_priv->universal_planes)
  1881. continue;
  1882. if (put_user(plane->base.id, plane_ptr + copied))
  1883. return -EFAULT;
  1884. copied++;
  1885. }
  1886. }
  1887. plane_resp->count_planes = num_planes;
  1888. return 0;
  1889. }
  1890. /**
  1891. * drm_mode_getplane - get plane configuration
  1892. * @dev: DRM device
  1893. * @data: ioctl data
  1894. * @file_priv: DRM file info
  1895. *
  1896. * Construct a plane configuration structure to return to the user.
  1897. *
  1898. * Called by the user via ioctl.
  1899. *
  1900. * Returns:
  1901. * Zero on success, negative errno on failure.
  1902. */
  1903. int drm_mode_getplane(struct drm_device *dev, void *data,
  1904. struct drm_file *file_priv)
  1905. {
  1906. struct drm_mode_get_plane *plane_resp = data;
  1907. struct drm_plane *plane;
  1908. uint32_t __user *format_ptr;
  1909. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1910. return -EINVAL;
  1911. plane = drm_plane_find(dev, plane_resp->plane_id);
  1912. if (!plane)
  1913. return -ENOENT;
  1914. drm_modeset_lock(&plane->mutex, NULL);
  1915. if (plane->crtc)
  1916. plane_resp->crtc_id = plane->crtc->base.id;
  1917. else
  1918. plane_resp->crtc_id = 0;
  1919. if (plane->fb)
  1920. plane_resp->fb_id = plane->fb->base.id;
  1921. else
  1922. plane_resp->fb_id = 0;
  1923. drm_modeset_unlock(&plane->mutex);
  1924. plane_resp->plane_id = plane->base.id;
  1925. plane_resp->possible_crtcs = plane->possible_crtcs;
  1926. plane_resp->gamma_size = 0;
  1927. /*
  1928. * This ioctl is called twice, once to determine how much space is
  1929. * needed, and the 2nd time to fill it.
  1930. */
  1931. if (plane->format_count &&
  1932. (plane_resp->count_format_types >= plane->format_count)) {
  1933. format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
  1934. if (copy_to_user(format_ptr,
  1935. plane->format_types,
  1936. sizeof(uint32_t) * plane->format_count)) {
  1937. return -EFAULT;
  1938. }
  1939. }
  1940. plane_resp->count_format_types = plane->format_count;
  1941. return 0;
  1942. }
  1943. /**
  1944. * drm_plane_check_pixel_format - Check if the plane supports the pixel format
  1945. * @plane: plane to check for format support
  1946. * @format: the pixel format
  1947. *
  1948. * Returns:
  1949. * Zero of @plane has @format in its list of supported pixel formats, -EINVAL
  1950. * otherwise.
  1951. */
  1952. int drm_plane_check_pixel_format(const struct drm_plane *plane, u32 format)
  1953. {
  1954. unsigned int i;
  1955. for (i = 0; i < plane->format_count; i++) {
  1956. if (format == plane->format_types[i])
  1957. return 0;
  1958. }
  1959. return -EINVAL;
  1960. }
  1961. /*
  1962. * setplane_internal - setplane handler for internal callers
  1963. *
  1964. * Note that we assume an extra reference has already been taken on fb. If the
  1965. * update fails, this reference will be dropped before return; if it succeeds,
  1966. * the previous framebuffer (if any) will be unreferenced instead.
  1967. *
  1968. * src_{x,y,w,h} are provided in 16.16 fixed point format
  1969. */
  1970. static int __setplane_internal(struct drm_plane *plane,
  1971. struct drm_crtc *crtc,
  1972. struct drm_framebuffer *fb,
  1973. int32_t crtc_x, int32_t crtc_y,
  1974. uint32_t crtc_w, uint32_t crtc_h,
  1975. /* src_{x,y,w,h} values are 16.16 fixed point */
  1976. uint32_t src_x, uint32_t src_y,
  1977. uint32_t src_w, uint32_t src_h)
  1978. {
  1979. int ret = 0;
  1980. unsigned int fb_width, fb_height;
  1981. /* No fb means shut it down */
  1982. if (!fb) {
  1983. plane->old_fb = plane->fb;
  1984. ret = plane->funcs->disable_plane(plane);
  1985. if (!ret) {
  1986. plane->crtc = NULL;
  1987. plane->fb = NULL;
  1988. } else {
  1989. plane->old_fb = NULL;
  1990. }
  1991. goto out;
  1992. }
  1993. /* Check whether this plane is usable on this CRTC */
  1994. if (!(plane->possible_crtcs & drm_crtc_mask(crtc))) {
  1995. DRM_DEBUG_KMS("Invalid crtc for plane\n");
  1996. ret = -EINVAL;
  1997. goto out;
  1998. }
  1999. /* Check whether this plane supports the fb pixel format. */
  2000. ret = drm_plane_check_pixel_format(plane, fb->pixel_format);
  2001. if (ret) {
  2002. DRM_DEBUG_KMS("Invalid pixel format %s\n",
  2003. drm_get_format_name(fb->pixel_format));
  2004. goto out;
  2005. }
  2006. /* Give drivers some help against integer overflows */
  2007. if (crtc_w > INT_MAX ||
  2008. crtc_x > INT_MAX - (int32_t) crtc_w ||
  2009. crtc_h > INT_MAX ||
  2010. crtc_y > INT_MAX - (int32_t) crtc_h) {
  2011. DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
  2012. crtc_w, crtc_h, crtc_x, crtc_y);
  2013. return -ERANGE;
  2014. }
  2015. fb_width = fb->width << 16;
  2016. fb_height = fb->height << 16;
  2017. /* Make sure source coordinates are inside the fb. */
  2018. if (src_w > fb_width ||
  2019. src_x > fb_width - src_w ||
  2020. src_h > fb_height ||
  2021. src_y > fb_height - src_h) {
  2022. DRM_DEBUG_KMS("Invalid source coordinates "
  2023. "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
  2024. src_w >> 16, ((src_w & 0xffff) * 15625) >> 10,
  2025. src_h >> 16, ((src_h & 0xffff) * 15625) >> 10,
  2026. src_x >> 16, ((src_x & 0xffff) * 15625) >> 10,
  2027. src_y >> 16, ((src_y & 0xffff) * 15625) >> 10);
  2028. ret = -ENOSPC;
  2029. goto out;
  2030. }
  2031. plane->old_fb = plane->fb;
  2032. ret = plane->funcs->update_plane(plane, crtc, fb,
  2033. crtc_x, crtc_y, crtc_w, crtc_h,
  2034. src_x, src_y, src_w, src_h);
  2035. if (!ret) {
  2036. plane->crtc = crtc;
  2037. plane->fb = fb;
  2038. fb = NULL;
  2039. } else {
  2040. plane->old_fb = NULL;
  2041. }
  2042. out:
  2043. if (fb)
  2044. drm_framebuffer_unreference(fb);
  2045. if (plane->old_fb)
  2046. drm_framebuffer_unreference(plane->old_fb);
  2047. plane->old_fb = NULL;
  2048. return ret;
  2049. }
  2050. static int setplane_internal(struct drm_plane *plane,
  2051. struct drm_crtc *crtc,
  2052. struct drm_framebuffer *fb,
  2053. int32_t crtc_x, int32_t crtc_y,
  2054. uint32_t crtc_w, uint32_t crtc_h,
  2055. /* src_{x,y,w,h} values are 16.16 fixed point */
  2056. uint32_t src_x, uint32_t src_y,
  2057. uint32_t src_w, uint32_t src_h)
  2058. {
  2059. int ret;
  2060. drm_modeset_lock_all(plane->dev);
  2061. ret = __setplane_internal(plane, crtc, fb,
  2062. crtc_x, crtc_y, crtc_w, crtc_h,
  2063. src_x, src_y, src_w, src_h);
  2064. drm_modeset_unlock_all(plane->dev);
  2065. return ret;
  2066. }
  2067. /**
  2068. * drm_mode_setplane - configure a plane's configuration
  2069. * @dev: DRM device
  2070. * @data: ioctl data*
  2071. * @file_priv: DRM file info
  2072. *
  2073. * Set plane configuration, including placement, fb, scaling, and other factors.
  2074. * Or pass a NULL fb to disable (planes may be disabled without providing a
  2075. * valid crtc).
  2076. *
  2077. * Returns:
  2078. * Zero on success, negative errno on failure.
  2079. */
  2080. int drm_mode_setplane(struct drm_device *dev, void *data,
  2081. struct drm_file *file_priv)
  2082. {
  2083. struct drm_mode_set_plane *plane_req = data;
  2084. struct drm_plane *plane;
  2085. struct drm_crtc *crtc = NULL;
  2086. struct drm_framebuffer *fb = NULL;
  2087. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2088. return -EINVAL;
  2089. /*
  2090. * First, find the plane, crtc, and fb objects. If not available,
  2091. * we don't bother to call the driver.
  2092. */
  2093. plane = drm_plane_find(dev, plane_req->plane_id);
  2094. if (!plane) {
  2095. DRM_DEBUG_KMS("Unknown plane ID %d\n",
  2096. plane_req->plane_id);
  2097. return -ENOENT;
  2098. }
  2099. if (plane_req->fb_id) {
  2100. fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
  2101. if (!fb) {
  2102. DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
  2103. plane_req->fb_id);
  2104. return -ENOENT;
  2105. }
  2106. crtc = drm_crtc_find(dev, plane_req->crtc_id);
  2107. if (!crtc) {
  2108. DRM_DEBUG_KMS("Unknown crtc ID %d\n",
  2109. plane_req->crtc_id);
  2110. return -ENOENT;
  2111. }
  2112. }
  2113. /*
  2114. * setplane_internal will take care of deref'ing either the old or new
  2115. * framebuffer depending on success.
  2116. */
  2117. return setplane_internal(plane, crtc, fb,
  2118. plane_req->crtc_x, plane_req->crtc_y,
  2119. plane_req->crtc_w, plane_req->crtc_h,
  2120. plane_req->src_x, plane_req->src_y,
  2121. plane_req->src_w, plane_req->src_h);
  2122. }
  2123. /**
  2124. * drm_mode_set_config_internal - helper to call ->set_config
  2125. * @set: modeset config to set
  2126. *
  2127. * This is a little helper to wrap internal calls to the ->set_config driver
  2128. * interface. The only thing it adds is correct refcounting dance.
  2129. *
  2130. * Returns:
  2131. * Zero on success, negative errno on failure.
  2132. */
  2133. int drm_mode_set_config_internal(struct drm_mode_set *set)
  2134. {
  2135. struct drm_crtc *crtc = set->crtc;
  2136. struct drm_framebuffer *fb;
  2137. struct drm_crtc *tmp;
  2138. int ret;
  2139. /*
  2140. * NOTE: ->set_config can also disable other crtcs (if we steal all
  2141. * connectors from it), hence we need to refcount the fbs across all
  2142. * crtcs. Atomic modeset will have saner semantics ...
  2143. */
  2144. drm_for_each_crtc(tmp, crtc->dev)
  2145. tmp->primary->old_fb = tmp->primary->fb;
  2146. fb = set->fb;
  2147. ret = crtc->funcs->set_config(set);
  2148. if (ret == 0) {
  2149. crtc->primary->crtc = crtc;
  2150. crtc->primary->fb = fb;
  2151. }
  2152. drm_for_each_crtc(tmp, crtc->dev) {
  2153. if (tmp->primary->fb)
  2154. drm_framebuffer_reference(tmp->primary->fb);
  2155. if (tmp->primary->old_fb)
  2156. drm_framebuffer_unreference(tmp->primary->old_fb);
  2157. tmp->primary->old_fb = NULL;
  2158. }
  2159. return ret;
  2160. }
  2161. EXPORT_SYMBOL(drm_mode_set_config_internal);
  2162. /**
  2163. * drm_crtc_get_hv_timing - Fetches hdisplay/vdisplay for given mode
  2164. * @mode: mode to query
  2165. * @hdisplay: hdisplay value to fill in
  2166. * @vdisplay: vdisplay value to fill in
  2167. *
  2168. * The vdisplay value will be doubled if the specified mode is a stereo mode of
  2169. * the appropriate layout.
  2170. */
  2171. void drm_crtc_get_hv_timing(const struct drm_display_mode *mode,
  2172. int *hdisplay, int *vdisplay)
  2173. {
  2174. struct drm_display_mode adjusted;
  2175. drm_mode_copy(&adjusted, mode);
  2176. drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE_ONLY);
  2177. *hdisplay = adjusted.crtc_hdisplay;
  2178. *vdisplay = adjusted.crtc_vdisplay;
  2179. }
  2180. EXPORT_SYMBOL(drm_crtc_get_hv_timing);
  2181. /**
  2182. * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
  2183. * CRTC viewport
  2184. * @crtc: CRTC that framebuffer will be displayed on
  2185. * @x: x panning
  2186. * @y: y panning
  2187. * @mode: mode that framebuffer will be displayed under
  2188. * @fb: framebuffer to check size of
  2189. */
  2190. int drm_crtc_check_viewport(const struct drm_crtc *crtc,
  2191. int x, int y,
  2192. const struct drm_display_mode *mode,
  2193. const struct drm_framebuffer *fb)
  2194. {
  2195. int hdisplay, vdisplay;
  2196. drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
  2197. if (crtc->invert_dimensions)
  2198. swap(hdisplay, vdisplay);
  2199. if (hdisplay > fb->width ||
  2200. vdisplay > fb->height ||
  2201. x > fb->width - hdisplay ||
  2202. y > fb->height - vdisplay) {
  2203. DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
  2204. fb->width, fb->height, hdisplay, vdisplay, x, y,
  2205. crtc->invert_dimensions ? " (inverted)" : "");
  2206. return -ENOSPC;
  2207. }
  2208. return 0;
  2209. }
  2210. EXPORT_SYMBOL(drm_crtc_check_viewport);
  2211. /**
  2212. * drm_mode_setcrtc - set CRTC configuration
  2213. * @dev: drm device for the ioctl
  2214. * @data: data pointer for the ioctl
  2215. * @file_priv: drm file for the ioctl call
  2216. *
  2217. * Build a new CRTC configuration based on user request.
  2218. *
  2219. * Called by the user via ioctl.
  2220. *
  2221. * Returns:
  2222. * Zero on success, negative errno on failure.
  2223. */
  2224. int drm_mode_setcrtc(struct drm_device *dev, void *data,
  2225. struct drm_file *file_priv)
  2226. {
  2227. struct drm_mode_config *config = &dev->mode_config;
  2228. struct drm_mode_crtc *crtc_req = data;
  2229. struct drm_crtc *crtc;
  2230. struct drm_connector **connector_set = NULL, *connector;
  2231. struct drm_framebuffer *fb = NULL;
  2232. struct drm_display_mode *mode = NULL;
  2233. struct drm_mode_set set;
  2234. uint32_t __user *set_connectors_ptr;
  2235. int ret;
  2236. int i;
  2237. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2238. return -EINVAL;
  2239. /*
  2240. * Universal plane src offsets are only 16.16, prevent havoc for
  2241. * drivers using universal plane code internally.
  2242. */
  2243. if (crtc_req->x & 0xffff0000 || crtc_req->y & 0xffff0000)
  2244. return -ERANGE;
  2245. drm_modeset_lock_all(dev);
  2246. crtc = drm_crtc_find(dev, crtc_req->crtc_id);
  2247. if (!crtc) {
  2248. DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
  2249. ret = -ENOENT;
  2250. goto out;
  2251. }
  2252. DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
  2253. if (crtc_req->mode_valid) {
  2254. /* If we have a mode we need a framebuffer. */
  2255. /* If we pass -1, set the mode with the currently bound fb */
  2256. if (crtc_req->fb_id == -1) {
  2257. if (!crtc->primary->fb) {
  2258. DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
  2259. ret = -EINVAL;
  2260. goto out;
  2261. }
  2262. fb = crtc->primary->fb;
  2263. /* Make refcounting symmetric with the lookup path. */
  2264. drm_framebuffer_reference(fb);
  2265. } else {
  2266. fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
  2267. if (!fb) {
  2268. DRM_DEBUG_KMS("Unknown FB ID%d\n",
  2269. crtc_req->fb_id);
  2270. ret = -ENOENT;
  2271. goto out;
  2272. }
  2273. }
  2274. mode = drm_mode_create(dev);
  2275. if (!mode) {
  2276. ret = -ENOMEM;
  2277. goto out;
  2278. }
  2279. ret = drm_mode_convert_umode(mode, &crtc_req->mode);
  2280. if (ret) {
  2281. DRM_DEBUG_KMS("Invalid mode\n");
  2282. goto out;
  2283. }
  2284. drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
  2285. /*
  2286. * Check whether the primary plane supports the fb pixel format.
  2287. * Drivers not implementing the universal planes API use a
  2288. * default formats list provided by the DRM core which doesn't
  2289. * match real hardware capabilities. Skip the check in that
  2290. * case.
  2291. */
  2292. if (!crtc->primary->format_default) {
  2293. ret = drm_plane_check_pixel_format(crtc->primary,
  2294. fb->pixel_format);
  2295. if (ret) {
  2296. DRM_DEBUG_KMS("Invalid pixel format %s\n",
  2297. drm_get_format_name(fb->pixel_format));
  2298. goto out;
  2299. }
  2300. }
  2301. ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
  2302. mode, fb);
  2303. if (ret)
  2304. goto out;
  2305. }
  2306. if (crtc_req->count_connectors == 0 && mode) {
  2307. DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
  2308. ret = -EINVAL;
  2309. goto out;
  2310. }
  2311. if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
  2312. DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
  2313. crtc_req->count_connectors);
  2314. ret = -EINVAL;
  2315. goto out;
  2316. }
  2317. if (crtc_req->count_connectors > 0) {
  2318. u32 out_id;
  2319. /* Avoid unbounded kernel memory allocation */
  2320. if (crtc_req->count_connectors > config->num_connector) {
  2321. ret = -EINVAL;
  2322. goto out;
  2323. }
  2324. connector_set = kmalloc_array(crtc_req->count_connectors,
  2325. sizeof(struct drm_connector *),
  2326. GFP_KERNEL);
  2327. if (!connector_set) {
  2328. ret = -ENOMEM;
  2329. goto out;
  2330. }
  2331. for (i = 0; i < crtc_req->count_connectors; i++) {
  2332. set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
  2333. if (get_user(out_id, &set_connectors_ptr[i])) {
  2334. ret = -EFAULT;
  2335. goto out;
  2336. }
  2337. connector = drm_connector_find(dev, out_id);
  2338. if (!connector) {
  2339. DRM_DEBUG_KMS("Connector id %d unknown\n",
  2340. out_id);
  2341. ret = -ENOENT;
  2342. goto out;
  2343. }
  2344. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  2345. connector->base.id,
  2346. connector->name);
  2347. connector_set[i] = connector;
  2348. }
  2349. }
  2350. set.crtc = crtc;
  2351. set.x = crtc_req->x;
  2352. set.y = crtc_req->y;
  2353. set.mode = mode;
  2354. set.connectors = connector_set;
  2355. set.num_connectors = crtc_req->count_connectors;
  2356. set.fb = fb;
  2357. ret = drm_mode_set_config_internal(&set);
  2358. out:
  2359. if (fb)
  2360. drm_framebuffer_unreference(fb);
  2361. kfree(connector_set);
  2362. drm_mode_destroy(dev, mode);
  2363. drm_modeset_unlock_all(dev);
  2364. return ret;
  2365. }
  2366. /**
  2367. * drm_mode_cursor_universal - translate legacy cursor ioctl call into a
  2368. * universal plane handler call
  2369. * @crtc: crtc to update cursor for
  2370. * @req: data pointer for the ioctl
  2371. * @file_priv: drm file for the ioctl call
  2372. *
  2373. * Legacy cursor ioctl's work directly with driver buffer handles. To
  2374. * translate legacy ioctl calls into universal plane handler calls, we need to
  2375. * wrap the native buffer handle in a drm_framebuffer.
  2376. *
  2377. * Note that we assume any handle passed to the legacy ioctls was a 32-bit ARGB
  2378. * buffer with a pitch of 4*width; the universal plane interface should be used
  2379. * directly in cases where the hardware can support other buffer settings and
  2380. * userspace wants to make use of these capabilities.
  2381. *
  2382. * Returns:
  2383. * Zero on success, negative errno on failure.
  2384. */
  2385. static int drm_mode_cursor_universal(struct drm_crtc *crtc,
  2386. struct drm_mode_cursor2 *req,
  2387. struct drm_file *file_priv)
  2388. {
  2389. struct drm_device *dev = crtc->dev;
  2390. struct drm_framebuffer *fb = NULL;
  2391. struct drm_mode_fb_cmd2 fbreq = {
  2392. .width = req->width,
  2393. .height = req->height,
  2394. .pixel_format = DRM_FORMAT_ARGB8888,
  2395. .pitches = { req->width * 4 },
  2396. .handles = { req->handle },
  2397. };
  2398. int32_t crtc_x, crtc_y;
  2399. uint32_t crtc_w = 0, crtc_h = 0;
  2400. uint32_t src_w = 0, src_h = 0;
  2401. int ret = 0;
  2402. BUG_ON(!crtc->cursor);
  2403. WARN_ON(crtc->cursor->crtc != crtc && crtc->cursor->crtc != NULL);
  2404. /*
  2405. * Obtain fb we'll be using (either new or existing) and take an extra
  2406. * reference to it if fb != null. setplane will take care of dropping
  2407. * the reference if the plane update fails.
  2408. */
  2409. if (req->flags & DRM_MODE_CURSOR_BO) {
  2410. if (req->handle) {
  2411. fb = internal_framebuffer_create(dev, &fbreq, file_priv);
  2412. if (IS_ERR(fb)) {
  2413. DRM_DEBUG_KMS("failed to wrap cursor buffer in drm framebuffer\n");
  2414. return PTR_ERR(fb);
  2415. }
  2416. } else {
  2417. fb = NULL;
  2418. }
  2419. } else {
  2420. fb = crtc->cursor->fb;
  2421. if (fb)
  2422. drm_framebuffer_reference(fb);
  2423. }
  2424. if (req->flags & DRM_MODE_CURSOR_MOVE) {
  2425. crtc_x = req->x;
  2426. crtc_y = req->y;
  2427. } else {
  2428. crtc_x = crtc->cursor_x;
  2429. crtc_y = crtc->cursor_y;
  2430. }
  2431. if (fb) {
  2432. crtc_w = fb->width;
  2433. crtc_h = fb->height;
  2434. src_w = fb->width << 16;
  2435. src_h = fb->height << 16;
  2436. }
  2437. /*
  2438. * setplane_internal will take care of deref'ing either the old or new
  2439. * framebuffer depending on success.
  2440. */
  2441. ret = __setplane_internal(crtc->cursor, crtc, fb,
  2442. crtc_x, crtc_y, crtc_w, crtc_h,
  2443. 0, 0, src_w, src_h);
  2444. /* Update successful; save new cursor position, if necessary */
  2445. if (ret == 0 && req->flags & DRM_MODE_CURSOR_MOVE) {
  2446. crtc->cursor_x = req->x;
  2447. crtc->cursor_y = req->y;
  2448. }
  2449. return ret;
  2450. }
  2451. static int drm_mode_cursor_common(struct drm_device *dev,
  2452. struct drm_mode_cursor2 *req,
  2453. struct drm_file *file_priv)
  2454. {
  2455. struct drm_crtc *crtc;
  2456. int ret = 0;
  2457. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2458. return -EINVAL;
  2459. if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
  2460. return -EINVAL;
  2461. crtc = drm_crtc_find(dev, req->crtc_id);
  2462. if (!crtc) {
  2463. DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
  2464. return -ENOENT;
  2465. }
  2466. /*
  2467. * If this crtc has a universal cursor plane, call that plane's update
  2468. * handler rather than using legacy cursor handlers.
  2469. */
  2470. drm_modeset_lock_crtc(crtc, crtc->cursor);
  2471. if (crtc->cursor) {
  2472. ret = drm_mode_cursor_universal(crtc, req, file_priv);
  2473. goto out;
  2474. }
  2475. if (req->flags & DRM_MODE_CURSOR_BO) {
  2476. if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
  2477. ret = -ENXIO;
  2478. goto out;
  2479. }
  2480. /* Turns off the cursor if handle is 0 */
  2481. if (crtc->funcs->cursor_set2)
  2482. ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
  2483. req->width, req->height, req->hot_x, req->hot_y);
  2484. else
  2485. ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
  2486. req->width, req->height);
  2487. }
  2488. if (req->flags & DRM_MODE_CURSOR_MOVE) {
  2489. if (crtc->funcs->cursor_move) {
  2490. ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
  2491. } else {
  2492. ret = -EFAULT;
  2493. goto out;
  2494. }
  2495. }
  2496. out:
  2497. drm_modeset_unlock_crtc(crtc);
  2498. return ret;
  2499. }
  2500. /**
  2501. * drm_mode_cursor_ioctl - set CRTC's cursor configuration
  2502. * @dev: drm device for the ioctl
  2503. * @data: data pointer for the ioctl
  2504. * @file_priv: drm file for the ioctl call
  2505. *
  2506. * Set the cursor configuration based on user request.
  2507. *
  2508. * Called by the user via ioctl.
  2509. *
  2510. * Returns:
  2511. * Zero on success, negative errno on failure.
  2512. */
  2513. int drm_mode_cursor_ioctl(struct drm_device *dev,
  2514. void *data, struct drm_file *file_priv)
  2515. {
  2516. struct drm_mode_cursor *req = data;
  2517. struct drm_mode_cursor2 new_req;
  2518. memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
  2519. new_req.hot_x = new_req.hot_y = 0;
  2520. return drm_mode_cursor_common(dev, &new_req, file_priv);
  2521. }
  2522. /**
  2523. * drm_mode_cursor2_ioctl - set CRTC's cursor configuration
  2524. * @dev: drm device for the ioctl
  2525. * @data: data pointer for the ioctl
  2526. * @file_priv: drm file for the ioctl call
  2527. *
  2528. * Set the cursor configuration based on user request. This implements the 2nd
  2529. * version of the cursor ioctl, which allows userspace to additionally specify
  2530. * the hotspot of the pointer.
  2531. *
  2532. * Called by the user via ioctl.
  2533. *
  2534. * Returns:
  2535. * Zero on success, negative errno on failure.
  2536. */
  2537. int drm_mode_cursor2_ioctl(struct drm_device *dev,
  2538. void *data, struct drm_file *file_priv)
  2539. {
  2540. struct drm_mode_cursor2 *req = data;
  2541. return drm_mode_cursor_common(dev, req, file_priv);
  2542. }
  2543. /**
  2544. * drm_mode_legacy_fb_format - compute drm fourcc code from legacy description
  2545. * @bpp: bits per pixels
  2546. * @depth: bit depth per pixel
  2547. *
  2548. * Computes a drm fourcc pixel format code for the given @bpp/@depth values.
  2549. * Useful in fbdev emulation code, since that deals in those values.
  2550. */
  2551. uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
  2552. {
  2553. uint32_t fmt;
  2554. switch (bpp) {
  2555. case 8:
  2556. fmt = DRM_FORMAT_C8;
  2557. break;
  2558. case 16:
  2559. if (depth == 15)
  2560. fmt = DRM_FORMAT_XRGB1555;
  2561. else
  2562. fmt = DRM_FORMAT_RGB565;
  2563. break;
  2564. case 24:
  2565. fmt = DRM_FORMAT_RGB888;
  2566. break;
  2567. case 32:
  2568. if (depth == 24)
  2569. fmt = DRM_FORMAT_XRGB8888;
  2570. else if (depth == 30)
  2571. fmt = DRM_FORMAT_XRGB2101010;
  2572. else
  2573. fmt = DRM_FORMAT_ARGB8888;
  2574. break;
  2575. default:
  2576. DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
  2577. fmt = DRM_FORMAT_XRGB8888;
  2578. break;
  2579. }
  2580. return fmt;
  2581. }
  2582. EXPORT_SYMBOL(drm_mode_legacy_fb_format);
  2583. /**
  2584. * drm_mode_addfb - add an FB to the graphics configuration
  2585. * @dev: drm device for the ioctl
  2586. * @data: data pointer for the ioctl
  2587. * @file_priv: drm file for the ioctl call
  2588. *
  2589. * Add a new FB to the specified CRTC, given a user request. This is the
  2590. * original addfb ioctl which only supported RGB formats.
  2591. *
  2592. * Called by the user via ioctl.
  2593. *
  2594. * Returns:
  2595. * Zero on success, negative errno on failure.
  2596. */
  2597. int drm_mode_addfb(struct drm_device *dev,
  2598. void *data, struct drm_file *file_priv)
  2599. {
  2600. struct drm_mode_fb_cmd *or = data;
  2601. struct drm_mode_fb_cmd2 r = {};
  2602. int ret;
  2603. /* convert to new format and call new ioctl */
  2604. r.fb_id = or->fb_id;
  2605. r.width = or->width;
  2606. r.height = or->height;
  2607. r.pitches[0] = or->pitch;
  2608. r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
  2609. r.handles[0] = or->handle;
  2610. ret = drm_mode_addfb2(dev, &r, file_priv);
  2611. if (ret)
  2612. return ret;
  2613. or->fb_id = r.fb_id;
  2614. return 0;
  2615. }
  2616. static int format_check(const struct drm_mode_fb_cmd2 *r)
  2617. {
  2618. uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
  2619. switch (format) {
  2620. case DRM_FORMAT_C8:
  2621. case DRM_FORMAT_RGB332:
  2622. case DRM_FORMAT_BGR233:
  2623. case DRM_FORMAT_XRGB4444:
  2624. case DRM_FORMAT_XBGR4444:
  2625. case DRM_FORMAT_RGBX4444:
  2626. case DRM_FORMAT_BGRX4444:
  2627. case DRM_FORMAT_ARGB4444:
  2628. case DRM_FORMAT_ABGR4444:
  2629. case DRM_FORMAT_RGBA4444:
  2630. case DRM_FORMAT_BGRA4444:
  2631. case DRM_FORMAT_XRGB1555:
  2632. case DRM_FORMAT_XBGR1555:
  2633. case DRM_FORMAT_RGBX5551:
  2634. case DRM_FORMAT_BGRX5551:
  2635. case DRM_FORMAT_ARGB1555:
  2636. case DRM_FORMAT_ABGR1555:
  2637. case DRM_FORMAT_RGBA5551:
  2638. case DRM_FORMAT_BGRA5551:
  2639. case DRM_FORMAT_RGB565:
  2640. case DRM_FORMAT_BGR565:
  2641. case DRM_FORMAT_RGB888:
  2642. case DRM_FORMAT_BGR888:
  2643. case DRM_FORMAT_XRGB8888:
  2644. case DRM_FORMAT_XBGR8888:
  2645. case DRM_FORMAT_RGBX8888:
  2646. case DRM_FORMAT_BGRX8888:
  2647. case DRM_FORMAT_ARGB8888:
  2648. case DRM_FORMAT_ABGR8888:
  2649. case DRM_FORMAT_RGBA8888:
  2650. case DRM_FORMAT_BGRA8888:
  2651. case DRM_FORMAT_XRGB2101010:
  2652. case DRM_FORMAT_XBGR2101010:
  2653. case DRM_FORMAT_RGBX1010102:
  2654. case DRM_FORMAT_BGRX1010102:
  2655. case DRM_FORMAT_ARGB2101010:
  2656. case DRM_FORMAT_ABGR2101010:
  2657. case DRM_FORMAT_RGBA1010102:
  2658. case DRM_FORMAT_BGRA1010102:
  2659. case DRM_FORMAT_YUYV:
  2660. case DRM_FORMAT_YVYU:
  2661. case DRM_FORMAT_UYVY:
  2662. case DRM_FORMAT_VYUY:
  2663. case DRM_FORMAT_AYUV:
  2664. case DRM_FORMAT_NV12:
  2665. case DRM_FORMAT_NV21:
  2666. case DRM_FORMAT_NV16:
  2667. case DRM_FORMAT_NV61:
  2668. case DRM_FORMAT_NV24:
  2669. case DRM_FORMAT_NV42:
  2670. case DRM_FORMAT_YUV410:
  2671. case DRM_FORMAT_YVU410:
  2672. case DRM_FORMAT_YUV411:
  2673. case DRM_FORMAT_YVU411:
  2674. case DRM_FORMAT_YUV420:
  2675. case DRM_FORMAT_YVU420:
  2676. case DRM_FORMAT_YUV422:
  2677. case DRM_FORMAT_YVU422:
  2678. case DRM_FORMAT_YUV444:
  2679. case DRM_FORMAT_YVU444:
  2680. return 0;
  2681. default:
  2682. DRM_DEBUG_KMS("invalid pixel format %s\n",
  2683. drm_get_format_name(r->pixel_format));
  2684. return -EINVAL;
  2685. }
  2686. }
  2687. static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
  2688. {
  2689. int ret, hsub, vsub, num_planes, i;
  2690. ret = format_check(r);
  2691. if (ret) {
  2692. DRM_DEBUG_KMS("bad framebuffer format %s\n",
  2693. drm_get_format_name(r->pixel_format));
  2694. return ret;
  2695. }
  2696. hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
  2697. vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
  2698. num_planes = drm_format_num_planes(r->pixel_format);
  2699. if (r->width == 0 || r->width % hsub) {
  2700. DRM_DEBUG_KMS("bad framebuffer width %u\n", r->width);
  2701. return -EINVAL;
  2702. }
  2703. if (r->height == 0 || r->height % vsub) {
  2704. DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
  2705. return -EINVAL;
  2706. }
  2707. for (i = 0; i < num_planes; i++) {
  2708. unsigned int width = r->width / (i != 0 ? hsub : 1);
  2709. unsigned int height = r->height / (i != 0 ? vsub : 1);
  2710. unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
  2711. if (!r->handles[i]) {
  2712. DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
  2713. return -EINVAL;
  2714. }
  2715. if ((uint64_t) width * cpp > UINT_MAX)
  2716. return -ERANGE;
  2717. if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
  2718. return -ERANGE;
  2719. if (r->pitches[i] < width * cpp) {
  2720. DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
  2721. return -EINVAL;
  2722. }
  2723. if (r->modifier[i] && !(r->flags & DRM_MODE_FB_MODIFIERS)) {
  2724. DRM_DEBUG_KMS("bad fb modifier %llu for plane %d\n",
  2725. r->modifier[i], i);
  2726. return -EINVAL;
  2727. }
  2728. /* modifier specific checks: */
  2729. switch (r->modifier[i]) {
  2730. case DRM_FORMAT_MOD_SAMSUNG_64_32_TILE:
  2731. /* NOTE: the pitch restriction may be lifted later if it turns
  2732. * out that no hw has this restriction:
  2733. */
  2734. if (r->pixel_format != DRM_FORMAT_NV12 ||
  2735. width % 128 || height % 32 ||
  2736. r->pitches[i] % 128) {
  2737. DRM_DEBUG_KMS("bad modifier data for plane %d\n", i);
  2738. return -EINVAL;
  2739. }
  2740. break;
  2741. default:
  2742. break;
  2743. }
  2744. }
  2745. for (i = num_planes; i < 4; i++) {
  2746. if (r->modifier[i]) {
  2747. DRM_DEBUG_KMS("non-zero modifier for unused plane %d\n", i);
  2748. return -EINVAL;
  2749. }
  2750. /* Pre-FB_MODIFIERS userspace didn't clear the structs properly. */
  2751. if (!(r->flags & DRM_MODE_FB_MODIFIERS))
  2752. continue;
  2753. if (r->handles[i]) {
  2754. DRM_DEBUG_KMS("buffer object handle for unused plane %d\n", i);
  2755. return -EINVAL;
  2756. }
  2757. if (r->pitches[i]) {
  2758. DRM_DEBUG_KMS("non-zero pitch for unused plane %d\n", i);
  2759. return -EINVAL;
  2760. }
  2761. if (r->offsets[i]) {
  2762. DRM_DEBUG_KMS("non-zero offset for unused plane %d\n", i);
  2763. return -EINVAL;
  2764. }
  2765. }
  2766. return 0;
  2767. }
  2768. static struct drm_framebuffer *
  2769. internal_framebuffer_create(struct drm_device *dev,
  2770. struct drm_mode_fb_cmd2 *r,
  2771. struct drm_file *file_priv)
  2772. {
  2773. struct drm_mode_config *config = &dev->mode_config;
  2774. struct drm_framebuffer *fb;
  2775. int ret;
  2776. if (r->flags & ~(DRM_MODE_FB_INTERLACED | DRM_MODE_FB_MODIFIERS)) {
  2777. DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
  2778. return ERR_PTR(-EINVAL);
  2779. }
  2780. if ((config->min_width > r->width) || (r->width > config->max_width)) {
  2781. DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
  2782. r->width, config->min_width, config->max_width);
  2783. return ERR_PTR(-EINVAL);
  2784. }
  2785. if ((config->min_height > r->height) || (r->height > config->max_height)) {
  2786. DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
  2787. r->height, config->min_height, config->max_height);
  2788. return ERR_PTR(-EINVAL);
  2789. }
  2790. if (r->flags & DRM_MODE_FB_MODIFIERS &&
  2791. !dev->mode_config.allow_fb_modifiers) {
  2792. DRM_DEBUG_KMS("driver does not support fb modifiers\n");
  2793. return ERR_PTR(-EINVAL);
  2794. }
  2795. ret = framebuffer_check(r);
  2796. if (ret)
  2797. return ERR_PTR(ret);
  2798. fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
  2799. if (IS_ERR(fb)) {
  2800. DRM_DEBUG_KMS("could not create framebuffer\n");
  2801. return fb;
  2802. }
  2803. return fb;
  2804. }
  2805. /**
  2806. * drm_mode_addfb2 - add an FB to the graphics configuration
  2807. * @dev: drm device for the ioctl
  2808. * @data: data pointer for the ioctl
  2809. * @file_priv: drm file for the ioctl call
  2810. *
  2811. * Add a new FB to the specified CRTC, given a user request with format. This is
  2812. * the 2nd version of the addfb ioctl, which supports multi-planar framebuffers
  2813. * and uses fourcc codes as pixel format specifiers.
  2814. *
  2815. * Called by the user via ioctl.
  2816. *
  2817. * Returns:
  2818. * Zero on success, negative errno on failure.
  2819. */
  2820. int drm_mode_addfb2(struct drm_device *dev,
  2821. void *data, struct drm_file *file_priv)
  2822. {
  2823. struct drm_mode_fb_cmd2 *r = data;
  2824. struct drm_framebuffer *fb;
  2825. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2826. return -EINVAL;
  2827. fb = internal_framebuffer_create(dev, r, file_priv);
  2828. if (IS_ERR(fb))
  2829. return PTR_ERR(fb);
  2830. /* Transfer ownership to the filp for reaping on close */
  2831. DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
  2832. mutex_lock(&file_priv->fbs_lock);
  2833. r->fb_id = fb->base.id;
  2834. list_add(&fb->filp_head, &file_priv->fbs);
  2835. mutex_unlock(&file_priv->fbs_lock);
  2836. return 0;
  2837. }
  2838. /**
  2839. * drm_mode_rmfb - remove an FB from the configuration
  2840. * @dev: drm device for the ioctl
  2841. * @data: data pointer for the ioctl
  2842. * @file_priv: drm file for the ioctl call
  2843. *
  2844. * Remove the FB specified by the user.
  2845. *
  2846. * Called by the user via ioctl.
  2847. *
  2848. * Returns:
  2849. * Zero on success, negative errno on failure.
  2850. */
  2851. int drm_mode_rmfb(struct drm_device *dev,
  2852. void *data, struct drm_file *file_priv)
  2853. {
  2854. struct drm_framebuffer *fb = NULL;
  2855. struct drm_framebuffer *fbl = NULL;
  2856. uint32_t *id = data;
  2857. int found = 0;
  2858. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2859. return -EINVAL;
  2860. mutex_lock(&file_priv->fbs_lock);
  2861. mutex_lock(&dev->mode_config.fb_lock);
  2862. fb = __drm_framebuffer_lookup(dev, *id);
  2863. if (!fb)
  2864. goto fail_lookup;
  2865. list_for_each_entry(fbl, &file_priv->fbs, filp_head)
  2866. if (fb == fbl)
  2867. found = 1;
  2868. if (!found)
  2869. goto fail_lookup;
  2870. /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
  2871. __drm_framebuffer_unregister(dev, fb);
  2872. list_del_init(&fb->filp_head);
  2873. mutex_unlock(&dev->mode_config.fb_lock);
  2874. mutex_unlock(&file_priv->fbs_lock);
  2875. drm_framebuffer_remove(fb);
  2876. return 0;
  2877. fail_lookup:
  2878. mutex_unlock(&dev->mode_config.fb_lock);
  2879. mutex_unlock(&file_priv->fbs_lock);
  2880. return -ENOENT;
  2881. }
  2882. /**
  2883. * drm_mode_getfb - get FB info
  2884. * @dev: drm device for the ioctl
  2885. * @data: data pointer for the ioctl
  2886. * @file_priv: drm file for the ioctl call
  2887. *
  2888. * Lookup the FB given its ID and return info about it.
  2889. *
  2890. * Called by the user via ioctl.
  2891. *
  2892. * Returns:
  2893. * Zero on success, negative errno on failure.
  2894. */
  2895. int drm_mode_getfb(struct drm_device *dev,
  2896. void *data, struct drm_file *file_priv)
  2897. {
  2898. struct drm_mode_fb_cmd *r = data;
  2899. struct drm_framebuffer *fb;
  2900. int ret;
  2901. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2902. return -EINVAL;
  2903. fb = drm_framebuffer_lookup(dev, r->fb_id);
  2904. if (!fb)
  2905. return -ENOENT;
  2906. r->height = fb->height;
  2907. r->width = fb->width;
  2908. r->depth = fb->depth;
  2909. r->bpp = fb->bits_per_pixel;
  2910. r->pitch = fb->pitches[0];
  2911. if (fb->funcs->create_handle) {
  2912. if (file_priv->is_master || capable(CAP_SYS_ADMIN) ||
  2913. drm_is_control_client(file_priv)) {
  2914. ret = fb->funcs->create_handle(fb, file_priv,
  2915. &r->handle);
  2916. } else {
  2917. /* GET_FB() is an unprivileged ioctl so we must not
  2918. * return a buffer-handle to non-master processes! For
  2919. * backwards-compatibility reasons, we cannot make
  2920. * GET_FB() privileged, so just return an invalid handle
  2921. * for non-masters. */
  2922. r->handle = 0;
  2923. ret = 0;
  2924. }
  2925. } else {
  2926. ret = -ENODEV;
  2927. }
  2928. drm_framebuffer_unreference(fb);
  2929. return ret;
  2930. }
  2931. /**
  2932. * drm_mode_dirtyfb_ioctl - flush frontbuffer rendering on an FB
  2933. * @dev: drm device for the ioctl
  2934. * @data: data pointer for the ioctl
  2935. * @file_priv: drm file for the ioctl call
  2936. *
  2937. * Lookup the FB and flush out the damaged area supplied by userspace as a clip
  2938. * rectangle list. Generic userspace which does frontbuffer rendering must call
  2939. * this ioctl to flush out the changes on manual-update display outputs, e.g.
  2940. * usb display-link, mipi manual update panels or edp panel self refresh modes.
  2941. *
  2942. * Modesetting drivers which always update the frontbuffer do not need to
  2943. * implement the corresponding ->dirty framebuffer callback.
  2944. *
  2945. * Called by the user via ioctl.
  2946. *
  2947. * Returns:
  2948. * Zero on success, negative errno on failure.
  2949. */
  2950. int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
  2951. void *data, struct drm_file *file_priv)
  2952. {
  2953. struct drm_clip_rect __user *clips_ptr;
  2954. struct drm_clip_rect *clips = NULL;
  2955. struct drm_mode_fb_dirty_cmd *r = data;
  2956. struct drm_framebuffer *fb;
  2957. unsigned flags;
  2958. int num_clips;
  2959. int ret;
  2960. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2961. return -EINVAL;
  2962. fb = drm_framebuffer_lookup(dev, r->fb_id);
  2963. if (!fb)
  2964. return -ENOENT;
  2965. num_clips = r->num_clips;
  2966. clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
  2967. if (!num_clips != !clips_ptr) {
  2968. ret = -EINVAL;
  2969. goto out_err1;
  2970. }
  2971. flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
  2972. /* If userspace annotates copy, clips must come in pairs */
  2973. if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
  2974. ret = -EINVAL;
  2975. goto out_err1;
  2976. }
  2977. if (num_clips && clips_ptr) {
  2978. if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
  2979. ret = -EINVAL;
  2980. goto out_err1;
  2981. }
  2982. clips = kcalloc(num_clips, sizeof(*clips), GFP_KERNEL);
  2983. if (!clips) {
  2984. ret = -ENOMEM;
  2985. goto out_err1;
  2986. }
  2987. ret = copy_from_user(clips, clips_ptr,
  2988. num_clips * sizeof(*clips));
  2989. if (ret) {
  2990. ret = -EFAULT;
  2991. goto out_err2;
  2992. }
  2993. }
  2994. if (fb->funcs->dirty) {
  2995. ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
  2996. clips, num_clips);
  2997. } else {
  2998. ret = -ENOSYS;
  2999. }
  3000. out_err2:
  3001. kfree(clips);
  3002. out_err1:
  3003. drm_framebuffer_unreference(fb);
  3004. return ret;
  3005. }
  3006. /**
  3007. * drm_fb_release - remove and free the FBs on this file
  3008. * @priv: drm file for the ioctl
  3009. *
  3010. * Destroy all the FBs associated with @filp.
  3011. *
  3012. * Called by the user via ioctl.
  3013. *
  3014. * Returns:
  3015. * Zero on success, negative errno on failure.
  3016. */
  3017. void drm_fb_release(struct drm_file *priv)
  3018. {
  3019. struct drm_device *dev = priv->minor->dev;
  3020. struct drm_framebuffer *fb, *tfb;
  3021. /*
  3022. * When the file gets released that means no one else can access the fb
  3023. * list any more, so no need to grab fpriv->fbs_lock. And we need to
  3024. * avoid upsetting lockdep since the universal cursor code adds a
  3025. * framebuffer while holding mutex locks.
  3026. *
  3027. * Note that a real deadlock between fpriv->fbs_lock and the modeset
  3028. * locks is impossible here since no one else but this function can get
  3029. * at it any more.
  3030. */
  3031. list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
  3032. mutex_lock(&dev->mode_config.fb_lock);
  3033. /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
  3034. __drm_framebuffer_unregister(dev, fb);
  3035. mutex_unlock(&dev->mode_config.fb_lock);
  3036. list_del_init(&fb->filp_head);
  3037. /* This will also drop the fpriv->fbs reference. */
  3038. drm_framebuffer_remove(fb);
  3039. }
  3040. }
  3041. /**
  3042. * drm_property_create - create a new property type
  3043. * @dev: drm device
  3044. * @flags: flags specifying the property type
  3045. * @name: name of the property
  3046. * @num_values: number of pre-defined values
  3047. *
  3048. * This creates a new generic drm property which can then be attached to a drm
  3049. * object with drm_object_attach_property. The returned property object must be
  3050. * freed with drm_property_destroy.
  3051. *
  3052. * Note that the DRM core keeps a per-device list of properties and that, if
  3053. * drm_mode_config_cleanup() is called, it will destroy all properties created
  3054. * by the driver.
  3055. *
  3056. * Returns:
  3057. * A pointer to the newly created property on success, NULL on failure.
  3058. */
  3059. struct drm_property *drm_property_create(struct drm_device *dev, int flags,
  3060. const char *name, int num_values)
  3061. {
  3062. struct drm_property *property = NULL;
  3063. int ret;
  3064. property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
  3065. if (!property)
  3066. return NULL;
  3067. property->dev = dev;
  3068. if (num_values) {
  3069. property->values = kcalloc(num_values, sizeof(uint64_t),
  3070. GFP_KERNEL);
  3071. if (!property->values)
  3072. goto fail;
  3073. }
  3074. ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
  3075. if (ret)
  3076. goto fail;
  3077. property->flags = flags;
  3078. property->num_values = num_values;
  3079. INIT_LIST_HEAD(&property->enum_list);
  3080. if (name) {
  3081. strncpy(property->name, name, DRM_PROP_NAME_LEN);
  3082. property->name[DRM_PROP_NAME_LEN-1] = '\0';
  3083. }
  3084. list_add_tail(&property->head, &dev->mode_config.property_list);
  3085. WARN_ON(!drm_property_type_valid(property));
  3086. return property;
  3087. fail:
  3088. kfree(property->values);
  3089. kfree(property);
  3090. return NULL;
  3091. }
  3092. EXPORT_SYMBOL(drm_property_create);
  3093. /**
  3094. * drm_property_create_enum - create a new enumeration property type
  3095. * @dev: drm device
  3096. * @flags: flags specifying the property type
  3097. * @name: name of the property
  3098. * @props: enumeration lists with property values
  3099. * @num_values: number of pre-defined values
  3100. *
  3101. * This creates a new generic drm property which can then be attached to a drm
  3102. * object with drm_object_attach_property. The returned property object must be
  3103. * freed with drm_property_destroy.
  3104. *
  3105. * Userspace is only allowed to set one of the predefined values for enumeration
  3106. * properties.
  3107. *
  3108. * Returns:
  3109. * A pointer to the newly created property on success, NULL on failure.
  3110. */
  3111. struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
  3112. const char *name,
  3113. const struct drm_prop_enum_list *props,
  3114. int num_values)
  3115. {
  3116. struct drm_property *property;
  3117. int i, ret;
  3118. flags |= DRM_MODE_PROP_ENUM;
  3119. property = drm_property_create(dev, flags, name, num_values);
  3120. if (!property)
  3121. return NULL;
  3122. for (i = 0; i < num_values; i++) {
  3123. ret = drm_property_add_enum(property, i,
  3124. props[i].type,
  3125. props[i].name);
  3126. if (ret) {
  3127. drm_property_destroy(dev, property);
  3128. return NULL;
  3129. }
  3130. }
  3131. return property;
  3132. }
  3133. EXPORT_SYMBOL(drm_property_create_enum);
  3134. /**
  3135. * drm_property_create_bitmask - create a new bitmask property type
  3136. * @dev: drm device
  3137. * @flags: flags specifying the property type
  3138. * @name: name of the property
  3139. * @props: enumeration lists with property bitflags
  3140. * @num_props: size of the @props array
  3141. * @supported_bits: bitmask of all supported enumeration values
  3142. *
  3143. * This creates a new bitmask drm property which can then be attached to a drm
  3144. * object with drm_object_attach_property. The returned property object must be
  3145. * freed with drm_property_destroy.
  3146. *
  3147. * Compared to plain enumeration properties userspace is allowed to set any
  3148. * or'ed together combination of the predefined property bitflag values
  3149. *
  3150. * Returns:
  3151. * A pointer to the newly created property on success, NULL on failure.
  3152. */
  3153. struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
  3154. int flags, const char *name,
  3155. const struct drm_prop_enum_list *props,
  3156. int num_props,
  3157. uint64_t supported_bits)
  3158. {
  3159. struct drm_property *property;
  3160. int i, ret, index = 0;
  3161. int num_values = hweight64(supported_bits);
  3162. flags |= DRM_MODE_PROP_BITMASK;
  3163. property = drm_property_create(dev, flags, name, num_values);
  3164. if (!property)
  3165. return NULL;
  3166. for (i = 0; i < num_props; i++) {
  3167. if (!(supported_bits & (1ULL << props[i].type)))
  3168. continue;
  3169. if (WARN_ON(index >= num_values)) {
  3170. drm_property_destroy(dev, property);
  3171. return NULL;
  3172. }
  3173. ret = drm_property_add_enum(property, index++,
  3174. props[i].type,
  3175. props[i].name);
  3176. if (ret) {
  3177. drm_property_destroy(dev, property);
  3178. return NULL;
  3179. }
  3180. }
  3181. return property;
  3182. }
  3183. EXPORT_SYMBOL(drm_property_create_bitmask);
  3184. static struct drm_property *property_create_range(struct drm_device *dev,
  3185. int flags, const char *name,
  3186. uint64_t min, uint64_t max)
  3187. {
  3188. struct drm_property *property;
  3189. property = drm_property_create(dev, flags, name, 2);
  3190. if (!property)
  3191. return NULL;
  3192. property->values[0] = min;
  3193. property->values[1] = max;
  3194. return property;
  3195. }
  3196. /**
  3197. * drm_property_create_range - create a new unsigned ranged property type
  3198. * @dev: drm device
  3199. * @flags: flags specifying the property type
  3200. * @name: name of the property
  3201. * @min: minimum value of the property
  3202. * @max: maximum value of the property
  3203. *
  3204. * This creates a new generic drm property which can then be attached to a drm
  3205. * object with drm_object_attach_property. The returned property object must be
  3206. * freed with drm_property_destroy.
  3207. *
  3208. * Userspace is allowed to set any unsigned integer value in the (min, max)
  3209. * range inclusive.
  3210. *
  3211. * Returns:
  3212. * A pointer to the newly created property on success, NULL on failure.
  3213. */
  3214. struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
  3215. const char *name,
  3216. uint64_t min, uint64_t max)
  3217. {
  3218. return property_create_range(dev, DRM_MODE_PROP_RANGE | flags,
  3219. name, min, max);
  3220. }
  3221. EXPORT_SYMBOL(drm_property_create_range);
  3222. /**
  3223. * drm_property_create_signed_range - create a new signed ranged property type
  3224. * @dev: drm device
  3225. * @flags: flags specifying the property type
  3226. * @name: name of the property
  3227. * @min: minimum value of the property
  3228. * @max: maximum value of the property
  3229. *
  3230. * This creates a new generic drm property which can then be attached to a drm
  3231. * object with drm_object_attach_property. The returned property object must be
  3232. * freed with drm_property_destroy.
  3233. *
  3234. * Userspace is allowed to set any signed integer value in the (min, max)
  3235. * range inclusive.
  3236. *
  3237. * Returns:
  3238. * A pointer to the newly created property on success, NULL on failure.
  3239. */
  3240. struct drm_property *drm_property_create_signed_range(struct drm_device *dev,
  3241. int flags, const char *name,
  3242. int64_t min, int64_t max)
  3243. {
  3244. return property_create_range(dev, DRM_MODE_PROP_SIGNED_RANGE | flags,
  3245. name, I642U64(min), I642U64(max));
  3246. }
  3247. EXPORT_SYMBOL(drm_property_create_signed_range);
  3248. /**
  3249. * drm_property_create_object - create a new object property type
  3250. * @dev: drm device
  3251. * @flags: flags specifying the property type
  3252. * @name: name of the property
  3253. * @type: object type from DRM_MODE_OBJECT_* defines
  3254. *
  3255. * This creates a new generic drm property which can then be attached to a drm
  3256. * object with drm_object_attach_property. The returned property object must be
  3257. * freed with drm_property_destroy.
  3258. *
  3259. * Userspace is only allowed to set this to any property value of the given
  3260. * @type. Only useful for atomic properties, which is enforced.
  3261. *
  3262. * Returns:
  3263. * A pointer to the newly created property on success, NULL on failure.
  3264. */
  3265. struct drm_property *drm_property_create_object(struct drm_device *dev,
  3266. int flags, const char *name, uint32_t type)
  3267. {
  3268. struct drm_property *property;
  3269. flags |= DRM_MODE_PROP_OBJECT;
  3270. if (WARN_ON(!(flags & DRM_MODE_PROP_ATOMIC)))
  3271. return NULL;
  3272. property = drm_property_create(dev, flags, name, 1);
  3273. if (!property)
  3274. return NULL;
  3275. property->values[0] = type;
  3276. return property;
  3277. }
  3278. EXPORT_SYMBOL(drm_property_create_object);
  3279. /**
  3280. * drm_property_create_bool - create a new boolean property type
  3281. * @dev: drm device
  3282. * @flags: flags specifying the property type
  3283. * @name: name of the property
  3284. *
  3285. * This creates a new generic drm property which can then be attached to a drm
  3286. * object with drm_object_attach_property. The returned property object must be
  3287. * freed with drm_property_destroy.
  3288. *
  3289. * This is implemented as a ranged property with only {0, 1} as valid values.
  3290. *
  3291. * Returns:
  3292. * A pointer to the newly created property on success, NULL on failure.
  3293. */
  3294. struct drm_property *drm_property_create_bool(struct drm_device *dev, int flags,
  3295. const char *name)
  3296. {
  3297. return drm_property_create_range(dev, flags, name, 0, 1);
  3298. }
  3299. EXPORT_SYMBOL(drm_property_create_bool);
  3300. /**
  3301. * drm_property_add_enum - add a possible value to an enumeration property
  3302. * @property: enumeration property to change
  3303. * @index: index of the new enumeration
  3304. * @value: value of the new enumeration
  3305. * @name: symbolic name of the new enumeration
  3306. *
  3307. * This functions adds enumerations to a property.
  3308. *
  3309. * It's use is deprecated, drivers should use one of the more specific helpers
  3310. * to directly create the property with all enumerations already attached.
  3311. *
  3312. * Returns:
  3313. * Zero on success, error code on failure.
  3314. */
  3315. int drm_property_add_enum(struct drm_property *property, int index,
  3316. uint64_t value, const char *name)
  3317. {
  3318. struct drm_property_enum *prop_enum;
  3319. if (!(drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
  3320. drm_property_type_is(property, DRM_MODE_PROP_BITMASK)))
  3321. return -EINVAL;
  3322. /*
  3323. * Bitmask enum properties have the additional constraint of values
  3324. * from 0 to 63
  3325. */
  3326. if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK) &&
  3327. (value > 63))
  3328. return -EINVAL;
  3329. if (!list_empty(&property->enum_list)) {
  3330. list_for_each_entry(prop_enum, &property->enum_list, head) {
  3331. if (prop_enum->value == value) {
  3332. strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
  3333. prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
  3334. return 0;
  3335. }
  3336. }
  3337. }
  3338. prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
  3339. if (!prop_enum)
  3340. return -ENOMEM;
  3341. strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
  3342. prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
  3343. prop_enum->value = value;
  3344. property->values[index] = value;
  3345. list_add_tail(&prop_enum->head, &property->enum_list);
  3346. return 0;
  3347. }
  3348. EXPORT_SYMBOL(drm_property_add_enum);
  3349. /**
  3350. * drm_property_destroy - destroy a drm property
  3351. * @dev: drm device
  3352. * @property: property to destry
  3353. *
  3354. * This function frees a property including any attached resources like
  3355. * enumeration values.
  3356. */
  3357. void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
  3358. {
  3359. struct drm_property_enum *prop_enum, *pt;
  3360. list_for_each_entry_safe(prop_enum, pt, &property->enum_list, head) {
  3361. list_del(&prop_enum->head);
  3362. kfree(prop_enum);
  3363. }
  3364. if (property->num_values)
  3365. kfree(property->values);
  3366. drm_mode_object_put(dev, &property->base);
  3367. list_del(&property->head);
  3368. kfree(property);
  3369. }
  3370. EXPORT_SYMBOL(drm_property_destroy);
  3371. /**
  3372. * drm_object_attach_property - attach a property to a modeset object
  3373. * @obj: drm modeset object
  3374. * @property: property to attach
  3375. * @init_val: initial value of the property
  3376. *
  3377. * This attaches the given property to the modeset object with the given initial
  3378. * value. Currently this function cannot fail since the properties are stored in
  3379. * a statically sized array.
  3380. */
  3381. void drm_object_attach_property(struct drm_mode_object *obj,
  3382. struct drm_property *property,
  3383. uint64_t init_val)
  3384. {
  3385. int count = obj->properties->count;
  3386. if (count == DRM_OBJECT_MAX_PROPERTY) {
  3387. WARN(1, "Failed to attach object property (type: 0x%x). Please "
  3388. "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
  3389. "you see this message on the same object type.\n",
  3390. obj->type);
  3391. return;
  3392. }
  3393. obj->properties->properties[count] = property;
  3394. obj->properties->values[count] = init_val;
  3395. obj->properties->count++;
  3396. if (property->flags & DRM_MODE_PROP_ATOMIC)
  3397. obj->properties->atomic_count++;
  3398. }
  3399. EXPORT_SYMBOL(drm_object_attach_property);
  3400. /**
  3401. * drm_object_property_set_value - set the value of a property
  3402. * @obj: drm mode object to set property value for
  3403. * @property: property to set
  3404. * @val: value the property should be set to
  3405. *
  3406. * This functions sets a given property on a given object. This function only
  3407. * changes the software state of the property, it does not call into the
  3408. * driver's ->set_property callback.
  3409. *
  3410. * Returns:
  3411. * Zero on success, error code on failure.
  3412. */
  3413. int drm_object_property_set_value(struct drm_mode_object *obj,
  3414. struct drm_property *property, uint64_t val)
  3415. {
  3416. int i;
  3417. for (i = 0; i < obj->properties->count; i++) {
  3418. if (obj->properties->properties[i] == property) {
  3419. obj->properties->values[i] = val;
  3420. return 0;
  3421. }
  3422. }
  3423. return -EINVAL;
  3424. }
  3425. EXPORT_SYMBOL(drm_object_property_set_value);
  3426. /**
  3427. * drm_object_property_get_value - retrieve the value of a property
  3428. * @obj: drm mode object to get property value from
  3429. * @property: property to retrieve
  3430. * @val: storage for the property value
  3431. *
  3432. * This function retrieves the softare state of the given property for the given
  3433. * property. Since there is no driver callback to retrieve the current property
  3434. * value this might be out of sync with the hardware, depending upon the driver
  3435. * and property.
  3436. *
  3437. * Returns:
  3438. * Zero on success, error code on failure.
  3439. */
  3440. int drm_object_property_get_value(struct drm_mode_object *obj,
  3441. struct drm_property *property, uint64_t *val)
  3442. {
  3443. int i;
  3444. /* read-only properties bypass atomic mechanism and still store
  3445. * their value in obj->properties->values[].. mostly to avoid
  3446. * having to deal w/ EDID and similar props in atomic paths:
  3447. */
  3448. if (drm_core_check_feature(property->dev, DRIVER_ATOMIC) &&
  3449. !(property->flags & DRM_MODE_PROP_IMMUTABLE))
  3450. return drm_atomic_get_property(obj, property, val);
  3451. for (i = 0; i < obj->properties->count; i++) {
  3452. if (obj->properties->properties[i] == property) {
  3453. *val = obj->properties->values[i];
  3454. return 0;
  3455. }
  3456. }
  3457. return -EINVAL;
  3458. }
  3459. EXPORT_SYMBOL(drm_object_property_get_value);
  3460. /**
  3461. * drm_mode_getproperty_ioctl - get the property metadata
  3462. * @dev: DRM device
  3463. * @data: ioctl data
  3464. * @file_priv: DRM file info
  3465. *
  3466. * This function retrieves the metadata for a given property, like the different
  3467. * possible values for an enum property or the limits for a range property.
  3468. *
  3469. * Blob properties are special
  3470. *
  3471. * Called by the user via ioctl.
  3472. *
  3473. * Returns:
  3474. * Zero on success, negative errno on failure.
  3475. */
  3476. int drm_mode_getproperty_ioctl(struct drm_device *dev,
  3477. void *data, struct drm_file *file_priv)
  3478. {
  3479. struct drm_mode_get_property *out_resp = data;
  3480. struct drm_property *property;
  3481. int enum_count = 0;
  3482. int value_count = 0;
  3483. int ret = 0, i;
  3484. int copied;
  3485. struct drm_property_enum *prop_enum;
  3486. struct drm_mode_property_enum __user *enum_ptr;
  3487. uint64_t __user *values_ptr;
  3488. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  3489. return -EINVAL;
  3490. drm_modeset_lock_all(dev);
  3491. property = drm_property_find(dev, out_resp->prop_id);
  3492. if (!property) {
  3493. ret = -ENOENT;
  3494. goto done;
  3495. }
  3496. if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
  3497. drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
  3498. list_for_each_entry(prop_enum, &property->enum_list, head)
  3499. enum_count++;
  3500. }
  3501. value_count = property->num_values;
  3502. strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
  3503. out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
  3504. out_resp->flags = property->flags;
  3505. if ((out_resp->count_values >= value_count) && value_count) {
  3506. values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
  3507. for (i = 0; i < value_count; i++) {
  3508. if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
  3509. ret = -EFAULT;
  3510. goto done;
  3511. }
  3512. }
  3513. }
  3514. out_resp->count_values = value_count;
  3515. if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
  3516. drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
  3517. if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
  3518. copied = 0;
  3519. enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
  3520. list_for_each_entry(prop_enum, &property->enum_list, head) {
  3521. if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
  3522. ret = -EFAULT;
  3523. goto done;
  3524. }
  3525. if (copy_to_user(&enum_ptr[copied].name,
  3526. &prop_enum->name, DRM_PROP_NAME_LEN)) {
  3527. ret = -EFAULT;
  3528. goto done;
  3529. }
  3530. copied++;
  3531. }
  3532. }
  3533. out_resp->count_enum_blobs = enum_count;
  3534. }
  3535. /*
  3536. * NOTE: The idea seems to have been to use this to read all the blob
  3537. * property values. But nothing ever added them to the corresponding
  3538. * list, userspace always used the special-purpose get_blob ioctl to
  3539. * read the value for a blob property. It also doesn't make a lot of
  3540. * sense to return values here when everything else is just metadata for
  3541. * the property itself.
  3542. */
  3543. if (drm_property_type_is(property, DRM_MODE_PROP_BLOB))
  3544. out_resp->count_enum_blobs = 0;
  3545. done:
  3546. drm_modeset_unlock_all(dev);
  3547. return ret;
  3548. }
  3549. /**
  3550. * drm_property_create_blob - Create new blob property
  3551. *
  3552. * Creates a new blob property for a specified DRM device, optionally
  3553. * copying data.
  3554. *
  3555. * @dev: DRM device to create property for
  3556. * @length: Length to allocate for blob data
  3557. * @data: If specified, copies data into blob
  3558. *
  3559. * Returns:
  3560. * New blob property with a single reference on success, or an ERR_PTR
  3561. * value on failure.
  3562. */
  3563. struct drm_property_blob *
  3564. drm_property_create_blob(struct drm_device *dev, size_t length,
  3565. const void *data)
  3566. {
  3567. struct drm_property_blob *blob;
  3568. int ret;
  3569. if (!length)
  3570. return ERR_PTR(-EINVAL);
  3571. blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
  3572. if (!blob)
  3573. return ERR_PTR(-ENOMEM);
  3574. /* This must be explicitly initialised, so we can safely call list_del
  3575. * on it in the removal handler, even if it isn't in a file list. */
  3576. INIT_LIST_HEAD(&blob->head_file);
  3577. blob->length = length;
  3578. blob->dev = dev;
  3579. if (data)
  3580. memcpy(blob->data, data, length);
  3581. mutex_lock(&dev->mode_config.blob_lock);
  3582. ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
  3583. if (ret) {
  3584. kfree(blob);
  3585. mutex_unlock(&dev->mode_config.blob_lock);
  3586. return ERR_PTR(-EINVAL);
  3587. }
  3588. kref_init(&blob->refcount);
  3589. list_add_tail(&blob->head_global,
  3590. &dev->mode_config.property_blob_list);
  3591. mutex_unlock(&dev->mode_config.blob_lock);
  3592. return blob;
  3593. }
  3594. EXPORT_SYMBOL(drm_property_create_blob);
  3595. /**
  3596. * drm_property_free_blob - Blob property destructor
  3597. *
  3598. * Internal free function for blob properties; must not be used directly.
  3599. *
  3600. * @kref: Reference
  3601. */
  3602. static void drm_property_free_blob(struct kref *kref)
  3603. {
  3604. struct drm_property_blob *blob =
  3605. container_of(kref, struct drm_property_blob, refcount);
  3606. WARN_ON(!mutex_is_locked(&blob->dev->mode_config.blob_lock));
  3607. list_del(&blob->head_global);
  3608. list_del(&blob->head_file);
  3609. drm_mode_object_put(blob->dev, &blob->base);
  3610. kfree(blob);
  3611. }
  3612. /**
  3613. * drm_property_unreference_blob - Unreference a blob property
  3614. *
  3615. * Drop a reference on a blob property. May free the object.
  3616. *
  3617. * @blob: Pointer to blob property
  3618. */
  3619. void drm_property_unreference_blob(struct drm_property_blob *blob)
  3620. {
  3621. struct drm_device *dev;
  3622. if (!blob)
  3623. return;
  3624. dev = blob->dev;
  3625. DRM_DEBUG("%p: blob ID: %d (%d)\n", blob, blob->base.id, atomic_read(&blob->refcount.refcount));
  3626. if (kref_put_mutex(&blob->refcount, drm_property_free_blob,
  3627. &dev->mode_config.blob_lock))
  3628. mutex_unlock(&dev->mode_config.blob_lock);
  3629. else
  3630. might_lock(&dev->mode_config.blob_lock);
  3631. }
  3632. EXPORT_SYMBOL(drm_property_unreference_blob);
  3633. /**
  3634. * drm_property_unreference_blob_locked - Unreference a blob property with blob_lock held
  3635. *
  3636. * Drop a reference on a blob property. May free the object. This must be
  3637. * called with blob_lock held.
  3638. *
  3639. * @blob: Pointer to blob property
  3640. */
  3641. static void drm_property_unreference_blob_locked(struct drm_property_blob *blob)
  3642. {
  3643. if (!blob)
  3644. return;
  3645. DRM_DEBUG("%p: blob ID: %d (%d)\n", blob, blob->base.id, atomic_read(&blob->refcount.refcount));
  3646. kref_put(&blob->refcount, drm_property_free_blob);
  3647. }
  3648. /**
  3649. * drm_property_destroy_user_blobs - destroy all blobs created by this client
  3650. * @dev: DRM device
  3651. * @file_priv: destroy all blobs owned by this file handle
  3652. */
  3653. void drm_property_destroy_user_blobs(struct drm_device *dev,
  3654. struct drm_file *file_priv)
  3655. {
  3656. struct drm_property_blob *blob, *bt;
  3657. mutex_lock(&dev->mode_config.blob_lock);
  3658. list_for_each_entry_safe(blob, bt, &file_priv->blobs, head_file) {
  3659. list_del_init(&blob->head_file);
  3660. drm_property_unreference_blob_locked(blob);
  3661. }
  3662. mutex_unlock(&dev->mode_config.blob_lock);
  3663. }
  3664. /**
  3665. * drm_property_reference_blob - Take a reference on an existing property
  3666. *
  3667. * Take a new reference on an existing blob property.
  3668. *
  3669. * @blob: Pointer to blob property
  3670. */
  3671. struct drm_property_blob *drm_property_reference_blob(struct drm_property_blob *blob)
  3672. {
  3673. DRM_DEBUG("%p: blob ID: %d (%d)\n", blob, blob->base.id, atomic_read(&blob->refcount.refcount));
  3674. kref_get(&blob->refcount);
  3675. return blob;
  3676. }
  3677. EXPORT_SYMBOL(drm_property_reference_blob);
  3678. /*
  3679. * Like drm_property_lookup_blob, but does not return an additional reference.
  3680. * Must be called with blob_lock held.
  3681. */
  3682. static struct drm_property_blob *__drm_property_lookup_blob(struct drm_device *dev,
  3683. uint32_t id)
  3684. {
  3685. struct drm_mode_object *obj = NULL;
  3686. struct drm_property_blob *blob;
  3687. WARN_ON(!mutex_is_locked(&dev->mode_config.blob_lock));
  3688. mutex_lock(&dev->mode_config.idr_mutex);
  3689. obj = idr_find(&dev->mode_config.crtc_idr, id);
  3690. if (!obj || (obj->type != DRM_MODE_OBJECT_BLOB) || (obj->id != id))
  3691. blob = NULL;
  3692. else
  3693. blob = obj_to_blob(obj);
  3694. mutex_unlock(&dev->mode_config.idr_mutex);
  3695. return blob;
  3696. }
  3697. /**
  3698. * drm_property_lookup_blob - look up a blob property and take a reference
  3699. * @dev: drm device
  3700. * @id: id of the blob property
  3701. *
  3702. * If successful, this takes an additional reference to the blob property.
  3703. * callers need to make sure to eventually unreference the returned property
  3704. * again, using @drm_property_unreference_blob.
  3705. */
  3706. struct drm_property_blob *drm_property_lookup_blob(struct drm_device *dev,
  3707. uint32_t id)
  3708. {
  3709. struct drm_property_blob *blob;
  3710. mutex_lock(&dev->mode_config.blob_lock);
  3711. blob = __drm_property_lookup_blob(dev, id);
  3712. if (blob) {
  3713. if (!kref_get_unless_zero(&blob->refcount))
  3714. blob = NULL;
  3715. }
  3716. mutex_unlock(&dev->mode_config.blob_lock);
  3717. return blob;
  3718. }
  3719. EXPORT_SYMBOL(drm_property_lookup_blob);
  3720. /**
  3721. * drm_property_replace_global_blob - atomically replace existing blob property
  3722. * @dev: drm device
  3723. * @replace: location of blob property pointer to be replaced
  3724. * @length: length of data for new blob, or 0 for no data
  3725. * @data: content for new blob, or NULL for no data
  3726. * @obj_holds_id: optional object for property holding blob ID
  3727. * @prop_holds_id: optional property holding blob ID
  3728. * @return 0 on success or error on failure
  3729. *
  3730. * This function will atomically replace a global property in the blob list,
  3731. * optionally updating a property which holds the ID of that property. It is
  3732. * guaranteed to be atomic: no caller will be allowed to see intermediate
  3733. * results, and either the entire operation will succeed and clean up the
  3734. * previous property, or it will fail and the state will be unchanged.
  3735. *
  3736. * If length is 0 or data is NULL, no new blob will be created, and the holding
  3737. * property, if specified, will be set to 0.
  3738. *
  3739. * Access to the replace pointer is assumed to be protected by the caller, e.g.
  3740. * by holding the relevant modesetting object lock for its parent.
  3741. *
  3742. * For example, a drm_connector has a 'PATH' property, which contains the ID
  3743. * of a blob property with the value of the MST path information. Calling this
  3744. * function with replace pointing to the connector's path_blob_ptr, length and
  3745. * data set for the new path information, obj_holds_id set to the connector's
  3746. * base object, and prop_holds_id set to the path property name, will perform
  3747. * a completely atomic update. The access to path_blob_ptr is protected by the
  3748. * caller holding a lock on the connector.
  3749. */
  3750. static int drm_property_replace_global_blob(struct drm_device *dev,
  3751. struct drm_property_blob **replace,
  3752. size_t length,
  3753. const void *data,
  3754. struct drm_mode_object *obj_holds_id,
  3755. struct drm_property *prop_holds_id)
  3756. {
  3757. struct drm_property_blob *new_blob = NULL;
  3758. struct drm_property_blob *old_blob = NULL;
  3759. int ret;
  3760. WARN_ON(replace == NULL);
  3761. old_blob = *replace;
  3762. if (length && data) {
  3763. new_blob = drm_property_create_blob(dev, length, data);
  3764. if (IS_ERR(new_blob))
  3765. return PTR_ERR(new_blob);
  3766. }
  3767. /* This does not need to be synchronised with blob_lock, as the
  3768. * get_properties ioctl locks all modesetting objects, and
  3769. * obj_holds_id must be locked before calling here, so we cannot
  3770. * have its value out of sync with the list membership modified
  3771. * below under blob_lock. */
  3772. if (obj_holds_id) {
  3773. ret = drm_object_property_set_value(obj_holds_id,
  3774. prop_holds_id,
  3775. new_blob ?
  3776. new_blob->base.id : 0);
  3777. if (ret != 0)
  3778. goto err_created;
  3779. }
  3780. drm_property_unreference_blob(old_blob);
  3781. *replace = new_blob;
  3782. return 0;
  3783. err_created:
  3784. drm_property_unreference_blob(new_blob);
  3785. return ret;
  3786. }
  3787. /**
  3788. * drm_mode_getblob_ioctl - get the contents of a blob property value
  3789. * @dev: DRM device
  3790. * @data: ioctl data
  3791. * @file_priv: DRM file info
  3792. *
  3793. * This function retrieves the contents of a blob property. The value stored in
  3794. * an object's blob property is just a normal modeset object id.
  3795. *
  3796. * Called by the user via ioctl.
  3797. *
  3798. * Returns:
  3799. * Zero on success, negative errno on failure.
  3800. */
  3801. int drm_mode_getblob_ioctl(struct drm_device *dev,
  3802. void *data, struct drm_file *file_priv)
  3803. {
  3804. struct drm_mode_get_blob *out_resp = data;
  3805. struct drm_property_blob *blob;
  3806. int ret = 0;
  3807. void __user *blob_ptr;
  3808. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  3809. return -EINVAL;
  3810. drm_modeset_lock_all(dev);
  3811. mutex_lock(&dev->mode_config.blob_lock);
  3812. blob = __drm_property_lookup_blob(dev, out_resp->blob_id);
  3813. if (!blob) {
  3814. ret = -ENOENT;
  3815. goto done;
  3816. }
  3817. if (out_resp->length == blob->length) {
  3818. blob_ptr = (void __user *)(unsigned long)out_resp->data;
  3819. if (copy_to_user(blob_ptr, blob->data, blob->length)) {
  3820. ret = -EFAULT;
  3821. goto done;
  3822. }
  3823. }
  3824. out_resp->length = blob->length;
  3825. done:
  3826. mutex_unlock(&dev->mode_config.blob_lock);
  3827. drm_modeset_unlock_all(dev);
  3828. return ret;
  3829. }
  3830. /**
  3831. * drm_mode_createblob_ioctl - create a new blob property
  3832. * @dev: DRM device
  3833. * @data: ioctl data
  3834. * @file_priv: DRM file info
  3835. *
  3836. * This function creates a new blob property with user-defined values. In order
  3837. * to give us sensible validation and checking when creating, rather than at
  3838. * every potential use, we also require a type to be provided upfront.
  3839. *
  3840. * Called by the user via ioctl.
  3841. *
  3842. * Returns:
  3843. * Zero on success, negative errno on failure.
  3844. */
  3845. int drm_mode_createblob_ioctl(struct drm_device *dev,
  3846. void *data, struct drm_file *file_priv)
  3847. {
  3848. struct drm_mode_create_blob *out_resp = data;
  3849. struct drm_property_blob *blob;
  3850. void __user *blob_ptr;
  3851. int ret = 0;
  3852. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  3853. return -EINVAL;
  3854. blob = drm_property_create_blob(dev, out_resp->length, NULL);
  3855. if (IS_ERR(blob))
  3856. return PTR_ERR(blob);
  3857. blob_ptr = (void __user *)(unsigned long)out_resp->data;
  3858. if (copy_from_user(blob->data, blob_ptr, out_resp->length)) {
  3859. ret = -EFAULT;
  3860. goto out_blob;
  3861. }
  3862. /* Dropping the lock between create_blob and our access here is safe
  3863. * as only the same file_priv can remove the blob; at this point, it is
  3864. * not associated with any file_priv. */
  3865. mutex_lock(&dev->mode_config.blob_lock);
  3866. out_resp->blob_id = blob->base.id;
  3867. list_add_tail(&file_priv->blobs, &blob->head_file);
  3868. mutex_unlock(&dev->mode_config.blob_lock);
  3869. return 0;
  3870. out_blob:
  3871. drm_property_unreference_blob(blob);
  3872. return ret;
  3873. }
  3874. /**
  3875. * drm_mode_destroyblob_ioctl - destroy a user blob property
  3876. * @dev: DRM device
  3877. * @data: ioctl data
  3878. * @file_priv: DRM file info
  3879. *
  3880. * Destroy an existing user-defined blob property.
  3881. *
  3882. * Called by the user via ioctl.
  3883. *
  3884. * Returns:
  3885. * Zero on success, negative errno on failure.
  3886. */
  3887. int drm_mode_destroyblob_ioctl(struct drm_device *dev,
  3888. void *data, struct drm_file *file_priv)
  3889. {
  3890. struct drm_mode_destroy_blob *out_resp = data;
  3891. struct drm_property_blob *blob = NULL, *bt;
  3892. bool found = false;
  3893. int ret = 0;
  3894. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  3895. return -EINVAL;
  3896. mutex_lock(&dev->mode_config.blob_lock);
  3897. blob = __drm_property_lookup_blob(dev, out_resp->blob_id);
  3898. if (!blob) {
  3899. ret = -ENOENT;
  3900. goto err;
  3901. }
  3902. /* Ensure the property was actually created by this user. */
  3903. list_for_each_entry(bt, &file_priv->blobs, head_file) {
  3904. if (bt == blob) {
  3905. found = true;
  3906. break;
  3907. }
  3908. }
  3909. if (!found) {
  3910. ret = -EPERM;
  3911. goto err;
  3912. }
  3913. /* We must drop head_file here, because we may not be the last
  3914. * reference on the blob. */
  3915. list_del_init(&blob->head_file);
  3916. drm_property_unreference_blob_locked(blob);
  3917. mutex_unlock(&dev->mode_config.blob_lock);
  3918. return 0;
  3919. err:
  3920. mutex_unlock(&dev->mode_config.blob_lock);
  3921. return ret;
  3922. }
  3923. /**
  3924. * drm_mode_connector_set_path_property - set tile property on connector
  3925. * @connector: connector to set property on.
  3926. * @path: path to use for property; must not be NULL.
  3927. *
  3928. * This creates a property to expose to userspace to specify a
  3929. * connector path. This is mainly used for DisplayPort MST where
  3930. * connectors have a topology and we want to allow userspace to give
  3931. * them more meaningful names.
  3932. *
  3933. * Returns:
  3934. * Zero on success, negative errno on failure.
  3935. */
  3936. int drm_mode_connector_set_path_property(struct drm_connector *connector,
  3937. const char *path)
  3938. {
  3939. struct drm_device *dev = connector->dev;
  3940. int ret;
  3941. ret = drm_property_replace_global_blob(dev,
  3942. &connector->path_blob_ptr,
  3943. strlen(path) + 1,
  3944. path,
  3945. &connector->base,
  3946. dev->mode_config.path_property);
  3947. return ret;
  3948. }
  3949. EXPORT_SYMBOL(drm_mode_connector_set_path_property);
  3950. /**
  3951. * drm_mode_connector_set_tile_property - set tile property on connector
  3952. * @connector: connector to set property on.
  3953. *
  3954. * This looks up the tile information for a connector, and creates a
  3955. * property for userspace to parse if it exists. The property is of
  3956. * the form of 8 integers using ':' as a separator.
  3957. *
  3958. * Returns:
  3959. * Zero on success, errno on failure.
  3960. */
  3961. int drm_mode_connector_set_tile_property(struct drm_connector *connector)
  3962. {
  3963. struct drm_device *dev = connector->dev;
  3964. char tile[256];
  3965. int ret;
  3966. if (!connector->has_tile) {
  3967. ret = drm_property_replace_global_blob(dev,
  3968. &connector->tile_blob_ptr,
  3969. 0,
  3970. NULL,
  3971. &connector->base,
  3972. dev->mode_config.tile_property);
  3973. return ret;
  3974. }
  3975. snprintf(tile, 256, "%d:%d:%d:%d:%d:%d:%d:%d",
  3976. connector->tile_group->id, connector->tile_is_single_monitor,
  3977. connector->num_h_tile, connector->num_v_tile,
  3978. connector->tile_h_loc, connector->tile_v_loc,
  3979. connector->tile_h_size, connector->tile_v_size);
  3980. ret = drm_property_replace_global_blob(dev,
  3981. &connector->tile_blob_ptr,
  3982. strlen(tile) + 1,
  3983. tile,
  3984. &connector->base,
  3985. dev->mode_config.tile_property);
  3986. return ret;
  3987. }
  3988. EXPORT_SYMBOL(drm_mode_connector_set_tile_property);
  3989. /**
  3990. * drm_mode_connector_update_edid_property - update the edid property of a connector
  3991. * @connector: drm connector
  3992. * @edid: new value of the edid property
  3993. *
  3994. * This function creates a new blob modeset object and assigns its id to the
  3995. * connector's edid property.
  3996. *
  3997. * Returns:
  3998. * Zero on success, negative errno on failure.
  3999. */
  4000. int drm_mode_connector_update_edid_property(struct drm_connector *connector,
  4001. const struct edid *edid)
  4002. {
  4003. struct drm_device *dev = connector->dev;
  4004. size_t size = 0;
  4005. int ret;
  4006. /* ignore requests to set edid when overridden */
  4007. if (connector->override_edid)
  4008. return 0;
  4009. if (edid)
  4010. size = EDID_LENGTH * (1 + edid->extensions);
  4011. ret = drm_property_replace_global_blob(dev,
  4012. &connector->edid_blob_ptr,
  4013. size,
  4014. edid,
  4015. &connector->base,
  4016. dev->mode_config.edid_property);
  4017. return ret;
  4018. }
  4019. EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
  4020. /* Some properties could refer to dynamic refcnt'd objects, or things that
  4021. * need special locking to handle lifetime issues (ie. to ensure the prop
  4022. * value doesn't become invalid part way through the property update due to
  4023. * race). The value returned by reference via 'obj' should be passed back
  4024. * to drm_property_change_valid_put() after the property is set (and the
  4025. * object to which the property is attached has a chance to take it's own
  4026. * reference).
  4027. */
  4028. bool drm_property_change_valid_get(struct drm_property *property,
  4029. uint64_t value, struct drm_mode_object **ref)
  4030. {
  4031. int i;
  4032. if (property->flags & DRM_MODE_PROP_IMMUTABLE)
  4033. return false;
  4034. *ref = NULL;
  4035. if (drm_property_type_is(property, DRM_MODE_PROP_RANGE)) {
  4036. if (value < property->values[0] || value > property->values[1])
  4037. return false;
  4038. return true;
  4039. } else if (drm_property_type_is(property, DRM_MODE_PROP_SIGNED_RANGE)) {
  4040. int64_t svalue = U642I64(value);
  4041. if (svalue < U642I64(property->values[0]) ||
  4042. svalue > U642I64(property->values[1]))
  4043. return false;
  4044. return true;
  4045. } else if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
  4046. uint64_t valid_mask = 0;
  4047. for (i = 0; i < property->num_values; i++)
  4048. valid_mask |= (1ULL << property->values[i]);
  4049. return !(value & ~valid_mask);
  4050. } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
  4051. struct drm_property_blob *blob;
  4052. if (value == 0)
  4053. return true;
  4054. blob = drm_property_lookup_blob(property->dev, value);
  4055. if (blob) {
  4056. *ref = &blob->base;
  4057. return true;
  4058. } else {
  4059. return false;
  4060. }
  4061. } else if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
  4062. /* a zero value for an object property translates to null: */
  4063. if (value == 0)
  4064. return true;
  4065. /* handle refcnt'd objects specially: */
  4066. if (property->values[0] == DRM_MODE_OBJECT_FB) {
  4067. struct drm_framebuffer *fb;
  4068. fb = drm_framebuffer_lookup(property->dev, value);
  4069. if (fb) {
  4070. *ref = &fb->base;
  4071. return true;
  4072. } else {
  4073. return false;
  4074. }
  4075. } else {
  4076. return _object_find(property->dev, value, property->values[0]) != NULL;
  4077. }
  4078. }
  4079. for (i = 0; i < property->num_values; i++)
  4080. if (property->values[i] == value)
  4081. return true;
  4082. return false;
  4083. }
  4084. void drm_property_change_valid_put(struct drm_property *property,
  4085. struct drm_mode_object *ref)
  4086. {
  4087. if (!ref)
  4088. return;
  4089. if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
  4090. if (property->values[0] == DRM_MODE_OBJECT_FB)
  4091. drm_framebuffer_unreference(obj_to_fb(ref));
  4092. } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB))
  4093. drm_property_unreference_blob(obj_to_blob(ref));
  4094. }
  4095. /**
  4096. * drm_mode_connector_property_set_ioctl - set the current value of a connector property
  4097. * @dev: DRM device
  4098. * @data: ioctl data
  4099. * @file_priv: DRM file info
  4100. *
  4101. * This function sets the current value for a connectors's property. It also
  4102. * calls into a driver's ->set_property callback to update the hardware state
  4103. *
  4104. * Called by the user via ioctl.
  4105. *
  4106. * Returns:
  4107. * Zero on success, negative errno on failure.
  4108. */
  4109. int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
  4110. void *data, struct drm_file *file_priv)
  4111. {
  4112. struct drm_mode_connector_set_property *conn_set_prop = data;
  4113. struct drm_mode_obj_set_property obj_set_prop = {
  4114. .value = conn_set_prop->value,
  4115. .prop_id = conn_set_prop->prop_id,
  4116. .obj_id = conn_set_prop->connector_id,
  4117. .obj_type = DRM_MODE_OBJECT_CONNECTOR
  4118. };
  4119. /* It does all the locking and checking we need */
  4120. return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
  4121. }
  4122. static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
  4123. struct drm_property *property,
  4124. uint64_t value)
  4125. {
  4126. int ret = -EINVAL;
  4127. struct drm_connector *connector = obj_to_connector(obj);
  4128. /* Do DPMS ourselves */
  4129. if (property == connector->dev->mode_config.dpms_property) {
  4130. ret = 0;
  4131. if (connector->funcs->dpms)
  4132. ret = (*connector->funcs->dpms)(connector, (int)value);
  4133. } else if (connector->funcs->set_property)
  4134. ret = connector->funcs->set_property(connector, property, value);
  4135. /* store the property value if successful */
  4136. if (!ret)
  4137. drm_object_property_set_value(&connector->base, property, value);
  4138. return ret;
  4139. }
  4140. static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
  4141. struct drm_property *property,
  4142. uint64_t value)
  4143. {
  4144. int ret = -EINVAL;
  4145. struct drm_crtc *crtc = obj_to_crtc(obj);
  4146. if (crtc->funcs->set_property)
  4147. ret = crtc->funcs->set_property(crtc, property, value);
  4148. if (!ret)
  4149. drm_object_property_set_value(obj, property, value);
  4150. return ret;
  4151. }
  4152. /**
  4153. * drm_mode_plane_set_obj_prop - set the value of a property
  4154. * @plane: drm plane object to set property value for
  4155. * @property: property to set
  4156. * @value: value the property should be set to
  4157. *
  4158. * This functions sets a given property on a given plane object. This function
  4159. * calls the driver's ->set_property callback and changes the software state of
  4160. * the property if the callback succeeds.
  4161. *
  4162. * Returns:
  4163. * Zero on success, error code on failure.
  4164. */
  4165. int drm_mode_plane_set_obj_prop(struct drm_plane *plane,
  4166. struct drm_property *property,
  4167. uint64_t value)
  4168. {
  4169. int ret = -EINVAL;
  4170. struct drm_mode_object *obj = &plane->base;
  4171. if (plane->funcs->set_property)
  4172. ret = plane->funcs->set_property(plane, property, value);
  4173. if (!ret)
  4174. drm_object_property_set_value(obj, property, value);
  4175. return ret;
  4176. }
  4177. EXPORT_SYMBOL(drm_mode_plane_set_obj_prop);
  4178. /**
  4179. * drm_mode_obj_get_properties_ioctl - get the current value of a object's property
  4180. * @dev: DRM device
  4181. * @data: ioctl data
  4182. * @file_priv: DRM file info
  4183. *
  4184. * This function retrieves the current value for an object's property. Compared
  4185. * to the connector specific ioctl this one is extended to also work on crtc and
  4186. * plane objects.
  4187. *
  4188. * Called by the user via ioctl.
  4189. *
  4190. * Returns:
  4191. * Zero on success, negative errno on failure.
  4192. */
  4193. int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
  4194. struct drm_file *file_priv)
  4195. {
  4196. struct drm_mode_obj_get_properties *arg = data;
  4197. struct drm_mode_object *obj;
  4198. int ret = 0;
  4199. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  4200. return -EINVAL;
  4201. drm_modeset_lock_all(dev);
  4202. obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
  4203. if (!obj) {
  4204. ret = -ENOENT;
  4205. goto out;
  4206. }
  4207. if (!obj->properties) {
  4208. ret = -EINVAL;
  4209. goto out;
  4210. }
  4211. ret = get_properties(obj, file_priv->atomic,
  4212. (uint32_t __user *)(unsigned long)(arg->props_ptr),
  4213. (uint64_t __user *)(unsigned long)(arg->prop_values_ptr),
  4214. &arg->count_props);
  4215. out:
  4216. drm_modeset_unlock_all(dev);
  4217. return ret;
  4218. }
  4219. /**
  4220. * drm_mode_obj_set_property_ioctl - set the current value of an object's property
  4221. * @dev: DRM device
  4222. * @data: ioctl data
  4223. * @file_priv: DRM file info
  4224. *
  4225. * This function sets the current value for an object's property. It also calls
  4226. * into a driver's ->set_property callback to update the hardware state.
  4227. * Compared to the connector specific ioctl this one is extended to also work on
  4228. * crtc and plane objects.
  4229. *
  4230. * Called by the user via ioctl.
  4231. *
  4232. * Returns:
  4233. * Zero on success, negative errno on failure.
  4234. */
  4235. int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
  4236. struct drm_file *file_priv)
  4237. {
  4238. struct drm_mode_obj_set_property *arg = data;
  4239. struct drm_mode_object *arg_obj;
  4240. struct drm_mode_object *prop_obj;
  4241. struct drm_property *property;
  4242. int i, ret = -EINVAL;
  4243. struct drm_mode_object *ref;
  4244. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  4245. return -EINVAL;
  4246. drm_modeset_lock_all(dev);
  4247. arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
  4248. if (!arg_obj) {
  4249. ret = -ENOENT;
  4250. goto out;
  4251. }
  4252. if (!arg_obj->properties)
  4253. goto out;
  4254. for (i = 0; i < arg_obj->properties->count; i++)
  4255. if (arg_obj->properties->properties[i]->base.id == arg->prop_id)
  4256. break;
  4257. if (i == arg_obj->properties->count)
  4258. goto out;
  4259. prop_obj = drm_mode_object_find(dev, arg->prop_id,
  4260. DRM_MODE_OBJECT_PROPERTY);
  4261. if (!prop_obj) {
  4262. ret = -ENOENT;
  4263. goto out;
  4264. }
  4265. property = obj_to_property(prop_obj);
  4266. if (!drm_property_change_valid_get(property, arg->value, &ref))
  4267. goto out;
  4268. switch (arg_obj->type) {
  4269. case DRM_MODE_OBJECT_CONNECTOR:
  4270. ret = drm_mode_connector_set_obj_prop(arg_obj, property,
  4271. arg->value);
  4272. break;
  4273. case DRM_MODE_OBJECT_CRTC:
  4274. ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
  4275. break;
  4276. case DRM_MODE_OBJECT_PLANE:
  4277. ret = drm_mode_plane_set_obj_prop(obj_to_plane(arg_obj),
  4278. property, arg->value);
  4279. break;
  4280. }
  4281. drm_property_change_valid_put(property, ref);
  4282. out:
  4283. drm_modeset_unlock_all(dev);
  4284. return ret;
  4285. }
  4286. /**
  4287. * drm_mode_connector_attach_encoder - attach a connector to an encoder
  4288. * @connector: connector to attach
  4289. * @encoder: encoder to attach @connector to
  4290. *
  4291. * This function links up a connector to an encoder. Note that the routing
  4292. * restrictions between encoders and crtcs are exposed to userspace through the
  4293. * possible_clones and possible_crtcs bitmasks.
  4294. *
  4295. * Returns:
  4296. * Zero on success, negative errno on failure.
  4297. */
  4298. int drm_mode_connector_attach_encoder(struct drm_connector *connector,
  4299. struct drm_encoder *encoder)
  4300. {
  4301. int i;
  4302. for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
  4303. if (connector->encoder_ids[i] == 0) {
  4304. connector->encoder_ids[i] = encoder->base.id;
  4305. return 0;
  4306. }
  4307. }
  4308. return -ENOMEM;
  4309. }
  4310. EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
  4311. /**
  4312. * drm_mode_crtc_set_gamma_size - set the gamma table size
  4313. * @crtc: CRTC to set the gamma table size for
  4314. * @gamma_size: size of the gamma table
  4315. *
  4316. * Drivers which support gamma tables should set this to the supported gamma
  4317. * table size when initializing the CRTC. Currently the drm core only supports a
  4318. * fixed gamma table size.
  4319. *
  4320. * Returns:
  4321. * Zero on success, negative errno on failure.
  4322. */
  4323. int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
  4324. int gamma_size)
  4325. {
  4326. crtc->gamma_size = gamma_size;
  4327. crtc->gamma_store = kcalloc(gamma_size, sizeof(uint16_t) * 3,
  4328. GFP_KERNEL);
  4329. if (!crtc->gamma_store) {
  4330. crtc->gamma_size = 0;
  4331. return -ENOMEM;
  4332. }
  4333. return 0;
  4334. }
  4335. EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
  4336. /**
  4337. * drm_mode_gamma_set_ioctl - set the gamma table
  4338. * @dev: DRM device
  4339. * @data: ioctl data
  4340. * @file_priv: DRM file info
  4341. *
  4342. * Set the gamma table of a CRTC to the one passed in by the user. Userspace can
  4343. * inquire the required gamma table size through drm_mode_gamma_get_ioctl.
  4344. *
  4345. * Called by the user via ioctl.
  4346. *
  4347. * Returns:
  4348. * Zero on success, negative errno on failure.
  4349. */
  4350. int drm_mode_gamma_set_ioctl(struct drm_device *dev,
  4351. void *data, struct drm_file *file_priv)
  4352. {
  4353. struct drm_mode_crtc_lut *crtc_lut = data;
  4354. struct drm_crtc *crtc;
  4355. void *r_base, *g_base, *b_base;
  4356. int size;
  4357. int ret = 0;
  4358. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  4359. return -EINVAL;
  4360. drm_modeset_lock_all(dev);
  4361. crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
  4362. if (!crtc) {
  4363. ret = -ENOENT;
  4364. goto out;
  4365. }
  4366. if (crtc->funcs->gamma_set == NULL) {
  4367. ret = -ENOSYS;
  4368. goto out;
  4369. }
  4370. /* memcpy into gamma store */
  4371. if (crtc_lut->gamma_size != crtc->gamma_size) {
  4372. ret = -EINVAL;
  4373. goto out;
  4374. }
  4375. size = crtc_lut->gamma_size * (sizeof(uint16_t));
  4376. r_base = crtc->gamma_store;
  4377. if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
  4378. ret = -EFAULT;
  4379. goto out;
  4380. }
  4381. g_base = r_base + size;
  4382. if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
  4383. ret = -EFAULT;
  4384. goto out;
  4385. }
  4386. b_base = g_base + size;
  4387. if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
  4388. ret = -EFAULT;
  4389. goto out;
  4390. }
  4391. crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
  4392. out:
  4393. drm_modeset_unlock_all(dev);
  4394. return ret;
  4395. }
  4396. /**
  4397. * drm_mode_gamma_get_ioctl - get the gamma table
  4398. * @dev: DRM device
  4399. * @data: ioctl data
  4400. * @file_priv: DRM file info
  4401. *
  4402. * Copy the current gamma table into the storage provided. This also provides
  4403. * the gamma table size the driver expects, which can be used to size the
  4404. * allocated storage.
  4405. *
  4406. * Called by the user via ioctl.
  4407. *
  4408. * Returns:
  4409. * Zero on success, negative errno on failure.
  4410. */
  4411. int drm_mode_gamma_get_ioctl(struct drm_device *dev,
  4412. void *data, struct drm_file *file_priv)
  4413. {
  4414. struct drm_mode_crtc_lut *crtc_lut = data;
  4415. struct drm_crtc *crtc;
  4416. void *r_base, *g_base, *b_base;
  4417. int size;
  4418. int ret = 0;
  4419. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  4420. return -EINVAL;
  4421. drm_modeset_lock_all(dev);
  4422. crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
  4423. if (!crtc) {
  4424. ret = -ENOENT;
  4425. goto out;
  4426. }
  4427. /* memcpy into gamma store */
  4428. if (crtc_lut->gamma_size != crtc->gamma_size) {
  4429. ret = -EINVAL;
  4430. goto out;
  4431. }
  4432. size = crtc_lut->gamma_size * (sizeof(uint16_t));
  4433. r_base = crtc->gamma_store;
  4434. if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
  4435. ret = -EFAULT;
  4436. goto out;
  4437. }
  4438. g_base = r_base + size;
  4439. if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
  4440. ret = -EFAULT;
  4441. goto out;
  4442. }
  4443. b_base = g_base + size;
  4444. if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
  4445. ret = -EFAULT;
  4446. goto out;
  4447. }
  4448. out:
  4449. drm_modeset_unlock_all(dev);
  4450. return ret;
  4451. }
  4452. /**
  4453. * drm_mode_page_flip_ioctl - schedule an asynchronous fb update
  4454. * @dev: DRM device
  4455. * @data: ioctl data
  4456. * @file_priv: DRM file info
  4457. *
  4458. * This schedules an asynchronous update on a given CRTC, called page flip.
  4459. * Optionally a drm event is generated to signal the completion of the event.
  4460. * Generic drivers cannot assume that a pageflip with changed framebuffer
  4461. * properties (including driver specific metadata like tiling layout) will work,
  4462. * but some drivers support e.g. pixel format changes through the pageflip
  4463. * ioctl.
  4464. *
  4465. * Called by the user via ioctl.
  4466. *
  4467. * Returns:
  4468. * Zero on success, negative errno on failure.
  4469. */
  4470. int drm_mode_page_flip_ioctl(struct drm_device *dev,
  4471. void *data, struct drm_file *file_priv)
  4472. {
  4473. struct drm_mode_crtc_page_flip *page_flip = data;
  4474. struct drm_crtc *crtc;
  4475. struct drm_framebuffer *fb = NULL;
  4476. struct drm_pending_vblank_event *e = NULL;
  4477. unsigned long flags;
  4478. int ret = -EINVAL;
  4479. if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
  4480. page_flip->reserved != 0)
  4481. return -EINVAL;
  4482. if ((page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC) && !dev->mode_config.async_page_flip)
  4483. return -EINVAL;
  4484. crtc = drm_crtc_find(dev, page_flip->crtc_id);
  4485. if (!crtc)
  4486. return -ENOENT;
  4487. drm_modeset_lock_crtc(crtc, crtc->primary);
  4488. if (crtc->primary->fb == NULL) {
  4489. /* The framebuffer is currently unbound, presumably
  4490. * due to a hotplug event, that userspace has not
  4491. * yet discovered.
  4492. */
  4493. ret = -EBUSY;
  4494. goto out;
  4495. }
  4496. if (crtc->funcs->page_flip == NULL)
  4497. goto out;
  4498. fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
  4499. if (!fb) {
  4500. ret = -ENOENT;
  4501. goto out;
  4502. }
  4503. ret = drm_crtc_check_viewport(crtc, crtc->x, crtc->y, &crtc->mode, fb);
  4504. if (ret)
  4505. goto out;
  4506. if (crtc->primary->fb->pixel_format != fb->pixel_format) {
  4507. DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
  4508. ret = -EINVAL;
  4509. goto out;
  4510. }
  4511. if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
  4512. ret = -ENOMEM;
  4513. spin_lock_irqsave(&dev->event_lock, flags);
  4514. if (file_priv->event_space < sizeof(e->event)) {
  4515. spin_unlock_irqrestore(&dev->event_lock, flags);
  4516. goto out;
  4517. }
  4518. file_priv->event_space -= sizeof(e->event);
  4519. spin_unlock_irqrestore(&dev->event_lock, flags);
  4520. e = kzalloc(sizeof(*e), GFP_KERNEL);
  4521. if (e == NULL) {
  4522. spin_lock_irqsave(&dev->event_lock, flags);
  4523. file_priv->event_space += sizeof(e->event);
  4524. spin_unlock_irqrestore(&dev->event_lock, flags);
  4525. goto out;
  4526. }
  4527. e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
  4528. e->event.base.length = sizeof(e->event);
  4529. e->event.user_data = page_flip->user_data;
  4530. e->base.event = &e->event.base;
  4531. e->base.file_priv = file_priv;
  4532. e->base.destroy =
  4533. (void (*) (struct drm_pending_event *)) kfree;
  4534. }
  4535. crtc->primary->old_fb = crtc->primary->fb;
  4536. ret = crtc->funcs->page_flip(crtc, fb, e, page_flip->flags);
  4537. if (ret) {
  4538. if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
  4539. spin_lock_irqsave(&dev->event_lock, flags);
  4540. file_priv->event_space += sizeof(e->event);
  4541. spin_unlock_irqrestore(&dev->event_lock, flags);
  4542. kfree(e);
  4543. }
  4544. /* Keep the old fb, don't unref it. */
  4545. crtc->primary->old_fb = NULL;
  4546. } else {
  4547. crtc->primary->fb = fb;
  4548. /* Unref only the old framebuffer. */
  4549. fb = NULL;
  4550. }
  4551. out:
  4552. if (fb)
  4553. drm_framebuffer_unreference(fb);
  4554. if (crtc->primary->old_fb)
  4555. drm_framebuffer_unreference(crtc->primary->old_fb);
  4556. crtc->primary->old_fb = NULL;
  4557. drm_modeset_unlock_crtc(crtc);
  4558. return ret;
  4559. }
  4560. /**
  4561. * drm_mode_config_reset - call ->reset callbacks
  4562. * @dev: drm device
  4563. *
  4564. * This functions calls all the crtc's, encoder's and connector's ->reset
  4565. * callback. Drivers can use this in e.g. their driver load or resume code to
  4566. * reset hardware and software state.
  4567. */
  4568. void drm_mode_config_reset(struct drm_device *dev)
  4569. {
  4570. struct drm_crtc *crtc;
  4571. struct drm_plane *plane;
  4572. struct drm_encoder *encoder;
  4573. struct drm_connector *connector;
  4574. drm_for_each_plane(plane, dev)
  4575. if (plane->funcs->reset)
  4576. plane->funcs->reset(plane);
  4577. drm_for_each_crtc(crtc, dev)
  4578. if (crtc->funcs->reset)
  4579. crtc->funcs->reset(crtc);
  4580. drm_for_each_encoder(encoder, dev)
  4581. if (encoder->funcs->reset)
  4582. encoder->funcs->reset(encoder);
  4583. mutex_lock(&dev->mode_config.mutex);
  4584. drm_for_each_connector(connector, dev)
  4585. if (connector->funcs->reset)
  4586. connector->funcs->reset(connector);
  4587. mutex_unlock(&dev->mode_config.mutex);
  4588. }
  4589. EXPORT_SYMBOL(drm_mode_config_reset);
  4590. /**
  4591. * drm_mode_create_dumb_ioctl - create a dumb backing storage buffer
  4592. * @dev: DRM device
  4593. * @data: ioctl data
  4594. * @file_priv: DRM file info
  4595. *
  4596. * This creates a new dumb buffer in the driver's backing storage manager (GEM,
  4597. * TTM or something else entirely) and returns the resulting buffer handle. This
  4598. * handle can then be wrapped up into a framebuffer modeset object.
  4599. *
  4600. * Note that userspace is not allowed to use such objects for render
  4601. * acceleration - drivers must create their own private ioctls for such a use
  4602. * case.
  4603. *
  4604. * Called by the user via ioctl.
  4605. *
  4606. * Returns:
  4607. * Zero on success, negative errno on failure.
  4608. */
  4609. int drm_mode_create_dumb_ioctl(struct drm_device *dev,
  4610. void *data, struct drm_file *file_priv)
  4611. {
  4612. struct drm_mode_create_dumb *args = data;
  4613. u32 cpp, stride, size;
  4614. if (!dev->driver->dumb_create)
  4615. return -ENOSYS;
  4616. if (!args->width || !args->height || !args->bpp)
  4617. return -EINVAL;
  4618. /* overflow checks for 32bit size calculations */
  4619. /* NOTE: DIV_ROUND_UP() can overflow */
  4620. cpp = DIV_ROUND_UP(args->bpp, 8);
  4621. if (!cpp || cpp > 0xffffffffU / args->width)
  4622. return -EINVAL;
  4623. stride = cpp * args->width;
  4624. if (args->height > 0xffffffffU / stride)
  4625. return -EINVAL;
  4626. /* test for wrap-around */
  4627. size = args->height * stride;
  4628. if (PAGE_ALIGN(size) == 0)
  4629. return -EINVAL;
  4630. /*
  4631. * handle, pitch and size are output parameters. Zero them out to
  4632. * prevent drivers from accidentally using uninitialized data. Since
  4633. * not all existing userspace is clearing these fields properly we
  4634. * cannot reject IOCTL with garbage in them.
  4635. */
  4636. args->handle = 0;
  4637. args->pitch = 0;
  4638. args->size = 0;
  4639. return dev->driver->dumb_create(file_priv, dev, args);
  4640. }
  4641. /**
  4642. * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer
  4643. * @dev: DRM device
  4644. * @data: ioctl data
  4645. * @file_priv: DRM file info
  4646. *
  4647. * Allocate an offset in the drm device node's address space to be able to
  4648. * memory map a dumb buffer.
  4649. *
  4650. * Called by the user via ioctl.
  4651. *
  4652. * Returns:
  4653. * Zero on success, negative errno on failure.
  4654. */
  4655. int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
  4656. void *data, struct drm_file *file_priv)
  4657. {
  4658. struct drm_mode_map_dumb *args = data;
  4659. /* call driver ioctl to get mmap offset */
  4660. if (!dev->driver->dumb_map_offset)
  4661. return -ENOSYS;
  4662. return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
  4663. }
  4664. /**
  4665. * drm_mode_destroy_dumb_ioctl - destroy a dumb backing strage buffer
  4666. * @dev: DRM device
  4667. * @data: ioctl data
  4668. * @file_priv: DRM file info
  4669. *
  4670. * This destroys the userspace handle for the given dumb backing storage buffer.
  4671. * Since buffer objects must be reference counted in the kernel a buffer object
  4672. * won't be immediately freed if a framebuffer modeset object still uses it.
  4673. *
  4674. * Called by the user via ioctl.
  4675. *
  4676. * Returns:
  4677. * Zero on success, negative errno on failure.
  4678. */
  4679. int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
  4680. void *data, struct drm_file *file_priv)
  4681. {
  4682. struct drm_mode_destroy_dumb *args = data;
  4683. if (!dev->driver->dumb_destroy)
  4684. return -ENOSYS;
  4685. return dev->driver->dumb_destroy(file_priv, dev, args->handle);
  4686. }
  4687. /**
  4688. * drm_fb_get_bpp_depth - get the bpp/depth values for format
  4689. * @format: pixel format (DRM_FORMAT_*)
  4690. * @depth: storage for the depth value
  4691. * @bpp: storage for the bpp value
  4692. *
  4693. * This only supports RGB formats here for compat with code that doesn't use
  4694. * pixel formats directly yet.
  4695. */
  4696. void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
  4697. int *bpp)
  4698. {
  4699. switch (format) {
  4700. case DRM_FORMAT_C8:
  4701. case DRM_FORMAT_RGB332:
  4702. case DRM_FORMAT_BGR233:
  4703. *depth = 8;
  4704. *bpp = 8;
  4705. break;
  4706. case DRM_FORMAT_XRGB1555:
  4707. case DRM_FORMAT_XBGR1555:
  4708. case DRM_FORMAT_RGBX5551:
  4709. case DRM_FORMAT_BGRX5551:
  4710. case DRM_FORMAT_ARGB1555:
  4711. case DRM_FORMAT_ABGR1555:
  4712. case DRM_FORMAT_RGBA5551:
  4713. case DRM_FORMAT_BGRA5551:
  4714. *depth = 15;
  4715. *bpp = 16;
  4716. break;
  4717. case DRM_FORMAT_RGB565:
  4718. case DRM_FORMAT_BGR565:
  4719. *depth = 16;
  4720. *bpp = 16;
  4721. break;
  4722. case DRM_FORMAT_RGB888:
  4723. case DRM_FORMAT_BGR888:
  4724. *depth = 24;
  4725. *bpp = 24;
  4726. break;
  4727. case DRM_FORMAT_XRGB8888:
  4728. case DRM_FORMAT_XBGR8888:
  4729. case DRM_FORMAT_RGBX8888:
  4730. case DRM_FORMAT_BGRX8888:
  4731. *depth = 24;
  4732. *bpp = 32;
  4733. break;
  4734. case DRM_FORMAT_XRGB2101010:
  4735. case DRM_FORMAT_XBGR2101010:
  4736. case DRM_FORMAT_RGBX1010102:
  4737. case DRM_FORMAT_BGRX1010102:
  4738. case DRM_FORMAT_ARGB2101010:
  4739. case DRM_FORMAT_ABGR2101010:
  4740. case DRM_FORMAT_RGBA1010102:
  4741. case DRM_FORMAT_BGRA1010102:
  4742. *depth = 30;
  4743. *bpp = 32;
  4744. break;
  4745. case DRM_FORMAT_ARGB8888:
  4746. case DRM_FORMAT_ABGR8888:
  4747. case DRM_FORMAT_RGBA8888:
  4748. case DRM_FORMAT_BGRA8888:
  4749. *depth = 32;
  4750. *bpp = 32;
  4751. break;
  4752. default:
  4753. DRM_DEBUG_KMS("unsupported pixel format %s\n",
  4754. drm_get_format_name(format));
  4755. *depth = 0;
  4756. *bpp = 0;
  4757. break;
  4758. }
  4759. }
  4760. EXPORT_SYMBOL(drm_fb_get_bpp_depth);
  4761. /**
  4762. * drm_format_num_planes - get the number of planes for format
  4763. * @format: pixel format (DRM_FORMAT_*)
  4764. *
  4765. * Returns:
  4766. * The number of planes used by the specified pixel format.
  4767. */
  4768. int drm_format_num_planes(uint32_t format)
  4769. {
  4770. switch (format) {
  4771. case DRM_FORMAT_YUV410:
  4772. case DRM_FORMAT_YVU410:
  4773. case DRM_FORMAT_YUV411:
  4774. case DRM_FORMAT_YVU411:
  4775. case DRM_FORMAT_YUV420:
  4776. case DRM_FORMAT_YVU420:
  4777. case DRM_FORMAT_YUV422:
  4778. case DRM_FORMAT_YVU422:
  4779. case DRM_FORMAT_YUV444:
  4780. case DRM_FORMAT_YVU444:
  4781. return 3;
  4782. case DRM_FORMAT_NV12:
  4783. case DRM_FORMAT_NV21:
  4784. case DRM_FORMAT_NV16:
  4785. case DRM_FORMAT_NV61:
  4786. case DRM_FORMAT_NV24:
  4787. case DRM_FORMAT_NV42:
  4788. return 2;
  4789. default:
  4790. return 1;
  4791. }
  4792. }
  4793. EXPORT_SYMBOL(drm_format_num_planes);
  4794. /**
  4795. * drm_format_plane_cpp - determine the bytes per pixel value
  4796. * @format: pixel format (DRM_FORMAT_*)
  4797. * @plane: plane index
  4798. *
  4799. * Returns:
  4800. * The bytes per pixel value for the specified plane.
  4801. */
  4802. int drm_format_plane_cpp(uint32_t format, int plane)
  4803. {
  4804. unsigned int depth;
  4805. int bpp;
  4806. if (plane >= drm_format_num_planes(format))
  4807. return 0;
  4808. switch (format) {
  4809. case DRM_FORMAT_YUYV:
  4810. case DRM_FORMAT_YVYU:
  4811. case DRM_FORMAT_UYVY:
  4812. case DRM_FORMAT_VYUY:
  4813. return 2;
  4814. case DRM_FORMAT_NV12:
  4815. case DRM_FORMAT_NV21:
  4816. case DRM_FORMAT_NV16:
  4817. case DRM_FORMAT_NV61:
  4818. case DRM_FORMAT_NV24:
  4819. case DRM_FORMAT_NV42:
  4820. return plane ? 2 : 1;
  4821. case DRM_FORMAT_YUV410:
  4822. case DRM_FORMAT_YVU410:
  4823. case DRM_FORMAT_YUV411:
  4824. case DRM_FORMAT_YVU411:
  4825. case DRM_FORMAT_YUV420:
  4826. case DRM_FORMAT_YVU420:
  4827. case DRM_FORMAT_YUV422:
  4828. case DRM_FORMAT_YVU422:
  4829. case DRM_FORMAT_YUV444:
  4830. case DRM_FORMAT_YVU444:
  4831. return 1;
  4832. default:
  4833. drm_fb_get_bpp_depth(format, &depth, &bpp);
  4834. return bpp >> 3;
  4835. }
  4836. }
  4837. EXPORT_SYMBOL(drm_format_plane_cpp);
  4838. /**
  4839. * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
  4840. * @format: pixel format (DRM_FORMAT_*)
  4841. *
  4842. * Returns:
  4843. * The horizontal chroma subsampling factor for the
  4844. * specified pixel format.
  4845. */
  4846. int drm_format_horz_chroma_subsampling(uint32_t format)
  4847. {
  4848. switch (format) {
  4849. case DRM_FORMAT_YUV411:
  4850. case DRM_FORMAT_YVU411:
  4851. case DRM_FORMAT_YUV410:
  4852. case DRM_FORMAT_YVU410:
  4853. return 4;
  4854. case DRM_FORMAT_YUYV:
  4855. case DRM_FORMAT_YVYU:
  4856. case DRM_FORMAT_UYVY:
  4857. case DRM_FORMAT_VYUY:
  4858. case DRM_FORMAT_NV12:
  4859. case DRM_FORMAT_NV21:
  4860. case DRM_FORMAT_NV16:
  4861. case DRM_FORMAT_NV61:
  4862. case DRM_FORMAT_YUV422:
  4863. case DRM_FORMAT_YVU422:
  4864. case DRM_FORMAT_YUV420:
  4865. case DRM_FORMAT_YVU420:
  4866. return 2;
  4867. default:
  4868. return 1;
  4869. }
  4870. }
  4871. EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
  4872. /**
  4873. * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
  4874. * @format: pixel format (DRM_FORMAT_*)
  4875. *
  4876. * Returns:
  4877. * The vertical chroma subsampling factor for the
  4878. * specified pixel format.
  4879. */
  4880. int drm_format_vert_chroma_subsampling(uint32_t format)
  4881. {
  4882. switch (format) {
  4883. case DRM_FORMAT_YUV410:
  4884. case DRM_FORMAT_YVU410:
  4885. return 4;
  4886. case DRM_FORMAT_YUV420:
  4887. case DRM_FORMAT_YVU420:
  4888. case DRM_FORMAT_NV12:
  4889. case DRM_FORMAT_NV21:
  4890. return 2;
  4891. default:
  4892. return 1;
  4893. }
  4894. }
  4895. EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);
  4896. /**
  4897. * drm_rotation_simplify() - Try to simplify the rotation
  4898. * @rotation: Rotation to be simplified
  4899. * @supported_rotations: Supported rotations
  4900. *
  4901. * Attempt to simplify the rotation to a form that is supported.
  4902. * Eg. if the hardware supports everything except DRM_REFLECT_X
  4903. * one could call this function like this:
  4904. *
  4905. * drm_rotation_simplify(rotation, BIT(DRM_ROTATE_0) |
  4906. * BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_180) |
  4907. * BIT(DRM_ROTATE_270) | BIT(DRM_REFLECT_Y));
  4908. *
  4909. * to eliminate the DRM_ROTATE_X flag. Depending on what kind of
  4910. * transforms the hardware supports, this function may not
  4911. * be able to produce a supported transform, so the caller should
  4912. * check the result afterwards.
  4913. */
  4914. unsigned int drm_rotation_simplify(unsigned int rotation,
  4915. unsigned int supported_rotations)
  4916. {
  4917. if (rotation & ~supported_rotations) {
  4918. rotation ^= BIT(DRM_REFLECT_X) | BIT(DRM_REFLECT_Y);
  4919. rotation = (rotation & ~0xf) | BIT((ffs(rotation & 0xf) + 1) % 4);
  4920. }
  4921. return rotation;
  4922. }
  4923. EXPORT_SYMBOL(drm_rotation_simplify);
  4924. /**
  4925. * drm_mode_config_init - initialize DRM mode_configuration structure
  4926. * @dev: DRM device
  4927. *
  4928. * Initialize @dev's mode_config structure, used for tracking the graphics
  4929. * configuration of @dev.
  4930. *
  4931. * Since this initializes the modeset locks, no locking is possible. Which is no
  4932. * problem, since this should happen single threaded at init time. It is the
  4933. * driver's problem to ensure this guarantee.
  4934. *
  4935. */
  4936. void drm_mode_config_init(struct drm_device *dev)
  4937. {
  4938. mutex_init(&dev->mode_config.mutex);
  4939. drm_modeset_lock_init(&dev->mode_config.connection_mutex);
  4940. mutex_init(&dev->mode_config.idr_mutex);
  4941. mutex_init(&dev->mode_config.fb_lock);
  4942. mutex_init(&dev->mode_config.blob_lock);
  4943. INIT_LIST_HEAD(&dev->mode_config.fb_list);
  4944. INIT_LIST_HEAD(&dev->mode_config.crtc_list);
  4945. INIT_LIST_HEAD(&dev->mode_config.connector_list);
  4946. INIT_LIST_HEAD(&dev->mode_config.encoder_list);
  4947. INIT_LIST_HEAD(&dev->mode_config.property_list);
  4948. INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
  4949. INIT_LIST_HEAD(&dev->mode_config.plane_list);
  4950. idr_init(&dev->mode_config.crtc_idr);
  4951. idr_init(&dev->mode_config.tile_idr);
  4952. drm_modeset_lock_all(dev);
  4953. drm_mode_create_standard_properties(dev);
  4954. drm_modeset_unlock_all(dev);
  4955. /* Just to be sure */
  4956. dev->mode_config.num_fb = 0;
  4957. dev->mode_config.num_connector = 0;
  4958. dev->mode_config.num_crtc = 0;
  4959. dev->mode_config.num_encoder = 0;
  4960. dev->mode_config.num_overlay_plane = 0;
  4961. dev->mode_config.num_total_plane = 0;
  4962. }
  4963. EXPORT_SYMBOL(drm_mode_config_init);
  4964. /**
  4965. * drm_mode_config_cleanup - free up DRM mode_config info
  4966. * @dev: DRM device
  4967. *
  4968. * Free up all the connectors and CRTCs associated with this DRM device, then
  4969. * free up the framebuffers and associated buffer objects.
  4970. *
  4971. * Note that since this /should/ happen single-threaded at driver/device
  4972. * teardown time, no locking is required. It's the driver's job to ensure that
  4973. * this guarantee actually holds true.
  4974. *
  4975. * FIXME: cleanup any dangling user buffer objects too
  4976. */
  4977. void drm_mode_config_cleanup(struct drm_device *dev)
  4978. {
  4979. struct drm_connector *connector, *ot;
  4980. struct drm_crtc *crtc, *ct;
  4981. struct drm_encoder *encoder, *enct;
  4982. struct drm_framebuffer *fb, *fbt;
  4983. struct drm_property *property, *pt;
  4984. struct drm_property_blob *blob, *bt;
  4985. struct drm_plane *plane, *plt;
  4986. list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
  4987. head) {
  4988. encoder->funcs->destroy(encoder);
  4989. }
  4990. list_for_each_entry_safe(connector, ot,
  4991. &dev->mode_config.connector_list, head) {
  4992. connector->funcs->destroy(connector);
  4993. }
  4994. list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
  4995. head) {
  4996. drm_property_destroy(dev, property);
  4997. }
  4998. list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
  4999. head_global) {
  5000. drm_property_unreference_blob(blob);
  5001. }
  5002. /*
  5003. * Single-threaded teardown context, so it's not required to grab the
  5004. * fb_lock to protect against concurrent fb_list access. Contrary, it
  5005. * would actually deadlock with the drm_framebuffer_cleanup function.
  5006. *
  5007. * Also, if there are any framebuffers left, that's a driver leak now,
  5008. * so politely WARN about this.
  5009. */
  5010. WARN_ON(!list_empty(&dev->mode_config.fb_list));
  5011. list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
  5012. drm_framebuffer_remove(fb);
  5013. }
  5014. list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
  5015. head) {
  5016. plane->funcs->destroy(plane);
  5017. }
  5018. list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
  5019. crtc->funcs->destroy(crtc);
  5020. }
  5021. idr_destroy(&dev->mode_config.tile_idr);
  5022. idr_destroy(&dev->mode_config.crtc_idr);
  5023. drm_modeset_lock_fini(&dev->mode_config.connection_mutex);
  5024. }
  5025. EXPORT_SYMBOL(drm_mode_config_cleanup);
  5026. struct drm_property *drm_mode_create_rotation_property(struct drm_device *dev,
  5027. unsigned int supported_rotations)
  5028. {
  5029. static const struct drm_prop_enum_list props[] = {
  5030. { DRM_ROTATE_0, "rotate-0" },
  5031. { DRM_ROTATE_90, "rotate-90" },
  5032. { DRM_ROTATE_180, "rotate-180" },
  5033. { DRM_ROTATE_270, "rotate-270" },
  5034. { DRM_REFLECT_X, "reflect-x" },
  5035. { DRM_REFLECT_Y, "reflect-y" },
  5036. };
  5037. return drm_property_create_bitmask(dev, 0, "rotation",
  5038. props, ARRAY_SIZE(props),
  5039. supported_rotations);
  5040. }
  5041. EXPORT_SYMBOL(drm_mode_create_rotation_property);
  5042. /**
  5043. * DOC: Tile group
  5044. *
  5045. * Tile groups are used to represent tiled monitors with a unique
  5046. * integer identifier. Tiled monitors using DisplayID v1.3 have
  5047. * a unique 8-byte handle, we store this in a tile group, so we
  5048. * have a common identifier for all tiles in a monitor group.
  5049. */
  5050. static void drm_tile_group_free(struct kref *kref)
  5051. {
  5052. struct drm_tile_group *tg = container_of(kref, struct drm_tile_group, refcount);
  5053. struct drm_device *dev = tg->dev;
  5054. mutex_lock(&dev->mode_config.idr_mutex);
  5055. idr_remove(&dev->mode_config.tile_idr, tg->id);
  5056. mutex_unlock(&dev->mode_config.idr_mutex);
  5057. kfree(tg);
  5058. }
  5059. /**
  5060. * drm_mode_put_tile_group - drop a reference to a tile group.
  5061. * @dev: DRM device
  5062. * @tg: tile group to drop reference to.
  5063. *
  5064. * drop reference to tile group and free if 0.
  5065. */
  5066. void drm_mode_put_tile_group(struct drm_device *dev,
  5067. struct drm_tile_group *tg)
  5068. {
  5069. kref_put(&tg->refcount, drm_tile_group_free);
  5070. }
  5071. /**
  5072. * drm_mode_get_tile_group - get a reference to an existing tile group
  5073. * @dev: DRM device
  5074. * @topology: 8-bytes unique per monitor.
  5075. *
  5076. * Use the unique bytes to get a reference to an existing tile group.
  5077. *
  5078. * RETURNS:
  5079. * tile group or NULL if not found.
  5080. */
  5081. struct drm_tile_group *drm_mode_get_tile_group(struct drm_device *dev,
  5082. char topology[8])
  5083. {
  5084. struct drm_tile_group *tg;
  5085. int id;
  5086. mutex_lock(&dev->mode_config.idr_mutex);
  5087. idr_for_each_entry(&dev->mode_config.tile_idr, tg, id) {
  5088. if (!memcmp(tg->group_data, topology, 8)) {
  5089. if (!kref_get_unless_zero(&tg->refcount))
  5090. tg = NULL;
  5091. mutex_unlock(&dev->mode_config.idr_mutex);
  5092. return tg;
  5093. }
  5094. }
  5095. mutex_unlock(&dev->mode_config.idr_mutex);
  5096. return NULL;
  5097. }
  5098. EXPORT_SYMBOL(drm_mode_get_tile_group);
  5099. /**
  5100. * drm_mode_create_tile_group - create a tile group from a displayid description
  5101. * @dev: DRM device
  5102. * @topology: 8-bytes unique per monitor.
  5103. *
  5104. * Create a tile group for the unique monitor, and get a unique
  5105. * identifier for the tile group.
  5106. *
  5107. * RETURNS:
  5108. * new tile group or error.
  5109. */
  5110. struct drm_tile_group *drm_mode_create_tile_group(struct drm_device *dev,
  5111. char topology[8])
  5112. {
  5113. struct drm_tile_group *tg;
  5114. int ret;
  5115. tg = kzalloc(sizeof(*tg), GFP_KERNEL);
  5116. if (!tg)
  5117. return ERR_PTR(-ENOMEM);
  5118. kref_init(&tg->refcount);
  5119. memcpy(tg->group_data, topology, 8);
  5120. tg->dev = dev;
  5121. mutex_lock(&dev->mode_config.idr_mutex);
  5122. ret = idr_alloc(&dev->mode_config.tile_idr, tg, 1, 0, GFP_KERNEL);
  5123. if (ret >= 0) {
  5124. tg->id = ret;
  5125. } else {
  5126. kfree(tg);
  5127. tg = ERR_PTR(ret);
  5128. }
  5129. mutex_unlock(&dev->mode_config.idr_mutex);
  5130. return tg;
  5131. }
  5132. EXPORT_SYMBOL(drm_mode_create_tile_group);