setup-res.c
上传用户:lgb322
上传日期:2013-02-24
资源大小:30529k
文件大小:6k
源码类别:

嵌入式Linux

开发平台:

Unix_Linux

  1. /*
  2.  * drivers/pci/setup-res.c
  3.  *
  4.  * Extruded from code written by
  5.  *      Dave Rusling (david.rusling@reo.mts.dec.com)
  6.  *      David Mosberger (davidm@cs.arizona.edu)
  7.  * David Miller (davem@redhat.com)
  8.  *
  9.  * Support routines for initializing a PCI subsystem.
  10.  */
  11. /* fixed for multiple pci buses, 1999 Andrea Arcangeli <andrea@suse.de> */
  12. /*
  13.  * Nov 2000, Ivan Kokshaysky <ink@jurassic.park.msu.ru>
  14.  *      Resource sorting
  15.  */
  16. #include <linux/init.h>
  17. #include <linux/kernel.h>
  18. #include <linux/pci.h>
  19. #include <linux/errno.h>
  20. #include <linux/ioport.h>
  21. #include <linux/cache.h>
  22. #include <linux/slab.h>
  23. #define DEBUG_CONFIG 0
  24. #if DEBUG_CONFIG
  25. # define DBGC(args)     printk args
  26. #else
  27. # define DBGC(args)
  28. #endif
  29. int __init
  30. pci_claim_resource(struct pci_dev *dev, int resource)
  31. {
  32.         struct resource *res = &dev->resource[resource];
  33. struct resource *root = pci_find_parent_resource(dev, res);
  34. int err;
  35. err = -EINVAL;
  36. if (root != NULL) {
  37. err = request_resource(root, res);
  38. if (err) {
  39. printk(KERN_ERR "PCI: Address space collision on "
  40.        "region %d of device %s [%lx:%lx]n",
  41.        resource, dev->name, res->start, res->end);
  42. }
  43. } else {
  44. printk(KERN_ERR "PCI: No parent found for region %d "
  45.        "of device %sn", resource, dev->name);
  46. }
  47. return err;
  48. }
  49. /*
  50.  * Given the PCI bus a device resides on, try to
  51.  * find an acceptable resource allocation for a
  52.  * specific device resource..
  53.  */
  54. static int pci_assign_bus_resource(const struct pci_bus *bus,
  55. struct pci_dev *dev,
  56. struct resource *res,
  57. unsigned long size,
  58. unsigned long min,
  59. unsigned int type_mask,
  60. int resno)
  61. {
  62. int i;
  63. type_mask |= IORESOURCE_IO | IORESOURCE_MEM;
  64. for (i = 0 ; i < 4; i++) {
  65. struct resource *r = bus->resource[i];
  66. if (!r)
  67. continue;
  68. /* type_mask must match */
  69. if ((res->flags ^ r->flags) & type_mask)
  70. continue;
  71. /* We cannot allocate a non-prefetching resource from a pre-fetching area */
  72. if ((r->flags & IORESOURCE_PREFETCH) && !(res->flags & IORESOURCE_PREFETCH))
  73. continue;
  74. /* Ok, try it out.. */
  75. if (allocate_resource(r, res, size, min, -1, size, pcibios_align_resource, dev) < 0)
  76. continue;
  77. /* Update PCI config space.  */
  78. pcibios_update_resource(dev, r, res, resno);
  79. return 0;
  80. }
  81. return -EBUSY;
  82. }
  83. int 
  84. pci_assign_resource(struct pci_dev *dev, int i)
  85. {
  86. const struct pci_bus *bus = dev->bus;
  87. struct resource *res = dev->resource + i;
  88. unsigned long size, min;
  89. size = res->end - res->start + 1;
  90. min = (res->flags & IORESOURCE_IO) ? PCIBIOS_MIN_IO : PCIBIOS_MIN_MEM;
  91. /* First, try exact prefetching match.. */
  92. if (pci_assign_bus_resource(bus, dev, res, size, min, IORESOURCE_PREFETCH, i) < 0) {
  93. /*
  94.  * That failed.
  95.  *
  96.  * But a prefetching area can handle a non-prefetching
  97.  * window (it will just not perform as well).
  98.  */
  99. if (!(res->flags & IORESOURCE_PREFETCH) || pci_assign_bus_resource(bus, dev, res, size, min, 0, i) < 0) {
  100. printk(KERN_ERR "PCI: Failed to allocate resource %d(%lx-%lx) for %sn",
  101.        i, res->start, res->end, dev->slot_name);
  102. return -EBUSY;
  103. }
  104. }
  105. DBGC((KERN_ERR "  got res[%lx:%lx] for resource %d of %sn", res->start,
  106. res->end, i, dev->name));
  107. return 0;
  108. }
  109. /* Sort resources of a given type by alignment */
  110. void __init
  111. pdev_sort_resources(struct pci_dev *dev,
  112.     struct resource_list *head, u32 type_mask)
  113. {
  114. int i;
  115. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  116. struct resource *r;
  117. struct resource_list *list, *tmp;
  118. unsigned long r_size;
  119. /* PCI-PCI bridges may have I/O ports or
  120.    memory on the primary bus */
  121. if (dev->class >> 8 == PCI_CLASS_BRIDGE_PCI &&
  122. i >= PCI_BRIDGE_RESOURCES)
  123. continue;
  124. r = &dev->resource[i];
  125. r_size = r->end - r->start;
  126. if (!(r->flags & type_mask) || r->parent)
  127. continue;
  128. if (!r_size) {
  129. printk(KERN_WARNING "PCI: Ignore bogus resource %d "
  130.  "[%lx:%lx] of %sn",
  131.   i, r->start, r->end, dev->name);
  132. continue;
  133. }
  134. for (list = head; ; list = list->next) {
  135. unsigned long size = 0;
  136. struct resource_list *ln = list->next;
  137. if (ln)
  138. size = ln->res->end - ln->res->start;
  139. if (r_size > size) {
  140. tmp = kmalloc(sizeof(*tmp), GFP_KERNEL);
  141. if (!tmp) {
  142. printk(KERN_ERR "pdev_sort_resources(): kmalloc() failed!n");
  143. continue;
  144. }
  145. tmp->next = ln;
  146. tmp->res = r;
  147. tmp->dev = dev;
  148. list->next = tmp;
  149. break;
  150. }
  151. }
  152. }
  153. }
  154. void __init
  155. pdev_enable_device(struct pci_dev *dev)
  156. {
  157. u32 reg;
  158. u16 cmd;
  159. int i;
  160. DBGC((KERN_ERR "PCI enable device: (%s)n", dev->name));
  161. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  162. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  163. struct resource *res = &dev->resource[i];
  164. if (res->flags & IORESOURCE_IO)
  165. cmd |= PCI_COMMAND_IO;
  166. else if (res->flags & IORESOURCE_MEM)
  167. cmd |= PCI_COMMAND_MEMORY;
  168. }
  169. /* Special case, disable the ROM.  Several devices act funny
  170.    (ie. do not respond to memory space writes) when it is left
  171.    enabled.  A good example are QlogicISP adapters.  */
  172. if (dev->rom_base_reg) {
  173. pci_read_config_dword(dev, dev->rom_base_reg, &reg);
  174. reg &= ~PCI_ROM_ADDRESS_ENABLE;
  175. pci_write_config_dword(dev, dev->rom_base_reg, reg);
  176. dev->resource[PCI_ROM_RESOURCE].flags &= ~PCI_ROM_ADDRESS_ENABLE;
  177. }
  178. /* All of these (may) have I/O scattered all around and may not
  179.    use I/O base address registers at all.  So we just have to
  180.    always enable IO to these devices.  */
  181. if ((dev->class >> 8) == PCI_CLASS_NOT_DEFINED
  182.     || (dev->class >> 8) == PCI_CLASS_NOT_DEFINED_VGA
  183.     || (dev->class >> 8) == PCI_CLASS_STORAGE_IDE
  184.     || (dev->class >> 16) == PCI_BASE_CLASS_DISPLAY) {
  185. cmd |= PCI_COMMAND_IO;
  186. }
  187. /* ??? Always turn on bus mastering.  If the device doesn't support
  188.    it, the bit will go into the bucket. */
  189. cmd |= PCI_COMMAND_MASTER;
  190. /* Set the cache line and default latency (32).  */
  191. pci_write_config_word(dev, PCI_CACHE_LINE_SIZE,
  192. (32 << 8) | (L1_CACHE_BYTES / sizeof(u32)));
  193. /* Enable the appropriate bits in the PCI command register.  */
  194. pci_write_config_word(dev, PCI_COMMAND, cmd);
  195. DBGC((KERN_ERR "  cmd reg 0x%xn", cmd));
  196. }