cam_isp_context.c 212 KB

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