1 // SPDX-License-Identifier: GPL-2.0+
3 * IBM Hot Plug Controller Driver
5 * Written By: Irene Zubarev, IBM Corporation
7 * Copyright (C) 2001 Greg Kroah-Hartman (greg@kroah.com)
8 * Copyright (C) 2001,2002 IBM Corp.
10 * All rights reserved.
12 * Send feedback to <gregkh@us.ibm.com>
16 #include <linux/module.h>
17 #include <linux/slab.h>
18 #include <linux/pci.h>
19 #include <linux/list.h>
20 #include <linux/init.h>
23 static int flags = 0; /* for testing */
25 static void update_resources(struct bus_node *bus_cur, int type, int rangeno);
26 static int once_over(void);
27 static int remove_ranges(struct bus_node *, struct bus_node *);
28 static int update_bridge_ranges(struct bus_node **);
29 static int add_bus_range(int type, struct range_node *, struct bus_node *);
30 static void fix_resources(struct bus_node *);
31 static struct bus_node *find_bus_wprev(u8, struct bus_node **, u8);
33 static LIST_HEAD(gbuses);
35 static struct bus_node * __init alloc_error_bus(struct ebda_pci_rsrc *curr, u8 busno, int flag)
37 struct bus_node *newbus;
39 if (!(curr) && !(flag)) {
40 err("NULL pointer passed\n");
44 newbus = kzalloc(sizeof(struct bus_node), GFP_KERNEL);
49 newbus->busno = busno;
51 newbus->busno = curr->bus_num;
52 list_add_tail(&newbus->bus_list, &gbuses);
56 static struct resource_node * __init alloc_resources(struct ebda_pci_rsrc *curr)
58 struct resource_node *rs;
61 err("NULL passed to allocate\n");
65 rs = kzalloc(sizeof(struct resource_node), GFP_KERNEL);
69 rs->busno = curr->bus_num;
70 rs->devfunc = curr->dev_fun;
71 rs->start = curr->start_addr;
72 rs->end = curr->end_addr;
73 rs->len = curr->end_addr - curr->start_addr + 1;
77 static int __init alloc_bus_range(struct bus_node **new_bus, struct range_node **new_range, struct ebda_pci_rsrc *curr, int flag, u8 first_bus)
79 struct bus_node *newbus;
80 struct range_node *newrange;
84 newbus = kzalloc(sizeof(struct bus_node), GFP_KERNEL);
88 newbus->busno = curr->bus_num;
93 num_ranges = newbus->noMemRanges;
96 num_ranges = newbus->noPFMemRanges;
99 num_ranges = newbus->noIORanges;
104 newrange = kzalloc(sizeof(struct range_node), GFP_KERNEL);
110 newrange->start = curr->start_addr;
111 newrange->end = curr->end_addr;
113 if (first_bus || (!num_ranges))
114 newrange->rangeno = 1;
116 /* need to insert our range */
117 add_bus_range(flag, newrange, newbus);
118 debug("%d resource Primary Bus inserted on bus %x [%x - %x]\n", flag, newbus->busno, newrange->start, newrange->end);
123 newbus->rangeMem = newrange;
125 newbus->noMemRanges = 1;
127 debug("First Memory Primary on bus %x, [%x - %x]\n", newbus->busno, newrange->start, newrange->end);
128 ++newbus->noMemRanges;
129 fix_resources(newbus);
133 newbus->rangeIO = newrange;
135 newbus->noIORanges = 1;
137 debug("First IO Primary on bus %x, [%x - %x]\n", newbus->busno, newrange->start, newrange->end);
138 ++newbus->noIORanges;
139 fix_resources(newbus);
143 newbus->rangePFMem = newrange;
145 newbus->noPFMemRanges = 1;
147 debug("1st PFMemory Primary on Bus %x [%x - %x]\n", newbus->busno, newrange->start, newrange->end);
148 ++newbus->noPFMemRanges;
149 fix_resources(newbus);
156 *new_range = newrange;
162 * 1. The ranges are ordered. The buses are not ordered. (First come)
164 * 2. If cannot allocate out of PFMem range, allocate from Mem ranges. PFmemFromMem
165 * are not sorted. (no need since use mem node). To not change the entire code, we
166 * also add mem node whenever this case happens so as not to change
167 * ibmphp_check_mem_resource etc(and since it really is taking Mem resource)
170 /*****************************************************************************
171 * This is the Resource Management initialization function. It will go through
172 * the Resource list taken from EBDA and fill in this module's data structures
174 * THIS IS NOT TAKING INTO CONSIDERATION IO RESTRICTIONS OF PRIMARY BUSES,
175 * SINCE WE'RE GOING TO ASSUME FOR NOW WE DON'T HAVE THOSE ON OUR BUSES FOR NOW
177 * Input: ptr to the head of the resource list from EBDA
178 * Output: 0, -1 or error codes
179 ***************************************************************************/
180 int __init ibmphp_rsrc_init(void)
182 struct ebda_pci_rsrc *curr;
183 struct range_node *newrange = NULL;
184 struct bus_node *newbus = NULL;
185 struct bus_node *bus_cur;
186 struct bus_node *bus_prev;
187 struct resource_node *new_io = NULL;
188 struct resource_node *new_mem = NULL;
189 struct resource_node *new_pfmem = NULL;
192 list_for_each_entry(curr, &ibmphp_ebda_pci_rsrc_head,
193 ebda_pci_rsrc_list) {
194 if (!(curr->rsrc_type & PCIDEVMASK)) {
195 /* EBDA still lists non PCI devices, so ignore... */
196 debug("this is not a PCI DEVICE in rsrc_init, please take care\n");
200 /* this is a primary bus resource */
201 if (curr->rsrc_type & PRIMARYBUSMASK) {
203 if ((curr->rsrc_type & RESTYPE) == MMASK) {
204 /* no bus structure exists in place yet */
205 if (list_empty(&gbuses)) {
206 rc = alloc_bus_range(&newbus, &newrange, curr, MEM, 1);
209 list_add_tail(&newbus->bus_list, &gbuses);
210 debug("gbuses = NULL, Memory Primary Bus %x [%x - %x]\n", newbus->busno, newrange->start, newrange->end);
212 bus_cur = find_bus_wprev(curr->bus_num, &bus_prev, 1);
215 rc = alloc_bus_range(&bus_cur, &newrange, curr, MEM, 0);
219 /* went through all the buses and didn't find ours, need to create a new bus node */
220 rc = alloc_bus_range(&newbus, &newrange, curr, MEM, 1);
224 list_add_tail(&newbus->bus_list, &gbuses);
225 debug("New Bus, Memory Primary Bus %x [%x - %x]\n", newbus->busno, newrange->start, newrange->end);
228 } else if ((curr->rsrc_type & RESTYPE) == PFMASK) {
229 /* prefetchable memory */
230 if (list_empty(&gbuses)) {
231 /* no bus structure exists in place yet */
232 rc = alloc_bus_range(&newbus, &newrange, curr, PFMEM, 1);
235 list_add_tail(&newbus->bus_list, &gbuses);
236 debug("gbuses = NULL, PFMemory Primary Bus %x [%x - %x]\n", newbus->busno, newrange->start, newrange->end);
238 bus_cur = find_bus_wprev(curr->bus_num, &bus_prev, 1);
241 rc = alloc_bus_range(&bus_cur, &newrange, curr, PFMEM, 0);
245 /* went through all the buses and didn't find ours, need to create a new bus node */
246 rc = alloc_bus_range(&newbus, &newrange, curr, PFMEM, 1);
249 list_add_tail(&newbus->bus_list, &gbuses);
250 debug("1st Bus, PFMemory Primary Bus %x [%x - %x]\n", newbus->busno, newrange->start, newrange->end);
253 } else if ((curr->rsrc_type & RESTYPE) == IOMASK) {
255 if (list_empty(&gbuses)) {
256 /* no bus structure exists in place yet */
257 rc = alloc_bus_range(&newbus, &newrange, curr, IO, 1);
260 list_add_tail(&newbus->bus_list, &gbuses);
261 debug("gbuses = NULL, IO Primary Bus %x [%x - %x]\n", newbus->busno, newrange->start, newrange->end);
263 bus_cur = find_bus_wprev(curr->bus_num, &bus_prev, 1);
265 rc = alloc_bus_range(&bus_cur, &newrange, curr, IO, 0);
269 /* went through all the buses and didn't find ours, need to create a new bus node */
270 rc = alloc_bus_range(&newbus, &newrange, curr, IO, 1);
273 list_add_tail(&newbus->bus_list, &gbuses);
274 debug("1st Bus, IO Primary Bus %x [%x - %x]\n", newbus->busno, newrange->start, newrange->end);
279 ; /* type is reserved WHAT TO DO IN THIS CASE???
283 /* regular pci device resource */
284 if ((curr->rsrc_type & RESTYPE) == MMASK) {
285 /* Memory resource */
286 new_mem = alloc_resources(curr);
291 * if it didn't find the bus, means PCI dev
292 * came b4 the Primary Bus info, so need to
293 * create a bus rangeno becomes a problem...
294 * assign a -1 and then update once the range
295 * actually appears...
297 if (ibmphp_add_resource(new_mem) < 0) {
298 newbus = alloc_error_bus(curr, 0, 0);
301 newbus->firstMem = new_mem;
302 ++newbus->needMemUpdate;
303 new_mem->rangeno = -1;
305 debug("Memory resource for device %x, bus %x, [%x - %x]\n", new_mem->devfunc, new_mem->busno, new_mem->start, new_mem->end);
307 } else if ((curr->rsrc_type & RESTYPE) == PFMASK) {
308 /* PFMemory resource */
309 new_pfmem = alloc_resources(curr);
312 new_pfmem->type = PFMEM;
313 new_pfmem->fromMem = 0;
314 if (ibmphp_add_resource(new_pfmem) < 0) {
315 newbus = alloc_error_bus(curr, 0, 0);
318 newbus->firstPFMem = new_pfmem;
319 ++newbus->needPFMemUpdate;
320 new_pfmem->rangeno = -1;
323 debug("PFMemory resource for device %x, bus %x, [%x - %x]\n", new_pfmem->devfunc, new_pfmem->busno, new_pfmem->start, new_pfmem->end);
324 } else if ((curr->rsrc_type & RESTYPE) == IOMASK) {
326 new_io = alloc_resources(curr);
332 * if it didn't find the bus, means PCI dev
333 * came b4 the Primary Bus info, so need to
334 * create a bus rangeno becomes a problem...
335 * Can assign a -1 and then update once the
336 * range actually appears...
338 if (ibmphp_add_resource(new_io) < 0) {
339 newbus = alloc_error_bus(curr, 0, 0);
342 newbus->firstIO = new_io;
343 ++newbus->needIOUpdate;
344 new_io->rangeno = -1;
346 debug("IO resource for device %x, bus %x, [%x - %x]\n", new_io->devfunc, new_io->busno, new_io->start, new_io->end);
351 list_for_each_entry(bus_cur, &gbuses, bus_list) {
352 /* This is to get info about PPB resources, since EBDA doesn't put this info into the primary bus info */
353 rc = update_bridge_ranges(&bus_cur);
357 return once_over(); /* This is to align ranges (so no -1) */
360 /********************************************************************************
361 * This function adds a range into a sorted list of ranges per bus for a particular
362 * range type, it then calls another routine to update the range numbers on the
363 * pci devices' resources for the appropriate resource
365 * Input: type of the resource, range to add, current bus
366 * Output: 0 or -1, bus and range ptrs
367 ********************************************************************************/
368 static int add_bus_range(int type, struct range_node *range, struct bus_node *bus_cur)
370 struct range_node *range_cur = NULL;
371 struct range_node *range_prev;
372 int count = 0, i_init;
377 range_cur = bus_cur->rangeMem;
378 noRanges = bus_cur->noMemRanges;
381 range_cur = bus_cur->rangePFMem;
382 noRanges = bus_cur->noPFMemRanges;
385 range_cur = bus_cur->rangeIO;
386 noRanges = bus_cur->noIORanges;
392 if (range->start < range_cur->start)
394 range_prev = range_cur;
395 range_cur = range_cur->next;
399 /* our range will go at the beginning of the list */
402 bus_cur->rangeMem = range;
405 bus_cur->rangePFMem = range;
408 bus_cur->rangeIO = range;
411 range->next = range_cur;
414 } else if (!range_cur) {
415 /* our range will go at the end of the list */
417 range_prev->next = range;
418 range->rangeno = range_prev->rangeno + 1;
421 /* the range is in the middle */
422 range_prev->next = range;
423 range->next = range_cur;
424 range->rangeno = range_cur->rangeno;
425 i_init = range_prev->rangeno;
428 for (count = i_init; count < noRanges; ++count) {
429 ++range_cur->rangeno;
430 range_cur = range_cur->next;
433 update_resources(bus_cur, type, i_init + 1);
437 /*******************************************************************************
438 * This routine goes through the list of resources of type 'type' and updates
439 * the range numbers that they correspond to. It was called from add_bus_range fnc
441 * Input: bus, type of the resource, the rangeno starting from which to update
442 ******************************************************************************/
443 static void update_resources(struct bus_node *bus_cur, int type, int rangeno)
445 struct resource_node *res = NULL;
446 u8 eol = 0; /* end of list indicator */
450 if (bus_cur->firstMem)
451 res = bus_cur->firstMem;
454 if (bus_cur->firstPFMem)
455 res = bus_cur->firstPFMem;
458 if (bus_cur->firstIO)
459 res = bus_cur->firstIO;
465 if (res->rangeno == rangeno)
469 else if (res->nextRange)
470 res = res->nextRange;
478 /* found the range */
487 static void fix_me(struct resource_node *res, struct bus_node *bus_cur, struct range_node *range)
503 if (res->rangeno == -1) {
505 if ((res->start >= range->start) && (res->end <= range->end)) {
506 res->rangeno = range->rangeno;
507 debug("%s->rangeno in fix_resources is %d\n", str, res->rangeno);
510 --bus_cur->needIOUpdate;
513 --bus_cur->needMemUpdate;
516 --bus_cur->needPFMemUpdate;
527 res = res->nextRange;
532 /*****************************************************************************
533 * This routine reassigns the range numbers to the resources that had a -1
534 * This case can happen only if upon initialization, resources taken by pci dev
535 * appear in EBDA before the resources allocated for that bus, since we don't
536 * know the range, we assign -1, and this routine is called after a new range
537 * is assigned to see the resources with unknown range belong to the added range
540 * Output: none, list of resources for that bus are fixed if can be
541 *******************************************************************************/
542 static void fix_resources(struct bus_node *bus_cur)
544 struct range_node *range;
545 struct resource_node *res;
547 debug("%s - bus_cur->busno = %d\n", __func__, bus_cur->busno);
549 if (bus_cur->needIOUpdate) {
550 res = bus_cur->firstIO;
551 range = bus_cur->rangeIO;
552 fix_me(res, bus_cur, range);
554 if (bus_cur->needMemUpdate) {
555 res = bus_cur->firstMem;
556 range = bus_cur->rangeMem;
557 fix_me(res, bus_cur, range);
559 if (bus_cur->needPFMemUpdate) {
560 res = bus_cur->firstPFMem;
561 range = bus_cur->rangePFMem;
562 fix_me(res, bus_cur, range);
566 /*******************************************************************************
567 * This routine adds a resource to the list of resources to the appropriate bus
568 * based on their resource type and sorted by their starting addresses. It assigns
569 * the ptrs to next and nextRange if needed.
571 * Input: resource ptr
572 * Output: ptrs assigned (to the node)
574 *******************************************************************************/
575 int ibmphp_add_resource(struct resource_node *res)
577 struct resource_node *res_cur;
578 struct resource_node *res_prev;
579 struct bus_node *bus_cur;
580 struct range_node *range_cur = NULL;
581 struct resource_node *res_start = NULL;
583 debug("%s - enter\n", __func__);
586 err("NULL passed to add\n");
590 bus_cur = find_bus_wprev(res->busno, NULL, 0);
593 /* didn't find a bus, something's wrong!!! */
594 debug("no bus in the system, either pci_dev's wrong or allocation failed\n");
601 range_cur = bus_cur->rangeIO;
602 res_start = bus_cur->firstIO;
605 range_cur = bus_cur->rangeMem;
606 res_start = bus_cur->firstMem;
609 range_cur = bus_cur->rangePFMem;
610 res_start = bus_cur->firstPFMem;
613 err("cannot read the type of the resource to add... problem\n");
617 if ((res->start >= range_cur->start) && (res->end <= range_cur->end)) {
618 res->rangeno = range_cur->rangeno;
621 range_cur = range_cur->next;
624 /* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
625 * this is again the case of rangeno = -1
626 * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
632 ++bus_cur->needIOUpdate;
635 ++bus_cur->needMemUpdate;
638 ++bus_cur->needPFMemUpdate;
644 debug("The range is %d\n", res->rangeno);
646 /* no first{IO,Mem,Pfmem} on the bus, 1st IO/Mem/Pfmem resource ever */
649 bus_cur->firstIO = res;
652 bus_cur->firstMem = res;
655 bus_cur->firstPFMem = res;
659 res->nextRange = NULL;
664 debug("res_cur->rangeno is %d\n", res_cur->rangeno);
667 if (res_cur->rangeno >= res->rangeno)
671 res_cur = res_cur->next;
673 res_cur = res_cur->nextRange;
677 /* at the end of the resource list */
678 debug("i should be here, [%x - %x]\n", res->start, res->end);
679 res_prev->nextRange = res;
681 res->nextRange = NULL;
682 } else if (res_cur->rangeno == res->rangeno) {
683 /* in the same range */
685 if (res->start < res_cur->start)
688 res_cur = res_cur->next;
691 /* the last resource in this range */
692 res_prev->next = res;
694 res->nextRange = res_prev->nextRange;
695 res_prev->nextRange = NULL;
696 } else if (res->start < res_cur->start) {
697 /* at the beginning or middle of the range */
701 bus_cur->firstIO = res;
704 bus_cur->firstMem = res;
707 bus_cur->firstPFMem = res;
710 } else if (res_prev->rangeno == res_cur->rangeno)
711 res_prev->next = res;
713 res_prev->nextRange = res;
716 res->nextRange = NULL;
719 /* this is the case where it is 1st occurrence of the range */
721 /* at the beginning of the resource list */
725 res->nextRange = bus_cur->firstIO;
726 bus_cur->firstIO = res;
729 res->nextRange = bus_cur->firstMem;
730 bus_cur->firstMem = res;
733 res->nextRange = bus_cur->firstPFMem;
734 bus_cur->firstPFMem = res;
737 } else if (res_cur->rangeno > res->rangeno) {
738 /* in the middle of the resource list */
739 res_prev->nextRange = res;
741 res->nextRange = res_cur;
746 debug("%s - exit\n", __func__);
750 /****************************************************************************
751 * This routine will remove the resource from the list of resources
753 * Input: io, mem, and/or pfmem resource to be deleted
754 * Output: modified resource list
756 ****************************************************************************/
757 int ibmphp_remove_resource(struct resource_node *res)
759 struct bus_node *bus_cur;
760 struct resource_node *res_cur = NULL;
761 struct resource_node *res_prev;
762 struct resource_node *mem_cur;
766 err("resource to remove is NULL\n");
770 bus_cur = find_bus_wprev(res->busno, NULL, 0);
773 err("cannot find corresponding bus of the io resource to remove bailing out...\n");
779 res_cur = bus_cur->firstIO;
783 res_cur = bus_cur->firstMem;
787 res_cur = bus_cur->firstPFMem;
791 err("unknown type for resource to remove\n");
797 if ((res_cur->start == res->start) && (res_cur->end == res->end))
801 res_cur = res_cur->next;
803 res_cur = res_cur->nextRange;
807 if (res->type == PFMEM) {
809 * case where pfmem might be in the PFMemFromMem list
810 * so will also need to remove the corresponding mem
813 res_cur = bus_cur->firstPFMemFromMem;
817 if ((res_cur->start == res->start) && (res_cur->end == res->end)) {
818 mem_cur = bus_cur->firstMem;
820 if ((mem_cur->start == res_cur->start)
821 && (mem_cur->end == res_cur->end))
824 mem_cur = mem_cur->next;
826 mem_cur = mem_cur->nextRange;
829 err("cannot find corresponding mem node for pfmem...\n");
833 ibmphp_remove_resource(mem_cur);
835 bus_cur->firstPFMemFromMem = res_cur->next;
837 res_prev->next = res_cur->next;
843 res_cur = res_cur->next;
845 res_cur = res_cur->nextRange;
848 err("cannot find pfmem to delete...\n");
852 err("the %s resource is not in the list to be deleted...\n", type);
857 /* first device to be deleted */
861 bus_cur->firstIO = res_cur->next;
864 bus_cur->firstMem = res_cur->next;
867 bus_cur->firstPFMem = res_cur->next;
870 } else if (res_cur->nextRange) {
873 bus_cur->firstIO = res_cur->nextRange;
876 bus_cur->firstMem = res_cur->nextRange;
879 bus_cur->firstPFMem = res_cur->nextRange;
885 bus_cur->firstIO = NULL;
888 bus_cur->firstMem = NULL;
891 bus_cur->firstPFMem = NULL;
899 if (res_prev->rangeno == res_cur->rangeno)
900 res_prev->next = res_cur->next;
902 res_prev->nextRange = res_cur->next;
903 } else if (res_cur->nextRange) {
904 res_prev->next = NULL;
905 res_prev->nextRange = res_cur->nextRange;
907 res_prev->next = NULL;
908 res_prev->nextRange = NULL;
917 static struct range_node *find_range(struct bus_node *bus_cur, struct resource_node *res)
919 struct range_node *range = NULL;
923 range = bus_cur->rangeIO;
926 range = bus_cur->rangeMem;
929 range = bus_cur->rangePFMem;
932 err("cannot read resource type in find_range\n");
936 if (res->rangeno == range->rangeno)
943 /*****************************************************************************
944 * This routine will check to make sure the io/mem/pfmem->len that the device asked for
945 * can fit w/i our list of available IO/MEM/PFMEM resources. If cannot, returns -EINVAL,
946 * otherwise, returns 0
949 * Output: the correct start and end address are inputted into the resource node,
951 *****************************************************************************/
952 int ibmphp_check_resource(struct resource_node *res, u8 bridge)
954 struct bus_node *bus_cur;
955 struct range_node *range = NULL;
956 struct resource_node *res_prev;
957 struct resource_node *res_cur = NULL;
958 u32 len_cur = 0, start_cur = 0, len_tmp = 0;
960 u32 tmp_start; /* this is to make sure start address is divisible by the length needed */
968 /* The rules for bridges are different, 4K divisible for IO, 1M for (pf)mem*/
970 tmp_divide = IOBRIDGE;
972 tmp_divide = MEMBRIDGE;
974 tmp_divide = res->len;
976 bus_cur = find_bus_wprev(res->busno, NULL, 0);
979 /* didn't find a bus, something's wrong!!! */
980 debug("no bus in the system, either pci_dev's wrong or allocation failed\n");
984 debug("%s - enter\n", __func__);
985 debug("bus_cur->busno is %d\n", bus_cur->busno);
987 /* This is a quick fix to not mess up with the code very much. i.e.,
988 * 2000-2fff, len = 1000, but when we compare, we need it to be fff */
993 res_cur = bus_cur->firstIO;
994 noranges = bus_cur->noIORanges;
997 res_cur = bus_cur->firstMem;
998 noranges = bus_cur->noMemRanges;
1001 res_cur = bus_cur->firstPFMem;
1002 noranges = bus_cur->noPFMemRanges;
1005 err("wrong type of resource to check\n");
1011 range = find_range(bus_cur, res_cur);
1012 debug("%s - rangeno = %d\n", __func__, res_cur->rangeno);
1015 err("no range for the device exists... bailing out...\n");
1019 /* found our range */
1021 /* first time in the loop */
1022 len_tmp = res_cur->start - 1 - range->start;
1024 if ((res_cur->start != range->start) && (len_tmp >= res->len)) {
1025 debug("len_tmp = %x\n", len_tmp);
1027 if ((len_tmp < len_cur) || (len_cur == 0)) {
1029 if ((range->start % tmp_divide) == 0) {
1030 /* just perfect, starting address is divisible by length */
1033 start_cur = range->start;
1035 /* Needs adjusting */
1036 tmp_start = range->start;
1039 while ((len_tmp = res_cur->start - 1 - tmp_start) >= res->len) {
1040 if ((tmp_start % tmp_divide) == 0) {
1043 start_cur = tmp_start;
1046 tmp_start += tmp_divide - tmp_start % tmp_divide;
1047 if (tmp_start >= res_cur->start - 1)
1052 if (flag && len_cur == res->len) {
1053 debug("but we are not here, right?\n");
1054 res->start = start_cur;
1055 res->len += 1; /* To restore the balance */
1056 res->end = res->start + res->len - 1;
1062 if (!res_cur->next) {
1063 /* last device on the range */
1064 len_tmp = range->end - (res_cur->end + 1);
1066 if ((range->end != res_cur->end) && (len_tmp >= res->len)) {
1067 debug("len_tmp = %x\n", len_tmp);
1068 if ((len_tmp < len_cur) || (len_cur == 0)) {
1070 if (((res_cur->end + 1) % tmp_divide) == 0) {
1071 /* just perfect, starting address is divisible by length */
1074 start_cur = res_cur->end + 1;
1076 /* Needs adjusting */
1077 tmp_start = res_cur->end + 1;
1080 while ((len_tmp = range->end - tmp_start) >= res->len) {
1081 if ((tmp_start % tmp_divide) == 0) {
1084 start_cur = tmp_start;
1087 tmp_start += tmp_divide - tmp_start % tmp_divide;
1088 if (tmp_start >= range->end)
1092 if (flag && len_cur == res->len) {
1093 res->start = start_cur;
1094 res->len += 1; /* To restore the balance */
1095 res->end = res->start + res->len - 1;
1103 if (res_prev->rangeno != res_cur->rangeno) {
1104 /* 1st device on this range */
1105 len_tmp = res_cur->start - 1 - range->start;
1107 if ((res_cur->start != range->start) && (len_tmp >= res->len)) {
1108 if ((len_tmp < len_cur) || (len_cur == 0)) {
1109 if ((range->start % tmp_divide) == 0) {
1110 /* just perfect, starting address is divisible by length */
1113 start_cur = range->start;
1115 /* Needs adjusting */
1116 tmp_start = range->start;
1119 while ((len_tmp = res_cur->start - 1 - tmp_start) >= res->len) {
1120 if ((tmp_start % tmp_divide) == 0) {
1123 start_cur = tmp_start;
1126 tmp_start += tmp_divide - tmp_start % tmp_divide;
1127 if (tmp_start >= res_cur->start - 1)
1132 if (flag && len_cur == res->len) {
1133 res->start = start_cur;
1134 res->len += 1; /* To restore the balance */
1135 res->end = res->start + res->len - 1;
1141 /* in the same range */
1142 len_tmp = res_cur->start - 1 - res_prev->end - 1;
1144 if (len_tmp >= res->len) {
1145 if ((len_tmp < len_cur) || (len_cur == 0)) {
1146 if (((res_prev->end + 1) % tmp_divide) == 0) {
1147 /* just perfect, starting address's divisible by length */
1150 start_cur = res_prev->end + 1;
1152 /* Needs adjusting */
1153 tmp_start = res_prev->end + 1;
1156 while ((len_tmp = res_cur->start - 1 - tmp_start) >= res->len) {
1157 if ((tmp_start % tmp_divide) == 0) {
1160 start_cur = tmp_start;
1163 tmp_start += tmp_divide - tmp_start % tmp_divide;
1164 if (tmp_start >= res_cur->start - 1)
1169 if (flag && len_cur == res->len) {
1170 res->start = start_cur;
1171 res->len += 1; /* To restore the balance */
1172 res->end = res->start + res->len - 1;
1179 /* end if (res_prev) */
1182 res_cur = res_cur->next;
1184 res_cur = res_cur->nextRange;
1185 } /* end of while */
1189 /* 1st device ever */
1190 /* need to find appropriate range */
1191 switch (res->type) {
1193 range = bus_cur->rangeIO;
1196 range = bus_cur->rangeMem;
1199 range = bus_cur->rangePFMem;
1203 len_tmp = range->end - range->start;
1205 if (len_tmp >= res->len) {
1206 if ((len_tmp < len_cur) || (len_cur == 0)) {
1207 if ((range->start % tmp_divide) == 0) {
1208 /* just perfect, starting address's divisible by length */
1211 start_cur = range->start;
1213 /* Needs adjusting */
1214 tmp_start = range->start;
1217 while ((len_tmp = range->end - tmp_start) >= res->len) {
1218 if ((tmp_start % tmp_divide) == 0) {
1221 start_cur = tmp_start;
1224 tmp_start += tmp_divide - tmp_start % tmp_divide;
1225 if (tmp_start >= range->end)
1230 if (flag && len_cur == res->len) {
1231 res->start = start_cur;
1232 res->len += 1; /* To restore the balance */
1233 res->end = res->start + res->len - 1;
1238 range = range->next;
1239 } /* end of while */
1241 if ((!range) && (len_cur == 0)) {
1242 /* have gone through the list of devices and ranges and haven't found n.e.thing */
1243 err("no appropriate range.. bailing out...\n");
1245 } else if (len_cur) {
1246 res->start = start_cur;
1247 res->len += 1; /* To restore the balance */
1248 res->end = res->start + res->len - 1;
1254 debug("prev->rangeno = %d, noranges = %d\n", res_prev->rangeno, noranges);
1255 if (res_prev->rangeno < noranges) {
1256 /* if there're more ranges out there to check */
1257 switch (res->type) {
1259 range = bus_cur->rangeIO;
1262 range = bus_cur->rangeMem;
1265 range = bus_cur->rangePFMem;
1269 len_tmp = range->end - range->start;
1271 if (len_tmp >= res->len) {
1272 if ((len_tmp < len_cur) || (len_cur == 0)) {
1273 if ((range->start % tmp_divide) == 0) {
1274 /* just perfect, starting address's divisible by length */
1277 start_cur = range->start;
1279 /* Needs adjusting */
1280 tmp_start = range->start;
1283 while ((len_tmp = range->end - tmp_start) >= res->len) {
1284 if ((tmp_start % tmp_divide) == 0) {
1287 start_cur = tmp_start;
1290 tmp_start += tmp_divide - tmp_start % tmp_divide;
1291 if (tmp_start >= range->end)
1296 if (flag && len_cur == res->len) {
1297 res->start = start_cur;
1298 res->len += 1; /* To restore the balance */
1299 res->end = res->start + res->len - 1;
1304 range = range->next;
1305 } /* end of while */
1307 if ((!range) && (len_cur == 0)) {
1308 /* have gone through the list of devices and ranges and haven't found n.e.thing */
1309 err("no appropriate range.. bailing out...\n");
1311 } else if (len_cur) {
1312 res->start = start_cur;
1313 res->len += 1; /* To restore the balance */
1314 res->end = res->start + res->len - 1;
1318 /* no more ranges to check on */
1320 res->start = start_cur;
1321 res->len += 1; /* To restore the balance */
1322 res->end = res->start + res->len - 1;
1325 /* have gone through the list of devices and haven't found n.e.thing */
1326 err("no appropriate range.. bailing out...\n");
1330 } /* end if (!res_cur) */
1334 /********************************************************************************
1335 * This routine is called from remove_card if the card contained PPB.
1336 * It will remove all the resources on the bus as well as the bus itself
1338 * Output: 0, -ENODEV
1339 ********************************************************************************/
1340 int ibmphp_remove_bus(struct bus_node *bus, u8 parent_busno)
1342 struct resource_node *res_cur;
1343 struct resource_node *res_tmp;
1344 struct bus_node *prev_bus;
1347 prev_bus = find_bus_wprev(parent_busno, NULL, 0);
1350 debug("something terribly wrong. Cannot find parent bus to the one to remove\n");
1354 debug("In ibmphp_remove_bus... prev_bus->busno is %x\n", prev_bus->busno);
1356 rc = remove_ranges(bus, prev_bus);
1361 res_cur = bus->firstIO;
1365 res_cur = res_cur->next;
1367 res_cur = res_cur->nextRange;
1371 bus->firstIO = NULL;
1373 if (bus->firstMem) {
1374 res_cur = bus->firstMem;
1378 res_cur = res_cur->next;
1380 res_cur = res_cur->nextRange;
1384 bus->firstMem = NULL;
1386 if (bus->firstPFMem) {
1387 res_cur = bus->firstPFMem;
1391 res_cur = res_cur->next;
1393 res_cur = res_cur->nextRange;
1397 bus->firstPFMem = NULL;
1400 if (bus->firstPFMemFromMem) {
1401 res_cur = bus->firstPFMemFromMem;
1404 res_cur = res_cur->next;
1409 bus->firstPFMemFromMem = NULL;
1412 list_del(&bus->bus_list);
1417 /******************************************************************************
1418 * This routine deletes the ranges from a given bus, and the entries from the
1419 * parent's bus in the resources
1420 * Input: current bus, previous bus
1421 * Output: 0, -EINVAL
1422 ******************************************************************************/
1423 static int remove_ranges(struct bus_node *bus_cur, struct bus_node *bus_prev)
1425 struct range_node *range_cur;
1426 struct range_node *range_tmp;
1428 struct resource_node *res = NULL;
1430 if (bus_cur->noIORanges) {
1431 range_cur = bus_cur->rangeIO;
1432 for (i = 0; i < bus_cur->noIORanges; i++) {
1433 if (ibmphp_find_resource(bus_prev, range_cur->start, &res, IO) < 0)
1435 ibmphp_remove_resource(res);
1437 range_tmp = range_cur;
1438 range_cur = range_cur->next;
1442 bus_cur->rangeIO = NULL;
1444 if (bus_cur->noMemRanges) {
1445 range_cur = bus_cur->rangeMem;
1446 for (i = 0; i < bus_cur->noMemRanges; i++) {
1447 if (ibmphp_find_resource(bus_prev, range_cur->start, &res, MEM) < 0)
1450 ibmphp_remove_resource(res);
1451 range_tmp = range_cur;
1452 range_cur = range_cur->next;
1456 bus_cur->rangeMem = NULL;
1458 if (bus_cur->noPFMemRanges) {
1459 range_cur = bus_cur->rangePFMem;
1460 for (i = 0; i < bus_cur->noPFMemRanges; i++) {
1461 if (ibmphp_find_resource(bus_prev, range_cur->start, &res, PFMEM) < 0)
1464 ibmphp_remove_resource(res);
1465 range_tmp = range_cur;
1466 range_cur = range_cur->next;
1470 bus_cur->rangePFMem = NULL;
1476 * find the resource node in the bus
1477 * Input: Resource needed, start address of the resource, type of resource
1479 int ibmphp_find_resource(struct bus_node *bus, u32 start_address, struct resource_node **res, int flag)
1481 struct resource_node *res_cur = NULL;
1485 err("The bus passed in NULL to find resource\n");
1491 res_cur = bus->firstIO;
1495 res_cur = bus->firstMem;
1499 res_cur = bus->firstPFMem;
1503 err("wrong type of flag\n");
1508 if (res_cur->start == start_address) {
1513 res_cur = res_cur->next;
1515 res_cur = res_cur->nextRange;
1519 if (flag == PFMEM) {
1520 res_cur = bus->firstPFMemFromMem;
1522 if (res_cur->start == start_address) {
1526 res_cur = res_cur->next;
1529 debug("SOS...cannot find %s resource in the bus.\n", type);
1533 debug("SOS... cannot find %s resource in the bus.\n", type);
1539 debug("*res->start = %x\n", (*res)->start);
1544 /***********************************************************************
1545 * This routine will free the resource structures used by the
1546 * system. It is called from cleanup routine for the module
1549 ***********************************************************************/
1550 void ibmphp_free_resources(void)
1552 struct bus_node *bus_cur = NULL, *next;
1553 struct bus_node *bus_tmp;
1554 struct range_node *range_cur;
1555 struct range_node *range_tmp;
1556 struct resource_node *res_cur;
1557 struct resource_node *res_tmp;
1561 list_for_each_entry_safe(bus_cur, next, &gbuses, bus_list) {
1562 if (bus_cur->noIORanges) {
1563 range_cur = bus_cur->rangeIO;
1564 for (i = 0; i < bus_cur->noIORanges; i++) {
1567 range_tmp = range_cur;
1568 range_cur = range_cur->next;
1573 if (bus_cur->noMemRanges) {
1574 range_cur = bus_cur->rangeMem;
1575 for (i = 0; i < bus_cur->noMemRanges; i++) {
1578 range_tmp = range_cur;
1579 range_cur = range_cur->next;
1584 if (bus_cur->noPFMemRanges) {
1585 range_cur = bus_cur->rangePFMem;
1586 for (i = 0; i < bus_cur->noPFMemRanges; i++) {
1589 range_tmp = range_cur;
1590 range_cur = range_cur->next;
1596 if (bus_cur->firstIO) {
1597 res_cur = bus_cur->firstIO;
1601 res_cur = res_cur->next;
1603 res_cur = res_cur->nextRange;
1607 bus_cur->firstIO = NULL;
1609 if (bus_cur->firstMem) {
1610 res_cur = bus_cur->firstMem;
1614 res_cur = res_cur->next;
1616 res_cur = res_cur->nextRange;
1620 bus_cur->firstMem = NULL;
1622 if (bus_cur->firstPFMem) {
1623 res_cur = bus_cur->firstPFMem;
1627 res_cur = res_cur->next;
1629 res_cur = res_cur->nextRange;
1633 bus_cur->firstPFMem = NULL;
1636 if (bus_cur->firstPFMemFromMem) {
1637 res_cur = bus_cur->firstPFMemFromMem;
1640 res_cur = res_cur->next;
1645 bus_cur->firstPFMemFromMem = NULL;
1649 list_del(&bus_cur->bus_list);
1655 /*********************************************************************************
1656 * This function will go over the PFmem resources to check if the EBDA allocated
1657 * pfmem out of memory buckets of the bus. If so, it will change the range numbers
1658 * and a flag to indicate that this resource is out of memory. It will also move the
1659 * Pfmem out of the pfmem resource list to the PFMemFromMem list, and will create
1661 * This routine is called right after initialization
1662 *******************************************************************************/
1663 static int __init once_over(void)
1665 struct resource_node *pfmem_cur;
1666 struct resource_node *pfmem_prev;
1667 struct resource_node *mem;
1668 struct bus_node *bus_cur;
1670 list_for_each_entry(bus_cur, &gbuses, bus_list) {
1671 if ((!bus_cur->rangePFMem) && (bus_cur->firstPFMem)) {
1672 for (pfmem_cur = bus_cur->firstPFMem, pfmem_prev = NULL; pfmem_cur; pfmem_prev = pfmem_cur, pfmem_cur = pfmem_cur->next) {
1673 pfmem_cur->fromMem = 1;
1675 pfmem_prev->next = pfmem_cur->next;
1677 bus_cur->firstPFMem = pfmem_cur->next;
1679 if (!bus_cur->firstPFMemFromMem)
1680 pfmem_cur->next = NULL;
1682 /* we don't need to sort PFMemFromMem since we're using mem node for
1683 all the real work anyways, so just insert at the beginning of the
1686 pfmem_cur->next = bus_cur->firstPFMemFromMem;
1688 bus_cur->firstPFMemFromMem = pfmem_cur;
1690 mem = kzalloc(sizeof(struct resource_node), GFP_KERNEL);
1695 mem->busno = pfmem_cur->busno;
1696 mem->devfunc = pfmem_cur->devfunc;
1697 mem->start = pfmem_cur->start;
1698 mem->end = pfmem_cur->end;
1699 mem->len = pfmem_cur->len;
1700 if (ibmphp_add_resource(mem) < 0)
1701 err("Trouble...trouble... EBDA allocated pfmem from mem, but system doesn't display it has this space... unless not PCI device...\n");
1702 pfmem_cur->rangeno = mem->rangeno;
1703 } /* end for pfmem */
1705 } /* end list_for_each bus */
1709 int ibmphp_add_pfmem_from_mem(struct resource_node *pfmem)
1711 struct bus_node *bus_cur = find_bus_wprev(pfmem->busno, NULL, 0);
1714 err("cannot find bus of pfmem to add...\n");
1718 if (bus_cur->firstPFMemFromMem)
1719 pfmem->next = bus_cur->firstPFMemFromMem;
1723 bus_cur->firstPFMemFromMem = pfmem;
1728 /* This routine just goes through the buses to see if the bus already exists.
1729 * It is called from ibmphp_find_sec_number, to find out a secondary bus number for
1731 * Parameters: bus_number
1732 * Returns: Bus pointer or NULL
1734 struct bus_node *ibmphp_find_res_bus(u8 bus_number)
1736 return find_bus_wprev(bus_number, NULL, 0);
1739 static struct bus_node *find_bus_wprev(u8 bus_number, struct bus_node **prev, u8 flag)
1741 struct bus_node *bus_cur;
1743 list_for_each_entry(bus_cur, &gbuses, bus_list) {
1745 *prev = list_prev_entry(bus_cur, bus_list);
1746 if (bus_cur->busno == bus_number)
1753 void ibmphp_print_test(void)
1756 struct bus_node *bus_cur = NULL;
1757 struct range_node *range;
1758 struct resource_node *res;
1760 debug_pci("*****************START**********************\n");
1762 if ((!list_empty(&gbuses)) && flags) {
1763 err("The GBUSES is not NULL?!?!?!?!?\n");
1767 list_for_each_entry(bus_cur, &gbuses, bus_list) {
1768 debug_pci ("This is bus # %d. There are\n", bus_cur->busno);
1769 debug_pci ("IORanges = %d\t", bus_cur->noIORanges);
1770 debug_pci ("MemRanges = %d\t", bus_cur->noMemRanges);
1771 debug_pci ("PFMemRanges = %d\n", bus_cur->noPFMemRanges);
1772 debug_pci ("The IO Ranges are as follows:\n");
1773 if (bus_cur->rangeIO) {
1774 range = bus_cur->rangeIO;
1775 for (i = 0; i < bus_cur->noIORanges; i++) {
1776 debug_pci("rangeno is %d\n", range->rangeno);
1777 debug_pci("[%x - %x]\n", range->start, range->end);
1778 range = range->next;
1782 debug_pci("The Mem Ranges are as follows:\n");
1783 if (bus_cur->rangeMem) {
1784 range = bus_cur->rangeMem;
1785 for (i = 0; i < bus_cur->noMemRanges; i++) {
1786 debug_pci("rangeno is %d\n", range->rangeno);
1787 debug_pci("[%x - %x]\n", range->start, range->end);
1788 range = range->next;
1792 debug_pci("The PFMem Ranges are as follows:\n");
1794 if (bus_cur->rangePFMem) {
1795 range = bus_cur->rangePFMem;
1796 for (i = 0; i < bus_cur->noPFMemRanges; i++) {
1797 debug_pci("rangeno is %d\n", range->rangeno);
1798 debug_pci("[%x - %x]\n", range->start, range->end);
1799 range = range->next;
1803 debug_pci("The resources on this bus are as follows\n");
1805 debug_pci("IO...\n");
1806 if (bus_cur->firstIO) {
1807 res = bus_cur->firstIO;
1809 debug_pci("The range # is %d\n", res->rangeno);
1810 debug_pci("The bus, devfnc is %d, %x\n", res->busno, res->devfunc);
1811 debug_pci("[%x - %x], len=%x\n", res->start, res->end, res->len);
1814 else if (res->nextRange)
1815 res = res->nextRange;
1820 debug_pci("Mem...\n");
1821 if (bus_cur->firstMem) {
1822 res = bus_cur->firstMem;
1824 debug_pci("The range # is %d\n", res->rangeno);
1825 debug_pci("The bus, devfnc is %d, %x\n", res->busno, res->devfunc);
1826 debug_pci("[%x - %x], len=%x\n", res->start, res->end, res->len);
1829 else if (res->nextRange)
1830 res = res->nextRange;
1835 debug_pci("PFMem...\n");
1836 if (bus_cur->firstPFMem) {
1837 res = bus_cur->firstPFMem;
1839 debug_pci("The range # is %d\n", res->rangeno);
1840 debug_pci("The bus, devfnc is %d, %x\n", res->busno, res->devfunc);
1841 debug_pci("[%x - %x], len=%x\n", res->start, res->end, res->len);
1844 else if (res->nextRange)
1845 res = res->nextRange;
1851 debug_pci("PFMemFromMem...\n");
1852 if (bus_cur->firstPFMemFromMem) {
1853 res = bus_cur->firstPFMemFromMem;
1855 debug_pci("The range # is %d\n", res->rangeno);
1856 debug_pci("The bus, devfnc is %d, %x\n", res->busno, res->devfunc);
1857 debug_pci("[%x - %x], len=%x\n", res->start, res->end, res->len);
1862 debug_pci("***********************END***********************\n");
1865 static int range_exists_already(struct range_node *range, struct bus_node *bus_cur, u8 type)
1867 struct range_node *range_cur = NULL;
1870 range_cur = bus_cur->rangeIO;
1873 range_cur = bus_cur->rangeMem;
1876 range_cur = bus_cur->rangePFMem;
1879 err("wrong type passed to find out if range already exists\n");
1884 if ((range_cur->start == range->start) && (range_cur->end == range->end))
1886 range_cur = range_cur->next;
1892 /* This routine will read the windows for any PPB we have and update the
1893 * range info for the secondary bus, and will also input this info into
1894 * primary bus, since BIOS doesn't. This is for PPB that are in the system
1895 * on bootup. For bridged cards that were added during previous load of the
1896 * driver, only the ranges and the bus structure are added, the devices are
1898 * Input: primary busno
1900 * Note: this function doesn't take into account IO restrictions etc,
1901 * so will only work for bridges with no video/ISA devices behind them It
1902 * also will not work for onboard PPBs that can have more than 1 *bus
1903 * behind them All these are TO DO.
1904 * Also need to add more error checkings... (from fnc returns etc)
1906 static int __init update_bridge_ranges(struct bus_node **bus)
1908 u8 sec_busno, device, function, hdr_type, start_io_address, end_io_address;
1909 u16 vendor_id, upper_io_start, upper_io_end, start_mem_address, end_mem_address;
1910 u32 start_address, end_address, upper_start, upper_end;
1911 struct bus_node *bus_sec;
1912 struct bus_node *bus_cur;
1913 struct resource_node *io;
1914 struct resource_node *mem;
1915 struct resource_node *pfmem;
1916 struct range_node *range;
1922 ibmphp_pci_bus->number = bus_cur->busno;
1924 debug("inside %s\n", __func__);
1925 debug("bus_cur->busno = %x\n", bus_cur->busno);
1927 for (device = 0; device < 32; device++) {
1928 for (function = 0x00; function < 0x08; function++) {
1929 devfn = PCI_DEVFN(device, function);
1930 pci_bus_read_config_word(ibmphp_pci_bus, devfn, PCI_VENDOR_ID, &vendor_id);
1932 if (vendor_id != PCI_VENDOR_ID_NOTVALID) {
1933 /* found correct device!!! */
1934 pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_HEADER_TYPE, &hdr_type);
1937 case PCI_HEADER_TYPE_NORMAL:
1940 case PCI_HEADER_TYPE_MULTIDEVICE:
1942 case PCI_HEADER_TYPE_BRIDGE:
1945 case PCI_HEADER_TYPE_MULTIBRIDGE:
1946 /* We assume here that only 1 bus behind the bridge
1947 TO DO: add functionality for several:
1949 while (temp < subordinate) {
1954 pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_SECONDARY_BUS, &sec_busno);
1955 bus_sec = find_bus_wprev(sec_busno, NULL, 0);
1956 /* this bus structure doesn't exist yet, PPB was configured during previous loading of ibmphp */
1958 alloc_error_bus(NULL, sec_busno, 1);
1959 /* the rest will be populated during NVRAM call */
1962 pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_IO_BASE, &start_io_address);
1963 pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_IO_LIMIT, &end_io_address);
1964 pci_bus_read_config_word(ibmphp_pci_bus, devfn, PCI_IO_BASE_UPPER16, &upper_io_start);
1965 pci_bus_read_config_word(ibmphp_pci_bus, devfn, PCI_IO_LIMIT_UPPER16, &upper_io_end);
1966 start_address = (start_io_address & PCI_IO_RANGE_MASK) << 8;
1967 start_address |= (upper_io_start << 16);
1968 end_address = (end_io_address & PCI_IO_RANGE_MASK) << 8;
1969 end_address |= (upper_io_end << 16);
1971 if ((start_address) && (start_address <= end_address)) {
1972 range = kzalloc(sizeof(struct range_node), GFP_KERNEL);
1976 range->start = start_address;
1977 range->end = end_address + 0xfff;
1979 if (bus_sec->noIORanges > 0) {
1980 if (!range_exists_already(range, bus_sec, IO)) {
1981 add_bus_range(IO, range, bus_sec);
1982 ++bus_sec->noIORanges;
1988 /* 1st IO Range on the bus */
1990 bus_sec->rangeIO = range;
1991 ++bus_sec->noIORanges;
1993 fix_resources(bus_sec);
1995 if (ibmphp_find_resource(bus_cur, start_address, &io, IO)) {
1996 io = kzalloc(sizeof(struct resource_node), GFP_KERNEL);
2002 io->busno = bus_cur->busno;
2003 io->devfunc = ((device << 3) | (function & 0x7));
2004 io->start = start_address;
2005 io->end = end_address + 0xfff;
2006 io->len = io->end - io->start + 1;
2007 ibmphp_add_resource(io);
2011 pci_bus_read_config_word(ibmphp_pci_bus, devfn, PCI_MEMORY_BASE, &start_mem_address);
2012 pci_bus_read_config_word(ibmphp_pci_bus, devfn, PCI_MEMORY_LIMIT, &end_mem_address);
2014 start_address = 0x00000000 | (start_mem_address & PCI_MEMORY_RANGE_MASK) << 16;
2015 end_address = 0x00000000 | (end_mem_address & PCI_MEMORY_RANGE_MASK) << 16;
2017 if ((start_address) && (start_address <= end_address)) {
2019 range = kzalloc(sizeof(struct range_node), GFP_KERNEL);
2023 range->start = start_address;
2024 range->end = end_address + 0xfffff;
2026 if (bus_sec->noMemRanges > 0) {
2027 if (!range_exists_already(range, bus_sec, MEM)) {
2028 add_bus_range(MEM, range, bus_sec);
2029 ++bus_sec->noMemRanges;
2035 /* 1st Mem Range on the bus */
2037 bus_sec->rangeMem = range;
2038 ++bus_sec->noMemRanges;
2041 fix_resources(bus_sec);
2043 if (ibmphp_find_resource(bus_cur, start_address, &mem, MEM)) {
2044 mem = kzalloc(sizeof(struct resource_node), GFP_KERNEL);
2050 mem->busno = bus_cur->busno;
2051 mem->devfunc = ((device << 3) | (function & 0x7));
2052 mem->start = start_address;
2053 mem->end = end_address + 0xfffff;
2054 mem->len = mem->end - mem->start + 1;
2055 ibmphp_add_resource(mem);
2058 pci_bus_read_config_word(ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_BASE, &start_mem_address);
2059 pci_bus_read_config_word(ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, &end_mem_address);
2060 pci_bus_read_config_dword(ibmphp_pci_bus, devfn, PCI_PREF_BASE_UPPER32, &upper_start);
2061 pci_bus_read_config_dword(ibmphp_pci_bus, devfn, PCI_PREF_LIMIT_UPPER32, &upper_end);
2062 start_address = 0x00000000 | (start_mem_address & PCI_MEMORY_RANGE_MASK) << 16;
2063 end_address = 0x00000000 | (end_mem_address & PCI_MEMORY_RANGE_MASK) << 16;
2064 #if BITS_PER_LONG == 64
2065 start_address |= ((long) upper_start) << 32;
2066 end_address |= ((long) upper_end) << 32;
2069 if ((start_address) && (start_address <= end_address)) {
2071 range = kzalloc(sizeof(struct range_node), GFP_KERNEL);
2075 range->start = start_address;
2076 range->end = end_address + 0xfffff;
2078 if (bus_sec->noPFMemRanges > 0) {
2079 if (!range_exists_already(range, bus_sec, PFMEM)) {
2080 add_bus_range(PFMEM, range, bus_sec);
2081 ++bus_sec->noPFMemRanges;
2087 /* 1st PFMem Range on the bus */
2089 bus_sec->rangePFMem = range;
2090 ++bus_sec->noPFMemRanges;
2093 fix_resources(bus_sec);
2094 if (ibmphp_find_resource(bus_cur, start_address, &pfmem, PFMEM)) {
2095 pfmem = kzalloc(sizeof(struct resource_node), GFP_KERNEL);
2100 pfmem->type = PFMEM;
2101 pfmem->busno = bus_cur->busno;
2102 pfmem->devfunc = ((device << 3) | (function & 0x7));
2103 pfmem->start = start_address;
2104 pfmem->end = end_address + 0xfffff;
2105 pfmem->len = pfmem->end - pfmem->start + 1;
2108 ibmphp_add_resource(pfmem);
2112 } /* end of switch */
2113 } /* end if vendor */
2114 } /* end for function */
2115 } /* end for device */