soc/intel/fsp_broadwell_de: Move FSP_DEBUG_LEVEL option here
[coreboot.git] / payloads / coreinfo / pci_module.c
blob9f10b12703b5be861df259e1739424bb4f7e3cae
1 /*
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.
16 #include <arch/io.h>
17 #include <pci.h>
18 #include <libpayload.h>
19 #include "coreinfo.h"
21 #if IS_ENABLED(CONFIG_MODULE_PCI)
23 struct pci_devices {
24 pcidev_t device;
25 unsigned int id;
28 #define MAX_PCI_DEVICES 64
29 static struct pci_devices devices[MAX_PCI_DEVICES];
30 static int devices_index;
32 /* Number of entries to show in the list */
33 #define MENU_VISIBLE 16
35 static int menu_selected = 0;
36 static int menu_first = 0;
38 static void swap(struct pci_devices *a, struct pci_devices *b)
40 struct pci_devices tmp;
42 tmp.device = a->device;
43 tmp.id = a->id;
45 a->device = b->device;
46 a->id = b->id;
48 b->device = tmp.device;
49 b->id = tmp.id;
52 static int partition(struct pci_devices *list, int len)
54 int val = list[len / 2].device;
55 int index = 0;
56 int i;
58 swap(&list[len / 2], &list[len - 1]);
60 for (i = 0; i < len - 1; i++) {
61 if (list[i].device < val) {
62 swap(&list[i], &list[index]);
63 index++;
67 swap(&list[index], &list[len - 1]);
69 return index;
72 static void quicksort(struct pci_devices *list, int len)
74 int index;
76 if (len <= 1)
77 return;
79 index = partition(list, len);
81 quicksort(list, index);
82 quicksort(&(list[index]), len - index);
85 static void show_config_space(WINDOW *win, int row, int col, int index)
87 unsigned char cspace[256];
88 pcidev_t dev;
89 int i, x, y;
91 dev = devices[index].device;
93 for (i = 0; i < 256; i ++)
94 cspace[i] = pci_read_config8(dev, i);
96 for (y = 0; y < 16; y++) {
97 for (x = 0; x < 16; x++)
98 mvwprintw(win, row + y, col + (x * 3), "%2.2X ",
99 cspace[(y * 16) + x]);
103 static int pci_module_redraw(WINDOW *win)
105 unsigned int bus, slot, func;
106 int i, last;
108 print_module_title(win, "PCI Device List");
110 last = menu_first + MENU_VISIBLE;
112 if (last > devices_index)
113 last = devices_index;
115 for (i = 0; i < MENU_VISIBLE; i++) {
116 int item = menu_first + i;
118 /* Draw a blank space. */
119 if (item >= devices_index) {
120 wattrset(win, COLOR_PAIR(2));
121 mvwprintw(win, 2 + i, 1, " ");
122 continue;
125 bus = PCI_BUS(devices[item].device);
126 slot = PCI_SLOT(devices[item].device);
127 func = PCI_FUNC(devices[item].device);
129 if (item == menu_selected)
130 wattrset(win, COLOR_PAIR(3) | A_BOLD);
131 else
132 wattrset(win, COLOR_PAIR(2));
134 mvwprintw(win, 2 + i, 1, "%X:%2.2X.%2.2X %04X:%04X ",
135 bus, slot, func,
136 devices[item].id & 0xffff,
137 (devices[item].id >> 16) & 0xffff);
139 wattrset(win, COLOR_PAIR(2));
141 if (i == 0) {
142 if (item != 0)
143 mvwaddch(win, 2 + i, 19, ACS_UARROW);
145 if (i == MENU_VISIBLE - 1) {
146 if ((item + 1) < devices_index)
147 mvwaddch(win, 2 + i, 19, ACS_DARROW);
151 wattrset(win, COLOR_PAIR(2));
153 for (i = 0; i < 16; i++)
154 mvwprintw(win, 2, 26 + (i * 3), "%2.2X ", i);
156 wmove(win, 3, 25);
158 for (i = 0; i < 48; i++)
159 waddch(win, (i == 0) ? ACS_ULCORNER : ACS_HLINE);
161 for (i = 0; i < 16; i++) {
162 mvwprintw(win, 4 + i, 23, "%2.2X", i * 16);
163 wmove(win, 4 + i, 25);
164 waddch(win, ACS_VLINE);
167 show_config_space(win, 4, 26, menu_selected);
169 return 0;
172 static void pci_scan_bus(int bus)
174 int slot, func;
175 unsigned int val;
176 unsigned char hdr;
178 for (slot = 0; slot < 0x20; slot++) {
179 for (func = 0; func < 8; func++) {
180 pcidev_t dev = PCI_DEV(bus, slot, func);
182 val = pci_read_config32(dev, REG_VENDOR_ID);
184 /* Nobody home. */
185 if (val == 0xffffffff || val == 0x00000000 ||
186 val == 0x0000ffff || val == 0xffff0000) {
188 /* If function 0 is not present, no need
189 * to test other functions. */
190 if (func == 0)
191 func = 8;
192 continue;
195 /* FIXME: Remove this arbitrary limitation. */
196 if (devices_index >= MAX_PCI_DEVICES)
197 return;
199 devices[devices_index].device =
200 PCI_DEV(bus, slot, func);
202 devices[devices_index++].id = val;
204 /* If this is a bridge, then follow it. */
205 hdr = pci_read_config8(dev, REG_HEADER_TYPE);
207 if ((func == 0) && !(hdr & HEADER_TYPE_MULTIFUNCTION))
208 func = 8;
210 hdr &= ~HEADER_TYPE_MULTIFUNCTION;
211 if (hdr == HEADER_TYPE_BRIDGE ||
212 hdr == HEADER_TYPE_CARDBUS) {
213 unsigned int busses;
215 busses = pci_read_config32(dev, REG_PRIMARY_BUS);
217 pci_scan_bus((busses >> 8) & 0xff);
223 quicksort(devices, devices_index);
226 static int pci_module_handle(int key)
228 int ret = 0;
230 switch (key) {
231 case KEY_DOWN:
232 if (menu_selected + 1 < devices_index) {
233 menu_selected++;
234 ret = 1;
236 break;
237 case KEY_UP:
238 if (menu_selected > 0) {
239 menu_selected--;
240 ret = 1;
242 break;
245 if (!ret)
246 return ret;
248 if (menu_selected < menu_first)
249 menu_first = menu_selected;
250 else if (menu_selected >= menu_first + MENU_VISIBLE) {
251 menu_first = menu_selected - (MENU_VISIBLE - 1);
252 if (menu_first < 0)
253 menu_first = 0;
256 return ret;
259 static int pci_module_init(void)
261 pci_scan_bus(0);
262 return 0;
265 struct coreinfo_module pci_module = {
266 .name = "PCI",
267 .init = pci_module_init,
268 .redraw = pci_module_redraw,
269 .handle = pci_module_handle,
272 #else
274 struct coreinfo_module pci_module = {
277 #endif