drm_crtc.c 140 KB

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