cam_isp_context.c 176 KB

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