irq-gic-v3-its.c 38 KB

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