pci_common.c 5.6 KB

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