blob: 2f2fcc8f7f8beb14ca2bed0f556d92c113be49ff [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * IBM Hot Plug Controller Driver
3 *
4 * Written By: Irene Zubarev, IBM Corporation
5 *
6 * Copyright (C) 2001 Greg Kroah-Hartman (greg@kroah.com)
7 * Copyright (C) 2001,2002 IBM Corp.
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 <gregkh@us.ibm.com>
27 *
28 */
29
30#include <linux/module.h>
31#include <linux/slab.h>
32#include <linux/pci.h>
33#include <linux/list.h>
34#include <linux/init.h>
35#include "ibmphp.h"
36
37static int flags = 0; /* for testing */
38
39static void update_resources (struct bus_node *bus_cur, int type, int rangeno);
40static int once_over (void);
41static int remove_ranges (struct bus_node *, struct bus_node *);
42static int update_bridge_ranges (struct bus_node **);
H. Peter Anvinc85e4aa2010-02-10 17:45:09 -080043static int add_bus_range (int type, struct range_node *, struct bus_node *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044static void fix_resources (struct bus_node *);
45static struct bus_node *find_bus_wprev (u8, struct bus_node **, u8);
46
47static LIST_HEAD(gbuses);
48
Ryan Desfosses3c78bc62014-04-18 20:13:49 -040049static struct bus_node * __init alloc_error_bus (struct ebda_pci_rsrc *curr, u8 busno, int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050{
Ryan Desfosses3c78bc62014-04-18 20:13:49 -040051 struct bus_node *newbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53 if (!(curr) && !(flag)) {
54 err ("NULL pointer passed\n");
55 return NULL;
56 }
57
Eric Sesterhennf5afe802006-02-28 15:34:49 +010058 newbus = kzalloc(sizeof(struct bus_node), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 if (!newbus) {
60 err ("out of system memory\n");
61 return NULL;
62 }
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 if (flag)
65 newbus->busno = busno;
66 else
67 newbus->busno = curr->bus_num;
68 list_add_tail (&newbus->bus_list, &gbuses);
69 return newbus;
70}
71
Ryan Desfosses3c78bc62014-04-18 20:13:49 -040072static struct resource_node * __init alloc_resources (struct ebda_pci_rsrc *curr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073{
74 struct resource_node *rs;
Bjorn Helgaasf7625982013-11-14 11:28:18 -070075
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 if (!curr) {
77 err ("NULL passed to allocate\n");
78 return NULL;
79 }
80
Eric Sesterhennf5afe802006-02-28 15:34:49 +010081 rs = kzalloc(sizeof(struct resource_node), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 if (!rs) {
83 err ("out of system memory\n");
84 return NULL;
85 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 rs->busno = curr->bus_num;
87 rs->devfunc = curr->dev_fun;
88 rs->start = curr->start_addr;
89 rs->end = curr->end_addr;
90 rs->len = curr->end_addr - curr->start_addr + 1;
91 return rs;
92}
93
94static 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)
95{
Ryan Desfosses3c78bc62014-04-18 20:13:49 -040096 struct bus_node *newbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 struct range_node *newrange;
98 u8 num_ranges = 0;
99
100 if (first_bus) {
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100101 newbus = kzalloc(sizeof(struct bus_node), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 if (!newbus) {
103 err ("out of system memory.\n");
104 return -ENOMEM;
105 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 newbus->busno = curr->bus_num;
107 } else {
108 newbus = *new_bus;
109 switch (flag) {
110 case MEM:
111 num_ranges = newbus->noMemRanges;
112 break;
113 case PFMEM:
114 num_ranges = newbus->noPFMemRanges;
115 break;
116 case IO:
117 num_ranges = newbus->noIORanges;
118 break;
119 }
120 }
121
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100122 newrange = kzalloc(sizeof(struct range_node), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 if (!newrange) {
124 if (first_bus)
125 kfree (newbus);
126 err ("out of system memory\n");
127 return -ENOMEM;
128 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 newrange->start = curr->start_addr;
130 newrange->end = curr->end_addr;
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 if (first_bus || (!num_ranges))
133 newrange->rangeno = 1;
134 else {
135 /* need to insert our range */
H. Peter Anvinc85e4aa2010-02-10 17:45:09 -0800136 add_bus_range (flag, newrange, newbus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 debug ("%d resource Primary Bus inserted on bus %x [%x - %x]\n", flag, newbus->busno, newrange->start, newrange->end);
138 }
139
140 switch (flag) {
141 case MEM:
142 newbus->rangeMem = newrange;
143 if (first_bus)
144 newbus->noMemRanges = 1;
145 else {
146 debug ("First Memory Primary on bus %x, [%x - %x]\n", newbus->busno, newrange->start, newrange->end);
147 ++newbus->noMemRanges;
148 fix_resources (newbus);
149 }
150 break;
151 case IO:
152 newbus->rangeIO = newrange;
153 if (first_bus)
154 newbus->noIORanges = 1;
155 else {
156 debug ("First IO Primary on bus %x, [%x - %x]\n", newbus->busno, newrange->start, newrange->end);
157 ++newbus->noIORanges;
158 fix_resources (newbus);
159 }
160 break;
161 case PFMEM:
162 newbus->rangePFMem = newrange;
163 if (first_bus)
164 newbus->noPFMemRanges = 1;
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700165 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 debug ("1st PFMemory Primary on Bus %x [%x - %x]\n", newbus->busno, newrange->start, newrange->end);
167 ++newbus->noPFMemRanges;
168 fix_resources (newbus);
169 }
170
171 break;
172 }
173
174 *new_bus = newbus;
175 *new_range = newrange;
176 return 0;
177}
178
179
180/* Notes:
181 * 1. The ranges are ordered. The buses are not ordered. (First come)
182 *
183 * 2. If cannot allocate out of PFMem range, allocate from Mem ranges. PFmemFromMem
184 * are not sorted. (no need since use mem node). To not change the entire code, we
185 * also add mem node whenever this case happens so as not to change
186 * ibmphp_check_mem_resource etc (and since it really is taking Mem resource)
187 */
188
189/*****************************************************************************
190 * This is the Resource Management initialization function. It will go through
191 * the Resource list taken from EBDA and fill in this module's data structures
192 *
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700193 * THIS IS NOT TAKING INTO CONSIDERATION IO RESTRICTIONS OF PRIMARY BUSES,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 * SINCE WE'RE GOING TO ASSUME FOR NOW WE DON'T HAVE THOSE ON OUR BUSES FOR NOW
195 *
196 * Input: ptr to the head of the resource list from EBDA
197 * Output: 0, -1 or error codes
198 ***************************************************************************/
199int __init ibmphp_rsrc_init (void)
200{
201 struct ebda_pci_rsrc *curr;
202 struct range_node *newrange = NULL;
203 struct bus_node *newbus = NULL;
204 struct bus_node *bus_cur;
205 struct bus_node *bus_prev;
206 struct list_head *tmp;
207 struct resource_node *new_io = NULL;
208 struct resource_node *new_mem = NULL;
209 struct resource_node *new_pfmem = NULL;
210 int rc;
211 struct list_head *tmp_ebda;
212
213 list_for_each (tmp_ebda, &ibmphp_ebda_pci_rsrc_head) {
214 curr = list_entry (tmp_ebda, struct ebda_pci_rsrc, ebda_pci_rsrc_list);
215 if (!(curr->rsrc_type & PCIDEVMASK)) {
216 /* EBDA still lists non PCI devices, so ignore... */
217 debug ("this is not a PCI DEVICE in rsrc_init, please take care\n");
218 // continue;
219 }
220
221 /* this is a primary bus resource */
222 if (curr->rsrc_type & PRIMARYBUSMASK) {
223 /* memory */
224 if ((curr->rsrc_type & RESTYPE) == MMASK) {
225 /* no bus structure exists in place yet */
226 if (list_empty (&gbuses)) {
Quentin Lambert79e50e72014-09-07 20:03:32 +0200227 rc = alloc_bus_range(&newbus, &newrange, curr, MEM, 1);
228 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 return rc;
230 list_add_tail (&newbus->bus_list, &gbuses);
231 debug ("gbuses = NULL, Memory Primary Bus %x [%x - %x]\n", newbus->busno, newrange->start, newrange->end);
232 } else {
233 bus_cur = find_bus_wprev (curr->bus_num, &bus_prev, 1);
234 /* found our bus */
235 if (bus_cur) {
236 rc = alloc_bus_range (&bus_cur, &newrange, curr, MEM, 0);
237 if (rc)
238 return rc;
239 } else {
240 /* went through all the buses and didn't find ours, need to create a new bus node */
Quentin Lambert79e50e72014-09-07 20:03:32 +0200241 rc = alloc_bus_range(&newbus, &newrange, curr, MEM, 1);
242 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 return rc;
244
245 list_add_tail (&newbus->bus_list, &gbuses);
246 debug ("New Bus, Memory Primary Bus %x [%x - %x]\n", newbus->busno, newrange->start, newrange->end);
247 }
248 }
249 } else if ((curr->rsrc_type & RESTYPE) == PFMASK) {
250 /* prefetchable memory */
251 if (list_empty (&gbuses)) {
252 /* no bus structure exists in place yet */
Quentin Lambert79e50e72014-09-07 20:03:32 +0200253 rc = alloc_bus_range(&newbus, &newrange, curr, PFMEM, 1);
254 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 return rc;
256 list_add_tail (&newbus->bus_list, &gbuses);
257 debug ("gbuses = NULL, PFMemory Primary Bus %x [%x - %x]\n", newbus->busno, newrange->start, newrange->end);
258 } else {
259 bus_cur = find_bus_wprev (curr->bus_num, &bus_prev, 1);
260 if (bus_cur) {
261 /* found our bus */
262 rc = alloc_bus_range (&bus_cur, &newrange, curr, PFMEM, 0);
263 if (rc)
264 return rc;
265 } else {
266 /* went through all the buses and didn't find ours, need to create a new bus node */
Quentin Lambert79e50e72014-09-07 20:03:32 +0200267 rc = alloc_bus_range(&newbus, &newrange, curr, PFMEM, 1);
268 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 return rc;
270 list_add_tail (&newbus->bus_list, &gbuses);
271 debug ("1st Bus, PFMemory Primary Bus %x [%x - %x]\n", newbus->busno, newrange->start, newrange->end);
272 }
273 }
274 } else if ((curr->rsrc_type & RESTYPE) == IOMASK) {
275 /* IO */
276 if (list_empty (&gbuses)) {
277 /* no bus structure exists in place yet */
Quentin Lambert79e50e72014-09-07 20:03:32 +0200278 rc = alloc_bus_range(&newbus, &newrange, curr, IO, 1);
279 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 return rc;
281 list_add_tail (&newbus->bus_list, &gbuses);
282 debug ("gbuses = NULL, IO Primary Bus %x [%x - %x]\n", newbus->busno, newrange->start, newrange->end);
283 } else {
284 bus_cur = find_bus_wprev (curr->bus_num, &bus_prev, 1);
285 if (bus_cur) {
286 rc = alloc_bus_range (&bus_cur, &newrange, curr, IO, 0);
287 if (rc)
288 return rc;
289 } else {
290 /* went through all the buses and didn't find ours, need to create a new bus node */
Quentin Lambert79e50e72014-09-07 20:03:32 +0200291 rc = alloc_bus_range(&newbus, &newrange, curr, IO, 1);
292 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 return rc;
294 list_add_tail (&newbus->bus_list, &gbuses);
295 debug ("1st Bus, IO Primary Bus %x [%x - %x]\n", newbus->busno, newrange->start, newrange->end);
296 }
297 }
298
299 } else {
300 ; /* type is reserved WHAT TO DO IN THIS CASE???
301 NOTHING TO DO??? */
302 }
303 } else {
304 /* regular pci device resource */
305 if ((curr->rsrc_type & RESTYPE) == MMASK) {
306 /* Memory resource */
307 new_mem = alloc_resources (curr);
308 if (!new_mem)
309 return -ENOMEM;
310 new_mem->type = MEM;
311 /*
312 * if it didn't find the bus, means PCI dev
313 * came b4 the Primary Bus info, so need to
314 * create a bus rangeno becomes a problem...
315 * assign a -1 and then update once the range
316 * actually appears...
317 */
318 if (ibmphp_add_resource (new_mem) < 0) {
319 newbus = alloc_error_bus (curr, 0, 0);
320 if (!newbus)
321 return -ENOMEM;
322 newbus->firstMem = new_mem;
323 ++newbus->needMemUpdate;
324 new_mem->rangeno = -1;
325 }
326 debug ("Memory resource for device %x, bus %x, [%x - %x]\n", new_mem->devfunc, new_mem->busno, new_mem->start, new_mem->end);
327
328 } else if ((curr->rsrc_type & RESTYPE) == PFMASK) {
329 /* PFMemory resource */
330 new_pfmem = alloc_resources (curr);
331 if (!new_pfmem)
332 return -ENOMEM;
333 new_pfmem->type = PFMEM;
Kristen Accardidc6712d2006-03-14 16:24:47 -0800334 new_pfmem->fromMem = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 if (ibmphp_add_resource (new_pfmem) < 0) {
336 newbus = alloc_error_bus (curr, 0, 0);
337 if (!newbus)
338 return -ENOMEM;
339 newbus->firstPFMem = new_pfmem;
340 ++newbus->needPFMemUpdate;
341 new_pfmem->rangeno = -1;
342 }
343
344 debug ("PFMemory resource for device %x, bus %x, [%x - %x]\n", new_pfmem->devfunc, new_pfmem->busno, new_pfmem->start, new_pfmem->end);
345 } else if ((curr->rsrc_type & RESTYPE) == IOMASK) {
346 /* IO resource */
347 new_io = alloc_resources (curr);
348 if (!new_io)
349 return -ENOMEM;
350 new_io->type = IO;
351
352 /*
353 * if it didn't find the bus, means PCI dev
354 * came b4 the Primary Bus info, so need to
355 * create a bus rangeno becomes a problem...
356 * Can assign a -1 and then update once the
357 * range actually appears...
358 */
359 if (ibmphp_add_resource (new_io) < 0) {
360 newbus = alloc_error_bus (curr, 0, 0);
361 if (!newbus)
362 return -ENOMEM;
363 newbus->firstIO = new_io;
364 ++newbus->needIOUpdate;
365 new_io->rangeno = -1;
366 }
367 debug ("IO resource for device %x, bus %x, [%x - %x]\n", new_io->devfunc, new_io->busno, new_io->start, new_io->end);
368 }
369 }
370 }
371
372 list_for_each (tmp, &gbuses) {
373 bus_cur = list_entry (tmp, struct bus_node, bus_list);
374 /* This is to get info about PPB resources, since EBDA doesn't put this info into the primary bus info */
375 rc = update_bridge_ranges (&bus_cur);
376 if (rc)
377 return rc;
378 }
379 rc = once_over (); /* This is to align ranges (so no -1) */
380 if (rc)
381 return rc;
382 return 0;
383}
384
385/********************************************************************************
386 * This function adds a range into a sorted list of ranges per bus for a particular
387 * range type, it then calls another routine to update the range numbers on the
388 * pci devices' resources for the appropriate resource
389 *
390 * Input: type of the resource, range to add, current bus
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700391 * Output: 0 or -1, bus and range ptrs
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 ********************************************************************************/
H. Peter Anvinc85e4aa2010-02-10 17:45:09 -0800393static int add_bus_range (int type, struct range_node *range, struct bus_node *bus_cur)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394{
395 struct range_node *range_cur = NULL;
396 struct range_node *range_prev;
397 int count = 0, i_init;
398 int noRanges = 0;
399
400 switch (type) {
401 case MEM:
402 range_cur = bus_cur->rangeMem;
403 noRanges = bus_cur->noMemRanges;
404 break;
405 case PFMEM:
406 range_cur = bus_cur->rangePFMem;
407 noRanges = bus_cur->noPFMemRanges;
408 break;
409 case IO:
410 range_cur = bus_cur->rangeIO;
411 noRanges = bus_cur->noIORanges;
412 break;
413 }
414
415 range_prev = NULL;
416 while (range_cur) {
417 if (range->start < range_cur->start)
418 break;
419 range_prev = range_cur;
420 range_cur = range_cur->next;
421 count = count + 1;
422 }
423 if (!count) {
424 /* our range will go at the beginning of the list */
425 switch (type) {
426 case MEM:
427 bus_cur->rangeMem = range;
428 break;
429 case PFMEM:
430 bus_cur->rangePFMem = range;
431 break;
432 case IO:
433 bus_cur->rangeIO = range;
434 break;
435 }
436 range->next = range_cur;
437 range->rangeno = 1;
438 i_init = 0;
439 } else if (!range_cur) {
440 /* our range will go at the end of the list */
441 range->next = NULL;
442 range_prev->next = range;
443 range->rangeno = range_prev->rangeno + 1;
444 return 0;
445 } else {
446 /* the range is in the middle */
447 range_prev->next = range;
448 range->next = range_cur;
449 range->rangeno = range_cur->rangeno;
450 i_init = range_prev->rangeno;
451 }
452
453 for (count = i_init; count < noRanges; ++count) {
454 ++range_cur->rangeno;
455 range_cur = range_cur->next;
456 }
457
458 update_resources (bus_cur, type, i_init + 1);
459 return 0;
460}
461
462/*******************************************************************************
463 * This routine goes through the list of resources of type 'type' and updates
H. Peter Anvinc85e4aa2010-02-10 17:45:09 -0800464 * the range numbers that they correspond to. It was called from add_bus_range fnc
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 *
466 * Input: bus, type of the resource, the rangeno starting from which to update
467 ******************************************************************************/
468static void update_resources (struct bus_node *bus_cur, int type, int rangeno)
469{
470 struct resource_node *res = NULL;
Kristen Accardidc6712d2006-03-14 16:24:47 -0800471 u8 eol = 0; /* end of list indicator */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
473 switch (type) {
474 case MEM:
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700475 if (bus_cur->firstMem)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 res = bus_cur->firstMem;
477 break;
478 case PFMEM:
479 if (bus_cur->firstPFMem)
480 res = bus_cur->firstPFMem;
481 break;
482 case IO:
483 if (bus_cur->firstIO)
484 res = bus_cur->firstIO;
485 break;
486 }
487
488 if (res) {
489 while (res) {
490 if (res->rangeno == rangeno)
491 break;
492 if (res->next)
493 res = res->next;
494 else if (res->nextRange)
495 res = res->nextRange;
496 else {
Kristen Accardidc6712d2006-03-14 16:24:47 -0800497 eol = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 break;
499 }
500 }
501
502 if (!eol) {
503 /* found the range */
504 while (res) {
505 ++res->rangeno;
506 res = res->next;
507 }
508 }
509 }
510}
511
512static void fix_me (struct resource_node *res, struct bus_node *bus_cur, struct range_node *range)
513{
514 char * str = "";
515 switch (res->type) {
516 case IO:
517 str = "io";
518 break;
519 case MEM:
520 str = "mem";
521 break;
522 case PFMEM:
523 str = "pfmem";
524 break;
525 }
526
527 while (res) {
528 if (res->rangeno == -1) {
529 while (range) {
530 if ((res->start >= range->start) && (res->end <= range->end)) {
531 res->rangeno = range->rangeno;
532 debug ("%s->rangeno in fix_resources is %d\n", str, res->rangeno);
533 switch (res->type) {
534 case IO:
535 --bus_cur->needIOUpdate;
536 break;
537 case MEM:
538 --bus_cur->needMemUpdate;
539 break;
540 case PFMEM:
541 --bus_cur->needPFMemUpdate;
542 break;
543 }
544 break;
545 }
546 range = range->next;
547 }
548 }
549 if (res->next)
550 res = res->next;
551 else
552 res = res->nextRange;
553 }
554
555}
556
557/*****************************************************************************
558 * This routine reassigns the range numbers to the resources that had a -1
559 * This case can happen only if upon initialization, resources taken by pci dev
560 * appear in EBDA before the resources allocated for that bus, since we don't
561 * know the range, we assign -1, and this routine is called after a new range
562 * is assigned to see the resources with unknown range belong to the added range
563 *
564 * Input: current bus
565 * Output: none, list of resources for that bus are fixed if can be
566 *******************************************************************************/
567static void fix_resources (struct bus_node *bus_cur)
568{
569 struct range_node *range;
570 struct resource_node *res;
571
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800572 debug ("%s - bus_cur->busno = %d\n", __func__, bus_cur->busno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
574 if (bus_cur->needIOUpdate) {
575 res = bus_cur->firstIO;
576 range = bus_cur->rangeIO;
577 fix_me (res, bus_cur, range);
578 }
579 if (bus_cur->needMemUpdate) {
580 res = bus_cur->firstMem;
581 range = bus_cur->rangeMem;
582 fix_me (res, bus_cur, range);
583 }
584 if (bus_cur->needPFMemUpdate) {
585 res = bus_cur->firstPFMem;
586 range = bus_cur->rangePFMem;
587 fix_me (res, bus_cur, range);
588 }
589}
590
591/*******************************************************************************
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700592 * This routine adds a resource to the list of resources to the appropriate bus
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 * based on their resource type and sorted by their starting addresses. It assigns
594 * the ptrs to next and nextRange if needed.
595 *
596 * Input: resource ptr
597 * Output: ptrs assigned (to the node)
598 * 0 or -1
599 *******************************************************************************/
600int ibmphp_add_resource (struct resource_node *res)
601{
602 struct resource_node *res_cur;
603 struct resource_node *res_prev;
604 struct bus_node *bus_cur;
605 struct range_node *range_cur = NULL;
606 struct resource_node *res_start = NULL;
607
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800608 debug ("%s - enter\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
610 if (!res) {
611 err ("NULL passed to add\n");
612 return -ENODEV;
613 }
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700614
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 bus_cur = find_bus_wprev (res->busno, NULL, 0);
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700616
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 if (!bus_cur) {
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700618 /* didn't find a bus, something's wrong!!! */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 debug ("no bus in the system, either pci_dev's wrong or allocation failed\n");
620 return -ENODEV;
621 }
622
623 /* Normal case */
624 switch (res->type) {
625 case IO:
626 range_cur = bus_cur->rangeIO;
627 res_start = bus_cur->firstIO;
628 break;
629 case MEM:
630 range_cur = bus_cur->rangeMem;
631 res_start = bus_cur->firstMem;
632 break;
633 case PFMEM:
634 range_cur = bus_cur->rangePFMem;
635 res_start = bus_cur->firstPFMem;
636 break;
637 default:
638 err ("cannot read the type of the resource to add... problem\n");
639 return -EINVAL;
640 }
641 while (range_cur) {
642 if ((res->start >= range_cur->start) && (res->end <= range_cur->end)) {
643 res->rangeno = range_cur->rangeno;
644 break;
645 }
646 range_cur = range_cur->next;
647 }
648
649 /* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
650 * this is again the case of rangeno = -1
651 * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
652 */
653
654 if (!range_cur) {
655 switch (res->type) {
656 case IO:
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700657 ++bus_cur->needIOUpdate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 break;
659 case MEM:
660 ++bus_cur->needMemUpdate;
661 break;
662 case PFMEM:
663 ++bus_cur->needPFMemUpdate;
664 break;
665 }
666 res->rangeno = -1;
667 }
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700668
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 debug ("The range is %d\n", res->rangeno);
670 if (!res_start) {
671 /* no first{IO,Mem,Pfmem} on the bus, 1st IO/Mem/Pfmem resource ever */
672 switch (res->type) {
673 case IO:
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700674 bus_cur->firstIO = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 break;
676 case MEM:
677 bus_cur->firstMem = res;
678 break;
679 case PFMEM:
680 bus_cur->firstPFMem = res;
681 break;
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700682 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 res->next = NULL;
684 res->nextRange = NULL;
685 } else {
686 res_cur = res_start;
687 res_prev = NULL;
688
689 debug ("res_cur->rangeno is %d\n", res_cur->rangeno);
690
691 while (res_cur) {
692 if (res_cur->rangeno >= res->rangeno)
693 break;
694 res_prev = res_cur;
695 if (res_cur->next)
696 res_cur = res_cur->next;
697 else
698 res_cur = res_cur->nextRange;
699 }
700
701 if (!res_cur) {
702 /* at the end of the resource list */
703 debug ("i should be here, [%x - %x]\n", res->start, res->end);
704 res_prev->nextRange = res;
705 res->next = NULL;
706 res->nextRange = NULL;
707 } else if (res_cur->rangeno == res->rangeno) {
708 /* in the same range */
709 while (res_cur) {
710 if (res->start < res_cur->start)
711 break;
712 res_prev = res_cur;
713 res_cur = res_cur->next;
714 }
715 if (!res_cur) {
716 /* the last resource in this range */
717 res_prev->next = res;
718 res->next = NULL;
719 res->nextRange = res_prev->nextRange;
720 res_prev->nextRange = NULL;
721 } else if (res->start < res_cur->start) {
722 /* at the beginning or middle of the range */
723 if (!res_prev) {
724 switch (res->type) {
725 case IO:
726 bus_cur->firstIO = res;
727 break;
728 case MEM:
729 bus_cur->firstMem = res;
730 break;
731 case PFMEM:
732 bus_cur->firstPFMem = res;
733 break;
734 }
735 } else if (res_prev->rangeno == res_cur->rangeno)
736 res_prev->next = res;
737 else
738 res_prev->nextRange = res;
739
740 res->next = res_cur;
741 res->nextRange = NULL;
742 }
743 } else {
744 /* this is the case where it is 1st occurrence of the range */
745 if (!res_prev) {
746 /* at the beginning of the resource list */
747 res->next = NULL;
748 switch (res->type) {
749 case IO:
750 res->nextRange = bus_cur->firstIO;
751 bus_cur->firstIO = res;
752 break;
753 case MEM:
754 res->nextRange = bus_cur->firstMem;
755 bus_cur->firstMem = res;
756 break;
757 case PFMEM:
758 res->nextRange = bus_cur->firstPFMem;
759 bus_cur->firstPFMem = res;
760 break;
761 }
762 } else if (res_cur->rangeno > res->rangeno) {
763 /* in the middle of the resource list */
764 res_prev->nextRange = res;
765 res->next = NULL;
766 res->nextRange = res_cur;
767 }
768 }
769 }
770
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800771 debug ("%s - exit\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 return 0;
773}
774
775/****************************************************************************
776 * This routine will remove the resource from the list of resources
777 *
778 * Input: io, mem, and/or pfmem resource to be deleted
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700779 * Output: modified resource list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 * 0 or error code
781 ****************************************************************************/
782int ibmphp_remove_resource (struct resource_node *res)
783{
784 struct bus_node *bus_cur;
785 struct resource_node *res_cur = NULL;
786 struct resource_node *res_prev;
787 struct resource_node *mem_cur;
788 char * type = "";
789
790 if (!res) {
791 err ("resource to remove is NULL\n");
792 return -ENODEV;
793 }
794
795 bus_cur = find_bus_wprev (res->busno, NULL, 0);
796
797 if (!bus_cur) {
Ryan Desfosses227f0642014-04-18 20:13:50 -0400798 err ("cannot find corresponding bus of the io resource to remove bailing out...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 return -ENODEV;
800 }
801
802 switch (res->type) {
803 case IO:
804 res_cur = bus_cur->firstIO;
805 type = "io";
806 break;
807 case MEM:
808 res_cur = bus_cur->firstMem;
809 type = "mem";
810 break;
811 case PFMEM:
812 res_cur = bus_cur->firstPFMem;
813 type = "pfmem";
814 break;
815 default:
816 err ("unknown type for resource to remove\n");
817 return -EINVAL;
818 }
819 res_prev = NULL;
820
821 while (res_cur) {
822 if ((res_cur->start == res->start) && (res_cur->end == res->end))
823 break;
824 res_prev = res_cur;
825 if (res_cur->next)
826 res_cur = res_cur->next;
827 else
828 res_cur = res_cur->nextRange;
829 }
830
831 if (!res_cur) {
832 if (res->type == PFMEM) {
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700833 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 * case where pfmem might be in the PFMemFromMem list
835 * so will also need to remove the corresponding mem
836 * entry
837 */
838 res_cur = bus_cur->firstPFMemFromMem;
839 res_prev = NULL;
840
841 while (res_cur) {
842 if ((res_cur->start == res->start) && (res_cur->end == res->end)) {
843 mem_cur = bus_cur->firstMem;
844 while (mem_cur) {
845 if ((mem_cur->start == res_cur->start)
846 && (mem_cur->end == res_cur->end))
847 break;
848 if (mem_cur->next)
849 mem_cur = mem_cur->next;
850 else
851 mem_cur = mem_cur->nextRange;
852 }
853 if (!mem_cur) {
854 err ("cannot find corresponding mem node for pfmem...\n");
855 return -EINVAL;
856 }
857
858 ibmphp_remove_resource (mem_cur);
859 if (!res_prev)
860 bus_cur->firstPFMemFromMem = res_cur->next;
861 else
862 res_prev->next = res_cur->next;
863 kfree (res_cur);
864 return 0;
865 }
866 res_prev = res_cur;
867 if (res_cur->next)
868 res_cur = res_cur->next;
869 else
870 res_cur = res_cur->nextRange;
871 }
872 if (!res_cur) {
873 err ("cannot find pfmem to delete...\n");
874 return -EINVAL;
875 }
876 } else {
877 err ("the %s resource is not in the list to be deleted...\n", type);
878 return -EINVAL;
879 }
880 }
881 if (!res_prev) {
882 /* first device to be deleted */
883 if (res_cur->next) {
884 switch (res->type) {
885 case IO:
886 bus_cur->firstIO = res_cur->next;
887 break;
888 case MEM:
889 bus_cur->firstMem = res_cur->next;
890 break;
891 case PFMEM:
892 bus_cur->firstPFMem = res_cur->next;
893 break;
894 }
895 } else if (res_cur->nextRange) {
896 switch (res->type) {
897 case IO:
898 bus_cur->firstIO = res_cur->nextRange;
899 break;
900 case MEM:
901 bus_cur->firstMem = res_cur->nextRange;
902 break;
903 case PFMEM:
904 bus_cur->firstPFMem = res_cur->nextRange;
905 break;
906 }
907 } else {
908 switch (res->type) {
909 case IO:
910 bus_cur->firstIO = NULL;
911 break;
912 case MEM:
913 bus_cur->firstMem = NULL;
914 break;
915 case PFMEM:
916 bus_cur->firstPFMem = NULL;
917 break;
918 }
919 }
920 kfree (res_cur);
921 return 0;
922 } else {
923 if (res_cur->next) {
924 if (res_prev->rangeno == res_cur->rangeno)
925 res_prev->next = res_cur->next;
926 else
927 res_prev->nextRange = res_cur->next;
928 } else if (res_cur->nextRange) {
929 res_prev->next = NULL;
930 res_prev->nextRange = res_cur->nextRange;
931 } else {
932 res_prev->next = NULL;
933 res_prev->nextRange = NULL;
934 }
935 kfree (res_cur);
936 return 0;
937 }
938
939 return 0;
940}
941
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400942static struct range_node *find_range (struct bus_node *bus_cur, struct resource_node *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943{
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400944 struct range_node *range = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
946 switch (res->type) {
947 case IO:
948 range = bus_cur->rangeIO;
949 break;
950 case MEM:
951 range = bus_cur->rangeMem;
952 break;
953 case PFMEM:
954 range = bus_cur->rangePFMem;
955 break;
956 default:
957 err ("cannot read resource type in find_range\n");
958 }
959
960 while (range) {
961 if (res->rangeno == range->rangeno)
962 break;
963 range = range->next;
964 }
965 return range;
966}
967
968/*****************************************************************************
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700969 * This routine will check to make sure the io/mem/pfmem->len that the device asked for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 * can fit w/i our list of available IO/MEM/PFMEM resources. If cannot, returns -EINVAL,
971 * otherwise, returns 0
972 *
973 * Input: resource
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700974 * Output: the correct start and end address are inputted into the resource node,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 * 0 or -EINVAL
976 *****************************************************************************/
977int ibmphp_check_resource (struct resource_node *res, u8 bridge)
978{
979 struct bus_node *bus_cur;
980 struct range_node *range = NULL;
981 struct resource_node *res_prev;
982 struct resource_node *res_cur = NULL;
983 u32 len_cur = 0, start_cur = 0, len_tmp = 0;
984 int noranges = 0;
985 u32 tmp_start; /* this is to make sure start address is divisible by the length needed */
986 u32 tmp_divide;
Kristen Accardidc6712d2006-03-14 16:24:47 -0800987 u8 flag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
989 if (!res)
990 return -EINVAL;
991
992 if (bridge) {
993 /* The rules for bridges are different, 4K divisible for IO, 1M for (pf)mem*/
994 if (res->type == IO)
995 tmp_divide = IOBRIDGE;
996 else
997 tmp_divide = MEMBRIDGE;
998 } else
999 tmp_divide = res->len;
1000
1001 bus_cur = find_bus_wprev (res->busno, NULL, 0);
1002
1003 if (!bus_cur) {
Bjorn Helgaasf7625982013-11-14 11:28:18 -07001004 /* didn't find a bus, something's wrong!!! */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 debug ("no bus in the system, either pci_dev's wrong or allocation failed\n");
1006 return -EINVAL;
1007 }
1008
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001009 debug ("%s - enter\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 debug ("bus_cur->busno is %d\n", bus_cur->busno);
1011
1012 /* This is a quick fix to not mess up with the code very much. i.e.,
1013 * 2000-2fff, len = 1000, but when we compare, we need it to be fff */
1014 res->len -= 1;
1015
1016 switch (res->type) {
1017 case IO:
1018 res_cur = bus_cur->firstIO;
1019 noranges = bus_cur->noIORanges;
1020 break;
1021 case MEM:
1022 res_cur = bus_cur->firstMem;
1023 noranges = bus_cur->noMemRanges;
1024 break;
1025 case PFMEM:
1026 res_cur = bus_cur->firstPFMem;
1027 noranges = bus_cur->noPFMemRanges;
1028 break;
1029 default:
1030 err ("wrong type of resource to check\n");
1031 return -EINVAL;
1032 }
1033 res_prev = NULL;
1034
1035 while (res_cur) {
1036 range = find_range (bus_cur, res_cur);
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001037 debug ("%s - rangeno = %d\n", __func__, res_cur->rangeno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
1039 if (!range) {
1040 err ("no range for the device exists... bailing out...\n");
1041 return -EINVAL;
1042 }
1043
1044 /* found our range */
1045 if (!res_prev) {
1046 /* first time in the loop */
1047 if ((res_cur->start != range->start) && ((len_tmp = res_cur->start - 1 - range->start) >= res->len)) {
1048 debug ("len_tmp = %x\n", len_tmp);
1049
1050 if ((len_tmp < len_cur) || (len_cur == 0)) {
1051
1052 if ((range->start % tmp_divide) == 0) {
1053 /* just perfect, starting address is divisible by length */
Kristen Accardidc6712d2006-03-14 16:24:47 -08001054 flag = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 len_cur = len_tmp;
1056 start_cur = range->start;
1057 } else {
1058 /* Needs adjusting */
1059 tmp_start = range->start;
Kristen Accardidc6712d2006-03-14 16:24:47 -08001060 flag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
1062 while ((len_tmp = res_cur->start - 1 - tmp_start) >= res->len) {
1063 if ((tmp_start % tmp_divide) == 0) {
Kristen Accardidc6712d2006-03-14 16:24:47 -08001064 flag = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 len_cur = len_tmp;
1066 start_cur = tmp_start;
1067 break;
1068 }
1069 tmp_start += tmp_divide - tmp_start % tmp_divide;
1070 if (tmp_start >= res_cur->start - 1)
1071 break;
1072 }
1073 }
Bjorn Helgaasf7625982013-11-14 11:28:18 -07001074
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 if (flag && len_cur == res->len) {
1076 debug ("but we are not here, right?\n");
1077 res->start = start_cur;
1078 res->len += 1; /* To restore the balance */
1079 res->end = res->start + res->len - 1;
1080 return 0;
1081 }
1082 }
1083 }
1084 }
1085 if (!res_cur->next) {
1086 /* last device on the range */
1087 if ((range->end != res_cur->end) && ((len_tmp = range->end - (res_cur->end + 1)) >= res->len)) {
1088 debug ("len_tmp = %x\n", len_tmp);
1089 if ((len_tmp < len_cur) || (len_cur == 0)) {
1090
1091 if (((res_cur->end + 1) % tmp_divide) == 0) {
1092 /* just perfect, starting address is divisible by length */
Kristen Accardidc6712d2006-03-14 16:24:47 -08001093 flag = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 len_cur = len_tmp;
1095 start_cur = res_cur->end + 1;
1096 } else {
1097 /* Needs adjusting */
1098 tmp_start = res_cur->end + 1;
Kristen Accardidc6712d2006-03-14 16:24:47 -08001099 flag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
1101 while ((len_tmp = range->end - tmp_start) >= res->len) {
1102 if ((tmp_start % tmp_divide) == 0) {
Kristen Accardidc6712d2006-03-14 16:24:47 -08001103 flag = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 len_cur = len_tmp;
1105 start_cur = tmp_start;
1106 break;
1107 }
1108 tmp_start += tmp_divide - tmp_start % tmp_divide;
1109 if (tmp_start >= range->end)
1110 break;
1111 }
1112 }
1113 if (flag && len_cur == res->len) {
1114 res->start = start_cur;
1115 res->len += 1; /* To restore the balance */
1116 res->end = res->start + res->len - 1;
1117 return 0;
1118 }
1119 }
1120 }
1121 }
1122
1123 if (res_prev) {
1124 if (res_prev->rangeno != res_cur->rangeno) {
1125 /* 1st device on this range */
Bjorn Helgaasf7625982013-11-14 11:28:18 -07001126 if ((res_cur->start != range->start) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 ((len_tmp = res_cur->start - 1 - range->start) >= res->len)) {
1128 if ((len_tmp < len_cur) || (len_cur == 0)) {
Bjorn Helgaasf7625982013-11-14 11:28:18 -07001129 if ((range->start % tmp_divide) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 /* just perfect, starting address is divisible by length */
Kristen Accardidc6712d2006-03-14 16:24:47 -08001131 flag = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 len_cur = len_tmp;
1133 start_cur = range->start;
1134 } else {
1135 /* Needs adjusting */
1136 tmp_start = range->start;
Kristen Accardidc6712d2006-03-14 16:24:47 -08001137 flag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
1139 while ((len_tmp = res_cur->start - 1 - tmp_start) >= res->len) {
1140 if ((tmp_start % tmp_divide) == 0) {
Kristen Accardidc6712d2006-03-14 16:24:47 -08001141 flag = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 len_cur = len_tmp;
1143 start_cur = tmp_start;
1144 break;
1145 }
1146 tmp_start += tmp_divide - tmp_start % tmp_divide;
1147 if (tmp_start >= res_cur->start - 1)
1148 break;
1149 }
1150 }
1151
1152 if (flag && len_cur == res->len) {
1153 res->start = start_cur;
1154 res->len += 1; /* To restore the balance */
1155 res->end = res->start + res->len - 1;
1156 return 0;
1157 }
1158 }
1159 }
1160 } else {
1161 /* in the same range */
Quentin Lambert79e50e72014-09-07 20:03:32 +02001162 len_tmp = res_cur->start - 1 - res_prev->end - 1;
1163
1164 if (len_tmp >= res->len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 if ((len_tmp < len_cur) || (len_cur == 0)) {
1166 if (((res_prev->end + 1) % tmp_divide) == 0) {
1167 /* just perfect, starting address's divisible by length */
Kristen Accardidc6712d2006-03-14 16:24:47 -08001168 flag = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 len_cur = len_tmp;
1170 start_cur = res_prev->end + 1;
1171 } else {
1172 /* Needs adjusting */
1173 tmp_start = res_prev->end + 1;
Kristen Accardidc6712d2006-03-14 16:24:47 -08001174 flag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
1176 while ((len_tmp = res_cur->start - 1 - tmp_start) >= res->len) {
1177 if ((tmp_start % tmp_divide) == 0) {
Kristen Accardidc6712d2006-03-14 16:24:47 -08001178 flag = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 len_cur = len_tmp;
1180 start_cur = tmp_start;
1181 break;
1182 }
1183 tmp_start += tmp_divide - tmp_start % tmp_divide;
1184 if (tmp_start >= res_cur->start - 1)
1185 break;
1186 }
1187 }
1188
1189 if (flag && len_cur == res->len) {
1190 res->start = start_cur;
1191 res->len += 1; /* To restore the balance */
1192 res->end = res->start + res->len - 1;
1193 return 0;
1194 }
1195 }
1196 }
1197 }
1198 }
1199 /* end if (res_prev) */
1200 res_prev = res_cur;
1201 if (res_cur->next)
1202 res_cur = res_cur->next;
1203 else
1204 res_cur = res_cur->nextRange;
1205 } /* end of while */
1206
1207
1208 if (!res_prev) {
1209 /* 1st device ever */
1210 /* need to find appropriate range */
1211 switch (res->type) {
1212 case IO:
1213 range = bus_cur->rangeIO;
1214 break;
1215 case MEM:
1216 range = bus_cur->rangeMem;
1217 break;
1218 case PFMEM:
1219 range = bus_cur->rangePFMem;
1220 break;
1221 }
1222 while (range) {
Quentin Lambert79e50e72014-09-07 20:03:32 +02001223 len_tmp = range->end - range->start;
1224
1225 if (len_tmp >= res->len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 if ((len_tmp < len_cur) || (len_cur == 0)) {
1227 if ((range->start % tmp_divide) == 0) {
1228 /* just perfect, starting address's divisible by length */
Kristen Accardidc6712d2006-03-14 16:24:47 -08001229 flag = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 len_cur = len_tmp;
1231 start_cur = range->start;
1232 } else {
1233 /* Needs adjusting */
1234 tmp_start = range->start;
Kristen Accardidc6712d2006-03-14 16:24:47 -08001235 flag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
1237 while ((len_tmp = range->end - tmp_start) >= res->len) {
1238 if ((tmp_start % tmp_divide) == 0) {
Kristen Accardidc6712d2006-03-14 16:24:47 -08001239 flag = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 len_cur = len_tmp;
1241 start_cur = tmp_start;
1242 break;
1243 }
1244 tmp_start += tmp_divide - tmp_start % tmp_divide;
1245 if (tmp_start >= range->end)
1246 break;
1247 }
1248 }
1249
1250 if (flag && len_cur == res->len) {
1251 res->start = start_cur;
1252 res->len += 1; /* To restore the balance */
1253 res->end = res->start + res->len - 1;
1254 return 0;
1255 }
1256 }
1257 }
1258 range = range->next;
1259 } /* end of while */
1260
1261 if ((!range) && (len_cur == 0)) {
1262 /* have gone through the list of devices and ranges and haven't found n.e.thing */
1263 err ("no appropriate range.. bailing out...\n");
1264 return -EINVAL;
1265 } else if (len_cur) {
1266 res->start = start_cur;
1267 res->len += 1; /* To restore the balance */
1268 res->end = res->start + res->len - 1;
1269 return 0;
1270 }
1271 }
1272
1273 if (!res_cur) {
1274 debug ("prev->rangeno = %d, noranges = %d\n", res_prev->rangeno, noranges);
1275 if (res_prev->rangeno < noranges) {
1276 /* if there're more ranges out there to check */
1277 switch (res->type) {
1278 case IO:
1279 range = bus_cur->rangeIO;
1280 break;
1281 case MEM:
1282 range = bus_cur->rangeMem;
1283 break;
1284 case PFMEM:
1285 range = bus_cur->rangePFMem;
1286 break;
1287 }
1288 while (range) {
Quentin Lambert79e50e72014-09-07 20:03:32 +02001289 len_tmp = range->end - range->start;
1290
1291 if (len_tmp >= res->len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 if ((len_tmp < len_cur) || (len_cur == 0)) {
1293 if ((range->start % tmp_divide) == 0) {
1294 /* just perfect, starting address's divisible by length */
Kristen Accardidc6712d2006-03-14 16:24:47 -08001295 flag = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 len_cur = len_tmp;
1297 start_cur = range->start;
1298 } else {
1299 /* Needs adjusting */
1300 tmp_start = range->start;
Kristen Accardidc6712d2006-03-14 16:24:47 -08001301 flag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
1303 while ((len_tmp = range->end - tmp_start) >= res->len) {
1304 if ((tmp_start % tmp_divide) == 0) {
Kristen Accardidc6712d2006-03-14 16:24:47 -08001305 flag = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 len_cur = len_tmp;
1307 start_cur = tmp_start;
1308 break;
1309 }
1310 tmp_start += tmp_divide - tmp_start % tmp_divide;
1311 if (tmp_start >= range->end)
1312 break;
1313 }
1314 }
1315
1316 if (flag && len_cur == res->len) {
1317 res->start = start_cur;
1318 res->len += 1; /* To restore the balance */
1319 res->end = res->start + res->len - 1;
1320 return 0;
1321 }
1322 }
1323 }
1324 range = range->next;
1325 } /* end of while */
1326
1327 if ((!range) && (len_cur == 0)) {
1328 /* have gone through the list of devices and ranges and haven't found n.e.thing */
1329 err ("no appropriate range.. bailing out...\n");
1330 return -EINVAL;
1331 } else if (len_cur) {
1332 res->start = start_cur;
1333 res->len += 1; /* To restore the balance */
1334 res->end = res->start + res->len - 1;
1335 return 0;
1336 }
1337 } else {
1338 /* no more ranges to check on */
1339 if (len_cur) {
1340 res->start = start_cur;
1341 res->len += 1; /* To restore the balance */
1342 res->end = res->start + res->len - 1;
1343 return 0;
1344 } else {
1345 /* have gone through the list of devices and haven't found n.e.thing */
1346 err ("no appropriate range.. bailing out...\n");
1347 return -EINVAL;
1348 }
1349 }
Quentin Lambert382a9c92014-09-07 20:02:04 +02001350 } /* end if (!res_cur) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 return -EINVAL;
1352}
1353
1354/********************************************************************************
1355 * This routine is called from remove_card if the card contained PPB.
1356 * It will remove all the resources on the bus as well as the bus itself
1357 * Input: Bus
Bjorn Helgaasf7625982013-11-14 11:28:18 -07001358 * Output: 0, -ENODEV
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 ********************************************************************************/
1360int ibmphp_remove_bus (struct bus_node *bus, u8 parent_busno)
1361{
1362 struct resource_node *res_cur;
1363 struct resource_node *res_tmp;
1364 struct bus_node *prev_bus;
1365 int rc;
1366
Bjorn Helgaasf7625982013-11-14 11:28:18 -07001367 prev_bus = find_bus_wprev (parent_busno, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
1369 if (!prev_bus) {
1370 debug ("something terribly wrong. Cannot find parent bus to the one to remove\n");
1371 return -ENODEV;
1372 }
1373
1374 debug ("In ibmphp_remove_bus... prev_bus->busno is %x\n", prev_bus->busno);
1375
1376 rc = remove_ranges (bus, prev_bus);
1377 if (rc)
1378 return rc;
1379
1380 if (bus->firstIO) {
1381 res_cur = bus->firstIO;
1382 while (res_cur) {
1383 res_tmp = res_cur;
1384 if (res_cur->next)
1385 res_cur = res_cur->next;
1386 else
1387 res_cur = res_cur->nextRange;
1388 kfree (res_tmp);
1389 res_tmp = NULL;
1390 }
1391 bus->firstIO = NULL;
1392 }
1393 if (bus->firstMem) {
1394 res_cur = bus->firstMem;
1395 while (res_cur) {
1396 res_tmp = res_cur;
1397 if (res_cur->next)
1398 res_cur = res_cur->next;
1399 else
1400 res_cur = res_cur->nextRange;
1401 kfree (res_tmp);
1402 res_tmp = NULL;
1403 }
1404 bus->firstMem = NULL;
1405 }
1406 if (bus->firstPFMem) {
1407 res_cur = bus->firstPFMem;
1408 while (res_cur) {
1409 res_tmp = res_cur;
1410 if (res_cur->next)
1411 res_cur = res_cur->next;
1412 else
1413 res_cur = res_cur->nextRange;
1414 kfree (res_tmp);
1415 res_tmp = NULL;
1416 }
1417 bus->firstPFMem = NULL;
1418 }
1419
1420 if (bus->firstPFMemFromMem) {
1421 res_cur = bus->firstPFMemFromMem;
1422 while (res_cur) {
1423 res_tmp = res_cur;
1424 res_cur = res_cur->next;
1425
1426 kfree (res_tmp);
1427 res_tmp = NULL;
1428 }
1429 bus->firstPFMemFromMem = NULL;
1430 }
1431
1432 list_del (&bus->bus_list);
1433 kfree (bus);
1434 return 0;
1435}
1436
1437/******************************************************************************
Bjorn Helgaasf7625982013-11-14 11:28:18 -07001438 * This routine deletes the ranges from a given bus, and the entries from the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 * parent's bus in the resources
1440 * Input: current bus, previous bus
1441 * Output: 0, -EINVAL
1442 ******************************************************************************/
1443static int remove_ranges (struct bus_node *bus_cur, struct bus_node *bus_prev)
1444{
1445 struct range_node *range_cur;
1446 struct range_node *range_tmp;
1447 int i;
1448 struct resource_node *res = NULL;
1449
1450 if (bus_cur->noIORanges) {
1451 range_cur = bus_cur->rangeIO;
1452 for (i = 0; i < bus_cur->noIORanges; i++) {
1453 if (ibmphp_find_resource (bus_prev, range_cur->start, &res, IO) < 0)
1454 return -EINVAL;
1455 ibmphp_remove_resource (res);
1456
1457 range_tmp = range_cur;
1458 range_cur = range_cur->next;
1459 kfree (range_tmp);
1460 range_tmp = NULL;
1461 }
1462 bus_cur->rangeIO = NULL;
1463 }
1464 if (bus_cur->noMemRanges) {
1465 range_cur = bus_cur->rangeMem;
1466 for (i = 0; i < bus_cur->noMemRanges; i++) {
Bjorn Helgaasf7625982013-11-14 11:28:18 -07001467 if (ibmphp_find_resource (bus_prev, range_cur->start, &res, MEM) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 return -EINVAL;
1469
1470 ibmphp_remove_resource (res);
1471 range_tmp = range_cur;
1472 range_cur = range_cur->next;
1473 kfree (range_tmp);
1474 range_tmp = NULL;
1475 }
1476 bus_cur->rangeMem = NULL;
1477 }
1478 if (bus_cur->noPFMemRanges) {
1479 range_cur = bus_cur->rangePFMem;
1480 for (i = 0; i < bus_cur->noPFMemRanges; i++) {
Bjorn Helgaasf7625982013-11-14 11:28:18 -07001481 if (ibmphp_find_resource (bus_prev, range_cur->start, &res, PFMEM) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 return -EINVAL;
1483
1484 ibmphp_remove_resource (res);
1485 range_tmp = range_cur;
1486 range_cur = range_cur->next;
1487 kfree (range_tmp);
1488 range_tmp = NULL;
1489 }
1490 bus_cur->rangePFMem = NULL;
1491 }
1492 return 0;
1493}
1494
1495/*
Bjorn Helgaasf7625982013-11-14 11:28:18 -07001496 * find the resource node in the bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 * Input: Resource needed, start address of the resource, type of resource
1498 */
1499int ibmphp_find_resource (struct bus_node *bus, u32 start_address, struct resource_node **res, int flag)
1500{
1501 struct resource_node *res_cur = NULL;
1502 char * type = "";
1503
1504 if (!bus) {
1505 err ("The bus passed in NULL to find resource\n");
1506 return -ENODEV;
1507 }
1508
1509 switch (flag) {
1510 case IO:
1511 res_cur = bus->firstIO;
1512 type = "io";
1513 break;
1514 case MEM:
1515 res_cur = bus->firstMem;
1516 type = "mem";
1517 break;
1518 case PFMEM:
1519 res_cur = bus->firstPFMem;
1520 type = "pfmem";
1521 break;
1522 default:
1523 err ("wrong type of flag\n");
1524 return -EINVAL;
1525 }
Bjorn Helgaasf7625982013-11-14 11:28:18 -07001526
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 while (res_cur) {
1528 if (res_cur->start == start_address) {
1529 *res = res_cur;
1530 break;
1531 }
1532 if (res_cur->next)
1533 res_cur = res_cur->next;
1534 else
1535 res_cur = res_cur->nextRange;
1536 }
1537
1538 if (!res_cur) {
1539 if (flag == PFMEM) {
1540 res_cur = bus->firstPFMemFromMem;
1541 while (res_cur) {
1542 if (res_cur->start == start_address) {
1543 *res = res_cur;
1544 break;
1545 }
1546 res_cur = res_cur->next;
1547 }
1548 if (!res_cur) {
1549 debug ("SOS...cannot find %s resource in the bus.\n", type);
1550 return -EINVAL;
1551 }
1552 } else {
1553 debug ("SOS... cannot find %s resource in the bus.\n", type);
1554 return -EINVAL;
1555 }
1556 }
1557
1558 if (*res)
1559 debug ("*res->start = %x\n", (*res)->start);
1560
1561 return 0;
1562}
1563
1564/***********************************************************************
1565 * This routine will free the resource structures used by the
1566 * system. It is called from cleanup routine for the module
1567 * Parameters: none
1568 * Returns: none
1569 ***********************************************************************/
1570void ibmphp_free_resources (void)
1571{
1572 struct bus_node *bus_cur = NULL;
1573 struct bus_node *bus_tmp;
1574 struct range_node *range_cur;
1575 struct range_node *range_tmp;
1576 struct resource_node *res_cur;
1577 struct resource_node *res_tmp;
1578 struct list_head *tmp;
1579 struct list_head *next;
1580 int i = 0;
1581 flags = 1;
1582
1583 list_for_each_safe (tmp, next, &gbuses) {
1584 bus_cur = list_entry (tmp, struct bus_node, bus_list);
1585 if (bus_cur->noIORanges) {
1586 range_cur = bus_cur->rangeIO;
1587 for (i = 0; i < bus_cur->noIORanges; i++) {
1588 if (!range_cur)
1589 break;
1590 range_tmp = range_cur;
1591 range_cur = range_cur->next;
1592 kfree (range_tmp);
1593 range_tmp = NULL;
1594 }
1595 }
1596 if (bus_cur->noMemRanges) {
1597 range_cur = bus_cur->rangeMem;
1598 for (i = 0; i < bus_cur->noMemRanges; i++) {
1599 if (!range_cur)
1600 break;
1601 range_tmp = range_cur;
1602 range_cur = range_cur->next;
1603 kfree (range_tmp);
1604 range_tmp = NULL;
1605 }
1606 }
1607 if (bus_cur->noPFMemRanges) {
1608 range_cur = bus_cur->rangePFMem;
1609 for (i = 0; i < bus_cur->noPFMemRanges; i++) {
1610 if (!range_cur)
1611 break;
1612 range_tmp = range_cur;
1613 range_cur = range_cur->next;
1614 kfree (range_tmp);
1615 range_tmp = NULL;
1616 }
1617 }
1618
1619 if (bus_cur->firstIO) {
1620 res_cur = bus_cur->firstIO;
1621 while (res_cur) {
1622 res_tmp = res_cur;
1623 if (res_cur->next)
1624 res_cur = res_cur->next;
1625 else
1626 res_cur = res_cur->nextRange;
1627 kfree (res_tmp);
1628 res_tmp = NULL;
1629 }
1630 bus_cur->firstIO = NULL;
1631 }
1632 if (bus_cur->firstMem) {
1633 res_cur = bus_cur->firstMem;
1634 while (res_cur) {
1635 res_tmp = res_cur;
1636 if (res_cur->next)
1637 res_cur = res_cur->next;
1638 else
1639 res_cur = res_cur->nextRange;
1640 kfree (res_tmp);
1641 res_tmp = NULL;
1642 }
1643 bus_cur->firstMem = NULL;
1644 }
1645 if (bus_cur->firstPFMem) {
1646 res_cur = bus_cur->firstPFMem;
1647 while (res_cur) {
1648 res_tmp = res_cur;
1649 if (res_cur->next)
1650 res_cur = res_cur->next;
1651 else
1652 res_cur = res_cur->nextRange;
1653 kfree (res_tmp);
1654 res_tmp = NULL;
1655 }
1656 bus_cur->firstPFMem = NULL;
1657 }
1658
1659 if (bus_cur->firstPFMemFromMem) {
1660 res_cur = bus_cur->firstPFMemFromMem;
1661 while (res_cur) {
1662 res_tmp = res_cur;
1663 res_cur = res_cur->next;
1664
1665 kfree (res_tmp);
1666 res_tmp = NULL;
1667 }
1668 bus_cur->firstPFMemFromMem = NULL;
1669 }
1670
1671 bus_tmp = bus_cur;
1672 list_del (&bus_cur->bus_list);
1673 kfree (bus_tmp);
1674 bus_tmp = NULL;
1675 }
1676}
1677
1678/*********************************************************************************
1679 * This function will go over the PFmem resources to check if the EBDA allocated
1680 * pfmem out of memory buckets of the bus. If so, it will change the range numbers
1681 * and a flag to indicate that this resource is out of memory. It will also move the
1682 * Pfmem out of the pfmem resource list to the PFMemFromMem list, and will create
1683 * a new Mem node
1684 * This routine is called right after initialization
1685 *******************************************************************************/
1686static int __init once_over (void)
1687{
1688 struct resource_node *pfmem_cur;
1689 struct resource_node *pfmem_prev;
1690 struct resource_node *mem;
1691 struct bus_node *bus_cur;
1692 struct list_head *tmp;
1693
1694 list_for_each (tmp, &gbuses) {
1695 bus_cur = list_entry (tmp, struct bus_node, bus_list);
1696 if ((!bus_cur->rangePFMem) && (bus_cur->firstPFMem)) {
1697 for (pfmem_cur = bus_cur->firstPFMem, pfmem_prev = NULL; pfmem_cur; pfmem_prev = pfmem_cur, pfmem_cur = pfmem_cur->next) {
Kristen Accardidc6712d2006-03-14 16:24:47 -08001698 pfmem_cur->fromMem = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 if (pfmem_prev)
1700 pfmem_prev->next = pfmem_cur->next;
1701 else
1702 bus_cur->firstPFMem = pfmem_cur->next;
1703
1704 if (!bus_cur->firstPFMemFromMem)
1705 pfmem_cur->next = NULL;
1706 else
1707 /* we don't need to sort PFMemFromMem since we're using mem node for
1708 all the real work anyways, so just insert at the beginning of the
1709 list
1710 */
1711 pfmem_cur->next = bus_cur->firstPFMemFromMem;
1712
1713 bus_cur->firstPFMemFromMem = pfmem_cur;
1714
Eric Sesterhennf5afe802006-02-28 15:34:49 +01001715 mem = kzalloc(sizeof(struct resource_node), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 if (!mem) {
1717 err ("out of system memory\n");
1718 return -ENOMEM;
1719 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 mem->type = MEM;
1721 mem->busno = pfmem_cur->busno;
1722 mem->devfunc = pfmem_cur->devfunc;
1723 mem->start = pfmem_cur->start;
1724 mem->end = pfmem_cur->end;
1725 mem->len = pfmem_cur->len;
1726 if (ibmphp_add_resource (mem) < 0)
1727 err ("Trouble...trouble... EBDA allocated pfmem from mem, but system doesn't display it has this space... unless not PCI device...\n");
1728 pfmem_cur->rangeno = mem->rangeno;
1729 } /* end for pfmem */
1730 } /* end if */
1731 } /* end list_for_each bus */
Bjorn Helgaasf7625982013-11-14 11:28:18 -07001732 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733}
1734
1735int ibmphp_add_pfmem_from_mem (struct resource_node *pfmem)
1736{
1737 struct bus_node *bus_cur = find_bus_wprev (pfmem->busno, NULL, 0);
1738
1739 if (!bus_cur) {
1740 err ("cannot find bus of pfmem to add...\n");
1741 return -ENODEV;
1742 }
1743
1744 if (bus_cur->firstPFMemFromMem)
1745 pfmem->next = bus_cur->firstPFMemFromMem;
1746 else
1747 pfmem->next = NULL;
1748
1749 bus_cur->firstPFMemFromMem = pfmem;
1750
1751 return 0;
1752}
1753
1754/* This routine just goes through the buses to see if the bus already exists.
1755 * It is called from ibmphp_find_sec_number, to find out a secondary bus number for
1756 * bridged cards
1757 * Parameters: bus_number
1758 * Returns: Bus pointer or NULL
1759 */
1760struct bus_node *ibmphp_find_res_bus (u8 bus_number)
1761{
1762 return find_bus_wprev (bus_number, NULL, 0);
1763}
1764
1765static struct bus_node *find_bus_wprev (u8 bus_number, struct bus_node **prev, u8 flag)
1766{
1767 struct bus_node *bus_cur;
1768 struct list_head *tmp;
1769 struct list_head *tmp_prev;
1770
1771 list_for_each (tmp, &gbuses) {
1772 tmp_prev = tmp->prev;
1773 bus_cur = list_entry (tmp, struct bus_node, bus_list);
Bjorn Helgaasf7625982013-11-14 11:28:18 -07001774 if (flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 *prev = list_entry (tmp_prev, struct bus_node, bus_list);
Bjorn Helgaasf7625982013-11-14 11:28:18 -07001776 if (bus_cur->busno == bus_number)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 return bus_cur;
1778 }
1779
1780 return NULL;
1781}
1782
1783void ibmphp_print_test (void)
1784{
1785 int i = 0;
1786 struct bus_node *bus_cur = NULL;
1787 struct range_node *range;
1788 struct resource_node *res;
1789 struct list_head *tmp;
Bjorn Helgaasf7625982013-11-14 11:28:18 -07001790
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 debug_pci ("*****************START**********************\n");
1792
1793 if ((!list_empty(&gbuses)) && flags) {
1794 err ("The GBUSES is not NULL?!?!?!?!?\n");
1795 return;
1796 }
1797
1798 list_for_each (tmp, &gbuses) {
1799 bus_cur = list_entry (tmp, struct bus_node, bus_list);
1800 debug_pci ("This is bus # %d. There are\n", bus_cur->busno);
1801 debug_pci ("IORanges = %d\t", bus_cur->noIORanges);
1802 debug_pci ("MemRanges = %d\t", bus_cur->noMemRanges);
1803 debug_pci ("PFMemRanges = %d\n", bus_cur->noPFMemRanges);
1804 debug_pci ("The IO Ranges are as follows:\n");
1805 if (bus_cur->rangeIO) {
1806 range = bus_cur->rangeIO;
1807 for (i = 0; i < bus_cur->noIORanges; i++) {
1808 debug_pci ("rangeno is %d\n", range->rangeno);
1809 debug_pci ("[%x - %x]\n", range->start, range->end);
1810 range = range->next;
1811 }
1812 }
1813
1814 debug_pci ("The Mem Ranges are as follows:\n");
1815 if (bus_cur->rangeMem) {
1816 range = bus_cur->rangeMem;
1817 for (i = 0; i < bus_cur->noMemRanges; i++) {
1818 debug_pci ("rangeno is %d\n", range->rangeno);
1819 debug_pci ("[%x - %x]\n", range->start, range->end);
1820 range = range->next;
1821 }
1822 }
1823
1824 debug_pci ("The PFMem Ranges are as follows:\n");
1825
1826 if (bus_cur->rangePFMem) {
1827 range = bus_cur->rangePFMem;
1828 for (i = 0; i < bus_cur->noPFMemRanges; i++) {
1829 debug_pci ("rangeno is %d\n", range->rangeno);
1830 debug_pci ("[%x - %x]\n", range->start, range->end);
1831 range = range->next;
1832 }
1833 }
1834
1835 debug_pci ("The resources on this bus are as follows\n");
1836
1837 debug_pci ("IO...\n");
1838 if (bus_cur->firstIO) {
1839 res = bus_cur->firstIO;
1840 while (res) {
1841 debug_pci ("The range # is %d\n", res->rangeno);
1842 debug_pci ("The bus, devfnc is %d, %x\n", res->busno, res->devfunc);
1843 debug_pci ("[%x - %x], len=%x\n", res->start, res->end, res->len);
1844 if (res->next)
1845 res = res->next;
1846 else if (res->nextRange)
1847 res = res->nextRange;
1848 else
1849 break;
1850 }
1851 }
1852 debug_pci ("Mem...\n");
1853 if (bus_cur->firstMem) {
1854 res = bus_cur->firstMem;
1855 while (res) {
1856 debug_pci ("The range # is %d\n", res->rangeno);
1857 debug_pci ("The bus, devfnc is %d, %x\n", res->busno, res->devfunc);
1858 debug_pci ("[%x - %x], len=%x\n", res->start, res->end, res->len);
1859 if (res->next)
1860 res = res->next;
1861 else if (res->nextRange)
1862 res = res->nextRange;
1863 else
1864 break;
1865 }
1866 }
1867 debug_pci ("PFMem...\n");
1868 if (bus_cur->firstPFMem) {
1869 res = bus_cur->firstPFMem;
1870 while (res) {
1871 debug_pci ("The range # is %d\n", res->rangeno);
1872 debug_pci ("The bus, devfnc is %d, %x\n", res->busno, res->devfunc);
1873 debug_pci ("[%x - %x], len=%x\n", res->start, res->end, res->len);
1874 if (res->next)
1875 res = res->next;
1876 else if (res->nextRange)
1877 res = res->nextRange;
1878 else
1879 break;
1880 }
1881 }
1882
1883 debug_pci ("PFMemFromMem...\n");
1884 if (bus_cur->firstPFMemFromMem) {
1885 res = bus_cur->firstPFMemFromMem;
1886 while (res) {
1887 debug_pci ("The range # is %d\n", res->rangeno);
1888 debug_pci ("The bus, devfnc is %d, %x\n", res->busno, res->devfunc);
1889 debug_pci ("[%x - %x], len=%x\n", res->start, res->end, res->len);
1890 res = res->next;
1891 }
1892 }
1893 }
1894 debug_pci ("***********************END***********************\n");
1895}
1896
1897static int range_exists_already (struct range_node * range, struct bus_node * bus_cur, u8 type)
1898{
1899 struct range_node * range_cur = NULL;
1900 switch (type) {
1901 case IO:
1902 range_cur = bus_cur->rangeIO;
1903 break;
1904 case MEM:
1905 range_cur = bus_cur->rangeMem;
1906 break;
1907 case PFMEM:
1908 range_cur = bus_cur->rangePFMem;
1909 break;
1910 default:
1911 err ("wrong type passed to find out if range already exists\n");
1912 return -ENODEV;
1913 }
1914
1915 while (range_cur) {
1916 if ((range_cur->start == range->start) && (range_cur->end == range->end))
1917 return 1;
1918 range_cur = range_cur->next;
1919 }
Bjorn Helgaasf7625982013-11-14 11:28:18 -07001920
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 return 0;
1922}
1923
1924/* This routine will read the windows for any PPB we have and update the
1925 * range info for the secondary bus, and will also input this info into
1926 * primary bus, since BIOS doesn't. This is for PPB that are in the system
1927 * on bootup. For bridged cards that were added during previous load of the
1928 * driver, only the ranges and the bus structure are added, the devices are
1929 * added from NVRAM
1930 * Input: primary busno
1931 * Returns: none
1932 * Note: this function doesn't take into account IO restrictions etc,
1933 * so will only work for bridges with no video/ISA devices behind them It
Bjorn Helgaasf7625982013-11-14 11:28:18 -07001934 * also will not work for onboard PPBs that can have more than 1 *bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 * behind them All these are TO DO.
1936 * Also need to add more error checkings... (from fnc returns etc)
1937 */
1938static int __init update_bridge_ranges (struct bus_node **bus)
1939{
1940 u8 sec_busno, device, function, hdr_type, start_io_address, end_io_address;
1941 u16 vendor_id, upper_io_start, upper_io_end, start_mem_address, end_mem_address;
1942 u32 start_address, end_address, upper_start, upper_end;
1943 struct bus_node *bus_sec;
1944 struct bus_node *bus_cur;
1945 struct resource_node *io;
1946 struct resource_node *mem;
1947 struct resource_node *pfmem;
1948 struct range_node *range;
1949 unsigned int devfn;
1950
1951 bus_cur = *bus;
1952 if (!bus_cur)
1953 return -ENODEV;
1954 ibmphp_pci_bus->number = bus_cur->busno;
1955
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001956 debug ("inside %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 debug ("bus_cur->busno = %x\n", bus_cur->busno);
1958
1959 for (device = 0; device < 32; device++) {
1960 for (function = 0x00; function < 0x08; function++) {
1961 devfn = PCI_DEVFN(device, function);
1962 pci_bus_read_config_word (ibmphp_pci_bus, devfn, PCI_VENDOR_ID, &vendor_id);
1963
1964 if (vendor_id != PCI_VENDOR_ID_NOTVALID) {
1965 /* found correct device!!! */
1966 pci_bus_read_config_byte (ibmphp_pci_bus, devfn, PCI_HEADER_TYPE, &hdr_type);
1967
1968 switch (hdr_type) {
1969 case PCI_HEADER_TYPE_NORMAL:
1970 function = 0x8;
1971 break;
1972 case PCI_HEADER_TYPE_MULTIDEVICE:
1973 break;
1974 case PCI_HEADER_TYPE_BRIDGE:
1975 function = 0x8;
1976 case PCI_HEADER_TYPE_MULTIBRIDGE:
Bjorn Helgaasf7625982013-11-14 11:28:18 -07001977 /* We assume here that only 1 bus behind the bridge
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 TO DO: add functionality for several:
1979 temp = secondary;
1980 while (temp < subordinate) {
1981 ...
1982 temp++;
1983 }
1984 */
1985 pci_bus_read_config_byte (ibmphp_pci_bus, devfn, PCI_SECONDARY_BUS, &sec_busno);
Bjorn Helgaasf7625982013-11-14 11:28:18 -07001986 bus_sec = find_bus_wprev (sec_busno, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 /* this bus structure doesn't exist yet, PPB was configured during previous loading of ibmphp */
1988 if (!bus_sec) {
1989 bus_sec = alloc_error_bus (NULL, sec_busno, 1);
1990 /* the rest will be populated during NVRAM call */
1991 return 0;
1992 }
1993 pci_bus_read_config_byte (ibmphp_pci_bus, devfn, PCI_IO_BASE, &start_io_address);
1994 pci_bus_read_config_byte (ibmphp_pci_bus, devfn, PCI_IO_LIMIT, &end_io_address);
1995 pci_bus_read_config_word (ibmphp_pci_bus, devfn, PCI_IO_BASE_UPPER16, &upper_io_start);
1996 pci_bus_read_config_word (ibmphp_pci_bus, devfn, PCI_IO_LIMIT_UPPER16, &upper_io_end);
1997 start_address = (start_io_address & PCI_IO_RANGE_MASK) << 8;
1998 start_address |= (upper_io_start << 16);
1999 end_address = (end_io_address & PCI_IO_RANGE_MASK) << 8;
2000 end_address |= (upper_io_end << 16);
2001
2002 if ((start_address) && (start_address <= end_address)) {
Eric Sesterhennf5afe802006-02-28 15:34:49 +01002003 range = kzalloc(sizeof(struct range_node), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 if (!range) {
2005 err ("out of system memory\n");
2006 return -ENOMEM;
2007 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 range->start = start_address;
2009 range->end = end_address + 0xfff;
2010
2011 if (bus_sec->noIORanges > 0) {
2012 if (!range_exists_already (range, bus_sec, IO)) {
H. Peter Anvinc85e4aa2010-02-10 17:45:09 -08002013 add_bus_range (IO, range, bus_sec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 ++bus_sec->noIORanges;
2015 } else {
2016 kfree (range);
2017 range = NULL;
2018 }
2019 } else {
2020 /* 1st IO Range on the bus */
2021 range->rangeno = 1;
2022 bus_sec->rangeIO = range;
2023 ++bus_sec->noIORanges;
2024 }
2025 fix_resources (bus_sec);
2026
2027 if (ibmphp_find_resource (bus_cur, start_address, &io, IO)) {
Eric Sesterhennf5afe802006-02-28 15:34:49 +01002028 io = kzalloc(sizeof(struct resource_node), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 if (!io) {
2030 kfree (range);
2031 err ("out of system memory\n");
2032 return -ENOMEM;
2033 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 io->type = IO;
2035 io->busno = bus_cur->busno;
2036 io->devfunc = ((device << 3) | (function & 0x7));
2037 io->start = start_address;
2038 io->end = end_address + 0xfff;
2039 io->len = io->end - io->start + 1;
2040 ibmphp_add_resource (io);
2041 }
Bjorn Helgaasf7625982013-11-14 11:28:18 -07002042 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043
2044 pci_bus_read_config_word (ibmphp_pci_bus, devfn, PCI_MEMORY_BASE, &start_mem_address);
2045 pci_bus_read_config_word (ibmphp_pci_bus, devfn, PCI_MEMORY_LIMIT, &end_mem_address);
2046
2047 start_address = 0x00000000 | (start_mem_address & PCI_MEMORY_RANGE_MASK) << 16;
2048 end_address = 0x00000000 | (end_mem_address & PCI_MEMORY_RANGE_MASK) << 16;
2049
2050 if ((start_address) && (start_address <= end_address)) {
2051
Eric Sesterhennf5afe802006-02-28 15:34:49 +01002052 range = kzalloc(sizeof(struct range_node), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 if (!range) {
2054 err ("out of system memory\n");
2055 return -ENOMEM;
2056 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 range->start = start_address;
2058 range->end = end_address + 0xfffff;
2059
2060 if (bus_sec->noMemRanges > 0) {
2061 if (!range_exists_already (range, bus_sec, MEM)) {
H. Peter Anvinc85e4aa2010-02-10 17:45:09 -08002062 add_bus_range (MEM, range, bus_sec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 ++bus_sec->noMemRanges;
2064 } else {
2065 kfree (range);
2066 range = NULL;
2067 }
2068 } else {
2069 /* 1st Mem Range on the bus */
2070 range->rangeno = 1;
2071 bus_sec->rangeMem = range;
2072 ++bus_sec->noMemRanges;
2073 }
2074
2075 fix_resources (bus_sec);
2076
2077 if (ibmphp_find_resource (bus_cur, start_address, &mem, MEM)) {
Eric Sesterhennf5afe802006-02-28 15:34:49 +01002078 mem = kzalloc(sizeof(struct resource_node), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 if (!mem) {
2080 kfree (range);
2081 err ("out of system memory\n");
2082 return -ENOMEM;
2083 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 mem->type = MEM;
2085 mem->busno = bus_cur->busno;
2086 mem->devfunc = ((device << 3) | (function & 0x7));
2087 mem->start = start_address;
2088 mem->end = end_address + 0xfffff;
2089 mem->len = mem->end - mem->start + 1;
2090 ibmphp_add_resource (mem);
2091 }
2092 }
2093 pci_bus_read_config_word (ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_BASE, &start_mem_address);
2094 pci_bus_read_config_word (ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, &end_mem_address);
2095 pci_bus_read_config_dword (ibmphp_pci_bus, devfn, PCI_PREF_BASE_UPPER32, &upper_start);
2096 pci_bus_read_config_dword (ibmphp_pci_bus, devfn, PCI_PREF_LIMIT_UPPER32, &upper_end);
2097 start_address = 0x00000000 | (start_mem_address & PCI_MEMORY_RANGE_MASK) << 16;
2098 end_address = 0x00000000 | (end_mem_address & PCI_MEMORY_RANGE_MASK) << 16;
2099#if BITS_PER_LONG == 64
2100 start_address |= ((long) upper_start) << 32;
2101 end_address |= ((long) upper_end) << 32;
2102#endif
2103
2104 if ((start_address) && (start_address <= end_address)) {
2105
Eric Sesterhennf5afe802006-02-28 15:34:49 +01002106 range = kzalloc(sizeof(struct range_node), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 if (!range) {
2108 err ("out of system memory\n");
2109 return -ENOMEM;
2110 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 range->start = start_address;
2112 range->end = end_address + 0xfffff;
2113
2114 if (bus_sec->noPFMemRanges > 0) {
2115 if (!range_exists_already (range, bus_sec, PFMEM)) {
H. Peter Anvinc85e4aa2010-02-10 17:45:09 -08002116 add_bus_range (PFMEM, range, bus_sec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 ++bus_sec->noPFMemRanges;
2118 } else {
2119 kfree (range);
2120 range = NULL;
2121 }
2122 } else {
2123 /* 1st PFMem Range on the bus */
2124 range->rangeno = 1;
2125 bus_sec->rangePFMem = range;
2126 ++bus_sec->noPFMemRanges;
2127 }
2128
2129 fix_resources (bus_sec);
2130 if (ibmphp_find_resource (bus_cur, start_address, &pfmem, PFMEM)) {
Eric Sesterhennf5afe802006-02-28 15:34:49 +01002131 pfmem = kzalloc(sizeof(struct resource_node), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 if (!pfmem) {
2133 kfree (range);
2134 err ("out of system memory\n");
2135 return -ENOMEM;
2136 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 pfmem->type = PFMEM;
2138 pfmem->busno = bus_cur->busno;
2139 pfmem->devfunc = ((device << 3) | (function & 0x7));
2140 pfmem->start = start_address;
2141 pfmem->end = end_address + 0xfffff;
2142 pfmem->len = pfmem->end - pfmem->start + 1;
Kristen Accardidc6712d2006-03-14 16:24:47 -08002143 pfmem->fromMem = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144
2145 ibmphp_add_resource (pfmem);
2146 }
2147 }
2148 break;
2149 } /* end of switch */
2150 } /* end if vendor */
2151 } /* end for function */
2152 } /* end for device */
2153
2154 bus = &bus_cur;
2155 return 0;
2156}