ps3pic.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*-
  2. * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
  3. *
  4. * Copyright 2010 Nathan Whitehorn
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  16. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  17. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  18. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  19. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  20. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  21. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  22. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  24. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  25. * SUCH DAMAGE.
  26. *
  27. * $FreeBSD$
  28. */
  29. #include <sys/param.h>
  30. #include <sys/systm.h>
  31. #include <sys/module.h>
  32. #include <sys/bus.h>
  33. #include <sys/conf.h>
  34. #include <sys/kernel.h>
  35. #include <sys/malloc.h>
  36. #include <vm/vm.h>
  37. #include <vm/pmap.h>
  38. #include <machine/bus.h>
  39. #include <machine/intr_machdep.h>
  40. #include <machine/md_var.h>
  41. #include <machine/platform.h>
  42. #include "ps3-hvcall.h"
  43. #include "pic_if.h"
  44. static void ps3pic_identify(driver_t *driver, device_t parent);
  45. static int ps3pic_probe(device_t);
  46. static int ps3pic_attach(device_t);
  47. static void ps3pic_dispatch(device_t, struct trapframe *);
  48. static void ps3pic_enable(device_t, u_int, u_int, void **);
  49. static void ps3pic_eoi(device_t, u_int, void *);
  50. static void ps3pic_ipi(device_t, u_int);
  51. static void ps3pic_mask(device_t, u_int, void *);
  52. static void ps3pic_unmask(device_t, u_int, void *);
  53. struct ps3pic_softc {
  54. volatile uint64_t *bitmap_thread0;
  55. volatile uint64_t *mask_thread0;
  56. volatile uint64_t *bitmap_thread1;
  57. volatile uint64_t *mask_thread1;
  58. uint64_t sc_ipi_outlet[2];
  59. uint64_t sc_ipi_virq;
  60. int sc_vector[64];
  61. };
  62. static device_method_t ps3pic_methods[] = {
  63. /* Device interface */
  64. DEVMETHOD(device_identify, ps3pic_identify),
  65. DEVMETHOD(device_probe, ps3pic_probe),
  66. DEVMETHOD(device_attach, ps3pic_attach),
  67. /* PIC interface */
  68. DEVMETHOD(pic_dispatch, ps3pic_dispatch),
  69. DEVMETHOD(pic_enable, ps3pic_enable),
  70. DEVMETHOD(pic_eoi, ps3pic_eoi),
  71. DEVMETHOD(pic_ipi, ps3pic_ipi),
  72. DEVMETHOD(pic_mask, ps3pic_mask),
  73. DEVMETHOD(pic_unmask, ps3pic_unmask),
  74. { 0, 0 },
  75. };
  76. static driver_t ps3pic_driver = {
  77. "ps3pic",
  78. ps3pic_methods,
  79. sizeof(struct ps3pic_softc)
  80. };
  81. static devclass_t ps3pic_devclass;
  82. DRIVER_MODULE(ps3pic, nexus, ps3pic_driver, ps3pic_devclass, 0, 0);
  83. static MALLOC_DEFINE(M_PS3PIC, "ps3pic", "PS3 PIC");
  84. static void
  85. ps3pic_identify(driver_t *driver, device_t parent)
  86. {
  87. if (strcmp(installed_platform(), "ps3") != 0)
  88. return;
  89. if (device_find_child(parent, "ps3pic", -1) == NULL)
  90. BUS_ADD_CHILD(parent, 0, "ps3pic", 0);
  91. }
  92. static int
  93. ps3pic_probe(device_t dev)
  94. {
  95. device_set_desc(dev, "Playstation 3 interrupt controller");
  96. return (BUS_PROBE_NOWILDCARD);
  97. }
  98. static int
  99. ps3pic_attach(device_t dev)
  100. {
  101. struct ps3pic_softc *sc;
  102. uint64_t ppe;
  103. int thread;
  104. sc = device_get_softc(dev);
  105. sc->bitmap_thread0 = contigmalloc(128 /* 512 bits * 2 */, M_PS3PIC,
  106. M_NOWAIT | M_ZERO, 0, BUS_SPACE_MAXADDR, 64 /* alignment */,
  107. PAGE_SIZE /* boundary */);
  108. sc->mask_thread0 = sc->bitmap_thread0 + 4;
  109. sc->bitmap_thread1 = sc->bitmap_thread0 + 8;
  110. sc->mask_thread1 = sc->bitmap_thread0 + 12;
  111. lv1_get_logical_ppe_id(&ppe);
  112. thread = 32 - fls(mfctrl());
  113. lv1_configure_irq_state_bitmap(ppe, thread,
  114. vtophys(sc->bitmap_thread0));
  115. sc->sc_ipi_virq = 63;
  116. #ifdef SMP
  117. lv1_configure_irq_state_bitmap(ppe, !thread,
  118. vtophys(sc->bitmap_thread1));
  119. /* Map both IPIs to the same VIRQ to avoid changes in intr_machdep */
  120. lv1_construct_event_receive_port(&sc->sc_ipi_outlet[0]);
  121. lv1_connect_irq_plug_ext(ppe, thread, sc->sc_ipi_virq,
  122. sc->sc_ipi_outlet[0], 0);
  123. lv1_construct_event_receive_port(&sc->sc_ipi_outlet[1]);
  124. lv1_connect_irq_plug_ext(ppe, !thread, sc->sc_ipi_virq,
  125. sc->sc_ipi_outlet[1], 0);
  126. #endif
  127. powerpc_register_pic(dev, 0, sc->sc_ipi_virq, 1, FALSE);
  128. return (0);
  129. }
  130. /*
  131. * PIC I/F methods.
  132. */
  133. static void
  134. ps3pic_dispatch(device_t dev, struct trapframe *tf)
  135. {
  136. uint64_t bitmap, mask;
  137. int irq;
  138. struct ps3pic_softc *sc;
  139. sc = device_get_softc(dev);
  140. if (PCPU_GET(cpuid) == 0) {
  141. bitmap = atomic_readandclear_64(&sc->bitmap_thread0[0]);
  142. mask = sc->mask_thread0[0];
  143. } else {
  144. bitmap = atomic_readandclear_64(&sc->bitmap_thread1[0]);
  145. mask = sc->mask_thread1[0];
  146. }
  147. powerpc_sync();
  148. while ((irq = ffsl(bitmap & mask) - 1) != -1) {
  149. bitmap &= ~(1UL << irq);
  150. powerpc_dispatch_intr(sc->sc_vector[63 - irq], tf);
  151. }
  152. }
  153. static void
  154. ps3pic_enable(device_t dev, u_int irq, u_int vector, void **priv)
  155. {
  156. struct ps3pic_softc *sc;
  157. sc = device_get_softc(dev);
  158. sc->sc_vector[irq] = vector;
  159. ps3pic_unmask(dev, irq, priv);
  160. }
  161. static void
  162. ps3pic_eoi(device_t dev, u_int irq, void *priv)
  163. {
  164. uint64_t ppe;
  165. int thread;
  166. lv1_get_logical_ppe_id(&ppe);
  167. thread = 32 - fls(mfctrl());
  168. lv1_end_of_interrupt_ext(ppe, thread, irq);
  169. }
  170. static void
  171. ps3pic_ipi(device_t dev, u_int cpu)
  172. {
  173. struct ps3pic_softc *sc;
  174. sc = device_get_softc(dev);
  175. lv1_send_event_locally(sc->sc_ipi_outlet[cpu]);
  176. }
  177. static void
  178. ps3pic_mask(device_t dev, u_int irq, void *priv)
  179. {
  180. struct ps3pic_softc *sc;
  181. uint64_t ppe;
  182. sc = device_get_softc(dev);
  183. /* Do not mask IPIs! */
  184. if (irq == sc->sc_ipi_virq)
  185. return;
  186. atomic_clear_64(&sc->mask_thread0[0], 1UL << (63 - irq));
  187. atomic_clear_64(&sc->mask_thread1[0], 1UL << (63 - irq));
  188. lv1_get_logical_ppe_id(&ppe);
  189. lv1_did_update_interrupt_mask(ppe, 0);
  190. lv1_did_update_interrupt_mask(ppe, 1);
  191. }
  192. static void
  193. ps3pic_unmask(device_t dev, u_int irq, void *priv)
  194. {
  195. struct ps3pic_softc *sc;
  196. uint64_t ppe;
  197. sc = device_get_softc(dev);
  198. atomic_set_64(&sc->mask_thread0[0], 1UL << (63 - irq));
  199. atomic_set_64(&sc->mask_thread1[0], 1UL << (63 - irq));
  200. lv1_get_logical_ppe_id(&ppe);
  201. lv1_did_update_interrupt_mask(ppe, 0);
  202. lv1_did_update_interrupt_mask(ppe, 1);
  203. }