ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / pci / hotplug / pciehprm_nonacpi.c
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 void * pciehprm_get_slot(struct slot *slot)
56 {
57         return NULL;
58 }
59
60 int pciehprm_get_physical_slot_number(struct controller *ctrl, u32 *sun, u8 busnum, u8 devnum)
61 {
62
63         *sun = (u8) (ctrl->first_slot);
64         return 0;
65 }
66
67
68 static void print_pci_resource ( struct pci_resource    *aprh)
69 {
70         struct pci_resource     *res;
71
72         for (res = aprh; res; res = res->next)
73                 dbg("        base= 0x%x length= 0x%x\n", res->base, res->length);
74 }
75
76
77 static void phprm_dump_func_res( struct pci_func *fun)
78 {
79         struct pci_func *func = fun;
80
81         if (func->bus_head) {
82                 dbg(":    BUS Resources:\n");
83                 print_pci_resource (func->bus_head);
84         }
85         if (func->io_head) {
86                 dbg(":    IO Resources:\n");
87                 print_pci_resource (func->io_head);
88         }
89         if (func->mem_head) {
90                 dbg(":    MEM Resources:\n");
91                 print_pci_resource (func->mem_head);
92         }
93         if (func->p_mem_head) {
94                 dbg(":    PMEM Resources:\n");
95                 print_pci_resource (func->p_mem_head);
96         }
97 }
98
99 static int phprm_get_used_resources (
100         struct controller *ctrl,
101         struct pci_func *func
102         )
103 {
104         return pciehp_save_used_resources (ctrl, func, !DISABLE_CARD);
105 }
106
107 static int phprm_delete_resource(
108         struct pci_resource **aprh,
109         ulong base,
110         ulong size)
111 {
112         struct pci_resource *res;
113         struct pci_resource *prevnode;
114         struct pci_resource *split_node;
115         ulong tbase;
116
117         pciehp_resource_sort_and_combine(aprh);
118
119         for (res = *aprh; res; res = res->next) {
120                 if (res->base > base)
121                         continue;
122
123                 if ((res->base + res->length) < (base + size))
124                         continue;
125
126                 if (res->base < base) {
127                         tbase = base;
128
129                         if ((res->length - (tbase - res->base)) < size)
130                                 continue;
131
132                         split_node = (struct pci_resource *) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
133                         if (!split_node)
134                                 return -ENOMEM;
135
136                         split_node->base = res->base;
137                         split_node->length = tbase - res->base;
138                         res->base = tbase;
139                         res->length -= split_node->length;
140
141                         split_node->next = res->next;
142                         res->next = split_node;
143                 }
144
145                 if (res->length >= size) {
146                         split_node = (struct pci_resource*) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
147                         if (!split_node)
148                                 return -ENOMEM;
149
150                         split_node->base = res->base + size;
151                         split_node->length = res->length - size;
152                         res->length = size;
153
154                         split_node->next = res->next;
155                         res->next = split_node;
156                 }
157
158                 if (*aprh == res) {
159                         *aprh = res->next;
160                 } else {
161                         prevnode = *aprh;
162                         while (prevnode->next != res)
163                                 prevnode = prevnode->next;
164
165                         prevnode->next = res->next;
166                 }
167                 res->next = NULL;
168                 kfree(res);
169                 break;
170         }
171
172         return 0;
173 }
174
175
176 static int phprm_delete_resources(
177         struct pci_resource **aprh,
178         struct pci_resource *this
179         )
180 {
181         struct pci_resource *res;
182
183         for (res = this; res; res = res->next)
184                 phprm_delete_resource(aprh, res->base, res->length);
185
186         return 0;
187 }
188
189
190 static int configure_existing_function(
191         struct controller *ctrl,
192         struct pci_func *func
193         )
194 {
195         int rc;
196
197         /* see how much resources the func has used. */
198         rc = phprm_get_used_resources (ctrl, func);
199
200         if (!rc) {
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);
206                 if (rc)
207                         warn("aCEF: cannot del used resources\n");
208         } else
209                 err("aCEF: cannot get used resources\n");
210
211         return rc;
212 }
213
214 static int pciehprm_delete_resource(
215         struct pci_resource **aprh,
216         ulong base,
217         ulong size)
218 {
219         struct pci_resource *res;
220         struct pci_resource *prevnode;
221         struct pci_resource *split_node;
222         ulong tbase;
223
224         pciehp_resource_sort_and_combine(aprh);
225
226         for (res = *aprh; res; res = res->next) {
227                 if (res->base > base)
228                         continue;
229
230                 if ((res->base + res->length) < (base + size))
231                         continue;
232
233                 if (res->base < base) {
234                         tbase = base;
235
236                         if ((res->length - (tbase - res->base)) < size)
237                                 continue;
238
239                         split_node = (struct pci_resource *) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
240                         if (!split_node)
241                                 return -ENOMEM;
242
243                         split_node->base = res->base;
244                         split_node->length = tbase - res->base;
245                         res->base = tbase;
246                         res->length -= split_node->length;
247
248                         split_node->next = res->next;
249                         res->next = split_node;
250                 }
251
252                 if (res->length >= size) {
253                         split_node = (struct pci_resource*) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
254                         if (!split_node)
255                                 return -ENOMEM;
256
257                         split_node->base = res->base + size;
258                         split_node->length = res->length - size;
259                         res->length = size;
260
261                         split_node->next = res->next;
262                         res->next = split_node;
263                 }
264
265                 if (*aprh == res) {
266                         *aprh = res->next;
267                 } else {
268                         prevnode = *aprh;
269                         while (prevnode->next != res)
270                                 prevnode = prevnode->next;
271
272                         prevnode->next = res->next;
273                 }
274                 res->next = NULL;
275                 kfree(res);
276                 break;
277         }
278
279         return 0;
280 }
281
282 static int bind_pci_resources_to_slots ( struct controller *ctrl)
283 {
284         struct pci_func *func;
285         int busn = ctrl->slot_bus;
286         int devn, funn;
287         u32     vid;
288
289         for (devn = 0; devn < 32; devn++) {
290                 for (funn = 0; funn < 8; funn++) {
291                         /*
292                         if (devn == ctrl->device && funn == ctrl->function)
293                                 continue;
294                         */
295                         /* find out if this entry is for an occupied slot */
296                         vid = 0xFFFFFFFF;
297
298                         pci_bus_read_config_dword(ctrl->pci_dev->subordinate, PCI_DEVFN(devn, funn), PCI_VENDOR_ID, &vid);
299
300                         if (vid != 0xFFFFFFFF) {
301                                 dbg("%s: vid = %x bus %x dev %x fun %x\n", __FUNCTION__,
302                                 vid, busn, devn, funn);
303                                 func = pciehp_slot_find(busn, devn, funn);
304                                 dbg("%s: func = %p\n", __FUNCTION__,func);
305                                 if (!func)
306                                         continue;
307                                 configure_existing_function(ctrl, func);
308                                 dbg("aCCF:existing PCI 0x%x Func ResourceDump\n", ctrl->bus);
309                                 phprm_dump_func_res(func);
310                         }
311                 }
312         }
313
314         return 0;
315 }
316
317 static void phprm_dump_ctrl_res( struct controller *ctlr)
318 {
319         struct controller *ctrl = ctlr;
320
321         if (ctrl->bus_head) {
322                 dbg(":    BUS Resources:\n");
323                 print_pci_resource (ctrl->bus_head);
324         }
325         if (ctrl->io_head) {
326                 dbg(":    IO Resources:\n");
327                 print_pci_resource (ctrl->io_head);
328         }
329         if (ctrl->mem_head) {
330                 dbg(":    MEM Resources:\n");
331                 print_pci_resource (ctrl->mem_head);
332         }
333         if (ctrl->p_mem_head) {
334                 dbg(":    PMEM Resources:\n");
335                 print_pci_resource (ctrl->p_mem_head);
336         }
337 }
338
339 /*
340  * phprm_find_available_resources
341  *
342  *  Finds available memory, IO, and IRQ resources for programming
343  *  devices which may be added to the system
344  *  this function is for hot plug ADD!
345  *
346  * returns 0 if success
347  */
348 int pciehprm_find_available_resources(struct controller *ctrl)
349 {
350         struct pci_func func;
351         u32 rc;
352
353         memset(&func, 0, sizeof(struct pci_func));
354
355         func.bus = ctrl->bus;
356         func.device = ctrl->device;
357         func.function = ctrl->function;
358         func.is_a_board = 1;
359
360         /* Get resources for this PCI bridge */
361         rc = pciehp_save_used_resources (ctrl, &func, !DISABLE_CARD);
362         dbg("%s: pciehp_save_used_resources rc = %d\n", __FUNCTION__, rc);
363
364         if (func.mem_head)
365                 func.mem_head->next = ctrl->mem_head;
366         ctrl->mem_head = func.mem_head;
367
368         if (func.p_mem_head)
369                 func.p_mem_head->next = ctrl->p_mem_head;
370         ctrl->p_mem_head = func.p_mem_head;
371
372         if (func.io_head)
373                 func.io_head->next = ctrl->io_head;
374         ctrl->io_head = func.io_head;
375
376         if(func.bus_head)
377                 func.bus_head->next = ctrl->bus_head;
378         ctrl->bus_head = func.bus_head;
379
380         if (ctrl->bus_head)
381                 pciehprm_delete_resource(&ctrl->bus_head, ctrl->pci_dev->subordinate->number, 1);
382         
383         dbg("%s:pre-Bind PCI 0x%x Ctrl Resource Dump\n", __FUNCTION__, ctrl->bus);
384         phprm_dump_ctrl_res(ctrl);
385
386         dbg("%s: before bind_pci_resources_to slots\n", __FUNCTION__);
387
388         bind_pci_resources_to_slots (ctrl);
389
390         dbg("%s:post-Bind PCI 0x%x Ctrl Resource Dump\n", __FUNCTION__, ctrl->bus);
391         phprm_dump_ctrl_res(ctrl);
392
393         return (rc);
394 }
395
396 int pciehprm_set_hpp(
397         struct controller *ctrl,
398         struct pci_func *func,
399         u8      card_type)
400 {
401         u32 rc;
402         u8 temp_byte;
403         struct pci_bus lpci_bus, *pci_bus;
404         unsigned int    devfn;
405         memcpy(&lpci_bus, ctrl->pci_bus, sizeof(lpci_bus));
406         pci_bus = &lpci_bus;
407         pci_bus->number = func->bus;
408         devfn = PCI_DEVFN(func->device, func->function);
409
410         temp_byte = 0x40;       /* hard coded value for LT */
411         if (card_type == PCI_HEADER_TYPE_BRIDGE) {
412                 /* set subordinate Latency Timer */
413                 rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_SEC_LATENCY_TIMER, temp_byte);
414
415                 if (rc) {
416                         dbg("%s: set secondary LT error. b:d:f(%02x:%02x:%02x)\n", __FUNCTION__, 
417                                 func->bus, func->device, func->function);
418                         return rc;
419                 }
420         }
421
422         /* set base Latency Timer */
423         rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_LATENCY_TIMER, temp_byte);
424
425         if (rc) {
426                 dbg("%s: set LT error. b:d:f(%02x:%02x:%02x)\n", __FUNCTION__, func->bus, func->device, func->function);
427                 return rc;
428         }
429
430         /* set Cache Line size */
431         temp_byte = 0x08;       /* hard coded value for CLS */
432
433         rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_CACHE_LINE_SIZE, temp_byte);
434
435         if (rc) {
436                 dbg("%s: set CLS error. b:d:f(%02x:%02x:%02x)\n", __FUNCTION__, func->bus, func->device, func->function);
437         }
438
439         /* set enable_perr */
440         /* set enable_serr */
441
442         return rc;
443 }
444
445 void pciehprm_enable_card(
446         struct controller *ctrl,
447         struct pci_func *func,
448         u8 card_type)
449 {
450         u16 command, bcommand;
451         struct pci_bus lpci_bus, *pci_bus;
452         unsigned int devfn;
453         int rc;
454
455         memcpy(&lpci_bus, ctrl->pci_bus, sizeof(lpci_bus));
456         pci_bus = &lpci_bus;
457         pci_bus->number = func->bus;
458         devfn = PCI_DEVFN(func->device, func->function);
459
460         rc = pci_bus_read_config_word(pci_bus, devfn, PCI_COMMAND, &command);
461
462         command |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR
463                 | PCI_COMMAND_MASTER | PCI_COMMAND_INVALIDATE
464                 | PCI_COMMAND_IO | PCI_COMMAND_MEMORY;
465
466         rc = pci_bus_write_config_word(pci_bus, devfn, PCI_COMMAND, command);
467
468         if (card_type == PCI_HEADER_TYPE_BRIDGE) {
469
470                 rc = pci_bus_read_config_word(pci_bus, devfn, PCI_BRIDGE_CONTROL, &bcommand);
471
472                 bcommand |= PCI_BRIDGE_CTL_PARITY | PCI_BRIDGE_CTL_SERR
473                         | PCI_BRIDGE_CTL_NO_ISA;
474
475                 rc = pci_bus_write_config_word(pci_bus, devfn, PCI_BRIDGE_CONTROL, bcommand);
476         }
477 }
478
479 static int legacy_pciehprm_init_pci(void)
480 {
481         return 0;
482 }
483
484 int pciehprm_init(enum php_ctlr_type ctrl_type)
485 {
486         int retval;
487
488         switch (ctrl_type) {
489         case PCI:
490                 retval = legacy_pciehprm_init_pci();
491                 break;
492         default:
493                 retval = -ENODEV;
494                 break;
495         }
496
497         return retval;
498 }