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