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