cam_isp_context.c 227 KB

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