drm_crtc.c 160 KB

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