cam_isp_context.c 191 KB

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