irq-gic-v3-its.c 36 KB

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