cam_isp_context.c 174 KB

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