irq-gic-v3-its.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425
  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. raw_spin_lock(&its->lock);
  345. cmd = its_allocate_entry(its);
  346. if (!cmd) { /* We're soooooo screewed... */
  347. pr_err_ratelimited("ITS can't allocate, dropping command\n");
  348. raw_spin_unlock(&its->lock);
  349. return;
  350. }
  351. sync_col = builder(cmd, desc);
  352. its_flush_cmd(its, cmd);
  353. if (sync_col) {
  354. sync_cmd = its_allocate_entry(its);
  355. if (!sync_cmd) {
  356. pr_err_ratelimited("ITS can't SYNC, skipping\n");
  357. goto post;
  358. }
  359. its_encode_cmd(sync_cmd, GITS_CMD_SYNC);
  360. its_encode_target(sync_cmd, sync_col->target_address);
  361. its_fixup_cmd(sync_cmd);
  362. its_flush_cmd(its, sync_cmd);
  363. }
  364. post:
  365. next_cmd = its_post_commands(its);
  366. raw_spin_unlock(&its->lock);
  367. its_wait_for_range_completion(its, cmd, next_cmd);
  368. }
  369. static void its_send_inv(struct its_device *dev, u32 event_id)
  370. {
  371. struct its_cmd_desc desc;
  372. desc.its_inv_cmd.dev = dev;
  373. desc.its_inv_cmd.event_id = event_id;
  374. its_send_single_command(dev->its, its_build_inv_cmd, &desc);
  375. }
  376. static void its_send_mapd(struct its_device *dev, int valid)
  377. {
  378. struct its_cmd_desc desc;
  379. desc.its_mapd_cmd.dev = dev;
  380. desc.its_mapd_cmd.valid = !!valid;
  381. its_send_single_command(dev->its, its_build_mapd_cmd, &desc);
  382. }
  383. static void its_send_mapc(struct its_node *its, struct its_collection *col,
  384. int valid)
  385. {
  386. struct its_cmd_desc desc;
  387. desc.its_mapc_cmd.col = col;
  388. desc.its_mapc_cmd.valid = !!valid;
  389. its_send_single_command(its, its_build_mapc_cmd, &desc);
  390. }
  391. static void its_send_mapvi(struct its_device *dev, u32 irq_id, u32 id)
  392. {
  393. struct its_cmd_desc desc;
  394. desc.its_mapvi_cmd.dev = dev;
  395. desc.its_mapvi_cmd.phys_id = irq_id;
  396. desc.its_mapvi_cmd.event_id = id;
  397. its_send_single_command(dev->its, its_build_mapvi_cmd, &desc);
  398. }
  399. static void its_send_movi(struct its_device *dev,
  400. struct its_collection *col, u32 id)
  401. {
  402. struct its_cmd_desc desc;
  403. desc.its_movi_cmd.dev = dev;
  404. desc.its_movi_cmd.col = col;
  405. desc.its_movi_cmd.id = id;
  406. its_send_single_command(dev->its, its_build_movi_cmd, &desc);
  407. }
  408. static void its_send_discard(struct its_device *dev, u32 id)
  409. {
  410. struct its_cmd_desc desc;
  411. desc.its_discard_cmd.dev = dev;
  412. desc.its_discard_cmd.event_id = id;
  413. its_send_single_command(dev->its, its_build_discard_cmd, &desc);
  414. }
  415. static void its_send_invall(struct its_node *its, struct its_collection *col)
  416. {
  417. struct its_cmd_desc desc;
  418. desc.its_invall_cmd.col = col;
  419. its_send_single_command(its, its_build_invall_cmd, &desc);
  420. }
  421. /*
  422. * irqchip functions - assumes MSI, mostly.
  423. */
  424. static inline u32 its_get_event_id(struct irq_data *d)
  425. {
  426. struct its_device *its_dev = irq_data_get_irq_chip_data(d);
  427. return d->hwirq - its_dev->lpi_base;
  428. }
  429. static void lpi_set_config(struct irq_data *d, bool enable)
  430. {
  431. struct its_device *its_dev = irq_data_get_irq_chip_data(d);
  432. irq_hw_number_t hwirq = d->hwirq;
  433. u32 id = its_get_event_id(d);
  434. u8 *cfg = page_address(gic_rdists->prop_page) + hwirq - 8192;
  435. if (enable)
  436. *cfg |= LPI_PROP_ENABLED;
  437. else
  438. *cfg &= ~LPI_PROP_ENABLED;
  439. /*
  440. * Make the above write visible to the redistributors.
  441. * And yes, we're flushing exactly: One. Single. Byte.
  442. * Humpf...
  443. */
  444. if (gic_rdists->flags & RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING)
  445. __flush_dcache_area(cfg, sizeof(*cfg));
  446. else
  447. dsb(ishst);
  448. its_send_inv(its_dev, id);
  449. }
  450. static void its_mask_irq(struct irq_data *d)
  451. {
  452. lpi_set_config(d, false);
  453. }
  454. static void its_unmask_irq(struct irq_data *d)
  455. {
  456. lpi_set_config(d, true);
  457. }
  458. static void its_eoi_irq(struct irq_data *d)
  459. {
  460. gic_write_eoir(d->hwirq);
  461. }
  462. static int its_set_affinity(struct irq_data *d, const struct cpumask *mask_val,
  463. bool force)
  464. {
  465. unsigned int cpu = cpumask_any_and(mask_val, cpu_online_mask);
  466. struct its_device *its_dev = irq_data_get_irq_chip_data(d);
  467. struct its_collection *target_col;
  468. u32 id = its_get_event_id(d);
  469. if (cpu >= nr_cpu_ids)
  470. return -EINVAL;
  471. target_col = &its_dev->its->collections[cpu];
  472. its_send_movi(its_dev, target_col, id);
  473. its_dev->collection = target_col;
  474. return IRQ_SET_MASK_OK_DONE;
  475. }
  476. static void its_irq_compose_msi_msg(struct irq_data *d, struct msi_msg *msg)
  477. {
  478. struct its_device *its_dev = irq_data_get_irq_chip_data(d);
  479. struct its_node *its;
  480. u64 addr;
  481. its = its_dev->its;
  482. addr = its->phys_base + GITS_TRANSLATER;
  483. msg->address_lo = addr & ((1UL << 32) - 1);
  484. msg->address_hi = addr >> 32;
  485. msg->data = its_get_event_id(d);
  486. }
  487. static struct irq_chip its_irq_chip = {
  488. .name = "ITS",
  489. .irq_mask = its_mask_irq,
  490. .irq_unmask = its_unmask_irq,
  491. .irq_eoi = its_eoi_irq,
  492. .irq_set_affinity = its_set_affinity,
  493. .irq_compose_msi_msg = its_irq_compose_msi_msg,
  494. };
  495. static void its_mask_msi_irq(struct irq_data *d)
  496. {
  497. pci_msi_mask_irq(d);
  498. irq_chip_mask_parent(d);
  499. }
  500. static void its_unmask_msi_irq(struct irq_data *d)
  501. {
  502. pci_msi_unmask_irq(d);
  503. irq_chip_unmask_parent(d);
  504. }
  505. static struct irq_chip its_msi_irq_chip = {
  506. .name = "ITS-MSI",
  507. .irq_unmask = its_unmask_msi_irq,
  508. .irq_mask = its_mask_msi_irq,
  509. .irq_eoi = irq_chip_eoi_parent,
  510. .irq_write_msi_msg = pci_msi_domain_write_msg,
  511. };
  512. /*
  513. * How we allocate LPIs:
  514. *
  515. * The GIC has id_bits bits for interrupt identifiers. From there, we
  516. * must subtract 8192 which are reserved for SGIs/PPIs/SPIs. Then, as
  517. * we allocate LPIs by chunks of 32, we can shift the whole thing by 5
  518. * bits to the right.
  519. *
  520. * This gives us (((1UL << id_bits) - 8192) >> 5) possible allocations.
  521. */
  522. #define IRQS_PER_CHUNK_SHIFT 5
  523. #define IRQS_PER_CHUNK (1 << IRQS_PER_CHUNK_SHIFT)
  524. static unsigned long *lpi_bitmap;
  525. static u32 lpi_chunks;
  526. static DEFINE_SPINLOCK(lpi_lock);
  527. static int its_lpi_to_chunk(int lpi)
  528. {
  529. return (lpi - 8192) >> IRQS_PER_CHUNK_SHIFT;
  530. }
  531. static int its_chunk_to_lpi(int chunk)
  532. {
  533. return (chunk << IRQS_PER_CHUNK_SHIFT) + 8192;
  534. }
  535. static int its_lpi_init(u32 id_bits)
  536. {
  537. lpi_chunks = its_lpi_to_chunk(1UL << id_bits);
  538. lpi_bitmap = kzalloc(BITS_TO_LONGS(lpi_chunks) * sizeof(long),
  539. GFP_KERNEL);
  540. if (!lpi_bitmap) {
  541. lpi_chunks = 0;
  542. return -ENOMEM;
  543. }
  544. pr_info("ITS: Allocated %d chunks for LPIs\n", (int)lpi_chunks);
  545. return 0;
  546. }
  547. static unsigned long *its_lpi_alloc_chunks(int nr_irqs, int *base, int *nr_ids)
  548. {
  549. unsigned long *bitmap = NULL;
  550. int chunk_id;
  551. int nr_chunks;
  552. int i;
  553. nr_chunks = DIV_ROUND_UP(nr_irqs, IRQS_PER_CHUNK);
  554. spin_lock(&lpi_lock);
  555. do {
  556. chunk_id = bitmap_find_next_zero_area(lpi_bitmap, lpi_chunks,
  557. 0, nr_chunks, 0);
  558. if (chunk_id < lpi_chunks)
  559. break;
  560. nr_chunks--;
  561. } while (nr_chunks > 0);
  562. if (!nr_chunks)
  563. goto out;
  564. bitmap = kzalloc(BITS_TO_LONGS(nr_chunks * IRQS_PER_CHUNK) * sizeof (long),
  565. GFP_ATOMIC);
  566. if (!bitmap)
  567. goto out;
  568. for (i = 0; i < nr_chunks; i++)
  569. set_bit(chunk_id + i, lpi_bitmap);
  570. *base = its_chunk_to_lpi(chunk_id);
  571. *nr_ids = nr_chunks * IRQS_PER_CHUNK;
  572. out:
  573. spin_unlock(&lpi_lock);
  574. return bitmap;
  575. }
  576. static void its_lpi_free(unsigned long *bitmap, int base, int nr_ids)
  577. {
  578. int lpi;
  579. spin_lock(&lpi_lock);
  580. for (lpi = base; lpi < (base + nr_ids); lpi += IRQS_PER_CHUNK) {
  581. int chunk = its_lpi_to_chunk(lpi);
  582. BUG_ON(chunk > lpi_chunks);
  583. if (test_bit(chunk, lpi_bitmap)) {
  584. clear_bit(chunk, lpi_bitmap);
  585. } else {
  586. pr_err("Bad LPI chunk %d\n", chunk);
  587. }
  588. }
  589. spin_unlock(&lpi_lock);
  590. kfree(bitmap);
  591. }
  592. /*
  593. * We allocate 64kB for PROPBASE. That gives us at most 64K LPIs to
  594. * deal with (one configuration byte per interrupt). PENDBASE has to
  595. * be 64kB aligned (one bit per LPI, plus 8192 bits for SPI/PPI/SGI).
  596. */
  597. #define LPI_PROPBASE_SZ SZ_64K
  598. #define LPI_PENDBASE_SZ (LPI_PROPBASE_SZ / 8 + SZ_1K)
  599. /*
  600. * This is how many bits of ID we need, including the useless ones.
  601. */
  602. #define LPI_NRBITS ilog2(LPI_PROPBASE_SZ + SZ_8K)
  603. #define LPI_PROP_DEFAULT_PRIO 0xa0
  604. static int __init its_alloc_lpi_tables(void)
  605. {
  606. phys_addr_t paddr;
  607. gic_rdists->prop_page = alloc_pages(GFP_NOWAIT,
  608. get_order(LPI_PROPBASE_SZ));
  609. if (!gic_rdists->prop_page) {
  610. pr_err("Failed to allocate PROPBASE\n");
  611. return -ENOMEM;
  612. }
  613. paddr = page_to_phys(gic_rdists->prop_page);
  614. pr_info("GIC: using LPI property table @%pa\n", &paddr);
  615. /* Priority 0xa0, Group-1, disabled */
  616. memset(page_address(gic_rdists->prop_page),
  617. LPI_PROP_DEFAULT_PRIO | LPI_PROP_GROUP1,
  618. LPI_PROPBASE_SZ);
  619. /* Make sure the GIC will observe the written configuration */
  620. __flush_dcache_area(page_address(gic_rdists->prop_page), LPI_PROPBASE_SZ);
  621. return 0;
  622. }
  623. static const char *its_base_type_string[] = {
  624. [GITS_BASER_TYPE_DEVICE] = "Devices",
  625. [GITS_BASER_TYPE_VCPU] = "Virtual CPUs",
  626. [GITS_BASER_TYPE_CPU] = "Physical CPUs",
  627. [GITS_BASER_TYPE_COLLECTION] = "Interrupt Collections",
  628. [GITS_BASER_TYPE_RESERVED5] = "Reserved (5)",
  629. [GITS_BASER_TYPE_RESERVED6] = "Reserved (6)",
  630. [GITS_BASER_TYPE_RESERVED7] = "Reserved (7)",
  631. };
  632. static void its_free_tables(struct its_node *its)
  633. {
  634. int i;
  635. for (i = 0; i < GITS_BASER_NR_REGS; i++) {
  636. if (its->tables[i]) {
  637. free_page((unsigned long)its->tables[i]);
  638. its->tables[i] = NULL;
  639. }
  640. }
  641. }
  642. static int its_alloc_tables(struct its_node *its)
  643. {
  644. int err;
  645. int i;
  646. int psz = PAGE_SIZE;
  647. u64 shr = GITS_BASER_InnerShareable;
  648. for (i = 0; i < GITS_BASER_NR_REGS; i++) {
  649. u64 val = readq_relaxed(its->base + GITS_BASER + i * 8);
  650. u64 type = GITS_BASER_TYPE(val);
  651. u64 entry_size = GITS_BASER_ENTRY_SIZE(val);
  652. u64 tmp;
  653. void *base;
  654. if (type == GITS_BASER_TYPE_NONE)
  655. continue;
  656. /* We're lazy and only allocate a single page for now */
  657. base = (void *)get_zeroed_page(GFP_KERNEL);
  658. if (!base) {
  659. err = -ENOMEM;
  660. goto out_free;
  661. }
  662. its->tables[i] = base;
  663. retry_baser:
  664. val = (virt_to_phys(base) |
  665. (type << GITS_BASER_TYPE_SHIFT) |
  666. ((entry_size - 1) << GITS_BASER_ENTRY_SIZE_SHIFT) |
  667. GITS_BASER_WaWb |
  668. shr |
  669. GITS_BASER_VALID);
  670. switch (psz) {
  671. case SZ_4K:
  672. val |= GITS_BASER_PAGE_SIZE_4K;
  673. break;
  674. case SZ_16K:
  675. val |= GITS_BASER_PAGE_SIZE_16K;
  676. break;
  677. case SZ_64K:
  678. val |= GITS_BASER_PAGE_SIZE_64K;
  679. break;
  680. }
  681. val |= (PAGE_SIZE / psz) - 1;
  682. writeq_relaxed(val, its->base + GITS_BASER + i * 8);
  683. tmp = readq_relaxed(its->base + GITS_BASER + i * 8);
  684. if ((val ^ tmp) & GITS_BASER_SHAREABILITY_MASK) {
  685. /*
  686. * Shareability didn't stick. Just use
  687. * whatever the read reported, which is likely
  688. * to be the only thing this redistributor
  689. * supports.
  690. */
  691. shr = tmp & GITS_BASER_SHAREABILITY_MASK;
  692. goto retry_baser;
  693. }
  694. if ((val ^ tmp) & GITS_BASER_PAGE_SIZE_MASK) {
  695. /*
  696. * Page size didn't stick. Let's try a smaller
  697. * size and retry. If we reach 4K, then
  698. * something is horribly wrong...
  699. */
  700. switch (psz) {
  701. case SZ_16K:
  702. psz = SZ_4K;
  703. goto retry_baser;
  704. case SZ_64K:
  705. psz = SZ_16K;
  706. goto retry_baser;
  707. }
  708. }
  709. if (val != tmp) {
  710. pr_err("ITS: %s: GITS_BASER%d doesn't stick: %lx %lx\n",
  711. its->msi_chip.of_node->full_name, i,
  712. (unsigned long) val, (unsigned long) tmp);
  713. err = -ENXIO;
  714. goto out_free;
  715. }
  716. pr_info("ITS: allocated %d %s @%lx (psz %dK, shr %d)\n",
  717. (int)(PAGE_SIZE / entry_size),
  718. its_base_type_string[type],
  719. (unsigned long)virt_to_phys(base),
  720. psz / SZ_1K, (int)shr >> GITS_BASER_SHAREABILITY_SHIFT);
  721. }
  722. return 0;
  723. out_free:
  724. its_free_tables(its);
  725. return err;
  726. }
  727. static int its_alloc_collections(struct its_node *its)
  728. {
  729. its->collections = kzalloc(nr_cpu_ids * sizeof(*its->collections),
  730. GFP_KERNEL);
  731. if (!its->collections)
  732. return -ENOMEM;
  733. return 0;
  734. }
  735. static void its_cpu_init_lpis(void)
  736. {
  737. void __iomem *rbase = gic_data_rdist_rd_base();
  738. struct page *pend_page;
  739. u64 val, tmp;
  740. /* If we didn't allocate the pending table yet, do it now */
  741. pend_page = gic_data_rdist()->pend_page;
  742. if (!pend_page) {
  743. phys_addr_t paddr;
  744. /*
  745. * The pending pages have to be at least 64kB aligned,
  746. * hence the 'max(LPI_PENDBASE_SZ, SZ_64K)' below.
  747. */
  748. pend_page = alloc_pages(GFP_NOWAIT | __GFP_ZERO,
  749. get_order(max(LPI_PENDBASE_SZ, SZ_64K)));
  750. if (!pend_page) {
  751. pr_err("Failed to allocate PENDBASE for CPU%d\n",
  752. smp_processor_id());
  753. return;
  754. }
  755. /* Make sure the GIC will observe the zero-ed page */
  756. __flush_dcache_area(page_address(pend_page), LPI_PENDBASE_SZ);
  757. paddr = page_to_phys(pend_page);
  758. pr_info("CPU%d: using LPI pending table @%pa\n",
  759. smp_processor_id(), &paddr);
  760. gic_data_rdist()->pend_page = pend_page;
  761. }
  762. /* Disable LPIs */
  763. val = readl_relaxed(rbase + GICR_CTLR);
  764. val &= ~GICR_CTLR_ENABLE_LPIS;
  765. writel_relaxed(val, rbase + GICR_CTLR);
  766. /*
  767. * Make sure any change to the table is observable by the GIC.
  768. */
  769. dsb(sy);
  770. /* set PROPBASE */
  771. val = (page_to_phys(gic_rdists->prop_page) |
  772. GICR_PROPBASER_InnerShareable |
  773. GICR_PROPBASER_WaWb |
  774. ((LPI_NRBITS - 1) & GICR_PROPBASER_IDBITS_MASK));
  775. writeq_relaxed(val, rbase + GICR_PROPBASER);
  776. tmp = readq_relaxed(rbase + GICR_PROPBASER);
  777. if ((tmp ^ val) & GICR_PROPBASER_SHAREABILITY_MASK) {
  778. pr_info_once("GIC: using cache flushing for LPI property table\n");
  779. gic_rdists->flags |= RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING;
  780. }
  781. /* set PENDBASE */
  782. val = (page_to_phys(pend_page) |
  783. GICR_PROPBASER_InnerShareable |
  784. GICR_PROPBASER_WaWb);
  785. writeq_relaxed(val, rbase + GICR_PENDBASER);
  786. /* Enable LPIs */
  787. val = readl_relaxed(rbase + GICR_CTLR);
  788. val |= GICR_CTLR_ENABLE_LPIS;
  789. writel_relaxed(val, rbase + GICR_CTLR);
  790. /* Make sure the GIC has seen the above */
  791. dsb(sy);
  792. }
  793. static void its_cpu_init_collection(void)
  794. {
  795. struct its_node *its;
  796. int cpu;
  797. spin_lock(&its_lock);
  798. cpu = smp_processor_id();
  799. list_for_each_entry(its, &its_nodes, entry) {
  800. u64 target;
  801. /*
  802. * We now have to bind each collection to its target
  803. * redistributor.
  804. */
  805. if (readq_relaxed(its->base + GITS_TYPER) & GITS_TYPER_PTA) {
  806. /*
  807. * This ITS wants the physical address of the
  808. * redistributor.
  809. */
  810. target = gic_data_rdist()->phys_base;
  811. } else {
  812. /*
  813. * This ITS wants a linear CPU number.
  814. */
  815. target = readq_relaxed(gic_data_rdist_rd_base() + GICR_TYPER);
  816. target = GICR_TYPER_CPU_NUMBER(target);
  817. }
  818. /* Perform collection mapping */
  819. its->collections[cpu].target_address = target;
  820. its->collections[cpu].col_id = cpu;
  821. its_send_mapc(its, &its->collections[cpu], 1);
  822. its_send_invall(its, &its->collections[cpu]);
  823. }
  824. spin_unlock(&its_lock);
  825. }
  826. static struct its_device *its_find_device(struct its_node *its, u32 dev_id)
  827. {
  828. struct its_device *its_dev = NULL, *tmp;
  829. raw_spin_lock(&its->lock);
  830. list_for_each_entry(tmp, &its->its_device_list, entry) {
  831. if (tmp->device_id == dev_id) {
  832. its_dev = tmp;
  833. break;
  834. }
  835. }
  836. raw_spin_unlock(&its->lock);
  837. return its_dev;
  838. }
  839. static struct its_device *its_create_device(struct its_node *its, u32 dev_id,
  840. int nvecs)
  841. {
  842. struct its_device *dev;
  843. unsigned long *lpi_map;
  844. void *itt;
  845. int lpi_base;
  846. int nr_lpis;
  847. int nr_ites;
  848. int cpu;
  849. int sz;
  850. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  851. /*
  852. * At least one bit of EventID is being used, hence a minimum
  853. * of two entries. No, the architecture doesn't let you
  854. * express an ITT with a single entry.
  855. */
  856. nr_ites = max(2UL, roundup_pow_of_two(nvecs));
  857. sz = nr_ites * its->ite_size;
  858. sz = max(sz, ITS_ITT_ALIGN) + ITS_ITT_ALIGN - 1;
  859. itt = kmalloc(sz, GFP_KERNEL);
  860. lpi_map = its_lpi_alloc_chunks(nvecs, &lpi_base, &nr_lpis);
  861. if (!dev || !itt || !lpi_map) {
  862. kfree(dev);
  863. kfree(itt);
  864. kfree(lpi_map);
  865. return NULL;
  866. }
  867. dev->its = its;
  868. dev->itt = itt;
  869. dev->nr_ites = nr_ites;
  870. dev->lpi_map = lpi_map;
  871. dev->lpi_base = lpi_base;
  872. dev->nr_lpis = nr_lpis;
  873. dev->device_id = dev_id;
  874. INIT_LIST_HEAD(&dev->entry);
  875. raw_spin_lock(&its->lock);
  876. list_add(&dev->entry, &its->its_device_list);
  877. raw_spin_unlock(&its->lock);
  878. /* Bind the device to the first possible CPU */
  879. cpu = cpumask_first(cpu_online_mask);
  880. dev->collection = &its->collections[cpu];
  881. /* Map device to its ITT */
  882. its_send_mapd(dev, 1);
  883. return dev;
  884. }
  885. static void its_free_device(struct its_device *its_dev)
  886. {
  887. raw_spin_lock(&its_dev->its->lock);
  888. list_del(&its_dev->entry);
  889. raw_spin_unlock(&its_dev->its->lock);
  890. kfree(its_dev->itt);
  891. kfree(its_dev);
  892. }
  893. static int its_alloc_device_irq(struct its_device *dev, irq_hw_number_t *hwirq)
  894. {
  895. int idx;
  896. idx = find_first_zero_bit(dev->lpi_map, dev->nr_lpis);
  897. if (idx == dev->nr_lpis)
  898. return -ENOSPC;
  899. *hwirq = dev->lpi_base + idx;
  900. set_bit(idx, dev->lpi_map);
  901. return 0;
  902. }
  903. static int its_msi_prepare(struct irq_domain *domain, struct device *dev,
  904. int nvec, msi_alloc_info_t *info)
  905. {
  906. struct pci_dev *pdev;
  907. struct its_node *its;
  908. u32 dev_id;
  909. struct its_device *its_dev;
  910. if (!dev_is_pci(dev))
  911. return -EINVAL;
  912. pdev = to_pci_dev(dev);
  913. dev_id = PCI_DEVID(pdev->bus->number, pdev->devfn);
  914. its = domain->parent->host_data;
  915. its_dev = its_find_device(its, dev_id);
  916. if (WARN_ON(its_dev))
  917. return -EINVAL;
  918. its_dev = its_create_device(its, dev_id, nvec);
  919. if (!its_dev)
  920. return -ENOMEM;
  921. dev_dbg(&pdev->dev, "ITT %d entries, %d bits\n", nvec, ilog2(nvec));
  922. info->scratchpad[0].ptr = its_dev;
  923. info->scratchpad[1].ptr = dev;
  924. return 0;
  925. }
  926. static struct msi_domain_ops its_pci_msi_ops = {
  927. .msi_prepare = its_msi_prepare,
  928. };
  929. static struct msi_domain_info its_pci_msi_domain_info = {
  930. .flags = (MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS |
  931. MSI_FLAG_MULTI_PCI_MSI | MSI_FLAG_PCI_MSIX),
  932. .ops = &its_pci_msi_ops,
  933. .chip = &its_msi_irq_chip,
  934. };
  935. static int its_irq_gic_domain_alloc(struct irq_domain *domain,
  936. unsigned int virq,
  937. irq_hw_number_t hwirq)
  938. {
  939. struct of_phandle_args args;
  940. args.np = domain->parent->of_node;
  941. args.args_count = 3;
  942. args.args[0] = GIC_IRQ_TYPE_LPI;
  943. args.args[1] = hwirq;
  944. args.args[2] = IRQ_TYPE_EDGE_RISING;
  945. return irq_domain_alloc_irqs_parent(domain, virq, 1, &args);
  946. }
  947. static int its_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
  948. unsigned int nr_irqs, void *args)
  949. {
  950. msi_alloc_info_t *info = args;
  951. struct its_device *its_dev = info->scratchpad[0].ptr;
  952. irq_hw_number_t hwirq;
  953. int err;
  954. int i;
  955. for (i = 0; i < nr_irqs; i++) {
  956. err = its_alloc_device_irq(its_dev, &hwirq);
  957. if (err)
  958. return err;
  959. err = its_irq_gic_domain_alloc(domain, virq + i, hwirq);
  960. if (err)
  961. return err;
  962. irq_domain_set_hwirq_and_chip(domain, virq + i,
  963. hwirq, &its_irq_chip, its_dev);
  964. dev_dbg(info->scratchpad[1].ptr, "ID:%d pID:%d vID:%d\n",
  965. (int)(hwirq - its_dev->lpi_base), (int)hwirq, virq + i);
  966. }
  967. return 0;
  968. }
  969. static void its_irq_domain_activate(struct irq_domain *domain,
  970. struct irq_data *d)
  971. {
  972. struct its_device *its_dev = irq_data_get_irq_chip_data(d);
  973. u32 event = its_get_event_id(d);
  974. /* Map the GIC IRQ and event to the device */
  975. its_send_mapvi(its_dev, d->hwirq, event);
  976. }
  977. static void its_irq_domain_deactivate(struct irq_domain *domain,
  978. struct irq_data *d)
  979. {
  980. struct its_device *its_dev = irq_data_get_irq_chip_data(d);
  981. u32 event = its_get_event_id(d);
  982. /* Stop the delivery of interrupts */
  983. its_send_discard(its_dev, event);
  984. }
  985. static void its_irq_domain_free(struct irq_domain *domain, unsigned int virq,
  986. unsigned int nr_irqs)
  987. {
  988. struct irq_data *d = irq_domain_get_irq_data(domain, virq);
  989. struct its_device *its_dev = irq_data_get_irq_chip_data(d);
  990. int i;
  991. for (i = 0; i < nr_irqs; i++) {
  992. struct irq_data *data = irq_domain_get_irq_data(domain,
  993. virq + i);
  994. u32 event = its_get_event_id(data);
  995. /* Mark interrupt index as unused */
  996. clear_bit(event, its_dev->lpi_map);
  997. /* Nuke the entry in the domain */
  998. irq_domain_reset_irq_data(data);
  999. }
  1000. /* If all interrupts have been freed, start mopping the floor */
  1001. if (bitmap_empty(its_dev->lpi_map, its_dev->nr_lpis)) {
  1002. its_lpi_free(its_dev->lpi_map,
  1003. its_dev->lpi_base,
  1004. its_dev->nr_lpis);
  1005. /* Unmap device/itt */
  1006. its_send_mapd(its_dev, 0);
  1007. its_free_device(its_dev);
  1008. }
  1009. irq_domain_free_irqs_parent(domain, virq, nr_irqs);
  1010. }
  1011. static const struct irq_domain_ops its_domain_ops = {
  1012. .alloc = its_irq_domain_alloc,
  1013. .free = its_irq_domain_free,
  1014. .activate = its_irq_domain_activate,
  1015. .deactivate = its_irq_domain_deactivate,
  1016. };
  1017. static int its_probe(struct device_node *node, struct irq_domain *parent)
  1018. {
  1019. struct resource res;
  1020. struct its_node *its;
  1021. void __iomem *its_base;
  1022. u32 val;
  1023. u64 baser, tmp;
  1024. int err;
  1025. err = of_address_to_resource(node, 0, &res);
  1026. if (err) {
  1027. pr_warn("%s: no regs?\n", node->full_name);
  1028. return -ENXIO;
  1029. }
  1030. its_base = ioremap(res.start, resource_size(&res));
  1031. if (!its_base) {
  1032. pr_warn("%s: unable to map registers\n", node->full_name);
  1033. return -ENOMEM;
  1034. }
  1035. val = readl_relaxed(its_base + GITS_PIDR2) & GIC_PIDR2_ARCH_MASK;
  1036. if (val != 0x30 && val != 0x40) {
  1037. pr_warn("%s: no ITS detected, giving up\n", node->full_name);
  1038. err = -ENODEV;
  1039. goto out_unmap;
  1040. }
  1041. pr_info("ITS: %s\n", node->full_name);
  1042. its = kzalloc(sizeof(*its), GFP_KERNEL);
  1043. if (!its) {
  1044. err = -ENOMEM;
  1045. goto out_unmap;
  1046. }
  1047. raw_spin_lock_init(&its->lock);
  1048. INIT_LIST_HEAD(&its->entry);
  1049. INIT_LIST_HEAD(&its->its_device_list);
  1050. its->base = its_base;
  1051. its->phys_base = res.start;
  1052. its->msi_chip.of_node = node;
  1053. its->ite_size = ((readl_relaxed(its_base + GITS_TYPER) >> 4) & 0xf) + 1;
  1054. its->cmd_base = kzalloc(ITS_CMD_QUEUE_SZ, GFP_KERNEL);
  1055. if (!its->cmd_base) {
  1056. err = -ENOMEM;
  1057. goto out_free_its;
  1058. }
  1059. its->cmd_write = its->cmd_base;
  1060. err = its_alloc_tables(its);
  1061. if (err)
  1062. goto out_free_cmd;
  1063. err = its_alloc_collections(its);
  1064. if (err)
  1065. goto out_free_tables;
  1066. baser = (virt_to_phys(its->cmd_base) |
  1067. GITS_CBASER_WaWb |
  1068. GITS_CBASER_InnerShareable |
  1069. (ITS_CMD_QUEUE_SZ / SZ_4K - 1) |
  1070. GITS_CBASER_VALID);
  1071. writeq_relaxed(baser, its->base + GITS_CBASER);
  1072. tmp = readq_relaxed(its->base + GITS_CBASER);
  1073. writeq_relaxed(0, its->base + GITS_CWRITER);
  1074. writel_relaxed(1, its->base + GITS_CTLR);
  1075. if ((tmp ^ baser) & GITS_BASER_SHAREABILITY_MASK) {
  1076. pr_info("ITS: using cache flushing for cmd queue\n");
  1077. its->flags |= ITS_FLAGS_CMDQ_NEEDS_FLUSHING;
  1078. }
  1079. if (of_property_read_bool(its->msi_chip.of_node, "msi-controller")) {
  1080. its->domain = irq_domain_add_tree(NULL, &its_domain_ops, its);
  1081. if (!its->domain) {
  1082. err = -ENOMEM;
  1083. goto out_free_tables;
  1084. }
  1085. its->domain->parent = parent;
  1086. its->msi_chip.domain = pci_msi_create_irq_domain(node,
  1087. &its_pci_msi_domain_info,
  1088. its->domain);
  1089. if (!its->msi_chip.domain) {
  1090. err = -ENOMEM;
  1091. goto out_free_domains;
  1092. }
  1093. err = of_pci_msi_chip_add(&its->msi_chip);
  1094. if (err)
  1095. goto out_free_domains;
  1096. }
  1097. spin_lock(&its_lock);
  1098. list_add(&its->entry, &its_nodes);
  1099. spin_unlock(&its_lock);
  1100. return 0;
  1101. out_free_domains:
  1102. if (its->msi_chip.domain)
  1103. irq_domain_remove(its->msi_chip.domain);
  1104. if (its->domain)
  1105. irq_domain_remove(its->domain);
  1106. out_free_tables:
  1107. its_free_tables(its);
  1108. out_free_cmd:
  1109. kfree(its->cmd_base);
  1110. out_free_its:
  1111. kfree(its);
  1112. out_unmap:
  1113. iounmap(its_base);
  1114. pr_err("ITS: failed probing %s (%d)\n", node->full_name, err);
  1115. return err;
  1116. }
  1117. static bool gic_rdists_supports_plpis(void)
  1118. {
  1119. return !!(readl_relaxed(gic_data_rdist_rd_base() + GICR_TYPER) & GICR_TYPER_PLPIS);
  1120. }
  1121. int its_cpu_init(void)
  1122. {
  1123. if (!gic_rdists_supports_plpis()) {
  1124. pr_info("CPU%d: LPIs not supported\n", smp_processor_id());
  1125. return -ENXIO;
  1126. }
  1127. if (!list_empty(&its_nodes)) {
  1128. its_cpu_init_lpis();
  1129. its_cpu_init_collection();
  1130. }
  1131. return 0;
  1132. }
  1133. static struct of_device_id its_device_id[] = {
  1134. { .compatible = "arm,gic-v3-its", },
  1135. {},
  1136. };
  1137. int its_init(struct device_node *node, struct rdists *rdists,
  1138. struct irq_domain *parent_domain)
  1139. {
  1140. struct device_node *np;
  1141. for (np = of_find_matching_node(node, its_device_id); np;
  1142. np = of_find_matching_node(np, its_device_id)) {
  1143. its_probe(np, parent_domain);
  1144. }
  1145. if (list_empty(&its_nodes)) {
  1146. pr_warn("ITS: No ITS available, not enabling LPIs\n");
  1147. return -ENXIO;
  1148. }
  1149. gic_rdists = rdists;
  1150. gic_root_node = node;
  1151. its_alloc_lpi_tables();
  1152. its_lpi_init(rdists->id_bits);
  1153. return 0;
  1154. }