cam_isp_context.c 107 KB

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