blob: 3407cbe86d3c41eb2b6bc687a7053520c1dd1e85 [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>
Stefan Reinauer28dff392008-08-13 08:23:06 +000021#include <pci.h>
Uwe Hermann754edf72008-08-04 21:02:07 +000022#include <libpayload.h>
Jordan Crouse7249f792008-03-20 00:11:05 +000023#include "coreinfo.h"
24
Uwe Hermannab5b3e02008-03-31 20:30:18 +000025#ifdef CONFIG_MODULE_PCI
26
Jordan Crouse7249f792008-03-20 00:11:05 +000027struct pci_devices {
Stefan Reinauer28dff392008-08-13 08:23:06 +000028 pcidev_t device;
Jordan Crouse7249f792008-03-20 00:11:05 +000029 unsigned int id;
30};
31
32static struct pci_devices devices[64];
33static int devices_index;
34
Jordan Crouse7249f792008-03-20 00:11:05 +000035/* Number of entries to show in the list */
36#define MENU_VISIBLE 16
37
38static int menu_selected = 0;
39static int menu_first = 0;
40
41static void swap(struct pci_devices *a, struct pci_devices *b)
42{
43 struct pci_devices tmp;
44
45 tmp.device = a->device;
46 tmp.id = a->id;
47
48 a->device = b->device;
49 a->id = b->id;
50
51 b->device = tmp.device;
52 b->id = tmp.id;
53}
54
55static int partition(struct pci_devices *list, int len)
56{
57 int val = list[len / 2].device;
58 int index = 0;
59 int i;
60
61 swap(&list[len / 2], &list[len - 1]);
62
Uwe Hermann3a406fe2008-03-20 01:11:28 +000063 for (i = 0; i < len - 1; i++) {
Jordan Crouse7249f792008-03-20 00:11:05 +000064 if (list[i].device < val) {
65 swap(&list[i], &list[index]);
66 index++;
67 }
68 }
69
70 swap(&list[index], &list[len - 1]);
Uwe Hermann3a406fe2008-03-20 01:11:28 +000071
Jordan Crouse7249f792008-03-20 00:11:05 +000072 return index;
73}
74
75static void quicksort(struct pci_devices *list, int len)
76{
77 int index;
78
79 if (len <= 1)
80 return;
81
82 index = partition(list, len);
83
84 quicksort(list, index);
85 quicksort(&(list[index]), len - index);
86}
87
Uwe Hermann35845a22008-03-20 20:05:22 +000088static void show_config_space(WINDOW *win, int row, int col, int index)
Jordan Crouse7249f792008-03-20 00:11:05 +000089{
Stefan Reinauer28dff392008-08-13 08:23:06 +000090 unsigned char cspace[256];
91 pcidev_t dev;
Jordan Crouse7249f792008-03-20 00:11:05 +000092 int i, x, y;
93
Stefan Reinauer28dff392008-08-13 08:23:06 +000094 dev = devices[index].device;
Jordan Crouse7249f792008-03-20 00:11:05 +000095
Stefan Reinauer28dff392008-08-13 08:23:06 +000096 for (i = 0; i < 256; i ++)
97 cspace[i] = pci_read_config8(dev, i);
Jordan Crouse7249f792008-03-20 00:11:05 +000098
Stefan Reinauer28dff392008-08-13 08:23:06 +000099 for (y = 0; y < 16; y++) {
Uwe Hermann3a406fe2008-03-20 01:11:28 +0000100 for (x = 0; x < 16; x++)
101 mvwprintw(win, row + y, col + (x * 3), "%2.2X ",
102 cspace[(y * 16) + x]);
Jordan Crouse7249f792008-03-20 00:11:05 +0000103 }
104}
105
Uwe Hermann0bfb5c42008-03-23 15:34:04 +0000106static int pci_module_redraw(WINDOW *win)
Jordan Crouse7249f792008-03-20 00:11:05 +0000107{
Stefan Reinauer28dff392008-08-13 08:23:06 +0000108 unsigned int bus, slot, func;
Uwe Hermann3a406fe2008-03-20 01:11:28 +0000109 int i, last;
Jordan Crouse7249f792008-03-20 00:11:05 +0000110
111 print_module_title(win, "PCI Device List");
112
113 last = menu_first + MENU_VISIBLE;
114
115 if (last > devices_index)
Uwe Hermann3a406fe2008-03-20 01:11:28 +0000116 last = devices_index;
Jordan Crouse7249f792008-03-20 00:11:05 +0000117
Uwe Hermann3a406fe2008-03-20 01:11:28 +0000118 for (i = 0; i < MENU_VISIBLE; i++) {
Jordan Crouse7249f792008-03-20 00:11:05 +0000119 int item = menu_first + i;
120
Uwe Hermann3a406fe2008-03-20 01:11:28 +0000121 /* Draw a blank space. */
Jordan Crouse7249f792008-03-20 00:11:05 +0000122 if (item >= devices_index) {
123 wattrset(win, COLOR_PAIR(2));
124 mvwprintw(win, 2 + i, 1, " ");
125 continue;
126 }
127
Stefan Reinauer28dff392008-08-13 08:23:06 +0000128 bus = PCI_BUS(devices[item].device);
129 slot = PCI_SLOT(devices[item].device);
130 func = PCI_FUNC(devices[item].device);
Jordan Crouse7249f792008-03-20 00:11:05 +0000131
132 if (item == menu_selected)
133 wattrset(win, COLOR_PAIR(3) | A_BOLD);
134 else
135 wattrset(win, COLOR_PAIR(2));
136
Uwe Hermanna0c00932008-03-27 20:46:49 +0000137 mvwprintw(win, 2 + i, 1, "%X:%2.2X.%2.2X %04X:%04X ",
Stefan Reinauer28dff392008-08-13 08:23:06 +0000138 bus, slot, func,
Uwe Hermann3a406fe2008-03-20 01:11:28 +0000139 devices[item].id & 0xffff,
140 (devices[item].id >> 16) & 0xffff);
Jordan Crouse7249f792008-03-20 00:11:05 +0000141
142 wattrset(win, COLOR_PAIR(2));
143
144 if (i == 0) {
145 if (item != 0)
Ulf Jordand133d9a2008-08-11 20:35:32 +0000146 mvwaddch(win, 2 + i, 19, ACS_UARROW);
Jordan Crouse7249f792008-03-20 00:11:05 +0000147 }
148 if (i == MENU_VISIBLE - 1) {
149 if ((item + 1) < devices_index)
Ulf Jordand133d9a2008-08-11 20:35:32 +0000150 mvwaddch(win, 2 + i, 19, ACS_DARROW);
Jordan Crouse7249f792008-03-20 00:11:05 +0000151 }
152 }
153
154 wattrset(win, COLOR_PAIR(2));
155
Uwe Hermann3a406fe2008-03-20 01:11:28 +0000156 for (i = 0; i < 16; i++)
Jordan Crouse7249f792008-03-20 00:11:05 +0000157 mvwprintw(win, 2, 26 + (i * 3), "%2.2X ", i);
158
159 wmove(win, 3, 25);
160
Uwe Hermann3a406fe2008-03-20 01:11:28 +0000161 for (i = 0; i < 48; i++)
Ulf Jordand133d9a2008-08-11 20:35:32 +0000162 waddch(win, (i == 0) ? ACS_ULCORNER : ACS_HLINE);
Jordan Crouse7249f792008-03-20 00:11:05 +0000163
Stefan Reinauer28dff392008-08-13 08:23:06 +0000164 for (i = 0; i < 16; i++) {
Jordan Crouse7249f792008-03-20 00:11:05 +0000165 mvwprintw(win, 4 + i, 23, "%2.2X", i * 16);
166 wmove(win, 4 + i, 25);
Ulf Jordand133d9a2008-08-11 20:35:32 +0000167 waddch(win, ACS_VLINE);
Jordan Crouse7249f792008-03-20 00:11:05 +0000168 }
169
170 show_config_space(win, 4, 26, menu_selected);
171
172 return 0;
173}
174
175static void pci_scan_bus(int bus)
176{
Stefan Reinauer28dff392008-08-13 08:23:06 +0000177 int slot, func;
Jordan Crouse7249f792008-03-20 00:11:05 +0000178 unsigned int val;
179 unsigned char hdr;
180
Stefan Reinauerbc5379a2008-08-13 09:38:12 +0000181 for (slot = 0; slot < 0x20; slot++) {
Stefan Reinauer28dff392008-08-13 08:23:06 +0000182 for (func = 0; func < 8; func++) {
183 pcidev_t dev = PCI_DEV(bus, slot, func);
Stefan Reinauer88ad6b02008-08-07 19:09:17 +0000184
185 val = pci_read_config32(dev, REG_VENDOR_ID);
Jordan Crouse7249f792008-03-20 00:11:05 +0000186
Uwe Hermann3a406fe2008-03-20 01:11:28 +0000187 /* Nobody home. */
Jordan Crouse7249f792008-03-20 00:11:05 +0000188 if (val == 0xffffffff || val == 0x00000000 ||
189 val == 0x0000ffff || val == 0xffff0000)
190 continue;
191
Uwe Hermann3a406fe2008-03-20 01:11:28 +0000192 /* FIXME: Remove this arbitrary limitation. */
Jordan Crouse7249f792008-03-20 00:11:05 +0000193 if (devices_index >= 64)
194 return;
195
Stefan Reinauer28dff392008-08-13 08:23:06 +0000196 devices[devices_index].device =
197 PCI_DEV(bus, slot, func);
Jordan Crouse7249f792008-03-20 00:11:05 +0000198
199 devices[devices_index++].id = val;
200
Uwe Hermann3a406fe2008-03-20 01:11:28 +0000201 /* If this is a bridge, then follow it. */
Stefan Reinauer88ad6b02008-08-07 19:09:17 +0000202 hdr = pci_read_config8(dev, REG_HEADER_TYPE);
Uwe Hermann3a406fe2008-03-20 01:11:28 +0000203 hdr &= 0x7f;
Jordan Crouse7249f792008-03-20 00:11:05 +0000204 if (hdr == HEADER_TYPE_BRIDGE ||
205 hdr == HEADER_TYPE_CARDBUS) {
206 unsigned int busses;
207
Stefan Reinauer88ad6b02008-08-07 19:09:17 +0000208 busses = pci_read_config32(dev, REG_PRIMARY_BUS);
Jordan Crouse7249f792008-03-20 00:11:05 +0000209
Uwe Hermann3a406fe2008-03-20 01:11:28 +0000210 pci_scan_bus((busses >> 8) & 0xff);
Jordan Crouse7249f792008-03-20 00:11:05 +0000211
212 }
213 }
214 }
215
216 quicksort(devices, devices_index);
217}
218
Uwe Hermann0bfb5c42008-03-23 15:34:04 +0000219static int pci_module_handle(int key)
Jordan Crouse7249f792008-03-20 00:11:05 +0000220{
221 int ret = 0;
222
Uwe Hermann3a406fe2008-03-20 01:11:28 +0000223 switch (key) {
Jordan Crouse7249f792008-03-20 00:11:05 +0000224 case KEY_DOWN:
225 if (menu_selected + 1 < devices_index) {
226 menu_selected++;
227 ret = 1;
228 }
Jordan Crouse7249f792008-03-20 00:11:05 +0000229 break;
Jordan Crouse7249f792008-03-20 00:11:05 +0000230 case KEY_UP:
231 if (menu_selected > 0) {
232 menu_selected--;
233 ret = 1;
234 }
Jordan Crouse7249f792008-03-20 00:11:05 +0000235 break;
236 }
237
238 if (!ret)
239 return ret;
240
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);
245 if (menu_first < 0)
246 menu_first = 0;
247 }
248
Jordan Crouse7249f792008-03-20 00:11:05 +0000249 return ret;
250}
251
Uwe Hermann0bfb5c42008-03-23 15:34:04 +0000252static int pci_module_init(void)
Jordan Crouse7249f792008-03-20 00:11:05 +0000253{
Jordan Crouse7249f792008-03-20 00:11:05 +0000254 pci_scan_bus(0);
Jordan Crouse7249f792008-03-20 00:11:05 +0000255 return 0;
256}
257
258struct coreinfo_module pci_module = {
259 .name = "PCI",
260 .init = pci_module_init,
261 .redraw = pci_module_redraw,
262 .handle = pci_module_handle,
263};
Uwe Hermannab5b3e02008-03-31 20:30:18 +0000264
265#else
266
267struct coreinfo_module pci_module = {
268};
269
270#endif