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