irq-gic-v3-its.c 37 KB

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