irq-gic-v3-its.c 92 KB

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