1 | /*␊ |
2 | *␊ |
3 | * Copyright 2008 by Islam M. Ahmed Zaid. All rights reserved.␊ |
4 | *␊ |
5 | */␊ |
6 | ␊ |
7 | #include "libsaio.h"␊ |
8 | #include "bootstruct.h"␊ |
9 | #include "pci.h"␊ |
10 | #include "pci_root.h"␊ |
11 | #include "modules.h"␊ |
12 | ␊ |
13 | #ifndef DEBUG_PCI␊ |
14 | #define DEBUG_PCI 0␊ |
15 | #endif␊ |
16 | ␊ |
17 | #if DEBUG_PCI␊ |
18 | #define DBG(x...)␉␉printf(x)␊ |
19 | #else␊ |
20 | #define DBG(x...)␊ |
21 | #endif␊ |
22 | ␊ |
23 | pci_dt_t␉*root_pci_dev;␊ |
24 | static char* dev_path;␉// TODO: Figure out what is going on here...␊ |
25 | ␊ |
26 | ␊ |
27 | uint8_t pci_config_read8(uint32_t pci_addr, uint8_t reg)␊ |
28 | {␊ |
29 | ␉pci_addr |= reg & ~3;␊ |
30 | ␉outl(PCI_ADDR_REG, pci_addr);␊ |
31 | ␉return inb(PCI_DATA_REG + (reg & 3));␊ |
32 | }␊ |
33 | ␊ |
34 | uint16_t pci_config_read16(uint32_t pci_addr, uint8_t reg)␊ |
35 | {␊ |
36 | ␉pci_addr |= reg & ~3;␊ |
37 | ␉outl(PCI_ADDR_REG, pci_addr);␊ |
38 | ␉return inw(PCI_DATA_REG + (reg & 2));␊ |
39 | }␊ |
40 | ␊ |
41 | uint32_t pci_config_read32(uint32_t pci_addr, uint8_t reg)␊ |
42 | {␊ |
43 | ␉pci_addr |= reg & ~3;␊ |
44 | ␉outl(PCI_ADDR_REG, pci_addr);␊ |
45 | ␉return inl(PCI_DATA_REG);␊ |
46 | }␊ |
47 | ␊ |
48 | void pci_config_write8(uint32_t pci_addr, uint8_t reg, uint8_t data)␊ |
49 | {␊ |
50 | ␉pci_addr |= reg & ~3;␊ |
51 | ␉outl(PCI_ADDR_REG, pci_addr);␊ |
52 | ␉outb(PCI_DATA_REG + (reg & 3), data);␊ |
53 | }␊ |
54 | ␊ |
55 | void pci_config_write16(uint32_t pci_addr, uint8_t reg, uint16_t data)␊ |
56 | {␊ |
57 | ␉pci_addr |= reg & ~3;␊ |
58 | ␉outl(PCI_ADDR_REG, pci_addr);␊ |
59 | ␉outw(PCI_DATA_REG + (reg & 2), data);␊ |
60 | }␊ |
61 | ␊ |
62 | void pci_config_write32(uint32_t pci_addr, uint8_t reg, uint32_t data)␊ |
63 | {␊ |
64 | ␉pci_addr |= reg & ~3;␊ |
65 | ␉outl(PCI_ADDR_REG, pci_addr);␊ |
66 | ␉outl(PCI_DATA_REG, data);␊ |
67 | }␊ |
68 | ␊ |
69 | void scan_pci_bus(pci_dt_t *start, uint8_t bus)␊ |
70 | {␊ |
71 | ␉pci_dt_t␉*new;␊ |
72 | ␉pci_dt_t␉**current = &start->children;␊ |
73 | ␉uint32_t␉id;␊ |
74 | ␉uint32_t␉pci_addr;␊ |
75 | ␉uint8_t␉␉dev;␊ |
76 | ␉uint8_t␉␉func;␊ |
77 | ␉uint8_t␉␉secondary_bus;␊ |
78 | ␉uint8_t␉␉header_type;␊ |
79 | ␉␊ |
80 | ␉for (dev = 0; dev < 32; dev++)␊ |
81 | ␉{␊ |
82 | ␉␉for (func = 0; func < 8; func++)␊ |
83 | ␉␉{␊ |
84 | ␉␉␉pci_addr = PCIADDR(bus, dev, func);␊ |
85 | ␉␉␉id = pci_config_read32(pci_addr, PCI_VENDOR_ID);␊ |
86 | ␉␉␉if (!id || id == 0xffffffff)␊ |
87 | ␉␉␉{␊ |
88 | ␉␉␉␉continue;␊ |
89 | ␉␉␉}␊ |
90 | ␉␉␉new = (pci_dt_t*)malloc(sizeof(pci_dt_t));␊ |
91 | ␉␉␉bzero(new, sizeof(pci_dt_t));␊ |
92 | ␉␉␉new->dev.addr␉= pci_addr;␊ |
93 | ␉␉␉new->vendor_id␉= id & 0xffff;␊ |
94 | ␉␉␉new->device_id␉= (id >> 16) & 0xffff;␊ |
95 | ␉␉␉new->subsys_id.subsys_id␉= pci_config_read32(pci_addr, PCI_SUBSYSTEM_VENDOR_ID);␊ |
96 | ␉␉␉new->class_id␉= pci_config_read16(pci_addr, PCI_CLASS_DEVICE);␊ |
97 | ␉␉␉new->parent␉= start;␊ |
98 | ␉␉␉␊ |
99 | ␉␉␉header_type = pci_config_read8(pci_addr, PCI_HEADER_TYPE);␊ |
100 | ␉␉␉switch (header_type & 0x7f)␊ |
101 | ␉␉␉{␊ |
102 | ␉␉␉␉case PCI_HEADER_TYPE_BRIDGE:␊ |
103 | ␉␉␉␉case PCI_HEADER_TYPE_CARDBUS:␊ |
104 | ␉␉␉␉␉secondary_bus = pci_config_read8(pci_addr, PCI_SECONDARY_BUS);␊ |
105 | ␉␉␉␉␉if (secondary_bus != 0)␊ |
106 | ␉␉␉␉␉{␊ |
107 | ␉␉␉␉␉␉scan_pci_bus(new, secondary_bus);␊ |
108 | ␉␉␉␉␉}␊ |
109 | ␉␉␉␉␉break;␊ |
110 | ␉␉␉}␊ |
111 | ␉␉␉*current = new;␊ |
112 | ␉␉␉current = &new->next;␊ |
113 | ␉␉␉␊ |
114 | ␉␉␉if ((func == 0) && ((header_type & 0x80) == 0))␊ |
115 | ␉␉␉{␊ |
116 | ␉␉␉␉break;␊ |
117 | ␉␉␉}␊ |
118 | ␉␉}␊ |
119 | ␉}␊ |
120 | }␊ |
121 | ␊ |
122 | void enable_pci_devs(void)␊ |
123 | {␊ |
124 | ␉uint16_t id;␊ |
125 | ␉uint32_t rcba, *fd;␊ |
126 | ␉␊ |
127 | ␉id = pci_config_read16(PCIADDR(0, 0x00, 0), 0x00);␊ |
128 | ␉/* make sure we're on Intel chipset */␊ |
129 | ␉if (id != 0x8086)␊ |
130 | ␉␉return;␊ |
131 | ␉rcba = pci_config_read32(PCIADDR(0, 0x1f, 0), 0xf0) & ~1;␊ |
132 | ␉fd = (uint32_t *)(rcba + 0x3418);␊ |
133 | ␉/* set SMBus Disable (SD) to 0 */␊ |
134 | ␉*fd &= ~0x8;␊ |
135 | ␉/* and all devices? */␊ |
136 | ␉//*fd = 0x1;␊ |
137 | }␊ |
138 | ␊ |
139 | ␊ |
140 | void build_pci_dt(void)␊ |
141 | {␊ |
142 | ␉dev_path = malloc(sizeof(char) * 256);␉// TODO: remove␊ |
143 | ␉␊ |
144 | ␉root_pci_dev = malloc(sizeof(pci_dt_t));␊ |
145 | ␉bzero(root_pci_dev, sizeof(pci_dt_t));␊ |
146 | ␉enable_pci_devs();␊ |
147 | ␉scan_pci_bus(root_pci_dev, 0);␊ |
148 | #if DEBUG_PCI␊ |
149 | #ifndef OPTION_ROM␊ |
150 | ␉dump_pci_dt(root_pci_dev->children);␊ |
151 | ␉pause();␊ |
152 | #endif␊ |
153 | #endif␊ |
154 | }␊ |
155 | ␊ |
156 | char *get_pci_dev_path(pci_dt_t *pci_dt)␊ |
157 | {␊ |
158 | ␉char* buffer = malloc(sizeof(char) * 256);␊ |
159 | ␊ |
160 | ␉pci_dt_t␉*current;␊ |
161 | ␉pci_dt_t␉*end;␊ |
162 | ␉char␉␉tmp[64];␊ |
163 | ␉␊ |
164 | ␉buffer[0] = 0;␉␊ |
165 | ␉end = root_pci_dev;␊ |
166 | ␉␊ |
167 | ␉int uid = getPciRootUID();␊ |
168 | ␉while (end != pci_dt)␊ |
169 | ␉{␊ |
170 | ␉␉current = pci_dt;␊ |
171 | ␉␉while (current->parent != end)␊ |
172 | ␉␉␉current = current->parent;␉␉␉␊ |
173 | ␉␉end = current;␊ |
174 | ␉␉if (current->parent == root_pci_dev)␊ |
175 | ␉␉{␊ |
176 | ␉␉␉sprintf(tmp, "PciRoot(0x%x)/Pci(0x%x,0x%x)", uid, ␊ |
177 | ␉␉␉␉␉current->dev.bits.dev, current->dev.bits.func);␊ |
178 | ␉␉} ␊ |
179 | ␉␉else ␊ |
180 | ␉␉{␊ |
181 | ␉␉␉sprintf(tmp, "/Pci(0x%x,0x%x)", ␊ |
182 | ␉␉␉␉␉current->dev.bits.dev, current->dev.bits.func);␊ |
183 | ␉␉}␊ |
184 | ␉␉sprintf(buffer, "%s%s", buffer, tmp);␊ |
185 | ␉}␊ |
186 | ␉return buffer;␊ |
187 | }␊ |
188 | ␊ |
189 | void setup_pci_devs(pci_dt_t *pci_dt)␊ |
190 | {␊ |
191 | ␉pci_dt_t *current = pci_dt;␊ |
192 | ␉␊ |
193 | ␉␊ |
194 | ␉while (current)␊ |
195 | ␉{␊ |
196 | ␉␉execute_hook("PCIDevice", current, NULL, NULL, NULL, NULL, NULL);␊ |
197 | ␉␉␊ |
198 | ␉␉setup_pci_devs(current->children);␊ |
199 | ␉␉current = current->next;␊ |
200 | ␉}␊ |
201 | }␊ |
202 | ␊ |
203 | #ifndef OPTION_ROM␊ |
204 | void dump_pci_dt(pci_dt_t *pci_dt)␊ |
205 | {␊ |
206 | ␉pci_dt_t␉*current;␊ |
207 | ␉␊ |
208 | ␉current = pci_dt;␊ |
209 | ␉while (current) ␊ |
210 | ␉{␊ |
211 | ␉␉printf("%02x:%02x.%x [%04x] [%04x:%04x] :: %s\n", ␊ |
212 | ␉␉␉ current->dev.bits.bus, current->dev.bits.dev, current->dev.bits.func, ␊ |
213 | ␉␉␉ current->class_id, current->vendor_id, current->device_id, ␊ |
214 | ␉␉␉ get_pci_dev_path(current));␊ |
215 | ␉␉dump_pci_dt(current->children);␊ |
216 | ␉␉current = current->next;␊ |
217 | ␉}␊ |
218 | }␊ |
219 | #endif␊ |
220 | |