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