Jacob Garber | 07201d7 | 2020-09-08 12:25:44 -0600 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Jordan Crouse | 7249f79 | 2008-03-20 00:11:05 +0000 | [diff] [blame] | 2 | |
| 3 | #include <arch/io.h> |
Stefan Reinauer | 28dff39 | 2008-08-13 08:23:06 +0000 | [diff] [blame] | 4 | #include <pci.h> |
Uwe Hermann | 754edf7 | 2008-08-04 21:02:07 +0000 | [diff] [blame] | 5 | #include <libpayload.h> |
Jordan Crouse | 7249f79 | 2008-03-20 00:11:05 +0000 | [diff] [blame] | 6 | #include "coreinfo.h" |
| 7 | |
Julius Werner | eab2a29 | 2019-03-05 16:55:15 -0800 | [diff] [blame] | 8 | #if CONFIG(MODULE_PCI) |
Uwe Hermann | ab5b3e0 | 2008-03-31 20:30:18 +0000 | [diff] [blame] | 9 | |
Jordan Crouse | 7249f79 | 2008-03-20 00:11:05 +0000 | [diff] [blame] | 10 | struct pci_devices { |
Stefan Reinauer | 28dff39 | 2008-08-13 08:23:06 +0000 | [diff] [blame] | 11 | pcidev_t device; |
Jordan Crouse | 7249f79 | 2008-03-20 00:11:05 +0000 | [diff] [blame] | 12 | unsigned int id; |
| 13 | }; |
| 14 | |
Stefan Reinauer | 10d0a81 | 2008-09-05 15:18:15 +0000 | [diff] [blame] | 15 | #define MAX_PCI_DEVICES 64 |
| 16 | static struct pci_devices devices[MAX_PCI_DEVICES]; |
Jordan Crouse | 7249f79 | 2008-03-20 00:11:05 +0000 | [diff] [blame] | 17 | static int devices_index; |
| 18 | |
Jordan Crouse | 7249f79 | 2008-03-20 00:11:05 +0000 | [diff] [blame] | 19 | /* Number of entries to show in the list */ |
| 20 | #define MENU_VISIBLE 16 |
| 21 | |
| 22 | static int menu_selected = 0; |
| 23 | static int menu_first = 0; |
| 24 | |
| 25 | static void swap(struct pci_devices *a, struct pci_devices *b) |
| 26 | { |
| 27 | struct pci_devices tmp; |
| 28 | |
| 29 | tmp.device = a->device; |
| 30 | tmp.id = a->id; |
| 31 | |
| 32 | a->device = b->device; |
| 33 | a->id = b->id; |
| 34 | |
| 35 | b->device = tmp.device; |
| 36 | b->id = tmp.id; |
| 37 | } |
| 38 | |
| 39 | static int partition(struct pci_devices *list, int len) |
| 40 | { |
Jacob Garber | a711e9c | 2019-06-28 10:58:56 -0600 | [diff] [blame] | 41 | pcidev_t val = list[len / 2].device; |
Jordan Crouse | 7249f79 | 2008-03-20 00:11:05 +0000 | [diff] [blame] | 42 | int index = 0; |
| 43 | int i; |
| 44 | |
| 45 | swap(&list[len / 2], &list[len - 1]); |
| 46 | |
Uwe Hermann | 3a406fe | 2008-03-20 01:11:28 +0000 | [diff] [blame] | 47 | for (i = 0; i < len - 1; i++) { |
Jordan Crouse | 7249f79 | 2008-03-20 00:11:05 +0000 | [diff] [blame] | 48 | if (list[i].device < val) { |
| 49 | swap(&list[i], &list[index]); |
| 50 | index++; |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | swap(&list[index], &list[len - 1]); |
Uwe Hermann | 3a406fe | 2008-03-20 01:11:28 +0000 | [diff] [blame] | 55 | |
Jordan Crouse | 7249f79 | 2008-03-20 00:11:05 +0000 | [diff] [blame] | 56 | return index; |
| 57 | } |
| 58 | |
| 59 | static void quicksort(struct pci_devices *list, int len) |
| 60 | { |
| 61 | int index; |
| 62 | |
| 63 | if (len <= 1) |
| 64 | return; |
| 65 | |
| 66 | index = partition(list, len); |
| 67 | |
| 68 | quicksort(list, index); |
| 69 | quicksort(&(list[index]), len - index); |
| 70 | } |
| 71 | |
Uwe Hermann | 35845a2 | 2008-03-20 20:05:22 +0000 | [diff] [blame] | 72 | static void show_config_space(WINDOW *win, int row, int col, int index) |
Jordan Crouse | 7249f79 | 2008-03-20 00:11:05 +0000 | [diff] [blame] | 73 | { |
Stefan Reinauer | 28dff39 | 2008-08-13 08:23:06 +0000 | [diff] [blame] | 74 | unsigned char cspace[256]; |
| 75 | pcidev_t dev; |
Jordan Crouse | 7249f79 | 2008-03-20 00:11:05 +0000 | [diff] [blame] | 76 | int i, x, y; |
| 77 | |
Stefan Reinauer | 28dff39 | 2008-08-13 08:23:06 +0000 | [diff] [blame] | 78 | dev = devices[index].device; |
Jordan Crouse | 7249f79 | 2008-03-20 00:11:05 +0000 | [diff] [blame] | 79 | |
Stefan Reinauer | 28dff39 | 2008-08-13 08:23:06 +0000 | [diff] [blame] | 80 | for (i = 0; i < 256; i ++) |
| 81 | cspace[i] = pci_read_config8(dev, i); |
Jordan Crouse | 7249f79 | 2008-03-20 00:11:05 +0000 | [diff] [blame] | 82 | |
Stefan Reinauer | 28dff39 | 2008-08-13 08:23:06 +0000 | [diff] [blame] | 83 | for (y = 0; y < 16; y++) { |
Uwe Hermann | 3a406fe | 2008-03-20 01:11:28 +0000 | [diff] [blame] | 84 | for (x = 0; x < 16; x++) |
| 85 | mvwprintw(win, row + y, col + (x * 3), "%2.2X ", |
| 86 | cspace[(y * 16) + x]); |
Jordan Crouse | 7249f79 | 2008-03-20 00:11:05 +0000 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | |
Uwe Hermann | 0bfb5c4 | 2008-03-23 15:34:04 +0000 | [diff] [blame] | 90 | static int pci_module_redraw(WINDOW *win) |
Jordan Crouse | 7249f79 | 2008-03-20 00:11:05 +0000 | [diff] [blame] | 91 | { |
Stefan Reinauer | 28dff39 | 2008-08-13 08:23:06 +0000 | [diff] [blame] | 92 | unsigned int bus, slot, func; |
Jacob Garber | dce10f8 | 2019-04-06 16:10:36 -0600 | [diff] [blame] | 93 | int i; |
Jordan Crouse | 7249f79 | 2008-03-20 00:11:05 +0000 | [diff] [blame] | 94 | |
| 95 | print_module_title(win, "PCI Device List"); |
| 96 | |
Uwe Hermann | 3a406fe | 2008-03-20 01:11:28 +0000 | [diff] [blame] | 97 | for (i = 0; i < MENU_VISIBLE; i++) { |
Jordan Crouse | 7249f79 | 2008-03-20 00:11:05 +0000 | [diff] [blame] | 98 | int item = menu_first + i; |
| 99 | |
Uwe Hermann | 3a406fe | 2008-03-20 01:11:28 +0000 | [diff] [blame] | 100 | /* Draw a blank space. */ |
Jordan Crouse | 7249f79 | 2008-03-20 00:11:05 +0000 | [diff] [blame] | 101 | if (item >= devices_index) { |
| 102 | wattrset(win, COLOR_PAIR(2)); |
| 103 | mvwprintw(win, 2 + i, 1, " "); |
| 104 | continue; |
| 105 | } |
| 106 | |
Stefan Reinauer | 28dff39 | 2008-08-13 08:23:06 +0000 | [diff] [blame] | 107 | bus = PCI_BUS(devices[item].device); |
| 108 | slot = PCI_SLOT(devices[item].device); |
| 109 | func = PCI_FUNC(devices[item].device); |
Jordan Crouse | 7249f79 | 2008-03-20 00:11:05 +0000 | [diff] [blame] | 110 | |
| 111 | if (item == menu_selected) |
| 112 | wattrset(win, COLOR_PAIR(3) | A_BOLD); |
| 113 | else |
| 114 | wattrset(win, COLOR_PAIR(2)); |
| 115 | |
Uwe Hermann | a0c0093 | 2008-03-27 20:46:49 +0000 | [diff] [blame] | 116 | mvwprintw(win, 2 + i, 1, "%X:%2.2X.%2.2X %04X:%04X ", |
Stefan Reinauer | 28dff39 | 2008-08-13 08:23:06 +0000 | [diff] [blame] | 117 | bus, slot, func, |
Uwe Hermann | 3a406fe | 2008-03-20 01:11:28 +0000 | [diff] [blame] | 118 | devices[item].id & 0xffff, |
| 119 | (devices[item].id >> 16) & 0xffff); |
Jordan Crouse | 7249f79 | 2008-03-20 00:11:05 +0000 | [diff] [blame] | 120 | |
| 121 | wattrset(win, COLOR_PAIR(2)); |
| 122 | |
| 123 | if (i == 0) { |
| 124 | if (item != 0) |
Ulf Jordan | d133d9a | 2008-08-11 20:35:32 +0000 | [diff] [blame] | 125 | mvwaddch(win, 2 + i, 19, ACS_UARROW); |
Jordan Crouse | 7249f79 | 2008-03-20 00:11:05 +0000 | [diff] [blame] | 126 | } |
| 127 | if (i == MENU_VISIBLE - 1) { |
| 128 | if ((item + 1) < devices_index) |
Ulf Jordan | d133d9a | 2008-08-11 20:35:32 +0000 | [diff] [blame] | 129 | mvwaddch(win, 2 + i, 19, ACS_DARROW); |
Jordan Crouse | 7249f79 | 2008-03-20 00:11:05 +0000 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | |
| 133 | wattrset(win, COLOR_PAIR(2)); |
| 134 | |
Uwe Hermann | 3a406fe | 2008-03-20 01:11:28 +0000 | [diff] [blame] | 135 | for (i = 0; i < 16; i++) |
Jordan Crouse | 7249f79 | 2008-03-20 00:11:05 +0000 | [diff] [blame] | 136 | mvwprintw(win, 2, 26 + (i * 3), "%2.2X ", i); |
| 137 | |
| 138 | wmove(win, 3, 25); |
| 139 | |
Uwe Hermann | 3a406fe | 2008-03-20 01:11:28 +0000 | [diff] [blame] | 140 | for (i = 0; i < 48; i++) |
Ulf Jordan | d133d9a | 2008-08-11 20:35:32 +0000 | [diff] [blame] | 141 | waddch(win, (i == 0) ? ACS_ULCORNER : ACS_HLINE); |
Jordan Crouse | 7249f79 | 2008-03-20 00:11:05 +0000 | [diff] [blame] | 142 | |
Stefan Reinauer | 28dff39 | 2008-08-13 08:23:06 +0000 | [diff] [blame] | 143 | for (i = 0; i < 16; i++) { |
Jordan Crouse | 7249f79 | 2008-03-20 00:11:05 +0000 | [diff] [blame] | 144 | mvwprintw(win, 4 + i, 23, "%2.2X", i * 16); |
| 145 | wmove(win, 4 + i, 25); |
Ulf Jordan | d133d9a | 2008-08-11 20:35:32 +0000 | [diff] [blame] | 146 | waddch(win, ACS_VLINE); |
Jordan Crouse | 7249f79 | 2008-03-20 00:11:05 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | show_config_space(win, 4, 26, menu_selected); |
| 150 | |
| 151 | return 0; |
| 152 | } |
| 153 | |
Felix Singer | 6034b0f | 2020-10-19 17:42:59 +0200 | [diff] [blame] | 154 | static void ci_pci_scan_bus(int bus) |
Jordan Crouse | 7249f79 | 2008-03-20 00:11:05 +0000 | [diff] [blame] | 155 | { |
Stefan Reinauer | 28dff39 | 2008-08-13 08:23:06 +0000 | [diff] [blame] | 156 | int slot, func; |
Jordan Crouse | 7249f79 | 2008-03-20 00:11:05 +0000 | [diff] [blame] | 157 | unsigned int val; |
| 158 | unsigned char hdr; |
| 159 | |
Stefan Reinauer | bc5379a | 2008-08-13 09:38:12 +0000 | [diff] [blame] | 160 | for (slot = 0; slot < 0x20; slot++) { |
Stefan Reinauer | 28dff39 | 2008-08-13 08:23:06 +0000 | [diff] [blame] | 161 | for (func = 0; func < 8; func++) { |
| 162 | pcidev_t dev = PCI_DEV(bus, slot, func); |
Stefan Reinauer | 88ad6b0 | 2008-08-07 19:09:17 +0000 | [diff] [blame] | 163 | |
| 164 | val = pci_read_config32(dev, REG_VENDOR_ID); |
Jordan Crouse | 7249f79 | 2008-03-20 00:11:05 +0000 | [diff] [blame] | 165 | |
Uwe Hermann | 3a406fe | 2008-03-20 01:11:28 +0000 | [diff] [blame] | 166 | /* Nobody home. */ |
Jordan Crouse | 7249f79 | 2008-03-20 00:11:05 +0000 | [diff] [blame] | 167 | if (val == 0xffffffff || val == 0x00000000 || |
Kyösti Mälkki | 1dc5ce3 | 2018-06-09 08:25:03 +0300 | [diff] [blame] | 168 | val == 0x0000ffff || val == 0xffff0000) { |
| 169 | |
| 170 | /* If function 0 is not present, no need |
| 171 | * to test other functions. */ |
| 172 | if (func == 0) |
| 173 | func = 8; |
Jordan Crouse | 7249f79 | 2008-03-20 00:11:05 +0000 | [diff] [blame] | 174 | continue; |
Kyösti Mälkki | 1dc5ce3 | 2018-06-09 08:25:03 +0300 | [diff] [blame] | 175 | } |
Jordan Crouse | 7249f79 | 2008-03-20 00:11:05 +0000 | [diff] [blame] | 176 | |
Uwe Hermann | 3a406fe | 2008-03-20 01:11:28 +0000 | [diff] [blame] | 177 | /* FIXME: Remove this arbitrary limitation. */ |
Stefan Reinauer | 10d0a81 | 2008-09-05 15:18:15 +0000 | [diff] [blame] | 178 | if (devices_index >= MAX_PCI_DEVICES) |
Jordan Crouse | 7249f79 | 2008-03-20 00:11:05 +0000 | [diff] [blame] | 179 | return; |
| 180 | |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 181 | devices[devices_index].device = |
Stefan Reinauer | 28dff39 | 2008-08-13 08:23:06 +0000 | [diff] [blame] | 182 | PCI_DEV(bus, slot, func); |
Jordan Crouse | 7249f79 | 2008-03-20 00:11:05 +0000 | [diff] [blame] | 183 | |
| 184 | devices[devices_index++].id = val; |
| 185 | |
Uwe Hermann | 3a406fe | 2008-03-20 01:11:28 +0000 | [diff] [blame] | 186 | /* If this is a bridge, then follow it. */ |
Stefan Reinauer | 88ad6b0 | 2008-08-07 19:09:17 +0000 | [diff] [blame] | 187 | hdr = pci_read_config8(dev, REG_HEADER_TYPE); |
Kyösti Mälkki | 1dc5ce3 | 2018-06-09 08:25:03 +0300 | [diff] [blame] | 188 | |
| 189 | if ((func == 0) && !(hdr & HEADER_TYPE_MULTIFUNCTION)) |
| 190 | func = 8; |
| 191 | |
| 192 | hdr &= ~HEADER_TYPE_MULTIFUNCTION; |
Jordan Crouse | 7249f79 | 2008-03-20 00:11:05 +0000 | [diff] [blame] | 193 | if (hdr == HEADER_TYPE_BRIDGE || |
| 194 | hdr == HEADER_TYPE_CARDBUS) { |
| 195 | unsigned int busses; |
| 196 | |
Stefan Reinauer | 88ad6b0 | 2008-08-07 19:09:17 +0000 | [diff] [blame] | 197 | busses = pci_read_config32(dev, REG_PRIMARY_BUS); |
Jordan Crouse | 7249f79 | 2008-03-20 00:11:05 +0000 | [diff] [blame] | 198 | |
Felix Singer | 6034b0f | 2020-10-19 17:42:59 +0200 | [diff] [blame] | 199 | ci_pci_scan_bus((busses >> 8) & 0xff); |
Jordan Crouse | 7249f79 | 2008-03-20 00:11:05 +0000 | [diff] [blame] | 200 | |
| 201 | } |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | quicksort(devices, devices_index); |
| 206 | } |
| 207 | |
Uwe Hermann | 0bfb5c4 | 2008-03-23 15:34:04 +0000 | [diff] [blame] | 208 | static int pci_module_handle(int key) |
Jordan Crouse | 7249f79 | 2008-03-20 00:11:05 +0000 | [diff] [blame] | 209 | { |
| 210 | int ret = 0; |
| 211 | |
Uwe Hermann | 3a406fe | 2008-03-20 01:11:28 +0000 | [diff] [blame] | 212 | switch (key) { |
Jordan Crouse | 7249f79 | 2008-03-20 00:11:05 +0000 | [diff] [blame] | 213 | case KEY_DOWN: |
| 214 | if (menu_selected + 1 < devices_index) { |
| 215 | menu_selected++; |
| 216 | ret = 1; |
| 217 | } |
Jordan Crouse | 7249f79 | 2008-03-20 00:11:05 +0000 | [diff] [blame] | 218 | break; |
Jordan Crouse | 7249f79 | 2008-03-20 00:11:05 +0000 | [diff] [blame] | 219 | case KEY_UP: |
| 220 | if (menu_selected > 0) { |
| 221 | menu_selected--; |
| 222 | ret = 1; |
| 223 | } |
Jordan Crouse | 7249f79 | 2008-03-20 00:11:05 +0000 | [diff] [blame] | 224 | break; |
| 225 | } |
| 226 | |
| 227 | if (!ret) |
| 228 | return ret; |
| 229 | |
| 230 | if (menu_selected < menu_first) |
| 231 | menu_first = menu_selected; |
| 232 | else if (menu_selected >= menu_first + MENU_VISIBLE) { |
| 233 | menu_first = menu_selected - (MENU_VISIBLE - 1); |
| 234 | if (menu_first < 0) |
| 235 | menu_first = 0; |
| 236 | } |
| 237 | |
Jordan Crouse | 7249f79 | 2008-03-20 00:11:05 +0000 | [diff] [blame] | 238 | return ret; |
| 239 | } |
| 240 | |
Uwe Hermann | 0bfb5c4 | 2008-03-23 15:34:04 +0000 | [diff] [blame] | 241 | static int pci_module_init(void) |
Jordan Crouse | 7249f79 | 2008-03-20 00:11:05 +0000 | [diff] [blame] | 242 | { |
Felix Singer | 6034b0f | 2020-10-19 17:42:59 +0200 | [diff] [blame] | 243 | ci_pci_scan_bus(0); |
Jordan Crouse | 7249f79 | 2008-03-20 00:11:05 +0000 | [diff] [blame] | 244 | return 0; |
| 245 | } |
| 246 | |
| 247 | struct coreinfo_module pci_module = { |
| 248 | .name = "PCI", |
| 249 | .init = pci_module_init, |
| 250 | .redraw = pci_module_redraw, |
| 251 | .handle = pci_module_handle, |
| 252 | }; |
Uwe Hermann | ab5b3e0 | 2008-03-31 20:30:18 +0000 | [diff] [blame] | 253 | |
| 254 | #else |
| 255 | |
| 256 | struct coreinfo_module pci_module = { |
| 257 | }; |
| 258 | |
| 259 | #endif |