Quickfix to repair 'make clean; make menuconfig' (trivial).
[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 #ifdef CONFIG_MODULE_PCI
24
25 struct pci_devices {
26         unsigned short device;
27         unsigned int id;
28 };
29
30 static struct pci_devices devices[64];
31 static int devices_index;
32
33 #define REG_VENDOR_ID   0x00
34 #define REG_HEADER_TYPE 0x0e
35 #define REG_PRIMARY_BUS 0x18
36
37 #define HEADER_TYPE_NORMAL  0
38 #define HEADER_TYPE_BRIDGE  1
39 #define HEADER_TYPE_CARDBUS 2
40
41 #define PCI_ADDR(_bus, _dev, _reg) \
42         (0x80000000 | (_bus << 16) | (_dev << 8) | (_reg & ~3))
43
44 /* Number of entries to show in the list */
45 #define MENU_VISIBLE 16
46
47 static int menu_selected = 0;
48 static int menu_first = 0;
49
50 static void swap(struct pci_devices *a, struct pci_devices *b)
51 {
52         struct pci_devices tmp;
53
54         tmp.device = a->device;
55         tmp.id = a->id;
56
57         a->device = b->device;
58         a->id = b->id;
59
60         b->device = tmp.device;
61         b->id = tmp.id;
62 }
63
64 static int partition(struct pci_devices *list, int len)
65 {
66         int val = list[len / 2].device;
67         int index = 0;
68         int i;
69
70         swap(&list[len / 2], &list[len - 1]);
71
72         for (i = 0; i < len - 1; i++) {
73                 if (list[i].device < val) {
74                         swap(&list[i], &list[index]);
75                         index++;
76                 }
77         }
78
79         swap(&list[index], &list[len - 1]);
80
81         return index;
82 }
83
84 static void quicksort(struct pci_devices *list, int len)
85 {
86         int index;
87
88         if (len <= 1)
89                 return;
90
91         index = partition(list, len);
92
93         quicksort(list, index);
94         quicksort(&(list[index]), len - index);
95 }
96
97 static void pci_read_dword(unsigned int bus, unsigned int devfn,
98                            unsigned int reg, unsigned int *val)
99 {
100         outl(PCI_ADDR(bus, devfn, reg), 0xcf8);
101         *val = inl(0xcfc);
102 }
103
104 static void pci_read_byte(unsigned int bus, unsigned int devfn,
105                           unsigned int reg, unsigned char *val)
106 {
107         outl(PCI_ADDR(bus, devfn, reg), 0xcf8);
108         *val = inb(0xcfc + (reg & 3));
109 }
110
111 static void show_config_space(WINDOW *win, int row, int col, int index)
112 {
113         unsigned char cspace[64];
114         int bus, devfn;
115         int i, x, y;
116
117         bus = (devices[index].device >> 8) & 0xff;
118         devfn = devices[index].device & 0xff;
119
120         for (i = 0; i < 64; i += 4)
121                 pci_read_dword(bus, devfn, i, ((unsigned int *)&cspace[i]));
122
123         for (y = 0; y < 4; y++) {
124                 for (x = 0; x < 16; x++)
125                         mvwprintw(win, row + y, col + (x * 3), "%2.2X ",
126                                   cspace[(y * 16) + x]);
127         }
128 }
129
130 static int pci_module_redraw(WINDOW *win)
131 {
132         unsigned int bus, devfn, func;
133         int i, last;
134
135         print_module_title(win, "PCI Device List");
136
137         last = menu_first + MENU_VISIBLE;
138
139         if (last > devices_index)
140                 last = devices_index;
141
142         for (i = 0; i < MENU_VISIBLE; i++) {
143                 int item = menu_first + i;
144
145                 /* Draw a blank space. */
146                 if (item >= devices_index) {
147                         wattrset(win, COLOR_PAIR(2));
148                         mvwprintw(win, 2 + i, 1, "                 ");
149                         continue;
150                 }
151
152                 bus = (devices[item].device >> 8) & 0xff;
153                 devfn = (devices[item].device & 0xff) / 8;
154                 func = (devices[item].device & 0xff) % 8;
155
156                 if (item == menu_selected)
157                         wattrset(win, COLOR_PAIR(3) | A_BOLD);
158                 else
159                         wattrset(win, COLOR_PAIR(2));
160
161                 mvwprintw(win, 2 + i, 1, "%X:%2.2X.%2.2X %04X:%04X  ",
162                           bus, devfn, func,
163                           devices[item].id & 0xffff,
164                           (devices[item].id >> 16) & 0xffff);
165
166                 wattrset(win, COLOR_PAIR(2));
167
168                 if (i == 0) {
169                         if (item != 0)
170                                 mvwprintw(win, 2 + i, 19, "\30");
171                 }
172                 if (i == MENU_VISIBLE - 1) {
173                         if ((item + 1) < devices_index)
174                                 mvwprintw(win, 2 + i, 19, "\31");
175                 }
176         }
177
178         wattrset(win, COLOR_PAIR(2));
179
180         for (i = 0; i < 16; i++)
181                 mvwprintw(win, 2, 26 + (i * 3), "%2.2X ", i);
182
183         wmove(win, 3, 25);
184
185         for (i = 0; i < 48; i++)
186                 waddch(win, (i == 0) ? '\332' : '\304');
187
188         for (i = 0; i < 4; i++) {
189                 mvwprintw(win, 4 + i, 23, "%2.2X", i * 16);
190                 wmove(win, 4 + i, 25);
191                 waddch(win, '\263');
192         }
193
194         show_config_space(win, 4, 26, menu_selected);
195
196         return 0;
197 }
198
199 static void pci_scan_bus(int bus)
200 {
201         int devfn, func;
202         unsigned int val;
203         unsigned char hdr;
204
205         for (devfn = 0; devfn < 0x100;) {
206                 for (func = 0; func < 8; func++, devfn++) {
207                         pci_read_dword(bus, devfn, REG_VENDOR_ID, &val);
208
209                         /* Nobody home. */
210                         if (val == 0xffffffff || val == 0x00000000 ||
211                             val == 0x0000ffff || val == 0xffff0000)
212                                 continue;
213
214                         /* FIXME: Remove this arbitrary limitation. */
215                         if (devices_index >= 64)
216                                 return;
217
218                         devices[devices_index].device =
219                             ((bus & 0xff) << 8) | (devfn & 0xff);
220
221                         devices[devices_index++].id = val;
222
223                         /* If this is a bridge, then follow it. */
224                         pci_read_byte(bus, devfn, REG_HEADER_TYPE, &hdr);
225                         hdr &= 0x7f;
226                         if (hdr == HEADER_TYPE_BRIDGE ||
227                             hdr == HEADER_TYPE_CARDBUS) {
228                                 unsigned int busses;
229
230                                 pci_read_dword(bus, devfn, REG_PRIMARY_BUS,
231                                                &busses);
232
233                                 pci_scan_bus((busses >> 8) & 0xff);
234
235                         }
236                 }
237         }
238
239         quicksort(devices, devices_index);
240 }
241
242 static int pci_module_handle(int key)
243 {
244         int ret = 0;
245
246         switch (key) {
247         case KEY_DOWN:
248                 if (menu_selected + 1 < devices_index) {
249                         menu_selected++;
250                         ret = 1;
251                 }
252                 break;
253         case KEY_UP:
254                 if (menu_selected > 0) {
255                         menu_selected--;
256                         ret = 1;
257                 }
258                 break;
259         }
260
261         if (!ret)
262                 return ret;
263
264         if (menu_selected < menu_first)
265                 menu_first = menu_selected;
266         else if (menu_selected >= menu_first + MENU_VISIBLE) {
267                 menu_first = menu_selected - (MENU_VISIBLE - 1);
268                 if (menu_first < 0)
269                         menu_first = 0;
270         }
271
272         return ret;
273 }
274
275 static int pci_module_init(void)
276 {
277         pci_scan_bus(0);
278         return 0;
279 }
280
281 struct coreinfo_module pci_module = {
282         .name = "PCI",
283         .init = pci_module_init,
284         .redraw = pci_module_redraw,
285         .handle = pci_module_handle,
286 };
287
288 #else
289
290 struct coreinfo_module pci_module = {
291 };
292
293 #endif