cvp_hfi.c 114 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2018-2021, The Linux Foundation. All rights reserved.
  4. */
  5. #include <asm/memory.h>
  6. #include <linux/coresight-stm.h>
  7. #include <linux/delay.h>
  8. #include <linux/devfreq.h>
  9. #include <linux/hash.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/io.h>
  12. #include <linux/iommu.h>
  13. #include <linux/iopoll.h>
  14. #include <linux/of.h>
  15. #include <linux/pm_qos.h>
  16. #include <linux/regulator/consumer.h>
  17. #include <linux/slab.h>
  18. #include <linux/workqueue.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/soc/qcom/llcc-qcom.h>
  21. #include <linux/qcom_scm.h>
  22. #include <linux/soc/qcom/smem.h>
  23. #include <linux/dma-mapping.h>
  24. #include <linux/reset.h>
  25. #include "hfi_packetization.h"
  26. #include "msm_cvp_debug.h"
  27. #include "cvp_core_hfi.h"
  28. #include "cvp_hfi_helper.h"
  29. #include "cvp_hfi_io.h"
  30. #include "msm_cvp_dsp.h"
  31. #include "msm_cvp_clocks.h"
  32. #define FIRMWARE_SIZE 0X00A00000
  33. #define REG_ADDR_OFFSET_BITMASK 0x000FFFFF
  34. #define QDSS_IOVA_START 0x80001000
  35. #define MIN_PAYLOAD_SIZE 3
  36. struct cvp_tzbsp_memprot {
  37. u32 cp_start;
  38. u32 cp_size;
  39. u32 cp_nonpixel_start;
  40. u32 cp_nonpixel_size;
  41. };
  42. #define TZBSP_PIL_SET_STATE 0xA
  43. #define TZBSP_CVP_PAS_ID 26
  44. /* Poll interval in uS */
  45. #define POLL_INTERVAL_US 50
  46. enum tzbsp_subsys_state {
  47. TZ_SUBSYS_STATE_SUSPEND = 0,
  48. TZ_SUBSYS_STATE_RESUME = 1,
  49. TZ_SUBSYS_STATE_RESTORE_THRESHOLD = 2,
  50. };
  51. const struct msm_cvp_gov_data CVP_DEFAULT_BUS_VOTE = {
  52. .data = NULL,
  53. .data_count = 0,
  54. };
  55. const int cvp_max_packets = 32;
  56. static void iris_hfi_pm_handler(struct work_struct *work);
  57. static DECLARE_DELAYED_WORK(iris_hfi_pm_work, iris_hfi_pm_handler);
  58. static inline int __resume(struct iris_hfi_device *device);
  59. static inline int __suspend(struct iris_hfi_device *device);
  60. static int __disable_regulator(struct iris_hfi_device *device,
  61. const char *name);
  62. static int __enable_regulator(struct iris_hfi_device *device,
  63. const char *name);
  64. static void __flush_debug_queue(struct iris_hfi_device *device, u8 *packet);
  65. static int __initialize_packetization(struct iris_hfi_device *device);
  66. static struct cvp_hal_session *__get_session(struct iris_hfi_device *device,
  67. u32 session_id);
  68. static bool __is_session_valid(struct iris_hfi_device *device,
  69. struct cvp_hal_session *session, const char *func);
  70. static int __iface_cmdq_write(struct iris_hfi_device *device,
  71. void *pkt);
  72. static int __load_fw(struct iris_hfi_device *device);
  73. static void __unload_fw(struct iris_hfi_device *device);
  74. static int __tzbsp_set_cvp_state(enum tzbsp_subsys_state state);
  75. static int __enable_subcaches(struct iris_hfi_device *device);
  76. static int __set_subcaches(struct iris_hfi_device *device);
  77. static int __release_subcaches(struct iris_hfi_device *device);
  78. static int __disable_subcaches(struct iris_hfi_device *device);
  79. static int __power_collapse(struct iris_hfi_device *device, bool force);
  80. static int iris_hfi_noc_error_info(void *dev);
  81. static void interrupt_init_iris2(struct iris_hfi_device *device);
  82. static void setup_dsp_uc_memmap_vpu5(struct iris_hfi_device *device);
  83. static void clock_config_on_enable_vpu5(struct iris_hfi_device *device);
  84. static int reset_ahb2axi_bridge(struct iris_hfi_device *device);
  85. static void power_off_iris2(struct iris_hfi_device *device);
  86. static int __set_ubwc_config(struct iris_hfi_device *device);
  87. static void __noc_error_info_iris2(struct iris_hfi_device *device);
  88. static int __enable_hw_power_collapse(struct iris_hfi_device *device);
  89. static int __power_off_controller(struct iris_hfi_device *device);
  90. static struct iris_hfi_vpu_ops iris2_ops = {
  91. .interrupt_init = interrupt_init_iris2,
  92. .setup_dsp_uc_memmap = setup_dsp_uc_memmap_vpu5,
  93. .clock_config_on_enable = clock_config_on_enable_vpu5,
  94. .reset_ahb2axi_bridge = reset_ahb2axi_bridge,
  95. .power_off = power_off_iris2,
  96. .noc_error_info = __noc_error_info_iris2,
  97. };
  98. /**
  99. * Utility function to enforce some of our assumptions. Spam calls to this
  100. * in hotspots in code to double check some of the assumptions that we hold.
  101. */
  102. static inline void __strict_check(struct iris_hfi_device *device)
  103. {
  104. msm_cvp_res_handle_fatal_hw_error(device->res,
  105. !mutex_is_locked(&device->lock));
  106. }
  107. static inline void __set_state(struct iris_hfi_device *device,
  108. enum iris_hfi_state state)
  109. {
  110. device->state = state;
  111. }
  112. static inline bool __core_in_valid_state(struct iris_hfi_device *device)
  113. {
  114. return device->state != IRIS_STATE_DEINIT;
  115. }
  116. static inline bool is_sys_cache_present(struct iris_hfi_device *device)
  117. {
  118. return device->res->sys_cache_present;
  119. }
  120. #define ROW_SIZE 32
  121. int get_hfi_version(void)
  122. {
  123. struct msm_cvp_core *core;
  124. struct iris_hfi_device *hfi;
  125. core = list_first_entry(&cvp_driver->cores, struct msm_cvp_core, list);
  126. hfi = (struct iris_hfi_device *)core->device->hfi_device_data;
  127. return hfi->version;
  128. }
  129. unsigned int get_msg_size(struct cvp_hfi_msg_session_hdr *hdr)
  130. {
  131. struct msm_cvp_core *core;
  132. struct iris_hfi_device *device;
  133. u32 minor_ver;
  134. core = list_first_entry(&cvp_driver->cores, struct msm_cvp_core, list);
  135. if (core)
  136. device = core->device->hfi_device_data;
  137. else
  138. return 0;
  139. if (!device) {
  140. dprintk(CVP_ERR, "%s: NULL device\n", __func__);
  141. return 0;
  142. }
  143. minor_ver = (device->version & HFI_VERSION_MINOR_MASK) >>
  144. HFI_VERSION_MINOR_SHIFT;
  145. if (minor_ver < 2)
  146. return sizeof(struct cvp_hfi_msg_session_hdr);
  147. if (hdr->packet_type == HFI_MSG_SESSION_CVP_FD)
  148. return sizeof(struct cvp_hfi_msg_session_hdr_ext);
  149. else
  150. return sizeof(struct cvp_hfi_msg_session_hdr);
  151. }
  152. unsigned int get_msg_session_id(void *msg)
  153. {
  154. struct cvp_hfi_msg_session_hdr *hdr =
  155. (struct cvp_hfi_msg_session_hdr *)msg;
  156. return hdr->session_id;
  157. }
  158. unsigned int get_msg_errorcode(void *msg)
  159. {
  160. struct cvp_hfi_msg_session_hdr *hdr =
  161. (struct cvp_hfi_msg_session_hdr *)msg;
  162. return hdr->error_type;
  163. }
  164. int get_msg_opconfigs(void *msg, unsigned int *session_id,
  165. unsigned int *error_type, unsigned int *config_id)
  166. {
  167. struct cvp_hfi_msg_session_op_cfg_packet *cfg =
  168. (struct cvp_hfi_msg_session_op_cfg_packet *)msg;
  169. *session_id = cfg->session_id;
  170. *error_type = cfg->error_type;
  171. *config_id = cfg->op_conf_id;
  172. return 0;
  173. }
  174. static void __dump_packet(u8 *packet, enum cvp_msg_prio log_level)
  175. {
  176. u32 c = 0, packet_size = *(u32 *)packet;
  177. /*
  178. * row must contain enough for 0xdeadbaad * 8 to be converted into
  179. * "de ad ba ab " * 8 + '\0'
  180. */
  181. char row[3 * ROW_SIZE];
  182. for (c = 0; c * ROW_SIZE < packet_size; ++c) {
  183. int bytes_to_read = ((c + 1) * ROW_SIZE > packet_size) ?
  184. packet_size % ROW_SIZE : ROW_SIZE;
  185. hex_dump_to_buffer(packet + c * ROW_SIZE, bytes_to_read,
  186. ROW_SIZE, 4, row, sizeof(row), false);
  187. dprintk(log_level, "%s\n", row);
  188. }
  189. }
  190. static int __dsp_suspend(struct iris_hfi_device *device, bool force, u32 flags)
  191. {
  192. int rc;
  193. struct cvp_hal_session *temp;
  194. if (msm_cvp_dsp_disable)
  195. return 0;
  196. list_for_each_entry(temp, &device->sess_head, list) {
  197. /* if forceful suspend, don't check session pause info */
  198. if (force)
  199. continue;
  200. /* don't suspend if cvp session is not paused */
  201. if (!(temp->flags & SESSION_PAUSE)) {
  202. dprintk(CVP_DSP,
  203. "%s: cvp session %x not paused\n",
  204. __func__, hash32_ptr(temp));
  205. return -EBUSY;
  206. }
  207. }
  208. dprintk(CVP_DSP, "%s: suspend dsp\n", __func__);
  209. rc = cvp_dsp_suspend(flags);
  210. if (rc) {
  211. dprintk(CVP_ERR, "%s: dsp suspend failed with error %d\n",
  212. __func__, rc);
  213. return -EINVAL;
  214. }
  215. dprintk(CVP_DSP, "%s: dsp suspended\n", __func__);
  216. return 0;
  217. }
  218. static int __dsp_resume(struct iris_hfi_device *device, u32 flags)
  219. {
  220. int rc;
  221. if (msm_cvp_dsp_disable)
  222. return 0;
  223. dprintk(CVP_DSP, "%s: resume dsp\n", __func__);
  224. rc = cvp_dsp_resume(flags);
  225. if (rc) {
  226. dprintk(CVP_ERR,
  227. "%s: dsp resume failed with error %d\n",
  228. __func__, rc);
  229. return rc;
  230. }
  231. dprintk(CVP_DSP, "%s: dsp resumed\n", __func__);
  232. return rc;
  233. }
  234. static int __dsp_shutdown(struct iris_hfi_device *device, u32 flags)
  235. {
  236. int rc;
  237. if (msm_cvp_dsp_disable)
  238. return 0;
  239. dprintk(CVP_DSP, "%s: shutdown dsp\n", __func__);
  240. rc = cvp_dsp_shutdown(flags);
  241. if (rc) {
  242. dprintk(CVP_ERR,
  243. "%s: dsp shutdown failed with error %d\n",
  244. __func__, rc);
  245. WARN_ON(1);
  246. }
  247. dprintk(CVP_DSP, "%s: dsp shutdown successful\n", __func__);
  248. return rc;
  249. }
  250. static int __acquire_regulator(struct regulator_info *rinfo,
  251. struct iris_hfi_device *device)
  252. {
  253. int rc = 0;
  254. if (rinfo->has_hw_power_collapse) {
  255. rc = regulator_set_mode(rinfo->regulator,
  256. REGULATOR_MODE_NORMAL);
  257. if (rc) {
  258. /*
  259. * This is somewhat fatal, but nothing we can do
  260. * about it. We can't disable the regulator w/o
  261. * getting it back under s/w control
  262. */
  263. dprintk(CVP_WARN,
  264. "Failed to acquire regulator control: %s\n",
  265. rinfo->name);
  266. } else {
  267. dprintk(CVP_PWR,
  268. "Acquire regulator control from HW: %s\n",
  269. rinfo->name);
  270. }
  271. }
  272. if (!regulator_is_enabled(rinfo->regulator)) {
  273. dprintk(CVP_WARN, "Regulator is not enabled %s\n",
  274. rinfo->name);
  275. msm_cvp_res_handle_fatal_hw_error(device->res, true);
  276. }
  277. return rc;
  278. }
  279. static int __hand_off_regulator(struct regulator_info *rinfo)
  280. {
  281. int rc = 0;
  282. if (rinfo->has_hw_power_collapse) {
  283. rc = regulator_set_mode(rinfo->regulator,
  284. REGULATOR_MODE_FAST);
  285. if (rc) {
  286. dprintk(CVP_WARN,
  287. "Failed to hand off regulator control: %s\n",
  288. rinfo->name);
  289. } else {
  290. dprintk(CVP_PWR,
  291. "Hand off regulator control to HW: %s\n",
  292. rinfo->name);
  293. }
  294. }
  295. return rc;
  296. }
  297. static int __hand_off_regulators(struct iris_hfi_device *device)
  298. {
  299. struct regulator_info *rinfo;
  300. int rc = 0, c = 0;
  301. iris_hfi_for_each_regulator(device, rinfo) {
  302. rc = __hand_off_regulator(rinfo);
  303. /*
  304. * If one regulator hand off failed, driver should take
  305. * the control for other regulators back.
  306. */
  307. if (rc)
  308. goto err_reg_handoff_failed;
  309. c++;
  310. }
  311. return rc;
  312. err_reg_handoff_failed:
  313. iris_hfi_for_each_regulator_reverse_continue(device, rinfo, c)
  314. __acquire_regulator(rinfo, device);
  315. return rc;
  316. }
  317. static int __write_queue(struct cvp_iface_q_info *qinfo, u8 *packet,
  318. bool *rx_req_is_set)
  319. {
  320. struct cvp_hfi_queue_header *queue;
  321. u32 packet_size_in_words, new_write_idx;
  322. u32 empty_space, read_idx, write_idx;
  323. u32 *write_ptr;
  324. if (!qinfo || !packet) {
  325. dprintk(CVP_ERR, "Invalid Params\n");
  326. return -EINVAL;
  327. } else if (!qinfo->q_array.align_virtual_addr) {
  328. dprintk(CVP_WARN, "Queues have already been freed\n");
  329. return -EINVAL;
  330. }
  331. queue = (struct cvp_hfi_queue_header *) qinfo->q_hdr;
  332. if (!queue) {
  333. dprintk(CVP_ERR, "queue not present\n");
  334. return -ENOENT;
  335. }
  336. if (msm_cvp_debug & CVP_PKT) {
  337. dprintk(CVP_PKT, "%s: %pK\n", __func__, qinfo);
  338. __dump_packet(packet, CVP_PKT);
  339. }
  340. packet_size_in_words = (*(u32 *)packet) >> 2;
  341. if (!packet_size_in_words || packet_size_in_words >
  342. qinfo->q_array.mem_size>>2) {
  343. dprintk(CVP_ERR, "Invalid packet size\n");
  344. return -ENODATA;
  345. }
  346. spin_lock(&qinfo->hfi_lock);
  347. read_idx = queue->qhdr_read_idx;
  348. write_idx = queue->qhdr_write_idx;
  349. empty_space = (write_idx >= read_idx) ?
  350. ((qinfo->q_array.mem_size>>2) - (write_idx - read_idx)) :
  351. (read_idx - write_idx);
  352. if (empty_space <= packet_size_in_words) {
  353. queue->qhdr_tx_req = 1;
  354. spin_unlock(&qinfo->hfi_lock);
  355. dprintk(CVP_ERR, "Insufficient size (%d) to write (%d)\n",
  356. empty_space, packet_size_in_words);
  357. return -ENOTEMPTY;
  358. }
  359. queue->qhdr_tx_req = 0;
  360. new_write_idx = write_idx + packet_size_in_words;
  361. write_ptr = (u32 *)((qinfo->q_array.align_virtual_addr) +
  362. (write_idx << 2));
  363. if (write_ptr < (u32 *)qinfo->q_array.align_virtual_addr ||
  364. write_ptr > (u32 *)(qinfo->q_array.align_virtual_addr +
  365. qinfo->q_array.mem_size)) {
  366. spin_unlock(&qinfo->hfi_lock);
  367. dprintk(CVP_ERR, "Invalid write index\n");
  368. return -ENODATA;
  369. }
  370. if (new_write_idx < (qinfo->q_array.mem_size >> 2)) {
  371. memcpy(write_ptr, packet, packet_size_in_words << 2);
  372. } else {
  373. new_write_idx -= qinfo->q_array.mem_size >> 2;
  374. memcpy(write_ptr, packet, (packet_size_in_words -
  375. new_write_idx) << 2);
  376. memcpy((void *)qinfo->q_array.align_virtual_addr,
  377. packet + ((packet_size_in_words - new_write_idx) << 2),
  378. new_write_idx << 2);
  379. }
  380. /*
  381. * Memory barrier to make sure packet is written before updating the
  382. * write index
  383. */
  384. mb();
  385. queue->qhdr_write_idx = new_write_idx;
  386. if (rx_req_is_set)
  387. *rx_req_is_set = queue->qhdr_rx_req == 1;
  388. /*
  389. * Memory barrier to make sure write index is updated before an
  390. * interrupt is raised.
  391. */
  392. mb();
  393. spin_unlock(&qinfo->hfi_lock);
  394. return 0;
  395. }
  396. static int __read_queue(struct cvp_iface_q_info *qinfo, u8 *packet,
  397. u32 *pb_tx_req_is_set)
  398. {
  399. struct cvp_hfi_queue_header *queue;
  400. u32 packet_size_in_words, new_read_idx;
  401. u32 *read_ptr;
  402. u32 receive_request = 0;
  403. u32 read_idx, write_idx;
  404. int rc = 0;
  405. if (!qinfo || !packet || !pb_tx_req_is_set) {
  406. dprintk(CVP_ERR, "Invalid Params\n");
  407. return -EINVAL;
  408. } else if (!qinfo->q_array.align_virtual_addr) {
  409. dprintk(CVP_WARN, "Queues have already been freed\n");
  410. return -EINVAL;
  411. }
  412. /*
  413. * Memory barrier to make sure data is valid before
  414. *reading it
  415. */
  416. mb();
  417. queue = (struct cvp_hfi_queue_header *) qinfo->q_hdr;
  418. if (!queue) {
  419. dprintk(CVP_ERR, "Queue memory is not allocated\n");
  420. return -ENOMEM;
  421. }
  422. /*
  423. * Do not set receive request for debug queue, if set,
  424. * Iris generates interrupt for debug messages even
  425. * when there is no response message available.
  426. * In general debug queue will not become full as it
  427. * is being emptied out for every interrupt from Iris.
  428. * Iris will anyway generates interrupt if it is full.
  429. */
  430. spin_lock(&qinfo->hfi_lock);
  431. if (queue->qhdr_type & HFI_Q_ID_CTRL_TO_HOST_MSG_Q)
  432. receive_request = 1;
  433. read_idx = queue->qhdr_read_idx;
  434. write_idx = queue->qhdr_write_idx;
  435. if (read_idx == write_idx) {
  436. queue->qhdr_rx_req = receive_request;
  437. /*
  438. * mb() to ensure qhdr is updated in main memory
  439. * so that iris reads the updated header values
  440. */
  441. mb();
  442. *pb_tx_req_is_set = 0;
  443. if (write_idx != queue->qhdr_write_idx) {
  444. queue->qhdr_rx_req = 0;
  445. } else {
  446. spin_unlock(&qinfo->hfi_lock);
  447. dprintk(CVP_HFI,
  448. "%s queue is empty, rx_req = %u, tx_req = %u, read_idx = %u\n",
  449. receive_request ? "message" : "debug",
  450. queue->qhdr_rx_req, queue->qhdr_tx_req,
  451. queue->qhdr_read_idx);
  452. return -ENODATA;
  453. }
  454. }
  455. read_ptr = (u32 *)((qinfo->q_array.align_virtual_addr) +
  456. (read_idx << 2));
  457. if (read_ptr < (u32 *)qinfo->q_array.align_virtual_addr ||
  458. read_ptr > (u32 *)(qinfo->q_array.align_virtual_addr +
  459. qinfo->q_array.mem_size - sizeof(*read_ptr))) {
  460. spin_unlock(&qinfo->hfi_lock);
  461. dprintk(CVP_ERR, "Invalid read index\n");
  462. return -ENODATA;
  463. }
  464. packet_size_in_words = (*read_ptr) >> 2;
  465. if (!packet_size_in_words) {
  466. spin_unlock(&qinfo->hfi_lock);
  467. dprintk(CVP_ERR, "Zero packet size\n");
  468. return -ENODATA;
  469. }
  470. new_read_idx = read_idx + packet_size_in_words;
  471. if (((packet_size_in_words << 2) <= CVP_IFACEQ_VAR_HUGE_PKT_SIZE)
  472. && read_idx <= (qinfo->q_array.mem_size >> 2)) {
  473. if (new_read_idx < (qinfo->q_array.mem_size >> 2)) {
  474. memcpy(packet, read_ptr,
  475. packet_size_in_words << 2);
  476. } else {
  477. new_read_idx -= (qinfo->q_array.mem_size >> 2);
  478. memcpy(packet, read_ptr,
  479. (packet_size_in_words - new_read_idx) << 2);
  480. memcpy(packet + ((packet_size_in_words -
  481. new_read_idx) << 2),
  482. (u8 *)qinfo->q_array.align_virtual_addr,
  483. new_read_idx << 2);
  484. }
  485. } else {
  486. dprintk(CVP_WARN,
  487. "BAD packet received, read_idx: %#x, pkt_size: %d\n",
  488. read_idx, packet_size_in_words << 2);
  489. dprintk(CVP_WARN, "Dropping this packet\n");
  490. new_read_idx = write_idx;
  491. rc = -ENODATA;
  492. }
  493. if (new_read_idx != queue->qhdr_write_idx)
  494. queue->qhdr_rx_req = 0;
  495. else
  496. queue->qhdr_rx_req = receive_request;
  497. queue->qhdr_read_idx = new_read_idx;
  498. /*
  499. * mb() to ensure qhdr is updated in main memory
  500. * so that iris reads the updated header values
  501. */
  502. mb();
  503. *pb_tx_req_is_set = (queue->qhdr_tx_req == 1) ? 1 : 0;
  504. spin_unlock(&qinfo->hfi_lock);
  505. if ((msm_cvp_debug & CVP_PKT) &&
  506. !(queue->qhdr_type & HFI_Q_ID_CTRL_TO_HOST_DEBUG_Q)) {
  507. dprintk(CVP_PKT, "%s: %pK\n", __func__, qinfo);
  508. __dump_packet(packet, CVP_PKT);
  509. }
  510. return rc;
  511. }
  512. static int __smem_alloc(struct iris_hfi_device *dev, struct cvp_mem_addr *mem,
  513. u32 size, u32 align, u32 flags)
  514. {
  515. struct msm_cvp_smem *alloc = &mem->mem_data;
  516. int rc = 0;
  517. if (!dev || !mem || !size) {
  518. dprintk(CVP_ERR, "Invalid Params\n");
  519. return -EINVAL;
  520. }
  521. dprintk(CVP_INFO, "start to alloc size: %d, flags: %d\n", size, flags);
  522. alloc->flags = flags;
  523. rc = msm_cvp_smem_alloc(size, align, 1, (void *)dev->res, alloc);
  524. if (rc) {
  525. dprintk(CVP_ERR, "Alloc failed\n");
  526. rc = -ENOMEM;
  527. goto fail_smem_alloc;
  528. }
  529. dprintk(CVP_MEM, "%s: ptr = %pK, size = %d\n", __func__,
  530. alloc->kvaddr, size);
  531. mem->mem_size = alloc->size;
  532. mem->align_virtual_addr = alloc->kvaddr;
  533. mem->align_device_addr = alloc->device_addr;
  534. return rc;
  535. fail_smem_alloc:
  536. return rc;
  537. }
  538. static void __smem_free(struct iris_hfi_device *dev, struct msm_cvp_smem *mem)
  539. {
  540. if (!dev || !mem) {
  541. dprintk(CVP_ERR, "invalid param %pK %pK\n", dev, mem);
  542. return;
  543. }
  544. msm_cvp_smem_free(mem);
  545. }
  546. static void __write_register(struct iris_hfi_device *device,
  547. u32 reg, u32 value)
  548. {
  549. u32 hwiosymaddr = reg;
  550. u8 *base_addr;
  551. if (!device) {
  552. dprintk(CVP_ERR, "Invalid params: %pK\n", device);
  553. return;
  554. }
  555. __strict_check(device);
  556. if (!device->power_enabled) {
  557. dprintk(CVP_WARN,
  558. "HFI Write register failed : Power is OFF\n");
  559. msm_cvp_res_handle_fatal_hw_error(device->res, true);
  560. return;
  561. }
  562. base_addr = device->cvp_hal_data->register_base;
  563. dprintk(CVP_REG, "Base addr: %pK, written to: %#x, Value: %#x...\n",
  564. base_addr, hwiosymaddr, value);
  565. base_addr += hwiosymaddr;
  566. writel_relaxed(value, base_addr);
  567. /*
  568. * Memory barrier to make sure value is written into the register.
  569. */
  570. wmb();
  571. }
  572. static int __read_gcc_register(struct iris_hfi_device *device, u32 reg)
  573. {
  574. int rc = 0;
  575. u8 *base_addr;
  576. if (!device) {
  577. dprintk(CVP_ERR, "Invalid params: %pK\n", device);
  578. return -EINVAL;
  579. }
  580. __strict_check(device);
  581. if (!device->power_enabled) {
  582. dprintk(CVP_WARN,
  583. "%s HFI Read register failed : Power is OFF\n",
  584. __func__);
  585. msm_cvp_res_handle_fatal_hw_error(device->res, true);
  586. return -EINVAL;
  587. }
  588. base_addr = device->cvp_hal_data->gcc_reg_base;
  589. rc = readl_relaxed(base_addr + reg);
  590. /*
  591. * Memory barrier to make sure value is read correctly from the
  592. * register.
  593. */
  594. rmb();
  595. dprintk(CVP_REG,
  596. "GCC Base addr: %pK, read from: %#x, value: %#x...\n",
  597. base_addr, reg, rc);
  598. return rc;
  599. }
  600. static int __read_register(struct iris_hfi_device *device, u32 reg)
  601. {
  602. int rc = 0;
  603. u8 *base_addr;
  604. if (!device) {
  605. dprintk(CVP_ERR, "Invalid params: %pK\n", device);
  606. return -EINVAL;
  607. }
  608. __strict_check(device);
  609. if (!device->power_enabled) {
  610. dprintk(CVP_WARN,
  611. "HFI Read register failed : Power is OFF\n");
  612. msm_cvp_res_handle_fatal_hw_error(device->res, true);
  613. return -EINVAL;
  614. }
  615. base_addr = device->cvp_hal_data->register_base;
  616. rc = readl_relaxed(base_addr + reg);
  617. /*
  618. * Memory barrier to make sure value is read correctly from the
  619. * register.
  620. */
  621. rmb();
  622. dprintk(CVP_REG, "Base addr: %pK, read from: %#x, value: %#x...\n",
  623. base_addr, reg, rc);
  624. return rc;
  625. }
  626. static void __set_registers(struct iris_hfi_device *device)
  627. {
  628. struct reg_set *reg_set;
  629. int i;
  630. if (!device->res) {
  631. dprintk(CVP_ERR,
  632. "device resources null, cannot set registers\n");
  633. return;
  634. }
  635. reg_set = &device->res->reg_set;
  636. for (i = 0; i < reg_set->count; i++) {
  637. __write_register(device, reg_set->reg_tbl[i].reg,
  638. reg_set->reg_tbl[i].value);
  639. dprintk(CVP_REG, "write_reg offset=%x, val=%x\n",
  640. reg_set->reg_tbl[i].reg,
  641. reg_set->reg_tbl[i].value);
  642. }
  643. }
  644. /*
  645. * The existence of this function is a hack for 8996 (or certain Iris versions)
  646. * to overcome a hardware bug. Whenever the GDSCs momentarily power collapse
  647. * (after calling __hand_off_regulators()), the values of the threshold
  648. * registers (typically programmed by TZ) are incorrectly reset. As a result
  649. * reprogram these registers at certain agreed upon points.
  650. */
  651. static void __set_threshold_registers(struct iris_hfi_device *device)
  652. {
  653. u32 version = __read_register(device, CVP_WRAPPER_HW_VERSION);
  654. version &= ~GENMASK(15, 0);
  655. if (version != (0x3 << 28 | 0x43 << 16))
  656. return;
  657. if (__tzbsp_set_cvp_state(TZ_SUBSYS_STATE_RESTORE_THRESHOLD))
  658. dprintk(CVP_ERR, "Failed to restore threshold values\n");
  659. }
  660. static int __unvote_buses(struct iris_hfi_device *device)
  661. {
  662. int rc = 0;
  663. struct bus_info *bus = NULL;
  664. kfree(device->bus_vote.data);
  665. device->bus_vote.data = NULL;
  666. device->bus_vote.data_count = 0;
  667. iris_hfi_for_each_bus(device, bus) {
  668. rc = icc_set_bw(bus->client, 0, 0);
  669. if (rc) {
  670. dprintk(CVP_ERR,
  671. "%s: Failed unvoting bus\n", __func__);
  672. goto err_unknown_device;
  673. }
  674. }
  675. err_unknown_device:
  676. return rc;
  677. }
  678. static int __vote_buses(struct iris_hfi_device *device,
  679. struct cvp_bus_vote_data *data, int num_data)
  680. {
  681. int rc = 0;
  682. struct bus_info *bus = NULL;
  683. struct cvp_bus_vote_data *new_data = NULL;
  684. if (!num_data) {
  685. dprintk(CVP_PWR, "No vote data available\n");
  686. goto no_data_count;
  687. } else if (!data) {
  688. dprintk(CVP_ERR, "Invalid voting data\n");
  689. return -EINVAL;
  690. }
  691. new_data = kmemdup(data, num_data * sizeof(*new_data), GFP_KERNEL);
  692. if (!new_data) {
  693. dprintk(CVP_ERR, "Can't alloc memory to cache bus votes\n");
  694. rc = -ENOMEM;
  695. goto err_no_mem;
  696. }
  697. no_data_count:
  698. kfree(device->bus_vote.data);
  699. device->bus_vote.data = new_data;
  700. device->bus_vote.data_count = num_data;
  701. iris_hfi_for_each_bus(device, bus) {
  702. if (bus) {
  703. rc = icc_set_bw(bus->client, bus->range[1], 0);
  704. if (rc)
  705. dprintk(CVP_ERR,
  706. "Failed voting bus %s to ab %u\n",
  707. bus->name, bus->range[1]*1000);
  708. }
  709. }
  710. err_no_mem:
  711. return rc;
  712. }
  713. static int iris_hfi_vote_buses(void *dev, struct cvp_bus_vote_data *d, int n)
  714. {
  715. int rc = 0;
  716. struct iris_hfi_device *device = dev;
  717. if (!device)
  718. return -EINVAL;
  719. mutex_lock(&device->lock);
  720. rc = __vote_buses(device, d, n);
  721. mutex_unlock(&device->lock);
  722. return rc;
  723. }
  724. static int __core_set_resource(struct iris_hfi_device *device,
  725. struct cvp_resource_hdr *resource_hdr, void *resource_value)
  726. {
  727. struct cvp_hfi_cmd_sys_set_resource_packet *pkt;
  728. u8 packet[CVP_IFACEQ_VAR_SMALL_PKT_SIZE];
  729. int rc = 0;
  730. if (!device || !resource_hdr || !resource_value) {
  731. dprintk(CVP_ERR, "set_res: Invalid Params\n");
  732. return -EINVAL;
  733. }
  734. pkt = (struct cvp_hfi_cmd_sys_set_resource_packet *) packet;
  735. rc = call_hfi_pkt_op(device, sys_set_resource,
  736. pkt, resource_hdr, resource_value);
  737. if (rc) {
  738. dprintk(CVP_ERR, "set_res: failed to create packet\n");
  739. goto err_create_pkt;
  740. }
  741. rc = __iface_cmdq_write(device, pkt);
  742. if (rc)
  743. rc = -ENOTEMPTY;
  744. err_create_pkt:
  745. return rc;
  746. }
  747. static int __core_release_resource(struct iris_hfi_device *device,
  748. struct cvp_resource_hdr *resource_hdr)
  749. {
  750. struct cvp_hfi_cmd_sys_release_resource_packet *pkt;
  751. u8 packet[CVP_IFACEQ_VAR_SMALL_PKT_SIZE];
  752. int rc = 0;
  753. if (!device || !resource_hdr) {
  754. dprintk(CVP_ERR, "release_res: Invalid Params\n");
  755. return -EINVAL;
  756. }
  757. pkt = (struct cvp_hfi_cmd_sys_release_resource_packet *) packet;
  758. rc = call_hfi_pkt_op(device, sys_release_resource,
  759. pkt, resource_hdr);
  760. if (rc) {
  761. dprintk(CVP_ERR, "release_res: failed to create packet\n");
  762. goto err_create_pkt;
  763. }
  764. rc = __iface_cmdq_write(device, pkt);
  765. if (rc)
  766. rc = -ENOTEMPTY;
  767. err_create_pkt:
  768. return rc;
  769. }
  770. static int __tzbsp_set_cvp_state(enum tzbsp_subsys_state state)
  771. {
  772. int rc = 0;
  773. rc = qcom_scm_set_remote_state(state, TZBSP_CVP_PAS_ID);
  774. dprintk(CVP_CORE, "Set state %d, resp %d\n", state, rc);
  775. if (rc) {
  776. dprintk(CVP_ERR, "Failed qcom_scm_set_remote_state %d\n", rc);
  777. return rc;
  778. }
  779. return 0;
  780. }
  781. static inline int __boot_firmware(struct iris_hfi_device *device)
  782. {
  783. int rc = 0, loop = 10;
  784. u32 ctrl_init_val = 0, ctrl_status = 0, count = 0, max_tries = 1000;
  785. u32 reg_gdsc;
  786. /*
  787. * Hand off control of regulators to h/w _after_ enabling clocks.
  788. * Note that the GDSC will turn off when switching from normal
  789. * (s/w triggered) to fast (HW triggered) unless the h/w vote is
  790. * present. Since Iris isn't up yet, the GDSC will be off briefly.
  791. */
  792. if (__enable_hw_power_collapse(device))
  793. dprintk(CVP_ERR, "Failed to enabled inter-frame PC\n");
  794. while (loop) {
  795. reg_gdsc = __read_register(device, CVP_CC_MVS1_GDSCR);
  796. if (reg_gdsc & 0x80000000) {
  797. usleep_range(100, 200);
  798. loop--;
  799. } else {
  800. break;
  801. }
  802. }
  803. if (!loop)
  804. dprintk(CVP_ERR, "fail to power off CORE during resume\n");
  805. ctrl_init_val = BIT(0);
  806. __write_register(device, CVP_CTRL_INIT, ctrl_init_val);
  807. while (!ctrl_status && count < max_tries) {
  808. ctrl_status = __read_register(device, CVP_CTRL_STATUS);
  809. if ((ctrl_status & CVP_CTRL_ERROR_STATUS__M) == 0x4) {
  810. dprintk(CVP_ERR, "invalid setting for UC_REGION\n");
  811. rc = -ENODATA;
  812. break;
  813. }
  814. /* Reduce to 1/100th and x100 of max_tries */
  815. usleep_range(500, 1000);
  816. count++;
  817. }
  818. if (!(ctrl_status & CVP_CTRL_INIT_STATUS__M)) {
  819. dprintk(CVP_ERR, "Failed to boot FW status: %x\n",
  820. ctrl_status);
  821. rc = -ENODEV;
  822. }
  823. /* Enable interrupt before sending commands to tensilica */
  824. __write_register(device, CVP_CPU_CS_H2XSOFTINTEN, 0x1);
  825. __write_register(device, CVP_CPU_CS_X2RPMh, 0x0);
  826. return rc;
  827. }
  828. static int iris_hfi_resume(void *dev)
  829. {
  830. int rc = 0;
  831. struct iris_hfi_device *device = (struct iris_hfi_device *) dev;
  832. if (!device) {
  833. dprintk(CVP_ERR, "%s invalid device\n", __func__);
  834. return -EINVAL;
  835. }
  836. dprintk(CVP_CORE, "Resuming Iris\n");
  837. mutex_lock(&device->lock);
  838. rc = __resume(device);
  839. mutex_unlock(&device->lock);
  840. return rc;
  841. }
  842. static int iris_hfi_suspend(void *dev)
  843. {
  844. int rc = 0;
  845. struct iris_hfi_device *device = (struct iris_hfi_device *) dev;
  846. if (!device) {
  847. dprintk(CVP_ERR, "%s invalid device\n", __func__);
  848. return -EINVAL;
  849. } else if (!device->res->sw_power_collapsible) {
  850. return -ENOTSUPP;
  851. }
  852. dprintk(CVP_CORE, "Suspending Iris\n");
  853. mutex_lock(&device->lock);
  854. rc = __power_collapse(device, true);
  855. if (rc) {
  856. dprintk(CVP_WARN, "%s: Iris is busy\n", __func__);
  857. rc = -EBUSY;
  858. }
  859. mutex_unlock(&device->lock);
  860. /* Cancel pending delayed works if any */
  861. if (!rc)
  862. cancel_delayed_work(&iris_hfi_pm_work);
  863. return rc;
  864. }
  865. static void cvp_dump_csr(struct iris_hfi_device *dev)
  866. {
  867. u32 reg;
  868. if (!dev)
  869. return;
  870. if (!dev->power_enabled || dev->reg_dumped)
  871. return;
  872. reg = __read_register(dev, CVP_WRAPPER_CPU_STATUS);
  873. dprintk(CVP_ERR, "CVP_WRAPPER_CPU_STATUS: %x\n", reg);
  874. reg = __read_register(dev, CVP_CPU_CS_SCIACMDARG0);
  875. dprintk(CVP_ERR, "CVP_CPU_CS_SCIACMDARG0: %x\n", reg);
  876. reg = __read_register(dev, CVP_WRAPPER_CPU_CLOCK_CONFIG);
  877. dprintk(CVP_ERR, "CVP_WRAPPER_CPU_CLOCK_CONFIG: %x\n", reg);
  878. reg = __read_register(dev, CVP_WRAPPER_CORE_CLOCK_CONFIG);
  879. dprintk(CVP_ERR, "CVP_WRAPPER_CORE_CLOCK_CONFIG: %x\n", reg);
  880. reg = __read_register(dev, CVP_WRAPPER_INTR_STATUS);
  881. dprintk(CVP_ERR, "CVP_WRAPPER_INTR_STATUS: %x\n", reg);
  882. reg = __read_register(dev, CVP_CPU_CS_H2ASOFTINT);
  883. dprintk(CVP_ERR, "CVP_CPU_CS_H2ASOFTINT: %x\n", reg);
  884. reg = __read_register(dev, CVP_CPU_CS_A2HSOFTINT);
  885. dprintk(CVP_ERR, "CVP_CPU_CS_A2HSOFTINT: %x\n", reg);
  886. reg = __read_register(dev, CVP_CC_MVS1C_GDSCR);
  887. dprintk(CVP_ERR, "CVP_CC_MVS1C_GDSCR: %x\n", reg);
  888. reg = __read_register(dev, CVP_CC_MVS1C_CBCR);
  889. dprintk(CVP_ERR, "CVP_CC_MVS1C_CBCR: %x\n", reg);
  890. dev->reg_dumped = true;
  891. }
  892. static int iris_hfi_flush_debug_queue(void *dev)
  893. {
  894. int rc = 0;
  895. struct iris_hfi_device *device = (struct iris_hfi_device *) dev;
  896. if (!device) {
  897. dprintk(CVP_ERR, "%s invalid device\n", __func__);
  898. return -EINVAL;
  899. }
  900. cvp_dump_csr(device);
  901. mutex_lock(&device->lock);
  902. if (!device->power_enabled) {
  903. dprintk(CVP_WARN, "%s: iris power off\n", __func__);
  904. rc = -EINVAL;
  905. goto exit;
  906. }
  907. __flush_debug_queue(device, NULL);
  908. exit:
  909. mutex_unlock(&device->lock);
  910. return rc;
  911. }
  912. static int iris_hfi_scale_clocks(void *dev, u32 freq)
  913. {
  914. int rc = 0;
  915. struct iris_hfi_device *device = dev;
  916. if (!device) {
  917. dprintk(CVP_ERR, "Invalid args: %pK\n", device);
  918. return -EINVAL;
  919. }
  920. mutex_lock(&device->lock);
  921. if (__resume(device)) {
  922. dprintk(CVP_ERR, "Resume from power collapse failed\n");
  923. rc = -ENODEV;
  924. goto exit;
  925. }
  926. rc = msm_cvp_set_clocks_impl(device, freq);
  927. exit:
  928. mutex_unlock(&device->lock);
  929. return rc;
  930. }
  931. /* Writes into cmdq without raising an interrupt */
  932. static int __iface_cmdq_write_relaxed(struct iris_hfi_device *device,
  933. void *pkt, bool *requires_interrupt)
  934. {
  935. struct cvp_iface_q_info *q_info;
  936. struct cvp_hal_cmd_pkt_hdr *cmd_packet;
  937. int result = -E2BIG;
  938. if (!device || !pkt) {
  939. dprintk(CVP_ERR, "Invalid Params\n");
  940. return -EINVAL;
  941. }
  942. __strict_check(device);
  943. if (!__core_in_valid_state(device)) {
  944. dprintk(CVP_ERR, "%s - fw not in init state\n", __func__);
  945. result = -EINVAL;
  946. goto err_q_null;
  947. }
  948. cmd_packet = (struct cvp_hal_cmd_pkt_hdr *)pkt;
  949. device->last_packet_type = cmd_packet->packet_type;
  950. q_info = &device->iface_queues[CVP_IFACEQ_CMDQ_IDX];
  951. if (!q_info) {
  952. dprintk(CVP_ERR, "cannot write to shared Q's\n");
  953. goto err_q_null;
  954. }
  955. if (!q_info->q_array.align_virtual_addr) {
  956. dprintk(CVP_ERR, "cannot write to shared CMD Q's\n");
  957. result = -ENODATA;
  958. goto err_q_null;
  959. }
  960. if (__resume(device)) {
  961. dprintk(CVP_ERR, "%s: Power on failed\n", __func__);
  962. goto err_q_write;
  963. }
  964. if (!__write_queue(q_info, (u8 *)pkt, requires_interrupt)) {
  965. if (device->res->sw_power_collapsible) {
  966. cancel_delayed_work(&iris_hfi_pm_work);
  967. if (!queue_delayed_work(device->iris_pm_workq,
  968. &iris_hfi_pm_work,
  969. msecs_to_jiffies(
  970. device->res->msm_cvp_pwr_collapse_delay))) {
  971. dprintk(CVP_PWR,
  972. "PM work already scheduled\n");
  973. }
  974. }
  975. result = 0;
  976. } else {
  977. dprintk(CVP_ERR, "__iface_cmdq_write: queue full\n");
  978. }
  979. err_q_write:
  980. err_q_null:
  981. return result;
  982. }
  983. static int __iface_cmdq_write(struct iris_hfi_device *device, void *pkt)
  984. {
  985. bool needs_interrupt = false;
  986. int rc = __iface_cmdq_write_relaxed(device, pkt, &needs_interrupt);
  987. if (!rc && needs_interrupt) {
  988. /* Consumer of cmdq prefers that we raise an interrupt */
  989. rc = 0;
  990. __write_register(device, CVP_CPU_CS_H2ASOFTINT, 1);
  991. }
  992. return rc;
  993. }
  994. static int __iface_msgq_read(struct iris_hfi_device *device, void *pkt)
  995. {
  996. u32 tx_req_is_set = 0;
  997. int rc = 0;
  998. struct cvp_iface_q_info *q_info;
  999. if (!pkt) {
  1000. dprintk(CVP_ERR, "Invalid Params\n");
  1001. return -EINVAL;
  1002. }
  1003. __strict_check(device);
  1004. if (!__core_in_valid_state(device)) {
  1005. dprintk(CVP_WARN, "%s - fw not in init state\n", __func__);
  1006. rc = -EINVAL;
  1007. goto read_error_null;
  1008. }
  1009. q_info = &device->iface_queues[CVP_IFACEQ_MSGQ_IDX];
  1010. if (q_info->q_array.align_virtual_addr == NULL) {
  1011. dprintk(CVP_ERR, "cannot read from shared MSG Q's\n");
  1012. rc = -ENODATA;
  1013. goto read_error_null;
  1014. }
  1015. if (!__read_queue(q_info, (u8 *)pkt, &tx_req_is_set)) {
  1016. if (tx_req_is_set)
  1017. __write_register(device, CVP_CPU_CS_H2ASOFTINT, 1);
  1018. rc = 0;
  1019. } else
  1020. rc = -ENODATA;
  1021. read_error_null:
  1022. return rc;
  1023. }
  1024. static int __iface_dbgq_read(struct iris_hfi_device *device, void *pkt)
  1025. {
  1026. u32 tx_req_is_set = 0;
  1027. int rc = 0;
  1028. struct cvp_iface_q_info *q_info;
  1029. if (!pkt) {
  1030. dprintk(CVP_ERR, "Invalid Params\n");
  1031. return -EINVAL;
  1032. }
  1033. __strict_check(device);
  1034. q_info = &device->iface_queues[CVP_IFACEQ_DBGQ_IDX];
  1035. if (q_info->q_array.align_virtual_addr == NULL) {
  1036. dprintk(CVP_ERR, "cannot read from shared DBG Q's\n");
  1037. rc = -ENODATA;
  1038. goto dbg_error_null;
  1039. }
  1040. if (!__read_queue(q_info, (u8 *)pkt, &tx_req_is_set)) {
  1041. if (tx_req_is_set)
  1042. __write_register(device, CVP_CPU_CS_H2ASOFTINT, 1);
  1043. rc = 0;
  1044. } else
  1045. rc = -ENODATA;
  1046. dbg_error_null:
  1047. return rc;
  1048. }
  1049. static void __set_queue_hdr_defaults(struct cvp_hfi_queue_header *q_hdr)
  1050. {
  1051. q_hdr->qhdr_status = 0x1;
  1052. q_hdr->qhdr_type = CVP_IFACEQ_DFLT_QHDR;
  1053. q_hdr->qhdr_q_size = CVP_IFACEQ_QUEUE_SIZE / 4;
  1054. q_hdr->qhdr_pkt_size = 0;
  1055. q_hdr->qhdr_rx_wm = 0x1;
  1056. q_hdr->qhdr_tx_wm = 0x1;
  1057. q_hdr->qhdr_rx_req = 0x1;
  1058. q_hdr->qhdr_tx_req = 0x0;
  1059. q_hdr->qhdr_rx_irq_status = 0x0;
  1060. q_hdr->qhdr_tx_irq_status = 0x0;
  1061. q_hdr->qhdr_read_idx = 0x0;
  1062. q_hdr->qhdr_write_idx = 0x0;
  1063. }
  1064. static void __interface_dsp_queues_release(struct iris_hfi_device *device)
  1065. {
  1066. int i;
  1067. struct msm_cvp_smem *mem_data = &device->dsp_iface_q_table.mem_data;
  1068. struct context_bank_info *cb = mem_data->mapping_info.cb_info;
  1069. if (!device->dsp_iface_q_table.align_virtual_addr) {
  1070. dprintk(CVP_ERR, "%s: already released\n", __func__);
  1071. return;
  1072. }
  1073. dma_unmap_single_attrs(cb->dev, mem_data->device_addr,
  1074. mem_data->size, DMA_BIDIRECTIONAL, 0);
  1075. dma_free_coherent(device->res->mem_cdsp.dev, mem_data->size,
  1076. mem_data->kvaddr, mem_data->dma_handle);
  1077. for (i = 0; i < CVP_IFACEQ_NUMQ; i++) {
  1078. device->dsp_iface_queues[i].q_hdr = NULL;
  1079. device->dsp_iface_queues[i].q_array.align_virtual_addr = NULL;
  1080. device->dsp_iface_queues[i].q_array.align_device_addr = 0;
  1081. }
  1082. device->dsp_iface_q_table.align_virtual_addr = NULL;
  1083. device->dsp_iface_q_table.align_device_addr = 0;
  1084. }
  1085. static int __interface_dsp_queues_init(struct iris_hfi_device *dev)
  1086. {
  1087. int rc = 0;
  1088. u32 i;
  1089. struct cvp_iface_q_info *iface_q;
  1090. int offset = 0;
  1091. phys_addr_t fw_bias = 0;
  1092. size_t q_size;
  1093. struct msm_cvp_smem *mem_data;
  1094. void *kvaddr;
  1095. dma_addr_t dma_handle;
  1096. dma_addr_t iova;
  1097. struct context_bank_info *cb;
  1098. q_size = ALIGN(QUEUE_SIZE, SZ_1M);
  1099. mem_data = &dev->dsp_iface_q_table.mem_data;
  1100. /* Allocate dsp queues from CDSP device memory */
  1101. kvaddr = dma_alloc_coherent(dev->res->mem_cdsp.dev, q_size,
  1102. &dma_handle, GFP_KERNEL);
  1103. if (IS_ERR_OR_NULL(kvaddr)) {
  1104. dprintk(CVP_ERR, "%s: failed dma allocation\n", __func__);
  1105. goto fail_dma_alloc;
  1106. }
  1107. cb = msm_cvp_smem_get_context_bank(dev->res, 0);
  1108. if (!cb) {
  1109. dprintk(CVP_ERR,
  1110. "%s: failed to get context bank\n", __func__);
  1111. goto fail_dma_map;
  1112. }
  1113. iova = dma_map_single_attrs(cb->dev, phys_to_virt(dma_handle),
  1114. q_size, DMA_BIDIRECTIONAL, 0);
  1115. if (dma_mapping_error(cb->dev, iova)) {
  1116. dprintk(CVP_ERR, "%s: failed dma mapping\n", __func__);
  1117. goto fail_dma_map;
  1118. }
  1119. dprintk(CVP_DSP,
  1120. "%s: kvaddr %pK dma_handle %#llx iova %#llx size %zd\n",
  1121. __func__, kvaddr, dma_handle, iova, q_size);
  1122. memset(mem_data, 0, sizeof(struct msm_cvp_smem));
  1123. mem_data->kvaddr = kvaddr;
  1124. mem_data->device_addr = iova;
  1125. mem_data->dma_handle = dma_handle;
  1126. mem_data->size = q_size;
  1127. mem_data->mapping_info.cb_info = cb;
  1128. if (!is_iommu_present(dev->res))
  1129. fw_bias = dev->cvp_hal_data->firmware_base;
  1130. dev->dsp_iface_q_table.align_virtual_addr = kvaddr;
  1131. dev->dsp_iface_q_table.align_device_addr = iova - fw_bias;
  1132. dev->dsp_iface_q_table.mem_size = CVP_IFACEQ_TABLE_SIZE;
  1133. offset = dev->dsp_iface_q_table.mem_size;
  1134. for (i = 0; i < CVP_IFACEQ_NUMQ; i++) {
  1135. iface_q = &dev->dsp_iface_queues[i];
  1136. iface_q->q_array.align_device_addr = iova + offset - fw_bias;
  1137. iface_q->q_array.align_virtual_addr = kvaddr + offset;
  1138. iface_q->q_array.mem_size = CVP_IFACEQ_QUEUE_SIZE;
  1139. offset += iface_q->q_array.mem_size;
  1140. spin_lock_init(&iface_q->hfi_lock);
  1141. }
  1142. cvp_dsp_init_hfi_queue_hdr(dev);
  1143. return rc;
  1144. fail_dma_map:
  1145. dma_free_coherent(dev->res->mem_cdsp.dev, q_size, kvaddr, dma_handle);
  1146. fail_dma_alloc:
  1147. return -ENOMEM;
  1148. }
  1149. static void __interface_queues_release(struct iris_hfi_device *device)
  1150. {
  1151. int i;
  1152. struct cvp_hfi_mem_map_table *qdss;
  1153. struct cvp_hfi_mem_map *mem_map;
  1154. int num_entries = device->res->qdss_addr_set.count;
  1155. unsigned long mem_map_table_base_addr;
  1156. struct context_bank_info *cb;
  1157. if (device->qdss.align_virtual_addr) {
  1158. qdss = (struct cvp_hfi_mem_map_table *)
  1159. device->qdss.align_virtual_addr;
  1160. qdss->mem_map_num_entries = num_entries;
  1161. mem_map_table_base_addr =
  1162. device->qdss.align_device_addr +
  1163. sizeof(struct cvp_hfi_mem_map_table);
  1164. qdss->mem_map_table_base_addr =
  1165. (u32)mem_map_table_base_addr;
  1166. if ((unsigned long)qdss->mem_map_table_base_addr !=
  1167. mem_map_table_base_addr) {
  1168. dprintk(CVP_ERR,
  1169. "Invalid mem_map_table_base_addr %#lx",
  1170. mem_map_table_base_addr);
  1171. }
  1172. mem_map = (struct cvp_hfi_mem_map *)(qdss + 1);
  1173. cb = msm_cvp_smem_get_context_bank(device->res, 0);
  1174. for (i = 0; cb && i < num_entries; i++) {
  1175. iommu_unmap(cb->domain,
  1176. mem_map[i].virtual_addr,
  1177. mem_map[i].size);
  1178. }
  1179. __smem_free(device, &device->qdss.mem_data);
  1180. }
  1181. __smem_free(device, &device->iface_q_table.mem_data);
  1182. __smem_free(device, &device->sfr.mem_data);
  1183. for (i = 0; i < CVP_IFACEQ_NUMQ; i++) {
  1184. device->iface_queues[i].q_hdr = NULL;
  1185. device->iface_queues[i].q_array.align_virtual_addr = NULL;
  1186. device->iface_queues[i].q_array.align_device_addr = 0;
  1187. }
  1188. device->iface_q_table.align_virtual_addr = NULL;
  1189. device->iface_q_table.align_device_addr = 0;
  1190. device->qdss.align_virtual_addr = NULL;
  1191. device->qdss.align_device_addr = 0;
  1192. device->sfr.align_virtual_addr = NULL;
  1193. device->sfr.align_device_addr = 0;
  1194. device->mem_addr.align_virtual_addr = NULL;
  1195. device->mem_addr.align_device_addr = 0;
  1196. __interface_dsp_queues_release(device);
  1197. }
  1198. static int __get_qdss_iommu_virtual_addr(struct iris_hfi_device *dev,
  1199. struct cvp_hfi_mem_map *mem_map,
  1200. struct iommu_domain *domain)
  1201. {
  1202. int i;
  1203. int rc = 0;
  1204. dma_addr_t iova = QDSS_IOVA_START;
  1205. int num_entries = dev->res->qdss_addr_set.count;
  1206. struct addr_range *qdss_addr_tbl = dev->res->qdss_addr_set.addr_tbl;
  1207. if (!num_entries)
  1208. return -ENODATA;
  1209. for (i = 0; i < num_entries; i++) {
  1210. if (domain) {
  1211. rc = iommu_map(domain, iova,
  1212. qdss_addr_tbl[i].start,
  1213. qdss_addr_tbl[i].size,
  1214. IOMMU_READ | IOMMU_WRITE);
  1215. if (rc) {
  1216. dprintk(CVP_ERR,
  1217. "IOMMU QDSS mapping failed for addr %#x\n",
  1218. qdss_addr_tbl[i].start);
  1219. rc = -ENOMEM;
  1220. break;
  1221. }
  1222. } else {
  1223. iova = qdss_addr_tbl[i].start;
  1224. }
  1225. mem_map[i].virtual_addr = (u32)iova;
  1226. mem_map[i].physical_addr = qdss_addr_tbl[i].start;
  1227. mem_map[i].size = qdss_addr_tbl[i].size;
  1228. mem_map[i].attr = 0x0;
  1229. iova += mem_map[i].size;
  1230. }
  1231. if (i < num_entries) {
  1232. dprintk(CVP_ERR,
  1233. "QDSS mapping failed, Freeing other entries %d\n", i);
  1234. for (--i; domain && i >= 0; i--) {
  1235. iommu_unmap(domain,
  1236. mem_map[i].virtual_addr,
  1237. mem_map[i].size);
  1238. }
  1239. }
  1240. return rc;
  1241. }
  1242. static void __setup_ucregion_memory_map(struct iris_hfi_device *device)
  1243. {
  1244. __write_register(device, CVP_UC_REGION_ADDR,
  1245. (u32)device->iface_q_table.align_device_addr);
  1246. __write_register(device, CVP_UC_REGION_SIZE, SHARED_QSIZE);
  1247. __write_register(device, CVP_QTBL_ADDR,
  1248. (u32)device->iface_q_table.align_device_addr);
  1249. __write_register(device, CVP_QTBL_INFO, 0x01);
  1250. if (device->sfr.align_device_addr)
  1251. __write_register(device, CVP_SFR_ADDR,
  1252. (u32)device->sfr.align_device_addr);
  1253. if (device->qdss.align_device_addr)
  1254. __write_register(device, CVP_MMAP_ADDR,
  1255. (u32)device->qdss.align_device_addr);
  1256. call_iris_op(device, setup_dsp_uc_memmap, device);
  1257. }
  1258. static int __interface_queues_init(struct iris_hfi_device *dev)
  1259. {
  1260. struct cvp_hfi_queue_table_header *q_tbl_hdr;
  1261. struct cvp_hfi_queue_header *q_hdr;
  1262. u32 i;
  1263. int rc = 0;
  1264. struct cvp_hfi_mem_map_table *qdss;
  1265. struct cvp_hfi_mem_map *mem_map;
  1266. struct cvp_iface_q_info *iface_q;
  1267. struct cvp_hfi_sfr_struct *vsfr;
  1268. struct cvp_mem_addr *mem_addr;
  1269. int offset = 0;
  1270. int num_entries = dev->res->qdss_addr_set.count;
  1271. phys_addr_t fw_bias = 0;
  1272. size_t q_size;
  1273. unsigned long mem_map_table_base_addr;
  1274. struct context_bank_info *cb;
  1275. q_size = SHARED_QSIZE - ALIGNED_SFR_SIZE - ALIGNED_QDSS_SIZE;
  1276. mem_addr = &dev->mem_addr;
  1277. if (!is_iommu_present(dev->res))
  1278. fw_bias = dev->cvp_hal_data->firmware_base;
  1279. rc = __smem_alloc(dev, mem_addr, q_size, 1, SMEM_UNCACHED);
  1280. if (rc) {
  1281. dprintk(CVP_ERR, "iface_q_table_alloc_fail\n");
  1282. goto fail_alloc_queue;
  1283. }
  1284. dev->iface_q_table.align_virtual_addr = mem_addr->align_virtual_addr;
  1285. dev->iface_q_table.align_device_addr = mem_addr->align_device_addr -
  1286. fw_bias;
  1287. dev->iface_q_table.mem_size = CVP_IFACEQ_TABLE_SIZE;
  1288. dev->iface_q_table.mem_data = mem_addr->mem_data;
  1289. offset += dev->iface_q_table.mem_size;
  1290. for (i = 0; i < CVP_IFACEQ_NUMQ; i++) {
  1291. iface_q = &dev->iface_queues[i];
  1292. iface_q->q_array.align_device_addr = mem_addr->align_device_addr
  1293. + offset - fw_bias;
  1294. iface_q->q_array.align_virtual_addr =
  1295. mem_addr->align_virtual_addr + offset;
  1296. iface_q->q_array.mem_size = CVP_IFACEQ_QUEUE_SIZE;
  1297. offset += iface_q->q_array.mem_size;
  1298. iface_q->q_hdr = CVP_IFACEQ_GET_QHDR_START_ADDR(
  1299. dev->iface_q_table.align_virtual_addr, i);
  1300. __set_queue_hdr_defaults(iface_q->q_hdr);
  1301. spin_lock_init(&iface_q->hfi_lock);
  1302. }
  1303. if ((msm_cvp_fw_debug_mode & HFI_DEBUG_MODE_QDSS) && num_entries) {
  1304. rc = __smem_alloc(dev, mem_addr, ALIGNED_QDSS_SIZE, 1,
  1305. SMEM_UNCACHED);
  1306. if (rc) {
  1307. dprintk(CVP_WARN,
  1308. "qdss_alloc_fail: QDSS messages logging will not work\n");
  1309. dev->qdss.align_device_addr = 0;
  1310. } else {
  1311. dev->qdss.align_device_addr =
  1312. mem_addr->align_device_addr - fw_bias;
  1313. dev->qdss.align_virtual_addr =
  1314. mem_addr->align_virtual_addr;
  1315. dev->qdss.mem_size = ALIGNED_QDSS_SIZE;
  1316. dev->qdss.mem_data = mem_addr->mem_data;
  1317. }
  1318. }
  1319. rc = __smem_alloc(dev, mem_addr, ALIGNED_SFR_SIZE, 1, SMEM_UNCACHED);
  1320. if (rc) {
  1321. dprintk(CVP_WARN, "sfr_alloc_fail: SFR not will work\n");
  1322. dev->sfr.align_device_addr = 0;
  1323. } else {
  1324. dev->sfr.align_device_addr = mem_addr->align_device_addr -
  1325. fw_bias;
  1326. dev->sfr.align_virtual_addr = mem_addr->align_virtual_addr;
  1327. dev->sfr.mem_size = ALIGNED_SFR_SIZE;
  1328. dev->sfr.mem_data = mem_addr->mem_data;
  1329. }
  1330. q_tbl_hdr = (struct cvp_hfi_queue_table_header *)
  1331. dev->iface_q_table.align_virtual_addr;
  1332. q_tbl_hdr->qtbl_version = 0;
  1333. q_tbl_hdr->device_addr = (void *)dev;
  1334. strlcpy(q_tbl_hdr->name, "msm_cvp", sizeof(q_tbl_hdr->name));
  1335. q_tbl_hdr->qtbl_size = CVP_IFACEQ_TABLE_SIZE;
  1336. q_tbl_hdr->qtbl_qhdr0_offset =
  1337. sizeof(struct cvp_hfi_queue_table_header);
  1338. q_tbl_hdr->qtbl_qhdr_size = sizeof(struct cvp_hfi_queue_header);
  1339. q_tbl_hdr->qtbl_num_q = CVP_IFACEQ_NUMQ;
  1340. q_tbl_hdr->qtbl_num_active_q = CVP_IFACEQ_NUMQ;
  1341. iface_q = &dev->iface_queues[CVP_IFACEQ_CMDQ_IDX];
  1342. q_hdr = iface_q->q_hdr;
  1343. q_hdr->qhdr_start_addr = iface_q->q_array.align_device_addr;
  1344. q_hdr->qhdr_type |= HFI_Q_ID_HOST_TO_CTRL_CMD_Q;
  1345. iface_q = &dev->iface_queues[CVP_IFACEQ_MSGQ_IDX];
  1346. q_hdr = iface_q->q_hdr;
  1347. q_hdr->qhdr_start_addr = iface_q->q_array.align_device_addr;
  1348. q_hdr->qhdr_type |= HFI_Q_ID_CTRL_TO_HOST_MSG_Q;
  1349. iface_q = &dev->iface_queues[CVP_IFACEQ_DBGQ_IDX];
  1350. q_hdr = iface_q->q_hdr;
  1351. q_hdr->qhdr_start_addr = iface_q->q_array.align_device_addr;
  1352. q_hdr->qhdr_type |= HFI_Q_ID_CTRL_TO_HOST_DEBUG_Q;
  1353. /*
  1354. * Set receive request to zero on debug queue as there is no
  1355. * need of interrupt from cvp hardware for debug messages
  1356. */
  1357. q_hdr->qhdr_rx_req = 0;
  1358. if (dev->qdss.align_virtual_addr) {
  1359. qdss =
  1360. (struct cvp_hfi_mem_map_table *)dev->qdss.align_virtual_addr;
  1361. qdss->mem_map_num_entries = num_entries;
  1362. mem_map_table_base_addr = dev->qdss.align_device_addr +
  1363. sizeof(struct cvp_hfi_mem_map_table);
  1364. qdss->mem_map_table_base_addr = mem_map_table_base_addr;
  1365. mem_map = (struct cvp_hfi_mem_map *)(qdss + 1);
  1366. cb = msm_cvp_smem_get_context_bank(dev->res, 0);
  1367. if (!cb) {
  1368. dprintk(CVP_ERR,
  1369. "%s: failed to get context bank\n", __func__);
  1370. return -EINVAL;
  1371. }
  1372. rc = __get_qdss_iommu_virtual_addr(dev, mem_map, cb->domain);
  1373. if (rc) {
  1374. dprintk(CVP_ERR,
  1375. "IOMMU mapping failed, Freeing qdss memdata\n");
  1376. __smem_free(dev, &dev->qdss.mem_data);
  1377. dev->qdss.align_virtual_addr = NULL;
  1378. dev->qdss.align_device_addr = 0;
  1379. }
  1380. }
  1381. vsfr = (struct cvp_hfi_sfr_struct *) dev->sfr.align_virtual_addr;
  1382. if (vsfr)
  1383. vsfr->bufSize = ALIGNED_SFR_SIZE;
  1384. rc = __interface_dsp_queues_init(dev);
  1385. if (rc) {
  1386. dprintk(CVP_ERR, "dsp_queues_init failed\n");
  1387. goto fail_alloc_queue;
  1388. }
  1389. __setup_ucregion_memory_map(dev);
  1390. return 0;
  1391. fail_alloc_queue:
  1392. return -ENOMEM;
  1393. }
  1394. static int __sys_set_debug(struct iris_hfi_device *device, u32 debug)
  1395. {
  1396. u8 packet[CVP_IFACEQ_VAR_SMALL_PKT_SIZE];
  1397. int rc = 0;
  1398. struct cvp_hfi_cmd_sys_set_property_packet *pkt =
  1399. (struct cvp_hfi_cmd_sys_set_property_packet *) &packet;
  1400. rc = call_hfi_pkt_op(device, sys_debug_config, pkt, debug);
  1401. if (rc) {
  1402. dprintk(CVP_WARN,
  1403. "Debug mode setting to FW failed\n");
  1404. return -ENOTEMPTY;
  1405. }
  1406. if (__iface_cmdq_write(device, pkt))
  1407. return -ENOTEMPTY;
  1408. return 0;
  1409. }
  1410. static int __sys_set_idle_indicator(struct iris_hfi_device *device,
  1411. bool enable)
  1412. {
  1413. u8 packet[CVP_IFACEQ_VAR_SMALL_PKT_SIZE];
  1414. int rc = 0;
  1415. struct cvp_hfi_cmd_sys_set_property_packet *pkt =
  1416. (struct cvp_hfi_cmd_sys_set_property_packet *) &packet;
  1417. rc = call_hfi_pkt_op(device, sys_set_idle_indicator, pkt, enable);
  1418. if (__iface_cmdq_write(device, pkt))
  1419. return -ENOTEMPTY;
  1420. return 0;
  1421. }
  1422. static int __sys_set_coverage(struct iris_hfi_device *device, u32 mode)
  1423. {
  1424. u8 packet[CVP_IFACEQ_VAR_SMALL_PKT_SIZE];
  1425. int rc = 0;
  1426. struct cvp_hfi_cmd_sys_set_property_packet *pkt =
  1427. (struct cvp_hfi_cmd_sys_set_property_packet *) &packet;
  1428. rc = call_hfi_pkt_op(device, sys_coverage_config,
  1429. pkt, mode);
  1430. if (rc) {
  1431. dprintk(CVP_WARN,
  1432. "Coverage mode setting to FW failed\n");
  1433. return -ENOTEMPTY;
  1434. }
  1435. if (__iface_cmdq_write(device, pkt)) {
  1436. dprintk(CVP_WARN, "Failed to send coverage pkt to f/w\n");
  1437. return -ENOTEMPTY;
  1438. }
  1439. return 0;
  1440. }
  1441. static int __sys_set_power_control(struct iris_hfi_device *device,
  1442. bool enable)
  1443. {
  1444. struct regulator_info *rinfo;
  1445. bool supported = false;
  1446. u8 packet[CVP_IFACEQ_VAR_SMALL_PKT_SIZE];
  1447. struct cvp_hfi_cmd_sys_set_property_packet *pkt =
  1448. (struct cvp_hfi_cmd_sys_set_property_packet *) &packet;
  1449. iris_hfi_for_each_regulator(device, rinfo) {
  1450. if (rinfo->has_hw_power_collapse) {
  1451. supported = true;
  1452. break;
  1453. }
  1454. }
  1455. if (!supported)
  1456. return 0;
  1457. call_hfi_pkt_op(device, sys_power_control, pkt, enable);
  1458. if (__iface_cmdq_write(device, pkt))
  1459. return -ENOTEMPTY;
  1460. return 0;
  1461. }
  1462. static int iris_hfi_core_init(void *device)
  1463. {
  1464. int rc = 0;
  1465. u32 ipcc_iova;
  1466. struct cvp_hfi_cmd_sys_init_packet pkt;
  1467. struct cvp_hfi_cmd_sys_get_property_packet version_pkt;
  1468. struct iris_hfi_device *dev;
  1469. if (!device) {
  1470. dprintk(CVP_ERR, "Invalid device\n");
  1471. return -ENODEV;
  1472. }
  1473. dev = device;
  1474. dprintk(CVP_CORE, "Core initializing\n");
  1475. mutex_lock(&dev->lock);
  1476. dev->bus_vote.data =
  1477. kzalloc(sizeof(struct cvp_bus_vote_data), GFP_KERNEL);
  1478. if (!dev->bus_vote.data) {
  1479. dprintk(CVP_ERR, "Bus vote data memory is not allocated\n");
  1480. rc = -ENOMEM;
  1481. goto err_no_mem;
  1482. }
  1483. dev->bus_vote.data_count = 1;
  1484. dev->bus_vote.data->power_mode = CVP_POWER_TURBO;
  1485. rc = __load_fw(dev);
  1486. if (rc) {
  1487. dprintk(CVP_ERR, "Failed to load Iris FW\n");
  1488. goto err_load_fw;
  1489. }
  1490. __set_state(dev, IRIS_STATE_INIT);
  1491. dev->reg_dumped = false;
  1492. dprintk(CVP_CORE, "Dev_Virt: %pa, Reg_Virt: %pK\n",
  1493. &dev->cvp_hal_data->firmware_base,
  1494. dev->cvp_hal_data->register_base);
  1495. rc = __interface_queues_init(dev);
  1496. if (rc) {
  1497. dprintk(CVP_ERR, "failed to init queues\n");
  1498. rc = -ENOMEM;
  1499. goto err_core_init;
  1500. }
  1501. rc = msm_cvp_map_ipcc_regs(&ipcc_iova);
  1502. if (!rc) {
  1503. dprintk(CVP_CORE, "IPCC iova 0x%x\n", ipcc_iova);
  1504. __write_register(dev, CVP_MMAP_ADDR, ipcc_iova);
  1505. }
  1506. rc = __boot_firmware(dev);
  1507. if (rc) {
  1508. dprintk(CVP_ERR, "Failed to start core\n");
  1509. rc = -ENODEV;
  1510. goto err_core_init;
  1511. }
  1512. dev->version = __read_register(dev, CVP_VERSION_INFO);
  1513. rc = call_hfi_pkt_op(dev, sys_init, &pkt, 0);
  1514. if (rc) {
  1515. dprintk(CVP_ERR, "Failed to create sys init pkt\n");
  1516. goto err_core_init;
  1517. }
  1518. if (__iface_cmdq_write(dev, &pkt)) {
  1519. rc = -ENOTEMPTY;
  1520. goto err_core_init;
  1521. }
  1522. rc = call_hfi_pkt_op(dev, sys_image_version, &version_pkt);
  1523. if (rc || __iface_cmdq_write(dev, &version_pkt))
  1524. dprintk(CVP_WARN, "Failed to send image version pkt to f/w\n");
  1525. __sys_set_debug(device, msm_cvp_fw_debug);
  1526. __enable_subcaches(device);
  1527. __set_subcaches(device);
  1528. __set_ubwc_config(device);
  1529. __sys_set_idle_indicator(device, true);
  1530. if (dev->res->pm_qos_latency_us)
  1531. cpu_latency_qos_add_request(&dev->qos,
  1532. dev->res->pm_qos_latency_us);
  1533. /* mmrm registration */
  1534. if (msm_cvp_mmrm_enabled) {
  1535. rc = msm_cvp_mmrm_register(device);
  1536. if (rc) {
  1537. dprintk(CVP_ERR, "Failed to register mmrm client\n");
  1538. goto err_core_init;
  1539. }
  1540. }
  1541. mutex_unlock(&dev->lock);
  1542. cvp_dsp_send_hfi_queue();
  1543. dprintk(CVP_CORE, "Core inited successfully\n");
  1544. return 0;
  1545. err_core_init:
  1546. __set_state(dev, IRIS_STATE_DEINIT);
  1547. __unload_fw(dev);
  1548. err_load_fw:
  1549. err_no_mem:
  1550. dprintk(CVP_ERR, "Core init failed\n");
  1551. mutex_unlock(&dev->lock);
  1552. return rc;
  1553. }
  1554. static int iris_hfi_core_release(void *dev)
  1555. {
  1556. int rc = 0;
  1557. struct iris_hfi_device *device = dev;
  1558. struct cvp_hal_session *session, *next;
  1559. if (!device) {
  1560. dprintk(CVP_ERR, "invalid device\n");
  1561. return -ENODEV;
  1562. }
  1563. mutex_lock(&device->lock);
  1564. dprintk(CVP_WARN, "Core releasing\n");
  1565. if (device->res->pm_qos_latency_us &&
  1566. cpu_latency_qos_request_active(&device->qos))
  1567. cpu_latency_qos_remove_request(&device->qos);
  1568. __resume(device);
  1569. __set_state(device, IRIS_STATE_DEINIT);
  1570. __dsp_shutdown(device, 0);
  1571. if (msm_cvp_mmrm_enabled) {
  1572. rc = msm_cvp_mmrm_deregister(device);
  1573. if (rc) {
  1574. dprintk(CVP_ERR,
  1575. "%s: Failed msm_cvp_mmrm_deregister:%d\n",
  1576. __func__, rc);
  1577. }
  1578. }
  1579. __unload_fw(device);
  1580. /* unlink all sessions from device */
  1581. list_for_each_entry_safe(session, next, &device->sess_head, list) {
  1582. list_del(&session->list);
  1583. session->device = NULL;
  1584. }
  1585. dprintk(CVP_CORE, "Core released successfully\n");
  1586. mutex_unlock(&device->lock);
  1587. return rc;
  1588. }
  1589. static void __core_clear_interrupt(struct iris_hfi_device *device)
  1590. {
  1591. u32 intr_status = 0, mask = 0;
  1592. if (!device) {
  1593. dprintk(CVP_ERR, "%s: NULL device\n", __func__);
  1594. return;
  1595. }
  1596. intr_status = __read_register(device, CVP_WRAPPER_INTR_STATUS);
  1597. mask = (CVP_WRAPPER_INTR_MASK_A2HCPU_BMSK | CVP_FATAL_INTR_BMSK);
  1598. if (intr_status & mask) {
  1599. device->intr_status |= intr_status;
  1600. device->reg_count++;
  1601. dprintk(CVP_CORE,
  1602. "INTERRUPT for device: %pK: times: %d status: %d\n",
  1603. device, device->reg_count, intr_status);
  1604. } else {
  1605. device->spur_count++;
  1606. }
  1607. __write_register(device, CVP_CPU_CS_A2HSOFTINTCLR, 1);
  1608. }
  1609. static int iris_hfi_core_trigger_ssr(void *device,
  1610. enum hal_ssr_trigger_type type)
  1611. {
  1612. struct cvp_hfi_cmd_sys_test_ssr_packet pkt;
  1613. int rc = 0;
  1614. struct iris_hfi_device *dev;
  1615. if (!device) {
  1616. dprintk(CVP_ERR, "invalid device\n");
  1617. return -ENODEV;
  1618. }
  1619. dev = device;
  1620. if (mutex_trylock(&dev->lock)) {
  1621. rc = call_hfi_pkt_op(dev, ssr_cmd, type, &pkt);
  1622. if (rc) {
  1623. dprintk(CVP_ERR, "%s: failed to create packet\n",
  1624. __func__);
  1625. goto err_create_pkt;
  1626. }
  1627. if (__iface_cmdq_write(dev, &pkt))
  1628. rc = -ENOTEMPTY;
  1629. } else {
  1630. return -EAGAIN;
  1631. }
  1632. err_create_pkt:
  1633. mutex_unlock(&dev->lock);
  1634. return rc;
  1635. }
  1636. static void __set_default_sys_properties(struct iris_hfi_device *device)
  1637. {
  1638. if (__sys_set_debug(device, msm_cvp_fw_debug))
  1639. dprintk(CVP_WARN, "Setting fw_debug msg ON failed\n");
  1640. if (__sys_set_power_control(device, msm_cvp_fw_low_power_mode))
  1641. dprintk(CVP_WARN, "Setting h/w power collapse ON failed\n");
  1642. }
  1643. static void __session_clean(struct cvp_hal_session *session)
  1644. {
  1645. struct cvp_hal_session *temp, *next;
  1646. struct iris_hfi_device *device;
  1647. if (!session || !session->device) {
  1648. dprintk(CVP_WARN, "%s: invalid params\n", __func__);
  1649. return;
  1650. }
  1651. device = session->device;
  1652. dprintk(CVP_SESS, "deleted the session: %pK\n", session);
  1653. /*
  1654. * session might have been removed from the device list in
  1655. * core_release, so check and remove if it is in the list
  1656. */
  1657. list_for_each_entry_safe(temp, next, &device->sess_head, list) {
  1658. if (session == temp) {
  1659. list_del(&session->list);
  1660. break;
  1661. }
  1662. }
  1663. /* Poison the session handle with zeros */
  1664. *session = (struct cvp_hal_session){ {0} };
  1665. kfree(session);
  1666. }
  1667. static int iris_hfi_session_clean(void *session)
  1668. {
  1669. struct cvp_hal_session *sess_close;
  1670. struct iris_hfi_device *device;
  1671. if (!session) {
  1672. dprintk(CVP_ERR, "Invalid Params %s\n", __func__);
  1673. return -EINVAL;
  1674. }
  1675. sess_close = session;
  1676. device = sess_close->device;
  1677. if (!device) {
  1678. dprintk(CVP_ERR, "Invalid device handle %s\n", __func__);
  1679. return -EINVAL;
  1680. }
  1681. mutex_lock(&device->lock);
  1682. __session_clean(sess_close);
  1683. mutex_unlock(&device->lock);
  1684. return 0;
  1685. }
  1686. static int iris_hfi_session_init(void *device, void *session_id,
  1687. void **new_session)
  1688. {
  1689. struct cvp_hfi_cmd_sys_session_init_packet pkt;
  1690. struct iris_hfi_device *dev;
  1691. struct cvp_hal_session *s;
  1692. if (!device || !new_session) {
  1693. dprintk(CVP_ERR, "%s - invalid input\n", __func__);
  1694. return -EINVAL;
  1695. }
  1696. dev = device;
  1697. mutex_lock(&dev->lock);
  1698. s = kzalloc(sizeof(*s), GFP_KERNEL);
  1699. if (!s) {
  1700. dprintk(CVP_ERR, "new session fail: Out of memory\n");
  1701. goto err_session_init_fail;
  1702. }
  1703. s->session_id = session_id;
  1704. s->device = dev;
  1705. dprintk(CVP_SESS,
  1706. "%s: inst %pK, session %pK\n", __func__, session_id, s);
  1707. list_add_tail(&s->list, &dev->sess_head);
  1708. __set_default_sys_properties(device);
  1709. if (call_hfi_pkt_op(dev, session_init, &pkt, s)) {
  1710. dprintk(CVP_ERR, "session_init: failed to create packet\n");
  1711. goto err_session_init_fail;
  1712. }
  1713. *new_session = s;
  1714. if (__iface_cmdq_write(dev, &pkt))
  1715. goto err_session_init_fail;
  1716. mutex_unlock(&dev->lock);
  1717. return 0;
  1718. err_session_init_fail:
  1719. if (s)
  1720. __session_clean(s);
  1721. *new_session = NULL;
  1722. mutex_unlock(&dev->lock);
  1723. return -EINVAL;
  1724. }
  1725. static int __send_session_cmd(struct cvp_hal_session *session, int pkt_type)
  1726. {
  1727. struct cvp_hal_session_cmd_pkt pkt;
  1728. int rc = 0;
  1729. struct iris_hfi_device *device = session->device;
  1730. if (!__is_session_valid(device, session, __func__))
  1731. return -ECONNRESET;
  1732. rc = call_hfi_pkt_op(device, session_cmd,
  1733. &pkt, pkt_type, session);
  1734. if (rc == -EPERM)
  1735. return 0;
  1736. if (rc) {
  1737. dprintk(CVP_ERR, "send session cmd: create pkt failed\n");
  1738. goto err_create_pkt;
  1739. }
  1740. if (__iface_cmdq_write(session->device, &pkt))
  1741. rc = -ENOTEMPTY;
  1742. err_create_pkt:
  1743. return rc;
  1744. }
  1745. static int iris_hfi_session_end(void *session)
  1746. {
  1747. struct cvp_hal_session *sess;
  1748. struct iris_hfi_device *device;
  1749. int rc = 0;
  1750. if (!session) {
  1751. dprintk(CVP_ERR, "Invalid Params %s\n", __func__);
  1752. return -EINVAL;
  1753. }
  1754. sess = session;
  1755. device = sess->device;
  1756. if (!device) {
  1757. dprintk(CVP_ERR, "Invalid session %s\n", __func__);
  1758. return -EINVAL;
  1759. }
  1760. mutex_lock(&device->lock);
  1761. if (msm_cvp_fw_coverage) {
  1762. if (__sys_set_coverage(sess->device, msm_cvp_fw_coverage))
  1763. dprintk(CVP_WARN, "Fw_coverage msg ON failed\n");
  1764. }
  1765. rc = __send_session_cmd(session, HFI_CMD_SYS_SESSION_END);
  1766. mutex_unlock(&device->lock);
  1767. return rc;
  1768. }
  1769. static int iris_hfi_session_abort(void *sess)
  1770. {
  1771. struct cvp_hal_session *session = sess;
  1772. struct iris_hfi_device *device;
  1773. int rc = 0;
  1774. if (!session || !session->device) {
  1775. dprintk(CVP_ERR, "Invalid Params %s\n", __func__);
  1776. return -EINVAL;
  1777. }
  1778. device = session->device;
  1779. mutex_lock(&device->lock);
  1780. rc = __send_session_cmd(session, HFI_CMD_SYS_SESSION_ABORT);
  1781. mutex_unlock(&device->lock);
  1782. return rc;
  1783. }
  1784. static int iris_hfi_session_set_buffers(void *sess, u32 iova, u32 size)
  1785. {
  1786. struct cvp_hfi_cmd_session_set_buffers_packet pkt;
  1787. int rc = 0;
  1788. struct cvp_hal_session *session = sess;
  1789. struct iris_hfi_device *device;
  1790. if (!session || !session->device || !iova || !size) {
  1791. dprintk(CVP_ERR, "Invalid Params\n");
  1792. return -EINVAL;
  1793. }
  1794. device = session->device;
  1795. mutex_lock(&device->lock);
  1796. if (!__is_session_valid(device, session, __func__)) {
  1797. rc = -ECONNRESET;
  1798. goto err_create_pkt;
  1799. }
  1800. rc = call_hfi_pkt_op(device, session_set_buffers,
  1801. &pkt, session, iova, size);
  1802. if (rc) {
  1803. dprintk(CVP_ERR, "set buffers: failed to create packet\n");
  1804. goto err_create_pkt;
  1805. }
  1806. if (__iface_cmdq_write(session->device, &pkt))
  1807. rc = -ENOTEMPTY;
  1808. err_create_pkt:
  1809. mutex_unlock(&device->lock);
  1810. return rc;
  1811. }
  1812. static int iris_hfi_session_release_buffers(void *sess)
  1813. {
  1814. struct cvp_session_release_buffers_packet pkt;
  1815. int rc = 0;
  1816. struct cvp_hal_session *session = sess;
  1817. struct iris_hfi_device *device;
  1818. if (!session || !session->device) {
  1819. dprintk(CVP_ERR, "Invalid Params\n");
  1820. return -EINVAL;
  1821. }
  1822. device = session->device;
  1823. mutex_lock(&device->lock);
  1824. if (!__is_session_valid(device, session, __func__)) {
  1825. rc = -ECONNRESET;
  1826. goto err_create_pkt;
  1827. }
  1828. rc = call_hfi_pkt_op(device, session_release_buffers, &pkt, session);
  1829. if (rc) {
  1830. dprintk(CVP_ERR, "release buffers: failed to create packet\n");
  1831. goto err_create_pkt;
  1832. }
  1833. if (__iface_cmdq_write(session->device, &pkt))
  1834. rc = -ENOTEMPTY;
  1835. err_create_pkt:
  1836. mutex_unlock(&device->lock);
  1837. return rc;
  1838. }
  1839. static int iris_hfi_session_send(void *sess,
  1840. struct eva_kmd_hfi_packet *in_pkt)
  1841. {
  1842. int rc = 0;
  1843. struct eva_kmd_hfi_packet pkt;
  1844. struct cvp_hal_session *session = sess;
  1845. struct iris_hfi_device *device;
  1846. if (!session || !session->device) {
  1847. dprintk(CVP_ERR, "invalid session");
  1848. return -ENODEV;
  1849. }
  1850. device = session->device;
  1851. mutex_lock(&device->lock);
  1852. if (!__is_session_valid(device, session, __func__)) {
  1853. rc = -ECONNRESET;
  1854. goto err_send_pkt;
  1855. }
  1856. rc = call_hfi_pkt_op(device, session_send,
  1857. &pkt, session, in_pkt);
  1858. if (rc) {
  1859. dprintk(CVP_ERR,
  1860. "failed to create pkt\n");
  1861. goto err_send_pkt;
  1862. }
  1863. if (__iface_cmdq_write(session->device, &pkt))
  1864. rc = -ENOTEMPTY;
  1865. err_send_pkt:
  1866. mutex_unlock(&device->lock);
  1867. return rc;
  1868. return rc;
  1869. }
  1870. static int iris_hfi_session_flush(void *sess)
  1871. {
  1872. struct cvp_hal_session *session = sess;
  1873. struct iris_hfi_device *device;
  1874. int rc = 0;
  1875. if (!session || !session->device) {
  1876. dprintk(CVP_ERR, "Invalid Params %s\n", __func__);
  1877. return -EINVAL;
  1878. }
  1879. device = session->device;
  1880. mutex_lock(&device->lock);
  1881. rc = __send_session_cmd(session, HFI_CMD_SESSION_CVP_FLUSH);
  1882. mutex_unlock(&device->lock);
  1883. return rc;
  1884. }
  1885. static int __check_core_registered(struct iris_hfi_device *device,
  1886. phys_addr_t fw_addr, u8 *reg_addr, u32 reg_size,
  1887. phys_addr_t irq)
  1888. {
  1889. struct cvp_hal_data *cvp_hal_data;
  1890. if (!device) {
  1891. dprintk(CVP_INFO, "no device Registered\n");
  1892. return -EINVAL;
  1893. }
  1894. cvp_hal_data = device->cvp_hal_data;
  1895. if (!cvp_hal_data)
  1896. return -EINVAL;
  1897. if (cvp_hal_data->irq == irq &&
  1898. (CONTAINS(cvp_hal_data->firmware_base,
  1899. FIRMWARE_SIZE, fw_addr) ||
  1900. CONTAINS(fw_addr, FIRMWARE_SIZE,
  1901. cvp_hal_data->firmware_base) ||
  1902. CONTAINS(cvp_hal_data->register_base,
  1903. reg_size, reg_addr) ||
  1904. CONTAINS(reg_addr, reg_size,
  1905. cvp_hal_data->register_base) ||
  1906. OVERLAPS(cvp_hal_data->register_base,
  1907. reg_size, reg_addr, reg_size) ||
  1908. OVERLAPS(reg_addr, reg_size,
  1909. cvp_hal_data->register_base,
  1910. reg_size) ||
  1911. OVERLAPS(cvp_hal_data->firmware_base,
  1912. FIRMWARE_SIZE, fw_addr,
  1913. FIRMWARE_SIZE) ||
  1914. OVERLAPS(fw_addr, FIRMWARE_SIZE,
  1915. cvp_hal_data->firmware_base,
  1916. FIRMWARE_SIZE))) {
  1917. return 0;
  1918. }
  1919. dprintk(CVP_INFO, "Device not registered\n");
  1920. return -EINVAL;
  1921. }
  1922. static void __process_fatal_error(
  1923. struct iris_hfi_device *device)
  1924. {
  1925. struct msm_cvp_cb_cmd_done cmd_done = {0};
  1926. cmd_done.device_id = device->device_id;
  1927. device->callback(HAL_SYS_ERROR, &cmd_done);
  1928. }
  1929. static int __prepare_pc(struct iris_hfi_device *device)
  1930. {
  1931. int rc = 0;
  1932. struct cvp_hfi_cmd_sys_pc_prep_packet pkt;
  1933. rc = call_hfi_pkt_op(device, sys_pc_prep, &pkt);
  1934. if (rc) {
  1935. dprintk(CVP_ERR, "Failed to create sys pc prep pkt\n");
  1936. goto err_pc_prep;
  1937. }
  1938. if (__iface_cmdq_write(device, &pkt))
  1939. rc = -ENOTEMPTY;
  1940. if (rc)
  1941. dprintk(CVP_ERR, "Failed to prepare iris for power off");
  1942. err_pc_prep:
  1943. return rc;
  1944. }
  1945. static void iris_hfi_pm_handler(struct work_struct *work)
  1946. {
  1947. int rc = 0;
  1948. struct msm_cvp_core *core;
  1949. struct iris_hfi_device *device;
  1950. core = list_first_entry(&cvp_driver->cores, struct msm_cvp_core, list);
  1951. if (core)
  1952. device = core->device->hfi_device_data;
  1953. else
  1954. return;
  1955. if (!device) {
  1956. dprintk(CVP_ERR, "%s: NULL device\n", __func__);
  1957. return;
  1958. }
  1959. dprintk(CVP_PWR,
  1960. "Entering %s\n", __func__);
  1961. /*
  1962. * It is ok to check this variable outside the lock since
  1963. * it is being updated in this context only
  1964. */
  1965. if (device->skip_pc_count >= CVP_MAX_PC_SKIP_COUNT) {
  1966. dprintk(CVP_WARN, "Failed to PC for %d times\n",
  1967. device->skip_pc_count);
  1968. device->skip_pc_count = 0;
  1969. __process_fatal_error(device);
  1970. return;
  1971. }
  1972. mutex_lock(&device->lock);
  1973. if (gfa_cv.state == DSP_SUSPEND)
  1974. rc = __power_collapse(device, true);
  1975. else
  1976. rc = __power_collapse(device, false);
  1977. mutex_unlock(&device->lock);
  1978. switch (rc) {
  1979. case 0:
  1980. device->skip_pc_count = 0;
  1981. /* Cancel pending delayed works if any */
  1982. cancel_delayed_work(&iris_hfi_pm_work);
  1983. dprintk(CVP_PWR, "%s: power collapse successful!\n",
  1984. __func__);
  1985. break;
  1986. case -EBUSY:
  1987. device->skip_pc_count = 0;
  1988. dprintk(CVP_PWR, "%s: retry PC as cvp is busy\n", __func__);
  1989. queue_delayed_work(device->iris_pm_workq,
  1990. &iris_hfi_pm_work, msecs_to_jiffies(
  1991. device->res->msm_cvp_pwr_collapse_delay));
  1992. break;
  1993. case -EAGAIN:
  1994. device->skip_pc_count++;
  1995. dprintk(CVP_WARN, "%s: retry power collapse (count %d)\n",
  1996. __func__, device->skip_pc_count);
  1997. queue_delayed_work(device->iris_pm_workq,
  1998. &iris_hfi_pm_work, msecs_to_jiffies(
  1999. device->res->msm_cvp_pwr_collapse_delay));
  2000. break;
  2001. default:
  2002. dprintk(CVP_ERR, "%s: power collapse failed\n", __func__);
  2003. break;
  2004. }
  2005. }
  2006. static int __power_collapse(struct iris_hfi_device *device, bool force)
  2007. {
  2008. int rc = 0;
  2009. u32 wfi_status = 0, idle_status = 0, pc_ready = 0;
  2010. u32 flags = 0;
  2011. int count = 0;
  2012. const int max_tries = 150;
  2013. if (!device) {
  2014. dprintk(CVP_ERR, "%s: invalid params\n", __func__);
  2015. return -EINVAL;
  2016. }
  2017. if (!device->power_enabled) {
  2018. dprintk(CVP_PWR, "%s: Power already disabled\n",
  2019. __func__);
  2020. goto exit;
  2021. }
  2022. rc = __core_in_valid_state(device);
  2023. if (!rc) {
  2024. dprintk(CVP_WARN,
  2025. "Core is in bad state, Skipping power collapse\n");
  2026. return -EINVAL;
  2027. }
  2028. rc = __dsp_suspend(device, force, flags);
  2029. if (rc == -EBUSY)
  2030. goto exit;
  2031. else if (rc)
  2032. goto skip_power_off;
  2033. pc_ready = __read_register(device, CVP_CTRL_STATUS) &
  2034. CVP_CTRL_STATUS_PC_READY;
  2035. if (!pc_ready) {
  2036. wfi_status = __read_register(device,
  2037. CVP_WRAPPER_CPU_STATUS);
  2038. idle_status = __read_register(device,
  2039. CVP_CTRL_STATUS);
  2040. if (!(wfi_status & BIT(0))) {
  2041. dprintk(CVP_WARN,
  2042. "Skipping PC as wfi_status (%#x) bit not set\n",
  2043. wfi_status);
  2044. goto skip_power_off;
  2045. }
  2046. if (!(idle_status & BIT(30))) {
  2047. dprintk(CVP_WARN,
  2048. "Skipping PC as idle_status (%#x) bit not set\n",
  2049. idle_status);
  2050. goto skip_power_off;
  2051. }
  2052. rc = __prepare_pc(device);
  2053. if (rc) {
  2054. dprintk(CVP_WARN, "Failed PC %d\n", rc);
  2055. goto skip_power_off;
  2056. }
  2057. while (count < max_tries) {
  2058. wfi_status = __read_register(device,
  2059. CVP_WRAPPER_CPU_STATUS);
  2060. pc_ready = __read_register(device,
  2061. CVP_CTRL_STATUS);
  2062. if ((wfi_status & BIT(0)) && (pc_ready &
  2063. CVP_CTRL_STATUS_PC_READY))
  2064. break;
  2065. usleep_range(150, 250);
  2066. count++;
  2067. }
  2068. if (count == max_tries) {
  2069. dprintk(CVP_ERR,
  2070. "Skip PC. Core is not in right state (%#x, %#x)\n",
  2071. wfi_status, pc_ready);
  2072. goto skip_power_off;
  2073. }
  2074. }
  2075. __flush_debug_queue(device, device->raw_packet);
  2076. rc = __suspend(device);
  2077. if (rc)
  2078. dprintk(CVP_ERR, "Failed __suspend\n");
  2079. exit:
  2080. return rc;
  2081. skip_power_off:
  2082. dprintk(CVP_PWR, "Skip PC(%#x, %#x, %#x)\n",
  2083. wfi_status, idle_status, pc_ready);
  2084. __flush_debug_queue(device, device->raw_packet);
  2085. return -EAGAIN;
  2086. }
  2087. static void __process_sys_error(struct iris_hfi_device *device)
  2088. {
  2089. struct cvp_hfi_sfr_struct *vsfr = NULL;
  2090. vsfr = (struct cvp_hfi_sfr_struct *)device->sfr.align_virtual_addr;
  2091. if (vsfr) {
  2092. void *p = memchr(vsfr->rg_data, '\0', vsfr->bufSize);
  2093. /*
  2094. * SFR isn't guaranteed to be NULL terminated
  2095. * since SYS_ERROR indicates that Iris is in the
  2096. * process of crashing.
  2097. */
  2098. if (p == NULL)
  2099. vsfr->rg_data[vsfr->bufSize - 1] = '\0';
  2100. dprintk(CVP_ERR, "SFR Message from FW: %s\n",
  2101. vsfr->rg_data);
  2102. }
  2103. }
  2104. static void __flush_debug_queue(struct iris_hfi_device *device, u8 *packet)
  2105. {
  2106. bool local_packet = false;
  2107. enum cvp_msg_prio log_level = CVP_FW;
  2108. if (!device) {
  2109. dprintk(CVP_ERR, "%s: Invalid params\n", __func__);
  2110. return;
  2111. }
  2112. if (!packet) {
  2113. packet = kzalloc(CVP_IFACEQ_VAR_HUGE_PKT_SIZE, GFP_KERNEL);
  2114. if (!packet) {
  2115. dprintk(CVP_ERR, "In %s() Fail to allocate mem\n",
  2116. __func__);
  2117. return;
  2118. }
  2119. local_packet = true;
  2120. /*
  2121. * Local packek is used when something FATAL occurred.
  2122. * It is good to print these logs by default.
  2123. */
  2124. log_level = CVP_ERR;
  2125. }
  2126. #define SKIP_INVALID_PKT(pkt_size, payload_size, pkt_hdr_size) ({ \
  2127. if (pkt_size < pkt_hdr_size || \
  2128. payload_size < MIN_PAYLOAD_SIZE || \
  2129. payload_size > \
  2130. (pkt_size - pkt_hdr_size + sizeof(u8))) { \
  2131. dprintk(CVP_ERR, \
  2132. "%s: invalid msg size - %d\n", \
  2133. __func__, pkt->msg_size); \
  2134. continue; \
  2135. } \
  2136. })
  2137. while (!__iface_dbgq_read(device, packet)) {
  2138. struct cvp_hfi_packet_header *pkt =
  2139. (struct cvp_hfi_packet_header *) packet;
  2140. if (pkt->size < sizeof(struct cvp_hfi_packet_header)) {
  2141. dprintk(CVP_ERR, "Invalid pkt size - %s\n",
  2142. __func__);
  2143. continue;
  2144. }
  2145. if (pkt->packet_type == HFI_MSG_SYS_DEBUG) {
  2146. struct cvp_hfi_msg_sys_debug_packet *pkt =
  2147. (struct cvp_hfi_msg_sys_debug_packet *) packet;
  2148. SKIP_INVALID_PKT(pkt->size,
  2149. pkt->msg_size, sizeof(*pkt));
  2150. /*
  2151. * All fw messages starts with new line character. This
  2152. * causes dprintk to print this message in two lines
  2153. * in the kernel log. Ignoring the first character
  2154. * from the message fixes this to print it in a single
  2155. * line.
  2156. */
  2157. pkt->rg_msg_data[pkt->msg_size-1] = '\0';
  2158. dprintk(log_level, "%s", &pkt->rg_msg_data[1]);
  2159. }
  2160. }
  2161. #undef SKIP_INVALID_PKT
  2162. if (local_packet)
  2163. kfree(packet);
  2164. }
  2165. static bool __is_session_valid(struct iris_hfi_device *device,
  2166. struct cvp_hal_session *session, const char *func)
  2167. {
  2168. struct cvp_hal_session *temp = NULL;
  2169. if (!device || !session)
  2170. goto invalid;
  2171. list_for_each_entry(temp, &device->sess_head, list)
  2172. if (session == temp)
  2173. return true;
  2174. invalid:
  2175. dprintk(CVP_WARN, "%s: device %pK, invalid session %pK\n",
  2176. func, device, session);
  2177. return false;
  2178. }
  2179. static struct cvp_hal_session *__get_session(struct iris_hfi_device *device,
  2180. u32 session_id)
  2181. {
  2182. struct cvp_hal_session *temp = NULL;
  2183. list_for_each_entry(temp, &device->sess_head, list) {
  2184. if (session_id == hash32_ptr(temp))
  2185. return temp;
  2186. }
  2187. return NULL;
  2188. }
  2189. #define _INVALID_MSG_ "Unrecognized MSG (%#x) session (%pK), discarding\n"
  2190. #define _INVALID_STATE_ "Ignore responses from %d to %d invalid state\n"
  2191. #define _DEVFREQ_FAIL_ "Failed to add devfreq device bus %s governor %s: %d\n"
  2192. static void process_system_msg(struct msm_cvp_cb_info *info,
  2193. struct iris_hfi_device *device,
  2194. void *raw_packet)
  2195. {
  2196. struct cvp_hal_sys_init_done sys_init_done = {0};
  2197. switch (info->response_type) {
  2198. case HAL_SYS_ERROR:
  2199. __process_sys_error(device);
  2200. break;
  2201. case HAL_SYS_RELEASE_RESOURCE_DONE:
  2202. dprintk(CVP_CORE, "Received SYS_RELEASE_RESOURCE\n");
  2203. break;
  2204. case HAL_SYS_INIT_DONE:
  2205. dprintk(CVP_CORE, "Received SYS_INIT_DONE\n");
  2206. sys_init_done.capabilities =
  2207. device->sys_init_capabilities;
  2208. cvp_hfi_process_sys_init_done_prop_read(
  2209. (struct cvp_hfi_msg_sys_init_done_packet *)
  2210. raw_packet, &sys_init_done);
  2211. info->response.cmd.data.sys_init_done = sys_init_done;
  2212. break;
  2213. default:
  2214. break;
  2215. }
  2216. }
  2217. static void **get_session_id(struct msm_cvp_cb_info *info)
  2218. {
  2219. void **session_id = NULL;
  2220. /* For session-related packets, validate session */
  2221. switch (info->response_type) {
  2222. case HAL_SESSION_INIT_DONE:
  2223. case HAL_SESSION_END_DONE:
  2224. case HAL_SESSION_ABORT_DONE:
  2225. case HAL_SESSION_STOP_DONE:
  2226. case HAL_SESSION_FLUSH_DONE:
  2227. case HAL_SESSION_SET_BUFFER_DONE:
  2228. case HAL_SESSION_SUSPEND_DONE:
  2229. case HAL_SESSION_RESUME_DONE:
  2230. case HAL_SESSION_SET_PROP_DONE:
  2231. case HAL_SESSION_GET_PROP_DONE:
  2232. case HAL_SESSION_RELEASE_BUFFER_DONE:
  2233. case HAL_SESSION_REGISTER_BUFFER_DONE:
  2234. case HAL_SESSION_UNREGISTER_BUFFER_DONE:
  2235. case HAL_SESSION_PROPERTY_INFO:
  2236. case HAL_SESSION_EVENT_CHANGE:
  2237. case HAL_SESSION_DUMP_NOTIFY:
  2238. session_id = &info->response.cmd.session_id;
  2239. break;
  2240. case HAL_SESSION_ERROR:
  2241. session_id = &info->response.data.session_id;
  2242. break;
  2243. case HAL_RESPONSE_UNUSED:
  2244. default:
  2245. session_id = NULL;
  2246. break;
  2247. }
  2248. return session_id;
  2249. }
  2250. static void print_msg_hdr(void *hdr)
  2251. {
  2252. struct cvp_hfi_msg_session_hdr *new_hdr =
  2253. (struct cvp_hfi_msg_session_hdr *)hdr;
  2254. dprintk(CVP_HFI, "HFI MSG received: %x %x %x %x %x %x %x\n",
  2255. new_hdr->size, new_hdr->packet_type,
  2256. new_hdr->session_id,
  2257. new_hdr->client_data.transaction_id,
  2258. new_hdr->client_data.data1,
  2259. new_hdr->client_data.data2,
  2260. new_hdr->error_type);
  2261. }
  2262. static int __response_handler(struct iris_hfi_device *device)
  2263. {
  2264. struct msm_cvp_cb_info *packets;
  2265. int packet_count = 0;
  2266. u8 *raw_packet = NULL;
  2267. bool requeue_pm_work = true;
  2268. if (!device || device->state != IRIS_STATE_INIT)
  2269. return 0;
  2270. packets = device->response_pkt;
  2271. raw_packet = device->raw_packet;
  2272. if (!raw_packet || !packets) {
  2273. dprintk(CVP_ERR,
  2274. "%s: Invalid args : Res pkt = %pK, Raw pkt = %pK\n",
  2275. __func__, packets, raw_packet);
  2276. return 0;
  2277. }
  2278. if (device->intr_status & CVP_FATAL_INTR_BMSK) {
  2279. struct cvp_hfi_sfr_struct *vsfr = (struct cvp_hfi_sfr_struct *)
  2280. device->sfr.align_virtual_addr;
  2281. struct msm_cvp_cb_info info = {
  2282. .response_type = HAL_SYS_WATCHDOG_TIMEOUT,
  2283. .response.cmd = {
  2284. .device_id = device->device_id,
  2285. }
  2286. };
  2287. if (vsfr)
  2288. dprintk(CVP_ERR, "SFR Message from FW: %s\n",
  2289. vsfr->rg_data);
  2290. if (device->intr_status & CVP_WRAPPER_INTR_MASK_CPU_NOC_BMSK)
  2291. dprintk(CVP_ERR, "Received Xtensa NOC error\n");
  2292. if (device->intr_status & CVP_WRAPPER_INTR_MASK_CORE_NOC_BMSK)
  2293. dprintk(CVP_ERR, "Received CVP core NOC error\n");
  2294. if (device->intr_status & CVP_WRAPPER_INTR_MASK_A2HWD_BMSK)
  2295. dprintk(CVP_ERR, "Received CVP watchdog timeout\n");
  2296. packets[packet_count++] = info;
  2297. goto exit;
  2298. }
  2299. /* Bleed the msg queue dry of packets */
  2300. while (!__iface_msgq_read(device, raw_packet)) {
  2301. void **session_id = NULL;
  2302. struct msm_cvp_cb_info *info = &packets[packet_count++];
  2303. struct cvp_hfi_msg_session_hdr *hdr =
  2304. (struct cvp_hfi_msg_session_hdr *)raw_packet;
  2305. int rc = 0;
  2306. print_msg_hdr(hdr);
  2307. rc = cvp_hfi_process_msg_packet(device->device_id,
  2308. raw_packet, info);
  2309. if (rc) {
  2310. dprintk(CVP_WARN,
  2311. "Corrupt/unknown packet found, discarding\n");
  2312. --packet_count;
  2313. continue;
  2314. } else if (info->response_type == HAL_NO_RESP) {
  2315. --packet_count;
  2316. continue;
  2317. }
  2318. /* Process the packet types that we're interested in */
  2319. process_system_msg(info, device, raw_packet);
  2320. session_id = get_session_id(info);
  2321. /*
  2322. * hfi_process_msg_packet provides a session_id that's a hashed
  2323. * value of struct cvp_hal_session, we need to coerce the hashed
  2324. * value back to pointer that we can use. Ideally, hfi_process\
  2325. * _msg_packet should take care of this, but it doesn't have
  2326. * required information for it
  2327. */
  2328. if (session_id) {
  2329. struct cvp_hal_session *session = NULL;
  2330. if (upper_32_bits((uintptr_t)*session_id) != 0) {
  2331. dprintk(CVP_ERR,
  2332. "Upper 32-bits != 0 for sess_id=%pK\n",
  2333. *session_id);
  2334. }
  2335. session = __get_session(device,
  2336. (u32)(uintptr_t)*session_id);
  2337. if (!session) {
  2338. dprintk(CVP_ERR, _INVALID_MSG_,
  2339. info->response_type,
  2340. *session_id);
  2341. --packet_count;
  2342. continue;
  2343. }
  2344. *session_id = session->session_id;
  2345. }
  2346. if (packet_count >= cvp_max_packets) {
  2347. dprintk(CVP_WARN,
  2348. "Too many packets in message queue!\n");
  2349. break;
  2350. }
  2351. /* do not read packets after sys error packet */
  2352. if (info->response_type == HAL_SYS_ERROR)
  2353. break;
  2354. }
  2355. if (requeue_pm_work && device->res->sw_power_collapsible) {
  2356. cancel_delayed_work(&iris_hfi_pm_work);
  2357. if (!queue_delayed_work(device->iris_pm_workq,
  2358. &iris_hfi_pm_work,
  2359. msecs_to_jiffies(
  2360. device->res->msm_cvp_pwr_collapse_delay))) {
  2361. dprintk(CVP_ERR, "PM work already scheduled\n");
  2362. }
  2363. }
  2364. exit:
  2365. __flush_debug_queue(device, raw_packet);
  2366. return packet_count;
  2367. }
  2368. static void iris_hfi_core_work_handler(struct work_struct *work)
  2369. {
  2370. struct msm_cvp_core *core;
  2371. struct iris_hfi_device *device;
  2372. int num_responses = 0, i = 0;
  2373. u32 intr_status;
  2374. static bool warning_on = true;
  2375. core = list_first_entry(&cvp_driver->cores, struct msm_cvp_core, list);
  2376. if (core)
  2377. device = core->device->hfi_device_data;
  2378. else
  2379. return;
  2380. mutex_lock(&device->lock);
  2381. if (!__core_in_valid_state(device)) {
  2382. if (warning_on) {
  2383. dprintk(CVP_WARN, "%s Core not in init state\n",
  2384. __func__);
  2385. warning_on = false;
  2386. }
  2387. goto err_no_work;
  2388. }
  2389. warning_on = true;
  2390. if (!device->callback) {
  2391. dprintk(CVP_ERR, "No interrupt callback function: %pK\n",
  2392. device);
  2393. goto err_no_work;
  2394. }
  2395. if (__resume(device)) {
  2396. dprintk(CVP_ERR, "%s: Power enable failed\n", __func__);
  2397. goto err_no_work;
  2398. }
  2399. __core_clear_interrupt(device);
  2400. num_responses = __response_handler(device);
  2401. dprintk(CVP_HFI, "%s:: cvp_driver_debug num_responses = %d ",
  2402. __func__, num_responses);
  2403. err_no_work:
  2404. /* Keep the interrupt status before releasing device lock */
  2405. intr_status = device->intr_status;
  2406. mutex_unlock(&device->lock);
  2407. /*
  2408. * Issue the callbacks outside of the locked contex to preserve
  2409. * re-entrancy.
  2410. */
  2411. for (i = 0; !IS_ERR_OR_NULL(device->response_pkt) &&
  2412. i < num_responses; ++i) {
  2413. struct msm_cvp_cb_info *r = &device->response_pkt[i];
  2414. void *rsp = (void *)&r->response;
  2415. if (!__core_in_valid_state(device)) {
  2416. dprintk(CVP_ERR,
  2417. _INVALID_STATE_, (i + 1), num_responses);
  2418. break;
  2419. }
  2420. dprintk(CVP_HFI, "Processing response %d of %d, type %d\n",
  2421. (i + 1), num_responses, r->response_type);
  2422. device->callback(r->response_type, rsp);
  2423. }
  2424. /* We need re-enable the irq which was disabled in ISR handler */
  2425. if (!(intr_status & CVP_WRAPPER_INTR_STATUS_A2HWD_BMSK))
  2426. enable_irq(device->cvp_hal_data->irq);
  2427. /*
  2428. * XXX: Don't add any code beyond here. Reacquiring locks after release
  2429. * it above doesn't guarantee the atomicity that we're aiming for.
  2430. */
  2431. }
  2432. static DECLARE_WORK(iris_hfi_work, iris_hfi_core_work_handler);
  2433. static irqreturn_t iris_hfi_isr(int irq, void *dev)
  2434. {
  2435. struct iris_hfi_device *device = dev;
  2436. disable_irq_nosync(irq);
  2437. queue_work(device->cvp_workq, &iris_hfi_work);
  2438. return IRQ_HANDLED;
  2439. }
  2440. static int __init_regs_and_interrupts(struct iris_hfi_device *device,
  2441. struct msm_cvp_platform_resources *res)
  2442. {
  2443. struct cvp_hal_data *hal = NULL;
  2444. int rc = 0;
  2445. rc = __check_core_registered(device, res->firmware_base,
  2446. (u8 *)(uintptr_t)res->register_base,
  2447. res->register_size, res->irq);
  2448. if (!rc) {
  2449. dprintk(CVP_ERR, "Core present/Already added\n");
  2450. rc = -EEXIST;
  2451. goto err_core_init;
  2452. }
  2453. hal = kzalloc(sizeof(*hal), GFP_KERNEL);
  2454. if (!hal) {
  2455. dprintk(CVP_ERR, "Failed to alloc\n");
  2456. rc = -ENOMEM;
  2457. goto err_core_init;
  2458. }
  2459. hal->irq = res->irq;
  2460. hal->firmware_base = res->firmware_base;
  2461. hal->register_base = devm_ioremap(&res->pdev->dev,
  2462. res->register_base, res->register_size);
  2463. hal->register_size = res->register_size;
  2464. if (!hal->register_base) {
  2465. dprintk(CVP_ERR,
  2466. "could not map reg addr %pa of size %d\n",
  2467. &res->register_base, res->register_size);
  2468. goto error_irq_fail;
  2469. }
  2470. if (res->gcc_reg_base) {
  2471. hal->gcc_reg_base = devm_ioremap(&res->pdev->dev,
  2472. res->gcc_reg_base, res->gcc_reg_size);
  2473. hal->gcc_reg_size = res->gcc_reg_size;
  2474. if (!hal->gcc_reg_base)
  2475. dprintk(CVP_ERR,
  2476. "could not map gcc reg addr %pa of size %d\n",
  2477. &res->gcc_reg_base, res->gcc_reg_size);
  2478. }
  2479. device->cvp_hal_data = hal;
  2480. rc = request_irq(res->irq, iris_hfi_isr, IRQF_TRIGGER_HIGH,
  2481. "msm_cvp", device);
  2482. if (unlikely(rc)) {
  2483. dprintk(CVP_ERR, "() :request_irq failed\n");
  2484. goto error_irq_fail;
  2485. }
  2486. disable_irq_nosync(res->irq);
  2487. dprintk(CVP_INFO,
  2488. "firmware_base = %pa, register_base = %pa, register_size = %d\n",
  2489. &res->firmware_base, &res->register_base,
  2490. res->register_size);
  2491. return rc;
  2492. error_irq_fail:
  2493. kfree(hal);
  2494. err_core_init:
  2495. return rc;
  2496. }
  2497. static int __handle_reset_clk(struct msm_cvp_platform_resources *res,
  2498. int reset_index, enum reset_state state,
  2499. enum power_state pwr_state)
  2500. {
  2501. int rc = 0;
  2502. struct reset_control *rst;
  2503. struct reset_info rst_info;
  2504. struct reset_set *rst_set = &res->reset_set;
  2505. if (!rst_set->reset_tbl)
  2506. return 0;
  2507. rst_info = rst_set->reset_tbl[reset_index];
  2508. rst = rst_info.rst;
  2509. dprintk(CVP_PWR, "reset_clk: name %s reset_state %d rst %pK ps=%d\n",
  2510. rst_set->reset_tbl[reset_index].name, state, rst, pwr_state);
  2511. switch (state) {
  2512. case INIT:
  2513. if (rst)
  2514. goto skip_reset_init;
  2515. rst = devm_reset_control_get(&res->pdev->dev,
  2516. rst_set->reset_tbl[reset_index].name);
  2517. if (IS_ERR(rst))
  2518. rc = PTR_ERR(rst);
  2519. rst_set->reset_tbl[reset_index].rst = rst;
  2520. break;
  2521. case ASSERT:
  2522. if (!rst) {
  2523. rc = PTR_ERR(rst);
  2524. goto failed_to_reset;
  2525. }
  2526. if (pwr_state != rst_info.required_state)
  2527. break;
  2528. rc = reset_control_assert(rst);
  2529. break;
  2530. case DEASSERT:
  2531. if (!rst) {
  2532. rc = PTR_ERR(rst);
  2533. goto failed_to_reset;
  2534. }
  2535. if (pwr_state != rst_info.required_state)
  2536. break;
  2537. rc = reset_control_deassert(rst);
  2538. break;
  2539. default:
  2540. dprintk(CVP_ERR, "Invalid reset request\n");
  2541. if (rc)
  2542. goto failed_to_reset;
  2543. }
  2544. return 0;
  2545. skip_reset_init:
  2546. failed_to_reset:
  2547. return rc;
  2548. }
  2549. static int reset_ahb2axi_bridge(struct iris_hfi_device *device)
  2550. {
  2551. int rc, i;
  2552. enum power_state s;
  2553. if (!device) {
  2554. dprintk(CVP_ERR, "NULL device\n");
  2555. rc = -EINVAL;
  2556. goto failed_to_reset;
  2557. }
  2558. if (device->power_enabled)
  2559. s = CVP_POWER_ON;
  2560. else
  2561. s = CVP_POWER_OFF;
  2562. for (i = 0; i < device->res->reset_set.count; i++) {
  2563. rc = __handle_reset_clk(device->res, i, ASSERT, s);
  2564. if (rc) {
  2565. dprintk(CVP_ERR,
  2566. "failed to assert reset clocks\n");
  2567. goto failed_to_reset;
  2568. }
  2569. /* wait for deassert */
  2570. usleep_range(1000, 1050);
  2571. rc = __handle_reset_clk(device->res, i, DEASSERT, s);
  2572. if (rc) {
  2573. dprintk(CVP_ERR,
  2574. "failed to deassert reset clocks\n");
  2575. goto failed_to_reset;
  2576. }
  2577. }
  2578. return 0;
  2579. failed_to_reset:
  2580. return rc;
  2581. }
  2582. static void __deinit_bus(struct iris_hfi_device *device)
  2583. {
  2584. struct bus_info *bus = NULL;
  2585. if (!device)
  2586. return;
  2587. kfree(device->bus_vote.data);
  2588. device->bus_vote = CVP_DEFAULT_BUS_VOTE;
  2589. iris_hfi_for_each_bus_reverse(device, bus) {
  2590. dev_set_drvdata(bus->dev, NULL);
  2591. icc_put(bus->client);
  2592. bus->client = NULL;
  2593. }
  2594. }
  2595. static int __init_bus(struct iris_hfi_device *device)
  2596. {
  2597. struct bus_info *bus = NULL;
  2598. int rc = 0;
  2599. if (!device)
  2600. return -EINVAL;
  2601. iris_hfi_for_each_bus(device, bus) {
  2602. /*
  2603. * This is stupid, but there's no other easy way to ahold
  2604. * of struct bus_info in iris_hfi_devfreq_*()
  2605. */
  2606. WARN(dev_get_drvdata(bus->dev), "%s's drvdata already set\n",
  2607. dev_name(bus->dev));
  2608. dev_set_drvdata(bus->dev, device);
  2609. bus->client = icc_get(&device->res->pdev->dev,
  2610. bus->master, bus->slave);
  2611. if (IS_ERR_OR_NULL(bus->client)) {
  2612. rc = PTR_ERR(bus->client) ?: -EBADHANDLE;
  2613. dprintk(CVP_ERR, "Failed to register bus %s: %d\n",
  2614. bus->name, rc);
  2615. bus->client = NULL;
  2616. goto err_add_dev;
  2617. }
  2618. }
  2619. return 0;
  2620. err_add_dev:
  2621. __deinit_bus(device);
  2622. return rc;
  2623. }
  2624. static void __deinit_regulators(struct iris_hfi_device *device)
  2625. {
  2626. struct regulator_info *rinfo = NULL;
  2627. iris_hfi_for_each_regulator_reverse(device, rinfo) {
  2628. if (rinfo->regulator) {
  2629. regulator_put(rinfo->regulator);
  2630. rinfo->regulator = NULL;
  2631. }
  2632. }
  2633. }
  2634. static int __init_regulators(struct iris_hfi_device *device)
  2635. {
  2636. int rc = 0;
  2637. struct regulator_info *rinfo = NULL;
  2638. iris_hfi_for_each_regulator(device, rinfo) {
  2639. rinfo->regulator = regulator_get(&device->res->pdev->dev,
  2640. rinfo->name);
  2641. if (IS_ERR_OR_NULL(rinfo->regulator)) {
  2642. rc = PTR_ERR(rinfo->regulator) ?: -EBADHANDLE;
  2643. dprintk(CVP_ERR, "Failed to get regulator: %s\n",
  2644. rinfo->name);
  2645. rinfo->regulator = NULL;
  2646. goto err_reg_get;
  2647. }
  2648. }
  2649. return 0;
  2650. err_reg_get:
  2651. __deinit_regulators(device);
  2652. return rc;
  2653. }
  2654. static void __deinit_subcaches(struct iris_hfi_device *device)
  2655. {
  2656. struct subcache_info *sinfo = NULL;
  2657. if (!device) {
  2658. dprintk(CVP_ERR, "deinit_subcaches: invalid device %pK\n",
  2659. device);
  2660. goto exit;
  2661. }
  2662. if (!is_sys_cache_present(device))
  2663. goto exit;
  2664. iris_hfi_for_each_subcache_reverse(device, sinfo) {
  2665. if (sinfo->subcache) {
  2666. dprintk(CVP_CORE, "deinit_subcaches: %s\n",
  2667. sinfo->name);
  2668. llcc_slice_putd(sinfo->subcache);
  2669. sinfo->subcache = NULL;
  2670. }
  2671. }
  2672. exit:
  2673. return;
  2674. }
  2675. static int __init_subcaches(struct iris_hfi_device *device)
  2676. {
  2677. int rc = 0;
  2678. struct subcache_info *sinfo = NULL;
  2679. if (!device) {
  2680. dprintk(CVP_ERR, "init_subcaches: invalid device %pK\n",
  2681. device);
  2682. return -EINVAL;
  2683. }
  2684. if (!is_sys_cache_present(device))
  2685. return 0;
  2686. iris_hfi_for_each_subcache(device, sinfo) {
  2687. if (!strcmp("cvp", sinfo->name)) {
  2688. sinfo->subcache = llcc_slice_getd(LLCC_CVP);
  2689. } else if (!strcmp("cvpfw", sinfo->name)) {
  2690. sinfo->subcache = llcc_slice_getd(LLCC_CVPFW);
  2691. } else {
  2692. dprintk(CVP_ERR, "Invalid subcache name %s\n",
  2693. sinfo->name);
  2694. }
  2695. if (IS_ERR_OR_NULL(sinfo->subcache)) {
  2696. rc = PTR_ERR(sinfo->subcache) ?
  2697. PTR_ERR(sinfo->subcache) : -EBADHANDLE;
  2698. dprintk(CVP_ERR,
  2699. "init_subcaches: invalid subcache: %s rc %d\n",
  2700. sinfo->name, rc);
  2701. sinfo->subcache = NULL;
  2702. goto err_subcache_get;
  2703. }
  2704. dprintk(CVP_CORE, "init_subcaches: %s\n",
  2705. sinfo->name);
  2706. }
  2707. return 0;
  2708. err_subcache_get:
  2709. __deinit_subcaches(device);
  2710. return rc;
  2711. }
  2712. static int __init_resources(struct iris_hfi_device *device,
  2713. struct msm_cvp_platform_resources *res)
  2714. {
  2715. int i, rc = 0;
  2716. rc = __init_regulators(device);
  2717. if (rc) {
  2718. dprintk(CVP_ERR, "Failed to get all regulators\n");
  2719. return -ENODEV;
  2720. }
  2721. rc = msm_cvp_init_clocks(device);
  2722. if (rc) {
  2723. dprintk(CVP_ERR, "Failed to init clocks\n");
  2724. rc = -ENODEV;
  2725. goto err_init_clocks;
  2726. }
  2727. for (i = 0; i < device->res->reset_set.count; i++) {
  2728. rc = __handle_reset_clk(res, i, INIT, 0);
  2729. if (rc) {
  2730. dprintk(CVP_ERR, "Failed to init reset clocks\n");
  2731. rc = -ENODEV;
  2732. goto err_init_reset_clk;
  2733. }
  2734. }
  2735. rc = __init_bus(device);
  2736. if (rc) {
  2737. dprintk(CVP_ERR, "Failed to init bus: %d\n", rc);
  2738. goto err_init_bus;
  2739. }
  2740. rc = __init_subcaches(device);
  2741. if (rc)
  2742. dprintk(CVP_WARN, "Failed to init subcaches: %d\n", rc);
  2743. device->sys_init_capabilities =
  2744. kzalloc(sizeof(struct msm_cvp_capability)
  2745. * CVP_MAX_SESSIONS, GFP_KERNEL);
  2746. return rc;
  2747. err_init_reset_clk:
  2748. err_init_bus:
  2749. msm_cvp_deinit_clocks(device);
  2750. err_init_clocks:
  2751. __deinit_regulators(device);
  2752. return rc;
  2753. }
  2754. static void __deinit_resources(struct iris_hfi_device *device)
  2755. {
  2756. __deinit_subcaches(device);
  2757. __deinit_bus(device);
  2758. msm_cvp_deinit_clocks(device);
  2759. __deinit_regulators(device);
  2760. kfree(device->sys_init_capabilities);
  2761. device->sys_init_capabilities = NULL;
  2762. }
  2763. static int __disable_regulator_impl(struct regulator_info *rinfo,
  2764. struct iris_hfi_device *device)
  2765. {
  2766. int rc = 0;
  2767. dprintk(CVP_PWR, "Disabling regulator %s\n", rinfo->name);
  2768. /*
  2769. * This call is needed. Driver needs to acquire the control back
  2770. * from HW in order to disable the regualtor. Else the behavior
  2771. * is unknown.
  2772. */
  2773. rc = __acquire_regulator(rinfo, device);
  2774. if (rc) {
  2775. /*
  2776. * This is somewhat fatal, but nothing we can do
  2777. * about it. We can't disable the regulator w/o
  2778. * getting it back under s/w control
  2779. */
  2780. dprintk(CVP_WARN,
  2781. "Failed to acquire control on %s\n",
  2782. rinfo->name);
  2783. goto disable_regulator_failed;
  2784. }
  2785. rc = regulator_disable(rinfo->regulator);
  2786. if (rc) {
  2787. dprintk(CVP_WARN,
  2788. "Failed to disable %s: %d\n",
  2789. rinfo->name, rc);
  2790. goto disable_regulator_failed;
  2791. }
  2792. return 0;
  2793. disable_regulator_failed:
  2794. /* Bring attention to this issue */
  2795. msm_cvp_res_handle_fatal_hw_error(device->res, true);
  2796. return rc;
  2797. }
  2798. static int __enable_hw_power_collapse(struct iris_hfi_device *device)
  2799. {
  2800. int rc = 0;
  2801. if (!msm_cvp_fw_low_power_mode) {
  2802. dprintk(CVP_PWR, "Not enabling hardware power collapse\n");
  2803. return 0;
  2804. }
  2805. rc = __hand_off_regulators(device);
  2806. if (rc)
  2807. dprintk(CVP_WARN,
  2808. "%s : Failed to enable HW power collapse %d\n",
  2809. __func__, rc);
  2810. return rc;
  2811. }
  2812. static int __enable_regulator(struct iris_hfi_device *device,
  2813. const char *name)
  2814. {
  2815. int rc = 0;
  2816. struct regulator_info *rinfo;
  2817. iris_hfi_for_each_regulator(device, rinfo) {
  2818. if (strcmp(rinfo->name, name))
  2819. continue;
  2820. rc = regulator_enable(rinfo->regulator);
  2821. if (rc) {
  2822. dprintk(CVP_ERR, "Failed to enable %s: %d\n",
  2823. rinfo->name, rc);
  2824. return rc;
  2825. }
  2826. if (!regulator_is_enabled(rinfo->regulator)) {
  2827. dprintk(CVP_ERR,"%s: regulator %s not enabled\n",
  2828. __func__, rinfo->name);
  2829. regulator_disable(rinfo->regulator);
  2830. return -EINVAL;
  2831. }
  2832. dprintk(CVP_PWR, "Enabled regulator %s\n", rinfo->name);
  2833. return 0;
  2834. }
  2835. dprintk(CVP_ERR, "regulator %s not found\n");
  2836. return -EINVAL;
  2837. }
  2838. static int __disable_regulator(struct iris_hfi_device *device,
  2839. const char *name)
  2840. {
  2841. struct regulator_info *rinfo;
  2842. iris_hfi_for_each_regulator_reverse(device, rinfo) {
  2843. if (strcmp(rinfo->name, name))
  2844. continue;
  2845. __disable_regulator_impl(rinfo, device);
  2846. dprintk(CVP_PWR, "%s Disabled regulator %s\n", __func__, name);
  2847. return 0;
  2848. }
  2849. dprintk(CVP_ERR, "%s regulator %s not found\n", __func__, name);
  2850. return -EINVAL;
  2851. }
  2852. static int __enable_subcaches(struct iris_hfi_device *device)
  2853. {
  2854. int rc = 0;
  2855. u32 c = 0;
  2856. struct subcache_info *sinfo;
  2857. if (msm_cvp_syscache_disable || !is_sys_cache_present(device))
  2858. return 0;
  2859. /* Activate subcaches */
  2860. iris_hfi_for_each_subcache(device, sinfo) {
  2861. rc = llcc_slice_activate(sinfo->subcache);
  2862. if (rc) {
  2863. dprintk(CVP_WARN, "Failed to activate %s: %d\n",
  2864. sinfo->name, rc);
  2865. msm_cvp_res_handle_fatal_hw_error(device->res, true);
  2866. goto err_activate_fail;
  2867. }
  2868. sinfo->isactive = true;
  2869. dprintk(CVP_CORE, "Activated subcache %s\n", sinfo->name);
  2870. c++;
  2871. }
  2872. dprintk(CVP_CORE, "Activated %d Subcaches to CVP\n", c);
  2873. return 0;
  2874. err_activate_fail:
  2875. __release_subcaches(device);
  2876. __disable_subcaches(device);
  2877. return 0;
  2878. }
  2879. static int __set_subcaches(struct iris_hfi_device *device)
  2880. {
  2881. int rc = 0;
  2882. u32 c = 0;
  2883. struct subcache_info *sinfo;
  2884. u32 resource[CVP_MAX_SUBCACHE_SIZE];
  2885. struct cvp_hfi_resource_syscache_info_type *sc_res_info;
  2886. struct cvp_hfi_resource_subcache_type *sc_res;
  2887. struct cvp_resource_hdr rhdr;
  2888. if (device->res->sys_cache_res_set || msm_cvp_syscache_disable) {
  2889. dprintk(CVP_CORE, "Subcaches already set or disabled\n");
  2890. return 0;
  2891. }
  2892. memset((void *)resource, 0x0, (sizeof(u32) * CVP_MAX_SUBCACHE_SIZE));
  2893. sc_res_info = (struct cvp_hfi_resource_syscache_info_type *)resource;
  2894. sc_res = &(sc_res_info->rg_subcache_entries[0]);
  2895. iris_hfi_for_each_subcache(device, sinfo) {
  2896. if (sinfo->isactive) {
  2897. sc_res[c].size = sinfo->subcache->slice_size;
  2898. sc_res[c].sc_id = sinfo->subcache->slice_id;
  2899. c++;
  2900. }
  2901. }
  2902. /* Set resource to CVP for activated subcaches */
  2903. if (c) {
  2904. dprintk(CVP_CORE, "Setting %d Subcaches\n", c);
  2905. rhdr.resource_handle = sc_res_info; /* cookie */
  2906. rhdr.resource_id = CVP_RESOURCE_SYSCACHE;
  2907. sc_res_info->num_entries = c;
  2908. rc = __core_set_resource(device, &rhdr, (void *)sc_res_info);
  2909. if (rc) {
  2910. dprintk(CVP_WARN, "Failed to set subcaches %d\n", rc);
  2911. goto err_fail_set_subacaches;
  2912. }
  2913. iris_hfi_for_each_subcache(device, sinfo) {
  2914. if (sinfo->isactive)
  2915. sinfo->isset = true;
  2916. }
  2917. dprintk(CVP_CORE, "Set Subcaches done to CVP\n");
  2918. device->res->sys_cache_res_set = true;
  2919. }
  2920. return 0;
  2921. err_fail_set_subacaches:
  2922. __disable_subcaches(device);
  2923. return 0;
  2924. }
  2925. static int __release_subcaches(struct iris_hfi_device *device)
  2926. {
  2927. struct subcache_info *sinfo;
  2928. int rc = 0;
  2929. u32 c = 0;
  2930. u32 resource[CVP_MAX_SUBCACHE_SIZE];
  2931. struct cvp_hfi_resource_syscache_info_type *sc_res_info;
  2932. struct cvp_hfi_resource_subcache_type *sc_res;
  2933. struct cvp_resource_hdr rhdr;
  2934. if (msm_cvp_syscache_disable || !is_sys_cache_present(device))
  2935. return 0;
  2936. memset((void *)resource, 0x0, (sizeof(u32) * CVP_MAX_SUBCACHE_SIZE));
  2937. sc_res_info = (struct cvp_hfi_resource_syscache_info_type *)resource;
  2938. sc_res = &(sc_res_info->rg_subcache_entries[0]);
  2939. /* Release resource command to Iris */
  2940. iris_hfi_for_each_subcache_reverse(device, sinfo) {
  2941. if (sinfo->isset) {
  2942. /* Update the entry */
  2943. sc_res[c].size = sinfo->subcache->slice_size;
  2944. sc_res[c].sc_id = sinfo->subcache->slice_id;
  2945. c++;
  2946. sinfo->isset = false;
  2947. }
  2948. }
  2949. if (c > 0) {
  2950. dprintk(CVP_CORE, "Releasing %d subcaches\n", c);
  2951. rhdr.resource_handle = sc_res_info; /* cookie */
  2952. rhdr.resource_id = CVP_RESOURCE_SYSCACHE;
  2953. rc = __core_release_resource(device, &rhdr);
  2954. if (rc)
  2955. dprintk(CVP_WARN,
  2956. "Failed to release %d subcaches\n", c);
  2957. }
  2958. device->res->sys_cache_res_set = false;
  2959. return 0;
  2960. }
  2961. static int __disable_subcaches(struct iris_hfi_device *device)
  2962. {
  2963. struct subcache_info *sinfo;
  2964. int rc = 0;
  2965. if (msm_cvp_syscache_disable || !is_sys_cache_present(device))
  2966. return 0;
  2967. /* De-activate subcaches */
  2968. iris_hfi_for_each_subcache_reverse(device, sinfo) {
  2969. if (sinfo->isactive) {
  2970. dprintk(CVP_CORE, "De-activate subcache %s\n",
  2971. sinfo->name);
  2972. rc = llcc_slice_deactivate(sinfo->subcache);
  2973. if (rc) {
  2974. dprintk(CVP_WARN,
  2975. "Failed to de-activate %s: %d\n",
  2976. sinfo->name, rc);
  2977. }
  2978. sinfo->isactive = false;
  2979. }
  2980. }
  2981. return 0;
  2982. }
  2983. static void interrupt_init_iris2(struct iris_hfi_device *device)
  2984. {
  2985. u32 mask_val = 0;
  2986. /* All interrupts should be disabled initially 0x1F6 : Reset value */
  2987. mask_val = __read_register(device, CVP_WRAPPER_INTR_MASK);
  2988. /* Write 0 to unmask CPU and WD interrupts */
  2989. mask_val &= ~(CVP_FATAL_INTR_BMSK | CVP_WRAPPER_INTR_MASK_A2HCPU_BMSK);
  2990. __write_register(device, CVP_WRAPPER_INTR_MASK, mask_val);
  2991. dprintk(CVP_REG, "Init irq: reg: %x, mask value %x\n",
  2992. CVP_WRAPPER_INTR_MASK, mask_val);
  2993. }
  2994. static void setup_dsp_uc_memmap_vpu5(struct iris_hfi_device *device)
  2995. {
  2996. /* initialize DSP QTBL & UCREGION with CPU queues */
  2997. __write_register(device, HFI_DSP_QTBL_ADDR,
  2998. (u32)device->dsp_iface_q_table.align_device_addr);
  2999. __write_register(device, HFI_DSP_UC_REGION_ADDR,
  3000. (u32)device->dsp_iface_q_table.align_device_addr);
  3001. __write_register(device, HFI_DSP_UC_REGION_SIZE,
  3002. device->dsp_iface_q_table.mem_data.size);
  3003. }
  3004. static void clock_config_on_enable_vpu5(struct iris_hfi_device *device)
  3005. {
  3006. __write_register(device, CVP_WRAPPER_CPU_CLOCK_CONFIG, 0);
  3007. }
  3008. static int __set_ubwc_config(struct iris_hfi_device *device)
  3009. {
  3010. u8 packet[CVP_IFACEQ_VAR_SMALL_PKT_SIZE];
  3011. int rc = 0;
  3012. struct cvp_hfi_cmd_sys_set_property_packet *pkt =
  3013. (struct cvp_hfi_cmd_sys_set_property_packet *) &packet;
  3014. if (!device->res->ubwc_config)
  3015. return 0;
  3016. rc = call_hfi_pkt_op(device, sys_ubwc_config, pkt,
  3017. device->res->ubwc_config);
  3018. if (rc) {
  3019. dprintk(CVP_WARN,
  3020. "ubwc config setting to FW failed\n");
  3021. rc = -ENOTEMPTY;
  3022. goto fail_to_set_ubwc_config;
  3023. }
  3024. if (__iface_cmdq_write(device, pkt)) {
  3025. rc = -ENOTEMPTY;
  3026. goto fail_to_set_ubwc_config;
  3027. }
  3028. fail_to_set_ubwc_config:
  3029. return rc;
  3030. }
  3031. static int __power_on_controller(struct iris_hfi_device *device)
  3032. {
  3033. int rc = 0;
  3034. rc = __enable_regulator(device, "cvp");
  3035. if (rc) {
  3036. dprintk(CVP_ERR, "Failed to enable ctrler: %d\n", rc);
  3037. return rc;
  3038. }
  3039. rc = call_iris_op(device, reset_ahb2axi_bridge, device);
  3040. if (rc) {
  3041. dprintk(CVP_ERR, "Failed to reset ahb2axi: %d\n", rc);
  3042. goto fail_reset_clks;
  3043. }
  3044. rc = msm_cvp_prepare_enable_clk(device, "gcc_video_axi1");
  3045. if (rc) {
  3046. dprintk(CVP_ERR, "Failed to enable axi1 clk: %d\n", rc);
  3047. goto fail_reset_clks;
  3048. }
  3049. rc = msm_cvp_prepare_enable_clk(device, "cvp_clk");
  3050. if (rc) {
  3051. dprintk(CVP_ERR, "Failed to enable cvp_clk: %d\n", rc);
  3052. goto fail_enable_clk;
  3053. }
  3054. dprintk(CVP_PWR, "EVA controller powered on\n");
  3055. return 0;
  3056. fail_enable_clk:
  3057. msm_cvp_disable_unprepare_clk(device, "gcc_video_axi1");
  3058. fail_reset_clks:
  3059. __disable_regulator(device, "cvp");
  3060. return rc;
  3061. }
  3062. static int __power_on_core(struct iris_hfi_device *device)
  3063. {
  3064. int rc = 0;
  3065. rc = __enable_regulator(device, "cvp-core");
  3066. if (rc) {
  3067. dprintk(CVP_ERR, "Failed to enable core: %d\n", rc);
  3068. return rc;
  3069. }
  3070. rc = msm_cvp_prepare_enable_clk(device, "core_clk");
  3071. if (rc) {
  3072. dprintk(CVP_ERR, "Failed to enable core_clk: %d\n", rc);
  3073. __disable_regulator(device, "cvp-core");
  3074. return rc;
  3075. }
  3076. dprintk(CVP_PWR, "EVA core powered on\n");
  3077. return 0;
  3078. }
  3079. static int __iris_power_on(struct iris_hfi_device *device)
  3080. {
  3081. int rc = 0;
  3082. if (device->power_enabled)
  3083. return 0;
  3084. /* Vote for all hardware resources */
  3085. rc = __vote_buses(device, device->bus_vote.data,
  3086. device->bus_vote.data_count);
  3087. if (rc) {
  3088. dprintk(CVP_ERR, "Failed to vote buses, err: %d\n", rc);
  3089. goto fail_vote_buses;
  3090. }
  3091. rc = __power_on_controller(device);
  3092. if (rc)
  3093. goto fail_enable_controller;
  3094. rc = __power_on_core(device);
  3095. if (rc)
  3096. goto fail_enable_core;
  3097. rc = msm_cvp_scale_clocks(device);
  3098. if (rc) {
  3099. dprintk(CVP_WARN,
  3100. "Failed to scale clocks, perf may regress\n");
  3101. rc = 0;
  3102. } else {
  3103. dprintk(CVP_PWR, "Done with scaling\n");
  3104. }
  3105. /*Do not access registers before this point!*/
  3106. device->power_enabled = true;
  3107. /*
  3108. * Re-program all of the registers that get reset as a result of
  3109. * regulator_disable() and _enable()
  3110. */
  3111. __set_registers(device);
  3112. dprintk(CVP_CORE, "Done with register set\n");
  3113. call_iris_op(device, interrupt_init, device);
  3114. dprintk(CVP_CORE, "Done with interrupt enabling\n");
  3115. device->intr_status = 0;
  3116. enable_irq(device->cvp_hal_data->irq);
  3117. pr_info(CVP_DBG_TAG "cvp (eva) powered on\n", "pwr");
  3118. return 0;
  3119. fail_enable_core:
  3120. __power_off_controller(device);
  3121. fail_enable_controller:
  3122. __unvote_buses(device);
  3123. fail_vote_buses:
  3124. device->power_enabled = false;
  3125. return rc;
  3126. }
  3127. static inline int __suspend(struct iris_hfi_device *device)
  3128. {
  3129. int rc = 0;
  3130. if (!device) {
  3131. dprintk(CVP_ERR, "Invalid params: %pK\n", device);
  3132. return -EINVAL;
  3133. } else if (!device->power_enabled) {
  3134. dprintk(CVP_PWR, "Power already disabled\n");
  3135. return 0;
  3136. }
  3137. dprintk(CVP_PWR, "Entering suspend\n");
  3138. if (device->res->pm_qos_latency_us &&
  3139. cpu_latency_qos_request_active(&device->qos))
  3140. cpu_latency_qos_remove_request(&device->qos);
  3141. rc = __tzbsp_set_cvp_state(TZ_SUBSYS_STATE_SUSPEND);
  3142. if (rc) {
  3143. dprintk(CVP_WARN, "Failed to suspend cvp core %d\n", rc);
  3144. goto err_tzbsp_suspend;
  3145. }
  3146. __disable_subcaches(device);
  3147. call_iris_op(device, power_off, device);
  3148. return rc;
  3149. err_tzbsp_suspend:
  3150. return rc;
  3151. }
  3152. static void __print_sidebandmanager_regs(struct iris_hfi_device *device)
  3153. {
  3154. u32 sbm_ln0_low, axi_cbcr;
  3155. u32 main_sbm_ln0_low = 0xdeadbeef, main_sbm_ln0_high = 0xdeadbeef;
  3156. u32 main_sbm_ln1_high = 0xdeadbeef, cpu_cs_x2rpmh;
  3157. sbm_ln0_low =
  3158. __read_register(device, CVP_NOC_SBM_SENSELN0_LOW);
  3159. cpu_cs_x2rpmh = __read_register(device, CVP_CPU_CS_X2RPMh);
  3160. __write_register(device, CVP_CPU_CS_X2RPMh,
  3161. (cpu_cs_x2rpmh | CVP_CPU_CS_X2RPMh_SWOVERRIDE_BMSK));
  3162. usleep_range(500, 1000);
  3163. cpu_cs_x2rpmh = __read_register(device, CVP_CPU_CS_X2RPMh);
  3164. if (!(cpu_cs_x2rpmh & CVP_CPU_CS_X2RPMh_SWOVERRIDE_BMSK)) {
  3165. dprintk(CVP_WARN,
  3166. "failed set CVP_CPU_CS_X2RPMH mask %x\n",
  3167. cpu_cs_x2rpmh);
  3168. goto exit;
  3169. }
  3170. axi_cbcr = __read_gcc_register(device, CVP_GCC_VIDEO_AXI1_CBCR);
  3171. if (axi_cbcr & 0x80000000) {
  3172. dprintk(CVP_WARN, "failed to turn on AXI clock %x\n",
  3173. axi_cbcr);
  3174. goto exit;
  3175. }
  3176. main_sbm_ln0_low = __read_register(device,
  3177. CVP_NOC_MAIN_SIDEBANDMANAGER_SENSELN0_LOW);
  3178. main_sbm_ln0_high = __read_register(device,
  3179. CVP_NOC_MAIN_SIDEBANDMANAGER_SENSELN0_HIGH);
  3180. main_sbm_ln1_high = __read_register(device,
  3181. CVP_NOC_MAIN_SIDEBANDMANAGER_SENSELN1_HIGH);
  3182. exit:
  3183. cpu_cs_x2rpmh = cpu_cs_x2rpmh & (~CVP_CPU_CS_X2RPMh_SWOVERRIDE_BMSK);
  3184. __write_register(device, CVP_CPU_CS_X2RPMh, cpu_cs_x2rpmh);
  3185. dprintk(CVP_WARN, "Sidebandmanager regs %x %x %x %x %x\n",
  3186. sbm_ln0_low, main_sbm_ln0_low,
  3187. main_sbm_ln0_high, main_sbm_ln1_high,
  3188. cpu_cs_x2rpmh);
  3189. }
  3190. static int __power_off_controller(struct iris_hfi_device *device)
  3191. {
  3192. u32 lpi_status, reg_status = 0, count = 0, max_count = 1000;
  3193. /* HPG 6.2.2 Step 1 */
  3194. __write_register(device, CVP_CPU_CS_X2RPMh, 0x3);
  3195. /* HPG 6.2.2 Step 2, noc to low power */
  3196. __write_register(device, CVP_AON_WRAPPER_MVP_NOC_LPI_CONTROL, 0x1);
  3197. while (!reg_status && count < max_count) {
  3198. lpi_status =
  3199. __read_register(device,
  3200. CVP_AON_WRAPPER_MVP_NOC_LPI_STATUS);
  3201. reg_status = lpi_status & BIT(0);
  3202. /* Wait for noc lpi status to be set */
  3203. usleep_range(50, 100);
  3204. count++;
  3205. }
  3206. dprintk(CVP_PWR,
  3207. "Noc: lpi_status %x noc_status %x (count %d)\n",
  3208. lpi_status, reg_status, count);
  3209. if (count == max_count) {
  3210. u32 pc_ready, wfi_status;
  3211. wfi_status = __read_register(device, CVP_WRAPPER_CPU_STATUS);
  3212. pc_ready = __read_register(device, CVP_CTRL_STATUS);
  3213. dprintk(CVP_WARN,
  3214. "NOC not in qaccept status %x %x %x %x\n",
  3215. reg_status, lpi_status, wfi_status, pc_ready);
  3216. __print_sidebandmanager_regs(device);
  3217. }
  3218. /* HPG 6.2.2 Step 3, debug bridge to low power BYPASSED */
  3219. /* HPG 6.2.2 Step 4, debug bridge to lpi release */
  3220. __write_register(device,
  3221. CVP_WRAPPER_DEBUG_BRIDGE_LPI_CONTROL, 0x0);
  3222. lpi_status = 0x1;
  3223. count = 0;
  3224. while (lpi_status && count < max_count) {
  3225. lpi_status = __read_register(device,
  3226. CVP_WRAPPER_DEBUG_BRIDGE_LPI_STATUS);
  3227. usleep_range(50, 100);
  3228. count++;
  3229. }
  3230. dprintk(CVP_PWR,
  3231. "DBLP Release: lpi_status %d(count %d)\n",
  3232. lpi_status, count);
  3233. if (count == max_count) {
  3234. dprintk(CVP_WARN,
  3235. "DBLP Release: lpi_status %x\n", lpi_status);
  3236. }
  3237. /* HPG 6.2.2 Step 5 */
  3238. msm_cvp_disable_unprepare_clk(device, "cvp_clk");
  3239. /* HPG 6.2.2 Step 6 */
  3240. __disable_regulator(device, "cvp");
  3241. /* HPG 6.2.2 Step 7 */
  3242. msm_cvp_disable_unprepare_clk(device, "gcc_video_axi1");
  3243. return 0;
  3244. }
  3245. static int __power_off_core(struct iris_hfi_device *device)
  3246. {
  3247. u32 config, value = 0, count = 0, warn_flag = 0;
  3248. const u32 max_count = 10;
  3249. value = __read_register(device, CVP_CC_MVS1_GDSCR);
  3250. if (!(value & 0x80000000)) {
  3251. /*
  3252. * Core has been powered off by f/w.
  3253. * Check NOC reset registers to ensure
  3254. * NO outstanding NoC transactions
  3255. */
  3256. value = __read_register(device, CVP_NOC_RESET_ACK);
  3257. if (value) {
  3258. dprintk(CVP_WARN,
  3259. "Core off with NOC RESET ACK non-zero %x\n",
  3260. value);
  3261. __print_sidebandmanager_regs(device);
  3262. }
  3263. __disable_regulator(device, "cvp-core");
  3264. msm_cvp_disable_unprepare_clk(device, "core_clk");
  3265. return 0;
  3266. }
  3267. dprintk(CVP_PWR, "Driver controls Core power off now\n");
  3268. /*
  3269. * check to make sure core clock branch enabled else
  3270. * we cannot read core idle register
  3271. */
  3272. config = __read_register(device, CVP_WRAPPER_CORE_CLOCK_CONFIG);
  3273. if (config) {
  3274. dprintk(CVP_PWR,
  3275. "core clock config not enabled, enable it to access core\n");
  3276. __write_register(device, CVP_WRAPPER_CORE_CLOCK_CONFIG, 0);
  3277. }
  3278. /*
  3279. * add MNoC idle check before collapsing MVS1 per HPG update
  3280. * poll for NoC DMA idle -> HPG 6.2.1
  3281. *
  3282. */
  3283. do {
  3284. value = __read_register(device, CVP_SS_IDLE_STATUS);
  3285. if (value & 0x400000)
  3286. break;
  3287. else
  3288. usleep_range(1000, 2000);
  3289. count++;
  3290. } while (count < max_count);
  3291. if (count == max_count) {
  3292. dprintk(CVP_WARN, "Core fail to go idle %x\n", value);
  3293. warn_flag = 1;
  3294. }
  3295. /* Apply partial reset on MSF interface and wait for ACK */
  3296. __write_register(device, CVP_NOC_RESET_REQ, 0x7);
  3297. count = 0;
  3298. do {
  3299. value = __read_register(device, CVP_NOC_RESET_ACK);
  3300. if ((value & 0x7) == 0x7)
  3301. break;
  3302. else
  3303. usleep_range(100, 200);
  3304. count++;
  3305. } while (count < max_count);
  3306. if (count == max_count) {
  3307. dprintk(CVP_WARN, "Core NoC reset assert failed %x\n", value);
  3308. warn_flag = 1;
  3309. }
  3310. /* De-assert partial reset on MSF interface and wait for ACK */
  3311. __write_register(device, CVP_NOC_RESET_REQ, 0x0);
  3312. count = 0;
  3313. do {
  3314. value = __read_register(device, CVP_NOC_RESET_ACK);
  3315. if ((value & 0x1) == 0x0)
  3316. break;
  3317. else
  3318. usleep_range(100, 200);
  3319. count++;
  3320. } while (count < max_count);
  3321. if (count == max_count) {
  3322. dprintk(CVP_WARN, "Core NoC reset de-assert failed\n");
  3323. warn_flag = 1;
  3324. }
  3325. if (warn_flag)
  3326. __print_sidebandmanager_regs(device);
  3327. /* Reset both sides of 2 ahb2ahb_bridges (TZ and non-TZ) */
  3328. __write_register(device, CVP_AHB_BRIDGE_SYNC_RESET, 0x3);
  3329. __write_register(device, CVP_AHB_BRIDGE_SYNC_RESET, 0x2);
  3330. __write_register(device, CVP_AHB_BRIDGE_SYNC_RESET, 0x0);
  3331. __write_register(device, CVP_WRAPPER_CORE_CLOCK_CONFIG, config);
  3332. __disable_regulator(device, "cvp-core");
  3333. msm_cvp_disable_unprepare_clk(device, "core_clk");
  3334. return 0;
  3335. }
  3336. static void power_off_iris2(struct iris_hfi_device *device)
  3337. {
  3338. if (!device->power_enabled || !device->res->sw_power_collapsible)
  3339. return;
  3340. if (!(device->intr_status & CVP_WRAPPER_INTR_STATUS_A2HWD_BMSK))
  3341. disable_irq_nosync(device->cvp_hal_data->irq);
  3342. device->intr_status = 0;
  3343. __power_off_core(device);
  3344. __power_off_controller(device);
  3345. if (__unvote_buses(device))
  3346. dprintk(CVP_WARN, "Failed to unvote for buses\n");
  3347. /*Do not access registers after this point!*/
  3348. device->power_enabled = false;
  3349. pr_info(CVP_DBG_TAG "cvp (eva) power collapsed\n", "pwr");
  3350. }
  3351. static inline int __resume(struct iris_hfi_device *device)
  3352. {
  3353. int rc = 0;
  3354. u32 flags = 0, reg_gdsc, reg_cbcr;
  3355. if (!device) {
  3356. dprintk(CVP_ERR, "Invalid params: %pK\n", device);
  3357. return -EINVAL;
  3358. } else if (device->power_enabled) {
  3359. goto exit;
  3360. } else if (!__core_in_valid_state(device)) {
  3361. dprintk(CVP_PWR, "iris_hfi_device in deinit state.");
  3362. return -EINVAL;
  3363. }
  3364. dprintk(CVP_PWR, "Resuming from power collapse\n");
  3365. rc = __iris_power_on(device);
  3366. if (rc) {
  3367. dprintk(CVP_ERR, "Failed to power on cvp\n");
  3368. goto err_iris_power_on;
  3369. }
  3370. reg_gdsc = __read_register(device, CVP_CC_MVS1C_GDSCR);
  3371. reg_cbcr = __read_register(device, CVP_CC_MVS1C_CBCR);
  3372. if (!(reg_gdsc & 0x80000000) || (reg_cbcr & 0x80000000))
  3373. dprintk(CVP_ERR, "CVP power on failed gdsc %x cbcr %x\n",
  3374. reg_gdsc, reg_cbcr);
  3375. /* Reboot the firmware */
  3376. rc = __tzbsp_set_cvp_state(TZ_SUBSYS_STATE_RESUME);
  3377. if (rc) {
  3378. dprintk(CVP_ERR, "Failed to resume cvp core %d\n", rc);
  3379. goto err_set_cvp_state;
  3380. }
  3381. __setup_ucregion_memory_map(device);
  3382. /* Wait for boot completion */
  3383. rc = __boot_firmware(device);
  3384. if (rc) {
  3385. dprintk(CVP_ERR, "Failed to reset cvp core\n");
  3386. goto err_reset_core;
  3387. }
  3388. /*
  3389. * Work around for H/W bug, need to reprogram these registers once
  3390. * firmware is out reset
  3391. */
  3392. __set_threshold_registers(device);
  3393. if (device->res->pm_qos_latency_us)
  3394. cpu_latency_qos_add_request(&device->qos,
  3395. device->res->pm_qos_latency_us);
  3396. __sys_set_debug(device, msm_cvp_fw_debug);
  3397. __enable_subcaches(device);
  3398. __set_subcaches(device);
  3399. __dsp_resume(device, flags);
  3400. dprintk(CVP_PWR, "Resumed from power collapse\n");
  3401. exit:
  3402. /* Don't reset skip_pc_count for SYS_PC_PREP cmd */
  3403. if (device->last_packet_type != HFI_CMD_SYS_PC_PREP)
  3404. device->skip_pc_count = 0;
  3405. return rc;
  3406. err_reset_core:
  3407. __tzbsp_set_cvp_state(TZ_SUBSYS_STATE_SUSPEND);
  3408. err_set_cvp_state:
  3409. call_iris_op(device, power_off, device);
  3410. err_iris_power_on:
  3411. dprintk(CVP_ERR, "Failed to resume from power collapse\n");
  3412. return rc;
  3413. }
  3414. static int __load_fw(struct iris_hfi_device *device)
  3415. {
  3416. int rc = 0;
  3417. /* Initialize resources */
  3418. rc = __init_resources(device, device->res);
  3419. if (rc) {
  3420. dprintk(CVP_ERR, "Failed to init resources: %d\n", rc);
  3421. goto fail_init_res;
  3422. }
  3423. rc = __initialize_packetization(device);
  3424. if (rc) {
  3425. dprintk(CVP_ERR, "Failed to initialize packetization\n");
  3426. goto fail_init_pkt;
  3427. }
  3428. rc = __iris_power_on(device);
  3429. if (rc) {
  3430. dprintk(CVP_ERR, "Failed to power on iris in in load_fw\n");
  3431. goto fail_iris_power_on;
  3432. }
  3433. if ((!device->res->use_non_secure_pil && !device->res->firmware_base)
  3434. || device->res->use_non_secure_pil) {
  3435. rc = load_cvp_fw_impl(device);
  3436. if (rc)
  3437. goto fail_load_fw;
  3438. }
  3439. return rc;
  3440. fail_load_fw:
  3441. call_iris_op(device, power_off, device);
  3442. fail_iris_power_on:
  3443. fail_init_pkt:
  3444. __deinit_resources(device);
  3445. fail_init_res:
  3446. return rc;
  3447. }
  3448. static void __unload_fw(struct iris_hfi_device *device)
  3449. {
  3450. if (!device->resources.fw.cookie)
  3451. return;
  3452. cancel_delayed_work(&iris_hfi_pm_work);
  3453. if (device->state != IRIS_STATE_DEINIT)
  3454. flush_workqueue(device->iris_pm_workq);
  3455. unload_cvp_fw_impl(device);
  3456. __interface_queues_release(device);
  3457. call_iris_op(device, power_off, device);
  3458. __deinit_resources(device);
  3459. dprintk(CVP_WARN, "Firmware unloaded\n");
  3460. }
  3461. static int iris_hfi_get_fw_info(void *dev, struct cvp_hal_fw_info *fw_info)
  3462. {
  3463. int i = 0;
  3464. struct iris_hfi_device *device = dev;
  3465. if (!device || !fw_info) {
  3466. dprintk(CVP_ERR,
  3467. "%s Invalid parameter: device = %pK fw_info = %pK\n",
  3468. __func__, device, fw_info);
  3469. return -EINVAL;
  3470. }
  3471. mutex_lock(&device->lock);
  3472. while (cvp_driver->fw_version[i++] != 'V' && i < CVP_VERSION_LENGTH)
  3473. ;
  3474. if (i == CVP_VERSION_LENGTH - 1) {
  3475. dprintk(CVP_WARN, "Iris version string is not proper\n");
  3476. fw_info->version[0] = '\0';
  3477. goto fail_version_string;
  3478. }
  3479. memcpy(&fw_info->version[0], &cvp_driver->fw_version[0],
  3480. CVP_VERSION_LENGTH);
  3481. fw_info->version[CVP_VERSION_LENGTH - 1] = '\0';
  3482. fail_version_string:
  3483. dprintk(CVP_CORE, "F/W version retrieved : %s\n", fw_info->version);
  3484. fw_info->base_addr = device->cvp_hal_data->firmware_base;
  3485. fw_info->register_base = device->res->register_base;
  3486. fw_info->register_size = device->cvp_hal_data->register_size;
  3487. fw_info->irq = device->cvp_hal_data->irq;
  3488. mutex_unlock(&device->lock);
  3489. return 0;
  3490. }
  3491. static int iris_hfi_get_core_capabilities(void *dev)
  3492. {
  3493. dprintk(CVP_CORE, "%s not supported yet!\n", __func__);
  3494. return 0;
  3495. }
  3496. static const char * const mid_names[16] = {
  3497. "CVP_FW",
  3498. "ARP_DATA",
  3499. "CVP_OD_NON_PIXEL",
  3500. "CVP_OD_ORIG_PIXEL",
  3501. "CVP_OD_WR_PIXEL",
  3502. "CVP_MPU_ORIG_PIXEL",
  3503. "CVP_MPU_REF_PIXEL",
  3504. "CVP_MPU_NON_PIXEL",
  3505. "CVP_MPU_DFS",
  3506. "CVP_FDU_NON_PIXEL",
  3507. "CVP_FDU_PIXEL",
  3508. "CVP_ICA_PIXEL",
  3509. "Invalid",
  3510. "Invalid",
  3511. "Invalid",
  3512. "Invalid"
  3513. };
  3514. static void __print_reg_details(u32 val)
  3515. {
  3516. u32 mid, sid;
  3517. mid = (val >> 5) & 0xF;
  3518. sid = (val >> 2) & 0x7;
  3519. dprintk(CVP_ERR, "CVP_NOC_CORE_ERL_MAIN_ERRLOG3_LOW: %#x\n", val);
  3520. dprintk(CVP_ERR, "Sub-client:%s, SID: %d\n", mid_names[mid], sid);
  3521. }
  3522. static void __err_log(bool logging, u32 *data, const char *name, u32 val)
  3523. {
  3524. if (logging)
  3525. *data = val;
  3526. dprintk(CVP_ERR, "%s: %#x\n", name, val);
  3527. }
  3528. static void __noc_error_info_iris2(struct iris_hfi_device *device)
  3529. {
  3530. struct msm_cvp_core *core;
  3531. struct cvp_noc_log *noc_log;
  3532. u32 val = 0, regi, i;
  3533. bool log_required = false;
  3534. core = list_first_entry(&cvp_driver->cores, struct msm_cvp_core, list);
  3535. if (!core->ssr_count && core->resources.max_ssr_allowed > 1)
  3536. log_required = true;
  3537. noc_log = &core->log.noc_log;
  3538. val = __read_register(device, CVP_NOC_ERR_SWID_LOW_OFFS);
  3539. __err_log(log_required, &noc_log->err_ctrl_swid_low,
  3540. "CVP_NOC_ERL_MAIN_SWID_LOW", val);
  3541. val = __read_register(device, CVP_NOC_ERR_SWID_HIGH_OFFS);
  3542. __err_log(log_required, &noc_log->err_ctrl_swid_high,
  3543. "CVP_NOC_ERL_MAIN_SWID_HIGH", val);
  3544. val = __read_register(device, CVP_NOC_ERR_MAINCTL_LOW_OFFS);
  3545. __err_log(log_required, &noc_log->err_ctrl_mainctl_low,
  3546. "CVP_NOC_ERL_MAIN_MAINCTL_LOW", val);
  3547. val = __read_register(device, CVP_NOC_ERR_ERRVLD_LOW_OFFS);
  3548. __err_log(log_required, &noc_log->err_ctrl_errvld_low,
  3549. "CVP_NOC_ERL_MAIN_ERRVLD_LOW", val);
  3550. val = __read_register(device, CVP_NOC_ERR_ERRCLR_LOW_OFFS);
  3551. __err_log(log_required, &noc_log->err_ctrl_errclr_low,
  3552. "CVP_NOC_ERL_MAIN_ERRCLR_LOW", val);
  3553. val = __read_register(device, CVP_NOC_ERR_ERRLOG0_LOW_OFFS);
  3554. __err_log(log_required, &noc_log->err_ctrl_errlog0_low,
  3555. "CVP_NOC_ERL_MAIN_ERRLOG0_LOW", val);
  3556. val = __read_register(device, CVP_NOC_ERR_ERRLOG0_HIGH_OFFS);
  3557. __err_log(log_required, &noc_log->err_ctrl_errlog0_high,
  3558. "CVP_NOC_ERL_MAIN_ERRLOG0_HIGH", val);
  3559. val = __read_register(device, CVP_NOC_ERR_ERRLOG1_LOW_OFFS);
  3560. __err_log(log_required, &noc_log->err_ctrl_errlog1_low,
  3561. "CVP_NOC_ERL_MAIN_ERRLOG1_LOW", val);
  3562. val = __read_register(device, CVP_NOC_ERR_ERRLOG1_HIGH_OFFS);
  3563. __err_log(log_required, &noc_log->err_ctrl_errlog1_high,
  3564. "CVP_NOC_ERL_MAIN_ERRLOG1_HIGH", val);
  3565. val = __read_register(device, CVP_NOC_ERR_ERRLOG2_LOW_OFFS);
  3566. __err_log(log_required, &noc_log->err_ctrl_errlog2_low,
  3567. "CVP_NOC_ERL_MAIN_ERRLOG2_LOW", val);
  3568. val = __read_register(device, CVP_NOC_ERR_ERRLOG2_HIGH_OFFS);
  3569. __err_log(log_required, &noc_log->err_ctrl_errlog2_high,
  3570. "CVP_NOC_ERL_MAIN_ERRLOG2_HIGH", val);
  3571. val = __read_register(device, CVP_NOC_ERR_ERRLOG3_LOW_OFFS);
  3572. __err_log(log_required, &noc_log->err_ctrl_errlog3_low,
  3573. "CVP_NOC_ERL_MAIN_ERRLOG3_LOW", val);
  3574. val = __read_register(device, CVP_NOC_ERR_ERRLOG3_HIGH_OFFS);
  3575. __err_log(log_required, &noc_log->err_ctrl_errlog3_high,
  3576. "CVP_NOC_ERL_MAIN_ERRLOG3_HIGH", val);
  3577. val = __read_register(device, CVP_NOC_CORE_ERR_SWID_LOW_OFFS);
  3578. __err_log(log_required, &noc_log->err_core_swid_low,
  3579. "CVP_NOC__CORE_ERL_MAIN_SWID_LOW", val);
  3580. val = __read_register(device, CVP_NOC_CORE_ERR_SWID_HIGH_OFFS);
  3581. __err_log(log_required, &noc_log->err_core_swid_high,
  3582. "CVP_NOC_CORE_ERL_MAIN_SWID_HIGH", val);
  3583. val = __read_register(device, CVP_NOC_CORE_ERR_MAINCTL_LOW_OFFS);
  3584. __err_log(log_required, &noc_log->err_core_mainctl_low,
  3585. "CVP_NOC_CORE_ERL_MAIN_MAINCTL_LOW", val);
  3586. val = __read_register(device, CVP_NOC_CORE_ERR_ERRVLD_LOW_OFFS);
  3587. __err_log(log_required, &noc_log->err_core_errvld_low,
  3588. "CVP_NOC_CORE_ERL_MAIN_ERRVLD_LOW", val);
  3589. val = __read_register(device, CVP_NOC_CORE_ERR_ERRCLR_LOW_OFFS);
  3590. __err_log(log_required, &noc_log->err_core_errclr_low,
  3591. "CVP_NOC_CORE_ERL_MAIN_ERRCLR_LOW", val);
  3592. val = __read_register(device, CVP_NOC_CORE_ERR_ERRLOG0_LOW_OFFS);
  3593. __err_log(log_required, &noc_log->err_core_errlog0_low,
  3594. "CVP_NOC_CORE_ERL_MAIN_ERRLOG0_LOW", val);
  3595. val = __read_register(device, CVP_NOC_CORE_ERR_ERRLOG0_HIGH_OFFS);
  3596. __err_log(log_required, &noc_log->err_core_errlog0_high,
  3597. "CVP_NOC_CORE_ERL_MAIN_ERRLOG0_HIGH", val);
  3598. val = __read_register(device, CVP_NOC_CORE_ERR_ERRLOG1_LOW_OFFS);
  3599. __err_log(log_required, &noc_log->err_core_errlog1_low,
  3600. "CVP_NOC_CORE_ERL_MAIN_ERRLOG1_LOW", val);
  3601. val = __read_register(device, CVP_NOC_CORE_ERR_ERRLOG1_HIGH_OFFS);
  3602. __err_log(log_required, &noc_log->err_core_errlog1_high,
  3603. "CVP_NOC_CORE_ERL_MAIN_ERRLOG1_HIGH", val);
  3604. val = __read_register(device, CVP_NOC_CORE_ERR_ERRLOG2_LOW_OFFS);
  3605. __err_log(log_required, &noc_log->err_core_errlog2_low,
  3606. "CVP_NOC_CORE_ERL_MAIN_ERRLOG2_LOW", val);
  3607. val = __read_register(device, CVP_NOC_CORE_ERR_ERRLOG2_HIGH_OFFS);
  3608. __err_log(log_required, &noc_log->err_core_errlog2_high,
  3609. "CVP_NOC_CORE_ERL_MAIN_ERRLOG2_HIGH", val);
  3610. val = __read_register(device, CVP_NOC_CORE_ERR_ERRLOG3_LOW_OFFS);
  3611. __err_log(log_required, &noc_log->err_core_errlog3_low,
  3612. "CORE ERRLOG3_LOW, below details", val);
  3613. __print_reg_details(val);
  3614. val = __read_register(device, CVP_NOC_CORE_ERR_ERRLOG3_HIGH_OFFS);
  3615. __err_log(log_required, &noc_log->err_core_errlog3_high,
  3616. "CVP_NOC_CORE_ERL_MAIN_ERRLOG3_HIGH", val);
  3617. #define CVP_SS_CLK_HALT 0x8
  3618. #define CVP_SS_CLK_EN 0xC
  3619. #define CVP_SS_ARP_TEST_BUS_CONTROL 0x700
  3620. #define CVP_SS_ARP_TEST_BUS_REGISTER 0x704
  3621. #define CVP_DMA_TEST_BUS_CONTROL 0x66A0
  3622. #define CVP_DMA_TEST_BUS_REGISTER 0x66A4
  3623. #define CVP_VPU_WRAPPER_CORE_CONFIG 0xB0088
  3624. __write_register(device, CVP_SS_CLK_HALT, 0);
  3625. __write_register(device, CVP_SS_CLK_EN, 0x3f);
  3626. __write_register(device, CVP_VPU_WRAPPER_CORE_CONFIG, 0);
  3627. for (i = 0; i < 15; i++) {
  3628. regi = 0xC0000000 + i;
  3629. __write_register(device, CVP_SS_ARP_TEST_BUS_CONTROL, regi);
  3630. val = __read_register(device, CVP_SS_ARP_TEST_BUS_REGISTER);
  3631. noc_log->arp_test_bus[i] = val;
  3632. dprintk(CVP_ERR, "ARP_CTL:%x - %x\n", regi, val);
  3633. }
  3634. for (i = 0; i < 512; i++) {
  3635. regi = 0x40000000 + i;
  3636. __write_register(device, CVP_DMA_TEST_BUS_CONTROL, regi);
  3637. val = __read_register(device, CVP_DMA_TEST_BUS_REGISTER);
  3638. noc_log->dma_test_bus[i] = val;
  3639. dprintk(CVP_ERR, "DMA_CTL:%x - %x\n", regi, val);
  3640. }
  3641. }
  3642. static int iris_hfi_noc_error_info(void *dev)
  3643. {
  3644. struct iris_hfi_device *device;
  3645. if (!dev) {
  3646. dprintk(CVP_ERR, "%s: null device\n", __func__);
  3647. return -EINVAL;
  3648. }
  3649. device = dev;
  3650. mutex_lock(&device->lock);
  3651. dprintk(CVP_ERR, "%s: non error information\n", __func__);
  3652. call_iris_op(device, noc_error_info, device);
  3653. mutex_unlock(&device->lock);
  3654. return 0;
  3655. }
  3656. static int __initialize_packetization(struct iris_hfi_device *device)
  3657. {
  3658. int rc = 0;
  3659. if (!device || !device->res) {
  3660. dprintk(CVP_ERR, "%s - invalid param\n", __func__);
  3661. return -EINVAL;
  3662. }
  3663. device->packetization_type = HFI_PACKETIZATION_4XX;
  3664. device->pkt_ops = cvp_hfi_get_pkt_ops_handle(
  3665. device->packetization_type);
  3666. if (!device->pkt_ops) {
  3667. rc = -EINVAL;
  3668. dprintk(CVP_ERR, "Failed to get pkt_ops handle\n");
  3669. }
  3670. return rc;
  3671. }
  3672. void __init_cvp_ops(struct iris_hfi_device *device)
  3673. {
  3674. device->vpu_ops = &iris2_ops;
  3675. }
  3676. static struct iris_hfi_device *__add_device(u32 device_id,
  3677. struct msm_cvp_platform_resources *res,
  3678. hfi_cmd_response_callback callback)
  3679. {
  3680. struct iris_hfi_device *hdevice = NULL;
  3681. int rc = 0;
  3682. if (!res || !callback) {
  3683. dprintk(CVP_ERR, "Invalid Parameters\n");
  3684. return NULL;
  3685. }
  3686. dprintk(CVP_INFO, "%s: device_id: %d\n", __func__, device_id);
  3687. hdevice = kzalloc(sizeof(*hdevice), GFP_KERNEL);
  3688. if (!hdevice) {
  3689. dprintk(CVP_ERR, "failed to allocate new device\n");
  3690. goto exit;
  3691. }
  3692. hdevice->response_pkt = kmalloc_array(cvp_max_packets,
  3693. sizeof(*hdevice->response_pkt), GFP_KERNEL);
  3694. if (!hdevice->response_pkt) {
  3695. dprintk(CVP_ERR, "failed to allocate response_pkt\n");
  3696. goto err_cleanup;
  3697. }
  3698. hdevice->raw_packet =
  3699. kzalloc(CVP_IFACEQ_VAR_HUGE_PKT_SIZE, GFP_KERNEL);
  3700. if (!hdevice->raw_packet) {
  3701. dprintk(CVP_ERR, "failed to allocate raw packet\n");
  3702. goto err_cleanup;
  3703. }
  3704. rc = __init_regs_and_interrupts(hdevice, res);
  3705. if (rc)
  3706. goto err_cleanup;
  3707. hdevice->res = res;
  3708. hdevice->device_id = device_id;
  3709. hdevice->callback = callback;
  3710. __init_cvp_ops(hdevice);
  3711. hdevice->cvp_workq = create_singlethread_workqueue(
  3712. "msm_cvp_workerq_iris");
  3713. if (!hdevice->cvp_workq) {
  3714. dprintk(CVP_ERR, ": create cvp workq failed\n");
  3715. goto err_cleanup;
  3716. }
  3717. hdevice->iris_pm_workq = create_singlethread_workqueue(
  3718. "pm_workerq_iris");
  3719. if (!hdevice->iris_pm_workq) {
  3720. dprintk(CVP_ERR, ": create pm workq failed\n");
  3721. goto err_cleanup;
  3722. }
  3723. mutex_init(&hdevice->lock);
  3724. INIT_LIST_HEAD(&hdevice->sess_head);
  3725. return hdevice;
  3726. err_cleanup:
  3727. if (hdevice->iris_pm_workq)
  3728. destroy_workqueue(hdevice->iris_pm_workq);
  3729. if (hdevice->cvp_workq)
  3730. destroy_workqueue(hdevice->cvp_workq);
  3731. kfree(hdevice->response_pkt);
  3732. kfree(hdevice->raw_packet);
  3733. kfree(hdevice);
  3734. exit:
  3735. return NULL;
  3736. }
  3737. static struct iris_hfi_device *__get_device(u32 device_id,
  3738. struct msm_cvp_platform_resources *res,
  3739. hfi_cmd_response_callback callback)
  3740. {
  3741. if (!res || !callback) {
  3742. dprintk(CVP_ERR, "Invalid params: %pK %pK\n", res, callback);
  3743. return NULL;
  3744. }
  3745. return __add_device(device_id, res, callback);
  3746. }
  3747. void cvp_iris_hfi_delete_device(void *device)
  3748. {
  3749. struct msm_cvp_core *core;
  3750. struct iris_hfi_device *dev = NULL;
  3751. if (!device)
  3752. return;
  3753. core = list_first_entry(&cvp_driver->cores, struct msm_cvp_core, list);
  3754. if (core)
  3755. dev = core->device->hfi_device_data;
  3756. if (!dev)
  3757. return;
  3758. mutex_destroy(&dev->lock);
  3759. destroy_workqueue(dev->cvp_workq);
  3760. destroy_workqueue(dev->iris_pm_workq);
  3761. free_irq(dev->cvp_hal_data->irq, dev);
  3762. iounmap(dev->cvp_hal_data->register_base);
  3763. iounmap(dev->cvp_hal_data->gcc_reg_base);
  3764. kfree(dev->cvp_hal_data);
  3765. kfree(dev->response_pkt);
  3766. kfree(dev->raw_packet);
  3767. kfree(dev);
  3768. }
  3769. static int iris_hfi_validate_session(void *sess, const char *func)
  3770. {
  3771. struct cvp_hal_session *session = sess;
  3772. int rc = 0;
  3773. struct iris_hfi_device *device;
  3774. if (!session || !session->device) {
  3775. dprintk(CVP_ERR, " %s Invalid Params %pK\n", __func__, session);
  3776. return -EINVAL;
  3777. }
  3778. device = session->device;
  3779. mutex_lock(&device->lock);
  3780. if (!__is_session_valid(device, session, func))
  3781. rc = -ECONNRESET;
  3782. mutex_unlock(&device->lock);
  3783. return rc;
  3784. }
  3785. static void iris_init_hfi_callbacks(struct cvp_hfi_device *hdev)
  3786. {
  3787. hdev->core_init = iris_hfi_core_init;
  3788. hdev->core_release = iris_hfi_core_release;
  3789. hdev->core_trigger_ssr = iris_hfi_core_trigger_ssr;
  3790. hdev->session_init = iris_hfi_session_init;
  3791. hdev->session_end = iris_hfi_session_end;
  3792. hdev->session_abort = iris_hfi_session_abort;
  3793. hdev->session_clean = iris_hfi_session_clean;
  3794. hdev->session_set_buffers = iris_hfi_session_set_buffers;
  3795. hdev->session_release_buffers = iris_hfi_session_release_buffers;
  3796. hdev->session_send = iris_hfi_session_send;
  3797. hdev->session_flush = iris_hfi_session_flush;
  3798. hdev->scale_clocks = iris_hfi_scale_clocks;
  3799. hdev->vote_bus = iris_hfi_vote_buses;
  3800. hdev->get_fw_info = iris_hfi_get_fw_info;
  3801. hdev->get_core_capabilities = iris_hfi_get_core_capabilities;
  3802. hdev->suspend = iris_hfi_suspend;
  3803. hdev->resume = iris_hfi_resume;
  3804. hdev->flush_debug_queue = iris_hfi_flush_debug_queue;
  3805. hdev->noc_error_info = iris_hfi_noc_error_info;
  3806. hdev->validate_session = iris_hfi_validate_session;
  3807. }
  3808. int cvp_iris_hfi_initialize(struct cvp_hfi_device *hdev, u32 device_id,
  3809. struct msm_cvp_platform_resources *res,
  3810. hfi_cmd_response_callback callback)
  3811. {
  3812. int rc = 0;
  3813. if (!hdev || !res || !callback) {
  3814. dprintk(CVP_ERR, "Invalid params: %pK %pK %pK\n",
  3815. hdev, res, callback);
  3816. rc = -EINVAL;
  3817. goto err_iris_hfi_init;
  3818. }
  3819. hdev->hfi_device_data = __get_device(device_id, res, callback);
  3820. if (IS_ERR_OR_NULL(hdev->hfi_device_data)) {
  3821. rc = PTR_ERR(hdev->hfi_device_data) ?: -EINVAL;
  3822. goto err_iris_hfi_init;
  3823. }
  3824. iris_init_hfi_callbacks(hdev);
  3825. err_iris_hfi_init:
  3826. return rc;
  3827. }