cam_isp_context.c 191 KB

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