2 * PCIEHPRM NONACPI: PHP Resource Manager for Non-ACPI/Legacy platform
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
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.
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
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.
26 * Send feedback to <greg@kroah.com>, <kristen.c.accardi@intel.com>
30 #include <linux/config.h>
31 #include <linux/module.h>
32 #include <linux/kernel.h>
33 #include <linux/types.h>
34 #include <linux/sched.h>
35 #include <linux/pci.h>
36 #include <linux/init.h>
37 #include <linux/slab.h>
39 #include <asm/uaccess.h>
41 #include <asm/iosapic.h>
46 #include "pciehprm_nonacpi.h"
49 void pciehprm_cleanup(void)
54 int pciehprm_print_pirt(void)
59 int pciehprm_get_physical_slot_number(struct controller *ctrl, u32 *sun, u8 busnum, u8 devnum)
62 *sun = (u8) (ctrl->first_slot);
67 static void print_pci_resource ( struct pci_resource *aprh)
69 struct pci_resource *res;
71 for (res = aprh; res; res = res->next)
72 dbg(" base= 0x%x length= 0x%x\n", res->base, res->length);
76 static void phprm_dump_func_res( struct pci_func *fun)
78 struct pci_func *func = fun;
81 dbg(": BUS Resources:\n");
82 print_pci_resource (func->bus_head);
85 dbg(": IO Resources:\n");
86 print_pci_resource (func->io_head);
89 dbg(": MEM Resources:\n");
90 print_pci_resource (func->mem_head);
92 if (func->p_mem_head) {
93 dbg(": PMEM Resources:\n");
94 print_pci_resource (func->p_mem_head);
98 static int phprm_get_used_resources (
99 struct controller *ctrl,
100 struct pci_func *func
103 return pciehp_save_used_resources (ctrl, func, !DISABLE_CARD);
106 static int phprm_delete_resource(
107 struct pci_resource **aprh,
111 struct pci_resource *res;
112 struct pci_resource *prevnode;
113 struct pci_resource *split_node;
116 pciehp_resource_sort_and_combine(aprh);
118 for (res = *aprh; res; res = res->next) {
119 if (res->base > base)
122 if ((res->base + res->length) < (base + size))
125 if (res->base < base) {
128 if ((res->length - (tbase - res->base)) < size)
131 split_node = (struct pci_resource *) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
135 split_node->base = res->base;
136 split_node->length = tbase - res->base;
138 res->length -= split_node->length;
140 split_node->next = res->next;
141 res->next = split_node;
144 if (res->length >= size) {
145 split_node = (struct pci_resource*) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
149 split_node->base = res->base + size;
150 split_node->length = res->length - size;
153 split_node->next = res->next;
154 res->next = split_node;
161 while (prevnode->next != res)
162 prevnode = prevnode->next;
164 prevnode->next = res->next;
175 static int phprm_delete_resources(
176 struct pci_resource **aprh,
177 struct pci_resource *this
180 struct pci_resource *res;
182 for (res = this; res; res = res->next)
183 phprm_delete_resource(aprh, res->base, res->length);
189 static int configure_existing_function(
190 struct controller *ctrl,
191 struct pci_func *func
196 /* see how much resources the func has used. */
197 rc = phprm_get_used_resources (ctrl, func);
200 /* subtract the resources used by the func from ctrl resources */
201 rc = phprm_delete_resources (&ctrl->bus_head, func->bus_head);
202 rc |= phprm_delete_resources (&ctrl->io_head, func->io_head);
203 rc |= phprm_delete_resources (&ctrl->mem_head, func->mem_head);
204 rc |= phprm_delete_resources (&ctrl->p_mem_head, func->p_mem_head);
206 warn("aCEF: cannot del used resources\n");
208 err("aCEF: cannot get used resources\n");
213 static int pciehprm_delete_resource(
214 struct pci_resource **aprh,
218 struct pci_resource *res;
219 struct pci_resource *prevnode;
220 struct pci_resource *split_node;
223 pciehp_resource_sort_and_combine(aprh);
225 for (res = *aprh; res; res = res->next) {
226 if (res->base > base)
229 if ((res->base + res->length) < (base + size))
232 if (res->base < base) {
235 if ((res->length - (tbase - res->base)) < size)
238 split_node = (struct pci_resource *) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
242 split_node->base = res->base;
243 split_node->length = tbase - res->base;
245 res->length -= split_node->length;
247 split_node->next = res->next;
248 res->next = split_node;
251 if (res->length >= size) {
252 split_node = (struct pci_resource*) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
256 split_node->base = res->base + size;
257 split_node->length = res->length - size;
260 split_node->next = res->next;
261 res->next = split_node;
268 while (prevnode->next != res)
269 prevnode = prevnode->next;
271 prevnode->next = res->next;
281 static int bind_pci_resources_to_slots ( struct controller *ctrl)
283 struct pci_func *func, new_func;
284 int busn = ctrl->slot_bus;
288 for (devn = 0; devn < 32; devn++) {
289 for (funn = 0; funn < 8; funn++) {
291 if (devn == ctrl->device && funn == ctrl->function)
294 /* find out if this entry is for an occupied slot */
297 pci_bus_read_config_dword(ctrl->pci_dev->subordinate, PCI_DEVFN(devn, funn), PCI_VENDOR_ID, &vid);
299 if (vid != 0xFFFFFFFF) {
300 dbg("%s: vid = %x bus %x dev %x fun %x\n", __FUNCTION__,
301 vid, busn, devn, funn);
302 func = pciehp_slot_find(busn, devn, funn);
303 dbg("%s: func = %p\n", __FUNCTION__,func);
305 memset(&new_func, 0, sizeof(struct pci_func));
307 new_func.device = devn;
308 new_func.function = funn;
309 new_func.is_a_board = 1;
310 configure_existing_function(ctrl, &new_func);
311 phprm_dump_func_res(&new_func);
313 configure_existing_function(ctrl, func);
314 phprm_dump_func_res(func);
316 dbg("aCCF:existing PCI 0x%x Func ResourceDump\n", ctrl->bus);
324 static void phprm_dump_ctrl_res( struct controller *ctlr)
326 struct controller *ctrl = ctlr;
328 if (ctrl->bus_head) {
329 dbg(": BUS Resources:\n");
330 print_pci_resource (ctrl->bus_head);
333 dbg(": IO Resources:\n");
334 print_pci_resource (ctrl->io_head);
336 if (ctrl->mem_head) {
337 dbg(": MEM Resources:\n");
338 print_pci_resource (ctrl->mem_head);
340 if (ctrl->p_mem_head) {
341 dbg(": PMEM Resources:\n");
342 print_pci_resource (ctrl->p_mem_head);
347 * phprm_find_available_resources
349 * Finds available memory, IO, and IRQ resources for programming
350 * devices which may be added to the system
351 * this function is for hot plug ADD!
353 * returns 0 if success
355 int pciehprm_find_available_resources(struct controller *ctrl)
357 struct pci_func func;
360 memset(&func, 0, sizeof(struct pci_func));
362 func.bus = ctrl->bus;
363 func.device = ctrl->device;
364 func.function = ctrl->function;
367 /* Get resources for this PCI bridge */
368 rc = pciehp_save_used_resources (ctrl, &func, !DISABLE_CARD);
369 dbg("%s: pciehp_save_used_resources rc = %d\n", __FUNCTION__, rc);
372 func.mem_head->next = ctrl->mem_head;
373 ctrl->mem_head = func.mem_head;
376 func.p_mem_head->next = ctrl->p_mem_head;
377 ctrl->p_mem_head = func.p_mem_head;
380 func.io_head->next = ctrl->io_head;
381 ctrl->io_head = func.io_head;
384 func.bus_head->next = ctrl->bus_head;
385 ctrl->bus_head = func.bus_head;
388 pciehprm_delete_resource(&ctrl->bus_head, ctrl->pci_dev->subordinate->number, 1);
390 dbg("%s:pre-Bind PCI 0x%x Ctrl Resource Dump\n", __FUNCTION__, ctrl->bus);
391 phprm_dump_ctrl_res(ctrl);
393 dbg("%s: before bind_pci_resources_to slots\n", __FUNCTION__);
395 bind_pci_resources_to_slots (ctrl);
397 dbg("%s:post-Bind PCI 0x%x Ctrl Resource Dump\n", __FUNCTION__, ctrl->bus);
398 phprm_dump_ctrl_res(ctrl);
403 int pciehprm_set_hpp(
404 struct controller *ctrl,
405 struct pci_func *func,
410 struct pci_bus lpci_bus, *pci_bus;
412 memcpy(&lpci_bus, ctrl->pci_bus, sizeof(lpci_bus));
414 pci_bus->number = func->bus;
415 devfn = PCI_DEVFN(func->device, func->function);
417 temp_byte = 0x40; /* hard coded value for LT */
418 if (card_type == PCI_HEADER_TYPE_BRIDGE) {
419 /* set subordinate Latency Timer */
420 rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_SEC_LATENCY_TIMER, temp_byte);
423 dbg("%s: set secondary LT error. b:d:f(%02x:%02x:%02x)\n", __FUNCTION__,
424 func->bus, func->device, func->function);
429 /* set base Latency Timer */
430 rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_LATENCY_TIMER, temp_byte);
433 dbg("%s: set LT error. b:d:f(%02x:%02x:%02x)\n", __FUNCTION__, func->bus, func->device, func->function);
437 /* set Cache Line size */
438 temp_byte = 0x08; /* hard coded value for CLS */
440 rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_CACHE_LINE_SIZE, temp_byte);
443 dbg("%s: set CLS error. b:d:f(%02x:%02x:%02x)\n", __FUNCTION__, func->bus, func->device, func->function);
446 /* set enable_perr */
447 /* set enable_serr */
452 void pciehprm_enable_card(
453 struct controller *ctrl,
454 struct pci_func *func,
457 u16 command, bcommand;
458 struct pci_bus lpci_bus, *pci_bus;
462 memcpy(&lpci_bus, ctrl->pci_bus, sizeof(lpci_bus));
464 pci_bus->number = func->bus;
465 devfn = PCI_DEVFN(func->device, func->function);
467 rc = pci_bus_read_config_word(pci_bus, devfn, PCI_COMMAND, &command);
469 command |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR
470 | PCI_COMMAND_MASTER | PCI_COMMAND_INVALIDATE
471 | PCI_COMMAND_IO | PCI_COMMAND_MEMORY;
473 rc = pci_bus_write_config_word(pci_bus, devfn, PCI_COMMAND, command);
475 if (card_type == PCI_HEADER_TYPE_BRIDGE) {
477 rc = pci_bus_read_config_word(pci_bus, devfn, PCI_BRIDGE_CONTROL, &bcommand);
479 bcommand |= PCI_BRIDGE_CTL_PARITY | PCI_BRIDGE_CTL_SERR
480 | PCI_BRIDGE_CTL_NO_ISA;
482 rc = pci_bus_write_config_word(pci_bus, devfn, PCI_BRIDGE_CONTROL, bcommand);
486 static int legacy_pciehprm_init_pci(void)
491 int pciehprm_init(enum php_ctlr_type ctrl_type)
497 retval = legacy_pciehprm_init_pci();