1 /* $OpenBSD: pci_kn20aa.c,v 1.28 2015/07/26 05:09:44 miod Exp $ */ 2 /* $NetBSD: pci_kn20aa.c,v 1.21 1996/11/17 02:05:27 cgd Exp $ */ 3 4 /* 5 * Copyright (c) 1995, 1996 Carnegie-Mellon University. 6 * All rights reserved. 7 * 8 * Author: Chris G. Demetriou 9 * 10 * Permission to use, copy, modify and distribute this software and 11 * its documentation is hereby granted, provided that both the copyright 12 * notice and this permission notice appear in all copies of the 13 * software, derivative works or modified versions, and any portions 14 * thereof, and that both notices appear in supporting documentation. 15 * 16 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 17 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 18 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 19 * 20 * Carnegie Mellon requests users of this software to return to 21 * 22 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 23 * School of Computer Science 24 * Carnegie Mellon University 25 * Pittsburgh PA 15213-3890 26 * 27 * any improvements or extensions that they make and grant Carnegie the 28 * rights to redistribute these changes. 29 */ 30 31 #include <sys/types.h> 32 #include <sys/param.h> 33 #include <sys/time.h> 34 #include <sys/systm.h> 35 #include <sys/errno.h> 36 #include <sys/malloc.h> 37 #include <sys/device.h> 38 #include <sys/syslog.h> 39 40 #include <uvm/uvm_extern.h> 41 42 #include <machine/autoconf.h> 43 44 #include <dev/pci/pcireg.h> 45 #include <dev/pci/pcivar.h> 46 #include <dev/pci/ppbreg.h> 47 48 #include <alpha/pci/ciareg.h> 49 #include <alpha/pci/ciavar.h> 50 51 #include <alpha/pci/pci_kn20aa.h> 52 53 #include "sio.h" 54 #if NSIO 55 #include <alpha/pci/siovar.h> 56 #endif 57 58 int dec_kn20aa_intr_map(struct pci_attach_args *, pci_intr_handle_t *); 59 const char *dec_kn20aa_intr_string(void *, pci_intr_handle_t); 60 int dec_kn20aa_intr_line(void *, pci_intr_handle_t); 61 void *dec_kn20aa_intr_establish(void *, pci_intr_handle_t, 62 int, int (*func)(void *), void *, const char *); 63 void dec_kn20aa_intr_disestablish(void *, void *); 64 65 #define KN20AA_PCEB_IRQ 31 66 #define KN20AA_MAX_IRQ 32 67 #define PCI_STRAY_MAX 5 68 69 struct alpha_shared_intr *kn20aa_pci_intr; 70 struct evcount kn20aa_intr_count; 71 72 void kn20aa_iointr(void *arg, unsigned long vec); 73 void kn20aa_enable_intr(int irq); 74 void kn20aa_disable_intr(int irq); 75 76 void 77 pci_kn20aa_pickintr(ccp) 78 struct cia_config *ccp; 79 { 80 int i; 81 bus_space_tag_t iot = &ccp->cc_iot; 82 pci_chipset_tag_t pc = &ccp->cc_pc; 83 84 pc->pc_intr_v = ccp; 85 pc->pc_intr_map = dec_kn20aa_intr_map; 86 pc->pc_intr_string = dec_kn20aa_intr_string; 87 pc->pc_intr_line = dec_kn20aa_intr_line; 88 pc->pc_intr_establish = dec_kn20aa_intr_establish; 89 pc->pc_intr_disestablish = dec_kn20aa_intr_disestablish; 90 91 /* Not supported on KN20AA. */ 92 pc->pc_pciide_compat_intr_establish = NULL; 93 pc->pc_pciide_compat_intr_disestablish = NULL; 94 95 kn20aa_pci_intr = alpha_shared_intr_alloc(KN20AA_MAX_IRQ); 96 for (i = 0; i < KN20AA_MAX_IRQ; i++) 97 alpha_shared_intr_set_maxstrays(kn20aa_pci_intr, i, 98 PCI_STRAY_MAX); 99 100 #if NSIO 101 sio_intr_setup(pc, iot); 102 kn20aa_enable_intr(KN20AA_PCEB_IRQ); 103 #endif 104 } 105 106 int 107 dec_kn20aa_intr_map(pa, ihp) 108 struct pci_attach_args *pa; 109 pci_intr_handle_t *ihp; 110 { 111 pcitag_t bustag = pa->pa_intrtag; 112 int buspin = pa->pa_intrpin; 113 pci_chipset_tag_t pc = pa->pa_pc; 114 int device; 115 int kn20aa_irq; 116 117 if (pa->pa_bridgetag) { 118 buspin = PPB_INTERRUPT_SWIZZLE(pa->pa_rawintrpin, 119 pa->pa_device); 120 if (pa->pa_bridgeih[buspin - 1] != 0) { 121 *ihp = pa->pa_bridgeih[buspin - 1]; 122 return 0; 123 } 124 125 return 1; 126 } 127 128 /* 129 * Slot->interrupt translation. Appears to work, though it 130 * may not hold up forever. 131 * 132 * The DEC engineers who did this hardware obviously engaged 133 * in random drug testing. 134 */ 135 pci_decompose_tag(pc, bustag, NULL, &device, NULL); 136 switch (device) { 137 case 11: 138 case 12: 139 kn20aa_irq = ((device - 11) + 0) * 4; 140 break; 141 142 case 7: 143 kn20aa_irq = 8; 144 break; 145 146 case 9: 147 kn20aa_irq = 12; 148 break; 149 150 case 6: /* 21040 on AlphaStation 500 */ 151 kn20aa_irq = 13; 152 break; 153 154 case 8: 155 kn20aa_irq = 16; 156 break; 157 158 default: 159 printf("dec_kn20aa_intr_map: don't know how to setup %d/%d/%d\n", 160 pa->pa_bus, pa->pa_device, pa->pa_function); 161 return 1; 162 } 163 164 if (kn20aa_irq != 13) 165 kn20aa_irq += buspin - 1; 166 167 if (kn20aa_irq >= KN20AA_MAX_IRQ) { 168 printf("dec_kn20aa_intr_map: kn20aa_irq %d too large for %d/%d/%d\n", 169 kn20aa_irq, pa->pa_bus, pa->pa_device, pa->pa_function); 170 return 1; 171 } 172 173 *ihp = kn20aa_irq; 174 return 0; 175 } 176 177 const char * 178 dec_kn20aa_intr_string(ccv, ih) 179 void *ccv; 180 pci_intr_handle_t ih; 181 { 182 static char irqstr[15]; /* 11 + 2 + NULL + sanity */ 183 184 if (ih > KN20AA_MAX_IRQ) 185 panic("dec_kn20aa_intr_string: bogus kn20aa IRQ 0x%lx", ih); 186 187 snprintf(irqstr, sizeof irqstr, "kn20aa irq %ld", ih); 188 return (irqstr); 189 } 190 191 int 192 dec_kn20aa_intr_line(ccv, ih) 193 void *ccv; 194 pci_intr_handle_t ih; 195 { 196 return (ih); 197 } 198 199 void * 200 dec_kn20aa_intr_establish(ccv, ih, level, func, arg, name) 201 void *ccv, *arg; 202 pci_intr_handle_t ih; 203 int level; 204 int (*func)(void *); 205 const char *name; 206 { 207 void *cookie; 208 209 if (ih > KN20AA_MAX_IRQ) 210 panic("dec_kn20aa_intr_establish: bogus kn20aa IRQ 0x%lx", 211 ih); 212 213 cookie = alpha_shared_intr_establish(kn20aa_pci_intr, ih, IST_LEVEL, 214 level, func, arg, name); 215 216 if (cookie != NULL && 217 alpha_shared_intr_firstactive(kn20aa_pci_intr, ih)) { 218 scb_set(0x900 + SCB_IDXTOVEC(ih), kn20aa_iointr, NULL); 219 kn20aa_enable_intr(ih); 220 } 221 return (cookie); 222 } 223 224 void 225 dec_kn20aa_intr_disestablish(ccv, cookie) 226 void *ccv, *cookie; 227 { 228 struct alpha_shared_intrhand *ih = cookie; 229 unsigned int irq = ih->ih_num; 230 int s; 231 232 s = splhigh(); 233 234 alpha_shared_intr_disestablish(kn20aa_pci_intr, cookie); 235 if (alpha_shared_intr_isactive(kn20aa_pci_intr, irq) == 0) { 236 kn20aa_disable_intr(irq); 237 alpha_shared_intr_set_dfltsharetype(kn20aa_pci_intr, irq, 238 IST_NONE); 239 scb_free(0x900 + SCB_IDXTOVEC(irq)); 240 } 241 splx(s); 242 } 243 244 void 245 kn20aa_iointr(arg, vec) 246 void *arg; 247 unsigned long vec; 248 { 249 int irq; 250 251 irq = SCB_VECTOIDX(vec - 0x900); 252 253 if (!alpha_shared_intr_dispatch(kn20aa_pci_intr, irq)) { 254 alpha_shared_intr_stray(kn20aa_pci_intr, irq, 255 "kn20aa irq"); 256 if (ALPHA_SHARED_INTR_DISABLE(kn20aa_pci_intr, irq)) 257 kn20aa_disable_intr(irq); 258 } else 259 alpha_shared_intr_reset_strays(kn20aa_pci_intr, irq); 260 } 261 262 void 263 kn20aa_enable_intr(irq) 264 int irq; 265 { 266 267 /* 268 * From disassembling small bits of the OSF/1 kernel: 269 * the following appears to enable a given interrupt request. 270 * "blech." I'd give valuable body parts for better docs or 271 * for a good decompiler. 272 */ 273 alpha_mb(); 274 REGVAL(0x8780000000L + 0x40L) |= (1 << irq); /* XXX */ 275 alpha_mb(); 276 } 277 278 void 279 kn20aa_disable_intr(irq) 280 int irq; 281 { 282 283 alpha_mb(); 284 REGVAL(0x8780000000L + 0x40L) &= ~(1 << irq); /* XXX */ 285 alpha_mb(); 286 } 287