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