1 /* $NetBSD: ttwoga_pci.c,v 1.8 2015/10/02 05:22:49 msaitoh Exp $ */ 2 3 /*- 4 * Copyright (c) 1999, 2000 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Jason R. Thorpe. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 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> /* RCS ID & Copyright macro defns */ 33 34 __KERNEL_RCSID(0, "$NetBSD: ttwoga_pci.c,v 1.8 2015/10/02 05:22:49 msaitoh Exp $"); 35 36 #include <sys/param.h> 37 #include <sys/systm.h> 38 #include <sys/kernel.h> 39 #include <sys/device.h> 40 41 #include <dev/pci/pcireg.h> 42 #include <dev/pci/pcivar.h> 43 44 #include <alpha/pci/ttwogareg.h> 45 #include <alpha/pci/ttwogavar.h> 46 47 void ttwoga_attach_hook(device_t, device_t, 48 struct pcibus_attach_args *); 49 int ttwoga_bus_maxdevs(void *, int); 50 pcitag_t ttwoga_make_tag(void *, int, int, int); 51 void ttwoga_decompose_tag(void *, pcitag_t, int *, int *, 52 int *); 53 pcireg_t ttwoga_conf_read(void *, pcitag_t, int); 54 void ttwoga_conf_write(void *, pcitag_t, int, pcireg_t); 55 56 paddr_t ttwoga_make_type0addr(int, int); 57 58 /* 59 * The T2 has an annoying bug that can manifest itself while 60 * probing for devices. If a non-existent CBUS address is 61 * accessed with a read, "a few" subsequent write cycles 62 * will generate spurious memory errors. We work around this 63 * by tracking which CPU has the PCI configuration mutex and 64 * "expect" machine checks on that CPU for the duraction of 65 * the PCI configuration access routine. 66 */ 67 68 static kmutex_t ttwoga_conf_lock; 69 cpuid_t ttwoga_conf_cpu; /* XXX core logic bug */ 70 71 #define TTWOGA_CONF_LOCK() \ 72 do { \ 73 mutex_enter(&ttwoga_conf_lock); \ 74 ttwoga_conf_cpu = cpu_number(); \ 75 } while (0) 76 77 #define TTWOGA_CONF_UNLOCK() \ 78 do { \ 79 ttwoga_conf_cpu = (cpuid_t)-1; \ 80 mutex_exit(&ttwoga_conf_lock); \ 81 } while (0) 82 83 void 84 ttwoga_pci_init(pci_chipset_tag_t pc, void *v) 85 { 86 87 mutex_init(&ttwoga_conf_lock, MUTEX_DEFAULT, IPL_HIGH); 88 89 pc->pc_conf_v = v; 90 pc->pc_attach_hook = ttwoga_attach_hook; 91 pc->pc_bus_maxdevs = ttwoga_bus_maxdevs; 92 pc->pc_make_tag = ttwoga_make_tag; 93 pc->pc_decompose_tag = ttwoga_decompose_tag; 94 pc->pc_conf_read = ttwoga_conf_read; 95 pc->pc_conf_write = ttwoga_conf_write; 96 } 97 98 void 99 ttwoga_attach_hook(device_t parent, device_t self, 100 struct pcibus_attach_args *pba) 101 { 102 } 103 104 int 105 ttwoga_bus_maxdevs(void *cpv, int busno) 106 { 107 108 return 32; 109 } 110 111 pcitag_t 112 ttwoga_make_tag(void *cpv, int b, int d, int f) 113 { 114 115 /* This is the format used for Type 1 configuration cycles. */ 116 return (b << 16) | (d << 11) | (f << 8); 117 } 118 119 void 120 ttwoga_decompose_tag(void *cpv, pcitag_t tag, int *bp, int *dp, int *fp) 121 { 122 123 if (bp != NULL) 124 *bp = (tag >> 16) & 0xff; 125 if (dp != NULL) 126 *dp = (tag >> 11) & 0x1f; 127 if (fp != NULL) 128 *fp = (tag >> 8) & 0x7; 129 } 130 131 paddr_t 132 ttwoga_make_type0addr(int d, int f) 133 { 134 135 if (d > 8) /* XXX ??? */ 136 return ((paddr_t) -1); 137 return ((0x0800UL << d) | (f << 8)); 138 } 139 140 pcireg_t 141 ttwoga_conf_read(void *cpv, pcitag_t tag, int offset) 142 { 143 struct ttwoga_config *tcp = cpv; 144 pcireg_t *datap, data; 145 int b, d, f, ba; 146 paddr_t addr; 147 uint64_t old_hae3; 148 149 if ((unsigned int)offset >= PCI_CONF_SIZE) 150 return (pcireg_t) -1; 151 152 pci_decompose_tag(&tcp->tc_pc, tag, &b, &d, &f); 153 154 addr = b ? tag : ttwoga_make_type0addr(d, f); 155 if (addr == (paddr_t)-1) 156 return ((pcireg_t) -1); 157 158 TTWOGA_CONF_LOCK(); 159 160 alpha_mb(); 161 old_hae3 = T2GA(tcp, T2_HAE0_3) & ~HAE0_3_PCA; 162 T2GA(tcp, T2_HAE0_3) = 163 old_hae3 | ((b ? 1UL : 0UL) << HAE0_3_PCA_SHIFT); 164 alpha_mb(); 165 alpha_mb(); 166 167 datap = 168 (pcireg_t *)ALPHA_PHYS_TO_K0SEG(tcp->tc_sysmap->tsmap_conf_base | 169 addr << 5UL | /* address shift */ 170 (offset & ~0x03) << 5UL | /* address shift */ 171 0x3 << 3UL); /* 4-byte, size shift */ 172 data = (pcireg_t)-1; 173 if (!(ba = badaddr(datap, sizeof *datap))) 174 data = *datap; 175 176 alpha_mb(); 177 T2GA(tcp, T2_HAE0_3) = old_hae3; 178 alpha_mb(); 179 alpha_mb(); 180 181 alpha_pal_draina(); 182 alpha_mb(); 183 alpha_mb(); 184 185 TTWOGA_CONF_UNLOCK(); 186 187 #if 0 188 printf("ttwoga_conf_read: tag 0x%lx, reg 0x%x -> 0x%x @ %p%s\n", 189 tag, offset, data, datap, ba ? " (badaddr)" : ""); 190 #endif 191 192 return (data); 193 } 194 195 void 196 ttwoga_conf_write(void *cpv, pcitag_t tag, int offset, pcireg_t data) 197 { 198 struct ttwoga_config *tcp = cpv; 199 pcireg_t *datap; 200 int b, d, f; 201 paddr_t addr; 202 uint64_t old_hae3; 203 204 if ((unsigned int)offset >= PCI_CONF_SIZE) 205 return; 206 207 pci_decompose_tag(&tcp->tc_pc, tag, &b, &d, &f); 208 209 addr = b ? tag : ttwoga_make_type0addr(d, f); 210 if (addr == (paddr_t)-1) 211 return; 212 213 TTWOGA_CONF_LOCK(); 214 215 alpha_mb(); 216 old_hae3 = T2GA(tcp, T2_HAE0_3) & ~HAE0_3_PCA; 217 T2GA(tcp, T2_HAE0_3) = 218 old_hae3 | ((b ? 1UL : 0UL) << HAE0_3_PCA_SHIFT); 219 alpha_mb(); 220 alpha_mb(); 221 222 datap = 223 (pcireg_t *)ALPHA_PHYS_TO_K0SEG(tcp->tc_sysmap->tsmap_conf_base | 224 addr << 5UL | /* address shift */ 225 (offset & ~0x03) << 5UL | /* address shift */ 226 0x3 << 3UL); /* 4-byte, size shift */ 227 228 alpha_mb(); 229 *datap = data; 230 alpha_mb(); 231 alpha_mb(); 232 233 alpha_mb(); 234 T2GA(tcp, T2_HAE0_3) = old_hae3; 235 alpha_mb(); 236 alpha_mb(); 237 238 TTWOGA_CONF_UNLOCK(); 239 240 #if 0 241 printf("ttwoga_conf_write: tag 0x%lx, reg 0x%x -> 0x%x @ %p\n", 242 tag, offset, data, datap); 243 #endif 244 } 245