irq-armada-370-xp.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  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. hwirq = armada_370_xp_alloc_msi();
  119. if (hwirq < 0)
  120. return hwirq;
  121. virq = irq_create_mapping(armada_370_xp_msi_domain, hwirq);
  122. if (!virq) {
  123. armada_370_xp_free_msi(hwirq);
  124. return -EINVAL;
  125. }
  126. irq_set_msi_desc(virq, desc);
  127. msg.address_lo = msi_doorbell_addr;
  128. msg.address_hi = 0;
  129. msg.data = 0xf00 | (hwirq + 16);
  130. write_msi_msg(virq, &msg);
  131. return 0;
  132. }
  133. static void armada_370_xp_teardown_msi_irq(struct msi_chip *chip,
  134. unsigned int irq)
  135. {
  136. struct irq_data *d = irq_get_irq_data(irq);
  137. unsigned long hwirq = d->hwirq;
  138. irq_dispose_mapping(irq);
  139. armada_370_xp_free_msi(hwirq);
  140. }
  141. static int armada_370_xp_check_msi_device(struct msi_chip *chip, struct pci_dev *dev,
  142. int nvec, int type)
  143. {
  144. /* We support MSI, but not MSI-X */
  145. if (type == PCI_CAP_ID_MSI)
  146. return 0;
  147. return -EINVAL;
  148. }
  149. static struct irq_chip armada_370_xp_msi_irq_chip = {
  150. .name = "armada_370_xp_msi_irq",
  151. .irq_enable = unmask_msi_irq,
  152. .irq_disable = mask_msi_irq,
  153. .irq_mask = mask_msi_irq,
  154. .irq_unmask = unmask_msi_irq,
  155. };
  156. static int armada_370_xp_msi_map(struct irq_domain *domain, unsigned int virq,
  157. irq_hw_number_t hw)
  158. {
  159. irq_set_chip_and_handler(virq, &armada_370_xp_msi_irq_chip,
  160. handle_simple_irq);
  161. set_irq_flags(virq, IRQF_VALID);
  162. return 0;
  163. }
  164. static const struct irq_domain_ops armada_370_xp_msi_irq_ops = {
  165. .map = armada_370_xp_msi_map,
  166. };
  167. static int armada_370_xp_msi_init(struct device_node *node,
  168. phys_addr_t main_int_phys_base)
  169. {
  170. struct msi_chip *msi_chip;
  171. u32 reg;
  172. int ret;
  173. msi_doorbell_addr = main_int_phys_base +
  174. ARMADA_370_XP_SW_TRIG_INT_OFFS;
  175. msi_chip = kzalloc(sizeof(*msi_chip), GFP_KERNEL);
  176. if (!msi_chip)
  177. return -ENOMEM;
  178. msi_chip->setup_irq = armada_370_xp_setup_msi_irq;
  179. msi_chip->teardown_irq = armada_370_xp_teardown_msi_irq;
  180. msi_chip->check_device = armada_370_xp_check_msi_device;
  181. msi_chip->of_node = node;
  182. armada_370_xp_msi_domain =
  183. irq_domain_add_linear(NULL, PCI_MSI_DOORBELL_NR,
  184. &armada_370_xp_msi_irq_ops,
  185. NULL);
  186. if (!armada_370_xp_msi_domain) {
  187. kfree(msi_chip);
  188. return -ENOMEM;
  189. }
  190. ret = of_pci_msi_chip_add(msi_chip);
  191. if (ret < 0) {
  192. irq_domain_remove(armada_370_xp_msi_domain);
  193. kfree(msi_chip);
  194. return ret;
  195. }
  196. reg = readl(per_cpu_int_base + ARMADA_370_XP_IN_DRBEL_MSK_OFFS)
  197. | PCI_MSI_DOORBELL_MASK;
  198. writel(reg, per_cpu_int_base +
  199. ARMADA_370_XP_IN_DRBEL_MSK_OFFS);
  200. /* Unmask IPI interrupt */
  201. writel(1, per_cpu_int_base + ARMADA_370_XP_INT_CLEAR_MASK_OFFS);
  202. return 0;
  203. }
  204. #else
  205. static inline int armada_370_xp_msi_init(struct device_node *node,
  206. phys_addr_t main_int_phys_base)
  207. {
  208. return 0;
  209. }
  210. #endif
  211. #ifdef CONFIG_SMP
  212. static DEFINE_RAW_SPINLOCK(irq_controller_lock);
  213. static int armada_xp_set_affinity(struct irq_data *d,
  214. const struct cpumask *mask_val, bool force)
  215. {
  216. irq_hw_number_t hwirq = irqd_to_hwirq(d);
  217. unsigned long reg, mask;
  218. int cpu;
  219. /* Select a single core from the affinity mask which is online */
  220. cpu = cpumask_any_and(mask_val, cpu_online_mask);
  221. mask = 1UL << 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 & (~ARMADA_370_XP_INT_SOURCE_CPU_MASK)) | 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. static void armada_mpic_send_doorbell(const struct cpumask *mask,
  262. unsigned int irq)
  263. {
  264. int cpu;
  265. unsigned long map = 0;
  266. /* Convert our logical CPU mask into a physical one. */
  267. for_each_cpu(cpu, mask)
  268. map |= 1 << cpu_logical_map(cpu);
  269. /*
  270. * Ensure that stores to Normal memory are visible to the
  271. * other CPUs before issuing the IPI.
  272. */
  273. dsb();
  274. /* submit softirq */
  275. writel((map << 8) | irq, main_int_base +
  276. ARMADA_370_XP_SW_TRIG_INT_OFFS);
  277. }
  278. static void armada_xp_mpic_smp_cpu_init(void)
  279. {
  280. u32 control;
  281. int nr_irqs, i;
  282. control = readl(main_int_base + ARMADA_370_XP_INT_CONTROL);
  283. nr_irqs = (control >> 2) & 0x3ff;
  284. for (i = 0; i < nr_irqs; i++)
  285. writel(i, per_cpu_int_base + ARMADA_370_XP_INT_SET_MASK_OFFS);
  286. /* Clear pending IPIs */
  287. writel(0, per_cpu_int_base + ARMADA_370_XP_IN_DRBEL_CAUSE_OFFS);
  288. /* Enable first 8 IPIs */
  289. writel(IPI_DOORBELL_MASK, per_cpu_int_base +
  290. ARMADA_370_XP_IN_DRBEL_MSK_OFFS);
  291. /* Unmask IPI interrupt */
  292. writel(0, per_cpu_int_base + ARMADA_370_XP_INT_CLEAR_MASK_OFFS);
  293. }
  294. static int armada_xp_mpic_secondary_init(struct notifier_block *nfb,
  295. unsigned long action, void *hcpu)
  296. {
  297. if (action == CPU_STARTING || action == CPU_STARTING_FROZEN)
  298. armada_xp_mpic_smp_cpu_init();
  299. return NOTIFY_OK;
  300. }
  301. static struct notifier_block armada_370_xp_mpic_cpu_notifier = {
  302. .notifier_call = armada_xp_mpic_secondary_init,
  303. .priority = 100,
  304. };
  305. #endif /* CONFIG_SMP */
  306. static struct irq_domain_ops armada_370_xp_mpic_irq_ops = {
  307. .map = armada_370_xp_mpic_irq_map,
  308. .xlate = irq_domain_xlate_onecell,
  309. };
  310. #ifdef CONFIG_PCI_MSI
  311. static void armada_370_xp_handle_msi_irq(struct pt_regs *regs, bool is_chained)
  312. {
  313. u32 msimask, msinr;
  314. msimask = readl_relaxed(per_cpu_int_base +
  315. ARMADA_370_XP_IN_DRBEL_CAUSE_OFFS)
  316. & PCI_MSI_DOORBELL_MASK;
  317. writel(~msimask, per_cpu_int_base +
  318. ARMADA_370_XP_IN_DRBEL_CAUSE_OFFS);
  319. for (msinr = PCI_MSI_DOORBELL_START;
  320. msinr < PCI_MSI_DOORBELL_END; msinr++) {
  321. int irq;
  322. if (!(msimask & BIT(msinr)))
  323. continue;
  324. irq = irq_find_mapping(armada_370_xp_msi_domain,
  325. msinr - 16);
  326. if (is_chained)
  327. generic_handle_irq(irq);
  328. else
  329. handle_IRQ(irq, regs);
  330. }
  331. }
  332. #else
  333. static void armada_370_xp_handle_msi_irq(struct pt_regs *r, bool b) {}
  334. #endif
  335. static void armada_370_xp_mpic_handle_cascade_irq(unsigned int irq,
  336. struct irq_desc *desc)
  337. {
  338. struct irq_chip *chip = irq_get_chip(irq);
  339. unsigned long irqmap, irqn;
  340. unsigned int cascade_irq;
  341. chained_irq_enter(chip, desc);
  342. irqmap = readl_relaxed(per_cpu_int_base + ARMADA_375_PPI_CAUSE);
  343. if (irqmap & BIT(0)) {
  344. armada_370_xp_handle_msi_irq(NULL, true);
  345. irqmap &= ~BIT(0);
  346. }
  347. for_each_set_bit(irqn, &irqmap, BITS_PER_LONG) {
  348. cascade_irq = irq_find_mapping(armada_370_xp_mpic_domain, irqn);
  349. generic_handle_irq(cascade_irq);
  350. }
  351. chained_irq_exit(chip, desc);
  352. }
  353. static void __exception_irq_entry
  354. armada_370_xp_handle_irq(struct pt_regs *regs)
  355. {
  356. u32 irqstat, irqnr;
  357. do {
  358. irqstat = readl_relaxed(per_cpu_int_base +
  359. ARMADA_370_XP_CPU_INTACK_OFFS);
  360. irqnr = irqstat & 0x3FF;
  361. if (irqnr > 1022)
  362. break;
  363. if (irqnr > 1) {
  364. irqnr = irq_find_mapping(armada_370_xp_mpic_domain,
  365. irqnr);
  366. handle_IRQ(irqnr, regs);
  367. continue;
  368. }
  369. /* MSI handling */
  370. if (irqnr == 1)
  371. armada_370_xp_handle_msi_irq(regs, false);
  372. #ifdef CONFIG_SMP
  373. /* IPI Handling */
  374. if (irqnr == 0) {
  375. u32 ipimask, ipinr;
  376. ipimask = readl_relaxed(per_cpu_int_base +
  377. ARMADA_370_XP_IN_DRBEL_CAUSE_OFFS)
  378. & IPI_DOORBELL_MASK;
  379. writel(~ipimask, per_cpu_int_base +
  380. ARMADA_370_XP_IN_DRBEL_CAUSE_OFFS);
  381. /* Handle all pending doorbells */
  382. for (ipinr = IPI_DOORBELL_START;
  383. ipinr < IPI_DOORBELL_END; ipinr++) {
  384. if (ipimask & (0x1 << ipinr))
  385. handle_IPI(ipinr, regs);
  386. }
  387. continue;
  388. }
  389. #endif
  390. } while (1);
  391. }
  392. static int __init armada_370_xp_mpic_of_init(struct device_node *node,
  393. struct device_node *parent)
  394. {
  395. struct resource main_int_res, per_cpu_int_res;
  396. int parent_irq, nr_irqs, i;
  397. u32 control;
  398. BUG_ON(of_address_to_resource(node, 0, &main_int_res));
  399. BUG_ON(of_address_to_resource(node, 1, &per_cpu_int_res));
  400. BUG_ON(!request_mem_region(main_int_res.start,
  401. resource_size(&main_int_res),
  402. node->full_name));
  403. BUG_ON(!request_mem_region(per_cpu_int_res.start,
  404. resource_size(&per_cpu_int_res),
  405. node->full_name));
  406. main_int_base = ioremap(main_int_res.start,
  407. resource_size(&main_int_res));
  408. BUG_ON(!main_int_base);
  409. per_cpu_int_base = ioremap(per_cpu_int_res.start,
  410. resource_size(&per_cpu_int_res));
  411. BUG_ON(!per_cpu_int_base);
  412. control = readl(main_int_base + ARMADA_370_XP_INT_CONTROL);
  413. nr_irqs = (control >> 2) & 0x3ff;
  414. for (i = 0; i < nr_irqs; i++)
  415. writel(i, main_int_base + ARMADA_370_XP_INT_CLEAR_ENABLE_OFFS);
  416. armada_370_xp_mpic_domain =
  417. irq_domain_add_linear(node, nr_irqs,
  418. &armada_370_xp_mpic_irq_ops, NULL);
  419. BUG_ON(!armada_370_xp_mpic_domain);
  420. #ifdef CONFIG_SMP
  421. armada_xp_mpic_smp_cpu_init();
  422. #endif
  423. armada_370_xp_msi_init(node, main_int_res.start);
  424. parent_irq = irq_of_parse_and_map(node, 0);
  425. if (parent_irq <= 0) {
  426. irq_set_default_host(armada_370_xp_mpic_domain);
  427. set_handle_irq(armada_370_xp_handle_irq);
  428. #ifdef CONFIG_SMP
  429. set_smp_cross_call(armada_mpic_send_doorbell);
  430. register_cpu_notifier(&armada_370_xp_mpic_cpu_notifier);
  431. #endif
  432. } else {
  433. irq_set_chained_handler(parent_irq,
  434. armada_370_xp_mpic_handle_cascade_irq);
  435. }
  436. return 0;
  437. }
  438. IRQCHIP_DECLARE(armada_370_xp_mpic, "marvell,mpic", armada_370_xp_mpic_of_init);