cam_isp_context.c 207 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127
  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_in_flushed_state(
  3596. struct cam_context *ctx,
  3597. struct cam_req_mgr_flush_request *flush_req)
  3598. {
  3599. CAM_INFO(CAM_ISP, "Flush (type %d) in flushed state req id %lld ctx_id:%d",
  3600. flush_req->type, flush_req->req_id, ctx->ctx_id);
  3601. if (flush_req->req_id > ctx->last_flush_req)
  3602. ctx->last_flush_req = flush_req->req_id;
  3603. return 0;
  3604. }
  3605. static int __cam_isp_ctx_flush_req(struct cam_context *ctx,
  3606. struct list_head *req_list, struct cam_req_mgr_flush_request *flush_req)
  3607. {
  3608. int i, rc, tmp = 0;
  3609. uint32_t cancel_req_id_found = 0;
  3610. struct cam_ctx_request *req;
  3611. struct cam_ctx_request *req_temp;
  3612. struct cam_isp_ctx_req *req_isp;
  3613. struct list_head flush_list;
  3614. struct cam_isp_context *ctx_isp = NULL;
  3615. ctx_isp = (struct cam_isp_context *) ctx->ctx_priv;
  3616. INIT_LIST_HEAD(&flush_list);
  3617. if (list_empty(req_list)) {
  3618. CAM_DBG(CAM_ISP, "request list is empty");
  3619. if (flush_req->type == CAM_REQ_MGR_FLUSH_TYPE_CANCEL_REQ) {
  3620. CAM_INFO(CAM_ISP, "no request to cancel (last applied:%lld cancel:%lld)",
  3621. ctx_isp->last_applied_req_id, flush_req->req_id);
  3622. return -EINVAL;
  3623. } else
  3624. return 0;
  3625. }
  3626. CAM_DBG(CAM_REQ, "Flush [%u] in progress for req_id %llu",
  3627. flush_req->type, flush_req->req_id);
  3628. list_for_each_entry_safe(req, req_temp, req_list, list) {
  3629. if (flush_req->type == CAM_REQ_MGR_FLUSH_TYPE_CANCEL_REQ) {
  3630. if (req->request_id != flush_req->req_id) {
  3631. continue;
  3632. } else {
  3633. list_del_init(&req->list);
  3634. list_add_tail(&req->list, &flush_list);
  3635. cancel_req_id_found = 1;
  3636. __cam_isp_ctx_update_state_monitor_array(
  3637. ctx_isp,
  3638. CAM_ISP_STATE_CHANGE_TRIGGER_FLUSH,
  3639. req->request_id);
  3640. break;
  3641. }
  3642. }
  3643. list_del_init(&req->list);
  3644. list_add_tail(&req->list, &flush_list);
  3645. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  3646. CAM_ISP_STATE_CHANGE_TRIGGER_FLUSH, req->request_id);
  3647. }
  3648. if (list_empty(&flush_list)) {
  3649. /*
  3650. * Maybe the req isn't sent to KMD since UMD already skip
  3651. * req in CSL layer.
  3652. */
  3653. CAM_INFO(CAM_ISP,
  3654. "flush list is empty, flush type %d for req %llu",
  3655. flush_req->type, flush_req->req_id);
  3656. return 0;
  3657. }
  3658. list_for_each_entry_safe(req, req_temp, &flush_list, list) {
  3659. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  3660. for (i = 0; i < req_isp->num_fence_map_out; i++) {
  3661. if (req_isp->fence_map_out[i].sync_id != -1) {
  3662. CAM_DBG(CAM_ISP, "Flush req 0x%llx, fence %d",
  3663. req->request_id,
  3664. req_isp->fence_map_out[i].sync_id);
  3665. rc = cam_sync_signal(
  3666. req_isp->fence_map_out[i].sync_id,
  3667. CAM_SYNC_STATE_SIGNALED_CANCEL,
  3668. CAM_SYNC_ISP_EVENT_FLUSH);
  3669. if (rc) {
  3670. tmp = req_isp->fence_map_out[i].sync_id;
  3671. CAM_ERR_RATE_LIMIT(CAM_ISP,
  3672. "signal fence %d failed", tmp);
  3673. }
  3674. req_isp->fence_map_out[i].sync_id = -1;
  3675. }
  3676. }
  3677. req_isp->reapply_type = CAM_CONFIG_REAPPLY_NONE;
  3678. req_isp->cdm_reset_before_apply = false;
  3679. list_del_init(&req->list);
  3680. list_add_tail(&req->list, &ctx->free_req_list);
  3681. }
  3682. return 0;
  3683. }
  3684. static int __cam_isp_ctx_flush_req_in_top_state(
  3685. struct cam_context *ctx,
  3686. struct cam_req_mgr_flush_request *flush_req)
  3687. {
  3688. int rc = 0;
  3689. struct cam_isp_context *ctx_isp;
  3690. struct cam_isp_stop_args stop_isp;
  3691. struct cam_hw_stop_args stop_args;
  3692. struct cam_hw_reset_args reset_args;
  3693. struct cam_req_mgr_timer_notify timer;
  3694. ctx_isp = (struct cam_isp_context *) ctx->ctx_priv;
  3695. CAM_DBG(CAM_ISP, "Flush pending list");
  3696. spin_lock_bh(&ctx->lock);
  3697. rc = __cam_isp_ctx_flush_req(ctx, &ctx->pending_req_list, flush_req);
  3698. spin_unlock_bh(&ctx->lock);
  3699. if (flush_req->type == CAM_REQ_MGR_FLUSH_TYPE_ALL) {
  3700. if (ctx->state <= CAM_CTX_READY) {
  3701. ctx->state = CAM_CTX_ACQUIRED;
  3702. goto end;
  3703. }
  3704. spin_lock_bh(&ctx->lock);
  3705. ctx->state = CAM_CTX_FLUSHED;
  3706. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_HALT;
  3707. spin_unlock_bh(&ctx->lock);
  3708. CAM_INFO(CAM_ISP, "Last request id to flush is %lld, ctx_id:%d",
  3709. flush_req->req_id, ctx->ctx_id);
  3710. ctx->last_flush_req = flush_req->req_id;
  3711. __cam_isp_ctx_trigger_reg_dump(CAM_HW_MGR_CMD_REG_DUMP_ON_FLUSH, ctx);
  3712. stop_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  3713. stop_isp.hw_stop_cmd = CAM_ISP_HW_STOP_IMMEDIATELY;
  3714. stop_isp.stop_only = true;
  3715. stop_isp.is_internal_stop = false;
  3716. stop_args.args = (void *)&stop_isp;
  3717. rc = ctx->hw_mgr_intf->hw_stop(ctx->hw_mgr_intf->hw_mgr_priv,
  3718. &stop_args);
  3719. if (rc)
  3720. CAM_ERR(CAM_ISP, "Failed to stop HW in Flush rc: %d",
  3721. rc);
  3722. CAM_INFO(CAM_ISP, "Stop HW complete. Reset HW next.");
  3723. CAM_DBG(CAM_ISP, "Flush wait and active lists");
  3724. if (ctx->ctx_crm_intf && ctx->ctx_crm_intf->notify_timer) {
  3725. timer.link_hdl = ctx->link_hdl;
  3726. timer.dev_hdl = ctx->dev_hdl;
  3727. timer.state = false;
  3728. ctx->ctx_crm_intf->notify_timer(&timer);
  3729. }
  3730. spin_lock_bh(&ctx->lock);
  3731. if (!list_empty(&ctx->wait_req_list))
  3732. rc = __cam_isp_ctx_flush_req(ctx, &ctx->wait_req_list,
  3733. flush_req);
  3734. if (!list_empty(&ctx->active_req_list))
  3735. rc = __cam_isp_ctx_flush_req(ctx, &ctx->active_req_list,
  3736. flush_req);
  3737. ctx_isp->active_req_cnt = 0;
  3738. spin_unlock_bh(&ctx->lock);
  3739. reset_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  3740. rc = ctx->hw_mgr_intf->hw_reset(ctx->hw_mgr_intf->hw_mgr_priv,
  3741. &reset_args);
  3742. if (rc)
  3743. CAM_ERR(CAM_ISP, "Failed to reset HW rc: %d", rc);
  3744. ctx_isp->init_received = false;
  3745. }
  3746. end:
  3747. ctx_isp->bubble_frame_cnt = 0;
  3748. atomic_set(&ctx_isp->process_bubble, 0);
  3749. atomic_set(&ctx_isp->rxd_epoch, 0);
  3750. atomic_set(&ctx_isp->internal_recovery_set, 0);
  3751. return rc;
  3752. }
  3753. static int __cam_isp_ctx_flush_req_in_ready(
  3754. struct cam_context *ctx,
  3755. struct cam_req_mgr_flush_request *flush_req)
  3756. {
  3757. int rc = 0;
  3758. CAM_DBG(CAM_ISP, "try to flush pending list");
  3759. spin_lock_bh(&ctx->lock);
  3760. rc = __cam_isp_ctx_flush_req(ctx, &ctx->pending_req_list, flush_req);
  3761. /* if nothing is in pending req list, change state to acquire */
  3762. if (list_empty(&ctx->pending_req_list))
  3763. ctx->state = CAM_CTX_ACQUIRED;
  3764. spin_unlock_bh(&ctx->lock);
  3765. trace_cam_context_state("ISP", ctx);
  3766. CAM_DBG(CAM_ISP, "Flush request in ready state. next state %d",
  3767. ctx->state);
  3768. return rc;
  3769. }
  3770. static struct cam_ctx_ops
  3771. cam_isp_ctx_activated_state_machine[CAM_ISP_CTX_ACTIVATED_MAX] = {
  3772. /* SOF */
  3773. {
  3774. .ioctl_ops = {},
  3775. .crm_ops = {
  3776. .apply_req = __cam_isp_ctx_apply_req_in_sof,
  3777. .notify_frame_skip =
  3778. __cam_isp_ctx_apply_default_req_settings,
  3779. },
  3780. .irq_ops = NULL,
  3781. },
  3782. /* APPLIED */
  3783. {
  3784. .ioctl_ops = {},
  3785. .crm_ops = {},
  3786. .irq_ops = NULL,
  3787. },
  3788. /* EPOCH */
  3789. {
  3790. .ioctl_ops = {},
  3791. .crm_ops = {
  3792. .apply_req = __cam_isp_ctx_apply_req_in_epoch,
  3793. .notify_frame_skip =
  3794. __cam_isp_ctx_apply_default_req_settings,
  3795. },
  3796. .irq_ops = NULL,
  3797. },
  3798. /* BUBBLE */
  3799. {
  3800. .ioctl_ops = {},
  3801. .crm_ops = {
  3802. .apply_req = __cam_isp_ctx_apply_req_in_bubble,
  3803. .notify_frame_skip =
  3804. __cam_isp_ctx_apply_default_req_settings,
  3805. },
  3806. .irq_ops = NULL,
  3807. },
  3808. /* Bubble Applied */
  3809. {
  3810. .ioctl_ops = {},
  3811. .crm_ops = {},
  3812. .irq_ops = NULL,
  3813. },
  3814. /* HW ERROR */
  3815. {
  3816. .ioctl_ops = {},
  3817. .crm_ops = {},
  3818. .irq_ops = NULL,
  3819. },
  3820. /* HALT */
  3821. {
  3822. .ioctl_ops = {},
  3823. .crm_ops = {},
  3824. .irq_ops = NULL,
  3825. },
  3826. };
  3827. static struct cam_ctx_ops
  3828. cam_isp_ctx_fs2_state_machine[CAM_ISP_CTX_ACTIVATED_MAX] = {
  3829. /* SOF */
  3830. {
  3831. .ioctl_ops = {},
  3832. .crm_ops = {
  3833. .apply_req = __cam_isp_ctx_apply_req_in_sof,
  3834. },
  3835. .irq_ops = NULL,
  3836. },
  3837. /* APPLIED */
  3838. {
  3839. .ioctl_ops = {},
  3840. .crm_ops = {},
  3841. .irq_ops = NULL,
  3842. },
  3843. /* EPOCH */
  3844. {
  3845. .ioctl_ops = {},
  3846. .crm_ops = {
  3847. .apply_req = __cam_isp_ctx_apply_req_in_epoch,
  3848. },
  3849. .irq_ops = NULL,
  3850. },
  3851. /* BUBBLE */
  3852. {
  3853. .ioctl_ops = {},
  3854. .crm_ops = {
  3855. .apply_req = __cam_isp_ctx_apply_req_in_bubble,
  3856. },
  3857. .irq_ops = NULL,
  3858. },
  3859. /* Bubble Applied */
  3860. {
  3861. .ioctl_ops = {},
  3862. .crm_ops = {},
  3863. .irq_ops = NULL,
  3864. },
  3865. /* HW ERROR */
  3866. {
  3867. .ioctl_ops = {},
  3868. .crm_ops = {},
  3869. .irq_ops = NULL,
  3870. },
  3871. /* HALT */
  3872. {
  3873. .ioctl_ops = {},
  3874. .crm_ops = {},
  3875. .irq_ops = NULL,
  3876. },
  3877. };
  3878. static int __cam_isp_ctx_rdi_only_sof_in_top_state(
  3879. struct cam_isp_context *ctx_isp, void *evt_data)
  3880. {
  3881. int rc = 0;
  3882. struct cam_context *ctx = ctx_isp->base;
  3883. struct cam_isp_hw_sof_event_data *sof_event_data = evt_data;
  3884. uint64_t request_id = 0;
  3885. if (!evt_data) {
  3886. CAM_ERR(CAM_ISP, "in valid sof event data");
  3887. return -EINVAL;
  3888. }
  3889. ctx_isp->frame_id++;
  3890. ctx_isp->sof_timestamp_val = sof_event_data->timestamp;
  3891. ctx_isp->boot_timestamp = sof_event_data->boot_time;
  3892. CAM_DBG(CAM_ISP, "frame id: %lld time stamp:0x%llx",
  3893. ctx_isp->frame_id, ctx_isp->sof_timestamp_val);
  3894. /*
  3895. * notify reqmgr with sof signal. Note, due to scheduling delay
  3896. * we can run into situation that two active requests has already
  3897. * be in the active queue while we try to do the notification.
  3898. * In this case, we need to skip the current notification. This
  3899. * helps the state machine to catch up the delay.
  3900. */
  3901. if (ctx_isp->active_req_cnt <= 2) {
  3902. __cam_isp_ctx_notify_trigger_util(CAM_TRIGGER_POINT_SOF, ctx_isp);
  3903. /*
  3904. * It's possible for rup done to be processed before
  3905. * SOF, check for first active request shutter here
  3906. */
  3907. if (!list_empty(&ctx->active_req_list)) {
  3908. struct cam_ctx_request *req = NULL;
  3909. req = list_first_entry(&ctx->active_req_list,
  3910. struct cam_ctx_request, list);
  3911. if (req->request_id > ctx_isp->reported_req_id) {
  3912. request_id = req->request_id;
  3913. ctx_isp->reported_req_id = request_id;
  3914. }
  3915. }
  3916. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  3917. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  3918. } else {
  3919. CAM_ERR_RATE_LIMIT(CAM_ISP, "Can not notify SOF to CRM");
  3920. }
  3921. if (list_empty(&ctx->active_req_list))
  3922. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_SOF;
  3923. else
  3924. CAM_DBG(CAM_ISP, "Still need to wait for the buf done");
  3925. CAM_DBG(CAM_ISP, "next Substate[%s]",
  3926. __cam_isp_ctx_substate_val_to_type(
  3927. ctx_isp->substate_activated));
  3928. return rc;
  3929. }
  3930. static int __cam_isp_ctx_rdi_only_sof_in_applied_state(
  3931. struct cam_isp_context *ctx_isp, void *evt_data)
  3932. {
  3933. struct cam_isp_hw_sof_event_data *sof_event_data = evt_data;
  3934. if (!evt_data) {
  3935. CAM_ERR(CAM_ISP, "in valid sof event data");
  3936. return -EINVAL;
  3937. }
  3938. ctx_isp->frame_id++;
  3939. ctx_isp->sof_timestamp_val = sof_event_data->timestamp;
  3940. ctx_isp->boot_timestamp = sof_event_data->boot_time;
  3941. CAM_DBG(CAM_ISP, "frame id: %lld time stamp:0x%llx",
  3942. ctx_isp->frame_id, ctx_isp->sof_timestamp_val);
  3943. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_BUBBLE_APPLIED;
  3944. CAM_DBG(CAM_ISP, "next Substate[%s]",
  3945. __cam_isp_ctx_substate_val_to_type(
  3946. ctx_isp->substate_activated));
  3947. return 0;
  3948. }
  3949. static int __cam_isp_ctx_rdi_only_sof_in_bubble_applied(
  3950. struct cam_isp_context *ctx_isp, void *evt_data)
  3951. {
  3952. struct cam_ctx_request *req;
  3953. struct cam_isp_ctx_req *req_isp;
  3954. struct cam_context *ctx = ctx_isp->base;
  3955. struct cam_isp_hw_sof_event_data *sof_event_data = evt_data;
  3956. uint64_t request_id = 0;
  3957. /*
  3958. * Sof in bubble applied state means, reg update not received.
  3959. * before increment frame id and override time stamp value, send
  3960. * the previous sof time stamp that got captured in the
  3961. * sof in applied state.
  3962. */
  3963. CAM_DBG(CAM_ISP, "frame id: %lld time stamp:0x%llx",
  3964. ctx_isp->frame_id, ctx_isp->sof_timestamp_val);
  3965. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  3966. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  3967. ctx_isp->frame_id++;
  3968. ctx_isp->sof_timestamp_val = sof_event_data->timestamp;
  3969. ctx_isp->boot_timestamp = sof_event_data->boot_time;
  3970. CAM_DBG(CAM_ISP, "frame id: %lld time stamp:0x%llx",
  3971. ctx_isp->frame_id, ctx_isp->sof_timestamp_val);
  3972. if (list_empty(&ctx->wait_req_list)) {
  3973. /*
  3974. * If no pending req in epoch, this is an error case.
  3975. * The recovery is to go back to sof state
  3976. */
  3977. CAM_ERR(CAM_ISP, "No wait request");
  3978. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_SOF;
  3979. /* Send SOF event as empty frame*/
  3980. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  3981. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  3982. goto end;
  3983. }
  3984. req = list_first_entry(&ctx->wait_req_list, struct cam_ctx_request,
  3985. list);
  3986. req_isp = (struct cam_isp_ctx_req *)req->req_priv;
  3987. req_isp->bubble_detected = true;
  3988. CAM_INFO_RATE_LIMIT(CAM_ISP, "Ctx:%d Report Bubble flag %d req id:%lld",
  3989. ctx->ctx_id, req_isp->bubble_report, req->request_id);
  3990. req_isp->reapply_type = CAM_CONFIG_REAPPLY_IO;
  3991. req_isp->cdm_reset_before_apply = false;
  3992. if (req_isp->bubble_report) {
  3993. __cam_isp_ctx_notify_error_util(CAM_TRIGGER_POINT_SOF, CRM_KMD_ERR_BUBBLE,
  3994. req->request_id, ctx_isp);
  3995. atomic_set(&ctx_isp->process_bubble, 1);
  3996. } else {
  3997. req_isp->bubble_report = 0;
  3998. }
  3999. /*
  4000. * Always move the request to active list. Let buf done
  4001. * function handles the rest.
  4002. */
  4003. list_del_init(&req->list);
  4004. list_add_tail(&req->list, &ctx->active_req_list);
  4005. ctx_isp->active_req_cnt++;
  4006. CAM_DBG(CAM_ISP, "move request %lld to active list(cnt = %d)",
  4007. req->request_id, ctx_isp->active_req_cnt);
  4008. if (!req_isp->bubble_report) {
  4009. if (req->request_id > ctx_isp->reported_req_id) {
  4010. request_id = req->request_id;
  4011. ctx_isp->reported_req_id = request_id;
  4012. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  4013. CAM_REQ_MGR_SOF_EVENT_ERROR);
  4014. } else
  4015. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  4016. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  4017. } else
  4018. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  4019. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  4020. /* change the state to bubble, as reg update has not come */
  4021. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_BUBBLE;
  4022. CAM_DBG(CAM_ISP, "next Substate[%s]",
  4023. __cam_isp_ctx_substate_val_to_type(
  4024. ctx_isp->substate_activated));
  4025. end:
  4026. return 0;
  4027. }
  4028. static int __cam_isp_ctx_rdi_only_sof_in_bubble_state(
  4029. struct cam_isp_context *ctx_isp, void *evt_data)
  4030. {
  4031. uint32_t i;
  4032. struct cam_ctx_request *req;
  4033. struct cam_context *ctx = ctx_isp->base;
  4034. struct cam_isp_hw_sof_event_data *sof_event_data = evt_data;
  4035. struct cam_isp_ctx_req *req_isp;
  4036. struct cam_hw_cmd_args hw_cmd_args;
  4037. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  4038. uint64_t request_id = 0;
  4039. uint64_t last_cdm_done_req = 0;
  4040. int rc = 0;
  4041. if (!evt_data) {
  4042. CAM_ERR(CAM_ISP, "in valid sof event data");
  4043. return -EINVAL;
  4044. }
  4045. ctx_isp->frame_id++;
  4046. ctx_isp->sof_timestamp_val = sof_event_data->timestamp;
  4047. ctx_isp->boot_timestamp = sof_event_data->boot_time;
  4048. CAM_DBG(CAM_ISP, "frame id: %lld time stamp:0x%llx",
  4049. ctx_isp->frame_id, ctx_isp->sof_timestamp_val);
  4050. if (atomic_read(&ctx_isp->process_bubble)) {
  4051. if (list_empty(&ctx->active_req_list)) {
  4052. CAM_ERR(CAM_ISP, "No available active req in bubble");
  4053. atomic_set(&ctx_isp->process_bubble, 0);
  4054. return -EINVAL;
  4055. }
  4056. if (ctx_isp->last_sof_timestamp ==
  4057. ctx_isp->sof_timestamp_val) {
  4058. CAM_DBG(CAM_ISP,
  4059. "Tasklet delay detected! Bubble frame: %lld check skipped, sof_timestamp: %lld, ctx_id: %d",
  4060. ctx_isp->frame_id,
  4061. ctx_isp->sof_timestamp_val,
  4062. ctx->ctx_id);
  4063. goto end;
  4064. }
  4065. req = list_first_entry(&ctx->active_req_list,
  4066. struct cam_ctx_request, list);
  4067. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  4068. if (req_isp->bubble_detected) {
  4069. hw_cmd_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  4070. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  4071. isp_hw_cmd_args.cmd_type =
  4072. CAM_ISP_HW_MGR_GET_LAST_CDM_DONE;
  4073. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  4074. rc = ctx->hw_mgr_intf->hw_cmd(
  4075. ctx->hw_mgr_intf->hw_mgr_priv,
  4076. &hw_cmd_args);
  4077. if (rc) {
  4078. CAM_ERR(CAM_ISP, "HW command failed");
  4079. return rc;
  4080. }
  4081. last_cdm_done_req = isp_hw_cmd_args.u.last_cdm_done;
  4082. CAM_DBG(CAM_ISP, "last_cdm_done req: %d ctx_id: %d",
  4083. last_cdm_done_req, ctx->ctx_id);
  4084. if (last_cdm_done_req >= req->request_id) {
  4085. CAM_DBG(CAM_ISP,
  4086. "CDM callback detected for req: %lld, possible buf_done delay, waiting for buf_done",
  4087. req->request_id);
  4088. goto end;
  4089. } else {
  4090. CAM_WARN(CAM_ISP,
  4091. "CDM callback not happened for req: %lld, possible CDM stuck or workqueue delay",
  4092. req->request_id);
  4093. req_isp->num_acked = 0;
  4094. req_isp->num_deferred_acks = 0;
  4095. req_isp->bubble_detected = false;
  4096. req_isp->cdm_reset_before_apply = true;
  4097. list_del_init(&req->list);
  4098. list_add(&req->list, &ctx->pending_req_list);
  4099. atomic_set(&ctx_isp->process_bubble, 0);
  4100. ctx_isp->active_req_cnt--;
  4101. CAM_DBG(CAM_REQ,
  4102. "Move active req: %lld to pending list(cnt = %d) [bubble re-apply],ctx %u",
  4103. req->request_id,
  4104. ctx_isp->active_req_cnt, ctx->ctx_id);
  4105. }
  4106. goto end;
  4107. }
  4108. }
  4109. /*
  4110. * Signal all active requests with error and move the all the active
  4111. * requests to free list
  4112. */
  4113. while (!list_empty(&ctx->active_req_list)) {
  4114. req = list_first_entry(&ctx->active_req_list,
  4115. struct cam_ctx_request, list);
  4116. list_del_init(&req->list);
  4117. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  4118. CAM_DBG(CAM_ISP, "signal fence in active list. fence num %d",
  4119. req_isp->num_fence_map_out);
  4120. for (i = 0; i < req_isp->num_fence_map_out; i++)
  4121. if (req_isp->fence_map_out[i].sync_id != -1) {
  4122. cam_sync_signal(
  4123. req_isp->fence_map_out[i].sync_id,
  4124. CAM_SYNC_STATE_SIGNALED_ERROR,
  4125. CAM_SYNC_ISP_EVENT_BUBBLE);
  4126. }
  4127. list_add_tail(&req->list, &ctx->free_req_list);
  4128. ctx_isp->active_req_cnt--;
  4129. }
  4130. end:
  4131. /* notify reqmgr with sof signal */
  4132. __cam_isp_ctx_notify_trigger_util(CAM_TRIGGER_POINT_SOF, ctx_isp);
  4133. /*
  4134. * It is idle frame with out any applied request id, send
  4135. * request id as zero
  4136. */
  4137. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  4138. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  4139. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_SOF;
  4140. CAM_DBG(CAM_ISP, "next Substate[%s]",
  4141. __cam_isp_ctx_substate_val_to_type(
  4142. ctx_isp->substate_activated));
  4143. ctx_isp->last_sof_timestamp = ctx_isp->sof_timestamp_val;
  4144. return 0;
  4145. }
  4146. static int __cam_isp_ctx_rdi_only_reg_upd_in_bubble_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. req = list_first_entry(&ctx->active_req_list,
  4152. struct cam_ctx_request, list);
  4153. CAM_INFO(CAM_ISP, "Received RUP for Bubble Request", req->request_id);
  4154. return 0;
  4155. }
  4156. static int __cam_isp_ctx_rdi_only_reg_upd_in_bubble_applied_state(
  4157. struct cam_isp_context *ctx_isp, void *evt_data)
  4158. {
  4159. struct cam_ctx_request *req = NULL;
  4160. struct cam_context *ctx = ctx_isp->base;
  4161. struct cam_isp_ctx_req *req_isp;
  4162. uint64_t request_id = 0;
  4163. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_EPOCH;
  4164. /* notify reqmgr with sof signal*/
  4165. if (list_empty(&ctx->wait_req_list)) {
  4166. CAM_ERR(CAM_ISP, "Reg upd ack with no waiting request");
  4167. goto error;
  4168. }
  4169. req = list_first_entry(&ctx->wait_req_list,
  4170. struct cam_ctx_request, list);
  4171. list_del_init(&req->list);
  4172. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  4173. request_id =
  4174. (req_isp->hw_update_data.packet_opcode_type ==
  4175. CAM_ISP_PACKET_INIT_DEV) ? 0 : req->request_id;
  4176. if (req_isp->num_fence_map_out != 0) {
  4177. list_add_tail(&req->list, &ctx->active_req_list);
  4178. ctx_isp->active_req_cnt++;
  4179. CAM_DBG(CAM_ISP,
  4180. "move request %lld to active list(cnt = %d)",
  4181. req->request_id, ctx_isp->active_req_cnt);
  4182. /* if packet has buffers, set correct request id */
  4183. request_id = req->request_id;
  4184. } else {
  4185. /* no io config, so the request is completed. */
  4186. list_add_tail(&req->list, &ctx->free_req_list);
  4187. CAM_DBG(CAM_ISP,
  4188. "move active req %lld to free list(cnt=%d)",
  4189. req->request_id, ctx_isp->active_req_cnt);
  4190. }
  4191. __cam_isp_ctx_notify_trigger_util(CAM_TRIGGER_POINT_SOF, ctx_isp);
  4192. if (request_id)
  4193. ctx_isp->reported_req_id = request_id;
  4194. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  4195. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  4196. CAM_DBG(CAM_ISP, "next Substate[%s]",
  4197. __cam_isp_ctx_substate_val_to_type(
  4198. ctx_isp->substate_activated));
  4199. __cam_isp_ctx_update_event_record(ctx_isp,
  4200. CAM_ISP_CTX_EVENT_RUP, req);
  4201. return 0;
  4202. error:
  4203. /* Send SOF event as idle frame*/
  4204. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  4205. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  4206. __cam_isp_ctx_update_event_record(ctx_isp,
  4207. CAM_ISP_CTX_EVENT_RUP, NULL);
  4208. /*
  4209. * There is no request in the pending list, move the sub state machine
  4210. * to SOF sub state
  4211. */
  4212. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_SOF;
  4213. return 0;
  4214. }
  4215. static struct cam_isp_ctx_irq_ops
  4216. cam_isp_ctx_rdi_only_activated_state_machine_irq
  4217. [CAM_ISP_CTX_ACTIVATED_MAX] = {
  4218. /* SOF */
  4219. {
  4220. .irq_ops = {
  4221. NULL,
  4222. __cam_isp_ctx_rdi_only_sof_in_top_state,
  4223. __cam_isp_ctx_reg_upd_in_sof,
  4224. NULL,
  4225. NULL,
  4226. NULL,
  4227. },
  4228. },
  4229. /* APPLIED */
  4230. {
  4231. .irq_ops = {
  4232. __cam_isp_ctx_handle_error,
  4233. __cam_isp_ctx_rdi_only_sof_in_applied_state,
  4234. __cam_isp_ctx_reg_upd_in_applied_state,
  4235. NULL,
  4236. NULL,
  4237. __cam_isp_ctx_buf_done_in_applied,
  4238. },
  4239. },
  4240. /* EPOCH */
  4241. {
  4242. .irq_ops = {
  4243. __cam_isp_ctx_handle_error,
  4244. __cam_isp_ctx_rdi_only_sof_in_top_state,
  4245. NULL,
  4246. NULL,
  4247. NULL,
  4248. __cam_isp_ctx_buf_done_in_epoch,
  4249. },
  4250. },
  4251. /* BUBBLE*/
  4252. {
  4253. .irq_ops = {
  4254. __cam_isp_ctx_handle_error,
  4255. __cam_isp_ctx_rdi_only_sof_in_bubble_state,
  4256. __cam_isp_ctx_rdi_only_reg_upd_in_bubble_state,
  4257. NULL,
  4258. NULL,
  4259. __cam_isp_ctx_buf_done_in_bubble,
  4260. },
  4261. },
  4262. /* BUBBLE APPLIED ie PRE_BUBBLE */
  4263. {
  4264. .irq_ops = {
  4265. __cam_isp_ctx_handle_error,
  4266. __cam_isp_ctx_rdi_only_sof_in_bubble_applied,
  4267. __cam_isp_ctx_rdi_only_reg_upd_in_bubble_applied_state,
  4268. NULL,
  4269. NULL,
  4270. __cam_isp_ctx_buf_done_in_bubble_applied,
  4271. },
  4272. },
  4273. /* HW ERROR */
  4274. {
  4275. },
  4276. /* HALT */
  4277. {
  4278. },
  4279. };
  4280. static int __cam_isp_ctx_rdi_only_apply_req_top_state(
  4281. struct cam_context *ctx, struct cam_req_mgr_apply_request *apply)
  4282. {
  4283. int rc = 0;
  4284. struct cam_isp_context *ctx_isp =
  4285. (struct cam_isp_context *) ctx->ctx_priv;
  4286. CAM_DBG(CAM_ISP, "current Substate[%s]",
  4287. __cam_isp_ctx_substate_val_to_type(
  4288. ctx_isp->substate_activated));
  4289. rc = __cam_isp_ctx_apply_req_in_activated_state(ctx, apply,
  4290. CAM_ISP_CTX_ACTIVATED_APPLIED);
  4291. CAM_DBG(CAM_ISP, "new Substate[%s]",
  4292. __cam_isp_ctx_substate_val_to_type(
  4293. ctx_isp->substate_activated));
  4294. if (rc)
  4295. CAM_ERR_RATE_LIMIT(CAM_ISP,
  4296. "ctx_id:%d Apply failed in Substate[%s], rc %d",
  4297. ctx->ctx_id,
  4298. __cam_isp_ctx_substate_val_to_type(
  4299. ctx_isp->substate_activated), rc);
  4300. return rc;
  4301. }
  4302. static struct cam_ctx_ops
  4303. cam_isp_ctx_rdi_only_activated_state_machine
  4304. [CAM_ISP_CTX_ACTIVATED_MAX] = {
  4305. /* SOF */
  4306. {
  4307. .ioctl_ops = {},
  4308. .crm_ops = {
  4309. .apply_req = __cam_isp_ctx_rdi_only_apply_req_top_state,
  4310. },
  4311. .irq_ops = NULL,
  4312. },
  4313. /* APPLIED */
  4314. {
  4315. .ioctl_ops = {},
  4316. .crm_ops = {},
  4317. .irq_ops = NULL,
  4318. },
  4319. /* EPOCH */
  4320. {
  4321. .ioctl_ops = {},
  4322. .crm_ops = {
  4323. .apply_req = __cam_isp_ctx_rdi_only_apply_req_top_state,
  4324. },
  4325. .irq_ops = NULL,
  4326. },
  4327. /* PRE BUBBLE */
  4328. {
  4329. .ioctl_ops = {},
  4330. .crm_ops = {},
  4331. .irq_ops = NULL,
  4332. },
  4333. /* BUBBLE */
  4334. {
  4335. .ioctl_ops = {},
  4336. .crm_ops = {},
  4337. .irq_ops = NULL,
  4338. },
  4339. /* HW ERROR */
  4340. {
  4341. .ioctl_ops = {},
  4342. .crm_ops = {},
  4343. .irq_ops = NULL,
  4344. },
  4345. /* HALT */
  4346. {
  4347. .ioctl_ops = {},
  4348. .crm_ops = {},
  4349. .irq_ops = NULL,
  4350. },
  4351. };
  4352. static int __cam_isp_ctx_flush_dev_in_top_state(struct cam_context *ctx,
  4353. struct cam_flush_dev_cmd *cmd)
  4354. {
  4355. struct cam_isp_context *ctx_isp = ctx->ctx_priv;
  4356. struct cam_req_mgr_flush_request flush_req;
  4357. if (!ctx_isp->offline_context) {
  4358. CAM_ERR(CAM_ISP, "flush dev only supported in offline context");
  4359. return -EINVAL;
  4360. }
  4361. flush_req.type = (cmd->flush_type == CAM_FLUSH_TYPE_ALL) ? CAM_REQ_MGR_FLUSH_TYPE_ALL :
  4362. CAM_REQ_MGR_FLUSH_TYPE_CANCEL_REQ;
  4363. flush_req.req_id = cmd->req_id;
  4364. CAM_DBG(CAM_ISP, "offline flush (type:%u, req:%lu)", flush_req.type, flush_req.req_id);
  4365. switch (ctx->state) {
  4366. case CAM_CTX_ACQUIRED:
  4367. case CAM_CTX_ACTIVATED:
  4368. return __cam_isp_ctx_flush_req_in_top_state(ctx, &flush_req);
  4369. case CAM_CTX_READY:
  4370. return __cam_isp_ctx_flush_req_in_ready(ctx, &flush_req);
  4371. default:
  4372. CAM_ERR(CAM_ISP, "flush dev in wrong state: %d", ctx->state);
  4373. return -EINVAL;
  4374. }
  4375. if (cmd->flush_type == CAM_FLUSH_TYPE_ALL)
  4376. cam_req_mgr_workq_flush(ctx_isp->workq);
  4377. }
  4378. static void __cam_isp_ctx_free_mem_hw_entries(struct cam_context *ctx)
  4379. {
  4380. int i;
  4381. if (ctx->out_map_entries) {
  4382. for (i = 0; i < CAM_ISP_CTX_REQ_MAX; i++) {
  4383. kfree(ctx->out_map_entries[i]);
  4384. ctx->out_map_entries[i] = NULL;
  4385. }
  4386. kfree(ctx->out_map_entries);
  4387. ctx->out_map_entries = NULL;
  4388. }
  4389. if (ctx->in_map_entries) {
  4390. for (i = 0; i < CAM_ISP_CTX_REQ_MAX; i++) {
  4391. kfree(ctx->in_map_entries[i]);
  4392. ctx->in_map_entries[i] = NULL;
  4393. }
  4394. kfree(ctx->in_map_entries);
  4395. ctx->in_map_entries = NULL;
  4396. }
  4397. if (ctx->hw_update_entry) {
  4398. for (i = 0; i < CAM_ISP_CTX_REQ_MAX; i++) {
  4399. kfree(ctx->hw_update_entry[i]);
  4400. ctx->hw_update_entry[i] = NULL;
  4401. }
  4402. kfree(ctx->hw_update_entry);
  4403. ctx->hw_update_entry = NULL;
  4404. }
  4405. ctx->max_out_map_entries = 0;
  4406. ctx->max_in_map_entries = 0;
  4407. ctx->max_hw_update_entries = 0;
  4408. }
  4409. static int __cam_isp_ctx_release_hw_in_top_state(struct cam_context *ctx,
  4410. void *cmd)
  4411. {
  4412. int rc = 0;
  4413. struct cam_hw_release_args rel_arg;
  4414. struct cam_isp_context *ctx_isp =
  4415. (struct cam_isp_context *) ctx->ctx_priv;
  4416. struct cam_req_mgr_flush_request flush_req;
  4417. int i;
  4418. if (ctx_isp->hw_ctx) {
  4419. rel_arg.ctxt_to_hw_map = ctx_isp->hw_ctx;
  4420. ctx->hw_mgr_intf->hw_release(ctx->hw_mgr_intf->hw_mgr_priv,
  4421. &rel_arg);
  4422. ctx_isp->hw_ctx = NULL;
  4423. } else {
  4424. CAM_ERR(CAM_ISP, "No hw resources acquired for ctx[%u]", ctx->ctx_id);
  4425. }
  4426. ctx->last_flush_req = 0;
  4427. ctx_isp->custom_enabled = false;
  4428. ctx_isp->use_frame_header_ts = false;
  4429. ctx_isp->use_default_apply = false;
  4430. ctx_isp->frame_id = 0;
  4431. ctx_isp->active_req_cnt = 0;
  4432. ctx_isp->reported_req_id = 0;
  4433. ctx_isp->hw_acquired = false;
  4434. ctx_isp->init_received = false;
  4435. ctx_isp->support_consumed_addr = false;
  4436. ctx_isp->aeb_enabled = false;
  4437. ctx_isp->do_internal_recovery = false;
  4438. ctx_isp->req_info.last_bufdone_req_id = 0;
  4439. atomic64_set(&ctx_isp->state_monitor_head, -1);
  4440. for (i = 0; i < CAM_ISP_CTX_EVENT_MAX; i++)
  4441. atomic64_set(&ctx_isp->event_record_head[i], -1);
  4442. /*
  4443. * Ideally, we should never have any active request here.
  4444. * But we still add some sanity check code here to help the debug
  4445. */
  4446. if (!list_empty(&ctx->active_req_list))
  4447. CAM_WARN(CAM_ISP, "Active list is not empty");
  4448. /* Flush all the pending request list */
  4449. flush_req.type = CAM_REQ_MGR_FLUSH_TYPE_ALL;
  4450. flush_req.link_hdl = ctx->link_hdl;
  4451. flush_req.dev_hdl = ctx->dev_hdl;
  4452. flush_req.req_id = 0;
  4453. CAM_DBG(CAM_ISP, "try to flush pending list");
  4454. spin_lock_bh(&ctx->lock);
  4455. rc = __cam_isp_ctx_flush_req(ctx, &ctx->pending_req_list, &flush_req);
  4456. spin_unlock_bh(&ctx->lock);
  4457. __cam_isp_ctx_free_mem_hw_entries(ctx);
  4458. cam_req_mgr_workq_destroy(&ctx_isp->workq);
  4459. ctx->state = CAM_CTX_ACQUIRED;
  4460. trace_cam_context_state("ISP", ctx);
  4461. CAM_DBG(CAM_ISP, "Release device success[%u] next state %d",
  4462. ctx->ctx_id, ctx->state);
  4463. return rc;
  4464. }
  4465. /* top level state machine */
  4466. static int __cam_isp_ctx_release_dev_in_top_state(struct cam_context *ctx,
  4467. struct cam_release_dev_cmd *cmd)
  4468. {
  4469. int rc = 0;
  4470. int i;
  4471. struct cam_hw_release_args rel_arg;
  4472. struct cam_isp_context *ctx_isp =
  4473. (struct cam_isp_context *) ctx->ctx_priv;
  4474. struct cam_req_mgr_flush_request flush_req;
  4475. if (cmd && ctx_isp->hw_ctx) {
  4476. CAM_ERR(CAM_ISP, "releasing hw");
  4477. __cam_isp_ctx_release_hw_in_top_state(ctx, NULL);
  4478. }
  4479. if (ctx_isp->hw_ctx) {
  4480. rel_arg.ctxt_to_hw_map = ctx_isp->hw_ctx;
  4481. ctx->hw_mgr_intf->hw_release(ctx->hw_mgr_intf->hw_mgr_priv,
  4482. &rel_arg);
  4483. ctx_isp->hw_ctx = NULL;
  4484. }
  4485. ctx->session_hdl = -1;
  4486. ctx->dev_hdl = -1;
  4487. ctx->link_hdl = -1;
  4488. ctx->ctx_crm_intf = NULL;
  4489. ctx->last_flush_req = 0;
  4490. ctx_isp->frame_id = 0;
  4491. ctx_isp->active_req_cnt = 0;
  4492. ctx_isp->reported_req_id = 0;
  4493. ctx_isp->hw_acquired = false;
  4494. ctx_isp->init_received = false;
  4495. ctx_isp->offline_context = false;
  4496. ctx_isp->rdi_only_context = false;
  4497. ctx_isp->req_info.last_bufdone_req_id = 0;
  4498. ctx_isp->v4l2_event_sub_ids = 0;
  4499. atomic64_set(&ctx_isp->state_monitor_head, -1);
  4500. for (i = 0; i < CAM_ISP_CTX_EVENT_MAX; i++)
  4501. atomic64_set(&ctx_isp->event_record_head[i], -1);
  4502. /*
  4503. * Ideally, we should never have any active request here.
  4504. * But we still add some sanity check code here to help the debug
  4505. */
  4506. if (!list_empty(&ctx->active_req_list))
  4507. CAM_ERR(CAM_ISP, "Active list is not empty");
  4508. /* Flush all the pending request list */
  4509. flush_req.type = CAM_REQ_MGR_FLUSH_TYPE_ALL;
  4510. flush_req.link_hdl = ctx->link_hdl;
  4511. flush_req.dev_hdl = ctx->dev_hdl;
  4512. flush_req.req_id = 0;
  4513. CAM_DBG(CAM_ISP, "try to flush pending list");
  4514. spin_lock_bh(&ctx->lock);
  4515. rc = __cam_isp_ctx_flush_req(ctx, &ctx->pending_req_list, &flush_req);
  4516. spin_unlock_bh(&ctx->lock);
  4517. __cam_isp_ctx_free_mem_hw_entries(ctx);
  4518. ctx->state = CAM_CTX_AVAILABLE;
  4519. trace_cam_context_state("ISP", ctx);
  4520. CAM_DBG(CAM_ISP, "Release device success[%u] next state %d",
  4521. ctx->ctx_id, ctx->state);
  4522. return rc;
  4523. }
  4524. static int __cam_isp_ctx_config_dev_in_top_state(
  4525. struct cam_context *ctx, struct cam_config_dev_cmd *cmd)
  4526. {
  4527. int rc = 0, i;
  4528. struct cam_ctx_request *req = NULL;
  4529. struct cam_isp_ctx_req *req_isp;
  4530. struct cam_packet *packet;
  4531. size_t remain_len = 0;
  4532. struct cam_hw_prepare_update_args cfg = {0};
  4533. struct cam_req_mgr_add_request add_req;
  4534. struct cam_isp_context *ctx_isp =
  4535. (struct cam_isp_context *) ctx->ctx_priv;
  4536. struct cam_hw_cmd_args hw_cmd_args;
  4537. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  4538. uint32_t packet_opcode = 0;
  4539. CAM_DBG(CAM_ISP, "get free request object......");
  4540. /* get free request */
  4541. spin_lock_bh(&ctx->lock);
  4542. if (!list_empty(&ctx->free_req_list)) {
  4543. req = list_first_entry(&ctx->free_req_list,
  4544. struct cam_ctx_request, list);
  4545. list_del_init(&req->list);
  4546. }
  4547. spin_unlock_bh(&ctx->lock);
  4548. if (!req) {
  4549. CAM_ERR(CAM_ISP, "No more request obj free");
  4550. return -ENOMEM;
  4551. }
  4552. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  4553. remain_len = cam_context_parse_config_cmd(ctx, cmd, &packet);
  4554. if (IS_ERR(packet)) {
  4555. rc = PTR_ERR(packet);
  4556. goto free_req;
  4557. }
  4558. /* Query the packet opcode */
  4559. hw_cmd_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  4560. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  4561. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_GET_PACKET_OPCODE;
  4562. isp_hw_cmd_args.cmd_data = (void *)packet;
  4563. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  4564. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  4565. &hw_cmd_args);
  4566. if (rc) {
  4567. CAM_ERR(CAM_ISP, "HW command failed");
  4568. goto free_req;
  4569. }
  4570. packet_opcode = isp_hw_cmd_args.u.packet_op_code;
  4571. if ((packet_opcode == CAM_ISP_PACKET_UPDATE_DEV)
  4572. && (packet->header.request_id <= ctx->last_flush_req)) {
  4573. CAM_INFO(CAM_ISP,
  4574. "request %lld has been flushed, reject packet",
  4575. packet->header.request_id);
  4576. rc = -EBADR;
  4577. goto free_req;
  4578. } else if ((packet_opcode == CAM_ISP_PACKET_INIT_DEV)
  4579. && (packet->header.request_id <= ctx->last_flush_req)
  4580. && ctx->last_flush_req && packet->header.request_id) {
  4581. CAM_WARN(CAM_ISP,
  4582. "last flushed req is %lld, config dev(init) for req %lld",
  4583. ctx->last_flush_req, packet->header.request_id);
  4584. rc = -EBADR;
  4585. goto free_req;
  4586. }
  4587. cfg.packet = packet;
  4588. cfg.remain_len = remain_len;
  4589. cfg.ctxt_to_hw_map = ctx_isp->hw_ctx;
  4590. cfg.max_hw_update_entries = ctx->max_hw_update_entries;
  4591. cfg.hw_update_entries = req_isp->cfg;
  4592. cfg.max_out_map_entries = ctx->max_out_map_entries;
  4593. cfg.max_in_map_entries = ctx->max_in_map_entries;
  4594. cfg.out_map_entries = req_isp->fence_map_out;
  4595. cfg.in_map_entries = req_isp->fence_map_in;
  4596. cfg.priv = &req_isp->hw_update_data;
  4597. cfg.pf_data = &(req->pf_data);
  4598. cfg.num_out_map_entries = 0;
  4599. cfg.num_in_map_entries = 0;
  4600. memset(&req_isp->hw_update_data, 0, sizeof(req_isp->hw_update_data));
  4601. rc = ctx->hw_mgr_intf->hw_prepare_update(
  4602. ctx->hw_mgr_intf->hw_mgr_priv, &cfg);
  4603. if (rc != 0) {
  4604. CAM_ERR(CAM_ISP, "Prepare config packet failed in HW layer");
  4605. rc = -EFAULT;
  4606. goto free_req;
  4607. }
  4608. req_isp->num_cfg = cfg.num_hw_update_entries;
  4609. req_isp->num_fence_map_out = cfg.num_out_map_entries;
  4610. req_isp->num_fence_map_in = cfg.num_in_map_entries;
  4611. req_isp->num_acked = 0;
  4612. req_isp->num_deferred_acks = 0;
  4613. req_isp->bubble_detected = false;
  4614. req_isp->cdm_reset_before_apply = false;
  4615. req_isp->hw_update_data.packet = packet;
  4616. for (i = 0; i < req_isp->num_fence_map_out; i++) {
  4617. rc = cam_sync_get_obj_ref(req_isp->fence_map_out[i].sync_id);
  4618. if (rc) {
  4619. CAM_ERR(CAM_ISP, "Can't get ref for fence %d",
  4620. req_isp->fence_map_out[i].sync_id);
  4621. goto put_ref;
  4622. }
  4623. }
  4624. CAM_DBG(CAM_ISP,
  4625. "packet req-id:%lld, opcode:%d, num_entry:%d, num_fence_out: %d, num_fence_in: %d",
  4626. packet->header.request_id, req_isp->hw_update_data.packet_opcode_type,
  4627. req_isp->num_cfg, req_isp->num_fence_map_out, req_isp->num_fence_map_in);
  4628. req->request_id = packet->header.request_id;
  4629. req->status = 1;
  4630. if (req_isp->hw_update_data.packet_opcode_type ==
  4631. CAM_ISP_PACKET_INIT_DEV) {
  4632. if (ctx->state < CAM_CTX_ACTIVATED) {
  4633. rc = __cam_isp_ctx_enqueue_init_request(ctx, req);
  4634. if (rc)
  4635. CAM_ERR(CAM_ISP, "Enqueue INIT pkt failed");
  4636. ctx_isp->init_received = true;
  4637. } else {
  4638. rc = -EINVAL;
  4639. CAM_ERR(CAM_ISP, "Recevied INIT pkt in wrong state:%d",
  4640. ctx->state);
  4641. }
  4642. } else {
  4643. if ((ctx->state == CAM_CTX_FLUSHED) || (ctx->state < CAM_CTX_READY)) {
  4644. rc = -EINVAL;
  4645. CAM_ERR(CAM_ISP, "Received update req %lld in wrong state:%d",
  4646. req->request_id, ctx->state);
  4647. goto put_ref;
  4648. }
  4649. if (ctx_isp->offline_context) {
  4650. __cam_isp_ctx_enqueue_request_in_order(ctx, req);
  4651. } else if (ctx->ctx_crm_intf->add_req) {
  4652. memset(&add_req, 0, sizeof(add_req));
  4653. add_req.link_hdl = ctx->link_hdl;
  4654. add_req.dev_hdl = ctx->dev_hdl;
  4655. add_req.req_id = req->request_id;
  4656. rc = ctx->ctx_crm_intf->add_req(&add_req);
  4657. if (rc) {
  4658. CAM_ERR(CAM_ISP, "Add req failed: req id=%llu",
  4659. req->request_id);
  4660. } else {
  4661. __cam_isp_ctx_enqueue_request_in_order(
  4662. ctx, req);
  4663. }
  4664. } else {
  4665. CAM_ERR(CAM_ISP, "Unable to add request: req id=%llu", req->request_id);
  4666. rc = -ENODEV;
  4667. }
  4668. }
  4669. if (rc)
  4670. goto put_ref;
  4671. CAM_DBG(CAM_REQ,
  4672. "Preprocessing Config req_id %lld successful on ctx %u",
  4673. req->request_id, ctx->ctx_id);
  4674. if (ctx_isp->offline_context && atomic_read(&ctx_isp->rxd_epoch)) {
  4675. __cam_isp_ctx_schedule_apply_req_offline(ctx_isp);
  4676. }
  4677. return rc;
  4678. put_ref:
  4679. for (--i; i >= 0; i--) {
  4680. if (cam_sync_put_obj_ref(req_isp->fence_map_out[i].sync_id))
  4681. CAM_ERR(CAM_CTXT, "Failed to put ref of fence %d",
  4682. req_isp->fence_map_out[i].sync_id);
  4683. }
  4684. free_req:
  4685. spin_lock_bh(&ctx->lock);
  4686. list_add_tail(&req->list, &ctx->free_req_list);
  4687. spin_unlock_bh(&ctx->lock);
  4688. return rc;
  4689. }
  4690. static int __cam_isp_ctx_allocate_mem_hw_entries(
  4691. struct cam_context *ctx,
  4692. struct cam_hw_acquire_args *param)
  4693. {
  4694. int rc = 0, i;
  4695. uint32_t max_res = 0;
  4696. uint32_t max_hw_upd_entries = CAM_ISP_CTX_CFG_MAX;
  4697. struct cam_ctx_request *req;
  4698. struct cam_ctx_request *temp_req;
  4699. struct cam_isp_ctx_req *req_isp;
  4700. if (!param->op_params.param_list[0])
  4701. max_res = CAM_ISP_CTX_RES_MAX;
  4702. else {
  4703. max_res = param->op_params.param_list[0];
  4704. if (param->op_flags & CAM_IFE_CTX_SFE_EN) {
  4705. max_res += param->op_params.param_list[1];
  4706. max_hw_upd_entries = CAM_ISP_SFE_CTX_CFG_MAX;
  4707. }
  4708. }
  4709. ctx->max_in_map_entries = max_res;
  4710. ctx->max_out_map_entries = max_res;
  4711. ctx->max_hw_update_entries = max_hw_upd_entries;
  4712. CAM_DBG(CAM_ISP,
  4713. "Allocate max_entries: 0x%x max_res: 0x%x is_sfe_en: %d",
  4714. max_hw_upd_entries, max_res, (param->op_flags & CAM_IFE_CTX_SFE_EN));
  4715. ctx->hw_update_entry = kcalloc(CAM_ISP_CTX_REQ_MAX, sizeof(struct cam_hw_update_entry *),
  4716. GFP_KERNEL);
  4717. if (!ctx->hw_update_entry) {
  4718. CAM_ERR(CAM_CTXT, "%s[%d] no memory ",
  4719. ctx->dev_name, ctx->ctx_id);
  4720. return -ENOMEM;
  4721. }
  4722. for (i = 0; i < CAM_ISP_CTX_REQ_MAX; i++) {
  4723. ctx->hw_update_entry[i] = kcalloc(ctx->max_hw_update_entries,
  4724. sizeof(struct cam_hw_update_entry), GFP_KERNEL);
  4725. if (!ctx->hw_update_entry[i]) {
  4726. CAM_ERR(CAM_CTXT, "%s[%d] no memory for hw_update_entry: %u",
  4727. ctx->dev_name, ctx->ctx_id, i);
  4728. return -ENOMEM;
  4729. }
  4730. }
  4731. ctx->in_map_entries = kcalloc(CAM_ISP_CTX_REQ_MAX, sizeof(struct cam_hw_fence_map_entry *),
  4732. GFP_KERNEL);
  4733. if (!ctx->in_map_entries) {
  4734. CAM_ERR(CAM_CTXT, "%s[%d] no memory for in_map_entries",
  4735. ctx->dev_name, ctx->ctx_id);
  4736. rc = -ENOMEM;
  4737. goto end;
  4738. }
  4739. for (i = 0; i < CAM_ISP_CTX_REQ_MAX; i++) {
  4740. ctx->in_map_entries[i] = kcalloc(ctx->max_in_map_entries,
  4741. sizeof(struct cam_hw_fence_map_entry),
  4742. GFP_KERNEL);
  4743. if (!ctx->in_map_entries[i]) {
  4744. CAM_ERR(CAM_CTXT, "%s[%d] no memory for in_map_entries: %u",
  4745. ctx->dev_name, ctx->ctx_id, i);
  4746. rc = -ENOMEM;
  4747. goto end;
  4748. }
  4749. }
  4750. ctx->out_map_entries = kcalloc(CAM_ISP_CTX_REQ_MAX, sizeof(struct cam_hw_fence_map_entry *),
  4751. GFP_KERNEL);
  4752. if (!ctx->out_map_entries) {
  4753. CAM_ERR(CAM_CTXT, "%s[%d] no memory for out_map_entries",
  4754. ctx->dev_name, ctx->ctx_id);
  4755. rc = -ENOMEM;
  4756. goto end;
  4757. }
  4758. for (i = 0; i < CAM_ISP_CTX_REQ_MAX; i++) {
  4759. ctx->out_map_entries[i] = kcalloc(ctx->max_out_map_entries,
  4760. sizeof(struct cam_hw_fence_map_entry),
  4761. GFP_KERNEL);
  4762. if (!ctx->out_map_entries[i]) {
  4763. CAM_ERR(CAM_CTXT, "%s[%d] no memory for out_map_entries: %u",
  4764. ctx->dev_name, ctx->ctx_id, i);
  4765. rc = -ENOMEM;
  4766. goto end;
  4767. }
  4768. }
  4769. list_for_each_entry_safe(req, temp_req,
  4770. &ctx->free_req_list, list) {
  4771. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  4772. req_isp->cfg = ctx->hw_update_entry[req->index];
  4773. req_isp->fence_map_in = ctx->in_map_entries[req->index];
  4774. req_isp->fence_map_out = ctx->out_map_entries[req->index];
  4775. }
  4776. return rc;
  4777. end:
  4778. __cam_isp_ctx_free_mem_hw_entries(ctx);
  4779. return rc;
  4780. }
  4781. static int __cam_isp_ctx_acquire_dev_in_available(struct cam_context *ctx,
  4782. struct cam_acquire_dev_cmd *cmd)
  4783. {
  4784. int rc = 0;
  4785. int i;
  4786. struct cam_hw_acquire_args param;
  4787. struct cam_isp_resource *isp_res = NULL;
  4788. struct cam_create_dev_hdl req_hdl_param;
  4789. struct cam_hw_release_args release;
  4790. struct cam_isp_context *ctx_isp =
  4791. (struct cam_isp_context *) ctx->ctx_priv;
  4792. struct cam_hw_cmd_args hw_cmd_args;
  4793. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  4794. if (!ctx->hw_mgr_intf) {
  4795. CAM_ERR(CAM_ISP, "HW interface is not ready");
  4796. rc = -EFAULT;
  4797. goto end;
  4798. }
  4799. CAM_DBG(CAM_ISP,
  4800. "session_hdl 0x%x, num_resources %d, hdl type %d, res %lld",
  4801. cmd->session_handle, cmd->num_resources,
  4802. cmd->handle_type, cmd->resource_hdl);
  4803. ctx_isp->v4l2_event_sub_ids = cam_req_mgr_get_id_subscribed();
  4804. if (cmd->num_resources == CAM_API_COMPAT_CONSTANT) {
  4805. ctx_isp->split_acquire = true;
  4806. CAM_DBG(CAM_ISP, "Acquire dev handle");
  4807. goto get_dev_handle;
  4808. }
  4809. if (cmd->num_resources > CAM_ISP_CTX_RES_MAX) {
  4810. CAM_ERR(CAM_ISP, "Too much resources in the acquire");
  4811. rc = -ENOMEM;
  4812. goto end;
  4813. }
  4814. /* for now we only support user pointer */
  4815. if (cmd->handle_type != 1) {
  4816. CAM_ERR(CAM_ISP, "Only user pointer is supported");
  4817. rc = -EINVAL;
  4818. goto end;
  4819. }
  4820. isp_res = kzalloc(
  4821. sizeof(*isp_res)*cmd->num_resources, GFP_KERNEL);
  4822. if (!isp_res) {
  4823. rc = -ENOMEM;
  4824. goto end;
  4825. }
  4826. CAM_DBG(CAM_ISP, "start copy %d resources from user",
  4827. cmd->num_resources);
  4828. if (copy_from_user(isp_res, u64_to_user_ptr(cmd->resource_hdl),
  4829. sizeof(*isp_res)*cmd->num_resources)) {
  4830. rc = -EFAULT;
  4831. goto free_res;
  4832. }
  4833. memset(&param, 0, sizeof(param));
  4834. param.context_data = ctx;
  4835. param.event_cb = ctx->irq_cb_intf;
  4836. param.num_acq = cmd->num_resources;
  4837. param.acquire_info = (uintptr_t) isp_res;
  4838. rc = __cam_isp_ctx_allocate_mem_hw_entries(ctx, &param);
  4839. if (rc) {
  4840. CAM_ERR(CAM_ISP, "Ctx[%d] allocate hw entry fail",
  4841. ctx->ctx_id);
  4842. goto free_res;
  4843. }
  4844. /* call HW manager to reserve the resource */
  4845. rc = ctx->hw_mgr_intf->hw_acquire(ctx->hw_mgr_intf->hw_mgr_priv,
  4846. &param);
  4847. if (rc != 0) {
  4848. CAM_ERR(CAM_ISP, "Acquire device failed");
  4849. goto free_res;
  4850. }
  4851. /* Query the context has rdi only resource */
  4852. hw_cmd_args.ctxt_to_hw_map = param.ctxt_to_hw_map;
  4853. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  4854. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_CMD_CTX_TYPE;
  4855. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  4856. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  4857. &hw_cmd_args);
  4858. if (rc) {
  4859. CAM_ERR(CAM_ISP, "HW command failed");
  4860. goto free_hw;
  4861. }
  4862. if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_RDI) {
  4863. /*
  4864. * this context has rdi only resource assign rdi only
  4865. * state machine
  4866. */
  4867. CAM_DBG(CAM_ISP, "RDI only session Context");
  4868. ctx_isp->substate_machine_irq =
  4869. cam_isp_ctx_rdi_only_activated_state_machine_irq;
  4870. ctx_isp->substate_machine =
  4871. cam_isp_ctx_rdi_only_activated_state_machine;
  4872. ctx_isp->rdi_only_context = true;
  4873. } else if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_FS2) {
  4874. CAM_DBG(CAM_ISP, "FS2 Session has PIX, RD and RDI");
  4875. ctx_isp->substate_machine_irq =
  4876. cam_isp_ctx_fs2_state_machine_irq;
  4877. ctx_isp->substate_machine =
  4878. cam_isp_ctx_fs2_state_machine;
  4879. } else if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_OFFLINE) {
  4880. CAM_DBG(CAM_ISP, "offline Session has PIX and RD resources");
  4881. ctx_isp->substate_machine_irq =
  4882. cam_isp_ctx_offline_state_machine_irq;
  4883. } else {
  4884. CAM_DBG(CAM_ISP, "Session has PIX or PIX and RDI resources");
  4885. ctx_isp->substate_machine_irq =
  4886. cam_isp_ctx_activated_state_machine_irq;
  4887. ctx_isp->substate_machine =
  4888. cam_isp_ctx_activated_state_machine;
  4889. }
  4890. ctx_isp->hw_ctx = param.ctxt_to_hw_map;
  4891. ctx_isp->hw_acquired = true;
  4892. ctx_isp->split_acquire = false;
  4893. ctx->ctxt_to_hw_map = param.ctxt_to_hw_map;
  4894. atomic64_set(&ctx_isp->state_monitor_head, -1);
  4895. for (i = 0; i < CAM_ISP_CTX_EVENT_MAX; i++)
  4896. atomic64_set(&ctx_isp->event_record_head[i], -1);
  4897. kfree(isp_res);
  4898. isp_res = NULL;
  4899. get_dev_handle:
  4900. req_hdl_param.session_hdl = cmd->session_handle;
  4901. /* bridge is not ready for these flags. so false for now */
  4902. req_hdl_param.v4l2_sub_dev_flag = 0;
  4903. req_hdl_param.media_entity_flag = 0;
  4904. req_hdl_param.ops = ctx->crm_ctx_intf;
  4905. req_hdl_param.priv = ctx;
  4906. req_hdl_param.dev_id = CAM_ISP;
  4907. CAM_DBG(CAM_ISP, "get device handle form bridge");
  4908. ctx->dev_hdl = cam_create_device_hdl(&req_hdl_param);
  4909. if (ctx->dev_hdl <= 0) {
  4910. rc = -EFAULT;
  4911. CAM_ERR(CAM_ISP, "Can not create device handle");
  4912. goto free_hw;
  4913. }
  4914. cmd->dev_handle = ctx->dev_hdl;
  4915. /* store session information */
  4916. ctx->session_hdl = cmd->session_handle;
  4917. ctx->state = CAM_CTX_ACQUIRED;
  4918. trace_cam_context_state("ISP", ctx);
  4919. CAM_DBG(CAM_ISP,
  4920. "Acquire success on session_hdl 0x%x num_rsrces %d ctx %u",
  4921. cmd->session_handle, cmd->num_resources, ctx->ctx_id);
  4922. return rc;
  4923. free_hw:
  4924. release.ctxt_to_hw_map = ctx_isp->hw_ctx;
  4925. if (ctx_isp->hw_acquired)
  4926. ctx->hw_mgr_intf->hw_release(ctx->hw_mgr_intf->hw_mgr_priv,
  4927. &release);
  4928. ctx_isp->hw_ctx = NULL;
  4929. ctx_isp->hw_acquired = false;
  4930. free_res:
  4931. kfree(isp_res);
  4932. end:
  4933. return rc;
  4934. }
  4935. static int __cam_isp_ctx_acquire_hw_v1(struct cam_context *ctx,
  4936. void *args)
  4937. {
  4938. int rc = 0;
  4939. int i;
  4940. struct cam_acquire_hw_cmd_v1 *cmd =
  4941. (struct cam_acquire_hw_cmd_v1 *)args;
  4942. struct cam_hw_acquire_args param;
  4943. struct cam_hw_release_args release;
  4944. struct cam_isp_context *ctx_isp =
  4945. (struct cam_isp_context *) ctx->ctx_priv;
  4946. struct cam_hw_cmd_args hw_cmd_args;
  4947. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  4948. struct cam_isp_acquire_hw_info *acquire_hw_info = NULL;
  4949. if (!ctx->hw_mgr_intf) {
  4950. CAM_ERR(CAM_ISP, "HW interface is not ready");
  4951. rc = -EFAULT;
  4952. goto end;
  4953. }
  4954. CAM_DBG(CAM_ISP,
  4955. "session_hdl 0x%x, hdl type %d, res %lld",
  4956. cmd->session_handle, cmd->handle_type, cmd->resource_hdl);
  4957. /* for now we only support user pointer */
  4958. if (cmd->handle_type != 1) {
  4959. CAM_ERR(CAM_ISP, "Only user pointer is supported");
  4960. rc = -EINVAL;
  4961. goto end;
  4962. }
  4963. if (cmd->data_size < sizeof(*acquire_hw_info)) {
  4964. CAM_ERR(CAM_ISP, "data_size is not a valid value");
  4965. goto end;
  4966. }
  4967. acquire_hw_info = kzalloc(cmd->data_size, GFP_KERNEL);
  4968. if (!acquire_hw_info) {
  4969. rc = -ENOMEM;
  4970. goto end;
  4971. }
  4972. CAM_DBG(CAM_ISP, "start copy resources from user");
  4973. if (copy_from_user(acquire_hw_info, (void __user *)cmd->resource_hdl,
  4974. cmd->data_size)) {
  4975. rc = -EFAULT;
  4976. goto free_res;
  4977. }
  4978. memset(&param, 0, sizeof(param));
  4979. param.context_data = ctx;
  4980. param.event_cb = ctx->irq_cb_intf;
  4981. param.num_acq = CAM_API_COMPAT_CONSTANT;
  4982. param.acquire_info_size = cmd->data_size;
  4983. param.acquire_info = (uint64_t) acquire_hw_info;
  4984. param.mini_dump_cb = __cam_isp_ctx_minidump_cb;
  4985. rc = __cam_isp_ctx_allocate_mem_hw_entries(ctx,
  4986. &param);
  4987. if (rc) {
  4988. CAM_ERR(CAM_ISP, "Ctx[%d] allocate hw entry fail",
  4989. ctx->ctx_id);
  4990. goto free_res;
  4991. }
  4992. /* call HW manager to reserve the resource */
  4993. rc = ctx->hw_mgr_intf->hw_acquire(ctx->hw_mgr_intf->hw_mgr_priv,
  4994. &param);
  4995. if (rc != 0) {
  4996. CAM_ERR(CAM_ISP, "Acquire device failed");
  4997. goto free_res;
  4998. }
  4999. ctx_isp->support_consumed_addr =
  5000. (param.op_flags & CAM_IFE_CTX_FRAME_HEADER_EN);
  5001. /* Query the context has rdi only resource */
  5002. hw_cmd_args.ctxt_to_hw_map = param.ctxt_to_hw_map;
  5003. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  5004. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_CMD_CTX_TYPE;
  5005. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  5006. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  5007. &hw_cmd_args);
  5008. if (rc) {
  5009. CAM_ERR(CAM_ISP, "HW command failed");
  5010. goto free_hw;
  5011. }
  5012. if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_RDI) {
  5013. /*
  5014. * this context has rdi only resource assign rdi only
  5015. * state machine
  5016. */
  5017. CAM_DBG(CAM_ISP, "RDI only session Context");
  5018. ctx_isp->substate_machine_irq =
  5019. cam_isp_ctx_rdi_only_activated_state_machine_irq;
  5020. ctx_isp->substate_machine =
  5021. cam_isp_ctx_rdi_only_activated_state_machine;
  5022. ctx_isp->rdi_only_context = true;
  5023. } else if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_FS2) {
  5024. CAM_DBG(CAM_ISP, "FS2 Session has PIX, RD and RDI");
  5025. ctx_isp->substate_machine_irq =
  5026. cam_isp_ctx_fs2_state_machine_irq;
  5027. ctx_isp->substate_machine =
  5028. cam_isp_ctx_fs2_state_machine;
  5029. } else if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_OFFLINE) {
  5030. CAM_DBG(CAM_ISP, "Offline session has PIX and RD resources");
  5031. ctx_isp->substate_machine_irq =
  5032. cam_isp_ctx_offline_state_machine_irq;
  5033. ctx_isp->substate_machine = NULL;
  5034. } else {
  5035. CAM_DBG(CAM_ISP, "Session has PIX or PIX and RDI resources");
  5036. ctx_isp->substate_machine_irq =
  5037. cam_isp_ctx_activated_state_machine_irq;
  5038. ctx_isp->substate_machine =
  5039. cam_isp_ctx_activated_state_machine;
  5040. }
  5041. ctx_isp->hw_ctx = param.ctxt_to_hw_map;
  5042. ctx_isp->hw_acquired = true;
  5043. ctx->ctxt_to_hw_map = param.ctxt_to_hw_map;
  5044. atomic64_set(&ctx_isp->state_monitor_head, -1);
  5045. for (i = 0; i < CAM_ISP_CTX_EVENT_MAX; i++)
  5046. atomic64_set(&ctx_isp->event_record_head[i], -1);
  5047. trace_cam_context_state("ISP", ctx);
  5048. CAM_DBG(CAM_ISP,
  5049. "Acquire success on session_hdl 0x%xs ctx_type %d ctx_id %u",
  5050. ctx->session_hdl, isp_hw_cmd_args.u.ctx_type, ctx->ctx_id);
  5051. kfree(acquire_hw_info);
  5052. return rc;
  5053. free_hw:
  5054. release.ctxt_to_hw_map = ctx_isp->hw_ctx;
  5055. ctx->hw_mgr_intf->hw_release(ctx->hw_mgr_intf->hw_mgr_priv, &release);
  5056. ctx_isp->hw_ctx = NULL;
  5057. ctx_isp->hw_acquired = false;
  5058. free_res:
  5059. kfree(acquire_hw_info);
  5060. end:
  5061. return rc;
  5062. }
  5063. static void cam_req_mgr_process_workq_offline_ife_worker(struct work_struct *w)
  5064. {
  5065. cam_req_mgr_process_workq(w);
  5066. }
  5067. static int __cam_isp_ctx_acquire_hw_v2(struct cam_context *ctx,
  5068. void *args)
  5069. {
  5070. int rc = 0, i, j;
  5071. struct cam_acquire_hw_cmd_v2 *cmd =
  5072. (struct cam_acquire_hw_cmd_v2 *)args;
  5073. struct cam_hw_acquire_args param;
  5074. struct cam_hw_release_args release;
  5075. struct cam_isp_context *ctx_isp =
  5076. (struct cam_isp_context *) ctx->ctx_priv;
  5077. struct cam_hw_cmd_args hw_cmd_args;
  5078. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  5079. struct cam_isp_acquire_hw_info *acquire_hw_info = NULL;
  5080. if (!ctx->hw_mgr_intf) {
  5081. CAM_ERR(CAM_ISP, "HW interface is not ready");
  5082. rc = -EFAULT;
  5083. goto end;
  5084. }
  5085. CAM_DBG(CAM_ISP,
  5086. "session_hdl 0x%x, hdl type %d, res %lld",
  5087. cmd->session_handle, cmd->handle_type, cmd->resource_hdl);
  5088. /* for now we only support user pointer */
  5089. if (cmd->handle_type != 1) {
  5090. CAM_ERR(CAM_ISP, "Only user pointer is supported");
  5091. rc = -EINVAL;
  5092. goto end;
  5093. }
  5094. if (cmd->data_size < sizeof(*acquire_hw_info)) {
  5095. CAM_ERR(CAM_ISP, "data_size is not a valid value");
  5096. goto end;
  5097. }
  5098. acquire_hw_info = kzalloc(cmd->data_size, GFP_KERNEL);
  5099. if (!acquire_hw_info) {
  5100. rc = -ENOMEM;
  5101. goto end;
  5102. }
  5103. CAM_DBG(CAM_ISP, "start copy resources from user");
  5104. if (copy_from_user(acquire_hw_info, (void __user *)cmd->resource_hdl,
  5105. cmd->data_size)) {
  5106. rc = -EFAULT;
  5107. goto free_res;
  5108. }
  5109. memset(&param, 0, sizeof(param));
  5110. param.context_data = ctx;
  5111. param.event_cb = ctx->irq_cb_intf;
  5112. param.num_acq = CAM_API_COMPAT_CONSTANT;
  5113. param.acquire_info_size = cmd->data_size;
  5114. param.acquire_info = (uint64_t) acquire_hw_info;
  5115. param.mini_dump_cb = __cam_isp_ctx_minidump_cb;
  5116. /* call HW manager to reserve the resource */
  5117. rc = ctx->hw_mgr_intf->hw_acquire(ctx->hw_mgr_intf->hw_mgr_priv,
  5118. &param);
  5119. if (rc != 0) {
  5120. CAM_ERR(CAM_ISP, "Acquire device failed");
  5121. goto free_res;
  5122. }
  5123. rc = __cam_isp_ctx_allocate_mem_hw_entries(ctx, &param);
  5124. if (rc) {
  5125. CAM_ERR(CAM_ISP, "Ctx[%d] allocate hw entry fail",
  5126. ctx->ctx_id);
  5127. goto free_hw;
  5128. }
  5129. /*
  5130. * Set feature flag if applicable
  5131. * custom hw is supported only on v2
  5132. */
  5133. ctx_isp->custom_enabled =
  5134. (param.op_flags & CAM_IFE_CTX_CUSTOM_EN);
  5135. ctx_isp->use_frame_header_ts =
  5136. (param.op_flags & CAM_IFE_CTX_FRAME_HEADER_EN);
  5137. ctx_isp->use_default_apply =
  5138. (param.op_flags & CAM_IFE_CTX_APPLY_DEFAULT_CFG);
  5139. ctx_isp->support_consumed_addr =
  5140. (param.op_flags & CAM_IFE_CTX_CONSUME_ADDR_EN);
  5141. ctx_isp->aeb_enabled =
  5142. (param.op_flags & CAM_IFE_CTX_AEB_EN);
  5143. if ((ctx_isp->aeb_enabled) && (!isp_ctx_debug.disable_internal_recovery))
  5144. ctx_isp->do_internal_recovery = true;
  5145. /* Query the context has rdi only resource */
  5146. hw_cmd_args.ctxt_to_hw_map = param.ctxt_to_hw_map;
  5147. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  5148. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_CMD_CTX_TYPE;
  5149. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  5150. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  5151. &hw_cmd_args);
  5152. if (rc) {
  5153. CAM_ERR(CAM_ISP, "HW command failed");
  5154. goto free_hw;
  5155. }
  5156. if (param.valid_acquired_hw) {
  5157. for (i = 0; i < CAM_MAX_ACQ_RES; i++)
  5158. cmd->hw_info.acquired_hw_id[i] =
  5159. param.acquired_hw_id[i];
  5160. for (i = 0; i < CAM_MAX_ACQ_RES; i++)
  5161. for (j = 0; j < CAM_MAX_HW_SPLIT; j++)
  5162. cmd->hw_info.acquired_hw_path[i][j] =
  5163. param.acquired_hw_path[i][j];
  5164. }
  5165. cmd->hw_info.valid_acquired_hw =
  5166. param.valid_acquired_hw;
  5167. cmd->hw_info.valid_acquired_hw = param.valid_acquired_hw;
  5168. if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_RDI) {
  5169. /*
  5170. * this context has rdi only resource assign rdi only
  5171. * state machine
  5172. */
  5173. CAM_DBG(CAM_ISP, "RDI only session Context");
  5174. ctx_isp->substate_machine_irq =
  5175. cam_isp_ctx_rdi_only_activated_state_machine_irq;
  5176. ctx_isp->substate_machine =
  5177. cam_isp_ctx_rdi_only_activated_state_machine;
  5178. ctx_isp->rdi_only_context = true;
  5179. } else if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_FS2) {
  5180. CAM_DBG(CAM_ISP, "FS2 Session has PIX, RD and RDI");
  5181. ctx_isp->substate_machine_irq =
  5182. cam_isp_ctx_fs2_state_machine_irq;
  5183. ctx_isp->substate_machine =
  5184. cam_isp_ctx_fs2_state_machine;
  5185. } else if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_OFFLINE) {
  5186. CAM_DBG(CAM_ISP, "Offline Session has PIX and RD resources");
  5187. ctx_isp->substate_machine_irq =
  5188. cam_isp_ctx_offline_state_machine_irq;
  5189. ctx_isp->substate_machine = NULL;
  5190. ctx_isp->offline_context = true;
  5191. rc = cam_req_mgr_workq_create("offline_ife", 20,
  5192. &ctx_isp->workq, CRM_WORKQ_USAGE_IRQ, 0,
  5193. cam_req_mgr_process_workq_offline_ife_worker);
  5194. if (rc)
  5195. CAM_ERR(CAM_ISP,
  5196. "Failed to create workq for offline IFE rc:%d",
  5197. rc);
  5198. } else {
  5199. CAM_DBG(CAM_ISP, "Session has PIX or PIX and RDI resources");
  5200. ctx_isp->substate_machine_irq =
  5201. cam_isp_ctx_activated_state_machine_irq;
  5202. ctx_isp->substate_machine =
  5203. cam_isp_ctx_activated_state_machine;
  5204. }
  5205. ctx_isp->hw_ctx = param.ctxt_to_hw_map;
  5206. ctx_isp->hw_acquired = true;
  5207. ctx->ctxt_to_hw_map = param.ctxt_to_hw_map;
  5208. trace_cam_context_state("ISP", ctx);
  5209. CAM_DBG(CAM_ISP,
  5210. "Acquire success on session_hdl 0x%xs ctx_type %d ctx_id %u",
  5211. ctx->session_hdl, isp_hw_cmd_args.u.ctx_type, ctx->ctx_id);
  5212. kfree(acquire_hw_info);
  5213. return rc;
  5214. free_hw:
  5215. release.ctxt_to_hw_map = ctx_isp->hw_ctx;
  5216. ctx->hw_mgr_intf->hw_release(ctx->hw_mgr_intf->hw_mgr_priv, &release);
  5217. ctx_isp->hw_ctx = NULL;
  5218. ctx_isp->hw_acquired = false;
  5219. free_res:
  5220. kfree(acquire_hw_info);
  5221. end:
  5222. return rc;
  5223. }
  5224. static int __cam_isp_ctx_acquire_hw_in_acquired(struct cam_context *ctx,
  5225. void *args)
  5226. {
  5227. int rc = -EINVAL;
  5228. uint32_t api_version;
  5229. if (!ctx || !args) {
  5230. CAM_ERR(CAM_ISP, "Invalid input pointer");
  5231. return rc;
  5232. }
  5233. api_version = *((uint32_t *)args);
  5234. if (api_version == 1)
  5235. rc = __cam_isp_ctx_acquire_hw_v1(ctx, args);
  5236. else if (api_version == 2)
  5237. rc = __cam_isp_ctx_acquire_hw_v2(ctx, args);
  5238. else
  5239. CAM_ERR(CAM_ISP, "Unsupported api version %d", api_version);
  5240. return rc;
  5241. }
  5242. static int __cam_isp_ctx_config_dev_in_acquired(struct cam_context *ctx,
  5243. struct cam_config_dev_cmd *cmd)
  5244. {
  5245. int rc = 0;
  5246. struct cam_isp_context *ctx_isp =
  5247. (struct cam_isp_context *) ctx->ctx_priv;
  5248. if (!ctx_isp->hw_acquired) {
  5249. CAM_ERR(CAM_ISP, "HW is not acquired, reject packet");
  5250. return -EINVAL;
  5251. }
  5252. rc = __cam_isp_ctx_config_dev_in_top_state(ctx, cmd);
  5253. if (!rc && ((ctx->link_hdl >= 0) || ctx_isp->offline_context)) {
  5254. ctx->state = CAM_CTX_READY;
  5255. trace_cam_context_state("ISP", ctx);
  5256. }
  5257. CAM_DBG(CAM_ISP, "next state %d", ctx->state);
  5258. return rc;
  5259. }
  5260. static int __cam_isp_ctx_config_dev_in_flushed(struct cam_context *ctx,
  5261. struct cam_config_dev_cmd *cmd)
  5262. {
  5263. int rc = 0;
  5264. struct cam_start_stop_dev_cmd start_cmd;
  5265. struct cam_hw_cmd_args hw_cmd_args;
  5266. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  5267. struct cam_isp_context *ctx_isp =
  5268. (struct cam_isp_context *) ctx->ctx_priv;
  5269. if (!ctx_isp->hw_acquired) {
  5270. CAM_ERR(CAM_ISP, "HW is not acquired, reject packet");
  5271. rc = -EINVAL;
  5272. goto end;
  5273. }
  5274. rc = __cam_isp_ctx_config_dev_in_top_state(ctx, cmd);
  5275. if (rc)
  5276. goto end;
  5277. if (!ctx_isp->init_received) {
  5278. CAM_WARN(CAM_ISP,
  5279. "Received update packet in flushed state, skip start");
  5280. goto end;
  5281. }
  5282. hw_cmd_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  5283. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  5284. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_CMD_RESUME_HW;
  5285. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  5286. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  5287. &hw_cmd_args);
  5288. if (rc) {
  5289. CAM_ERR(CAM_ISP, "Failed to resume HW rc: %d", rc);
  5290. goto end;
  5291. }
  5292. start_cmd.dev_handle = cmd->dev_handle;
  5293. start_cmd.session_handle = cmd->session_handle;
  5294. rc = __cam_isp_ctx_start_dev_in_ready(ctx, &start_cmd);
  5295. if (rc)
  5296. CAM_ERR(CAM_ISP,
  5297. "Failed to re-start HW after flush rc: %d", rc);
  5298. else
  5299. CAM_INFO(CAM_ISP,
  5300. "Received init after flush. Re-start HW complete in ctx:%d",
  5301. ctx->ctx_id);
  5302. end:
  5303. CAM_DBG(CAM_ISP, "next state %d sub_state:%d", ctx->state,
  5304. ctx_isp->substate_activated);
  5305. return rc;
  5306. }
  5307. static int __cam_isp_ctx_link_in_acquired(struct cam_context *ctx,
  5308. struct cam_req_mgr_core_dev_link_setup *link)
  5309. {
  5310. int rc = 0;
  5311. struct cam_isp_context *ctx_isp =
  5312. (struct cam_isp_context *) ctx->ctx_priv;
  5313. if (!link) {
  5314. CAM_ERR(CAM_ISP, "setup link info is null: %pK ctx: %u",
  5315. link, ctx->ctx_id);
  5316. return -EINVAL;
  5317. }
  5318. if (!link->crm_cb) {
  5319. CAM_ERR(CAM_ISP, "crm cb is null: %pK ctx: %u",
  5320. link->crm_cb, ctx->ctx_id);
  5321. return -EINVAL;
  5322. }
  5323. CAM_DBG(CAM_ISP, "Enter.........");
  5324. ctx->link_hdl = link->link_hdl;
  5325. ctx->ctx_crm_intf = link->crm_cb;
  5326. ctx_isp->subscribe_event =
  5327. CAM_TRIGGER_POINT_SOF | CAM_TRIGGER_POINT_EOF;
  5328. ctx_isp->trigger_id = link->trigger_id;
  5329. /* change state only if we had the init config */
  5330. if (ctx_isp->init_received) {
  5331. ctx->state = CAM_CTX_READY;
  5332. trace_cam_context_state("ISP", ctx);
  5333. }
  5334. CAM_DBG(CAM_ISP, "next state %d", ctx->state);
  5335. return rc;
  5336. }
  5337. static int __cam_isp_ctx_unlink_in_acquired(struct cam_context *ctx,
  5338. struct cam_req_mgr_core_dev_link_setup *unlink)
  5339. {
  5340. int rc = 0;
  5341. struct cam_isp_context *ctx_isp =
  5342. (struct cam_isp_context *) ctx->ctx_priv;
  5343. ctx->link_hdl = -1;
  5344. ctx->ctx_crm_intf = NULL;
  5345. ctx_isp->trigger_id = -1;
  5346. return rc;
  5347. }
  5348. static int __cam_isp_ctx_get_dev_info_in_acquired(struct cam_context *ctx,
  5349. struct cam_req_mgr_device_info *dev_info)
  5350. {
  5351. int rc = 0;
  5352. dev_info->dev_hdl = ctx->dev_hdl;
  5353. strlcpy(dev_info->name, CAM_ISP_DEV_NAME, sizeof(dev_info->name));
  5354. dev_info->dev_id = CAM_REQ_MGR_DEVICE_IFE;
  5355. dev_info->p_delay = 1;
  5356. dev_info->trigger = CAM_TRIGGER_POINT_SOF;
  5357. dev_info->trigger_on = true;
  5358. return rc;
  5359. }
  5360. static inline void __cam_isp_context_reset_ctx_params(
  5361. struct cam_isp_context *ctx_isp)
  5362. {
  5363. atomic_set(&ctx_isp->process_bubble, 0);
  5364. atomic_set(&ctx_isp->rxd_epoch, 0);
  5365. atomic_set(&ctx_isp->internal_recovery_set, 0);
  5366. ctx_isp->frame_id = 0;
  5367. ctx_isp->sof_timestamp_val = 0;
  5368. ctx_isp->boot_timestamp = 0;
  5369. ctx_isp->active_req_cnt = 0;
  5370. ctx_isp->reported_req_id = 0;
  5371. ctx_isp->bubble_frame_cnt = 0;
  5372. ctx_isp->recovery_req_id = 0;
  5373. }
  5374. static int __cam_isp_ctx_start_dev_in_ready(struct cam_context *ctx,
  5375. struct cam_start_stop_dev_cmd *cmd)
  5376. {
  5377. int rc = 0;
  5378. int i;
  5379. struct cam_isp_start_args start_isp;
  5380. struct cam_ctx_request *req;
  5381. struct cam_isp_ctx_req *req_isp;
  5382. struct cam_isp_context *ctx_isp =
  5383. (struct cam_isp_context *) ctx->ctx_priv;
  5384. if (cmd->session_handle != ctx->session_hdl ||
  5385. cmd->dev_handle != ctx->dev_hdl) {
  5386. rc = -EPERM;
  5387. goto end;
  5388. }
  5389. if (list_empty(&ctx->pending_req_list)) {
  5390. /* should never happen */
  5391. CAM_ERR(CAM_ISP, "Start device with empty configuration");
  5392. rc = -EFAULT;
  5393. goto end;
  5394. } else {
  5395. req = list_first_entry(&ctx->pending_req_list,
  5396. struct cam_ctx_request, list);
  5397. }
  5398. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  5399. if (!ctx_isp->hw_ctx) {
  5400. CAM_ERR(CAM_ISP, "Wrong hw context pointer.");
  5401. rc = -EFAULT;
  5402. goto end;
  5403. }
  5404. start_isp.hw_config.ctxt_to_hw_map = ctx_isp->hw_ctx;
  5405. start_isp.hw_config.request_id = req->request_id;
  5406. start_isp.hw_config.hw_update_entries = req_isp->cfg;
  5407. start_isp.hw_config.num_hw_update_entries = req_isp->num_cfg;
  5408. start_isp.hw_config.priv = &req_isp->hw_update_data;
  5409. start_isp.hw_config.init_packet = 1;
  5410. start_isp.hw_config.reapply_type = CAM_CONFIG_REAPPLY_NONE;
  5411. start_isp.hw_config.cdm_reset_before_apply = false;
  5412. start_isp.is_internal_start = false;
  5413. ctx_isp->last_applied_req_id = req->request_id;
  5414. if (ctx->state == CAM_CTX_FLUSHED)
  5415. start_isp.start_only = true;
  5416. else
  5417. start_isp.start_only = false;
  5418. __cam_isp_context_reset_ctx_params(ctx_isp);
  5419. ctx_isp->substate_activated = ctx_isp->rdi_only_context ?
  5420. CAM_ISP_CTX_ACTIVATED_APPLIED :
  5421. (req_isp->num_fence_map_out) ? CAM_ISP_CTX_ACTIVATED_EPOCH :
  5422. CAM_ISP_CTX_ACTIVATED_SOF;
  5423. atomic64_set(&ctx_isp->state_monitor_head, -1);
  5424. for (i = 0; i < CAM_ISP_CTX_EVENT_MAX; i++)
  5425. atomic64_set(&ctx_isp->event_record_head[i], -1);
  5426. /*
  5427. * In case of CSID TPG we might receive SOF and RUP IRQs
  5428. * before hw_mgr_intf->hw_start has returned. So move
  5429. * req out of pending list before hw_start and add it
  5430. * back to pending list if hw_start fails.
  5431. */
  5432. list_del_init(&req->list);
  5433. if (ctx_isp->offline_context && !req_isp->num_fence_map_out) {
  5434. list_add_tail(&req->list, &ctx->free_req_list);
  5435. atomic_set(&ctx_isp->rxd_epoch, 1);
  5436. CAM_DBG(CAM_REQ,
  5437. "Move pending req: %lld to free list(cnt: %d) offline ctx %u",
  5438. req->request_id, ctx_isp->active_req_cnt, ctx->ctx_id);
  5439. } else if (ctx_isp->rdi_only_context || !req_isp->num_fence_map_out) {
  5440. list_add_tail(&req->list, &ctx->wait_req_list);
  5441. CAM_DBG(CAM_REQ,
  5442. "Move pending req: %lld to wait list(cnt: %d) ctx %u",
  5443. req->request_id, ctx_isp->active_req_cnt, ctx->ctx_id);
  5444. } else {
  5445. list_add_tail(&req->list, &ctx->active_req_list);
  5446. ctx_isp->active_req_cnt++;
  5447. CAM_DBG(CAM_REQ,
  5448. "Move pending req: %lld to active list(cnt: %d) ctx %u offline %d",
  5449. req->request_id, ctx_isp->active_req_cnt, ctx->ctx_id,
  5450. ctx_isp->offline_context);
  5451. }
  5452. /*
  5453. * Only place to change state before calling the hw due to
  5454. * hardware tasklet has higher priority that can cause the
  5455. * irq handling comes early
  5456. */
  5457. ctx->state = CAM_CTX_ACTIVATED;
  5458. trace_cam_context_state("ISP", ctx);
  5459. rc = ctx->hw_mgr_intf->hw_start(ctx->hw_mgr_intf->hw_mgr_priv,
  5460. &start_isp);
  5461. if (rc) {
  5462. /* HW failure. user need to clean up the resource */
  5463. CAM_ERR(CAM_ISP, "Start HW failed");
  5464. ctx->state = CAM_CTX_READY;
  5465. if ((rc == -ETIMEDOUT) &&
  5466. (isp_ctx_debug.enable_cdm_cmd_buff_dump))
  5467. rc = cam_isp_ctx_dump_req(req_isp, 0, 0, NULL, false);
  5468. trace_cam_context_state("ISP", ctx);
  5469. list_del_init(&req->list);
  5470. list_add(&req->list, &ctx->pending_req_list);
  5471. goto end;
  5472. }
  5473. CAM_DBG(CAM_ISP, "start device success ctx %u", ctx->ctx_id);
  5474. end:
  5475. return rc;
  5476. }
  5477. static int __cam_isp_ctx_unlink_in_ready(struct cam_context *ctx,
  5478. struct cam_req_mgr_core_dev_link_setup *unlink)
  5479. {
  5480. int rc = 0;
  5481. ctx->link_hdl = -1;
  5482. ctx->ctx_crm_intf = NULL;
  5483. ctx->state = CAM_CTX_ACQUIRED;
  5484. trace_cam_context_state("ISP", ctx);
  5485. return rc;
  5486. }
  5487. static int __cam_isp_ctx_stop_dev_in_activated_unlock(
  5488. struct cam_context *ctx, struct cam_start_stop_dev_cmd *stop_cmd)
  5489. {
  5490. int rc = 0;
  5491. uint32_t i;
  5492. struct cam_hw_stop_args stop;
  5493. struct cam_ctx_request *req;
  5494. struct cam_isp_ctx_req *req_isp;
  5495. struct cam_isp_context *ctx_isp =
  5496. (struct cam_isp_context *) ctx->ctx_priv;
  5497. struct cam_isp_stop_args stop_isp;
  5498. /* Mask off all the incoming hardware events */
  5499. spin_lock_bh(&ctx->lock);
  5500. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_HALT;
  5501. spin_unlock_bh(&ctx->lock);
  5502. /* stop hw first */
  5503. if (ctx_isp->hw_ctx) {
  5504. stop.ctxt_to_hw_map = ctx_isp->hw_ctx;
  5505. stop_isp.hw_stop_cmd = CAM_ISP_HW_STOP_IMMEDIATELY;
  5506. stop_isp.stop_only = false;
  5507. stop_isp.is_internal_stop = false;
  5508. stop.args = (void *) &stop_isp;
  5509. ctx->hw_mgr_intf->hw_stop(ctx->hw_mgr_intf->hw_mgr_priv,
  5510. &stop);
  5511. }
  5512. CAM_DBG(CAM_ISP, "next Substate[%s]",
  5513. __cam_isp_ctx_substate_val_to_type(
  5514. ctx_isp->substate_activated));
  5515. if (ctx->ctx_crm_intf &&
  5516. ctx->ctx_crm_intf->notify_stop) {
  5517. struct cam_req_mgr_notify_stop notify;
  5518. notify.link_hdl = ctx->link_hdl;
  5519. CAM_DBG(CAM_ISP,
  5520. "Notify CRM about device stop ctx %u link 0x%x",
  5521. ctx->ctx_id, ctx->link_hdl);
  5522. ctx->ctx_crm_intf->notify_stop(&notify);
  5523. } else if (!ctx_isp->offline_context)
  5524. CAM_ERR(CAM_ISP, "cb not present");
  5525. while (!list_empty(&ctx->pending_req_list)) {
  5526. req = list_first_entry(&ctx->pending_req_list,
  5527. struct cam_ctx_request, list);
  5528. list_del_init(&req->list);
  5529. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  5530. CAM_DBG(CAM_ISP, "signal fence in pending list. fence num %d",
  5531. req_isp->num_fence_map_out);
  5532. for (i = 0; i < req_isp->num_fence_map_out; i++)
  5533. if (req_isp->fence_map_out[i].sync_id != -1) {
  5534. cam_sync_signal(
  5535. req_isp->fence_map_out[i].sync_id,
  5536. CAM_SYNC_STATE_SIGNALED_CANCEL,
  5537. CAM_SYNC_ISP_EVENT_HW_STOP);
  5538. }
  5539. list_add_tail(&req->list, &ctx->free_req_list);
  5540. }
  5541. while (!list_empty(&ctx->wait_req_list)) {
  5542. req = list_first_entry(&ctx->wait_req_list,
  5543. struct cam_ctx_request, list);
  5544. list_del_init(&req->list);
  5545. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  5546. CAM_DBG(CAM_ISP, "signal fence in wait list. fence num %d",
  5547. req_isp->num_fence_map_out);
  5548. for (i = 0; i < req_isp->num_fence_map_out; i++)
  5549. if (req_isp->fence_map_out[i].sync_id != -1) {
  5550. cam_sync_signal(
  5551. req_isp->fence_map_out[i].sync_id,
  5552. CAM_SYNC_STATE_SIGNALED_CANCEL,
  5553. CAM_SYNC_ISP_EVENT_HW_STOP);
  5554. }
  5555. list_add_tail(&req->list, &ctx->free_req_list);
  5556. }
  5557. while (!list_empty(&ctx->active_req_list)) {
  5558. req = list_first_entry(&ctx->active_req_list,
  5559. struct cam_ctx_request, list);
  5560. list_del_init(&req->list);
  5561. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  5562. CAM_DBG(CAM_ISP, "signal fence in active list. fence num %d",
  5563. req_isp->num_fence_map_out);
  5564. for (i = 0; i < req_isp->num_fence_map_out; i++)
  5565. if (req_isp->fence_map_out[i].sync_id != -1) {
  5566. cam_sync_signal(
  5567. req_isp->fence_map_out[i].sync_id,
  5568. CAM_SYNC_STATE_SIGNALED_CANCEL,
  5569. CAM_SYNC_ISP_EVENT_HW_STOP);
  5570. }
  5571. list_add_tail(&req->list, &ctx->free_req_list);
  5572. }
  5573. ctx_isp->frame_id = 0;
  5574. ctx_isp->active_req_cnt = 0;
  5575. ctx_isp->reported_req_id = 0;
  5576. ctx_isp->last_applied_req_id = 0;
  5577. ctx_isp->req_info.last_bufdone_req_id = 0;
  5578. ctx_isp->bubble_frame_cnt = 0;
  5579. atomic_set(&ctx_isp->process_bubble, 0);
  5580. atomic_set(&ctx_isp->internal_recovery_set, 0);
  5581. atomic_set(&ctx_isp->rxd_epoch, 0);
  5582. atomic64_set(&ctx_isp->state_monitor_head, -1);
  5583. for (i = 0; i < CAM_ISP_CTX_EVENT_MAX; i++)
  5584. atomic64_set(&ctx_isp->event_record_head[i], -1);
  5585. CAM_DBG(CAM_ISP, "Stop device success next state %d on ctx %u",
  5586. ctx->state, ctx->ctx_id);
  5587. if (!stop_cmd) {
  5588. rc = __cam_isp_ctx_unlink_in_ready(ctx, NULL);
  5589. if (rc)
  5590. CAM_ERR(CAM_ISP, "Unlink failed rc=%d", rc);
  5591. }
  5592. return rc;
  5593. }
  5594. static int __cam_isp_ctx_stop_dev_in_activated(struct cam_context *ctx,
  5595. struct cam_start_stop_dev_cmd *cmd)
  5596. {
  5597. int rc = 0;
  5598. struct cam_isp_context *ctx_isp =
  5599. (struct cam_isp_context *)ctx->ctx_priv;
  5600. __cam_isp_ctx_stop_dev_in_activated_unlock(ctx, cmd);
  5601. ctx_isp->init_received = false;
  5602. ctx->state = CAM_CTX_ACQUIRED;
  5603. trace_cam_context_state("ISP", ctx);
  5604. return rc;
  5605. }
  5606. static int __cam_isp_ctx_release_dev_in_activated(struct cam_context *ctx,
  5607. struct cam_release_dev_cmd *cmd)
  5608. {
  5609. int rc = 0;
  5610. rc = __cam_isp_ctx_stop_dev_in_activated_unlock(ctx, NULL);
  5611. if (rc)
  5612. CAM_ERR(CAM_ISP, "Stop device failed rc=%d", rc);
  5613. rc = __cam_isp_ctx_release_dev_in_top_state(ctx, cmd);
  5614. if (rc)
  5615. CAM_ERR(CAM_ISP, "Release device failed rc=%d", rc);
  5616. return rc;
  5617. }
  5618. static int __cam_isp_ctx_release_hw_in_activated(struct cam_context *ctx,
  5619. void *cmd)
  5620. {
  5621. int rc = 0;
  5622. rc = __cam_isp_ctx_stop_dev_in_activated_unlock(ctx, NULL);
  5623. if (rc)
  5624. CAM_ERR(CAM_ISP, "Stop device failed rc=%d", rc);
  5625. rc = __cam_isp_ctx_release_hw_in_top_state(ctx, cmd);
  5626. if (rc)
  5627. CAM_ERR(CAM_ISP, "Release hw failed rc=%d", rc);
  5628. return rc;
  5629. }
  5630. static int __cam_isp_ctx_link_pause(struct cam_context *ctx)
  5631. {
  5632. int rc = 0;
  5633. struct cam_hw_cmd_args hw_cmd_args;
  5634. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  5635. hw_cmd_args.ctxt_to_hw_map = ctx->ctxt_to_hw_map;
  5636. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  5637. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_CMD_PAUSE_HW;
  5638. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  5639. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  5640. &hw_cmd_args);
  5641. return rc;
  5642. }
  5643. static int __cam_isp_ctx_link_resume(struct cam_context *ctx)
  5644. {
  5645. int rc = 0;
  5646. struct cam_hw_cmd_args hw_cmd_args;
  5647. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  5648. hw_cmd_args.ctxt_to_hw_map = ctx->ctxt_to_hw_map;
  5649. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  5650. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_CMD_RESUME_HW;
  5651. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  5652. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  5653. &hw_cmd_args);
  5654. return rc;
  5655. }
  5656. static int __cam_isp_ctx_handle_sof_freeze_evt(
  5657. struct cam_context *ctx)
  5658. {
  5659. int rc = 0;
  5660. struct cam_hw_cmd_args hw_cmd_args;
  5661. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  5662. hw_cmd_args.ctxt_to_hw_map = ctx->ctxt_to_hw_map;
  5663. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  5664. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_CMD_SOF_DEBUG;
  5665. isp_hw_cmd_args.u.sof_irq_enable = 1;
  5666. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  5667. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  5668. &hw_cmd_args);
  5669. return rc;
  5670. }
  5671. static int __cam_isp_ctx_reset_and_recover(
  5672. bool skip_resume, struct cam_context *ctx)
  5673. {
  5674. int rc = 0;
  5675. struct cam_isp_context *ctx_isp =
  5676. (struct cam_isp_context *)ctx->ctx_priv;
  5677. struct cam_isp_stop_args stop_isp;
  5678. struct cam_hw_stop_args stop_args;
  5679. struct cam_isp_start_args start_isp;
  5680. struct cam_hw_cmd_args hw_cmd_args;
  5681. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  5682. struct cam_ctx_request *req;
  5683. struct cam_isp_ctx_req *req_isp;
  5684. spin_lock(&ctx->lock);
  5685. if (ctx_isp->active_req_cnt) {
  5686. spin_unlock(&ctx->lock);
  5687. CAM_WARN(CAM_ISP,
  5688. "Active list not empty: %u in ctx: %u on link: 0x%x, retry recovery for req: %lld after buf_done",
  5689. ctx_isp->active_req_cnt, ctx->ctx_id,
  5690. ctx->link_hdl, ctx_isp->recovery_req_id);
  5691. goto end;
  5692. }
  5693. if (ctx->state != CAM_CTX_ACTIVATED) {
  5694. spin_unlock(&ctx->lock);
  5695. CAM_ERR(CAM_ISP,
  5696. "In wrong state %d, for recovery ctx: %u in link: 0x%x recovery req: %lld",
  5697. ctx->state, ctx->ctx_id,
  5698. ctx->link_hdl, ctx_isp->recovery_req_id);
  5699. rc = -EINVAL;
  5700. goto end;
  5701. }
  5702. if (list_empty(&ctx->pending_req_list)) {
  5703. /* Cannot start with no request */
  5704. spin_unlock(&ctx->lock);
  5705. CAM_ERR(CAM_ISP,
  5706. "Failed to reset and recover last_applied_req: %llu in ctx: %u on link: 0x%x",
  5707. ctx_isp->last_applied_req_id, ctx->ctx_id, ctx->link_hdl);
  5708. rc = -EFAULT;
  5709. goto end;
  5710. }
  5711. spin_unlock(&ctx->lock);
  5712. if (!ctx_isp->hw_ctx) {
  5713. CAM_ERR(CAM_ISP,
  5714. "Invalid hw context pointer ctx: %u on link: 0x%x",
  5715. ctx->ctx_id, ctx->link_hdl);
  5716. rc = -EFAULT;
  5717. goto end;
  5718. }
  5719. req = list_first_entry(&ctx->pending_req_list,
  5720. struct cam_ctx_request, list);
  5721. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  5722. req_isp->bubble_detected = false;
  5723. CAM_INFO(CAM_ISP,
  5724. "Trigger Halt, Reset & Resume for req: %llu ctx: %u in state: %d link: 0x%x",
  5725. req->request_id, ctx->ctx_id, ctx->state, ctx->link_hdl);
  5726. stop_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  5727. stop_isp.hw_stop_cmd = CAM_ISP_HW_STOP_IMMEDIATELY;
  5728. stop_isp.stop_only = true;
  5729. stop_isp.is_internal_stop = true;
  5730. stop_args.args = (void *)&stop_isp;
  5731. rc = ctx->hw_mgr_intf->hw_stop(ctx->hw_mgr_intf->hw_mgr_priv,
  5732. &stop_args);
  5733. if (rc) {
  5734. CAM_ERR(CAM_ISP, "Failed to stop HW rc: %d ctx: %u",
  5735. rc, ctx->ctx_id);
  5736. goto end;
  5737. }
  5738. CAM_DBG(CAM_ISP, "Stop HW success ctx: %u link: 0x%x",
  5739. ctx->ctx_id, ctx->link_hdl);
  5740. /* API provides provision to stream off and not resume as well in case of fatal errors */
  5741. if (skip_resume) {
  5742. atomic_set(&ctx_isp->internal_recovery_set, 0);
  5743. CAM_INFO(CAM_ISP,
  5744. "Halting streaming off IFE/SFE ctx: %u last_applied_req: %lld [recovery_req: %lld] on link: 0x%x",
  5745. ctx->ctx_id, ctx_isp->last_applied_req_id,
  5746. ctx_isp->recovery_req_id, ctx->link_hdl);
  5747. goto end;
  5748. }
  5749. hw_cmd_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  5750. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  5751. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_CMD_RESUME_HW;
  5752. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  5753. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  5754. &hw_cmd_args);
  5755. if (rc) {
  5756. CAM_ERR(CAM_ISP, "Failed to resume HW rc: %d ctx: %u", rc, ctx->ctx_id);
  5757. goto end;
  5758. }
  5759. CAM_DBG(CAM_ISP, "Resume call success ctx: %u on link: 0x%x",
  5760. ctx->ctx_id, ctx->link_hdl);
  5761. start_isp.hw_config.ctxt_to_hw_map = ctx_isp->hw_ctx;
  5762. start_isp.hw_config.request_id = req->request_id;
  5763. start_isp.hw_config.hw_update_entries = req_isp->cfg;
  5764. start_isp.hw_config.num_hw_update_entries = req_isp->num_cfg;
  5765. start_isp.hw_config.priv = &req_isp->hw_update_data;
  5766. start_isp.hw_config.init_packet = 1;
  5767. start_isp.hw_config.reapply_type = CAM_CONFIG_REAPPLY_IQ;
  5768. start_isp.hw_config.cdm_reset_before_apply = false;
  5769. start_isp.start_only = true;
  5770. start_isp.is_internal_start = true;
  5771. __cam_isp_context_reset_internal_recovery_params(ctx_isp);
  5772. ctx_isp->substate_activated = ctx_isp->rdi_only_context ?
  5773. CAM_ISP_CTX_ACTIVATED_APPLIED : CAM_ISP_CTX_ACTIVATED_SOF;
  5774. rc = ctx->hw_mgr_intf->hw_start(ctx->hw_mgr_intf->hw_mgr_priv,
  5775. &start_isp);
  5776. if (rc) {
  5777. CAM_ERR(CAM_ISP, "Start HW failed");
  5778. ctx->state = CAM_CTX_READY;
  5779. goto end;
  5780. }
  5781. /* IQ applied for this request, on next trigger skip IQ cfg */
  5782. req_isp->reapply_type = CAM_CONFIG_REAPPLY_IO;
  5783. CAM_DBG(CAM_ISP, "Internal Start HW success ctx %u on link: 0x%x",
  5784. ctx->ctx_id, ctx->link_hdl);
  5785. end:
  5786. return rc;
  5787. }
  5788. static int __cam_isp_ctx_process_evt(struct cam_context *ctx,
  5789. struct cam_req_mgr_link_evt_data *link_evt_data)
  5790. {
  5791. int rc = 0;
  5792. switch (link_evt_data->evt_type) {
  5793. case CAM_REQ_MGR_LINK_EVT_ERR:
  5794. /* No handling */
  5795. break;
  5796. case CAM_REQ_MGR_LINK_EVT_PAUSE:
  5797. rc = __cam_isp_ctx_link_pause(ctx);
  5798. break;
  5799. case CAM_REQ_MGR_LINK_EVT_RESUME:
  5800. rc = __cam_isp_ctx_link_resume(ctx);
  5801. break;
  5802. case CAM_REQ_MGR_LINK_EVT_SOF_FREEZE:
  5803. rc = __cam_isp_ctx_handle_sof_freeze_evt(ctx);
  5804. break;
  5805. case CAM_REQ_MGR_LINK_EVT_STALLED:
  5806. if (ctx->state == CAM_CTX_ACTIVATED)
  5807. rc = __cam_isp_ctx_trigger_reg_dump(CAM_HW_MGR_CMD_REG_DUMP_ON_ERROR, ctx);
  5808. break;
  5809. default:
  5810. CAM_WARN(CAM_ISP,
  5811. "Unsupported event type: 0x%x on ctx: %u",
  5812. link_evt_data->evt_type, ctx->ctx_id);
  5813. rc = -EINVAL;
  5814. break;
  5815. }
  5816. return rc;
  5817. }
  5818. static int __cam_isp_ctx_unlink_in_activated(struct cam_context *ctx,
  5819. struct cam_req_mgr_core_dev_link_setup *unlink)
  5820. {
  5821. int rc = 0;
  5822. CAM_WARN(CAM_ISP,
  5823. "Received unlink in activated state. It's unexpected");
  5824. rc = __cam_isp_ctx_stop_dev_in_activated_unlock(ctx, NULL);
  5825. if (rc)
  5826. CAM_WARN(CAM_ISP, "Stop device failed rc=%d", rc);
  5827. rc = __cam_isp_ctx_unlink_in_ready(ctx, unlink);
  5828. if (rc)
  5829. CAM_ERR(CAM_ISP, "Unlink failed rc=%d", rc);
  5830. return rc;
  5831. }
  5832. static int __cam_isp_ctx_apply_req(struct cam_context *ctx,
  5833. struct cam_req_mgr_apply_request *apply)
  5834. {
  5835. int rc = 0;
  5836. struct cam_ctx_ops *ctx_ops = NULL;
  5837. struct cam_isp_context *ctx_isp =
  5838. (struct cam_isp_context *) ctx->ctx_priv;
  5839. trace_cam_apply_req("ISP", ctx->ctx_id, apply->request_id, apply->link_hdl);
  5840. CAM_DBG(CAM_ISP, "Enter: apply req in Substate[%s] request_id:%lld",
  5841. __cam_isp_ctx_substate_val_to_type(
  5842. ctx_isp->substate_activated), apply->request_id);
  5843. ctx_ops = &ctx_isp->substate_machine[ctx_isp->substate_activated];
  5844. if (ctx_ops->crm_ops.apply_req) {
  5845. rc = ctx_ops->crm_ops.apply_req(ctx, apply);
  5846. } else {
  5847. CAM_WARN_RATE_LIMIT(CAM_ISP,
  5848. "No handle function in activated Substate[%s]",
  5849. __cam_isp_ctx_substate_val_to_type(
  5850. ctx_isp->substate_activated));
  5851. rc = -EFAULT;
  5852. }
  5853. if (rc)
  5854. CAM_WARN_RATE_LIMIT(CAM_ISP,
  5855. "Apply failed in active Substate[%s] rc %d",
  5856. __cam_isp_ctx_substate_val_to_type(
  5857. ctx_isp->substate_activated), rc);
  5858. return rc;
  5859. }
  5860. static int __cam_isp_ctx_apply_default_settings(
  5861. struct cam_context *ctx,
  5862. struct cam_req_mgr_apply_request *apply)
  5863. {
  5864. int rc = 0;
  5865. struct cam_ctx_ops *ctx_ops = NULL;
  5866. struct cam_isp_context *ctx_isp =
  5867. (struct cam_isp_context *) ctx->ctx_priv;
  5868. if (!ctx_isp->use_default_apply)
  5869. return 0;
  5870. if (!(apply->trigger_point & ctx_isp->subscribe_event)) {
  5871. CAM_WARN(CAM_ISP,
  5872. "Trigger: %u not subscribed for: %u",
  5873. apply->trigger_point, ctx_isp->subscribe_event);
  5874. return 0;
  5875. }
  5876. /* Allow apply default settings for IFE only at SOF */
  5877. if (apply->trigger_point != CAM_TRIGGER_POINT_SOF)
  5878. return 0;
  5879. if ((ctx_isp->aeb_enabled) && (atomic_read(&ctx_isp->internal_recovery_set)))
  5880. return __cam_isp_ctx_reset_and_recover(false, ctx);
  5881. CAM_DBG(CAM_ISP,
  5882. "Enter: apply req in Substate:%d request _id:%lld ctx:%u on link:0x%x",
  5883. ctx_isp->substate_activated, apply->request_id,
  5884. ctx->ctx_id, ctx->link_hdl);
  5885. ctx_ops = &ctx_isp->substate_machine[
  5886. ctx_isp->substate_activated];
  5887. if (ctx_ops->crm_ops.notify_frame_skip) {
  5888. rc = ctx_ops->crm_ops.notify_frame_skip(ctx, apply);
  5889. } else {
  5890. CAM_WARN_RATE_LIMIT(CAM_ISP,
  5891. "No handle function in activated substate %d",
  5892. ctx_isp->substate_activated);
  5893. rc = -EFAULT;
  5894. }
  5895. if (rc)
  5896. CAM_WARN_RATE_LIMIT(CAM_ISP,
  5897. "Apply default failed in active substate %d rc %d",
  5898. ctx_isp->substate_activated, rc);
  5899. return rc;
  5900. }
  5901. static int __cam_isp_ctx_handle_irq_in_activated(void *context,
  5902. uint32_t evt_id, void *evt_data)
  5903. {
  5904. int rc = 0;
  5905. struct cam_isp_ctx_irq_ops *irq_ops = NULL;
  5906. struct cam_context *ctx = (struct cam_context *)context;
  5907. struct cam_isp_context *ctx_isp =
  5908. (struct cam_isp_context *)ctx->ctx_priv;
  5909. spin_lock(&ctx->lock);
  5910. trace_cam_isp_activated_irq(ctx, ctx_isp->substate_activated, evt_id,
  5911. __cam_isp_ctx_get_event_ts(evt_id, evt_data));
  5912. CAM_DBG(CAM_ISP, "Enter: State %d, Substate[%s], evt id %d",
  5913. ctx->state, __cam_isp_ctx_substate_val_to_type(
  5914. ctx_isp->substate_activated), evt_id);
  5915. irq_ops = &ctx_isp->substate_machine_irq[ctx_isp->substate_activated];
  5916. if (irq_ops->irq_ops[evt_id]) {
  5917. rc = irq_ops->irq_ops[evt_id](ctx_isp, evt_data);
  5918. } else {
  5919. CAM_DBG(CAM_ISP, "No handle function for Substate[%s]",
  5920. __cam_isp_ctx_substate_val_to_type(
  5921. ctx_isp->substate_activated));
  5922. if (isp_ctx_debug.enable_state_monitor_dump)
  5923. __cam_isp_ctx_dump_state_monitor_array(ctx_isp);
  5924. }
  5925. CAM_DBG(CAM_ISP, "Exit: State %d Substate[%s]",
  5926. ctx->state, __cam_isp_ctx_substate_val_to_type(
  5927. ctx_isp->substate_activated));
  5928. spin_unlock(&ctx->lock);
  5929. return rc;
  5930. }
  5931. /* top state machine */
  5932. static struct cam_ctx_ops
  5933. cam_isp_ctx_top_state_machine[CAM_CTX_STATE_MAX] = {
  5934. /* Uninit */
  5935. {
  5936. .ioctl_ops = {},
  5937. .crm_ops = {},
  5938. .irq_ops = NULL,
  5939. },
  5940. /* Available */
  5941. {
  5942. .ioctl_ops = {
  5943. .acquire_dev = __cam_isp_ctx_acquire_dev_in_available,
  5944. },
  5945. .crm_ops = {},
  5946. .irq_ops = NULL,
  5947. },
  5948. /* Acquired */
  5949. {
  5950. .ioctl_ops = {
  5951. .acquire_hw = __cam_isp_ctx_acquire_hw_in_acquired,
  5952. .release_dev = __cam_isp_ctx_release_dev_in_top_state,
  5953. .config_dev = __cam_isp_ctx_config_dev_in_acquired,
  5954. .flush_dev = __cam_isp_ctx_flush_dev_in_top_state,
  5955. .release_hw = __cam_isp_ctx_release_hw_in_top_state,
  5956. },
  5957. .crm_ops = {
  5958. .link = __cam_isp_ctx_link_in_acquired,
  5959. .unlink = __cam_isp_ctx_unlink_in_acquired,
  5960. .get_dev_info = __cam_isp_ctx_get_dev_info_in_acquired,
  5961. .flush_req = __cam_isp_ctx_flush_req_in_top_state,
  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. /* Ready */
  5969. {
  5970. .ioctl_ops = {
  5971. .start_dev = __cam_isp_ctx_start_dev_in_ready,
  5972. .release_dev = __cam_isp_ctx_release_dev_in_top_state,
  5973. .config_dev = __cam_isp_ctx_config_dev_in_top_state,
  5974. .flush_dev = __cam_isp_ctx_flush_dev_in_top_state,
  5975. .release_hw = __cam_isp_ctx_release_hw_in_top_state,
  5976. },
  5977. .crm_ops = {
  5978. .unlink = __cam_isp_ctx_unlink_in_ready,
  5979. .flush_req = __cam_isp_ctx_flush_req_in_ready,
  5980. .dump_req = __cam_isp_ctx_dump_in_top_state,
  5981. },
  5982. .irq_ops = NULL,
  5983. .pagefault_ops = cam_isp_context_dump_requests,
  5984. .dumpinfo_ops = cam_isp_context_info_dump,
  5985. },
  5986. /* Flushed */
  5987. {
  5988. .ioctl_ops = {
  5989. .stop_dev = __cam_isp_ctx_stop_dev_in_activated,
  5990. .release_dev = __cam_isp_ctx_release_dev_in_activated,
  5991. .config_dev = __cam_isp_ctx_config_dev_in_flushed,
  5992. .release_hw = __cam_isp_ctx_release_hw_in_activated,
  5993. },
  5994. .crm_ops = {
  5995. .unlink = __cam_isp_ctx_unlink_in_ready,
  5996. .process_evt = __cam_isp_ctx_process_evt,
  5997. .flush_req = __cam_isp_ctx_flush_req_in_flushed_state,
  5998. },
  5999. .irq_ops = NULL,
  6000. .pagefault_ops = cam_isp_context_dump_requests,
  6001. .dumpinfo_ops = cam_isp_context_info_dump,
  6002. },
  6003. /* Activated */
  6004. {
  6005. .ioctl_ops = {
  6006. .stop_dev = __cam_isp_ctx_stop_dev_in_activated,
  6007. .release_dev = __cam_isp_ctx_release_dev_in_activated,
  6008. .config_dev = __cam_isp_ctx_config_dev_in_top_state,
  6009. .flush_dev = __cam_isp_ctx_flush_dev_in_top_state,
  6010. .release_hw = __cam_isp_ctx_release_hw_in_activated,
  6011. },
  6012. .crm_ops = {
  6013. .unlink = __cam_isp_ctx_unlink_in_activated,
  6014. .apply_req = __cam_isp_ctx_apply_req,
  6015. .notify_frame_skip =
  6016. __cam_isp_ctx_apply_default_settings,
  6017. .flush_req = __cam_isp_ctx_flush_req_in_top_state,
  6018. .process_evt = __cam_isp_ctx_process_evt,
  6019. .dump_req = __cam_isp_ctx_dump_in_top_state,
  6020. },
  6021. .irq_ops = __cam_isp_ctx_handle_irq_in_activated,
  6022. .pagefault_ops = cam_isp_context_dump_requests,
  6023. .dumpinfo_ops = cam_isp_context_info_dump,
  6024. .recovery_ops = cam_isp_context_hw_recovery,
  6025. },
  6026. };
  6027. static int cam_isp_context_hw_recovery(void *priv, void *data)
  6028. {
  6029. struct cam_context *ctx = priv;
  6030. int rc = -EPERM;
  6031. if (ctx->hw_mgr_intf->hw_recovery)
  6032. rc = ctx->hw_mgr_intf->hw_recovery(ctx->hw_mgr_intf->hw_mgr_priv, data);
  6033. else
  6034. CAM_ERR(CAM_ISP, "hw mgr doesn't support recovery");
  6035. return rc;
  6036. }
  6037. static int cam_isp_context_dump_requests(void *data,
  6038. struct cam_smmu_pf_info *pf_info)
  6039. {
  6040. struct cam_context *ctx = (struct cam_context *)data;
  6041. struct cam_ctx_request *req = NULL;
  6042. struct cam_ctx_request *req_temp = NULL;
  6043. struct cam_isp_ctx_req *req_isp = NULL;
  6044. struct cam_isp_prepare_hw_update_data *hw_update_data = NULL;
  6045. struct cam_hw_mgr_dump_pf_data *pf_dbg_entry = NULL;
  6046. struct cam_req_mgr_message req_msg = {0};
  6047. struct cam_isp_context *ctx_isp;
  6048. uint32_t resource_type = 0;
  6049. bool mem_found = false, ctx_found = false, send_error = false;
  6050. int rc = 0;
  6051. struct cam_isp_context *isp_ctx =
  6052. (struct cam_isp_context *)ctx->ctx_priv;
  6053. if (!isp_ctx) {
  6054. CAM_ERR(CAM_ISP, "Invalid isp ctx");
  6055. return -EINVAL;
  6056. }
  6057. CAM_INFO(CAM_ISP, "iommu fault handler for isp ctx %d state %d",
  6058. ctx->ctx_id, ctx->state);
  6059. list_for_each_entry_safe(req, req_temp,
  6060. &ctx->active_req_list, list) {
  6061. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  6062. hw_update_data = &req_isp->hw_update_data;
  6063. pf_dbg_entry = &(req->pf_data);
  6064. CAM_INFO(CAM_ISP, "Active List: req_id : %lld ",
  6065. req->request_id);
  6066. rc = cam_context_dump_pf_info_to_hw(ctx, pf_dbg_entry,
  6067. &mem_found, &ctx_found, &resource_type, pf_info);
  6068. if (rc)
  6069. CAM_ERR(CAM_ISP, "Failed to dump pf info");
  6070. if (ctx_found)
  6071. send_error = true;
  6072. }
  6073. CAM_INFO(CAM_ISP, "Iterating over wait_list of isp ctx %d state %d",
  6074. ctx->ctx_id, ctx->state);
  6075. list_for_each_entry_safe(req, req_temp,
  6076. &ctx->wait_req_list, list) {
  6077. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  6078. hw_update_data = &req_isp->hw_update_data;
  6079. pf_dbg_entry = &(req->pf_data);
  6080. CAM_INFO(CAM_ISP, "Wait List: req_id : %lld ", req->request_id);
  6081. rc = cam_context_dump_pf_info_to_hw(ctx, pf_dbg_entry,
  6082. &mem_found, &ctx_found, &resource_type, pf_info);
  6083. if (rc)
  6084. CAM_ERR(CAM_ISP, "Failed to dump pf info");
  6085. if (ctx_found)
  6086. send_error = true;
  6087. }
  6088. /*
  6089. * In certain scenarios we observe both overflow and SMMU pagefault
  6090. * for a particular request. If overflow is handled before page fault
  6091. * we need to traverse through pending request list because if
  6092. * bubble recovery is enabled on any request we move that request
  6093. * and all the subsequent requests to the pending list while handling
  6094. * overflow error.
  6095. */
  6096. CAM_INFO(CAM_ISP,
  6097. "Iterating over pending req list of isp ctx %d state %d",
  6098. ctx->ctx_id, ctx->state);
  6099. list_for_each_entry_safe(req, req_temp,
  6100. &ctx->pending_req_list, list) {
  6101. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  6102. hw_update_data = &req_isp->hw_update_data;
  6103. pf_dbg_entry = &(req->pf_data);
  6104. CAM_INFO(CAM_ISP, "Pending List: req_id : %lld ",
  6105. req->request_id);
  6106. rc = cam_context_dump_pf_info_to_hw(ctx, pf_dbg_entry,
  6107. &mem_found, &ctx_found, &resource_type, pf_info);
  6108. if (rc)
  6109. CAM_ERR(CAM_ISP, "Failed to dump pf info");
  6110. if (ctx_found)
  6111. send_error = true;
  6112. }
  6113. if (resource_type) {
  6114. ctx_isp = (struct cam_isp_context *) ctx->ctx_priv;
  6115. CAM_ERR(CAM_ISP,
  6116. "Page fault on resource:%s (0x%x) ctx id:%d frame id:%d reported id:%lld applied id:%lld",
  6117. __cam_isp_resource_handle_id_to_type(
  6118. ctx_isp->isp_device_type, resource_type),
  6119. resource_type, ctx->ctx_id, ctx_isp->frame_id,
  6120. ctx_isp->reported_req_id, ctx_isp->last_applied_req_id);
  6121. }
  6122. if (send_error) {
  6123. CAM_INFO(CAM_ISP,
  6124. "page fault notifying to umd ctx %u session_hdl:%d device_hdl:%d link_hdl:%d",
  6125. ctx->ctx_id, ctx->session_hdl,
  6126. ctx->dev_hdl, ctx->link_hdl);
  6127. req_msg.session_hdl = ctx->session_hdl;
  6128. req_msg.u.err_msg.device_hdl = ctx->dev_hdl;
  6129. req_msg.u.err_msg.error_type =
  6130. CAM_REQ_MGR_ERROR_TYPE_PAGE_FAULT;
  6131. req_msg.u.err_msg.link_hdl = ctx->link_hdl;
  6132. req_msg.u.err_msg.request_id = 0;
  6133. req_msg.u.err_msg.resource_size = 0x0;
  6134. req_msg.u.err_msg.error_code = CAM_REQ_MGR_ISP_UNREPORTED_ERROR;
  6135. if (cam_req_mgr_notify_message(&req_msg,
  6136. V4L_EVENT_CAM_REQ_MGR_ERROR,
  6137. V4L_EVENT_CAM_REQ_MGR_EVENT))
  6138. CAM_ERR(CAM_ISP,
  6139. "could not send page fault notification ctx %u session_hdl:%d device_hdl:%d link_hdl:%d",
  6140. ctx->ctx_id, ctx->session_hdl,
  6141. ctx->dev_hdl, ctx->link_hdl);
  6142. }
  6143. return rc;
  6144. }
  6145. static int cam_isp_context_debug_register(void)
  6146. {
  6147. int rc = 0;
  6148. struct dentry *dbgfileptr = NULL;
  6149. dbgfileptr = debugfs_create_dir("camera_isp_ctx", NULL);
  6150. if (!dbgfileptr) {
  6151. CAM_ERR(CAM_ISP, "DebugFS could not create directory!");
  6152. rc = -ENOENT;
  6153. goto end;
  6154. }
  6155. /* Store parent inode for cleanup in caller */
  6156. isp_ctx_debug.dentry = dbgfileptr;
  6157. debugfs_create_u32("enable_state_monitor_dump", 0644,
  6158. isp_ctx_debug.dentry, &isp_ctx_debug.enable_state_monitor_dump);
  6159. debugfs_create_u8("enable_cdm_cmd_buffer_dump", 0644,
  6160. isp_ctx_debug.dentry, &isp_ctx_debug.enable_cdm_cmd_buff_dump);
  6161. debugfs_create_bool("disable_internal_recovery", 0644,
  6162. isp_ctx_debug.dentry, &isp_ctx_debug.disable_internal_recovery);
  6163. if (IS_ERR(dbgfileptr)) {
  6164. if (PTR_ERR(dbgfileptr) == -ENODEV)
  6165. CAM_WARN(CAM_ISP, "DebugFS not enabled in kernel!");
  6166. else
  6167. rc = PTR_ERR(dbgfileptr);
  6168. }
  6169. end:
  6170. return rc;
  6171. }
  6172. int cam_isp_context_init(struct cam_isp_context *ctx,
  6173. struct cam_context *ctx_base,
  6174. struct cam_req_mgr_kmd_ops *crm_node_intf,
  6175. struct cam_hw_mgr_intf *hw_intf,
  6176. uint32_t ctx_id,
  6177. uint32_t isp_device_type,
  6178. int img_iommu_hdl)
  6179. {
  6180. int rc = -1;
  6181. int i;
  6182. if (!ctx || !ctx_base) {
  6183. CAM_ERR(CAM_ISP, "Invalid Context");
  6184. goto err;
  6185. }
  6186. /* ISP context setup */
  6187. memset(ctx, 0, sizeof(*ctx));
  6188. ctx->base = ctx_base;
  6189. ctx->frame_id = 0;
  6190. ctx->custom_enabled = false;
  6191. ctx->use_frame_header_ts = false;
  6192. ctx->use_default_apply = false;
  6193. ctx->active_req_cnt = 0;
  6194. ctx->reported_req_id = 0;
  6195. ctx->bubble_frame_cnt = 0;
  6196. ctx->req_info.last_bufdone_req_id = 0;
  6197. ctx->v4l2_event_sub_ids = 0;
  6198. ctx->hw_ctx = NULL;
  6199. ctx->substate_activated = CAM_ISP_CTX_ACTIVATED_SOF;
  6200. ctx->substate_machine = cam_isp_ctx_activated_state_machine;
  6201. ctx->substate_machine_irq = cam_isp_ctx_activated_state_machine_irq;
  6202. ctx->init_timestamp = jiffies_to_msecs(jiffies);
  6203. ctx->isp_device_type = isp_device_type;
  6204. for (i = 0; i < CAM_ISP_CTX_REQ_MAX; i++) {
  6205. ctx->req_base[i].req_priv = &ctx->req_isp[i];
  6206. ctx->req_isp[i].base = &ctx->req_base[i];
  6207. }
  6208. /* camera context setup */
  6209. rc = cam_context_init(ctx_base, isp_dev_name, CAM_ISP, ctx_id,
  6210. crm_node_intf, hw_intf, ctx->req_base, CAM_ISP_CTX_REQ_MAX, img_iommu_hdl);
  6211. if (rc) {
  6212. CAM_ERR(CAM_ISP, "Camera Context Base init failed");
  6213. goto err;
  6214. }
  6215. /* link camera context with isp context */
  6216. ctx_base->state_machine = cam_isp_ctx_top_state_machine;
  6217. ctx_base->ctx_priv = ctx;
  6218. /* initializing current state for error logging */
  6219. for (i = 0; i < CAM_ISP_CTX_STATE_MONITOR_MAX_ENTRIES; i++) {
  6220. ctx->cam_isp_ctx_state_monitor[i].curr_state =
  6221. CAM_ISP_CTX_ACTIVATED_MAX;
  6222. }
  6223. atomic64_set(&ctx->state_monitor_head, -1);
  6224. for (i = 0; i < CAM_ISP_CTX_EVENT_MAX; i++)
  6225. atomic64_set(&ctx->event_record_head[i], -1);
  6226. if (!isp_ctx_debug.dentry)
  6227. cam_isp_context_debug_register();
  6228. err:
  6229. return rc;
  6230. }
  6231. int cam_isp_context_deinit(struct cam_isp_context *ctx)
  6232. {
  6233. if (ctx->base)
  6234. cam_context_deinit(ctx->base);
  6235. if (ctx->substate_activated != CAM_ISP_CTX_ACTIVATED_SOF)
  6236. CAM_ERR(CAM_ISP, "ISP context Substate[%s] is invalid",
  6237. __cam_isp_ctx_substate_val_to_type(
  6238. ctx->substate_activated));
  6239. debugfs_remove_recursive(isp_ctx_debug.dentry);
  6240. isp_ctx_debug.dentry = NULL;
  6241. memset(ctx, 0, sizeof(*ctx));
  6242. return 0;
  6243. }