cam_isp_context.c 192 KB

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