drm_crtc.c 152 KB

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