blob: d2c0591389bbf3deb8cb4c4078ca0cfa536e87c6 [file] [log] [blame]
Jordan Crouse7249f792008-03-20 00:11:05 +00001/*
2 * This file is part of the coreinfo project.
3 *
4 * Copyright (C) 2008 Advanced Micro Devices, Inc.
5 *
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.
Uwe Hermann3a406fe2008-03-20 01:11:28 +00009 *
Jordan Crouse7249f792008-03-20 00:11:05 +000010 * 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.
Uwe Hermann3a406fe2008-03-20 01:11:28 +000014 *
Jordan Crouse7249f792008-03-20 00:11:05 +000015 * 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
18 */
19
20#include <arch/io.h>
Uwe Hermann754edf72008-08-04 21:02:07 +000021#include <libpayload.h>
Jordan Crouse7249f792008-03-20 00:11:05 +000022#include "coreinfo.h"
23
Uwe Hermannab5b3e02008-03-31 20:30:18 +000024#ifdef CONFIG_MODULE_PCI
25
Jordan Crouse7249f792008-03-20 00:11:05 +000026struct pci_devices {
27 unsigned short device;
28 unsigned int id;
29};
30
31static struct pci_devices devices[64];
32static int devices_index;
33
Jordan Crouse7249f792008-03-20 00:11:05 +000034/* Number of entries to show in the list */
35#define MENU_VISIBLE 16
36
37static int menu_selected = 0;
38static int menu_first = 0;
39
40static void swap(struct pci_devices *a, struct pci_devices *b)
41{
42 struct pci_devices tmp;
43
44 tmp.device = a->device;
45 tmp.id = a->id;
46
47 a->device = b->device;
48 a->id = b->id;
49
50 b->device = tmp.device;
51 b->id = tmp.id;
52}
53
54static int partition(struct pci_devices *list, int len)
55{
56 int val = list[len / 2].device;
57 int index = 0;
58 int i;
59
60 swap(&list[len / 2], &list[len - 1]);
61
Uwe Hermann3a406fe2008-03-20 01:11:28 +000062 for (i = 0; i < len - 1; i++) {
Jordan Crouse7249f792008-03-20 00:11:05 +000063 if (list[i].device < val) {
64 swap(&list[i], &list[index]);
65 index++;
66 }
67 }
68
69 swap(&list[index], &list[len - 1]);
Uwe Hermann3a406fe2008-03-20 01:11:28 +000070
Jordan Crouse7249f792008-03-20 00:11:05 +000071 return index;
72}
73
74static void quicksort(struct pci_devices *list, int len)
75{
76 int index;
77
78 if (len <= 1)
79 return;
80
81 index = partition(list, len);
82
83 quicksort(list, index);
84 quicksort(&(list[index]), len - index);
85}
86
Uwe Hermann35845a22008-03-20 20:05:22 +000087static void show_config_space(WINDOW *win, int row, int col, int index)
Jordan Crouse7249f792008-03-20 00:11:05 +000088{
89 unsigned char cspace[64];
90 int bus, devfn;
91 int i, x, y;
92
Uwe Hermann3a406fe2008-03-20 01:11:28 +000093 bus = (devices[index].device >> 8) & 0xff;
94 devfn = devices[index].device & 0xff;
Jordan Crouse7249f792008-03-20 00:11:05 +000095
Uwe Hermann3a406fe2008-03-20 01:11:28 +000096 for (i = 0; i < 64; i += 4)
Stefan Reinauer88ad6b02008-08-07 19:09:17 +000097 cspace[i] = pci_read_config32(PCI_DEV(bus, PCI_SLOT(devfn),
98 PCI_FUNC(devfn)), i);
Jordan Crouse7249f792008-03-20 00:11:05 +000099
Uwe Hermann3a406fe2008-03-20 01:11:28 +0000100 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]);
Jordan Crouse7249f792008-03-20 00:11:05 +0000104 }
105}
106
Uwe Hermann0bfb5c42008-03-23 15:34:04 +0000107static int pci_module_redraw(WINDOW *win)
Jordan Crouse7249f792008-03-20 00:11:05 +0000108{
109 unsigned int bus, devfn, func;
Uwe Hermann3a406fe2008-03-20 01:11:28 +0000110 int i, last;
Jordan Crouse7249f792008-03-20 00:11:05 +0000111
112 print_module_title(win, "PCI Device List");
113
114 last = menu_first + MENU_VISIBLE;
115
116 if (last > devices_index)
Uwe Hermann3a406fe2008-03-20 01:11:28 +0000117 last = devices_index;
Jordan Crouse7249f792008-03-20 00:11:05 +0000118
Uwe Hermann3a406fe2008-03-20 01:11:28 +0000119 for (i = 0; i < MENU_VISIBLE; i++) {
Jordan Crouse7249f792008-03-20 00:11:05 +0000120 int item = menu_first + i;
121
Uwe Hermann3a406fe2008-03-20 01:11:28 +0000122 /* Draw a blank space. */
Jordan Crouse7249f792008-03-20 00:11:05 +0000123 if (item >= devices_index) {
124 wattrset(win, COLOR_PAIR(2));
125 mvwprintw(win, 2 + i, 1, " ");
126 continue;
127 }
128
Uwe Hermann3a406fe2008-03-20 01:11:28 +0000129 bus = (devices[item].device >> 8) & 0xff;
130 devfn = (devices[item].device & 0xff) / 8;
131 func = (devices[item].device & 0xff) % 8;
Jordan Crouse7249f792008-03-20 00:11:05 +0000132
133 if (item == menu_selected)
134 wattrset(win, COLOR_PAIR(3) | A_BOLD);
135 else
136 wattrset(win, COLOR_PAIR(2));
137
Uwe Hermanna0c00932008-03-27 20:46:49 +0000138 mvwprintw(win, 2 + i, 1, "%X:%2.2X.%2.2X %04X:%04X ",
Jordan Crouse7249f792008-03-20 00:11:05 +0000139 bus, devfn, func,
Uwe Hermann3a406fe2008-03-20 01:11:28 +0000140 devices[item].id & 0xffff,
141 (devices[item].id >> 16) & 0xffff);
Jordan Crouse7249f792008-03-20 00:11:05 +0000142
143 wattrset(win, COLOR_PAIR(2));
144
145 if (i == 0) {
146 if (item != 0)
Ulf Jordand133d9a2008-08-11 20:35:32 +0000147 mvwaddch(win, 2 + i, 19, ACS_UARROW);
Jordan Crouse7249f792008-03-20 00:11:05 +0000148 }
149 if (i == MENU_VISIBLE - 1) {
150 if ((item + 1) < devices_index)
Ulf Jordand133d9a2008-08-11 20:35:32 +0000151 mvwaddch(win, 2 + i, 19, ACS_DARROW);
Jordan Crouse7249f792008-03-20 00:11:05 +0000152 }
153 }
154
155 wattrset(win, COLOR_PAIR(2));
156
Uwe Hermann3a406fe2008-03-20 01:11:28 +0000157 for (i = 0; i < 16; i++)
Jordan Crouse7249f792008-03-20 00:11:05 +0000158 mvwprintw(win, 2, 26 + (i * 3), "%2.2X ", i);
159
160 wmove(win, 3, 25);
161
Uwe Hermann3a406fe2008-03-20 01:11:28 +0000162 for (i = 0; i < 48; i++)
Ulf Jordand133d9a2008-08-11 20:35:32 +0000163 waddch(win, (i == 0) ? ACS_ULCORNER : ACS_HLINE);
Jordan Crouse7249f792008-03-20 00:11:05 +0000164
Uwe Hermann3a406fe2008-03-20 01:11:28 +0000165 for (i = 0; i < 4; i++) {
Jordan Crouse7249f792008-03-20 00:11:05 +0000166 mvwprintw(win, 4 + i, 23, "%2.2X", i * 16);
167 wmove(win, 4 + i, 25);
Ulf Jordand133d9a2008-08-11 20:35:32 +0000168 waddch(win, ACS_VLINE);
Jordan Crouse7249f792008-03-20 00:11:05 +0000169 }
170
171 show_config_space(win, 4, 26, menu_selected);
172
173 return 0;
174}
175
176static void pci_scan_bus(int bus)
177{
178 int devfn, func;
179 unsigned int val;
180 unsigned char hdr;
181
Uwe Hermann3a406fe2008-03-20 01:11:28 +0000182 for (devfn = 0; devfn < 0x100;) {
183 for (func = 0; func < 8; func++, devfn++) {
Stefan Reinauer88ad6b02008-08-07 19:09:17 +0000184 pcidev_t dev = PCI_DEV(bus, PCI_SLOT(devfn),
185 PCI_FUNC(devfn));
186
187 val = pci_read_config32(dev, REG_VENDOR_ID);
Jordan Crouse7249f792008-03-20 00:11:05 +0000188
Uwe Hermann3a406fe2008-03-20 01:11:28 +0000189 /* Nobody home. */
Jordan Crouse7249f792008-03-20 00:11:05 +0000190 if (val == 0xffffffff || val == 0x00000000 ||
191 val == 0x0000ffff || val == 0xffff0000)
192 continue;
193
Uwe Hermann3a406fe2008-03-20 01:11:28 +0000194 /* FIXME: Remove this arbitrary limitation. */
Jordan Crouse7249f792008-03-20 00:11:05 +0000195 if (devices_index >= 64)
196 return;
197
Uwe Hermann3a406fe2008-03-20 01:11:28 +0000198 devices[devices_index].device =
199 ((bus & 0xff) << 8) | (devfn & 0xff);
Jordan Crouse7249f792008-03-20 00:11:05 +0000200
201 devices[devices_index++].id = val;
202
Uwe Hermann3a406fe2008-03-20 01:11:28 +0000203 /* If this is a bridge, then follow it. */
Stefan Reinauer88ad6b02008-08-07 19:09:17 +0000204 hdr = pci_read_config8(dev, REG_HEADER_TYPE);
Uwe Hermann3a406fe2008-03-20 01:11:28 +0000205 hdr &= 0x7f;
Jordan Crouse7249f792008-03-20 00:11:05 +0000206 if (hdr == HEADER_TYPE_BRIDGE ||
207 hdr == HEADER_TYPE_CARDBUS) {
208 unsigned int busses;
209
Stefan Reinauer88ad6b02008-08-07 19:09:17 +0000210 busses = pci_read_config32(dev, REG_PRIMARY_BUS);
Jordan Crouse7249f792008-03-20 00:11:05 +0000211
Uwe Hermann3a406fe2008-03-20 01:11:28 +0000212 pci_scan_bus((busses >> 8) & 0xff);
Jordan Crouse7249f792008-03-20 00:11:05 +0000213
214 }
215 }
216 }
217
218 quicksort(devices, devices_index);
219}
220
Uwe Hermann0bfb5c42008-03-23 15:34:04 +0000221static int pci_module_handle(int key)
Jordan Crouse7249f792008-03-20 00:11:05 +0000222{
223 int ret = 0;
224
Uwe Hermann3a406fe2008-03-20 01:11:28 +0000225 switch (key) {
Jordan Crouse7249f792008-03-20 00:11:05 +0000226 case KEY_DOWN:
227 if (menu_selected + 1 < devices_index) {
228 menu_selected++;
229 ret = 1;
230 }
Jordan Crouse7249f792008-03-20 00:11:05 +0000231 break;
Jordan Crouse7249f792008-03-20 00:11:05 +0000232 case KEY_UP:
233 if (menu_selected > 0) {
234 menu_selected--;
235 ret = 1;
236 }
Jordan Crouse7249f792008-03-20 00:11:05 +0000237 break;
238 }
239
240 if (!ret)
241 return ret;
242
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);
247 if (menu_first < 0)
248 menu_first = 0;
249 }
250
Jordan Crouse7249f792008-03-20 00:11:05 +0000251 return ret;
252}
253
Uwe Hermann0bfb5c42008-03-23 15:34:04 +0000254static int pci_module_init(void)
Jordan Crouse7249f792008-03-20 00:11:05 +0000255{
Jordan Crouse7249f792008-03-20 00:11:05 +0000256 pci_scan_bus(0);
Jordan Crouse7249f792008-03-20 00:11:05 +0000257 return 0;
258}
259
260struct coreinfo_module pci_module = {
261 .name = "PCI",
262 .init = pci_module_init,
263 .redraw = pci_module_redraw,
264 .handle = pci_module_handle,
265};
Uwe Hermannab5b3e02008-03-31 20:30:18 +0000266
267#else
268
269struct coreinfo_module pci_module = {
270};
271
272#endif