Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/*	$NetBSD: pic_uic.c,v 1.9 2021/03/05 05:35:50 rin Exp $	*/

/*
 * Copyright 2002 Wasabi Systems, Inc.
 * All rights reserved.
 *
 * Written by Eduardo Horvath and Simon Burge for Wasabi Systems, Inc.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *      This product includes software developed for the NetBSD Project by
 *      Wasabi Systems, Inc.
 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
 *    or promote products derived from this software without specific prior
 *    written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: pic_uic.c,v 1.9 2021/03/05 05:35:50 rin Exp $");

#ifdef _KERNEL_OPT
#include "opt_ppcarch.h"
#include "opt_uic.h"
#endif

#include <sys/param.h>
#include <sys/kernel.h>
#include <sys/evcnt.h>
#include <sys/cpu.h>

#include <machine/intr.h>
#include <machine/psl.h>

#include <powerpc/spr.h>
#include <powerpc/ibm4xx/spr.h>
#include <powerpc/ibm4xx/cpu.h>

#include <powerpc/pic/picvar.h>

/*
 * Number of interrupts (hard + soft), irq number legality test,
 * mapping of irq number to mask and a way to pick irq number
 * off a mask of active intrs.
 */
#define	IRQ_TO_MASK(irq) 	(0x80000000UL >> ((irq) & 0x1f))
#define	IRQ_OF_MASK(mask) 	__builtin_clz(mask)

static void	uic_enable_irq(struct pic_ops *, int, int);
static void	uic_disable_irq(struct pic_ops *, int);
static int	uic_get_irq(struct pic_ops *, int);
static void	uic_ack_irq(struct pic_ops *, int);
static void	uic_establish_irq(struct pic_ops *, int, int, int);

struct uic {
	uint32_t uic_intr_enable;	/* cached intr enable mask */
#ifdef PPC_IBM403
	/*
	 * Not clearly documented in reference manual, but DCR_EXISR
	 * register is not updated immediately after some bits are
	 * cleared by mtdcr, no matter whether sync (= eieio) and/or
	 * isync are issued.
	 *
	 * Therefore, we have to manage our own status mask in the
	 * interrupt handler; see uic_{ack,get}_irq() for more details.
	 * This is what we did in obsoleted powerpc/ibm4xx/intr.c.
	 */
	uint32_t uic_intr_status;
#endif
	uint32_t (*uic_mf_intr_status)(void);
	uint32_t (*uic_mf_intr_enable)(void);
	void (*uic_mt_intr_enable)(uint32_t);
	void (*uic_mt_intr_ack)(uint32_t);
};

/*
 * Platform specific code may override any of the above.
 */
#ifdef PPC_IBM403

#include <powerpc/ibm4xx/dcr403cgx.h>

static uint32_t
uic403_mfdcr_intr_status(void)
{
	return mfdcr(DCR_EXISR);
}

static uint32_t
uic403_mfdcr_intr_enable(void)
{
	return mfdcr(DCR_EXIER);
}

static void
uic403_mtdcr_intr_ack(uint32_t v)
{
	mtdcr(DCR_EXISR, v);
}

static void
uic403_mtdcr_intr_enable(uint32_t v)
{
	mtdcr(DCR_EXIER, v);
}

struct uic uic403 = {
	.uic_intr_enable =	0,
	.uic_mf_intr_status =	uic403_mfdcr_intr_status,
	.uic_mf_intr_enable =	uic403_mfdcr_intr_enable,
	.uic_mt_intr_enable =	uic403_mtdcr_intr_enable,
	.uic_mt_intr_ack =	uic403_mtdcr_intr_ack,
};

struct pic_ops pic_uic403 = {
	.pic_cookie = &uic403,
	.pic_numintrs = 32,
	.pic_enable_irq = uic_enable_irq,
	.pic_reenable_irq = uic_enable_irq,
	.pic_disable_irq = uic_disable_irq,
	.pic_establish_irq = uic_establish_irq,
	.pic_get_irq = uic_get_irq,
	.pic_ack_irq = uic_ack_irq,
	.pic_finish_setup = NULL,
	.pic_name = "uic0"
};

#else /* Generic 405/440/460 Universal Interrupt Controller */

