Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * PCIEHPRM NONACPI: PHP Resource Manager for Non-ACPI/Legacy platform |
| 3 | * |
| 4 | * Copyright (C) 1995,2001 Compaq Computer Corporation |
| 5 | * Copyright (C) 2001 Greg Kroah-Hartman (greg@kroah.com) |
| 6 | * Copyright (C) 2001 IBM Corp. |
| 7 | * Copyright (C) 2003-2004 Intel Corporation |
| 8 | * |
| 9 | * All rights reserved. |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License as published by |
| 13 | * the Free Software Foundation; either version 2 of the License, or (at |
| 14 | * your option) any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, but |
| 17 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or |
| 19 | * NON INFRINGEMENT. See the GNU General Public License for more |
| 20 | * details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License |
| 23 | * along with this program; if not, write to the Free Software |
| 24 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 25 | * |
| 26 | * Send feedback to <greg@kroah.com>, <dely.l.sy@intel.com> |
| 27 | * |
| 28 | */ |
| 29 | |
| 30 | #include <linux/config.h> |
| 31 | #include <linux/module.h> |
| 32 | #include <linux/kernel.h> |
| 33 | #include <linux/types.h> |
| 34 | #include <linux/pci.h> |
| 35 | #include <linux/init.h> |
| 36 | #include <asm/uaccess.h> |
| 37 | #ifdef CONFIG_IA64 |
| 38 | #include <asm/iosapic.h> |
| 39 | #endif |
| 40 | #include "pciehp.h" |
| 41 | #include "pciehprm.h" |
| 42 | #include "pciehprm_nonacpi.h" |
| 43 | |
| 44 | |
| 45 | void pciehprm_cleanup(void) |
| 46 | { |
| 47 | return; |
| 48 | } |
| 49 | |
| 50 | int pciehprm_print_pirt(void) |
| 51 | { |
| 52 | return 0; |
| 53 | } |
| 54 | |
| 55 | int pciehprm_get_physical_slot_number(struct controller *ctrl, u32 *sun, u8 busnum, u8 devnum) |
| 56 | { |
| 57 | |
| 58 | *sun = (u8) (ctrl->first_slot); |
| 59 | return 0; |
| 60 | } |
| 61 | |
| 62 | |
| 63 | static void print_pci_resource ( struct pci_resource *aprh) |
| 64 | { |
| 65 | struct pci_resource *res; |
| 66 | |
| 67 | for (res = aprh; res; res = res->next) |
| 68 | dbg(" base= 0x%x length= 0x%x\n", res->base, res->length); |
| 69 | } |
| 70 | |
| 71 | |
| 72 | static void phprm_dump_func_res( struct pci_func *fun) |
| 73 | { |
| 74 | struct pci_func *func = fun; |
| 75 | |
| 76 | if (func->bus_head) { |
| 77 | dbg(": BUS Resources:\n"); |
| 78 | print_pci_resource (func->bus_head); |
| 79 | } |
| 80 | if (func->io_head) { |
| 81 | dbg(": IO Resources:\n"); |
| 82 | print_pci_resource (func->io_head); |
| 83 | } |
| 84 | if (func->mem_head) { |
| 85 | dbg(": MEM Resources:\n"); |
| 86 | print_pci_resource (func->mem_head); |
| 87 | } |
| 88 | if (func->p_mem_head) { |
| 89 | dbg(": PMEM Resources:\n"); |
| 90 | print_pci_resource (func->p_mem_head); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | static int phprm_get_used_resources ( |
| 95 | struct controller *ctrl, |
| 96 | struct pci_func *func |
| 97 | ) |
| 98 | { |
| 99 | return pciehp_save_used_resources (ctrl, func, !DISABLE_CARD); |
| 100 | } |
| 101 | |
| 102 | static int phprm_delete_resource( |
| 103 | struct pci_resource **aprh, |
| 104 | ulong base, |
| 105 | ulong size) |
| 106 | { |
| 107 | struct pci_resource *res; |
| 108 | struct pci_resource *prevnode; |
| 109 | struct pci_resource *split_node; |
| 110 | ulong tbase; |
| 111 | |
| 112 | pciehp_resource_sort_and_combine(aprh); |
| 113 | |
| 114 | for (res = *aprh; res; res = res->next) { |
| 115 | if (res->base > base) |
| 116 | continue; |
| 117 | |
| 118 | if ((res->base + res->length) < (base + size)) |
| 119 | continue; |
| 120 | |
| 121 | if (res->base < base) { |
| 122 | tbase = base; |
| 123 | |
| 124 | if ((res->length - (tbase - res->base)) < size) |
| 125 | continue; |
| 126 | |
| 127 | split_node = (struct pci_resource *) kmalloc(sizeof(struct pci_resource), GFP_KERNEL); |
| 128 | if (!split_node) |
| 129 | return -ENOMEM; |
| 130 | |
| 131 | split_node->base = res->base; |
| 132 | split_node->length = tbase - res->base; |
| 133 | res->base = tbase; |
| 134 | res->length -= split_node->length; |
| 135 | |
| 136 | split_node->next = res->next; |
| 137 | res->next = split_node; |
| 138 | } |
| 139 | |
| 140 | if (res->length >= size) { |
| 141 | split_node = (struct pci_resource*) kmalloc(sizeof(struct pci_resource), GFP_KERNEL); |
| 142 | if (!split_node) |
| 143 | return -ENOMEM; |
| 144 | |
| 145 | split_node->base = res->base + size; |
| 146 | split_node->length = res->length - size; |
| 147 | res->length = size; |
| 148 | |
| 149 | split_node->next = res->next; |
| 150 | res->next = split_node; |
| 151 | } |
| 152 | |
| 153 | if (*aprh == res) { |
| 154 | *aprh = res->next; |
| 155 | } else { |
| 156 | prevnode = *aprh; |
| 157 | while (prevnode->next != res) |
| 158 | prevnode = prevnode->next; |
| 159 | |
| 160 | prevnode->next = res->next; |
| 161 | } |
| 162 | res->next = NULL; |
| 163 | kfree(res); |
| 164 | break; |
| 165 | } |
| 166 | |
| 167 | return 0; |
| 168 | } |
| 169 | |
| 170 | |
| 171 | static int phprm_delete_resources( |
| 172 | struct pci_resource **aprh, |
| 173 | struct pci_resource *this |
| 174 | ) |
| 175 | { |
| 176 | struct pci_resource *res; |
| 177 | |
| 178 | for (res = this; res; res = res->next) |
| 179 | phprm_delete_resource(aprh, res->base, res->length); |
| 180 | |
| 181 | return 0; |
| 182 | } |
| 183 | |
| 184 | |
| 185 | static int configure_existing_function( |
| 186 | struct controller *ctrl, |
| 187 | struct pci_func *func |
| 188 | ) |
| 189 | { |
| 190 | int rc; |
| 191 | |
| 192 | /* see how much resources the func has used. */ |
| 193 | rc = phprm_get_used_resources (ctrl, func); |
| 194 | |
| 195 | if (!rc) { |
| 196 | /* subtract the resources used by the func from ctrl resources */ |
| 197 | rc = phprm_delete_resources (&ctrl->bus_head, func->bus_head); |
| 198 | rc |= phprm_delete_resources (&ctrl->io_head, func->io_head); |
| 199 | rc |= phprm_delete_resources (&ctrl->mem_head, func->mem_head); |
| 200 | rc |= phprm_delete_resources (&ctrl->p_mem_head, func->p_mem_head); |
| 201 | if (rc) |
| 202 | warn("aCEF: cannot del used resources\n"); |
| 203 | } else |
| 204 | err("aCEF: cannot get used resources\n"); |
| 205 | |
| 206 | return rc; |
| 207 | } |
| 208 | |
| 209 | static int pciehprm_delete_resource( |
| 210 | struct pci_resource **aprh, |
| 211 | ulong base, |
| 212 | ulong size) |
| 213 | { |
| 214 | struct pci_resource *res; |
| 215 | struct pci_resource *prevnode; |
| 216 | struct pci_resource *split_node; |
| 217 | ulong tbase; |
| 218 | |
| 219 | pciehp_resource_sort_and_combine(aprh); |
| 220 | |
| 221 | for (res = *aprh; res; res = res->next) { |
| 222 | if (res->base > base) |
| 223 | continue; |
| 224 | |
| 225 | if ((res->base + res->length) < (base + size)) |
| 226 | continue; |
| 227 | |
| 228 | if (res->base < base) { |
| 229 | tbase = base; |
| 230 | |
| 231 | if ((res->length - (tbase - res->base)) < size) |
| 232 | continue; |
| 233 | |
| 234 | split_node = (struct pci_resource *) kmalloc(sizeof(struct pci_resource), GFP_KERNEL); |
| 235 | if (!split_node) |
| 236 | return -ENOMEM; |
| 237 | |
| 238 | split_node->base = res->base; |
| 239 | split_node->length = tbase - res->base; |
| 240 | res->base = tbase; |
| 241 | res->length -= split_node->length; |
| 242 | |
| 243 | split_node->next = res->next; |
| 244 | res->next = split_node; |
| 245 | } |
| 246 | |
| 247 | if (res->length >= size) { |
| 248 | split_node = (struct pci_resource*) kmalloc(sizeof(struct pci_resource), GFP_KERNEL); |
| 249 | if (!split_node) |
| 250 | return -ENOMEM; |
| 251 | |
| 252 | split_node->base = res->base + size; |
| 253 | split_node->length = res->length - size; |
| 254 | res->length = size; |
| 255 | |
| 256 | split_node->next = res->next; |
| 257 | res->next = split_node; |
| 258 | } |
| 259 | |
| 260 | if (*aprh == res) { |
| 261 | *aprh = res->next; |
| 262 | } else { |
| 263 | prevnode = *aprh; |
| 264 | while (prevnode->next != res) |
| 265 | prevnode = prevnode->next; |
| 266 | |
| 267 | prevnode->next = res->next; |
| 268 | } |
| 269 | res->next = NULL; |
| 270 | kfree(res); |
| 271 | break; |
| 272 | } |
| 273 | |
| 274 | return 0; |
| 275 | } |
| 276 | |
| 277 | static int bind_pci_resources_to_slots ( struct controller *ctrl) |
| 278 | { |
| 279 | struct pci_func *func, new_func; |
| 280 | int busn = ctrl->slot_bus; |
| 281 | int devn, funn; |
| 282 | u32 vid; |
| 283 | |
| 284 | for (devn = 0; devn < 32; devn++) { |
| 285 | for (funn = 0; funn < 8; funn++) { |
| 286 | /* |
| 287 | if (devn == ctrl->device && funn == ctrl->function) |
| 288 | continue; |
| 289 | */ |
| 290 | /* find out if this entry is for an occupied slot */ |
| 291 | vid = 0xFFFFFFFF; |
| 292 | |
| 293 | pci_bus_read_config_dword(ctrl->pci_dev->subordinate, PCI_DEVFN(devn, funn), PCI_VENDOR_ID, &vid); |
| 294 | |
| 295 | if (vid != 0xFFFFFFFF) { |
| 296 | dbg("%s: vid = %x bus %x dev %x fun %x\n", __FUNCTION__, |
| 297 | vid, busn, devn, funn); |
| 298 | func = pciehp_slot_find(busn, devn, funn); |
| 299 | dbg("%s: func = %p\n", __FUNCTION__,func); |
| 300 | if (!func) { |
| 301 | memset(&new_func, 0, sizeof(struct pci_func)); |
| 302 | new_func.bus = busn; |
| 303 | new_func.device = devn; |
| 304 | new_func.function = funn; |
| 305 | new_func.is_a_board = 1; |
| 306 | configure_existing_function(ctrl, &new_func); |
| 307 | phprm_dump_func_res(&new_func); |
| 308 | } else { |
| 309 | configure_existing_function(ctrl, func); |
| 310 | phprm_dump_func_res(func); |
| 311 | } |
| 312 | dbg("aCCF:existing PCI 0x%x Func ResourceDump\n", ctrl->bus); |
| 313 | } |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | return 0; |
| 318 | } |
| 319 | |
| 320 | static void phprm_dump_ctrl_res( struct controller *ctlr) |
| 321 | { |
| 322 | struct controller *ctrl = ctlr; |
| 323 | |
| 324 | if (ctrl->bus_head) { |
| 325 | dbg(": BUS Resources:\n"); |
| 326 | print_pci_resource (ctrl->bus_head); |
| 327 | } |
| 328 | if (ctrl->io_head) { |
| 329 | dbg(": IO Resources:\n"); |
| 330 | print_pci_resource (ctrl->io_head); |
| 331 | } |
| 332 | if (ctrl->mem_head) { |
| 333 | dbg(": MEM Resources:\n"); |
| 334 | print_pci_resource (ctrl->mem_head); |
| 335 | } |
| 336 | if (ctrl->p_mem_head) { |
| 337 | dbg(": PMEM Resources:\n"); |
| 338 | print_pci_resource (ctrl->p_mem_head); |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | /* |
| 343 | * phprm_find_available_resources |
| 344 | * |
| 345 | * Finds available memory, IO, and IRQ resources for programming |
| 346 | * devices which may be added to the system |
| 347 | * this function is for hot plug ADD! |
| 348 | * |
| 349 | * returns 0 if success |
| 350 | */ |
| 351 | int pciehprm_find_available_resources(struct controller *ctrl) |
| 352 | { |
| 353 | struct pci_func func; |
| 354 | u32 rc; |
| 355 | |
| 356 | memset(&func, 0, sizeof(struct pci_func)); |
| 357 | |
| 358 | func.bus = ctrl->bus; |
| 359 | func.device = ctrl->device; |
| 360 | func.function = ctrl->function; |
| 361 | func.is_a_board = 1; |
| 362 | |
| 363 | /* Get resources for this PCI bridge */ |
| 364 | rc = pciehp_save_used_resources (ctrl, &func, !DISABLE_CARD); |
| 365 | dbg("%s: pciehp_save_used_resources rc = %d\n", __FUNCTION__, rc); |
| 366 | |
| 367 | if (func.mem_head) |
| 368 | func.mem_head->next = ctrl->mem_head; |
| 369 | ctrl->mem_head = func.mem_head; |
| 370 | |
| 371 | if (func.p_mem_head) |
| 372 | func.p_mem_head->next = ctrl->p_mem_head; |
| 373 | ctrl->p_mem_head = func.p_mem_head; |
| 374 | |
| 375 | if (func.io_head) |
| 376 | func.io_head->next = ctrl->io_head; |
| 377 | ctrl->io_head = func.io_head; |
| 378 | |
| 379 | if(func.bus_head) |
| 380 | func.bus_head->next = ctrl->bus_head; |
| 381 | ctrl->bus_head = func.bus_head; |
| 382 | |
| 383 | if (ctrl->bus_head) |
| 384 | pciehprm_delete_resource(&ctrl->bus_head, ctrl->pci_dev->subordinate->number, 1); |
| 385 | |
| 386 | dbg("%s:pre-Bind PCI 0x%x Ctrl Resource Dump\n", __FUNCTION__, ctrl->bus); |
| 387 | phprm_dump_ctrl_res(ctrl); |
| 388 | |
| 389 | dbg("%s: before bind_pci_resources_to slots\n", __FUNCTION__); |
| 390 | |
| 391 | bind_pci_resources_to_slots (ctrl); |
| 392 | |
| 393 | dbg("%s:post-Bind PCI 0x%x Ctrl Resource Dump\n", __FUNCTION__, ctrl->bus); |
| 394 | phprm_dump_ctrl_res(ctrl); |
| 395 | |
| 396 | return (rc); |
| 397 | } |
| 398 | |
| 399 | int pciehprm_set_hpp( |
| 400 | struct controller *ctrl, |
| 401 | struct pci_func *func, |
| 402 | u8 card_type) |
| 403 | { |
| 404 | u32 rc; |
| 405 | u8 temp_byte; |
| 406 | struct pci_bus lpci_bus, *pci_bus; |
| 407 | unsigned int devfn; |
| 408 | memcpy(&lpci_bus, ctrl->pci_bus, sizeof(lpci_bus)); |
| 409 | pci_bus = &lpci_bus; |
| 410 | pci_bus->number = func->bus; |
| 411 | devfn = PCI_DEVFN(func->device, func->function); |
| 412 | |
| 413 | temp_byte = 0x40; /* hard coded value for LT */ |
| 414 | if (card_type == PCI_HEADER_TYPE_BRIDGE) { |
| 415 | /* set subordinate Latency Timer */ |
| 416 | rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_SEC_LATENCY_TIMER, temp_byte); |
| 417 | |
| 418 | if (rc) { |
| 419 | dbg("%s: set secondary LT error. b:d:f(%02x:%02x:%02x)\n", __FUNCTION__, |
| 420 | func->bus, func->device, func->function); |
| 421 | return rc; |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | /* set base Latency Timer */ |
| 426 | rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_LATENCY_TIMER, temp_byte); |
| 427 | |
| 428 | if (rc) { |
| 429 | dbg("%s: set LT error. b:d:f(%02x:%02x:%02x)\n", __FUNCTION__, func->bus, func->device, func->function); |
| 430 | return rc; |
| 431 | } |
| 432 | |
| 433 | /* set Cache Line size */ |
| 434 | temp_byte = 0x08; /* hard coded value for CLS */ |
| 435 | |
| 436 | rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_CACHE_LINE_SIZE, temp_byte); |
| 437 | |
| 438 | if (rc) { |
| 439 | dbg("%s: set CLS error. b:d:f(%02x:%02x:%02x)\n", __FUNCTION__, func->bus, func->device, func->function); |
| 440 | } |
| 441 | |
| 442 | /* set enable_perr */ |
| 443 | /* set enable_serr */ |
| 444 | |
| 445 | return rc; |
| 446 | } |
| 447 | |
| 448 | void pciehprm_enable_card( |
| 449 | struct controller *ctrl, |
| 450 | struct pci_func *func, |
| 451 | u8 card_type) |
| 452 | { |
| 453 | u16 command, bcommand; |
| 454 | struct pci_bus lpci_bus, *pci_bus; |
| 455 | unsigned int devfn; |
| 456 | int rc; |
| 457 | |
| 458 | memcpy(&lpci_bus, ctrl->pci_bus, sizeof(lpci_bus)); |
| 459 | pci_bus = &lpci_bus; |
| 460 | pci_bus->number = func->bus; |
| 461 | devfn = PCI_DEVFN(func->device, func->function); |
| 462 | |
| 463 | rc = pci_bus_read_config_word(pci_bus, devfn, PCI_COMMAND, &command); |
| 464 | |
| 465 | command |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR |
| 466 | | PCI_COMMAND_MASTER | PCI_COMMAND_INVALIDATE |
| 467 | | PCI_COMMAND_IO | PCI_COMMAND_MEMORY; |
| 468 | |
| 469 | rc = pci_bus_write_config_word(pci_bus, devfn, PCI_COMMAND, command); |
| 470 | |
| 471 | if (card_type == PCI_HEADER_TYPE_BRIDGE) { |
| 472 | |
| 473 | rc = pci_bus_read_config_word(pci_bus, devfn, PCI_BRIDGE_CONTROL, &bcommand); |
| 474 | |
| 475 | bcommand |= PCI_BRIDGE_CTL_PARITY | PCI_BRIDGE_CTL_SERR |
| 476 | | PCI_BRIDGE_CTL_NO_ISA; |
| 477 | |
| 478 | rc = pci_bus_write_config_word(pci_bus, devfn, PCI_BRIDGE_CONTROL, bcommand); |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | static int legacy_pciehprm_init_pci(void) |
| 483 | { |
| 484 | return 0; |
| 485 | } |
| 486 | |
| 487 | int pciehprm_init(enum php_ctlr_type ctrl_type) |
| 488 | { |
| 489 | int retval; |
| 490 | |
| 491 | switch (ctrl_type) { |
| 492 | case PCI: |
| 493 | retval = legacy_pciehprm_init_pci(); |
| 494 | break; |
| 495 | default: |
| 496 | retval = -ENODEV; |
| 497 | break; |
| 498 | } |
| 499 | |
| 500 | return retval; |
| 501 | } |