cam_isp_context.c 175 KB

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