cam_isp_context.c 160 KB

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