1/* $NetBSD: ttwoga_pci.c,v 1.6 2011/06/14 15:34:22 matt 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.6 2011/06/14 15:34:22 matt 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 47void ttwoga_attach_hook(device_t, device_t, 48 struct pcibus_attach_args *); 49int ttwoga_bus_maxdevs(void *, int); 50pcitag_t ttwoga_make_tag(void *, int, int, int); 51void ttwoga_decompose_tag(void *, pcitag_t, int *, int *, 52 int *); 53pcireg_t ttwoga_conf_read(void *, pcitag_t, int); 54void ttwoga_conf_write(void *, pcitag_t, int, pcireg_t); 55 56paddr_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 68static kmutex_t ttwoga_conf_lock; 69cpuid_t ttwoga_conf_cpu; /* XXX core logic bug */ 70 71#define TTWOGA_CONF_LOCK() \ 72do { \ 73 mutex_enter(&ttwoga_conf_lock); \ 74 ttwoga_conf_cpu = cpu_number(); \ 75} while (0) 76 77#define TTWOGA_CONF_UNLOCK() \ 78do { \ 79 ttwoga_conf_cpu = (cpuid_t)-1; \ 80 mutex_exit(&ttwoga_conf_lock); \ 81} while (0) 82 83void 84ttwoga_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 98void 99ttwoga_attach_hook(device_t parent, device_t self, 100 struct pcibus_attach_args *pba) 101{ 102} 103 104int 105ttwoga_bus_maxdevs(void *cpv, int busno) 106{ 107 108 return 32; 109} 110 111pcitag_t 112ttwoga_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 119void 120ttwoga_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 131paddr_t 132ttwoga_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 140pcireg_t 141ttwoga_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 pci_decompose_tag(&tcp->tc_pc, tag, &b, &d, &f); 150 151 addr = b ? tag : ttwoga_make_type0addr(d, f); 152 if (addr == (paddr_t)-1) 153 return ((pcireg_t) -1); 154 155 TTWOGA_CONF_LOCK(); 156 157 alpha_mb(); 158 old_hae3 = T2GA(tcp, T2_HAE0_3) & ~HAE0_3_PCA; 159 T2GA(tcp, T2_HAE0_3) = 160 old_hae3 | ((b ? 1UL : 0UL) << HAE0_3_PCA_SHIFT); 161 alpha_mb(); 162 alpha_mb(); 163 164 datap = 165 (pcireg_t *)ALPHA_PHYS_TO_K0SEG(tcp->tc_sysmap->tsmap_conf_base | 166 addr << 5UL | /* address shift */ 167 (offset & ~0x03) << 5UL | /* address shift */ 168 0x3 << 3UL); /* 4-byte, size shift */ 169 data = (pcireg_t)-1; 170 if (!(ba = badaddr(datap, sizeof *datap))) 171 data = *datap; 172 173 alpha_mb(); 174 T2GA(tcp, T2_HAE0_3) = old_hae3; 175 alpha_mb(); 176 alpha_mb(); 177 178 alpha_pal_draina(); 179 alpha_mb(); 180 alpha_mb(); 181 182 TTWOGA_CONF_UNLOCK(); 183 184#if 0 185 printf("ttwoga_conf_read: tag 0x%lx, reg 0x%x -> 0x%x @ %p%s\n", 186 tag, offset, data, datap, ba ? " (badaddr)" : ""); 187#endif 188 189 return (data); 190} 191 192void 193ttwoga_conf_write(void *cpv, pcitag_t tag, int offset, pcireg_t data) 194{ 195 struct ttwoga_config *tcp = cpv; 196 pcireg_t *datap; 197 int b, d, f; 198 paddr_t addr; 199 uint64_t old_hae3; 200 201 pci_decompose_tag(&tcp->tc_pc, tag, &b, &d, &f); 202 203 addr = b ? tag : ttwoga_make_type0addr(d, f); 204 if (addr == (paddr_t)-1) 205 return; 206 207 TTWOGA_CONF_LOCK(); 208 209 alpha_mb(); 210 old_hae3 = T2GA(tcp, T2_HAE0_3) & ~HAE0_3_PCA; 211 T2GA(tcp, T2_HAE0_3) = 212 old_hae3 | ((b ? 1UL : 0UL) << HAE0_3_PCA_SHIFT); 213 alpha_mb(); 214 alpha_mb(); 215 216 datap = 217 (pcireg_t *)ALPHA_PHYS_TO_K0SEG(tcp->tc_sysmap->tsmap_conf_base | 218 addr << 5UL | /* address shift */ 219 (offset & ~0x03) << 5UL | /* address shift */ 220 0x3 << 3UL); /* 4-byte, size shift */ 221 222 alpha_mb(); 223 *datap = data; 224 alpha_mb(); 225 alpha_mb(); 226 227 alpha_mb(); 228 T2GA(tcp, T2_HAE0_3) = old_hae3; 229 alpha_mb(); 230 alpha_mb(); 231 232 TTWOGA_CONF_UNLOCK(); 233 234#if 0 235 printf("ttwoga_conf_write: tag 0x%lx, reg 0x%x -> 0x%x @ %p\n", 236 tag, offset, data, datap); 237#endif 238} 239