cam_isp_context.c 214 KB

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