nv50_display.c 116 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470
  1. /*
  2. * Copyright 2011 Red Hat Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * Authors: Ben Skeggs
  23. */
  24. #include <linux/dma-mapping.h>
  25. #include <drm/drmP.h>
  26. #include <drm/drm_atomic.h>
  27. #include <drm/drm_atomic_helper.h>
  28. #include <drm/drm_crtc_helper.h>
  29. #include <drm/drm_dp_helper.h>
  30. #include <drm/drm_fb_helper.h>
  31. #include <drm/drm_plane_helper.h>
  32. #include <nvif/class.h>
  33. #include <nvif/cl0002.h>
  34. #include <nvif/cl5070.h>
  35. #include <nvif/cl507a.h>
  36. #include <nvif/cl507b.h>
  37. #include <nvif/cl507c.h>
  38. #include <nvif/cl507d.h>
  39. #include <nvif/cl507e.h>
  40. #include <nvif/event.h>
  41. #include "nouveau_drv.h"
  42. #include "nouveau_dma.h"
  43. #include "nouveau_gem.h"
  44. #include "nouveau_connector.h"
  45. #include "nouveau_encoder.h"
  46. #include "nouveau_crtc.h"
  47. #include "nouveau_fence.h"
  48. #include "nouveau_fbcon.h"
  49. #include "nv50_display.h"
  50. #define EVO_DMA_NR 9
  51. #define EVO_MASTER (0x00)
  52. #define EVO_FLIP(c) (0x01 + (c))
  53. #define EVO_OVLY(c) (0x05 + (c))
  54. #define EVO_OIMM(c) (0x09 + (c))
  55. #define EVO_CURS(c) (0x0d + (c))
  56. /* offsets in shared sync bo of various structures */
  57. #define EVO_SYNC(c, o) ((c) * 0x0100 + (o))
  58. #define EVO_MAST_NTFY EVO_SYNC( 0, 0x00)
  59. #define EVO_FLIP_SEM0(c) EVO_SYNC((c) + 1, 0x00)
  60. #define EVO_FLIP_SEM1(c) EVO_SYNC((c) + 1, 0x10)
  61. #define EVO_FLIP_NTFY0(c) EVO_SYNC((c) + 1, 0x20)
  62. #define EVO_FLIP_NTFY1(c) EVO_SYNC((c) + 1, 0x30)
  63. /******************************************************************************
  64. * Atomic state
  65. *****************************************************************************/
  66. #define nv50_atom(p) container_of((p), struct nv50_atom, state)
  67. struct nv50_atom {
  68. struct drm_atomic_state state;
  69. struct list_head outp;
  70. bool lock_core;
  71. bool flush_disable;
  72. };
  73. struct nv50_outp_atom {
  74. struct list_head head;
  75. struct drm_encoder *encoder;
  76. bool flush_disable;
  77. union {
  78. struct {
  79. bool ctrl:1;
  80. };
  81. u8 mask;
  82. } clr;
  83. union {
  84. struct {
  85. bool ctrl:1;
  86. };
  87. u8 mask;
  88. } set;
  89. };
  90. #define nv50_head_atom(p) container_of((p), struct nv50_head_atom, state)
  91. struct nv50_head_atom {
  92. struct drm_crtc_state state;
  93. struct {
  94. u16 iW;
  95. u16 iH;
  96. u16 oW;
  97. u16 oH;
  98. } view;
  99. struct nv50_head_mode {
  100. bool interlace;
  101. u32 clock;
  102. struct {
  103. u16 active;
  104. u16 synce;
  105. u16 blanke;
  106. u16 blanks;
  107. } h;
  108. struct {
  109. u32 active;
  110. u16 synce;
  111. u16 blanke;
  112. u16 blanks;
  113. u16 blank2s;
  114. u16 blank2e;
  115. u16 blankus;
  116. } v;
  117. } mode;
  118. struct {
  119. u32 handle;
  120. u64 offset:40;
  121. } lut;
  122. struct {
  123. bool visible;
  124. u32 handle;
  125. u64 offset:40;
  126. u8 format;
  127. u8 kind:7;
  128. u8 layout:1;
  129. u8 block:4;
  130. u32 pitch:20;
  131. u16 x;
  132. u16 y;
  133. u16 w;
  134. u16 h;
  135. } core;
  136. struct {
  137. bool visible;
  138. u32 handle;
  139. u64 offset:40;
  140. u8 layout:1;
  141. u8 format:1;
  142. } curs;
  143. struct {
  144. u8 depth;
  145. u8 cpp;
  146. u16 x;
  147. u16 y;
  148. u16 w;
  149. u16 h;
  150. } base;
  151. struct {
  152. u8 cpp;
  153. } ovly;
  154. struct {
  155. bool enable:1;
  156. u8 bits:2;
  157. u8 mode:4;
  158. } dither;
  159. struct {
  160. struct {
  161. u16 cos:12;
  162. u16 sin:12;
  163. } sat;
  164. } procamp;
  165. union {
  166. struct {
  167. bool core:1;
  168. bool curs:1;
  169. };
  170. u8 mask;
  171. } clr;
  172. union {
  173. struct {
  174. bool core:1;
  175. bool curs:1;
  176. bool view:1;
  177. bool mode:1;
  178. bool base:1;
  179. bool ovly:1;
  180. bool dither:1;
  181. bool procamp:1;
  182. };
  183. u16 mask;
  184. } set;
  185. };
  186. static inline struct nv50_head_atom *
  187. nv50_head_atom_get(struct drm_atomic_state *state, struct drm_crtc *crtc)
  188. {
  189. struct drm_crtc_state *statec = drm_atomic_get_crtc_state(state, crtc);
  190. if (IS_ERR(statec))
  191. return (void *)statec;
  192. return nv50_head_atom(statec);
  193. }
  194. #define nv50_wndw_atom(p) container_of((p), struct nv50_wndw_atom, state)
  195. struct nv50_wndw_atom {
  196. struct drm_plane_state state;
  197. u8 interval;
  198. struct drm_rect clip;
  199. struct {
  200. u32 handle;
  201. u16 offset:12;
  202. bool awaken:1;
  203. } ntfy;
  204. struct {
  205. u32 handle;
  206. u16 offset:12;
  207. u32 acquire;
  208. u32 release;
  209. } sema;
  210. struct {
  211. u8 enable:2;
  212. } lut;
  213. struct {
  214. u8 mode:2;
  215. u8 interval:4;
  216. u8 format;
  217. u8 kind:7;
  218. u8 layout:1;
  219. u8 block:4;
  220. u32 pitch:20;
  221. u16 w;
  222. u16 h;
  223. u32 handle;
  224. u64 offset;
  225. } image;
  226. struct {
  227. u16 x;
  228. u16 y;
  229. } point;
  230. union {
  231. struct {
  232. bool ntfy:1;
  233. bool sema:1;
  234. bool image:1;
  235. };
  236. u8 mask;
  237. } clr;
  238. union {
  239. struct {
  240. bool ntfy:1;
  241. bool sema:1;
  242. bool image:1;
  243. bool lut:1;
  244. bool point:1;
  245. };
  246. u8 mask;
  247. } set;
  248. };
  249. /******************************************************************************
  250. * EVO channel
  251. *****************************************************************************/
  252. struct nv50_chan {
  253. struct nvif_object user;
  254. struct nvif_device *device;
  255. };
  256. static int
  257. nv50_chan_create(struct nvif_device *device, struct nvif_object *disp,
  258. const s32 *oclass, u8 head, void *data, u32 size,
  259. struct nv50_chan *chan)
  260. {
  261. struct nvif_sclass *sclass;
  262. int ret, i, n;
  263. chan->device = device;
  264. ret = n = nvif_object_sclass_get(disp, &sclass);
  265. if (ret < 0)
  266. return ret;
  267. while (oclass[0]) {
  268. for (i = 0; i < n; i++) {
  269. if (sclass[i].oclass == oclass[0]) {
  270. ret = nvif_object_init(disp, 0, oclass[0],
  271. data, size, &chan->user);
  272. if (ret == 0)
  273. nvif_object_map(&chan->user);
  274. nvif_object_sclass_put(&sclass);
  275. return ret;
  276. }
  277. }
  278. oclass++;
  279. }
  280. nvif_object_sclass_put(&sclass);
  281. return -ENOSYS;
  282. }
  283. static void
  284. nv50_chan_destroy(struct nv50_chan *chan)
  285. {
  286. nvif_object_fini(&chan->user);
  287. }
  288. /******************************************************************************
  289. * PIO EVO channel
  290. *****************************************************************************/
  291. struct nv50_pioc {
  292. struct nv50_chan base;
  293. };
  294. static void
  295. nv50_pioc_destroy(struct nv50_pioc *pioc)
  296. {
  297. nv50_chan_destroy(&pioc->base);
  298. }
  299. static int
  300. nv50_pioc_create(struct nvif_device *device, struct nvif_object *disp,
  301. const s32 *oclass, u8 head, void *data, u32 size,
  302. struct nv50_pioc *pioc)
  303. {
  304. return nv50_chan_create(device, disp, oclass, head, data, size,
  305. &pioc->base);
  306. }
  307. /******************************************************************************
  308. * Overlay Immediate
  309. *****************************************************************************/
  310. struct nv50_oimm {
  311. struct nv50_pioc base;
  312. };
  313. static int
  314. nv50_oimm_create(struct nvif_device *device, struct nvif_object *disp,
  315. int head, struct nv50_oimm *oimm)
  316. {
  317. struct nv50_disp_cursor_v0 args = {
  318. .head = head,
  319. };
  320. static const s32 oclass[] = {
  321. GK104_DISP_OVERLAY,
  322. GF110_DISP_OVERLAY,
  323. GT214_DISP_OVERLAY,
  324. G82_DISP_OVERLAY,
  325. NV50_DISP_OVERLAY,
  326. 0
  327. };
  328. return nv50_pioc_create(device, disp, oclass, head, &args, sizeof(args),
  329. &oimm->base);
  330. }
  331. /******************************************************************************
  332. * DMA EVO channel
  333. *****************************************************************************/
  334. struct nv50_dmac_ctxdma {
  335. struct list_head head;
  336. struct nvif_object object;
  337. };
  338. struct nv50_dmac {
  339. struct nv50_chan base;
  340. dma_addr_t handle;
  341. u32 *ptr;
  342. struct nvif_object sync;
  343. struct nvif_object vram;
  344. struct list_head ctxdma;
  345. /* Protects against concurrent pushbuf access to this channel, lock is
  346. * grabbed by evo_wait (if the pushbuf reservation is successful) and
  347. * dropped again by evo_kick. */
  348. struct mutex lock;
  349. };
  350. static void
  351. nv50_dmac_ctxdma_del(struct nv50_dmac_ctxdma *ctxdma)
  352. {
  353. nvif_object_fini(&ctxdma->object);
  354. list_del(&ctxdma->head);
  355. kfree(ctxdma);
  356. }
  357. static struct nv50_dmac_ctxdma *
  358. nv50_dmac_ctxdma_new(struct nv50_dmac *dmac, struct nouveau_framebuffer *fb)
  359. {
  360. struct nouveau_drm *drm = nouveau_drm(fb->base.dev);
  361. struct nv50_dmac_ctxdma *ctxdma;
  362. const u8 kind = (fb->nvbo->tile_flags & 0x0000ff00) >> 8;
  363. const u32 handle = 0xfb000000 | kind;
  364. struct {
  365. struct nv_dma_v0 base;
  366. union {
  367. struct nv50_dma_v0 nv50;
  368. struct gf100_dma_v0 gf100;
  369. struct gf119_dma_v0 gf119;
  370. };
  371. } args = {};
  372. u32 argc = sizeof(args.base);
  373. int ret;
  374. list_for_each_entry(ctxdma, &dmac->ctxdma, head) {
  375. if (ctxdma->object.handle == handle)
  376. return ctxdma;
  377. }
  378. if (!(ctxdma = kzalloc(sizeof(*ctxdma), GFP_KERNEL)))
  379. return ERR_PTR(-ENOMEM);
  380. list_add(&ctxdma->head, &dmac->ctxdma);
  381. args.base.target = NV_DMA_V0_TARGET_VRAM;
  382. args.base.access = NV_DMA_V0_ACCESS_RDWR;
  383. args.base.start = 0;
  384. args.base.limit = drm->client.device.info.ram_user - 1;
  385. if (drm->client.device.info.chipset < 0x80) {
  386. args.nv50.part = NV50_DMA_V0_PART_256;
  387. argc += sizeof(args.nv50);
  388. } else
  389. if (drm->client.device.info.chipset < 0xc0) {
  390. args.nv50.part = NV50_DMA_V0_PART_256;
  391. args.nv50.kind = kind;
  392. argc += sizeof(args.nv50);
  393. } else
  394. if (drm->client.device.info.chipset < 0xd0) {
  395. args.gf100.kind = kind;
  396. argc += sizeof(args.gf100);
  397. } else {
  398. args.gf119.page = GF119_DMA_V0_PAGE_LP;
  399. args.gf119.kind = kind;
  400. argc += sizeof(args.gf119);
  401. }
  402. ret = nvif_object_init(&dmac->base.user, handle, NV_DMA_IN_MEMORY,
  403. &args, argc, &ctxdma->object);
  404. if (ret) {
  405. nv50_dmac_ctxdma_del(ctxdma);
  406. return ERR_PTR(ret);
  407. }
  408. return ctxdma;
  409. }
  410. static void
  411. nv50_dmac_destroy(struct nv50_dmac *dmac, struct nvif_object *disp)
  412. {
  413. struct nvif_device *device = dmac->base.device;
  414. struct nv50_dmac_ctxdma *ctxdma, *ctxtmp;
  415. list_for_each_entry_safe(ctxdma, ctxtmp, &dmac->ctxdma, head) {
  416. nv50_dmac_ctxdma_del(ctxdma);
  417. }
  418. nvif_object_fini(&dmac->vram);
  419. nvif_object_fini(&dmac->sync);
  420. nv50_chan_destroy(&dmac->base);
  421. if (dmac->ptr) {
  422. struct device *dev = nvxx_device(device)->dev;
  423. dma_free_coherent(dev, PAGE_SIZE, dmac->ptr, dmac->handle);
  424. }
  425. }
  426. static int
  427. nv50_dmac_create(struct nvif_device *device, struct nvif_object *disp,
  428. const s32 *oclass, u8 head, void *data, u32 size, u64 syncbuf,
  429. struct nv50_dmac *dmac)
  430. {
  431. struct nv50_disp_core_channel_dma_v0 *args = data;
  432. struct nvif_object pushbuf;
  433. int ret;
  434. mutex_init(&dmac->lock);
  435. dmac->ptr = dma_alloc_coherent(nvxx_device(device)->dev, PAGE_SIZE,
  436. &dmac->handle, GFP_KERNEL);
  437. if (!dmac->ptr)
  438. return -ENOMEM;
  439. ret = nvif_object_init(&device->object, 0, NV_DMA_FROM_MEMORY,
  440. &(struct nv_dma_v0) {
  441. .target = NV_DMA_V0_TARGET_PCI_US,
  442. .access = NV_DMA_V0_ACCESS_RD,
  443. .start = dmac->handle + 0x0000,
  444. .limit = dmac->handle + 0x0fff,
  445. }, sizeof(struct nv_dma_v0), &pushbuf);
  446. if (ret)
  447. return ret;
  448. args->pushbuf = nvif_handle(&pushbuf);
  449. ret = nv50_chan_create(device, disp, oclass, head, data, size,
  450. &dmac->base);
  451. nvif_object_fini(&pushbuf);
  452. if (ret)
  453. return ret;
  454. ret = nvif_object_init(&dmac->base.user, 0xf0000000, NV_DMA_IN_MEMORY,
  455. &(struct nv_dma_v0) {
  456. .target = NV_DMA_V0_TARGET_VRAM,
  457. .access = NV_DMA_V0_ACCESS_RDWR,
  458. .start = syncbuf + 0x0000,
  459. .limit = syncbuf + 0x0fff,
  460. }, sizeof(struct nv_dma_v0),
  461. &dmac->sync);
  462. if (ret)
  463. return ret;
  464. ret = nvif_object_init(&dmac->base.user, 0xf0000001, NV_DMA_IN_MEMORY,
  465. &(struct nv_dma_v0) {
  466. .target = NV_DMA_V0_TARGET_VRAM,
  467. .access = NV_DMA_V0_ACCESS_RDWR,
  468. .start = 0,
  469. .limit = device->info.ram_user - 1,
  470. }, sizeof(struct nv_dma_v0),
  471. &dmac->vram);
  472. if (ret)
  473. return ret;
  474. INIT_LIST_HEAD(&dmac->ctxdma);
  475. return ret;
  476. }
  477. /******************************************************************************
  478. * Core
  479. *****************************************************************************/
  480. struct nv50_mast {
  481. struct nv50_dmac base;
  482. };
  483. static int
  484. nv50_core_create(struct nvif_device *device, struct nvif_object *disp,
  485. u64 syncbuf, struct nv50_mast *core)
  486. {
  487. struct nv50_disp_core_channel_dma_v0 args = {
  488. .pushbuf = 0xb0007d00,
  489. };
  490. static const s32 oclass[] = {
  491. GP102_DISP_CORE_CHANNEL_DMA,
  492. GP100_DISP_CORE_CHANNEL_DMA,
  493. GM200_DISP_CORE_CHANNEL_DMA,
  494. GM107_DISP_CORE_CHANNEL_DMA,
  495. GK110_DISP_CORE_CHANNEL_DMA,
  496. GK104_DISP_CORE_CHANNEL_DMA,
  497. GF110_DISP_CORE_CHANNEL_DMA,
  498. GT214_DISP_CORE_CHANNEL_DMA,
  499. GT206_DISP_CORE_CHANNEL_DMA,
  500. GT200_DISP_CORE_CHANNEL_DMA,
  501. G82_DISP_CORE_CHANNEL_DMA,
  502. NV50_DISP_CORE_CHANNEL_DMA,
  503. 0
  504. };
  505. return nv50_dmac_create(device, disp, oclass, 0, &args, sizeof(args),
  506. syncbuf, &core->base);
  507. }
  508. /******************************************************************************
  509. * Base
  510. *****************************************************************************/
  511. struct nv50_sync {
  512. struct nv50_dmac base;
  513. u32 addr;
  514. u32 data;
  515. };
  516. static int
  517. nv50_base_create(struct nvif_device *device, struct nvif_object *disp,
  518. int head, u64 syncbuf, struct nv50_sync *base)
  519. {
  520. struct nv50_disp_base_channel_dma_v0 args = {
  521. .pushbuf = 0xb0007c00 | head,
  522. .head = head,
  523. };
  524. static const s32 oclass[] = {
  525. GK110_DISP_BASE_CHANNEL_DMA,
  526. GK104_DISP_BASE_CHANNEL_DMA,
  527. GF110_DISP_BASE_CHANNEL_DMA,
  528. GT214_DISP_BASE_CHANNEL_DMA,
  529. GT200_DISP_BASE_CHANNEL_DMA,
  530. G82_DISP_BASE_CHANNEL_DMA,
  531. NV50_DISP_BASE_CHANNEL_DMA,
  532. 0
  533. };
  534. return nv50_dmac_create(device, disp, oclass, head, &args, sizeof(args),
  535. syncbuf, &base->base);
  536. }
  537. /******************************************************************************
  538. * Overlay
  539. *****************************************************************************/
  540. struct nv50_ovly {
  541. struct nv50_dmac base;
  542. };
  543. static int
  544. nv50_ovly_create(struct nvif_device *device, struct nvif_object *disp,
  545. int head, u64 syncbuf, struct nv50_ovly *ovly)
  546. {
  547. struct nv50_disp_overlay_channel_dma_v0 args = {
  548. .pushbuf = 0xb0007e00 | head,
  549. .head = head,
  550. };
  551. static const s32 oclass[] = {
  552. GK104_DISP_OVERLAY_CONTROL_DMA,
  553. GF110_DISP_OVERLAY_CONTROL_DMA,
  554. GT214_DISP_OVERLAY_CHANNEL_DMA,
  555. GT200_DISP_OVERLAY_CHANNEL_DMA,
  556. G82_DISP_OVERLAY_CHANNEL_DMA,
  557. NV50_DISP_OVERLAY_CHANNEL_DMA,
  558. 0
  559. };
  560. return nv50_dmac_create(device, disp, oclass, head, &args, sizeof(args),
  561. syncbuf, &ovly->base);
  562. }
  563. struct nv50_head {
  564. struct nouveau_crtc base;
  565. struct nv50_ovly ovly;
  566. struct nv50_oimm oimm;
  567. };
  568. #define nv50_head(c) ((struct nv50_head *)nouveau_crtc(c))
  569. #define nv50_ovly(c) (&nv50_head(c)->ovly)
  570. #define nv50_oimm(c) (&nv50_head(c)->oimm)
  571. #define nv50_chan(c) (&(c)->base.base)
  572. #define nv50_vers(c) nv50_chan(c)->user.oclass
  573. struct nv50_disp {
  574. struct nvif_object *disp;
  575. struct nv50_mast mast;
  576. struct nouveau_bo *sync;
  577. struct mutex mutex;
  578. };
  579. static struct nv50_disp *
  580. nv50_disp(struct drm_device *dev)
  581. {
  582. return nouveau_display(dev)->priv;
  583. }
  584. #define nv50_mast(d) (&nv50_disp(d)->mast)
  585. /******************************************************************************
  586. * EVO channel helpers
  587. *****************************************************************************/
  588. static u32 *
  589. evo_wait(void *evoc, int nr)
  590. {
  591. struct nv50_dmac *dmac = evoc;
  592. struct nvif_device *device = dmac->base.device;
  593. u32 put = nvif_rd32(&dmac->base.user, 0x0000) / 4;
  594. mutex_lock(&dmac->lock);
  595. if (put + nr >= (PAGE_SIZE / 4) - 8) {
  596. dmac->ptr[put] = 0x20000000;
  597. nvif_wr32(&dmac->base.user, 0x0000, 0x00000000);
  598. if (nvif_msec(device, 2000,
  599. if (!nvif_rd32(&dmac->base.user, 0x0004))
  600. break;
  601. ) < 0) {
  602. mutex_unlock(&dmac->lock);
  603. pr_err("nouveau: evo channel stalled\n");
  604. return NULL;
  605. }
  606. put = 0;
  607. }
  608. return dmac->ptr + put;
  609. }
  610. static void
  611. evo_kick(u32 *push, void *evoc)
  612. {
  613. struct nv50_dmac *dmac = evoc;
  614. nvif_wr32(&dmac->base.user, 0x0000, (push - dmac->ptr) << 2);
  615. mutex_unlock(&dmac->lock);
  616. }
  617. #define evo_mthd(p, m, s) do { \
  618. const u32 _m = (m), _s = (s); \
  619. if (drm_debug & DRM_UT_KMS) \
  620. pr_err("%04x %d %s\n", _m, _s, __func__); \
  621. *((p)++) = ((_s << 18) | _m); \
  622. } while(0)
  623. #define evo_data(p, d) do { \
  624. const u32 _d = (d); \
  625. if (drm_debug & DRM_UT_KMS) \
  626. pr_err("\t%08x\n", _d); \
  627. *((p)++) = _d; \
  628. } while(0)
  629. /******************************************************************************
  630. * Plane
  631. *****************************************************************************/
  632. #define nv50_wndw(p) container_of((p), struct nv50_wndw, plane)
  633. struct nv50_wndw {
  634. const struct nv50_wndw_func *func;
  635. struct nv50_dmac *dmac;
  636. struct drm_plane plane;
  637. struct nvif_notify notify;
  638. u16 ntfy;
  639. u16 sema;
  640. u32 data;
  641. };
  642. struct nv50_wndw_func {
  643. void *(*dtor)(struct nv50_wndw *);
  644. int (*acquire)(struct nv50_wndw *, struct nv50_wndw_atom *asyw,
  645. struct nv50_head_atom *asyh);
  646. void (*release)(struct nv50_wndw *, struct nv50_wndw_atom *asyw,
  647. struct nv50_head_atom *asyh);
  648. void (*prepare)(struct nv50_wndw *, struct nv50_head_atom *asyh,
  649. struct nv50_wndw_atom *asyw);
  650. void (*sema_set)(struct nv50_wndw *, struct nv50_wndw_atom *);
  651. void (*sema_clr)(struct nv50_wndw *);
  652. void (*ntfy_set)(struct nv50_wndw *, struct nv50_wndw_atom *);
  653. void (*ntfy_clr)(struct nv50_wndw *);
  654. int (*ntfy_wait_begun)(struct nv50_wndw *, struct nv50_wndw_atom *);
  655. void (*image_set)(struct nv50_wndw *, struct nv50_wndw_atom *);
  656. void (*image_clr)(struct nv50_wndw *);
  657. void (*lut)(struct nv50_wndw *, struct nv50_wndw_atom *);
  658. void (*point)(struct nv50_wndw *, struct nv50_wndw_atom *);
  659. u32 (*update)(struct nv50_wndw *, u32 interlock);
  660. };
  661. static int
  662. nv50_wndw_wait_armed(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw)
  663. {
  664. if (asyw->set.ntfy)
  665. return wndw->func->ntfy_wait_begun(wndw, asyw);
  666. return 0;
  667. }
  668. static u32
  669. nv50_wndw_flush_clr(struct nv50_wndw *wndw, u32 interlock, bool flush,
  670. struct nv50_wndw_atom *asyw)
  671. {
  672. if (asyw->clr.sema && (!asyw->set.sema || flush))
  673. wndw->func->sema_clr(wndw);
  674. if (asyw->clr.ntfy && (!asyw->set.ntfy || flush))
  675. wndw->func->ntfy_clr(wndw);
  676. if (asyw->clr.image && (!asyw->set.image || flush))
  677. wndw->func->image_clr(wndw);
  678. return flush ? wndw->func->update(wndw, interlock) : 0;
  679. }
  680. static u32
  681. nv50_wndw_flush_set(struct nv50_wndw *wndw, u32 interlock,
  682. struct nv50_wndw_atom *asyw)
  683. {
  684. if (interlock) {
  685. asyw->image.mode = 0;
  686. asyw->image.interval = 1;
  687. }
  688. if (asyw->set.sema ) wndw->func->sema_set (wndw, asyw);
  689. if (asyw->set.ntfy ) wndw->func->ntfy_set (wndw, asyw);
  690. if (asyw->set.image) wndw->func->image_set(wndw, asyw);
  691. if (asyw->set.lut ) wndw->func->lut (wndw, asyw);
  692. if (asyw->set.point) wndw->func->point (wndw, asyw);
  693. return wndw->func->update(wndw, interlock);
  694. }
  695. static void
  696. nv50_wndw_atomic_check_release(struct nv50_wndw *wndw,
  697. struct nv50_wndw_atom *asyw,
  698. struct nv50_head_atom *asyh)
  699. {
  700. struct nouveau_drm *drm = nouveau_drm(wndw->plane.dev);
  701. NV_ATOMIC(drm, "%s release\n", wndw->plane.name);
  702. wndw->func->release(wndw, asyw, asyh);
  703. asyw->ntfy.handle = 0;
  704. asyw->sema.handle = 0;
  705. }
  706. static int
  707. nv50_wndw_atomic_check_acquire(struct nv50_wndw *wndw,
  708. struct nv50_wndw_atom *asyw,
  709. struct nv50_head_atom *asyh)
  710. {
  711. struct nouveau_framebuffer *fb = nouveau_framebuffer(asyw->state.fb);
  712. struct nouveau_drm *drm = nouveau_drm(wndw->plane.dev);
  713. int ret;
  714. NV_ATOMIC(drm, "%s acquire\n", wndw->plane.name);
  715. asyw->clip.x1 = 0;
  716. asyw->clip.y1 = 0;
  717. asyw->clip.x2 = asyh->state.mode.hdisplay;
  718. asyw->clip.y2 = asyh->state.mode.vdisplay;
  719. asyw->image.w = fb->base.width;
  720. asyw->image.h = fb->base.height;
  721. asyw->image.kind = (fb->nvbo->tile_flags & 0x0000ff00) >> 8;
  722. if (asyh->state.pageflip_flags & DRM_MODE_PAGE_FLIP_ASYNC)
  723. asyw->interval = 0;
  724. else
  725. asyw->interval = 1;
  726. if (asyw->image.kind) {
  727. asyw->image.layout = 0;
  728. if (drm->client.device.info.chipset >= 0xc0)
  729. asyw->image.block = fb->nvbo->tile_mode >> 4;
  730. else
  731. asyw->image.block = fb->nvbo->tile_mode;
  732. asyw->image.pitch = (fb->base.pitches[0] / 4) << 4;
  733. } else {
  734. asyw->image.layout = 1;
  735. asyw->image.block = 0;
  736. asyw->image.pitch = fb->base.pitches[0];
  737. }
  738. ret = wndw->func->acquire(wndw, asyw, asyh);
  739. if (ret)
  740. return ret;
  741. if (asyw->set.image) {
  742. if (!(asyw->image.mode = asyw->interval ? 0 : 1))
  743. asyw->image.interval = asyw->interval;
  744. else
  745. asyw->image.interval = 0;
  746. }
  747. return 0;
  748. }
  749. static int
  750. nv50_wndw_atomic_check(struct drm_plane *plane, struct drm_plane_state *state)
  751. {
  752. struct nouveau_drm *drm = nouveau_drm(plane->dev);
  753. struct nv50_wndw *wndw = nv50_wndw(plane);
  754. struct nv50_wndw_atom *armw = nv50_wndw_atom(wndw->plane.state);
  755. struct nv50_wndw_atom *asyw = nv50_wndw_atom(state);
  756. struct nv50_head_atom *harm = NULL, *asyh = NULL;
  757. bool varm = false, asyv = false, asym = false;
  758. int ret;
  759. NV_ATOMIC(drm, "%s atomic_check\n", plane->name);
  760. if (asyw->state.crtc) {
  761. asyh = nv50_head_atom_get(asyw->state.state, asyw->state.crtc);
  762. if (IS_ERR(asyh))
  763. return PTR_ERR(asyh);
  764. asym = drm_atomic_crtc_needs_modeset(&asyh->state);
  765. asyv = asyh->state.active;
  766. }
  767. if (armw->state.crtc) {
  768. harm = nv50_head_atom_get(asyw->state.state, armw->state.crtc);
  769. if (IS_ERR(harm))
  770. return PTR_ERR(harm);
  771. varm = harm->state.crtc->state->active;
  772. }
  773. if (asyv) {
  774. asyw->point.x = asyw->state.crtc_x;
  775. asyw->point.y = asyw->state.crtc_y;
  776. if (memcmp(&armw->point, &asyw->point, sizeof(asyw->point)))
  777. asyw->set.point = true;
  778. ret = nv50_wndw_atomic_check_acquire(wndw, asyw, asyh);
  779. if (ret)
  780. return ret;
  781. } else
  782. if (varm) {
  783. nv50_wndw_atomic_check_release(wndw, asyw, harm);
  784. } else {
  785. return 0;
  786. }
  787. if (!asyv || asym) {
  788. asyw->clr.ntfy = armw->ntfy.handle != 0;
  789. asyw->clr.sema = armw->sema.handle != 0;
  790. if (wndw->func->image_clr)
  791. asyw->clr.image = armw->image.handle != 0;
  792. asyw->set.lut = wndw->func->lut && asyv;
  793. }
  794. return 0;
  795. }
  796. static void
  797. nv50_wndw_cleanup_fb(struct drm_plane *plane, struct drm_plane_state *old_state)
  798. {
  799. struct nouveau_framebuffer *fb = nouveau_framebuffer(old_state->fb);
  800. struct nouveau_drm *drm = nouveau_drm(plane->dev);
  801. NV_ATOMIC(drm, "%s cleanup: %p\n", plane->name, old_state->fb);
  802. if (!old_state->fb)
  803. return;
  804. nouveau_bo_unpin(fb->nvbo);
  805. }
  806. static int
  807. nv50_wndw_prepare_fb(struct drm_plane *plane, struct drm_plane_state *state)
  808. {
  809. struct nouveau_framebuffer *fb = nouveau_framebuffer(state->fb);
  810. struct nouveau_drm *drm = nouveau_drm(plane->dev);
  811. struct nv50_wndw *wndw = nv50_wndw(plane);
  812. struct nv50_wndw_atom *asyw = nv50_wndw_atom(state);
  813. struct nv50_head_atom *asyh;
  814. struct nv50_dmac_ctxdma *ctxdma;
  815. int ret;
  816. NV_ATOMIC(drm, "%s prepare: %p\n", plane->name, state->fb);
  817. if (!asyw->state.fb)
  818. return 0;
  819. ret = nouveau_bo_pin(fb->nvbo, TTM_PL_FLAG_VRAM, true);
  820. if (ret)
  821. return ret;
  822. ctxdma = nv50_dmac_ctxdma_new(wndw->dmac, fb);
  823. if (IS_ERR(ctxdma)) {
  824. nouveau_bo_unpin(fb->nvbo);
  825. return PTR_ERR(ctxdma);
  826. }
  827. asyw->state.fence = reservation_object_get_excl_rcu(fb->nvbo->bo.resv);
  828. asyw->image.handle = ctxdma->object.handle;
  829. asyw->image.offset = fb->nvbo->bo.offset;
  830. if (wndw->func->prepare) {
  831. asyh = nv50_head_atom_get(asyw->state.state, asyw->state.crtc);
  832. if (IS_ERR(asyh))
  833. return PTR_ERR(asyh);
  834. wndw->func->prepare(wndw, asyh, asyw);
  835. }
  836. return 0;
  837. }
  838. static const struct drm_plane_helper_funcs
  839. nv50_wndw_helper = {
  840. .prepare_fb = nv50_wndw_prepare_fb,
  841. .cleanup_fb = nv50_wndw_cleanup_fb,
  842. .atomic_check = nv50_wndw_atomic_check,
  843. };
  844. static void
  845. nv50_wndw_atomic_destroy_state(struct drm_plane *plane,
  846. struct drm_plane_state *state)
  847. {
  848. struct nv50_wndw_atom *asyw = nv50_wndw_atom(state);
  849. __drm_atomic_helper_plane_destroy_state(&asyw->state);
  850. kfree(asyw);
  851. }
  852. static struct drm_plane_state *
  853. nv50_wndw_atomic_duplicate_state(struct drm_plane *plane)
  854. {
  855. struct nv50_wndw_atom *armw = nv50_wndw_atom(plane->state);
  856. struct nv50_wndw_atom *asyw;
  857. if (!(asyw = kmalloc(sizeof(*asyw), GFP_KERNEL)))
  858. return NULL;
  859. __drm_atomic_helper_plane_duplicate_state(plane, &asyw->state);
  860. asyw->interval = 1;
  861. asyw->sema = armw->sema;
  862. asyw->ntfy = armw->ntfy;
  863. asyw->image = armw->image;
  864. asyw->point = armw->point;
  865. asyw->lut = armw->lut;
  866. asyw->clr.mask = 0;
  867. asyw->set.mask = 0;
  868. return &asyw->state;
  869. }
  870. static void
  871. nv50_wndw_reset(struct drm_plane *plane)
  872. {
  873. struct nv50_wndw_atom *asyw;
  874. if (WARN_ON(!(asyw = kzalloc(sizeof(*asyw), GFP_KERNEL))))
  875. return;
  876. if (plane->state)
  877. plane->funcs->atomic_destroy_state(plane, plane->state);
  878. plane->state = &asyw->state;
  879. plane->state->plane = plane;
  880. plane->state->rotation = DRM_ROTATE_0;
  881. }
  882. static void
  883. nv50_wndw_destroy(struct drm_plane *plane)
  884. {
  885. struct nv50_wndw *wndw = nv50_wndw(plane);
  886. void *data;
  887. nvif_notify_fini(&wndw->notify);
  888. data = wndw->func->dtor(wndw);
  889. drm_plane_cleanup(&wndw->plane);
  890. kfree(data);
  891. }
  892. static const struct drm_plane_funcs
  893. nv50_wndw = {
  894. .update_plane = drm_atomic_helper_update_plane,
  895. .disable_plane = drm_atomic_helper_disable_plane,
  896. .destroy = nv50_wndw_destroy,
  897. .reset = nv50_wndw_reset,
  898. .set_property = drm_atomic_helper_plane_set_property,
  899. .atomic_duplicate_state = nv50_wndw_atomic_duplicate_state,
  900. .atomic_destroy_state = nv50_wndw_atomic_destroy_state,
  901. };
  902. static void
  903. nv50_wndw_fini(struct nv50_wndw *wndw)
  904. {
  905. nvif_notify_put(&wndw->notify);
  906. }
  907. static void
  908. nv50_wndw_init(struct nv50_wndw *wndw)
  909. {
  910. nvif_notify_get(&wndw->notify);
  911. }
  912. static int
  913. nv50_wndw_ctor(const struct nv50_wndw_func *func, struct drm_device *dev,
  914. enum drm_plane_type type, const char *name, int index,
  915. struct nv50_dmac *dmac, const u32 *format, int nformat,
  916. struct nv50_wndw *wndw)
  917. {
  918. int ret;
  919. wndw->func = func;
  920. wndw->dmac = dmac;
  921. ret = drm_universal_plane_init(dev, &wndw->plane, 0, &nv50_wndw, format,
  922. nformat, type, "%s-%d", name, index);
  923. if (ret)
  924. return ret;
  925. drm_plane_helper_add(&wndw->plane, &nv50_wndw_helper);
  926. return 0;
  927. }
  928. /******************************************************************************
  929. * Cursor plane
  930. *****************************************************************************/
  931. #define nv50_curs(p) container_of((p), struct nv50_curs, wndw)
  932. struct nv50_curs {
  933. struct nv50_wndw wndw;
  934. struct nvif_object chan;
  935. };
  936. static u32
  937. nv50_curs_update(struct nv50_wndw *wndw, u32 interlock)
  938. {
  939. struct nv50_curs *curs = nv50_curs(wndw);
  940. nvif_wr32(&curs->chan, 0x0080, 0x00000000);
  941. return 0;
  942. }
  943. static void
  944. nv50_curs_point(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw)
  945. {
  946. struct nv50_curs *curs = nv50_curs(wndw);
  947. nvif_wr32(&curs->chan, 0x0084, (asyw->point.y << 16) | asyw->point.x);
  948. }
  949. static void
  950. nv50_curs_prepare(struct nv50_wndw *wndw, struct nv50_head_atom *asyh,
  951. struct nv50_wndw_atom *asyw)
  952. {
  953. u32 handle = nv50_disp(wndw->plane.dev)->mast.base.vram.handle;
  954. u32 offset = asyw->image.offset;
  955. if (asyh->curs.handle != handle || asyh->curs.offset != offset) {
  956. asyh->curs.handle = handle;
  957. asyh->curs.offset = offset;
  958. asyh->set.curs = asyh->curs.visible;
  959. }
  960. }
  961. static void
  962. nv50_curs_release(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw,
  963. struct nv50_head_atom *asyh)
  964. {
  965. asyh->curs.visible = false;
  966. }
  967. static int
  968. nv50_curs_acquire(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw,
  969. struct nv50_head_atom *asyh)
  970. {
  971. int ret;
  972. ret = drm_plane_helper_check_state(&asyw->state, &asyw->clip,
  973. DRM_PLANE_HELPER_NO_SCALING,
  974. DRM_PLANE_HELPER_NO_SCALING,
  975. true, true);
  976. asyh->curs.visible = asyw->state.visible;
  977. if (ret || !asyh->curs.visible)
  978. return ret;
  979. switch (asyw->state.fb->width) {
  980. case 32: asyh->curs.layout = 0; break;
  981. case 64: asyh->curs.layout = 1; break;
  982. default:
  983. return -EINVAL;
  984. }
  985. if (asyw->state.fb->width != asyw->state.fb->height)
  986. return -EINVAL;
  987. switch (asyw->state.fb->format->format) {
  988. case DRM_FORMAT_ARGB8888: asyh->curs.format = 1; break;
  989. default:
  990. WARN_ON(1);
  991. return -EINVAL;
  992. }
  993. return 0;
  994. }
  995. static void *
  996. nv50_curs_dtor(struct nv50_wndw *wndw)
  997. {
  998. struct nv50_curs *curs = nv50_curs(wndw);
  999. nvif_object_fini(&curs->chan);
  1000. return curs;
  1001. }
  1002. static const u32
  1003. nv50_curs_format[] = {
  1004. DRM_FORMAT_ARGB8888,
  1005. };
  1006. static const struct nv50_wndw_func
  1007. nv50_curs = {
  1008. .dtor = nv50_curs_dtor,
  1009. .acquire = nv50_curs_acquire,
  1010. .release = nv50_curs_release,
  1011. .prepare = nv50_curs_prepare,
  1012. .point = nv50_curs_point,
  1013. .update = nv50_curs_update,
  1014. };
  1015. static int
  1016. nv50_curs_new(struct nouveau_drm *drm, struct nv50_head *head,
  1017. struct nv50_curs **pcurs)
  1018. {
  1019. static const struct nvif_mclass curses[] = {
  1020. { GK104_DISP_CURSOR, 0 },
  1021. { GF110_DISP_CURSOR, 0 },
  1022. { GT214_DISP_CURSOR, 0 },
  1023. { G82_DISP_CURSOR, 0 },
  1024. { NV50_DISP_CURSOR, 0 },
  1025. {}
  1026. };
  1027. struct nv50_disp_cursor_v0 args = {
  1028. .head = head->base.index,
  1029. };
  1030. struct nv50_disp *disp = nv50_disp(drm->dev);
  1031. struct nv50_curs *curs;
  1032. int cid, ret;
  1033. cid = nvif_mclass(disp->disp, curses);
  1034. if (cid < 0) {
  1035. NV_ERROR(drm, "No supported cursor immediate class\n");
  1036. return cid;
  1037. }
  1038. if (!(curs = *pcurs = kzalloc(sizeof(*curs), GFP_KERNEL)))
  1039. return -ENOMEM;
  1040. ret = nv50_wndw_ctor(&nv50_curs, drm->dev, DRM_PLANE_TYPE_CURSOR,
  1041. "curs", head->base.index, &disp->mast.base,
  1042. nv50_curs_format, ARRAY_SIZE(nv50_curs_format),
  1043. &curs->wndw);
  1044. if (ret) {
  1045. kfree(curs);
  1046. return ret;
  1047. }
  1048. ret = nvif_object_init(disp->disp, 0, curses[cid].oclass, &args,
  1049. sizeof(args), &curs->chan);
  1050. if (ret) {
  1051. NV_ERROR(drm, "curs%04x allocation failed: %d\n",
  1052. curses[cid].oclass, ret);
  1053. return ret;
  1054. }
  1055. return 0;
  1056. }
  1057. /******************************************************************************
  1058. * Primary plane
  1059. *****************************************************************************/
  1060. #define nv50_base(p) container_of((p), struct nv50_base, wndw)
  1061. struct nv50_base {
  1062. struct nv50_wndw wndw;
  1063. struct nv50_sync chan;
  1064. int id;
  1065. };
  1066. static int
  1067. nv50_base_notify(struct nvif_notify *notify)
  1068. {
  1069. return NVIF_NOTIFY_KEEP;
  1070. }
  1071. static void
  1072. nv50_base_lut(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw)
  1073. {
  1074. struct nv50_base *base = nv50_base(wndw);
  1075. u32 *push;
  1076. if ((push = evo_wait(&base->chan, 2))) {
  1077. evo_mthd(push, 0x00e0, 1);
  1078. evo_data(push, asyw->lut.enable << 30);
  1079. evo_kick(push, &base->chan);
  1080. }
  1081. }
  1082. static void
  1083. nv50_base_image_clr(struct nv50_wndw *wndw)
  1084. {
  1085. struct nv50_base *base = nv50_base(wndw);
  1086. u32 *push;
  1087. if ((push = evo_wait(&base->chan, 4))) {
  1088. evo_mthd(push, 0x0084, 1);
  1089. evo_data(push, 0x00000000);
  1090. evo_mthd(push, 0x00c0, 1);
  1091. evo_data(push, 0x00000000);
  1092. evo_kick(push, &base->chan);
  1093. }
  1094. }
  1095. static void
  1096. nv50_base_image_set(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw)
  1097. {
  1098. struct nv50_base *base = nv50_base(wndw);
  1099. const s32 oclass = base->chan.base.base.user.oclass;
  1100. u32 *push;
  1101. if ((push = evo_wait(&base->chan, 10))) {
  1102. evo_mthd(push, 0x0084, 1);
  1103. evo_data(push, (asyw->image.mode << 8) |
  1104. (asyw->image.interval << 4));
  1105. evo_mthd(push, 0x00c0, 1);
  1106. evo_data(push, asyw->image.handle);
  1107. if (oclass < G82_DISP_BASE_CHANNEL_DMA) {
  1108. evo_mthd(push, 0x0800, 5);
  1109. evo_data(push, asyw->image.offset >> 8);
  1110. evo_data(push, 0x00000000);
  1111. evo_data(push, (asyw->image.h << 16) | asyw->image.w);
  1112. evo_data(push, (asyw->image.layout << 20) |
  1113. asyw->image.pitch |
  1114. asyw->image.block);
  1115. evo_data(push, (asyw->image.kind << 16) |
  1116. (asyw->image.format << 8));
  1117. } else
  1118. if (oclass < GF110_DISP_BASE_CHANNEL_DMA) {
  1119. evo_mthd(push, 0x0800, 5);
  1120. evo_data(push, asyw->image.offset >> 8);
  1121. evo_data(push, 0x00000000);
  1122. evo_data(push, (asyw->image.h << 16) | asyw->image.w);
  1123. evo_data(push, (asyw->image.layout << 20) |
  1124. asyw->image.pitch |
  1125. asyw->image.block);
  1126. evo_data(push, asyw->image.format << 8);
  1127. } else {
  1128. evo_mthd(push, 0x0400, 5);
  1129. evo_data(push, asyw->image.offset >> 8);
  1130. evo_data(push, 0x00000000);
  1131. evo_data(push, (asyw->image.h << 16) | asyw->image.w);
  1132. evo_data(push, (asyw->image.layout << 24) |
  1133. asyw->image.pitch |
  1134. asyw->image.block);
  1135. evo_data(push, asyw->image.format << 8);
  1136. }
  1137. evo_kick(push, &base->chan);
  1138. }
  1139. }
  1140. static void
  1141. nv50_base_ntfy_clr(struct nv50_wndw *wndw)
  1142. {
  1143. struct nv50_base *base = nv50_base(wndw);
  1144. u32 *push;
  1145. if ((push = evo_wait(&base->chan, 2))) {
  1146. evo_mthd(push, 0x00a4, 1);
  1147. evo_data(push, 0x00000000);
  1148. evo_kick(push, &base->chan);
  1149. }
  1150. }
  1151. static void
  1152. nv50_base_ntfy_set(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw)
  1153. {
  1154. struct nv50_base *base = nv50_base(wndw);
  1155. u32 *push;
  1156. if ((push = evo_wait(&base->chan, 3))) {
  1157. evo_mthd(push, 0x00a0, 2);
  1158. evo_data(push, (asyw->ntfy.awaken << 30) | asyw->ntfy.offset);
  1159. evo_data(push, asyw->ntfy.handle);
  1160. evo_kick(push, &base->chan);
  1161. }
  1162. }
  1163. static void
  1164. nv50_base_sema_clr(struct nv50_wndw *wndw)
  1165. {
  1166. struct nv50_base *base = nv50_base(wndw);
  1167. u32 *push;
  1168. if ((push = evo_wait(&base->chan, 2))) {
  1169. evo_mthd(push, 0x0094, 1);
  1170. evo_data(push, 0x00000000);
  1171. evo_kick(push, &base->chan);
  1172. }
  1173. }
  1174. static void
  1175. nv50_base_sema_set(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw)
  1176. {
  1177. struct nv50_base *base = nv50_base(wndw);
  1178. u32 *push;
  1179. if ((push = evo_wait(&base->chan, 5))) {
  1180. evo_mthd(push, 0x0088, 4);
  1181. evo_data(push, asyw->sema.offset);
  1182. evo_data(push, asyw->sema.acquire);
  1183. evo_data(push, asyw->sema.release);
  1184. evo_data(push, asyw->sema.handle);
  1185. evo_kick(push, &base->chan);
  1186. }
  1187. }
  1188. static u32
  1189. nv50_base_update(struct nv50_wndw *wndw, u32 interlock)
  1190. {
  1191. struct nv50_base *base = nv50_base(wndw);
  1192. u32 *push;
  1193. if (!(push = evo_wait(&base->chan, 2)))
  1194. return 0;
  1195. evo_mthd(push, 0x0080, 1);
  1196. evo_data(push, interlock);
  1197. evo_kick(push, &base->chan);
  1198. if (base->chan.base.base.user.oclass < GF110_DISP_BASE_CHANNEL_DMA)
  1199. return interlock ? 2 << (base->id * 8) : 0;
  1200. return interlock ? 2 << (base->id * 4) : 0;
  1201. }
  1202. static int
  1203. nv50_base_ntfy_wait_begun(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw)
  1204. {
  1205. struct nouveau_drm *drm = nouveau_drm(wndw->plane.dev);
  1206. struct nv50_disp *disp = nv50_disp(wndw->plane.dev);
  1207. if (nvif_msec(&drm->client.device, 2000ULL,
  1208. u32 data = nouveau_bo_rd32(disp->sync, asyw->ntfy.offset / 4);
  1209. if ((data & 0xc0000000) == 0x40000000)
  1210. break;
  1211. usleep_range(1, 2);
  1212. ) < 0)
  1213. return -ETIMEDOUT;
  1214. return 0;
  1215. }
  1216. static void
  1217. nv50_base_release(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw,
  1218. struct nv50_head_atom *asyh)
  1219. {
  1220. asyh->base.cpp = 0;
  1221. }
  1222. static int
  1223. nv50_base_acquire(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw,
  1224. struct nv50_head_atom *asyh)
  1225. {
  1226. const struct drm_framebuffer *fb = asyw->state.fb;
  1227. int ret;
  1228. if (!fb->format->depth)
  1229. return -EINVAL;
  1230. ret = drm_plane_helper_check_state(&asyw->state, &asyw->clip,
  1231. DRM_PLANE_HELPER_NO_SCALING,
  1232. DRM_PLANE_HELPER_NO_SCALING,
  1233. false, true);
  1234. if (ret)
  1235. return ret;
  1236. asyh->base.depth = fb->format->depth;
  1237. asyh->base.cpp = fb->format->cpp[0];
  1238. asyh->base.x = asyw->state.src.x1 >> 16;
  1239. asyh->base.y = asyw->state.src.y1 >> 16;
  1240. asyh->base.w = asyw->state.fb->width;
  1241. asyh->base.h = asyw->state.fb->height;
  1242. switch (fb->format->format) {
  1243. case DRM_FORMAT_C8 : asyw->image.format = 0x1e; break;
  1244. case DRM_FORMAT_RGB565 : asyw->image.format = 0xe8; break;
  1245. case DRM_FORMAT_XRGB1555 :
  1246. case DRM_FORMAT_ARGB1555 : asyw->image.format = 0xe9; break;
  1247. case DRM_FORMAT_XRGB8888 :
  1248. case DRM_FORMAT_ARGB8888 : asyw->image.format = 0xcf; break;
  1249. case DRM_FORMAT_XBGR2101010:
  1250. case DRM_FORMAT_ABGR2101010: asyw->image.format = 0xd1; break;
  1251. case DRM_FORMAT_XBGR8888 :
  1252. case DRM_FORMAT_ABGR8888 : asyw->image.format = 0xd5; break;
  1253. default:
  1254. WARN_ON(1);
  1255. return -EINVAL;
  1256. }
  1257. asyw->lut.enable = 1;
  1258. asyw->set.image = true;
  1259. return 0;
  1260. }
  1261. static void *
  1262. nv50_base_dtor(struct nv50_wndw *wndw)
  1263. {
  1264. struct nv50_disp *disp = nv50_disp(wndw->plane.dev);
  1265. struct nv50_base *base = nv50_base(wndw);
  1266. nv50_dmac_destroy(&base->chan.base, disp->disp);
  1267. return base;
  1268. }
  1269. static const u32
  1270. nv50_base_format[] = {
  1271. DRM_FORMAT_C8,
  1272. DRM_FORMAT_RGB565,
  1273. DRM_FORMAT_XRGB1555,
  1274. DRM_FORMAT_ARGB1555,
  1275. DRM_FORMAT_XRGB8888,
  1276. DRM_FORMAT_ARGB8888,
  1277. DRM_FORMAT_XBGR2101010,
  1278. DRM_FORMAT_ABGR2101010,
  1279. DRM_FORMAT_XBGR8888,
  1280. DRM_FORMAT_ABGR8888,
  1281. };
  1282. static const struct nv50_wndw_func
  1283. nv50_base = {
  1284. .dtor = nv50_base_dtor,
  1285. .acquire = nv50_base_acquire,
  1286. .release = nv50_base_release,
  1287. .sema_set = nv50_base_sema_set,
  1288. .sema_clr = nv50_base_sema_clr,
  1289. .ntfy_set = nv50_base_ntfy_set,
  1290. .ntfy_clr = nv50_base_ntfy_clr,
  1291. .ntfy_wait_begun = nv50_base_ntfy_wait_begun,
  1292. .image_set = nv50_base_image_set,
  1293. .image_clr = nv50_base_image_clr,
  1294. .lut = nv50_base_lut,
  1295. .update = nv50_base_update,
  1296. };
  1297. static int
  1298. nv50_base_new(struct nouveau_drm *drm, struct nv50_head *head,
  1299. struct nv50_base **pbase)
  1300. {
  1301. struct nv50_disp *disp = nv50_disp(drm->dev);
  1302. struct nv50_base *base;
  1303. int ret;
  1304. if (!(base = *pbase = kzalloc(sizeof(*base), GFP_KERNEL)))
  1305. return -ENOMEM;
  1306. base->id = head->base.index;
  1307. base->wndw.ntfy = EVO_FLIP_NTFY0(base->id);
  1308. base->wndw.sema = EVO_FLIP_SEM0(base->id);
  1309. base->wndw.data = 0x00000000;
  1310. ret = nv50_wndw_ctor(&nv50_base, drm->dev, DRM_PLANE_TYPE_PRIMARY,
  1311. "base", base->id, &base->chan.base,
  1312. nv50_base_format, ARRAY_SIZE(nv50_base_format),
  1313. &base->wndw);
  1314. if (ret) {
  1315. kfree(base);
  1316. return ret;
  1317. }
  1318. ret = nv50_base_create(&drm->client.device, disp->disp, base->id,
  1319. disp->sync->bo.offset, &base->chan);
  1320. if (ret)
  1321. return ret;
  1322. return nvif_notify_init(&base->chan.base.base.user, nv50_base_notify,
  1323. false,
  1324. NV50_DISP_BASE_CHANNEL_DMA_V0_NTFY_UEVENT,
  1325. &(struct nvif_notify_uevent_req) {},
  1326. sizeof(struct nvif_notify_uevent_req),
  1327. sizeof(struct nvif_notify_uevent_rep),
  1328. &base->wndw.notify);
  1329. }
  1330. /******************************************************************************
  1331. * Head
  1332. *****************************************************************************/
  1333. static void
  1334. nv50_head_procamp(struct nv50_head *head, struct nv50_head_atom *asyh)
  1335. {
  1336. struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->mast.base;
  1337. u32 *push;
  1338. if ((push = evo_wait(core, 2))) {
  1339. if (core->base.user.oclass < GF110_DISP_CORE_CHANNEL_DMA)
  1340. evo_mthd(push, 0x08a8 + (head->base.index * 0x400), 1);
  1341. else
  1342. evo_mthd(push, 0x0498 + (head->base.index * 0x300), 1);
  1343. evo_data(push, (asyh->procamp.sat.sin << 20) |
  1344. (asyh->procamp.sat.cos << 8));
  1345. evo_kick(push, core);
  1346. }
  1347. }
  1348. static void
  1349. nv50_head_dither(struct nv50_head *head, struct nv50_head_atom *asyh)
  1350. {
  1351. struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->mast.base;
  1352. u32 *push;
  1353. if ((push = evo_wait(core, 2))) {
  1354. if (core->base.user.oclass < GF110_DISP_CORE_CHANNEL_DMA)
  1355. evo_mthd(push, 0x08a0 + (head->base.index * 0x0400), 1);
  1356. else
  1357. if (core->base.user.oclass < GK104_DISP_CORE_CHANNEL_DMA)
  1358. evo_mthd(push, 0x0490 + (head->base.index * 0x0300), 1);
  1359. else
  1360. evo_mthd(push, 0x04a0 + (head->base.index * 0x0300), 1);
  1361. evo_data(push, (asyh->dither.mode << 3) |
  1362. (asyh->dither.bits << 1) |
  1363. asyh->dither.enable);
  1364. evo_kick(push, core);
  1365. }
  1366. }
  1367. static void
  1368. nv50_head_ovly(struct nv50_head *head, struct nv50_head_atom *asyh)
  1369. {
  1370. struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->mast.base;
  1371. u32 bounds = 0;
  1372. u32 *push;
  1373. if (asyh->base.cpp) {
  1374. switch (asyh->base.cpp) {
  1375. case 8: bounds |= 0x00000500; break;
  1376. case 4: bounds |= 0x00000300; break;
  1377. case 2: bounds |= 0x00000100; break;
  1378. default:
  1379. WARN_ON(1);
  1380. break;
  1381. }
  1382. bounds |= 0x00000001;
  1383. }
  1384. if ((push = evo_wait(core, 2))) {
  1385. if (core->base.user.oclass < GF110_DISP_CORE_CHANNEL_DMA)
  1386. evo_mthd(push, 0x0904 + head->base.index * 0x400, 1);
  1387. else
  1388. evo_mthd(push, 0x04d4 + head->base.index * 0x300, 1);
  1389. evo_data(push, bounds);
  1390. evo_kick(push, core);
  1391. }
  1392. }
  1393. static void
  1394. nv50_head_base(struct nv50_head *head, struct nv50_head_atom *asyh)
  1395. {
  1396. struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->mast.base;
  1397. u32 bounds = 0;
  1398. u32 *push;
  1399. if (asyh->base.cpp) {
  1400. switch (asyh->base.cpp) {
  1401. case 8: bounds |= 0x00000500; break;
  1402. case 4: bounds |= 0x00000300; break;
  1403. case 2: bounds |= 0x00000100; break;
  1404. case 1: bounds |= 0x00000000; break;
  1405. default:
  1406. WARN_ON(1);
  1407. break;
  1408. }
  1409. bounds |= 0x00000001;
  1410. }
  1411. if ((push = evo_wait(core, 2))) {
  1412. if (core->base.user.oclass < GF110_DISP_CORE_CHANNEL_DMA)
  1413. evo_mthd(push, 0x0900 + head->base.index * 0x400, 1);
  1414. else
  1415. evo_mthd(push, 0x04d0 + head->base.index * 0x300, 1);
  1416. evo_data(push, bounds);
  1417. evo_kick(push, core);
  1418. }
  1419. }
  1420. static void
  1421. nv50_head_curs_clr(struct nv50_head *head)
  1422. {
  1423. struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->mast.base;
  1424. u32 *push;
  1425. if ((push = evo_wait(core, 4))) {
  1426. if (core->base.user.oclass < G82_DISP_CORE_CHANNEL_DMA) {
  1427. evo_mthd(push, 0x0880 + head->base.index * 0x400, 1);
  1428. evo_data(push, 0x05000000);
  1429. } else
  1430. if (core->base.user.oclass < GF110_DISP_CORE_CHANNEL_DMA) {
  1431. evo_mthd(push, 0x0880 + head->base.index * 0x400, 1);
  1432. evo_data(push, 0x05000000);
  1433. evo_mthd(push, 0x089c + head->base.index * 0x400, 1);
  1434. evo_data(push, 0x00000000);
  1435. } else {
  1436. evo_mthd(push, 0x0480 + head->base.index * 0x300, 1);
  1437. evo_data(push, 0x05000000);
  1438. evo_mthd(push, 0x048c + head->base.index * 0x300, 1);
  1439. evo_data(push, 0x00000000);
  1440. }
  1441. evo_kick(push, core);
  1442. }
  1443. }
  1444. static void
  1445. nv50_head_curs_set(struct nv50_head *head, struct nv50_head_atom *asyh)
  1446. {
  1447. struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->mast.base;
  1448. u32 *push;
  1449. if ((push = evo_wait(core, 5))) {
  1450. if (core->base.user.oclass < G82_DISP_BASE_CHANNEL_DMA) {
  1451. evo_mthd(push, 0x0880 + head->base.index * 0x400, 2);
  1452. evo_data(push, 0x80000000 | (asyh->curs.layout << 26) |
  1453. (asyh->curs.format << 24));
  1454. evo_data(push, asyh->curs.offset >> 8);
  1455. } else
  1456. if (core->base.user.oclass < GF110_DISP_BASE_CHANNEL_DMA) {
  1457. evo_mthd(push, 0x0880 + head->base.index * 0x400, 2);
  1458. evo_data(push, 0x80000000 | (asyh->curs.layout << 26) |
  1459. (asyh->curs.format << 24));
  1460. evo_data(push, asyh->curs.offset >> 8);
  1461. evo_mthd(push, 0x089c + head->base.index * 0x400, 1);
  1462. evo_data(push, asyh->curs.handle);
  1463. } else {
  1464. evo_mthd(push, 0x0480 + head->base.index * 0x300, 2);
  1465. evo_data(push, 0x80000000 | (asyh->curs.layout << 26) |
  1466. (asyh->curs.format << 24));
  1467. evo_data(push, asyh->curs.offset >> 8);
  1468. evo_mthd(push, 0x048c + head->base.index * 0x300, 1);
  1469. evo_data(push, asyh->curs.handle);
  1470. }
  1471. evo_kick(push, core);
  1472. }
  1473. }
  1474. static void
  1475. nv50_head_core_clr(struct nv50_head *head)
  1476. {
  1477. struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->mast.base;
  1478. u32 *push;
  1479. if ((push = evo_wait(core, 2))) {
  1480. if (core->base.user.oclass < GF110_DISP_CORE_CHANNEL_DMA)
  1481. evo_mthd(push, 0x0874 + head->base.index * 0x400, 1);
  1482. else
  1483. evo_mthd(push, 0x0474 + head->base.index * 0x300, 1);
  1484. evo_data(push, 0x00000000);
  1485. evo_kick(push, core);
  1486. }
  1487. }
  1488. static void
  1489. nv50_head_core_set(struct nv50_head *head, struct nv50_head_atom *asyh)
  1490. {
  1491. struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->mast.base;
  1492. u32 *push;
  1493. if ((push = evo_wait(core, 9))) {
  1494. if (core->base.user.oclass < G82_DISP_CORE_CHANNEL_DMA) {
  1495. evo_mthd(push, 0x0860 + head->base.index * 0x400, 1);
  1496. evo_data(push, asyh->core.offset >> 8);
  1497. evo_mthd(push, 0x0868 + head->base.index * 0x400, 4);
  1498. evo_data(push, (asyh->core.h << 16) | asyh->core.w);
  1499. evo_data(push, asyh->core.layout << 20 |
  1500. (asyh->core.pitch >> 8) << 8 |
  1501. asyh->core.block);
  1502. evo_data(push, asyh->core.kind << 16 |
  1503. asyh->core.format << 8);
  1504. evo_data(push, asyh->core.handle);
  1505. evo_mthd(push, 0x08c0 + head->base.index * 0x400, 1);
  1506. evo_data(push, (asyh->core.y << 16) | asyh->core.x);
  1507. /* EVO will complain with INVALID_STATE if we have an
  1508. * active cursor and (re)specify HeadSetContextDmaIso
  1509. * without also updating HeadSetOffsetCursor.
  1510. */
  1511. asyh->set.curs = asyh->curs.visible;
  1512. } else
  1513. if (core->base.user.oclass < GF110_DISP_CORE_CHANNEL_DMA) {
  1514. evo_mthd(push, 0x0860 + head->base.index * 0x400, 1);
  1515. evo_data(push, asyh->core.offset >> 8);
  1516. evo_mthd(push, 0x0868 + head->base.index * 0x400, 4);
  1517. evo_data(push, (asyh->core.h << 16) | asyh->core.w);
  1518. evo_data(push, asyh->core.layout << 20 |
  1519. (asyh->core.pitch >> 8) << 8 |
  1520. asyh->core.block);
  1521. evo_data(push, asyh->core.format << 8);
  1522. evo_data(push, asyh->core.handle);
  1523. evo_mthd(push, 0x08c0 + head->base.index * 0x400, 1);
  1524. evo_data(push, (asyh->core.y << 16) | asyh->core.x);
  1525. } else {
  1526. evo_mthd(push, 0x0460 + head->base.index * 0x300, 1);
  1527. evo_data(push, asyh->core.offset >> 8);
  1528. evo_mthd(push, 0x0468 + head->base.index * 0x300, 4);
  1529. evo_data(push, (asyh->core.h << 16) | asyh->core.w);
  1530. evo_data(push, asyh->core.layout << 24 |
  1531. (asyh->core.pitch >> 8) << 8 |
  1532. asyh->core.block);
  1533. evo_data(push, asyh->core.format << 8);
  1534. evo_data(push, asyh->core.handle);
  1535. evo_mthd(push, 0x04b0 + head->base.index * 0x300, 1);
  1536. evo_data(push, (asyh->core.y << 16) | asyh->core.x);
  1537. }
  1538. evo_kick(push, core);
  1539. }
  1540. }
  1541. static void
  1542. nv50_head_lut_clr(struct nv50_head *head)
  1543. {
  1544. struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->mast.base;
  1545. u32 *push;
  1546. if ((push = evo_wait(core, 4))) {
  1547. if (core->base.user.oclass < G82_DISP_CORE_CHANNEL_DMA) {
  1548. evo_mthd(push, 0x0840 + (head->base.index * 0x400), 1);
  1549. evo_data(push, 0x40000000);
  1550. } else
  1551. if (core->base.user.oclass < GF110_DISP_CORE_CHANNEL_DMA) {
  1552. evo_mthd(push, 0x0840 + (head->base.index * 0x400), 1);
  1553. evo_data(push, 0x40000000);
  1554. evo_mthd(push, 0x085c + (head->base.index * 0x400), 1);
  1555. evo_data(push, 0x00000000);
  1556. } else {
  1557. evo_mthd(push, 0x0440 + (head->base.index * 0x300), 1);
  1558. evo_data(push, 0x03000000);
  1559. evo_mthd(push, 0x045c + (head->base.index * 0x300), 1);
  1560. evo_data(push, 0x00000000);
  1561. }
  1562. evo_kick(push, core);
  1563. }
  1564. }
  1565. static void
  1566. nv50_head_lut_set(struct nv50_head *head, struct nv50_head_atom *asyh)
  1567. {
  1568. struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->mast.base;
  1569. u32 *push;
  1570. if ((push = evo_wait(core, 7))) {
  1571. if (core->base.user.oclass < G82_DISP_CORE_CHANNEL_DMA) {
  1572. evo_mthd(push, 0x0840 + (head->base.index * 0x400), 2);
  1573. evo_data(push, 0xc0000000);
  1574. evo_data(push, asyh->lut.offset >> 8);
  1575. } else
  1576. if (core->base.user.oclass < GF110_DISP_CORE_CHANNEL_DMA) {
  1577. evo_mthd(push, 0x0840 + (head->base.index * 0x400), 2);
  1578. evo_data(push, 0xc0000000);
  1579. evo_data(push, asyh->lut.offset >> 8);
  1580. evo_mthd(push, 0x085c + (head->base.index * 0x400), 1);
  1581. evo_data(push, asyh->lut.handle);
  1582. } else {
  1583. evo_mthd(push, 0x0440 + (head->base.index * 0x300), 4);
  1584. evo_data(push, 0x83000000);
  1585. evo_data(push, asyh->lut.offset >> 8);
  1586. evo_data(push, 0x00000000);
  1587. evo_data(push, 0x00000000);
  1588. evo_mthd(push, 0x045c + (head->base.index * 0x300), 1);
  1589. evo_data(push, asyh->lut.handle);
  1590. }
  1591. evo_kick(push, core);
  1592. }
  1593. }
  1594. static void
  1595. nv50_head_mode(struct nv50_head *head, struct nv50_head_atom *asyh)
  1596. {
  1597. struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->mast.base;
  1598. struct nv50_head_mode *m = &asyh->mode;
  1599. u32 *push;
  1600. if ((push = evo_wait(core, 14))) {
  1601. if (core->base.user.oclass < GF110_DISP_CORE_CHANNEL_DMA) {
  1602. evo_mthd(push, 0x0804 + (head->base.index * 0x400), 2);
  1603. evo_data(push, 0x00800000 | m->clock);
  1604. evo_data(push, m->interlace ? 0x00000002 : 0x00000000);
  1605. evo_mthd(push, 0x0810 + (head->base.index * 0x400), 7);
  1606. evo_data(push, 0x00000000);
  1607. evo_data(push, (m->v.active << 16) | m->h.active );
  1608. evo_data(push, (m->v.synce << 16) | m->h.synce );
  1609. evo_data(push, (m->v.blanke << 16) | m->h.blanke );
  1610. evo_data(push, (m->v.blanks << 16) | m->h.blanks );
  1611. evo_data(push, (m->v.blank2e << 16) | m->v.blank2s);
  1612. evo_data(push, asyh->mode.v.blankus);
  1613. evo_mthd(push, 0x082c + (head->base.index * 0x400), 1);
  1614. evo_data(push, 0x00000000);
  1615. } else {
  1616. evo_mthd(push, 0x0410 + (head->base.index * 0x300), 6);
  1617. evo_data(push, 0x00000000);
  1618. evo_data(push, (m->v.active << 16) | m->h.active );
  1619. evo_data(push, (m->v.synce << 16) | m->h.synce );
  1620. evo_data(push, (m->v.blanke << 16) | m->h.blanke );
  1621. evo_data(push, (m->v.blanks << 16) | m->h.blanks );
  1622. evo_data(push, (m->v.blank2e << 16) | m->v.blank2s);
  1623. evo_mthd(push, 0x042c + (head->base.index * 0x300), 2);
  1624. evo_data(push, 0x00000000); /* ??? */
  1625. evo_data(push, 0xffffff00);
  1626. evo_mthd(push, 0x0450 + (head->base.index * 0x300), 3);
  1627. evo_data(push, m->clock * 1000);
  1628. evo_data(push, 0x00200000); /* ??? */
  1629. evo_data(push, m->clock * 1000);
  1630. }
  1631. evo_kick(push, core);
  1632. }
  1633. }
  1634. static void
  1635. nv50_head_view(struct nv50_head *head, struct nv50_head_atom *asyh)
  1636. {
  1637. struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->mast.base;
  1638. u32 *push;
  1639. if ((push = evo_wait(core, 10))) {
  1640. if (core->base.user.oclass < GF110_DISP_CORE_CHANNEL_DMA) {
  1641. evo_mthd(push, 0x08a4 + (head->base.index * 0x400), 1);
  1642. evo_data(push, 0x00000000);
  1643. evo_mthd(push, 0x08c8 + (head->base.index * 0x400), 1);
  1644. evo_data(push, (asyh->view.iH << 16) | asyh->view.iW);
  1645. evo_mthd(push, 0x08d8 + (head->base.index * 0x400), 2);
  1646. evo_data(push, (asyh->view.oH << 16) | asyh->view.oW);
  1647. evo_data(push, (asyh->view.oH << 16) | asyh->view.oW);
  1648. } else {
  1649. evo_mthd(push, 0x0494 + (head->base.index * 0x300), 1);
  1650. evo_data(push, 0x00000000);
  1651. evo_mthd(push, 0x04b8 + (head->base.index * 0x300), 1);
  1652. evo_data(push, (asyh->view.iH << 16) | asyh->view.iW);
  1653. evo_mthd(push, 0x04c0 + (head->base.index * 0x300), 3);
  1654. evo_data(push, (asyh->view.oH << 16) | asyh->view.oW);
  1655. evo_data(push, (asyh->view.oH << 16) | asyh->view.oW);
  1656. evo_data(push, (asyh->view.oH << 16) | asyh->view.oW);
  1657. }
  1658. evo_kick(push, core);
  1659. }
  1660. }
  1661. static void
  1662. nv50_head_flush_clr(struct nv50_head *head, struct nv50_head_atom *asyh, bool y)
  1663. {
  1664. if (asyh->clr.core && (!asyh->set.core || y))
  1665. nv50_head_lut_clr(head);
  1666. if (asyh->clr.core && (!asyh->set.core || y))
  1667. nv50_head_core_clr(head);
  1668. if (asyh->clr.curs && (!asyh->set.curs || y))
  1669. nv50_head_curs_clr(head);
  1670. }
  1671. static void
  1672. nv50_head_flush_set(struct nv50_head *head, struct nv50_head_atom *asyh)
  1673. {
  1674. if (asyh->set.view ) nv50_head_view (head, asyh);
  1675. if (asyh->set.mode ) nv50_head_mode (head, asyh);
  1676. if (asyh->set.core ) nv50_head_lut_set (head, asyh);
  1677. if (asyh->set.core ) nv50_head_core_set(head, asyh);
  1678. if (asyh->set.curs ) nv50_head_curs_set(head, asyh);
  1679. if (asyh->set.base ) nv50_head_base (head, asyh);
  1680. if (asyh->set.ovly ) nv50_head_ovly (head, asyh);
  1681. if (asyh->set.dither ) nv50_head_dither (head, asyh);
  1682. if (asyh->set.procamp) nv50_head_procamp (head, asyh);
  1683. }
  1684. static void
  1685. nv50_head_atomic_check_procamp(struct nv50_head_atom *armh,
  1686. struct nv50_head_atom *asyh,
  1687. struct nouveau_conn_atom *asyc)
  1688. {
  1689. const int vib = asyc->procamp.color_vibrance - 100;
  1690. const int hue = asyc->procamp.vibrant_hue - 90;
  1691. const int adj = (vib > 0) ? 50 : 0;
  1692. asyh->procamp.sat.cos = ((vib * 2047 + adj) / 100) & 0xfff;
  1693. asyh->procamp.sat.sin = ((hue * 2047) / 100) & 0xfff;
  1694. asyh->set.procamp = true;
  1695. }
  1696. static void
  1697. nv50_head_atomic_check_dither(struct nv50_head_atom *armh,
  1698. struct nv50_head_atom *asyh,
  1699. struct nouveau_conn_atom *asyc)
  1700. {
  1701. struct drm_connector *connector = asyc->state.connector;
  1702. u32 mode = 0x00;
  1703. if (asyc->dither.mode == DITHERING_MODE_AUTO) {
  1704. if (asyh->base.depth > connector->display_info.bpc * 3)
  1705. mode = DITHERING_MODE_DYNAMIC2X2;
  1706. } else {
  1707. mode = asyc->dither.mode;
  1708. }
  1709. if (asyc->dither.depth == DITHERING_DEPTH_AUTO) {
  1710. if (connector->display_info.bpc >= 8)
  1711. mode |= DITHERING_DEPTH_8BPC;
  1712. } else {
  1713. mode |= asyc->dither.depth;
  1714. }
  1715. asyh->dither.enable = mode;
  1716. asyh->dither.bits = mode >> 1;
  1717. asyh->dither.mode = mode >> 3;
  1718. asyh->set.dither = true;
  1719. }
  1720. static void
  1721. nv50_head_atomic_check_view(struct nv50_head_atom *armh,
  1722. struct nv50_head_atom *asyh,
  1723. struct nouveau_conn_atom *asyc)
  1724. {
  1725. struct drm_connector *connector = asyc->state.connector;
  1726. struct drm_display_mode *omode = &asyh->state.adjusted_mode;
  1727. struct drm_display_mode *umode = &asyh->state.mode;
  1728. int mode = asyc->scaler.mode;
  1729. struct edid *edid;
  1730. if (connector->edid_blob_ptr)
  1731. edid = (struct edid *)connector->edid_blob_ptr->data;
  1732. else
  1733. edid = NULL;
  1734. if (!asyc->scaler.full) {
  1735. if (mode == DRM_MODE_SCALE_NONE)
  1736. omode = umode;
  1737. } else {
  1738. /* Non-EDID LVDS/eDP mode. */
  1739. mode = DRM_MODE_SCALE_FULLSCREEN;
  1740. }
  1741. asyh->view.iW = umode->hdisplay;
  1742. asyh->view.iH = umode->vdisplay;
  1743. asyh->view.oW = omode->hdisplay;
  1744. asyh->view.oH = omode->vdisplay;
  1745. if (omode->flags & DRM_MODE_FLAG_DBLSCAN)
  1746. asyh->view.oH *= 2;
  1747. /* Add overscan compensation if necessary, will keep the aspect
  1748. * ratio the same as the backend mode unless overridden by the
  1749. * user setting both hborder and vborder properties.
  1750. */
  1751. if ((asyc->scaler.underscan.mode == UNDERSCAN_ON ||
  1752. (asyc->scaler.underscan.mode == UNDERSCAN_AUTO &&
  1753. drm_detect_hdmi_monitor(edid)))) {
  1754. u32 bX = asyc->scaler.underscan.hborder;
  1755. u32 bY = asyc->scaler.underscan.vborder;
  1756. u32 r = (asyh->view.oH << 19) / asyh->view.oW;
  1757. if (bX) {
  1758. asyh->view.oW -= (bX * 2);
  1759. if (bY) asyh->view.oH -= (bY * 2);
  1760. else asyh->view.oH = ((asyh->view.oW * r) + (r / 2)) >> 19;
  1761. } else {
  1762. asyh->view.oW -= (asyh->view.oW >> 4) + 32;
  1763. if (bY) asyh->view.oH -= (bY * 2);
  1764. else asyh->view.oH = ((asyh->view.oW * r) + (r / 2)) >> 19;
  1765. }
  1766. }
  1767. /* Handle CENTER/ASPECT scaling, taking into account the areas
  1768. * removed already for overscan compensation.
  1769. */
  1770. switch (mode) {
  1771. case DRM_MODE_SCALE_CENTER:
  1772. asyh->view.oW = min((u16)umode->hdisplay, asyh->view.oW);
  1773. asyh->view.oH = min((u16)umode->vdisplay, asyh->view.oH);
  1774. /* fall-through */
  1775. case DRM_MODE_SCALE_ASPECT:
  1776. if (asyh->view.oH < asyh->view.oW) {
  1777. u32 r = (asyh->view.iW << 19) / asyh->view.iH;
  1778. asyh->view.oW = ((asyh->view.oH * r) + (r / 2)) >> 19;
  1779. } else {
  1780. u32 r = (asyh->view.iH << 19) / asyh->view.iW;
  1781. asyh->view.oH = ((asyh->view.oW * r) + (r / 2)) >> 19;
  1782. }
  1783. break;
  1784. default:
  1785. break;
  1786. }
  1787. asyh->set.view = true;
  1788. }
  1789. static void
  1790. nv50_head_atomic_check_mode(struct nv50_head *head, struct nv50_head_atom *asyh)
  1791. {
  1792. struct drm_display_mode *mode = &asyh->state.adjusted_mode;
  1793. u32 ilace = (mode->flags & DRM_MODE_FLAG_INTERLACE) ? 2 : 1;
  1794. u32 vscan = (mode->flags & DRM_MODE_FLAG_DBLSCAN) ? 2 : 1;
  1795. u32 hbackp = mode->htotal - mode->hsync_end;
  1796. u32 vbackp = (mode->vtotal - mode->vsync_end) * vscan / ilace;
  1797. u32 hfrontp = mode->hsync_start - mode->hdisplay;
  1798. u32 vfrontp = (mode->vsync_start - mode->vdisplay) * vscan / ilace;
  1799. u32 blankus;
  1800. struct nv50_head_mode *m = &asyh->mode;
  1801. m->h.active = mode->htotal;
  1802. m->h.synce = mode->hsync_end - mode->hsync_start - 1;
  1803. m->h.blanke = m->h.synce + hbackp;
  1804. m->h.blanks = mode->htotal - hfrontp - 1;
  1805. m->v.active = mode->vtotal * vscan / ilace;
  1806. m->v.synce = ((mode->vsync_end - mode->vsync_start) * vscan / ilace) - 1;
  1807. m->v.blanke = m->v.synce + vbackp;
  1808. m->v.blanks = m->v.active - vfrontp - 1;
  1809. /*XXX: Safe underestimate, even "0" works */
  1810. blankus = (m->v.active - mode->vdisplay - 2) * m->h.active;
  1811. blankus *= 1000;
  1812. blankus /= mode->clock;
  1813. m->v.blankus = blankus;
  1814. if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
  1815. m->v.blank2e = m->v.active + m->v.synce + vbackp;
  1816. m->v.blank2s = m->v.blank2e + (mode->vdisplay * vscan / ilace);
  1817. m->v.active = (m->v.active * 2) + 1;
  1818. m->interlace = true;
  1819. } else {
  1820. m->v.blank2e = 0;
  1821. m->v.blank2s = 1;
  1822. m->interlace = false;
  1823. }
  1824. m->clock = mode->clock;
  1825. drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
  1826. asyh->set.mode = true;
  1827. }
  1828. static int
  1829. nv50_head_atomic_check(struct drm_crtc *crtc, struct drm_crtc_state *state)
  1830. {
  1831. struct nouveau_drm *drm = nouveau_drm(crtc->dev);
  1832. struct nv50_disp *disp = nv50_disp(crtc->dev);
  1833. struct nv50_head *head = nv50_head(crtc);
  1834. struct nv50_head_atom *armh = nv50_head_atom(crtc->state);
  1835. struct nv50_head_atom *asyh = nv50_head_atom(state);
  1836. struct nouveau_conn_atom *asyc = NULL;
  1837. struct drm_connector_state *conns;
  1838. struct drm_connector *conn;
  1839. int i;
  1840. NV_ATOMIC(drm, "%s atomic_check %d\n", crtc->name, asyh->state.active);
  1841. if (asyh->state.active) {
  1842. for_each_connector_in_state(asyh->state.state, conn, conns, i) {
  1843. if (conns->crtc == crtc) {
  1844. asyc = nouveau_conn_atom(conns);
  1845. break;
  1846. }
  1847. }
  1848. if (armh->state.active) {
  1849. if (asyc) {
  1850. if (asyh->state.mode_changed)
  1851. asyc->set.scaler = true;
  1852. if (armh->base.depth != asyh->base.depth)
  1853. asyc->set.dither = true;
  1854. }
  1855. } else {
  1856. asyc->set.mask = ~0;
  1857. asyh->set.mask = ~0;
  1858. }
  1859. if (asyh->state.mode_changed)
  1860. nv50_head_atomic_check_mode(head, asyh);
  1861. if (asyc) {
  1862. if (asyc->set.scaler)
  1863. nv50_head_atomic_check_view(armh, asyh, asyc);
  1864. if (asyc->set.dither)
  1865. nv50_head_atomic_check_dither(armh, asyh, asyc);
  1866. if (asyc->set.procamp)
  1867. nv50_head_atomic_check_procamp(armh, asyh, asyc);
  1868. }
  1869. if ((asyh->core.visible = (asyh->base.cpp != 0))) {
  1870. asyh->core.x = asyh->base.x;
  1871. asyh->core.y = asyh->base.y;
  1872. asyh->core.w = asyh->base.w;
  1873. asyh->core.h = asyh->base.h;
  1874. } else
  1875. if ((asyh->core.visible = asyh->curs.visible)) {
  1876. /*XXX: We need to either find some way of having the
  1877. * primary base layer appear black, while still
  1878. * being able to display the other layers, or we
  1879. * need to allocate a dummy black surface here.
  1880. */
  1881. asyh->core.x = 0;
  1882. asyh->core.y = 0;
  1883. asyh->core.w = asyh->state.mode.hdisplay;
  1884. asyh->core.h = asyh->state.mode.vdisplay;
  1885. }
  1886. asyh->core.handle = disp->mast.base.vram.handle;
  1887. asyh->core.offset = 0;
  1888. asyh->core.format = 0xcf;
  1889. asyh->core.kind = 0;
  1890. asyh->core.layout = 1;
  1891. asyh->core.block = 0;
  1892. asyh->core.pitch = ALIGN(asyh->core.w, 64) * 4;
  1893. asyh->lut.handle = disp->mast.base.vram.handle;
  1894. asyh->lut.offset = head->base.lut.nvbo->bo.offset;
  1895. asyh->set.base = armh->base.cpp != asyh->base.cpp;
  1896. asyh->set.ovly = armh->ovly.cpp != asyh->ovly.cpp;
  1897. } else {
  1898. asyh->core.visible = false;
  1899. asyh->curs.visible = false;
  1900. asyh->base.cpp = 0;
  1901. asyh->ovly.cpp = 0;
  1902. }
  1903. if (!drm_atomic_crtc_needs_modeset(&asyh->state)) {
  1904. if (asyh->core.visible) {
  1905. if (memcmp(&armh->core, &asyh->core, sizeof(asyh->core)))
  1906. asyh->set.core = true;
  1907. } else
  1908. if (armh->core.visible) {
  1909. asyh->clr.core = true;
  1910. }
  1911. if (asyh->curs.visible) {
  1912. if (memcmp(&armh->curs, &asyh->curs, sizeof(asyh->curs)))
  1913. asyh->set.curs = true;
  1914. } else
  1915. if (armh->curs.visible) {
  1916. asyh->clr.curs = true;
  1917. }
  1918. } else {
  1919. asyh->clr.core = armh->core.visible;
  1920. asyh->clr.curs = armh->curs.visible;
  1921. asyh->set.core = asyh->core.visible;
  1922. asyh->set.curs = asyh->curs.visible;
  1923. }
  1924. if (asyh->clr.mask || asyh->set.mask)
  1925. nv50_atom(asyh->state.state)->lock_core = true;
  1926. return 0;
  1927. }
  1928. static void
  1929. nv50_head_lut_load(struct drm_crtc *crtc)
  1930. {
  1931. struct nv50_disp *disp = nv50_disp(crtc->dev);
  1932. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  1933. void __iomem *lut = nvbo_kmap_obj_iovirtual(nv_crtc->lut.nvbo);
  1934. int i;
  1935. for (i = 0; i < 256; i++) {
  1936. u16 r = nv_crtc->lut.r[i] >> 2;
  1937. u16 g = nv_crtc->lut.g[i] >> 2;
  1938. u16 b = nv_crtc->lut.b[i] >> 2;
  1939. if (disp->disp->oclass < GF110_DISP) {
  1940. writew(r + 0x0000, lut + (i * 0x08) + 0);
  1941. writew(g + 0x0000, lut + (i * 0x08) + 2);
  1942. writew(b + 0x0000, lut + (i * 0x08) + 4);
  1943. } else {
  1944. writew(r + 0x6000, lut + (i * 0x20) + 0);
  1945. writew(g + 0x6000, lut + (i * 0x20) + 2);
  1946. writew(b + 0x6000, lut + (i * 0x20) + 4);
  1947. }
  1948. }
  1949. }
  1950. static const struct drm_crtc_helper_funcs
  1951. nv50_head_help = {
  1952. .load_lut = nv50_head_lut_load,
  1953. .atomic_check = nv50_head_atomic_check,
  1954. };
  1955. static int
  1956. nv50_head_gamma_set(struct drm_crtc *crtc, u16 *r, u16 *g, u16 *b,
  1957. uint32_t size,
  1958. struct drm_modeset_acquire_ctx *ctx)
  1959. {
  1960. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  1961. u32 i;
  1962. for (i = 0; i < size; i++) {
  1963. nv_crtc->lut.r[i] = r[i];
  1964. nv_crtc->lut.g[i] = g[i];
  1965. nv_crtc->lut.b[i] = b[i];
  1966. }
  1967. nv50_head_lut_load(crtc);
  1968. return 0;
  1969. }
  1970. static void
  1971. nv50_head_atomic_destroy_state(struct drm_crtc *crtc,
  1972. struct drm_crtc_state *state)
  1973. {
  1974. struct nv50_head_atom *asyh = nv50_head_atom(state);
  1975. __drm_atomic_helper_crtc_destroy_state(&asyh->state);
  1976. kfree(asyh);
  1977. }
  1978. static struct drm_crtc_state *
  1979. nv50_head_atomic_duplicate_state(struct drm_crtc *crtc)
  1980. {
  1981. struct nv50_head_atom *armh = nv50_head_atom(crtc->state);
  1982. struct nv50_head_atom *asyh;
  1983. if (!(asyh = kmalloc(sizeof(*asyh), GFP_KERNEL)))
  1984. return NULL;
  1985. __drm_atomic_helper_crtc_duplicate_state(crtc, &asyh->state);
  1986. asyh->view = armh->view;
  1987. asyh->mode = armh->mode;
  1988. asyh->lut = armh->lut;
  1989. asyh->core = armh->core;
  1990. asyh->curs = armh->curs;
  1991. asyh->base = armh->base;
  1992. asyh->ovly = armh->ovly;
  1993. asyh->dither = armh->dither;
  1994. asyh->procamp = armh->procamp;
  1995. asyh->clr.mask = 0;
  1996. asyh->set.mask = 0;
  1997. return &asyh->state;
  1998. }
  1999. static void
  2000. __drm_atomic_helper_crtc_reset(struct drm_crtc *crtc,
  2001. struct drm_crtc_state *state)
  2002. {
  2003. if (crtc->state)
  2004. crtc->funcs->atomic_destroy_state(crtc, crtc->state);
  2005. crtc->state = state;
  2006. crtc->state->crtc = crtc;
  2007. }
  2008. static void
  2009. nv50_head_reset(struct drm_crtc *crtc)
  2010. {
  2011. struct nv50_head_atom *asyh;
  2012. if (WARN_ON(!(asyh = kzalloc(sizeof(*asyh), GFP_KERNEL))))
  2013. return;
  2014. __drm_atomic_helper_crtc_reset(crtc, &asyh->state);
  2015. }
  2016. static void
  2017. nv50_head_destroy(struct drm_crtc *crtc)
  2018. {
  2019. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  2020. struct nv50_disp *disp = nv50_disp(crtc->dev);
  2021. struct nv50_head *head = nv50_head(crtc);
  2022. nv50_dmac_destroy(&head->ovly.base, disp->disp);
  2023. nv50_pioc_destroy(&head->oimm.base);
  2024. nouveau_bo_unmap(nv_crtc->lut.nvbo);
  2025. if (nv_crtc->lut.nvbo)
  2026. nouveau_bo_unpin(nv_crtc->lut.nvbo);
  2027. nouveau_bo_ref(NULL, &nv_crtc->lut.nvbo);
  2028. drm_crtc_cleanup(crtc);
  2029. kfree(crtc);
  2030. }
  2031. static const struct drm_crtc_funcs
  2032. nv50_head_func = {
  2033. .reset = nv50_head_reset,
  2034. .gamma_set = nv50_head_gamma_set,
  2035. .destroy = nv50_head_destroy,
  2036. .set_config = drm_atomic_helper_set_config,
  2037. .page_flip = drm_atomic_helper_page_flip,
  2038. .set_property = drm_atomic_helper_crtc_set_property,
  2039. .atomic_duplicate_state = nv50_head_atomic_duplicate_state,
  2040. .atomic_destroy_state = nv50_head_atomic_destroy_state,
  2041. };
  2042. static int
  2043. nv50_head_create(struct drm_device *dev, int index)
  2044. {
  2045. struct nouveau_drm *drm = nouveau_drm(dev);
  2046. struct nvif_device *device = &drm->client.device;
  2047. struct nv50_disp *disp = nv50_disp(dev);
  2048. struct nv50_head *head;
  2049. struct nv50_base *base;
  2050. struct nv50_curs *curs;
  2051. struct drm_crtc *crtc;
  2052. int ret, i;
  2053. head = kzalloc(sizeof(*head), GFP_KERNEL);
  2054. if (!head)
  2055. return -ENOMEM;
  2056. head->base.index = index;
  2057. for (i = 0; i < 256; i++) {
  2058. head->base.lut.r[i] = i << 8;
  2059. head->base.lut.g[i] = i << 8;
  2060. head->base.lut.b[i] = i << 8;
  2061. }
  2062. ret = nv50_base_new(drm, head, &base);
  2063. if (ret == 0)
  2064. ret = nv50_curs_new(drm, head, &curs);
  2065. if (ret) {
  2066. kfree(head);
  2067. return ret;
  2068. }
  2069. crtc = &head->base.base;
  2070. drm_crtc_init_with_planes(dev, crtc, &base->wndw.plane,
  2071. &curs->wndw.plane, &nv50_head_func,
  2072. "head-%d", head->base.index);
  2073. drm_crtc_helper_add(crtc, &nv50_head_help);
  2074. drm_mode_crtc_set_gamma_size(crtc, 256);
  2075. ret = nouveau_bo_new(&drm->client, 8192, 0x100, TTM_PL_FLAG_VRAM,
  2076. 0, 0x0000, NULL, NULL, &head->base.lut.nvbo);
  2077. if (!ret) {
  2078. ret = nouveau_bo_pin(head->base.lut.nvbo, TTM_PL_FLAG_VRAM, true);
  2079. if (!ret) {
  2080. ret = nouveau_bo_map(head->base.lut.nvbo);
  2081. if (ret)
  2082. nouveau_bo_unpin(head->base.lut.nvbo);
  2083. }
  2084. if (ret)
  2085. nouveau_bo_ref(NULL, &head->base.lut.nvbo);
  2086. }
  2087. if (ret)
  2088. goto out;
  2089. /* allocate overlay resources */
  2090. ret = nv50_oimm_create(device, disp->disp, index, &head->oimm);
  2091. if (ret)
  2092. goto out;
  2093. ret = nv50_ovly_create(device, disp->disp, index, disp->sync->bo.offset,
  2094. &head->ovly);
  2095. if (ret)
  2096. goto out;
  2097. out:
  2098. if (ret)
  2099. nv50_head_destroy(crtc);
  2100. return ret;
  2101. }
  2102. /******************************************************************************
  2103. * Output path helpers
  2104. *****************************************************************************/
  2105. static int
  2106. nv50_outp_atomic_check_view(struct drm_encoder *encoder,
  2107. struct drm_crtc_state *crtc_state,
  2108. struct drm_connector_state *conn_state,
  2109. struct drm_display_mode *native_mode)
  2110. {
  2111. struct drm_display_mode *adjusted_mode = &crtc_state->adjusted_mode;
  2112. struct drm_display_mode *mode = &crtc_state->mode;
  2113. struct drm_connector *connector = conn_state->connector;
  2114. struct nouveau_conn_atom *asyc = nouveau_conn_atom(conn_state);
  2115. struct nouveau_drm *drm = nouveau_drm(encoder->dev);
  2116. NV_ATOMIC(drm, "%s atomic_check\n", encoder->name);
  2117. asyc->scaler.full = false;
  2118. if (!native_mode)
  2119. return 0;
  2120. if (asyc->scaler.mode == DRM_MODE_SCALE_NONE) {
  2121. switch (connector->connector_type) {
  2122. case DRM_MODE_CONNECTOR_LVDS:
  2123. case DRM_MODE_CONNECTOR_eDP:
  2124. /* Force use of scaler for non-EDID modes. */
  2125. if (adjusted_mode->type & DRM_MODE_TYPE_DRIVER)
  2126. break;
  2127. mode = native_mode;
  2128. asyc->scaler.full = true;
  2129. break;
  2130. default:
  2131. break;
  2132. }
  2133. } else {
  2134. mode = native_mode;
  2135. }
  2136. if (!drm_mode_equal(adjusted_mode, mode)) {
  2137. drm_mode_copy(adjusted_mode, mode);
  2138. crtc_state->mode_changed = true;
  2139. }
  2140. return 0;
  2141. }
  2142. static int
  2143. nv50_outp_atomic_check(struct drm_encoder *encoder,
  2144. struct drm_crtc_state *crtc_state,
  2145. struct drm_connector_state *conn_state)
  2146. {
  2147. struct nouveau_connector *nv_connector =
  2148. nouveau_connector(conn_state->connector);
  2149. return nv50_outp_atomic_check_view(encoder, crtc_state, conn_state,
  2150. nv_connector->native_mode);
  2151. }
  2152. /******************************************************************************
  2153. * DAC
  2154. *****************************************************************************/
  2155. static void
  2156. nv50_dac_dpms(struct drm_encoder *encoder, int mode)
  2157. {
  2158. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  2159. struct nv50_disp *disp = nv50_disp(encoder->dev);
  2160. struct {
  2161. struct nv50_disp_mthd_v1 base;
  2162. struct nv50_disp_dac_pwr_v0 pwr;
  2163. } args = {
  2164. .base.version = 1,
  2165. .base.method = NV50_DISP_MTHD_V1_DAC_PWR,
  2166. .base.hasht = nv_encoder->dcb->hasht,
  2167. .base.hashm = nv_encoder->dcb->hashm,
  2168. .pwr.state = 1,
  2169. .pwr.data = 1,
  2170. .pwr.vsync = (mode != DRM_MODE_DPMS_SUSPEND &&
  2171. mode != DRM_MODE_DPMS_OFF),
  2172. .pwr.hsync = (mode != DRM_MODE_DPMS_STANDBY &&
  2173. mode != DRM_MODE_DPMS_OFF),
  2174. };
  2175. nvif_mthd(disp->disp, 0, &args, sizeof(args));
  2176. }
  2177. static void
  2178. nv50_dac_disable(struct drm_encoder *encoder)
  2179. {
  2180. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  2181. struct nv50_mast *mast = nv50_mast(encoder->dev);
  2182. const int or = nv_encoder->or;
  2183. u32 *push;
  2184. if (nv_encoder->crtc) {
  2185. push = evo_wait(mast, 4);
  2186. if (push) {
  2187. if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
  2188. evo_mthd(push, 0x0400 + (or * 0x080), 1);
  2189. evo_data(push, 0x00000000);
  2190. } else {
  2191. evo_mthd(push, 0x0180 + (or * 0x020), 1);
  2192. evo_data(push, 0x00000000);
  2193. }
  2194. evo_kick(push, mast);
  2195. }
  2196. }
  2197. nv_encoder->crtc = NULL;
  2198. }
  2199. static void
  2200. nv50_dac_enable(struct drm_encoder *encoder)
  2201. {
  2202. struct nv50_mast *mast = nv50_mast(encoder->dev);
  2203. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  2204. struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
  2205. struct drm_display_mode *mode = &nv_crtc->base.state->adjusted_mode;
  2206. u32 *push;
  2207. push = evo_wait(mast, 8);
  2208. if (push) {
  2209. if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
  2210. u32 syncs = 0x00000000;
  2211. if (mode->flags & DRM_MODE_FLAG_NHSYNC)
  2212. syncs |= 0x00000001;
  2213. if (mode->flags & DRM_MODE_FLAG_NVSYNC)
  2214. syncs |= 0x00000002;
  2215. evo_mthd(push, 0x0400 + (nv_encoder->or * 0x080), 2);
  2216. evo_data(push, 1 << nv_crtc->index);
  2217. evo_data(push, syncs);
  2218. } else {
  2219. u32 magic = 0x31ec6000 | (nv_crtc->index << 25);
  2220. u32 syncs = 0x00000001;
  2221. if (mode->flags & DRM_MODE_FLAG_NHSYNC)
  2222. syncs |= 0x00000008;
  2223. if (mode->flags & DRM_MODE_FLAG_NVSYNC)
  2224. syncs |= 0x00000010;
  2225. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  2226. magic |= 0x00000001;
  2227. evo_mthd(push, 0x0404 + (nv_crtc->index * 0x300), 2);
  2228. evo_data(push, syncs);
  2229. evo_data(push, magic);
  2230. evo_mthd(push, 0x0180 + (nv_encoder->or * 0x020), 1);
  2231. evo_data(push, 1 << nv_crtc->index);
  2232. }
  2233. evo_kick(push, mast);
  2234. }
  2235. nv_encoder->crtc = encoder->crtc;
  2236. }
  2237. static enum drm_connector_status
  2238. nv50_dac_detect(struct drm_encoder *encoder, struct drm_connector *connector)
  2239. {
  2240. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  2241. struct nv50_disp *disp = nv50_disp(encoder->dev);
  2242. struct {
  2243. struct nv50_disp_mthd_v1 base;
  2244. struct nv50_disp_dac_load_v0 load;
  2245. } args = {
  2246. .base.version = 1,
  2247. .base.method = NV50_DISP_MTHD_V1_DAC_LOAD,
  2248. .base.hasht = nv_encoder->dcb->hasht,
  2249. .base.hashm = nv_encoder->dcb->hashm,
  2250. };
  2251. int ret;
  2252. args.load.data = nouveau_drm(encoder->dev)->vbios.dactestval;
  2253. if (args.load.data == 0)
  2254. args.load.data = 340;
  2255. ret = nvif_mthd(disp->disp, 0, &args, sizeof(args));
  2256. if (ret || !args.load.load)
  2257. return connector_status_disconnected;
  2258. return connector_status_connected;
  2259. }
  2260. static const struct drm_encoder_helper_funcs
  2261. nv50_dac_help = {
  2262. .dpms = nv50_dac_dpms,
  2263. .atomic_check = nv50_outp_atomic_check,
  2264. .enable = nv50_dac_enable,
  2265. .disable = nv50_dac_disable,
  2266. .detect = nv50_dac_detect
  2267. };
  2268. static void
  2269. nv50_dac_destroy(struct drm_encoder *encoder)
  2270. {
  2271. drm_encoder_cleanup(encoder);
  2272. kfree(encoder);
  2273. }
  2274. static const struct drm_encoder_funcs
  2275. nv50_dac_func = {
  2276. .destroy = nv50_dac_destroy,
  2277. };
  2278. static int
  2279. nv50_dac_create(struct drm_connector *connector, struct dcb_output *dcbe)
  2280. {
  2281. struct nouveau_drm *drm = nouveau_drm(connector->dev);
  2282. struct nvkm_i2c *i2c = nvxx_i2c(&drm->client.device);
  2283. struct nvkm_i2c_bus *bus;
  2284. struct nouveau_encoder *nv_encoder;
  2285. struct drm_encoder *encoder;
  2286. int type = DRM_MODE_ENCODER_DAC;
  2287. nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
  2288. if (!nv_encoder)
  2289. return -ENOMEM;
  2290. nv_encoder->dcb = dcbe;
  2291. nv_encoder->or = ffs(dcbe->or) - 1;
  2292. bus = nvkm_i2c_bus_find(i2c, dcbe->i2c_index);
  2293. if (bus)
  2294. nv_encoder->i2c = &bus->i2c;
  2295. encoder = to_drm_encoder(nv_encoder);
  2296. encoder->possible_crtcs = dcbe->heads;
  2297. encoder->possible_clones = 0;
  2298. drm_encoder_init(connector->dev, encoder, &nv50_dac_func, type,
  2299. "dac-%04x-%04x", dcbe->hasht, dcbe->hashm);
  2300. drm_encoder_helper_add(encoder, &nv50_dac_help);
  2301. drm_mode_connector_attach_encoder(connector, encoder);
  2302. return 0;
  2303. }
  2304. /******************************************************************************
  2305. * Audio
  2306. *****************************************************************************/
  2307. static void
  2308. nv50_audio_disable(struct drm_encoder *encoder, struct nouveau_crtc *nv_crtc)
  2309. {
  2310. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  2311. struct nv50_disp *disp = nv50_disp(encoder->dev);
  2312. struct {
  2313. struct nv50_disp_mthd_v1 base;
  2314. struct nv50_disp_sor_hda_eld_v0 eld;
  2315. } args = {
  2316. .base.version = 1,
  2317. .base.method = NV50_DISP_MTHD_V1_SOR_HDA_ELD,
  2318. .base.hasht = nv_encoder->dcb->hasht,
  2319. .base.hashm = (0xf0ff & nv_encoder->dcb->hashm) |
  2320. (0x0100 << nv_crtc->index),
  2321. };
  2322. nvif_mthd(disp->disp, 0, &args, sizeof(args));
  2323. }
  2324. static void
  2325. nv50_audio_enable(struct drm_encoder *encoder, struct drm_display_mode *mode)
  2326. {
  2327. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  2328. struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
  2329. struct nouveau_connector *nv_connector;
  2330. struct nv50_disp *disp = nv50_disp(encoder->dev);
  2331. struct __packed {
  2332. struct {
  2333. struct nv50_disp_mthd_v1 mthd;
  2334. struct nv50_disp_sor_hda_eld_v0 eld;
  2335. } base;
  2336. u8 data[sizeof(nv_connector->base.eld)];
  2337. } args = {
  2338. .base.mthd.version = 1,
  2339. .base.mthd.method = NV50_DISP_MTHD_V1_SOR_HDA_ELD,
  2340. .base.mthd.hasht = nv_encoder->dcb->hasht,
  2341. .base.mthd.hashm = (0xf0ff & nv_encoder->dcb->hashm) |
  2342. (0x0100 << nv_crtc->index),
  2343. };
  2344. nv_connector = nouveau_encoder_connector_get(nv_encoder);
  2345. if (!drm_detect_monitor_audio(nv_connector->edid))
  2346. return;
  2347. drm_edid_to_eld(&nv_connector->base, nv_connector->edid);
  2348. memcpy(args.data, nv_connector->base.eld, sizeof(args.data));
  2349. nvif_mthd(disp->disp, 0, &args,
  2350. sizeof(args.base) + drm_eld_size(args.data));
  2351. }
  2352. /******************************************************************************
  2353. * HDMI
  2354. *****************************************************************************/
  2355. static void
  2356. nv50_hdmi_disable(struct drm_encoder *encoder, struct nouveau_crtc *nv_crtc)
  2357. {
  2358. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  2359. struct nv50_disp *disp = nv50_disp(encoder->dev);
  2360. struct {
  2361. struct nv50_disp_mthd_v1 base;
  2362. struct nv50_disp_sor_hdmi_pwr_v0 pwr;
  2363. } args = {
  2364. .base.version = 1,
  2365. .base.method = NV50_DISP_MTHD_V1_SOR_HDMI_PWR,
  2366. .base.hasht = nv_encoder->dcb->hasht,
  2367. .base.hashm = (0xf0ff & nv_encoder->dcb->hashm) |
  2368. (0x0100 << nv_crtc->index),
  2369. };
  2370. nvif_mthd(disp->disp, 0, &args, sizeof(args));
  2371. }
  2372. static void
  2373. nv50_hdmi_enable(struct drm_encoder *encoder, struct drm_display_mode *mode)
  2374. {
  2375. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  2376. struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
  2377. struct nv50_disp *disp = nv50_disp(encoder->dev);
  2378. struct {
  2379. struct nv50_disp_mthd_v1 base;
  2380. struct nv50_disp_sor_hdmi_pwr_v0 pwr;
  2381. } args = {
  2382. .base.version = 1,
  2383. .base.method = NV50_DISP_MTHD_V1_SOR_HDMI_PWR,
  2384. .base.hasht = nv_encoder->dcb->hasht,
  2385. .base.hashm = (0xf0ff & nv_encoder->dcb->hashm) |
  2386. (0x0100 << nv_crtc->index),
  2387. .pwr.state = 1,
  2388. .pwr.rekey = 56, /* binary driver, and tegra, constant */
  2389. };
  2390. struct nouveau_connector *nv_connector;
  2391. u32 max_ac_packet;
  2392. nv_connector = nouveau_encoder_connector_get(nv_encoder);
  2393. if (!drm_detect_hdmi_monitor(nv_connector->edid))
  2394. return;
  2395. max_ac_packet = mode->htotal - mode->hdisplay;
  2396. max_ac_packet -= args.pwr.rekey;
  2397. max_ac_packet -= 18; /* constant from tegra */
  2398. args.pwr.max_ac_packet = max_ac_packet / 32;
  2399. nvif_mthd(disp->disp, 0, &args, sizeof(args));
  2400. nv50_audio_enable(encoder, mode);
  2401. }
  2402. /******************************************************************************
  2403. * MST
  2404. *****************************************************************************/
  2405. #define nv50_mstm(p) container_of((p), struct nv50_mstm, mgr)
  2406. #define nv50_mstc(p) container_of((p), struct nv50_mstc, connector)
  2407. #define nv50_msto(p) container_of((p), struct nv50_msto, encoder)
  2408. struct nv50_mstm {
  2409. struct nouveau_encoder *outp;
  2410. struct drm_dp_mst_topology_mgr mgr;
  2411. struct nv50_msto *msto[4];
  2412. bool modified;
  2413. };
  2414. struct nv50_mstc {
  2415. struct nv50_mstm *mstm;
  2416. struct drm_dp_mst_port *port;
  2417. struct drm_connector connector;
  2418. struct drm_display_mode *native;
  2419. struct edid *edid;
  2420. int pbn;
  2421. };
  2422. struct nv50_msto {
  2423. struct drm_encoder encoder;
  2424. struct nv50_head *head;
  2425. struct nv50_mstc *mstc;
  2426. bool disabled;
  2427. };
  2428. static struct drm_dp_payload *
  2429. nv50_msto_payload(struct nv50_msto *msto)
  2430. {
  2431. struct nouveau_drm *drm = nouveau_drm(msto->encoder.dev);
  2432. struct nv50_mstc *mstc = msto->mstc;
  2433. struct nv50_mstm *mstm = mstc->mstm;
  2434. int vcpi = mstc->port->vcpi.vcpi, i;
  2435. NV_ATOMIC(drm, "%s: vcpi %d\n", msto->encoder.name, vcpi);
  2436. for (i = 0; i < mstm->mgr.max_payloads; i++) {
  2437. struct drm_dp_payload *payload = &mstm->mgr.payloads[i];
  2438. NV_ATOMIC(drm, "%s: %d: vcpi %d start 0x%02x slots 0x%02x\n",
  2439. mstm->outp->base.base.name, i, payload->vcpi,
  2440. payload->start_slot, payload->num_slots);
  2441. }
  2442. for (i = 0; i < mstm->mgr.max_payloads; i++) {
  2443. struct drm_dp_payload *payload = &mstm->mgr.payloads[i];
  2444. if (payload->vcpi == vcpi)
  2445. return payload;
  2446. }
  2447. return NULL;
  2448. }
  2449. static void
  2450. nv50_msto_cleanup(struct nv50_msto *msto)
  2451. {
  2452. struct nouveau_drm *drm = nouveau_drm(msto->encoder.dev);
  2453. struct nv50_mstc *mstc = msto->mstc;
  2454. struct nv50_mstm *mstm = mstc->mstm;
  2455. NV_ATOMIC(drm, "%s: msto cleanup\n", msto->encoder.name);
  2456. if (mstc->port && mstc->port->vcpi.vcpi > 0 && !nv50_msto_payload(msto))
  2457. drm_dp_mst_deallocate_vcpi(&mstm->mgr, mstc->port);
  2458. if (msto->disabled) {
  2459. msto->mstc = NULL;
  2460. msto->head = NULL;
  2461. msto->disabled = false;
  2462. }
  2463. }
  2464. static void
  2465. nv50_msto_prepare(struct nv50_msto *msto)
  2466. {
  2467. struct nouveau_drm *drm = nouveau_drm(msto->encoder.dev);
  2468. struct nv50_mstc *mstc = msto->mstc;
  2469. struct nv50_mstm *mstm = mstc->mstm;
  2470. struct {
  2471. struct nv50_disp_mthd_v1 base;
  2472. struct nv50_disp_sor_dp_mst_vcpi_v0 vcpi;
  2473. } args = {
  2474. .base.version = 1,
  2475. .base.method = NV50_DISP_MTHD_V1_SOR_DP_MST_VCPI,
  2476. .base.hasht = mstm->outp->dcb->hasht,
  2477. .base.hashm = (0xf0ff & mstm->outp->dcb->hashm) |
  2478. (0x0100 << msto->head->base.index),
  2479. };
  2480. NV_ATOMIC(drm, "%s: msto prepare\n", msto->encoder.name);
  2481. if (mstc->port && mstc->port->vcpi.vcpi > 0) {
  2482. struct drm_dp_payload *payload = nv50_msto_payload(msto);
  2483. if (payload) {
  2484. args.vcpi.start_slot = payload->start_slot;
  2485. args.vcpi.num_slots = payload->num_slots;
  2486. args.vcpi.pbn = mstc->port->vcpi.pbn;
  2487. args.vcpi.aligned_pbn = mstc->port->vcpi.aligned_pbn;
  2488. }
  2489. }
  2490. NV_ATOMIC(drm, "%s: %s: %02x %02x %04x %04x\n",
  2491. msto->encoder.name, msto->head->base.base.name,
  2492. args.vcpi.start_slot, args.vcpi.num_slots,
  2493. args.vcpi.pbn, args.vcpi.aligned_pbn);
  2494. nvif_mthd(&drm->display->disp, 0, &args, sizeof(args));
  2495. }
  2496. static int
  2497. nv50_msto_atomic_check(struct drm_encoder *encoder,
  2498. struct drm_crtc_state *crtc_state,
  2499. struct drm_connector_state *conn_state)
  2500. {
  2501. struct nv50_mstc *mstc = nv50_mstc(conn_state->connector);
  2502. struct nv50_mstm *mstm = mstc->mstm;
  2503. int bpp = conn_state->connector->display_info.bpc * 3;
  2504. int slots;
  2505. mstc->pbn = drm_dp_calc_pbn_mode(crtc_state->adjusted_mode.clock, bpp);
  2506. slots = drm_dp_find_vcpi_slots(&mstm->mgr, mstc->pbn);
  2507. if (slots < 0)
  2508. return slots;
  2509. return nv50_outp_atomic_check_view(encoder, crtc_state, conn_state,
  2510. mstc->native);
  2511. }
  2512. static void
  2513. nv50_msto_enable(struct drm_encoder *encoder)
  2514. {
  2515. struct nv50_head *head = nv50_head(encoder->crtc);
  2516. struct nv50_msto *msto = nv50_msto(encoder);
  2517. struct nv50_mstc *mstc = NULL;
  2518. struct nv50_mstm *mstm = NULL;
  2519. struct drm_connector *connector;
  2520. u8 proto, depth;
  2521. int slots;
  2522. bool r;
  2523. drm_for_each_connector(connector, encoder->dev) {
  2524. if (connector->state->best_encoder == &msto->encoder) {
  2525. mstc = nv50_mstc(connector);
  2526. mstm = mstc->mstm;
  2527. break;
  2528. }
  2529. }
  2530. if (WARN_ON(!mstc))
  2531. return;
  2532. slots = drm_dp_find_vcpi_slots(&mstm->mgr, mstc->pbn);
  2533. r = drm_dp_mst_allocate_vcpi(&mstm->mgr, mstc->port, mstc->pbn, slots);
  2534. WARN_ON(!r);
  2535. if (mstm->outp->dcb->sorconf.link & 1)
  2536. proto = 0x8;
  2537. else
  2538. proto = 0x9;
  2539. switch (mstc->connector.display_info.bpc) {
  2540. case 6: depth = 0x2; break;
  2541. case 8: depth = 0x5; break;
  2542. case 10:
  2543. default: depth = 0x6; break;
  2544. }
  2545. mstm->outp->update(mstm->outp, head->base.index,
  2546. &head->base.base.state->adjusted_mode, proto, depth);
  2547. msto->head = head;
  2548. msto->mstc = mstc;
  2549. mstm->modified = true;
  2550. }
  2551. static void
  2552. nv50_msto_disable(struct drm_encoder *encoder)
  2553. {
  2554. struct nv50_msto *msto = nv50_msto(encoder);
  2555. struct nv50_mstc *mstc = msto->mstc;
  2556. struct nv50_mstm *mstm = mstc->mstm;
  2557. if (mstc->port)
  2558. drm_dp_mst_reset_vcpi_slots(&mstm->mgr, mstc->port);
  2559. mstm->outp->update(mstm->outp, msto->head->base.index, NULL, 0, 0);
  2560. mstm->modified = true;
  2561. msto->disabled = true;
  2562. }
  2563. static const struct drm_encoder_helper_funcs
  2564. nv50_msto_help = {
  2565. .disable = nv50_msto_disable,
  2566. .enable = nv50_msto_enable,
  2567. .atomic_check = nv50_msto_atomic_check,
  2568. };
  2569. static void
  2570. nv50_msto_destroy(struct drm_encoder *encoder)
  2571. {
  2572. struct nv50_msto *msto = nv50_msto(encoder);
  2573. drm_encoder_cleanup(&msto->encoder);
  2574. kfree(msto);
  2575. }
  2576. static const struct drm_encoder_funcs
  2577. nv50_msto = {
  2578. .destroy = nv50_msto_destroy,
  2579. };
  2580. static int
  2581. nv50_msto_new(struct drm_device *dev, u32 heads, const char *name, int id,
  2582. struct nv50_msto **pmsto)
  2583. {
  2584. struct nv50_msto *msto;
  2585. int ret;
  2586. if (!(msto = *pmsto = kzalloc(sizeof(*msto), GFP_KERNEL)))
  2587. return -ENOMEM;
  2588. ret = drm_encoder_init(dev, &msto->encoder, &nv50_msto,
  2589. DRM_MODE_ENCODER_DPMST, "%s-mst-%d", name, id);
  2590. if (ret) {
  2591. kfree(*pmsto);
  2592. *pmsto = NULL;
  2593. return ret;
  2594. }
  2595. drm_encoder_helper_add(&msto->encoder, &nv50_msto_help);
  2596. msto->encoder.possible_crtcs = heads;
  2597. return 0;
  2598. }
  2599. static struct drm_encoder *
  2600. nv50_mstc_atomic_best_encoder(struct drm_connector *connector,
  2601. struct drm_connector_state *connector_state)
  2602. {
  2603. struct nv50_head *head = nv50_head(connector_state->crtc);
  2604. struct nv50_mstc *mstc = nv50_mstc(connector);
  2605. if (mstc->port) {
  2606. struct nv50_mstm *mstm = mstc->mstm;
  2607. return &mstm->msto[head->base.index]->encoder;
  2608. }
  2609. return NULL;
  2610. }
  2611. static struct drm_encoder *
  2612. nv50_mstc_best_encoder(struct drm_connector *connector)
  2613. {
  2614. struct nv50_mstc *mstc = nv50_mstc(connector);
  2615. if (mstc->port) {
  2616. struct nv50_mstm *mstm = mstc->mstm;
  2617. return &mstm->msto[0]->encoder;
  2618. }
  2619. return NULL;
  2620. }
  2621. static enum drm_mode_status
  2622. nv50_mstc_mode_valid(struct drm_connector *connector,
  2623. struct drm_display_mode *mode)
  2624. {
  2625. return MODE_OK;
  2626. }
  2627. static int
  2628. nv50_mstc_get_modes(struct drm_connector *connector)
  2629. {
  2630. struct nv50_mstc *mstc = nv50_mstc(connector);
  2631. int ret = 0;
  2632. mstc->edid = drm_dp_mst_get_edid(&mstc->connector, mstc->port->mgr, mstc->port);
  2633. drm_mode_connector_update_edid_property(&mstc->connector, mstc->edid);
  2634. if (mstc->edid) {
  2635. ret = drm_add_edid_modes(&mstc->connector, mstc->edid);
  2636. drm_edid_to_eld(&mstc->connector, mstc->edid);
  2637. }
  2638. if (!mstc->connector.display_info.bpc)
  2639. mstc->connector.display_info.bpc = 8;
  2640. if (mstc->native)
  2641. drm_mode_destroy(mstc->connector.dev, mstc->native);
  2642. mstc->native = nouveau_conn_native_mode(&mstc->connector);
  2643. return ret;
  2644. }
  2645. static const struct drm_connector_helper_funcs
  2646. nv50_mstc_help = {
  2647. .get_modes = nv50_mstc_get_modes,
  2648. .mode_valid = nv50_mstc_mode_valid,
  2649. .best_encoder = nv50_mstc_best_encoder,
  2650. .atomic_best_encoder = nv50_mstc_atomic_best_encoder,
  2651. };
  2652. static enum drm_connector_status
  2653. nv50_mstc_detect(struct drm_connector *connector, bool force)
  2654. {
  2655. struct nv50_mstc *mstc = nv50_mstc(connector);
  2656. if (!mstc->port)
  2657. return connector_status_disconnected;
  2658. return drm_dp_mst_detect_port(connector, mstc->port->mgr, mstc->port);
  2659. }
  2660. static void
  2661. nv50_mstc_destroy(struct drm_connector *connector)
  2662. {
  2663. struct nv50_mstc *mstc = nv50_mstc(connector);
  2664. drm_connector_cleanup(&mstc->connector);
  2665. kfree(mstc);
  2666. }
  2667. static const struct drm_connector_funcs
  2668. nv50_mstc = {
  2669. .dpms = drm_atomic_helper_connector_dpms,
  2670. .reset = nouveau_conn_reset,
  2671. .detect = nv50_mstc_detect,
  2672. .fill_modes = drm_helper_probe_single_connector_modes,
  2673. .set_property = drm_atomic_helper_connector_set_property,
  2674. .destroy = nv50_mstc_destroy,
  2675. .atomic_duplicate_state = nouveau_conn_atomic_duplicate_state,
  2676. .atomic_destroy_state = nouveau_conn_atomic_destroy_state,
  2677. .atomic_set_property = nouveau_conn_atomic_set_property,
  2678. .atomic_get_property = nouveau_conn_atomic_get_property,
  2679. };
  2680. static int
  2681. nv50_mstc_new(struct nv50_mstm *mstm, struct drm_dp_mst_port *port,
  2682. const char *path, struct nv50_mstc **pmstc)
  2683. {
  2684. struct drm_device *dev = mstm->outp->base.base.dev;
  2685. struct nv50_mstc *mstc;
  2686. int ret, i;
  2687. if (!(mstc = *pmstc = kzalloc(sizeof(*mstc), GFP_KERNEL)))
  2688. return -ENOMEM;
  2689. mstc->mstm = mstm;
  2690. mstc->port = port;
  2691. ret = drm_connector_init(dev, &mstc->connector, &nv50_mstc,
  2692. DRM_MODE_CONNECTOR_DisplayPort);
  2693. if (ret) {
  2694. kfree(*pmstc);
  2695. *pmstc = NULL;
  2696. return ret;
  2697. }
  2698. drm_connector_helper_add(&mstc->connector, &nv50_mstc_help);
  2699. mstc->connector.funcs->reset(&mstc->connector);
  2700. nouveau_conn_attach_properties(&mstc->connector);
  2701. for (i = 0; i < ARRAY_SIZE(mstm->msto) && mstm->msto; i++)
  2702. drm_mode_connector_attach_encoder(&mstc->connector, &mstm->msto[i]->encoder);
  2703. drm_object_attach_property(&mstc->connector.base, dev->mode_config.path_property, 0);
  2704. drm_object_attach_property(&mstc->connector.base, dev->mode_config.tile_property, 0);
  2705. drm_mode_connector_set_path_property(&mstc->connector, path);
  2706. return 0;
  2707. }
  2708. static void
  2709. nv50_mstm_cleanup(struct nv50_mstm *mstm)
  2710. {
  2711. struct nouveau_drm *drm = nouveau_drm(mstm->outp->base.base.dev);
  2712. struct drm_encoder *encoder;
  2713. int ret;
  2714. NV_ATOMIC(drm, "%s: mstm cleanup\n", mstm->outp->base.base.name);
  2715. ret = drm_dp_check_act_status(&mstm->mgr);
  2716. ret = drm_dp_update_payload_part2(&mstm->mgr);
  2717. drm_for_each_encoder(encoder, mstm->outp->base.base.dev) {
  2718. if (encoder->encoder_type == DRM_MODE_ENCODER_DPMST) {
  2719. struct nv50_msto *msto = nv50_msto(encoder);
  2720. struct nv50_mstc *mstc = msto->mstc;
  2721. if (mstc && mstc->mstm == mstm)
  2722. nv50_msto_cleanup(msto);
  2723. }
  2724. }
  2725. mstm->modified = false;
  2726. }
  2727. static void
  2728. nv50_mstm_prepare(struct nv50_mstm *mstm)
  2729. {
  2730. struct nouveau_drm *drm = nouveau_drm(mstm->outp->base.base.dev);
  2731. struct drm_encoder *encoder;
  2732. int ret;
  2733. NV_ATOMIC(drm, "%s: mstm prepare\n", mstm->outp->base.base.name);
  2734. ret = drm_dp_update_payload_part1(&mstm->mgr);
  2735. drm_for_each_encoder(encoder, mstm->outp->base.base.dev) {
  2736. if (encoder->encoder_type == DRM_MODE_ENCODER_DPMST) {
  2737. struct nv50_msto *msto = nv50_msto(encoder);
  2738. struct nv50_mstc *mstc = msto->mstc;
  2739. if (mstc && mstc->mstm == mstm)
  2740. nv50_msto_prepare(msto);
  2741. }
  2742. }
  2743. }
  2744. static void
  2745. nv50_mstm_hotplug(struct drm_dp_mst_topology_mgr *mgr)
  2746. {
  2747. struct nv50_mstm *mstm = nv50_mstm(mgr);
  2748. drm_kms_helper_hotplug_event(mstm->outp->base.base.dev);
  2749. }
  2750. static void
  2751. nv50_mstm_destroy_connector(struct drm_dp_mst_topology_mgr *mgr,
  2752. struct drm_connector *connector)
  2753. {
  2754. struct nouveau_drm *drm = nouveau_drm(connector->dev);
  2755. struct nv50_mstc *mstc = nv50_mstc(connector);
  2756. drm_connector_unregister(&mstc->connector);
  2757. drm_modeset_lock_all(drm->dev);
  2758. drm_fb_helper_remove_one_connector(&drm->fbcon->helper, &mstc->connector);
  2759. mstc->port = NULL;
  2760. drm_modeset_unlock_all(drm->dev);
  2761. drm_connector_unreference(&mstc->connector);
  2762. }
  2763. static void
  2764. nv50_mstm_register_connector(struct drm_connector *connector)
  2765. {
  2766. struct nouveau_drm *drm = nouveau_drm(connector->dev);
  2767. drm_modeset_lock_all(drm->dev);
  2768. drm_fb_helper_add_one_connector(&drm->fbcon->helper, connector);
  2769. drm_modeset_unlock_all(drm->dev);
  2770. drm_connector_register(connector);
  2771. }
  2772. static struct drm_connector *
  2773. nv50_mstm_add_connector(struct drm_dp_mst_topology_mgr *mgr,
  2774. struct drm_dp_mst_port *port, const char *path)
  2775. {
  2776. struct nv50_mstm *mstm = nv50_mstm(mgr);
  2777. struct nv50_mstc *mstc;
  2778. int ret;
  2779. ret = nv50_mstc_new(mstm, port, path, &mstc);
  2780. if (ret) {
  2781. if (mstc)
  2782. mstc->connector.funcs->destroy(&mstc->connector);
  2783. return NULL;
  2784. }
  2785. return &mstc->connector;
  2786. }
  2787. static const struct drm_dp_mst_topology_cbs
  2788. nv50_mstm = {
  2789. .add_connector = nv50_mstm_add_connector,
  2790. .register_connector = nv50_mstm_register_connector,
  2791. .destroy_connector = nv50_mstm_destroy_connector,
  2792. .hotplug = nv50_mstm_hotplug,
  2793. };
  2794. void
  2795. nv50_mstm_service(struct nv50_mstm *mstm)
  2796. {
  2797. struct drm_dp_aux *aux = mstm->mgr.aux;
  2798. bool handled = true;
  2799. int ret;
  2800. u8 esi[8] = {};
  2801. while (handled) {
  2802. ret = drm_dp_dpcd_read(aux, DP_SINK_COUNT_ESI, esi, 8);
  2803. if (ret != 8) {
  2804. drm_dp_mst_topology_mgr_set_mst(&mstm->mgr, false);
  2805. return;
  2806. }
  2807. drm_dp_mst_hpd_irq(&mstm->mgr, esi, &handled);
  2808. if (!handled)
  2809. break;
  2810. drm_dp_dpcd_write(aux, DP_SINK_COUNT_ESI + 1, &esi[1], 3);
  2811. }
  2812. }
  2813. void
  2814. nv50_mstm_remove(struct nv50_mstm *mstm)
  2815. {
  2816. if (mstm)
  2817. drm_dp_mst_topology_mgr_set_mst(&mstm->mgr, false);
  2818. }
  2819. static int
  2820. nv50_mstm_enable(struct nv50_mstm *mstm, u8 dpcd, int state)
  2821. {
  2822. struct nouveau_encoder *outp = mstm->outp;
  2823. struct {
  2824. struct nv50_disp_mthd_v1 base;
  2825. struct nv50_disp_sor_dp_mst_link_v0 mst;
  2826. } args = {
  2827. .base.version = 1,
  2828. .base.method = NV50_DISP_MTHD_V1_SOR_DP_MST_LINK,
  2829. .base.hasht = outp->dcb->hasht,
  2830. .base.hashm = outp->dcb->hashm,
  2831. .mst.state = state,
  2832. };
  2833. struct nouveau_drm *drm = nouveau_drm(outp->base.base.dev);
  2834. struct nvif_object *disp = &drm->display->disp;
  2835. int ret;
  2836. if (dpcd >= 0x12) {
  2837. ret = drm_dp_dpcd_readb(mstm->mgr.aux, DP_MSTM_CTRL, &dpcd);
  2838. if (ret < 0)
  2839. return ret;
  2840. dpcd &= ~DP_MST_EN;
  2841. if (state)
  2842. dpcd |= DP_MST_EN;
  2843. ret = drm_dp_dpcd_writeb(mstm->mgr.aux, DP_MSTM_CTRL, dpcd);
  2844. if (ret < 0)
  2845. return ret;
  2846. }
  2847. return nvif_mthd(disp, 0, &args, sizeof(args));
  2848. }
  2849. int
  2850. nv50_mstm_detect(struct nv50_mstm *mstm, u8 dpcd[8], int allow)
  2851. {
  2852. int ret, state = 0;
  2853. if (!mstm)
  2854. return 0;
  2855. if (dpcd[0] >= 0x12) {
  2856. ret = drm_dp_dpcd_readb(mstm->mgr.aux, DP_MSTM_CAP, &dpcd[1]);
  2857. if (ret < 0)
  2858. return ret;
  2859. if (!(dpcd[1] & DP_MST_CAP))
  2860. dpcd[0] = 0x11;
  2861. else
  2862. state = allow;
  2863. }
  2864. ret = nv50_mstm_enable(mstm, dpcd[0], state);
  2865. if (ret)
  2866. return ret;
  2867. ret = drm_dp_mst_topology_mgr_set_mst(&mstm->mgr, state);
  2868. if (ret)
  2869. return nv50_mstm_enable(mstm, dpcd[0], 0);
  2870. return mstm->mgr.mst_state;
  2871. }
  2872. static void
  2873. nv50_mstm_fini(struct nv50_mstm *mstm)
  2874. {
  2875. if (mstm && mstm->mgr.mst_state)
  2876. drm_dp_mst_topology_mgr_suspend(&mstm->mgr);
  2877. }
  2878. static void
  2879. nv50_mstm_init(struct nv50_mstm *mstm)
  2880. {
  2881. if (mstm && mstm->mgr.mst_state)
  2882. drm_dp_mst_topology_mgr_resume(&mstm->mgr);
  2883. }
  2884. static void
  2885. nv50_mstm_del(struct nv50_mstm **pmstm)
  2886. {
  2887. struct nv50_mstm *mstm = *pmstm;
  2888. if (mstm) {
  2889. kfree(*pmstm);
  2890. *pmstm = NULL;
  2891. }
  2892. }
  2893. static int
  2894. nv50_mstm_new(struct nouveau_encoder *outp, struct drm_dp_aux *aux, int aux_max,
  2895. int conn_base_id, struct nv50_mstm **pmstm)
  2896. {
  2897. const int max_payloads = hweight8(outp->dcb->heads);
  2898. struct drm_device *dev = outp->base.base.dev;
  2899. struct nv50_mstm *mstm;
  2900. int ret, i;
  2901. u8 dpcd;
  2902. /* This is a workaround for some monitors not functioning
  2903. * correctly in MST mode on initial module load. I think
  2904. * some bad interaction with the VBIOS may be responsible.
  2905. *
  2906. * A good ol' off and on again seems to work here ;)
  2907. */
  2908. ret = drm_dp_dpcd_readb(aux, DP_DPCD_REV, &dpcd);
  2909. if (ret >= 0 && dpcd >= 0x12)
  2910. drm_dp_dpcd_writeb(aux, DP_MSTM_CTRL, 0);
  2911. if (!(mstm = *pmstm = kzalloc(sizeof(*mstm), GFP_KERNEL)))
  2912. return -ENOMEM;
  2913. mstm->outp = outp;
  2914. mstm->mgr.cbs = &nv50_mstm;
  2915. ret = drm_dp_mst_topology_mgr_init(&mstm->mgr, dev, aux, aux_max,
  2916. max_payloads, conn_base_id);
  2917. if (ret)
  2918. return ret;
  2919. for (i = 0; i < max_payloads; i++) {
  2920. ret = nv50_msto_new(dev, outp->dcb->heads, outp->base.base.name,
  2921. i, &mstm->msto[i]);
  2922. if (ret)
  2923. return ret;
  2924. }
  2925. return 0;
  2926. }
  2927. /******************************************************************************
  2928. * SOR
  2929. *****************************************************************************/
  2930. static void
  2931. nv50_sor_dpms(struct drm_encoder *encoder, int mode)
  2932. {
  2933. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  2934. struct nv50_disp *disp = nv50_disp(encoder->dev);
  2935. struct {
  2936. struct nv50_disp_mthd_v1 base;
  2937. struct nv50_disp_sor_pwr_v0 pwr;
  2938. } args = {
  2939. .base.version = 1,
  2940. .base.method = NV50_DISP_MTHD_V1_SOR_PWR,
  2941. .base.hasht = nv_encoder->dcb->hasht,
  2942. .base.hashm = nv_encoder->dcb->hashm,
  2943. .pwr.state = mode == DRM_MODE_DPMS_ON,
  2944. };
  2945. nvif_mthd(disp->disp, 0, &args, sizeof(args));
  2946. }
  2947. static void
  2948. nv50_sor_update(struct nouveau_encoder *nv_encoder, u8 head,
  2949. struct drm_display_mode *mode, u8 proto, u8 depth)
  2950. {
  2951. struct nv50_dmac *core = &nv50_mast(nv_encoder->base.base.dev)->base;
  2952. u32 *push;
  2953. if (!mode) {
  2954. nv_encoder->ctrl &= ~BIT(head);
  2955. if (!(nv_encoder->ctrl & 0x0000000f))
  2956. nv_encoder->ctrl = 0;
  2957. } else {
  2958. nv_encoder->ctrl |= proto << 8;
  2959. nv_encoder->ctrl |= BIT(head);
  2960. }
  2961. if ((push = evo_wait(core, 6))) {
  2962. if (core->base.user.oclass < GF110_DISP_CORE_CHANNEL_DMA) {
  2963. if (mode) {
  2964. if (mode->flags & DRM_MODE_FLAG_NHSYNC)
  2965. nv_encoder->ctrl |= 0x00001000;
  2966. if (mode->flags & DRM_MODE_FLAG_NVSYNC)
  2967. nv_encoder->ctrl |= 0x00002000;
  2968. nv_encoder->ctrl |= depth << 16;
  2969. }
  2970. evo_mthd(push, 0x0600 + (nv_encoder->or * 0x40), 1);
  2971. } else {
  2972. if (mode) {
  2973. u32 magic = 0x31ec6000 | (head << 25);
  2974. u32 syncs = 0x00000001;
  2975. if (mode->flags & DRM_MODE_FLAG_NHSYNC)
  2976. syncs |= 0x00000008;
  2977. if (mode->flags & DRM_MODE_FLAG_NVSYNC)
  2978. syncs |= 0x00000010;
  2979. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  2980. magic |= 0x00000001;
  2981. evo_mthd(push, 0x0404 + (head * 0x300), 2);
  2982. evo_data(push, syncs | (depth << 6));
  2983. evo_data(push, magic);
  2984. }
  2985. evo_mthd(push, 0x0200 + (nv_encoder->or * 0x20), 1);
  2986. }
  2987. evo_data(push, nv_encoder->ctrl);
  2988. evo_kick(push, core);
  2989. }
  2990. }
  2991. static void
  2992. nv50_sor_disable(struct drm_encoder *encoder)
  2993. {
  2994. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  2995. struct nouveau_crtc *nv_crtc = nouveau_crtc(nv_encoder->crtc);
  2996. nv_encoder->crtc = NULL;
  2997. if (nv_crtc) {
  2998. struct nvkm_i2c_aux *aux = nv_encoder->aux;
  2999. u8 pwr;
  3000. if (aux) {
  3001. int ret = nvkm_rdaux(aux, DP_SET_POWER, &pwr, 1);
  3002. if (ret == 0) {
  3003. pwr &= ~DP_SET_POWER_MASK;
  3004. pwr |= DP_SET_POWER_D3;
  3005. nvkm_wraux(aux, DP_SET_POWER, &pwr, 1);
  3006. }
  3007. }
  3008. nv_encoder->update(nv_encoder, nv_crtc->index, NULL, 0, 0);
  3009. nv50_audio_disable(encoder, nv_crtc);
  3010. nv50_hdmi_disable(&nv_encoder->base.base, nv_crtc);
  3011. }
  3012. }
  3013. static void
  3014. nv50_sor_enable(struct drm_encoder *encoder)
  3015. {
  3016. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  3017. struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
  3018. struct drm_display_mode *mode = &nv_crtc->base.state->adjusted_mode;
  3019. struct {
  3020. struct nv50_disp_mthd_v1 base;
  3021. struct nv50_disp_sor_lvds_script_v0 lvds;
  3022. } lvds = {
  3023. .base.version = 1,
  3024. .base.method = NV50_DISP_MTHD_V1_SOR_LVDS_SCRIPT,
  3025. .base.hasht = nv_encoder->dcb->hasht,
  3026. .base.hashm = nv_encoder->dcb->hashm,
  3027. };
  3028. struct nv50_disp *disp = nv50_disp(encoder->dev);
  3029. struct drm_device *dev = encoder->dev;
  3030. struct nouveau_drm *drm = nouveau_drm(dev);
  3031. struct nouveau_connector *nv_connector;
  3032. struct nvbios *bios = &drm->vbios;
  3033. u8 proto = 0xf;
  3034. u8 depth = 0x0;
  3035. nv_connector = nouveau_encoder_connector_get(nv_encoder);
  3036. nv_encoder->crtc = encoder->crtc;
  3037. switch (nv_encoder->dcb->type) {
  3038. case DCB_OUTPUT_TMDS:
  3039. if (nv_encoder->dcb->sorconf.link & 1) {
  3040. proto = 0x1;
  3041. /* Only enable dual-link if:
  3042. * - Need to (i.e. rate > 165MHz)
  3043. * - DCB says we can
  3044. * - Not an HDMI monitor, since there's no dual-link
  3045. * on HDMI.
  3046. */
  3047. if (mode->clock >= 165000 &&
  3048. nv_encoder->dcb->duallink_possible &&
  3049. !drm_detect_hdmi_monitor(nv_connector->edid))
  3050. proto |= 0x4;
  3051. } else {
  3052. proto = 0x2;
  3053. }
  3054. nv50_hdmi_enable(&nv_encoder->base.base, mode);
  3055. break;
  3056. case DCB_OUTPUT_LVDS:
  3057. proto = 0x0;
  3058. if (bios->fp_no_ddc) {
  3059. if (bios->fp.dual_link)
  3060. lvds.lvds.script |= 0x0100;
  3061. if (bios->fp.if_is_24bit)
  3062. lvds.lvds.script |= 0x0200;
  3063. } else {
  3064. if (nv_connector->type == DCB_CONNECTOR_LVDS_SPWG) {
  3065. if (((u8 *)nv_connector->edid)[121] == 2)
  3066. lvds.lvds.script |= 0x0100;
  3067. } else
  3068. if (mode->clock >= bios->fp.duallink_transition_clk) {
  3069. lvds.lvds.script |= 0x0100;
  3070. }
  3071. if (lvds.lvds.script & 0x0100) {
  3072. if (bios->fp.strapless_is_24bit & 2)
  3073. lvds.lvds.script |= 0x0200;
  3074. } else {
  3075. if (bios->fp.strapless_is_24bit & 1)
  3076. lvds.lvds.script |= 0x0200;
  3077. }
  3078. if (nv_connector->base.display_info.bpc == 8)
  3079. lvds.lvds.script |= 0x0200;
  3080. }
  3081. nvif_mthd(disp->disp, 0, &lvds, sizeof(lvds));
  3082. break;
  3083. case DCB_OUTPUT_DP:
  3084. if (nv_connector->base.display_info.bpc == 6)
  3085. depth = 0x2;
  3086. else
  3087. if (nv_connector->base.display_info.bpc == 8)
  3088. depth = 0x5;
  3089. else
  3090. depth = 0x6;
  3091. if (nv_encoder->dcb->sorconf.link & 1)
  3092. proto = 0x8;
  3093. else
  3094. proto = 0x9;
  3095. nv50_audio_enable(encoder, mode);
  3096. break;
  3097. default:
  3098. BUG();
  3099. break;
  3100. }
  3101. nv_encoder->update(nv_encoder, nv_crtc->index, mode, proto, depth);
  3102. }
  3103. static const struct drm_encoder_helper_funcs
  3104. nv50_sor_help = {
  3105. .dpms = nv50_sor_dpms,
  3106. .atomic_check = nv50_outp_atomic_check,
  3107. .enable = nv50_sor_enable,
  3108. .disable = nv50_sor_disable,
  3109. };
  3110. static void
  3111. nv50_sor_destroy(struct drm_encoder *encoder)
  3112. {
  3113. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  3114. nv50_mstm_del(&nv_encoder->dp.mstm);
  3115. drm_encoder_cleanup(encoder);
  3116. kfree(encoder);
  3117. }
  3118. static const struct drm_encoder_funcs
  3119. nv50_sor_func = {
  3120. .destroy = nv50_sor_destroy,
  3121. };
  3122. static int
  3123. nv50_sor_create(struct drm_connector *connector, struct dcb_output *dcbe)
  3124. {
  3125. struct nouveau_connector *nv_connector = nouveau_connector(connector);
  3126. struct nouveau_drm *drm = nouveau_drm(connector->dev);
  3127. struct nvkm_i2c *i2c = nvxx_i2c(&drm->client.device);
  3128. struct nouveau_encoder *nv_encoder;
  3129. struct drm_encoder *encoder;
  3130. int type, ret;
  3131. switch (dcbe->type) {
  3132. case DCB_OUTPUT_LVDS: type = DRM_MODE_ENCODER_LVDS; break;
  3133. case DCB_OUTPUT_TMDS:
  3134. case DCB_OUTPUT_DP:
  3135. default:
  3136. type = DRM_MODE_ENCODER_TMDS;
  3137. break;
  3138. }
  3139. nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
  3140. if (!nv_encoder)
  3141. return -ENOMEM;
  3142. nv_encoder->dcb = dcbe;
  3143. nv_encoder->or = ffs(dcbe->or) - 1;
  3144. nv_encoder->update = nv50_sor_update;
  3145. encoder = to_drm_encoder(nv_encoder);
  3146. encoder->possible_crtcs = dcbe->heads;
  3147. encoder->possible_clones = 0;
  3148. drm_encoder_init(connector->dev, encoder, &nv50_sor_func, type,
  3149. "sor-%04x-%04x", dcbe->hasht, dcbe->hashm);
  3150. drm_encoder_helper_add(encoder, &nv50_sor_help);
  3151. drm_mode_connector_attach_encoder(connector, encoder);
  3152. if (dcbe->type == DCB_OUTPUT_DP) {
  3153. struct nvkm_i2c_aux *aux =
  3154. nvkm_i2c_aux_find(i2c, dcbe->i2c_index);
  3155. if (aux) {
  3156. nv_encoder->i2c = &nv_connector->aux.ddc;
  3157. nv_encoder->aux = aux;
  3158. }
  3159. /*TODO: Use DP Info Table to check for support. */
  3160. if (nv50_disp(encoder->dev)->disp->oclass >= GF110_DISP) {
  3161. ret = nv50_mstm_new(nv_encoder, &nv_connector->aux, 16,
  3162. nv_connector->base.base.id,
  3163. &nv_encoder->dp.mstm);
  3164. if (ret)
  3165. return ret;
  3166. }
  3167. } else {
  3168. struct nvkm_i2c_bus *bus =
  3169. nvkm_i2c_bus_find(i2c, dcbe->i2c_index);
  3170. if (bus)
  3171. nv_encoder->i2c = &bus->i2c;
  3172. }
  3173. return 0;
  3174. }
  3175. /******************************************************************************
  3176. * PIOR
  3177. *****************************************************************************/
  3178. static void
  3179. nv50_pior_dpms(struct drm_encoder *encoder, int mode)
  3180. {
  3181. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  3182. struct nv50_disp *disp = nv50_disp(encoder->dev);
  3183. struct {
  3184. struct nv50_disp_mthd_v1 base;
  3185. struct nv50_disp_pior_pwr_v0 pwr;
  3186. } args = {
  3187. .base.version = 1,
  3188. .base.method = NV50_DISP_MTHD_V1_PIOR_PWR,
  3189. .base.hasht = nv_encoder->dcb->hasht,
  3190. .base.hashm = nv_encoder->dcb->hashm,
  3191. .pwr.state = mode == DRM_MODE_DPMS_ON,
  3192. .pwr.type = nv_encoder->dcb->type,
  3193. };
  3194. nvif_mthd(disp->disp, 0, &args, sizeof(args));
  3195. }
  3196. static int
  3197. nv50_pior_atomic_check(struct drm_encoder *encoder,
  3198. struct drm_crtc_state *crtc_state,
  3199. struct drm_connector_state *conn_state)
  3200. {
  3201. int ret = nv50_outp_atomic_check(encoder, crtc_state, conn_state);
  3202. if (ret)
  3203. return ret;
  3204. crtc_state->adjusted_mode.clock *= 2;
  3205. return 0;
  3206. }
  3207. static void
  3208. nv50_pior_disable(struct drm_encoder *encoder)
  3209. {
  3210. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  3211. struct nv50_mast *mast = nv50_mast(encoder->dev);
  3212. const int or = nv_encoder->or;
  3213. u32 *push;
  3214. if (nv_encoder->crtc) {
  3215. push = evo_wait(mast, 4);
  3216. if (push) {
  3217. if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
  3218. evo_mthd(push, 0x0700 + (or * 0x040), 1);
  3219. evo_data(push, 0x00000000);
  3220. }
  3221. evo_kick(push, mast);
  3222. }
  3223. }
  3224. nv_encoder->crtc = NULL;
  3225. }
  3226. static void
  3227. nv50_pior_enable(struct drm_encoder *encoder)
  3228. {
  3229. struct nv50_mast *mast = nv50_mast(encoder->dev);
  3230. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  3231. struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
  3232. struct nouveau_connector *nv_connector;
  3233. struct drm_display_mode *mode = &nv_crtc->base.state->adjusted_mode;
  3234. u8 owner = 1 << nv_crtc->index;
  3235. u8 proto, depth;
  3236. u32 *push;
  3237. nv_connector = nouveau_encoder_connector_get(nv_encoder);
  3238. switch (nv_connector->base.display_info.bpc) {
  3239. case 10: depth = 0x6; break;
  3240. case 8: depth = 0x5; break;
  3241. case 6: depth = 0x2; break;
  3242. default: depth = 0x0; break;
  3243. }
  3244. switch (nv_encoder->dcb->type) {
  3245. case DCB_OUTPUT_TMDS:
  3246. case DCB_OUTPUT_DP:
  3247. proto = 0x0;
  3248. break;
  3249. default:
  3250. BUG();
  3251. break;
  3252. }
  3253. push = evo_wait(mast, 8);
  3254. if (push) {
  3255. if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
  3256. u32 ctrl = (depth << 16) | (proto << 8) | owner;
  3257. if (mode->flags & DRM_MODE_FLAG_NHSYNC)
  3258. ctrl |= 0x00001000;
  3259. if (mode->flags & DRM_MODE_FLAG_NVSYNC)
  3260. ctrl |= 0x00002000;
  3261. evo_mthd(push, 0x0700 + (nv_encoder->or * 0x040), 1);
  3262. evo_data(push, ctrl);
  3263. }
  3264. evo_kick(push, mast);
  3265. }
  3266. nv_encoder->crtc = encoder->crtc;
  3267. }
  3268. static const struct drm_encoder_helper_funcs
  3269. nv50_pior_help = {
  3270. .dpms = nv50_pior_dpms,
  3271. .atomic_check = nv50_pior_atomic_check,
  3272. .enable = nv50_pior_enable,
  3273. .disable = nv50_pior_disable,
  3274. };
  3275. static void
  3276. nv50_pior_destroy(struct drm_encoder *encoder)
  3277. {
  3278. drm_encoder_cleanup(encoder);
  3279. kfree(encoder);
  3280. }
  3281. static const struct drm_encoder_funcs
  3282. nv50_pior_func = {
  3283. .destroy = nv50_pior_destroy,
  3284. };
  3285. static int
  3286. nv50_pior_create(struct drm_connector *connector, struct dcb_output *dcbe)
  3287. {
  3288. struct nouveau_connector *nv_connector = nouveau_connector(connector);
  3289. struct nouveau_drm *drm = nouveau_drm(connector->dev);
  3290. struct nvkm_i2c *i2c = nvxx_i2c(&drm->client.device);
  3291. struct nvkm_i2c_bus *bus = NULL;
  3292. struct nvkm_i2c_aux *aux = NULL;
  3293. struct i2c_adapter *ddc;
  3294. struct nouveau_encoder *nv_encoder;
  3295. struct drm_encoder *encoder;
  3296. int type;
  3297. switch (dcbe->type) {
  3298. case DCB_OUTPUT_TMDS:
  3299. bus = nvkm_i2c_bus_find(i2c, NVKM_I2C_BUS_EXT(dcbe->extdev));
  3300. ddc = bus ? &bus->i2c : NULL;
  3301. type = DRM_MODE_ENCODER_TMDS;
  3302. break;
  3303. case DCB_OUTPUT_DP:
  3304. aux = nvkm_i2c_aux_find(i2c, NVKM_I2C_AUX_EXT(dcbe->extdev));
  3305. ddc = aux ? &nv_connector->aux.ddc : NULL;
  3306. type = DRM_MODE_ENCODER_TMDS;
  3307. break;
  3308. default:
  3309. return -ENODEV;
  3310. }
  3311. nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
  3312. if (!nv_encoder)
  3313. return -ENOMEM;
  3314. nv_encoder->dcb = dcbe;
  3315. nv_encoder->or = ffs(dcbe->or) - 1;
  3316. nv_encoder->i2c = ddc;
  3317. nv_encoder->aux = aux;
  3318. encoder = to_drm_encoder(nv_encoder);
  3319. encoder->possible_crtcs = dcbe->heads;
  3320. encoder->possible_clones = 0;
  3321. drm_encoder_init(connector->dev, encoder, &nv50_pior_func, type,
  3322. "pior-%04x-%04x", dcbe->hasht, dcbe->hashm);
  3323. drm_encoder_helper_add(encoder, &nv50_pior_help);
  3324. drm_mode_connector_attach_encoder(connector, encoder);
  3325. return 0;
  3326. }
  3327. /******************************************************************************
  3328. * Atomic
  3329. *****************************************************************************/
  3330. static void
  3331. nv50_disp_atomic_commit_core(struct nouveau_drm *drm, u32 interlock)
  3332. {
  3333. struct nv50_disp *disp = nv50_disp(drm->dev);
  3334. struct nv50_dmac *core = &disp->mast.base;
  3335. struct nv50_mstm *mstm;
  3336. struct drm_encoder *encoder;
  3337. u32 *push;
  3338. NV_ATOMIC(drm, "commit core %08x\n", interlock);
  3339. drm_for_each_encoder(encoder, drm->dev) {
  3340. if (encoder->encoder_type != DRM_MODE_ENCODER_DPMST) {
  3341. mstm = nouveau_encoder(encoder)->dp.mstm;
  3342. if (mstm && mstm->modified)
  3343. nv50_mstm_prepare(mstm);
  3344. }
  3345. }
  3346. if ((push = evo_wait(core, 5))) {
  3347. evo_mthd(push, 0x0084, 1);
  3348. evo_data(push, 0x80000000);
  3349. evo_mthd(push, 0x0080, 2);
  3350. evo_data(push, interlock);
  3351. evo_data(push, 0x00000000);
  3352. nouveau_bo_wr32(disp->sync, 0, 0x00000000);
  3353. evo_kick(push, core);
  3354. if (nvif_msec(&drm->client.device, 2000ULL,
  3355. if (nouveau_bo_rd32(disp->sync, 0))
  3356. break;
  3357. usleep_range(1, 2);
  3358. ) < 0)
  3359. NV_ERROR(drm, "EVO timeout\n");
  3360. }
  3361. drm_for_each_encoder(encoder, drm->dev) {
  3362. if (encoder->encoder_type != DRM_MODE_ENCODER_DPMST) {
  3363. mstm = nouveau_encoder(encoder)->dp.mstm;
  3364. if (mstm && mstm->modified)
  3365. nv50_mstm_cleanup(mstm);
  3366. }
  3367. }
  3368. }
  3369. static void
  3370. nv50_disp_atomic_commit_tail(struct drm_atomic_state *state)
  3371. {
  3372. struct drm_device *dev = state->dev;
  3373. struct drm_crtc_state *crtc_state;
  3374. struct drm_crtc *crtc;
  3375. struct drm_plane_state *plane_state;
  3376. struct drm_plane *plane;
  3377. struct nouveau_drm *drm = nouveau_drm(dev);
  3378. struct nv50_disp *disp = nv50_disp(dev);
  3379. struct nv50_atom *atom = nv50_atom(state);
  3380. struct nv50_outp_atom *outp, *outt;
  3381. u32 interlock_core = 0;
  3382. u32 interlock_chan = 0;
  3383. int i;
  3384. NV_ATOMIC(drm, "commit %d %d\n", atom->lock_core, atom->flush_disable);
  3385. drm_atomic_helper_wait_for_fences(dev, state, false);
  3386. drm_atomic_helper_wait_for_dependencies(state);
  3387. drm_atomic_helper_update_legacy_modeset_state(dev, state);
  3388. if (atom->lock_core)
  3389. mutex_lock(&disp->mutex);
  3390. /* Disable head(s). */
  3391. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  3392. struct nv50_head_atom *asyh = nv50_head_atom(crtc->state);
  3393. struct nv50_head *head = nv50_head(crtc);
  3394. NV_ATOMIC(drm, "%s: clr %04x (set %04x)\n", crtc->name,
  3395. asyh->clr.mask, asyh->set.mask);
  3396. if (asyh->clr.mask) {
  3397. nv50_head_flush_clr(head, asyh, atom->flush_disable);
  3398. interlock_core |= 1;
  3399. }
  3400. }
  3401. /* Disable plane(s). */
  3402. for_each_plane_in_state(state, plane, plane_state, i) {
  3403. struct nv50_wndw_atom *asyw = nv50_wndw_atom(plane->state);
  3404. struct nv50_wndw *wndw = nv50_wndw(plane);
  3405. NV_ATOMIC(drm, "%s: clr %02x (set %02x)\n", plane->name,
  3406. asyw->clr.mask, asyw->set.mask);
  3407. if (!asyw->clr.mask)
  3408. continue;
  3409. interlock_chan |= nv50_wndw_flush_clr(wndw, interlock_core,
  3410. atom->flush_disable,
  3411. asyw);
  3412. }
  3413. /* Disable output path(s). */
  3414. list_for_each_entry(outp, &atom->outp, head) {
  3415. const struct drm_encoder_helper_funcs *help;
  3416. struct drm_encoder *encoder;
  3417. encoder = outp->encoder;
  3418. help = encoder->helper_private;
  3419. NV_ATOMIC(drm, "%s: clr %02x (set %02x)\n", encoder->name,
  3420. outp->clr.mask, outp->set.mask);
  3421. if (outp->clr.mask) {
  3422. help->disable(encoder);
  3423. interlock_core |= 1;
  3424. if (outp->flush_disable) {
  3425. nv50_disp_atomic_commit_core(drm, interlock_chan);
  3426. interlock_core = 0;
  3427. interlock_chan = 0;
  3428. }
  3429. }
  3430. }
  3431. /* Flush disable. */
  3432. if (interlock_core) {
  3433. if (atom->flush_disable) {
  3434. nv50_disp_atomic_commit_core(drm, interlock_chan);
  3435. interlock_core = 0;
  3436. interlock_chan = 0;
  3437. }
  3438. }
  3439. /* Update output path(s). */
  3440. list_for_each_entry_safe(outp, outt, &atom->outp, head) {
  3441. const struct drm_encoder_helper_funcs *help;
  3442. struct drm_encoder *encoder;
  3443. encoder = outp->encoder;
  3444. help = encoder->helper_private;
  3445. NV_ATOMIC(drm, "%s: set %02x (clr %02x)\n", encoder->name,
  3446. outp->set.mask, outp->clr.mask);
  3447. if (outp->set.mask) {
  3448. help->enable(encoder);
  3449. interlock_core = 1;
  3450. }
  3451. list_del(&outp->head);
  3452. kfree(outp);
  3453. }
  3454. /* Update head(s). */
  3455. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  3456. struct nv50_head_atom *asyh = nv50_head_atom(crtc->state);
  3457. struct nv50_head *head = nv50_head(crtc);
  3458. NV_ATOMIC(drm, "%s: set %04x (clr %04x)\n", crtc->name,
  3459. asyh->set.mask, asyh->clr.mask);
  3460. if (asyh->set.mask) {
  3461. nv50_head_flush_set(head, asyh);
  3462. interlock_core = 1;
  3463. }
  3464. }
  3465. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  3466. if (crtc->state->event)
  3467. drm_crtc_vblank_get(crtc);
  3468. }
  3469. /* Update plane(s). */
  3470. for_each_plane_in_state(state, plane, plane_state, i) {
  3471. struct nv50_wndw_atom *asyw = nv50_wndw_atom(plane->state);
  3472. struct nv50_wndw *wndw = nv50_wndw(plane);
  3473. NV_ATOMIC(drm, "%s: set %02x (clr %02x)\n", plane->name,
  3474. asyw->set.mask, asyw->clr.mask);
  3475. if ( !asyw->set.mask &&
  3476. (!asyw->clr.mask || atom->flush_disable))
  3477. continue;
  3478. interlock_chan |= nv50_wndw_flush_set(wndw, interlock_core, asyw);
  3479. }
  3480. /* Flush update. */
  3481. if (interlock_core) {
  3482. if (!interlock_chan && atom->state.legacy_cursor_update) {
  3483. u32 *push = evo_wait(&disp->mast, 2);
  3484. if (push) {
  3485. evo_mthd(push, 0x0080, 1);
  3486. evo_data(push, 0x00000000);
  3487. evo_kick(push, &disp->mast);
  3488. }
  3489. } else {
  3490. nv50_disp_atomic_commit_core(drm, interlock_chan);
  3491. }
  3492. }
  3493. if (atom->lock_core)
  3494. mutex_unlock(&disp->mutex);
  3495. /* Wait for HW to signal completion. */
  3496. for_each_plane_in_state(state, plane, plane_state, i) {
  3497. struct nv50_wndw_atom *asyw = nv50_wndw_atom(plane->state);
  3498. struct nv50_wndw *wndw = nv50_wndw(plane);
  3499. int ret = nv50_wndw_wait_armed(wndw, asyw);
  3500. if (ret)
  3501. NV_ERROR(drm, "%s: timeout\n", plane->name);
  3502. }
  3503. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  3504. if (crtc->state->event) {
  3505. unsigned long flags;
  3506. /* Get correct count/ts if racing with vblank irq */
  3507. drm_accurate_vblank_count(crtc);
  3508. spin_lock_irqsave(&crtc->dev->event_lock, flags);
  3509. drm_crtc_send_vblank_event(crtc, crtc->state->event);
  3510. spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
  3511. crtc->state->event = NULL;
  3512. drm_crtc_vblank_put(crtc);
  3513. }
  3514. }
  3515. drm_atomic_helper_commit_hw_done(state);
  3516. drm_atomic_helper_cleanup_planes(dev, state);
  3517. drm_atomic_helper_commit_cleanup_done(state);
  3518. drm_atomic_state_put(state);
  3519. }
  3520. static void
  3521. nv50_disp_atomic_commit_work(struct work_struct *work)
  3522. {
  3523. struct drm_atomic_state *state =
  3524. container_of(work, typeof(*state), commit_work);
  3525. nv50_disp_atomic_commit_tail(state);
  3526. }
  3527. static int
  3528. nv50_disp_atomic_commit(struct drm_device *dev,
  3529. struct drm_atomic_state *state, bool nonblock)
  3530. {
  3531. struct nouveau_drm *drm = nouveau_drm(dev);
  3532. struct nv50_disp *disp = nv50_disp(dev);
  3533. struct drm_plane_state *plane_state;
  3534. struct drm_plane *plane;
  3535. struct drm_crtc *crtc;
  3536. bool active = false;
  3537. int ret, i;
  3538. ret = pm_runtime_get_sync(dev->dev);
  3539. if (ret < 0 && ret != -EACCES)
  3540. return ret;
  3541. ret = drm_atomic_helper_setup_commit(state, nonblock);
  3542. if (ret)
  3543. goto done;
  3544. INIT_WORK(&state->commit_work, nv50_disp_atomic_commit_work);
  3545. ret = drm_atomic_helper_prepare_planes(dev, state);
  3546. if (ret)
  3547. goto done;
  3548. if (!nonblock) {
  3549. ret = drm_atomic_helper_wait_for_fences(dev, state, true);
  3550. if (ret)
  3551. goto done;
  3552. }
  3553. for_each_plane_in_state(state, plane, plane_state, i) {
  3554. struct nv50_wndw_atom *asyw = nv50_wndw_atom(plane_state);
  3555. struct nv50_wndw *wndw = nv50_wndw(plane);
  3556. if (asyw->set.image) {
  3557. asyw->ntfy.handle = wndw->dmac->sync.handle;
  3558. asyw->ntfy.offset = wndw->ntfy;
  3559. asyw->ntfy.awaken = false;
  3560. asyw->set.ntfy = true;
  3561. nouveau_bo_wr32(disp->sync, wndw->ntfy / 4, 0x00000000);
  3562. wndw->ntfy ^= 0x10;
  3563. }
  3564. }
  3565. drm_atomic_helper_swap_state(state, true);
  3566. drm_atomic_state_get(state);
  3567. if (nonblock)
  3568. queue_work(system_unbound_wq, &state->commit_work);
  3569. else
  3570. nv50_disp_atomic_commit_tail(state);
  3571. drm_for_each_crtc(crtc, dev) {
  3572. if (crtc->state->enable) {
  3573. if (!drm->have_disp_power_ref) {
  3574. drm->have_disp_power_ref = true;
  3575. return ret;
  3576. }
  3577. active = true;
  3578. break;
  3579. }
  3580. }
  3581. if (!active && drm->have_disp_power_ref) {
  3582. pm_runtime_put_autosuspend(dev->dev);
  3583. drm->have_disp_power_ref = false;
  3584. }
  3585. done:
  3586. pm_runtime_put_autosuspend(dev->dev);
  3587. return ret;
  3588. }
  3589. static struct nv50_outp_atom *
  3590. nv50_disp_outp_atomic_add(struct nv50_atom *atom, struct drm_encoder *encoder)
  3591. {
  3592. struct nv50_outp_atom *outp;
  3593. list_for_each_entry(outp, &atom->outp, head) {
  3594. if (outp->encoder == encoder)
  3595. return outp;
  3596. }
  3597. outp = kzalloc(sizeof(*outp), GFP_KERNEL);
  3598. if (!outp)
  3599. return ERR_PTR(-ENOMEM);
  3600. list_add(&outp->head, &atom->outp);
  3601. outp->encoder = encoder;
  3602. return outp;
  3603. }
  3604. static int
  3605. nv50_disp_outp_atomic_check_clr(struct nv50_atom *atom,
  3606. struct drm_connector *connector)
  3607. {
  3608. struct drm_encoder *encoder = connector->state->best_encoder;
  3609. struct drm_crtc_state *crtc_state;
  3610. struct drm_crtc *crtc;
  3611. struct nv50_outp_atom *outp;
  3612. if (!(crtc = connector->state->crtc))
  3613. return 0;
  3614. crtc_state = drm_atomic_get_existing_crtc_state(&atom->state, crtc);
  3615. if (crtc->state->active && drm_atomic_crtc_needs_modeset(crtc_state)) {
  3616. outp = nv50_disp_outp_atomic_add(atom, encoder);
  3617. if (IS_ERR(outp))
  3618. return PTR_ERR(outp);
  3619. if (outp->encoder->encoder_type == DRM_MODE_ENCODER_DPMST) {
  3620. outp->flush_disable = true;
  3621. atom->flush_disable = true;
  3622. }
  3623. outp->clr.ctrl = true;
  3624. atom->lock_core = true;
  3625. }
  3626. return 0;
  3627. }
  3628. static int
  3629. nv50_disp_outp_atomic_check_set(struct nv50_atom *atom,
  3630. struct drm_connector_state *connector_state)
  3631. {
  3632. struct drm_encoder *encoder = connector_state->best_encoder;
  3633. struct drm_crtc_state *crtc_state;
  3634. struct drm_crtc *crtc;
  3635. struct nv50_outp_atom *outp;
  3636. if (!(crtc = connector_state->crtc))
  3637. return 0;
  3638. crtc_state = drm_atomic_get_existing_crtc_state(&atom->state, crtc);
  3639. if (crtc_state->active && drm_atomic_crtc_needs_modeset(crtc_state)) {
  3640. outp = nv50_disp_outp_atomic_add(atom, encoder);
  3641. if (IS_ERR(outp))
  3642. return PTR_ERR(outp);
  3643. outp->set.ctrl = true;
  3644. atom->lock_core = true;
  3645. }
  3646. return 0;
  3647. }
  3648. static int
  3649. nv50_disp_atomic_check(struct drm_device *dev, struct drm_atomic_state *state)
  3650. {
  3651. struct nv50_atom *atom = nv50_atom(state);
  3652. struct drm_connector_state *connector_state;
  3653. struct drm_connector *connector;
  3654. int ret, i;
  3655. ret = drm_atomic_helper_check(dev, state);
  3656. if (ret)
  3657. return ret;
  3658. for_each_connector_in_state(state, connector, connector_state, i) {
  3659. ret = nv50_disp_outp_atomic_check_clr(atom, connector);
  3660. if (ret)
  3661. return ret;
  3662. ret = nv50_disp_outp_atomic_check_set(atom, connector_state);
  3663. if (ret)
  3664. return ret;
  3665. }
  3666. return 0;
  3667. }
  3668. static void
  3669. nv50_disp_atomic_state_clear(struct drm_atomic_state *state)
  3670. {
  3671. struct nv50_atom *atom = nv50_atom(state);
  3672. struct nv50_outp_atom *outp, *outt;
  3673. list_for_each_entry_safe(outp, outt, &atom->outp, head) {
  3674. list_del(&outp->head);
  3675. kfree(outp);
  3676. }
  3677. drm_atomic_state_default_clear(state);
  3678. }
  3679. static void
  3680. nv50_disp_atomic_state_free(struct drm_atomic_state *state)
  3681. {
  3682. struct nv50_atom *atom = nv50_atom(state);
  3683. drm_atomic_state_default_release(&atom->state);
  3684. kfree(atom);
  3685. }
  3686. static struct drm_atomic_state *
  3687. nv50_disp_atomic_state_alloc(struct drm_device *dev)
  3688. {
  3689. struct nv50_atom *atom;
  3690. if (!(atom = kzalloc(sizeof(*atom), GFP_KERNEL)) ||
  3691. drm_atomic_state_init(dev, &atom->state) < 0) {
  3692. kfree(atom);
  3693. return NULL;
  3694. }
  3695. INIT_LIST_HEAD(&atom->outp);
  3696. return &atom->state;
  3697. }
  3698. static const struct drm_mode_config_funcs
  3699. nv50_disp_func = {
  3700. .fb_create = nouveau_user_framebuffer_create,
  3701. .output_poll_changed = nouveau_fbcon_output_poll_changed,
  3702. .atomic_check = nv50_disp_atomic_check,
  3703. .atomic_commit = nv50_disp_atomic_commit,
  3704. .atomic_state_alloc = nv50_disp_atomic_state_alloc,
  3705. .atomic_state_clear = nv50_disp_atomic_state_clear,
  3706. .atomic_state_free = nv50_disp_atomic_state_free,
  3707. };
  3708. /******************************************************************************
  3709. * Init
  3710. *****************************************************************************/
  3711. void
  3712. nv50_display_fini(struct drm_device *dev)
  3713. {
  3714. struct nouveau_encoder *nv_encoder;
  3715. struct drm_encoder *encoder;
  3716. struct drm_plane *plane;
  3717. drm_for_each_plane(plane, dev) {
  3718. struct nv50_wndw *wndw = nv50_wndw(plane);
  3719. if (plane->funcs != &nv50_wndw)
  3720. continue;
  3721. nv50_wndw_fini(wndw);
  3722. }
  3723. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  3724. if (encoder->encoder_type != DRM_MODE_ENCODER_DPMST) {
  3725. nv_encoder = nouveau_encoder(encoder);
  3726. nv50_mstm_fini(nv_encoder->dp.mstm);
  3727. }
  3728. }
  3729. }
  3730. int
  3731. nv50_display_init(struct drm_device *dev)
  3732. {
  3733. struct drm_encoder *encoder;
  3734. struct drm_plane *plane;
  3735. struct drm_crtc *crtc;
  3736. u32 *push;
  3737. push = evo_wait(nv50_mast(dev), 32);
  3738. if (!push)
  3739. return -EBUSY;
  3740. evo_mthd(push, 0x0088, 1);
  3741. evo_data(push, nv50_mast(dev)->base.sync.handle);
  3742. evo_kick(push, nv50_mast(dev));
  3743. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  3744. if (encoder->encoder_type != DRM_MODE_ENCODER_DPMST) {
  3745. const struct drm_encoder_helper_funcs *help;
  3746. struct nouveau_encoder *nv_encoder;
  3747. nv_encoder = nouveau_encoder(encoder);
  3748. help = encoder->helper_private;
  3749. if (help && help->dpms)
  3750. help->dpms(encoder, DRM_MODE_DPMS_ON);
  3751. nv50_mstm_init(nv_encoder->dp.mstm);
  3752. }
  3753. }
  3754. drm_for_each_crtc(crtc, dev) {
  3755. nv50_head_lut_load(crtc);
  3756. }
  3757. drm_for_each_plane(plane, dev) {
  3758. struct nv50_wndw *wndw = nv50_wndw(plane);
  3759. if (plane->funcs != &nv50_wndw)
  3760. continue;
  3761. nv50_wndw_init(wndw);
  3762. }
  3763. return 0;
  3764. }
  3765. void
  3766. nv50_display_destroy(struct drm_device *dev)
  3767. {
  3768. struct nv50_disp *disp = nv50_disp(dev);
  3769. nv50_dmac_destroy(&disp->mast.base, disp->disp);
  3770. nouveau_bo_unmap(disp->sync);
  3771. if (disp->sync)
  3772. nouveau_bo_unpin(disp->sync);
  3773. nouveau_bo_ref(NULL, &disp->sync);
  3774. nouveau_display(dev)->priv = NULL;
  3775. kfree(disp);
  3776. }
  3777. MODULE_PARM_DESC(atomic, "Expose atomic ioctl (default: disabled)");
  3778. static int nouveau_atomic = 0;
  3779. module_param_named(atomic, nouveau_atomic, int, 0400);
  3780. int
  3781. nv50_display_create(struct drm_device *dev)
  3782. {
  3783. struct nvif_device *device = &nouveau_drm(dev)->client.device;
  3784. struct nouveau_drm *drm = nouveau_drm(dev);
  3785. struct dcb_table *dcb = &drm->vbios.dcb;
  3786. struct drm_connector *connector, *tmp;
  3787. struct nv50_disp *disp;
  3788. struct dcb_output *dcbe;
  3789. int crtcs, ret, i;
  3790. disp = kzalloc(sizeof(*disp), GFP_KERNEL);
  3791. if (!disp)
  3792. return -ENOMEM;
  3793. mutex_init(&disp->mutex);
  3794. nouveau_display(dev)->priv = disp;
  3795. nouveau_display(dev)->dtor = nv50_display_destroy;
  3796. nouveau_display(dev)->init = nv50_display_init;
  3797. nouveau_display(dev)->fini = nv50_display_fini;
  3798. disp->disp = &nouveau_display(dev)->disp;
  3799. dev->mode_config.funcs = &nv50_disp_func;
  3800. if (nouveau_atomic)
  3801. dev->driver->driver_features |= DRIVER_ATOMIC;
  3802. /* small shared memory area we use for notifiers and semaphores */
  3803. ret = nouveau_bo_new(&drm->client, 4096, 0x1000, TTM_PL_FLAG_VRAM,
  3804. 0, 0x0000, NULL, NULL, &disp->sync);
  3805. if (!ret) {
  3806. ret = nouveau_bo_pin(disp->sync, TTM_PL_FLAG_VRAM, true);
  3807. if (!ret) {
  3808. ret = nouveau_bo_map(disp->sync);
  3809. if (ret)
  3810. nouveau_bo_unpin(disp->sync);
  3811. }
  3812. if (ret)
  3813. nouveau_bo_ref(NULL, &disp->sync);
  3814. }
  3815. if (ret)
  3816. goto out;
  3817. /* allocate master evo channel */
  3818. ret = nv50_core_create(device, disp->disp, disp->sync->bo.offset,
  3819. &disp->mast);
  3820. if (ret)
  3821. goto out;
  3822. /* create crtc objects to represent the hw heads */
  3823. if (disp->disp->oclass >= GF110_DISP)
  3824. crtcs = nvif_rd32(&device->object, 0x022448);
  3825. else
  3826. crtcs = 2;
  3827. for (i = 0; i < crtcs; i++) {
  3828. ret = nv50_head_create(dev, i);
  3829. if (ret)
  3830. goto out;
  3831. }
  3832. /* create encoder/connector objects based on VBIOS DCB table */
  3833. for (i = 0, dcbe = &dcb->entry[0]; i < dcb->entries; i++, dcbe++) {
  3834. connector = nouveau_connector_create(dev, dcbe->connector);
  3835. if (IS_ERR(connector))
  3836. continue;
  3837. if (dcbe->location == DCB_LOC_ON_CHIP) {
  3838. switch (dcbe->type) {
  3839. case DCB_OUTPUT_TMDS:
  3840. case DCB_OUTPUT_LVDS:
  3841. case DCB_OUTPUT_DP:
  3842. ret = nv50_sor_create(connector, dcbe);
  3843. break;
  3844. case DCB_OUTPUT_ANALOG:
  3845. ret = nv50_dac_create(connector, dcbe);
  3846. break;
  3847. default:
  3848. ret = -ENODEV;
  3849. break;
  3850. }
  3851. } else {
  3852. ret = nv50_pior_create(connector, dcbe);
  3853. }
  3854. if (ret) {
  3855. NV_WARN(drm, "failed to create encoder %d/%d/%d: %d\n",
  3856. dcbe->location, dcbe->type,
  3857. ffs(dcbe->or) - 1, ret);
  3858. ret = 0;
  3859. }
  3860. }
  3861. /* cull any connectors we created that don't have an encoder */
  3862. list_for_each_entry_safe(connector, tmp, &dev->mode_config.connector_list, head) {
  3863. if (connector->encoder_ids[0])
  3864. continue;
  3865. NV_WARN(drm, "%s has no encoders, removing\n",
  3866. connector->name);
  3867. connector->funcs->destroy(connector);
  3868. }
  3869. out:
  3870. if (ret)
  3871. nv50_display_destroy(dev);
  3872. return ret;
  3873. }