cam_isp_context.c 148 KB

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