cam_isp_context.c 172 KB

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