cam_isp_context.c 170 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2017-2020, 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_BE:
  531. return "STATS_BE";
  532. case CAM_ISP_IFE_LITE_OUT_RES_GAMMA:
  533. return "GAMMA";
  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_dump_req_info(
  2897. struct cam_context *ctx,
  2898. struct cam_ctx_request *req,
  2899. uintptr_t cpu_addr,
  2900. size_t buf_len,
  2901. size_t *offset)
  2902. {
  2903. int i, rc;
  2904. uint8_t *dst;
  2905. int32_t *addr, *start;
  2906. uint32_t min_len;
  2907. size_t remain_len;
  2908. struct cam_isp_ctx_req *req_isp;
  2909. struct cam_isp_context *ctx_isp;
  2910. struct cam_isp_context_dump_header *hdr;
  2911. if (!req || !ctx || !offset || !cpu_addr || !buf_len) {
  2912. CAM_ERR(CAM_ISP, "Invalid parameters %pK %pK %pK %zu",
  2913. req, ctx, offset, buf_len);
  2914. return -EINVAL;
  2915. }
  2916. req_isp = (struct cam_isp_ctx_req *)req->req_priv;
  2917. ctx_isp = (struct cam_isp_context *)ctx->ctx_priv;
  2918. if (buf_len <= *offset) {
  2919. CAM_WARN(CAM_ISP, "Dump buffer overshoot len %zu offset %zu",
  2920. buf_len, *offset);
  2921. return -ENOSPC;
  2922. }
  2923. remain_len = buf_len - *offset;
  2924. min_len = sizeof(struct cam_isp_context_dump_header) +
  2925. (CAM_ISP_CTX_DUMP_REQUEST_NUM_WORDS *
  2926. req_isp->num_fence_map_out *
  2927. sizeof(int32_t));
  2928. if (remain_len < min_len) {
  2929. CAM_WARN(CAM_ISP, "Dump buffer exhaust remain %zu min %u",
  2930. remain_len, min_len);
  2931. return -ENOSPC;
  2932. }
  2933. dst = (uint8_t *)cpu_addr + *offset;
  2934. hdr = (struct cam_isp_context_dump_header *)dst;
  2935. hdr->word_size = sizeof(int32_t);
  2936. scnprintf(hdr->tag, CAM_ISP_CONTEXT_DUMP_TAG_MAX_LEN,
  2937. "ISP_OUT_FENCE:");
  2938. addr = (int32_t *)(dst + sizeof(struct cam_isp_context_dump_header));
  2939. start = addr;
  2940. for (i = 0; i < req_isp->num_fence_map_out; i++) {
  2941. *addr++ = req_isp->fence_map_out[i].resource_handle;
  2942. *addr++ = req_isp->fence_map_out[i].sync_id;
  2943. }
  2944. hdr->size = hdr->word_size * (addr - start);
  2945. *offset += hdr->size + sizeof(struct cam_isp_context_dump_header);
  2946. rc = cam_isp_ctx_dump_req(req_isp, cpu_addr, buf_len,
  2947. offset, true);
  2948. return rc;
  2949. }
  2950. static int __cam_isp_ctx_dump_in_top_state(
  2951. struct cam_context *ctx,
  2952. struct cam_req_mgr_dump_info *dump_info)
  2953. {
  2954. int rc = 0;
  2955. bool dump_only_event_record = false;
  2956. size_t buf_len;
  2957. size_t remain_len;
  2958. uint8_t *dst;
  2959. ktime_t cur_time;
  2960. uint32_t min_len;
  2961. uint64_t diff;
  2962. uint64_t *addr, *start;
  2963. uintptr_t cpu_addr;
  2964. struct timespec64 ts;
  2965. struct cam_isp_context *ctx_isp;
  2966. struct cam_ctx_request *req = NULL;
  2967. struct cam_isp_ctx_req *req_isp;
  2968. struct cam_ctx_request *req_temp;
  2969. struct cam_hw_dump_args dump_args;
  2970. struct cam_isp_context_dump_header *hdr;
  2971. spin_lock_bh(&ctx->lock);
  2972. list_for_each_entry_safe(req, req_temp,
  2973. &ctx->active_req_list, list) {
  2974. if (req->request_id == dump_info->req_id) {
  2975. CAM_INFO(CAM_ISP, "isp dump active list req: %lld",
  2976. dump_info->req_id);
  2977. goto hw_dump;
  2978. }
  2979. }
  2980. list_for_each_entry_safe(req, req_temp,
  2981. &ctx->wait_req_list, list) {
  2982. if (req->request_id == dump_info->req_id) {
  2983. CAM_INFO(CAM_ISP, "isp dump wait list req: %lld",
  2984. dump_info->req_id);
  2985. goto hw_dump;
  2986. }
  2987. }
  2988. spin_unlock_bh(&ctx->lock);
  2989. return rc;
  2990. hw_dump:
  2991. rc = cam_mem_get_cpu_buf(dump_info->buf_handle,
  2992. &cpu_addr, &buf_len);
  2993. if (rc) {
  2994. CAM_ERR(CAM_ISP, "Invalid handle %u rc %d",
  2995. dump_info->buf_handle, rc);
  2996. spin_unlock_bh(&ctx->lock);
  2997. return rc;
  2998. }
  2999. if (buf_len <= dump_info->offset) {
  3000. spin_unlock_bh(&ctx->lock);
  3001. CAM_WARN(CAM_ISP, "Dump buffer overshoot len %zu offset %zu",
  3002. buf_len, dump_info->offset);
  3003. return -ENOSPC;
  3004. }
  3005. remain_len = buf_len - dump_info->offset;
  3006. min_len = sizeof(struct cam_isp_context_dump_header) +
  3007. (CAM_ISP_CTX_DUMP_NUM_WORDS * sizeof(uint64_t));
  3008. if (remain_len < min_len) {
  3009. spin_unlock_bh(&ctx->lock);
  3010. CAM_WARN(CAM_ISP, "Dump buffer exhaust remain %zu min %u",
  3011. remain_len, min_len);
  3012. return -ENOSPC;
  3013. }
  3014. ctx_isp = (struct cam_isp_context *) ctx->ctx_priv;
  3015. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  3016. cur_time = ktime_get();
  3017. diff = ktime_us_delta(
  3018. req_isp->event_timestamp[CAM_ISP_CTX_EVENT_APPLY],
  3019. cur_time);
  3020. if (diff < CAM_ISP_CTX_RESPONSE_TIME_THRESHOLD) {
  3021. CAM_INFO(CAM_ISP, "req %lld found no error",
  3022. req->request_id);
  3023. dump_only_event_record = true;
  3024. }
  3025. dst = (uint8_t *)cpu_addr + dump_info->offset;
  3026. hdr = (struct cam_isp_context_dump_header *)dst;
  3027. scnprintf(hdr->tag, CAM_ISP_CONTEXT_DUMP_TAG_MAX_LEN,
  3028. "ISP_CTX_DUMP:");
  3029. hdr->word_size = sizeof(uint64_t);
  3030. addr = (uint64_t *)(dst +
  3031. sizeof(struct cam_isp_context_dump_header));
  3032. start = addr;
  3033. *addr++ = req->request_id;
  3034. ts = ktime_to_timespec64(
  3035. req_isp->event_timestamp[CAM_ISP_CTX_EVENT_APPLY]);
  3036. *addr++ = ts.tv_sec;
  3037. *addr++ = ts.tv_nsec/NSEC_PER_USEC;
  3038. ts = ktime_to_timespec64(cur_time);
  3039. *addr++ = ts.tv_sec;
  3040. *addr++ = ts.tv_nsec/NSEC_PER_USEC;
  3041. hdr->size = hdr->word_size * (addr - start);
  3042. dump_info->offset += hdr->size +
  3043. sizeof(struct cam_isp_context_dump_header);
  3044. rc = __cam_isp_ctx_dump_event_record(ctx_isp, cpu_addr,
  3045. buf_len, &dump_info->offset);
  3046. if (rc) {
  3047. CAM_ERR(CAM_ISP, "Dump event fail %lld",
  3048. req->request_id);
  3049. spin_unlock_bh(&ctx->lock);
  3050. return rc;
  3051. }
  3052. if (dump_only_event_record) {
  3053. spin_unlock_bh(&ctx->lock);
  3054. return rc;
  3055. }
  3056. rc = __cam_isp_ctx_dump_req_info(ctx, req, cpu_addr,
  3057. buf_len, &dump_info->offset);
  3058. if (rc) {
  3059. CAM_ERR(CAM_ISP, "Dump Req info fail %lld",
  3060. req->request_id);
  3061. spin_unlock_bh(&ctx->lock);
  3062. return rc;
  3063. }
  3064. spin_unlock_bh(&ctx->lock);
  3065. if (ctx->hw_mgr_intf->hw_dump) {
  3066. dump_args.offset = dump_info->offset;
  3067. dump_args.request_id = dump_info->req_id;
  3068. dump_args.buf_handle = dump_info->buf_handle;
  3069. dump_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  3070. rc = ctx->hw_mgr_intf->hw_dump(
  3071. ctx->hw_mgr_intf->hw_mgr_priv,
  3072. &dump_args);
  3073. dump_info->offset = dump_args.offset;
  3074. }
  3075. return rc;
  3076. }
  3077. static int __cam_isp_ctx_flush_req(struct cam_context *ctx,
  3078. struct list_head *req_list, struct cam_req_mgr_flush_request *flush_req)
  3079. {
  3080. int i, rc, tmp = 0;
  3081. uint32_t cancel_req_id_found = 0;
  3082. struct cam_ctx_request *req;
  3083. struct cam_ctx_request *req_temp;
  3084. struct cam_isp_ctx_req *req_isp;
  3085. struct list_head flush_list;
  3086. struct cam_isp_context *ctx_isp = NULL;
  3087. ctx_isp = (struct cam_isp_context *) ctx->ctx_priv;
  3088. INIT_LIST_HEAD(&flush_list);
  3089. if (list_empty(req_list)) {
  3090. CAM_DBG(CAM_ISP, "request list is empty");
  3091. if (flush_req->type == CAM_REQ_MGR_FLUSH_TYPE_CANCEL_REQ) {
  3092. CAM_ERR(CAM_ISP, "no request to cancel");
  3093. return -EINVAL;
  3094. } else
  3095. return 0;
  3096. }
  3097. CAM_DBG(CAM_REQ, "Flush [%u] in progress for req_id %llu",
  3098. flush_req->type, flush_req->req_id);
  3099. list_for_each_entry_safe(req, req_temp, req_list, list) {
  3100. if (flush_req->type == CAM_REQ_MGR_FLUSH_TYPE_CANCEL_REQ) {
  3101. if (req->request_id != flush_req->req_id) {
  3102. continue;
  3103. } else {
  3104. list_del_init(&req->list);
  3105. list_add_tail(&req->list, &flush_list);
  3106. cancel_req_id_found = 1;
  3107. __cam_isp_ctx_update_state_monitor_array(
  3108. ctx_isp,
  3109. CAM_ISP_STATE_CHANGE_TRIGGER_FLUSH,
  3110. req->request_id);
  3111. break;
  3112. }
  3113. }
  3114. list_del_init(&req->list);
  3115. list_add_tail(&req->list, &flush_list);
  3116. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  3117. CAM_ISP_STATE_CHANGE_TRIGGER_FLUSH, req->request_id);
  3118. }
  3119. list_for_each_entry_safe(req, req_temp, &flush_list, list) {
  3120. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  3121. for (i = 0; i < req_isp->num_fence_map_out; i++) {
  3122. if (req_isp->fence_map_out[i].sync_id != -1) {
  3123. CAM_DBG(CAM_ISP, "Flush req 0x%llx, fence %d",
  3124. req->request_id,
  3125. req_isp->fence_map_out[i].sync_id);
  3126. rc = cam_sync_signal(
  3127. req_isp->fence_map_out[i].sync_id,
  3128. CAM_SYNC_STATE_SIGNALED_CANCEL,
  3129. CAM_SYNC_ISP_EVENT_FLUSH);
  3130. if (rc) {
  3131. tmp = req_isp->fence_map_out[i].sync_id;
  3132. CAM_ERR_RATE_LIMIT(CAM_ISP,
  3133. "signal fence %d failed", tmp);
  3134. }
  3135. req_isp->fence_map_out[i].sync_id = -1;
  3136. }
  3137. }
  3138. req_isp->reapply = false;
  3139. req_isp->cdm_reset_before_apply = false;
  3140. list_del_init(&req->list);
  3141. list_add_tail(&req->list, &ctx->free_req_list);
  3142. }
  3143. if (flush_req->type == CAM_REQ_MGR_FLUSH_TYPE_CANCEL_REQ &&
  3144. !cancel_req_id_found)
  3145. CAM_DBG(CAM_ISP,
  3146. "Flush request id:%lld is not found in the list",
  3147. flush_req->req_id);
  3148. return 0;
  3149. }
  3150. static int __cam_isp_ctx_flush_req_in_top_state(
  3151. struct cam_context *ctx,
  3152. struct cam_req_mgr_flush_request *flush_req)
  3153. {
  3154. int rc = 0;
  3155. struct cam_isp_context *ctx_isp;
  3156. struct cam_isp_stop_args stop_isp;
  3157. struct cam_hw_stop_args stop_args;
  3158. struct cam_hw_reset_args reset_args;
  3159. struct cam_hw_cmd_args hw_cmd_args;
  3160. struct cam_req_mgr_timer_notify timer;
  3161. ctx_isp = (struct cam_isp_context *) ctx->ctx_priv;
  3162. CAM_DBG(CAM_ISP, "Flush pending list");
  3163. spin_lock_bh(&ctx->lock);
  3164. rc = __cam_isp_ctx_flush_req(ctx, &ctx->pending_req_list, flush_req);
  3165. spin_unlock_bh(&ctx->lock);
  3166. if (flush_req->type == CAM_REQ_MGR_FLUSH_TYPE_ALL) {
  3167. if (ctx->state <= CAM_CTX_READY) {
  3168. ctx->state = CAM_CTX_ACQUIRED;
  3169. goto end;
  3170. }
  3171. spin_lock_bh(&ctx->lock);
  3172. ctx->state = CAM_CTX_FLUSHED;
  3173. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_HALT;
  3174. spin_unlock_bh(&ctx->lock);
  3175. CAM_INFO(CAM_ISP, "Last request id to flush is %lld, ctx_id:%d",
  3176. flush_req->req_id, ctx->ctx_id);
  3177. ctx->last_flush_req = flush_req->req_id;
  3178. memset(&hw_cmd_args, 0, sizeof(hw_cmd_args));
  3179. hw_cmd_args.ctxt_to_hw_map = ctx->ctxt_to_hw_map;
  3180. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_REG_DUMP_ON_FLUSH;
  3181. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  3182. &hw_cmd_args);
  3183. if (rc) {
  3184. CAM_ERR(CAM_ISP, "Reg dump on flush failed rc: %d", rc);
  3185. rc = 0;
  3186. }
  3187. stop_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  3188. stop_isp.hw_stop_cmd = CAM_ISP_HW_STOP_IMMEDIATELY;
  3189. stop_isp.stop_only = true;
  3190. stop_args.args = (void *)&stop_isp;
  3191. rc = ctx->hw_mgr_intf->hw_stop(ctx->hw_mgr_intf->hw_mgr_priv,
  3192. &stop_args);
  3193. if (rc)
  3194. CAM_ERR(CAM_ISP, "Failed to stop HW in Flush rc: %d",
  3195. rc);
  3196. CAM_INFO(CAM_ISP, "Stop HW complete. Reset HW next.");
  3197. CAM_DBG(CAM_ISP, "Flush wait and active lists");
  3198. if (ctx->ctx_crm_intf && ctx->ctx_crm_intf->notify_timer) {
  3199. timer.link_hdl = ctx->link_hdl;
  3200. timer.dev_hdl = ctx->dev_hdl;
  3201. timer.state = false;
  3202. ctx->ctx_crm_intf->notify_timer(&timer);
  3203. }
  3204. spin_lock_bh(&ctx->lock);
  3205. if (!list_empty(&ctx->wait_req_list))
  3206. rc = __cam_isp_ctx_flush_req(ctx, &ctx->wait_req_list,
  3207. flush_req);
  3208. if (!list_empty(&ctx->active_req_list))
  3209. rc = __cam_isp_ctx_flush_req(ctx, &ctx->active_req_list,
  3210. flush_req);
  3211. ctx_isp->active_req_cnt = 0;
  3212. spin_unlock_bh(&ctx->lock);
  3213. reset_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  3214. rc = ctx->hw_mgr_intf->hw_reset(ctx->hw_mgr_intf->hw_mgr_priv,
  3215. &reset_args);
  3216. if (rc)
  3217. CAM_ERR(CAM_ISP, "Failed to reset HW rc: %d", rc);
  3218. ctx_isp->init_received = false;
  3219. }
  3220. end:
  3221. ctx_isp->bubble_frame_cnt = 0;
  3222. atomic_set(&ctx_isp->process_bubble, 0);
  3223. atomic_set(&ctx_isp->rxd_epoch, 0);
  3224. return rc;
  3225. }
  3226. static int __cam_isp_ctx_flush_req_in_ready(
  3227. struct cam_context *ctx,
  3228. struct cam_req_mgr_flush_request *flush_req)
  3229. {
  3230. int rc = 0;
  3231. CAM_DBG(CAM_ISP, "try to flush pending list");
  3232. spin_lock_bh(&ctx->lock);
  3233. rc = __cam_isp_ctx_flush_req(ctx, &ctx->pending_req_list, flush_req);
  3234. /* if nothing is in pending req list, change state to acquire */
  3235. if (list_empty(&ctx->pending_req_list))
  3236. ctx->state = CAM_CTX_ACQUIRED;
  3237. spin_unlock_bh(&ctx->lock);
  3238. trace_cam_context_state("ISP", ctx);
  3239. CAM_DBG(CAM_ISP, "Flush request in ready state. next state %d",
  3240. ctx->state);
  3241. return rc;
  3242. }
  3243. static struct cam_ctx_ops
  3244. cam_isp_ctx_activated_state_machine[CAM_ISP_CTX_ACTIVATED_MAX] = {
  3245. /* SOF */
  3246. {
  3247. .ioctl_ops = {},
  3248. .crm_ops = {
  3249. .apply_req = __cam_isp_ctx_apply_req_in_sof,
  3250. },
  3251. .irq_ops = NULL,
  3252. },
  3253. /* APPLIED */
  3254. {
  3255. .ioctl_ops = {},
  3256. .crm_ops = {},
  3257. .irq_ops = NULL,
  3258. },
  3259. /* EPOCH */
  3260. {
  3261. .ioctl_ops = {},
  3262. .crm_ops = {
  3263. .apply_req = __cam_isp_ctx_apply_req_in_epoch,
  3264. },
  3265. .irq_ops = NULL,
  3266. },
  3267. /* BUBBLE */
  3268. {
  3269. .ioctl_ops = {},
  3270. .crm_ops = {
  3271. .apply_req = __cam_isp_ctx_apply_req_in_bubble,
  3272. },
  3273. .irq_ops = NULL,
  3274. },
  3275. /* Bubble Applied */
  3276. {
  3277. .ioctl_ops = {},
  3278. .crm_ops = {},
  3279. .irq_ops = NULL,
  3280. },
  3281. /* HW ERROR */
  3282. {
  3283. .ioctl_ops = {},
  3284. .crm_ops = {},
  3285. .irq_ops = NULL,
  3286. },
  3287. /* HALT */
  3288. {
  3289. .ioctl_ops = {},
  3290. .crm_ops = {},
  3291. .irq_ops = NULL,
  3292. },
  3293. };
  3294. static struct cam_ctx_ops
  3295. cam_isp_ctx_fs2_state_machine[CAM_ISP_CTX_ACTIVATED_MAX] = {
  3296. /* SOF */
  3297. {
  3298. .ioctl_ops = {},
  3299. .crm_ops = {
  3300. .apply_req = __cam_isp_ctx_apply_req_in_sof,
  3301. },
  3302. .irq_ops = NULL,
  3303. },
  3304. /* APPLIED */
  3305. {
  3306. .ioctl_ops = {},
  3307. .crm_ops = {},
  3308. .irq_ops = NULL,
  3309. },
  3310. /* EPOCH */
  3311. {
  3312. .ioctl_ops = {},
  3313. .crm_ops = {
  3314. .apply_req = __cam_isp_ctx_apply_req_in_epoch,
  3315. },
  3316. .irq_ops = NULL,
  3317. },
  3318. /* BUBBLE */
  3319. {
  3320. .ioctl_ops = {},
  3321. .crm_ops = {
  3322. .apply_req = __cam_isp_ctx_apply_req_in_bubble,
  3323. },
  3324. .irq_ops = NULL,
  3325. },
  3326. /* Bubble Applied */
  3327. {
  3328. .ioctl_ops = {},
  3329. .crm_ops = {},
  3330. .irq_ops = NULL,
  3331. },
  3332. /* HW ERROR */
  3333. {
  3334. .ioctl_ops = {},
  3335. .crm_ops = {},
  3336. .irq_ops = NULL,
  3337. },
  3338. /* HALT */
  3339. {
  3340. .ioctl_ops = {},
  3341. .crm_ops = {},
  3342. .irq_ops = NULL,
  3343. },
  3344. };
  3345. static int __cam_isp_ctx_rdi_only_sof_in_top_state(
  3346. struct cam_isp_context *ctx_isp, void *evt_data)
  3347. {
  3348. int rc = 0;
  3349. struct cam_context *ctx = ctx_isp->base;
  3350. struct cam_req_mgr_trigger_notify notify;
  3351. struct cam_isp_hw_sof_event_data *sof_event_data = evt_data;
  3352. uint64_t request_id = 0;
  3353. if (!evt_data) {
  3354. CAM_ERR(CAM_ISP, "in valid sof event data");
  3355. return -EINVAL;
  3356. }
  3357. ctx_isp->frame_id++;
  3358. ctx_isp->sof_timestamp_val = sof_event_data->timestamp;
  3359. ctx_isp->boot_timestamp = sof_event_data->boot_time;
  3360. CAM_DBG(CAM_ISP, "frame id: %lld time stamp:0x%llx",
  3361. ctx_isp->frame_id, ctx_isp->sof_timestamp_val);
  3362. /*
  3363. * notify reqmgr with sof signal. Note, due to scheduling delay
  3364. * we can run into situation that two active requests has already
  3365. * be in the active queue while we try to do the notification.
  3366. * In this case, we need to skip the current notification. This
  3367. * helps the state machine to catch up the delay.
  3368. */
  3369. if (ctx->ctx_crm_intf && ctx->ctx_crm_intf->notify_trigger &&
  3370. ctx_isp->active_req_cnt <= 2) {
  3371. notify.link_hdl = ctx->link_hdl;
  3372. notify.dev_hdl = ctx->dev_hdl;
  3373. notify.frame_id = ctx_isp->frame_id;
  3374. notify.trigger = CAM_TRIGGER_POINT_SOF;
  3375. notify.req_id = ctx_isp->req_info.last_bufdone_req_id;
  3376. notify.sof_timestamp_val = ctx_isp->sof_timestamp_val;
  3377. ctx->ctx_crm_intf->notify_trigger(&notify);
  3378. CAM_DBG(CAM_ISP, "Notify CRM SOF frame %lld",
  3379. ctx_isp->frame_id);
  3380. /*
  3381. * It is idle frame with out any applied request id, send
  3382. * request id as zero
  3383. */
  3384. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  3385. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  3386. } else {
  3387. CAM_ERR_RATE_LIMIT(CAM_ISP, "Can not notify SOF to CRM");
  3388. }
  3389. if (list_empty(&ctx->active_req_list))
  3390. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_SOF;
  3391. else
  3392. CAM_DBG(CAM_ISP, "Still need to wait for the buf done");
  3393. CAM_DBG(CAM_ISP, "next Substate[%s]",
  3394. __cam_isp_ctx_substate_val_to_type(
  3395. ctx_isp->substate_activated));
  3396. return rc;
  3397. }
  3398. static int __cam_isp_ctx_rdi_only_sof_in_applied_state(
  3399. struct cam_isp_context *ctx_isp, void *evt_data)
  3400. {
  3401. struct cam_isp_hw_sof_event_data *sof_event_data = evt_data;
  3402. if (!evt_data) {
  3403. CAM_ERR(CAM_ISP, "in valid sof event data");
  3404. return -EINVAL;
  3405. }
  3406. ctx_isp->frame_id++;
  3407. ctx_isp->sof_timestamp_val = sof_event_data->timestamp;
  3408. ctx_isp->boot_timestamp = sof_event_data->boot_time;
  3409. CAM_DBG(CAM_ISP, "frame id: %lld time stamp:0x%llx",
  3410. ctx_isp->frame_id, ctx_isp->sof_timestamp_val);
  3411. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_BUBBLE_APPLIED;
  3412. CAM_DBG(CAM_ISP, "next Substate[%s]",
  3413. __cam_isp_ctx_substate_val_to_type(
  3414. ctx_isp->substate_activated));
  3415. return 0;
  3416. }
  3417. static int __cam_isp_ctx_rdi_only_sof_in_bubble_applied(
  3418. struct cam_isp_context *ctx_isp, void *evt_data)
  3419. {
  3420. struct cam_ctx_request *req;
  3421. struct cam_isp_ctx_req *req_isp;
  3422. struct cam_context *ctx = ctx_isp->base;
  3423. struct cam_isp_hw_sof_event_data *sof_event_data = evt_data;
  3424. uint64_t request_id = 0;
  3425. /*
  3426. * Sof in bubble applied state means, reg update not received.
  3427. * before increment frame id and override time stamp value, send
  3428. * the previous sof time stamp that got captured in the
  3429. * sof in applied state.
  3430. */
  3431. CAM_DBG(CAM_ISP, "frame id: %lld time stamp:0x%llx",
  3432. ctx_isp->frame_id, ctx_isp->sof_timestamp_val);
  3433. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  3434. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  3435. ctx_isp->frame_id++;
  3436. ctx_isp->sof_timestamp_val = sof_event_data->timestamp;
  3437. ctx_isp->boot_timestamp = sof_event_data->boot_time;
  3438. CAM_DBG(CAM_ISP, "frame id: %lld time stamp:0x%llx",
  3439. ctx_isp->frame_id, ctx_isp->sof_timestamp_val);
  3440. if (list_empty(&ctx->wait_req_list)) {
  3441. /*
  3442. * If no pending req in epoch, this is an error case.
  3443. * The recovery is to go back to sof state
  3444. */
  3445. CAM_ERR(CAM_ISP, "No wait request");
  3446. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_SOF;
  3447. /* Send SOF event as empty frame*/
  3448. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  3449. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  3450. goto end;
  3451. }
  3452. req = list_first_entry(&ctx->wait_req_list, struct cam_ctx_request,
  3453. list);
  3454. req_isp = (struct cam_isp_ctx_req *)req->req_priv;
  3455. req_isp->bubble_detected = true;
  3456. CAM_INFO_RATE_LIMIT(CAM_ISP, "Ctx:%d Report Bubble flag %d req id:%lld",
  3457. ctx->ctx_id, req_isp->bubble_report, req->request_id);
  3458. req_isp->reapply = true;
  3459. req_isp->cdm_reset_before_apply = false;
  3460. if (req_isp->bubble_report && ctx->ctx_crm_intf &&
  3461. ctx->ctx_crm_intf->notify_err) {
  3462. struct cam_req_mgr_error_notify notify;
  3463. notify.link_hdl = ctx->link_hdl;
  3464. notify.dev_hdl = ctx->dev_hdl;
  3465. notify.req_id = req->request_id;
  3466. notify.error = CRM_KMD_ERR_BUBBLE;
  3467. notify.trigger = 0;
  3468. if (ctx_isp->subscribe_event & CAM_TRIGGER_POINT_SOF)
  3469. notify.trigger = CAM_TRIGGER_POINT_SOF;
  3470. notify.frame_id = ctx_isp->frame_id;
  3471. notify.sof_timestamp_val = ctx_isp->sof_timestamp_val;
  3472. CAM_WARN_RATE_LIMIT(CAM_ISP,
  3473. "Notify CRM about Bubble req %lld frame %lld ctx %u",
  3474. req->request_id,
  3475. ctx_isp->frame_id,
  3476. ctx->ctx_id);
  3477. ctx->ctx_crm_intf->notify_err(&notify);
  3478. } else {
  3479. req_isp->bubble_report = 0;
  3480. }
  3481. /*
  3482. * Always move the request to active list. Let buf done
  3483. * function handles the rest.
  3484. */
  3485. list_del_init(&req->list);
  3486. list_add_tail(&req->list, &ctx->active_req_list);
  3487. ctx_isp->active_req_cnt++;
  3488. CAM_DBG(CAM_ISP, "move request %lld to active list(cnt = %d)",
  3489. req->request_id, ctx_isp->active_req_cnt);
  3490. if (!req_isp->bubble_report) {
  3491. if (req->request_id > ctx_isp->reported_req_id) {
  3492. request_id = req->request_id;
  3493. ctx_isp->reported_req_id = request_id;
  3494. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  3495. CAM_REQ_MGR_SOF_EVENT_ERROR);
  3496. } else
  3497. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  3498. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  3499. } else
  3500. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  3501. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  3502. /* change the state to bubble, as reg update has not come */
  3503. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_BUBBLE;
  3504. CAM_DBG(CAM_ISP, "next Substate[%s]",
  3505. __cam_isp_ctx_substate_val_to_type(
  3506. ctx_isp->substate_activated));
  3507. end:
  3508. return 0;
  3509. }
  3510. static int __cam_isp_ctx_rdi_only_sof_in_bubble_state(
  3511. struct cam_isp_context *ctx_isp, void *evt_data)
  3512. {
  3513. uint32_t i;
  3514. struct cam_ctx_request *req;
  3515. struct cam_context *ctx = ctx_isp->base;
  3516. struct cam_req_mgr_trigger_notify notify;
  3517. struct cam_isp_hw_sof_event_data *sof_event_data = evt_data;
  3518. struct cam_isp_ctx_req *req_isp;
  3519. uint64_t request_id = 0;
  3520. if (!evt_data) {
  3521. CAM_ERR(CAM_ISP, "in valid sof event data");
  3522. return -EINVAL;
  3523. }
  3524. ctx_isp->frame_id++;
  3525. ctx_isp->sof_timestamp_val = sof_event_data->timestamp;
  3526. ctx_isp->boot_timestamp = sof_event_data->boot_time;
  3527. CAM_DBG(CAM_ISP, "frame id: %lld time stamp:0x%llx",
  3528. ctx_isp->frame_id, ctx_isp->sof_timestamp_val);
  3529. /*
  3530. * Signal all active requests with error and move the all the active
  3531. * requests to free list
  3532. */
  3533. while (!list_empty(&ctx->active_req_list)) {
  3534. req = list_first_entry(&ctx->active_req_list,
  3535. struct cam_ctx_request, list);
  3536. list_del_init(&req->list);
  3537. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  3538. CAM_DBG(CAM_ISP, "signal fence in active list. fence num %d",
  3539. req_isp->num_fence_map_out);
  3540. for (i = 0; i < req_isp->num_fence_map_out; i++)
  3541. if (req_isp->fence_map_out[i].sync_id != -1) {
  3542. cam_sync_signal(
  3543. req_isp->fence_map_out[i].sync_id,
  3544. CAM_SYNC_STATE_SIGNALED_ERROR,
  3545. CAM_SYNC_ISP_EVENT_BUBBLE);
  3546. }
  3547. list_add_tail(&req->list, &ctx->free_req_list);
  3548. ctx_isp->active_req_cnt--;
  3549. }
  3550. /* notify reqmgr with sof signal */
  3551. if (ctx->ctx_crm_intf && ctx->ctx_crm_intf->notify_trigger) {
  3552. notify.link_hdl = ctx->link_hdl;
  3553. notify.dev_hdl = ctx->dev_hdl;
  3554. notify.frame_id = ctx_isp->frame_id;
  3555. notify.trigger = CAM_TRIGGER_POINT_SOF;
  3556. notify.req_id = ctx_isp->req_info.last_bufdone_req_id;
  3557. notify.sof_timestamp_val = ctx_isp->sof_timestamp_val;
  3558. ctx->ctx_crm_intf->notify_trigger(&notify);
  3559. CAM_DBG(CAM_ISP, "Notify CRM SOF frame %lld",
  3560. ctx_isp->frame_id);
  3561. } else {
  3562. CAM_ERR(CAM_ISP, "Can not notify SOF to CRM");
  3563. }
  3564. /*
  3565. * It is idle frame with out any applied request id, send
  3566. * request id as zero
  3567. */
  3568. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  3569. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  3570. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_SOF;
  3571. CAM_DBG(CAM_ISP, "next Substate[%s]",
  3572. __cam_isp_ctx_substate_val_to_type(
  3573. ctx_isp->substate_activated));
  3574. return 0;
  3575. }
  3576. static int __cam_isp_ctx_rdi_only_reg_upd_in_bubble_state(
  3577. struct cam_isp_context *ctx_isp, void *evt_data)
  3578. {
  3579. struct cam_ctx_request *req = NULL;
  3580. struct cam_context *ctx = ctx_isp->base;
  3581. req = list_first_entry(&ctx->active_req_list,
  3582. struct cam_ctx_request, list);
  3583. CAM_INFO(CAM_ISP, "Received RUP for Bubble Request", req->request_id);
  3584. return 0;
  3585. }
  3586. static int __cam_isp_ctx_rdi_only_reg_upd_in_bubble_applied_state(
  3587. struct cam_isp_context *ctx_isp, void *evt_data)
  3588. {
  3589. struct cam_ctx_request *req = NULL;
  3590. struct cam_context *ctx = ctx_isp->base;
  3591. struct cam_isp_ctx_req *req_isp;
  3592. struct cam_req_mgr_trigger_notify notify;
  3593. uint64_t request_id = 0;
  3594. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_EPOCH;
  3595. /* notify reqmgr with sof signal*/
  3596. if (ctx->ctx_crm_intf && ctx->ctx_crm_intf->notify_trigger) {
  3597. if (list_empty(&ctx->wait_req_list)) {
  3598. CAM_ERR(CAM_ISP, "Reg upd ack with no waiting request");
  3599. goto error;
  3600. }
  3601. req = list_first_entry(&ctx->wait_req_list,
  3602. struct cam_ctx_request, list);
  3603. list_del_init(&req->list);
  3604. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  3605. request_id =
  3606. (req_isp->hw_update_data.packet_opcode_type ==
  3607. CAM_ISP_PACKET_INIT_DEV) ?
  3608. 0 : req->request_id;
  3609. if (req_isp->num_fence_map_out != 0) {
  3610. list_add_tail(&req->list, &ctx->active_req_list);
  3611. ctx_isp->active_req_cnt++;
  3612. CAM_DBG(CAM_ISP,
  3613. "move request %lld to active list(cnt = %d)",
  3614. req->request_id, ctx_isp->active_req_cnt);
  3615. /* if packet has buffers, set correct request id */
  3616. request_id = req->request_id;
  3617. } else {
  3618. /* no io config, so the request is completed. */
  3619. list_add_tail(&req->list, &ctx->free_req_list);
  3620. CAM_DBG(CAM_ISP,
  3621. "move active req %lld to free list(cnt=%d)",
  3622. req->request_id, ctx_isp->active_req_cnt);
  3623. }
  3624. notify.link_hdl = ctx->link_hdl;
  3625. notify.dev_hdl = ctx->dev_hdl;
  3626. notify.frame_id = ctx_isp->frame_id;
  3627. notify.trigger = CAM_TRIGGER_POINT_SOF;
  3628. notify.req_id = ctx_isp->req_info.last_bufdone_req_id;
  3629. notify.sof_timestamp_val = ctx_isp->sof_timestamp_val;
  3630. ctx->ctx_crm_intf->notify_trigger(&notify);
  3631. CAM_DBG(CAM_ISP, "Notify CRM SOF frame %lld",
  3632. ctx_isp->frame_id);
  3633. } else {
  3634. CAM_ERR(CAM_ISP, "Can not notify SOF to CRM");
  3635. }
  3636. if (request_id)
  3637. ctx_isp->reported_req_id = request_id;
  3638. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  3639. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  3640. CAM_DBG(CAM_ISP, "next Substate[%s]",
  3641. __cam_isp_ctx_substate_val_to_type(
  3642. ctx_isp->substate_activated));
  3643. __cam_isp_ctx_update_event_record(ctx_isp,
  3644. CAM_ISP_CTX_EVENT_RUP, req);
  3645. return 0;
  3646. error:
  3647. /* Send SOF event as idle frame*/
  3648. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  3649. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  3650. __cam_isp_ctx_update_event_record(ctx_isp,
  3651. CAM_ISP_CTX_EVENT_RUP, NULL);
  3652. /*
  3653. * There is no request in the pending list, move the sub state machine
  3654. * to SOF sub state
  3655. */
  3656. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_SOF;
  3657. return 0;
  3658. }
  3659. static struct cam_isp_ctx_irq_ops
  3660. cam_isp_ctx_rdi_only_activated_state_machine_irq
  3661. [CAM_ISP_CTX_ACTIVATED_MAX] = {
  3662. /* SOF */
  3663. {
  3664. .irq_ops = {
  3665. NULL,
  3666. __cam_isp_ctx_rdi_only_sof_in_top_state,
  3667. __cam_isp_ctx_reg_upd_in_sof,
  3668. NULL,
  3669. NULL,
  3670. NULL,
  3671. },
  3672. },
  3673. /* APPLIED */
  3674. {
  3675. .irq_ops = {
  3676. __cam_isp_ctx_handle_error,
  3677. __cam_isp_ctx_rdi_only_sof_in_applied_state,
  3678. NULL,
  3679. NULL,
  3680. NULL,
  3681. __cam_isp_ctx_buf_done_in_applied,
  3682. },
  3683. },
  3684. /* EPOCH */
  3685. {
  3686. .irq_ops = {
  3687. __cam_isp_ctx_handle_error,
  3688. __cam_isp_ctx_rdi_only_sof_in_top_state,
  3689. NULL,
  3690. NULL,
  3691. NULL,
  3692. __cam_isp_ctx_buf_done_in_epoch,
  3693. },
  3694. },
  3695. /* BUBBLE*/
  3696. {
  3697. .irq_ops = {
  3698. __cam_isp_ctx_handle_error,
  3699. __cam_isp_ctx_rdi_only_sof_in_bubble_state,
  3700. __cam_isp_ctx_rdi_only_reg_upd_in_bubble_state,
  3701. NULL,
  3702. NULL,
  3703. __cam_isp_ctx_buf_done_in_bubble,
  3704. },
  3705. },
  3706. /* BUBBLE APPLIED ie PRE_BUBBLE */
  3707. {
  3708. .irq_ops = {
  3709. __cam_isp_ctx_handle_error,
  3710. __cam_isp_ctx_rdi_only_sof_in_bubble_applied,
  3711. __cam_isp_ctx_rdi_only_reg_upd_in_bubble_applied_state,
  3712. NULL,
  3713. NULL,
  3714. __cam_isp_ctx_buf_done_in_bubble_applied,
  3715. },
  3716. },
  3717. /* HW ERROR */
  3718. {
  3719. },
  3720. /* HALT */
  3721. {
  3722. },
  3723. };
  3724. static int __cam_isp_ctx_rdi_only_apply_req_top_state(
  3725. struct cam_context *ctx, struct cam_req_mgr_apply_request *apply)
  3726. {
  3727. int rc = 0;
  3728. struct cam_isp_context *ctx_isp =
  3729. (struct cam_isp_context *) ctx->ctx_priv;
  3730. CAM_DBG(CAM_ISP, "current Substate[%s]",
  3731. __cam_isp_ctx_substate_val_to_type(
  3732. ctx_isp->substate_activated));
  3733. rc = __cam_isp_ctx_apply_req_in_activated_state(ctx, apply,
  3734. CAM_ISP_CTX_ACTIVATED_APPLIED);
  3735. CAM_DBG(CAM_ISP, "new Substate[%s]",
  3736. __cam_isp_ctx_substate_val_to_type(
  3737. ctx_isp->substate_activated));
  3738. if (rc)
  3739. CAM_ERR_RATE_LIMIT(CAM_ISP,
  3740. "ctx_id:%d Apply failed in Substate[%s], rc %d",
  3741. ctx->ctx_id,
  3742. __cam_isp_ctx_substate_val_to_type(
  3743. ctx_isp->substate_activated), rc);
  3744. return rc;
  3745. }
  3746. static struct cam_ctx_ops
  3747. cam_isp_ctx_rdi_only_activated_state_machine
  3748. [CAM_ISP_CTX_ACTIVATED_MAX] = {
  3749. /* SOF */
  3750. {
  3751. .ioctl_ops = {},
  3752. .crm_ops = {
  3753. .apply_req = __cam_isp_ctx_rdi_only_apply_req_top_state,
  3754. },
  3755. .irq_ops = NULL,
  3756. },
  3757. /* APPLIED */
  3758. {
  3759. .ioctl_ops = {},
  3760. .crm_ops = {},
  3761. .irq_ops = NULL,
  3762. },
  3763. /* EPOCH */
  3764. {
  3765. .ioctl_ops = {},
  3766. .crm_ops = {
  3767. .apply_req = __cam_isp_ctx_rdi_only_apply_req_top_state,
  3768. },
  3769. .irq_ops = NULL,
  3770. },
  3771. /* PRE BUBBLE */
  3772. {
  3773. .ioctl_ops = {},
  3774. .crm_ops = {},
  3775. .irq_ops = NULL,
  3776. },
  3777. /* BUBBLE */
  3778. {
  3779. .ioctl_ops = {},
  3780. .crm_ops = {},
  3781. .irq_ops = NULL,
  3782. },
  3783. /* HW ERROR */
  3784. {
  3785. .ioctl_ops = {},
  3786. .crm_ops = {},
  3787. .irq_ops = NULL,
  3788. },
  3789. /* HALT */
  3790. {
  3791. .ioctl_ops = {},
  3792. .crm_ops = {},
  3793. .irq_ops = NULL,
  3794. },
  3795. };
  3796. static int __cam_isp_ctx_release_hw_in_top_state(struct cam_context *ctx,
  3797. void *cmd)
  3798. {
  3799. int rc = 0;
  3800. struct cam_hw_release_args rel_arg;
  3801. struct cam_isp_context *ctx_isp =
  3802. (struct cam_isp_context *) ctx->ctx_priv;
  3803. struct cam_req_mgr_flush_request flush_req;
  3804. int i;
  3805. if (ctx_isp->hw_ctx) {
  3806. rel_arg.ctxt_to_hw_map = ctx_isp->hw_ctx;
  3807. ctx->hw_mgr_intf->hw_release(ctx->hw_mgr_intf->hw_mgr_priv,
  3808. &rel_arg);
  3809. ctx_isp->hw_ctx = NULL;
  3810. } else {
  3811. CAM_ERR(CAM_ISP, "No hw resources acquired for ctx[%u]", ctx->ctx_id);
  3812. }
  3813. ctx->last_flush_req = 0;
  3814. ctx_isp->custom_enabled = false;
  3815. ctx_isp->use_frame_header_ts = false;
  3816. ctx_isp->frame_id = 0;
  3817. ctx_isp->active_req_cnt = 0;
  3818. ctx_isp->reported_req_id = 0;
  3819. ctx_isp->hw_acquired = false;
  3820. ctx_isp->init_received = false;
  3821. ctx_isp->support_consumed_addr = false;
  3822. ctx_isp->req_info.last_bufdone_req_id = 0;
  3823. atomic64_set(&ctx_isp->state_monitor_head, -1);
  3824. for (i = 0; i < CAM_ISP_CTX_EVENT_MAX; i++)
  3825. atomic64_set(&ctx_isp->event_record_head[i], -1);
  3826. /*
  3827. * Ideally, we should never have any active request here.
  3828. * But we still add some sanity check code here to help the debug
  3829. */
  3830. if (!list_empty(&ctx->active_req_list))
  3831. CAM_WARN(CAM_ISP, "Active list is not empty");
  3832. /* Flush all the pending request list */
  3833. flush_req.type = CAM_REQ_MGR_FLUSH_TYPE_ALL;
  3834. flush_req.link_hdl = ctx->link_hdl;
  3835. flush_req.dev_hdl = ctx->dev_hdl;
  3836. flush_req.req_id = 0;
  3837. CAM_DBG(CAM_ISP, "try to flush pending list");
  3838. spin_lock_bh(&ctx->lock);
  3839. rc = __cam_isp_ctx_flush_req(ctx, &ctx->pending_req_list, &flush_req);
  3840. spin_unlock_bh(&ctx->lock);
  3841. ctx->state = CAM_CTX_ACQUIRED;
  3842. trace_cam_context_state("ISP", ctx);
  3843. CAM_DBG(CAM_ISP, "Release device success[%u] next state %d",
  3844. ctx->ctx_id, ctx->state);
  3845. return rc;
  3846. }
  3847. /* top level state machine */
  3848. static int __cam_isp_ctx_release_dev_in_top_state(struct cam_context *ctx,
  3849. struct cam_release_dev_cmd *cmd)
  3850. {
  3851. int rc = 0;
  3852. int i;
  3853. struct cam_hw_release_args rel_arg;
  3854. struct cam_isp_context *ctx_isp =
  3855. (struct cam_isp_context *) ctx->ctx_priv;
  3856. struct cam_req_mgr_flush_request flush_req;
  3857. if (cmd && ctx_isp->hw_ctx) {
  3858. CAM_ERR(CAM_ISP, "releasing hw");
  3859. __cam_isp_ctx_release_hw_in_top_state(ctx, NULL);
  3860. }
  3861. if (ctx_isp->hw_ctx) {
  3862. rel_arg.ctxt_to_hw_map = ctx_isp->hw_ctx;
  3863. ctx->hw_mgr_intf->hw_release(ctx->hw_mgr_intf->hw_mgr_priv,
  3864. &rel_arg);
  3865. ctx_isp->hw_ctx = NULL;
  3866. }
  3867. ctx->session_hdl = -1;
  3868. ctx->dev_hdl = -1;
  3869. ctx->link_hdl = -1;
  3870. ctx->ctx_crm_intf = NULL;
  3871. ctx->last_flush_req = 0;
  3872. ctx_isp->frame_id = 0;
  3873. ctx_isp->active_req_cnt = 0;
  3874. ctx_isp->reported_req_id = 0;
  3875. ctx_isp->hw_acquired = false;
  3876. ctx_isp->init_received = false;
  3877. ctx_isp->offline_context = false;
  3878. ctx_isp->rdi_only_context = false;
  3879. ctx_isp->req_info.last_bufdone_req_id = 0;
  3880. atomic64_set(&ctx_isp->state_monitor_head, -1);
  3881. for (i = 0; i < CAM_ISP_CTX_EVENT_MAX; i++)
  3882. atomic64_set(&ctx_isp->event_record_head[i], -1);
  3883. /*
  3884. * Ideally, we should never have any active request here.
  3885. * But we still add some sanity check code here to help the debug
  3886. */
  3887. if (!list_empty(&ctx->active_req_list))
  3888. CAM_ERR(CAM_ISP, "Active list is not empty");
  3889. /* Flush all the pending request list */
  3890. flush_req.type = CAM_REQ_MGR_FLUSH_TYPE_ALL;
  3891. flush_req.link_hdl = ctx->link_hdl;
  3892. flush_req.dev_hdl = ctx->dev_hdl;
  3893. flush_req.req_id = 0;
  3894. CAM_DBG(CAM_ISP, "try to flush pending list");
  3895. spin_lock_bh(&ctx->lock);
  3896. rc = __cam_isp_ctx_flush_req(ctx, &ctx->pending_req_list, &flush_req);
  3897. spin_unlock_bh(&ctx->lock);
  3898. ctx->state = CAM_CTX_AVAILABLE;
  3899. trace_cam_context_state("ISP", ctx);
  3900. CAM_DBG(CAM_ISP, "Release device success[%u] next state %d",
  3901. ctx->ctx_id, ctx->state);
  3902. return rc;
  3903. }
  3904. static int __cam_isp_ctx_config_dev_in_top_state(
  3905. struct cam_context *ctx, struct cam_config_dev_cmd *cmd)
  3906. {
  3907. int rc = 0, i;
  3908. struct cam_ctx_request *req = NULL;
  3909. struct cam_isp_ctx_req *req_isp;
  3910. uintptr_t packet_addr;
  3911. struct cam_packet *packet;
  3912. size_t len = 0;
  3913. size_t remain_len = 0;
  3914. struct cam_hw_prepare_update_args cfg = {0};
  3915. struct cam_req_mgr_add_request add_req;
  3916. struct cam_isp_context *ctx_isp =
  3917. (struct cam_isp_context *) ctx->ctx_priv;
  3918. struct cam_hw_cmd_args hw_cmd_args;
  3919. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  3920. uint32_t packet_opcode = 0;
  3921. CAM_DBG(CAM_ISP, "get free request object......");
  3922. /* get free request */
  3923. spin_lock_bh(&ctx->lock);
  3924. if (!list_empty(&ctx->free_req_list)) {
  3925. req = list_first_entry(&ctx->free_req_list,
  3926. struct cam_ctx_request, list);
  3927. list_del_init(&req->list);
  3928. }
  3929. spin_unlock_bh(&ctx->lock);
  3930. if (!req) {
  3931. CAM_ERR(CAM_ISP, "No more request obj free");
  3932. return -ENOMEM;
  3933. }
  3934. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  3935. /* for config dev, only memory handle is supported */
  3936. /* map packet from the memhandle */
  3937. rc = cam_mem_get_cpu_buf((int32_t) cmd->packet_handle,
  3938. &packet_addr, &len);
  3939. if (rc != 0) {
  3940. CAM_ERR(CAM_ISP, "Can not get packet address");
  3941. rc = -EINVAL;
  3942. goto free_req;
  3943. }
  3944. remain_len = len;
  3945. if ((len < sizeof(struct cam_packet)) ||
  3946. ((size_t)cmd->offset >= len - sizeof(struct cam_packet))) {
  3947. CAM_ERR(CAM_ISP, "invalid buff length: %zu or offset", len);
  3948. rc = -EINVAL;
  3949. goto free_req;
  3950. }
  3951. remain_len -= (size_t)cmd->offset;
  3952. packet = (struct cam_packet *)(packet_addr + (uint32_t)cmd->offset);
  3953. CAM_DBG(CAM_ISP, "pack_handle %llx", cmd->packet_handle);
  3954. CAM_DBG(CAM_ISP, "packet address is 0x%zx", packet_addr);
  3955. CAM_DBG(CAM_ISP, "packet with length %zu, offset 0x%llx",
  3956. len, cmd->offset);
  3957. CAM_DBG(CAM_ISP, "Packet request id %lld",
  3958. packet->header.request_id);
  3959. CAM_DBG(CAM_ISP, "Packet size 0x%x", packet->header.size);
  3960. CAM_DBG(CAM_ISP, "packet op %d", packet->header.op_code);
  3961. /* Query the packet opcode */
  3962. hw_cmd_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  3963. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  3964. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_GET_PACKET_OPCODE;
  3965. isp_hw_cmd_args.cmd_data = (void *)packet;
  3966. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  3967. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  3968. &hw_cmd_args);
  3969. if (rc) {
  3970. CAM_ERR(CAM_ISP, "HW command failed");
  3971. goto free_req;
  3972. }
  3973. packet_opcode = isp_hw_cmd_args.u.packet_op_code;
  3974. CAM_DBG(CAM_ISP, "packet op %d", packet_opcode);
  3975. if ((packet_opcode == CAM_ISP_PACKET_UPDATE_DEV)
  3976. && (packet->header.request_id <= ctx->last_flush_req)) {
  3977. CAM_INFO(CAM_ISP,
  3978. "request %lld has been flushed, reject packet",
  3979. packet->header.request_id);
  3980. rc = -EBADR;
  3981. goto free_req;
  3982. }
  3983. cfg.packet = packet;
  3984. cfg.remain_len = remain_len;
  3985. cfg.ctxt_to_hw_map = ctx_isp->hw_ctx;
  3986. cfg.max_hw_update_entries = CAM_ISP_CTX_CFG_MAX;
  3987. cfg.hw_update_entries = req_isp->cfg;
  3988. cfg.max_out_map_entries = CAM_ISP_CTX_RES_MAX;
  3989. cfg.max_in_map_entries = CAM_ISP_CTX_RES_MAX;
  3990. cfg.out_map_entries = req_isp->fence_map_out;
  3991. cfg.in_map_entries = req_isp->fence_map_in;
  3992. cfg.priv = &req_isp->hw_update_data;
  3993. cfg.pf_data = &(req->pf_data);
  3994. cfg.num_out_map_entries = 0;
  3995. cfg.num_in_map_entries = 0;
  3996. CAM_DBG(CAM_ISP, "try to prepare config packet......");
  3997. rc = ctx->hw_mgr_intf->hw_prepare_update(
  3998. ctx->hw_mgr_intf->hw_mgr_priv, &cfg);
  3999. if (rc != 0) {
  4000. CAM_ERR(CAM_ISP, "Prepare config packet failed in HW layer");
  4001. rc = -EFAULT;
  4002. goto free_req;
  4003. }
  4004. req_isp->num_cfg = cfg.num_hw_update_entries;
  4005. req_isp->num_fence_map_out = cfg.num_out_map_entries;
  4006. req_isp->num_fence_map_in = cfg.num_in_map_entries;
  4007. req_isp->num_acked = 0;
  4008. req_isp->num_deferred_acks = 0;
  4009. req_isp->bubble_detected = false;
  4010. req_isp->cdm_reset_before_apply = false;
  4011. req_isp->hw_update_data.packet = packet;
  4012. for (i = 0; i < req_isp->num_fence_map_out; i++) {
  4013. rc = cam_sync_get_obj_ref(req_isp->fence_map_out[i].sync_id);
  4014. if (rc) {
  4015. CAM_ERR(CAM_ISP, "Can't get ref for fence %d",
  4016. req_isp->fence_map_out[i].sync_id);
  4017. goto put_ref;
  4018. }
  4019. }
  4020. CAM_DBG(CAM_ISP, "num_entry: %d, num fence out: %d, num fence in: %d",
  4021. req_isp->num_cfg, req_isp->num_fence_map_out,
  4022. req_isp->num_fence_map_in);
  4023. req->request_id = packet->header.request_id;
  4024. req->status = 1;
  4025. CAM_DBG(CAM_ISP, "Packet request id %lld packet opcode:%d",
  4026. packet->header.request_id,
  4027. req_isp->hw_update_data.packet_opcode_type);
  4028. if (req_isp->hw_update_data.packet_opcode_type ==
  4029. CAM_ISP_PACKET_INIT_DEV) {
  4030. if (ctx->state < CAM_CTX_ACTIVATED) {
  4031. rc = __cam_isp_ctx_enqueue_init_request(ctx, req);
  4032. if (rc)
  4033. CAM_ERR(CAM_ISP, "Enqueue INIT pkt failed");
  4034. ctx_isp->init_received = true;
  4035. } else {
  4036. rc = -EINVAL;
  4037. CAM_ERR(CAM_ISP, "Recevied INIT pkt in wrong state:%d",
  4038. ctx->state);
  4039. }
  4040. } else {
  4041. if (ctx_isp->offline_context) {
  4042. __cam_isp_ctx_enqueue_request_in_order(ctx, req);
  4043. } else if ((ctx->state != CAM_CTX_FLUSHED) &&
  4044. (ctx->state >= CAM_CTX_READY) &&
  4045. ctx->ctx_crm_intf->add_req) {
  4046. add_req.link_hdl = ctx->link_hdl;
  4047. add_req.dev_hdl = ctx->dev_hdl;
  4048. add_req.req_id = req->request_id;
  4049. add_req.skip_before_applying = 0;
  4050. add_req.trigger_eof = false;
  4051. rc = ctx->ctx_crm_intf->add_req(&add_req);
  4052. if (rc) {
  4053. CAM_ERR(CAM_ISP, "Add req failed: req id=%llu",
  4054. req->request_id);
  4055. } else {
  4056. __cam_isp_ctx_enqueue_request_in_order(
  4057. ctx, req);
  4058. }
  4059. } else {
  4060. rc = -EINVAL;
  4061. CAM_ERR(CAM_ISP,
  4062. "Recevied update req %lld in wrong state:%d",
  4063. req->request_id, ctx->state);
  4064. }
  4065. }
  4066. if (rc)
  4067. goto put_ref;
  4068. CAM_DBG(CAM_REQ,
  4069. "Preprocessing Config req_id %lld successful on ctx %u",
  4070. req->request_id, ctx->ctx_id);
  4071. if (ctx_isp->offline_context && atomic_read(&ctx_isp->rxd_epoch)) {
  4072. __cam_isp_ctx_schedule_apply_req_offline(ctx_isp);
  4073. }
  4074. return rc;
  4075. put_ref:
  4076. for (--i; i >= 0; i--) {
  4077. if (cam_sync_put_obj_ref(req_isp->fence_map_out[i].sync_id))
  4078. CAM_ERR(CAM_CTXT, "Failed to put ref of fence %d",
  4079. req_isp->fence_map_out[i].sync_id);
  4080. }
  4081. free_req:
  4082. spin_lock_bh(&ctx->lock);
  4083. list_add_tail(&req->list, &ctx->free_req_list);
  4084. spin_unlock_bh(&ctx->lock);
  4085. return rc;
  4086. }
  4087. static int __cam_isp_ctx_acquire_dev_in_available(struct cam_context *ctx,
  4088. struct cam_acquire_dev_cmd *cmd)
  4089. {
  4090. int rc = 0;
  4091. int i;
  4092. struct cam_hw_acquire_args param;
  4093. struct cam_isp_resource *isp_res = NULL;
  4094. struct cam_create_dev_hdl req_hdl_param;
  4095. struct cam_hw_release_args release;
  4096. struct cam_isp_context *ctx_isp =
  4097. (struct cam_isp_context *) ctx->ctx_priv;
  4098. struct cam_hw_cmd_args hw_cmd_args;
  4099. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  4100. if (!ctx->hw_mgr_intf) {
  4101. CAM_ERR(CAM_ISP, "HW interface is not ready");
  4102. rc = -EFAULT;
  4103. goto end;
  4104. }
  4105. CAM_DBG(CAM_ISP,
  4106. "session_hdl 0x%x, num_resources %d, hdl type %d, res %lld",
  4107. cmd->session_handle, cmd->num_resources,
  4108. cmd->handle_type, cmd->resource_hdl);
  4109. if (cmd->num_resources == CAM_API_COMPAT_CONSTANT) {
  4110. ctx_isp->split_acquire = true;
  4111. CAM_DBG(CAM_ISP, "Acquire dev handle");
  4112. goto get_dev_handle;
  4113. }
  4114. if (cmd->num_resources > CAM_ISP_CTX_RES_MAX) {
  4115. CAM_ERR(CAM_ISP, "Too much resources in the acquire");
  4116. rc = -ENOMEM;
  4117. goto end;
  4118. }
  4119. /* for now we only support user pointer */
  4120. if (cmd->handle_type != 1) {
  4121. CAM_ERR(CAM_ISP, "Only user pointer is supported");
  4122. rc = -EINVAL;
  4123. goto end;
  4124. }
  4125. isp_res = kzalloc(
  4126. sizeof(*isp_res)*cmd->num_resources, GFP_KERNEL);
  4127. if (!isp_res) {
  4128. rc = -ENOMEM;
  4129. goto end;
  4130. }
  4131. CAM_DBG(CAM_ISP, "start copy %d resources from user",
  4132. cmd->num_resources);
  4133. if (copy_from_user(isp_res, u64_to_user_ptr(cmd->resource_hdl),
  4134. sizeof(*isp_res)*cmd->num_resources)) {
  4135. rc = -EFAULT;
  4136. goto free_res;
  4137. }
  4138. param.context_data = ctx;
  4139. param.event_cb = ctx->irq_cb_intf;
  4140. param.num_acq = cmd->num_resources;
  4141. param.acquire_info = (uintptr_t) isp_res;
  4142. /* call HW manager to reserve the resource */
  4143. rc = ctx->hw_mgr_intf->hw_acquire(ctx->hw_mgr_intf->hw_mgr_priv,
  4144. &param);
  4145. if (rc != 0) {
  4146. CAM_ERR(CAM_ISP, "Acquire device failed");
  4147. goto free_res;
  4148. }
  4149. /* Query the context has rdi only resource */
  4150. hw_cmd_args.ctxt_to_hw_map = param.ctxt_to_hw_map;
  4151. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  4152. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_CMD_CTX_TYPE;
  4153. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  4154. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  4155. &hw_cmd_args);
  4156. if (rc) {
  4157. CAM_ERR(CAM_ISP, "HW command failed");
  4158. goto free_hw;
  4159. }
  4160. if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_RDI) {
  4161. /*
  4162. * this context has rdi only resource assign rdi only
  4163. * state machine
  4164. */
  4165. CAM_DBG(CAM_ISP, "RDI only session Context");
  4166. ctx_isp->substate_machine_irq =
  4167. cam_isp_ctx_rdi_only_activated_state_machine_irq;
  4168. ctx_isp->substate_machine =
  4169. cam_isp_ctx_rdi_only_activated_state_machine;
  4170. ctx_isp->rdi_only_context = true;
  4171. } else if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_FS2) {
  4172. CAM_DBG(CAM_ISP, "FS2 Session has PIX, RD and RDI");
  4173. ctx_isp->substate_machine_irq =
  4174. cam_isp_ctx_fs2_state_machine_irq;
  4175. ctx_isp->substate_machine =
  4176. cam_isp_ctx_fs2_state_machine;
  4177. } else if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_OFFLINE) {
  4178. CAM_DBG(CAM_ISP, "offline Session has PIX and RD resources");
  4179. ctx_isp->substate_machine_irq =
  4180. cam_isp_ctx_offline_state_machine_irq;
  4181. } else {
  4182. CAM_DBG(CAM_ISP, "Session has PIX or PIX and RDI resources");
  4183. ctx_isp->substate_machine_irq =
  4184. cam_isp_ctx_activated_state_machine_irq;
  4185. ctx_isp->substate_machine =
  4186. cam_isp_ctx_activated_state_machine;
  4187. }
  4188. ctx_isp->hw_ctx = param.ctxt_to_hw_map;
  4189. ctx_isp->hw_acquired = true;
  4190. ctx_isp->split_acquire = false;
  4191. ctx->ctxt_to_hw_map = param.ctxt_to_hw_map;
  4192. atomic64_set(&ctx_isp->state_monitor_head, -1);
  4193. for (i = 0; i < CAM_ISP_CTX_EVENT_MAX; i++)
  4194. atomic64_set(&ctx_isp->event_record_head[i], -1);
  4195. kfree(isp_res);
  4196. isp_res = NULL;
  4197. get_dev_handle:
  4198. req_hdl_param.session_hdl = cmd->session_handle;
  4199. /* bridge is not ready for these flags. so false for now */
  4200. req_hdl_param.v4l2_sub_dev_flag = 0;
  4201. req_hdl_param.media_entity_flag = 0;
  4202. req_hdl_param.ops = ctx->crm_ctx_intf;
  4203. req_hdl_param.priv = ctx;
  4204. req_hdl_param.dev_id = CAM_ISP;
  4205. CAM_DBG(CAM_ISP, "get device handle form bridge");
  4206. ctx->dev_hdl = cam_create_device_hdl(&req_hdl_param);
  4207. if (ctx->dev_hdl <= 0) {
  4208. rc = -EFAULT;
  4209. CAM_ERR(CAM_ISP, "Can not create device handle");
  4210. goto free_hw;
  4211. }
  4212. cmd->dev_handle = ctx->dev_hdl;
  4213. /* store session information */
  4214. ctx->session_hdl = cmd->session_handle;
  4215. ctx->state = CAM_CTX_ACQUIRED;
  4216. trace_cam_context_state("ISP", ctx);
  4217. CAM_DBG(CAM_ISP,
  4218. "Acquire success on session_hdl 0x%x num_rsrces %d ctx %u",
  4219. cmd->session_handle, cmd->num_resources, ctx->ctx_id);
  4220. return rc;
  4221. free_hw:
  4222. release.ctxt_to_hw_map = ctx_isp->hw_ctx;
  4223. if (ctx_isp->hw_acquired)
  4224. ctx->hw_mgr_intf->hw_release(ctx->hw_mgr_intf->hw_mgr_priv,
  4225. &release);
  4226. ctx_isp->hw_ctx = NULL;
  4227. ctx_isp->hw_acquired = false;
  4228. free_res:
  4229. kfree(isp_res);
  4230. end:
  4231. return rc;
  4232. }
  4233. static int __cam_isp_ctx_acquire_hw_v1(struct cam_context *ctx,
  4234. void *args)
  4235. {
  4236. int rc = 0;
  4237. int i;
  4238. struct cam_acquire_hw_cmd_v1 *cmd =
  4239. (struct cam_acquire_hw_cmd_v1 *)args;
  4240. struct cam_hw_acquire_args param;
  4241. struct cam_hw_release_args release;
  4242. struct cam_isp_context *ctx_isp =
  4243. (struct cam_isp_context *) ctx->ctx_priv;
  4244. struct cam_hw_cmd_args hw_cmd_args;
  4245. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  4246. struct cam_isp_acquire_hw_info *acquire_hw_info = NULL;
  4247. if (!ctx->hw_mgr_intf) {
  4248. CAM_ERR(CAM_ISP, "HW interface is not ready");
  4249. rc = -EFAULT;
  4250. goto end;
  4251. }
  4252. CAM_DBG(CAM_ISP,
  4253. "session_hdl 0x%x, hdl type %d, res %lld",
  4254. cmd->session_handle, cmd->handle_type, cmd->resource_hdl);
  4255. /* for now we only support user pointer */
  4256. if (cmd->handle_type != 1) {
  4257. CAM_ERR(CAM_ISP, "Only user pointer is supported");
  4258. rc = -EINVAL;
  4259. goto end;
  4260. }
  4261. if (cmd->data_size < sizeof(*acquire_hw_info)) {
  4262. CAM_ERR(CAM_ISP, "data_size is not a valid value");
  4263. goto end;
  4264. }
  4265. acquire_hw_info = kzalloc(cmd->data_size, GFP_KERNEL);
  4266. if (!acquire_hw_info) {
  4267. rc = -ENOMEM;
  4268. goto end;
  4269. }
  4270. CAM_DBG(CAM_ISP, "start copy resources from user");
  4271. if (copy_from_user(acquire_hw_info, (void __user *)cmd->resource_hdl,
  4272. cmd->data_size)) {
  4273. rc = -EFAULT;
  4274. goto free_res;
  4275. }
  4276. memset(&param, 0, sizeof(param));
  4277. param.context_data = ctx;
  4278. param.event_cb = ctx->irq_cb_intf;
  4279. param.num_acq = CAM_API_COMPAT_CONSTANT;
  4280. param.acquire_info_size = cmd->data_size;
  4281. param.acquire_info = (uint64_t) acquire_hw_info;
  4282. /* call HW manager to reserve the resource */
  4283. rc = ctx->hw_mgr_intf->hw_acquire(ctx->hw_mgr_intf->hw_mgr_priv,
  4284. &param);
  4285. if (rc != 0) {
  4286. CAM_ERR(CAM_ISP, "Acquire device failed");
  4287. goto free_res;
  4288. }
  4289. ctx_isp->support_consumed_addr =
  4290. param.support_consumed_addr;
  4291. /* Query the context has rdi only resource */
  4292. hw_cmd_args.ctxt_to_hw_map = param.ctxt_to_hw_map;
  4293. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  4294. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_CMD_CTX_TYPE;
  4295. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  4296. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  4297. &hw_cmd_args);
  4298. if (rc) {
  4299. CAM_ERR(CAM_ISP, "HW command failed");
  4300. goto free_hw;
  4301. }
  4302. if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_RDI) {
  4303. /*
  4304. * this context has rdi only resource assign rdi only
  4305. * state machine
  4306. */
  4307. CAM_DBG(CAM_ISP, "RDI only session Context");
  4308. ctx_isp->substate_machine_irq =
  4309. cam_isp_ctx_rdi_only_activated_state_machine_irq;
  4310. ctx_isp->substate_machine =
  4311. cam_isp_ctx_rdi_only_activated_state_machine;
  4312. ctx_isp->rdi_only_context = true;
  4313. } else if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_FS2) {
  4314. CAM_DBG(CAM_ISP, "FS2 Session has PIX, RD and RDI");
  4315. ctx_isp->substate_machine_irq =
  4316. cam_isp_ctx_fs2_state_machine_irq;
  4317. ctx_isp->substate_machine =
  4318. cam_isp_ctx_fs2_state_machine;
  4319. } else if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_OFFLINE) {
  4320. CAM_DBG(CAM_ISP, "Offline session has PIX and RD resources");
  4321. ctx_isp->substate_machine_irq =
  4322. cam_isp_ctx_offline_state_machine_irq;
  4323. ctx_isp->substate_machine = NULL;
  4324. } else {
  4325. CAM_DBG(CAM_ISP, "Session has PIX or PIX and RDI resources");
  4326. ctx_isp->substate_machine_irq =
  4327. cam_isp_ctx_activated_state_machine_irq;
  4328. ctx_isp->substate_machine =
  4329. cam_isp_ctx_activated_state_machine;
  4330. }
  4331. ctx_isp->hw_ctx = param.ctxt_to_hw_map;
  4332. ctx_isp->hw_acquired = true;
  4333. ctx->ctxt_to_hw_map = param.ctxt_to_hw_map;
  4334. atomic64_set(&ctx_isp->state_monitor_head, -1);
  4335. for (i = 0; i < CAM_ISP_CTX_EVENT_MAX; i++)
  4336. atomic64_set(&ctx_isp->event_record_head[i], -1);
  4337. trace_cam_context_state("ISP", ctx);
  4338. CAM_DBG(CAM_ISP,
  4339. "Acquire success on session_hdl 0x%xs ctx_type %d ctx_id %u",
  4340. ctx->session_hdl, isp_hw_cmd_args.u.ctx_type, ctx->ctx_id);
  4341. kfree(acquire_hw_info);
  4342. return rc;
  4343. free_hw:
  4344. release.ctxt_to_hw_map = ctx_isp->hw_ctx;
  4345. ctx->hw_mgr_intf->hw_release(ctx->hw_mgr_intf->hw_mgr_priv, &release);
  4346. ctx_isp->hw_ctx = NULL;
  4347. ctx_isp->hw_acquired = false;
  4348. free_res:
  4349. kfree(acquire_hw_info);
  4350. end:
  4351. return rc;
  4352. }
  4353. static void cam_req_mgr_process_workq_offline_ife_worker(struct work_struct *w)
  4354. {
  4355. cam_req_mgr_process_workq(w);
  4356. }
  4357. static int __cam_isp_ctx_acquire_hw_v2(struct cam_context *ctx,
  4358. void *args)
  4359. {
  4360. int rc = 0, i, j;
  4361. struct cam_acquire_hw_cmd_v2 *cmd =
  4362. (struct cam_acquire_hw_cmd_v2 *)args;
  4363. struct cam_hw_acquire_args param;
  4364. struct cam_hw_release_args release;
  4365. struct cam_isp_context *ctx_isp =
  4366. (struct cam_isp_context *) ctx->ctx_priv;
  4367. struct cam_hw_cmd_args hw_cmd_args;
  4368. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  4369. struct cam_isp_acquire_hw_info *acquire_hw_info = NULL;
  4370. if (!ctx->hw_mgr_intf) {
  4371. CAM_ERR(CAM_ISP, "HW interface is not ready");
  4372. rc = -EFAULT;
  4373. goto end;
  4374. }
  4375. CAM_DBG(CAM_ISP,
  4376. "session_hdl 0x%x, hdl type %d, res %lld",
  4377. cmd->session_handle, cmd->handle_type, cmd->resource_hdl);
  4378. /* for now we only support user pointer */
  4379. if (cmd->handle_type != 1) {
  4380. CAM_ERR(CAM_ISP, "Only user pointer is supported");
  4381. rc = -EINVAL;
  4382. goto end;
  4383. }
  4384. if (cmd->data_size < sizeof(*acquire_hw_info)) {
  4385. CAM_ERR(CAM_ISP, "data_size is not a valid value");
  4386. goto end;
  4387. }
  4388. acquire_hw_info = kzalloc(cmd->data_size, GFP_KERNEL);
  4389. if (!acquire_hw_info) {
  4390. rc = -ENOMEM;
  4391. goto end;
  4392. }
  4393. CAM_DBG(CAM_ISP, "start copy resources from user");
  4394. if (copy_from_user(acquire_hw_info, (void __user *)cmd->resource_hdl,
  4395. cmd->data_size)) {
  4396. rc = -EFAULT;
  4397. goto free_res;
  4398. }
  4399. memset(&param, 0, sizeof(param));
  4400. param.context_data = ctx;
  4401. param.event_cb = ctx->irq_cb_intf;
  4402. param.num_acq = CAM_API_COMPAT_CONSTANT;
  4403. param.acquire_info_size = cmd->data_size;
  4404. param.acquire_info = (uint64_t) acquire_hw_info;
  4405. /* call HW manager to reserve the resource */
  4406. rc = ctx->hw_mgr_intf->hw_acquire(ctx->hw_mgr_intf->hw_mgr_priv,
  4407. &param);
  4408. if (rc != 0) {
  4409. CAM_ERR(CAM_ISP, "Acquire device failed");
  4410. goto free_res;
  4411. }
  4412. /* Set custom flag if applicable
  4413. * custom hw is supported only on v2
  4414. */
  4415. ctx_isp->custom_enabled = param.custom_enabled;
  4416. ctx_isp->use_frame_header_ts = param.use_frame_header_ts;
  4417. ctx_isp->support_consumed_addr =
  4418. param.support_consumed_addr;
  4419. /* Query the context has rdi only resource */
  4420. hw_cmd_args.ctxt_to_hw_map = param.ctxt_to_hw_map;
  4421. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  4422. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_CMD_CTX_TYPE;
  4423. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  4424. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  4425. &hw_cmd_args);
  4426. if (rc) {
  4427. CAM_ERR(CAM_ISP, "HW command failed");
  4428. goto free_hw;
  4429. }
  4430. if (param.valid_acquired_hw) {
  4431. for (i = 0; i < CAM_MAX_ACQ_RES; i++)
  4432. cmd->hw_info.acquired_hw_id[i] =
  4433. param.acquired_hw_id[i];
  4434. for (i = 0; i < CAM_MAX_ACQ_RES; i++)
  4435. for (j = 0; j < CAM_MAX_HW_SPLIT; j++)
  4436. cmd->hw_info.acquired_hw_path[i][j] =
  4437. param.acquired_hw_path[i][j];
  4438. }
  4439. cmd->hw_info.valid_acquired_hw =
  4440. param.valid_acquired_hw;
  4441. cmd->hw_info.valid_acquired_hw = param.valid_acquired_hw;
  4442. if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_RDI) {
  4443. /*
  4444. * this context has rdi only resource assign rdi only
  4445. * state machine
  4446. */
  4447. CAM_DBG(CAM_ISP, "RDI only session Context");
  4448. ctx_isp->substate_machine_irq =
  4449. cam_isp_ctx_rdi_only_activated_state_machine_irq;
  4450. ctx_isp->substate_machine =
  4451. cam_isp_ctx_rdi_only_activated_state_machine;
  4452. ctx_isp->rdi_only_context = true;
  4453. } else if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_FS2) {
  4454. CAM_DBG(CAM_ISP, "FS2 Session has PIX, RD and RDI");
  4455. ctx_isp->substate_machine_irq =
  4456. cam_isp_ctx_fs2_state_machine_irq;
  4457. ctx_isp->substate_machine =
  4458. cam_isp_ctx_fs2_state_machine;
  4459. } else if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_OFFLINE) {
  4460. CAM_DBG(CAM_ISP, "Offline Session has PIX and RD resources");
  4461. ctx_isp->substate_machine_irq =
  4462. cam_isp_ctx_offline_state_machine_irq;
  4463. ctx_isp->substate_machine = NULL;
  4464. ctx_isp->offline_context = true;
  4465. rc = cam_req_mgr_workq_create("offline_ife", 20,
  4466. &ctx_isp->workq, CRM_WORKQ_USAGE_IRQ, 0,
  4467. cam_req_mgr_process_workq_offline_ife_worker);
  4468. if (rc)
  4469. CAM_ERR(CAM_ISP,
  4470. "Failed to create workq for offline IFE rc:%d",
  4471. rc);
  4472. } else {
  4473. CAM_DBG(CAM_ISP, "Session has PIX or PIX and RDI resources");
  4474. ctx_isp->substate_machine_irq =
  4475. cam_isp_ctx_activated_state_machine_irq;
  4476. ctx_isp->substate_machine =
  4477. cam_isp_ctx_activated_state_machine;
  4478. }
  4479. ctx_isp->hw_ctx = param.ctxt_to_hw_map;
  4480. ctx_isp->hw_acquired = true;
  4481. ctx->ctxt_to_hw_map = param.ctxt_to_hw_map;
  4482. trace_cam_context_state("ISP", ctx);
  4483. CAM_DBG(CAM_ISP,
  4484. "Acquire success on session_hdl 0x%xs ctx_type %d ctx_id %u",
  4485. ctx->session_hdl, isp_hw_cmd_args.u.ctx_type, ctx->ctx_id);
  4486. kfree(acquire_hw_info);
  4487. return rc;
  4488. free_hw:
  4489. release.ctxt_to_hw_map = ctx_isp->hw_ctx;
  4490. ctx->hw_mgr_intf->hw_release(ctx->hw_mgr_intf->hw_mgr_priv, &release);
  4491. ctx_isp->hw_ctx = NULL;
  4492. ctx_isp->hw_acquired = false;
  4493. free_res:
  4494. kfree(acquire_hw_info);
  4495. end:
  4496. return rc;
  4497. }
  4498. static int __cam_isp_ctx_acquire_hw_in_acquired(struct cam_context *ctx,
  4499. void *args)
  4500. {
  4501. int rc = -EINVAL;
  4502. uint32_t api_version;
  4503. if (!ctx || !args) {
  4504. CAM_ERR(CAM_ISP, "Invalid input pointer");
  4505. return rc;
  4506. }
  4507. api_version = *((uint32_t *)args);
  4508. if (api_version == 1)
  4509. rc = __cam_isp_ctx_acquire_hw_v1(ctx, args);
  4510. else if (api_version == 2)
  4511. rc = __cam_isp_ctx_acquire_hw_v2(ctx, args);
  4512. else
  4513. CAM_ERR(CAM_ISP, "Unsupported api version %d", api_version);
  4514. return rc;
  4515. }
  4516. static int __cam_isp_ctx_config_dev_in_acquired(struct cam_context *ctx,
  4517. struct cam_config_dev_cmd *cmd)
  4518. {
  4519. int rc = 0;
  4520. struct cam_isp_context *ctx_isp =
  4521. (struct cam_isp_context *) ctx->ctx_priv;
  4522. if (!ctx_isp->hw_acquired) {
  4523. CAM_ERR(CAM_ISP, "HW is not acquired, reject packet");
  4524. return -EINVAL;
  4525. }
  4526. rc = __cam_isp_ctx_config_dev_in_top_state(ctx, cmd);
  4527. if (!rc && ((ctx->link_hdl >= 0) || ctx_isp->offline_context)) {
  4528. ctx->state = CAM_CTX_READY;
  4529. trace_cam_context_state("ISP", ctx);
  4530. }
  4531. CAM_DBG(CAM_ISP, "next state %d", ctx->state);
  4532. return rc;
  4533. }
  4534. static int __cam_isp_ctx_config_dev_in_flushed(struct cam_context *ctx,
  4535. struct cam_config_dev_cmd *cmd)
  4536. {
  4537. int rc = 0;
  4538. struct cam_start_stop_dev_cmd start_cmd;
  4539. struct cam_hw_cmd_args hw_cmd_args;
  4540. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  4541. struct cam_isp_context *ctx_isp =
  4542. (struct cam_isp_context *) ctx->ctx_priv;
  4543. if (!ctx_isp->hw_acquired) {
  4544. CAM_ERR(CAM_ISP, "HW is not acquired, reject packet");
  4545. rc = -EINVAL;
  4546. goto end;
  4547. }
  4548. rc = __cam_isp_ctx_config_dev_in_top_state(ctx, cmd);
  4549. if (rc)
  4550. goto end;
  4551. if (!ctx_isp->init_received) {
  4552. CAM_WARN(CAM_ISP,
  4553. "Received update packet in flushed state, skip start");
  4554. goto end;
  4555. }
  4556. hw_cmd_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  4557. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  4558. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_CMD_RESUME_HW;
  4559. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  4560. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  4561. &hw_cmd_args);
  4562. if (rc) {
  4563. CAM_ERR(CAM_ISP, "Failed to resume HW rc: %d", rc);
  4564. goto end;
  4565. }
  4566. start_cmd.dev_handle = cmd->dev_handle;
  4567. start_cmd.session_handle = cmd->session_handle;
  4568. rc = __cam_isp_ctx_start_dev_in_ready(ctx, &start_cmd);
  4569. if (rc)
  4570. CAM_ERR(CAM_ISP,
  4571. "Failed to re-start HW after flush rc: %d", rc);
  4572. else
  4573. CAM_INFO(CAM_ISP,
  4574. "Received init after flush. Re-start HW complete in ctx:%d",
  4575. ctx->ctx_id);
  4576. end:
  4577. CAM_DBG(CAM_ISP, "next state %d sub_state:%d", ctx->state,
  4578. ctx_isp->substate_activated);
  4579. return rc;
  4580. }
  4581. static int __cam_isp_ctx_link_in_acquired(struct cam_context *ctx,
  4582. struct cam_req_mgr_core_dev_link_setup *link)
  4583. {
  4584. int rc = 0;
  4585. struct cam_isp_context *ctx_isp =
  4586. (struct cam_isp_context *) ctx->ctx_priv;
  4587. CAM_DBG(CAM_ISP, "Enter.........");
  4588. ctx->link_hdl = link->link_hdl;
  4589. ctx->ctx_crm_intf = link->crm_cb;
  4590. ctx_isp->subscribe_event =
  4591. CAM_TRIGGER_POINT_SOF | CAM_TRIGGER_POINT_EOF;
  4592. ctx_isp->trigger_id = link->trigger_id;
  4593. /* change state only if we had the init config */
  4594. if (ctx_isp->init_received) {
  4595. ctx->state = CAM_CTX_READY;
  4596. trace_cam_context_state("ISP", ctx);
  4597. }
  4598. CAM_DBG(CAM_ISP, "next state %d", ctx->state);
  4599. return rc;
  4600. }
  4601. static int __cam_isp_ctx_unlink_in_acquired(struct cam_context *ctx,
  4602. struct cam_req_mgr_core_dev_link_setup *unlink)
  4603. {
  4604. int rc = 0;
  4605. struct cam_isp_context *ctx_isp =
  4606. (struct cam_isp_context *) ctx->ctx_priv;
  4607. ctx->link_hdl = -1;
  4608. ctx->ctx_crm_intf = NULL;
  4609. ctx_isp->trigger_id = -1;
  4610. return rc;
  4611. }
  4612. static int __cam_isp_ctx_get_dev_info_in_acquired(struct cam_context *ctx,
  4613. struct cam_req_mgr_device_info *dev_info)
  4614. {
  4615. int rc = 0;
  4616. dev_info->dev_hdl = ctx->dev_hdl;
  4617. strlcpy(dev_info->name, CAM_ISP_DEV_NAME, sizeof(dev_info->name));
  4618. dev_info->dev_id = CAM_REQ_MGR_DEVICE_IFE;
  4619. dev_info->p_delay = 1;
  4620. dev_info->trigger = CAM_TRIGGER_POINT_SOF;
  4621. dev_info->trigger_on = true;
  4622. return rc;
  4623. }
  4624. static int __cam_isp_ctx_start_dev_in_ready(struct cam_context *ctx,
  4625. struct cam_start_stop_dev_cmd *cmd)
  4626. {
  4627. int rc = 0;
  4628. int i;
  4629. struct cam_isp_start_args start_isp;
  4630. struct cam_ctx_request *req;
  4631. struct cam_isp_ctx_req *req_isp;
  4632. struct cam_isp_context *ctx_isp =
  4633. (struct cam_isp_context *) ctx->ctx_priv;
  4634. if (cmd->session_handle != ctx->session_hdl ||
  4635. cmd->dev_handle != ctx->dev_hdl) {
  4636. rc = -EPERM;
  4637. goto end;
  4638. }
  4639. if (list_empty(&ctx->pending_req_list)) {
  4640. /* should never happen */
  4641. CAM_ERR(CAM_ISP, "Start device with empty configuration");
  4642. rc = -EFAULT;
  4643. goto end;
  4644. } else {
  4645. req = list_first_entry(&ctx->pending_req_list,
  4646. struct cam_ctx_request, list);
  4647. }
  4648. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  4649. if (!ctx_isp->hw_ctx) {
  4650. CAM_ERR(CAM_ISP, "Wrong hw context pointer.");
  4651. rc = -EFAULT;
  4652. goto end;
  4653. }
  4654. start_isp.hw_config.ctxt_to_hw_map = ctx_isp->hw_ctx;
  4655. start_isp.hw_config.request_id = req->request_id;
  4656. start_isp.hw_config.hw_update_entries = req_isp->cfg;
  4657. start_isp.hw_config.num_hw_update_entries = req_isp->num_cfg;
  4658. start_isp.hw_config.priv = &req_isp->hw_update_data;
  4659. start_isp.hw_config.init_packet = 1;
  4660. start_isp.hw_config.reapply = 0;
  4661. start_isp.hw_config.cdm_reset_before_apply = false;
  4662. ctx_isp->last_applied_req_id = req->request_id;
  4663. if (ctx->state == CAM_CTX_FLUSHED)
  4664. start_isp.start_only = true;
  4665. else
  4666. start_isp.start_only = false;
  4667. atomic_set(&ctx_isp->process_bubble, 0);
  4668. atomic_set(&ctx_isp->rxd_epoch, 0);
  4669. ctx_isp->frame_id = 0;
  4670. ctx_isp->active_req_cnt = 0;
  4671. ctx_isp->reported_req_id = 0;
  4672. ctx_isp->bubble_frame_cnt = 0;
  4673. ctx_isp->substate_activated = ctx_isp->rdi_only_context ?
  4674. CAM_ISP_CTX_ACTIVATED_APPLIED :
  4675. (req_isp->num_fence_map_out) ? CAM_ISP_CTX_ACTIVATED_EPOCH :
  4676. CAM_ISP_CTX_ACTIVATED_SOF;
  4677. atomic64_set(&ctx_isp->state_monitor_head, -1);
  4678. for (i = 0; i < CAM_ISP_CTX_EVENT_MAX; i++)
  4679. atomic64_set(&ctx_isp->event_record_head[i], -1);
  4680. /*
  4681. * In case of CSID TPG we might receive SOF and RUP IRQs
  4682. * before hw_mgr_intf->hw_start has returned. So move
  4683. * req out of pending list before hw_start and add it
  4684. * back to pending list if hw_start fails.
  4685. */
  4686. list_del_init(&req->list);
  4687. if (ctx_isp->offline_context && !req_isp->num_fence_map_out) {
  4688. list_add_tail(&req->list, &ctx->free_req_list);
  4689. atomic_set(&ctx_isp->rxd_epoch, 1);
  4690. CAM_DBG(CAM_REQ,
  4691. "Move pending req: %lld to free list(cnt: %d) offline ctx %u",
  4692. req->request_id, ctx_isp->active_req_cnt, ctx->ctx_id);
  4693. } else if (ctx_isp->rdi_only_context || !req_isp->num_fence_map_out) {
  4694. list_add_tail(&req->list, &ctx->wait_req_list);
  4695. CAM_DBG(CAM_REQ,
  4696. "Move pending req: %lld to wait list(cnt: %d) ctx %u",
  4697. req->request_id, ctx_isp->active_req_cnt, ctx->ctx_id);
  4698. } else {
  4699. list_add_tail(&req->list, &ctx->active_req_list);
  4700. ctx_isp->active_req_cnt++;
  4701. CAM_DBG(CAM_REQ,
  4702. "Move pending req: %lld to active list(cnt: %d) ctx %u offline %d",
  4703. req->request_id, ctx_isp->active_req_cnt, ctx->ctx_id,
  4704. ctx_isp->offline_context);
  4705. }
  4706. /*
  4707. * Only place to change state before calling the hw due to
  4708. * hardware tasklet has higher priority that can cause the
  4709. * irq handling comes early
  4710. */
  4711. ctx->state = CAM_CTX_ACTIVATED;
  4712. trace_cam_context_state("ISP", ctx);
  4713. rc = ctx->hw_mgr_intf->hw_start(ctx->hw_mgr_intf->hw_mgr_priv,
  4714. &start_isp);
  4715. if (rc) {
  4716. /* HW failure. user need to clean up the resource */
  4717. CAM_ERR(CAM_ISP, "Start HW failed");
  4718. ctx->state = CAM_CTX_READY;
  4719. if ((rc == -ETIMEDOUT) &&
  4720. (isp_ctx_debug.enable_cdm_cmd_buff_dump))
  4721. rc = cam_isp_ctx_dump_req(req_isp, 0, 0, NULL, false);
  4722. trace_cam_context_state("ISP", ctx);
  4723. list_del_init(&req->list);
  4724. list_add(&req->list, &ctx->pending_req_list);
  4725. goto end;
  4726. }
  4727. CAM_DBG(CAM_ISP, "start device success ctx %u", ctx->ctx_id);
  4728. end:
  4729. return rc;
  4730. }
  4731. static int __cam_isp_ctx_unlink_in_ready(struct cam_context *ctx,
  4732. struct cam_req_mgr_core_dev_link_setup *unlink)
  4733. {
  4734. int rc = 0;
  4735. ctx->link_hdl = -1;
  4736. ctx->ctx_crm_intf = NULL;
  4737. ctx->state = CAM_CTX_ACQUIRED;
  4738. trace_cam_context_state("ISP", ctx);
  4739. return rc;
  4740. }
  4741. static int __cam_isp_ctx_stop_dev_in_activated_unlock(
  4742. struct cam_context *ctx, struct cam_start_stop_dev_cmd *stop_cmd)
  4743. {
  4744. int rc = 0;
  4745. uint32_t i;
  4746. struct cam_hw_stop_args stop;
  4747. struct cam_ctx_request *req;
  4748. struct cam_isp_ctx_req *req_isp;
  4749. struct cam_isp_context *ctx_isp =
  4750. (struct cam_isp_context *) ctx->ctx_priv;
  4751. struct cam_isp_stop_args stop_isp;
  4752. /* Mask off all the incoming hardware events */
  4753. spin_lock_bh(&ctx->lock);
  4754. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_HALT;
  4755. spin_unlock_bh(&ctx->lock);
  4756. /* stop hw first */
  4757. if (ctx_isp->hw_ctx) {
  4758. stop.ctxt_to_hw_map = ctx_isp->hw_ctx;
  4759. stop_isp.hw_stop_cmd = CAM_ISP_HW_STOP_IMMEDIATELY;
  4760. stop_isp.stop_only = false;
  4761. stop.args = (void *) &stop_isp;
  4762. ctx->hw_mgr_intf->hw_stop(ctx->hw_mgr_intf->hw_mgr_priv,
  4763. &stop);
  4764. }
  4765. CAM_DBG(CAM_ISP, "next Substate[%s]",
  4766. __cam_isp_ctx_substate_val_to_type(
  4767. ctx_isp->substate_activated));
  4768. if (ctx->ctx_crm_intf &&
  4769. ctx->ctx_crm_intf->notify_stop) {
  4770. struct cam_req_mgr_notify_stop notify;
  4771. notify.link_hdl = ctx->link_hdl;
  4772. CAM_DBG(CAM_ISP,
  4773. "Notify CRM about device stop ctx %u link 0x%x",
  4774. ctx->ctx_id, ctx->link_hdl);
  4775. ctx->ctx_crm_intf->notify_stop(&notify);
  4776. } else
  4777. CAM_ERR(CAM_ISP, "cb not present");
  4778. while (!list_empty(&ctx->pending_req_list)) {
  4779. req = list_first_entry(&ctx->pending_req_list,
  4780. struct cam_ctx_request, list);
  4781. list_del_init(&req->list);
  4782. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  4783. CAM_DBG(CAM_ISP, "signal fence in pending list. fence num %d",
  4784. req_isp->num_fence_map_out);
  4785. for (i = 0; i < req_isp->num_fence_map_out; i++)
  4786. if (req_isp->fence_map_out[i].sync_id != -1) {
  4787. cam_sync_signal(
  4788. req_isp->fence_map_out[i].sync_id,
  4789. CAM_SYNC_STATE_SIGNALED_CANCEL,
  4790. CAM_SYNC_ISP_EVENT_HW_STOP);
  4791. }
  4792. list_add_tail(&req->list, &ctx->free_req_list);
  4793. }
  4794. while (!list_empty(&ctx->wait_req_list)) {
  4795. req = list_first_entry(&ctx->wait_req_list,
  4796. struct cam_ctx_request, list);
  4797. list_del_init(&req->list);
  4798. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  4799. CAM_DBG(CAM_ISP, "signal fence in wait list. fence num %d",
  4800. req_isp->num_fence_map_out);
  4801. for (i = 0; i < req_isp->num_fence_map_out; i++)
  4802. if (req_isp->fence_map_out[i].sync_id != -1) {
  4803. cam_sync_signal(
  4804. req_isp->fence_map_out[i].sync_id,
  4805. CAM_SYNC_STATE_SIGNALED_CANCEL,
  4806. CAM_SYNC_ISP_EVENT_HW_STOP);
  4807. }
  4808. list_add_tail(&req->list, &ctx->free_req_list);
  4809. }
  4810. while (!list_empty(&ctx->active_req_list)) {
  4811. req = list_first_entry(&ctx->active_req_list,
  4812. struct cam_ctx_request, list);
  4813. list_del_init(&req->list);
  4814. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  4815. CAM_DBG(CAM_ISP, "signal fence in active list. fence num %d",
  4816. req_isp->num_fence_map_out);
  4817. for (i = 0; i < req_isp->num_fence_map_out; i++)
  4818. if (req_isp->fence_map_out[i].sync_id != -1) {
  4819. cam_sync_signal(
  4820. req_isp->fence_map_out[i].sync_id,
  4821. CAM_SYNC_STATE_SIGNALED_CANCEL,
  4822. CAM_SYNC_ISP_EVENT_HW_STOP);
  4823. }
  4824. list_add_tail(&req->list, &ctx->free_req_list);
  4825. }
  4826. ctx_isp->frame_id = 0;
  4827. ctx_isp->active_req_cnt = 0;
  4828. ctx_isp->reported_req_id = 0;
  4829. ctx_isp->last_applied_req_id = 0;
  4830. ctx_isp->req_info.last_bufdone_req_id = 0;
  4831. ctx_isp->bubble_frame_cnt = 0;
  4832. atomic_set(&ctx_isp->process_bubble, 0);
  4833. atomic_set(&ctx_isp->rxd_epoch, 0);
  4834. atomic64_set(&ctx_isp->state_monitor_head, -1);
  4835. for (i = 0; i < CAM_ISP_CTX_EVENT_MAX; i++)
  4836. atomic64_set(&ctx_isp->event_record_head[i], -1);
  4837. CAM_DBG(CAM_ISP, "Stop device success next state %d on ctx %u",
  4838. ctx->state, ctx->ctx_id);
  4839. if (!stop_cmd) {
  4840. rc = __cam_isp_ctx_unlink_in_ready(ctx, NULL);
  4841. if (rc)
  4842. CAM_ERR(CAM_ISP, "Unlink failed rc=%d", rc);
  4843. }
  4844. return rc;
  4845. }
  4846. static int __cam_isp_ctx_stop_dev_in_activated(struct cam_context *ctx,
  4847. struct cam_start_stop_dev_cmd *cmd)
  4848. {
  4849. int rc = 0;
  4850. struct cam_isp_context *ctx_isp =
  4851. (struct cam_isp_context *)ctx->ctx_priv;
  4852. __cam_isp_ctx_stop_dev_in_activated_unlock(ctx, cmd);
  4853. ctx_isp->init_received = false;
  4854. ctx->state = CAM_CTX_ACQUIRED;
  4855. trace_cam_context_state("ISP", ctx);
  4856. return rc;
  4857. }
  4858. static int __cam_isp_ctx_release_dev_in_activated(struct cam_context *ctx,
  4859. struct cam_release_dev_cmd *cmd)
  4860. {
  4861. int rc = 0;
  4862. rc = __cam_isp_ctx_stop_dev_in_activated_unlock(ctx, NULL);
  4863. if (rc)
  4864. CAM_ERR(CAM_ISP, "Stop device failed rc=%d", rc);
  4865. rc = __cam_isp_ctx_release_dev_in_top_state(ctx, cmd);
  4866. if (rc)
  4867. CAM_ERR(CAM_ISP, "Release device failed rc=%d", rc);
  4868. return rc;
  4869. }
  4870. static int __cam_isp_ctx_release_hw_in_activated(struct cam_context *ctx,
  4871. void *cmd)
  4872. {
  4873. int rc = 0;
  4874. rc = __cam_isp_ctx_stop_dev_in_activated_unlock(ctx, NULL);
  4875. if (rc)
  4876. CAM_ERR(CAM_ISP, "Stop device failed rc=%d", rc);
  4877. rc = __cam_isp_ctx_release_hw_in_top_state(ctx, cmd);
  4878. if (rc)
  4879. CAM_ERR(CAM_ISP, "Release hw failed rc=%d", rc);
  4880. return rc;
  4881. }
  4882. static int __cam_isp_ctx_link_pause(struct cam_context *ctx)
  4883. {
  4884. int rc = 0;
  4885. struct cam_hw_cmd_args hw_cmd_args;
  4886. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  4887. struct cam_isp_context *ctx_isp =
  4888. (struct cam_isp_context *) ctx->ctx_priv;
  4889. hw_cmd_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  4890. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  4891. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_CMD_PAUSE_HW;
  4892. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  4893. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  4894. &hw_cmd_args);
  4895. return rc;
  4896. }
  4897. static int __cam_isp_ctx_link_resume(struct cam_context *ctx)
  4898. {
  4899. int rc = 0;
  4900. struct cam_hw_cmd_args hw_cmd_args;
  4901. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  4902. struct cam_isp_context *ctx_isp =
  4903. (struct cam_isp_context *) ctx->ctx_priv;
  4904. hw_cmd_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  4905. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  4906. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_CMD_RESUME_HW;
  4907. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  4908. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  4909. &hw_cmd_args);
  4910. return rc;
  4911. }
  4912. static int __cam_isp_ctx_handle_sof_freeze_evt(
  4913. struct cam_context *ctx)
  4914. {
  4915. int rc = 0;
  4916. struct cam_hw_cmd_args hw_cmd_args;
  4917. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  4918. struct cam_isp_context *ctx_isp =
  4919. (struct cam_isp_context *) ctx->ctx_priv;
  4920. hw_cmd_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  4921. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  4922. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_CMD_SOF_DEBUG;
  4923. isp_hw_cmd_args.u.sof_irq_enable = 1;
  4924. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  4925. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  4926. &hw_cmd_args);
  4927. return rc;
  4928. }
  4929. static int __cam_isp_ctx_process_evt(struct cam_context *ctx,
  4930. struct cam_req_mgr_link_evt_data *link_evt_data)
  4931. {
  4932. int rc = 0;
  4933. switch (link_evt_data->evt_type) {
  4934. case CAM_REQ_MGR_LINK_EVT_ERR:
  4935. /* No need to handle this message now */
  4936. break;
  4937. case CAM_REQ_MGR_LINK_EVT_PAUSE:
  4938. __cam_isp_ctx_link_pause(ctx);
  4939. break;
  4940. case CAM_REQ_MGR_LINK_EVT_RESUME:
  4941. __cam_isp_ctx_link_resume(ctx);
  4942. break;
  4943. case CAM_REQ_MGR_LINK_EVT_SOF_FREEZE:
  4944. __cam_isp_ctx_handle_sof_freeze_evt(ctx);
  4945. break;
  4946. default:
  4947. CAM_WARN(CAM_ISP, "Unknown event from CRM");
  4948. break;
  4949. }
  4950. return rc;
  4951. }
  4952. static int __cam_isp_ctx_unlink_in_activated(struct cam_context *ctx,
  4953. struct cam_req_mgr_core_dev_link_setup *unlink)
  4954. {
  4955. int rc = 0;
  4956. CAM_WARN(CAM_ISP,
  4957. "Received unlink in activated state. It's unexpected");
  4958. rc = __cam_isp_ctx_stop_dev_in_activated_unlock(ctx, NULL);
  4959. if (rc)
  4960. CAM_WARN(CAM_ISP, "Stop device failed rc=%d", rc);
  4961. rc = __cam_isp_ctx_unlink_in_ready(ctx, unlink);
  4962. if (rc)
  4963. CAM_ERR(CAM_ISP, "Unlink failed rc=%d", rc);
  4964. return rc;
  4965. }
  4966. static int __cam_isp_ctx_apply_req(struct cam_context *ctx,
  4967. struct cam_req_mgr_apply_request *apply)
  4968. {
  4969. int rc = 0;
  4970. struct cam_ctx_ops *ctx_ops = NULL;
  4971. struct cam_isp_context *ctx_isp =
  4972. (struct cam_isp_context *) ctx->ctx_priv;
  4973. trace_cam_apply_req("ISP", apply->request_id);
  4974. CAM_DBG(CAM_ISP, "Enter: apply req in Substate[%s] request_id:%lld",
  4975. __cam_isp_ctx_substate_val_to_type(
  4976. ctx_isp->substate_activated), apply->request_id);
  4977. ctx_ops = &ctx_isp->substate_machine[ctx_isp->substate_activated];
  4978. if (ctx_ops->crm_ops.apply_req) {
  4979. rc = ctx_ops->crm_ops.apply_req(ctx, apply);
  4980. } else {
  4981. CAM_WARN_RATE_LIMIT(CAM_ISP,
  4982. "No handle function in activated Substate[%s]",
  4983. __cam_isp_ctx_substate_val_to_type(
  4984. ctx_isp->substate_activated));
  4985. rc = -EFAULT;
  4986. }
  4987. if (rc)
  4988. CAM_WARN_RATE_LIMIT(CAM_ISP,
  4989. "Apply failed in active Substate[%s] rc %d",
  4990. __cam_isp_ctx_substate_val_to_type(
  4991. ctx_isp->substate_activated), rc);
  4992. return rc;
  4993. }
  4994. static int __cam_isp_ctx_handle_irq_in_activated(void *context,
  4995. uint32_t evt_id, void *evt_data)
  4996. {
  4997. int rc = 0;
  4998. struct cam_isp_ctx_irq_ops *irq_ops = NULL;
  4999. struct cam_context *ctx = (struct cam_context *)context;
  5000. struct cam_isp_context *ctx_isp =
  5001. (struct cam_isp_context *)ctx->ctx_priv;
  5002. spin_lock(&ctx->lock);
  5003. trace_cam_isp_activated_irq(ctx, ctx_isp->substate_activated, evt_id,
  5004. __cam_isp_ctx_get_event_ts(evt_id, evt_data));
  5005. CAM_DBG(CAM_ISP, "Enter: State %d, Substate[%s], evt id %d",
  5006. ctx->state, __cam_isp_ctx_substate_val_to_type(
  5007. ctx_isp->substate_activated), evt_id);
  5008. irq_ops = &ctx_isp->substate_machine_irq[ctx_isp->substate_activated];
  5009. if (irq_ops->irq_ops[evt_id]) {
  5010. rc = irq_ops->irq_ops[evt_id](ctx_isp, evt_data);
  5011. } else {
  5012. CAM_DBG(CAM_ISP, "No handle function for Substate[%s]",
  5013. __cam_isp_ctx_substate_val_to_type(
  5014. ctx_isp->substate_activated));
  5015. if (isp_ctx_debug.enable_state_monitor_dump)
  5016. __cam_isp_ctx_dump_state_monitor_array(ctx_isp);
  5017. }
  5018. CAM_DBG(CAM_ISP, "Exit: State %d Substate[%s]",
  5019. ctx->state, __cam_isp_ctx_substate_val_to_type(
  5020. ctx_isp->substate_activated));
  5021. spin_unlock(&ctx->lock);
  5022. return rc;
  5023. }
  5024. /* top state machine */
  5025. static struct cam_ctx_ops
  5026. cam_isp_ctx_top_state_machine[CAM_CTX_STATE_MAX] = {
  5027. /* Uninit */
  5028. {
  5029. .ioctl_ops = {},
  5030. .crm_ops = {},
  5031. .irq_ops = NULL,
  5032. },
  5033. /* Available */
  5034. {
  5035. .ioctl_ops = {
  5036. .acquire_dev = __cam_isp_ctx_acquire_dev_in_available,
  5037. },
  5038. .crm_ops = {},
  5039. .irq_ops = NULL,
  5040. },
  5041. /* Acquired */
  5042. {
  5043. .ioctl_ops = {
  5044. .acquire_hw = __cam_isp_ctx_acquire_hw_in_acquired,
  5045. .release_dev = __cam_isp_ctx_release_dev_in_top_state,
  5046. .config_dev = __cam_isp_ctx_config_dev_in_acquired,
  5047. .release_hw = __cam_isp_ctx_release_hw_in_top_state,
  5048. },
  5049. .crm_ops = {
  5050. .link = __cam_isp_ctx_link_in_acquired,
  5051. .unlink = __cam_isp_ctx_unlink_in_acquired,
  5052. .get_dev_info = __cam_isp_ctx_get_dev_info_in_acquired,
  5053. .flush_req = __cam_isp_ctx_flush_req_in_top_state,
  5054. .dump_req = __cam_isp_ctx_dump_in_top_state,
  5055. },
  5056. .irq_ops = NULL,
  5057. .pagefault_ops = cam_isp_context_dump_requests,
  5058. .dumpinfo_ops = cam_isp_context_info_dump,
  5059. },
  5060. /* Ready */
  5061. {
  5062. .ioctl_ops = {
  5063. .start_dev = __cam_isp_ctx_start_dev_in_ready,
  5064. .release_dev = __cam_isp_ctx_release_dev_in_top_state,
  5065. .config_dev = __cam_isp_ctx_config_dev_in_top_state,
  5066. .release_hw = __cam_isp_ctx_release_hw_in_top_state,
  5067. },
  5068. .crm_ops = {
  5069. .unlink = __cam_isp_ctx_unlink_in_ready,
  5070. .flush_req = __cam_isp_ctx_flush_req_in_ready,
  5071. .dump_req = __cam_isp_ctx_dump_in_top_state,
  5072. },
  5073. .irq_ops = NULL,
  5074. .pagefault_ops = cam_isp_context_dump_requests,
  5075. .dumpinfo_ops = cam_isp_context_info_dump,
  5076. },
  5077. /* Flushed */
  5078. {
  5079. .ioctl_ops = {
  5080. .stop_dev = __cam_isp_ctx_stop_dev_in_activated,
  5081. .release_dev = __cam_isp_ctx_release_dev_in_activated,
  5082. .config_dev = __cam_isp_ctx_config_dev_in_flushed,
  5083. .release_hw = __cam_isp_ctx_release_hw_in_activated,
  5084. },
  5085. .crm_ops = {
  5086. .unlink = __cam_isp_ctx_unlink_in_ready,
  5087. .process_evt = __cam_isp_ctx_process_evt,
  5088. },
  5089. .irq_ops = NULL,
  5090. .pagefault_ops = cam_isp_context_dump_requests,
  5091. .dumpinfo_ops = cam_isp_context_info_dump,
  5092. },
  5093. /* Activated */
  5094. {
  5095. .ioctl_ops = {
  5096. .stop_dev = __cam_isp_ctx_stop_dev_in_activated,
  5097. .release_dev = __cam_isp_ctx_release_dev_in_activated,
  5098. .config_dev = __cam_isp_ctx_config_dev_in_top_state,
  5099. .release_hw = __cam_isp_ctx_release_hw_in_activated,
  5100. },
  5101. .crm_ops = {
  5102. .unlink = __cam_isp_ctx_unlink_in_activated,
  5103. .apply_req = __cam_isp_ctx_apply_req,
  5104. .flush_req = __cam_isp_ctx_flush_req_in_top_state,
  5105. .process_evt = __cam_isp_ctx_process_evt,
  5106. .dump_req = __cam_isp_ctx_dump_in_top_state,
  5107. },
  5108. .irq_ops = __cam_isp_ctx_handle_irq_in_activated,
  5109. .pagefault_ops = cam_isp_context_dump_requests,
  5110. .dumpinfo_ops = cam_isp_context_info_dump,
  5111. },
  5112. };
  5113. static int cam_isp_context_dump_requests(void *data,
  5114. struct cam_smmu_pf_info *pf_info)
  5115. {
  5116. struct cam_context *ctx = (struct cam_context *)data;
  5117. struct cam_ctx_request *req = NULL;
  5118. struct cam_ctx_request *req_temp = NULL;
  5119. struct cam_isp_ctx_req *req_isp = NULL;
  5120. struct cam_isp_prepare_hw_update_data *hw_update_data = NULL;
  5121. struct cam_hw_mgr_dump_pf_data *pf_dbg_entry = NULL;
  5122. struct cam_req_mgr_message req_msg;
  5123. struct cam_isp_context *ctx_isp;
  5124. uint32_t resource_type = 0;
  5125. bool mem_found = false, ctx_found = false, send_error = false;
  5126. int rc = 0;
  5127. struct cam_isp_context *isp_ctx =
  5128. (struct cam_isp_context *)ctx->ctx_priv;
  5129. if (!isp_ctx) {
  5130. CAM_ERR(CAM_ISP, "Invalid isp ctx");
  5131. return -EINVAL;
  5132. }
  5133. CAM_INFO(CAM_ISP, "iommu fault handler for isp ctx %d state %d",
  5134. ctx->ctx_id, ctx->state);
  5135. list_for_each_entry_safe(req, req_temp,
  5136. &ctx->active_req_list, list) {
  5137. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  5138. hw_update_data = &req_isp->hw_update_data;
  5139. pf_dbg_entry = &(req->pf_data);
  5140. CAM_INFO(CAM_ISP, "Active List: req_id : %lld ",
  5141. req->request_id);
  5142. rc = cam_context_dump_pf_info_to_hw(ctx, pf_dbg_entry->packet,
  5143. &mem_found, &ctx_found, &resource_type, pf_info);
  5144. if (rc)
  5145. CAM_ERR(CAM_ISP, "Failed to dump pf info");
  5146. if (ctx_found)
  5147. send_error = true;
  5148. }
  5149. CAM_INFO(CAM_ISP, "Iterating over wait_list of isp ctx %d state %d",
  5150. ctx->ctx_id, ctx->state);
  5151. list_for_each_entry_safe(req, req_temp,
  5152. &ctx->wait_req_list, list) {
  5153. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  5154. hw_update_data = &req_isp->hw_update_data;
  5155. pf_dbg_entry = &(req->pf_data);
  5156. CAM_INFO(CAM_ISP, "Wait List: req_id : %lld ", req->request_id);
  5157. rc = cam_context_dump_pf_info_to_hw(ctx, pf_dbg_entry->packet,
  5158. &mem_found, &ctx_found, &resource_type, pf_info);
  5159. if (rc)
  5160. CAM_ERR(CAM_ISP, "Failed to dump pf info");
  5161. if (ctx_found)
  5162. send_error = true;
  5163. }
  5164. /*
  5165. * In certain scenarios we observe both overflow and SMMU pagefault
  5166. * for a particular request. If overflow is handled before page fault
  5167. * we need to traverse through pending request list because if
  5168. * bubble recovery is enabled on any request we move that request
  5169. * and all the subsequent requests to the pending list while handling
  5170. * overflow error.
  5171. */
  5172. CAM_INFO(CAM_ISP,
  5173. "Iterating over pending req list of isp ctx %d state %d",
  5174. ctx->ctx_id, ctx->state);
  5175. list_for_each_entry_safe(req, req_temp,
  5176. &ctx->pending_req_list, list) {
  5177. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  5178. hw_update_data = &req_isp->hw_update_data;
  5179. pf_dbg_entry = &(req->pf_data);
  5180. CAM_INFO(CAM_ISP, "Pending List: req_id : %lld ",
  5181. req->request_id);
  5182. rc = cam_context_dump_pf_info_to_hw(ctx, pf_dbg_entry->packet,
  5183. &mem_found, &ctx_found, &resource_type, pf_info);
  5184. if (rc)
  5185. CAM_ERR(CAM_ISP, "Failed to dump pf info");
  5186. if (ctx_found)
  5187. send_error = true;
  5188. }
  5189. if (resource_type) {
  5190. ctx_isp = (struct cam_isp_context *) ctx->ctx_priv;
  5191. if (ctx_isp->isp_device_type == CAM_IFE_DEVICE_TYPE)
  5192. CAM_ERR(CAM_ISP,
  5193. "Page fault on resource id:%s (0x%x) ctx id:%d frame id:%d reported id:%lld applied id:%lld",
  5194. __cam_isp_resource_handle_id_to_type(
  5195. resource_type),
  5196. resource_type, ctx->ctx_id, ctx_isp->frame_id,
  5197. ctx_isp->reported_req_id,
  5198. ctx_isp->last_applied_req_id);
  5199. else
  5200. CAM_ERR(CAM_ISP,
  5201. "Page fault on resource id:%s (0x%x) ctx id:%d frame id:%d reported id:%lld applied id:%lld",
  5202. __cam_isp_tfe_resource_handle_id_to_type(
  5203. resource_type),
  5204. resource_type, ctx->ctx_id, ctx_isp->frame_id,
  5205. ctx_isp->reported_req_id,
  5206. ctx_isp->last_applied_req_id);
  5207. }
  5208. if (send_error) {
  5209. CAM_INFO(CAM_ISP,
  5210. "page fault notifying to umd ctx %u session_hdl:%d device_hdl:%d link_hdl:%d",
  5211. ctx->ctx_id, ctx->session_hdl,
  5212. ctx->dev_hdl, ctx->link_hdl);
  5213. req_msg.session_hdl = ctx->session_hdl;
  5214. req_msg.u.err_msg.device_hdl = ctx->dev_hdl;
  5215. req_msg.u.err_msg.error_type =
  5216. CAM_REQ_MGR_ERROR_TYPE_PAGE_FAULT;
  5217. req_msg.u.err_msg.link_hdl = ctx->link_hdl;
  5218. req_msg.u.err_msg.request_id = 0;
  5219. req_msg.u.err_msg.resource_size = 0x0;
  5220. if (cam_req_mgr_notify_message(&req_msg,
  5221. V4L_EVENT_CAM_REQ_MGR_ERROR,
  5222. V4L_EVENT_CAM_REQ_MGR_EVENT))
  5223. CAM_ERR(CAM_ISP,
  5224. "could not send page fault notification ctx %u session_hdl:%d device_hdl:%d link_hdl:%d",
  5225. ctx->ctx_id, ctx->session_hdl,
  5226. ctx->dev_hdl, ctx->link_hdl);
  5227. }
  5228. return rc;
  5229. }
  5230. static int cam_isp_context_debug_register(void)
  5231. {
  5232. int rc = 0;
  5233. struct dentry *dbgfileptr = NULL;
  5234. dbgfileptr = debugfs_create_dir("camera_isp_ctx", NULL);
  5235. if (!dbgfileptr) {
  5236. CAM_ERR(CAM_ISP, "DebugFS could not create directory!");
  5237. rc = -ENOENT;
  5238. goto end;
  5239. }
  5240. /* Store parent inode for cleanup in caller */
  5241. isp_ctx_debug.dentry = dbgfileptr;
  5242. dbgfileptr = debugfs_create_u32("enable_state_monitor_dump", 0644,
  5243. isp_ctx_debug.dentry, &isp_ctx_debug.enable_state_monitor_dump);
  5244. dbgfileptr = debugfs_create_u8("enable_cdm_cmd_buffer_dump", 0644,
  5245. isp_ctx_debug.dentry, &isp_ctx_debug.enable_cdm_cmd_buff_dump);
  5246. if (IS_ERR(dbgfileptr)) {
  5247. if (PTR_ERR(dbgfileptr) == -ENODEV)
  5248. CAM_WARN(CAM_ISP, "DebugFS not enabled in kernel!");
  5249. else
  5250. rc = PTR_ERR(dbgfileptr);
  5251. }
  5252. end:
  5253. return rc;
  5254. }
  5255. int cam_isp_context_init(struct cam_isp_context *ctx,
  5256. struct cam_context *ctx_base,
  5257. struct cam_req_mgr_kmd_ops *crm_node_intf,
  5258. struct cam_hw_mgr_intf *hw_intf,
  5259. uint32_t ctx_id,
  5260. uint32_t isp_device_type)
  5261. {
  5262. int rc = -1;
  5263. int i;
  5264. if (!ctx || !ctx_base) {
  5265. CAM_ERR(CAM_ISP, "Invalid Context");
  5266. goto err;
  5267. }
  5268. /* ISP context setup */
  5269. memset(ctx, 0, sizeof(*ctx));
  5270. ctx->base = ctx_base;
  5271. ctx->frame_id = 0;
  5272. ctx->custom_enabled = false;
  5273. ctx->use_frame_header_ts = false;
  5274. ctx->active_req_cnt = 0;
  5275. ctx->reported_req_id = 0;
  5276. ctx->bubble_frame_cnt = 0;
  5277. ctx->req_info.last_bufdone_req_id = 0;
  5278. ctx->hw_ctx = NULL;
  5279. ctx->substate_activated = CAM_ISP_CTX_ACTIVATED_SOF;
  5280. ctx->substate_machine = cam_isp_ctx_activated_state_machine;
  5281. ctx->substate_machine_irq = cam_isp_ctx_activated_state_machine_irq;
  5282. ctx->init_timestamp = jiffies_to_msecs(jiffies);
  5283. ctx->isp_device_type = isp_device_type;
  5284. for (i = 0; i < CAM_ISP_CTX_REQ_MAX; i++) {
  5285. ctx->req_base[i].req_priv = &ctx->req_isp[i];
  5286. ctx->req_isp[i].base = &ctx->req_base[i];
  5287. }
  5288. /* camera context setup */
  5289. rc = cam_context_init(ctx_base, isp_dev_name, CAM_ISP, ctx_id,
  5290. crm_node_intf, hw_intf, ctx->req_base, CAM_ISP_CTX_REQ_MAX);
  5291. if (rc) {
  5292. CAM_ERR(CAM_ISP, "Camera Context Base init failed");
  5293. goto err;
  5294. }
  5295. /* link camera context with isp context */
  5296. ctx_base->state_machine = cam_isp_ctx_top_state_machine;
  5297. ctx_base->ctx_priv = ctx;
  5298. /* initializing current state for error logging */
  5299. for (i = 0; i < CAM_ISP_CTX_STATE_MONITOR_MAX_ENTRIES; i++) {
  5300. ctx->cam_isp_ctx_state_monitor[i].curr_state =
  5301. CAM_ISP_CTX_ACTIVATED_MAX;
  5302. }
  5303. atomic64_set(&ctx->state_monitor_head, -1);
  5304. for (i = 0; i < CAM_ISP_CTX_EVENT_MAX; i++)
  5305. atomic64_set(&ctx->event_record_head[i], -1);
  5306. if (!isp_ctx_debug.dentry)
  5307. cam_isp_context_debug_register();
  5308. err:
  5309. return rc;
  5310. }
  5311. int cam_isp_context_deinit(struct cam_isp_context *ctx)
  5312. {
  5313. if (ctx->base)
  5314. cam_context_deinit(ctx->base);
  5315. if (ctx->substate_activated != CAM_ISP_CTX_ACTIVATED_SOF)
  5316. CAM_ERR(CAM_ISP, "ISP context Substate[%s] is invalid",
  5317. __cam_isp_ctx_substate_val_to_type(
  5318. ctx->substate_activated));
  5319. debugfs_remove_recursive(isp_ctx_debug.dentry);
  5320. isp_ctx_debug.dentry = NULL;
  5321. memset(ctx, 0, sizeof(*ctx));
  5322. return 0;
  5323. }