ia64/linux-2.6.18-xen.hg

view arch/sparc64/kernel/of_device.c @ 897:329ea0ccb344

balloon: try harder to balloon up under memory pressure.

Currently if the balloon driver is unable to increase the guest's
reservation it assumes the failure was due to reaching its full
allocation, gives up on the ballooning operation and records the limit
it reached as the "hard limit". The driver will not try again until
the target is set again (even to the same value).

However it is possible that ballooning has in fact failed due to
memory pressure in the host and therefore it is desirable to keep
attempting to reach the target in case memory becomes available. The
most likely scenario is that some guests are ballooning down while
others are ballooning up and therefore there is temporary memory
pressure while things stabilise. You would not expect a well behaved
toolstack to ask a domain to balloon to more than its allocation nor
would you expect it to deliberately over-commit memory by setting
balloon targets which exceed the total host memory.

This patch drops the concept of a hard limit and causes the balloon
driver to retry increasing the reservation on a timer in the same
manner as when decreasing the reservation.

Also if we partially succeed in increasing the reservation
(i.e. receive less pages than we asked for) then we may as well keep
those pages rather than returning them to Xen.

Signed-off-by: Ian Campbell <ian.campbell@citrix.com>
author Keir Fraser <keir.fraser@citrix.com>
date Fri Jun 05 14:01:20 2009 +0100 (2009-06-05)
parents 3e8752eb6d9c
children
line source
1 #include <linux/config.h>
2 #include <linux/string.h>
3 #include <linux/kernel.h>
4 #include <linux/init.h>
5 #include <linux/module.h>
6 #include <linux/mod_devicetable.h>
7 #include <linux/slab.h>
9 #include <asm/errno.h>
10 #include <asm/of_device.h>
12 /**
13 * of_match_device - Tell if an of_device structure has a matching
14 * of_match structure
15 * @ids: array of of device match structures to search in
16 * @dev: the of device structure to match against
17 *
18 * Used by a driver to check whether an of_device present in the
19 * system is in its list of supported devices.
20 */
21 const struct of_device_id *of_match_device(const struct of_device_id *matches,
22 const struct of_device *dev)
23 {
24 if (!dev->node)
25 return NULL;
26 while (matches->name[0] || matches->type[0] || matches->compatible[0]) {
27 int match = 1;
28 if (matches->name[0])
29 match &= dev->node->name
30 && !strcmp(matches->name, dev->node->name);
31 if (matches->type[0])
32 match &= dev->node->type
33 && !strcmp(matches->type, dev->node->type);
34 if (matches->compatible[0])
35 match &= of_device_is_compatible(dev->node,
36 matches->compatible);
37 if (match)
38 return matches;
39 matches++;
40 }
41 return NULL;
42 }
44 static int of_platform_bus_match(struct device *dev, struct device_driver *drv)
45 {
46 struct of_device * of_dev = to_of_device(dev);
47 struct of_platform_driver * of_drv = to_of_platform_driver(drv);
48 const struct of_device_id * matches = of_drv->match_table;
50 if (!matches)
51 return 0;
53 return of_match_device(matches, of_dev) != NULL;
54 }
56 struct of_device *of_dev_get(struct of_device *dev)
57 {
58 struct device *tmp;
60 if (!dev)
61 return NULL;
62 tmp = get_device(&dev->dev);
63 if (tmp)
64 return to_of_device(tmp);
65 else
66 return NULL;
67 }
69 void of_dev_put(struct of_device *dev)
70 {
71 if (dev)
72 put_device(&dev->dev);
73 }
76 static int of_device_probe(struct device *dev)
77 {
78 int error = -ENODEV;
79 struct of_platform_driver *drv;
80 struct of_device *of_dev;
81 const struct of_device_id *match;
83 drv = to_of_platform_driver(dev->driver);
84 of_dev = to_of_device(dev);
86 if (!drv->probe)
87 return error;
89 of_dev_get(of_dev);
91 match = of_match_device(drv->match_table, of_dev);
92 if (match)
93 error = drv->probe(of_dev, match);
94 if (error)
95 of_dev_put(of_dev);
97 return error;
98 }
100 static int of_device_remove(struct device *dev)
101 {
102 struct of_device * of_dev = to_of_device(dev);
103 struct of_platform_driver * drv = to_of_platform_driver(dev->driver);
105 if (dev->driver && drv->remove)
106 drv->remove(of_dev);
107 return 0;
108 }
110 static int of_device_suspend(struct device *dev, pm_message_t state)
111 {
112 struct of_device * of_dev = to_of_device(dev);
113 struct of_platform_driver * drv = to_of_platform_driver(dev->driver);
114 int error = 0;
116 if (dev->driver && drv->suspend)
117 error = drv->suspend(of_dev, state);
118 return error;
119 }
121 static int of_device_resume(struct device * dev)
122 {
123 struct of_device * of_dev = to_of_device(dev);
124 struct of_platform_driver * drv = to_of_platform_driver(dev->driver);
125 int error = 0;
127 if (dev->driver && drv->resume)
128 error = drv->resume(of_dev);
129 return error;
130 }
132 void __iomem *of_ioremap(struct resource *res, unsigned long offset, unsigned long size, char *name)
133 {
134 unsigned long ret = res->start + offset;
136 if (!request_region(ret, size, name))
137 ret = 0;
139 return (void __iomem *) ret;
140 }
141 EXPORT_SYMBOL(of_ioremap);
143 void of_iounmap(void __iomem *base, unsigned long size)
144 {
145 release_region((unsigned long) base, size);
146 }
147 EXPORT_SYMBOL(of_iounmap);
149 static int node_match(struct device *dev, void *data)
150 {
151 struct of_device *op = to_of_device(dev);
152 struct device_node *dp = data;
154 return (op->node == dp);
155 }
157 struct of_device *of_find_device_by_node(struct device_node *dp)
158 {
159 struct device *dev = bus_find_device(&of_bus_type, NULL,
160 dp, node_match);
162 if (dev)
163 return to_of_device(dev);
165 return NULL;
166 }
167 EXPORT_SYMBOL(of_find_device_by_node);
169 #ifdef CONFIG_PCI
170 struct bus_type isa_bus_type = {
171 .name = "isa",
172 .match = of_platform_bus_match,
173 .probe = of_device_probe,
174 .remove = of_device_remove,
175 .suspend = of_device_suspend,
176 .resume = of_device_resume,
177 };
178 EXPORT_SYMBOL(isa_bus_type);
180 struct bus_type ebus_bus_type = {
181 .name = "ebus",
182 .match = of_platform_bus_match,
183 .probe = of_device_probe,
184 .remove = of_device_remove,
185 .suspend = of_device_suspend,
186 .resume = of_device_resume,
187 };
188 EXPORT_SYMBOL(ebus_bus_type);
189 #endif
191 #ifdef CONFIG_SBUS
192 struct bus_type sbus_bus_type = {
193 .name = "sbus",
194 .match = of_platform_bus_match,
195 .probe = of_device_probe,
196 .remove = of_device_remove,
197 .suspend = of_device_suspend,
198 .resume = of_device_resume,
199 };
200 EXPORT_SYMBOL(sbus_bus_type);
201 #endif
203 struct bus_type of_bus_type = {
204 .name = "of",
205 .match = of_platform_bus_match,
206 .probe = of_device_probe,
207 .remove = of_device_remove,
208 .suspend = of_device_suspend,
209 .resume = of_device_resume,
210 };
211 EXPORT_SYMBOL(of_bus_type);
213 static inline u64 of_read_addr(const u32 *cell, int size)
214 {
215 u64 r = 0;
216 while (size--)
217 r = (r << 32) | *(cell++);
218 return r;
219 }
221 static void __init get_cells(struct device_node *dp,
222 int *addrc, int *sizec)
223 {
224 if (addrc)
225 *addrc = of_n_addr_cells(dp);
226 if (sizec)
227 *sizec = of_n_size_cells(dp);
228 }
230 /* Max address size we deal with */
231 #define OF_MAX_ADDR_CELLS 4
233 struct of_bus {
234 const char *name;
235 const char *addr_prop_name;
236 int (*match)(struct device_node *parent);
237 void (*count_cells)(struct device_node *child,
238 int *addrc, int *sizec);
239 int (*map)(u32 *addr, const u32 *range,
240 int na, int ns, int pna);
241 unsigned int (*get_flags)(u32 *addr);
242 };
244 /*
245 * Default translator (generic bus)
246 */
248 static void of_bus_default_count_cells(struct device_node *dev,
249 int *addrc, int *sizec)
250 {
251 get_cells(dev, addrc, sizec);
252 }
254 /* Make sure the least significant 64-bits are in-range. Even
255 * for 3 or 4 cell values it is a good enough approximation.
256 */
257 static int of_out_of_range(const u32 *addr, const u32 *base,
258 const u32 *size, int na, int ns)
259 {
260 u64 a = of_read_addr(addr, na);
261 u64 b = of_read_addr(base, na);
263 if (a < b)
264 return 1;
266 b += of_read_addr(size, ns);
267 if (a >= b)
268 return 1;
270 return 0;
271 }
273 static int of_bus_default_map(u32 *addr, const u32 *range,
274 int na, int ns, int pna)
275 {
276 u32 result[OF_MAX_ADDR_CELLS];
277 int i;
279 if (ns > 2) {
280 printk("of_device: Cannot handle size cells (%d) > 2.", ns);
281 return -EINVAL;
282 }
284 if (of_out_of_range(addr, range, range + na + pna, na, ns))
285 return -EINVAL;
287 /* Start with the parent range base. */
288 memcpy(result, range + na, pna * 4);
290 /* Add in the child address offset. */
291 for (i = 0; i < na; i++)
292 result[pna - 1 - i] +=
293 (addr[na - 1 - i] -
294 range[na - 1 - i]);
296 memcpy(addr, result, pna * 4);
298 return 0;
299 }
301 static unsigned int of_bus_default_get_flags(u32 *addr)
302 {
303 return IORESOURCE_MEM;
304 }
306 /*
307 * PCI bus specific translator
308 */
310 static int of_bus_pci_match(struct device_node *np)
311 {
312 if (!strcmp(np->type, "pci") || !strcmp(np->type, "pciex")) {
313 /* Do not do PCI specific frobbing if the
314 * PCI bridge lacks a ranges property. We
315 * want to pass it through up to the next
316 * parent as-is, not with the PCI translate
317 * method which chops off the top address cell.
318 */
319 if (!of_find_property(np, "ranges", NULL))
320 return 0;
322 return 1;
323 }
325 return 0;
326 }
328 static void of_bus_pci_count_cells(struct device_node *np,
329 int *addrc, int *sizec)
330 {
331 if (addrc)
332 *addrc = 3;
333 if (sizec)
334 *sizec = 2;
335 }
337 static int of_bus_pci_map(u32 *addr, const u32 *range,
338 int na, int ns, int pna)
339 {
340 u32 result[OF_MAX_ADDR_CELLS];
341 int i;
343 /* Check address type match */
344 if ((addr[0] ^ range[0]) & 0x03000000)
345 return -EINVAL;
347 if (of_out_of_range(addr + 1, range + 1, range + na + pna,
348 na - 1, ns))
349 return -EINVAL;
351 /* Start with the parent range base. */
352 memcpy(result, range + na, pna * 4);
354 /* Add in the child address offset, skipping high cell. */
355 for (i = 0; i < na - 1; i++)
356 result[pna - 1 - i] +=
357 (addr[na - 1 - i] -
358 range[na - 1 - i]);
360 memcpy(addr, result, pna * 4);
362 return 0;
363 }
365 static unsigned int of_bus_pci_get_flags(u32 *addr)
366 {
367 unsigned int flags = 0;
368 u32 w = addr[0];
370 switch((w >> 24) & 0x03) {
371 case 0x01:
372 flags |= IORESOURCE_IO;
373 case 0x02: /* 32 bits */
374 case 0x03: /* 64 bits */
375 flags |= IORESOURCE_MEM;
376 }
377 if (w & 0x40000000)
378 flags |= IORESOURCE_PREFETCH;
379 return flags;
380 }
382 /*
383 * SBUS bus specific translator
384 */
386 static int of_bus_sbus_match(struct device_node *np)
387 {
388 return !strcmp(np->name, "sbus") ||
389 !strcmp(np->name, "sbi");
390 }
392 static void of_bus_sbus_count_cells(struct device_node *child,
393 int *addrc, int *sizec)
394 {
395 if (addrc)
396 *addrc = 2;
397 if (sizec)
398 *sizec = 1;
399 }
401 /*
402 * FHC/Central bus specific translator.
403 *
404 * This is just needed to hard-code the address and size cell
405 * counts. 'fhc' and 'central' nodes lack the #address-cells and
406 * #size-cells properties, and if you walk to the root on such
407 * Enterprise boxes all you'll get is a #size-cells of 2 which is
408 * not what we want to use.
409 */
410 static int of_bus_fhc_match(struct device_node *np)
411 {
412 return !strcmp(np->name, "fhc") ||
413 !strcmp(np->name, "central");
414 }
416 #define of_bus_fhc_count_cells of_bus_sbus_count_cells
418 /*
419 * Array of bus specific translators
420 */
422 static struct of_bus of_busses[] = {
423 /* PCI */
424 {
425 .name = "pci",
426 .addr_prop_name = "assigned-addresses",
427 .match = of_bus_pci_match,
428 .count_cells = of_bus_pci_count_cells,
429 .map = of_bus_pci_map,
430 .get_flags = of_bus_pci_get_flags,
431 },
432 /* SBUS */
433 {
434 .name = "sbus",
435 .addr_prop_name = "reg",
436 .match = of_bus_sbus_match,
437 .count_cells = of_bus_sbus_count_cells,
438 .map = of_bus_default_map,
439 .get_flags = of_bus_default_get_flags,
440 },
441 /* FHC */
442 {
443 .name = "fhc",
444 .addr_prop_name = "reg",
445 .match = of_bus_fhc_match,
446 .count_cells = of_bus_fhc_count_cells,
447 .map = of_bus_default_map,
448 .get_flags = of_bus_default_get_flags,
449 },
450 /* Default */
451 {
452 .name = "default",
453 .addr_prop_name = "reg",
454 .match = NULL,
455 .count_cells = of_bus_default_count_cells,
456 .map = of_bus_default_map,
457 .get_flags = of_bus_default_get_flags,
458 },
459 };
461 static struct of_bus *of_match_bus(struct device_node *np)
462 {
463 int i;
465 for (i = 0; i < ARRAY_SIZE(of_busses); i ++)
466 if (!of_busses[i].match || of_busses[i].match(np))
467 return &of_busses[i];
468 BUG();
469 return NULL;
470 }
472 static int __init build_one_resource(struct device_node *parent,
473 struct of_bus *bus,
474 struct of_bus *pbus,
475 u32 *addr,
476 int na, int ns, int pna)
477 {
478 u32 *ranges;
479 unsigned int rlen;
480 int rone;
482 ranges = of_get_property(parent, "ranges", &rlen);
483 if (ranges == NULL || rlen == 0) {
484 u32 result[OF_MAX_ADDR_CELLS];
485 int i;
487 memset(result, 0, pna * 4);
488 for (i = 0; i < na; i++)
489 result[pna - 1 - i] =
490 addr[na - 1 - i];
492 memcpy(addr, result, pna * 4);
493 return 0;
494 }
496 /* Now walk through the ranges */
497 rlen /= 4;
498 rone = na + pna + ns;
499 for (; rlen >= rone; rlen -= rone, ranges += rone) {
500 if (!bus->map(addr, ranges, na, ns, pna))
501 return 0;
502 }
504 return 1;
505 }
507 static int __init use_1to1_mapping(struct device_node *pp)
508 {
509 char *model;
511 /* If this is on the PMU bus, don't try to translate it even
512 * if a ranges property exists.
513 */
514 if (!strcmp(pp->name, "pmu"))
515 return 1;
517 /* If we have a ranges property in the parent, use it. */
518 if (of_find_property(pp, "ranges", NULL) != NULL)
519 return 0;
521 /* If the parent is the dma node of an ISA bus, pass
522 * the translation up to the root.
523 */
524 if (!strcmp(pp->name, "dma"))
525 return 0;
527 /* Similarly for Simba PCI bridges. */
528 model = of_get_property(pp, "model", NULL);
529 if (model && !strcmp(model, "SUNW,simba"))
530 return 0;
532 return 1;
533 }
535 static int of_resource_verbose;
537 static void __init build_device_resources(struct of_device *op,
538 struct device *parent)
539 {
540 struct of_device *p_op;
541 struct of_bus *bus;
542 int na, ns;
543 int index, num_reg;
544 void *preg;
546 if (!parent)
547 return;
549 p_op = to_of_device(parent);
550 bus = of_match_bus(p_op->node);
551 bus->count_cells(op->node, &na, &ns);
553 preg = of_get_property(op->node, bus->addr_prop_name, &num_reg);
554 if (!preg || num_reg == 0)
555 return;
557 /* Convert to num-cells. */
558 num_reg /= 4;
560 /* Convert to num-entries. */
561 num_reg /= na + ns;
563 /* Prevent overruning the op->resources[] array. */
564 if (num_reg > PROMREG_MAX) {
565 printk(KERN_WARNING "%s: Too many regs (%d), "
566 "limiting to %d.\n",
567 op->node->full_name, num_reg, PROMREG_MAX);
568 num_reg = PROMREG_MAX;
569 }
571 for (index = 0; index < num_reg; index++) {
572 struct resource *r = &op->resource[index];
573 u32 addr[OF_MAX_ADDR_CELLS];
574 u32 *reg = (preg + (index * ((na + ns) * 4)));
575 struct device_node *dp = op->node;
576 struct device_node *pp = p_op->node;
577 struct of_bus *pbus;
578 u64 size, result = OF_BAD_ADDR;
579 unsigned long flags;
580 int dna, dns;
581 int pna, pns;
583 size = of_read_addr(reg + na, ns);
584 flags = bus->get_flags(reg);
586 memcpy(addr, reg, na * 4);
588 if (use_1to1_mapping(pp)) {
589 result = of_read_addr(addr, na);
590 goto build_res;
591 }
593 dna = na;
594 dns = ns;
596 while (1) {
597 dp = pp;
598 pp = dp->parent;
599 if (!pp) {
600 result = of_read_addr(addr, dna);
601 break;
602 }
604 pbus = of_match_bus(pp);
605 pbus->count_cells(dp, &pna, &pns);
607 if (build_one_resource(dp, bus, pbus, addr,
608 dna, dns, pna))
609 break;
611 dna = pna;
612 dns = pns;
613 bus = pbus;
614 }
616 build_res:
617 memset(r, 0, sizeof(*r));
619 if (of_resource_verbose)
620 printk("%s reg[%d] -> %lx\n",
621 op->node->full_name, index,
622 result);
624 if (result != OF_BAD_ADDR) {
625 if (tlb_type == hypervisor)
626 result &= 0x0fffffffffffffffUL;
628 r->start = result;
629 r->end = result + size - 1;
630 r->flags = flags;
631 } else {
632 r->start = ~0UL;
633 r->end = ~0UL;
634 }
635 r->name = op->node->name;
636 }
637 }
639 static struct device_node * __init
640 apply_interrupt_map(struct device_node *dp, struct device_node *pp,
641 u32 *imap, int imlen, u32 *imask,
642 unsigned int *irq_p)
643 {
644 struct device_node *cp;
645 unsigned int irq = *irq_p;
646 struct of_bus *bus;
647 phandle handle;
648 u32 *reg;
649 int na, num_reg, i;
651 bus = of_match_bus(pp);
652 bus->count_cells(dp, &na, NULL);
654 reg = of_get_property(dp, "reg", &num_reg);
655 if (!reg || !num_reg)
656 return NULL;
658 imlen /= ((na + 3) * 4);
659 handle = 0;
660 for (i = 0; i < imlen; i++) {
661 int j;
663 for (j = 0; j < na; j++) {
664 if ((reg[j] & imask[j]) != imap[j])
665 goto next;
666 }
667 if (imap[na] == irq) {
668 handle = imap[na + 1];
669 irq = imap[na + 2];
670 break;
671 }
673 next:
674 imap += (na + 3);
675 }
676 if (i == imlen) {
677 /* Psycho and Sabre PCI controllers can have 'interrupt-map'
678 * properties that do not include the on-board device
679 * interrupts. Instead, the device's 'interrupts' property
680 * is already a fully specified INO value.
681 *
682 * Handle this by deciding that, if we didn't get a
683 * match in the parent's 'interrupt-map', and the
684 * parent is an IRQ translater, then use the parent as
685 * our IRQ controller.
686 */
687 if (pp->irq_trans)
688 return pp;
690 return NULL;
691 }
693 *irq_p = irq;
694 cp = of_find_node_by_phandle(handle);
696 return cp;
697 }
699 static unsigned int __init pci_irq_swizzle(struct device_node *dp,
700 struct device_node *pp,
701 unsigned int irq)
702 {
703 struct linux_prom_pci_registers *regs;
704 unsigned int devfn, slot, ret;
706 if (irq < 1 || irq > 4)
707 return irq;
709 regs = of_get_property(dp, "reg", NULL);
710 if (!regs)
711 return irq;
713 devfn = (regs->phys_hi >> 8) & 0xff;
714 slot = (devfn >> 3) & 0x1f;
716 ret = ((irq - 1 + (slot & 3)) & 3) + 1;
718 return ret;
719 }
721 static int of_irq_verbose;
723 static unsigned int __init build_one_device_irq(struct of_device *op,
724 struct device *parent,
725 unsigned int irq)
726 {
727 struct device_node *dp = op->node;
728 struct device_node *pp, *ip;
729 unsigned int orig_irq = irq;
731 if (irq == 0xffffffff)
732 return irq;
734 if (dp->irq_trans) {
735 irq = dp->irq_trans->irq_build(dp, irq,
736 dp->irq_trans->data);
738 if (of_irq_verbose)
739 printk("%s: direct translate %x --> %x\n",
740 dp->full_name, orig_irq, irq);
742 return irq;
743 }
745 /* Something more complicated. Walk up to the root, applying
746 * interrupt-map or bus specific translations, until we hit
747 * an IRQ translator.
748 *
749 * If we hit a bus type or situation we cannot handle, we
750 * stop and assume that the original IRQ number was in a
751 * format which has special meaning to it's immediate parent.
752 */
753 pp = dp->parent;
754 ip = NULL;
755 while (pp) {
756 void *imap, *imsk;
757 int imlen;
759 imap = of_get_property(pp, "interrupt-map", &imlen);
760 imsk = of_get_property(pp, "interrupt-map-mask", NULL);
761 if (imap && imsk) {
762 struct device_node *iret;
763 int this_orig_irq = irq;
765 iret = apply_interrupt_map(dp, pp,
766 imap, imlen, imsk,
767 &irq);
769 if (of_irq_verbose)
770 printk("%s: Apply [%s:%x] imap --> [%s:%x]\n",
771 op->node->full_name,
772 pp->full_name, this_orig_irq,
773 (iret ? iret->full_name : "NULL"), irq);
775 if (!iret)
776 break;
778 if (iret->irq_trans) {
779 ip = iret;
780 break;
781 }
782 } else {
783 if (!strcmp(pp->type, "pci") ||
784 !strcmp(pp->type, "pciex")) {
785 unsigned int this_orig_irq = irq;
787 irq = pci_irq_swizzle(dp, pp, irq);
788 if (of_irq_verbose)
789 printk("%s: PCI swizzle [%s] "
790 "%x --> %x\n",
791 op->node->full_name,
792 pp->full_name, this_orig_irq,
793 irq);
795 }
797 if (pp->irq_trans) {
798 ip = pp;
799 break;
800 }
801 }
802 dp = pp;
803 pp = pp->parent;
804 }
805 if (!ip)
806 return orig_irq;
808 irq = ip->irq_trans->irq_build(op->node, irq,
809 ip->irq_trans->data);
810 if (of_irq_verbose)
811 printk("%s: Apply IRQ trans [%s] %x --> %x\n",
812 op->node->full_name, ip->full_name, orig_irq, irq);
814 return irq;
815 }
817 static struct of_device * __init scan_one_device(struct device_node *dp,
818 struct device *parent)
819 {
820 struct of_device *op = kzalloc(sizeof(*op), GFP_KERNEL);
821 unsigned int *irq;
822 int len, i;
824 if (!op)
825 return NULL;
827 op->node = dp;
829 op->clock_freq = of_getintprop_default(dp, "clock-frequency",
830 (25*1000*1000));
831 op->portid = of_getintprop_default(dp, "upa-portid", -1);
832 if (op->portid == -1)
833 op->portid = of_getintprop_default(dp, "portid", -1);
835 irq = of_get_property(dp, "interrupts", &len);
836 if (irq) {
837 memcpy(op->irqs, irq, len);
838 op->num_irqs = len / 4;
839 } else {
840 op->num_irqs = 0;
841 }
843 /* Prevent overruning the op->irqs[] array. */
844 if (op->num_irqs > PROMINTR_MAX) {
845 printk(KERN_WARNING "%s: Too many irqs (%d), "
846 "limiting to %d.\n",
847 dp->full_name, op->num_irqs, PROMINTR_MAX);
848 op->num_irqs = PROMINTR_MAX;
849 }
851 build_device_resources(op, parent);
852 for (i = 0; i < op->num_irqs; i++)
853 op->irqs[i] = build_one_device_irq(op, parent, op->irqs[i]);
855 op->dev.parent = parent;
856 op->dev.bus = &of_bus_type;
857 if (!parent)
858 strcpy(op->dev.bus_id, "root");
859 else
860 strcpy(op->dev.bus_id, dp->path_component_name);
862 if (of_device_register(op)) {
863 printk("%s: Could not register of device.\n",
864 dp->full_name);
865 kfree(op);
866 op = NULL;
867 }
869 return op;
870 }
872 static void __init scan_tree(struct device_node *dp, struct device *parent)
873 {
874 while (dp) {
875 struct of_device *op = scan_one_device(dp, parent);
877 if (op)
878 scan_tree(dp->child, &op->dev);
880 dp = dp->sibling;
881 }
882 }
884 static void __init scan_of_devices(void)
885 {
886 struct device_node *root = of_find_node_by_path("/");
887 struct of_device *parent;
889 parent = scan_one_device(root, NULL);
890 if (!parent)
891 return;
893 scan_tree(root->child, &parent->dev);
894 }
896 static int __init of_bus_driver_init(void)
897 {
898 int err;
900 err = bus_register(&of_bus_type);
901 #ifdef CONFIG_PCI
902 if (!err)
903 err = bus_register(&isa_bus_type);
904 if (!err)
905 err = bus_register(&ebus_bus_type);
906 #endif
907 #ifdef CONFIG_SBUS
908 if (!err)
909 err = bus_register(&sbus_bus_type);
910 #endif
912 if (!err)
913 scan_of_devices();
915 return err;
916 }
918 postcore_initcall(of_bus_driver_init);
920 static int __init of_debug(char *str)
921 {
922 int val = 0;
924 get_option(&str, &val);
925 if (val & 1)
926 of_resource_verbose = 1;
927 if (val & 2)
928 of_irq_verbose = 1;
929 return 1;
930 }
932 __setup("of_debug=", of_debug);
934 int of_register_driver(struct of_platform_driver *drv, struct bus_type *bus)
935 {
936 /* initialize common driver fields */
937 drv->driver.name = drv->name;
938 drv->driver.bus = bus;
940 /* register with core */
941 return driver_register(&drv->driver);
942 }
944 void of_unregister_driver(struct of_platform_driver *drv)
945 {
946 driver_unregister(&drv->driver);
947 }
950 static ssize_t dev_show_devspec(struct device *dev, struct device_attribute *attr, char *buf)
951 {
952 struct of_device *ofdev;
954 ofdev = to_of_device(dev);
955 return sprintf(buf, "%s", ofdev->node->full_name);
956 }
958 static DEVICE_ATTR(devspec, S_IRUGO, dev_show_devspec, NULL);
960 /**
961 * of_release_dev - free an of device structure when all users of it are finished.
962 * @dev: device that's been disconnected
963 *
964 * Will be called only by the device core when all users of this of device are
965 * done.
966 */
967 void of_release_dev(struct device *dev)
968 {
969 struct of_device *ofdev;
971 ofdev = to_of_device(dev);
973 kfree(ofdev);
974 }
976 int of_device_register(struct of_device *ofdev)
977 {
978 int rc;
980 BUG_ON(ofdev->node == NULL);
982 rc = device_register(&ofdev->dev);
983 if (rc)
984 return rc;
986 rc = device_create_file(&ofdev->dev, &dev_attr_devspec);
987 if (rc)
988 device_unregister(&ofdev->dev);
990 return rc;
991 }
993 void of_device_unregister(struct of_device *ofdev)
994 {
995 device_remove_file(&ofdev->dev, &dev_attr_devspec);
996 device_unregister(&ofdev->dev);
997 }
999 struct of_device* of_platform_device_create(struct device_node *np,
1000 const char *bus_id,
1001 struct device *parent,
1002 struct bus_type *bus)
1004 struct of_device *dev;
1006 dev = kmalloc(sizeof(*dev), GFP_KERNEL);
1007 if (!dev)
1008 return NULL;
1009 memset(dev, 0, sizeof(*dev));
1011 dev->dev.parent = parent;
1012 dev->dev.bus = bus;
1013 dev->dev.release = of_release_dev;
1015 strlcpy(dev->dev.bus_id, bus_id, BUS_ID_SIZE);
1017 if (of_device_register(dev) != 0) {
1018 kfree(dev);
1019 return NULL;
1022 return dev;
1025 EXPORT_SYMBOL(of_match_device);
1026 EXPORT_SYMBOL(of_register_driver);
1027 EXPORT_SYMBOL(of_unregister_driver);
1028 EXPORT_SYMBOL(of_device_register);
1029 EXPORT_SYMBOL(of_device_unregister);
1030 EXPORT_SYMBOL(of_dev_get);
1031 EXPORT_SYMBOL(of_dev_put);
1032 EXPORT_SYMBOL(of_platform_device_create);
1033 EXPORT_SYMBOL(of_release_dev);