octeon-irq.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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 int 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. return 0;
  184. }
  185. #endif
  186. static struct irq_chip octeon_irq_chip_ciu0 = {
  187. .name = "CIU0",
  188. .enable = octeon_irq_ciu0_enable,
  189. .disable = octeon_irq_ciu0_disable,
  190. .ack = octeon_irq_ciu0_ack,
  191. .eoi = octeon_irq_ciu0_eoi,
  192. #ifdef CONFIG_SMP
  193. .set_affinity = octeon_irq_ciu0_set_affinity,
  194. #endif
  195. };
  196. static void octeon_irq_ciu1_ack(unsigned int irq)
  197. {
  198. /*
  199. * In order to avoid any locking accessing the CIU, we
  200. * acknowledge CIU interrupts by disabling all of them. This
  201. * way we can use a per core register and avoid any out of
  202. * core locking requirements. This has the side affect that
  203. * CIU interrupts can't be processed recursively. We don't
  204. * need to disable IRQs to make these atomic since they are
  205. * already disabled earlier in the low level interrupt code.
  206. */
  207. clear_c0_status(0x100 << 3);
  208. }
  209. static void octeon_irq_ciu1_eoi(unsigned int irq)
  210. {
  211. /*
  212. * Enable all CIU interrupts again. We don't need to disable
  213. * IRQs to make these atomic since they are already disabled
  214. * earlier in the low level interrupt code.
  215. */
  216. set_c0_status(0x100 << 3);
  217. }
  218. static void octeon_irq_ciu1_enable(unsigned int irq)
  219. {
  220. int coreid = cvmx_get_core_num();
  221. unsigned long flags;
  222. uint64_t en1;
  223. int bit = irq - OCTEON_IRQ_WDOG0; /* Bit 0-63 of EN1 */
  224. /*
  225. * A read lock is used here to make sure only one core is ever
  226. * updating the CIU enable bits at a time. During an enable
  227. * the cores don't interfere with each other. During a disable
  228. * the write lock stops any enables that might cause a
  229. * problem.
  230. */
  231. read_lock_irqsave(&octeon_irq_ciu1_rwlock, flags);
  232. en1 = cvmx_read_csr(CVMX_CIU_INTX_EN1(coreid * 2 + 1));
  233. en1 |= 1ull << bit;
  234. cvmx_write_csr(CVMX_CIU_INTX_EN1(coreid * 2 + 1), en1);
  235. cvmx_read_csr(CVMX_CIU_INTX_EN1(coreid * 2 + 1));
  236. read_unlock_irqrestore(&octeon_irq_ciu1_rwlock, flags);
  237. }
  238. static void octeon_irq_ciu1_disable(unsigned int irq)
  239. {
  240. int bit = irq - OCTEON_IRQ_WDOG0; /* Bit 0-63 of EN1 */
  241. unsigned long flags;
  242. uint64_t en1;
  243. #ifdef CONFIG_SMP
  244. int cpu;
  245. write_lock_irqsave(&octeon_irq_ciu1_rwlock, flags);
  246. for_each_online_cpu(cpu) {
  247. int coreid = cpu_logical_map(cpu);
  248. en1 = cvmx_read_csr(CVMX_CIU_INTX_EN1(coreid * 2 + 1));
  249. en1 &= ~(1ull << bit);
  250. cvmx_write_csr(CVMX_CIU_INTX_EN1(coreid * 2 + 1), en1);
  251. }
  252. /*
  253. * We need to do a read after the last update to make sure all
  254. * of them are done.
  255. */
  256. cvmx_read_csr(CVMX_CIU_INTX_EN1(cvmx_get_core_num() * 2 + 1));
  257. write_unlock_irqrestore(&octeon_irq_ciu1_rwlock, flags);
  258. #else
  259. int coreid = cvmx_get_core_num();
  260. local_irq_save(flags);
  261. en1 = cvmx_read_csr(CVMX_CIU_INTX_EN1(coreid * 2 + 1));
  262. en1 &= ~(1ull << bit);
  263. cvmx_write_csr(CVMX_CIU_INTX_EN1(coreid * 2 + 1), en1);
  264. cvmx_read_csr(CVMX_CIU_INTX_EN1(coreid * 2 + 1));
  265. local_irq_restore(flags);
  266. #endif
  267. }
  268. #ifdef CONFIG_SMP
  269. static int octeon_irq_ciu1_set_affinity(unsigned int irq, const struct cpumask *dest)
  270. {
  271. int cpu;
  272. int bit = irq - OCTEON_IRQ_WDOG0; /* Bit 0-63 of EN1 */
  273. write_lock(&octeon_irq_ciu1_rwlock);
  274. for_each_online_cpu(cpu) {
  275. int coreid = cpu_logical_map(cpu);
  276. uint64_t en1 =
  277. cvmx_read_csr(CVMX_CIU_INTX_EN1
  278. (coreid * 2 + 1));
  279. if (cpumask_test_cpu(cpu, dest))
  280. en1 |= 1ull << bit;
  281. else
  282. en1 &= ~(1ull << bit);
  283. cvmx_write_csr(CVMX_CIU_INTX_EN1(coreid * 2 + 1), en1);
  284. }
  285. /*
  286. * We need to do a read after the last update to make sure all
  287. * of them are done.
  288. */
  289. cvmx_read_csr(CVMX_CIU_INTX_EN1(cvmx_get_core_num() * 2 + 1));
  290. write_unlock(&octeon_irq_ciu1_rwlock);
  291. return 0;
  292. }
  293. #endif
  294. static struct irq_chip octeon_irq_chip_ciu1 = {
  295. .name = "CIU1",
  296. .enable = octeon_irq_ciu1_enable,
  297. .disable = octeon_irq_ciu1_disable,
  298. .ack = octeon_irq_ciu1_ack,
  299. .eoi = octeon_irq_ciu1_eoi,
  300. #ifdef CONFIG_SMP
  301. .set_affinity = octeon_irq_ciu1_set_affinity,
  302. #endif
  303. };
  304. #ifdef CONFIG_PCI_MSI
  305. static void octeon_irq_msi_ack(unsigned int irq)
  306. {
  307. if (!octeon_has_feature(OCTEON_FEATURE_PCIE)) {
  308. /* These chips have PCI */
  309. cvmx_write_csr(CVMX_NPI_NPI_MSI_RCV,
  310. 1ull << (irq - OCTEON_IRQ_MSI_BIT0));
  311. } else {
  312. /*
  313. * These chips have PCIe. Thankfully the ACK doesn't
  314. * need any locking.
  315. */
  316. cvmx_write_csr(CVMX_PEXP_NPEI_MSI_RCV0,
  317. 1ull << (irq - OCTEON_IRQ_MSI_BIT0));
  318. }
  319. }
  320. static void octeon_irq_msi_eoi(unsigned int irq)
  321. {
  322. /* Nothing needed */
  323. }
  324. static void octeon_irq_msi_enable(unsigned int irq)
  325. {
  326. if (!octeon_has_feature(OCTEON_FEATURE_PCIE)) {
  327. /*
  328. * Octeon PCI doesn't have the ability to mask/unmask
  329. * MSI interrupts individually. Instead of
  330. * masking/unmasking them in groups of 16, we simple
  331. * assume MSI devices are well behaved. MSI
  332. * interrupts are always enable and the ACK is assumed
  333. * to be enough.
  334. */
  335. } else {
  336. /* These chips have PCIe. Note that we only support
  337. * the first 64 MSI interrupts. Unfortunately all the
  338. * MSI enables are in the same register. We use
  339. * MSI0's lock to control access to them all.
  340. */
  341. uint64_t en;
  342. unsigned long flags;
  343. spin_lock_irqsave(&octeon_irq_msi_lock, flags);
  344. en = cvmx_read_csr(CVMX_PEXP_NPEI_MSI_ENB0);
  345. en |= 1ull << (irq - OCTEON_IRQ_MSI_BIT0);
  346. cvmx_write_csr(CVMX_PEXP_NPEI_MSI_ENB0, en);
  347. cvmx_read_csr(CVMX_PEXP_NPEI_MSI_ENB0);
  348. spin_unlock_irqrestore(&octeon_irq_msi_lock, flags);
  349. }
  350. }
  351. static void octeon_irq_msi_disable(unsigned int irq)
  352. {
  353. if (!octeon_has_feature(OCTEON_FEATURE_PCIE)) {
  354. /* See comment in enable */
  355. } else {
  356. /*
  357. * These chips have PCIe. Note that we only support
  358. * the first 64 MSI interrupts. Unfortunately all the
  359. * MSI enables are in the same register. We use
  360. * MSI0's lock to control access to them all.
  361. */
  362. uint64_t en;
  363. unsigned long flags;
  364. spin_lock_irqsave(&octeon_irq_msi_lock, flags);
  365. en = cvmx_read_csr(CVMX_PEXP_NPEI_MSI_ENB0);
  366. en &= ~(1ull << (irq - OCTEON_IRQ_MSI_BIT0));
  367. cvmx_write_csr(CVMX_PEXP_NPEI_MSI_ENB0, en);
  368. cvmx_read_csr(CVMX_PEXP_NPEI_MSI_ENB0);
  369. spin_unlock_irqrestore(&octeon_irq_msi_lock, flags);
  370. }
  371. }
  372. static struct irq_chip octeon_irq_chip_msi = {
  373. .name = "MSI",
  374. .enable = octeon_irq_msi_enable,
  375. .disable = octeon_irq_msi_disable,
  376. .ack = octeon_irq_msi_ack,
  377. .eoi = octeon_irq_msi_eoi,
  378. };
  379. #endif
  380. void __init arch_init_irq(void)
  381. {
  382. int irq;
  383. #ifdef CONFIG_SMP
  384. /* Set the default affinity to the boot cpu. */
  385. cpumask_clear(irq_default_affinity);
  386. cpumask_set_cpu(smp_processor_id(), irq_default_affinity);
  387. #endif
  388. if (NR_IRQS < OCTEON_IRQ_LAST)
  389. pr_err("octeon_irq_init: NR_IRQS is set too low\n");
  390. /* 0 - 15 reserved for i8259 master and slave controller. */
  391. /* 17 - 23 Mips internal */
  392. for (irq = OCTEON_IRQ_SW0; irq <= OCTEON_IRQ_TIMER; irq++) {
  393. set_irq_chip_and_handler(irq, &octeon_irq_chip_core,
  394. handle_percpu_irq);
  395. }
  396. /* 24 - 87 CIU_INT_SUM0 */
  397. for (irq = OCTEON_IRQ_WORKQ0; irq <= OCTEON_IRQ_BOOTDMA; irq++) {
  398. set_irq_chip_and_handler(irq, &octeon_irq_chip_ciu0,
  399. handle_percpu_irq);
  400. }
  401. /* 88 - 151 CIU_INT_SUM1 */
  402. for (irq = OCTEON_IRQ_WDOG0; irq <= OCTEON_IRQ_RESERVED151; irq++) {
  403. set_irq_chip_and_handler(irq, &octeon_irq_chip_ciu1,
  404. handle_percpu_irq);
  405. }
  406. #ifdef CONFIG_PCI_MSI
  407. /* 152 - 215 PCI/PCIe MSI interrupts */
  408. for (irq = OCTEON_IRQ_MSI_BIT0; irq <= OCTEON_IRQ_MSI_BIT63; irq++) {
  409. set_irq_chip_and_handler(irq, &octeon_irq_chip_msi,
  410. handle_percpu_irq);
  411. }
  412. #endif
  413. set_c0_status(0x300 << 2);
  414. }
  415. asmlinkage void plat_irq_dispatch(void)
  416. {
  417. const unsigned long core_id = cvmx_get_core_num();
  418. const uint64_t ciu_sum0_address = CVMX_CIU_INTX_SUM0(core_id * 2);
  419. const uint64_t ciu_en0_address = CVMX_CIU_INTX_EN0(core_id * 2);
  420. const uint64_t ciu_sum1_address = CVMX_CIU_INT_SUM1;
  421. const uint64_t ciu_en1_address = CVMX_CIU_INTX_EN1(core_id * 2 + 1);
  422. unsigned long cop0_cause;
  423. unsigned long cop0_status;
  424. uint64_t ciu_en;
  425. uint64_t ciu_sum;
  426. while (1) {
  427. cop0_cause = read_c0_cause();
  428. cop0_status = read_c0_status();
  429. cop0_cause &= cop0_status;
  430. cop0_cause &= ST0_IM;
  431. if (unlikely(cop0_cause & STATUSF_IP2)) {
  432. ciu_sum = cvmx_read_csr(ciu_sum0_address);
  433. ciu_en = cvmx_read_csr(ciu_en0_address);
  434. ciu_sum &= ciu_en;
  435. if (likely(ciu_sum))
  436. do_IRQ(fls64(ciu_sum) + OCTEON_IRQ_WORKQ0 - 1);
  437. else
  438. spurious_interrupt();
  439. } else if (unlikely(cop0_cause & STATUSF_IP3)) {
  440. ciu_sum = cvmx_read_csr(ciu_sum1_address);
  441. ciu_en = cvmx_read_csr(ciu_en1_address);
  442. ciu_sum &= ciu_en;
  443. if (likely(ciu_sum))
  444. do_IRQ(fls64(ciu_sum) + OCTEON_IRQ_WDOG0 - 1);
  445. else
  446. spurious_interrupt();
  447. } else if (likely(cop0_cause)) {
  448. do_IRQ(fls(cop0_cause) - 9 + MIPS_CPU_IRQ_BASE);
  449. } else {
  450. break;
  451. }
  452. }
  453. }