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