corinfo: Inital release of the coreinfo code
[coreboot.git] / payloads / coreinfo / pci_module.c
1 /*
2  * This file is part of the coreinfo project.
3  *
4  * Copyright (C) 2008 Advanced Micro Devices, Inc.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; version 2 of the License.
9  * 
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  * 
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
18  */
19
20 #include <arch/io.h>
21 #include "coreinfo.h"
22
23 struct pci_devices {
24         unsigned short device;
25         unsigned int id;
26 };
27
28 static struct pci_devices devices[64];
29 static int devices_index;
30
31 #define REG_VENDOR_ID   0x00
32 #define REG_HEADER_TYPE 0x0E
33 #define REG_PRIMARY_BUS 0x18
34
35 #define HEADER_TYPE_NORMAL  0
36 #define HEADER_TYPE_BRIDGE  1
37 #define HEADER_TYPE_CARDBUS 2
38
39 #define PCI_ADDR(_bus, _dev, _reg) \
40 (0x80000000 | (_bus << 16) | (_dev << 8) | (_reg & ~3))
41
42 /* Number of entries to show in the list */
43 #define MENU_VISIBLE 16
44
45 static int menu_selected = 0;
46 static int menu_first = 0;
47
48 static void swap(struct pci_devices *a, struct pci_devices *b)
49 {
50         struct pci_devices tmp;
51
52         tmp.device = a->device;
53         tmp.id = a->id;
54
55         a->device = b->device;
56         a->id = b->id;
57
58         b->device = tmp.device;
59         b->id = tmp.id;
60 }
61
62 static int partition(struct pci_devices *list, int len)
63 {
64         int val = list[len / 2].device;
65         int index = 0;
66         int i;
67
68         swap(&list[len / 2], &list[len - 1]);
69
70         for(i = 0; i < len - 1; i++) {
71
72                 if (list[i].device < val) {
73                         swap(&list[i], &list[index]);
74                         index++;
75                 }
76         }
77
78         swap(&list[index], &list[len - 1]);
79         return index;
80 }
81
82 static void quicksort(struct pci_devices *list, int len)
83 {
84         int index;
85
86         if (len <= 1)
87                 return;
88
89         index = partition(list, len);
90
91         quicksort(list, index);
92         quicksort(&(list[index]), len - index);
93 }
94
95 static void pci_read_dword(unsigned int bus, unsigned int devfn,
96                            unsigned int reg, unsigned int *val)
97 {
98         outl(PCI_ADDR(bus, devfn, reg), 0xCF8);
99         *val = inl(0xCFC);
100 }
101
102 static void pci_read_byte(unsigned int bus, unsigned int devfn,
103                           unsigned int reg, unsigned char *val)
104 {
105         outl(PCI_ADDR(bus, devfn, reg), 0xCF8);
106         *val = inb(0xCFC + (reg & 3));
107 }
108
109 static int show_config_space(WINDOW *win, int row, int col, int index)
110 {
111         unsigned char cspace[64];
112         int bus, devfn;
113         int i, x, y;
114
115         bus = (devices[index].device >> 8) & 0xFF;
116         devfn = devices[index].device & 0xFF;
117
118         for(i = 0; i < 64; i+= 4)
119                 pci_read_dword(bus, devfn, i, ((int *) &cspace[i]));
120
121         for(y = 0; y < 4; y++) {
122                 for(x = 0; x < 16; x++)
123                         mvwprintw(win, row + y, col + (x * 3), "%2.2X ", cspace[(y * 16) + x]);
124         }
125 }
126
127 int pci_module_redraw(WINDOW *win)
128 {
129         unsigned int bus, devfn, func;
130         int i;
131         int last;
132
133         print_module_title(win, "PCI Device List");
134
135         last = menu_first + MENU_VISIBLE;
136
137         if (last > devices_index)
138           last = devices_index;
139
140         for(i = 0; i < MENU_VISIBLE; i++) {
141                 int item = menu_first + i;
142
143                 /* Draw a blank space */
144
145                 if (item >= devices_index) {
146                         wattrset(win, COLOR_PAIR(2));
147                         mvwprintw(win, 2 + i, 1, "                 ");
148                         continue;
149                 }
150
151                 bus = (devices[item].device >> 8) & 0xFF;
152                 devfn = (devices[item].device & 0xFF) / 8;
153                 func = (devices[item].device & 0xFF) % 8;
154
155                 if (item == menu_selected)
156                         wattrset(win, COLOR_PAIR(3) | A_BOLD);
157                 else
158                         wattrset(win, COLOR_PAIR(2));
159
160                 mvwprintw(win, 2+i, 1, "%X:%2.2X.%2.2X %X:%X  ",
161                           bus, devfn, func,
162                           devices[item].id & 0xFFFF,
163                           (devices[item].id >> 16) & 0xFFFF);
164
165                 wattrset(win, COLOR_PAIR(2));
166
167                 if (i == 0) {
168                         if (item != 0)
169                                 mvwprintw(win, 2+ i, 19, "\30");
170                 }
171                 if (i == MENU_VISIBLE - 1) {
172                         if ((item + 1) < devices_index)
173                                 mvwprintw(win, 2+ i, 19, "\31");
174                 }
175         }
176
177         wattrset(win, COLOR_PAIR(2));
178
179         for(i = 0; i < 16; i++)
180                 mvwprintw(win, 2, 26 + (i * 3), "%2.2X ", i);
181
182         wmove(win, 3, 25);
183
184         for(i = 0; i < 48; i++)
185                 waddch(win, (i == 0) ? '\332' : '\304');
186
187         for(i = 0; i < 4; i++) {
188                 mvwprintw(win, 4 + i, 23, "%2.2X", i * 16);
189                 wmove(win, 4 + i, 25);
190                 waddch(win, '\263');
191         }
192
193         show_config_space(win, 4, 26, menu_selected);
194
195         return 0;
196 }
197
198 static void pci_scan_bus(int bus)
199 {
200         int devfn, func;
201         unsigned int val;
202         unsigned char hdr;
203
204         for(devfn = 0; devfn < 0x100; ) {
205                 for(func = 0; func < 8; func++, devfn++) {
206                         pci_read_dword(bus, devfn, REG_VENDOR_ID, &val);
207
208                         /* Nobody home */
209
210                         if (val == 0xffffffff || val == 0x00000000 ||
211                             val == 0x0000ffff || val == 0xffff0000)
212                                 continue;
213
214                         /* FIXME: Remove this arbitrary limitation */
215
216                         if (devices_index >= 64)
217                                 return;
218
219                         devices[devices_index].device = 
220                                 ((bus & 0xFF) << 8) | (devfn & 0xFF);
221
222                         devices[devices_index++].id = val;
223
224                         /* If this is a bridge, then follow it */
225
226                         pci_read_byte(bus, devfn, REG_HEADER_TYPE, &hdr);
227
228                         hdr &= 0x7F;
229
230                         if (hdr == HEADER_TYPE_BRIDGE ||
231                             hdr == HEADER_TYPE_CARDBUS) {
232                                 unsigned int busses;
233
234                                 pci_read_dword(bus, devfn, REG_PRIMARY_BUS,
235                                                &busses);
236
237                                 pci_scan_bus((busses >> 8) & 0xFF);
238
239                         }
240                 }
241         }
242
243         quicksort(devices, devices_index);
244 }
245
246 int pci_module_handle(int key)
247 {
248         int ret = 0;
249
250         switch(key) {
251         case KEY_DOWN:
252                 if (menu_selected + 1 < devices_index) {
253                         menu_selected++;
254                         ret = 1;
255                 }
256
257                 break;
258
259         case KEY_UP:
260                 if (menu_selected > 0) {
261                         menu_selected--;
262                         ret = 1;
263                 }
264
265                 break;
266         }
267
268         if (!ret)
269                 return ret;
270
271         if (menu_selected < menu_first)
272                 menu_first = menu_selected;
273         else if (menu_selected >= menu_first + MENU_VISIBLE) {
274                 menu_first = menu_selected - (MENU_VISIBLE - 1);
275                 if (menu_first < 0)
276                         menu_first = 0;
277         }
278
279
280         return ret;
281 }
282
283 int pci_module_init(void)
284 {
285         unsigned int val;
286         int bus = 0;
287
288
289         pci_scan_bus(0);
290
291         return 0;
292 }
293
294 struct coreinfo_module pci_module = {
295         .name = "PCI",
296         .init = pci_module_init,
297         .redraw = pci_module_redraw,
298         .handle = pci_module_handle,
299 };