2008-03-20 01:11:05 +01:00
|
|
|
/*
|
|
|
|
* This file is part of the coreinfo project.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2008 Advanced Micro Devices, Inc.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; version 2 of the License.
|
2008-03-20 02:11:28 +01:00
|
|
|
*
|
2008-03-20 01:11:05 +01:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2008-03-20 02:11:28 +01:00
|
|
|
*
|
2008-03-20 01:11:05 +01:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <arch/io.h>
|
|
|
|
#include "coreinfo.h"
|
|
|
|
|
|
|
|
struct pci_devices {
|
|
|
|
unsigned short device;
|
|
|
|
unsigned int id;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct pci_devices devices[64];
|
|
|
|
static int devices_index;
|
|
|
|
|
|
|
|
#define REG_VENDOR_ID 0x00
|
2008-03-20 02:11:28 +01:00
|
|
|
#define REG_HEADER_TYPE 0x0e
|
2008-03-20 01:11:05 +01:00
|
|
|
#define REG_PRIMARY_BUS 0x18
|
|
|
|
|
|
|
|
#define HEADER_TYPE_NORMAL 0
|
|
|
|
#define HEADER_TYPE_BRIDGE 1
|
|
|
|
#define HEADER_TYPE_CARDBUS 2
|
|
|
|
|
|
|
|
#define PCI_ADDR(_bus, _dev, _reg) \
|
2008-03-20 02:11:28 +01:00
|
|
|
(0x80000000 | (_bus << 16) | (_dev << 8) | (_reg & ~3))
|
2008-03-20 01:11:05 +01:00
|
|
|
|
|
|
|
/* Number of entries to show in the list */
|
|
|
|
#define MENU_VISIBLE 16
|
|
|
|
|
|
|
|
static int menu_selected = 0;
|
|
|
|
static int menu_first = 0;
|
|
|
|
|
|
|
|
static void swap(struct pci_devices *a, struct pci_devices *b)
|
|
|
|
{
|
|
|
|
struct pci_devices tmp;
|
|
|
|
|
|
|
|
tmp.device = a->device;
|
|
|
|
tmp.id = a->id;
|
|
|
|
|
|
|
|
a->device = b->device;
|
|
|
|
a->id = b->id;
|
|
|
|
|
|
|
|
b->device = tmp.device;
|
|
|
|
b->id = tmp.id;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int partition(struct pci_devices *list, int len)
|
|
|
|
{
|
|
|
|
int val = list[len / 2].device;
|
|
|
|
int index = 0;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
swap(&list[len / 2], &list[len - 1]);
|
|
|
|
|
2008-03-20 02:11:28 +01:00
|
|
|
for (i = 0; i < len - 1; i++) {
|
2008-03-20 01:11:05 +01:00
|
|
|
if (list[i].device < val) {
|
|
|
|
swap(&list[i], &list[index]);
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
swap(&list[index], &list[len - 1]);
|
2008-03-20 02:11:28 +01:00
|
|
|
|
2008-03-20 01:11:05 +01:00
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void quicksort(struct pci_devices *list, int len)
|
|
|
|
{
|
|
|
|
int index;
|
|
|
|
|
|
|
|
if (len <= 1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
index = partition(list, len);
|
|
|
|
|
|
|
|
quicksort(list, index);
|
|
|
|
quicksort(&(list[index]), len - index);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void pci_read_dword(unsigned int bus, unsigned int devfn,
|
|
|
|
unsigned int reg, unsigned int *val)
|
|
|
|
{
|
2008-03-20 02:11:28 +01:00
|
|
|
outl(PCI_ADDR(bus, devfn, reg), 0xcf8);
|
|
|
|
*val = inl(0xcfc);
|
2008-03-20 01:11:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void pci_read_byte(unsigned int bus, unsigned int devfn,
|
|
|
|
unsigned int reg, unsigned char *val)
|
|
|
|
{
|
2008-03-20 02:11:28 +01:00
|
|
|
outl(PCI_ADDR(bus, devfn, reg), 0xcf8);
|
|
|
|
*val = inb(0xcfc + (reg & 3));
|
2008-03-20 01:11:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static int show_config_space(WINDOW *win, int row, int col, int index)
|
|
|
|
{
|
|
|
|
unsigned char cspace[64];
|
|
|
|
int bus, devfn;
|
|
|
|
int i, x, y;
|
|
|
|
|
2008-03-20 02:11:28 +01:00
|
|
|
bus = (devices[index].device >> 8) & 0xff;
|
|
|
|
devfn = devices[index].device & 0xff;
|
2008-03-20 01:11:05 +01:00
|
|
|
|
2008-03-20 02:11:28 +01:00
|
|
|
for (i = 0; i < 64; i += 4)
|
|
|
|
pci_read_dword(bus, devfn, i, ((int *)&cspace[i]));
|
2008-03-20 01:11:05 +01:00
|
|
|
|
2008-03-20 02:11:28 +01:00
|
|
|
for (y = 0; y < 4; y++) {
|
|
|
|
for (x = 0; x < 16; x++)
|
|
|
|
mvwprintw(win, row + y, col + (x * 3), "%2.2X ",
|
|
|
|
cspace[(y * 16) + x]);
|
2008-03-20 01:11:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int pci_module_redraw(WINDOW *win)
|
|
|
|
{
|
|
|
|
unsigned int bus, devfn, func;
|
2008-03-20 02:11:28 +01:00
|
|
|
int i, last;
|
2008-03-20 01:11:05 +01:00
|
|
|
|
|
|
|
print_module_title(win, "PCI Device List");
|
|
|
|
|
|
|
|
last = menu_first + MENU_VISIBLE;
|
|
|
|
|
|
|
|
if (last > devices_index)
|
2008-03-20 02:11:28 +01:00
|
|
|
last = devices_index;
|
2008-03-20 01:11:05 +01:00
|
|
|
|
2008-03-20 02:11:28 +01:00
|
|
|
for (i = 0; i < MENU_VISIBLE; i++) {
|
2008-03-20 01:11:05 +01:00
|
|
|
int item = menu_first + i;
|
|
|
|
|
2008-03-20 02:11:28 +01:00
|
|
|
/* Draw a blank space. */
|
2008-03-20 01:11:05 +01:00
|
|
|
if (item >= devices_index) {
|
|
|
|
wattrset(win, COLOR_PAIR(2));
|
|
|
|
mvwprintw(win, 2 + i, 1, " ");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2008-03-20 02:11:28 +01:00
|
|
|
bus = (devices[item].device >> 8) & 0xff;
|
|
|
|
devfn = (devices[item].device & 0xff) / 8;
|
|
|
|
func = (devices[item].device & 0xff) % 8;
|
2008-03-20 01:11:05 +01:00
|
|
|
|
|
|
|
if (item == menu_selected)
|
|
|
|
wattrset(win, COLOR_PAIR(3) | A_BOLD);
|
|
|
|
else
|
|
|
|
wattrset(win, COLOR_PAIR(2));
|
|
|
|
|
2008-03-20 02:11:28 +01:00
|
|
|
mvwprintw(win, 2 + i, 1, "%X:%2.2X.%2.2X %X:%X ",
|
2008-03-20 01:11:05 +01:00
|
|
|
bus, devfn, func,
|
2008-03-20 02:11:28 +01:00
|
|
|
devices[item].id & 0xffff,
|
|
|
|
(devices[item].id >> 16) & 0xffff);
|
2008-03-20 01:11:05 +01:00
|
|
|
|
|
|
|
wattrset(win, COLOR_PAIR(2));
|
|
|
|
|
|
|
|
if (i == 0) {
|
|
|
|
if (item != 0)
|
2008-03-20 02:11:28 +01:00
|
|
|
mvwprintw(win, 2 + i, 19, "\30");
|
2008-03-20 01:11:05 +01:00
|
|
|
}
|
|
|
|
if (i == MENU_VISIBLE - 1) {
|
|
|
|
if ((item + 1) < devices_index)
|
2008-03-20 02:11:28 +01:00
|
|
|
mvwprintw(win, 2 + i, 19, "\31");
|
2008-03-20 01:11:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
wattrset(win, COLOR_PAIR(2));
|
|
|
|
|
2008-03-20 02:11:28 +01:00
|
|
|
for (i = 0; i < 16; i++)
|
2008-03-20 01:11:05 +01:00
|
|
|
mvwprintw(win, 2, 26 + (i * 3), "%2.2X ", i);
|
|
|
|
|
|
|
|
wmove(win, 3, 25);
|
|
|
|
|
2008-03-20 02:11:28 +01:00
|
|
|
for (i = 0; i < 48; i++)
|
2008-03-20 01:11:05 +01:00
|
|
|
waddch(win, (i == 0) ? '\332' : '\304');
|
|
|
|
|
2008-03-20 02:11:28 +01:00
|
|
|
for (i = 0; i < 4; i++) {
|
2008-03-20 01:11:05 +01:00
|
|
|
mvwprintw(win, 4 + i, 23, "%2.2X", i * 16);
|
|
|
|
wmove(win, 4 + i, 25);
|
|
|
|
waddch(win, '\263');
|
|
|
|
}
|
|
|
|
|
|
|
|
show_config_space(win, 4, 26, menu_selected);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void pci_scan_bus(int bus)
|
|
|
|
{
|
|
|
|
int devfn, func;
|
|
|
|
unsigned int val;
|
|
|
|
unsigned char hdr;
|
|
|
|
|
2008-03-20 02:11:28 +01:00
|
|
|
for (devfn = 0; devfn < 0x100;) {
|
|
|
|
for (func = 0; func < 8; func++, devfn++) {
|
2008-03-20 01:11:05 +01:00
|
|
|
pci_read_dword(bus, devfn, REG_VENDOR_ID, &val);
|
|
|
|
|
2008-03-20 02:11:28 +01:00
|
|
|
/* Nobody home. */
|
2008-03-20 01:11:05 +01:00
|
|
|
if (val == 0xffffffff || val == 0x00000000 ||
|
|
|
|
val == 0x0000ffff || val == 0xffff0000)
|
|
|
|
continue;
|
|
|
|
|
2008-03-20 02:11:28 +01:00
|
|
|
/* FIXME: Remove this arbitrary limitation. */
|
2008-03-20 01:11:05 +01:00
|
|
|
if (devices_index >= 64)
|
|
|
|
return;
|
|
|
|
|
2008-03-20 02:11:28 +01:00
|
|
|
devices[devices_index].device =
|
|
|
|
((bus & 0xff) << 8) | (devfn & 0xff);
|
2008-03-20 01:11:05 +01:00
|
|
|
|
|
|
|
devices[devices_index++].id = val;
|
|
|
|
|
2008-03-20 02:11:28 +01:00
|
|
|
/* If this is a bridge, then follow it. */
|
2008-03-20 01:11:05 +01:00
|
|
|
pci_read_byte(bus, devfn, REG_HEADER_TYPE, &hdr);
|
2008-03-20 02:11:28 +01:00
|
|
|
hdr &= 0x7f;
|
2008-03-20 01:11:05 +01:00
|
|
|
if (hdr == HEADER_TYPE_BRIDGE ||
|
|
|
|
hdr == HEADER_TYPE_CARDBUS) {
|
|
|
|
unsigned int busses;
|
|
|
|
|
|
|
|
pci_read_dword(bus, devfn, REG_PRIMARY_BUS,
|
|
|
|
&busses);
|
|
|
|
|
2008-03-20 02:11:28 +01:00
|
|
|
pci_scan_bus((busses >> 8) & 0xff);
|
2008-03-20 01:11:05 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
quicksort(devices, devices_index);
|
|
|
|
}
|
|
|
|
|
|
|
|
int pci_module_handle(int key)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
|
2008-03-20 02:11:28 +01:00
|
|
|
switch (key) {
|
2008-03-20 01:11:05 +01:00
|
|
|
case KEY_DOWN:
|
|
|
|
if (menu_selected + 1 < devices_index) {
|
|
|
|
menu_selected++;
|
|
|
|
ret = 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case KEY_UP:
|
|
|
|
if (menu_selected > 0) {
|
|
|
|
menu_selected--;
|
|
|
|
ret = 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (menu_selected < menu_first)
|
|
|
|
menu_first = menu_selected;
|
|
|
|
else if (menu_selected >= menu_first + MENU_VISIBLE) {
|
|
|
|
menu_first = menu_selected - (MENU_VISIBLE - 1);
|
|
|
|
if (menu_first < 0)
|
|
|
|
menu_first = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int pci_module_init(void)
|
|
|
|
{
|
|
|
|
unsigned int val;
|
|
|
|
int bus = 0;
|
|
|
|
|
|
|
|
pci_scan_bus(0);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct coreinfo_module pci_module = {
|
|
|
|
.name = "PCI",
|
|
|
|
.init = pci_module_init,
|
|
|
|
.redraw = pci_module_redraw,
|
|
|
|
.handle = pci_module_handle,
|
|
|
|
};
|