drm_crtc.c 145 KB

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