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
22 #include <libpayload.h>
25 #ifdef CONFIG_MODULE_PCI
32 static struct pci_devices devices[64];
33 static int devices_index;
35 /* Number of entries to show in the list */
36 #define MENU_VISIBLE 16
38 static int menu_selected = 0;
39 static int menu_first = 0;
41 static void swap(struct pci_devices *a, struct pci_devices *b)
43 struct pci_devices tmp;
45 tmp.device = a->device;
48 a->device = b->device;
51 b->device = tmp.device;
55 static int partition(struct pci_devices *list, int len)
57 int val = list[len / 2].device;
61 swap(&list[len / 2], &list[len - 1]);
63 for (i = 0; i < len - 1; i++) {
64 if (list[i].device < val) {
65 swap(&list[i], &list[index]);
70 swap(&list[index], &list[len - 1]);
75 static void quicksort(struct pci_devices *list, int len)
82 index = partition(list, len);
84 quicksort(list, index);
85 quicksort(&(list[index]), len - index);
88 static void show_config_space(WINDOW *win, int row, int col, int index)
90 unsigned char cspace[256];
94 dev = devices[index].device;
96 for (i = 0; i < 256; i ++)
97 cspace[i] = pci_read_config8(dev, i);
99 for (y = 0; y < 16; y++) {
100 for (x = 0; x < 16; x++)
101 mvwprintw(win, row + y, col + (x * 3), "%2.2X ",
102 cspace[(y * 16) + x]);
106 static int pci_module_redraw(WINDOW *win)
108 unsigned int bus, slot, func;
111 print_module_title(win, "PCI Device List");
113 last = menu_first + MENU_VISIBLE;
115 if (last > devices_index)
116 last = devices_index;
118 for (i = 0; i < MENU_VISIBLE; i++) {
119 int item = menu_first + i;
121 /* Draw a blank space. */
122 if (item >= devices_index) {
123 wattrset(win, COLOR_PAIR(2));
124 mvwprintw(win, 2 + i, 1, " ");
128 bus = PCI_BUS(devices[item].device);
129 slot = PCI_SLOT(devices[item].device);
130 func = PCI_FUNC(devices[item].device);
132 if (item == menu_selected)
133 wattrset(win, COLOR_PAIR(3) | A_BOLD);
135 wattrset(win, COLOR_PAIR(2));
137 mvwprintw(win, 2 + i, 1, "%X:%2.2X.%2.2X %04X:%04X ",
139 devices[item].id & 0xffff,
140 (devices[item].id >> 16) & 0xffff);
142 wattrset(win, COLOR_PAIR(2));
146 mvwaddch(win, 2 + i, 19, ACS_UARROW);
148 if (i == MENU_VISIBLE - 1) {
149 if ((item + 1) < devices_index)
150 mvwaddch(win, 2 + i, 19, ACS_DARROW);
154 wattrset(win, COLOR_PAIR(2));
156 for (i = 0; i < 16; i++)
157 mvwprintw(win, 2, 26 + (i * 3), "%2.2X ", i);
161 for (i = 0; i < 48; i++)
162 waddch(win, (i == 0) ? ACS_ULCORNER : ACS_HLINE);
164 for (i = 0; i < 16; i++) {
165 mvwprintw(win, 4 + i, 23, "%2.2X", i * 16);
166 wmove(win, 4 + i, 25);
167 waddch(win, ACS_VLINE);
170 show_config_space(win, 4, 26, menu_selected);
175 static void pci_scan_bus(int bus)
181 for (slot = 0; slot < 0x1f; slot++) {
182 for (func = 0; func < 8; func++) {
183 pcidev_t dev = PCI_DEV(bus, slot, func);
185 val = pci_read_config32(dev, REG_VENDOR_ID);
188 if (val == 0xffffffff || val == 0x00000000 ||
189 val == 0x0000ffff || val == 0xffff0000)
192 /* FIXME: Remove this arbitrary limitation. */
193 if (devices_index >= 64)
196 devices[devices_index].device =
197 PCI_DEV(bus, slot, func);
199 devices[devices_index++].id = val;
201 /* If this is a bridge, then follow it. */
202 hdr = pci_read_config8(dev, REG_HEADER_TYPE);
204 if (hdr == HEADER_TYPE_BRIDGE ||
205 hdr == HEADER_TYPE_CARDBUS) {
208 busses = pci_read_config32(dev, REG_PRIMARY_BUS);
210 pci_scan_bus((busses >> 8) & 0xff);
216 quicksort(devices, devices_index);
219 static int pci_module_handle(int key)
225 if (menu_selected + 1 < devices_index) {
231 if (menu_selected > 0) {
241 if (menu_selected < menu_first)
242 menu_first = menu_selected;
243 else if (menu_selected >= menu_first + MENU_VISIBLE) {
244 menu_first = menu_selected - (MENU_VISIBLE - 1);
252 static int pci_module_init(void)
258 struct coreinfo_module pci_module = {
260 .init = pci_module_init,
261 .redraw = pci_module_redraw,
262 .handle = pci_module_handle,
267 struct coreinfo_module pci_module = {