nv50_display.c 116 KB

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