drm_crtc.c 138 KB

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