| 1 |
|
| 2 |
|
| 3 |
#include <console/console.h> |
| 4 |
#include <device/pci.h> |
| 5 |
#include <device/pci_ops.h> |
| 6 |
#include <intelblocks/gpio.h> |
| 7 |
#include <intelblocks/irq.h> |
| 8 |
#include <intelblocks/lpc_lib.h> |
| 9 |
#include <soc/pci_devs.h> |
| 10 |
#include <southbridge/intel/common/acpi_pirq_gen.h> |
| 11 |
#include <stdlib.h> |
| 12 |
#include <types.h> |
| 13 |
|
| 14 |
#define MIN_SHARED_IRQ 16 |
| 15 |
#define MAX_SHARED_IRQ 23 |
| 16 |
#define TOTAL_SHARED_IRQ (MAX_SHARED_IRQ - MIN_SHARED_IRQ + 1) |
| 17 |
#define MAX_IRQS 120 |
| 18 |
|
| 19 |
#define IDX2PIN(i) (enum pci_pin)((i) + PCI_INT_A) |
| 20 |
#define PIN2IDX(p) (size_t)((p) - PCI_INT_A) |
| 21 |
|
| 22 |
struct pin_info { |
| 23 |
enum pin_state { |
| 24 |
FREE_PIN, |
| 25 |
SHARED_IRQ_PIN, |
| 26 |
UNIQUE_IRQ_PIN, |
| 27 |
} pin_state; |
| 28 |
unsigned int usage_count; |
| 29 |
unsigned int irq; |
| 30 |
}; |
| 31 |
|
| 32 |
static unsigned int irq_share_count[TOTAL_SHARED_IRQ]; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
static int find_free_unique_irq(void) |
| 61 |
{ |
| 62 |
static unsigned int next_irq = MAX_SHARED_IRQ + 1; |
| 63 |
|
| 64 |
while (next_irq < MAX_IRQS && gpio_routes_ioapic_irq(next_irq)) |
| 65 |
++next_irq; |
| 66 |
|
| 67 |
if (next_irq == MAX_IRQS) |
| 68 |
return INVALID_IRQ; |
| 69 |
|
| 70 |
return next_irq++; |
| 71 |
} |
| 72 |
|
| 73 |
static enum pci_pin find_free_pin(const struct pin_info pin_info[PCI_INT_MAX]) |
| 74 |
{ |
| 75 |
for (size_t pin_idx = 0; pin_idx < PCI_INT_MAX; pin_idx++) { |
| 76 |
if (pin_info[pin_idx].pin_state == FREE_PIN) |
| 77 |
return IDX2PIN(pin_idx); |
| 78 |
} |
| 79 |
|
| 80 |
return PCI_INT_NONE; |
| 81 |
} |
| 82 |
|
| 83 |
static enum pci_pin find_shareable_pin(const struct pin_info pin_info[PCI_INT_MAX]) |
| 84 |
{ |
| 85 |
unsigned int least_shared = 255; |
| 86 |
int least_index = -1; |
| 87 |
|
| 88 |
for (size_t pin_idx = 0; pin_idx < PCI_INT_MAX; pin_idx++) { |
| 89 |
if (pin_info[pin_idx].pin_state == SHARED_IRQ_PIN && |
| 90 |
pin_info[pin_idx].usage_count < least_shared) { |
| 91 |
least_shared = pin_info[pin_idx].usage_count; |
| 92 |
least_index = pin_idx; |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
if (least_index < 0) |
| 97 |
return PCI_INT_NONE; |
| 98 |
|
| 99 |
return IDX2PIN(least_index); |
| 100 |
} |
| 101 |
|
| 102 |
static enum pirq find_global_least_used_pirq(void) |
| 103 |
{ |
| 104 |
unsigned int least_shared = 255; |
| 105 |
int least_index = -1; |
| 106 |
|
| 107 |
for (size_t i = 0; i < TOTAL_SHARED_IRQ; i++) { |
| 108 |
if (irq_share_count[i] < least_shared) { |
| 109 |
least_shared = irq_share_count[i]; |
| 110 |
least_index = i; |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
if (least_index >= 0) |
| 115 |
return (enum pirq)least_index + PIRQ_A; |
| 116 |
|
| 117 |
return PIRQ_INVALID; |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
static int pirq_to_irq(enum pirq pirq) |
| 122 |
{ |
| 123 |
return pirq_idx(pirq) + MIN_SHARED_IRQ; |
| 124 |
} |
| 125 |
|
| 126 |
static bool assign_pirq(struct pin_info pin_info[PCI_INT_MAX], enum pci_pin pin, enum pirq pirq) |
| 127 |
{ |
| 128 |
if (pirq < PIRQ_A || pirq > PIRQ_H) { |
| 129 |
printk(BIOS_ERR, "Invalid pirq constraint %u\n", pirq); |
| 130 |
return false; |
| 131 |
} |
| 132 |
|
| 133 |
const int irq = pirq_to_irq(pirq); |
| 134 |
pin_info[PIN2IDX(pin)].irq = irq; |
| 135 |
irq_share_count[pirq_idx(pirq)]++; |
| 136 |
return true; |
| 137 |
} |
| 138 |
|
| 139 |
static bool assign_pin(enum pci_pin pin, unsigned int fn, enum pin_state state, |
| 140 |
struct pin_info *pin_info, |
| 141 |
enum pci_pin fn_pin_map[MAX_FNS]) |
| 142 |
{ |
| 143 |
if (pin < PCI_INT_A || pin > PCI_INT_D) { |
| 144 |
printk(BIOS_ERR, "Invalid pin constraint %u\n", pin); |
| 145 |
return false; |
| 146 |
} |
| 147 |
|
| 148 |
const size_t pin_idx = PIN2IDX(pin); |
| 149 |
pin_info[pin_idx].pin_state = state; |
| 150 |
pin_info[pin_idx].usage_count++; |
| 151 |
fn_pin_map[fn] = pin; |
| 152 |
|
| 153 |
return true; |
| 154 |
} |
| 155 |
|
| 156 |
static bool assign_fixed_pins(const struct slot_irq_constraints *constraints, |
| 157 |
struct pin_info *pin_info, enum pci_pin fn_pin_map[MAX_FNS]) |
| 158 |
{ |
| 159 |
for (size_t i = 0; i < MAX_FNS; i++) { |
| 160 |
const enum pci_pin fixed_int_pin = constraints->fns[i].fixed_int_pin; |
| 161 |
if (fixed_int_pin == PCI_INT_NONE) |
| 162 |
continue; |
| 163 |
|
| 164 |
if (!assign_pin(fixed_int_pin, i, SHARED_IRQ_PIN, pin_info, fn_pin_map)) |
| 165 |
return false; |
| 166 |
} |
| 167 |
|
| 168 |
return true; |
| 169 |
} |
| 170 |
|
| 171 |
static bool assign_fixed_pirqs(const struct slot_irq_constraints *constraints, |
| 172 |
struct pin_info *pin_info, enum pci_pin fn_pin_map[MAX_FNS]) |
| 173 |
{ |
| 174 |
for (size_t i = 0; i < MAX_FNS; i++) { |
| 175 |
const enum pirq fixed_pirq = constraints->fns[i].fixed_pirq; |
| 176 |
if (fixed_pirq == PIRQ_INVALID) |
| 177 |
continue; |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
const enum pci_pin pin = fn_pin_map[i]; |
| 182 |
if (pin == PCI_INT_NONE) { |
| 183 |
printk(BIOS_ERR, "Slot %u, pirq %u, no pin for function %zu\n", |
| 184 |
constraints->slot, fixed_pirq, i); |
| 185 |
return false; |
| 186 |
} |
| 187 |
|
| 188 |
if (!assign_pirq(pin_info, pin, fixed_pirq)) |
| 189 |
return false; |
| 190 |
} |
| 191 |
|
| 192 |
return true; |
| 193 |
} |
| 194 |
|
| 195 |
static bool assign_direct_irqs(const struct slot_irq_constraints *constraints, |
| 196 |
struct pin_info *pin_info, enum pci_pin fn_pin_map[MAX_FNS]) |
| 197 |
{ |
| 198 |
for (size_t i = 0; i < MAX_FNS; i++) { |
| 199 |
if (constraints->fns[i].irq_route != IRQ_DIRECT) |
| 200 |
continue; |
| 201 |
|
| 202 |
enum pci_pin pin = find_free_pin(pin_info); |
| 203 |
if (pin == PCI_INT_NONE) |
| 204 |
return false; |
| 205 |
|
| 206 |
if (!assign_pin(pin, i, UNIQUE_IRQ_PIN, pin_info, fn_pin_map)) |
| 207 |
return false; |
| 208 |
|
| 209 |
const int irq = find_free_unique_irq(); |
| 210 |
if (irq == INVALID_IRQ) { |
| 211 |
printk(BIOS_ERR, "No free unique IRQs found\n"); |
| 212 |
return false; |
| 213 |
} |
| 214 |
|
| 215 |
const size_t pin_idx = PIN2IDX(pin); |
| 216 |
pin_info[pin_idx].irq = irq; |
| 217 |
} |
| 218 |
|
| 219 |
return true; |
| 220 |
} |
| 221 |
|
| 222 |
static bool assign_shareable_pins(const struct slot_irq_constraints *constraints, |
| 223 |
struct pin_info *pin_info, enum pci_pin fn_pin_map[MAX_FNS]) |
| 224 |
{ |
| 225 |
for (size_t i = 0; i < MAX_FNS; i++) { |
| 226 |
if (constraints->fns[i].irq_route != IRQ_PIRQ) |
| 227 |
continue; |
| 228 |
|
| 229 |
if (fn_pin_map[i] == PCI_INT_NONE) { |
| 230 |
enum pci_pin pin = find_free_pin(pin_info); |
| 231 |
if (pin == PCI_INT_NONE) { |
| 232 |
pin = find_shareable_pin(pin_info); |
| 233 |
|
| 234 |
if (pin == PCI_INT_NONE) { |
| 235 |
printk(BIOS_ERR, "No shareable pins found\n"); |
| 236 |
return false; |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
if (!assign_pin(pin, i, SHARED_IRQ_PIN, pin_info, fn_pin_map)) |
| 241 |
return false; |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
return true; |
| 246 |
} |
| 247 |
|
| 248 |
static bool assign_pirqs(struct pin_info pin_info[PCI_INT_MAX]) |
| 249 |
{ |
| 250 |
for (size_t pin_idx = 0; pin_idx < PCI_INT_MAX; pin_idx++) { |
| 251 |
if (pin_info[pin_idx].pin_state != SHARED_IRQ_PIN || pin_info[pin_idx].irq != 0) |
| 252 |
continue; |
| 253 |
|
| 254 |
enum pirq pirq = find_global_least_used_pirq(); |
| 255 |
if (pirq == PIRQ_INVALID) |
| 256 |
return false; |
| 257 |
|
| 258 |
if (!assign_pirq(pin_info, IDX2PIN(pin_idx), pirq)) |
| 259 |
return false; |
| 260 |
} |
| 261 |
|
| 262 |
return true; |
| 263 |
} |
| 264 |
|
| 265 |
static void add_entry(struct pci_irq_entry **head, pci_devfn_t devfn, enum pci_pin pin, |
| 266 |
unsigned int irq) |
| 267 |
{ |
| 268 |
struct pci_irq_entry *entry = malloc(sizeof(*entry)); |
| 269 |
struct pci_irq_entry **tmp = head; |
| 270 |
|
| 271 |
entry->devfn = devfn; |
| 272 |
entry->pin = pin; |
| 273 |
entry->irq = irq; |
| 274 |
entry->next = NULL; |
| 275 |
|
| 276 |
while (*tmp) |
| 277 |
tmp = &(*tmp)->next; |
| 278 |
|
| 279 |
*tmp = entry; |
| 280 |
} |
| 281 |
|
| 282 |
static void add_slot_entries(struct pci_irq_entry **head, unsigned int slot, |
| 283 |
struct pin_info pin_info[PCI_INT_MAX], |
| 284 |
const enum pci_pin fn_pin_map[MAX_FNS]) |
| 285 |
{ |
| 286 |
for (size_t fn = 0; fn < MAX_FNS; fn++) { |
| 287 |
if (fn_pin_map[fn] == PCI_INT_NONE) |
| 288 |
continue; |
| 289 |
|
| 290 |
const size_t pin_idx = PIN2IDX(fn_pin_map[fn]); |
| 291 |
add_entry(head, PCI_DEVFN(slot, fn), fn_pin_map[fn], pin_info[pin_idx].irq); |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
static bool assign_slot(struct pci_irq_entry **head, |
| 296 |
const struct slot_irq_constraints *constraints) |
| 297 |
{ |
| 298 |
struct pin_info pin_info[PCI_INT_MAX] = {0}; |
| 299 |
enum pci_pin fn_pin_map[MAX_FNS] = {0}; |
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
if (!assign_fixed_pins(constraints, pin_info, fn_pin_map)) |
| 306 |
return false; |
| 307 |
|
| 308 |
if (!assign_fixed_pirqs(constraints, pin_info, fn_pin_map)) |
| 309 |
return false; |
| 310 |
|
| 311 |
if (!assign_direct_irqs(constraints, pin_info, fn_pin_map)) |
| 312 |
return false; |
| 313 |
|
| 314 |
if (!assign_shareable_pins(constraints, pin_info, fn_pin_map)) |
| 315 |
return false; |
| 316 |
|
| 317 |
if (!assign_pirqs(pin_info)) |
| 318 |
return false; |
| 319 |
|
| 320 |
add_slot_entries(head, constraints->slot, pin_info, fn_pin_map); |
| 321 |
return true; |
| 322 |
} |
| 323 |
|
| 324 |
static struct pci_irq_entry *cached_entries; |
| 325 |
|
| 326 |
bool assign_pci_irqs(const struct slot_irq_constraints *constraints, size_t num_slots) |
| 327 |
{ |
| 328 |
for (size_t i = 0; i < num_slots; i++) { |
| 329 |
if (!assign_slot(&cached_entries, &constraints[i])) |
| 330 |
return false; |
| 331 |
} |
| 332 |
|
| 333 |
const struct pci_irq_entry *entry = cached_entries; |
| 334 |
while (entry) { |
| 335 |
printk(BIOS_INFO, "PCI %2X.%X, %s, using IRQ #%d\n", |
| 336 |
PCI_SLOT(entry->devfn), PCI_FUNC(entry->devfn), |
| 337 |
pin_to_str(entry->pin), entry->irq); |
| 338 |
|
| 339 |
entry = entry->next; |
| 340 |
} |
| 341 |
|
| 342 |
return true; |
| 343 |
} |
| 344 |
|
| 345 |
const struct pci_irq_entry *get_cached_pci_irqs(void) |
| 346 |
{ |
| 347 |
return cached_entries; |
| 348 |
} |
| 349 |
|
| 350 |
static enum pirq irq_to_pirq(unsigned int irq) |
| 351 |
{ |
| 352 |
if (irq >= MIN_SHARED_IRQ && irq <= MAX_SHARED_IRQ) |
| 353 |
return (enum pirq)(irq - MIN_SHARED_IRQ + PIRQ_A); |
| 354 |
else |
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
return PIRQ_INVALID; |
| 361 |
} |
| 362 |
|
| 363 |
bool generate_pin_irq_map(void) |
| 364 |
{ |
| 365 |
struct slot_pin_irq_map *pin_irq_map; |
| 366 |
const uint8_t *legacy_pirq_routing; |
| 367 |
struct pic_pirq_map pirq_map = {0}; |
| 368 |
size_t map_count = 0; |
| 369 |
size_t pirq_routes; |
| 370 |
size_t i; |
| 371 |
|
| 372 |
if (!cached_entries) |
| 373 |
return false; |
| 374 |
|
| 375 |
pin_irq_map = calloc(MAX_SLOTS * PCI_INT_MAX, sizeof(struct slot_pin_irq_map)); |
| 376 |
|
| 377 |
pirq_map.type = PIRQ_GSI; |
| 378 |
legacy_pirq_routing = lpc_get_pic_pirq_routing(&pirq_routes); |
| 379 |
for (i = 0; i < PIRQ_COUNT && i < pirq_routes; i++) |
| 380 |
pirq_map.gsi[i] = legacy_pirq_routing[i]; |
| 381 |
|
| 382 |
const struct pci_irq_entry *entry = cached_entries; |
| 383 |
while (entry) { |
| 384 |
const unsigned int slot = PCI_SLOT(entry->devfn); |
| 385 |
|
| 386 |
if (is_slot_pin_assigned(pin_irq_map, map_count, slot, entry->pin)) { |
| 387 |
entry = entry->next; |
| 388 |
continue; |
| 389 |
} |
| 390 |
|
| 391 |
pin_irq_map[map_count].slot = slot; |
| 392 |
pin_irq_map[map_count].pin = entry->pin; |
| 393 |
pin_irq_map[map_count].apic_gsi = entry->irq; |
| 394 |
pin_irq_map[map_count].pic_pirq = irq_to_pirq(entry->irq); |
| 395 |
map_count++; |
| 396 |
entry = entry->next; |
| 397 |
} |
| 398 |
|
| 399 |
intel_write_pci_PRT("\\_SB.PCI0", pin_irq_map, map_count, &pirq_map); |
| 400 |
free(pin_irq_map); |
| 401 |
|
| 402 |
return true; |
| 403 |
} |
| 404 |
|
| 405 |
bool __weak is_pch_slot(unsigned int devfn) |
| 406 |
{ |
| 407 |
if (PCI_SLOT(devfn) >= MIN_PCH_SLOT) |
| 408 |
return true; |
| 409 |
return false; |
| 410 |
} |
| 411 |
|
| 412 |
bool irq_program_non_pch(void) |
| 413 |
{ |
| 414 |
const struct pci_irq_entry *entry = cached_entries; |
| 415 |
|
| 416 |
if (!entry) |
| 417 |
return false; |
| 418 |
|
| 419 |
while (entry) { |
| 420 |
if (is_pch_slot(entry->devfn)) { |
| 421 |
entry = entry->next; |
| 422 |
continue; |
| 423 |
} |
| 424 |
|
| 425 |
if (entry->irq) { |
| 426 |
pci_devfn_t dev = PCI_DEV(0, PCI_SLOT(entry->devfn), |
| 427 |
PCI_FUNC(entry->devfn)); |
| 428 |
pci_s_write_config8(dev, PCI_INTERRUPT_LINE, entry->irq); |
| 429 |
pci_s_write_config8(dev, PCI_INTERRUPT_PIN, (uint8_t)entry->pin); |
| 430 |
} |
| 431 |
|
| 432 |
entry = entry->next; |
| 433 |
} |
| 434 |
|
| 435 |
return true; |
| 436 |
} |
| 437 |
|
| 438 |
int get_pci_devfn_irq(unsigned int devfn) |
| 439 |
{ |
| 440 |
const struct pci_irq_entry *entry = cached_entries; |
| 441 |
|
| 442 |
while (entry) { |
| 443 |
if (entry->devfn == devfn) |
| 444 |
return entry->irq; |
| 445 |
|
| 446 |
entry = entry->next; |
| 447 |
} |
| 448 |
|
| 449 |
return INVALID_IRQ; |
| 450 |
} |