cam_isp_context.c 113 KB

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