nv50_display.c 116 KB

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