2 * This file is part of the coreinfo project.
4 * Copyright (C) 2008 Advanced Micro Devices, Inc.
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.
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.
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
21 #include <libpayload.h>
24 #ifdef CONFIG_MODULE_PCI
27 unsigned short device;
31 static struct pci_devices devices[64];
32 static int devices_index;
34 /* Number of entries to show in the list */
35 #define MENU_VISIBLE 16
37 static int menu_selected = 0;
38 static int menu_first = 0;
40 static void swap(struct pci_devices *a, struct pci_devices *b)
42 struct pci_devices tmp;
44 tmp.device = a->device;
47 a->device = b->device;
50 b->device = tmp.device;
54 static int partition(struct pci_devices *list, int len)
56 int val = list[len / 2].device;
60 swap(&list[len / 2], &list[len - 1]);
62 for (i = 0; i < len - 1; i++) {
63 if (list[i].device < val) {
64 swap(&list[i], &list[index]);
69 swap(&list[index], &list[len - 1]);
74 static void quicksort(struct pci_devices *list, int len)
81 index = partition(list, len);
83 quicksort(list, index);
84 quicksort(&(list[index]), len - index);
87 static void show_config_space(WINDOW *win, int row, int col, int index)
89 unsigned char cspace[64];
93 bus = (devices[index].device >> 8) & 0xff;
94 devfn = devices[index].device & 0xff;
96 for (i = 0; i < 64; i += 4)
97 cspace[i] = pci_read_config32(PCI_DEV(bus, PCI_SLOT(devfn),
100 for (y = 0; y < 4; y++) {
101 for (x = 0; x < 16; x++)
102 mvwprintw(win, row + y, col + (x * 3), "%2.2X ",
103 cspace[(y * 16) + x]);
107 static int pci_module_redraw(WINDOW *win)
109 unsigned int bus, devfn, func;
112 print_module_title(win, "PCI Device List");
114 last = menu_first + MENU_VISIBLE;
116 if (last > devices_index)
117 last = devices_index;
119 for (i = 0; i < MENU_VISIBLE; i++) {
120 int item = menu_first + i;
122 /* Draw a blank space. */
123 if (item >= devices_index) {
124 wattrset(win, COLOR_PAIR(2));
125 mvwprintw(win, 2 + i, 1, " ");
129 bus = (devices[item].device >> 8) & 0xff;
130 devfn = (devices[item].device & 0xff) / 8;
131 func = (devices[item].device & 0xff) % 8;
133 if (item == menu_selected)
134 wattrset(win, COLOR_PAIR(3) | A_BOLD);
136 wattrset(win, COLOR_PAIR(2));
138 mvwprintw(win, 2 + i, 1, "%X:%2.2X.%2.2X %04X:%04X ",
140 devices[item].id & 0xffff,
141 (devices[item].id >> 16) & 0xffff);
143 wattrset(win, COLOR_PAIR(2));
147 mvwprintw(win, 2 + i, 19, "\30");
149 if (i == MENU_VISIBLE - 1) {
150 if ((item + 1) < devices_index)
151 mvwprintw(win, 2 + i, 19, "\31");
155 wattrset(win, COLOR_PAIR(2));
157 for (i = 0; i < 16; i++)
158 mvwprintw(win, 2, 26 + (i * 3), "%2.2X ", i);
162 for (i = 0; i < 48; i++)
163 waddch(win, (i == 0) ? '\332' : '\304');
165 for (i = 0; i < 4; i++) {
166 mvwprintw(win, 4 + i, 23, "%2.2X", i * 16);
167 wmove(win, 4 + i, 25);
171 show_config_space(win, 4, 26, menu_selected);
176 static void pci_scan_bus(int bus)
182 for (devfn = 0; devfn < 0x100;) {
183 for (func = 0; func < 8; func++, devfn++) {
184 pcidev_t dev = PCI_DEV(bus, PCI_SLOT(devfn),
187 val = pci_read_config32(dev, REG_VENDOR_ID);
190 if (val == 0xffffffff || val == 0x00000000 ||
191 val == 0x0000ffff || val == 0xffff0000)
194 /* FIXME: Remove this arbitrary limitation. */
195 if (devices_index >= 64)
198 devices[devices_index].device =
199 ((bus & 0xff) << 8) | (devfn & 0xff);
201 devices[devices_index++].id = val;
203 /* If this is a bridge, then follow it. */
204 hdr = pci_read_config8(dev, REG_HEADER_TYPE);
206 if (hdr == HEADER_TYPE_BRIDGE ||
207 hdr == HEADER_TYPE_CARDBUS) {
210 busses = pci_read_config32(dev, REG_PRIMARY_BUS);
212 pci_scan_bus((busses >> 8) & 0xff);
218 quicksort(devices, devices_index);
221 static int pci_module_handle(int key)
227 if (menu_selected + 1 < devices_index) {
233 if (menu_selected > 0) {
243 if (menu_selected < menu_first)
244 menu_first = menu_selected;
245 else if (menu_selected >= menu_first + MENU_VISIBLE) {
246 menu_first = menu_selected - (MENU_VISIBLE - 1);
254 static int pci_module_init(void)
260 struct coreinfo_module pci_module = {
262 .init = pci_module_init,
263 .redraw = pci_module_redraw,
264 .handle = pci_module_handle,
269 struct coreinfo_module pci_module = {