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++) {
72 if (list[i].device < val) {
73 swap(&list[i], &list[index]);
78 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 int 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, ((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 ", cspace[(y * 16) + x]);
127 int pci_module_redraw(WINDOW *win)
129 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 */
145 if (item >= devices_index) {
146 wattrset(win, COLOR_PAIR(2));
147 mvwprintw(win, 2 + i, 1, " ");
151 bus = (devices[item].device >> 8) & 0xFF;
152 devfn = (devices[item].device & 0xFF) / 8;
153 func = (devices[item].device & 0xFF) % 8;
155 if (item == menu_selected)
156 wattrset(win, COLOR_PAIR(3) | A_BOLD);
158 wattrset(win, COLOR_PAIR(2));
160 mvwprintw(win, 2+i, 1, "%X:%2.2X.%2.2X %X:%X ",
162 devices[item].id & 0xFFFF,
163 (devices[item].id >> 16) & 0xFFFF);
165 wattrset(win, COLOR_PAIR(2));
169 mvwprintw(win, 2+ i, 19, "\30");
171 if (i == MENU_VISIBLE - 1) {
172 if ((item + 1) < devices_index)
173 mvwprintw(win, 2+ i, 19, "\31");
177 wattrset(win, COLOR_PAIR(2));
179 for(i = 0; i < 16; i++)
180 mvwprintw(win, 2, 26 + (i * 3), "%2.2X ", i);
184 for(i = 0; i < 48; i++)
185 waddch(win, (i == 0) ? '\332' : '\304');
187 for(i = 0; i < 4; i++) {
188 mvwprintw(win, 4 + i, 23, "%2.2X", i * 16);
189 wmove(win, 4 + i, 25);
193 show_config_space(win, 4, 26, menu_selected);
198 static void pci_scan_bus(int bus)
204 for(devfn = 0; devfn < 0x100; ) {
205 for(func = 0; func < 8; func++, devfn++) {
206 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 */
216 if (devices_index >= 64)
219 devices[devices_index].device =
220 ((bus & 0xFF) << 8) | (devfn & 0xFF);
222 devices[devices_index++].id = val;
224 /* If this is a bridge, then follow it */
226 pci_read_byte(bus, devfn, REG_HEADER_TYPE, &hdr);
230 if (hdr == HEADER_TYPE_BRIDGE ||
231 hdr == HEADER_TYPE_CARDBUS) {
234 pci_read_dword(bus, devfn, REG_PRIMARY_BUS,
237 pci_scan_bus((busses >> 8) & 0xFF);
243 quicksort(devices, devices_index);
246 int pci_module_handle(int key)
252 if (menu_selected + 1 < devices_index) {
260 if (menu_selected > 0) {
271 if (menu_selected < menu_first)
272 menu_first = menu_selected;
273 else if (menu_selected >= menu_first + MENU_VISIBLE) {
274 menu_first = menu_selected - (MENU_VISIBLE - 1);
283 int pci_module_init(void)
294 struct coreinfo_module pci_module = {
296 .init = pci_module_init,
297 .redraw = pci_module_redraw,
298 .handle = pci_module_handle,