Change license from GPLv3 to LGPLv3.
[seabios.git] / src / pci.c
1 // PCI config space access functions.
2 //
3 // Copyright (C) 2008  Kevin O'Connor <kevin@koconnor.net>
4 // Copyright (C) 2002  MandrakeSoft S.A.
5 //
6 // This file may be distributed under the terms of the GNU LGPLv3 license.
7
8 #include "pci.h" // pci_config_writel
9 #include "ioport.h" // outl
10 #include "util.h" // dprintf
11 #include "config.h" // CONFIG_*
12 #include "pci_regs.h" // PCI_VENDOR_ID
13 #include "farptr.h" // SET_VAR
14
15 void pci_config_writel(u16 bdf, u32 addr, u32 val)
16 {
17     outl(0x80000000 | (bdf << 8) | (addr & 0xfc), PORT_PCI_CMD);
18     outl(val, PORT_PCI_DATA);
19 }
20
21 void pci_config_writew(u16 bdf, u32 addr, u16 val)
22 {
23     outl(0x80000000 | (bdf << 8) | (addr & 0xfc), PORT_PCI_CMD);
24     outw(val, PORT_PCI_DATA + (addr & 2));
25 }
26
27 void pci_config_writeb(u16 bdf, u32 addr, u8 val)
28 {
29     outl(0x80000000 | (bdf << 8) | (addr & 0xfc), PORT_PCI_CMD);
30     outb(val, PORT_PCI_DATA + (addr & 3));
31 }
32
33 u32 pci_config_readl(u16 bdf, u32 addr)
34 {
35     outl(0x80000000 | (bdf << 8) | (addr & 0xfc), PORT_PCI_CMD);
36     return inl(PORT_PCI_DATA);
37 }
38
39 u16 pci_config_readw(u16 bdf, u32 addr)
40 {
41     outl(0x80000000 | (bdf << 8) | (addr & 0xfc), PORT_PCI_CMD);
42     return inw(PORT_PCI_DATA + (addr & 2));
43 }
44
45 u8 pci_config_readb(u16 bdf, u32 addr)
46 {
47     outl(0x80000000 | (bdf << 8) | (addr & 0xfc), PORT_PCI_CMD);
48     return inb(PORT_PCI_DATA + (addr & 3));
49 }
50
51 int
52 pci_next(int bdf, int *pmax)
53 {
54     if (pci_bdf_to_fn(bdf) == 1
55         && (pci_config_readb(bdf-1, PCI_HEADER_TYPE) & 0x80) == 0)
56         // Last found device wasn't a multi-function device - skip to
57         // the next device.
58         bdf += 7;
59
60     int max = *pmax;
61     for (;;) {
62         if (bdf >= max)
63             return -1;
64
65         u16 v = pci_config_readw(bdf, PCI_VENDOR_ID);
66         if (v != 0x0000 && v != 0xffff)
67             // Device is present.
68             break;
69
70         if (pci_bdf_to_fn(bdf) == 0)
71             bdf += 8;
72         else
73             bdf += 1;
74     }
75
76     // Check if found device is a bridge.
77     u32 v = pci_config_readb(bdf, PCI_HEADER_TYPE);
78     v &= 0x7f;
79     if (v == PCI_HEADER_TYPE_BRIDGE || v == PCI_HEADER_TYPE_CARDBUS) {
80         v = pci_config_readl(bdf, PCI_PRIMARY_BUS);
81         int newmax = (v & 0xff00) + 0x0100;
82         if (newmax > max)
83             *pmax = newmax;
84     }
85
86     return bdf;
87 }
88
89 // Search for a device with the specified vendor and device ids.
90 int
91 pci_find_device(u16 vendid, u16 devid)
92 {
93     u32 id = (devid << 16) | vendid;
94     int bdf, max;
95     foreachpci(bdf, max) {
96         u32 v = pci_config_readl(bdf, PCI_VENDOR_ID);
97         if (v != id)
98             continue;
99         // Found it.
100         return bdf;
101     }
102     return -1;
103 }
104
105 // Search for a device with the specified class id.
106 int
107 pci_find_class(u16 classid)
108 {
109     int bdf, max;
110     foreachpci(bdf, max) {
111         u16 v = pci_config_readw(bdf, PCI_CLASS_DEVICE);
112         if (v != classid)
113             continue;
114         // Found it.
115         return bdf;
116     }
117     return -1;
118 }