cam_isp_context.c 147 KB

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