pci: Add MPTable Host-PCI/PCI-PCI bridges drivers.
[dragonfly.git] / sys / bus / pci / i386 / pci_cfgreg.c
1 /*-
2  * Copyright (c) 1997, Stefan Esser <se@kfreebsd.org>
3  * Copyright (c) 2000, Michael Smith <msmith@kfreebsd.org>
4  * Copyright (c) 2000, BSDi
5  * Copyright (c) 2004, Scott Long <scottl@kfreebsd.org>
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice unmodified, this list of conditions, and the following
13  *    disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  *
29  * $FreeBSD: src/sys/i386/pci/pci_cfgreg.c,v 1.124.2.2.6.1 2009/04/15 03:14:26 kensmith Exp $
30  */
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/bus.h>
35 #include <sys/lock.h>
36 #include <sys/malloc.h>
37 #include <sys/thread2.h>
38 #include <sys/spinlock.h>
39 #include <sys/spinlock2.h>
40 #include <sys/queue.h>
41 #include <bus/pci/pcivar.h>
42 #include <bus/pci/pcireg.h>
43 #include "pci_cfgreg.h"
44 #include <machine/pc/bios.h>
45
46 #include <vm/vm.h>
47 #include <vm/vm_param.h>
48 #include <vm/vm_kern.h>
49 #include <vm/vm_extern.h>
50 #include <vm/pmap.h>
51 #include <machine/pmap.h>
52
53 #if defined(__DragonFly__)
54 #define mtx_init(a, b, c, d) spin_init(a)
55 #define mtx_lock_spin(a) spin_lock_wr(a)
56 #define mtx_unlock_spin(a) spin_unlock_wr(a)
57 #endif
58
59 #define PRVERB(a) do {                                                  \
60         if (bootverbose)                                                \
61                 kprintf a ;                                             \
62 } while(0)
63
64 #define PCIE_CACHE 8
65 struct pcie_cfg_elem {
66         TAILQ_ENTRY(pcie_cfg_elem)      elem;
67         vm_offset_t     vapage;
68         vm_paddr_t      papage;
69 };
70
71 enum {
72         CFGMECH_NONE = 0,
73         CFGMECH_1,
74         CFGMECH_2,
75         CFGMECH_PCIE,
76 };
77
78 static TAILQ_HEAD(pcie_cfg_list, pcie_cfg_elem) pcie_list[MAXCPU];
79 static uint32_t pciebar;
80 static int cfgmech;
81 static int devmax;
82 #if defined(__DragonFly__)
83 static struct spinlock pcicfg_mtx;
84 #else
85 static struct mtx pcicfg_mtx;
86 #endif
87
88 static int      pcireg_cfgread(int bus, int slot, int func, int reg, int bytes);
89 static void     pcireg_cfgwrite(int bus, int slot, int func, int reg, int data, int bytes);
90 static int      pcireg_cfgopen(void);
91
92 static int      pciereg_cfgopen(void);
93 static int      pciereg_cfgread(int bus, int slot, int func, int reg,
94                                 int bytes);
95 static void     pciereg_cfgwrite(int bus, int slot, int func, int reg,
96                                  int data, int bytes);
97
98 /*
99  * Some BIOS writers seem to want to ignore the spec and put
100  * 0 in the intline rather than 255 to indicate none.  Some use
101  * numbers in the range 128-254 to indicate something strange and
102  * apparently undocumented anywhere.  Assume these are completely bogus
103  * and map them to 255, which means "none".
104  */
105 static __inline int 
106 pci_i386_map_intline(int line)
107 {
108         if (line == 0 || line >= 128)
109                 return (PCI_INVALID_IRQ);
110         return (line);
111 }
112
113 static u_int16_t
114 pcibios_get_version(void)
115 {
116         struct bios_regs args;
117
118         if (PCIbios.ventry == 0) {
119                 PRVERB(("pcibios: No call entry point\n"));
120                 return (0);
121         }
122         args.eax = PCIBIOS_BIOS_PRESENT;
123         if (bios32(&args, PCIbios.ventry, GSEL(GCODE_SEL, SEL_KPL))) {
124                 PRVERB(("pcibios: BIOS_PRESENT call failed\n"));
125                 return (0);
126         }
127         if (args.edx != 0x20494350) {
128                 PRVERB(("pcibios: BIOS_PRESENT didn't return 'PCI ' in edx\n"));
129                 return (0);
130         }
131         return (args.ebx & 0xffff);
132 }
133
134 /* 
135  * Initialise access to PCI configuration space 
136  */
137 int
138 pci_cfgregopen(void)
139 {
140         static int              opened = 0;
141         u_int16_t               vid, did;
142         u_int16_t               v;
143         if (opened)
144                 return(1);
145
146         if (pcireg_cfgopen() == 0)
147                 return(0);
148
149         v = pcibios_get_version();
150         if (v > 0)
151                 PRVERB(("pcibios: BIOS version %x.%02x\n", (v & 0xff00) >> 8,
152                     v & 0xff));
153         mtx_init(&pcicfg_mtx, "pcicfg", NULL, MTX_SPIN);
154         opened = 1;
155
156         /* $PIR requires PCI BIOS 2.10 or greater. */
157         if (v >= 0x0210)
158                 pci_pir_open();
159
160         /*
161          * Grope around in the PCI config space to see if this is a
162          * chipset that is capable of doing memory-mapped config cycles.
163          * This also implies that it can do PCIe extended config cycles.
164          */
165
166         /* Check for supported chipsets */
167         vid = pci_cfgregread(0, 0, 0, PCIR_VENDOR, 2);
168         did = pci_cfgregread(0, 0, 0, PCIR_DEVICE, 2);
169         if (vid == 0x8086) {
170                 if (did == 0x3590 || did == 0x3592) {
171                         /* Intel 7520 or 7320 */
172                         pciebar = pci_cfgregread(0, 0, 0, 0xce, 2) << 16;
173                         pciereg_cfgopen();
174                 } else if (did == 0x2580 || did == 0x2584) {
175                         /* Intel 915 or 925 */
176                         pciebar = pci_cfgregread(0, 0, 0, 0x48, 4);
177                         pciereg_cfgopen();
178                 }
179         }
180
181         return(1);
182 }
183
184 /* 
185  * Read configuration space register
186  */
187 u_int32_t
188 pci_cfgregread(int bus, int slot, int func, int reg, int bytes)
189 {
190         uint32_t line;
191
192         /*
193          * Some BIOS writers seem to want to ignore the spec and put
194          * 0 in the intline rather than 255 to indicate none.  The rest of
195          * the code uses 255 as an invalid IRQ.
196          */
197         if (reg == PCIR_INTLINE && bytes == 1) {
198                 line = pcireg_cfgread(bus, slot, func, PCIR_INTLINE, 1);
199                 return (pci_i386_map_intline(line));
200         }
201         return (pcireg_cfgread(bus, slot, func, reg, bytes));
202 }
203
204 /* 
205  * Write configuration space register 
206  */
207 void
208 pci_cfgregwrite(int bus, int slot, int func, int reg, u_int32_t data, int bytes)
209 {
210
211         pcireg_cfgwrite(bus, slot, func, reg, data, bytes);
212 }
213
214 /* 
215  * Configuration space access using direct register operations
216  */
217
218 /* enable configuration space accesses and return data port address */
219 static int
220 pci_cfgenable(unsigned bus, unsigned slot, unsigned func, int reg, int bytes)
221 {
222         int dataport = 0;
223
224 #ifdef XBOX
225         if (arch_i386_is_xbox) {
226                 /*
227                  * The Xbox MCPX chipset is a derivative of the nForce 1
228                  * chipset. It almost has the same bus layout; some devices
229                  * cannot be used, because they have been removed.
230                  */
231
232                 /*
233                  * Devices 00:00.1 and 00:00.2 used to be memory controllers on
234                  * the nForce chipset, but on the Xbox, using them will lockup
235                  * the chipset.
236                  */
237                 if (bus == 0 && slot == 0 && (func == 1 || func == 2))
238                         return dataport;
239                 
240                 /*
241                  * Bus 1 only contains a VGA controller at 01:00.0. When you try
242                  * to probe beyond that device, you only get garbage, which
243                  * could cause lockups.
244                  */
245                 if (bus == 1 && (slot != 0 || func != 0))
246                         return dataport;
247                 
248                 /*
249                  * Bus 2 used to contain the AGP controller, but the Xbox MCPX
250                  * doesn't have one. Probing it can cause lockups.
251                  */
252                 if (bus >= 2)
253                         return dataport;
254         }
255 #endif
256
257         if (bus <= PCI_BUSMAX
258             && slot < devmax
259             && func <= PCI_FUNCMAX
260             && reg <= PCI_REGMAX
261             && bytes != 3
262             && (unsigned) bytes <= 4
263             && (reg & (bytes - 1)) == 0) {
264                 switch (cfgmech) {
265                 case CFGMECH_1:
266                         outl(CONF1_ADDR_PORT, (1 << 31)
267                             | (bus << 16) | (slot << 11) 
268                             | (func << 8) | (reg & ~0x03));
269                         dataport = CONF1_DATA_PORT + (reg & 0x03);
270                         break;
271                 case CFGMECH_2:
272                         outb(CONF2_ENABLE_PORT, 0xf0 | (func << 1));
273                         outb(CONF2_FORWARD_PORT, bus);
274                         dataport = 0xc000 | (slot << 8) | reg;
275                         break;
276                 }
277         }
278         return (dataport);
279 }
280
281 /* disable configuration space accesses */
282 static void
283 pci_cfgdisable(void)
284 {
285         switch (cfgmech) {
286         case CFGMECH_1:
287                 /*
288                  * Do nothing for the config mechanism 1 case.
289                  * Writing a 0 to the address port can apparently
290                  * confuse some bridges and cause spurious
291                  * access failures.
292                  */
293                 break;
294         case CFGMECH_2:
295                 outb(CONF2_ENABLE_PORT, 0);
296                 break;
297         }
298 }
299
300 static int
301 pcireg_cfgread(int bus, int slot, int func, int reg, int bytes)
302 {
303         int data = -1;
304         int port;
305
306         if (cfgmech == CFGMECH_PCIE) {
307                 data = pciereg_cfgread(bus, slot, func, reg, bytes);
308                 return (data);
309         }
310
311         mtx_lock_spin(&pcicfg_mtx);
312         port = pci_cfgenable(bus, slot, func, reg, bytes);
313         if (port != 0) {
314                 switch (bytes) {
315                 case 1:
316                         data = inb(port);
317                         break;
318                 case 2:
319                         data = inw(port);
320                         break;
321                 case 4:
322                         data = inl(port);
323                         break;
324                 }
325                 pci_cfgdisable();
326         }
327         mtx_unlock_spin(&pcicfg_mtx);
328         return (data);
329 }
330
331 static void
332 pcireg_cfgwrite(int bus, int slot, int func, int reg, int data, int bytes)
333 {
334         int port;
335
336         if (cfgmech == CFGMECH_PCIE) {
337                 pciereg_cfgwrite(bus, slot, func, reg, data, bytes);
338                 return;
339         }
340
341         mtx_lock_spin(&pcicfg_mtx);
342         port = pci_cfgenable(bus, slot, func, reg, bytes);
343         if (port != 0) {
344                 switch (bytes) {
345                 case 1:
346                         outb(port, data);
347                         break;
348                 case 2:
349                         outw(port, data);
350                         break;
351                 case 4:
352                         outl(port, data);
353                         break;
354                 }
355                 pci_cfgdisable();
356         }
357         mtx_unlock_spin(&pcicfg_mtx);
358 }
359
360 /* check whether the configuration mechanism has been correctly identified */
361 static int
362 pci_cfgcheck(int maxdev)
363 {
364         uint32_t id, class;
365         uint8_t header;
366         uint8_t device;
367         int port;
368
369         if (bootverbose) 
370                 kprintf("pci_cfgcheck:\tdevice ");
371
372         for (device = 0; device < maxdev; device++) {
373                 if (bootverbose) 
374                         kprintf("%d ", device);
375
376                 port = pci_cfgenable(0, device, 0, 0, 4);
377                 id = inl(port);
378                 if (id == 0 || id == 0xffffffff)
379                         continue;
380
381                 port = pci_cfgenable(0, device, 0, 8, 4);
382                 class = inl(port) >> 8;
383                 if (bootverbose)
384                         kprintf("[class=%06x] ", class);
385                 if (class == 0 || (class & 0xf870ff) != 0)
386                         continue;
387
388                 port = pci_cfgenable(0, device, 0, 14, 1);
389                 header = inb(port);
390                 if (bootverbose)
391                         kprintf("[hdr=%02x] ", header);
392                 if ((header & 0x7e) != 0)
393                         continue;
394
395                 if (bootverbose)
396                         kprintf("is there (id=%08x)\n", id);
397
398                 pci_cfgdisable();
399                 return (1);
400         }
401         if (bootverbose) 
402                 kprintf("-- nothing found\n");
403
404         pci_cfgdisable();
405         return (0);
406 }
407
408 static int
409 pcireg_cfgopen(void)
410 {
411         uint32_t mode1res, oldval1;
412         uint8_t mode2res, oldval2;
413
414         /* Check for type #1 first. */
415         oldval1 = inl(CONF1_ADDR_PORT);
416
417         if (bootverbose) {
418                 kprintf("pci_open(1):\tmode 1 addr port (0x0cf8) is 0x%08x\n",
419                     oldval1);
420         }
421
422         cfgmech = CFGMECH_1;
423         devmax = 32;
424
425         outl(CONF1_ADDR_PORT, CONF1_ENABLE_CHK);
426         DELAY(1);
427         mode1res = inl(CONF1_ADDR_PORT);
428         outl(CONF1_ADDR_PORT, oldval1);
429
430         if (bootverbose)
431                 kprintf("pci_open(1a):\tmode1res=0x%08x (0x%08lx)\n",  mode1res,
432                     CONF1_ENABLE_CHK);
433
434         if (mode1res) {
435                 if (pci_cfgcheck(32)) 
436                         return (cfgmech);
437         }
438
439         outl(CONF1_ADDR_PORT, CONF1_ENABLE_CHK1);
440         mode1res = inl(CONF1_ADDR_PORT);
441         outl(CONF1_ADDR_PORT, oldval1);
442
443         if (bootverbose)
444                 kprintf("pci_open(1b):\tmode1res=0x%08x (0x%08lx)\n",  mode1res,
445                     CONF1_ENABLE_CHK1);
446
447         if ((mode1res & CONF1_ENABLE_MSK1) == CONF1_ENABLE_RES1) {
448                 if (pci_cfgcheck(32)) 
449                         return (cfgmech);
450         }
451
452         /* Type #1 didn't work, so try type #2. */
453         oldval2 = inb(CONF2_ENABLE_PORT);
454
455         if (bootverbose) {
456                 kprintf("pci_open(2):\tmode 2 enable port (0x0cf8) is 0x%02x\n",
457                     oldval2);
458         }
459
460         if ((oldval2 & 0xf0) == 0) {
461
462                 cfgmech = CFGMECH_2;
463                 devmax = 16;
464
465                 outb(CONF2_ENABLE_PORT, CONF2_ENABLE_CHK);
466                 mode2res = inb(CONF2_ENABLE_PORT);
467                 outb(CONF2_ENABLE_PORT, oldval2);
468
469                 if (bootverbose)
470                         kprintf("pci_open(2a):\tmode2res=0x%02x (0x%02x)\n",
471                             mode2res, CONF2_ENABLE_CHK);
472
473                 if (mode2res == CONF2_ENABLE_RES) {
474                         if (bootverbose)
475                                 kprintf("pci_open(2a):\tnow trying mechanism 2\n");
476
477                         if (pci_cfgcheck(16)) 
478                                 return (cfgmech);
479                 }
480         }
481
482         /* Nothing worked, so punt. */
483         cfgmech = CFGMECH_NONE;
484         devmax = 0;
485         return (cfgmech);
486 }
487
488 static int
489 pciereg_cfgopen(void)
490 {
491 #ifdef PCIE_CFG_MECH
492         struct pcie_cfg_list *pcielist;
493         struct pcie_cfg_elem *pcie_array, *elem;
494 #ifdef SMP
495         struct pcpu *pc;
496 #endif
497         vm_offset_t va;
498         int i;
499
500         if (bootverbose)
501                 kprintf("Setting up PCIe mappings for BAR 0x%x\n", pciebar);
502
503 #ifdef SMP
504         SLIST_FOREACH(pc, &cpuhead, pc_allcpu)
505 #endif
506         {
507
508                 pcie_array = kmalloc(sizeof(struct pcie_cfg_elem) * PCIE_CACHE,
509                     M_DEVBUF, M_NOWAIT);
510                 if (pcie_array == NULL)
511                         return (0);
512
513                 va = kmem_alloc_nofault(&kernel_map, PCIE_CACHE * PAGE_SIZE);
514                 if (va == 0) {
515                         kfree(pcie_array, M_DEVBUF);
516                         return (0);
517                 }
518
519 #ifdef SMP
520                 pcielist = &pcie_list[pc->pc_cpuid];
521 #else
522                 pcielist = &pcie_list[0];
523 #endif
524                 TAILQ_INIT(pcielist);
525                 for (i = 0; i < PCIE_CACHE; i++) {
526                         elem = &pcie_array[i];
527                         elem->vapage = va + (i * PAGE_SIZE);
528                         elem->papage = 0;
529                         TAILQ_INSERT_HEAD(pcielist, elem, elem);
530                 }
531         }
532
533         
534         cfgmech = CFGMECH_PCIE;
535         devmax = 32;
536         return (1);
537 #else   /* !PCIE_CFG_MECH */
538         return (0);
539 #endif  /* PCIE_CFG_MECH */
540 }
541
542 #define PCIE_PADDR(bar, reg, bus, slot, func)   \
543         ((bar)                          |       \
544         (((bus) & 0xff) << 20)          |       \
545         (((slot) & 0x1f) << 15)         |       \
546         (((func) & 0x7) << 12)          |       \
547         ((reg) & 0xfff))
548
549 /*
550  * Find an element in the cache that matches the physical page desired, or
551  * create a new mapping from the least recently used element.
552  * A very simple LRU algorithm is used here, does it need to be more
553  * efficient?
554  */
555 static __inline struct pcie_cfg_elem *
556 pciereg_findelem(vm_paddr_t papage)
557 {
558         struct pcie_cfg_list *pcielist;
559         struct pcie_cfg_elem *elem;
560         pcielist = &pcie_list[mycpuid];
561         TAILQ_FOREACH(elem, pcielist, elem) {
562                 if (elem->papage == papage)
563                         break;
564         }
565
566         if (elem == NULL) {
567                 elem = TAILQ_LAST(pcielist, pcie_cfg_list);
568                 if (elem->papage != 0) {
569                         pmap_kremove(elem->vapage);
570                         cpu_invlpg(&elem->vapage);
571                 }
572                 pmap_kenter(elem->vapage, papage);
573                 elem->papage = papage;
574         }
575
576         if (elem != TAILQ_FIRST(pcielist)) {
577                 TAILQ_REMOVE(pcielist, elem, elem);
578                 TAILQ_INSERT_HEAD(pcielist, elem, elem);
579         }
580         return (elem);
581 }
582
583 static int
584 pciereg_cfgread(int bus, int slot, int func, int reg, int bytes)
585 {
586         struct pcie_cfg_elem *elem;
587         volatile vm_offset_t va;
588         vm_paddr_t pa, papage;
589         int data;
590
591         crit_enter();
592         pa = PCIE_PADDR(pciebar, reg, bus, slot, func);
593         papage = pa & ~PAGE_MASK;
594         elem = pciereg_findelem(papage);
595         va = elem->vapage | (pa & PAGE_MASK);
596
597         switch (bytes) {
598         case 4:
599                 data = *(volatile uint32_t *)(va);
600                 break;
601         case 2:
602                 data = *(volatile uint16_t *)(va);
603                 break;
604         case 1:
605                 data = *(volatile uint8_t *)(va);
606                 break;
607         default:
608                 panic("pciereg_cfgread: invalid width");
609         }
610
611         crit_exit();
612         return (data);
613 }
614
615 static void
616 pciereg_cfgwrite(int bus, int slot, int func, int reg, int data, int bytes)
617 {
618         struct pcie_cfg_elem *elem;
619         volatile vm_offset_t va;
620         vm_paddr_t pa, papage;
621
622         crit_enter();
623         pa = PCIE_PADDR(pciebar, reg, bus, slot, func);
624         papage = pa & ~PAGE_MASK;
625         elem = pciereg_findelem(papage);
626         va = elem->vapage | (pa & PAGE_MASK);
627
628         switch (bytes) {
629         case 4:
630                 *(volatile uint32_t *)(va) = data;
631                 break;
632         case 2:
633                 *(volatile uint16_t *)(va) = data;
634                 break;
635         case 1:
636                 *(volatile uint8_t *)(va) = data;
637                 break;
638         default:
639                 panic("pciereg_cfgwrite: invalid width");
640         }
641
642         crit_exit();
643 }