pci.c 11 KB

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