sys/bus/pci: Remove left over inclusion <sys/cdefs.h>.
[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 #ifdef APIC_IO
46 #include <machine/smp.h>
47 #endif
48
49 #include <vm/vm.h>
50 #include <vm/vm_param.h>
51 #include <vm/vm_kern.h>
52 #include <vm/vm_extern.h>
53 #include <vm/pmap.h>
54 #include <machine/pmap.h>
55
56 #if defined(__DragonFly__)
57 #define mtx_init(a, b, c, d) spin_init(a)
58 #define mtx_lock_spin(a) spin_lock_wr(a)
59 #define mtx_unlock_spin(a) spin_unlock_wr(a)
60 #endif
61
62 #define PRVERB(a) do {                                                  \
63         if (bootverbose)                                                \
64                 kprintf a ;                                             \
65 } while(0)
66
67 #define PCIE_CACHE 8
68 struct pcie_cfg_elem {
69         TAILQ_ENTRY(pcie_cfg_elem)      elem;
70         vm_offset_t     vapage;
71         vm_paddr_t      papage;
72 };
73
74 enum {
75         CFGMECH_NONE = 0,
76         CFGMECH_1,
77         CFGMECH_2,
78         CFGMECH_PCIE,
79 };
80
81 static TAILQ_HEAD(pcie_cfg_list, pcie_cfg_elem) pcie_list[MAXCPU];
82 static uint32_t pciebar;
83 static int cfgmech;
84 static int devmax;
85 #if defined(__DragonFly__)
86 static struct spinlock pcicfg_mtx;
87 #else
88 static struct mtx pcicfg_mtx;
89 #endif
90
91 static int      pcireg_cfgread(int bus, int slot, int func, int reg, int bytes);
92 static void     pcireg_cfgwrite(int bus, int slot, int func, int reg, int data, int bytes);
93 static int      pcireg_cfgopen(void);
94
95 static int      pciereg_cfgopen(void);
96 static int      pciereg_cfgread(int bus, int slot, int func, int reg,
97                                 int bytes);
98 static void     pciereg_cfgwrite(int bus, int slot, int func, int reg,
99                                  int data, int bytes);
100
101 /*
102  * Some BIOS writers seem to want to ignore the spec and put
103  * 0 in the intline rather than 255 to indicate none.  Some use
104  * numbers in the range 128-254 to indicate something strange and
105  * apparently undocumented anywhere.  Assume these are completely bogus
106  * and map them to 255, which means "none".
107  */
108 static __inline int 
109 pci_i386_map_intline(int line)
110 {
111         if (line == 0 || line >= 128)
112                 return (PCI_INVALID_IRQ);
113         return (line);
114 }
115
116 static u_int16_t
117 pcibios_get_version(void)
118 {
119         struct bios_regs args;
120
121         if (PCIbios.ventry == 0) {
122                 PRVERB(("pcibios: No call entry point\n"));
123                 return (0);
124         }
125         args.eax = PCIBIOS_BIOS_PRESENT;
126         if (bios32(&args, PCIbios.ventry, GSEL(GCODE_SEL, SEL_KPL))) {
127                 PRVERB(("pcibios: BIOS_PRESENT call failed\n"));
128                 return (0);
129         }
130         if (args.edx != 0x20494350) {
131                 PRVERB(("pcibios: BIOS_PRESENT didn't return 'PCI ' in edx\n"));
132                 return (0);
133         }
134         return (args.ebx & 0xffff);
135 }
136
137 /* 
138  * Initialise access to PCI configuration space 
139  */
140 int
141 pci_cfgregopen(void)
142 {
143         static int              opened = 0;
144         u_int16_t               vid, did;
145         u_int16_t               v;
146         if (opened)
147                 return(1);
148
149         if (pcireg_cfgopen() == 0)
150                 return(0);
151
152         v = pcibios_get_version();
153         if (v > 0)
154                 PRVERB(("pcibios: BIOS version %x.%02x\n", (v & 0xff00) >> 8,
155                     v & 0xff));
156         mtx_init(&pcicfg_mtx, "pcicfg", NULL, MTX_SPIN);
157         opened = 1;
158
159         /* $PIR requires PCI BIOS 2.10 or greater. */
160         if (v >= 0x0210)
161                 pci_pir_open();
162
163         /*
164          * Grope around in the PCI config space to see if this is a
165          * chipset that is capable of doing memory-mapped config cycles.
166          * This also implies that it can do PCIe extended config cycles.
167          */
168
169         /* Check for supported chipsets */
170         vid = pci_cfgregread(0, 0, 0, PCIR_VENDOR, 2);
171         did = pci_cfgregread(0, 0, 0, PCIR_DEVICE, 2);
172         if (vid == 0x8086) {
173                 if (did == 0x3590 || did == 0x3592) {
174                         /* Intel 7520 or 7320 */
175                         pciebar = pci_cfgregread(0, 0, 0, 0xce, 2) << 16;
176                         pciereg_cfgopen();
177                 } else if (did == 0x2580 || did == 0x2584) {
178                         /* Intel 915 or 925 */
179                         pciebar = pci_cfgregread(0, 0, 0, 0x48, 4);
180                         pciereg_cfgopen();
181                 }
182         }
183
184         return(1);
185 }
186
187 /* 
188  * Read configuration space register
189  */
190 u_int32_t
191 pci_cfgregread(int bus, int slot, int func, int reg, int bytes)
192 {
193         uint32_t line;
194 #ifdef APIC_IO
195         uint32_t pin;
196
197         /*
198          * If we are using the APIC, the contents of the intline
199          * register will probably be wrong (since they are set up for
200          * use with the PIC.  Rather than rewrite these registers
201          * (maybe that would be smarter) we trap attempts to read them
202          * and translate to our private vector numbers.
203          */
204         if ((reg == PCIR_INTLINE) && (bytes == 1)) {
205
206                 pin = pcireg_cfgread(bus, slot, func, PCIR_INTPIN, 1);
207                 line = pcireg_cfgread(bus, slot, func, PCIR_INTLINE, 1);
208
209                 if (pin != 0) {
210                         int airq;
211
212                         airq = pci_apic_irq(bus, slot, pin);
213                         if (airq >= 0) {
214                                 /* PCI specific entry found in MP table */
215                                 if (airq != line)
216                                         undirect_pci_irq(line);
217                                 return (airq);
218                         } else {
219                                 /* 
220                                  * PCI interrupts might be redirected to the
221                                  * ISA bus according to some MP tables. Use the
222                                  * same methods as used by the ISA devices
223                                  * devices to find the proper IOAPIC int pin.
224                                  */
225                                 airq = isa_apic_irq(line);
226                                 if ((airq >= 0) && (airq != line)) {
227                                         /* XXX: undirect_pci_irq() ? */
228                                         undirect_isa_irq(line);
229                                         return (airq);
230                                 }
231                         }
232                 }
233                 return (line);
234         }
235 #else
236         /*
237          * Some BIOS writers seem to want to ignore the spec and put
238          * 0 in the intline rather than 255 to indicate none.  The rest of
239          * the code uses 255 as an invalid IRQ.
240          */
241         if (reg == PCIR_INTLINE && bytes == 1) {
242                 line = pcireg_cfgread(bus, slot, func, PCIR_INTLINE, 1);
243                 return (pci_i386_map_intline(line));
244         }
245 #endif /* APIC_IO */
246         return (pcireg_cfgread(bus, slot, func, reg, bytes));
247 }
248
249 /* 
250  * Write configuration space register 
251  */
252 void
253 pci_cfgregwrite(int bus, int slot, int func, int reg, u_int32_t data, int bytes)
254 {
255
256         pcireg_cfgwrite(bus, slot, func, reg, data, bytes);
257 }
258
259 /* 
260  * Configuration space access using direct register operations
261  */
262
263 /* enable configuration space accesses and return data port address */
264 static int
265 pci_cfgenable(unsigned bus, unsigned slot, unsigned func, int reg, int bytes)
266 {
267         int dataport = 0;
268
269 #ifdef XBOX
270         if (arch_i386_is_xbox) {
271                 /*
272                  * The Xbox MCPX chipset is a derivative of the nForce 1
273                  * chipset. It almost has the same bus layout; some devices
274                  * cannot be used, because they have been removed.
275                  */
276
277                 /*
278                  * Devices 00:00.1 and 00:00.2 used to be memory controllers on
279                  * the nForce chipset, but on the Xbox, using them will lockup
280                  * the chipset.
281                  */
282                 if (bus == 0 && slot == 0 && (func == 1 || func == 2))
283                         return dataport;
284                 
285                 /*
286                  * Bus 1 only contains a VGA controller at 01:00.0. When you try
287                  * to probe beyond that device, you only get garbage, which
288                  * could cause lockups.
289                  */
290                 if (bus == 1 && (slot != 0 || func != 0))
291                         return dataport;
292                 
293                 /*
294                  * Bus 2 used to contain the AGP controller, but the Xbox MCPX
295                  * doesn't have one. Probing it can cause lockups.
296                  */
297                 if (bus >= 2)
298                         return dataport;
299         }
300 #endif
301
302         if (bus <= PCI_BUSMAX
303             && slot < devmax
304             && func <= PCI_FUNCMAX
305             && reg <= PCI_REGMAX
306             && bytes != 3
307             && (unsigned) bytes <= 4
308             && (reg & (bytes - 1)) == 0) {
309                 switch (cfgmech) {
310                 case CFGMECH_1:
311                         outl(CONF1_ADDR_PORT, (1 << 31)
312                             | (bus << 16) | (slot << 11) 
313                             | (func << 8) | (reg & ~0x03));
314                         dataport = CONF1_DATA_PORT + (reg & 0x03);
315                         break;
316                 case CFGMECH_2:
317                         outb(CONF2_ENABLE_PORT, 0xf0 | (func << 1));
318                         outb(CONF2_FORWARD_PORT, bus);
319                         dataport = 0xc000 | (slot << 8) | reg;
320                         break;
321                 }
322         }
323         return (dataport);
324 }
325
326 /* disable configuration space accesses */
327 static void
328 pci_cfgdisable(void)
329 {
330         switch (cfgmech) {
331         case CFGMECH_1:
332                 /*
333                  * Do nothing for the config mechanism 1 case.
334                  * Writing a 0 to the address port can apparently
335                  * confuse some bridges and cause spurious
336                  * access failures.
337                  */
338                 break;
339         case CFGMECH_2:
340                 outb(CONF2_ENABLE_PORT, 0);
341                 break;
342         }
343 }
344
345 static int
346 pcireg_cfgread(int bus, int slot, int func, int reg, int bytes)
347 {
348         int data = -1;
349         int port;
350
351         if (cfgmech == CFGMECH_PCIE) {
352                 data = pciereg_cfgread(bus, slot, func, reg, bytes);
353                 return (data);
354         }
355
356         mtx_lock_spin(&pcicfg_mtx);
357         port = pci_cfgenable(bus, slot, func, reg, bytes);
358         if (port != 0) {
359                 switch (bytes) {
360                 case 1:
361                         data = inb(port);
362                         break;
363                 case 2:
364                         data = inw(port);
365                         break;
366                 case 4:
367                         data = inl(port);
368                         break;
369                 }
370                 pci_cfgdisable();
371         }
372         mtx_unlock_spin(&pcicfg_mtx);
373         return (data);
374 }
375
376 static void
377 pcireg_cfgwrite(int bus, int slot, int func, int reg, int data, int bytes)
378 {
379         int port;
380
381         if (cfgmech == CFGMECH_PCIE) {
382                 pciereg_cfgwrite(bus, slot, func, reg, data, bytes);
383                 return;
384         }
385
386         mtx_lock_spin(&pcicfg_mtx);
387         port = pci_cfgenable(bus, slot, func, reg, bytes);
388         if (port != 0) {
389                 switch (bytes) {
390                 case 1:
391                         outb(port, data);
392                         break;
393                 case 2:
394                         outw(port, data);
395                         break;
396                 case 4:
397                         outl(port, data);
398                         break;
399                 }
400                 pci_cfgdisable();
401         }
402         mtx_unlock_spin(&pcicfg_mtx);
403 }
404
405 /* check whether the configuration mechanism has been correctly identified */
406 static int
407 pci_cfgcheck(int maxdev)
408 {
409         uint32_t id, class;
410         uint8_t header;
411         uint8_t device;
412         int port;
413
414         if (bootverbose) 
415                 kprintf("pci_cfgcheck:\tdevice ");
416
417         for (device = 0; device < maxdev; device++) {
418                 if (bootverbose) 
419                         kprintf("%d ", device);
420
421                 port = pci_cfgenable(0, device, 0, 0, 4);
422                 id = inl(port);
423                 if (id == 0 || id == 0xffffffff)
424                         continue;
425
426                 port = pci_cfgenable(0, device, 0, 8, 4);
427                 class = inl(port) >> 8;
428                 if (bootverbose)
429                         kprintf("[class=%06x] ", class);
430                 if (class == 0 || (class & 0xf870ff) != 0)
431                         continue;
432
433                 port = pci_cfgenable(0, device, 0, 14, 1);
434                 header = inb(port);
435                 if (bootverbose)
436                         kprintf("[hdr=%02x] ", header);
437                 if ((header & 0x7e) != 0)
438                         continue;
439
440                 if (bootverbose)
441                         kprintf("is there (id=%08x)\n", id);
442
443                 pci_cfgdisable();
444                 return (1);
445         }
446         if (bootverbose) 
447                 kprintf("-- nothing found\n");
448
449         pci_cfgdisable();
450         return (0);
451 }
452
453 static int
454 pcireg_cfgopen(void)
455 {
456         uint32_t mode1res, oldval1;
457         uint8_t mode2res, oldval2;
458
459         /* Check for type #1 first. */
460         oldval1 = inl(CONF1_ADDR_PORT);
461
462         if (bootverbose) {
463                 kprintf("pci_open(1):\tmode 1 addr port (0x0cf8) is 0x%08x\n",
464                     oldval1);
465         }
466
467         cfgmech = CFGMECH_1;
468         devmax = 32;
469
470         outl(CONF1_ADDR_PORT, CONF1_ENABLE_CHK);
471         DELAY(1);
472         mode1res = inl(CONF1_ADDR_PORT);
473         outl(CONF1_ADDR_PORT, oldval1);
474
475         if (bootverbose)
476                 kprintf("pci_open(1a):\tmode1res=0x%08x (0x%08lx)\n",  mode1res,
477                     CONF1_ENABLE_CHK);
478
479         if (mode1res) {
480                 if (pci_cfgcheck(32)) 
481                         return (cfgmech);
482         }
483
484         outl(CONF1_ADDR_PORT, CONF1_ENABLE_CHK1);
485         mode1res = inl(CONF1_ADDR_PORT);
486         outl(CONF1_ADDR_PORT, oldval1);
487
488         if (bootverbose)
489                 kprintf("pci_open(1b):\tmode1res=0x%08x (0x%08lx)\n",  mode1res,
490                     CONF1_ENABLE_CHK1);
491
492         if ((mode1res & CONF1_ENABLE_MSK1) == CONF1_ENABLE_RES1) {
493                 if (pci_cfgcheck(32)) 
494                         return (cfgmech);
495         }
496
497         /* Type #1 didn't work, so try type #2. */
498         oldval2 = inb(CONF2_ENABLE_PORT);
499
500         if (bootverbose) {
501                 kprintf("pci_open(2):\tmode 2 enable port (0x0cf8) is 0x%02x\n",
502                     oldval2);
503         }
504
505         if ((oldval2 & 0xf0) == 0) {
506
507                 cfgmech = CFGMECH_2;
508                 devmax = 16;
509
510                 outb(CONF2_ENABLE_PORT, CONF2_ENABLE_CHK);
511                 mode2res = inb(CONF2_ENABLE_PORT);
512                 outb(CONF2_ENABLE_PORT, oldval2);
513
514                 if (bootverbose)
515                         kprintf("pci_open(2a):\tmode2res=0x%02x (0x%02x)\n",
516                             mode2res, CONF2_ENABLE_CHK);
517
518                 if (mode2res == CONF2_ENABLE_RES) {
519                         if (bootverbose)
520                                 kprintf("pci_open(2a):\tnow trying mechanism 2\n");
521
522                         if (pci_cfgcheck(16)) 
523                                 return (cfgmech);
524                 }
525         }
526
527         /* Nothing worked, so punt. */
528         cfgmech = CFGMECH_NONE;
529         devmax = 0;
530         return (cfgmech);
531 }
532
533 static int
534 pciereg_cfgopen(void)
535 {
536 #ifdef PCIE_CFG_MECH
537         struct pcie_cfg_list *pcielist;
538         struct pcie_cfg_elem *pcie_array, *elem;
539 #ifdef SMP
540         struct pcpu *pc;
541 #endif
542         vm_offset_t va;
543         int i;
544
545         if (bootverbose)
546                 kprintf("Setting up PCIe mappings for BAR 0x%x\n", pciebar);
547
548 #ifdef SMP
549         SLIST_FOREACH(pc, &cpuhead, pc_allcpu)
550 #endif
551         {
552
553                 pcie_array = kmalloc(sizeof(struct pcie_cfg_elem) * PCIE_CACHE,
554                     M_DEVBUF, M_NOWAIT);
555                 if (pcie_array == NULL)
556                         return (0);
557
558                 va = kmem_alloc_nofault(&kernel_map, PCIE_CACHE * PAGE_SIZE);
559                 if (va == 0) {
560                         kfree(pcie_array, M_DEVBUF);
561                         return (0);
562                 }
563
564 #ifdef SMP
565                 pcielist = &pcie_list[pc->pc_cpuid];
566 #else
567                 pcielist = &pcie_list[0];
568 #endif
569                 TAILQ_INIT(pcielist);
570                 for (i = 0; i < PCIE_CACHE; i++) {
571                         elem = &pcie_array[i];
572                         elem->vapage = va + (i * PAGE_SIZE);
573                         elem->papage = 0;
574                         TAILQ_INSERT_HEAD(pcielist, elem, elem);
575                 }
576         }
577
578         
579         cfgmech = CFGMECH_PCIE;
580         devmax = 32;
581         return (1);
582 #else   /* !PCIE_CFG_MECH */
583         return (0);
584 #endif  /* PCIE_CFG_MECH */
585 }
586
587 #define PCIE_PADDR(bar, reg, bus, slot, func)   \
588         ((bar)                          |       \
589         (((bus) & 0xff) << 20)          |       \
590         (((slot) & 0x1f) << 15)         |       \
591         (((func) & 0x7) << 12)          |       \
592         ((reg) & 0xfff))
593
594 /*
595  * Find an element in the cache that matches the physical page desired, or
596  * create a new mapping from the least recently used element.
597  * A very simple LRU algorithm is used here, does it need to be more
598  * efficient?
599  */
600 static __inline struct pcie_cfg_elem *
601 pciereg_findelem(vm_paddr_t papage)
602 {
603         struct pcie_cfg_list *pcielist;
604         struct pcie_cfg_elem *elem;
605         pcielist = &pcie_list[mycpuid];
606         TAILQ_FOREACH(elem, pcielist, elem) {
607                 if (elem->papage == papage)
608                         break;
609         }
610
611         if (elem == NULL) {
612                 elem = TAILQ_LAST(pcielist, pcie_cfg_list);
613                 if (elem->papage != 0) {
614                         pmap_kremove(elem->vapage);
615                         cpu_invlpg(&elem->vapage);
616                 }
617                 pmap_kenter(elem->vapage, papage);
618                 elem->papage = papage;
619         }
620
621         if (elem != TAILQ_FIRST(pcielist)) {
622                 TAILQ_REMOVE(pcielist, elem, elem);
623                 TAILQ_INSERT_HEAD(pcielist, elem, elem);
624         }
625         return (elem);
626 }
627
628 static int
629 pciereg_cfgread(int bus, int slot, int func, int reg, int bytes)
630 {
631         struct pcie_cfg_elem *elem;
632         volatile vm_offset_t va;
633         vm_paddr_t pa, papage;
634         int data;
635
636         crit_enter();
637         pa = PCIE_PADDR(pciebar, reg, bus, slot, func);
638         papage = pa & ~PAGE_MASK;
639         elem = pciereg_findelem(papage);
640         va = elem->vapage | (pa & PAGE_MASK);
641
642         switch (bytes) {
643         case 4:
644                 data = *(volatile uint32_t *)(va);
645                 break;
646         case 2:
647                 data = *(volatile uint16_t *)(va);
648                 break;
649         case 1:
650                 data = *(volatile uint8_t *)(va);
651                 break;
652         default:
653                 panic("pciereg_cfgread: invalid width");
654         }
655
656         crit_exit();
657         return (data);
658 }
659
660 static void
661 pciereg_cfgwrite(int bus, int slot, int func, int reg, int data, int bytes)
662 {
663         struct pcie_cfg_elem *elem;
664         volatile vm_offset_t va;
665         vm_paddr_t pa, papage;
666
667         crit_enter();
668         pa = PCIE_PADDR(pciebar, reg, bus, slot, func);
669         papage = pa & ~PAGE_MASK;
670         elem = pciereg_findelem(papage);
671         va = elem->vapage | (pa & PAGE_MASK);
672
673         switch (bytes) {
674         case 4:
675                 *(volatile uint32_t *)(va) = data;
676                 break;
677         case 2:
678                 *(volatile uint16_t *)(va) = data;
679                 break;
680         case 1:
681                 *(volatile uint8_t *)(va) = data;
682                 break;
683         default:
684                 panic("pciereg_cfgwrite: invalid width");
685         }
686
687         crit_exit();
688 }