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