cam_isp_context.c 214 KB

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