irq-armada-370-xp.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. /*
  2. * Marvell Armada 370 and Armada XP SoC IRQ handling
  3. *
  4. * Copyright (C) 2012 Marvell
  5. *
  6. * Lior Amsalem <alior@marvell.com>
  7. * Gregory CLEMENT <gregory.clement@free-electrons.com>
  8. * Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
  9. * Ben Dooks <ben.dooks@codethink.co.uk>
  10. *
  11. * This file is licensed under the terms of the GNU General Public
  12. * License version 2. This program is licensed "as is" without any
  13. * warranty of any kind, whether express or implied.
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/init.h>
  18. #include <linux/irq.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/irqchip/chained_irq.h>
  21. #include <linux/cpu.h>
  22. #include <linux/io.h>
  23. #include <linux/of_address.h>
  24. #include <linux/of_irq.h>
  25. #include <linux/of_pci.h>
  26. #include <linux/irqdomain.h>
  27. #include <linux/slab.h>
  28. #include <linux/msi.h>
  29. #include <asm/mach/arch.h>
  30. #include <asm/exception.h>
  31. #include <asm/smp_plat.h>
  32. #include <asm/mach/irq.h>
  33. #include "irqchip.h"
  34. /* Interrupt Controller Registers Map */
  35. #define ARMADA_370_XP_INT_SET_MASK_OFFS (0x48)
  36. #define ARMADA_370_XP_INT_CLEAR_MASK_OFFS (0x4C)
  37. #define ARMADA_370_XP_INT_CONTROL (0x00)
  38. #define ARMADA_370_XP_INT_SET_ENABLE_OFFS (0x30)
  39. #define ARMADA_370_XP_INT_CLEAR_ENABLE_OFFS (0x34)
  40. #define ARMADA_370_XP_INT_SOURCE_CTL(irq) (0x100 + irq*4)
  41. #define ARMADA_370_XP_INT_SOURCE_CPU_MASK 0xF
  42. #define ARMADA_370_XP_CPU_INTACK_OFFS (0x44)
  43. #define ARMADA_375_PPI_CAUSE (0x10)
  44. #define ARMADA_370_XP_SW_TRIG_INT_OFFS (0x4)
  45. #define ARMADA_370_XP_IN_DRBEL_MSK_OFFS (0xc)
  46. #define ARMADA_370_XP_IN_DRBEL_CAUSE_OFFS (0x8)
  47. #define ARMADA_370_XP_MAX_PER_CPU_IRQS (28)
  48. #define ARMADA_370_XP_TIMER0_PER_CPU_IRQ (5)
  49. #define IPI_DOORBELL_START (0)
  50. #define IPI_DOORBELL_END (8)
  51. #define IPI_DOORBELL_MASK 0xFF
  52. #define PCI_MSI_DOORBELL_START (16)
  53. #define PCI_MSI_DOORBELL_NR (16)
  54. #define PCI_MSI_DOORBELL_END (32)
  55. #define PCI_MSI_DOORBELL_MASK 0xFFFF0000
  56. static void __iomem *per_cpu_int_base;
  57. static void __iomem *main_int_base;
  58. static struct irq_domain *armada_370_xp_mpic_domain;
  59. #ifdef CONFIG_PCI_MSI
  60. static struct irq_domain *armada_370_xp_msi_domain;
  61. static DECLARE_BITMAP(msi_used, PCI_MSI_DOORBELL_NR);
  62. static DEFINE_MUTEX(msi_used_lock);
  63. static phys_addr_t msi_doorbell_addr;
  64. #endif
  65. /*
  66. * In SMP mode:
  67. * For shared global interrupts, mask/unmask global enable bit
  68. * For CPU interrupts, mask/unmask the calling CPU's bit
  69. */
  70. static void armada_370_xp_irq_mask(struct irq_data *d)
  71. {
  72. irq_hw_number_t hwirq = irqd_to_hwirq(d);
  73. if (hwirq != ARMADA_370_XP_TIMER0_PER_CPU_IRQ)
  74. writel(hwirq, main_int_base +
  75. ARMADA_370_XP_INT_CLEAR_ENABLE_OFFS);
  76. else
  77. writel(hwirq, per_cpu_int_base +
  78. ARMADA_370_XP_INT_SET_MASK_OFFS);
  79. }
  80. static void armada_370_xp_irq_unmask(struct irq_data *d)
  81. {
  82. irq_hw_number_t hwirq = irqd_to_hwirq(d);
  83. if (hwirq != ARMADA_370_XP_TIMER0_PER_CPU_IRQ)
  84. writel(hwirq, main_int_base +
  85. ARMADA_370_XP_INT_SET_ENABLE_OFFS);
  86. else
  87. writel(hwirq, per_cpu_int_base +
  88. ARMADA_370_XP_INT_CLEAR_MASK_OFFS);
  89. }
  90. #ifdef CONFIG_PCI_MSI
  91. static int armada_370_xp_alloc_msi(void)
  92. {
  93. int hwirq;
  94. mutex_lock(&msi_used_lock);
  95. hwirq = find_first_zero_bit(&msi_used, PCI_MSI_DOORBELL_NR);
  96. if (hwirq >= PCI_MSI_DOORBELL_NR)
  97. hwirq = -ENOSPC;
  98. else
  99. set_bit(hwirq, msi_used);
  100. mutex_unlock(&msi_used_lock);
  101. return hwirq;
  102. }
  103. static void armada_370_xp_free_msi(int hwirq)
  104. {
  105. mutex_lock(&msi_used_lock);
  106. if (!test_bit(hwirq, msi_used))
  107. pr_err("trying to free unused MSI#%d\n", hwirq);
  108. else
  109. clear_bit(hwirq, msi_used);
  110. mutex_unlock(&msi_used_lock);
  111. }
  112. static int armada_370_xp_setup_msi_irq(struct msi_chip *chip,
  113. struct pci_dev *pdev,
  114. struct msi_desc *desc)
  115. {
  116. struct msi_msg msg;
  117. int virq, hwirq;
  118. /* We support MSI, but not MSI-X */
  119. if (desc->msi_attrib.is_msix)
  120. return -EINVAL;
  121. hwirq = armada_370_xp_alloc_msi();
  122. if (hwirq < 0)
  123. return hwirq;
  124. virq = irq_create_mapping(armada_370_xp_msi_domain, hwirq);
  125. if (!virq) {
  126. armada_370_xp_free_msi(hwirq);
  127. return -EINVAL;
  128. }
  129. irq_set_msi_desc(virq, desc);
  130. msg.address_lo = msi_doorbell_addr;
  131. msg.address_hi = 0;
  132. msg.data = 0xf00 | (hwirq + 16);
  133. write_msi_msg(virq, &msg);
  134. return 0;
  135. }
  136. static void armada_370_xp_teardown_msi_irq(struct msi_chip *chip,
  137. unsigned int irq)
  138. {
  139. struct irq_data *d = irq_get_irq_data(irq);
  140. unsigned long hwirq = d->hwirq;
  141. irq_dispose_mapping(irq);
  142. armada_370_xp_free_msi(hwirq);
  143. }
  144. static struct irq_chip armada_370_xp_msi_irq_chip = {
  145. .name = "armada_370_xp_msi_irq",
  146. .irq_enable = unmask_msi_irq,
  147. .irq_disable = mask_msi_irq,
  148. .irq_mask = mask_msi_irq,
  149. .irq_unmask = unmask_msi_irq,
  150. };
  151. static int armada_370_xp_msi_map(struct irq_domain *domain, unsigned int virq,
  152. irq_hw_number_t hw)
  153. {
  154. irq_set_chip_and_handler(virq, &armada_370_xp_msi_irq_chip,
  155. handle_simple_irq);
  156. set_irq_flags(virq, IRQF_VALID);
  157. return 0;
  158. }
  159. static const struct irq_domain_ops armada_370_xp_msi_irq_ops = {
  160. .map = armada_370_xp_msi_map,
  161. };
  162. static int armada_370_xp_msi_init(struct device_node *node,
  163. phys_addr_t main_int_phys_base)
  164. {
  165. struct msi_chip *msi_chip;
  166. u32 reg;
  167. int ret;
  168. msi_doorbell_addr = main_int_phys_base +
  169. ARMADA_370_XP_SW_TRIG_INT_OFFS;
  170. msi_chip = kzalloc(sizeof(*msi_chip), GFP_KERNEL);
  171. if (!msi_chip)
  172. return -ENOMEM;
  173. msi_chip->setup_irq = armada_370_xp_setup_msi_irq;
  174. msi_chip->teardown_irq = armada_370_xp_teardown_msi_irq;
  175. msi_chip->of_node = node;
  176. armada_370_xp_msi_domain =
  177. irq_domain_add_linear(NULL, PCI_MSI_DOORBELL_NR,
  178. &armada_370_xp_msi_irq_ops,
  179. NULL);
  180. if (!armada_370_xp_msi_domain) {
  181. kfree(msi_chip);
  182. return -ENOMEM;
  183. }
  184. ret = of_pci_msi_chip_add(msi_chip);
  185. if (ret < 0) {
  186. irq_domain_remove(armada_370_xp_msi_domain);
  187. kfree(msi_chip);
  188. return ret;
  189. }
  190. reg = readl(per_cpu_int_base + ARMADA_370_XP_IN_DRBEL_MSK_OFFS)
  191. | PCI_MSI_DOORBELL_MASK;
  192. writel(reg, per_cpu_int_base +
  193. ARMADA_370_XP_IN_DRBEL_MSK_OFFS);
  194. /* Unmask IPI interrupt */
  195. writel(1, per_cpu_int_base + ARMADA_370_XP_INT_CLEAR_MASK_OFFS);
  196. return 0;
  197. }
  198. #else
  199. static inline int armada_370_xp_msi_init(struct device_node *node,
  200. phys_addr_t main_int_phys_base)
  201. {
  202. return 0;
  203. }
  204. #endif
  205. #ifdef CONFIG_SMP
  206. static DEFINE_RAW_SPINLOCK(irq_controller_lock);
  207. static int armada_xp_set_affinity(struct irq_data *d,
  208. const struct cpumask *mask_val, bool force)
  209. {
  210. irq_hw_number_t hwirq = irqd_to_hwirq(d);
  211. unsigned long reg, mask;
  212. int cpu;
  213. /* Select a single core from the affinity mask which is online */
  214. cpu = cpumask_any_and(mask_val, cpu_online_mask);
  215. mask = 1UL << cpu_logical_map(cpu);
  216. raw_spin_lock(&irq_controller_lock);
  217. reg = readl(main_int_base + ARMADA_370_XP_INT_SOURCE_CTL(hwirq));
  218. reg = (reg & (~ARMADA_370_XP_INT_SOURCE_CPU_MASK)) | mask;
  219. writel(reg, main_int_base + ARMADA_370_XP_INT_SOURCE_CTL(hwirq));
  220. raw_spin_unlock(&irq_controller_lock);
  221. return 0;
  222. }
  223. #endif
  224. static struct irq_chip armada_370_xp_irq_chip = {
  225. .name = "armada_370_xp_irq",
  226. .irq_mask = armada_370_xp_irq_mask,
  227. .irq_mask_ack = armada_370_xp_irq_mask,
  228. .irq_unmask = armada_370_xp_irq_unmask,
  229. #ifdef CONFIG_SMP
  230. .irq_set_affinity = armada_xp_set_affinity,
  231. #endif
  232. };
  233. static int armada_370_xp_mpic_irq_map(struct irq_domain *h,
  234. unsigned int virq, irq_hw_number_t hw)
  235. {
  236. armada_370_xp_irq_mask(irq_get_irq_data(virq));
  237. if (hw != ARMADA_370_XP_TIMER0_PER_CPU_IRQ)
  238. writel(hw, per_cpu_int_base +
  239. ARMADA_370_XP_INT_CLEAR_MASK_OFFS);
  240. else
  241. writel(hw, main_int_base + ARMADA_370_XP_INT_SET_ENABLE_OFFS);
  242. irq_set_status_flags(virq, IRQ_LEVEL);
  243. if (hw == ARMADA_370_XP_TIMER0_PER_CPU_IRQ) {
  244. irq_set_percpu_devid(virq);
  245. irq_set_chip_and_handler(virq, &armada_370_xp_irq_chip,
  246. handle_percpu_devid_irq);
  247. } else {
  248. irq_set_chip_and_handler(virq, &armada_370_xp_irq_chip,
  249. handle_level_irq);
  250. }
  251. set_irq_flags(virq, IRQF_VALID | IRQF_PROBE);
  252. return 0;
  253. }
  254. #ifdef CONFIG_SMP
  255. static void armada_mpic_send_doorbell(const struct cpumask *mask,
  256. unsigned int irq)
  257. {
  258. int cpu;
  259. unsigned long map = 0;
  260. /* Convert our logical CPU mask into a physical one. */
  261. for_each_cpu(cpu, mask)
  262. map |= 1 << cpu_logical_map(cpu);
  263. /*
  264. * Ensure that stores to Normal memory are visible to the
  265. * other CPUs before issuing the IPI.
  266. */
  267. dsb();
  268. /* submit softirq */
  269. writel((map << 8) | irq, main_int_base +
  270. ARMADA_370_XP_SW_TRIG_INT_OFFS);
  271. }
  272. static void armada_xp_mpic_smp_cpu_init(void)
  273. {
  274. u32 control;
  275. int nr_irqs, i;
  276. control = readl(main_int_base + ARMADA_370_XP_INT_CONTROL);
  277. nr_irqs = (control >> 2) & 0x3ff;
  278. for (i = 0; i < nr_irqs; i++)
  279. writel(i, per_cpu_int_base + ARMADA_370_XP_INT_SET_MASK_OFFS);
  280. /* Clear pending IPIs */
  281. writel(0, per_cpu_int_base + ARMADA_370_XP_IN_DRBEL_CAUSE_OFFS);
  282. /* Enable first 8 IPIs */
  283. writel(IPI_DOORBELL_MASK, per_cpu_int_base +
  284. ARMADA_370_XP_IN_DRBEL_MSK_OFFS);
  285. /* Unmask IPI interrupt */
  286. writel(0, per_cpu_int_base + ARMADA_370_XP_INT_CLEAR_MASK_OFFS);
  287. }
  288. static int armada_xp_mpic_secondary_init(struct notifier_block *nfb,
  289. unsigned long action, void *hcpu)
  290. {
  291. if (action == CPU_STARTING || action == CPU_STARTING_FROZEN)
  292. armada_xp_mpic_smp_cpu_init();
  293. return NOTIFY_OK;
  294. }
  295. static struct notifier_block armada_370_xp_mpic_cpu_notifier = {
  296. .notifier_call = armada_xp_mpic_secondary_init,
  297. .priority = 100,
  298. };
  299. #endif /* CONFIG_SMP */
  300. static struct irq_domain_ops armada_370_xp_mpic_irq_ops = {
  301. .map = armada_370_xp_mpic_irq_map,
  302. .xlate = irq_domain_xlate_onecell,
  303. };
  304. #ifdef CONFIG_PCI_MSI
  305. static void armada_370_xp_handle_msi_irq(struct pt_regs *regs, bool is_chained)
  306. {
  307. u32 msimask, msinr;
  308. msimask = readl_relaxed(per_cpu_int_base +
  309. ARMADA_370_XP_IN_DRBEL_CAUSE_OFFS)
  310. & PCI_MSI_DOORBELL_MASK;
  311. writel(~msimask, per_cpu_int_base +
  312. ARMADA_370_XP_IN_DRBEL_CAUSE_OFFS);
  313. for (msinr = PCI_MSI_DOORBELL_START;
  314. msinr < PCI_MSI_DOORBELL_END; msinr++) {
  315. int irq;
  316. if (!(msimask & BIT(msinr)))
  317. continue;
  318. if (is_chained) {
  319. irq = irq_find_mapping(armada_370_xp_msi_domain,
  320. msinr - 16);
  321. generic_handle_irq(irq);
  322. } else {
  323. irq = msinr - 16;
  324. handle_domain_irq(armada_370_xp_msi_domain,
  325. irq, regs);
  326. }
  327. }
  328. }
  329. #else
  330. static void armada_370_xp_handle_msi_irq(struct pt_regs *r, bool b) {}
  331. #endif
  332. static void armada_370_xp_mpic_handle_cascade_irq(unsigned int irq,
  333. struct irq_desc *desc)
  334. {
  335. struct irq_chip *chip = irq_get_chip(irq);
  336. unsigned long irqmap, irqn;
  337. unsigned int cascade_irq;
  338. chained_irq_enter(chip, desc);
  339. irqmap = readl_relaxed(per_cpu_int_base + ARMADA_375_PPI_CAUSE);
  340. if (irqmap & BIT(0)) {
  341. armada_370_xp_handle_msi_irq(NULL, true);
  342. irqmap &= ~BIT(0);
  343. }
  344. for_each_set_bit(irqn, &irqmap, BITS_PER_LONG) {
  345. cascade_irq = irq_find_mapping(armada_370_xp_mpic_domain, irqn);
  346. generic_handle_irq(cascade_irq);
  347. }
  348. chained_irq_exit(chip, desc);
  349. }
  350. static void __exception_irq_entry
  351. armada_370_xp_handle_irq(struct pt_regs *regs)
  352. {
  353. u32 irqstat, irqnr;
  354. do {
  355. irqstat = readl_relaxed(per_cpu_int_base +
  356. ARMADA_370_XP_CPU_INTACK_OFFS);
  357. irqnr = irqstat & 0x3FF;
  358. if (irqnr > 1022)
  359. break;
  360. if (irqnr > 1) {
  361. handle_domain_irq(armada_370_xp_mpic_domain,
  362. irqnr, regs);
  363. continue;
  364. }
  365. /* MSI handling */
  366. if (irqnr == 1)
  367. armada_370_xp_handle_msi_irq(regs, false);
  368. #ifdef CONFIG_SMP
  369. /* IPI Handling */
  370. if (irqnr == 0) {
  371. u32 ipimask, ipinr;
  372. ipimask = readl_relaxed(per_cpu_int_base +
  373. ARMADA_370_XP_IN_DRBEL_CAUSE_OFFS)
  374. & IPI_DOORBELL_MASK;
  375. writel(~ipimask, per_cpu_int_base +
  376. ARMADA_370_XP_IN_DRBEL_CAUSE_OFFS);
  377. /* Handle all pending doorbells */
  378. for (ipinr = IPI_DOORBELL_START;
  379. ipinr < IPI_DOORBELL_END; ipinr++) {
  380. if (ipimask & (0x1 << ipinr))
  381. handle_IPI(ipinr, regs);
  382. }
  383. continue;
  384. }
  385. #endif
  386. } while (1);
  387. }
  388. static int __init armada_370_xp_mpic_of_init(struct device_node *node,
  389. struct device_node *parent)
  390. {
  391. struct resource main_int_res, per_cpu_int_res;
  392. int parent_irq, nr_irqs, i;
  393. u32 control;
  394. BUG_ON(of_address_to_resource(node, 0, &main_int_res));
  395. BUG_ON(of_address_to_resource(node, 1, &per_cpu_int_res));
  396. BUG_ON(!request_mem_region(main_int_res.start,
  397. resource_size(&main_int_res),
  398. node->full_name));
  399. BUG_ON(!request_mem_region(per_cpu_int_res.start,
  400. resource_size(&per_cpu_int_res),
  401. node->full_name));
  402. main_int_base = ioremap(main_int_res.start,
  403. resource_size(&main_int_res));
  404. BUG_ON(!main_int_base);
  405. per_cpu_int_base = ioremap(per_cpu_int_res.start,
  406. resource_size(&per_cpu_int_res));
  407. BUG_ON(!per_cpu_int_base);
  408. control = readl(main_int_base + ARMADA_370_XP_INT_CONTROL);
  409. nr_irqs = (control >> 2) & 0x3ff;
  410. for (i = 0; i < nr_irqs; i++)
  411. writel(i, main_int_base + ARMADA_370_XP_INT_CLEAR_ENABLE_OFFS);
  412. armada_370_xp_mpic_domain =
  413. irq_domain_add_linear(node, nr_irqs,
  414. &armada_370_xp_mpic_irq_ops, NULL);
  415. BUG_ON(!armada_370_xp_mpic_domain);
  416. #ifdef CONFIG_SMP
  417. armada_xp_mpic_smp_cpu_init();
  418. #endif
  419. armada_370_xp_msi_init(node, main_int_res.start);
  420. parent_irq = irq_of_parse_and_map(node, 0);
  421. if (parent_irq <= 0) {
  422. irq_set_default_host(armada_370_xp_mpic_domain);
  423. set_handle_irq(armada_370_xp_handle_irq);
  424. #ifdef CONFIG_SMP
  425. set_smp_cross_call(armada_mpic_send_doorbell);
  426. register_cpu_notifier(&armada_370_xp_mpic_cpu_notifier);
  427. #endif
  428. } else {
  429. irq_set_chained_handler(parent_irq,
  430. armada_370_xp_mpic_handle_cascade_irq);
  431. }
  432. return 0;
  433. }
  434. IRQCHIP_DECLARE(armada_370_xp_mpic, "marvell,mpic", armada_370_xp_mpic_of_init);