pci_common.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /* pci_common.c: PCI controller common support.
  2. *
  3. * Copyright (C) 1999, 2007 David S. Miller (davem@davemloft.net)
  4. */
  5. #include <linux/string.h>
  6. #include <linux/slab.h>
  7. #include <linux/init.h>
  8. #include <linux/pci.h>
  9. #include <linux/device.h>
  10. #include <asm/prom.h>
  11. #include <asm/of_device.h>
  12. #include <asm/oplib.h>
  13. #include "pci_impl.h"
  14. void pci_get_pbm_props(struct pci_pbm_info *pbm)
  15. {
  16. const u32 *val = of_get_property(pbm->prom_node, "bus-range", NULL);
  17. pbm->pci_first_busno = val[0];
  18. pbm->pci_last_busno = val[1];
  19. val = of_get_property(pbm->prom_node, "ino-bitmap", NULL);
  20. if (val) {
  21. pbm->ino_bitmap = (((u64)val[1] << 32UL) |
  22. ((u64)val[0] << 0UL));
  23. }
  24. }
  25. static void pci_register_legacy_regions(struct resource *io_res,
  26. struct resource *mem_res)
  27. {
  28. struct resource *p;
  29. /* VGA Video RAM. */
  30. p = kzalloc(sizeof(*p), GFP_KERNEL);
  31. if (!p)
  32. return;
  33. p->name = "Video RAM area";
  34. p->start = mem_res->start + 0xa0000UL;
  35. p->end = p->start + 0x1ffffUL;
  36. p->flags = IORESOURCE_BUSY;
  37. request_resource(mem_res, p);
  38. p = kzalloc(sizeof(*p), GFP_KERNEL);
  39. if (!p)
  40. return;
  41. p->name = "System ROM";
  42. p->start = mem_res->start + 0xf0000UL;
  43. p->end = p->start + 0xffffUL;
  44. p->flags = IORESOURCE_BUSY;
  45. request_resource(mem_res, p);
  46. p = kzalloc(sizeof(*p), GFP_KERNEL);
  47. if (!p)
  48. return;
  49. p->name = "Video ROM";
  50. p->start = mem_res->start + 0xc0000UL;
  51. p->end = p->start + 0x7fffUL;
  52. p->flags = IORESOURCE_BUSY;
  53. request_resource(mem_res, p);
  54. }
  55. static void pci_register_iommu_region(struct pci_pbm_info *pbm)
  56. {
  57. const u32 *vdma = of_get_property(pbm->prom_node, "virtual-dma", NULL);
  58. if (vdma) {
  59. struct resource *rp = kmalloc(sizeof(*rp), GFP_KERNEL);
  60. if (!rp) {
  61. prom_printf("Cannot allocate IOMMU resource.\n");
  62. prom_halt();
  63. }
  64. rp->name = "IOMMU";
  65. rp->start = pbm->mem_space.start + (unsigned long) vdma[0];
  66. rp->end = rp->start + (unsigned long) vdma[1] - 1UL;
  67. rp->flags = IORESOURCE_BUSY;
  68. request_resource(&pbm->mem_space, rp);
  69. }
  70. }
  71. void pci_determine_mem_io_space(struct pci_pbm_info *pbm)
  72. {
  73. const struct linux_prom_pci_ranges *pbm_ranges;
  74. int i, saw_mem, saw_io;
  75. int num_pbm_ranges;
  76. saw_mem = saw_io = 0;
  77. pbm_ranges = of_get_property(pbm->prom_node, "ranges", &i);
  78. num_pbm_ranges = i / sizeof(*pbm_ranges);
  79. for (i = 0; i < num_pbm_ranges; i++) {
  80. const struct linux_prom_pci_ranges *pr = &pbm_ranges[i];
  81. unsigned long a;
  82. u32 parent_phys_hi, parent_phys_lo;
  83. int type;
  84. parent_phys_hi = pr->parent_phys_hi;
  85. parent_phys_lo = pr->parent_phys_lo;
  86. if (tlb_type == hypervisor)
  87. parent_phys_hi &= 0x0fffffff;
  88. type = (pr->child_phys_hi >> 24) & 0x3;
  89. a = (((unsigned long)parent_phys_hi << 32UL) |
  90. ((unsigned long)parent_phys_lo << 0UL));
  91. switch (type) {
  92. case 0:
  93. /* PCI config space, 16MB */
  94. pbm->config_space = a;
  95. break;
  96. case 1:
  97. /* 16-bit IO space, 16MB */
  98. pbm->io_space.start = a;
  99. pbm->io_space.end = a + ((16UL*1024UL*1024UL) - 1UL);
  100. pbm->io_space.flags = IORESOURCE_IO;
  101. saw_io = 1;
  102. break;
  103. case 2:
  104. /* 32-bit MEM space, 2GB */
  105. pbm->mem_space.start = a;
  106. pbm->mem_space.end = a + (0x80000000UL - 1UL);
  107. pbm->mem_space.flags = IORESOURCE_MEM;
  108. saw_mem = 1;
  109. break;
  110. case 3:
  111. /* XXX 64-bit MEM handling XXX */
  112. default:
  113. break;
  114. };
  115. }
  116. if (!saw_io || !saw_mem) {
  117. prom_printf("%s: Fatal error, missing %s PBM range.\n",
  118. pbm->name,
  119. (!saw_io ? "IO" : "MEM"));
  120. prom_halt();
  121. }
  122. printk("%s: PCI IO[%lx] MEM[%lx]\n",
  123. pbm->name,
  124. pbm->io_space.start,
  125. pbm->mem_space.start);
  126. pbm->io_space.name = pbm->mem_space.name = pbm->name;
  127. request_resource(&ioport_resource, &pbm->io_space);
  128. request_resource(&iomem_resource, &pbm->mem_space);
  129. pci_register_legacy_regions(&pbm->io_space,
  130. &pbm->mem_space);
  131. pci_register_iommu_region(pbm);
  132. }
  133. /* Generic helper routines for PCI error reporting. */
  134. void pci_scan_for_target_abort(struct pci_pbm_info *pbm,
  135. struct pci_bus *pbus)
  136. {
  137. struct pci_dev *pdev;
  138. struct pci_bus *bus;
  139. list_for_each_entry(pdev, &pbus->devices, bus_list) {
  140. u16 status, error_bits;
  141. pci_read_config_word(pdev, PCI_STATUS, &status);
  142. error_bits =
  143. (status & (PCI_STATUS_SIG_TARGET_ABORT |
  144. PCI_STATUS_REC_TARGET_ABORT));
  145. if (error_bits) {
  146. pci_write_config_word(pdev, PCI_STATUS, error_bits);
  147. printk("%s: Device %s saw Target Abort [%016x]\n",
  148. pbm->name, pci_name(pdev), status);
  149. }
  150. }
  151. list_for_each_entry(bus, &pbus->children, node)
  152. pci_scan_for_target_abort(pbm, bus);
  153. }
  154. void pci_scan_for_master_abort(struct pci_pbm_info *pbm,
  155. struct pci_bus *pbus)
  156. {
  157. struct pci_dev *pdev;
  158. struct pci_bus *bus;
  159. list_for_each_entry(pdev, &pbus->devices, bus_list) {
  160. u16 status, error_bits;
  161. pci_read_config_word(pdev, PCI_STATUS, &status);
  162. error_bits =
  163. (status & (PCI_STATUS_REC_MASTER_ABORT));
  164. if (error_bits) {
  165. pci_write_config_word(pdev, PCI_STATUS, error_bits);
  166. printk("%s: Device %s received Master Abort [%016x]\n",
  167. pbm->name, pci_name(pdev), status);
  168. }
  169. }
  170. list_for_each_entry(bus, &pbus->children, node)
  171. pci_scan_for_master_abort(pbm, bus);
  172. }
  173. void pci_scan_for_parity_error(struct pci_pbm_info *pbm,
  174. struct pci_bus *pbus)
  175. {
  176. struct pci_dev *pdev;
  177. struct pci_bus *bus;
  178. list_for_each_entry(pdev, &pbus->devices, bus_list) {
  179. u16 status, error_bits;
  180. pci_read_config_word(pdev, PCI_STATUS, &status);
  181. error_bits =
  182. (status & (PCI_STATUS_PARITY |
  183. PCI_STATUS_DETECTED_PARITY));
  184. if (error_bits) {
  185. pci_write_config_word(pdev, PCI_STATUS, error_bits);
  186. printk("%s: Device %s saw Parity Error [%016x]\n",
  187. pbm->name, pci_name(pdev), status);
  188. }
  189. }
  190. list_for_each_entry(bus, &pbus->children, node)
  191. pci_scan_for_parity_error(pbm, bus);
  192. }