irq-armada-370-xp.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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/io.h>
  21. #include <linux/of_address.h>
  22. #include <linux/of_irq.h>
  23. #include <linux/of_pci.h>
  24. #include <linux/irqdomain.h>
  25. #include <linux/slab.h>
  26. #include <linux/msi.h>
  27. #include <asm/mach/arch.h>
  28. #include <asm/exception.h>
  29. #include <asm/smp_plat.h>
  30. #include <asm/mach/irq.h>
  31. #include "irqchip.h"
  32. /* Interrupt Controller Registers Map */
  33. #define ARMADA_370_XP_INT_SET_MASK_OFFS (0x48)
  34. #define ARMADA_370_XP_INT_CLEAR_MASK_OFFS (0x4C)
  35. #define ARMADA_370_XP_INT_CONTROL (0x00)
  36. #define ARMADA_370_XP_INT_SET_ENABLE_OFFS (0x30)
  37. #define ARMADA_370_XP_INT_CLEAR_ENABLE_OFFS (0x34)
  38. #define ARMADA_370_XP_INT_SOURCE_CTL(irq) (0x100 + irq*4)
  39. #define ARMADA_370_XP_CPU_INTACK_OFFS (0x44)
  40. #define ARMADA_370_XP_SW_TRIG_INT_OFFS (0x4)
  41. #define ARMADA_370_XP_IN_DRBEL_MSK_OFFS (0xc)
  42. #define ARMADA_370_XP_IN_DRBEL_CAUSE_OFFS (0x8)
  43. #define ARMADA_370_XP_MAX_PER_CPU_IRQS (28)
  44. #define ARMADA_370_XP_TIMER0_PER_CPU_IRQ (5)
  45. #define IPI_DOORBELL_START (0)
  46. #define IPI_DOORBELL_END (8)
  47. #define IPI_DOORBELL_MASK 0xFF
  48. #define PCI_MSI_DOORBELL_START (16)
  49. #define PCI_MSI_DOORBELL_NR (16)
  50. #define PCI_MSI_DOORBELL_END (32)
  51. #define PCI_MSI_DOORBELL_MASK 0xFFFF0000
  52. static DEFINE_RAW_SPINLOCK(irq_controller_lock);
  53. static void __iomem *per_cpu_int_base;
  54. static void __iomem *main_int_base;
  55. static struct irq_domain *armada_370_xp_mpic_domain;
  56. #ifdef CONFIG_PCI_MSI
  57. static struct irq_domain *armada_370_xp_msi_domain;
  58. static DECLARE_BITMAP(msi_used, PCI_MSI_DOORBELL_NR);
  59. static DEFINE_MUTEX(msi_used_lock);
  60. static phys_addr_t msi_doorbell_addr;
  61. #endif
  62. /*
  63. * In SMP mode:
  64. * For shared global interrupts, mask/unmask global enable bit
  65. * For CPU interrupts, mask/unmask the calling CPU's bit
  66. */
  67. static void armada_370_xp_irq_mask(struct irq_data *d)
  68. {
  69. irq_hw_number_t hwirq = irqd_to_hwirq(d);
  70. if (hwirq != ARMADA_370_XP_TIMER0_PER_CPU_IRQ)
  71. writel(hwirq, main_int_base +
  72. ARMADA_370_XP_INT_CLEAR_ENABLE_OFFS);
  73. else
  74. writel(hwirq, per_cpu_int_base +
  75. ARMADA_370_XP_INT_SET_MASK_OFFS);
  76. }
  77. static void armada_370_xp_irq_unmask(struct irq_data *d)
  78. {
  79. irq_hw_number_t hwirq = irqd_to_hwirq(d);
  80. if (hwirq != ARMADA_370_XP_TIMER0_PER_CPU_IRQ)
  81. writel(hwirq, main_int_base +
  82. ARMADA_370_XP_INT_SET_ENABLE_OFFS);
  83. else
  84. writel(hwirq, per_cpu_int_base +
  85. ARMADA_370_XP_INT_CLEAR_MASK_OFFS);
  86. }
  87. #ifdef CONFIG_PCI_MSI
  88. static int armada_370_xp_alloc_msi(void)
  89. {
  90. int hwirq;
  91. mutex_lock(&msi_used_lock);
  92. hwirq = find_first_zero_bit(&msi_used, PCI_MSI_DOORBELL_NR);
  93. if (hwirq >= PCI_MSI_DOORBELL_NR)
  94. hwirq = -ENOSPC;
  95. else
  96. set_bit(hwirq, msi_used);
  97. mutex_unlock(&msi_used_lock);
  98. return hwirq;
  99. }
  100. static void armada_370_xp_free_msi(int hwirq)
  101. {
  102. mutex_lock(&msi_used_lock);
  103. if (!test_bit(hwirq, msi_used))
  104. pr_err("trying to free unused MSI#%d\n", hwirq);
  105. else
  106. clear_bit(hwirq, msi_used);
  107. mutex_unlock(&msi_used_lock);
  108. }
  109. static int armada_370_xp_setup_msi_irq(struct msi_chip *chip,
  110. struct pci_dev *pdev,
  111. struct msi_desc *desc)
  112. {
  113. struct msi_msg msg;
  114. irq_hw_number_t hwirq;
  115. int virq;
  116. hwirq = armada_370_xp_alloc_msi();
  117. if (hwirq < 0)
  118. return hwirq;
  119. virq = irq_create_mapping(armada_370_xp_msi_domain, hwirq);
  120. if (!virq) {
  121. armada_370_xp_free_msi(hwirq);
  122. return -EINVAL;
  123. }
  124. irq_set_msi_desc(virq, desc);
  125. msg.address_lo = msi_doorbell_addr;
  126. msg.address_hi = 0;
  127. msg.data = 0xf00 | (hwirq + 16);
  128. write_msi_msg(virq, &msg);
  129. return 0;
  130. }
  131. static void armada_370_xp_teardown_msi_irq(struct msi_chip *chip,
  132. unsigned int irq)
  133. {
  134. struct irq_data *d = irq_get_irq_data(irq);
  135. irq_dispose_mapping(irq);
  136. armada_370_xp_free_msi(d->hwirq);
  137. }
  138. static struct irq_chip armada_370_xp_msi_irq_chip = {
  139. .name = "armada_370_xp_msi_irq",
  140. .irq_enable = unmask_msi_irq,
  141. .irq_disable = mask_msi_irq,
  142. .irq_mask = mask_msi_irq,
  143. .irq_unmask = unmask_msi_irq,
  144. };
  145. static int armada_370_xp_msi_map(struct irq_domain *domain, unsigned int virq,
  146. irq_hw_number_t hw)
  147. {
  148. irq_set_chip_and_handler(virq, &armada_370_xp_msi_irq_chip,
  149. handle_simple_irq);
  150. set_irq_flags(virq, IRQF_VALID);
  151. return 0;
  152. }
  153. static const struct irq_domain_ops armada_370_xp_msi_irq_ops = {
  154. .map = armada_370_xp_msi_map,
  155. };
  156. static int armada_370_xp_msi_init(struct device_node *node,
  157. phys_addr_t main_int_phys_base)
  158. {
  159. struct msi_chip *msi_chip;
  160. u32 reg;
  161. int ret;
  162. msi_doorbell_addr = main_int_phys_base +
  163. ARMADA_370_XP_SW_TRIG_INT_OFFS;
  164. msi_chip = kzalloc(sizeof(*msi_chip), GFP_KERNEL);
  165. if (!msi_chip)
  166. return -ENOMEM;
  167. msi_chip->setup_irq = armada_370_xp_setup_msi_irq;
  168. msi_chip->teardown_irq = armada_370_xp_teardown_msi_irq;
  169. msi_chip->of_node = node;
  170. armada_370_xp_msi_domain =
  171. irq_domain_add_linear(NULL, PCI_MSI_DOORBELL_NR,
  172. &armada_370_xp_msi_irq_ops,
  173. NULL);
  174. if (!armada_370_xp_msi_domain) {
  175. kfree(msi_chip);
  176. return -ENOMEM;
  177. }
  178. ret = of_pci_msi_chip_add(msi_chip);
  179. if (ret < 0) {
  180. irq_domain_remove(armada_370_xp_msi_domain);
  181. kfree(msi_chip);
  182. return ret;
  183. }
  184. reg = readl(per_cpu_int_base + ARMADA_370_XP_IN_DRBEL_MSK_OFFS)
  185. | PCI_MSI_DOORBELL_MASK;
  186. writel(reg, per_cpu_int_base +
  187. ARMADA_370_XP_IN_DRBEL_MSK_OFFS);
  188. /* Unmask IPI interrupt */
  189. writel(1, per_cpu_int_base + ARMADA_370_XP_INT_CLEAR_MASK_OFFS);
  190. return 0;
  191. }
  192. #else
  193. static inline int armada_370_xp_msi_init(struct device_node *node,
  194. phys_addr_t main_int_phys_base)
  195. {
  196. return 0;
  197. }
  198. #endif
  199. #ifdef CONFIG_SMP
  200. static int armada_xp_set_affinity(struct irq_data *d,
  201. const struct cpumask *mask_val, bool force)
  202. {
  203. unsigned long reg;
  204. unsigned long new_mask = 0;
  205. unsigned long online_mask = 0;
  206. unsigned long count = 0;
  207. irq_hw_number_t hwirq = irqd_to_hwirq(d);
  208. int cpu;
  209. for_each_cpu(cpu, mask_val) {
  210. new_mask |= 1 << cpu_logical_map(cpu);
  211. count++;
  212. }
  213. /*
  214. * Forbid mutlicore interrupt affinity
  215. * This is required since the MPIC HW doesn't limit
  216. * several CPUs from acknowledging the same interrupt.
  217. */
  218. if (count > 1)
  219. return -EINVAL;
  220. for_each_cpu(cpu, cpu_online_mask)
  221. online_mask |= 1 << cpu_logical_map(cpu);
  222. raw_spin_lock(&irq_controller_lock);
  223. reg = readl(main_int_base + ARMADA_370_XP_INT_SOURCE_CTL(hwirq));
  224. reg = (reg & (~online_mask)) | new_mask;
  225. writel(reg, main_int_base + ARMADA_370_XP_INT_SOURCE_CTL(hwirq));
  226. raw_spin_unlock(&irq_controller_lock);
  227. return 0;
  228. }
  229. #endif
  230. static struct irq_chip armada_370_xp_irq_chip = {
  231. .name = "armada_370_xp_irq",
  232. .irq_mask = armada_370_xp_irq_mask,
  233. .irq_mask_ack = armada_370_xp_irq_mask,
  234. .irq_unmask = armada_370_xp_irq_unmask,
  235. #ifdef CONFIG_SMP
  236. .irq_set_affinity = armada_xp_set_affinity,
  237. #endif
  238. };
  239. static int armada_370_xp_mpic_irq_map(struct irq_domain *h,
  240. unsigned int virq, irq_hw_number_t hw)
  241. {
  242. armada_370_xp_irq_mask(irq_get_irq_data(virq));
  243. if (hw != ARMADA_370_XP_TIMER0_PER_CPU_IRQ)
  244. writel(hw, per_cpu_int_base +
  245. ARMADA_370_XP_INT_CLEAR_MASK_OFFS);
  246. else
  247. writel(hw, main_int_base + ARMADA_370_XP_INT_SET_ENABLE_OFFS);
  248. irq_set_status_flags(virq, IRQ_LEVEL);
  249. if (hw == ARMADA_370_XP_TIMER0_PER_CPU_IRQ) {
  250. irq_set_percpu_devid(virq);
  251. irq_set_chip_and_handler(virq, &armada_370_xp_irq_chip,
  252. handle_percpu_devid_irq);
  253. } else {
  254. irq_set_chip_and_handler(virq, &armada_370_xp_irq_chip,
  255. handle_level_irq);
  256. }
  257. set_irq_flags(virq, IRQF_VALID | IRQF_PROBE);
  258. return 0;
  259. }
  260. #ifdef CONFIG_SMP
  261. void armada_mpic_send_doorbell(const struct cpumask *mask, unsigned int irq)
  262. {
  263. int cpu;
  264. unsigned long map = 0;
  265. /* Convert our logical CPU mask into a physical one. */
  266. for_each_cpu(cpu, mask)
  267. map |= 1 << cpu_logical_map(cpu);
  268. /*
  269. * Ensure that stores to Normal memory are visible to the
  270. * other CPUs before issuing the IPI.
  271. */
  272. dsb();
  273. /* submit softirq */
  274. writel((map << 8) | irq, main_int_base +
  275. ARMADA_370_XP_SW_TRIG_INT_OFFS);
  276. }
  277. void armada_xp_mpic_smp_cpu_init(void)
  278. {
  279. /* Clear pending IPIs */
  280. writel(0, per_cpu_int_base + ARMADA_370_XP_IN_DRBEL_CAUSE_OFFS);
  281. /* Enable first 8 IPIs */
  282. writel(IPI_DOORBELL_MASK, per_cpu_int_base +
  283. ARMADA_370_XP_IN_DRBEL_MSK_OFFS);
  284. /* Unmask IPI interrupt */
  285. writel(0, per_cpu_int_base + ARMADA_370_XP_INT_CLEAR_MASK_OFFS);
  286. }
  287. #endif /* CONFIG_SMP */
  288. static struct irq_domain_ops armada_370_xp_mpic_irq_ops = {
  289. .map = armada_370_xp_mpic_irq_map,
  290. .xlate = irq_domain_xlate_onecell,
  291. };
  292. static asmlinkage void __exception_irq_entry
  293. armada_370_xp_handle_irq(struct pt_regs *regs)
  294. {
  295. u32 irqstat, irqnr;
  296. do {
  297. irqstat = readl_relaxed(per_cpu_int_base +
  298. ARMADA_370_XP_CPU_INTACK_OFFS);
  299. irqnr = irqstat & 0x3FF;
  300. if (irqnr > 1022)
  301. break;
  302. if (irqnr > 1) {
  303. irqnr = irq_find_mapping(armada_370_xp_mpic_domain,
  304. irqnr);
  305. handle_IRQ(irqnr, regs);
  306. continue;
  307. }
  308. #ifdef CONFIG_PCI_MSI
  309. /* MSI handling */
  310. if (irqnr == 1) {
  311. u32 msimask, msinr;
  312. msimask = readl_relaxed(per_cpu_int_base +
  313. ARMADA_370_XP_IN_DRBEL_CAUSE_OFFS)
  314. & PCI_MSI_DOORBELL_MASK;
  315. writel(~PCI_MSI_DOORBELL_MASK, per_cpu_int_base +
  316. ARMADA_370_XP_IN_DRBEL_CAUSE_OFFS);
  317. for (msinr = PCI_MSI_DOORBELL_START;
  318. msinr < PCI_MSI_DOORBELL_END; msinr++) {
  319. int irq;
  320. if (!(msimask & BIT(msinr)))
  321. continue;
  322. irq = irq_find_mapping(armada_370_xp_msi_domain,
  323. msinr - 16);
  324. handle_IRQ(irq, regs);
  325. }
  326. }
  327. #endif
  328. #ifdef CONFIG_SMP
  329. /* IPI Handling */
  330. if (irqnr == 0) {
  331. u32 ipimask, ipinr;
  332. ipimask = readl_relaxed(per_cpu_int_base +
  333. ARMADA_370_XP_IN_DRBEL_CAUSE_OFFS)
  334. & IPI_DOORBELL_MASK;
  335. writel(~IPI_DOORBELL_MASK, per_cpu_int_base +
  336. ARMADA_370_XP_IN_DRBEL_CAUSE_OFFS);
  337. /* Handle all pending doorbells */
  338. for (ipinr = IPI_DOORBELL_START;
  339. ipinr < IPI_DOORBELL_END; ipinr++) {
  340. if (ipimask & (0x1 << ipinr))
  341. handle_IPI(ipinr, regs);
  342. }
  343. continue;
  344. }
  345. #endif
  346. } while (1);
  347. }
  348. static int __init armada_370_xp_mpic_of_init(struct device_node *node,
  349. struct device_node *parent)
  350. {
  351. struct resource main_int_res, per_cpu_int_res;
  352. u32 control;
  353. BUG_ON(of_address_to_resource(node, 0, &main_int_res));
  354. BUG_ON(of_address_to_resource(node, 1, &per_cpu_int_res));
  355. BUG_ON(!request_mem_region(main_int_res.start,
  356. resource_size(&main_int_res),
  357. node->full_name));
  358. BUG_ON(!request_mem_region(per_cpu_int_res.start,
  359. resource_size(&per_cpu_int_res),
  360. node->full_name));
  361. main_int_base = ioremap(main_int_res.start,
  362. resource_size(&main_int_res));
  363. BUG_ON(!main_int_base);
  364. per_cpu_int_base = ioremap(per_cpu_int_res.start,
  365. resource_size(&per_cpu_int_res));
  366. BUG_ON(!per_cpu_int_base);
  367. control = readl(main_int_base + ARMADA_370_XP_INT_CONTROL);
  368. armada_370_xp_mpic_domain =
  369. irq_domain_add_linear(node, (control >> 2) & 0x3ff,
  370. &armada_370_xp_mpic_irq_ops, NULL);
  371. BUG_ON(!armada_370_xp_mpic_domain);
  372. irq_set_default_host(armada_370_xp_mpic_domain);
  373. #ifdef CONFIG_SMP
  374. armada_xp_mpic_smp_cpu_init();
  375. /*
  376. * Set the default affinity from all CPUs to the boot cpu.
  377. * This is required since the MPIC doesn't limit several CPUs
  378. * from acknowledging the same interrupt.
  379. */
  380. cpumask_clear(irq_default_affinity);
  381. cpumask_set_cpu(smp_processor_id(), irq_default_affinity);
  382. #endif
  383. armada_370_xp_msi_init(node, main_int_res.start);
  384. set_handle_irq(armada_370_xp_handle_irq);
  385. return 0;
  386. }
  387. IRQCHIP_DECLARE(armada_370_xp_mpic, "marvell,mpic", armada_370_xp_mpic_of_init);