Smaller fixes to allow using -Wall (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 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                 if (list[i].device < val) {
72                         swap(&list[i], &list[index]);
73                         index++;
74                 }
75         }
76
77         swap(&list[index], &list[len - 1]);
78
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 void 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, ((unsigned 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 ",
124                                   cspace[(y * 16) + x]);
125         }
126 }
127
128 int pci_module_redraw(WINDOW *win)
129 {
130         unsigned int bus, devfn, func;
131         int i, 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                 if (item >= devices_index) {
145                         wattrset(win, COLOR_PAIR(2));
146                         mvwprintw(win, 2 + i, 1, "                 ");
147                         continue;
148                 }
149
150                 bus = (devices[item].device >> 8) & 0xff;
151                 devfn = (devices[item].device & 0xff) / 8;
152                 func = (devices[item].device & 0xff) % 8;
153
154                 if (item == menu_selected)
155                         wattrset(win, COLOR_PAIR(3) | A_BOLD);
156                 else
157                         wattrset(win, COLOR_PAIR(2));
158
159                 mvwprintw(win, 2 + i, 1, "%X:%2.2X.%2.2X %X:%X  ",
160                           bus, devfn, func,
161                           devices[item].id & 0xffff,
162                           (devices[item].id >> 16) & 0xffff);
163
164                 wattrset(win, COLOR_PAIR(2));
165
166                 if (i == 0) {
167                         if (item != 0)
168                                 mvwprintw(win, 2 + i, 19, "\30");
169                 }
170                 if (i == MENU_VISIBLE - 1) {
171                         if ((item + 1) < devices_index)
172                                 mvwprintw(win, 2 + i, 19, "\31");
173                 }
174         }
175
176         wattrset(win, COLOR_PAIR(2));
177
178         for (i = 0; i < 16; i++)
179                 mvwprintw(win, 2, 26 + (i * 3), "%2.2X ", i);
180
181         wmove(win, 3, 25);
182
183         for (i = 0; i < 48; i++)
184                 waddch(win, (i == 0) ? '\332' : '\304');
185
186         for (i = 0; i < 4; i++) {
187                 mvwprintw(win, 4 + i, 23, "%2.2X", i * 16);
188                 wmove(win, 4 + i, 25);
189                 waddch(win, '\263');
190         }
191
192         show_config_space(win, 4, 26, menu_selected);
193
194         return 0;
195 }
196
197 static void pci_scan_bus(int bus)
198 {
199         int devfn, func;
200         unsigned int val;
201         unsigned char hdr;
202
203         for (devfn = 0; devfn < 0x100;) {
204                 for (func = 0; func < 8; func++, devfn++) {
205                         pci_read_dword(bus, devfn, REG_VENDOR_ID, &val);
206
207                         /* Nobody home. */
208                         if (val == 0xffffffff || val == 0x00000000 ||
209                             val == 0x0000ffff || val == 0xffff0000)
210                                 continue;
211
212                         /* FIXME: Remove this arbitrary limitation. */
213                         if (devices_index >= 64)
214                                 return;
215
216                         devices[devices_index].device =
217                             ((bus & 0xff) << 8) | (devfn & 0xff);
218
219                         devices[devices_index++].id = val;
220
221                         /* If this is a bridge, then follow it. */
222                         pci_read_byte(bus, devfn, REG_HEADER_TYPE, &hdr);
223                         hdr &= 0x7f;
224                         if (hdr == HEADER_TYPE_BRIDGE ||
225                             hdr == HEADER_TYPE_CARDBUS) {
226                                 unsigned int busses;
227
228                                 pci_read_dword(bus, devfn, REG_PRIMARY_BUS,
229                                                &busses);
230
231                                 pci_scan_bus((busses >> 8) & 0xff);
232
233                         }
234                 }
235         }
236
237         quicksort(devices, devices_index);
238 }
239
240 int pci_module_handle(int key)
241 {
242         int ret = 0;
243
244         switch (key) {
245         case KEY_DOWN:
246                 if (menu_selected + 1 < devices_index) {
247                         menu_selected++;
248                         ret = 1;
249                 }
250                 break;
251         case KEY_UP:
252                 if (menu_selected > 0) {
253                         menu_selected--;
254                         ret = 1;
255                 }
256                 break;
257         }
258
259         if (!ret)
260                 return ret;
261
262         if (menu_selected < menu_first)
263                 menu_first = menu_selected;
264         else if (menu_selected >= menu_first + MENU_VISIBLE) {
265                 menu_first = menu_selected - (MENU_VISIBLE - 1);
266                 if (menu_first < 0)
267                         menu_first = 0;
268         }
269
270         return ret;
271 }
272
273 int pci_module_init(void)
274 {
275         pci_scan_bus(0);
276         return 0;
277 }
278
279 struct coreinfo_module pci_module = {
280         .name = "PCI",
281         .init = pci_module_init,
282         .redraw = pci_module_redraw,
283         .handle = pci_module_handle,
284 };