cam_isp_context.c 141 KB

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