1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
|
/* $NetBSD: pci_machdep.c,v 1.20 1996/03/04 19:39:31 cgd Exp $ */
/*
* Copyright (c) 1994 Charles Hannum. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Charles Hannum.
* 4. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Machine-specific functions for PCI autoconfiguration.
*
* On PCs, there are two methods of generating PCI configuration cycles.
* We try to detect the appropriate mechanism for this machine and set
* up a few function pointers to access the correct method directly.
*
* The configuration method can be hard-coded in the config file by
* using `options PCI_CONF_MODE=N', where `N' is the configuration mode
* as defined section 3.6.4.1, `Generating Configuration Cycles'.
*/
#include <sys/types.h>
#include <sys/param.h>
#include <sys/time.h>
#include <sys/systm.h>
#include <sys/errno.h>
#include <sys/device.h>
#include <vm/vm.h>
#include <vm/vm_kern.h>
#include <machine/pio.h>
#include <i386/isa/icu.h>
#include <dev/isa/isavar.h>
#include <dev/pci/pcivar.h>
#include <dev/pci/pcireg.h>
int pci_mode = -1;
#define PCI_MODE1_ENABLE 0x80000000UL
#define PCI_MODE1_ADDRESS_REG 0x0cf8
#define PCI_MODE1_DATA_REG 0x0cfc
#define PCI_MODE2_ENABLE_REG 0x0cf8
#define PCI_MODE2_FORWARD_REG 0x0cfa
pcitag_t
pci_make_tag(bus, device, function)
int bus, device, function;
{
pcitag_t tag;
#ifndef PCI_CONF_MODE
switch (pci_mode) {
case 1:
goto mode1;
case 2:
goto mode2;
default:
panic("pci_make_tag: mode not configured");
}
#endif
#if !defined(PCI_CONF_MODE) || (PCI_CONF_MODE == 1)
mode1:
if (bus >= 256 || device >= 32 || function >= 8)
panic("pci_make_tag: bad request");
tag.mode1 = PCI_MODE1_ENABLE |
(bus << 16) | (device << 11) | (function << 8);
return tag;
#endif
#if !defined(PCI_CONF_MODE) || (PCI_CONF_MODE == 2)
mode2:
if (bus >= 256 || device >= 16 || function >= 8)
panic("pci_make_tag: bad request");
tag.mode2.port = 0xc000 | (device << 8);
tag.mode2.enable = 0xf0 | (function << 1);
tag.mode2.forward = bus;
return tag;
#endif
}
pcireg_t
pci_conf_read(tag, reg)
pcitag_t tag;
int reg;
{
pcireg_t data;
#ifndef PCI_CONF_MODE
switch (pci_mode) {
case 1:
goto mode1;
case 2:
goto mode2;
default:
panic("pci_conf_read: mode not configured");
}
#endif
#if !defined(PCI_CONF_MODE) || (PCI_CONF_MODE == 1)
mode1:
outl(PCI_MODE1_ADDRESS_REG, tag.mode1 | reg);
data = inl(PCI_MODE1_DATA_REG);
outl(PCI_MODE1_ADDRESS_REG, 0);
return data;
#endif
#if !defined(PCI_CONF_MODE) || (PCI_CONF_MODE == 2)
mode2:
outb(PCI_MODE2_ENABLE_REG, tag.mode2.enable);
outb(PCI_MODE2_FORWARD_REG, tag.mode2.forward);
data = inl(tag.mode2.port | reg);
outb(PCI_MODE2_ENABLE_REG, 0);
return data;
#endif
}
void
pci_conf_write(tag, reg, data)
pcitag_t tag;
int reg;
pcireg_t data;
{
#ifndef PCI_CONF_MODE
switch (pci_mode) {
case 1:
goto mode1;
case 2:
goto mode2;
default:
panic("pci_conf_write: mode not configured");
}
#endif
#if !defined(PCI_CONF_MODE) || (PCI_CONF_MODE == 1)
mode1:
outl(PCI_MODE1_ADDRESS_REG, tag.mode1 | reg);
outl(PCI_MODE1_DATA_REG, data);
outl(PCI_MODE1_ADDRESS_REG, 0);
#endif
#if !defined(PCI_CONF_MODE) || (PCI_CONF_MODE == 2)
mode2:
outb(PCI_MODE2_ENABLE_REG, tag.mode2.enable);
outb(PCI_MODE2_FORWARD_REG, tag.mode2.forward);
outl(tag.mode2.port | reg, data);
outb(PCI_MODE2_ENABLE_REG, 0);
#endif
}
int
pci_mode_detect()
{
#ifdef PCI_CONF_MODE
#if (PCI_CONF_MODE == 1) || (PCI_CONF_MODE == 2)
return (pci_mode = PCI_CONF_MODE);
#else
#error Invalid PCI configuration mode.
#endif
#else
if (pci_mode != -1)
return pci_mode;
/*
* We try to divine which configuration mode the host bridge wants. We
* try mode 2 first, because our probe for mode 1 is likely to succeed
* for mode 2 also.
*
* XXX
* This should really be done using the PCI BIOS.
*/
outb(PCI_MODE2_ENABLE_REG, 0);
outb(PCI_MODE2_FORWARD_REG, 0);
if (inb(PCI_MODE2_ENABLE_REG) != 0 ||
inb(PCI_MODE2_FORWARD_REG) != 0)
goto not2;
return (pci_mode = 2);
not2:
outl(PCI_MODE1_ADDRESS_REG, PCI_MODE1_ENABLE);
if (inl(PCI_MODE1_ADDRESS_REG) != PCI_MODE1_ENABLE)
goto not1;
outl(PCI_MODE1_ADDRESS_REG, 0);
if (inl(PCI_MODE1_ADDRESS_REG) != 0)
goto not1;
return (pci_mode = 1);
not1:
return (pci_mode = 0);
#endif
}
int
pci_map_io(tag, reg, iobasep)
pcitag_t tag;
int reg;
int *iobasep;
{
pcireg_t address;
int iobase;
if (reg < PCI_MAPREG_START || reg >= PCI_MAPREG_END || (reg & 3))
panic("pci_map_io: bad request");
address = pci_conf_read(tag, reg);
if (PCI_MAPREG_TYPE(address) != PCI_MAPREG_TYPE_IO)
panic("pci_map_io: not an I/O mapping register");
iobase = PCI_MAPREG_IO_ADDR(address);
*iobasep = iobase;
return 0;
}
int
pci_map_mem(tag, reg, vap, pap)
pcitag_t tag;
int reg;
vm_offset_t *vap, *pap;
{
pcireg_t address, mask;
int cachable;
vm_size_t size;
vm_offset_t va, pa;
if (reg < PCI_MAPREG_START || reg >= PCI_MAPREG_END || (reg & 3))
panic("pci_map_mem: bad request");
/*
* Section 6.2.5.1, `Address Maps', tells us that:
*
* 1) The builtin software should have already mapped the device in a
* reasonable way.
*
* 2) A device which wants 2^n bytes of memory will hardwire the bottom
* n bits of the address to 0. As recommended, we write all 1s and see
* what we get back.
*/
address = pci_conf_read(tag, reg);
pci_conf_write(tag, reg, 0xffffffff);
mask = pci_conf_read(tag, reg);
pci_conf_write(tag, reg, address);
if (PCI_MAPREG_TYPE(address) == PCI_MAPREG_TYPE_IO)
panic("pci_map_mem: I/O mapping register");
switch (address & PCI_MAPREG_MEM_TYPE_MASK) {
case PCI_MAPREG_MEM_TYPE_32BIT:
case PCI_MAPREG_MEM_TYPE_32BIT_1M:
break;
case PCI_MAPREG_MEM_TYPE_64BIT:
printf("pci_map_mem: 64-bit memory mapping register\n");
return EOPNOTSUPP;
default:
printf("pci_map_mem: reserved mapping register type\n");
return EINVAL;
}
pa = PCI_MAPREG_MEM_ADDR(address);
size = ~PCI_MAPREG_MEM_ADDR(mask) + 1;
if (size < NBPG)
size = NBPG;
va = kmem_alloc_pageable(kernel_map, size);
if (va == 0) {
printf("pci_map_mem: not enough memory\n");
return ENOMEM;
}
/*
* Tell the driver where we mapped it.
*
* If the region is smaller than one page, adjust the virtual address
* to the same page offset as the physical address.
*/
*vap = va + (pa & PGOFSET);
*pap = pa;
#if 1
printf("pci_map_mem: mapping memory at virtual %08x, physical %08x\n", *vap, *pap);
#endif
/* Map the space into the kernel page table. */
cachable = PCI_MAPREG_MEM_CACHEABLE(address);
pa &= ~PGOFSET;
while (size) {
pmap_enter(pmap_kernel(), va, pa, VM_PROT_READ | VM_PROT_WRITE,
TRUE);
if (!cachable)
pmap_changebit(pa, PG_N, ~0);
else
pmap_changebit(pa, 0, ~PG_N);
va += NBPG;
pa += NBPG;
size -= NBPG;
}
return 0;
}
void *
pci_map_int(tag, level, func, arg, what)
pcitag_t tag;
int level;
int (*func) __P((void *));
void *arg;
char *what;
{
pcireg_t data;
int pin, line;
data = pci_conf_read(tag, PCI_INTERRUPT_REG);
pin = PCI_INTERRUPT_PIN(data);
line = PCI_INTERRUPT_LINE(data);
if (pin == 0) {
/* No IRQ used. */
return 0;
}
if (pin > 4) {
printf("pci_map_int: bad interrupt pin %d\n", pin);
return NULL;
}
/*
* Section 6.2.4, `Miscellaneous Functions', says that 255 means
* `unknown' or `no connection' on a PC. We assume that a device with
* `no connection' either doesn't have an interrupt (in which case the
* pin number should be 0, and would have been noticed above), or
* wasn't configured by the BIOS (in which case we punt, since there's
* no real way we can know how the interrupt lines are mapped in the
* hardware).
*
* XXX
* Since IRQ 0 is only used by the clock, and we can't actually be sure
* that the BIOS did its job, we also recognize that as meaning that
* the BIOS has not configured the device.
*/
if (line == 0 || line == 255) {
printf("pci_map_int: no mapping for pin %c\n", '@' + pin);
return NULL;
} else {
if (line >= ICU_LEN) {
printf("pci_map_int: bad interrupt line %d\n", line);
return NULL;
}
if (line == 2) {
printf("pci_map_int: changed line 2 to line 9\n");
line = 9;
}
}
#if 1
printf("pci_map_int: pin %c mapped to line %d\n", '@' + pin, line);
#endif
return isa_intr_establish(line, IST_LEVEL, level, func, arg, what);
}
|