cam_isp_context.c 214 KB

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