openpic.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. /*-
  2. * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
  3. *
  4. * Copyright (C) 2002 Benno Rice.
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY Benno Rice ``AS IS'' AND ANY EXPRESS OR
  17. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  18. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  19. * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. *
  27. * $FreeBSD$
  28. */
  29. #include <sys/param.h>
  30. #include <sys/systm.h>
  31. #include <sys/bus.h>
  32. #include <sys/conf.h>
  33. #include <sys/kernel.h>
  34. #include <sys/ktr.h>
  35. #include <sys/proc.h>
  36. #include <sys/rman.h>
  37. #include <sys/sched.h>
  38. #include <sys/smp.h>
  39. #include <machine/bus.h>
  40. #include <machine/intr_machdep.h>
  41. #include <machine/md_var.h>
  42. #include <machine/pio.h>
  43. #include <machine/resource.h>
  44. #include <vm/vm.h>
  45. #include <vm/pmap.h>
  46. #include <machine/openpicreg.h>
  47. #include <machine/openpicvar.h>
  48. #include "pic_if.h"
  49. #define OPENPIC_NIPIS 4
  50. devclass_t openpic_devclass;
  51. /*
  52. * Local routines
  53. */
  54. static int openpic_intr(void *arg);
  55. static __inline uint32_t
  56. openpic_read(struct openpic_softc *sc, u_int reg)
  57. {
  58. return (bus_space_read_4(sc->sc_bt, sc->sc_bh, reg));
  59. }
  60. static __inline void
  61. openpic_write(struct openpic_softc *sc, u_int reg, uint32_t val)
  62. {
  63. bus_space_write_4(sc->sc_bt, sc->sc_bh, reg, val);
  64. }
  65. int
  66. openpic_common_attach(device_t dev, uint32_t node)
  67. {
  68. struct openpic_softc *sc;
  69. u_int cpu, ipi, irq;
  70. u_int32_t x;
  71. sc = device_get_softc(dev);
  72. sc->sc_dev = dev;
  73. sc->sc_rid = 0;
  74. sc->sc_memr = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->sc_rid,
  75. RF_ACTIVE);
  76. if (sc->sc_memr == NULL) {
  77. device_printf(dev, "Could not alloc mem resource!\n");
  78. return (ENXIO);
  79. }
  80. sc->sc_bt = rman_get_bustag(sc->sc_memr);
  81. sc->sc_bh = rman_get_bushandle(sc->sc_memr);
  82. /* Reset the PIC */
  83. x = openpic_read(sc, OPENPIC_CONFIG);
  84. x |= OPENPIC_CONFIG_RESET;
  85. openpic_write(sc, OPENPIC_CONFIG, x);
  86. while (openpic_read(sc, OPENPIC_CONFIG) & OPENPIC_CONFIG_RESET) {
  87. powerpc_sync();
  88. DELAY(100);
  89. }
  90. /* Check if this is a cascaded PIC */
  91. sc->sc_irq = 0;
  92. sc->sc_intr = NULL;
  93. do {
  94. struct resource_list *rl;
  95. rl = BUS_GET_RESOURCE_LIST(device_get_parent(dev), dev);
  96. if (rl == NULL)
  97. break;
  98. if (resource_list_find(rl, SYS_RES_IRQ, 0) == NULL)
  99. break;
  100. sc->sc_intr = bus_alloc_resource_any(dev, SYS_RES_IRQ,
  101. &sc->sc_irq, RF_ACTIVE);
  102. /* XXX Cascaded PICs pass NULL trapframes! */
  103. bus_setup_intr(dev, sc->sc_intr, INTR_TYPE_MISC | INTR_MPSAFE,
  104. openpic_intr, NULL, dev, &sc->sc_icookie);
  105. } while (0);
  106. /* Reset the PIC */
  107. x = openpic_read(sc, OPENPIC_CONFIG);
  108. x |= OPENPIC_CONFIG_RESET;
  109. openpic_write(sc, OPENPIC_CONFIG, x);
  110. while (openpic_read(sc, OPENPIC_CONFIG) & OPENPIC_CONFIG_RESET) {
  111. powerpc_sync();
  112. DELAY(100);
  113. }
  114. x = openpic_read(sc, OPENPIC_FEATURE);
  115. switch (x & OPENPIC_FEATURE_VERSION_MASK) {
  116. case 1:
  117. sc->sc_version = "1.0";
  118. break;
  119. case 2:
  120. sc->sc_version = "1.2";
  121. break;
  122. case 3:
  123. sc->sc_version = "1.3";
  124. break;
  125. default:
  126. sc->sc_version = "unknown";
  127. break;
  128. }
  129. sc->sc_ncpu = ((x & OPENPIC_FEATURE_LAST_CPU_MASK) >>
  130. OPENPIC_FEATURE_LAST_CPU_SHIFT) + 1;
  131. sc->sc_nirq = ((x & OPENPIC_FEATURE_LAST_IRQ_MASK) >>
  132. OPENPIC_FEATURE_LAST_IRQ_SHIFT) + 1;
  133. /*
  134. * PSIM seems to report 1 too many IRQs and CPUs
  135. */
  136. if (sc->sc_psim) {
  137. sc->sc_nirq--;
  138. sc->sc_ncpu--;
  139. }
  140. if (bootverbose)
  141. device_printf(dev,
  142. "Version %s, supports %d CPUs and %d irqs\n",
  143. sc->sc_version, sc->sc_ncpu, sc->sc_nirq);
  144. /*
  145. * Allow more IRQs than what the PIC says it handles. Some Freescale PICs
  146. * have MSIs that show up above the PIC's self-described 196 IRQs
  147. * (P5020 starts MSI IRQs at 224).
  148. */
  149. if (sc->sc_quirks & OPENPIC_QUIRK_HIDDEN_IRQS)
  150. sc->sc_nirq = OPENPIC_IRQMAX - OPENPIC_NIPIS;
  151. for (cpu = 0; cpu < sc->sc_ncpu; cpu++)
  152. openpic_write(sc, OPENPIC_PCPU_TPR(cpu), 15);
  153. /* Reset and disable all interrupts. */
  154. for (irq = 0; irq < sc->sc_nirq; irq++) {
  155. x = irq; /* irq == vector. */
  156. x |= OPENPIC_IMASK;
  157. x |= OPENPIC_POLARITY_NEGATIVE;
  158. x |= OPENPIC_SENSE_LEVEL;
  159. x |= 8 << OPENPIC_PRIORITY_SHIFT;
  160. openpic_write(sc, OPENPIC_SRC_VECTOR(irq), x);
  161. }
  162. /* Reset and disable all IPIs. */
  163. for (ipi = 0; ipi < OPENPIC_NIPIS; ipi++) {
  164. x = sc->sc_nirq + ipi;
  165. x |= OPENPIC_IMASK;
  166. x |= 15 << OPENPIC_PRIORITY_SHIFT;
  167. openpic_write(sc, OPENPIC_IPI_VECTOR(ipi), x);
  168. }
  169. /* we don't need 8259 passthrough mode */
  170. x = openpic_read(sc, OPENPIC_CONFIG);
  171. x |= OPENPIC_CONFIG_8259_PASSTHRU_DISABLE;
  172. openpic_write(sc, OPENPIC_CONFIG, x);
  173. /* send all interrupts to cpu 0 */
  174. for (irq = 0; irq < sc->sc_nirq; irq++)
  175. openpic_write(sc, OPENPIC_IDEST(irq), 1 << 0);
  176. /* clear all pending interrupts from cpu 0 */
  177. for (irq = 0; irq < sc->sc_nirq; irq++) {
  178. (void)openpic_read(sc, OPENPIC_PCPU_IACK(0));
  179. openpic_write(sc, OPENPIC_PCPU_EOI(0), 0);
  180. }
  181. for (cpu = 0; cpu < sc->sc_ncpu; cpu++)
  182. openpic_write(sc, OPENPIC_PCPU_TPR(cpu), 0);
  183. powerpc_register_pic(dev, node, sc->sc_nirq, OPENPIC_NIPIS, FALSE);
  184. /* If this is not a cascaded PIC, it must be the root PIC */
  185. if (sc->sc_intr == NULL)
  186. root_pic = dev;
  187. return (0);
  188. }
  189. /*
  190. * PIC I/F methods
  191. */
  192. void
  193. openpic_bind(device_t dev, u_int irq, cpuset_t cpumask, void **priv __unused)
  194. {
  195. struct openpic_softc *sc;
  196. uint32_t mask;
  197. /* If we aren't directly connected to the CPU, this won't work */
  198. if (dev != root_pic)
  199. return;
  200. sc = device_get_softc(dev);
  201. /*
  202. * XXX: openpic_write() is very special and just needs a 32 bits mask.
  203. * For the moment, just play dirty and get the first half word.
  204. */
  205. mask = cpumask.__bits[0] & 0xffffffff;
  206. if (sc->sc_quirks & OPENPIC_QUIRK_SINGLE_BIND) {
  207. int i = mftb() % CPU_COUNT(&cpumask);
  208. int cpu, ncpu;
  209. ncpu = 0;
  210. CPU_FOREACH(cpu) {
  211. if (!(mask & (1 << cpu)))
  212. continue;
  213. if (ncpu == i)
  214. break;
  215. ncpu++;
  216. }
  217. mask &= (1 << cpu);
  218. }
  219. openpic_write(sc, OPENPIC_IDEST(irq), mask);
  220. }
  221. void
  222. openpic_config(device_t dev, u_int irq, enum intr_trigger trig,
  223. enum intr_polarity pol)
  224. {
  225. struct openpic_softc *sc;
  226. uint32_t x;
  227. sc = device_get_softc(dev);
  228. x = openpic_read(sc, OPENPIC_SRC_VECTOR(irq));
  229. if (pol == INTR_POLARITY_LOW)
  230. x &= ~OPENPIC_POLARITY_POSITIVE;
  231. else
  232. x |= OPENPIC_POLARITY_POSITIVE;
  233. if (trig == INTR_TRIGGER_EDGE)
  234. x &= ~OPENPIC_SENSE_LEVEL;
  235. else
  236. x |= OPENPIC_SENSE_LEVEL;
  237. openpic_write(sc, OPENPIC_SRC_VECTOR(irq), x);
  238. }
  239. static int
  240. openpic_intr(void *arg)
  241. {
  242. device_t dev = (device_t)(arg);
  243. /* XXX Cascaded PICs do not pass non-NULL trapframes! */
  244. openpic_dispatch(dev, NULL);
  245. return (FILTER_HANDLED);
  246. }
  247. void
  248. openpic_dispatch(device_t dev, struct trapframe *tf)
  249. {
  250. struct openpic_softc *sc;
  251. u_int cpuid, vector;
  252. CTR1(KTR_INTR, "%s: got interrupt", __func__);
  253. cpuid = (dev == root_pic) ? PCPU_GET(cpuid) : 0;
  254. sc = device_get_softc(dev);
  255. while (1) {
  256. vector = openpic_read(sc, OPENPIC_PCPU_IACK(cpuid));
  257. vector &= OPENPIC_VECTOR_MASK;
  258. if (vector == 255)
  259. break;
  260. powerpc_dispatch_intr(vector, tf);
  261. }
  262. }
  263. void
  264. openpic_enable(device_t dev, u_int irq, u_int vector, void **priv __unused)
  265. {
  266. struct openpic_softc *sc;
  267. uint32_t x;
  268. sc = device_get_softc(dev);
  269. if (irq < sc->sc_nirq) {
  270. x = openpic_read(sc, OPENPIC_SRC_VECTOR(irq));
  271. x &= ~(OPENPIC_IMASK | OPENPIC_VECTOR_MASK);
  272. x |= vector;
  273. openpic_write(sc, OPENPIC_SRC_VECTOR(irq), x);
  274. } else {
  275. x = openpic_read(sc, OPENPIC_IPI_VECTOR(0));
  276. x &= ~(OPENPIC_IMASK | OPENPIC_VECTOR_MASK);
  277. x |= vector;
  278. openpic_write(sc, OPENPIC_IPI_VECTOR(0), x);
  279. }
  280. }
  281. void
  282. openpic_eoi(device_t dev, u_int irq __unused, void *priv __unused)
  283. {
  284. struct openpic_softc *sc;
  285. u_int cpuid;
  286. cpuid = (dev == root_pic) ? PCPU_GET(cpuid) : 0;
  287. sc = device_get_softc(dev);
  288. openpic_write(sc, OPENPIC_PCPU_EOI(cpuid), 0);
  289. }
  290. void
  291. openpic_ipi(device_t dev, u_int cpu)
  292. {
  293. struct openpic_softc *sc;
  294. KASSERT(dev == root_pic, ("Cannot send IPIs from non-root OpenPIC"));
  295. sc = device_get_softc(dev);
  296. sched_pin();
  297. openpic_write(sc, OPENPIC_PCPU_IPI_DISPATCH(PCPU_GET(cpuid), 0),
  298. 1u << cpu);
  299. sched_unpin();
  300. }
  301. void
  302. openpic_mask(device_t dev, u_int irq, void *priv __unused)
  303. {
  304. struct openpic_softc *sc;
  305. uint32_t x;
  306. sc = device_get_softc(dev);
  307. if (irq < sc->sc_nirq) {
  308. x = openpic_read(sc, OPENPIC_SRC_VECTOR(irq));
  309. x |= OPENPIC_IMASK;
  310. openpic_write(sc, OPENPIC_SRC_VECTOR(irq), x);
  311. } else {
  312. x = openpic_read(sc, OPENPIC_IPI_VECTOR(0));
  313. x |= OPENPIC_IMASK;
  314. openpic_write(sc, OPENPIC_IPI_VECTOR(0), x);
  315. }
  316. }
  317. void
  318. openpic_unmask(device_t dev, u_int irq, void *priv __unused)
  319. {
  320. struct openpic_softc *sc;
  321. uint32_t x;
  322. sc = device_get_softc(dev);
  323. if (irq < sc->sc_nirq) {
  324. x = openpic_read(sc, OPENPIC_SRC_VECTOR(irq));
  325. x &= ~OPENPIC_IMASK;
  326. openpic_write(sc, OPENPIC_SRC_VECTOR(irq), x);
  327. } else {
  328. x = openpic_read(sc, OPENPIC_IPI_VECTOR(0));
  329. x &= ~OPENPIC_IMASK;
  330. openpic_write(sc, OPENPIC_IPI_VECTOR(0), x);
  331. }
  332. }
  333. int
  334. openpic_suspend(device_t dev)
  335. {
  336. struct openpic_softc *sc;
  337. int i;
  338. sc = device_get_softc(dev);
  339. sc->sc_saved_config = bus_read_4(sc->sc_memr, OPENPIC_CONFIG);
  340. for (i = 0; i < OPENPIC_NIPIS; i++) {
  341. sc->sc_saved_ipis[i] = bus_read_4(sc->sc_memr, OPENPIC_IPI_VECTOR(i));
  342. }
  343. for (i = 0; i < 4; i++) {
  344. sc->sc_saved_prios[i] = bus_read_4(sc->sc_memr, OPENPIC_PCPU_TPR(i));
  345. }
  346. for (i = 0; i < OPENPIC_TIMERS; i++) {
  347. sc->sc_saved_timers[i].tcnt = bus_read_4(sc->sc_memr, OPENPIC_TCNT(i));
  348. sc->sc_saved_timers[i].tbase = bus_read_4(sc->sc_memr, OPENPIC_TBASE(i));
  349. sc->sc_saved_timers[i].tvec = bus_read_4(sc->sc_memr, OPENPIC_TVEC(i));
  350. sc->sc_saved_timers[i].tdst = bus_read_4(sc->sc_memr, OPENPIC_TDST(i));
  351. }
  352. for (i = 0; i < OPENPIC_SRC_VECTOR_COUNT; i++)
  353. sc->sc_saved_vectors[i] =
  354. bus_read_4(sc->sc_memr, OPENPIC_SRC_VECTOR(i)) & ~OPENPIC_ACTIVITY;
  355. return (0);
  356. }
  357. int
  358. openpic_resume(device_t dev)
  359. {
  360. struct openpic_softc *sc;
  361. int i;
  362. sc = device_get_softc(dev);
  363. sc->sc_saved_config = bus_read_4(sc->sc_memr, OPENPIC_CONFIG);
  364. for (i = 0; i < OPENPIC_NIPIS; i++) {
  365. bus_write_4(sc->sc_memr, OPENPIC_IPI_VECTOR(i), sc->sc_saved_ipis[i]);
  366. }
  367. for (i = 0; i < 4; i++) {
  368. bus_write_4(sc->sc_memr, OPENPIC_PCPU_TPR(i), sc->sc_saved_prios[i]);
  369. }
  370. for (i = 0; i < OPENPIC_TIMERS; i++) {
  371. bus_write_4(sc->sc_memr, OPENPIC_TCNT(i), sc->sc_saved_timers[i].tcnt);
  372. bus_write_4(sc->sc_memr, OPENPIC_TBASE(i), sc->sc_saved_timers[i].tbase);
  373. bus_write_4(sc->sc_memr, OPENPIC_TVEC(i), sc->sc_saved_timers[i].tvec);
  374. bus_write_4(sc->sc_memr, OPENPIC_TDST(i), sc->sc_saved_timers[i].tdst);
  375. }
  376. for (i = 0; i < OPENPIC_SRC_VECTOR_COUNT; i++)
  377. bus_write_4(sc->sc_memr, OPENPIC_SRC_VECTOR(i), sc->sc_saved_vectors[i]);
  378. return (0);
  379. }