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
23 #ifdef CONFIG_MODULE_PCI
26 unsigned short device;
30 static struct pci_devices devices[64];
31 static int devices_index;
33 #define REG_VENDOR_ID 0x00
34 #define REG_HEADER_TYPE 0x0e
35 #define REG_PRIMARY_BUS 0x18
37 #define HEADER_TYPE_NORMAL 0
38 #define HEADER_TYPE_BRIDGE 1
39 #define HEADER_TYPE_CARDBUS 2
41 #define PCI_ADDR(_bus, _dev, _reg) \
42 (0x80000000 | (_bus << 16) | (_dev << 8) | (_reg & ~3))
44 /* Number of entries to show in the list */
45 #define MENU_VISIBLE 16
47 static int menu_selected = 0;
48 static int menu_first = 0;
50 static void swap(struct pci_devices *a, struct pci_devices *b)
52 struct pci_devices tmp;
54 tmp.device = a->device;
57 a->device = b->device;
60 b->device = tmp.device;
64 static int partition(struct pci_devices *list, int len)
66 int val = list[len / 2].device;
70 swap(&list[len / 2], &list[len - 1]);
72 for (i = 0; i < len - 1; i++) {
73 if (list[i].device < val) {
74 swap(&list[i], &list[index]);
79 swap(&list[index], &list[len - 1]);
84 static void quicksort(struct pci_devices *list, int len)
91 index = partition(list, len);
93 quicksort(list, index);
94 quicksort(&(list[index]), len - index);
97 static void pci_read_dword(unsigned int bus, unsigned int devfn,
98 unsigned int reg, unsigned int *val)
100 outl(PCI_ADDR(bus, devfn, reg), 0xcf8);
104 static void pci_read_byte(unsigned int bus, unsigned int devfn,
105 unsigned int reg, unsigned char *val)
107 outl(PCI_ADDR(bus, devfn, reg), 0xcf8);
108 *val = inb(0xcfc + (reg & 3));
111 static void show_config_space(WINDOW *win, int row, int col, int index)
113 unsigned char cspace[64];
117 bus = (devices[index].device >> 8) & 0xff;
118 devfn = devices[index].device & 0xff;
120 for (i = 0; i < 64; i += 4)
121 pci_read_dword(bus, devfn, i, ((unsigned int *)&cspace[i]));
123 for (y = 0; y < 4; y++) {
124 for (x = 0; x < 16; x++)
125 mvwprintw(win, row + y, col + (x * 3), "%2.2X ",
126 cspace[(y * 16) + x]);
130 static int pci_module_redraw(WINDOW *win)
132 unsigned int bus, devfn, func;
135 print_module_title(win, "PCI Device List");
137 last = menu_first + MENU_VISIBLE;
139 if (last > devices_index)
140 last = devices_index;
142 for (i = 0; i < MENU_VISIBLE; i++) {
143 int item = menu_first + i;
145 /* Draw a blank space. */
146 if (item >= devices_index) {
147 wattrset(win, COLOR_PAIR(2));
148 mvwprintw(win, 2 + i, 1, " ");
152 bus = (devices[item].device >> 8) & 0xff;
153 devfn = (devices[item].device & 0xff) / 8;
154 func = (devices[item].device & 0xff) % 8;
156 if (item == menu_selected)
157 wattrset(win, COLOR_PAIR(3) | A_BOLD);
159 wattrset(win, COLOR_PAIR(2));
161 mvwprintw(win, 2 + i, 1, "%X:%2.2X.%2.2X %04X:%04X ",
163 devices[item].id & 0xffff,
164 (devices[item].id >> 16) & 0xffff);
166 wattrset(win, COLOR_PAIR(2));
170 mvwprintw(win, 2 + i, 19, "\30");
172 if (i == MENU_VISIBLE - 1) {
173 if ((item + 1) < devices_index)
174 mvwprintw(win, 2 + i, 19, "\31");
178 wattrset(win, COLOR_PAIR(2));
180 for (i = 0; i < 16; i++)
181 mvwprintw(win, 2, 26 + (i * 3), "%2.2X ", i);
185 for (i = 0; i < 48; i++)
186 waddch(win, (i == 0) ? '\332' : '\304');
188 for (i = 0; i < 4; i++) {
189 mvwprintw(win, 4 + i, 23, "%2.2X", i * 16);
190 wmove(win, 4 + i, 25);
194 show_config_space(win, 4, 26, menu_selected);
199 static void pci_scan_bus(int bus)
205 for (devfn = 0; devfn < 0x100;) {
206 for (func = 0; func < 8; func++, devfn++) {
207 pci_read_dword(bus, devfn, REG_VENDOR_ID, &val);
210 if (val == 0xffffffff || val == 0x00000000 ||
211 val == 0x0000ffff || val == 0xffff0000)
214 /* FIXME: Remove this arbitrary limitation. */
215 if (devices_index >= 64)
218 devices[devices_index].device =
219 ((bus & 0xff) << 8) | (devfn & 0xff);
221 devices[devices_index++].id = val;
223 /* If this is a bridge, then follow it. */
224 pci_read_byte(bus, devfn, REG_HEADER_TYPE, &hdr);
226 if (hdr == HEADER_TYPE_BRIDGE ||
227 hdr == HEADER_TYPE_CARDBUS) {
230 pci_read_dword(bus, devfn, REG_PRIMARY_BUS,
233 pci_scan_bus((busses >> 8) & 0xff);
239 quicksort(devices, devices_index);
242 static int pci_module_handle(int key)
248 if (menu_selected + 1 < devices_index) {
254 if (menu_selected > 0) {
264 if (menu_selected < menu_first)
265 menu_first = menu_selected;
266 else if (menu_selected >= menu_first + MENU_VISIBLE) {
267 menu_first = menu_selected - (MENU_VISIBLE - 1);
275 static int pci_module_init(void)
281 struct coreinfo_module pci_module = {
283 .init = pci_module_init,
284 .redraw = pci_module_redraw,
285 .handle = pci_module_handle,
290 struct coreinfo_module pci_module = {