cam_isp_context.c 206 KB

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