cam_isp_context.c 146 KB

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