cam_isp_context.c 235 KB

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