mtk_drm_crtc.c 208 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511
  1. /*
  2. * Copyright (c) 2015 MediaTek Inc.
  3. * Copyright (C) 2021 XiaoMi, Inc.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include <asm/barrier.h>
  15. #include <drm/drmP.h>
  16. #include <drm/drm_atomic.h>
  17. #include <drm/drm_atomic_helper.h>
  18. #include <drm/drm_crtc_helper.h>
  19. #include <drm/drm_plane_helper.h>
  20. #include <drm/drm_fourcc.h>
  21. #include <linux/clk.h>
  22. #include <linux/pm_runtime.h>
  23. #include <linux/soc/mediatek/mtk-cmdq.h>
  24. #include <linux/mailbox_controller.h>
  25. #include <linux/of_address.h>
  26. #include <linux/of_platform.h>
  27. #include <soc/mediatek/smi.h>
  28. #include <linux/kthread.h>
  29. #include <linux/sched.h>
  30. #include <uapi/linux/sched/types.h>
  31. #include "mtk_drm_arr.h"
  32. #include "mtk_drm_drv.h"
  33. #include "mtk_drm_crtc.h"
  34. #include "mtk_drm_ddp.h"
  35. #include "mtk_drm_ddp_comp.h"
  36. #include "mtk_drm_gem.h"
  37. #include "mtk_drm_plane.h"
  38. #include "mtk_writeback.h"
  39. #include "mtk_fence.h"
  40. #include "mtk_sync.h"
  41. #include "mtk_drm_session.h"
  42. #include "mtk_dump.h"
  43. #include "mtk_drm_fb.h"
  44. #include "mtk_rect.h"
  45. #include "mtk_drm_ddp_addon.h"
  46. #include "mtk_drm_helper.h"
  47. #include "mtk_drm_lowpower.h"
  48. #include "mtk_drm_fbdev.h"
  49. #include "mtk_drm_assert.h"
  50. #include "mtk_drm_mmp.h"
  51. #include "mtk_disp_recovery.h"
  52. #include "mtk_drm_arr.h"
  53. #include "mtk_drm_trace.h"
  54. #include "cmdq-sec.h"
  55. #include "cmdq-sec-iwc-common.h"
  56. #include "mtk_disp_ccorr.h"
  57. /* *****Panel_Master*********** */
  58. #include "mtk_fbconfig_kdebug.h"
  59. #include "mtk_layering_rule_base.h"
  60. static struct mtk_drm_property mtk_crtc_property[CRTC_PROP_MAX] = {
  61. {DRM_MODE_PROP_ATOMIC, "OVERLAP_LAYER_NUM", 0, UINT_MAX, 0},
  62. {DRM_MODE_PROP_ATOMIC, "LAYERING_IDX", 0, UINT_MAX, 0},
  63. {DRM_MODE_PROP_ATOMIC, "PRESENT_FENCE", 0, UINT_MAX, 0},
  64. {DRM_MODE_PROP_ATOMIC, "DOZE_ACTIVE", 0, UINT_MAX, 0},
  65. {DRM_MODE_PROP_ATOMIC, "OUTPUT_ENABLE", 0, UINT_MAX, 0},
  66. {DRM_MODE_PROP_ATOMIC, "OUTPUT_BUFF_IDX", 0, UINT_MAX, 0},
  67. {DRM_MODE_PROP_ATOMIC, "OUTPUT_X", 0, UINT_MAX, 0},
  68. {DRM_MODE_PROP_ATOMIC, "OUTPUT_Y", 0, UINT_MAX, 0},
  69. {DRM_MODE_PROP_ATOMIC, "OUTPUT_WIDTH", 0, UINT_MAX, 0},
  70. {DRM_MODE_PROP_ATOMIC, "OUTPUT_HEIGHT", 0, UINT_MAX, 0},
  71. {DRM_MODE_PROP_ATOMIC, "OUTPUT_FB_ID", 0, UINT_MAX, 0},
  72. {DRM_MODE_PROP_ATOMIC, "INTF_BUFF_IDX", 0, UINT_MAX, 0},
  73. {DRM_MODE_PROP_ATOMIC, "DISP_MODE_IDX", 0, UINT_MAX, 0},
  74. {DRM_MODE_PROP_ATOMIC, "HBM_ENABLE", 0, UINT_MAX, 0},
  75. {DRM_MODE_PROP_ATOMIC, "COLOR_TRANSFORM", 0, UINT_MAX, 0},
  76. {DRM_MODE_PROP_ATOMIC, "USER_SCEN", 0, UINT_MAX, 0},
  77. {DRM_MODE_PROP_ATOMIC, "HDR_ENABLE", 0, UINT_MAX, 0},
  78. };
  79. bool hdr_en;
  80. static const char * const crtc_gce_client_str[] = {
  81. DECLARE_GCE_CLIENT(DECLARE_STR)};
  82. static struct pm_qos_request mm_freq_request;
  83. #ifdef MTK_FB_MMDVFS_SUPPORT
  84. static u64 freq_steps[MAX_FREQ_STEP];
  85. static u32 step_size;
  86. #endif
  87. struct drm_crtc *_get_context(void)
  88. {
  89. static int is_context_inited;
  90. static struct drm_crtc g_context;
  91. if (!is_context_inited) {
  92. memset((void *)&g_context, 0, sizeof(
  93. struct drm_crtc));
  94. is_context_inited = 1;
  95. }
  96. return &g_context;
  97. }
  98. static void mtk_drm_crtc_finish_page_flip(struct mtk_drm_crtc *mtk_crtc)
  99. {
  100. struct drm_crtc *crtc = &mtk_crtc->base;
  101. unsigned long flags;
  102. spin_lock_irqsave(&crtc->dev->event_lock, flags);
  103. drm_crtc_send_vblank_event(crtc, mtk_crtc->event);
  104. drm_crtc_vblank_put(crtc);
  105. mtk_crtc->event = NULL;
  106. spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
  107. }
  108. static void mtk_drm_finish_page_flip(struct mtk_drm_crtc *mtk_crtc)
  109. {
  110. drm_crtc_handle_vblank(&mtk_crtc->base);
  111. if (mtk_crtc->pending_needs_vblank) {
  112. mtk_drm_crtc_finish_page_flip(mtk_crtc);
  113. mtk_crtc->pending_needs_vblank = false;
  114. }
  115. }
  116. static void mtk_drm_crtc_destroy(struct drm_crtc *crtc)
  117. {
  118. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  119. mtk_disp_mutex_put(mtk_crtc->mutex[0]);
  120. drm_crtc_cleanup(crtc);
  121. }
  122. static void mtk_drm_crtc_reset(struct drm_crtc *crtc)
  123. {
  124. struct mtk_crtc_state *state;
  125. if (crtc->state) {
  126. __drm_atomic_helper_crtc_destroy_state(crtc->state);
  127. state = to_mtk_crtc_state(crtc->state);
  128. memset(state, 0, sizeof(*state));
  129. } else {
  130. state = kzalloc(sizeof(*state), GFP_KERNEL);
  131. if (!state)
  132. return;
  133. crtc->state = &state->base;
  134. }
  135. state->base.crtc = crtc;
  136. }
  137. static int mtk_drm_wait_blank(struct mtk_drm_crtc *mtk_crtc,
  138. bool blank, long timeout)
  139. {
  140. int ret;
  141. ret = wait_event_timeout(mtk_crtc->state_wait_queue,
  142. mtk_crtc->crtc_blank == blank, timeout);
  143. return ret;
  144. }
  145. int mtk_drm_crtc_wait_blank(struct mtk_drm_crtc *mtk_crtc)
  146. {
  147. int ret = 0;
  148. DDPMSG("%s wait TUI finish\n", __func__);
  149. while (mtk_crtc->crtc_blank == true) {
  150. // DDP_MUTEX_UNLOCK(&mtk_crtc->blank_lock, __func__, __LINE__);
  151. ret |= mtk_drm_wait_blank(mtk_crtc, false, HZ / 5);
  152. // DDP_MUTEX_LOCK(&mtk_crtc->blank_lock, __func__, __LINE__);
  153. }
  154. DDPMSG("%s TUI done state=%d\n", __func__,
  155. mtk_crtc->crtc_blank);
  156. return ret;
  157. }
  158. void mtk_drm_crtc_dump(struct drm_crtc *crtc)
  159. {
  160. int i, j;
  161. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  162. struct mtk_crtc_state *state;
  163. struct mtk_drm_private *priv = crtc->dev->dev_private;
  164. const struct mtk_addon_scenario_data *addon_data;
  165. const struct mtk_addon_path_data *addon_path;
  166. enum addon_module module;
  167. struct mtk_ddp_comp *comp;
  168. int crtc_id = drm_crtc_index(crtc);
  169. struct mtk_panel_params *panel_ext = mtk_drm_get_lcm_ext_params(crtc);
  170. if (!priv->power_state) {
  171. DDPDUMP("DRM dev is not in power on state, skip %s\n",
  172. __func__);
  173. return;
  174. }
  175. if (crtc_id < 0) {
  176. DDPPR_ERR("%s: Invalid crtc_id:%d\n", __func__, crtc_id);
  177. return;
  178. }
  179. DDPINFO("%s\n", __func__);
  180. switch (priv->data->mmsys_id) {
  181. case MMSYS_MT2701:
  182. break;
  183. case MMSYS_MT2712:
  184. break;
  185. case MMSYS_MT8173:
  186. break;
  187. case MMSYS_MT6779:
  188. break;
  189. case MMSYS_MT6885:
  190. mmsys_config_dump_reg_mt6885(mtk_crtc->config_regs);
  191. mutex_dump_reg_mt6885(mtk_crtc->mutex[0]);
  192. for_each_comp_in_cur_crtc_path(comp, mtk_crtc, i, j)
  193. mtk_dump_reg(comp);
  194. break;
  195. case MMSYS_MT6873:
  196. case MMSYS_MT6853:
  197. case MMSYS_MT6833:
  198. mmsys_config_dump_reg_mt6873(mtk_crtc->config_regs);
  199. mutex_dump_reg_mt6873(mtk_crtc->mutex[0]);
  200. break;
  201. default:
  202. pr_info("%s mtk drm not support mmsys id %d\n",
  203. __func__, priv->data->mmsys_id);
  204. break;
  205. }
  206. for_each_comp_in_cur_crtc_path(comp, mtk_crtc, i, j) mtk_dump_reg(comp);
  207. if (!crtc->state) {
  208. DDPDUMP("%s dump nothing for null state\n", __func__);
  209. return;
  210. }
  211. state = to_mtk_crtc_state(crtc->state);
  212. addon_data = mtk_addon_get_scenario_data(__func__, crtc,
  213. state->lye_state.scn[crtc_id]);
  214. if (!addon_data)
  215. return;
  216. for (i = 0; i < addon_data->module_num; i++) {
  217. module = addon_data->module_data[i].module;
  218. addon_path = mtk_addon_module_get_path(module);
  219. for (j = 0; j < addon_path->path_len; j++) {
  220. if (mtk_ddp_comp_get_type(addon_path->path[j])
  221. == MTK_DISP_VIRTUAL)
  222. continue;
  223. comp = priv->ddp_comp[addon_path->path[j]];
  224. mtk_dump_reg(comp);
  225. }
  226. }
  227. if (panel_ext &&
  228. panel_ext->output_mode == MTK_PANEL_DSC_SINGLE_PORT) {
  229. comp = priv->ddp_comp[DDP_COMPONENT_DSC0];
  230. mtk_dump_reg(comp);
  231. }
  232. }
  233. void mtk_drm_crtc_analysis(struct drm_crtc *crtc)
  234. {
  235. int i, j;
  236. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  237. struct mtk_crtc_state *state;
  238. struct mtk_drm_private *priv = crtc->dev->dev_private;
  239. const struct mtk_addon_scenario_data *addon_data;
  240. const struct mtk_addon_path_data *addon_path;
  241. enum addon_module module;
  242. struct mtk_ddp_comp *comp;
  243. int crtc_id = drm_crtc_index(crtc);
  244. #ifndef CONFIG_FPGA_EARLY_PORTING
  245. if (!priv->power_state) {
  246. DDPDUMP("DRM dev is not in power on state, skip %s\n",
  247. __func__);
  248. return;
  249. }
  250. #endif
  251. DDPFUNC("crtc%d\n", crtc_id);
  252. switch (priv->data->mmsys_id) {
  253. case MMSYS_MT2701:
  254. break;
  255. case MMSYS_MT2712:
  256. break;
  257. case MMSYS_MT8173:
  258. break;
  259. case MMSYS_MT6779:
  260. break;
  261. case MMSYS_MT6885:
  262. mmsys_config_dump_analysis_mt6885(mtk_crtc->config_regs);
  263. if (mtk_crtc->is_dual_pipe) {
  264. DDPDUMP("anlysis dual pipe\n");
  265. mtk_ddp_dual_pipe_dump(mtk_crtc);
  266. for_each_comp_in_dual_pipe(comp, mtk_crtc, i, j) {
  267. mtk_dump_analysis(comp);
  268. mtk_dump_reg(comp);
  269. }
  270. }
  271. mutex_dump_analysis_mt6885(mtk_crtc->mutex[0]);
  272. break;
  273. case MMSYS_MT6873:
  274. mmsys_config_dump_analysis_mt6873(mtk_crtc->config_regs);
  275. mutex_dump_analysis_mt6873(mtk_crtc->mutex[0]);
  276. break;
  277. case MMSYS_MT6853:
  278. mmsys_config_dump_analysis_mt6853(mtk_crtc->config_regs);
  279. mutex_dump_analysis_mt6853(mtk_crtc->mutex[0]);
  280. break;
  281. case MMSYS_MT6833:
  282. mmsys_config_dump_analysis_mt6833(mtk_crtc->config_regs);
  283. mutex_dump_analysis_mt6833(mtk_crtc->mutex[0]);
  284. break;
  285. default:
  286. pr_info("%s mtk drm not support mmsys id %d\n",
  287. __func__, priv->data->mmsys_id);
  288. break;
  289. }
  290. for_each_comp_in_cur_crtc_path(comp, mtk_crtc, i, j)
  291. mtk_dump_analysis(comp);
  292. if (!crtc->state) {
  293. DDPDUMP("%s dump nothing for null state\n", __func__);
  294. return;
  295. }
  296. state = to_mtk_crtc_state(crtc->state);
  297. if (crtc_id < 0) {
  298. DDPPR_ERR("%s: Invalid crtc_id:%d\n", __func__, crtc_id);
  299. return;
  300. }
  301. addon_data = mtk_addon_get_scenario_data(__func__, crtc,
  302. state->lye_state.scn[crtc_id]);
  303. if (!addon_data)
  304. return;
  305. for (i = 0; i < addon_data->module_num; i++) {
  306. module = addon_data->module_data[i].module;
  307. addon_path = mtk_addon_module_get_path(module);
  308. for (j = 0; j < addon_path->path_len; j++) {
  309. if (mtk_ddp_comp_get_type(addon_path->path[j])
  310. == MTK_DISP_VIRTUAL)
  311. continue;
  312. comp = priv->ddp_comp[addon_path->path[j]];
  313. mtk_dump_analysis(comp);
  314. }
  315. }
  316. }
  317. struct mtk_ddp_comp *mtk_ddp_comp_request_output(struct mtk_drm_crtc *mtk_crtc)
  318. {
  319. struct mtk_ddp_comp *comp;
  320. int i, j;
  321. for_each_comp_in_crtc_path_reverse(
  322. comp, mtk_crtc, i,
  323. j)
  324. if (mtk_ddp_comp_is_output(comp))
  325. return comp;
  326. /* This CRTC does not contain output comp */
  327. return NULL;
  328. }
  329. void mtk_crtc_change_output_mode(struct drm_crtc *crtc, int aod_en)
  330. {
  331. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  332. struct mtk_ddp_comp *comp;
  333. comp = mtk_ddp_comp_request_output(mtk_crtc);
  334. if (!comp)
  335. return;
  336. DDPINFO("%s\n", __func__);
  337. switch (comp->id) {
  338. case DDP_COMPONENT_DSI0:
  339. case DDP_COMPONENT_DSI1:
  340. mtk_ddp_comp_io_cmd(comp, NULL, DSI_CHANGE_MODE, &aod_en);
  341. break;
  342. default:
  343. break;
  344. }
  345. }
  346. bool mtk_crtc_is_connector_enable(struct mtk_drm_crtc *mtk_crtc)
  347. {
  348. struct mtk_ddp_comp *comp = mtk_ddp_comp_request_output(mtk_crtc);
  349. bool enable = 0;
  350. if (comp == NULL || mtk_ddp_comp_get_type(comp->id) != MTK_DSI)
  351. return enable;
  352. if (comp->funcs && comp->funcs->io_cmd)
  353. comp->funcs->io_cmd(comp, NULL, CONNECTOR_IS_ENABLE, &enable);
  354. return enable;
  355. }
  356. static struct drm_crtc_state *
  357. mtk_drm_crtc_duplicate_state(struct drm_crtc *crtc)
  358. {
  359. struct mtk_crtc_state *state, *old_state;
  360. state = kzalloc(sizeof(*state), GFP_KERNEL);
  361. if (!state)
  362. return NULL;
  363. if (!crtc) {
  364. DDPPR_ERR("NULL crtc\n");
  365. kfree(state);
  366. return NULL;
  367. }
  368. if (crtc->state)
  369. __drm_atomic_helper_crtc_duplicate_state(crtc, &state->base);
  370. if (state->base.crtc != crtc)
  371. DDPAEE("%s:%d, invalid crtc:(%p,%p)\n",
  372. __func__, __LINE__,
  373. state->base.crtc, crtc);
  374. state->base.crtc = crtc;
  375. if (crtc->state) {
  376. old_state = to_mtk_crtc_state(crtc->state);
  377. state->lye_state = old_state->lye_state;
  378. state->rsz_src_roi = old_state->rsz_src_roi;
  379. state->rsz_dst_roi = old_state->rsz_dst_roi;
  380. state->prop_val[CRTC_PROP_DOZE_ACTIVE] =
  381. old_state->prop_val[CRTC_PROP_DOZE_ACTIVE];
  382. }
  383. return &state->base;
  384. }
  385. static void mtk_drm_crtc_destroy_state(struct drm_crtc *crtc,
  386. struct drm_crtc_state *state)
  387. {
  388. struct mtk_crtc_state *s;
  389. s = to_mtk_crtc_state(state);
  390. __drm_atomic_helper_crtc_destroy_state(state);
  391. kfree(s);
  392. }
  393. static int mtk_drm_crtc_set_property(struct drm_crtc *crtc,
  394. struct drm_crtc_state *state,
  395. struct drm_property *property,
  396. uint64_t val)
  397. {
  398. struct drm_device *dev = crtc->dev;
  399. struct mtk_drm_private *private = dev->dev_private;
  400. struct mtk_crtc_state *crtc_state = to_mtk_crtc_state(state);
  401. int index = drm_crtc_index(crtc);
  402. int ret = 0;
  403. int i;
  404. if (index < 0)
  405. return -EINVAL;
  406. for (i = 0; i < CRTC_PROP_MAX; i++) {
  407. if (private->crtc_property[index][i] == property) {
  408. crtc_state->prop_val[i] = (unsigned int)val;
  409. DDPINFO("crtc:%d set property:%s %d\n",
  410. index, property->name,
  411. (unsigned int)val);
  412. return ret;
  413. }
  414. }
  415. DDPPR_ERR("fail to set property:%s %d\n", property->name,
  416. (unsigned int)val);
  417. return -EINVAL;
  418. }
  419. static int mtk_drm_crtc_get_property(struct drm_crtc *crtc,
  420. const struct drm_crtc_state *state,
  421. struct drm_property *property,
  422. uint64_t *val)
  423. {
  424. struct drm_device *dev = crtc->dev;
  425. struct mtk_drm_private *private = dev->dev_private;
  426. struct mtk_crtc_state *crtc_state = to_mtk_crtc_state(state);
  427. int ret = 0;
  428. int index = drm_crtc_index(crtc);
  429. int i;
  430. for (i = 0; i < CRTC_PROP_MAX; i++) {
  431. if (private->crtc_property[index][i] == property) {
  432. *val = crtc_state->prop_val[i];
  433. DDPINFO("get property:%s %lld\n", property->name, *val);
  434. return ret;
  435. }
  436. }
  437. DDPPR_ERR("fail to get property:%s %p\n", property->name, val);
  438. return -EINVAL;
  439. }
  440. struct mtk_ddp_comp *mtk_crtc_get_comp(struct drm_crtc *crtc,
  441. unsigned int path_id,
  442. unsigned int comp_idx)
  443. {
  444. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  445. struct mtk_crtc_ddp_ctx *ddp_ctx = mtk_crtc->ddp_ctx;
  446. if (mtk_crtc->ddp_mode > DDP_MINOR) {
  447. DDPPR_ERR("invalid ddp mode:%d!\n", mtk_crtc->ddp_mode);
  448. return NULL;
  449. }
  450. return ddp_ctx[mtk_crtc->ddp_mode].ddp_comp[path_id][comp_idx];
  451. }
  452. static void mtk_drm_crtc_lfr_update(struct drm_crtc *crtc,
  453. struct cmdq_pkt *cmdq_handle)
  454. {
  455. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  456. struct mtk_ddp_comp *output_comp =
  457. mtk_ddp_comp_request_output(mtk_crtc);
  458. mtk_ddp_comp_io_cmd(output_comp, cmdq_handle, DSI_LFR_UPDATE, NULL);
  459. }
  460. static bool mtk_drm_crtc_mode_fixup(struct drm_crtc *crtc,
  461. const struct drm_display_mode *mode,
  462. struct drm_display_mode *adjusted_mode)
  463. {
  464. /* Nothing to do here, but this callback is mandatory. */
  465. return true;
  466. }
  467. static void mtk_drm_crtc_mode_set_nofb(struct drm_crtc *crtc)
  468. {
  469. struct mtk_crtc_state *state = to_mtk_crtc_state(crtc->state);
  470. state->pending_width = crtc->mode.hdisplay;
  471. state->pending_height = crtc->mode.vdisplay;
  472. state->pending_vrefresh = crtc->mode.vrefresh;
  473. wmb(); /* Make sure the above parameters are set before update */
  474. state->pending_config = true;
  475. }
  476. static int mtk_crtc_enable_vblank_thread(void *data)
  477. {
  478. int ret = 0;
  479. struct drm_crtc *crtc = (struct drm_crtc *)data;
  480. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  481. while (1) {
  482. ret = wait_event_interruptible(
  483. mtk_crtc->vblank_enable_wq,
  484. atomic_read(&mtk_crtc->vblank_enable_task_active));
  485. DDP_MUTEX_LOCK(&mtk_crtc->lock, __func__, __LINE__);
  486. if (mtk_crtc->enabled)
  487. mtk_drm_idlemgr_kick(__func__, &mtk_crtc->base, 0);
  488. DDP_MUTEX_UNLOCK(&mtk_crtc->lock, __func__, __LINE__);
  489. atomic_set(&mtk_crtc->vblank_enable_task_active, 0);
  490. if (kthread_should_stop()) {
  491. DDPPR_ERR("%s stopped\n", __func__);
  492. break;
  493. }
  494. }
  495. return 0;
  496. }
  497. int mtk_drm_crtc_enable_vblank(struct drm_device *drm, unsigned int pipe)
  498. {
  499. struct mtk_drm_private *priv = drm->dev_private;
  500. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(priv->crtc[pipe]);
  501. struct mtk_ddp_comp *comp = mtk_crtc_get_comp(&mtk_crtc->base, 0, 0);
  502. mtk_crtc->vblank_en = 1;
  503. if (!mtk_crtc->enabled) {
  504. CRTC_MMP_MARK(pipe, enable_vblank, 0xFFFFFFFF,
  505. 0xFFFFFFFF);
  506. return 0;
  507. }
  508. /* We only consider CRTC0 vsync so far, need to modify to DPI, DPTX */
  509. if (mtk_drm_helper_get_opt(priv->helper_opt, MTK_DRM_OPT_IDLE_MGR) &&
  510. drm_crtc_index(&mtk_crtc->base) == 0) {
  511. /* The enable vblank is called in spinlock, so we create another
  512. * thread to kick idle mode for cmd mode vsync
  513. */
  514. atomic_set(&mtk_crtc->vblank_enable_task_active, 1);
  515. wake_up_interruptible(&mtk_crtc->vblank_enable_wq);
  516. }
  517. CRTC_MMP_MARK(pipe, enable_vblank, (unsigned long)comp,
  518. (unsigned long)&mtk_crtc->base);
  519. return 0;
  520. }
  521. static void bl_cmdq_cb(struct cmdq_cb_data data)
  522. {
  523. struct mtk_cmdq_cb_data *cb_data = data.data;
  524. struct drm_crtc *crtc = cb_data->crtc;
  525. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  526. struct cmdq_pkt_buffer *cmdq_buf = &(mtk_crtc->gce_obj.buf);
  527. int id;
  528. unsigned int bl_idx = 0;
  529. id = drm_crtc_index(crtc);
  530. if (id == 0) {
  531. bl_idx = *(unsigned int *)(cmdq_buf->va_base +
  532. DISP_SLOT_CUR_BL_IDX);
  533. CRTC_MMP_MARK(id, bl_cb, bl_idx, 0);
  534. }
  535. cmdq_pkt_destroy(cb_data->cmdq_handle);
  536. kfree(cb_data);
  537. }
  538. int mtk_drm_setbacklight(struct drm_crtc *crtc, unsigned int level)
  539. {
  540. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  541. struct cmdq_pkt *cmdq_handle;
  542. struct mtk_ddp_comp *comp = mtk_ddp_comp_request_output(mtk_crtc);
  543. struct mtk_cmdq_cb_data *cb_data;
  544. static unsigned int bl_cnt;
  545. struct cmdq_pkt_buffer *cmdq_buf;
  546. bool is_frame_mode;
  547. int index = drm_crtc_index(crtc);
  548. CRTC_MMP_EVENT_START(index, backlight, (unsigned long)crtc,
  549. level);
  550. DDP_MUTEX_LOCK(&mtk_crtc->lock, __func__, __LINE__);
  551. if (!(mtk_crtc->enabled)) {
  552. DDPINFO("Sleep State set backlight stop --crtc not ebable\n");
  553. mutex_unlock(&mtk_crtc->lock);
  554. CRTC_MMP_EVENT_END(index, backlight, 0, 0);
  555. return -EINVAL;
  556. }
  557. if (!comp) {
  558. DDPINFO("%s no output comp\n", __func__);
  559. mutex_unlock(&mtk_crtc->lock);
  560. CRTC_MMP_EVENT_END(index, backlight, 0, 1);
  561. return -EINVAL;
  562. }
  563. mtk_drm_idlemgr_kick(__func__, crtc, 0);
  564. cb_data = kmalloc(sizeof(*cb_data), GFP_KERNEL);
  565. if (!cb_data) {
  566. DDP_MUTEX_UNLOCK(&mtk_crtc->lock, __func__, __LINE__);
  567. DDPPR_ERR("cb data creation failed\n");
  568. CRTC_MMP_EVENT_END(index, backlight, 0, 2);
  569. return 0;
  570. }
  571. is_frame_mode = mtk_crtc_is_frame_trigger_mode(&mtk_crtc->base);
  572. cmdq_handle =
  573. cmdq_pkt_create(mtk_crtc->gce_obj.client[CLIENT_DSI_CFG]);
  574. if (mtk_crtc_with_sub_path(crtc, mtk_crtc->ddp_mode))
  575. mtk_crtc_wait_frame_done(mtk_crtc, cmdq_handle,
  576. DDP_SECOND_PATH, 0);
  577. else
  578. mtk_crtc_wait_frame_done(mtk_crtc, cmdq_handle,
  579. DDP_FIRST_PATH, 0);
  580. if (is_frame_mode) {
  581. cmdq_pkt_wfe(cmdq_handle,
  582. mtk_crtc->gce_obj.event[EVENT_CABC_EOF]);
  583. cmdq_pkt_clear_event(cmdq_handle,
  584. mtk_crtc->gce_obj.event[EVENT_STREAM_DIRTY]);
  585. }
  586. /* set backlight */
  587. if (comp->funcs && comp->funcs->io_cmd)
  588. comp->funcs->io_cmd(comp, cmdq_handle, DSI_SET_BL, &level);
  589. if (is_frame_mode) {
  590. cmdq_pkt_set_event(cmdq_handle,
  591. mtk_crtc->gce_obj.event[EVENT_STREAM_DIRTY]);
  592. cmdq_pkt_set_event(cmdq_handle,
  593. mtk_crtc->gce_obj.event[EVENT_CABC_EOF]);
  594. }
  595. /* add counter to check update frequency */
  596. cmdq_buf = &(mtk_crtc->gce_obj.buf);
  597. cmdq_pkt_write(cmdq_handle, mtk_crtc->gce_obj.base,
  598. cmdq_buf->pa_base +
  599. DISP_SLOT_CUR_BL_IDX,
  600. bl_cnt, ~0);
  601. CRTC_MMP_MARK(index, backlight, bl_cnt, 0);
  602. bl_cnt++;
  603. cb_data->crtc = crtc;
  604. cb_data->cmdq_handle = cmdq_handle;
  605. if (cmdq_pkt_flush_threaded(cmdq_handle, bl_cmdq_cb, cb_data) < 0)
  606. DDPPR_ERR("failed to flush bl_cmdq_cb\n");
  607. DDP_MUTEX_UNLOCK(&mtk_crtc->lock, __func__, __LINE__);
  608. CRTC_MMP_EVENT_END(index, backlight, (unsigned long)crtc,
  609. level);
  610. return 0;
  611. }
  612. int mtk_drm_setbacklight_grp(struct drm_crtc *crtc, unsigned int level)
  613. {
  614. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  615. struct cmdq_pkt *cmdq_handle;
  616. struct mtk_ddp_comp *comp = mtk_ddp_comp_request_output(mtk_crtc);
  617. bool is_frame_mode;
  618. int index = drm_crtc_index(crtc);
  619. CRTC_MMP_EVENT_START(index, backlight_grp, (unsigned long)crtc,
  620. level);
  621. DDP_MUTEX_LOCK(&mtk_crtc->lock, __func__, __LINE__);
  622. if (!(mtk_crtc->enabled)) {
  623. DDPINFO("%s:%d, crtc is slept\n", __func__,
  624. __LINE__);
  625. DDP_MUTEX_UNLOCK(&mtk_crtc->lock, __func__, __LINE__);
  626. CRTC_MMP_EVENT_END(index, backlight_grp, 0, 0);
  627. return -EINVAL;
  628. }
  629. mtk_drm_idlemgr_kick(__func__, crtc, 0);
  630. is_frame_mode = mtk_crtc_is_frame_trigger_mode(crtc);
  631. cmdq_handle = cmdq_pkt_create(mtk_crtc->gce_obj.client[CLIENT_CFG]);
  632. if (is_frame_mode) {
  633. cmdq_pkt_wfe(cmdq_handle,
  634. mtk_crtc->gce_obj.event[EVENT_CABC_EOF]);
  635. cmdq_pkt_clear_event(cmdq_handle,
  636. mtk_crtc->gce_obj.event[EVENT_STREAM_DIRTY]);
  637. }
  638. if (mtk_crtc_with_sub_path(crtc, mtk_crtc->ddp_mode))
  639. mtk_crtc_wait_frame_done(mtk_crtc, cmdq_handle,
  640. DDP_SECOND_PATH, 0);
  641. else
  642. mtk_crtc_wait_frame_done(mtk_crtc, cmdq_handle,
  643. DDP_FIRST_PATH, 0);
  644. if (comp && comp->funcs && comp->funcs->io_cmd)
  645. comp->funcs->io_cmd(comp, cmdq_handle, DSI_SET_BL_GRP, &level);
  646. if (is_frame_mode) {
  647. cmdq_pkt_set_event(cmdq_handle,
  648. mtk_crtc->gce_obj.event[EVENT_STREAM_DIRTY]);
  649. cmdq_pkt_set_event(cmdq_handle,
  650. mtk_crtc->gce_obj.event[EVENT_CABC_EOF]);
  651. }
  652. cmdq_pkt_flush(cmdq_handle);
  653. cmdq_pkt_destroy(cmdq_handle);
  654. DDP_MUTEX_UNLOCK(&mtk_crtc->lock, __func__, __LINE__);
  655. CRTC_MMP_EVENT_END(index, backlight_grp, (unsigned long)crtc,
  656. level);
  657. return 0;
  658. }
  659. static void mtk_drm_crtc_wk_lock(struct drm_crtc *crtc, bool get,
  660. const char *func, int line);
  661. int mtk_drm_aod_setbacklight(struct drm_crtc *crtc, unsigned int level)
  662. {
  663. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  664. struct mtk_ddp_comp *output_comp, *comp;
  665. struct cmdq_pkt *cmdq_handle;
  666. bool is_frame_mode;
  667. struct cmdq_client *client;
  668. int i, j;
  669. struct mtk_crtc_state *crtc_state;
  670. DDP_MUTEX_LOCK(&mtk_crtc->lock, __func__, __LINE__);
  671. crtc_state = to_mtk_crtc_state(crtc->state);
  672. if (mtk_crtc->enabled && !crtc_state->prop_val[CRTC_PROP_DOZE_ACTIVE]) {
  673. DDPINFO("%s:%d, crtc is on and not in doze mode\n",
  674. __func__, __LINE__);
  675. DDP_MUTEX_UNLOCK(&mtk_crtc->lock, __func__, __LINE__);
  676. return -EINVAL;
  677. }
  678. CRTC_MMP_EVENT_START(0, backlight, 0x123,
  679. level);
  680. mtk_drm_crtc_wk_lock(crtc, 1, __func__, __LINE__);
  681. output_comp = mtk_ddp_comp_request_output(mtk_crtc);
  682. if (unlikely(!output_comp)) {
  683. mtk_drm_crtc_wk_lock(crtc, 0, __func__, __LINE__);
  684. DDP_MUTEX_UNLOCK(&mtk_crtc->lock, __func__, __LINE__);
  685. return -ENODEV;
  686. }
  687. client = mtk_crtc->gce_obj.client[CLIENT_CFG];
  688. if (!mtk_crtc->enabled) {
  689. /* 1. power on mtcmos */
  690. mtk_drm_top_clk_prepare_enable(crtc->dev);
  691. if (mtk_crtc_with_trigger_loop(crtc))
  692. mtk_crtc_start_trig_loop(crtc);
  693. mtk_ddp_comp_io_cmd(output_comp, NULL, CONNECTOR_ENABLE, NULL);
  694. for_each_comp_in_cur_crtc_path(comp, mtk_crtc, i, j)
  695. mtk_dump_analysis(comp);
  696. }
  697. /* send LCM CMD */
  698. is_frame_mode = mtk_crtc_is_frame_trigger_mode(&mtk_crtc->base);
  699. if (is_frame_mode)
  700. cmdq_handle =
  701. cmdq_pkt_create(mtk_crtc->gce_obj.client[CLIENT_CFG]);
  702. else
  703. cmdq_handle =
  704. cmdq_pkt_create(
  705. mtk_crtc->gce_obj.client[CLIENT_DSI_CFG]);
  706. if (is_frame_mode) {
  707. cmdq_pkt_wfe(cmdq_handle,
  708. mtk_crtc->gce_obj.event[EVENT_CABC_EOF]);
  709. cmdq_pkt_clear_event(cmdq_handle,
  710. mtk_crtc->gce_obj.event[EVENT_STREAM_DIRTY]);
  711. }
  712. if (mtk_crtc_with_sub_path(crtc, mtk_crtc->ddp_mode))
  713. mtk_crtc_wait_frame_done(mtk_crtc, cmdq_handle,
  714. DDP_SECOND_PATH, 0);
  715. else
  716. mtk_crtc_wait_frame_done(mtk_crtc, cmdq_handle,
  717. DDP_FIRST_PATH, 0);
  718. /* set backlight */
  719. if (output_comp->funcs && output_comp->funcs->io_cmd)
  720. output_comp->funcs->io_cmd(output_comp,
  721. cmdq_handle, DSI_SET_BL_AOD, &level);
  722. if (is_frame_mode) {
  723. cmdq_pkt_set_event(cmdq_handle,
  724. mtk_crtc->gce_obj.event[EVENT_STREAM_DIRTY]);
  725. cmdq_pkt_set_event(cmdq_handle,
  726. mtk_crtc->gce_obj.event[EVENT_CABC_EOF]);
  727. }
  728. cmdq_pkt_flush(cmdq_handle);
  729. cmdq_pkt_destroy(cmdq_handle);
  730. if (!mtk_crtc->enabled) {
  731. if (mtk_crtc_with_trigger_loop(crtc))
  732. mtk_crtc_stop_trig_loop(crtc);
  733. mtk_ddp_comp_io_cmd(output_comp, NULL, CONNECTOR_DISABLE, NULL);
  734. mtk_drm_top_clk_disable_unprepare(crtc->dev);
  735. }
  736. mtk_drm_crtc_wk_lock(crtc, 0, __func__, __LINE__);
  737. CRTC_MMP_EVENT_END(0, backlight, 0x123,
  738. level);
  739. DDP_MUTEX_UNLOCK(&mtk_crtc->lock, __func__, __LINE__);
  740. return 0;
  741. }
  742. static int mtk_drm_crtc_set_panel_hbm(struct drm_crtc *crtc, bool en)
  743. {
  744. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  745. struct mtk_ddp_comp *comp = mtk_ddp_comp_request_output(mtk_crtc);
  746. struct cmdq_pkt *cmdq_handle;
  747. bool is_frame_mode;
  748. bool state = false;
  749. if (!(comp && comp->funcs && comp->funcs->io_cmd))
  750. return -EINVAL;
  751. comp->funcs->io_cmd(comp, NULL, DSI_HBM_GET_STATE, &state);
  752. if (state == en)
  753. return 0;
  754. if (!(mtk_crtc->enabled)) {
  755. DDPINFO("%s: skip, slept\n", __func__);
  756. return -EINVAL;
  757. }
  758. mtk_drm_idlemgr_kick(__func__, crtc, 0);
  759. DDPINFO("%s:set LCM hbm en:%d\n", __func__, en);
  760. is_frame_mode = mtk_crtc_is_frame_trigger_mode(&mtk_crtc->base);
  761. cmdq_handle =
  762. cmdq_pkt_create(mtk_crtc->gce_obj.client[CLIENT_DSI_CFG]);
  763. if (is_frame_mode) {
  764. cmdq_pkt_wfe(cmdq_handle,
  765. mtk_crtc->gce_obj.event[EVENT_CABC_EOF]);
  766. cmdq_pkt_clear_event(cmdq_handle,
  767. mtk_crtc->gce_obj.event[EVENT_STREAM_DIRTY]);
  768. }
  769. mtk_crtc_wait_frame_done(mtk_crtc, cmdq_handle, DDP_FIRST_PATH, 0);
  770. comp->funcs->io_cmd(comp, cmdq_handle, DSI_HBM_SET, &en);
  771. if (is_frame_mode)
  772. cmdq_pkt_set_event(cmdq_handle,
  773. mtk_crtc->gce_obj.event[EVENT_CABC_EOF]);
  774. cmdq_pkt_flush(cmdq_handle);
  775. cmdq_pkt_destroy(cmdq_handle);
  776. return 0;
  777. }
  778. static int mtk_drm_crtc_hbm_wait(struct drm_crtc *crtc, bool en)
  779. {
  780. struct mtk_panel_params *panel_ext = mtk_drm_get_lcm_ext_params(crtc);
  781. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  782. struct mtk_ddp_comp *comp = mtk_ddp_comp_request_output(mtk_crtc);
  783. bool wait = false;
  784. unsigned int wait_count = 0;
  785. if (!(comp && comp->funcs && comp->funcs->io_cmd))
  786. return -EINVAL;
  787. comp->funcs->io_cmd(comp, NULL, DSI_HBM_GET_WAIT_STATE, &wait);
  788. if (wait != true)
  789. return 0;
  790. if (!panel_ext)
  791. return -EINVAL;
  792. wait_count = en ? panel_ext->hbm_en_time : panel_ext->hbm_dis_time;
  793. DDPINFO("LCM hbm %s wait %u-TE\n", en ? "enable" : "disable",
  794. wait_count);
  795. while (wait_count) {
  796. mtk_drm_idlemgr_kick(__func__, crtc, 0);
  797. wait_count--;
  798. comp->funcs->io_cmd(comp, NULL, DSI_HBM_WAIT, NULL);
  799. }
  800. wait = false;
  801. comp->funcs->io_cmd(comp, NULL, DSI_HBM_SET_WAIT_STATE, &wait);
  802. return 0;
  803. }
  804. void mtk_drm_crtc_disable_vblank(struct drm_device *drm, unsigned int pipe)
  805. {
  806. struct mtk_drm_private *priv = drm->dev_private;
  807. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(priv->crtc[pipe]);
  808. struct mtk_ddp_comp *comp = mtk_crtc_get_comp(&mtk_crtc->base, 0, 0);
  809. DDPINFO("%s\n", __func__);
  810. mtk_crtc->vblank_en = 0;
  811. CRTC_MMP_MARK(pipe, disable_vblank, (unsigned long)comp,
  812. (unsigned long)&mtk_crtc->base);
  813. }
  814. bool mtk_crtc_get_vblank_timestamp(struct drm_device *dev, unsigned int pipe,
  815. int *max_error,
  816. struct timeval *vblank_time,
  817. bool in_vblank_irq)
  818. {
  819. struct mtk_drm_private *priv = dev->dev_private;
  820. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(priv->crtc[pipe]);
  821. *vblank_time = mtk_crtc->vblank_time;
  822. return true;
  823. }
  824. /*dp 4k resolution 3840*2160*/
  825. bool mtk_crtc_is_dual_pipe(struct drm_crtc *crtc)
  826. {
  827. if ((drm_crtc_index(crtc) == 1) &&
  828. (crtc->state->adjusted_mode.hdisplay == 1920*2)) {
  829. DDPFUNC();
  830. return true;
  831. } else
  832. return false;
  833. }
  834. void mtk_crtc_prepare_dual_pipe(struct mtk_drm_crtc *mtk_crtc)
  835. {
  836. int i, j;
  837. enum mtk_ddp_comp_id comp_id;
  838. struct mtk_ddp_comp *comp;
  839. struct device *dev = mtk_crtc->base.dev->dev;
  840. struct mtk_drm_private *priv = mtk_crtc->base.dev->dev_private;
  841. struct drm_crtc *crtc = &mtk_crtc->base;
  842. if (mtk_crtc_is_dual_pipe(&(mtk_crtc->base))) {
  843. mtk_crtc->is_dual_pipe = true;
  844. #ifdef MTK_FB_MMDVFS_SUPPORT
  845. pm_qos_update_request(&mm_freq_request, freq_steps[1]);
  846. DDPFUNC("current freq: %d\n",
  847. pm_qos_request(PM_QOS_DISP_FREQ));
  848. #endif
  849. } else {
  850. mtk_crtc->is_dual_pipe = false;
  851. #ifdef MTK_FB_MMDVFS_SUPPORT
  852. if (drm_crtc_index(&mtk_crtc->base) == 1)
  853. pm_qos_update_request(&mm_freq_request, freq_steps[3]);
  854. DDPFUNC("crtc%d single pipe current freq: %d\n",
  855. drm_crtc_index(&mtk_crtc->base),
  856. pm_qos_request(PM_QOS_DISP_FREQ));
  857. #endif
  858. return;
  859. }
  860. for (j = 0; j < DDP_SECOND_PATH; j++) {
  861. mtk_crtc->dual_pipe_ddp_ctx.ddp_comp_nr[j] =
  862. mtk_crtc->path_data->dual_path_len[j];
  863. mtk_crtc->dual_pipe_ddp_ctx.ddp_comp[j] = devm_kmalloc_array(
  864. dev, mtk_crtc->path_data->dual_path_len[j],
  865. sizeof(struct mtk_ddp_comp *), GFP_KERNEL);
  866. DDPFUNC("j:%d,com_nr:%d,path_len:%d\n",
  867. j, mtk_crtc->dual_pipe_ddp_ctx.ddp_comp_nr[j],
  868. mtk_crtc->path_data->dual_path_len[j]);
  869. }
  870. for_each_comp_id_in_dual_pipe(comp_id, mtk_crtc->path_data, i, j) {
  871. DDPFUNC("comp id %d\n", comp_id);
  872. comp = priv->ddp_comp[comp_id];
  873. mtk_crtc->dual_pipe_ddp_ctx.ddp_comp[i][j] = comp;
  874. comp->mtk_crtc = mtk_crtc;
  875. }
  876. /*4k 30 use DISP_MERGE1, 4k 60 use DSC*/
  877. if (crtc->state->adjusted_mode.vrefresh == 30) {
  878. comp = priv->ddp_comp[DDP_COMPONENT_MERGE1];
  879. mtk_crtc->dual_pipe_ddp_ctx.ddp_comp[0][2] = comp;
  880. comp->mtk_crtc = mtk_crtc;
  881. }
  882. }
  883. static void user_cmd_cmdq_cb(struct cmdq_cb_data data)
  884. {
  885. struct mtk_cmdq_cb_data *cb_data = data.data;
  886. struct drm_crtc *crtc = cb_data->crtc;
  887. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  888. struct cmdq_pkt_buffer *cmdq_buf = &(mtk_crtc->gce_obj.buf);
  889. int id;
  890. unsigned int user_cmd_idx = 0;
  891. id = drm_crtc_index(crtc);
  892. if (id == 0) {
  893. user_cmd_idx = *(unsigned int *)(cmdq_buf->va_base +
  894. DISP_SLOT_CUR_USER_CMD_IDX);
  895. CRTC_MMP_MARK(id, user_cmd_cb, user_cmd_idx, 0);
  896. }
  897. cmdq_pkt_destroy(cb_data->cmdq_handle);
  898. kfree(cb_data);
  899. }
  900. int mtk_crtc_user_cmd(struct drm_crtc *crtc, struct mtk_ddp_comp *comp,
  901. unsigned int cmd, void *params)
  902. {
  903. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  904. struct cmdq_pkt *cmdq_handle;
  905. struct mtk_cmdq_cb_data *cb_data;
  906. static unsigned int user_cmd_cnt;
  907. struct cmdq_pkt_buffer *cmdq_buf;
  908. int index = 0;
  909. if (!mtk_crtc) {
  910. DDPPR_ERR("%s:%d, invalid crtc:0x%p\n",
  911. __func__, __LINE__, crtc);
  912. return -1;
  913. }
  914. DDP_MUTEX_LOCK(&mtk_crtc->lock, __func__, __LINE__);
  915. CRTC_MMP_EVENT_START(index, user_cmd, (unsigned long)crtc,
  916. (unsigned long)comp);
  917. if ((!crtc) || (!comp)) {
  918. DDPPR_ERR("%s:%d, invalid arg:(0x%p,0x%p)\n",
  919. __func__, __LINE__,
  920. crtc, comp);
  921. CRTC_MMP_MARK(index, user_cmd, 0, 0);
  922. goto err;
  923. }
  924. index = drm_crtc_index(crtc);
  925. if (index) {
  926. DDPPR_ERR("%s:%d, invalid crtc:0x%p, index:%d\n",
  927. __func__, __LINE__, crtc, index);
  928. CRTC_MMP_MARK(index, user_cmd, 0, 1);
  929. goto err;
  930. }
  931. if (!(mtk_crtc->enabled)) {
  932. DDPINFO("%s:%d, slepted\n", __func__, __LINE__);
  933. CRTC_MMP_EVENT_END(index, user_cmd, 0, 2);
  934. DDP_MUTEX_UNLOCK(&mtk_crtc->lock, __func__, __LINE__);
  935. return 0;
  936. }
  937. mtk_drm_idlemgr_kick(__func__, crtc, 0);
  938. cb_data = kmalloc(sizeof(*cb_data), GFP_KERNEL);
  939. if (!cb_data) {
  940. DDPPR_ERR("cb data creation failed\n");
  941. CRTC_MMP_MARK(index, user_cmd, 0, 3);
  942. goto err;
  943. }
  944. CRTC_MMP_MARK(index, user_cmd, user_cmd_cnt, 0);
  945. cmdq_handle = cmdq_pkt_create(mtk_crtc->gce_obj.client[CLIENT_CFG]);
  946. CRTC_MMP_MARK(index, user_cmd, user_cmd_cnt, 1);
  947. if (mtk_crtc_with_sub_path(crtc, mtk_crtc->ddp_mode))
  948. mtk_crtc_wait_frame_done(mtk_crtc, cmdq_handle,
  949. DDP_SECOND_PATH, 0);
  950. else
  951. mtk_crtc_wait_frame_done(mtk_crtc, cmdq_handle,
  952. DDP_FIRST_PATH, 0);
  953. CRTC_MMP_MARK(index, user_cmd, user_cmd_cnt, 2);
  954. /* set user command */
  955. if (comp && comp->funcs && comp->funcs->user_cmd && !comp->blank_mode)
  956. comp->funcs->user_cmd(comp, cmdq_handle, cmd, (void *)params);
  957. else {
  958. DDPPR_ERR("%s:%d, invalid comp:(0x%p,0x%p)\n",
  959. __func__, __LINE__, comp, comp->funcs);
  960. CRTC_MMP_MARK(index, user_cmd, 0, 4);
  961. goto err2;
  962. }
  963. CRTC_MMP_MARK(index, user_cmd, user_cmd_cnt, 3);
  964. /* add counter to check update frequency */
  965. cmdq_buf = &(mtk_crtc->gce_obj.buf);
  966. cmdq_pkt_write(cmdq_handle, mtk_crtc->gce_obj.base,
  967. cmdq_buf->pa_base +
  968. DISP_SLOT_CUR_USER_CMD_IDX,
  969. user_cmd_cnt, ~0);
  970. CRTC_MMP_MARK(index, user_cmd, user_cmd_cnt, 4);
  971. user_cmd_cnt++;
  972. cb_data->crtc = crtc;
  973. cb_data->cmdq_handle = cmdq_handle;
  974. if (cmdq_pkt_flush_threaded(cmdq_handle, user_cmd_cmdq_cb, cb_data) < 0)
  975. DDPPR_ERR("failed to flush user_cmd\n");
  976. CRTC_MMP_EVENT_END(index, user_cmd, (unsigned long)cmd,
  977. (unsigned long)params);
  978. DDP_MUTEX_UNLOCK(&mtk_crtc->lock, __func__, __LINE__);
  979. return 0;
  980. err:
  981. CRTC_MMP_EVENT_END(index, user_cmd, 0, 0);
  982. DDP_MUTEX_UNLOCK(&mtk_crtc->lock, __func__, __LINE__);
  983. return -1;
  984. err2:
  985. kfree(cb_data);
  986. CRTC_MMP_EVENT_END(index, user_cmd, 0, 0);
  987. DDP_MUTEX_UNLOCK(&mtk_crtc->lock, __func__, __LINE__);
  988. return -1;
  989. }
  990. /* power on all modules on this CRTC */
  991. void mtk_crtc_ddp_prepare(struct mtk_drm_crtc *mtk_crtc)
  992. {
  993. int i, j, k, ddp_mode;
  994. struct mtk_ddp_comp *comp;
  995. struct mtk_drm_private *priv = mtk_crtc->base.dev->dev_private;
  996. const struct mtk_addon_scenario_data *addon_data;
  997. const struct mtk_addon_path_data *addon_path;
  998. enum addon_module module;
  999. struct drm_crtc *crtc = &mtk_crtc->base;
  1000. struct mtk_panel_params *panel_ext =
  1001. mtk_drm_get_lcm_ext_params(crtc);
  1002. for_each_comp_in_all_crtc_mode(comp, mtk_crtc, i, j, ddp_mode)
  1003. mtk_ddp_comp_prepare(comp);
  1004. for (i = 0; i < ADDON_SCN_NR; i++) {
  1005. addon_data = mtk_addon_get_scenario_data(__func__,
  1006. &mtk_crtc->base, i);
  1007. if (!addon_data)
  1008. break;
  1009. for (j = 0; j < addon_data->module_num; j++) {
  1010. module = addon_data->module_data[j].module;
  1011. addon_path = mtk_addon_module_get_path(module);
  1012. for (k = 0; k < addon_path->path_len; k++) {
  1013. comp = priv->ddp_comp[addon_path->path[k]];
  1014. mtk_ddp_comp_prepare(comp);
  1015. }
  1016. }
  1017. }
  1018. if (panel_ext &&
  1019. panel_ext->output_mode == MTK_PANEL_DSC_SINGLE_PORT) {
  1020. comp = priv->ddp_comp[DDP_COMPONENT_DSC0];
  1021. mtk_ddp_comp_clk_prepare(comp);
  1022. }
  1023. if (mtk_crtc->is_dual_pipe) {
  1024. for_each_comp_in_dual_pipe(comp, mtk_crtc, i, j)
  1025. mtk_ddp_comp_clk_prepare(comp);
  1026. }
  1027. /*TODO , Move to prop place rdma4/5 VDE from DP_VDE*/
  1028. if (drm_crtc_index(crtc) == 1)
  1029. writel_relaxed(0x220000, mtk_crtc->config_regs + 0xE10);
  1030. }
  1031. void mtk_crtc_ddp_unprepare(struct mtk_drm_crtc *mtk_crtc)
  1032. {
  1033. int i, j, k, ddp_mode;
  1034. struct mtk_ddp_comp *comp;
  1035. struct mtk_drm_private *priv = mtk_crtc->base.dev->dev_private;
  1036. const struct mtk_addon_scenario_data *addon_data;
  1037. const struct mtk_addon_path_data *addon_path;
  1038. enum addon_module module;
  1039. struct drm_crtc *crtc = &mtk_crtc->base;
  1040. struct mtk_panel_params *panel_ext =
  1041. mtk_drm_get_lcm_ext_params(crtc);
  1042. for_each_comp_in_all_crtc_mode(comp, mtk_crtc, i, j, ddp_mode)
  1043. mtk_ddp_comp_unprepare(comp);
  1044. for (i = 0; i < ADDON_SCN_NR; i++) {
  1045. addon_data = mtk_addon_get_scenario_data(__func__,
  1046. &mtk_crtc->base, i);
  1047. if (!addon_data)
  1048. break;
  1049. for (j = 0; j < addon_data->module_num; j++) {
  1050. module = addon_data->module_data[j].module;
  1051. addon_path = mtk_addon_module_get_path(module);
  1052. for (k = 0; k < addon_path->path_len; k++) {
  1053. comp = priv->ddp_comp[addon_path->path[k]];
  1054. mtk_ddp_comp_unprepare(comp);
  1055. }
  1056. }
  1057. }
  1058. if (panel_ext && panel_ext->output_mode == MTK_PANEL_DSC_SINGLE_PORT) {
  1059. comp = priv->ddp_comp[DDP_COMPONENT_DSC0];
  1060. mtk_ddp_comp_clk_unprepare(comp);
  1061. }
  1062. if (mtk_crtc->is_dual_pipe) {
  1063. for_each_comp_in_dual_pipe(comp, mtk_crtc, i, j)
  1064. mtk_ddp_comp_clk_unprepare(comp);
  1065. }
  1066. /*restore default mm freq, PM_QOS_MM_FREQ_DEFAULT_VALUE*/
  1067. if (drm_crtc_index(&mtk_crtc->base) == 1)
  1068. pm_qos_update_request(&mm_freq_request, 0);
  1069. }
  1070. #ifdef MTK_DRM_ADVANCE
  1071. static struct mtk_ddp_comp *
  1072. mtk_crtc_get_plane_comp(struct drm_crtc *crtc,
  1073. struct mtk_plane_state *plane_state)
  1074. {
  1075. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  1076. struct mtk_ddp_comp *comp = NULL;
  1077. int i, j;
  1078. if (plane_state->comp_state.comp_id == 0)
  1079. return mtk_crtc_get_comp(crtc, 0, 0);
  1080. for_each_comp_in_cur_crtc_path(comp, mtk_crtc, i,
  1081. j)
  1082. if (comp->id == plane_state->comp_state.comp_id) {
  1083. DDPINFO("%s i:%d, ovl_comp_id:%d\n",
  1084. __func__, i,
  1085. plane_state->comp_state.comp_id);
  1086. DDPINFO("lye_id:%d, ext_lye_id:%d\n",
  1087. plane_state->comp_state.lye_id,
  1088. plane_state->comp_state.ext_lye_id);
  1089. return comp;
  1090. }
  1091. return comp;
  1092. }
  1093. static int mtk_crtc_get_dc_fb_size(struct drm_crtc *crtc)
  1094. {
  1095. /* DC buffer color format is RGB888 */
  1096. return crtc->state->adjusted_mode.vdisplay *
  1097. crtc->state->adjusted_mode.hdisplay * 3;
  1098. }
  1099. static void _mtk_crtc_atmoic_addon_module_disconnect(
  1100. struct drm_crtc *crtc, unsigned int ddp_mode,
  1101. struct mtk_lye_ddp_state *lye_state, struct cmdq_pkt *cmdq_handle)
  1102. {
  1103. int i;
  1104. const struct mtk_addon_scenario_data *addon_data;
  1105. const struct mtk_addon_module_data *addon_module;
  1106. union mtk_addon_config addon_config;
  1107. addon_data = mtk_addon_get_scenario_data(__func__, crtc,
  1108. lye_state->scn[drm_crtc_index(crtc)]);
  1109. if (!addon_data)
  1110. return;
  1111. for (i = 0; i < addon_data->module_num; i++) {
  1112. addon_module = &addon_data->module_data[i];
  1113. addon_config.config_type.module = addon_module->module;
  1114. addon_config.config_type.type = addon_module->type;
  1115. if (addon_module->type == ADDON_BETWEEN &&
  1116. (addon_module->module == DISP_RSZ ||
  1117. addon_module->module == DISP_RSZ_v2)) {
  1118. int w = crtc->state->adjusted_mode.hdisplay;
  1119. int h = crtc->state->adjusted_mode.vdisplay;
  1120. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  1121. struct mtk_ddp_comp *output_comp;
  1122. struct mtk_rect rsz_roi = {0, 0, w, h};
  1123. output_comp = mtk_ddp_comp_request_output(mtk_crtc);
  1124. if (output_comp &&
  1125. drm_crtc_index(crtc) == 0) {
  1126. rsz_roi.width = mtk_ddp_comp_io_cmd(
  1127. output_comp, NULL,
  1128. DSI_GET_VIRTUAL_WIDTH, NULL);
  1129. rsz_roi.height = mtk_ddp_comp_io_cmd(
  1130. output_comp, NULL,
  1131. DSI_GET_VIRTUAL_HEIGH, NULL);
  1132. }
  1133. addon_config.addon_rsz_config.rsz_src_roi = rsz_roi;
  1134. addon_config.addon_rsz_config.rsz_dst_roi = rsz_roi;
  1135. addon_config.addon_rsz_config.lc_tgt_layer =
  1136. lye_state->lc_tgt_layer;
  1137. mtk_addon_disconnect_between(
  1138. crtc, ddp_mode, addon_module, &addon_config,
  1139. cmdq_handle);
  1140. } else
  1141. DDPPR_ERR("addon type:%d + module:%d not support\n",
  1142. addon_module->type, addon_module->module);
  1143. }
  1144. }
  1145. static void
  1146. _mtk_crtc_atmoic_addon_module_connect(
  1147. struct drm_crtc *crtc,
  1148. unsigned int ddp_mode,
  1149. struct mtk_lye_ddp_state *lye_state,
  1150. struct cmdq_pkt *cmdq_handle)
  1151. {
  1152. int i;
  1153. const struct mtk_addon_scenario_data *addon_data;
  1154. const struct mtk_addon_module_data *addon_module;
  1155. union mtk_addon_config addon_config;
  1156. addon_data = mtk_addon_get_scenario_data(__func__, crtc,
  1157. lye_state->scn[drm_crtc_index(crtc)]);
  1158. if (!addon_data)
  1159. return;
  1160. for (i = 0; i < addon_data->module_num; i++) {
  1161. addon_module = &addon_data->module_data[i];
  1162. addon_config.config_type.module = addon_module->module;
  1163. addon_config.config_type.type = addon_module->type;
  1164. if (addon_module->type == ADDON_BETWEEN &&
  1165. (addon_module->module == DISP_RSZ ||
  1166. addon_module->module == DISP_RSZ_v2)) {
  1167. struct mtk_crtc_state *state =
  1168. to_mtk_crtc_state(crtc->state);
  1169. addon_config.addon_rsz_config.rsz_src_roi =
  1170. state->rsz_src_roi;
  1171. addon_config.addon_rsz_config.rsz_dst_roi =
  1172. state->rsz_dst_roi;
  1173. addon_config.addon_rsz_config.lc_tgt_layer =
  1174. lye_state->lc_tgt_layer;
  1175. mtk_addon_connect_between(crtc, ddp_mode, addon_module,
  1176. &addon_config, cmdq_handle);
  1177. } else
  1178. DDPPR_ERR("addon type:%d + module:%d not support\n",
  1179. addon_module->type, addon_module->module);
  1180. }
  1181. }
  1182. static void mtk_crtc_atmoic_ddp_config(struct drm_crtc *crtc,
  1183. struct mtk_drm_lyeblob_ids *lyeblob_ids,
  1184. struct cmdq_pkt *cmdq_handle)
  1185. {
  1186. struct mtk_lye_ddp_state *lye_state, *old_lye_state;
  1187. struct drm_property_blob *blob;
  1188. struct drm_device *dev = crtc->dev;
  1189. struct mtk_crtc_state *state = to_mtk_crtc_state(crtc->state);
  1190. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  1191. struct mtk_drm_private *priv = crtc->dev->dev_private;
  1192. if (lyeblob_ids->ddp_blob_id) {
  1193. blob = drm_property_lookup_blob(dev, lyeblob_ids->ddp_blob_id);
  1194. lye_state = (struct mtk_lye_ddp_state *)blob->data;
  1195. drm_property_unreference_blob(blob);
  1196. old_lye_state = &state->lye_state;
  1197. _mtk_crtc_atmoic_addon_module_disconnect(crtc,
  1198. mtk_crtc->ddp_mode,
  1199. old_lye_state,
  1200. cmdq_handle);
  1201. /* When open VDS path switch feature, Don't need RSZ */
  1202. if (!(mtk_drm_helper_get_opt(priv->helper_opt,
  1203. MTK_DRM_OPT_VDS_PATH_SWITCH) &&
  1204. priv->need_vds_path_switch))
  1205. _mtk_crtc_atmoic_addon_module_connect(crtc,
  1206. mtk_crtc->ddp_mode,
  1207. lye_state,
  1208. cmdq_handle);
  1209. state->lye_state = *lye_state;
  1210. }
  1211. }
  1212. static void mtk_crtc_free_ddpblob_ids(struct drm_crtc *crtc,
  1213. struct mtk_drm_lyeblob_ids *lyeblob_ids)
  1214. {
  1215. struct drm_device *dev = crtc->dev;
  1216. struct drm_property_blob *blob;
  1217. if (lyeblob_ids->ddp_blob_id) {
  1218. blob = drm_property_lookup_blob(dev, lyeblob_ids->ddp_blob_id);
  1219. drm_property_unreference_blob(blob);
  1220. drm_property_unreference_blob(blob);
  1221. list_del(&lyeblob_ids->list);
  1222. kfree(lyeblob_ids);
  1223. }
  1224. }
  1225. static void mtk_crtc_free_lyeblob_ids(struct drm_crtc *crtc,
  1226. struct mtk_drm_lyeblob_ids *lyeblob_ids)
  1227. {
  1228. struct drm_device *dev = crtc->dev;
  1229. struct drm_property_blob *blob;
  1230. int32_t blob_id;
  1231. int i, j;
  1232. for (j = 0; j < MAX_CRTC; j++) {
  1233. if (!((lyeblob_ids->ref_cnt_mask >> j) & 0x1))
  1234. continue;
  1235. for (i = 0; i < OVL_LAYER_NR; i++) {
  1236. blob_id = lyeblob_ids->lye_plane_blob_id[j][i];
  1237. if (blob_id > 0) {
  1238. blob = drm_property_lookup_blob(dev, blob_id);
  1239. drm_property_unreference_blob(blob);
  1240. drm_property_unreference_blob(blob);
  1241. }
  1242. }
  1243. }
  1244. }
  1245. static void mtk_crtc_get_plane_comp_state(struct drm_crtc *crtc,
  1246. struct cmdq_pkt *cmdq_handle)
  1247. {
  1248. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  1249. struct mtk_plane_comp_state *comp_state;
  1250. int i, j, k;
  1251. for (i = 0; i < mtk_crtc->layer_nr; i++) {
  1252. struct drm_plane *plane = &mtk_crtc->planes[i].base;
  1253. struct mtk_plane_state *plane_state;
  1254. struct mtk_ddp_comp *comp = NULL;
  1255. plane_state = to_mtk_plane_state(plane->state);
  1256. comp_state = &(plane_state->comp_state);
  1257. if (plane_state->base.visible) {
  1258. for_each_comp_in_cur_crtc_path(
  1259. comp, mtk_crtc, j,
  1260. k) {
  1261. if (comp->id != comp_state->comp_id)
  1262. continue;
  1263. mtk_ddp_comp_layer_off(
  1264. comp,
  1265. comp_state->lye_id,
  1266. comp_state->ext_lye_id,
  1267. cmdq_handle);
  1268. if (mtk_crtc->is_dual_pipe) {
  1269. struct mtk_crtc_ddp_ctx *ddp_ctx;
  1270. int index = DDP_FIRST_PATH;
  1271. ddp_ctx = &mtk_crtc->dual_pipe_ddp_ctx;
  1272. comp = ddp_ctx->ddp_comp[index][0];
  1273. mtk_ddp_comp_layer_off(
  1274. comp,
  1275. comp_state->lye_id,
  1276. comp_state->ext_lye_id,
  1277. cmdq_handle);
  1278. }
  1279. break;
  1280. }
  1281. }
  1282. /* Set the crtc to plane state for releasing fence purpose.*/
  1283. plane_state->crtc = crtc;
  1284. mtk_plane_get_comp_state(plane, &plane_state->comp_state, crtc,
  1285. 0);
  1286. }
  1287. }
  1288. unsigned int mtk_drm_primary_frame_bw(struct drm_crtc *i_crtc)
  1289. {
  1290. unsigned long long bw = 0;
  1291. struct drm_crtc *crtc = i_crtc;
  1292. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  1293. struct mtk_ddp_comp *output_comp;
  1294. if (drm_crtc_index(i_crtc) != 0) {
  1295. DDPPR_ERR("%s no support CRTC%u", __func__,
  1296. drm_crtc_index(i_crtc));
  1297. drm_for_each_crtc(crtc, i_crtc->dev) {
  1298. if (drm_crtc_index(crtc) == 0)
  1299. break;
  1300. }
  1301. }
  1302. output_comp = mtk_ddp_comp_request_output(mtk_crtc);
  1303. if (output_comp)
  1304. mtk_ddp_comp_io_cmd(output_comp, NULL,
  1305. GET_FRAME_HRT_BW_BY_DATARATE, &bw);
  1306. return (unsigned int)bw;
  1307. }
  1308. static unsigned int overlap_to_bw(struct drm_crtc *crtc,
  1309. unsigned int overlap_num)
  1310. {
  1311. unsigned int bw_base = mtk_drm_primary_frame_bw(crtc);
  1312. unsigned int bw = bw_base * overlap_num / 2;
  1313. return bw;
  1314. }
  1315. static void mtk_crtc_update_hrt_state(struct drm_crtc *crtc,
  1316. unsigned int frame_weight,
  1317. struct cmdq_pkt *cmdq_handle)
  1318. {
  1319. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  1320. struct mtk_crtc_state *crtc_state = to_mtk_crtc_state(crtc->state);
  1321. struct cmdq_pkt_buffer *cmdq_buf = &(mtk_crtc->gce_obj.buf);
  1322. unsigned int bw = overlap_to_bw(crtc, frame_weight);
  1323. DDPINFO("%s bw=%d, last_hrt_req=%d\n",
  1324. __func__, bw, mtk_crtc->qos_ctx->last_hrt_req);
  1325. /* Only update HRT information on path with HRT comp */
  1326. if (bw > mtk_crtc->qos_ctx->last_hrt_req) {
  1327. #ifdef MTK_FB_MMDVFS_SUPPORT
  1328. mtk_disp_set_hrt_bw(mtk_crtc, bw);
  1329. #endif
  1330. cmdq_pkt_write(cmdq_handle, mtk_crtc->gce_obj.base,
  1331. cmdq_buf->pa_base + DISP_SLOT_CUR_HRT_LEVEL,
  1332. NO_PENDING_HRT, ~0);
  1333. } else if (bw < mtk_crtc->qos_ctx->last_hrt_req) {
  1334. cmdq_pkt_write(cmdq_handle, mtk_crtc->gce_obj.base,
  1335. cmdq_buf->pa_base + DISP_SLOT_CUR_HRT_LEVEL,
  1336. bw, ~0);
  1337. }
  1338. mtk_crtc->qos_ctx->last_hrt_req = bw;
  1339. cmdq_pkt_write(cmdq_handle, mtk_crtc->gce_obj.base,
  1340. cmdq_buf->pa_base + DISP_SLOT_CUR_HRT_IDX,
  1341. crtc_state->prop_val[CRTC_PROP_LYE_IDX], ~0);
  1342. }
  1343. #if defined(CONFIG_MACH_MT6853) || defined(CONFIG_MACH_MT6833)
  1344. static void mtk_crtc_update_hrt_state_ex(struct drm_crtc *crtc,
  1345. struct mtk_drm_lyeblob_ids *lyeblob_ids,
  1346. struct cmdq_pkt *cmdq_handle)
  1347. {
  1348. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  1349. struct mtk_crtc_state *crtc_state = to_mtk_crtc_state(crtc->state);
  1350. struct cmdq_pkt_buffer *cmdq_buf = &(mtk_crtc->gce_obj.buf);
  1351. unsigned int bw = overlap_to_bw(crtc, lyeblob_ids->frame_weight);
  1352. int crtc_idx = drm_crtc_index(crtc);
  1353. unsigned int ovl0_2l_no_compress_num =
  1354. HRT_GET_NO_COMPRESS_FLAG(lyeblob_ids->hrt_num);
  1355. struct mtk_ddp_comp *output_comp;
  1356. struct drm_display_mode *mode = NULL;
  1357. unsigned int max_fps = 0;
  1358. DDPINFO("%s bw=%d, last_hrt_req=%d\n",
  1359. __func__, bw, mtk_crtc->qos_ctx->last_hrt_req);
  1360. output_comp = mtk_ddp_comp_request_output(mtk_crtc);
  1361. if (output_comp && ((output_comp->id == DDP_COMPONENT_DSI0) ||
  1362. (output_comp->id == DDP_COMPONENT_DSI1))
  1363. && !(mtk_dsi_is_cmd_mode(output_comp)))
  1364. mtk_ddp_comp_io_cmd(output_comp, NULL,
  1365. DSI_GET_MODE_BY_MAX_VREFRESH, &mode);
  1366. if (mode)
  1367. max_fps = mode->vrefresh;
  1368. DDPINFO("%s CRTC%u bw:%d, no_compress_num:%d max_fps:%d\n",
  1369. __func__, crtc_idx, bw, ovl0_2l_no_compress_num, max_fps);
  1370. /* Workaround for 120hz SMI larb BW limitation */
  1371. if (crtc_idx == 0 && max_fps == 120) {
  1372. if (ovl0_2l_no_compress_num == 1 &&
  1373. bw < 2944) {
  1374. bw = 2944;
  1375. DDPINFO("%s CRTC%u dram freq to 1600hz\n",
  1376. __func__, crtc_idx);
  1377. } else if (ovl0_2l_no_compress_num == 2 &&
  1378. bw < 3433) {
  1379. bw = 3433;
  1380. DDPINFO("%s CRTC%u dram freq to 2400hz\n",
  1381. __func__, crtc_idx);
  1382. }
  1383. }
  1384. /* Only update HRT information on path with HRT comp */
  1385. if (bw > mtk_crtc->qos_ctx->last_hrt_req) {
  1386. #ifdef MTK_FB_MMDVFS_SUPPORT
  1387. mtk_disp_set_hrt_bw(mtk_crtc, bw);
  1388. #endif
  1389. mtk_crtc->qos_ctx->last_hrt_req = bw;
  1390. cmdq_pkt_write(cmdq_handle, mtk_crtc->gce_obj.base,
  1391. cmdq_buf->pa_base + DISP_SLOT_CUR_HRT_LEVEL,
  1392. NO_PENDING_HRT, ~0);
  1393. } else if (bw < mtk_crtc->qos_ctx->last_hrt_req) {
  1394. cmdq_pkt_write(cmdq_handle, mtk_crtc->gce_obj.base,
  1395. cmdq_buf->pa_base + DISP_SLOT_CUR_HRT_LEVEL,
  1396. bw, ~0);
  1397. }
  1398. mtk_crtc->qos_ctx->last_hrt_req = bw;
  1399. cmdq_pkt_write(cmdq_handle, mtk_crtc->gce_obj.base,
  1400. cmdq_buf->pa_base + DISP_SLOT_CUR_HRT_IDX,
  1401. crtc_state->prop_val[CRTC_PROP_LYE_IDX], ~0);
  1402. }
  1403. #endif
  1404. static void copy_drm_disp_mode(struct drm_display_mode *src,
  1405. struct drm_display_mode *dst)
  1406. {
  1407. dst->clock = src->clock;
  1408. dst->hdisplay = src->hdisplay;
  1409. dst->hsync_start = src->hsync_start;
  1410. dst->hsync_end = src->hsync_end;
  1411. dst->htotal = src->htotal;
  1412. dst->vdisplay = src->vdisplay;
  1413. dst->vsync_start = src->vsync_start;
  1414. dst->vsync_end = src->vsync_end;
  1415. dst->vtotal = src->vtotal;
  1416. dst->vrefresh = src->vrefresh;
  1417. }
  1418. struct golden_setting_context *
  1419. __get_golden_setting_context(struct mtk_drm_crtc *mtk_crtc)
  1420. {
  1421. static struct golden_setting_context gs_ctx[MAX_CRTC];
  1422. struct drm_crtc *crtc = &mtk_crtc->base;
  1423. int idx = drm_crtc_index(&mtk_crtc->base);
  1424. /* default setting */
  1425. gs_ctx[idx].is_dc = 0;
  1426. /* primary_display */
  1427. switch (idx) {
  1428. case 0:
  1429. gs_ctx[idx].is_vdo_mode =
  1430. mtk_crtc_is_frame_trigger_mode(crtc) ? 0 : 1;
  1431. gs_ctx[idx].dst_width = crtc->state->adjusted_mode.hdisplay;
  1432. gs_ctx[idx].dst_height = crtc->state->adjusted_mode.vdisplay;
  1433. if (mtk_crtc->panel_ext && mtk_crtc->panel_ext->params) {
  1434. struct mtk_panel_params *params;
  1435. params = mtk_crtc->panel_ext->params;
  1436. if (params->dyn_fps.switch_en == 1 &&
  1437. params->dyn_fps.vact_timing_fps != 0)
  1438. gs_ctx[idx].vrefresh =
  1439. params->dyn_fps.vact_timing_fps;
  1440. else
  1441. gs_ctx[idx].vrefresh =
  1442. crtc->state->adjusted_mode.vrefresh;
  1443. } else
  1444. gs_ctx[idx].vrefresh =
  1445. crtc->state->adjusted_mode.vrefresh;
  1446. break;
  1447. case 1:
  1448. /* TO DO: need more smart judge */
  1449. gs_ctx[idx].is_vdo_mode = 1;
  1450. gs_ctx[idx].dst_width = crtc->state->adjusted_mode.hdisplay;
  1451. gs_ctx[idx].dst_height = crtc->state->adjusted_mode.vdisplay;
  1452. break;
  1453. case 2:
  1454. /* TO DO: need more smart judge */
  1455. gs_ctx[idx].is_vdo_mode = 0;
  1456. break;
  1457. }
  1458. return &gs_ctx[idx];
  1459. }
  1460. unsigned int mtk_crtc_get_idle_interval(struct drm_crtc *crtc, unsigned int fps)
  1461. {
  1462. unsigned int idle_interval = mtk_drm_get_idle_check_interval(crtc);
  1463. /*calculate the timeout to enter idle in ms*/
  1464. if (idle_interval > 50)
  1465. return 0;
  1466. idle_interval = (3 * 1000) / fps + 1;
  1467. DDPMSG("[fps]:%s,[fps->idle interval][%d fps->%d ms]\n",
  1468. __func__, fps, idle_interval);
  1469. return idle_interval;
  1470. }
  1471. static void mtk_crtc_disp_mode_switch_begin(struct drm_crtc *crtc,
  1472. struct drm_crtc_state *old_state, struct mtk_crtc_state *mtk_state,
  1473. struct cmdq_pkt *cmdq_handle)
  1474. {
  1475. struct mtk_crtc_state *old_mtk_state = to_mtk_crtc_state(old_state);
  1476. struct drm_display_mode *mode;
  1477. struct mtk_ddp_config cfg;
  1478. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  1479. struct mtk_ddp_comp *comp;
  1480. unsigned int fps_src, fps_dst;
  1481. unsigned int i, j;
  1482. unsigned int fps_chg_index = 0;
  1483. unsigned int _idle_timeout = 50;/*ms*/
  1484. int en = 1;
  1485. struct mtk_ddp_comp *output_comp;
  1486. /* Check if disp_mode_idx change */
  1487. if (old_mtk_state->prop_val[CRTC_PROP_DISP_MODE_IDX] ==
  1488. mtk_state->prop_val[CRTC_PROP_DISP_MODE_IDX])
  1489. return;
  1490. DDPMSG("%s from %u to %u\n", __func__,
  1491. old_mtk_state->prop_val[CRTC_PROP_DISP_MODE_IDX],
  1492. mtk_state->prop_val[CRTC_PROP_DISP_MODE_IDX]);
  1493. /* Update mode & adjusted_mode in CRTC */
  1494. mode = mtk_drm_crtc_avail_disp_mode(crtc,
  1495. mtk_state->prop_val[CRTC_PROP_DISP_MODE_IDX]);
  1496. fps_src = crtc->state->mode.vrefresh;
  1497. fps_dst = mode->vrefresh;
  1498. copy_drm_disp_mode(mode, &crtc->state->mode);
  1499. drm_mode_set_crtcinfo(&crtc->state->mode, 0);
  1500. copy_drm_disp_mode(mode, &crtc->state->adjusted_mode);
  1501. drm_mode_set_crtcinfo(&crtc->state->adjusted_mode, 0);
  1502. output_comp = mtk_ddp_comp_request_output(mtk_crtc);
  1503. if (output_comp)
  1504. mtk_ddp_comp_io_cmd(output_comp, NULL, DYN_FPS_INDEX,
  1505. old_state);
  1506. fps_chg_index = output_comp->mtk_crtc->fps_change_index;
  1507. //to do fps change index adjust
  1508. if (fps_chg_index &
  1509. (DYNFPS_DSI_HFP | DYNFPS_DSI_MIPI_CLK)) {
  1510. /*ToDo HFP/MIPI CLOCK solution*/
  1511. DDPMSG("%s,Update RDMA golden_setting\n", __func__);
  1512. /* Update RDMA golden_setting */
  1513. cfg.w = crtc->state->adjusted_mode.hdisplay;
  1514. cfg.h = crtc->state->adjusted_mode.vdisplay;
  1515. cfg.vrefresh = crtc->state->adjusted_mode.vrefresh;
  1516. cfg.bpc = mtk_crtc->bpc;
  1517. cfg.p_golden_setting_context =
  1518. __get_golden_setting_context(mtk_crtc);
  1519. for_each_comp_in_cur_crtc_path(comp, mtk_crtc, i, j)
  1520. mtk_ddp_comp_io_cmd(comp, cmdq_handle,
  1521. MTK_IO_CMD_RDMA_GOLDEN_SETTING, &cfg);
  1522. }
  1523. mtk_ddp_comp_io_cmd(output_comp, cmdq_handle, DSI_LFR_SET, NULL);
  1524. /* pull up mm clk if dst fps is higher than src fps */
  1525. if (output_comp && fps_dst >= fps_src)
  1526. mtk_ddp_comp_io_cmd(output_comp, NULL, SET_MMCLK_BY_DATARATE,
  1527. &en);
  1528. /* Change DSI mipi clk & send LCM cmd */
  1529. if (output_comp)
  1530. mtk_ddp_comp_io_cmd(output_comp, NULL, DSI_TIMING_CHANGE,
  1531. old_state);
  1532. if (output_comp && fps_dst < fps_src)
  1533. mtk_ddp_comp_io_cmd(output_comp, NULL, SET_MMCLK_BY_DATARATE,
  1534. &en);
  1535. drm_invoke_fps_chg_callbacks(crtc->state->adjusted_mode.vrefresh);
  1536. /* update framedur_ns for VSYNC report */
  1537. drm_calc_timestamping_constants(crtc, &crtc->state->mode);
  1538. /* update idle timeout*/
  1539. _idle_timeout = mtk_crtc_get_idle_interval(crtc, fps_dst);
  1540. if (_idle_timeout > 0)
  1541. mtk_drm_set_idle_check_interval(crtc, _idle_timeout);
  1542. mtk_drm_idlemgr_kick(__func__, crtc, 0);
  1543. }
  1544. bool already_free;
  1545. bool mtk_crtc_frame_buffer_existed(void)
  1546. {
  1547. DDPMSG("%s, frame buffer is freed:%d\n", __func__, already_free);
  1548. return !already_free;
  1549. }
  1550. static void mtk_crtc_update_ddp_state(struct drm_crtc *crtc,
  1551. struct drm_crtc_state *old_crtc_state,
  1552. struct mtk_crtc_state *crtc_state,
  1553. struct cmdq_pkt *cmdq_handle)
  1554. {
  1555. struct mtk_crtc_state *old_mtk_state =
  1556. to_mtk_crtc_state(old_crtc_state);
  1557. struct mtk_drm_lyeblob_ids *lyeblob_ids, *next;
  1558. struct mtk_drm_private *mtk_drm = crtc->dev->dev_private;
  1559. int index = drm_crtc_index(crtc);
  1560. int crtc_mask = 0x1 << index;
  1561. unsigned int prop_lye_idx;
  1562. unsigned int pan_disp_frame_weight = 4;
  1563. struct drm_device *dev = crtc->dev;
  1564. mutex_lock(&mtk_drm->lyeblob_list_mutex);
  1565. prop_lye_idx = crtc_state->prop_val[CRTC_PROP_LYE_IDX];
  1566. /*set_hrt_bw for pan display ,set 4 for two RGB layer*/
  1567. if (index == 0 && prop_lye_idx == 0) {
  1568. DDPINFO("%s prop_lye_idx is 0, mode switch from %u to %u\n",
  1569. __func__,
  1570. old_mtk_state->prop_val[CRTC_PROP_DISP_MODE_IDX],
  1571. crtc_state->prop_val[CRTC_PROP_DISP_MODE_IDX]);
  1572. /*
  1573. * prop_lye_idx is 0 when suspend. Update display mode to avoid
  1574. * the dsi params not sync with the mode of new crtc state.
  1575. */
  1576. mtk_crtc_disp_mode_switch_begin(crtc,
  1577. old_crtc_state, crtc_state,
  1578. cmdq_handle);
  1579. mtk_crtc_update_hrt_state(crtc, pan_disp_frame_weight,
  1580. cmdq_handle);
  1581. }
  1582. list_for_each_entry_safe(lyeblob_ids, next, &mtk_drm->lyeblob_head,
  1583. list) {
  1584. if (lyeblob_ids->lye_idx > prop_lye_idx) {
  1585. DDPMSG("lyeblob lost ID:%d\n", prop_lye_idx);
  1586. break;
  1587. } else if (lyeblob_ids->lye_idx == prop_lye_idx) {
  1588. if (index == 0)
  1589. mtk_crtc_disp_mode_switch_begin(crtc,
  1590. old_crtc_state, crtc_state,
  1591. cmdq_handle);
  1592. if (index == 0) {
  1593. #if defined(CONFIG_MACH_MT6853) || defined(CONFIG_MACH_MT6833)
  1594. mtk_crtc_update_hrt_state_ex(
  1595. crtc, lyeblob_ids,
  1596. cmdq_handle);
  1597. #else
  1598. mtk_crtc_update_hrt_state(
  1599. crtc, lyeblob_ids->frame_weight,
  1600. cmdq_handle);
  1601. #endif
  1602. }
  1603. mtk_crtc_get_plane_comp_state(crtc, cmdq_handle);
  1604. mtk_crtc_atmoic_ddp_config(crtc, lyeblob_ids,
  1605. cmdq_handle);
  1606. if (lyeblob_ids->lye_idx == 2 && !already_free) {
  1607. /*free fb buf in second query valid*/
  1608. DDPMSG("%s, %d release frame buffer\n");
  1609. mtk_drm_fb_gem_release(dev);
  1610. free_fb_buf();
  1611. already_free = true;
  1612. }
  1613. break;
  1614. } else if (lyeblob_ids->lye_idx < prop_lye_idx) {
  1615. if (lyeblob_ids->ref_cnt) {
  1616. DDPINFO("free:(0x%x,0x%x), cnt:%d\n",
  1617. lyeblob_ids->free_cnt_mask,
  1618. crtc_mask,
  1619. lyeblob_ids->ref_cnt);
  1620. if (lyeblob_ids->free_cnt_mask & crtc_mask) {
  1621. lyeblob_ids->free_cnt_mask &=
  1622. (~crtc_mask);
  1623. lyeblob_ids->ref_cnt--;
  1624. DDPINFO("free:(0x%x,0x%x), cnt:%d\n",
  1625. lyeblob_ids->free_cnt_mask,
  1626. crtc_mask,
  1627. lyeblob_ids->ref_cnt);
  1628. }
  1629. if (!lyeblob_ids->ref_cnt) {
  1630. DDPINFO("free lyeblob:(%d,%d)\n",
  1631. lyeblob_ids->lye_idx,
  1632. prop_lye_idx);
  1633. mtk_crtc_free_lyeblob_ids(crtc,
  1634. lyeblob_ids);
  1635. mtk_crtc_free_ddpblob_ids(crtc,
  1636. lyeblob_ids);
  1637. }
  1638. }
  1639. }
  1640. }
  1641. mutex_unlock(&mtk_drm->lyeblob_list_mutex);
  1642. }
  1643. #ifdef MTK_DRM_FENCE_SUPPORT
  1644. static void mtk_crtc_release_lye_idx(struct drm_crtc *crtc)
  1645. {
  1646. struct mtk_drm_lyeblob_ids *lyeblob_ids, *next;
  1647. struct mtk_drm_private *mtk_drm = crtc->dev->dev_private;
  1648. int index = drm_crtc_index(crtc);
  1649. int crtc_mask = 0x1 << index;
  1650. mutex_lock(&mtk_drm->lyeblob_list_mutex);
  1651. list_for_each_entry_safe(lyeblob_ids, next, &mtk_drm->lyeblob_head,
  1652. list) {
  1653. if (lyeblob_ids->ref_cnt) {
  1654. DDPINFO("%s:%d free:(0x%x,0x%x), cnt:%d\n",
  1655. __func__, __LINE__,
  1656. lyeblob_ids->free_cnt_mask,
  1657. crtc_mask,
  1658. lyeblob_ids->ref_cnt);
  1659. if (lyeblob_ids->free_cnt_mask & crtc_mask) {
  1660. lyeblob_ids->free_cnt_mask &= (~crtc_mask);
  1661. lyeblob_ids->ref_cnt--;
  1662. DDPINFO("%s:%d free:(0x%x,0x%x), cnt:%d\n",
  1663. __func__, __LINE__,
  1664. lyeblob_ids->free_cnt_mask,
  1665. crtc_mask,
  1666. lyeblob_ids->ref_cnt);
  1667. }
  1668. if (!lyeblob_ids->ref_cnt) {
  1669. DDPINFO("%s:%d free lyeblob:%d\n",
  1670. __func__, __LINE__,
  1671. lyeblob_ids->lye_idx);
  1672. mtk_crtc_free_lyeblob_ids(crtc,
  1673. lyeblob_ids);
  1674. mtk_crtc_free_ddpblob_ids(crtc,
  1675. lyeblob_ids);
  1676. }
  1677. }
  1678. }
  1679. mutex_unlock(&mtk_drm->lyeblob_list_mutex);
  1680. }
  1681. #endif
  1682. #endif
  1683. bool mtk_crtc_with_trigger_loop(struct drm_crtc *crtc)
  1684. {
  1685. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  1686. if (mtk_crtc->gce_obj.client[CLIENT_TRIG_LOOP])
  1687. return true;
  1688. return false;
  1689. }
  1690. /* sw workaround to fix gce hw bug */
  1691. #if defined(CONFIG_MACH_MT6873) || defined(CONFIG_MACH_MT6853) || \
  1692. defined(CONFIG_MACH_MT6833)
  1693. bool mtk_crtc_with_sodi_loop(struct drm_crtc *crtc)
  1694. {
  1695. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  1696. struct mtk_drm_private *priv = NULL;
  1697. priv = mtk_crtc->base.dev->dev_private;
  1698. if (mtk_crtc->gce_obj.client[CLIENT_SODI_LOOP])
  1699. return true;
  1700. return false;
  1701. }
  1702. #endif
  1703. bool mtk_crtc_is_frame_trigger_mode(struct drm_crtc *crtc)
  1704. {
  1705. struct mtk_drm_private *priv = crtc->dev->dev_private;
  1706. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  1707. int crtc_id = drm_crtc_index(crtc);
  1708. struct mtk_ddp_comp *comp = NULL;
  1709. int i;
  1710. if (crtc_id == 0)
  1711. return mtk_dsi_is_cmd_mode(priv->ddp_comp[DDP_COMPONENT_DSI0]);
  1712. for_each_comp_in_crtc_target_path(
  1713. comp, mtk_crtc, i,
  1714. DDP_FIRST_PATH)
  1715. if (mtk_ddp_comp_is_output(comp))
  1716. break;
  1717. if (!comp) {
  1718. DDPPR_ERR("%s, Cannot find output component\n", __func__);
  1719. return false;
  1720. }
  1721. if (comp->id == DDP_COMPONENT_DP_INTF0 ||
  1722. comp->id == DDP_COMPONENT_DPI0 ||
  1723. comp->id == DDP_COMPONENT_DPI1) {
  1724. pr_info("%s(%d-%d) is vdo mode\n", __func__,
  1725. comp->id, DDP_COMPONENT_DPI0);
  1726. return false;
  1727. }
  1728. return true;
  1729. }
  1730. static bool mtk_crtc_target_is_dc_mode(struct drm_crtc *crtc,
  1731. unsigned int ddp_mode)
  1732. {
  1733. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  1734. if (ddp_mode >= DDP_MODE_NR)
  1735. return false;
  1736. if (mtk_crtc->ddp_ctx[ddp_mode].ddp_comp_nr[1])
  1737. return true;
  1738. return false;
  1739. }
  1740. static bool mtk_crtc_support_dc_mode(struct drm_crtc *crtc)
  1741. {
  1742. int i;
  1743. for (i = 0; i < DDP_MODE_NR; i++)
  1744. if (mtk_crtc_target_is_dc_mode(crtc, i))
  1745. return true;
  1746. return false;
  1747. }
  1748. bool mtk_crtc_is_dc_mode(struct drm_crtc *crtc)
  1749. {
  1750. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  1751. return mtk_crtc_target_is_dc_mode(crtc, mtk_crtc->ddp_mode);
  1752. }
  1753. bool mtk_crtc_is_mem_mode(struct drm_crtc *crtc)
  1754. {
  1755. /* for find memory session */
  1756. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  1757. struct mtk_ddp_comp *comp = mtk_ddp_comp_request_output(mtk_crtc);
  1758. if (!comp)
  1759. return false;
  1760. if (comp->id == DDP_COMPONENT_WDMA0 ||
  1761. comp->id == DDP_COMPONENT_WDMA1)
  1762. return true;
  1763. return false;
  1764. }
  1765. int get_path_wait_event(struct mtk_drm_crtc *mtk_crtc,
  1766. enum CRTC_DDP_PATH ddp_path)
  1767. {
  1768. struct mtk_ddp_comp *comp = NULL;
  1769. int i;
  1770. for_each_comp_in_crtc_target_path(
  1771. comp, mtk_crtc, i,
  1772. ddp_path)
  1773. if (mtk_ddp_comp_is_output(comp))
  1774. break;
  1775. if (!comp) {
  1776. DDPPR_ERR("%s, Cannot find output component\n", __func__);
  1777. return -EINVAL;
  1778. }
  1779. if (comp->id == DDP_COMPONENT_DSI0 || comp->id == DDP_COMPONENT_DSI1) {
  1780. if (mtk_crtc_is_frame_trigger_mode(&mtk_crtc->base))
  1781. return mtk_crtc->gce_obj.event[EVENT_STREAM_EOF];
  1782. else
  1783. return mtk_crtc->gce_obj.event[EVENT_VDO_EOF];
  1784. } else if (comp->id == DDP_COMPONENT_DP_INTF0) {
  1785. return mtk_crtc->gce_obj.event[EVENT_VDO_EOF];
  1786. } else if (comp->id == DDP_COMPONENT_WDMA0) {
  1787. return mtk_crtc->gce_obj.event[EVENT_WDMA0_EOF];
  1788. } else if (comp->id == DDP_COMPONENT_WDMA1) {
  1789. return mtk_crtc->gce_obj.event[EVENT_WDMA1_EOF];
  1790. }
  1791. DDPPR_ERR("The output component has not frame done event\n");
  1792. return -EINVAL;
  1793. }
  1794. void mtk_crtc_wait_frame_done(struct mtk_drm_crtc *mtk_crtc,
  1795. struct cmdq_pkt *cmdq_handle,
  1796. enum CRTC_DDP_PATH ddp_path,
  1797. int clear_event)
  1798. {
  1799. int gce_event;
  1800. gce_event = get_path_wait_event(mtk_crtc, ddp_path);
  1801. if (gce_event < 0)
  1802. return;
  1803. if (gce_event == mtk_crtc->gce_obj.event[EVENT_STREAM_EOF] ||
  1804. gce_event == mtk_crtc->gce_obj.event[EVENT_VDO_EOF]) {
  1805. struct mtk_drm_private *priv;
  1806. if (clear_event)
  1807. cmdq_pkt_wfe(cmdq_handle, gce_event);
  1808. else
  1809. cmdq_pkt_wait_no_clear(cmdq_handle, gce_event);
  1810. priv = mtk_crtc->base.dev->dev_private;
  1811. if (gce_event == mtk_crtc->gce_obj.event[EVENT_VDO_EOF] &&
  1812. mtk_drm_helper_get_opt(priv->helper_opt,
  1813. MTK_DRM_OPT_LAYER_REC) &&
  1814. mtk_crtc->layer_rec_en) {
  1815. cmdq_pkt_wait_no_clear(cmdq_handle,
  1816. mtk_crtc->gce_obj.event[EVENT_STREAM_EOF]);
  1817. }
  1818. } else if (gce_event == mtk_crtc->gce_obj.event[EVENT_WDMA0_EOF]) {
  1819. /* Must clear WDMA_EOF in decouple mode */
  1820. if (mtk_crtc_is_dc_mode(&mtk_crtc->base))
  1821. cmdq_pkt_wfe(cmdq_handle, gce_event);
  1822. } else if (gce_event == mtk_crtc->gce_obj.event[EVENT_WDMA1_EOF]) {
  1823. if (mtk_crtc_is_dc_mode(&mtk_crtc->base))
  1824. cmdq_pkt_wfe(cmdq_handle, gce_event);
  1825. } else
  1826. DDPPR_ERR("The output component has not frame done event\n");
  1827. }
  1828. static void mtk_crtc_cmdq_timeout_cb(struct cmdq_cb_data data)
  1829. {
  1830. struct drm_crtc *crtc = data.data;
  1831. if (!crtc) {
  1832. DDPPR_ERR("%s find crtc fail\n", __func__);
  1833. return;
  1834. }
  1835. DDPPR_ERR("%s cmdq timeout, crtc id:%d\n", __func__,
  1836. drm_crtc_index(crtc));
  1837. mtk_drm_crtc_analysis(crtc);
  1838. mtk_drm_crtc_dump(crtc);
  1839. /* CMDQ driver would not trigger aee when timeout. */
  1840. DDPAEE("%s cmdq timeout, crtc id:%d\n", __func__, drm_crtc_index(crtc));
  1841. }
  1842. void mtk_crtc_pkt_create(struct cmdq_pkt **cmdq_handle, struct drm_crtc *crtc,
  1843. struct cmdq_client *cl)
  1844. {
  1845. *cmdq_handle = cmdq_pkt_create(cl);
  1846. if (IS_ERR_OR_NULL(*cmdq_handle)) {
  1847. DDPPR_ERR("%s create handle fail, %x\n",
  1848. __func__, *cmdq_handle);
  1849. return;
  1850. }
  1851. (*cmdq_handle)->err_cb.cb = mtk_crtc_cmdq_timeout_cb;
  1852. (*cmdq_handle)->err_cb.data = crtc;
  1853. }
  1854. static void sub_cmdq_cb(struct cmdq_cb_data data)
  1855. {
  1856. struct mtk_cmdq_cb_data *cb_data = data.data;
  1857. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(cb_data->crtc);
  1858. struct mtk_drm_private *priv = mtk_crtc->base.dev->dev_private;
  1859. struct cmdq_pkt_buffer *cmdq_buf = &(mtk_crtc->gce_obj.buf);
  1860. int session_id = -1, id = drm_crtc_index(cb_data->crtc), i;
  1861. unsigned int intr_fence = 0;
  1862. for (i = 0; i < MAX_SESSION_COUNT; i++) {
  1863. if ((id + 1) == MTK_SESSION_TYPE(priv->session_id[i])) {
  1864. session_id = priv->session_id[i];
  1865. break;
  1866. }
  1867. }
  1868. /* Release output buffer fence */
  1869. intr_fence = *(unsigned int *)(cmdq_buf->va_base +
  1870. DISP_SLOT_CUR_INTERFACE_FENCE);
  1871. if (intr_fence >= 1) {
  1872. DDPINFO("intr fence_idx:%d\n", intr_fence);
  1873. mtk_release_fence(session_id,
  1874. mtk_fence_get_interface_timeline_id(), intr_fence - 1);
  1875. }
  1876. cmdq_pkt_destroy(cb_data->cmdq_handle);
  1877. kfree(cb_data);
  1878. }
  1879. void mtk_crtc_release_output_buffer_fence(
  1880. struct drm_crtc *crtc, int session_id)
  1881. {
  1882. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  1883. struct cmdq_pkt_buffer *cmdq_buf = &(mtk_crtc->gce_obj.buf);
  1884. unsigned int fence_idx = 0;
  1885. fence_idx = *(unsigned int *)(cmdq_buf->va_base +
  1886. DISP_SLOT_CUR_OUTPUT_FENCE);
  1887. if (fence_idx) {
  1888. DDPINFO("output fence_idx:%d\n", fence_idx);
  1889. mtk_release_fence(session_id,
  1890. mtk_fence_get_output_timeline_id(), fence_idx);
  1891. }
  1892. }
  1893. struct drm_framebuffer *mtk_drm_framebuffer_lookup(struct drm_device *dev,
  1894. unsigned int id)
  1895. {
  1896. struct drm_framebuffer *fb = NULL;
  1897. fb = drm_framebuffer_lookup(dev, NULL, id);
  1898. if (!fb)
  1899. return NULL;
  1900. /* CRITICAL: drop the reference we picked up in framebuffer lookup */
  1901. drm_framebuffer_put(fb);
  1902. return fb;
  1903. }
  1904. static void mtk_crtc_dc_config_color_matrix(struct drm_crtc *crtc,
  1905. struct cmdq_pkt *cmdq_handle)
  1906. {
  1907. int i, mode, ccorr_matrix[16], all_zero = 1;
  1908. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  1909. struct cmdq_pkt_buffer *cmdq_buf = &(mtk_crtc->gce_obj.buf);
  1910. struct mtk_crtc_ddp_ctx *ddp_ctx;
  1911. bool set = false;
  1912. /* Get color matrix data from backup slot*/
  1913. mode = *(int *)(cmdq_buf->va_base + DISP_SLOT_COLOR_MATRIX_PARAMS(0));
  1914. for (i = 0; i < 16; i++)
  1915. ccorr_matrix[i] = *(int *)(cmdq_buf->va_base +
  1916. DISP_SLOT_COLOR_MATRIX_PARAMS(i + 1));
  1917. for (i = 0; i <= 15; i += 5) {
  1918. if (ccorr_matrix[i] != 0) {
  1919. all_zero = 0;
  1920. break;
  1921. }
  1922. }
  1923. if (all_zero)
  1924. DDPPR_ERR("CCORR color matrix backup param is zero matrix\n");
  1925. else {
  1926. ddp_ctx = &mtk_crtc->ddp_ctx[mtk_crtc->ddp_mode];
  1927. for (i = 0; i < ddp_ctx->ddp_comp_nr[DDP_SECOND_PATH]; i++) {
  1928. struct mtk_ddp_comp *comp =
  1929. ddp_ctx->ddp_comp[DDP_SECOND_PATH][i];
  1930. if (comp->id == DDP_COMPONENT_CCORR0) {
  1931. disp_ccorr_set_color_matrix(comp, cmdq_handle,
  1932. ccorr_matrix, mode, false);
  1933. set = true;
  1934. break;
  1935. }
  1936. }
  1937. if (!set)
  1938. DDPPR_ERR("Cannot not find DDP_COMPONENT_CCORR0\n");
  1939. }
  1940. }
  1941. void mtk_crtc_dc_prim_path_update(struct drm_crtc *crtc)
  1942. {
  1943. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  1944. struct cmdq_pkt *cmdq_handle;
  1945. struct mtk_plane_state plane_state;
  1946. struct drm_framebuffer *fb;
  1947. struct mtk_crtc_ddp_ctx *ddp_ctx;
  1948. struct cmdq_pkt_buffer *cmdq_buf = &(mtk_crtc->gce_obj.buf);
  1949. struct mtk_cmdq_cb_data *cb_data;
  1950. unsigned int fb_idx, fb_id;
  1951. int session_id;
  1952. DDPINFO("%s+\n", __func__);
  1953. DDP_MUTEX_LOCK(&mtk_crtc->lock, __func__, __LINE__);
  1954. if (!mtk_crtc_is_dc_mode(crtc)) {
  1955. DDP_MUTEX_UNLOCK(&mtk_crtc->lock, __func__, __LINE__);
  1956. return;
  1957. }
  1958. session_id = mtk_get_session_id(crtc);
  1959. mtk_crtc_release_output_buffer_fence(crtc, session_id);
  1960. /* find fb for RDMA */
  1961. fb_idx = *(unsigned int *)(cmdq_buf->va_base + DISP_SLOT_RDMA_FB_IDX);
  1962. fb_id = *(unsigned int *)(cmdq_buf->va_base + DISP_SLOT_RDMA_FB_ID);
  1963. /* 1-to-2*/
  1964. if (!fb_id)
  1965. goto end;
  1966. fb = mtk_drm_framebuffer_lookup(mtk_crtc->base.dev, fb_id);
  1967. if (fb == NULL) {
  1968. DDPPR_ERR("%s cannot find fb fb_id:%u\n", __func__, fb_id);
  1969. goto end;
  1970. }
  1971. cb_data = kmalloc(sizeof(*cb_data), GFP_KERNEL);
  1972. if (!cb_data) {
  1973. DDP_MUTEX_UNLOCK(&mtk_crtc->lock, __func__, __LINE__);
  1974. DDPPR_ERR("cb data creation failed\n");
  1975. return;
  1976. }
  1977. mtk_crtc_pkt_create(&cmdq_handle, crtc,
  1978. mtk_crtc->gce_obj.client[CLIENT_CFG]);
  1979. cmdq_pkt_wait_no_clear(cmdq_handle,
  1980. get_path_wait_event(mtk_crtc, DDP_FIRST_PATH));
  1981. mtk_crtc_wait_frame_done(mtk_crtc, cmdq_handle, DDP_SECOND_PATH, 0);
  1982. ddp_ctx = &mtk_crtc->ddp_ctx[mtk_crtc->ddp_mode];
  1983. plane_state.pending.enable = true;
  1984. plane_state.pending.pitch = fb->pitches[0];
  1985. plane_state.pending.format = fb->format->format;
  1986. plane_state.pending.addr =
  1987. mtk_fb_get_dma(fb) +
  1988. (dma_addr_t)mtk_crtc_get_dc_fb_size(crtc) *
  1989. (dma_addr_t)fb_idx;
  1990. plane_state.pending.size = mtk_fb_get_size(fb);
  1991. plane_state.pending.src_x = 0;
  1992. plane_state.pending.src_y = 0;
  1993. plane_state.pending.dst_x = 0;
  1994. plane_state.pending.dst_y = 0;
  1995. plane_state.pending.width = fb->width;
  1996. plane_state.pending.height = fb->height;
  1997. mtk_ddp_comp_layer_config(ddp_ctx->ddp_comp[DDP_SECOND_PATH][0], 0,
  1998. &plane_state, cmdq_handle);
  1999. mtk_crtc_dc_config_color_matrix(crtc, cmdq_handle);
  2000. if (mtk_crtc_is_frame_trigger_mode(&mtk_crtc->base))
  2001. cmdq_pkt_set_event(cmdq_handle,
  2002. mtk_crtc->gce_obj.event[EVENT_STREAM_DIRTY]);
  2003. cb_data->crtc = crtc;
  2004. cb_data->cmdq_handle = cmdq_handle;
  2005. if (cmdq_pkt_flush_threaded(cmdq_handle, sub_cmdq_cb, cb_data) < 0)
  2006. DDPPR_ERR("failed to flush sub\n");
  2007. end:
  2008. DDP_MUTEX_UNLOCK(&mtk_crtc->lock, __func__, __LINE__);
  2009. }
  2010. static void mtk_crtc_release_input_layer_fence(
  2011. struct drm_crtc *crtc, int session_id)
  2012. {
  2013. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  2014. struct cmdq_pkt_buffer *cmdq_buf = &(mtk_crtc->gce_obj.buf);
  2015. int i;
  2016. unsigned int fence_idx = 0;
  2017. cmdq_buf = &(mtk_crtc->gce_obj.buf);
  2018. for (i = 0; i < mtk_crtc->layer_nr; i++) {
  2019. unsigned int subtractor = 0;
  2020. fence_idx = *(unsigned int *)(cmdq_buf->va_base +
  2021. DISP_SLOT_CUR_CONFIG_FENCE(i));
  2022. subtractor = *(unsigned int *)(cmdq_buf->va_base +
  2023. DISP_SLOT_SUBTRACTOR_WHEN_FREE(i));
  2024. subtractor &= 0xFFFF;
  2025. if (drm_crtc_index(crtc) == 2)
  2026. DDPINFO("%d, fence_idx:%d, subtractor:%d\n",
  2027. i, fence_idx, subtractor);
  2028. mtk_release_fence(session_id, i, fence_idx - subtractor);
  2029. }
  2030. }
  2031. static void mtk_crtc_update_hrt_qos(struct drm_crtc *crtc,
  2032. unsigned int ddp_mode)
  2033. {
  2034. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  2035. struct cmdq_pkt_buffer *cmdq_buf = &(mtk_crtc->gce_obj.buf);
  2036. struct mtk_ddp_comp *comp;
  2037. unsigned int cur_hrt_bw, hrt_idx;
  2038. int i, j;
  2039. for_each_comp_in_target_ddp_mode_bound(comp, mtk_crtc,
  2040. i, j, ddp_mode, 0)
  2041. mtk_ddp_comp_io_cmd(comp, NULL, PMQOS_SET_BW, NULL);
  2042. if (drm_crtc_index(crtc) != 0)
  2043. return;
  2044. hrt_idx = *(unsigned int *)(cmdq_buf->va_base + DISP_SLOT_CUR_HRT_IDX);
  2045. atomic_set(&mtk_crtc->qos_ctx->last_hrt_idx, hrt_idx);
  2046. atomic_set(&mtk_crtc->qos_ctx->hrt_cond_sig, 1);
  2047. wake_up(&mtk_crtc->qos_ctx->hrt_cond_wq);
  2048. cur_hrt_bw = *(unsigned int *)(cmdq_buf->va_base +
  2049. DISP_SLOT_CUR_HRT_LEVEL);
  2050. if (cur_hrt_bw != NO_PENDING_HRT &&
  2051. cur_hrt_bw <= mtk_crtc->qos_ctx->last_hrt_req) {
  2052. DDPINFO("cur:%u last:%u, release HRT to last_hrt_req:%u\n",
  2053. cur_hrt_bw, mtk_crtc->qos_ctx->last_hrt_req,
  2054. mtk_crtc->qos_ctx->last_hrt_req);
  2055. #ifdef MTK_FB_MMDVFS_SUPPORT
  2056. mtk_disp_set_hrt_bw(mtk_crtc,
  2057. mtk_crtc->qos_ctx->last_hrt_req);
  2058. #endif
  2059. *(unsigned int *)(cmdq_buf->va_base + DISP_SLOT_CUR_HRT_LEVEL) =
  2060. NO_PENDING_HRT;
  2061. }
  2062. }
  2063. static void mtk_crtc_enable_iommu(struct mtk_drm_crtc *mtk_crtc,
  2064. struct cmdq_pkt *handle)
  2065. {
  2066. int i, j, p_mode;
  2067. struct mtk_ddp_comp *comp;
  2068. for_each_comp_in_all_crtc_mode(comp, mtk_crtc, i, j, p_mode)
  2069. mtk_ddp_comp_iommu_enable(comp, handle);
  2070. if (mtk_crtc->is_dual_pipe) {
  2071. for_each_comp_in_dual_pipe(comp, mtk_crtc, i, j)
  2072. mtk_ddp_comp_iommu_enable(comp, handle);
  2073. }
  2074. }
  2075. void mtk_crtc_enable_iommu_runtime(struct mtk_drm_crtc *mtk_crtc,
  2076. struct cmdq_pkt *handle)
  2077. {
  2078. int i, j;
  2079. struct mtk_ddp_comp *comp;
  2080. struct mtk_ddp_fb_info fb_info;
  2081. struct mtk_drm_private *priv = mtk_crtc->base.dev->dev_private;
  2082. struct mtk_drm_gem_obj *mtk_gem = to_mtk_gem_obj(priv->fbdev_bo);
  2083. unsigned int vramsize = 0, fps = 0;
  2084. phys_addr_t fb_base = 0;
  2085. mtk_crtc_enable_iommu(mtk_crtc, handle);
  2086. _parse_tag_videolfb(&vramsize, &fb_base, &fps);
  2087. fb_info.fb_mva = mtk_gem->dma_addr;
  2088. fb_info.fb_size =
  2089. priv->fb_helper.fb->width * priv->fb_helper.fb->height / 3;
  2090. fb_info.fb_pa = fb_base;
  2091. for_each_comp_in_cur_crtc_path(comp, mtk_crtc, i, j)
  2092. mtk_ddp_comp_io_cmd(comp, handle, OVL_REPLACE_BOOTUP_MVA,
  2093. &fb_info);
  2094. }
  2095. #ifdef MTK_DRM_CMDQ_ASYNC
  2096. static void ddp_cmdq_cb(struct cmdq_cb_data data)
  2097. {
  2098. struct mtk_cmdq_cb_data *cb_data = data.data;
  2099. struct drm_crtc_state *crtc_state = cb_data->state;
  2100. struct drm_atomic_state *atomic_state = crtc_state->state;
  2101. struct drm_crtc *crtc = crtc_state->crtc;
  2102. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  2103. int session_id, id;
  2104. unsigned int ovl_status = 0;
  2105. DDPINFO("crtc_state:%px, atomic_state:%px, crtc:%px\n",
  2106. crtc_state,
  2107. atomic_state,
  2108. crtc);
  2109. session_id = mtk_get_session_id(crtc);
  2110. id = drm_crtc_index(crtc);
  2111. CRTC_MMP_EVENT_START(id, frame_cfg, 0, 0);
  2112. if (id == 0) {
  2113. struct cmdq_pkt_buffer *cmdq_buf = &(mtk_crtc->gce_obj.buf);
  2114. ovl_status = *(unsigned int *)(cmdq_buf->va_base +
  2115. DISP_SLOT_OVL_STATUS);
  2116. #if defined(CONFIG_MACH_MT6885) || defined(CONFIG_MACH_MT6893)
  2117. if (ovl_status & 1)
  2118. DDPPR_ERR("ovl status error\n");
  2119. #endif
  2120. #if defined(CONFIG_MACH_MT6873) || defined(CONFIG_MACH_MT6853) || \
  2121. defined(CONFIG_MACH_MT6833)
  2122. if (ovl_status & 1) {
  2123. DDPPR_ERR("ovl status error\n");
  2124. mtk_drm_crtc_analysis(crtc);
  2125. mtk_drm_crtc_dump(crtc);
  2126. }
  2127. #endif
  2128. }
  2129. CRTC_MMP_MARK(id, frame_cfg, ovl_status, 0);
  2130. if (session_id > 0)
  2131. mtk_crtc_release_input_layer_fence(crtc, session_id);
  2132. #ifdef MTK_DRM_DELAY_PRESENT_FENCE
  2133. // release present fence
  2134. if (drm_crtc_index(crtc) != 2 && session_id > 0) {
  2135. struct cmdq_pkt_buffer *cmdq_buf = &(mtk_crtc->gce_obj.buf);
  2136. unsigned int fence_idx = *(unsigned int *)(cmdq_buf->va_base +
  2137. DISP_SLOT_PRESENT_FENCE(drm_crtc_index(crtc)));
  2138. mtk_release_present_fence(session_id, fence_idx);
  2139. }
  2140. #endif
  2141. DDP_MUTEX_LOCK(&mtk_crtc->lock, __func__, __LINE__);
  2142. if (!mtk_crtc_is_dc_mode(crtc) && session_id > 0)
  2143. mtk_crtc_release_output_buffer_fence(crtc, session_id);
  2144. mtk_crtc_update_hrt_qos(crtc, cb_data->misc);
  2145. if (mtk_crtc->pending_needs_vblank) {
  2146. mtk_drm_crtc_finish_page_flip(mtk_crtc);
  2147. mtk_crtc->pending_needs_vblank = false;
  2148. }
  2149. mtk_atomic_state_put_queue(atomic_state);
  2150. if (mtk_crtc->wb_enable == true) {
  2151. mtk_crtc->wb_enable = false;
  2152. drm_writeback_signal_completion(&mtk_crtc->wb_connector, 0);
  2153. }
  2154. DDP_MUTEX_UNLOCK(&mtk_crtc->lock, __func__, __LINE__);
  2155. cmdq_pkt_destroy(cb_data->cmdq_handle);
  2156. kfree(cb_data);
  2157. CRTC_MMP_EVENT_END(id, frame_cfg, 0, 0);
  2158. }
  2159. #else
  2160. /* ddp_cmdq_cb_blocking should be called within locked function */
  2161. static void ddp_cmdq_cb_blocking(struct mtk_cmdq_cb_data *cb_data)
  2162. {
  2163. struct drm_crtc_state *crtc_state = cb_data->state;
  2164. struct drm_atomic_state *atomic_state = crtc_state->state;
  2165. struct drm_crtc *crtc = crtc_state->crtc;
  2166. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  2167. struct mtk_drm_private *private;
  2168. int session_id = -1, id, i;
  2169. DDPINFO("%s:%d, cb_data:%px\n",
  2170. __func__, __LINE__,
  2171. cb_data);
  2172. DDPINFO("crtc_state:%px, atomic_state:%px, crtc:%px\n",
  2173. crtc_state,
  2174. atomic_state,
  2175. crtc);
  2176. id = drm_crtc_index(crtc);
  2177. private = mtk_crtc->base.dev->dev_private;
  2178. for (i = 0; i < MAX_SESSION_COUNT; i++) {
  2179. if ((id + 1) == MTK_SESSION_TYPE(private->session_id[i])) {
  2180. session_id = private->session_id[i];
  2181. break;
  2182. }
  2183. }
  2184. mtk_crtc_release_input_layer_fence(crtc, session_id);
  2185. mtk_crtc_release_output_buffer_fence(crtc, session_id);
  2186. mtk_crtc_update_hrt_qos(crtc, cb_data->misc);
  2187. if (mtk_crtc->pending_needs_vblank) {
  2188. mtk_drm_crtc_finish_page_flip(mtk_crtc);
  2189. mtk_crtc->pending_needs_vblank = false;
  2190. }
  2191. mtk_atomic_state_put_queue(atomic_state);
  2192. if (mtk_crtc->wb_enable == true) {
  2193. mtk_crtc->wb_enable = false;
  2194. drm_writeback_signal_completion(&mtk_crtc->wb_connector, 0);
  2195. }
  2196. cmdq_pkt_destroy(cb_data->cmdq_handle);
  2197. kfree(cb_data);
  2198. }
  2199. #endif
  2200. static void mtk_crtc_ddp_config(struct drm_crtc *crtc)
  2201. {
  2202. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  2203. struct mtk_crtc_state *state = to_mtk_crtc_state(mtk_crtc->base.state);
  2204. struct mtk_ddp_comp *comp = mtk_crtc_get_comp(crtc, 0, 0);
  2205. struct mtk_ddp_config cfg;
  2206. struct cmdq_pkt *cmdq_handle = state->cmdq_handle;
  2207. unsigned int i;
  2208. unsigned int ovl_is_busy;
  2209. struct cmdq_pkt_buffer *cmdq_buf = &(mtk_crtc->gce_obj.buf);
  2210. unsigned int last_fence, cur_fence, sub;
  2211. /*
  2212. * TODO: instead of updating the registers here, we should prepare
  2213. * working registers in atomic_commit and let the hardware command
  2214. * queue update module registers on vblank.
  2215. */
  2216. ovl_is_busy = readl(comp->regs) & 0x1UL;
  2217. if (ovl_is_busy == 0x1UL)
  2218. return;
  2219. if ((state->pending_config) == true) {
  2220. cfg.w = state->pending_width;
  2221. cfg.h = state->pending_height;
  2222. if (mtk_crtc->panel_ext && mtk_crtc->panel_ext->params) {
  2223. struct mtk_panel_params *params;
  2224. params = mtk_crtc->panel_ext->params;
  2225. if (params->dyn_fps.switch_en == 1 &&
  2226. params->dyn_fps.vact_timing_fps != 0)
  2227. cfg.vrefresh =
  2228. params->dyn_fps.vact_timing_fps;
  2229. else
  2230. cfg.vrefresh = state->pending_vrefresh;
  2231. } else
  2232. cfg.vrefresh = state->pending_vrefresh;
  2233. cfg.bpc = 0;
  2234. mtk_ddp_comp_config(comp, &cfg, cmdq_handle);
  2235. state->pending_config = false;
  2236. }
  2237. if ((mtk_crtc->pending_planes) == false)
  2238. return;
  2239. #ifndef CONFIG_MTK_DISPLAY_CMDQ
  2240. mtk_wb_atomic_commit(mtk_crtc);
  2241. #endif
  2242. for (i = 0; i < mtk_crtc->layer_nr; i++) {
  2243. struct drm_plane *plane = &mtk_crtc->planes[i].base;
  2244. struct mtk_plane_state *plane_state;
  2245. plane_state = to_mtk_plane_state(plane->state);
  2246. if ((plane_state->pending.config) == false)
  2247. continue;
  2248. mtk_ddp_comp_layer_config(comp, i, plane_state, cmdq_handle);
  2249. last_fence = *(unsigned int *)(cmdq_buf->va_base +
  2250. DISP_SLOT_CUR_CONFIG_FENCE(i));
  2251. cur_fence =
  2252. plane_state->pending.prop_val[PLANE_PROP_NEXT_BUFF_IDX];
  2253. if (cur_fence != -1 && cur_fence > last_fence)
  2254. cmdq_pkt_write(cmdq_handle, mtk_crtc->gce_obj.base,
  2255. cmdq_buf->pa_base +
  2256. DISP_SLOT_CUR_CONFIG_FENCE(i),
  2257. cur_fence, ~0);
  2258. sub = 1;
  2259. cmdq_pkt_write(cmdq_handle, mtk_crtc->gce_obj.base,
  2260. cmdq_buf->pa_base +
  2261. DISP_SLOT_SUBTRACTOR_WHEN_FREE(i),
  2262. sub, ~0);
  2263. plane_state->pending.config = false;
  2264. }
  2265. mtk_crtc->pending_planes = false;
  2266. if (mtk_crtc->wb_enable == true) {
  2267. mtk_crtc->wb_enable = false;
  2268. drm_writeback_signal_completion(&mtk_crtc->wb_connector, 0);
  2269. }
  2270. }
  2271. static void mtk_crtc_comp_trigger(struct mtk_drm_crtc *mtk_crtc,
  2272. struct cmdq_pkt *cmdq_handle,
  2273. enum mtk_ddp_comp_trigger_flag trig_flag)
  2274. {
  2275. int i, j;
  2276. struct mtk_ddp_comp *comp;
  2277. for_each_comp_in_cur_crtc_path(comp, mtk_crtc, i, j)
  2278. mtk_ddp_comp_config_trigger(comp, cmdq_handle, trig_flag);
  2279. }
  2280. int mtk_crtc_comp_is_busy(struct mtk_drm_crtc *mtk_crtc)
  2281. {
  2282. int ret = 0;
  2283. int i, j;
  2284. struct mtk_ddp_comp *comp;
  2285. for_each_comp_in_cur_crtc_path(comp, mtk_crtc, i, j) {
  2286. ret = mtk_ddp_comp_is_busy(comp);
  2287. if (ret)
  2288. return ret;
  2289. }
  2290. return ret;
  2291. }
  2292. /* TODO: need to remove this in vdo mode for lowpower */
  2293. static void trig_done_cb(struct cmdq_cb_data data)
  2294. {
  2295. CRTC_MMP_MARK((unsigned long)data.data, trig_loop_done, 0, 0);
  2296. }
  2297. void mtk_crtc_clear_wait_event(struct drm_crtc *crtc)
  2298. {
  2299. struct cmdq_pkt *cmdq_handle;
  2300. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  2301. if (mtk_crtc_is_frame_trigger_mode(crtc)) {
  2302. mtk_crtc_pkt_create(&cmdq_handle, crtc,
  2303. mtk_crtc->gce_obj.client[CLIENT_CFG]);
  2304. cmdq_pkt_set_event(cmdq_handle,
  2305. mtk_crtc->gce_obj.event[EVENT_STREAM_EOF]);
  2306. cmdq_pkt_set_event(cmdq_handle,
  2307. mtk_crtc->gce_obj.event[EVENT_ESD_EOF]);
  2308. cmdq_pkt_set_event(cmdq_handle,
  2309. mtk_crtc->gce_obj.event[EVENT_CABC_EOF]);
  2310. cmdq_pkt_flush(cmdq_handle);
  2311. cmdq_pkt_destroy(cmdq_handle);
  2312. }
  2313. }
  2314. static void mtk_crtc_rec_trig_cnt(struct mtk_drm_crtc *mtk_crtc,
  2315. struct cmdq_pkt *cmdq_handle)
  2316. {
  2317. struct cmdq_pkt_buffer *cmdq_buf = &mtk_crtc->gce_obj.buf;
  2318. struct cmdq_operand lop, rop;
  2319. lop.reg = true;
  2320. lop.idx = CMDQ_CPR_DISP_CNT;
  2321. rop.reg = false;
  2322. rop.value = 1;
  2323. cmdq_pkt_logic_command(cmdq_handle, CMDQ_LOGIC_ADD, CMDQ_CPR_DISP_CNT,
  2324. &lop, &rop);
  2325. cmdq_pkt_write_reg_addr(cmdq_handle,
  2326. cmdq_buf->pa_base + DISP_SLOT_TRIG_CNT,
  2327. CMDQ_CPR_DISP_CNT, U32_MAX);
  2328. }
  2329. /* sw workaround to fix gce hw bug */
  2330. #if defined(CONFIG_MACH_MT6873) || defined(CONFIG_MACH_MT6853) || \
  2331. defined(CONFIG_MACH_MT6833)
  2332. void mtk_crtc_start_sodi_loop(struct drm_crtc *crtc)
  2333. {
  2334. struct cmdq_pkt *cmdq_handle;
  2335. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  2336. struct mtk_drm_private *priv = NULL;
  2337. unsigned long crtc_id = (unsigned long)drm_crtc_index(crtc);
  2338. if (crtc_id) {
  2339. DDPDBG("%s:%d invalid crtc:%ld\n",
  2340. __func__, __LINE__, crtc_id);
  2341. return;
  2342. }
  2343. priv = mtk_crtc->base.dev->dev_private;
  2344. mtk_crtc->sodi_loop_cmdq_handle = cmdq_pkt_create(
  2345. mtk_crtc->gce_obj.client[CLIENT_SODI_LOOP]);
  2346. cmdq_handle = mtk_crtc->sodi_loop_cmdq_handle;
  2347. cmdq_pkt_wait_no_clear(cmdq_handle,
  2348. mtk_crtc->gce_obj.event[EVENT_VDO_EOF]);
  2349. cmdq_pkt_write(cmdq_handle, NULL,
  2350. GCE_BASE_ADDR + GCE_GCTL_VALUE, GCE_DDR_EN, GCE_DDR_EN);
  2351. cmdq_pkt_wfe(cmdq_handle,
  2352. mtk_crtc->gce_obj.event[EVENT_SYNC_TOKEN_SODI]);
  2353. cmdq_pkt_finalize_loop(cmdq_handle);
  2354. cmdq_pkt_flush_async(cmdq_handle, NULL, (void *)crtc_id);
  2355. }
  2356. #endif
  2357. void mtk_crtc_start_trig_loop(struct drm_crtc *crtc)
  2358. {
  2359. int ret = 0;
  2360. struct cmdq_pkt *cmdq_handle;
  2361. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  2362. unsigned long crtc_id = (unsigned long)drm_crtc_index(crtc);
  2363. struct mtk_drm_private *priv = crtc->dev->dev_private;
  2364. #if defined(CONFIG_MACH_MT6873) || defined(CONFIG_MACH_MT6853) || \
  2365. defined(CONFIG_MACH_MT6833)
  2366. struct cmdq_operand lop, rop;
  2367. const u16 reg_jump = CMDQ_THR_SPR_IDX1;
  2368. const u16 var1 = CMDQ_CPR_DDR_USR_CNT;
  2369. const u16 var2 = 0;
  2370. u32 inst_condi_jump;
  2371. u64 *inst, jump_pa;
  2372. lop.reg = true;
  2373. lop.idx = var1;
  2374. rop.reg = false;
  2375. rop.idx = var2;
  2376. #endif
  2377. if (crtc_id > 1) {
  2378. DDPPR_ERR("%s:%d invalid crtc:%ld\n",
  2379. __func__, __LINE__, crtc_id);
  2380. return;
  2381. }
  2382. mtk_crtc->trig_loop_cmdq_handle = cmdq_pkt_create(
  2383. mtk_crtc->gce_obj.client[CLIENT_TRIG_LOOP]);
  2384. cmdq_handle = mtk_crtc->trig_loop_cmdq_handle;
  2385. if (mtk_crtc_is_frame_trigger_mode(crtc)) {
  2386. cmdq_pkt_wfe(cmdq_handle,
  2387. mtk_crtc->gce_obj.event[EVENT_STREAM_DIRTY]);
  2388. #ifndef CONFIG_FPGA_EARLY_PORTING
  2389. cmdq_pkt_clear_event(cmdq_handle,
  2390. mtk_crtc->gce_obj.event[EVENT_TE]);
  2391. if (mtk_drm_lcm_is_connect())
  2392. cmdq_pkt_wfe(cmdq_handle,
  2393. mtk_crtc->gce_obj.event[EVENT_TE]);
  2394. /* The STREAM BLOCK EVENT is used for stopping frame trigger if
  2395. * the engine is stopped
  2396. */
  2397. cmdq_pkt_wait_no_clear(
  2398. cmdq_handle,
  2399. mtk_crtc->gce_obj.event[EVENT_STREAM_BLOCK]);
  2400. #endif
  2401. cmdq_pkt_clear_event(cmdq_handle,
  2402. mtk_crtc->gce_obj.event[EVENT_STREAM_EOF]);
  2403. cmdq_pkt_clear_event(
  2404. cmdq_handle,
  2405. mtk_crtc->gce_obj.event[EVENT_STREAM_DIRTY]);
  2406. #ifndef CONFIG_FPGA_EARLY_PORTING
  2407. cmdq_pkt_wait_no_clear(cmdq_handle,
  2408. mtk_crtc->gce_obj.event[EVENT_CABC_EOF]);
  2409. cmdq_pkt_wait_no_clear(cmdq_handle,
  2410. mtk_crtc->gce_obj.event[EVENT_ESD_EOF]);
  2411. #endif
  2412. /*Trigger*/
  2413. mtk_disp_mutex_enable_cmdq(mtk_crtc->mutex[0], cmdq_handle,
  2414. mtk_crtc->gce_obj.base);
  2415. mtk_crtc_comp_trigger(mtk_crtc, cmdq_handle,
  2416. MTK_TRIG_FLAG_TRIGGER);
  2417. cmdq_pkt_wfe(cmdq_handle,
  2418. mtk_crtc->gce_obj.event[EVENT_CMD_EOF]);
  2419. mtk_crtc_comp_trigger(mtk_crtc, cmdq_handle, MTK_TRIG_FLAG_EOF);
  2420. if (mtk_drm_helper_get_opt(priv->helper_opt,
  2421. MTK_DRM_OPT_LAYER_REC)) {
  2422. mtk_crtc_comp_trigger(mtk_crtc, cmdq_handle,
  2423. MTK_TRIG_FLAG_LAYER_REC);
  2424. mtk_crtc_rec_trig_cnt(mtk_crtc, cmdq_handle);
  2425. mtk_crtc->layer_rec_en = true;
  2426. } else {
  2427. mtk_crtc->layer_rec_en = false;
  2428. }
  2429. cmdq_pkt_set_event(cmdq_handle,
  2430. mtk_crtc->gce_obj.event[EVENT_STREAM_EOF]);
  2431. } else {
  2432. mtk_disp_mutex_submit_sof(mtk_crtc->mutex[0]);
  2433. cmdq_pkt_wfe(cmdq_handle,
  2434. mtk_crtc->gce_obj.event[EVENT_VDO_EOF]);
  2435. /* sw workaround to fix gce hw bug */
  2436. #if defined(CONFIG_MACH_MT6873) || defined(CONFIG_MACH_MT6853) || \
  2437. defined(CONFIG_MACH_MT6833)
  2438. cmdq_pkt_read(cmdq_handle, NULL,
  2439. GCE_BASE_ADDR + GCE_DEBUG_START_ADDR, var1);
  2440. /*mark condition jump */
  2441. inst_condi_jump = cmdq_handle->cmd_buf_size;
  2442. cmdq_pkt_assign_command(cmdq_handle, reg_jump, 0);
  2443. cmdq_pkt_cond_jump_abs(cmdq_handle, reg_jump, &lop, &rop,
  2444. CMDQ_NOT_EQUAL);
  2445. /* if condition false, will jump here */
  2446. cmdq_pkt_write(cmdq_handle, NULL,
  2447. GCE_BASE_ADDR + GCE_GCTL_VALUE, 0, GCE_DDR_EN);
  2448. /* if condition true, will jump curreent postzion */
  2449. inst = cmdq_pkt_get_va_by_offset(cmdq_handle, inst_condi_jump);
  2450. jump_pa = cmdq_pkt_get_pa_by_offset(cmdq_handle,
  2451. cmdq_handle->cmd_buf_size);
  2452. *inst = *inst | CMDQ_REG_SHIFT_ADDR(jump_pa);
  2453. cmdq_pkt_set_event(cmdq_handle,
  2454. mtk_crtc->gce_obj.event[EVENT_SYNC_TOKEN_SODI]);
  2455. #endif
  2456. if (mtk_drm_helper_get_opt(priv->helper_opt,
  2457. MTK_DRM_OPT_LAYER_REC)) {
  2458. cmdq_pkt_clear_event(cmdq_handle,
  2459. mtk_crtc->gce_obj.event[EVENT_RDMA0_EOF]);
  2460. cmdq_pkt_clear_event(cmdq_handle,
  2461. mtk_crtc->gce_obj.event[EVENT_STREAM_EOF]);
  2462. cmdq_pkt_wfe(cmdq_handle,
  2463. mtk_crtc->gce_obj.event[EVENT_RDMA0_EOF]);
  2464. mtk_crtc_comp_trigger(mtk_crtc, cmdq_handle,
  2465. MTK_TRIG_FLAG_LAYER_REC);
  2466. mtk_crtc_rec_trig_cnt(mtk_crtc, cmdq_handle);
  2467. cmdq_pkt_set_event(cmdq_handle,
  2468. mtk_crtc->gce_obj.event[EVENT_STREAM_EOF]);
  2469. mtk_crtc->layer_rec_en = true;
  2470. } else {
  2471. mtk_crtc->layer_rec_en = false;
  2472. }
  2473. }
  2474. cmdq_pkt_finalize_loop(cmdq_handle);
  2475. ret = cmdq_pkt_flush_async(cmdq_handle, trig_done_cb, (void *)crtc_id);
  2476. mtk_crtc_clear_wait_event(crtc);
  2477. }
  2478. void mtk_crtc_hw_block_ready(struct drm_crtc *crtc)
  2479. {
  2480. struct cmdq_pkt *cmdq_handle;
  2481. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  2482. mtk_crtc_pkt_create(&cmdq_handle, &mtk_crtc->base,
  2483. mtk_crtc->gce_obj.client[CLIENT_CFG]);
  2484. cmdq_pkt_set_event(cmdq_handle,
  2485. mtk_crtc->gce_obj.event[EVENT_STREAM_BLOCK]);
  2486. cmdq_pkt_flush(cmdq_handle);
  2487. cmdq_pkt_destroy(cmdq_handle);
  2488. }
  2489. void mtk_crtc_stop_trig_loop(struct drm_crtc *crtc)
  2490. {
  2491. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  2492. cmdq_mbox_stop(mtk_crtc->gce_obj.client[CLIENT_TRIG_LOOP]);
  2493. cmdq_pkt_destroy(mtk_crtc->trig_loop_cmdq_handle);
  2494. }
  2495. /* sw workaround to fix gce hw bug */
  2496. #if defined(CONFIG_MACH_MT6873) || defined(CONFIG_MACH_MT6853) || \
  2497. defined(CONFIG_MACH_MT6833)
  2498. void mtk_crtc_stop_sodi_loop(struct drm_crtc *crtc)
  2499. {
  2500. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  2501. struct mtk_drm_private *priv = NULL;
  2502. if (!mtk_crtc->sodi_loop_cmdq_handle) {
  2503. DDPDBG("%s: sodi_loop already stopped\n", __func__);
  2504. return;
  2505. }
  2506. priv = mtk_crtc->base.dev->dev_private;
  2507. cmdq_mbox_stop(mtk_crtc->gce_obj.client[CLIENT_SODI_LOOP]);
  2508. cmdq_pkt_destroy(mtk_crtc->sodi_loop_cmdq_handle);
  2509. mtk_crtc->sodi_loop_cmdq_handle = NULL;
  2510. }
  2511. #endif
  2512. long mtk_crtc_wait_status(struct drm_crtc *crtc, bool status, long timeout)
  2513. {
  2514. long ret;
  2515. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  2516. ret = wait_event_timeout(mtk_crtc->crtc_status_wq,
  2517. mtk_crtc->enabled == status, timeout);
  2518. return ret;
  2519. }
  2520. bool mtk_crtc_set_status(struct drm_crtc *crtc, bool status)
  2521. {
  2522. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  2523. bool old_status = mtk_crtc->enabled;
  2524. struct drm_device *dev = crtc->dev;
  2525. struct mtk_drm_private *private = dev->dev_private;
  2526. mtk_crtc->enabled = status;
  2527. wake_up(&mtk_crtc->crtc_status_wq);
  2528. if (drm_crtc_index(crtc) == 0 && private->fb_helper.fb && status) {
  2529. crtc->primary->fb = private->fb_helper.fb;
  2530. drm_mode_object_reference(&private->fb_helper.fb->base);
  2531. }
  2532. return old_status;
  2533. }
  2534. int mtk_crtc_attach_ddp_comp(struct drm_crtc *crtc, int ddp_mode,
  2535. bool is_attach)
  2536. {
  2537. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  2538. struct mtk_ddp_comp *comp;
  2539. int i, j;
  2540. if (ddp_mode < 0)
  2541. return -EINVAL;
  2542. for_each_comp_in_crtc_target_mode(comp, mtk_crtc, i, j, ddp_mode) {
  2543. if (is_attach)
  2544. comp->mtk_crtc = mtk_crtc;
  2545. else
  2546. comp->mtk_crtc = NULL;
  2547. }
  2548. return 0;
  2549. }
  2550. int mtk_crtc_update_ddp_sw_status(struct drm_crtc *crtc, int enable)
  2551. {
  2552. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  2553. if (enable)
  2554. mtk_crtc_attach_ddp_comp(crtc, mtk_crtc->ddp_mode, true);
  2555. else
  2556. mtk_crtc_attach_ddp_comp(crtc, mtk_crtc->ddp_mode, false);
  2557. return 0;
  2558. }
  2559. static void mtk_crtc_addon_connector_disconnect(struct drm_crtc *crtc,
  2560. struct cmdq_pkt *handle)
  2561. {
  2562. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  2563. struct mtk_panel_params *panel_ext = mtk_drm_get_lcm_ext_params(crtc);
  2564. struct mtk_ddp_comp *dsc_comp;
  2565. struct mtk_drm_private *priv = mtk_crtc->base.dev->dev_private;
  2566. if (panel_ext &&
  2567. panel_ext->output_mode == MTK_PANEL_DSC_SINGLE_PORT) {
  2568. dsc_comp = priv->ddp_comp[DDP_COMPONENT_DSC0];
  2569. #if defined(CONFIG_MACH_MT6885) || defined(CONFIG_MACH_MT6893)
  2570. mtk_ddp_remove_dsc_prim_MT6885(mtk_crtc, handle);
  2571. #endif
  2572. #if defined(CONFIG_MACH_MT6873)
  2573. mtk_ddp_remove_dsc_prim_MT6873(mtk_crtc, handle);
  2574. #endif
  2575. #if defined(CONFIG_MACH_MT6853)
  2576. mtk_ddp_remove_dsc_prim_MT6853(mtk_crtc, handle);
  2577. #endif
  2578. mtk_disp_mutex_remove_comp_with_cmdq(mtk_crtc, dsc_comp->id,
  2579. handle, 0);
  2580. mtk_ddp_comp_stop(dsc_comp, handle);
  2581. }
  2582. }
  2583. void mtk_crtc_disconnect_addon_module(struct drm_crtc *crtc)
  2584. {
  2585. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  2586. struct mtk_crtc_state *crtc_state = to_mtk_crtc_state(crtc->state);
  2587. struct cmdq_pkt *handle;
  2588. struct cmdq_client *client = mtk_crtc->gce_obj.client[CLIENT_CFG];
  2589. mtk_crtc_pkt_create(&handle, crtc, client);
  2590. _mtk_crtc_atmoic_addon_module_disconnect(
  2591. crtc, mtk_crtc->ddp_mode, &crtc_state->lye_state, handle);
  2592. mtk_crtc_addon_connector_disconnect(crtc, handle);
  2593. cmdq_pkt_flush(handle);
  2594. cmdq_pkt_destroy(handle);
  2595. }
  2596. static void mtk_crtc_addon_connector_connect(struct drm_crtc *crtc,
  2597. struct cmdq_pkt *handle)
  2598. {
  2599. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  2600. struct mtk_panel_params *panel_ext = mtk_drm_get_lcm_ext_params(crtc);
  2601. struct mtk_ddp_comp *dsc_comp;
  2602. struct mtk_drm_private *priv = mtk_crtc->base.dev->dev_private;
  2603. struct mtk_ddp_comp *output_comp;
  2604. if (panel_ext &&
  2605. panel_ext->output_mode == MTK_PANEL_DSC_SINGLE_PORT) {
  2606. struct mtk_ddp_config cfg;
  2607. dsc_comp = priv->ddp_comp[DDP_COMPONENT_DSC0];
  2608. cfg.w = crtc->state->adjusted_mode.hdisplay;
  2609. cfg.h = crtc->state->adjusted_mode.vdisplay;
  2610. output_comp = mtk_ddp_comp_request_output(mtk_crtc);
  2611. if (output_comp && drm_crtc_index(crtc) == 0) {
  2612. cfg.w = mtk_ddp_comp_io_cmd(
  2613. output_comp, NULL,
  2614. DSI_GET_VIRTUAL_WIDTH, NULL);
  2615. cfg.h = mtk_ddp_comp_io_cmd(
  2616. output_comp, NULL,
  2617. DSI_GET_VIRTUAL_HEIGH, NULL);
  2618. }
  2619. if (mtk_crtc->panel_ext && mtk_crtc->panel_ext->params) {
  2620. struct mtk_panel_params *params;
  2621. params = mtk_crtc->panel_ext->params;
  2622. if (params->dyn_fps.switch_en == 1 &&
  2623. params->dyn_fps.vact_timing_fps != 0)
  2624. cfg.vrefresh =
  2625. params->dyn_fps.vact_timing_fps;
  2626. else
  2627. cfg.vrefresh =
  2628. crtc->state->adjusted_mode.vrefresh;
  2629. } else
  2630. cfg.vrefresh = crtc->state->adjusted_mode.vrefresh;
  2631. cfg.bpc = mtk_crtc->bpc;
  2632. cfg.p_golden_setting_context =
  2633. __get_golden_setting_context(mtk_crtc);
  2634. dsc_comp->mtk_crtc = mtk_crtc;
  2635. /* insert DSC */
  2636. #if defined(CONFIG_MACH_MT6885) || defined(CONFIG_MACH_MT6893)
  2637. mtk_ddp_insert_dsc_prim_MT6885(mtk_crtc, handle);
  2638. #endif
  2639. #if defined(CONFIG_MACH_MT6873)
  2640. mtk_ddp_insert_dsc_prim_MT6873(mtk_crtc, handle);
  2641. #endif
  2642. #if defined(CONFIG_MACH_MT6853)
  2643. mtk_ddp_insert_dsc_prim_MT6853(mtk_crtc, handle);
  2644. #endif
  2645. mtk_disp_mutex_add_comp_with_cmdq(mtk_crtc, dsc_comp->id,
  2646. mtk_crtc_is_frame_trigger_mode(&mtk_crtc->base),
  2647. handle, 0);
  2648. mtk_ddp_comp_config(dsc_comp, &cfg, handle);
  2649. mtk_ddp_comp_start(dsc_comp, handle);
  2650. }
  2651. }
  2652. void mtk_crtc_connect_addon_module(struct drm_crtc *crtc)
  2653. {
  2654. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  2655. struct mtk_crtc_state *crtc_state = to_mtk_crtc_state(crtc->state);
  2656. struct cmdq_pkt *handle;
  2657. struct cmdq_client *client = mtk_crtc->gce_obj.client[CLIENT_CFG];
  2658. mtk_crtc_pkt_create(&handle, crtc, client);
  2659. _mtk_crtc_atmoic_addon_module_connect(crtc, mtk_crtc->ddp_mode,
  2660. &crtc_state->lye_state, handle);
  2661. mtk_crtc_addon_connector_connect(crtc, handle);
  2662. cmdq_pkt_flush(handle);
  2663. cmdq_pkt_destroy(handle);
  2664. }
  2665. /* set mutex & path mux for this CRTC default path */
  2666. void mtk_crtc_connect_default_path(struct mtk_drm_crtc *mtk_crtc)
  2667. {
  2668. unsigned int i, j;
  2669. struct mtk_ddp_comp *comp;
  2670. struct drm_crtc *crtc = &mtk_crtc->base;
  2671. struct mtk_ddp_comp **ddp_comp;
  2672. enum mtk_ddp_comp_id prev_id, next_id;
  2673. /* connect path */
  2674. for_each_comp_in_crtc_path_bound(comp, mtk_crtc, i, j, 1) {
  2675. ddp_comp = mtk_crtc->ddp_ctx[mtk_crtc->ddp_mode].ddp_comp[i];
  2676. prev_id = (j == 0 ? DDP_COMPONENT_ID_MAX : ddp_comp[j - 1]->id);
  2677. next_id = ddp_comp[j + 1]->id;
  2678. mtk_ddp_add_comp_to_path(mtk_crtc, ddp_comp[j], prev_id,
  2679. next_id);
  2680. }
  2681. /* add module in mutex */
  2682. if (mtk_crtc_is_dc_mode(crtc)) {
  2683. for_each_comp_in_crtc_target_path(comp, mtk_crtc, i,
  2684. DDP_FIRST_PATH)
  2685. mtk_disp_mutex_add_comp(mtk_crtc->mutex[1], comp->id);
  2686. for_each_comp_in_crtc_target_path(comp, mtk_crtc, i,
  2687. DDP_SECOND_PATH)
  2688. mtk_disp_mutex_add_comp(mtk_crtc->mutex[0], comp->id);
  2689. } else {
  2690. for_each_comp_in_crtc_target_path(comp, mtk_crtc, i,
  2691. DDP_FIRST_PATH)
  2692. mtk_disp_mutex_add_comp(mtk_crtc->mutex[0], comp->id);
  2693. }
  2694. if (mtk_crtc->is_dual_pipe) {
  2695. mtk_ddp_connect_dual_pipe_path(mtk_crtc, mtk_crtc->mutex[0]);
  2696. for_each_comp_in_dual_pipe(comp, mtk_crtc, i, j)
  2697. mtk_disp_mutex_add_comp(mtk_crtc->mutex[0], comp->id);
  2698. }
  2699. /* set mutex sof, eof */
  2700. mtk_disp_mutex_src_set(mtk_crtc, mtk_crtc_is_frame_trigger_mode(crtc));
  2701. /* if VDO mode, enable mutex by CPU here */
  2702. if (!mtk_crtc_is_frame_trigger_mode(crtc))
  2703. mtk_disp_mutex_enable(mtk_crtc->mutex[0]);
  2704. }
  2705. void mtk_crtc_init_plane_setting(struct mtk_drm_crtc *mtk_crtc)
  2706. {
  2707. struct mtk_plane_state *plane_state;
  2708. struct mtk_plane_pending_state *pending;
  2709. struct drm_plane *plane = &mtk_crtc->planes[0].base;
  2710. plane_state = to_mtk_plane_state(plane->state);
  2711. pending = &plane_state->pending;
  2712. pending->pitch = mtk_crtc->base.state->adjusted_mode.hdisplay*3;
  2713. pending->format = DRM_FORMAT_RGB888;
  2714. pending->src_x = 0;
  2715. pending->src_y = 0;
  2716. pending->dst_x = 0;
  2717. pending->dst_y = 0;
  2718. pending->height = mtk_crtc->base.state->adjusted_mode.vdisplay;
  2719. pending->width = mtk_crtc->base.state->adjusted_mode.hdisplay;
  2720. pending->config = 1;
  2721. pending->dirty = 1;
  2722. pending->enable = true;
  2723. /*constant color layer*/
  2724. pending->addr = 0;
  2725. pending->prop_val[PLANE_PROP_PLANE_ALPHA] = 0xFF;
  2726. pending->prop_val[PLANE_PROP_COMPRESS] = 0;
  2727. pending->prop_val[PLANE_PROP_ALPHA_CON] = 0;
  2728. plane_state->comp_state.lye_id = 0;
  2729. plane_state->comp_state.ext_lye_id = 0;
  2730. }
  2731. /* restore ovl layer config and set dal layer if any */
  2732. void mtk_crtc_restore_plane_setting(struct mtk_drm_crtc *mtk_crtc)
  2733. {
  2734. unsigned int i, j;
  2735. struct drm_crtc *crtc = &mtk_crtc->base;
  2736. struct cmdq_pkt *cmdq_handle;
  2737. struct mtk_ddp_comp *comp;
  2738. mtk_crtc_pkt_create(&cmdq_handle, &mtk_crtc->base,
  2739. mtk_crtc->gce_obj.client[CLIENT_CFG]);
  2740. if (drm_crtc_index(crtc) == 1)
  2741. mtk_crtc_init_plane_setting(mtk_crtc);
  2742. for (i = 0; i < mtk_crtc->layer_nr; i++) {
  2743. struct mtk_drm_private *priv = crtc->dev->dev_private;
  2744. struct drm_plane *plane = &mtk_crtc->planes[i].base;
  2745. struct mtk_plane_state *plane_state;
  2746. plane_state = to_mtk_plane_state(plane->state);
  2747. if (i >= OVL_PHY_LAYER_NR && !plane_state->comp_state.comp_id)
  2748. continue;
  2749. if (plane_state->comp_state.comp_id)
  2750. comp = priv->ddp_comp[plane_state->comp_state.comp_id];
  2751. else {
  2752. struct mtk_crtc_ddp_ctx *ddp_ctx;
  2753. /* TODO: all plane should contain proper mtk_plane_state
  2754. */
  2755. ddp_ctx = &mtk_crtc->ddp_ctx[mtk_crtc->ddp_mode];
  2756. comp = ddp_ctx->ddp_comp[DDP_FIRST_PATH][0];
  2757. }
  2758. if (comp == NULL)
  2759. continue;
  2760. mtk_ddp_comp_layer_config(comp, i, plane_state, cmdq_handle);
  2761. if (comp->id == DDP_COMPONENT_OVL2_2L
  2762. && mtk_crtc->is_dual_pipe) {
  2763. struct mtk_crtc_ddp_ctx *ddp_ctx;
  2764. DDPFUNC();
  2765. if (plane_state->pending.addr)
  2766. plane_state->pending.addr +=
  2767. plane_state->pending.pitch/2;
  2768. plane_state->pending.dst_x = 0;
  2769. plane_state->pending.dst_y = 0;
  2770. ddp_ctx = &mtk_crtc->dual_pipe_ddp_ctx;
  2771. comp = ddp_ctx->ddp_comp[DDP_FIRST_PATH][0];
  2772. plane_state->comp_state.comp_id =
  2773. DDP_COMPONENT_OVL3_2L;
  2774. mtk_ddp_comp_layer_config(comp,
  2775. i, plane_state, cmdq_handle);
  2776. //will be used next time
  2777. plane_state->comp_state.comp_id = DDP_COMPONENT_OVL2_2L;
  2778. }
  2779. }
  2780. if (mtk_drm_dal_enable() && drm_crtc_index(crtc) == 0)
  2781. drm_set_dal(&mtk_crtc->base, cmdq_handle);
  2782. /* Update QOS BW*/
  2783. for_each_comp_in_cur_crtc_path(comp, mtk_crtc, i, j)
  2784. mtk_ddp_comp_io_cmd(comp, cmdq_handle,
  2785. PMQOS_UPDATE_BW, NULL);
  2786. cmdq_pkt_flush(cmdq_handle);
  2787. cmdq_pkt_destroy(cmdq_handle);
  2788. }
  2789. static void mtk_crtc_disable_plane_setting(struct mtk_drm_crtc *mtk_crtc)
  2790. {
  2791. unsigned int i;
  2792. for (i = 0; i < mtk_crtc->layer_nr; i++) {
  2793. struct drm_plane *plane = &mtk_crtc->planes[i].base;
  2794. struct mtk_plane_state *plane_state
  2795. = to_mtk_plane_state(plane->state);
  2796. if (i < OVL_PHY_LAYER_NR || plane_state->comp_state.comp_id) {
  2797. plane_state->pending.enable = 0;
  2798. }
  2799. }
  2800. }
  2801. static void set_dirty_cmdq_cb(struct cmdq_cb_data data)
  2802. {
  2803. struct mtk_cmdq_cb_data *cb_data = data.data;
  2804. cmdq_pkt_destroy(cb_data->cmdq_handle);
  2805. kfree(cb_data);
  2806. }
  2807. static void mtk_crtc_set_dirty(struct mtk_drm_crtc *mtk_crtc)
  2808. {
  2809. struct cmdq_pkt *cmdq_handle;
  2810. struct mtk_cmdq_cb_data *cb_data;
  2811. cb_data = kmalloc(sizeof(*cb_data), GFP_KERNEL);
  2812. if (!cb_data) {
  2813. DDPINFO("%s:%d, cb data creation failed\n",
  2814. __func__, __LINE__);
  2815. return;
  2816. }
  2817. mtk_crtc_pkt_create(&cmdq_handle, &mtk_crtc->base,
  2818. mtk_crtc->gce_obj.client[CLIENT_CFG]);
  2819. cmdq_pkt_set_event(cmdq_handle,
  2820. mtk_crtc->gce_obj.event[EVENT_STREAM_DIRTY]);
  2821. cmdq_pkt_set_event(cmdq_handle,
  2822. mtk_crtc->gce_obj.event[EVENT_CABC_EOF]);
  2823. cmdq_pkt_set_event(cmdq_handle,
  2824. mtk_crtc->gce_obj.event[EVENT_ESD_EOF]);
  2825. cb_data->cmdq_handle = cmdq_handle;
  2826. if (cmdq_pkt_flush_threaded(cmdq_handle,
  2827. set_dirty_cmdq_cb, cb_data) < 0)
  2828. DDPPR_ERR("failed to flush set_dirty\n");
  2829. }
  2830. static int __mtk_check_trigger(struct mtk_drm_crtc *mtk_crtc)
  2831. {
  2832. struct drm_crtc *crtc = &mtk_crtc->base;
  2833. int index = drm_crtc_index(crtc);
  2834. struct mtk_crtc_state *mtk_state;
  2835. DDP_MUTEX_LOCK(&mtk_crtc->lock, __func__, __LINE__);
  2836. CRTC_MMP_EVENT_START(index, check_trigger, 0, 0);
  2837. mtk_drm_idlemgr_kick(__func__, &mtk_crtc->base, 0);
  2838. mtk_state = to_mtk_crtc_state(crtc->state);
  2839. if (!mtk_state->prop_val[CRTC_PROP_DOZE_ACTIVE] ||
  2840. (mtk_state->prop_val[CRTC_PROP_DOZE_ACTIVE] &&
  2841. atomic_read(&mtk_crtc->already_config))) {
  2842. mtk_crtc_set_dirty(mtk_crtc);
  2843. } else
  2844. DDPINFO("%s skip mtk_crtc_set_dirty\n", __func__);
  2845. CRTC_MMP_EVENT_END(index, check_trigger, 0, 0);
  2846. DDP_MUTEX_UNLOCK(&mtk_crtc->lock, __func__, __LINE__);
  2847. return 0;
  2848. }
  2849. static int _mtk_crtc_check_trigger(void *data)
  2850. {
  2851. struct mtk_drm_crtc *mtk_crtc = (struct mtk_drm_crtc *) data;
  2852. struct sched_param param = {.sched_priority = 94 };
  2853. int ret;
  2854. sched_setscheduler(current, SCHED_RR, &param);
  2855. atomic_set(&mtk_crtc->trig_event_act, 0);
  2856. while (1) {
  2857. ret = wait_event_interruptible(mtk_crtc->trigger_event,
  2858. atomic_read(&mtk_crtc->trig_event_act));
  2859. if (ret < 0)
  2860. DDPPR_ERR("wait %s fail, ret=%d\n", __func__, ret);
  2861. atomic_set(&mtk_crtc->trig_event_act, 0);
  2862. __mtk_check_trigger(mtk_crtc);
  2863. if (kthread_should_stop())
  2864. break;
  2865. }
  2866. return 0;
  2867. }
  2868. static int _mtk_crtc_check_trigger_delay(void *data)
  2869. {
  2870. struct mtk_drm_crtc *mtk_crtc = (struct mtk_drm_crtc *) data;
  2871. struct sched_param param = {.sched_priority = 94 };
  2872. int ret;
  2873. sched_setscheduler(current, SCHED_RR, &param);
  2874. atomic_set(&mtk_crtc->trig_delay_act, 0);
  2875. while (1) {
  2876. ret = wait_event_interruptible(mtk_crtc->trigger_delay,
  2877. atomic_read(&mtk_crtc->trig_delay_act));
  2878. if (ret < 0)
  2879. DDPPR_ERR("wait %s fail, ret=%d\n", __func__, ret);
  2880. atomic_set(&mtk_crtc->trig_delay_act, 0);
  2881. atomic_set(&mtk_crtc->delayed_trig, 0);
  2882. usleep_range(32000, 33000);
  2883. if (!atomic_read(&mtk_crtc->delayed_trig))
  2884. __mtk_check_trigger(mtk_crtc);
  2885. if (kthread_should_stop())
  2886. break;
  2887. }
  2888. return 0;
  2889. }
  2890. void mtk_crtc_check_trigger(struct mtk_drm_crtc *mtk_crtc, bool delay,
  2891. bool need_lock)
  2892. {
  2893. struct drm_crtc *crtc = &mtk_crtc->base;
  2894. int index = 0;
  2895. struct mtk_crtc_state *mtk_state;
  2896. struct mtk_panel_ext *panel_ext;
  2897. if (!mtk_crtc) {
  2898. DDPPR_ERR("%s:%d, invalid crtc:0x%p\n",
  2899. __func__, __LINE__, crtc);
  2900. return;
  2901. }
  2902. if (need_lock)
  2903. DDP_MUTEX_LOCK(&mtk_crtc->lock, __func__, __LINE__);
  2904. CRTC_MMP_EVENT_START(index, kick_trigger, (unsigned long)crtc, 0);
  2905. index = drm_crtc_index(crtc);
  2906. if (index) {
  2907. DDPPR_ERR("%s:%d, invalid crtc:0x%p, index:%d\n",
  2908. __func__, __LINE__, crtc, index);
  2909. CRTC_MMP_MARK(index, kick_trigger, 0, 1);
  2910. goto err;
  2911. }
  2912. if (!(mtk_crtc->enabled)) {
  2913. DDPINFO("%s:%d, slepted\n", __func__, __LINE__);
  2914. CRTC_MMP_MARK(index, kick_trigger, 0, 2);
  2915. goto err;
  2916. }
  2917. if (!mtk_crtc_is_frame_trigger_mode(&mtk_crtc->base)) {
  2918. DDPINFO("%s:%d, not in trigger mode\n", __func__, __LINE__);
  2919. CRTC_MMP_MARK(index, kick_trigger, 0, 3);
  2920. goto err;
  2921. }
  2922. panel_ext = mtk_crtc->panel_ext;
  2923. mtk_state = to_mtk_crtc_state(crtc->state);
  2924. if (mtk_crtc_is_frame_trigger_mode(crtc) &&
  2925. mtk_state->prop_val[CRTC_PROP_DOZE_ACTIVE] &&
  2926. panel_ext && panel_ext->params->doze_delay > 1){
  2927. DDPINFO("%s:%d, doze not to trigger\n", __func__, __LINE__);
  2928. goto err;
  2929. }
  2930. if (delay) {
  2931. /* implicit way make sure wait queue was initiated */
  2932. if (unlikely(&mtk_crtc->trigger_delay_task == NULL)) {
  2933. CRTC_MMP_MARK(index, kick_trigger, 0, 4);
  2934. goto err;
  2935. }
  2936. atomic_set(&mtk_crtc->trig_delay_act, 1);
  2937. wake_up_interruptible(&mtk_crtc->trigger_delay);
  2938. } else {
  2939. /* implicit way make sure wait queue was initiated */
  2940. if (unlikely(&mtk_crtc->trigger_event_task == NULL)) {
  2941. CRTC_MMP_MARK(index, kick_trigger, 0, 5);
  2942. goto err;
  2943. }
  2944. atomic_set(&mtk_crtc->trig_event_act, 1);
  2945. wake_up_interruptible(&mtk_crtc->trigger_event);
  2946. }
  2947. err:
  2948. CRTC_MMP_EVENT_END(index, kick_trigger, 0, 0);
  2949. if (need_lock)
  2950. DDP_MUTEX_UNLOCK(&mtk_crtc->lock, __func__, __LINE__);
  2951. }
  2952. void mtk_crtc_config_default_path(struct mtk_drm_crtc *mtk_crtc)
  2953. {
  2954. int i, j;
  2955. struct drm_crtc *crtc = &mtk_crtc->base;
  2956. struct mtk_drm_private *priv = crtc->dev->dev_private;
  2957. struct cmdq_pkt *cmdq_handle;
  2958. struct mtk_ddp_config cfg;
  2959. struct mtk_ddp_comp *comp;
  2960. struct mtk_ddp_comp *output_comp;
  2961. output_comp = mtk_ddp_comp_request_output(mtk_crtc);
  2962. cfg.w = crtc->state->adjusted_mode.hdisplay;
  2963. cfg.h = crtc->state->adjusted_mode.vdisplay;
  2964. if (output_comp && drm_crtc_index(crtc) == 0) {
  2965. cfg.w = mtk_ddp_comp_io_cmd(output_comp, NULL,
  2966. DSI_GET_VIRTUAL_WIDTH, NULL);
  2967. cfg.h = mtk_ddp_comp_io_cmd(output_comp, NULL,
  2968. DSI_GET_VIRTUAL_HEIGH, NULL);
  2969. }
  2970. if (mtk_crtc->panel_ext && mtk_crtc->panel_ext->params &&
  2971. mtk_crtc->panel_ext->params->dyn_fps.switch_en == 1
  2972. && mtk_crtc->panel_ext->params->dyn_fps.vact_timing_fps != 0)
  2973. cfg.vrefresh =
  2974. mtk_crtc->panel_ext->params->dyn_fps.vact_timing_fps;
  2975. else
  2976. cfg.vrefresh = crtc->state->adjusted_mode.vrefresh;
  2977. cfg.bpc = mtk_crtc->bpc;
  2978. cfg.p_golden_setting_context = __get_golden_setting_context(mtk_crtc);
  2979. mtk_crtc_pkt_create(&cmdq_handle, &mtk_crtc->base,
  2980. mtk_crtc->gce_obj.client[CLIENT_CFG]);
  2981. for_each_comp_in_cur_crtc_path(comp, mtk_crtc, i, j) {
  2982. mtk_ddp_comp_config(comp, &cfg, cmdq_handle);
  2983. mtk_ddp_comp_start(comp, cmdq_handle);
  2984. if (!mtk_drm_helper_get_opt(
  2985. priv->helper_opt,
  2986. MTK_DRM_OPT_USE_PQ))
  2987. mtk_ddp_comp_bypass(comp, cmdq_handle);
  2988. }
  2989. if (mtk_crtc->is_dual_pipe) {
  2990. DDPFUNC();
  2991. for_each_comp_in_dual_pipe(comp, mtk_crtc, i, j) {
  2992. mtk_ddp_comp_config(comp, &cfg, cmdq_handle);
  2993. mtk_ddp_comp_start(comp, cmdq_handle);
  2994. }
  2995. }
  2996. /* Althought some of the m4u port may be enabled in LK stage.
  2997. * To make sure the driver independent, we still enable all the
  2998. * componets port here.
  2999. */
  3000. mtk_crtc_enable_iommu(mtk_crtc, cmdq_handle);
  3001. cmdq_pkt_flush(cmdq_handle);
  3002. cmdq_pkt_destroy(cmdq_handle);
  3003. }
  3004. static void mtk_crtc_all_layer_off(struct mtk_drm_crtc *mtk_crtc,
  3005. struct cmdq_pkt *cmdq_handle)
  3006. {
  3007. int i, j, keep_first_layer;
  3008. struct mtk_ddp_comp *comp;
  3009. keep_first_layer = true;
  3010. for_each_comp_in_cur_crtc_path(comp, mtk_crtc, i, j)
  3011. mtk_ddp_comp_io_cmd(comp, cmdq_handle,
  3012. OVL_ALL_LAYER_OFF, &keep_first_layer);
  3013. }
  3014. void mtk_crtc_stop_ddp(struct mtk_drm_crtc *mtk_crtc,
  3015. struct cmdq_pkt *cmdq_handle)
  3016. {
  3017. int i, j;
  3018. struct mtk_ddp_comp *comp;
  3019. /* If VDO mode, stop DSI mode first */
  3020. if (!mtk_crtc_is_frame_trigger_mode(&mtk_crtc->base) &&
  3021. mtk_crtc_is_connector_enable(mtk_crtc)) {
  3022. for_each_comp_in_cur_crtc_path(comp, mtk_crtc, i, j)
  3023. mtk_ddp_comp_io_cmd(comp, cmdq_handle,
  3024. DSI_STOP_VDO_MODE, NULL);
  3025. }
  3026. for_each_comp_in_cur_crtc_path(comp, mtk_crtc, i, j)
  3027. mtk_ddp_comp_stop(comp, cmdq_handle);
  3028. if (mtk_crtc->is_dual_pipe) {
  3029. for_each_comp_in_dual_pipe(comp, mtk_crtc, i, j)
  3030. mtk_ddp_comp_stop(comp, cmdq_handle);
  3031. }
  3032. }
  3033. /* Stop trig loop and stop all modules in this CRTC */
  3034. void mtk_crtc_stop(struct mtk_drm_crtc *mtk_crtc, bool need_wait)
  3035. {
  3036. struct cmdq_pkt *cmdq_handle;
  3037. struct mtk_ddp_comp *comp;
  3038. int i, j;
  3039. unsigned int crtc_id = drm_crtc_index(&mtk_crtc->base);
  3040. struct drm_crtc *crtc = &mtk_crtc->base;
  3041. DDPINFO("%s:%d +\n", __func__, __LINE__);
  3042. /* 0. Waiting CLIENT_DSI_CFG thread done */
  3043. if (crtc_id == 0) {
  3044. mtk_crtc_pkt_create(&cmdq_handle, &mtk_crtc->base,
  3045. mtk_crtc->gce_obj.client[CLIENT_DSI_CFG]);
  3046. cmdq_pkt_flush(cmdq_handle);
  3047. cmdq_pkt_destroy(cmdq_handle);
  3048. }
  3049. mtk_crtc_pkt_create(&cmdq_handle, &mtk_crtc->base,
  3050. mtk_crtc->gce_obj.client[CLIENT_CFG]);
  3051. if (!need_wait)
  3052. goto skip;
  3053. if (crtc_id == 2) {
  3054. int gce_event =
  3055. get_path_wait_event(mtk_crtc, mtk_crtc->ddp_mode);
  3056. if (gce_event > 0)
  3057. cmdq_pkt_wait_no_clear(cmdq_handle, gce_event);
  3058. } else if (mtk_crtc_is_frame_trigger_mode(&mtk_crtc->base)) {
  3059. /* 1. wait stream eof & clear tocken */
  3060. /* clear eof token to prevent any config after this command */
  3061. cmdq_pkt_wfe(cmdq_handle,
  3062. mtk_crtc->gce_obj.event[EVENT_STREAM_EOF]);
  3063. /* clear dirty token to prevent trigger loop start */
  3064. cmdq_pkt_clear_event(
  3065. cmdq_handle,
  3066. mtk_crtc->gce_obj.event[EVENT_STREAM_BLOCK]);
  3067. } else if (mtk_crtc_is_connector_enable(mtk_crtc)) {
  3068. /* In vdo mode, DSI would be stop when disable connector
  3069. * Do not wait frame done in this case.
  3070. */
  3071. cmdq_pkt_wfe(cmdq_handle,
  3072. mtk_crtc->gce_obj.event[EVENT_VDO_EOF]);
  3073. }
  3074. skip:
  3075. /* 2. stop all modules in this CRTC */
  3076. mtk_crtc_stop_ddp(mtk_crtc, cmdq_handle);
  3077. /* 3. Reset QOS BW after CRTC stop */
  3078. for_each_comp_in_cur_crtc_path(comp, mtk_crtc, i, j)
  3079. mtk_ddp_comp_io_cmd(comp, cmdq_handle,
  3080. PMQOS_UPDATE_BW, NULL);
  3081. cmdq_pkt_flush(cmdq_handle);
  3082. cmdq_pkt_destroy(cmdq_handle);
  3083. /* 4. Set QOS BW to 0 */
  3084. for_each_comp_in_cur_crtc_path(comp, mtk_crtc, i, j)
  3085. mtk_ddp_comp_io_cmd(comp, NULL, PMQOS_SET_BW, NULL);
  3086. /* 5. Set HRT BW to 0 */
  3087. #ifdef MTK_FB_MMDVFS_SUPPORT
  3088. if (drm_crtc_index(crtc) == 0)
  3089. mtk_disp_set_hrt_bw(mtk_crtc, 0);
  3090. #endif
  3091. /* 6. stop trig loop */
  3092. if (mtk_crtc_with_trigger_loop(crtc)) {
  3093. mtk_crtc_stop_trig_loop(crtc);
  3094. #if defined(CONFIG_MACH_MT6873) || defined(CONFIG_MACH_MT6853) || \
  3095. defined(CONFIG_MACH_MT6833)
  3096. if (mtk_crtc_with_sodi_loop(crtc) &&
  3097. (!mtk_crtc_is_frame_trigger_mode(crtc)))
  3098. mtk_crtc_stop_sodi_loop(crtc);
  3099. #endif
  3100. }
  3101. DDPINFO("%s:%d -\n", __func__, __LINE__);
  3102. }
  3103. /* TODO: how to remove add-on module? */
  3104. void mtk_crtc_disconnect_default_path(struct mtk_drm_crtc *mtk_crtc)
  3105. {
  3106. int i, j;
  3107. struct drm_crtc *crtc = &mtk_crtc->base;
  3108. struct mtk_ddp_comp *comp;
  3109. struct mtk_ddp_comp **ddp_comp;
  3110. /* if VDO mode, disable mutex by CPU here */
  3111. if (!mtk_crtc_is_frame_trigger_mode(crtc))
  3112. mtk_disp_mutex_disable(mtk_crtc->mutex[0]);
  3113. for_each_comp_in_crtc_path_bound(comp, mtk_crtc, i, j, 1) {
  3114. ddp_comp = mtk_crtc->ddp_ctx[mtk_crtc->ddp_mode].ddp_comp[i];
  3115. mtk_ddp_remove_comp_from_path(
  3116. mtk_crtc->config_regs, mtk_crtc->mmsys_reg_data,
  3117. ddp_comp[j]->id, ddp_comp[j + 1]->id);
  3118. }
  3119. if (mtk_crtc_is_dc_mode(crtc)) {
  3120. for_each_comp_in_crtc_target_path(comp, mtk_crtc, i,
  3121. DDP_FIRST_PATH)
  3122. mtk_disp_mutex_remove_comp(mtk_crtc->mutex[1],
  3123. comp->id);
  3124. for_each_comp_in_crtc_target_path(comp, mtk_crtc, i,
  3125. DDP_SECOND_PATH)
  3126. mtk_disp_mutex_remove_comp(mtk_crtc->mutex[0],
  3127. comp->id);
  3128. } else {
  3129. for_each_comp_in_crtc_target_path(comp, mtk_crtc, i,
  3130. DDP_FIRST_PATH)
  3131. mtk_disp_mutex_remove_comp(mtk_crtc->mutex[0],
  3132. comp->id);
  3133. }
  3134. if (mtk_crtc->is_dual_pipe) {
  3135. for_each_comp_in_dual_pipe(comp, mtk_crtc, i, j)
  3136. mtk_disp_mutex_remove_comp(mtk_crtc->mutex[0],
  3137. comp->id);
  3138. }
  3139. }
  3140. void mtk_drm_crtc_enable(struct drm_crtc *crtc)
  3141. {
  3142. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  3143. struct mtk_crtc_state *mtk_state = to_mtk_crtc_state(crtc->state);
  3144. unsigned int crtc_id = drm_crtc_index(crtc);
  3145. struct cmdq_client *client;
  3146. struct mtk_ddp_comp *comp;
  3147. int i, j;
  3148. struct mtk_ddp_comp *output_comp = NULL;
  3149. int en = 1;
  3150. output_comp = mtk_ddp_comp_request_output(mtk_crtc);
  3151. if (output_comp)
  3152. mtk_ddp_comp_io_cmd(output_comp, NULL, SET_MMCLK_BY_DATARATE,
  3153. &en);
  3154. CRTC_MMP_EVENT_START(crtc_id, enable,
  3155. mtk_crtc->enabled, 0);
  3156. if (mtk_crtc->enabled) {
  3157. CRTC_MMP_MARK(crtc_id, enable, 0, 0);
  3158. DDPINFO("crtc%d skip %s\n", crtc_id, __func__);
  3159. goto end;
  3160. } else if (mtk_crtc->ddp_mode == DDP_NO_USE) {
  3161. CRTC_MMP_MARK(crtc_id, enable, 0, 1);
  3162. DDPINFO("crtc%d skip %s, ddp_mode: NO_USE\n", crtc_id,
  3163. __func__);
  3164. goto end;
  3165. }
  3166. DDPINFO("crtc%d do %s\n", crtc_id, __func__);
  3167. CRTC_MMP_MARK(crtc_id, enable, 1, 0);
  3168. /*for dual pipe*/
  3169. mtk_crtc_prepare_dual_pipe(mtk_crtc);
  3170. /* attach the crtc to each componet */
  3171. mtk_crtc_attach_ddp_comp(crtc, mtk_crtc->ddp_mode, true);
  3172. #ifndef CONFIG_FPGA_EARLY_PORTING
  3173. /* 1. power on mtcmos */
  3174. mtk_drm_top_clk_prepare_enable(crtc->dev);
  3175. /* 2. prepare modules would be used in this CRTC */
  3176. mtk_crtc_ddp_prepare(mtk_crtc);
  3177. #endif
  3178. /* 3. power on cmdq client */
  3179. if (crtc_id == 2) {
  3180. client = mtk_crtc->gce_obj.client[CLIENT_CFG];
  3181. cmdq_mbox_enable(client->chan);
  3182. CRTC_MMP_MARK(crtc_id, enable, 1, 1);
  3183. }
  3184. /* 4. start trigger loop first to keep gce alive */
  3185. if (mtk_crtc_with_trigger_loop(crtc)) {
  3186. #if defined(CONFIG_MACH_MT6873) || defined(CONFIG_MACH_MT6853) || \
  3187. defined(CONFIG_MACH_MT6833)
  3188. if (mtk_crtc_with_sodi_loop(crtc) &&
  3189. (!mtk_crtc_is_frame_trigger_mode(crtc)))
  3190. mtk_crtc_start_sodi_loop(crtc);
  3191. #endif
  3192. mtk_crtc_start_trig_loop(crtc);
  3193. }
  3194. if (mtk_crtc_is_mem_mode(crtc) || mtk_crtc_is_dc_mode(crtc)) {
  3195. struct golden_setting_context *ctx =
  3196. __get_golden_setting_context(mtk_crtc);
  3197. struct cmdq_pkt *cmdq_handle;
  3198. int gce_event =
  3199. get_path_wait_event(mtk_crtc, mtk_crtc->ddp_mode);
  3200. ctx->is_dc = 1;
  3201. cmdq_handle =
  3202. cmdq_pkt_create(mtk_crtc->gce_obj.client[CLIENT_CFG]);
  3203. if (gce_event > 0)
  3204. cmdq_pkt_set_event(cmdq_handle, gce_event);
  3205. cmdq_pkt_flush(cmdq_handle);
  3206. cmdq_pkt_destroy(cmdq_handle);
  3207. }
  3208. CRTC_MMP_MARK(crtc_id, enable, 1, 2);
  3209. /* 5. connect path */
  3210. mtk_crtc_connect_default_path(mtk_crtc);
  3211. if (!crtc_id)
  3212. mtk_crtc->qos_ctx->last_hrt_req = 0;
  3213. /* 6. config ddp engine */
  3214. mtk_crtc_config_default_path(mtk_crtc);
  3215. CRTC_MMP_MARK(crtc_id, enable, 1, 3);
  3216. /* 7. disconnect addon module and config */
  3217. mtk_crtc_connect_addon_module(crtc);
  3218. /* 8. restore OVL setting */
  3219. mtk_crtc_restore_plane_setting(mtk_crtc);
  3220. /* 9. Set QOS BW */
  3221. for_each_comp_in_cur_crtc_path(comp, mtk_crtc, i, j)
  3222. mtk_ddp_comp_io_cmd(comp, NULL, PMQOS_SET_BW, NULL);
  3223. /* 10. set dirty for cmd mode */
  3224. if (mtk_crtc_is_frame_trigger_mode(crtc) &&
  3225. !mtk_state->prop_val[CRTC_PROP_DOZE_ACTIVE])
  3226. mtk_crtc_set_dirty(mtk_crtc);
  3227. /* 11. set vblank*/
  3228. drm_crtc_vblank_on(crtc);
  3229. #ifdef MTK_DRM_ESD_SUPPORT
  3230. /* 12. enable ESD check */
  3231. if (mtk_drm_lcm_is_connect())
  3232. mtk_disp_esd_check_switch(crtc, true);
  3233. #endif
  3234. /* 13. enable fake vsync if need*/
  3235. mtk_drm_fake_vsync_switch(crtc, true);
  3236. /* 14. set CRTC SW status */
  3237. mtk_crtc_set_status(crtc, true);
  3238. end:
  3239. CRTC_MMP_EVENT_END(crtc_id, enable,
  3240. mtk_crtc->enabled, 0);
  3241. }
  3242. static void mtk_drm_crtc_wk_lock(struct drm_crtc *crtc, bool get,
  3243. const char *func, int line)
  3244. {
  3245. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  3246. DDPMSG("CRTC%d %s wakelock %s %d\n",
  3247. drm_crtc_index(crtc), (get ? "hold" : "release"),
  3248. func, line);
  3249. if (get)
  3250. __pm_stay_awake(&mtk_crtc->wk_lock);
  3251. else
  3252. __pm_relax(&mtk_crtc->wk_lock);
  3253. }
  3254. unsigned int mtk_drm_dump_wk_lock(
  3255. struct mtk_drm_private *priv, char *stringbuf, int buf_len)
  3256. {
  3257. unsigned int len = 0;
  3258. int i = 0;
  3259. struct drm_crtc *crtc;
  3260. struct mtk_drm_crtc *mtk_crtc;
  3261. len += scnprintf(stringbuf + len, buf_len - len,
  3262. "========== wakelock Info ==========\n");
  3263. for (i = 0; i < 3; i++) {
  3264. crtc = priv->crtc[i];
  3265. if (!crtc)
  3266. continue;
  3267. mtk_crtc = to_mtk_crtc(crtc);
  3268. len += scnprintf(stringbuf + len, buf_len - len,
  3269. "CRTC%d wk active:%d; ", i,
  3270. mtk_crtc->wk_lock.active);
  3271. }
  3272. len += scnprintf(stringbuf + len, buf_len - len, "\n\n");
  3273. return len;
  3274. }
  3275. void mtk_drm_crtc_atomic_resume(struct drm_crtc *crtc,
  3276. struct drm_crtc_state *old_crtc_state)
  3277. {
  3278. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  3279. int index = drm_crtc_index(crtc);
  3280. struct mtk_drm_private *priv = crtc->dev->dev_private;
  3281. struct mtk_drm_crtc *mtk_crtc0 = to_mtk_crtc(priv->crtc[0]);
  3282. /* When open VDS path switch feature, After VDS created,
  3283. * VDS will call setcrtc, So atomic commit will be called,
  3284. * but OVL0_2L is in use by main disp, So we need to skip
  3285. * this action.
  3286. */
  3287. if (mtk_drm_helper_get_opt(priv->helper_opt,
  3288. MTK_DRM_OPT_VDS_PATH_SWITCH) && (index == 2)) {
  3289. if (atomic_read(&mtk_crtc0->already_config) &&
  3290. (!priv->vds_path_switch_done)) {
  3291. DDPMSG("Switch vds: VDS need skip first crtc enable\n");
  3292. return;
  3293. } else if (!atomic_read(&mtk_crtc0->already_config)) {
  3294. DDPMSG("Switch vds: VDS no need skip as crtc0 disable\n");
  3295. priv->vds_path_enable = 1;
  3296. }
  3297. }
  3298. CRTC_MMP_EVENT_START(index, resume,
  3299. mtk_crtc->enabled, index);
  3300. /* hold wakelock */
  3301. mtk_drm_crtc_wk_lock(crtc, 1, __func__, __LINE__);
  3302. mtk_drm_crtc_enable(crtc);
  3303. CRTC_MMP_EVENT_END(index, resume,
  3304. mtk_crtc->enabled, 0);
  3305. }
  3306. bool mtk_crtc_with_sub_path(struct drm_crtc *crtc, unsigned int ddp_mode);
  3307. #ifdef CONFIG_MTK_ROUND_CORNER_SUPPORT
  3308. void mtk_crtc_config_round_corner(struct drm_crtc *crtc,
  3309. struct cmdq_pkt *handle)
  3310. {
  3311. struct mtk_ddp_config cfg;
  3312. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  3313. struct mtk_ddp_comp *comp;
  3314. int i, j;
  3315. int cur_path_idx;
  3316. cfg.w = crtc->mode.hdisplay;
  3317. cfg.h = crtc->mode.vdisplay;
  3318. for_each_comp_in_cur_crtc_path(
  3319. comp, mtk_crtc, i, j)
  3320. if (comp->id == DDP_COMPONENT_POSTMASK0 ||
  3321. comp->id == DDP_COMPONENT_POSTMASK1) {
  3322. if (mtk_crtc_with_sub_path(crtc, mtk_crtc->ddp_mode))
  3323. cur_path_idx = DDP_SECOND_PATH;
  3324. else
  3325. cur_path_idx = DDP_FIRST_PATH;
  3326. mtk_crtc_wait_frame_done(mtk_crtc,
  3327. handle, cur_path_idx, 0);
  3328. mtk_ddp_comp_config(comp, &cfg, handle);
  3329. break;
  3330. }
  3331. }
  3332. void mtk_crtc_load_round_corner_pattern(struct drm_crtc *crtc,
  3333. struct cmdq_pkt *handle)
  3334. {
  3335. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  3336. struct mtk_panel_params *panel_ext = mtk_drm_get_lcm_ext_params(crtc);
  3337. struct mtk_drm_gem_obj *gem;
  3338. if (panel_ext && panel_ext->round_corner_en) {
  3339. gem = mtk_drm_gem_create(
  3340. crtc->dev, panel_ext->corner_pattern_tp_size, true);
  3341. if (!gem) {
  3342. DDPPR_ERR("%s gem create fail\n", __func__);
  3343. return;
  3344. }
  3345. memcpy(gem->kvaddr, panel_ext->corner_pattern_lt_addr,
  3346. panel_ext->corner_pattern_tp_size);
  3347. mtk_crtc->round_corner_gem = gem;
  3348. mtk_crtc_config_round_corner(crtc, handle);
  3349. }
  3350. }
  3351. #endif
  3352. struct drm_display_mode *mtk_drm_crtc_avail_disp_mode(struct drm_crtc *crtc,
  3353. unsigned int idx)
  3354. {
  3355. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  3356. /* If not crtc0, use crtc0 instead. TODO: need to reconsidered for
  3357. * secondary display, i.e: DP, HDMI
  3358. */
  3359. if (drm_crtc_index(crtc) != 0) {
  3360. struct drm_crtc *crtc0;
  3361. DDPPR_ERR("%s no support CRTC%u", __func__,
  3362. drm_crtc_index(crtc));
  3363. drm_for_each_crtc(crtc0, crtc->dev) {
  3364. if (drm_crtc_index(crtc0) == 0)
  3365. break;
  3366. }
  3367. mtk_crtc = to_mtk_crtc(crtc0);
  3368. }
  3369. if (idx >= mtk_crtc->avail_modes_num) {
  3370. DDPPR_ERR("%s idx:%u exceed avail_num:%u", __func__,
  3371. idx, mtk_crtc->avail_modes_num);
  3372. idx = 0;
  3373. }
  3374. return &mtk_crtc->avail_modes[idx];
  3375. }
  3376. static void mtk_drm_crtc_init_para(struct drm_crtc *crtc)
  3377. {
  3378. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  3379. unsigned int crtc_id = drm_crtc_index(&mtk_crtc->base);
  3380. struct mtk_ddp_comp *comp;
  3381. struct drm_display_mode *timing = NULL;
  3382. int en = 1;
  3383. comp = mtk_ddp_comp_request_output(mtk_crtc);
  3384. if (comp == NULL)
  3385. return;
  3386. mtk_ddp_comp_io_cmd(comp, NULL, DSI_GET_TIMING, &timing);
  3387. if (timing == NULL)
  3388. return;
  3389. crtc->mode.hdisplay = timing->hdisplay;
  3390. crtc->mode.vdisplay = timing->vdisplay;
  3391. crtc->state->adjusted_mode.clock = timing->clock;
  3392. crtc->state->adjusted_mode.hdisplay = timing->hdisplay;
  3393. crtc->state->adjusted_mode.hsync_start = timing->hsync_start;
  3394. crtc->state->adjusted_mode.hsync_end = timing->hsync_end;
  3395. crtc->state->adjusted_mode.htotal = timing->htotal;
  3396. crtc->state->adjusted_mode.hskew = timing->hskew;
  3397. crtc->state->adjusted_mode.vdisplay = timing->vdisplay;
  3398. crtc->state->adjusted_mode.vsync_start = timing->vsync_start;
  3399. crtc->state->adjusted_mode.vsync_end = timing->vsync_end;
  3400. crtc->state->adjusted_mode.vtotal = timing->vtotal;
  3401. crtc->state->adjusted_mode.vscan = timing->vscan;
  3402. crtc->state->adjusted_mode.vrefresh = timing->vrefresh;
  3403. drm_invoke_fps_chg_callbacks(timing->vrefresh);
  3404. mtk_crtc_attach_ddp_comp(crtc, mtk_crtc->ddp_mode, true);
  3405. /* backup display context */
  3406. if (crtc_id == 0) {
  3407. pgc->mode = *timing;
  3408. DDPMSG("width:%d, height:%d\n", pgc->mode.hdisplay,
  3409. pgc->mode.vdisplay);
  3410. }
  3411. /* store display mode for crtc0 only */
  3412. if (comp && drm_crtc_index(&mtk_crtc->base) == 0) {
  3413. mtk_ddp_comp_io_cmd(comp, NULL,
  3414. DSI_SET_CRTC_AVAIL_MODES, mtk_crtc);
  3415. mtk_ddp_comp_io_cmd(comp, NULL, SET_MMCLK_BY_DATARATE, &en);
  3416. /*need enable hrt_bw for pan display*/
  3417. #ifdef MTK_FB_MMDVFS_SUPPORT
  3418. mtk_drm_pan_disp_set_hrt_bw(crtc, __func__);
  3419. #endif
  3420. } else {
  3421. mtk_crtc->avail_modes_num = 0;
  3422. mtk_crtc->avail_modes = NULL;
  3423. }
  3424. }
  3425. void mtk_crtc_first_enable_ddp_config(struct mtk_drm_crtc *mtk_crtc)
  3426. {
  3427. struct drm_crtc *crtc = &mtk_crtc->base;
  3428. struct cmdq_pkt *cmdq_handle;
  3429. struct mtk_ddp_comp *comp;
  3430. struct mtk_ddp_config cfg = {0};
  3431. int i, j;
  3432. struct mtk_ddp_comp *output_comp;
  3433. output_comp = mtk_ddp_comp_request_output(mtk_crtc);
  3434. cfg.w = crtc->mode.hdisplay;
  3435. cfg.h = crtc->mode.vdisplay;
  3436. if (output_comp && drm_crtc_index(crtc) == 0) {
  3437. cfg.w = mtk_ddp_comp_io_cmd(output_comp, NULL,
  3438. DSI_GET_VIRTUAL_WIDTH, NULL);
  3439. cfg.h = mtk_ddp_comp_io_cmd(output_comp, NULL,
  3440. DSI_GET_VIRTUAL_HEIGH, NULL);
  3441. }
  3442. cfg.p_golden_setting_context =
  3443. __get_golden_setting_context(mtk_crtc);
  3444. mtk_crtc_pkt_create(&cmdq_handle, &mtk_crtc->base,
  3445. mtk_crtc->gce_obj.client[CLIENT_CFG]);
  3446. cmdq_pkt_clear_event(cmdq_handle,
  3447. mtk_crtc->gce_obj.event[EVENT_VDO_EOF]);
  3448. mtk_crtc_wait_frame_done(mtk_crtc, cmdq_handle, DDP_FIRST_PATH, 0);
  3449. /*1. Show LK logo only */
  3450. mtk_crtc_all_layer_off(mtk_crtc, cmdq_handle);
  3451. /*2. Load Round Corner */
  3452. #ifdef CONFIG_MTK_ROUND_CORNER_SUPPORT
  3453. mtk_crtc_load_round_corner_pattern(&mtk_crtc->base, cmdq_handle);
  3454. #endif
  3455. /*3. Enable M4U port and replace OVL address to mva */
  3456. mtk_crtc_enable_iommu_runtime(mtk_crtc, cmdq_handle);
  3457. /*4. Enable Frame done IRQ & process first config */
  3458. for_each_comp_in_cur_crtc_path(comp, mtk_crtc, i, j) {
  3459. mtk_ddp_comp_first_cfg(comp, &cfg, cmdq_handle);
  3460. mtk_ddp_comp_io_cmd(comp, cmdq_handle, IRQ_LEVEL_ALL, NULL);
  3461. mtk_ddp_comp_io_cmd(comp, cmdq_handle,
  3462. MTK_IO_CMD_RDMA_GOLDEN_SETTING, &cfg);
  3463. }
  3464. cmdq_pkt_flush(cmdq_handle);
  3465. cmdq_pkt_destroy(cmdq_handle);
  3466. if (mtk_crtc_is_frame_trigger_mode(&mtk_crtc->base))
  3467. mtk_crtc_set_dirty(mtk_crtc);
  3468. }
  3469. void mtk_drm_crtc_first_enable(struct drm_crtc *crtc)
  3470. {
  3471. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  3472. unsigned int crtc_id = drm_crtc_index(&mtk_crtc->base);
  3473. #ifndef CONFIG_FPGA_EARLY_PORTING
  3474. struct mtk_drm_private *priv = crtc->dev->dev_private;
  3475. #endif
  3476. mtk_drm_crtc_init_para(crtc);
  3477. if (mtk_crtc->enabled) {
  3478. DDPINFO("crtc%d skip %s\n", crtc_id, __func__);
  3479. return;
  3480. }
  3481. DDPINFO("crtc%d do %s\n", crtc_id, __func__);
  3482. /* 1. hold wakelock */
  3483. mtk_drm_crtc_wk_lock(crtc, 1, __func__, __LINE__);
  3484. /* 2. start trigger loop first to keep gce alive */
  3485. if (mtk_crtc_with_trigger_loop(crtc)) {
  3486. #if defined(CONFIG_MACH_MT6873) || defined(CONFIG_MACH_MT6853) || \
  3487. defined(CONFIG_MACH_MT6833)
  3488. if (mtk_crtc_with_sodi_loop(crtc) &&
  3489. (!mtk_crtc_is_frame_trigger_mode(crtc)))
  3490. mtk_crtc_start_sodi_loop(crtc);
  3491. #endif
  3492. mtk_crtc_start_trig_loop(crtc);
  3493. }
  3494. /* 3. Regsister configuration */
  3495. mtk_crtc_first_enable_ddp_config(mtk_crtc);
  3496. #ifndef CONFIG_FPGA_EARLY_PORTING
  3497. /* 4. power on mtcmos */
  3498. mtk_drm_top_clk_prepare_enable(crtc->dev);
  3499. /* 5. prepare modules would be used in this CRTC */
  3500. mtk_crtc_ddp_prepare(mtk_crtc);
  3501. /* 6. sodi config */
  3502. if (priv->data->sodi_config) {
  3503. struct mtk_ddp_comp *comp;
  3504. int i, j;
  3505. bool en = 1;
  3506. for_each_comp_in_cur_crtc_path(comp, mtk_crtc, i, j)
  3507. priv->data->sodi_config(crtc->dev, comp->id, NULL, &en);
  3508. }
  3509. #endif
  3510. /* 7. set vblank*/
  3511. drm_crtc_vblank_on(crtc);
  3512. /* 8. set CRTC SW status */
  3513. mtk_crtc_set_status(crtc, true);
  3514. /* 9. power off mtcmos*/
  3515. /* Because of align lk hw power status,
  3516. * we power on mtcmos at the beginning of the display initialization.
  3517. * We power off mtcmos at the end of the display initialization.
  3518. * Here we only decrease ref count, the power will hold on.
  3519. */
  3520. mtk_drm_top_clk_disable_unprepare(crtc->dev);
  3521. }
  3522. void mtk_drm_crtc_disable(struct drm_crtc *crtc, bool need_wait)
  3523. {
  3524. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  3525. unsigned int crtc_id = drm_crtc_index(&mtk_crtc->base);
  3526. struct mtk_ddp_comp *comp = NULL;
  3527. struct mtk_ddp_comp *output_comp = NULL;
  3528. struct cmdq_client *client;
  3529. int en = 0;
  3530. CRTC_MMP_EVENT_START(crtc_id, disable,
  3531. mtk_crtc->enabled, 0);
  3532. output_comp = mtk_ddp_comp_request_output(mtk_crtc);
  3533. if (output_comp)
  3534. mtk_ddp_comp_io_cmd(output_comp, NULL, SET_MMCLK_BY_DATARATE,
  3535. &en);
  3536. if (!mtk_crtc->enabled) {
  3537. CRTC_MMP_MARK(crtc_id, disable, 0, 0);
  3538. DDPINFO("crtc%d skip %s\n", crtc_id, __func__);
  3539. goto end;
  3540. } else if (mtk_crtc->ddp_mode == DDP_NO_USE) {
  3541. CRTC_MMP_MARK(crtc_id, disable, 0, 1);
  3542. DDPINFO("crtc%d skip %s, ddp_mode: NO_USE\n", crtc_id,
  3543. __func__);
  3544. goto end;
  3545. }
  3546. DDPINFO("%s:%d crtc%d+\n", __func__, __LINE__, crtc_id);
  3547. CRTC_MMP_MARK(crtc_id, disable, 1, 0);
  3548. /* 1. kick idle */
  3549. mtk_drm_idlemgr_kick(__func__, crtc, 0);
  3550. /* 2. disable fake vsync if need */
  3551. mtk_drm_fake_vsync_switch(crtc, false);
  3552. /* 3. disable ESD check */
  3553. if (mtk_drm_lcm_is_connect())
  3554. mtk_disp_esd_check_switch(crtc, false);
  3555. /* 4. stop CRTC */
  3556. mtk_crtc_stop(mtk_crtc, need_wait);
  3557. CRTC_MMP_MARK(crtc_id, disable, 1, 1);
  3558. /* 5. disconnect addon module and recover config */
  3559. mtk_crtc_disconnect_addon_module(crtc);
  3560. /* 6. disconnect path */
  3561. mtk_crtc_disconnect_default_path(mtk_crtc);
  3562. /* 7. disable vblank */
  3563. drm_crtc_vblank_off(crtc);
  3564. /* 8. power off cmdq client */
  3565. if (crtc_id == 2) {
  3566. client = mtk_crtc->gce_obj.client[CLIENT_CFG];
  3567. cmdq_mbox_disable(client->chan);
  3568. CRTC_MMP_MARK(crtc_id, disable, 1, 2);
  3569. }
  3570. /* 9. power off all modules in this CRTC */
  3571. mtk_crtc_ddp_unprepare(mtk_crtc);
  3572. /* 10. power off MTCMOS*/
  3573. /* TODO: need to check how to unprepare MTCMOS */
  3574. mtk_drm_top_clk_disable_unprepare(crtc->dev);
  3575. /* Workaround: if CRTC2, reset wdma->fb to NULL to prevent CRTC2
  3576. * config wdma and cause KE
  3577. */
  3578. if (crtc_id == 2) {
  3579. comp = mtk_ddp_comp_find_by_id(crtc, DDP_COMPONENT_WDMA0);
  3580. if (!comp)
  3581. comp = mtk_ddp_comp_find_by_id(crtc,
  3582. DDP_COMPONENT_WDMA1);
  3583. if (comp)
  3584. comp->fb = NULL;
  3585. }
  3586. /* 11. set CRTC SW status */
  3587. mtk_crtc_set_status(crtc, false);
  3588. end:
  3589. CRTC_MMP_EVENT_END(crtc_id, disable,
  3590. mtk_crtc->enabled, 0);
  3591. DDPINFO("%s:%d -\n", __func__, __LINE__);
  3592. }
  3593. #ifdef MTK_DRM_FENCE_SUPPORT
  3594. static void mtk_drm_crtc_release_fence(struct drm_crtc *crtc)
  3595. {
  3596. struct mtk_drm_private *priv = crtc->dev->dev_private;
  3597. unsigned int id = drm_crtc_index(crtc), i;
  3598. int session_id = -1;
  3599. for (i = 0; i < MAX_SESSION_COUNT; i++) {
  3600. if (id + 1 == MTK_SESSION_TYPE(priv->session_id[i])) {
  3601. session_id = priv->session_id[i];
  3602. break;
  3603. }
  3604. }
  3605. if (session_id == -1) {
  3606. DDPPR_ERR("%s no session for CRTC%u\n", __func__, id);
  3607. return;
  3608. }
  3609. /* release input layer fence */
  3610. DDPMSG("CRTC%u release input fence\n", id);
  3611. for (i = 0; i < MTK_TIMELINE_OUTPUT_TIMELINE_ID; i++)
  3612. mtk_release_layer_fence(session_id, i);
  3613. /* release output fence for crtc2 */
  3614. if (id == 2) {
  3615. DDPMSG("CRTC%u release output fence\n", id);
  3616. mtk_release_layer_fence(session_id,
  3617. MTK_TIMELINE_OUTPUT_TIMELINE_ID);
  3618. }
  3619. /* release present fence */
  3620. if (MTK_SESSION_TYPE(session_id) == MTK_SESSION_PRIMARY ||
  3621. MTK_SESSION_TYPE(session_id) == MTK_SESSION_EXTERNAL)
  3622. mtk_drm_suspend_release_present_fence(crtc->dev->dev, id);
  3623. }
  3624. #endif
  3625. void mtk_drm_crtc_suspend(struct drm_crtc *crtc)
  3626. {
  3627. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  3628. int index = drm_crtc_index(crtc);
  3629. CRTC_MMP_EVENT_START(index, suspend,
  3630. mtk_crtc->enabled, 0);
  3631. mtk_drm_crtc_wait_blank(mtk_crtc);
  3632. /* disable engine secure state */
  3633. #if defined(CONFIG_MTK_SEC_VIDEO_PATH_SUPPORT)
  3634. if (index == 2 && mtk_crtc->sec_on) {
  3635. mtk_crtc_disable_secure_state(crtc);
  3636. mtk_crtc->sec_on = false;
  3637. }
  3638. #endif
  3639. mtk_drm_crtc_disable(crtc, true);
  3640. mtk_crtc_disable_plane_setting(mtk_crtc);
  3641. /* release all fence */
  3642. #ifdef MTK_DRM_FENCE_SUPPORT
  3643. mtk_drm_crtc_release_fence(crtc);
  3644. mtk_crtc_release_lye_idx(crtc);
  3645. #endif
  3646. atomic_set(&mtk_crtc->already_config, 0);
  3647. /* release wakelock */
  3648. mtk_drm_crtc_wk_lock(crtc, 0, __func__, __LINE__);
  3649. CRTC_MMP_EVENT_END(index, suspend,
  3650. mtk_crtc->enabled, 0);
  3651. }
  3652. #if defined(CONFIG_MTK_SEC_VIDEO_PATH_SUPPORT)
  3653. int mtk_crtc_check_out_sec(struct drm_crtc *crtc)
  3654. {
  3655. struct mtk_crtc_state *state = to_mtk_crtc_state(crtc->state);
  3656. int out_sec = 0;
  3657. if (state->prop_val[CRTC_PROP_OUTPUT_ENABLE]) {
  3658. /* Output buffer configuration for virtual display */
  3659. out_sec = mtk_drm_fb_is_secure(
  3660. mtk_drm_framebuffer_lookup(crtc->dev,
  3661. state->prop_val[CRTC_PROP_OUTPUT_FB_ID]));
  3662. DDPINFO("%s lookup wb fb:%u sec:%d\n", __func__,
  3663. state->prop_val[CRTC_PROP_OUTPUT_FB_ID], out_sec);
  3664. }
  3665. return out_sec;
  3666. }
  3667. static u64 mtk_crtc_secure_port_lookup(struct mtk_ddp_comp *comp)
  3668. {
  3669. u64 ret = 0;
  3670. if (!comp)
  3671. return ret;
  3672. switch (comp->id) {
  3673. case DDP_COMPONENT_WDMA0:
  3674. ret = 1LL << CMDQ_SEC_DISP_WDMA0;
  3675. break;
  3676. case DDP_COMPONENT_WDMA1:
  3677. ret = 1LL << CMDQ_SEC_DISP_WDMA1;
  3678. break;
  3679. default:
  3680. ret = 0;
  3681. }
  3682. return ret;
  3683. }
  3684. void mtk_crtc_disable_secure_state(struct drm_crtc *crtc)
  3685. {
  3686. struct cmdq_pkt *cmdq_handle;
  3687. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  3688. struct mtk_ddp_comp *comp = NULL;
  3689. u32 sec_disp_type, idx = drm_crtc_index(crtc);
  3690. u64 sec_disp_port;
  3691. comp = mtk_ddp_comp_request_output(mtk_crtc);
  3692. if (idx == 0) {
  3693. sec_disp_type =
  3694. CMDQ_SEC_DISP_PRIMARY_DISABLE_SECURE_PATH;
  3695. sec_disp_port = 0;
  3696. } else {
  3697. sec_disp_type =
  3698. CMDQ_SEC_DISP_SUB_DISABLE_SECURE_PATH;
  3699. sec_disp_port = (idx == 1) ? 0 :
  3700. mtk_crtc_secure_port_lookup(comp);
  3701. }
  3702. DDPINFO("%s+ crtc%d\n", __func__, drm_crtc_index(crtc));
  3703. mtk_crtc_pkt_create(&cmdq_handle, crtc,
  3704. mtk_crtc->gce_obj.client[CLIENT_SEC_CFG]);
  3705. /* Secure path only support DL mode, so we just wait
  3706. * the first path frame done here
  3707. */
  3708. mtk_crtc_wait_frame_done(mtk_crtc, cmdq_handle, DDP_FIRST_PATH, 0);
  3709. /* Disable secure path */
  3710. cmdq_sec_pkt_set_data(cmdq_handle,
  3711. 0,
  3712. sec_disp_port,
  3713. sec_disp_type,
  3714. CMDQ_METAEX_NONE);
  3715. cmdq_pkt_flush(cmdq_handle);
  3716. cmdq_pkt_destroy(cmdq_handle);
  3717. DDPINFO("%s-\n", __func__);
  3718. }
  3719. #endif
  3720. struct cmdq_pkt *mtk_crtc_gce_commit_begin(struct drm_crtc *crtc)
  3721. {
  3722. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  3723. struct cmdq_pkt *cmdq_handle;
  3724. if (mtk_crtc->sec_on)
  3725. mtk_crtc_pkt_create(&cmdq_handle, crtc,
  3726. mtk_crtc->gce_obj.client[CLIENT_SEC_CFG]);
  3727. else if (mtk_crtc_is_dc_mode(crtc))
  3728. mtk_crtc_pkt_create(&cmdq_handle, crtc,
  3729. mtk_crtc->gce_obj.client[CLIENT_SUB_CFG]);
  3730. else
  3731. mtk_crtc_pkt_create(&cmdq_handle, crtc,
  3732. mtk_crtc->gce_obj.client[CLIENT_CFG]);
  3733. mtk_crtc_wait_frame_done(mtk_crtc, cmdq_handle, DDP_FIRST_PATH, 0);
  3734. if (mtk_crtc->sec_on) {
  3735. #if defined(CONFIG_MTK_SEC_VIDEO_PATH_SUPPORT)
  3736. u32 sec_disp_type, idx = drm_crtc_index(crtc);
  3737. u64 sec_disp_port;
  3738. struct mtk_ddp_comp *comp = NULL;
  3739. comp = mtk_ddp_comp_request_output(mtk_crtc);
  3740. if (idx == 0) {
  3741. sec_disp_type = CMDQ_SEC_PRIMARY_DISP;
  3742. sec_disp_port = 0;
  3743. } else {
  3744. sec_disp_type = CMDQ_SEC_SUB_DISP;
  3745. sec_disp_port = (idx == 1) ? 0 :
  3746. mtk_crtc_secure_port_lookup(comp);
  3747. }
  3748. cmdq_sec_pkt_set_data(cmdq_handle, 0,
  3749. sec_disp_port, sec_disp_type,
  3750. CMDQ_METAEX_NONE);
  3751. #endif
  3752. DDPDBG("%s:%d crtc:0x%p, sec_on:%d +\n",
  3753. __func__, __LINE__,
  3754. crtc,
  3755. mtk_crtc->sec_on);
  3756. }
  3757. return cmdq_handle;
  3758. }
  3759. static void mtk_drm_crtc_atomic_begin(struct drm_crtc *crtc,
  3760. struct drm_crtc_state *old_crtc_state)
  3761. {
  3762. struct mtk_crtc_state *state = to_mtk_crtc_state(crtc->state);
  3763. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  3764. int index = drm_crtc_index(crtc);
  3765. struct mtk_ddp_comp *comp;
  3766. int i, j;
  3767. int crtc_idx = drm_crtc_index(crtc);
  3768. struct mtk_drm_private *priv = crtc->dev->dev_private;
  3769. /* When open VDS path switch feature, we will resume VDS crtc
  3770. * in it's second atomic commit, and the crtc will be resumed
  3771. * one time.
  3772. */
  3773. if (mtk_drm_helper_get_opt(priv->helper_opt,
  3774. MTK_DRM_OPT_VDS_PATH_SWITCH) && (crtc_idx == 2))
  3775. if (priv->vds_path_switch_done &&
  3776. !priv->vds_path_enable) {
  3777. DDPMSG("Switch vds: CRTC2 vds enable\n");
  3778. mtk_drm_crtc_atomic_resume(crtc, NULL);
  3779. priv->vds_path_enable = 1;
  3780. }
  3781. CRTC_MMP_EVENT_START(index, atomic_begin,
  3782. (unsigned long)mtk_crtc->event,
  3783. (unsigned long)state->base.event);
  3784. if (mtk_crtc->event && state->base.event)
  3785. DRM_ERROR("new event while there is still a pending event\n");
  3786. if (mtk_crtc->ddp_mode == DDP_NO_USE) {
  3787. CRTC_MMP_MARK(index, atomic_begin, 0, 0);
  3788. goto end;
  3789. }
  3790. mtk_drm_idlemgr_kick(__func__, crtc, 0);
  3791. if (state->base.event) {
  3792. state->base.event->pipe = index;
  3793. if (drm_crtc_vblank_get(crtc) != 0)
  3794. DDPAEE("%s:%d, invalid vblank:%d, crtc:%p\n",
  3795. __func__, __LINE__,
  3796. drm_crtc_vblank_get(crtc), crtc);
  3797. mtk_crtc->event = state->base.event;
  3798. state->base.event = NULL;
  3799. }
  3800. mtk_drm_trace_begin("mtk_drm_crtc_atomic:%d-%d",
  3801. crtc_idx, state->prop_val[CRTC_PROP_PRES_FENCE_IDX]);
  3802. state->cmdq_handle = mtk_crtc_gce_commit_begin(crtc);
  3803. #ifdef MTK_DRM_ADVANCE
  3804. mtk_crtc_update_ddp_state(crtc, old_crtc_state, state,
  3805. state->cmdq_handle);
  3806. #endif
  3807. /* reset BW */
  3808. for_each_comp_in_cur_crtc_path(comp, mtk_crtc, i, j) {
  3809. comp->qos_bw = 0;
  3810. comp->fbdc_bw = 0;
  3811. comp->hrt_bw = 0;
  3812. }
  3813. end:
  3814. CRTC_MMP_EVENT_END(index, atomic_begin,
  3815. (unsigned long)mtk_crtc->event,
  3816. (unsigned long)state->base.event);
  3817. }
  3818. static inline void mtk_drm_layer_dispatch_to_dual_pipe(
  3819. struct mtk_plane_state *plane_state,
  3820. struct mtk_plane_state *plane_state_l,
  3821. struct mtk_plane_state *plane_state_r,
  3822. unsigned int w)
  3823. {
  3824. memcpy(plane_state_l,
  3825. plane_state, sizeof(struct mtk_plane_state));
  3826. memcpy(plane_state_r,
  3827. plane_state, sizeof(struct mtk_plane_state));
  3828. /*left path*/
  3829. plane_state_l->pending.width = w/2 -
  3830. plane_state_l->pending.dst_x;
  3831. if (w/2 < plane_state->pending.dst_x)
  3832. plane_state_l->pending.enable = 0;
  3833. if (plane_state_l->pending.width > plane_state->pending.width)
  3834. plane_state_l->pending.width = plane_state->pending.width;
  3835. /*right path*/
  3836. plane_state_r->pending.width +=
  3837. plane_state_r->pending.dst_x - w/2;
  3838. plane_state_r->pending.dst_x +=
  3839. plane_state->pending.width -
  3840. plane_state_r->pending.width - w/2;
  3841. plane_state_r->pending.src_x +=
  3842. plane_state->pending.width -
  3843. plane_state_r->pending.width;
  3844. if (w/2 > (plane_state->pending.width
  3845. + plane_state->pending.dst_x))
  3846. plane_state_r->pending.enable = 0;
  3847. if (plane_state_r->pending.width
  3848. > plane_state->pending.width)
  3849. plane_state_r->pending.width =
  3850. plane_state->pending.width;
  3851. memcpy(plane_state,
  3852. plane_state_l, sizeof(struct mtk_plane_state));
  3853. }
  3854. void mtk_drm_crtc_plane_update(struct drm_crtc *crtc, struct drm_plane *plane,
  3855. struct mtk_plane_state *plane_state)
  3856. {
  3857. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  3858. unsigned int plane_index = to_crtc_plane_index(plane->index);
  3859. struct drm_crtc_state *crtc_state = crtc->state;
  3860. struct mtk_crtc_state *state = to_mtk_crtc_state(crtc_state);
  3861. struct mtk_ddp_comp *comp = mtk_crtc_get_comp(crtc, 0, 0);
  3862. #ifdef CONFIG_MTK_DISPLAY_CMDQ
  3863. unsigned int v = crtc->state->adjusted_mode.vdisplay;
  3864. unsigned int h = crtc->state->adjusted_mode.hdisplay;
  3865. #endif
  3866. struct cmdq_pkt *cmdq_handle = state->cmdq_handle;
  3867. struct cmdq_pkt_buffer *cmdq_buf = &(mtk_crtc->gce_obj.buf);
  3868. unsigned int last_fence, cur_fence, sub;
  3869. dma_addr_t addr;
  3870. if (comp)
  3871. DDPINFO("%s+ comp_id:%d, comp_id:%d\n", __func__, comp->id,
  3872. plane_state->comp_state.comp_id);
  3873. if (plane_state->pending.enable) {
  3874. if (mtk_crtc->is_dual_pipe) {
  3875. struct mtk_plane_state plane_state_l;
  3876. struct mtk_plane_state plane_state_r;
  3877. mtk_drm_layer_dispatch_to_dual_pipe(plane_state,
  3878. &plane_state_l, &plane_state_r,
  3879. crtc->state->adjusted_mode.hdisplay);
  3880. comp = mtk_crtc->dual_pipe_ddp_ctx.ddp_comp[0][0];
  3881. plane_state_r.comp_state.comp_id = comp->id;
  3882. mtk_ddp_comp_layer_config(comp, plane_index,
  3883. &plane_state_r, cmdq_handle);
  3884. DDPINFO("%s+ comp_id:%d, comp_id:%d\n",
  3885. __func__, comp->id,
  3886. plane_state_r.comp_state.comp_id);
  3887. }
  3888. comp = mtk_crtc_get_plane_comp(crtc, plane_state);
  3889. mtk_ddp_comp_layer_config(comp, plane_index, plane_state,
  3890. cmdq_handle);
  3891. #ifdef CONFIG_MTK_DISPLAY_CMDQ
  3892. mtk_wb_atomic_commit(mtk_crtc, v, h, state->cmdq_handle);
  3893. #else
  3894. mtk_wb_atomic_commit(mtk_crtc);
  3895. #endif
  3896. } else if (state->prop_val[CRTC_PROP_USER_SCEN] &
  3897. USER_SCEN_BLANK) {
  3898. /* plane disable at mtk_crtc_get_plane_comp_state() actually */
  3899. /* following statement is for disable all layers during suspend */
  3900. comp = mtk_crtc_get_plane_comp(crtc, plane_state);
  3901. mtk_ddp_comp_layer_config(comp, plane_index, plane_state,
  3902. cmdq_handle);
  3903. }
  3904. last_fence = *(unsigned int *)(cmdq_buf->va_base +
  3905. DISP_SLOT_CUR_CONFIG_FENCE(plane_index));
  3906. cur_fence = plane_state->pending.prop_val[PLANE_PROP_NEXT_BUFF_IDX];
  3907. addr = cmdq_buf->pa_base + DISP_SLOT_CUR_CONFIG_FENCE(plane_index);
  3908. if (cur_fence != -1 && cur_fence > last_fence)
  3909. cmdq_pkt_write(cmdq_handle, mtk_crtc->gce_obj.base, addr,
  3910. cur_fence, ~0);
  3911. if (plane_state->pending.enable &&
  3912. plane_state->pending.format != DRM_FORMAT_C8)
  3913. sub = 1;
  3914. else
  3915. sub = 0;
  3916. addr = cmdq_buf->pa_base + DISP_SLOT_SUBTRACTOR_WHEN_FREE(plane_index);
  3917. cmdq_pkt_write(cmdq_handle, mtk_crtc->gce_obj.base, addr, sub, ~0);
  3918. DDPINFO("%s-\n", __func__);
  3919. }
  3920. static void mtk_crtc_wb_comp_config(struct drm_crtc *crtc,
  3921. struct cmdq_pkt *cmdq_handle)
  3922. {
  3923. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  3924. struct mtk_crtc_state *state = to_mtk_crtc_state(crtc->state);
  3925. struct mtk_ddp_comp *comp = NULL;
  3926. struct mtk_crtc_ddp_ctx *ddp_ctx = NULL;
  3927. struct mtk_ddp_config cfg;
  3928. comp = mtk_ddp_comp_find_by_id(crtc, DDP_COMPONENT_WDMA0);
  3929. if (!comp)
  3930. comp = mtk_ddp_comp_find_by_id(crtc, DDP_COMPONENT_WDMA1);
  3931. if (!comp) {
  3932. DDPPR_ERR("The wb component is not exsit\n");
  3933. return;
  3934. }
  3935. memset(&cfg, 0x0, sizeof(struct mtk_ddp_config));
  3936. if (state->prop_val[CRTC_PROP_OUTPUT_ENABLE]) {
  3937. /* Output buffer configuration for virtual display */
  3938. DDPINFO("lookup wb fb:%u\n",
  3939. state->prop_val[CRTC_PROP_OUTPUT_FB_ID]);
  3940. comp->fb = mtk_drm_framebuffer_lookup(crtc->dev,
  3941. state->prop_val[CRTC_PROP_OUTPUT_FB_ID]);
  3942. if (comp->fb == NULL) {
  3943. DDPPR_ERR("%s cannot find fb fb_id:%u\n", __func__,
  3944. state->prop_val[CRTC_PROP_OUTPUT_FB_ID]);
  3945. return;
  3946. }
  3947. cfg.w = state->prop_val[CRTC_PROP_OUTPUT_WIDTH];
  3948. cfg.h = state->prop_val[CRTC_PROP_OUTPUT_HEIGHT];
  3949. cfg.x = state->prop_val[CRTC_PROP_OUTPUT_X];
  3950. cfg.y = state->prop_val[CRTC_PROP_OUTPUT_Y];
  3951. if (mtk_crtc->panel_ext && mtk_crtc->panel_ext->params) {
  3952. struct mtk_panel_params *params;
  3953. params = mtk_crtc->panel_ext->params;
  3954. if (params->dyn_fps.switch_en == 1 &&
  3955. params->dyn_fps.vact_timing_fps != 0)
  3956. cfg.vrefresh =
  3957. params->dyn_fps.vact_timing_fps;
  3958. else
  3959. cfg.vrefresh =
  3960. crtc->state->adjusted_mode.vrefresh;
  3961. } else
  3962. cfg.vrefresh = crtc->state->adjusted_mode.vrefresh;
  3963. cfg.bpc = mtk_crtc->bpc;
  3964. cfg.p_golden_setting_context =
  3965. __get_golden_setting_context(mtk_crtc);
  3966. cfg.p_golden_setting_context->dst_width = cfg.w;
  3967. cfg.p_golden_setting_context->dst_height = cfg.h;
  3968. } else {
  3969. /* Output buffer configuration for internal decouple mode */
  3970. ddp_ctx = &mtk_crtc->ddp_ctx[mtk_crtc->ddp_mode];
  3971. comp->fb = ddp_ctx->dc_fb;
  3972. ddp_ctx->dc_fb_idx =
  3973. (ddp_ctx->dc_fb_idx + 1) % MAX_CRTC_DC_FB;
  3974. ddp_ctx->dc_fb->offsets[0] =
  3975. mtk_crtc_get_dc_fb_size(crtc) * ddp_ctx->dc_fb_idx;
  3976. cfg.w = crtc->state->adjusted_mode.hdisplay;
  3977. cfg.h = crtc->state->adjusted_mode.vdisplay;
  3978. if (mtk_crtc->panel_ext && mtk_crtc->panel_ext->params) {
  3979. struct mtk_panel_params *params;
  3980. params = mtk_crtc->panel_ext->params;
  3981. if (params->dyn_fps.switch_en == 1 &&
  3982. params->dyn_fps.vact_timing_fps != 0)
  3983. cfg.vrefresh =
  3984. params->dyn_fps.vact_timing_fps;
  3985. else
  3986. cfg.vrefresh =
  3987. crtc->state->adjusted_mode.vrefresh;
  3988. } else
  3989. cfg.vrefresh = crtc->state->adjusted_mode.vrefresh;
  3990. cfg.bpc = mtk_crtc->bpc;
  3991. cfg.p_golden_setting_context =
  3992. __get_golden_setting_context(mtk_crtc);
  3993. }
  3994. mtk_ddp_comp_config(comp, &cfg, cmdq_handle);
  3995. }
  3996. static void mtk_crtc_wb_backup_to_slot(struct drm_crtc *crtc,
  3997. struct cmdq_pkt *cmdq_handle)
  3998. {
  3999. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  4000. struct mtk_crtc_state *state = to_mtk_crtc_state(crtc->state);
  4001. struct mtk_crtc_ddp_ctx *ddp_ctx = NULL;
  4002. dma_addr_t addr;
  4003. struct cmdq_pkt_buffer *cmdq_buf = &(mtk_crtc->gce_obj.buf);
  4004. ddp_ctx = &mtk_crtc->ddp_ctx[mtk_crtc->ddp_mode];
  4005. addr = cmdq_buf->pa_base + DISP_SLOT_RDMA_FB_IDX;
  4006. if (state->prop_val[CRTC_PROP_OUTPUT_ENABLE]) {
  4007. cmdq_pkt_write(cmdq_handle, mtk_crtc->gce_obj.base,
  4008. addr, 0, ~0);
  4009. } else {
  4010. cmdq_pkt_write(cmdq_handle, mtk_crtc->gce_obj.base,
  4011. addr, ddp_ctx->dc_fb_idx, ~0);
  4012. }
  4013. addr = cmdq_buf->pa_base + DISP_SLOT_RDMA_FB_ID;
  4014. if (state->prop_val[CRTC_PROP_OUTPUT_ENABLE]) {
  4015. cmdq_pkt_write(cmdq_handle, mtk_crtc->gce_obj.base,
  4016. addr, state->prop_val[CRTC_PROP_OUTPUT_FB_ID], ~0);
  4017. } else {
  4018. cmdq_pkt_write(cmdq_handle, mtk_crtc->gce_obj.base,
  4019. addr, ddp_ctx->dc_fb->base.id, ~0);
  4020. }
  4021. addr = cmdq_buf->pa_base + DISP_SLOT_CUR_OUTPUT_FENCE;
  4022. cmdq_pkt_write(cmdq_handle, mtk_crtc->gce_obj.base,
  4023. addr, state->prop_val[CRTC_PROP_OUTPUT_FENCE_IDX], ~0);
  4024. addr = cmdq_buf->pa_base + DISP_SLOT_CUR_INTERFACE_FENCE;
  4025. cmdq_pkt_write(cmdq_handle, mtk_crtc->gce_obj.base,
  4026. addr, state->prop_val[CRTC_PROP_INTF_FENCE_IDX], ~0);
  4027. }
  4028. int mtk_crtc_gec_flush_check(struct drm_crtc *crtc)
  4029. {
  4030. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  4031. struct mtk_crtc_state *state = to_mtk_crtc_state(crtc->state);
  4032. struct mtk_ddp_comp *output_comp = NULL;
  4033. output_comp = mtk_ddp_comp_request_output(mtk_crtc);
  4034. if (output_comp) {
  4035. switch (output_comp->id) {
  4036. case DDP_COMPONENT_WDMA0:
  4037. case DDP_COMPONENT_WDMA1:
  4038. if (!state->prop_val[CRTC_PROP_OUTPUT_ENABLE])
  4039. return -EINVAL;
  4040. break;
  4041. default:
  4042. break;
  4043. }
  4044. }
  4045. return 0;
  4046. }
  4047. static struct disp_ccorr_config *mtk_crtc_get_color_matrix_data(
  4048. struct drm_crtc *crtc)
  4049. {
  4050. struct mtk_crtc_state *state = to_mtk_crtc_state(crtc->state);
  4051. int blob_id;
  4052. struct disp_ccorr_config *ccorr_config = NULL;
  4053. struct drm_property_blob *blob;
  4054. int *color_matrix;
  4055. blob_id = state->prop_val[CRTC_PROP_COLOR_TRANSFORM];
  4056. /* if blod_id == 0 means this time no new color matrix need to set */
  4057. if (!blob_id)
  4058. goto end;
  4059. blob = drm_property_lookup_blob(crtc->dev, blob_id);
  4060. if (!blob) {
  4061. DDPPR_ERR("Cannot get color matrix blob: %d!\n", blob_id);
  4062. goto end;
  4063. }
  4064. ccorr_config = (struct disp_ccorr_config *)blob->data;
  4065. drm_property_unreference_blob(blob);
  4066. if (ccorr_config) {
  4067. int i = 0, all_zero = 1;
  4068. color_matrix = ccorr_config->color_matrix;
  4069. for (i = 0; i <= 15; i += 5) {
  4070. if (color_matrix[i] != 0) {
  4071. all_zero = 0;
  4072. break;
  4073. }
  4074. }
  4075. if (all_zero) {
  4076. DDPPR_ERR("HWC set zero color matrix!\n");
  4077. goto end;
  4078. }
  4079. } else
  4080. DDPPR_ERR("Blob cannot get ccorr_config data!\n");
  4081. end:
  4082. return ccorr_config;
  4083. }
  4084. static void mtk_crtc_backup_color_matrix_data(struct drm_crtc *crtc,
  4085. struct disp_ccorr_config *ccorr_config,
  4086. struct cmdq_pkt *cmdq_handle)
  4087. {
  4088. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  4089. struct cmdq_pkt_buffer *cmdq_buf = &(mtk_crtc->gce_obj.buf);
  4090. dma_addr_t addr;
  4091. int i;
  4092. if (!ccorr_config)
  4093. return;
  4094. addr = cmdq_buf->pa_base + DISP_SLOT_COLOR_MATRIX_PARAMS(0);
  4095. cmdq_pkt_write(cmdq_handle, mtk_crtc->gce_obj.base,
  4096. addr, ccorr_config->mode, ~0);
  4097. for (i = 0; i < 16; i++) {
  4098. addr = cmdq_buf->pa_base +
  4099. DISP_SLOT_COLOR_MATRIX_PARAMS(i + 1);
  4100. cmdq_pkt_write(cmdq_handle, mtk_crtc->gce_obj.base,
  4101. addr, ccorr_config->color_matrix[i], ~0);
  4102. }
  4103. }
  4104. static void mtk_crtc_dl_config_color_matrix(struct drm_crtc *crtc,
  4105. struct disp_ccorr_config *ccorr_config,
  4106. struct cmdq_pkt *cmdq_handle)
  4107. {
  4108. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  4109. struct mtk_crtc_ddp_ctx *ddp_ctx;
  4110. bool set = false;
  4111. int i;
  4112. if (!ccorr_config)
  4113. return;
  4114. ddp_ctx = &mtk_crtc->ddp_ctx[mtk_crtc->ddp_mode];
  4115. for (i = 0; i < ddp_ctx->ddp_comp_nr[DDP_FIRST_PATH]; i++) {
  4116. struct mtk_ddp_comp *comp =
  4117. ddp_ctx->ddp_comp[DDP_FIRST_PATH][i];
  4118. if (comp->id == DDP_COMPONENT_CCORR0) {
  4119. disp_ccorr_set_color_matrix(comp, cmdq_handle,
  4120. ccorr_config->color_matrix,
  4121. ccorr_config->mode, ccorr_config->featureFlag);
  4122. set = true;
  4123. break;
  4124. }
  4125. }
  4126. if (set)
  4127. mtk_crtc_backup_color_matrix_data(crtc, ccorr_config,
  4128. cmdq_handle);
  4129. else
  4130. DDPPR_ERR("Cannot not find DDP_COMPONENT_CCORR0\n");
  4131. }
  4132. int mtk_crtc_gce_flush(struct drm_crtc *crtc, void *gce_cb,
  4133. void *cb_data, struct cmdq_pkt *cmdq_handle)
  4134. {
  4135. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  4136. struct mtk_crtc_state *state = to_mtk_crtc_state(crtc->state);
  4137. struct disp_ccorr_config *ccorr_config = NULL;
  4138. if (mtk_crtc_gec_flush_check(crtc) < 0) {
  4139. if (cb_data) {
  4140. struct drm_crtc_state *crtc_state;
  4141. struct drm_atomic_state *atomic_state;
  4142. crtc_state = ((struct mtk_cmdq_cb_data *)cb_data)->state;
  4143. atomic_state = crtc_state->state;
  4144. mtk_atomic_state_put(atomic_state);
  4145. }
  4146. cmdq_pkt_destroy(cmdq_handle);
  4147. kfree(cb_data);
  4148. DDPPR_ERR("flush check failed\n");
  4149. return -1;
  4150. }
  4151. /* apply color matrix if crtc0 is DL */
  4152. ccorr_config = mtk_crtc_get_color_matrix_data(crtc);
  4153. if (drm_crtc_index(crtc) == 0 && (!mtk_crtc_is_dc_mode(crtc)))
  4154. mtk_crtc_dl_config_color_matrix(crtc, ccorr_config,
  4155. cmdq_handle);
  4156. if (mtk_crtc_is_dc_mode(crtc) ||
  4157. state->prop_val[CRTC_PROP_OUTPUT_ENABLE]) {
  4158. int gce_event =
  4159. get_path_wait_event(mtk_crtc, mtk_crtc->ddp_mode);
  4160. mtk_crtc_wb_comp_config(crtc, cmdq_handle);
  4161. if (mtk_crtc_is_dc_mode(crtc))
  4162. /* Decouple and Decouple mirror mode */
  4163. mtk_disp_mutex_enable_cmdq(mtk_crtc->mutex[1],
  4164. cmdq_handle, mtk_crtc->gce_obj.base);
  4165. else {
  4166. /* For virtual display write-back path */
  4167. cmdq_pkt_clear_event(cmdq_handle, gce_event);
  4168. mtk_disp_mutex_enable_cmdq(mtk_crtc->mutex[0],
  4169. cmdq_handle, mtk_crtc->gce_obj.base);
  4170. }
  4171. cmdq_pkt_wait_no_clear(cmdq_handle, gce_event);
  4172. } else if (mtk_crtc_is_frame_trigger_mode(crtc) &&
  4173. mtk_crtc_with_trigger_loop(crtc)) {
  4174. /* DL with trigger loop */
  4175. cmdq_pkt_set_event(cmdq_handle,
  4176. mtk_crtc->gce_obj.event[EVENT_STREAM_DIRTY]);
  4177. } else {
  4178. /* DL without trigger loop */
  4179. mtk_disp_mutex_enable_cmdq(mtk_crtc->mutex[0],
  4180. cmdq_handle, mtk_crtc->gce_obj.base);
  4181. }
  4182. if (mtk_crtc_is_dc_mode(crtc) ||
  4183. state->prop_val[CRTC_PROP_OUTPUT_ENABLE]) {
  4184. mtk_crtc_wb_backup_to_slot(crtc, cmdq_handle);
  4185. /* backup color matrix for DC and DC Mirror for RDMA update*/
  4186. if (mtk_crtc_is_dc_mode(crtc))
  4187. mtk_crtc_backup_color_matrix_data(crtc, ccorr_config,
  4188. cmdq_handle);
  4189. }
  4190. #ifdef MTK_DRM_CMDQ_ASYNC
  4191. if (cmdq_pkt_flush_threaded(cmdq_handle,
  4192. gce_cb, cb_data) < 0)
  4193. DDPPR_ERR("failed to flush gce_cb\n");
  4194. #else
  4195. cmdq_pkt_flush(cmdq_handle);
  4196. #endif
  4197. return 0;
  4198. }
  4199. static void mtk_drm_crtc_enable_fake_layer(struct drm_crtc *crtc,
  4200. struct drm_crtc_state *old_crtc_state)
  4201. {
  4202. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  4203. struct drm_plane *plane;
  4204. struct mtk_drm_private *priv = crtc->dev->dev_private;
  4205. struct mtk_plane_state *plane_state;
  4206. struct mtk_plane_pending_state *pending;
  4207. struct mtk_ddp_comp *comp;
  4208. struct mtk_crtc_state *state = to_mtk_crtc_state(crtc->state);
  4209. struct mtk_drm_fake_layer *fake_layer = &mtk_crtc->fake_layer;
  4210. int i, idx, layer_num;
  4211. if (drm_crtc_index(crtc) != 0)
  4212. return;
  4213. DDPINFO("%s\n", __func__);
  4214. for (i = 0 ; i < PRIMARY_OVL_PHY_LAYER_NR ; i++) {
  4215. plane = &mtk_crtc->planes[i].base;
  4216. plane_state = to_mtk_plane_state(plane->state);
  4217. pending = &plane_state->pending;
  4218. pending->addr = mtk_fb_get_dma(fake_layer->fake_layer_buf[i]);
  4219. pending->size = mtk_fb_get_size(fake_layer->fake_layer_buf[i]);
  4220. pending->pitch = fake_layer->fake_layer_buf[i]->pitches[0];
  4221. pending->format = fake_layer->fake_layer_buf[i]->format->format;
  4222. pending->modifier = fake_layer->fake_layer_buf[i]->modifier[0];
  4223. pending->src_x = 0;
  4224. pending->src_y = 0;
  4225. pending->dst_x = 0;
  4226. pending->dst_y = 0;
  4227. pending->height = fake_layer->fake_layer_buf[i]->height;
  4228. pending->width = fake_layer->fake_layer_buf[i]->width;
  4229. pending->config = 1;
  4230. pending->dirty = 1;
  4231. if (mtk_crtc->fake_layer.fake_layer_mask & BIT(i))
  4232. pending->enable = true;
  4233. else
  4234. pending->enable = false;
  4235. pending->prop_val[PLANE_PROP_ALPHA_CON] = 0x1;
  4236. pending->prop_val[PLANE_PROP_PLANE_ALPHA] = 0xFF;
  4237. pending->prop_val[PLANE_PROP_COMPRESS] = 0;
  4238. layer_num = mtk_ovl_layer_num(
  4239. priv->ddp_comp[DDP_COMPONENT_OVL0_2L]);
  4240. if (layer_num < 0) {
  4241. DDPPR_ERR("invalid layer num:%d\n", layer_num);
  4242. continue;
  4243. }
  4244. if (i < layer_num) {
  4245. comp = priv->ddp_comp[DDP_COMPONENT_OVL0_2L];
  4246. idx = i;
  4247. } else {
  4248. comp = priv->ddp_comp[DDP_COMPONENT_OVL0];
  4249. idx = i - layer_num;
  4250. }
  4251. plane_state->comp_state.comp_id = comp->id;
  4252. plane_state->comp_state.lye_id = idx;
  4253. plane_state->comp_state.ext_lye_id = 0;
  4254. mtk_ddp_comp_layer_config(comp, plane_state->comp_state.lye_id,
  4255. plane_state, state->cmdq_handle);
  4256. }
  4257. for (i = 0 ; i < PRIMARY_OVL_EXT_LAYER_NR ; i++) {
  4258. plane = &mtk_crtc->planes[i + PRIMARY_OVL_PHY_LAYER_NR].base;
  4259. plane_state = to_mtk_plane_state(plane->state);
  4260. pending = &plane_state->pending;
  4261. pending->dirty = 1;
  4262. pending->enable = false;
  4263. if (i < (PRIMARY_OVL_EXT_LAYER_NR / 2)) {
  4264. comp = priv->ddp_comp[DDP_COMPONENT_OVL0_2L];
  4265. idx = i + 1;
  4266. } else {
  4267. comp = priv->ddp_comp[DDP_COMPONENT_OVL0];
  4268. idx = i + 1 - (PRIMARY_OVL_EXT_LAYER_NR / 2);
  4269. }
  4270. plane_state->comp_state.comp_id = comp->id;
  4271. plane_state->comp_state.lye_id = 0;
  4272. plane_state->comp_state.ext_lye_id = idx;
  4273. mtk_ddp_comp_layer_config(comp, plane_state->comp_state.lye_id,
  4274. plane_state, state->cmdq_handle);
  4275. }
  4276. }
  4277. static void mtk_drm_crtc_disable_fake_layer(struct drm_crtc *crtc,
  4278. struct drm_crtc_state *old_crtc_state)
  4279. {
  4280. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  4281. struct drm_plane *plane;
  4282. struct mtk_drm_private *priv = crtc->dev->dev_private;
  4283. struct mtk_plane_state *plane_state;
  4284. struct mtk_plane_pending_state *pending;
  4285. struct mtk_ddp_comp *comp;
  4286. struct mtk_crtc_state *state = to_mtk_crtc_state(crtc->state);
  4287. int i, idx, layer_num;
  4288. if (drm_crtc_index(crtc) != 0)
  4289. return;
  4290. DDPINFO("%s\n", __func__);
  4291. for (i = 0 ; i < PRIMARY_OVL_PHY_LAYER_NR ; i++) {
  4292. plane = &mtk_crtc->planes[i].base;
  4293. plane_state = to_mtk_plane_state(plane->state);
  4294. pending = &plane_state->pending;
  4295. pending->dirty = 1;
  4296. pending->enable = false;
  4297. layer_num = mtk_ovl_layer_num(
  4298. priv->ddp_comp[DDP_COMPONENT_OVL0_2L]);
  4299. if (layer_num < 0) {
  4300. DDPPR_ERR("invalid layer num:%d\n", layer_num);
  4301. continue;
  4302. }
  4303. if (i < layer_num) {
  4304. comp = priv->ddp_comp[DDP_COMPONENT_OVL0_2L];
  4305. idx = i;
  4306. } else {
  4307. comp = priv->ddp_comp[DDP_COMPONENT_OVL0];
  4308. idx = i - layer_num;
  4309. }
  4310. plane_state->comp_state.comp_id = comp->id;
  4311. plane_state->comp_state.lye_id = idx;
  4312. plane_state->comp_state.ext_lye_id = 0;
  4313. mtk_ddp_comp_layer_config(comp, plane_state->comp_state.lye_id,
  4314. plane_state, state->cmdq_handle);
  4315. }
  4316. }
  4317. static void mtk_drm_crtc_atomic_flush(struct drm_crtc *crtc,
  4318. struct drm_crtc_state *old_crtc_state)
  4319. {
  4320. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  4321. struct mtk_drm_private *priv = crtc->dev->dev_private;
  4322. int index = drm_crtc_index(crtc);
  4323. unsigned int pending_planes = 0;
  4324. unsigned int i, j;
  4325. unsigned int ret = 0;
  4326. struct drm_crtc_state *crtc_state = crtc->state;
  4327. struct mtk_crtc_state *state = to_mtk_crtc_state(crtc_state);
  4328. struct cmdq_pkt *cmdq_handle = state->cmdq_handle;
  4329. struct mtk_cmdq_cb_data *cb_data;
  4330. struct mtk_ddp_comp *comp;
  4331. struct mtk_drm_crtc *mtk_crtc0 = to_mtk_crtc(priv->crtc[0]);
  4332. CRTC_MMP_EVENT_START(index, atomic_flush, (unsigned long)crtc_state,
  4333. (unsigned long)old_crtc_state);
  4334. if (mtk_crtc->ddp_mode == DDP_NO_USE) {
  4335. CRTC_MMP_MARK(index, atomic_flush, 0, 0);
  4336. goto end;
  4337. }
  4338. cb_data = kmalloc(sizeof(*cb_data), GFP_KERNEL);
  4339. if (!cb_data) {
  4340. DDPPR_ERR("cb data creation failed\n");
  4341. CRTC_MMP_MARK(index, atomic_flush, 0, 1);
  4342. goto end;
  4343. }
  4344. if (mtk_crtc->event)
  4345. mtk_crtc->pending_needs_vblank = true;
  4346. for (i = 0; i < mtk_crtc->layer_nr; i++) {
  4347. struct drm_plane *plane = &mtk_crtc->planes[i].base;
  4348. struct mtk_plane_state *plane_state;
  4349. plane_state = to_mtk_plane_state(plane->state);
  4350. if (plane_state->pending.dirty) {
  4351. plane_state->pending.config = true;
  4352. plane_state->pending.dirty = false;
  4353. pending_planes |= BIT(i);
  4354. }
  4355. }
  4356. if (pending_planes)
  4357. mtk_crtc->pending_planes = true;
  4358. if (mtk_drm_helper_get_opt(priv->helper_opt, MTK_DRM_OPT_HBM)) {
  4359. bool hbm_en = false;
  4360. hbm_en = (bool)state->prop_val[CRTC_PROP_HBM_ENABLE];
  4361. mtk_drm_crtc_set_panel_hbm(crtc, hbm_en);
  4362. mtk_drm_crtc_hbm_wait(crtc, hbm_en);
  4363. }
  4364. hdr_en = (bool)state->prop_val[CRTC_PROP_HDR_ENABLE];
  4365. for_each_comp_in_cur_crtc_path(comp, mtk_crtc, i, j) {
  4366. if (crtc->state->color_mgmt_changed)
  4367. mtk_ddp_gamma_set(comp, crtc->state, cmdq_handle);
  4368. mtk_ddp_comp_io_cmd(comp, cmdq_handle,
  4369. PMQOS_UPDATE_BW, NULL);
  4370. mtk_ddp_comp_io_cmd(comp, cmdq_handle,
  4371. FRAME_DIRTY, NULL);
  4372. }
  4373. if ((priv->data->shadow_register) == true) {
  4374. mtk_disp_mutex_acquire(mtk_crtc->mutex[0]);
  4375. mtk_crtc_ddp_config(crtc);
  4376. mtk_disp_mutex_release(mtk_crtc->mutex[0]);
  4377. }
  4378. if (mtk_crtc->fake_layer.fake_layer_mask)
  4379. mtk_drm_crtc_enable_fake_layer(crtc, old_crtc_state);
  4380. else if (mtk_crtc->fake_layer.first_dis) {
  4381. mtk_drm_crtc_disable_fake_layer(crtc, old_crtc_state);
  4382. mtk_crtc->fake_layer.first_dis = false;
  4383. }
  4384. /* backup ovl0 2l status for crtc0 */
  4385. if (index == 0) {
  4386. comp = mtk_ddp_comp_find_by_id(crtc, DDP_COMPONENT_OVL0_2L);
  4387. if (comp != NULL)
  4388. mtk_ddp_comp_io_cmd(comp, cmdq_handle,
  4389. BACKUP_OVL_STATUS, NULL);
  4390. }
  4391. #ifdef MTK_DRM_DELAY_PRESENT_FENCE
  4392. /* backup present fence */
  4393. if (state->prop_val[CRTC_PROP_PRES_FENCE_IDX] != (unsigned int)-1) {
  4394. struct cmdq_pkt_buffer *cmdq_buf = &(mtk_crtc->gce_obj.buf);
  4395. dma_addr_t addr =
  4396. cmdq_buf->pa_base +
  4397. DISP_SLOT_PRESENT_FENCE(drm_crtc_index(crtc));
  4398. cmdq_pkt_write(cmdq_handle,
  4399. mtk_crtc->gce_obj.base, addr,
  4400. state->prop_val[CRTC_PROP_PRES_FENCE_IDX], ~0);
  4401. }
  4402. #endif
  4403. atomic_set(&mtk_crtc->delayed_trig, 1);
  4404. cb_data->state = old_crtc_state;
  4405. cb_data->cmdq_handle = cmdq_handle;
  4406. cb_data->misc = mtk_crtc->ddp_mode;
  4407. /* This refcnt would be release in ddp_cmdq_cb */
  4408. mtk_atomic_state_get(old_crtc_state->state);
  4409. mtk_drm_crtc_lfr_update(crtc, cmdq_handle);
  4410. #ifdef MTK_DRM_CMDQ_ASYNC
  4411. ret = mtk_crtc_gce_flush(crtc, ddp_cmdq_cb, cb_data, cmdq_handle);
  4412. if (ret) {
  4413. DDPPR_ERR("mtk_crtc_gce_flush failed!\n");
  4414. goto end;
  4415. }
  4416. CRTC_MMP_MARK(index, atomic_flush, (unsigned long)cmdq_handle,
  4417. (unsigned long)cmdq_handle->cmd_buf_size);
  4418. #else
  4419. ret = mtk_crtc_gce_flush(crtc, NULL, NULL, cmdq_handle);
  4420. if (ret) {
  4421. DDPPR_ERR("mtk_crtc_gce_flush failed!\n");
  4422. goto end;
  4423. }
  4424. ddp_cmdq_cb_blocking(cb_data);
  4425. #endif
  4426. #ifdef MTK_DRM_FENCE_SUPPORT
  4427. if (state->prop_val[CRTC_PROP_PRES_FENCE_IDX] != (unsigned int)-1)
  4428. mtk_drm_fence_update(state->prop_val[CRTC_PROP_PRES_FENCE_IDX],
  4429. index);
  4430. #endif
  4431. /* When open VDS path switch feature, After VDS created
  4432. * we need take away the OVL0_2L from main display.
  4433. */
  4434. if (mtk_drm_helper_get_opt(priv->helper_opt,
  4435. MTK_DRM_OPT_VDS_PATH_SWITCH) &&
  4436. priv->vds_path_switch_dirty &&
  4437. !priv->vds_path_switch_done) {
  4438. if ((index == 0) && atomic_read(&mtk_crtc0->already_config)) {
  4439. DDPMSG("Switch vds: mtk_crtc0 enable:%d\n",
  4440. atomic_read(&mtk_crtc0->already_config));
  4441. mtk_need_vds_path_switch(crtc);
  4442. }
  4443. if ((index == 2) && (!atomic_read(&mtk_crtc0->already_config))) {
  4444. DDPMSG("Switch vds: mtk_crtc0 enable:%d\n",
  4445. atomic_read(&mtk_crtc0->already_config));
  4446. mtk_need_vds_path_switch(priv->crtc[0]);
  4447. }
  4448. }
  4449. end:
  4450. CRTC_MMP_EVENT_END(index, atomic_flush, (unsigned long)crtc_state,
  4451. (unsigned long)old_crtc_state);
  4452. mtk_drm_trace_end();
  4453. }
  4454. static const struct drm_crtc_funcs mtk_crtc_funcs = {
  4455. .set_config = drm_atomic_helper_set_config,
  4456. .page_flip = drm_atomic_helper_page_flip,
  4457. .destroy = mtk_drm_crtc_destroy,
  4458. .reset = mtk_drm_crtc_reset,
  4459. .atomic_duplicate_state = mtk_drm_crtc_duplicate_state,
  4460. .atomic_destroy_state = mtk_drm_crtc_destroy_state,
  4461. .atomic_set_property = mtk_drm_crtc_set_property,
  4462. .atomic_get_property = mtk_drm_crtc_get_property,
  4463. .gamma_set = drm_atomic_helper_legacy_gamma_set,
  4464. };
  4465. static const struct drm_crtc_helper_funcs mtk_crtc_helper_funcs = {
  4466. .mode_fixup = mtk_drm_crtc_mode_fixup,
  4467. .mode_set_nofb = mtk_drm_crtc_mode_set_nofb,
  4468. .atomic_enable = mtk_drm_crtc_atomic_resume,
  4469. .disable = mtk_drm_crtc_suspend,
  4470. .atomic_begin = mtk_drm_crtc_atomic_begin,
  4471. .atomic_flush = mtk_drm_crtc_atomic_flush,
  4472. };
  4473. static void mtk_drm_crtc_attach_property(struct drm_crtc *crtc)
  4474. {
  4475. struct drm_device *dev = crtc->dev;
  4476. struct mtk_drm_private *private = dev->dev_private;
  4477. struct drm_property *prop;
  4478. static struct drm_property *mtk_crtc_prop[CRTC_PROP_MAX];
  4479. struct mtk_drm_property *crtc_prop;
  4480. int index = drm_crtc_index(crtc);
  4481. int i;
  4482. static int num;
  4483. DDPINFO("%s:%d crtc:%d\n", __func__, __LINE__, index);
  4484. if (num == 0) {
  4485. for (i = 0; i < CRTC_PROP_MAX; i++) {
  4486. crtc_prop = &(mtk_crtc_property[i]);
  4487. mtk_crtc_prop[i] = drm_property_create_range(
  4488. dev, crtc_prop->flags, crtc_prop->name,
  4489. crtc_prop->min, crtc_prop->max);
  4490. if (!mtk_crtc_prop[i]) {
  4491. DDPPR_ERR("fail to create property:%s\n",
  4492. crtc_prop->name);
  4493. return;
  4494. }
  4495. DDPINFO("create property:%s, flags:0x%x\n",
  4496. crtc_prop->name, mtk_crtc_prop[i]->flags);
  4497. }
  4498. num++;
  4499. }
  4500. for (i = 0; i < CRTC_PROP_MAX; i++) {
  4501. prop = private->crtc_property[index][i];
  4502. crtc_prop = &(mtk_crtc_property[i]);
  4503. DDPINFO("%s:%d prop:%p\n", __func__, __LINE__, prop);
  4504. if (!prop) {
  4505. prop = mtk_crtc_prop[i];
  4506. private
  4507. ->crtc_property[index][i] = prop;
  4508. drm_object_attach_property(&crtc->base, prop,
  4509. crtc_prop->val);
  4510. }
  4511. }
  4512. }
  4513. static int mtk_drm_crtc_init(struct drm_device *drm,
  4514. struct mtk_drm_crtc *mtk_crtc,
  4515. struct drm_plane *primary,
  4516. struct drm_plane *cursor, unsigned int pipe)
  4517. {
  4518. int ret;
  4519. DDPINFO("%s+\n", __func__);
  4520. ret = drm_crtc_init_with_planes(drm, &mtk_crtc->base, primary, cursor,
  4521. &mtk_crtc_funcs, NULL);
  4522. if (ret)
  4523. goto err_cleanup_crtc;
  4524. drm_crtc_helper_add(&mtk_crtc->base, &mtk_crtc_helper_funcs);
  4525. mtk_drm_crtc_attach_property(&mtk_crtc->base);
  4526. DDPINFO("%s-\n", __func__);
  4527. return 0;
  4528. err_cleanup_crtc:
  4529. drm_crtc_cleanup(&mtk_crtc->base);
  4530. return ret;
  4531. }
  4532. void mtk_crtc_ddp_irq(struct drm_crtc *crtc, struct mtk_ddp_comp *comp)
  4533. {
  4534. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  4535. struct mtk_drm_private *priv = crtc->dev->dev_private;
  4536. if (priv->data->shadow_register == false)
  4537. mtk_crtc_ddp_config(crtc);
  4538. mtk_drm_finish_page_flip(mtk_crtc);
  4539. }
  4540. void mtk_crtc_vblank_irq(struct drm_crtc *crtc)
  4541. {
  4542. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  4543. char tag_name[100] = {'\0'};
  4544. ktime_t ktime = ktime_get();
  4545. mtk_crtc->vblank_time = ktime_to_timeval(ktime);
  4546. sprintf(tag_name, "%d|HW_VSYNC|%lld",
  4547. DRM_TRACE_VSYNC_ID, ktime);
  4548. mtk_drm_trace_c("%s", tag_name);
  4549. /*
  4550. * DDPMSG("%s CRTC%d %s\n", __func__,
  4551. * drm_crtc_index(crtc), tag_name);
  4552. */
  4553. drm_crtc_handle_vblank(&mtk_crtc->base);
  4554. sprintf(tag_name, "%d|HW_VSYNC|%d",
  4555. DRM_TRACE_VSYNC_ID, 0);
  4556. mtk_drm_trace_c("%s", tag_name);
  4557. }
  4558. static void mtk_crtc_get_output_comp_name(struct mtk_drm_crtc *mtk_crtc,
  4559. char *buf, int buf_len)
  4560. {
  4561. int i, j;
  4562. struct mtk_ddp_comp *comp;
  4563. for_each_comp_in_crtc_path_reverse(comp, mtk_crtc, i,
  4564. j)
  4565. if (mtk_ddp_comp_is_output(comp)) {
  4566. mtk_ddp_comp_get_name(comp, buf, buf_len);
  4567. return;
  4568. }
  4569. DDPPR_ERR("%s(), no output comp found for crtc%d, set buf to 0\n",
  4570. __func__, drm_crtc_index(&mtk_crtc->base));
  4571. memset(buf, 0, buf_len);
  4572. }
  4573. static void mtk_crtc_get_event_name(struct mtk_drm_crtc *mtk_crtc, char *buf,
  4574. int buf_len, int event_id)
  4575. {
  4576. int crtc_id, len;
  4577. char output_comp[20];
  4578. /* TODO: remove hardcode comp event */
  4579. crtc_id = drm_crtc_index(&mtk_crtc->base);
  4580. switch (event_id) {
  4581. case EVENT_STREAM_DIRTY:
  4582. len = snprintf(buf, buf_len, "disp_token_stream_dirty%d",
  4583. drm_crtc_index(&mtk_crtc->base));
  4584. break;
  4585. #if defined(CONFIG_MACH_MT6873) || defined(CONFIG_MACH_MT6853) || \
  4586. defined(CONFIG_MACH_MT6833)
  4587. case EVENT_SYNC_TOKEN_SODI:
  4588. len = snprintf(buf, buf_len, "disp_token_sodi%d",
  4589. drm_crtc_index(&mtk_crtc->base));
  4590. break;
  4591. #endif
  4592. case EVENT_STREAM_EOF:
  4593. len = snprintf(buf, buf_len, "disp_token_stream_eof%d",
  4594. drm_crtc_index(&mtk_crtc->base));
  4595. break;
  4596. case EVENT_VDO_EOF:
  4597. len = snprintf(buf, buf_len, "disp_mutex%d_eof",
  4598. drm_crtc_index(&mtk_crtc->base));
  4599. break;
  4600. case EVENT_CMD_EOF:
  4601. mtk_crtc_get_output_comp_name(mtk_crtc, output_comp,
  4602. sizeof(output_comp));
  4603. len = snprintf(buf, buf_len, "disp_%s_eof", output_comp);
  4604. break;
  4605. case EVENT_TE:
  4606. mtk_crtc_get_output_comp_name(mtk_crtc, output_comp,
  4607. sizeof(output_comp));
  4608. len = snprintf(buf, buf_len, "disp_wait_%s_te", output_comp);
  4609. break;
  4610. case EVENT_ESD_EOF:
  4611. len = snprintf(buf, buf_len, "disp_token_esd_eof%d",
  4612. drm_crtc_index(&mtk_crtc->base));
  4613. break;
  4614. case EVENT_RDMA0_EOF:
  4615. len = snprintf(buf, buf_len, "disp_rdma0_eof%d",
  4616. drm_crtc_index(&mtk_crtc->base));
  4617. break;
  4618. case EVENT_WDMA0_EOF:
  4619. len = snprintf(buf, buf_len, "disp_wdma0_eof%d",
  4620. drm_crtc_index(&mtk_crtc->base));
  4621. break;
  4622. case EVENT_WDMA1_EOF:
  4623. len = snprintf(buf, buf_len, "disp_wdma1_eof%d",
  4624. drm_crtc_index(&mtk_crtc->base));
  4625. break;
  4626. case EVENT_STREAM_BLOCK:
  4627. len = snprintf(buf, buf_len, "disp_token_stream_block%d",
  4628. drm_crtc_index(&mtk_crtc->base));
  4629. break;
  4630. case EVENT_CABC_EOF:
  4631. len = snprintf(buf, buf_len, "disp_token_cabc_eof%d",
  4632. drm_crtc_index(&mtk_crtc->base));
  4633. break;
  4634. case EVENT_DSI0_SOF:
  4635. len = snprintf(buf, buf_len, "disp_dsi0_sof%d",
  4636. drm_crtc_index(&mtk_crtc->base));
  4637. break;
  4638. default:
  4639. DDPPR_ERR("%s invalid event_id:%d\n", __func__, event_id);
  4640. memset(output_comp, 0, sizeof(output_comp));
  4641. }
  4642. }
  4643. static void mtk_crtc_init_color_matrix_data_slot(
  4644. struct mtk_drm_crtc *mtk_crtc)
  4645. {
  4646. struct cmdq_pkt *cmdq_handle;
  4647. struct disp_ccorr_config ccorr_config = {.mode = 1,
  4648. .color_matrix = {
  4649. 1024, 0, 0, 0,
  4650. 0, 1024, 0, 0,
  4651. 0, 0, 1024, 0,
  4652. 0, 0, 0, 1024},
  4653. .featureFlag = false };
  4654. mtk_crtc_pkt_create(&cmdq_handle, &mtk_crtc->base,
  4655. mtk_crtc->gce_obj.client[CLIENT_CFG]);
  4656. mtk_crtc_backup_color_matrix_data(&mtk_crtc->base, &ccorr_config,
  4657. cmdq_handle);
  4658. cmdq_pkt_flush(cmdq_handle);
  4659. cmdq_pkt_destroy(cmdq_handle);
  4660. }
  4661. static void mtk_crtc_init_gce_obj(struct drm_device *drm_dev,
  4662. struct mtk_drm_crtc *mtk_crtc)
  4663. {
  4664. struct device *dev = drm_dev->dev;
  4665. struct mtk_drm_private *priv = drm_dev->dev_private;
  4666. struct cmdq_pkt_buffer *cmdq_buf;
  4667. char buf[50];
  4668. int len, index, i;
  4669. /* Load CRTC GCE client */
  4670. for (i = 0; i < CLIENT_TYPE_MAX; i++) {
  4671. DRM_INFO("%s(), %s, %d", __func__,
  4672. crtc_gce_client_str[i],
  4673. drm_crtc_index(&mtk_crtc->base));
  4674. len = snprintf(buf, sizeof(buf), "%s%d", crtc_gce_client_str[i],
  4675. drm_crtc_index(&mtk_crtc->base));
  4676. if (len < 0) {
  4677. /* Handle snprintf() error */
  4678. DDPPR_ERR("%s:snprintf error\n", __func__);
  4679. return;
  4680. }
  4681. index = of_property_match_string(dev->of_node,
  4682. "gce-client-names", buf);
  4683. if (index < 0) {
  4684. mtk_crtc->gce_obj.client[i] = NULL;
  4685. continue;
  4686. }
  4687. mtk_crtc->gce_obj.client[i] =
  4688. cmdq_mbox_create(dev, index);
  4689. if (i != CLIENT_SEC_CFG)
  4690. continue;
  4691. if (drm_crtc_index(&mtk_crtc->base) == 0)
  4692. continue;
  4693. /* crtc1 & crtc2 share same secure gce thread */
  4694. if (priv->ext_sec_client == NULL)
  4695. priv->ext_sec_client = mtk_crtc->gce_obj.client[i];
  4696. else
  4697. mtk_crtc->gce_obj.client[i] = priv->ext_sec_client;
  4698. }
  4699. /* Load CRTC GCE event */
  4700. for (i = 0; i < EVENT_TYPE_MAX; i++) {
  4701. mtk_crtc_get_event_name(mtk_crtc, buf, sizeof(buf), i);
  4702. mtk_crtc->gce_obj.event[i] = cmdq_dev_get_event(dev, buf);
  4703. }
  4704. cmdq_buf = &(mtk_crtc->gce_obj.buf);
  4705. if (mtk_crtc->gce_obj.client[CLIENT_CFG]) {
  4706. DDPINFO("[CRTC][CHECK-1]0x%p\n",
  4707. mtk_crtc->gce_obj.client[CLIENT_CFG]);
  4708. if (mtk_crtc->gce_obj.client[CLIENT_CFG]->chan) {
  4709. DDPINFO("[CRTC][CHECK-2]0x%p\n",
  4710. mtk_crtc->gce_obj.client[CLIENT_CFG]->chan);
  4711. if (mtk_crtc->gce_obj.client[CLIENT_CFG]->chan->mbox) {
  4712. DDPINFO("[CRTC][CHECK-3]0x%p\n",
  4713. mtk_crtc->gce_obj.client[CLIENT_CFG]
  4714. ->chan->mbox);
  4715. if (mtk_crtc->gce_obj.client[CLIENT_CFG]
  4716. ->chan->mbox->dev) {
  4717. DDPINFO("[CRTC][CHECK-4]0x%p\n",
  4718. mtk_crtc->gce_obj
  4719. .client[CLIENT_CFG]
  4720. ->chan->mbox->dev);
  4721. }
  4722. }
  4723. }
  4724. }
  4725. cmdq_buf->va_base = cmdq_mbox_buf_alloc(
  4726. mtk_crtc->gce_obj.client[CLIENT_CFG]->chan->mbox->dev,
  4727. &(cmdq_buf->pa_base));
  4728. if (!cmdq_buf->va_base) {
  4729. DDPPR_ERR("va base is NULL\n");
  4730. return;
  4731. }
  4732. memset(cmdq_buf->va_base, 0, DISP_SLOT_SIZE);
  4733. mtk_crtc_init_color_matrix_data_slot(mtk_crtc);
  4734. mtk_crtc->gce_obj.base = cmdq_register_device(dev);
  4735. }
  4736. void mtk_drm_fake_vsync_switch(struct drm_crtc *crtc, bool enable)
  4737. {
  4738. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  4739. struct mtk_drm_fake_vsync *fake_vsync = mtk_crtc->fake_vsync;
  4740. if (drm_crtc_index(crtc) != 0 || mtk_drm_lcm_is_connect() ||
  4741. !mtk_crtc_is_frame_trigger_mode(crtc))
  4742. return;
  4743. if (unlikely(!fake_vsync)) {
  4744. DDPPR_ERR("%s:invalid fake_vsync pointer\n", __func__);
  4745. return;
  4746. }
  4747. atomic_set(&fake_vsync->fvsync_active, enable);
  4748. if (enable)
  4749. wake_up_interruptible(&fake_vsync->fvsync_wq);
  4750. }
  4751. static int mtk_drm_fake_vsync_kthread(void *data)
  4752. {
  4753. struct sched_param param = {.sched_priority = 87 };
  4754. struct drm_crtc *crtc = (struct drm_crtc *)data;
  4755. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  4756. struct mtk_drm_fake_vsync *fake_vsync = mtk_crtc->fake_vsync;
  4757. int ret = 0;
  4758. sched_setscheduler(current, SCHED_RR, &param);
  4759. while (1) {
  4760. ret = wait_event_interruptible(fake_vsync->fvsync_wq,
  4761. atomic_read(&fake_vsync->fvsync_active));
  4762. mtk_crtc_vblank_irq(crtc);
  4763. usleep_range(16700, 17700);
  4764. if (kthread_should_stop())
  4765. break;
  4766. }
  4767. return 0;
  4768. }
  4769. void mtk_drm_fake_vsync_init(struct drm_crtc *crtc)
  4770. {
  4771. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  4772. struct mtk_drm_fake_vsync *fake_vsync =
  4773. kzalloc(sizeof(struct mtk_drm_fake_vsync), GFP_KERNEL);
  4774. const int len = 50;
  4775. char name[len];
  4776. if (drm_crtc_index(crtc) != 0 || mtk_drm_lcm_is_connect() ||
  4777. !mtk_crtc_is_frame_trigger_mode(crtc)) {
  4778. kfree(fake_vsync);
  4779. return;
  4780. }
  4781. snprintf(name, len, "mtk_drm_fake_vsync:%d", drm_crtc_index(crtc));
  4782. fake_vsync->fvsync_task = kthread_create(mtk_drm_fake_vsync_kthread,
  4783. crtc, name);
  4784. init_waitqueue_head(&fake_vsync->fvsync_wq);
  4785. atomic_set(&fake_vsync->fvsync_active, 1);
  4786. mtk_crtc->fake_vsync = fake_vsync;
  4787. wake_up_process(fake_vsync->fvsync_task);
  4788. }
  4789. static int dc_main_path_commit_thread(void *data)
  4790. {
  4791. int ret;
  4792. struct sched_param param = {.sched_priority = 94 };
  4793. struct drm_crtc *crtc = data;
  4794. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  4795. sched_setscheduler(current, SCHED_RR, &param);
  4796. while (1) {
  4797. ret = wait_event_interruptible(mtk_crtc->dc_main_path_commit_wq,
  4798. atomic_read(&mtk_crtc->dc_main_path_commit_event));
  4799. if (ret == 0) {
  4800. atomic_set(&mtk_crtc->dc_main_path_commit_event, 0);
  4801. mtk_crtc_dc_prim_path_update(crtc);
  4802. } else {
  4803. DDPINFO("wait dc commit event interrupted, ret = %d\n",
  4804. ret);
  4805. }
  4806. if (kthread_should_stop())
  4807. break;
  4808. }
  4809. return 0;
  4810. }
  4811. int mtk_drm_crtc_create(struct drm_device *drm_dev,
  4812. const struct mtk_crtc_path_data *path_data)
  4813. {
  4814. struct mtk_drm_private *priv = drm_dev->dev_private;
  4815. struct device *dev = drm_dev->dev;
  4816. struct mtk_drm_crtc *mtk_crtc;
  4817. struct mtk_ddp_comp *output_comp;
  4818. enum drm_plane_type type;
  4819. unsigned int zpos;
  4820. int pipe = priv->num_pipes;
  4821. int ret;
  4822. int i, j, p_mode;
  4823. #ifdef MTK_FB_MMDVFS_SUPPORT
  4824. u32 result;
  4825. #endif
  4826. enum mtk_ddp_comp_id comp_id;
  4827. DDPMSG("%s+\n", __func__);
  4828. if (!path_data)
  4829. return 0;
  4830. for_each_comp_id_in_path_data(comp_id, path_data, i, j, p_mode) {
  4831. struct device_node *node;
  4832. if (mtk_ddp_comp_get_type(comp_id) == MTK_DISP_VIRTUAL)
  4833. continue;
  4834. if (comp_id < 0) {
  4835. DDPPR_ERR("%s: Invalid comp_id:%d\n", __func__, comp_id);
  4836. return 0;
  4837. }
  4838. node = priv->comp_node[comp_id];
  4839. if (!node) {
  4840. dev_info(
  4841. dev,
  4842. "Not creating crtc %d because component %d is disabled or missing\n",
  4843. pipe, comp_id);
  4844. DDPPR_ERR(
  4845. "Not creating crtc %d because component %d is disabled or missing\n",
  4846. pipe, comp_id);
  4847. return 0;
  4848. }
  4849. }
  4850. mtk_crtc = devm_kzalloc(dev, sizeof(*mtk_crtc), GFP_KERNEL);
  4851. if (!mtk_crtc)
  4852. return -ENOMEM;
  4853. // TODO: It should use platform_driverdata or device tree to define it.
  4854. // It's just for P90 temp workaround, will be modifyed later.
  4855. if (pipe == 0)
  4856. mtk_crtc->layer_nr = OVL_LAYER_NR;
  4857. else if (pipe == 1)
  4858. mtk_crtc->layer_nr = EXTERNAL_INPUT_LAYER_NR;
  4859. else if (pipe == 2)
  4860. mtk_crtc->layer_nr = MEMORY_INPUT_LAYER_NR;
  4861. mutex_init(&mtk_crtc->lock);
  4862. mtk_crtc->config_regs = priv->config_regs;
  4863. mtk_crtc->config_regs_pa = priv->config_regs_pa;
  4864. mtk_crtc->mmsys_reg_data = priv->reg_data;
  4865. mtk_crtc->path_data = path_data;
  4866. mtk_crtc->is_dual_pipe = false;
  4867. for (i = 0; i < DDP_MODE_NR; i++) {
  4868. for (j = 0; j < DDP_PATH_NR; j++) {
  4869. mtk_crtc->ddp_ctx[i].ddp_comp_nr[j] =
  4870. path_data->path_len[i][j];
  4871. mtk_crtc->ddp_ctx[i].ddp_comp[j] = devm_kmalloc_array(
  4872. dev, path_data->path_len[i][j],
  4873. sizeof(struct mtk_ddp_comp *), GFP_KERNEL);
  4874. mtk_crtc->ddp_ctx[i].req_hrt[j] =
  4875. path_data->path_req_hrt[i][j];
  4876. }
  4877. mtk_crtc->ddp_ctx[i].wb_comp_nr = path_data->wb_path_len[i];
  4878. mtk_crtc->ddp_ctx[i].wb_comp = devm_kmalloc_array(
  4879. dev, path_data->wb_path_len[i],
  4880. sizeof(struct mtk_ddp_comp *), GFP_KERNEL);
  4881. }
  4882. for (i = 0; i < DDP_PATH_NR; i++) {
  4883. mtk_crtc->mutex[i] = mtk_disp_mutex_get(priv->mutex_dev,
  4884. pipe * DDP_PATH_NR + i);
  4885. if (IS_ERR(mtk_crtc->mutex[i])) {
  4886. ret = PTR_ERR(mtk_crtc->mutex[i]);
  4887. dev_err(dev, "Failed to get mutex: %d\n", ret);
  4888. return ret;
  4889. }
  4890. }
  4891. for_each_comp_id_in_path_data(comp_id, path_data, i, j, p_mode) {
  4892. struct mtk_ddp_comp *comp;
  4893. struct device_node *node;
  4894. bool *rdma_memory_mode;
  4895. if (comp_id < 0) {
  4896. DDPPR_ERR("%s: Invalid comp_id:%d\n", __func__, comp_id);
  4897. return 0;
  4898. }
  4899. if (mtk_ddp_comp_get_type(comp_id) == MTK_DISP_VIRTUAL) {
  4900. struct mtk_ddp_comp *comp;
  4901. comp = kzalloc(sizeof(*comp), GFP_KERNEL);
  4902. comp->id = comp_id;
  4903. mtk_crtc->ddp_ctx[p_mode].ddp_comp[i][j] = comp;
  4904. continue;
  4905. }
  4906. node = priv->comp_node[comp_id];
  4907. comp = priv->ddp_comp[comp_id];
  4908. if (!comp) {
  4909. dev_err(dev, "Component %s not initialized\n",
  4910. node->full_name);
  4911. return -ENODEV;
  4912. }
  4913. if ((p_mode == DDP_MAJOR) && (comp_id == DDP_COMPONENT_WDMA0 ||
  4914. comp_id == DDP_COMPONENT_WDMA1)) {
  4915. ret = mtk_wb_connector_init(drm_dev, mtk_crtc);
  4916. if (ret != 0)
  4917. return ret;
  4918. ret = mtk_wb_set_possible_crtcs(drm_dev, mtk_crtc,
  4919. BIT(pipe));
  4920. if (ret != 0)
  4921. return ret;
  4922. }
  4923. if ((j == 0) && (p_mode == DDP_MAJOR) &&
  4924. (comp_id == DDP_COMPONENT_RDMA0 ||
  4925. comp_id == DDP_COMPONENT_RDMA1 ||
  4926. comp_id == DDP_COMPONENT_RDMA2 ||
  4927. comp_id == DDP_COMPONENT_RDMA3 ||
  4928. comp_id == DDP_COMPONENT_RDMA4 ||
  4929. comp_id == DDP_COMPONENT_RDMA5)) {
  4930. rdma_memory_mode = comp->comp_mode;
  4931. *rdma_memory_mode = true;
  4932. mtk_crtc->layer_nr = RDMA_LAYER_NR;
  4933. }
  4934. mtk_crtc->ddp_ctx[p_mode].ddp_comp[i][j] = comp;
  4935. }
  4936. for_each_wb_comp_id_in_path_data(comp_id, path_data, i, p_mode) {
  4937. struct mtk_ddp_comp *comp;
  4938. struct device_node *node;
  4939. if (comp_id < 0) {
  4940. DDPPR_ERR("%s: Invalid comp_id:%d\n", __func__, comp_id);
  4941. return 0;
  4942. }
  4943. if (mtk_ddp_comp_get_type(comp_id) == MTK_DISP_VIRTUAL) {
  4944. struct mtk_ddp_comp *comp;
  4945. comp = kzalloc(sizeof(*comp), GFP_KERNEL);
  4946. comp->id = comp_id;
  4947. mtk_crtc->ddp_ctx[p_mode].wb_comp[i] = comp;
  4948. continue;
  4949. }
  4950. node = priv->comp_node[comp_id];
  4951. comp = priv->ddp_comp[comp_id];
  4952. if (!comp) {
  4953. dev_err(dev, "Component %s not initialized\n",
  4954. node->full_name);
  4955. return -ENODEV;
  4956. }
  4957. mtk_crtc->ddp_ctx[p_mode].wb_comp[i] = comp;
  4958. }
  4959. mtk_crtc->planes = devm_kzalloc(dev,
  4960. mtk_crtc->layer_nr * sizeof(struct mtk_drm_plane),
  4961. GFP_KERNEL);
  4962. if (!mtk_crtc->planes)
  4963. return -ENOMEM;
  4964. for (zpos = 0; zpos < mtk_crtc->layer_nr; zpos++) {
  4965. type = (zpos == 0) ? DRM_PLANE_TYPE_PRIMARY
  4966. : (zpos == (mtk_crtc->layer_nr - 1UL))
  4967. ? DRM_PLANE_TYPE_CURSOR
  4968. : DRM_PLANE_TYPE_OVERLAY;
  4969. ret = mtk_plane_init(drm_dev, &mtk_crtc->planes[zpos], zpos,
  4970. BIT(pipe), type);
  4971. mtk_crtc->planes[zpos].base.crtc = &mtk_crtc->base;
  4972. if (ret)
  4973. return ret;
  4974. }
  4975. if (mtk_crtc->layer_nr == 1UL) {
  4976. ret = mtk_drm_crtc_init(drm_dev, mtk_crtc,
  4977. &mtk_crtc->planes[0].base, NULL, pipe);
  4978. } else {
  4979. ret = mtk_drm_crtc_init(
  4980. drm_dev, mtk_crtc, &mtk_crtc->planes[0].base,
  4981. &mtk_crtc->planes[mtk_crtc->layer_nr - 1UL].base, pipe);
  4982. }
  4983. if (ret < 0)
  4984. return ret;
  4985. output_comp = mtk_ddp_comp_request_output(mtk_crtc);
  4986. if (output_comp)
  4987. mtk_ddp_comp_io_cmd(output_comp, NULL, REQ_PANEL_EXT,
  4988. &mtk_crtc->panel_ext);
  4989. drm_mode_crtc_set_gamma_size(&mtk_crtc->base, MTK_LUT_SIZE);
  4990. /* TODO: Skip color mgmt first */
  4991. // drm_crtc_enable_color_mgmt(&mtk_crtc->base, 0, false, MTK_LUT_SIZE);
  4992. priv->crtc[pipe] = &mtk_crtc->base;
  4993. priv->num_pipes++;
  4994. dma_set_coherent_mask(dev, DMA_BIT_MASK(32));
  4995. mtk_crtc_init_gce_obj(drm_dev, mtk_crtc);
  4996. mtk_crtc->vblank_en = 1;
  4997. if (mtk_drm_helper_get_opt(priv->helper_opt, MTK_DRM_OPT_IDLE_MGR) &&
  4998. drm_crtc_index(&mtk_crtc->base) == 0) {
  4999. char name[50];
  5000. mtk_drm_idlemgr_init(&mtk_crtc->base,
  5001. drm_crtc_index(&mtk_crtc->base));
  5002. snprintf(name, sizeof(name), "enable_vblank");
  5003. mtk_crtc->vblank_enable_task = kthread_create(
  5004. mtk_crtc_enable_vblank_thread, priv->crtc[pipe], name);
  5005. init_waitqueue_head(&mtk_crtc->vblank_enable_wq);
  5006. wake_up_process(mtk_crtc->vblank_enable_task);
  5007. }
  5008. init_waitqueue_head(&mtk_crtc->crtc_status_wq);
  5009. if (drm_crtc_index(&mtk_crtc->base) == 0) {
  5010. mtk_disp_hrt_cond_init(&mtk_crtc->base);
  5011. atomic_set(&mtk_crtc->qos_ctx->last_hrt_idx, 0);
  5012. mtk_crtc->qos_ctx->last_hrt_req = 0;
  5013. init_waitqueue_head(&mtk_crtc->qos_ctx->hrt_cond_wq);
  5014. }
  5015. mtk_disp_chk_recover_init(&mtk_crtc->base);
  5016. mtk_drm_fake_vsync_init(&mtk_crtc->base);
  5017. #ifdef MTK_FB_MMDVFS_SUPPORT
  5018. if (drm_crtc_index(&mtk_crtc->base) == 1) {
  5019. pm_qos_add_request(&mm_freq_request, PM_QOS_DISP_FREQ,
  5020. PM_QOS_MM_FREQ_DEFAULT_VALUE);
  5021. result = mmdvfs_qos_get_freq_steps(PM_QOS_DISP_FREQ,
  5022. freq_steps, &step_size);
  5023. if (result < 0)
  5024. DDPPR_ERR("Get mmdvfs steps fail, result:%d\n", result);
  5025. }
  5026. #endif
  5027. if (mtk_crtc_support_dc_mode(&mtk_crtc->base)) {
  5028. mtk_crtc->dc_main_path_commit_task = kthread_create(
  5029. dc_main_path_commit_thread,
  5030. &mtk_crtc->base, "decouple_update_rdma_cfg");
  5031. atomic_set(&mtk_crtc->dc_main_path_commit_event, 1);
  5032. init_waitqueue_head(&mtk_crtc->dc_main_path_commit_wq);
  5033. wake_up_process(mtk_crtc->dc_main_path_commit_task);
  5034. }
  5035. if (drm_crtc_index(&mtk_crtc->base) == 0) {
  5036. init_waitqueue_head(&mtk_crtc->trigger_event);
  5037. mtk_crtc->trigger_event_task =
  5038. kthread_create(_mtk_crtc_check_trigger,
  5039. mtk_crtc, "ddp_trig");
  5040. wake_up_process(mtk_crtc->trigger_event_task);
  5041. init_waitqueue_head(&mtk_crtc->trigger_delay);
  5042. mtk_crtc->trigger_delay_task =
  5043. kthread_create(_mtk_crtc_check_trigger_delay,
  5044. mtk_crtc, "ddp_trig_d");
  5045. wake_up_process(mtk_crtc->trigger_delay_task);
  5046. /* For protect crtc blank state */
  5047. mutex_init(&mtk_crtc->blank_lock);
  5048. init_waitqueue_head(&mtk_crtc->state_wait_queue);
  5049. }
  5050. /* init wakelock resources */
  5051. {
  5052. unsigned int len = 21;
  5053. mtk_crtc->wk_lock_name = vzalloc(len * sizeof(char));
  5054. snprintf(mtk_crtc->wk_lock_name, len * sizeof(char),
  5055. "disp_crtc%u_wakelock",
  5056. drm_crtc_index(&mtk_crtc->base));
  5057. wakeup_source_init(&mtk_crtc->wk_lock, mtk_crtc->wk_lock_name);
  5058. }
  5059. DDPMSG("%s-CRTC%d create successfully\n", __func__,
  5060. priv->num_pipes - 1);
  5061. return 0;
  5062. }
  5063. int mtk_drm_crtc_getfence_ioctl(struct drm_device *dev, void *data,
  5064. struct drm_file *file_priv)
  5065. {
  5066. int ret = 0;
  5067. struct drm_crtc *crtc;
  5068. struct drm_mtk_fence *args = data;
  5069. struct mtk_drm_private *private;
  5070. struct fence_data fence;
  5071. unsigned int fence_idx;
  5072. struct mtk_fence_info *l_info = NULL;
  5073. int tl, idx;
  5074. crtc = drm_crtc_find(dev, file_priv, args->crtc_id);
  5075. if (!crtc) {
  5076. DDPPR_ERR("Unknown CRTC ID %d\n", args->crtc_id);
  5077. ret = -ENOENT;
  5078. return ret;
  5079. }
  5080. DDPDBG("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
  5081. idx = drm_crtc_index(crtc);
  5082. if (!crtc->dev) {
  5083. DDPPR_ERR("%s:%d dev is null\n", __func__, __LINE__);
  5084. ret = -EFAULT;
  5085. return ret;
  5086. }
  5087. if (!crtc->dev->dev_private) {
  5088. DDPPR_ERR("%s:%d dev private is null\n", __func__, __LINE__);
  5089. ret = -EFAULT;
  5090. return ret;
  5091. }
  5092. private = crtc->dev->dev_private;
  5093. fence_idx = atomic_read(&private->crtc_present[idx]);
  5094. tl = mtk_fence_get_present_timeline_id(mtk_get_session_id(crtc));
  5095. l_info = mtk_fence_get_layer_info(mtk_get_session_id(crtc), tl);
  5096. if (!l_info) {
  5097. DDPPR_ERR("%s:%d layer_info is null\n", __func__, __LINE__);
  5098. ret = -EFAULT;
  5099. return ret;
  5100. }
  5101. /* create fence */
  5102. fence.fence = MTK_INVALID_FENCE_FD;
  5103. fence.value = ++fence_idx;
  5104. atomic_inc(&private->crtc_present[idx]);
  5105. ret = mtk_sync_fence_create(l_info->timeline, &fence);
  5106. if (ret) {
  5107. DDPPR_ERR("%d,L%d create Fence Object failed!\n",
  5108. MTK_SESSION_DEV(mtk_get_session_id(crtc)), tl);
  5109. ret = -EFAULT;
  5110. }
  5111. args->fence_fd = fence.fence;
  5112. args->fence_idx = fence.value;
  5113. DDPFENCE("P+/%d/L%d/idx%d/fd%d\n",
  5114. MTK_SESSION_DEV(mtk_get_session_id(crtc)),
  5115. tl, args->fence_idx,
  5116. args->fence_fd);
  5117. return ret;
  5118. }
  5119. static int __crtc_need_composition_wb(struct drm_crtc *crtc)
  5120. {
  5121. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  5122. struct mtk_crtc_ddp_ctx *ddp_ctx;
  5123. ddp_ctx = &mtk_crtc->ddp_ctx[mtk_crtc->ddp_mode];
  5124. if (ddp_ctx->wb_comp_nr != 0)
  5125. return true;
  5126. else
  5127. return false;
  5128. }
  5129. static void mtk_crtc_disconnect_single_path_cmdq(struct drm_crtc *crtc,
  5130. struct cmdq_pkt *cmdq_handle,
  5131. unsigned int path_idx,
  5132. unsigned int ddp_mode,
  5133. unsigned int mutex_id)
  5134. {
  5135. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  5136. struct mtk_crtc_ddp_ctx *ddp_ctx;
  5137. int i;
  5138. ddp_ctx = &mtk_crtc->ddp_ctx[ddp_mode];
  5139. for (i = 0; i < ddp_ctx->ddp_comp_nr[path_idx] - 1; i++)
  5140. mtk_ddp_remove_comp_from_path_with_cmdq(
  5141. mtk_crtc, ddp_ctx->ddp_comp[path_idx][i]->id,
  5142. ddp_ctx->ddp_comp[path_idx][i + 1]->id, cmdq_handle);
  5143. for (i = 0; i < ddp_ctx->ddp_comp_nr[path_idx]; i++)
  5144. mtk_disp_mutex_remove_comp_with_cmdq(
  5145. mtk_crtc, ddp_ctx->ddp_comp[path_idx][i]->id,
  5146. cmdq_handle, mutex_id);
  5147. }
  5148. static void mtk_crtc_connect_single_path_cmdq(struct drm_crtc *crtc,
  5149. struct cmdq_pkt *cmdq_handle,
  5150. unsigned int path_idx,
  5151. unsigned int ddp_mode,
  5152. unsigned int mutex_id)
  5153. {
  5154. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  5155. struct mtk_crtc_ddp_ctx *ddp_ctx;
  5156. int i;
  5157. ddp_ctx = &mtk_crtc->ddp_ctx[ddp_mode];
  5158. for (i = 0; i < ddp_ctx->ddp_comp_nr[path_idx] - 1; i++)
  5159. mtk_ddp_add_comp_to_path_with_cmdq(
  5160. mtk_crtc, ddp_ctx->ddp_comp[path_idx][i]->id,
  5161. ddp_ctx->ddp_comp[path_idx][i + 1]->id, cmdq_handle);
  5162. for (i = 0; i < ddp_ctx->ddp_comp_nr[path_idx]; i++)
  5163. mtk_disp_mutex_add_comp_with_cmdq(
  5164. mtk_crtc, ddp_ctx->ddp_comp[path_idx][i]->id,
  5165. mtk_crtc_is_frame_trigger_mode(&mtk_crtc->base),
  5166. cmdq_handle, mutex_id);
  5167. }
  5168. static void mtk_crtc_config_dual_pipe_cmdq(struct mtk_drm_crtc *mtk_crtc,
  5169. struct cmdq_pkt *cmdq_handle,
  5170. unsigned int path_idx,
  5171. struct mtk_ddp_config *cfg)
  5172. {
  5173. //struct mtk_drm_private *priv = mtk_crtc->base.dev->dev_private;
  5174. struct mtk_crtc_ddp_ctx *ddp_ctx;
  5175. int i;
  5176. DDPFUNC();
  5177. ddp_ctx = &mtk_crtc->dual_pipe_ddp_ctx;
  5178. for (i = 0; i < ddp_ctx->ddp_comp_nr[path_idx]; i++) {
  5179. struct mtk_ddp_comp *comp = ddp_ctx->ddp_comp[path_idx][i];
  5180. if (comp->id == DDP_COMPONENT_RDMA4 ||
  5181. comp->id == DDP_COMPONENT_RDMA5) {
  5182. bool *rdma_memory_mode =
  5183. ddp_ctx->ddp_comp[path_idx][i]->comp_mode;
  5184. *rdma_memory_mode = false;
  5185. break;
  5186. }
  5187. }
  5188. for (i = 0; i < ddp_ctx->ddp_comp_nr[path_idx]; i++) {
  5189. struct mtk_ddp_comp *comp = ddp_ctx->ddp_comp[path_idx][i];
  5190. mtk_ddp_comp_config(comp, cfg, cmdq_handle);
  5191. mtk_ddp_comp_start(comp, cmdq_handle);
  5192. }
  5193. }
  5194. static void mtk_crtc_config_single_path_cmdq(struct drm_crtc *crtc,
  5195. struct cmdq_pkt *cmdq_handle,
  5196. unsigned int path_idx,
  5197. unsigned int ddp_mode,
  5198. struct mtk_ddp_config *cfg)
  5199. {
  5200. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  5201. struct mtk_drm_private *priv = crtc->dev->dev_private;
  5202. struct mtk_crtc_ddp_ctx *ddp_ctx;
  5203. int i;
  5204. ddp_ctx = &mtk_crtc->ddp_ctx[ddp_mode];
  5205. for (i = 0; i < ddp_ctx->ddp_comp_nr[path_idx]; i++) {
  5206. struct mtk_ddp_comp *comp = ddp_ctx->ddp_comp[path_idx][i];
  5207. if (comp->id == DDP_COMPONENT_RDMA0 ||
  5208. comp->id == DDP_COMPONENT_RDMA1 ||
  5209. comp->id == DDP_COMPONENT_RDMA2 ||
  5210. comp->id == DDP_COMPONENT_RDMA4 ||
  5211. comp->id == DDP_COMPONENT_RDMA5) {
  5212. bool *rdma_memory_mode =
  5213. ddp_ctx->ddp_comp[path_idx][i]->comp_mode;
  5214. if (i == 0)
  5215. *rdma_memory_mode = true;
  5216. else
  5217. *rdma_memory_mode = false;
  5218. break;
  5219. }
  5220. }
  5221. for (i = 0; i < ddp_ctx->ddp_comp_nr[path_idx]; i++) {
  5222. struct mtk_ddp_comp *comp = ddp_ctx->ddp_comp[path_idx][i];
  5223. mtk_ddp_comp_config(comp, cfg, cmdq_handle);
  5224. mtk_ddp_comp_start(comp, cmdq_handle);
  5225. if (!mtk_drm_helper_get_opt(
  5226. priv->helper_opt,
  5227. MTK_DRM_OPT_USE_PQ))
  5228. mtk_ddp_comp_bypass(comp, cmdq_handle);
  5229. }
  5230. if (mtk_crtc->is_dual_pipe)
  5231. mtk_crtc_config_dual_pipe_cmdq(mtk_crtc, cmdq_handle,
  5232. path_idx, cfg);
  5233. }
  5234. static void mtk_crtc_create_wb_path_cmdq(struct drm_crtc *crtc,
  5235. struct cmdq_pkt *cmdq_handle,
  5236. unsigned int ddp_mode,
  5237. unsigned int mutex_id,
  5238. struct mtk_ddp_config *cfg)
  5239. {
  5240. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  5241. struct mtk_drm_private *priv = crtc->dev->dev_private;
  5242. struct mtk_crtc_ddp_ctx *ddp_ctx;
  5243. struct mtk_drm_gem_obj *mtk_gem;
  5244. struct mtk_ddp_comp *comp;
  5245. dma_addr_t addr;
  5246. struct cmdq_pkt_buffer *cmdq_buf = &(mtk_crtc->gce_obj.buf);
  5247. int i;
  5248. ddp_ctx = &mtk_crtc->ddp_ctx[ddp_mode];
  5249. for (i = 0; i < ddp_ctx->wb_comp_nr - 1; i++)
  5250. mtk_ddp_add_comp_to_path_with_cmdq(
  5251. mtk_crtc, ddp_ctx->wb_comp[i]->id,
  5252. ddp_ctx->wb_comp[i + 1]->id, cmdq_handle);
  5253. for (i = 0; i < ddp_ctx->wb_comp_nr; i++)
  5254. mtk_disp_mutex_add_comp_with_cmdq(
  5255. mtk_crtc, ddp_ctx->wb_comp[i]->id,
  5256. mtk_crtc_is_frame_trigger_mode(&mtk_crtc->base),
  5257. cmdq_handle, mutex_id);
  5258. if (!ddp_ctx->wb_fb) {
  5259. struct drm_mode_fb_cmd2 mode = {0};
  5260. mode.width = crtc->state->adjusted_mode.hdisplay;
  5261. mode.height = crtc->state->adjusted_mode.vdisplay;
  5262. mtk_gem = mtk_drm_gem_create(
  5263. crtc->dev, mode.width * mode.height * 3, true);
  5264. mode.pixel_format = DRM_FORMAT_RGB888;
  5265. mode.pitches[0] = mode.width * 3;
  5266. ddp_ctx->wb_fb = mtk_drm_framebuffer_create(
  5267. crtc->dev, &mode, &mtk_gem->base);
  5268. }
  5269. for (i = 0; i < ddp_ctx->wb_comp_nr; i++) {
  5270. comp = ddp_ctx->wb_comp[i];
  5271. if (comp->id == DDP_COMPONENT_WDMA0 ||
  5272. comp->id == DDP_COMPONENT_WDMA1) {
  5273. comp->fb = ddp_ctx->wb_fb;
  5274. break;
  5275. }
  5276. }
  5277. /* All the 1to2 path shoulbe be real-time */
  5278. for (i = 0; i < ddp_ctx->wb_comp_nr; i++) {
  5279. struct mtk_ddp_comp *comp = ddp_ctx->wb_comp[i];
  5280. mtk_ddp_comp_config(comp, cfg, cmdq_handle);
  5281. mtk_ddp_comp_start(comp, cmdq_handle);
  5282. if (!mtk_drm_helper_get_opt(
  5283. priv->helper_opt,
  5284. MTK_DRM_OPT_USE_PQ))
  5285. mtk_ddp_comp_bypass(comp, cmdq_handle);
  5286. }
  5287. addr = cmdq_buf->pa_base + DISP_SLOT_RDMA_FB_ID;
  5288. cmdq_pkt_write(cmdq_handle, mtk_crtc->gce_obj.base,
  5289. addr, 0, ~0);
  5290. }
  5291. static void mtk_crtc_destroy_wb_path_cmdq(struct drm_crtc *crtc,
  5292. struct cmdq_pkt *cmdq_handle,
  5293. unsigned int mutex_id,
  5294. struct mtk_ddp_config *cfg)
  5295. {
  5296. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  5297. struct mtk_crtc_ddp_ctx *ddp_ctx;
  5298. int i;
  5299. if (!__crtc_need_composition_wb(crtc))
  5300. return;
  5301. ddp_ctx = &mtk_crtc->ddp_ctx[mtk_crtc->ddp_mode];
  5302. for (i = 0; i < ddp_ctx->wb_comp_nr - 1; i++)
  5303. mtk_ddp_remove_comp_from_path_with_cmdq(
  5304. mtk_crtc, ddp_ctx->wb_comp[i]->id,
  5305. ddp_ctx->wb_comp[i + 1]->id, cmdq_handle);
  5306. for (i = 0; i < ddp_ctx->wb_comp_nr; i++)
  5307. mtk_disp_mutex_remove_comp_with_cmdq(mtk_crtc,
  5308. ddp_ctx->wb_comp[i]->id,
  5309. cmdq_handle, mutex_id);
  5310. }
  5311. static void mtk_crtc_config_wb_path_cmdq(struct drm_crtc *crtc,
  5312. struct cmdq_pkt *cmdq_handle,
  5313. unsigned int path_idx,
  5314. unsigned int ddp_mode)
  5315. {
  5316. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  5317. struct mtk_crtc_ddp_ctx *ddp_ctx;
  5318. struct mtk_plane_state plane_state;
  5319. struct drm_framebuffer *fb = NULL;
  5320. struct mtk_ddp_comp *comp;
  5321. int i;
  5322. if (!__crtc_need_composition_wb(crtc))
  5323. return;
  5324. ddp_ctx = &mtk_crtc->ddp_ctx[mtk_crtc->ddp_mode];
  5325. for (i = 0; i < ddp_ctx->wb_comp_nr; i++) {
  5326. comp = ddp_ctx->wb_comp[i];
  5327. if (comp->id == DDP_COMPONENT_WDMA0 ||
  5328. comp->id == DDP_COMPONENT_WDMA1) {
  5329. fb = comp->fb;
  5330. break;
  5331. }
  5332. }
  5333. if (!fb) {
  5334. DDPPR_ERR("%s, fb is empty\n", __func__);
  5335. return;
  5336. }
  5337. plane_state.pending.enable = true;
  5338. plane_state.pending.pitch = fb->pitches[0];
  5339. plane_state.pending.format = fb->format->format;
  5340. plane_state.pending.addr = mtk_fb_get_dma(fb);
  5341. plane_state.pending.size = mtk_fb_get_size(fb);
  5342. plane_state.pending.src_x = 0;
  5343. plane_state.pending.src_y = 0;
  5344. plane_state.pending.dst_x = 0;
  5345. plane_state.pending.dst_y = 0;
  5346. plane_state.pending.width = fb->width;
  5347. plane_state.pending.height = fb->height;
  5348. ddp_ctx = &mtk_crtc->ddp_ctx[ddp_mode];
  5349. mtk_ddp_comp_layer_config(ddp_ctx->ddp_comp[path_idx][0], 0,
  5350. &plane_state, cmdq_handle);
  5351. }
  5352. static int __mtk_crtc_composition_wb(
  5353. struct drm_crtc *crtc,
  5354. unsigned int ddp_mode,
  5355. struct mtk_ddp_config *cfg)
  5356. {
  5357. struct cmdq_pkt *cmdq_handle;
  5358. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  5359. int gce_event;
  5360. if (!__crtc_need_composition_wb(crtc))
  5361. return 0;
  5362. DDPINFO("%s\n", __func__);
  5363. gce_event = get_path_wait_event(mtk_crtc, mtk_crtc->ddp_mode);
  5364. mtk_crtc_pkt_create(&cmdq_handle, crtc,
  5365. mtk_crtc->gce_obj.client[CLIENT_CFG]);
  5366. mtk_crtc_wait_frame_done(mtk_crtc, cmdq_handle, DDP_FIRST_PATH, 0);
  5367. mtk_crtc_create_wb_path_cmdq(crtc, cmdq_handle, mtk_crtc->ddp_mode, 0,
  5368. cfg);
  5369. if (mtk_crtc_is_frame_trigger_mode(&mtk_crtc->base))
  5370. cmdq_pkt_set_event(cmdq_handle,
  5371. mtk_crtc->gce_obj.event[EVENT_STREAM_DIRTY]);
  5372. if (gce_event > 0) {
  5373. cmdq_pkt_clear_event(cmdq_handle, gce_event);
  5374. cmdq_pkt_wait_no_clear(cmdq_handle, gce_event);
  5375. }
  5376. cmdq_pkt_flush(cmdq_handle);
  5377. cmdq_pkt_destroy(cmdq_handle);
  5378. return 0;
  5379. }
  5380. bool mtk_crtc_with_sub_path(struct drm_crtc *crtc, unsigned int ddp_mode)
  5381. {
  5382. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  5383. return mtk_crtc->ddp_ctx[ddp_mode].ddp_comp_nr[1];
  5384. }
  5385. static void __mtk_crtc_prim_path_switch(struct drm_crtc *crtc,
  5386. unsigned int ddp_mode,
  5387. struct mtk_ddp_config *cfg)
  5388. {
  5389. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  5390. struct mtk_crtc_state *crtc_state = to_mtk_crtc_state(crtc->state);
  5391. struct cmdq_pkt *cmdq_handle;
  5392. int cur_path_idx, next_path_idx;
  5393. DDPINFO("%s\n", __func__);
  5394. if (mtk_crtc_with_sub_path(crtc, mtk_crtc->ddp_mode))
  5395. cur_path_idx = DDP_SECOND_PATH;
  5396. else
  5397. cur_path_idx = DDP_FIRST_PATH;
  5398. if (mtk_crtc_with_sub_path(crtc, ddp_mode))
  5399. next_path_idx = DDP_SECOND_PATH;
  5400. else
  5401. next_path_idx = DDP_FIRST_PATH;
  5402. mtk_crtc_pkt_create(&cmdq_handle,
  5403. crtc, mtk_crtc->gce_obj.client[CLIENT_CFG]);
  5404. if (mtk_crtc_is_frame_trigger_mode(&mtk_crtc->base))
  5405. cmdq_pkt_clear_event(
  5406. cmdq_handle,
  5407. mtk_crtc->gce_obj.event[EVENT_STREAM_DIRTY]);
  5408. mtk_crtc_wait_frame_done(mtk_crtc, cmdq_handle, cur_path_idx, 0);
  5409. /* 1. Disconnect current path and remove component mutexs */
  5410. if (!mtk_crtc_with_sub_path(crtc, mtk_crtc->ddp_mode)) {
  5411. _mtk_crtc_atmoic_addon_module_disconnect(
  5412. crtc, mtk_crtc->ddp_mode, &crtc_state->lye_state,
  5413. cmdq_handle);
  5414. }
  5415. mtk_crtc_addon_connector_disconnect(crtc, cmdq_handle);
  5416. mtk_crtc_disconnect_single_path_cmdq(crtc, cmdq_handle, cur_path_idx,
  5417. mtk_crtc->ddp_mode, 0);
  5418. /* 2. Remove composition path and cooresonding mutexs */
  5419. mtk_crtc_destroy_wb_path_cmdq(crtc, cmdq_handle, 0, cfg);
  5420. /* 3. Connect new primary path and add component mutexs */
  5421. mtk_crtc_connect_single_path_cmdq(crtc, cmdq_handle, next_path_idx,
  5422. ddp_mode, 0);
  5423. /* TODO: refine addon_connector */
  5424. mtk_crtc_addon_connector_connect(crtc, cmdq_handle);
  5425. /* 4. Primary path configurations */
  5426. cfg->p_golden_setting_context =
  5427. __get_golden_setting_context(mtk_crtc);
  5428. if (mtk_crtc_target_is_dc_mode(crtc, ddp_mode))
  5429. cfg->p_golden_setting_context->is_dc = 1;
  5430. else
  5431. cfg->p_golden_setting_context->is_dc = 0;
  5432. mtk_crtc_config_single_path_cmdq(crtc, cmdq_handle, next_path_idx,
  5433. ddp_mode, cfg);
  5434. if (!mtk_crtc_with_sub_path(crtc, ddp_mode))
  5435. _mtk_crtc_atmoic_addon_module_connect(
  5436. crtc, ddp_mode, &crtc_state->lye_state, cmdq_handle);
  5437. /* 5. Set composed write back buffer */
  5438. mtk_crtc_config_wb_path_cmdq(crtc, cmdq_handle, next_path_idx,
  5439. ddp_mode);
  5440. if (mtk_crtc_is_frame_trigger_mode(&mtk_crtc->base))
  5441. cmdq_pkt_set_event(cmdq_handle,
  5442. mtk_crtc->gce_obj.event[EVENT_STREAM_DIRTY]);
  5443. cmdq_pkt_flush(cmdq_handle);
  5444. cmdq_pkt_destroy(cmdq_handle);
  5445. }
  5446. static void __mtk_crtc_old_sub_path_destroy(struct drm_crtc *crtc,
  5447. struct mtk_ddp_config *cfg)
  5448. {
  5449. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  5450. struct mtk_crtc_state *crtc_state = to_mtk_crtc_state(crtc->state);
  5451. struct cmdq_pkt *cmdq_handle;
  5452. struct mtk_crtc_ddp_ctx *ddp_ctx;
  5453. int i;
  5454. struct mtk_ddp_comp *comp = NULL;
  5455. int index = drm_crtc_index(crtc);
  5456. if (!mtk_crtc_with_sub_path(crtc, mtk_crtc->ddp_mode))
  5457. return;
  5458. mtk_crtc_pkt_create(&cmdq_handle, crtc,
  5459. mtk_crtc->gce_obj.client[CLIENT_CFG]);
  5460. mtk_crtc_wait_frame_done(mtk_crtc, cmdq_handle, DDP_FIRST_PATH, 0);
  5461. _mtk_crtc_atmoic_addon_module_disconnect(crtc, mtk_crtc->ddp_mode,
  5462. &crtc_state->lye_state,
  5463. cmdq_handle);
  5464. ddp_ctx = &mtk_crtc->ddp_ctx[mtk_crtc->ddp_mode];
  5465. for (i = 0; i < ddp_ctx->ddp_comp_nr[DDP_FIRST_PATH]; i++) {
  5466. struct mtk_ddp_comp *comp =
  5467. ddp_ctx->ddp_comp[DDP_FIRST_PATH][i];
  5468. mtk_ddp_comp_stop(comp, cmdq_handle);
  5469. }
  5470. mtk_crtc_disconnect_single_path_cmdq(crtc, cmdq_handle,
  5471. DDP_FIRST_PATH, mtk_crtc->ddp_mode, 1);
  5472. /* Workaround: if CRTC0, reset wdma->fb to NULL to prevent CRTC2
  5473. * config wdma and cause KE
  5474. */
  5475. if (index == 0) {
  5476. comp = mtk_ddp_comp_find_by_id(crtc, DDP_COMPONENT_WDMA0);
  5477. if (!comp)
  5478. comp = mtk_ddp_comp_find_by_id(crtc,
  5479. DDP_COMPONENT_WDMA1);
  5480. if (comp)
  5481. comp->fb = NULL;
  5482. }
  5483. cmdq_pkt_flush(cmdq_handle);
  5484. cmdq_pkt_destroy(cmdq_handle);
  5485. }
  5486. static void __mtk_crtc_sub_path_create(
  5487. struct drm_crtc *crtc,
  5488. unsigned int ddp_mode,
  5489. struct mtk_ddp_config *cfg)
  5490. {
  5491. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  5492. struct mtk_crtc_state *crtc_state = to_mtk_crtc_state(crtc->state);
  5493. struct cmdq_pkt *cmdq_handle;
  5494. if (!mtk_crtc_with_sub_path(crtc, ddp_mode))
  5495. return;
  5496. mtk_crtc_pkt_create(&cmdq_handle, crtc,
  5497. mtk_crtc->gce_obj.client[CLIENT_CFG]);
  5498. if (mtk_crtc_is_frame_trigger_mode(&mtk_crtc->base))
  5499. cmdq_pkt_clear_event(
  5500. cmdq_handle,
  5501. mtk_crtc->gce_obj.event[EVENT_STREAM_DIRTY]);
  5502. /* 1. Connect Sub Path*/
  5503. mtk_crtc_connect_single_path_cmdq(crtc, cmdq_handle, DDP_FIRST_PATH,
  5504. ddp_mode, 1);
  5505. /* 2. Sub path configuration */
  5506. mtk_crtc_config_single_path_cmdq(crtc, cmdq_handle, DDP_FIRST_PATH,
  5507. ddp_mode, cfg);
  5508. _mtk_crtc_atmoic_addon_module_connect(
  5509. crtc, ddp_mode, &crtc_state->lye_state, cmdq_handle);
  5510. cmdq_pkt_flush(cmdq_handle);
  5511. cmdq_pkt_destroy(cmdq_handle);
  5512. }
  5513. static void mtk_crtc_dc_fb_control(struct drm_crtc *crtc,
  5514. unsigned int ddp_mode)
  5515. {
  5516. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  5517. struct mtk_crtc_ddp_ctx *ddp_ctx;
  5518. struct drm_mode_fb_cmd2 mode = {0};
  5519. struct mtk_drm_gem_obj *mtk_gem;
  5520. DDPINFO("%s\n", __func__);
  5521. ddp_ctx = &mtk_crtc->ddp_ctx[ddp_mode];
  5522. if (mtk_crtc_target_is_dc_mode(crtc, ddp_mode) &&
  5523. ddp_ctx->dc_fb == NULL) {
  5524. ddp_ctx = &mtk_crtc->ddp_ctx[ddp_mode];
  5525. mode.width = crtc->state->adjusted_mode.hdisplay;
  5526. mode.height = crtc->state->adjusted_mode.vdisplay;
  5527. mtk_gem = mtk_drm_gem_create(
  5528. crtc->dev,
  5529. mtk_crtc_get_dc_fb_size(crtc) * MAX_CRTC_DC_FB, true);
  5530. mode.pixel_format = DRM_FORMAT_RGB888;
  5531. mode.pitches[0] = mode.width * 3;
  5532. ddp_ctx->dc_fb = mtk_drm_framebuffer_create(crtc->dev, &mode,
  5533. &mtk_gem->base);
  5534. }
  5535. /* do not create wb_fb & dc buffer repeatedly */
  5536. #if 0
  5537. ddp_ctx = &mtk_crtc->ddp_ctx[mtk_crtc->ddp_mode];
  5538. if (!mtk_crtc_target_is_dc_mode(crtc, mtk_crtc->ddp_mode)
  5539. && ddp_ctx->dc_fb) {
  5540. drm_framebuffer_cleanup(ddp_ctx->dc_fb);
  5541. ddp_ctx->dc_fb_idx = 0;
  5542. }
  5543. if (ddp_ctx->wb_fb) {
  5544. drm_framebuffer_cleanup(ddp_ctx->wb_fb);
  5545. ddp_ctx->wb_fb = NULL;
  5546. }
  5547. #endif
  5548. }
  5549. void mtk_crtc_path_switch_prepare(struct drm_crtc *crtc, unsigned int ddp_mode,
  5550. struct mtk_ddp_config *cfg)
  5551. {
  5552. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  5553. cfg->w = crtc->state->adjusted_mode.hdisplay;
  5554. cfg->h = crtc->state->adjusted_mode.vdisplay;
  5555. if (mtk_crtc->panel_ext && mtk_crtc->panel_ext->params &&
  5556. mtk_crtc->panel_ext->params->dyn_fps.switch_en == 1
  5557. && mtk_crtc->panel_ext->params->dyn_fps.vact_timing_fps != 0)
  5558. cfg->vrefresh =
  5559. mtk_crtc->panel_ext->params->dyn_fps.vact_timing_fps;
  5560. else
  5561. cfg->vrefresh = crtc->state->adjusted_mode.vrefresh;
  5562. cfg->bpc = mtk_crtc->bpc;
  5563. cfg->x = 0;
  5564. cfg->y = 0;
  5565. cfg->p_golden_setting_context = __get_golden_setting_context(mtk_crtc);
  5566. /* The components in target ddp_mode may be used during path switching,
  5567. * so attach
  5568. * the CRTC to them.
  5569. */
  5570. mtk_crtc_attach_ddp_comp(crtc, ddp_mode, true);
  5571. }
  5572. void mtk_crtc_path_switch_update_ddp_status(struct drm_crtc *crtc,
  5573. unsigned int ddp_mode)
  5574. {
  5575. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  5576. mtk_crtc_attach_ddp_comp(crtc, mtk_crtc->ddp_mode, false);
  5577. mtk_crtc_attach_ddp_comp(crtc, ddp_mode, true);
  5578. }
  5579. void mtk_need_vds_path_switch(struct drm_crtc *crtc)
  5580. {
  5581. struct mtk_drm_private *priv = crtc->dev->dev_private;
  5582. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  5583. int index = drm_crtc_index(crtc);
  5584. int i = 0;
  5585. int comp_nr = 0;
  5586. if (!(priv && mtk_crtc && (index >= 0))) {
  5587. DDPPR_ERR("%s:%d:Error Invalid params\n");
  5588. return;
  5589. }
  5590. /* In order to confirm it will be called one time,
  5591. * when switch to or switch back, So need some flags
  5592. * to control it.
  5593. */
  5594. if (priv->vds_path_switch_dirty &&
  5595. !priv->vds_path_switch_done && (index == 0)) {
  5596. /* kick idle */
  5597. mtk_drm_idlemgr_kick(__func__, crtc, 0);
  5598. CRTC_MMP_EVENT_START(index, path_switch, mtk_crtc->ddp_mode, 0);
  5599. /* Switch main display path, take away ovl0_2l from main display */
  5600. if (priv->need_vds_path_switch) {
  5601. struct mtk_ddp_comp *comp_ovl0;
  5602. struct mtk_ddp_comp *comp_ovl0_2l;
  5603. struct cmdq_pkt *cmdq_handle;
  5604. struct mtk_crtc_state *crtc_state = to_mtk_crtc_state(crtc->state);
  5605. cmdq_handle =
  5606. cmdq_pkt_create(mtk_crtc->gce_obj.client[CLIENT_CFG]);
  5607. mtk_crtc_wait_frame_done(mtk_crtc,
  5608. cmdq_handle, DDP_FIRST_PATH, 0);
  5609. /* Disconnect current path and remove component mutexs */
  5610. _mtk_crtc_atmoic_addon_module_disconnect(
  5611. crtc, mtk_crtc->ddp_mode,
  5612. &crtc_state->lye_state, cmdq_handle);
  5613. /* Stop ovl 2l */
  5614. comp_ovl0_2l = priv->ddp_comp[DDP_COMPONENT_OVL0_2L];
  5615. mtk_ddp_comp_stop(comp_ovl0_2l, cmdq_handle);
  5616. /* Change ovl0 bg mode frmoe DL mode to const mode */
  5617. comp_ovl0 = priv->ddp_comp[DDP_COMPONENT_OVL0];
  5618. cmdq_pkt_write(cmdq_handle, comp_ovl0->cmdq_base,
  5619. comp_ovl0->regs_pa + DISP_OVL_DATAPATH_CON, 0x0, 0x4);
  5620. mtk_ddp_remove_comp_from_path_with_cmdq(
  5621. mtk_crtc, DDP_COMPONENT_OVL0_2L,
  5622. DDP_COMPONENT_OVL0, cmdq_handle);
  5623. mtk_disp_mutex_remove_comp_with_cmdq(
  5624. mtk_crtc, DDP_COMPONENT_OVL0_2L, cmdq_handle, 0);
  5625. cmdq_pkt_flush(cmdq_handle);
  5626. cmdq_pkt_destroy(cmdq_handle);
  5627. /* unprepare ovl 2l */
  5628. if (atomic_read(&mtk_crtc->already_config))
  5629. mtk_ddp_comp_unprepare(comp_ovl0_2l);
  5630. CRTC_MMP_MARK(index, path_switch, 0xFFFF, 1);
  5631. DDPMSG("Switch vds: Switch ovl0_2l to vds\n");
  5632. /* Update ddp ctx ddp_comp_nr */
  5633. mtk_crtc->ddp_ctx[DDP_MAJOR].ddp_comp_nr[DDP_FIRST_PATH]
  5634. = mtk_crtc->path_data->path_len[
  5635. DDP_MAJOR][DDP_FIRST_PATH] - 1;
  5636. /* Update ddp ctx ddp_comp */
  5637. comp_nr = mtk_crtc->path_data->path_len[
  5638. DDP_MAJOR][DDP_FIRST_PATH];
  5639. for (i = 0; i < comp_nr - 1; i++)
  5640. mtk_crtc->ddp_ctx[
  5641. DDP_MAJOR].ddp_comp[DDP_FIRST_PATH][i] =
  5642. mtk_crtc->ddp_ctx[
  5643. DDP_MAJOR].ddp_comp[DDP_FIRST_PATH][i+1];
  5644. mtk_crtc_attach_ddp_comp(crtc, mtk_crtc->ddp_mode, true);
  5645. /* Update Switch done flag */
  5646. priv->vds_path_switch_done = 1;
  5647. /* Switch main display path, take back ovl0_2l to main display */
  5648. } else {
  5649. struct mtk_ddp_comp *comp_ovl0;
  5650. struct mtk_ddp_comp *comp_ovl0_2l;
  5651. int width = crtc->state->adjusted_mode.hdisplay;
  5652. int height = crtc->state->adjusted_mode.vdisplay;
  5653. struct cmdq_pkt *cmdq_handle;
  5654. int keep_first_layer = false;
  5655. cmdq_handle =
  5656. cmdq_pkt_create(mtk_crtc->gce_obj.client[CLIENT_CFG]);
  5657. mtk_crtc_wait_frame_done(mtk_crtc,
  5658. cmdq_handle, DDP_FIRST_PATH, 0);
  5659. /* Change ovl0 bg mode frmoe const mode to DL mode */
  5660. comp_ovl0 = priv->ddp_comp[DDP_COMPONENT_OVL0];
  5661. cmdq_pkt_write(cmdq_handle, comp_ovl0->cmdq_base,
  5662. comp_ovl0->regs_pa + DISP_OVL_DATAPATH_CON, 0x4, 0x4);
  5663. /* Change ovl0 ROI size */
  5664. comp_ovl0_2l = priv->ddp_comp[DDP_COMPONENT_OVL0_2L];
  5665. cmdq_pkt_write(cmdq_handle, comp_ovl0_2l->cmdq_base,
  5666. comp_ovl0_2l->regs_pa + DISP_OVL_ROI_SIZE,
  5667. height << 16 | width, ~0);
  5668. /* Connect cur path components */
  5669. mtk_ddp_add_comp_to_path_with_cmdq(
  5670. mtk_crtc, DDP_COMPONENT_OVL0_2L,
  5671. DDP_COMPONENT_OVL0, cmdq_handle);
  5672. mtk_disp_mutex_add_comp_with_cmdq(
  5673. mtk_crtc, DDP_COMPONENT_OVL0_2L,
  5674. mtk_crtc_is_frame_trigger_mode(&mtk_crtc->base),
  5675. cmdq_handle, 0);
  5676. /* Switch back need reprepare ovl0_2l */
  5677. mtk_ddp_comp_prepare(comp_ovl0_2l);
  5678. mtk_ddp_comp_start(comp_ovl0_2l, cmdq_handle);
  5679. /* ovl 2l should not have old config, so disable
  5680. * all ovl 2l layer when flush first frame after
  5681. * switch ovl 2l back to main disp
  5682. */
  5683. mtk_ddp_comp_io_cmd(comp_ovl0_2l, cmdq_handle,
  5684. OVL_ALL_LAYER_OFF, &keep_first_layer);
  5685. cmdq_pkt_flush(cmdq_handle);
  5686. cmdq_pkt_destroy(cmdq_handle);
  5687. CRTC_MMP_MARK(index, path_switch, 0xFFFF, 2);
  5688. DDPMSG("Switch vds: Switch ovl0_2l to main disp\n");
  5689. /* Update ddp ctx ddp_comp_nr */
  5690. mtk_crtc->ddp_ctx[DDP_MAJOR].ddp_comp_nr[DDP_FIRST_PATH]
  5691. = mtk_crtc->path_data->path_len[
  5692. DDP_MAJOR][DDP_FIRST_PATH];
  5693. /* Update ddp ctx ddp_comp */
  5694. comp_nr = mtk_crtc->path_data->path_len[
  5695. DDP_MAJOR][DDP_FIRST_PATH];
  5696. for (i = comp_nr - 1; i > 0; i--)
  5697. mtk_crtc->ddp_ctx[
  5698. DDP_MAJOR].ddp_comp[DDP_FIRST_PATH][i] =
  5699. mtk_crtc->ddp_ctx[
  5700. DDP_MAJOR].ddp_comp[DDP_FIRST_PATH][i-1];
  5701. mtk_crtc->ddp_ctx[
  5702. DDP_MAJOR].ddp_comp[DDP_FIRST_PATH][0] =
  5703. priv->ddp_comp[DDP_COMPONENT_OVL0_2L];
  5704. mtk_crtc_attach_ddp_comp(crtc, mtk_crtc->ddp_mode, true);
  5705. /* Update Switch done flag */
  5706. priv->vds_path_switch_dirty = 0;
  5707. }
  5708. DDPMSG("Switch vds: Switch ovl0_2l Done\n");
  5709. CRTC_MMP_EVENT_END(index, path_switch, crtc->enabled, 0);
  5710. }
  5711. }
  5712. int mtk_crtc_path_switch(struct drm_crtc *crtc, unsigned int ddp_mode,
  5713. int need_lock)
  5714. {
  5715. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  5716. struct mtk_ddp_config cfg;
  5717. int index = drm_crtc_index(crtc);
  5718. bool need_wait;
  5719. CRTC_MMP_EVENT_START(index, path_switch, mtk_crtc->ddp_mode,
  5720. ddp_mode);
  5721. if (need_lock)
  5722. DDP_MUTEX_LOCK(&mtk_crtc->lock, __func__, __LINE__);
  5723. if (ddp_mode == mtk_crtc->ddp_mode || !crtc->enabled) {
  5724. DDPINFO("CRTC%d skip path switch %u->%u, enable:%d\n",
  5725. index, mtk_crtc->ddp_mode,
  5726. ddp_mode, crtc->enabled);
  5727. CRTC_MMP_MARK(index, path_switch, 0, 0);
  5728. goto done;
  5729. }
  5730. if ((index == 0) && !mtk_crtc->enabled) {
  5731. DDPINFO("CRTC%d skip path switch %u->%u, enable:%d\n",
  5732. index, mtk_crtc->ddp_mode,
  5733. ddp_mode, mtk_crtc->enabled);
  5734. CRTC_MMP_MARK(index, path_switch, 0, 1);
  5735. goto done2;
  5736. }
  5737. mtk_drm_crtc_wait_blank(mtk_crtc);
  5738. DDPINFO("%s crtc%d path switch(%d->%d)\n", __func__, index,
  5739. mtk_crtc->ddp_mode, ddp_mode);
  5740. mtk_drm_idlemgr_kick(__func__, crtc, 0);
  5741. CRTC_MMP_MARK(index, path_switch, 1, 0);
  5742. /* 0. Special NO_USE ddp mode control. In NO_USE ddp mode, the HW path
  5743. * is disabled currently or to be disabled. So the control use the
  5744. * CRTC enable/disable function to create/destroy path.
  5745. */
  5746. if (ddp_mode == DDP_NO_USE) {
  5747. CRTC_MMP_MARK(index, path_switch, 0, 2);
  5748. if ((mtk_crtc->ddp_mode == DDP_MAJOR) && (index == 2))
  5749. need_wait = false;
  5750. else
  5751. need_wait = true;
  5752. mtk_drm_crtc_disable(crtc, need_wait);
  5753. goto done;
  5754. } else if (mtk_crtc->ddp_mode == DDP_NO_USE) {
  5755. CRTC_MMP_MARK(index, path_switch, 0, 3);
  5756. mtk_crtc->ddp_mode = ddp_mode;
  5757. mtk_drm_crtc_enable(crtc);
  5758. goto done;
  5759. }
  5760. mtk_crtc_path_switch_prepare(crtc, ddp_mode, &cfg);
  5761. /* 1 Destroy original sub path */
  5762. __mtk_crtc_old_sub_path_destroy(crtc, &cfg);
  5763. /* 2 Composing planes and write back to buffer */
  5764. __mtk_crtc_composition_wb(crtc, ddp_mode, &cfg);
  5765. /* 3. Composing planes and write back to buffer */
  5766. __mtk_crtc_prim_path_switch(crtc, ddp_mode, &cfg);
  5767. CRTC_MMP_MARK(index, path_switch, 1, 1);
  5768. /* 4. Create new sub path */
  5769. __mtk_crtc_sub_path_create(crtc, ddp_mode, &cfg);
  5770. /* 5. create and destroy the fb used in ddp */
  5771. mtk_crtc_dc_fb_control(crtc, ddp_mode);
  5772. done:
  5773. mtk_crtc->ddp_mode = ddp_mode;
  5774. if (crtc->enabled && mtk_crtc->ddp_mode != DDP_NO_USE)
  5775. mtk_crtc_update_ddp_sw_status(crtc, true);
  5776. if (need_lock)
  5777. DDP_MUTEX_UNLOCK(&mtk_crtc->lock, __func__, __LINE__);
  5778. CRTC_MMP_EVENT_END(index, path_switch, crtc->enabled,
  5779. need_lock);
  5780. DDPINFO("%s:%d -\n", __func__, __LINE__);
  5781. return 0;
  5782. done2:
  5783. if (need_lock)
  5784. DDP_MUTEX_UNLOCK(&mtk_crtc->lock, __func__, __LINE__);
  5785. CRTC_MMP_EVENT_END(index, path_switch, crtc->enabled,
  5786. need_lock);
  5787. DDPINFO("%s:%d -\n", __func__, __LINE__);
  5788. return 0;
  5789. }
  5790. int mtk_crtc_get_mutex_id(struct drm_crtc *crtc, unsigned int ddp_mode,
  5791. enum mtk_ddp_comp_id find_comp)
  5792. {
  5793. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  5794. int crtc_id = drm_crtc_index(crtc);
  5795. int i, j;
  5796. struct mtk_ddp_comp *comp;
  5797. int find = -1;
  5798. bool has_connector = true;
  5799. for_each_comp_in_target_ddp_mode(comp, mtk_crtc, i, j,
  5800. ddp_mode)
  5801. if (comp->id == find_comp) {
  5802. find = i;
  5803. break;
  5804. }
  5805. if (find == -1) {
  5806. DDPPR_ERR("component %d is not found in path %d of crtc %d\n",
  5807. find_comp, ddp_mode, crtc_id);
  5808. return -1;
  5809. }
  5810. if (mtk_crtc_with_sub_path(crtc, ddp_mode) && find == 0)
  5811. has_connector = false;
  5812. switch (crtc_id) {
  5813. case 0:
  5814. if (has_connector)
  5815. return 0;
  5816. else
  5817. return 1;
  5818. case 1:
  5819. if (has_connector)
  5820. return 2;
  5821. else
  5822. return 3;
  5823. case 2:
  5824. if (has_connector)
  5825. return 4;
  5826. else
  5827. return 5;
  5828. default:
  5829. DDPPR_ERR("not define mutex id in crtc %d\n", crtc_id);
  5830. }
  5831. return -1;
  5832. }
  5833. void mtk_crtc_disconnect_path_between_component(struct drm_crtc *crtc,
  5834. unsigned int ddp_mode,
  5835. enum mtk_ddp_comp_id prev,
  5836. enum mtk_ddp_comp_id next,
  5837. struct cmdq_pkt *cmdq_handle)
  5838. {
  5839. int i, j;
  5840. struct mtk_ddp_comp *comp;
  5841. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  5842. int mutex_id = mtk_crtc_get_mutex_id(crtc, ddp_mode, prev);
  5843. struct mtk_crtc_ddp_ctx *ddp_ctx = &mtk_crtc->ddp_ctx[ddp_mode];
  5844. int find_idx = -1;
  5845. if (mutex_id < 0) {
  5846. DDPPR_ERR("invalid mutex id:%d\n", mutex_id);
  5847. return;
  5848. }
  5849. for_each_comp_in_target_ddp_mode(comp, mtk_crtc, i, j, ddp_mode) {
  5850. if (comp->id == prev)
  5851. find_idx = j;
  5852. if (find_idx > -1 && j > find_idx) {
  5853. mtk_ddp_remove_comp_from_path_with_cmdq(
  5854. mtk_crtc, ddp_ctx->ddp_comp[i][j - 1]->id,
  5855. comp->id, cmdq_handle);
  5856. if (comp->id != next)
  5857. mtk_disp_mutex_remove_comp_with_cmdq(
  5858. mtk_crtc, comp->id, cmdq_handle,
  5859. mutex_id);
  5860. }
  5861. if (comp->id == next)
  5862. return;
  5863. }
  5864. }
  5865. void mtk_crtc_connect_path_between_component(struct drm_crtc *crtc,
  5866. unsigned int ddp_mode,
  5867. enum mtk_ddp_comp_id prev,
  5868. enum mtk_ddp_comp_id next,
  5869. struct cmdq_pkt *cmdq_handle)
  5870. {
  5871. int i, j;
  5872. struct mtk_ddp_comp *comp;
  5873. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  5874. int mutex_id = mtk_crtc_get_mutex_id(crtc, ddp_mode, prev);
  5875. struct mtk_crtc_ddp_ctx *ddp_ctx = &mtk_crtc->ddp_ctx[ddp_mode];
  5876. int find_idx = -1;
  5877. if (mutex_id < 0) {
  5878. DDPPR_ERR("invalid mutex id:%d\n", mutex_id);
  5879. return;
  5880. }
  5881. for_each_comp_in_target_ddp_mode(comp, mtk_crtc, i, j, ddp_mode) {
  5882. if (comp->id == prev)
  5883. find_idx = j;
  5884. if (find_idx > -1 && j > find_idx) {
  5885. mtk_ddp_add_comp_to_path_with_cmdq(
  5886. mtk_crtc, ddp_ctx->ddp_comp[i][j - 1]->id,
  5887. comp->id, cmdq_handle);
  5888. if (comp->id != next)
  5889. mtk_disp_mutex_add_comp_with_cmdq(
  5890. mtk_crtc, comp->id,
  5891. mtk_crtc_is_frame_trigger_mode(crtc),
  5892. cmdq_handle, mutex_id);
  5893. }
  5894. if (comp->id == next)
  5895. return;
  5896. }
  5897. }
  5898. int mtk_crtc_find_comp(struct drm_crtc *crtc, unsigned int ddp_mode,
  5899. enum mtk_ddp_comp_id comp_id)
  5900. {
  5901. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  5902. struct mtk_ddp_comp *comp;
  5903. int i, j;
  5904. for_each_comp_in_target_ddp_mode(
  5905. comp, mtk_crtc, i, j,
  5906. ddp_mode)
  5907. if (comp->id == comp_id &&
  5908. mtk_ddp_comp_get_type(comp_id) !=
  5909. MTK_DISP_VIRTUAL)
  5910. return comp->id;
  5911. return -1;
  5912. }
  5913. int mtk_crtc_find_next_comp(struct drm_crtc *crtc, unsigned int ddp_mode,
  5914. enum mtk_ddp_comp_id comp_id)
  5915. {
  5916. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  5917. struct mtk_ddp_comp *comp;
  5918. int id;
  5919. int i, j, k;
  5920. for_each_comp_in_target_ddp_mode(comp, mtk_crtc, i, j, ddp_mode) {
  5921. if (comp->id == comp_id) {
  5922. struct mtk_crtc_ddp_ctx *ddp_ctx =
  5923. &mtk_crtc->ddp_ctx[ddp_mode];
  5924. for (k = j + 1; k < ddp_ctx->ddp_comp_nr[i]; k++) {
  5925. id = ddp_ctx->ddp_comp[i][k]->id;
  5926. if (mtk_ddp_comp_get_type(id) !=
  5927. MTK_DISP_VIRTUAL)
  5928. return ddp_ctx->ddp_comp[i][k]->id;
  5929. }
  5930. return -1;
  5931. }
  5932. }
  5933. return -1;
  5934. }
  5935. int mtk_crtc_find_prev_comp(struct drm_crtc *crtc, unsigned int ddp_mode,
  5936. enum mtk_ddp_comp_id comp_id)
  5937. {
  5938. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  5939. struct mtk_ddp_comp *comp;
  5940. int real_comp_id = -1;
  5941. int real_comp_path_idx = -1;
  5942. int i, j;
  5943. for_each_comp_in_target_ddp_mode(comp, mtk_crtc, i, j, ddp_mode) {
  5944. if (comp->id == comp_id) {
  5945. if (i == real_comp_path_idx)
  5946. return real_comp_id;
  5947. else
  5948. return -1;
  5949. }
  5950. if (mtk_ddp_comp_get_type(comp->id) != MTK_DISP_VIRTUAL) {
  5951. real_comp_id = comp->id;
  5952. real_comp_path_idx = i;
  5953. }
  5954. }
  5955. return -1;
  5956. }
  5957. int mtk_crtc_mipi_freq_switch(struct drm_crtc *crtc, unsigned int en,
  5958. unsigned int userdata)
  5959. {
  5960. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  5961. struct mtk_ddp_comp *comp;
  5962. struct mtk_panel_ext *ext = mtk_crtc->panel_ext;
  5963. if (mtk_crtc->mipi_hopping_sta == en)
  5964. return 0;
  5965. if (!(ext && ext->params &&
  5966. ext->params->dyn.switch_en == 1))
  5967. return 0;
  5968. DDPMSG("%s, userdata=%d, en=%d\n", __func__, userdata, en);
  5969. DDP_MUTEX_LOCK(&mtk_crtc->lock, __func__, __LINE__);
  5970. mtk_crtc->mipi_hopping_sta = en;
  5971. comp = mtk_ddp_comp_request_output(mtk_crtc);
  5972. if (!comp) {
  5973. DDPPR_ERR("request output fail\n");
  5974. DDP_MUTEX_UNLOCK(&mtk_crtc->lock, __func__, __LINE__);
  5975. return -EINVAL;
  5976. }
  5977. mtk_ddp_comp_io_cmd(comp,
  5978. NULL, MIPI_HOPPING, &en);
  5979. DDP_MUTEX_UNLOCK(&mtk_crtc->lock, __func__, __LINE__);
  5980. return 0;
  5981. }
  5982. char *mtk_crtc_index_spy(int crtc_index)
  5983. {
  5984. switch (crtc_index) {
  5985. case 0:
  5986. return "P";
  5987. case 1:
  5988. return "E";
  5989. case 2:
  5990. return "M";
  5991. default:
  5992. return "Unknown";
  5993. }
  5994. }
  5995. int mtk_crtc_osc_freq_switch(struct drm_crtc *crtc, unsigned int en,
  5996. unsigned int userdata)
  5997. {
  5998. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  5999. struct mtk_ddp_comp *comp;
  6000. struct mtk_panel_ext *ext = mtk_crtc->panel_ext;
  6001. if (mtk_crtc->panel_osc_hopping_sta == en)
  6002. return 0;
  6003. if (!(ext && ext->params))
  6004. return 0;
  6005. DDPMSG("%s, userdata=%d, en=%d\n", __func__, userdata, en);
  6006. DDP_MUTEX_LOCK(&mtk_crtc->lock, __func__, __LINE__);
  6007. mtk_crtc->panel_osc_hopping_sta = en;
  6008. if (!mtk_crtc->enabled)
  6009. goto done;
  6010. comp = mtk_ddp_comp_request_output(mtk_crtc);
  6011. if (!comp) {
  6012. DDPPR_ERR("request output fail\n");
  6013. DDP_MUTEX_UNLOCK(&mtk_crtc->lock, __func__, __LINE__);
  6014. return -EINVAL;
  6015. }
  6016. /* Following section is for customization */
  6017. /* Start */
  6018. /* e.g. lmtk_ddp_comp_io_cmd(comp,
  6019. * NULL, PANEL_OSC_HOPPING, &en);
  6020. */
  6021. /* End */
  6022. done:
  6023. DDP_MUTEX_UNLOCK(&mtk_crtc->lock, __func__, __LINE__);
  6024. return 0;
  6025. }
  6026. int mtk_crtc_enter_tui(struct drm_crtc *crtc)
  6027. {
  6028. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  6029. struct mtk_drm_private *priv = crtc->dev->dev_private;
  6030. unsigned int hrt_idx;
  6031. int i;
  6032. DDPMSG("%s\n", __func__);
  6033. DDP_MUTEX_LOCK(&mtk_crtc->lock, __func__, __LINE__);
  6034. mtk_crtc->crtc_blank = true;
  6035. mtk_disp_esd_check_switch(crtc, 0);
  6036. mtk_drm_set_idlemgr(crtc, 0, 0);
  6037. hrt_idx = _layering_rule_get_hrt_idx();
  6038. hrt_idx++;
  6039. atomic_set(&priv->rollback_all, 1);
  6040. drm_trigger_repaint(DRM_REPAINT_FOR_IDLE, crtc->dev);
  6041. DDP_MUTEX_UNLOCK(&mtk_crtc->lock, __func__, __LINE__);
  6042. /* TODO: Potential risk, display suspend after release lock */
  6043. for (i = 0; i < 60; ++i) {
  6044. usleep_range(16667, 17000);
  6045. if (atomic_read(&mtk_crtc->qos_ctx->last_hrt_idx) >=
  6046. hrt_idx)
  6047. break;
  6048. }
  6049. if (i >= 60)
  6050. DDPPR_ERR("wait repaint %d\n", i);
  6051. DDP_MUTEX_LOCK(&mtk_crtc->blank_lock, __func__, __LINE__);
  6052. /* TODO: HardCode select OVL0, maybe store in platform data */
  6053. priv->ddp_comp[DDP_COMPONENT_OVL0]->blank_mode = true;
  6054. DDP_MUTEX_UNLOCK(&mtk_crtc->blank_lock, __func__, __LINE__);
  6055. wake_up(&mtk_crtc->state_wait_queue);
  6056. return 0;
  6057. }
  6058. int mtk_crtc_exit_tui(struct drm_crtc *crtc)
  6059. {
  6060. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  6061. struct mtk_drm_private *priv = crtc->dev->dev_private;
  6062. DDPMSG("%s\n", __func__);
  6063. DDP_MUTEX_LOCK(&mtk_crtc->blank_lock, __func__, __LINE__);
  6064. /* TODO: Hard Code select OVL0, maybe store in platform data */
  6065. priv->ddp_comp[DDP_COMPONENT_OVL0]->blank_mode = false;
  6066. mtk_crtc->crtc_blank = false;
  6067. atomic_set(&priv->rollback_all, 0);
  6068. DDP_MUTEX_UNLOCK(&mtk_crtc->blank_lock, __func__, __LINE__);
  6069. wake_up(&mtk_crtc->state_wait_queue);
  6070. DDP_MUTEX_LOCK(&mtk_crtc->lock, __func__, __LINE__);
  6071. mtk_drm_set_idlemgr(crtc, 1, 0);
  6072. mtk_disp_esd_check_switch(crtc, 1);
  6073. DDP_MUTEX_UNLOCK(&mtk_crtc->lock, __func__, __LINE__);
  6074. return 0;
  6075. }
  6076. /********************** Legacy DISP API ****************************/
  6077. unsigned int DISP_GetScreenWidth(void)
  6078. {
  6079. if (!pgc) {
  6080. DDPPR_ERR("LCM is not registered.\n");
  6081. return 0;
  6082. }
  6083. return pgc->mode.hdisplay;
  6084. }
  6085. unsigned int DISP_GetScreenHeight(void)
  6086. {
  6087. if (!pgc) {
  6088. DDPPR_ERR("LCM is not registered.\n");
  6089. return 0;
  6090. }
  6091. return pgc->mode.vdisplay;
  6092. }
  6093. int mtk_crtc_lcm_ATA(struct drm_crtc *crtc)
  6094. {
  6095. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  6096. struct mtk_ddp_comp *output_comp;
  6097. struct cmdq_pkt *cmdq_handle;
  6098. struct mtk_panel_params *panel_ext =
  6099. mtk_drm_get_lcm_ext_params(crtc);
  6100. int ret = 0;
  6101. DDPINFO("%s\n", __func__);
  6102. mtk_disp_esd_check_switch(crtc, 0);
  6103. output_comp = mtk_ddp_comp_request_output(mtk_crtc);
  6104. if (unlikely(!output_comp)) {
  6105. DDPPR_ERR("%s:invalid output comp\n", __func__);
  6106. ret = -EINVAL;
  6107. goto out;
  6108. }
  6109. if (unlikely(!panel_ext)) {
  6110. DDPPR_ERR("%s:invalid panel_ext\n", __func__);
  6111. ret = -EINVAL;
  6112. goto out;
  6113. }
  6114. DDPINFO("[ATA_LCM]primary display path stop[begin]\n");
  6115. if (!mtk_crtc_is_frame_trigger_mode(crtc)) {
  6116. mtk_crtc_pkt_create(&cmdq_handle, crtc,
  6117. mtk_crtc->gce_obj.client[CLIENT_CFG]);
  6118. mtk_ddp_comp_io_cmd(output_comp,
  6119. cmdq_handle, DSI_STOP_VDO_MODE, NULL);
  6120. cmdq_pkt_flush(cmdq_handle);
  6121. cmdq_pkt_destroy(cmdq_handle);
  6122. }
  6123. DDPINFO("[ATA_LCM]primary display path stop[end]\n");
  6124. mtk_ddp_comp_io_cmd(output_comp, NULL, LCM_ATA_CHECK, &ret);
  6125. if (!mtk_crtc_is_frame_trigger_mode(crtc)) {
  6126. mtk_crtc_pkt_create(&cmdq_handle, crtc,
  6127. mtk_crtc->gce_obj.client[CLIENT_CFG]);
  6128. mtk_ddp_comp_io_cmd(output_comp,
  6129. cmdq_handle, DSI_START_VDO_MODE, NULL);
  6130. mtk_disp_mutex_trigger(mtk_crtc->mutex[0], cmdq_handle);
  6131. mtk_ddp_comp_io_cmd(output_comp, cmdq_handle, COMP_REG_START,
  6132. NULL);
  6133. cmdq_pkt_flush(cmdq_handle);
  6134. cmdq_pkt_destroy(cmdq_handle);
  6135. }
  6136. out:
  6137. mtk_disp_esd_check_switch(crtc, 1);
  6138. return ret;
  6139. }
  6140. unsigned int mtk_drm_primary_display_get_debug_state(
  6141. struct mtk_drm_private *priv, char *stringbuf, int buf_len)
  6142. {
  6143. int len = 0;
  6144. struct drm_crtc *crtc = priv->crtc[0];
  6145. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  6146. struct mtk_crtc_state *mtk_state = to_mtk_crtc_state(crtc->state);
  6147. struct mtk_ddp_comp *comp;
  6148. char *panel_name;
  6149. comp = mtk_ddp_comp_request_output(mtk_crtc);
  6150. if (unlikely(!comp))
  6151. DDPPR_ERR("%s:invalid output comp\n", __func__);
  6152. mtk_ddp_comp_io_cmd(comp, NULL, GET_PANEL_NAME,
  6153. &panel_name);
  6154. len += scnprintf(stringbuf + len, buf_len - len,
  6155. "========== Primary Display Info ==========\n");
  6156. len += scnprintf(stringbuf + len, buf_len - len,
  6157. "LCM Driver=[%s] Resolution=%ux%u, Connected:%s\n",
  6158. panel_name, crtc->state->adjusted_mode.hdisplay,
  6159. crtc->state->adjusted_mode.vdisplay,
  6160. (mtk_drm_lcm_is_connect() ? "Y" : "N"));
  6161. len += scnprintf(stringbuf + len, buf_len - len,
  6162. "FPS = %d, display mode idx = %u, %s mode\n",
  6163. crtc->state->adjusted_mode.vrefresh,
  6164. mtk_state->prop_val[CRTC_PROP_DISP_MODE_IDX],
  6165. (mtk_crtc_is_frame_trigger_mode(crtc) ?
  6166. "cmd" : "vdo"));
  6167. len += scnprintf(stringbuf + len, buf_len - len,
  6168. "================================================\n\n");
  6169. return len;
  6170. }
  6171. int MMPathTraceCrtcPlanes(struct drm_crtc *crtc,
  6172. char *str, int strlen, int n)
  6173. {
  6174. unsigned int i = 0, addr;
  6175. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  6176. for (i = 0; i < mtk_crtc->layer_nr; i++) {
  6177. struct mtk_plane_state *state =
  6178. to_mtk_plane_state(mtk_crtc->planes[i].base.state);
  6179. struct mtk_plane_pending_state *pending = &state->pending;
  6180. if (pending->enable == 0)
  6181. continue;
  6182. addr = (unsigned int)pending->addr;
  6183. n += scnprintf(str + n, strlen - n, "in_%d=0x%x, ",
  6184. i, addr);
  6185. n += scnprintf(str + n, strlen - n, "in_%d_width=%u, ",
  6186. i, pending->width);
  6187. n += scnprintf(str + n, strlen - n, "in_%d_height=%u, ",
  6188. i, pending->height);
  6189. n += scnprintf(str + n, strlen - n, "in_%d_fmt=%s, ",
  6190. i, mtk_get_format_name(pending->format));
  6191. n += scnprintf(str + n, strlen - n, "in_%d_bpp=%u, ",
  6192. i, mtk_get_format_bpp(pending->format));
  6193. n += scnprintf(str + n, strlen - n, "in_%d_compr=%u, ",
  6194. i, pending->prop_val[PLANE_PROP_COMPRESS]);
  6195. }
  6196. return n;
  6197. }
  6198. /* ************ Panel Master **************** */
  6199. void mtk_crtc_start_for_pm(struct drm_crtc *crtc)
  6200. {
  6201. int i, j;
  6202. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  6203. struct mtk_ddp_comp *comp;
  6204. struct cmdq_pkt *cmdq_handle;
  6205. /* start trig loop */
  6206. if (mtk_crtc_with_trigger_loop(crtc))
  6207. mtk_crtc_start_trig_loop(crtc);
  6208. mtk_crtc_pkt_create(&cmdq_handle, &mtk_crtc->base,
  6209. mtk_crtc->gce_obj.client[CLIENT_CFG]);
  6210. /*if VDO mode start DSI MODE */
  6211. if (!mtk_crtc_is_frame_trigger_mode(crtc) &&
  6212. mtk_crtc_is_connector_enable(mtk_crtc)) {
  6213. for_each_comp_in_cur_crtc_path(comp, mtk_crtc, i, j) {
  6214. mtk_ddp_comp_io_cmd(comp, cmdq_handle,
  6215. DSI_START_VDO_MODE, NULL);
  6216. }
  6217. }
  6218. for_each_comp_in_cur_crtc_path(comp, mtk_crtc, i, j) {
  6219. mtk_ddp_comp_start(comp, cmdq_handle);
  6220. }
  6221. cmdq_pkt_flush(cmdq_handle);
  6222. cmdq_pkt_destroy(cmdq_handle);
  6223. }
  6224. void mtk_crtc_stop_for_pm(struct mtk_drm_crtc *mtk_crtc, bool need_wait)
  6225. {
  6226. struct cmdq_pkt *cmdq_handle;
  6227. unsigned int crtc_id = drm_crtc_index(&mtk_crtc->base);
  6228. struct drm_crtc *crtc = &mtk_crtc->base;
  6229. DDPINFO("%s:%d +\n", __func__, __LINE__);
  6230. /* 0. Waiting CLIENT_DSI_CFG thread done */
  6231. if (crtc_id == 0) {
  6232. mtk_crtc_pkt_create(&cmdq_handle, &mtk_crtc->base,
  6233. mtk_crtc->gce_obj.client[CLIENT_DSI_CFG]);
  6234. cmdq_pkt_flush(cmdq_handle);
  6235. cmdq_pkt_destroy(cmdq_handle);
  6236. }
  6237. mtk_crtc_pkt_create(&cmdq_handle, &mtk_crtc->base,
  6238. mtk_crtc->gce_obj.client[CLIENT_CFG]);
  6239. if (!need_wait)
  6240. goto skip;
  6241. if (crtc_id == 2) {
  6242. cmdq_pkt_wait_no_clear(cmdq_handle,
  6243. mtk_crtc->gce_obj.event[EVENT_WDMA0_EOF]);
  6244. } else if (mtk_crtc_is_frame_trigger_mode(&mtk_crtc->base)) {
  6245. /* 1. wait stream eof & clear tocken */
  6246. /* clear eof token to prevent any config after this command */
  6247. cmdq_pkt_wfe(cmdq_handle,
  6248. mtk_crtc->gce_obj.event[EVENT_STREAM_EOF]);
  6249. /* clear dirty token to prevent trigger loop start */
  6250. cmdq_pkt_clear_event(
  6251. cmdq_handle,
  6252. mtk_crtc->gce_obj.event[EVENT_STREAM_BLOCK]);
  6253. } else if (mtk_crtc_is_connector_enable(mtk_crtc)) {
  6254. /* In vdo mode, DSI would be stop when disable connector
  6255. * Do not wait frame done in this case.
  6256. */
  6257. cmdq_pkt_wfe(cmdq_handle,
  6258. mtk_crtc->gce_obj.event[EVENT_VDO_EOF]);
  6259. }
  6260. skip:
  6261. /* 2. stop all modules in this CRTC */
  6262. mtk_crtc_stop_ddp(mtk_crtc, cmdq_handle);
  6263. cmdq_pkt_flush(cmdq_handle);
  6264. cmdq_pkt_destroy(cmdq_handle);
  6265. /* 3. stop trig loop */
  6266. if (mtk_crtc_with_trigger_loop(crtc)) {
  6267. mtk_crtc_stop_trig_loop(crtc);
  6268. #if defined(CONFIG_MACH_MT6873) || defined(CONFIG_MACH_MT6853) || \
  6269. defined(CONFIG_MACH_MT6833)
  6270. if (mtk_crtc_with_sodi_loop(crtc) &&
  6271. (!mtk_crtc_is_frame_trigger_mode(crtc)))
  6272. mtk_crtc_stop_sodi_loop(crtc);
  6273. #endif
  6274. }
  6275. DDPINFO("%s:%d -\n", __func__, __LINE__);
  6276. }
  6277. /* *********** Panel Master end ************** */
  6278. bool mtk_drm_get_hdr_property(void)
  6279. {
  6280. return hdr_en;
  6281. }