cvp_hfi.c 107 KB

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