cam_isp_context.c 191 KB

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