cam_isp_context.c 265 KB

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