cam_isp_context.c 169 KB

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