cam_isp_context.c 275 KB

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