]> CyberLeo.Net >> Repos - FreeBSD/releng/9.0.git/blob - sys/powerpc/ps3/ps3pic.c
Copy stable/9 to releng/9.0 as part of the FreeBSD 9.0-RELEASE release
[FreeBSD/releng/9.0.git] / sys / powerpc / ps3 / ps3pic.c
1 /*-
2  * Copyright 2010 Nathan Whitehorn
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
18  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
20  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
21  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  *
25  * $FreeBSD$
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);
53 static void     ps3pic_eoi(device_t, u_int);
54 static void     ps3pic_ipi(device_t, u_int);
55 static void     ps3pic_mask(device_t, u_int);
56 static void     ps3pic_unmask(device_t, u_int);
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         int             sc_vector[64];
66 };
67
68 static device_method_t  ps3pic_methods[] = {
69         /* Device interface */
70         DEVMETHOD(device_identify,      ps3pic_identify),
71         DEVMETHOD(device_probe,         ps3pic_probe),
72         DEVMETHOD(device_attach,        ps3pic_attach),
73
74         /* PIC interface */
75         DEVMETHOD(pic_dispatch,         ps3pic_dispatch),
76         DEVMETHOD(pic_enable,           ps3pic_enable),
77         DEVMETHOD(pic_eoi,              ps3pic_eoi),
78         DEVMETHOD(pic_ipi,              ps3pic_ipi),
79         DEVMETHOD(pic_mask,             ps3pic_mask),
80         DEVMETHOD(pic_unmask,           ps3pic_unmask),
81
82         { 0, 0 },
83 };
84
85 static driver_t ps3pic_driver = {
86         "ps3pic",
87         ps3pic_methods,
88         sizeof(struct ps3pic_softc)
89 };
90
91 static devclass_t ps3pic_devclass;
92
93 DRIVER_MODULE(ps3pic, nexus, ps3pic_driver, ps3pic_devclass, 0, 0);
94
95 static MALLOC_DEFINE(M_PS3PIC, "ps3pic", "PS3 PIC");
96
97 static void
98 ps3pic_identify(driver_t *driver, device_t parent)
99 {
100         if (strcmp(installed_platform(), "ps3") != 0)
101                 return;
102
103         if (device_find_child(parent, "ps3pic", -1) == NULL)
104                 BUS_ADD_CHILD(parent, 0, "ps3pic", 0);
105 }
106
107 static int
108 ps3pic_probe(device_t dev)
109 {
110         device_set_desc(dev, "Playstation 3 interrupt controller");
111         return (BUS_PROBE_NOWILDCARD);
112 }
113
114 static int
115 ps3pic_attach(device_t dev)
116 {
117         struct ps3pic_softc *sc;
118         uint64_t ppe;
119         int thread;
120
121         sc = device_get_softc(dev);
122
123         sc->bitmap_thread0 = contigmalloc(128 /* 512 bits * 2 */, M_PS3PIC,
124             M_NOWAIT | M_ZERO, 0, BUS_SPACE_MAXADDR, 64 /* alignment */,
125             PAGE_SIZE /* boundary */);
126         sc->mask_thread0 = sc->bitmap_thread0 + 4;
127         sc->bitmap_thread1 = sc->bitmap_thread0 + 8;
128         sc->mask_thread1 = sc->bitmap_thread0 + 12;
129
130         lv1_get_logical_ppe_id(&ppe);
131         thread = 32 - fls(mfctrl());
132         lv1_configure_irq_state_bitmap(ppe, thread,
133             vtophys(sc->bitmap_thread0));
134 #ifdef SMP
135         lv1_configure_irq_state_bitmap(ppe, !thread,
136             vtophys(sc->bitmap_thread1));
137
138         /* Map both IPIs to the same VIRQ to avoid changes in intr_machdep */
139         lv1_construct_event_receive_port(&sc->sc_ipi_outlet[0]);
140         lv1_connect_irq_plug_ext(ppe, thread, sc->sc_ipi_outlet[0],
141             sc->sc_ipi_outlet[0], 0);
142         lv1_construct_event_receive_port(&sc->sc_ipi_outlet[1]);
143         lv1_connect_irq_plug_ext(ppe, !thread, sc->sc_ipi_outlet[0],
144             sc->sc_ipi_outlet[1], 0);
145 #endif
146
147         powerpc_register_pic(dev, 0, sc->sc_ipi_outlet[0], 1, FALSE);
148         return (0);
149 }
150
151 /*
152  * PIC I/F methods.
153  */
154
155 static void
156 ps3pic_dispatch(device_t dev, struct trapframe *tf)
157 {
158         uint64_t bitmap, mask;
159         int irq;
160         struct ps3pic_softc *sc;
161
162         sc = device_get_softc(dev);
163
164         if (PCPU_GET(cpuid) == 0) {
165                 bitmap = sc->bitmap_thread0[0];
166                 mask = sc->mask_thread0[0];
167         } else {
168                 bitmap = sc->bitmap_thread1[0];
169                 mask = sc->mask_thread1[0];
170         }
171
172         while ((irq = ffsl(bitmap & mask) - 1) != -1) {
173                 bitmap &= ~(1UL << irq);
174                 powerpc_dispatch_intr(sc->sc_vector[63 - irq], tf);
175         }
176 }
177
178 static void
179 ps3pic_enable(device_t dev, u_int irq, u_int vector)
180 {
181         struct ps3pic_softc *sc;
182
183         sc = device_get_softc(dev);
184         sc->sc_vector[irq] = vector;
185
186         ps3pic_unmask(dev, irq);
187 }
188
189 static void
190 ps3pic_eoi(device_t dev, u_int irq)
191 {
192         uint64_t ppe;
193         int thread;
194
195         lv1_get_logical_ppe_id(&ppe);
196         thread = 32 - fls(mfctrl());
197
198         lv1_end_of_interrupt_ext(ppe, thread, irq);
199 }
200
201 static void
202 ps3pic_ipi(device_t dev, u_int cpu)
203 {
204         struct ps3pic_softc *sc;
205         sc = device_get_softc(dev);
206
207         lv1_send_event_locally(sc->sc_ipi_outlet[cpu]);
208 }
209
210 static void
211 ps3pic_mask(device_t dev, u_int irq)
212 {
213         struct ps3pic_softc *sc;
214         uint64_t ppe;
215
216         sc = device_get_softc(dev);
217
218         /* Do not mask IPIs! */
219         if (irq == sc->sc_ipi_outlet[0])
220                 return;
221
222         atomic_clear_64(&sc->mask_thread0[0], 1UL << (63 - irq));
223         atomic_clear_64(&sc->mask_thread1[0], 1UL << (63 - irq));
224
225         lv1_get_logical_ppe_id(&ppe);
226         lv1_did_update_interrupt_mask(ppe, 0);
227         lv1_did_update_interrupt_mask(ppe, 1);
228 }
229
230 static void
231 ps3pic_unmask(device_t dev, u_int irq)
232 {
233         struct ps3pic_softc *sc;
234         uint64_t ppe;
235
236         sc = device_get_softc(dev);
237         atomic_set_64(&sc->mask_thread0[0], 1UL << (63 - irq));
238         atomic_set_64(&sc->mask_thread1[0], 1UL << (63 - irq));
239
240         lv1_get_logical_ppe_id(&ppe);
241         lv1_did_update_interrupt_mask(ppe, 0);
242         lv1_did_update_interrupt_mask(ppe, 1);
243 }