irq-vic.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * linux/arch/arm/common/vic.c
  4. *
  5. * Copyright (C) 1999 - 2003 ARM Limited
  6. * Copyright (C) 2000 Deep Blue Solutions Ltd
  7. */
  8. #include <linux/export.h>
  9. #include <linux/init.h>
  10. #include <linux/list.h>
  11. #include <linux/io.h>
  12. #include <linux/irq.h>
  13. #include <linux/irqchip.h>
  14. #include <linux/irqchip/chained_irq.h>
  15. #include <linux/irqdomain.h>
  16. #include <linux/of.h>
  17. #include <linux/of_address.h>
  18. #include <linux/of_irq.h>
  19. #include <linux/syscore_ops.h>
  20. #include <linux/device.h>
  21. #include <linux/amba/bus.h>
  22. #include <linux/irqchip/arm-vic.h>
  23. #include <asm/exception.h>
  24. #include <asm/irq.h>
  25. #define VIC_IRQ_STATUS 0x00
  26. #define VIC_FIQ_STATUS 0x04
  27. #define VIC_RAW_STATUS 0x08
  28. #define VIC_INT_SELECT 0x0c /* 1 = FIQ, 0 = IRQ */
  29. #define VIC_INT_ENABLE 0x10 /* 1 = enable, 0 = disable */
  30. #define VIC_INT_ENABLE_CLEAR 0x14
  31. #define VIC_INT_SOFT 0x18
  32. #define VIC_INT_SOFT_CLEAR 0x1c
  33. #define VIC_PROTECT 0x20
  34. #define VIC_PL190_VECT_ADDR 0x30 /* PL190 only */
  35. #define VIC_PL190_DEF_VECT_ADDR 0x34 /* PL190 only */
  36. #define VIC_VECT_ADDR0 0x100 /* 0 to 15 (0..31 PL192) */
  37. #define VIC_VECT_CNTL0 0x200 /* 0 to 15 (0..31 PL192) */
  38. #define VIC_ITCR 0x300 /* VIC test control register */
  39. #define VIC_VECT_CNTL_ENABLE (1 << 5)
  40. #define VIC_PL192_VECT_ADDR 0xF00
  41. /**
  42. * struct vic_device - VIC PM device
  43. * @base: The register base for the VIC.
  44. * @irq: The IRQ number for the base of the VIC.
  45. * @valid_sources: A bitmask of valid interrupts
  46. * @resume_sources: A bitmask of interrupts for resume.
  47. * @resume_irqs: The IRQs enabled for resume.
  48. * @int_select: Save for VIC_INT_SELECT.
  49. * @int_enable: Save for VIC_INT_ENABLE.
  50. * @soft_int: Save for VIC_INT_SOFT.
  51. * @protect: Save for VIC_PROTECT.
  52. * @domain: The IRQ domain for the VIC.
  53. */
  54. struct vic_device {
  55. void __iomem *base;
  56. int irq;
  57. u32 valid_sources;
  58. u32 resume_sources;
  59. u32 resume_irqs;
  60. u32 int_select;
  61. u32 int_enable;
  62. u32 soft_int;
  63. u32 protect;
  64. struct irq_domain *domain;
  65. };
  66. /* we cannot allocate memory when VICs are initially registered */
  67. static struct vic_device vic_devices[CONFIG_ARM_VIC_NR];
  68. static int vic_id;
  69. static void vic_handle_irq(struct pt_regs *regs);
  70. /**
  71. * vic_init2 - common initialisation code
  72. * @base: Base of the VIC.
  73. *
  74. * Common initialisation code for registration
  75. * and resume.
  76. */
  77. static void vic_init2(void __iomem *base)
  78. {
  79. int i;
  80. for (i = 0; i < 16; i++) {
  81. void __iomem *reg = base + VIC_VECT_CNTL0 + (i * 4);
  82. writel(VIC_VECT_CNTL_ENABLE | i, reg);
  83. }
  84. writel(32, base + VIC_PL190_DEF_VECT_ADDR);
  85. }
  86. #ifdef CONFIG_PM
  87. static void resume_one_vic(struct vic_device *vic)
  88. {
  89. void __iomem *base = vic->base;
  90. printk(KERN_DEBUG "%s: resuming vic at %p\n", __func__, base);
  91. /* re-initialise static settings */
  92. vic_init2(base);
  93. writel(vic->int_select, base + VIC_INT_SELECT);
  94. writel(vic->protect, base + VIC_PROTECT);
  95. /* set the enabled ints and then clear the non-enabled */
  96. writel(vic->int_enable, base + VIC_INT_ENABLE);
  97. writel(~vic->int_enable, base + VIC_INT_ENABLE_CLEAR);
  98. /* and the same for the soft-int register */
  99. writel(vic->soft_int, base + VIC_INT_SOFT);
  100. writel(~vic->soft_int, base + VIC_INT_SOFT_CLEAR);
  101. }
  102. static void vic_resume(void *data)
  103. {
  104. int id;
  105. for (id = vic_id - 1; id >= 0; id--)
  106. resume_one_vic(vic_devices + id);
  107. }
  108. static void suspend_one_vic(struct vic_device *vic)
  109. {
  110. void __iomem *base = vic->base;
  111. printk(KERN_DEBUG "%s: suspending vic at %p\n", __func__, base);
  112. vic->int_select = readl(base + VIC_INT_SELECT);
  113. vic->int_enable = readl(base + VIC_INT_ENABLE);
  114. vic->soft_int = readl(base + VIC_INT_SOFT);
  115. vic->protect = readl(base + VIC_PROTECT);
  116. /* set the interrupts (if any) that are used for
  117. * resuming the system */
  118. writel(vic->resume_irqs, base + VIC_INT_ENABLE);
  119. writel(~vic->resume_irqs, base + VIC_INT_ENABLE_CLEAR);
  120. }
  121. static int vic_suspend(void *data)
  122. {
  123. int id;
  124. for (id = 0; id < vic_id; id++)
  125. suspend_one_vic(vic_devices + id);
  126. return 0;
  127. }
  128. static const struct syscore_ops vic_syscore_ops = {
  129. .suspend = vic_suspend,
  130. .resume = vic_resume,
  131. };
  132. static struct syscore vic_syscore = {
  133. .ops = &vic_syscore_ops,
  134. };
  135. /**
  136. * vic_pm_init - initcall to register VIC pm
  137. *
  138. * This is called via late_initcall() to register
  139. * the resources for the VICs due to the early
  140. * nature of the VIC's registration.
  141. */
  142. static int __init vic_pm_init(void)
  143. {
  144. if (vic_id > 0)
  145. register_syscore(&vic_syscore);
  146. return 0;
  147. }
  148. late_initcall(vic_pm_init);
  149. #endif /* CONFIG_PM */
  150. static struct irq_chip vic_chip;
  151. static int vic_irqdomain_map(struct irq_domain *d, unsigned int irq,
  152. irq_hw_number_t hwirq)
  153. {
  154. struct vic_device *v = d->host_data;
  155. /* Skip invalid IRQs, only register handlers for the real ones */
  156. if (!(v->valid_sources & (1 << hwirq)))
  157. return -EPERM;
  158. irq_set_chip_and_handler(irq, &vic_chip, handle_level_irq);
  159. irq_set_chip_data(irq, v->base);
  160. irq_set_probe(irq);
  161. return 0;
  162. }
  163. /*
  164. * Handle each interrupt in a single VIC. Returns non-zero if we've
  165. * handled at least one interrupt. This reads the status register
  166. * before handling each interrupt, which is necessary given that
  167. * handle_IRQ may briefly re-enable interrupts for soft IRQ handling.
  168. */
  169. static int handle_one_vic(struct vic_device *vic, struct pt_regs *regs)
  170. {
  171. u32 stat, irq;
  172. int handled = 0;
  173. while ((stat = readl_relaxed(vic->base + VIC_IRQ_STATUS))) {
  174. irq = ffs(stat) - 1;
  175. generic_handle_domain_irq(vic->domain, irq);
  176. handled = 1;
  177. }
  178. return handled;
  179. }
  180. static void vic_handle_irq_cascaded(struct irq_desc *desc)
  181. {
  182. u32 stat, hwirq;
  183. struct irq_chip *host_chip = irq_desc_get_chip(desc);
  184. struct vic_device *vic = irq_desc_get_handler_data(desc);
  185. chained_irq_enter(host_chip, desc);
  186. while ((stat = readl_relaxed(vic->base + VIC_IRQ_STATUS))) {
  187. hwirq = ffs(stat) - 1;
  188. generic_handle_domain_irq(vic->domain, hwirq);
  189. }
  190. chained_irq_exit(host_chip, desc);
  191. }
  192. /*
  193. * Keep iterating over all registered VIC's until there are no pending
  194. * interrupts.
  195. */
  196. static void __exception_irq_entry vic_handle_irq(struct pt_regs *regs)
  197. {
  198. int i, handled;
  199. do {
  200. for (i = 0, handled = 0; i < vic_id; ++i)
  201. handled |= handle_one_vic(&vic_devices[i], regs);
  202. } while (handled);
  203. }
  204. static const struct irq_domain_ops vic_irqdomain_ops = {
  205. .map = vic_irqdomain_map,
  206. .xlate = irq_domain_xlate_onetwocell,
  207. };
  208. /**
  209. * vic_register() - Register a VIC.
  210. * @base: The base address of the VIC.
  211. * @parent_irq: The parent IRQ if cascaded, else 0.
  212. * @irq: The base IRQ for the VIC.
  213. * @valid_sources: bitmask of valid interrupts
  214. * @resume_sources: bitmask of interrupts allowed for resume sources.
  215. * @node: The device tree node associated with the VIC.
  216. *
  217. * Register the VIC with the system device tree so that it can be notified
  218. * of suspend and resume requests and ensure that the correct actions are
  219. * taken to re-instate the settings on resume.
  220. *
  221. * This also configures the IRQ domain for the VIC.
  222. */
  223. static void __init vic_register(void __iomem *base, unsigned int parent_irq,
  224. unsigned int irq,
  225. u32 valid_sources, u32 resume_sources,
  226. struct device_node *node)
  227. {
  228. struct vic_device *v;
  229. int i;
  230. if (vic_id >= ARRAY_SIZE(vic_devices)) {
  231. printk(KERN_ERR "%s: too few VICs, increase CONFIG_ARM_VIC_NR\n", __func__);
  232. return;
  233. }
  234. v = &vic_devices[vic_id];
  235. v->base = base;
  236. v->valid_sources = valid_sources;
  237. v->resume_sources = resume_sources;
  238. set_handle_irq(vic_handle_irq);
  239. vic_id++;
  240. if (parent_irq) {
  241. irq_set_chained_handler_and_data(parent_irq,
  242. vic_handle_irq_cascaded, v);
  243. }
  244. v->domain = irq_domain_create_simple(of_fwnode_handle(node),
  245. fls(valid_sources), irq,
  246. &vic_irqdomain_ops, v);
  247. /* create an IRQ mapping for each valid IRQ */
  248. for (i = 0; i < fls(valid_sources); i++)
  249. if (valid_sources & (1 << i))
  250. irq_create_mapping(v->domain, i);
  251. /* If no base IRQ was passed, figure out our allocated base */
  252. if (irq)
  253. v->irq = irq;
  254. else
  255. v->irq = irq_find_mapping(v->domain, 0);
  256. }
  257. static void vic_ack_irq(struct irq_data *d)
  258. {
  259. void __iomem *base = irq_data_get_irq_chip_data(d);
  260. unsigned int irq = d->hwirq;
  261. writel(1 << irq, base + VIC_INT_ENABLE_CLEAR);
  262. /* moreover, clear the soft-triggered, in case it was the reason */
  263. writel(1 << irq, base + VIC_INT_SOFT_CLEAR);
  264. }
  265. static void vic_mask_irq(struct irq_data *d)
  266. {
  267. void __iomem *base = irq_data_get_irq_chip_data(d);
  268. unsigned int irq = d->hwirq;
  269. writel(1 << irq, base + VIC_INT_ENABLE_CLEAR);
  270. }
  271. static void vic_unmask_irq(struct irq_data *d)
  272. {
  273. void __iomem *base = irq_data_get_irq_chip_data(d);
  274. unsigned int irq = d->hwirq;
  275. writel(1 << irq, base + VIC_INT_ENABLE);
  276. }
  277. #if defined(CONFIG_PM)
  278. static struct vic_device *vic_from_irq(unsigned int irq)
  279. {
  280. struct vic_device *v = vic_devices;
  281. unsigned int base_irq = irq & ~31;
  282. int id;
  283. for (id = 0; id < vic_id; id++, v++) {
  284. if (v->irq == base_irq)
  285. return v;
  286. }
  287. return NULL;
  288. }
  289. static int vic_set_wake(struct irq_data *d, unsigned int on)
  290. {
  291. struct vic_device *v = vic_from_irq(d->irq);
  292. unsigned int off = d->hwirq;
  293. u32 bit = 1 << off;
  294. if (!v)
  295. return -EINVAL;
  296. if (!(bit & v->resume_sources))
  297. return -EINVAL;
  298. if (on)
  299. v->resume_irqs |= bit;
  300. else
  301. v->resume_irqs &= ~bit;
  302. return 0;
  303. }
  304. #else
  305. #define vic_set_wake NULL
  306. #endif /* CONFIG_PM */
  307. static struct irq_chip vic_chip = {
  308. .name = "VIC",
  309. .irq_ack = vic_ack_irq,
  310. .irq_mask = vic_mask_irq,
  311. .irq_unmask = vic_unmask_irq,
  312. .irq_set_wake = vic_set_wake,
  313. };
  314. static void __init vic_disable(void __iomem *base)
  315. {
  316. writel(0, base + VIC_INT_SELECT);
  317. writel(0, base + VIC_INT_ENABLE);
  318. writel(~0, base + VIC_INT_ENABLE_CLEAR);
  319. writel(0, base + VIC_ITCR);
  320. writel(~0, base + VIC_INT_SOFT_CLEAR);
  321. }
  322. static void __init vic_clear_interrupts(void __iomem *base)
  323. {
  324. unsigned int i;
  325. writel(0, base + VIC_PL190_VECT_ADDR);
  326. for (i = 0; i < 19; i++) {
  327. unsigned int value;
  328. value = readl(base + VIC_PL190_VECT_ADDR);
  329. writel(value, base + VIC_PL190_VECT_ADDR);
  330. }
  331. }
  332. /*
  333. * The PL190 cell from ARM has been modified by ST to handle 64 interrupts.
  334. * The original cell has 32 interrupts, while the modified one has 64,
  335. * replicating two blocks 0x00..0x1f in 0x20..0x3f. In that case
  336. * the probe function is called twice, with base set to offset 000
  337. * and 020 within the page. We call this "second block".
  338. */
  339. static void __init vic_init_st(void __iomem *base, unsigned int irq_start,
  340. u32 vic_sources, struct device_node *node)
  341. {
  342. unsigned int i;
  343. int vic_2nd_block = ((unsigned long)base & ~PAGE_MASK) != 0;
  344. /* Disable all interrupts initially. */
  345. vic_disable(base);
  346. /*
  347. * Make sure we clear all existing interrupts. The vector registers
  348. * in this cell are after the second block of general registers,
  349. * so we can address them using standard offsets, but only from
  350. * the second base address, which is 0x20 in the page
  351. */
  352. if (vic_2nd_block) {
  353. vic_clear_interrupts(base);
  354. /* ST has 16 vectors as well, but we don't enable them by now */
  355. for (i = 0; i < 16; i++) {
  356. void __iomem *reg = base + VIC_VECT_CNTL0 + (i * 4);
  357. writel(0, reg);
  358. }
  359. writel(32, base + VIC_PL190_DEF_VECT_ADDR);
  360. }
  361. vic_register(base, 0, irq_start, vic_sources, 0, node);
  362. }
  363. static void __init __vic_init(void __iomem *base, int parent_irq, int irq_start,
  364. u32 vic_sources, u32 resume_sources,
  365. struct device_node *node)
  366. {
  367. unsigned int i;
  368. u32 cellid = 0;
  369. enum amba_vendor vendor;
  370. /* Identify which VIC cell this one is, by reading the ID */
  371. for (i = 0; i < 4; i++) {
  372. void __iomem *addr;
  373. addr = (void __iomem *)((u32)base & PAGE_MASK) + 0xfe0 + (i * 4);
  374. cellid |= (readl(addr) & 0xff) << (8 * i);
  375. }
  376. vendor = (cellid >> 12) & 0xff;
  377. printk(KERN_INFO "VIC @%p: id 0x%08x, vendor 0x%02x\n",
  378. base, cellid, vendor);
  379. switch(vendor) {
  380. case AMBA_VENDOR_ST:
  381. vic_init_st(base, irq_start, vic_sources, node);
  382. return;
  383. default:
  384. printk(KERN_WARNING "VIC: unknown vendor, continuing anyways\n");
  385. fallthrough;
  386. case AMBA_VENDOR_ARM:
  387. break;
  388. }
  389. /* Disable all interrupts initially. */
  390. vic_disable(base);
  391. /* Make sure we clear all existing interrupts */
  392. vic_clear_interrupts(base);
  393. vic_init2(base);
  394. vic_register(base, parent_irq, irq_start, vic_sources, resume_sources, node);
  395. }
  396. /**
  397. * vic_init() - initialise a vectored interrupt controller
  398. * @base: iomem base address
  399. * @irq_start: starting interrupt number, must be muliple of 32
  400. * @vic_sources: bitmask of interrupt sources to allow
  401. * @resume_sources: bitmask of interrupt sources to allow for resume
  402. */
  403. void __init vic_init(void __iomem *base, unsigned int irq_start,
  404. u32 vic_sources, u32 resume_sources)
  405. {
  406. __vic_init(base, 0, irq_start, vic_sources, resume_sources, NULL);
  407. }
  408. #ifdef CONFIG_OF
  409. static int __init vic_of_init(struct device_node *node,
  410. struct device_node *parent)
  411. {
  412. void __iomem *regs;
  413. u32 interrupt_mask = ~0;
  414. u32 wakeup_mask = ~0;
  415. int parent_irq;
  416. regs = of_iomap(node, 0);
  417. if (WARN_ON(!regs))
  418. return -EIO;
  419. of_property_read_u32(node, "valid-mask", &interrupt_mask);
  420. of_property_read_u32(node, "valid-wakeup-mask", &wakeup_mask);
  421. parent_irq = of_irq_get(node, 0);
  422. if (parent_irq < 0)
  423. parent_irq = 0;
  424. /*
  425. * Passing 0 as first IRQ makes the simple domain allocate descriptors
  426. */
  427. __vic_init(regs, parent_irq, 0, interrupt_mask, wakeup_mask, node);
  428. return 0;
  429. }
  430. IRQCHIP_DECLARE(arm_pl190_vic, "arm,pl190-vic", vic_of_init);
  431. IRQCHIP_DECLARE(arm_pl192_vic, "arm,pl192-vic", vic_of_init);
  432. IRQCHIP_DECLARE(arm_versatile_vic, "arm,versatile-vic", vic_of_init);
  433. #endif /* CONFIG OF */