pci.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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 __init
  50. quirk_isa_bridge(struct pci_dev *dev)
  51. {
  52. dev->class = PCI_CLASS_BRIDGE_ISA << 8;
  53. }
  54. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82378, quirk_isa_bridge);
  55. static void __init
  56. 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 __init
  93. pcibios_fixup_final(struct pci_dev *dev)
  94. {
  95. unsigned int class = dev->class >> 8;
  96. if (class == PCI_CLASS_BRIDGE_ISA || class == PCI_CLASS_BRIDGE_EISA) {
  97. dev->dma_mask = MAX_ISA_DMA_ADDRESS - 1;
  98. isa_bridge = dev;
  99. }
  100. }
  101. DECLARE_PCI_FIXUP_FINAL(PCI_ANY_ID, PCI_ANY_ID, pcibios_fixup_final);
  102. /* Just declaring that the power-of-ten prefixes are actually the
  103. power-of-two ones doesn't make it true :) */
  104. #define KB 1024
  105. #define MB (1024*KB)
  106. #define GB (1024*MB)
  107. resource_size_t
  108. pcibios_align_resource(void *data, const struct resource *res,
  109. resource_size_t size, resource_size_t align)
  110. {
  111. struct pci_dev *dev = data;
  112. struct pci_controller *hose = dev->sysdata;
  113. unsigned long alignto;
  114. resource_size_t start = res->start;
  115. if (res->flags & IORESOURCE_IO) {
  116. /* Make sure we start at our min on all hoses */
  117. if (start - hose->io_space->start < PCIBIOS_MIN_IO)
  118. start = PCIBIOS_MIN_IO + hose->io_space->start;
  119. /*
  120. * Put everything into 0x00-0xff region modulo 0x400
  121. */
  122. if (start & 0x300)
  123. start = (start + 0x3ff) & ~0x3ff;
  124. }
  125. else if (res->flags & IORESOURCE_MEM) {
  126. /* Make sure we start at our min on all hoses */
  127. if (start - hose->mem_space->start < PCIBIOS_MIN_MEM)
  128. start = PCIBIOS_MIN_MEM + hose->mem_space->start;
  129. /*
  130. * The following holds at least for the Low Cost
  131. * Alpha implementation of the PCI interface:
  132. *
  133. * In sparse memory address space, the first
  134. * octant (16MB) of every 128MB segment is
  135. * aliased to the very first 16 MB of the
  136. * address space (i.e., it aliases the ISA
  137. * memory address space). Thus, we try to
  138. * avoid allocating PCI devices in that range.
  139. * Can be allocated in 2nd-7th octant only.
  140. * Devices that need more than 112MB of
  141. * address space must be accessed through
  142. * dense memory space only!
  143. */
  144. /* Align to multiple of size of minimum base. */
  145. alignto = max_t(resource_size_t, 0x1000, align);
  146. start = ALIGN(start, alignto);
  147. if (hose->sparse_mem_base && size <= 7 * 16*MB) {
  148. if (((start / (16*MB)) & 0x7) == 0) {
  149. start &= ~(128*MB - 1);
  150. start += 16*MB;
  151. start = ALIGN(start, alignto);
  152. }
  153. if (start/(128*MB) != (start + size - 1)/(128*MB)) {
  154. start &= ~(128*MB - 1);
  155. start += (128 + 16)*MB;
  156. start = ALIGN(start, alignto);
  157. }
  158. }
  159. }
  160. return start;
  161. }
  162. #undef KB
  163. #undef MB
  164. #undef GB
  165. static int __init
  166. pcibios_init(void)
  167. {
  168. if (alpha_mv.init_pci)
  169. alpha_mv.init_pci();
  170. return 0;
  171. }
  172. subsys_initcall(pcibios_init);
  173. char * __devinit
  174. pcibios_setup(char *str)
  175. {
  176. return str;
  177. }
  178. #ifdef ALPHA_RESTORE_SRM_SETUP
  179. static struct pdev_srm_saved_conf *srm_saved_configs;
  180. void __devinit
  181. pdev_save_srm_config(struct pci_dev *dev)
  182. {
  183. struct pdev_srm_saved_conf *tmp;
  184. static int printed = 0;
  185. if (!alpha_using_srm || pci_has_flag(PCI_PROBE_ONLY))
  186. return;
  187. if (!printed) {
  188. printk(KERN_INFO "pci: enabling save/restore of SRM state\n");
  189. printed = 1;
  190. }
  191. tmp = kmalloc(sizeof(*tmp), GFP_KERNEL);
  192. if (!tmp) {
  193. printk(KERN_ERR "%s: kmalloc() failed!\n", __func__);
  194. return;
  195. }
  196. tmp->next = srm_saved_configs;
  197. tmp->dev = dev;
  198. pci_save_state(dev);
  199. srm_saved_configs = tmp;
  200. }
  201. void
  202. pci_restore_srm_config(void)
  203. {
  204. struct pdev_srm_saved_conf *tmp;
  205. /* No need to restore if probed only. */
  206. if (pci_has_flag(PCI_PROBE_ONLY))
  207. return;
  208. /* Restore SRM config. */
  209. for (tmp = srm_saved_configs; tmp; tmp = tmp->next) {
  210. pci_restore_state(tmp->dev);
  211. }
  212. }
  213. #endif
  214. void __devinit
  215. 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. void __init
  227. pcibios_update_irq(struct pci_dev *dev, int irq)
  228. {
  229. pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq);
  230. }
  231. int
  232. pcibios_enable_device(struct pci_dev *dev, int mask)
  233. {
  234. return pci_enable_resources(dev, mask);
  235. }
  236. /*
  237. * If we set up a device for bus mastering, we need to check the latency
  238. * timer as certain firmware forgets to set it properly, as seen
  239. * on SX164 and LX164 with SRM.
  240. */
  241. void
  242. pcibios_set_master(struct pci_dev *dev)
  243. {
  244. u8 lat;
  245. pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
  246. if (lat >= 16) return;
  247. printk("PCI: Setting latency timer of device %s to 64\n",
  248. pci_name(dev));
  249. pci_write_config_byte(dev, PCI_LATENCY_TIMER, 64);
  250. }
  251. void __init
  252. pcibios_claim_one_bus(struct pci_bus *b)
  253. {
  254. struct pci_dev *dev;
  255. struct pci_bus *child_bus;
  256. list_for_each_entry(dev, &b->devices, bus_list) {
  257. int i;
  258. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  259. struct resource *r = &dev->resource[i];
  260. if (r->parent || !r->start || !r->flags)
  261. continue;
  262. if (pci_has_flag(PCI_PROBE_ONLY) ||
  263. (r->flags & IORESOURCE_PCI_FIXED))
  264. pci_claim_resource(dev, i);
  265. }
  266. }
  267. list_for_each_entry(child_bus, &b->children, node)
  268. pcibios_claim_one_bus(child_bus);
  269. }
  270. static void __init
  271. pcibios_claim_console_setup(void)
  272. {
  273. struct pci_bus *b;
  274. list_for_each_entry(b, &pci_root_buses, node)
  275. pcibios_claim_one_bus(b);
  276. }
  277. void __init
  278. common_init_pci(void)
  279. {
  280. struct pci_controller *hose;
  281. struct list_head resources;
  282. struct pci_bus *bus;
  283. int next_busno;
  284. int need_domain_info = 0;
  285. u32 pci_mem_end;
  286. u32 sg_base;
  287. unsigned long end;
  288. /* Scan all of the recorded PCI controllers. */
  289. for (next_busno = 0, hose = hose_head; hose; hose = hose->next) {
  290. sg_base = hose->sg_pci ? hose->sg_pci->dma_base : ~0;
  291. /* Adjust hose mem_space limit to prevent PCI allocations
  292. in the iommu windows. */
  293. pci_mem_end = min((u32)__direct_map_base, sg_base) - 1;
  294. end = hose->mem_space->start + pci_mem_end;
  295. if (hose->mem_space->end > end)
  296. hose->mem_space->end = end;
  297. INIT_LIST_HEAD(&resources);
  298. pci_add_resource_offset(&resources, hose->io_space,
  299. hose->io_space->start);
  300. pci_add_resource_offset(&resources, hose->mem_space,
  301. hose->mem_space->start);
  302. bus = pci_scan_root_bus(NULL, next_busno, alpha_mv.pci_ops,
  303. hose, &resources);
  304. hose->bus = bus;
  305. hose->need_domain_info = need_domain_info;
  306. next_busno = bus->subordinate + 1;
  307. /* Don't allow 8-bit bus number overflow inside the hose -
  308. reserve some space for bridges. */
  309. if (next_busno > 224) {
  310. next_busno = 0;
  311. need_domain_info = 1;
  312. }
  313. }
  314. pcibios_claim_console_setup();
  315. pci_assign_unassigned_resources();
  316. pci_fixup_irqs(alpha_mv.pci_swizzle, alpha_mv.pci_map_irq);
  317. }
  318. struct pci_controller * __init
  319. alloc_pci_controller(void)
  320. {
  321. struct pci_controller *hose;
  322. hose = alloc_bootmem(sizeof(*hose));
  323. *hose_tail = hose;
  324. hose_tail = &hose->next;
  325. return hose;
  326. }
  327. struct resource * __init
  328. alloc_resource(void)
  329. {
  330. struct resource *res;
  331. res = alloc_bootmem(sizeof(*res));
  332. return res;
  333. }
  334. /* Provide information on locations of various I/O regions in physical
  335. memory. Do this on a per-card basis so that we choose the right hose. */
  336. asmlinkage long
  337. sys_pciconfig_iobase(long which, unsigned long bus, unsigned long dfn)
  338. {
  339. struct pci_controller *hose;
  340. struct pci_dev *dev;
  341. /* from hose or from bus.devfn */
  342. if (which & IOBASE_FROM_HOSE) {
  343. for(hose = hose_head; hose; hose = hose->next)
  344. if (hose->index == bus) break;
  345. if (!hose) return -ENODEV;
  346. } else {
  347. /* Special hook for ISA access. */
  348. if (bus == 0 && dfn == 0) {
  349. hose = pci_isa_hose;
  350. } else {
  351. dev = pci_get_bus_and_slot(bus, dfn);
  352. if (!dev)
  353. return -ENODEV;
  354. hose = dev->sysdata;
  355. pci_dev_put(dev);
  356. }
  357. }
  358. switch (which & ~IOBASE_FROM_HOSE) {
  359. case IOBASE_HOSE:
  360. return hose->index;
  361. case IOBASE_SPARSE_MEM:
  362. return hose->sparse_mem_base;
  363. case IOBASE_DENSE_MEM:
  364. return hose->dense_mem_base;
  365. case IOBASE_SPARSE_IO:
  366. return hose->sparse_io_base;
  367. case IOBASE_DENSE_IO:
  368. return hose->dense_io_base;
  369. case IOBASE_ROOT_BUS:
  370. return hose->bus->number;
  371. }
  372. return -EOPNOTSUPP;
  373. }
  374. /* Destroy an __iomem token. Not copied from lib/iomap.c. */
  375. void pci_iounmap(struct pci_dev *dev, void __iomem * addr)
  376. {
  377. if (__is_mmio(addr))
  378. iounmap(addr);
  379. }
  380. EXPORT_SYMBOL(pci_iounmap);
  381. /* FIXME: Some boxes have multiple ISA bridges! */
  382. struct pci_dev *isa_bridge;
  383. EXPORT_SYMBOL(isa_bridge);