xref: /netbsd-src/sys/dev/pci/pci_map.c (revision dc306354b0b29af51801a7632f1e95265a68cd81)
1 /*	$NetBSD: pci_map.c,v 1.5 1998/08/15 10:10:54 mycroft Exp $	*/
2 
3 /*-
4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Charles M. Hannum.
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  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *        This product includes software developed by the NetBSD
21  *        Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 /*
40  * PCI device mapping.
41  */
42 
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/device.h>
46 
47 #include <dev/pci/pcireg.h>
48 #include <dev/pci/pcivar.h>
49 
50 static int pci_io_find __P((pci_chipset_tag_t, pcitag_t, int, pcireg_t,
51     bus_addr_t *, bus_size_t *, int *));
52 static int pci_mem_find __P((pci_chipset_tag_t, pcitag_t, int, pcireg_t,
53     bus_addr_t *, bus_size_t *, int *));
54 
55 static int
56 pci_io_find(pc, tag, reg, type, basep, sizep, flagsp)
57 	pci_chipset_tag_t pc;
58 	pcitag_t tag;
59 	int reg;
60 	pcireg_t type;
61 	bus_addr_t *basep;
62 	bus_size_t *sizep;
63 	int *flagsp;
64 {
65 	pcireg_t address, mask;
66 	int s;
67 
68 	if (reg < PCI_MAPREG_START || reg >= PCI_MAPREG_END || (reg & 3))
69 		panic("pci_io_find: bad request");
70 
71 	/*
72 	 * Section 6.2.5.1, `Address Maps', tells us that:
73 	 *
74 	 * 1) The builtin software should have already mapped the device in a
75 	 * reasonable way.
76 	 *
77 	 * 2) A device which wants 2^n bytes of memory will hardwire the bottom
78 	 * n bits of the address to 0.  As recommended, we write all 1s and see
79 	 * what we get back.
80 	 */
81 	s = splhigh();
82 	address = pci_conf_read(pc, tag, reg);
83 	pci_conf_write(pc, tag, reg, 0xffffffff);
84 	mask = pci_conf_read(pc, tag, reg);
85 	pci_conf_write(pc, tag, reg, address);
86 	splx(s);
87 
88 	if (PCI_MAPREG_TYPE(address) != PCI_MAPREG_TYPE_IO) {
89 		printf("pci_io_find: expected type i/o, found mem\n");
90 		return (1);
91 	}
92 
93 	if (PCI_MAPREG_IO_SIZE(mask) == 0) {
94 		printf("pci_io_find: void region\n");
95 		return (1);
96 	}
97 
98 	if (basep != 0)
99 		*basep = PCI_MAPREG_IO_ADDR(address);
100 	if (sizep != 0)
101 		*sizep = PCI_MAPREG_IO_SIZE(mask);
102 	if (flagsp != 0)
103 		*flagsp = 0;
104 
105 	return (0);
106 }
107 
108 static int
109 pci_mem_find(pc, tag, reg, type, basep, sizep, flagsp)
110 	pci_chipset_tag_t pc;
111 	pcitag_t tag;
112 	int reg;
113 	pcireg_t type;
114 	bus_addr_t *basep;
115 	bus_size_t *sizep;
116 	int *flagsp;
117 {
118 	pcireg_t address, mask;
119 	int s;
120 
121 	if (reg < PCI_MAPREG_START || reg >= PCI_MAPREG_END || (reg & 3))
122 		panic("pci_find_mem: bad request");
123 
124 	/*
125 	 * Section 6.2.5.1, `Address Maps', tells us that:
126 	 *
127 	 * 1) The builtin software should have already mapped the device in a
128 	 * reasonable way.
129 	 *
130 	 * 2) A device which wants 2^n bytes of memory will hardwire the bottom
131 	 * n bits of the address to 0.  As recommended, we write all 1s and see
132 	 * what we get back.
133 	 */
134 	s = splhigh();
135 	address = pci_conf_read(pc, tag, reg);
136 	pci_conf_write(pc, tag, reg, 0xffffffff);
137 	mask = pci_conf_read(pc, tag, reg);
138 	pci_conf_write(pc, tag, reg, address);
139 	splx(s);
140 
141 	if (PCI_MAPREG_TYPE(address) != PCI_MAPREG_TYPE_MEM) {
142 		printf("pci_mem_find: expected type mem, found i/o\n");
143 		return (1);
144 	}
145 	if (PCI_MAPREG_MEM_TYPE(address) != PCI_MAPREG_MEM_TYPE(type)) {
146 		printf("pci_mem_find: expected mem type %08x, found %08x\n",
147 		    PCI_MAPREG_MEM_TYPE(type),
148 		    PCI_MAPREG_MEM_TYPE(address));
149 		return (1);
150 	}
151 
152 	if (PCI_MAPREG_MEM_SIZE(mask) == 0) {
153 		printf("pci_mem_find: void region\n");
154 		return (1);
155 	}
156 
157 	switch (PCI_MAPREG_MEM_TYPE(address)) {
158 	case PCI_MAPREG_MEM_TYPE_32BIT:
159 	case PCI_MAPREG_MEM_TYPE_32BIT_1M:
160 		break;
161 	case PCI_MAPREG_MEM_TYPE_64BIT:
162 		printf("pci_mem_find: 64-bit memory mapping register\n");
163 		return (1);
164 	default:
165 		printf("pci_mem_find: reserved mapping register type\n");
166 		return (1);
167 	}
168 
169 	if (basep != 0)
170 		*basep = PCI_MAPREG_MEM_ADDR(address);
171 	if (sizep != 0)
172 		*sizep = PCI_MAPREG_MEM_SIZE(mask);
173 	if (flagsp != 0)
174 		*flagsp = PCI_MAPREG_MEM_CACHEABLE(address) ?
175 		    BUS_SPACE_MAP_CACHEABLE : 0;
176 
177 	return (0);
178 }
179 
180 int
181 pci_mapreg_info(pc, tag, reg, type, basep, sizep, flagsp)
182 	pci_chipset_tag_t pc;
183 	pcitag_t tag;
184 	int reg;
185 	pcireg_t type;
186 	bus_addr_t *basep;
187 	bus_size_t *sizep;
188 	int *flagsp;
189 {
190 
191 	if (PCI_MAPREG_TYPE(type) == PCI_MAPREG_TYPE_IO)
192 		return (pci_io_find(pc, tag, reg, type, basep, sizep,
193 		    flagsp));
194 	else
195 		return (pci_mem_find(pc, tag, reg, type, basep, sizep,
196 		    flagsp));
197 }
198 
199 int
200 pci_mapreg_map(pa, reg, type, busflags, tagp, handlep, basep, sizep)
201 	struct pci_attach_args *pa;
202 	int reg, busflags;
203 	pcireg_t type;
204 	bus_space_tag_t *tagp;
205 	bus_space_handle_t *handlep;
206 	bus_addr_t *basep;
207 	bus_size_t *sizep;
208 {
209 	bus_space_tag_t tag;
210 	bus_space_handle_t handle;
211 	bus_addr_t base;
212 	bus_size_t size;
213 	int flags;
214 
215 	if (PCI_MAPREG_TYPE(type) == PCI_MAPREG_TYPE_IO) {
216 		if ((pa->pa_flags & PCI_FLAGS_IO_ENABLED) == 0)
217 			return (1);
218 		if (pci_io_find(pa->pa_pc, pa->pa_tag, reg, type, &base,
219 		    &size, &flags))
220 			return (1);
221 		tag = pa->pa_iot;
222 	} else {
223 		if ((pa->pa_flags & PCI_FLAGS_MEM_ENABLED) == 0)
224 			return (1);
225 		if (pci_mem_find(pa->pa_pc, pa->pa_tag, reg, type, &base,
226 		    &size, &flags))
227 			return (1);
228 		tag = pa->pa_memt;
229 	}
230 
231 	if (bus_space_map(tag, base, size, busflags | flags, &handle))
232 		return (1);
233 
234 	if (tagp != 0)
235 		*tagp = tag;
236 	if (handlep != 0)
237 		*handlep = handle;
238 	if (basep != 0)
239 		*basep = base;
240 	if (sizep != 0)
241 		*sizep = size;
242 
243 	return (0);
244 }
245