cam_isp_context.c 112 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2017-2019, 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_STATE_MONITOR_HEAD(head) \
  23. (atomic64_add_return(1, head) % \
  24. CAM_ISP_CTX_STATE_MONITOR_MAX_ENTRIES)
  25. static int cam_isp_context_dump_active_request(void *data, unsigned long iova,
  26. uint32_t buf_info);
  27. static void __cam_isp_ctx_update_state_monitor_array(
  28. struct cam_isp_context *ctx_isp,
  29. enum cam_isp_state_change_trigger trigger_type,
  30. uint64_t req_id)
  31. {
  32. int iterator = INC_STATE_MONITOR_HEAD(&ctx_isp->state_monitor_head);
  33. ctx_isp->cam_isp_ctx_state_monitor[iterator].curr_state =
  34. ctx_isp->substate_activated;
  35. ctx_isp->cam_isp_ctx_state_monitor[iterator].frame_id =
  36. ctx_isp->frame_id;
  37. ctx_isp->cam_isp_ctx_state_monitor[iterator].trigger =
  38. trigger_type;
  39. ctx_isp->cam_isp_ctx_state_monitor[iterator].req_id =
  40. req_id;
  41. ctx_isp->cam_isp_ctx_state_monitor[iterator].evt_time_stamp =
  42. jiffies_to_msecs(jiffies) - ctx_isp->init_timestamp;
  43. }
  44. static const char *__cam_isp_ctx_substate_val_to_type(
  45. uint32_t type)
  46. {
  47. switch (type) {
  48. case CAM_ISP_CTX_ACTIVATED_SOF:
  49. return "SOF";
  50. case CAM_ISP_CTX_ACTIVATED_APPLIED:
  51. return "APPLIED";
  52. case CAM_ISP_CTX_ACTIVATED_EPOCH:
  53. return "EPOCH";
  54. case CAM_ISP_CTX_ACTIVATED_BUBBLE:
  55. return "BUBBLE";
  56. case CAM_ISP_CTX_ACTIVATED_BUBBLE_APPLIED:
  57. return "BUBBLE_APPLIED";
  58. case CAM_ISP_CTX_ACTIVATED_HW_ERROR:
  59. return "HW_ERROR";
  60. case CAM_ISP_CTX_ACTIVATED_HALT:
  61. return "HALT";
  62. default:
  63. return "CAM_ISP_CTX_INVALID_STATE";
  64. }
  65. }
  66. static const char *__cam_isp_hw_evt_val_to_type(
  67. uint32_t evt_id)
  68. {
  69. switch (evt_id) {
  70. case CAM_ISP_STATE_CHANGE_TRIGGER_ERROR:
  71. return "ERROR";
  72. case CAM_ISP_STATE_CHANGE_TRIGGER_APPLIED:
  73. return "APPLIED";
  74. case CAM_ISP_STATE_CHANGE_TRIGGER_SOF:
  75. return "SOF";
  76. case CAM_ISP_STATE_CHANGE_TRIGGER_REG_UPDATE:
  77. return "REG_UPDATE";
  78. case CAM_ISP_STATE_CHANGE_TRIGGER_EPOCH:
  79. return "EPOCH";
  80. case CAM_ISP_STATE_CHANGE_TRIGGER_EOF:
  81. return "EOF";
  82. case CAM_ISP_STATE_CHANGE_TRIGGER_DONE:
  83. return "DONE";
  84. case CAM_ISP_STATE_CHANGE_TRIGGER_FLUSH:
  85. return "FLUSH";
  86. default:
  87. return "CAM_ISP_EVENT_INVALID";
  88. }
  89. }
  90. static void __cam_isp_ctx_dump_state_monitor_array(
  91. struct cam_isp_context *ctx_isp)
  92. {
  93. int i = 0;
  94. int64_t state_head = 0;
  95. uint32_t index, num_entries, oldest_entry;
  96. state_head = atomic64_read(&ctx_isp->state_monitor_head);
  97. if (state_head == -1) {
  98. return;
  99. } else if (state_head < CAM_ISP_CTX_STATE_MONITOR_MAX_ENTRIES) {
  100. num_entries = state_head;
  101. oldest_entry = 0;
  102. } else {
  103. num_entries = CAM_ISP_CTX_STATE_MONITOR_MAX_ENTRIES;
  104. oldest_entry = (state_head + 1) %
  105. CAM_ISP_CTX_STATE_MONITOR_MAX_ENTRIES;
  106. }
  107. CAM_ERR(CAM_ISP,
  108. "Dumping state information for preceding requests");
  109. index = oldest_entry;
  110. for (i = 0; i < num_entries; i++) {
  111. CAM_ERR(CAM_ISP,
  112. "Index[%d] time[%d] : State[%s] Frame[%lld] ReqId[%llu] evt_type[%s]",
  113. index,
  114. ctx_isp->cam_isp_ctx_state_monitor[index].evt_time_stamp,
  115. __cam_isp_ctx_substate_val_to_type(
  116. ctx_isp->cam_isp_ctx_state_monitor[index].curr_state),
  117. ctx_isp->cam_isp_ctx_state_monitor[index].frame_id,
  118. ctx_isp->cam_isp_ctx_state_monitor[index].req_id,
  119. __cam_isp_hw_evt_val_to_type(
  120. ctx_isp->cam_isp_ctx_state_monitor[index].trigger));
  121. index = (index + 1) % CAM_ISP_CTX_STATE_MONITOR_MAX_ENTRIES;
  122. }
  123. }
  124. static int cam_isp_context_info_dump(void *context,
  125. enum cam_context_dump_id id)
  126. {
  127. struct cam_context *ctx = (struct cam_context *)context;
  128. switch (id) {
  129. case CAM_CTX_DUMP_ACQ_INFO: {
  130. cam_context_dump_hw_acq_info(ctx);
  131. break;
  132. }
  133. default:
  134. CAM_DBG(CAM_ISP, "DUMP id not valid %u", id);
  135. break;
  136. }
  137. return 0;
  138. }
  139. static void cam_isp_ctx_dump_req(struct cam_isp_ctx_req *req_isp)
  140. {
  141. int i = 0, rc = 0;
  142. size_t len = 0;
  143. uint32_t *buf_addr;
  144. uint32_t *buf_start, *buf_end;
  145. size_t remain_len = 0;
  146. for (i = 0; i < req_isp->num_cfg; i++) {
  147. rc = cam_packet_util_get_cmd_mem_addr(
  148. req_isp->cfg[i].handle, &buf_addr, &len);
  149. if (rc) {
  150. CAM_ERR_RATE_LIMIT(CAM_ISP,
  151. "Failed to get_cmd_mem_addr, rc=%d",
  152. rc);
  153. } else {
  154. if (req_isp->cfg[i].offset >= ((uint32_t)len)) {
  155. CAM_ERR(CAM_ISP,
  156. "Invalid offset exp %u actual %u",
  157. req_isp->cfg[i].offset, (uint32_t)len);
  158. return;
  159. }
  160. remain_len = len - req_isp->cfg[i].offset;
  161. if (req_isp->cfg[i].len >
  162. ((uint32_t)remain_len)) {
  163. CAM_ERR(CAM_ISP,
  164. "Invalid len exp %u remain_len %u",
  165. req_isp->cfg[i].len,
  166. (uint32_t)remain_len);
  167. return;
  168. }
  169. buf_start = (uint32_t *)((uint8_t *) buf_addr +
  170. req_isp->cfg[i].offset);
  171. buf_end = (uint32_t *)((uint8_t *) buf_start +
  172. req_isp->cfg[i].len - 1);
  173. cam_cdm_util_dump_cmd_buf(buf_start, buf_end);
  174. }
  175. }
  176. }
  177. static int __cam_isp_ctx_enqueue_request_in_order(
  178. struct cam_context *ctx, struct cam_ctx_request *req)
  179. {
  180. struct cam_ctx_request *req_current;
  181. struct cam_ctx_request *req_prev;
  182. struct list_head temp_list;
  183. INIT_LIST_HEAD(&temp_list);
  184. spin_lock_bh(&ctx->lock);
  185. if (list_empty(&ctx->pending_req_list)) {
  186. list_add_tail(&req->list, &ctx->pending_req_list);
  187. } else {
  188. list_for_each_entry_safe_reverse(
  189. req_current, req_prev, &ctx->pending_req_list, list) {
  190. if (req->request_id < req_current->request_id) {
  191. list_del_init(&req_current->list);
  192. list_add(&req_current->list, &temp_list);
  193. continue;
  194. } else if (req->request_id == req_current->request_id) {
  195. CAM_WARN(CAM_ISP,
  196. "Received duplicated request %lld",
  197. req->request_id);
  198. }
  199. break;
  200. }
  201. list_add_tail(&req->list, &ctx->pending_req_list);
  202. if (!list_empty(&temp_list)) {
  203. list_for_each_entry_safe(
  204. req_current, req_prev, &temp_list, list) {
  205. list_del_init(&req_current->list);
  206. list_add_tail(&req_current->list,
  207. &ctx->pending_req_list);
  208. }
  209. }
  210. }
  211. spin_unlock_bh(&ctx->lock);
  212. return 0;
  213. }
  214. static int __cam_isp_ctx_enqueue_init_request(
  215. struct cam_context *ctx, struct cam_ctx_request *req)
  216. {
  217. int rc = 0;
  218. struct cam_ctx_request *req_old;
  219. struct cam_isp_ctx_req *req_isp_old;
  220. struct cam_isp_ctx_req *req_isp_new;
  221. spin_lock_bh(&ctx->lock);
  222. if (list_empty(&ctx->pending_req_list)) {
  223. list_add_tail(&req->list, &ctx->pending_req_list);
  224. CAM_DBG(CAM_ISP, "INIT packet added req id= %d",
  225. req->request_id);
  226. goto end;
  227. }
  228. req_old = list_first_entry(&ctx->pending_req_list,
  229. struct cam_ctx_request, list);
  230. req_isp_old = (struct cam_isp_ctx_req *) req_old->req_priv;
  231. req_isp_new = (struct cam_isp_ctx_req *) req->req_priv;
  232. if (req_isp_old->hw_update_data.packet_opcode_type ==
  233. CAM_ISP_PACKET_INIT_DEV) {
  234. if ((req_isp_old->num_cfg + req_isp_new->num_cfg) >=
  235. CAM_ISP_CTX_CFG_MAX) {
  236. CAM_WARN(CAM_ISP, "Can not merge INIT pkt");
  237. rc = -ENOMEM;
  238. }
  239. if (req_isp_old->num_fence_map_out != 0 ||
  240. req_isp_old->num_fence_map_in != 0) {
  241. CAM_WARN(CAM_ISP, "Invalid INIT pkt sequence");
  242. rc = -EINVAL;
  243. }
  244. if (!rc) {
  245. memcpy(req_isp_old->fence_map_out,
  246. req_isp_new->fence_map_out,
  247. sizeof(req_isp_new->fence_map_out[0])*
  248. req_isp_new->num_fence_map_out);
  249. req_isp_old->num_fence_map_out =
  250. req_isp_new->num_fence_map_out;
  251. memcpy(req_isp_old->fence_map_in,
  252. req_isp_new->fence_map_in,
  253. sizeof(req_isp_new->fence_map_in[0])*
  254. req_isp_new->num_fence_map_in);
  255. req_isp_old->num_fence_map_in =
  256. req_isp_new->num_fence_map_in;
  257. memcpy(&req_isp_old->cfg[req_isp_old->num_cfg],
  258. req_isp_new->cfg,
  259. sizeof(req_isp_new->cfg[0])*
  260. req_isp_new->num_cfg);
  261. req_isp_old->num_cfg += req_isp_new->num_cfg;
  262. req_old->request_id = req->request_id;
  263. list_add_tail(&req->list, &ctx->free_req_list);
  264. }
  265. } else {
  266. CAM_WARN(CAM_ISP,
  267. "Received Update pkt before INIT pkt. req_id= %lld",
  268. req->request_id);
  269. rc = -EINVAL;
  270. }
  271. end:
  272. spin_unlock_bh(&ctx->lock);
  273. return rc;
  274. }
  275. static const char *__cam_isp_resource_handle_id_to_type(
  276. uint32_t resource_handle)
  277. {
  278. switch (resource_handle) {
  279. case CAM_ISP_IFE_OUT_RES_FULL:
  280. return "FULL";
  281. case CAM_ISP_IFE_OUT_RES_DS4:
  282. return "DS4";
  283. case CAM_ISP_IFE_OUT_RES_DS16:
  284. return "DS16";
  285. case CAM_ISP_IFE_OUT_RES_RAW_DUMP:
  286. return "RAW_DUMP";
  287. case CAM_ISP_IFE_OUT_RES_FD:
  288. return "FD";
  289. case CAM_ISP_IFE_OUT_RES_PDAF:
  290. return "PDAF";
  291. case CAM_ISP_IFE_OUT_RES_RDI_0:
  292. return "RDI_0";
  293. case CAM_ISP_IFE_OUT_RES_RDI_1:
  294. return "RDI_1";
  295. case CAM_ISP_IFE_OUT_RES_RDI_2:
  296. return "RDI_2";
  297. case CAM_ISP_IFE_OUT_RES_RDI_3:
  298. return "RDI_3";
  299. case CAM_ISP_IFE_OUT_RES_STATS_HDR_BE:
  300. return "STATS_HDR_BE";
  301. case CAM_ISP_IFE_OUT_RES_STATS_HDR_BHIST:
  302. return "STATS_HDR_BHIST";
  303. case CAM_ISP_IFE_OUT_RES_STATS_TL_BG:
  304. return "STATS_TL_BG";
  305. case CAM_ISP_IFE_OUT_RES_STATS_BF:
  306. return "STATS_BF";
  307. case CAM_ISP_IFE_OUT_RES_STATS_AWB_BG:
  308. return "STATS_AWB_BG";
  309. case CAM_ISP_IFE_OUT_RES_STATS_BHIST:
  310. return "STATS_BHIST";
  311. case CAM_ISP_IFE_OUT_RES_STATS_RS:
  312. return "STATS_RS";
  313. case CAM_ISP_IFE_OUT_RES_STATS_CS:
  314. return "STATS_CS";
  315. case CAM_ISP_IFE_OUT_RES_STATS_IHIST:
  316. return "STATS_IHIST";
  317. case CAM_ISP_IFE_OUT_RES_FULL_DISP:
  318. return "FULL_DISP";
  319. case CAM_ISP_IFE_OUT_RES_DS4_DISP:
  320. return "DS4_DISP";
  321. case CAM_ISP_IFE_OUT_RES_DS16_DISP:
  322. return "DS16_DISP";
  323. case CAM_ISP_IFE_OUT_RES_2PD:
  324. return "2PD";
  325. case CAM_ISP_IFE_OUT_RES_RDI_RD:
  326. return "RDI_RD";
  327. case CAM_ISP_IFE_OUT_RES_LCR:
  328. return "LCR";
  329. default:
  330. return "CAM_ISP_Invalid_Resource_Type";
  331. }
  332. }
  333. static uint64_t __cam_isp_ctx_get_event_ts(uint32_t evt_id, void *evt_data)
  334. {
  335. uint64_t ts = 0;
  336. if (!evt_data)
  337. return 0;
  338. switch (evt_id) {
  339. case CAM_ISP_HW_EVENT_ERROR:
  340. ts = ((struct cam_isp_hw_error_event_data *)evt_data)->
  341. timestamp;
  342. break;
  343. case CAM_ISP_HW_EVENT_SOF:
  344. ts = ((struct cam_isp_hw_sof_event_data *)evt_data)->
  345. timestamp;
  346. break;
  347. case CAM_ISP_HW_EVENT_REG_UPDATE:
  348. ts = ((struct cam_isp_hw_reg_update_event_data *)evt_data)->
  349. timestamp;
  350. break;
  351. case CAM_ISP_HW_EVENT_EPOCH:
  352. ts = ((struct cam_isp_hw_epoch_event_data *)evt_data)->
  353. timestamp;
  354. break;
  355. case CAM_ISP_HW_EVENT_EOF:
  356. ts = ((struct cam_isp_hw_eof_event_data *)evt_data)->
  357. timestamp;
  358. break;
  359. case CAM_ISP_HW_EVENT_DONE:
  360. break;
  361. default:
  362. CAM_DBG(CAM_ISP, "Invalid Event Type %d", evt_id);
  363. }
  364. return ts;
  365. }
  366. static void __cam_isp_ctx_send_sof_boot_timestamp(
  367. struct cam_isp_context *ctx_isp, uint64_t request_id,
  368. uint32_t sof_event_status)
  369. {
  370. struct cam_req_mgr_message req_msg;
  371. req_msg.session_hdl = ctx_isp->base->session_hdl;
  372. req_msg.u.frame_msg.frame_id = ctx_isp->frame_id;
  373. req_msg.u.frame_msg.request_id = request_id;
  374. req_msg.u.frame_msg.timestamp = ctx_isp->boot_timestamp;
  375. req_msg.u.frame_msg.link_hdl = ctx_isp->base->link_hdl;
  376. req_msg.u.frame_msg.sof_status = sof_event_status;
  377. CAM_DBG(CAM_ISP,
  378. "request id:%lld frame number:%lld boot time stamp:0x%llx",
  379. request_id, ctx_isp->frame_id,
  380. ctx_isp->boot_timestamp);
  381. if (cam_req_mgr_notify_message(&req_msg,
  382. V4L_EVENT_CAM_REQ_MGR_SOF_BOOT_TS,
  383. V4L_EVENT_CAM_REQ_MGR_EVENT))
  384. CAM_ERR(CAM_ISP,
  385. "Error in notifying the boot time for req id:%lld",
  386. request_id);
  387. }
  388. static void __cam_isp_ctx_send_sof_timestamp(
  389. struct cam_isp_context *ctx_isp, uint64_t request_id,
  390. uint32_t sof_event_status)
  391. {
  392. struct cam_req_mgr_message req_msg;
  393. req_msg.session_hdl = ctx_isp->base->session_hdl;
  394. req_msg.u.frame_msg.frame_id = ctx_isp->frame_id;
  395. req_msg.u.frame_msg.request_id = request_id;
  396. req_msg.u.frame_msg.timestamp = ctx_isp->sof_timestamp_val;
  397. req_msg.u.frame_msg.link_hdl = ctx_isp->base->link_hdl;
  398. req_msg.u.frame_msg.sof_status = sof_event_status;
  399. CAM_DBG(CAM_ISP,
  400. "request id:%lld frame number:%lld SOF time stamp:0x%llx",
  401. request_id, ctx_isp->frame_id,
  402. ctx_isp->sof_timestamp_val);
  403. CAM_DBG(CAM_ISP, "sof status:%d", sof_event_status);
  404. if (cam_req_mgr_notify_message(&req_msg,
  405. V4L_EVENT_CAM_REQ_MGR_SOF, V4L_EVENT_CAM_REQ_MGR_EVENT))
  406. CAM_ERR(CAM_ISP,
  407. "Error in notifying the sof time for req id:%lld",
  408. request_id);
  409. __cam_isp_ctx_send_sof_boot_timestamp(ctx_isp,
  410. request_id, sof_event_status);
  411. }
  412. static void __cam_isp_ctx_handle_buf_done_fail_log(
  413. uint64_t request_id, struct cam_isp_ctx_req *req_isp)
  414. {
  415. int i;
  416. if (req_isp->num_fence_map_out >= CAM_ISP_CTX_RES_MAX) {
  417. CAM_ERR(CAM_ISP,
  418. "Num Resources exceed mMAX %d >= %d ",
  419. req_isp->num_fence_map_out, CAM_ISP_CTX_RES_MAX);
  420. return;
  421. }
  422. CAM_WARN(CAM_ISP,
  423. "Prev Req[%lld] : num_out=%d, num_acked=%d, bubble : report=%d, detected=%d",
  424. request_id, req_isp->num_fence_map_out, req_isp->num_acked,
  425. req_isp->bubble_report, req_isp->bubble_detected);
  426. CAM_WARN(CAM_ISP,
  427. "Resource Handles that fail to generate buf_done in prev frame");
  428. for (i = 0; i < req_isp->num_fence_map_out; i++) {
  429. if (req_isp->fence_map_out[i].sync_id != -1) {
  430. CAM_WARN(CAM_ISP,
  431. "Resource_Handle: [%s][0x%x] Sync_ID: [0x%x]",
  432. __cam_isp_resource_handle_id_to_type(
  433. req_isp->fence_map_out[i].resource_handle),
  434. req_isp->fence_map_out[i].resource_handle,
  435. req_isp->fence_map_out[i].sync_id);
  436. }
  437. }
  438. }
  439. static int __cam_isp_ctx_handle_buf_done_for_request(
  440. struct cam_isp_context *ctx_isp,
  441. struct cam_ctx_request *req,
  442. struct cam_isp_hw_done_event_data *done,
  443. uint32_t bubble_state,
  444. struct cam_isp_hw_done_event_data *done_next_req)
  445. {
  446. int rc = 0;
  447. int i, j;
  448. struct cam_isp_ctx_req *req_isp;
  449. struct cam_context *ctx = ctx_isp->base;
  450. uint64_t buf_done_req_id;
  451. trace_cam_buf_done("ISP", ctx, req);
  452. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  453. CAM_DBG(CAM_ISP, "Enter with bubble_state %d, req_bubble_detected %d",
  454. bubble_state, req_isp->bubble_detected);
  455. if (done_next_req) {
  456. done_next_req->num_handles = 0;
  457. done_next_req->timestamp = done->timestamp;
  458. }
  459. for (i = 0; i < done->num_handles; i++) {
  460. for (j = 0; j < req_isp->num_fence_map_out; j++) {
  461. if (done->resource_handle[i] ==
  462. req_isp->fence_map_out[j].resource_handle)
  463. break;
  464. }
  465. if (j == req_isp->num_fence_map_out) {
  466. CAM_ERR(CAM_ISP,
  467. "Can not find matching lane handle 0x%x!",
  468. done->resource_handle[i]);
  469. rc = -EINVAL;
  470. continue;
  471. }
  472. if (req_isp->fence_map_out[j].sync_id == -1) {
  473. CAM_WARN(CAM_ISP,
  474. "Duplicate BUF_DONE for req %lld : i=%d, j=%d, res=%s",
  475. req->request_id, i, j,
  476. __cam_isp_resource_handle_id_to_type(
  477. done->resource_handle[i]));
  478. if (done_next_req) {
  479. done_next_req->resource_handle
  480. [done_next_req->num_handles++] =
  481. done->resource_handle[i];
  482. }
  483. continue;
  484. }
  485. if (!req_isp->bubble_detected) {
  486. CAM_DBG(CAM_ISP,
  487. "Sync with success: req %lld res 0x%x fd 0x%x, ctx %u",
  488. req->request_id,
  489. req_isp->fence_map_out[j].resource_handle,
  490. req_isp->fence_map_out[j].sync_id,
  491. ctx->ctx_id);
  492. rc = cam_sync_signal(req_isp->fence_map_out[j].sync_id,
  493. CAM_SYNC_STATE_SIGNALED_SUCCESS);
  494. if (rc)
  495. CAM_DBG(CAM_ISP, "Sync failed with rc = %d",
  496. rc);
  497. } else if (!req_isp->bubble_report) {
  498. CAM_ERR(CAM_ISP,
  499. "Sync with failure: req %lld res 0x%x fd 0x%x, ctx %u",
  500. req->request_id,
  501. req_isp->fence_map_out[j].resource_handle,
  502. req_isp->fence_map_out[j].sync_id,
  503. ctx->ctx_id);
  504. rc = cam_sync_signal(req_isp->fence_map_out[j].sync_id,
  505. CAM_SYNC_STATE_SIGNALED_ERROR);
  506. if (rc)
  507. CAM_ERR(CAM_ISP, "Sync failed with rc = %d",
  508. rc);
  509. } else {
  510. /*
  511. * Ignore the buffer done if bubble detect is on
  512. * Increment the ack number here, and queue the
  513. * request back to pending list whenever all the
  514. * buffers are done.
  515. */
  516. req_isp->num_acked++;
  517. CAM_DBG(CAM_ISP,
  518. "buf done with bubble state %d recovery %d",
  519. bubble_state, req_isp->bubble_report);
  520. continue;
  521. }
  522. CAM_DBG(CAM_ISP, "req %lld, reset sync id 0x%x ctx %u",
  523. req->request_id,
  524. req_isp->fence_map_out[j].sync_id, ctx->ctx_id);
  525. if (!rc) {
  526. req_isp->num_acked++;
  527. req_isp->fence_map_out[j].sync_id = -1;
  528. }
  529. }
  530. if (req_isp->num_acked > req_isp->num_fence_map_out) {
  531. /* Should not happen */
  532. CAM_ERR(CAM_ISP,
  533. "WARNING: req_id %lld num_acked %d > map_out %d, ctx %u",
  534. req->request_id, req_isp->num_acked,
  535. req_isp->num_fence_map_out, ctx->ctx_id);
  536. WARN_ON(req_isp->num_acked > req_isp->num_fence_map_out);
  537. }
  538. if (req_isp->num_acked != req_isp->num_fence_map_out)
  539. return rc;
  540. ctx_isp->active_req_cnt--;
  541. buf_done_req_id = req->request_id;
  542. if (req_isp->bubble_detected && req_isp->bubble_report) {
  543. req_isp->num_acked = 0;
  544. req_isp->bubble_detected = false;
  545. list_del_init(&req->list);
  546. list_add(&req->list, &ctx->pending_req_list);
  547. atomic_set(&ctx_isp->process_bubble, 0);
  548. CAM_DBG(CAM_REQ,
  549. "Move active request %lld to pending list(cnt = %d) [bubble recovery], ctx %u",
  550. req->request_id, ctx_isp->active_req_cnt, ctx->ctx_id);
  551. } else {
  552. if (ctx_isp->reported_req_id < buf_done_req_id) {
  553. ctx_isp->reported_req_id = buf_done_req_id;
  554. __cam_isp_ctx_send_sof_timestamp(ctx_isp,
  555. buf_done_req_id, CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  556. }
  557. list_del_init(&req->list);
  558. list_add_tail(&req->list, &ctx->free_req_list);
  559. CAM_DBG(CAM_REQ,
  560. "Move active request %lld to free list(cnt = %d) [all fences done], ctx %u",
  561. buf_done_req_id, ctx_isp->active_req_cnt, ctx->ctx_id);
  562. }
  563. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  564. CAM_ISP_STATE_CHANGE_TRIGGER_DONE, buf_done_req_id);
  565. return rc;
  566. }
  567. static int __cam_isp_ctx_handle_buf_done_in_activated_state(
  568. struct cam_isp_context *ctx_isp,
  569. struct cam_isp_hw_done_event_data *done,
  570. uint32_t bubble_state)
  571. {
  572. int rc = 0;
  573. struct cam_ctx_request *req;
  574. struct cam_context *ctx = ctx_isp->base;
  575. struct cam_isp_hw_done_event_data done_next_req;
  576. if (list_empty(&ctx->active_req_list)) {
  577. CAM_DBG(CAM_ISP, "Buf done with no active request");
  578. return 0;
  579. }
  580. req = list_first_entry(&ctx->active_req_list,
  581. struct cam_ctx_request, list);
  582. rc = __cam_isp_ctx_handle_buf_done_for_request(ctx_isp, req, done,
  583. bubble_state, &done_next_req);
  584. if (done_next_req.num_handles) {
  585. struct cam_isp_hw_done_event_data unhandled_res;
  586. struct cam_ctx_request *next_req = list_next_entry(req, list);
  587. if (next_req) {
  588. /*
  589. * Few resource handles are already signalled in the
  590. * current request, lets check if there is another
  591. * request waiting for these resources. This can
  592. * happen if handling some of next request's buf done
  593. * events are happening first before handling current
  594. * request's remaining buf dones due to IRQ scheduling.
  595. * Lets check only one more request as we will have
  596. * maximum of 2 requests in active_list at any time.
  597. */
  598. CAM_WARN(CAM_ISP,
  599. "Unhandled buf done resources for req %lld, trying next request %lld in active_list",
  600. req->request_id, next_req->request_id);
  601. __cam_isp_ctx_handle_buf_done_for_request(ctx_isp,
  602. next_req, &done_next_req,
  603. bubble_state, &unhandled_res);
  604. if (unhandled_res.num_handles == 0)
  605. CAM_INFO(CAM_ISP,
  606. "BUF Done event handed for next request %lld",
  607. next_req->request_id);
  608. else
  609. CAM_ERR(CAM_ISP,
  610. "BUF Done not handled for next request %lld",
  611. next_req->request_id);
  612. }
  613. }
  614. return rc;
  615. }
  616. static int __cam_isp_ctx_reg_upd_in_epoch_state(
  617. struct cam_isp_context *ctx_isp, void *evt_data)
  618. {
  619. if (ctx_isp->frame_id == 1)
  620. CAM_DBG(CAM_ISP, "Reg update for early PCR");
  621. else
  622. CAM_WARN(CAM_ISP,
  623. "Unexpected reg update in activated substate:%d for frame_id:%lld",
  624. ctx_isp->substate_activated, ctx_isp->frame_id);
  625. return 0;
  626. }
  627. static int __cam_isp_ctx_reg_upd_in_applied_state(
  628. struct cam_isp_context *ctx_isp, void *evt_data)
  629. {
  630. int rc = 0;
  631. struct cam_ctx_request *req;
  632. struct cam_context *ctx = ctx_isp->base;
  633. struct cam_isp_ctx_req *req_isp;
  634. uint64_t request_id = 0;
  635. if (list_empty(&ctx->wait_req_list)) {
  636. CAM_ERR(CAM_ISP, "Reg upd ack with no waiting request");
  637. goto end;
  638. }
  639. req = list_first_entry(&ctx->wait_req_list,
  640. struct cam_ctx_request, list);
  641. list_del_init(&req->list);
  642. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  643. if (req_isp->num_fence_map_out != 0) {
  644. list_add_tail(&req->list, &ctx->active_req_list);
  645. ctx_isp->active_req_cnt++;
  646. request_id = req->request_id;
  647. CAM_DBG(CAM_REQ,
  648. "move request %lld to active list(cnt = %d), ctx %u",
  649. req->request_id, ctx_isp->active_req_cnt, ctx->ctx_id);
  650. } else {
  651. /* no io config, so the request is completed. */
  652. list_add_tail(&req->list, &ctx->free_req_list);
  653. CAM_DBG(CAM_ISP,
  654. "move active request %lld to free list(cnt = %d), ctx %u",
  655. req->request_id, ctx_isp->active_req_cnt, ctx->ctx_id);
  656. }
  657. /*
  658. * This function only called directly from applied and bubble applied
  659. * state so change substate here.
  660. */
  661. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_EPOCH;
  662. CAM_DBG(CAM_ISP, "next substate %d", ctx_isp->substate_activated);
  663. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  664. CAM_ISP_STATE_CHANGE_TRIGGER_REG_UPDATE, request_id);
  665. end:
  666. return rc;
  667. }
  668. static int __cam_isp_ctx_notify_sof_in_activated_state(
  669. struct cam_isp_context *ctx_isp, void *evt_data)
  670. {
  671. int rc = 0;
  672. struct cam_req_mgr_trigger_notify notify;
  673. struct cam_context *ctx = ctx_isp->base;
  674. struct cam_ctx_request *req;
  675. uint64_t request_id = 0;
  676. /*
  677. * notify reqmgr with sof signal. Note, due to scheduling delay
  678. * we can run into situation that two active requests has already
  679. * be in the active queue while we try to do the notification.
  680. * In this case, we need to skip the current notification. This
  681. * helps the state machine to catch up the delay.
  682. */
  683. if (ctx->ctx_crm_intf && ctx->ctx_crm_intf->notify_trigger &&
  684. ctx_isp->active_req_cnt <= 2) {
  685. if (ctx_isp->subscribe_event & CAM_TRIGGER_POINT_SOF) {
  686. notify.link_hdl = ctx->link_hdl;
  687. notify.dev_hdl = ctx->dev_hdl;
  688. notify.frame_id = ctx_isp->frame_id;
  689. notify.trigger = CAM_TRIGGER_POINT_SOF;
  690. ctx->ctx_crm_intf->notify_trigger(&notify);
  691. CAM_DBG(CAM_ISP, "Notify CRM SOF frame %lld ctx %u",
  692. ctx_isp->frame_id, ctx->ctx_id);
  693. }
  694. list_for_each_entry(req, &ctx->active_req_list, list) {
  695. if (req->request_id > ctx_isp->reported_req_id) {
  696. request_id = req->request_id;
  697. ctx_isp->reported_req_id = request_id;
  698. break;
  699. }
  700. }
  701. if (ctx_isp->substate_activated == CAM_ISP_CTX_ACTIVATED_BUBBLE)
  702. request_id = 0;
  703. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  704. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  705. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  706. CAM_ISP_STATE_CHANGE_TRIGGER_EPOCH,
  707. request_id);
  708. } else {
  709. CAM_ERR_RATE_LIMIT(CAM_ISP,
  710. "Can not notify SOF to CRM for ctx %u",
  711. ctx->ctx_id);
  712. rc = -EFAULT;
  713. }
  714. return 0;
  715. }
  716. static int __cam_isp_ctx_notify_eof_in_activated_state(
  717. struct cam_isp_context *ctx_isp, void *evt_data)
  718. {
  719. int rc = 0;
  720. struct cam_req_mgr_trigger_notify notify;
  721. struct cam_context *ctx = ctx_isp->base;
  722. if (!(ctx_isp->subscribe_event & CAM_TRIGGER_POINT_EOF))
  723. return rc;
  724. /* notify reqmgr with eof signal */
  725. if (ctx->ctx_crm_intf && ctx->ctx_crm_intf->notify_trigger) {
  726. notify.link_hdl = ctx->link_hdl;
  727. notify.dev_hdl = ctx->dev_hdl;
  728. notify.frame_id = ctx_isp->frame_id;
  729. notify.trigger = CAM_TRIGGER_POINT_EOF;
  730. ctx->ctx_crm_intf->notify_trigger(&notify);
  731. CAM_DBG(CAM_ISP, "Notify CRM EOF frame %lld ctx %u",
  732. ctx_isp->frame_id, ctx->ctx_id);
  733. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  734. CAM_ISP_STATE_CHANGE_TRIGGER_EOF, 0);
  735. } else {
  736. CAM_ERR(CAM_ISP, "Can not notify EOF to CRM for ctx %u",
  737. ctx->ctx_id);
  738. rc = -EFAULT;
  739. }
  740. return rc;
  741. }
  742. static int __cam_isp_ctx_reg_upd_in_hw_error(
  743. struct cam_isp_context *ctx_isp, void *evt_data)
  744. {
  745. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_SOF;
  746. return 0;
  747. }
  748. static int __cam_isp_ctx_sof_in_activated_state(
  749. struct cam_isp_context *ctx_isp, void *evt_data)
  750. {
  751. int rc = 0;
  752. struct cam_isp_hw_sof_event_data *sof_event_data = evt_data;
  753. struct cam_ctx_request *req = NULL;
  754. struct cam_context *ctx = ctx_isp->base;
  755. uint64_t request_id = 0;
  756. /* First check if there is a valid request in active list */
  757. list_for_each_entry(req, &ctx->active_req_list, list) {
  758. if (req->request_id > ctx_isp->reported_req_id) {
  759. request_id = req->request_id;
  760. break;
  761. }
  762. }
  763. /*
  764. * If nothing in active list, current request might have not moved
  765. * from wait to active list. This could happen if REG_UPDATE to sw
  766. * is coming immediately after SOF
  767. */
  768. if (request_id == 0) {
  769. req = list_first_entry(&ctx->wait_req_list,
  770. struct cam_ctx_request, list);
  771. if (req)
  772. request_id = req->request_id;
  773. }
  774. if (!evt_data) {
  775. CAM_ERR(CAM_ISP, "in valid sof event data");
  776. return -EINVAL;
  777. }
  778. ctx_isp->frame_id++;
  779. ctx_isp->sof_timestamp_val = sof_event_data->timestamp;
  780. ctx_isp->boot_timestamp = sof_event_data->boot_time;
  781. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  782. CAM_ISP_STATE_CHANGE_TRIGGER_SOF, request_id);
  783. CAM_DBG(CAM_ISP, "frame id: %lld time stamp:0x%llx, ctx %u",
  784. ctx_isp->frame_id, ctx_isp->sof_timestamp_val, ctx->ctx_id);
  785. return rc;
  786. }
  787. static int __cam_isp_ctx_reg_upd_in_sof(struct cam_isp_context *ctx_isp,
  788. void *evt_data)
  789. {
  790. int rc = 0;
  791. struct cam_ctx_request *req = NULL;
  792. struct cam_isp_ctx_req *req_isp;
  793. struct cam_context *ctx = ctx_isp->base;
  794. if (ctx->state != CAM_CTX_ACTIVATED && ctx_isp->frame_id > 1) {
  795. CAM_DBG(CAM_ISP, "invalid RUP");
  796. goto end;
  797. }
  798. /*
  799. * This is for the first update. The initial setting will
  800. * cause the reg_upd in the first frame.
  801. */
  802. if (!list_empty(&ctx->wait_req_list)) {
  803. req = list_first_entry(&ctx->wait_req_list,
  804. struct cam_ctx_request, list);
  805. list_del_init(&req->list);
  806. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  807. if (req_isp->num_fence_map_out == req_isp->num_acked)
  808. list_add_tail(&req->list, &ctx->free_req_list);
  809. else
  810. CAM_ERR(CAM_ISP,
  811. "receive rup in unexpected state");
  812. }
  813. if (req != NULL) {
  814. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  815. CAM_ISP_STATE_CHANGE_TRIGGER_REG_UPDATE,
  816. req->request_id);
  817. }
  818. end:
  819. return rc;
  820. }
  821. static int __cam_isp_ctx_epoch_in_applied(struct cam_isp_context *ctx_isp,
  822. void *evt_data)
  823. {
  824. struct cam_ctx_request *req;
  825. struct cam_isp_ctx_req *req_isp;
  826. struct cam_context *ctx = ctx_isp->base;
  827. uint64_t request_id = 0;
  828. if (list_empty(&ctx->wait_req_list)) {
  829. /*
  830. * If no wait req in epoch, this is an error case.
  831. * The recovery is to go back to sof state
  832. */
  833. CAM_ERR(CAM_ISP, "No wait request");
  834. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_SOF;
  835. /* Send SOF event as empty frame*/
  836. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  837. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  838. goto end;
  839. }
  840. req = list_first_entry(&ctx->wait_req_list, struct cam_ctx_request,
  841. list);
  842. req_isp = (struct cam_isp_ctx_req *)req->req_priv;
  843. req_isp->bubble_detected = true;
  844. CAM_DBG(CAM_ISP, "Report Bubble flag %d", req_isp->bubble_report);
  845. if (req_isp->bubble_report && ctx->ctx_crm_intf &&
  846. ctx->ctx_crm_intf->notify_err) {
  847. struct cam_req_mgr_error_notify notify;
  848. notify.link_hdl = ctx->link_hdl;
  849. notify.dev_hdl = ctx->dev_hdl;
  850. notify.req_id = req->request_id;
  851. notify.error = CRM_KMD_ERR_BUBBLE;
  852. CAM_WARN(CAM_ISP,
  853. "Notify CRM about Bubble req %lld frame %lld, ctx %u",
  854. req->request_id, ctx_isp->frame_id, ctx->ctx_id);
  855. ctx->ctx_crm_intf->notify_err(&notify);
  856. atomic_set(&ctx_isp->process_bubble, 1);
  857. } else {
  858. req_isp->bubble_report = 0;
  859. }
  860. /*
  861. * Always move the request to active list. Let buf done
  862. * function handles the rest.
  863. */
  864. list_del_init(&req->list);
  865. list_add_tail(&req->list, &ctx->active_req_list);
  866. ctx_isp->active_req_cnt++;
  867. CAM_DBG(CAM_REQ, "move request %lld to active list(cnt = %d), ctx %u",
  868. req->request_id, ctx_isp->active_req_cnt, ctx->ctx_id);
  869. if (req->request_id > ctx_isp->reported_req_id) {
  870. request_id = req->request_id;
  871. ctx_isp->reported_req_id = request_id;
  872. }
  873. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  874. CAM_REQ_MGR_SOF_EVENT_ERROR);
  875. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_BUBBLE;
  876. CAM_DBG(CAM_ISP, "next substate %d",
  877. ctx_isp->substate_activated);
  878. end:
  879. if (request_id == 0) {
  880. req = list_last_entry(&ctx->active_req_list,
  881. struct cam_ctx_request, list);
  882. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  883. CAM_ISP_STATE_CHANGE_TRIGGER_EPOCH, req->request_id);
  884. } else {
  885. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  886. CAM_ISP_STATE_CHANGE_TRIGGER_EPOCH, request_id);
  887. }
  888. return 0;
  889. }
  890. static int __cam_isp_ctx_buf_done_in_applied(struct cam_isp_context *ctx_isp,
  891. void *evt_data)
  892. {
  893. int rc = 0;
  894. struct cam_isp_hw_done_event_data *done =
  895. (struct cam_isp_hw_done_event_data *) evt_data;
  896. rc = __cam_isp_ctx_handle_buf_done_in_activated_state(ctx_isp, done, 0);
  897. return rc;
  898. }
  899. static int __cam_isp_ctx_sof_in_epoch(struct cam_isp_context *ctx_isp,
  900. void *evt_data)
  901. {
  902. int rc = 0;
  903. struct cam_context *ctx = ctx_isp->base;
  904. struct cam_isp_hw_sof_event_data *sof_event_data = evt_data;
  905. struct cam_ctx_request *req;
  906. if (!evt_data) {
  907. CAM_ERR(CAM_ISP, "in valid sof event data");
  908. return -EINVAL;
  909. }
  910. ctx_isp->frame_id++;
  911. ctx_isp->sof_timestamp_val = sof_event_data->timestamp;
  912. ctx_isp->boot_timestamp = sof_event_data->boot_time;
  913. if (list_empty(&ctx->active_req_list))
  914. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_SOF;
  915. else
  916. CAM_DBG(CAM_ISP, "Still need to wait for the buf done");
  917. req = list_last_entry(&ctx->active_req_list,
  918. struct cam_ctx_request, list);
  919. if (req)
  920. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  921. CAM_ISP_STATE_CHANGE_TRIGGER_SOF,
  922. req->request_id);
  923. CAM_DBG(CAM_ISP, "next substate %d",
  924. ctx_isp->substate_activated);
  925. return rc;
  926. }
  927. static int __cam_isp_ctx_buf_done_in_epoch(struct cam_isp_context *ctx_isp,
  928. void *evt_data)
  929. {
  930. int rc = 0;
  931. struct cam_isp_hw_done_event_data *done =
  932. (struct cam_isp_hw_done_event_data *) evt_data;
  933. rc = __cam_isp_ctx_handle_buf_done_in_activated_state(ctx_isp, done, 0);
  934. return rc;
  935. }
  936. static int __cam_isp_ctx_buf_done_in_bubble(
  937. struct cam_isp_context *ctx_isp, void *evt_data)
  938. {
  939. int rc = 0;
  940. struct cam_isp_hw_done_event_data *done =
  941. (struct cam_isp_hw_done_event_data *) evt_data;
  942. rc = __cam_isp_ctx_handle_buf_done_in_activated_state(ctx_isp, done, 1);
  943. return rc;
  944. }
  945. static int __cam_isp_ctx_epoch_in_bubble_applied(
  946. struct cam_isp_context *ctx_isp, void *evt_data)
  947. {
  948. struct cam_ctx_request *req;
  949. struct cam_isp_ctx_req *req_isp;
  950. struct cam_context *ctx = ctx_isp->base;
  951. uint64_t request_id = 0;
  952. /*
  953. * This means we missed the reg upd ack. So we need to
  954. * transition to BUBBLE state again.
  955. */
  956. if (list_empty(&ctx->wait_req_list)) {
  957. /*
  958. * If no pending req in epoch, this is an error case.
  959. * Just go back to the bubble state.
  960. */
  961. CAM_ERR(CAM_ISP, "No pending request.");
  962. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  963. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  964. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_BUBBLE;
  965. goto end;
  966. }
  967. req = list_first_entry(&ctx->wait_req_list, struct cam_ctx_request,
  968. list);
  969. req_isp = (struct cam_isp_ctx_req *)req->req_priv;
  970. req_isp->bubble_detected = true;
  971. if (req_isp->bubble_report && ctx->ctx_crm_intf &&
  972. ctx->ctx_crm_intf->notify_err) {
  973. struct cam_req_mgr_error_notify notify;
  974. notify.link_hdl = ctx->link_hdl;
  975. notify.dev_hdl = ctx->dev_hdl;
  976. notify.req_id = req->request_id;
  977. notify.error = CRM_KMD_ERR_BUBBLE;
  978. CAM_WARN(CAM_REQ,
  979. "Notify CRM about Bubble req_id %llu frame %lld, ctx %u",
  980. req->request_id, ctx_isp->frame_id, ctx->ctx_id);
  981. ctx->ctx_crm_intf->notify_err(&notify);
  982. atomic_set(&ctx_isp->process_bubble, 1);
  983. } else {
  984. req_isp->bubble_report = 0;
  985. }
  986. /*
  987. * Always move the request to active list. Let buf done
  988. * function handles the rest.
  989. */
  990. list_del_init(&req->list);
  991. list_add_tail(&req->list, &ctx->active_req_list);
  992. ctx_isp->active_req_cnt++;
  993. CAM_DBG(CAM_ISP, "move request %lld to active list(cnt = %d) ctx %u",
  994. req->request_id, ctx_isp->active_req_cnt);
  995. if (!req_isp->bubble_report) {
  996. if (req->request_id > ctx_isp->reported_req_id) {
  997. request_id = req->request_id;
  998. ctx_isp->reported_req_id = request_id;
  999. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  1000. CAM_REQ_MGR_SOF_EVENT_ERROR);
  1001. } else
  1002. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  1003. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  1004. } else
  1005. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  1006. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  1007. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_BUBBLE;
  1008. CAM_DBG(CAM_ISP, "next substate %d", ctx_isp->substate_activated);
  1009. end:
  1010. req = list_last_entry(&ctx->active_req_list, struct cam_ctx_request,
  1011. list);
  1012. if (req)
  1013. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  1014. CAM_ISP_STATE_CHANGE_TRIGGER_EPOCH, req->request_id);
  1015. return 0;
  1016. }
  1017. static int __cam_isp_ctx_buf_done_in_bubble_applied(
  1018. struct cam_isp_context *ctx_isp, void *evt_data)
  1019. {
  1020. int rc = 0;
  1021. struct cam_isp_hw_done_event_data *done =
  1022. (struct cam_isp_hw_done_event_data *) evt_data;
  1023. rc = __cam_isp_ctx_handle_buf_done_in_activated_state(ctx_isp, done, 1);
  1024. return rc;
  1025. }
  1026. static int __cam_isp_ctx_handle_error(struct cam_isp_context *ctx_isp,
  1027. void *evt_data)
  1028. {
  1029. int rc = 0;
  1030. uint32_t i = 0;
  1031. bool found = 0;
  1032. struct cam_ctx_request *req = NULL;
  1033. struct cam_ctx_request *req_to_report = NULL;
  1034. struct cam_ctx_request *req_to_dump = NULL;
  1035. struct cam_ctx_request *req_temp;
  1036. struct cam_isp_ctx_req *req_isp = NULL;
  1037. struct cam_isp_ctx_req *req_isp_to_report = NULL;
  1038. struct cam_req_mgr_error_notify notify = {};
  1039. uint64_t error_request_id;
  1040. struct cam_hw_fence_map_entry *fence_map_out = NULL;
  1041. struct cam_req_mgr_message req_msg;
  1042. struct cam_context *ctx = ctx_isp->base;
  1043. struct cam_isp_hw_error_event_data *error_event_data =
  1044. (struct cam_isp_hw_error_event_data *)evt_data;
  1045. uint32_t error_type = error_event_data->error_type;
  1046. CAM_DBG(CAM_ISP, "Enter error_type = %d", error_type);
  1047. if ((error_type == CAM_ISP_HW_ERROR_OVERFLOW) ||
  1048. (error_type == CAM_ISP_HW_ERROR_BUSIF_OVERFLOW) ||
  1049. (error_type == CAM_ISP_HW_ERROR_VIOLATION)) {
  1050. struct cam_hw_cmd_args hw_cmd_args;
  1051. memset(&hw_cmd_args, 0, sizeof(hw_cmd_args));
  1052. hw_cmd_args.ctxt_to_hw_map = ctx->ctxt_to_hw_map;
  1053. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_REG_DUMP_ON_ERROR;
  1054. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  1055. &hw_cmd_args);
  1056. if (rc) {
  1057. CAM_ERR(CAM_ISP, "Reg dump on error failed rc: %d", rc);
  1058. rc = 0;
  1059. }
  1060. }
  1061. /*
  1062. * The error is likely caused by first request on the active list.
  1063. * If active list is empty check wait list (maybe error hit as soon
  1064. * as RUP and we handle error before RUP.
  1065. */
  1066. if (list_empty(&ctx->active_req_list)) {
  1067. CAM_DBG(CAM_ISP,
  1068. "handling error with no active request");
  1069. if (list_empty(&ctx->wait_req_list)) {
  1070. CAM_ERR_RATE_LIMIT(CAM_ISP,
  1071. "Error with no active/wait request");
  1072. goto end;
  1073. } else {
  1074. req_to_dump = list_first_entry(&ctx->wait_req_list,
  1075. struct cam_ctx_request, list);
  1076. }
  1077. } else {
  1078. req_to_dump = list_first_entry(&ctx->active_req_list,
  1079. struct cam_ctx_request, list);
  1080. }
  1081. req_isp = (struct cam_isp_ctx_req *) req_to_dump->req_priv;
  1082. if (error_event_data->enable_req_dump)
  1083. cam_isp_ctx_dump_req(req_isp);
  1084. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  1085. CAM_ISP_STATE_CHANGE_TRIGGER_ERROR, req_to_dump->request_id);
  1086. list_for_each_entry_safe(req, req_temp,
  1087. &ctx->active_req_list, list) {
  1088. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  1089. if (!req_isp->bubble_report) {
  1090. for (i = 0; i < req_isp->num_fence_map_out; i++) {
  1091. fence_map_out =
  1092. &req_isp->fence_map_out[i];
  1093. CAM_ERR(CAM_ISP,
  1094. "req %llu, Sync fd 0x%x ctx %u",
  1095. req->request_id,
  1096. req_isp->fence_map_out[i].sync_id,
  1097. ctx->ctx_id);
  1098. if (req_isp->fence_map_out[i].sync_id != -1) {
  1099. rc = cam_sync_signal(
  1100. fence_map_out->sync_id,
  1101. CAM_SYNC_STATE_SIGNALED_ERROR);
  1102. fence_map_out->sync_id = -1;
  1103. }
  1104. }
  1105. list_del_init(&req->list);
  1106. list_add_tail(&req->list, &ctx->free_req_list);
  1107. ctx_isp->active_req_cnt--;
  1108. } else {
  1109. found = 1;
  1110. break;
  1111. }
  1112. }
  1113. if (found)
  1114. goto move_to_pending;
  1115. list_for_each_entry_safe(req, req_temp,
  1116. &ctx->wait_req_list, list) {
  1117. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  1118. if (!req_isp->bubble_report) {
  1119. for (i = 0; i < req_isp->num_fence_map_out; i++) {
  1120. fence_map_out =
  1121. &req_isp->fence_map_out[i];
  1122. CAM_ERR(CAM_ISP,
  1123. "req %llu, Sync fd 0x%x ctx %u",
  1124. req->request_id,
  1125. req_isp->fence_map_out[i].sync_id,
  1126. ctx->ctx_id);
  1127. if (req_isp->fence_map_out[i].sync_id != -1) {
  1128. rc = cam_sync_signal(
  1129. fence_map_out->sync_id,
  1130. CAM_SYNC_STATE_SIGNALED_ERROR);
  1131. fence_map_out->sync_id = -1;
  1132. }
  1133. }
  1134. list_del_init(&req->list);
  1135. list_add_tail(&req->list, &ctx->free_req_list);
  1136. ctx_isp->active_req_cnt--;
  1137. } else {
  1138. found = 1;
  1139. break;
  1140. }
  1141. }
  1142. move_to_pending:
  1143. /*
  1144. * If bubble recovery is enabled on any request we need to move that
  1145. * request and all the subsequent requests to the pending list.
  1146. * Note:
  1147. * We need to traverse the active list in reverse order and add
  1148. * to head of pending list.
  1149. * e.g. pending current state: 10, 11 | active current state: 8, 9
  1150. * intermittent for loop iteration- pending: 9, 10, 11 | active: 8
  1151. * final state - pending: 8, 9, 10, 11 | active: NULL
  1152. */
  1153. if (found) {
  1154. list_for_each_entry_safe_reverse(req, req_temp,
  1155. &ctx->active_req_list, list) {
  1156. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  1157. list_del_init(&req->list);
  1158. list_add(&req->list, &ctx->pending_req_list);
  1159. ctx_isp->active_req_cnt--;
  1160. }
  1161. list_for_each_entry_safe_reverse(req, req_temp,
  1162. &ctx->wait_req_list, list) {
  1163. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  1164. list_del_init(&req->list);
  1165. list_add(&req->list, &ctx->pending_req_list);
  1166. ctx_isp->active_req_cnt--;
  1167. }
  1168. }
  1169. end:
  1170. do {
  1171. if (list_empty(&ctx->pending_req_list)) {
  1172. error_request_id = ctx_isp->last_applied_req_id + 1;
  1173. req_isp = NULL;
  1174. break;
  1175. }
  1176. req = list_first_entry(&ctx->pending_req_list,
  1177. struct cam_ctx_request, list);
  1178. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  1179. error_request_id = ctx_isp->last_applied_req_id;
  1180. if (req_isp->bubble_report) {
  1181. req_to_report = req;
  1182. req_isp_to_report = req_to_report->req_priv;
  1183. break;
  1184. }
  1185. for (i = 0; i < req_isp->num_fence_map_out; i++) {
  1186. if (req_isp->fence_map_out[i].sync_id != -1)
  1187. rc = cam_sync_signal(
  1188. req_isp->fence_map_out[i].sync_id,
  1189. CAM_SYNC_STATE_SIGNALED_ERROR);
  1190. req_isp->fence_map_out[i].sync_id = -1;
  1191. }
  1192. list_del_init(&req->list);
  1193. list_add_tail(&req->list, &ctx->free_req_list);
  1194. } while (req->request_id < ctx_isp->last_applied_req_id);
  1195. if (ctx->ctx_crm_intf && ctx->ctx_crm_intf->notify_err) {
  1196. notify.link_hdl = ctx->link_hdl;
  1197. notify.dev_hdl = ctx->dev_hdl;
  1198. notify.req_id = error_request_id;
  1199. if (req_isp_to_report && req_isp_to_report->bubble_report) {
  1200. if (error_event_data->recovery_enabled)
  1201. notify.error = CRM_KMD_ERR_BUBBLE;
  1202. } else {
  1203. notify.error = CRM_KMD_ERR_FATAL;
  1204. }
  1205. CAM_WARN(CAM_ISP,
  1206. "Notify CRM: req %lld, frame %lld ctx %u, error %d",
  1207. error_request_id, ctx_isp->frame_id, ctx->ctx_id,
  1208. notify.error);
  1209. ctx->ctx_crm_intf->notify_err(&notify);
  1210. /*
  1211. * Need to send error occurred in KMD
  1212. * This will help UMD to take necessary action
  1213. * and to dump relevant info
  1214. */
  1215. if (notify.error == CRM_KMD_ERR_FATAL) {
  1216. req_msg.session_hdl = ctx_isp->base->session_hdl;
  1217. req_msg.u.err_msg.device_hdl = ctx_isp->base->dev_hdl;
  1218. req_msg.u.err_msg.error_type =
  1219. CAM_REQ_MGR_ERROR_TYPE_RECOVERY;
  1220. req_msg.u.err_msg.link_hdl = ctx_isp->base->link_hdl;
  1221. req_msg.u.err_msg.request_id = error_request_id;
  1222. req_msg.u.err_msg.resource_size = 0x0;
  1223. if (cam_req_mgr_notify_message(&req_msg,
  1224. V4L_EVENT_CAM_REQ_MGR_ERROR,
  1225. V4L_EVENT_CAM_REQ_MGR_EVENT))
  1226. CAM_ERR(CAM_ISP,
  1227. "Error in notifying the error time for req id:%lld ctx %u",
  1228. ctx_isp->last_applied_req_id,
  1229. ctx->ctx_id);
  1230. }
  1231. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_HW_ERROR;
  1232. } else {
  1233. CAM_ERR_RATE_LIMIT(CAM_ISP,
  1234. "Can not notify ERRROR to CRM for ctx %u",
  1235. ctx->ctx_id);
  1236. rc = -EFAULT;
  1237. }
  1238. CAM_DBG(CAM_ISP, "Exit");
  1239. return rc;
  1240. }
  1241. static int __cam_isp_ctx_fs2_sof_in_sof_state(
  1242. struct cam_isp_context *ctx_isp, void *evt_data)
  1243. {
  1244. int rc = 0;
  1245. struct cam_isp_hw_sof_event_data *sof_event_data = evt_data;
  1246. struct cam_ctx_request *req;
  1247. struct cam_context *ctx = ctx_isp->base;
  1248. struct cam_req_mgr_trigger_notify notify;
  1249. uint64_t request_id = 0;
  1250. if (!evt_data) {
  1251. CAM_ERR(CAM_ISP, "in valid sof event data");
  1252. return -EINVAL;
  1253. }
  1254. ctx_isp->frame_id++;
  1255. ctx_isp->sof_timestamp_val = sof_event_data->timestamp;
  1256. ctx_isp->boot_timestamp = sof_event_data->boot_time;
  1257. CAM_DBG(CAM_ISP, "frame id: %lld time stamp:0x%llx",
  1258. ctx_isp->frame_id, ctx_isp->sof_timestamp_val);
  1259. if (!(list_empty(&ctx->wait_req_list)))
  1260. goto end;
  1261. if (ctx->ctx_crm_intf && ctx->ctx_crm_intf->notify_trigger &&
  1262. ctx_isp->active_req_cnt <= 2) {
  1263. if (ctx_isp->subscribe_event & CAM_TRIGGER_POINT_SOF) {
  1264. notify.link_hdl = ctx->link_hdl;
  1265. notify.dev_hdl = ctx->dev_hdl;
  1266. notify.frame_id = ctx_isp->frame_id;
  1267. notify.trigger = CAM_TRIGGER_POINT_SOF;
  1268. ctx->ctx_crm_intf->notify_trigger(&notify);
  1269. CAM_DBG(CAM_ISP, "Notify CRM SOF frame %lld",
  1270. ctx_isp->frame_id);
  1271. }
  1272. list_for_each_entry(req, &ctx->active_req_list, list) {
  1273. if (req->request_id > ctx_isp->reported_req_id) {
  1274. request_id = req->request_id;
  1275. ctx_isp->reported_req_id = request_id;
  1276. break;
  1277. }
  1278. }
  1279. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  1280. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  1281. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  1282. CAM_ISP_STATE_CHANGE_TRIGGER_SOF, request_id);
  1283. } else {
  1284. CAM_ERR_RATE_LIMIT(CAM_ISP, "Can not notify SOF to CRM");
  1285. rc = -EFAULT;
  1286. }
  1287. end:
  1288. return rc;
  1289. }
  1290. static int __cam_isp_ctx_fs2_buf_done(struct cam_isp_context *ctx_isp,
  1291. void *evt_data)
  1292. {
  1293. int rc = 0;
  1294. struct cam_isp_hw_done_event_data *done =
  1295. (struct cam_isp_hw_done_event_data *) evt_data;
  1296. struct cam_context *ctx = ctx_isp->base;
  1297. int prev_active_req_cnt = 0;
  1298. int curr_req_id = 0;
  1299. struct cam_ctx_request *req;
  1300. prev_active_req_cnt = ctx_isp->active_req_cnt;
  1301. req = list_first_entry(&ctx->active_req_list,
  1302. struct cam_ctx_request, list);
  1303. if (req)
  1304. curr_req_id = req->request_id;
  1305. rc = __cam_isp_ctx_handle_buf_done_in_activated_state(ctx_isp, done, 0);
  1306. if (prev_active_req_cnt == ctx_isp->active_req_cnt + 1) {
  1307. if (list_empty(&ctx->wait_req_list) &&
  1308. list_empty(&ctx->active_req_list)) {
  1309. CAM_DBG(CAM_ISP, "No request, move to SOF");
  1310. ctx_isp->substate_activated =
  1311. CAM_ISP_CTX_ACTIVATED_SOF;
  1312. if (ctx_isp->reported_req_id < curr_req_id) {
  1313. ctx_isp->reported_req_id = curr_req_id;
  1314. __cam_isp_ctx_send_sof_timestamp(ctx_isp,
  1315. curr_req_id,
  1316. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  1317. }
  1318. }
  1319. }
  1320. return rc;
  1321. }
  1322. static int __cam_isp_ctx_fs2_buf_done_in_epoch(struct cam_isp_context *ctx_isp,
  1323. void *evt_data)
  1324. {
  1325. int rc = 0;
  1326. rc = __cam_isp_ctx_fs2_buf_done(ctx_isp, evt_data);
  1327. return rc;
  1328. }
  1329. static int __cam_isp_ctx_fs2_buf_done_in_applied(
  1330. struct cam_isp_context *ctx_isp,
  1331. void *evt_data)
  1332. {
  1333. int rc = 0;
  1334. rc = __cam_isp_ctx_fs2_buf_done(ctx_isp, evt_data);
  1335. return rc;
  1336. }
  1337. static int __cam_isp_ctx_fs2_reg_upd_in_sof(struct cam_isp_context *ctx_isp,
  1338. void *evt_data)
  1339. {
  1340. int rc = 0;
  1341. struct cam_ctx_request *req = NULL;
  1342. struct cam_isp_ctx_req *req_isp;
  1343. struct cam_context *ctx = ctx_isp->base;
  1344. if (ctx->state != CAM_CTX_ACTIVATED && ctx_isp->frame_id > 1) {
  1345. CAM_DBG(CAM_ISP, "invalid RUP");
  1346. goto end;
  1347. }
  1348. /*
  1349. * This is for the first update. The initial setting will
  1350. * cause the reg_upd in the first frame.
  1351. */
  1352. if (!list_empty(&ctx->wait_req_list)) {
  1353. req = list_first_entry(&ctx->wait_req_list,
  1354. struct cam_ctx_request, list);
  1355. list_del_init(&req->list);
  1356. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  1357. if (req_isp->num_fence_map_out == req_isp->num_acked)
  1358. list_add_tail(&req->list, &ctx->free_req_list);
  1359. else
  1360. CAM_ERR(CAM_ISP,
  1361. "receive rup in unexpected state");
  1362. }
  1363. if (req != NULL) {
  1364. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  1365. CAM_ISP_STATE_CHANGE_TRIGGER_REG_UPDATE,
  1366. req->request_id);
  1367. }
  1368. end:
  1369. return rc;
  1370. }
  1371. static int __cam_isp_ctx_fs2_reg_upd_in_applied_state(
  1372. struct cam_isp_context *ctx_isp, void *evt_data)
  1373. {
  1374. int rc = 0;
  1375. struct cam_ctx_request *req = NULL;
  1376. struct cam_context *ctx = ctx_isp->base;
  1377. struct cam_isp_ctx_req *req_isp;
  1378. struct cam_req_mgr_trigger_notify notify;
  1379. uint64_t request_id = 0;
  1380. if (list_empty(&ctx->wait_req_list)) {
  1381. CAM_ERR(CAM_ISP, "Reg upd ack with no waiting request");
  1382. goto end;
  1383. }
  1384. req = list_first_entry(&ctx->wait_req_list,
  1385. struct cam_ctx_request, list);
  1386. list_del_init(&req->list);
  1387. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  1388. if (req_isp->num_fence_map_out != 0) {
  1389. list_add_tail(&req->list, &ctx->active_req_list);
  1390. ctx_isp->active_req_cnt++;
  1391. CAM_DBG(CAM_REQ, "move request %lld to active list(cnt = %d)",
  1392. req->request_id, ctx_isp->active_req_cnt);
  1393. } else {
  1394. /* no io config, so the request is completed. */
  1395. list_add_tail(&req->list, &ctx->free_req_list);
  1396. }
  1397. /*
  1398. * This function only called directly from applied and bubble applied
  1399. * state so change substate here.
  1400. */
  1401. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_EPOCH;
  1402. if (req_isp->num_fence_map_out != 1)
  1403. goto end;
  1404. if (ctx->ctx_crm_intf && ctx->ctx_crm_intf->notify_trigger &&
  1405. ctx_isp->active_req_cnt <= 2) {
  1406. list_for_each_entry(req, &ctx->active_req_list, list) {
  1407. if (req->request_id > ctx_isp->reported_req_id) {
  1408. request_id = req->request_id;
  1409. ctx_isp->reported_req_id = request_id;
  1410. break;
  1411. }
  1412. }
  1413. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  1414. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  1415. if (ctx_isp->subscribe_event & CAM_TRIGGER_POINT_SOF) {
  1416. notify.link_hdl = ctx->link_hdl;
  1417. notify.dev_hdl = ctx->dev_hdl;
  1418. notify.frame_id = ctx_isp->frame_id;
  1419. notify.trigger = CAM_TRIGGER_POINT_SOF;
  1420. ctx->ctx_crm_intf->notify_trigger(&notify);
  1421. CAM_DBG(CAM_ISP, "Notify CRM SOF frame %lld",
  1422. ctx_isp->frame_id);
  1423. }
  1424. } else {
  1425. CAM_ERR_RATE_LIMIT(CAM_ISP, "Can not notify SOF to CRM");
  1426. rc = -EFAULT;
  1427. }
  1428. CAM_DBG(CAM_ISP, "next substate %d", ctx_isp->substate_activated);
  1429. end:
  1430. if (req != NULL && !rc) {
  1431. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  1432. CAM_ISP_STATE_CHANGE_TRIGGER_REG_UPDATE,
  1433. req->request_id);
  1434. }
  1435. return rc;
  1436. }
  1437. static struct cam_isp_ctx_irq_ops
  1438. cam_isp_ctx_activated_state_machine_irq[CAM_ISP_CTX_ACTIVATED_MAX] = {
  1439. /* SOF */
  1440. {
  1441. .irq_ops = {
  1442. __cam_isp_ctx_handle_error,
  1443. __cam_isp_ctx_sof_in_activated_state,
  1444. __cam_isp_ctx_reg_upd_in_sof,
  1445. __cam_isp_ctx_notify_sof_in_activated_state,
  1446. __cam_isp_ctx_notify_eof_in_activated_state,
  1447. NULL,
  1448. },
  1449. },
  1450. /* APPLIED */
  1451. {
  1452. .irq_ops = {
  1453. __cam_isp_ctx_handle_error,
  1454. __cam_isp_ctx_sof_in_activated_state,
  1455. __cam_isp_ctx_reg_upd_in_applied_state,
  1456. __cam_isp_ctx_epoch_in_applied,
  1457. __cam_isp_ctx_notify_eof_in_activated_state,
  1458. __cam_isp_ctx_buf_done_in_applied,
  1459. },
  1460. },
  1461. /* EPOCH */
  1462. {
  1463. .irq_ops = {
  1464. __cam_isp_ctx_handle_error,
  1465. __cam_isp_ctx_sof_in_epoch,
  1466. __cam_isp_ctx_reg_upd_in_epoch_state,
  1467. __cam_isp_ctx_notify_sof_in_activated_state,
  1468. __cam_isp_ctx_notify_eof_in_activated_state,
  1469. __cam_isp_ctx_buf_done_in_epoch,
  1470. },
  1471. },
  1472. /* BUBBLE */
  1473. {
  1474. .irq_ops = {
  1475. __cam_isp_ctx_handle_error,
  1476. __cam_isp_ctx_sof_in_activated_state,
  1477. NULL,
  1478. __cam_isp_ctx_notify_sof_in_activated_state,
  1479. __cam_isp_ctx_notify_eof_in_activated_state,
  1480. __cam_isp_ctx_buf_done_in_bubble,
  1481. },
  1482. },
  1483. /* Bubble Applied */
  1484. {
  1485. .irq_ops = {
  1486. __cam_isp_ctx_handle_error,
  1487. __cam_isp_ctx_sof_in_activated_state,
  1488. __cam_isp_ctx_reg_upd_in_applied_state,
  1489. __cam_isp_ctx_epoch_in_bubble_applied,
  1490. NULL,
  1491. __cam_isp_ctx_buf_done_in_bubble_applied,
  1492. },
  1493. },
  1494. /* HW ERROR */
  1495. {
  1496. .irq_ops = {
  1497. NULL,
  1498. __cam_isp_ctx_sof_in_activated_state,
  1499. __cam_isp_ctx_reg_upd_in_hw_error,
  1500. NULL,
  1501. NULL,
  1502. NULL,
  1503. },
  1504. },
  1505. /* HALT */
  1506. {
  1507. },
  1508. };
  1509. static struct cam_isp_ctx_irq_ops
  1510. cam_isp_ctx_fs2_state_machine_irq[CAM_ISP_CTX_ACTIVATED_MAX] = {
  1511. /* SOF */
  1512. {
  1513. .irq_ops = {
  1514. __cam_isp_ctx_handle_error,
  1515. __cam_isp_ctx_fs2_sof_in_sof_state,
  1516. __cam_isp_ctx_fs2_reg_upd_in_sof,
  1517. __cam_isp_ctx_fs2_sof_in_sof_state,
  1518. __cam_isp_ctx_notify_eof_in_activated_state,
  1519. NULL,
  1520. },
  1521. },
  1522. /* APPLIED */
  1523. {
  1524. .irq_ops = {
  1525. __cam_isp_ctx_handle_error,
  1526. __cam_isp_ctx_sof_in_activated_state,
  1527. __cam_isp_ctx_fs2_reg_upd_in_applied_state,
  1528. __cam_isp_ctx_epoch_in_applied,
  1529. __cam_isp_ctx_notify_eof_in_activated_state,
  1530. __cam_isp_ctx_fs2_buf_done_in_applied,
  1531. },
  1532. },
  1533. /* EPOCH */
  1534. {
  1535. .irq_ops = {
  1536. __cam_isp_ctx_handle_error,
  1537. __cam_isp_ctx_sof_in_epoch,
  1538. __cam_isp_ctx_reg_upd_in_epoch_state,
  1539. __cam_isp_ctx_notify_sof_in_activated_state,
  1540. __cam_isp_ctx_notify_eof_in_activated_state,
  1541. __cam_isp_ctx_fs2_buf_done_in_epoch,
  1542. },
  1543. },
  1544. /* BUBBLE */
  1545. {
  1546. .irq_ops = {
  1547. __cam_isp_ctx_handle_error,
  1548. __cam_isp_ctx_sof_in_activated_state,
  1549. NULL,
  1550. __cam_isp_ctx_notify_sof_in_activated_state,
  1551. __cam_isp_ctx_notify_eof_in_activated_state,
  1552. __cam_isp_ctx_buf_done_in_bubble,
  1553. },
  1554. },
  1555. /* Bubble Applied */
  1556. {
  1557. .irq_ops = {
  1558. __cam_isp_ctx_handle_error,
  1559. __cam_isp_ctx_sof_in_activated_state,
  1560. __cam_isp_ctx_reg_upd_in_applied_state,
  1561. __cam_isp_ctx_epoch_in_bubble_applied,
  1562. NULL,
  1563. __cam_isp_ctx_buf_done_in_bubble_applied,
  1564. },
  1565. },
  1566. /* HW ERROR */
  1567. {
  1568. .irq_ops = {
  1569. NULL,
  1570. __cam_isp_ctx_sof_in_activated_state,
  1571. __cam_isp_ctx_reg_upd_in_hw_error,
  1572. NULL,
  1573. NULL,
  1574. NULL,
  1575. },
  1576. },
  1577. /* HALT */
  1578. {
  1579. },
  1580. };
  1581. static int __cam_isp_ctx_apply_req_in_activated_state(
  1582. struct cam_context *ctx, struct cam_req_mgr_apply_request *apply,
  1583. uint32_t next_state)
  1584. {
  1585. int rc = 0;
  1586. struct cam_ctx_request *req;
  1587. struct cam_ctx_request *active_req = NULL;
  1588. struct cam_isp_ctx_req *req_isp;
  1589. struct cam_isp_ctx_req *active_req_isp;
  1590. struct cam_isp_context *ctx_isp = NULL;
  1591. struct cam_hw_config_args cfg;
  1592. if (list_empty(&ctx->pending_req_list)) {
  1593. CAM_ERR(CAM_ISP, "No available request for Apply id %lld",
  1594. apply->request_id);
  1595. rc = -EFAULT;
  1596. goto end;
  1597. }
  1598. /*
  1599. * When the pipeline has issue, the requests can be queued up in the
  1600. * pipeline. In this case, we should reject the additional request.
  1601. * The maximum number of request allowed to be outstanding is 2.
  1602. *
  1603. */
  1604. ctx_isp = (struct cam_isp_context *) ctx->ctx_priv;
  1605. if (atomic_read(&ctx_isp->process_bubble)) {
  1606. CAM_INFO(CAM_ISP,
  1607. "Processing bubble cannot apply Request Id %llu",
  1608. apply->request_id);
  1609. rc = -EAGAIN;
  1610. goto end;
  1611. }
  1612. spin_lock_bh(&ctx->lock);
  1613. req = list_first_entry(&ctx->pending_req_list, struct cam_ctx_request,
  1614. list);
  1615. spin_unlock_bh(&ctx->lock);
  1616. /*
  1617. * Check whether the request id is matching the tip, if not, this means
  1618. * we are in the middle of the error handling. Need to reject this apply
  1619. */
  1620. if (req->request_id != apply->request_id) {
  1621. CAM_ERR_RATE_LIMIT(CAM_ISP,
  1622. "Invalid Request Id asking %llu existing %llu",
  1623. apply->request_id, req->request_id);
  1624. rc = -EFAULT;
  1625. goto end;
  1626. }
  1627. CAM_DBG(CAM_REQ, "Apply request %lld in substate %d ctx %u",
  1628. req->request_id, ctx_isp->substate_activated, ctx->ctx_id);
  1629. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  1630. if (ctx_isp->active_req_cnt >= 2) {
  1631. CAM_WARN(CAM_ISP,
  1632. "Reject apply request (id %lld) due to congestion(cnt = %d) ctx %u",
  1633. req->request_id,
  1634. ctx_isp->active_req_cnt,
  1635. ctx->ctx_id);
  1636. spin_lock_bh(&ctx->lock);
  1637. if (!list_empty(&ctx->active_req_list))
  1638. active_req = list_first_entry(&ctx->active_req_list,
  1639. struct cam_ctx_request, list);
  1640. else
  1641. CAM_ERR_RATE_LIMIT(CAM_ISP,
  1642. "WARNING: should not happen (cnt = %d) but active_list empty",
  1643. ctx_isp->active_req_cnt);
  1644. spin_unlock_bh(&ctx->lock);
  1645. if (active_req) {
  1646. active_req_isp =
  1647. (struct cam_isp_ctx_req *) active_req->req_priv;
  1648. __cam_isp_ctx_handle_buf_done_fail_log(
  1649. active_req->request_id, active_req_isp);
  1650. }
  1651. rc = -EFAULT;
  1652. goto end;
  1653. }
  1654. req_isp->bubble_report = apply->report_if_bubble;
  1655. cfg.ctxt_to_hw_map = ctx_isp->hw_ctx;
  1656. cfg.request_id = req->request_id;
  1657. cfg.hw_update_entries = req_isp->cfg;
  1658. cfg.num_hw_update_entries = req_isp->num_cfg;
  1659. cfg.priv = &req_isp->hw_update_data;
  1660. cfg.init_packet = 0;
  1661. rc = ctx->hw_mgr_intf->hw_config(ctx->hw_mgr_intf->hw_mgr_priv, &cfg);
  1662. if (rc) {
  1663. CAM_ERR_RATE_LIMIT(CAM_ISP, "Can not apply the configuration");
  1664. } else {
  1665. spin_lock_bh(&ctx->lock);
  1666. ctx_isp->substate_activated = next_state;
  1667. ctx_isp->last_applied_req_id = apply->request_id;
  1668. list_del_init(&req->list);
  1669. list_add_tail(&req->list, &ctx->wait_req_list);
  1670. CAM_DBG(CAM_ISP, "new substate state %d, applied req %lld",
  1671. next_state, ctx_isp->last_applied_req_id);
  1672. spin_unlock_bh(&ctx->lock);
  1673. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  1674. CAM_ISP_STATE_CHANGE_TRIGGER_APPLIED,
  1675. req->request_id);
  1676. }
  1677. end:
  1678. return rc;
  1679. }
  1680. static int __cam_isp_ctx_apply_req_in_sof(
  1681. struct cam_context *ctx, struct cam_req_mgr_apply_request *apply)
  1682. {
  1683. int rc = 0;
  1684. struct cam_isp_context *ctx_isp =
  1685. (struct cam_isp_context *) ctx->ctx_priv;
  1686. CAM_DBG(CAM_ISP, "current substate %d",
  1687. ctx_isp->substate_activated);
  1688. rc = __cam_isp_ctx_apply_req_in_activated_state(ctx, apply,
  1689. CAM_ISP_CTX_ACTIVATED_APPLIED);
  1690. CAM_DBG(CAM_ISP, "new substate %d", ctx_isp->substate_activated);
  1691. if (rc)
  1692. CAM_DBG(CAM_ISP, "Apply failed in state %d, rc %d",
  1693. ctx_isp->substate_activated, rc);
  1694. return rc;
  1695. }
  1696. static int __cam_isp_ctx_apply_req_in_epoch(
  1697. struct cam_context *ctx, struct cam_req_mgr_apply_request *apply)
  1698. {
  1699. int rc = 0;
  1700. struct cam_isp_context *ctx_isp =
  1701. (struct cam_isp_context *) ctx->ctx_priv;
  1702. CAM_DBG(CAM_ISP, "current substate %d",
  1703. ctx_isp->substate_activated);
  1704. rc = __cam_isp_ctx_apply_req_in_activated_state(ctx, apply,
  1705. CAM_ISP_CTX_ACTIVATED_APPLIED);
  1706. CAM_DBG(CAM_ISP, "new substate %d", ctx_isp->substate_activated);
  1707. if (rc)
  1708. CAM_DBG(CAM_ISP, "Apply failed in state %d, rc %d",
  1709. ctx_isp->substate_activated, rc);
  1710. return rc;
  1711. }
  1712. static int __cam_isp_ctx_apply_req_in_bubble(
  1713. struct cam_context *ctx, struct cam_req_mgr_apply_request *apply)
  1714. {
  1715. int rc = 0;
  1716. struct cam_isp_context *ctx_isp =
  1717. (struct cam_isp_context *) ctx->ctx_priv;
  1718. CAM_DBG(CAM_ISP, "current substate %d",
  1719. ctx_isp->substate_activated);
  1720. rc = __cam_isp_ctx_apply_req_in_activated_state(ctx, apply,
  1721. CAM_ISP_CTX_ACTIVATED_BUBBLE_APPLIED);
  1722. CAM_DBG(CAM_ISP, "new substate %d", ctx_isp->substate_activated);
  1723. if (rc)
  1724. CAM_DBG(CAM_ISP, "Apply failed in state %d, rc %d",
  1725. ctx_isp->substate_activated, rc);
  1726. return rc;
  1727. }
  1728. static int __cam_isp_ctx_flush_req(struct cam_context *ctx,
  1729. struct list_head *req_list, struct cam_req_mgr_flush_request *flush_req)
  1730. {
  1731. int i, rc, tmp = 0;
  1732. uint32_t cancel_req_id_found = 0;
  1733. struct cam_ctx_request *req;
  1734. struct cam_ctx_request *req_temp;
  1735. struct cam_isp_ctx_req *req_isp;
  1736. struct list_head flush_list;
  1737. struct cam_isp_context *ctx_isp = NULL;
  1738. ctx_isp = (struct cam_isp_context *) ctx->ctx_priv;
  1739. INIT_LIST_HEAD(&flush_list);
  1740. if (list_empty(req_list)) {
  1741. CAM_DBG(CAM_ISP, "request list is empty");
  1742. if (flush_req->type == CAM_REQ_MGR_FLUSH_TYPE_CANCEL_REQ) {
  1743. CAM_ERR(CAM_ISP, "no request to cancel");
  1744. return -EINVAL;
  1745. } else
  1746. return 0;
  1747. }
  1748. CAM_DBG(CAM_REQ, "Flush [%u] in progress for req_id %llu",
  1749. flush_req->type, flush_req->req_id);
  1750. list_for_each_entry_safe(req, req_temp, req_list, list) {
  1751. if (flush_req->type == CAM_REQ_MGR_FLUSH_TYPE_CANCEL_REQ) {
  1752. if (req->request_id != flush_req->req_id) {
  1753. continue;
  1754. } else {
  1755. list_del_init(&req->list);
  1756. list_add_tail(&req->list, &flush_list);
  1757. cancel_req_id_found = 1;
  1758. __cam_isp_ctx_update_state_monitor_array(
  1759. ctx_isp,
  1760. CAM_ISP_STATE_CHANGE_TRIGGER_FLUSH,
  1761. req->request_id);
  1762. break;
  1763. }
  1764. }
  1765. list_del_init(&req->list);
  1766. list_add_tail(&req->list, &flush_list);
  1767. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  1768. CAM_ISP_STATE_CHANGE_TRIGGER_FLUSH, req->request_id);
  1769. }
  1770. list_for_each_entry_safe(req, req_temp, &flush_list, list) {
  1771. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  1772. for (i = 0; i < req_isp->num_fence_map_out; i++) {
  1773. if (req_isp->fence_map_out[i].sync_id != -1) {
  1774. CAM_DBG(CAM_ISP, "Flush req 0x%llx, fence %d",
  1775. req->request_id,
  1776. req_isp->fence_map_out[i].sync_id);
  1777. rc = cam_sync_signal(
  1778. req_isp->fence_map_out[i].sync_id,
  1779. CAM_SYNC_STATE_SIGNALED_ERROR);
  1780. if (rc) {
  1781. tmp = req_isp->fence_map_out[i].sync_id;
  1782. CAM_ERR_RATE_LIMIT(CAM_ISP,
  1783. "signal fence %d failed", tmp);
  1784. }
  1785. req_isp->fence_map_out[i].sync_id = -1;
  1786. }
  1787. }
  1788. list_add_tail(&req->list, &ctx->free_req_list);
  1789. }
  1790. if (flush_req->type == CAM_REQ_MGR_FLUSH_TYPE_CANCEL_REQ &&
  1791. !cancel_req_id_found)
  1792. CAM_DBG(CAM_ISP,
  1793. "Flush request id:%lld is not found in the list",
  1794. flush_req->req_id);
  1795. return 0;
  1796. }
  1797. static int __cam_isp_ctx_flush_req_in_top_state(
  1798. struct cam_context *ctx,
  1799. struct cam_req_mgr_flush_request *flush_req)
  1800. {
  1801. int rc = 0;
  1802. struct cam_isp_context *ctx_isp;
  1803. struct cam_hw_cmd_args hw_cmd_args;
  1804. ctx_isp = (struct cam_isp_context *) ctx->ctx_priv;
  1805. if (flush_req->type == CAM_REQ_MGR_FLUSH_TYPE_ALL) {
  1806. CAM_INFO(CAM_ISP, "Last request id to flush is %lld",
  1807. flush_req->req_id);
  1808. ctx->last_flush_req = flush_req->req_id;
  1809. }
  1810. CAM_DBG(CAM_ISP, "try to flush pending list");
  1811. spin_lock_bh(&ctx->lock);
  1812. rc = __cam_isp_ctx_flush_req(ctx, &ctx->pending_req_list, flush_req);
  1813. spin_unlock_bh(&ctx->lock);
  1814. memset(&hw_cmd_args, 0, sizeof(hw_cmd_args));
  1815. hw_cmd_args.ctxt_to_hw_map = ctx->ctxt_to_hw_map;
  1816. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_REG_DUMP_ON_FLUSH;
  1817. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  1818. &hw_cmd_args);
  1819. if (rc) {
  1820. CAM_ERR(CAM_ISP, "Reg dump on flush failed rc: %d", rc);
  1821. rc = 0;
  1822. }
  1823. atomic_set(&ctx_isp->process_bubble, 0);
  1824. return rc;
  1825. }
  1826. static int __cam_isp_ctx_flush_req_in_ready(
  1827. struct cam_context *ctx,
  1828. struct cam_req_mgr_flush_request *flush_req)
  1829. {
  1830. int rc = 0;
  1831. CAM_DBG(CAM_ISP, "try to flush pending list");
  1832. spin_lock_bh(&ctx->lock);
  1833. rc = __cam_isp_ctx_flush_req(ctx, &ctx->pending_req_list, flush_req);
  1834. /* if nothing is in pending req list, change state to acquire */
  1835. if (list_empty(&ctx->pending_req_list))
  1836. ctx->state = CAM_CTX_ACQUIRED;
  1837. spin_unlock_bh(&ctx->lock);
  1838. trace_cam_context_state("ISP", ctx);
  1839. CAM_DBG(CAM_ISP, "Flush request in ready state. next state %d",
  1840. ctx->state);
  1841. return rc;
  1842. }
  1843. static struct cam_ctx_ops
  1844. cam_isp_ctx_activated_state_machine[CAM_ISP_CTX_ACTIVATED_MAX] = {
  1845. /* SOF */
  1846. {
  1847. .ioctl_ops = {},
  1848. .crm_ops = {
  1849. .apply_req = __cam_isp_ctx_apply_req_in_sof,
  1850. },
  1851. .irq_ops = NULL,
  1852. },
  1853. /* APPLIED */
  1854. {
  1855. .ioctl_ops = {},
  1856. .crm_ops = {},
  1857. .irq_ops = NULL,
  1858. },
  1859. /* EPOCH */
  1860. {
  1861. .ioctl_ops = {},
  1862. .crm_ops = {
  1863. .apply_req = __cam_isp_ctx_apply_req_in_epoch,
  1864. },
  1865. .irq_ops = NULL,
  1866. },
  1867. /* BUBBLE */
  1868. {
  1869. .ioctl_ops = {},
  1870. .crm_ops = {
  1871. .apply_req = __cam_isp_ctx_apply_req_in_bubble,
  1872. },
  1873. .irq_ops = NULL,
  1874. },
  1875. /* Bubble Applied */
  1876. {
  1877. .ioctl_ops = {},
  1878. .crm_ops = {},
  1879. .irq_ops = NULL,
  1880. },
  1881. /* HW ERROR */
  1882. {
  1883. .ioctl_ops = {},
  1884. .crm_ops = {},
  1885. .irq_ops = NULL,
  1886. },
  1887. /* HALT */
  1888. {
  1889. .ioctl_ops = {},
  1890. .crm_ops = {},
  1891. .irq_ops = NULL,
  1892. },
  1893. };
  1894. static struct cam_ctx_ops
  1895. cam_isp_ctx_fs2_state_machine[CAM_ISP_CTX_ACTIVATED_MAX] = {
  1896. /* SOF */
  1897. {
  1898. .ioctl_ops = {},
  1899. .crm_ops = {
  1900. .apply_req = __cam_isp_ctx_apply_req_in_sof,
  1901. },
  1902. .irq_ops = NULL,
  1903. },
  1904. /* APPLIED */
  1905. {
  1906. .ioctl_ops = {},
  1907. .crm_ops = {},
  1908. .irq_ops = NULL,
  1909. },
  1910. /* EPOCH */
  1911. {
  1912. .ioctl_ops = {},
  1913. .crm_ops = {
  1914. .apply_req = __cam_isp_ctx_apply_req_in_epoch,
  1915. },
  1916. .irq_ops = NULL,
  1917. },
  1918. /* BUBBLE */
  1919. {
  1920. .ioctl_ops = {},
  1921. .crm_ops = {
  1922. .apply_req = __cam_isp_ctx_apply_req_in_bubble,
  1923. },
  1924. .irq_ops = NULL,
  1925. },
  1926. /* Bubble Applied */
  1927. {
  1928. .ioctl_ops = {},
  1929. .crm_ops = {},
  1930. .irq_ops = NULL,
  1931. },
  1932. /* HW ERROR */
  1933. {
  1934. .ioctl_ops = {},
  1935. .crm_ops = {},
  1936. .irq_ops = NULL,
  1937. },
  1938. /* HALT */
  1939. {
  1940. .ioctl_ops = {},
  1941. .crm_ops = {},
  1942. .irq_ops = NULL,
  1943. },
  1944. };
  1945. static int __cam_isp_ctx_rdi_only_sof_in_top_state(
  1946. struct cam_isp_context *ctx_isp, void *evt_data)
  1947. {
  1948. int rc = 0;
  1949. struct cam_context *ctx = ctx_isp->base;
  1950. struct cam_req_mgr_trigger_notify notify;
  1951. struct cam_isp_hw_sof_event_data *sof_event_data = evt_data;
  1952. uint64_t request_id = 0;
  1953. if (!evt_data) {
  1954. CAM_ERR(CAM_ISP, "in valid sof event data");
  1955. return -EINVAL;
  1956. }
  1957. ctx_isp->frame_id++;
  1958. ctx_isp->sof_timestamp_val = sof_event_data->timestamp;
  1959. ctx_isp->boot_timestamp = sof_event_data->boot_time;
  1960. CAM_DBG(CAM_ISP, "frame id: %lld time stamp:0x%llx",
  1961. ctx_isp->frame_id, ctx_isp->sof_timestamp_val);
  1962. /*
  1963. * notify reqmgr with sof signal. Note, due to scheduling delay
  1964. * we can run into situation that two active requests has already
  1965. * be in the active queue while we try to do the notification.
  1966. * In this case, we need to skip the current notification. This
  1967. * helps the state machine to catch up the delay.
  1968. */
  1969. if (ctx->ctx_crm_intf && ctx->ctx_crm_intf->notify_trigger &&
  1970. ctx_isp->active_req_cnt <= 2) {
  1971. notify.link_hdl = ctx->link_hdl;
  1972. notify.dev_hdl = ctx->dev_hdl;
  1973. notify.frame_id = ctx_isp->frame_id;
  1974. notify.trigger = CAM_TRIGGER_POINT_SOF;
  1975. ctx->ctx_crm_intf->notify_trigger(&notify);
  1976. CAM_DBG(CAM_ISP, "Notify CRM SOF frame %lld",
  1977. ctx_isp->frame_id);
  1978. /*
  1979. * It is idle frame with out any applied request id, send
  1980. * request id as zero
  1981. */
  1982. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  1983. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  1984. } else {
  1985. CAM_ERR_RATE_LIMIT(CAM_ISP, "Can not notify SOF to CRM");
  1986. }
  1987. if (list_empty(&ctx->active_req_list))
  1988. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_SOF;
  1989. else
  1990. CAM_DBG(CAM_ISP, "Still need to wait for the buf done");
  1991. CAM_DBG(CAM_ISP, "next substate %d",
  1992. ctx_isp->substate_activated);
  1993. return rc;
  1994. }
  1995. static int __cam_isp_ctx_rdi_only_sof_in_applied_state(
  1996. struct cam_isp_context *ctx_isp, void *evt_data)
  1997. {
  1998. struct cam_isp_hw_sof_event_data *sof_event_data = evt_data;
  1999. if (!evt_data) {
  2000. CAM_ERR(CAM_ISP, "in valid sof event data");
  2001. return -EINVAL;
  2002. }
  2003. ctx_isp->frame_id++;
  2004. ctx_isp->sof_timestamp_val = sof_event_data->timestamp;
  2005. ctx_isp->boot_timestamp = sof_event_data->boot_time;
  2006. CAM_DBG(CAM_ISP, "frame id: %lld time stamp:0x%llx",
  2007. ctx_isp->frame_id, ctx_isp->sof_timestamp_val);
  2008. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_BUBBLE_APPLIED;
  2009. CAM_DBG(CAM_ISP, "next substate %d", ctx_isp->substate_activated);
  2010. return 0;
  2011. }
  2012. static int __cam_isp_ctx_rdi_only_sof_in_bubble_applied(
  2013. struct cam_isp_context *ctx_isp, void *evt_data)
  2014. {
  2015. struct cam_ctx_request *req;
  2016. struct cam_isp_ctx_req *req_isp;
  2017. struct cam_context *ctx = ctx_isp->base;
  2018. struct cam_isp_hw_sof_event_data *sof_event_data = evt_data;
  2019. uint64_t request_id = 0;
  2020. /*
  2021. * Sof in bubble applied state means, reg update not received.
  2022. * before increment frame id and override time stamp value, send
  2023. * the previous sof time stamp that got captured in the
  2024. * sof in applied state.
  2025. */
  2026. CAM_DBG(CAM_ISP, "frame id: %lld time stamp:0x%llx",
  2027. ctx_isp->frame_id, ctx_isp->sof_timestamp_val);
  2028. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  2029. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  2030. ctx_isp->frame_id++;
  2031. ctx_isp->sof_timestamp_val = sof_event_data->timestamp;
  2032. ctx_isp->boot_timestamp = sof_event_data->boot_time;
  2033. CAM_DBG(CAM_ISP, "frame id: %lld time stamp:0x%llx",
  2034. ctx_isp->frame_id, ctx_isp->sof_timestamp_val);
  2035. if (list_empty(&ctx->wait_req_list)) {
  2036. /*
  2037. * If no pending req in epoch, this is an error case.
  2038. * The recovery is to go back to sof state
  2039. */
  2040. CAM_ERR(CAM_ISP, "No wait request");
  2041. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_SOF;
  2042. /* Send SOF event as empty frame*/
  2043. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  2044. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  2045. goto end;
  2046. }
  2047. req = list_first_entry(&ctx->wait_req_list, struct cam_ctx_request,
  2048. list);
  2049. req_isp = (struct cam_isp_ctx_req *)req->req_priv;
  2050. req_isp->bubble_detected = true;
  2051. CAM_DBG(CAM_ISP, "Report Bubble flag %d", req_isp->bubble_report);
  2052. if (req_isp->bubble_report && ctx->ctx_crm_intf &&
  2053. ctx->ctx_crm_intf->notify_err) {
  2054. struct cam_req_mgr_error_notify notify;
  2055. notify.link_hdl = ctx->link_hdl;
  2056. notify.dev_hdl = ctx->dev_hdl;
  2057. notify.req_id = req->request_id;
  2058. notify.error = CRM_KMD_ERR_BUBBLE;
  2059. CAM_WARN(CAM_ISP,
  2060. "Notify CRM about Bubble req %lld frame %lld ctx %u",
  2061. req->request_id,
  2062. ctx_isp->frame_id,
  2063. ctx->ctx_id);
  2064. ctx->ctx_crm_intf->notify_err(&notify);
  2065. } else {
  2066. req_isp->bubble_report = 0;
  2067. }
  2068. /*
  2069. * Always move the request to active list. Let buf done
  2070. * function handles the rest.
  2071. */
  2072. list_del_init(&req->list);
  2073. list_add_tail(&req->list, &ctx->active_req_list);
  2074. ctx_isp->active_req_cnt++;
  2075. CAM_DBG(CAM_ISP, "move request %lld to active list(cnt = %d)",
  2076. req->request_id, ctx_isp->active_req_cnt);
  2077. if (!req_isp->bubble_report) {
  2078. if (req->request_id > ctx_isp->reported_req_id) {
  2079. request_id = req->request_id;
  2080. ctx_isp->reported_req_id = request_id;
  2081. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  2082. CAM_REQ_MGR_SOF_EVENT_ERROR);
  2083. } else
  2084. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  2085. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  2086. } else
  2087. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  2088. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  2089. /* change the state to bubble, as reg update has not come */
  2090. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_BUBBLE;
  2091. CAM_DBG(CAM_ISP, "next substate %d", ctx_isp->substate_activated);
  2092. end:
  2093. return 0;
  2094. }
  2095. static int __cam_isp_ctx_rdi_only_sof_in_bubble_state(
  2096. struct cam_isp_context *ctx_isp, void *evt_data)
  2097. {
  2098. uint32_t i;
  2099. struct cam_ctx_request *req;
  2100. struct cam_context *ctx = ctx_isp->base;
  2101. struct cam_req_mgr_trigger_notify notify;
  2102. struct cam_isp_hw_sof_event_data *sof_event_data = evt_data;
  2103. struct cam_isp_ctx_req *req_isp;
  2104. uint64_t request_id = 0;
  2105. if (!evt_data) {
  2106. CAM_ERR(CAM_ISP, "in valid sof event data");
  2107. return -EINVAL;
  2108. }
  2109. ctx_isp->frame_id++;
  2110. ctx_isp->sof_timestamp_val = sof_event_data->timestamp;
  2111. ctx_isp->boot_timestamp = sof_event_data->boot_time;
  2112. CAM_DBG(CAM_ISP, "frame id: %lld time stamp:0x%llx",
  2113. ctx_isp->frame_id, ctx_isp->sof_timestamp_val);
  2114. /*
  2115. * Signal all active requests with error and move the all the active
  2116. * requests to free list
  2117. */
  2118. while (!list_empty(&ctx->active_req_list)) {
  2119. req = list_first_entry(&ctx->active_req_list,
  2120. struct cam_ctx_request, list);
  2121. list_del_init(&req->list);
  2122. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  2123. CAM_DBG(CAM_ISP, "signal fence in active list. fence num %d",
  2124. req_isp->num_fence_map_out);
  2125. for (i = 0; i < req_isp->num_fence_map_out; i++)
  2126. if (req_isp->fence_map_out[i].sync_id != -1) {
  2127. cam_sync_signal(
  2128. req_isp->fence_map_out[i].sync_id,
  2129. CAM_SYNC_STATE_SIGNALED_ERROR);
  2130. }
  2131. list_add_tail(&req->list, &ctx->free_req_list);
  2132. ctx_isp->active_req_cnt--;
  2133. }
  2134. /* notify reqmgr with sof signal */
  2135. if (ctx->ctx_crm_intf && ctx->ctx_crm_intf->notify_trigger) {
  2136. notify.link_hdl = ctx->link_hdl;
  2137. notify.dev_hdl = ctx->dev_hdl;
  2138. notify.frame_id = ctx_isp->frame_id;
  2139. notify.trigger = CAM_TRIGGER_POINT_SOF;
  2140. ctx->ctx_crm_intf->notify_trigger(&notify);
  2141. CAM_DBG(CAM_ISP, "Notify CRM SOF frame %lld",
  2142. ctx_isp->frame_id);
  2143. } else {
  2144. CAM_ERR(CAM_ISP, "Can not notify SOF to CRM");
  2145. }
  2146. /*
  2147. * It is idle frame with out any applied request id, send
  2148. * request id as zero
  2149. */
  2150. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  2151. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  2152. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_SOF;
  2153. CAM_DBG(CAM_ISP, "next substate %d",
  2154. ctx_isp->substate_activated);
  2155. return 0;
  2156. }
  2157. static int __cam_isp_ctx_rdi_only_reg_upd_in_bubble_applied_state(
  2158. struct cam_isp_context *ctx_isp, void *evt_data)
  2159. {
  2160. struct cam_ctx_request *req;
  2161. struct cam_context *ctx = ctx_isp->base;
  2162. struct cam_isp_ctx_req *req_isp;
  2163. struct cam_req_mgr_trigger_notify notify;
  2164. uint64_t request_id = 0;
  2165. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_EPOCH;
  2166. /* notify reqmgr with sof signal*/
  2167. if (ctx->ctx_crm_intf && ctx->ctx_crm_intf->notify_trigger) {
  2168. if (list_empty(&ctx->wait_req_list)) {
  2169. CAM_ERR(CAM_ISP, "Reg upd ack with no waiting request");
  2170. goto error;
  2171. }
  2172. req = list_first_entry(&ctx->wait_req_list,
  2173. struct cam_ctx_request, list);
  2174. list_del_init(&req->list);
  2175. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  2176. request_id =
  2177. (req_isp->hw_update_data.packet_opcode_type ==
  2178. CAM_ISP_PACKET_INIT_DEV) ?
  2179. 0 : req->request_id;
  2180. if (req_isp->num_fence_map_out != 0) {
  2181. list_add_tail(&req->list, &ctx->active_req_list);
  2182. ctx_isp->active_req_cnt++;
  2183. CAM_DBG(CAM_ISP,
  2184. "move request %lld to active list(cnt = %d)",
  2185. req->request_id, ctx_isp->active_req_cnt);
  2186. /* if packet has buffers, set correct request id */
  2187. request_id = req->request_id;
  2188. } else {
  2189. /* no io config, so the request is completed. */
  2190. list_add_tail(&req->list, &ctx->free_req_list);
  2191. CAM_DBG(CAM_ISP,
  2192. "move active req %lld to free list(cnt=%d)",
  2193. req->request_id, ctx_isp->active_req_cnt);
  2194. }
  2195. notify.link_hdl = ctx->link_hdl;
  2196. notify.dev_hdl = ctx->dev_hdl;
  2197. notify.frame_id = ctx_isp->frame_id;
  2198. notify.trigger = CAM_TRIGGER_POINT_SOF;
  2199. ctx->ctx_crm_intf->notify_trigger(&notify);
  2200. CAM_DBG(CAM_ISP, "Notify CRM SOF frame %lld",
  2201. ctx_isp->frame_id);
  2202. } else {
  2203. CAM_ERR(CAM_ISP, "Can not notify SOF to CRM");
  2204. }
  2205. if (request_id)
  2206. ctx_isp->reported_req_id = request_id;
  2207. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  2208. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  2209. CAM_DBG(CAM_ISP, "next substate %d", ctx_isp->substate_activated);
  2210. return 0;
  2211. error:
  2212. /* Send SOF event as idle frame*/
  2213. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  2214. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  2215. /*
  2216. * There is no request in the pending list, move the sub state machine
  2217. * to SOF sub state
  2218. */
  2219. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_SOF;
  2220. return 0;
  2221. }
  2222. static struct cam_isp_ctx_irq_ops
  2223. cam_isp_ctx_rdi_only_activated_state_machine_irq
  2224. [CAM_ISP_CTX_ACTIVATED_MAX] = {
  2225. /* SOF */
  2226. {
  2227. .irq_ops = {
  2228. NULL,
  2229. __cam_isp_ctx_rdi_only_sof_in_top_state,
  2230. __cam_isp_ctx_reg_upd_in_sof,
  2231. NULL,
  2232. NULL,
  2233. NULL,
  2234. },
  2235. },
  2236. /* APPLIED */
  2237. {
  2238. .irq_ops = {
  2239. __cam_isp_ctx_handle_error,
  2240. __cam_isp_ctx_rdi_only_sof_in_applied_state,
  2241. NULL,
  2242. NULL,
  2243. NULL,
  2244. __cam_isp_ctx_buf_done_in_applied,
  2245. },
  2246. },
  2247. /* EPOCH */
  2248. {
  2249. .irq_ops = {
  2250. __cam_isp_ctx_handle_error,
  2251. __cam_isp_ctx_rdi_only_sof_in_top_state,
  2252. NULL,
  2253. NULL,
  2254. NULL,
  2255. __cam_isp_ctx_buf_done_in_epoch,
  2256. },
  2257. },
  2258. /* BUBBLE*/
  2259. {
  2260. .irq_ops = {
  2261. __cam_isp_ctx_handle_error,
  2262. __cam_isp_ctx_rdi_only_sof_in_bubble_state,
  2263. NULL,
  2264. NULL,
  2265. NULL,
  2266. __cam_isp_ctx_buf_done_in_bubble,
  2267. },
  2268. },
  2269. /* BUBBLE APPLIED ie PRE_BUBBLE */
  2270. {
  2271. .irq_ops = {
  2272. __cam_isp_ctx_handle_error,
  2273. __cam_isp_ctx_rdi_only_sof_in_bubble_applied,
  2274. __cam_isp_ctx_rdi_only_reg_upd_in_bubble_applied_state,
  2275. NULL,
  2276. NULL,
  2277. __cam_isp_ctx_buf_done_in_bubble_applied,
  2278. },
  2279. },
  2280. /* HW ERROR */
  2281. {
  2282. },
  2283. /* HALT */
  2284. {
  2285. },
  2286. };
  2287. static int __cam_isp_ctx_rdi_only_apply_req_top_state(
  2288. struct cam_context *ctx, struct cam_req_mgr_apply_request *apply)
  2289. {
  2290. int rc = 0;
  2291. struct cam_isp_context *ctx_isp =
  2292. (struct cam_isp_context *) ctx->ctx_priv;
  2293. CAM_DBG(CAM_ISP, "current substate %d",
  2294. ctx_isp->substate_activated);
  2295. rc = __cam_isp_ctx_apply_req_in_activated_state(ctx, apply,
  2296. CAM_ISP_CTX_ACTIVATED_APPLIED);
  2297. CAM_DBG(CAM_ISP, "new substate %d", ctx_isp->substate_activated);
  2298. if (rc)
  2299. CAM_ERR(CAM_ISP, "Apply failed in state %d, rc %d",
  2300. ctx_isp->substate_activated, rc);
  2301. return rc;
  2302. }
  2303. static struct cam_ctx_ops
  2304. cam_isp_ctx_rdi_only_activated_state_machine
  2305. [CAM_ISP_CTX_ACTIVATED_MAX] = {
  2306. /* SOF */
  2307. {
  2308. .ioctl_ops = {},
  2309. .crm_ops = {
  2310. .apply_req = __cam_isp_ctx_rdi_only_apply_req_top_state,
  2311. },
  2312. .irq_ops = NULL,
  2313. },
  2314. /* APPLIED */
  2315. {
  2316. .ioctl_ops = {},
  2317. .crm_ops = {},
  2318. .irq_ops = NULL,
  2319. },
  2320. /* EPOCH */
  2321. {
  2322. .ioctl_ops = {},
  2323. .crm_ops = {
  2324. .apply_req = __cam_isp_ctx_rdi_only_apply_req_top_state,
  2325. },
  2326. .irq_ops = NULL,
  2327. },
  2328. /* PRE BUBBLE */
  2329. {
  2330. .ioctl_ops = {},
  2331. .crm_ops = {},
  2332. .irq_ops = NULL,
  2333. },
  2334. /* BUBBLE */
  2335. {
  2336. .ioctl_ops = {},
  2337. .crm_ops = {},
  2338. .irq_ops = NULL,
  2339. },
  2340. /* HW ERROR */
  2341. {
  2342. .ioctl_ops = {},
  2343. .crm_ops = {},
  2344. .irq_ops = NULL,
  2345. },
  2346. /* HALT */
  2347. {
  2348. .ioctl_ops = {},
  2349. .crm_ops = {},
  2350. .irq_ops = NULL,
  2351. },
  2352. };
  2353. static int __cam_isp_ctx_release_hw_in_top_state(struct cam_context *ctx,
  2354. void *cmd)
  2355. {
  2356. int rc = 0;
  2357. struct cam_hw_release_args rel_arg;
  2358. struct cam_isp_context *ctx_isp =
  2359. (struct cam_isp_context *) ctx->ctx_priv;
  2360. struct cam_req_mgr_flush_request flush_req;
  2361. if (ctx_isp->hw_ctx) {
  2362. rel_arg.ctxt_to_hw_map = ctx_isp->hw_ctx;
  2363. ctx->hw_mgr_intf->hw_release(ctx->hw_mgr_intf->hw_mgr_priv,
  2364. &rel_arg);
  2365. ctx_isp->hw_ctx = NULL;
  2366. } else {
  2367. CAM_ERR(CAM_ISP, "No hw resources acquired for this ctx");
  2368. }
  2369. ctx->last_flush_req = 0;
  2370. ctx_isp->frame_id = 0;
  2371. ctx_isp->active_req_cnt = 0;
  2372. ctx_isp->reported_req_id = 0;
  2373. ctx_isp->hw_acquired = false;
  2374. ctx_isp->init_received = false;
  2375. atomic64_set(&ctx_isp->state_monitor_head, -1);
  2376. /*
  2377. * Ideally, we should never have any active request here.
  2378. * But we still add some sanity check code here to help the debug
  2379. */
  2380. if (!list_empty(&ctx->active_req_list))
  2381. CAM_WARN(CAM_ISP, "Active list is not empty");
  2382. /* Flush all the pending request list */
  2383. flush_req.type = CAM_REQ_MGR_FLUSH_TYPE_ALL;
  2384. flush_req.link_hdl = ctx->link_hdl;
  2385. flush_req.dev_hdl = ctx->dev_hdl;
  2386. CAM_DBG(CAM_ISP, "try to flush pending list");
  2387. spin_lock_bh(&ctx->lock);
  2388. rc = __cam_isp_ctx_flush_req(ctx, &ctx->pending_req_list, &flush_req);
  2389. spin_unlock_bh(&ctx->lock);
  2390. ctx->state = CAM_CTX_ACQUIRED;
  2391. trace_cam_context_state("ISP", ctx);
  2392. CAM_DBG(CAM_ISP, "Release device success[%u] next state %d",
  2393. ctx->ctx_id, ctx->state);
  2394. return rc;
  2395. }
  2396. /* top level state machine */
  2397. static int __cam_isp_ctx_release_dev_in_top_state(struct cam_context *ctx,
  2398. struct cam_release_dev_cmd *cmd)
  2399. {
  2400. int rc = 0;
  2401. struct cam_hw_release_args rel_arg;
  2402. struct cam_isp_context *ctx_isp =
  2403. (struct cam_isp_context *) ctx->ctx_priv;
  2404. struct cam_req_mgr_flush_request flush_req;
  2405. if (cmd && ctx_isp->hw_ctx) {
  2406. CAM_ERR(CAM_ISP, "releasing hw");
  2407. __cam_isp_ctx_release_hw_in_top_state(ctx, NULL);
  2408. }
  2409. if (ctx_isp->hw_ctx) {
  2410. rel_arg.ctxt_to_hw_map = ctx_isp->hw_ctx;
  2411. ctx->hw_mgr_intf->hw_release(ctx->hw_mgr_intf->hw_mgr_priv,
  2412. &rel_arg);
  2413. ctx_isp->hw_ctx = NULL;
  2414. }
  2415. ctx->session_hdl = -1;
  2416. ctx->dev_hdl = -1;
  2417. ctx->link_hdl = -1;
  2418. ctx->ctx_crm_intf = NULL;
  2419. ctx->last_flush_req = 0;
  2420. ctx_isp->frame_id = 0;
  2421. ctx_isp->active_req_cnt = 0;
  2422. ctx_isp->reported_req_id = 0;
  2423. ctx_isp->hw_acquired = false;
  2424. ctx_isp->init_received = false;
  2425. atomic64_set(&ctx_isp->state_monitor_head, -1);
  2426. /*
  2427. * Ideally, we should never have any active request here.
  2428. * But we still add some sanity check code here to help the debug
  2429. */
  2430. if (!list_empty(&ctx->active_req_list))
  2431. CAM_ERR(CAM_ISP, "Active list is not empty");
  2432. /* Flush all the pending request list */
  2433. flush_req.type = CAM_REQ_MGR_FLUSH_TYPE_ALL;
  2434. flush_req.link_hdl = ctx->link_hdl;
  2435. flush_req.dev_hdl = ctx->dev_hdl;
  2436. CAM_DBG(CAM_ISP, "try to flush pending list");
  2437. spin_lock_bh(&ctx->lock);
  2438. rc = __cam_isp_ctx_flush_req(ctx, &ctx->pending_req_list, &flush_req);
  2439. spin_unlock_bh(&ctx->lock);
  2440. ctx->state = CAM_CTX_AVAILABLE;
  2441. trace_cam_context_state("ISP", ctx);
  2442. CAM_DBG(CAM_ISP, "Release device success[%u] next state %d",
  2443. ctx->ctx_id, ctx->state);
  2444. return rc;
  2445. }
  2446. static int __cam_isp_ctx_config_dev_in_top_state(
  2447. struct cam_context *ctx, struct cam_config_dev_cmd *cmd)
  2448. {
  2449. int rc = 0, i;
  2450. struct cam_ctx_request *req = NULL;
  2451. struct cam_isp_ctx_req *req_isp;
  2452. uintptr_t packet_addr;
  2453. struct cam_packet *packet;
  2454. size_t len = 0;
  2455. size_t remain_len = 0;
  2456. struct cam_hw_prepare_update_args cfg;
  2457. struct cam_req_mgr_add_request add_req;
  2458. struct cam_isp_context *ctx_isp =
  2459. (struct cam_isp_context *) ctx->ctx_priv;
  2460. CAM_DBG(CAM_ISP, "get free request object......");
  2461. /* get free request */
  2462. spin_lock_bh(&ctx->lock);
  2463. if (!list_empty(&ctx->free_req_list)) {
  2464. req = list_first_entry(&ctx->free_req_list,
  2465. struct cam_ctx_request, list);
  2466. list_del_init(&req->list);
  2467. }
  2468. spin_unlock_bh(&ctx->lock);
  2469. if (!req) {
  2470. CAM_ERR(CAM_ISP, "No more request obj free");
  2471. return -ENOMEM;
  2472. }
  2473. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  2474. /* for config dev, only memory handle is supported */
  2475. /* map packet from the memhandle */
  2476. rc = cam_mem_get_cpu_buf((int32_t) cmd->packet_handle,
  2477. &packet_addr, &len);
  2478. if (rc != 0) {
  2479. CAM_ERR(CAM_ISP, "Can not get packet address");
  2480. rc = -EINVAL;
  2481. goto free_req;
  2482. }
  2483. remain_len = len;
  2484. if ((len < sizeof(struct cam_packet)) ||
  2485. ((size_t)cmd->offset >= len - sizeof(struct cam_packet))) {
  2486. CAM_ERR(CAM_ISP, "invalid buff length: %zu or offset", len);
  2487. rc = -EINVAL;
  2488. goto free_req;
  2489. }
  2490. remain_len -= (size_t)cmd->offset;
  2491. packet = (struct cam_packet *)(packet_addr + (uint32_t)cmd->offset);
  2492. CAM_DBG(CAM_ISP, "pack_handle %llx", cmd->packet_handle);
  2493. CAM_DBG(CAM_ISP, "packet address is 0x%zx", packet_addr);
  2494. CAM_DBG(CAM_ISP, "packet with length %zu, offset 0x%llx",
  2495. len, cmd->offset);
  2496. CAM_DBG(CAM_ISP, "Packet request id %lld",
  2497. packet->header.request_id);
  2498. CAM_DBG(CAM_ISP, "Packet size 0x%x", packet->header.size);
  2499. CAM_DBG(CAM_ISP, "packet op %d", packet->header.op_code);
  2500. if ((((packet->header.op_code + 1) & 0xF) == CAM_ISP_PACKET_UPDATE_DEV)
  2501. && (packet->header.request_id <= ctx->last_flush_req)) {
  2502. CAM_INFO(CAM_ISP,
  2503. "request %lld has been flushed, reject packet",
  2504. packet->header.request_id);
  2505. rc = -EINVAL;
  2506. goto free_req;
  2507. }
  2508. /* preprocess the configuration */
  2509. memset(&cfg, 0, sizeof(cfg));
  2510. cfg.packet = packet;
  2511. cfg.remain_len = remain_len;
  2512. cfg.ctxt_to_hw_map = ctx_isp->hw_ctx;
  2513. cfg.max_hw_update_entries = CAM_ISP_CTX_CFG_MAX;
  2514. cfg.hw_update_entries = req_isp->cfg;
  2515. cfg.max_out_map_entries = CAM_ISP_CTX_RES_MAX;
  2516. cfg.max_in_map_entries = CAM_ISP_CTX_RES_MAX;
  2517. cfg.out_map_entries = req_isp->fence_map_out;
  2518. cfg.in_map_entries = req_isp->fence_map_in;
  2519. cfg.priv = &req_isp->hw_update_data;
  2520. cfg.pf_data = &(req->pf_data);
  2521. CAM_DBG(CAM_ISP, "try to prepare config packet......");
  2522. rc = ctx->hw_mgr_intf->hw_prepare_update(
  2523. ctx->hw_mgr_intf->hw_mgr_priv, &cfg);
  2524. if (rc != 0) {
  2525. CAM_ERR(CAM_ISP, "Prepare config packet failed in HW layer");
  2526. rc = -EFAULT;
  2527. goto free_req;
  2528. }
  2529. req_isp->num_cfg = cfg.num_hw_update_entries;
  2530. req_isp->num_fence_map_out = cfg.num_out_map_entries;
  2531. req_isp->num_fence_map_in = cfg.num_in_map_entries;
  2532. req_isp->num_acked = 0;
  2533. req_isp->bubble_detected = false;
  2534. for (i = 0; i < req_isp->num_fence_map_out; i++) {
  2535. rc = cam_sync_get_obj_ref(req_isp->fence_map_out[i].sync_id);
  2536. if (rc) {
  2537. CAM_ERR(CAM_ISP, "Can't get ref for fence %d",
  2538. req_isp->fence_map_out[i].sync_id);
  2539. goto put_ref;
  2540. }
  2541. }
  2542. CAM_DBG(CAM_ISP, "num_entry: %d, num fence out: %d, num fence in: %d",
  2543. req_isp->num_cfg, req_isp->num_fence_map_out,
  2544. req_isp->num_fence_map_in);
  2545. req->request_id = packet->header.request_id;
  2546. req->status = 1;
  2547. CAM_DBG(CAM_ISP, "Packet request id %lld packet opcode:%d",
  2548. packet->header.request_id,
  2549. req_isp->hw_update_data.packet_opcode_type);
  2550. if (req_isp->hw_update_data.packet_opcode_type ==
  2551. CAM_ISP_PACKET_INIT_DEV) {
  2552. if (ctx->state < CAM_CTX_ACTIVATED) {
  2553. rc = __cam_isp_ctx_enqueue_init_request(ctx, req);
  2554. if (rc)
  2555. CAM_ERR(CAM_ISP, "Enqueue INIT pkt failed");
  2556. ctx_isp->init_received = true;
  2557. } else {
  2558. rc = -EINVAL;
  2559. CAM_ERR(CAM_ISP, "Recevied INIT pkt in wrong state");
  2560. }
  2561. } else {
  2562. if (ctx->state >= CAM_CTX_READY && ctx->ctx_crm_intf->add_req) {
  2563. add_req.link_hdl = ctx->link_hdl;
  2564. add_req.dev_hdl = ctx->dev_hdl;
  2565. add_req.req_id = req->request_id;
  2566. add_req.skip_before_applying = 0;
  2567. rc = ctx->ctx_crm_intf->add_req(&add_req);
  2568. if (rc) {
  2569. CAM_ERR(CAM_ISP, "Add req failed: req id=%llu",
  2570. req->request_id);
  2571. } else {
  2572. __cam_isp_ctx_enqueue_request_in_order(
  2573. ctx, req);
  2574. }
  2575. } else {
  2576. rc = -EINVAL;
  2577. CAM_ERR(CAM_ISP, "Recevied Update in wrong state");
  2578. }
  2579. }
  2580. if (rc)
  2581. goto put_ref;
  2582. CAM_DBG(CAM_REQ,
  2583. "Preprocessing Config req_id %lld successful on ctx %u",
  2584. req->request_id, ctx->ctx_id);
  2585. return rc;
  2586. put_ref:
  2587. for (--i; i >= 0; i--) {
  2588. if (cam_sync_put_obj_ref(req_isp->fence_map_out[i].sync_id))
  2589. CAM_ERR(CAM_CTXT, "Failed to put ref of fence %d",
  2590. req_isp->fence_map_out[i].sync_id);
  2591. }
  2592. free_req:
  2593. spin_lock_bh(&ctx->lock);
  2594. list_add_tail(&req->list, &ctx->free_req_list);
  2595. spin_unlock_bh(&ctx->lock);
  2596. return rc;
  2597. }
  2598. static int __cam_isp_ctx_acquire_dev_in_available(struct cam_context *ctx,
  2599. struct cam_acquire_dev_cmd *cmd)
  2600. {
  2601. int rc = 0;
  2602. struct cam_hw_acquire_args param;
  2603. struct cam_isp_resource *isp_res = NULL;
  2604. struct cam_create_dev_hdl req_hdl_param;
  2605. struct cam_hw_release_args release;
  2606. struct cam_isp_context *ctx_isp =
  2607. (struct cam_isp_context *) ctx->ctx_priv;
  2608. struct cam_hw_cmd_args hw_cmd_args;
  2609. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  2610. if (!ctx->hw_mgr_intf) {
  2611. CAM_ERR(CAM_ISP, "HW interface is not ready");
  2612. rc = -EFAULT;
  2613. goto end;
  2614. }
  2615. CAM_DBG(CAM_ISP,
  2616. "session_hdl 0x%x, num_resources %d, hdl type %d, res %lld",
  2617. cmd->session_handle, cmd->num_resources,
  2618. cmd->handle_type, cmd->resource_hdl);
  2619. if (cmd->num_resources == CAM_API_COMPAT_CONSTANT) {
  2620. ctx_isp->split_acquire = true;
  2621. CAM_DBG(CAM_ISP, "Acquire dev handle");
  2622. goto get_dev_handle;
  2623. }
  2624. if (cmd->num_resources > CAM_ISP_CTX_RES_MAX) {
  2625. CAM_ERR(CAM_ISP, "Too much resources in the acquire");
  2626. rc = -ENOMEM;
  2627. goto end;
  2628. }
  2629. /* for now we only support user pointer */
  2630. if (cmd->handle_type != 1) {
  2631. CAM_ERR(CAM_ISP, "Only user pointer is supported");
  2632. rc = -EINVAL;
  2633. goto end;
  2634. }
  2635. isp_res = kzalloc(
  2636. sizeof(*isp_res)*cmd->num_resources, GFP_KERNEL);
  2637. if (!isp_res) {
  2638. rc = -ENOMEM;
  2639. goto end;
  2640. }
  2641. CAM_DBG(CAM_ISP, "start copy %d resources from user",
  2642. cmd->num_resources);
  2643. if (copy_from_user(isp_res, u64_to_user_ptr(cmd->resource_hdl),
  2644. sizeof(*isp_res)*cmd->num_resources)) {
  2645. rc = -EFAULT;
  2646. goto free_res;
  2647. }
  2648. param.context_data = ctx;
  2649. param.event_cb = ctx->irq_cb_intf;
  2650. param.num_acq = cmd->num_resources;
  2651. param.acquire_info = (uintptr_t) isp_res;
  2652. /* call HW manager to reserve the resource */
  2653. rc = ctx->hw_mgr_intf->hw_acquire(ctx->hw_mgr_intf->hw_mgr_priv,
  2654. &param);
  2655. if (rc != 0) {
  2656. CAM_ERR(CAM_ISP, "Acquire device failed");
  2657. goto free_res;
  2658. }
  2659. /* Query the context has rdi only resource */
  2660. hw_cmd_args.ctxt_to_hw_map = param.ctxt_to_hw_map;
  2661. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  2662. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_CMD_CTX_TYPE;
  2663. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  2664. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  2665. &hw_cmd_args);
  2666. if (rc) {
  2667. CAM_ERR(CAM_ISP, "HW command failed");
  2668. goto free_hw;
  2669. }
  2670. if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_RDI) {
  2671. /*
  2672. * this context has rdi only resource assign rdi only
  2673. * state machine
  2674. */
  2675. CAM_DBG(CAM_ISP, "RDI only session Context");
  2676. ctx_isp->substate_machine_irq =
  2677. cam_isp_ctx_rdi_only_activated_state_machine_irq;
  2678. ctx_isp->substate_machine =
  2679. cam_isp_ctx_rdi_only_activated_state_machine;
  2680. ctx_isp->rdi_only_context = true;
  2681. } else if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_FS2) {
  2682. CAM_DBG(CAM_ISP, "FS2 Session has PIX ,RD and RDI");
  2683. ctx_isp->substate_machine_irq =
  2684. cam_isp_ctx_fs2_state_machine_irq;
  2685. ctx_isp->substate_machine =
  2686. cam_isp_ctx_fs2_state_machine;
  2687. } else {
  2688. CAM_DBG(CAM_ISP, "Session has PIX or PIX and RDI resources");
  2689. ctx_isp->substate_machine_irq =
  2690. cam_isp_ctx_activated_state_machine_irq;
  2691. ctx_isp->substate_machine =
  2692. cam_isp_ctx_activated_state_machine;
  2693. }
  2694. ctx_isp->hw_ctx = param.ctxt_to_hw_map;
  2695. ctx_isp->hw_acquired = true;
  2696. ctx_isp->split_acquire = false;
  2697. ctx->ctxt_to_hw_map = param.ctxt_to_hw_map;
  2698. atomic64_set(&ctx_isp->state_monitor_head, -1);
  2699. kfree(isp_res);
  2700. isp_res = NULL;
  2701. get_dev_handle:
  2702. req_hdl_param.session_hdl = cmd->session_handle;
  2703. /* bridge is not ready for these flags. so false for now */
  2704. req_hdl_param.v4l2_sub_dev_flag = 0;
  2705. req_hdl_param.media_entity_flag = 0;
  2706. req_hdl_param.ops = ctx->crm_ctx_intf;
  2707. req_hdl_param.priv = ctx;
  2708. CAM_DBG(CAM_ISP, "get device handle form bridge");
  2709. ctx->dev_hdl = cam_create_device_hdl(&req_hdl_param);
  2710. if (ctx->dev_hdl <= 0) {
  2711. rc = -EFAULT;
  2712. CAM_ERR(CAM_ISP, "Can not create device handle");
  2713. goto free_hw;
  2714. }
  2715. cmd->dev_handle = ctx->dev_hdl;
  2716. /* store session information */
  2717. ctx->session_hdl = cmd->session_handle;
  2718. ctx->state = CAM_CTX_ACQUIRED;
  2719. trace_cam_context_state("ISP", ctx);
  2720. CAM_DBG(CAM_ISP,
  2721. "Acquire success on session_hdl 0x%x num_rsrces %d ctx %u",
  2722. cmd->session_handle, cmd->num_resources, ctx->ctx_id);
  2723. return rc;
  2724. free_hw:
  2725. release.ctxt_to_hw_map = ctx_isp->hw_ctx;
  2726. if (ctx_isp->hw_acquired)
  2727. ctx->hw_mgr_intf->hw_release(ctx->hw_mgr_intf->hw_mgr_priv,
  2728. &release);
  2729. ctx_isp->hw_ctx = NULL;
  2730. ctx_isp->hw_acquired = false;
  2731. free_res:
  2732. kfree(isp_res);
  2733. end:
  2734. return rc;
  2735. }
  2736. static int __cam_isp_ctx_acquire_hw_v1(struct cam_context *ctx,
  2737. void *args)
  2738. {
  2739. int rc = 0;
  2740. struct cam_acquire_hw_cmd_v1 *cmd =
  2741. (struct cam_acquire_hw_cmd_v1 *)args;
  2742. struct cam_hw_acquire_args param;
  2743. struct cam_hw_release_args release;
  2744. struct cam_isp_context *ctx_isp =
  2745. (struct cam_isp_context *) ctx->ctx_priv;
  2746. struct cam_hw_cmd_args hw_cmd_args;
  2747. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  2748. struct cam_isp_acquire_hw_info *acquire_hw_info = NULL;
  2749. if (!ctx->hw_mgr_intf) {
  2750. CAM_ERR(CAM_ISP, "HW interface is not ready");
  2751. rc = -EFAULT;
  2752. goto end;
  2753. }
  2754. CAM_DBG(CAM_ISP,
  2755. "session_hdl 0x%x, hdl type %d, res %lld",
  2756. cmd->session_handle, cmd->handle_type, cmd->resource_hdl);
  2757. /* for now we only support user pointer */
  2758. if (cmd->handle_type != 1) {
  2759. CAM_ERR(CAM_ISP, "Only user pointer is supported");
  2760. rc = -EINVAL;
  2761. goto end;
  2762. }
  2763. if (cmd->data_size < sizeof(*acquire_hw_info)) {
  2764. CAM_ERR(CAM_ISP, "data_size is not a valid value");
  2765. goto end;
  2766. }
  2767. acquire_hw_info = kzalloc(cmd->data_size, GFP_KERNEL);
  2768. if (!acquire_hw_info) {
  2769. rc = -ENOMEM;
  2770. goto end;
  2771. }
  2772. CAM_DBG(CAM_ISP, "start copy resources from user");
  2773. if (copy_from_user(acquire_hw_info, (void __user *)cmd->resource_hdl,
  2774. cmd->data_size)) {
  2775. rc = -EFAULT;
  2776. goto free_res;
  2777. }
  2778. memset(&param, 0, sizeof(param));
  2779. param.context_data = ctx;
  2780. param.event_cb = ctx->irq_cb_intf;
  2781. param.num_acq = CAM_API_COMPAT_CONSTANT;
  2782. param.acquire_info_size = cmd->data_size;
  2783. param.acquire_info = (uint64_t) acquire_hw_info;
  2784. /* call HW manager to reserve the resource */
  2785. rc = ctx->hw_mgr_intf->hw_acquire(ctx->hw_mgr_intf->hw_mgr_priv,
  2786. &param);
  2787. if (rc != 0) {
  2788. CAM_ERR(CAM_ISP, "Acquire device failed");
  2789. goto free_res;
  2790. }
  2791. /* Query the context has rdi only resource */
  2792. hw_cmd_args.ctxt_to_hw_map = param.ctxt_to_hw_map;
  2793. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  2794. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_CMD_CTX_TYPE;
  2795. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  2796. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  2797. &hw_cmd_args);
  2798. if (rc) {
  2799. CAM_ERR(CAM_ISP, "HW command failed");
  2800. goto free_hw;
  2801. }
  2802. if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_RDI) {
  2803. /*
  2804. * this context has rdi only resource assign rdi only
  2805. * state machine
  2806. */
  2807. CAM_DBG(CAM_ISP, "RDI only session Context");
  2808. ctx_isp->substate_machine_irq =
  2809. cam_isp_ctx_rdi_only_activated_state_machine_irq;
  2810. ctx_isp->substate_machine =
  2811. cam_isp_ctx_rdi_only_activated_state_machine;
  2812. ctx_isp->rdi_only_context = true;
  2813. } else if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_FS2) {
  2814. CAM_DBG(CAM_ISP, "FS2 Session has PIX ,RD and RDI");
  2815. ctx_isp->substate_machine_irq =
  2816. cam_isp_ctx_fs2_state_machine_irq;
  2817. ctx_isp->substate_machine =
  2818. cam_isp_ctx_fs2_state_machine;
  2819. } else {
  2820. CAM_DBG(CAM_ISP, "Session has PIX or PIX and RDI resources");
  2821. ctx_isp->substate_machine_irq =
  2822. cam_isp_ctx_activated_state_machine_irq;
  2823. ctx_isp->substate_machine =
  2824. cam_isp_ctx_activated_state_machine;
  2825. }
  2826. ctx_isp->hw_ctx = param.ctxt_to_hw_map;
  2827. ctx_isp->hw_acquired = true;
  2828. ctx->ctxt_to_hw_map = param.ctxt_to_hw_map;
  2829. atomic64_set(&ctx_isp->state_monitor_head, -1);
  2830. trace_cam_context_state("ISP", ctx);
  2831. CAM_DBG(CAM_ISP,
  2832. "Acquire success on session_hdl 0x%xs ctx_type %d ctx_id %u",
  2833. ctx->session_hdl, isp_hw_cmd_args.u.ctx_type, ctx->ctx_id);
  2834. kfree(acquire_hw_info);
  2835. return rc;
  2836. free_hw:
  2837. release.ctxt_to_hw_map = ctx_isp->hw_ctx;
  2838. ctx->hw_mgr_intf->hw_release(ctx->hw_mgr_intf->hw_mgr_priv, &release);
  2839. ctx_isp->hw_ctx = NULL;
  2840. ctx_isp->hw_acquired = false;
  2841. free_res:
  2842. kfree(acquire_hw_info);
  2843. end:
  2844. return rc;
  2845. }
  2846. static int __cam_isp_ctx_acquire_hw_v2(struct cam_context *ctx,
  2847. void *args)
  2848. {
  2849. int rc = 0, i, j;
  2850. struct cam_acquire_hw_cmd_v2 *cmd =
  2851. (struct cam_acquire_hw_cmd_v2 *)args;
  2852. struct cam_hw_acquire_args param;
  2853. struct cam_hw_release_args release;
  2854. struct cam_isp_context *ctx_isp =
  2855. (struct cam_isp_context *) ctx->ctx_priv;
  2856. struct cam_hw_cmd_args hw_cmd_args;
  2857. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  2858. struct cam_isp_acquire_hw_info *acquire_hw_info = NULL;
  2859. if (!ctx->hw_mgr_intf) {
  2860. CAM_ERR(CAM_ISP, "HW interface is not ready");
  2861. rc = -EFAULT;
  2862. goto end;
  2863. }
  2864. CAM_DBG(CAM_ISP,
  2865. "session_hdl 0x%x, hdl type %d, res %lld",
  2866. cmd->session_handle, cmd->handle_type, cmd->resource_hdl);
  2867. /* for now we only support user pointer */
  2868. if (cmd->handle_type != 1) {
  2869. CAM_ERR(CAM_ISP, "Only user pointer is supported");
  2870. rc = -EINVAL;
  2871. goto end;
  2872. }
  2873. if (cmd->data_size < sizeof(*acquire_hw_info)) {
  2874. CAM_ERR(CAM_ISP, "data_size is not a valid value");
  2875. goto end;
  2876. }
  2877. acquire_hw_info = kzalloc(cmd->data_size, GFP_KERNEL);
  2878. if (!acquire_hw_info) {
  2879. rc = -ENOMEM;
  2880. goto end;
  2881. }
  2882. CAM_DBG(CAM_ISP, "start copy resources from user");
  2883. if (copy_from_user(acquire_hw_info, (void __user *)cmd->resource_hdl,
  2884. cmd->data_size)) {
  2885. rc = -EFAULT;
  2886. goto free_res;
  2887. }
  2888. memset(&param, 0, sizeof(param));
  2889. param.context_data = ctx;
  2890. param.event_cb = ctx->irq_cb_intf;
  2891. param.num_acq = CAM_API_COMPAT_CONSTANT;
  2892. param.acquire_info_size = cmd->data_size;
  2893. param.acquire_info = (uint64_t) acquire_hw_info;
  2894. /* call HW manager to reserve the resource */
  2895. rc = ctx->hw_mgr_intf->hw_acquire(ctx->hw_mgr_intf->hw_mgr_priv,
  2896. &param);
  2897. if (rc != 0) {
  2898. CAM_ERR(CAM_ISP, "Acquire device failed");
  2899. goto free_res;
  2900. }
  2901. /* Query the context has rdi only resource */
  2902. hw_cmd_args.ctxt_to_hw_map = param.ctxt_to_hw_map;
  2903. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  2904. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_CMD_CTX_TYPE;
  2905. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  2906. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  2907. &hw_cmd_args);
  2908. if (rc) {
  2909. CAM_ERR(CAM_ISP, "HW command failed");
  2910. goto free_hw;
  2911. }
  2912. if (param.valid_acquired_hw) {
  2913. for (i = 0; i < CAM_MAX_ACQ_RES; i++)
  2914. cmd->hw_info.acquired_hw_id[i] =
  2915. param.acquired_hw_id[i];
  2916. for (i = 0; i < CAM_MAX_ACQ_RES; i++)
  2917. for (j = 0; j < CAM_MAX_HW_SPLIT; j++)
  2918. cmd->hw_info.acquired_hw_path[i][j] =
  2919. param.acquired_hw_path[i][j];
  2920. }
  2921. cmd->hw_info.valid_acquired_hw =
  2922. param.valid_acquired_hw;
  2923. cmd->hw_info.valid_acquired_hw = param.valid_acquired_hw;
  2924. if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_RDI) {
  2925. /*
  2926. * this context has rdi only resource assign rdi only
  2927. * state machine
  2928. */
  2929. CAM_DBG(CAM_ISP, "RDI only session Context");
  2930. ctx_isp->substate_machine_irq =
  2931. cam_isp_ctx_rdi_only_activated_state_machine_irq;
  2932. ctx_isp->substate_machine =
  2933. cam_isp_ctx_rdi_only_activated_state_machine;
  2934. ctx_isp->rdi_only_context = true;
  2935. } else if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_FS2) {
  2936. CAM_DBG(CAM_ISP, "FS2 Session has PIX ,RD and RDI");
  2937. ctx_isp->substate_machine_irq =
  2938. cam_isp_ctx_fs2_state_machine_irq;
  2939. ctx_isp->substate_machine =
  2940. cam_isp_ctx_fs2_state_machine;
  2941. } else {
  2942. CAM_DBG(CAM_ISP, "Session has PIX or PIX and RDI resources");
  2943. ctx_isp->substate_machine_irq =
  2944. cam_isp_ctx_activated_state_machine_irq;
  2945. ctx_isp->substate_machine =
  2946. cam_isp_ctx_activated_state_machine;
  2947. }
  2948. ctx_isp->hw_ctx = param.ctxt_to_hw_map;
  2949. ctx_isp->hw_acquired = true;
  2950. ctx->ctxt_to_hw_map = param.ctxt_to_hw_map;
  2951. trace_cam_context_state("ISP", ctx);
  2952. CAM_DBG(CAM_ISP,
  2953. "Acquire success on session_hdl 0x%xs ctx_type %d ctx_id %u",
  2954. ctx->session_hdl, isp_hw_cmd_args.u.ctx_type, ctx->ctx_id);
  2955. kfree(acquire_hw_info);
  2956. return rc;
  2957. free_hw:
  2958. release.ctxt_to_hw_map = ctx_isp->hw_ctx;
  2959. ctx->hw_mgr_intf->hw_release(ctx->hw_mgr_intf->hw_mgr_priv, &release);
  2960. ctx_isp->hw_ctx = NULL;
  2961. ctx_isp->hw_acquired = false;
  2962. free_res:
  2963. kfree(acquire_hw_info);
  2964. end:
  2965. return rc;
  2966. }
  2967. static int __cam_isp_ctx_acquire_hw_in_acquired(struct cam_context *ctx,
  2968. void *args)
  2969. {
  2970. int rc = -EINVAL;
  2971. uint32_t api_version;
  2972. if (!ctx || !args) {
  2973. CAM_ERR(CAM_ISP, "Invalid input pointer");
  2974. return rc;
  2975. }
  2976. api_version = *((uint32_t *)args);
  2977. if (api_version == 1)
  2978. rc = __cam_isp_ctx_acquire_hw_v1(ctx, args);
  2979. else if (api_version == 2)
  2980. rc = __cam_isp_ctx_acquire_hw_v2(ctx, args);
  2981. else
  2982. CAM_ERR(CAM_ISP, "Unsupported api version %d", api_version);
  2983. return rc;
  2984. }
  2985. static int __cam_isp_ctx_config_dev_in_acquired(struct cam_context *ctx,
  2986. struct cam_config_dev_cmd *cmd)
  2987. {
  2988. int rc = 0;
  2989. struct cam_isp_context *ctx_isp =
  2990. (struct cam_isp_context *) ctx->ctx_priv;
  2991. if (!ctx_isp->hw_acquired) {
  2992. CAM_ERR(CAM_ISP, "HW is not acquired, reject packet");
  2993. return -EINVAL;
  2994. }
  2995. rc = __cam_isp_ctx_config_dev_in_top_state(ctx, cmd);
  2996. if (!rc && (ctx->link_hdl >= 0)) {
  2997. ctx->state = CAM_CTX_READY;
  2998. trace_cam_context_state("ISP", ctx);
  2999. }
  3000. CAM_DBG(CAM_ISP, "next state %d", ctx->state);
  3001. return rc;
  3002. }
  3003. static int __cam_isp_ctx_link_in_acquired(struct cam_context *ctx,
  3004. struct cam_req_mgr_core_dev_link_setup *link)
  3005. {
  3006. int rc = 0;
  3007. struct cam_isp_context *ctx_isp =
  3008. (struct cam_isp_context *) ctx->ctx_priv;
  3009. CAM_DBG(CAM_ISP, "Enter.........");
  3010. ctx->link_hdl = link->link_hdl;
  3011. ctx->ctx_crm_intf = link->crm_cb;
  3012. ctx_isp->subscribe_event = link->subscribe_event;
  3013. /* change state only if we had the init config */
  3014. if (ctx_isp->init_received) {
  3015. ctx->state = CAM_CTX_READY;
  3016. trace_cam_context_state("ISP", ctx);
  3017. }
  3018. CAM_DBG(CAM_ISP, "next state %d", ctx->state);
  3019. return rc;
  3020. }
  3021. static int __cam_isp_ctx_unlink_in_acquired(struct cam_context *ctx,
  3022. struct cam_req_mgr_core_dev_link_setup *unlink)
  3023. {
  3024. int rc = 0;
  3025. ctx->link_hdl = -1;
  3026. ctx->ctx_crm_intf = NULL;
  3027. return rc;
  3028. }
  3029. static int __cam_isp_ctx_get_dev_info_in_acquired(struct cam_context *ctx,
  3030. struct cam_req_mgr_device_info *dev_info)
  3031. {
  3032. int rc = 0;
  3033. dev_info->dev_hdl = ctx->dev_hdl;
  3034. strlcpy(dev_info->name, CAM_ISP_DEV_NAME, sizeof(dev_info->name));
  3035. dev_info->dev_id = CAM_REQ_MGR_DEVICE_IFE;
  3036. dev_info->p_delay = 1;
  3037. dev_info->trigger = CAM_TRIGGER_POINT_SOF;
  3038. return rc;
  3039. }
  3040. static int __cam_isp_ctx_start_dev_in_ready(struct cam_context *ctx,
  3041. struct cam_start_stop_dev_cmd *cmd)
  3042. {
  3043. int rc = 0;
  3044. struct cam_isp_start_args start_isp;
  3045. struct cam_ctx_request *req;
  3046. struct cam_isp_ctx_req *req_isp;
  3047. struct cam_isp_context *ctx_isp =
  3048. (struct cam_isp_context *) ctx->ctx_priv;
  3049. if (cmd->session_handle != ctx->session_hdl ||
  3050. cmd->dev_handle != ctx->dev_hdl) {
  3051. rc = -EPERM;
  3052. goto end;
  3053. }
  3054. if (list_empty(&ctx->pending_req_list)) {
  3055. /* should never happen */
  3056. CAM_ERR(CAM_ISP, "Start device with empty configuration");
  3057. rc = -EFAULT;
  3058. goto end;
  3059. } else {
  3060. req = list_first_entry(&ctx->pending_req_list,
  3061. struct cam_ctx_request, list);
  3062. }
  3063. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  3064. if (!ctx_isp->hw_ctx) {
  3065. CAM_ERR(CAM_ISP, "Wrong hw context pointer.");
  3066. rc = -EFAULT;
  3067. goto end;
  3068. }
  3069. start_isp.hw_config.ctxt_to_hw_map = ctx_isp->hw_ctx;
  3070. start_isp.hw_config.request_id = req->request_id;
  3071. start_isp.hw_config.hw_update_entries = req_isp->cfg;
  3072. start_isp.hw_config.num_hw_update_entries = req_isp->num_cfg;
  3073. start_isp.hw_config.priv = &req_isp->hw_update_data;
  3074. start_isp.hw_config.init_packet = 1;
  3075. start_isp.start_only = false;
  3076. atomic_set(&ctx_isp->process_bubble, 0);
  3077. ctx_isp->frame_id = 0;
  3078. ctx_isp->active_req_cnt = 0;
  3079. ctx_isp->reported_req_id = 0;
  3080. ctx_isp->substate_activated = ctx_isp->rdi_only_context ?
  3081. CAM_ISP_CTX_ACTIVATED_APPLIED :
  3082. (req_isp->num_fence_map_out) ? CAM_ISP_CTX_ACTIVATED_EPOCH :
  3083. CAM_ISP_CTX_ACTIVATED_SOF;
  3084. atomic64_set(&ctx_isp->state_monitor_head, -1);
  3085. /*
  3086. * In case of CSID TPG we might receive SOF and RUP IRQs
  3087. * before hw_mgr_intf->hw_start has returned. So move
  3088. * req out of pending list before hw_start and add it
  3089. * back to pending list if hw_start fails.
  3090. */
  3091. list_del_init(&req->list);
  3092. if (ctx_isp->rdi_only_context || !req_isp->num_fence_map_out) {
  3093. list_add_tail(&req->list, &ctx->wait_req_list);
  3094. } else {
  3095. list_add_tail(&req->list, &ctx->active_req_list);
  3096. ctx_isp->active_req_cnt++;
  3097. }
  3098. /*
  3099. * Only place to change state before calling the hw due to
  3100. * hardware tasklet has higher priority that can cause the
  3101. * irq handling comes early
  3102. */
  3103. ctx->state = CAM_CTX_ACTIVATED;
  3104. trace_cam_context_state("ISP", ctx);
  3105. rc = ctx->hw_mgr_intf->hw_start(ctx->hw_mgr_intf->hw_mgr_priv,
  3106. &start_isp);
  3107. if (rc) {
  3108. /* HW failure. user need to clean up the resource */
  3109. CAM_ERR(CAM_ISP, "Start HW failed");
  3110. ctx->state = CAM_CTX_READY;
  3111. trace_cam_context_state("ISP", ctx);
  3112. list_del_init(&req->list);
  3113. list_add(&req->list, &ctx->pending_req_list);
  3114. goto end;
  3115. }
  3116. CAM_DBG(CAM_ISP, "start device success ctx %u", ctx->ctx_id);
  3117. end:
  3118. return rc;
  3119. }
  3120. static int __cam_isp_ctx_unlink_in_ready(struct cam_context *ctx,
  3121. struct cam_req_mgr_core_dev_link_setup *unlink)
  3122. {
  3123. int rc = 0;
  3124. ctx->link_hdl = -1;
  3125. ctx->ctx_crm_intf = NULL;
  3126. ctx->state = CAM_CTX_ACQUIRED;
  3127. trace_cam_context_state("ISP", ctx);
  3128. return rc;
  3129. }
  3130. static int __cam_isp_ctx_stop_dev_in_activated_unlock(
  3131. struct cam_context *ctx, struct cam_start_stop_dev_cmd *stop_cmd)
  3132. {
  3133. int rc = 0;
  3134. uint32_t i;
  3135. struct cam_hw_stop_args stop;
  3136. struct cam_ctx_request *req;
  3137. struct cam_isp_ctx_req *req_isp;
  3138. struct cam_isp_context *ctx_isp =
  3139. (struct cam_isp_context *) ctx->ctx_priv;
  3140. struct cam_isp_stop_args stop_isp;
  3141. /* stop hw first */
  3142. if (ctx_isp->hw_ctx) {
  3143. stop.ctxt_to_hw_map = ctx_isp->hw_ctx;
  3144. if (stop_cmd)
  3145. stop_isp.hw_stop_cmd =
  3146. CAM_ISP_HW_STOP_AT_FRAME_BOUNDARY;
  3147. else
  3148. stop_isp.hw_stop_cmd = CAM_ISP_HW_STOP_IMMEDIATELY;
  3149. stop_isp.stop_only = false;
  3150. stop.args = (void *) &stop_isp;
  3151. ctx->hw_mgr_intf->hw_stop(ctx->hw_mgr_intf->hw_mgr_priv,
  3152. &stop);
  3153. }
  3154. /* Mask off all the incoming hardware events */
  3155. spin_lock_bh(&ctx->lock);
  3156. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_HALT;
  3157. spin_unlock_bh(&ctx->lock);
  3158. CAM_DBG(CAM_ISP, "next substate %d", ctx_isp->substate_activated);
  3159. while (!list_empty(&ctx->pending_req_list)) {
  3160. req = list_first_entry(&ctx->pending_req_list,
  3161. struct cam_ctx_request, list);
  3162. list_del_init(&req->list);
  3163. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  3164. CAM_DBG(CAM_ISP, "signal fence in pending list. fence num %d",
  3165. req_isp->num_fence_map_out);
  3166. for (i = 0; i < req_isp->num_fence_map_out; i++)
  3167. if (req_isp->fence_map_out[i].sync_id != -1) {
  3168. cam_sync_signal(
  3169. req_isp->fence_map_out[i].sync_id,
  3170. CAM_SYNC_STATE_SIGNALED_ERROR);
  3171. }
  3172. list_add_tail(&req->list, &ctx->free_req_list);
  3173. }
  3174. while (!list_empty(&ctx->wait_req_list)) {
  3175. req = list_first_entry(&ctx->wait_req_list,
  3176. struct cam_ctx_request, list);
  3177. list_del_init(&req->list);
  3178. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  3179. CAM_DBG(CAM_ISP, "signal fence in wait list. fence num %d",
  3180. req_isp->num_fence_map_out);
  3181. for (i = 0; i < req_isp->num_fence_map_out; i++)
  3182. if (req_isp->fence_map_out[i].sync_id != -1) {
  3183. cam_sync_signal(
  3184. req_isp->fence_map_out[i].sync_id,
  3185. CAM_SYNC_STATE_SIGNALED_ERROR);
  3186. }
  3187. list_add_tail(&req->list, &ctx->free_req_list);
  3188. }
  3189. while (!list_empty(&ctx->active_req_list)) {
  3190. req = list_first_entry(&ctx->active_req_list,
  3191. struct cam_ctx_request, list);
  3192. list_del_init(&req->list);
  3193. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  3194. CAM_DBG(CAM_ISP, "signal fence in active list. fence num %d",
  3195. req_isp->num_fence_map_out);
  3196. for (i = 0; i < req_isp->num_fence_map_out; i++)
  3197. if (req_isp->fence_map_out[i].sync_id != -1) {
  3198. cam_sync_signal(
  3199. req_isp->fence_map_out[i].sync_id,
  3200. CAM_SYNC_STATE_SIGNALED_ERROR);
  3201. }
  3202. list_add_tail(&req->list, &ctx->free_req_list);
  3203. }
  3204. ctx_isp->frame_id = 0;
  3205. ctx_isp->active_req_cnt = 0;
  3206. ctx_isp->reported_req_id = 0;
  3207. atomic_set(&ctx_isp->process_bubble, 0);
  3208. atomic64_set(&ctx_isp->state_monitor_head, -1);
  3209. CAM_DBG(CAM_ISP, "Stop device success next state %d on ctx %u",
  3210. ctx->state, ctx->ctx_id);
  3211. if (!stop_cmd) {
  3212. rc = __cam_isp_ctx_unlink_in_ready(ctx, NULL);
  3213. if (rc)
  3214. CAM_ERR(CAM_ISP, "Unlink failed rc=%d", rc);
  3215. }
  3216. return rc;
  3217. }
  3218. static int __cam_isp_ctx_stop_dev_in_activated(struct cam_context *ctx,
  3219. struct cam_start_stop_dev_cmd *cmd)
  3220. {
  3221. int rc = 0;
  3222. struct cam_isp_context *ctx_isp =
  3223. (struct cam_isp_context *)ctx->ctx_priv;
  3224. __cam_isp_ctx_stop_dev_in_activated_unlock(ctx, cmd);
  3225. ctx_isp->init_received = false;
  3226. ctx->state = CAM_CTX_ACQUIRED;
  3227. trace_cam_context_state("ISP", ctx);
  3228. return rc;
  3229. }
  3230. static int __cam_isp_ctx_release_dev_in_activated(struct cam_context *ctx,
  3231. struct cam_release_dev_cmd *cmd)
  3232. {
  3233. int rc = 0;
  3234. rc = __cam_isp_ctx_stop_dev_in_activated_unlock(ctx, NULL);
  3235. if (rc)
  3236. CAM_ERR(CAM_ISP, "Stop device failed rc=%d", rc);
  3237. rc = __cam_isp_ctx_release_dev_in_top_state(ctx, cmd);
  3238. if (rc)
  3239. CAM_ERR(CAM_ISP, "Release device failed rc=%d", rc);
  3240. return rc;
  3241. }
  3242. static int __cam_isp_ctx_release_hw_in_activated(struct cam_context *ctx,
  3243. void *cmd)
  3244. {
  3245. int rc = 0;
  3246. rc = __cam_isp_ctx_stop_dev_in_activated_unlock(ctx, NULL);
  3247. if (rc)
  3248. CAM_ERR(CAM_ISP, "Stop device failed rc=%d", rc);
  3249. rc = __cam_isp_ctx_release_hw_in_top_state(ctx, cmd);
  3250. if (rc)
  3251. CAM_ERR(CAM_ISP, "Release hw failed rc=%d", rc);
  3252. return rc;
  3253. }
  3254. static int __cam_isp_ctx_link_pause(struct cam_context *ctx)
  3255. {
  3256. int rc = 0;
  3257. struct cam_hw_cmd_args hw_cmd_args;
  3258. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  3259. struct cam_isp_context *ctx_isp =
  3260. (struct cam_isp_context *) ctx->ctx_priv;
  3261. hw_cmd_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  3262. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  3263. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_CMD_PAUSE_HW;
  3264. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  3265. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  3266. &hw_cmd_args);
  3267. return rc;
  3268. }
  3269. static int __cam_isp_ctx_link_resume(struct cam_context *ctx)
  3270. {
  3271. int rc = 0;
  3272. struct cam_hw_cmd_args hw_cmd_args;
  3273. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  3274. struct cam_isp_context *ctx_isp =
  3275. (struct cam_isp_context *) ctx->ctx_priv;
  3276. hw_cmd_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  3277. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  3278. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_CMD_RESUME_HW;
  3279. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  3280. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  3281. &hw_cmd_args);
  3282. return rc;
  3283. }
  3284. static int __cam_isp_ctx_handle_sof_freeze_evt(
  3285. struct cam_context *ctx)
  3286. {
  3287. int rc = 0;
  3288. struct cam_hw_cmd_args hw_cmd_args;
  3289. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  3290. struct cam_isp_context *ctx_isp =
  3291. (struct cam_isp_context *) ctx->ctx_priv;
  3292. hw_cmd_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  3293. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  3294. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_CMD_SOF_DEBUG;
  3295. isp_hw_cmd_args.u.sof_irq_enable = 1;
  3296. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  3297. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  3298. &hw_cmd_args);
  3299. return rc;
  3300. }
  3301. static int __cam_isp_ctx_process_evt(struct cam_context *ctx,
  3302. struct cam_req_mgr_link_evt_data *link_evt_data)
  3303. {
  3304. int rc = 0;
  3305. switch (link_evt_data->evt_type) {
  3306. case CAM_REQ_MGR_LINK_EVT_ERR:
  3307. /* No need to handle this message now */
  3308. break;
  3309. case CAM_REQ_MGR_LINK_EVT_PAUSE:
  3310. __cam_isp_ctx_link_pause(ctx);
  3311. break;
  3312. case CAM_REQ_MGR_LINK_EVT_RESUME:
  3313. __cam_isp_ctx_link_resume(ctx);
  3314. break;
  3315. case CAM_REQ_MGR_LINK_EVT_SOF_FREEZE:
  3316. __cam_isp_ctx_handle_sof_freeze_evt(ctx);
  3317. break;
  3318. default:
  3319. CAM_WARN(CAM_ISP, "Unknown event from CRM");
  3320. break;
  3321. }
  3322. return rc;
  3323. }
  3324. static int __cam_isp_ctx_unlink_in_activated(struct cam_context *ctx,
  3325. struct cam_req_mgr_core_dev_link_setup *unlink)
  3326. {
  3327. int rc = 0;
  3328. CAM_WARN(CAM_ISP,
  3329. "Received unlink in activated state. It's unexpected");
  3330. rc = __cam_isp_ctx_stop_dev_in_activated_unlock(ctx, NULL);
  3331. if (rc)
  3332. CAM_WARN(CAM_ISP, "Stop device failed rc=%d", rc);
  3333. rc = __cam_isp_ctx_unlink_in_ready(ctx, unlink);
  3334. if (rc)
  3335. CAM_ERR(CAM_ISP, "Unlink failed rc=%d", rc);
  3336. return rc;
  3337. }
  3338. static int __cam_isp_ctx_apply_req(struct cam_context *ctx,
  3339. struct cam_req_mgr_apply_request *apply)
  3340. {
  3341. int rc = 0;
  3342. struct cam_ctx_ops *ctx_ops = NULL;
  3343. struct cam_isp_context *ctx_isp =
  3344. (struct cam_isp_context *) ctx->ctx_priv;
  3345. trace_cam_apply_req("ISP", apply->request_id);
  3346. CAM_DBG(CAM_ISP, "Enter: apply req in Substate %d request _id:%lld",
  3347. ctx_isp->substate_activated, apply->request_id);
  3348. ctx_ops = &ctx_isp->substate_machine[ctx_isp->substate_activated];
  3349. if (ctx_ops->crm_ops.apply_req) {
  3350. rc = ctx_ops->crm_ops.apply_req(ctx, apply);
  3351. } else {
  3352. CAM_WARN_RATE_LIMIT(CAM_ISP,
  3353. "No handle function in activated substate %d",
  3354. ctx_isp->substate_activated);
  3355. rc = -EFAULT;
  3356. }
  3357. if (rc)
  3358. CAM_WARN_RATE_LIMIT(CAM_ISP,
  3359. "Apply failed in active substate %d rc %d",
  3360. ctx_isp->substate_activated, rc);
  3361. return rc;
  3362. }
  3363. static int __cam_isp_ctx_handle_irq_in_activated(void *context,
  3364. uint32_t evt_id, void *evt_data)
  3365. {
  3366. int rc = 0;
  3367. struct cam_isp_ctx_irq_ops *irq_ops = NULL;
  3368. struct cam_context *ctx = (struct cam_context *)context;
  3369. struct cam_isp_context *ctx_isp =
  3370. (struct cam_isp_context *)ctx->ctx_priv;
  3371. spin_lock(&ctx->lock);
  3372. trace_cam_isp_activated_irq(ctx, ctx_isp->substate_activated, evt_id,
  3373. __cam_isp_ctx_get_event_ts(evt_id, evt_data));
  3374. CAM_DBG(CAM_ISP, "Enter: State %d, Substate %d, evt id %d",
  3375. ctx->state, ctx_isp->substate_activated, evt_id);
  3376. irq_ops = &ctx_isp->substate_machine_irq[ctx_isp->substate_activated];
  3377. if (irq_ops->irq_ops[evt_id]) {
  3378. rc = irq_ops->irq_ops[evt_id](ctx_isp, evt_data);
  3379. } else {
  3380. CAM_DBG(CAM_ISP, "No handle function for substate %d",
  3381. ctx_isp->substate_activated);
  3382. if (isp_ctx_debug.enable_state_monitor_dump)
  3383. __cam_isp_ctx_dump_state_monitor_array(ctx_isp);
  3384. }
  3385. CAM_DBG(CAM_ISP, "Exit: State %d Substate %d",
  3386. ctx->state, ctx_isp->substate_activated);
  3387. spin_unlock(&ctx->lock);
  3388. return rc;
  3389. }
  3390. /* top state machine */
  3391. static struct cam_ctx_ops
  3392. cam_isp_ctx_top_state_machine[CAM_CTX_STATE_MAX] = {
  3393. /* Uninit */
  3394. {
  3395. .ioctl_ops = {},
  3396. .crm_ops = {},
  3397. .irq_ops = NULL,
  3398. },
  3399. /* Available */
  3400. {
  3401. .ioctl_ops = {
  3402. .acquire_dev = __cam_isp_ctx_acquire_dev_in_available,
  3403. },
  3404. .crm_ops = {},
  3405. .irq_ops = NULL,
  3406. },
  3407. /* Acquired */
  3408. {
  3409. .ioctl_ops = {
  3410. .acquire_hw = __cam_isp_ctx_acquire_hw_in_acquired,
  3411. .release_dev = __cam_isp_ctx_release_dev_in_top_state,
  3412. .config_dev = __cam_isp_ctx_config_dev_in_acquired,
  3413. .release_hw = __cam_isp_ctx_release_hw_in_top_state,
  3414. },
  3415. .crm_ops = {
  3416. .link = __cam_isp_ctx_link_in_acquired,
  3417. .unlink = __cam_isp_ctx_unlink_in_acquired,
  3418. .get_dev_info = __cam_isp_ctx_get_dev_info_in_acquired,
  3419. .flush_req = __cam_isp_ctx_flush_req_in_top_state,
  3420. },
  3421. .irq_ops = NULL,
  3422. .pagefault_ops = cam_isp_context_dump_active_request,
  3423. .dumpinfo_ops = cam_isp_context_info_dump,
  3424. },
  3425. /* Ready */
  3426. {
  3427. .ioctl_ops = {
  3428. .start_dev = __cam_isp_ctx_start_dev_in_ready,
  3429. .release_dev = __cam_isp_ctx_release_dev_in_top_state,
  3430. .config_dev = __cam_isp_ctx_config_dev_in_top_state,
  3431. .release_hw = __cam_isp_ctx_release_hw_in_top_state,
  3432. },
  3433. .crm_ops = {
  3434. .unlink = __cam_isp_ctx_unlink_in_ready,
  3435. .flush_req = __cam_isp_ctx_flush_req_in_ready,
  3436. },
  3437. .irq_ops = NULL,
  3438. .pagefault_ops = cam_isp_context_dump_active_request,
  3439. .dumpinfo_ops = cam_isp_context_info_dump,
  3440. },
  3441. /* Activated */
  3442. {
  3443. .ioctl_ops = {
  3444. .stop_dev = __cam_isp_ctx_stop_dev_in_activated,
  3445. .release_dev = __cam_isp_ctx_release_dev_in_activated,
  3446. .config_dev = __cam_isp_ctx_config_dev_in_top_state,
  3447. .release_hw = __cam_isp_ctx_release_hw_in_activated,
  3448. },
  3449. .crm_ops = {
  3450. .unlink = __cam_isp_ctx_unlink_in_activated,
  3451. .apply_req = __cam_isp_ctx_apply_req,
  3452. .flush_req = __cam_isp_ctx_flush_req_in_top_state,
  3453. .process_evt = __cam_isp_ctx_process_evt,
  3454. },
  3455. .irq_ops = __cam_isp_ctx_handle_irq_in_activated,
  3456. .pagefault_ops = cam_isp_context_dump_active_request,
  3457. .dumpinfo_ops = cam_isp_context_info_dump,
  3458. },
  3459. };
  3460. static int cam_isp_context_dump_active_request(void *data, unsigned long iova,
  3461. uint32_t buf_info)
  3462. {
  3463. struct cam_context *ctx = (struct cam_context *)data;
  3464. struct cam_ctx_request *req = NULL;
  3465. struct cam_ctx_request *req_temp = NULL;
  3466. struct cam_isp_ctx_req *req_isp = NULL;
  3467. struct cam_isp_prepare_hw_update_data *hw_update_data = NULL;
  3468. struct cam_hw_mgr_dump_pf_data *pf_dbg_entry = NULL;
  3469. bool mem_found = false;
  3470. int rc = 0;
  3471. struct cam_isp_context *isp_ctx =
  3472. (struct cam_isp_context *)ctx->ctx_priv;
  3473. if (!isp_ctx) {
  3474. CAM_ERR(CAM_ISP, "Invalid isp ctx");
  3475. return -EINVAL;
  3476. }
  3477. CAM_INFO(CAM_ISP, "iommu fault handler for isp ctx %d state %d",
  3478. ctx->ctx_id, ctx->state);
  3479. list_for_each_entry_safe(req, req_temp,
  3480. &ctx->active_req_list, list) {
  3481. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  3482. hw_update_data = &req_isp->hw_update_data;
  3483. pf_dbg_entry = &(req->pf_data);
  3484. CAM_INFO(CAM_ISP, "req_id : %lld ", req->request_id);
  3485. rc = cam_context_dump_pf_info_to_hw(ctx, pf_dbg_entry->packet,
  3486. iova, buf_info, &mem_found);
  3487. if (rc)
  3488. CAM_ERR(CAM_ISP, "Failed to dump pf info");
  3489. if (mem_found)
  3490. CAM_ERR(CAM_ISP, "Found page fault in req %lld %d",
  3491. req->request_id, rc);
  3492. }
  3493. CAM_INFO(CAM_ISP, "Iterating over wait_list of isp ctx %d state %d",
  3494. ctx->ctx_id, ctx->state);
  3495. list_for_each_entry_safe(req, req_temp,
  3496. &ctx->wait_req_list, list) {
  3497. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  3498. hw_update_data = &req_isp->hw_update_data;
  3499. pf_dbg_entry = &(req->pf_data);
  3500. CAM_INFO(CAM_ISP, "req_id : %lld ", req->request_id);
  3501. rc = cam_context_dump_pf_info_to_hw(ctx, pf_dbg_entry->packet,
  3502. iova, buf_info, &mem_found);
  3503. if (rc)
  3504. CAM_ERR(CAM_ISP, "Failed to dump pf info");
  3505. if (mem_found)
  3506. CAM_ERR(CAM_ISP, "Found page fault in req %lld %d",
  3507. req->request_id, rc);
  3508. }
  3509. /*
  3510. * In certain scenarios we observe both overflow and SMMU pagefault
  3511. * for a particular request. If overflow is handled before page fault
  3512. * we need to traverse through pending request list because if
  3513. * bubble recovery is enabled on any request we move that request
  3514. * and all the subsequent requests to the pending list while handling
  3515. * overflow error.
  3516. */
  3517. CAM_INFO(CAM_ISP,
  3518. "Iterating over pending req list of isp ctx %d state %d",
  3519. ctx->ctx_id, ctx->state);
  3520. list_for_each_entry_safe(req, req_temp,
  3521. &ctx->pending_req_list, list) {
  3522. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  3523. hw_update_data = &req_isp->hw_update_data;
  3524. pf_dbg_entry = &(req->pf_data);
  3525. CAM_INFO(CAM_ISP, "req_id : %lld ", req->request_id);
  3526. rc = cam_context_dump_pf_info_to_hw(ctx, pf_dbg_entry->packet,
  3527. iova, buf_info, &mem_found);
  3528. if (rc)
  3529. CAM_ERR(CAM_ISP, "Failed to dump pf info");
  3530. if (mem_found)
  3531. CAM_ERR(CAM_ISP, "Found page fault in req %lld %d",
  3532. req->request_id, rc);
  3533. }
  3534. return rc;
  3535. }
  3536. static int cam_isp_context_debug_register(void)
  3537. {
  3538. isp_ctx_debug.dentry = debugfs_create_dir("camera_isp_ctx",
  3539. NULL);
  3540. if (!isp_ctx_debug.dentry) {
  3541. CAM_ERR(CAM_ISP, "failed to create dentry");
  3542. return -ENOMEM;
  3543. }
  3544. if (!debugfs_create_u32("enable_state_monitor_dump",
  3545. 0644,
  3546. isp_ctx_debug.dentry,
  3547. &isp_ctx_debug.enable_state_monitor_dump)) {
  3548. CAM_ERR(CAM_ISP, "failed to create enable_state_monitor_dump");
  3549. goto err;
  3550. }
  3551. return 0;
  3552. err:
  3553. debugfs_remove_recursive(isp_ctx_debug.dentry);
  3554. return -ENOMEM;
  3555. }
  3556. int cam_isp_context_init(struct cam_isp_context *ctx,
  3557. struct cam_context *ctx_base,
  3558. struct cam_req_mgr_kmd_ops *crm_node_intf,
  3559. struct cam_hw_mgr_intf *hw_intf,
  3560. uint32_t ctx_id)
  3561. {
  3562. int rc = -1;
  3563. int i;
  3564. if (!ctx || !ctx_base) {
  3565. CAM_ERR(CAM_ISP, "Invalid Context");
  3566. goto err;
  3567. }
  3568. /* ISP context setup */
  3569. memset(ctx, 0, sizeof(*ctx));
  3570. ctx->base = ctx_base;
  3571. ctx->frame_id = 0;
  3572. ctx->active_req_cnt = 0;
  3573. ctx->reported_req_id = 0;
  3574. ctx->hw_ctx = NULL;
  3575. ctx->substate_activated = CAM_ISP_CTX_ACTIVATED_SOF;
  3576. ctx->substate_machine = cam_isp_ctx_activated_state_machine;
  3577. ctx->substate_machine_irq = cam_isp_ctx_activated_state_machine_irq;
  3578. ctx->init_timestamp = jiffies_to_msecs(jiffies);
  3579. for (i = 0; i < CAM_CTX_REQ_MAX; i++) {
  3580. ctx->req_base[i].req_priv = &ctx->req_isp[i];
  3581. ctx->req_isp[i].base = &ctx->req_base[i];
  3582. }
  3583. /* camera context setup */
  3584. rc = cam_context_init(ctx_base, isp_dev_name, CAM_ISP, ctx_id,
  3585. crm_node_intf, hw_intf, ctx->req_base, CAM_CTX_REQ_MAX);
  3586. if (rc) {
  3587. CAM_ERR(CAM_ISP, "Camera Context Base init failed");
  3588. goto err;
  3589. }
  3590. /* link camera context with isp context */
  3591. ctx_base->state_machine = cam_isp_ctx_top_state_machine;
  3592. ctx_base->ctx_priv = ctx;
  3593. /* initializing current state for error logging */
  3594. for (i = 0; i < CAM_ISP_CTX_STATE_MONITOR_MAX_ENTRIES; i++) {
  3595. ctx->cam_isp_ctx_state_monitor[i].curr_state =
  3596. CAM_ISP_CTX_ACTIVATED_MAX;
  3597. }
  3598. atomic64_set(&ctx->state_monitor_head, -1);
  3599. cam_isp_context_debug_register();
  3600. err:
  3601. return rc;
  3602. }
  3603. int cam_isp_context_deinit(struct cam_isp_context *ctx)
  3604. {
  3605. int rc = 0;
  3606. if (ctx->base)
  3607. cam_context_deinit(ctx->base);
  3608. if (ctx->substate_activated != CAM_ISP_CTX_ACTIVATED_SOF)
  3609. CAM_ERR(CAM_ISP, "ISP context substate is invalid");
  3610. memset(ctx, 0, sizeof(*ctx));
  3611. return rc;
  3612. }