cam_isp_context.c 231 KB

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