cam_isp_context.c 150 KB

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