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