pci.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/arch/alpha/kernel/pci.c
  4. *
  5. * Extruded from code written by
  6. * Dave Rusling ([email protected])
  7. * David Mosberger ([email protected])
  8. */
  9. /* 2.3.x PCI/resources, 1999 Andrea Arcangeli <[email protected]> */
  10. /*
  11. * Nov 2000, Ivan Kokshaysky <[email protected]>
  12. * PCI-PCI bridges cleanup
  13. */
  14. #include <linux/string.h>
  15. #include <linux/pci.h>
  16. #include <linux/init.h>
  17. #include <linux/ioport.h>
  18. #include <linux/kernel.h>
  19. #include <linux/memblock.h>
  20. #include <linux/module.h>
  21. #include <linux/cache.h>
  22. #include <linux/slab.h>
  23. #include <linux/syscalls.h>
  24. #include <asm/machvec.h>
  25. #include "proto.h"
  26. #include "pci_impl.h"
  27. /*
  28. * Some string constants used by the various core logics.
  29. */
  30. const char *const pci_io_names[] = {
  31. "PCI IO bus 0", "PCI IO bus 1", "PCI IO bus 2", "PCI IO bus 3",
  32. "PCI IO bus 4", "PCI IO bus 5", "PCI IO bus 6", "PCI IO bus 7"
  33. };
  34. const char *const pci_mem_names[] = {
  35. "PCI mem bus 0", "PCI mem bus 1", "PCI mem bus 2", "PCI mem bus 3",
  36. "PCI mem bus 4", "PCI mem bus 5", "PCI mem bus 6", "PCI mem bus 7"
  37. };
  38. const char pci_hae0_name[] = "HAE0";
  39. /*
  40. * If PCI_PROBE_ONLY in pci_flags is set, we don't change any PCI resource
  41. * assignments.
  42. */
  43. /*
  44. * The PCI controller list.
  45. */
  46. struct pci_controller *hose_head, **hose_tail = &hose_head;
  47. struct pci_controller *pci_isa_hose;
  48. /*
  49. * Quirks.
  50. */
  51. static void quirk_isa_bridge(struct pci_dev *dev)
  52. {
  53. dev->class = PCI_CLASS_BRIDGE_ISA << 8;
  54. }
  55. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82378, quirk_isa_bridge);
  56. static void quirk_cypress(struct pci_dev *dev)
  57. {
  58. /* The Notorious Cy82C693 chip. */
  59. /* The generic legacy mode IDE fixup in drivers/pci/probe.c
  60. doesn't work correctly with the Cypress IDE controller as
  61. it has non-standard register layout. Fix that. */
  62. if (dev->class >> 8 == PCI_CLASS_STORAGE_IDE) {
  63. dev->resource[2].start = dev->resource[3].start = 0;
  64. dev->resource[2].end = dev->resource[3].end = 0;
  65. dev->resource[2].flags = dev->resource[3].flags = 0;
  66. if (PCI_FUNC(dev->devfn) == 2) {
  67. dev->resource[0].start = 0x170;
  68. dev->resource[0].end = 0x177;
  69. dev->resource[1].start = 0x376;
  70. dev->resource[1].end = 0x376;
  71. }
  72. }
  73. /* The Cypress bridge responds on the PCI bus in the address range
  74. 0xffff0000-0xffffffff (conventional x86 BIOS ROM). There is no
  75. way to turn this off. The bridge also supports several extended
  76. BIOS ranges (disabled after power-up), and some consoles do turn
  77. them on. So if we use a large direct-map window, or a large SG
  78. window, we must avoid the entire 0xfff00000-0xffffffff region. */
  79. if (dev->class >> 8 == PCI_CLASS_BRIDGE_ISA) {
  80. if (__direct_map_base + __direct_map_size >= 0xfff00000UL)
  81. __direct_map_size = 0xfff00000UL - __direct_map_base;
  82. else {
  83. struct pci_controller *hose = dev->sysdata;
  84. struct pci_iommu_arena *pci = hose->sg_pci;
  85. if (pci && pci->dma_base + pci->size >= 0xfff00000UL)
  86. pci->size = 0xfff00000UL - pci->dma_base;
  87. }
  88. }
  89. }
  90. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_CONTAQ, PCI_DEVICE_ID_CONTAQ_82C693, quirk_cypress);
  91. /* Called for each device after PCI setup is done. */
  92. static void pcibios_fixup_final(struct pci_dev *dev)
  93. {
  94. unsigned int class = dev->class >> 8;
  95. if (class == PCI_CLASS_BRIDGE_ISA || class == PCI_CLASS_BRIDGE_EISA) {
  96. dev->dma_mask = MAX_ISA_DMA_ADDRESS - 1;
  97. isa_bridge = dev;
  98. }
  99. }
  100. DECLARE_PCI_FIXUP_FINAL(PCI_ANY_ID, PCI_ANY_ID, pcibios_fixup_final);
  101. /* Just declaring that the power-of-ten prefixes are actually the
  102. power-of-two ones doesn't make it true :) */
  103. #define KB 1024
  104. #define MB (1024*KB)
  105. #define GB (1024*MB)
  106. resource_size_t
  107. pcibios_align_resource(void *data, const struct resource *res,
  108. resource_size_t size, resource_size_t align)
  109. {
  110. struct pci_dev *dev = data;
  111. struct pci_controller *hose = dev->sysdata;
  112. unsigned long alignto;
  113. resource_size_t start = res->start;
  114. if (res->flags & IORESOURCE_IO) {
  115. /* Make sure we start at our min on all hoses */
  116. if (start - hose->io_space->start < PCIBIOS_MIN_IO)
  117. start = PCIBIOS_MIN_IO + hose->io_space->start;
  118. /*
  119. * Put everything into 0x00-0xff region modulo 0x400
  120. */
  121. if (start & 0x300)
  122. start = (start + 0x3ff) & ~0x3ff;
  123. }
  124. else if (res->flags & IORESOURCE_MEM) {
  125. /* Make sure we start at our min on all hoses */
  126. if (start - hose->mem_space->start < PCIBIOS_MIN_MEM)
  127. start = PCIBIOS_MIN_MEM + hose->mem_space->start;
  128. /*
  129. * The following holds at least for the Low Cost
  130. * Alpha implementation of the PCI interface:
  131. *
  132. * In sparse memory address space, the first
  133. * octant (16MB) of every 128MB segment is
  134. * aliased to the very first 16 MB of the
  135. * address space (i.e., it aliases the ISA
  136. * memory address space). Thus, we try to
  137. * avoid allocating PCI devices in that range.
  138. * Can be allocated in 2nd-7th octant only.
  139. * Devices that need more than 112MB of
  140. * address space must be accessed through
  141. * dense memory space only!
  142. */
  143. /* Align to multiple of size of minimum base. */
  144. alignto = max_t(resource_size_t, 0x1000, align);
  145. start = ALIGN(start, alignto);
  146. if (hose->sparse_mem_base && size <= 7 * 16*MB) {
  147. if (((start / (16*MB)) & 0x7) == 0) {
  148. start &= ~(128*MB - 1);
  149. start += 16*MB;
  150. start = ALIGN(start, alignto);
  151. }
  152. if (start/(128*MB) != (start + size - 1)/(128*MB)) {
  153. start &= ~(128*MB - 1);
  154. start += (128 + 16)*MB;
  155. start = ALIGN(start, alignto);
  156. }
  157. }
  158. }
  159. return start;
  160. }
  161. #undef KB
  162. #undef MB
  163. #undef GB
  164. static int __init
  165. pcibios_init(void)
  166. {
  167. if (alpha_mv.init_pci)
  168. alpha_mv.init_pci();
  169. return 0;
  170. }
  171. subsys_initcall(pcibios_init);
  172. #ifdef ALPHA_RESTORE_SRM_SETUP
  173. /* Store PCI device configuration left by SRM here. */
  174. struct pdev_srm_saved_conf
  175. {
  176. struct pdev_srm_saved_conf *next;
  177. struct pci_dev *dev;
  178. };
  179. static struct pdev_srm_saved_conf *srm_saved_configs;
  180. static void pdev_save_srm_config(struct pci_dev *dev)
  181. {
  182. struct pdev_srm_saved_conf *tmp;
  183. static int printed = 0;
  184. if (!alpha_using_srm || pci_has_flag(PCI_PROBE_ONLY))
  185. return;
  186. if (!printed) {
  187. printk(KERN_INFO "pci: enabling save/restore of SRM state\n");
  188. printed = 1;
  189. }
  190. tmp = kmalloc(sizeof(*tmp), GFP_KERNEL);
  191. if (!tmp) {
  192. printk(KERN_ERR "%s: kmalloc() failed!\n", __func__);
  193. return;
  194. }
  195. tmp->next = srm_saved_configs;
  196. tmp->dev = dev;
  197. pci_save_state(dev);
  198. srm_saved_configs = tmp;
  199. }
  200. void
  201. pci_restore_srm_config(void)
  202. {
  203. struct pdev_srm_saved_conf *tmp;
  204. /* No need to restore if probed only. */
  205. if (pci_has_flag(PCI_PROBE_ONLY))
  206. return;
  207. /* Restore SRM config. */
  208. for (tmp = srm_saved_configs; tmp; tmp = tmp->next) {
  209. pci_restore_state(tmp->dev);
  210. }
  211. }
  212. #else
  213. #define pdev_save_srm_config(dev) do {} while (0)
  214. #endif
  215. void pcibios_fixup_bus(struct pci_bus *bus)
  216. {
  217. struct pci_dev *dev = bus->self;
  218. if (pci_has_flag(PCI_PROBE_ONLY) && dev &&
  219. (dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
  220. pci_read_bridge_bases(bus);
  221. }
  222. list_for_each_entry(dev, &bus->devices, bus_list) {
  223. pdev_save_srm_config(dev);
  224. }
  225. }
  226. /*
  227. * If we set up a device for bus mastering, we need to check the latency
  228. * timer as certain firmware forgets to set it properly, as seen
  229. * on SX164 and LX164 with SRM.
  230. */
  231. void
  232. pcibios_set_master(struct pci_dev *dev)
  233. {
  234. u8 lat;
  235. pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
  236. if (lat >= 16) return;
  237. printk("PCI: Setting latency timer of device %s to 64\n",
  238. pci_name(dev));
  239. pci_write_config_byte(dev, PCI_LATENCY_TIMER, 64);
  240. }
  241. void __init
  242. pcibios_claim_one_bus(struct pci_bus *b)
  243. {
  244. struct pci_dev *dev;
  245. struct pci_bus *child_bus;
  246. list_for_each_entry(dev, &b->devices, bus_list) {
  247. int i;
  248. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  249. struct resource *r = &dev->resource[i];
  250. if (r->parent || !r->start || !r->flags)
  251. continue;
  252. if (pci_has_flag(PCI_PROBE_ONLY) ||
  253. (r->flags & IORESOURCE_PCI_FIXED)) {
  254. if (pci_claim_resource(dev, i) == 0)
  255. continue;
  256. pci_claim_bridge_resource(dev, i);
  257. }
  258. }
  259. }
  260. list_for_each_entry(child_bus, &b->children, node)
  261. pcibios_claim_one_bus(child_bus);
  262. }
  263. static void __init
  264. pcibios_claim_console_setup(void)
  265. {
  266. struct pci_bus *b;
  267. list_for_each_entry(b, &pci_root_buses, node)
  268. pcibios_claim_one_bus(b);
  269. }
  270. void __init
  271. common_init_pci(void)
  272. {
  273. struct pci_controller *hose;
  274. struct list_head resources;
  275. struct pci_host_bridge *bridge;
  276. struct pci_bus *bus;
  277. int ret, next_busno;
  278. int need_domain_info = 0;
  279. u32 pci_mem_end;
  280. u32 sg_base;
  281. unsigned long end;
  282. /* Scan all of the recorded PCI controllers. */
  283. for (next_busno = 0, hose = hose_head; hose; hose = hose->next) {
  284. sg_base = hose->sg_pci ? hose->sg_pci->dma_base : ~0;
  285. /* Adjust hose mem_space limit to prevent PCI allocations
  286. in the iommu windows. */
  287. pci_mem_end = min((u32)__direct_map_base, sg_base) - 1;
  288. end = hose->mem_space->start + pci_mem_end;
  289. if (hose->mem_space->end > end)
  290. hose->mem_space->end = end;
  291. INIT_LIST_HEAD(&resources);
  292. pci_add_resource_offset(&resources, hose->io_space,
  293. hose->io_space->start);
  294. pci_add_resource_offset(&resources, hose->mem_space,
  295. hose->mem_space->start);
  296. bridge = pci_alloc_host_bridge(0);
  297. if (!bridge)
  298. continue;
  299. list_splice_init(&resources, &bridge->windows);
  300. bridge->dev.parent = NULL;
  301. bridge->sysdata = hose;
  302. bridge->busnr = next_busno;
  303. bridge->ops = alpha_mv.pci_ops;
  304. bridge->swizzle_irq = alpha_mv.pci_swizzle;
  305. bridge->map_irq = alpha_mv.pci_map_irq;
  306. ret = pci_scan_root_bus_bridge(bridge);
  307. if (ret) {
  308. pci_free_host_bridge(bridge);
  309. continue;
  310. }
  311. bus = hose->bus = bridge->bus;
  312. hose->need_domain_info = need_domain_info;
  313. next_busno = bus->busn_res.end + 1;
  314. /* Don't allow 8-bit bus number overflow inside the hose -
  315. reserve some space for bridges. */
  316. if (next_busno > 224) {
  317. next_busno = 0;
  318. need_domain_info = 1;
  319. }
  320. }
  321. pcibios_claim_console_setup();
  322. pci_assign_unassigned_resources();
  323. for (hose = hose_head; hose; hose = hose->next) {
  324. bus = hose->bus;
  325. if (bus)
  326. pci_bus_add_devices(bus);
  327. }
  328. }
  329. struct pci_controller * __init
  330. alloc_pci_controller(void)
  331. {
  332. struct pci_controller *hose;
  333. hose = memblock_alloc(sizeof(*hose), SMP_CACHE_BYTES);
  334. if (!hose)
  335. panic("%s: Failed to allocate %zu bytes\n", __func__,
  336. sizeof(*hose));
  337. *hose_tail = hose;
  338. hose_tail = &hose->next;
  339. return hose;
  340. }
  341. struct resource * __init
  342. alloc_resource(void)
  343. {
  344. void *ptr = memblock_alloc(sizeof(struct resource), SMP_CACHE_BYTES);
  345. if (!ptr)
  346. panic("%s: Failed to allocate %zu bytes\n", __func__,
  347. sizeof(struct resource));
  348. return ptr;
  349. }
  350. /* Provide information on locations of various I/O regions in physical
  351. memory. Do this on a per-card basis so that we choose the right hose. */
  352. SYSCALL_DEFINE3(pciconfig_iobase, long, which, unsigned long, bus,
  353. unsigned long, dfn)
  354. {
  355. struct pci_controller *hose;
  356. struct pci_dev *dev;
  357. /* from hose or from bus.devfn */
  358. if (which & IOBASE_FROM_HOSE) {
  359. for(hose = hose_head; hose; hose = hose->next)
  360. if (hose->index == bus) break;
  361. if (!hose) return -ENODEV;
  362. } else {
  363. /* Special hook for ISA access. */
  364. if (bus == 0 && dfn == 0) {
  365. hose = pci_isa_hose;
  366. } else {
  367. dev = pci_get_domain_bus_and_slot(0, bus, dfn);
  368. if (!dev)
  369. return -ENODEV;
  370. hose = dev->sysdata;
  371. pci_dev_put(dev);
  372. }
  373. }
  374. switch (which & ~IOBASE_FROM_HOSE) {
  375. case IOBASE_HOSE:
  376. return hose->index;
  377. case IOBASE_SPARSE_MEM:
  378. return hose->sparse_mem_base;
  379. case IOBASE_DENSE_MEM:
  380. return hose->dense_mem_base;
  381. case IOBASE_SPARSE_IO:
  382. return hose->sparse_io_base;
  383. case IOBASE_DENSE_IO:
  384. return hose->dense_io_base;
  385. case IOBASE_ROOT_BUS:
  386. return hose->bus->number;
  387. }
  388. return -EOPNOTSUPP;
  389. }
  390. /* Destroy an __iomem token. Not copied from lib/iomap.c. */
  391. void pci_iounmap(struct pci_dev *dev, void __iomem * addr)
  392. {
  393. if (__is_mmio(addr))
  394. iounmap(addr);
  395. }
  396. EXPORT_SYMBOL(pci_iounmap);
  397. /* FIXME: Some boxes have multiple ISA bridges! */
  398. struct pci_dev *isa_bridge;
  399. EXPORT_SYMBOL(isa_bridge);