cam_isp_context.c 149 KB

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