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
24 unsigned short device;
28 static struct pci_devices devices[64];
29 static int devices_index;
31 #define REG_VENDOR_ID 0x00
32 #define REG_HEADER_TYPE 0x0e
33 #define REG_PRIMARY_BUS 0x18
35 #define HEADER_TYPE_NORMAL 0
36 #define HEADER_TYPE_BRIDGE 1
37 #define HEADER_TYPE_CARDBUS 2
39 #define PCI_ADDR(_bus, _dev, _reg) \
40 (0x80000000 | (_bus << 16) | (_dev << 8) | (_reg & ~3))
42 /* Number of entries to show in the list */
43 #define MENU_VISIBLE 16
45 static int menu_selected = 0;
46 static int menu_first = 0;
48 static void swap(struct pci_devices *a, struct pci_devices *b)
50 struct pci_devices tmp;
52 tmp.device = a->device;
55 a->device = b->device;
58 b->device = tmp.device;
62 static int partition(struct pci_devices *list, int len)
64 int val = list[len / 2].device;
68 swap(&list[len / 2], &list[len - 1]);
70 for (i = 0; i < len - 1; i++) {
71 if (list[i].device < val) {
72 swap(&list[i], &list[index]);
77 swap(&list[index], &list[len - 1]);
82 static void quicksort(struct pci_devices *list, int len)
89 index = partition(list, len);
91 quicksort(list, index);
92 quicksort(&(list[index]), len - index);
95 static void pci_read_dword(unsigned int bus, unsigned int devfn,
96 unsigned int reg, unsigned int *val)
98 outl(PCI_ADDR(bus, devfn, reg), 0xcf8);
102 static void pci_read_byte(unsigned int bus, unsigned int devfn,
103 unsigned int reg, unsigned char *val)
105 outl(PCI_ADDR(bus, devfn, reg), 0xcf8);
106 *val = inb(0xcfc + (reg & 3));
109 static void show_config_space(WINDOW *win, int row, int col, int index)
111 unsigned char cspace[64];
115 bus = (devices[index].device >> 8) & 0xff;
116 devfn = devices[index].device & 0xff;
118 for (i = 0; i < 64; i += 4)
119 pci_read_dword(bus, devfn, i, ((unsigned int *)&cspace[i]));
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]);
128 int pci_module_redraw(WINDOW *win)
130 unsigned int bus, devfn, func;
133 print_module_title(win, "PCI Device List");
135 last = menu_first + MENU_VISIBLE;
137 if (last > devices_index)
138 last = devices_index;
140 for (i = 0; i < MENU_VISIBLE; i++) {
141 int item = menu_first + i;
143 /* Draw a blank space. */
144 if (item >= devices_index) {
145 wattrset(win, COLOR_PAIR(2));
146 mvwprintw(win, 2 + i, 1, " ");
150 bus = (devices[item].device >> 8) & 0xff;
151 devfn = (devices[item].device & 0xff) / 8;
152 func = (devices[item].device & 0xff) % 8;
154 if (item == menu_selected)
155 wattrset(win, COLOR_PAIR(3) | A_BOLD);
157 wattrset(win, COLOR_PAIR(2));
159 mvwprintw(win, 2 + i, 1, "%X:%2.2X.%2.2X %X:%X ",
161 devices[item].id & 0xffff,
162 (devices[item].id >> 16) & 0xffff);
164 wattrset(win, COLOR_PAIR(2));
168 mvwprintw(win, 2 + i, 19, "\30");
170 if (i == MENU_VISIBLE - 1) {
171 if ((item + 1) < devices_index)
172 mvwprintw(win, 2 + i, 19, "\31");
176 wattrset(win, COLOR_PAIR(2));
178 for (i = 0; i < 16; i++)
179 mvwprintw(win, 2, 26 + (i * 3), "%2.2X ", i);
183 for (i = 0; i < 48; i++)
184 waddch(win, (i == 0) ? '\332' : '\304');
186 for (i = 0; i < 4; i++) {
187 mvwprintw(win, 4 + i, 23, "%2.2X", i * 16);
188 wmove(win, 4 + i, 25);
192 show_config_space(win, 4, 26, menu_selected);
197 static void pci_scan_bus(int bus)
203 for (devfn = 0; devfn < 0x100;) {
204 for (func = 0; func < 8; func++, devfn++) {
205 pci_read_dword(bus, devfn, REG_VENDOR_ID, &val);
208 if (val == 0xffffffff || val == 0x00000000 ||
209 val == 0x0000ffff || val == 0xffff0000)
212 /* FIXME: Remove this arbitrary limitation. */
213 if (devices_index >= 64)
216 devices[devices_index].device =
217 ((bus & 0xff) << 8) | (devfn & 0xff);
219 devices[devices_index++].id = val;
221 /* If this is a bridge, then follow it. */
222 pci_read_byte(bus, devfn, REG_HEADER_TYPE, &hdr);
224 if (hdr == HEADER_TYPE_BRIDGE ||
225 hdr == HEADER_TYPE_CARDBUS) {
228 pci_read_dword(bus, devfn, REG_PRIMARY_BUS,
231 pci_scan_bus((busses >> 8) & 0xff);
237 quicksort(devices, devices_index);
240 int pci_module_handle(int key)
246 if (menu_selected + 1 < devices_index) {
252 if (menu_selected > 0) {
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);
273 int pci_module_init(void)
279 struct coreinfo_module pci_module = {
281 .init = pci_module_init,
282 .redraw = pci_module_redraw,
283 .handle = pci_module_handle,