cam_isp_context.c 108 KB

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