cam_isp_context.c 176 KB

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