pci.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * New-style PCI core.
  4. *
  5. * Copyright (c) 2004 - 2009 Paul Mundt
  6. * Copyright (c) 2002 M. R. Brown
  7. *
  8. * Modelled after arch/mips/pci/pci.c:
  9. * Copyright (C) 2003, 04 Ralf Baechle ([email protected])
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/mm.h>
  13. #include <linux/pci.h>
  14. #include <linux/init.h>
  15. #include <linux/types.h>
  16. #include <linux/io.h>
  17. #include <linux/mutex.h>
  18. #include <linux/spinlock.h>
  19. #include <linux/export.h>
  20. unsigned long PCIBIOS_MIN_IO = 0x0000;
  21. unsigned long PCIBIOS_MIN_MEM = 0;
  22. /*
  23. * The PCI controller list.
  24. */
  25. static struct pci_channel *hose_head, **hose_tail = &hose_head;
  26. static int pci_initialized;
  27. static void pcibios_scanbus(struct pci_channel *hose)
  28. {
  29. static int next_busno;
  30. static int need_domain_info;
  31. LIST_HEAD(resources);
  32. struct resource *res;
  33. resource_size_t offset;
  34. int i, ret;
  35. struct pci_host_bridge *bridge;
  36. bridge = pci_alloc_host_bridge(0);
  37. if (!bridge)
  38. return;
  39. for (i = 0; i < hose->nr_resources; i++) {
  40. res = hose->resources + i;
  41. offset = 0;
  42. if (res->flags & IORESOURCE_DISABLED)
  43. continue;
  44. if (res->flags & IORESOURCE_IO)
  45. offset = hose->io_offset;
  46. else if (res->flags & IORESOURCE_MEM)
  47. offset = hose->mem_offset;
  48. pci_add_resource_offset(&resources, res, offset);
  49. }
  50. list_splice_init(&resources, &bridge->windows);
  51. bridge->dev.parent = NULL;
  52. bridge->sysdata = hose;
  53. bridge->busnr = next_busno;
  54. bridge->ops = hose->pci_ops;
  55. bridge->swizzle_irq = pci_common_swizzle;
  56. bridge->map_irq = pcibios_map_platform_irq;
  57. ret = pci_scan_root_bus_bridge(bridge);
  58. if (ret) {
  59. pci_free_host_bridge(bridge);
  60. return;
  61. }
  62. hose->bus = bridge->bus;
  63. need_domain_info = need_domain_info || hose->index;
  64. hose->need_domain_info = need_domain_info;
  65. next_busno = hose->bus->busn_res.end + 1;
  66. /* Don't allow 8-bit bus number overflow inside the hose -
  67. reserve some space for bridges. */
  68. if (next_busno > 224) {
  69. next_busno = 0;
  70. need_domain_info = 1;
  71. }
  72. pci_bus_size_bridges(hose->bus);
  73. pci_bus_assign_resources(hose->bus);
  74. pci_bus_add_devices(hose->bus);
  75. }
  76. /*
  77. * This interrupt-safe spinlock protects all accesses to PCI
  78. * configuration space.
  79. */
  80. DEFINE_RAW_SPINLOCK(pci_config_lock);
  81. static DEFINE_MUTEX(pci_scan_mutex);
  82. int register_pci_controller(struct pci_channel *hose)
  83. {
  84. int i;
  85. for (i = 0; i < hose->nr_resources; i++) {
  86. struct resource *res = hose->resources + i;
  87. if (res->flags & IORESOURCE_DISABLED)
  88. continue;
  89. if (res->flags & IORESOURCE_IO) {
  90. if (request_resource(&ioport_resource, res) < 0)
  91. goto out;
  92. } else {
  93. if (request_resource(&iomem_resource, res) < 0)
  94. goto out;
  95. }
  96. }
  97. *hose_tail = hose;
  98. hose_tail = &hose->next;
  99. /*
  100. * Do not panic here but later - this might happen before console init.
  101. */
  102. if (!hose->io_map_base) {
  103. pr_warn("registering PCI controller with io_map_base unset\n");
  104. }
  105. /*
  106. * Setup the ERR/PERR and SERR timers, if available.
  107. */
  108. pcibios_enable_timers(hose);
  109. /*
  110. * Scan the bus if it is register after the PCI subsystem
  111. * initialization.
  112. */
  113. if (pci_initialized) {
  114. mutex_lock(&pci_scan_mutex);
  115. pcibios_scanbus(hose);
  116. mutex_unlock(&pci_scan_mutex);
  117. }
  118. return 0;
  119. out:
  120. for (--i; i >= 0; i--)
  121. release_resource(&hose->resources[i]);
  122. pr_warn("Skipping PCI bus scan due to resource conflict\n");
  123. return -1;
  124. }
  125. static int __init pcibios_init(void)
  126. {
  127. struct pci_channel *hose;
  128. /* Scan all of the recorded PCI controllers. */
  129. for (hose = hose_head; hose; hose = hose->next)
  130. pcibios_scanbus(hose);
  131. pci_initialized = 1;
  132. return 0;
  133. }
  134. subsys_initcall(pcibios_init);
  135. /*
  136. * We need to avoid collisions with `mirrored' VGA ports
  137. * and other strange ISA hardware, so we always want the
  138. * addresses to be allocated in the 0x000-0x0ff region
  139. * modulo 0x400.
  140. */
  141. resource_size_t pcibios_align_resource(void *data, const struct resource *res,
  142. resource_size_t size, resource_size_t align)
  143. {
  144. struct pci_dev *dev = data;
  145. struct pci_channel *hose = dev->sysdata;
  146. resource_size_t start = res->start;
  147. if (res->flags & IORESOURCE_IO) {
  148. if (start < PCIBIOS_MIN_IO + hose->resources[0].start)
  149. start = PCIBIOS_MIN_IO + hose->resources[0].start;
  150. /*
  151. * Put everything into 0x00-0xff region modulo 0x400.
  152. */
  153. if (start & 0x300)
  154. start = (start + 0x3ff) & ~0x3ff;
  155. }
  156. return start;
  157. }
  158. static void __init
  159. pcibios_bus_report_status_early(struct pci_channel *hose,
  160. int top_bus, int current_bus,
  161. unsigned int status_mask, int warn)
  162. {
  163. unsigned int pci_devfn;
  164. u16 status;
  165. int ret;
  166. for (pci_devfn = 0; pci_devfn < 0xff; pci_devfn++) {
  167. if (PCI_FUNC(pci_devfn))
  168. continue;
  169. ret = early_read_config_word(hose, top_bus, current_bus,
  170. pci_devfn, PCI_STATUS, &status);
  171. if (ret != PCIBIOS_SUCCESSFUL)
  172. continue;
  173. if (status == 0xffff)
  174. continue;
  175. early_write_config_word(hose, top_bus, current_bus,
  176. pci_devfn, PCI_STATUS,
  177. status & status_mask);
  178. if (warn)
  179. pr_cont("(%02x:%02x: %04X) ", current_bus, pci_devfn,
  180. status);
  181. }
  182. }
  183. /*
  184. * We can't use pci_find_device() here since we are
  185. * called from interrupt context.
  186. */
  187. static void __ref
  188. pcibios_bus_report_status(struct pci_bus *bus, unsigned int status_mask,
  189. int warn)
  190. {
  191. struct pci_dev *dev;
  192. list_for_each_entry(dev, &bus->devices, bus_list) {
  193. u16 status;
  194. /*
  195. * ignore host bridge - we handle
  196. * that separately
  197. */
  198. if (dev->bus->number == 0 && dev->devfn == 0)
  199. continue;
  200. pci_read_config_word(dev, PCI_STATUS, &status);
  201. if (status == 0xffff)
  202. continue;
  203. if ((status & status_mask) == 0)
  204. continue;
  205. /* clear the status errors */
  206. pci_write_config_word(dev, PCI_STATUS, status & status_mask);
  207. if (warn)
  208. pr_cont("(%s: %04X) ", pci_name(dev), status);
  209. }
  210. list_for_each_entry(dev, &bus->devices, bus_list)
  211. if (dev->subordinate)
  212. pcibios_bus_report_status(dev->subordinate, status_mask, warn);
  213. }
  214. void __ref pcibios_report_status(unsigned int status_mask, int warn)
  215. {
  216. struct pci_channel *hose;
  217. for (hose = hose_head; hose; hose = hose->next) {
  218. if (unlikely(!hose->bus))
  219. pcibios_bus_report_status_early(hose, hose_head->index,
  220. hose->index, status_mask, warn);
  221. else
  222. pcibios_bus_report_status(hose->bus, status_mask, warn);
  223. }
  224. }
  225. #ifndef CONFIG_GENERIC_IOMAP
  226. void __iomem *__pci_ioport_map(struct pci_dev *dev,
  227. unsigned long port, unsigned int nr)
  228. {
  229. struct pci_channel *chan = dev->sysdata;
  230. if (unlikely(!chan->io_map_base)) {
  231. chan->io_map_base = sh_io_port_base;
  232. if (pci_domains_supported)
  233. panic("To avoid data corruption io_map_base MUST be "
  234. "set with multiple PCI domains.");
  235. }
  236. return (void __iomem *)(chan->io_map_base + port);
  237. }
  238. void pci_iounmap(struct pci_dev *dev, void __iomem *addr)
  239. {
  240. iounmap(addr);
  241. }
  242. EXPORT_SYMBOL(pci_iounmap);
  243. #endif /* CONFIG_GENERIC_IOMAP */
  244. EXPORT_SYMBOL(PCIBIOS_MIN_IO);
  245. EXPORT_SYMBOL(PCIBIOS_MIN_MEM);