1 | /*␊ |
2 | *␊ |
3 | * Copyright 2008 by Islam M. Ahmed Zaid. All rights reserved.␊ |
4 | *␊ |
5 | */␊ |
6 | ␊ |
7 | #include "libsaio.h"␊ |
8 | #include "boot.h"␊ |
9 | #include "bootstruct.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...)␉␉msglog(x)␊ |
21 | #endif␊ |
22 | ␊ |
23 | //static ␊ |
24 | pci_dt_t␉*root_pci_dev;␊ |
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 | ␉␉␉if (!new)␊ |
92 | ␉␉␉␉return;␊ |
93 | ␉␉␉memset(new, 0, sizeof(pci_dt_t));␊ |
94 | ␉␉␉␊ |
95 | ␉␉␉new->dev.addr␉= pci_addr;␊ |
96 | ␉␉␉new->vendor_id␉= id & 0xffff;␊ |
97 | ␉␉␉new->device_id␉= (id >> 16) & 0xffff;␊ |
98 | ␉␉␉new->subsys_id.subsys_id␉= pci_config_read32(pci_addr, PCI_SUBSYSTEM_VENDOR_ID);␊ |
99 | ␉␉␉new->subclass␉= pci_config_read8(pci_addr, PCI_CLASS_PROG);␊ |
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 | ␉␉␉}␊ |
115 | ␉␉␉*current = new;␊ |
116 | ␉␉␉current = &new->next;␊ |
117 | ␉␉␉␊ |
118 | ␉␉␉if ((func == 0) && ((header_type & 0x80) == 0))␊ |
119 | ␉␉␉{␊ |
120 | ␉␉␉␉break;␊ |
121 | ␉␉␉}␊ |
122 | ␉␉}␊ |
123 | ␉}␊ |
124 | }␊ |
125 | ␊ |
126 | void enable_pci_devs(void)␊ |
127 | {␊ |
128 | ␉uint16_t id;␊ |
129 | ␉uint32_t rcba, *fd;␊ |
130 | ␉␊ |
131 | ␉id = pci_config_read16(PCIADDR(0, 0x00, 0), 0x00);␊ |
132 | ␉/* make sure we're on Intel chipset */␊ |
133 | ␉if (id != 0x8086)␊ |
134 | ␉␉return;␊ |
135 | ␉rcba = pci_config_read32(PCIADDR(0, 0x1f, 0), 0xf0) & ~1;␊ |
136 | ␉fd = (uint32_t *)(rcba + 0x3418);␊ |
137 | ␉/* set SMBus Disable (SD) to 0 */␊ |
138 | ␉*fd &= ~0x8;␊ |
139 | ␉/* and all devices? */␊ |
140 | ␉//*fd = 0x1;␊ |
141 | }␊ |
142 | ␊ |
143 | ␊ |
144 | void build_pci_dt(void)␊ |
145 | {␊ |
146 | ␉root_pci_dev = malloc(sizeof(pci_dt_t));␊ |
147 | ␉␊ |
148 | ␉if (!root_pci_dev)␊ |
149 | ␉␉return;␊ |
150 | ␉gRootPCIDev = (void*)root_pci_dev;␊ |
151 | ␉bzero(root_pci_dev, sizeof(pci_dt_t));␊ |
152 | ␉enable_pci_devs();␊ |
153 | ␉scan_pci_bus(root_pci_dev, 0);␊ |
154 | #if 1 //DEBUG_PCI␊ |
155 | ␉dump_pci_dt(root_pci_dev->children);␊ |
156 | ␉//pause();␊ |
157 | #endif␊ |
158 | }␊ |
159 | ␊ |
160 | static char dev_path[256];␊ |
161 | char *get_pci_dev_path(pci_dt_t *pci_dt)␊ |
162 | {␊ |
163 | ␉pci_dt_t␉*current;␊ |
164 | ␉pci_dt_t␉*end;␊ |
165 | ␉char␉␉tmp[64];␊ |
166 | ␉␊ |
167 | ␉dev_path[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 | ␉␉strcat(dev_path, tmp);␊ |
188 | ␉}␊ |
189 | ␉return dev_path;␊ |
190 | }␊ |
191 | ␊ |
192 | #ifndef OPTION_ROM␊ |
193 | void dump_pci_dt(pci_dt_t *pci_dt)␊ |
194 | {␊ |
195 | ␉pci_dt_t␉*current;␊ |
196 | ␉␊ |
197 | ␉current = pci_dt;␊ |
198 | ␉while (current) {␊ |
199 | ␉␉msglog("%02x:%02x.%x [%04x%02x] [%04x:%04x] (subsys [%04x:%04x]):: %s\n", ␊ |
200 | ␉␉␉ current->dev.bits.bus, current->dev.bits.dev, current->dev.bits.func, ␊ |
201 | ␉␉␉ current->class_id, current->subclass, current->vendor_id, current->device_id, ␊ |
202 | ␉␉␉current->subsys_id.subsys.vendor_id, current->subsys_id.subsys.device_id, ␊ |
203 | ␉␉␉ get_pci_dev_path(current));␊ |
204 | ␉␉dump_pci_dt(current->children);␊ |
205 | ␉␉current = current->next;␊ |
206 | ␉}␊ |
207 | }␊ |
208 | #endif␊ |
209 | |