cam_isp_context.c 288 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2017-2021, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2022-2023, Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #include <linux/debugfs.h>
  7. #include <linux/videodev2.h>
  8. #include <linux/slab.h>
  9. #include <linux/uaccess.h>
  10. #include <linux/ratelimit.h>
  11. #include "cam_mem_mgr.h"
  12. #include "cam_sync_api.h"
  13. #include "cam_req_mgr_dev.h"
  14. #include "cam_trace.h"
  15. #include "cam_debug_util.h"
  16. #include "cam_packet_util.h"
  17. #include "cam_context_utils.h"
  18. #include "cam_cdm_util.h"
  19. #include "cam_isp_context.h"
  20. #include "cam_common_util.h"
  21. #include "cam_req_mgr_debug.h"
  22. #include "cam_cpas_api.h"
  23. #include "cam_ife_hw_mgr.h"
  24. #include "cam_subdev.h"
  25. static const char isp_dev_name[] = "cam-isp";
  26. static struct cam_isp_ctx_debug isp_ctx_debug;
  27. #define INC_HEAD(head, max_entries, ret) \
  28. div_u64_rem(atomic64_add_return(1, head),\
  29. max_entries, (ret))
  30. static int cam_isp_context_dump_requests(void *data,
  31. void *pf_args);
  32. static int cam_isp_context_hw_recovery(void *priv, void *data);
  33. static int cam_isp_context_handle_message(void *context,
  34. uint32_t msg_type, void *data);
  35. static int __cam_isp_ctx_start_dev_in_ready(struct cam_context *ctx,
  36. struct cam_start_stop_dev_cmd *cmd);
  37. static void __cam_isp_ctx_dump_state_monitor_array(
  38. struct cam_isp_context *ctx_isp);
  39. static const char *__cam_isp_hw_evt_val_to_type(
  40. uint32_t evt_id);
  41. static const char *__cam_isp_ctx_substate_val_to_type(
  42. enum cam_isp_ctx_activated_substate type);
  43. static int __cam_isp_ctx_check_deferred_buf_done(
  44. struct cam_isp_context *ctx_isp,
  45. struct cam_isp_hw_done_event_data *done,
  46. uint32_t bubble_state);
  47. static const char *__cam_isp_evt_val_to_type(
  48. uint32_t evt_id)
  49. {
  50. switch (evt_id) {
  51. case CAM_ISP_CTX_EVENT_SUBMIT:
  52. return "SUBMIT";
  53. case CAM_ISP_CTX_EVENT_APPLY:
  54. return "APPLY";
  55. case CAM_ISP_CTX_EVENT_EPOCH:
  56. return "EPOCH";
  57. case CAM_ISP_CTX_EVENT_RUP:
  58. return "RUP";
  59. case CAM_ISP_CTX_EVENT_BUFDONE:
  60. return "BUFDONE";
  61. case CAM_ISP_CTX_EVENT_SHUTTER:
  62. return "SHUTTER_NOTIFICATION";
  63. default:
  64. return "CAM_ISP_EVENT_INVALID";
  65. }
  66. }
  67. static void __cam_isp_ctx_update_event_record(
  68. struct cam_isp_context *ctx_isp,
  69. enum cam_isp_ctx_event event,
  70. struct cam_ctx_request *req,
  71. void *event_data)
  72. {
  73. int iterator = 0;
  74. ktime_t cur_time;
  75. struct cam_isp_ctx_req *req_isp;
  76. if (!ctx_isp) {
  77. CAM_ERR(CAM_ISP, "Invalid Args");
  78. return;
  79. }
  80. switch (event) {
  81. case CAM_ISP_CTX_EVENT_EPOCH:
  82. case CAM_ISP_CTX_EVENT_RUP:
  83. case CAM_ISP_CTX_EVENT_BUFDONE:
  84. case CAM_ISP_CTX_EVENT_SHUTTER:
  85. break;
  86. case CAM_ISP_CTX_EVENT_SUBMIT:
  87. case CAM_ISP_CTX_EVENT_APPLY:
  88. if (!req) {
  89. CAM_ERR(CAM_ISP, "Invalid arg for event %d", event);
  90. return;
  91. }
  92. break;
  93. default:
  94. break;
  95. }
  96. INC_HEAD(&ctx_isp->dbg_monitors.event_record_head[event],
  97. CAM_ISP_CTX_EVENT_RECORD_MAX_ENTRIES, &iterator);
  98. cur_time = ktime_get();
  99. if (req) {
  100. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  101. ctx_isp->dbg_monitors.event_record[event][iterator].req_id =
  102. req->request_id;
  103. req_isp->event_timestamp[event] = cur_time;
  104. } else {
  105. ctx_isp->dbg_monitors.event_record[event][iterator].req_id = 0;
  106. }
  107. ctx_isp->dbg_monitors.event_record[event][iterator].timestamp = cur_time;
  108. if (event_data == NULL)
  109. return;
  110. /* Update event specific data */
  111. ctx_isp->dbg_monitors.event_record[event][iterator].event_type = event;
  112. if (event == CAM_ISP_CTX_EVENT_SHUTTER) {
  113. ctx_isp->dbg_monitors.event_record[event][iterator].req_id =
  114. ((struct shutter_event *)event_data)->req_id;
  115. ctx_isp->dbg_monitors.event_record[event][iterator].event.shutter_event.req_id =
  116. ((struct shutter_event *)event_data)->req_id;
  117. ctx_isp->dbg_monitors.event_record[event][iterator].event.shutter_event.status =
  118. ((struct shutter_event *)event_data)->status;
  119. ctx_isp->dbg_monitors.event_record[event][iterator].event.shutter_event.frame_id =
  120. ((struct shutter_event *)event_data)->frame_id;
  121. ctx_isp->dbg_monitors.event_record[event][iterator].event.shutter_event.boot_ts =
  122. ((struct shutter_event *)event_data)->boot_ts;
  123. ctx_isp->dbg_monitors.event_record[event][iterator].event.shutter_event.sof_ts =
  124. ((struct shutter_event *)event_data)->sof_ts;
  125. }
  126. }
  127. static int __cam_isp_ctx_handle_sof_freeze_evt(
  128. struct cam_context *ctx)
  129. {
  130. int rc = 0;
  131. struct cam_isp_context *ctx_isp;
  132. struct cam_hw_cmd_args hw_cmd_args;
  133. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  134. ctx_isp = (struct cam_isp_context *)ctx->ctx_priv;
  135. hw_cmd_args.ctxt_to_hw_map = ctx->ctxt_to_hw_map;
  136. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  137. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_CMD_SOF_DEBUG;
  138. isp_hw_cmd_args.u.sof_irq_enable = 1;
  139. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  140. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  141. &hw_cmd_args);
  142. ctx_isp->sof_dbg_irq_en = true;
  143. return rc;
  144. }
  145. static void *cam_isp_ctx_user_dump_events(
  146. void *dump_struct, uint8_t *addr_ptr)
  147. {
  148. uint64_t *addr;
  149. struct cam_isp_context_event_record *record;
  150. struct timespec64 ts;
  151. record = (struct cam_isp_context_event_record *)dump_struct;
  152. addr = (uint64_t *)addr_ptr;
  153. ts = ktime_to_timespec64(record->timestamp);
  154. *addr++ = record->req_id;
  155. *addr++ = ts.tv_sec;
  156. *addr++ = ts.tv_nsec / NSEC_PER_USEC;
  157. return addr;
  158. }
  159. static int __cam_isp_ctx_print_event_record(struct cam_isp_context *ctx_isp)
  160. {
  161. int i, j, rc = 0;
  162. int index;
  163. uint32_t oldest_entry, num_entries;
  164. uint64_t state_head;
  165. struct cam_isp_context_event_record *record;
  166. uint32_t len;
  167. uint8_t buf[CAM_ISP_CONTEXT_DBG_BUF_LEN];
  168. struct timespec64 ts;
  169. struct cam_context *ctx = ctx_isp->base;
  170. for (i = 0; i < CAM_ISP_CTX_EVENT_MAX; i++) {
  171. state_head = atomic64_read(&ctx_isp->dbg_monitors.event_record_head[i]);
  172. if (state_head == -1) {
  173. return 0;
  174. } else if (state_head < CAM_ISP_CTX_EVENT_RECORD_MAX_ENTRIES) {
  175. num_entries = state_head + 1;
  176. oldest_entry = 0;
  177. } else {
  178. num_entries = CAM_ISP_CTX_EVENT_RECORD_MAX_ENTRIES;
  179. div_u64_rem(state_head + 1,
  180. CAM_ISP_CTX_EVENT_RECORD_MAX_ENTRIES,
  181. &oldest_entry);
  182. }
  183. index = oldest_entry;
  184. len = 0;
  185. memset(buf, 0, CAM_ISP_CONTEXT_DBG_BUF_LEN);
  186. for (j = 0; j < num_entries; j++) {
  187. record = &ctx_isp->dbg_monitors.event_record[i][index];
  188. ts = ktime_to_timespec64(record->timestamp);
  189. if (len >= CAM_ISP_CONTEXT_DBG_BUF_LEN) {
  190. CAM_WARN(CAM_ISP, "Overshooting buffer length %llu", len);
  191. break;
  192. }
  193. if (record->event_type != CAM_ISP_CTX_EVENT_SHUTTER)
  194. len += scnprintf(buf + len, CAM_ISP_CONTEXT_DBG_BUF_LEN - len,
  195. "%llu[%lld:%06lld] ", record->req_id, ts.tv_sec,
  196. ts.tv_nsec / NSEC_PER_USEC);
  197. else
  198. /*
  199. * Output format
  200. * req Id[timestamp] status frmId softs bootts
  201. */
  202. len += scnprintf(buf + len, (CAM_ISP_CONTEXT_DBG_BUF_LEN) - len,
  203. "%llu[%lld:%06lld] [%d %lld %llu %llu] | ",
  204. record->req_id, ts.tv_sec,
  205. ts.tv_nsec / NSEC_PER_USEC,
  206. record->event.shutter_event.status,
  207. record->event.shutter_event.frame_id,
  208. record->event.shutter_event.sof_ts,
  209. record->event.shutter_event.boot_ts);
  210. index = (index + 1) %
  211. CAM_ISP_CTX_EVENT_RECORD_MAX_ENTRIES;
  212. }
  213. if (len)
  214. CAM_INFO(CAM_ISP, "Ctx:%d %s: %s",
  215. ctx->ctx_id, __cam_isp_evt_val_to_type(i), buf);
  216. }
  217. return rc;
  218. }
  219. static int __cam_isp_ctx_dump_event_record(
  220. struct cam_isp_context *ctx_isp,
  221. struct cam_common_hw_dump_args *dump_args)
  222. {
  223. int i, j, rc = 0;
  224. int index;
  225. size_t remain_len;
  226. uint32_t oldest_entry, num_entries;
  227. uint32_t min_len;
  228. uint64_t state_head;
  229. struct cam_isp_context_event_record *record;
  230. if (!dump_args || !ctx_isp) {
  231. CAM_ERR(CAM_ISP, "Invalid args %pK %pK",
  232. dump_args, ctx_isp);
  233. return -EINVAL;
  234. }
  235. for (i = 0; i < CAM_ISP_CTX_EVENT_MAX; i++) {
  236. state_head = atomic64_read(&ctx_isp->dbg_monitors.event_record_head[i]);
  237. if (state_head == -1)
  238. return 0;
  239. else if (state_head < CAM_ISP_CTX_EVENT_RECORD_MAX_ENTRIES) {
  240. num_entries = state_head + 1;
  241. oldest_entry = 0;
  242. } else {
  243. num_entries = CAM_ISP_CTX_EVENT_RECORD_MAX_ENTRIES;
  244. div_u64_rem(state_head + 1, CAM_ISP_CTX_EVENT_RECORD_MAX_ENTRIES,
  245. &oldest_entry);
  246. }
  247. index = oldest_entry;
  248. if (dump_args->buf_len <= dump_args->offset) {
  249. CAM_WARN(CAM_ISP, "Dump buffer overshoot len %zu offset %zu",
  250. dump_args->buf_len, dump_args->offset);
  251. return -ENOSPC;
  252. }
  253. min_len = sizeof(struct cam_isp_context_dump_header) +
  254. ((num_entries * CAM_ISP_CTX_DUMP_EVENT_NUM_WORDS) *
  255. sizeof(uint64_t));
  256. remain_len = dump_args->buf_len - dump_args->offset;
  257. if (remain_len < min_len) {
  258. CAM_WARN(CAM_ISP,
  259. "Dump buffer exhaust remain %zu min %u",
  260. remain_len, min_len);
  261. return -ENOSPC;
  262. }
  263. for (j = 0; j < num_entries; j++) {
  264. record = &ctx_isp->dbg_monitors.event_record[i][index];
  265. rc = cam_common_user_dump_helper(dump_args, cam_isp_ctx_user_dump_events,
  266. record, sizeof(uint64_t), "ISP_EVT_%s:",
  267. __cam_isp_evt_val_to_type(i));
  268. if (rc) {
  269. CAM_ERR(CAM_ISP,
  270. "CAM_ISP_CONTEXT DUMP_EVENT_RECORD: Dump failed, rc: %d",
  271. rc);
  272. return rc;
  273. }
  274. index = (index + 1) % CAM_ISP_CTX_EVENT_RECORD_MAX_ENTRIES;
  275. }
  276. }
  277. return rc;
  278. }
  279. static void __cam_isp_ctx_req_mini_dump(struct cam_ctx_request *req,
  280. uint8_t *start_addr, uint8_t *end_addr,
  281. unsigned long *bytes_updated)
  282. {
  283. struct cam_isp_ctx_req_mini_dump *req_md;
  284. struct cam_buf_io_cfg *io_cfg;
  285. struct cam_isp_ctx_req *req_isp;
  286. struct cam_packet *packet = NULL;
  287. unsigned long bytes_required = 0;
  288. bytes_required = sizeof(*req_md);
  289. *bytes_updated = 0;
  290. if (start_addr + bytes_required > end_addr)
  291. return;
  292. req_md = (struct cam_isp_ctx_req_mini_dump *)start_addr;
  293. req_isp = (struct cam_isp_ctx_req *)req->req_priv;
  294. req_md->num_acked = req_isp->num_acked;
  295. req_md->num_deferred_acks = req_isp->num_deferred_acks;
  296. req_md->bubble_report = req_isp->bubble_report;
  297. req_md->bubble_detected = req_isp->bubble_detected;
  298. req_md->reapply_type = req_isp->reapply_type;
  299. req_md->request_id = req->request_id;
  300. *bytes_updated += bytes_required;
  301. if (req_isp->num_fence_map_out) {
  302. bytes_required = sizeof(struct cam_hw_fence_map_entry) *
  303. req_isp->num_fence_map_out;
  304. if (start_addr + *bytes_updated + bytes_required > end_addr)
  305. return;
  306. req_md->map_out = (struct cam_hw_fence_map_entry *)
  307. ((uint8_t *)start_addr + *bytes_updated);
  308. memcpy(req_md->map_out, req_isp->fence_map_out, bytes_required);
  309. req_md->num_fence_map_out = req_isp->num_fence_map_out;
  310. *bytes_updated += bytes_required;
  311. }
  312. if (req_isp->num_fence_map_in) {
  313. bytes_required = sizeof(struct cam_hw_fence_map_entry) *
  314. req_isp->num_fence_map_in;
  315. if (start_addr + *bytes_updated + bytes_required > end_addr)
  316. return;
  317. req_md->map_in = (struct cam_hw_fence_map_entry *)
  318. ((uint8_t *)start_addr + *bytes_updated);
  319. memcpy(req_md->map_in, req_isp->fence_map_in, bytes_required);
  320. req_md->num_fence_map_in = req_isp->num_fence_map_in;
  321. *bytes_updated += bytes_required;
  322. }
  323. packet = req_isp->hw_update_data.packet;
  324. if (packet && packet->num_io_configs) {
  325. bytes_required = packet->num_io_configs * sizeof(struct cam_buf_io_cfg);
  326. if (start_addr + *bytes_updated + bytes_required > end_addr)
  327. return;
  328. io_cfg = (struct cam_buf_io_cfg *)((uint32_t *)&packet->payload +
  329. packet->io_configs_offset / 4);
  330. req_md->io_cfg = (struct cam_buf_io_cfg *)((uint8_t *)start_addr + *bytes_updated);
  331. memcpy(req_md->io_cfg, io_cfg, bytes_required);
  332. *bytes_updated += bytes_required;
  333. req_md->num_io_cfg = packet->num_io_configs;
  334. }
  335. }
  336. static int __cam_isp_ctx_minidump_cb(void *priv, void *args)
  337. {
  338. struct cam_isp_ctx_mini_dump_info *md;
  339. struct cam_isp_context *ctx_isp;
  340. struct cam_context *ctx;
  341. struct cam_ctx_request *req, *req_temp;
  342. struct cam_hw_mini_dump_args *dump_args;
  343. uint8_t *start_addr;
  344. uint8_t *end_addr;
  345. unsigned long total_bytes = 0;
  346. unsigned long bytes_updated = 0;
  347. uint32_t i;
  348. if (!priv || !args) {
  349. CAM_ERR(CAM_ISP, "invalid params");
  350. return 0;
  351. }
  352. dump_args = (struct cam_hw_mini_dump_args *)args;
  353. if (dump_args->len < sizeof(*md)) {
  354. CAM_ERR(CAM_ISP,
  355. "In sufficient size received %lu required size: %zu",
  356. dump_args->len, sizeof(*md));
  357. return 0;
  358. }
  359. ctx = (struct cam_context *)priv;
  360. ctx_isp = (struct cam_isp_context *)ctx->ctx_priv;
  361. start_addr = (uint8_t *)dump_args->start_addr;
  362. end_addr = start_addr + dump_args->len;
  363. md = (struct cam_isp_ctx_mini_dump_info *)dump_args->start_addr;
  364. md->sof_timestamp_val = ctx_isp->sof_timestamp_val;
  365. md->boot_timestamp = ctx_isp->boot_timestamp;
  366. md->last_sof_timestamp = ctx_isp->last_sof_timestamp;
  367. md->init_timestamp = ctx_isp->init_timestamp;
  368. md->frame_id = ctx_isp->frame_id;
  369. md->reported_req_id = ctx_isp->reported_req_id;
  370. md->last_applied_req_id = ctx_isp->last_applied_req_id;
  371. md->last_bufdone_err_apply_req_id =
  372. ctx_isp->last_bufdone_err_apply_req_id;
  373. md->frame_id_meta = ctx_isp->frame_id_meta;
  374. md->substate_activated = ctx_isp->substate_activated;
  375. md->ctx_id = ctx->ctx_id;
  376. md->subscribe_event = ctx_isp->subscribe_event;
  377. md->bubble_frame_cnt = ctx_isp->bubble_frame_cnt;
  378. md->isp_device_type = ctx_isp->isp_device_type;
  379. md->active_req_cnt = ctx_isp->active_req_cnt;
  380. md->trigger_id = ctx_isp->trigger_id;
  381. md->rdi_only_context = ctx_isp->rdi_only_context;
  382. md->offline_context = ctx_isp->offline_context;
  383. md->hw_acquired = ctx_isp->hw_acquired;
  384. md->init_received = ctx_isp->init_received;
  385. md->split_acquire = ctx_isp->split_acquire;
  386. md->use_frame_header_ts = ctx_isp->use_frame_header_ts;
  387. md->support_consumed_addr = ctx_isp->support_consumed_addr;
  388. md->use_default_apply = ctx_isp->use_default_apply;
  389. md->apply_in_progress = atomic_read(&ctx_isp->apply_in_progress);
  390. md->process_bubble = atomic_read(&ctx_isp->process_bubble);
  391. md->rxd_epoch = atomic_read(&ctx_isp->rxd_epoch);
  392. for (i = 0; i < CAM_ISP_CTX_EVENT_MAX; i++) {
  393. memcpy(md->event_record[i], ctx_isp->dbg_monitors.event_record[i],
  394. sizeof(struct cam_isp_context_event_record) *
  395. CAM_ISP_CTX_EVENT_RECORD_MAX_ENTRIES);
  396. }
  397. total_bytes += sizeof(*md);
  398. if (start_addr + total_bytes >= end_addr)
  399. goto end;
  400. if (!list_empty(&ctx->active_req_list)) {
  401. md->active_list = (struct cam_isp_ctx_req_mini_dump *)
  402. (start_addr + total_bytes);
  403. list_for_each_entry_safe(req, req_temp, &ctx->active_req_list, list) {
  404. bytes_updated = 0;
  405. __cam_isp_ctx_req_mini_dump(req,
  406. (uint8_t *)&md->active_list[md->active_cnt++],
  407. end_addr, &bytes_updated);
  408. total_bytes += bytes_updated;
  409. if ((start_addr + total_bytes >= end_addr))
  410. goto end;
  411. }
  412. }
  413. if (!list_empty(&ctx->wait_req_list)) {
  414. md->wait_list = (struct cam_isp_ctx_req_mini_dump *)
  415. (start_addr + total_bytes);
  416. list_for_each_entry_safe(req, req_temp, &ctx->wait_req_list, list) {
  417. bytes_updated = 0;
  418. __cam_isp_ctx_req_mini_dump(req,
  419. (uint8_t *)&md->wait_list[md->wait_cnt++],
  420. end_addr, &bytes_updated);
  421. total_bytes += bytes_updated;
  422. if ((start_addr + total_bytes >= end_addr))
  423. goto end;
  424. }
  425. }
  426. if (!list_empty(&ctx->pending_req_list)) {
  427. md->pending_list = (struct cam_isp_ctx_req_mini_dump *)
  428. (start_addr + total_bytes);
  429. list_for_each_entry_safe(req, req_temp, &ctx->pending_req_list, list) {
  430. bytes_updated = 0;
  431. __cam_isp_ctx_req_mini_dump(req,
  432. (uint8_t *)&md->pending_list[md->pending_cnt++],
  433. end_addr, &bytes_updated);
  434. total_bytes += bytes_updated;
  435. if ((start_addr + total_bytes >= end_addr))
  436. goto end;
  437. }
  438. }
  439. end:
  440. dump_args->bytes_written = total_bytes;
  441. return 0;
  442. }
  443. static void __cam_isp_ctx_update_state_monitor_array(
  444. struct cam_isp_context *ctx_isp,
  445. enum cam_isp_state_change_trigger trigger_type,
  446. uint64_t req_id)
  447. {
  448. int iterator;
  449. struct cam_context *ctx = ctx_isp->base;
  450. INC_HEAD(&ctx_isp->dbg_monitors.state_monitor_head,
  451. CAM_ISP_CTX_STATE_MONITOR_MAX_ENTRIES, &iterator);
  452. ctx_isp->dbg_monitors.state_monitor[iterator].curr_state =
  453. ctx_isp->substate_activated;
  454. ctx_isp->dbg_monitors.state_monitor[iterator].frame_id =
  455. ctx_isp->frame_id;
  456. ctx_isp->dbg_monitors.state_monitor[iterator].trigger =
  457. trigger_type;
  458. ctx_isp->dbg_monitors.state_monitor[iterator].req_id =
  459. req_id;
  460. if (trigger_type == CAM_ISP_STATE_CHANGE_TRIGGER_CDM_DONE)
  461. ctx_isp->dbg_monitors.state_monitor[iterator].evt_time_stamp =
  462. ctx->cdm_done_ts;
  463. else
  464. ktime_get_clocktai_ts64(
  465. &ctx_isp->dbg_monitors.state_monitor[iterator].evt_time_stamp);
  466. }
  467. static int __cam_isp_ctx_update_frame_timing_record(
  468. enum cam_isp_hw_event_type hw_evt, struct cam_isp_context *ctx_isp)
  469. {
  470. uint32_t index = 0;
  471. /* Update event index on IFE SOF - primary event */
  472. if (hw_evt == CAM_ISP_HW_EVENT_SOF)
  473. INC_HEAD(&ctx_isp->dbg_monitors.frame_monitor_head,
  474. CAM_ISP_CTX_MAX_FRAME_RECORDS, &index);
  475. else
  476. div_u64_rem(atomic64_read(&ctx_isp->dbg_monitors.frame_monitor_head),
  477. CAM_ISP_CTX_MAX_FRAME_RECORDS, &index);
  478. switch (hw_evt) {
  479. case CAM_ISP_HW_EVENT_SOF:
  480. CAM_GET_BOOT_TIMESTAMP(ctx_isp->dbg_monitors.frame_monitor[index].sof_ts);
  481. break;
  482. case CAM_ISP_HW_EVENT_EOF:
  483. CAM_GET_BOOT_TIMESTAMP(ctx_isp->dbg_monitors.frame_monitor[index].eof_ts);
  484. break;
  485. case CAM_ISP_HW_EVENT_EPOCH:
  486. CAM_GET_BOOT_TIMESTAMP(ctx_isp->dbg_monitors.frame_monitor[index].epoch_ts);
  487. break;
  488. case CAM_ISP_HW_SECONDARY_EVENT:
  489. CAM_GET_BOOT_TIMESTAMP(ctx_isp->dbg_monitors.frame_monitor[index].secondary_sof_ts);
  490. break;
  491. default:
  492. break;
  493. }
  494. return 0;
  495. }
  496. static void __cam_isp_ctx_dump_frame_timing_record(
  497. struct cam_isp_context *ctx_isp)
  498. {
  499. int i = 0;
  500. int64_t state_head = 0;
  501. uint32_t index, num_entries, oldest_entry;
  502. state_head = atomic64_read(&ctx_isp->dbg_monitors.frame_monitor_head);
  503. if (state_head == -1)
  504. return;
  505. if (state_head < CAM_ISP_CTX_MAX_FRAME_RECORDS) {
  506. num_entries = state_head + 1;
  507. oldest_entry = 0;
  508. } else {
  509. num_entries = CAM_ISP_CTX_MAX_FRAME_RECORDS;
  510. div_u64_rem(state_head + 1, CAM_ISP_CTX_MAX_FRAME_RECORDS,
  511. &oldest_entry);
  512. }
  513. index = oldest_entry;
  514. for (i = 0; i < num_entries; i++) {
  515. CAM_INFO(CAM_ISP,
  516. "Index: %u SOF_TS: %llu.%llu EPOCH_TS: %llu.%llu EOF_TS: %llu.%llu SEC_SOF: %llu.%llu",
  517. index,
  518. ctx_isp->dbg_monitors.frame_monitor[index].sof_ts.tv_sec,
  519. ctx_isp->dbg_monitors.frame_monitor[index].sof_ts.tv_nsec / NSEC_PER_USEC,
  520. ctx_isp->dbg_monitors.frame_monitor[index].epoch_ts.tv_sec,
  521. ctx_isp->dbg_monitors.frame_monitor[index].epoch_ts.tv_nsec / NSEC_PER_USEC,
  522. ctx_isp->dbg_monitors.frame_monitor[index].eof_ts.tv_sec,
  523. ctx_isp->dbg_monitors.frame_monitor[index].eof_ts.tv_nsec / NSEC_PER_USEC,
  524. ctx_isp->dbg_monitors.frame_monitor[index].secondary_sof_ts.tv_sec,
  525. ctx_isp->dbg_monitors.frame_monitor[index].secondary_sof_ts.tv_nsec /
  526. NSEC_PER_USEC);
  527. index = (index + 1) % CAM_ISP_CTX_MAX_FRAME_RECORDS;
  528. }
  529. }
  530. static const char *__cam_isp_ctx_substate_val_to_type(
  531. enum cam_isp_ctx_activated_substate type)
  532. {
  533. switch (type) {
  534. case CAM_ISP_CTX_ACTIVATED_SOF:
  535. return "SOF";
  536. case CAM_ISP_CTX_ACTIVATED_APPLIED:
  537. return "APPLIED";
  538. case CAM_ISP_CTX_ACTIVATED_EPOCH:
  539. return "EPOCH";
  540. case CAM_ISP_CTX_ACTIVATED_BUBBLE:
  541. return "BUBBLE";
  542. case CAM_ISP_CTX_ACTIVATED_BUBBLE_APPLIED:
  543. return "BUBBLE_APPLIED";
  544. case CAM_ISP_CTX_ACTIVATED_HW_ERROR:
  545. return "HW_ERROR";
  546. case CAM_ISP_CTX_ACTIVATED_HALT:
  547. return "HALT";
  548. default:
  549. return "INVALID";
  550. }
  551. }
  552. static const char *__cam_isp_hw_evt_val_to_type(
  553. uint32_t evt_id)
  554. {
  555. switch (evt_id) {
  556. case CAM_ISP_STATE_CHANGE_TRIGGER_ERROR:
  557. return "ERROR";
  558. case CAM_ISP_STATE_CHANGE_TRIGGER_APPLIED:
  559. return "APPLIED";
  560. case CAM_ISP_STATE_CHANGE_TRIGGER_SOF:
  561. return "SOF";
  562. case CAM_ISP_STATE_CHANGE_TRIGGER_REG_UPDATE:
  563. return "REG_UPDATE";
  564. case CAM_ISP_STATE_CHANGE_TRIGGER_EPOCH:
  565. return "EPOCH";
  566. case CAM_ISP_STATE_CHANGE_TRIGGER_EOF:
  567. return "EOF";
  568. case CAM_ISP_STATE_CHANGE_TRIGGER_CDM_DONE:
  569. return "CDM_DONE";
  570. case CAM_ISP_STATE_CHANGE_TRIGGER_DONE:
  571. return "DONE";
  572. case CAM_ISP_STATE_CHANGE_TRIGGER_FLUSH:
  573. return "FLUSH";
  574. case CAM_ISP_STATE_CHANGE_TRIGGER_SEC_EVT_SOF:
  575. return "SEC_EVT_SOF";
  576. case CAM_ISP_STATE_CHANGE_TRIGGER_SEC_EVT_EPOCH:
  577. return "SEC_EVT_EPOCH";
  578. case CAM_ISP_STATE_CHANGE_TRIGGER_FRAME_DROP:
  579. return "OUT_OF_SYNC_FRAME_DROP";
  580. default:
  581. return "CAM_ISP_EVENT_INVALID";
  582. }
  583. }
  584. static void __cam_isp_ctx_dump_state_monitor_array(
  585. struct cam_isp_context *ctx_isp)
  586. {
  587. int i = 0;
  588. int64_t state_head = 0;
  589. uint32_t index, num_entries, oldest_entry;
  590. struct tm ts;
  591. state_head = atomic64_read(&ctx_isp->dbg_monitors.state_monitor_head);
  592. if (state_head == -1)
  593. return;
  594. if (state_head < CAM_ISP_CTX_STATE_MONITOR_MAX_ENTRIES) {
  595. num_entries = state_head + 1;
  596. oldest_entry = 0;
  597. } else {
  598. num_entries = CAM_ISP_CTX_STATE_MONITOR_MAX_ENTRIES;
  599. div_u64_rem(state_head + 1, CAM_ISP_CTX_STATE_MONITOR_MAX_ENTRIES,
  600. &oldest_entry);
  601. }
  602. CAM_ERR(CAM_ISP,
  603. "Dumping state information for preceding requests");
  604. index = oldest_entry;
  605. for (i = 0; i < num_entries; i++) {
  606. time64_to_tm(ctx_isp->dbg_monitors.state_monitor[index].evt_time_stamp.tv_sec,
  607. 0, &ts);
  608. CAM_ERR(CAM_ISP,
  609. "Idx[%d] time[%d-%d %d:%d:%d.%lld]:Substate[%s] Frame[%lld] Req[%llu] evt[%s]",
  610. index, ts.tm_mon + 1, ts.tm_mday, ts.tm_hour, ts.tm_min, ts.tm_sec,
  611. ctx_isp->dbg_monitors.state_monitor[index].evt_time_stamp.tv_nsec / 1000000,
  612. __cam_isp_ctx_substate_val_to_type(
  613. ctx_isp->dbg_monitors.state_monitor[index].curr_state),
  614. ctx_isp->dbg_monitors.state_monitor[index].frame_id,
  615. ctx_isp->dbg_monitors.state_monitor[index].req_id,
  616. __cam_isp_hw_evt_val_to_type(
  617. ctx_isp->dbg_monitors.state_monitor[index].trigger));
  618. index = (index + 1) % CAM_ISP_CTX_STATE_MONITOR_MAX_ENTRIES;
  619. }
  620. }
  621. static void *cam_isp_ctx_user_dump_state_monitor_array_info(
  622. void *dump_struct, uint8_t *addr_ptr)
  623. {
  624. struct cam_isp_context_state_monitor *evt = NULL;
  625. uint64_t *addr;
  626. evt = (struct cam_isp_context_state_monitor *)dump_struct;
  627. addr = (uint64_t *)addr_ptr;
  628. *addr++ = evt->evt_time_stamp.tv_sec;
  629. *addr++ = evt->evt_time_stamp.tv_nsec / NSEC_PER_USEC;
  630. *addr++ = evt->frame_id;
  631. *addr++ = evt->req_id;
  632. return addr;
  633. }
  634. static int __cam_isp_ctx_user_dump_state_monitor_array(
  635. struct cam_isp_context *ctx_isp,
  636. struct cam_common_hw_dump_args *dump_args)
  637. {
  638. int i, rc = 0;
  639. int index;
  640. uint32_t oldest_entry;
  641. uint32_t num_entries;
  642. uint64_t state_head = 0;
  643. if (!dump_args || !ctx_isp) {
  644. CAM_ERR(CAM_ISP, "Invalid args %pK %pK",
  645. dump_args, ctx_isp);
  646. return -EINVAL;
  647. }
  648. state_head = atomic64_read(&ctx_isp->dbg_monitors.state_monitor_head);
  649. if (state_head == -1) {
  650. return 0;
  651. } else if (state_head < CAM_ISP_CTX_STATE_MONITOR_MAX_ENTRIES) {
  652. num_entries = state_head;
  653. oldest_entry = 0;
  654. } else {
  655. num_entries = CAM_ISP_CTX_STATE_MONITOR_MAX_ENTRIES;
  656. div_u64_rem(state_head + 1,
  657. CAM_ISP_CTX_STATE_MONITOR_MAX_ENTRIES, &oldest_entry);
  658. }
  659. CAM_ERR(CAM_ISP,
  660. "Dumping state information for preceding requests");
  661. index = oldest_entry;
  662. for (i = 0; i < num_entries; i++) {
  663. rc = cam_common_user_dump_helper(dump_args,
  664. cam_isp_ctx_user_dump_state_monitor_array_info,
  665. &ctx_isp->dbg_monitors.state_monitor[index],
  666. sizeof(uint64_t), "ISP_STATE_MONITOR.%s.%s:",
  667. __cam_isp_ctx_substate_val_to_type(
  668. ctx_isp->dbg_monitors.state_monitor[index].curr_state),
  669. __cam_isp_hw_evt_val_to_type(
  670. ctx_isp->dbg_monitors.state_monitor[index].trigger));
  671. if (rc) {
  672. CAM_ERR(CAM_ISP, "CAM ISP CONTEXT: Event record dump failed, rc: %d", rc);
  673. return rc;
  674. }
  675. index = (index + 1) % CAM_ISP_CTX_STATE_MONITOR_MAX_ENTRIES;
  676. }
  677. return rc;
  678. }
  679. static int cam_isp_context_info_dump(void *context,
  680. enum cam_context_dump_id id)
  681. {
  682. struct cam_context *ctx = (struct cam_context *)context;
  683. switch (id) {
  684. case CAM_CTX_DUMP_ACQ_INFO: {
  685. cam_context_dump_hw_acq_info(ctx);
  686. break;
  687. }
  688. default:
  689. CAM_DBG(CAM_ISP, "DUMP id not valid %u, ctx_idx: %u, link: 0x%x",
  690. id, ctx->ctx_id, ctx->link_hdl);
  691. break;
  692. }
  693. return 0;
  694. }
  695. static const char *__cam_isp_ctx_crm_trigger_point_to_string(
  696. int trigger_point)
  697. {
  698. switch (trigger_point) {
  699. case CAM_TRIGGER_POINT_SOF:
  700. return "SOF";
  701. case CAM_TRIGGER_POINT_EOF:
  702. return "EOF";
  703. default:
  704. return "Invalid";
  705. }
  706. }
  707. static int __cam_isp_ctx_notify_trigger_util(
  708. int trigger_type, struct cam_isp_context *ctx_isp)
  709. {
  710. int rc = -EINVAL;
  711. struct cam_context *ctx = ctx_isp->base;
  712. struct cam_req_mgr_trigger_notify notify;
  713. /* Trigger type not supported, return */
  714. if (!(ctx_isp->subscribe_event & trigger_type)) {
  715. CAM_DBG(CAM_ISP,
  716. "%s trigger point not subscribed for in mask: %u in ctx: %u on link: 0x%x last_bufdone: %lld",
  717. __cam_isp_ctx_crm_trigger_point_to_string(trigger_type),
  718. ctx_isp->subscribe_event, ctx->ctx_id, ctx->link_hdl,
  719. ctx_isp->req_info.last_bufdone_req_id);
  720. return 0;
  721. }
  722. /* Skip CRM notify when recovery is in progress */
  723. if (atomic_read(&ctx_isp->internal_recovery_set)) {
  724. CAM_DBG(CAM_ISP,
  725. "Internal recovery in progress skip notifying %s trigger point in ctx: %u on link: 0x%x",
  726. __cam_isp_ctx_crm_trigger_point_to_string(trigger_type),
  727. ctx->ctx_id, ctx->link_hdl);
  728. return 0;
  729. }
  730. notify.link_hdl = ctx->link_hdl;
  731. notify.dev_hdl = ctx->dev_hdl;
  732. notify.frame_id = ctx_isp->frame_id;
  733. notify.trigger = trigger_type;
  734. notify.req_id = ctx_isp->req_info.last_bufdone_req_id;
  735. notify.sof_timestamp_val = ctx_isp->sof_timestamp_val;
  736. notify.trigger_id = ctx_isp->trigger_id;
  737. CAM_DBG(CAM_ISP,
  738. "Notify CRM %s on frame: %llu ctx: %u link: 0x%x last_buf_done_req: %lld",
  739. __cam_isp_ctx_crm_trigger_point_to_string(trigger_type),
  740. ctx_isp->frame_id, ctx->ctx_id, ctx->link_hdl,
  741. ctx_isp->req_info.last_bufdone_req_id);
  742. rc = ctx->ctx_crm_intf->notify_trigger(&notify);
  743. if (rc)
  744. CAM_ERR_RATE_LIMIT(CAM_ISP,
  745. "Failed to notify CRM %s on frame: %llu ctx: %u link: 0x%x last_buf_done_req: %lld rc: %d",
  746. __cam_isp_ctx_crm_trigger_point_to_string(trigger_type),
  747. ctx_isp->frame_id, ctx->ctx_id, ctx->link_hdl,
  748. ctx_isp->req_info.last_bufdone_req_id, rc);
  749. return rc;
  750. }
  751. static int __cam_isp_ctx_notify_v4l2_error_event(
  752. uint32_t error_type, uint32_t error_code,
  753. uint64_t error_request_id, struct cam_context *ctx)
  754. {
  755. int rc = 0;
  756. struct cam_req_mgr_message req_msg;
  757. req_msg.session_hdl = ctx->session_hdl;
  758. req_msg.u.err_msg.device_hdl = ctx->dev_hdl;
  759. req_msg.u.err_msg.error_type = error_type;
  760. req_msg.u.err_msg.link_hdl = ctx->link_hdl;
  761. req_msg.u.err_msg.request_id = error_request_id;
  762. req_msg.u.err_msg.resource_size = 0x0;
  763. req_msg.u.err_msg.error_code = error_code;
  764. CAM_DBG(CAM_ISP,
  765. "v4l2 error event [type: %u code: %u] for req: %llu in ctx: %u on link: 0x%x notified successfully",
  766. error_type, error_code, error_request_id, ctx->ctx_id, ctx->link_hdl);
  767. rc = cam_req_mgr_notify_message(&req_msg,
  768. V4L_EVENT_CAM_REQ_MGR_ERROR,
  769. V4L_EVENT_CAM_REQ_MGR_EVENT);
  770. if (rc)
  771. CAM_ERR(CAM_ISP,
  772. "Notifying v4l2 error [type: %u code: %u] failed for req id:%llu in ctx %u on link: 0x%x",
  773. error_type, error_code, error_request_id, ctx->ctx_id, ctx->link_hdl);
  774. return rc;
  775. }
  776. static int __cam_isp_ctx_notify_error_util(
  777. uint32_t trigger_type, enum cam_req_mgr_device_error error,
  778. uint64_t req_id, struct cam_isp_context *ctx_isp)
  779. {
  780. int rc = -EINVAL;
  781. struct cam_context *ctx = ctx_isp->base;
  782. struct cam_req_mgr_error_notify notify;
  783. notify.link_hdl = ctx->link_hdl;
  784. notify.dev_hdl = ctx->dev_hdl;
  785. notify.req_id = req_id;
  786. notify.error = error;
  787. notify.trigger = trigger_type;
  788. notify.frame_id = ctx_isp->frame_id;
  789. notify.sof_timestamp_val = ctx_isp->sof_timestamp_val;
  790. if (error == CRM_KMD_ERR_BUBBLE)
  791. CAM_WARN(CAM_ISP,
  792. "Notify CRM about bubble req: %llu frame: %llu in ctx: %u on link: 0x%x",
  793. req_id, ctx_isp->frame_id, ctx->ctx_id, ctx->link_hdl);
  794. else
  795. CAM_ERR(CAM_ISP,
  796. "Notify CRM about fatal error: %u req: %llu frame: %llu in ctx: %u on link: 0x%x",
  797. error, req_id, ctx_isp->frame_id, ctx->ctx_id, ctx->link_hdl);
  798. rc = ctx->ctx_crm_intf->notify_err(&notify);
  799. if (rc)
  800. CAM_ERR(CAM_ISP,
  801. "Failed to notify error: %u for req: %lu on ctx: %u in link: 0x%x",
  802. error, req_id, ctx->ctx_id, ctx->link_hdl);
  803. return rc;
  804. }
  805. static int __cam_isp_ctx_trigger_reg_dump(
  806. enum cam_hw_mgr_command cmd,
  807. struct cam_context *ctx)
  808. {
  809. int rc = 0;
  810. struct cam_hw_cmd_args hw_cmd_args;
  811. hw_cmd_args.ctxt_to_hw_map = ctx->ctxt_to_hw_map;
  812. hw_cmd_args.cmd_type = cmd;
  813. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  814. &hw_cmd_args);
  815. if (rc) {
  816. CAM_ERR(CAM_ISP, "Reg dump on error failed ctx: %u link: 0x%x rc: %d",
  817. ctx->ctx_id, ctx->link_hdl, rc);
  818. goto end;
  819. }
  820. CAM_DBG(CAM_ISP,
  821. "Reg dump type: %u successful in ctx: %u on link: 0x%x",
  822. cmd, ctx->ctx_id, ctx->link_hdl);
  823. end:
  824. return rc;
  825. }
  826. static int __cam_isp_ctx_pause_crm_timer(
  827. struct cam_context *ctx)
  828. {
  829. int rc = -EINVAL;
  830. struct cam_req_mgr_timer_notify timer;
  831. if (!ctx || !ctx->ctx_crm_intf)
  832. goto end;
  833. timer.link_hdl = ctx->link_hdl;
  834. timer.dev_hdl = ctx->dev_hdl;
  835. timer.state = false;
  836. rc = ctx->ctx_crm_intf->notify_timer(&timer);
  837. if (rc) {
  838. CAM_ERR(CAM_ISP, "Failed to pause sof timer in ctx: %u on link: 0x%x",
  839. ctx->ctx_id, ctx->link_hdl);
  840. goto end;
  841. }
  842. CAM_DBG(CAM_ISP, "Notify CRM to pause timer for ctx: %u link: 0x%x success",
  843. ctx->ctx_id, ctx->link_hdl);
  844. end:
  845. return rc;
  846. }
  847. static inline void __cam_isp_ctx_update_sof_ts_util(
  848. struct cam_isp_hw_sof_event_data *sof_event_data,
  849. struct cam_isp_context *ctx_isp)
  850. {
  851. /* Delayed update, skip if ts is already updated */
  852. if (ctx_isp->sof_timestamp_val == sof_event_data->timestamp)
  853. return;
  854. ctx_isp->frame_id++;
  855. ctx_isp->sof_timestamp_val = sof_event_data->timestamp;
  856. ctx_isp->boot_timestamp = sof_event_data->boot_time;
  857. }
  858. static int cam_isp_ctx_dump_req(
  859. struct cam_isp_ctx_req *req_isp,
  860. uintptr_t cpu_addr,
  861. size_t buf_len,
  862. size_t *offset,
  863. bool dump_to_buff)
  864. {
  865. int i, rc = 0;
  866. size_t len = 0;
  867. uint32_t *buf_addr;
  868. uint32_t *buf_start, *buf_end;
  869. size_t remain_len = 0;
  870. struct cam_cdm_cmd_buf_dump_info dump_info;
  871. for (i = 0; i < req_isp->num_cfg; i++) {
  872. rc = cam_packet_util_get_cmd_mem_addr(
  873. req_isp->cfg[i].handle, &buf_addr, &len);
  874. if (rc) {
  875. CAM_ERR_RATE_LIMIT(CAM_ISP,
  876. "Failed to get_cmd_mem_addr, rc=%d",
  877. rc);
  878. } else {
  879. if (req_isp->cfg[i].offset >= ((uint32_t)len)) {
  880. CAM_ERR(CAM_ISP,
  881. "Invalid offset exp %u actual %u",
  882. req_isp->cfg[i].offset, (uint32_t)len);
  883. cam_mem_put_cpu_buf(req_isp->cfg[i].handle);
  884. return -EINVAL;
  885. }
  886. remain_len = len - req_isp->cfg[i].offset;
  887. if (req_isp->cfg[i].len >
  888. ((uint32_t)remain_len)) {
  889. CAM_ERR(CAM_ISP,
  890. "Invalid len exp %u remain_len %u",
  891. req_isp->cfg[i].len,
  892. (uint32_t)remain_len);
  893. cam_mem_put_cpu_buf(req_isp->cfg[i].handle);
  894. return -EINVAL;
  895. }
  896. buf_start = (uint32_t *)((uint8_t *) buf_addr +
  897. req_isp->cfg[i].offset);
  898. buf_end = (uint32_t *)((uint8_t *) buf_start +
  899. req_isp->cfg[i].len - 1);
  900. if (dump_to_buff) {
  901. if (!cpu_addr || !offset || !buf_len) {
  902. CAM_ERR(CAM_ISP, "Invalid args");
  903. cam_mem_put_cpu_buf(req_isp->cfg[i].handle);
  904. break;
  905. }
  906. dump_info.src_start = buf_start;
  907. dump_info.src_end = buf_end;
  908. dump_info.dst_start = cpu_addr;
  909. dump_info.dst_offset = *offset;
  910. dump_info.dst_max_size = buf_len;
  911. rc = cam_cdm_util_dump_cmd_bufs_v2(
  912. &dump_info);
  913. *offset = dump_info.dst_offset;
  914. if (rc) {
  915. cam_mem_put_cpu_buf(req_isp->cfg[i].handle);
  916. return rc;
  917. }
  918. } else
  919. cam_cdm_util_dump_cmd_buf(buf_start, buf_end);
  920. cam_mem_put_cpu_buf(req_isp->cfg[i].handle);
  921. }
  922. }
  923. return rc;
  924. }
  925. static int __cam_isp_ctx_enqueue_request_in_order(
  926. struct cam_context *ctx, struct cam_ctx_request *req, bool lock)
  927. {
  928. struct cam_ctx_request *req_current;
  929. struct cam_ctx_request *req_prev;
  930. struct list_head temp_list;
  931. struct cam_isp_context *ctx_isp;
  932. INIT_LIST_HEAD(&temp_list);
  933. if (lock)
  934. spin_lock_bh(&ctx->lock);
  935. if (list_empty(&ctx->pending_req_list)) {
  936. list_add_tail(&req->list, &ctx->pending_req_list);
  937. } else {
  938. list_for_each_entry_safe_reverse(
  939. req_current, req_prev, &ctx->pending_req_list, list) {
  940. if (req->request_id < req_current->request_id) {
  941. list_del_init(&req_current->list);
  942. list_add(&req_current->list, &temp_list);
  943. continue;
  944. } else if (req->request_id == req_current->request_id) {
  945. CAM_WARN(CAM_ISP,
  946. "Received duplicated request %lld, ctx_idx: %u link: 0x%x",
  947. req->request_id, ctx->ctx_id, ctx->link_hdl);
  948. }
  949. break;
  950. }
  951. list_add_tail(&req->list, &ctx->pending_req_list);
  952. if (!list_empty(&temp_list)) {
  953. list_for_each_entry_safe(
  954. req_current, req_prev, &temp_list, list) {
  955. list_del_init(&req_current->list);
  956. list_add_tail(&req_current->list,
  957. &ctx->pending_req_list);
  958. }
  959. }
  960. }
  961. ctx_isp = (struct cam_isp_context *) ctx->ctx_priv;
  962. __cam_isp_ctx_update_event_record(ctx_isp,
  963. CAM_ISP_CTX_EVENT_SUBMIT, req, NULL);
  964. if (lock)
  965. spin_unlock_bh(&ctx->lock);
  966. return 0;
  967. }
  968. static int __cam_isp_ctx_enqueue_init_request(
  969. struct cam_context *ctx, struct cam_ctx_request *req)
  970. {
  971. int rc = 0;
  972. struct cam_ctx_request *req_old;
  973. struct cam_isp_ctx_req *req_isp_old;
  974. struct cam_isp_ctx_req *req_isp_new;
  975. struct cam_isp_prepare_hw_update_data *req_update_old;
  976. struct cam_isp_prepare_hw_update_data *req_update_new;
  977. struct cam_isp_prepare_hw_update_data *hw_update_data;
  978. struct cam_isp_fcg_config_info *fcg_info_old;
  979. struct cam_isp_fcg_config_info *fcg_info_new;
  980. spin_lock_bh(&ctx->lock);
  981. if (list_empty(&ctx->pending_req_list)) {
  982. list_add_tail(&req->list, &ctx->pending_req_list);
  983. CAM_DBG(CAM_ISP, "INIT packet added req id= %d, ctx_idx: %u, link: 0x%x",
  984. req->request_id, ctx->ctx_id, ctx->link_hdl);
  985. goto end;
  986. }
  987. req_old = list_first_entry(&ctx->pending_req_list,
  988. struct cam_ctx_request, list);
  989. req_isp_old = (struct cam_isp_ctx_req *) req_old->req_priv;
  990. req_isp_new = (struct cam_isp_ctx_req *) req->req_priv;
  991. if (req_isp_old->hw_update_data.packet_opcode_type ==
  992. CAM_ISP_PACKET_INIT_DEV) {
  993. if ((req_isp_old->num_cfg + req_isp_new->num_cfg) >=
  994. ctx->max_hw_update_entries) {
  995. CAM_WARN(CAM_ISP,
  996. "Can not merge INIT pkt num_cfgs = %d, ctx_idx: %u, link: 0x%x",
  997. (req_isp_old->num_cfg +
  998. req_isp_new->num_cfg), ctx->ctx_id, ctx->link_hdl);
  999. rc = -ENOMEM;
  1000. }
  1001. if (req_isp_old->num_fence_map_out != 0 ||
  1002. req_isp_old->num_fence_map_in != 0) {
  1003. CAM_WARN(CAM_ISP, "Invalid INIT pkt sequence, ctx_idx: %u, link: 0x%x",
  1004. ctx->ctx_id, ctx->link_hdl);
  1005. rc = -EINVAL;
  1006. }
  1007. if (!rc) {
  1008. memcpy(req_isp_old->fence_map_out,
  1009. req_isp_new->fence_map_out,
  1010. sizeof(req_isp_new->fence_map_out[0])*
  1011. req_isp_new->num_fence_map_out);
  1012. req_isp_old->num_fence_map_out =
  1013. req_isp_new->num_fence_map_out;
  1014. memcpy(req_isp_old->fence_map_in,
  1015. req_isp_new->fence_map_in,
  1016. sizeof(req_isp_new->fence_map_in[0])*
  1017. req_isp_new->num_fence_map_in);
  1018. req_isp_old->num_fence_map_in =
  1019. req_isp_new->num_fence_map_in;
  1020. /* Copy hw update entries, num_cfg is updated later */
  1021. memcpy(&req_isp_old->cfg[req_isp_old->num_cfg],
  1022. req_isp_new->cfg,
  1023. sizeof(req_isp_new->cfg[0]) *
  1024. req_isp_new->num_cfg);
  1025. memcpy(&req_old->pf_data, &req->pf_data,
  1026. sizeof(struct cam_hw_mgr_pf_request_info));
  1027. if (req_isp_new->hw_update_data.num_reg_dump_buf) {
  1028. req_update_new = &req_isp_new->hw_update_data;
  1029. req_update_old = &req_isp_old->hw_update_data;
  1030. memcpy(&req_update_old->reg_dump_buf_desc,
  1031. &req_update_new->reg_dump_buf_desc,
  1032. sizeof(struct cam_cmd_buf_desc) *
  1033. req_update_new->num_reg_dump_buf);
  1034. req_update_old->num_reg_dump_buf =
  1035. req_update_new->num_reg_dump_buf;
  1036. }
  1037. /* Update HW update params for ePCR */
  1038. hw_update_data = &req_isp_new->hw_update_data;
  1039. req_isp_old->hw_update_data.frame_header_res_id =
  1040. req_isp_new->hw_update_data.frame_header_res_id;
  1041. req_isp_old->hw_update_data.frame_header_cpu_addr =
  1042. hw_update_data->frame_header_cpu_addr;
  1043. if (req_isp_new->hw_update_data.mup_en) {
  1044. req_isp_old->hw_update_data.mup_en =
  1045. req_isp_new->hw_update_data.mup_en;
  1046. req_isp_old->hw_update_data.mup_val =
  1047. req_isp_new->hw_update_data.mup_val;
  1048. req_isp_old->hw_update_data.num_exp =
  1049. req_isp_new->hw_update_data.num_exp;
  1050. }
  1051. /* Copy FCG HW update params */
  1052. fcg_info_new = &hw_update_data->fcg_info;
  1053. fcg_info_old = &req_isp_old->hw_update_data.fcg_info;
  1054. fcg_info_old->use_current_cfg = true;
  1055. if (fcg_info_new->ife_fcg_online) {
  1056. fcg_info_old->ife_fcg_online = true;
  1057. fcg_info_old->ife_fcg_entry_idx =
  1058. req_isp_old->num_cfg +
  1059. fcg_info_new->ife_fcg_entry_idx;
  1060. memcpy(&fcg_info_old->ife_fcg_config,
  1061. &fcg_info_new->ife_fcg_config,
  1062. sizeof(struct cam_isp_fcg_config_internal));
  1063. }
  1064. if (fcg_info_new->sfe_fcg_online) {
  1065. fcg_info_old->sfe_fcg_online = true;
  1066. fcg_info_old->sfe_fcg_entry_idx =
  1067. req_isp_old->num_cfg +
  1068. fcg_info_new->sfe_fcg_entry_idx;
  1069. memcpy(&fcg_info_old->sfe_fcg_config,
  1070. &fcg_info_new->sfe_fcg_config,
  1071. sizeof(struct cam_isp_fcg_config_internal));
  1072. }
  1073. req_isp_old->num_cfg += req_isp_new->num_cfg;
  1074. req_old->request_id = req->request_id;
  1075. list_splice_init(&req->buf_tracker, &req_old->buf_tracker);
  1076. list_add_tail(&req->list, &ctx->free_req_list);
  1077. }
  1078. } else {
  1079. CAM_WARN(CAM_ISP,
  1080. "Received Update pkt before INIT pkt. req_id= %lld, ctx_idx: %u, link: 0x%x",
  1081. req->request_id, ctx->ctx_id, ctx->link_hdl);
  1082. rc = -EINVAL;
  1083. }
  1084. end:
  1085. spin_unlock_bh(&ctx->lock);
  1086. return rc;
  1087. }
  1088. static char *__cam_isp_ife_sfe_resource_handle_id_to_type(
  1089. uint32_t resource_handle)
  1090. {
  1091. switch (resource_handle) {
  1092. /* IFE output ports */
  1093. case CAM_ISP_IFE_OUT_RES_FULL: return "IFE_FULL";
  1094. case CAM_ISP_IFE_OUT_RES_DS4: return "IFE_DS4";
  1095. case CAM_ISP_IFE_OUT_RES_DS16: return "IFE_DS16";
  1096. case CAM_ISP_IFE_OUT_RES_RAW_DUMP: return "IFE_RAW_DUMP";
  1097. case CAM_ISP_IFE_OUT_RES_FD: return "IFE_FD";
  1098. case CAM_ISP_IFE_OUT_RES_PDAF: return "IFE_PDAF";
  1099. case CAM_ISP_IFE_OUT_RES_RDI_0: return "IFE_RDI_0";
  1100. case CAM_ISP_IFE_OUT_RES_RDI_1: return "IFE_RDI_1";
  1101. case CAM_ISP_IFE_OUT_RES_RDI_2: return "IFE_RDI_2";
  1102. case CAM_ISP_IFE_OUT_RES_RDI_3: return "IFE_RDI_3";
  1103. case CAM_ISP_IFE_OUT_RES_STATS_HDR_BE: return "IFE_STATS_HDR_BE";
  1104. case CAM_ISP_IFE_OUT_RES_STATS_HDR_BHIST: return "IFE_STATS_HDR_BHIST";
  1105. case CAM_ISP_IFE_OUT_RES_STATS_TL_BG: return "IFE_STATS_TL_BG";
  1106. case CAM_ISP_IFE_OUT_RES_STATS_BF: return "IFE_STATS_BF";
  1107. case CAM_ISP_IFE_OUT_RES_STATS_AWB_BG: return "IFE_STATS_AWB_BG";
  1108. case CAM_ISP_IFE_OUT_RES_STATS_BHIST: return "IFE_STATS_BHIST";
  1109. case CAM_ISP_IFE_OUT_RES_STATS_RS: return "IFE_STATS_RS";
  1110. case CAM_ISP_IFE_OUT_RES_STATS_CS: return "IFE_STATS_CS";
  1111. case CAM_ISP_IFE_OUT_RES_STATS_IHIST: return "IFE_STATS_IHIST";
  1112. case CAM_ISP_IFE_OUT_RES_FULL_DISP: return "IFE_FULL_DISP";
  1113. case CAM_ISP_IFE_OUT_RES_DS4_DISP: return "IFE_DS4_DISP";
  1114. case CAM_ISP_IFE_OUT_RES_DS16_DISP: return "IFE_DS16_DISP";
  1115. case CAM_ISP_IFE_OUT_RES_2PD: return "IFE_2PD";
  1116. case CAM_ISP_IFE_OUT_RES_LCR: return "IFE_LCR";
  1117. case CAM_ISP_IFE_OUT_RES_AWB_BFW: return "IFE_AWB_BFW";
  1118. case CAM_ISP_IFE_OUT_RES_PREPROCESS_2PD: return "IFE_PREPROCESS_2PD";
  1119. case CAM_ISP_IFE_OUT_RES_STATS_AEC_BE: return "IFE_STATS_AEC_BE";
  1120. case CAM_ISP_IFE_OUT_RES_LTM_STATS: return "IFE_LTM_STATS";
  1121. case CAM_ISP_IFE_OUT_RES_STATS_GTM_BHIST: return "IFE_STATS_GTM_BHIST";
  1122. case CAM_ISP_IFE_LITE_OUT_RES_STATS_BG: return "IFE_STATS_BG";
  1123. case CAM_ISP_IFE_LITE_OUT_RES_PREPROCESS_RAW: return "IFE_PREPROCESS_RAW";
  1124. case CAM_ISP_IFE_OUT_RES_SPARSE_PD: return "IFE_SPARSE_PD";
  1125. case CAM_ISP_IFE_OUT_RES_STATS_CAF: return "IFE_STATS_CAF";
  1126. case CAM_ISP_IFE_OUT_RES_STATS_BAYER_RS: return "IFE_STATS_BAYER_RS";
  1127. case CAM_ISP_IFE_OUT_RES_PDAF_PARSED_DATA: return "IFE_PDAF_PARSED_DATA";
  1128. case CAM_ISP_IFE_OUT_RES_STATS_ALSC: return "IFE_STATS_ALSC";
  1129. /* SFE output ports */
  1130. case CAM_ISP_SFE_OUT_RES_RDI_0: return "SFE_RDI_0";
  1131. case CAM_ISP_SFE_OUT_RES_RDI_1: return "SFE_RDI_1";
  1132. case CAM_ISP_SFE_OUT_RES_RDI_2: return "SFE_RDI_2";
  1133. case CAM_ISP_SFE_OUT_RES_RDI_3: return "SFE_RDI_3";
  1134. case CAM_ISP_SFE_OUT_RES_RDI_4: return "SFE_RDI_4";
  1135. case CAM_ISP_SFE_OUT_BE_STATS_0: return "SFE_BE_STATS_0";
  1136. case CAM_ISP_SFE_OUT_BE_STATS_1: return "SFE_BE_STATS_1";
  1137. case CAM_ISP_SFE_OUT_BE_STATS_2: return "SFE_BE_STATS_2";
  1138. case CAM_ISP_SFE_OUT_BHIST_STATS_0: return "SFE_BHIST_STATS_0";
  1139. case CAM_ISP_SFE_OUT_BHIST_STATS_1: return "SFE_BHIST_STATS_1";
  1140. case CAM_ISP_SFE_OUT_BHIST_STATS_2: return "SFE_BHIST_STATS_2";
  1141. case CAM_ISP_SFE_OUT_RES_LCR: return "SFE_LCR";
  1142. case CAM_ISP_SFE_OUT_RES_RAW_DUMP: return "SFE_PROCESSED_RAW";
  1143. case CAM_ISP_SFE_OUT_RES_IR: return "SFE_IR";
  1144. case CAM_ISP_SFE_OUT_BAYER_RS_STATS_0: return "SFE_RS_STATS_0";
  1145. case CAM_ISP_SFE_OUT_BAYER_RS_STATS_1: return "SFE_RS_STATS_1";
  1146. case CAM_ISP_SFE_OUT_BAYER_RS_STATS_2: return "SFE_RS_STATS_2";
  1147. case CAM_ISP_SFE_OUT_HDR_STATS: return "HDR_STATS";
  1148. /* SFE input ports */
  1149. case CAM_ISP_SFE_IN_RD_0: return "SFE_RD_0";
  1150. case CAM_ISP_SFE_IN_RD_1: return "SFE_RD_1";
  1151. case CAM_ISP_SFE_IN_RD_2: return "SFE_RD_2";
  1152. /* Handle invalid type */
  1153. default: return "Invalid_Resource_Type";
  1154. }
  1155. }
  1156. static const char *__cam_isp_tfe_resource_handle_id_to_type(
  1157. uint32_t resource_handle)
  1158. {
  1159. switch (resource_handle) {
  1160. /* TFE output ports */
  1161. case CAM_ISP_TFE_OUT_RES_FULL: return "TFE_FULL";
  1162. case CAM_ISP_TFE_OUT_RES_RAW_DUMP: return "TFE_RAW_DUMP";
  1163. case CAM_ISP_TFE_OUT_RES_PDAF: return "TFE_PDAF";
  1164. case CAM_ISP_TFE_OUT_RES_RDI_0: return "TFE_RDI_0";
  1165. case CAM_ISP_TFE_OUT_RES_RDI_1: return "TFE_RDI_1";
  1166. case CAM_ISP_TFE_OUT_RES_RDI_2: return "TFE_RDI_2";
  1167. case CAM_ISP_TFE_OUT_RES_STATS_HDR_BE: return "TFE_STATS_HDR_BE";
  1168. case CAM_ISP_TFE_OUT_RES_STATS_HDR_BHIST: return "TFE_STATS_HDR_BHIST";
  1169. case CAM_ISP_TFE_OUT_RES_STATS_TL_BG: return "TFE_STATS_TL_BG";
  1170. case CAM_ISP_TFE_OUT_RES_STATS_BF: return "TFE_STATS_BF";
  1171. case CAM_ISP_TFE_OUT_RES_STATS_AWB_BG: return "TFE_STATS_AWB_BG";
  1172. case CAM_ISP_TFE_OUT_RES_STATS_RS: return "TFE_STATS_RS";
  1173. case CAM_ISP_TFE_OUT_RES_DS4: return "TFE_DS_4";
  1174. case CAM_ISP_TFE_OUT_RES_DS16: return "TFE_DS_16";
  1175. case CAM_ISP_TFE_OUT_RES_AI: return "TFE_AI";
  1176. case CAM_ISP_TFE_OUT_RES_PD_LCR_STATS: return "TFE_LCR_STATS";
  1177. case CAM_ISP_TFE_OUT_RES_PD_PREPROCESSED: return "TFE_PD_PREPROCESSED";
  1178. case CAM_ISP_TFE_OUT_RES_PD_PARSED: return "TFE_PD_PARSED";
  1179. /* Handle invalid type */
  1180. default: return "Invalid_Resource_Type";
  1181. }
  1182. }
  1183. static const char *__cam_isp_resource_handle_id_to_type(
  1184. uint32_t device_type, uint32_t resource_handle)
  1185. {
  1186. switch (device_type) {
  1187. case CAM_IFE_DEVICE_TYPE:
  1188. case CAM_TFE_MC_DEVICE_TYPE:
  1189. return __cam_isp_ife_sfe_resource_handle_id_to_type(resource_handle);
  1190. case CAM_TFE_DEVICE_TYPE:
  1191. return __cam_isp_tfe_resource_handle_id_to_type(resource_handle);
  1192. default:
  1193. return "INVALID_DEV_TYPE";
  1194. }
  1195. }
  1196. static uint64_t __cam_isp_ctx_get_event_ts(uint32_t evt_id, void *evt_data)
  1197. {
  1198. uint64_t ts = 0;
  1199. if (!evt_data)
  1200. return 0;
  1201. switch (evt_id) {
  1202. case CAM_ISP_HW_EVENT_ERROR:
  1203. ts = ((struct cam_isp_hw_error_event_data *)evt_data)->
  1204. timestamp;
  1205. break;
  1206. case CAM_ISP_HW_EVENT_SOF:
  1207. ts = ((struct cam_isp_hw_sof_event_data *)evt_data)->
  1208. timestamp;
  1209. break;
  1210. case CAM_ISP_HW_EVENT_REG_UPDATE:
  1211. ts = ((struct cam_isp_hw_reg_update_event_data *)evt_data)->
  1212. timestamp;
  1213. break;
  1214. case CAM_ISP_HW_EVENT_EPOCH:
  1215. ts = ((struct cam_isp_hw_epoch_event_data *)evt_data)->
  1216. timestamp;
  1217. break;
  1218. case CAM_ISP_HW_EVENT_EOF:
  1219. ts = ((struct cam_isp_hw_eof_event_data *)evt_data)->
  1220. timestamp;
  1221. break;
  1222. case CAM_ISP_HW_EVENT_DONE:
  1223. case CAM_ISP_HW_SECONDARY_EVENT:
  1224. break;
  1225. default:
  1226. CAM_DBG(CAM_ISP, "Invalid Event Type %d", evt_id);
  1227. }
  1228. return ts;
  1229. }
  1230. static int __cam_isp_ctx_get_hw_timestamp(struct cam_context *ctx, uint64_t *prev_ts,
  1231. uint64_t *curr_ts, uint64_t *boot_ts)
  1232. {
  1233. struct cam_hw_cmd_args hw_cmd_args;
  1234. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  1235. int rc;
  1236. hw_cmd_args.ctxt_to_hw_map = ctx->ctxt_to_hw_map;
  1237. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  1238. hw_cmd_args.u.internal_args = &isp_hw_cmd_args;
  1239. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_GET_SOF_TS;
  1240. rc = ctx->hw_mgr_intf->hw_cmd(ctx->ctxt_to_hw_map, &hw_cmd_args);
  1241. if (rc)
  1242. return rc;
  1243. if (isp_hw_cmd_args.u.sof_ts.prev >= isp_hw_cmd_args.u.sof_ts.curr) {
  1244. CAM_ERR(CAM_ISP, "ctx:%u link:0x%x prev timestamp greater than curr timestamp",
  1245. ctx->ctx_id, ctx->link_hdl);
  1246. return -EINVAL;
  1247. }
  1248. *prev_ts = isp_hw_cmd_args.u.sof_ts.prev;
  1249. *curr_ts = isp_hw_cmd_args.u.sof_ts.curr;
  1250. *boot_ts = isp_hw_cmd_args.u.sof_ts.boot;
  1251. return 0;
  1252. }
  1253. static int __cam_isp_ctx_get_cdm_done_timestamp(struct cam_context *ctx,
  1254. uint64_t *last_cdm_done_req)
  1255. {
  1256. struct cam_hw_cmd_args hw_cmd_args;
  1257. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  1258. struct tm ts;
  1259. int rc;
  1260. hw_cmd_args.ctxt_to_hw_map = ctx->ctxt_to_hw_map;
  1261. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  1262. hw_cmd_args.u.internal_args = &isp_hw_cmd_args;
  1263. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_GET_LAST_CDM_DONE;
  1264. rc = ctx->hw_mgr_intf->hw_cmd(ctx->ctxt_to_hw_map, &hw_cmd_args);
  1265. if (rc)
  1266. return rc;
  1267. *last_cdm_done_req = isp_hw_cmd_args.u.last_cdm_done;
  1268. ctx->cdm_done_ts = isp_hw_cmd_args.cdm_done_ts;
  1269. time64_to_tm(isp_hw_cmd_args.cdm_done_ts.tv_sec, 0, &ts);
  1270. CAM_DBG(CAM_ISP,
  1271. "last_cdm_done req: %llu ctx: %u link: 0x%x time[%d-%d %d:%d:%d.%lld]",
  1272. last_cdm_done_req, ctx->ctx_id, ctx->link_hdl,
  1273. ts.tm_mon + 1, ts.tm_mday, ts.tm_hour, ts.tm_min, ts.tm_sec,
  1274. isp_hw_cmd_args.cdm_done_ts.tv_nsec / 1000000);
  1275. return 0;
  1276. }
  1277. static int __cam_isp_ctx_recover_sof_timestamp(struct cam_context *ctx, uint64_t request_id)
  1278. {
  1279. struct cam_isp_context *ctx_isp = ctx->ctx_priv;
  1280. uint64_t prev_ts, curr_ts, boot_ts;
  1281. uint64_t a, b, c;
  1282. int rc;
  1283. rc = __cam_isp_ctx_get_hw_timestamp(ctx, &prev_ts, &curr_ts, &boot_ts);
  1284. if (rc) {
  1285. CAM_ERR(CAM_ISP, "ctx:%u link: 0x%x Failed to get timestamp from HW",
  1286. ctx->ctx_id, ctx->link_hdl);
  1287. return rc;
  1288. }
  1289. /**
  1290. * If the last received SOF was for frame A and we have missed the SOF for frame B,
  1291. * then we need to find out if the hardware is at frame B or C.
  1292. * +-----+-----+-----+
  1293. * | A | B | C |
  1294. * +-----+-----+-----+
  1295. */
  1296. a = ctx_isp->sof_timestamp_val;
  1297. if (a == prev_ts) {
  1298. /* Hardware is at frame B */
  1299. b = curr_ts;
  1300. CAM_DBG(CAM_ISP, "ctx:%u link:0x%x recover time(last:0x%llx,curr:0x%llx)req:%llu",
  1301. ctx->ctx_id, ctx->link_hdl, a, b, request_id);
  1302. } else if (a < prev_ts) {
  1303. /* Hardware is at frame C */
  1304. b = prev_ts;
  1305. c = curr_ts;
  1306. CAM_DBG(CAM_ISP,
  1307. "ctx:%u link:0x%x recover time(last:0x%llx,prev:0x%llx,curr:0x%llx)req:%llu",
  1308. ctx->ctx_id, ctx->link_hdl, a, b, c, request_id);
  1309. } else {
  1310. /* Hardware is at frame A (which we supposedly missed) */
  1311. CAM_ERR_RATE_LIMIT(CAM_ISP,
  1312. "ctx:%u link: 0x%x erroneous call to SOF recovery(last:0x%llx, prev:0x%llx, curr:0x%llx) req: %llu",
  1313. ctx->ctx_id, ctx->link_hdl, a, prev_ts, curr_ts, request_id);
  1314. return 0;
  1315. }
  1316. ctx_isp->boot_timestamp = boot_ts + (b - curr_ts);
  1317. ctx_isp->sof_timestamp_val = b;
  1318. ctx_isp->frame_id++;
  1319. return 0;
  1320. }
  1321. static void __cam_isp_ctx_send_sof_boot_timestamp(
  1322. struct cam_isp_context *ctx_isp, uint64_t request_id,
  1323. uint32_t sof_event_status, struct shutter_event *shutter_event)
  1324. {
  1325. struct cam_req_mgr_message req_msg;
  1326. req_msg.session_hdl = ctx_isp->base->session_hdl;
  1327. req_msg.u.frame_msg.frame_id = ctx_isp->frame_id;
  1328. req_msg.u.frame_msg.request_id = request_id;
  1329. req_msg.u.frame_msg.timestamp = ctx_isp->boot_timestamp;
  1330. req_msg.u.frame_msg.link_hdl = ctx_isp->base->link_hdl;
  1331. req_msg.u.frame_msg.sof_status = sof_event_status;
  1332. req_msg.u.frame_msg.frame_id_meta = ctx_isp->frame_id_meta;
  1333. CAM_DBG(CAM_ISP,
  1334. "request id:%lld frame number:%lld boot time stamp:0x%llx status:%u",
  1335. request_id, ctx_isp->frame_id,
  1336. ctx_isp->boot_timestamp, sof_event_status);
  1337. shutter_event->frame_id = ctx_isp->frame_id;
  1338. shutter_event->req_id = request_id;
  1339. shutter_event->boot_ts = ctx_isp->boot_timestamp;
  1340. shutter_event->sof_ts = ctx_isp->sof_timestamp_val;
  1341. if (cam_req_mgr_notify_message(&req_msg,
  1342. V4L_EVENT_CAM_REQ_MGR_SOF_BOOT_TS,
  1343. V4L_EVENT_CAM_REQ_MGR_EVENT))
  1344. CAM_ERR(CAM_ISP,
  1345. "Error in notifying the boot time for req id:%lld",
  1346. request_id);
  1347. __cam_isp_ctx_update_event_record(ctx_isp,
  1348. CAM_ISP_CTX_EVENT_SHUTTER, NULL, shutter_event);
  1349. }
  1350. static void __cam_isp_ctx_send_unified_timestamp(
  1351. struct cam_isp_context *ctx_isp, uint64_t request_id,
  1352. struct shutter_event *shutter_event)
  1353. {
  1354. struct cam_req_mgr_message req_msg;
  1355. req_msg.session_hdl = ctx_isp->base->session_hdl;
  1356. req_msg.u.frame_msg_v2.frame_id = ctx_isp->frame_id;
  1357. req_msg.u.frame_msg_v2.request_id = request_id;
  1358. req_msg.u.frame_msg_v2.timestamps[CAM_REQ_SOF_QTIMER_TIMESTAMP] =
  1359. (request_id == 0) ? 0 : ctx_isp->sof_timestamp_val;
  1360. req_msg.u.frame_msg_v2.timestamps[CAM_REQ_BOOT_TIMESTAMP] = ctx_isp->boot_timestamp;
  1361. req_msg.u.frame_msg_v2.link_hdl = ctx_isp->base->link_hdl;
  1362. req_msg.u.frame_msg_v2.frame_id_meta = ctx_isp->frame_id_meta;
  1363. CAM_DBG(CAM_ISP,
  1364. "link hdl 0x%x request id:%lld frame number:%lld SOF time stamp:0x%llx ctx %d\
  1365. boot time stamp:0x%llx", ctx_isp->base->link_hdl, request_id,
  1366. ctx_isp->frame_id, ctx_isp->sof_timestamp_val,ctx_isp->base->ctx_id,
  1367. ctx_isp->boot_timestamp);
  1368. shutter_event->frame_id = ctx_isp->frame_id;
  1369. shutter_event->req_id = request_id;
  1370. shutter_event->boot_ts = ctx_isp->boot_timestamp;
  1371. shutter_event->sof_ts = ctx_isp->sof_timestamp_val;
  1372. if (cam_req_mgr_notify_message(&req_msg,
  1373. V4L_EVENT_CAM_REQ_MGR_SOF_UNIFIED_TS, V4L_EVENT_CAM_REQ_MGR_EVENT))
  1374. CAM_ERR(CAM_ISP,
  1375. "Error in notifying the sof and boot time for req id:%lld",
  1376. request_id);
  1377. __cam_isp_ctx_update_event_record(ctx_isp,
  1378. CAM_ISP_CTX_EVENT_SHUTTER, NULL, shutter_event);
  1379. }
  1380. static void __cam_isp_ctx_send_sof_timestamp_frame_header(
  1381. struct cam_isp_context *ctx_isp, uint32_t *frame_header_cpu_addr,
  1382. uint64_t request_id, uint32_t sof_event_status)
  1383. {
  1384. uint32_t *time32 = NULL;
  1385. uint64_t timestamp = 0;
  1386. struct cam_req_mgr_message req_msg;
  1387. time32 = frame_header_cpu_addr;
  1388. timestamp = (uint64_t) time32[1];
  1389. timestamp = timestamp << 24;
  1390. timestamp |= (uint64_t)(time32[0] >> 8);
  1391. timestamp = mul_u64_u32_div(timestamp,
  1392. CAM_IFE_QTIMER_MUL_FACTOR,
  1393. CAM_IFE_QTIMER_DIV_FACTOR);
  1394. ctx_isp->sof_timestamp_val = timestamp;
  1395. req_msg.session_hdl = ctx_isp->base->session_hdl;
  1396. req_msg.u.frame_msg.frame_id = ctx_isp->frame_id;
  1397. req_msg.u.frame_msg.request_id = request_id;
  1398. req_msg.u.frame_msg.timestamp = ctx_isp->sof_timestamp_val;
  1399. req_msg.u.frame_msg.link_hdl = ctx_isp->base->link_hdl;
  1400. req_msg.u.frame_msg.sof_status = sof_event_status;
  1401. CAM_DBG(CAM_ISP,
  1402. "request id:%lld frame number:%lld SOF time stamp:0x%llx status:%u",
  1403. request_id, ctx_isp->frame_id,
  1404. ctx_isp->sof_timestamp_val, sof_event_status);
  1405. if (cam_req_mgr_notify_message(&req_msg,
  1406. V4L_EVENT_CAM_REQ_MGR_SOF, V4L_EVENT_CAM_REQ_MGR_EVENT))
  1407. CAM_ERR(CAM_ISP,
  1408. "Error in notifying the sof time for req id:%lld",
  1409. request_id);
  1410. }
  1411. static void __cam_isp_ctx_send_sof_timestamp(
  1412. struct cam_isp_context *ctx_isp, uint64_t request_id,
  1413. uint32_t sof_event_status)
  1414. {
  1415. struct cam_req_mgr_message req_msg;
  1416. struct cam_context *ctx = ctx_isp->base;
  1417. struct shutter_event shutter_event = {0};
  1418. if (ctx_isp->reported_frame_id == ctx_isp->frame_id) {
  1419. if (__cam_isp_ctx_recover_sof_timestamp(ctx_isp->base, request_id))
  1420. CAM_WARN(CAM_ISP, "Missed SOF.No SOF timestamp recovery,ctx:%u,link:0x%x",
  1421. ctx->ctx_id, ctx->link_hdl);
  1422. }
  1423. if (request_id == 0 && (ctx_isp->reported_frame_id == ctx_isp->frame_id)) {
  1424. CAM_WARN_RATE_LIMIT(CAM_ISP,
  1425. "Missed SOF Recovery for invalid req, Skip notificaiton to userspace Ctx: %u link: 0x%x frame_id %u",
  1426. ctx->ctx_id, ctx->link_hdl, ctx_isp->frame_id);
  1427. return;
  1428. }
  1429. ctx_isp->reported_frame_id = ctx_isp->frame_id;
  1430. shutter_event.status = sof_event_status;
  1431. if ((ctx_isp->v4l2_event_sub_ids & (1 << V4L_EVENT_CAM_REQ_MGR_SOF_UNIFIED_TS))
  1432. && !ctx_isp->use_frame_header_ts) {
  1433. __cam_isp_ctx_send_unified_timestamp(ctx_isp, request_id, &shutter_event);
  1434. return;
  1435. }
  1436. if ((ctx_isp->use_frame_header_ts) || (request_id == 0))
  1437. goto end;
  1438. req_msg.session_hdl = ctx_isp->base->session_hdl;
  1439. req_msg.u.frame_msg.frame_id = ctx_isp->frame_id;
  1440. req_msg.u.frame_msg.request_id = request_id;
  1441. req_msg.u.frame_msg.timestamp = ctx_isp->sof_timestamp_val;
  1442. req_msg.u.frame_msg.link_hdl = ctx_isp->base->link_hdl;
  1443. req_msg.u.frame_msg.sof_status = sof_event_status;
  1444. req_msg.u.frame_msg.frame_id_meta = ctx_isp->frame_id_meta;
  1445. CAM_DBG(CAM_ISP,
  1446. "request id:%lld frame number:%lld SOF time stamp:0x%llx status:%u ctx_idx: %u, link: 0x%x",
  1447. request_id, ctx_isp->frame_id,
  1448. ctx_isp->sof_timestamp_val, sof_event_status, ctx->ctx_id, ctx->link_hdl);
  1449. if (cam_req_mgr_notify_message(&req_msg,
  1450. V4L_EVENT_CAM_REQ_MGR_SOF, V4L_EVENT_CAM_REQ_MGR_EVENT))
  1451. CAM_ERR(CAM_ISP,
  1452. "Error in notifying the sof time for req id:%lld, ctx_idx: %u, link: 0x%x",
  1453. request_id, ctx->ctx_id, ctx->link_hdl);
  1454. end:
  1455. __cam_isp_ctx_send_sof_boot_timestamp(ctx_isp,
  1456. request_id, sof_event_status, &shutter_event);
  1457. }
  1458. static void __cam_isp_ctx_handle_buf_done_fail_log(
  1459. struct cam_isp_context *ctx_isp, uint64_t request_id,
  1460. struct cam_isp_ctx_req *req_isp)
  1461. {
  1462. int i;
  1463. struct cam_context *ctx = ctx_isp->base;
  1464. const char *handle_type;
  1465. if (req_isp->num_fence_map_out >= CAM_ISP_CTX_RES_MAX) {
  1466. CAM_ERR(CAM_ISP,
  1467. "Num Resources exceed mMAX %d >= %d ",
  1468. req_isp->num_fence_map_out, CAM_ISP_CTX_RES_MAX);
  1469. return;
  1470. }
  1471. CAM_WARN_RATE_LIMIT(CAM_ISP,
  1472. "Prev Req[%lld] : num_out=%d, num_acked=%d, bubble : report=%d, detected=%d",
  1473. request_id, req_isp->num_fence_map_out, req_isp->num_acked,
  1474. req_isp->bubble_report, req_isp->bubble_detected);
  1475. CAM_WARN_RATE_LIMIT(CAM_ISP,
  1476. "Resource Handles that fail to generate buf_done in prev frame");
  1477. for (i = 0; i < req_isp->num_fence_map_out; i++) {
  1478. if (req_isp->fence_map_out[i].sync_id != -1) {
  1479. handle_type = __cam_isp_resource_handle_id_to_type(
  1480. ctx_isp->isp_device_type,
  1481. req_isp->fence_map_out[i].resource_handle);
  1482. trace_cam_log_event("Buf_done Congestion",
  1483. handle_type, request_id, req_isp->fence_map_out[i].sync_id);
  1484. CAM_WARN_RATE_LIMIT(CAM_ISP,
  1485. "Resource_Handle: [%s][0x%x] Sync_ID: [0x%x]",
  1486. handle_type,
  1487. req_isp->fence_map_out[i].resource_handle,
  1488. req_isp->fence_map_out[i].sync_id);
  1489. }
  1490. }
  1491. ctx_isp->congestion_cnt++;
  1492. /* Trigger SOF freeze debug dump on 3 or greater instances of congestion */
  1493. if ((ctx_isp->congestion_cnt >= CAM_ISP_CONTEXT_CONGESTION_CNT_MAX) &&
  1494. (!ctx_isp->sof_dbg_irq_en))
  1495. __cam_isp_ctx_handle_sof_freeze_evt(ctx);
  1496. }
  1497. static void __cam_isp_context_reset_internal_recovery_params(
  1498. struct cam_isp_context *ctx_isp)
  1499. {
  1500. atomic_set(&ctx_isp->internal_recovery_set, 0);
  1501. atomic_set(&ctx_isp->process_bubble, 0);
  1502. ctx_isp->aeb_error_cnt = 0;
  1503. ctx_isp->bubble_frame_cnt = 0;
  1504. ctx_isp->congestion_cnt = 0;
  1505. ctx_isp->sof_dbg_irq_en = false;
  1506. }
  1507. static int __cam_isp_context_try_internal_recovery(
  1508. struct cam_isp_context *ctx_isp)
  1509. {
  1510. int rc = 0;
  1511. struct cam_context *ctx = ctx_isp->base;
  1512. struct cam_ctx_request *req;
  1513. struct cam_isp_ctx_req *req_isp;
  1514. /*
  1515. * Start with wait list, if recovery is stil set
  1516. * errored request has not been moved to pending yet.
  1517. * Buf done for errored request has not occurred recover
  1518. * from here
  1519. */
  1520. if (!list_empty(&ctx->wait_req_list)) {
  1521. req = list_first_entry(&ctx->wait_req_list, struct cam_ctx_request, list);
  1522. req_isp = (struct cam_isp_ctx_req *)req->req_priv;
  1523. if (req->request_id == ctx_isp->recovery_req_id) {
  1524. rc = __cam_isp_ctx_notify_error_util(CAM_TRIGGER_POINT_SOF,
  1525. CRM_KMD_ERR_BUBBLE, ctx_isp->recovery_req_id, ctx_isp);
  1526. if (rc) {
  1527. /* Unable to do bubble recovery reset back to normal */
  1528. CAM_WARN(CAM_ISP,
  1529. "Unable to perform internal recovery [bubble reporting failed] for req: %llu in ctx: %u on link: 0x%x",
  1530. req->request_id, ctx->ctx_id, ctx->link_hdl);
  1531. __cam_isp_context_reset_internal_recovery_params(ctx_isp);
  1532. req_isp->bubble_detected = false;
  1533. goto end;
  1534. }
  1535. list_del_init(&req->list);
  1536. list_add(&req->list, &ctx->pending_req_list);
  1537. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_BUBBLE;
  1538. CAM_INFO(CAM_ISP,
  1539. "Internal recovery for req: %llu in ctx: %u on link: 0x%x triggered",
  1540. ctx_isp->recovery_req_id, ctx->ctx_id, ctx->link_hdl);
  1541. goto end;
  1542. }
  1543. }
  1544. /*
  1545. * If not in wait list only other possibility is request is in pending list
  1546. * on error detection, bubble detect is set assuming new frame after detection
  1547. * comes in, there is an rup it's moved to active list and it finishes with
  1548. * it's buf done's
  1549. */
  1550. if (!list_empty(&ctx->pending_req_list)) {
  1551. req = list_first_entry(&ctx->pending_req_list, struct cam_ctx_request, list);
  1552. req_isp = (struct cam_isp_ctx_req *)req->req_priv;
  1553. if (req->request_id == ctx_isp->recovery_req_id) {
  1554. rc = __cam_isp_ctx_notify_error_util(CAM_TRIGGER_POINT_SOF,
  1555. CRM_KMD_ERR_BUBBLE, ctx_isp->recovery_req_id, ctx_isp);
  1556. if (rc) {
  1557. /* Unable to do bubble recovery reset back to normal */
  1558. CAM_WARN(CAM_ISP,
  1559. "Unable to perform internal recovery [bubble reporting failed] for req: %llu in ctx: %u on link: 0x%x",
  1560. req->request_id, ctx->ctx_id, ctx->link_hdl);
  1561. __cam_isp_context_reset_internal_recovery_params(ctx_isp);
  1562. req_isp->bubble_detected = false;
  1563. goto end;
  1564. }
  1565. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_BUBBLE;
  1566. CAM_INFO(CAM_ISP,
  1567. "Internal recovery for req: %llu in ctx: %u on link: 0x%x triggered",
  1568. ctx_isp->recovery_req_id, ctx->ctx_id, ctx->link_hdl);
  1569. goto end;
  1570. }
  1571. }
  1572. /* If request is not found in either of the lists skip recovery */
  1573. __cam_isp_context_reset_internal_recovery_params(ctx_isp);
  1574. end:
  1575. return rc;
  1576. }
  1577. static int __cam_isp_ctx_handle_buf_done_for_req_list(
  1578. struct cam_isp_context *ctx_isp,
  1579. struct cam_ctx_request *req)
  1580. {
  1581. int rc = 0, i;
  1582. uint64_t buf_done_req_id;
  1583. struct cam_isp_ctx_req *req_isp;
  1584. struct cam_context *ctx = ctx_isp->base;
  1585. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  1586. ctx_isp->active_req_cnt--;
  1587. buf_done_req_id = req->request_id;
  1588. if (req_isp->bubble_detected && req_isp->bubble_report) {
  1589. req_isp->num_acked = 0;
  1590. req_isp->num_deferred_acks = 0;
  1591. req_isp->bubble_detected = false;
  1592. list_del_init(&req->list);
  1593. atomic_set(&ctx_isp->process_bubble, 0);
  1594. req_isp->cdm_reset_before_apply = false;
  1595. ctx_isp->bubble_frame_cnt = 0;
  1596. if (buf_done_req_id <= ctx->last_flush_req) {
  1597. cam_smmu_buffer_tracker_putref(&req->buf_tracker);
  1598. for (i = 0; i < req_isp->num_fence_map_out; i++)
  1599. rc = cam_sync_signal(
  1600. req_isp->fence_map_out[i].sync_id,
  1601. CAM_SYNC_STATE_SIGNALED_ERROR,
  1602. CAM_SYNC_ISP_EVENT_BUBBLE);
  1603. list_add_tail(&req->list, &ctx->free_req_list);
  1604. CAM_DBG(CAM_REQ,
  1605. "Move active request %lld to free list(cnt = %d) [flushed], ctx %u, link: 0x%x",
  1606. buf_done_req_id, ctx_isp->active_req_cnt,
  1607. ctx->ctx_id, ctx->link_hdl);
  1608. ctx_isp->last_bufdone_err_apply_req_id = 0;
  1609. } else {
  1610. list_add(&req->list, &ctx->pending_req_list);
  1611. CAM_DBG(CAM_REQ,
  1612. "Move active request %lld to pending list(cnt = %d) [bubble recovery], ctx %u, link: 0x%x",
  1613. req->request_id, ctx_isp->active_req_cnt,
  1614. ctx->ctx_id, ctx->link_hdl);
  1615. }
  1616. } else {
  1617. if (!ctx_isp->use_frame_header_ts) {
  1618. if (ctx_isp->reported_req_id < buf_done_req_id) {
  1619. ctx_isp->reported_req_id = buf_done_req_id;
  1620. __cam_isp_ctx_send_sof_timestamp(ctx_isp,
  1621. buf_done_req_id,
  1622. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  1623. }
  1624. }
  1625. list_del_init(&req->list);
  1626. cam_smmu_buffer_tracker_putref(&req->buf_tracker);
  1627. list_add_tail(&req->list, &ctx->free_req_list);
  1628. req_isp->reapply_type = CAM_CONFIG_REAPPLY_NONE;
  1629. req_isp->cdm_reset_before_apply = false;
  1630. req_isp->num_acked = 0;
  1631. req_isp->num_deferred_acks = 0;
  1632. /*
  1633. * Only update the process_bubble and bubble_frame_cnt
  1634. * when bubble is detected on this req, in case the other
  1635. * request is processing bubble.
  1636. */
  1637. if (req_isp->bubble_detected) {
  1638. atomic_set(&ctx_isp->process_bubble, 0);
  1639. ctx_isp->bubble_frame_cnt = 0;
  1640. req_isp->bubble_detected = false;
  1641. }
  1642. CAM_DBG(CAM_REQ,
  1643. "Move active request %lld to free list(cnt = %d) [all fences done], ctx %u link: 0x%x",
  1644. buf_done_req_id, ctx_isp->active_req_cnt, ctx->ctx_id, ctx->link_hdl);
  1645. ctx_isp->req_info.last_bufdone_req_id = req->request_id;
  1646. ctx_isp->last_bufdone_err_apply_req_id = 0;
  1647. }
  1648. if (atomic_read(&ctx_isp->internal_recovery_set) && !ctx_isp->active_req_cnt)
  1649. __cam_isp_context_try_internal_recovery(ctx_isp);
  1650. cam_cpas_notify_event("IFE BufDone", buf_done_req_id);
  1651. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  1652. CAM_ISP_STATE_CHANGE_TRIGGER_DONE, buf_done_req_id);
  1653. __cam_isp_ctx_update_event_record(ctx_isp,
  1654. CAM_ISP_CTX_EVENT_BUFDONE, req, NULL);
  1655. return rc;
  1656. }
  1657. static int __cam_isp_ctx_handle_buf_done_for_request(
  1658. struct cam_isp_context *ctx_isp,
  1659. struct cam_ctx_request *req,
  1660. struct cam_isp_hw_done_event_data *done,
  1661. uint32_t bubble_state,
  1662. struct cam_isp_hw_done_event_data *done_next_req)
  1663. {
  1664. int rc = 0;
  1665. int i, j, k;
  1666. bool not_found = false;
  1667. struct cam_isp_ctx_req *req_isp;
  1668. struct cam_context *ctx = ctx_isp->base;
  1669. const char *handle_type;
  1670. struct cam_isp_context_comp_record *comp_grp = NULL;
  1671. trace_cam_buf_done("ISP", ctx, req);
  1672. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  1673. CAM_DBG(CAM_ISP, "Enter with bubble_state %d, req_bubble_detected %d, ctx %u link: 0x%x",
  1674. bubble_state, req_isp->bubble_detected, ctx->ctx_id, ctx->link_hdl);
  1675. done_next_req->resource_handle = 0;
  1676. done_next_req->timestamp = done->timestamp;
  1677. for (i = 0; i < req_isp->num_fence_map_out; i++) {
  1678. if (done->resource_handle ==
  1679. req_isp->fence_map_out[i].resource_handle)
  1680. break;
  1681. }
  1682. if (done->hw_type == CAM_ISP_HW_TYPE_SFE)
  1683. comp_grp = &ctx_isp->sfe_bus_comp_grp[done->comp_group_id];
  1684. else
  1685. comp_grp = &ctx_isp->vfe_bus_comp_grp[done->comp_group_id];
  1686. if (!comp_grp) {
  1687. CAM_ERR(CAM_ISP, "comp_grp is NULL");
  1688. rc = -EINVAL;
  1689. return rc;
  1690. }
  1691. if (i == req_isp->num_fence_map_out) {
  1692. for (j = 0; j < comp_grp->num_res; j++) {
  1693. not_found = false;
  1694. if (comp_grp->res_id[j] == done->resource_handle)
  1695. continue;
  1696. for (k = 0; k < req_isp->num_fence_map_out; k++)
  1697. if (comp_grp->res_id[j] ==
  1698. req_isp->fence_map_out[k].resource_handle)
  1699. break;
  1700. if ((k == req_isp->num_fence_map_out) && (j != comp_grp->num_res - 1))
  1701. continue;
  1702. else if (k != req_isp->num_fence_map_out)
  1703. break;
  1704. else
  1705. not_found = true;
  1706. }
  1707. }
  1708. if (not_found) {
  1709. /*
  1710. * If not found in current request, it could be
  1711. * belonging to next request, this can happen if
  1712. * IRQ delay happens. It is only valid when the
  1713. * platform doesn't have last consumed address.
  1714. */
  1715. CAM_WARN(CAM_ISP,
  1716. "BUF_DONE for res %s not found in Req %lld ",
  1717. __cam_isp_resource_handle_id_to_type(
  1718. ctx_isp->isp_device_type,
  1719. done->resource_handle),
  1720. req->request_id);
  1721. done_next_req->hw_type = done->hw_type;
  1722. done_next_req->resource_handle = done->resource_handle;
  1723. done_next_req->comp_group_id = done->comp_group_id;
  1724. goto check_deferred;
  1725. }
  1726. for (i = 0; i < comp_grp->num_res; i++) {
  1727. for (j = 0; j < req_isp->num_fence_map_out; j++) {
  1728. if (comp_grp->res_id[i] ==
  1729. req_isp->fence_map_out[j].resource_handle)
  1730. break;
  1731. }
  1732. if (j == req_isp->num_fence_map_out) {
  1733. /*
  1734. * If not found in current request, it could be
  1735. * belonging to an active port with no valid fence
  1736. * bound to it, we needn't process it.
  1737. */
  1738. CAM_DBG(CAM_ISP,
  1739. "BUF_DONE for res %s not active in Req %lld ctx %u link: 0x%x",
  1740. __cam_isp_resource_handle_id_to_type(
  1741. ctx_isp->isp_device_type,
  1742. comp_grp->res_id[i]),
  1743. req->request_id, ctx->ctx_id, ctx->link_hdl);
  1744. continue;
  1745. }
  1746. if (req_isp->fence_map_out[j].sync_id == -1) {
  1747. handle_type =
  1748. __cam_isp_resource_handle_id_to_type(
  1749. ctx_isp->isp_device_type,
  1750. req_isp->fence_map_out[j].resource_handle);
  1751. CAM_WARN(CAM_ISP,
  1752. "Duplicate BUF_DONE for req %lld : i=%d, j=%d, res=%s, ctx %u link: 0x%x",
  1753. req->request_id, i, j, handle_type, ctx->ctx_id, ctx->link_hdl);
  1754. trace_cam_log_event("Duplicate BufDone",
  1755. handle_type, req->request_id, ctx->ctx_id);
  1756. continue;
  1757. }
  1758. /* Get buf handles from packet and retrieve them from presil framework */
  1759. if (cam_presil_mode_enabled()) {
  1760. rc = cam_presil_retrieve_buffers_from_packet(req_isp->hw_update_data.packet,
  1761. ctx->img_iommu_hdl, req_isp->fence_map_out[j].resource_handle);
  1762. if (rc) {
  1763. CAM_ERR(CAM_ISP,
  1764. "Failed to retrieve image buffers req_id:%d ctx_id:%u link: 0x%x bubble detected:%d rc:%d",
  1765. req->request_id, ctx->ctx_id, ctx->link_hdl,
  1766. req_isp->bubble_detected, rc);
  1767. return rc;
  1768. }
  1769. }
  1770. if (!req_isp->bubble_detected) {
  1771. CAM_DBG(CAM_ISP,
  1772. "Sync with success: req %lld res 0x%x fd 0x%x, ctx %u link: 0x%x",
  1773. req->request_id,
  1774. req_isp->fence_map_out[j].resource_handle,
  1775. req_isp->fence_map_out[j].sync_id,
  1776. ctx->ctx_id, ctx->link_hdl);
  1777. cam_smmu_buffer_tracker_buffer_putref(
  1778. req_isp->fence_map_out[j].buffer_tracker);
  1779. rc = cam_sync_signal(req_isp->fence_map_out[j].sync_id,
  1780. CAM_SYNC_STATE_SIGNALED_SUCCESS,
  1781. CAM_SYNC_COMMON_EVENT_SUCCESS);
  1782. if (rc)
  1783. CAM_DBG(CAM_ISP, "Sync failed with rc = %d, ctx %u link: 0x%x",
  1784. rc, ctx->ctx_id, ctx->link_hdl);
  1785. } else if (!req_isp->bubble_report) {
  1786. CAM_DBG(CAM_ISP,
  1787. "Sync with failure: req %lld res 0x%x fd 0x%x, ctx %u link: 0x%x",
  1788. req->request_id,
  1789. req_isp->fence_map_out[j].resource_handle,
  1790. req_isp->fence_map_out[j].sync_id,
  1791. ctx->ctx_id, ctx->link_hdl);
  1792. cam_smmu_buffer_tracker_buffer_putref(
  1793. req_isp->fence_map_out[j].buffer_tracker);
  1794. rc = cam_sync_signal(req_isp->fence_map_out[j].sync_id,
  1795. CAM_SYNC_STATE_SIGNALED_ERROR,
  1796. CAM_SYNC_ISP_EVENT_BUBBLE);
  1797. if (rc)
  1798. CAM_ERR(CAM_ISP, "Sync failed with rc = %d, ctx %u link: 0x%x",
  1799. rc, ctx->ctx_id, ctx->link_hdl);
  1800. } else {
  1801. /*
  1802. * Ignore the buffer done if bubble detect is on
  1803. * Increment the ack number here, and queue the
  1804. * request back to pending list whenever all the
  1805. * buffers are done.
  1806. */
  1807. req_isp->num_acked++;
  1808. CAM_DBG(CAM_ISP,
  1809. "buf done with bubble state %d recovery %d for req %lld, ctx %u link: 0x%x",
  1810. bubble_state,
  1811. req_isp->bubble_report,
  1812. req->request_id,
  1813. ctx->ctx_id, ctx->link_hdl);
  1814. continue;
  1815. }
  1816. CAM_DBG(CAM_ISP, "req %lld, reset sync id 0x%x ctx %u link: 0x%x",
  1817. req->request_id,
  1818. req_isp->fence_map_out[j].sync_id, ctx->ctx_id, ctx->link_hdl);
  1819. if (!rc) {
  1820. req_isp->num_acked++;
  1821. req_isp->fence_map_out[j].sync_id = -1;
  1822. }
  1823. if ((ctx_isp->use_frame_header_ts) &&
  1824. (req_isp->hw_update_data.frame_header_res_id ==
  1825. req_isp->fence_map_out[j].resource_handle))
  1826. __cam_isp_ctx_send_sof_timestamp_frame_header(
  1827. ctx_isp,
  1828. req_isp->hw_update_data.frame_header_cpu_addr,
  1829. req->request_id, CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  1830. }
  1831. check_deferred:
  1832. if (req_isp->num_acked > req_isp->num_fence_map_out) {
  1833. /* Should not happen */
  1834. CAM_ERR(CAM_ISP,
  1835. "WARNING: req_id %lld num_acked %d > map_out %d, ctx %u link: 0x%x",
  1836. req->request_id, req_isp->num_acked,
  1837. req_isp->num_fence_map_out, ctx->ctx_id, ctx->link_hdl);
  1838. WARN_ON(req_isp->num_acked > req_isp->num_fence_map_out);
  1839. }
  1840. if (req_isp->num_acked != req_isp->num_fence_map_out)
  1841. return rc;
  1842. rc = __cam_isp_ctx_handle_buf_done_for_req_list(ctx_isp, req);
  1843. return rc;
  1844. }
  1845. static int __cam_isp_handle_deferred_buf_done(
  1846. struct cam_isp_context *ctx_isp,
  1847. struct cam_ctx_request *req,
  1848. bool bubble_handling,
  1849. uint32_t status, uint32_t event_cause)
  1850. {
  1851. int i, j;
  1852. int rc = 0;
  1853. struct cam_isp_ctx_req *req_isp =
  1854. (struct cam_isp_ctx_req *) req->req_priv;
  1855. struct cam_context *ctx = ctx_isp->base;
  1856. CAM_DBG(CAM_ISP,
  1857. "ctx[%u] link[0x%x] : Req %llu : Handling %d deferred buf_dones num_acked=%d, bubble_handling=%d",
  1858. ctx->ctx_id, ctx->link_hdl, req->request_id, req_isp->num_deferred_acks,
  1859. req_isp->num_acked, bubble_handling);
  1860. for (i = 0; i < req_isp->num_deferred_acks; i++) {
  1861. j = req_isp->deferred_fence_map_index[i];
  1862. CAM_DBG(CAM_ISP,
  1863. "ctx[%u] link[0x%x] : Sync with status=%d, event_cause=%d: req %lld res 0x%x sync_id 0x%x",
  1864. ctx->ctx_id, ctx->link_hdl, status, event_cause,
  1865. req->request_id,
  1866. req_isp->fence_map_out[j].resource_handle,
  1867. req_isp->fence_map_out[j].sync_id);
  1868. if (req_isp->fence_map_out[j].sync_id == -1) {
  1869. CAM_WARN(CAM_ISP,
  1870. "ctx[%u] link[0x%x] : Deferred buf_done already signalled, req_id=%llu, j=%d, res=0x%x",
  1871. ctx->ctx_id, ctx->link_hdl, req->request_id, j,
  1872. req_isp->fence_map_out[j].resource_handle);
  1873. continue;
  1874. }
  1875. if (!bubble_handling) {
  1876. CAM_WARN(CAM_ISP,
  1877. "Unexpected Buf done for res=0x%x on ctx[%u] link[0x%x] for Req %llu, status=%d, possible bh delays",
  1878. req_isp->fence_map_out[j].resource_handle, ctx->ctx_id,
  1879. ctx->link_hdl, req->request_id, status);
  1880. rc = cam_sync_signal(req_isp->fence_map_out[j].sync_id,
  1881. status, event_cause);
  1882. if (rc) {
  1883. CAM_ERR(CAM_ISP,
  1884. "ctx[%u] link[0x%x] : Sync signal for Req %llu, sync_id %d status=%d failed with rc = %d",
  1885. ctx->ctx_id, ctx->link_hdl, req->request_id,
  1886. req_isp->fence_map_out[j].sync_id,
  1887. status, rc);
  1888. } else {
  1889. req_isp->num_acked++;
  1890. req_isp->fence_map_out[j].sync_id = -1;
  1891. }
  1892. } else {
  1893. req_isp->num_acked++;
  1894. }
  1895. }
  1896. CAM_DBG(CAM_ISP,
  1897. "ctx[%u] link[0x%x] : Req %llu : Handled %d deferred buf_dones num_acked=%d, num_fence_map_out=%d",
  1898. ctx->ctx_id, ctx->link_hdl, req->request_id, req_isp->num_deferred_acks,
  1899. req_isp->num_acked, req_isp->num_fence_map_out);
  1900. req_isp->num_deferred_acks = 0;
  1901. return rc;
  1902. }
  1903. static int __cam_isp_ctx_handle_deferred_buf_done_in_bubble(
  1904. struct cam_isp_context *ctx_isp,
  1905. struct cam_ctx_request *req)
  1906. {
  1907. int rc = 0;
  1908. struct cam_context *ctx = ctx_isp->base;
  1909. struct cam_isp_ctx_req *req_isp;
  1910. req_isp = (struct cam_isp_ctx_req *)req->req_priv;
  1911. if (req_isp->num_deferred_acks)
  1912. rc = __cam_isp_handle_deferred_buf_done(ctx_isp, req,
  1913. req_isp->bubble_report,
  1914. CAM_SYNC_STATE_SIGNALED_ERROR,
  1915. CAM_SYNC_ISP_EVENT_BUBBLE);
  1916. if (req_isp->num_acked > req_isp->num_fence_map_out) {
  1917. /* Should not happen */
  1918. CAM_ERR(CAM_ISP,
  1919. "WARNING: req_id %lld num_acked %d > map_out %d, ctx %u, link[0x%x]",
  1920. req->request_id, req_isp->num_acked,
  1921. req_isp->num_fence_map_out, ctx->ctx_id, ctx->link_hdl);
  1922. WARN_ON(req_isp->num_acked > req_isp->num_fence_map_out);
  1923. }
  1924. if (req_isp->num_acked == req_isp->num_fence_map_out)
  1925. rc = __cam_isp_ctx_handle_buf_done_for_req_list(ctx_isp, req);
  1926. return rc;
  1927. }
  1928. static int __cam_isp_ctx_handle_buf_done_for_request_verify_addr(
  1929. struct cam_isp_context *ctx_isp,
  1930. struct cam_ctx_request *req,
  1931. struct cam_isp_hw_done_event_data *done,
  1932. uint32_t bubble_state,
  1933. bool verify_consumed_addr,
  1934. bool defer_buf_done)
  1935. {
  1936. int rc = 0;
  1937. int i, j, k, def_idx;
  1938. bool not_found = false;
  1939. bool duplicate_defer_buf_done = false;
  1940. struct cam_isp_ctx_req *req_isp;
  1941. struct cam_context *ctx = ctx_isp->base;
  1942. const char *handle_type;
  1943. uint32_t cmp_addr = 0;
  1944. struct cam_isp_hw_done_event_data unhandled_done = {0};
  1945. struct cam_isp_context_comp_record *comp_grp = NULL;
  1946. struct cam_hw_cmd_args hw_cmd_args;
  1947. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  1948. trace_cam_buf_done("ISP", ctx, req);
  1949. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  1950. CAM_DBG(CAM_ISP, "Enter with bubble_state %d, req_bubble_detected %d, ctx %u, link[0x%x]",
  1951. bubble_state, req_isp->bubble_detected, ctx->ctx_id, ctx->link_hdl);
  1952. unhandled_done.timestamp = done->timestamp;
  1953. for (i = 0; i < req_isp->num_fence_map_out; i++) {
  1954. if (done->resource_handle ==
  1955. req_isp->fence_map_out[i].resource_handle) {
  1956. cmp_addr = cam_smmu_is_expanded_memory() ? CAM_36BIT_INTF_GET_IOVA_BASE(
  1957. req_isp->fence_map_out[i].image_buf_addr[0]) :
  1958. req_isp->fence_map_out[i].image_buf_addr[0];
  1959. if (!verify_consumed_addr ||
  1960. (verify_consumed_addr && (done->last_consumed_addr == cmp_addr))) {
  1961. break;
  1962. }
  1963. }
  1964. }
  1965. if (done->hw_type == CAM_ISP_HW_TYPE_SFE)
  1966. comp_grp = &ctx_isp->sfe_bus_comp_grp[done->comp_group_id];
  1967. else
  1968. comp_grp = &ctx_isp->vfe_bus_comp_grp[done->comp_group_id];
  1969. if (!comp_grp) {
  1970. CAM_ERR(CAM_ISP, "comp_grp is NULL");
  1971. rc = -EINVAL;
  1972. return rc;
  1973. }
  1974. if (i == req_isp->num_fence_map_out) {
  1975. not_found = true;
  1976. for (j = 0; j < comp_grp->num_res; j++) {
  1977. /* If the res is same with original res, we don't need to read again */
  1978. if (comp_grp->res_id[j] == done->resource_handle)
  1979. continue;
  1980. /* Check if the res in the requested list */
  1981. for (k = 0; k < req_isp->num_fence_map_out; k++)
  1982. if (comp_grp->res_id[j] ==
  1983. req_isp->fence_map_out[k].resource_handle)
  1984. break;
  1985. /* If res_id[j] isn't in requested list, then try next res in the group */
  1986. if (k == req_isp->num_fence_map_out) {
  1987. if (j != comp_grp->num_res - 1)
  1988. continue;
  1989. else
  1990. break;
  1991. }
  1992. if (!verify_consumed_addr) {
  1993. not_found = false;
  1994. break;
  1995. }
  1996. /*
  1997. * Find out the res from the requested list,
  1998. * then we can get last consumed address from this port.
  1999. */
  2000. done->resource_handle = comp_grp->res_id[j];
  2001. done->last_consumed_addr = 0;
  2002. hw_cmd_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  2003. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  2004. isp_hw_cmd_args.cmd_type =
  2005. CAM_ISP_HW_MGR_GET_LAST_CONSUMED_ADDR;
  2006. isp_hw_cmd_args.cmd_data = done;
  2007. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  2008. rc = ctx->hw_mgr_intf->hw_cmd(
  2009. ctx->hw_mgr_intf->hw_mgr_priv,
  2010. &hw_cmd_args);
  2011. if (rc) {
  2012. CAM_ERR(CAM_ISP, "HW command failed, ctx %u, link: 0x%x",
  2013. ctx->ctx_id, ctx->link_hdl);
  2014. return rc;
  2015. }
  2016. cmp_addr = cam_smmu_is_expanded_memory() ?
  2017. CAM_36BIT_INTF_GET_IOVA_BASE(
  2018. req_isp->fence_map_out[k].image_buf_addr[0]) :
  2019. req_isp->fence_map_out[k].image_buf_addr[0];
  2020. CAM_DBG(CAM_ISP, "Get res %s last_consumed_addr:0x%x cmp_addr:0x%x",
  2021. __cam_isp_resource_handle_id_to_type(
  2022. ctx_isp->isp_device_type, done->resource_handle),
  2023. done->last_consumed_addr, cmp_addr);
  2024. if (done->last_consumed_addr == cmp_addr) {
  2025. CAM_INFO(CAM_ISP, "found...");
  2026. not_found = false;
  2027. break;
  2028. }
  2029. }
  2030. }
  2031. if (not_found) {
  2032. /*
  2033. * If not found in current request, it could be
  2034. * belonging to next request, this can happen if
  2035. * IRQ delay happens. It is only valid when the
  2036. * platform doesn't have last consumed address.
  2037. */
  2038. CAM_WARN(CAM_ISP,
  2039. "BUF_DONE for res %s last_consumed_addr:0x%x not found in Req %lld ",
  2040. __cam_isp_resource_handle_id_to_type(
  2041. ctx_isp->isp_device_type, done->resource_handle),
  2042. done->last_consumed_addr,
  2043. req->request_id);
  2044. unhandled_done.hw_type = done->hw_type;
  2045. unhandled_done.resource_handle = done->resource_handle;
  2046. unhandled_done.comp_group_id = done->comp_group_id;
  2047. unhandled_done.last_consumed_addr = done->last_consumed_addr;
  2048. goto check_deferred;
  2049. }
  2050. for (i = 0; i < comp_grp->num_res; i++) {
  2051. for (j = 0; j < req_isp->num_fence_map_out; j++) {
  2052. if (comp_grp->res_id[i] ==
  2053. req_isp->fence_map_out[j].resource_handle)
  2054. break;
  2055. }
  2056. if (j == req_isp->num_fence_map_out) {
  2057. /*
  2058. * If not found in current request, it could be
  2059. * belonging to an active port with no valid fence
  2060. * bound to it, we needn't process it.
  2061. */
  2062. CAM_DBG(CAM_ISP,
  2063. "BUF_DONE for res %s not active in Req %lld ctx %u, link[0x%x]",
  2064. __cam_isp_resource_handle_id_to_type(
  2065. ctx_isp->isp_device_type, comp_grp->res_id[i]),
  2066. req->request_id, ctx->ctx_id, ctx->link_hdl);
  2067. continue;
  2068. }
  2069. if (req_isp->fence_map_out[j].sync_id == -1) {
  2070. handle_type = __cam_isp_resource_handle_id_to_type(
  2071. ctx_isp->isp_device_type,
  2072. req_isp->fence_map_out[j].resource_handle);
  2073. CAM_WARN(CAM_ISP,
  2074. "Duplicate BUF_DONE for req %lld : i=%d, j=%d, res=%s, ctx %u, link[0x%x]",
  2075. req->request_id, i, j, handle_type, ctx->ctx_id, ctx->link_hdl);
  2076. trace_cam_log_event("Duplicate BufDone",
  2077. handle_type, req->request_id, ctx->ctx_id);
  2078. continue;
  2079. }
  2080. /* Get buf handles from packet and retrieve them from presil framework */
  2081. if (cam_presil_mode_enabled()) {
  2082. rc = cam_presil_retrieve_buffers_from_packet(req_isp->hw_update_data.packet,
  2083. ctx->img_iommu_hdl, req_isp->fence_map_out[j].resource_handle);
  2084. if (rc) {
  2085. CAM_ERR(CAM_ISP,
  2086. "Failed to retrieve image buffers req_id:%d ctx_id:%u link[0x%x] bubble detected:%d rc:%d",
  2087. req->request_id, ctx->ctx_id, ctx->link_hdl,
  2088. req_isp->bubble_detected, rc);
  2089. return rc;
  2090. }
  2091. }
  2092. if (defer_buf_done) {
  2093. uint32_t deferred_indx = req_isp->num_deferred_acks;
  2094. duplicate_defer_buf_done = false;
  2095. for (k = 0; k < req_isp->num_deferred_acks; k++) {
  2096. def_idx = req_isp->deferred_fence_map_index[k];
  2097. if (def_idx == j) {
  2098. CAM_WARN(CAM_ISP,
  2099. "duplicate deferred ack for ctx[%u] link[0x%x] req %lld res 0x%x sync_id 0x%x",
  2100. ctx->ctx_id, ctx->link_hdl,
  2101. req->request_id,
  2102. req_isp->fence_map_out[j].resource_handle,
  2103. req_isp->fence_map_out[j].sync_id);
  2104. duplicate_defer_buf_done = true;
  2105. break;
  2106. }
  2107. }
  2108. if (duplicate_defer_buf_done)
  2109. continue;
  2110. if (req_isp->num_deferred_acks == req_isp->num_fence_map_out) {
  2111. CAM_WARN(CAM_ISP,
  2112. "WARNING: req_id %lld num_deferred_acks %d > map_out %d, ctx_idx:%u link[0x%x]",
  2113. req->request_id, req_isp->num_deferred_acks,
  2114. req_isp->num_fence_map_out, ctx->ctx_id, ctx->link_hdl);
  2115. continue;
  2116. }
  2117. /*
  2118. * If we are handling this BUF_DONE event for a request
  2119. * that is still in wait_list, do not signal now,
  2120. * instead mark it as done and handle it later -
  2121. * if this request is going into BUBBLE state later
  2122. * it will automatically be re-applied. If this is not
  2123. * going into BUBBLE, signal fences later.
  2124. * Note - we will come here only if the last consumed
  2125. * address matches with this ports buffer.
  2126. */
  2127. req_isp->deferred_fence_map_index[deferred_indx] = j;
  2128. req_isp->num_deferred_acks++;
  2129. CAM_DBG(CAM_ISP,
  2130. "ctx[%u] link[0x%x]:Deferred buf done for %llu with bubble state %d recovery %d",
  2131. ctx->ctx_id, ctx->link_hdl, req->request_id, bubble_state,
  2132. req_isp->bubble_report);
  2133. CAM_DBG(CAM_ISP,
  2134. "ctx[%u] link[0x%x]:Deferred info:num_acks=%d,fence_map_index=%d,resource_handle=0x%x,sync_id=%d",
  2135. ctx->ctx_id, ctx->link_hdl, req_isp->num_deferred_acks, j,
  2136. req_isp->fence_map_out[j].resource_handle,
  2137. req_isp->fence_map_out[j].sync_id);
  2138. continue;
  2139. } else if (!req_isp->bubble_detected) {
  2140. CAM_DBG(CAM_ISP,
  2141. "Sync with success: req %lld res 0x%x fd 0x%x, ctx:%u link[0x%x]",
  2142. req->request_id,
  2143. req_isp->fence_map_out[j].resource_handle,
  2144. req_isp->fence_map_out[j].sync_id,
  2145. ctx->ctx_id, ctx->link_hdl);
  2146. cam_smmu_buffer_tracker_buffer_putref(
  2147. req_isp->fence_map_out[j].buffer_tracker);
  2148. rc = cam_sync_signal(req_isp->fence_map_out[j].sync_id,
  2149. CAM_SYNC_STATE_SIGNALED_SUCCESS,
  2150. CAM_SYNC_COMMON_EVENT_SUCCESS);
  2151. if (rc) {
  2152. CAM_ERR(CAM_ISP,
  2153. "Sync=%u for req=%llu failed with rc=%d ctx:%u link[0x%x]",
  2154. req_isp->fence_map_out[j].sync_id, req->request_id,
  2155. rc, ctx->ctx_id, ctx->link_hdl);
  2156. } else if (req_isp->num_deferred_acks) {
  2157. /* Process deferred buf_done acks */
  2158. __cam_isp_handle_deferred_buf_done(ctx_isp,
  2159. req, false,
  2160. CAM_SYNC_STATE_SIGNALED_SUCCESS,
  2161. CAM_SYNC_COMMON_EVENT_SUCCESS);
  2162. }
  2163. /* Reset fence */
  2164. req_isp->fence_map_out[j].sync_id = -1;
  2165. } else if (!req_isp->bubble_report) {
  2166. CAM_DBG(CAM_ISP,
  2167. "Sync with failure: req %lld res 0x%x fd 0x%x, ctx:%u link[0x%x]",
  2168. req->request_id,
  2169. req_isp->fence_map_out[j].resource_handle,
  2170. req_isp->fence_map_out[j].sync_id,
  2171. ctx->ctx_id, ctx->link_hdl);
  2172. cam_smmu_buffer_tracker_buffer_putref(
  2173. req_isp->fence_map_out[j].buffer_tracker);
  2174. rc = cam_sync_signal(req_isp->fence_map_out[j].sync_id,
  2175. CAM_SYNC_STATE_SIGNALED_ERROR,
  2176. CAM_SYNC_ISP_EVENT_BUBBLE);
  2177. if (rc) {
  2178. CAM_ERR(CAM_ISP,
  2179. "Sync:%u for req:%llu failed with rc:%d,ctx:%u,link[0x%x]",
  2180. req_isp->fence_map_out[j].sync_id, req->request_id,
  2181. rc, ctx->ctx_id, ctx->link_hdl);
  2182. } else if (req_isp->num_deferred_acks) {
  2183. /* Process deferred buf_done acks */
  2184. __cam_isp_handle_deferred_buf_done(ctx_isp, req,
  2185. false,
  2186. CAM_SYNC_STATE_SIGNALED_ERROR,
  2187. CAM_SYNC_ISP_EVENT_BUBBLE);
  2188. }
  2189. /* Reset fence */
  2190. req_isp->fence_map_out[j].sync_id = -1;
  2191. } else {
  2192. /*
  2193. * Ignore the buffer done if bubble detect is on
  2194. * Increment the ack number here, and queue the
  2195. * request back to pending list whenever all the
  2196. * buffers are done.
  2197. */
  2198. req_isp->num_acked++;
  2199. CAM_DBG(CAM_ISP,
  2200. "buf done with bubble state %d recovery %d for req %lld, ctx_idx:%u link[0x%x]",
  2201. bubble_state,
  2202. req_isp->bubble_report,
  2203. req->request_id,
  2204. ctx->ctx_id, ctx->link_hdl);
  2205. /* Process deferred buf_done acks */
  2206. if (req_isp->num_deferred_acks)
  2207. __cam_isp_handle_deferred_buf_done(ctx_isp, req,
  2208. true,
  2209. CAM_SYNC_STATE_SIGNALED_ERROR,
  2210. CAM_SYNC_ISP_EVENT_BUBBLE);
  2211. if (req_isp->num_acked == req_isp->num_fence_map_out) {
  2212. rc = __cam_isp_ctx_handle_buf_done_for_req_list(ctx_isp, req);
  2213. if (rc)
  2214. CAM_ERR(CAM_ISP,
  2215. "Error in buf done for req = %llu with rc = %d, ctx_idx:%u link[0x%x]",
  2216. req->request_id, rc, ctx->ctx_id, ctx->link_hdl);
  2217. return rc;
  2218. }
  2219. continue;
  2220. }
  2221. CAM_DBG(CAM_ISP, "req %lld, reset sync id 0x%x ctx_idx:%u link[0x%x]",
  2222. req->request_id,
  2223. req_isp->fence_map_out[j].sync_id, ctx->ctx_id, ctx->link_hdl);
  2224. if (!rc) {
  2225. req_isp->num_acked++;
  2226. }
  2227. if ((ctx_isp->use_frame_header_ts) &&
  2228. (req_isp->hw_update_data.frame_header_res_id ==
  2229. req_isp->fence_map_out[j].resource_handle))
  2230. __cam_isp_ctx_send_sof_timestamp_frame_header(
  2231. ctx_isp,
  2232. req_isp->hw_update_data.frame_header_cpu_addr,
  2233. req->request_id, CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  2234. }
  2235. check_deferred:
  2236. if ((unhandled_done.resource_handle > 0) && (!defer_buf_done))
  2237. __cam_isp_ctx_check_deferred_buf_done(
  2238. ctx_isp, &unhandled_done, bubble_state);
  2239. if (req_isp->num_acked > req_isp->num_fence_map_out) {
  2240. /* Should not happen */
  2241. CAM_ERR(CAM_ISP,
  2242. "WARNING: req_id %lld num_acked %d > map_out %d, ctx_idx:%u link[0x%x]",
  2243. req->request_id, req_isp->num_acked,
  2244. req_isp->num_fence_map_out, ctx->ctx_id, ctx->link_hdl);
  2245. }
  2246. if (req_isp->num_acked != req_isp->num_fence_map_out)
  2247. return rc;
  2248. rc = __cam_isp_ctx_handle_buf_done_for_req_list(ctx_isp, req);
  2249. return rc;
  2250. }
  2251. static int __cam_isp_ctx_handle_buf_done(
  2252. struct cam_isp_context *ctx_isp,
  2253. struct cam_isp_hw_done_event_data *done,
  2254. uint32_t bubble_state)
  2255. {
  2256. int rc = 0;
  2257. struct cam_ctx_request *req;
  2258. struct cam_context *ctx = ctx_isp->base;
  2259. struct cam_isp_hw_done_event_data done_next_req = {0};
  2260. if (list_empty(&ctx->active_req_list)) {
  2261. CAM_WARN(CAM_ISP, "Buf done with no active request, ctx_idx:%u link[0x%x]",
  2262. ctx->ctx_id, ctx->link_hdl);
  2263. return 0;
  2264. }
  2265. req = list_first_entry(&ctx->active_req_list,
  2266. struct cam_ctx_request, list);
  2267. rc = __cam_isp_ctx_handle_buf_done_for_request(ctx_isp, req, done,
  2268. bubble_state, &done_next_req);
  2269. if (done_next_req.resource_handle) {
  2270. struct cam_isp_hw_done_event_data unhandled_res = {0};
  2271. struct cam_ctx_request *next_req = list_last_entry(
  2272. &ctx->active_req_list, struct cam_ctx_request, list);
  2273. if (next_req->request_id != req->request_id) {
  2274. /*
  2275. * Few resource handles are already signalled in the
  2276. * current request, lets check if there is another
  2277. * request waiting for these resources. This can
  2278. * happen if handling some of next request's buf done
  2279. * events are happening first before handling current
  2280. * request's remaining buf dones due to IRQ scheduling.
  2281. * Lets check only one more request as we will have
  2282. * maximum of 2 requests in active_list at any time.
  2283. */
  2284. CAM_WARN(CAM_ISP,
  2285. "Unhandled bufdone resources for req %lld,trying next request %lld,ctx:%u link[0x%x]",
  2286. req->request_id, next_req->request_id, ctx->ctx_id, ctx->link_hdl);
  2287. __cam_isp_ctx_handle_buf_done_for_request(ctx_isp,
  2288. next_req, &done_next_req,
  2289. bubble_state, &unhandled_res);
  2290. if (unhandled_res.resource_handle == 0)
  2291. CAM_INFO(CAM_ISP,
  2292. "BUF Done event handed for next request %lld, ctx_idx:%u link[0x%x]",
  2293. next_req->request_id, ctx->ctx_id, ctx->link_hdl);
  2294. else
  2295. CAM_ERR(CAM_ISP,
  2296. "BUF Done not handled for next request %lld, ctx_idx:%u link[0x%x]",
  2297. next_req->request_id, ctx->ctx_id, ctx->link_hdl);
  2298. } else {
  2299. CAM_WARN(CAM_ISP,
  2300. "Req %lld only active request, spurious buf_done rxd, ctx_idx:%u link[0x%x]",
  2301. req->request_id, ctx->ctx_id, ctx->link_hdl);
  2302. }
  2303. }
  2304. return rc;
  2305. }
  2306. static void __cam_isp_ctx_buf_done_match_req(
  2307. struct cam_ctx_request *req,
  2308. struct cam_isp_hw_done_event_data *done,
  2309. bool *irq_delay_detected)
  2310. {
  2311. int i;
  2312. uint32_t match_count = 0;
  2313. struct cam_isp_ctx_req *req_isp;
  2314. uint32_t cmp_addr = 0;
  2315. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  2316. for (i = 0; i < req_isp->num_fence_map_out; i++) {
  2317. cmp_addr = cam_smmu_is_expanded_memory() ? CAM_36BIT_INTF_GET_IOVA_BASE(
  2318. req_isp->fence_map_out[i].image_buf_addr[0]) :
  2319. req_isp->fence_map_out[i].image_buf_addr[0];
  2320. if ((done->resource_handle ==
  2321. req_isp->fence_map_out[i].resource_handle) &&
  2322. (done->last_consumed_addr == cmp_addr)) {
  2323. match_count++;
  2324. break;
  2325. }
  2326. }
  2327. if (match_count > 0)
  2328. *irq_delay_detected = true;
  2329. else
  2330. *irq_delay_detected = false;
  2331. CAM_DBG(CAM_ISP,
  2332. "buf done num handles %d match count %d for next req:%lld",
  2333. done->resource_handle, match_count, req->request_id);
  2334. CAM_DBG(CAM_ISP,
  2335. "irq_delay_detected %d", *irq_delay_detected);
  2336. }
  2337. static void __cam_isp_ctx_try_buf_done_process_for_active_request(
  2338. uint32_t deferred_ack_start_idx, struct cam_isp_context *ctx_isp,
  2339. struct cam_ctx_request *deferred_req)
  2340. {
  2341. int i, j, deferred_map_idx, rc;
  2342. struct cam_context *ctx = ctx_isp->base;
  2343. struct cam_ctx_request *curr_active_req;
  2344. struct cam_isp_ctx_req *curr_active_isp_req;
  2345. struct cam_isp_ctx_req *deferred_isp_req;
  2346. if (list_empty(&ctx->active_req_list))
  2347. return;
  2348. curr_active_req = list_first_entry(&ctx->active_req_list,
  2349. struct cam_ctx_request, list);
  2350. curr_active_isp_req = (struct cam_isp_ctx_req *)curr_active_req->req_priv;
  2351. deferred_isp_req = (struct cam_isp_ctx_req *)deferred_req->req_priv;
  2352. /* Check from newly updated deferred acks */
  2353. for (i = deferred_ack_start_idx; i < deferred_isp_req->num_deferred_acks; i++) {
  2354. deferred_map_idx = deferred_isp_req->deferred_fence_map_index[i];
  2355. for (j = 0; j < curr_active_isp_req->num_fence_map_out; j++) {
  2356. /* resource needs to match */
  2357. if (curr_active_isp_req->fence_map_out[j].resource_handle !=
  2358. deferred_isp_req->fence_map_out[deferred_map_idx].resource_handle)
  2359. continue;
  2360. /* Check if fence is valid */
  2361. if (curr_active_isp_req->fence_map_out[j].sync_id == -1)
  2362. break;
  2363. CAM_WARN(CAM_ISP,
  2364. "Processing delayed buf done req: %llu bubble_detected: %s res: 0x%x fd: 0x%x, ctx: %u link: 0x%x [deferred req: %llu last applied: %llu]",
  2365. curr_active_req->request_id,
  2366. CAM_BOOL_TO_YESNO(curr_active_isp_req->bubble_detected),
  2367. curr_active_isp_req->fence_map_out[j].resource_handle,
  2368. curr_active_isp_req->fence_map_out[j].sync_id,
  2369. ctx->ctx_id, ctx->link_hdl,
  2370. deferred_req->request_id, ctx_isp->last_applied_req_id);
  2371. /* Signal only if bubble is not detected for this request */
  2372. if (!curr_active_isp_req->bubble_detected) {
  2373. rc = cam_sync_signal(curr_active_isp_req->fence_map_out[j].sync_id,
  2374. CAM_SYNC_STATE_SIGNALED_SUCCESS,
  2375. CAM_SYNC_COMMON_EVENT_SUCCESS);
  2376. if (rc)
  2377. CAM_ERR(CAM_ISP,
  2378. "Sync: %d for req: %llu failed with rc: %d, ctx: %u link: 0x%x",
  2379. curr_active_isp_req->fence_map_out[j].sync_id,
  2380. curr_active_req->request_id, rc,
  2381. ctx->ctx_id, ctx->link_hdl);
  2382. curr_active_isp_req->fence_map_out[j].sync_id = -1;
  2383. }
  2384. curr_active_isp_req->num_acked++;
  2385. break;
  2386. }
  2387. }
  2388. }
  2389. static int __cam_isp_ctx_check_deferred_buf_done(
  2390. struct cam_isp_context *ctx_isp,
  2391. struct cam_isp_hw_done_event_data *done,
  2392. uint32_t bubble_state)
  2393. {
  2394. int rc = 0;
  2395. uint32_t curr_num_deferred = 0;
  2396. struct cam_ctx_request *req;
  2397. struct cam_context *ctx = ctx_isp->base;
  2398. struct cam_isp_ctx_req *req_isp;
  2399. bool req_in_pending_wait_list = false;
  2400. if (!list_empty(&ctx->wait_req_list)) {
  2401. req = list_first_entry(&ctx->wait_req_list,
  2402. struct cam_ctx_request, list);
  2403. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  2404. curr_num_deferred = req_isp->num_deferred_acks;
  2405. req_in_pending_wait_list = true;
  2406. if (ctx_isp->last_applied_req_id !=
  2407. ctx_isp->last_bufdone_err_apply_req_id) {
  2408. CAM_DBG(CAM_ISP,
  2409. "Trying to find buf done with req in wait list, req %llu last apply id:%lld last err id:%lld curr_num_deferred: %u, ctx: %u link: 0x%x",
  2410. req->request_id, ctx_isp->last_applied_req_id,
  2411. ctx_isp->last_bufdone_err_apply_req_id, curr_num_deferred,
  2412. ctx->ctx_id, ctx->link_hdl);
  2413. ctx_isp->last_bufdone_err_apply_req_id =
  2414. ctx_isp->last_applied_req_id;
  2415. }
  2416. /*
  2417. * Verify consumed address for this request to make sure
  2418. * we are handling the buf_done for the correct
  2419. * buffer. Also defer actual buf_done handling, i.e
  2420. * do not signal the fence as this request may go into
  2421. * Bubble state eventully.
  2422. */
  2423. rc = __cam_isp_ctx_handle_buf_done_for_request_verify_addr(
  2424. ctx_isp, req, done, bubble_state, true, true);
  2425. /* Check for active req if any deferred is processed */
  2426. if (req_isp->num_deferred_acks > curr_num_deferred)
  2427. __cam_isp_ctx_try_buf_done_process_for_active_request(
  2428. curr_num_deferred, ctx_isp, req);
  2429. } else if (!list_empty(&ctx->pending_req_list)) {
  2430. /*
  2431. * We saw the case that the hw config is blocked due to
  2432. * some reason, the we get the reg upd and buf done before
  2433. * the req is added to wait req list.
  2434. */
  2435. req = list_first_entry(&ctx->pending_req_list,
  2436. struct cam_ctx_request, list);
  2437. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  2438. curr_num_deferred = req_isp->num_deferred_acks;
  2439. req_in_pending_wait_list = true;
  2440. if (ctx_isp->last_applied_req_id !=
  2441. ctx_isp->last_bufdone_err_apply_req_id) {
  2442. CAM_DBG(CAM_ISP,
  2443. "Trying to find buf done with req in pending list, req %llu last apply id:%lld last err id:%lld curr_num_deferred: %u, ctx: %u link: 0x%x",
  2444. req->request_id, ctx_isp->last_applied_req_id,
  2445. ctx_isp->last_bufdone_err_apply_req_id, curr_num_deferred,
  2446. ctx->ctx_id, ctx->link_hdl);
  2447. ctx_isp->last_bufdone_err_apply_req_id =
  2448. ctx_isp->last_applied_req_id;
  2449. }
  2450. /*
  2451. * Verify consumed address for this request to make sure
  2452. * we are handling the buf_done for the correct
  2453. * buffer. Also defer actual buf_done handling, i.e
  2454. * do not signal the fence as this request may go into
  2455. * Bubble state eventully.
  2456. */
  2457. rc = __cam_isp_ctx_handle_buf_done_for_request_verify_addr(
  2458. ctx_isp, req, done, bubble_state, true, true);
  2459. /* Check for active req if any deferred is processed */
  2460. if (req_isp->num_deferred_acks > curr_num_deferred)
  2461. __cam_isp_ctx_try_buf_done_process_for_active_request(
  2462. curr_num_deferred, ctx_isp, req);
  2463. }
  2464. if (!req_in_pending_wait_list && (ctx_isp->last_applied_req_id !=
  2465. ctx_isp->last_bufdone_err_apply_req_id)) {
  2466. CAM_DBG(CAM_ISP,
  2467. "Bufdone without active request bubble_state=%d last_applied_req_id:%lld,ctx:%u link:0x%x",
  2468. bubble_state, ctx_isp->last_applied_req_id, ctx->ctx_id, ctx->link_hdl);
  2469. ctx_isp->last_bufdone_err_apply_req_id =
  2470. ctx_isp->last_applied_req_id;
  2471. }
  2472. return rc;
  2473. }
  2474. static int __cam_isp_ctx_handle_buf_done_verify_addr(
  2475. struct cam_isp_context *ctx_isp,
  2476. struct cam_isp_hw_done_event_data *done,
  2477. uint32_t bubble_state)
  2478. {
  2479. int rc = 0;
  2480. bool irq_delay_detected = false;
  2481. struct cam_ctx_request *req;
  2482. struct cam_ctx_request *next_req = NULL;
  2483. struct cam_context *ctx = ctx_isp->base;
  2484. if (list_empty(&ctx->active_req_list)) {
  2485. return __cam_isp_ctx_check_deferred_buf_done(
  2486. ctx_isp, done, bubble_state);
  2487. }
  2488. req = list_first_entry(&ctx->active_req_list,
  2489. struct cam_ctx_request, list);
  2490. if (ctx_isp->active_req_cnt > 1) {
  2491. next_req = list_last_entry(
  2492. &ctx->active_req_list,
  2493. struct cam_ctx_request, list);
  2494. if (next_req->request_id != req->request_id)
  2495. __cam_isp_ctx_buf_done_match_req(next_req, done,
  2496. &irq_delay_detected);
  2497. else
  2498. CAM_WARN(CAM_ISP,
  2499. "Req %lld only active request, spurious buf_done rxd, ctx: %u link: 0x%x",
  2500. req->request_id, ctx->ctx_id, ctx->link_hdl);
  2501. }
  2502. /*
  2503. * If irq delay isn't detected, then we need to verify
  2504. * the consumed address for current req, otherwise, we
  2505. * can't verify the consumed address.
  2506. */
  2507. rc = __cam_isp_ctx_handle_buf_done_for_request_verify_addr(
  2508. ctx_isp, req, done, bubble_state,
  2509. !irq_delay_detected, false);
  2510. /*
  2511. * Verify the consumed address for next req all the time,
  2512. * since the reported buf done event may belong to current
  2513. * req, then we can't signal this event for next req.
  2514. */
  2515. if (!rc && irq_delay_detected)
  2516. rc = __cam_isp_ctx_handle_buf_done_for_request_verify_addr(
  2517. ctx_isp, next_req, done,
  2518. bubble_state, true, false);
  2519. return rc;
  2520. }
  2521. static int __cam_isp_ctx_handle_buf_done_in_activated_state(
  2522. struct cam_isp_context *ctx_isp,
  2523. struct cam_isp_hw_done_event_data *done,
  2524. uint32_t bubble_state)
  2525. {
  2526. int rc = 0;
  2527. if (ctx_isp->support_consumed_addr)
  2528. rc = __cam_isp_ctx_handle_buf_done_verify_addr(
  2529. ctx_isp, done, bubble_state);
  2530. else
  2531. rc = __cam_isp_ctx_handle_buf_done(
  2532. ctx_isp, done, bubble_state);
  2533. return rc;
  2534. }
  2535. static int __cam_isp_ctx_apply_pending_req(
  2536. void *priv, void *data)
  2537. {
  2538. int rc = 0;
  2539. int64_t prev_applied_req;
  2540. struct cam_context *ctx = NULL;
  2541. struct cam_isp_context *ctx_isp = priv;
  2542. struct cam_ctx_request *req;
  2543. struct cam_isp_ctx_req *req_isp;
  2544. struct cam_hw_config_args cfg = {0};
  2545. if (!ctx_isp) {
  2546. CAM_ERR(CAM_ISP, "Invalid ctx_isp:%pK", ctx);
  2547. rc = -EINVAL;
  2548. goto end;
  2549. }
  2550. ctx = ctx_isp->base;
  2551. if (list_empty(&ctx->pending_req_list)) {
  2552. CAM_DBG(CAM_ISP,
  2553. "No pending requests to apply, ctx_idx: %u, link: 0x%x",
  2554. ctx->ctx_id, ctx->link_hdl);
  2555. rc = -EFAULT;
  2556. goto end;
  2557. }
  2558. if (ctx_isp->vfps_aux_context) {
  2559. if (ctx_isp->substate_activated == CAM_ISP_CTX_ACTIVATED_APPLIED)
  2560. goto end;
  2561. if (ctx_isp->active_req_cnt >= 1)
  2562. goto end;
  2563. } else {
  2564. if ((ctx->state != CAM_CTX_ACTIVATED) ||
  2565. (!atomic_read(&ctx_isp->rxd_epoch)) ||
  2566. (ctx_isp->substate_activated == CAM_ISP_CTX_ACTIVATED_APPLIED))
  2567. goto end;
  2568. if (ctx_isp->active_req_cnt >= 2)
  2569. goto end;
  2570. }
  2571. spin_lock_bh(&ctx->lock);
  2572. req = list_first_entry(&ctx->pending_req_list, struct cam_ctx_request,
  2573. list);
  2574. spin_unlock_bh(&ctx->lock);
  2575. CAM_DBG(CAM_REQ, "Apply request %lld in substate %d ctx_idx: %u, link: 0x%x",
  2576. req->request_id, ctx_isp->substate_activated, ctx->ctx_id, ctx->link_hdl);
  2577. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  2578. cfg.ctxt_to_hw_map = ctx_isp->hw_ctx;
  2579. cfg.request_id = req->request_id;
  2580. cfg.hw_update_entries = req_isp->cfg;
  2581. cfg.num_hw_update_entries = req_isp->num_cfg;
  2582. cfg.priv = &req_isp->hw_update_data;
  2583. /*
  2584. * Offline mode may receive the SOF and REG_UPD earlier than
  2585. * CDM processing return back, so we set the substate before
  2586. * apply setting.
  2587. */
  2588. spin_lock_bh(&ctx->lock);
  2589. atomic_set(&ctx_isp->rxd_epoch, 0);
  2590. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_APPLIED;
  2591. prev_applied_req = ctx_isp->last_applied_req_id;
  2592. ctx_isp->last_applied_req_id = req->request_id;
  2593. atomic_set(&ctx_isp->apply_in_progress, 1);
  2594. list_del_init(&req->list);
  2595. list_add_tail(&req->list, &ctx->wait_req_list);
  2596. spin_unlock_bh(&ctx->lock);
  2597. rc = ctx->hw_mgr_intf->hw_config(ctx->hw_mgr_intf->hw_mgr_priv, &cfg);
  2598. if (rc) {
  2599. CAM_ERR_RATE_LIMIT(CAM_ISP,
  2600. "Can not apply the configuration,ctx: %u,link: 0x%x",
  2601. ctx->ctx_id, ctx->link_hdl);
  2602. spin_lock_bh(&ctx->lock);
  2603. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_SOF;
  2604. ctx_isp->last_applied_req_id = prev_applied_req;
  2605. atomic_set(&ctx_isp->apply_in_progress, 0);
  2606. list_del_init(&req->list);
  2607. list_add(&req->list, &ctx->pending_req_list);
  2608. spin_unlock_bh(&ctx->lock);
  2609. } else {
  2610. atomic_set(&ctx_isp->apply_in_progress, 0);
  2611. CAM_DBG(CAM_ISP, "New substate state %d, applied req %lld, ctx: %u, link: 0x%x",
  2612. CAM_ISP_CTX_ACTIVATED_APPLIED,
  2613. ctx_isp->last_applied_req_id, ctx->ctx_id, ctx->link_hdl);
  2614. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  2615. CAM_ISP_STATE_CHANGE_TRIGGER_APPLIED,
  2616. req->request_id);
  2617. }
  2618. end:
  2619. return rc;
  2620. }
  2621. static int __cam_isp_ctx_schedule_apply_req(
  2622. struct cam_isp_context *ctx_isp)
  2623. {
  2624. int rc = 0;
  2625. struct crm_workq_task *task;
  2626. task = cam_req_mgr_workq_get_task(ctx_isp->workq);
  2627. if (!task) {
  2628. CAM_ERR(CAM_ISP, "No task for worker");
  2629. return -ENOMEM;
  2630. }
  2631. task->process_cb = __cam_isp_ctx_apply_pending_req;
  2632. rc = cam_req_mgr_workq_enqueue_task(task, ctx_isp, CRM_TASK_PRIORITY_0);
  2633. if (rc)
  2634. CAM_ERR(CAM_ISP, "Failed to schedule task rc:%d", rc);
  2635. return rc;
  2636. }
  2637. static int __cam_isp_ctx_offline_epoch_in_activated_state(
  2638. struct cam_isp_context *ctx_isp, void *evt_data)
  2639. {
  2640. struct cam_context *ctx = ctx_isp->base;
  2641. struct cam_ctx_request *req, *req_temp;
  2642. uint64_t request_id = 0;
  2643. atomic_set(&ctx_isp->rxd_epoch, 1);
  2644. CAM_DBG(CAM_ISP, "SOF frame %lld ctx %u link: 0x%x", ctx_isp->frame_id,
  2645. ctx->ctx_id, ctx->link_hdl);
  2646. /*
  2647. * For offline it is not possible for epoch to be generated without
  2648. * RUP done. IRQ scheduling delays can possibly cause this.
  2649. */
  2650. if (list_empty(&ctx->active_req_list)) {
  2651. CAM_WARN(CAM_ISP,
  2652. "Active list empty on ctx:%u link:0x%x - EPOCH serviced before RUP",
  2653. ctx->ctx_id, ctx->link_hdl);
  2654. } else {
  2655. list_for_each_entry_safe(req, req_temp, &ctx->active_req_list, list) {
  2656. if (req->request_id > ctx_isp->reported_req_id) {
  2657. request_id = req->request_id;
  2658. ctx_isp->reported_req_id = request_id;
  2659. break;
  2660. }
  2661. }
  2662. }
  2663. __cam_isp_ctx_schedule_apply_req(ctx_isp);
  2664. /*
  2665. * If no valid request, wait for RUP shutter posted after buf done
  2666. */
  2667. if (request_id)
  2668. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  2669. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  2670. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  2671. CAM_ISP_STATE_CHANGE_TRIGGER_EPOCH,
  2672. request_id);
  2673. return 0;
  2674. }
  2675. static int __cam_isp_ctx_reg_upd_in_epoch_bubble_state(
  2676. struct cam_isp_context *ctx_isp, void *evt_data)
  2677. {
  2678. if (ctx_isp->frame_id == 1)
  2679. CAM_DBG(CAM_ISP, "Reg update in Substate[%s] for early PCR",
  2680. __cam_isp_ctx_substate_val_to_type(
  2681. ctx_isp->substate_activated));
  2682. else
  2683. CAM_WARN_RATE_LIMIT(CAM_ISP,
  2684. "ctx:%u Unexpected regupdate in activated Substate[%s] for frame_id:%lld",
  2685. ctx_isp->base->ctx_id,
  2686. __cam_isp_ctx_substate_val_to_type(
  2687. ctx_isp->substate_activated),
  2688. ctx_isp->frame_id);
  2689. return 0;
  2690. }
  2691. static int __cam_isp_ctx_reg_upd_in_applied_state(
  2692. struct cam_isp_context *ctx_isp, void *evt_data)
  2693. {
  2694. int rc = 0;
  2695. struct cam_ctx_request *req;
  2696. struct cam_context *ctx = ctx_isp->base;
  2697. struct cam_isp_ctx_req *req_isp;
  2698. uint64_t request_id = 0;
  2699. if (list_empty(&ctx->wait_req_list)) {
  2700. CAM_ERR(CAM_ISP, "Reg upd ack with no waiting request, ctx_idx: %u, link: 0x%x",
  2701. ctx->ctx_id, ctx->link_hdl);
  2702. goto end;
  2703. }
  2704. req = list_first_entry(&ctx->wait_req_list,
  2705. struct cam_ctx_request, list);
  2706. list_del_init(&req->list);
  2707. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  2708. if (req_isp->num_fence_map_out != 0) {
  2709. list_add_tail(&req->list, &ctx->active_req_list);
  2710. ctx_isp->active_req_cnt++;
  2711. request_id = req->request_id;
  2712. CAM_DBG(CAM_REQ,
  2713. "move request %lld to active list(cnt = %d), ctx %u, link: 0x%x",
  2714. req->request_id, ctx_isp->active_req_cnt, ctx->ctx_id, ctx->link_hdl);
  2715. __cam_isp_ctx_update_event_record(ctx_isp,
  2716. CAM_ISP_CTX_EVENT_RUP, req, NULL);
  2717. } else {
  2718. cam_smmu_buffer_tracker_putref(&req->buf_tracker);
  2719. /* no io config, so the request is completed. */
  2720. list_add_tail(&req->list, &ctx->free_req_list);
  2721. CAM_DBG(CAM_ISP,
  2722. "move active request %lld to free list(cnt = %d), ctx %u, link: 0x%x",
  2723. req->request_id, ctx_isp->active_req_cnt, ctx->ctx_id, ctx->link_hdl);
  2724. }
  2725. /*
  2726. * This function only called directly from applied and bubble applied
  2727. * state so change substate here.
  2728. */
  2729. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_EPOCH;
  2730. CAM_DBG(CAM_ISP, "next Substate[%s], ctx %u, link: 0x%x",
  2731. __cam_isp_ctx_substate_val_to_type(
  2732. ctx_isp->substate_activated), ctx->ctx_id, ctx->link_hdl);
  2733. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  2734. CAM_ISP_STATE_CHANGE_TRIGGER_REG_UPDATE, request_id);
  2735. end:
  2736. return rc;
  2737. }
  2738. static int __cam_isp_ctx_notify_sof_in_activated_state(
  2739. struct cam_isp_context *ctx_isp, void *evt_data)
  2740. {
  2741. int rc = 0;
  2742. uint64_t request_id = 0;
  2743. struct cam_context *ctx = ctx_isp->base;
  2744. struct cam_ctx_request *req;
  2745. struct cam_isp_ctx_req *req_isp;
  2746. struct cam_hw_cmd_args hw_cmd_args;
  2747. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  2748. uint64_t last_cdm_done_req = 0;
  2749. struct cam_isp_hw_epoch_event_data *epoch_done_event_data =
  2750. (struct cam_isp_hw_epoch_event_data *)evt_data;
  2751. if (!evt_data) {
  2752. CAM_ERR(CAM_ISP, "invalid event data");
  2753. return -EINVAL;
  2754. }
  2755. ctx_isp->frame_id_meta = epoch_done_event_data->frame_id_meta;
  2756. if (atomic_read(&ctx_isp->process_bubble)) {
  2757. if (list_empty(&ctx->active_req_list)) {
  2758. CAM_ERR(CAM_ISP,
  2759. "No available active req in bubble, ctx %u, link: 0x%x",
  2760. ctx->ctx_id, ctx->link_hdl);
  2761. atomic_set(&ctx_isp->process_bubble, 0);
  2762. ctx_isp->bubble_frame_cnt = 0;
  2763. rc = -EINVAL;
  2764. return rc;
  2765. }
  2766. if (ctx_isp->last_sof_timestamp ==
  2767. ctx_isp->sof_timestamp_val) {
  2768. CAM_DBG(CAM_ISP,
  2769. "Tasklet delay detected! Bubble frame check skipped, sof_timestamp: %lld, ctx %u, link: 0x%x",
  2770. ctx_isp->sof_timestamp_val, ctx->ctx_id, ctx->link_hdl);
  2771. goto notify_only;
  2772. }
  2773. req = list_first_entry(&ctx->active_req_list,
  2774. struct cam_ctx_request, list);
  2775. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  2776. if (ctx_isp->bubble_frame_cnt >= 1 &&
  2777. req_isp->bubble_detected) {
  2778. hw_cmd_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  2779. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  2780. isp_hw_cmd_args.cmd_type =
  2781. CAM_ISP_HW_MGR_GET_LAST_CDM_DONE;
  2782. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  2783. rc = ctx->hw_mgr_intf->hw_cmd(
  2784. ctx->hw_mgr_intf->hw_mgr_priv,
  2785. &hw_cmd_args);
  2786. if (rc) {
  2787. CAM_ERR(CAM_ISP, "HW command failed, ctx %u, link: 0x%x",
  2788. ctx->ctx_id, ctx->link_hdl);
  2789. return rc;
  2790. }
  2791. last_cdm_done_req = isp_hw_cmd_args.u.last_cdm_done;
  2792. CAM_DBG(CAM_ISP, "last_cdm_done req: %d, ctx %u, link: 0x%x",
  2793. last_cdm_done_req, ctx->ctx_id, ctx->link_hdl);
  2794. if (last_cdm_done_req >= req->request_id) {
  2795. CAM_DBG(CAM_ISP,
  2796. "invalid sof event data CDM cb detected for req: %lld, possible buf_done delay, waiting for buf_done, ctx %u, link: 0x%x",
  2797. req->request_id, ctx->ctx_id, ctx->link_hdl);
  2798. ctx_isp->bubble_frame_cnt = 0;
  2799. } else {
  2800. CAM_DBG(CAM_ISP,
  2801. "CDM callback not happened for req: %lld, possible CDM stuck or workqueue delay, ctx %u, link: 0x%x",
  2802. req->request_id, ctx->ctx_id, ctx->link_hdl);
  2803. req_isp->num_acked = 0;
  2804. req_isp->num_deferred_acks = 0;
  2805. ctx_isp->bubble_frame_cnt = 0;
  2806. req_isp->bubble_detected = false;
  2807. req_isp->cdm_reset_before_apply = true;
  2808. list_del_init(&req->list);
  2809. list_add(&req->list, &ctx->pending_req_list);
  2810. atomic_set(&ctx_isp->process_bubble, 0);
  2811. ctx_isp->active_req_cnt--;
  2812. CAM_DBG(CAM_REQ,
  2813. "Move active req: %lld to pending list(cnt = %d) [bubble re-apply], ctx %u link: 0x%x",
  2814. req->request_id,
  2815. ctx_isp->active_req_cnt, ctx->ctx_id, ctx->link_hdl);
  2816. }
  2817. } else if (req_isp->bubble_detected) {
  2818. ctx_isp->bubble_frame_cnt++;
  2819. CAM_DBG(CAM_ISP,
  2820. "Waiting on bufdone for bubble req: %lld, since frame_cnt = %lld, ctx %u link: 0x%x",
  2821. req->request_id,
  2822. ctx_isp->bubble_frame_cnt, ctx->ctx_id, ctx->link_hdl);
  2823. } else {
  2824. CAM_DBG(CAM_ISP, "Delayed bufdone for req: %lld, ctx %u link: 0x%x",
  2825. req->request_id, ctx->ctx_id, ctx->link_hdl);
  2826. }
  2827. }
  2828. notify_only:
  2829. /*
  2830. * notify reqmgr with sof signal. Note, due to scheduling delay
  2831. * we can run into situation that two active requests has already
  2832. * be in the active queue while we try to do the notification.
  2833. * In this case, we need to skip the current notification. This
  2834. * helps the state machine to catch up the delay.
  2835. */
  2836. if (ctx_isp->active_req_cnt <= 2) {
  2837. __cam_isp_ctx_notify_trigger_util(CAM_TRIGGER_POINT_SOF, ctx_isp);
  2838. list_for_each_entry(req, &ctx->active_req_list, list) {
  2839. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  2840. if ((!req_isp->bubble_detected) &&
  2841. (req->request_id > ctx_isp->reported_req_id)) {
  2842. request_id = req->request_id;
  2843. __cam_isp_ctx_update_event_record(ctx_isp,
  2844. CAM_ISP_CTX_EVENT_EPOCH, req, NULL);
  2845. break;
  2846. }
  2847. }
  2848. if (ctx_isp->substate_activated == CAM_ISP_CTX_ACTIVATED_BUBBLE)
  2849. request_id = 0;
  2850. if (request_id != 0)
  2851. ctx_isp->reported_req_id = request_id;
  2852. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  2853. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  2854. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  2855. CAM_ISP_STATE_CHANGE_TRIGGER_EPOCH,
  2856. request_id);
  2857. }
  2858. ctx_isp->last_sof_timestamp = ctx_isp->sof_timestamp_val;
  2859. return 0;
  2860. }
  2861. static int __cam_isp_ctx_notify_eof_in_activated_state(
  2862. struct cam_isp_context *ctx_isp, void *evt_data)
  2863. {
  2864. int rc = 0;
  2865. struct cam_context *ctx = ctx_isp->base;
  2866. uint64_t last_cdm_done_req = 0;
  2867. /* update last cdm done timestamp */
  2868. rc = __cam_isp_ctx_get_cdm_done_timestamp(ctx, &last_cdm_done_req);
  2869. if (rc)
  2870. CAM_ERR(CAM_ISP, "ctx:%u link: 0x%x Failed to get timestamp from HW",
  2871. ctx->ctx_id, ctx->link_hdl);
  2872. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  2873. CAM_ISP_STATE_CHANGE_TRIGGER_CDM_DONE, last_cdm_done_req);
  2874. /* notify reqmgr with eof signal */
  2875. rc = __cam_isp_ctx_notify_trigger_util(CAM_TRIGGER_POINT_EOF, ctx_isp);
  2876. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  2877. CAM_ISP_STATE_CHANGE_TRIGGER_EOF, 0);
  2878. return rc;
  2879. }
  2880. static int __cam_isp_ctx_reg_upd_in_hw_error(
  2881. struct cam_isp_context *ctx_isp, void *evt_data)
  2882. {
  2883. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_SOF;
  2884. return 0;
  2885. }
  2886. static int __cam_isp_ctx_sof_in_activated_state(
  2887. struct cam_isp_context *ctx_isp, void *evt_data)
  2888. {
  2889. int rc = 0;
  2890. struct cam_isp_hw_sof_event_data *sof_event_data = evt_data;
  2891. struct cam_ctx_request *req = NULL;
  2892. struct cam_context *ctx = ctx_isp->base;
  2893. uint64_t request_id = 0;
  2894. ctx_isp->last_sof_jiffies = jiffies;
  2895. /* First check if there is a valid request in active list */
  2896. list_for_each_entry(req, &ctx->active_req_list, list) {
  2897. if (req->request_id > ctx_isp->reported_req_id) {
  2898. request_id = req->request_id;
  2899. break;
  2900. }
  2901. }
  2902. /*
  2903. * If nothing in active list, current request might have not moved
  2904. * from wait to active list. This could happen if REG_UPDATE to sw
  2905. * is coming immediately after SOF
  2906. */
  2907. if (request_id == 0) {
  2908. req = list_first_entry(&ctx->wait_req_list,
  2909. struct cam_ctx_request, list);
  2910. if (req)
  2911. request_id = req->request_id;
  2912. }
  2913. if (!evt_data) {
  2914. CAM_ERR(CAM_ISP, "in valid sof event data, ctx_idx: %u, link: 0x%x",
  2915. ctx->ctx_id, ctx->link_hdl);
  2916. return -EINVAL;
  2917. }
  2918. __cam_isp_ctx_update_sof_ts_util(sof_event_data, ctx_isp);
  2919. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  2920. CAM_ISP_STATE_CHANGE_TRIGGER_SOF, request_id);
  2921. CAM_DBG(CAM_ISP, "frame id: %lld time stamp:0x%llx, ctx %u request %llu, link: 0x%x",
  2922. ctx_isp->frame_id, ctx_isp->sof_timestamp_val, ctx->ctx_id, request_id,
  2923. ctx->link_hdl);
  2924. return rc;
  2925. }
  2926. static int __cam_isp_ctx_reg_upd_in_sof(struct cam_isp_context *ctx_isp,
  2927. void *evt_data)
  2928. {
  2929. int rc = 0;
  2930. struct cam_ctx_request *req = NULL;
  2931. struct cam_isp_ctx_req *req_isp;
  2932. struct cam_context *ctx = ctx_isp->base;
  2933. if (ctx->state != CAM_CTX_ACTIVATED && ctx_isp->frame_id > 1) {
  2934. CAM_DBG(CAM_ISP, "invalid RUP");
  2935. goto end;
  2936. }
  2937. /*
  2938. * This is for the first update. The initial setting will
  2939. * cause the reg_upd in the first frame.
  2940. */
  2941. if (!list_empty(&ctx->wait_req_list)) {
  2942. req = list_first_entry(&ctx->wait_req_list,
  2943. struct cam_ctx_request, list);
  2944. list_del_init(&req->list);
  2945. cam_smmu_buffer_tracker_putref(&req->buf_tracker);
  2946. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  2947. if (req_isp->num_fence_map_out == req_isp->num_acked)
  2948. list_add_tail(&req->list, &ctx->free_req_list);
  2949. else
  2950. CAM_ERR(CAM_ISP,
  2951. "receive rup in unexpected state, ctx_idx: %u, link: 0x%x",
  2952. ctx->ctx_id, ctx->link_hdl);
  2953. }
  2954. if (req != NULL) {
  2955. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  2956. CAM_ISP_STATE_CHANGE_TRIGGER_REG_UPDATE,
  2957. req->request_id);
  2958. }
  2959. end:
  2960. return rc;
  2961. }
  2962. static int __cam_isp_ctx_epoch_in_applied(struct cam_isp_context *ctx_isp,
  2963. void *evt_data)
  2964. {
  2965. uint64_t request_id = 0;
  2966. uint32_t wait_req_cnt = 0;
  2967. uint32_t sof_event_status = CAM_REQ_MGR_SOF_EVENT_SUCCESS;
  2968. struct cam_ctx_request *req;
  2969. struct cam_isp_ctx_req *req_isp;
  2970. struct cam_context *ctx = ctx_isp->base;
  2971. struct cam_isp_hw_epoch_event_data *epoch_done_event_data =
  2972. (struct cam_isp_hw_epoch_event_data *)evt_data;
  2973. if (!evt_data) {
  2974. CAM_ERR(CAM_ISP, "invalid event data");
  2975. return -EINVAL;
  2976. }
  2977. ctx_isp->frame_id_meta = epoch_done_event_data->frame_id_meta;
  2978. if (list_empty(&ctx->wait_req_list)) {
  2979. /*
  2980. * If no wait req in epoch, this is an error case.
  2981. * The recovery is to go back to sof state
  2982. */
  2983. CAM_ERR(CAM_ISP, "Ctx:%u link: 0x%x No wait request", ctx->ctx_id, ctx->link_hdl);
  2984. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_SOF;
  2985. /* Send SOF event as empty frame*/
  2986. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  2987. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  2988. __cam_isp_ctx_update_event_record(ctx_isp,
  2989. CAM_ISP_CTX_EVENT_EPOCH, NULL, NULL);
  2990. goto end;
  2991. }
  2992. if (ctx_isp->last_applied_jiffies >= ctx_isp->last_sof_jiffies) {
  2993. list_for_each_entry(req, &ctx->wait_req_list, list) {
  2994. wait_req_cnt++;
  2995. }
  2996. /*
  2997. * The previous req is applied after SOF and there is only
  2998. * one applied req, we don't need to report bubble for this case.
  2999. */
  3000. if (wait_req_cnt == 1) {
  3001. req = list_first_entry(&ctx->wait_req_list,
  3002. struct cam_ctx_request, list);
  3003. request_id = req->request_id;
  3004. CAM_INFO(CAM_ISP,
  3005. "ctx:%d Don't report the bubble for req:%lld",
  3006. ctx->ctx_id, request_id);
  3007. goto end;
  3008. }
  3009. }
  3010. /* Update state prior to notifying CRM */
  3011. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_BUBBLE;
  3012. req = list_first_entry(&ctx->wait_req_list, struct cam_ctx_request,
  3013. list);
  3014. req_isp = (struct cam_isp_ctx_req *)req->req_priv;
  3015. req_isp->bubble_detected = true;
  3016. req_isp->reapply_type = CAM_CONFIG_REAPPLY_IO;
  3017. req_isp->cdm_reset_before_apply = false;
  3018. atomic_set(&ctx_isp->process_bubble, 1);
  3019. CAM_INFO_RATE_LIMIT(CAM_ISP, "ctx:%u link: 0x%x Report Bubble flag %d req id:%lld",
  3020. ctx->ctx_id, ctx->link_hdl, req_isp->bubble_report, req->request_id);
  3021. if (req_isp->bubble_report) {
  3022. __cam_isp_ctx_notify_error_util(CAM_TRIGGER_POINT_SOF, CRM_KMD_ERR_BUBBLE,
  3023. req->request_id, ctx_isp);
  3024. trace_cam_log_event("Bubble", "Rcvd epoch in applied state",
  3025. req->request_id, ctx->ctx_id);
  3026. } else {
  3027. req_isp->bubble_report = 0;
  3028. CAM_DBG(CAM_ISP, "Skip bubble recovery for req %lld ctx %u, link: 0x%x",
  3029. req->request_id, ctx->ctx_id, ctx->link_hdl);
  3030. if (ctx_isp->active_req_cnt <= 1)
  3031. __cam_isp_ctx_notify_trigger_util(CAM_TRIGGER_POINT_SOF, ctx_isp);
  3032. }
  3033. /*
  3034. * Always move the request to active list. Let buf done
  3035. * function handles the rest.
  3036. */
  3037. list_del_init(&req->list);
  3038. list_add_tail(&req->list, &ctx->active_req_list);
  3039. ctx_isp->active_req_cnt++;
  3040. CAM_DBG(CAM_REQ, "move request %lld to active list(cnt = %d), ctx %u, link: 0x%x",
  3041. req->request_id, ctx_isp->active_req_cnt, ctx->ctx_id, ctx->link_hdl);
  3042. /*
  3043. * Handle the deferred buf done after moving
  3044. * the bubble req to active req list.
  3045. */
  3046. __cam_isp_ctx_handle_deferred_buf_done_in_bubble(
  3047. ctx_isp, req);
  3048. /*
  3049. * Update the record before req pointer to
  3050. * other invalid req.
  3051. */
  3052. __cam_isp_ctx_update_event_record(ctx_isp,
  3053. CAM_ISP_CTX_EVENT_EPOCH, req, NULL);
  3054. /*
  3055. * Get the req again from active_req_list in case
  3056. * the active req cnt is 2.
  3057. */
  3058. list_for_each_entry(req, &ctx->active_req_list, list) {
  3059. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  3060. if ((!req_isp->bubble_report) &&
  3061. (req->request_id > ctx_isp->reported_req_id)) {
  3062. request_id = req->request_id;
  3063. ctx_isp->reported_req_id = request_id;
  3064. CAM_DBG(CAM_ISP,
  3065. "ctx %u link: 0x%x reported_req_id update to %lld",
  3066. ctx->ctx_id, ctx->link_hdl, ctx_isp->reported_req_id);
  3067. break;
  3068. }
  3069. }
  3070. if ((request_id != 0) && req_isp->bubble_detected)
  3071. sof_event_status = CAM_REQ_MGR_SOF_EVENT_ERROR;
  3072. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  3073. sof_event_status);
  3074. cam_req_mgr_debug_delay_detect();
  3075. trace_cam_delay_detect("ISP",
  3076. "bubble epoch_in_applied", req->request_id,
  3077. ctx->ctx_id, ctx->link_hdl, ctx->session_hdl,
  3078. CAM_DEFAULT_VALUE);
  3079. end:
  3080. if (request_id == 0) {
  3081. req = list_last_entry(&ctx->active_req_list,
  3082. struct cam_ctx_request, list);
  3083. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  3084. CAM_ISP_STATE_CHANGE_TRIGGER_EPOCH, req->request_id);
  3085. } else {
  3086. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  3087. CAM_ISP_STATE_CHANGE_TRIGGER_EPOCH, request_id);
  3088. }
  3089. CAM_DBG(CAM_ISP, "next Substate[%s], ctx_idx: %u link: 0x%x",
  3090. __cam_isp_ctx_substate_val_to_type(
  3091. ctx_isp->substate_activated), ctx->ctx_id, ctx->link_hdl);
  3092. return 0;
  3093. }
  3094. static int __cam_isp_ctx_buf_done_in_sof(struct cam_isp_context *ctx_isp,
  3095. void *evt_data)
  3096. {
  3097. int rc = 0;
  3098. struct cam_isp_hw_done_event_data *done =
  3099. (struct cam_isp_hw_done_event_data *) evt_data;
  3100. rc = __cam_isp_ctx_handle_buf_done_in_activated_state(ctx_isp, done, 0);
  3101. return rc;
  3102. }
  3103. static int __cam_isp_ctx_buf_done_in_applied(struct cam_isp_context *ctx_isp,
  3104. void *evt_data)
  3105. {
  3106. int rc = 0;
  3107. struct cam_isp_hw_done_event_data *done =
  3108. (struct cam_isp_hw_done_event_data *) evt_data;
  3109. rc = __cam_isp_ctx_handle_buf_done_in_activated_state(ctx_isp, done, 0);
  3110. return rc;
  3111. }
  3112. static int __cam_isp_ctx_sof_in_epoch(struct cam_isp_context *ctx_isp,
  3113. void *evt_data)
  3114. {
  3115. int rc = 0;
  3116. struct cam_context *ctx = ctx_isp->base;
  3117. struct cam_isp_hw_sof_event_data *sof_event_data = evt_data;
  3118. struct cam_ctx_request *req;
  3119. if (!evt_data) {
  3120. CAM_ERR(CAM_ISP, "in valid sof event data");
  3121. return -EINVAL;
  3122. }
  3123. ctx_isp->last_sof_jiffies = jiffies;
  3124. if (atomic_read(&ctx_isp->apply_in_progress))
  3125. CAM_INFO(CAM_ISP, "Apply is in progress at the time of SOF, ctx: %u, link: 0x%x",
  3126. ctx->ctx_id, ctx->link_hdl);
  3127. __cam_isp_ctx_update_sof_ts_util(sof_event_data, ctx_isp);
  3128. if (list_empty(&ctx->active_req_list))
  3129. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_SOF;
  3130. else
  3131. CAM_DBG(CAM_ISP, "Still need to wait for the buf done, ctx: %u, link: 0x%x",
  3132. ctx->ctx_id, ctx->link_hdl);
  3133. req = list_last_entry(&ctx->active_req_list,
  3134. struct cam_ctx_request, list);
  3135. if (req)
  3136. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  3137. CAM_ISP_STATE_CHANGE_TRIGGER_SOF,
  3138. req->request_id);
  3139. if (ctx_isp->frame_id == 1)
  3140. CAM_INFO(CAM_ISP,
  3141. "First SOF in EPCR ctx:%u link: 0x%x frame_id:%lld next substate %s",
  3142. ctx->ctx_id, ctx->link_hdl, ctx_isp->frame_id,
  3143. __cam_isp_ctx_substate_val_to_type(
  3144. ctx_isp->substate_activated));
  3145. CAM_DBG(CAM_ISP, "SOF in epoch ctx:%u link: 0x%x frame_id:%lld next substate:%s",
  3146. ctx->ctx_id, ctx->link_hdl, ctx_isp->frame_id,
  3147. __cam_isp_ctx_substate_val_to_type(
  3148. ctx_isp->substate_activated));
  3149. return rc;
  3150. }
  3151. static int __cam_isp_ctx_buf_done_in_epoch(struct cam_isp_context *ctx_isp,
  3152. void *evt_data)
  3153. {
  3154. int rc = 0;
  3155. struct cam_isp_hw_done_event_data *done =
  3156. (struct cam_isp_hw_done_event_data *) evt_data;
  3157. rc = __cam_isp_ctx_handle_buf_done_in_activated_state(ctx_isp, done, 0);
  3158. return rc;
  3159. }
  3160. static int __cam_isp_ctx_buf_done_in_bubble(
  3161. struct cam_isp_context *ctx_isp, void *evt_data)
  3162. {
  3163. int rc = 0;
  3164. struct cam_isp_hw_done_event_data *done =
  3165. (struct cam_isp_hw_done_event_data *) evt_data;
  3166. rc = __cam_isp_ctx_handle_buf_done_in_activated_state(ctx_isp, done, 1);
  3167. return rc;
  3168. }
  3169. static int __cam_isp_ctx_epoch_in_bubble_applied(
  3170. struct cam_isp_context *ctx_isp, void *evt_data)
  3171. {
  3172. uint64_t request_id = 0;
  3173. struct cam_ctx_request *req;
  3174. struct cam_isp_ctx_req *req_isp;
  3175. struct cam_context *ctx = ctx_isp->base;
  3176. struct cam_isp_hw_epoch_event_data *epoch_done_event_data =
  3177. (struct cam_isp_hw_epoch_event_data *)evt_data;
  3178. if (!evt_data) {
  3179. CAM_ERR(CAM_ISP, "invalid event data, ctx_idx: %u, link: 0x%x",
  3180. ctx->ctx_id, ctx->link_hdl);
  3181. return -EINVAL;
  3182. }
  3183. ctx_isp->frame_id_meta = epoch_done_event_data->frame_id_meta;
  3184. /*
  3185. * This means we missed the reg upd ack. So we need to
  3186. * transition to BUBBLE state again.
  3187. */
  3188. if (list_empty(&ctx->wait_req_list)) {
  3189. /*
  3190. * If no pending req in epoch, this is an error case.
  3191. * Just go back to the bubble state.
  3192. */
  3193. CAM_ERR(CAM_ISP, "ctx:%u link: 0x%x No pending request.",
  3194. ctx->ctx_id, ctx->link_hdl);
  3195. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  3196. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  3197. __cam_isp_ctx_update_event_record(ctx_isp,
  3198. CAM_ISP_CTX_EVENT_EPOCH, NULL, NULL);
  3199. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_BUBBLE;
  3200. goto end;
  3201. }
  3202. req = list_first_entry(&ctx->wait_req_list, struct cam_ctx_request,
  3203. list);
  3204. req_isp = (struct cam_isp_ctx_req *)req->req_priv;
  3205. req_isp->bubble_detected = true;
  3206. CAM_INFO_RATE_LIMIT(CAM_ISP, "Ctx:%u link: 0x%x Report Bubble flag %d req id:%lld",
  3207. ctx->ctx_id, ctx->link_hdl, req_isp->bubble_report, req->request_id);
  3208. req_isp->reapply_type = CAM_CONFIG_REAPPLY_IO;
  3209. req_isp->cdm_reset_before_apply = false;
  3210. if (req_isp->bubble_report) {
  3211. __cam_isp_ctx_notify_error_util(CAM_TRIGGER_POINT_SOF, CRM_KMD_ERR_BUBBLE,
  3212. req->request_id, ctx_isp);
  3213. atomic_set(&ctx_isp->process_bubble, 1);
  3214. } else {
  3215. req_isp->bubble_report = 0;
  3216. CAM_DBG(CAM_ISP, "Skip bubble recovery for req %lld ctx %u link: 0x%x",
  3217. req->request_id, ctx->ctx_id, ctx->link_hdl);
  3218. if (ctx_isp->active_req_cnt <= 1)
  3219. __cam_isp_ctx_notify_trigger_util(CAM_TRIGGER_POINT_SOF, ctx_isp);
  3220. atomic_set(&ctx_isp->process_bubble, 1);
  3221. }
  3222. /*
  3223. * Always move the request to active list. Let buf done
  3224. * function handles the rest.
  3225. */
  3226. list_del_init(&req->list);
  3227. list_add_tail(&req->list, &ctx->active_req_list);
  3228. ctx_isp->active_req_cnt++;
  3229. CAM_DBG(CAM_ISP, "move request %lld to active list(cnt = %d) ctx %u, link: 0x%x",
  3230. req->request_id, ctx_isp->active_req_cnt, ctx->ctx_id, ctx->link_hdl);
  3231. /*
  3232. * Handle the deferred buf done after moving
  3233. * the bubble req to active req list.
  3234. */
  3235. __cam_isp_ctx_handle_deferred_buf_done_in_bubble(
  3236. ctx_isp, req);
  3237. if (!req_isp->bubble_detected) {
  3238. req = list_first_entry(&ctx->pending_req_list, struct cam_ctx_request,
  3239. list);
  3240. req_isp->bubble_detected = true;
  3241. req_isp->reapply_type = CAM_CONFIG_REAPPLY_IO;
  3242. req_isp->cdm_reset_before_apply = false;
  3243. atomic_set(&ctx_isp->process_bubble, 1);
  3244. list_del_init(&req->list);
  3245. list_add_tail(&req->list, &ctx->active_req_list);
  3246. ctx_isp->active_req_cnt++;
  3247. }
  3248. if (!req_isp->bubble_report) {
  3249. if (req->request_id > ctx_isp->reported_req_id) {
  3250. request_id = req->request_id;
  3251. ctx_isp->reported_req_id = request_id;
  3252. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  3253. CAM_REQ_MGR_SOF_EVENT_ERROR);
  3254. __cam_isp_ctx_update_event_record(ctx_isp,
  3255. CAM_ISP_CTX_EVENT_EPOCH, req, NULL);
  3256. } else {
  3257. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  3258. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  3259. __cam_isp_ctx_update_event_record(ctx_isp,
  3260. CAM_ISP_CTX_EVENT_EPOCH, NULL, NULL);
  3261. }
  3262. } else {
  3263. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  3264. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  3265. __cam_isp_ctx_update_event_record(ctx_isp,
  3266. CAM_ISP_CTX_EVENT_EPOCH, NULL, NULL);
  3267. }
  3268. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_BUBBLE;
  3269. CAM_DBG(CAM_ISP, "next Substate[%s], ctx_idx: %u, link: 0x%x",
  3270. __cam_isp_ctx_substate_val_to_type(
  3271. ctx_isp->substate_activated), ctx->ctx_id, ctx->link_hdl);
  3272. cam_req_mgr_debug_delay_detect();
  3273. trace_cam_delay_detect("ISP",
  3274. "bubble epoch_in_bubble_applied",
  3275. req->request_id, ctx->ctx_id, ctx->link_hdl,
  3276. ctx->session_hdl,
  3277. CAM_DEFAULT_VALUE);
  3278. end:
  3279. req = list_last_entry(&ctx->active_req_list, struct cam_ctx_request,
  3280. list);
  3281. if (req)
  3282. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  3283. CAM_ISP_STATE_CHANGE_TRIGGER_EPOCH, req->request_id);
  3284. return 0;
  3285. }
  3286. static int __cam_isp_ctx_buf_done_in_bubble_applied(
  3287. struct cam_isp_context *ctx_isp, void *evt_data)
  3288. {
  3289. int rc = 0;
  3290. struct cam_isp_hw_done_event_data *done =
  3291. (struct cam_isp_hw_done_event_data *) evt_data;
  3292. rc = __cam_isp_ctx_handle_buf_done_in_activated_state(ctx_isp, done, 1);
  3293. return rc;
  3294. }
  3295. static void __cam_isp_get_notification_evt_params(
  3296. uint32_t hw_error, uint32_t *fence_evt_cause,
  3297. uint32_t *req_mgr_err_code, uint32_t *recovery_type)
  3298. {
  3299. uint32_t err_type, err_code = 0, recovery_type_temp;
  3300. err_type = CAM_SYNC_ISP_EVENT_UNKNOWN;
  3301. recovery_type_temp = CAM_REQ_MGR_ERROR_TYPE_RECOVERY;
  3302. if (hw_error & CAM_ISP_HW_ERROR_OVERFLOW) {
  3303. err_code |= CAM_REQ_MGR_ISP_UNREPORTED_ERROR;
  3304. err_type = CAM_SYNC_ISP_EVENT_OVERFLOW;
  3305. recovery_type_temp |= CAM_REQ_MGR_ERROR_TYPE_RECOVERY;
  3306. }
  3307. if (hw_error & CAM_ISP_HW_ERROR_CSID_OUTPUT_FIFO_OVERFLOW) {
  3308. err_code |= CAM_REQ_MGR_CSID_FIFO_OVERFLOW_ERROR;
  3309. err_type = CAM_SYNC_ISP_EVENT_CSID_OUTPUT_FIFO_OVERFLOW;
  3310. recovery_type_temp |= CAM_REQ_MGR_ERROR_TYPE_RECOVERY;
  3311. }
  3312. if (hw_error & CAM_ISP_HW_ERROR_RECOVERY_OVERFLOW) {
  3313. err_code |= CAM_REQ_MGR_CSID_RECOVERY_OVERFLOW_ERROR;
  3314. err_type = CAM_SYNC_ISP_EVENT_RECOVERY_OVERFLOW;
  3315. recovery_type_temp |= CAM_REQ_MGR_ERROR_TYPE_RECOVERY;
  3316. }
  3317. if (hw_error & CAM_ISP_HW_ERROR_P2I_ERROR) {
  3318. err_code |= CAM_REQ_MGR_ISP_UNREPORTED_ERROR;
  3319. err_type = CAM_SYNC_ISP_EVENT_P2I_ERROR;
  3320. recovery_type_temp |= CAM_REQ_MGR_ERROR_TYPE_RECOVERY;
  3321. }
  3322. if (hw_error & CAM_ISP_HW_ERROR_VIOLATION) {
  3323. err_code |= CAM_REQ_MGR_ISP_UNREPORTED_ERROR;
  3324. err_type = CAM_SYNC_ISP_EVENT_VIOLATION;
  3325. recovery_type_temp |= CAM_REQ_MGR_ERROR_TYPE_RECOVERY;
  3326. }
  3327. if (hw_error & CAM_ISP_HW_ERROR_HWPD_VIOLATION) {
  3328. err_code |= CAM_REQ_MGR_ISP_ERR_HWPD_VIOLATION;
  3329. err_type = CAM_SYNC_ISP_EVENT_VIOLATION;
  3330. recovery_type_temp |= CAM_REQ_MGR_ERROR_TYPE_RECOVERY;
  3331. }
  3332. if (hw_error & CAM_ISP_HW_ERROR_BUSIF_OVERFLOW) {
  3333. err_code |= CAM_REQ_MGR_ISP_UNREPORTED_ERROR;
  3334. err_type = CAM_SYNC_ISP_EVENT_BUSIF_OVERFLOW;
  3335. recovery_type_temp |= CAM_REQ_MGR_ERROR_TYPE_RECOVERY;
  3336. }
  3337. if (hw_error & CAM_ISP_HW_ERROR_CSID_SENSOR_SWITCH_ERROR) {
  3338. err_code |= CAM_REQ_MGR_CSID_ERR_ON_SENSOR_SWITCHING;
  3339. err_type = CAM_SYNC_ISP_EVENT_CSID_SENSOR_SWITCH_ERROR;
  3340. recovery_type_temp |= CAM_REQ_MGR_ERROR_TYPE_FULL_RECOVERY;
  3341. }
  3342. if (hw_error & CAM_ISP_HW_ERROR_CSID_LANE_FIFO_OVERFLOW) {
  3343. err_code |= CAM_REQ_MGR_CSID_LANE_FIFO_OVERFLOW_ERROR;
  3344. err_type = CAM_SYNC_ISP_EVENT_CSID_RX_ERROR;
  3345. recovery_type_temp |= CAM_REQ_MGR_ERROR_TYPE_FULL_RECOVERY;
  3346. }
  3347. if (hw_error & CAM_ISP_HW_ERROR_CSID_PKT_HDR_CORRUPTED) {
  3348. err_code |= CAM_REQ_MGR_CSID_RX_PKT_HDR_CORRUPTION;
  3349. err_type = CAM_SYNC_ISP_EVENT_CSID_RX_ERROR;
  3350. recovery_type_temp |= CAM_REQ_MGR_ERROR_TYPE_FULL_RECOVERY;
  3351. }
  3352. if (hw_error & CAM_ISP_HW_ERROR_CSID_MISSING_PKT_HDR_DATA) {
  3353. err_code |= CAM_REQ_MGR_CSID_MISSING_PKT_HDR_DATA;
  3354. err_type = CAM_SYNC_ISP_EVENT_CSID_RX_ERROR;
  3355. recovery_type_temp |= CAM_REQ_MGR_ERROR_TYPE_FULL_RECOVERY;
  3356. }
  3357. if (hw_error & CAM_ISP_HW_ERROR_CSID_UNBOUNDED_FRAME) {
  3358. err_code |= CAM_REQ_MGR_CSID_UNBOUNDED_FRAME;
  3359. err_type = CAM_SYNC_ISP_EVENT_CSID_RX_ERROR;
  3360. recovery_type_temp |= CAM_REQ_MGR_ERROR_TYPE_FULL_RECOVERY;
  3361. }
  3362. if (hw_error & CAM_ISP_HW_ERROR_CSID_FRAME_SIZE) {
  3363. err_code |= CAM_REQ_MGR_CSID_PIXEL_COUNT_MISMATCH;
  3364. err_type = CAM_SYNC_ISP_EVENT_CSID_RX_ERROR;
  3365. recovery_type_temp |= CAM_REQ_MGR_ERROR_TYPE_RECOVERY;
  3366. }
  3367. if (hw_error & CAM_ISP_HW_ERROR_CSID_MISSING_EOT) {
  3368. err_code |= CAM_REQ_MGR_CSID_MISSING_EOT;
  3369. err_type = CAM_SYNC_ISP_EVENT_CSID_RX_ERROR;
  3370. recovery_type_temp |= CAM_REQ_MGR_ERROR_TYPE_FULL_RECOVERY;
  3371. }
  3372. if (hw_error & CAM_ISP_HW_ERROR_CSID_PKT_PAYLOAD_CORRUPTED) {
  3373. err_code |= CAM_REQ_MGR_CSID_RX_PKT_PAYLOAD_CORRUPTION;
  3374. err_type = CAM_SYNC_ISP_EVENT_CSID_RX_ERROR;
  3375. recovery_type_temp |= CAM_REQ_MGR_ERROR_TYPE_FULL_RECOVERY;
  3376. }
  3377. if (recovery_type_temp == (CAM_REQ_MGR_ERROR_TYPE_FULL_RECOVERY |
  3378. CAM_REQ_MGR_ERROR_TYPE_RECOVERY))
  3379. recovery_type_temp = CAM_REQ_MGR_ERROR_TYPE_FULL_RECOVERY;
  3380. if (!err_code)
  3381. err_code = CAM_REQ_MGR_ISP_UNREPORTED_ERROR;
  3382. *req_mgr_err_code = err_code;
  3383. *fence_evt_cause = err_type;
  3384. *recovery_type = recovery_type_temp;
  3385. }
  3386. static bool __cam_isp_ctx_request_can_reapply(
  3387. struct cam_isp_ctx_req *req_isp)
  3388. {
  3389. int i;
  3390. for (i = 0; i < req_isp->num_fence_map_out; i++)
  3391. if (req_isp->fence_map_out[i].sync_id == -1)
  3392. return false;
  3393. return true;
  3394. }
  3395. static int __cam_isp_ctx_validate_for_req_reapply_util(
  3396. struct cam_isp_context *ctx_isp)
  3397. {
  3398. int rc = 0;
  3399. struct cam_ctx_request *req_temp;
  3400. struct cam_ctx_request *req = NULL;
  3401. struct cam_isp_ctx_req *req_isp = NULL;
  3402. struct cam_context *ctx = ctx_isp->base;
  3403. if (in_task())
  3404. spin_lock_bh(&ctx->lock);
  3405. /* Check for req in active/wait lists */
  3406. if (list_empty(&ctx->active_req_list)) {
  3407. CAM_DBG(CAM_ISP,
  3408. "Active request list empty for ctx: %u on link: 0x%x",
  3409. ctx->ctx_id, ctx->link_hdl);
  3410. if (list_empty(&ctx->wait_req_list)) {
  3411. CAM_WARN(CAM_ISP,
  3412. "No active/wait req for ctx: %u on link: 0x%x start from pending",
  3413. ctx->ctx_id, ctx->link_hdl);
  3414. rc = 0;
  3415. goto end;
  3416. }
  3417. }
  3418. /* Validate if all fences for active requests are not signaled */
  3419. if (!list_empty(&ctx->active_req_list)) {
  3420. list_for_each_entry_safe_reverse(req, req_temp,
  3421. &ctx->active_req_list, list) {
  3422. /*
  3423. * If some fences of the active request are already
  3424. * signaled, we should not do recovery for the buffer
  3425. * and timestamp consistency.
  3426. */
  3427. req_isp = (struct cam_isp_ctx_req *)req->req_priv;
  3428. if (!__cam_isp_ctx_request_can_reapply(req_isp)) {
  3429. CAM_WARN(CAM_ISP,
  3430. "Req: %llu in ctx:%u on link: 0x%x fence has partially signaled, cannot do recovery",
  3431. req->request_id, ctx->ctx_id, ctx->link_hdl);
  3432. rc = -EINVAL;
  3433. goto end;
  3434. }
  3435. }
  3436. }
  3437. /* Move active requests to pending list */
  3438. if (!list_empty(&ctx->active_req_list)) {
  3439. list_for_each_entry_safe_reverse(req, req_temp,
  3440. &ctx->active_req_list, list) {
  3441. list_del_init(&req->list);
  3442. __cam_isp_ctx_enqueue_request_in_order(ctx, req, false);
  3443. ctx_isp->active_req_cnt--;
  3444. CAM_DBG(CAM_ISP, "ctx:%u link:0x%x move active req %llu to pending",
  3445. ctx->ctx_id, ctx->link_hdl, req->request_id);
  3446. }
  3447. }
  3448. /* Move wait requests to pending list */
  3449. if (!list_empty(&ctx->wait_req_list)) {
  3450. list_for_each_entry_safe_reverse(req, req_temp, &ctx->wait_req_list, list) {
  3451. list_del_init(&req->list);
  3452. __cam_isp_ctx_enqueue_request_in_order(ctx, req, false);
  3453. CAM_DBG(CAM_ISP, "ctx:%u link:0x%x move wait req %llu to pending",
  3454. ctx->ctx_id, ctx->link_hdl, req->request_id);
  3455. }
  3456. }
  3457. end:
  3458. if (in_task())
  3459. spin_unlock_bh(&ctx->lock);
  3460. return rc;
  3461. }
  3462. static int __cam_isp_ctx_handle_recovery_req_util(
  3463. struct cam_isp_context *ctx_isp)
  3464. {
  3465. int rc = 0;
  3466. struct cam_context *ctx = ctx_isp->base;
  3467. struct cam_ctx_request *req_to_reapply = NULL;
  3468. if (list_empty(&ctx->pending_req_list)) {
  3469. CAM_WARN(CAM_ISP,
  3470. "No pending request to recover from on ctx: %u", ctx->ctx_id);
  3471. return -EINVAL;
  3472. }
  3473. req_to_reapply = list_first_entry(&ctx->pending_req_list,
  3474. struct cam_ctx_request, list);
  3475. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_EPOCH;
  3476. ctx_isp->recovery_req_id = req_to_reapply->request_id;
  3477. atomic_set(&ctx_isp->internal_recovery_set, 1);
  3478. CAM_INFO(CAM_ISP, "Notify CRM to reapply req:%llu for ctx:%u link:0x%x",
  3479. req_to_reapply->request_id, ctx->ctx_id, ctx->link_hdl);
  3480. rc = __cam_isp_ctx_notify_error_util(CAM_TRIGGER_POINT_SOF,
  3481. CRM_KMD_WARN_INTERNAL_RECOVERY, req_to_reapply->request_id,
  3482. ctx_isp);
  3483. if (rc) {
  3484. /* Unable to notify CRM to do reapply back to normal */
  3485. CAM_WARN(CAM_ISP,
  3486. "ctx:%u link:0x%x unable to notify CRM for req %llu",
  3487. ctx->ctx_id, ctx->link_hdl, ctx_isp->recovery_req_id);
  3488. ctx_isp->recovery_req_id = 0;
  3489. atomic_set(&ctx_isp->internal_recovery_set, 0);
  3490. }
  3491. return rc;
  3492. }
  3493. static int __cam_isp_ctx_trigger_error_req_reapply(
  3494. uint32_t err_type, struct cam_isp_context *ctx_isp)
  3495. {
  3496. int rc = 0;
  3497. struct cam_context *ctx = ctx_isp->base;
  3498. if ((err_type & CAM_ISP_HW_ERROR_RECOVERY_OVERFLOW) &&
  3499. (isp_ctx_debug.disable_internal_recovery_mask &
  3500. CAM_ISP_CTX_DISABLE_RECOVERY_BUS_OVERFLOW))
  3501. return -EINVAL;
  3502. /*
  3503. * For errors that can be recoverable within kmd, we
  3504. * try to do internal hw stop, restart and notify CRM
  3505. * to do reapply with the help of bubble control flow.
  3506. */
  3507. rc = __cam_isp_ctx_validate_for_req_reapply_util(ctx_isp);
  3508. if (rc)
  3509. goto end;
  3510. rc = __cam_isp_ctx_handle_recovery_req_util(ctx_isp);
  3511. if (rc)
  3512. goto end;
  3513. CAM_DBG(CAM_ISP, "Triggered internal recovery for req:%llu ctx:%u on link 0x%x",
  3514. ctx_isp->recovery_req_id, ctx->ctx_id, ctx->link_hdl);
  3515. end:
  3516. return rc;
  3517. }
  3518. static int __cam_isp_ctx_handle_error(struct cam_isp_context *ctx_isp,
  3519. void *evt_data)
  3520. {
  3521. int rc = 0;
  3522. enum cam_req_mgr_device_error error;
  3523. uint32_t i = 0;
  3524. bool found = 0;
  3525. struct cam_ctx_request *req = NULL;
  3526. struct cam_ctx_request *req_to_report = NULL;
  3527. struct cam_ctx_request *req_to_dump = NULL;
  3528. struct cam_ctx_request *req_temp;
  3529. struct cam_isp_ctx_req *req_isp = NULL;
  3530. struct cam_isp_ctx_req *req_isp_to_report = NULL;
  3531. uint64_t error_request_id;
  3532. struct cam_hw_fence_map_entry *fence_map_out = NULL;
  3533. uint32_t recovery_type, fence_evt_cause;
  3534. uint32_t req_mgr_err_code;
  3535. struct cam_context *ctx = ctx_isp->base;
  3536. struct cam_isp_hw_error_event_data *error_event_data =
  3537. (struct cam_isp_hw_error_event_data *)evt_data;
  3538. CAM_DBG(CAM_ISP, "Enter HW error_type = %d, ctx:%u on link 0x%x",
  3539. error_event_data->error_type, ctx->ctx_id, ctx->link_hdl);
  3540. if (error_event_data->try_internal_recovery) {
  3541. rc = __cam_isp_ctx_trigger_error_req_reapply(error_event_data->error_type, ctx_isp);
  3542. if (!rc)
  3543. goto exit;
  3544. }
  3545. if (!ctx_isp->offline_context)
  3546. __cam_isp_ctx_pause_crm_timer(ctx);
  3547. __cam_isp_ctx_dump_frame_timing_record(ctx_isp);
  3548. __cam_isp_ctx_trigger_reg_dump(CAM_HW_MGR_CMD_REG_DUMP_ON_ERROR, ctx);
  3549. __cam_isp_get_notification_evt_params(error_event_data->error_type,
  3550. &fence_evt_cause, &req_mgr_err_code, &recovery_type);
  3551. /*
  3552. * The error is likely caused by first request on the active list.
  3553. * If active list is empty check wait list (maybe error hit as soon
  3554. * as RUP and we handle error before RUP.
  3555. */
  3556. if (list_empty(&ctx->active_req_list)) {
  3557. CAM_DBG(CAM_ISP,
  3558. "handling error with no active request, ctx:%u on link 0x%x",
  3559. ctx->ctx_id, ctx->link_hdl);
  3560. if (list_empty(&ctx->wait_req_list)) {
  3561. CAM_ERR_RATE_LIMIT(CAM_ISP,
  3562. "Error with no active/wait request, ctx:%u on link 0x%x",
  3563. ctx->ctx_id, ctx->link_hdl);
  3564. goto end;
  3565. } else {
  3566. req_to_dump = list_first_entry(&ctx->wait_req_list,
  3567. struct cam_ctx_request, list);
  3568. }
  3569. } else {
  3570. req_to_dump = list_first_entry(&ctx->active_req_list,
  3571. struct cam_ctx_request, list);
  3572. }
  3573. req_isp = (struct cam_isp_ctx_req *) req_to_dump->req_priv;
  3574. if (error_event_data->enable_req_dump)
  3575. rc = cam_isp_ctx_dump_req(req_isp, 0, 0, NULL, false);
  3576. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  3577. CAM_ISP_STATE_CHANGE_TRIGGER_ERROR, req_to_dump->request_id);
  3578. list_for_each_entry_safe(req, req_temp,
  3579. &ctx->active_req_list, list) {
  3580. cam_smmu_buffer_tracker_putref(&req->buf_tracker);
  3581. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  3582. if (!req_isp->bubble_report) {
  3583. CAM_ERR(CAM_ISP, "signalled error for req %llu, ctx:%u on link 0x%x",
  3584. req->request_id, ctx->ctx_id, ctx->link_hdl);
  3585. for (i = 0; i < req_isp->num_fence_map_out; i++) {
  3586. fence_map_out =
  3587. &req_isp->fence_map_out[i];
  3588. if (req_isp->fence_map_out[i].sync_id != -1) {
  3589. CAM_DBG(CAM_ISP,
  3590. "req %llu, Sync fd 0x%x ctx %u, link 0x%x",
  3591. req->request_id,
  3592. req_isp->fence_map_out[i].sync_id,
  3593. ctx->ctx_id, ctx->link_hdl);
  3594. rc = cam_sync_signal(
  3595. fence_map_out->sync_id,
  3596. CAM_SYNC_STATE_SIGNALED_ERROR,
  3597. fence_evt_cause);
  3598. fence_map_out->sync_id = -1;
  3599. }
  3600. }
  3601. list_del_init(&req->list);
  3602. list_add_tail(&req->list, &ctx->free_req_list);
  3603. ctx_isp->active_req_cnt--;
  3604. } else {
  3605. found = 1;
  3606. break;
  3607. }
  3608. }
  3609. if (found)
  3610. goto move_to_pending;
  3611. list_for_each_entry_safe(req, req_temp,
  3612. &ctx->wait_req_list, list) {
  3613. cam_smmu_buffer_tracker_putref(&req->buf_tracker);
  3614. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  3615. if (!req_isp->bubble_report) {
  3616. CAM_ERR(CAM_ISP, "signalled error for req %llu, ctx %u, link 0x%x",
  3617. req->request_id, ctx->ctx_id, ctx->link_hdl);
  3618. for (i = 0; i < req_isp->num_fence_map_out; i++) {
  3619. fence_map_out =
  3620. &req_isp->fence_map_out[i];
  3621. if (req_isp->fence_map_out[i].sync_id != -1) {
  3622. CAM_DBG(CAM_ISP,
  3623. "req %llu, Sync fd 0x%x ctx %u link 0x%x",
  3624. req->request_id,
  3625. req_isp->fence_map_out[i].sync_id,
  3626. ctx->ctx_id, ctx->link_hdl);
  3627. rc = cam_sync_signal(
  3628. fence_map_out->sync_id,
  3629. CAM_SYNC_STATE_SIGNALED_ERROR,
  3630. fence_evt_cause);
  3631. fence_map_out->sync_id = -1;
  3632. }
  3633. }
  3634. list_del_init(&req->list);
  3635. list_add_tail(&req->list, &ctx->free_req_list);
  3636. } else {
  3637. found = 1;
  3638. break;
  3639. }
  3640. }
  3641. move_to_pending:
  3642. /*
  3643. * If bubble recovery is enabled on any request we need to move that
  3644. * request and all the subsequent requests to the pending list.
  3645. * Note:
  3646. * We need to traverse the active list in reverse order and add
  3647. * to head of pending list.
  3648. * e.g. pending current state: 10, 11 | active current state: 8, 9
  3649. * intermittent for loop iteration- pending: 9, 10, 11 | active: 8
  3650. * final state - pending: 8, 9, 10, 11 | active: NULL
  3651. */
  3652. if (found) {
  3653. list_for_each_entry_safe_reverse(req, req_temp,
  3654. &ctx->active_req_list, list) {
  3655. list_del_init(&req->list);
  3656. list_add(&req->list, &ctx->pending_req_list);
  3657. ctx_isp->active_req_cnt--;
  3658. }
  3659. list_for_each_entry_safe_reverse(req, req_temp,
  3660. &ctx->wait_req_list, list) {
  3661. list_del_init(&req->list);
  3662. list_add(&req->list, &ctx->pending_req_list);
  3663. }
  3664. }
  3665. end:
  3666. do {
  3667. if (list_empty(&ctx->pending_req_list)) {
  3668. error_request_id = ctx_isp->last_applied_req_id;
  3669. break;
  3670. }
  3671. req = list_first_entry(&ctx->pending_req_list,
  3672. struct cam_ctx_request, list);
  3673. cam_smmu_buffer_tracker_putref(&req->buf_tracker);
  3674. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  3675. error_request_id = ctx_isp->last_applied_req_id;
  3676. if (req_isp->bubble_report) {
  3677. req_to_report = req;
  3678. req_isp_to_report = req_to_report->req_priv;
  3679. break;
  3680. }
  3681. for (i = 0; i < req_isp->num_fence_map_out; i++) {
  3682. if (req_isp->fence_map_out[i].sync_id != -1)
  3683. rc = cam_sync_signal(
  3684. req_isp->fence_map_out[i].sync_id,
  3685. CAM_SYNC_STATE_SIGNALED_ERROR,
  3686. fence_evt_cause);
  3687. req_isp->fence_map_out[i].sync_id = -1;
  3688. }
  3689. list_del_init(&req->list);
  3690. list_add_tail(&req->list, &ctx->free_req_list);
  3691. } while (req->request_id < ctx_isp->last_applied_req_id);
  3692. if (ctx_isp->offline_context)
  3693. goto exit;
  3694. error = CRM_KMD_ERR_FATAL;
  3695. if (req_isp_to_report && req_isp_to_report->bubble_report)
  3696. if (error_event_data->recovery_enabled)
  3697. error = CRM_KMD_ERR_BUBBLE;
  3698. __cam_isp_ctx_notify_error_util(CAM_TRIGGER_POINT_SOF, error,
  3699. error_request_id, ctx_isp);
  3700. /*
  3701. * Need to send error occurred in KMD
  3702. * This will help UMD to take necessary action
  3703. * and to dump relevant info
  3704. */
  3705. if (error == CRM_KMD_ERR_FATAL)
  3706. __cam_isp_ctx_notify_v4l2_error_event(recovery_type,
  3707. req_mgr_err_code, error_request_id, ctx);
  3708. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_HW_ERROR;
  3709. CAM_DBG(CAM_ISP, "Handling error done on ctx: %u, link: 0x%x", ctx->ctx_id, ctx->link_hdl);
  3710. exit:
  3711. return rc;
  3712. }
  3713. static int __cam_isp_ctx_fs2_sof_in_sof_state(
  3714. struct cam_isp_context *ctx_isp, void *evt_data)
  3715. {
  3716. int rc = 0;
  3717. struct cam_isp_hw_sof_event_data *sof_event_data = evt_data;
  3718. struct cam_ctx_request *req;
  3719. struct cam_context *ctx = ctx_isp->base;
  3720. uint64_t request_id = 0;
  3721. if (!evt_data) {
  3722. CAM_ERR(CAM_ISP, "in valid sof event data, ctx: %u, link: 0x%x",
  3723. ctx->ctx_id, ctx->link_hdl);
  3724. return -EINVAL;
  3725. }
  3726. __cam_isp_ctx_update_sof_ts_util(sof_event_data, ctx_isp);
  3727. CAM_DBG(CAM_ISP, "frame id: %lld time stamp:0x%llx, ctx: %u, link: 0x%x",
  3728. ctx_isp->frame_id, ctx_isp->sof_timestamp_val, ctx->ctx_id, ctx->link_hdl);
  3729. if (!(list_empty(&ctx->wait_req_list)))
  3730. goto end;
  3731. if (ctx_isp->active_req_cnt <= 2) {
  3732. __cam_isp_ctx_notify_trigger_util(CAM_TRIGGER_POINT_SOF, ctx_isp);
  3733. list_for_each_entry(req, &ctx->active_req_list, list) {
  3734. if (req->request_id > ctx_isp->reported_req_id) {
  3735. request_id = req->request_id;
  3736. ctx_isp->reported_req_id = request_id;
  3737. break;
  3738. }
  3739. }
  3740. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  3741. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  3742. }
  3743. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  3744. CAM_ISP_STATE_CHANGE_TRIGGER_SOF, request_id);
  3745. end:
  3746. return rc;
  3747. }
  3748. static int __cam_isp_ctx_fs2_buf_done(struct cam_isp_context *ctx_isp,
  3749. void *evt_data)
  3750. {
  3751. int rc = 0;
  3752. struct cam_isp_hw_done_event_data *done =
  3753. (struct cam_isp_hw_done_event_data *) evt_data;
  3754. struct cam_context *ctx = ctx_isp->base;
  3755. int prev_active_req_cnt = 0;
  3756. int curr_req_id = 0;
  3757. struct cam_ctx_request *req;
  3758. prev_active_req_cnt = ctx_isp->active_req_cnt;
  3759. req = list_first_entry(&ctx->active_req_list,
  3760. struct cam_ctx_request, list);
  3761. if (req)
  3762. curr_req_id = req->request_id;
  3763. rc = __cam_isp_ctx_handle_buf_done_in_activated_state(ctx_isp, done, 0);
  3764. if (prev_active_req_cnt == ctx_isp->active_req_cnt + 1) {
  3765. if (list_empty(&ctx->wait_req_list) &&
  3766. list_empty(&ctx->active_req_list)) {
  3767. CAM_DBG(CAM_ISP, "No request, move to SOF, ctx_idx: %u, link: 0x%x",
  3768. ctx->ctx_id, ctx->link_hdl);
  3769. ctx_isp->substate_activated =
  3770. CAM_ISP_CTX_ACTIVATED_SOF;
  3771. if (ctx_isp->reported_req_id < curr_req_id) {
  3772. ctx_isp->reported_req_id = curr_req_id;
  3773. __cam_isp_ctx_send_sof_timestamp(ctx_isp,
  3774. curr_req_id,
  3775. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  3776. }
  3777. }
  3778. }
  3779. return rc;
  3780. }
  3781. static int __cam_isp_ctx_fs2_buf_done_in_epoch(struct cam_isp_context *ctx_isp,
  3782. void *evt_data)
  3783. {
  3784. int rc = 0;
  3785. rc = __cam_isp_ctx_fs2_buf_done(ctx_isp, evt_data);
  3786. return rc;
  3787. }
  3788. static int __cam_isp_ctx_fs2_buf_done_in_applied(
  3789. struct cam_isp_context *ctx_isp,
  3790. void *evt_data)
  3791. {
  3792. int rc = 0;
  3793. rc = __cam_isp_ctx_fs2_buf_done(ctx_isp, evt_data);
  3794. return rc;
  3795. }
  3796. static int __cam_isp_ctx_fs2_reg_upd_in_sof(struct cam_isp_context *ctx_isp,
  3797. void *evt_data)
  3798. {
  3799. int rc = 0;
  3800. struct cam_ctx_request *req = NULL;
  3801. struct cam_isp_ctx_req *req_isp;
  3802. struct cam_context *ctx = ctx_isp->base;
  3803. if (ctx->state != CAM_CTX_ACTIVATED && ctx_isp->frame_id > 1) {
  3804. CAM_DBG(CAM_ISP, "invalid RUP");
  3805. goto end;
  3806. }
  3807. /*
  3808. * This is for the first update. The initial setting will
  3809. * cause the reg_upd in the first frame.
  3810. */
  3811. if (!list_empty(&ctx->wait_req_list)) {
  3812. req = list_first_entry(&ctx->wait_req_list,
  3813. struct cam_ctx_request, list);
  3814. list_del_init(&req->list);
  3815. cam_smmu_buffer_tracker_putref(&req->buf_tracker);
  3816. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  3817. if (req_isp->num_fence_map_out == req_isp->num_acked)
  3818. list_add_tail(&req->list, &ctx->free_req_list);
  3819. else
  3820. CAM_ERR(CAM_ISP,
  3821. "receive rup in unexpected state, ctx_idx: %u, link: 0x%x",
  3822. ctx->ctx_id, ctx->link_hdl);
  3823. }
  3824. if (req != NULL) {
  3825. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  3826. CAM_ISP_STATE_CHANGE_TRIGGER_REG_UPDATE,
  3827. req->request_id);
  3828. }
  3829. end:
  3830. return rc;
  3831. }
  3832. static int __cam_isp_ctx_fs2_reg_upd_in_applied_state(
  3833. struct cam_isp_context *ctx_isp, void *evt_data)
  3834. {
  3835. int rc = 0;
  3836. struct cam_ctx_request *req = NULL;
  3837. struct cam_context *ctx = ctx_isp->base;
  3838. struct cam_isp_ctx_req *req_isp;
  3839. uint64_t request_id = 0;
  3840. if (list_empty(&ctx->wait_req_list)) {
  3841. CAM_ERR(CAM_ISP, "Reg upd ack with no waiting request, ctx_idx: %u, link: 0x%x",
  3842. ctx->ctx_id, ctx->link_hdl);
  3843. goto end;
  3844. }
  3845. req = list_first_entry(&ctx->wait_req_list,
  3846. struct cam_ctx_request, list);
  3847. list_del_init(&req->list);
  3848. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  3849. if (req_isp->num_fence_map_out != 0) {
  3850. list_add_tail(&req->list, &ctx->active_req_list);
  3851. ctx_isp->active_req_cnt++;
  3852. CAM_DBG(CAM_REQ, "move request %lld to active list(cnt = %d), ctx:%u,link:0x%x",
  3853. req->request_id, ctx_isp->active_req_cnt, ctx->ctx_id, ctx->link_hdl);
  3854. } else {
  3855. cam_smmu_buffer_tracker_putref(&req->buf_tracker);
  3856. /* no io config, so the request is completed. */
  3857. list_add_tail(&req->list, &ctx->free_req_list);
  3858. }
  3859. /*
  3860. * This function only called directly from applied and bubble applied
  3861. * state so change substate here.
  3862. */
  3863. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_EPOCH;
  3864. if (req_isp->num_fence_map_out != 1)
  3865. goto end;
  3866. if (ctx_isp->active_req_cnt <= 2) {
  3867. list_for_each_entry(req, &ctx->active_req_list, list) {
  3868. if (req->request_id > ctx_isp->reported_req_id) {
  3869. request_id = req->request_id;
  3870. ctx_isp->reported_req_id = request_id;
  3871. break;
  3872. }
  3873. }
  3874. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  3875. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  3876. __cam_isp_ctx_notify_trigger_util(CAM_TRIGGER_POINT_SOF, ctx_isp);
  3877. }
  3878. CAM_DBG(CAM_ISP, "next Substate[%s], ctx_idx: %u, link: 0x%x",
  3879. __cam_isp_ctx_substate_val_to_type(ctx_isp->substate_activated),
  3880. ctx->ctx_id, ctx->link_hdl);
  3881. end:
  3882. if (req != NULL && !rc) {
  3883. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  3884. CAM_ISP_STATE_CHANGE_TRIGGER_REG_UPDATE,
  3885. req->request_id);
  3886. }
  3887. return rc;
  3888. }
  3889. static void __cam_isp_ctx_notify_aeb_error_for_sec_event(
  3890. struct cam_isp_context *ctx_isp)
  3891. {
  3892. struct cam_context *ctx = ctx_isp->base;
  3893. if ((++ctx_isp->aeb_error_cnt) <= CAM_ISP_CONTEXT_AEB_ERROR_CNT_MAX) {
  3894. CAM_WARN(CAM_ISP,
  3895. "AEB slave RDI's current request's SOF seen after next req is applied for ctx: %u on link: 0x%x last_applied_req: %llu err_cnt: %u",
  3896. ctx->ctx_id, ctx->link_hdl, ctx_isp->last_applied_req_id, ctx_isp->aeb_error_cnt);
  3897. return;
  3898. }
  3899. CAM_ERR(CAM_ISP,
  3900. "Fatal - AEB slave RDI's current request's SOF seen after next req is applied, EPOCH height need to be re-configured for ctx: %u on link: 0x%x err_cnt: %u",
  3901. ctx->ctx_id, ctx->link_hdl, ctx_isp->aeb_error_cnt);
  3902. /* Pause CRM timer */
  3903. if (!ctx_isp->offline_context)
  3904. __cam_isp_ctx_pause_crm_timer(ctx);
  3905. /* Trigger reg dump */
  3906. __cam_isp_ctx_trigger_reg_dump(CAM_HW_MGR_CMD_REG_DUMP_ON_ERROR, ctx);
  3907. /* Notify CRM on fatal error */
  3908. __cam_isp_ctx_notify_error_util(CAM_TRIGGER_POINT_SOF, CRM_KMD_ERR_FATAL,
  3909. ctx_isp->last_applied_req_id, ctx_isp);
  3910. /* Notify userland on error */
  3911. __cam_isp_ctx_notify_v4l2_error_event(CAM_REQ_MGR_ERROR_TYPE_RECOVERY,
  3912. CAM_REQ_MGR_CSID_ERR_ON_SENSOR_SWITCHING, ctx_isp->last_applied_req_id, ctx);
  3913. /* Change state to HALT, stop further processing of HW events */
  3914. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_HALT;
  3915. /* Dump AEB debug info */
  3916. __cam_isp_ctx_dump_frame_timing_record(ctx_isp);
  3917. }
  3918. static int __cam_isp_ctx_trigger_internal_recovery(
  3919. bool sync_frame_drop, struct cam_isp_context *ctx_isp)
  3920. {
  3921. int rc = 0;
  3922. bool do_recovery = true;
  3923. struct cam_context *ctx = ctx_isp->base;
  3924. struct cam_ctx_request *req = NULL;
  3925. struct cam_isp_ctx_req *req_isp = NULL;
  3926. if (list_empty(&ctx->wait_req_list)) {
  3927. /*
  3928. * If the wait list is empty, and we encounter a "silent" frame drop
  3929. * then the settings applied on the previous frame, did not reflect
  3930. * at the next frame boundary, it's expected to latch a frame after.
  3931. * No need to recover. If it's an out of sync drop use pending req
  3932. */
  3933. if (sync_frame_drop && !list_empty(&ctx->pending_req_list))
  3934. req = list_first_entry(&ctx->pending_req_list,
  3935. struct cam_ctx_request, list);
  3936. else
  3937. do_recovery = false;
  3938. }
  3939. /* If both wait and pending list have no request to recover on */
  3940. if (!do_recovery) {
  3941. CAM_WARN(CAM_ISP,
  3942. "No request to perform recovery - ctx: %u on link: 0x%x last_applied: %lld last_buf_done: %lld",
  3943. ctx->ctx_id, ctx->link_hdl, ctx_isp->last_applied_req_id,
  3944. ctx_isp->req_info.last_bufdone_req_id);
  3945. goto end;
  3946. }
  3947. if (!req) {
  3948. req = list_first_entry(&ctx->wait_req_list, struct cam_ctx_request, list);
  3949. if (req->request_id != ctx_isp->last_applied_req_id)
  3950. CAM_WARN(CAM_ISP,
  3951. "Top of wait list req: %llu does not match with last applied: %llu in ctx: %u on link: 0x%x",
  3952. req->request_id, ctx_isp->last_applied_req_id,
  3953. ctx->ctx_id, ctx->link_hdl);
  3954. }
  3955. req_isp = (struct cam_isp_ctx_req *)req->req_priv;
  3956. /*
  3957. * Treat this as bubble, after recovery re-start from appropriate sub-state
  3958. * This will block servicing any further apply calls from CRM
  3959. */
  3960. atomic_set(&ctx_isp->internal_recovery_set, 1);
  3961. atomic_set(&ctx_isp->process_bubble, 1);
  3962. ctx_isp->recovery_req_id = req->request_id;
  3963. /* Wait for active request's to finish before issuing recovery */
  3964. if (ctx_isp->active_req_cnt) {
  3965. req_isp->bubble_detected = true;
  3966. CAM_WARN(CAM_ISP,
  3967. "Active req cnt: %u wait for all buf dones before kicking in recovery on req: %lld ctx: %u on link: 0x%x",
  3968. ctx_isp->active_req_cnt, ctx_isp->recovery_req_id,
  3969. ctx->ctx_id, ctx->link_hdl);
  3970. } else {
  3971. rc = __cam_isp_ctx_notify_error_util(CAM_TRIGGER_POINT_SOF, CRM_KMD_ERR_BUBBLE,
  3972. ctx_isp->recovery_req_id, ctx_isp);
  3973. if (rc) {
  3974. /* Unable to do bubble recovery reset back to normal */
  3975. CAM_WARN(CAM_ISP,
  3976. "Unable to perform internal recovery [bubble reporting failed] for req: %llu in ctx: %u on link: 0x%x",
  3977. ctx_isp->recovery_req_id, ctx->ctx_id, ctx->link_hdl);
  3978. __cam_isp_context_reset_internal_recovery_params(ctx_isp);
  3979. goto end;
  3980. }
  3981. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_BUBBLE;
  3982. list_del_init(&req->list);
  3983. list_add(&req->list, &ctx->pending_req_list);
  3984. }
  3985. end:
  3986. return rc;
  3987. }
  3988. static int __cam_isp_ctx_handle_secondary_events(
  3989. struct cam_isp_context *ctx_isp, void *evt_data)
  3990. {
  3991. int rc = 0;
  3992. bool recover = false, sync_frame_drop = false;
  3993. struct cam_context *ctx = ctx_isp->base;
  3994. struct cam_isp_hw_secondary_event_data *sec_evt_data =
  3995. (struct cam_isp_hw_secondary_event_data *)evt_data;
  3996. /* Current scheme to handle only for custom AEB */
  3997. if (!ctx_isp->aeb_enabled) {
  3998. CAM_WARN(CAM_ISP,
  3999. "Recovery not supported for non-AEB ctx: %u on link: 0x%x reject sec evt: %u",
  4000. ctx->ctx_id, ctx->link_hdl, sec_evt_data->evt_type);
  4001. goto end;
  4002. }
  4003. if (atomic_read(&ctx_isp->internal_recovery_set)) {
  4004. CAM_WARN(CAM_ISP,
  4005. "Internal recovery in progress in ctx: %u on link: 0x%x reject sec evt: %u",
  4006. ctx->ctx_id, ctx->link_hdl, sec_evt_data->evt_type);
  4007. goto end;
  4008. }
  4009. /*
  4010. * In case of custom AEB ensure first exposure frame has
  4011. * not moved forward with its settings without second/third
  4012. * expoure frame coming in. Also track for bubble, in case of system
  4013. * delays it's possible for the IFE settings to be not written to
  4014. * HW on a given frame. If these scenarios occurs flag as error,
  4015. * and recover.
  4016. */
  4017. switch (sec_evt_data->evt_type) {
  4018. case CAM_ISP_HW_SEC_EVENT_SOF:
  4019. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  4020. CAM_ISP_STATE_CHANGE_TRIGGER_SEC_EVT_SOF,
  4021. ctx_isp->last_applied_req_id);
  4022. __cam_isp_ctx_update_frame_timing_record(CAM_ISP_HW_SECONDARY_EVENT, ctx_isp);
  4023. /* Slave RDI's frame starting post IFE EPOCH - Fatal */
  4024. if ((ctx_isp->substate_activated ==
  4025. CAM_ISP_CTX_ACTIVATED_APPLIED) ||
  4026. (ctx_isp->substate_activated ==
  4027. CAM_ISP_CTX_ACTIVATED_BUBBLE_APPLIED))
  4028. __cam_isp_ctx_notify_aeb_error_for_sec_event(ctx_isp);
  4029. else
  4030. /* Reset error count */
  4031. ctx_isp->aeb_error_cnt = 0;
  4032. break;
  4033. case CAM_ISP_HW_SEC_EVENT_EPOCH:
  4034. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  4035. CAM_ISP_STATE_CHANGE_TRIGGER_SEC_EVT_EPOCH,
  4036. ctx_isp->last_applied_req_id);
  4037. ctx_isp->out_of_sync_cnt = 0;
  4038. /*
  4039. * Master RDI frame dropped in CSID, due to programming delay no RUP/AUP
  4040. * On such occasions use CSID CAMIF EPOCH for bubble detection, flag
  4041. * on detection and perform necessary bubble recovery
  4042. */
  4043. if ((ctx_isp->substate_activated ==
  4044. CAM_ISP_CTX_ACTIVATED_APPLIED) ||
  4045. (ctx_isp->substate_activated ==
  4046. CAM_ISP_CTX_ACTIVATED_BUBBLE_APPLIED)) {
  4047. recover = true;
  4048. CAM_WARN(CAM_ISP,
  4049. "Programming delay input frame dropped ctx: %u on link: 0x%x last_applied_req: %llu, kicking in internal recovery....",
  4050. ctx->ctx_id, ctx->link_hdl, ctx_isp->last_applied_req_id);
  4051. }
  4052. break;
  4053. case CAM_ISP_HW_SEC_EVENT_OUT_OF_SYNC_FRAME_DROP:
  4054. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  4055. CAM_ISP_STATE_CHANGE_TRIGGER_FRAME_DROP,
  4056. ctx_isp->last_applied_req_id);
  4057. /* Avoid recovery loop if frame is dropped at stream on */
  4058. if (!ctx_isp->frame_id) {
  4059. CAM_ERR(CAM_ISP,
  4060. "Sensor sync [vc mismatch] frame dropped at stream on ctx: %u link: 0x%x frame_id: %u last_applied_req: %lld",
  4061. ctx->ctx_id, ctx->link_hdl,
  4062. ctx_isp->frame_id, ctx_isp->last_applied_req_id);
  4063. rc = -EPERM;
  4064. break;
  4065. }
  4066. if (!(ctx_isp->out_of_sync_cnt++) &&
  4067. (ctx_isp->recovery_req_id == ctx_isp->last_applied_req_id)) {
  4068. CAM_WARN(CAM_ISP,
  4069. "Sensor sync [vc mismatch] frame dropped ctx: %u on link: 0x%x last_applied_req: %llu last_recovered_req: %llu out_of_sync_cnt: %u, recovery maybe in progress...",
  4070. ctx->ctx_id, ctx->link_hdl, ctx_isp->last_applied_req_id,
  4071. ctx_isp->recovery_req_id, ctx_isp->out_of_sync_cnt);
  4072. break;
  4073. }
  4074. recover = true;
  4075. sync_frame_drop = true;
  4076. ctx_isp->out_of_sync_cnt = 0;
  4077. CAM_WARN(CAM_ISP,
  4078. "Sensor sync [vc mismatch] frame dropped ctx: %u on link: 0x%x last_applied_req: %llu last_recovered_req: %llu out_of_sync_cnt: %u, kicking in internal recovery....",
  4079. ctx->ctx_id, ctx->link_hdl, ctx_isp->last_applied_req_id,
  4080. ctx_isp->recovery_req_id, ctx_isp->out_of_sync_cnt);
  4081. break;
  4082. default:
  4083. break;
  4084. }
  4085. if (recover &&
  4086. !(isp_ctx_debug.disable_internal_recovery_mask & CAM_ISP_CTX_DISABLE_RECOVERY_AEB))
  4087. rc = __cam_isp_ctx_trigger_internal_recovery(sync_frame_drop, ctx_isp);
  4088. end:
  4089. return rc;
  4090. }
  4091. static struct cam_isp_ctx_irq_ops
  4092. cam_isp_ctx_activated_state_machine_irq[CAM_ISP_CTX_ACTIVATED_MAX] = {
  4093. /* SOF */
  4094. {
  4095. .irq_ops = {
  4096. __cam_isp_ctx_handle_error,
  4097. __cam_isp_ctx_sof_in_activated_state,
  4098. __cam_isp_ctx_reg_upd_in_sof,
  4099. __cam_isp_ctx_notify_sof_in_activated_state,
  4100. __cam_isp_ctx_notify_eof_in_activated_state,
  4101. __cam_isp_ctx_buf_done_in_sof,
  4102. __cam_isp_ctx_handle_secondary_events,
  4103. },
  4104. },
  4105. /* APPLIED */
  4106. {
  4107. .irq_ops = {
  4108. __cam_isp_ctx_handle_error,
  4109. __cam_isp_ctx_sof_in_activated_state,
  4110. __cam_isp_ctx_reg_upd_in_applied_state,
  4111. __cam_isp_ctx_epoch_in_applied,
  4112. __cam_isp_ctx_notify_eof_in_activated_state,
  4113. __cam_isp_ctx_buf_done_in_applied,
  4114. __cam_isp_ctx_handle_secondary_events,
  4115. },
  4116. },
  4117. /* EPOCH */
  4118. {
  4119. .irq_ops = {
  4120. __cam_isp_ctx_handle_error,
  4121. __cam_isp_ctx_sof_in_epoch,
  4122. __cam_isp_ctx_reg_upd_in_epoch_bubble_state,
  4123. __cam_isp_ctx_notify_sof_in_activated_state,
  4124. __cam_isp_ctx_notify_eof_in_activated_state,
  4125. __cam_isp_ctx_buf_done_in_epoch,
  4126. __cam_isp_ctx_handle_secondary_events,
  4127. },
  4128. },
  4129. /* BUBBLE */
  4130. {
  4131. .irq_ops = {
  4132. __cam_isp_ctx_handle_error,
  4133. __cam_isp_ctx_sof_in_activated_state,
  4134. __cam_isp_ctx_reg_upd_in_epoch_bubble_state,
  4135. __cam_isp_ctx_notify_sof_in_activated_state,
  4136. __cam_isp_ctx_notify_eof_in_activated_state,
  4137. __cam_isp_ctx_buf_done_in_bubble,
  4138. __cam_isp_ctx_handle_secondary_events,
  4139. },
  4140. },
  4141. /* Bubble Applied */
  4142. {
  4143. .irq_ops = {
  4144. __cam_isp_ctx_handle_error,
  4145. __cam_isp_ctx_sof_in_activated_state,
  4146. __cam_isp_ctx_reg_upd_in_applied_state,
  4147. __cam_isp_ctx_epoch_in_bubble_applied,
  4148. NULL,
  4149. __cam_isp_ctx_buf_done_in_bubble_applied,
  4150. __cam_isp_ctx_handle_secondary_events,
  4151. },
  4152. },
  4153. /* HW ERROR */
  4154. {
  4155. .irq_ops = {
  4156. NULL,
  4157. __cam_isp_ctx_sof_in_activated_state,
  4158. __cam_isp_ctx_reg_upd_in_hw_error,
  4159. NULL,
  4160. NULL,
  4161. NULL,
  4162. },
  4163. },
  4164. /* HALT */
  4165. {
  4166. },
  4167. };
  4168. static struct cam_isp_ctx_irq_ops
  4169. cam_isp_ctx_fs2_state_machine_irq[CAM_ISP_CTX_ACTIVATED_MAX] = {
  4170. /* SOF */
  4171. {
  4172. .irq_ops = {
  4173. __cam_isp_ctx_handle_error,
  4174. __cam_isp_ctx_fs2_sof_in_sof_state,
  4175. __cam_isp_ctx_fs2_reg_upd_in_sof,
  4176. __cam_isp_ctx_fs2_sof_in_sof_state,
  4177. __cam_isp_ctx_notify_eof_in_activated_state,
  4178. NULL,
  4179. },
  4180. },
  4181. /* APPLIED */
  4182. {
  4183. .irq_ops = {
  4184. __cam_isp_ctx_handle_error,
  4185. __cam_isp_ctx_sof_in_activated_state,
  4186. __cam_isp_ctx_fs2_reg_upd_in_applied_state,
  4187. __cam_isp_ctx_epoch_in_applied,
  4188. __cam_isp_ctx_notify_eof_in_activated_state,
  4189. __cam_isp_ctx_fs2_buf_done_in_applied,
  4190. },
  4191. },
  4192. /* EPOCH */
  4193. {
  4194. .irq_ops = {
  4195. __cam_isp_ctx_handle_error,
  4196. __cam_isp_ctx_sof_in_epoch,
  4197. __cam_isp_ctx_reg_upd_in_epoch_bubble_state,
  4198. __cam_isp_ctx_notify_sof_in_activated_state,
  4199. __cam_isp_ctx_notify_eof_in_activated_state,
  4200. __cam_isp_ctx_fs2_buf_done_in_epoch,
  4201. },
  4202. },
  4203. /* BUBBLE */
  4204. {
  4205. .irq_ops = {
  4206. __cam_isp_ctx_handle_error,
  4207. __cam_isp_ctx_sof_in_activated_state,
  4208. __cam_isp_ctx_reg_upd_in_epoch_bubble_state,
  4209. __cam_isp_ctx_notify_sof_in_activated_state,
  4210. __cam_isp_ctx_notify_eof_in_activated_state,
  4211. __cam_isp_ctx_buf_done_in_bubble,
  4212. },
  4213. },
  4214. /* Bubble Applied */
  4215. {
  4216. .irq_ops = {
  4217. __cam_isp_ctx_handle_error,
  4218. __cam_isp_ctx_sof_in_activated_state,
  4219. __cam_isp_ctx_reg_upd_in_applied_state,
  4220. __cam_isp_ctx_epoch_in_bubble_applied,
  4221. NULL,
  4222. __cam_isp_ctx_buf_done_in_bubble_applied,
  4223. },
  4224. },
  4225. /* HW ERROR */
  4226. {
  4227. .irq_ops = {
  4228. NULL,
  4229. __cam_isp_ctx_sof_in_activated_state,
  4230. __cam_isp_ctx_reg_upd_in_hw_error,
  4231. NULL,
  4232. NULL,
  4233. NULL,
  4234. },
  4235. },
  4236. /* HALT */
  4237. {
  4238. },
  4239. };
  4240. static struct cam_isp_ctx_irq_ops
  4241. cam_isp_ctx_offline_state_machine_irq[CAM_ISP_CTX_ACTIVATED_MAX] = {
  4242. /* SOF */
  4243. {
  4244. .irq_ops = {
  4245. __cam_isp_ctx_handle_error,
  4246. NULL,
  4247. NULL,
  4248. NULL,
  4249. NULL,
  4250. NULL,
  4251. },
  4252. },
  4253. /* APPLIED */
  4254. {
  4255. .irq_ops = {
  4256. __cam_isp_ctx_handle_error,
  4257. __cam_isp_ctx_sof_in_activated_state,
  4258. __cam_isp_ctx_reg_upd_in_applied_state,
  4259. __cam_isp_ctx_offline_epoch_in_activated_state,
  4260. NULL,
  4261. __cam_isp_ctx_buf_done_in_applied,
  4262. },
  4263. },
  4264. /* EPOCH */
  4265. {
  4266. .irq_ops = {
  4267. __cam_isp_ctx_handle_error,
  4268. __cam_isp_ctx_sof_in_activated_state,
  4269. NULL,
  4270. __cam_isp_ctx_offline_epoch_in_activated_state,
  4271. NULL,
  4272. __cam_isp_ctx_buf_done_in_epoch,
  4273. },
  4274. },
  4275. /* BUBBLE */
  4276. {
  4277. },
  4278. /* Bubble Applied */
  4279. {
  4280. },
  4281. /* HW ERROR */
  4282. {
  4283. .irq_ops = {
  4284. NULL,
  4285. __cam_isp_ctx_sof_in_activated_state,
  4286. __cam_isp_ctx_reg_upd_in_hw_error,
  4287. NULL,
  4288. NULL,
  4289. NULL,
  4290. },
  4291. },
  4292. /* HALT */
  4293. {
  4294. },
  4295. };
  4296. static inline int cam_isp_context_apply_evt_injection(struct cam_context *ctx)
  4297. {
  4298. struct cam_isp_context *ctx_isp = ctx->ctx_priv;
  4299. struct cam_hw_inject_evt_param *evt_inject_params = &ctx_isp->evt_inject_params;
  4300. struct cam_common_evt_inject_data inject_evt = {0};
  4301. int rc;
  4302. inject_evt.evt_params = evt_inject_params;
  4303. rc = cam_context_apply_evt_injection(ctx, &inject_evt);
  4304. if (rc)
  4305. CAM_ERR(CAM_ISP, "Fail to apply event injection ctx_id: %u link: 0x%x req_id: %u",
  4306. ctx->ctx_id, ctx->link_hdl, evt_inject_params->req_id);
  4307. evt_inject_params->is_valid = false;
  4308. return rc;
  4309. }
  4310. static inline void __cam_isp_ctx_update_fcg_prediction_idx(
  4311. struct cam_context *ctx,
  4312. uint64_t request_id,
  4313. struct cam_isp_fcg_prediction_tracker *fcg_tracker,
  4314. struct cam_isp_fcg_config_info *fcg_info)
  4315. {
  4316. struct cam_isp_context *ctx_isp = ctx->ctx_priv;
  4317. if ((fcg_tracker->sum_skipped == 0) ||
  4318. (fcg_tracker->sum_skipped > CAM_ISP_MAX_FCG_PREDICTIONS)) {
  4319. fcg_info->use_current_cfg = true;
  4320. CAM_DBG(CAM_ISP,
  4321. "Apply req: %llu, Use current FCG value, frame_id: %llu, ctx_id: %u",
  4322. request_id, ctx_isp->frame_id, ctx->ctx_id);
  4323. } else {
  4324. fcg_info->prediction_idx = fcg_tracker->sum_skipped;
  4325. CAM_DBG(CAM_ISP,
  4326. "Apply req: %llu, FCG prediction: %u, frame_id: %llu, ctx_id: %u",
  4327. request_id, fcg_tracker->sum_skipped,
  4328. ctx_isp->frame_id, ctx->ctx_id);
  4329. }
  4330. }
  4331. static inline void __cam_isp_ctx_print_fcg_tracker(
  4332. struct cam_isp_fcg_prediction_tracker *fcg_tracker)
  4333. {
  4334. uint32_t skipped_list[CAM_ISP_AFD_PIPELINE_DELAY];
  4335. struct cam_isp_skip_frame_info *skip_info;
  4336. int i = 0;
  4337. list_for_each_entry(skip_info,
  4338. &fcg_tracker->skipped_list, list) {
  4339. skipped_list[i] = skip_info->num_frame_skipped;
  4340. i += 1;
  4341. }
  4342. CAM_DBG(CAM_ISP,
  4343. "FCG tracker num_skipped: %u, sum_skipped: %u, skipped list: [%u, %u, %u]",
  4344. fcg_tracker->num_skipped, fcg_tracker->sum_skipped,
  4345. skipped_list[0], skipped_list[1], skipped_list[2]);
  4346. }
  4347. static int __cam_isp_ctx_apply_req_in_activated_state(
  4348. struct cam_context *ctx, struct cam_req_mgr_apply_request *apply,
  4349. enum cam_isp_ctx_activated_substate next_state)
  4350. {
  4351. int rc = 0;
  4352. struct cam_ctx_request *req;
  4353. struct cam_ctx_request *active_req = NULL;
  4354. struct cam_isp_ctx_req *req_isp;
  4355. struct cam_isp_ctx_req *active_req_isp;
  4356. struct cam_isp_context *ctx_isp = NULL;
  4357. struct cam_hw_config_args cfg = {0};
  4358. struct cam_isp_skip_frame_info *skip_info;
  4359. struct cam_isp_fcg_prediction_tracker *fcg_tracker;
  4360. struct cam_isp_fcg_config_info *fcg_info;
  4361. ctx_isp = (struct cam_isp_context *) ctx->ctx_priv;
  4362. /* Reset mswitch ref cnt */
  4363. atomic_set(&ctx_isp->mswitch_default_apply_delay_ref_cnt,
  4364. ctx_isp->mswitch_default_apply_delay_max_cnt);
  4365. if (apply->re_apply)
  4366. if (apply->request_id <= ctx_isp->last_applied_req_id) {
  4367. CAM_INFO_RATE_LIMIT(CAM_ISP,
  4368. "ctx_id:%u link: 0x%x Trying to reapply the same request %llu again",
  4369. ctx->ctx_id, ctx->link_hdl,
  4370. apply->request_id);
  4371. return 0;
  4372. }
  4373. if (list_empty(&ctx->pending_req_list)) {
  4374. CAM_ERR_RATE_LIMIT(CAM_ISP,
  4375. "ctx_id:%u link: 0x%x No available request for Apply id %lld",
  4376. ctx->ctx_id, ctx->link_hdl,
  4377. apply->request_id);
  4378. rc = -EFAULT;
  4379. goto end;
  4380. }
  4381. /*
  4382. * When the pipeline has issue, the requests can be queued up in the
  4383. * pipeline. In this case, we should reject the additional request.
  4384. * The maximum number of request allowed to be outstanding is 2.
  4385. *
  4386. */
  4387. if (atomic_read(&ctx_isp->process_bubble)) {
  4388. CAM_INFO_RATE_LIMIT(CAM_ISP,
  4389. "ctx_id:%u link: 0x%x Processing bubble cannot apply Request Id %llu",
  4390. ctx->ctx_id, ctx->link_hdl,
  4391. apply->request_id);
  4392. rc = -EFAULT;
  4393. goto end;
  4394. }
  4395. /*
  4396. * When isp processing internal recovery, the crm may still apply
  4397. * req to isp ctx. In this case, we should reject this req apply.
  4398. */
  4399. if (atomic_read(&ctx_isp->internal_recovery_set)) {
  4400. CAM_INFO_RATE_LIMIT(CAM_ISP,
  4401. "ctx_id:%u link: 0x%x Processing recovery cannot apply Request Id %lld",
  4402. ctx->ctx_id, ctx->link_hdl,
  4403. apply->request_id);
  4404. rc = -EAGAIN;
  4405. goto end;
  4406. }
  4407. spin_lock_bh(&ctx->lock);
  4408. req = list_first_entry(&ctx->pending_req_list, struct cam_ctx_request,
  4409. list);
  4410. spin_unlock_bh(&ctx->lock);
  4411. /*
  4412. * Check whether the request id is matching the tip, if not, this means
  4413. * we are in the middle of the error handling. Need to reject this apply
  4414. */
  4415. if (req->request_id != apply->request_id) {
  4416. CAM_ERR_RATE_LIMIT(CAM_ISP,
  4417. "ctx_id:%u link: 0x%x Invalid Request Id asking %llu existing %llu",
  4418. ctx->ctx_id, ctx->link_hdl,
  4419. apply->request_id, req->request_id);
  4420. rc = -EFAULT;
  4421. goto end;
  4422. }
  4423. CAM_DBG(CAM_REQ, "Apply request %lld in Substate[%s] ctx %u, link: 0x%x",
  4424. req->request_id,
  4425. __cam_isp_ctx_substate_val_to_type(ctx_isp->substate_activated),
  4426. ctx->ctx_id, ctx->link_hdl);
  4427. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  4428. if (ctx_isp->active_req_cnt >= 2) {
  4429. CAM_WARN_RATE_LIMIT(CAM_ISP,
  4430. "Reject apply request (id %lld) due to congestion(cnt = %d) ctx %u, link: 0x%x",
  4431. req->request_id,
  4432. ctx_isp->active_req_cnt,
  4433. ctx->ctx_id, ctx->link_hdl);
  4434. spin_lock_bh(&ctx->lock);
  4435. if (!list_empty(&ctx->active_req_list))
  4436. active_req = list_first_entry(&ctx->active_req_list,
  4437. struct cam_ctx_request, list);
  4438. else
  4439. CAM_ERR_RATE_LIMIT(CAM_ISP,
  4440. "WARNING: should not happen (cnt = %d) but active_list empty, ctx %u, link: 0x%x",
  4441. ctx_isp->active_req_cnt, ctx->ctx_id, ctx->link_hdl);
  4442. spin_unlock_bh(&ctx->lock);
  4443. if (active_req) {
  4444. active_req_isp =
  4445. (struct cam_isp_ctx_req *) active_req->req_priv;
  4446. __cam_isp_ctx_handle_buf_done_fail_log(ctx_isp,
  4447. active_req->request_id, active_req_isp);
  4448. }
  4449. rc = -EFAULT;
  4450. goto end;
  4451. }
  4452. /* Reset congestion counter */
  4453. ctx_isp->congestion_cnt = 0;
  4454. req_isp->bubble_report = apply->report_if_bubble;
  4455. /*
  4456. * Reset all buf done/bubble flags for the req being applied
  4457. * If internal recovery has led to re-apply of same
  4458. * request, clear all stale entities
  4459. */
  4460. req_isp->num_acked = 0;
  4461. req_isp->num_deferred_acks = 0;
  4462. req_isp->cdm_reset_before_apply = false;
  4463. req_isp->bubble_detected = false;
  4464. cfg.ctxt_to_hw_map = ctx_isp->hw_ctx;
  4465. cfg.request_id = req->request_id;
  4466. cfg.hw_update_entries = req_isp->cfg;
  4467. cfg.num_hw_update_entries = req_isp->num_cfg;
  4468. cfg.priv = &req_isp->hw_update_data;
  4469. cfg.init_packet = 0;
  4470. cfg.reapply_type = req_isp->reapply_type;
  4471. cfg.cdm_reset_before_apply = req_isp->cdm_reset_before_apply;
  4472. if ((ctx_isp->evt_inject_params.is_valid) &&
  4473. (req->request_id == ctx_isp->evt_inject_params.req_id)) {
  4474. rc = cam_isp_context_apply_evt_injection(ctx_isp->base);
  4475. if (!rc)
  4476. goto end;
  4477. }
  4478. /* Decide the exact FCG prediction */
  4479. fcg_tracker = &ctx_isp->fcg_tracker;
  4480. fcg_info = &req_isp->hw_update_data.fcg_info;
  4481. if (!list_empty(&fcg_tracker->skipped_list)) {
  4482. __cam_isp_ctx_print_fcg_tracker(fcg_tracker);
  4483. skip_info = list_first_entry(&fcg_tracker->skipped_list,
  4484. struct cam_isp_skip_frame_info, list);
  4485. fcg_tracker->sum_skipped -= skip_info->num_frame_skipped;
  4486. if (unlikely((uint32_t)UINT_MAX - fcg_tracker->sum_skipped <
  4487. fcg_tracker->num_skipped))
  4488. fcg_tracker->num_skipped =
  4489. (uint32_t)UINT_MAX - fcg_tracker->sum_skipped;
  4490. fcg_tracker->sum_skipped += fcg_tracker->num_skipped;
  4491. skip_info->num_frame_skipped = fcg_tracker->num_skipped;
  4492. fcg_tracker->num_skipped = 0;
  4493. list_rotate_left(&fcg_tracker->skipped_list);
  4494. __cam_isp_ctx_print_fcg_tracker(fcg_tracker);
  4495. __cam_isp_ctx_update_fcg_prediction_idx(ctx,
  4496. apply->request_id, fcg_tracker, fcg_info);
  4497. }
  4498. atomic_set(&ctx_isp->apply_in_progress, 1);
  4499. rc = ctx->hw_mgr_intf->hw_config(ctx->hw_mgr_intf->hw_mgr_priv, &cfg);
  4500. if (!rc) {
  4501. spin_lock_bh(&ctx->lock);
  4502. ctx_isp->substate_activated = next_state;
  4503. ctx_isp->last_applied_req_id = apply->request_id;
  4504. ctx_isp->last_applied_jiffies = jiffies;
  4505. if (ctx_isp->is_tfe_shdr) {
  4506. if (ctx_isp->is_shdr_master && req_isp->hw_update_data.mup_en)
  4507. apply->dual_trigger_status = req_isp->hw_update_data.num_exp;
  4508. else
  4509. apply->dual_trigger_status = CAM_REQ_DUAL_TRIGGER_NONE;
  4510. } else {
  4511. apply->dual_trigger_status = CAM_REQ_DUAL_TRIGGER_NONE;
  4512. }
  4513. list_del_init(&req->list);
  4514. if (atomic_read(&ctx_isp->internal_recovery_set))
  4515. __cam_isp_ctx_enqueue_request_in_order(ctx, req, false);
  4516. else
  4517. list_add_tail(&req->list, &ctx->wait_req_list);
  4518. CAM_DBG(CAM_ISP, "new Substate[%s], applied req %lld, ctx_idx: %u, link: 0x%x",
  4519. __cam_isp_ctx_substate_val_to_type(next_state),
  4520. ctx_isp->last_applied_req_id, ctx->ctx_id, ctx->link_hdl);
  4521. spin_unlock_bh(&ctx->lock);
  4522. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  4523. CAM_ISP_STATE_CHANGE_TRIGGER_APPLIED,
  4524. req->request_id);
  4525. __cam_isp_ctx_update_event_record(ctx_isp,
  4526. CAM_ISP_CTX_EVENT_APPLY, req, NULL);
  4527. } else if (rc == -EALREADY) {
  4528. spin_lock_bh(&ctx->lock);
  4529. req_isp->bubble_detected = true;
  4530. req_isp->cdm_reset_before_apply = false;
  4531. atomic_set(&ctx_isp->process_bubble, 1);
  4532. list_del_init(&req->list);
  4533. list_add(&req->list, &ctx->active_req_list);
  4534. ctx_isp->active_req_cnt++;
  4535. spin_unlock_bh(&ctx->lock);
  4536. CAM_DBG(CAM_REQ,
  4537. "move request %lld to active list(cnt = %d), ctx %u, link: 0x%x",
  4538. req->request_id, ctx_isp->active_req_cnt, ctx->ctx_id, ctx->link_hdl);
  4539. } else {
  4540. CAM_ERR_RATE_LIMIT(CAM_ISP,
  4541. "ctx_id:%u link: 0x%x,Can not apply (req %lld) the configuration, rc %d",
  4542. ctx->ctx_id, ctx->link_hdl, apply->request_id, rc);
  4543. }
  4544. atomic_set(&ctx_isp->apply_in_progress, 0);
  4545. end:
  4546. return rc;
  4547. }
  4548. static int __cam_isp_ctx_apply_req_in_sof(
  4549. struct cam_context *ctx, struct cam_req_mgr_apply_request *apply)
  4550. {
  4551. int rc = 0;
  4552. struct cam_isp_context *ctx_isp =
  4553. (struct cam_isp_context *) ctx->ctx_priv;
  4554. CAM_DBG(CAM_ISP, "current Substate[%s], ctx %u, link: 0x%x",
  4555. __cam_isp_ctx_substate_val_to_type(
  4556. ctx_isp->substate_activated), ctx->ctx_id, ctx->link_hdl);
  4557. rc = __cam_isp_ctx_apply_req_in_activated_state(ctx, apply,
  4558. CAM_ISP_CTX_ACTIVATED_APPLIED);
  4559. CAM_DBG(CAM_ISP, "new Substate[%s], ctx %u, link: 0x%x",
  4560. __cam_isp_ctx_substate_val_to_type(
  4561. ctx_isp->substate_activated), ctx->ctx_id, ctx->link_hdl);
  4562. if (rc)
  4563. CAM_DBG(CAM_ISP, "Apply failed in Substate[%s], rc %d, ctx %u, link: 0x%x",
  4564. __cam_isp_ctx_substate_val_to_type(
  4565. ctx_isp->substate_activated), rc, ctx->ctx_id, ctx->link_hdl);
  4566. return rc;
  4567. }
  4568. static int __cam_isp_ctx_apply_req_in_epoch(
  4569. struct cam_context *ctx, struct cam_req_mgr_apply_request *apply)
  4570. {
  4571. int rc = 0;
  4572. struct cam_isp_context *ctx_isp =
  4573. (struct cam_isp_context *) ctx->ctx_priv;
  4574. CAM_DBG(CAM_ISP, "current Substate[%s], ctx %u, link: 0x%x",
  4575. __cam_isp_ctx_substate_val_to_type(
  4576. ctx_isp->substate_activated), ctx->ctx_id, ctx->link_hdl);
  4577. rc = __cam_isp_ctx_apply_req_in_activated_state(ctx, apply,
  4578. CAM_ISP_CTX_ACTIVATED_APPLIED);
  4579. CAM_DBG(CAM_ISP, "new Substate[%s], ctx %u, link: 0x%x",
  4580. __cam_isp_ctx_substate_val_to_type(
  4581. ctx_isp->substate_activated), ctx->ctx_id, ctx->link_hdl);
  4582. if (rc)
  4583. CAM_DBG(CAM_ISP, "Apply failed in Substate[%s], rc %d, ctx %u, link: 0x%x",
  4584. __cam_isp_ctx_substate_val_to_type(
  4585. ctx_isp->substate_activated), rc, ctx->ctx_id, ctx->link_hdl);
  4586. return rc;
  4587. }
  4588. static int __cam_isp_ctx_apply_req_in_bubble(
  4589. struct cam_context *ctx, struct cam_req_mgr_apply_request *apply)
  4590. {
  4591. int rc = 0;
  4592. struct cam_isp_context *ctx_isp =
  4593. (struct cam_isp_context *) ctx->ctx_priv;
  4594. CAM_DBG(CAM_ISP, "current Substate[%s], ctx %u, link: 0x%x",
  4595. __cam_isp_ctx_substate_val_to_type(
  4596. ctx_isp->substate_activated), ctx->ctx_id, ctx->link_hdl);
  4597. rc = __cam_isp_ctx_apply_req_in_activated_state(ctx, apply,
  4598. CAM_ISP_CTX_ACTIVATED_BUBBLE_APPLIED);
  4599. CAM_DBG(CAM_ISP, "new Substate[%s], ctx %u, link: 0x%x",
  4600. __cam_isp_ctx_substate_val_to_type(
  4601. ctx_isp->substate_activated), ctx->ctx_id, ctx->link_hdl);
  4602. if (rc)
  4603. CAM_DBG(CAM_ISP, "Apply failed in Substate[%s], rc %d, ctx %u, link: 0x%x",
  4604. __cam_isp_ctx_substate_val_to_type(
  4605. ctx_isp->substate_activated), rc, ctx->ctx_id, ctx->link_hdl);
  4606. return rc;
  4607. }
  4608. static void __cam_isp_ctx_find_mup_for_default_settings(
  4609. int64_t req_id, struct cam_context *ctx,
  4610. struct cam_ctx_request **switch_req)
  4611. {
  4612. struct cam_ctx_request *req, *temp_req;
  4613. if (list_empty(&ctx->pending_req_list)) {
  4614. CAM_DBG(CAM_ISP,
  4615. "Pending list empty, unable to find mup for req: %lld ctx: %u",
  4616. req_id, ctx->ctx_id);
  4617. return;
  4618. }
  4619. list_for_each_entry_safe(
  4620. req, temp_req, &ctx->pending_req_list, list) {
  4621. if (req->request_id == req_id) {
  4622. struct cam_isp_ctx_req *req_isp = (struct cam_isp_ctx_req *)req->req_priv;
  4623. if (req_isp->hw_update_data.mup_en) {
  4624. *switch_req = req;
  4625. CAM_DBG(CAM_ISP,
  4626. "Found mup for last applied max pd req: %lld in ctx: %u",
  4627. req_id, ctx->ctx_id);
  4628. }
  4629. }
  4630. }
  4631. }
  4632. static int __cam_isp_ctx_apply_default_req_settings(
  4633. struct cam_context *ctx, struct cam_req_mgr_apply_request *apply)
  4634. {
  4635. int rc = 0;
  4636. bool skip_rup_aup = false;
  4637. struct cam_ctx_request *req = NULL;
  4638. struct cam_isp_ctx_req *req_isp = NULL;
  4639. struct cam_isp_context *isp_ctx =
  4640. (struct cam_isp_context *) ctx->ctx_priv;
  4641. struct cam_hw_cmd_args hw_cmd_args;
  4642. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  4643. struct cam_hw_config_args cfg = {0};
  4644. if (isp_ctx->mode_switch_en && isp_ctx->handle_mswitch) {
  4645. if ((apply->last_applied_max_pd_req > 0) &&
  4646. (atomic_dec_and_test(&isp_ctx->mswitch_default_apply_delay_ref_cnt))) {
  4647. __cam_isp_ctx_find_mup_for_default_settings(
  4648. apply->last_applied_max_pd_req, ctx, &req);
  4649. }
  4650. if (req) {
  4651. req_isp = (struct cam_isp_ctx_req *)req->req_priv;
  4652. CAM_DBG(CAM_ISP,
  4653. "Applying IQ for mode switch req: %lld ctx: %u",
  4654. req->request_id, ctx->ctx_id);
  4655. cfg.ctxt_to_hw_map = isp_ctx->hw_ctx;
  4656. cfg.request_id = req->request_id;
  4657. cfg.hw_update_entries = req_isp->cfg;
  4658. cfg.num_hw_update_entries = req_isp->num_cfg;
  4659. cfg.priv = &req_isp->hw_update_data;
  4660. cfg.init_packet = 0;
  4661. cfg.reapply_type = CAM_CONFIG_REAPPLY_IQ;
  4662. cfg.cdm_reset_before_apply = req_isp->cdm_reset_before_apply;
  4663. rc = ctx->hw_mgr_intf->hw_config(ctx->hw_mgr_intf->hw_mgr_priv, &cfg);
  4664. if (rc) {
  4665. CAM_ERR(CAM_ISP,
  4666. "Failed to apply req: %lld IQ settings in ctx: %u",
  4667. req->request_id, ctx->ctx_id);
  4668. goto end;
  4669. }
  4670. skip_rup_aup = true;
  4671. }
  4672. }
  4673. if (isp_ctx->use_default_apply) {
  4674. hw_cmd_args.ctxt_to_hw_map = isp_ctx->hw_ctx;
  4675. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  4676. isp_hw_cmd_args.cmd_type =
  4677. CAM_ISP_HW_MGR_CMD_PROG_DEFAULT_CFG;
  4678. isp_hw_cmd_args.cmd_data = (void *)&skip_rup_aup;
  4679. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  4680. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  4681. &hw_cmd_args);
  4682. if (rc)
  4683. CAM_ERR(CAM_ISP,
  4684. "Failed to apply default settings rc %d ctx %u, link: 0x%x",
  4685. rc, ctx->ctx_id, ctx->link_hdl);
  4686. else
  4687. CAM_DBG(CAM_ISP, "Applied default settings rc %d ctx: %u link: 0x%x",
  4688. rc, ctx->ctx_id, ctx->link_hdl);
  4689. }
  4690. end:
  4691. return rc;
  4692. }
  4693. static void *cam_isp_ctx_user_dump_req_list(
  4694. void *dump_struct, uint8_t *addr_ptr)
  4695. {
  4696. struct list_head *head = NULL;
  4697. uint64_t *addr;
  4698. struct cam_ctx_request *req, *req_temp;
  4699. head = (struct list_head *)dump_struct;
  4700. addr = (uint64_t *)addr_ptr;
  4701. if (!list_empty(head)) {
  4702. list_for_each_entry_safe(req, req_temp, head, list) {
  4703. *addr++ = req->request_id;
  4704. }
  4705. }
  4706. return addr;
  4707. }
  4708. static void *cam_isp_ctx_user_dump_active_requests(
  4709. void *dump_struct, uint8_t *addr_ptr)
  4710. {
  4711. uint64_t *addr;
  4712. struct cam_ctx_request *req;
  4713. req = (struct cam_ctx_request *)dump_struct;
  4714. addr = (uint64_t *)addr_ptr;
  4715. *addr++ = req->request_id;
  4716. return addr;
  4717. }
  4718. static int __cam_isp_ctx_dump_req_info(
  4719. struct cam_context *ctx,
  4720. struct cam_ctx_request *req,
  4721. struct cam_common_hw_dump_args *dump_args)
  4722. {
  4723. int i, rc = 0;
  4724. uint32_t min_len;
  4725. size_t remain_len;
  4726. struct cam_isp_ctx_req *req_isp;
  4727. struct cam_ctx_request *req_temp;
  4728. if (!req || !ctx || !dump_args) {
  4729. CAM_ERR(CAM_ISP, "Invalid parameters %pK %pK %pK",
  4730. req, ctx, dump_args);
  4731. return -EINVAL;
  4732. }
  4733. req_isp = (struct cam_isp_ctx_req *)req->req_priv;
  4734. if (dump_args->buf_len <= dump_args->offset) {
  4735. CAM_WARN(CAM_ISP,
  4736. "Dump buffer overshoot len %zu offset %zu, ctx_idx: %u, link: 0x%x",
  4737. dump_args->buf_len, dump_args->offset, ctx->ctx_id, ctx->link_hdl);
  4738. return -ENOSPC;
  4739. }
  4740. remain_len = dump_args->buf_len - dump_args->offset;
  4741. min_len = sizeof(struct cam_isp_context_dump_header) +
  4742. (CAM_ISP_CTX_DUMP_REQUEST_NUM_WORDS *
  4743. req_isp->num_fence_map_out *
  4744. sizeof(uint64_t));
  4745. if (remain_len < min_len) {
  4746. CAM_WARN(CAM_ISP, "Dump buffer exhaust remain %zu min %u, ctx_idx: %u, link: 0x%x",
  4747. remain_len, min_len, ctx->ctx_id, ctx->link_hdl);
  4748. return -ENOSPC;
  4749. }
  4750. /* Dump pending request list */
  4751. rc = cam_common_user_dump_helper(dump_args, cam_isp_ctx_user_dump_req_list,
  4752. &ctx->pending_req_list, sizeof(uint64_t), "ISP_OUT_FENCE_PENDING_REQUESTS:");
  4753. if (rc) {
  4754. CAM_ERR(CAM_ISP,
  4755. "CAM_ISP_CONTEXT:Pending request dump failed, rc:%d, ctx:%u, link:0x%x",
  4756. rc, ctx->ctx_id, ctx->link_hdl);
  4757. return rc;
  4758. }
  4759. /* Dump applied request list */
  4760. rc = cam_common_user_dump_helper(dump_args, cam_isp_ctx_user_dump_req_list,
  4761. &ctx->wait_req_list, sizeof(uint64_t), "ISP_OUT_FENCE_APPLIED_REQUESTS:");
  4762. if (rc) {
  4763. CAM_ERR(CAM_ISP,
  4764. "CAM_ISP_CONTEXT: Applied request dump failed, rc:%d, ctx:%u, link:0x%x",
  4765. rc, ctx->ctx_id, ctx->link_hdl);
  4766. return rc;
  4767. }
  4768. /* Dump active request list */
  4769. rc = cam_common_user_dump_helper(dump_args, cam_isp_ctx_user_dump_req_list,
  4770. &ctx->active_req_list, sizeof(uint64_t), "ISP_OUT_FENCE_ACTIVE_REQUESTS:");
  4771. if (rc) {
  4772. CAM_ERR(CAM_ISP,
  4773. "CAM_ISP_CONTEXT: Active request dump failed, rc:%d, ctx:%u, link:0x%x",
  4774. rc, ctx->ctx_id, ctx->link_hdl);
  4775. return rc;
  4776. }
  4777. /* Dump active request fences */
  4778. if (!list_empty(&ctx->active_req_list)) {
  4779. list_for_each_entry_safe(req, req_temp, &ctx->active_req_list, list) {
  4780. req_isp = (struct cam_isp_ctx_req *)req->req_priv;
  4781. for (i = 0; i < req_isp->num_fence_map_out; i++) {
  4782. rc = cam_common_user_dump_helper(dump_args,
  4783. cam_isp_ctx_user_dump_active_requests,
  4784. req, sizeof(uint64_t),
  4785. "ISP_OUT_FENCE_REQUEST_ACTIVE.%s.%u.%d:",
  4786. __cam_isp_ife_sfe_resource_handle_id_to_type(
  4787. req_isp->fence_map_out[i].resource_handle),
  4788. req_isp->fence_map_out[i].image_buf_addr[0],
  4789. req_isp->fence_map_out[i].sync_id);
  4790. if (rc) {
  4791. CAM_ERR(CAM_ISP,
  4792. "CAM_ISP_CONTEXT DUMP_REQ_INFO: Dump failed, rc: %d, ctx_idx: %u, link: 0x%x",
  4793. rc, ctx->ctx_id, ctx->link_hdl);
  4794. return rc;
  4795. }
  4796. }
  4797. }
  4798. }
  4799. return rc;
  4800. }
  4801. static void *cam_isp_ctx_user_dump_timer(
  4802. void *dump_struct, uint8_t *addr_ptr)
  4803. {
  4804. struct cam_ctx_request *req = NULL;
  4805. struct cam_isp_ctx_req *req_isp = NULL;
  4806. uint64_t *addr;
  4807. ktime_t cur_time;
  4808. req = (struct cam_ctx_request *)dump_struct;
  4809. req_isp = (struct cam_isp_ctx_req *)req->req_priv;
  4810. cur_time = ktime_get();
  4811. addr = (uint64_t *)addr_ptr;
  4812. *addr++ = req->request_id;
  4813. *addr++ = ktime_to_timespec64(
  4814. req_isp->event_timestamp[CAM_ISP_CTX_EVENT_APPLY]).tv_sec;
  4815. *addr++ = ktime_to_timespec64(
  4816. req_isp->event_timestamp[CAM_ISP_CTX_EVENT_APPLY]).tv_nsec / NSEC_PER_USEC;
  4817. *addr++ = ktime_to_timespec64(cur_time).tv_sec;
  4818. *addr++ = ktime_to_timespec64(cur_time).tv_nsec / NSEC_PER_USEC;
  4819. return addr;
  4820. }
  4821. static void *cam_isp_ctx_user_dump_stream_info(
  4822. void *dump_struct, uint8_t *addr_ptr)
  4823. {
  4824. struct cam_context *ctx = NULL;
  4825. int32_t *addr;
  4826. ctx = (struct cam_context *)dump_struct;
  4827. addr = (int32_t *)addr_ptr;
  4828. *addr++ = ctx->ctx_id;
  4829. *addr++ = ctx->dev_hdl;
  4830. *addr++ = ctx->link_hdl;
  4831. return addr;
  4832. }
  4833. static int __cam_isp_ctx_dump_in_top_state(
  4834. struct cam_context *ctx,
  4835. struct cam_req_mgr_dump_info *dump_info)
  4836. {
  4837. int rc = 0;
  4838. bool dump_only_event_record = false;
  4839. size_t buf_len;
  4840. size_t remain_len;
  4841. ktime_t cur_time;
  4842. uint32_t min_len;
  4843. uint64_t diff;
  4844. uintptr_t cpu_addr;
  4845. uint8_t req_type;
  4846. struct cam_isp_context *ctx_isp;
  4847. struct cam_ctx_request *req = NULL;
  4848. struct cam_isp_ctx_req *req_isp;
  4849. struct cam_ctx_request *req_temp;
  4850. struct cam_hw_dump_args ife_dump_args;
  4851. struct cam_common_hw_dump_args dump_args;
  4852. struct cam_hw_cmd_args hw_cmd_args;
  4853. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  4854. spin_lock_bh(&ctx->lock);
  4855. list_for_each_entry_safe(req, req_temp,
  4856. &ctx->active_req_list, list) {
  4857. if (req->request_id == dump_info->req_id) {
  4858. CAM_INFO(CAM_ISP, "isp dump active list req: %lld, ctx_idx: %u, link: 0x%x",
  4859. dump_info->req_id, ctx->ctx_id, ctx->link_hdl);
  4860. req_type = 'a';
  4861. goto hw_dump;
  4862. }
  4863. }
  4864. list_for_each_entry_safe(req, req_temp,
  4865. &ctx->wait_req_list, list) {
  4866. if (req->request_id == dump_info->req_id) {
  4867. CAM_INFO(CAM_ISP, "isp dump wait list req: %lld, ctx_idx: %u, link: 0x%x",
  4868. dump_info->req_id, ctx->ctx_id, ctx->link_hdl);
  4869. req_type = 'w';
  4870. goto hw_dump;
  4871. }
  4872. }
  4873. list_for_each_entry_safe(req, req_temp,
  4874. &ctx->pending_req_list, list) {
  4875. if (req->request_id == dump_info->req_id) {
  4876. CAM_INFO(CAM_ISP,
  4877. "isp dump pending list req: %lld, ctx_idx: %u, link: 0x%x",
  4878. dump_info->req_id, ctx->ctx_id, ctx->link_hdl);
  4879. req_type = 'p';
  4880. goto hw_dump;
  4881. }
  4882. }
  4883. goto end;
  4884. hw_dump:
  4885. rc = cam_mem_get_cpu_buf(dump_info->buf_handle,
  4886. &cpu_addr, &buf_len);
  4887. if (rc) {
  4888. CAM_ERR(CAM_ISP, "Invalid handle %u rc %d, ctx_idx: %u, link: 0x%x",
  4889. dump_info->buf_handle, rc, ctx->ctx_id, ctx->link_hdl);
  4890. spin_unlock_bh(&ctx->lock);
  4891. return rc;
  4892. }
  4893. if (buf_len <= dump_info->offset) {
  4894. spin_unlock_bh(&ctx->lock);
  4895. CAM_WARN(CAM_ISP,
  4896. "Dump buffer overshoot len %zu offset %zu, ctx_idx: %u, link: 0x%x",
  4897. buf_len, dump_info->offset, ctx->ctx_id, ctx->link_hdl);
  4898. cam_mem_put_cpu_buf(dump_info->buf_handle);
  4899. return -ENOSPC;
  4900. }
  4901. remain_len = buf_len - dump_info->offset;
  4902. min_len = sizeof(struct cam_isp_context_dump_header) +
  4903. (CAM_ISP_CTX_DUMP_NUM_WORDS * sizeof(uint64_t));
  4904. if (remain_len < min_len) {
  4905. spin_unlock_bh(&ctx->lock);
  4906. CAM_WARN(CAM_ISP,
  4907. "Dump buffer exhaust remain %zu min %u, ctx_idx: %u, link: 0x%x",
  4908. remain_len, min_len, ctx->ctx_id, ctx->link_hdl);
  4909. cam_mem_put_cpu_buf(dump_info->buf_handle);
  4910. return -ENOSPC;
  4911. }
  4912. ctx_isp = (struct cam_isp_context *) ctx->ctx_priv;
  4913. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  4914. cur_time = ktime_get();
  4915. diff = ktime_us_delta(
  4916. req_isp->event_timestamp[CAM_ISP_CTX_EVENT_APPLY],
  4917. cur_time);
  4918. __cam_isp_ctx_print_event_record(ctx_isp);
  4919. if (diff < CAM_ISP_CTX_RESPONSE_TIME_THRESHOLD) {
  4920. CAM_INFO(CAM_ISP, "req %lld found no error, ctx_idx: %u, link: 0x%x",
  4921. req->request_id, ctx->ctx_id, ctx->link_hdl);
  4922. dump_only_event_record = true;
  4923. }
  4924. dump_args.req_id = dump_info->req_id;
  4925. dump_args.cpu_addr = cpu_addr;
  4926. dump_args.buf_len = buf_len;
  4927. dump_args.offset = dump_info->offset;
  4928. dump_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  4929. /* Dump time info */
  4930. rc = cam_common_user_dump_helper(&dump_args, cam_isp_ctx_user_dump_timer,
  4931. req, sizeof(uint64_t), "ISP_CTX_DUMP.%c:", req_type);
  4932. if (rc) {
  4933. CAM_ERR(CAM_ISP, "Time dump fail %lld, rc: %d, ctx_idx: %u, link: 0x%x",
  4934. req->request_id, rc, ctx->ctx_id, ctx->link_hdl);
  4935. goto end;
  4936. }
  4937. dump_info->offset = dump_args.offset;
  4938. /* Dump stream info */
  4939. ctx->ctxt_to_hw_map = ctx_isp->hw_ctx;
  4940. if (ctx->hw_mgr_intf->hw_dump) {
  4941. /* Dump first part of stream info from isp context */
  4942. rc = cam_common_user_dump_helper(&dump_args,
  4943. cam_isp_ctx_user_dump_stream_info, ctx,
  4944. sizeof(int32_t), "ISP_STREAM_INFO_FROM_CTX:");
  4945. if (rc) {
  4946. CAM_ERR(CAM_ISP,
  4947. "ISP CTX stream info dump fail %lld, rc: %d, ctx: %u, link: 0x%x",
  4948. req->request_id, rc, ctx->ctx_id, ctx->link_hdl);
  4949. goto end;
  4950. }
  4951. /* Dump second part of stream info from ife hw manager */
  4952. hw_cmd_args.ctxt_to_hw_map = ctx->ctxt_to_hw_map;
  4953. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  4954. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_DUMP_STREAM_INFO;
  4955. isp_hw_cmd_args.cmd_data = &dump_args;
  4956. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  4957. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv, &hw_cmd_args);
  4958. if (rc) {
  4959. CAM_ERR(CAM_ISP,
  4960. "IFE HW MGR stream info dump fail %lld, rc: %d, ctx: %u, link: 0x%x",
  4961. req->request_id, rc, ctx->ctx_id, ctx->link_hdl);
  4962. goto end;
  4963. }
  4964. dump_info->offset = dump_args.offset;
  4965. }
  4966. /* Dump event record */
  4967. rc = __cam_isp_ctx_dump_event_record(ctx_isp, &dump_args);
  4968. if (rc) {
  4969. CAM_ERR(CAM_ISP, "Event record dump fail %lld, rc: %d, ctx_idx: %u, link: 0x%x",
  4970. req->request_id, rc, ctx->ctx_id, ctx->link_hdl);
  4971. goto end;
  4972. }
  4973. dump_info->offset = dump_args.offset;
  4974. if (dump_only_event_record) {
  4975. goto end;
  4976. }
  4977. /* Dump state monitor array */
  4978. rc = __cam_isp_ctx_user_dump_state_monitor_array(ctx_isp, &dump_args);
  4979. if (rc) {
  4980. CAM_ERR(CAM_ISP, "Dump event fail %lld, rc: %d, ctx_idx: %u, link: 0x%x",
  4981. req->request_id, rc, ctx->ctx_id, ctx->link_hdl);
  4982. goto end;
  4983. }
  4984. /* Dump request info */
  4985. rc = __cam_isp_ctx_dump_req_info(ctx, req, &dump_args);
  4986. if (rc) {
  4987. CAM_ERR(CAM_ISP, "Dump Req info fail %lld, rc: %d, ctx_idx: %u, link: 0x%x",
  4988. req->request_id, rc, ctx->ctx_id, ctx->link_hdl);
  4989. goto end;
  4990. }
  4991. spin_unlock_bh(&ctx->lock);
  4992. /* Dump CSID, VFE, and SFE info */
  4993. dump_info->offset = dump_args.offset;
  4994. if (ctx->hw_mgr_intf->hw_dump) {
  4995. ife_dump_args.offset = dump_args.offset;
  4996. ife_dump_args.request_id = dump_info->req_id;
  4997. ife_dump_args.buf_handle = dump_info->buf_handle;
  4998. ife_dump_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  4999. rc = ctx->hw_mgr_intf->hw_dump(
  5000. ctx->hw_mgr_intf->hw_mgr_priv,
  5001. &ife_dump_args);
  5002. dump_info->offset = ife_dump_args.offset;
  5003. }
  5004. cam_mem_put_cpu_buf(dump_info->buf_handle);
  5005. return rc;
  5006. end:
  5007. spin_unlock_bh(&ctx->lock);
  5008. cam_mem_put_cpu_buf(dump_info->buf_handle);
  5009. return rc;
  5010. }
  5011. static int __cam_isp_ctx_flush_req_in_flushed_state(
  5012. struct cam_context *ctx,
  5013. struct cam_req_mgr_flush_request *flush_req)
  5014. {
  5015. CAM_INFO(CAM_ISP, "Flush (type %d) in flushed state req id %lld ctx_id:%u link: 0x%x",
  5016. flush_req->type, flush_req->req_id, ctx->ctx_id, ctx->link_hdl);
  5017. if (flush_req->req_id > ctx->last_flush_req)
  5018. ctx->last_flush_req = flush_req->req_id;
  5019. return 0;
  5020. }
  5021. static int __cam_isp_ctx_flush_req(struct cam_context *ctx,
  5022. struct list_head *req_list, struct cam_req_mgr_flush_request *flush_req)
  5023. {
  5024. int i, rc, tmp = 0;
  5025. struct cam_ctx_request *req;
  5026. struct cam_ctx_request *req_temp;
  5027. struct cam_isp_ctx_req *req_isp;
  5028. struct list_head flush_list;
  5029. struct cam_isp_context *ctx_isp = NULL;
  5030. ctx_isp = (struct cam_isp_context *) ctx->ctx_priv;
  5031. INIT_LIST_HEAD(&flush_list);
  5032. if (list_empty(req_list)) {
  5033. CAM_DBG(CAM_ISP, "request list is empty, ctx_id:%u link: 0x%x",
  5034. ctx->ctx_id, ctx->link_hdl);
  5035. if (flush_req->type == CAM_REQ_MGR_FLUSH_TYPE_CANCEL_REQ) {
  5036. CAM_INFO(CAM_ISP,
  5037. "no request to cancel(lastapplied:%lld cancel:%lld),ctx:%u link:0x%x",
  5038. ctx_isp->last_applied_req_id, flush_req->req_id,
  5039. ctx->ctx_id, ctx->link_hdl);
  5040. return -EINVAL;
  5041. } else
  5042. return 0;
  5043. }
  5044. CAM_DBG(CAM_REQ, "Flush [%u] in progress for req_id %llu, ctx_id:%u link: 0x%x",
  5045. flush_req->type, flush_req->req_id, ctx->ctx_id, ctx->link_hdl);
  5046. list_for_each_entry_safe(req, req_temp, req_list, list) {
  5047. if (flush_req->type == CAM_REQ_MGR_FLUSH_TYPE_CANCEL_REQ) {
  5048. if (req->request_id != flush_req->req_id) {
  5049. continue;
  5050. } else {
  5051. list_del_init(&req->list);
  5052. list_add_tail(&req->list, &flush_list);
  5053. __cam_isp_ctx_update_state_monitor_array(
  5054. ctx_isp,
  5055. CAM_ISP_STATE_CHANGE_TRIGGER_FLUSH,
  5056. req->request_id);
  5057. break;
  5058. }
  5059. }
  5060. list_del_init(&req->list);
  5061. list_add_tail(&req->list, &flush_list);
  5062. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  5063. CAM_ISP_STATE_CHANGE_TRIGGER_FLUSH, req->request_id);
  5064. }
  5065. if (list_empty(&flush_list)) {
  5066. /*
  5067. * Maybe the req isn't sent to KMD since UMD already skip
  5068. * req in CSL layer.
  5069. */
  5070. CAM_INFO(CAM_ISP,
  5071. "flush list is empty, flush type %d for req %llu, ctx_id:%u link: 0x%x",
  5072. flush_req->type, flush_req->req_id, ctx->ctx_id, ctx->link_hdl);
  5073. return 0;
  5074. }
  5075. list_for_each_entry_safe(req, req_temp, &flush_list, list) {
  5076. cam_smmu_buffer_tracker_putref(&req->buf_tracker);
  5077. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  5078. for (i = 0; i < req_isp->num_fence_map_out; i++) {
  5079. if (req_isp->fence_map_out[i].sync_id != -1) {
  5080. CAM_DBG(CAM_ISP,
  5081. "Flush req 0x%llx, fence %d, ctx_id:%u link: 0x%x",
  5082. req->request_id,
  5083. req_isp->fence_map_out[i].sync_id,
  5084. ctx->ctx_id, ctx->link_hdl);
  5085. rc = cam_sync_signal(
  5086. req_isp->fence_map_out[i].sync_id,
  5087. CAM_SYNC_STATE_SIGNALED_CANCEL,
  5088. CAM_SYNC_ISP_EVENT_FLUSH);
  5089. if (rc) {
  5090. tmp = req_isp->fence_map_out[i].sync_id;
  5091. CAM_ERR_RATE_LIMIT(CAM_ISP,
  5092. "signal fence %d failed, ctx_id:%u link: 0x%x",
  5093. tmp, ctx->ctx_id, ctx->link_hdl);
  5094. }
  5095. req_isp->fence_map_out[i].sync_id = -1;
  5096. }
  5097. }
  5098. req_isp->reapply_type = CAM_CONFIG_REAPPLY_NONE;
  5099. req_isp->cdm_reset_before_apply = false;
  5100. list_del_init(&req->list);
  5101. list_add_tail(&req->list, &ctx->free_req_list);
  5102. }
  5103. return 0;
  5104. }
  5105. static inline void __cam_isp_ctx_reset_fcg_tracker(
  5106. struct cam_context *ctx)
  5107. {
  5108. struct cam_isp_context *ctx_isp;
  5109. struct cam_isp_skip_frame_info *skip_info;
  5110. ctx_isp = (struct cam_isp_context *) ctx->ctx_priv;
  5111. /* Reset skipped_list for FCG config */
  5112. ctx_isp->fcg_tracker.sum_skipped = 0;
  5113. ctx_isp->fcg_tracker.num_skipped = 0;
  5114. list_for_each_entry(skip_info, &ctx_isp->fcg_tracker.skipped_list, list)
  5115. skip_info->num_frame_skipped = 0;
  5116. CAM_DBG(CAM_ISP, "Reset FCG skip info on ctx %u link: %x",
  5117. ctx->ctx_id, ctx->link_hdl);
  5118. }
  5119. static int __cam_isp_ctx_flush_req_in_top_state(
  5120. struct cam_context *ctx,
  5121. struct cam_req_mgr_flush_request *flush_req)
  5122. {
  5123. int rc = 0;
  5124. struct cam_isp_context *ctx_isp;
  5125. struct cam_isp_stop_args stop_isp;
  5126. struct cam_hw_stop_args stop_args;
  5127. struct cam_hw_reset_args reset_args;
  5128. struct cam_req_mgr_timer_notify timer;
  5129. ctx_isp = (struct cam_isp_context *) ctx->ctx_priv;
  5130. CAM_DBG(CAM_ISP, "Flush pending list, ctx_idx: %u, link: 0x%x", ctx->ctx_id, ctx->link_hdl);
  5131. spin_lock_bh(&ctx->lock);
  5132. __cam_isp_ctx_flush_req(ctx, &ctx->pending_req_list, flush_req);
  5133. spin_unlock_bh(&ctx->lock);
  5134. /* Reset skipped_list for FCG config */
  5135. __cam_isp_ctx_reset_fcg_tracker(ctx);
  5136. if (flush_req->type == CAM_REQ_MGR_FLUSH_TYPE_ALL) {
  5137. if (ctx->state <= CAM_CTX_READY) {
  5138. ctx->state = CAM_CTX_ACQUIRED;
  5139. goto end;
  5140. }
  5141. spin_lock_bh(&ctx->lock);
  5142. ctx->state = CAM_CTX_FLUSHED;
  5143. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_HALT;
  5144. spin_unlock_bh(&ctx->lock);
  5145. CAM_INFO(CAM_ISP, "Last request id to flush is %lld, ctx_id:%u link: 0x%x",
  5146. flush_req->req_id, ctx->ctx_id, ctx->link_hdl);
  5147. ctx->last_flush_req = flush_req->req_id;
  5148. __cam_isp_ctx_trigger_reg_dump(CAM_HW_MGR_CMD_REG_DUMP_ON_FLUSH, ctx);
  5149. stop_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  5150. stop_isp.hw_stop_cmd = CAM_ISP_HW_STOP_IMMEDIATELY;
  5151. stop_isp.stop_only = true;
  5152. stop_isp.is_internal_stop = false;
  5153. stop_args.args = (void *)&stop_isp;
  5154. rc = ctx->hw_mgr_intf->hw_stop(ctx->hw_mgr_intf->hw_mgr_priv,
  5155. &stop_args);
  5156. if (rc)
  5157. CAM_ERR(CAM_ISP, "Failed to stop HW in Flush rc: %d, ctx_id:%u link: 0x%x",
  5158. rc, ctx->ctx_id, ctx->link_hdl);
  5159. CAM_INFO(CAM_ISP, "Stop HW complete. Reset HW next.Ctx_id:%u link: 0x%x",
  5160. ctx->ctx_id, ctx->link_hdl);
  5161. CAM_DBG(CAM_ISP, "Flush wait and active lists, ctx_id:%u link: 0x%x",
  5162. ctx->ctx_id, ctx->link_hdl);
  5163. if (ctx->ctx_crm_intf && ctx->ctx_crm_intf->notify_timer) {
  5164. timer.link_hdl = ctx->link_hdl;
  5165. timer.dev_hdl = ctx->dev_hdl;
  5166. timer.state = false;
  5167. ctx->ctx_crm_intf->notify_timer(&timer);
  5168. }
  5169. spin_lock_bh(&ctx->lock);
  5170. if (!list_empty(&ctx->wait_req_list))
  5171. __cam_isp_ctx_flush_req(ctx, &ctx->wait_req_list,
  5172. flush_req);
  5173. if (!list_empty(&ctx->active_req_list))
  5174. __cam_isp_ctx_flush_req(ctx, &ctx->active_req_list,
  5175. flush_req);
  5176. ctx_isp->active_req_cnt = 0;
  5177. spin_unlock_bh(&ctx->lock);
  5178. reset_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  5179. rc = ctx->hw_mgr_intf->hw_reset(ctx->hw_mgr_intf->hw_mgr_priv,
  5180. &reset_args);
  5181. if (rc)
  5182. CAM_ERR(CAM_ISP, "Failed to reset HW rc: %d, ctx_id:%u link: 0x%x",
  5183. rc, ctx->ctx_id, ctx->link_hdl);
  5184. ctx_isp->init_received = false;
  5185. }
  5186. end:
  5187. ctx_isp->bubble_frame_cnt = 0;
  5188. ctx_isp->congestion_cnt = 0;
  5189. ctx_isp->sof_dbg_irq_en = false;
  5190. atomic_set(&ctx_isp->process_bubble, 0);
  5191. atomic_set(&ctx_isp->rxd_epoch, 0);
  5192. atomic_set(&ctx_isp->internal_recovery_set, 0);
  5193. return rc;
  5194. }
  5195. static int __cam_isp_ctx_flush_req_in_ready(
  5196. struct cam_context *ctx,
  5197. struct cam_req_mgr_flush_request *flush_req)
  5198. {
  5199. int rc = 0;
  5200. CAM_DBG(CAM_ISP, "try to flush pending list, ctx_id:%u link: 0x%x",
  5201. ctx->ctx_id, ctx->link_hdl);
  5202. spin_lock_bh(&ctx->lock);
  5203. rc = __cam_isp_ctx_flush_req(ctx, &ctx->pending_req_list, flush_req);
  5204. /* if nothing is in pending req list, change state to acquire */
  5205. if (list_empty(&ctx->pending_req_list))
  5206. ctx->state = CAM_CTX_ACQUIRED;
  5207. spin_unlock_bh(&ctx->lock);
  5208. trace_cam_context_state("ISP", ctx);
  5209. CAM_DBG(CAM_ISP, "Flush request in ready state. next state %d, ctx_id:%u link: 0x%x",
  5210. ctx->state, ctx->ctx_id, ctx->link_hdl);
  5211. return rc;
  5212. }
  5213. static struct cam_ctx_ops
  5214. cam_isp_ctx_activated_state_machine[CAM_ISP_CTX_ACTIVATED_MAX] = {
  5215. /* SOF */
  5216. {
  5217. .ioctl_ops = {},
  5218. .crm_ops = {
  5219. .apply_req = __cam_isp_ctx_apply_req_in_sof,
  5220. .notify_frame_skip =
  5221. __cam_isp_ctx_apply_default_req_settings,
  5222. },
  5223. .irq_ops = NULL,
  5224. },
  5225. /* APPLIED */
  5226. {
  5227. .ioctl_ops = {},
  5228. .crm_ops = {},
  5229. .irq_ops = NULL,
  5230. },
  5231. /* EPOCH */
  5232. {
  5233. .ioctl_ops = {},
  5234. .crm_ops = {
  5235. .apply_req = __cam_isp_ctx_apply_req_in_epoch,
  5236. .notify_frame_skip =
  5237. __cam_isp_ctx_apply_default_req_settings,
  5238. },
  5239. .irq_ops = NULL,
  5240. },
  5241. /* BUBBLE */
  5242. {
  5243. .ioctl_ops = {},
  5244. .crm_ops = {
  5245. .apply_req = __cam_isp_ctx_apply_req_in_bubble,
  5246. .notify_frame_skip =
  5247. __cam_isp_ctx_apply_default_req_settings,
  5248. },
  5249. .irq_ops = NULL,
  5250. },
  5251. /* Bubble Applied */
  5252. {
  5253. .ioctl_ops = {},
  5254. .crm_ops = {
  5255. .notify_frame_skip =
  5256. __cam_isp_ctx_apply_default_req_settings,
  5257. },
  5258. .irq_ops = NULL,
  5259. },
  5260. /* HW ERROR */
  5261. {
  5262. .ioctl_ops = {},
  5263. .crm_ops = {},
  5264. .irq_ops = NULL,
  5265. },
  5266. /* HALT */
  5267. {
  5268. .ioctl_ops = {},
  5269. .crm_ops = {},
  5270. .irq_ops = NULL,
  5271. },
  5272. };
  5273. static struct cam_ctx_ops
  5274. cam_isp_ctx_fs2_state_machine[CAM_ISP_CTX_ACTIVATED_MAX] = {
  5275. /* SOF */
  5276. {
  5277. .ioctl_ops = {},
  5278. .crm_ops = {
  5279. .apply_req = __cam_isp_ctx_apply_req_in_sof,
  5280. },
  5281. .irq_ops = NULL,
  5282. },
  5283. /* APPLIED */
  5284. {
  5285. .ioctl_ops = {},
  5286. .crm_ops = {},
  5287. .irq_ops = NULL,
  5288. },
  5289. /* EPOCH */
  5290. {
  5291. .ioctl_ops = {},
  5292. .crm_ops = {
  5293. .apply_req = __cam_isp_ctx_apply_req_in_epoch,
  5294. },
  5295. .irq_ops = NULL,
  5296. },
  5297. /* BUBBLE */
  5298. {
  5299. .ioctl_ops = {},
  5300. .crm_ops = {
  5301. .apply_req = __cam_isp_ctx_apply_req_in_bubble,
  5302. },
  5303. .irq_ops = NULL,
  5304. },
  5305. /* Bubble Applied */
  5306. {
  5307. .ioctl_ops = {},
  5308. .crm_ops = {},
  5309. .irq_ops = NULL,
  5310. },
  5311. /* HW ERROR */
  5312. {
  5313. .ioctl_ops = {},
  5314. .crm_ops = {},
  5315. .irq_ops = NULL,
  5316. },
  5317. /* HALT */
  5318. {
  5319. .ioctl_ops = {},
  5320. .crm_ops = {},
  5321. .irq_ops = NULL,
  5322. },
  5323. };
  5324. static int __cam_isp_ctx_rdi_only_sof_in_top_state(
  5325. struct cam_isp_context *ctx_isp, void *evt_data)
  5326. {
  5327. int rc = 0;
  5328. struct cam_context *ctx = ctx_isp->base;
  5329. struct cam_isp_hw_sof_event_data *sof_event_data = evt_data;
  5330. uint64_t request_id = 0;
  5331. if (!evt_data) {
  5332. CAM_ERR(CAM_ISP, "in valid sof event data, ctx_idx: %u, link: 0x%x",
  5333. ctx->ctx_id, ctx->link_hdl);
  5334. return -EINVAL;
  5335. }
  5336. __cam_isp_ctx_update_sof_ts_util(sof_event_data, ctx_isp);
  5337. CAM_DBG(CAM_ISP, "frame id: %lld time stamp:0x%llx, ctx_idx: %u, link: 0x%x",
  5338. ctx_isp->frame_id, ctx_isp->sof_timestamp_val, ctx->ctx_id, ctx->link_hdl);
  5339. /*
  5340. * notify reqmgr with sof signal. Note, due to scheduling delay
  5341. * we can run into situation that two active requests has already
  5342. * be in the active queue while we try to do the notification.
  5343. * In this case, we need to skip the current notification. This
  5344. * helps the state machine to catch up the delay.
  5345. */
  5346. if (ctx_isp->active_req_cnt <= 2) {
  5347. __cam_isp_ctx_notify_trigger_util(CAM_TRIGGER_POINT_SOF, ctx_isp);
  5348. /*
  5349. * It's possible for rup done to be processed before
  5350. * SOF, check for first active request shutter here
  5351. */
  5352. if (!list_empty(&ctx->active_req_list)) {
  5353. struct cam_ctx_request *req = NULL;
  5354. req = list_first_entry(&ctx->active_req_list,
  5355. struct cam_ctx_request, list);
  5356. if (req->request_id > ctx_isp->reported_req_id) {
  5357. request_id = req->request_id;
  5358. ctx_isp->reported_req_id = request_id;
  5359. }
  5360. }
  5361. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  5362. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  5363. } else {
  5364. CAM_ERR_RATE_LIMIT(CAM_ISP, "Can not notify SOF to CRM, ctx_idx: %u, link: 0x%x",
  5365. ctx->ctx_id, ctx->link_hdl);
  5366. }
  5367. if (list_empty(&ctx->active_req_list))
  5368. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_SOF;
  5369. else
  5370. CAM_DBG(CAM_ISP, "Still need to wait for the buf done, ctx_idx: %u, link: 0x%x",
  5371. ctx->ctx_id, ctx->link_hdl);
  5372. CAM_DBG(CAM_ISP, "next Substate[%s], ctx_idx: %u, link: 0x%x",
  5373. __cam_isp_ctx_substate_val_to_type(
  5374. ctx_isp->substate_activated), ctx->ctx_id, ctx->link_hdl);
  5375. return rc;
  5376. }
  5377. static int __cam_isp_ctx_rdi_only_sof_in_applied_state(
  5378. struct cam_isp_context *ctx_isp, void *evt_data)
  5379. {
  5380. struct cam_isp_hw_sof_event_data *sof_event_data = evt_data;
  5381. if (!evt_data) {
  5382. CAM_ERR(CAM_ISP, "in valid sof event data");
  5383. return -EINVAL;
  5384. }
  5385. __cam_isp_ctx_update_sof_ts_util(sof_event_data, ctx_isp);
  5386. CAM_DBG(CAM_ISP, "frame id: %lld time stamp:0x%llx",
  5387. ctx_isp->frame_id, ctx_isp->sof_timestamp_val);
  5388. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_BUBBLE_APPLIED;
  5389. CAM_DBG(CAM_ISP, "next Substate[%s]",
  5390. __cam_isp_ctx_substate_val_to_type(
  5391. ctx_isp->substate_activated));
  5392. return 0;
  5393. }
  5394. static int __cam_isp_ctx_rdi_only_sof_in_bubble_applied(
  5395. struct cam_isp_context *ctx_isp, void *evt_data)
  5396. {
  5397. struct cam_ctx_request *req;
  5398. struct cam_isp_ctx_req *req_isp;
  5399. struct cam_context *ctx = ctx_isp->base;
  5400. struct cam_isp_hw_sof_event_data *sof_event_data = evt_data;
  5401. uint64_t request_id = 0;
  5402. /*
  5403. * Sof in bubble applied state means, reg update not received.
  5404. * before increment frame id and override time stamp value, send
  5405. * the previous sof time stamp that got captured in the
  5406. * sof in applied state.
  5407. */
  5408. CAM_DBG(CAM_ISP, "frame id: %lld time stamp:0x%llx, ctx_idx: %u, link: 0x%x",
  5409. ctx_isp->frame_id, ctx_isp->sof_timestamp_val, ctx->ctx_id, ctx->link_hdl);
  5410. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  5411. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  5412. __cam_isp_ctx_update_sof_ts_util(sof_event_data, ctx_isp);
  5413. CAM_DBG(CAM_ISP, "frame id: %lld time stamp:0x%llx, ctx_idx: %u, link: 0x%x",
  5414. ctx_isp->frame_id, ctx_isp->sof_timestamp_val, ctx->ctx_id, ctx->link_hdl);
  5415. if (list_empty(&ctx->wait_req_list)) {
  5416. /*
  5417. * If no pending req in epoch, this is an error case.
  5418. * The recovery is to go back to sof state
  5419. */
  5420. CAM_ERR(CAM_ISP, "No wait request, ctx_idx: %u, link: 0x%x",
  5421. ctx->ctx_id, ctx->link_hdl);
  5422. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_SOF;
  5423. /* Send SOF event as empty frame*/
  5424. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  5425. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  5426. goto end;
  5427. }
  5428. req = list_first_entry(&ctx->wait_req_list, struct cam_ctx_request,
  5429. list);
  5430. req_isp = (struct cam_isp_ctx_req *)req->req_priv;
  5431. req_isp->bubble_detected = true;
  5432. CAM_INFO_RATE_LIMIT(CAM_ISP, "Ctx:%u link: 0x%x Report Bubble flag %d req id:%lld",
  5433. ctx->ctx_id, ctx->link_hdl, req_isp->bubble_report, req->request_id);
  5434. req_isp->reapply_type = CAM_CONFIG_REAPPLY_IO;
  5435. req_isp->cdm_reset_before_apply = false;
  5436. if (req_isp->bubble_report) {
  5437. __cam_isp_ctx_notify_error_util(CAM_TRIGGER_POINT_SOF, CRM_KMD_ERR_BUBBLE,
  5438. req->request_id, ctx_isp);
  5439. atomic_set(&ctx_isp->process_bubble, 1);
  5440. } else {
  5441. req_isp->bubble_report = 0;
  5442. }
  5443. /*
  5444. * Always move the request to active list. Let buf done
  5445. * function handles the rest.
  5446. */
  5447. list_del_init(&req->list);
  5448. list_add_tail(&req->list, &ctx->active_req_list);
  5449. ctx_isp->active_req_cnt++;
  5450. CAM_DBG(CAM_ISP, "move request %lld to active list(cnt = %d), ctx_idx: %u, link: 0x%x",
  5451. req->request_id, ctx_isp->active_req_cnt, ctx->ctx_id, ctx->link_hdl);
  5452. if (!req_isp->bubble_report) {
  5453. if (req->request_id > ctx_isp->reported_req_id) {
  5454. request_id = req->request_id;
  5455. ctx_isp->reported_req_id = request_id;
  5456. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  5457. CAM_REQ_MGR_SOF_EVENT_ERROR);
  5458. } else
  5459. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  5460. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  5461. } else
  5462. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  5463. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  5464. /* change the state to bubble, as reg update has not come */
  5465. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_BUBBLE;
  5466. CAM_DBG(CAM_ISP, "next Substate[%s], ctx_idx: %u, link: 0x%x",
  5467. __cam_isp_ctx_substate_val_to_type(
  5468. ctx_isp->substate_activated), ctx->ctx_id, ctx->link_hdl);
  5469. end:
  5470. return 0;
  5471. }
  5472. static int __cam_isp_ctx_rdi_only_sof_in_bubble_state(
  5473. struct cam_isp_context *ctx_isp, void *evt_data)
  5474. {
  5475. uint32_t i;
  5476. struct cam_ctx_request *req;
  5477. struct cam_context *ctx = ctx_isp->base;
  5478. struct cam_isp_hw_sof_event_data *sof_event_data = evt_data;
  5479. struct cam_isp_ctx_req *req_isp;
  5480. struct cam_hw_cmd_args hw_cmd_args;
  5481. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  5482. uint64_t request_id = 0;
  5483. uint64_t last_cdm_done_req = 0;
  5484. int rc = 0;
  5485. if (!evt_data) {
  5486. CAM_ERR(CAM_ISP, "in valid sof event data, ctx_idx: %u, link: 0x%x",
  5487. ctx->ctx_id, ctx->link_hdl);
  5488. return -EINVAL;
  5489. }
  5490. __cam_isp_ctx_update_sof_ts_util(sof_event_data, ctx_isp);
  5491. CAM_DBG(CAM_ISP, "frame id: %lld time stamp:0x%llx, ctx_idx: %u, link: 0x%x",
  5492. ctx_isp->frame_id, ctx_isp->sof_timestamp_val, ctx->ctx_id, ctx->link_hdl);
  5493. if (atomic_read(&ctx_isp->process_bubble)) {
  5494. if (list_empty(&ctx->active_req_list)) {
  5495. CAM_ERR(CAM_ISP, "No available active req in bubble, ctx: %u, link: 0x%x",
  5496. ctx->ctx_id, ctx->link_hdl);
  5497. atomic_set(&ctx_isp->process_bubble, 0);
  5498. return -EINVAL;
  5499. }
  5500. if (ctx_isp->last_sof_timestamp ==
  5501. ctx_isp->sof_timestamp_val) {
  5502. CAM_DBG(CAM_ISP,
  5503. "Tasklet delay detected! Bubble frame: %lld check skipped, sof_timestamp: %lld, ctx_id: %u, link: 0x%x",
  5504. ctx_isp->frame_id,
  5505. ctx_isp->sof_timestamp_val,
  5506. ctx->ctx_id, ctx->link_hdl);
  5507. goto end;
  5508. }
  5509. req = list_first_entry(&ctx->active_req_list,
  5510. struct cam_ctx_request, list);
  5511. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  5512. if (req_isp->bubble_detected) {
  5513. hw_cmd_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  5514. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  5515. isp_hw_cmd_args.cmd_type =
  5516. CAM_ISP_HW_MGR_GET_LAST_CDM_DONE;
  5517. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  5518. rc = ctx->hw_mgr_intf->hw_cmd(
  5519. ctx->hw_mgr_intf->hw_mgr_priv,
  5520. &hw_cmd_args);
  5521. if (rc) {
  5522. CAM_ERR(CAM_ISP, "HW command failed, ctx_id: %u, link: 0x%x",
  5523. ctx->ctx_id, ctx->link_hdl);
  5524. return rc;
  5525. }
  5526. last_cdm_done_req = isp_hw_cmd_args.u.last_cdm_done;
  5527. CAM_DBG(CAM_ISP, "last_cdm_done req: %d ctx_id: %u, link: 0x%x",
  5528. last_cdm_done_req, ctx->ctx_id, ctx->link_hdl);
  5529. if (last_cdm_done_req >= req->request_id) {
  5530. CAM_DBG(CAM_ISP,
  5531. "CDM callback detected for req: %lld, possible buf_done delay, waiting for buf_done, ctx_id: %u, link: 0x%x",
  5532. req->request_id, ctx->ctx_id, ctx->link_hdl);
  5533. if (req_isp->num_fence_map_out ==
  5534. req_isp->num_deferred_acks) {
  5535. __cam_isp_handle_deferred_buf_done(ctx_isp, req,
  5536. true,
  5537. CAM_SYNC_STATE_SIGNALED_ERROR,
  5538. CAM_SYNC_ISP_EVENT_BUBBLE);
  5539. __cam_isp_ctx_handle_buf_done_for_req_list(
  5540. ctx_isp, req);
  5541. }
  5542. goto end;
  5543. } else {
  5544. CAM_WARN(CAM_ISP,
  5545. "CDM callback not happened for req: %lld, possible CDM stuck or workqueue delay, ctx_id: %u, link: 0x%x",
  5546. req->request_id, ctx->ctx_id, ctx->link_hdl);
  5547. req_isp->num_acked = 0;
  5548. req_isp->num_deferred_acks = 0;
  5549. req_isp->bubble_detected = false;
  5550. req_isp->cdm_reset_before_apply = true;
  5551. list_del_init(&req->list);
  5552. list_add(&req->list, &ctx->pending_req_list);
  5553. atomic_set(&ctx_isp->process_bubble, 0);
  5554. ctx_isp->active_req_cnt--;
  5555. CAM_DBG(CAM_REQ,
  5556. "Move active req: %lld to pending list(cnt = %d) [bubble re-apply],ctx %u link: 0x%x",
  5557. req->request_id,
  5558. ctx_isp->active_req_cnt, ctx->ctx_id, ctx->link_hdl);
  5559. }
  5560. goto end;
  5561. }
  5562. }
  5563. /*
  5564. * Signal all active requests with error and move the all the active
  5565. * requests to free list
  5566. */
  5567. while (!list_empty(&ctx->active_req_list)) {
  5568. req = list_first_entry(&ctx->active_req_list,
  5569. struct cam_ctx_request, list);
  5570. list_del_init(&req->list);
  5571. cam_smmu_buffer_tracker_putref(&req->buf_tracker);
  5572. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  5573. CAM_DBG(CAM_ISP, "signal fence in active list. fence num %d, ctx %u link: 0x%x",
  5574. req_isp->num_fence_map_out, ctx->ctx_id, ctx->link_hdl);
  5575. for (i = 0; i < req_isp->num_fence_map_out; i++)
  5576. if (req_isp->fence_map_out[i].sync_id != -1) {
  5577. cam_sync_signal(
  5578. req_isp->fence_map_out[i].sync_id,
  5579. CAM_SYNC_STATE_SIGNALED_ERROR,
  5580. CAM_SYNC_ISP_EVENT_BUBBLE);
  5581. }
  5582. list_add_tail(&req->list, &ctx->free_req_list);
  5583. ctx_isp->active_req_cnt--;
  5584. }
  5585. end:
  5586. /* notify reqmgr with sof signal */
  5587. __cam_isp_ctx_notify_trigger_util(CAM_TRIGGER_POINT_SOF, ctx_isp);
  5588. /*
  5589. * It is idle frame with out any applied request id, send
  5590. * request id as zero
  5591. */
  5592. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  5593. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  5594. /*
  5595. * Can't move the substate to SOF if we are processing bubble,
  5596. * since the SOF substate can't receive REG_UPD and buf done,
  5597. * then the processing of bubble req can't be finished
  5598. */
  5599. if (!atomic_read(&ctx_isp->process_bubble))
  5600. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_SOF;
  5601. CAM_DBG(CAM_ISP, "next Substate[%s], ctx %u link: 0x%x",
  5602. __cam_isp_ctx_substate_val_to_type(
  5603. ctx_isp->substate_activated), ctx->ctx_id, ctx->link_hdl);
  5604. ctx_isp->last_sof_timestamp = ctx_isp->sof_timestamp_val;
  5605. return 0;
  5606. }
  5607. static int __cam_isp_ctx_rdi_only_reg_upd_in_bubble_state(
  5608. struct cam_isp_context *ctx_isp, void *evt_data)
  5609. {
  5610. struct cam_ctx_request *req = NULL;
  5611. struct cam_context *ctx = ctx_isp->base;
  5612. req = list_first_entry(&ctx->active_req_list,
  5613. struct cam_ctx_request, list);
  5614. CAM_INFO(CAM_ISP, "Received RUP for Bubble Request, ctx %u link: 0x%x",
  5615. req->request_id, ctx->ctx_id, ctx->link_hdl);
  5616. return 0;
  5617. }
  5618. static int __cam_isp_ctx_rdi_only_reg_upd_in_bubble_applied_state(
  5619. struct cam_isp_context *ctx_isp, void *evt_data)
  5620. {
  5621. struct cam_ctx_request *req = NULL;
  5622. struct cam_context *ctx = ctx_isp->base;
  5623. struct cam_isp_ctx_req *req_isp;
  5624. uint64_t request_id = 0;
  5625. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_EPOCH;
  5626. /* notify reqmgr with sof signal*/
  5627. if (list_empty(&ctx->wait_req_list)) {
  5628. CAM_ERR(CAM_ISP, "Reg upd ack with no waiting request, ctx %u link: 0x%x",
  5629. ctx->ctx_id, ctx->link_hdl);
  5630. goto error;
  5631. }
  5632. req = list_first_entry(&ctx->wait_req_list,
  5633. struct cam_ctx_request, list);
  5634. list_del_init(&req->list);
  5635. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  5636. request_id =
  5637. (req_isp->hw_update_data.packet_opcode_type ==
  5638. CAM_ISP_PACKET_INIT_DEV) ? 0 : req->request_id;
  5639. if (req_isp->num_fence_map_out != 0) {
  5640. list_add_tail(&req->list, &ctx->active_req_list);
  5641. ctx_isp->active_req_cnt++;
  5642. CAM_DBG(CAM_ISP,
  5643. "move request %lld to active list(cnt = %d), ctx %u link: 0x%x",
  5644. req->request_id, ctx_isp->active_req_cnt, ctx->ctx_id, ctx->link_hdl);
  5645. /* if packet has buffers, set correct request id */
  5646. request_id = req->request_id;
  5647. } else {
  5648. cam_smmu_buffer_tracker_putref(&req->buf_tracker);
  5649. /* no io config, so the request is completed. */
  5650. list_add_tail(&req->list, &ctx->free_req_list);
  5651. CAM_DBG(CAM_ISP,
  5652. "move active req %lld to free list(cnt=%d), ctx %u link: 0x%x",
  5653. req->request_id, ctx_isp->active_req_cnt, ctx->ctx_id, ctx->link_hdl);
  5654. }
  5655. __cam_isp_ctx_notify_trigger_util(CAM_TRIGGER_POINT_SOF, ctx_isp);
  5656. if (request_id)
  5657. ctx_isp->reported_req_id = request_id;
  5658. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  5659. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  5660. CAM_DBG(CAM_ISP, "next Substate[%s], ctx %u link: 0x%x",
  5661. __cam_isp_ctx_substate_val_to_type(
  5662. ctx_isp->substate_activated), ctx->ctx_id, ctx->link_hdl);
  5663. __cam_isp_ctx_update_event_record(ctx_isp,
  5664. CAM_ISP_CTX_EVENT_RUP, req, NULL);
  5665. return 0;
  5666. error:
  5667. /* Send SOF event as idle frame*/
  5668. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  5669. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  5670. __cam_isp_ctx_update_event_record(ctx_isp,
  5671. CAM_ISP_CTX_EVENT_RUP, NULL, NULL);
  5672. /*
  5673. * There is no request in the pending list, move the sub state machine
  5674. * to SOF sub state
  5675. */
  5676. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_SOF;
  5677. return 0;
  5678. }
  5679. static struct cam_isp_ctx_irq_ops
  5680. cam_isp_ctx_rdi_only_activated_state_machine_irq
  5681. [CAM_ISP_CTX_ACTIVATED_MAX] = {
  5682. /* SOF */
  5683. {
  5684. .irq_ops = {
  5685. NULL,
  5686. __cam_isp_ctx_rdi_only_sof_in_top_state,
  5687. __cam_isp_ctx_reg_upd_in_sof,
  5688. NULL,
  5689. __cam_isp_ctx_notify_eof_in_activated_state,
  5690. NULL,
  5691. },
  5692. },
  5693. /* APPLIED */
  5694. {
  5695. .irq_ops = {
  5696. __cam_isp_ctx_handle_error,
  5697. __cam_isp_ctx_rdi_only_sof_in_applied_state,
  5698. __cam_isp_ctx_reg_upd_in_applied_state,
  5699. NULL,
  5700. __cam_isp_ctx_notify_eof_in_activated_state,
  5701. __cam_isp_ctx_buf_done_in_applied,
  5702. },
  5703. },
  5704. /* EPOCH */
  5705. {
  5706. .irq_ops = {
  5707. __cam_isp_ctx_handle_error,
  5708. __cam_isp_ctx_rdi_only_sof_in_top_state,
  5709. NULL,
  5710. NULL,
  5711. __cam_isp_ctx_notify_eof_in_activated_state,
  5712. __cam_isp_ctx_buf_done_in_epoch,
  5713. },
  5714. },
  5715. /* BUBBLE*/
  5716. {
  5717. .irq_ops = {
  5718. __cam_isp_ctx_handle_error,
  5719. __cam_isp_ctx_rdi_only_sof_in_bubble_state,
  5720. __cam_isp_ctx_rdi_only_reg_upd_in_bubble_state,
  5721. NULL,
  5722. __cam_isp_ctx_notify_eof_in_activated_state,
  5723. __cam_isp_ctx_buf_done_in_bubble,
  5724. },
  5725. },
  5726. /* BUBBLE APPLIED ie PRE_BUBBLE */
  5727. {
  5728. .irq_ops = {
  5729. __cam_isp_ctx_handle_error,
  5730. __cam_isp_ctx_rdi_only_sof_in_bubble_applied,
  5731. __cam_isp_ctx_rdi_only_reg_upd_in_bubble_applied_state,
  5732. NULL,
  5733. __cam_isp_ctx_notify_eof_in_activated_state,
  5734. __cam_isp_ctx_buf_done_in_bubble_applied,
  5735. },
  5736. },
  5737. /* HW ERROR */
  5738. {
  5739. },
  5740. /* HALT */
  5741. {
  5742. },
  5743. };
  5744. static int __cam_isp_ctx_rdi_only_apply_req_top_state(
  5745. struct cam_context *ctx, struct cam_req_mgr_apply_request *apply)
  5746. {
  5747. int rc = 0;
  5748. struct cam_isp_context *ctx_isp =
  5749. (struct cam_isp_context *) ctx->ctx_priv;
  5750. CAM_DBG(CAM_ISP, "current Substate[%s], ctx_idx: %u, link: 0x%x",
  5751. __cam_isp_ctx_substate_val_to_type(
  5752. ctx_isp->substate_activated), ctx->ctx_id, ctx->link_hdl);
  5753. rc = __cam_isp_ctx_apply_req_in_activated_state(ctx, apply,
  5754. CAM_ISP_CTX_ACTIVATED_APPLIED);
  5755. CAM_DBG(CAM_ISP, "new Substate[%s], ctx_idx: %u, link: 0x%x",
  5756. __cam_isp_ctx_substate_val_to_type(
  5757. ctx_isp->substate_activated), ctx->ctx_id, ctx->link_hdl);
  5758. if (rc)
  5759. CAM_ERR_RATE_LIMIT(CAM_ISP,
  5760. "ctx_id:%u link: 0x%x Apply failed in Substate[%s], rc %d",
  5761. ctx->ctx_id, ctx->link_hdl,
  5762. __cam_isp_ctx_substate_val_to_type(
  5763. ctx_isp->substate_activated), rc);
  5764. return rc;
  5765. }
  5766. static struct cam_ctx_ops
  5767. cam_isp_ctx_rdi_only_activated_state_machine
  5768. [CAM_ISP_CTX_ACTIVATED_MAX] = {
  5769. /* SOF */
  5770. {
  5771. .ioctl_ops = {},
  5772. .crm_ops = {
  5773. .apply_req = __cam_isp_ctx_rdi_only_apply_req_top_state,
  5774. },
  5775. .irq_ops = NULL,
  5776. },
  5777. /* APPLIED */
  5778. {
  5779. .ioctl_ops = {},
  5780. .crm_ops = {},
  5781. .irq_ops = NULL,
  5782. },
  5783. /* EPOCH */
  5784. {
  5785. .ioctl_ops = {},
  5786. .crm_ops = {
  5787. .apply_req = __cam_isp_ctx_rdi_only_apply_req_top_state,
  5788. },
  5789. .irq_ops = NULL,
  5790. },
  5791. /* PRE BUBBLE */
  5792. {
  5793. .ioctl_ops = {},
  5794. .crm_ops = {},
  5795. .irq_ops = NULL,
  5796. },
  5797. /* BUBBLE */
  5798. {
  5799. .ioctl_ops = {},
  5800. .crm_ops = {},
  5801. .irq_ops = NULL,
  5802. },
  5803. /* HW ERROR */
  5804. {
  5805. .ioctl_ops = {},
  5806. .crm_ops = {},
  5807. .irq_ops = NULL,
  5808. },
  5809. /* HALT */
  5810. {
  5811. .ioctl_ops = {},
  5812. .crm_ops = {},
  5813. .irq_ops = NULL,
  5814. },
  5815. };
  5816. static int __cam_isp_ctx_flush_dev_in_top_state(struct cam_context *ctx,
  5817. struct cam_flush_dev_cmd *cmd)
  5818. {
  5819. struct cam_isp_context *ctx_isp = ctx->ctx_priv;
  5820. struct cam_req_mgr_flush_request flush_req;
  5821. if (!ctx_isp->offline_context) {
  5822. CAM_ERR(CAM_ISP, "flush dev only supported in offline context,ctx: %u, link:0x%x",
  5823. ctx->ctx_id, ctx->link_hdl);
  5824. return -EINVAL;
  5825. }
  5826. flush_req.type = (cmd->flush_type == CAM_FLUSH_TYPE_ALL) ? CAM_REQ_MGR_FLUSH_TYPE_ALL :
  5827. CAM_REQ_MGR_FLUSH_TYPE_CANCEL_REQ;
  5828. flush_req.req_id = cmd->req_id;
  5829. CAM_DBG(CAM_ISP, "offline flush (type:%u, req:%lu), ctx_idx: %u, link: 0x%x",
  5830. flush_req.type, flush_req.req_id, ctx->ctx_id, ctx->link_hdl);
  5831. switch (ctx->state) {
  5832. case CAM_CTX_ACQUIRED:
  5833. case CAM_CTX_ACTIVATED:
  5834. return __cam_isp_ctx_flush_req_in_top_state(ctx, &flush_req);
  5835. case CAM_CTX_READY:
  5836. return __cam_isp_ctx_flush_req_in_ready(ctx, &flush_req);
  5837. default:
  5838. CAM_ERR(CAM_ISP, "flush dev in wrong state: %d, ctx_idx: %u, link: 0x%x",
  5839. ctx->state, ctx->ctx_id, ctx->link_hdl);
  5840. return -EINVAL;
  5841. }
  5842. if (cmd->flush_type == CAM_FLUSH_TYPE_ALL)
  5843. cam_req_mgr_workq_flush(ctx_isp->workq);
  5844. }
  5845. static void __cam_isp_ctx_free_mem_hw_entries(struct cam_context *ctx)
  5846. {
  5847. int i;
  5848. if (ctx->out_map_entries) {
  5849. for (i = 0; i < CAM_ISP_CTX_REQ_MAX; i++) {
  5850. kfree(ctx->out_map_entries[i]);
  5851. ctx->out_map_entries[i] = NULL;
  5852. }
  5853. kfree(ctx->out_map_entries);
  5854. ctx->out_map_entries = NULL;
  5855. }
  5856. if (ctx->in_map_entries) {
  5857. for (i = 0; i < CAM_ISP_CTX_REQ_MAX; i++) {
  5858. kfree(ctx->in_map_entries[i]);
  5859. ctx->in_map_entries[i] = NULL;
  5860. }
  5861. kfree(ctx->in_map_entries);
  5862. ctx->in_map_entries = NULL;
  5863. }
  5864. if (ctx->hw_update_entry) {
  5865. for (i = 0; i < CAM_ISP_CTX_REQ_MAX; i++) {
  5866. kfree(ctx->hw_update_entry[i]);
  5867. ctx->hw_update_entry[i] = NULL;
  5868. }
  5869. kfree(ctx->hw_update_entry);
  5870. ctx->hw_update_entry = NULL;
  5871. }
  5872. ctx->max_out_map_entries = 0;
  5873. ctx->max_in_map_entries = 0;
  5874. ctx->max_hw_update_entries = 0;
  5875. }
  5876. static int __cam_isp_ctx_release_hw_in_top_state(struct cam_context *ctx,
  5877. void *cmd)
  5878. {
  5879. int rc = 0;
  5880. struct cam_hw_release_args rel_arg;
  5881. struct cam_isp_context *ctx_isp =
  5882. (struct cam_isp_context *) ctx->ctx_priv;
  5883. struct cam_req_mgr_flush_request flush_req;
  5884. int i;
  5885. if (ctx_isp->hw_ctx) {
  5886. rel_arg.ctxt_to_hw_map = ctx_isp->hw_ctx;
  5887. ctx->hw_mgr_intf->hw_release(ctx->hw_mgr_intf->hw_mgr_priv,
  5888. &rel_arg);
  5889. ctx_isp->hw_ctx = NULL;
  5890. } else {
  5891. CAM_ERR(CAM_ISP, "No hw resources acquired for ctx[%u], link: 0x%x",
  5892. ctx->ctx_id, ctx->link_hdl);
  5893. }
  5894. ctx->last_flush_req = 0;
  5895. ctx_isp->custom_enabled = false;
  5896. ctx_isp->use_frame_header_ts = false;
  5897. ctx_isp->use_default_apply = false;
  5898. ctx_isp->frame_id = 0;
  5899. ctx_isp->active_req_cnt = 0;
  5900. ctx_isp->reported_req_id = 0;
  5901. ctx_isp->reported_frame_id = 0;
  5902. ctx_isp->hw_acquired = false;
  5903. ctx_isp->init_received = false;
  5904. ctx_isp->support_consumed_addr = false;
  5905. ctx_isp->aeb_enabled = false;
  5906. ctx_isp->req_info.last_bufdone_req_id = 0;
  5907. kfree(ctx_isp->vfe_bus_comp_grp);
  5908. kfree(ctx_isp->sfe_bus_comp_grp);
  5909. ctx_isp->vfe_bus_comp_grp = NULL;
  5910. ctx_isp->sfe_bus_comp_grp = NULL;
  5911. atomic64_set(&ctx_isp->dbg_monitors.state_monitor_head, -1);
  5912. atomic64_set(&ctx_isp->dbg_monitors.frame_monitor_head, -1);
  5913. for (i = 0; i < CAM_ISP_CTX_EVENT_MAX; i++)
  5914. atomic64_set(&ctx_isp->dbg_monitors.event_record_head[i], -1);
  5915. /*
  5916. * Ideally, we should never have any active request here.
  5917. * But we still add some sanity check code here to help the debug
  5918. */
  5919. if (!list_empty(&ctx->active_req_list))
  5920. CAM_WARN(CAM_ISP, "Active list is not empty, ctx_idx: %u, link: 0x%x",
  5921. ctx->ctx_id, ctx->link_hdl);
  5922. /* Flush all the pending request list */
  5923. flush_req.type = CAM_REQ_MGR_FLUSH_TYPE_ALL;
  5924. flush_req.link_hdl = ctx->link_hdl;
  5925. flush_req.dev_hdl = ctx->dev_hdl;
  5926. flush_req.req_id = 0;
  5927. CAM_DBG(CAM_ISP, "try to flush pending list, ctx_idx: %u, link: 0x%x",
  5928. ctx->ctx_id, ctx->link_hdl);
  5929. spin_lock_bh(&ctx->lock);
  5930. rc = __cam_isp_ctx_flush_req(ctx, &ctx->pending_req_list, &flush_req);
  5931. spin_unlock_bh(&ctx->lock);
  5932. __cam_isp_ctx_free_mem_hw_entries(ctx);
  5933. cam_req_mgr_workq_destroy(&ctx_isp->workq);
  5934. ctx->state = CAM_CTX_ACQUIRED;
  5935. trace_cam_context_state("ISP", ctx);
  5936. CAM_DBG(CAM_ISP, "Release device success[%u] link: 0x%x next state %d",
  5937. ctx->ctx_id, ctx->link_hdl, ctx->state);
  5938. return rc;
  5939. }
  5940. /* top level state machine */
  5941. static int __cam_isp_ctx_release_dev_in_top_state(struct cam_context *ctx,
  5942. struct cam_release_dev_cmd *cmd)
  5943. {
  5944. int rc = 0;
  5945. int i;
  5946. struct cam_hw_release_args rel_arg;
  5947. struct cam_isp_context *ctx_isp =
  5948. (struct cam_isp_context *) ctx->ctx_priv;
  5949. struct cam_req_mgr_flush_request flush_req;
  5950. if (cmd && ctx_isp->hw_ctx) {
  5951. CAM_ERR(CAM_ISP, "releasing hw, ctx_idx: %u, link: 0x%x",
  5952. ctx->ctx_id, ctx->link_hdl);
  5953. __cam_isp_ctx_release_hw_in_top_state(ctx, NULL);
  5954. }
  5955. if (ctx_isp->hw_ctx) {
  5956. rel_arg.ctxt_to_hw_map = ctx_isp->hw_ctx;
  5957. ctx->hw_mgr_intf->hw_release(ctx->hw_mgr_intf->hw_mgr_priv,
  5958. &rel_arg);
  5959. ctx_isp->hw_ctx = NULL;
  5960. }
  5961. cam_common_release_evt_params(ctx->dev_hdl);
  5962. memset(&ctx_isp->evt_inject_params, 0, sizeof(struct cam_hw_inject_evt_param));
  5963. ctx->session_hdl = -1;
  5964. ctx->dev_hdl = -1;
  5965. ctx->link_hdl = -1;
  5966. ctx->ctx_crm_intf = NULL;
  5967. ctx->last_flush_req = 0;
  5968. ctx_isp->frame_id = 0;
  5969. ctx_isp->active_req_cnt = 0;
  5970. ctx_isp->reported_req_id = 0;
  5971. ctx_isp->reported_frame_id = 0;
  5972. ctx_isp->hw_acquired = false;
  5973. ctx_isp->init_received = false;
  5974. ctx_isp->offline_context = false;
  5975. ctx_isp->vfps_aux_context = false;
  5976. ctx_isp->rdi_only_context = false;
  5977. ctx_isp->req_info.last_bufdone_req_id = 0;
  5978. ctx_isp->v4l2_event_sub_ids = 0;
  5979. ctx_isp->resume_hw_in_flushed = false;
  5980. atomic64_set(&ctx_isp->dbg_monitors.state_monitor_head, -1);
  5981. atomic64_set(&ctx_isp->dbg_monitors.frame_monitor_head, -1);
  5982. for (i = 0; i < CAM_ISP_CTX_EVENT_MAX; i++)
  5983. atomic64_set(&ctx_isp->dbg_monitors.event_record_head[i], -1);
  5984. /*
  5985. * Ideally, we should never have any active request here.
  5986. * But we still add some sanity check code here to help the debug
  5987. */
  5988. if (!list_empty(&ctx->active_req_list))
  5989. CAM_ERR(CAM_ISP, "Active list is not empty, ctx_idx: %u, link: 0x%x",
  5990. ctx->ctx_id, ctx->link_hdl);
  5991. /* Flush all the pending request list */
  5992. flush_req.type = CAM_REQ_MGR_FLUSH_TYPE_ALL;
  5993. flush_req.link_hdl = ctx->link_hdl;
  5994. flush_req.dev_hdl = ctx->dev_hdl;
  5995. flush_req.req_id = 0;
  5996. CAM_DBG(CAM_ISP, "try to flush pending list, ctx_idx: %u, link: 0x%x",
  5997. ctx->ctx_id, ctx->link_hdl);
  5998. spin_lock_bh(&ctx->lock);
  5999. rc = __cam_isp_ctx_flush_req(ctx, &ctx->pending_req_list, &flush_req);
  6000. spin_unlock_bh(&ctx->lock);
  6001. __cam_isp_ctx_free_mem_hw_entries(ctx);
  6002. ctx->state = CAM_CTX_AVAILABLE;
  6003. trace_cam_context_state("ISP", ctx);
  6004. CAM_DBG(CAM_ISP, "Release device success[%u] link: 0x%x next state %d",
  6005. ctx->ctx_id, ctx->link_hdl, ctx->state);
  6006. return rc;
  6007. }
  6008. static int __cam_isp_ctx_config_dev_in_top_state(
  6009. struct cam_context *ctx, struct cam_config_dev_cmd *cmd)
  6010. {
  6011. int rc = 0, i;
  6012. struct cam_ctx_request *req = NULL;
  6013. struct cam_isp_ctx_req *req_isp;
  6014. struct cam_packet *packet;
  6015. size_t remain_len = 0;
  6016. struct cam_hw_prepare_update_args cfg = {0};
  6017. struct cam_req_mgr_add_request add_req;
  6018. struct cam_isp_context *ctx_isp =
  6019. (struct cam_isp_context *) ctx->ctx_priv;
  6020. struct cam_hw_cmd_args hw_cmd_args;
  6021. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  6022. uint32_t packet_opcode = 0;
  6023. CAM_DBG(CAM_ISP, "get free request object......ctx_idx: %u, link: 0x%x",
  6024. ctx->ctx_id, ctx->link_hdl);
  6025. /* get free request */
  6026. spin_lock_bh(&ctx->lock);
  6027. if (!list_empty(&ctx->free_req_list)) {
  6028. req = list_first_entry(&ctx->free_req_list,
  6029. struct cam_ctx_request, list);
  6030. list_del_init(&req->list);
  6031. }
  6032. spin_unlock_bh(&ctx->lock);
  6033. if (!req) {
  6034. CAM_ERR(CAM_ISP, "No more request obj free, ctx_idx: %u, link: 0x%x",
  6035. ctx->ctx_id, ctx->link_hdl);
  6036. return -ENOMEM;
  6037. }
  6038. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  6039. remain_len = cam_context_parse_config_cmd(ctx, cmd, &packet);
  6040. if (IS_ERR(packet)) {
  6041. rc = PTR_ERR(packet);
  6042. goto free_req;
  6043. }
  6044. /* Query the packet opcode */
  6045. hw_cmd_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  6046. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  6047. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_GET_PACKET_OPCODE;
  6048. isp_hw_cmd_args.cmd_data = (void *)packet;
  6049. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  6050. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  6051. &hw_cmd_args);
  6052. if (rc) {
  6053. CAM_ERR(CAM_ISP, "HW command failed, ctx_idx: %u, link: 0x%x",
  6054. ctx->ctx_id, ctx->link_hdl);
  6055. goto free_req;
  6056. }
  6057. packet_opcode = isp_hw_cmd_args.u.packet_op_code;
  6058. if ((packet_opcode == CAM_ISP_PACKET_UPDATE_DEV)
  6059. && (packet->header.request_id <= ctx->last_flush_req)) {
  6060. CAM_INFO(CAM_ISP,
  6061. "request %lld has been flushed, reject packet, ctx_idx: %u, link: 0x%x",
  6062. packet->header.request_id, ctx->ctx_id, ctx->link_hdl);
  6063. rc = -EBADR;
  6064. goto free_req;
  6065. } else if ((packet_opcode == CAM_ISP_PACKET_INIT_DEV)
  6066. && (packet->header.request_id <= ctx->last_flush_req)
  6067. && ctx->last_flush_req && packet->header.request_id) {
  6068. CAM_WARN(CAM_ISP,
  6069. "last flushed req is %lld, config dev(init) for req %lld, ctx_idx: %u, link: 0x%x",
  6070. ctx->last_flush_req, packet->header.request_id, ctx->ctx_id, ctx->link_hdl);
  6071. rc = -EBADR;
  6072. goto free_req;
  6073. }
  6074. cfg.packet = packet;
  6075. cfg.remain_len = remain_len;
  6076. cfg.ctxt_to_hw_map = ctx_isp->hw_ctx;
  6077. cfg.max_hw_update_entries = ctx->max_hw_update_entries;
  6078. cfg.hw_update_entries = req_isp->cfg;
  6079. cfg.max_out_map_entries = ctx->max_out_map_entries;
  6080. cfg.max_in_map_entries = ctx->max_in_map_entries;
  6081. cfg.out_map_entries = req_isp->fence_map_out;
  6082. cfg.in_map_entries = req_isp->fence_map_in;
  6083. cfg.priv = &req_isp->hw_update_data;
  6084. cfg.pf_data = &(req->pf_data);
  6085. cfg.num_out_map_entries = 0;
  6086. cfg.num_in_map_entries = 0;
  6087. cfg.buf_tracker = &req->buf_tracker;
  6088. memset(&req_isp->hw_update_data, 0, sizeof(req_isp->hw_update_data));
  6089. memset(req_isp->fence_map_out, 0, sizeof(struct cam_hw_fence_map_entry)
  6090. * ctx->max_out_map_entries);
  6091. INIT_LIST_HEAD(cfg.buf_tracker);
  6092. rc = ctx->hw_mgr_intf->hw_prepare_update(
  6093. ctx->hw_mgr_intf->hw_mgr_priv, &cfg);
  6094. if (rc != 0) {
  6095. CAM_ERR(CAM_ISP, "Prepare config packet failed in HW layer, ctx: %u, link: 0x%x",
  6096. ctx->ctx_id, ctx->link_hdl);
  6097. rc = -EFAULT;
  6098. goto free_req_and_buf_tracker_list;
  6099. }
  6100. req_isp->num_cfg = cfg.num_hw_update_entries;
  6101. req_isp->num_fence_map_out = cfg.num_out_map_entries;
  6102. req_isp->num_fence_map_in = cfg.num_in_map_entries;
  6103. req_isp->num_acked = 0;
  6104. req_isp->num_deferred_acks = 0;
  6105. req_isp->bubble_detected = false;
  6106. req_isp->cdm_reset_before_apply = false;
  6107. req_isp->hw_update_data.packet = packet;
  6108. req->pf_data.packet_handle = cmd->packet_handle;
  6109. req->pf_data.packet_offset = cmd->offset;
  6110. req->pf_data.req = req;
  6111. for (i = 0; i < req_isp->num_fence_map_out; i++) {
  6112. rc = cam_sync_get_obj_ref(req_isp->fence_map_out[i].sync_id);
  6113. if (rc) {
  6114. CAM_ERR(CAM_ISP, "Can't get ref for fence %d, ctx_idx: %u, link: 0x%x",
  6115. req_isp->fence_map_out[i].sync_id, ctx->ctx_id, ctx->link_hdl);
  6116. goto put_ref;
  6117. }
  6118. }
  6119. CAM_DBG(CAM_ISP,
  6120. "packet req-id:%lld, opcode:%d, num_entry:%d, num_fence_out: %d, num_fence_in: %d, ctx_idx: %u, link: 0x%x",
  6121. packet->header.request_id, req_isp->hw_update_data.packet_opcode_type,
  6122. req_isp->num_cfg, req_isp->num_fence_map_out, req_isp->num_fence_map_in,
  6123. ctx->ctx_id, ctx->link_hdl);
  6124. req->request_id = packet->header.request_id;
  6125. req->status = 1;
  6126. if (req_isp->hw_update_data.packet_opcode_type ==
  6127. CAM_ISP_PACKET_INIT_DEV) {
  6128. if (ctx->state < CAM_CTX_ACTIVATED) {
  6129. rc = __cam_isp_ctx_enqueue_init_request(ctx, req);
  6130. if (rc)
  6131. CAM_ERR(CAM_ISP, "Enqueue INIT pkt failed, ctx: %u, link: 0x%x",
  6132. ctx->ctx_id, ctx->link_hdl);
  6133. ctx_isp->init_received = true;
  6134. if ((ctx_isp->vfps_aux_context) && (req->request_id > 0))
  6135. ctx_isp->resume_hw_in_flushed = true;
  6136. else
  6137. ctx_isp->resume_hw_in_flushed = false;
  6138. } else {
  6139. rc = -EINVAL;
  6140. CAM_ERR(CAM_ISP, "Received INIT pkt in wrong state:%d, ctx:%u, link:0x%x",
  6141. ctx->state, ctx->ctx_id, ctx->link_hdl);
  6142. }
  6143. } else {
  6144. if ((ctx->state == CAM_CTX_FLUSHED) || (ctx->state < CAM_CTX_READY)) {
  6145. rc = -EINVAL;
  6146. CAM_ERR(CAM_ISP,
  6147. "Received update req %lld in wrong state:%d, ctx_idx: %u, link: 0x%x",
  6148. req->request_id, ctx->state, ctx->ctx_id, ctx->link_hdl);
  6149. goto put_ref;
  6150. }
  6151. if ((ctx_isp->offline_context) || (ctx_isp->vfps_aux_context)) {
  6152. __cam_isp_ctx_enqueue_request_in_order(ctx, req, true);
  6153. } else if (ctx->ctx_crm_intf->add_req) {
  6154. memset(&add_req, 0, sizeof(add_req));
  6155. add_req.link_hdl = ctx->link_hdl;
  6156. add_req.dev_hdl = ctx->dev_hdl;
  6157. add_req.req_id = req->request_id;
  6158. if (ctx_isp->is_shdr_master && req_isp->hw_update_data.mup_en)
  6159. add_req.num_exp = req_isp->hw_update_data.num_exp;
  6160. rc = ctx->ctx_crm_intf->add_req(&add_req);
  6161. if (rc) {
  6162. if (rc == -EBADR)
  6163. CAM_INFO(CAM_ISP,
  6164. "Add req failed: req id=%llu, it has been flushed on link 0x%x ctx %u",
  6165. req->request_id, ctx->link_hdl, ctx->ctx_id);
  6166. else
  6167. CAM_ERR(CAM_ISP,
  6168. "Add req failed: req id=%llu on link 0x%x ctx %u",
  6169. req->request_id, ctx->link_hdl, ctx->ctx_id);
  6170. } else {
  6171. __cam_isp_ctx_enqueue_request_in_order(ctx, req, true);
  6172. }
  6173. } else {
  6174. CAM_ERR(CAM_ISP, "Unable to add request: req id=%llu,ctx: %u,link: 0x%x",
  6175. req->request_id, ctx->ctx_id, ctx->link_hdl);
  6176. rc = -ENODEV;
  6177. }
  6178. }
  6179. if (rc)
  6180. goto put_ref;
  6181. CAM_DBG(CAM_REQ,
  6182. "Preprocessing Config req_id %lld successful on ctx %u, link: 0x%x",
  6183. req->request_id, ctx->ctx_id, ctx->link_hdl);
  6184. if (ctx_isp->offline_context && atomic_read(&ctx_isp->rxd_epoch))
  6185. __cam_isp_ctx_schedule_apply_req(ctx_isp);
  6186. else if (ctx_isp->vfps_aux_context &&
  6187. (req_isp->hw_update_data.packet_opcode_type != CAM_ISP_PACKET_INIT_DEV))
  6188. __cam_isp_ctx_schedule_apply_req(ctx_isp);
  6189. return rc;
  6190. put_ref:
  6191. for (--i; i >= 0; i--) {
  6192. if (cam_sync_put_obj_ref(req_isp->fence_map_out[i].sync_id))
  6193. CAM_ERR(CAM_CTXT, "Failed to put ref of fence %d, ctx_idx: %u, link: 0x%x",
  6194. req_isp->fence_map_out[i].sync_id, ctx->ctx_id, ctx->link_hdl);
  6195. }
  6196. free_req_and_buf_tracker_list:
  6197. cam_smmu_buffer_tracker_putref(&req->buf_tracker);
  6198. free_req:
  6199. spin_lock_bh(&ctx->lock);
  6200. list_add_tail(&req->list, &ctx->free_req_list);
  6201. spin_unlock_bh(&ctx->lock);
  6202. return rc;
  6203. }
  6204. static int __cam_isp_ctx_allocate_mem_hw_entries(
  6205. struct cam_context *ctx,
  6206. struct cam_hw_acquire_args *param)
  6207. {
  6208. int rc = 0, i;
  6209. uint32_t max_res = 0;
  6210. uint32_t max_hw_upd_entries = CAM_ISP_CTX_CFG_MAX;
  6211. struct cam_ctx_request *req;
  6212. struct cam_ctx_request *temp_req;
  6213. struct cam_isp_ctx_req *req_isp;
  6214. if (!param->op_params.param_list[0])
  6215. max_res = CAM_ISP_CTX_RES_MAX;
  6216. else {
  6217. max_res = param->op_params.param_list[0];
  6218. if (param->op_flags & CAM_IFE_CTX_SFE_EN) {
  6219. max_res += param->op_params.param_list[1];
  6220. max_hw_upd_entries = CAM_ISP_SFE_CTX_CFG_MAX;
  6221. }
  6222. }
  6223. ctx->max_in_map_entries = max_res;
  6224. ctx->max_out_map_entries = max_res;
  6225. ctx->max_hw_update_entries = max_hw_upd_entries;
  6226. CAM_DBG(CAM_ISP,
  6227. "Allocate max_entries: 0x%x max_res: 0x%x is_sfe_en: %d, ctx: %u, link: 0x%x",
  6228. max_hw_upd_entries, max_res, (param->op_flags & CAM_IFE_CTX_SFE_EN),
  6229. ctx->ctx_id, ctx->link_hdl);
  6230. ctx->hw_update_entry = kcalloc(CAM_ISP_CTX_REQ_MAX, sizeof(struct cam_hw_update_entry *),
  6231. GFP_KERNEL);
  6232. if (!ctx->hw_update_entry) {
  6233. CAM_ERR(CAM_CTXT, "%s[%u] no memory, link: 0x%x",
  6234. ctx->dev_name, ctx->ctx_id, ctx->link_hdl);
  6235. return -ENOMEM;
  6236. }
  6237. for (i = 0; i < CAM_ISP_CTX_REQ_MAX; i++) {
  6238. ctx->hw_update_entry[i] = kcalloc(ctx->max_hw_update_entries,
  6239. sizeof(struct cam_hw_update_entry), GFP_KERNEL);
  6240. if (!ctx->hw_update_entry[i]) {
  6241. CAM_ERR(CAM_CTXT, "%s[%u] no memory for hw_update_entry: %u, link: 0x%x",
  6242. ctx->dev_name, ctx->ctx_id, i, ctx->link_hdl);
  6243. return -ENOMEM;
  6244. }
  6245. }
  6246. ctx->in_map_entries = kcalloc(CAM_ISP_CTX_REQ_MAX, sizeof(struct cam_hw_fence_map_entry *),
  6247. GFP_KERNEL);
  6248. if (!ctx->in_map_entries) {
  6249. CAM_ERR(CAM_CTXT, "%s[%u] no memory for in_map_entries, link: 0x%x",
  6250. ctx->dev_name, ctx->ctx_id, ctx->link_hdl);
  6251. rc = -ENOMEM;
  6252. goto end;
  6253. }
  6254. for (i = 0; i < CAM_ISP_CTX_REQ_MAX; i++) {
  6255. ctx->in_map_entries[i] = kcalloc(ctx->max_in_map_entries,
  6256. sizeof(struct cam_hw_fence_map_entry),
  6257. GFP_KERNEL);
  6258. if (!ctx->in_map_entries[i]) {
  6259. CAM_ERR(CAM_CTXT, "%s[%u] no memory for in_map_entries: %u, link: 0x%x",
  6260. ctx->dev_name, ctx->ctx_id, i, ctx->link_hdl);
  6261. rc = -ENOMEM;
  6262. goto end;
  6263. }
  6264. }
  6265. ctx->out_map_entries = kcalloc(CAM_ISP_CTX_REQ_MAX, sizeof(struct cam_hw_fence_map_entry *),
  6266. GFP_KERNEL);
  6267. if (!ctx->out_map_entries) {
  6268. CAM_ERR(CAM_CTXT, "%s[%u] no memory for out_map_entries, link: 0x%x",
  6269. ctx->dev_name, ctx->ctx_id, ctx->link_hdl);
  6270. rc = -ENOMEM;
  6271. goto end;
  6272. }
  6273. for (i = 0; i < CAM_ISP_CTX_REQ_MAX; i++) {
  6274. ctx->out_map_entries[i] = kcalloc(ctx->max_out_map_entries,
  6275. sizeof(struct cam_hw_fence_map_entry),
  6276. GFP_KERNEL);
  6277. if (!ctx->out_map_entries[i]) {
  6278. CAM_ERR(CAM_CTXT, "%s[%u] no memory for out_map_entries: %u, link: 0x%x",
  6279. ctx->dev_name, ctx->ctx_id, i, ctx->link_hdl);
  6280. rc = -ENOMEM;
  6281. goto end;
  6282. }
  6283. }
  6284. list_for_each_entry_safe(req, temp_req,
  6285. &ctx->free_req_list, list) {
  6286. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  6287. req_isp->cfg = ctx->hw_update_entry[req->index];
  6288. req_isp->fence_map_in = ctx->in_map_entries[req->index];
  6289. req_isp->fence_map_out = ctx->out_map_entries[req->index];
  6290. }
  6291. return rc;
  6292. end:
  6293. __cam_isp_ctx_free_mem_hw_entries(ctx);
  6294. return rc;
  6295. }
  6296. static int __cam_isp_ctx_acquire_dev_in_available(struct cam_context *ctx,
  6297. struct cam_acquire_dev_cmd *cmd)
  6298. {
  6299. int rc = 0;
  6300. int i;
  6301. struct cam_hw_acquire_args param;
  6302. struct cam_isp_resource *isp_res = NULL;
  6303. struct cam_create_dev_hdl req_hdl_param;
  6304. struct cam_hw_release_args release;
  6305. struct cam_isp_context *ctx_isp =
  6306. (struct cam_isp_context *) ctx->ctx_priv;
  6307. struct cam_hw_cmd_args hw_cmd_args;
  6308. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  6309. if (!ctx->hw_mgr_intf) {
  6310. CAM_ERR(CAM_ISP, "HW interface is not ready, ctx_idx: %u, link: 0x%x",
  6311. ctx->ctx_id, ctx->link_hdl);
  6312. rc = -EFAULT;
  6313. goto end;
  6314. }
  6315. CAM_DBG(CAM_ISP,
  6316. "session_hdl 0x%x, num_resources %d, hdl type %d, res %lld, ctx_idx: %u, link: 0x%x",
  6317. cmd->session_handle, cmd->num_resources,
  6318. cmd->handle_type, cmd->resource_hdl, ctx->ctx_id, ctx->link_hdl);
  6319. ctx_isp->v4l2_event_sub_ids = cam_req_mgr_get_id_subscribed();
  6320. if (cmd->num_resources == CAM_API_COMPAT_CONSTANT) {
  6321. ctx_isp->split_acquire = true;
  6322. CAM_DBG(CAM_ISP, "Acquire dev handle, ctx_idx: %u, link: 0x%x",
  6323. ctx->ctx_id, ctx->link_hdl);
  6324. goto get_dev_handle;
  6325. }
  6326. if (cmd->num_resources > CAM_ISP_CTX_RES_MAX) {
  6327. CAM_ERR(CAM_ISP, "Too much resources in the acquire, ctx_idx: %u, link: 0x%x",
  6328. ctx->ctx_id, ctx->link_hdl);
  6329. rc = -ENOMEM;
  6330. goto end;
  6331. }
  6332. /* for now we only support user pointer */
  6333. if (cmd->handle_type != 1) {
  6334. CAM_ERR(CAM_ISP, "Only user pointer is supported, ctx_idx: %u, link: 0x%x",
  6335. ctx->ctx_id, ctx->link_hdl);
  6336. rc = -EINVAL;
  6337. goto end;
  6338. }
  6339. isp_res = kzalloc(
  6340. sizeof(*isp_res)*cmd->num_resources, GFP_KERNEL);
  6341. if (!isp_res) {
  6342. rc = -ENOMEM;
  6343. goto end;
  6344. }
  6345. CAM_DBG(CAM_ISP, "start copy %d resources from user, ctx_idx: %u, link: 0x%x",
  6346. cmd->num_resources, ctx->ctx_id, ctx->link_hdl);
  6347. if (copy_from_user(isp_res, u64_to_user_ptr(cmd->resource_hdl),
  6348. sizeof(*isp_res)*cmd->num_resources)) {
  6349. rc = -EFAULT;
  6350. goto free_res;
  6351. }
  6352. memset(&param, 0, sizeof(param));
  6353. param.context_data = ctx;
  6354. param.event_cb = ctx->irq_cb_intf;
  6355. param.sec_pf_evt_cb = cam_context_dump_pf_info;
  6356. param.num_acq = cmd->num_resources;
  6357. param.acquire_info = (uintptr_t) isp_res;
  6358. rc = __cam_isp_ctx_allocate_mem_hw_entries(ctx, &param);
  6359. if (rc) {
  6360. CAM_ERR(CAM_ISP, "Ctx[%u] link: 0x%x allocate hw entry fail",
  6361. ctx->ctx_id, ctx->link_hdl);
  6362. goto free_res;
  6363. }
  6364. /* call HW manager to reserve the resource */
  6365. rc = ctx->hw_mgr_intf->hw_acquire(ctx->hw_mgr_intf->hw_mgr_priv,
  6366. &param);
  6367. if (rc != 0) {
  6368. CAM_ERR(CAM_ISP, "Acquire device failed, ctx_idx: %u, link: 0x%x",
  6369. ctx->ctx_id, ctx->link_hdl);
  6370. goto free_res;
  6371. }
  6372. /* Query the context has rdi only resource */
  6373. hw_cmd_args.ctxt_to_hw_map = param.ctxt_to_hw_map;
  6374. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  6375. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_CMD_CTX_TYPE;
  6376. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  6377. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  6378. &hw_cmd_args);
  6379. if (rc) {
  6380. CAM_ERR(CAM_ISP, "HW command failed, ctx_idx: %u, link: 0x%x",
  6381. ctx->ctx_id, ctx->link_hdl);
  6382. goto free_hw;
  6383. }
  6384. if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_RDI) {
  6385. /*
  6386. * this context has rdi only resource assign rdi only
  6387. * state machine
  6388. */
  6389. CAM_DBG(CAM_ISP, "RDI only session Context, ctx_idx: %u, link: 0x%x",
  6390. ctx->ctx_id, ctx->link_hdl);
  6391. ctx_isp->substate_machine_irq =
  6392. cam_isp_ctx_rdi_only_activated_state_machine_irq;
  6393. ctx_isp->substate_machine =
  6394. cam_isp_ctx_rdi_only_activated_state_machine;
  6395. ctx_isp->rdi_only_context = true;
  6396. } else if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_FS2) {
  6397. CAM_DBG(CAM_ISP, "FS2 Session has PIX, RD and RDI, ctx_idx: %u, link: 0x%x",
  6398. ctx->ctx_id, ctx->link_hdl);
  6399. ctx_isp->substate_machine_irq =
  6400. cam_isp_ctx_fs2_state_machine_irq;
  6401. ctx_isp->substate_machine =
  6402. cam_isp_ctx_fs2_state_machine;
  6403. } else if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_OFFLINE) {
  6404. CAM_DBG(CAM_ISP,
  6405. "offline Session has PIX and RD resources, ctx_idx: %u, link: 0x%x",
  6406. ctx->ctx_id, ctx->link_hdl);
  6407. ctx_isp->substate_machine_irq =
  6408. cam_isp_ctx_offline_state_machine_irq;
  6409. } else {
  6410. CAM_DBG(CAM_ISP,
  6411. "Session has PIX or PIX and RDI resources, ctx_idx: %u, link: 0x%x",
  6412. ctx->ctx_id, ctx->link_hdl);
  6413. ctx_isp->substate_machine_irq =
  6414. cam_isp_ctx_activated_state_machine_irq;
  6415. ctx_isp->substate_machine =
  6416. cam_isp_ctx_activated_state_machine;
  6417. }
  6418. ctx_isp->hw_ctx = param.ctxt_to_hw_map;
  6419. ctx_isp->hw_acquired = true;
  6420. ctx_isp->split_acquire = false;
  6421. ctx->ctxt_to_hw_map = param.ctxt_to_hw_map;
  6422. atomic64_set(&ctx_isp->dbg_monitors.state_monitor_head, -1);
  6423. atomic64_set(&ctx_isp->dbg_monitors.frame_monitor_head, -1);
  6424. for (i = 0; i < CAM_ISP_CTX_EVENT_MAX; i++)
  6425. atomic64_set(&ctx_isp->dbg_monitors.event_record_head[i], -1);
  6426. CAM_INFO(CAM_ISP, "Ctx_type: %u, ctx_id: %u, hw_mgr_ctx: %u", isp_hw_cmd_args.u.ctx_type,
  6427. ctx->ctx_id, param.hw_mgr_ctx_id);
  6428. kfree(isp_res);
  6429. isp_res = NULL;
  6430. get_dev_handle:
  6431. req_hdl_param.session_hdl = cmd->session_handle;
  6432. /* bridge is not ready for these flags. so false for now */
  6433. req_hdl_param.v4l2_sub_dev_flag = 0;
  6434. req_hdl_param.media_entity_flag = 0;
  6435. req_hdl_param.ops = ctx->crm_ctx_intf;
  6436. req_hdl_param.priv = ctx;
  6437. req_hdl_param.dev_id = CAM_ISP;
  6438. CAM_DBG(CAM_ISP, "get device handle form bridge, ctx_idx: %u, link: 0x%x",
  6439. ctx->ctx_id, ctx->link_hdl);
  6440. ctx->dev_hdl = cam_create_device_hdl(&req_hdl_param);
  6441. if (ctx->dev_hdl <= 0) {
  6442. rc = -EFAULT;
  6443. CAM_ERR(CAM_ISP, "Can not create device handle, ctx_idx: %u, link: 0x%x",
  6444. ctx->ctx_id, ctx->link_hdl);
  6445. goto free_hw;
  6446. }
  6447. cmd->dev_handle = ctx->dev_hdl;
  6448. /* store session information */
  6449. ctx->session_hdl = cmd->session_handle;
  6450. ctx->state = CAM_CTX_ACQUIRED;
  6451. trace_cam_context_state("ISP", ctx);
  6452. CAM_INFO(CAM_ISP,
  6453. "Acquire success: session_hdl 0x%x num_rsrces %d ctx %u link: 0x%x",
  6454. cmd->session_handle, cmd->num_resources, ctx->ctx_id, ctx->link_hdl);
  6455. return rc;
  6456. free_hw:
  6457. release.ctxt_to_hw_map = ctx_isp->hw_ctx;
  6458. if (ctx_isp->hw_acquired)
  6459. ctx->hw_mgr_intf->hw_release(ctx->hw_mgr_intf->hw_mgr_priv,
  6460. &release);
  6461. ctx_isp->hw_ctx = NULL;
  6462. ctx_isp->hw_acquired = false;
  6463. free_res:
  6464. kfree(isp_res);
  6465. end:
  6466. return rc;
  6467. }
  6468. static int __cam_isp_ctx_acquire_hw_v1(struct cam_context *ctx,
  6469. void *args)
  6470. {
  6471. int rc = 0;
  6472. int i;
  6473. struct cam_acquire_hw_cmd_v1 *cmd =
  6474. (struct cam_acquire_hw_cmd_v1 *)args;
  6475. struct cam_hw_acquire_args param;
  6476. struct cam_hw_release_args release;
  6477. struct cam_isp_context *ctx_isp =
  6478. (struct cam_isp_context *) ctx->ctx_priv;
  6479. struct cam_hw_cmd_args hw_cmd_args;
  6480. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  6481. struct cam_isp_acquire_hw_info *acquire_hw_info = NULL;
  6482. if (!ctx->hw_mgr_intf) {
  6483. CAM_ERR(CAM_ISP, "HW interface is not ready, ctx %u link: 0x%x",
  6484. ctx->ctx_id, ctx->link_hdl);
  6485. rc = -EFAULT;
  6486. goto end;
  6487. }
  6488. CAM_DBG(CAM_ISP,
  6489. "session_hdl 0x%x, hdl type %d, res %lld ctx %u link: 0x%x",
  6490. cmd->session_handle, cmd->handle_type, cmd->resource_hdl,
  6491. ctx->ctx_id, ctx->link_hdl);
  6492. /* for now we only support user pointer */
  6493. if (cmd->handle_type != 1) {
  6494. CAM_ERR(CAM_ISP, "Only user pointer is supported, ctx %u link: 0x%x",
  6495. ctx->ctx_id, ctx->link_hdl);
  6496. rc = -EINVAL;
  6497. goto end;
  6498. }
  6499. if (cmd->data_size < sizeof(*acquire_hw_info)) {
  6500. CAM_ERR(CAM_ISP, "data_size is not a valid value, ctx %u link: 0x%x",
  6501. ctx->ctx_id, ctx->link_hdl);
  6502. goto end;
  6503. }
  6504. acquire_hw_info = kzalloc(cmd->data_size, GFP_KERNEL);
  6505. if (!acquire_hw_info) {
  6506. rc = -ENOMEM;
  6507. goto end;
  6508. }
  6509. CAM_DBG(CAM_ISP, "start copy resources from user, ctx_idx: %u, link: 0x%x",
  6510. ctx->ctx_id, ctx->link_hdl);
  6511. if (copy_from_user(acquire_hw_info, (void __user *)cmd->resource_hdl,
  6512. cmd->data_size)) {
  6513. rc = -EFAULT;
  6514. goto free_res;
  6515. }
  6516. memset(&param, 0, sizeof(param));
  6517. param.context_data = ctx;
  6518. param.event_cb = ctx->irq_cb_intf;
  6519. param.sec_pf_evt_cb = cam_context_dump_pf_info;
  6520. param.num_acq = CAM_API_COMPAT_CONSTANT;
  6521. param.acquire_info_size = cmd->data_size;
  6522. param.acquire_info = (uint64_t) acquire_hw_info;
  6523. param.mini_dump_cb = __cam_isp_ctx_minidump_cb;
  6524. rc = __cam_isp_ctx_allocate_mem_hw_entries(ctx,
  6525. &param);
  6526. if (rc) {
  6527. CAM_ERR(CAM_ISP, "Ctx[%u] link: 0x%x allocate hw entry fail",
  6528. ctx->ctx_id, ctx->link_hdl);
  6529. goto free_res;
  6530. }
  6531. /* call HW manager to reserve the resource */
  6532. rc = ctx->hw_mgr_intf->hw_acquire(ctx->hw_mgr_intf->hw_mgr_priv,
  6533. &param);
  6534. if (rc != 0) {
  6535. CAM_ERR(CAM_ISP, "Acquire device failed, ctx_idx: %u, link: 0x%x",
  6536. ctx->ctx_id, ctx->link_hdl);
  6537. goto free_res;
  6538. }
  6539. ctx_isp->support_consumed_addr =
  6540. (param.op_flags & CAM_IFE_CTX_CONSUME_ADDR_EN);
  6541. /* Query the context has rdi only resource */
  6542. hw_cmd_args.ctxt_to_hw_map = param.ctxt_to_hw_map;
  6543. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  6544. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_CMD_CTX_TYPE;
  6545. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  6546. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  6547. &hw_cmd_args);
  6548. if (rc) {
  6549. CAM_ERR(CAM_ISP, "HW command failed, ctx_idx: %u, link: 0x%x",
  6550. ctx->ctx_id, ctx->link_hdl);
  6551. goto free_hw;
  6552. }
  6553. if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_RDI) {
  6554. /*
  6555. * this context has rdi only resource assign rdi only
  6556. * state machine
  6557. */
  6558. CAM_DBG(CAM_ISP, "RDI only session Context, ctx_idx: %u, link: 0x%x",
  6559. ctx->ctx_id, ctx->link_hdl);
  6560. ctx_isp->substate_machine_irq =
  6561. cam_isp_ctx_rdi_only_activated_state_machine_irq;
  6562. ctx_isp->substate_machine =
  6563. cam_isp_ctx_rdi_only_activated_state_machine;
  6564. ctx_isp->rdi_only_context = true;
  6565. } else if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_FS2) {
  6566. CAM_DBG(CAM_ISP, "FS2 Session has PIX, RD and RDI, ctx_idx: %u, link: 0x%x",
  6567. ctx->ctx_id, ctx->link_hdl);
  6568. ctx_isp->substate_machine_irq =
  6569. cam_isp_ctx_fs2_state_machine_irq;
  6570. ctx_isp->substate_machine =
  6571. cam_isp_ctx_fs2_state_machine;
  6572. } else if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_OFFLINE) {
  6573. CAM_DBG(CAM_ISP, "Offline session has PIX and RD resources, ctx: %u, link: 0x%x",
  6574. ctx->ctx_id, ctx->link_hdl);
  6575. ctx_isp->substate_machine_irq =
  6576. cam_isp_ctx_offline_state_machine_irq;
  6577. ctx_isp->substate_machine = NULL;
  6578. } else {
  6579. CAM_DBG(CAM_ISP, "Session has PIX or PIX and RDI resources, ctx: %u, link: 0x%x",
  6580. ctx->ctx_id, ctx->link_hdl);
  6581. ctx_isp->substate_machine_irq =
  6582. cam_isp_ctx_activated_state_machine_irq;
  6583. ctx_isp->substate_machine =
  6584. cam_isp_ctx_activated_state_machine;
  6585. }
  6586. ctx_isp->hw_ctx = param.ctxt_to_hw_map;
  6587. ctx_isp->hw_acquired = true;
  6588. ctx->ctxt_to_hw_map = param.ctxt_to_hw_map;
  6589. atomic64_set(&ctx_isp->dbg_monitors.state_monitor_head, -1);
  6590. atomic64_set(&ctx_isp->dbg_monitors.frame_monitor_head, -1);
  6591. for (i = 0; i < CAM_ISP_CTX_EVENT_MAX; i++)
  6592. atomic64_set(&ctx_isp->dbg_monitors.event_record_head[i], -1);
  6593. trace_cam_context_state("ISP", ctx);
  6594. CAM_INFO(CAM_ISP,
  6595. "Acquire success:session_hdl 0x%xs ctx_type %d ctx %u link: 0x%x hw_mgr_ctx: %u",
  6596. ctx->session_hdl, isp_hw_cmd_args.u.ctx_type, ctx->ctx_id, ctx->link_hdl,
  6597. param.hw_mgr_ctx_id);
  6598. kfree(acquire_hw_info);
  6599. return rc;
  6600. free_hw:
  6601. release.ctxt_to_hw_map = ctx_isp->hw_ctx;
  6602. ctx->hw_mgr_intf->hw_release(ctx->hw_mgr_intf->hw_mgr_priv, &release);
  6603. ctx_isp->hw_ctx = NULL;
  6604. ctx_isp->hw_acquired = false;
  6605. free_res:
  6606. kfree(acquire_hw_info);
  6607. end:
  6608. return rc;
  6609. }
  6610. static void cam_req_mgr_process_workq_apply_req_worker(struct work_struct *w)
  6611. {
  6612. cam_req_mgr_process_workq(w);
  6613. }
  6614. static int __cam_isp_ctx_acquire_hw_v2(struct cam_context *ctx,
  6615. void *args)
  6616. {
  6617. int rc = 0, i, j;
  6618. struct cam_acquire_hw_cmd_v2 *cmd =
  6619. (struct cam_acquire_hw_cmd_v2 *)args;
  6620. struct cam_hw_acquire_args param;
  6621. struct cam_hw_release_args release;
  6622. struct cam_isp_context *ctx_isp =
  6623. (struct cam_isp_context *) ctx->ctx_priv;
  6624. struct cam_hw_cmd_args hw_cmd_args;
  6625. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  6626. struct cam_isp_acquire_hw_info *acquire_hw_info = NULL;
  6627. struct cam_isp_comp_record_query query_cmd;
  6628. if (!ctx->hw_mgr_intf) {
  6629. CAM_ERR(CAM_ISP, "HW interface is not ready, ctx_id %u link: 0x%x",
  6630. ctx->ctx_id, ctx->link_hdl);
  6631. rc = -EFAULT;
  6632. goto end;
  6633. }
  6634. CAM_DBG(CAM_ISP,
  6635. "session_hdl 0x%x, hdl type %d, res %lld, ctx_id %u link: 0x%x",
  6636. cmd->session_handle, cmd->handle_type, cmd->resource_hdl,
  6637. ctx->ctx_id, ctx->link_hdl);
  6638. /* for now we only support user pointer */
  6639. if (cmd->handle_type != 1) {
  6640. CAM_ERR(CAM_ISP, "Only user pointer is supported, ctx_id %u link: 0x%x",
  6641. ctx->ctx_id, ctx->link_hdl);
  6642. rc = -EINVAL;
  6643. goto end;
  6644. }
  6645. if (cmd->data_size < sizeof(*acquire_hw_info)) {
  6646. CAM_ERR(CAM_ISP, "data_size is not a valid value, ctx_id %u link: 0x%x",
  6647. ctx->ctx_id, ctx->link_hdl);
  6648. goto end;
  6649. }
  6650. acquire_hw_info = kzalloc(cmd->data_size, GFP_KERNEL);
  6651. if (!acquire_hw_info) {
  6652. rc = -ENOMEM;
  6653. goto end;
  6654. }
  6655. CAM_DBG(CAM_ISP, "start copy resources from user, ctx_id %u link: 0x%x",
  6656. ctx->ctx_id, ctx->link_hdl);
  6657. if (copy_from_user(acquire_hw_info, (void __user *)cmd->resource_hdl,
  6658. cmd->data_size)) {
  6659. rc = -EFAULT;
  6660. goto free_res;
  6661. }
  6662. memset(&param, 0, sizeof(param));
  6663. param.context_data = ctx;
  6664. param.event_cb = ctx->irq_cb_intf;
  6665. param.sec_pf_evt_cb = cam_context_dump_pf_info;
  6666. param.num_acq = CAM_API_COMPAT_CONSTANT;
  6667. param.acquire_info_size = cmd->data_size;
  6668. param.acquire_info = (uint64_t) acquire_hw_info;
  6669. param.mini_dump_cb = __cam_isp_ctx_minidump_cb;
  6670. /* call HW manager to reserve the resource */
  6671. rc = ctx->hw_mgr_intf->hw_acquire(ctx->hw_mgr_intf->hw_mgr_priv,
  6672. &param);
  6673. if (rc != 0) {
  6674. CAM_ERR(CAM_ISP, "Acquire device failed, ctx_id %u link: 0x%x",
  6675. ctx->ctx_id, ctx->link_hdl);
  6676. goto free_res;
  6677. }
  6678. rc = __cam_isp_ctx_allocate_mem_hw_entries(ctx, &param);
  6679. if (rc) {
  6680. CAM_ERR(CAM_ISP, "Ctx[%u] link: 0x%x allocate hw entry fail",
  6681. ctx->ctx_id, ctx->link_hdl);
  6682. goto free_hw;
  6683. }
  6684. /*
  6685. * Set feature flag if applicable
  6686. * custom hw is supported only on v2
  6687. */
  6688. ctx_isp->custom_enabled =
  6689. (param.op_flags & CAM_IFE_CTX_CUSTOM_EN);
  6690. ctx_isp->use_frame_header_ts =
  6691. (param.op_flags & CAM_IFE_CTX_FRAME_HEADER_EN);
  6692. ctx_isp->use_default_apply =
  6693. (param.op_flags & CAM_IFE_CTX_APPLY_DEFAULT_CFG);
  6694. ctx_isp->support_consumed_addr =
  6695. (param.op_flags & CAM_IFE_CTX_CONSUME_ADDR_EN);
  6696. ctx_isp->aeb_enabled =
  6697. (param.op_flags & CAM_IFE_CTX_AEB_EN);
  6698. ctx_isp->mode_switch_en =
  6699. (param.op_flags & CAM_IFE_CTX_DYNAMIC_SWITCH_EN);
  6700. ctx_isp->is_tfe_shdr = (param.op_flags & CAM_IFE_CTX_SHDR_EN);
  6701. ctx_isp->is_shdr_master = (param.op_flags & CAM_IFE_CTX_SHDR_IS_MASTER);
  6702. /* Query the context bus comp group information */
  6703. ctx_isp->vfe_bus_comp_grp = kcalloc(CAM_IFE_BUS_COMP_NUM_MAX,
  6704. sizeof(struct cam_isp_context_comp_record), GFP_KERNEL);
  6705. if (!ctx_isp->vfe_bus_comp_grp) {
  6706. CAM_ERR(CAM_CTXT, "%s[%d] no memory for vfe_bus_comp_grp",
  6707. ctx->dev_name, ctx->ctx_id);
  6708. rc = -ENOMEM;
  6709. goto end;
  6710. }
  6711. if (param.op_flags & CAM_IFE_CTX_SFE_EN) {
  6712. ctx_isp->sfe_bus_comp_grp = kcalloc(CAM_SFE_BUS_COMP_NUM_MAX,
  6713. sizeof(struct cam_isp_context_comp_record), GFP_KERNEL);
  6714. if (!ctx_isp->sfe_bus_comp_grp) {
  6715. CAM_ERR(CAM_CTXT, "%s[%d] no memory for sfe_bus_comp_grp",
  6716. ctx->dev_name, ctx->ctx_id);
  6717. rc = -ENOMEM;
  6718. goto end;
  6719. }
  6720. }
  6721. query_cmd.vfe_bus_comp_grp = ctx_isp->vfe_bus_comp_grp;
  6722. if (ctx_isp->sfe_bus_comp_grp)
  6723. query_cmd.sfe_bus_comp_grp = ctx_isp->sfe_bus_comp_grp;
  6724. hw_cmd_args.ctxt_to_hw_map = param.ctxt_to_hw_map;
  6725. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  6726. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_GET_BUS_COMP_GROUP;
  6727. isp_hw_cmd_args.cmd_data = &query_cmd;
  6728. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  6729. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  6730. &hw_cmd_args);
  6731. if (rc) {
  6732. CAM_ERR(CAM_ISP, "HW command failed");
  6733. goto free_hw;
  6734. }
  6735. /* Query the context has rdi only resource */
  6736. hw_cmd_args.ctxt_to_hw_map = param.ctxt_to_hw_map;
  6737. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  6738. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_CMD_CTX_TYPE;
  6739. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  6740. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  6741. &hw_cmd_args);
  6742. if (rc) {
  6743. CAM_ERR(CAM_ISP, "HW command failed, ctx_id %u link: 0x%x",
  6744. ctx->ctx_id, ctx->link_hdl);
  6745. goto free_hw;
  6746. }
  6747. if (param.valid_acquired_hw) {
  6748. for (i = 0; i < CAM_MAX_ACQ_RES; i++)
  6749. cmd->hw_info.acquired_hw_id[i] =
  6750. param.acquired_hw_id[i];
  6751. for (i = 0; i < CAM_MAX_ACQ_RES; i++)
  6752. for (j = 0; j < CAM_MAX_HW_SPLIT; j++)
  6753. cmd->hw_info.acquired_hw_path[i][j] =
  6754. param.acquired_hw_path[i][j];
  6755. ctx_isp->hw_idx = param.acquired_hw_id[0];
  6756. }
  6757. cmd->hw_info.valid_acquired_hw =
  6758. param.valid_acquired_hw;
  6759. cmd->hw_info.valid_acquired_hw = param.valid_acquired_hw;
  6760. if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_RDI) {
  6761. /*
  6762. * this context has rdi only resource assign rdi only
  6763. * state machine
  6764. */
  6765. CAM_DBG(CAM_ISP, "RDI only session Context, ctx_id %u link: 0x%x",
  6766. ctx->ctx_id, ctx->link_hdl);
  6767. ctx_isp->substate_machine_irq =
  6768. cam_isp_ctx_rdi_only_activated_state_machine_irq;
  6769. ctx_isp->substate_machine =
  6770. cam_isp_ctx_rdi_only_activated_state_machine;
  6771. ctx_isp->rdi_only_context = true;
  6772. } else if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_FS2) {
  6773. CAM_DBG(CAM_ISP, "FS2 Session has PIX, RD and RDI, ctx_id %u link: 0x%x",
  6774. ctx->ctx_id, ctx->link_hdl);
  6775. ctx_isp->substate_machine_irq =
  6776. cam_isp_ctx_fs2_state_machine_irq;
  6777. ctx_isp->substate_machine =
  6778. cam_isp_ctx_fs2_state_machine;
  6779. } else if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_OFFLINE) {
  6780. CAM_DBG(CAM_ISP, "Offline Session has PIX and RD resources, ctx_id %u link: 0x%x",
  6781. ctx->ctx_id, ctx->link_hdl);
  6782. ctx_isp->substate_machine_irq =
  6783. cam_isp_ctx_offline_state_machine_irq;
  6784. ctx_isp->substate_machine = NULL;
  6785. ctx_isp->offline_context = true;
  6786. } else {
  6787. CAM_DBG(CAM_ISP, "Session has PIX or PIX and RDI resources, ctx_id %u link: 0x%x",
  6788. ctx->ctx_id, ctx->link_hdl);
  6789. ctx_isp->substate_machine_irq =
  6790. cam_isp_ctx_activated_state_machine_irq;
  6791. ctx_isp->substate_machine =
  6792. cam_isp_ctx_activated_state_machine;
  6793. }
  6794. if (ctx_isp->offline_context || ctx_isp->vfps_aux_context) {
  6795. rc = cam_req_mgr_workq_create("ife_apply_req", 20,
  6796. &ctx_isp->workq, CRM_WORKQ_USAGE_IRQ, 0,
  6797. cam_req_mgr_process_workq_apply_req_worker);
  6798. if (rc)
  6799. CAM_ERR(CAM_ISP,
  6800. "Failed to create workq for IFE rc:%d offline: %s vfps: %s ctx_id %u link: 0x%x",
  6801. rc, CAM_BOOL_TO_YESNO(ctx_isp->offline_context),
  6802. CAM_BOOL_TO_YESNO(ctx_isp->vfps_aux_context),
  6803. ctx->ctx_id, ctx->link_hdl);
  6804. }
  6805. ctx_isp->hw_ctx = param.ctxt_to_hw_map;
  6806. ctx_isp->hw_acquired = true;
  6807. ctx->ctxt_to_hw_map = param.ctxt_to_hw_map;
  6808. ctx->hw_mgr_ctx_id = param.hw_mgr_ctx_id;
  6809. snprintf(ctx->ctx_id_string, sizeof(ctx->ctx_id_string),
  6810. "%s_ctx[%d]_hwmgrctx[%d]_hwidx[0x%x]",
  6811. ctx->dev_name,
  6812. ctx->ctx_id,
  6813. ctx->hw_mgr_ctx_id,
  6814. ctx_isp->hw_idx);
  6815. trace_cam_context_state("ISP", ctx);
  6816. CAM_INFO(CAM_ISP,
  6817. "Acquire success: session_hdl 0x%xs ctx_type %d ctx %u link: 0x%x hw_mgr_ctx: %u",
  6818. ctx->session_hdl, isp_hw_cmd_args.u.ctx_type, ctx->ctx_id, ctx->link_hdl,
  6819. param.hw_mgr_ctx_id);
  6820. kfree(acquire_hw_info);
  6821. return rc;
  6822. free_hw:
  6823. release.ctxt_to_hw_map = ctx_isp->hw_ctx;
  6824. ctx->hw_mgr_intf->hw_release(ctx->hw_mgr_intf->hw_mgr_priv, &release);
  6825. ctx_isp->hw_ctx = NULL;
  6826. ctx_isp->hw_acquired = false;
  6827. free_res:
  6828. kfree(acquire_hw_info);
  6829. end:
  6830. return rc;
  6831. }
  6832. static int __cam_isp_ctx_acquire_hw_in_acquired(struct cam_context *ctx,
  6833. void *args)
  6834. {
  6835. int rc = -EINVAL;
  6836. uint32_t api_version;
  6837. if (!ctx || !args) {
  6838. CAM_ERR(CAM_ISP, "Invalid input pointer");
  6839. return rc;
  6840. }
  6841. api_version = *((uint32_t *)args);
  6842. if (api_version == 1)
  6843. rc = __cam_isp_ctx_acquire_hw_v1(ctx, args);
  6844. else if (api_version == 2)
  6845. rc = __cam_isp_ctx_acquire_hw_v2(ctx, args);
  6846. else
  6847. CAM_ERR(CAM_ISP, "Unsupported api version %d, ctx_id %u link: 0x%x",
  6848. api_version, ctx->ctx_id, ctx->link_hdl);
  6849. return rc;
  6850. }
  6851. static int __cam_isp_ctx_config_dev_in_acquired(struct cam_context *ctx,
  6852. struct cam_config_dev_cmd *cmd)
  6853. {
  6854. int rc = 0;
  6855. struct cam_isp_context *ctx_isp =
  6856. (struct cam_isp_context *) ctx->ctx_priv;
  6857. if (!ctx_isp->hw_acquired) {
  6858. CAM_ERR(CAM_ISP, "HW is not acquired, reject packet, ctx_id %u link: 0x%x",
  6859. ctx->ctx_id, ctx->link_hdl);
  6860. return -EINVAL;
  6861. }
  6862. rc = __cam_isp_ctx_config_dev_in_top_state(ctx, cmd);
  6863. if (!rc && ((ctx->link_hdl >= 0) || ctx_isp->offline_context)) {
  6864. ctx->state = CAM_CTX_READY;
  6865. trace_cam_context_state("ISP", ctx);
  6866. }
  6867. CAM_DBG(CAM_ISP, "next state %d, ctx %u link: 0x%x",
  6868. ctx->state, ctx->ctx_id, ctx->link_hdl);
  6869. return rc;
  6870. }
  6871. static int __cam_isp_ctx_config_dev_in_flushed(struct cam_context *ctx,
  6872. struct cam_config_dev_cmd *cmd)
  6873. {
  6874. int rc = 0;
  6875. struct cam_start_stop_dev_cmd start_cmd;
  6876. struct cam_hw_cmd_args hw_cmd_args;
  6877. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  6878. struct cam_isp_context *ctx_isp =
  6879. (struct cam_isp_context *) ctx->ctx_priv;
  6880. if (!ctx_isp->hw_acquired) {
  6881. CAM_ERR(CAM_ISP, "HW is not acquired, reject packet, ctx_id %u link: 0x%x",
  6882. ctx->ctx_id, ctx->link_hdl);
  6883. rc = -EINVAL;
  6884. goto end;
  6885. }
  6886. rc = __cam_isp_ctx_config_dev_in_top_state(ctx, cmd);
  6887. if (rc)
  6888. goto end;
  6889. if (!ctx_isp->init_received) {
  6890. CAM_WARN(CAM_ISP,
  6891. "Received update pckt in flushed state, skip start, ctx %u link: 0x%x",
  6892. ctx->ctx_id, ctx->link_hdl);
  6893. goto end;
  6894. }
  6895. CAM_DBG(CAM_ISP, "vfps_ctx:%s resume_hw_in_flushed:%d ctx:%u link: 0x%x",
  6896. CAM_BOOL_TO_YESNO(ctx_isp->vfps_aux_context),
  6897. ctx_isp->resume_hw_in_flushed,
  6898. ctx->ctx_id, ctx->link_hdl);
  6899. if (ctx_isp->vfps_aux_context) {
  6900. /* Resume the HW only when we get first valid req */
  6901. if (!ctx_isp->resume_hw_in_flushed)
  6902. goto end;
  6903. else
  6904. ctx_isp->resume_hw_in_flushed = false;
  6905. }
  6906. hw_cmd_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  6907. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  6908. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_CMD_RESUME_HW;
  6909. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  6910. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  6911. &hw_cmd_args);
  6912. if (rc) {
  6913. CAM_ERR(CAM_ISP, "Failed to resume HW rc: %d, ctx_id %u link: 0x%x",
  6914. rc, ctx->ctx_id, ctx->link_hdl);
  6915. goto end;
  6916. }
  6917. start_cmd.dev_handle = cmd->dev_handle;
  6918. start_cmd.session_handle = cmd->session_handle;
  6919. rc = __cam_isp_ctx_start_dev_in_ready(ctx, &start_cmd);
  6920. if (rc)
  6921. CAM_ERR(CAM_ISP,
  6922. "Failed to re-start HW after flush rc: %d, ctx_id %u link: 0x%x",
  6923. rc, ctx->ctx_id, ctx->link_hdl);
  6924. else
  6925. CAM_INFO(CAM_ISP,
  6926. "Received init after flush. Re-start HW complete in ctx:%d, link: 0x%x",
  6927. ctx->ctx_id, ctx->link_hdl);
  6928. end:
  6929. CAM_DBG(CAM_ISP, "next state %d sub_state:%d ctx_id %u link: 0x%x", ctx->state,
  6930. ctx_isp->substate_activated, ctx->ctx_id, ctx->link_hdl);
  6931. return rc;
  6932. }
  6933. static int __cam_isp_ctx_link_in_acquired(struct cam_context *ctx,
  6934. struct cam_req_mgr_core_dev_link_setup *link)
  6935. {
  6936. int rc = 0;
  6937. struct cam_isp_context *ctx_isp =
  6938. (struct cam_isp_context *) ctx->ctx_priv;
  6939. if (!link) {
  6940. CAM_ERR(CAM_ISP, "setup link info is null: %pK ctx: %u link: 0x%x",
  6941. link, ctx->ctx_id, ctx->link_hdl);
  6942. return -EINVAL;
  6943. }
  6944. if (!link->crm_cb) {
  6945. CAM_ERR(CAM_ISP, "crm cb is null: %pK ctx: %u, link: 0x%x",
  6946. link->crm_cb, ctx->ctx_id, ctx->link_hdl);
  6947. return -EINVAL;
  6948. }
  6949. CAM_DBG(CAM_ISP, "Enter.........ctx: %u, link: 0x%x", ctx->ctx_id, ctx->link_hdl);
  6950. ctx->link_hdl = link->link_hdl;
  6951. ctx->ctx_crm_intf = link->crm_cb;
  6952. ctx_isp->subscribe_event =
  6953. CAM_TRIGGER_POINT_SOF | CAM_TRIGGER_POINT_EOF;
  6954. ctx_isp->trigger_id = link->trigger_id;
  6955. ctx_isp->mswitch_default_apply_delay_max_cnt = 0;
  6956. atomic_set(&ctx_isp->mswitch_default_apply_delay_ref_cnt, 0);
  6957. if ((link->mode_switch_max_delay - CAM_MODESWITCH_DELAY_1) > 0) {
  6958. ctx_isp->handle_mswitch = true;
  6959. ctx_isp->mswitch_default_apply_delay_max_cnt =
  6960. link->mode_switch_max_delay - CAM_MODESWITCH_DELAY_1;
  6961. CAM_DBG(CAM_ISP,
  6962. "Enabled mode switch handling on ctx: %u max delay cnt: %u",
  6963. ctx->ctx_id, ctx_isp->mswitch_default_apply_delay_max_cnt);
  6964. atomic_set(&ctx_isp->mswitch_default_apply_delay_ref_cnt,
  6965. ctx_isp->mswitch_default_apply_delay_max_cnt);
  6966. }
  6967. /* change state only if we had the init config */
  6968. if (ctx_isp->init_received) {
  6969. ctx->state = CAM_CTX_READY;
  6970. trace_cam_context_state("ISP", ctx);
  6971. }
  6972. CAM_DBG(CAM_ISP, "next state %d, ctx: %u, link: 0x%x",
  6973. ctx->state, ctx->ctx_id, ctx->link_hdl);
  6974. return rc;
  6975. }
  6976. static int __cam_isp_ctx_unlink_in_acquired(struct cam_context *ctx,
  6977. struct cam_req_mgr_core_dev_link_setup *unlink)
  6978. {
  6979. int rc = 0;
  6980. struct cam_isp_context *ctx_isp =
  6981. (struct cam_isp_context *) ctx->ctx_priv;
  6982. ctx->link_hdl = -1;
  6983. ctx->ctx_crm_intf = NULL;
  6984. ctx_isp->trigger_id = -1;
  6985. ctx_isp->mswitch_default_apply_delay_max_cnt = 0;
  6986. atomic_set(&ctx_isp->mswitch_default_apply_delay_ref_cnt, 0);
  6987. return rc;
  6988. }
  6989. static int __cam_isp_ctx_get_dev_info_in_acquired(struct cam_context *ctx,
  6990. struct cam_req_mgr_device_info *dev_info)
  6991. {
  6992. int rc = 0;
  6993. struct cam_isp_context *ctx_isp =
  6994. (struct cam_isp_context *) ctx->ctx_priv;
  6995. dev_info->dev_hdl = ctx->dev_hdl;
  6996. strlcpy(dev_info->name, CAM_ISP_DEV_NAME, sizeof(dev_info->name));
  6997. dev_info->dev_id = CAM_REQ_MGR_DEVICE_IFE;
  6998. dev_info->p_delay = CAM_PIPELINE_DELAY_1;
  6999. dev_info->m_delay = CAM_MODESWITCH_DELAY_1;
  7000. dev_info->trigger = CAM_TRIGGER_POINT_SOF;
  7001. dev_info->trigger_on = true;
  7002. dev_info->is_shdr = ctx_isp->is_tfe_shdr;
  7003. dev_info->is_shdr_master = ctx_isp->is_shdr_master;
  7004. return rc;
  7005. }
  7006. static inline void __cam_isp_context_reset_ctx_params(
  7007. struct cam_isp_context *ctx_isp)
  7008. {
  7009. atomic_set(&ctx_isp->process_bubble, 0);
  7010. atomic_set(&ctx_isp->rxd_epoch, 0);
  7011. atomic_set(&ctx_isp->internal_recovery_set, 0);
  7012. ctx_isp->frame_id = 0;
  7013. ctx_isp->sof_timestamp_val = 0;
  7014. ctx_isp->boot_timestamp = 0;
  7015. ctx_isp->active_req_cnt = 0;
  7016. ctx_isp->reported_req_id = 0;
  7017. ctx_isp->reported_frame_id = 0;
  7018. ctx_isp->bubble_frame_cnt = 0;
  7019. ctx_isp->congestion_cnt = 0;
  7020. ctx_isp->recovery_req_id = 0;
  7021. ctx_isp->aeb_error_cnt = 0;
  7022. ctx_isp->out_of_sync_cnt = 0;
  7023. ctx_isp->sof_dbg_irq_en = false;
  7024. ctx_isp->last_sof_jiffies = 0;
  7025. ctx_isp->last_applied_jiffies = 0;
  7026. }
  7027. static int __cam_isp_ctx_start_dev_in_ready(struct cam_context *ctx,
  7028. struct cam_start_stop_dev_cmd *cmd)
  7029. {
  7030. int rc = 0;
  7031. int i;
  7032. struct cam_isp_start_args start_isp;
  7033. struct cam_ctx_request *req;
  7034. struct cam_isp_ctx_req *req_isp;
  7035. struct cam_isp_context *ctx_isp =
  7036. (struct cam_isp_context *) ctx->ctx_priv;
  7037. if (cmd->session_handle != ctx->session_hdl ||
  7038. cmd->dev_handle != ctx->dev_hdl) {
  7039. rc = -EPERM;
  7040. goto end;
  7041. }
  7042. if (list_empty(&ctx->pending_req_list)) {
  7043. /* should never happen */
  7044. CAM_ERR(CAM_ISP, "Start device with empty configuration, ctx: %u, link: 0x%x",
  7045. ctx->ctx_id, ctx->link_hdl);
  7046. rc = -EFAULT;
  7047. goto end;
  7048. } else {
  7049. req = list_first_entry(&ctx->pending_req_list,
  7050. struct cam_ctx_request, list);
  7051. }
  7052. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  7053. if (!ctx_isp->hw_ctx) {
  7054. CAM_ERR(CAM_ISP, "Wrong hw context pointer.ctx_idx: %u, link: 0x%x",
  7055. ctx->ctx_id, ctx->link_hdl);
  7056. rc = -EFAULT;
  7057. goto end;
  7058. }
  7059. start_isp.hw_config.ctxt_to_hw_map = ctx_isp->hw_ctx;
  7060. start_isp.hw_config.request_id = req->request_id;
  7061. start_isp.hw_config.hw_update_entries = req_isp->cfg;
  7062. start_isp.hw_config.num_hw_update_entries = req_isp->num_cfg;
  7063. start_isp.hw_config.priv = &req_isp->hw_update_data;
  7064. start_isp.hw_config.init_packet = 1;
  7065. start_isp.hw_config.reapply_type = CAM_CONFIG_REAPPLY_NONE;
  7066. start_isp.hw_config.cdm_reset_before_apply = false;
  7067. start_isp.is_internal_start = false;
  7068. ctx_isp->last_applied_req_id = req->request_id;
  7069. if (ctx->state == CAM_CTX_FLUSHED)
  7070. start_isp.start_only = true;
  7071. else
  7072. start_isp.start_only = false;
  7073. __cam_isp_context_reset_ctx_params(ctx_isp);
  7074. ctx_isp->substate_activated = ctx_isp->rdi_only_context ?
  7075. CAM_ISP_CTX_ACTIVATED_APPLIED :
  7076. (req_isp->num_fence_map_out) ? CAM_ISP_CTX_ACTIVATED_EPOCH :
  7077. CAM_ISP_CTX_ACTIVATED_SOF;
  7078. atomic64_set(&ctx_isp->dbg_monitors.state_monitor_head, -1);
  7079. atomic64_set(&ctx_isp->dbg_monitors.frame_monitor_head, -1);
  7080. for (i = 0; i < CAM_ISP_CTX_EVENT_MAX; i++)
  7081. atomic64_set(&ctx_isp->dbg_monitors.event_record_head[i], -1);
  7082. /*
  7083. * In case of CSID TPG we might receive SOF and RUP IRQs
  7084. * before hw_mgr_intf->hw_start has returned. So move
  7085. * req out of pending list before hw_start and add it
  7086. * back to pending list if hw_start fails.
  7087. */
  7088. list_del_init(&req->list);
  7089. if (ctx_isp->offline_context && !req_isp->num_fence_map_out) {
  7090. cam_smmu_buffer_tracker_putref(&req->buf_tracker);
  7091. list_add_tail(&req->list, &ctx->free_req_list);
  7092. atomic_set(&ctx_isp->rxd_epoch, 1);
  7093. CAM_DBG(CAM_REQ,
  7094. "Move pending req: %lld to free list(cnt: %d) offline ctx %u link: 0x%x",
  7095. req->request_id, ctx_isp->active_req_cnt, ctx->ctx_id, ctx->link_hdl);
  7096. } else if (ctx_isp->rdi_only_context || !req_isp->num_fence_map_out) {
  7097. list_add_tail(&req->list, &ctx->wait_req_list);
  7098. CAM_DBG(CAM_REQ,
  7099. "Move pending req: %lld to wait list(cnt: %d) ctx %u link: 0x%x",
  7100. req->request_id, ctx_isp->active_req_cnt, ctx->ctx_id, ctx->link_hdl);
  7101. } else {
  7102. list_add_tail(&req->list, &ctx->active_req_list);
  7103. ctx_isp->active_req_cnt++;
  7104. CAM_DBG(CAM_REQ,
  7105. "Move pending req: %lld to active list(cnt: %d) ctx %u link: 0x%x offline %d",
  7106. req->request_id, ctx_isp->active_req_cnt, ctx->ctx_id, ctx->link_hdl,
  7107. ctx_isp->offline_context);
  7108. }
  7109. /*
  7110. * Only place to change state before calling the hw due to
  7111. * hardware tasklet has higher priority that can cause the
  7112. * irq handling comes early
  7113. */
  7114. ctx->state = CAM_CTX_ACTIVATED;
  7115. trace_cam_context_state("ISP", ctx);
  7116. rc = ctx->hw_mgr_intf->hw_start(ctx->hw_mgr_intf->hw_mgr_priv,
  7117. &start_isp);
  7118. if (rc) {
  7119. /* HW failure. user need to clean up the resource */
  7120. CAM_ERR(CAM_ISP, "Start HW failed, ctx %u link: 0x%x", ctx->ctx_id, ctx->link_hdl);
  7121. ctx->state = CAM_CTX_READY;
  7122. if ((rc == -ETIMEDOUT) &&
  7123. (isp_ctx_debug.enable_cdm_cmd_buff_dump))
  7124. rc = cam_isp_ctx_dump_req(req_isp, 0, 0, NULL, false);
  7125. trace_cam_context_state("ISP", ctx);
  7126. list_del_init(&req->list);
  7127. list_add(&req->list, &ctx->pending_req_list);
  7128. goto end;
  7129. }
  7130. CAM_DBG(CAM_ISP, "start device success ctx %u link: 0x%x", ctx->ctx_id, ctx->link_hdl);
  7131. end:
  7132. return rc;
  7133. }
  7134. static int __cam_isp_ctx_unlink_in_ready(struct cam_context *ctx,
  7135. struct cam_req_mgr_core_dev_link_setup *unlink)
  7136. {
  7137. int rc = 0;
  7138. ctx->link_hdl = -1;
  7139. ctx->ctx_crm_intf = NULL;
  7140. ctx->state = CAM_CTX_ACQUIRED;
  7141. trace_cam_context_state("ISP", ctx);
  7142. return rc;
  7143. }
  7144. static int __cam_isp_ctx_stop_dev_in_activated_unlock(
  7145. struct cam_context *ctx, struct cam_start_stop_dev_cmd *stop_cmd)
  7146. {
  7147. int rc = 0;
  7148. uint32_t i;
  7149. struct cam_hw_stop_args stop;
  7150. struct cam_ctx_request *req;
  7151. struct cam_isp_ctx_req *req_isp;
  7152. struct cam_isp_context *ctx_isp =
  7153. (struct cam_isp_context *) ctx->ctx_priv;
  7154. struct cam_isp_stop_args stop_isp;
  7155. /* Mask off all the incoming hardware events */
  7156. spin_lock_bh(&ctx->lock);
  7157. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_HALT;
  7158. spin_unlock_bh(&ctx->lock);
  7159. /* stop hw first */
  7160. if (ctx_isp->hw_ctx) {
  7161. stop.ctxt_to_hw_map = ctx_isp->hw_ctx;
  7162. stop_isp.hw_stop_cmd = CAM_ISP_HW_STOP_IMMEDIATELY;
  7163. stop_isp.stop_only = false;
  7164. stop_isp.is_internal_stop = false;
  7165. stop.args = (void *) &stop_isp;
  7166. ctx->hw_mgr_intf->hw_stop(ctx->hw_mgr_intf->hw_mgr_priv,
  7167. &stop);
  7168. }
  7169. CAM_DBG(CAM_ISP, "next Substate[%s], ctx_idx: %u, link: 0x%x",
  7170. __cam_isp_ctx_substate_val_to_type(
  7171. ctx_isp->substate_activated), ctx->ctx_id, ctx->link_hdl);
  7172. if (ctx->ctx_crm_intf &&
  7173. ctx->ctx_crm_intf->notify_stop) {
  7174. struct cam_req_mgr_notify_stop notify;
  7175. notify.link_hdl = ctx->link_hdl;
  7176. CAM_DBG(CAM_ISP,
  7177. "Notify CRM about device stop ctx %u link 0x%x",
  7178. ctx->ctx_id, ctx->link_hdl);
  7179. ctx->ctx_crm_intf->notify_stop(&notify);
  7180. } else if (!ctx_isp->offline_context)
  7181. CAM_ERR(CAM_ISP, "cb not present, ctx_idx: %u, link: 0x%x",
  7182. ctx->ctx_id, ctx->link_hdl);
  7183. while (!list_empty(&ctx->pending_req_list)) {
  7184. req = list_first_entry(&ctx->pending_req_list,
  7185. struct cam_ctx_request, list);
  7186. list_del_init(&req->list);
  7187. cam_smmu_buffer_tracker_putref(&req->buf_tracker);
  7188. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  7189. CAM_DBG(CAM_ISP, "signal fence in pending list. fence num %d ctx:%u, link: 0x%x",
  7190. req_isp->num_fence_map_out, ctx->ctx_id, ctx->link_hdl);
  7191. for (i = 0; i < req_isp->num_fence_map_out; i++)
  7192. if (req_isp->fence_map_out[i].sync_id != -1) {
  7193. cam_sync_signal(
  7194. req_isp->fence_map_out[i].sync_id,
  7195. CAM_SYNC_STATE_SIGNALED_CANCEL,
  7196. CAM_SYNC_ISP_EVENT_HW_STOP);
  7197. }
  7198. list_add_tail(&req->list, &ctx->free_req_list);
  7199. }
  7200. while (!list_empty(&ctx->wait_req_list)) {
  7201. req = list_first_entry(&ctx->wait_req_list,
  7202. struct cam_ctx_request, list);
  7203. list_del_init(&req->list);
  7204. cam_smmu_buffer_tracker_putref(&req->buf_tracker);
  7205. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  7206. CAM_DBG(CAM_ISP, "signal fence in wait list. fence num %d ctx: %u, link: 0x%x",
  7207. req_isp->num_fence_map_out, ctx->ctx_id, ctx->link_hdl);
  7208. for (i = 0; i < req_isp->num_fence_map_out; i++)
  7209. if (req_isp->fence_map_out[i].sync_id != -1) {
  7210. cam_sync_signal(
  7211. req_isp->fence_map_out[i].sync_id,
  7212. CAM_SYNC_STATE_SIGNALED_CANCEL,
  7213. CAM_SYNC_ISP_EVENT_HW_STOP);
  7214. }
  7215. list_add_tail(&req->list, &ctx->free_req_list);
  7216. }
  7217. while (!list_empty(&ctx->active_req_list)) {
  7218. req = list_first_entry(&ctx->active_req_list,
  7219. struct cam_ctx_request, list);
  7220. list_del_init(&req->list);
  7221. cam_smmu_buffer_tracker_putref(&req->buf_tracker);
  7222. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  7223. CAM_DBG(CAM_ISP, "signal fence in active list. fence num %d ctx: %u, link: 0x%x",
  7224. req_isp->num_fence_map_out, ctx->ctx_id, ctx->link_hdl);
  7225. for (i = 0; i < req_isp->num_fence_map_out; i++)
  7226. if (req_isp->fence_map_out[i].sync_id != -1) {
  7227. cam_sync_signal(
  7228. req_isp->fence_map_out[i].sync_id,
  7229. CAM_SYNC_STATE_SIGNALED_CANCEL,
  7230. CAM_SYNC_ISP_EVENT_HW_STOP);
  7231. }
  7232. list_add_tail(&req->list, &ctx->free_req_list);
  7233. }
  7234. ctx_isp->frame_id = 0;
  7235. ctx_isp->active_req_cnt = 0;
  7236. ctx_isp->reported_req_id = 0;
  7237. ctx_isp->reported_frame_id = 0;
  7238. ctx_isp->last_applied_req_id = 0;
  7239. ctx_isp->req_info.last_bufdone_req_id = 0;
  7240. ctx_isp->bubble_frame_cnt = 0;
  7241. ctx_isp->congestion_cnt = 0;
  7242. ctx_isp->sof_dbg_irq_en = false;
  7243. atomic_set(&ctx_isp->process_bubble, 0);
  7244. atomic_set(&ctx_isp->internal_recovery_set, 0);
  7245. atomic_set(&ctx_isp->rxd_epoch, 0);
  7246. atomic64_set(&ctx_isp->dbg_monitors.state_monitor_head, -1);
  7247. atomic64_set(&ctx_isp->dbg_monitors.frame_monitor_head, -1);
  7248. /* Reset skipped_list for FCG config */
  7249. __cam_isp_ctx_reset_fcg_tracker(ctx);
  7250. for (i = 0; i < CAM_ISP_CTX_EVENT_MAX; i++)
  7251. atomic64_set(&ctx_isp->dbg_monitors.event_record_head[i], -1);
  7252. CAM_DBG(CAM_ISP, "Stop device success next state %d on ctx %u link: 0x%x",
  7253. ctx->state, ctx->ctx_id, ctx->link_hdl);
  7254. if (!stop_cmd) {
  7255. rc = __cam_isp_ctx_unlink_in_ready(ctx, NULL);
  7256. if (rc)
  7257. CAM_ERR(CAM_ISP, "Unlink failed rc=%d, ctx %u link: 0x%x",
  7258. rc, ctx->ctx_id, ctx->link_hdl);
  7259. }
  7260. return rc;
  7261. }
  7262. static int __cam_isp_ctx_stop_dev_in_activated(struct cam_context *ctx,
  7263. struct cam_start_stop_dev_cmd *cmd)
  7264. {
  7265. int rc = 0;
  7266. struct cam_isp_context *ctx_isp =
  7267. (struct cam_isp_context *)ctx->ctx_priv;
  7268. __cam_isp_ctx_stop_dev_in_activated_unlock(ctx, cmd);
  7269. ctx_isp->init_received = false;
  7270. ctx->state = CAM_CTX_ACQUIRED;
  7271. trace_cam_context_state("ISP", ctx);
  7272. return rc;
  7273. }
  7274. static int __cam_isp_ctx_release_dev_in_activated(struct cam_context *ctx,
  7275. struct cam_release_dev_cmd *cmd)
  7276. {
  7277. int rc = 0;
  7278. rc = __cam_isp_ctx_stop_dev_in_activated_unlock(ctx, NULL);
  7279. if (rc)
  7280. CAM_ERR(CAM_ISP, "Stop device failed rc=%d, ctx %u link: 0x%x",
  7281. rc, ctx->ctx_id, ctx->link_hdl);
  7282. rc = __cam_isp_ctx_release_dev_in_top_state(ctx, cmd);
  7283. if (rc)
  7284. CAM_ERR(CAM_ISP, "Release device failed rc=%d ctx %u link: 0x%x",
  7285. rc, ctx->ctx_id, ctx->link_hdl);
  7286. return rc;
  7287. }
  7288. static int __cam_isp_ctx_release_hw_in_activated(struct cam_context *ctx,
  7289. void *cmd)
  7290. {
  7291. int rc = 0;
  7292. rc = __cam_isp_ctx_stop_dev_in_activated_unlock(ctx, NULL);
  7293. if (rc)
  7294. CAM_ERR(CAM_ISP, "Stop device failed rc=%d, ctx %u link: 0x%x",
  7295. rc, ctx->ctx_id, ctx->link_hdl);
  7296. rc = __cam_isp_ctx_release_hw_in_top_state(ctx, cmd);
  7297. if (rc)
  7298. CAM_ERR(CAM_ISP, "Release hw failed rc=%d, ctx %u link: 0x%x",
  7299. rc, ctx->ctx_id, ctx->link_hdl);
  7300. return rc;
  7301. }
  7302. static int __cam_isp_ctx_link_pause(struct cam_context *ctx)
  7303. {
  7304. int rc = 0;
  7305. struct cam_hw_cmd_args hw_cmd_args;
  7306. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  7307. hw_cmd_args.ctxt_to_hw_map = ctx->ctxt_to_hw_map;
  7308. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  7309. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_CMD_PAUSE_HW;
  7310. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  7311. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  7312. &hw_cmd_args);
  7313. return rc;
  7314. }
  7315. static int __cam_isp_ctx_link_resume(struct cam_context *ctx)
  7316. {
  7317. int rc = 0;
  7318. struct cam_hw_cmd_args hw_cmd_args;
  7319. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  7320. hw_cmd_args.ctxt_to_hw_map = ctx->ctxt_to_hw_map;
  7321. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  7322. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_CMD_RESUME_HW;
  7323. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  7324. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  7325. &hw_cmd_args);
  7326. return rc;
  7327. }
  7328. static int __cam_isp_ctx_reset_and_recover(
  7329. bool skip_resume, struct cam_context *ctx)
  7330. {
  7331. int rc = 0;
  7332. struct cam_isp_context *ctx_isp =
  7333. (struct cam_isp_context *)ctx->ctx_priv;
  7334. struct cam_isp_stop_args stop_isp;
  7335. struct cam_hw_stop_args stop_args;
  7336. struct cam_isp_start_args start_isp;
  7337. struct cam_hw_cmd_args hw_cmd_args;
  7338. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  7339. struct cam_ctx_request *req;
  7340. struct cam_isp_ctx_req *req_isp;
  7341. spin_lock_bh(&ctx->lock);
  7342. if (ctx_isp->active_req_cnt) {
  7343. spin_unlock_bh(&ctx->lock);
  7344. CAM_WARN(CAM_ISP,
  7345. "Active list not empty: %u in ctx: %u on link: 0x%x, retry recovery for req: %lld after buf_done",
  7346. ctx_isp->active_req_cnt, ctx->ctx_id,
  7347. ctx->link_hdl, ctx_isp->recovery_req_id);
  7348. goto end;
  7349. }
  7350. if (ctx->state != CAM_CTX_ACTIVATED) {
  7351. spin_unlock_bh(&ctx->lock);
  7352. CAM_ERR(CAM_ISP,
  7353. "In wrong state %d, for recovery ctx: %u in link: 0x%x recovery req: %lld",
  7354. ctx->state, ctx->ctx_id,
  7355. ctx->link_hdl, ctx_isp->recovery_req_id);
  7356. rc = -EINVAL;
  7357. goto end;
  7358. }
  7359. if (list_empty(&ctx->pending_req_list)) {
  7360. /* Cannot start with no request */
  7361. spin_unlock_bh(&ctx->lock);
  7362. CAM_ERR(CAM_ISP,
  7363. "Failed to reset and recover last_applied_req: %llu in ctx: %u on link: 0x%x",
  7364. ctx_isp->last_applied_req_id, ctx->ctx_id, ctx->link_hdl);
  7365. rc = -EFAULT;
  7366. goto end;
  7367. }
  7368. if (!ctx_isp->hw_ctx) {
  7369. spin_unlock_bh(&ctx->lock);
  7370. CAM_ERR(CAM_ISP,
  7371. "Invalid hw context pointer ctx: %u on link: 0x%x",
  7372. ctx->ctx_id, ctx->link_hdl);
  7373. rc = -EFAULT;
  7374. goto end;
  7375. }
  7376. /* Block all events till HW is resumed */
  7377. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_HALT;
  7378. req = list_first_entry(&ctx->pending_req_list,
  7379. struct cam_ctx_request, list);
  7380. spin_unlock_bh(&ctx->lock);
  7381. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  7382. CAM_INFO(CAM_ISP,
  7383. "Trigger Halt, Reset & Resume for req: %llu ctx: %u in state: %d link: 0x%x",
  7384. req->request_id, ctx->ctx_id, ctx->state, ctx->link_hdl);
  7385. stop_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  7386. stop_isp.hw_stop_cmd = CAM_ISP_HW_STOP_IMMEDIATELY;
  7387. stop_isp.stop_only = true;
  7388. stop_isp.is_internal_stop = true;
  7389. stop_args.args = (void *)&stop_isp;
  7390. rc = ctx->hw_mgr_intf->hw_stop(ctx->hw_mgr_intf->hw_mgr_priv,
  7391. &stop_args);
  7392. if (rc) {
  7393. CAM_ERR(CAM_ISP, "Failed to stop HW rc: %d ctx: %u link: 0x%x",
  7394. rc, ctx->ctx_id, ctx->link_hdl);
  7395. goto end;
  7396. }
  7397. CAM_DBG(CAM_ISP, "Stop HW success ctx: %u link: 0x%x",
  7398. ctx->ctx_id, ctx->link_hdl);
  7399. /* API provides provision to stream off and not resume as well in case of fatal errors */
  7400. if (skip_resume) {
  7401. atomic_set(&ctx_isp->internal_recovery_set, 0);
  7402. CAM_INFO(CAM_ISP,
  7403. "Halting streaming off IFE/SFE ctx: %u last_applied_req: %lld [recovery_req: %lld] on link: 0x%x",
  7404. ctx->ctx_id, ctx_isp->last_applied_req_id,
  7405. ctx_isp->recovery_req_id, ctx->link_hdl);
  7406. goto end;
  7407. }
  7408. hw_cmd_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  7409. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  7410. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_CMD_RESUME_HW;
  7411. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  7412. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  7413. &hw_cmd_args);
  7414. if (rc) {
  7415. CAM_ERR(CAM_ISP, "Failed to resume HW rc: %d ctx: %u link: 0x%x",
  7416. rc, ctx->ctx_id, ctx->link_hdl);
  7417. goto end;
  7418. }
  7419. CAM_DBG(CAM_ISP, "Resume call success ctx: %u on link: 0x%x",
  7420. ctx->ctx_id, ctx->link_hdl);
  7421. start_isp.hw_config.ctxt_to_hw_map = ctx_isp->hw_ctx;
  7422. start_isp.hw_config.request_id = req->request_id;
  7423. start_isp.hw_config.hw_update_entries = req_isp->cfg;
  7424. start_isp.hw_config.num_hw_update_entries = req_isp->num_cfg;
  7425. start_isp.hw_config.priv = &req_isp->hw_update_data;
  7426. start_isp.hw_config.init_packet = 1;
  7427. start_isp.hw_config.reapply_type = CAM_CONFIG_REAPPLY_IQ;
  7428. start_isp.hw_config.cdm_reset_before_apply = false;
  7429. start_isp.start_only = true;
  7430. start_isp.is_internal_start = true;
  7431. __cam_isp_context_reset_internal_recovery_params(ctx_isp);
  7432. ctx_isp->substate_activated = ctx_isp->rdi_only_context ?
  7433. CAM_ISP_CTX_ACTIVATED_APPLIED : CAM_ISP_CTX_ACTIVATED_SOF;
  7434. rc = ctx->hw_mgr_intf->hw_start(ctx->hw_mgr_intf->hw_mgr_priv,
  7435. &start_isp);
  7436. if (rc) {
  7437. CAM_ERR(CAM_ISP, "Start HW failed, ctx: %u link: 0x%x", ctx->ctx_id, ctx->link_hdl);
  7438. ctx->state = CAM_CTX_READY;
  7439. goto end;
  7440. }
  7441. /* IQ applied for this request, on next trigger skip IQ cfg */
  7442. req_isp->reapply_type = CAM_CONFIG_REAPPLY_IO;
  7443. /* Notify userland that KMD has done internal recovery */
  7444. __cam_isp_ctx_notify_v4l2_error_event(CAM_REQ_MGR_WARN_TYPE_KMD_RECOVERY,
  7445. 0, req->request_id, ctx);
  7446. CAM_INFO(CAM_ISP, "Internal Start HW success ctx %u on link: 0x%x for req: %llu",
  7447. ctx->ctx_id, ctx->link_hdl, req->request_id);
  7448. end:
  7449. return rc;
  7450. }
  7451. static bool __cam_isp_ctx_try_internal_recovery_for_bubble(
  7452. int64_t error_req_id, struct cam_context *ctx)
  7453. {
  7454. int rc;
  7455. struct cam_isp_context *ctx_isp =
  7456. (struct cam_isp_context *)ctx->ctx_priv;
  7457. if (isp_ctx_debug.disable_internal_recovery_mask &
  7458. CAM_ISP_CTX_DISABLE_RECOVERY_BUBBLE)
  7459. return false;
  7460. /* Perform recovery if bubble recovery is stalled */
  7461. if (!atomic_read(&ctx_isp->process_bubble))
  7462. return false;
  7463. /* Validate if errored request has been applied */
  7464. if (ctx_isp->last_applied_req_id < error_req_id) {
  7465. CAM_WARN(CAM_ISP,
  7466. "Skip trying for internal recovery last applied: %lld error_req: %lld for ctx: %u on link: 0x%x",
  7467. ctx_isp->last_applied_req_id, error_req_id,
  7468. ctx->ctx_id, ctx->link_hdl);
  7469. return false;
  7470. }
  7471. if (__cam_isp_ctx_validate_for_req_reapply_util(ctx_isp)) {
  7472. CAM_WARN(CAM_ISP,
  7473. "Internal recovery not possible for ctx: %u on link: 0x%x req: %lld [last_applied: %lld]",
  7474. ctx->ctx_id, ctx->link_hdl, error_req_id, ctx_isp->last_applied_req_id);
  7475. return false;
  7476. }
  7477. /* Trigger reset and recover */
  7478. atomic_set(&ctx_isp->internal_recovery_set, 1);
  7479. rc = __cam_isp_ctx_reset_and_recover(false, ctx);
  7480. if (rc) {
  7481. CAM_WARN(CAM_ISP,
  7482. "Internal recovery failed in ctx: %u on link: 0x%x req: %lld [last_applied: %lld]",
  7483. ctx->ctx_id, ctx->link_hdl, error_req_id, ctx_isp->last_applied_req_id);
  7484. atomic_set(&ctx_isp->internal_recovery_set, 0);
  7485. goto error;
  7486. }
  7487. CAM_DBG(CAM_ISP,
  7488. "Internal recovery done in ctx: %u on link: 0x%x req: %lld [last_applied: %lld]",
  7489. ctx->ctx_id, ctx->link_hdl, error_req_id, ctx_isp->last_applied_req_id);
  7490. return true;
  7491. error:
  7492. return false;
  7493. }
  7494. static int __cam_isp_ctx_process_evt(struct cam_context *ctx,
  7495. struct cam_req_mgr_link_evt_data *link_evt_data)
  7496. {
  7497. int rc = 0;
  7498. struct cam_isp_context *ctx_isp =
  7499. (struct cam_isp_context *) ctx->ctx_priv;
  7500. if ((ctx->state == CAM_CTX_ACQUIRED) &&
  7501. (link_evt_data->evt_type != CAM_REQ_MGR_LINK_EVT_UPDATE_PROPERTIES)) {
  7502. CAM_WARN(CAM_ISP,
  7503. "Get unexpect evt:%d in acquired state, ctx: %u on link: 0x%x",
  7504. link_evt_data->evt_type, ctx->ctx_id, ctx->link_hdl);
  7505. return -EINVAL;
  7506. }
  7507. switch (link_evt_data->evt_type) {
  7508. case CAM_REQ_MGR_LINK_EVT_ERR:
  7509. case CAM_REQ_MGR_LINK_EVT_EOF:
  7510. /* No handling */
  7511. break;
  7512. case CAM_REQ_MGR_LINK_EVT_PAUSE:
  7513. rc = __cam_isp_ctx_link_pause(ctx);
  7514. break;
  7515. case CAM_REQ_MGR_LINK_EVT_RESUME:
  7516. rc = __cam_isp_ctx_link_resume(ctx);
  7517. break;
  7518. case CAM_REQ_MGR_LINK_EVT_SOF_FREEZE:
  7519. rc = __cam_isp_ctx_handle_sof_freeze_evt(ctx);
  7520. break;
  7521. case CAM_REQ_MGR_LINK_EVT_STALLED: {
  7522. bool internal_recovery_skipped = false;
  7523. if (ctx->state == CAM_CTX_ACTIVATED) {
  7524. if (link_evt_data->try_for_recovery)
  7525. internal_recovery_skipped =
  7526. __cam_isp_ctx_try_internal_recovery_for_bubble(
  7527. link_evt_data->req_id, ctx);
  7528. if (!internal_recovery_skipped)
  7529. rc = __cam_isp_ctx_trigger_reg_dump(
  7530. CAM_HW_MGR_CMD_REG_DUMP_ON_ERROR, ctx);
  7531. }
  7532. link_evt_data->try_for_recovery = internal_recovery_skipped;
  7533. }
  7534. break;
  7535. case CAM_REQ_MGR_LINK_EVT_UPDATE_PROPERTIES:
  7536. if (link_evt_data->u.properties_mask &
  7537. CAM_LINK_PROPERTY_SENSOR_STANDBY_AFTER_EOF)
  7538. ctx_isp->vfps_aux_context = true;
  7539. else
  7540. ctx_isp->vfps_aux_context = false;
  7541. CAM_DBG(CAM_ISP, "vfps_aux_context:%s on ctx: %u link: 0x%x",
  7542. CAM_BOOL_TO_YESNO(ctx_isp->vfps_aux_context), ctx->ctx_id, ctx->link_hdl);
  7543. break;
  7544. default:
  7545. CAM_WARN(CAM_ISP,
  7546. "Unsupported event type: 0x%x on ctx: %u link: 0x%x",
  7547. link_evt_data->evt_type, ctx->ctx_id, ctx->link_hdl);
  7548. rc = -EINVAL;
  7549. break;
  7550. }
  7551. return rc;
  7552. }
  7553. static int __cam_isp_ctx_unlink_in_activated(struct cam_context *ctx,
  7554. struct cam_req_mgr_core_dev_link_setup *unlink)
  7555. {
  7556. int rc = 0;
  7557. CAM_WARN(CAM_ISP,
  7558. "Received unlink in activated state. It's unexpected, ctx: %u link: 0x%x",
  7559. ctx->ctx_id, ctx->link_hdl);
  7560. rc = __cam_isp_ctx_stop_dev_in_activated_unlock(ctx, NULL);
  7561. if (rc)
  7562. CAM_WARN(CAM_ISP, "Stop device failed rc=%d, ctx: %u link: 0x%x",
  7563. rc, ctx->ctx_id, ctx->link_hdl);
  7564. rc = __cam_isp_ctx_unlink_in_ready(ctx, unlink);
  7565. if (rc)
  7566. CAM_ERR(CAM_ISP, "Unlink failed rc=%d, ctx: %u link: 0x%x",
  7567. rc, ctx->ctx_id, ctx->link_hdl);
  7568. return rc;
  7569. }
  7570. static int __cam_isp_ctx_apply_req(struct cam_context *ctx,
  7571. struct cam_req_mgr_apply_request *apply)
  7572. {
  7573. int rc = 0;
  7574. struct cam_ctx_ops *ctx_ops = NULL;
  7575. struct cam_isp_context *ctx_isp =
  7576. (struct cam_isp_context *) ctx->ctx_priv;
  7577. trace_cam_apply_req("ISP", ctx->ctx_id, apply->request_id, apply->link_hdl);
  7578. CAM_DBG(CAM_ISP, "Enter: apply req in Substate[%s] request_id:%lld, ctx: %u link: 0x%x",
  7579. __cam_isp_ctx_substate_val_to_type(
  7580. ctx_isp->substate_activated), apply->request_id, ctx->ctx_id, ctx->link_hdl);
  7581. ctx_ops = &ctx_isp->substate_machine[ctx_isp->substate_activated];
  7582. if (ctx_ops->crm_ops.apply_req) {
  7583. rc = ctx_ops->crm_ops.apply_req(ctx, apply);
  7584. } else {
  7585. CAM_WARN_RATE_LIMIT(CAM_ISP,
  7586. "No handle function in activated Substate[%s], ctx: %u link: 0x%x",
  7587. __cam_isp_ctx_substate_val_to_type(
  7588. ctx_isp->substate_activated), ctx->ctx_id, ctx->link_hdl);
  7589. rc = -EFAULT;
  7590. }
  7591. if (rc)
  7592. CAM_WARN_RATE_LIMIT(CAM_ISP,
  7593. "Apply failed in active Substate[%s] rc %d, ctx: %u link: 0x%x",
  7594. __cam_isp_ctx_substate_val_to_type(
  7595. ctx_isp->substate_activated), rc, ctx->ctx_id, ctx->link_hdl);
  7596. return rc;
  7597. }
  7598. static int __cam_isp_ctx_apply_default_settings(
  7599. struct cam_context *ctx,
  7600. struct cam_req_mgr_apply_request *apply)
  7601. {
  7602. int rc = 0;
  7603. struct cam_ctx_ops *ctx_ops = NULL;
  7604. struct cam_isp_context *ctx_isp =
  7605. (struct cam_isp_context *) ctx->ctx_priv;
  7606. struct cam_isp_fcg_prediction_tracker *fcg_tracker;
  7607. if (!(apply->trigger_point & ctx_isp->subscribe_event)) {
  7608. CAM_WARN(CAM_ISP,
  7609. "Trigger: %u not subscribed for: %u, ctx: %u link: 0x%x",
  7610. apply->trigger_point, ctx_isp->subscribe_event, ctx->ctx_id,
  7611. ctx->link_hdl);
  7612. return 0;
  7613. }
  7614. /* Allow apply default settings for IFE only at SOF */
  7615. if (apply->trigger_point != CAM_TRIGGER_POINT_SOF)
  7616. return 0;
  7617. if (atomic_read(&ctx_isp->internal_recovery_set))
  7618. return __cam_isp_ctx_reset_and_recover(false, ctx);
  7619. /* FCG handling */
  7620. fcg_tracker = &ctx_isp->fcg_tracker;
  7621. if (ctx_isp->frame_id != 1)
  7622. fcg_tracker->num_skipped += 1;
  7623. CAM_DBG(CAM_ISP,
  7624. "Apply default settings, number of previous continuous skipped frames: %d, ctx_id: %d",
  7625. fcg_tracker->num_skipped, ctx->ctx_id);
  7626. /*
  7627. * Call notify frame skip for static offline cases or
  7628. * mode switch cases where IFE mode switch delay differs
  7629. * from other devices on the link
  7630. */
  7631. if ((ctx_isp->use_default_apply) ||
  7632. (ctx_isp->mode_switch_en && ctx_isp->handle_mswitch)) {
  7633. CAM_DBG(CAM_ISP,
  7634. "Enter: apply req in Substate:%d request _id:%lld ctx:%u on link:0x%x",
  7635. ctx_isp->substate_activated, apply->request_id,
  7636. ctx->ctx_id, ctx->link_hdl);
  7637. ctx_ops = &ctx_isp->substate_machine[
  7638. ctx_isp->substate_activated];
  7639. if (ctx_ops->crm_ops.notify_frame_skip) {
  7640. rc = ctx_ops->crm_ops.notify_frame_skip(ctx, apply);
  7641. } else {
  7642. CAM_WARN_RATE_LIMIT(CAM_ISP,
  7643. "No handle function in activated substate %d, ctx:%u on link:0x%x",
  7644. ctx_isp->substate_activated, ctx->ctx_id, ctx->link_hdl);
  7645. rc = -EFAULT;
  7646. }
  7647. if (rc)
  7648. CAM_WARN_RATE_LIMIT(CAM_ISP,
  7649. "Apply default failed in active substate %d rc %d ctx: %u link: 0x%x",
  7650. ctx_isp->substate_activated, rc, ctx->ctx_id, ctx->link_hdl);
  7651. }
  7652. return rc;
  7653. }
  7654. void __cam_isp_ctx_notify_cpas(struct cam_context *ctx, uint32_t evt_id)
  7655. {
  7656. uint64_t request_id = 0;
  7657. struct cam_isp_context *ctx_isp =
  7658. (struct cam_isp_context *)ctx->ctx_priv;
  7659. struct cam_ctx_request *req = NULL;
  7660. char ctx_evt_id_string[128];
  7661. switch (evt_id) {
  7662. case CAM_ISP_HW_EVENT_SOF:
  7663. if (!list_empty(&ctx->wait_req_list)) {
  7664. req = list_first_entry(&ctx->wait_req_list, struct cam_ctx_request, list);
  7665. request_id = req->request_id;
  7666. } else
  7667. request_id = 0;
  7668. if (ctx_isp->substate_activated == CAM_ISP_CTX_ACTIVATED_EPOCH &&
  7669. !list_empty(&ctx->active_req_list)) {
  7670. req = list_last_entry(&ctx->active_req_list, struct cam_ctx_request, list);
  7671. request_id = req->request_id;
  7672. CAM_DBG(CAM_ISP, "EPCR notify cpas");
  7673. }
  7674. break;
  7675. case CAM_ISP_HW_EVENT_EOF:
  7676. if (!list_empty(&ctx->active_req_list)) {
  7677. req = list_first_entry(&ctx->active_req_list, struct cam_ctx_request, list);
  7678. request_id = req->request_id;
  7679. } else
  7680. request_id = 0;
  7681. break;
  7682. case CAM_ISP_HW_EVENT_EPOCH:
  7683. if (list_empty(&ctx->wait_req_list)) {
  7684. if (!list_empty(&ctx->active_req_list)) {
  7685. req = list_last_entry(&ctx->active_req_list,
  7686. struct cam_ctx_request, list);
  7687. request_id = req->request_id;
  7688. } else
  7689. request_id = 0;
  7690. } else {
  7691. req = list_first_entry(&ctx->wait_req_list, struct cam_ctx_request, list);
  7692. request_id = req->request_id;
  7693. }
  7694. break;
  7695. default:
  7696. return;
  7697. }
  7698. snprintf(ctx_evt_id_string, sizeof(ctx_evt_id_string),
  7699. "%s_frame[%llu]_%s",
  7700. ctx->ctx_id_string,
  7701. ctx_isp->frame_id,
  7702. cam_isp_hw_evt_type_to_string(evt_id));
  7703. CAM_DBG(CAM_ISP, "Substate[%s] ctx: %s frame: %llu evt: %s req: %llu",
  7704. __cam_isp_ctx_substate_val_to_type(ctx_isp->substate_activated),
  7705. ctx->ctx_id_string,
  7706. ctx_isp->frame_id,
  7707. cam_isp_hw_evt_type_to_string(evt_id),
  7708. request_id);
  7709. cam_cpas_notify_event(ctx_evt_id_string, request_id);
  7710. }
  7711. static int __cam_isp_ctx_handle_irq_in_activated(void *context,
  7712. uint32_t evt_id, void *evt_data)
  7713. {
  7714. int rc = 0;
  7715. struct cam_isp_ctx_irq_ops *irq_ops = NULL;
  7716. struct cam_context *ctx = (struct cam_context *)context;
  7717. struct cam_isp_context *ctx_isp =
  7718. (struct cam_isp_context *)ctx->ctx_priv;
  7719. spin_lock(&ctx->lock);
  7720. trace_cam_isp_activated_irq(ctx, ctx_isp->substate_activated, evt_id,
  7721. __cam_isp_ctx_get_event_ts(evt_id, evt_data));
  7722. CAM_DBG(CAM_ISP, "Enter: State %d, Substate[%s], evt id %d, ctx:%u link: 0x%x",
  7723. ctx->state, __cam_isp_ctx_substate_val_to_type(
  7724. ctx_isp->substate_activated), evt_id,
  7725. ctx->ctx_id, ctx->link_hdl);
  7726. irq_ops = &ctx_isp->substate_machine_irq[ctx_isp->substate_activated];
  7727. if (irq_ops->irq_ops[evt_id]) {
  7728. rc = irq_ops->irq_ops[evt_id](ctx_isp, evt_data);
  7729. } else {
  7730. CAM_DBG(CAM_ISP,
  7731. "No handle function for Substate[%s], evt id %d, ctx:%u link: 0x%x",
  7732. __cam_isp_ctx_substate_val_to_type(
  7733. ctx_isp->substate_activated), evt_id,
  7734. ctx->ctx_id, ctx->link_hdl);
  7735. if (isp_ctx_debug.enable_state_monitor_dump)
  7736. __cam_isp_ctx_dump_state_monitor_array(ctx_isp);
  7737. }
  7738. if ((evt_id == CAM_ISP_HW_EVENT_SOF) ||
  7739. (evt_id == CAM_ISP_HW_EVENT_EOF) ||
  7740. (evt_id == CAM_ISP_HW_EVENT_EPOCH))
  7741. __cam_isp_ctx_update_frame_timing_record(evt_id, ctx_isp);
  7742. __cam_isp_ctx_notify_cpas(ctx, evt_id);
  7743. CAM_DBG(CAM_ISP, "Exit: State %d Substate[%s], ctx: %u link: 0x%x",
  7744. ctx->state, __cam_isp_ctx_substate_val_to_type(
  7745. ctx_isp->substate_activated), ctx->ctx_id, ctx->link_hdl);
  7746. spin_unlock(&ctx->lock);
  7747. return rc;
  7748. }
  7749. static int cam_isp_context_validate_event_notify_injection(struct cam_context *ctx,
  7750. struct cam_hw_inject_evt_param *evt_params)
  7751. {
  7752. int rc = 0;
  7753. uint32_t evt_type;
  7754. uint64_t req_id;
  7755. req_id = evt_params->req_id;
  7756. evt_type = evt_params->u.evt_notify.evt_notify_type;
  7757. switch (evt_type) {
  7758. case V4L_EVENT_CAM_REQ_MGR_ERROR: {
  7759. struct cam_hw_inject_err_evt_param *err_evt_params =
  7760. &evt_params->u.evt_notify.u.err_evt_params;
  7761. switch (err_evt_params->err_type) {
  7762. case CAM_REQ_MGR_ERROR_TYPE_RECOVERY:
  7763. case CAM_REQ_MGR_ERROR_TYPE_SOF_FREEZE:
  7764. case CAM_REQ_MGR_ERROR_TYPE_FULL_RECOVERY:
  7765. case CAM_REQ_MGR_WARN_TYPE_KMD_RECOVERY:
  7766. break;
  7767. default:
  7768. CAM_ERR(CAM_ISP,
  7769. "Invalid error type: %u for error event injection err type: %u req id: %llu ctx id: %u link: 0x%x dev hdl: %d",
  7770. err_evt_params->err_type, err_evt_params->err_code,
  7771. req_id, ctx->ctx_id, ctx->link_hdl, ctx->dev_hdl);
  7772. return -EINVAL;
  7773. }
  7774. CAM_INFO(CAM_ISP,
  7775. "Inject ERR evt: err code: %u err type: %u req id: %llu ctx id: %u link: 0x%x dev hdl: %d",
  7776. err_evt_params->err_code, err_evt_params->err_type,
  7777. req_id, ctx->ctx_id, ctx->link_hdl, ctx->dev_hdl);
  7778. break;
  7779. }
  7780. case V4L_EVENT_CAM_REQ_MGR_PF_ERROR: {
  7781. struct cam_hw_inject_pf_evt_param *pf_evt_params =
  7782. &evt_params->u.evt_notify.u.pf_evt_params;
  7783. bool non_fatal_en;
  7784. rc = cam_smmu_is_cb_non_fatal_fault_en(ctx->img_iommu_hdl, &non_fatal_en);
  7785. if (rc) {
  7786. CAM_ERR(CAM_ISP,
  7787. "Fail to query whether device's cb has non-fatal enabled rc:%d, ctx id: %u link: 0x%x",
  7788. rc, ctx->ctx_id, ctx->link_hdl);
  7789. return rc;
  7790. }
  7791. if (!non_fatal_en) {
  7792. CAM_ERR(CAM_ISP,
  7793. "Fail to inject pagefault event notif. Pagefault fatal for ISP,ctx:%u link:0x%x",
  7794. ctx->ctx_id, ctx->link_hdl);
  7795. return -EINVAL;
  7796. }
  7797. CAM_INFO(CAM_ISP,
  7798. "Inject PF evt: req_id:%llu ctx:%u link:0x%x dev hdl:%d ctx found:%hhu",
  7799. req_id, ctx->ctx_id, ctx->link_hdl, ctx->dev_hdl,
  7800. pf_evt_params->ctx_found);
  7801. break;
  7802. }
  7803. default:
  7804. CAM_ERR(CAM_ISP, "Event notification type not supported: %u, ctx: %u link: 0x%x",
  7805. evt_type, ctx->ctx_id, ctx->link_hdl);
  7806. rc = -EINVAL;
  7807. }
  7808. return rc;
  7809. }
  7810. static int cam_isp_context_inject_evt(void *context, void *evt_args)
  7811. {
  7812. struct cam_context *ctx = context;
  7813. struct cam_isp_context *ctx_isp = NULL;
  7814. struct cam_hw_inject_evt_param *evt_params = evt_args;
  7815. int rc = 0;
  7816. if (!ctx || !evt_args) {
  7817. CAM_ERR(CAM_ISP,
  7818. "Invalid params ctx %s event args %s",
  7819. CAM_IS_NULL_TO_STR(ctx), CAM_IS_NULL_TO_STR(evt_args));
  7820. return -EINVAL;
  7821. }
  7822. ctx_isp = ctx->ctx_priv;
  7823. if (evt_params->inject_id == CAM_COMMON_EVT_INJECT_NOTIFY_EVENT_TYPE) {
  7824. rc = cam_isp_context_validate_event_notify_injection(ctx, evt_params);
  7825. if (rc) {
  7826. CAM_ERR(CAM_ISP,
  7827. "Event notif injection failed validation rc:%d, ctx:%u link:0x%x",
  7828. rc, ctx->ctx_id, ctx->link_hdl);
  7829. return rc;
  7830. }
  7831. } else {
  7832. CAM_ERR(CAM_ISP, "Bufdone err injection %u not supported by ISP,ctx:%u link:0x%x",
  7833. evt_params->inject_id, ctx->ctx_id, ctx->link_hdl);
  7834. return -EINVAL;
  7835. }
  7836. memcpy(&ctx_isp->evt_inject_params, evt_params,
  7837. sizeof(struct cam_hw_inject_evt_param));
  7838. ctx_isp->evt_inject_params.is_valid = true;
  7839. return rc;
  7840. }
  7841. /* top state machine */
  7842. static struct cam_ctx_ops
  7843. cam_isp_ctx_top_state_machine[CAM_CTX_STATE_MAX] = {
  7844. /* Uninit */
  7845. {
  7846. .ioctl_ops = {},
  7847. .crm_ops = {},
  7848. .irq_ops = NULL,
  7849. },
  7850. /* Available */
  7851. {
  7852. .ioctl_ops = {
  7853. .acquire_dev = __cam_isp_ctx_acquire_dev_in_available,
  7854. },
  7855. .crm_ops = {},
  7856. .irq_ops = NULL,
  7857. },
  7858. /* Acquired */
  7859. {
  7860. .ioctl_ops = {
  7861. .acquire_hw = __cam_isp_ctx_acquire_hw_in_acquired,
  7862. .release_dev = __cam_isp_ctx_release_dev_in_top_state,
  7863. .config_dev = __cam_isp_ctx_config_dev_in_acquired,
  7864. .flush_dev = __cam_isp_ctx_flush_dev_in_top_state,
  7865. .release_hw = __cam_isp_ctx_release_hw_in_top_state,
  7866. },
  7867. .crm_ops = {
  7868. .link = __cam_isp_ctx_link_in_acquired,
  7869. .unlink = __cam_isp_ctx_unlink_in_acquired,
  7870. .get_dev_info = __cam_isp_ctx_get_dev_info_in_acquired,
  7871. .process_evt = __cam_isp_ctx_process_evt,
  7872. .flush_req = __cam_isp_ctx_flush_req_in_top_state,
  7873. .dump_req = __cam_isp_ctx_dump_in_top_state,
  7874. },
  7875. .irq_ops = NULL,
  7876. .pagefault_ops = cam_isp_context_dump_requests,
  7877. .dumpinfo_ops = cam_isp_context_info_dump,
  7878. .evt_inject_ops = cam_isp_context_inject_evt,
  7879. },
  7880. /* Ready */
  7881. {
  7882. .ioctl_ops = {
  7883. .start_dev = __cam_isp_ctx_start_dev_in_ready,
  7884. .release_dev = __cam_isp_ctx_release_dev_in_top_state,
  7885. .config_dev = __cam_isp_ctx_config_dev_in_top_state,
  7886. .flush_dev = __cam_isp_ctx_flush_dev_in_top_state,
  7887. .release_hw = __cam_isp_ctx_release_hw_in_top_state,
  7888. },
  7889. .crm_ops = {
  7890. .unlink = __cam_isp_ctx_unlink_in_ready,
  7891. .flush_req = __cam_isp_ctx_flush_req_in_ready,
  7892. .dump_req = __cam_isp_ctx_dump_in_top_state,
  7893. },
  7894. .irq_ops = NULL,
  7895. .pagefault_ops = cam_isp_context_dump_requests,
  7896. .dumpinfo_ops = cam_isp_context_info_dump,
  7897. .evt_inject_ops = cam_isp_context_inject_evt,
  7898. },
  7899. /* Flushed */
  7900. {
  7901. .ioctl_ops = {
  7902. .stop_dev = __cam_isp_ctx_stop_dev_in_activated,
  7903. .release_dev = __cam_isp_ctx_release_dev_in_activated,
  7904. .config_dev = __cam_isp_ctx_config_dev_in_flushed,
  7905. .release_hw = __cam_isp_ctx_release_hw_in_activated,
  7906. },
  7907. .crm_ops = {
  7908. .unlink = __cam_isp_ctx_unlink_in_ready,
  7909. .process_evt = __cam_isp_ctx_process_evt,
  7910. .flush_req = __cam_isp_ctx_flush_req_in_flushed_state,
  7911. },
  7912. .irq_ops = NULL,
  7913. .pagefault_ops = cam_isp_context_dump_requests,
  7914. .dumpinfo_ops = cam_isp_context_info_dump,
  7915. .evt_inject_ops = cam_isp_context_inject_evt,
  7916. .msg_cb_ops = cam_isp_context_handle_message,
  7917. },
  7918. /* Activated */
  7919. {
  7920. .ioctl_ops = {
  7921. .stop_dev = __cam_isp_ctx_stop_dev_in_activated,
  7922. .release_dev = __cam_isp_ctx_release_dev_in_activated,
  7923. .config_dev = __cam_isp_ctx_config_dev_in_top_state,
  7924. .flush_dev = __cam_isp_ctx_flush_dev_in_top_state,
  7925. .release_hw = __cam_isp_ctx_release_hw_in_activated,
  7926. },
  7927. .crm_ops = {
  7928. .unlink = __cam_isp_ctx_unlink_in_activated,
  7929. .apply_req = __cam_isp_ctx_apply_req,
  7930. .notify_frame_skip =
  7931. __cam_isp_ctx_apply_default_settings,
  7932. .flush_req = __cam_isp_ctx_flush_req_in_top_state,
  7933. .process_evt = __cam_isp_ctx_process_evt,
  7934. .dump_req = __cam_isp_ctx_dump_in_top_state,
  7935. },
  7936. .irq_ops = __cam_isp_ctx_handle_irq_in_activated,
  7937. .pagefault_ops = cam_isp_context_dump_requests,
  7938. .dumpinfo_ops = cam_isp_context_info_dump,
  7939. .recovery_ops = cam_isp_context_hw_recovery,
  7940. .evt_inject_ops = cam_isp_context_inject_evt,
  7941. .msg_cb_ops = cam_isp_context_handle_message,
  7942. },
  7943. };
  7944. static int cam_isp_context_hw_recovery(void *priv, void *data)
  7945. {
  7946. struct cam_context *ctx = priv;
  7947. int rc = -EPERM;
  7948. if (ctx->hw_mgr_intf->hw_recovery)
  7949. rc = ctx->hw_mgr_intf->hw_recovery(ctx->hw_mgr_intf->hw_mgr_priv, data);
  7950. else
  7951. CAM_ERR(CAM_ISP, "hw mgr doesn't support recovery, ctx_idx: %u, link: 0x%x",
  7952. ctx->ctx_id, ctx->link_hdl);
  7953. return rc;
  7954. }
  7955. static void cam_isp_context_find_faulted_context(struct cam_context *ctx,
  7956. struct list_head *req_list, struct cam_hw_dump_pf_args *pf_args, bool *found)
  7957. {
  7958. struct cam_ctx_request *req = NULL;
  7959. struct cam_ctx_request *req_temp = NULL;
  7960. int rc;
  7961. *found = false;
  7962. list_for_each_entry_safe(req, req_temp, req_list, list) {
  7963. CAM_INFO(CAM_ISP, "List req_id: %llu ctx id: %u link: 0x%x",
  7964. req->request_id, ctx->ctx_id, ctx->link_hdl);
  7965. rc = cam_context_dump_pf_info_to_hw(ctx, pf_args, &req->pf_data);
  7966. if (rc)
  7967. CAM_ERR(CAM_ISP, "Failed to dump pf info, ctx_idx: %u, link: 0x%x",
  7968. ctx->ctx_id, ctx->link_hdl);
  7969. /*
  7970. * Found faulted buffer. Even if faulted ctx is found, but
  7971. * continue to search for faulted buffer
  7972. */
  7973. if (pf_args->pf_context_info.mem_type != CAM_FAULT_BUF_NOT_FOUND) {
  7974. *found = true;
  7975. break;
  7976. }
  7977. }
  7978. }
  7979. static int cam_isp_context_dump_requests(void *data, void *args)
  7980. {
  7981. struct cam_context *ctx = (struct cam_context *)data;
  7982. struct cam_isp_context *ctx_isp;
  7983. struct cam_hw_dump_pf_args *pf_args = (struct cam_hw_dump_pf_args *)args;
  7984. int rc = 0;
  7985. bool found;
  7986. if (!ctx || !pf_args) {
  7987. CAM_ERR(CAM_ISP, "Invalid ctx %pK or pf args %pK",
  7988. ctx, pf_args);
  7989. return -EINVAL;
  7990. }
  7991. ctx_isp = (struct cam_isp_context *)ctx->ctx_priv;
  7992. if (!ctx_isp) {
  7993. CAM_ERR(CAM_ISP, "Invalid isp ctx");
  7994. return -EINVAL;
  7995. }
  7996. if (pf_args->handle_sec_pf)
  7997. goto end;
  7998. CAM_INFO(CAM_ISP,
  7999. "Iterating over active list for isp ctx %u link: 0x%x state %d",
  8000. ctx->ctx_id, ctx->link_hdl, ctx->state);
  8001. cam_isp_context_find_faulted_context(ctx, &ctx->active_req_list,
  8002. pf_args, &found);
  8003. if (found)
  8004. goto end;
  8005. CAM_INFO(CAM_ISP,
  8006. "Iterating over waiting list of isp ctx %u link: 0x%x state %d",
  8007. ctx->ctx_id, ctx->link_hdl, ctx->state);
  8008. cam_isp_context_find_faulted_context(ctx, &ctx->wait_req_list,
  8009. pf_args, &found);
  8010. if (found)
  8011. goto end;
  8012. /*
  8013. * In certain scenarios we observe both overflow and SMMU pagefault
  8014. * for a particular request. If overflow is handled before page fault
  8015. * we need to traverse through pending request list because if
  8016. * bubble recovery is enabled on any request we move that request
  8017. * and all the subsequent requests to the pending list while handling
  8018. * overflow error.
  8019. */
  8020. CAM_INFO(CAM_ISP,
  8021. "Iterating over pending req list of isp ctx %u link: 0x%x state %d",
  8022. ctx->ctx_id, ctx->link_hdl, ctx->state);
  8023. cam_isp_context_find_faulted_context(ctx, &ctx->pending_req_list,
  8024. pf_args, &found);
  8025. if (found)
  8026. goto end;
  8027. end:
  8028. if (pf_args->pf_context_info.resource_type) {
  8029. ctx_isp = (struct cam_isp_context *)ctx->ctx_priv;
  8030. CAM_INFO(CAM_ISP,
  8031. "Page fault on resource:%s (0x%x) ctx id:%u link: 0x%x frame id:%d reported id:%lld applied id:%lld",
  8032. __cam_isp_resource_handle_id_to_type(ctx_isp->isp_device_type,
  8033. pf_args->pf_context_info.resource_type),
  8034. pf_args->pf_context_info.resource_type,
  8035. ctx->ctx_id, ctx->link_hdl, ctx_isp->frame_id,
  8036. ctx_isp->reported_req_id, ctx_isp->last_applied_req_id);
  8037. }
  8038. /*
  8039. * Send PF notification to UMD if PF found on current CTX
  8040. * or it is forced to send PF notification to UMD even if no
  8041. * faulted context found
  8042. */
  8043. if (pf_args->pf_context_info.ctx_found ||
  8044. pf_args->pf_context_info.force_send_pf_evt)
  8045. rc = cam_context_send_pf_evt(ctx, pf_args);
  8046. if (rc)
  8047. CAM_ERR(CAM_ISP,
  8048. "Failed to notify PF event to userspace rc: %d, ctx id:%u link: 0x%x",
  8049. rc, ctx->ctx_id, ctx->link_hdl);
  8050. return rc;
  8051. }
  8052. static int cam_isp_context_handle_message(void *context,
  8053. uint32_t msg_type, void *data)
  8054. {
  8055. int rc = -EINVAL;
  8056. struct cam_hw_cmd_args hw_cmd_args = {0};
  8057. struct cam_isp_hw_cmd_args isp_hw_cmd_args = {0};
  8058. struct cam_context *ctx = (struct cam_context *)context;
  8059. hw_cmd_args.ctxt_to_hw_map = ctx->ctxt_to_hw_map;
  8060. switch (msg_type) {
  8061. case CAM_SUBDEV_MESSAGE_CLOCK_UPDATE:
  8062. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  8063. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_CMD_UPDATE_CLOCK;
  8064. isp_hw_cmd_args.cmd_data = data;
  8065. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  8066. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  8067. &hw_cmd_args);
  8068. if (rc)
  8069. CAM_ERR(CAM_ISP, "Update clock rate failed rc: %d", rc);
  8070. break;
  8071. default:
  8072. CAM_ERR(CAM_ISP, "Invalid message type %d", msg_type);
  8073. }
  8074. return rc;
  8075. }
  8076. static int cam_isp_context_debug_register(void)
  8077. {
  8078. int rc = 0;
  8079. struct dentry *dbgfileptr = NULL;
  8080. if (!cam_debugfs_available())
  8081. return 0;
  8082. rc = cam_debugfs_create_subdir("isp_ctx", &dbgfileptr);
  8083. if (rc) {
  8084. CAM_ERR(CAM_ISP, "DebugFS could not create directory!");
  8085. return rc;
  8086. }
  8087. /* Store parent inode for cleanup in caller */
  8088. isp_ctx_debug.dentry = dbgfileptr;
  8089. debugfs_create_u32("enable_state_monitor_dump", 0644,
  8090. isp_ctx_debug.dentry, &isp_ctx_debug.enable_state_monitor_dump);
  8091. debugfs_create_u8("enable_cdm_cmd_buffer_dump", 0644,
  8092. isp_ctx_debug.dentry, &isp_ctx_debug.enable_cdm_cmd_buff_dump);
  8093. debugfs_create_u32("disable_internal_recovery_mask", 0644,
  8094. isp_ctx_debug.dentry, &isp_ctx_debug.disable_internal_recovery_mask);
  8095. return 0;
  8096. }
  8097. int cam_isp_context_init(struct cam_isp_context *ctx,
  8098. struct cam_context *ctx_base,
  8099. struct cam_req_mgr_kmd_ops *crm_node_intf,
  8100. struct cam_hw_mgr_intf *hw_intf,
  8101. uint32_t ctx_id,
  8102. uint32_t isp_device_type,
  8103. int img_iommu_hdl)
  8104. {
  8105. int rc = -1;
  8106. int i;
  8107. struct cam_isp_skip_frame_info *skip_info, *temp;
  8108. if (!ctx || !ctx_base) {
  8109. CAM_ERR(CAM_ISP, "Invalid Context");
  8110. goto err;
  8111. }
  8112. /* ISP context setup */
  8113. memset(ctx, 0, sizeof(*ctx));
  8114. ctx->base = ctx_base;
  8115. ctx->frame_id = 0;
  8116. ctx->custom_enabled = false;
  8117. ctx->use_frame_header_ts = false;
  8118. ctx->use_default_apply = false;
  8119. ctx->active_req_cnt = 0;
  8120. ctx->reported_req_id = 0;
  8121. ctx->bubble_frame_cnt = 0;
  8122. ctx->congestion_cnt = 0;
  8123. ctx->req_info.last_bufdone_req_id = 0;
  8124. ctx->v4l2_event_sub_ids = 0;
  8125. ctx->hw_ctx = NULL;
  8126. ctx->substate_activated = CAM_ISP_CTX_ACTIVATED_SOF;
  8127. ctx->substate_machine = cam_isp_ctx_activated_state_machine;
  8128. ctx->substate_machine_irq = cam_isp_ctx_activated_state_machine_irq;
  8129. ctx->init_timestamp = jiffies_to_msecs(jiffies);
  8130. ctx->isp_device_type = isp_device_type;
  8131. for (i = 0; i < CAM_ISP_CTX_REQ_MAX; i++) {
  8132. ctx->req_base[i].req_priv = &ctx->req_isp[i];
  8133. ctx->req_isp[i].base = &ctx->req_base[i];
  8134. }
  8135. /* camera context setup */
  8136. rc = cam_context_init(ctx_base, isp_dev_name, CAM_ISP, ctx_id,
  8137. crm_node_intf, hw_intf, ctx->req_base, CAM_ISP_CTX_REQ_MAX, img_iommu_hdl);
  8138. if (rc) {
  8139. CAM_ERR(CAM_ISP, "Camera Context Base init failed, ctx_idx: %u, link: 0x%x",
  8140. ctx_base->ctx_id, ctx_base->link_hdl);
  8141. goto free_mem;
  8142. }
  8143. /* FCG related struct setup */
  8144. INIT_LIST_HEAD(&ctx->fcg_tracker.skipped_list);
  8145. for (i = 0; i < CAM_ISP_AFD_PIPELINE_DELAY; i++) {
  8146. skip_info = kzalloc(sizeof(struct cam_isp_skip_frame_info), GFP_KERNEL);
  8147. if (!skip_info) {
  8148. CAM_ERR(CAM_ISP,
  8149. "Failed to allocate memory for FCG struct, ctx_idx: %u, link: %x",
  8150. ctx_base->ctx_id, ctx_base->link_hdl);
  8151. rc = -ENOMEM;
  8152. goto free_mem;
  8153. }
  8154. list_add_tail(&skip_info->list, &ctx->fcg_tracker.skipped_list);
  8155. }
  8156. /* link camera context with isp context */
  8157. ctx_base->state_machine = cam_isp_ctx_top_state_machine;
  8158. ctx_base->ctx_priv = ctx;
  8159. /* initializing current state for error logging */
  8160. for (i = 0; i < CAM_ISP_CTX_STATE_MONITOR_MAX_ENTRIES; i++) {
  8161. ctx->dbg_monitors.state_monitor[i].curr_state =
  8162. CAM_ISP_CTX_ACTIVATED_MAX;
  8163. }
  8164. atomic64_set(&ctx->dbg_monitors.state_monitor_head, -1);
  8165. for (i = 0; i < CAM_ISP_CTX_EVENT_MAX; i++)
  8166. atomic64_set(&ctx->dbg_monitors.event_record_head[i], -1);
  8167. atomic64_set(&ctx->dbg_monitors.frame_monitor_head, -1);
  8168. if (!isp_ctx_debug.dentry)
  8169. cam_isp_context_debug_register();
  8170. return rc;
  8171. free_mem:
  8172. list_for_each_entry_safe(skip_info, temp,
  8173. &ctx->fcg_tracker.skipped_list, list) {
  8174. list_del(&skip_info->list);
  8175. kfree(skip_info);
  8176. skip_info = NULL;
  8177. }
  8178. err:
  8179. return rc;
  8180. }
  8181. int cam_isp_context_deinit(struct cam_isp_context *ctx)
  8182. {
  8183. struct cam_isp_skip_frame_info *skip_info, *temp;
  8184. list_for_each_entry_safe(skip_info, temp,
  8185. &ctx->fcg_tracker.skipped_list, list) {
  8186. list_del(&skip_info->list);
  8187. kfree(skip_info);
  8188. skip_info = NULL;
  8189. }
  8190. if (ctx->base)
  8191. cam_context_deinit(ctx->base);
  8192. if (ctx->substate_activated != CAM_ISP_CTX_ACTIVATED_SOF)
  8193. CAM_ERR(CAM_ISP, "ISP context Substate[%s] is invalid",
  8194. __cam_isp_ctx_substate_val_to_type(
  8195. ctx->substate_activated));
  8196. isp_ctx_debug.dentry = NULL;
  8197. memset(ctx, 0, sizeof(*ctx));
  8198. return 0;
  8199. }