cam_isp_context.c 191 KB

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