octeon-irq.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Copyright (C) 2004-2008 Cavium Networks
  7. */
  8. #include <linux/irq.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/hardirq.h>
  11. #include <asm/octeon/octeon.h>
  12. DEFINE_RWLOCK(octeon_irq_ciu0_rwlock);
  13. DEFINE_RWLOCK(octeon_irq_ciu1_rwlock);
  14. DEFINE_SPINLOCK(octeon_irq_msi_lock);
  15. static void octeon_irq_core_ack(unsigned int irq)
  16. {
  17. unsigned int bit = irq - OCTEON_IRQ_SW0;
  18. /*
  19. * We don't need to disable IRQs to make these atomic since
  20. * they are already disabled earlier in the low level
  21. * interrupt code.
  22. */
  23. clear_c0_status(0x100 << bit);
  24. /* The two user interrupts must be cleared manually. */
  25. if (bit < 2)
  26. clear_c0_cause(0x100 << bit);
  27. }
  28. static void octeon_irq_core_eoi(unsigned int irq)
  29. {
  30. struct irq_desc *desc = irq_desc + irq;
  31. unsigned int bit = irq - OCTEON_IRQ_SW0;
  32. /*
  33. * If an IRQ is being processed while we are disabling it the
  34. * handler will attempt to unmask the interrupt after it has
  35. * been disabled.
  36. */
  37. if (desc->status & IRQ_DISABLED)
  38. return;
  39. /* There is a race here. We should fix it. */
  40. /*
  41. * We don't need to disable IRQs to make these atomic since
  42. * they are already disabled earlier in the low level
  43. * interrupt code.
  44. */
  45. set_c0_status(0x100 << bit);
  46. }
  47. static void octeon_irq_core_enable(unsigned int irq)
  48. {
  49. unsigned long flags;
  50. unsigned int bit = irq - OCTEON_IRQ_SW0;
  51. /*
  52. * We need to disable interrupts to make sure our updates are
  53. * atomic.
  54. */
  55. local_irq_save(flags);
  56. set_c0_status(0x100 << bit);
  57. local_irq_restore(flags);
  58. }
  59. static void octeon_irq_core_disable_local(unsigned int irq)
  60. {
  61. unsigned long flags;
  62. unsigned int bit = irq - OCTEON_IRQ_SW0;
  63. /*
  64. * We need to disable interrupts to make sure our updates are
  65. * atomic.
  66. */
  67. local_irq_save(flags);
  68. clear_c0_status(0x100 << bit);
  69. local_irq_restore(flags);
  70. }
  71. static void octeon_irq_core_disable(unsigned int irq)
  72. {
  73. #ifdef CONFIG_SMP
  74. on_each_cpu((void (*)(void *)) octeon_irq_core_disable_local,
  75. (void *) (long) irq, 1);
  76. #else
  77. octeon_irq_core_disable_local(irq);
  78. #endif
  79. }
  80. static struct irq_chip octeon_irq_chip_core = {
  81. .name = "Core",
  82. .enable = octeon_irq_core_enable,
  83. .disable = octeon_irq_core_disable,
  84. .ack = octeon_irq_core_ack,
  85. .eoi = octeon_irq_core_eoi,
  86. };
  87. static void octeon_irq_ciu0_ack(unsigned int irq)
  88. {
  89. /*
  90. * In order to avoid any locking accessing the CIU, we
  91. * acknowledge CIU interrupts by disabling all of them. This
  92. * way we can use a per core register and avoid any out of
  93. * core locking requirements. This has the side affect that
  94. * CIU interrupts can't be processed recursively.
  95. *
  96. * We don't need to disable IRQs to make these atomic since
  97. * they are already disabled earlier in the low level
  98. * interrupt code.
  99. */
  100. clear_c0_status(0x100 << 2);
  101. }
  102. static void octeon_irq_ciu0_eoi(unsigned int irq)
  103. {
  104. /*
  105. * Enable all CIU interrupts again. We don't need to disable
  106. * IRQs to make these atomic since they are already disabled
  107. * earlier in the low level interrupt code.
  108. */
  109. set_c0_status(0x100 << 2);
  110. }
  111. static void octeon_irq_ciu0_enable(unsigned int irq)
  112. {
  113. int coreid = cvmx_get_core_num();
  114. unsigned long flags;
  115. uint64_t en0;
  116. int bit = irq - OCTEON_IRQ_WORKQ0; /* Bit 0-63 of EN0 */
  117. /*
  118. * A read lock is used here to make sure only one core is ever
  119. * updating the CIU enable bits at a time. During an enable
  120. * the cores don't interfere with each other. During a disable
  121. * the write lock stops any enables that might cause a
  122. * problem.
  123. */
  124. read_lock_irqsave(&octeon_irq_ciu0_rwlock, flags);
  125. en0 = cvmx_read_csr(CVMX_CIU_INTX_EN0(coreid * 2));
  126. en0 |= 1ull << bit;
  127. cvmx_write_csr(CVMX_CIU_INTX_EN0(coreid * 2), en0);
  128. cvmx_read_csr(CVMX_CIU_INTX_EN0(coreid * 2));
  129. read_unlock_irqrestore(&octeon_irq_ciu0_rwlock, flags);
  130. }
  131. static void octeon_irq_ciu0_disable(unsigned int irq)
  132. {
  133. int bit = irq - OCTEON_IRQ_WORKQ0; /* Bit 0-63 of EN0 */
  134. unsigned long flags;
  135. uint64_t en0;
  136. #ifdef CONFIG_SMP
  137. int cpu;
  138. write_lock_irqsave(&octeon_irq_ciu0_rwlock, flags);
  139. for_each_online_cpu(cpu) {
  140. int coreid = cpu_logical_map(cpu);
  141. en0 = cvmx_read_csr(CVMX_CIU_INTX_EN0(coreid * 2));
  142. en0 &= ~(1ull << bit);
  143. cvmx_write_csr(CVMX_CIU_INTX_EN0(coreid * 2), en0);
  144. }
  145. /*
  146. * We need to do a read after the last update to make sure all
  147. * of them are done.
  148. */
  149. cvmx_read_csr(CVMX_CIU_INTX_EN0(cvmx_get_core_num() * 2));
  150. write_unlock_irqrestore(&octeon_irq_ciu0_rwlock, flags);
  151. #else
  152. int coreid = cvmx_get_core_num();
  153. local_irq_save(flags);
  154. en0 = cvmx_read_csr(CVMX_CIU_INTX_EN0(coreid * 2));
  155. en0 &= ~(1ull << bit);
  156. cvmx_write_csr(CVMX_CIU_INTX_EN0(coreid * 2), en0);
  157. cvmx_read_csr(CVMX_CIU_INTX_EN0(coreid * 2));
  158. local_irq_restore(flags);
  159. #endif
  160. }
  161. #ifdef CONFIG_SMP
  162. static void octeon_irq_ciu0_set_affinity(unsigned int irq, const struct cpumask *dest)
  163. {
  164. int cpu;
  165. int bit = irq - OCTEON_IRQ_WORKQ0; /* Bit 0-63 of EN0 */
  166. write_lock(&octeon_irq_ciu0_rwlock);
  167. for_each_online_cpu(cpu) {
  168. int coreid = cpu_logical_map(cpu);
  169. uint64_t en0 =
  170. cvmx_read_csr(CVMX_CIU_INTX_EN0(coreid * 2));
  171. if (cpumask_test_cpu(cpu, dest))
  172. en0 |= 1ull << bit;
  173. else
  174. en0 &= ~(1ull << bit);
  175. cvmx_write_csr(CVMX_CIU_INTX_EN0(coreid * 2), en0);
  176. }
  177. /*
  178. * We need to do a read after the last update to make sure all
  179. * of them are done.
  180. */
  181. cvmx_read_csr(CVMX_CIU_INTX_EN0(cvmx_get_core_num() * 2));
  182. write_unlock(&octeon_irq_ciu0_rwlock);
  183. }
  184. #endif
  185. static struct irq_chip octeon_irq_chip_ciu0 = {
  186. .name = "CIU0",
  187. .enable = octeon_irq_ciu0_enable,
  188. .disable = octeon_irq_ciu0_disable,
  189. .ack = octeon_irq_ciu0_ack,
  190. .eoi = octeon_irq_ciu0_eoi,
  191. #ifdef CONFIG_SMP
  192. .set_affinity = octeon_irq_ciu0_set_affinity,
  193. #endif
  194. };
  195. static void octeon_irq_ciu1_ack(unsigned int irq)
  196. {
  197. /*
  198. * In order to avoid any locking accessing the CIU, we
  199. * acknowledge CIU interrupts by disabling all of them. This
  200. * way we can use a per core register and avoid any out of
  201. * core locking requirements. This has the side affect that
  202. * CIU interrupts can't be processed recursively. We don't
  203. * need to disable IRQs to make these atomic since they are
  204. * already disabled earlier in the low level interrupt code.
  205. */
  206. clear_c0_status(0x100 << 3);
  207. }
  208. static void octeon_irq_ciu1_eoi(unsigned int irq)
  209. {
  210. /*
  211. * Enable all CIU interrupts again. We don't need to disable
  212. * IRQs to make these atomic since they are already disabled
  213. * earlier in the low level interrupt code.
  214. */
  215. set_c0_status(0x100 << 3);
  216. }
  217. static void octeon_irq_ciu1_enable(unsigned int irq)
  218. {
  219. int coreid = cvmx_get_core_num();
  220. unsigned long flags;
  221. uint64_t en1;
  222. int bit = irq - OCTEON_IRQ_WDOG0; /* Bit 0-63 of EN1 */
  223. /*
  224. * A read lock is used here to make sure only one core is ever
  225. * updating the CIU enable bits at a time. During an enable
  226. * the cores don't interfere with each other. During a disable
  227. * the write lock stops any enables that might cause a
  228. * problem.
  229. */
  230. read_lock_irqsave(&octeon_irq_ciu1_rwlock, flags);
  231. en1 = cvmx_read_csr(CVMX_CIU_INTX_EN1(coreid * 2 + 1));
  232. en1 |= 1ull << bit;
  233. cvmx_write_csr(CVMX_CIU_INTX_EN1(coreid * 2 + 1), en1);
  234. cvmx_read_csr(CVMX_CIU_INTX_EN1(coreid * 2 + 1));
  235. read_unlock_irqrestore(&octeon_irq_ciu1_rwlock, flags);
  236. }
  237. static void octeon_irq_ciu1_disable(unsigned int irq)
  238. {
  239. int bit = irq - OCTEON_IRQ_WDOG0; /* Bit 0-63 of EN1 */
  240. unsigned long flags;
  241. uint64_t en1;
  242. #ifdef CONFIG_SMP
  243. int cpu;
  244. write_lock_irqsave(&octeon_irq_ciu1_rwlock, flags);
  245. for_each_online_cpu(cpu) {
  246. int coreid = cpu_logical_map(cpu);
  247. en1 = cvmx_read_csr(CVMX_CIU_INTX_EN1(coreid * 2 + 1));
  248. en1 &= ~(1ull << bit);
  249. cvmx_write_csr(CVMX_CIU_INTX_EN1(coreid * 2 + 1), en1);
  250. }
  251. /*
  252. * We need to do a read after the last update to make sure all
  253. * of them are done.
  254. */
  255. cvmx_read_csr(CVMX_CIU_INTX_EN1(cvmx_get_core_num() * 2 + 1));
  256. write_unlock_irqrestore(&octeon_irq_ciu1_rwlock, flags);
  257. #else
  258. int coreid = cvmx_get_core_num();
  259. local_irq_save(flags);
  260. en1 = cvmx_read_csr(CVMX_CIU_INTX_EN1(coreid * 2 + 1));
  261. en1 &= ~(1ull << bit);
  262. cvmx_write_csr(CVMX_CIU_INTX_EN1(coreid * 2 + 1), en1);
  263. cvmx_read_csr(CVMX_CIU_INTX_EN1(coreid * 2 + 1));
  264. local_irq_restore(flags);
  265. #endif
  266. }
  267. #ifdef CONFIG_SMP
  268. static void octeon_irq_ciu1_set_affinity(unsigned int irq, const struct cpumask *dest)
  269. {
  270. int cpu;
  271. int bit = irq - OCTEON_IRQ_WDOG0; /* Bit 0-63 of EN1 */
  272. write_lock(&octeon_irq_ciu1_rwlock);
  273. for_each_online_cpu(cpu) {
  274. int coreid = cpu_logical_map(cpu);
  275. uint64_t en1 =
  276. cvmx_read_csr(CVMX_CIU_INTX_EN1
  277. (coreid * 2 + 1));
  278. if (cpumask_test_cpu(cpu, dest))
  279. en1 |= 1ull << bit;
  280. else
  281. en1 &= ~(1ull << bit);
  282. cvmx_write_csr(CVMX_CIU_INTX_EN1(coreid * 2 + 1), en1);
  283. }
  284. /*
  285. * We need to do a read after the last update to make sure all
  286. * of them are done.
  287. */
  288. cvmx_read_csr(CVMX_CIU_INTX_EN1(cvmx_get_core_num() * 2 + 1));
  289. write_unlock(&octeon_irq_ciu1_rwlock);
  290. }
  291. #endif
  292. static struct irq_chip octeon_irq_chip_ciu1 = {
  293. .name = "CIU1",
  294. .enable = octeon_irq_ciu1_enable,
  295. .disable = octeon_irq_ciu1_disable,
  296. .ack = octeon_irq_ciu1_ack,
  297. .eoi = octeon_irq_ciu1_eoi,
  298. #ifdef CONFIG_SMP
  299. .set_affinity = octeon_irq_ciu1_set_affinity,
  300. #endif
  301. };
  302. #ifdef CONFIG_PCI_MSI
  303. static void octeon_irq_msi_ack(unsigned int irq)
  304. {
  305. if (!octeon_has_feature(OCTEON_FEATURE_PCIE)) {
  306. /* These chips have PCI */
  307. cvmx_write_csr(CVMX_NPI_NPI_MSI_RCV,
  308. 1ull << (irq - OCTEON_IRQ_MSI_BIT0));
  309. } else {
  310. /*
  311. * These chips have PCIe. Thankfully the ACK doesn't
  312. * need any locking.
  313. */
  314. cvmx_write_csr(CVMX_PEXP_NPEI_MSI_RCV0,
  315. 1ull << (irq - OCTEON_IRQ_MSI_BIT0));
  316. }
  317. }
  318. static void octeon_irq_msi_eoi(unsigned int irq)
  319. {
  320. /* Nothing needed */
  321. }
  322. static void octeon_irq_msi_enable(unsigned int irq)
  323. {
  324. if (!octeon_has_feature(OCTEON_FEATURE_PCIE)) {
  325. /*
  326. * Octeon PCI doesn't have the ability to mask/unmask
  327. * MSI interrupts individually. Instead of
  328. * masking/unmasking them in groups of 16, we simple
  329. * assume MSI devices are well behaved. MSI
  330. * interrupts are always enable and the ACK is assumed
  331. * to be enough.
  332. */
  333. } else {
  334. /* These chips have PCIe. Note that we only support
  335. * the first 64 MSI interrupts. Unfortunately all the
  336. * MSI enables are in the same register. We use
  337. * MSI0's lock to control access to them all.
  338. */
  339. uint64_t en;
  340. unsigned long flags;
  341. spin_lock_irqsave(&octeon_irq_msi_lock, flags);
  342. en = cvmx_read_csr(CVMX_PEXP_NPEI_MSI_ENB0);
  343. en |= 1ull << (irq - OCTEON_IRQ_MSI_BIT0);
  344. cvmx_write_csr(CVMX_PEXP_NPEI_MSI_ENB0, en);
  345. cvmx_read_csr(CVMX_PEXP_NPEI_MSI_ENB0);
  346. spin_unlock_irqrestore(&octeon_irq_msi_lock, flags);
  347. }
  348. }
  349. static void octeon_irq_msi_disable(unsigned int irq)
  350. {
  351. if (!octeon_has_feature(OCTEON_FEATURE_PCIE)) {
  352. /* See comment in enable */
  353. } else {
  354. /*
  355. * These chips have PCIe. Note that we only support
  356. * the first 64 MSI interrupts. Unfortunately all the
  357. * MSI enables are in the same register. We use
  358. * MSI0's lock to control access to them all.
  359. */
  360. uint64_t en;
  361. unsigned long flags;
  362. spin_lock_irqsave(&octeon_irq_msi_lock, flags);
  363. en = cvmx_read_csr(CVMX_PEXP_NPEI_MSI_ENB0);
  364. en &= ~(1ull << (irq - OCTEON_IRQ_MSI_BIT0));
  365. cvmx_write_csr(CVMX_PEXP_NPEI_MSI_ENB0, en);
  366. cvmx_read_csr(CVMX_PEXP_NPEI_MSI_ENB0);
  367. spin_unlock_irqrestore(&octeon_irq_msi_lock, flags);
  368. }
  369. }
  370. static struct irq_chip octeon_irq_chip_msi = {
  371. .name = "MSI",
  372. .enable = octeon_irq_msi_enable,
  373. .disable = octeon_irq_msi_disable,
  374. .ack = octeon_irq_msi_ack,
  375. .eoi = octeon_irq_msi_eoi,
  376. };
  377. #endif
  378. void __init arch_init_irq(void)
  379. {
  380. int irq;
  381. #ifdef CONFIG_SMP
  382. /* Set the default affinity to the boot cpu. */
  383. cpumask_clear(irq_default_affinity);
  384. cpumask_set_cpu(smp_processor_id(), irq_default_affinity);
  385. #endif
  386. if (NR_IRQS < OCTEON_IRQ_LAST)
  387. pr_err("octeon_irq_init: NR_IRQS is set too low\n");
  388. /* 0 - 15 reserved for i8259 master and slave controller. */
  389. /* 17 - 23 Mips internal */
  390. for (irq = OCTEON_IRQ_SW0; irq <= OCTEON_IRQ_TIMER; irq++) {
  391. set_irq_chip_and_handler(irq, &octeon_irq_chip_core,
  392. handle_percpu_irq);
  393. }
  394. /* 24 - 87 CIU_INT_SUM0 */
  395. for (irq = OCTEON_IRQ_WORKQ0; irq <= OCTEON_IRQ_BOOTDMA; irq++) {
  396. set_irq_chip_and_handler(irq, &octeon_irq_chip_ciu0,
  397. handle_percpu_irq);
  398. }
  399. /* 88 - 151 CIU_INT_SUM1 */
  400. for (irq = OCTEON_IRQ_WDOG0; irq <= OCTEON_IRQ_RESERVED151; irq++) {
  401. set_irq_chip_and_handler(irq, &octeon_irq_chip_ciu1,
  402. handle_percpu_irq);
  403. }
  404. #ifdef CONFIG_PCI_MSI
  405. /* 152 - 215 PCI/PCIe MSI interrupts */
  406. for (irq = OCTEON_IRQ_MSI_BIT0; irq <= OCTEON_IRQ_MSI_BIT63; irq++) {
  407. set_irq_chip_and_handler(irq, &octeon_irq_chip_msi,
  408. handle_percpu_irq);
  409. }
  410. #endif
  411. set_c0_status(0x300 << 2);
  412. }
  413. asmlinkage void plat_irq_dispatch(void)
  414. {
  415. const unsigned long core_id = cvmx_get_core_num();
  416. const uint64_t ciu_sum0_address = CVMX_CIU_INTX_SUM0(core_id * 2);
  417. const uint64_t ciu_en0_address = CVMX_CIU_INTX_EN0(core_id * 2);
  418. const uint64_t ciu_sum1_address = CVMX_CIU_INT_SUM1;
  419. const uint64_t ciu_en1_address = CVMX_CIU_INTX_EN1(core_id * 2 + 1);
  420. unsigned long cop0_cause;
  421. unsigned long cop0_status;
  422. uint64_t ciu_en;
  423. uint64_t ciu_sum;
  424. while (1) {
  425. cop0_cause = read_c0_cause();
  426. cop0_status = read_c0_status();
  427. cop0_cause &= cop0_status;
  428. cop0_cause &= ST0_IM;
  429. if (unlikely(cop0_cause & STATUSF_IP2)) {
  430. ciu_sum = cvmx_read_csr(ciu_sum0_address);
  431. ciu_en = cvmx_read_csr(ciu_en0_address);
  432. ciu_sum &= ciu_en;
  433. if (likely(ciu_sum))
  434. do_IRQ(fls64(ciu_sum) + OCTEON_IRQ_WORKQ0 - 1);
  435. else
  436. spurious_interrupt();
  437. } else if (unlikely(cop0_cause & STATUSF_IP3)) {
  438. ciu_sum = cvmx_read_csr(ciu_sum1_address);
  439. ciu_en = cvmx_read_csr(ciu_en1_address);
  440. ciu_sum &= ciu_en;
  441. if (likely(ciu_sum))
  442. do_IRQ(fls64(ciu_sum) + OCTEON_IRQ_WDOG0 - 1);
  443. else
  444. spurious_interrupt();
  445. } else if (likely(cop0_cause)) {
  446. do_IRQ(fls(cop0_cause) - 9 + MIPS_CPU_IRQ_BASE);
  447. } else {
  448. break;
  449. }
  450. }
  451. }