drm_crtc.c 137 KB

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