drm_crtc.c 136 KB

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