blob: cb53ed69f7124dc32271ff82b014cce23d94659d [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.
Jordan Crouse7249f792008-03-20 00:11:05 +000014 */
15
16#include <arch/io.h>
Stefan Reinauer28dff392008-08-13 08:23:06 +000017#include <pci.h>
Uwe Hermann754edf72008-08-04 21:02:07 +000018#include <libpayload.h>
Jordan Crouse7249f792008-03-20 00:11:05 +000019#include "coreinfo.h"
20
Julius Wernereab2a292019-03-05 16:55:15 -080021#if CONFIG(MODULE_PCI)
Uwe Hermannab5b3e02008-03-31 20:30:18 +000022
Jordan Crouse7249f792008-03-20 00:11:05 +000023struct pci_devices {
Stefan Reinauer28dff392008-08-13 08:23:06 +000024 pcidev_t device;
Jordan Crouse7249f792008-03-20 00:11:05 +000025 unsigned int id;
26};
27
Stefan Reinauer10d0a812008-09-05 15:18:15 +000028#define MAX_PCI_DEVICES 64
29static struct pci_devices devices[MAX_PCI_DEVICES];
Jordan Crouse7249f792008-03-20 00:11:05 +000030static int devices_index;
31
Jordan Crouse7249f792008-03-20 00:11:05 +000032/* Number of entries to show in the list */
33#define MENU_VISIBLE 16
34
35static int menu_selected = 0;
36static int menu_first = 0;
37
38static void swap(struct pci_devices *a, struct pci_devices *b)
39{
40 struct pci_devices tmp;
41
42 tmp.device = a->device;
43 tmp.id = a->id;
44
45 a->device = b->device;
46 a->id = b->id;
47
48 b->device = tmp.device;
49 b->id = tmp.id;
50}
51
52static int partition(struct pci_devices *list, int len)
53{
Jacob Garbera711e9c2019-06-28 10:58:56 -060054 pcidev_t val = list[len / 2].device;
Jordan Crouse7249f792008-03-20 00:11:05 +000055 int index = 0;
56 int i;
57
58 swap(&list[len / 2], &list[len - 1]);
59
Uwe Hermann3a406fe2008-03-20 01:11:28 +000060 for (i = 0; i < len - 1; i++) {
Jordan Crouse7249f792008-03-20 00:11:05 +000061 if (list[i].device < val) {
62 swap(&list[i], &list[index]);
63 index++;
64 }
65 }
66
67 swap(&list[index], &list[len - 1]);
Uwe Hermann3a406fe2008-03-20 01:11:28 +000068
Jordan Crouse7249f792008-03-20 00:11:05 +000069 return index;
70}
71
72static void quicksort(struct pci_devices *list, int len)
73{
74 int index;
75
76 if (len <= 1)
77 return;
78
79 index = partition(list, len);
80
81 quicksort(list, index);
82 quicksort(&(list[index]), len - index);
83}
84
Uwe Hermann35845a22008-03-20 20:05:22 +000085static void show_config_space(WINDOW *win, int row, int col, int index)
Jordan Crouse7249f792008-03-20 00:11:05 +000086{
Stefan Reinauer28dff392008-08-13 08:23:06 +000087 unsigned char cspace[256];
88 pcidev_t dev;
Jordan Crouse7249f792008-03-20 00:11:05 +000089 int i, x, y;
90
Stefan Reinauer28dff392008-08-13 08:23:06 +000091 dev = devices[index].device;
Jordan Crouse7249f792008-03-20 00:11:05 +000092
Stefan Reinauer28dff392008-08-13 08:23:06 +000093 for (i = 0; i < 256; i ++)
94 cspace[i] = pci_read_config8(dev, i);
Jordan Crouse7249f792008-03-20 00:11:05 +000095
Stefan Reinauer28dff392008-08-13 08:23:06 +000096 for (y = 0; y < 16; y++) {
Uwe Hermann3a406fe2008-03-20 01:11:28 +000097 for (x = 0; x < 16; x++)
98 mvwprintw(win, row + y, col + (x * 3), "%2.2X ",
99 cspace[(y * 16) + x]);
Jordan Crouse7249f792008-03-20 00:11:05 +0000100 }
101}
102
Uwe Hermann0bfb5c42008-03-23 15:34:04 +0000103static int pci_module_redraw(WINDOW *win)
Jordan Crouse7249f792008-03-20 00:11:05 +0000104{
Stefan Reinauer28dff392008-08-13 08:23:06 +0000105 unsigned int bus, slot, func;
Jacob Garberdce10f82019-04-06 16:10:36 -0600106 int i;
Jordan Crouse7249f792008-03-20 00:11:05 +0000107
108 print_module_title(win, "PCI Device List");
109
Uwe Hermann3a406fe2008-03-20 01:11:28 +0000110 for (i = 0; i < MENU_VISIBLE; i++) {
Jordan Crouse7249f792008-03-20 00:11:05 +0000111 int item = menu_first + i;
112
Uwe Hermann3a406fe2008-03-20 01:11:28 +0000113 /* Draw a blank space. */
Jordan Crouse7249f792008-03-20 00:11:05 +0000114 if (item >= devices_index) {
115 wattrset(win, COLOR_PAIR(2));
116 mvwprintw(win, 2 + i, 1, " ");
117 continue;
118 }
119
Stefan Reinauer28dff392008-08-13 08:23:06 +0000120 bus = PCI_BUS(devices[item].device);
121 slot = PCI_SLOT(devices[item].device);
122 func = PCI_FUNC(devices[item].device);
Jordan Crouse7249f792008-03-20 00:11:05 +0000123
124 if (item == menu_selected)
125 wattrset(win, COLOR_PAIR(3) | A_BOLD);
126 else
127 wattrset(win, COLOR_PAIR(2));
128
Uwe Hermanna0c00932008-03-27 20:46:49 +0000129 mvwprintw(win, 2 + i, 1, "%X:%2.2X.%2.2X %04X:%04X ",
Stefan Reinauer28dff392008-08-13 08:23:06 +0000130 bus, slot, func,
Uwe Hermann3a406fe2008-03-20 01:11:28 +0000131 devices[item].id & 0xffff,
132 (devices[item].id >> 16) & 0xffff);
Jordan Crouse7249f792008-03-20 00:11:05 +0000133
134 wattrset(win, COLOR_PAIR(2));
135
136 if (i == 0) {
137 if (item != 0)
Ulf Jordand133d9a2008-08-11 20:35:32 +0000138 mvwaddch(win, 2 + i, 19, ACS_UARROW);
Jordan Crouse7249f792008-03-20 00:11:05 +0000139 }
140 if (i == MENU_VISIBLE - 1) {
141 if ((item + 1) < devices_index)
Ulf Jordand133d9a2008-08-11 20:35:32 +0000142 mvwaddch(win, 2 + i, 19, ACS_DARROW);
Jordan Crouse7249f792008-03-20 00:11:05 +0000143 }
144 }
145
146 wattrset(win, COLOR_PAIR(2));
147
Uwe Hermann3a406fe2008-03-20 01:11:28 +0000148 for (i = 0; i < 16; i++)
Jordan Crouse7249f792008-03-20 00:11:05 +0000149 mvwprintw(win, 2, 26 + (i * 3), "%2.2X ", i);
150
151 wmove(win, 3, 25);
152
Uwe Hermann3a406fe2008-03-20 01:11:28 +0000153 for (i = 0; i < 48; i++)
Ulf Jordand133d9a2008-08-11 20:35:32 +0000154 waddch(win, (i == 0) ? ACS_ULCORNER : ACS_HLINE);
Jordan Crouse7249f792008-03-20 00:11:05 +0000155
Stefan Reinauer28dff392008-08-13 08:23:06 +0000156 for (i = 0; i < 16; i++) {
Jordan Crouse7249f792008-03-20 00:11:05 +0000157 mvwprintw(win, 4 + i, 23, "%2.2X", i * 16);
158 wmove(win, 4 + i, 25);
Ulf Jordand133d9a2008-08-11 20:35:32 +0000159 waddch(win, ACS_VLINE);
Jordan Crouse7249f792008-03-20 00:11:05 +0000160 }
161
162 show_config_space(win, 4, 26, menu_selected);
163
164 return 0;
165}
166
167static void pci_scan_bus(int bus)
168{
Stefan Reinauer28dff392008-08-13 08:23:06 +0000169 int slot, func;
Jordan Crouse7249f792008-03-20 00:11:05 +0000170 unsigned int val;
171 unsigned char hdr;
172
Stefan Reinauerbc5379a2008-08-13 09:38:12 +0000173 for (slot = 0; slot < 0x20; slot++) {
Stefan Reinauer28dff392008-08-13 08:23:06 +0000174 for (func = 0; func < 8; func++) {
175 pcidev_t dev = PCI_DEV(bus, slot, func);
Stefan Reinauer88ad6b02008-08-07 19:09:17 +0000176
177 val = pci_read_config32(dev, REG_VENDOR_ID);
Jordan Crouse7249f792008-03-20 00:11:05 +0000178
Uwe Hermann3a406fe2008-03-20 01:11:28 +0000179 /* Nobody home. */
Jordan Crouse7249f792008-03-20 00:11:05 +0000180 if (val == 0xffffffff || val == 0x00000000 ||
Kyösti Mälkki1dc5ce32018-06-09 08:25:03 +0300181 val == 0x0000ffff || val == 0xffff0000) {
182
183 /* If function 0 is not present, no need
184 * to test other functions. */
185 if (func == 0)
186 func = 8;
Jordan Crouse7249f792008-03-20 00:11:05 +0000187 continue;
Kyösti Mälkki1dc5ce32018-06-09 08:25:03 +0300188 }
Jordan Crouse7249f792008-03-20 00:11:05 +0000189
Uwe Hermann3a406fe2008-03-20 01:11:28 +0000190 /* FIXME: Remove this arbitrary limitation. */
Stefan Reinauer10d0a812008-09-05 15:18:15 +0000191 if (devices_index >= MAX_PCI_DEVICES)
Jordan Crouse7249f792008-03-20 00:11:05 +0000192 return;
193
Stefan Reinauer14e22772010-04-27 06:56:47 +0000194 devices[devices_index].device =
Stefan Reinauer28dff392008-08-13 08:23:06 +0000195 PCI_DEV(bus, slot, func);
Jordan Crouse7249f792008-03-20 00:11:05 +0000196
197 devices[devices_index++].id = val;
198
Uwe Hermann3a406fe2008-03-20 01:11:28 +0000199 /* If this is a bridge, then follow it. */
Stefan Reinauer88ad6b02008-08-07 19:09:17 +0000200 hdr = pci_read_config8(dev, REG_HEADER_TYPE);
Kyösti Mälkki1dc5ce32018-06-09 08:25:03 +0300201
202 if ((func == 0) && !(hdr & HEADER_TYPE_MULTIFUNCTION))
203 func = 8;
204
205 hdr &= ~HEADER_TYPE_MULTIFUNCTION;
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