cam_isp_context.c 202 KB

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