cam_isp_context.c 179 KB

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