cam_isp_context.c 238 KB

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