cam_isp_context.c 148 KB

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