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