drm_crtc.c 136 KB

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