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