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