cam_isp_context.c 205 KB

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