irq-gic-v3-its.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800
  1. /*
  2. * Copyright (C) 2013, 2014 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/bitmap.h>
  18. #include <linux/cpu.h>
  19. #include <linux/delay.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/log2.h>
  22. #include <linux/mm.h>
  23. #include <linux/msi.h>
  24. #include <linux/of.h>
  25. #include <linux/of_address.h>
  26. #include <linux/of_irq.h>
  27. #include <linux/of_pci.h>
  28. #include <linux/of_platform.h>
  29. #include <linux/percpu.h>
  30. #include <linux/slab.h>
  31. #include <linux/irqchip.h>
  32. #include <linux/irqchip/arm-gic-v3.h>
  33. #include <asm/cacheflush.h>
  34. #include <asm/cputype.h>
  35. #include <asm/exception.h>
  36. #include "irq-gic-common.h"
  37. #define ITS_FLAGS_CMDQ_NEEDS_FLUSHING (1ULL << 0)
  38. #define ITS_FLAGS_WORKAROUND_CAVIUM_22375 (1ULL << 1)
  39. #define ITS_FLAGS_WORKAROUND_CAVIUM_23144 (1ULL << 2)
  40. #define RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING (1 << 0)
  41. /*
  42. * Collection structure - just an ID, and a redistributor address to
  43. * ping. We use one per CPU as a bag of interrupts assigned to this
  44. * CPU.
  45. */
  46. struct its_collection {
  47. u64 target_address;
  48. u16 col_id;
  49. };
  50. /*
  51. * The ITS_BASER structure - contains memory information, cached
  52. * value of BASER register configuration and ITS page size.
  53. */
  54. struct its_baser {
  55. void *base;
  56. u64 val;
  57. u32 order;
  58. u32 psz;
  59. };
  60. /*
  61. * The ITS structure - contains most of the infrastructure, with the
  62. * top-level MSI domain, the command queue, the collections, and the
  63. * list of devices writing to it.
  64. */
  65. struct its_node {
  66. raw_spinlock_t lock;
  67. struct list_head entry;
  68. void __iomem *base;
  69. unsigned long phys_base;
  70. struct its_cmd_block *cmd_base;
  71. struct its_cmd_block *cmd_write;
  72. struct its_baser tables[GITS_BASER_NR_REGS];
  73. struct its_collection *collections;
  74. struct list_head its_device_list;
  75. u64 flags;
  76. u32 ite_size;
  77. u32 device_ids;
  78. int numa_node;
  79. };
  80. #define ITS_ITT_ALIGN SZ_256
  81. /* Convert page order to size in bytes */
  82. #define PAGE_ORDER_TO_SIZE(o) (PAGE_SIZE << (o))
  83. struct event_lpi_map {
  84. unsigned long *lpi_map;
  85. u16 *col_map;
  86. irq_hw_number_t lpi_base;
  87. int nr_lpis;
  88. };
  89. /*
  90. * The ITS view of a device - belongs to an ITS, a collection, owns an
  91. * interrupt translation table, and a list of interrupts.
  92. */
  93. struct its_device {
  94. struct list_head entry;
  95. struct its_node *its;
  96. struct event_lpi_map event_map;
  97. void *itt;
  98. u32 nr_ites;
  99. u32 device_id;
  100. };
  101. static LIST_HEAD(its_nodes);
  102. static DEFINE_SPINLOCK(its_lock);
  103. static struct rdists *gic_rdists;
  104. #define gic_data_rdist() (raw_cpu_ptr(gic_rdists->rdist))
  105. #define gic_data_rdist_rd_base() (gic_data_rdist()->rd_base)
  106. static struct its_collection *dev_event_to_col(struct its_device *its_dev,
  107. u32 event)
  108. {
  109. struct its_node *its = its_dev->its;
  110. return its->collections + its_dev->event_map.col_map[event];
  111. }
  112. /*
  113. * ITS command descriptors - parameters to be encoded in a command
  114. * block.
  115. */
  116. struct its_cmd_desc {
  117. union {
  118. struct {
  119. struct its_device *dev;
  120. u32 event_id;
  121. } its_inv_cmd;
  122. struct {
  123. struct its_device *dev;
  124. u32 event_id;
  125. } its_int_cmd;
  126. struct {
  127. struct its_device *dev;
  128. int valid;
  129. } its_mapd_cmd;
  130. struct {
  131. struct its_collection *col;
  132. int valid;
  133. } its_mapc_cmd;
  134. struct {
  135. struct its_device *dev;
  136. u32 phys_id;
  137. u32 event_id;
  138. } its_mapvi_cmd;
  139. struct {
  140. struct its_device *dev;
  141. struct its_collection *col;
  142. u32 event_id;
  143. } its_movi_cmd;
  144. struct {
  145. struct its_device *dev;
  146. u32 event_id;
  147. } its_discard_cmd;
  148. struct {
  149. struct its_collection *col;
  150. } its_invall_cmd;
  151. };
  152. };
  153. /*
  154. * The ITS command block, which is what the ITS actually parses.
  155. */
  156. struct its_cmd_block {
  157. u64 raw_cmd[4];
  158. };
  159. #define ITS_CMD_QUEUE_SZ SZ_64K
  160. #define ITS_CMD_QUEUE_NR_ENTRIES (ITS_CMD_QUEUE_SZ / sizeof(struct its_cmd_block))
  161. typedef struct its_collection *(*its_cmd_builder_t)(struct its_cmd_block *,
  162. struct its_cmd_desc *);
  163. static void its_encode_cmd(struct its_cmd_block *cmd, u8 cmd_nr)
  164. {
  165. cmd->raw_cmd[0] &= ~0xffUL;
  166. cmd->raw_cmd[0] |= cmd_nr;
  167. }
  168. static void its_encode_devid(struct its_cmd_block *cmd, u32 devid)
  169. {
  170. cmd->raw_cmd[0] &= BIT_ULL(32) - 1;
  171. cmd->raw_cmd[0] |= ((u64)devid) << 32;
  172. }
  173. static void its_encode_event_id(struct its_cmd_block *cmd, u32 id)
  174. {
  175. cmd->raw_cmd[1] &= ~0xffffffffUL;
  176. cmd->raw_cmd[1] |= id;
  177. }
  178. static void its_encode_phys_id(struct its_cmd_block *cmd, u32 phys_id)
  179. {
  180. cmd->raw_cmd[1] &= 0xffffffffUL;
  181. cmd->raw_cmd[1] |= ((u64)phys_id) << 32;
  182. }
  183. static void its_encode_size(struct its_cmd_block *cmd, u8 size)
  184. {
  185. cmd->raw_cmd[1] &= ~0x1fUL;
  186. cmd->raw_cmd[1] |= size & 0x1f;
  187. }
  188. static void its_encode_itt(struct its_cmd_block *cmd, u64 itt_addr)
  189. {
  190. cmd->raw_cmd[2] &= ~0xffffffffffffUL;
  191. cmd->raw_cmd[2] |= itt_addr & 0xffffffffff00UL;
  192. }
  193. static void its_encode_valid(struct its_cmd_block *cmd, int valid)
  194. {
  195. cmd->raw_cmd[2] &= ~(1UL << 63);
  196. cmd->raw_cmd[2] |= ((u64)!!valid) << 63;
  197. }
  198. static void its_encode_target(struct its_cmd_block *cmd, u64 target_addr)
  199. {
  200. cmd->raw_cmd[2] &= ~(0xffffffffUL << 16);
  201. cmd->raw_cmd[2] |= (target_addr & (0xffffffffUL << 16));
  202. }
  203. static void its_encode_collection(struct its_cmd_block *cmd, u16 col)
  204. {
  205. cmd->raw_cmd[2] &= ~0xffffUL;
  206. cmd->raw_cmd[2] |= col;
  207. }
  208. static inline void its_fixup_cmd(struct its_cmd_block *cmd)
  209. {
  210. /* Let's fixup BE commands */
  211. cmd->raw_cmd[0] = cpu_to_le64(cmd->raw_cmd[0]);
  212. cmd->raw_cmd[1] = cpu_to_le64(cmd->raw_cmd[1]);
  213. cmd->raw_cmd[2] = cpu_to_le64(cmd->raw_cmd[2]);
  214. cmd->raw_cmd[3] = cpu_to_le64(cmd->raw_cmd[3]);
  215. }
  216. static struct its_collection *its_build_mapd_cmd(struct its_cmd_block *cmd,
  217. struct its_cmd_desc *desc)
  218. {
  219. unsigned long itt_addr;
  220. u8 size = ilog2(desc->its_mapd_cmd.dev->nr_ites);
  221. itt_addr = virt_to_phys(desc->its_mapd_cmd.dev->itt);
  222. itt_addr = ALIGN(itt_addr, ITS_ITT_ALIGN);
  223. its_encode_cmd(cmd, GITS_CMD_MAPD);
  224. its_encode_devid(cmd, desc->its_mapd_cmd.dev->device_id);
  225. its_encode_size(cmd, size - 1);
  226. its_encode_itt(cmd, itt_addr);
  227. its_encode_valid(cmd, desc->its_mapd_cmd.valid);
  228. its_fixup_cmd(cmd);
  229. return NULL;
  230. }
  231. static struct its_collection *its_build_mapc_cmd(struct its_cmd_block *cmd,
  232. struct its_cmd_desc *desc)
  233. {
  234. its_encode_cmd(cmd, GITS_CMD_MAPC);
  235. its_encode_collection(cmd, desc->its_mapc_cmd.col->col_id);
  236. its_encode_target(cmd, desc->its_mapc_cmd.col->target_address);
  237. its_encode_valid(cmd, desc->its_mapc_cmd.valid);
  238. its_fixup_cmd(cmd);
  239. return desc->its_mapc_cmd.col;
  240. }
  241. static struct its_collection *its_build_mapvi_cmd(struct its_cmd_block *cmd,
  242. struct its_cmd_desc *desc)
  243. {
  244. struct its_collection *col;
  245. col = dev_event_to_col(desc->its_mapvi_cmd.dev,
  246. desc->its_mapvi_cmd.event_id);
  247. its_encode_cmd(cmd, GITS_CMD_MAPVI);
  248. its_encode_devid(cmd, desc->its_mapvi_cmd.dev->device_id);
  249. its_encode_event_id(cmd, desc->its_mapvi_cmd.event_id);
  250. its_encode_phys_id(cmd, desc->its_mapvi_cmd.phys_id);
  251. its_encode_collection(cmd, col->col_id);
  252. its_fixup_cmd(cmd);
  253. return col;
  254. }
  255. static struct its_collection *its_build_movi_cmd(struct its_cmd_block *cmd,
  256. struct its_cmd_desc *desc)
  257. {
  258. struct its_collection *col;
  259. col = dev_event_to_col(desc->its_movi_cmd.dev,
  260. desc->its_movi_cmd.event_id);
  261. its_encode_cmd(cmd, GITS_CMD_MOVI);
  262. its_encode_devid(cmd, desc->its_movi_cmd.dev->device_id);
  263. its_encode_event_id(cmd, desc->its_movi_cmd.event_id);
  264. its_encode_collection(cmd, desc->its_movi_cmd.col->col_id);
  265. its_fixup_cmd(cmd);
  266. return col;
  267. }
  268. static struct its_collection *its_build_discard_cmd(struct its_cmd_block *cmd,
  269. struct its_cmd_desc *desc)
  270. {
  271. struct its_collection *col;
  272. col = dev_event_to_col(desc->its_discard_cmd.dev,
  273. desc->its_discard_cmd.event_id);
  274. its_encode_cmd(cmd, GITS_CMD_DISCARD);
  275. its_encode_devid(cmd, desc->its_discard_cmd.dev->device_id);
  276. its_encode_event_id(cmd, desc->its_discard_cmd.event_id);
  277. its_fixup_cmd(cmd);
  278. return col;
  279. }
  280. static struct its_collection *its_build_inv_cmd(struct its_cmd_block *cmd,
  281. struct its_cmd_desc *desc)
  282. {
  283. struct its_collection *col;
  284. col = dev_event_to_col(desc->its_inv_cmd.dev,
  285. desc->its_inv_cmd.event_id);
  286. its_encode_cmd(cmd, GITS_CMD_INV);
  287. its_encode_devid(cmd, desc->its_inv_cmd.dev->device_id);
  288. its_encode_event_id(cmd, desc->its_inv_cmd.event_id);
  289. its_fixup_cmd(cmd);
  290. return col;
  291. }
  292. static struct its_collection *its_build_invall_cmd(struct its_cmd_block *cmd,
  293. struct its_cmd_desc *desc)
  294. {
  295. its_encode_cmd(cmd, GITS_CMD_INVALL);
  296. its_encode_collection(cmd, desc->its_mapc_cmd.col->col_id);
  297. its_fixup_cmd(cmd);
  298. return NULL;
  299. }
  300. static u64 its_cmd_ptr_to_offset(struct its_node *its,
  301. struct its_cmd_block *ptr)
  302. {
  303. return (ptr - its->cmd_base) * sizeof(*ptr);
  304. }
  305. static int its_queue_full(struct its_node *its)
  306. {
  307. int widx;
  308. int ridx;
  309. widx = its->cmd_write - its->cmd_base;
  310. ridx = readl_relaxed(its->base + GITS_CREADR) / sizeof(struct its_cmd_block);
  311. /* This is incredibly unlikely to happen, unless the ITS locks up. */
  312. if (((widx + 1) % ITS_CMD_QUEUE_NR_ENTRIES) == ridx)
  313. return 1;
  314. return 0;
  315. }
  316. static struct its_cmd_block *its_allocate_entry(struct its_node *its)
  317. {
  318. struct its_cmd_block *cmd;
  319. u32 count = 1000000; /* 1s! */
  320. while (its_queue_full(its)) {
  321. count--;
  322. if (!count) {
  323. pr_err_ratelimited("ITS queue not draining\n");
  324. return NULL;
  325. }
  326. cpu_relax();
  327. udelay(1);
  328. }
  329. cmd = its->cmd_write++;
  330. /* Handle queue wrapping */
  331. if (its->cmd_write == (its->cmd_base + ITS_CMD_QUEUE_NR_ENTRIES))
  332. its->cmd_write = its->cmd_base;
  333. return cmd;
  334. }
  335. static struct its_cmd_block *its_post_commands(struct its_node *its)
  336. {
  337. u64 wr = its_cmd_ptr_to_offset(its, its->cmd_write);
  338. writel_relaxed(wr, its->base + GITS_CWRITER);
  339. return its->cmd_write;
  340. }
  341. static void its_flush_cmd(struct its_node *its, struct its_cmd_block *cmd)
  342. {
  343. /*
  344. * Make sure the commands written to memory are observable by
  345. * the ITS.
  346. */
  347. if (its->flags & ITS_FLAGS_CMDQ_NEEDS_FLUSHING)
  348. __flush_dcache_area(cmd, sizeof(*cmd));
  349. else
  350. dsb(ishst);
  351. }
  352. static void its_wait_for_range_completion(struct its_node *its,
  353. struct its_cmd_block *from,
  354. struct its_cmd_block *to)
  355. {
  356. u64 rd_idx, from_idx, to_idx;
  357. u32 count = 1000000; /* 1s! */
  358. from_idx = its_cmd_ptr_to_offset(its, from);
  359. to_idx = its_cmd_ptr_to_offset(its, to);
  360. while (1) {
  361. rd_idx = readl_relaxed(its->base + GITS_CREADR);
  362. if (rd_idx >= to_idx || rd_idx < from_idx)
  363. break;
  364. count--;
  365. if (!count) {
  366. pr_err_ratelimited("ITS queue timeout\n");
  367. return;
  368. }
  369. cpu_relax();
  370. udelay(1);
  371. }
  372. }
  373. static void its_send_single_command(struct its_node *its,
  374. its_cmd_builder_t builder,
  375. struct its_cmd_desc *desc)
  376. {
  377. struct its_cmd_block *cmd, *sync_cmd, *next_cmd;
  378. struct its_collection *sync_col;
  379. unsigned long flags;
  380. raw_spin_lock_irqsave(&its->lock, flags);
  381. cmd = its_allocate_entry(its);
  382. if (!cmd) { /* We're soooooo screewed... */
  383. pr_err_ratelimited("ITS can't allocate, dropping command\n");
  384. raw_spin_unlock_irqrestore(&its->lock, flags);
  385. return;
  386. }
  387. sync_col = builder(cmd, desc);
  388. its_flush_cmd(its, cmd);
  389. if (sync_col) {
  390. sync_cmd = its_allocate_entry(its);
  391. if (!sync_cmd) {
  392. pr_err_ratelimited("ITS can't SYNC, skipping\n");
  393. goto post;
  394. }
  395. its_encode_cmd(sync_cmd, GITS_CMD_SYNC);
  396. its_encode_target(sync_cmd, sync_col->target_address);
  397. its_fixup_cmd(sync_cmd);
  398. its_flush_cmd(its, sync_cmd);
  399. }
  400. post:
  401. next_cmd = its_post_commands(its);
  402. raw_spin_unlock_irqrestore(&its->lock, flags);
  403. its_wait_for_range_completion(its, cmd, next_cmd);
  404. }
  405. static void its_send_inv(struct its_device *dev, u32 event_id)
  406. {
  407. struct its_cmd_desc desc;
  408. desc.its_inv_cmd.dev = dev;
  409. desc.its_inv_cmd.event_id = event_id;
  410. its_send_single_command(dev->its, its_build_inv_cmd, &desc);
  411. }
  412. static void its_send_mapd(struct its_device *dev, int valid)
  413. {
  414. struct its_cmd_desc desc;
  415. desc.its_mapd_cmd.dev = dev;
  416. desc.its_mapd_cmd.valid = !!valid;
  417. its_send_single_command(dev->its, its_build_mapd_cmd, &desc);
  418. }
  419. static void its_send_mapc(struct its_node *its, struct its_collection *col,
  420. int valid)
  421. {
  422. struct its_cmd_desc desc;
  423. desc.its_mapc_cmd.col = col;
  424. desc.its_mapc_cmd.valid = !!valid;
  425. its_send_single_command(its, its_build_mapc_cmd, &desc);
  426. }
  427. static void its_send_mapvi(struct its_device *dev, u32 irq_id, u32 id)
  428. {
  429. struct its_cmd_desc desc;
  430. desc.its_mapvi_cmd.dev = dev;
  431. desc.its_mapvi_cmd.phys_id = irq_id;
  432. desc.its_mapvi_cmd.event_id = id;
  433. its_send_single_command(dev->its, its_build_mapvi_cmd, &desc);
  434. }
  435. static void its_send_movi(struct its_device *dev,
  436. struct its_collection *col, u32 id)
  437. {
  438. struct its_cmd_desc desc;
  439. desc.its_movi_cmd.dev = dev;
  440. desc.its_movi_cmd.col = col;
  441. desc.its_movi_cmd.event_id = id;
  442. its_send_single_command(dev->its, its_build_movi_cmd, &desc);
  443. }
  444. static void its_send_discard(struct its_device *dev, u32 id)
  445. {
  446. struct its_cmd_desc desc;
  447. desc.its_discard_cmd.dev = dev;
  448. desc.its_discard_cmd.event_id = id;
  449. its_send_single_command(dev->its, its_build_discard_cmd, &desc);
  450. }
  451. static void its_send_invall(struct its_node *its, struct its_collection *col)
  452. {
  453. struct its_cmd_desc desc;
  454. desc.its_invall_cmd.col = col;
  455. its_send_single_command(its, its_build_invall_cmd, &desc);
  456. }
  457. /*
  458. * irqchip functions - assumes MSI, mostly.
  459. */
  460. static inline u32 its_get_event_id(struct irq_data *d)
  461. {
  462. struct its_device *its_dev = irq_data_get_irq_chip_data(d);
  463. return d->hwirq - its_dev->event_map.lpi_base;
  464. }
  465. static void lpi_set_config(struct irq_data *d, bool enable)
  466. {
  467. struct its_device *its_dev = irq_data_get_irq_chip_data(d);
  468. irq_hw_number_t hwirq = d->hwirq;
  469. u32 id = its_get_event_id(d);
  470. u8 *cfg = page_address(gic_rdists->prop_page) + hwirq - 8192;
  471. if (enable)
  472. *cfg |= LPI_PROP_ENABLED;
  473. else
  474. *cfg &= ~LPI_PROP_ENABLED;
  475. /*
  476. * Make the above write visible to the redistributors.
  477. * And yes, we're flushing exactly: One. Single. Byte.
  478. * Humpf...
  479. */
  480. if (gic_rdists->flags & RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING)
  481. __flush_dcache_area(cfg, sizeof(*cfg));
  482. else
  483. dsb(ishst);
  484. its_send_inv(its_dev, id);
  485. }
  486. static void its_mask_irq(struct irq_data *d)
  487. {
  488. lpi_set_config(d, false);
  489. }
  490. static void its_unmask_irq(struct irq_data *d)
  491. {
  492. lpi_set_config(d, true);
  493. }
  494. static int its_set_affinity(struct irq_data *d, const struct cpumask *mask_val,
  495. bool force)
  496. {
  497. unsigned int cpu;
  498. const struct cpumask *cpu_mask = cpu_online_mask;
  499. struct its_device *its_dev = irq_data_get_irq_chip_data(d);
  500. struct its_collection *target_col;
  501. u32 id = its_get_event_id(d);
  502. /* lpi cannot be routed to a redistributor that is on a foreign node */
  503. if (its_dev->its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_23144) {
  504. if (its_dev->its->numa_node >= 0) {
  505. cpu_mask = cpumask_of_node(its_dev->its->numa_node);
  506. if (!cpumask_intersects(mask_val, cpu_mask))
  507. return -EINVAL;
  508. }
  509. }
  510. cpu = cpumask_any_and(mask_val, cpu_mask);
  511. if (cpu >= nr_cpu_ids)
  512. return -EINVAL;
  513. target_col = &its_dev->its->collections[cpu];
  514. its_send_movi(its_dev, target_col, id);
  515. its_dev->event_map.col_map[id] = cpu;
  516. return IRQ_SET_MASK_OK_DONE;
  517. }
  518. static void its_irq_compose_msi_msg(struct irq_data *d, struct msi_msg *msg)
  519. {
  520. struct its_device *its_dev = irq_data_get_irq_chip_data(d);
  521. struct its_node *its;
  522. u64 addr;
  523. its = its_dev->its;
  524. addr = its->phys_base + GITS_TRANSLATER;
  525. msg->address_lo = addr & ((1UL << 32) - 1);
  526. msg->address_hi = addr >> 32;
  527. msg->data = its_get_event_id(d);
  528. }
  529. static struct irq_chip its_irq_chip = {
  530. .name = "ITS",
  531. .irq_mask = its_mask_irq,
  532. .irq_unmask = its_unmask_irq,
  533. .irq_eoi = irq_chip_eoi_parent,
  534. .irq_set_affinity = its_set_affinity,
  535. .irq_compose_msi_msg = its_irq_compose_msi_msg,
  536. };
  537. /*
  538. * How we allocate LPIs:
  539. *
  540. * The GIC has id_bits bits for interrupt identifiers. From there, we
  541. * must subtract 8192 which are reserved for SGIs/PPIs/SPIs. Then, as
  542. * we allocate LPIs by chunks of 32, we can shift the whole thing by 5
  543. * bits to the right.
  544. *
  545. * This gives us (((1UL << id_bits) - 8192) >> 5) possible allocations.
  546. */
  547. #define IRQS_PER_CHUNK_SHIFT 5
  548. #define IRQS_PER_CHUNK (1 << IRQS_PER_CHUNK_SHIFT)
  549. static unsigned long *lpi_bitmap;
  550. static u32 lpi_chunks;
  551. static DEFINE_SPINLOCK(lpi_lock);
  552. static int its_lpi_to_chunk(int lpi)
  553. {
  554. return (lpi - 8192) >> IRQS_PER_CHUNK_SHIFT;
  555. }
  556. static int its_chunk_to_lpi(int chunk)
  557. {
  558. return (chunk << IRQS_PER_CHUNK_SHIFT) + 8192;
  559. }
  560. static int __init its_lpi_init(u32 id_bits)
  561. {
  562. lpi_chunks = its_lpi_to_chunk(1UL << id_bits);
  563. lpi_bitmap = kzalloc(BITS_TO_LONGS(lpi_chunks) * sizeof(long),
  564. GFP_KERNEL);
  565. if (!lpi_bitmap) {
  566. lpi_chunks = 0;
  567. return -ENOMEM;
  568. }
  569. pr_info("ITS: Allocated %d chunks for LPIs\n", (int)lpi_chunks);
  570. return 0;
  571. }
  572. static unsigned long *its_lpi_alloc_chunks(int nr_irqs, int *base, int *nr_ids)
  573. {
  574. unsigned long *bitmap = NULL;
  575. int chunk_id;
  576. int nr_chunks;
  577. int i;
  578. nr_chunks = DIV_ROUND_UP(nr_irqs, IRQS_PER_CHUNK);
  579. spin_lock(&lpi_lock);
  580. do {
  581. chunk_id = bitmap_find_next_zero_area(lpi_bitmap, lpi_chunks,
  582. 0, nr_chunks, 0);
  583. if (chunk_id < lpi_chunks)
  584. break;
  585. nr_chunks--;
  586. } while (nr_chunks > 0);
  587. if (!nr_chunks)
  588. goto out;
  589. bitmap = kzalloc(BITS_TO_LONGS(nr_chunks * IRQS_PER_CHUNK) * sizeof (long),
  590. GFP_ATOMIC);
  591. if (!bitmap)
  592. goto out;
  593. for (i = 0; i < nr_chunks; i++)
  594. set_bit(chunk_id + i, lpi_bitmap);
  595. *base = its_chunk_to_lpi(chunk_id);
  596. *nr_ids = nr_chunks * IRQS_PER_CHUNK;
  597. out:
  598. spin_unlock(&lpi_lock);
  599. if (!bitmap)
  600. *base = *nr_ids = 0;
  601. return bitmap;
  602. }
  603. static void its_lpi_free(struct event_lpi_map *map)
  604. {
  605. int base = map->lpi_base;
  606. int nr_ids = map->nr_lpis;
  607. int lpi;
  608. spin_lock(&lpi_lock);
  609. for (lpi = base; lpi < (base + nr_ids); lpi += IRQS_PER_CHUNK) {
  610. int chunk = its_lpi_to_chunk(lpi);
  611. BUG_ON(chunk > lpi_chunks);
  612. if (test_bit(chunk, lpi_bitmap)) {
  613. clear_bit(chunk, lpi_bitmap);
  614. } else {
  615. pr_err("Bad LPI chunk %d\n", chunk);
  616. }
  617. }
  618. spin_unlock(&lpi_lock);
  619. kfree(map->lpi_map);
  620. kfree(map->col_map);
  621. }
  622. /*
  623. * We allocate 64kB for PROPBASE. That gives us at most 64K LPIs to
  624. * deal with (one configuration byte per interrupt). PENDBASE has to
  625. * be 64kB aligned (one bit per LPI, plus 8192 bits for SPI/PPI/SGI).
  626. */
  627. #define LPI_PROPBASE_SZ SZ_64K
  628. #define LPI_PENDBASE_SZ (LPI_PROPBASE_SZ / 8 + SZ_1K)
  629. /*
  630. * This is how many bits of ID we need, including the useless ones.
  631. */
  632. #define LPI_NRBITS ilog2(LPI_PROPBASE_SZ + SZ_8K)
  633. #define LPI_PROP_DEFAULT_PRIO 0xa0
  634. static int __init its_alloc_lpi_tables(void)
  635. {
  636. phys_addr_t paddr;
  637. gic_rdists->prop_page = alloc_pages(GFP_NOWAIT,
  638. get_order(LPI_PROPBASE_SZ));
  639. if (!gic_rdists->prop_page) {
  640. pr_err("Failed to allocate PROPBASE\n");
  641. return -ENOMEM;
  642. }
  643. paddr = page_to_phys(gic_rdists->prop_page);
  644. pr_info("GIC: using LPI property table @%pa\n", &paddr);
  645. /* Priority 0xa0, Group-1, disabled */
  646. memset(page_address(gic_rdists->prop_page),
  647. LPI_PROP_DEFAULT_PRIO | LPI_PROP_GROUP1,
  648. LPI_PROPBASE_SZ);
  649. /* Make sure the GIC will observe the written configuration */
  650. __flush_dcache_area(page_address(gic_rdists->prop_page), LPI_PROPBASE_SZ);
  651. return 0;
  652. }
  653. static const char *its_base_type_string[] = {
  654. [GITS_BASER_TYPE_DEVICE] = "Devices",
  655. [GITS_BASER_TYPE_VCPU] = "Virtual CPUs",
  656. [GITS_BASER_TYPE_CPU] = "Physical CPUs",
  657. [GITS_BASER_TYPE_COLLECTION] = "Interrupt Collections",
  658. [GITS_BASER_TYPE_RESERVED5] = "Reserved (5)",
  659. [GITS_BASER_TYPE_RESERVED6] = "Reserved (6)",
  660. [GITS_BASER_TYPE_RESERVED7] = "Reserved (7)",
  661. };
  662. static u64 its_read_baser(struct its_node *its, struct its_baser *baser)
  663. {
  664. u32 idx = baser - its->tables;
  665. return readq_relaxed(its->base + GITS_BASER + (idx << 3));
  666. }
  667. static void its_write_baser(struct its_node *its, struct its_baser *baser,
  668. u64 val)
  669. {
  670. u32 idx = baser - its->tables;
  671. writeq_relaxed(val, its->base + GITS_BASER + (idx << 3));
  672. baser->val = its_read_baser(its, baser);
  673. }
  674. static int its_setup_baser(struct its_node *its, struct its_baser *baser,
  675. u64 cache, u64 shr, u32 psz, u32 order,
  676. bool indirect)
  677. {
  678. u64 val = its_read_baser(its, baser);
  679. u64 esz = GITS_BASER_ENTRY_SIZE(val);
  680. u64 type = GITS_BASER_TYPE(val);
  681. u32 alloc_pages;
  682. void *base;
  683. u64 tmp;
  684. retry_alloc_baser:
  685. alloc_pages = (PAGE_ORDER_TO_SIZE(order) / psz);
  686. if (alloc_pages > GITS_BASER_PAGES_MAX) {
  687. pr_warn("ITS@%pa: %s too large, reduce ITS pages %u->%u\n",
  688. &its->phys_base, its_base_type_string[type],
  689. alloc_pages, GITS_BASER_PAGES_MAX);
  690. alloc_pages = GITS_BASER_PAGES_MAX;
  691. order = get_order(GITS_BASER_PAGES_MAX * psz);
  692. }
  693. base = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
  694. if (!base)
  695. return -ENOMEM;
  696. retry_baser:
  697. val = (virt_to_phys(base) |
  698. (type << GITS_BASER_TYPE_SHIFT) |
  699. ((esz - 1) << GITS_BASER_ENTRY_SIZE_SHIFT) |
  700. ((alloc_pages - 1) << GITS_BASER_PAGES_SHIFT) |
  701. cache |
  702. shr |
  703. GITS_BASER_VALID);
  704. val |= indirect ? GITS_BASER_INDIRECT : 0x0;
  705. switch (psz) {
  706. case SZ_4K:
  707. val |= GITS_BASER_PAGE_SIZE_4K;
  708. break;
  709. case SZ_16K:
  710. val |= GITS_BASER_PAGE_SIZE_16K;
  711. break;
  712. case SZ_64K:
  713. val |= GITS_BASER_PAGE_SIZE_64K;
  714. break;
  715. }
  716. its_write_baser(its, baser, val);
  717. tmp = baser->val;
  718. if ((val ^ tmp) & GITS_BASER_SHAREABILITY_MASK) {
  719. /*
  720. * Shareability didn't stick. Just use
  721. * whatever the read reported, which is likely
  722. * to be the only thing this redistributor
  723. * supports. If that's zero, make it
  724. * non-cacheable as well.
  725. */
  726. shr = tmp & GITS_BASER_SHAREABILITY_MASK;
  727. if (!shr) {
  728. cache = GITS_BASER_nC;
  729. __flush_dcache_area(base, PAGE_ORDER_TO_SIZE(order));
  730. }
  731. goto retry_baser;
  732. }
  733. if ((val ^ tmp) & GITS_BASER_PAGE_SIZE_MASK) {
  734. /*
  735. * Page size didn't stick. Let's try a smaller
  736. * size and retry. If we reach 4K, then
  737. * something is horribly wrong...
  738. */
  739. free_pages((unsigned long)base, order);
  740. baser->base = NULL;
  741. switch (psz) {
  742. case SZ_16K:
  743. psz = SZ_4K;
  744. goto retry_alloc_baser;
  745. case SZ_64K:
  746. psz = SZ_16K;
  747. goto retry_alloc_baser;
  748. }
  749. }
  750. if (val != tmp) {
  751. pr_err("ITS@%pa: %s doesn't stick: %lx %lx\n",
  752. &its->phys_base, its_base_type_string[type],
  753. (unsigned long) val, (unsigned long) tmp);
  754. free_pages((unsigned long)base, order);
  755. return -ENXIO;
  756. }
  757. baser->order = order;
  758. baser->base = base;
  759. baser->psz = psz;
  760. tmp = indirect ? GITS_LVL1_ENTRY_SIZE : esz;
  761. pr_info("ITS@%pa: allocated %d %s @%lx (%s, esz %d, psz %dK, shr %d)\n",
  762. &its->phys_base, (int)(PAGE_ORDER_TO_SIZE(order) / tmp),
  763. its_base_type_string[type],
  764. (unsigned long)virt_to_phys(base),
  765. indirect ? "indirect" : "flat", (int)esz,
  766. psz / SZ_1K, (int)shr >> GITS_BASER_SHAREABILITY_SHIFT);
  767. return 0;
  768. }
  769. static bool its_parse_baser_device(struct its_node *its, struct its_baser *baser,
  770. u32 psz, u32 *order)
  771. {
  772. u64 esz = GITS_BASER_ENTRY_SIZE(its_read_baser(its, baser));
  773. u64 val = GITS_BASER_InnerShareable | GITS_BASER_WaWb;
  774. u32 ids = its->device_ids;
  775. u32 new_order = *order;
  776. bool indirect = false;
  777. /* No need to enable Indirection if memory requirement < (psz*2)bytes */
  778. if ((esz << ids) > (psz * 2)) {
  779. /*
  780. * Find out whether hw supports a single or two-level table by
  781. * table by reading bit at offset '62' after writing '1' to it.
  782. */
  783. its_write_baser(its, baser, val | GITS_BASER_INDIRECT);
  784. indirect = !!(baser->val & GITS_BASER_INDIRECT);
  785. if (indirect) {
  786. /*
  787. * The size of the lvl2 table is equal to ITS page size
  788. * which is 'psz'. For computing lvl1 table size,
  789. * subtract ID bits that sparse lvl2 table from 'ids'
  790. * which is reported by ITS hardware times lvl1 table
  791. * entry size.
  792. */
  793. ids -= ilog2(psz / esz);
  794. esz = GITS_LVL1_ENTRY_SIZE;
  795. }
  796. }
  797. /*
  798. * Allocate as many entries as required to fit the
  799. * range of device IDs that the ITS can grok... The ID
  800. * space being incredibly sparse, this results in a
  801. * massive waste of memory if two-level device table
  802. * feature is not supported by hardware.
  803. */
  804. new_order = max_t(u32, get_order(esz << ids), new_order);
  805. if (new_order >= MAX_ORDER) {
  806. new_order = MAX_ORDER - 1;
  807. ids = ilog2(PAGE_ORDER_TO_SIZE(new_order) / esz);
  808. pr_warn("ITS@%pa: Device Table too large, reduce ids %u->%u\n",
  809. &its->phys_base, its->device_ids, ids);
  810. }
  811. *order = new_order;
  812. return indirect;
  813. }
  814. static void its_free_tables(struct its_node *its)
  815. {
  816. int i;
  817. for (i = 0; i < GITS_BASER_NR_REGS; i++) {
  818. if (its->tables[i].base) {
  819. free_pages((unsigned long)its->tables[i].base,
  820. its->tables[i].order);
  821. its->tables[i].base = NULL;
  822. }
  823. }
  824. }
  825. static int its_alloc_tables(struct its_node *its)
  826. {
  827. u64 typer = readq_relaxed(its->base + GITS_TYPER);
  828. u32 ids = GITS_TYPER_DEVBITS(typer);
  829. u64 shr = GITS_BASER_InnerShareable;
  830. u64 cache = GITS_BASER_WaWb;
  831. u32 psz = SZ_64K;
  832. int err, i;
  833. if (its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_22375) {
  834. /*
  835. * erratum 22375: only alloc 8MB table size
  836. * erratum 24313: ignore memory access type
  837. */
  838. cache = GITS_BASER_nCnB;
  839. ids = 0x14; /* 20 bits, 8MB */
  840. }
  841. its->device_ids = ids;
  842. for (i = 0; i < GITS_BASER_NR_REGS; i++) {
  843. struct its_baser *baser = its->tables + i;
  844. u64 val = its_read_baser(its, baser);
  845. u64 type = GITS_BASER_TYPE(val);
  846. u32 order = get_order(psz);
  847. bool indirect = false;
  848. if (type == GITS_BASER_TYPE_NONE)
  849. continue;
  850. if (type == GITS_BASER_TYPE_DEVICE)
  851. indirect = its_parse_baser_device(its, baser, psz, &order);
  852. err = its_setup_baser(its, baser, cache, shr, psz, order, indirect);
  853. if (err < 0) {
  854. its_free_tables(its);
  855. return err;
  856. }
  857. /* Update settings which will be used for next BASERn */
  858. psz = baser->psz;
  859. cache = baser->val & GITS_BASER_CACHEABILITY_MASK;
  860. shr = baser->val & GITS_BASER_SHAREABILITY_MASK;
  861. }
  862. return 0;
  863. }
  864. static int its_alloc_collections(struct its_node *its)
  865. {
  866. its->collections = kzalloc(nr_cpu_ids * sizeof(*its->collections),
  867. GFP_KERNEL);
  868. if (!its->collections)
  869. return -ENOMEM;
  870. return 0;
  871. }
  872. static void its_cpu_init_lpis(void)
  873. {
  874. void __iomem *rbase = gic_data_rdist_rd_base();
  875. struct page *pend_page;
  876. u64 val, tmp;
  877. /* If we didn't allocate the pending table yet, do it now */
  878. pend_page = gic_data_rdist()->pend_page;
  879. if (!pend_page) {
  880. phys_addr_t paddr;
  881. /*
  882. * The pending pages have to be at least 64kB aligned,
  883. * hence the 'max(LPI_PENDBASE_SZ, SZ_64K)' below.
  884. */
  885. pend_page = alloc_pages(GFP_NOWAIT | __GFP_ZERO,
  886. get_order(max(LPI_PENDBASE_SZ, SZ_64K)));
  887. if (!pend_page) {
  888. pr_err("Failed to allocate PENDBASE for CPU%d\n",
  889. smp_processor_id());
  890. return;
  891. }
  892. /* Make sure the GIC will observe the zero-ed page */
  893. __flush_dcache_area(page_address(pend_page), LPI_PENDBASE_SZ);
  894. paddr = page_to_phys(pend_page);
  895. pr_info("CPU%d: using LPI pending table @%pa\n",
  896. smp_processor_id(), &paddr);
  897. gic_data_rdist()->pend_page = pend_page;
  898. }
  899. /* Disable LPIs */
  900. val = readl_relaxed(rbase + GICR_CTLR);
  901. val &= ~GICR_CTLR_ENABLE_LPIS;
  902. writel_relaxed(val, rbase + GICR_CTLR);
  903. /*
  904. * Make sure any change to the table is observable by the GIC.
  905. */
  906. dsb(sy);
  907. /* set PROPBASE */
  908. val = (page_to_phys(gic_rdists->prop_page) |
  909. GICR_PROPBASER_InnerShareable |
  910. GICR_PROPBASER_WaWb |
  911. ((LPI_NRBITS - 1) & GICR_PROPBASER_IDBITS_MASK));
  912. writeq_relaxed(val, rbase + GICR_PROPBASER);
  913. tmp = readq_relaxed(rbase + GICR_PROPBASER);
  914. if ((tmp ^ val) & GICR_PROPBASER_SHAREABILITY_MASK) {
  915. if (!(tmp & GICR_PROPBASER_SHAREABILITY_MASK)) {
  916. /*
  917. * The HW reports non-shareable, we must
  918. * remove the cacheability attributes as
  919. * well.
  920. */
  921. val &= ~(GICR_PROPBASER_SHAREABILITY_MASK |
  922. GICR_PROPBASER_CACHEABILITY_MASK);
  923. val |= GICR_PROPBASER_nC;
  924. writeq_relaxed(val, rbase + GICR_PROPBASER);
  925. }
  926. pr_info_once("GIC: using cache flushing for LPI property table\n");
  927. gic_rdists->flags |= RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING;
  928. }
  929. /* set PENDBASE */
  930. val = (page_to_phys(pend_page) |
  931. GICR_PENDBASER_InnerShareable |
  932. GICR_PENDBASER_WaWb);
  933. writeq_relaxed(val, rbase + GICR_PENDBASER);
  934. tmp = readq_relaxed(rbase + GICR_PENDBASER);
  935. if (!(tmp & GICR_PENDBASER_SHAREABILITY_MASK)) {
  936. /*
  937. * The HW reports non-shareable, we must remove the
  938. * cacheability attributes as well.
  939. */
  940. val &= ~(GICR_PENDBASER_SHAREABILITY_MASK |
  941. GICR_PENDBASER_CACHEABILITY_MASK);
  942. val |= GICR_PENDBASER_nC;
  943. writeq_relaxed(val, rbase + GICR_PENDBASER);
  944. }
  945. /* Enable LPIs */
  946. val = readl_relaxed(rbase + GICR_CTLR);
  947. val |= GICR_CTLR_ENABLE_LPIS;
  948. writel_relaxed(val, rbase + GICR_CTLR);
  949. /* Make sure the GIC has seen the above */
  950. dsb(sy);
  951. }
  952. static void its_cpu_init_collection(void)
  953. {
  954. struct its_node *its;
  955. int cpu;
  956. spin_lock(&its_lock);
  957. cpu = smp_processor_id();
  958. list_for_each_entry(its, &its_nodes, entry) {
  959. u64 target;
  960. /* avoid cross node collections and its mapping */
  961. if (its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_23144) {
  962. struct device_node *cpu_node;
  963. cpu_node = of_get_cpu_node(cpu, NULL);
  964. if (its->numa_node != NUMA_NO_NODE &&
  965. its->numa_node != of_node_to_nid(cpu_node))
  966. continue;
  967. }
  968. /*
  969. * We now have to bind each collection to its target
  970. * redistributor.
  971. */
  972. if (readq_relaxed(its->base + GITS_TYPER) & GITS_TYPER_PTA) {
  973. /*
  974. * This ITS wants the physical address of the
  975. * redistributor.
  976. */
  977. target = gic_data_rdist()->phys_base;
  978. } else {
  979. /*
  980. * This ITS wants a linear CPU number.
  981. */
  982. target = readq_relaxed(gic_data_rdist_rd_base() + GICR_TYPER);
  983. target = GICR_TYPER_CPU_NUMBER(target) << 16;
  984. }
  985. /* Perform collection mapping */
  986. its->collections[cpu].target_address = target;
  987. its->collections[cpu].col_id = cpu;
  988. its_send_mapc(its, &its->collections[cpu], 1);
  989. its_send_invall(its, &its->collections[cpu]);
  990. }
  991. spin_unlock(&its_lock);
  992. }
  993. static struct its_device *its_find_device(struct its_node *its, u32 dev_id)
  994. {
  995. struct its_device *its_dev = NULL, *tmp;
  996. unsigned long flags;
  997. raw_spin_lock_irqsave(&its->lock, flags);
  998. list_for_each_entry(tmp, &its->its_device_list, entry) {
  999. if (tmp->device_id == dev_id) {
  1000. its_dev = tmp;
  1001. break;
  1002. }
  1003. }
  1004. raw_spin_unlock_irqrestore(&its->lock, flags);
  1005. return its_dev;
  1006. }
  1007. static struct its_baser *its_get_baser(struct its_node *its, u32 type)
  1008. {
  1009. int i;
  1010. for (i = 0; i < GITS_BASER_NR_REGS; i++) {
  1011. if (GITS_BASER_TYPE(its->tables[i].val) == type)
  1012. return &its->tables[i];
  1013. }
  1014. return NULL;
  1015. }
  1016. static bool its_alloc_device_table(struct its_node *its, u32 dev_id)
  1017. {
  1018. struct its_baser *baser;
  1019. struct page *page;
  1020. u32 esz, idx;
  1021. __le64 *table;
  1022. baser = its_get_baser(its, GITS_BASER_TYPE_DEVICE);
  1023. /* Don't allow device id that exceeds ITS hardware limit */
  1024. if (!baser)
  1025. return (ilog2(dev_id) < its->device_ids);
  1026. /* Don't allow device id that exceeds single, flat table limit */
  1027. esz = GITS_BASER_ENTRY_SIZE(baser->val);
  1028. if (!(baser->val & GITS_BASER_INDIRECT))
  1029. return (dev_id < (PAGE_ORDER_TO_SIZE(baser->order) / esz));
  1030. /* Compute 1st level table index & check if that exceeds table limit */
  1031. idx = dev_id >> ilog2(baser->psz / esz);
  1032. if (idx >= (PAGE_ORDER_TO_SIZE(baser->order) / GITS_LVL1_ENTRY_SIZE))
  1033. return false;
  1034. table = baser->base;
  1035. /* Allocate memory for 2nd level table */
  1036. if (!table[idx]) {
  1037. page = alloc_pages(GFP_KERNEL | __GFP_ZERO, get_order(baser->psz));
  1038. if (!page)
  1039. return false;
  1040. /* Flush Lvl2 table to PoC if hw doesn't support coherency */
  1041. if (!(baser->val & GITS_BASER_SHAREABILITY_MASK))
  1042. __flush_dcache_area(page_address(page), baser->psz);
  1043. table[idx] = cpu_to_le64(page_to_phys(page) | GITS_BASER_VALID);
  1044. /* Flush Lvl1 entry to PoC if hw doesn't support coherency */
  1045. if (!(baser->val & GITS_BASER_SHAREABILITY_MASK))
  1046. __flush_dcache_area(table + idx, GITS_LVL1_ENTRY_SIZE);
  1047. /* Ensure updated table contents are visible to ITS hardware */
  1048. dsb(sy);
  1049. }
  1050. return true;
  1051. }
  1052. static struct its_device *its_create_device(struct its_node *its, u32 dev_id,
  1053. int nvecs)
  1054. {
  1055. struct its_device *dev;
  1056. unsigned long *lpi_map;
  1057. unsigned long flags;
  1058. u16 *col_map = NULL;
  1059. void *itt;
  1060. int lpi_base;
  1061. int nr_lpis;
  1062. int nr_ites;
  1063. int sz;
  1064. if (!its_alloc_device_table(its, dev_id))
  1065. return NULL;
  1066. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  1067. /*
  1068. * At least one bit of EventID is being used, hence a minimum
  1069. * of two entries. No, the architecture doesn't let you
  1070. * express an ITT with a single entry.
  1071. */
  1072. nr_ites = max(2UL, roundup_pow_of_two(nvecs));
  1073. sz = nr_ites * its->ite_size;
  1074. sz = max(sz, ITS_ITT_ALIGN) + ITS_ITT_ALIGN - 1;
  1075. itt = kzalloc(sz, GFP_KERNEL);
  1076. lpi_map = its_lpi_alloc_chunks(nvecs, &lpi_base, &nr_lpis);
  1077. if (lpi_map)
  1078. col_map = kzalloc(sizeof(*col_map) * nr_lpis, GFP_KERNEL);
  1079. if (!dev || !itt || !lpi_map || !col_map) {
  1080. kfree(dev);
  1081. kfree(itt);
  1082. kfree(lpi_map);
  1083. kfree(col_map);
  1084. return NULL;
  1085. }
  1086. __flush_dcache_area(itt, sz);
  1087. dev->its = its;
  1088. dev->itt = itt;
  1089. dev->nr_ites = nr_ites;
  1090. dev->event_map.lpi_map = lpi_map;
  1091. dev->event_map.col_map = col_map;
  1092. dev->event_map.lpi_base = lpi_base;
  1093. dev->event_map.nr_lpis = nr_lpis;
  1094. dev->device_id = dev_id;
  1095. INIT_LIST_HEAD(&dev->entry);
  1096. raw_spin_lock_irqsave(&its->lock, flags);
  1097. list_add(&dev->entry, &its->its_device_list);
  1098. raw_spin_unlock_irqrestore(&its->lock, flags);
  1099. /* Map device to its ITT */
  1100. its_send_mapd(dev, 1);
  1101. return dev;
  1102. }
  1103. static void its_free_device(struct its_device *its_dev)
  1104. {
  1105. unsigned long flags;
  1106. raw_spin_lock_irqsave(&its_dev->its->lock, flags);
  1107. list_del(&its_dev->entry);
  1108. raw_spin_unlock_irqrestore(&its_dev->its->lock, flags);
  1109. kfree(its_dev->itt);
  1110. kfree(its_dev);
  1111. }
  1112. static int its_alloc_device_irq(struct its_device *dev, irq_hw_number_t *hwirq)
  1113. {
  1114. int idx;
  1115. idx = find_first_zero_bit(dev->event_map.lpi_map,
  1116. dev->event_map.nr_lpis);
  1117. if (idx == dev->event_map.nr_lpis)
  1118. return -ENOSPC;
  1119. *hwirq = dev->event_map.lpi_base + idx;
  1120. set_bit(idx, dev->event_map.lpi_map);
  1121. return 0;
  1122. }
  1123. static int its_msi_prepare(struct irq_domain *domain, struct device *dev,
  1124. int nvec, msi_alloc_info_t *info)
  1125. {
  1126. struct its_node *its;
  1127. struct its_device *its_dev;
  1128. struct msi_domain_info *msi_info;
  1129. u32 dev_id;
  1130. /*
  1131. * We ignore "dev" entierely, and rely on the dev_id that has
  1132. * been passed via the scratchpad. This limits this domain's
  1133. * usefulness to upper layers that definitely know that they
  1134. * are built on top of the ITS.
  1135. */
  1136. dev_id = info->scratchpad[0].ul;
  1137. msi_info = msi_get_domain_info(domain);
  1138. its = msi_info->data;
  1139. its_dev = its_find_device(its, dev_id);
  1140. if (its_dev) {
  1141. /*
  1142. * We already have seen this ID, probably through
  1143. * another alias (PCI bridge of some sort). No need to
  1144. * create the device.
  1145. */
  1146. pr_debug("Reusing ITT for devID %x\n", dev_id);
  1147. goto out;
  1148. }
  1149. its_dev = its_create_device(its, dev_id, nvec);
  1150. if (!its_dev)
  1151. return -ENOMEM;
  1152. pr_debug("ITT %d entries, %d bits\n", nvec, ilog2(nvec));
  1153. out:
  1154. info->scratchpad[0].ptr = its_dev;
  1155. return 0;
  1156. }
  1157. static struct msi_domain_ops its_msi_domain_ops = {
  1158. .msi_prepare = its_msi_prepare,
  1159. };
  1160. static int its_irq_gic_domain_alloc(struct irq_domain *domain,
  1161. unsigned int virq,
  1162. irq_hw_number_t hwirq)
  1163. {
  1164. struct irq_fwspec fwspec;
  1165. if (irq_domain_get_of_node(domain->parent)) {
  1166. fwspec.fwnode = domain->parent->fwnode;
  1167. fwspec.param_count = 3;
  1168. fwspec.param[0] = GIC_IRQ_TYPE_LPI;
  1169. fwspec.param[1] = hwirq;
  1170. fwspec.param[2] = IRQ_TYPE_EDGE_RISING;
  1171. } else {
  1172. return -EINVAL;
  1173. }
  1174. return irq_domain_alloc_irqs_parent(domain, virq, 1, &fwspec);
  1175. }
  1176. static int its_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
  1177. unsigned int nr_irqs, void *args)
  1178. {
  1179. msi_alloc_info_t *info = args;
  1180. struct its_device *its_dev = info->scratchpad[0].ptr;
  1181. irq_hw_number_t hwirq;
  1182. int err;
  1183. int i;
  1184. for (i = 0; i < nr_irqs; i++) {
  1185. err = its_alloc_device_irq(its_dev, &hwirq);
  1186. if (err)
  1187. return err;
  1188. err = its_irq_gic_domain_alloc(domain, virq + i, hwirq);
  1189. if (err)
  1190. return err;
  1191. irq_domain_set_hwirq_and_chip(domain, virq + i,
  1192. hwirq, &its_irq_chip, its_dev);
  1193. pr_debug("ID:%d pID:%d vID:%d\n",
  1194. (int)(hwirq - its_dev->event_map.lpi_base),
  1195. (int) hwirq, virq + i);
  1196. }
  1197. return 0;
  1198. }
  1199. static void its_irq_domain_activate(struct irq_domain *domain,
  1200. struct irq_data *d)
  1201. {
  1202. struct its_device *its_dev = irq_data_get_irq_chip_data(d);
  1203. u32 event = its_get_event_id(d);
  1204. const struct cpumask *cpu_mask = cpu_online_mask;
  1205. /* get the cpu_mask of local node */
  1206. if (its_dev->its->numa_node >= 0)
  1207. cpu_mask = cpumask_of_node(its_dev->its->numa_node);
  1208. /* Bind the LPI to the first possible CPU */
  1209. its_dev->event_map.col_map[event] = cpumask_first(cpu_mask);
  1210. /* Map the GIC IRQ and event to the device */
  1211. its_send_mapvi(its_dev, d->hwirq, event);
  1212. }
  1213. static void its_irq_domain_deactivate(struct irq_domain *domain,
  1214. struct irq_data *d)
  1215. {
  1216. struct its_device *its_dev = irq_data_get_irq_chip_data(d);
  1217. u32 event = its_get_event_id(d);
  1218. /* Stop the delivery of interrupts */
  1219. its_send_discard(its_dev, event);
  1220. }
  1221. static void its_irq_domain_free(struct irq_domain *domain, unsigned int virq,
  1222. unsigned int nr_irqs)
  1223. {
  1224. struct irq_data *d = irq_domain_get_irq_data(domain, virq);
  1225. struct its_device *its_dev = irq_data_get_irq_chip_data(d);
  1226. int i;
  1227. for (i = 0; i < nr_irqs; i++) {
  1228. struct irq_data *data = irq_domain_get_irq_data(domain,
  1229. virq + i);
  1230. u32 event = its_get_event_id(data);
  1231. /* Mark interrupt index as unused */
  1232. clear_bit(event, its_dev->event_map.lpi_map);
  1233. /* Nuke the entry in the domain */
  1234. irq_domain_reset_irq_data(data);
  1235. }
  1236. /* If all interrupts have been freed, start mopping the floor */
  1237. if (bitmap_empty(its_dev->event_map.lpi_map,
  1238. its_dev->event_map.nr_lpis)) {
  1239. its_lpi_free(&its_dev->event_map);
  1240. /* Unmap device/itt */
  1241. its_send_mapd(its_dev, 0);
  1242. its_free_device(its_dev);
  1243. }
  1244. irq_domain_free_irqs_parent(domain, virq, nr_irqs);
  1245. }
  1246. static const struct irq_domain_ops its_domain_ops = {
  1247. .alloc = its_irq_domain_alloc,
  1248. .free = its_irq_domain_free,
  1249. .activate = its_irq_domain_activate,
  1250. .deactivate = its_irq_domain_deactivate,
  1251. };
  1252. static int its_force_quiescent(void __iomem *base)
  1253. {
  1254. u32 count = 1000000; /* 1s */
  1255. u32 val;
  1256. val = readl_relaxed(base + GITS_CTLR);
  1257. /*
  1258. * GIC architecture specification requires the ITS to be both
  1259. * disabled and quiescent for writes to GITS_BASER<n> or
  1260. * GITS_CBASER to not have UNPREDICTABLE results.
  1261. */
  1262. if ((val & GITS_CTLR_QUIESCENT) && !(val & GITS_CTLR_ENABLE))
  1263. return 0;
  1264. /* Disable the generation of all interrupts to this ITS */
  1265. val &= ~GITS_CTLR_ENABLE;
  1266. writel_relaxed(val, base + GITS_CTLR);
  1267. /* Poll GITS_CTLR and wait until ITS becomes quiescent */
  1268. while (1) {
  1269. val = readl_relaxed(base + GITS_CTLR);
  1270. if (val & GITS_CTLR_QUIESCENT)
  1271. return 0;
  1272. count--;
  1273. if (!count)
  1274. return -EBUSY;
  1275. cpu_relax();
  1276. udelay(1);
  1277. }
  1278. }
  1279. static void __maybe_unused its_enable_quirk_cavium_22375(void *data)
  1280. {
  1281. struct its_node *its = data;
  1282. its->flags |= ITS_FLAGS_WORKAROUND_CAVIUM_22375;
  1283. }
  1284. static void __maybe_unused its_enable_quirk_cavium_23144(void *data)
  1285. {
  1286. struct its_node *its = data;
  1287. its->flags |= ITS_FLAGS_WORKAROUND_CAVIUM_23144;
  1288. }
  1289. static const struct gic_quirk its_quirks[] = {
  1290. #ifdef CONFIG_CAVIUM_ERRATUM_22375
  1291. {
  1292. .desc = "ITS: Cavium errata 22375, 24313",
  1293. .iidr = 0xa100034c, /* ThunderX pass 1.x */
  1294. .mask = 0xffff0fff,
  1295. .init = its_enable_quirk_cavium_22375,
  1296. },
  1297. #endif
  1298. #ifdef CONFIG_CAVIUM_ERRATUM_23144
  1299. {
  1300. .desc = "ITS: Cavium erratum 23144",
  1301. .iidr = 0xa100034c, /* ThunderX pass 1.x */
  1302. .mask = 0xffff0fff,
  1303. .init = its_enable_quirk_cavium_23144,
  1304. },
  1305. #endif
  1306. {
  1307. }
  1308. };
  1309. static void its_enable_quirks(struct its_node *its)
  1310. {
  1311. u32 iidr = readl_relaxed(its->base + GITS_IIDR);
  1312. gic_enable_quirks(iidr, its_quirks, its);
  1313. }
  1314. static int __init its_probe(struct device_node *node,
  1315. struct irq_domain *parent)
  1316. {
  1317. struct resource res;
  1318. struct its_node *its;
  1319. void __iomem *its_base;
  1320. struct irq_domain *inner_domain;
  1321. u32 val;
  1322. u64 baser, tmp;
  1323. int err;
  1324. err = of_address_to_resource(node, 0, &res);
  1325. if (err) {
  1326. pr_warn("%s: no regs?\n", node->full_name);
  1327. return -ENXIO;
  1328. }
  1329. its_base = ioremap(res.start, resource_size(&res));
  1330. if (!its_base) {
  1331. pr_warn("%s: unable to map registers\n", node->full_name);
  1332. return -ENOMEM;
  1333. }
  1334. val = readl_relaxed(its_base + GITS_PIDR2) & GIC_PIDR2_ARCH_MASK;
  1335. if (val != 0x30 && val != 0x40) {
  1336. pr_warn("%s: no ITS detected, giving up\n", node->full_name);
  1337. err = -ENODEV;
  1338. goto out_unmap;
  1339. }
  1340. err = its_force_quiescent(its_base);
  1341. if (err) {
  1342. pr_warn("%s: failed to quiesce, giving up\n",
  1343. node->full_name);
  1344. goto out_unmap;
  1345. }
  1346. pr_info("ITS: %s\n", node->full_name);
  1347. its = kzalloc(sizeof(*its), GFP_KERNEL);
  1348. if (!its) {
  1349. err = -ENOMEM;
  1350. goto out_unmap;
  1351. }
  1352. raw_spin_lock_init(&its->lock);
  1353. INIT_LIST_HEAD(&its->entry);
  1354. INIT_LIST_HEAD(&its->its_device_list);
  1355. its->base = its_base;
  1356. its->phys_base = res.start;
  1357. its->ite_size = ((readl_relaxed(its_base + GITS_TYPER) >> 4) & 0xf) + 1;
  1358. its->numa_node = of_node_to_nid(node);
  1359. its->cmd_base = kzalloc(ITS_CMD_QUEUE_SZ, GFP_KERNEL);
  1360. if (!its->cmd_base) {
  1361. err = -ENOMEM;
  1362. goto out_free_its;
  1363. }
  1364. its->cmd_write = its->cmd_base;
  1365. its_enable_quirks(its);
  1366. err = its_alloc_tables(its);
  1367. if (err)
  1368. goto out_free_cmd;
  1369. err = its_alloc_collections(its);
  1370. if (err)
  1371. goto out_free_tables;
  1372. baser = (virt_to_phys(its->cmd_base) |
  1373. GITS_CBASER_WaWb |
  1374. GITS_CBASER_InnerShareable |
  1375. (ITS_CMD_QUEUE_SZ / SZ_4K - 1) |
  1376. GITS_CBASER_VALID);
  1377. writeq_relaxed(baser, its->base + GITS_CBASER);
  1378. tmp = readq_relaxed(its->base + GITS_CBASER);
  1379. if ((tmp ^ baser) & GITS_CBASER_SHAREABILITY_MASK) {
  1380. if (!(tmp & GITS_CBASER_SHAREABILITY_MASK)) {
  1381. /*
  1382. * The HW reports non-shareable, we must
  1383. * remove the cacheability attributes as
  1384. * well.
  1385. */
  1386. baser &= ~(GITS_CBASER_SHAREABILITY_MASK |
  1387. GITS_CBASER_CACHEABILITY_MASK);
  1388. baser |= GITS_CBASER_nC;
  1389. writeq_relaxed(baser, its->base + GITS_CBASER);
  1390. }
  1391. pr_info("ITS: using cache flushing for cmd queue\n");
  1392. its->flags |= ITS_FLAGS_CMDQ_NEEDS_FLUSHING;
  1393. }
  1394. writeq_relaxed(0, its->base + GITS_CWRITER);
  1395. writel_relaxed(GITS_CTLR_ENABLE, its->base + GITS_CTLR);
  1396. if (of_property_read_bool(node, "msi-controller")) {
  1397. struct msi_domain_info *info;
  1398. info = kzalloc(sizeof(*info), GFP_KERNEL);
  1399. if (!info) {
  1400. err = -ENOMEM;
  1401. goto out_free_tables;
  1402. }
  1403. inner_domain = irq_domain_add_tree(node, &its_domain_ops, its);
  1404. if (!inner_domain) {
  1405. err = -ENOMEM;
  1406. kfree(info);
  1407. goto out_free_tables;
  1408. }
  1409. inner_domain->parent = parent;
  1410. inner_domain->bus_token = DOMAIN_BUS_NEXUS;
  1411. info->ops = &its_msi_domain_ops;
  1412. info->data = its;
  1413. inner_domain->host_data = info;
  1414. }
  1415. spin_lock(&its_lock);
  1416. list_add(&its->entry, &its_nodes);
  1417. spin_unlock(&its_lock);
  1418. return 0;
  1419. out_free_tables:
  1420. its_free_tables(its);
  1421. out_free_cmd:
  1422. kfree(its->cmd_base);
  1423. out_free_its:
  1424. kfree(its);
  1425. out_unmap:
  1426. iounmap(its_base);
  1427. pr_err("ITS: failed probing %s (%d)\n", node->full_name, err);
  1428. return err;
  1429. }
  1430. static bool gic_rdists_supports_plpis(void)
  1431. {
  1432. return !!(readl_relaxed(gic_data_rdist_rd_base() + GICR_TYPER) & GICR_TYPER_PLPIS);
  1433. }
  1434. int its_cpu_init(void)
  1435. {
  1436. if (!list_empty(&its_nodes)) {
  1437. if (!gic_rdists_supports_plpis()) {
  1438. pr_info("CPU%d: LPIs not supported\n", smp_processor_id());
  1439. return -ENXIO;
  1440. }
  1441. its_cpu_init_lpis();
  1442. its_cpu_init_collection();
  1443. }
  1444. return 0;
  1445. }
  1446. static struct of_device_id its_device_id[] = {
  1447. { .compatible = "arm,gic-v3-its", },
  1448. {},
  1449. };
  1450. int __init its_init(struct device_node *node, struct rdists *rdists,
  1451. struct irq_domain *parent_domain)
  1452. {
  1453. struct device_node *np;
  1454. for (np = of_find_matching_node(node, its_device_id); np;
  1455. np = of_find_matching_node(np, its_device_id)) {
  1456. its_probe(np, parent_domain);
  1457. }
  1458. if (list_empty(&its_nodes)) {
  1459. pr_warn("ITS: No ITS available, not enabling LPIs\n");
  1460. return -ENXIO;
  1461. }
  1462. gic_rdists = rdists;
  1463. its_alloc_lpi_tables();
  1464. its_lpi_init(rdists->id_bits);
  1465. return 0;
  1466. }