cam_isp_context.c 271 KB

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