irq-gic-v3-its.c 37 KB

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