#include <powerpc/ibm4xx/dcr4xx.h>

#include "opt_uic.h"

/* 405EP/405GP/405GPr/Virtex-4 */

static uint32_t
uic0_mfdcr_intr_status(void)
{
	return mfdcr(DCR_UIC0_BASE + DCR_UIC_MSR);
}

static uint32_t
uic0_mfdcr_intr_enable(void)
{
	return mfdcr(DCR_UIC0_BASE + DCR_UIC_ER);
}

static void
uic0_mtdcr_intr_ack(uint32_t v)
{
	mtdcr(DCR_UIC0_BASE + DCR_UIC_SR, v);
}

static void
uic0_mtdcr_intr_enable(uint32_t v)
{
	mtdcr(DCR_UIC0_BASE + DCR_UIC_ER, v);
}

struct uic uic0 = {
	.uic_intr_enable =	0,
	.uic_mf_intr_status =	uic0_mfdcr_intr_status,
	.uic_mf_intr_enable =	uic0_mfdcr_intr_enable,
	.uic_mt_intr_enable =	uic0_mtdcr_intr_enable,
	.uic_mt_intr_ack =	uic0_mtdcr_intr_ack,
};

struct pic_ops pic_uic0 = {
	.pic_cookie = &uic0,
	.pic_numintrs = 32,
	.pic_enable_irq = uic_enable_irq,
	.pic_reenable_irq = uic_enable_irq,
	.pic_disable_irq = uic_disable_irq,
	.pic_establish_irq = uic_establish_irq,
	.pic_get_irq = uic_get_irq,
	.pic_ack_irq = uic_ack_irq,
	.pic_finish_setup = NULL,
	.pic_name = "uic0"
};

#ifdef MULTIUIC

/* 440EP/440GP/440SP/405EX/440SPe/440GX */

static uint32_t
uic1_mfdcr_intr_status(void)
{
	return mfdcr(DCR_UIC1_BASE + DCR_UIC_MSR);
}

static uint32_t
uic1_mfdcr_intr_enable(void)
{
	return mfdcr(DCR_UIC1_BASE + DCR_UIC_ER);
}

static void
uic1_mtdcr_intr_ack(uint32_t v)
{
	mtdcr(DCR_UIC1_BASE + DCR_UIC_SR, v);
}

static void
uic1_mtdcr_intr_enable(uint32_t v)
{
	mtdcr(DCR_UIC1_BASE + DCR_UIC_ER, v);
}

extern struct pic_ops pic_uic1;

static void
uic1_finish_setup(struct pic_ops *pic)
{
	intr_establish_xname(30, IST_LEVEL, IPL_HIGH, pic_handle_intr,
	    &pic_uic1, "uic1");
}

struct uic uic1 = {
	.uic_intr_enable =	0,
	.uic_mf_intr_status =	uic1_mfdcr_intr_status,
	.uic_mf_intr_enable =	uic1_mfdcr_intr_enable,
	.uic_mt_intr_enable =	uic1_mtdcr_intr_enable,
	.uic_mt_intr_ack =	uic1_mtdcr_intr_ack,
};

struct pic_ops pic_uic1 = {
	.pic_cookie = &uic1,
	.pic_numintrs = 32,
	.pic_enable_irq = uic_enable_irq,
	.pic_reenable_irq = uic_enable_irq,
	.pic_disable_irq = uic_disable_irq,
	.pic_establish_irq = uic_establish_irq,
	.pic_get_irq = uic_get_irq,
	.pic_ack_irq = uic_ack_irq,
	.pic_finish_setup = uic1_finish_setup,
	.pic_name = "uic1"
};

/* 440EP/440GP/440SP/405EX/440SPe */

static uint32_t
uic2_mfdcr_intr_status(void)
{
	return mfdcr(DCR_UIC2_BASE + DCR_UIC_MSR);
}

static uint32_t
uic2_mfdcr_intr_enable(void)
{
	return mfdcr(DCR_UIC2_BASE + DCR_UIC_ER);
}

static void
uic2_mtdcr_intr_ack(uint32_t v)
{
	mtdcr(DCR_UIC2_BASE + DCR_UIC_SR, v);
}

