1 /* $NetBSD: pci_map.c,v 1.23 2008/04/28 20:23:55 martin Exp $ */
4 * Copyright (c) 1998, 2000 The NetBSD Foundation, Inc.
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.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: pci_map.c,v 1.23 2008/04/28 20:23:55 martin Exp $");
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/device.h>
43 #include <dev/pci/pcireg.h>
44 #include <dev/pci/pcivar.h>
47 pci_io_find(pci_chipset_tag_t pc
, pcitag_t tag
, int reg
, pcireg_t type
,
48 bus_addr_t
*basep
, bus_size_t
*sizep
, int *flagsp
)
50 pcireg_t address
, mask
;
53 if (reg
< PCI_MAPREG_START
||
56 * Can't do this check; some devices have mapping registers
57 * way out in left field.
59 reg
>= PCI_MAPREG_END
||
62 panic("pci_io_find: bad request");
65 * Section 6.2.5.1, `Address Maps', tells us that:
67 * 1) The builtin software should have already mapped the device in a
70 * 2) A device which wants 2^n bytes of memory will hardwire the bottom
71 * n bits of the address to 0. As recommended, we write all 1s and see
75 address
= pci_conf_read(pc
, tag
, reg
);
76 pci_conf_write(pc
, tag
, reg
, 0xffffffff);
77 mask
= pci_conf_read(pc
, tag
, reg
);
78 pci_conf_write(pc
, tag
, reg
, address
);
81 if (PCI_MAPREG_TYPE(address
) != PCI_MAPREG_TYPE_IO
) {
82 aprint_debug("pci_io_find: expected type i/o, found mem\n");
86 if (PCI_MAPREG_IO_SIZE(mask
) == 0) {
87 aprint_debug("pci_io_find: void region\n");
92 *basep
= PCI_MAPREG_IO_ADDR(address
);
94 *sizep
= PCI_MAPREG_IO_SIZE(mask
);
102 pci_mem_find(pci_chipset_tag_t pc
, pcitag_t tag
, int reg
, pcireg_t type
,
103 bus_addr_t
*basep
, bus_size_t
*sizep
, int *flagsp
)
105 pcireg_t address
, mask
, address1
= 0, mask1
= 0xffffffff;
106 u_int64_t waddress
, wmask
;
107 int s
, is64bit
, isrom
;
109 is64bit
= (PCI_MAPREG_MEM_TYPE(type
) == PCI_MAPREG_MEM_TYPE_64BIT
);
110 isrom
= (reg
== PCI_MAPREG_ROM
);
112 if ((!isrom
) && (reg
< PCI_MAPREG_START
||
115 * Can't do this check; some devices have mapping registers
116 * way out in left field.
118 reg
>= PCI_MAPREG_END
||
121 panic("pci_mem_find: bad request");
123 if (is64bit
&& (reg
+ 4) >= PCI_MAPREG_END
)
124 panic("pci_mem_find: bad 64-bit request");
127 * Section 6.2.5.1, `Address Maps', tells us that:
129 * 1) The builtin software should have already mapped the device in a
132 * 2) A device which wants 2^n bytes of memory will hardwire the bottom
133 * n bits of the address to 0. As recommended, we write all 1s and see
137 address
= pci_conf_read(pc
, tag
, reg
);
138 pci_conf_write(pc
, tag
, reg
, 0xffffffff);
139 mask
= pci_conf_read(pc
, tag
, reg
);
140 pci_conf_write(pc
, tag
, reg
, address
);
142 address1
= pci_conf_read(pc
, tag
, reg
+ 4);
143 pci_conf_write(pc
, tag
, reg
+ 4, 0xffffffff);
144 mask1
= pci_conf_read(pc
, tag
, reg
+ 4);
145 pci_conf_write(pc
, tag
, reg
+ 4, address1
);
151 * roms should have an enable bit instead of a memory
152 * type decoder bit. For normal BARs, make sure that
153 * the address decoder type matches what we asked for.
155 if (PCI_MAPREG_TYPE(address
) != PCI_MAPREG_TYPE_MEM
) {
156 printf("pci_mem_find: expected type mem, found i/o\n");
159 /* XXX Allow 64bit bars for 32bit requests.*/
160 if (PCI_MAPREG_MEM_TYPE(address
) !=
161 PCI_MAPREG_MEM_TYPE(type
) &&
162 PCI_MAPREG_MEM_TYPE(address
) !=
163 PCI_MAPREG_MEM_TYPE_64BIT
) {
164 printf("pci_mem_find: "
165 "expected mem type %08x, found %08x\n",
166 PCI_MAPREG_MEM_TYPE(type
),
167 PCI_MAPREG_MEM_TYPE(address
));
172 waddress
= (u_int64_t
)address1
<< 32UL | address
;
173 wmask
= (u_int64_t
)mask1
<< 32UL | mask
;
175 if ((is64bit
&& PCI_MAPREG_MEM64_SIZE(wmask
) == 0) ||
176 (!is64bit
&& PCI_MAPREG_MEM_SIZE(mask
) == 0)) {
177 aprint_debug("pci_mem_find: void region\n");
181 switch (PCI_MAPREG_MEM_TYPE(address
)) {
182 case PCI_MAPREG_MEM_TYPE_32BIT
:
183 case PCI_MAPREG_MEM_TYPE_32BIT_1M
:
185 case PCI_MAPREG_MEM_TYPE_64BIT
:
187 * Handle the case of a 64-bit memory register on a
188 * platform with 32-bit addressing. Make sure that
189 * the address assigned and the device's memory size
190 * fit in 32 bits. We implicitly assume that if
191 * bus_addr_t is 64-bit, then so is bus_size_t.
193 if (sizeof(u_int64_t
) > sizeof(bus_addr_t
) &&
194 (address1
!= 0 || mask1
!= 0xffffffff)) {
195 printf("pci_mem_find: 64-bit memory map which is "
196 "inaccessible on a 32-bit platform\n");
201 printf("pci_mem_find: reserved mapping register type\n");
205 if (sizeof(u_int64_t
) > sizeof(bus_addr_t
)) {
207 *basep
= PCI_MAPREG_MEM_ADDR(address
);
209 *sizep
= PCI_MAPREG_MEM_SIZE(mask
);
212 *basep
= PCI_MAPREG_MEM64_ADDR(waddress
);
214 *sizep
= PCI_MAPREG_MEM64_SIZE(wmask
);
217 *flagsp
= (isrom
|| PCI_MAPREG_MEM_PREFETCHABLE(address
)) ?
218 BUS_SPACE_MAP_PREFETCHABLE
: 0;
223 #define _PCI_MAPREG_TYPEBITS(reg) \
224 (PCI_MAPREG_TYPE(reg) == PCI_MAPREG_TYPE_IO ? \
225 reg & PCI_MAPREG_TYPE_MASK : \
226 reg & (PCI_MAPREG_TYPE_MASK|PCI_MAPREG_MEM_TYPE_MASK))
229 pci_mapreg_type(pci_chipset_tag_t pc
, pcitag_t tag
, int reg
)
232 return (_PCI_MAPREG_TYPEBITS(pci_conf_read(pc
, tag
, reg
)));
236 pci_mapreg_probe(pci_chipset_tag_t pc
, pcitag_t tag
, int reg
, pcireg_t
*typep
)
238 pcireg_t address
, mask
;
242 address
= pci_conf_read(pc
, tag
, reg
);
243 pci_conf_write(pc
, tag
, reg
, 0xffffffff);
244 mask
= pci_conf_read(pc
, tag
, reg
);
245 pci_conf_write(pc
, tag
, reg
, address
);
248 if (mask
== 0) /* unimplemented mapping register */
252 *typep
= _PCI_MAPREG_TYPEBITS(address
);
257 pci_mapreg_info(pci_chipset_tag_t pc
, pcitag_t tag
, int reg
, pcireg_t type
,
258 bus_addr_t
*basep
, bus_size_t
*sizep
, int *flagsp
)
261 if (PCI_MAPREG_TYPE(type
) == PCI_MAPREG_TYPE_IO
)
262 return (pci_io_find(pc
, tag
, reg
, type
, basep
, sizep
,
265 return (pci_mem_find(pc
, tag
, reg
, type
, basep
, sizep
,
270 pci_mapreg_map(struct pci_attach_args
*pa
, int reg
, pcireg_t type
,
271 int busflags
, bus_space_tag_t
*tagp
, bus_space_handle_t
*handlep
,
272 bus_addr_t
*basep
, bus_size_t
*sizep
)
274 return pci_mapreg_submap(pa
, reg
, type
, busflags
, 0, 0, tagp
,
275 handlep
, basep
, sizep
);
279 pci_mapreg_submap(struct pci_attach_args
*pa
, int reg
, pcireg_t type
,
280 int busflags
, bus_size_t maxsize
, bus_size_t offset
, bus_space_tag_t
*tagp
,
281 bus_space_handle_t
*handlep
, bus_addr_t
*basep
, bus_size_t
*sizep
)
284 bus_space_handle_t handle
;
289 if (PCI_MAPREG_TYPE(type
) == PCI_MAPREG_TYPE_IO
) {
290 if ((pa
->pa_flags
& PCI_FLAGS_IO_ENABLED
) == 0)
292 if (pci_io_find(pa
->pa_pc
, pa
->pa_tag
, reg
, type
, &base
,
297 if ((pa
->pa_flags
& PCI_FLAGS_MEM_ENABLED
) == 0)
299 if (pci_mem_find(pa
->pa_pc
, pa
->pa_tag
, reg
, type
, &base
,
305 if (reg
== PCI_MAPREG_ROM
) {
308 /* we have to enable the ROM address decoder... */
310 mask
= pci_conf_read(pa
->pa_pc
, pa
->pa_tag
, reg
);
311 mask
|= PCI_MAPREG_ROM_ENABLE
;
312 pci_conf_write(pa
->pa_pc
, pa
->pa_tag
, reg
, mask
);
316 /* If we're called with maxsize/offset of 0, behave like
320 maxsize
= (maxsize
&& offset
) ? maxsize
: size
;
323 if ((maxsize
< size
&& offset
+ maxsize
<= size
) || offset
!= 0)
326 if (bus_space_map(tag
, base
, maxsize
, busflags
| flags
, &handle
))
342 pci_find_rom(struct pci_attach_args
*pa
, bus_space_tag_t bst
,
343 bus_space_handle_t bsh
, int type
, bus_space_handle_t
*romh
, bus_size_t
*sz
)
345 bus_size_t romsz
, offset
= 0, imagesz
;
349 if (pci_mem_find(pa
->pa_pc
, pa
->pa_tag
, PCI_MAPREG_ROM
,
350 PCI_MAPREG_TYPE_ROM
, NULL
, &romsz
, NULL
))
354 * no upper bound check; i cannot imagine a 4GB ROM, but
355 * it appears the spec would allow it!
360 while (offset
< romsz
&& !done
){
361 struct pci_rom_header hdr
;
364 hdr
.romh_magic
= bus_space_read_2(bst
, bsh
,
365 offset
+ offsetof (struct pci_rom_header
, romh_magic
));
366 hdr
.romh_data_ptr
= bus_space_read_2(bst
, bsh
,
367 offset
+ offsetof (struct pci_rom_header
, romh_data_ptr
));
369 /* no warning: quite possibly ROM is simply not populated */
370 if (hdr
.romh_magic
!= PCI_ROM_HEADER_MAGIC
)
373 ptr
= offset
+ hdr
.romh_data_ptr
;
376 printf("pci_find_rom: rom data ptr out of range\n");
380 rom
.rom_signature
= bus_space_read_4(bst
, bsh
, ptr
);
381 rom
.rom_vendor
= bus_space_read_2(bst
, bsh
, ptr
+
382 offsetof(struct pci_rom
, rom_vendor
));
383 rom
.rom_product
= bus_space_read_2(bst
, bsh
, ptr
+
384 offsetof(struct pci_rom
, rom_product
));
385 rom
.rom_class
= bus_space_read_1(bst
, bsh
,
386 ptr
+ offsetof (struct pci_rom
, rom_class
));
387 rom
.rom_subclass
= bus_space_read_1(bst
, bsh
,
388 ptr
+ offsetof (struct pci_rom
, rom_subclass
));
389 rom
.rom_interface
= bus_space_read_1(bst
, bsh
,
390 ptr
+ offsetof (struct pci_rom
, rom_interface
));
391 rom
.rom_len
= bus_space_read_2(bst
, bsh
,
392 ptr
+ offsetof (struct pci_rom
, rom_len
));
393 rom
.rom_code_type
= bus_space_read_1(bst
, bsh
,
394 ptr
+ offsetof (struct pci_rom
, rom_code_type
));
395 rom
.rom_indicator
= bus_space_read_1(bst
, bsh
,
396 ptr
+ offsetof (struct pci_rom
, rom_indicator
));
398 if (rom
.rom_signature
!= PCI_ROM_SIGNATURE
) {
399 printf("pci_find_rom: bad rom data signature\n");
403 imagesz
= rom
.rom_len
* 512;
405 if ((rom
.rom_vendor
== PCI_VENDOR(pa
->pa_id
)) &&
406 (rom
.rom_product
== PCI_PRODUCT(pa
->pa_id
)) &&
407 (rom
.rom_class
== PCI_CLASS(pa
->pa_class
)) &&
408 (rom
.rom_subclass
== PCI_SUBCLASS(pa
->pa_class
)) &&
409 (rom
.rom_interface
== PCI_INTERFACE(pa
->pa_class
)) &&
410 (rom
.rom_code_type
== type
)) {
412 bus_space_subregion(bst
, bsh
, offset
, imagesz
, romh
);
416 /* last image check */
417 if (rom
.rom_indicator
& PCI_ROM_INDICATOR_LAST
)