drm_crtc.c 157 KB

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