pci_addr_fixup.c revision 1.3 1 1.2 cegger /* $NetBSD: pci_addr_fixup.c,v 1.3 2008/12/19 12:58:43 cegger Exp $ */
2 1.1 jmcneill
3 1.1 jmcneill /*-
4 1.1 jmcneill * Copyright (c) 2000 UCHIYAMA Yasushi. All rights reserved.
5 1.1 jmcneill *
6 1.1 jmcneill * Redistribution and use in source and binary forms, with or without
7 1.1 jmcneill * modification, are permitted provided that the following conditions
8 1.1 jmcneill * are met:
9 1.1 jmcneill * 1. Redistributions of source code must retain the above copyright
10 1.1 jmcneill * notice, this list of conditions and the following disclaimer.
11 1.1 jmcneill * 2. Redistributions in binary form must reproduce the above copyright
12 1.1 jmcneill * notice, this list of conditions and the following disclaimer in the
13 1.1 jmcneill * documentation and/or other materials provided with the distribution.
14 1.1 jmcneill * 3. The name of the author may not be used to endorse or promote products
15 1.1 jmcneill * derived from this software without specific prior written permission.
16 1.1 jmcneill *
17 1.1 jmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 1.1 jmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 1.1 jmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 1.1 jmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 1.1 jmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 1.1 jmcneill * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 1.1 jmcneill * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 1.1 jmcneill * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 1.1 jmcneill * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 1.1 jmcneill * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 1.1 jmcneill */
28 1.1 jmcneill
29 1.1 jmcneill #include <sys/cdefs.h>
30 1.2 cegger __KERNEL_RCSID(0, "$NetBSD: pci_addr_fixup.c,v 1.3 2008/12/19 12:58:43 cegger Exp $");
31 1.1 jmcneill
32 1.1 jmcneill #include <sys/param.h>
33 1.1 jmcneill #include <sys/systm.h>
34 1.3 cegger #include <sys/malloc.h>
35 1.1 jmcneill #include <sys/kernel.h>
36 1.1 jmcneill #include <sys/device.h>
37 1.1 jmcneill #include <sys/extent.h>
38 1.1 jmcneill
39 1.1 jmcneill #include <machine/bus.h>
40 1.1 jmcneill
41 1.1 jmcneill #include <dev/pci/pcireg.h>
42 1.1 jmcneill #include <dev/pci/pcivar.h>
43 1.1 jmcneill #include <dev/pci/pcidevs.h>
44 1.1 jmcneill
45 1.1 jmcneill #include <x86/pci/pci_addr_fixup.h>
46 1.1 jmcneill
47 1.1 jmcneill struct pciaddr pciaddr;
48 1.1 jmcneill
49 1.1 jmcneill static int pciaddrverbose = 0;
50 1.1 jmcneill
51 1.1 jmcneill void pciaddr_resource_reserve(pci_chipset_tag_t, pcitag_t, void *context);
52 1.1 jmcneill int pciaddr_do_resource_reserve(pci_chipset_tag_t, pcitag_t, int,
53 1.1 jmcneill void *, int, bus_addr_t *, bus_size_t);
54 1.1 jmcneill void pciaddr_resource_allocate(pci_chipset_tag_t, pcitag_t, void *context);
55 1.1 jmcneill int pciaddr_do_resource_allocate(pci_chipset_tag_t, pcitag_t, int,
56 1.1 jmcneill void *, int, bus_addr_t *, bus_size_t);
57 1.1 jmcneill int device_is_agp(pci_chipset_tag_t, pcitag_t);
58 1.1 jmcneill
59 1.1 jmcneill int device_is_agp(pci_chipset_tag_t, pcitag_t);
60 1.1 jmcneill
61 1.1 jmcneill #define PCIADDR_MEM_START 0x0
62 1.1 jmcneill #define PCIADDR_MEM_END 0xffffffff
63 1.1 jmcneill #define PCIADDR_PORT_START 0x0
64 1.1 jmcneill #define PCIADDR_PORT_END 0xffff
65 1.1 jmcneill
66 1.1 jmcneill /* for ISA devices */
67 1.1 jmcneill #define PCIADDR_ISAPORT_RESERVE 0x5800 /* empirical value */
68 1.1 jmcneill #define PCIADDR_ISAMEM_RESERVE (16 * 1024 * 1024)
69 1.1 jmcneill
70 1.1 jmcneill void
71 1.1 jmcneill pci_addr_fixup(pci_chipset_tag_t pc, int maxbus)
72 1.1 jmcneill {
73 1.1 jmcneill extern paddr_t avail_end;
74 1.1 jmcneill const char *verbose_header =
75 1.1 jmcneill "[%s]-----------------------\n"
76 1.1 jmcneill " device vendor product\n"
77 1.1 jmcneill " register space address size\n"
78 1.1 jmcneill "--------------------------------------------\n";
79 1.1 jmcneill const char *verbose_footer =
80 1.1 jmcneill "--------------------------[%3d devices bogus]\n";
81 1.1 jmcneill const struct {
82 1.1 jmcneill bus_addr_t start;
83 1.1 jmcneill bus_size_t size;
84 1.1 jmcneill const char *name;
85 1.1 jmcneill } system_reserve [] = {
86 1.1 jmcneill { 0xfec00000, 0x100000, "I/O APIC" },
87 1.1 jmcneill { 0xfee00000, 0x100000, "Local APIC" },
88 1.1 jmcneill { 0xfffe0000, 0x20000, "BIOS PROM" },
89 1.1 jmcneill { 0, 0, 0 }, /* terminator */
90 1.1 jmcneill }, *srp;
91 1.1 jmcneill paddr_t start;
92 1.1 jmcneill int error;
93 1.1 jmcneill
94 1.1 jmcneill pciaddr.extent_mem = extent_create("PCI I/O memory space",
95 1.1 jmcneill PCIADDR_MEM_START,
96 1.1 jmcneill PCIADDR_MEM_END,
97 1.1 jmcneill M_DEVBUF, 0, 0, EX_NOWAIT);
98 1.1 jmcneill KASSERT(pciaddr.extent_mem);
99 1.1 jmcneill pciaddr.extent_port = extent_create("PCI I/O port space",
100 1.1 jmcneill PCIADDR_PORT_START,
101 1.1 jmcneill PCIADDR_PORT_END,
102 1.1 jmcneill M_DEVBUF, 0, 0, EX_NOWAIT);
103 1.1 jmcneill KASSERT(pciaddr.extent_port);
104 1.1 jmcneill
105 1.1 jmcneill /*
106 1.1 jmcneill * 1. check & reserve system BIOS setting.
107 1.1 jmcneill */
108 1.1 jmcneill aprint_debug(verbose_header, "System BIOS Setting");
109 1.1 jmcneill pci_device_foreach(pc, maxbus, pciaddr_resource_reserve, NULL);
110 1.1 jmcneill aprint_debug(verbose_footer, pciaddr.nbogus);
111 1.1 jmcneill
112 1.1 jmcneill /*
113 1.1 jmcneill * 2. reserve non-PCI area.
114 1.1 jmcneill */
115 1.1 jmcneill for (srp = system_reserve; srp->size; srp++) {
116 1.1 jmcneill error = extent_alloc_region(pciaddr.extent_mem, srp->start,
117 1.1 jmcneill srp->size,
118 1.1 jmcneill EX_NOWAIT| EX_MALLOCOK);
119 1.1 jmcneill if (error != 0) {
120 1.1 jmcneill printf("WARNING: can't reserve area for %s.\n",
121 1.1 jmcneill srp->name);
122 1.1 jmcneill }
123 1.1 jmcneill }
124 1.1 jmcneill
125 1.1 jmcneill /*
126 1.1 jmcneill * 3. determine allocation space
127 1.1 jmcneill */
128 1.1 jmcneill start = x86_round_page(avail_end + 1);
129 1.1 jmcneill if (start < PCIADDR_ISAMEM_RESERVE)
130 1.1 jmcneill start = PCIADDR_ISAMEM_RESERVE;
131 1.1 jmcneill pciaddr.mem_alloc_start = (start + 0x100000 + 1) & ~(0x100000 - 1);
132 1.1 jmcneill pciaddr.port_alloc_start = PCIADDR_ISAPORT_RESERVE;
133 1.1 jmcneill aprint_debug(" Physical memory end: 0x%08x\n PCI memory mapped I/O "
134 1.1 jmcneill "space start: 0x%08x\n", (unsigned)avail_end,
135 1.1 jmcneill (unsigned)pciaddr.mem_alloc_start);
136 1.1 jmcneill
137 1.1 jmcneill if (pciaddr.nbogus == 0)
138 1.1 jmcneill return; /* no need to fixup */
139 1.1 jmcneill
140 1.1 jmcneill /*
141 1.1 jmcneill * 4. do fixup
142 1.1 jmcneill */
143 1.1 jmcneill aprint_debug(verbose_header, "PCIBIOS fixup stage");
144 1.1 jmcneill pciaddr.nbogus = 0;
145 1.1 jmcneill pci_device_foreach_min(pc, 0, maxbus, pciaddr_resource_allocate, NULL);
146 1.1 jmcneill aprint_debug(verbose_footer, pciaddr.nbogus);
147 1.1 jmcneill
148 1.1 jmcneill }
149 1.1 jmcneill
150 1.1 jmcneill void
151 1.1 jmcneill pciaddr_resource_reserve(pci_chipset_tag_t pc, pcitag_t tag,
152 1.1 jmcneill void *context)
153 1.1 jmcneill {
154 1.1 jmcneill if (pciaddrverbose)
155 1.1 jmcneill pciaddr_print_devid(pc, tag);
156 1.1 jmcneill pciaddr_resource_manage(pc, tag,
157 1.1 jmcneill pciaddr_do_resource_reserve,
158 1.1 jmcneill &pciaddr);
159 1.1 jmcneill }
160 1.1 jmcneill
161 1.1 jmcneill void
162 1.1 jmcneill pciaddr_resource_allocate(pci_chipset_tag_t pc, pcitag_t tag,
163 1.1 jmcneill void *context)
164 1.1 jmcneill {
165 1.1 jmcneill if (pciaddrverbose)
166 1.1 jmcneill pciaddr_print_devid(pc, tag);
167 1.1 jmcneill pciaddr_resource_manage(pc, tag,
168 1.1 jmcneill pciaddr_do_resource_allocate,
169 1.1 jmcneill &pciaddr);
170 1.1 jmcneill }
171 1.1 jmcneill
172 1.1 jmcneill void
173 1.1 jmcneill pciaddr_resource_manage(pci_chipset_tag_t pc, pcitag_t tag,
174 1.1 jmcneill pciaddr_resource_manage_func_t func, void *ctx)
175 1.1 jmcneill {
176 1.1 jmcneill pcireg_t val, mask;
177 1.1 jmcneill bus_addr_t addr;
178 1.1 jmcneill bus_size_t size;
179 1.1 jmcneill int error, useport, usemem, mapreg, type, reg_start, reg_end, width;
180 1.1 jmcneill
181 1.1 jmcneill val = pci_conf_read(pc, tag, PCI_BHLC_REG);
182 1.1 jmcneill switch (PCI_HDRTYPE_TYPE(val)) {
183 1.1 jmcneill default:
184 1.1 jmcneill printf("WARNING: unknown PCI device header.");
185 1.1 jmcneill pciaddr.nbogus++;
186 1.1 jmcneill return;
187 1.1 jmcneill case 0:
188 1.1 jmcneill reg_start = PCI_MAPREG_START;
189 1.1 jmcneill reg_end = PCI_MAPREG_END;
190 1.1 jmcneill break;
191 1.1 jmcneill case 1: /* PCI-PCI bridge */
192 1.1 jmcneill reg_start = PCI_MAPREG_START;
193 1.1 jmcneill reg_end = PCI_MAPREG_PPB_END;
194 1.1 jmcneill break;
195 1.1 jmcneill case 2: /* PCI-CardBus bridge */
196 1.1 jmcneill reg_start = PCI_MAPREG_START;
197 1.1 jmcneill reg_end = PCI_MAPREG_PCB_END;
198 1.1 jmcneill break;
199 1.1 jmcneill }
200 1.1 jmcneill error = useport = usemem = 0;
201 1.1 jmcneill
202 1.1 jmcneill for (mapreg = reg_start; mapreg < reg_end; mapreg += width) {
203 1.1 jmcneill /* inquire PCI device bus space requirement */
204 1.1 jmcneill val = pci_conf_read(pc, tag, mapreg);
205 1.1 jmcneill pci_conf_write(pc, tag, mapreg, ~0);
206 1.1 jmcneill
207 1.1 jmcneill mask = pci_conf_read(pc, tag, mapreg);
208 1.1 jmcneill pci_conf_write(pc, tag, mapreg, val);
209 1.1 jmcneill
210 1.1 jmcneill type = PCI_MAPREG_TYPE(val);
211 1.1 jmcneill width = 4;
212 1.1 jmcneill if (type == PCI_MAPREG_TYPE_MEM) {
213 1.1 jmcneill if (PCI_MAPREG_MEM_TYPE(val) ==
214 1.1 jmcneill PCI_MAPREG_MEM_TYPE_64BIT) {
215 1.1 jmcneill /* XXX We could examine the upper 32 bits
216 1.1 jmcneill * XXX of the BAR here, but we are totally
217 1.1 jmcneill * XXX unprepared to handle a non-zero value,
218 1.1 jmcneill * XXX either here or anywhere else in
219 1.1 jmcneill * XXX i386-land.
220 1.1 jmcneill * XXX So just arrange to not look at the
221 1.1 jmcneill * XXX upper 32 bits, lest we misinterpret
222 1.1 jmcneill * XXX it as a 32-bit BAR set to zero.
223 1.1 jmcneill */
224 1.1 jmcneill width = 8;
225 1.1 jmcneill }
226 1.1 jmcneill size = PCI_MAPREG_MEM_SIZE(mask);
227 1.1 jmcneill } else {
228 1.1 jmcneill size = PCI_MAPREG_IO_SIZE(mask);
229 1.1 jmcneill }
230 1.1 jmcneill addr = pciaddr_ioaddr(val);
231 1.1 jmcneill
232 1.1 jmcneill if (!size) /* unused register */
233 1.1 jmcneill continue;
234 1.1 jmcneill
235 1.1 jmcneill if (type == PCI_MAPREG_TYPE_MEM)
236 1.1 jmcneill ++usemem;
237 1.1 jmcneill else
238 1.1 jmcneill ++useport;
239 1.1 jmcneill
240 1.1 jmcneill /* reservation/allocation phase */
241 1.1 jmcneill error += (*func) (pc, tag, mapreg, ctx, type, &addr, size);
242 1.1 jmcneill
243 1.1 jmcneill aprint_debug("\n\t%02xh %s 0x%08x 0x%08x",
244 1.1 jmcneill mapreg, type ? "port" : "mem ",
245 1.1 jmcneill (unsigned int)addr, (unsigned int)size);
246 1.1 jmcneill }
247 1.1 jmcneill
248 1.1 jmcneill /* enable/disable PCI device */
249 1.1 jmcneill val = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
250 1.1 jmcneill if (error == 0)
251 1.1 jmcneill val |= (PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MEM_ENABLE |
252 1.1 jmcneill PCI_COMMAND_MASTER_ENABLE);
253 1.1 jmcneill else
254 1.1 jmcneill val &= ~(PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MEM_ENABLE |
255 1.1 jmcneill PCI_COMMAND_MASTER_ENABLE);
256 1.1 jmcneill pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG, val);
257 1.1 jmcneill
258 1.1 jmcneill if (error)
259 1.1 jmcneill pciaddr.nbogus++;
260 1.1 jmcneill
261 1.1 jmcneill aprint_debug("\n\t\t[%s]\n", error ? "NG" : "OK");
262 1.1 jmcneill }
263 1.1 jmcneill
264 1.1 jmcneill int
265 1.1 jmcneill pciaddr_do_resource_allocate(pci_chipset_tag_t pc, pcitag_t tag,
266 1.1 jmcneill int mapreg, void *ctx, int type, bus_addr_t *addr, bus_size_t size)
267 1.1 jmcneill {
268 1.1 jmcneill struct pciaddr *pciaddrmap = (struct pciaddr *)ctx;
269 1.1 jmcneill bus_addr_t start;
270 1.1 jmcneill int error;
271 1.1 jmcneill struct extent *ex;
272 1.1 jmcneill
273 1.1 jmcneill if (*addr) /* no need to allocate */
274 1.1 jmcneill return (0);
275 1.1 jmcneill
276 1.1 jmcneill ex = (type == PCI_MAPREG_TYPE_MEM ?
277 1.1 jmcneill pciaddrmap->extent_mem : pciaddrmap->extent_port);
278 1.1 jmcneill
279 1.1 jmcneill /* XXX Don't allocate if device is AGP device to avoid conflict. */
280 1.1 jmcneill if (device_is_agp(pc, tag))
281 1.1 jmcneill return (0);
282 1.1 jmcneill
283 1.1 jmcneill start = (type == PCI_MAPREG_TYPE_MEM ?
284 1.1 jmcneill pciaddrmap->mem_alloc_start : pciaddrmap->port_alloc_start);
285 1.1 jmcneill
286 1.1 jmcneill if (start < ex->ex_start || start + size - 1 >= ex->ex_end) {
287 1.1 jmcneill aprint_debug("No available resources. fixup failed\n");
288 1.1 jmcneill return (1);
289 1.1 jmcneill }
290 1.1 jmcneill error = extent_alloc_subregion(ex, start, ex->ex_end, size,
291 1.1 jmcneill size, 0,
292 1.1 jmcneill EX_FAST|EX_NOWAIT|EX_MALLOCOK, addr);
293 1.1 jmcneill if (error) {
294 1.1 jmcneill aprint_debug("No available resources. fixup failed\n");
295 1.1 jmcneill return (1);
296 1.1 jmcneill }
297 1.1 jmcneill
298 1.1 jmcneill /* write new address to PCI device configuration header */
299 1.1 jmcneill pci_conf_write(pc, tag, mapreg, *addr);
300 1.1 jmcneill /* check */
301 1.1 jmcneill if (!pciaddrverbose)
302 1.1 jmcneill {
303 1.1 jmcneill printf("pci_addr_fixup: ");
304 1.1 jmcneill pciaddr_print_devid(pc, tag);
305 1.1 jmcneill }
306 1.1 jmcneill if (pciaddr_ioaddr(pci_conf_read(pc, tag, mapreg)) != *addr) {
307 1.1 jmcneill pci_conf_write(pc, tag, mapreg, 0); /* clear */
308 1.1 jmcneill printf("fixup failed. (new address=%#x)\n", (unsigned)*addr);
309 1.1 jmcneill return (1);
310 1.1 jmcneill }
311 1.1 jmcneill if (!pciaddrverbose)
312 1.1 jmcneill printf("new address 0x%08x\n", (unsigned)*addr);
313 1.1 jmcneill
314 1.1 jmcneill return (0);
315 1.1 jmcneill }
316 1.1 jmcneill
317 1.1 jmcneill int
318 1.1 jmcneill pciaddr_do_resource_reserve(pci_chipset_tag_t pc, pcitag_t tag,
319 1.1 jmcneill int mapreg, void *ctx, int type, bus_addr_t *addr, bus_size_t size)
320 1.1 jmcneill {
321 1.1 jmcneill struct extent *ex;
322 1.1 jmcneill struct pciaddr *pciaddrmap = (struct pciaddr *)ctx;
323 1.1 jmcneill int error;
324 1.1 jmcneill
325 1.1 jmcneill if (*addr == 0)
326 1.1 jmcneill return (1);
327 1.1 jmcneill
328 1.1 jmcneill ex = (type == PCI_MAPREG_TYPE_MEM ?
329 1.1 jmcneill pciaddrmap->extent_mem : pciaddrmap->extent_port);
330 1.1 jmcneill
331 1.1 jmcneill error = extent_alloc_region(ex, *addr, size, EX_NOWAIT| EX_MALLOCOK);
332 1.1 jmcneill if (error) {
333 1.1 jmcneill aprint_debug("Resource conflict.\n");
334 1.1 jmcneill pci_conf_write(pc, tag, mapreg, 0); /* clear */
335 1.1 jmcneill return (1);
336 1.1 jmcneill }
337 1.1 jmcneill
338 1.1 jmcneill return (0);
339 1.1 jmcneill }
340 1.1 jmcneill
341 1.1 jmcneill bus_addr_t
342 1.1 jmcneill pciaddr_ioaddr(uint32_t val)
343 1.1 jmcneill {
344 1.1 jmcneill return ((PCI_MAPREG_TYPE(val) == PCI_MAPREG_TYPE_MEM)
345 1.1 jmcneill ? PCI_MAPREG_MEM_ADDR(val)
346 1.1 jmcneill : PCI_MAPREG_IO_ADDR(val));
347 1.1 jmcneill }
348 1.1 jmcneill
349 1.1 jmcneill void
350 1.1 jmcneill pciaddr_print_devid(pci_chipset_tag_t pc, pcitag_t tag)
351 1.1 jmcneill {
352 1.1 jmcneill int bus, device, function;
353 1.1 jmcneill pcireg_t id;
354 1.1 jmcneill
355 1.1 jmcneill id = pci_conf_read(pc, tag, PCI_ID_REG);
356 1.1 jmcneill pci_decompose_tag(pc, tag, &bus, &device, &function);
357 1.1 jmcneill printf("%03d:%02d:%d 0x%04x 0x%04x ", bus, device, function,
358 1.1 jmcneill PCI_VENDOR(id), PCI_PRODUCT(id));
359 1.1 jmcneill }
360 1.1 jmcneill
361 1.1 jmcneill int
362 1.1 jmcneill device_is_agp(pci_chipset_tag_t pc, pcitag_t tag)
363 1.1 jmcneill {
364 1.1 jmcneill pcireg_t class, status, rval;
365 1.1 jmcneill int off;
366 1.1 jmcneill
367 1.1 jmcneill /* Check AGP device. */
368 1.1 jmcneill class = pci_conf_read(pc, tag, PCI_CLASS_REG);
369 1.1 jmcneill if (PCI_CLASS(class) == PCI_CLASS_DISPLAY) {
370 1.1 jmcneill status = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
371 1.1 jmcneill if (status & PCI_STATUS_CAPLIST_SUPPORT) {
372 1.1 jmcneill rval = pci_conf_read(pc, tag, PCI_CAPLISTPTR_REG);
373 1.1 jmcneill for (off = PCI_CAPLIST_PTR(rval);
374 1.1 jmcneill off != 0;
375 1.1 jmcneill off = PCI_CAPLIST_NEXT(rval) ) {
376 1.1 jmcneill rval = pci_conf_read(pc, tag, off);
377 1.1 jmcneill if (PCI_CAPLIST_CAP(rval) == PCI_CAP_AGP)
378 1.1 jmcneill return (1);
379 1.1 jmcneill }
380 1.1 jmcneill }
381 1.1 jmcneill }
382 1.1 jmcneill return (0);
383 1.1 jmcneill }
384