xref: /freebsd/sys/powerpc/ps3/ps3pic.c (revision 02e9120893770924227138ba49df1edb3896112a)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
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 
28 #include <sys/param.h>
29 #include <sys/systm.h>
30 #include <sys/module.h>
31 #include <sys/bus.h>
32 #include <sys/conf.h>
33 #include <sys/kernel.h>
34 #include <sys/malloc.h>
35 
36 #include <vm/vm.h>
37 #include <vm/pmap.h>
38 
39 #include <machine/bus.h>
40 #include <machine/intr_machdep.h>
41 #include <machine/md_var.h>
42 #include <machine/platform.h>
43 
44 #include "ps3-hvcall.h"
45 #include "pic_if.h"
46 
47 static void	ps3pic_identify(driver_t *driver, device_t parent);
48 static int	ps3pic_probe(device_t);
49 static int	ps3pic_attach(device_t);
50 
51 static void	ps3pic_dispatch(device_t, struct trapframe *);
52 static void	ps3pic_enable(device_t, u_int, u_int, void **);
53 static void	ps3pic_eoi(device_t, u_int, void *);
54 static void	ps3pic_ipi(device_t, u_int);
55 static void	ps3pic_mask(device_t, u_int, void *);
56 static void	ps3pic_unmask(device_t, u_int, void *);
57 
58 struct ps3pic_softc {
59 	volatile uint64_t *bitmap_thread0;
60 	volatile uint64_t *mask_thread0;
61 	volatile uint64_t *bitmap_thread1;
62 	volatile uint64_t *mask_thread1;
63 
64 	uint64_t	sc_ipi_outlet[2];
65 	uint64_t	sc_ipi_virq;
66 	int		sc_vector[64];
67 };
68 
69 static device_method_t  ps3pic_methods[] = {
70 	/* Device interface */
71 	DEVMETHOD(device_identify,	ps3pic_identify),
72 	DEVMETHOD(device_probe,		ps3pic_probe),
73 	DEVMETHOD(device_attach,	ps3pic_attach),
74 
75 	/* PIC interface */
76 	DEVMETHOD(pic_dispatch,		ps3pic_dispatch),
77 	DEVMETHOD(pic_enable,		ps3pic_enable),
78 	DEVMETHOD(pic_eoi,		ps3pic_eoi),
79 	DEVMETHOD(pic_ipi,		ps3pic_ipi),
80 	DEVMETHOD(pic_mask,		ps3pic_mask),
81 	DEVMETHOD(pic_unmask,		ps3pic_unmask),
82 
83 	{ 0, 0 },
84 };
85 
86 static driver_t ps3pic_driver = {
87 	"ps3pic",
88 	ps3pic_methods,
89 	sizeof(struct ps3pic_softc)
90 };
91 
92 DRIVER_MODULE(ps3pic, nexus, ps3pic_driver, 0, 0);
93 
94 static MALLOC_DEFINE(M_PS3PIC, "ps3pic", "PS3 PIC");
95 
96 static void
97 ps3pic_identify(driver_t *driver, device_t parent)
98 {
99 	if (strcmp(installed_platform(), "ps3") != 0)
100 		return;
101 
102 	if (device_find_child(parent, "ps3pic", -1) == NULL)
103 		BUS_ADD_CHILD(parent, 0, "ps3pic", 0);
104 }
105 
106 static int
107 ps3pic_probe(device_t dev)
108 {
109 	device_set_desc(dev, "Playstation 3 interrupt controller");
110 	return (BUS_PROBE_NOWILDCARD);
111 }
112 
113 static int
114 ps3pic_attach(device_t dev)
115 {
116 	struct ps3pic_softc *sc;
117 	uint64_t ppe;
118 	int thread;
119 
120 	sc = device_get_softc(dev);
121 
122 	sc->bitmap_thread0 = contigmalloc(128 /* 512 bits * 2 */, M_PS3PIC,
123 	    M_NOWAIT | M_ZERO, 0, BUS_SPACE_MAXADDR, 64 /* alignment */,
124 	    PAGE_SIZE /* boundary */);
125 	sc->mask_thread0 = sc->bitmap_thread0 + 4;
126 	sc->bitmap_thread1 = sc->bitmap_thread0 + 8;
127 	sc->mask_thread1 = sc->bitmap_thread0 + 12;
128 
129 	lv1_get_logical_ppe_id(&ppe);
130 	thread = 32 - fls(mfctrl());
131 	lv1_configure_irq_state_bitmap(ppe, thread,
132 	    vtophys(sc->bitmap_thread0));
133 
134 	sc->sc_ipi_virq = 63;
135 
136 #ifdef SMP
137 	lv1_configure_irq_state_bitmap(ppe, !thread,
138 	    vtophys(sc->bitmap_thread1));
139 
140 	/* Map both IPIs to the same VIRQ to avoid changes in intr_machdep */
141 	lv1_construct_event_receive_port(&sc->sc_ipi_outlet[0]);
142 	lv1_connect_irq_plug_ext(ppe, thread, sc->sc_ipi_virq,
143 	    sc->sc_ipi_outlet[0], 0);
144 	lv1_construct_event_receive_port(&sc->sc_ipi_outlet[1]);
145 	lv1_connect_irq_plug_ext(ppe, !thread, sc->sc_ipi_virq,
146 	    sc->sc_ipi_outlet[1], 0);
147 #endif
148 
149 	powerpc_register_pic(dev, 0, sc->sc_ipi_virq, 1, FALSE);
150 	return (0);
151 }
152 
153 /*
154  * PIC I/F methods.
155  */
156 
157 static void
158 ps3pic_dispatch(device_t dev, struct trapframe *tf)
159 {
160 	uint64_t bitmap, mask;
161 	int irq;
162 	struct ps3pic_softc *sc;
163 
164 	sc = device_get_softc(dev);
165 
166 	if (PCPU_GET(cpuid) == 0) {
167 		bitmap = atomic_readandclear_64(&sc->bitmap_thread0[0]);
168 		mask = sc->mask_thread0[0];
169 	} else {
170 		bitmap = atomic_readandclear_64(&sc->bitmap_thread1[0]);
171 		mask = sc->mask_thread1[0];
172 	}
173 	powerpc_sync();
174 
175 	while ((irq = ffsl(bitmap & mask) - 1) != -1) {
176 		bitmap &= ~(1UL << irq);
177 		powerpc_dispatch_intr(sc->sc_vector[63 - irq], tf);
178 	}
179 }
180 
181 static void
182 ps3pic_enable(device_t dev, u_int irq, u_int vector, void **priv)
183 {
184 	struct ps3pic_softc *sc;
185 
186 	sc = device_get_softc(dev);
187 	sc->sc_vector[irq] = vector;
188 
189 	ps3pic_unmask(dev, irq, priv);
190 }
191 
192 static void
193 ps3pic_eoi(device_t dev, u_int irq, void *priv)
194 {
195 	uint64_t ppe;
196 	int thread;
197 
198 	lv1_get_logical_ppe_id(&ppe);
199 	thread = 32 - fls(mfctrl());
200 
201 	lv1_end_of_interrupt_ext(ppe, thread, irq);
202 }
203 
204 static void
205 ps3pic_ipi(device_t dev, u_int cpu)
206 {
207 	struct ps3pic_softc *sc;
208 	sc = device_get_softc(dev);
209 
210 	lv1_send_event_locally(sc->sc_ipi_outlet[cpu]);
211 }
212 
213 static void
214 ps3pic_mask(device_t dev, u_int irq, void *priv)
215 {
216 	struct ps3pic_softc *sc;
217 	uint64_t ppe;
218 
219 	sc = device_get_softc(dev);
220 
221 	/* Do not mask IPIs! */
222 	if (irq == sc->sc_ipi_virq)
223 		return;
224 
225 	atomic_clear_64(&sc->mask_thread0[0], 1UL << (63 - irq));
226 	atomic_clear_64(&sc->mask_thread1[0], 1UL << (63 - irq));
227 
228 	lv1_get_logical_ppe_id(&ppe);
229 	lv1_did_update_interrupt_mask(ppe, 0);
230 	lv1_did_update_interrupt_mask(ppe, 1);
231 }
232 
233 static void
234 ps3pic_unmask(device_t dev, u_int irq, void *priv)
235 {
236 	struct ps3pic_softc *sc;
237 	uint64_t ppe;
238 
239 	sc = device_get_softc(dev);
240 	atomic_set_64(&sc->mask_thread0[0], 1UL << (63 - irq));
241 	atomic_set_64(&sc->mask_thread1[0], 1UL << (63 - irq));
242 
243 	lv1_get_logical_ppe_id(&ppe);
244 	lv1_did_update_interrupt_mask(ppe, 0);
245 	lv1_did_update_interrupt_mask(ppe, 1);
246 }
247