cam_isp_context.c 158 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674
  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_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_ERR(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_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_ERR(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. struct cam_context *ctx = NULL;
  1169. struct cam_isp_context *ctx_isp = priv;
  1170. struct cam_ctx_request *req;
  1171. struct cam_isp_ctx_req *req_isp;
  1172. struct cam_hw_config_args cfg;
  1173. if (!ctx_isp) {
  1174. CAM_ERR(CAM_ISP, "Invalid ctx_isp:%pK", ctx);
  1175. rc = -EINVAL;
  1176. goto end;
  1177. }
  1178. ctx = ctx_isp->base;
  1179. if (list_empty(&ctx->pending_req_list)) {
  1180. CAM_DBG(CAM_ISP, "No pending requests to apply");
  1181. rc = -EFAULT;
  1182. goto end;
  1183. }
  1184. if ((ctx->state != CAM_CTX_ACTIVATED) ||
  1185. (!atomic_read(&ctx_isp->rxd_epoch)) ||
  1186. (ctx_isp->substate_activated == CAM_ISP_CTX_ACTIVATED_APPLIED))
  1187. goto end;
  1188. if (ctx_isp->active_req_cnt >= 2)
  1189. goto end;
  1190. spin_lock_bh(&ctx->lock);
  1191. req = list_first_entry(&ctx->pending_req_list, struct cam_ctx_request,
  1192. list);
  1193. spin_unlock_bh(&ctx->lock);
  1194. CAM_DBG(CAM_REQ, "Apply request %lld in substate %d ctx %u",
  1195. req->request_id, ctx_isp->substate_activated, ctx->ctx_id);
  1196. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  1197. memset(&cfg, 0, sizeof(cfg));
  1198. cfg.ctxt_to_hw_map = ctx_isp->hw_ctx;
  1199. cfg.request_id = req->request_id;
  1200. cfg.hw_update_entries = req_isp->cfg;
  1201. cfg.num_hw_update_entries = req_isp->num_cfg;
  1202. cfg.priv = &req_isp->hw_update_data;
  1203. cfg.init_packet = 0;
  1204. rc = ctx->hw_mgr_intf->hw_config(ctx->hw_mgr_intf->hw_mgr_priv, &cfg);
  1205. if (rc) {
  1206. CAM_ERR_RATE_LIMIT(CAM_ISP, "Can not apply the configuration");
  1207. } else {
  1208. spin_lock_bh(&ctx->lock);
  1209. atomic_set(&ctx_isp->rxd_epoch, 0);
  1210. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_APPLIED;
  1211. ctx_isp->last_applied_req_id = req->request_id;
  1212. list_del_init(&req->list);
  1213. list_add_tail(&req->list, &ctx->wait_req_list);
  1214. spin_unlock_bh(&ctx->lock);
  1215. CAM_DBG(CAM_ISP, "New substate state %d, applied req %lld",
  1216. CAM_ISP_CTX_ACTIVATED_APPLIED,
  1217. ctx_isp->last_applied_req_id);
  1218. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  1219. CAM_ISP_STATE_CHANGE_TRIGGER_APPLIED,
  1220. req->request_id);
  1221. }
  1222. end:
  1223. return rc;
  1224. }
  1225. static int __cam_isp_ctx_schedule_apply_req_offline(
  1226. struct cam_isp_context *ctx_isp)
  1227. {
  1228. int rc = 0;
  1229. struct crm_workq_task *task;
  1230. task = cam_req_mgr_workq_get_task(ctx_isp->workq);
  1231. if (!task) {
  1232. CAM_ERR(CAM_ISP, "No task for worker");
  1233. return -ENOMEM;
  1234. }
  1235. task->process_cb = __cam_isp_ctx_apply_req_offline;
  1236. rc = cam_req_mgr_workq_enqueue_task(task, ctx_isp, CRM_TASK_PRIORITY_0);
  1237. if (rc)
  1238. CAM_ERR(CAM_ISP, "Failed to schedule task rc:%d", rc);
  1239. return rc;
  1240. }
  1241. static int __cam_isp_ctx_offline_epoch_in_activated_state(
  1242. struct cam_isp_context *ctx_isp, void *evt_data)
  1243. {
  1244. struct cam_context *ctx = ctx_isp->base;
  1245. struct cam_ctx_request *req, *req_temp;
  1246. uint64_t request_id = 0;
  1247. atomic_set(&ctx_isp->rxd_epoch, 1);
  1248. CAM_DBG(CAM_ISP, "SOF frame %lld ctx %u", ctx_isp->frame_id,
  1249. ctx->ctx_id);
  1250. list_for_each_entry_safe(req, req_temp, &ctx->active_req_list, list) {
  1251. if (req->request_id > ctx_isp->reported_req_id) {
  1252. request_id = req->request_id;
  1253. ctx_isp->reported_req_id = request_id;
  1254. break;
  1255. }
  1256. }
  1257. __cam_isp_ctx_schedule_apply_req_offline(ctx_isp);
  1258. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  1259. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  1260. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  1261. CAM_ISP_STATE_CHANGE_TRIGGER_EPOCH,
  1262. request_id);
  1263. return 0;
  1264. }
  1265. static int __cam_isp_ctx_reg_upd_in_epoch_bubble_state(
  1266. struct cam_isp_context *ctx_isp, void *evt_data)
  1267. {
  1268. if (ctx_isp->frame_id == 1)
  1269. CAM_DBG(CAM_ISP, "Reg update in Substate[%s] for early PCR",
  1270. __cam_isp_ctx_substate_val_to_type(
  1271. ctx_isp->substate_activated));
  1272. else
  1273. CAM_WARN(CAM_ISP,
  1274. "Unexpected reg update in activated Substate[%s] for frame_id:%lld",
  1275. __cam_isp_ctx_substate_val_to_type(
  1276. ctx_isp->substate_activated),
  1277. ctx_isp->frame_id);
  1278. return 0;
  1279. }
  1280. static int __cam_isp_ctx_reg_upd_in_applied_state(
  1281. struct cam_isp_context *ctx_isp, void *evt_data)
  1282. {
  1283. int rc = 0;
  1284. struct cam_ctx_request *req;
  1285. struct cam_context *ctx = ctx_isp->base;
  1286. struct cam_isp_ctx_req *req_isp;
  1287. uint64_t request_id = 0;
  1288. if (list_empty(&ctx->wait_req_list)) {
  1289. CAM_ERR(CAM_ISP, "Reg upd ack with no waiting request");
  1290. goto end;
  1291. }
  1292. req = list_first_entry(&ctx->wait_req_list,
  1293. struct cam_ctx_request, list);
  1294. list_del_init(&req->list);
  1295. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  1296. if (req_isp->num_fence_map_out != 0) {
  1297. list_add_tail(&req->list, &ctx->active_req_list);
  1298. ctx_isp->active_req_cnt++;
  1299. request_id = req->request_id;
  1300. CAM_DBG(CAM_REQ,
  1301. "move request %lld to active list(cnt = %d), ctx %u",
  1302. req->request_id, ctx_isp->active_req_cnt, ctx->ctx_id);
  1303. __cam_isp_ctx_update_event_record(ctx_isp,
  1304. CAM_ISP_CTX_EVENT_RUP, req);
  1305. } else {
  1306. /* no io config, so the request is completed. */
  1307. list_add_tail(&req->list, &ctx->free_req_list);
  1308. CAM_DBG(CAM_ISP,
  1309. "move active request %lld to free list(cnt = %d), ctx %u",
  1310. req->request_id, ctx_isp->active_req_cnt, ctx->ctx_id);
  1311. }
  1312. /*
  1313. * This function only called directly from applied and bubble applied
  1314. * state so change substate here.
  1315. */
  1316. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_EPOCH;
  1317. CAM_DBG(CAM_ISP, "next Substate[%s]",
  1318. __cam_isp_ctx_substate_val_to_type(
  1319. ctx_isp->substate_activated));
  1320. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  1321. CAM_ISP_STATE_CHANGE_TRIGGER_REG_UPDATE, request_id);
  1322. end:
  1323. return rc;
  1324. }
  1325. static int __cam_isp_ctx_notify_sof_in_activated_state(
  1326. struct cam_isp_context *ctx_isp, void *evt_data)
  1327. {
  1328. int rc = 0;
  1329. uint64_t request_id = 0;
  1330. struct cam_req_mgr_trigger_notify notify;
  1331. struct cam_context *ctx = ctx_isp->base;
  1332. struct cam_ctx_request *req;
  1333. struct cam_isp_ctx_req *req_isp;
  1334. struct cam_isp_hw_epoch_event_data *epoch_done_event_data =
  1335. (struct cam_isp_hw_epoch_event_data *)evt_data;
  1336. if (!evt_data) {
  1337. CAM_ERR(CAM_ISP, "invalid event data");
  1338. return -EINVAL;
  1339. }
  1340. ctx_isp->frame_id_meta = epoch_done_event_data->frame_id_meta;
  1341. /*
  1342. * notify reqmgr with sof signal. Note, due to scheduling delay
  1343. * we can run into situation that two active requests has already
  1344. * be in the active queue while we try to do the notification.
  1345. * In this case, we need to skip the current notification. This
  1346. * helps the state machine to catch up the delay.
  1347. */
  1348. if (ctx->ctx_crm_intf && ctx->ctx_crm_intf->notify_trigger &&
  1349. ctx_isp->active_req_cnt <= 2) {
  1350. if (ctx_isp->subscribe_event & CAM_TRIGGER_POINT_SOF) {
  1351. notify.link_hdl = ctx->link_hdl;
  1352. notify.dev_hdl = ctx->dev_hdl;
  1353. notify.frame_id = ctx_isp->frame_id;
  1354. notify.trigger = CAM_TRIGGER_POINT_SOF;
  1355. notify.req_id = ctx_isp->req_info.last_bufdone_req_id;
  1356. notify.sof_timestamp_val = ctx_isp->sof_timestamp_val;
  1357. notify.trigger_id = ctx_isp->trigger_id;
  1358. ctx->ctx_crm_intf->notify_trigger(&notify);
  1359. CAM_DBG(CAM_ISP, "Notify CRM SOF frame %lld ctx %u",
  1360. ctx_isp->frame_id, ctx->ctx_id);
  1361. }
  1362. list_for_each_entry(req, &ctx->active_req_list, list) {
  1363. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  1364. if ((!req_isp->bubble_detected) &&
  1365. (req->request_id > ctx_isp->reported_req_id)) {
  1366. request_id = req->request_id;
  1367. ctx_isp->reported_req_id = request_id;
  1368. __cam_isp_ctx_update_event_record(ctx_isp,
  1369. CAM_ISP_CTX_EVENT_EPOCH, req);
  1370. break;
  1371. }
  1372. }
  1373. if (ctx_isp->substate_activated == CAM_ISP_CTX_ACTIVATED_BUBBLE)
  1374. request_id = 0;
  1375. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  1376. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  1377. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  1378. CAM_ISP_STATE_CHANGE_TRIGGER_EPOCH,
  1379. request_id);
  1380. } else {
  1381. CAM_ERR_RATE_LIMIT(CAM_ISP,
  1382. "Can not notify SOF to CRM for ctx %u",
  1383. ctx->ctx_id);
  1384. rc = -EFAULT;
  1385. }
  1386. return 0;
  1387. }
  1388. static int __cam_isp_ctx_notify_eof_in_activated_state(
  1389. struct cam_isp_context *ctx_isp, void *evt_data)
  1390. {
  1391. int rc = 0;
  1392. struct cam_req_mgr_trigger_notify notify;
  1393. struct cam_context *ctx = ctx_isp->base;
  1394. if (!(ctx_isp->subscribe_event & CAM_TRIGGER_POINT_EOF))
  1395. return rc;
  1396. /* notify reqmgr with eof signal */
  1397. if (ctx->ctx_crm_intf && ctx->ctx_crm_intf->notify_trigger) {
  1398. notify.link_hdl = ctx->link_hdl;
  1399. notify.dev_hdl = ctx->dev_hdl;
  1400. notify.frame_id = ctx_isp->frame_id;
  1401. notify.trigger = CAM_TRIGGER_POINT_EOF;
  1402. ctx->ctx_crm_intf->notify_trigger(&notify);
  1403. CAM_DBG(CAM_ISP, "Notify CRM EOF frame %lld ctx %u",
  1404. ctx_isp->frame_id, ctx->ctx_id);
  1405. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  1406. CAM_ISP_STATE_CHANGE_TRIGGER_EOF, 0);
  1407. } else {
  1408. CAM_ERR(CAM_ISP, "Can not notify EOF to CRM for ctx %u",
  1409. ctx->ctx_id);
  1410. rc = -EFAULT;
  1411. }
  1412. return rc;
  1413. }
  1414. static int __cam_isp_ctx_reg_upd_in_hw_error(
  1415. struct cam_isp_context *ctx_isp, void *evt_data)
  1416. {
  1417. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_SOF;
  1418. return 0;
  1419. }
  1420. static int __cam_isp_ctx_sof_in_activated_state(
  1421. struct cam_isp_context *ctx_isp, void *evt_data)
  1422. {
  1423. int rc = 0;
  1424. struct cam_isp_hw_sof_event_data *sof_event_data = evt_data;
  1425. struct cam_ctx_request *req = NULL;
  1426. struct cam_context *ctx = ctx_isp->base;
  1427. uint64_t request_id = 0;
  1428. /* First check if there is a valid request in active list */
  1429. list_for_each_entry(req, &ctx->active_req_list, list) {
  1430. if (req->request_id > ctx_isp->reported_req_id) {
  1431. request_id = req->request_id;
  1432. break;
  1433. }
  1434. }
  1435. /*
  1436. * If nothing in active list, current request might have not moved
  1437. * from wait to active list. This could happen if REG_UPDATE to sw
  1438. * is coming immediately after SOF
  1439. */
  1440. if (request_id == 0) {
  1441. req = list_first_entry(&ctx->wait_req_list,
  1442. struct cam_ctx_request, list);
  1443. if (req)
  1444. request_id = req->request_id;
  1445. }
  1446. if (!evt_data) {
  1447. CAM_ERR(CAM_ISP, "in valid sof event data");
  1448. return -EINVAL;
  1449. }
  1450. ctx_isp->frame_id++;
  1451. ctx_isp->sof_timestamp_val = sof_event_data->timestamp;
  1452. ctx_isp->boot_timestamp = sof_event_data->boot_time;
  1453. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  1454. CAM_ISP_STATE_CHANGE_TRIGGER_SOF, request_id);
  1455. CAM_DBG(CAM_ISP, "frame id: %lld time stamp:0x%llx, ctx %u",
  1456. ctx_isp->frame_id, ctx_isp->sof_timestamp_val, ctx->ctx_id);
  1457. return rc;
  1458. }
  1459. static int __cam_isp_ctx_reg_upd_in_sof(struct cam_isp_context *ctx_isp,
  1460. void *evt_data)
  1461. {
  1462. int rc = 0;
  1463. struct cam_ctx_request *req = NULL;
  1464. struct cam_isp_ctx_req *req_isp;
  1465. struct cam_context *ctx = ctx_isp->base;
  1466. if (ctx->state != CAM_CTX_ACTIVATED && ctx_isp->frame_id > 1) {
  1467. CAM_DBG(CAM_ISP, "invalid RUP");
  1468. goto end;
  1469. }
  1470. /*
  1471. * This is for the first update. The initial setting will
  1472. * cause the reg_upd in the first frame.
  1473. */
  1474. if (!list_empty(&ctx->wait_req_list)) {
  1475. req = list_first_entry(&ctx->wait_req_list,
  1476. struct cam_ctx_request, list);
  1477. list_del_init(&req->list);
  1478. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  1479. if (req_isp->num_fence_map_out == req_isp->num_acked)
  1480. list_add_tail(&req->list, &ctx->free_req_list);
  1481. else
  1482. CAM_ERR(CAM_ISP,
  1483. "receive rup in unexpected state");
  1484. }
  1485. if (req != NULL) {
  1486. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  1487. CAM_ISP_STATE_CHANGE_TRIGGER_REG_UPDATE,
  1488. req->request_id);
  1489. }
  1490. end:
  1491. return rc;
  1492. }
  1493. static int __cam_isp_ctx_epoch_in_applied(struct cam_isp_context *ctx_isp,
  1494. void *evt_data)
  1495. {
  1496. uint64_t request_id = 0;
  1497. struct cam_ctx_request *req;
  1498. struct cam_isp_ctx_req *req_isp;
  1499. struct cam_context *ctx = ctx_isp->base;
  1500. struct cam_isp_hw_epoch_event_data *epoch_done_event_data =
  1501. (struct cam_isp_hw_epoch_event_data *)evt_data;
  1502. if (!evt_data) {
  1503. CAM_ERR(CAM_ISP, "invalid event data");
  1504. return -EINVAL;
  1505. }
  1506. ctx_isp->frame_id_meta = epoch_done_event_data->frame_id_meta;
  1507. if (list_empty(&ctx->wait_req_list)) {
  1508. /*
  1509. * If no wait req in epoch, this is an error case.
  1510. * The recovery is to go back to sof state
  1511. */
  1512. CAM_ERR(CAM_ISP, "Ctx:%d No wait request", ctx->ctx_id);
  1513. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_SOF;
  1514. /* Send SOF event as empty frame*/
  1515. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  1516. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  1517. __cam_isp_ctx_update_event_record(ctx_isp,
  1518. CAM_ISP_CTX_EVENT_EPOCH, NULL);
  1519. goto end;
  1520. }
  1521. req = list_first_entry(&ctx->wait_req_list, struct cam_ctx_request,
  1522. list);
  1523. req_isp = (struct cam_isp_ctx_req *)req->req_priv;
  1524. req_isp->bubble_detected = true;
  1525. req_isp->reapply = true;
  1526. CAM_INFO(CAM_ISP, "ctx:%d Report Bubble flag %d req id:%lld",
  1527. ctx->ctx_id, req_isp->bubble_report, req->request_id);
  1528. if (req_isp->bubble_report && ctx->ctx_crm_intf &&
  1529. ctx->ctx_crm_intf->notify_err) {
  1530. struct cam_req_mgr_error_notify notify;
  1531. notify.link_hdl = ctx->link_hdl;
  1532. notify.dev_hdl = ctx->dev_hdl;
  1533. notify.req_id = req->request_id;
  1534. notify.error = CRM_KMD_ERR_BUBBLE;
  1535. notify.trigger = 0;
  1536. if (ctx_isp->subscribe_event & CAM_TRIGGER_POINT_SOF)
  1537. notify.trigger = CAM_TRIGGER_POINT_SOF;
  1538. notify.frame_id = ctx_isp->frame_id;
  1539. notify.sof_timestamp_val = ctx_isp->sof_timestamp_val;
  1540. CAM_WARN(CAM_ISP,
  1541. "Notify CRM about Bubble req %lld frame %lld, ctx %u",
  1542. req->request_id, ctx_isp->frame_id, ctx->ctx_id);
  1543. trace_cam_log_event("Bubble", "Rcvd epoch in applied state",
  1544. req->request_id, ctx->ctx_id);
  1545. ctx->ctx_crm_intf->notify_err(&notify);
  1546. atomic_set(&ctx_isp->process_bubble, 1);
  1547. } else {
  1548. req_isp->bubble_report = 0;
  1549. }
  1550. /*
  1551. * Always move the request to active list. Let buf done
  1552. * function handles the rest.
  1553. */
  1554. list_del_init(&req->list);
  1555. list_add_tail(&req->list, &ctx->active_req_list);
  1556. ctx_isp->active_req_cnt++;
  1557. CAM_DBG(CAM_REQ, "move request %lld to active list(cnt = %d), ctx %u",
  1558. req->request_id, ctx_isp->active_req_cnt, ctx->ctx_id);
  1559. if ((req->request_id > ctx_isp->reported_req_id)
  1560. && !req_isp->bubble_report) {
  1561. request_id = req->request_id;
  1562. ctx_isp->reported_req_id = request_id;
  1563. }
  1564. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  1565. CAM_REQ_MGR_SOF_EVENT_ERROR);
  1566. __cam_isp_ctx_update_event_record(ctx_isp,
  1567. CAM_ISP_CTX_EVENT_EPOCH, req);
  1568. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_BUBBLE;
  1569. CAM_DBG(CAM_ISP, "next Substate[%s]",
  1570. __cam_isp_ctx_substate_val_to_type(
  1571. ctx_isp->substate_activated));
  1572. end:
  1573. if (request_id == 0) {
  1574. req = list_last_entry(&ctx->active_req_list,
  1575. struct cam_ctx_request, list);
  1576. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  1577. CAM_ISP_STATE_CHANGE_TRIGGER_EPOCH, req->request_id);
  1578. } else {
  1579. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  1580. CAM_ISP_STATE_CHANGE_TRIGGER_EPOCH, request_id);
  1581. }
  1582. return 0;
  1583. }
  1584. static int __cam_isp_ctx_buf_done_in_applied(struct cam_isp_context *ctx_isp,
  1585. void *evt_data)
  1586. {
  1587. int rc = 0;
  1588. struct cam_isp_hw_done_event_data *done =
  1589. (struct cam_isp_hw_done_event_data *) evt_data;
  1590. rc = __cam_isp_ctx_handle_buf_done_in_activated_state(ctx_isp, done, 0);
  1591. return rc;
  1592. }
  1593. static int __cam_isp_ctx_sof_in_epoch(struct cam_isp_context *ctx_isp,
  1594. void *evt_data)
  1595. {
  1596. int rc = 0;
  1597. struct cam_context *ctx = ctx_isp->base;
  1598. struct cam_isp_hw_sof_event_data *sof_event_data = evt_data;
  1599. struct cam_ctx_request *req;
  1600. if (!evt_data) {
  1601. CAM_ERR(CAM_ISP, "in valid sof event data");
  1602. return -EINVAL;
  1603. }
  1604. ctx_isp->frame_id++;
  1605. ctx_isp->sof_timestamp_val = sof_event_data->timestamp;
  1606. ctx_isp->boot_timestamp = sof_event_data->boot_time;
  1607. if (list_empty(&ctx->active_req_list))
  1608. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_SOF;
  1609. else
  1610. CAM_DBG(CAM_ISP, "Still need to wait for the buf done");
  1611. req = list_last_entry(&ctx->active_req_list,
  1612. struct cam_ctx_request, list);
  1613. if (req)
  1614. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  1615. CAM_ISP_STATE_CHANGE_TRIGGER_SOF,
  1616. req->request_id);
  1617. if (ctx_isp->frame_id == 1)
  1618. CAM_INFO(CAM_ISP,
  1619. "First SOF in EPCR ctx:%d frame_id:%lld next substate %s",
  1620. ctx->ctx_id, ctx_isp->frame_id,
  1621. __cam_isp_ctx_substate_val_to_type(
  1622. ctx_isp->substate_activated));
  1623. CAM_DBG(CAM_ISP, "SOF in epoch ctx:%d frame_id:%lld next substate:%s",
  1624. ctx->ctx_id, ctx_isp->frame_id,
  1625. __cam_isp_ctx_substate_val_to_type(
  1626. ctx_isp->substate_activated));
  1627. return rc;
  1628. }
  1629. static int __cam_isp_ctx_buf_done_in_epoch(struct cam_isp_context *ctx_isp,
  1630. void *evt_data)
  1631. {
  1632. int rc = 0;
  1633. struct cam_isp_hw_done_event_data *done =
  1634. (struct cam_isp_hw_done_event_data *) evt_data;
  1635. rc = __cam_isp_ctx_handle_buf_done_in_activated_state(ctx_isp, done, 0);
  1636. return rc;
  1637. }
  1638. static int __cam_isp_ctx_buf_done_in_bubble(
  1639. struct cam_isp_context *ctx_isp, void *evt_data)
  1640. {
  1641. int rc = 0;
  1642. struct cam_isp_hw_done_event_data *done =
  1643. (struct cam_isp_hw_done_event_data *) evt_data;
  1644. rc = __cam_isp_ctx_handle_buf_done_in_activated_state(ctx_isp, done, 1);
  1645. return rc;
  1646. }
  1647. static int __cam_isp_ctx_epoch_in_bubble_applied(
  1648. struct cam_isp_context *ctx_isp, void *evt_data)
  1649. {
  1650. uint64_t request_id = 0;
  1651. struct cam_ctx_request *req;
  1652. struct cam_isp_ctx_req *req_isp;
  1653. struct cam_context *ctx = ctx_isp->base;
  1654. struct cam_isp_hw_epoch_event_data *epoch_done_event_data =
  1655. (struct cam_isp_hw_epoch_event_data *)evt_data;
  1656. if (!evt_data) {
  1657. CAM_ERR(CAM_ISP, "invalid event data");
  1658. return -EINVAL;
  1659. }
  1660. ctx_isp->frame_id_meta = epoch_done_event_data->frame_id_meta;
  1661. /*
  1662. * This means we missed the reg upd ack. So we need to
  1663. * transition to BUBBLE state again.
  1664. */
  1665. if (list_empty(&ctx->wait_req_list)) {
  1666. /*
  1667. * If no pending req in epoch, this is an error case.
  1668. * Just go back to the bubble state.
  1669. */
  1670. CAM_ERR(CAM_ISP, "ctx:%d No pending request.", ctx->ctx_id);
  1671. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  1672. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  1673. __cam_isp_ctx_update_event_record(ctx_isp,
  1674. CAM_ISP_CTX_EVENT_EPOCH, NULL);
  1675. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_BUBBLE;
  1676. goto end;
  1677. }
  1678. req = list_first_entry(&ctx->wait_req_list, struct cam_ctx_request,
  1679. list);
  1680. req_isp = (struct cam_isp_ctx_req *)req->req_priv;
  1681. req_isp->bubble_detected = true;
  1682. CAM_INFO(CAM_ISP, "Ctx:%d Report Bubble flag %d req id:%lld",
  1683. ctx->ctx_id, req_isp->bubble_report, req->request_id);
  1684. req_isp->reapply = true;
  1685. if (req_isp->bubble_report && ctx->ctx_crm_intf &&
  1686. ctx->ctx_crm_intf->notify_err) {
  1687. struct cam_req_mgr_error_notify notify;
  1688. notify.link_hdl = ctx->link_hdl;
  1689. notify.dev_hdl = ctx->dev_hdl;
  1690. notify.req_id = req->request_id;
  1691. notify.error = CRM_KMD_ERR_BUBBLE;
  1692. notify.trigger = 0;
  1693. if (ctx_isp->subscribe_event & CAM_TRIGGER_POINT_SOF)
  1694. notify.trigger = CAM_TRIGGER_POINT_SOF;
  1695. notify.frame_id = ctx_isp->frame_id;
  1696. notify.sof_timestamp_val = ctx_isp->sof_timestamp_val;
  1697. CAM_WARN(CAM_REQ,
  1698. "Notify CRM about Bubble req_id %llu frame %lld, ctx %u",
  1699. req->request_id, ctx_isp->frame_id, ctx->ctx_id);
  1700. ctx->ctx_crm_intf->notify_err(&notify);
  1701. atomic_set(&ctx_isp->process_bubble, 1);
  1702. } else {
  1703. req_isp->bubble_report = 0;
  1704. }
  1705. /*
  1706. * Always move the request to active list. Let buf done
  1707. * function handles the rest.
  1708. */
  1709. list_del_init(&req->list);
  1710. list_add_tail(&req->list, &ctx->active_req_list);
  1711. ctx_isp->active_req_cnt++;
  1712. CAM_DBG(CAM_ISP, "move request %lld to active list(cnt = %d) ctx %u",
  1713. req->request_id, ctx_isp->active_req_cnt);
  1714. if (!req_isp->bubble_report) {
  1715. if (req->request_id > ctx_isp->reported_req_id) {
  1716. request_id = req->request_id;
  1717. ctx_isp->reported_req_id = request_id;
  1718. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  1719. CAM_REQ_MGR_SOF_EVENT_ERROR);
  1720. __cam_isp_ctx_update_event_record(ctx_isp,
  1721. CAM_ISP_CTX_EVENT_EPOCH, req);
  1722. } else {
  1723. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  1724. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  1725. __cam_isp_ctx_update_event_record(ctx_isp,
  1726. CAM_ISP_CTX_EVENT_EPOCH, NULL);
  1727. }
  1728. } else {
  1729. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  1730. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  1731. __cam_isp_ctx_update_event_record(ctx_isp,
  1732. CAM_ISP_CTX_EVENT_EPOCH, NULL);
  1733. }
  1734. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_BUBBLE;
  1735. CAM_DBG(CAM_ISP, "next Substate[%s]",
  1736. __cam_isp_ctx_substate_val_to_type(
  1737. ctx_isp->substate_activated));
  1738. end:
  1739. req = list_last_entry(&ctx->active_req_list, struct cam_ctx_request,
  1740. list);
  1741. if (req)
  1742. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  1743. CAM_ISP_STATE_CHANGE_TRIGGER_EPOCH, req->request_id);
  1744. return 0;
  1745. }
  1746. static int __cam_isp_ctx_buf_done_in_bubble_applied(
  1747. struct cam_isp_context *ctx_isp, void *evt_data)
  1748. {
  1749. int rc = 0;
  1750. struct cam_isp_hw_done_event_data *done =
  1751. (struct cam_isp_hw_done_event_data *) evt_data;
  1752. rc = __cam_isp_ctx_handle_buf_done_in_activated_state(ctx_isp, done, 1);
  1753. return rc;
  1754. }
  1755. static uint32_t get_evt_param(uint32_t error_type)
  1756. {
  1757. switch (error_type) {
  1758. case CAM_ISP_HW_ERROR_OVERFLOW:
  1759. return CAM_SYNC_ISP_EVENT_OVERFLOW;
  1760. case CAM_ISP_HW_ERROR_P2I_ERROR:
  1761. return CAM_SYNC_ISP_EVENT_P2I_ERROR;
  1762. case CAM_ISP_HW_ERROR_VIOLATION:
  1763. return CAM_SYNC_ISP_EVENT_VIOLATION;
  1764. case CAM_ISP_HW_ERROR_BUSIF_OVERFLOW:
  1765. return CAM_SYNC_ISP_EVENT_BUSIF_OVERFLOW;
  1766. default:
  1767. return CAM_SYNC_ISP_EVENT_UNKNOWN;
  1768. }
  1769. }
  1770. static int __cam_isp_ctx_handle_error(struct cam_isp_context *ctx_isp,
  1771. void *evt_data)
  1772. {
  1773. int rc = 0;
  1774. uint32_t i = 0;
  1775. bool found = 0;
  1776. struct cam_ctx_request *req = NULL;
  1777. struct cam_ctx_request *req_to_report = NULL;
  1778. struct cam_ctx_request *req_to_dump = NULL;
  1779. struct cam_ctx_request *req_temp;
  1780. struct cam_isp_ctx_req *req_isp = NULL;
  1781. struct cam_isp_ctx_req *req_isp_to_report = NULL;
  1782. struct cam_req_mgr_error_notify notify = {};
  1783. uint64_t error_request_id;
  1784. struct cam_hw_fence_map_entry *fence_map_out = NULL;
  1785. struct cam_req_mgr_message req_msg;
  1786. uint32_t evt_param;
  1787. struct cam_context *ctx = ctx_isp->base;
  1788. struct cam_isp_hw_error_event_data *error_event_data =
  1789. (struct cam_isp_hw_error_event_data *)evt_data;
  1790. uint32_t error_type = error_event_data->error_type;
  1791. CAM_DBG(CAM_ISP, "Enter error_type = %d", error_type);
  1792. if ((error_type == CAM_ISP_HW_ERROR_OVERFLOW) ||
  1793. (error_type == CAM_ISP_HW_ERROR_BUSIF_OVERFLOW) ||
  1794. (error_type == CAM_ISP_HW_ERROR_VIOLATION)) {
  1795. struct cam_hw_cmd_args hw_cmd_args;
  1796. memset(&hw_cmd_args, 0, sizeof(hw_cmd_args));
  1797. hw_cmd_args.ctxt_to_hw_map = ctx->ctxt_to_hw_map;
  1798. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_REG_DUMP_ON_ERROR;
  1799. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  1800. &hw_cmd_args);
  1801. if (rc) {
  1802. CAM_ERR(CAM_ISP, "Reg dump on error failed rc: %d", rc);
  1803. rc = 0;
  1804. }
  1805. }
  1806. evt_param = get_evt_param(error_type);
  1807. /*
  1808. * The error is likely caused by first request on the active list.
  1809. * If active list is empty check wait list (maybe error hit as soon
  1810. * as RUP and we handle error before RUP.
  1811. */
  1812. if (list_empty(&ctx->active_req_list)) {
  1813. CAM_DBG(CAM_ISP,
  1814. "handling error with no active request");
  1815. if (list_empty(&ctx->wait_req_list)) {
  1816. CAM_ERR_RATE_LIMIT(CAM_ISP,
  1817. "Error with no active/wait request");
  1818. goto end;
  1819. } else {
  1820. req_to_dump = list_first_entry(&ctx->wait_req_list,
  1821. struct cam_ctx_request, list);
  1822. }
  1823. } else {
  1824. req_to_dump = list_first_entry(&ctx->active_req_list,
  1825. struct cam_ctx_request, list);
  1826. }
  1827. req_isp = (struct cam_isp_ctx_req *) req_to_dump->req_priv;
  1828. if (error_event_data->enable_req_dump)
  1829. rc = cam_isp_ctx_dump_req(req_isp, 0, 0, NULL, false);
  1830. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  1831. CAM_ISP_STATE_CHANGE_TRIGGER_ERROR, req_to_dump->request_id);
  1832. list_for_each_entry_safe(req, req_temp,
  1833. &ctx->active_req_list, list) {
  1834. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  1835. if (!req_isp->bubble_report) {
  1836. for (i = 0; i < req_isp->num_fence_map_out; i++) {
  1837. fence_map_out =
  1838. &req_isp->fence_map_out[i];
  1839. CAM_ERR(CAM_ISP,
  1840. "req %llu, Sync fd 0x%x ctx %u",
  1841. req->request_id,
  1842. req_isp->fence_map_out[i].sync_id,
  1843. ctx->ctx_id);
  1844. if (req_isp->fence_map_out[i].sync_id != -1) {
  1845. rc = cam_sync_signal(
  1846. fence_map_out->sync_id,
  1847. CAM_SYNC_STATE_SIGNALED_ERROR,
  1848. evt_param);
  1849. fence_map_out->sync_id = -1;
  1850. }
  1851. }
  1852. list_del_init(&req->list);
  1853. list_add_tail(&req->list, &ctx->free_req_list);
  1854. ctx_isp->active_req_cnt--;
  1855. } else {
  1856. found = 1;
  1857. break;
  1858. }
  1859. }
  1860. if (found)
  1861. goto move_to_pending;
  1862. list_for_each_entry_safe(req, req_temp,
  1863. &ctx->wait_req_list, list) {
  1864. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  1865. if (!req_isp->bubble_report) {
  1866. for (i = 0; i < req_isp->num_fence_map_out; i++) {
  1867. fence_map_out =
  1868. &req_isp->fence_map_out[i];
  1869. CAM_ERR(CAM_ISP,
  1870. "req %llu, Sync fd 0x%x ctx %u",
  1871. req->request_id,
  1872. req_isp->fence_map_out[i].sync_id,
  1873. ctx->ctx_id);
  1874. if (req_isp->fence_map_out[i].sync_id != -1) {
  1875. rc = cam_sync_signal(
  1876. fence_map_out->sync_id,
  1877. CAM_SYNC_STATE_SIGNALED_ERROR,
  1878. evt_param);
  1879. fence_map_out->sync_id = -1;
  1880. }
  1881. }
  1882. list_del_init(&req->list);
  1883. list_add_tail(&req->list, &ctx->free_req_list);
  1884. ctx_isp->active_req_cnt--;
  1885. } else {
  1886. found = 1;
  1887. break;
  1888. }
  1889. }
  1890. move_to_pending:
  1891. /*
  1892. * If bubble recovery is enabled on any request we need to move that
  1893. * request and all the subsequent requests to the pending list.
  1894. * Note:
  1895. * We need to traverse the active list in reverse order and add
  1896. * to head of pending list.
  1897. * e.g. pending current state: 10, 11 | active current state: 8, 9
  1898. * intermittent for loop iteration- pending: 9, 10, 11 | active: 8
  1899. * final state - pending: 8, 9, 10, 11 | active: NULL
  1900. */
  1901. if (found) {
  1902. list_for_each_entry_safe_reverse(req, req_temp,
  1903. &ctx->active_req_list, list) {
  1904. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  1905. list_del_init(&req->list);
  1906. list_add(&req->list, &ctx->pending_req_list);
  1907. ctx_isp->active_req_cnt--;
  1908. }
  1909. list_for_each_entry_safe_reverse(req, req_temp,
  1910. &ctx->wait_req_list, list) {
  1911. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  1912. list_del_init(&req->list);
  1913. list_add(&req->list, &ctx->pending_req_list);
  1914. ctx_isp->active_req_cnt--;
  1915. }
  1916. }
  1917. end:
  1918. do {
  1919. if (list_empty(&ctx->pending_req_list)) {
  1920. error_request_id = ctx_isp->last_applied_req_id;
  1921. req_isp = NULL;
  1922. break;
  1923. }
  1924. req = list_first_entry(&ctx->pending_req_list,
  1925. struct cam_ctx_request, list);
  1926. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  1927. error_request_id = ctx_isp->last_applied_req_id;
  1928. if (req_isp->bubble_report) {
  1929. req_to_report = req;
  1930. req_isp_to_report = req_to_report->req_priv;
  1931. break;
  1932. }
  1933. for (i = 0; i < req_isp->num_fence_map_out; i++) {
  1934. if (req_isp->fence_map_out[i].sync_id != -1)
  1935. rc = cam_sync_signal(
  1936. req_isp->fence_map_out[i].sync_id,
  1937. CAM_SYNC_STATE_SIGNALED_ERROR,
  1938. evt_param);
  1939. req_isp->fence_map_out[i].sync_id = -1;
  1940. }
  1941. list_del_init(&req->list);
  1942. list_add_tail(&req->list, &ctx->free_req_list);
  1943. } while (req->request_id < ctx_isp->last_applied_req_id);
  1944. if (ctx_isp->offline_context)
  1945. goto exit;
  1946. if (ctx->ctx_crm_intf && ctx->ctx_crm_intf->notify_err) {
  1947. notify.link_hdl = ctx->link_hdl;
  1948. notify.dev_hdl = ctx->dev_hdl;
  1949. notify.req_id = error_request_id;
  1950. notify.error = CRM_KMD_ERR_FATAL;
  1951. if (req_isp_to_report && req_isp_to_report->bubble_report)
  1952. if (error_event_data->recovery_enabled)
  1953. notify.error = CRM_KMD_ERR_BUBBLE;
  1954. CAM_WARN(CAM_ISP,
  1955. "Notify CRM: req %lld, frame %lld ctx %u, error %d",
  1956. error_request_id, ctx_isp->frame_id, ctx->ctx_id,
  1957. notify.error);
  1958. ctx->ctx_crm_intf->notify_err(&notify);
  1959. /*
  1960. * Need to send error occurred in KMD
  1961. * This will help UMD to take necessary action
  1962. * and to dump relevant info
  1963. */
  1964. if (notify.error == CRM_KMD_ERR_FATAL) {
  1965. req_msg.session_hdl = ctx_isp->base->session_hdl;
  1966. req_msg.u.err_msg.device_hdl = ctx_isp->base->dev_hdl;
  1967. if (error_type == CAM_ISP_HW_ERROR_CSID_FATAL)
  1968. req_msg.u.err_msg.error_type =
  1969. CAM_REQ_MGR_ERROR_TYPE_FULL_RECOVERY;
  1970. else
  1971. req_msg.u.err_msg.error_type =
  1972. CAM_REQ_MGR_ERROR_TYPE_RECOVERY;
  1973. req_msg.u.err_msg.link_hdl = ctx_isp->base->link_hdl;
  1974. req_msg.u.err_msg.request_id = error_request_id;
  1975. req_msg.u.err_msg.resource_size = 0x0;
  1976. if (cam_req_mgr_notify_message(&req_msg,
  1977. V4L_EVENT_CAM_REQ_MGR_ERROR,
  1978. V4L_EVENT_CAM_REQ_MGR_EVENT))
  1979. CAM_ERR(CAM_ISP,
  1980. "Error in notifying the error time for req id:%lld ctx %u",
  1981. ctx_isp->last_applied_req_id,
  1982. ctx->ctx_id);
  1983. }
  1984. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_HW_ERROR;
  1985. } else {
  1986. CAM_ERR_RATE_LIMIT(CAM_ISP,
  1987. "Can not notify ERRROR to CRM for ctx %u",
  1988. ctx->ctx_id);
  1989. rc = -EFAULT;
  1990. }
  1991. CAM_DBG(CAM_ISP, "Exit");
  1992. exit:
  1993. return rc;
  1994. }
  1995. static int __cam_isp_ctx_fs2_sof_in_sof_state(
  1996. struct cam_isp_context *ctx_isp, void *evt_data)
  1997. {
  1998. int rc = 0;
  1999. struct cam_isp_hw_sof_event_data *sof_event_data = evt_data;
  2000. struct cam_ctx_request *req;
  2001. struct cam_context *ctx = ctx_isp->base;
  2002. struct cam_req_mgr_trigger_notify notify;
  2003. uint64_t request_id = 0;
  2004. if (!evt_data) {
  2005. CAM_ERR(CAM_ISP, "in valid sof event data");
  2006. return -EINVAL;
  2007. }
  2008. ctx_isp->frame_id++;
  2009. ctx_isp->sof_timestamp_val = sof_event_data->timestamp;
  2010. ctx_isp->boot_timestamp = sof_event_data->boot_time;
  2011. CAM_DBG(CAM_ISP, "frame id: %lld time stamp:0x%llx",
  2012. ctx_isp->frame_id, ctx_isp->sof_timestamp_val);
  2013. if (!(list_empty(&ctx->wait_req_list)))
  2014. goto end;
  2015. if (ctx->ctx_crm_intf && ctx->ctx_crm_intf->notify_trigger &&
  2016. ctx_isp->active_req_cnt <= 2) {
  2017. if (ctx_isp->subscribe_event & CAM_TRIGGER_POINT_SOF) {
  2018. notify.link_hdl = ctx->link_hdl;
  2019. notify.dev_hdl = ctx->dev_hdl;
  2020. notify.frame_id = ctx_isp->frame_id;
  2021. notify.trigger = CAM_TRIGGER_POINT_SOF;
  2022. notify.req_id = ctx_isp->req_info.last_bufdone_req_id;
  2023. notify.sof_timestamp_val = ctx_isp->sof_timestamp_val;
  2024. ctx->ctx_crm_intf->notify_trigger(&notify);
  2025. CAM_DBG(CAM_ISP, "Notify CRM SOF frame %lld",
  2026. ctx_isp->frame_id);
  2027. }
  2028. list_for_each_entry(req, &ctx->active_req_list, list) {
  2029. if (req->request_id > ctx_isp->reported_req_id) {
  2030. request_id = req->request_id;
  2031. ctx_isp->reported_req_id = request_id;
  2032. break;
  2033. }
  2034. }
  2035. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  2036. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  2037. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  2038. CAM_ISP_STATE_CHANGE_TRIGGER_SOF, request_id);
  2039. } else {
  2040. CAM_ERR_RATE_LIMIT(CAM_ISP, "Can not notify SOF to CRM");
  2041. rc = -EFAULT;
  2042. }
  2043. end:
  2044. return rc;
  2045. }
  2046. static int __cam_isp_ctx_fs2_buf_done(struct cam_isp_context *ctx_isp,
  2047. void *evt_data)
  2048. {
  2049. int rc = 0;
  2050. struct cam_isp_hw_done_event_data *done =
  2051. (struct cam_isp_hw_done_event_data *) evt_data;
  2052. struct cam_context *ctx = ctx_isp->base;
  2053. int prev_active_req_cnt = 0;
  2054. int curr_req_id = 0;
  2055. struct cam_ctx_request *req;
  2056. prev_active_req_cnt = ctx_isp->active_req_cnt;
  2057. req = list_first_entry(&ctx->active_req_list,
  2058. struct cam_ctx_request, list);
  2059. if (req)
  2060. curr_req_id = req->request_id;
  2061. rc = __cam_isp_ctx_handle_buf_done_in_activated_state(ctx_isp, done, 0);
  2062. if (prev_active_req_cnt == ctx_isp->active_req_cnt + 1) {
  2063. if (list_empty(&ctx->wait_req_list) &&
  2064. list_empty(&ctx->active_req_list)) {
  2065. CAM_DBG(CAM_ISP, "No request, move to SOF");
  2066. ctx_isp->substate_activated =
  2067. CAM_ISP_CTX_ACTIVATED_SOF;
  2068. if (ctx_isp->reported_req_id < curr_req_id) {
  2069. ctx_isp->reported_req_id = curr_req_id;
  2070. __cam_isp_ctx_send_sof_timestamp(ctx_isp,
  2071. curr_req_id,
  2072. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  2073. }
  2074. }
  2075. }
  2076. return rc;
  2077. }
  2078. static int __cam_isp_ctx_fs2_buf_done_in_epoch(struct cam_isp_context *ctx_isp,
  2079. void *evt_data)
  2080. {
  2081. int rc = 0;
  2082. rc = __cam_isp_ctx_fs2_buf_done(ctx_isp, evt_data);
  2083. return rc;
  2084. }
  2085. static int __cam_isp_ctx_fs2_buf_done_in_applied(
  2086. struct cam_isp_context *ctx_isp,
  2087. void *evt_data)
  2088. {
  2089. int rc = 0;
  2090. rc = __cam_isp_ctx_fs2_buf_done(ctx_isp, evt_data);
  2091. return rc;
  2092. }
  2093. static int __cam_isp_ctx_fs2_reg_upd_in_sof(struct cam_isp_context *ctx_isp,
  2094. void *evt_data)
  2095. {
  2096. int rc = 0;
  2097. struct cam_ctx_request *req = NULL;
  2098. struct cam_isp_ctx_req *req_isp;
  2099. struct cam_context *ctx = ctx_isp->base;
  2100. if (ctx->state != CAM_CTX_ACTIVATED && ctx_isp->frame_id > 1) {
  2101. CAM_DBG(CAM_ISP, "invalid RUP");
  2102. goto end;
  2103. }
  2104. /*
  2105. * This is for the first update. The initial setting will
  2106. * cause the reg_upd in the first frame.
  2107. */
  2108. if (!list_empty(&ctx->wait_req_list)) {
  2109. req = list_first_entry(&ctx->wait_req_list,
  2110. struct cam_ctx_request, list);
  2111. list_del_init(&req->list);
  2112. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  2113. if (req_isp->num_fence_map_out == req_isp->num_acked)
  2114. list_add_tail(&req->list, &ctx->free_req_list);
  2115. else
  2116. CAM_ERR(CAM_ISP,
  2117. "receive rup in unexpected state");
  2118. }
  2119. if (req != NULL) {
  2120. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  2121. CAM_ISP_STATE_CHANGE_TRIGGER_REG_UPDATE,
  2122. req->request_id);
  2123. }
  2124. end:
  2125. return rc;
  2126. }
  2127. static int __cam_isp_ctx_fs2_reg_upd_in_applied_state(
  2128. struct cam_isp_context *ctx_isp, void *evt_data)
  2129. {
  2130. int rc = 0;
  2131. struct cam_ctx_request *req = NULL;
  2132. struct cam_context *ctx = ctx_isp->base;
  2133. struct cam_isp_ctx_req *req_isp;
  2134. struct cam_req_mgr_trigger_notify notify;
  2135. uint64_t request_id = 0;
  2136. if (list_empty(&ctx->wait_req_list)) {
  2137. CAM_ERR(CAM_ISP, "Reg upd ack with no waiting request");
  2138. goto end;
  2139. }
  2140. req = list_first_entry(&ctx->wait_req_list,
  2141. struct cam_ctx_request, list);
  2142. list_del_init(&req->list);
  2143. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  2144. if (req_isp->num_fence_map_out != 0) {
  2145. list_add_tail(&req->list, &ctx->active_req_list);
  2146. ctx_isp->active_req_cnt++;
  2147. CAM_DBG(CAM_REQ, "move request %lld to active list(cnt = %d)",
  2148. req->request_id, ctx_isp->active_req_cnt);
  2149. } else {
  2150. /* no io config, so the request is completed. */
  2151. list_add_tail(&req->list, &ctx->free_req_list);
  2152. }
  2153. /*
  2154. * This function only called directly from applied and bubble applied
  2155. * state so change substate here.
  2156. */
  2157. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_EPOCH;
  2158. if (req_isp->num_fence_map_out != 1)
  2159. goto end;
  2160. if (ctx->ctx_crm_intf && ctx->ctx_crm_intf->notify_trigger &&
  2161. ctx_isp->active_req_cnt <= 2) {
  2162. list_for_each_entry(req, &ctx->active_req_list, list) {
  2163. if (req->request_id > ctx_isp->reported_req_id) {
  2164. request_id = req->request_id;
  2165. ctx_isp->reported_req_id = request_id;
  2166. break;
  2167. }
  2168. }
  2169. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  2170. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  2171. if (ctx_isp->subscribe_event & CAM_TRIGGER_POINT_SOF) {
  2172. notify.link_hdl = ctx->link_hdl;
  2173. notify.dev_hdl = ctx->dev_hdl;
  2174. notify.frame_id = ctx_isp->frame_id;
  2175. notify.trigger = CAM_TRIGGER_POINT_SOF;
  2176. notify.req_id = ctx_isp->req_info.last_bufdone_req_id;
  2177. notify.sof_timestamp_val = ctx_isp->sof_timestamp_val;
  2178. ctx->ctx_crm_intf->notify_trigger(&notify);
  2179. CAM_DBG(CAM_ISP, "Notify CRM SOF frame %lld",
  2180. ctx_isp->frame_id);
  2181. }
  2182. } else {
  2183. CAM_ERR_RATE_LIMIT(CAM_ISP, "Can not notify SOF to CRM");
  2184. rc = -EFAULT;
  2185. }
  2186. CAM_DBG(CAM_ISP, "next Substate[%s]",
  2187. __cam_isp_ctx_substate_val_to_type(
  2188. ctx_isp->substate_activated));
  2189. end:
  2190. if (req != NULL && !rc) {
  2191. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  2192. CAM_ISP_STATE_CHANGE_TRIGGER_REG_UPDATE,
  2193. req->request_id);
  2194. }
  2195. return rc;
  2196. }
  2197. static struct cam_isp_ctx_irq_ops
  2198. cam_isp_ctx_activated_state_machine_irq[CAM_ISP_CTX_ACTIVATED_MAX] = {
  2199. /* SOF */
  2200. {
  2201. .irq_ops = {
  2202. __cam_isp_ctx_handle_error,
  2203. __cam_isp_ctx_sof_in_activated_state,
  2204. __cam_isp_ctx_reg_upd_in_sof,
  2205. __cam_isp_ctx_notify_sof_in_activated_state,
  2206. __cam_isp_ctx_notify_eof_in_activated_state,
  2207. NULL,
  2208. },
  2209. },
  2210. /* APPLIED */
  2211. {
  2212. .irq_ops = {
  2213. __cam_isp_ctx_handle_error,
  2214. __cam_isp_ctx_sof_in_activated_state,
  2215. __cam_isp_ctx_reg_upd_in_applied_state,
  2216. __cam_isp_ctx_epoch_in_applied,
  2217. __cam_isp_ctx_notify_eof_in_activated_state,
  2218. __cam_isp_ctx_buf_done_in_applied,
  2219. },
  2220. },
  2221. /* EPOCH */
  2222. {
  2223. .irq_ops = {
  2224. __cam_isp_ctx_handle_error,
  2225. __cam_isp_ctx_sof_in_epoch,
  2226. __cam_isp_ctx_reg_upd_in_epoch_bubble_state,
  2227. __cam_isp_ctx_notify_sof_in_activated_state,
  2228. __cam_isp_ctx_notify_eof_in_activated_state,
  2229. __cam_isp_ctx_buf_done_in_epoch,
  2230. },
  2231. },
  2232. /* BUBBLE */
  2233. {
  2234. .irq_ops = {
  2235. __cam_isp_ctx_handle_error,
  2236. __cam_isp_ctx_sof_in_activated_state,
  2237. __cam_isp_ctx_reg_upd_in_epoch_bubble_state,
  2238. __cam_isp_ctx_notify_sof_in_activated_state,
  2239. __cam_isp_ctx_notify_eof_in_activated_state,
  2240. __cam_isp_ctx_buf_done_in_bubble,
  2241. },
  2242. },
  2243. /* Bubble Applied */
  2244. {
  2245. .irq_ops = {
  2246. __cam_isp_ctx_handle_error,
  2247. __cam_isp_ctx_sof_in_activated_state,
  2248. __cam_isp_ctx_reg_upd_in_applied_state,
  2249. __cam_isp_ctx_epoch_in_bubble_applied,
  2250. NULL,
  2251. __cam_isp_ctx_buf_done_in_bubble_applied,
  2252. },
  2253. },
  2254. /* HW ERROR */
  2255. {
  2256. .irq_ops = {
  2257. NULL,
  2258. __cam_isp_ctx_sof_in_activated_state,
  2259. __cam_isp_ctx_reg_upd_in_hw_error,
  2260. NULL,
  2261. NULL,
  2262. NULL,
  2263. },
  2264. },
  2265. /* HALT */
  2266. {
  2267. },
  2268. };
  2269. static struct cam_isp_ctx_irq_ops
  2270. cam_isp_ctx_fs2_state_machine_irq[CAM_ISP_CTX_ACTIVATED_MAX] = {
  2271. /* SOF */
  2272. {
  2273. .irq_ops = {
  2274. __cam_isp_ctx_handle_error,
  2275. __cam_isp_ctx_fs2_sof_in_sof_state,
  2276. __cam_isp_ctx_fs2_reg_upd_in_sof,
  2277. __cam_isp_ctx_fs2_sof_in_sof_state,
  2278. __cam_isp_ctx_notify_eof_in_activated_state,
  2279. NULL,
  2280. },
  2281. },
  2282. /* APPLIED */
  2283. {
  2284. .irq_ops = {
  2285. __cam_isp_ctx_handle_error,
  2286. __cam_isp_ctx_sof_in_activated_state,
  2287. __cam_isp_ctx_fs2_reg_upd_in_applied_state,
  2288. __cam_isp_ctx_epoch_in_applied,
  2289. __cam_isp_ctx_notify_eof_in_activated_state,
  2290. __cam_isp_ctx_fs2_buf_done_in_applied,
  2291. },
  2292. },
  2293. /* EPOCH */
  2294. {
  2295. .irq_ops = {
  2296. __cam_isp_ctx_handle_error,
  2297. __cam_isp_ctx_sof_in_epoch,
  2298. __cam_isp_ctx_reg_upd_in_epoch_bubble_state,
  2299. __cam_isp_ctx_notify_sof_in_activated_state,
  2300. __cam_isp_ctx_notify_eof_in_activated_state,
  2301. __cam_isp_ctx_fs2_buf_done_in_epoch,
  2302. },
  2303. },
  2304. /* BUBBLE */
  2305. {
  2306. .irq_ops = {
  2307. __cam_isp_ctx_handle_error,
  2308. __cam_isp_ctx_sof_in_activated_state,
  2309. __cam_isp_ctx_reg_upd_in_epoch_bubble_state,
  2310. __cam_isp_ctx_notify_sof_in_activated_state,
  2311. __cam_isp_ctx_notify_eof_in_activated_state,
  2312. __cam_isp_ctx_buf_done_in_bubble,
  2313. },
  2314. },
  2315. /* Bubble Applied */
  2316. {
  2317. .irq_ops = {
  2318. __cam_isp_ctx_handle_error,
  2319. __cam_isp_ctx_sof_in_activated_state,
  2320. __cam_isp_ctx_reg_upd_in_applied_state,
  2321. __cam_isp_ctx_epoch_in_bubble_applied,
  2322. NULL,
  2323. __cam_isp_ctx_buf_done_in_bubble_applied,
  2324. },
  2325. },
  2326. /* HW ERROR */
  2327. {
  2328. .irq_ops = {
  2329. NULL,
  2330. __cam_isp_ctx_sof_in_activated_state,
  2331. __cam_isp_ctx_reg_upd_in_hw_error,
  2332. NULL,
  2333. NULL,
  2334. NULL,
  2335. },
  2336. },
  2337. /* HALT */
  2338. {
  2339. },
  2340. };
  2341. static struct cam_isp_ctx_irq_ops
  2342. cam_isp_ctx_offline_state_machine_irq[CAM_ISP_CTX_ACTIVATED_MAX] = {
  2343. /* SOF */
  2344. {
  2345. .irq_ops = {
  2346. __cam_isp_ctx_handle_error,
  2347. NULL,
  2348. NULL,
  2349. NULL,
  2350. NULL,
  2351. NULL,
  2352. },
  2353. },
  2354. /* APPLIED */
  2355. {
  2356. .irq_ops = {
  2357. __cam_isp_ctx_handle_error,
  2358. __cam_isp_ctx_sof_in_activated_state,
  2359. __cam_isp_ctx_reg_upd_in_applied_state,
  2360. NULL,
  2361. NULL,
  2362. __cam_isp_ctx_buf_done_in_applied,
  2363. },
  2364. },
  2365. /* EPOCH */
  2366. {
  2367. .irq_ops = {
  2368. __cam_isp_ctx_handle_error,
  2369. NULL,
  2370. NULL,
  2371. __cam_isp_ctx_offline_epoch_in_activated_state,
  2372. NULL,
  2373. __cam_isp_ctx_buf_done_in_epoch,
  2374. },
  2375. },
  2376. /* BUBBLE */
  2377. {
  2378. },
  2379. /* 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 int __cam_isp_ctx_apply_req_in_activated_state(
  2398. struct cam_context *ctx, struct cam_req_mgr_apply_request *apply,
  2399. enum cam_isp_ctx_activated_substate next_state)
  2400. {
  2401. int rc = 0;
  2402. struct cam_ctx_request *req;
  2403. struct cam_ctx_request *active_req = NULL;
  2404. struct cam_isp_ctx_req *req_isp;
  2405. struct cam_isp_ctx_req *active_req_isp;
  2406. struct cam_isp_context *ctx_isp = NULL;
  2407. struct cam_hw_config_args cfg = {0};
  2408. if (list_empty(&ctx->pending_req_list)) {
  2409. CAM_ERR(CAM_ISP, "No available request for Apply id %lld",
  2410. apply->request_id);
  2411. rc = -EFAULT;
  2412. goto end;
  2413. }
  2414. /*
  2415. * When the pipeline has issue, the requests can be queued up in the
  2416. * pipeline. In this case, we should reject the additional request.
  2417. * The maximum number of request allowed to be outstanding is 2.
  2418. *
  2419. */
  2420. ctx_isp = (struct cam_isp_context *) ctx->ctx_priv;
  2421. if (atomic_read(&ctx_isp->process_bubble)) {
  2422. CAM_INFO(CAM_ISP,
  2423. "Processing bubble cannot apply Request Id %llu",
  2424. apply->request_id);
  2425. rc = -EAGAIN;
  2426. goto end;
  2427. }
  2428. if (apply->re_apply)
  2429. if (apply->request_id <= ctx_isp->last_applied_req_id) {
  2430. CAM_INFO(CAM_ISP,
  2431. "Trying to reapply the same request %llu again",
  2432. apply->request_id);
  2433. return 0;
  2434. }
  2435. spin_lock_bh(&ctx->lock);
  2436. req = list_first_entry(&ctx->pending_req_list, struct cam_ctx_request,
  2437. list);
  2438. spin_unlock_bh(&ctx->lock);
  2439. /*
  2440. * Check whether the request id is matching the tip, if not, this means
  2441. * we are in the middle of the error handling. Need to reject this apply
  2442. */
  2443. if (req->request_id != apply->request_id) {
  2444. CAM_ERR_RATE_LIMIT(CAM_ISP,
  2445. "Invalid Request Id asking %llu existing %llu",
  2446. apply->request_id, req->request_id);
  2447. rc = -EFAULT;
  2448. goto end;
  2449. }
  2450. CAM_DBG(CAM_REQ, "Apply request %lld in Substate[%s] ctx %u",
  2451. req->request_id,
  2452. __cam_isp_ctx_substate_val_to_type(ctx_isp->substate_activated),
  2453. ctx->ctx_id);
  2454. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  2455. if (ctx_isp->active_req_cnt >= 2) {
  2456. CAM_WARN(CAM_ISP,
  2457. "Reject apply request (id %lld) due to congestion(cnt = %d) ctx %u",
  2458. req->request_id,
  2459. ctx_isp->active_req_cnt,
  2460. ctx->ctx_id);
  2461. spin_lock_bh(&ctx->lock);
  2462. if (!list_empty(&ctx->active_req_list))
  2463. active_req = list_first_entry(&ctx->active_req_list,
  2464. struct cam_ctx_request, list);
  2465. else
  2466. CAM_ERR_RATE_LIMIT(CAM_ISP,
  2467. "WARNING: should not happen (cnt = %d) but active_list empty",
  2468. ctx_isp->active_req_cnt);
  2469. spin_unlock_bh(&ctx->lock);
  2470. if (active_req) {
  2471. active_req_isp =
  2472. (struct cam_isp_ctx_req *) active_req->req_priv;
  2473. __cam_isp_ctx_handle_buf_done_fail_log(
  2474. active_req->request_id, active_req_isp,
  2475. ctx_isp->isp_device_type);
  2476. }
  2477. rc = -EFAULT;
  2478. goto end;
  2479. }
  2480. req_isp->bubble_report = apply->report_if_bubble;
  2481. cfg.ctxt_to_hw_map = ctx_isp->hw_ctx;
  2482. cfg.request_id = req->request_id;
  2483. cfg.hw_update_entries = req_isp->cfg;
  2484. cfg.num_hw_update_entries = req_isp->num_cfg;
  2485. cfg.priv = &req_isp->hw_update_data;
  2486. cfg.init_packet = 0;
  2487. cfg.reapply = req_isp->reapply;
  2488. rc = ctx->hw_mgr_intf->hw_config(ctx->hw_mgr_intf->hw_mgr_priv,
  2489. &cfg);
  2490. if (rc) {
  2491. CAM_ERR_RATE_LIMIT(CAM_ISP, "Can not apply the configuration");
  2492. } else {
  2493. spin_lock_bh(&ctx->lock);
  2494. ctx_isp->substate_activated = next_state;
  2495. ctx_isp->last_applied_req_id = apply->request_id;
  2496. list_del_init(&req->list);
  2497. list_add_tail(&req->list, &ctx->wait_req_list);
  2498. CAM_DBG(CAM_ISP, "new substate Substate[%s], applied req %lld",
  2499. __cam_isp_ctx_substate_val_to_type(next_state),
  2500. ctx_isp->last_applied_req_id);
  2501. spin_unlock_bh(&ctx->lock);
  2502. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  2503. CAM_ISP_STATE_CHANGE_TRIGGER_APPLIED,
  2504. req->request_id);
  2505. __cam_isp_ctx_update_event_record(ctx_isp,
  2506. CAM_ISP_CTX_EVENT_APPLY, req);
  2507. }
  2508. end:
  2509. return rc;
  2510. }
  2511. static int __cam_isp_ctx_apply_req_in_sof(
  2512. struct cam_context *ctx, struct cam_req_mgr_apply_request *apply)
  2513. {
  2514. int rc = 0;
  2515. struct cam_isp_context *ctx_isp =
  2516. (struct cam_isp_context *) ctx->ctx_priv;
  2517. CAM_DBG(CAM_ISP, "current Substate[%s]",
  2518. __cam_isp_ctx_substate_val_to_type(
  2519. ctx_isp->substate_activated));
  2520. rc = __cam_isp_ctx_apply_req_in_activated_state(ctx, apply,
  2521. CAM_ISP_CTX_ACTIVATED_APPLIED);
  2522. CAM_DBG(CAM_ISP, "new Substate[%s]",
  2523. __cam_isp_ctx_substate_val_to_type(
  2524. ctx_isp->substate_activated));
  2525. if (rc)
  2526. CAM_DBG(CAM_ISP, "Apply failed in Substate[%s], rc %d",
  2527. __cam_isp_ctx_substate_val_to_type(
  2528. ctx_isp->substate_activated), rc);
  2529. return rc;
  2530. }
  2531. static int __cam_isp_ctx_apply_req_in_epoch(
  2532. struct cam_context *ctx, struct cam_req_mgr_apply_request *apply)
  2533. {
  2534. int rc = 0;
  2535. struct cam_isp_context *ctx_isp =
  2536. (struct cam_isp_context *) ctx->ctx_priv;
  2537. CAM_DBG(CAM_ISP, "current Substate[%s]",
  2538. __cam_isp_ctx_substate_val_to_type(
  2539. ctx_isp->substate_activated));
  2540. rc = __cam_isp_ctx_apply_req_in_activated_state(ctx, apply,
  2541. CAM_ISP_CTX_ACTIVATED_APPLIED);
  2542. CAM_DBG(CAM_ISP, "new Substate[%s]",
  2543. __cam_isp_ctx_substate_val_to_type(
  2544. ctx_isp->substate_activated));
  2545. if (rc)
  2546. CAM_DBG(CAM_ISP, "Apply failed in Substate[%s], rc %d",
  2547. __cam_isp_ctx_substate_val_to_type(
  2548. ctx_isp->substate_activated), rc);
  2549. return rc;
  2550. }
  2551. static int __cam_isp_ctx_apply_req_in_bubble(
  2552. struct cam_context *ctx, struct cam_req_mgr_apply_request *apply)
  2553. {
  2554. int rc = 0;
  2555. struct cam_isp_context *ctx_isp =
  2556. (struct cam_isp_context *) ctx->ctx_priv;
  2557. CAM_DBG(CAM_ISP, "current Substate[%s]",
  2558. __cam_isp_ctx_substate_val_to_type(
  2559. ctx_isp->substate_activated));
  2560. rc = __cam_isp_ctx_apply_req_in_activated_state(ctx, apply,
  2561. CAM_ISP_CTX_ACTIVATED_BUBBLE_APPLIED);
  2562. CAM_DBG(CAM_ISP, "new Substate[%s]",
  2563. __cam_isp_ctx_substate_val_to_type(
  2564. ctx_isp->substate_activated));
  2565. if (rc)
  2566. CAM_DBG(CAM_ISP, "Apply failed in Substate[%s], rc %d",
  2567. __cam_isp_ctx_substate_val_to_type(
  2568. ctx_isp->substate_activated), rc);
  2569. return rc;
  2570. }
  2571. static int __cam_isp_ctx_dump_req_info(
  2572. struct cam_context *ctx,
  2573. struct cam_ctx_request *req,
  2574. uintptr_t cpu_addr,
  2575. size_t buf_len,
  2576. size_t *offset)
  2577. {
  2578. int i, rc;
  2579. uint8_t *dst;
  2580. int32_t *addr, *start;
  2581. uint32_t min_len;
  2582. size_t remain_len;
  2583. struct cam_isp_ctx_req *req_isp;
  2584. struct cam_isp_context *ctx_isp;
  2585. struct cam_isp_context_dump_header *hdr;
  2586. if (!req || !ctx || !offset || !cpu_addr || !buf_len) {
  2587. CAM_ERR(CAM_ISP, "Invalid parameters %pK %pK %pK %zu",
  2588. req, ctx, offset, buf_len);
  2589. return -EINVAL;
  2590. }
  2591. req_isp = (struct cam_isp_ctx_req *)req->req_priv;
  2592. ctx_isp = (struct cam_isp_context *)ctx->ctx_priv;
  2593. if (buf_len <= *offset) {
  2594. CAM_WARN(CAM_ISP, "Dump buffer overshoot len %zu offset %zu",
  2595. buf_len, *offset);
  2596. return -ENOSPC;
  2597. }
  2598. remain_len = buf_len - *offset;
  2599. min_len = sizeof(struct cam_isp_context_dump_header) +
  2600. (CAM_ISP_CTX_DUMP_REQUEST_NUM_WORDS *
  2601. req_isp->num_fence_map_out *
  2602. sizeof(int32_t));
  2603. if (remain_len < min_len) {
  2604. CAM_WARN(CAM_ISP, "Dump buffer exhaust remain %zu min %u",
  2605. remain_len, min_len);
  2606. return -ENOSPC;
  2607. }
  2608. dst = (uint8_t *)cpu_addr + *offset;
  2609. hdr = (struct cam_isp_context_dump_header *)dst;
  2610. hdr->word_size = sizeof(int32_t);
  2611. scnprintf(hdr->tag, CAM_ISP_CONTEXT_DUMP_TAG_MAX_LEN,
  2612. "ISP_OUT_FENCE:");
  2613. addr = (int32_t *)(dst + sizeof(struct cam_isp_context_dump_header));
  2614. start = addr;
  2615. for (i = 0; i < req_isp->num_fence_map_out; i++) {
  2616. *addr++ = req_isp->fence_map_out[i].resource_handle;
  2617. *addr++ = req_isp->fence_map_out[i].sync_id;
  2618. }
  2619. hdr->size = hdr->word_size * (addr - start);
  2620. *offset += hdr->size + sizeof(struct cam_isp_context_dump_header);
  2621. rc = cam_isp_ctx_dump_req(req_isp, cpu_addr, buf_len,
  2622. offset, true);
  2623. return rc;
  2624. }
  2625. static int __cam_isp_ctx_dump_in_top_state(
  2626. struct cam_context *ctx,
  2627. struct cam_req_mgr_dump_info *dump_info)
  2628. {
  2629. int rc = 0;
  2630. bool dump_only_event_record = false;
  2631. size_t buf_len;
  2632. size_t remain_len;
  2633. uint8_t *dst;
  2634. ktime_t cur_time;
  2635. uint32_t min_len;
  2636. uint64_t diff;
  2637. uint64_t *addr, *start;
  2638. uintptr_t cpu_addr;
  2639. struct timespec64 ts;
  2640. struct cam_isp_context *ctx_isp;
  2641. struct cam_ctx_request *req = NULL;
  2642. struct cam_isp_ctx_req *req_isp;
  2643. struct cam_ctx_request *req_temp;
  2644. struct cam_hw_dump_args dump_args;
  2645. struct cam_isp_context_dump_header *hdr;
  2646. spin_lock_bh(&ctx->lock);
  2647. list_for_each_entry_safe(req, req_temp,
  2648. &ctx->active_req_list, list) {
  2649. if (req->request_id == dump_info->req_id) {
  2650. CAM_INFO(CAM_ISP, "isp dump active list req: %lld",
  2651. dump_info->req_id);
  2652. goto hw_dump;
  2653. }
  2654. }
  2655. list_for_each_entry_safe(req, req_temp,
  2656. &ctx->wait_req_list, list) {
  2657. if (req->request_id == dump_info->req_id) {
  2658. CAM_INFO(CAM_ISP, "isp dump wait list req: %lld",
  2659. dump_info->req_id);
  2660. goto hw_dump;
  2661. }
  2662. }
  2663. spin_unlock_bh(&ctx->lock);
  2664. return rc;
  2665. hw_dump:
  2666. rc = cam_mem_get_cpu_buf(dump_info->buf_handle,
  2667. &cpu_addr, &buf_len);
  2668. if (rc) {
  2669. CAM_ERR(CAM_ISP, "Invalid handle %u rc %d",
  2670. dump_info->buf_handle, rc);
  2671. spin_unlock_bh(&ctx->lock);
  2672. return rc;
  2673. }
  2674. if (buf_len <= dump_info->offset) {
  2675. spin_unlock_bh(&ctx->lock);
  2676. CAM_WARN(CAM_ISP, "Dump buffer overshoot len %zu offset %zu",
  2677. buf_len, dump_info->offset);
  2678. return -ENOSPC;
  2679. }
  2680. remain_len = buf_len - dump_info->offset;
  2681. min_len = sizeof(struct cam_isp_context_dump_header) +
  2682. (CAM_ISP_CTX_DUMP_NUM_WORDS * sizeof(uint64_t));
  2683. if (remain_len < min_len) {
  2684. spin_unlock_bh(&ctx->lock);
  2685. CAM_WARN(CAM_ISP, "Dump buffer exhaust remain %zu min %u",
  2686. remain_len, min_len);
  2687. return -ENOSPC;
  2688. }
  2689. ctx_isp = (struct cam_isp_context *) ctx->ctx_priv;
  2690. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  2691. cur_time = ktime_get();
  2692. diff = ktime_us_delta(
  2693. req_isp->event_timestamp[CAM_ISP_CTX_EVENT_APPLY],
  2694. cur_time);
  2695. if (diff < CAM_ISP_CTX_RESPONSE_TIME_THRESHOLD) {
  2696. CAM_INFO(CAM_ISP, "req %lld found no error",
  2697. req->request_id);
  2698. dump_only_event_record = true;
  2699. }
  2700. dst = (uint8_t *)cpu_addr + dump_info->offset;
  2701. hdr = (struct cam_isp_context_dump_header *)dst;
  2702. scnprintf(hdr->tag, CAM_ISP_CONTEXT_DUMP_TAG_MAX_LEN,
  2703. "ISP_CTX_DUMP:");
  2704. hdr->word_size = sizeof(uint64_t);
  2705. addr = (uint64_t *)(dst +
  2706. sizeof(struct cam_isp_context_dump_header));
  2707. start = addr;
  2708. *addr++ = req->request_id;
  2709. ts = ktime_to_timespec64(
  2710. req_isp->event_timestamp[CAM_ISP_CTX_EVENT_APPLY]);
  2711. *addr++ = ts.tv_sec;
  2712. *addr++ = ts.tv_nsec/NSEC_PER_USEC;
  2713. ts = ktime_to_timespec64(cur_time);
  2714. *addr++ = ts.tv_sec;
  2715. *addr++ = ts.tv_nsec/NSEC_PER_USEC;
  2716. hdr->size = hdr->word_size * (addr - start);
  2717. dump_info->offset += hdr->size +
  2718. sizeof(struct cam_isp_context_dump_header);
  2719. rc = __cam_isp_ctx_dump_event_record(ctx_isp, cpu_addr,
  2720. buf_len, &dump_info->offset);
  2721. if (rc) {
  2722. CAM_ERR(CAM_ISP, "Dump event fail %lld",
  2723. req->request_id);
  2724. spin_unlock_bh(&ctx->lock);
  2725. return rc;
  2726. }
  2727. if (dump_only_event_record) {
  2728. spin_unlock_bh(&ctx->lock);
  2729. return rc;
  2730. }
  2731. rc = __cam_isp_ctx_dump_req_info(ctx, req, cpu_addr,
  2732. buf_len, &dump_info->offset);
  2733. if (rc) {
  2734. CAM_ERR(CAM_ISP, "Dump Req info fail %lld",
  2735. req->request_id);
  2736. spin_unlock_bh(&ctx->lock);
  2737. return rc;
  2738. }
  2739. spin_unlock_bh(&ctx->lock);
  2740. if (ctx->hw_mgr_intf->hw_dump) {
  2741. dump_args.offset = dump_info->offset;
  2742. dump_args.request_id = dump_info->req_id;
  2743. dump_args.buf_handle = dump_info->buf_handle;
  2744. dump_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  2745. rc = ctx->hw_mgr_intf->hw_dump(
  2746. ctx->hw_mgr_intf->hw_mgr_priv,
  2747. &dump_args);
  2748. dump_info->offset = dump_args.offset;
  2749. }
  2750. return rc;
  2751. }
  2752. static int __cam_isp_ctx_flush_req(struct cam_context *ctx,
  2753. struct list_head *req_list, struct cam_req_mgr_flush_request *flush_req)
  2754. {
  2755. int i, rc, tmp = 0;
  2756. uint32_t cancel_req_id_found = 0;
  2757. struct cam_ctx_request *req;
  2758. struct cam_ctx_request *req_temp;
  2759. struct cam_isp_ctx_req *req_isp;
  2760. struct list_head flush_list;
  2761. struct cam_isp_context *ctx_isp = NULL;
  2762. ctx_isp = (struct cam_isp_context *) ctx->ctx_priv;
  2763. INIT_LIST_HEAD(&flush_list);
  2764. if (list_empty(req_list)) {
  2765. CAM_DBG(CAM_ISP, "request list is empty");
  2766. if (flush_req->type == CAM_REQ_MGR_FLUSH_TYPE_CANCEL_REQ) {
  2767. CAM_ERR(CAM_ISP, "no request to cancel");
  2768. return -EINVAL;
  2769. } else
  2770. return 0;
  2771. }
  2772. CAM_DBG(CAM_REQ, "Flush [%u] in progress for req_id %llu",
  2773. flush_req->type, flush_req->req_id);
  2774. list_for_each_entry_safe(req, req_temp, req_list, list) {
  2775. if (flush_req->type == CAM_REQ_MGR_FLUSH_TYPE_CANCEL_REQ) {
  2776. if (req->request_id != flush_req->req_id) {
  2777. continue;
  2778. } else {
  2779. list_del_init(&req->list);
  2780. list_add_tail(&req->list, &flush_list);
  2781. cancel_req_id_found = 1;
  2782. __cam_isp_ctx_update_state_monitor_array(
  2783. ctx_isp,
  2784. CAM_ISP_STATE_CHANGE_TRIGGER_FLUSH,
  2785. req->request_id);
  2786. break;
  2787. }
  2788. }
  2789. list_del_init(&req->list);
  2790. list_add_tail(&req->list, &flush_list);
  2791. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  2792. CAM_ISP_STATE_CHANGE_TRIGGER_FLUSH, req->request_id);
  2793. }
  2794. list_for_each_entry_safe(req, req_temp, &flush_list, list) {
  2795. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  2796. for (i = 0; i < req_isp->num_fence_map_out; i++) {
  2797. if (req_isp->fence_map_out[i].sync_id != -1) {
  2798. CAM_DBG(CAM_ISP, "Flush req 0x%llx, fence %d",
  2799. req->request_id,
  2800. req_isp->fence_map_out[i].sync_id);
  2801. rc = cam_sync_signal(
  2802. req_isp->fence_map_out[i].sync_id,
  2803. CAM_SYNC_STATE_SIGNALED_CANCEL,
  2804. CAM_SYNC_ISP_EVENT_FLUSH);
  2805. if (rc) {
  2806. tmp = req_isp->fence_map_out[i].sync_id;
  2807. CAM_ERR_RATE_LIMIT(CAM_ISP,
  2808. "signal fence %d failed", tmp);
  2809. }
  2810. req_isp->fence_map_out[i].sync_id = -1;
  2811. }
  2812. }
  2813. req_isp->reapply = false;
  2814. list_del_init(&req->list);
  2815. list_add_tail(&req->list, &ctx->free_req_list);
  2816. }
  2817. if (flush_req->type == CAM_REQ_MGR_FLUSH_TYPE_CANCEL_REQ &&
  2818. !cancel_req_id_found)
  2819. CAM_DBG(CAM_ISP,
  2820. "Flush request id:%lld is not found in the list",
  2821. flush_req->req_id);
  2822. return 0;
  2823. }
  2824. static int __cam_isp_ctx_flush_req_in_top_state(
  2825. struct cam_context *ctx,
  2826. struct cam_req_mgr_flush_request *flush_req)
  2827. {
  2828. int rc = 0;
  2829. struct cam_isp_context *ctx_isp;
  2830. struct cam_isp_stop_args stop_isp;
  2831. struct cam_hw_stop_args stop_args;
  2832. struct cam_hw_reset_args reset_args;
  2833. struct cam_hw_cmd_args hw_cmd_args;
  2834. struct cam_req_mgr_timer_notify timer;
  2835. ctx_isp = (struct cam_isp_context *) ctx->ctx_priv;
  2836. CAM_DBG(CAM_ISP, "Flush pending list");
  2837. spin_lock_bh(&ctx->lock);
  2838. rc = __cam_isp_ctx_flush_req(ctx, &ctx->pending_req_list, flush_req);
  2839. spin_unlock_bh(&ctx->lock);
  2840. if (flush_req->type == CAM_REQ_MGR_FLUSH_TYPE_ALL) {
  2841. if (ctx->state <= CAM_CTX_READY) {
  2842. ctx->state = CAM_CTX_ACQUIRED;
  2843. goto end;
  2844. }
  2845. spin_lock_bh(&ctx->lock);
  2846. ctx->state = CAM_CTX_FLUSHED;
  2847. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_HALT;
  2848. spin_unlock_bh(&ctx->lock);
  2849. CAM_INFO(CAM_ISP, "Last request id to flush is %lld, ctx_id:%d",
  2850. flush_req->req_id, ctx->ctx_id);
  2851. ctx->last_flush_req = flush_req->req_id;
  2852. memset(&hw_cmd_args, 0, sizeof(hw_cmd_args));
  2853. hw_cmd_args.ctxt_to_hw_map = ctx->ctxt_to_hw_map;
  2854. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_REG_DUMP_ON_FLUSH;
  2855. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  2856. &hw_cmd_args);
  2857. if (rc) {
  2858. CAM_ERR(CAM_ISP, "Reg dump on flush failed rc: %d", rc);
  2859. rc = 0;
  2860. }
  2861. stop_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  2862. stop_isp.hw_stop_cmd = CAM_ISP_HW_STOP_IMMEDIATELY;
  2863. stop_isp.stop_only = true;
  2864. stop_args.args = (void *)&stop_isp;
  2865. rc = ctx->hw_mgr_intf->hw_stop(ctx->hw_mgr_intf->hw_mgr_priv,
  2866. &stop_args);
  2867. if (rc)
  2868. CAM_ERR(CAM_ISP, "Failed to stop HW in Flush rc: %d",
  2869. rc);
  2870. CAM_INFO(CAM_ISP, "Stop HW complete. Reset HW next.");
  2871. CAM_DBG(CAM_ISP, "Flush wait and active lists");
  2872. if (ctx->ctx_crm_intf && ctx->ctx_crm_intf->notify_timer) {
  2873. timer.link_hdl = ctx->link_hdl;
  2874. timer.dev_hdl = ctx->dev_hdl;
  2875. timer.state = false;
  2876. ctx->ctx_crm_intf->notify_timer(&timer);
  2877. }
  2878. spin_lock_bh(&ctx->lock);
  2879. if (!list_empty(&ctx->wait_req_list))
  2880. rc = __cam_isp_ctx_flush_req(ctx, &ctx->wait_req_list,
  2881. flush_req);
  2882. if (!list_empty(&ctx->active_req_list))
  2883. rc = __cam_isp_ctx_flush_req(ctx, &ctx->active_req_list,
  2884. flush_req);
  2885. ctx_isp->active_req_cnt = 0;
  2886. spin_unlock_bh(&ctx->lock);
  2887. reset_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  2888. rc = ctx->hw_mgr_intf->hw_reset(ctx->hw_mgr_intf->hw_mgr_priv,
  2889. &reset_args);
  2890. if (rc)
  2891. CAM_ERR(CAM_ISP, "Failed to reset HW rc: %d", rc);
  2892. ctx_isp->init_received = false;
  2893. }
  2894. end:
  2895. atomic_set(&ctx_isp->process_bubble, 0);
  2896. atomic_set(&ctx_isp->rxd_epoch, 0);
  2897. return rc;
  2898. }
  2899. static int __cam_isp_ctx_flush_req_in_ready(
  2900. struct cam_context *ctx,
  2901. struct cam_req_mgr_flush_request *flush_req)
  2902. {
  2903. int rc = 0;
  2904. CAM_DBG(CAM_ISP, "try to flush pending list");
  2905. spin_lock_bh(&ctx->lock);
  2906. rc = __cam_isp_ctx_flush_req(ctx, &ctx->pending_req_list, flush_req);
  2907. /* if nothing is in pending req list, change state to acquire */
  2908. if (list_empty(&ctx->pending_req_list))
  2909. ctx->state = CAM_CTX_ACQUIRED;
  2910. spin_unlock_bh(&ctx->lock);
  2911. trace_cam_context_state("ISP", ctx);
  2912. CAM_DBG(CAM_ISP, "Flush request in ready state. next state %d",
  2913. ctx->state);
  2914. return rc;
  2915. }
  2916. static struct cam_ctx_ops
  2917. cam_isp_ctx_activated_state_machine[CAM_ISP_CTX_ACTIVATED_MAX] = {
  2918. /* SOF */
  2919. {
  2920. .ioctl_ops = {},
  2921. .crm_ops = {
  2922. .apply_req = __cam_isp_ctx_apply_req_in_sof,
  2923. },
  2924. .irq_ops = NULL,
  2925. },
  2926. /* APPLIED */
  2927. {
  2928. .ioctl_ops = {},
  2929. .crm_ops = {},
  2930. .irq_ops = NULL,
  2931. },
  2932. /* EPOCH */
  2933. {
  2934. .ioctl_ops = {},
  2935. .crm_ops = {
  2936. .apply_req = __cam_isp_ctx_apply_req_in_epoch,
  2937. },
  2938. .irq_ops = NULL,
  2939. },
  2940. /* BUBBLE */
  2941. {
  2942. .ioctl_ops = {},
  2943. .crm_ops = {
  2944. .apply_req = __cam_isp_ctx_apply_req_in_bubble,
  2945. },
  2946. .irq_ops = NULL,
  2947. },
  2948. /* Bubble Applied */
  2949. {
  2950. .ioctl_ops = {},
  2951. .crm_ops = {},
  2952. .irq_ops = NULL,
  2953. },
  2954. /* HW ERROR */
  2955. {
  2956. .ioctl_ops = {},
  2957. .crm_ops = {},
  2958. .irq_ops = NULL,
  2959. },
  2960. /* HALT */
  2961. {
  2962. .ioctl_ops = {},
  2963. .crm_ops = {},
  2964. .irq_ops = NULL,
  2965. },
  2966. };
  2967. static struct cam_ctx_ops
  2968. cam_isp_ctx_fs2_state_machine[CAM_ISP_CTX_ACTIVATED_MAX] = {
  2969. /* SOF */
  2970. {
  2971. .ioctl_ops = {},
  2972. .crm_ops = {
  2973. .apply_req = __cam_isp_ctx_apply_req_in_sof,
  2974. },
  2975. .irq_ops = NULL,
  2976. },
  2977. /* APPLIED */
  2978. {
  2979. .ioctl_ops = {},
  2980. .crm_ops = {},
  2981. .irq_ops = NULL,
  2982. },
  2983. /* EPOCH */
  2984. {
  2985. .ioctl_ops = {},
  2986. .crm_ops = {
  2987. .apply_req = __cam_isp_ctx_apply_req_in_epoch,
  2988. },
  2989. .irq_ops = NULL,
  2990. },
  2991. /* BUBBLE */
  2992. {
  2993. .ioctl_ops = {},
  2994. .crm_ops = {
  2995. .apply_req = __cam_isp_ctx_apply_req_in_bubble,
  2996. },
  2997. .irq_ops = NULL,
  2998. },
  2999. /* Bubble Applied */
  3000. {
  3001. .ioctl_ops = {},
  3002. .crm_ops = {},
  3003. .irq_ops = NULL,
  3004. },
  3005. /* HW ERROR */
  3006. {
  3007. .ioctl_ops = {},
  3008. .crm_ops = {},
  3009. .irq_ops = NULL,
  3010. },
  3011. /* HALT */
  3012. {
  3013. .ioctl_ops = {},
  3014. .crm_ops = {},
  3015. .irq_ops = NULL,
  3016. },
  3017. };
  3018. static int __cam_isp_ctx_rdi_only_sof_in_top_state(
  3019. struct cam_isp_context *ctx_isp, void *evt_data)
  3020. {
  3021. int rc = 0;
  3022. struct cam_context *ctx = ctx_isp->base;
  3023. struct cam_req_mgr_trigger_notify notify;
  3024. struct cam_isp_hw_sof_event_data *sof_event_data = evt_data;
  3025. uint64_t request_id = 0;
  3026. if (!evt_data) {
  3027. CAM_ERR(CAM_ISP, "in valid sof event data");
  3028. return -EINVAL;
  3029. }
  3030. ctx_isp->frame_id++;
  3031. ctx_isp->sof_timestamp_val = sof_event_data->timestamp;
  3032. ctx_isp->boot_timestamp = sof_event_data->boot_time;
  3033. CAM_DBG(CAM_ISP, "frame id: %lld time stamp:0x%llx",
  3034. ctx_isp->frame_id, ctx_isp->sof_timestamp_val);
  3035. /*
  3036. * notify reqmgr with sof signal. Note, due to scheduling delay
  3037. * we can run into situation that two active requests has already
  3038. * be in the active queue while we try to do the notification.
  3039. * In this case, we need to skip the current notification. This
  3040. * helps the state machine to catch up the delay.
  3041. */
  3042. if (ctx->ctx_crm_intf && ctx->ctx_crm_intf->notify_trigger &&
  3043. ctx_isp->active_req_cnt <= 2) {
  3044. notify.link_hdl = ctx->link_hdl;
  3045. notify.dev_hdl = ctx->dev_hdl;
  3046. notify.frame_id = ctx_isp->frame_id;
  3047. notify.trigger = CAM_TRIGGER_POINT_SOF;
  3048. notify.req_id = ctx_isp->req_info.last_bufdone_req_id;
  3049. notify.sof_timestamp_val = ctx_isp->sof_timestamp_val;
  3050. ctx->ctx_crm_intf->notify_trigger(&notify);
  3051. CAM_DBG(CAM_ISP, "Notify CRM SOF frame %lld",
  3052. ctx_isp->frame_id);
  3053. /*
  3054. * It is idle frame with out any applied request id, send
  3055. * request id as zero
  3056. */
  3057. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  3058. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  3059. } else {
  3060. CAM_ERR_RATE_LIMIT(CAM_ISP, "Can not notify SOF to CRM");
  3061. }
  3062. if (list_empty(&ctx->active_req_list))
  3063. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_SOF;
  3064. else
  3065. CAM_DBG(CAM_ISP, "Still need to wait for the buf done");
  3066. CAM_DBG(CAM_ISP, "next Substate[%s]",
  3067. __cam_isp_ctx_substate_val_to_type(
  3068. ctx_isp->substate_activated));
  3069. return rc;
  3070. }
  3071. static int __cam_isp_ctx_rdi_only_sof_in_applied_state(
  3072. struct cam_isp_context *ctx_isp, void *evt_data)
  3073. {
  3074. struct cam_isp_hw_sof_event_data *sof_event_data = evt_data;
  3075. if (!evt_data) {
  3076. CAM_ERR(CAM_ISP, "in valid sof event data");
  3077. return -EINVAL;
  3078. }
  3079. ctx_isp->frame_id++;
  3080. ctx_isp->sof_timestamp_val = sof_event_data->timestamp;
  3081. ctx_isp->boot_timestamp = sof_event_data->boot_time;
  3082. CAM_DBG(CAM_ISP, "frame id: %lld time stamp:0x%llx",
  3083. ctx_isp->frame_id, ctx_isp->sof_timestamp_val);
  3084. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_BUBBLE_APPLIED;
  3085. CAM_DBG(CAM_ISP, "next Substate[%s]",
  3086. __cam_isp_ctx_substate_val_to_type(
  3087. ctx_isp->substate_activated));
  3088. return 0;
  3089. }
  3090. static int __cam_isp_ctx_rdi_only_sof_in_bubble_applied(
  3091. struct cam_isp_context *ctx_isp, void *evt_data)
  3092. {
  3093. struct cam_ctx_request *req;
  3094. struct cam_isp_ctx_req *req_isp;
  3095. struct cam_context *ctx = ctx_isp->base;
  3096. struct cam_isp_hw_sof_event_data *sof_event_data = evt_data;
  3097. uint64_t request_id = 0;
  3098. /*
  3099. * Sof in bubble applied state means, reg update not received.
  3100. * before increment frame id and override time stamp value, send
  3101. * the previous sof time stamp that got captured in the
  3102. * sof in applied state.
  3103. */
  3104. CAM_DBG(CAM_ISP, "frame id: %lld time stamp:0x%llx",
  3105. ctx_isp->frame_id, ctx_isp->sof_timestamp_val);
  3106. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  3107. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  3108. ctx_isp->frame_id++;
  3109. ctx_isp->sof_timestamp_val = sof_event_data->timestamp;
  3110. ctx_isp->boot_timestamp = sof_event_data->boot_time;
  3111. CAM_DBG(CAM_ISP, "frame id: %lld time stamp:0x%llx",
  3112. ctx_isp->frame_id, ctx_isp->sof_timestamp_val);
  3113. if (list_empty(&ctx->wait_req_list)) {
  3114. /*
  3115. * If no pending req in epoch, this is an error case.
  3116. * The recovery is to go back to sof state
  3117. */
  3118. CAM_ERR(CAM_ISP, "No wait request");
  3119. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_SOF;
  3120. /* Send SOF event as empty frame*/
  3121. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  3122. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  3123. goto end;
  3124. }
  3125. req = list_first_entry(&ctx->wait_req_list, struct cam_ctx_request,
  3126. list);
  3127. req_isp = (struct cam_isp_ctx_req *)req->req_priv;
  3128. req_isp->bubble_detected = true;
  3129. CAM_INFO(CAM_ISP, "Ctx:%d Report Bubble flag %d req id:%lld",
  3130. ctx->ctx_id, req_isp->bubble_report, req->request_id);
  3131. req_isp->reapply = true;
  3132. if (req_isp->bubble_report && ctx->ctx_crm_intf &&
  3133. ctx->ctx_crm_intf->notify_err) {
  3134. struct cam_req_mgr_error_notify notify;
  3135. notify.link_hdl = ctx->link_hdl;
  3136. notify.dev_hdl = ctx->dev_hdl;
  3137. notify.req_id = req->request_id;
  3138. notify.error = CRM_KMD_ERR_BUBBLE;
  3139. notify.trigger = 0;
  3140. if (ctx_isp->subscribe_event & CAM_TRIGGER_POINT_SOF)
  3141. notify.trigger = CAM_TRIGGER_POINT_SOF;
  3142. notify.frame_id = ctx_isp->frame_id;
  3143. notify.sof_timestamp_val = ctx_isp->sof_timestamp_val;
  3144. CAM_WARN(CAM_ISP,
  3145. "Notify CRM about Bubble req %lld frame %lld ctx %u",
  3146. req->request_id,
  3147. ctx_isp->frame_id,
  3148. ctx->ctx_id);
  3149. ctx->ctx_crm_intf->notify_err(&notify);
  3150. } else {
  3151. req_isp->bubble_report = 0;
  3152. }
  3153. /*
  3154. * Always move the request to active list. Let buf done
  3155. * function handles the rest.
  3156. */
  3157. list_del_init(&req->list);
  3158. list_add_tail(&req->list, &ctx->active_req_list);
  3159. ctx_isp->active_req_cnt++;
  3160. CAM_DBG(CAM_ISP, "move request %lld to active list(cnt = %d)",
  3161. req->request_id, ctx_isp->active_req_cnt);
  3162. if (!req_isp->bubble_report) {
  3163. if (req->request_id > ctx_isp->reported_req_id) {
  3164. request_id = req->request_id;
  3165. ctx_isp->reported_req_id = request_id;
  3166. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  3167. CAM_REQ_MGR_SOF_EVENT_ERROR);
  3168. } else
  3169. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  3170. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  3171. } else
  3172. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  3173. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  3174. /* change the state to bubble, as reg update has not come */
  3175. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_BUBBLE;
  3176. CAM_DBG(CAM_ISP, "next Substate[%s]",
  3177. __cam_isp_ctx_substate_val_to_type(
  3178. ctx_isp->substate_activated));
  3179. end:
  3180. return 0;
  3181. }
  3182. static int __cam_isp_ctx_rdi_only_sof_in_bubble_state(
  3183. struct cam_isp_context *ctx_isp, void *evt_data)
  3184. {
  3185. uint32_t i;
  3186. struct cam_ctx_request *req;
  3187. struct cam_context *ctx = ctx_isp->base;
  3188. struct cam_req_mgr_trigger_notify notify;
  3189. struct cam_isp_hw_sof_event_data *sof_event_data = evt_data;
  3190. struct cam_isp_ctx_req *req_isp;
  3191. uint64_t request_id = 0;
  3192. if (!evt_data) {
  3193. CAM_ERR(CAM_ISP, "in valid sof event data");
  3194. return -EINVAL;
  3195. }
  3196. ctx_isp->frame_id++;
  3197. ctx_isp->sof_timestamp_val = sof_event_data->timestamp;
  3198. ctx_isp->boot_timestamp = sof_event_data->boot_time;
  3199. CAM_DBG(CAM_ISP, "frame id: %lld time stamp:0x%llx",
  3200. ctx_isp->frame_id, ctx_isp->sof_timestamp_val);
  3201. /*
  3202. * Signal all active requests with error and move the all the active
  3203. * requests to free list
  3204. */
  3205. while (!list_empty(&ctx->active_req_list)) {
  3206. req = list_first_entry(&ctx->active_req_list,
  3207. struct cam_ctx_request, list);
  3208. list_del_init(&req->list);
  3209. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  3210. CAM_DBG(CAM_ISP, "signal fence in active list. fence num %d",
  3211. req_isp->num_fence_map_out);
  3212. for (i = 0; i < req_isp->num_fence_map_out; i++)
  3213. if (req_isp->fence_map_out[i].sync_id != -1) {
  3214. cam_sync_signal(
  3215. req_isp->fence_map_out[i].sync_id,
  3216. CAM_SYNC_STATE_SIGNALED_ERROR,
  3217. CAM_SYNC_ISP_EVENT_BUBBLE);
  3218. }
  3219. list_add_tail(&req->list, &ctx->free_req_list);
  3220. ctx_isp->active_req_cnt--;
  3221. }
  3222. /* notify reqmgr with sof signal */
  3223. if (ctx->ctx_crm_intf && ctx->ctx_crm_intf->notify_trigger) {
  3224. notify.link_hdl = ctx->link_hdl;
  3225. notify.dev_hdl = ctx->dev_hdl;
  3226. notify.frame_id = ctx_isp->frame_id;
  3227. notify.trigger = CAM_TRIGGER_POINT_SOF;
  3228. notify.req_id = ctx_isp->req_info.last_bufdone_req_id;
  3229. notify.sof_timestamp_val = ctx_isp->sof_timestamp_val;
  3230. ctx->ctx_crm_intf->notify_trigger(&notify);
  3231. CAM_DBG(CAM_ISP, "Notify CRM SOF frame %lld",
  3232. ctx_isp->frame_id);
  3233. } else {
  3234. CAM_ERR(CAM_ISP, "Can not notify SOF to CRM");
  3235. }
  3236. /*
  3237. * It is idle frame with out any applied request id, send
  3238. * request id as zero
  3239. */
  3240. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  3241. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  3242. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_SOF;
  3243. CAM_DBG(CAM_ISP, "next Substate[%s]",
  3244. __cam_isp_ctx_substate_val_to_type(
  3245. ctx_isp->substate_activated));
  3246. return 0;
  3247. }
  3248. static int __cam_isp_ctx_rdi_only_reg_upd_in_bubble_state(
  3249. struct cam_isp_context *ctx_isp, void *evt_data)
  3250. {
  3251. struct cam_ctx_request *req = NULL;
  3252. struct cam_context *ctx = ctx_isp->base;
  3253. req = list_first_entry(&ctx->active_req_list,
  3254. struct cam_ctx_request, list);
  3255. CAM_INFO(CAM_ISP, "Received RUP for Bubble Request", req->request_id);
  3256. return 0;
  3257. }
  3258. static int __cam_isp_ctx_rdi_only_reg_upd_in_bubble_applied_state(
  3259. struct cam_isp_context *ctx_isp, void *evt_data)
  3260. {
  3261. struct cam_ctx_request *req = NULL;
  3262. struct cam_context *ctx = ctx_isp->base;
  3263. struct cam_isp_ctx_req *req_isp;
  3264. struct cam_req_mgr_trigger_notify notify;
  3265. uint64_t request_id = 0;
  3266. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_EPOCH;
  3267. /* notify reqmgr with sof signal*/
  3268. if (ctx->ctx_crm_intf && ctx->ctx_crm_intf->notify_trigger) {
  3269. if (list_empty(&ctx->wait_req_list)) {
  3270. CAM_ERR(CAM_ISP, "Reg upd ack with no waiting request");
  3271. goto error;
  3272. }
  3273. req = list_first_entry(&ctx->wait_req_list,
  3274. struct cam_ctx_request, list);
  3275. list_del_init(&req->list);
  3276. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  3277. request_id =
  3278. (req_isp->hw_update_data.packet_opcode_type ==
  3279. CAM_ISP_PACKET_INIT_DEV) ?
  3280. 0 : req->request_id;
  3281. if (req_isp->num_fence_map_out != 0) {
  3282. list_add_tail(&req->list, &ctx->active_req_list);
  3283. ctx_isp->active_req_cnt++;
  3284. CAM_DBG(CAM_ISP,
  3285. "move request %lld to active list(cnt = %d)",
  3286. req->request_id, ctx_isp->active_req_cnt);
  3287. /* if packet has buffers, set correct request id */
  3288. request_id = req->request_id;
  3289. } else {
  3290. /* no io config, so the request is completed. */
  3291. list_add_tail(&req->list, &ctx->free_req_list);
  3292. CAM_DBG(CAM_ISP,
  3293. "move active req %lld to free list(cnt=%d)",
  3294. req->request_id, ctx_isp->active_req_cnt);
  3295. }
  3296. notify.link_hdl = ctx->link_hdl;
  3297. notify.dev_hdl = ctx->dev_hdl;
  3298. notify.frame_id = ctx_isp->frame_id;
  3299. notify.trigger = CAM_TRIGGER_POINT_SOF;
  3300. notify.req_id = ctx_isp->req_info.last_bufdone_req_id;
  3301. notify.sof_timestamp_val = ctx_isp->sof_timestamp_val;
  3302. ctx->ctx_crm_intf->notify_trigger(&notify);
  3303. CAM_DBG(CAM_ISP, "Notify CRM SOF frame %lld",
  3304. ctx_isp->frame_id);
  3305. } else {
  3306. CAM_ERR(CAM_ISP, "Can not notify SOF to CRM");
  3307. }
  3308. if (request_id)
  3309. ctx_isp->reported_req_id = request_id;
  3310. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  3311. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  3312. CAM_DBG(CAM_ISP, "next Substate[%s]",
  3313. __cam_isp_ctx_substate_val_to_type(
  3314. ctx_isp->substate_activated));
  3315. __cam_isp_ctx_update_event_record(ctx_isp,
  3316. CAM_ISP_CTX_EVENT_RUP, req);
  3317. return 0;
  3318. error:
  3319. /* Send SOF event as idle frame*/
  3320. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  3321. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  3322. __cam_isp_ctx_update_event_record(ctx_isp,
  3323. CAM_ISP_CTX_EVENT_RUP, NULL);
  3324. /*
  3325. * There is no request in the pending list, move the sub state machine
  3326. * to SOF sub state
  3327. */
  3328. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_SOF;
  3329. return 0;
  3330. }
  3331. static struct cam_isp_ctx_irq_ops
  3332. cam_isp_ctx_rdi_only_activated_state_machine_irq
  3333. [CAM_ISP_CTX_ACTIVATED_MAX] = {
  3334. /* SOF */
  3335. {
  3336. .irq_ops = {
  3337. NULL,
  3338. __cam_isp_ctx_rdi_only_sof_in_top_state,
  3339. __cam_isp_ctx_reg_upd_in_sof,
  3340. NULL,
  3341. NULL,
  3342. NULL,
  3343. },
  3344. },
  3345. /* APPLIED */
  3346. {
  3347. .irq_ops = {
  3348. __cam_isp_ctx_handle_error,
  3349. __cam_isp_ctx_rdi_only_sof_in_applied_state,
  3350. NULL,
  3351. NULL,
  3352. NULL,
  3353. __cam_isp_ctx_buf_done_in_applied,
  3354. },
  3355. },
  3356. /* EPOCH */
  3357. {
  3358. .irq_ops = {
  3359. __cam_isp_ctx_handle_error,
  3360. __cam_isp_ctx_rdi_only_sof_in_top_state,
  3361. NULL,
  3362. NULL,
  3363. NULL,
  3364. __cam_isp_ctx_buf_done_in_epoch,
  3365. },
  3366. },
  3367. /* BUBBLE*/
  3368. {
  3369. .irq_ops = {
  3370. __cam_isp_ctx_handle_error,
  3371. __cam_isp_ctx_rdi_only_sof_in_bubble_state,
  3372. __cam_isp_ctx_rdi_only_reg_upd_in_bubble_state,
  3373. NULL,
  3374. NULL,
  3375. __cam_isp_ctx_buf_done_in_bubble,
  3376. },
  3377. },
  3378. /* BUBBLE APPLIED ie PRE_BUBBLE */
  3379. {
  3380. .irq_ops = {
  3381. __cam_isp_ctx_handle_error,
  3382. __cam_isp_ctx_rdi_only_sof_in_bubble_applied,
  3383. __cam_isp_ctx_rdi_only_reg_upd_in_bubble_applied_state,
  3384. NULL,
  3385. NULL,
  3386. __cam_isp_ctx_buf_done_in_bubble_applied,
  3387. },
  3388. },
  3389. /* HW ERROR */
  3390. {
  3391. },
  3392. /* HALT */
  3393. {
  3394. },
  3395. };
  3396. static int __cam_isp_ctx_rdi_only_apply_req_top_state(
  3397. struct cam_context *ctx, struct cam_req_mgr_apply_request *apply)
  3398. {
  3399. int rc = 0;
  3400. struct cam_isp_context *ctx_isp =
  3401. (struct cam_isp_context *) ctx->ctx_priv;
  3402. CAM_DBG(CAM_ISP, "current Substate[%s]",
  3403. __cam_isp_ctx_substate_val_to_type(
  3404. ctx_isp->substate_activated));
  3405. rc = __cam_isp_ctx_apply_req_in_activated_state(ctx, apply,
  3406. CAM_ISP_CTX_ACTIVATED_APPLIED);
  3407. CAM_DBG(CAM_ISP, "new Substate[%s]",
  3408. __cam_isp_ctx_substate_val_to_type(
  3409. ctx_isp->substate_activated));
  3410. if (rc)
  3411. CAM_ERR(CAM_ISP, "Apply failed in Substate[%s], rc %d",
  3412. __cam_isp_ctx_substate_val_to_type(
  3413. ctx_isp->substate_activated), rc);
  3414. return rc;
  3415. }
  3416. static struct cam_ctx_ops
  3417. cam_isp_ctx_rdi_only_activated_state_machine
  3418. [CAM_ISP_CTX_ACTIVATED_MAX] = {
  3419. /* SOF */
  3420. {
  3421. .ioctl_ops = {},
  3422. .crm_ops = {
  3423. .apply_req = __cam_isp_ctx_rdi_only_apply_req_top_state,
  3424. },
  3425. .irq_ops = NULL,
  3426. },
  3427. /* APPLIED */
  3428. {
  3429. .ioctl_ops = {},
  3430. .crm_ops = {},
  3431. .irq_ops = NULL,
  3432. },
  3433. /* EPOCH */
  3434. {
  3435. .ioctl_ops = {},
  3436. .crm_ops = {
  3437. .apply_req = __cam_isp_ctx_rdi_only_apply_req_top_state,
  3438. },
  3439. .irq_ops = NULL,
  3440. },
  3441. /* PRE BUBBLE */
  3442. {
  3443. .ioctl_ops = {},
  3444. .crm_ops = {},
  3445. .irq_ops = NULL,
  3446. },
  3447. /* BUBBLE */
  3448. {
  3449. .ioctl_ops = {},
  3450. .crm_ops = {},
  3451. .irq_ops = NULL,
  3452. },
  3453. /* HW ERROR */
  3454. {
  3455. .ioctl_ops = {},
  3456. .crm_ops = {},
  3457. .irq_ops = NULL,
  3458. },
  3459. /* HALT */
  3460. {
  3461. .ioctl_ops = {},
  3462. .crm_ops = {},
  3463. .irq_ops = NULL,
  3464. },
  3465. };
  3466. static int __cam_isp_ctx_release_hw_in_top_state(struct cam_context *ctx,
  3467. void *cmd)
  3468. {
  3469. int rc = 0;
  3470. struct cam_hw_release_args rel_arg;
  3471. struct cam_isp_context *ctx_isp =
  3472. (struct cam_isp_context *) ctx->ctx_priv;
  3473. struct cam_req_mgr_flush_request flush_req;
  3474. int i;
  3475. if (ctx_isp->hw_ctx) {
  3476. rel_arg.ctxt_to_hw_map = ctx_isp->hw_ctx;
  3477. ctx->hw_mgr_intf->hw_release(ctx->hw_mgr_intf->hw_mgr_priv,
  3478. &rel_arg);
  3479. ctx_isp->hw_ctx = NULL;
  3480. } else {
  3481. CAM_ERR(CAM_ISP, "No hw resources acquired for this ctx");
  3482. }
  3483. ctx->last_flush_req = 0;
  3484. ctx_isp->custom_enabled = false;
  3485. ctx_isp->use_frame_header_ts = false;
  3486. ctx_isp->frame_id = 0;
  3487. ctx_isp->active_req_cnt = 0;
  3488. ctx_isp->reported_req_id = 0;
  3489. ctx_isp->hw_acquired = false;
  3490. ctx_isp->init_received = false;
  3491. ctx_isp->support_consumed_addr = false;
  3492. ctx_isp->req_info.last_bufdone_req_id = 0;
  3493. atomic64_set(&ctx_isp->state_monitor_head, -1);
  3494. for (i = 0; i < CAM_ISP_CTX_EVENT_MAX; i++)
  3495. atomic64_set(&ctx_isp->event_record_head[i], -1);
  3496. /*
  3497. * Ideally, we should never have any active request here.
  3498. * But we still add some sanity check code here to help the debug
  3499. */
  3500. if (!list_empty(&ctx->active_req_list))
  3501. CAM_WARN(CAM_ISP, "Active list is not empty");
  3502. /* Flush all the pending request list */
  3503. flush_req.type = CAM_REQ_MGR_FLUSH_TYPE_ALL;
  3504. flush_req.link_hdl = ctx->link_hdl;
  3505. flush_req.dev_hdl = ctx->dev_hdl;
  3506. flush_req.req_id = 0;
  3507. CAM_DBG(CAM_ISP, "try to flush pending list");
  3508. spin_lock_bh(&ctx->lock);
  3509. rc = __cam_isp_ctx_flush_req(ctx, &ctx->pending_req_list, &flush_req);
  3510. spin_unlock_bh(&ctx->lock);
  3511. ctx->state = CAM_CTX_ACQUIRED;
  3512. trace_cam_context_state("ISP", ctx);
  3513. CAM_DBG(CAM_ISP, "Release device success[%u] next state %d",
  3514. ctx->ctx_id, ctx->state);
  3515. return rc;
  3516. }
  3517. /* top level state machine */
  3518. static int __cam_isp_ctx_release_dev_in_top_state(struct cam_context *ctx,
  3519. struct cam_release_dev_cmd *cmd)
  3520. {
  3521. int rc = 0;
  3522. int i;
  3523. struct cam_hw_release_args rel_arg;
  3524. struct cam_isp_context *ctx_isp =
  3525. (struct cam_isp_context *) ctx->ctx_priv;
  3526. struct cam_req_mgr_flush_request flush_req;
  3527. if (cmd && ctx_isp->hw_ctx) {
  3528. CAM_ERR(CAM_ISP, "releasing hw");
  3529. __cam_isp_ctx_release_hw_in_top_state(ctx, NULL);
  3530. }
  3531. if (ctx_isp->hw_ctx) {
  3532. rel_arg.ctxt_to_hw_map = ctx_isp->hw_ctx;
  3533. ctx->hw_mgr_intf->hw_release(ctx->hw_mgr_intf->hw_mgr_priv,
  3534. &rel_arg);
  3535. ctx_isp->hw_ctx = NULL;
  3536. }
  3537. ctx->session_hdl = -1;
  3538. ctx->dev_hdl = -1;
  3539. ctx->link_hdl = -1;
  3540. ctx->ctx_crm_intf = NULL;
  3541. ctx->last_flush_req = 0;
  3542. ctx_isp->frame_id = 0;
  3543. ctx_isp->active_req_cnt = 0;
  3544. ctx_isp->reported_req_id = 0;
  3545. ctx_isp->hw_acquired = false;
  3546. ctx_isp->init_received = false;
  3547. ctx_isp->offline_context = false;
  3548. ctx_isp->rdi_only_context = false;
  3549. ctx_isp->req_info.last_bufdone_req_id = 0;
  3550. atomic64_set(&ctx_isp->state_monitor_head, -1);
  3551. for (i = 0; i < CAM_ISP_CTX_EVENT_MAX; i++)
  3552. atomic64_set(&ctx_isp->event_record_head[i], -1);
  3553. /*
  3554. * Ideally, we should never have any active request here.
  3555. * But we still add some sanity check code here to help the debug
  3556. */
  3557. if (!list_empty(&ctx->active_req_list))
  3558. CAM_ERR(CAM_ISP, "Active list is not empty");
  3559. /* Flush all the pending request list */
  3560. flush_req.type = CAM_REQ_MGR_FLUSH_TYPE_ALL;
  3561. flush_req.link_hdl = ctx->link_hdl;
  3562. flush_req.dev_hdl = ctx->dev_hdl;
  3563. flush_req.req_id = 0;
  3564. CAM_DBG(CAM_ISP, "try to flush pending list");
  3565. spin_lock_bh(&ctx->lock);
  3566. rc = __cam_isp_ctx_flush_req(ctx, &ctx->pending_req_list, &flush_req);
  3567. spin_unlock_bh(&ctx->lock);
  3568. ctx->state = CAM_CTX_AVAILABLE;
  3569. trace_cam_context_state("ISP", ctx);
  3570. CAM_DBG(CAM_ISP, "Release device success[%u] next state %d",
  3571. ctx->ctx_id, ctx->state);
  3572. return rc;
  3573. }
  3574. static int __cam_isp_ctx_config_dev_in_top_state(
  3575. struct cam_context *ctx, struct cam_config_dev_cmd *cmd)
  3576. {
  3577. int rc = 0, i;
  3578. struct cam_ctx_request *req = NULL;
  3579. struct cam_isp_ctx_req *req_isp;
  3580. uintptr_t packet_addr;
  3581. struct cam_packet *packet;
  3582. size_t len = 0;
  3583. size_t remain_len = 0;
  3584. struct cam_hw_prepare_update_args cfg = {0};
  3585. struct cam_req_mgr_add_request add_req;
  3586. struct cam_isp_context *ctx_isp =
  3587. (struct cam_isp_context *) ctx->ctx_priv;
  3588. struct cam_hw_cmd_args hw_cmd_args;
  3589. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  3590. uint32_t packet_opcode = 0;
  3591. CAM_DBG(CAM_ISP, "get free request object......");
  3592. /* get free request */
  3593. spin_lock_bh(&ctx->lock);
  3594. if (!list_empty(&ctx->free_req_list)) {
  3595. req = list_first_entry(&ctx->free_req_list,
  3596. struct cam_ctx_request, list);
  3597. list_del_init(&req->list);
  3598. }
  3599. spin_unlock_bh(&ctx->lock);
  3600. if (!req) {
  3601. CAM_ERR(CAM_ISP, "No more request obj free");
  3602. return -ENOMEM;
  3603. }
  3604. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  3605. /* for config dev, only memory handle is supported */
  3606. /* map packet from the memhandle */
  3607. rc = cam_mem_get_cpu_buf((int32_t) cmd->packet_handle,
  3608. &packet_addr, &len);
  3609. if (rc != 0) {
  3610. CAM_ERR(CAM_ISP, "Can not get packet address");
  3611. rc = -EINVAL;
  3612. goto free_req;
  3613. }
  3614. remain_len = len;
  3615. if ((len < sizeof(struct cam_packet)) ||
  3616. ((size_t)cmd->offset >= len - sizeof(struct cam_packet))) {
  3617. CAM_ERR(CAM_ISP, "invalid buff length: %zu or offset", len);
  3618. rc = -EINVAL;
  3619. goto free_req;
  3620. }
  3621. remain_len -= (size_t)cmd->offset;
  3622. packet = (struct cam_packet *)(packet_addr + (uint32_t)cmd->offset);
  3623. CAM_DBG(CAM_ISP, "pack_handle %llx", cmd->packet_handle);
  3624. CAM_DBG(CAM_ISP, "packet address is 0x%zx", packet_addr);
  3625. CAM_DBG(CAM_ISP, "packet with length %zu, offset 0x%llx",
  3626. len, cmd->offset);
  3627. CAM_DBG(CAM_ISP, "Packet request id %lld",
  3628. packet->header.request_id);
  3629. CAM_DBG(CAM_ISP, "Packet size 0x%x", packet->header.size);
  3630. CAM_DBG(CAM_ISP, "packet op %d", packet->header.op_code);
  3631. /* Query the packet opcode */
  3632. hw_cmd_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  3633. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  3634. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_GET_PACKET_OPCODE;
  3635. isp_hw_cmd_args.cmd_data = (void *)packet;
  3636. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  3637. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  3638. &hw_cmd_args);
  3639. if (rc) {
  3640. CAM_ERR(CAM_ISP, "HW command failed");
  3641. goto free_req;
  3642. }
  3643. packet_opcode = isp_hw_cmd_args.u.packet_op_code;
  3644. CAM_DBG(CAM_ISP, "packet op %d", packet_opcode);
  3645. if ((packet_opcode == CAM_ISP_PACKET_UPDATE_DEV)
  3646. && (packet->header.request_id <= ctx->last_flush_req)) {
  3647. CAM_INFO(CAM_ISP,
  3648. "request %lld has been flushed, reject packet",
  3649. packet->header.request_id);
  3650. rc = -EBADR;
  3651. goto free_req;
  3652. }
  3653. cfg.packet = packet;
  3654. cfg.remain_len = remain_len;
  3655. cfg.ctxt_to_hw_map = ctx_isp->hw_ctx;
  3656. cfg.max_hw_update_entries = CAM_ISP_CTX_CFG_MAX;
  3657. cfg.hw_update_entries = req_isp->cfg;
  3658. cfg.max_out_map_entries = CAM_ISP_CTX_RES_MAX;
  3659. cfg.max_in_map_entries = CAM_ISP_CTX_RES_MAX;
  3660. cfg.out_map_entries = req_isp->fence_map_out;
  3661. cfg.in_map_entries = req_isp->fence_map_in;
  3662. cfg.priv = &req_isp->hw_update_data;
  3663. cfg.pf_data = &(req->pf_data);
  3664. CAM_DBG(CAM_ISP, "try to prepare config packet......");
  3665. rc = ctx->hw_mgr_intf->hw_prepare_update(
  3666. ctx->hw_mgr_intf->hw_mgr_priv, &cfg);
  3667. if (rc != 0) {
  3668. CAM_ERR(CAM_ISP, "Prepare config packet failed in HW layer");
  3669. rc = -EFAULT;
  3670. goto free_req;
  3671. }
  3672. req_isp->num_cfg = cfg.num_hw_update_entries;
  3673. req_isp->num_fence_map_out = cfg.num_out_map_entries;
  3674. req_isp->num_fence_map_in = cfg.num_in_map_entries;
  3675. req_isp->num_acked = 0;
  3676. req_isp->bubble_detected = false;
  3677. for (i = 0; i < req_isp->num_fence_map_out; i++) {
  3678. rc = cam_sync_get_obj_ref(req_isp->fence_map_out[i].sync_id);
  3679. if (rc) {
  3680. CAM_ERR(CAM_ISP, "Can't get ref for fence %d",
  3681. req_isp->fence_map_out[i].sync_id);
  3682. goto put_ref;
  3683. }
  3684. }
  3685. CAM_DBG(CAM_ISP, "num_entry: %d, num fence out: %d, num fence in: %d",
  3686. req_isp->num_cfg, req_isp->num_fence_map_out,
  3687. req_isp->num_fence_map_in);
  3688. req->request_id = packet->header.request_id;
  3689. req->status = 1;
  3690. CAM_DBG(CAM_ISP, "Packet request id %lld packet opcode:%d",
  3691. packet->header.request_id,
  3692. req_isp->hw_update_data.packet_opcode_type);
  3693. if (req_isp->hw_update_data.packet_opcode_type ==
  3694. CAM_ISP_PACKET_INIT_DEV) {
  3695. if (ctx->state < CAM_CTX_ACTIVATED) {
  3696. rc = __cam_isp_ctx_enqueue_init_request(ctx, req);
  3697. if (rc)
  3698. CAM_ERR(CAM_ISP, "Enqueue INIT pkt failed");
  3699. ctx_isp->init_received = true;
  3700. } else {
  3701. rc = -EINVAL;
  3702. CAM_ERR(CAM_ISP, "Recevied INIT pkt in wrong state:%d",
  3703. ctx->state);
  3704. }
  3705. } else {
  3706. if (ctx_isp->offline_context) {
  3707. __cam_isp_ctx_enqueue_request_in_order(ctx, req);
  3708. } else if ((ctx->state != CAM_CTX_FLUSHED) &&
  3709. (ctx->state >= CAM_CTX_READY) &&
  3710. ctx->ctx_crm_intf->add_req) {
  3711. add_req.link_hdl = ctx->link_hdl;
  3712. add_req.dev_hdl = ctx->dev_hdl;
  3713. add_req.req_id = req->request_id;
  3714. add_req.skip_before_applying = 0;
  3715. add_req.trigger_eof = false;
  3716. rc = ctx->ctx_crm_intf->add_req(&add_req);
  3717. if (rc) {
  3718. CAM_ERR(CAM_ISP, "Add req failed: req id=%llu",
  3719. req->request_id);
  3720. } else {
  3721. __cam_isp_ctx_enqueue_request_in_order(
  3722. ctx, req);
  3723. }
  3724. } else {
  3725. rc = -EINVAL;
  3726. CAM_ERR(CAM_ISP,
  3727. "Recevied update req %lld in wrong state:%d",
  3728. req->request_id, ctx->state);
  3729. }
  3730. }
  3731. if (rc)
  3732. goto put_ref;
  3733. CAM_DBG(CAM_REQ,
  3734. "Preprocessing Config req_id %lld successful on ctx %u",
  3735. req->request_id, ctx->ctx_id);
  3736. if (ctx_isp->offline_context && atomic_read(&ctx_isp->rxd_epoch)) {
  3737. __cam_isp_ctx_schedule_apply_req_offline(ctx_isp);
  3738. }
  3739. return rc;
  3740. put_ref:
  3741. for (--i; i >= 0; i--) {
  3742. if (cam_sync_put_obj_ref(req_isp->fence_map_out[i].sync_id))
  3743. CAM_ERR(CAM_CTXT, "Failed to put ref of fence %d",
  3744. req_isp->fence_map_out[i].sync_id);
  3745. }
  3746. free_req:
  3747. spin_lock_bh(&ctx->lock);
  3748. list_add_tail(&req->list, &ctx->free_req_list);
  3749. spin_unlock_bh(&ctx->lock);
  3750. return rc;
  3751. }
  3752. static int __cam_isp_ctx_acquire_dev_in_available(struct cam_context *ctx,
  3753. struct cam_acquire_dev_cmd *cmd)
  3754. {
  3755. int rc = 0;
  3756. int i;
  3757. struct cam_hw_acquire_args param;
  3758. struct cam_isp_resource *isp_res = NULL;
  3759. struct cam_create_dev_hdl req_hdl_param;
  3760. struct cam_hw_release_args release;
  3761. struct cam_isp_context *ctx_isp =
  3762. (struct cam_isp_context *) ctx->ctx_priv;
  3763. struct cam_hw_cmd_args hw_cmd_args;
  3764. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  3765. if (!ctx->hw_mgr_intf) {
  3766. CAM_ERR(CAM_ISP, "HW interface is not ready");
  3767. rc = -EFAULT;
  3768. goto end;
  3769. }
  3770. CAM_DBG(CAM_ISP,
  3771. "session_hdl 0x%x, num_resources %d, hdl type %d, res %lld",
  3772. cmd->session_handle, cmd->num_resources,
  3773. cmd->handle_type, cmd->resource_hdl);
  3774. if (cmd->num_resources == CAM_API_COMPAT_CONSTANT) {
  3775. ctx_isp->split_acquire = true;
  3776. CAM_DBG(CAM_ISP, "Acquire dev handle");
  3777. goto get_dev_handle;
  3778. }
  3779. if (cmd->num_resources > CAM_ISP_CTX_RES_MAX) {
  3780. CAM_ERR(CAM_ISP, "Too much resources in the acquire");
  3781. rc = -ENOMEM;
  3782. goto end;
  3783. }
  3784. /* for now we only support user pointer */
  3785. if (cmd->handle_type != 1) {
  3786. CAM_ERR(CAM_ISP, "Only user pointer is supported");
  3787. rc = -EINVAL;
  3788. goto end;
  3789. }
  3790. isp_res = kzalloc(
  3791. sizeof(*isp_res)*cmd->num_resources, GFP_KERNEL);
  3792. if (!isp_res) {
  3793. rc = -ENOMEM;
  3794. goto end;
  3795. }
  3796. CAM_DBG(CAM_ISP, "start copy %d resources from user",
  3797. cmd->num_resources);
  3798. if (copy_from_user(isp_res, u64_to_user_ptr(cmd->resource_hdl),
  3799. sizeof(*isp_res)*cmd->num_resources)) {
  3800. rc = -EFAULT;
  3801. goto free_res;
  3802. }
  3803. param.context_data = ctx;
  3804. param.event_cb = ctx->irq_cb_intf;
  3805. param.num_acq = cmd->num_resources;
  3806. param.acquire_info = (uintptr_t) isp_res;
  3807. /* call HW manager to reserve the resource */
  3808. rc = ctx->hw_mgr_intf->hw_acquire(ctx->hw_mgr_intf->hw_mgr_priv,
  3809. &param);
  3810. if (rc != 0) {
  3811. CAM_ERR(CAM_ISP, "Acquire device failed");
  3812. goto free_res;
  3813. }
  3814. /* Query the context has rdi only resource */
  3815. hw_cmd_args.ctxt_to_hw_map = param.ctxt_to_hw_map;
  3816. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  3817. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_CMD_CTX_TYPE;
  3818. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  3819. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  3820. &hw_cmd_args);
  3821. if (rc) {
  3822. CAM_ERR(CAM_ISP, "HW command failed");
  3823. goto free_hw;
  3824. }
  3825. if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_RDI) {
  3826. /*
  3827. * this context has rdi only resource assign rdi only
  3828. * state machine
  3829. */
  3830. CAM_DBG(CAM_ISP, "RDI only session Context");
  3831. ctx_isp->substate_machine_irq =
  3832. cam_isp_ctx_rdi_only_activated_state_machine_irq;
  3833. ctx_isp->substate_machine =
  3834. cam_isp_ctx_rdi_only_activated_state_machine;
  3835. ctx_isp->rdi_only_context = true;
  3836. } else if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_FS2) {
  3837. CAM_DBG(CAM_ISP, "FS2 Session has PIX, RD and RDI");
  3838. ctx_isp->substate_machine_irq =
  3839. cam_isp_ctx_fs2_state_machine_irq;
  3840. ctx_isp->substate_machine =
  3841. cam_isp_ctx_fs2_state_machine;
  3842. } else if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_OFFLINE) {
  3843. CAM_DBG(CAM_ISP, "offline Session has PIX and RD resources");
  3844. ctx_isp->substate_machine_irq =
  3845. cam_isp_ctx_offline_state_machine_irq;
  3846. } else {
  3847. CAM_DBG(CAM_ISP, "Session has PIX or PIX and RDI resources");
  3848. ctx_isp->substate_machine_irq =
  3849. cam_isp_ctx_activated_state_machine_irq;
  3850. ctx_isp->substate_machine =
  3851. cam_isp_ctx_activated_state_machine;
  3852. }
  3853. ctx_isp->hw_ctx = param.ctxt_to_hw_map;
  3854. ctx_isp->hw_acquired = true;
  3855. ctx_isp->split_acquire = false;
  3856. ctx->ctxt_to_hw_map = param.ctxt_to_hw_map;
  3857. atomic64_set(&ctx_isp->state_monitor_head, -1);
  3858. for (i = 0; i < CAM_ISP_CTX_EVENT_MAX; i++)
  3859. atomic64_set(&ctx_isp->event_record_head[i], -1);
  3860. kfree(isp_res);
  3861. isp_res = NULL;
  3862. get_dev_handle:
  3863. req_hdl_param.session_hdl = cmd->session_handle;
  3864. /* bridge is not ready for these flags. so false for now */
  3865. req_hdl_param.v4l2_sub_dev_flag = 0;
  3866. req_hdl_param.media_entity_flag = 0;
  3867. req_hdl_param.ops = ctx->crm_ctx_intf;
  3868. req_hdl_param.priv = ctx;
  3869. CAM_DBG(CAM_ISP, "get device handle form bridge");
  3870. ctx->dev_hdl = cam_create_device_hdl(&req_hdl_param);
  3871. if (ctx->dev_hdl <= 0) {
  3872. rc = -EFAULT;
  3873. CAM_ERR(CAM_ISP, "Can not create device handle");
  3874. goto free_hw;
  3875. }
  3876. cmd->dev_handle = ctx->dev_hdl;
  3877. /* store session information */
  3878. ctx->session_hdl = cmd->session_handle;
  3879. ctx->state = CAM_CTX_ACQUIRED;
  3880. trace_cam_context_state("ISP", ctx);
  3881. CAM_DBG(CAM_ISP,
  3882. "Acquire success on session_hdl 0x%x num_rsrces %d ctx %u",
  3883. cmd->session_handle, cmd->num_resources, ctx->ctx_id);
  3884. return rc;
  3885. free_hw:
  3886. release.ctxt_to_hw_map = ctx_isp->hw_ctx;
  3887. if (ctx_isp->hw_acquired)
  3888. ctx->hw_mgr_intf->hw_release(ctx->hw_mgr_intf->hw_mgr_priv,
  3889. &release);
  3890. ctx_isp->hw_ctx = NULL;
  3891. ctx_isp->hw_acquired = false;
  3892. free_res:
  3893. kfree(isp_res);
  3894. end:
  3895. return rc;
  3896. }
  3897. static int __cam_isp_ctx_acquire_hw_v1(struct cam_context *ctx,
  3898. void *args)
  3899. {
  3900. int rc = 0;
  3901. int i;
  3902. struct cam_acquire_hw_cmd_v1 *cmd =
  3903. (struct cam_acquire_hw_cmd_v1 *)args;
  3904. struct cam_hw_acquire_args param;
  3905. struct cam_hw_release_args release;
  3906. struct cam_isp_context *ctx_isp =
  3907. (struct cam_isp_context *) ctx->ctx_priv;
  3908. struct cam_hw_cmd_args hw_cmd_args;
  3909. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  3910. struct cam_isp_acquire_hw_info *acquire_hw_info = NULL;
  3911. if (!ctx->hw_mgr_intf) {
  3912. CAM_ERR(CAM_ISP, "HW interface is not ready");
  3913. rc = -EFAULT;
  3914. goto end;
  3915. }
  3916. CAM_DBG(CAM_ISP,
  3917. "session_hdl 0x%x, hdl type %d, res %lld",
  3918. cmd->session_handle, cmd->handle_type, cmd->resource_hdl);
  3919. /* for now we only support user pointer */
  3920. if (cmd->handle_type != 1) {
  3921. CAM_ERR(CAM_ISP, "Only user pointer is supported");
  3922. rc = -EINVAL;
  3923. goto end;
  3924. }
  3925. if (cmd->data_size < sizeof(*acquire_hw_info)) {
  3926. CAM_ERR(CAM_ISP, "data_size is not a valid value");
  3927. goto end;
  3928. }
  3929. acquire_hw_info = kzalloc(cmd->data_size, GFP_KERNEL);
  3930. if (!acquire_hw_info) {
  3931. rc = -ENOMEM;
  3932. goto end;
  3933. }
  3934. CAM_DBG(CAM_ISP, "start copy resources from user");
  3935. if (copy_from_user(acquire_hw_info, (void __user *)cmd->resource_hdl,
  3936. cmd->data_size)) {
  3937. rc = -EFAULT;
  3938. goto free_res;
  3939. }
  3940. memset(&param, 0, sizeof(param));
  3941. param.context_data = ctx;
  3942. param.event_cb = ctx->irq_cb_intf;
  3943. param.num_acq = CAM_API_COMPAT_CONSTANT;
  3944. param.acquire_info_size = cmd->data_size;
  3945. param.acquire_info = (uint64_t) acquire_hw_info;
  3946. /* call HW manager to reserve the resource */
  3947. rc = ctx->hw_mgr_intf->hw_acquire(ctx->hw_mgr_intf->hw_mgr_priv,
  3948. &param);
  3949. if (rc != 0) {
  3950. CAM_ERR(CAM_ISP, "Acquire device failed");
  3951. goto free_res;
  3952. }
  3953. /* Query the context has rdi only resource */
  3954. hw_cmd_args.ctxt_to_hw_map = param.ctxt_to_hw_map;
  3955. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  3956. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_CMD_CTX_TYPE;
  3957. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  3958. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  3959. &hw_cmd_args);
  3960. if (rc) {
  3961. CAM_ERR(CAM_ISP, "HW command failed");
  3962. goto free_hw;
  3963. }
  3964. if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_RDI) {
  3965. /*
  3966. * this context has rdi only resource assign rdi only
  3967. * state machine
  3968. */
  3969. CAM_DBG(CAM_ISP, "RDI only session Context");
  3970. ctx_isp->substate_machine_irq =
  3971. cam_isp_ctx_rdi_only_activated_state_machine_irq;
  3972. ctx_isp->substate_machine =
  3973. cam_isp_ctx_rdi_only_activated_state_machine;
  3974. ctx_isp->rdi_only_context = true;
  3975. } else if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_FS2) {
  3976. CAM_DBG(CAM_ISP, "FS2 Session has PIX, RD and RDI");
  3977. ctx_isp->substate_machine_irq =
  3978. cam_isp_ctx_fs2_state_machine_irq;
  3979. ctx_isp->substate_machine =
  3980. cam_isp_ctx_fs2_state_machine;
  3981. } else if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_OFFLINE) {
  3982. CAM_DBG(CAM_ISP, "Offline session has PIX and RD resources");
  3983. ctx_isp->substate_machine_irq =
  3984. cam_isp_ctx_offline_state_machine_irq;
  3985. ctx_isp->substate_machine = NULL;
  3986. } else {
  3987. CAM_DBG(CAM_ISP, "Session has PIX or PIX and RDI resources");
  3988. ctx_isp->substate_machine_irq =
  3989. cam_isp_ctx_activated_state_machine_irq;
  3990. ctx_isp->substate_machine =
  3991. cam_isp_ctx_activated_state_machine;
  3992. }
  3993. ctx_isp->hw_ctx = param.ctxt_to_hw_map;
  3994. ctx_isp->hw_acquired = true;
  3995. ctx->ctxt_to_hw_map = param.ctxt_to_hw_map;
  3996. atomic64_set(&ctx_isp->state_monitor_head, -1);
  3997. for (i = 0; i < CAM_ISP_CTX_EVENT_MAX; i++)
  3998. atomic64_set(&ctx_isp->event_record_head[i], -1);
  3999. trace_cam_context_state("ISP", ctx);
  4000. CAM_DBG(CAM_ISP,
  4001. "Acquire success on session_hdl 0x%xs ctx_type %d ctx_id %u",
  4002. ctx->session_hdl, isp_hw_cmd_args.u.ctx_type, ctx->ctx_id);
  4003. kfree(acquire_hw_info);
  4004. return rc;
  4005. free_hw:
  4006. release.ctxt_to_hw_map = ctx_isp->hw_ctx;
  4007. ctx->hw_mgr_intf->hw_release(ctx->hw_mgr_intf->hw_mgr_priv, &release);
  4008. ctx_isp->hw_ctx = NULL;
  4009. ctx_isp->hw_acquired = false;
  4010. free_res:
  4011. kfree(acquire_hw_info);
  4012. end:
  4013. return rc;
  4014. }
  4015. static void cam_req_mgr_process_workq_offline_ife_worker(struct work_struct *w)
  4016. {
  4017. cam_req_mgr_process_workq(w);
  4018. }
  4019. static int __cam_isp_ctx_acquire_hw_v2(struct cam_context *ctx,
  4020. void *args)
  4021. {
  4022. int rc = 0, i, j;
  4023. struct cam_acquire_hw_cmd_v2 *cmd =
  4024. (struct cam_acquire_hw_cmd_v2 *)args;
  4025. struct cam_hw_acquire_args param;
  4026. struct cam_hw_release_args release;
  4027. struct cam_isp_context *ctx_isp =
  4028. (struct cam_isp_context *) ctx->ctx_priv;
  4029. struct cam_hw_cmd_args hw_cmd_args;
  4030. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  4031. struct cam_isp_acquire_hw_info *acquire_hw_info = NULL;
  4032. if (!ctx->hw_mgr_intf) {
  4033. CAM_ERR(CAM_ISP, "HW interface is not ready");
  4034. rc = -EFAULT;
  4035. goto end;
  4036. }
  4037. CAM_DBG(CAM_ISP,
  4038. "session_hdl 0x%x, hdl type %d, res %lld",
  4039. cmd->session_handle, cmd->handle_type, cmd->resource_hdl);
  4040. /* for now we only support user pointer */
  4041. if (cmd->handle_type != 1) {
  4042. CAM_ERR(CAM_ISP, "Only user pointer is supported");
  4043. rc = -EINVAL;
  4044. goto end;
  4045. }
  4046. if (cmd->data_size < sizeof(*acquire_hw_info)) {
  4047. CAM_ERR(CAM_ISP, "data_size is not a valid value");
  4048. goto end;
  4049. }
  4050. acquire_hw_info = kzalloc(cmd->data_size, GFP_KERNEL);
  4051. if (!acquire_hw_info) {
  4052. rc = -ENOMEM;
  4053. goto end;
  4054. }
  4055. CAM_DBG(CAM_ISP, "start copy resources from user");
  4056. if (copy_from_user(acquire_hw_info, (void __user *)cmd->resource_hdl,
  4057. cmd->data_size)) {
  4058. rc = -EFAULT;
  4059. goto free_res;
  4060. }
  4061. memset(&param, 0, sizeof(param));
  4062. param.context_data = ctx;
  4063. param.event_cb = ctx->irq_cb_intf;
  4064. param.num_acq = CAM_API_COMPAT_CONSTANT;
  4065. param.acquire_info_size = cmd->data_size;
  4066. param.acquire_info = (uint64_t) acquire_hw_info;
  4067. /* call HW manager to reserve the resource */
  4068. rc = ctx->hw_mgr_intf->hw_acquire(ctx->hw_mgr_intf->hw_mgr_priv,
  4069. &param);
  4070. if (rc != 0) {
  4071. CAM_ERR(CAM_ISP, "Acquire device failed");
  4072. goto free_res;
  4073. }
  4074. /* Set custom flag if applicable
  4075. * custom hw is supported only on v2
  4076. */
  4077. ctx_isp->custom_enabled = param.custom_enabled;
  4078. ctx_isp->use_frame_header_ts = param.use_frame_header_ts;
  4079. ctx_isp->support_consumed_addr =
  4080. param.support_consumed_addr;
  4081. /* Query the context has rdi only resource */
  4082. hw_cmd_args.ctxt_to_hw_map = param.ctxt_to_hw_map;
  4083. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  4084. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_CMD_CTX_TYPE;
  4085. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  4086. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  4087. &hw_cmd_args);
  4088. if (rc) {
  4089. CAM_ERR(CAM_ISP, "HW command failed");
  4090. goto free_hw;
  4091. }
  4092. if (param.valid_acquired_hw) {
  4093. for (i = 0; i < CAM_MAX_ACQ_RES; i++)
  4094. cmd->hw_info.acquired_hw_id[i] =
  4095. param.acquired_hw_id[i];
  4096. for (i = 0; i < CAM_MAX_ACQ_RES; i++)
  4097. for (j = 0; j < CAM_MAX_HW_SPLIT; j++)
  4098. cmd->hw_info.acquired_hw_path[i][j] =
  4099. param.acquired_hw_path[i][j];
  4100. }
  4101. cmd->hw_info.valid_acquired_hw =
  4102. param.valid_acquired_hw;
  4103. cmd->hw_info.valid_acquired_hw = param.valid_acquired_hw;
  4104. if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_RDI) {
  4105. /*
  4106. * this context has rdi only resource assign rdi only
  4107. * state machine
  4108. */
  4109. CAM_DBG(CAM_ISP, "RDI only session Context");
  4110. ctx_isp->substate_machine_irq =
  4111. cam_isp_ctx_rdi_only_activated_state_machine_irq;
  4112. ctx_isp->substate_machine =
  4113. cam_isp_ctx_rdi_only_activated_state_machine;
  4114. ctx_isp->rdi_only_context = true;
  4115. } else if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_FS2) {
  4116. CAM_DBG(CAM_ISP, "FS2 Session has PIX, RD and RDI");
  4117. ctx_isp->substate_machine_irq =
  4118. cam_isp_ctx_fs2_state_machine_irq;
  4119. ctx_isp->substate_machine =
  4120. cam_isp_ctx_fs2_state_machine;
  4121. } else if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_OFFLINE) {
  4122. CAM_DBG(CAM_ISP, "Offline Session has PIX and RD resources");
  4123. ctx_isp->substate_machine_irq =
  4124. cam_isp_ctx_offline_state_machine_irq;
  4125. ctx_isp->substate_machine = NULL;
  4126. ctx_isp->offline_context = true;
  4127. rc = cam_req_mgr_workq_create("offline_ife", 20,
  4128. &ctx_isp->workq, CRM_WORKQ_USAGE_IRQ, 0,
  4129. cam_req_mgr_process_workq_offline_ife_worker);
  4130. if (rc)
  4131. CAM_ERR(CAM_ISP,
  4132. "Failed to create workq for offline IFE rc:%d",
  4133. rc);
  4134. } else {
  4135. CAM_DBG(CAM_ISP, "Session has PIX or PIX and RDI resources");
  4136. ctx_isp->substate_machine_irq =
  4137. cam_isp_ctx_activated_state_machine_irq;
  4138. ctx_isp->substate_machine =
  4139. cam_isp_ctx_activated_state_machine;
  4140. }
  4141. ctx_isp->hw_ctx = param.ctxt_to_hw_map;
  4142. ctx_isp->hw_acquired = true;
  4143. ctx->ctxt_to_hw_map = param.ctxt_to_hw_map;
  4144. trace_cam_context_state("ISP", ctx);
  4145. CAM_DBG(CAM_ISP,
  4146. "Acquire success on session_hdl 0x%xs ctx_type %d ctx_id %u",
  4147. ctx->session_hdl, isp_hw_cmd_args.u.ctx_type, ctx->ctx_id);
  4148. kfree(acquire_hw_info);
  4149. return rc;
  4150. free_hw:
  4151. release.ctxt_to_hw_map = ctx_isp->hw_ctx;
  4152. ctx->hw_mgr_intf->hw_release(ctx->hw_mgr_intf->hw_mgr_priv, &release);
  4153. ctx_isp->hw_ctx = NULL;
  4154. ctx_isp->hw_acquired = false;
  4155. free_res:
  4156. kfree(acquire_hw_info);
  4157. end:
  4158. return rc;
  4159. }
  4160. static int __cam_isp_ctx_acquire_hw_in_acquired(struct cam_context *ctx,
  4161. void *args)
  4162. {
  4163. int rc = -EINVAL;
  4164. uint32_t api_version;
  4165. if (!ctx || !args) {
  4166. CAM_ERR(CAM_ISP, "Invalid input pointer");
  4167. return rc;
  4168. }
  4169. api_version = *((uint32_t *)args);
  4170. if (api_version == 1)
  4171. rc = __cam_isp_ctx_acquire_hw_v1(ctx, args);
  4172. else if (api_version == 2)
  4173. rc = __cam_isp_ctx_acquire_hw_v2(ctx, args);
  4174. else
  4175. CAM_ERR(CAM_ISP, "Unsupported api version %d", api_version);
  4176. return rc;
  4177. }
  4178. static int __cam_isp_ctx_config_dev_in_acquired(struct cam_context *ctx,
  4179. struct cam_config_dev_cmd *cmd)
  4180. {
  4181. int rc = 0;
  4182. struct cam_isp_context *ctx_isp =
  4183. (struct cam_isp_context *) ctx->ctx_priv;
  4184. if (!ctx_isp->hw_acquired) {
  4185. CAM_ERR(CAM_ISP, "HW is not acquired, reject packet");
  4186. return -EINVAL;
  4187. }
  4188. rc = __cam_isp_ctx_config_dev_in_top_state(ctx, cmd);
  4189. if (!rc && ((ctx->link_hdl >= 0) || ctx_isp->offline_context)) {
  4190. ctx->state = CAM_CTX_READY;
  4191. trace_cam_context_state("ISP", ctx);
  4192. }
  4193. CAM_DBG(CAM_ISP, "next state %d", ctx->state);
  4194. return rc;
  4195. }
  4196. static int __cam_isp_ctx_config_dev_in_flushed(struct cam_context *ctx,
  4197. struct cam_config_dev_cmd *cmd)
  4198. {
  4199. int rc = 0;
  4200. struct cam_start_stop_dev_cmd start_cmd;
  4201. struct cam_hw_cmd_args hw_cmd_args;
  4202. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  4203. struct cam_isp_context *ctx_isp =
  4204. (struct cam_isp_context *) ctx->ctx_priv;
  4205. if (!ctx_isp->hw_acquired) {
  4206. CAM_ERR(CAM_ISP, "HW is not acquired, reject packet");
  4207. rc = -EINVAL;
  4208. goto end;
  4209. }
  4210. rc = __cam_isp_ctx_config_dev_in_top_state(ctx, cmd);
  4211. if (rc)
  4212. goto end;
  4213. if (!ctx_isp->init_received) {
  4214. CAM_WARN(CAM_ISP,
  4215. "Received update packet in flushed state, skip start");
  4216. goto end;
  4217. }
  4218. hw_cmd_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  4219. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  4220. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_CMD_RESUME_HW;
  4221. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  4222. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  4223. &hw_cmd_args);
  4224. if (rc) {
  4225. CAM_ERR(CAM_ISP, "Failed to resume HW rc: %d", rc);
  4226. goto end;
  4227. }
  4228. start_cmd.dev_handle = cmd->dev_handle;
  4229. start_cmd.session_handle = cmd->session_handle;
  4230. rc = __cam_isp_ctx_start_dev_in_ready(ctx, &start_cmd);
  4231. if (rc)
  4232. CAM_ERR(CAM_ISP,
  4233. "Failed to re-start HW after flush rc: %d", rc);
  4234. else
  4235. CAM_INFO(CAM_ISP,
  4236. "Received init after flush. Re-start HW complete in ctx:%d",
  4237. ctx->ctx_id);
  4238. end:
  4239. CAM_DBG(CAM_ISP, "next state %d sub_state:%d", ctx->state,
  4240. ctx_isp->substate_activated);
  4241. return rc;
  4242. }
  4243. static int __cam_isp_ctx_link_in_acquired(struct cam_context *ctx,
  4244. struct cam_req_mgr_core_dev_link_setup *link)
  4245. {
  4246. int rc = 0;
  4247. struct cam_isp_context *ctx_isp =
  4248. (struct cam_isp_context *) ctx->ctx_priv;
  4249. CAM_DBG(CAM_ISP, "Enter.........");
  4250. ctx->link_hdl = link->link_hdl;
  4251. ctx->ctx_crm_intf = link->crm_cb;
  4252. ctx_isp->subscribe_event =
  4253. CAM_TRIGGER_POINT_SOF | CAM_TRIGGER_POINT_EOF;
  4254. ctx_isp->trigger_id = link->trigger_id;
  4255. /* change state only if we had the init config */
  4256. if (ctx_isp->init_received) {
  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_unlink_in_acquired(struct cam_context *ctx,
  4264. struct cam_req_mgr_core_dev_link_setup *unlink)
  4265. {
  4266. int rc = 0;
  4267. struct cam_isp_context *ctx_isp =
  4268. (struct cam_isp_context *) ctx->ctx_priv;
  4269. ctx->link_hdl = -1;
  4270. ctx->ctx_crm_intf = NULL;
  4271. ctx_isp->trigger_id = -1;
  4272. return rc;
  4273. }
  4274. static int __cam_isp_ctx_get_dev_info_in_acquired(struct cam_context *ctx,
  4275. struct cam_req_mgr_device_info *dev_info)
  4276. {
  4277. int rc = 0;
  4278. dev_info->dev_hdl = ctx->dev_hdl;
  4279. strlcpy(dev_info->name, CAM_ISP_DEV_NAME, sizeof(dev_info->name));
  4280. dev_info->dev_id = CAM_REQ_MGR_DEVICE_IFE;
  4281. dev_info->p_delay = 1;
  4282. dev_info->trigger = CAM_TRIGGER_POINT_SOF;
  4283. dev_info->trigger_on = true;
  4284. return rc;
  4285. }
  4286. static int __cam_isp_ctx_start_dev_in_ready(struct cam_context *ctx,
  4287. struct cam_start_stop_dev_cmd *cmd)
  4288. {
  4289. int rc = 0;
  4290. int i;
  4291. struct cam_isp_start_args start_isp;
  4292. struct cam_ctx_request *req;
  4293. struct cam_isp_ctx_req *req_isp;
  4294. struct cam_isp_context *ctx_isp =
  4295. (struct cam_isp_context *) ctx->ctx_priv;
  4296. if (cmd->session_handle != ctx->session_hdl ||
  4297. cmd->dev_handle != ctx->dev_hdl) {
  4298. rc = -EPERM;
  4299. goto end;
  4300. }
  4301. if (list_empty(&ctx->pending_req_list)) {
  4302. /* should never happen */
  4303. CAM_ERR(CAM_ISP, "Start device with empty configuration");
  4304. rc = -EFAULT;
  4305. goto end;
  4306. } else {
  4307. req = list_first_entry(&ctx->pending_req_list,
  4308. struct cam_ctx_request, list);
  4309. }
  4310. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  4311. if (!ctx_isp->hw_ctx) {
  4312. CAM_ERR(CAM_ISP, "Wrong hw context pointer.");
  4313. rc = -EFAULT;
  4314. goto end;
  4315. }
  4316. start_isp.hw_config.ctxt_to_hw_map = ctx_isp->hw_ctx;
  4317. start_isp.hw_config.request_id = req->request_id;
  4318. start_isp.hw_config.hw_update_entries = req_isp->cfg;
  4319. start_isp.hw_config.num_hw_update_entries = req_isp->num_cfg;
  4320. start_isp.hw_config.priv = &req_isp->hw_update_data;
  4321. start_isp.hw_config.init_packet = 1;
  4322. start_isp.hw_config.reapply = 0;
  4323. ctx_isp->last_applied_req_id = req->request_id;
  4324. if (ctx->state == CAM_CTX_FLUSHED)
  4325. start_isp.start_only = true;
  4326. else
  4327. start_isp.start_only = false;
  4328. atomic_set(&ctx_isp->process_bubble, 0);
  4329. atomic_set(&ctx_isp->rxd_epoch, 0);
  4330. ctx_isp->frame_id = 0;
  4331. ctx_isp->active_req_cnt = 0;
  4332. ctx_isp->reported_req_id = 0;
  4333. ctx_isp->substate_activated = ctx_isp->rdi_only_context ?
  4334. CAM_ISP_CTX_ACTIVATED_APPLIED :
  4335. (req_isp->num_fence_map_out) ? CAM_ISP_CTX_ACTIVATED_EPOCH :
  4336. CAM_ISP_CTX_ACTIVATED_SOF;
  4337. atomic64_set(&ctx_isp->state_monitor_head, -1);
  4338. for (i = 0; i < CAM_ISP_CTX_EVENT_MAX; i++)
  4339. atomic64_set(&ctx_isp->event_record_head[i], -1);
  4340. /*
  4341. * In case of CSID TPG we might receive SOF and RUP IRQs
  4342. * before hw_mgr_intf->hw_start has returned. So move
  4343. * req out of pending list before hw_start and add it
  4344. * back to pending list if hw_start fails.
  4345. */
  4346. list_del_init(&req->list);
  4347. if (ctx_isp->offline_context && !req_isp->num_fence_map_out) {
  4348. list_add_tail(&req->list, &ctx->free_req_list);
  4349. atomic_set(&ctx_isp->rxd_epoch, 1);
  4350. CAM_DBG(CAM_REQ,
  4351. "Move pending req: %lld to free list(cnt: %d) offline ctx %u",
  4352. req->request_id, ctx_isp->active_req_cnt, ctx->ctx_id);
  4353. } else if (ctx_isp->rdi_only_context || !req_isp->num_fence_map_out) {
  4354. list_add_tail(&req->list, &ctx->wait_req_list);
  4355. CAM_DBG(CAM_REQ,
  4356. "Move pending req: %lld to wait list(cnt: %d) ctx %u",
  4357. req->request_id, ctx_isp->active_req_cnt, ctx->ctx_id);
  4358. } else {
  4359. list_add_tail(&req->list, &ctx->active_req_list);
  4360. ctx_isp->active_req_cnt++;
  4361. CAM_DBG(CAM_REQ,
  4362. "Move pending req: %lld to active list(cnt: %d) ctx %u offline %d",
  4363. req->request_id, ctx_isp->active_req_cnt, ctx->ctx_id,
  4364. ctx_isp->offline_context);
  4365. }
  4366. /*
  4367. * Only place to change state before calling the hw due to
  4368. * hardware tasklet has higher priority that can cause the
  4369. * irq handling comes early
  4370. */
  4371. ctx->state = CAM_CTX_ACTIVATED;
  4372. trace_cam_context_state("ISP", ctx);
  4373. rc = ctx->hw_mgr_intf->hw_start(ctx->hw_mgr_intf->hw_mgr_priv,
  4374. &start_isp);
  4375. if (rc) {
  4376. /* HW failure. user need to clean up the resource */
  4377. CAM_ERR(CAM_ISP, "Start HW failed");
  4378. ctx->state = CAM_CTX_READY;
  4379. trace_cam_context_state("ISP", ctx);
  4380. if (rc == -ETIMEDOUT)
  4381. rc = cam_isp_ctx_dump_req(req_isp, 0, 0, NULL, false);
  4382. list_del_init(&req->list);
  4383. list_add(&req->list, &ctx->pending_req_list);
  4384. goto end;
  4385. }
  4386. CAM_DBG(CAM_ISP, "start device success ctx %u", ctx->ctx_id);
  4387. end:
  4388. return rc;
  4389. }
  4390. static int __cam_isp_ctx_unlink_in_ready(struct cam_context *ctx,
  4391. struct cam_req_mgr_core_dev_link_setup *unlink)
  4392. {
  4393. int rc = 0;
  4394. ctx->link_hdl = -1;
  4395. ctx->ctx_crm_intf = NULL;
  4396. ctx->state = CAM_CTX_ACQUIRED;
  4397. trace_cam_context_state("ISP", ctx);
  4398. return rc;
  4399. }
  4400. static int __cam_isp_ctx_stop_dev_in_activated_unlock(
  4401. struct cam_context *ctx, struct cam_start_stop_dev_cmd *stop_cmd)
  4402. {
  4403. int rc = 0;
  4404. uint32_t i;
  4405. struct cam_hw_stop_args stop;
  4406. struct cam_ctx_request *req;
  4407. struct cam_isp_ctx_req *req_isp;
  4408. struct cam_isp_context *ctx_isp =
  4409. (struct cam_isp_context *) ctx->ctx_priv;
  4410. struct cam_isp_stop_args stop_isp;
  4411. /* stop hw first */
  4412. if (ctx_isp->hw_ctx) {
  4413. stop.ctxt_to_hw_map = ctx_isp->hw_ctx;
  4414. if (stop_cmd)
  4415. stop_isp.hw_stop_cmd =
  4416. CAM_ISP_HW_STOP_AT_FRAME_BOUNDARY;
  4417. else
  4418. stop_isp.hw_stop_cmd = CAM_ISP_HW_STOP_IMMEDIATELY;
  4419. stop_isp.stop_only = false;
  4420. stop.args = (void *) &stop_isp;
  4421. ctx->hw_mgr_intf->hw_stop(ctx->hw_mgr_intf->hw_mgr_priv,
  4422. &stop);
  4423. }
  4424. /* Mask off all the incoming hardware events */
  4425. spin_lock_bh(&ctx->lock);
  4426. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_HALT;
  4427. spin_unlock_bh(&ctx->lock);
  4428. CAM_DBG(CAM_ISP, "next Substate[%s]",
  4429. __cam_isp_ctx_substate_val_to_type(
  4430. ctx_isp->substate_activated));
  4431. if (ctx->ctx_crm_intf &&
  4432. ctx->ctx_crm_intf->notify_stop) {
  4433. struct cam_req_mgr_notify_stop notify;
  4434. notify.link_hdl = ctx->link_hdl;
  4435. CAM_DBG(CAM_ISP,
  4436. "Notify CRM about device stop ctx %u link 0x%x",
  4437. ctx->ctx_id, ctx->link_hdl);
  4438. ctx->ctx_crm_intf->notify_stop(&notify);
  4439. } else
  4440. CAM_ERR(CAM_ISP, "cb not present");
  4441. while (!list_empty(&ctx->pending_req_list)) {
  4442. req = list_first_entry(&ctx->pending_req_list,
  4443. struct cam_ctx_request, list);
  4444. list_del_init(&req->list);
  4445. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  4446. CAM_DBG(CAM_ISP, "signal fence in pending list. fence num %d",
  4447. req_isp->num_fence_map_out);
  4448. for (i = 0; i < req_isp->num_fence_map_out; i++)
  4449. if (req_isp->fence_map_out[i].sync_id != -1) {
  4450. cam_sync_signal(
  4451. req_isp->fence_map_out[i].sync_id,
  4452. CAM_SYNC_STATE_SIGNALED_CANCEL,
  4453. CAM_SYNC_ISP_EVENT_HW_STOP);
  4454. }
  4455. list_add_tail(&req->list, &ctx->free_req_list);
  4456. }
  4457. while (!list_empty(&ctx->wait_req_list)) {
  4458. req = list_first_entry(&ctx->wait_req_list,
  4459. struct cam_ctx_request, list);
  4460. list_del_init(&req->list);
  4461. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  4462. CAM_DBG(CAM_ISP, "signal fence in wait list. fence num %d",
  4463. req_isp->num_fence_map_out);
  4464. for (i = 0; i < req_isp->num_fence_map_out; i++)
  4465. if (req_isp->fence_map_out[i].sync_id != -1) {
  4466. cam_sync_signal(
  4467. req_isp->fence_map_out[i].sync_id,
  4468. CAM_SYNC_STATE_SIGNALED_CANCEL,
  4469. CAM_SYNC_ISP_EVENT_HW_STOP);
  4470. }
  4471. list_add_tail(&req->list, &ctx->free_req_list);
  4472. }
  4473. while (!list_empty(&ctx->active_req_list)) {
  4474. req = list_first_entry(&ctx->active_req_list,
  4475. struct cam_ctx_request, list);
  4476. list_del_init(&req->list);
  4477. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  4478. CAM_DBG(CAM_ISP, "signal fence in active list. fence num %d",
  4479. req_isp->num_fence_map_out);
  4480. for (i = 0; i < req_isp->num_fence_map_out; i++)
  4481. if (req_isp->fence_map_out[i].sync_id != -1) {
  4482. cam_sync_signal(
  4483. req_isp->fence_map_out[i].sync_id,
  4484. CAM_SYNC_STATE_SIGNALED_CANCEL,
  4485. CAM_SYNC_ISP_EVENT_HW_STOP);
  4486. }
  4487. list_add_tail(&req->list, &ctx->free_req_list);
  4488. }
  4489. ctx_isp->frame_id = 0;
  4490. ctx_isp->active_req_cnt = 0;
  4491. ctx_isp->reported_req_id = 0;
  4492. ctx_isp->last_applied_req_id = 0;
  4493. ctx_isp->req_info.last_bufdone_req_id = 0;
  4494. atomic_set(&ctx_isp->process_bubble, 0);
  4495. atomic_set(&ctx_isp->rxd_epoch, 0);
  4496. atomic64_set(&ctx_isp->state_monitor_head, -1);
  4497. for (i = 0; i < CAM_ISP_CTX_EVENT_MAX; i++)
  4498. atomic64_set(&ctx_isp->event_record_head[i], -1);
  4499. CAM_DBG(CAM_ISP, "Stop device success next state %d on ctx %u",
  4500. ctx->state, ctx->ctx_id);
  4501. if (!stop_cmd) {
  4502. rc = __cam_isp_ctx_unlink_in_ready(ctx, NULL);
  4503. if (rc)
  4504. CAM_ERR(CAM_ISP, "Unlink failed rc=%d", rc);
  4505. }
  4506. return rc;
  4507. }
  4508. static int __cam_isp_ctx_stop_dev_in_activated(struct cam_context *ctx,
  4509. struct cam_start_stop_dev_cmd *cmd)
  4510. {
  4511. int rc = 0;
  4512. struct cam_isp_context *ctx_isp =
  4513. (struct cam_isp_context *)ctx->ctx_priv;
  4514. __cam_isp_ctx_stop_dev_in_activated_unlock(ctx, cmd);
  4515. ctx_isp->init_received = false;
  4516. ctx->state = CAM_CTX_ACQUIRED;
  4517. trace_cam_context_state("ISP", ctx);
  4518. return rc;
  4519. }
  4520. static int __cam_isp_ctx_release_dev_in_activated(struct cam_context *ctx,
  4521. struct cam_release_dev_cmd *cmd)
  4522. {
  4523. int rc = 0;
  4524. rc = __cam_isp_ctx_stop_dev_in_activated_unlock(ctx, NULL);
  4525. if (rc)
  4526. CAM_ERR(CAM_ISP, "Stop device failed rc=%d", rc);
  4527. rc = __cam_isp_ctx_release_dev_in_top_state(ctx, cmd);
  4528. if (rc)
  4529. CAM_ERR(CAM_ISP, "Release device failed rc=%d", rc);
  4530. return rc;
  4531. }
  4532. static int __cam_isp_ctx_release_hw_in_activated(struct cam_context *ctx,
  4533. void *cmd)
  4534. {
  4535. int rc = 0;
  4536. rc = __cam_isp_ctx_stop_dev_in_activated_unlock(ctx, NULL);
  4537. if (rc)
  4538. CAM_ERR(CAM_ISP, "Stop device failed rc=%d", rc);
  4539. rc = __cam_isp_ctx_release_hw_in_top_state(ctx, cmd);
  4540. if (rc)
  4541. CAM_ERR(CAM_ISP, "Release hw failed rc=%d", rc);
  4542. return rc;
  4543. }
  4544. static int __cam_isp_ctx_link_pause(struct cam_context *ctx)
  4545. {
  4546. int rc = 0;
  4547. struct cam_hw_cmd_args hw_cmd_args;
  4548. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  4549. struct cam_isp_context *ctx_isp =
  4550. (struct cam_isp_context *) ctx->ctx_priv;
  4551. hw_cmd_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  4552. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  4553. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_CMD_PAUSE_HW;
  4554. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  4555. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  4556. &hw_cmd_args);
  4557. return rc;
  4558. }
  4559. static int __cam_isp_ctx_link_resume(struct cam_context *ctx)
  4560. {
  4561. int rc = 0;
  4562. struct cam_hw_cmd_args hw_cmd_args;
  4563. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  4564. struct cam_isp_context *ctx_isp =
  4565. (struct cam_isp_context *) ctx->ctx_priv;
  4566. hw_cmd_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  4567. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  4568. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_CMD_RESUME_HW;
  4569. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  4570. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  4571. &hw_cmd_args);
  4572. return rc;
  4573. }
  4574. static int __cam_isp_ctx_handle_sof_freeze_evt(
  4575. struct cam_context *ctx)
  4576. {
  4577. int rc = 0;
  4578. struct cam_hw_cmd_args hw_cmd_args;
  4579. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  4580. struct cam_isp_context *ctx_isp =
  4581. (struct cam_isp_context *) ctx->ctx_priv;
  4582. hw_cmd_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  4583. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  4584. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_CMD_SOF_DEBUG;
  4585. isp_hw_cmd_args.u.sof_irq_enable = 1;
  4586. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  4587. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  4588. &hw_cmd_args);
  4589. return rc;
  4590. }
  4591. static int __cam_isp_ctx_process_evt(struct cam_context *ctx,
  4592. struct cam_req_mgr_link_evt_data *link_evt_data)
  4593. {
  4594. int rc = 0;
  4595. switch (link_evt_data->evt_type) {
  4596. case CAM_REQ_MGR_LINK_EVT_ERR:
  4597. /* No need to handle this message now */
  4598. break;
  4599. case CAM_REQ_MGR_LINK_EVT_PAUSE:
  4600. __cam_isp_ctx_link_pause(ctx);
  4601. break;
  4602. case CAM_REQ_MGR_LINK_EVT_RESUME:
  4603. __cam_isp_ctx_link_resume(ctx);
  4604. break;
  4605. case CAM_REQ_MGR_LINK_EVT_SOF_FREEZE:
  4606. __cam_isp_ctx_handle_sof_freeze_evt(ctx);
  4607. break;
  4608. default:
  4609. CAM_WARN(CAM_ISP, "Unknown event from CRM");
  4610. break;
  4611. }
  4612. return rc;
  4613. }
  4614. static int __cam_isp_ctx_unlink_in_activated(struct cam_context *ctx,
  4615. struct cam_req_mgr_core_dev_link_setup *unlink)
  4616. {
  4617. int rc = 0;
  4618. CAM_WARN(CAM_ISP,
  4619. "Received unlink in activated state. It's unexpected");
  4620. rc = __cam_isp_ctx_stop_dev_in_activated_unlock(ctx, NULL);
  4621. if (rc)
  4622. CAM_WARN(CAM_ISP, "Stop device failed rc=%d", rc);
  4623. rc = __cam_isp_ctx_unlink_in_ready(ctx, unlink);
  4624. if (rc)
  4625. CAM_ERR(CAM_ISP, "Unlink failed rc=%d", rc);
  4626. return rc;
  4627. }
  4628. static int __cam_isp_ctx_apply_req(struct cam_context *ctx,
  4629. struct cam_req_mgr_apply_request *apply)
  4630. {
  4631. int rc = 0;
  4632. struct cam_ctx_ops *ctx_ops = NULL;
  4633. struct cam_isp_context *ctx_isp =
  4634. (struct cam_isp_context *) ctx->ctx_priv;
  4635. trace_cam_apply_req("ISP", apply->request_id);
  4636. CAM_DBG(CAM_ISP, "Enter: apply req in Substate[%s] request_id:%lld",
  4637. __cam_isp_ctx_substate_val_to_type(
  4638. ctx_isp->substate_activated), apply->request_id);
  4639. ctx_ops = &ctx_isp->substate_machine[ctx_isp->substate_activated];
  4640. if (ctx_ops->crm_ops.apply_req) {
  4641. rc = ctx_ops->crm_ops.apply_req(ctx, apply);
  4642. } else {
  4643. CAM_WARN_RATE_LIMIT(CAM_ISP,
  4644. "No handle function in activated Substate[%s]",
  4645. __cam_isp_ctx_substate_val_to_type(
  4646. ctx_isp->substate_activated));
  4647. rc = -EFAULT;
  4648. }
  4649. if (rc)
  4650. CAM_WARN_RATE_LIMIT(CAM_ISP,
  4651. "Apply failed in active Substate[%s] rc %d",
  4652. __cam_isp_ctx_substate_val_to_type(
  4653. ctx_isp->substate_activated), rc);
  4654. return rc;
  4655. }
  4656. static int __cam_isp_ctx_handle_irq_in_activated(void *context,
  4657. uint32_t evt_id, void *evt_data)
  4658. {
  4659. int rc = 0;
  4660. struct cam_isp_ctx_irq_ops *irq_ops = NULL;
  4661. struct cam_context *ctx = (struct cam_context *)context;
  4662. struct cam_isp_context *ctx_isp =
  4663. (struct cam_isp_context *)ctx->ctx_priv;
  4664. spin_lock(&ctx->lock);
  4665. trace_cam_isp_activated_irq(ctx, ctx_isp->substate_activated, evt_id,
  4666. __cam_isp_ctx_get_event_ts(evt_id, evt_data));
  4667. CAM_DBG(CAM_ISP, "Enter: State %d, Substate[%s], evt id %d",
  4668. ctx->state, __cam_isp_ctx_substate_val_to_type(
  4669. ctx_isp->substate_activated), evt_id);
  4670. irq_ops = &ctx_isp->substate_machine_irq[ctx_isp->substate_activated];
  4671. if (irq_ops->irq_ops[evt_id]) {
  4672. rc = irq_ops->irq_ops[evt_id](ctx_isp, evt_data);
  4673. } else {
  4674. CAM_DBG(CAM_ISP, "No handle function for Substate[%s]",
  4675. __cam_isp_ctx_substate_val_to_type(
  4676. ctx_isp->substate_activated));
  4677. if (isp_ctx_debug.enable_state_monitor_dump)
  4678. __cam_isp_ctx_dump_state_monitor_array(ctx_isp);
  4679. }
  4680. CAM_DBG(CAM_ISP, "Exit: State %d Substate[%s]",
  4681. ctx->state, __cam_isp_ctx_substate_val_to_type(
  4682. ctx_isp->substate_activated));
  4683. spin_unlock(&ctx->lock);
  4684. return rc;
  4685. }
  4686. /* top state machine */
  4687. static struct cam_ctx_ops
  4688. cam_isp_ctx_top_state_machine[CAM_CTX_STATE_MAX] = {
  4689. /* Uninit */
  4690. {
  4691. .ioctl_ops = {},
  4692. .crm_ops = {},
  4693. .irq_ops = NULL,
  4694. },
  4695. /* Available */
  4696. {
  4697. .ioctl_ops = {
  4698. .acquire_dev = __cam_isp_ctx_acquire_dev_in_available,
  4699. },
  4700. .crm_ops = {},
  4701. .irq_ops = NULL,
  4702. },
  4703. /* Acquired */
  4704. {
  4705. .ioctl_ops = {
  4706. .acquire_hw = __cam_isp_ctx_acquire_hw_in_acquired,
  4707. .release_dev = __cam_isp_ctx_release_dev_in_top_state,
  4708. .config_dev = __cam_isp_ctx_config_dev_in_acquired,
  4709. .release_hw = __cam_isp_ctx_release_hw_in_top_state,
  4710. },
  4711. .crm_ops = {
  4712. .link = __cam_isp_ctx_link_in_acquired,
  4713. .unlink = __cam_isp_ctx_unlink_in_acquired,
  4714. .get_dev_info = __cam_isp_ctx_get_dev_info_in_acquired,
  4715. .flush_req = __cam_isp_ctx_flush_req_in_top_state,
  4716. .dump_req = __cam_isp_ctx_dump_in_top_state,
  4717. },
  4718. .irq_ops = NULL,
  4719. .pagefault_ops = cam_isp_context_dump_requests,
  4720. .dumpinfo_ops = cam_isp_context_info_dump,
  4721. },
  4722. /* Ready */
  4723. {
  4724. .ioctl_ops = {
  4725. .start_dev = __cam_isp_ctx_start_dev_in_ready,
  4726. .release_dev = __cam_isp_ctx_release_dev_in_top_state,
  4727. .config_dev = __cam_isp_ctx_config_dev_in_top_state,
  4728. .release_hw = __cam_isp_ctx_release_hw_in_top_state,
  4729. },
  4730. .crm_ops = {
  4731. .unlink = __cam_isp_ctx_unlink_in_ready,
  4732. .flush_req = __cam_isp_ctx_flush_req_in_ready,
  4733. .dump_req = __cam_isp_ctx_dump_in_top_state,
  4734. },
  4735. .irq_ops = NULL,
  4736. .pagefault_ops = cam_isp_context_dump_requests,
  4737. .dumpinfo_ops = cam_isp_context_info_dump,
  4738. },
  4739. /* Flushed */
  4740. {
  4741. .ioctl_ops = {
  4742. .stop_dev = __cam_isp_ctx_stop_dev_in_activated,
  4743. .release_dev = __cam_isp_ctx_release_dev_in_activated,
  4744. .config_dev = __cam_isp_ctx_config_dev_in_flushed,
  4745. .release_hw = __cam_isp_ctx_release_hw_in_activated,
  4746. },
  4747. .crm_ops = {
  4748. .unlink = __cam_isp_ctx_unlink_in_ready,
  4749. .process_evt = __cam_isp_ctx_process_evt,
  4750. },
  4751. .irq_ops = NULL,
  4752. .pagefault_ops = cam_isp_context_dump_requests,
  4753. .dumpinfo_ops = cam_isp_context_info_dump,
  4754. },
  4755. /* Activated */
  4756. {
  4757. .ioctl_ops = {
  4758. .stop_dev = __cam_isp_ctx_stop_dev_in_activated,
  4759. .release_dev = __cam_isp_ctx_release_dev_in_activated,
  4760. .config_dev = __cam_isp_ctx_config_dev_in_top_state,
  4761. .release_hw = __cam_isp_ctx_release_hw_in_activated,
  4762. },
  4763. .crm_ops = {
  4764. .unlink = __cam_isp_ctx_unlink_in_activated,
  4765. .apply_req = __cam_isp_ctx_apply_req,
  4766. .flush_req = __cam_isp_ctx_flush_req_in_top_state,
  4767. .process_evt = __cam_isp_ctx_process_evt,
  4768. .dump_req = __cam_isp_ctx_dump_in_top_state,
  4769. },
  4770. .irq_ops = __cam_isp_ctx_handle_irq_in_activated,
  4771. .pagefault_ops = cam_isp_context_dump_requests,
  4772. .dumpinfo_ops = cam_isp_context_info_dump,
  4773. },
  4774. };
  4775. static int cam_isp_context_dump_requests(void *data,
  4776. struct cam_smmu_pf_info *pf_info)
  4777. {
  4778. struct cam_context *ctx = (struct cam_context *)data;
  4779. struct cam_ctx_request *req = NULL;
  4780. struct cam_ctx_request *req_temp = NULL;
  4781. struct cam_isp_ctx_req *req_isp = NULL;
  4782. struct cam_isp_prepare_hw_update_data *hw_update_data = NULL;
  4783. struct cam_hw_mgr_dump_pf_data *pf_dbg_entry = NULL;
  4784. struct cam_req_mgr_message req_msg;
  4785. struct cam_isp_context *ctx_isp;
  4786. uint32_t resource_type = 0;
  4787. bool mem_found = false, ctx_found = false, send_error = false;
  4788. int rc = 0;
  4789. struct cam_isp_context *isp_ctx =
  4790. (struct cam_isp_context *)ctx->ctx_priv;
  4791. if (!isp_ctx) {
  4792. CAM_ERR(CAM_ISP, "Invalid isp ctx");
  4793. return -EINVAL;
  4794. }
  4795. CAM_INFO(CAM_ISP, "iommu fault handler for isp ctx %d state %d",
  4796. ctx->ctx_id, ctx->state);
  4797. list_for_each_entry_safe(req, req_temp,
  4798. &ctx->active_req_list, list) {
  4799. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  4800. hw_update_data = &req_isp->hw_update_data;
  4801. pf_dbg_entry = &(req->pf_data);
  4802. CAM_INFO(CAM_ISP, "Active List: req_id : %lld ",
  4803. req->request_id);
  4804. rc = cam_context_dump_pf_info_to_hw(ctx, pf_dbg_entry->packet,
  4805. &mem_found, &ctx_found, &resource_type, pf_info);
  4806. if (rc)
  4807. CAM_ERR(CAM_ISP, "Failed to dump pf info");
  4808. if (ctx_found)
  4809. send_error = true;
  4810. }
  4811. CAM_INFO(CAM_ISP, "Iterating over wait_list of isp ctx %d state %d",
  4812. ctx->ctx_id, ctx->state);
  4813. list_for_each_entry_safe(req, req_temp,
  4814. &ctx->wait_req_list, list) {
  4815. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  4816. hw_update_data = &req_isp->hw_update_data;
  4817. pf_dbg_entry = &(req->pf_data);
  4818. CAM_INFO(CAM_ISP, "Wait List: req_id : %lld ", req->request_id);
  4819. rc = cam_context_dump_pf_info_to_hw(ctx, pf_dbg_entry->packet,
  4820. &mem_found, &ctx_found, &resource_type, pf_info);
  4821. if (rc)
  4822. CAM_ERR(CAM_ISP, "Failed to dump pf info");
  4823. if (ctx_found)
  4824. send_error = true;
  4825. }
  4826. /*
  4827. * In certain scenarios we observe both overflow and SMMU pagefault
  4828. * for a particular request. If overflow is handled before page fault
  4829. * we need to traverse through pending request list because if
  4830. * bubble recovery is enabled on any request we move that request
  4831. * and all the subsequent requests to the pending list while handling
  4832. * overflow error.
  4833. */
  4834. CAM_INFO(CAM_ISP,
  4835. "Iterating over pending req list of isp ctx %d state %d",
  4836. ctx->ctx_id, ctx->state);
  4837. list_for_each_entry_safe(req, req_temp,
  4838. &ctx->pending_req_list, list) {
  4839. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  4840. hw_update_data = &req_isp->hw_update_data;
  4841. pf_dbg_entry = &(req->pf_data);
  4842. CAM_INFO(CAM_ISP, "Pending List: req_id : %lld ",
  4843. req->request_id);
  4844. rc = cam_context_dump_pf_info_to_hw(ctx, pf_dbg_entry->packet,
  4845. &mem_found, &ctx_found, &resource_type, pf_info);
  4846. if (rc)
  4847. CAM_ERR(CAM_ISP, "Failed to dump pf info");
  4848. if (ctx_found)
  4849. send_error = true;
  4850. }
  4851. if (resource_type) {
  4852. ctx_isp = (struct cam_isp_context *) ctx->ctx_priv;
  4853. if (ctx_isp->isp_device_type == CAM_IFE_DEVICE_TYPE)
  4854. CAM_ERR(CAM_ISP,
  4855. "Page fault on resource id:%s (0x%x) ctx id:%d frame id:%d reported id:%lld applied id:%lld",
  4856. __cam_isp_resource_handle_id_to_type(
  4857. resource_type),
  4858. resource_type, ctx->ctx_id, ctx_isp->frame_id,
  4859. ctx_isp->reported_req_id,
  4860. ctx_isp->last_applied_req_id);
  4861. else
  4862. CAM_ERR(CAM_ISP,
  4863. "Page fault on resource id:%s (0x%x) ctx id:%d frame id:%d reported id:%lld applied id:%lld",
  4864. __cam_isp_tfe_resource_handle_id_to_type(
  4865. resource_type),
  4866. resource_type, ctx->ctx_id, ctx_isp->frame_id,
  4867. ctx_isp->reported_req_id,
  4868. ctx_isp->last_applied_req_id);
  4869. }
  4870. if (send_error) {
  4871. CAM_INFO(CAM_ISP,
  4872. "page fault notifying to umd ctx %u session_hdl:%d device_hdl:%d link_hdl:%d",
  4873. ctx->ctx_id, ctx->session_hdl,
  4874. ctx->dev_hdl, ctx->link_hdl);
  4875. req_msg.session_hdl = ctx->session_hdl;
  4876. req_msg.u.err_msg.device_hdl = ctx->dev_hdl;
  4877. req_msg.u.err_msg.error_type =
  4878. CAM_REQ_MGR_ERROR_TYPE_PAGE_FAULT;
  4879. req_msg.u.err_msg.link_hdl = ctx->link_hdl;
  4880. req_msg.u.err_msg.request_id = 0;
  4881. req_msg.u.err_msg.resource_size = 0x0;
  4882. if (cam_req_mgr_notify_message(&req_msg,
  4883. V4L_EVENT_CAM_REQ_MGR_ERROR,
  4884. V4L_EVENT_CAM_REQ_MGR_EVENT))
  4885. CAM_ERR(CAM_ISP,
  4886. "could not send page fault notification ctx %u session_hdl:%d device_hdl:%d link_hdl:%d",
  4887. ctx->ctx_id, ctx->session_hdl,
  4888. ctx->dev_hdl, ctx->link_hdl);
  4889. }
  4890. return rc;
  4891. }
  4892. static int cam_isp_context_debug_register(void)
  4893. {
  4894. int rc = 0;
  4895. struct dentry *dbgfileptr = NULL;
  4896. dbgfileptr = debugfs_create_dir("camera_isp_ctx", NULL);
  4897. if (!dbgfileptr) {
  4898. CAM_ERR(CAM_ICP,"DebugFS could not create directory!");
  4899. rc = -ENOENT;
  4900. goto end;
  4901. }
  4902. /* Store parent inode for cleanup in caller */
  4903. isp_ctx_debug.dentry = dbgfileptr;
  4904. dbgfileptr = debugfs_create_u32("enable_state_monitor_dump", 0644,
  4905. isp_ctx_debug.dentry, &isp_ctx_debug.enable_state_monitor_dump);
  4906. if (IS_ERR(dbgfileptr)) {
  4907. if (PTR_ERR(dbgfileptr) == -ENODEV)
  4908. CAM_WARN(CAM_ICP, "DebugFS not enabled in kernel!");
  4909. else
  4910. rc = PTR_ERR(dbgfileptr);
  4911. }
  4912. end:
  4913. return rc;
  4914. }
  4915. int cam_isp_context_init(struct cam_isp_context *ctx,
  4916. struct cam_context *ctx_base,
  4917. struct cam_req_mgr_kmd_ops *crm_node_intf,
  4918. struct cam_hw_mgr_intf *hw_intf,
  4919. uint32_t ctx_id,
  4920. uint32_t isp_device_type)
  4921. {
  4922. int rc = -1;
  4923. int i;
  4924. if (!ctx || !ctx_base) {
  4925. CAM_ERR(CAM_ISP, "Invalid Context");
  4926. goto err;
  4927. }
  4928. /* ISP context setup */
  4929. memset(ctx, 0, sizeof(*ctx));
  4930. ctx->base = ctx_base;
  4931. ctx->frame_id = 0;
  4932. ctx->custom_enabled = false;
  4933. ctx->use_frame_header_ts = false;
  4934. ctx->active_req_cnt = 0;
  4935. ctx->reported_req_id = 0;
  4936. ctx->req_info.last_bufdone_req_id = 0;
  4937. ctx->hw_ctx = NULL;
  4938. ctx->substate_activated = CAM_ISP_CTX_ACTIVATED_SOF;
  4939. ctx->substate_machine = cam_isp_ctx_activated_state_machine;
  4940. ctx->substate_machine_irq = cam_isp_ctx_activated_state_machine_irq;
  4941. ctx->init_timestamp = jiffies_to_msecs(jiffies);
  4942. ctx->isp_device_type = isp_device_type;
  4943. for (i = 0; i < CAM_CTX_REQ_MAX; i++) {
  4944. ctx->req_base[i].req_priv = &ctx->req_isp[i];
  4945. ctx->req_isp[i].base = &ctx->req_base[i];
  4946. }
  4947. /* camera context setup */
  4948. rc = cam_context_init(ctx_base, isp_dev_name, CAM_ISP, ctx_id,
  4949. crm_node_intf, hw_intf, ctx->req_base, CAM_CTX_REQ_MAX);
  4950. if (rc) {
  4951. CAM_ERR(CAM_ISP, "Camera Context Base init failed");
  4952. goto err;
  4953. }
  4954. /* link camera context with isp context */
  4955. ctx_base->state_machine = cam_isp_ctx_top_state_machine;
  4956. ctx_base->ctx_priv = ctx;
  4957. /* initializing current state for error logging */
  4958. for (i = 0; i < CAM_ISP_CTX_STATE_MONITOR_MAX_ENTRIES; i++) {
  4959. ctx->cam_isp_ctx_state_monitor[i].curr_state =
  4960. CAM_ISP_CTX_ACTIVATED_MAX;
  4961. }
  4962. atomic64_set(&ctx->state_monitor_head, -1);
  4963. for (i = 0; i < CAM_ISP_CTX_EVENT_MAX; i++)
  4964. atomic64_set(&ctx->event_record_head[i], -1);
  4965. if (!isp_ctx_debug.dentry)
  4966. cam_isp_context_debug_register();
  4967. err:
  4968. return rc;
  4969. }
  4970. int cam_isp_context_deinit(struct cam_isp_context *ctx)
  4971. {
  4972. if (ctx->base)
  4973. cam_context_deinit(ctx->base);
  4974. if (ctx->substate_activated != CAM_ISP_CTX_ACTIVATED_SOF)
  4975. CAM_ERR(CAM_ISP, "ISP context Substate[%s] is invalid",
  4976. __cam_isp_ctx_substate_val_to_type(
  4977. ctx->substate_activated));
  4978. debugfs_remove_recursive(isp_ctx_debug.dentry);
  4979. isp_ctx_debug.dentry = NULL;
  4980. memset(ctx, 0, sizeof(*ctx));
  4981. return 0;
  4982. }