2 * SHPCHPRM 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>, <dely.l.sy@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/pci.h>
35 #include <linux/init.h>
36 #include <asm/uaccess.h>
38 #include <asm/iosapic.h>
42 #include "shpchprm_nonacpi.h"
44 void shpchprm_cleanup(void)
49 int shpchprm_print_pirt(void)
54 void * shpchprm_get_slot(struct slot *slot)
59 int shpchprm_get_physical_slot_number(struct controller *ctrl, u32 *sun, u8 busnum, u8 devnum)
61 int offset = devnum - ctrl->slot_device_offset;
63 dbg("%s: ctrl->slot_num_inc %d, offset %d\n", __FUNCTION__, ctrl->slot_num_inc, offset);
64 *sun = (u8) (ctrl->first_slot + ctrl->slot_num_inc * offset);
68 static void print_pci_resource ( struct pci_resource *aprh)
70 struct pci_resource *res;
72 for (res = aprh; res; res = res->next)
73 dbg(" base= 0x%x length= 0x%x\n", res->base, res->length);
77 static void phprm_dump_func_res( struct pci_func *fun)
79 struct pci_func *func = fun;
82 dbg(": BUS Resources:\n");
83 print_pci_resource (func->bus_head);
86 dbg(": IO Resources:\n");
87 print_pci_resource (func->io_head);
90 dbg(": MEM Resources:\n");
91 print_pci_resource (func->mem_head);
93 if (func->p_mem_head) {
94 dbg(": PMEM Resources:\n");
95 print_pci_resource (func->p_mem_head);
99 static int phprm_get_used_resources (
100 struct controller *ctrl,
101 struct pci_func *func
104 return shpchp_save_used_resources (ctrl, func, !DISABLE_CARD);
107 static int phprm_delete_resource(
108 struct pci_resource **aprh,
112 struct pci_resource *res;
113 struct pci_resource *prevnode;
114 struct pci_resource *split_node;
117 shpchp_resource_sort_and_combine(aprh);
119 for (res = *aprh; res; res = res->next) {
120 if (res->base > base)
123 if ((res->base + res->length) < (base + size))
126 if (res->base < base) {
129 if ((res->length - (tbase - res->base)) < size)
132 split_node = (struct pci_resource *) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
136 split_node->base = res->base;
137 split_node->length = tbase - res->base;
139 res->length -= split_node->length;
141 split_node->next = res->next;
142 res->next = split_node;
145 if (res->length >= size) {
146 split_node = (struct pci_resource*) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
150 split_node->base = res->base + size;
151 split_node->length = res->length - size;
154 split_node->next = res->next;
155 res->next = split_node;
162 while (prevnode->next != res)
163 prevnode = prevnode->next;
165 prevnode->next = res->next;
176 static int phprm_delete_resources(
177 struct pci_resource **aprh,
178 struct pci_resource *this
181 struct pci_resource *res;
183 for (res = this; res; res = res->next)
184 phprm_delete_resource(aprh, res->base, res->length);
190 static int configure_existing_function(
191 struct controller *ctrl,
192 struct pci_func *func
197 /* see how much resources the func has used. */
198 rc = phprm_get_used_resources (ctrl, func);
201 /* subtract the resources used by the func from ctrl resources */
202 rc = phprm_delete_resources (&ctrl->bus_head, func->bus_head);
203 rc |= phprm_delete_resources (&ctrl->io_head, func->io_head);
204 rc |= phprm_delete_resources (&ctrl->mem_head, func->mem_head);
205 rc |= phprm_delete_resources (&ctrl->p_mem_head, func->p_mem_head);
207 warn("aCEF: cannot del used resources\n");
209 err("aCEF: cannot get used resources\n");
214 static int bind_pci_resources_to_slots ( struct controller *ctrl)
216 struct pci_func *func, new_func;
217 int busn = ctrl->slot_bus;
221 for (devn = 0; devn < 32; devn++) {
222 for (funn = 0; funn < 8; funn++) {
223 /* if (devn == ctrl->device && funn == ctrl->function)
226 /* find out if this entry is for an occupied slot */
229 pci_bus_read_config_dword(ctrl->pci_dev->subordinate, PCI_DEVFN(devn, funn), PCI_VENDOR_ID, &vid);
231 if (vid != 0xFFFFFFFF) {
232 func = shpchp_slot_find(busn, devn, funn);
234 memset(&new_func, 0, sizeof(struct pci_func));
236 new_func.device = devn;
237 new_func.function = funn;
238 new_func.is_a_board = 1;
239 configure_existing_function(ctrl, &new_func);
240 phprm_dump_func_res(&new_func);
242 configure_existing_function(ctrl, func);
243 phprm_dump_func_res(func);
245 dbg("aCCF:existing PCI 0x%x Func ResourceDump\n", ctrl->bus);
253 static void phprm_dump_ctrl_res( struct controller *ctlr)
255 struct controller *ctrl = ctlr;
257 if (ctrl->bus_head) {
258 dbg(": BUS Resources:\n");
259 print_pci_resource (ctrl->bus_head);
262 dbg(": IO Resources:\n");
263 print_pci_resource (ctrl->io_head);
265 if (ctrl->mem_head) {
266 dbg(": MEM Resources:\n");
267 print_pci_resource (ctrl->mem_head);
269 if (ctrl->p_mem_head) {
270 dbg(": PMEM Resources:\n");
271 print_pci_resource (ctrl->p_mem_head);
276 * phprm_find_available_resources
278 * Finds available memory, IO, and IRQ resources for programming
279 * devices which may be added to the system
280 * this function is for hot plug ADD!
282 * returns 0 if success
284 int shpchprm_find_available_resources(struct controller *ctrl)
286 struct pci_func func;
289 memset(&func, 0, sizeof(struct pci_func));
291 func.bus = ctrl->bus;
292 func.device = ctrl->device;
293 func.function = ctrl->function;
296 /* Get resources for this PCI bridge */
297 rc = shpchp_save_used_resources (ctrl, &func, !DISABLE_CARD);
298 dbg("%s: shpchp_save_used_resources rc = %d\n", __FUNCTION__, rc);
301 func.mem_head->next = ctrl->mem_head;
302 ctrl->mem_head = func.mem_head;
305 func.p_mem_head->next = ctrl->p_mem_head;
306 ctrl->p_mem_head = func.p_mem_head;
309 func.io_head->next = ctrl->io_head;
310 ctrl->io_head = func.io_head;
313 func.bus_head->next = ctrl->bus_head;
314 ctrl->bus_head = func.bus_head;
316 phprm_delete_resource(&ctrl->bus_head, ctrl->pci_dev->subordinate->number, 1);
318 dbg("%s:pre-Bind PCI 0x%x Ctrl Resource Dump\n", __FUNCTION__, ctrl->bus);
319 phprm_dump_ctrl_res(ctrl);
321 bind_pci_resources_to_slots (ctrl);
323 dbg("%s:post-Bind PCI 0x%x Ctrl Resource Dump\n", __FUNCTION__, ctrl->bus);
324 phprm_dump_ctrl_res(ctrl);
327 /* If all of the following fail, we don't have any resources for hot plug add */
329 rc &= shpchp_resource_sort_and_combine(&(ctrl->mem_head));
330 rc &= shpchp_resource_sort_and_combine(&(ctrl->p_mem_head));
331 rc &= shpchp_resource_sort_and_combine(&(ctrl->io_head));
332 rc &= shpchp_resource_sort_and_combine(&(ctrl->bus_head));
337 int shpchprm_set_hpp(
338 struct controller *ctrl,
339 struct pci_func *func,
344 struct pci_bus lpci_bus, *pci_bus;
346 memcpy(&lpci_bus, ctrl->pci_bus, sizeof(lpci_bus));
348 pci_bus->number = func->bus;
349 devfn = PCI_DEVFN(func->device, func->function);
351 temp_byte = 0x40; /* hard coded value for LT */
352 if (card_type == PCI_HEADER_TYPE_BRIDGE) {
353 /* set subordinate Latency Timer */
354 rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_SEC_LATENCY_TIMER, temp_byte);
357 dbg("%s: set secondary LT error. b:d:f(%02x:%02x:%02x)\n", __FUNCTION__, func->bus, func->device, func->function);
362 /* set base Latency Timer */
363 rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_LATENCY_TIMER, temp_byte);
366 dbg("%s: set LT error. b:d:f(%02x:%02x:%02x)\n", __FUNCTION__, func->bus, func->device, func->function);
370 /* set Cache Line size */
371 temp_byte = 0x08; /* hard coded value for CLS */
373 rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_CACHE_LINE_SIZE, temp_byte);
376 dbg("%s: set CLS error. b:d:f(%02x:%02x:%02x)\n", __FUNCTION__, func->bus, func->device, func->function);
379 /* set enable_perr */
380 /* set enable_serr */
385 void shpchprm_enable_card(
386 struct controller *ctrl,
387 struct pci_func *func,
390 u16 command, bcommand;
391 struct pci_bus lpci_bus, *pci_bus;
395 memcpy(&lpci_bus, ctrl->pci_bus, sizeof(lpci_bus));
397 pci_bus->number = func->bus;
398 devfn = PCI_DEVFN(func->device, func->function);
400 rc = pci_bus_read_config_word(pci_bus, devfn, PCI_COMMAND, &command);
402 command |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR
403 | PCI_COMMAND_MASTER | PCI_COMMAND_INVALIDATE
404 | PCI_COMMAND_IO | PCI_COMMAND_MEMORY;
406 rc = pci_bus_write_config_word(pci_bus, devfn, PCI_COMMAND, command);
408 if (card_type == PCI_HEADER_TYPE_BRIDGE) {
410 rc = pci_bus_read_config_word(pci_bus, devfn, PCI_BRIDGE_CONTROL, &bcommand);
412 bcommand |= PCI_BRIDGE_CTL_PARITY | PCI_BRIDGE_CTL_SERR
413 | PCI_BRIDGE_CTL_NO_ISA;
415 rc = pci_bus_write_config_word(pci_bus, devfn, PCI_BRIDGE_CONTROL, bcommand);
419 static int legacy_shpchprm_init_pci(void)
424 int shpchprm_init(enum php_ctlr_type ctrl_type)
430 retval = legacy_shpchprm_init_pci();