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