static void
uic2_mtdcr_intr_enable(uint32_t v)
{
	mtdcr(DCR_UIC2_BASE + DCR_UIC_ER, v);
}

extern struct pic_ops pic_uic2;

static void
uic2_finish_setup(struct pic_ops *pic)
{
	intr_establish_xname(28, IST_LEVEL, IPL_HIGH, pic_handle_intr,
	    &pic_uic2, "uic2");
}

static struct uic uic2 = {
	.uic_intr_enable =	0,
	.uic_mf_intr_status =	uic2_mfdcr_intr_status,
	.uic_mf_intr_enable =	uic2_mfdcr_intr_enable,
	.uic_mt_intr_enable =	uic2_mtdcr_intr_enable,
	.uic_mt_intr_ack =	uic2_mtdcr_intr_ack,
};

struct pic_ops pic_uic2 = {
	.pic_cookie = &uic2,
	.pic_numintrs = 32,
	.pic_enable_irq = uic_enable_irq,
	.pic_reenable_irq = uic_enable_irq,
	.pic_disable_irq = uic_disable_irq,
	.pic_establish_irq = uic_establish_irq,
	.pic_get_irq = uic_get_irq,
	.pic_ack_irq = uic_ack_irq,
	.pic_finish_setup = uic2_finish_setup,
	.pic_name = "uic2"
};

#endif /* MULTIUIC */
#endif /* !PPC_IBM403 */

/*
 * Set up interrupt mapping array.
 */
void
intr_init(void)
{
#ifdef PPC_IBM403
	struct pic_ops * const pic = &pic_uic403;
#else
	struct pic_ops * const pic = &pic_uic0;
#endif
	struct uic * const uic = pic->pic_cookie;

	uic->uic_mt_intr_enable(0x00000000); 	/* mask all */
	uic->uic_mt_intr_ack(0xffffffff);	/* acknowledge all */

	pic_add(pic);
}

static void
uic_disable_irq(struct pic_ops *pic, int irq)
{
	struct uic * const uic = pic->pic_cookie;
	const uint32_t irqmask = IRQ_TO_MASK(irq);
	if ((uic->uic_intr_enable & irqmask) == 0)
		return;
	uic->uic_intr_enable ^= irqmask;
	(*uic->uic_mt_intr_enable)(uic->uic_intr_enable);
#ifdef IRQ_DEBUG
	printf("%s: %s: irq=%d, mask=%08x\n", __func__,
	    pic->pic_name, irq, irqmask);
#endif
}

static void
uic_enable_irq(struct pic_ops *pic, int irq, int type)
{
	struct uic * const uic = pic->pic_cookie;
	const uint32_t irqmask = IRQ_TO_MASK(irq);
	if ((uic->uic_intr_enable & irqmask) != 0)
		return;
	uic->uic_intr_enable ^= irqmask;
	(*uic->uic_mt_intr_enable)(uic->uic_intr_enable);
#ifdef IRQ_DEBUG
	printf("%s: %s: irq=%d, mask=%08x\n", __func__,
	    pic->pic_name, irq, irqmask);
#endif
}

static void
uic_ack_irq(struct pic_ops *pic, int irq)
{
	struct uic * const uic = pic->pic_cookie;
	const uint32_t irqmask = IRQ_TO_MASK(irq);

#ifdef PPC_IBM403
	uic->uic_intr_status &= ~irqmask;
#endif

	(*uic->uic_mt_intr_ack)(irqmask);
}

static int
uic_get_irq(struct pic_ops *pic, int req)
{
	struct uic * const uic = pic->pic_cookie;

#ifdef PPC_IBM403
	if (req == PIC_GET_IRQ)
		uic->uic_intr_status = (*uic->uic_mf_intr_status)();
	const uint32_t irqmask = uic->uic_intr_status;
#else
	const uint32_t irqmask = (*uic->uic_mf_intr_status)();
#endif

	if (irqmask == 0)
		return 255;
	return IRQ_OF_MASK(irqmask);
}

/*
 * Register an interrupt handler.
 */
static void
uic_establish_irq(struct pic_ops *pic, int irq, int type, int ipl)
{
}