cam_isp_context.c 170 KB

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