pci_map.c revision 1.16.16.1 1 /* $NetBSD: pci_map.c,v 1.16.16.1 2006/10/22 06:06:18 yamt Exp $ */
2
3 /*-
4 * Copyright (c) 1998, 2000 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; by William R. Studenmund; 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 * 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/cdefs.h>
44 __KERNEL_RCSID(0, "$NetBSD: pci_map.c,v 1.16.16.1 2006/10/22 06:06:18 yamt Exp $");
45
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/device.h>
49
50 #include <dev/pci/pcireg.h>
51 #include <dev/pci/pcivar.h>
52
53 static int
54 pci_io_find(pci_chipset_tag_t pc, pcitag_t tag, int reg, pcireg_t type __unused,
55 bus_addr_t *basep, bus_size_t *sizep, int *flagsp)
56 {
57 pcireg_t address, mask;
58 int s;
59
60 if (reg < PCI_MAPREG_START ||
61 #if 0
62 /*
63 * Can't do this check; some devices have mapping registers
64 * way out in left field.
65 */
66 reg >= PCI_MAPREG_END ||
67 #endif
68 (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(pci_chipset_tag_t pc, pcitag_t tag, int reg, pcireg_t type,
110 bus_addr_t *basep, bus_size_t *sizep, int *flagsp)
111 {
112 pcireg_t address, mask, address1 = 0, mask1 = 0xffffffff;
113 u_int64_t waddress, wmask;
114 int s, is64bit, isrom;
115
116 is64bit = (PCI_MAPREG_MEM_TYPE(type) == PCI_MAPREG_MEM_TYPE_64BIT);
117 isrom = (reg == PCI_MAPREG_ROM);
118
119 if ((!isrom) && (reg < PCI_MAPREG_START ||
120 #if 0
121 /*
122 * Can't do this check; some devices have mapping registers
123 * way out in left field.
124 */
125 reg >= PCI_MAPREG_END ||
126 #endif
127 (reg & 3)))
128 panic("pci_mem_find: bad request");
129
130 if (is64bit && (reg + 4) >= PCI_MAPREG_END)
131 panic("pci_mem_find: bad 64-bit request");
132
133 /*
134 * Section 6.2.5.1, `Address Maps', tells us that:
135 *
136 * 1) The builtin software should have already mapped the device in a
137 * reasonable way.
138 *
139 * 2) A device which wants 2^n bytes of memory will hardwire the bottom
140 * n bits of the address to 0. As recommended, we write all 1s and see
141 * what we get back.
142 */
143 s = splhigh();
144 address = pci_conf_read(pc, tag, reg);
145 pci_conf_write(pc, tag, reg, 0xffffffff);
146 mask = pci_conf_read(pc, tag, reg);
147 pci_conf_write(pc, tag, reg, address);
148 if (is64bit) {
149 address1 = pci_conf_read(pc, tag, reg + 4);
150 pci_conf_write(pc, tag, reg + 4, 0xffffffff);
151 mask1 = pci_conf_read(pc, tag, reg + 4);
152 pci_conf_write(pc, tag, reg + 4, address1);
153 }
154 splx(s);
155
156 if (!isrom) {
157 /*
158 * roms should have an enable bit instead of a memory
159 * type decoder bit. For normal BARs, make sure that
160 * the address decoder type matches what we asked for.
161 */
162 if (PCI_MAPREG_TYPE(address) != PCI_MAPREG_TYPE_MEM) {
163 printf("pci_mem_find: expected type mem, found i/o\n");
164 return (1);
165 }
166 if (PCI_MAPREG_MEM_TYPE(address) !=
167 PCI_MAPREG_MEM_TYPE(type)) {
168 printf("pci_mem_find: "
169 "expected mem type %08x, found %08x\n",
170 PCI_MAPREG_MEM_TYPE(type),
171 PCI_MAPREG_MEM_TYPE(address));
172 return (1);
173 }
174 }
175
176 waddress = (u_int64_t)address1 << 32UL | address;
177 wmask = (u_int64_t)mask1 << 32UL | mask;
178
179 if ((is64bit && PCI_MAPREG_MEM64_SIZE(wmask) == 0) ||
180 (!is64bit && PCI_MAPREG_MEM_SIZE(mask) == 0)) {
181 printf("pci_mem_find: void region\n");
182 return (1);
183 }
184
185 switch (PCI_MAPREG_MEM_TYPE(address)) {
186 case PCI_MAPREG_MEM_TYPE_32BIT:
187 case PCI_MAPREG_MEM_TYPE_32BIT_1M:
188 break;
189 case PCI_MAPREG_MEM_TYPE_64BIT:
190 /*
191 * Handle the case of a 64-bit memory register on a
192 * platform with 32-bit addressing. Make sure that
193 * the address assigned and the device's memory size
194 * fit in 32 bits. We implicitly assume that if
195 * bus_addr_t is 64-bit, then so is bus_size_t.
196 */
197 if (sizeof(u_int64_t) > sizeof(bus_addr_t) &&
198 (address1 != 0 || mask1 != 0xffffffff)) {
199 printf("pci_mem_find: 64-bit memory map which is "
200 "inaccessible on a 32-bit platform\n");
201 return (1);
202 }
203 break;
204 default:
205 printf("pci_mem_find: reserved mapping register type\n");
206 return (1);
207 }
208
209 if (sizeof(u_int64_t) > sizeof(bus_addr_t)) {
210 if (basep != 0)
211 *basep = PCI_MAPREG_MEM_ADDR(address);
212 if (sizep != 0)
213 *sizep = PCI_MAPREG_MEM_SIZE(mask);
214 } else {
215 if (basep != 0)
216 *basep = PCI_MAPREG_MEM64_ADDR(waddress);
217 if (sizep != 0)
218 *sizep = PCI_MAPREG_MEM64_SIZE(wmask);
219 }
220 if (flagsp != 0)
221 *flagsp = (isrom || PCI_MAPREG_MEM_PREFETCHABLE(address)) ?
222 BUS_SPACE_MAP_PREFETCHABLE : 0;
223
224 return (0);
225 }
226
227 #define _PCI_MAPREG_TYPEBITS(reg) \
228 (PCI_MAPREG_TYPE(reg) == PCI_MAPREG_TYPE_IO ? \
229 reg & PCI_MAPREG_TYPE_MASK : \
230 reg & (PCI_MAPREG_TYPE_MASK|PCI_MAPREG_MEM_TYPE_MASK))
231
232 pcireg_t
233 pci_mapreg_type(pci_chipset_tag_t pc, pcitag_t tag, int reg)
234 {
235
236 return (_PCI_MAPREG_TYPEBITS(pci_conf_read(pc, tag, reg)));
237 }
238
239 int
240 pci_mapreg_probe(pci_chipset_tag_t pc, pcitag_t tag, int reg, pcireg_t *typep)
241 {
242 pcireg_t address, mask;
243 int s;
244
245 s = splhigh();
246 address = pci_conf_read(pc, tag, reg);
247 pci_conf_write(pc, tag, reg, 0xffffffff);
248 mask = pci_conf_read(pc, tag, reg);
249 pci_conf_write(pc, tag, reg, address);
250 splx(s);
251
252 if (mask == 0) /* unimplemented mapping register */
253 return (0);
254
255 if (typep)
256 *typep = _PCI_MAPREG_TYPEBITS(address);
257 return (1);
258 }
259
260 int
261 pci_mapreg_info(pci_chipset_tag_t pc, pcitag_t tag, int reg, pcireg_t type,
262 bus_addr_t *basep, bus_size_t *sizep, int *flagsp)
263 {
264
265 if (PCI_MAPREG_TYPE(type) == PCI_MAPREG_TYPE_IO)
266 return (pci_io_find(pc, tag, reg, type, basep, sizep,
267 flagsp));
268 else
269 return (pci_mem_find(pc, tag, reg, type, basep, sizep,
270 flagsp));
271 }
272
273 int
274 pci_mapreg_map(struct pci_attach_args *pa, int reg, pcireg_t type,
275 int busflags, bus_space_tag_t *tagp, bus_space_handle_t *handlep,
276 bus_addr_t *basep, bus_size_t *sizep)
277 {
278 bus_space_tag_t tag;
279 bus_space_handle_t handle;
280 bus_addr_t base;
281 bus_size_t size;
282 int flags;
283
284 if (PCI_MAPREG_TYPE(type) == PCI_MAPREG_TYPE_IO) {
285 if ((pa->pa_flags & PCI_FLAGS_IO_ENABLED) == 0)
286 return (1);
287 if (pci_io_find(pa->pa_pc, pa->pa_tag, reg, type, &base,
288 &size, &flags))
289 return (1);
290 tag = pa->pa_iot;
291 } else {
292 if ((pa->pa_flags & PCI_FLAGS_MEM_ENABLED) == 0)
293 return (1);
294 if (pci_mem_find(pa->pa_pc, pa->pa_tag, reg, type, &base,
295 &size, &flags))
296 return (1);
297 tag = pa->pa_memt;
298 }
299
300 if (reg == PCI_MAPREG_ROM) {
301 pcireg_t mask;
302 int s;
303 /* we have to enable the ROM address decoder... */
304 s = splhigh();
305 mask = pci_conf_read(pa->pa_pc, pa->pa_tag, reg);
306 mask |= PCI_MAPREG_ROM_ENABLE;
307 pci_conf_write(pa->pa_pc, pa->pa_tag, reg, mask);
308 splx(s);
309 }
310
311 if (bus_space_map(tag, base, size, busflags | flags, &handle))
312 return (1);
313
314 if (tagp != 0)
315 *tagp = tag;
316 if (handlep != 0)
317 *handlep = handle;
318 if (basep != 0)
319 *basep = base;
320 if (sizep != 0)
321 *sizep = size;
322
323 return (0);
324 }
325
326 int
327 pci_find_rom(struct pci_attach_args *pa, bus_space_tag_t bst,
328 bus_space_handle_t bsh, int type, bus_space_handle_t *romh, bus_size_t *sz)
329 {
330 bus_size_t romsz, offset = 0, imagesz;
331 uint16_t ptr;
332 int done = 0;
333
334 if (pci_mem_find(pa->pa_pc, pa->pa_tag, PCI_MAPREG_ROM,
335 PCI_MAPREG_TYPE_ROM, NULL, &romsz, NULL))
336 return 1;
337
338 /*
339 * no upper bound check; i cannot imagine a 4GB ROM, but
340 * it appears the spec would allow it!
341 */
342 if (romsz < 1024)
343 return 1;
344
345 while (offset < romsz && !done){
346 struct pci_rom_header hdr;
347 struct pci_rom rom;
348
349 hdr.romh_magic = bus_space_read_2(bst, bsh,
350 offset + offsetof (struct pci_rom_header, romh_magic));
351 hdr.romh_data_ptr = bus_space_read_2(bst, bsh,
352 offset + offsetof (struct pci_rom_header, romh_data_ptr));
353
354 /* no warning: quite possibly ROM is simply not populated */
355 if (hdr.romh_magic != PCI_ROM_HEADER_MAGIC)
356 return 1;
357
358 ptr = offset + hdr.romh_data_ptr;
359
360 if (ptr > romsz) {
361 printf("pci_find_rom: rom data ptr out of range\n");
362 return 1;
363 }
364
365 rom.rom_signature = bus_space_read_4(bst, bsh, ptr);
366 rom.rom_vendor = bus_space_read_2(bst, bsh, ptr +
367 offsetof(struct pci_rom, rom_vendor));
368 rom.rom_product = bus_space_read_2(bst, bsh, ptr +
369 offsetof(struct pci_rom, rom_product));
370 rom.rom_class = bus_space_read_1(bst, bsh,
371 ptr + offsetof (struct pci_rom, rom_class));
372 rom.rom_subclass = bus_space_read_1(bst, bsh,
373 ptr + offsetof (struct pci_rom, rom_subclass));
374 rom.rom_interface = bus_space_read_1(bst, bsh,
375 ptr + offsetof (struct pci_rom, rom_interface));
376 rom.rom_len = bus_space_read_2(bst, bsh,
377 ptr + offsetof (struct pci_rom, rom_len));
378 rom.rom_code_type = bus_space_read_1(bst, bsh,
379 ptr + offsetof (struct pci_rom, rom_code_type));
380 rom.rom_indicator = bus_space_read_1(bst, bsh,
381 ptr + offsetof (struct pci_rom, rom_indicator));
382
383 if (rom.rom_signature != PCI_ROM_SIGNATURE) {
384 printf("pci_find_rom: bad rom data signature\n");
385 return 1;
386 }
387
388 imagesz = rom.rom_len * 512;
389
390 if ((rom.rom_vendor == PCI_VENDOR(pa->pa_id)) &&
391 (rom.rom_product == PCI_PRODUCT(pa->pa_id)) &&
392 (rom.rom_class == PCI_CLASS(pa->pa_class)) &&
393 (rom.rom_subclass == PCI_SUBCLASS(pa->pa_class)) &&
394 (rom.rom_interface == PCI_INTERFACE(pa->pa_class)) &&
395 (rom.rom_code_type == type)) {
396 *sz = imagesz;
397 bus_space_subregion(bst, bsh, offset, imagesz, romh);
398 return 0;
399 }
400
401 /* last image check */
402 if (rom.rom_indicator & PCI_ROM_INDICATOR_LAST)
403 return 1;
404
405 /* offset by size */
406 offset += imagesz;
407 }
408 return 1;
409 }
410