drm_crtc.c 132 KB

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