pic_distopenpic.c revision 1.12 1 /* $NetBSD: pic_distopenpic.c,v 1.12 2020/07/06 09:34:18 rin Exp $ */
2
3 /*-
4 * Copyright (c) 2008 Tim Rightnour
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 * 3. Neither the name of The NetBSD Foundation nor the names of its
16 * contributors may be used to endorse or promote products derived
17 * from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: pic_distopenpic.c,v 1.12 2020/07/06 09:34:18 rin Exp $");
34
35 #ifdef _KERNEL_OPT
36 #include "opt_interrupt.h"
37 #include "opt_openpic.h"
38 #endif
39
40 #include <sys/param.h>
41 #include <sys/kernel.h>
42 #include <sys/kmem.h>
43
44 #include <uvm/uvm_extern.h>
45
46 #include <machine/pio.h>
47 #include <powerpc/openpic.h>
48
49 #include <powerpc/pic/picvar.h>
50
51 /* distributed stuff */
52 static int opic_isu_from_irq(struct openpic_ops *, int, int *);
53 static u_int distopic_read(struct openpic_ops *, int, int);
54 static void distopic_write(struct openpic_ops *, int, int, u_int);
55 static void distopic_establish_irq(struct pic_ops *, int, int, int);
56 static void distopic_enable_irq(struct pic_ops *, int, int);
57 static void distopic_disable_irq(struct pic_ops *, int);
58 static void distopic_finish_setup(struct pic_ops *);
59
60 struct pic_ops *
61 setup_distributed_openpic(void *addr, int nrofisus, void **isu, int *maps)
62 {
63 struct openpic_ops *opicops;
64 struct pic_ops *pic;
65 int irq, i;
66 u_int x;
67
68 openpic_base = (void *)addr;
69 opicops = kmem_alloc(sizeof(*opicops), KM_SLEEP);
70 pic = &opicops->pic;
71
72 x = openpic_read(OPENPIC_FEATURE);
73 if (((x & 0x07ff0000) >> 16) != 0)
74 panic("Can't handle a distributed openpic with internal ISU");
75
76 opicops->nrofisus = nrofisus;
77 opicops->isu = kmem_alloc(sizeof(volatile u_char *) * nrofisus,
78 KM_SLEEP);
79 opicops->irq_per = kmem_alloc(sizeof(uint8_t) * nrofisus, KM_SLEEP);
80
81 for (irq=0, i=0; i < nrofisus ; i++) {
82 opicops->isu[i] = (void *)isu[i];
83 opicops->irq_per[i] = maps[i]/0x20;
84 irq += maps[i]/0x20;
85 aprint_debug("%d: irqtotal=%d, added %d\n", i, irq,
86 maps[i]/0x20);
87 }
88 aprint_normal("OpenPIC Version 1.%d: "
89 "Supports %d CPUs and %d interrupt sources.\n",
90 x & 0xff, ((x & 0x1f00) >> 8) + 1, irq);
91 pic->pic_numintrs = irq;
92 pic->pic_cookie = addr;
93 pic->pic_enable_irq = distopic_enable_irq;
94 pic->pic_reenable_irq = distopic_enable_irq;
95 pic->pic_disable_irq = distopic_disable_irq;
96 pic->pic_get_irq = opic_get_irq;
97 pic->pic_ack_irq = opic_ack_irq;
98 pic->pic_establish_irq = distopic_establish_irq;
99 pic->pic_finish_setup = distopic_finish_setup;
100 opicops->flags = OPENPIC_FLAG_DIST;
101 strcpy(pic->pic_name, "openpic");
102 pic_add(pic);
103
104 openpic_set_priority(0, 15);
105
106 for (i=0; i < nrofisus; i++) {
107 for (irq = 0; irq < opicops->irq_per[i]; irq++) {
108 /* make sure to keep disabled */
109 distopic_write(opicops, i,
110 OPENPIC_DSRC_VECTOR_OFFSET(irq), OPENPIC_IMASK);
111 /* send all interrupts to CPU 0 */
112 distopic_write(opicops, i,
113 OPENPIC_DSRC_IDEST_OFFSET(irq), 1 << 0);
114 }
115 }
116
117 x = openpic_read(OPENPIC_CONFIG);
118 x |= OPENPIC_CONFIG_8259_PASSTHRU_DISABLE;
119 openpic_write(OPENPIC_CONFIG, x);
120
121 openpic_write(OPENPIC_SPURIOUS_VECTOR, 0xff);
122
123 openpic_set_priority(0, 0);
124
125 /* clear all pending interrunts */
126 for (irq = 0; irq < pic->pic_numintrs; irq++) {
127 openpic_read_irq(0);
128 openpic_eoi(0);
129 }
130
131 #if 0
132 printf("timebase freq=%d\n", openpic_read(0x10f0));
133 #endif
134 return pic;
135
136 }
137
138 /* Begin distributed openpic code */
139
140 static int
141 opic_isu_from_irq(struct openpic_ops *opic, int irq, int *realirq)
142 {
143 int i;
144
145 for (i=0; i < opic->nrofisus; i++) {
146 if (irq < opic->irq_per[i]) {
147 *realirq = irq;
148 return i;
149 } else
150 irq -= opic->irq_per[i];
151 }
152 return -1;
153 }
154
155 static u_int
156 distopic_read(struct openpic_ops *opic, int isu, int offset)
157 {
158 volatile unsigned char *addr = opic->isu[isu] + offset;
159
160 return in32rb(addr);
161 }
162
163 static void
164 distopic_write(struct openpic_ops *opic, int isu, int offset, u_int val)
165 {
166 volatile unsigned char *addr = opic->isu[isu] + offset;
167
168 out32rb(addr, val);
169 }
170
171 static void
172 distopic_establish_irq(struct pic_ops *pic, int irq, int type, int pri)
173 {
174 struct openpic_ops *opic = (struct openpic_ops *)pic;
175 int isu, realirq = -1, realpri = uimax(1, uimin(15, pri));
176 uint32_t x;
177
178 isu = opic_isu_from_irq(opic, irq, &realirq);
179 KASSERT(isu != -1);
180
181 x = irq;
182 x |= OPENPIC_IMASK;
183
184 if ((realirq == 0 && isu == 0) ||
185 type == IST_EDGE_RISING || type == IST_LEVEL_HIGH)
186 x |= OPENPIC_POLARITY_POSITIVE;
187 else
188 x |= OPENPIC_POLARITY_NEGATIVE;
189
190 if (type == IST_EDGE_FALLING || type == IST_EDGE_RISING)
191 x |= OPENPIC_SENSE_EDGE;
192 else
193 x |= OPENPIC_SENSE_LEVEL;
194
195 x |= realpri << OPENPIC_PRIORITY_SHIFT;
196 distopic_write(opic, isu, OPENPIC_DSRC_VECTOR_OFFSET(realirq), x);
197
198 aprint_debug("%s: setting IRQ %d to priority %d 0x%x\n", __func__,
199 irq, realpri, x);
200 }
201
202 static void
203 distopic_enable_irq(struct pic_ops *pic, int irq, int type)
204 {
205 struct openpic_ops *opic = (struct openpic_ops *)pic;
206 int isu, realirq = -1;
207 u_int x;
208
209 isu = opic_isu_from_irq(opic, irq, &realirq);
210 KASSERT(isu != -1);
211 x = distopic_read(opic, isu, OPENPIC_DSRC_VECTOR_OFFSET(realirq));
212 x &= ~OPENPIC_IMASK;
213 distopic_write(opic, isu, OPENPIC_DSRC_VECTOR_OFFSET(realirq), x);
214 }
215
216 static void
217 distopic_disable_irq(struct pic_ops *pic, int irq)
218 {
219 struct openpic_ops *opic = (struct openpic_ops *)pic;
220 int isu, realirq = -1;
221 u_int x;
222
223 isu = opic_isu_from_irq(opic, irq, &realirq);
224 KASSERT(isu != -1);
225 x = distopic_read(opic, isu, OPENPIC_DSRC_VECTOR_OFFSET(realirq));
226 x |= OPENPIC_IMASK;
227 distopic_write(opic, isu, OPENPIC_DSRC_VECTOR_OFFSET(realirq), x);
228 }
229
230 static void
231 distopic_finish_setup(struct pic_ops *pic)
232 {
233 struct openpic_ops *opic = (struct openpic_ops *)pic;
234 uint32_t cpumask = 0;
235 int i, irq;
236
237 #ifdef OPENPIC_DISTRIBUTE
238 for (i = 0; i < ncpu; i++)
239 cpumask |= (1 << cpu_info[i].ci_index);
240 #else
241 cpumask = 1;
242 #endif
243 for (i=0; i < opic->nrofisus; i++) {
244 for (irq = 0; irq < opic->irq_per[i]; irq++) {
245 distopic_write(opic, i, OPENPIC_DSRC_IDEST_OFFSET(irq),
246 cpumask);
247 }
248 }
249 }
250