irq-gic-v3-its.c 90 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684
  1. /*
  2. * Copyright (C) 2013-2017 ARM Limited, All Rights Reserved.
  3. * Author: Marc Zyngier <marc.zyngier@arm.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <linux/acpi.h>
  18. #include <linux/acpi_iort.h>
  19. #include <linux/bitmap.h>
  20. #include <linux/cpu.h>
  21. #include <linux/delay.h>
  22. #include <linux/dma-iommu.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/irqdomain.h>
  25. #include <linux/log2.h>
  26. #include <linux/mm.h>
  27. #include <linux/msi.h>
  28. #include <linux/of.h>
  29. #include <linux/of_address.h>
  30. #include <linux/of_irq.h>
  31. #include <linux/of_pci.h>
  32. #include <linux/of_platform.h>
  33. #include <linux/percpu.h>
  34. #include <linux/slab.h>
  35. #include <linux/syscore_ops.h>
  36. #include <linux/irqchip.h>
  37. #include <linux/irqchip/arm-gic-v3.h>
  38. #include <linux/irqchip/arm-gic-v4.h>
  39. #include <asm/cputype.h>
  40. #include <asm/exception.h>
  41. #include "irq-gic-common.h"
  42. #define ITS_FLAGS_CMDQ_NEEDS_FLUSHING (1ULL << 0)
  43. #define ITS_FLAGS_WORKAROUND_CAVIUM_22375 (1ULL << 1)
  44. #define ITS_FLAGS_WORKAROUND_CAVIUM_23144 (1ULL << 2)
  45. #define ITS_FLAGS_SAVE_SUSPEND_STATE (1ULL << 3)
  46. #define RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING (1 << 0)
  47. static u32 lpi_id_bits;
  48. /*
  49. * We allocate memory for PROPBASE to cover 2 ^ lpi_id_bits LPIs to
  50. * deal with (one configuration byte per interrupt). PENDBASE has to
  51. * be 64kB aligned (one bit per LPI, plus 8192 bits for SPI/PPI/SGI).
  52. */
  53. #define LPI_NRBITS lpi_id_bits
  54. #define LPI_PROPBASE_SZ ALIGN(BIT(LPI_NRBITS), SZ_64K)
  55. #define LPI_PENDBASE_SZ ALIGN(BIT(LPI_NRBITS) / 8, SZ_64K)
  56. #define LPI_PROP_DEFAULT_PRIO 0xa0
  57. /*
  58. * Collection structure - just an ID, and a redistributor address to
  59. * ping. We use one per CPU as a bag of interrupts assigned to this
  60. * CPU.
  61. */
  62. struct its_collection {
  63. u64 target_address;
  64. u16 col_id;
  65. };
  66. /*
  67. * The ITS_BASER structure - contains memory information, cached
  68. * value of BASER register configuration and ITS page size.
  69. */
  70. struct its_baser {
  71. void *base;
  72. u64 val;
  73. u32 order;
  74. u32 psz;
  75. };
  76. struct its_device;
  77. /*
  78. * The ITS structure - contains most of the infrastructure, with the
  79. * top-level MSI domain, the command queue, the collections, and the
  80. * list of devices writing to it.
  81. */
  82. struct its_node {
  83. raw_spinlock_t lock;
  84. struct list_head entry;
  85. void __iomem *base;
  86. phys_addr_t phys_base;
  87. struct its_cmd_block *cmd_base;
  88. struct its_cmd_block *cmd_write;
  89. struct its_baser tables[GITS_BASER_NR_REGS];
  90. struct its_collection *collections;
  91. struct fwnode_handle *fwnode_handle;
  92. u64 (*get_msi_base)(struct its_device *its_dev);
  93. u64 cbaser_save;
  94. u32 ctlr_save;
  95. struct list_head its_device_list;
  96. u64 flags;
  97. unsigned long list_nr;
  98. u32 ite_size;
  99. u32 device_ids;
  100. int numa_node;
  101. unsigned int msi_domain_flags;
  102. u32 pre_its_base; /* for Socionext Synquacer */
  103. bool is_v4;
  104. int vlpi_redist_offset;
  105. };
  106. #define ITS_ITT_ALIGN SZ_256
  107. /* The maximum number of VPEID bits supported by VLPI commands */
  108. #define ITS_MAX_VPEID_BITS (16)
  109. #define ITS_MAX_VPEID (1 << (ITS_MAX_VPEID_BITS))
  110. /* Convert page order to size in bytes */
  111. #define PAGE_ORDER_TO_SIZE(o) (PAGE_SIZE << (o))
  112. struct event_lpi_map {
  113. unsigned long *lpi_map;
  114. u16 *col_map;
  115. irq_hw_number_t lpi_base;
  116. int nr_lpis;
  117. struct mutex vlpi_lock;
  118. struct its_vm *vm;
  119. struct its_vlpi_map *vlpi_maps;
  120. int nr_vlpis;
  121. };
  122. /*
  123. * The ITS view of a device - belongs to an ITS, owns an interrupt
  124. * translation table, and a list of interrupts. If it some of its
  125. * LPIs are injected into a guest (GICv4), the event_map.vm field
  126. * indicates which one.
  127. */
  128. struct its_device {
  129. struct list_head entry;
  130. struct its_node *its;
  131. struct event_lpi_map event_map;
  132. void *itt;
  133. u32 nr_ites;
  134. u32 device_id;
  135. };
  136. static struct {
  137. raw_spinlock_t lock;
  138. struct its_device *dev;
  139. struct its_vpe **vpes;
  140. int next_victim;
  141. } vpe_proxy;
  142. static LIST_HEAD(its_nodes);
  143. static DEFINE_SPINLOCK(its_lock);
  144. static struct rdists *gic_rdists;
  145. static struct irq_domain *its_parent;
  146. static unsigned long its_list_map;
  147. static u16 vmovp_seq_num;
  148. static DEFINE_RAW_SPINLOCK(vmovp_lock);
  149. static DEFINE_IDA(its_vpeid_ida);
  150. #define gic_data_rdist() (raw_cpu_ptr(gic_rdists->rdist))
  151. #define gic_data_rdist_rd_base() (gic_data_rdist()->rd_base)
  152. #define gic_data_rdist_vlpi_base() (gic_data_rdist_rd_base() + SZ_128K)
  153. static struct its_collection *dev_event_to_col(struct its_device *its_dev,
  154. u32 event)
  155. {
  156. struct its_node *its = its_dev->its;
  157. return its->collections + its_dev->event_map.col_map[event];
  158. }
  159. /*
  160. * ITS command descriptors - parameters to be encoded in a command
  161. * block.
  162. */
  163. struct its_cmd_desc {
  164. union {
  165. struct {
  166. struct its_device *dev;
  167. u32 event_id;
  168. } its_inv_cmd;
  169. struct {
  170. struct its_device *dev;
  171. u32 event_id;
  172. } its_clear_cmd;
  173. struct {
  174. struct its_device *dev;
  175. u32 event_id;
  176. } its_int_cmd;
  177. struct {
  178. struct its_device *dev;
  179. int valid;
  180. } its_mapd_cmd;
  181. struct {
  182. struct its_collection *col;
  183. int valid;
  184. } its_mapc_cmd;
  185. struct {
  186. struct its_device *dev;
  187. u32 phys_id;
  188. u32 event_id;
  189. } its_mapti_cmd;
  190. struct {
  191. struct its_device *dev;
  192. struct its_collection *col;
  193. u32 event_id;
  194. } its_movi_cmd;
  195. struct {
  196. struct its_device *dev;
  197. u32 event_id;
  198. } its_discard_cmd;
  199. struct {
  200. struct its_collection *col;
  201. } its_invall_cmd;
  202. struct {
  203. struct its_vpe *vpe;
  204. } its_vinvall_cmd;
  205. struct {
  206. struct its_vpe *vpe;
  207. struct its_collection *col;
  208. bool valid;
  209. } its_vmapp_cmd;
  210. struct {
  211. struct its_vpe *vpe;
  212. struct its_device *dev;
  213. u32 virt_id;
  214. u32 event_id;
  215. bool db_enabled;
  216. } its_vmapti_cmd;
  217. struct {
  218. struct its_vpe *vpe;
  219. struct its_device *dev;
  220. u32 event_id;
  221. bool db_enabled;
  222. } its_vmovi_cmd;
  223. struct {
  224. struct its_vpe *vpe;
  225. struct its_collection *col;
  226. u16 seq_num;
  227. u16 its_list;
  228. } its_vmovp_cmd;
  229. };
  230. };
  231. /*
  232. * The ITS command block, which is what the ITS actually parses.
  233. */
  234. struct its_cmd_block {
  235. u64 raw_cmd[4];
  236. };
  237. #define ITS_CMD_QUEUE_SZ SZ_64K
  238. #define ITS_CMD_QUEUE_NR_ENTRIES (ITS_CMD_QUEUE_SZ / sizeof(struct its_cmd_block))
  239. typedef struct its_collection *(*its_cmd_builder_t)(struct its_node *,
  240. struct its_cmd_block *,
  241. struct its_cmd_desc *);
  242. typedef struct its_vpe *(*its_cmd_vbuilder_t)(struct its_node *,
  243. struct its_cmd_block *,
  244. struct its_cmd_desc *);
  245. static void its_mask_encode(u64 *raw_cmd, u64 val, int h, int l)
  246. {
  247. u64 mask = GENMASK_ULL(h, l);
  248. *raw_cmd &= ~mask;
  249. *raw_cmd |= (val << l) & mask;
  250. }
  251. static void its_encode_cmd(struct its_cmd_block *cmd, u8 cmd_nr)
  252. {
  253. its_mask_encode(&cmd->raw_cmd[0], cmd_nr, 7, 0);
  254. }
  255. static void its_encode_devid(struct its_cmd_block *cmd, u32 devid)
  256. {
  257. its_mask_encode(&cmd->raw_cmd[0], devid, 63, 32);
  258. }
  259. static void its_encode_event_id(struct its_cmd_block *cmd, u32 id)
  260. {
  261. its_mask_encode(&cmd->raw_cmd[1], id, 31, 0);
  262. }
  263. static void its_encode_phys_id(struct its_cmd_block *cmd, u32 phys_id)
  264. {
  265. its_mask_encode(&cmd->raw_cmd[1], phys_id, 63, 32);
  266. }
  267. static void its_encode_size(struct its_cmd_block *cmd, u8 size)
  268. {
  269. its_mask_encode(&cmd->raw_cmd[1], size, 4, 0);
  270. }
  271. static void its_encode_itt(struct its_cmd_block *cmd, u64 itt_addr)
  272. {
  273. its_mask_encode(&cmd->raw_cmd[2], itt_addr >> 8, 51, 8);
  274. }
  275. static void its_encode_valid(struct its_cmd_block *cmd, int valid)
  276. {
  277. its_mask_encode(&cmd->raw_cmd[2], !!valid, 63, 63);
  278. }
  279. static void its_encode_target(struct its_cmd_block *cmd, u64 target_addr)
  280. {
  281. its_mask_encode(&cmd->raw_cmd[2], target_addr >> 16, 51, 16);
  282. }
  283. static void its_encode_collection(struct its_cmd_block *cmd, u16 col)
  284. {
  285. its_mask_encode(&cmd->raw_cmd[2], col, 15, 0);
  286. }
  287. static void its_encode_vpeid(struct its_cmd_block *cmd, u16 vpeid)
  288. {
  289. its_mask_encode(&cmd->raw_cmd[1], vpeid, 47, 32);
  290. }
  291. static void its_encode_virt_id(struct its_cmd_block *cmd, u32 virt_id)
  292. {
  293. its_mask_encode(&cmd->raw_cmd[2], virt_id, 31, 0);
  294. }
  295. static void its_encode_db_phys_id(struct its_cmd_block *cmd, u32 db_phys_id)
  296. {
  297. its_mask_encode(&cmd->raw_cmd[2], db_phys_id, 63, 32);
  298. }
  299. static void its_encode_db_valid(struct its_cmd_block *cmd, bool db_valid)
  300. {
  301. its_mask_encode(&cmd->raw_cmd[2], db_valid, 0, 0);
  302. }
  303. static void its_encode_seq_num(struct its_cmd_block *cmd, u16 seq_num)
  304. {
  305. its_mask_encode(&cmd->raw_cmd[0], seq_num, 47, 32);
  306. }
  307. static void its_encode_its_list(struct its_cmd_block *cmd, u16 its_list)
  308. {
  309. its_mask_encode(&cmd->raw_cmd[1], its_list, 15, 0);
  310. }
  311. static void its_encode_vpt_addr(struct its_cmd_block *cmd, u64 vpt_pa)
  312. {
  313. its_mask_encode(&cmd->raw_cmd[3], vpt_pa >> 16, 51, 16);
  314. }
  315. static void its_encode_vpt_size(struct its_cmd_block *cmd, u8 vpt_size)
  316. {
  317. its_mask_encode(&cmd->raw_cmd[3], vpt_size, 4, 0);
  318. }
  319. static inline void its_fixup_cmd(struct its_cmd_block *cmd)
  320. {
  321. /* Let's fixup BE commands */
  322. cmd->raw_cmd[0] = cpu_to_le64(cmd->raw_cmd[0]);
  323. cmd->raw_cmd[1] = cpu_to_le64(cmd->raw_cmd[1]);
  324. cmd->raw_cmd[2] = cpu_to_le64(cmd->raw_cmd[2]);
  325. cmd->raw_cmd[3] = cpu_to_le64(cmd->raw_cmd[3]);
  326. }
  327. static struct its_collection *its_build_mapd_cmd(struct its_node *its,
  328. struct its_cmd_block *cmd,
  329. struct its_cmd_desc *desc)
  330. {
  331. unsigned long itt_addr;
  332. u8 size = ilog2(desc->its_mapd_cmd.dev->nr_ites);
  333. itt_addr = virt_to_phys(desc->its_mapd_cmd.dev->itt);
  334. itt_addr = ALIGN(itt_addr, ITS_ITT_ALIGN);
  335. its_encode_cmd(cmd, GITS_CMD_MAPD);
  336. its_encode_devid(cmd, desc->its_mapd_cmd.dev->device_id);
  337. its_encode_size(cmd, size - 1);
  338. its_encode_itt(cmd, itt_addr);
  339. its_encode_valid(cmd, desc->its_mapd_cmd.valid);
  340. its_fixup_cmd(cmd);
  341. return NULL;
  342. }
  343. static struct its_collection *its_build_mapc_cmd(struct its_node *its,
  344. struct its_cmd_block *cmd,
  345. struct its_cmd_desc *desc)
  346. {
  347. its_encode_cmd(cmd, GITS_CMD_MAPC);
  348. its_encode_collection(cmd, desc->its_mapc_cmd.col->col_id);
  349. its_encode_target(cmd, desc->its_mapc_cmd.col->target_address);
  350. its_encode_valid(cmd, desc->its_mapc_cmd.valid);
  351. its_fixup_cmd(cmd);
  352. return desc->its_mapc_cmd.col;
  353. }
  354. static struct its_collection *its_build_mapti_cmd(struct its_node *its,
  355. struct its_cmd_block *cmd,
  356. struct its_cmd_desc *desc)
  357. {
  358. struct its_collection *col;
  359. col = dev_event_to_col(desc->its_mapti_cmd.dev,
  360. desc->its_mapti_cmd.event_id);
  361. its_encode_cmd(cmd, GITS_CMD_MAPTI);
  362. its_encode_devid(cmd, desc->its_mapti_cmd.dev->device_id);
  363. its_encode_event_id(cmd, desc->its_mapti_cmd.event_id);
  364. its_encode_phys_id(cmd, desc->its_mapti_cmd.phys_id);
  365. its_encode_collection(cmd, col->col_id);
  366. its_fixup_cmd(cmd);
  367. return col;
  368. }
  369. static struct its_collection *its_build_movi_cmd(struct its_node *its,
  370. struct its_cmd_block *cmd,
  371. struct its_cmd_desc *desc)
  372. {
  373. struct its_collection *col;
  374. col = dev_event_to_col(desc->its_movi_cmd.dev,
  375. desc->its_movi_cmd.event_id);
  376. its_encode_cmd(cmd, GITS_CMD_MOVI);
  377. its_encode_devid(cmd, desc->its_movi_cmd.dev->device_id);
  378. its_encode_event_id(cmd, desc->its_movi_cmd.event_id);
  379. its_encode_collection(cmd, desc->its_movi_cmd.col->col_id);
  380. its_fixup_cmd(cmd);
  381. return col;
  382. }
  383. static struct its_collection *its_build_discard_cmd(struct its_node *its,
  384. struct its_cmd_block *cmd,
  385. struct its_cmd_desc *desc)
  386. {
  387. struct its_collection *col;
  388. col = dev_event_to_col(desc->its_discard_cmd.dev,
  389. desc->its_discard_cmd.event_id);
  390. its_encode_cmd(cmd, GITS_CMD_DISCARD);
  391. its_encode_devid(cmd, desc->its_discard_cmd.dev->device_id);
  392. its_encode_event_id(cmd, desc->its_discard_cmd.event_id);
  393. its_fixup_cmd(cmd);
  394. return col;
  395. }
  396. static struct its_collection *its_build_inv_cmd(struct its_node *its,
  397. struct its_cmd_block *cmd,
  398. struct its_cmd_desc *desc)
  399. {
  400. struct its_collection *col;
  401. col = dev_event_to_col(desc->its_inv_cmd.dev,
  402. desc->its_inv_cmd.event_id);
  403. its_encode_cmd(cmd, GITS_CMD_INV);
  404. its_encode_devid(cmd, desc->its_inv_cmd.dev->device_id);
  405. its_encode_event_id(cmd, desc->its_inv_cmd.event_id);
  406. its_fixup_cmd(cmd);
  407. return col;
  408. }
  409. static struct its_collection *its_build_int_cmd(struct its_node *its,
  410. struct its_cmd_block *cmd,
  411. struct its_cmd_desc *desc)
  412. {
  413. struct its_collection *col;
  414. col = dev_event_to_col(desc->its_int_cmd.dev,
  415. desc->its_int_cmd.event_id);
  416. its_encode_cmd(cmd, GITS_CMD_INT);
  417. its_encode_devid(cmd, desc->its_int_cmd.dev->device_id);
  418. its_encode_event_id(cmd, desc->its_int_cmd.event_id);
  419. its_fixup_cmd(cmd);
  420. return col;
  421. }
  422. static struct its_collection *its_build_clear_cmd(struct its_node *its,
  423. struct its_cmd_block *cmd,
  424. struct its_cmd_desc *desc)
  425. {
  426. struct its_collection *col;
  427. col = dev_event_to_col(desc->its_clear_cmd.dev,
  428. desc->its_clear_cmd.event_id);
  429. its_encode_cmd(cmd, GITS_CMD_CLEAR);
  430. its_encode_devid(cmd, desc->its_clear_cmd.dev->device_id);
  431. its_encode_event_id(cmd, desc->its_clear_cmd.event_id);
  432. its_fixup_cmd(cmd);
  433. return col;
  434. }
  435. static struct its_collection *its_build_invall_cmd(struct its_node *its,
  436. struct its_cmd_block *cmd,
  437. struct its_cmd_desc *desc)
  438. {
  439. its_encode_cmd(cmd, GITS_CMD_INVALL);
  440. its_encode_collection(cmd, desc->its_mapc_cmd.col->col_id);
  441. its_fixup_cmd(cmd);
  442. return NULL;
  443. }
  444. static struct its_vpe *its_build_vinvall_cmd(struct its_node *its,
  445. struct its_cmd_block *cmd,
  446. struct its_cmd_desc *desc)
  447. {
  448. its_encode_cmd(cmd, GITS_CMD_VINVALL);
  449. its_encode_vpeid(cmd, desc->its_vinvall_cmd.vpe->vpe_id);
  450. its_fixup_cmd(cmd);
  451. return desc->its_vinvall_cmd.vpe;
  452. }
  453. static struct its_vpe *its_build_vmapp_cmd(struct its_node *its,
  454. struct its_cmd_block *cmd,
  455. struct its_cmd_desc *desc)
  456. {
  457. unsigned long vpt_addr;
  458. u64 target;
  459. vpt_addr = virt_to_phys(page_address(desc->its_vmapp_cmd.vpe->vpt_page));
  460. target = desc->its_vmapp_cmd.col->target_address + its->vlpi_redist_offset;
  461. its_encode_cmd(cmd, GITS_CMD_VMAPP);
  462. its_encode_vpeid(cmd, desc->its_vmapp_cmd.vpe->vpe_id);
  463. its_encode_valid(cmd, desc->its_vmapp_cmd.valid);
  464. its_encode_target(cmd, target);
  465. its_encode_vpt_addr(cmd, vpt_addr);
  466. its_encode_vpt_size(cmd, LPI_NRBITS - 1);
  467. its_fixup_cmd(cmd);
  468. return desc->its_vmapp_cmd.vpe;
  469. }
  470. static struct its_vpe *its_build_vmapti_cmd(struct its_node *its,
  471. struct its_cmd_block *cmd,
  472. struct its_cmd_desc *desc)
  473. {
  474. u32 db;
  475. if (desc->its_vmapti_cmd.db_enabled)
  476. db = desc->its_vmapti_cmd.vpe->vpe_db_lpi;
  477. else
  478. db = 1023;
  479. its_encode_cmd(cmd, GITS_CMD_VMAPTI);
  480. its_encode_devid(cmd, desc->its_vmapti_cmd.dev->device_id);
  481. its_encode_vpeid(cmd, desc->its_vmapti_cmd.vpe->vpe_id);
  482. its_encode_event_id(cmd, desc->its_vmapti_cmd.event_id);
  483. its_encode_db_phys_id(cmd, db);
  484. its_encode_virt_id(cmd, desc->its_vmapti_cmd.virt_id);
  485. its_fixup_cmd(cmd);
  486. return desc->its_vmapti_cmd.vpe;
  487. }
  488. static struct its_vpe *its_build_vmovi_cmd(struct its_node *its,
  489. struct its_cmd_block *cmd,
  490. struct its_cmd_desc *desc)
  491. {
  492. u32 db;
  493. if (desc->its_vmovi_cmd.db_enabled)
  494. db = desc->its_vmovi_cmd.vpe->vpe_db_lpi;
  495. else
  496. db = 1023;
  497. its_encode_cmd(cmd, GITS_CMD_VMOVI);
  498. its_encode_devid(cmd, desc->its_vmovi_cmd.dev->device_id);
  499. its_encode_vpeid(cmd, desc->its_vmovi_cmd.vpe->vpe_id);
  500. its_encode_event_id(cmd, desc->its_vmovi_cmd.event_id);
  501. its_encode_db_phys_id(cmd, db);
  502. its_encode_db_valid(cmd, true);
  503. its_fixup_cmd(cmd);
  504. return desc->its_vmovi_cmd.vpe;
  505. }
  506. static struct its_vpe *its_build_vmovp_cmd(struct its_node *its,
  507. struct its_cmd_block *cmd,
  508. struct its_cmd_desc *desc)
  509. {
  510. u64 target;
  511. target = desc->its_vmovp_cmd.col->target_address + its->vlpi_redist_offset;
  512. its_encode_cmd(cmd, GITS_CMD_VMOVP);
  513. its_encode_seq_num(cmd, desc->its_vmovp_cmd.seq_num);
  514. its_encode_its_list(cmd, desc->its_vmovp_cmd.its_list);
  515. its_encode_vpeid(cmd, desc->its_vmovp_cmd.vpe->vpe_id);
  516. its_encode_target(cmd, target);
  517. its_fixup_cmd(cmd);
  518. return desc->its_vmovp_cmd.vpe;
  519. }
  520. static u64 its_cmd_ptr_to_offset(struct its_node *its,
  521. struct its_cmd_block *ptr)
  522. {
  523. return (ptr - its->cmd_base) * sizeof(*ptr);
  524. }
  525. static int its_queue_full(struct its_node *its)
  526. {
  527. int widx;
  528. int ridx;
  529. widx = its->cmd_write - its->cmd_base;
  530. ridx = readl_relaxed(its->base + GITS_CREADR) / sizeof(struct its_cmd_block);
  531. /* This is incredibly unlikely to happen, unless the ITS locks up. */
  532. if (((widx + 1) % ITS_CMD_QUEUE_NR_ENTRIES) == ridx)
  533. return 1;
  534. return 0;
  535. }
  536. static struct its_cmd_block *its_allocate_entry(struct its_node *its)
  537. {
  538. struct its_cmd_block *cmd;
  539. u32 count = 1000000; /* 1s! */
  540. while (its_queue_full(its)) {
  541. count--;
  542. if (!count) {
  543. pr_err_ratelimited("ITS queue not draining\n");
  544. return NULL;
  545. }
  546. cpu_relax();
  547. udelay(1);
  548. }
  549. cmd = its->cmd_write++;
  550. /* Handle queue wrapping */
  551. if (its->cmd_write == (its->cmd_base + ITS_CMD_QUEUE_NR_ENTRIES))
  552. its->cmd_write = its->cmd_base;
  553. /* Clear command */
  554. cmd->raw_cmd[0] = 0;
  555. cmd->raw_cmd[1] = 0;
  556. cmd->raw_cmd[2] = 0;
  557. cmd->raw_cmd[3] = 0;
  558. return cmd;
  559. }
  560. static struct its_cmd_block *its_post_commands(struct its_node *its)
  561. {
  562. u64 wr = its_cmd_ptr_to_offset(its, its->cmd_write);
  563. writel_relaxed(wr, its->base + GITS_CWRITER);
  564. return its->cmd_write;
  565. }
  566. static void its_flush_cmd(struct its_node *its, struct its_cmd_block *cmd)
  567. {
  568. /*
  569. * Make sure the commands written to memory are observable by
  570. * the ITS.
  571. */
  572. if (its->flags & ITS_FLAGS_CMDQ_NEEDS_FLUSHING)
  573. gic_flush_dcache_to_poc(cmd, sizeof(*cmd));
  574. else
  575. dsb(ishst);
  576. }
  577. static int its_wait_for_range_completion(struct its_node *its,
  578. struct its_cmd_block *from,
  579. struct its_cmd_block *to)
  580. {
  581. u64 rd_idx, from_idx, to_idx;
  582. u32 count = 1000000; /* 1s! */
  583. from_idx = its_cmd_ptr_to_offset(its, from);
  584. to_idx = its_cmd_ptr_to_offset(its, to);
  585. while (1) {
  586. rd_idx = readl_relaxed(its->base + GITS_CREADR);
  587. /* Direct case */
  588. if (from_idx < to_idx && rd_idx >= to_idx)
  589. break;
  590. /* Wrapped case */
  591. if (from_idx >= to_idx && rd_idx >= to_idx && rd_idx < from_idx)
  592. break;
  593. count--;
  594. if (!count) {
  595. pr_err_ratelimited("ITS queue timeout (%llu %llu %llu)\n",
  596. from_idx, to_idx, rd_idx);
  597. return -1;
  598. }
  599. cpu_relax();
  600. udelay(1);
  601. }
  602. return 0;
  603. }
  604. /* Warning, macro hell follows */
  605. #define BUILD_SINGLE_CMD_FUNC(name, buildtype, synctype, buildfn) \
  606. void name(struct its_node *its, \
  607. buildtype builder, \
  608. struct its_cmd_desc *desc) \
  609. { \
  610. struct its_cmd_block *cmd, *sync_cmd, *next_cmd; \
  611. synctype *sync_obj; \
  612. unsigned long flags; \
  613. \
  614. raw_spin_lock_irqsave(&its->lock, flags); \
  615. \
  616. cmd = its_allocate_entry(its); \
  617. if (!cmd) { /* We're soooooo screewed... */ \
  618. raw_spin_unlock_irqrestore(&its->lock, flags); \
  619. return; \
  620. } \
  621. sync_obj = builder(its, cmd, desc); \
  622. its_flush_cmd(its, cmd); \
  623. \
  624. if (sync_obj) { \
  625. sync_cmd = its_allocate_entry(its); \
  626. if (!sync_cmd) \
  627. goto post; \
  628. \
  629. buildfn(its, sync_cmd, sync_obj); \
  630. its_flush_cmd(its, sync_cmd); \
  631. } \
  632. \
  633. post: \
  634. next_cmd = its_post_commands(its); \
  635. raw_spin_unlock_irqrestore(&its->lock, flags); \
  636. \
  637. if (its_wait_for_range_completion(its, cmd, next_cmd)) \
  638. pr_err_ratelimited("ITS cmd %ps failed\n", builder); \
  639. }
  640. static void its_build_sync_cmd(struct its_node *its,
  641. struct its_cmd_block *sync_cmd,
  642. struct its_collection *sync_col)
  643. {
  644. its_encode_cmd(sync_cmd, GITS_CMD_SYNC);
  645. its_encode_target(sync_cmd, sync_col->target_address);
  646. its_fixup_cmd(sync_cmd);
  647. }
  648. static BUILD_SINGLE_CMD_FUNC(its_send_single_command, its_cmd_builder_t,
  649. struct its_collection, its_build_sync_cmd)
  650. static void its_build_vsync_cmd(struct its_node *its,
  651. struct its_cmd_block *sync_cmd,
  652. struct its_vpe *sync_vpe)
  653. {
  654. its_encode_cmd(sync_cmd, GITS_CMD_VSYNC);
  655. its_encode_vpeid(sync_cmd, sync_vpe->vpe_id);
  656. its_fixup_cmd(sync_cmd);
  657. }
  658. static BUILD_SINGLE_CMD_FUNC(its_send_single_vcommand, its_cmd_vbuilder_t,
  659. struct its_vpe, its_build_vsync_cmd)
  660. static void its_send_int(struct its_device *dev, u32 event_id)
  661. {
  662. struct its_cmd_desc desc;
  663. desc.its_int_cmd.dev = dev;
  664. desc.its_int_cmd.event_id = event_id;
  665. its_send_single_command(dev->its, its_build_int_cmd, &desc);
  666. }
  667. static void its_send_clear(struct its_device *dev, u32 event_id)
  668. {
  669. struct its_cmd_desc desc;
  670. desc.its_clear_cmd.dev = dev;
  671. desc.its_clear_cmd.event_id = event_id;
  672. its_send_single_command(dev->its, its_build_clear_cmd, &desc);
  673. }
  674. static void its_send_inv(struct its_device *dev, u32 event_id)
  675. {
  676. struct its_cmd_desc desc;
  677. desc.its_inv_cmd.dev = dev;
  678. desc.its_inv_cmd.event_id = event_id;
  679. its_send_single_command(dev->its, its_build_inv_cmd, &desc);
  680. }
  681. static void its_send_mapd(struct its_device *dev, int valid)
  682. {
  683. struct its_cmd_desc desc;
  684. desc.its_mapd_cmd.dev = dev;
  685. desc.its_mapd_cmd.valid = !!valid;
  686. its_send_single_command(dev->its, its_build_mapd_cmd, &desc);
  687. }
  688. static void its_send_mapc(struct its_node *its, struct its_collection *col,
  689. int valid)
  690. {
  691. struct its_cmd_desc desc;
  692. desc.its_mapc_cmd.col = col;
  693. desc.its_mapc_cmd.valid = !!valid;
  694. its_send_single_command(its, its_build_mapc_cmd, &desc);
  695. }
  696. static void its_send_mapti(struct its_device *dev, u32 irq_id, u32 id)
  697. {
  698. struct its_cmd_desc desc;
  699. desc.its_mapti_cmd.dev = dev;
  700. desc.its_mapti_cmd.phys_id = irq_id;
  701. desc.its_mapti_cmd.event_id = id;
  702. its_send_single_command(dev->its, its_build_mapti_cmd, &desc);
  703. }
  704. static void its_send_movi(struct its_device *dev,
  705. struct its_collection *col, u32 id)
  706. {
  707. struct its_cmd_desc desc;
  708. desc.its_movi_cmd.dev = dev;
  709. desc.its_movi_cmd.col = col;
  710. desc.its_movi_cmd.event_id = id;
  711. its_send_single_command(dev->its, its_build_movi_cmd, &desc);
  712. }
  713. static void its_send_discard(struct its_device *dev, u32 id)
  714. {
  715. struct its_cmd_desc desc;
  716. desc.its_discard_cmd.dev = dev;
  717. desc.its_discard_cmd.event_id = id;
  718. its_send_single_command(dev->its, its_build_discard_cmd, &desc);
  719. }
  720. static void its_send_invall(struct its_node *its, struct its_collection *col)
  721. {
  722. struct its_cmd_desc desc;
  723. desc.its_invall_cmd.col = col;
  724. its_send_single_command(its, its_build_invall_cmd, &desc);
  725. }
  726. static void its_send_vmapti(struct its_device *dev, u32 id)
  727. {
  728. struct its_vlpi_map *map = &dev->event_map.vlpi_maps[id];
  729. struct its_cmd_desc desc;
  730. desc.its_vmapti_cmd.vpe = map->vpe;
  731. desc.its_vmapti_cmd.dev = dev;
  732. desc.its_vmapti_cmd.virt_id = map->vintid;
  733. desc.its_vmapti_cmd.event_id = id;
  734. desc.its_vmapti_cmd.db_enabled = map->db_enabled;
  735. its_send_single_vcommand(dev->its, its_build_vmapti_cmd, &desc);
  736. }
  737. static void its_send_vmovi(struct its_device *dev, u32 id)
  738. {
  739. struct its_vlpi_map *map = &dev->event_map.vlpi_maps[id];
  740. struct its_cmd_desc desc;
  741. desc.its_vmovi_cmd.vpe = map->vpe;
  742. desc.its_vmovi_cmd.dev = dev;
  743. desc.its_vmovi_cmd.event_id = id;
  744. desc.its_vmovi_cmd.db_enabled = map->db_enabled;
  745. its_send_single_vcommand(dev->its, its_build_vmovi_cmd, &desc);
  746. }
  747. static void its_send_vmapp(struct its_node *its,
  748. struct its_vpe *vpe, bool valid)
  749. {
  750. struct its_cmd_desc desc;
  751. desc.its_vmapp_cmd.vpe = vpe;
  752. desc.its_vmapp_cmd.valid = valid;
  753. desc.its_vmapp_cmd.col = &its->collections[vpe->col_idx];
  754. its_send_single_vcommand(its, its_build_vmapp_cmd, &desc);
  755. }
  756. static void its_send_vmovp(struct its_vpe *vpe)
  757. {
  758. struct its_cmd_desc desc;
  759. struct its_node *its;
  760. unsigned long flags;
  761. int col_id = vpe->col_idx;
  762. desc.its_vmovp_cmd.vpe = vpe;
  763. desc.its_vmovp_cmd.its_list = (u16)its_list_map;
  764. if (!its_list_map) {
  765. its = list_first_entry(&its_nodes, struct its_node, entry);
  766. desc.its_vmovp_cmd.seq_num = 0;
  767. desc.its_vmovp_cmd.col = &its->collections[col_id];
  768. its_send_single_vcommand(its, its_build_vmovp_cmd, &desc);
  769. return;
  770. }
  771. /*
  772. * Yet another marvel of the architecture. If using the
  773. * its_list "feature", we need to make sure that all ITSs
  774. * receive all VMOVP commands in the same order. The only way
  775. * to guarantee this is to make vmovp a serialization point.
  776. *
  777. * Wall <-- Head.
  778. */
  779. raw_spin_lock_irqsave(&vmovp_lock, flags);
  780. desc.its_vmovp_cmd.seq_num = vmovp_seq_num++;
  781. /* Emit VMOVPs */
  782. list_for_each_entry(its, &its_nodes, entry) {
  783. if (!its->is_v4)
  784. continue;
  785. if (!vpe->its_vm->vlpi_count[its->list_nr])
  786. continue;
  787. desc.its_vmovp_cmd.col = &its->collections[col_id];
  788. its_send_single_vcommand(its, its_build_vmovp_cmd, &desc);
  789. }
  790. raw_spin_unlock_irqrestore(&vmovp_lock, flags);
  791. }
  792. static void its_send_vinvall(struct its_node *its, struct its_vpe *vpe)
  793. {
  794. struct its_cmd_desc desc;
  795. desc.its_vinvall_cmd.vpe = vpe;
  796. its_send_single_vcommand(its, its_build_vinvall_cmd, &desc);
  797. }
  798. /*
  799. * irqchip functions - assumes MSI, mostly.
  800. */
  801. static inline u32 its_get_event_id(struct irq_data *d)
  802. {
  803. struct its_device *its_dev = irq_data_get_irq_chip_data(d);
  804. return d->hwirq - its_dev->event_map.lpi_base;
  805. }
  806. static void lpi_write_config(struct irq_data *d, u8 clr, u8 set)
  807. {
  808. irq_hw_number_t hwirq;
  809. struct page *prop_page;
  810. u8 *cfg;
  811. if (irqd_is_forwarded_to_vcpu(d)) {
  812. struct its_device *its_dev = irq_data_get_irq_chip_data(d);
  813. u32 event = its_get_event_id(d);
  814. struct its_vlpi_map *map;
  815. prop_page = its_dev->event_map.vm->vprop_page;
  816. map = &its_dev->event_map.vlpi_maps[event];
  817. hwirq = map->vintid;
  818. /* Remember the updated property */
  819. map->properties &= ~clr;
  820. map->properties |= set | LPI_PROP_GROUP1;
  821. } else {
  822. prop_page = gic_rdists->prop_page;
  823. hwirq = d->hwirq;
  824. }
  825. cfg = page_address(prop_page) + hwirq - 8192;
  826. *cfg &= ~clr;
  827. *cfg |= set | LPI_PROP_GROUP1;
  828. /*
  829. * Make the above write visible to the redistributors.
  830. * And yes, we're flushing exactly: One. Single. Byte.
  831. * Humpf...
  832. */
  833. if (gic_rdists->flags & RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING)
  834. gic_flush_dcache_to_poc(cfg, sizeof(*cfg));
  835. else
  836. dsb(ishst);
  837. }
  838. static void lpi_update_config(struct irq_data *d, u8 clr, u8 set)
  839. {
  840. struct its_device *its_dev = irq_data_get_irq_chip_data(d);
  841. lpi_write_config(d, clr, set);
  842. its_send_inv(its_dev, its_get_event_id(d));
  843. }
  844. static void its_vlpi_set_doorbell(struct irq_data *d, bool enable)
  845. {
  846. struct its_device *its_dev = irq_data_get_irq_chip_data(d);
  847. u32 event = its_get_event_id(d);
  848. if (its_dev->event_map.vlpi_maps[event].db_enabled == enable)
  849. return;
  850. its_dev->event_map.vlpi_maps[event].db_enabled = enable;
  851. /*
  852. * More fun with the architecture:
  853. *
  854. * Ideally, we'd issue a VMAPTI to set the doorbell to its LPI
  855. * value or to 1023, depending on the enable bit. But that
  856. * would be issueing a mapping for an /existing/ DevID+EventID
  857. * pair, which is UNPREDICTABLE. Instead, let's issue a VMOVI
  858. * to the /same/ vPE, using this opportunity to adjust the
  859. * doorbell. Mouahahahaha. We loves it, Precious.
  860. */
  861. its_send_vmovi(its_dev, event);
  862. }
  863. static void its_mask_irq(struct irq_data *d)
  864. {
  865. if (irqd_is_forwarded_to_vcpu(d))
  866. its_vlpi_set_doorbell(d, false);
  867. lpi_update_config(d, LPI_PROP_ENABLED, 0);
  868. }
  869. static void its_unmask_irq(struct irq_data *d)
  870. {
  871. if (irqd_is_forwarded_to_vcpu(d))
  872. its_vlpi_set_doorbell(d, true);
  873. lpi_update_config(d, 0, LPI_PROP_ENABLED);
  874. }
  875. static int its_set_affinity(struct irq_data *d, const struct cpumask *mask_val,
  876. bool force)
  877. {
  878. unsigned int cpu;
  879. const struct cpumask *cpu_mask = cpu_online_mask;
  880. struct its_device *its_dev = irq_data_get_irq_chip_data(d);
  881. struct its_collection *target_col;
  882. u32 id = its_get_event_id(d);
  883. /* A forwarded interrupt should use irq_set_vcpu_affinity */
  884. if (irqd_is_forwarded_to_vcpu(d))
  885. return -EINVAL;
  886. /* lpi cannot be routed to a redistributor that is on a foreign node */
  887. if (its_dev->its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_23144) {
  888. if (its_dev->its->numa_node >= 0) {
  889. cpu_mask = cpumask_of_node(its_dev->its->numa_node);
  890. if (!cpumask_intersects(mask_val, cpu_mask))
  891. return -EINVAL;
  892. }
  893. }
  894. cpu = cpumask_any_and(mask_val, cpu_mask);
  895. if (cpu >= nr_cpu_ids)
  896. return -EINVAL;
  897. /* don't set the affinity when the target cpu is same as current one */
  898. if (cpu != its_dev->event_map.col_map[id]) {
  899. target_col = &its_dev->its->collections[cpu];
  900. its_send_movi(its_dev, target_col, id);
  901. its_dev->event_map.col_map[id] = cpu;
  902. irq_data_update_effective_affinity(d, cpumask_of(cpu));
  903. }
  904. return IRQ_SET_MASK_OK_DONE;
  905. }
  906. static u64 its_irq_get_msi_base(struct its_device *its_dev)
  907. {
  908. struct its_node *its = its_dev->its;
  909. return its->phys_base + GITS_TRANSLATER;
  910. }
  911. static void its_irq_compose_msi_msg(struct irq_data *d, struct msi_msg *msg)
  912. {
  913. struct its_device *its_dev = irq_data_get_irq_chip_data(d);
  914. struct its_node *its;
  915. u64 addr;
  916. its = its_dev->its;
  917. addr = its->get_msi_base(its_dev);
  918. msg->address_lo = lower_32_bits(addr);
  919. msg->address_hi = upper_32_bits(addr);
  920. msg->data = its_get_event_id(d);
  921. iommu_dma_map_msi_msg(d->irq, msg);
  922. }
  923. static int its_irq_set_irqchip_state(struct irq_data *d,
  924. enum irqchip_irq_state which,
  925. bool state)
  926. {
  927. struct its_device *its_dev = irq_data_get_irq_chip_data(d);
  928. u32 event = its_get_event_id(d);
  929. if (which != IRQCHIP_STATE_PENDING)
  930. return -EINVAL;
  931. if (state)
  932. its_send_int(its_dev, event);
  933. else
  934. its_send_clear(its_dev, event);
  935. return 0;
  936. }
  937. static void its_map_vm(struct its_node *its, struct its_vm *vm)
  938. {
  939. unsigned long flags;
  940. /* Not using the ITS list? Everything is always mapped. */
  941. if (!its_list_map)
  942. return;
  943. raw_spin_lock_irqsave(&vmovp_lock, flags);
  944. /*
  945. * If the VM wasn't mapped yet, iterate over the vpes and get
  946. * them mapped now.
  947. */
  948. vm->vlpi_count[its->list_nr]++;
  949. if (vm->vlpi_count[its->list_nr] == 1) {
  950. int i;
  951. for (i = 0; i < vm->nr_vpes; i++) {
  952. struct its_vpe *vpe = vm->vpes[i];
  953. struct irq_data *d = irq_get_irq_data(vpe->irq);
  954. /* Map the VPE to the first possible CPU */
  955. vpe->col_idx = cpumask_first(cpu_online_mask);
  956. its_send_vmapp(its, vpe, true);
  957. its_send_vinvall(its, vpe);
  958. irq_data_update_effective_affinity(d, cpumask_of(vpe->col_idx));
  959. }
  960. }
  961. raw_spin_unlock_irqrestore(&vmovp_lock, flags);
  962. }
  963. static void its_unmap_vm(struct its_node *its, struct its_vm *vm)
  964. {
  965. unsigned long flags;
  966. /* Not using the ITS list? Everything is always mapped. */
  967. if (!its_list_map)
  968. return;
  969. raw_spin_lock_irqsave(&vmovp_lock, flags);
  970. if (!--vm->vlpi_count[its->list_nr]) {
  971. int i;
  972. for (i = 0; i < vm->nr_vpes; i++)
  973. its_send_vmapp(its, vm->vpes[i], false);
  974. }
  975. raw_spin_unlock_irqrestore(&vmovp_lock, flags);
  976. }
  977. static int its_vlpi_map(struct irq_data *d, struct its_cmd_info *info)
  978. {
  979. struct its_device *its_dev = irq_data_get_irq_chip_data(d);
  980. u32 event = its_get_event_id(d);
  981. int ret = 0;
  982. if (!info->map)
  983. return -EINVAL;
  984. mutex_lock(&its_dev->event_map.vlpi_lock);
  985. if (!its_dev->event_map.vm) {
  986. struct its_vlpi_map *maps;
  987. maps = kzalloc(sizeof(*maps) * its_dev->event_map.nr_lpis,
  988. GFP_KERNEL);
  989. if (!maps) {
  990. ret = -ENOMEM;
  991. goto out;
  992. }
  993. its_dev->event_map.vm = info->map->vm;
  994. its_dev->event_map.vlpi_maps = maps;
  995. } else if (its_dev->event_map.vm != info->map->vm) {
  996. ret = -EINVAL;
  997. goto out;
  998. }
  999. /* Get our private copy of the mapping information */
  1000. its_dev->event_map.vlpi_maps[event] = *info->map;
  1001. if (irqd_is_forwarded_to_vcpu(d)) {
  1002. /* Already mapped, move it around */
  1003. its_send_vmovi(its_dev, event);
  1004. } else {
  1005. /* Ensure all the VPEs are mapped on this ITS */
  1006. its_map_vm(its_dev->its, info->map->vm);
  1007. /*
  1008. * Flag the interrupt as forwarded so that we can
  1009. * start poking the virtual property table.
  1010. */
  1011. irqd_set_forwarded_to_vcpu(d);
  1012. /* Write out the property to the prop table */
  1013. lpi_write_config(d, 0xff, info->map->properties);
  1014. /* Drop the physical mapping */
  1015. its_send_discard(its_dev, event);
  1016. /* and install the virtual one */
  1017. its_send_vmapti(its_dev, event);
  1018. /* Increment the number of VLPIs */
  1019. its_dev->event_map.nr_vlpis++;
  1020. }
  1021. out:
  1022. mutex_unlock(&its_dev->event_map.vlpi_lock);
  1023. return ret;
  1024. }
  1025. static int its_vlpi_get(struct irq_data *d, struct its_cmd_info *info)
  1026. {
  1027. struct its_device *its_dev = irq_data_get_irq_chip_data(d);
  1028. u32 event = its_get_event_id(d);
  1029. int ret = 0;
  1030. mutex_lock(&its_dev->event_map.vlpi_lock);
  1031. if (!its_dev->event_map.vm ||
  1032. !its_dev->event_map.vlpi_maps[event].vm) {
  1033. ret = -EINVAL;
  1034. goto out;
  1035. }
  1036. /* Copy our mapping information to the incoming request */
  1037. *info->map = its_dev->event_map.vlpi_maps[event];
  1038. out:
  1039. mutex_unlock(&its_dev->event_map.vlpi_lock);
  1040. return ret;
  1041. }
  1042. static int its_vlpi_unmap(struct irq_data *d)
  1043. {
  1044. struct its_device *its_dev = irq_data_get_irq_chip_data(d);
  1045. u32 event = its_get_event_id(d);
  1046. int ret = 0;
  1047. mutex_lock(&its_dev->event_map.vlpi_lock);
  1048. if (!its_dev->event_map.vm || !irqd_is_forwarded_to_vcpu(d)) {
  1049. ret = -EINVAL;
  1050. goto out;
  1051. }
  1052. /* Drop the virtual mapping */
  1053. its_send_discard(its_dev, event);
  1054. /* and restore the physical one */
  1055. irqd_clr_forwarded_to_vcpu(d);
  1056. its_send_mapti(its_dev, d->hwirq, event);
  1057. lpi_update_config(d, 0xff, (LPI_PROP_DEFAULT_PRIO |
  1058. LPI_PROP_ENABLED |
  1059. LPI_PROP_GROUP1));
  1060. /* Potentially unmap the VM from this ITS */
  1061. its_unmap_vm(its_dev->its, its_dev->event_map.vm);
  1062. /*
  1063. * Drop the refcount and make the device available again if
  1064. * this was the last VLPI.
  1065. */
  1066. if (!--its_dev->event_map.nr_vlpis) {
  1067. its_dev->event_map.vm = NULL;
  1068. kfree(its_dev->event_map.vlpi_maps);
  1069. }
  1070. out:
  1071. mutex_unlock(&its_dev->event_map.vlpi_lock);
  1072. return ret;
  1073. }
  1074. static int its_vlpi_prop_update(struct irq_data *d, struct its_cmd_info *info)
  1075. {
  1076. struct its_device *its_dev = irq_data_get_irq_chip_data(d);
  1077. if (!its_dev->event_map.vm || !irqd_is_forwarded_to_vcpu(d))
  1078. return -EINVAL;
  1079. if (info->cmd_type == PROP_UPDATE_AND_INV_VLPI)
  1080. lpi_update_config(d, 0xff, info->config);
  1081. else
  1082. lpi_write_config(d, 0xff, info->config);
  1083. its_vlpi_set_doorbell(d, !!(info->config & LPI_PROP_ENABLED));
  1084. return 0;
  1085. }
  1086. static int its_irq_set_vcpu_affinity(struct irq_data *d, void *vcpu_info)
  1087. {
  1088. struct its_device *its_dev = irq_data_get_irq_chip_data(d);
  1089. struct its_cmd_info *info = vcpu_info;
  1090. /* Need a v4 ITS */
  1091. if (!its_dev->its->is_v4)
  1092. return -EINVAL;
  1093. /* Unmap request? */
  1094. if (!info)
  1095. return its_vlpi_unmap(d);
  1096. switch (info->cmd_type) {
  1097. case MAP_VLPI:
  1098. return its_vlpi_map(d, info);
  1099. case GET_VLPI:
  1100. return its_vlpi_get(d, info);
  1101. case PROP_UPDATE_VLPI:
  1102. case PROP_UPDATE_AND_INV_VLPI:
  1103. return its_vlpi_prop_update(d, info);
  1104. default:
  1105. return -EINVAL;
  1106. }
  1107. }
  1108. static struct irq_chip its_irq_chip = {
  1109. .name = "ITS",
  1110. .irq_mask = its_mask_irq,
  1111. .irq_unmask = its_unmask_irq,
  1112. .irq_eoi = irq_chip_eoi_parent,
  1113. .irq_set_affinity = its_set_affinity,
  1114. .irq_compose_msi_msg = its_irq_compose_msi_msg,
  1115. .irq_set_irqchip_state = its_irq_set_irqchip_state,
  1116. .irq_set_vcpu_affinity = its_irq_set_vcpu_affinity,
  1117. };
  1118. /*
  1119. * How we allocate LPIs:
  1120. *
  1121. * The GIC has id_bits bits for interrupt identifiers. From there, we
  1122. * must subtract 8192 which are reserved for SGIs/PPIs/SPIs. Then, as
  1123. * we allocate LPIs by chunks of 32, we can shift the whole thing by 5
  1124. * bits to the right.
  1125. *
  1126. * This gives us (((1UL << id_bits) - 8192) >> 5) possible allocations.
  1127. */
  1128. #define IRQS_PER_CHUNK_SHIFT 5
  1129. #define IRQS_PER_CHUNK (1UL << IRQS_PER_CHUNK_SHIFT)
  1130. #define ITS_MAX_LPI_NRBITS 16 /* 64K LPIs */
  1131. static unsigned long *lpi_bitmap;
  1132. static u32 lpi_chunks;
  1133. static DEFINE_SPINLOCK(lpi_lock);
  1134. static int its_lpi_to_chunk(int lpi)
  1135. {
  1136. return (lpi - 8192) >> IRQS_PER_CHUNK_SHIFT;
  1137. }
  1138. static int its_chunk_to_lpi(int chunk)
  1139. {
  1140. return (chunk << IRQS_PER_CHUNK_SHIFT) + 8192;
  1141. }
  1142. static int __init its_lpi_init(u32 id_bits)
  1143. {
  1144. lpi_chunks = its_lpi_to_chunk(1UL << id_bits);
  1145. lpi_bitmap = kzalloc(BITS_TO_LONGS(lpi_chunks) * sizeof(long),
  1146. GFP_KERNEL);
  1147. if (!lpi_bitmap) {
  1148. lpi_chunks = 0;
  1149. return -ENOMEM;
  1150. }
  1151. pr_info("ITS: Allocated %d chunks for LPIs\n", (int)lpi_chunks);
  1152. return 0;
  1153. }
  1154. static unsigned long *its_lpi_alloc_chunks(int nr_irqs, int *base, int *nr_ids)
  1155. {
  1156. unsigned long *bitmap = NULL;
  1157. int chunk_id;
  1158. int nr_chunks;
  1159. int i;
  1160. nr_chunks = DIV_ROUND_UP(nr_irqs, IRQS_PER_CHUNK);
  1161. spin_lock(&lpi_lock);
  1162. do {
  1163. chunk_id = bitmap_find_next_zero_area(lpi_bitmap, lpi_chunks,
  1164. 0, nr_chunks, 0);
  1165. if (chunk_id < lpi_chunks)
  1166. break;
  1167. nr_chunks--;
  1168. } while (nr_chunks > 0);
  1169. if (!nr_chunks)
  1170. goto out;
  1171. bitmap = kzalloc(BITS_TO_LONGS(nr_chunks * IRQS_PER_CHUNK) * sizeof (long),
  1172. GFP_ATOMIC);
  1173. if (!bitmap)
  1174. goto out;
  1175. for (i = 0; i < nr_chunks; i++)
  1176. set_bit(chunk_id + i, lpi_bitmap);
  1177. *base = its_chunk_to_lpi(chunk_id);
  1178. *nr_ids = nr_chunks * IRQS_PER_CHUNK;
  1179. out:
  1180. spin_unlock(&lpi_lock);
  1181. if (!bitmap)
  1182. *base = *nr_ids = 0;
  1183. return bitmap;
  1184. }
  1185. static void its_lpi_free_chunks(unsigned long *bitmap, int base, int nr_ids)
  1186. {
  1187. int lpi;
  1188. spin_lock(&lpi_lock);
  1189. for (lpi = base; lpi < (base + nr_ids); lpi += IRQS_PER_CHUNK) {
  1190. int chunk = its_lpi_to_chunk(lpi);
  1191. BUG_ON(chunk > lpi_chunks);
  1192. if (test_bit(chunk, lpi_bitmap)) {
  1193. clear_bit(chunk, lpi_bitmap);
  1194. } else {
  1195. pr_err("Bad LPI chunk %d\n", chunk);
  1196. }
  1197. }
  1198. spin_unlock(&lpi_lock);
  1199. kfree(bitmap);
  1200. }
  1201. static struct page *its_allocate_prop_table(gfp_t gfp_flags)
  1202. {
  1203. struct page *prop_page;
  1204. prop_page = alloc_pages(gfp_flags, get_order(LPI_PROPBASE_SZ));
  1205. if (!prop_page)
  1206. return NULL;
  1207. /* Priority 0xa0, Group-1, disabled */
  1208. memset(page_address(prop_page),
  1209. LPI_PROP_DEFAULT_PRIO | LPI_PROP_GROUP1,
  1210. LPI_PROPBASE_SZ);
  1211. /* Make sure the GIC will observe the written configuration */
  1212. gic_flush_dcache_to_poc(page_address(prop_page), LPI_PROPBASE_SZ);
  1213. return prop_page;
  1214. }
  1215. static void its_free_prop_table(struct page *prop_page)
  1216. {
  1217. free_pages((unsigned long)page_address(prop_page),
  1218. get_order(LPI_PROPBASE_SZ));
  1219. }
  1220. static int __init its_alloc_lpi_tables(void)
  1221. {
  1222. phys_addr_t paddr;
  1223. lpi_id_bits = min_t(u32, gic_rdists->id_bits, ITS_MAX_LPI_NRBITS);
  1224. gic_rdists->prop_page = its_allocate_prop_table(GFP_NOWAIT);
  1225. if (!gic_rdists->prop_page) {
  1226. pr_err("Failed to allocate PROPBASE\n");
  1227. return -ENOMEM;
  1228. }
  1229. paddr = page_to_phys(gic_rdists->prop_page);
  1230. pr_info("GIC: using LPI property table @%pa\n", &paddr);
  1231. return its_lpi_init(lpi_id_bits);
  1232. }
  1233. static const char *its_base_type_string[] = {
  1234. [GITS_BASER_TYPE_DEVICE] = "Devices",
  1235. [GITS_BASER_TYPE_VCPU] = "Virtual CPUs",
  1236. [GITS_BASER_TYPE_RESERVED3] = "Reserved (3)",
  1237. [GITS_BASER_TYPE_COLLECTION] = "Interrupt Collections",
  1238. [GITS_BASER_TYPE_RESERVED5] = "Reserved (5)",
  1239. [GITS_BASER_TYPE_RESERVED6] = "Reserved (6)",
  1240. [GITS_BASER_TYPE_RESERVED7] = "Reserved (7)",
  1241. };
  1242. static u64 its_read_baser(struct its_node *its, struct its_baser *baser)
  1243. {
  1244. u32 idx = baser - its->tables;
  1245. return gits_read_baser(its->base + GITS_BASER + (idx << 3));
  1246. }
  1247. static void its_write_baser(struct its_node *its, struct its_baser *baser,
  1248. u64 val)
  1249. {
  1250. u32 idx = baser - its->tables;
  1251. gits_write_baser(val, its->base + GITS_BASER + (idx << 3));
  1252. baser->val = its_read_baser(its, baser);
  1253. }
  1254. static int its_setup_baser(struct its_node *its, struct its_baser *baser,
  1255. u64 cache, u64 shr, u32 psz, u32 order,
  1256. bool indirect)
  1257. {
  1258. u64 val = its_read_baser(its, baser);
  1259. u64 esz = GITS_BASER_ENTRY_SIZE(val);
  1260. u64 type = GITS_BASER_TYPE(val);
  1261. u64 baser_phys, tmp;
  1262. u32 alloc_pages;
  1263. void *base;
  1264. retry_alloc_baser:
  1265. alloc_pages = (PAGE_ORDER_TO_SIZE(order) / psz);
  1266. if (alloc_pages > GITS_BASER_PAGES_MAX) {
  1267. pr_warn("ITS@%pa: %s too large, reduce ITS pages %u->%u\n",
  1268. &its->phys_base, its_base_type_string[type],
  1269. alloc_pages, GITS_BASER_PAGES_MAX);
  1270. alloc_pages = GITS_BASER_PAGES_MAX;
  1271. order = get_order(GITS_BASER_PAGES_MAX * psz);
  1272. }
  1273. base = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
  1274. if (!base)
  1275. return -ENOMEM;
  1276. baser_phys = virt_to_phys(base);
  1277. /* Check if the physical address of the memory is above 48bits */
  1278. if (IS_ENABLED(CONFIG_ARM64_64K_PAGES) && (baser_phys >> 48)) {
  1279. /* 52bit PA is supported only when PageSize=64K */
  1280. if (psz != SZ_64K) {
  1281. pr_err("ITS: no 52bit PA support when psz=%d\n", psz);
  1282. free_pages((unsigned long)base, order);
  1283. return -ENXIO;
  1284. }
  1285. /* Convert 52bit PA to 48bit field */
  1286. baser_phys = GITS_BASER_PHYS_52_to_48(baser_phys);
  1287. }
  1288. retry_baser:
  1289. val = (baser_phys |
  1290. (type << GITS_BASER_TYPE_SHIFT) |
  1291. ((esz - 1) << GITS_BASER_ENTRY_SIZE_SHIFT) |
  1292. ((alloc_pages - 1) << GITS_BASER_PAGES_SHIFT) |
  1293. cache |
  1294. shr |
  1295. GITS_BASER_VALID);
  1296. val |= indirect ? GITS_BASER_INDIRECT : 0x0;
  1297. switch (psz) {
  1298. case SZ_4K:
  1299. val |= GITS_BASER_PAGE_SIZE_4K;
  1300. break;
  1301. case SZ_16K:
  1302. val |= GITS_BASER_PAGE_SIZE_16K;
  1303. break;
  1304. case SZ_64K:
  1305. val |= GITS_BASER_PAGE_SIZE_64K;
  1306. break;
  1307. }
  1308. its_write_baser(its, baser, val);
  1309. tmp = baser->val;
  1310. if ((val ^ tmp) & GITS_BASER_SHAREABILITY_MASK) {
  1311. /*
  1312. * Shareability didn't stick. Just use
  1313. * whatever the read reported, which is likely
  1314. * to be the only thing this redistributor
  1315. * supports. If that's zero, make it
  1316. * non-cacheable as well.
  1317. */
  1318. shr = tmp & GITS_BASER_SHAREABILITY_MASK;
  1319. if (!shr) {
  1320. cache = GITS_BASER_nC;
  1321. gic_flush_dcache_to_poc(base, PAGE_ORDER_TO_SIZE(order));
  1322. }
  1323. goto retry_baser;
  1324. }
  1325. if ((val ^ tmp) & GITS_BASER_PAGE_SIZE_MASK) {
  1326. /*
  1327. * Page size didn't stick. Let's try a smaller
  1328. * size and retry. If we reach 4K, then
  1329. * something is horribly wrong...
  1330. */
  1331. free_pages((unsigned long)base, order);
  1332. baser->base = NULL;
  1333. switch (psz) {
  1334. case SZ_16K:
  1335. psz = SZ_4K;
  1336. goto retry_alloc_baser;
  1337. case SZ_64K:
  1338. psz = SZ_16K;
  1339. goto retry_alloc_baser;
  1340. }
  1341. }
  1342. if (val != tmp) {
  1343. pr_err("ITS@%pa: %s doesn't stick: %llx %llx\n",
  1344. &its->phys_base, its_base_type_string[type],
  1345. val, tmp);
  1346. free_pages((unsigned long)base, order);
  1347. return -ENXIO;
  1348. }
  1349. baser->order = order;
  1350. baser->base = base;
  1351. baser->psz = psz;
  1352. tmp = indirect ? GITS_LVL1_ENTRY_SIZE : esz;
  1353. pr_info("ITS@%pa: allocated %d %s @%lx (%s, esz %d, psz %dK, shr %d)\n",
  1354. &its->phys_base, (int)(PAGE_ORDER_TO_SIZE(order) / (int)tmp),
  1355. its_base_type_string[type],
  1356. (unsigned long)virt_to_phys(base),
  1357. indirect ? "indirect" : "flat", (int)esz,
  1358. psz / SZ_1K, (int)shr >> GITS_BASER_SHAREABILITY_SHIFT);
  1359. return 0;
  1360. }
  1361. static bool its_parse_indirect_baser(struct its_node *its,
  1362. struct its_baser *baser,
  1363. u32 psz, u32 *order, u32 ids)
  1364. {
  1365. u64 tmp = its_read_baser(its, baser);
  1366. u64 type = GITS_BASER_TYPE(tmp);
  1367. u64 esz = GITS_BASER_ENTRY_SIZE(tmp);
  1368. u64 val = GITS_BASER_InnerShareable | GITS_BASER_RaWaWb;
  1369. u32 new_order = *order;
  1370. bool indirect = false;
  1371. /* No need to enable Indirection if memory requirement < (psz*2)bytes */
  1372. if ((esz << ids) > (psz * 2)) {
  1373. /*
  1374. * Find out whether hw supports a single or two-level table by
  1375. * table by reading bit at offset '62' after writing '1' to it.
  1376. */
  1377. its_write_baser(its, baser, val | GITS_BASER_INDIRECT);
  1378. indirect = !!(baser->val & GITS_BASER_INDIRECT);
  1379. if (indirect) {
  1380. /*
  1381. * The size of the lvl2 table is equal to ITS page size
  1382. * which is 'psz'. For computing lvl1 table size,
  1383. * subtract ID bits that sparse lvl2 table from 'ids'
  1384. * which is reported by ITS hardware times lvl1 table
  1385. * entry size.
  1386. */
  1387. ids -= ilog2(psz / (int)esz);
  1388. esz = GITS_LVL1_ENTRY_SIZE;
  1389. }
  1390. }
  1391. /*
  1392. * Allocate as many entries as required to fit the
  1393. * range of device IDs that the ITS can grok... The ID
  1394. * space being incredibly sparse, this results in a
  1395. * massive waste of memory if two-level device table
  1396. * feature is not supported by hardware.
  1397. */
  1398. new_order = max_t(u32, get_order(esz << ids), new_order);
  1399. if (new_order >= MAX_ORDER) {
  1400. new_order = MAX_ORDER - 1;
  1401. ids = ilog2(PAGE_ORDER_TO_SIZE(new_order) / (int)esz);
  1402. pr_warn("ITS@%pa: %s Table too large, reduce ids %u->%u\n",
  1403. &its->phys_base, its_base_type_string[type],
  1404. its->device_ids, ids);
  1405. }
  1406. *order = new_order;
  1407. return indirect;
  1408. }
  1409. static void its_free_tables(struct its_node *its)
  1410. {
  1411. int i;
  1412. for (i = 0; i < GITS_BASER_NR_REGS; i++) {
  1413. if (its->tables[i].base) {
  1414. free_pages((unsigned long)its->tables[i].base,
  1415. its->tables[i].order);
  1416. its->tables[i].base = NULL;
  1417. }
  1418. }
  1419. }
  1420. static int its_alloc_tables(struct its_node *its)
  1421. {
  1422. u64 shr = GITS_BASER_InnerShareable;
  1423. u64 cache = GITS_BASER_RaWaWb;
  1424. u32 psz = SZ_64K;
  1425. int err, i;
  1426. if (its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_22375)
  1427. /* erratum 24313: ignore memory access type */
  1428. cache = GITS_BASER_nCnB;
  1429. for (i = 0; i < GITS_BASER_NR_REGS; i++) {
  1430. struct its_baser *baser = its->tables + i;
  1431. u64 val = its_read_baser(its, baser);
  1432. u64 type = GITS_BASER_TYPE(val);
  1433. u32 order = get_order(psz);
  1434. bool indirect = false;
  1435. switch (type) {
  1436. case GITS_BASER_TYPE_NONE:
  1437. continue;
  1438. case GITS_BASER_TYPE_DEVICE:
  1439. indirect = its_parse_indirect_baser(its, baser,
  1440. psz, &order,
  1441. its->device_ids);
  1442. case GITS_BASER_TYPE_VCPU:
  1443. indirect = its_parse_indirect_baser(its, baser,
  1444. psz, &order,
  1445. ITS_MAX_VPEID_BITS);
  1446. break;
  1447. }
  1448. err = its_setup_baser(its, baser, cache, shr, psz, order, indirect);
  1449. if (err < 0) {
  1450. its_free_tables(its);
  1451. return err;
  1452. }
  1453. /* Update settings which will be used for next BASERn */
  1454. psz = baser->psz;
  1455. cache = baser->val & GITS_BASER_CACHEABILITY_MASK;
  1456. shr = baser->val & GITS_BASER_SHAREABILITY_MASK;
  1457. }
  1458. return 0;
  1459. }
  1460. static int its_alloc_collections(struct its_node *its)
  1461. {
  1462. its->collections = kzalloc(nr_cpu_ids * sizeof(*its->collections),
  1463. GFP_KERNEL);
  1464. if (!its->collections)
  1465. return -ENOMEM;
  1466. return 0;
  1467. }
  1468. static struct page *its_allocate_pending_table(gfp_t gfp_flags)
  1469. {
  1470. struct page *pend_page;
  1471. /*
  1472. * The pending pages have to be at least 64kB aligned,
  1473. * hence the 'max(LPI_PENDBASE_SZ, SZ_64K)' below.
  1474. */
  1475. pend_page = alloc_pages(gfp_flags | __GFP_ZERO,
  1476. get_order(max_t(u32, LPI_PENDBASE_SZ, SZ_64K)));
  1477. if (!pend_page)
  1478. return NULL;
  1479. /* Make sure the GIC will observe the zero-ed page */
  1480. gic_flush_dcache_to_poc(page_address(pend_page), LPI_PENDBASE_SZ);
  1481. return pend_page;
  1482. }
  1483. static void its_free_pending_table(struct page *pt)
  1484. {
  1485. free_pages((unsigned long)page_address(pt),
  1486. get_order(max_t(u32, LPI_PENDBASE_SZ, SZ_64K)));
  1487. }
  1488. static void its_cpu_init_lpis(void)
  1489. {
  1490. void __iomem *rbase = gic_data_rdist_rd_base();
  1491. struct page *pend_page;
  1492. u64 val, tmp;
  1493. /* If we didn't allocate the pending table yet, do it now */
  1494. pend_page = gic_data_rdist()->pend_page;
  1495. if (!pend_page) {
  1496. phys_addr_t paddr;
  1497. pend_page = its_allocate_pending_table(GFP_NOWAIT);
  1498. if (!pend_page) {
  1499. pr_err("Failed to allocate PENDBASE for CPU%d\n",
  1500. smp_processor_id());
  1501. return;
  1502. }
  1503. paddr = page_to_phys(pend_page);
  1504. pr_info("CPU%d: using LPI pending table @%pa\n",
  1505. smp_processor_id(), &paddr);
  1506. gic_data_rdist()->pend_page = pend_page;
  1507. }
  1508. /* set PROPBASE */
  1509. val = (page_to_phys(gic_rdists->prop_page) |
  1510. GICR_PROPBASER_InnerShareable |
  1511. GICR_PROPBASER_RaWaWb |
  1512. ((LPI_NRBITS - 1) & GICR_PROPBASER_IDBITS_MASK));
  1513. gicr_write_propbaser(val, rbase + GICR_PROPBASER);
  1514. tmp = gicr_read_propbaser(rbase + GICR_PROPBASER);
  1515. if ((tmp ^ val) & GICR_PROPBASER_SHAREABILITY_MASK) {
  1516. if (!(tmp & GICR_PROPBASER_SHAREABILITY_MASK)) {
  1517. /*
  1518. * The HW reports non-shareable, we must
  1519. * remove the cacheability attributes as
  1520. * well.
  1521. */
  1522. val &= ~(GICR_PROPBASER_SHAREABILITY_MASK |
  1523. GICR_PROPBASER_CACHEABILITY_MASK);
  1524. val |= GICR_PROPBASER_nC;
  1525. gicr_write_propbaser(val, rbase + GICR_PROPBASER);
  1526. }
  1527. pr_info_once("GIC: using cache flushing for LPI property table\n");
  1528. gic_rdists->flags |= RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING;
  1529. }
  1530. /* set PENDBASE */
  1531. val = (page_to_phys(pend_page) |
  1532. GICR_PENDBASER_InnerShareable |
  1533. GICR_PENDBASER_RaWaWb);
  1534. gicr_write_pendbaser(val, rbase + GICR_PENDBASER);
  1535. tmp = gicr_read_pendbaser(rbase + GICR_PENDBASER);
  1536. if (!(tmp & GICR_PENDBASER_SHAREABILITY_MASK)) {
  1537. /*
  1538. * The HW reports non-shareable, we must remove the
  1539. * cacheability attributes as well.
  1540. */
  1541. val &= ~(GICR_PENDBASER_SHAREABILITY_MASK |
  1542. GICR_PENDBASER_CACHEABILITY_MASK);
  1543. val |= GICR_PENDBASER_nC;
  1544. gicr_write_pendbaser(val, rbase + GICR_PENDBASER);
  1545. }
  1546. /* Enable LPIs */
  1547. val = readl_relaxed(rbase + GICR_CTLR);
  1548. val |= GICR_CTLR_ENABLE_LPIS;
  1549. writel_relaxed(val, rbase + GICR_CTLR);
  1550. /* Make sure the GIC has seen the above */
  1551. dsb(sy);
  1552. }
  1553. static void its_cpu_init_collection(struct its_node *its)
  1554. {
  1555. int cpu = smp_processor_id();
  1556. u64 target;
  1557. /* avoid cross node collections and its mapping */
  1558. if (its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_23144) {
  1559. struct device_node *cpu_node;
  1560. cpu_node = of_get_cpu_node(cpu, NULL);
  1561. if (its->numa_node != NUMA_NO_NODE &&
  1562. its->numa_node != of_node_to_nid(cpu_node))
  1563. return;
  1564. }
  1565. /*
  1566. * We now have to bind each collection to its target
  1567. * redistributor.
  1568. */
  1569. if (gic_read_typer(its->base + GITS_TYPER) & GITS_TYPER_PTA) {
  1570. /*
  1571. * This ITS wants the physical address of the
  1572. * redistributor.
  1573. */
  1574. target = gic_data_rdist()->phys_base;
  1575. } else {
  1576. /* This ITS wants a linear CPU number. */
  1577. target = gic_read_typer(gic_data_rdist_rd_base() + GICR_TYPER);
  1578. target = GICR_TYPER_CPU_NUMBER(target) << 16;
  1579. }
  1580. /* Perform collection mapping */
  1581. its->collections[cpu].target_address = target;
  1582. its->collections[cpu].col_id = cpu;
  1583. its_send_mapc(its, &its->collections[cpu], 1);
  1584. its_send_invall(its, &its->collections[cpu]);
  1585. }
  1586. static void its_cpu_init_collections(void)
  1587. {
  1588. struct its_node *its;
  1589. spin_lock(&its_lock);
  1590. list_for_each_entry(its, &its_nodes, entry)
  1591. its_cpu_init_collection(its);
  1592. spin_unlock(&its_lock);
  1593. }
  1594. static struct its_device *its_find_device(struct its_node *its, u32 dev_id)
  1595. {
  1596. struct its_device *its_dev = NULL, *tmp;
  1597. unsigned long flags;
  1598. raw_spin_lock_irqsave(&its->lock, flags);
  1599. list_for_each_entry(tmp, &its->its_device_list, entry) {
  1600. if (tmp->device_id == dev_id) {
  1601. its_dev = tmp;
  1602. break;
  1603. }
  1604. }
  1605. raw_spin_unlock_irqrestore(&its->lock, flags);
  1606. return its_dev;
  1607. }
  1608. static struct its_baser *its_get_baser(struct its_node *its, u32 type)
  1609. {
  1610. int i;
  1611. for (i = 0; i < GITS_BASER_NR_REGS; i++) {
  1612. if (GITS_BASER_TYPE(its->tables[i].val) == type)
  1613. return &its->tables[i];
  1614. }
  1615. return NULL;
  1616. }
  1617. static bool its_alloc_table_entry(struct its_baser *baser, u32 id)
  1618. {
  1619. struct page *page;
  1620. u32 esz, idx;
  1621. __le64 *table;
  1622. /* Don't allow device id that exceeds single, flat table limit */
  1623. esz = GITS_BASER_ENTRY_SIZE(baser->val);
  1624. if (!(baser->val & GITS_BASER_INDIRECT))
  1625. return (id < (PAGE_ORDER_TO_SIZE(baser->order) / esz));
  1626. /* Compute 1st level table index & check if that exceeds table limit */
  1627. idx = id >> ilog2(baser->psz / esz);
  1628. if (idx >= (PAGE_ORDER_TO_SIZE(baser->order) / GITS_LVL1_ENTRY_SIZE))
  1629. return false;
  1630. table = baser->base;
  1631. /* Allocate memory for 2nd level table */
  1632. if (!table[idx]) {
  1633. page = alloc_pages(GFP_KERNEL | __GFP_ZERO, get_order(baser->psz));
  1634. if (!page)
  1635. return false;
  1636. /* Flush Lvl2 table to PoC if hw doesn't support coherency */
  1637. if (!(baser->val & GITS_BASER_SHAREABILITY_MASK))
  1638. gic_flush_dcache_to_poc(page_address(page), baser->psz);
  1639. table[idx] = cpu_to_le64(page_to_phys(page) | GITS_BASER_VALID);
  1640. /* Flush Lvl1 entry to PoC if hw doesn't support coherency */
  1641. if (!(baser->val & GITS_BASER_SHAREABILITY_MASK))
  1642. gic_flush_dcache_to_poc(table + idx, GITS_LVL1_ENTRY_SIZE);
  1643. /* Ensure updated table contents are visible to ITS hardware */
  1644. dsb(sy);
  1645. }
  1646. return true;
  1647. }
  1648. static bool its_alloc_device_table(struct its_node *its, u32 dev_id)
  1649. {
  1650. struct its_baser *baser;
  1651. baser = its_get_baser(its, GITS_BASER_TYPE_DEVICE);
  1652. /* Don't allow device id that exceeds ITS hardware limit */
  1653. if (!baser)
  1654. return (ilog2(dev_id) < its->device_ids);
  1655. return its_alloc_table_entry(baser, dev_id);
  1656. }
  1657. static bool its_alloc_vpe_table(u32 vpe_id)
  1658. {
  1659. struct its_node *its;
  1660. /*
  1661. * Make sure the L2 tables are allocated on *all* v4 ITSs. We
  1662. * could try and only do it on ITSs corresponding to devices
  1663. * that have interrupts targeted at this VPE, but the
  1664. * complexity becomes crazy (and you have tons of memory
  1665. * anyway, right?).
  1666. */
  1667. list_for_each_entry(its, &its_nodes, entry) {
  1668. struct its_baser *baser;
  1669. if (!its->is_v4)
  1670. continue;
  1671. baser = its_get_baser(its, GITS_BASER_TYPE_VCPU);
  1672. if (!baser)
  1673. return false;
  1674. if (!its_alloc_table_entry(baser, vpe_id))
  1675. return false;
  1676. }
  1677. return true;
  1678. }
  1679. static struct its_device *its_create_device(struct its_node *its, u32 dev_id,
  1680. int nvecs, bool alloc_lpis)
  1681. {
  1682. struct its_device *dev;
  1683. unsigned long *lpi_map = NULL;
  1684. unsigned long flags;
  1685. u16 *col_map = NULL;
  1686. void *itt;
  1687. int lpi_base;
  1688. int nr_lpis;
  1689. int nr_ites;
  1690. int sz;
  1691. if (!its_alloc_device_table(its, dev_id))
  1692. return NULL;
  1693. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  1694. /*
  1695. * We allocate at least one chunk worth of LPIs bet device,
  1696. * and thus that many ITEs. The device may require less though.
  1697. */
  1698. nr_ites = max(IRQS_PER_CHUNK, roundup_pow_of_two(nvecs));
  1699. sz = nr_ites * its->ite_size;
  1700. sz = max(sz, ITS_ITT_ALIGN) + ITS_ITT_ALIGN - 1;
  1701. itt = kzalloc(sz, GFP_KERNEL);
  1702. if (alloc_lpis) {
  1703. lpi_map = its_lpi_alloc_chunks(nvecs, &lpi_base, &nr_lpis);
  1704. if (lpi_map)
  1705. col_map = kzalloc(sizeof(*col_map) * nr_lpis,
  1706. GFP_KERNEL);
  1707. } else {
  1708. col_map = kzalloc(sizeof(*col_map) * nr_ites, GFP_KERNEL);
  1709. nr_lpis = 0;
  1710. lpi_base = 0;
  1711. }
  1712. if (!dev || !itt || !col_map || (!lpi_map && alloc_lpis)) {
  1713. kfree(dev);
  1714. kfree(itt);
  1715. kfree(lpi_map);
  1716. kfree(col_map);
  1717. return NULL;
  1718. }
  1719. gic_flush_dcache_to_poc(itt, sz);
  1720. dev->its = its;
  1721. dev->itt = itt;
  1722. dev->nr_ites = nr_ites;
  1723. dev->event_map.lpi_map = lpi_map;
  1724. dev->event_map.col_map = col_map;
  1725. dev->event_map.lpi_base = lpi_base;
  1726. dev->event_map.nr_lpis = nr_lpis;
  1727. mutex_init(&dev->event_map.vlpi_lock);
  1728. dev->device_id = dev_id;
  1729. INIT_LIST_HEAD(&dev->entry);
  1730. raw_spin_lock_irqsave(&its->lock, flags);
  1731. list_add(&dev->entry, &its->its_device_list);
  1732. raw_spin_unlock_irqrestore(&its->lock, flags);
  1733. /* Map device to its ITT */
  1734. its_send_mapd(dev, 1);
  1735. return dev;
  1736. }
  1737. static void its_free_device(struct its_device *its_dev)
  1738. {
  1739. unsigned long flags;
  1740. raw_spin_lock_irqsave(&its_dev->its->lock, flags);
  1741. list_del(&its_dev->entry);
  1742. raw_spin_unlock_irqrestore(&its_dev->its->lock, flags);
  1743. kfree(its_dev->itt);
  1744. kfree(its_dev);
  1745. }
  1746. static int its_alloc_device_irq(struct its_device *dev, irq_hw_number_t *hwirq)
  1747. {
  1748. int idx;
  1749. idx = find_first_zero_bit(dev->event_map.lpi_map,
  1750. dev->event_map.nr_lpis);
  1751. if (idx == dev->event_map.nr_lpis)
  1752. return -ENOSPC;
  1753. *hwirq = dev->event_map.lpi_base + idx;
  1754. set_bit(idx, dev->event_map.lpi_map);
  1755. return 0;
  1756. }
  1757. static int its_msi_prepare(struct irq_domain *domain, struct device *dev,
  1758. int nvec, msi_alloc_info_t *info)
  1759. {
  1760. struct its_node *its;
  1761. struct its_device *its_dev;
  1762. struct msi_domain_info *msi_info;
  1763. u32 dev_id;
  1764. /*
  1765. * We ignore "dev" entierely, and rely on the dev_id that has
  1766. * been passed via the scratchpad. This limits this domain's
  1767. * usefulness to upper layers that definitely know that they
  1768. * are built on top of the ITS.
  1769. */
  1770. dev_id = info->scratchpad[0].ul;
  1771. msi_info = msi_get_domain_info(domain);
  1772. its = msi_info->data;
  1773. if (!gic_rdists->has_direct_lpi &&
  1774. vpe_proxy.dev &&
  1775. vpe_proxy.dev->its == its &&
  1776. dev_id == vpe_proxy.dev->device_id) {
  1777. /* Bad luck. Get yourself a better implementation */
  1778. WARN_ONCE(1, "DevId %x clashes with GICv4 VPE proxy device\n",
  1779. dev_id);
  1780. return -EINVAL;
  1781. }
  1782. its_dev = its_find_device(its, dev_id);
  1783. if (its_dev) {
  1784. /*
  1785. * We already have seen this ID, probably through
  1786. * another alias (PCI bridge of some sort). No need to
  1787. * create the device.
  1788. */
  1789. pr_debug("Reusing ITT for devID %x\n", dev_id);
  1790. goto out;
  1791. }
  1792. its_dev = its_create_device(its, dev_id, nvec, true);
  1793. if (!its_dev)
  1794. return -ENOMEM;
  1795. pr_debug("ITT %d entries, %d bits\n", nvec, ilog2(nvec));
  1796. out:
  1797. info->scratchpad[0].ptr = its_dev;
  1798. return 0;
  1799. }
  1800. static struct msi_domain_ops its_msi_domain_ops = {
  1801. .msi_prepare = its_msi_prepare,
  1802. };
  1803. static int its_irq_gic_domain_alloc(struct irq_domain *domain,
  1804. unsigned int virq,
  1805. irq_hw_number_t hwirq)
  1806. {
  1807. struct irq_fwspec fwspec;
  1808. if (irq_domain_get_of_node(domain->parent)) {
  1809. fwspec.fwnode = domain->parent->fwnode;
  1810. fwspec.param_count = 3;
  1811. fwspec.param[0] = GIC_IRQ_TYPE_LPI;
  1812. fwspec.param[1] = hwirq;
  1813. fwspec.param[2] = IRQ_TYPE_EDGE_RISING;
  1814. } else if (is_fwnode_irqchip(domain->parent->fwnode)) {
  1815. fwspec.fwnode = domain->parent->fwnode;
  1816. fwspec.param_count = 2;
  1817. fwspec.param[0] = hwirq;
  1818. fwspec.param[1] = IRQ_TYPE_EDGE_RISING;
  1819. } else {
  1820. return -EINVAL;
  1821. }
  1822. return irq_domain_alloc_irqs_parent(domain, virq, 1, &fwspec);
  1823. }
  1824. static int its_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
  1825. unsigned int nr_irqs, void *args)
  1826. {
  1827. msi_alloc_info_t *info = args;
  1828. struct its_device *its_dev = info->scratchpad[0].ptr;
  1829. irq_hw_number_t hwirq;
  1830. int err;
  1831. int i;
  1832. for (i = 0; i < nr_irqs; i++) {
  1833. err = its_alloc_device_irq(its_dev, &hwirq);
  1834. if (err)
  1835. return err;
  1836. err = its_irq_gic_domain_alloc(domain, virq + i, hwirq);
  1837. if (err)
  1838. return err;
  1839. irq_domain_set_hwirq_and_chip(domain, virq + i,
  1840. hwirq, &its_irq_chip, its_dev);
  1841. irqd_set_single_target(irq_desc_get_irq_data(irq_to_desc(virq + i)));
  1842. pr_debug("ID:%d pID:%d vID:%d\n",
  1843. (int)(hwirq - its_dev->event_map.lpi_base),
  1844. (int) hwirq, virq + i);
  1845. }
  1846. return 0;
  1847. }
  1848. static int its_irq_domain_activate(struct irq_domain *domain,
  1849. struct irq_data *d, bool reserve)
  1850. {
  1851. struct its_device *its_dev = irq_data_get_irq_chip_data(d);
  1852. u32 event = its_get_event_id(d);
  1853. const struct cpumask *cpu_mask = cpu_online_mask;
  1854. int cpu;
  1855. /* get the cpu_mask of local node */
  1856. if (its_dev->its->numa_node >= 0)
  1857. cpu_mask = cpumask_of_node(its_dev->its->numa_node);
  1858. /* Bind the LPI to the first possible CPU */
  1859. cpu = cpumask_first(cpu_mask);
  1860. its_dev->event_map.col_map[event] = cpu;
  1861. irq_data_update_effective_affinity(d, cpumask_of(cpu));
  1862. /* Map the GIC IRQ and event to the device */
  1863. its_send_mapti(its_dev, d->hwirq, event);
  1864. return 0;
  1865. }
  1866. static void its_irq_domain_deactivate(struct irq_domain *domain,
  1867. struct irq_data *d)
  1868. {
  1869. struct its_device *its_dev = irq_data_get_irq_chip_data(d);
  1870. u32 event = its_get_event_id(d);
  1871. /* Stop the delivery of interrupts */
  1872. its_send_discard(its_dev, event);
  1873. }
  1874. static void its_irq_domain_free(struct irq_domain *domain, unsigned int virq,
  1875. unsigned int nr_irqs)
  1876. {
  1877. struct irq_data *d = irq_domain_get_irq_data(domain, virq);
  1878. struct its_device *its_dev = irq_data_get_irq_chip_data(d);
  1879. int i;
  1880. for (i = 0; i < nr_irqs; i++) {
  1881. struct irq_data *data = irq_domain_get_irq_data(domain,
  1882. virq + i);
  1883. u32 event = its_get_event_id(data);
  1884. /* Mark interrupt index as unused */
  1885. clear_bit(event, its_dev->event_map.lpi_map);
  1886. /* Nuke the entry in the domain */
  1887. irq_domain_reset_irq_data(data);
  1888. }
  1889. /* If all interrupts have been freed, start mopping the floor */
  1890. if (bitmap_empty(its_dev->event_map.lpi_map,
  1891. its_dev->event_map.nr_lpis)) {
  1892. its_lpi_free_chunks(its_dev->event_map.lpi_map,
  1893. its_dev->event_map.lpi_base,
  1894. its_dev->event_map.nr_lpis);
  1895. kfree(its_dev->event_map.col_map);
  1896. /* Unmap device/itt */
  1897. its_send_mapd(its_dev, 0);
  1898. its_free_device(its_dev);
  1899. }
  1900. irq_domain_free_irqs_parent(domain, virq, nr_irqs);
  1901. }
  1902. static const struct irq_domain_ops its_domain_ops = {
  1903. .alloc = its_irq_domain_alloc,
  1904. .free = its_irq_domain_free,
  1905. .activate = its_irq_domain_activate,
  1906. .deactivate = its_irq_domain_deactivate,
  1907. };
  1908. /*
  1909. * This is insane.
  1910. *
  1911. * If a GICv4 doesn't implement Direct LPIs (which is extremely
  1912. * likely), the only way to perform an invalidate is to use a fake
  1913. * device to issue an INV command, implying that the LPI has first
  1914. * been mapped to some event on that device. Since this is not exactly
  1915. * cheap, we try to keep that mapping around as long as possible, and
  1916. * only issue an UNMAP if we're short on available slots.
  1917. *
  1918. * Broken by design(tm).
  1919. */
  1920. static void its_vpe_db_proxy_unmap_locked(struct its_vpe *vpe)
  1921. {
  1922. /* Already unmapped? */
  1923. if (vpe->vpe_proxy_event == -1)
  1924. return;
  1925. its_send_discard(vpe_proxy.dev, vpe->vpe_proxy_event);
  1926. vpe_proxy.vpes[vpe->vpe_proxy_event] = NULL;
  1927. /*
  1928. * We don't track empty slots at all, so let's move the
  1929. * next_victim pointer if we can quickly reuse that slot
  1930. * instead of nuking an existing entry. Not clear that this is
  1931. * always a win though, and this might just generate a ripple
  1932. * effect... Let's just hope VPEs don't migrate too often.
  1933. */
  1934. if (vpe_proxy.vpes[vpe_proxy.next_victim])
  1935. vpe_proxy.next_victim = vpe->vpe_proxy_event;
  1936. vpe->vpe_proxy_event = -1;
  1937. }
  1938. static void its_vpe_db_proxy_unmap(struct its_vpe *vpe)
  1939. {
  1940. if (!gic_rdists->has_direct_lpi) {
  1941. unsigned long flags;
  1942. raw_spin_lock_irqsave(&vpe_proxy.lock, flags);
  1943. its_vpe_db_proxy_unmap_locked(vpe);
  1944. raw_spin_unlock_irqrestore(&vpe_proxy.lock, flags);
  1945. }
  1946. }
  1947. static void its_vpe_db_proxy_map_locked(struct its_vpe *vpe)
  1948. {
  1949. /* Already mapped? */
  1950. if (vpe->vpe_proxy_event != -1)
  1951. return;
  1952. /* This slot was already allocated. Kick the other VPE out. */
  1953. if (vpe_proxy.vpes[vpe_proxy.next_victim])
  1954. its_vpe_db_proxy_unmap_locked(vpe_proxy.vpes[vpe_proxy.next_victim]);
  1955. /* Map the new VPE instead */
  1956. vpe_proxy.vpes[vpe_proxy.next_victim] = vpe;
  1957. vpe->vpe_proxy_event = vpe_proxy.next_victim;
  1958. vpe_proxy.next_victim = (vpe_proxy.next_victim + 1) % vpe_proxy.dev->nr_ites;
  1959. vpe_proxy.dev->event_map.col_map[vpe->vpe_proxy_event] = vpe->col_idx;
  1960. its_send_mapti(vpe_proxy.dev, vpe->vpe_db_lpi, vpe->vpe_proxy_event);
  1961. }
  1962. static void its_vpe_db_proxy_move(struct its_vpe *vpe, int from, int to)
  1963. {
  1964. unsigned long flags;
  1965. struct its_collection *target_col;
  1966. if (gic_rdists->has_direct_lpi) {
  1967. void __iomem *rdbase;
  1968. rdbase = per_cpu_ptr(gic_rdists->rdist, from)->rd_base;
  1969. gic_write_lpir(vpe->vpe_db_lpi, rdbase + GICR_CLRLPIR);
  1970. while (gic_read_lpir(rdbase + GICR_SYNCR) & 1)
  1971. cpu_relax();
  1972. return;
  1973. }
  1974. raw_spin_lock_irqsave(&vpe_proxy.lock, flags);
  1975. its_vpe_db_proxy_map_locked(vpe);
  1976. target_col = &vpe_proxy.dev->its->collections[to];
  1977. its_send_movi(vpe_proxy.dev, target_col, vpe->vpe_proxy_event);
  1978. vpe_proxy.dev->event_map.col_map[vpe->vpe_proxy_event] = to;
  1979. raw_spin_unlock_irqrestore(&vpe_proxy.lock, flags);
  1980. }
  1981. static int its_vpe_set_affinity(struct irq_data *d,
  1982. const struct cpumask *mask_val,
  1983. bool force)
  1984. {
  1985. struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
  1986. int cpu = cpumask_first(mask_val);
  1987. /*
  1988. * Changing affinity is mega expensive, so let's be as lazy as
  1989. * we can and only do it if we really have to. Also, if mapped
  1990. * into the proxy device, we need to move the doorbell
  1991. * interrupt to its new location.
  1992. */
  1993. if (vpe->col_idx != cpu) {
  1994. int from = vpe->col_idx;
  1995. vpe->col_idx = cpu;
  1996. its_send_vmovp(vpe);
  1997. its_vpe_db_proxy_move(vpe, from, cpu);
  1998. }
  1999. irq_data_update_effective_affinity(d, cpumask_of(cpu));
  2000. return IRQ_SET_MASK_OK_DONE;
  2001. }
  2002. static void its_vpe_schedule(struct its_vpe *vpe)
  2003. {
  2004. void __iomem *vlpi_base = gic_data_rdist_vlpi_base();
  2005. u64 val;
  2006. /* Schedule the VPE */
  2007. val = virt_to_phys(page_address(vpe->its_vm->vprop_page)) &
  2008. GENMASK_ULL(51, 12);
  2009. val |= (LPI_NRBITS - 1) & GICR_VPROPBASER_IDBITS_MASK;
  2010. val |= GICR_VPROPBASER_RaWb;
  2011. val |= GICR_VPROPBASER_InnerShareable;
  2012. gits_write_vpropbaser(val, vlpi_base + GICR_VPROPBASER);
  2013. val = virt_to_phys(page_address(vpe->vpt_page)) &
  2014. GENMASK_ULL(51, 16);
  2015. val |= GICR_VPENDBASER_RaWaWb;
  2016. val |= GICR_VPENDBASER_NonShareable;
  2017. /*
  2018. * There is no good way of finding out if the pending table is
  2019. * empty as we can race against the doorbell interrupt very
  2020. * easily. So in the end, vpe->pending_last is only an
  2021. * indication that the vcpu has something pending, not one
  2022. * that the pending table is empty. A good implementation
  2023. * would be able to read its coarse map pretty quickly anyway,
  2024. * making this a tolerable issue.
  2025. */
  2026. val |= GICR_VPENDBASER_PendingLast;
  2027. val |= vpe->idai ? GICR_VPENDBASER_IDAI : 0;
  2028. val |= GICR_VPENDBASER_Valid;
  2029. gits_write_vpendbaser(val, vlpi_base + GICR_VPENDBASER);
  2030. }
  2031. static void its_vpe_deschedule(struct its_vpe *vpe)
  2032. {
  2033. void __iomem *vlpi_base = gic_data_rdist_vlpi_base();
  2034. u32 count = 1000000; /* 1s! */
  2035. bool clean;
  2036. u64 val;
  2037. /* We're being scheduled out */
  2038. val = gits_read_vpendbaser(vlpi_base + GICR_VPENDBASER);
  2039. val &= ~GICR_VPENDBASER_Valid;
  2040. gits_write_vpendbaser(val, vlpi_base + GICR_VPENDBASER);
  2041. do {
  2042. val = gits_read_vpendbaser(vlpi_base + GICR_VPENDBASER);
  2043. clean = !(val & GICR_VPENDBASER_Dirty);
  2044. if (!clean) {
  2045. count--;
  2046. cpu_relax();
  2047. udelay(1);
  2048. }
  2049. } while (!clean && count);
  2050. if (unlikely(!clean && !count)) {
  2051. pr_err_ratelimited("ITS virtual pending table not cleaning\n");
  2052. vpe->idai = false;
  2053. vpe->pending_last = true;
  2054. } else {
  2055. vpe->idai = !!(val & GICR_VPENDBASER_IDAI);
  2056. vpe->pending_last = !!(val & GICR_VPENDBASER_PendingLast);
  2057. }
  2058. }
  2059. static void its_vpe_invall(struct its_vpe *vpe)
  2060. {
  2061. struct its_node *its;
  2062. list_for_each_entry(its, &its_nodes, entry) {
  2063. if (!its->is_v4)
  2064. continue;
  2065. if (its_list_map && !vpe->its_vm->vlpi_count[its->list_nr])
  2066. continue;
  2067. /*
  2068. * Sending a VINVALL to a single ITS is enough, as all
  2069. * we need is to reach the redistributors.
  2070. */
  2071. its_send_vinvall(its, vpe);
  2072. return;
  2073. }
  2074. }
  2075. static int its_vpe_set_vcpu_affinity(struct irq_data *d, void *vcpu_info)
  2076. {
  2077. struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
  2078. struct its_cmd_info *info = vcpu_info;
  2079. switch (info->cmd_type) {
  2080. case SCHEDULE_VPE:
  2081. its_vpe_schedule(vpe);
  2082. return 0;
  2083. case DESCHEDULE_VPE:
  2084. its_vpe_deschedule(vpe);
  2085. return 0;
  2086. case INVALL_VPE:
  2087. its_vpe_invall(vpe);
  2088. return 0;
  2089. default:
  2090. return -EINVAL;
  2091. }
  2092. }
  2093. static void its_vpe_send_cmd(struct its_vpe *vpe,
  2094. void (*cmd)(struct its_device *, u32))
  2095. {
  2096. unsigned long flags;
  2097. raw_spin_lock_irqsave(&vpe_proxy.lock, flags);
  2098. its_vpe_db_proxy_map_locked(vpe);
  2099. cmd(vpe_proxy.dev, vpe->vpe_proxy_event);
  2100. raw_spin_unlock_irqrestore(&vpe_proxy.lock, flags);
  2101. }
  2102. static void its_vpe_send_inv(struct irq_data *d)
  2103. {
  2104. struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
  2105. if (gic_rdists->has_direct_lpi) {
  2106. void __iomem *rdbase;
  2107. rdbase = per_cpu_ptr(gic_rdists->rdist, vpe->col_idx)->rd_base;
  2108. gic_write_lpir(vpe->vpe_db_lpi, rdbase + GICR_INVLPIR);
  2109. while (gic_read_lpir(rdbase + GICR_SYNCR) & 1)
  2110. cpu_relax();
  2111. } else {
  2112. its_vpe_send_cmd(vpe, its_send_inv);
  2113. }
  2114. }
  2115. static void its_vpe_mask_irq(struct irq_data *d)
  2116. {
  2117. /*
  2118. * We need to unmask the LPI, which is described by the parent
  2119. * irq_data. Instead of calling into the parent (which won't
  2120. * exactly do the right thing, let's simply use the
  2121. * parent_data pointer. Yes, I'm naughty.
  2122. */
  2123. lpi_write_config(d->parent_data, LPI_PROP_ENABLED, 0);
  2124. its_vpe_send_inv(d);
  2125. }
  2126. static void its_vpe_unmask_irq(struct irq_data *d)
  2127. {
  2128. /* Same hack as above... */
  2129. lpi_write_config(d->parent_data, 0, LPI_PROP_ENABLED);
  2130. its_vpe_send_inv(d);
  2131. }
  2132. static int its_vpe_set_irqchip_state(struct irq_data *d,
  2133. enum irqchip_irq_state which,
  2134. bool state)
  2135. {
  2136. struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
  2137. if (which != IRQCHIP_STATE_PENDING)
  2138. return -EINVAL;
  2139. if (gic_rdists->has_direct_lpi) {
  2140. void __iomem *rdbase;
  2141. rdbase = per_cpu_ptr(gic_rdists->rdist, vpe->col_idx)->rd_base;
  2142. if (state) {
  2143. gic_write_lpir(vpe->vpe_db_lpi, rdbase + GICR_SETLPIR);
  2144. } else {
  2145. gic_write_lpir(vpe->vpe_db_lpi, rdbase + GICR_CLRLPIR);
  2146. while (gic_read_lpir(rdbase + GICR_SYNCR) & 1)
  2147. cpu_relax();
  2148. }
  2149. } else {
  2150. if (state)
  2151. its_vpe_send_cmd(vpe, its_send_int);
  2152. else
  2153. its_vpe_send_cmd(vpe, its_send_clear);
  2154. }
  2155. return 0;
  2156. }
  2157. static struct irq_chip its_vpe_irq_chip = {
  2158. .name = "GICv4-vpe",
  2159. .irq_mask = its_vpe_mask_irq,
  2160. .irq_unmask = its_vpe_unmask_irq,
  2161. .irq_eoi = irq_chip_eoi_parent,
  2162. .irq_set_affinity = its_vpe_set_affinity,
  2163. .irq_set_irqchip_state = its_vpe_set_irqchip_state,
  2164. .irq_set_vcpu_affinity = its_vpe_set_vcpu_affinity,
  2165. };
  2166. static int its_vpe_id_alloc(void)
  2167. {
  2168. return ida_simple_get(&its_vpeid_ida, 0, ITS_MAX_VPEID, GFP_KERNEL);
  2169. }
  2170. static void its_vpe_id_free(u16 id)
  2171. {
  2172. ida_simple_remove(&its_vpeid_ida, id);
  2173. }
  2174. static int its_vpe_init(struct its_vpe *vpe)
  2175. {
  2176. struct page *vpt_page;
  2177. int vpe_id;
  2178. /* Allocate vpe_id */
  2179. vpe_id = its_vpe_id_alloc();
  2180. if (vpe_id < 0)
  2181. return vpe_id;
  2182. /* Allocate VPT */
  2183. vpt_page = its_allocate_pending_table(GFP_KERNEL);
  2184. if (!vpt_page) {
  2185. its_vpe_id_free(vpe_id);
  2186. return -ENOMEM;
  2187. }
  2188. if (!its_alloc_vpe_table(vpe_id)) {
  2189. its_vpe_id_free(vpe_id);
  2190. its_free_pending_table(vpe->vpt_page);
  2191. return -ENOMEM;
  2192. }
  2193. vpe->vpe_id = vpe_id;
  2194. vpe->vpt_page = vpt_page;
  2195. vpe->vpe_proxy_event = -1;
  2196. return 0;
  2197. }
  2198. static void its_vpe_teardown(struct its_vpe *vpe)
  2199. {
  2200. its_vpe_db_proxy_unmap(vpe);
  2201. its_vpe_id_free(vpe->vpe_id);
  2202. its_free_pending_table(vpe->vpt_page);
  2203. }
  2204. static void its_vpe_irq_domain_free(struct irq_domain *domain,
  2205. unsigned int virq,
  2206. unsigned int nr_irqs)
  2207. {
  2208. struct its_vm *vm = domain->host_data;
  2209. int i;
  2210. irq_domain_free_irqs_parent(domain, virq, nr_irqs);
  2211. for (i = 0; i < nr_irqs; i++) {
  2212. struct irq_data *data = irq_domain_get_irq_data(domain,
  2213. virq + i);
  2214. struct its_vpe *vpe = irq_data_get_irq_chip_data(data);
  2215. BUG_ON(vm != vpe->its_vm);
  2216. clear_bit(data->hwirq, vm->db_bitmap);
  2217. its_vpe_teardown(vpe);
  2218. irq_domain_reset_irq_data(data);
  2219. }
  2220. if (bitmap_empty(vm->db_bitmap, vm->nr_db_lpis)) {
  2221. its_lpi_free_chunks(vm->db_bitmap, vm->db_lpi_base, vm->nr_db_lpis);
  2222. its_free_prop_table(vm->vprop_page);
  2223. }
  2224. }
  2225. static int its_vpe_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
  2226. unsigned int nr_irqs, void *args)
  2227. {
  2228. struct its_vm *vm = args;
  2229. unsigned long *bitmap;
  2230. struct page *vprop_page;
  2231. int base, nr_ids, i, err = 0;
  2232. BUG_ON(!vm);
  2233. bitmap = its_lpi_alloc_chunks(nr_irqs, &base, &nr_ids);
  2234. if (!bitmap)
  2235. return -ENOMEM;
  2236. if (nr_ids < nr_irqs) {
  2237. its_lpi_free_chunks(bitmap, base, nr_ids);
  2238. return -ENOMEM;
  2239. }
  2240. vprop_page = its_allocate_prop_table(GFP_KERNEL);
  2241. if (!vprop_page) {
  2242. its_lpi_free_chunks(bitmap, base, nr_ids);
  2243. return -ENOMEM;
  2244. }
  2245. vm->db_bitmap = bitmap;
  2246. vm->db_lpi_base = base;
  2247. vm->nr_db_lpis = nr_ids;
  2248. vm->vprop_page = vprop_page;
  2249. for (i = 0; i < nr_irqs; i++) {
  2250. vm->vpes[i]->vpe_db_lpi = base + i;
  2251. err = its_vpe_init(vm->vpes[i]);
  2252. if (err)
  2253. break;
  2254. err = its_irq_gic_domain_alloc(domain, virq + i,
  2255. vm->vpes[i]->vpe_db_lpi);
  2256. if (err)
  2257. break;
  2258. irq_domain_set_hwirq_and_chip(domain, virq + i, i,
  2259. &its_vpe_irq_chip, vm->vpes[i]);
  2260. set_bit(i, bitmap);
  2261. }
  2262. if (err) {
  2263. if (i > 0)
  2264. its_vpe_irq_domain_free(domain, virq, i - 1);
  2265. its_lpi_free_chunks(bitmap, base, nr_ids);
  2266. its_free_prop_table(vprop_page);
  2267. }
  2268. return err;
  2269. }
  2270. static int its_vpe_irq_domain_activate(struct irq_domain *domain,
  2271. struct irq_data *d, bool reserve)
  2272. {
  2273. struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
  2274. struct its_node *its;
  2275. /* If we use the list map, we issue VMAPP on demand... */
  2276. if (its_list_map)
  2277. return 0;
  2278. /* Map the VPE to the first possible CPU */
  2279. vpe->col_idx = cpumask_first(cpu_online_mask);
  2280. list_for_each_entry(its, &its_nodes, entry) {
  2281. if (!its->is_v4)
  2282. continue;
  2283. its_send_vmapp(its, vpe, true);
  2284. its_send_vinvall(its, vpe);
  2285. }
  2286. irq_data_update_effective_affinity(d, cpumask_of(vpe->col_idx));
  2287. return 0;
  2288. }
  2289. static void its_vpe_irq_domain_deactivate(struct irq_domain *domain,
  2290. struct irq_data *d)
  2291. {
  2292. struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
  2293. struct its_node *its;
  2294. /*
  2295. * If we use the list map, we unmap the VPE once no VLPIs are
  2296. * associated with the VM.
  2297. */
  2298. if (its_list_map)
  2299. return;
  2300. list_for_each_entry(its, &its_nodes, entry) {
  2301. if (!its->is_v4)
  2302. continue;
  2303. its_send_vmapp(its, vpe, false);
  2304. }
  2305. }
  2306. static const struct irq_domain_ops its_vpe_domain_ops = {
  2307. .alloc = its_vpe_irq_domain_alloc,
  2308. .free = its_vpe_irq_domain_free,
  2309. .activate = its_vpe_irq_domain_activate,
  2310. .deactivate = its_vpe_irq_domain_deactivate,
  2311. };
  2312. static int its_force_quiescent(void __iomem *base)
  2313. {
  2314. u32 count = 1000000; /* 1s */
  2315. u32 val;
  2316. val = readl_relaxed(base + GITS_CTLR);
  2317. /*
  2318. * GIC architecture specification requires the ITS to be both
  2319. * disabled and quiescent for writes to GITS_BASER<n> or
  2320. * GITS_CBASER to not have UNPREDICTABLE results.
  2321. */
  2322. if ((val & GITS_CTLR_QUIESCENT) && !(val & GITS_CTLR_ENABLE))
  2323. return 0;
  2324. /* Disable the generation of all interrupts to this ITS */
  2325. val &= ~(GITS_CTLR_ENABLE | GITS_CTLR_ImDe);
  2326. writel_relaxed(val, base + GITS_CTLR);
  2327. /* Poll GITS_CTLR and wait until ITS becomes quiescent */
  2328. while (1) {
  2329. val = readl_relaxed(base + GITS_CTLR);
  2330. if (val & GITS_CTLR_QUIESCENT)
  2331. return 0;
  2332. count--;
  2333. if (!count)
  2334. return -EBUSY;
  2335. cpu_relax();
  2336. udelay(1);
  2337. }
  2338. }
  2339. static bool __maybe_unused its_enable_quirk_cavium_22375(void *data)
  2340. {
  2341. struct its_node *its = data;
  2342. /* erratum 22375: only alloc 8MB table size */
  2343. its->device_ids = 0x14; /* 20 bits, 8MB */
  2344. its->flags |= ITS_FLAGS_WORKAROUND_CAVIUM_22375;
  2345. return true;
  2346. }
  2347. static bool __maybe_unused its_enable_quirk_cavium_23144(void *data)
  2348. {
  2349. struct its_node *its = data;
  2350. its->flags |= ITS_FLAGS_WORKAROUND_CAVIUM_23144;
  2351. return true;
  2352. }
  2353. static bool __maybe_unused its_enable_quirk_qdf2400_e0065(void *data)
  2354. {
  2355. struct its_node *its = data;
  2356. /* On QDF2400, the size of the ITE is 16Bytes */
  2357. its->ite_size = 16;
  2358. return true;
  2359. }
  2360. static u64 its_irq_get_msi_base_pre_its(struct its_device *its_dev)
  2361. {
  2362. struct its_node *its = its_dev->its;
  2363. /*
  2364. * The Socionext Synquacer SoC has a so-called 'pre-ITS',
  2365. * which maps 32-bit writes targeted at a separate window of
  2366. * size '4 << device_id_bits' onto writes to GITS_TRANSLATER
  2367. * with device ID taken from bits [device_id_bits + 1:2] of
  2368. * the window offset.
  2369. */
  2370. return its->pre_its_base + (its_dev->device_id << 2);
  2371. }
  2372. static bool __maybe_unused its_enable_quirk_socionext_synquacer(void *data)
  2373. {
  2374. struct its_node *its = data;
  2375. u32 pre_its_window[2];
  2376. u32 ids;
  2377. if (!fwnode_property_read_u32_array(its->fwnode_handle,
  2378. "socionext,synquacer-pre-its",
  2379. pre_its_window,
  2380. ARRAY_SIZE(pre_its_window))) {
  2381. its->pre_its_base = pre_its_window[0];
  2382. its->get_msi_base = its_irq_get_msi_base_pre_its;
  2383. ids = ilog2(pre_its_window[1]) - 2;
  2384. if (its->device_ids > ids)
  2385. its->device_ids = ids;
  2386. /* the pre-ITS breaks isolation, so disable MSI remapping */
  2387. its->msi_domain_flags &= ~IRQ_DOMAIN_FLAG_MSI_REMAP;
  2388. return true;
  2389. }
  2390. return false;
  2391. }
  2392. static bool __maybe_unused its_enable_quirk_hip07_161600802(void *data)
  2393. {
  2394. struct its_node *its = data;
  2395. /*
  2396. * Hip07 insists on using the wrong address for the VLPI
  2397. * page. Trick it into doing the right thing...
  2398. */
  2399. its->vlpi_redist_offset = SZ_128K;
  2400. return true;
  2401. }
  2402. static const struct gic_quirk its_quirks[] = {
  2403. #ifdef CONFIG_CAVIUM_ERRATUM_22375
  2404. {
  2405. .desc = "ITS: Cavium errata 22375, 24313",
  2406. .iidr = 0xa100034c, /* ThunderX pass 1.x */
  2407. .mask = 0xffff0fff,
  2408. .init = its_enable_quirk_cavium_22375,
  2409. },
  2410. #endif
  2411. #ifdef CONFIG_CAVIUM_ERRATUM_23144
  2412. {
  2413. .desc = "ITS: Cavium erratum 23144",
  2414. .iidr = 0xa100034c, /* ThunderX pass 1.x */
  2415. .mask = 0xffff0fff,
  2416. .init = its_enable_quirk_cavium_23144,
  2417. },
  2418. #endif
  2419. #ifdef CONFIG_QCOM_QDF2400_ERRATUM_0065
  2420. {
  2421. .desc = "ITS: QDF2400 erratum 0065",
  2422. .iidr = 0x00001070, /* QDF2400 ITS rev 1.x */
  2423. .mask = 0xffffffff,
  2424. .init = its_enable_quirk_qdf2400_e0065,
  2425. },
  2426. #endif
  2427. #ifdef CONFIG_SOCIONEXT_SYNQUACER_PREITS
  2428. {
  2429. /*
  2430. * The Socionext Synquacer SoC incorporates ARM's own GIC-500
  2431. * implementation, but with a 'pre-ITS' added that requires
  2432. * special handling in software.
  2433. */
  2434. .desc = "ITS: Socionext Synquacer pre-ITS",
  2435. .iidr = 0x0001143b,
  2436. .mask = 0xffffffff,
  2437. .init = its_enable_quirk_socionext_synquacer,
  2438. },
  2439. #endif
  2440. #ifdef CONFIG_HISILICON_ERRATUM_161600802
  2441. {
  2442. .desc = "ITS: Hip07 erratum 161600802",
  2443. .iidr = 0x00000004,
  2444. .mask = 0xffffffff,
  2445. .init = its_enable_quirk_hip07_161600802,
  2446. },
  2447. #endif
  2448. {
  2449. }
  2450. };
  2451. static void its_enable_quirks(struct its_node *its)
  2452. {
  2453. u32 iidr = readl_relaxed(its->base + GITS_IIDR);
  2454. gic_enable_quirks(iidr, its_quirks, its);
  2455. }
  2456. static int its_save_disable(void)
  2457. {
  2458. struct its_node *its;
  2459. int err = 0;
  2460. spin_lock(&its_lock);
  2461. list_for_each_entry(its, &its_nodes, entry) {
  2462. void __iomem *base;
  2463. if (!(its->flags & ITS_FLAGS_SAVE_SUSPEND_STATE))
  2464. continue;
  2465. base = its->base;
  2466. its->ctlr_save = readl_relaxed(base + GITS_CTLR);
  2467. err = its_force_quiescent(base);
  2468. if (err) {
  2469. pr_err("ITS@%pa: failed to quiesce: %d\n",
  2470. &its->phys_base, err);
  2471. writel_relaxed(its->ctlr_save, base + GITS_CTLR);
  2472. goto err;
  2473. }
  2474. its->cbaser_save = gits_read_cbaser(base + GITS_CBASER);
  2475. }
  2476. err:
  2477. if (err) {
  2478. list_for_each_entry_continue_reverse(its, &its_nodes, entry) {
  2479. void __iomem *base;
  2480. if (!(its->flags & ITS_FLAGS_SAVE_SUSPEND_STATE))
  2481. continue;
  2482. base = its->base;
  2483. writel_relaxed(its->ctlr_save, base + GITS_CTLR);
  2484. }
  2485. }
  2486. spin_unlock(&its_lock);
  2487. return err;
  2488. }
  2489. static void its_restore_enable(void)
  2490. {
  2491. struct its_node *its;
  2492. int ret;
  2493. spin_lock(&its_lock);
  2494. list_for_each_entry(its, &its_nodes, entry) {
  2495. void __iomem *base;
  2496. int i;
  2497. if (!(its->flags & ITS_FLAGS_SAVE_SUSPEND_STATE))
  2498. continue;
  2499. base = its->base;
  2500. /*
  2501. * Make sure that the ITS is disabled. If it fails to quiesce,
  2502. * don't restore it since writing to CBASER or BASER<n>
  2503. * registers is undefined according to the GIC v3 ITS
  2504. * Specification.
  2505. */
  2506. ret = its_force_quiescent(base);
  2507. if (ret) {
  2508. pr_err("ITS@%pa: failed to quiesce on resume: %d\n",
  2509. &its->phys_base, ret);
  2510. continue;
  2511. }
  2512. gits_write_cbaser(its->cbaser_save, base + GITS_CBASER);
  2513. /*
  2514. * Writing CBASER resets CREADR to 0, so make CWRITER and
  2515. * cmd_write line up with it.
  2516. */
  2517. its->cmd_write = its->cmd_base;
  2518. gits_write_cwriter(0, base + GITS_CWRITER);
  2519. /* Restore GITS_BASER from the value cache. */
  2520. for (i = 0; i < GITS_BASER_NR_REGS; i++) {
  2521. struct its_baser *baser = &its->tables[i];
  2522. if (!(baser->val & GITS_BASER_VALID))
  2523. continue;
  2524. its_write_baser(its, baser, baser->val);
  2525. }
  2526. writel_relaxed(its->ctlr_save, base + GITS_CTLR);
  2527. /*
  2528. * Reinit the collection if it's stored in the ITS. This is
  2529. * indicated by the col_id being less than the HCC field.
  2530. * CID < HCC as specified in the GIC v3 Documentation.
  2531. */
  2532. if (its->collections[smp_processor_id()].col_id <
  2533. GITS_TYPER_HCC(gic_read_typer(base + GITS_TYPER)))
  2534. its_cpu_init_collection(its);
  2535. }
  2536. spin_unlock(&its_lock);
  2537. }
  2538. static struct syscore_ops its_syscore_ops = {
  2539. .suspend = its_save_disable,
  2540. .resume = its_restore_enable,
  2541. };
  2542. static int its_init_domain(struct fwnode_handle *handle, struct its_node *its)
  2543. {
  2544. struct irq_domain *inner_domain;
  2545. struct msi_domain_info *info;
  2546. info = kzalloc(sizeof(*info), GFP_KERNEL);
  2547. if (!info)
  2548. return -ENOMEM;
  2549. inner_domain = irq_domain_create_tree(handle, &its_domain_ops, its);
  2550. if (!inner_domain) {
  2551. kfree(info);
  2552. return -ENOMEM;
  2553. }
  2554. inner_domain->parent = its_parent;
  2555. irq_domain_update_bus_token(inner_domain, DOMAIN_BUS_NEXUS);
  2556. inner_domain->flags |= its->msi_domain_flags;
  2557. info->ops = &its_msi_domain_ops;
  2558. info->data = its;
  2559. inner_domain->host_data = info;
  2560. return 0;
  2561. }
  2562. static int its_init_vpe_domain(void)
  2563. {
  2564. struct its_node *its;
  2565. u32 devid;
  2566. int entries;
  2567. if (gic_rdists->has_direct_lpi) {
  2568. pr_info("ITS: Using DirectLPI for VPE invalidation\n");
  2569. return 0;
  2570. }
  2571. /* Any ITS will do, even if not v4 */
  2572. its = list_first_entry(&its_nodes, struct its_node, entry);
  2573. entries = roundup_pow_of_two(nr_cpu_ids);
  2574. vpe_proxy.vpes = kzalloc(sizeof(*vpe_proxy.vpes) * entries,
  2575. GFP_KERNEL);
  2576. if (!vpe_proxy.vpes) {
  2577. pr_err("ITS: Can't allocate GICv4 proxy device array\n");
  2578. return -ENOMEM;
  2579. }
  2580. /* Use the last possible DevID */
  2581. devid = GENMASK(its->device_ids - 1, 0);
  2582. vpe_proxy.dev = its_create_device(its, devid, entries, false);
  2583. if (!vpe_proxy.dev) {
  2584. kfree(vpe_proxy.vpes);
  2585. pr_err("ITS: Can't allocate GICv4 proxy device\n");
  2586. return -ENOMEM;
  2587. }
  2588. BUG_ON(entries > vpe_proxy.dev->nr_ites);
  2589. raw_spin_lock_init(&vpe_proxy.lock);
  2590. vpe_proxy.next_victim = 0;
  2591. pr_info("ITS: Allocated DevID %x as GICv4 proxy device (%d slots)\n",
  2592. devid, vpe_proxy.dev->nr_ites);
  2593. return 0;
  2594. }
  2595. static int __init its_compute_its_list_map(struct resource *res,
  2596. void __iomem *its_base)
  2597. {
  2598. int its_number;
  2599. u32 ctlr;
  2600. /*
  2601. * This is assumed to be done early enough that we're
  2602. * guaranteed to be single-threaded, hence no
  2603. * locking. Should this change, we should address
  2604. * this.
  2605. */
  2606. its_number = find_first_zero_bit(&its_list_map, GICv4_ITS_LIST_MAX);
  2607. if (its_number >= GICv4_ITS_LIST_MAX) {
  2608. pr_err("ITS@%pa: No ITSList entry available!\n",
  2609. &res->start);
  2610. return -EINVAL;
  2611. }
  2612. ctlr = readl_relaxed(its_base + GITS_CTLR);
  2613. ctlr &= ~GITS_CTLR_ITS_NUMBER;
  2614. ctlr |= its_number << GITS_CTLR_ITS_NUMBER_SHIFT;
  2615. writel_relaxed(ctlr, its_base + GITS_CTLR);
  2616. ctlr = readl_relaxed(its_base + GITS_CTLR);
  2617. if ((ctlr & GITS_CTLR_ITS_NUMBER) != (its_number << GITS_CTLR_ITS_NUMBER_SHIFT)) {
  2618. its_number = ctlr & GITS_CTLR_ITS_NUMBER;
  2619. its_number >>= GITS_CTLR_ITS_NUMBER_SHIFT;
  2620. }
  2621. if (test_and_set_bit(its_number, &its_list_map)) {
  2622. pr_err("ITS@%pa: Duplicate ITSList entry %d\n",
  2623. &res->start, its_number);
  2624. return -EINVAL;
  2625. }
  2626. return its_number;
  2627. }
  2628. static int __init its_probe_one(struct resource *res,
  2629. struct fwnode_handle *handle, int numa_node)
  2630. {
  2631. struct its_node *its;
  2632. void __iomem *its_base;
  2633. u32 val, ctlr;
  2634. u64 baser, tmp, typer;
  2635. int err;
  2636. its_base = ioremap(res->start, resource_size(res));
  2637. if (!its_base) {
  2638. pr_warn("ITS@%pa: Unable to map ITS registers\n", &res->start);
  2639. return -ENOMEM;
  2640. }
  2641. val = readl_relaxed(its_base + GITS_PIDR2) & GIC_PIDR2_ARCH_MASK;
  2642. if (val != 0x30 && val != 0x40) {
  2643. pr_warn("ITS@%pa: No ITS detected, giving up\n", &res->start);
  2644. err = -ENODEV;
  2645. goto out_unmap;
  2646. }
  2647. err = its_force_quiescent(its_base);
  2648. if (err) {
  2649. pr_warn("ITS@%pa: Failed to quiesce, giving up\n", &res->start);
  2650. goto out_unmap;
  2651. }
  2652. pr_info("ITS %pR\n", res);
  2653. its = kzalloc(sizeof(*its), GFP_KERNEL);
  2654. if (!its) {
  2655. err = -ENOMEM;
  2656. goto out_unmap;
  2657. }
  2658. raw_spin_lock_init(&its->lock);
  2659. INIT_LIST_HEAD(&its->entry);
  2660. INIT_LIST_HEAD(&its->its_device_list);
  2661. typer = gic_read_typer(its_base + GITS_TYPER);
  2662. its->base = its_base;
  2663. its->phys_base = res->start;
  2664. its->ite_size = GITS_TYPER_ITT_ENTRY_SIZE(typer);
  2665. its->device_ids = GITS_TYPER_DEVBITS(typer);
  2666. its->is_v4 = !!(typer & GITS_TYPER_VLPIS);
  2667. if (its->is_v4) {
  2668. if (!(typer & GITS_TYPER_VMOVP)) {
  2669. err = its_compute_its_list_map(res, its_base);
  2670. if (err < 0)
  2671. goto out_free_its;
  2672. its->list_nr = err;
  2673. pr_info("ITS@%pa: Using ITS number %d\n",
  2674. &res->start, err);
  2675. } else {
  2676. pr_info("ITS@%pa: Single VMOVP capable\n", &res->start);
  2677. }
  2678. }
  2679. its->numa_node = numa_node;
  2680. its->cmd_base = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
  2681. get_order(ITS_CMD_QUEUE_SZ));
  2682. if (!its->cmd_base) {
  2683. err = -ENOMEM;
  2684. goto out_free_its;
  2685. }
  2686. its->cmd_write = its->cmd_base;
  2687. its->fwnode_handle = handle;
  2688. its->get_msi_base = its_irq_get_msi_base;
  2689. its->msi_domain_flags = IRQ_DOMAIN_FLAG_MSI_REMAP;
  2690. its_enable_quirks(its);
  2691. err = its_alloc_tables(its);
  2692. if (err)
  2693. goto out_free_cmd;
  2694. err = its_alloc_collections(its);
  2695. if (err)
  2696. goto out_free_tables;
  2697. baser = (virt_to_phys(its->cmd_base) |
  2698. GITS_CBASER_RaWaWb |
  2699. GITS_CBASER_InnerShareable |
  2700. (ITS_CMD_QUEUE_SZ / SZ_4K - 1) |
  2701. GITS_CBASER_VALID);
  2702. gits_write_cbaser(baser, its->base + GITS_CBASER);
  2703. tmp = gits_read_cbaser(its->base + GITS_CBASER);
  2704. if ((tmp ^ baser) & GITS_CBASER_SHAREABILITY_MASK) {
  2705. if (!(tmp & GITS_CBASER_SHAREABILITY_MASK)) {
  2706. /*
  2707. * The HW reports non-shareable, we must
  2708. * remove the cacheability attributes as
  2709. * well.
  2710. */
  2711. baser &= ~(GITS_CBASER_SHAREABILITY_MASK |
  2712. GITS_CBASER_CACHEABILITY_MASK);
  2713. baser |= GITS_CBASER_nC;
  2714. gits_write_cbaser(baser, its->base + GITS_CBASER);
  2715. }
  2716. pr_info("ITS: using cache flushing for cmd queue\n");
  2717. its->flags |= ITS_FLAGS_CMDQ_NEEDS_FLUSHING;
  2718. }
  2719. gits_write_cwriter(0, its->base + GITS_CWRITER);
  2720. ctlr = readl_relaxed(its->base + GITS_CTLR);
  2721. ctlr |= GITS_CTLR_ENABLE;
  2722. if (its->is_v4)
  2723. ctlr |= GITS_CTLR_ImDe;
  2724. writel_relaxed(ctlr, its->base + GITS_CTLR);
  2725. if (GITS_TYPER_HCC(typer))
  2726. its->flags |= ITS_FLAGS_SAVE_SUSPEND_STATE;
  2727. err = its_init_domain(handle, its);
  2728. if (err)
  2729. goto out_free_tables;
  2730. spin_lock(&its_lock);
  2731. list_add(&its->entry, &its_nodes);
  2732. spin_unlock(&its_lock);
  2733. return 0;
  2734. out_free_tables:
  2735. its_free_tables(its);
  2736. out_free_cmd:
  2737. free_pages((unsigned long)its->cmd_base, get_order(ITS_CMD_QUEUE_SZ));
  2738. out_free_its:
  2739. kfree(its);
  2740. out_unmap:
  2741. iounmap(its_base);
  2742. pr_err("ITS@%pa: failed probing (%d)\n", &res->start, err);
  2743. return err;
  2744. }
  2745. static bool gic_rdists_supports_plpis(void)
  2746. {
  2747. return !!(gic_read_typer(gic_data_rdist_rd_base() + GICR_TYPER) & GICR_TYPER_PLPIS);
  2748. }
  2749. static int redist_disable_lpis(void)
  2750. {
  2751. void __iomem *rbase = gic_data_rdist_rd_base();
  2752. u64 timeout = USEC_PER_SEC;
  2753. u64 val;
  2754. if (!gic_rdists_supports_plpis()) {
  2755. pr_info("CPU%d: LPIs not supported\n", smp_processor_id());
  2756. return -ENXIO;
  2757. }
  2758. val = readl_relaxed(rbase + GICR_CTLR);
  2759. if (!(val & GICR_CTLR_ENABLE_LPIS))
  2760. return 0;
  2761. pr_warn("CPU%d: Booted with LPIs enabled, memory probably corrupted\n",
  2762. smp_processor_id());
  2763. add_taint(TAINT_CRAP, LOCKDEP_STILL_OK);
  2764. /* Disable LPIs */
  2765. val &= ~GICR_CTLR_ENABLE_LPIS;
  2766. writel_relaxed(val, rbase + GICR_CTLR);
  2767. /* Make sure any change to GICR_CTLR is observable by the GIC */
  2768. dsb(sy);
  2769. /*
  2770. * Software must observe RWP==0 after clearing GICR_CTLR.EnableLPIs
  2771. * from 1 to 0 before programming GICR_PEND{PROP}BASER registers.
  2772. * Error out if we time out waiting for RWP to clear.
  2773. */
  2774. while (readl_relaxed(rbase + GICR_CTLR) & GICR_CTLR_RWP) {
  2775. if (!timeout) {
  2776. pr_err("CPU%d: Timeout while disabling LPIs\n",
  2777. smp_processor_id());
  2778. return -ETIMEDOUT;
  2779. }
  2780. udelay(1);
  2781. timeout--;
  2782. }
  2783. /*
  2784. * After it has been written to 1, it is IMPLEMENTATION
  2785. * DEFINED whether GICR_CTLR.EnableLPI becomes RES1 or can be
  2786. * cleared to 0. Error out if clearing the bit failed.
  2787. */
  2788. if (readl_relaxed(rbase + GICR_CTLR) & GICR_CTLR_ENABLE_LPIS) {
  2789. pr_err("CPU%d: Failed to disable LPIs\n", smp_processor_id());
  2790. return -EBUSY;
  2791. }
  2792. return 0;
  2793. }
  2794. int its_cpu_init(void)
  2795. {
  2796. if (!list_empty(&its_nodes)) {
  2797. int ret;
  2798. ret = redist_disable_lpis();
  2799. if (ret)
  2800. return ret;
  2801. its_cpu_init_lpis();
  2802. its_cpu_init_collections();
  2803. }
  2804. return 0;
  2805. }
  2806. static const struct of_device_id its_device_id[] = {
  2807. { .compatible = "arm,gic-v3-its", },
  2808. {},
  2809. };
  2810. static int __init its_of_probe(struct device_node *node)
  2811. {
  2812. struct device_node *np;
  2813. struct resource res;
  2814. for (np = of_find_matching_node(node, its_device_id); np;
  2815. np = of_find_matching_node(np, its_device_id)) {
  2816. if (!of_device_is_available(np))
  2817. continue;
  2818. if (!of_property_read_bool(np, "msi-controller")) {
  2819. pr_warn("%pOF: no msi-controller property, ITS ignored\n",
  2820. np);
  2821. continue;
  2822. }
  2823. if (of_address_to_resource(np, 0, &res)) {
  2824. pr_warn("%pOF: no regs?\n", np);
  2825. continue;
  2826. }
  2827. its_probe_one(&res, &np->fwnode, of_node_to_nid(np));
  2828. }
  2829. return 0;
  2830. }
  2831. #ifdef CONFIG_ACPI
  2832. #define ACPI_GICV3_ITS_MEM_SIZE (SZ_128K)
  2833. #ifdef CONFIG_ACPI_NUMA
  2834. struct its_srat_map {
  2835. /* numa node id */
  2836. u32 numa_node;
  2837. /* GIC ITS ID */
  2838. u32 its_id;
  2839. };
  2840. static struct its_srat_map *its_srat_maps __initdata;
  2841. static int its_in_srat __initdata;
  2842. static int __init acpi_get_its_numa_node(u32 its_id)
  2843. {
  2844. int i;
  2845. for (i = 0; i < its_in_srat; i++) {
  2846. if (its_id == its_srat_maps[i].its_id)
  2847. return its_srat_maps[i].numa_node;
  2848. }
  2849. return NUMA_NO_NODE;
  2850. }
  2851. static int __init gic_acpi_match_srat_its(struct acpi_subtable_header *header,
  2852. const unsigned long end)
  2853. {
  2854. return 0;
  2855. }
  2856. static int __init gic_acpi_parse_srat_its(struct acpi_subtable_header *header,
  2857. const unsigned long end)
  2858. {
  2859. int node;
  2860. struct acpi_srat_gic_its_affinity *its_affinity;
  2861. its_affinity = (struct acpi_srat_gic_its_affinity *)header;
  2862. if (!its_affinity)
  2863. return -EINVAL;
  2864. if (its_affinity->header.length < sizeof(*its_affinity)) {
  2865. pr_err("SRAT: Invalid header length %d in ITS affinity\n",
  2866. its_affinity->header.length);
  2867. return -EINVAL;
  2868. }
  2869. node = acpi_map_pxm_to_node(its_affinity->proximity_domain);
  2870. if (node == NUMA_NO_NODE || node >= MAX_NUMNODES) {
  2871. pr_err("SRAT: Invalid NUMA node %d in ITS affinity\n", node);
  2872. return 0;
  2873. }
  2874. its_srat_maps[its_in_srat].numa_node = node;
  2875. its_srat_maps[its_in_srat].its_id = its_affinity->its_id;
  2876. its_in_srat++;
  2877. pr_info("SRAT: PXM %d -> ITS %d -> Node %d\n",
  2878. its_affinity->proximity_domain, its_affinity->its_id, node);
  2879. return 0;
  2880. }
  2881. static void __init acpi_table_parse_srat_its(void)
  2882. {
  2883. int count;
  2884. count = acpi_table_parse_entries(ACPI_SIG_SRAT,
  2885. sizeof(struct acpi_table_srat),
  2886. ACPI_SRAT_TYPE_GIC_ITS_AFFINITY,
  2887. gic_acpi_match_srat_its, 0);
  2888. if (count <= 0)
  2889. return;
  2890. its_srat_maps = kmalloc(count * sizeof(struct its_srat_map),
  2891. GFP_KERNEL);
  2892. if (!its_srat_maps) {
  2893. pr_warn("SRAT: Failed to allocate memory for its_srat_maps!\n");
  2894. return;
  2895. }
  2896. acpi_table_parse_entries(ACPI_SIG_SRAT,
  2897. sizeof(struct acpi_table_srat),
  2898. ACPI_SRAT_TYPE_GIC_ITS_AFFINITY,
  2899. gic_acpi_parse_srat_its, 0);
  2900. }
  2901. /* free the its_srat_maps after ITS probing */
  2902. static void __init acpi_its_srat_maps_free(void)
  2903. {
  2904. kfree(its_srat_maps);
  2905. }
  2906. #else
  2907. static void __init acpi_table_parse_srat_its(void) { }
  2908. static int __init acpi_get_its_numa_node(u32 its_id) { return NUMA_NO_NODE; }
  2909. static void __init acpi_its_srat_maps_free(void) { }
  2910. #endif
  2911. static int __init gic_acpi_parse_madt_its(struct acpi_subtable_header *header,
  2912. const unsigned long end)
  2913. {
  2914. struct acpi_madt_generic_translator *its_entry;
  2915. struct fwnode_handle *dom_handle;
  2916. struct resource res;
  2917. int err;
  2918. its_entry = (struct acpi_madt_generic_translator *)header;
  2919. memset(&res, 0, sizeof(res));
  2920. res.start = its_entry->base_address;
  2921. res.end = its_entry->base_address + ACPI_GICV3_ITS_MEM_SIZE - 1;
  2922. res.flags = IORESOURCE_MEM;
  2923. dom_handle = irq_domain_alloc_fwnode((void *)its_entry->base_address);
  2924. if (!dom_handle) {
  2925. pr_err("ITS@%pa: Unable to allocate GICv3 ITS domain token\n",
  2926. &res.start);
  2927. return -ENOMEM;
  2928. }
  2929. err = iort_register_domain_token(its_entry->translation_id, res.start,
  2930. dom_handle);
  2931. if (err) {
  2932. pr_err("ITS@%pa: Unable to register GICv3 ITS domain token (ITS ID %d) to IORT\n",
  2933. &res.start, its_entry->translation_id);
  2934. goto dom_err;
  2935. }
  2936. err = its_probe_one(&res, dom_handle,
  2937. acpi_get_its_numa_node(its_entry->translation_id));
  2938. if (!err)
  2939. return 0;
  2940. iort_deregister_domain_token(its_entry->translation_id);
  2941. dom_err:
  2942. irq_domain_free_fwnode(dom_handle);
  2943. return err;
  2944. }
  2945. static void __init its_acpi_probe(void)
  2946. {
  2947. acpi_table_parse_srat_its();
  2948. acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_TRANSLATOR,
  2949. gic_acpi_parse_madt_its, 0);
  2950. acpi_its_srat_maps_free();
  2951. }
  2952. #else
  2953. static void __init its_acpi_probe(void) { }
  2954. #endif
  2955. int __init its_init(struct fwnode_handle *handle, struct rdists *rdists,
  2956. struct irq_domain *parent_domain)
  2957. {
  2958. struct device_node *of_node;
  2959. struct its_node *its;
  2960. bool has_v4 = false;
  2961. int err;
  2962. its_parent = parent_domain;
  2963. of_node = to_of_node(handle);
  2964. if (of_node)
  2965. its_of_probe(of_node);
  2966. else
  2967. its_acpi_probe();
  2968. if (list_empty(&its_nodes)) {
  2969. pr_warn("ITS: No ITS available, not enabling LPIs\n");
  2970. return -ENXIO;
  2971. }
  2972. gic_rdists = rdists;
  2973. err = its_alloc_lpi_tables();
  2974. if (err)
  2975. return err;
  2976. list_for_each_entry(its, &its_nodes, entry)
  2977. has_v4 |= its->is_v4;
  2978. if (has_v4 & rdists->has_vlpis) {
  2979. if (its_init_vpe_domain() ||
  2980. its_init_v4(parent_domain, &its_vpe_domain_ops)) {
  2981. rdists->has_vlpis = false;
  2982. pr_err("ITS: Disabling GICv4 support\n");
  2983. }
  2984. }
  2985. register_syscore_ops(&its_syscore_ops);
  2986. return 0;
  2987. }