cam_isp_context.c 234 KB

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