Home | History | Annotate | Line # | Download | only in pci
pci_map.c revision 1.5.12.2
      1  1.5.12.2   bouyer /*	$NetBSD: pci_map.c,v 1.5.12.2 2000/12/08 09:12:33 bouyer Exp $	*/
      2       1.1  mycroft 
      3       1.5  mycroft /*-
      4  1.5.12.1   bouyer  * Copyright (c) 1998, 2000 The NetBSD Foundation, Inc.
      5       1.5  mycroft  * All rights reserved.
      6       1.5  mycroft  *
      7       1.5  mycroft  * This code is derived from software contributed to The NetBSD Foundation
      8  1.5.12.1   bouyer  * by Charles M. Hannum; by William R. Studenmund; by Jason R. Thorpe.
      9       1.1  mycroft  *
     10       1.1  mycroft  * Redistribution and use in source and binary forms, with or without
     11       1.1  mycroft  * modification, are permitted provided that the following conditions
     12       1.1  mycroft  * are met:
     13       1.1  mycroft  * 1. Redistributions of source code must retain the above copyright
     14       1.1  mycroft  *    notice, this list of conditions and the following disclaimer.
     15       1.1  mycroft  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.1  mycroft  *    notice, this list of conditions and the following disclaimer in the
     17       1.1  mycroft  *    documentation and/or other materials provided with the distribution.
     18       1.1  mycroft  * 3. All advertising materials mentioning features or use of this software
     19       1.1  mycroft  *    must display the following acknowledgement:
     20       1.5  mycroft  *        This product includes software developed by the NetBSD
     21       1.5  mycroft  *        Foundation, Inc. and its contributors.
     22       1.5  mycroft  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23       1.5  mycroft  *    contributors may be used to endorse or promote products derived
     24       1.5  mycroft  *    from this software without specific prior written permission.
     25       1.1  mycroft  *
     26       1.5  mycroft  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27       1.5  mycroft  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28       1.5  mycroft  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29       1.5  mycroft  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30       1.5  mycroft  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31       1.5  mycroft  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32       1.5  mycroft  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33       1.5  mycroft  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34       1.5  mycroft  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35       1.5  mycroft  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36       1.5  mycroft  * POSSIBILITY OF SUCH DAMAGE.
     37       1.1  mycroft  */
     38       1.1  mycroft 
     39       1.1  mycroft /*
     40       1.1  mycroft  * PCI device mapping.
     41       1.1  mycroft  */
     42       1.1  mycroft 
     43       1.1  mycroft #include <sys/param.h>
     44       1.1  mycroft #include <sys/systm.h>
     45       1.1  mycroft #include <sys/device.h>
     46       1.1  mycroft 
     47       1.1  mycroft #include <dev/pci/pcireg.h>
     48       1.1  mycroft #include <dev/pci/pcivar.h>
     49       1.1  mycroft 
     50       1.1  mycroft static int
     51  1.5.12.2   bouyer pci_io_find(pci_chipset_tag_t pc, pcitag_t tag, int reg, pcireg_t type,
     52  1.5.12.2   bouyer     bus_addr_t *basep, bus_size_t *sizep, int *flagsp)
     53       1.1  mycroft {
     54       1.1  mycroft 	pcireg_t address, mask;
     55       1.1  mycroft 	int s;
     56       1.1  mycroft 
     57  1.5.12.1   bouyer 	if (reg < PCI_MAPREG_START ||
     58  1.5.12.1   bouyer #if 0
     59  1.5.12.1   bouyer 	    /*
     60  1.5.12.1   bouyer 	     * Can't do this check; some devices have mapping registers
     61  1.5.12.1   bouyer 	     * way out in left field.
     62  1.5.12.1   bouyer 	     */
     63  1.5.12.1   bouyer 	    reg >= PCI_MAPREG_END ||
     64  1.5.12.1   bouyer #endif
     65  1.5.12.1   bouyer 	    (reg & 3))
     66       1.1  mycroft 		panic("pci_io_find: bad request");
     67       1.1  mycroft 
     68       1.1  mycroft 	/*
     69       1.1  mycroft 	 * Section 6.2.5.1, `Address Maps', tells us that:
     70       1.1  mycroft 	 *
     71       1.1  mycroft 	 * 1) The builtin software should have already mapped the device in a
     72       1.1  mycroft 	 * reasonable way.
     73       1.1  mycroft 	 *
     74       1.1  mycroft 	 * 2) A device which wants 2^n bytes of memory will hardwire the bottom
     75       1.1  mycroft 	 * n bits of the address to 0.  As recommended, we write all 1s and see
     76       1.1  mycroft 	 * what we get back.
     77       1.1  mycroft 	 */
     78       1.1  mycroft 	s = splhigh();
     79       1.1  mycroft 	address = pci_conf_read(pc, tag, reg);
     80       1.1  mycroft 	pci_conf_write(pc, tag, reg, 0xffffffff);
     81       1.1  mycroft 	mask = pci_conf_read(pc, tag, reg);
     82       1.1  mycroft 	pci_conf_write(pc, tag, reg, address);
     83       1.1  mycroft 	splx(s);
     84       1.1  mycroft 
     85       1.1  mycroft 	if (PCI_MAPREG_TYPE(address) != PCI_MAPREG_TYPE_IO) {
     86       1.1  mycroft 		printf("pci_io_find: expected type i/o, found mem\n");
     87       1.1  mycroft 		return (1);
     88       1.1  mycroft 	}
     89       1.1  mycroft 
     90       1.2  mycroft 	if (PCI_MAPREG_IO_SIZE(mask) == 0) {
     91       1.2  mycroft 		printf("pci_io_find: void region\n");
     92       1.2  mycroft 		return (1);
     93       1.2  mycroft 	}
     94       1.2  mycroft 
     95       1.1  mycroft 	if (basep != 0)
     96       1.1  mycroft 		*basep = PCI_MAPREG_IO_ADDR(address);
     97       1.1  mycroft 	if (sizep != 0)
     98       1.1  mycroft 		*sizep = PCI_MAPREG_IO_SIZE(mask);
     99       1.1  mycroft 	if (flagsp != 0)
    100       1.1  mycroft 		*flagsp = 0;
    101       1.1  mycroft 
    102       1.1  mycroft 	return (0);
    103       1.1  mycroft }
    104       1.1  mycroft 
    105       1.1  mycroft static int
    106  1.5.12.2   bouyer pci_mem_find(pci_chipset_tag_t pc, pcitag_t tag, int reg, pcireg_t type,
    107  1.5.12.2   bouyer     bus_addr_t *basep, bus_size_t *sizep, int *flagsp)
    108       1.1  mycroft {
    109  1.5.12.1   bouyer 	pcireg_t address, mask, address1 = 0, mask1 = 0xffffffff;
    110  1.5.12.1   bouyer 	u_int64_t waddress, wmask;
    111  1.5.12.1   bouyer 	int s, is64bit;
    112  1.5.12.1   bouyer 
    113  1.5.12.1   bouyer 	is64bit = (PCI_MAPREG_MEM_TYPE(type) == PCI_MAPREG_MEM_TYPE_64BIT);
    114  1.5.12.1   bouyer 
    115  1.5.12.1   bouyer 	if (reg < PCI_MAPREG_START ||
    116  1.5.12.1   bouyer #if 0
    117  1.5.12.1   bouyer 	    /*
    118  1.5.12.1   bouyer 	     * Can't do this check; some devices have mapping registers
    119  1.5.12.1   bouyer 	     * way out in left field.
    120  1.5.12.1   bouyer 	     */
    121  1.5.12.1   bouyer 	    reg >= PCI_MAPREG_END ||
    122  1.5.12.1   bouyer #endif
    123  1.5.12.1   bouyer 	    (reg & 3))
    124  1.5.12.1   bouyer 		panic("pci_mem_find: bad request");
    125       1.1  mycroft 
    126  1.5.12.1   bouyer 	if (is64bit && (reg + 4) >= PCI_MAPREG_END)
    127  1.5.12.1   bouyer 		panic("pci_mem_find: bad 64-bit request");
    128       1.1  mycroft 
    129       1.1  mycroft 	/*
    130       1.1  mycroft 	 * Section 6.2.5.1, `Address Maps', tells us that:
    131       1.1  mycroft 	 *
    132       1.1  mycroft 	 * 1) The builtin software should have already mapped the device in a
    133       1.1  mycroft 	 * reasonable way.
    134       1.1  mycroft 	 *
    135       1.1  mycroft 	 * 2) A device which wants 2^n bytes of memory will hardwire the bottom
    136       1.1  mycroft 	 * n bits of the address to 0.  As recommended, we write all 1s and see
    137       1.1  mycroft 	 * what we get back.
    138       1.1  mycroft 	 */
    139       1.1  mycroft 	s = splhigh();
    140       1.1  mycroft 	address = pci_conf_read(pc, tag, reg);
    141       1.1  mycroft 	pci_conf_write(pc, tag, reg, 0xffffffff);
    142       1.1  mycroft 	mask = pci_conf_read(pc, tag, reg);
    143       1.1  mycroft 	pci_conf_write(pc, tag, reg, address);
    144  1.5.12.1   bouyer 	if (is64bit) {
    145  1.5.12.1   bouyer 		address1 = pci_conf_read(pc, tag, reg + 4);
    146  1.5.12.1   bouyer 		pci_conf_write(pc, tag, reg + 4, 0xffffffff);
    147  1.5.12.1   bouyer 		mask1 = pci_conf_read(pc, tag, reg + 4);
    148  1.5.12.1   bouyer 		pci_conf_write(pc, tag, reg + 4, address1);
    149  1.5.12.1   bouyer 	}
    150       1.1  mycroft 	splx(s);
    151       1.1  mycroft 
    152       1.1  mycroft 	if (PCI_MAPREG_TYPE(address) != PCI_MAPREG_TYPE_MEM) {
    153       1.1  mycroft 		printf("pci_mem_find: expected type mem, found i/o\n");
    154       1.1  mycroft 		return (1);
    155       1.1  mycroft 	}
    156       1.1  mycroft 	if (PCI_MAPREG_MEM_TYPE(address) != PCI_MAPREG_MEM_TYPE(type)) {
    157       1.1  mycroft 		printf("pci_mem_find: expected mem type %08x, found %08x\n",
    158       1.1  mycroft 		    PCI_MAPREG_MEM_TYPE(type),
    159       1.1  mycroft 		    PCI_MAPREG_MEM_TYPE(address));
    160       1.2  mycroft 		return (1);
    161       1.2  mycroft 	}
    162       1.2  mycroft 
    163  1.5.12.1   bouyer 	waddress = (u_int64_t)address1 << 32UL | address;
    164  1.5.12.1   bouyer 	wmask = (u_int64_t)mask1 << 32UL | mask;
    165  1.5.12.1   bouyer 
    166  1.5.12.1   bouyer 	if (PCI_MAPREG_MEM64_SIZE(wmask) == 0) {
    167       1.2  mycroft 		printf("pci_mem_find: void region\n");
    168       1.1  mycroft 		return (1);
    169       1.1  mycroft 	}
    170       1.1  mycroft 
    171       1.1  mycroft 	switch (PCI_MAPREG_MEM_TYPE(address)) {
    172       1.1  mycroft 	case PCI_MAPREG_MEM_TYPE_32BIT:
    173       1.1  mycroft 	case PCI_MAPREG_MEM_TYPE_32BIT_1M:
    174       1.1  mycroft 		break;
    175       1.1  mycroft 	case PCI_MAPREG_MEM_TYPE_64BIT:
    176  1.5.12.1   bouyer 		/*
    177  1.5.12.1   bouyer 		 * Handle the case of a 64-bit memory register on a
    178  1.5.12.1   bouyer 		 * platform with 32-bit addressing.  Make sure that
    179  1.5.12.1   bouyer 		 * the address assigned and the device's memory size
    180  1.5.12.1   bouyer 		 * fit in 32 bits.  We implicitly assume that if
    181  1.5.12.1   bouyer 		 * bus_addr_t is 64-bit, then so is bus_size_t.
    182  1.5.12.1   bouyer 		 */
    183  1.5.12.1   bouyer 		if (sizeof(u_int64_t) > sizeof(bus_addr_t) &&
    184  1.5.12.1   bouyer 		    (address1 != 0 || mask1 != 0xffffffff)) {
    185  1.5.12.1   bouyer 			printf("pci_mem_find: 64-bit memory map which is "
    186  1.5.12.1   bouyer 			    "inaccessible on a 32-bit platform\n");
    187  1.5.12.1   bouyer 			return (1);
    188  1.5.12.1   bouyer 		}
    189  1.5.12.1   bouyer 		break;
    190       1.1  mycroft 	default:
    191       1.1  mycroft 		printf("pci_mem_find: reserved mapping register type\n");
    192       1.1  mycroft 		return (1);
    193       1.1  mycroft 	}
    194       1.1  mycroft 
    195  1.5.12.1   bouyer 	if (sizeof(u_int64_t) > sizeof(bus_addr_t)) {
    196  1.5.12.1   bouyer 		if (basep != 0)
    197  1.5.12.1   bouyer 			*basep = PCI_MAPREG_MEM_ADDR(address);
    198  1.5.12.1   bouyer 		if (sizep != 0)
    199  1.5.12.1   bouyer 			*sizep = PCI_MAPREG_MEM_SIZE(mask);
    200  1.5.12.1   bouyer 	} else {
    201  1.5.12.1   bouyer 		if (basep != 0)
    202  1.5.12.1   bouyer 			*basep = PCI_MAPREG_MEM64_ADDR(waddress);
    203  1.5.12.1   bouyer 		if (sizep != 0)
    204  1.5.12.1   bouyer 			*sizep = PCI_MAPREG_MEM64_SIZE(wmask);
    205  1.5.12.1   bouyer 	}
    206       1.1  mycroft 	if (flagsp != 0)
    207  1.5.12.1   bouyer 		*flagsp = PCI_MAPREG_MEM_PREFETCHABLE(address) ?
    208  1.5.12.1   bouyer 		    BUS_SPACE_MAP_PREFETCHABLE : 0;
    209       1.1  mycroft 
    210       1.1  mycroft 	return (0);
    211  1.5.12.1   bouyer }
    212  1.5.12.1   bouyer 
    213  1.5.12.1   bouyer pcireg_t
    214  1.5.12.2   bouyer pci_mapreg_type(pci_chipset_tag_t pc, pcitag_t tag, int reg)
    215  1.5.12.1   bouyer {
    216  1.5.12.1   bouyer 	pcireg_t rv;
    217  1.5.12.1   bouyer 
    218  1.5.12.1   bouyer 	rv = pci_conf_read(pc, tag, reg);
    219  1.5.12.1   bouyer 	if (PCI_MAPREG_TYPE(rv) == PCI_MAPREG_TYPE_IO)
    220  1.5.12.1   bouyer 		rv &= PCI_MAPREG_TYPE_MASK;
    221  1.5.12.1   bouyer 	else
    222  1.5.12.1   bouyer 		rv &= PCI_MAPREG_TYPE_MASK|PCI_MAPREG_MEM_TYPE_MASK;
    223  1.5.12.1   bouyer 	return (rv);
    224       1.1  mycroft }
    225       1.1  mycroft 
    226       1.1  mycroft int
    227  1.5.12.2   bouyer pci_mapreg_info(pci_chipset_tag_t pc, pcitag_t tag, int reg, pcireg_t type,
    228  1.5.12.2   bouyer     bus_addr_t *basep, bus_size_t *sizep, int *flagsp)
    229       1.1  mycroft {
    230       1.1  mycroft 
    231       1.1  mycroft 	if (PCI_MAPREG_TYPE(type) == PCI_MAPREG_TYPE_IO)
    232       1.4  thorpej 		return (pci_io_find(pc, tag, reg, type, basep, sizep,
    233       1.4  thorpej 		    flagsp));
    234       1.1  mycroft 	else
    235       1.4  thorpej 		return (pci_mem_find(pc, tag, reg, type, basep, sizep,
    236       1.4  thorpej 		    flagsp));
    237       1.1  mycroft }
    238       1.1  mycroft 
    239       1.1  mycroft int
    240  1.5.12.2   bouyer pci_mapreg_map(struct pci_attach_args *pa, int reg, pcireg_t type,
    241  1.5.12.2   bouyer     int busflags, bus_space_tag_t *tagp, bus_space_handle_t *handlep,
    242  1.5.12.2   bouyer     bus_addr_t *basep, bus_size_t *sizep)
    243       1.1  mycroft {
    244       1.1  mycroft 	bus_space_tag_t tag;
    245       1.1  mycroft 	bus_space_handle_t handle;
    246       1.1  mycroft 	bus_addr_t base;
    247       1.1  mycroft 	bus_size_t size;
    248       1.1  mycroft 	int flags;
    249       1.1  mycroft 
    250       1.1  mycroft 	if (PCI_MAPREG_TYPE(type) == PCI_MAPREG_TYPE_IO) {
    251       1.1  mycroft 		if ((pa->pa_flags & PCI_FLAGS_IO_ENABLED) == 0)
    252       1.1  mycroft 			return (1);
    253       1.1  mycroft 		if (pci_io_find(pa->pa_pc, pa->pa_tag, reg, type, &base,
    254       1.1  mycroft 		    &size, &flags))
    255       1.1  mycroft 			return (1);
    256       1.1  mycroft 		tag = pa->pa_iot;
    257       1.1  mycroft 	} else {
    258       1.1  mycroft 		if ((pa->pa_flags & PCI_FLAGS_MEM_ENABLED) == 0)
    259       1.1  mycroft 			return (1);
    260       1.1  mycroft 		if (pci_mem_find(pa->pa_pc, pa->pa_tag, reg, type, &base,
    261       1.1  mycroft 		    &size, &flags))
    262       1.1  mycroft 			return (1);
    263       1.1  mycroft 		tag = pa->pa_memt;
    264       1.1  mycroft 	}
    265       1.1  mycroft 
    266       1.1  mycroft 	if (bus_space_map(tag, base, size, busflags | flags, &handle))
    267       1.1  mycroft 		return (1);
    268       1.1  mycroft 
    269       1.1  mycroft 	if (tagp != 0)
    270       1.1  mycroft 		*tagp = tag;
    271       1.1  mycroft 	if (handlep != 0)
    272       1.1  mycroft 		*handlep = handle;
    273       1.1  mycroft 	if (basep != 0)
    274       1.1  mycroft 		*basep = base;
    275       1.1  mycroft 	if (sizep != 0)
    276       1.1  mycroft 		*sizep = size;
    277       1.1  mycroft 
    278       1.1  mycroft 	return (0);
    279       1.1  mycroft }
    280