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