pci.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  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. /* Indicate whether we respect the PCI setup left by console. */
  38. /*
  39. * Make this long-lived so that we know when shutting down
  40. * whether we probed only or not.
  41. */
  42. int pci_probe_only;
  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 __init
  52. quirk_isa_bridge(struct pci_dev *dev)
  53. {
  54. dev->class = PCI_CLASS_BRIDGE_ISA << 8;
  55. }
  56. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82378, quirk_isa_bridge);
  57. static void __init
  58. quirk_cypress(struct pci_dev *dev)
  59. {
  60. /* The Notorious Cy82C693 chip. */
  61. /* The generic legacy mode IDE fixup in drivers/pci/probe.c
  62. doesn't work correctly with the Cypress IDE controller as
  63. it has non-standard register layout. Fix that. */
  64. if (dev->class >> 8 == PCI_CLASS_STORAGE_IDE) {
  65. dev->resource[2].start = dev->resource[3].start = 0;
  66. dev->resource[2].end = dev->resource[3].end = 0;
  67. dev->resource[2].flags = dev->resource[3].flags = 0;
  68. if (PCI_FUNC(dev->devfn) == 2) {
  69. dev->resource[0].start = 0x170;
  70. dev->resource[0].end = 0x177;
  71. dev->resource[1].start = 0x376;
  72. dev->resource[1].end = 0x376;
  73. }
  74. }
  75. /* The Cypress bridge responds on the PCI bus in the address range
  76. 0xffff0000-0xffffffff (conventional x86 BIOS ROM). There is no
  77. way to turn this off. The bridge also supports several extended
  78. BIOS ranges (disabled after power-up), and some consoles do turn
  79. them on. So if we use a large direct-map window, or a large SG
  80. window, we must avoid the entire 0xfff00000-0xffffffff region. */
  81. if (dev->class >> 8 == PCI_CLASS_BRIDGE_ISA) {
  82. if (__direct_map_base + __direct_map_size >= 0xfff00000UL)
  83. __direct_map_size = 0xfff00000UL - __direct_map_base;
  84. else {
  85. struct pci_controller *hose = dev->sysdata;
  86. struct pci_iommu_arena *pci = hose->sg_pci;
  87. if (pci && pci->dma_base + pci->size >= 0xfff00000UL)
  88. pci->size = 0xfff00000UL - pci->dma_base;
  89. }
  90. }
  91. }
  92. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_CONTAQ, PCI_DEVICE_ID_CONTAQ_82C693, quirk_cypress);
  93. /* Called for each device after PCI setup is done. */
  94. static void __init
  95. pcibios_fixup_final(struct pci_dev *dev)
  96. {
  97. unsigned int class = dev->class >> 8;
  98. if (class == PCI_CLASS_BRIDGE_ISA || class == PCI_CLASS_BRIDGE_EISA) {
  99. dev->dma_mask = MAX_ISA_DMA_ADDRESS - 1;
  100. isa_bridge = dev;
  101. }
  102. }
  103. DECLARE_PCI_FIXUP_FINAL(PCI_ANY_ID, PCI_ANY_ID, pcibios_fixup_final);
  104. /* Just declaring that the power-of-ten prefixes are actually the
  105. power-of-two ones doesn't make it true :) */
  106. #define KB 1024
  107. #define MB (1024*KB)
  108. #define GB (1024*MB)
  109. resource_size_t
  110. pcibios_align_resource(void *data, const struct resource *res,
  111. resource_size_t size, resource_size_t align)
  112. {
  113. struct pci_dev *dev = data;
  114. struct pci_controller *hose = dev->sysdata;
  115. unsigned long alignto;
  116. resource_size_t start = res->start;
  117. if (res->flags & IORESOURCE_IO) {
  118. /* Make sure we start at our min on all hoses */
  119. if (start - hose->io_space->start < PCIBIOS_MIN_IO)
  120. start = PCIBIOS_MIN_IO + hose->io_space->start;
  121. /*
  122. * Put everything into 0x00-0xff region modulo 0x400
  123. */
  124. if (start & 0x300)
  125. start = (start + 0x3ff) & ~0x3ff;
  126. }
  127. else if (res->flags & IORESOURCE_MEM) {
  128. /* Make sure we start at our min on all hoses */
  129. if (start - hose->mem_space->start < PCIBIOS_MIN_MEM)
  130. start = PCIBIOS_MIN_MEM + hose->mem_space->start;
  131. /*
  132. * The following holds at least for the Low Cost
  133. * Alpha implementation of the PCI interface:
  134. *
  135. * In sparse memory address space, the first
  136. * octant (16MB) of every 128MB segment is
  137. * aliased to the very first 16 MB of the
  138. * address space (i.e., it aliases the ISA
  139. * memory address space). Thus, we try to
  140. * avoid allocating PCI devices in that range.
  141. * Can be allocated in 2nd-7th octant only.
  142. * Devices that need more than 112MB of
  143. * address space must be accessed through
  144. * dense memory space only!
  145. */
  146. /* Align to multiple of size of minimum base. */
  147. alignto = max_t(resource_size_t, 0x1000, align);
  148. start = ALIGN(start, alignto);
  149. if (hose->sparse_mem_base && size <= 7 * 16*MB) {
  150. if (((start / (16*MB)) & 0x7) == 0) {
  151. start &= ~(128*MB - 1);
  152. start += 16*MB;
  153. start = ALIGN(start, alignto);
  154. }
  155. if (start/(128*MB) != (start + size - 1)/(128*MB)) {
  156. start &= ~(128*MB - 1);
  157. start += (128 + 16)*MB;
  158. start = ALIGN(start, alignto);
  159. }
  160. }
  161. }
  162. return start;
  163. }
  164. #undef KB
  165. #undef MB
  166. #undef GB
  167. static int __init
  168. pcibios_init(void)
  169. {
  170. if (alpha_mv.init_pci)
  171. alpha_mv.init_pci();
  172. return 0;
  173. }
  174. subsys_initcall(pcibios_init);
  175. char * __devinit
  176. pcibios_setup(char *str)
  177. {
  178. return str;
  179. }
  180. #ifdef ALPHA_RESTORE_SRM_SETUP
  181. static struct pdev_srm_saved_conf *srm_saved_configs;
  182. void __devinit
  183. pdev_save_srm_config(struct pci_dev *dev)
  184. {
  185. struct pdev_srm_saved_conf *tmp;
  186. static int printed = 0;
  187. if (!alpha_using_srm || pci_probe_only)
  188. return;
  189. if (!printed) {
  190. printk(KERN_INFO "pci: enabling save/restore of SRM state\n");
  191. printed = 1;
  192. }
  193. tmp = kmalloc(sizeof(*tmp), GFP_KERNEL);
  194. if (!tmp) {
  195. printk(KERN_ERR "%s: kmalloc() failed!\n", __func__);
  196. return;
  197. }
  198. tmp->next = srm_saved_configs;
  199. tmp->dev = dev;
  200. pci_save_state(dev);
  201. srm_saved_configs = tmp;
  202. }
  203. void
  204. pci_restore_srm_config(void)
  205. {
  206. struct pdev_srm_saved_conf *tmp;
  207. /* No need to restore if probed only. */
  208. if (pci_probe_only)
  209. return;
  210. /* Restore SRM config. */
  211. for (tmp = srm_saved_configs; tmp; tmp = tmp->next) {
  212. pci_restore_state(tmp->dev);
  213. }
  214. }
  215. #endif
  216. void __devinit
  217. pcibios_fixup_resource(struct resource *res, struct resource *root)
  218. {
  219. res->start += root->start;
  220. res->end += root->start;
  221. }
  222. void __devinit
  223. pcibios_fixup_device_resources(struct pci_dev *dev, struct pci_bus *bus)
  224. {
  225. /* Update device resources. */
  226. struct pci_controller *hose = (struct pci_controller *)bus->sysdata;
  227. int i;
  228. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  229. if (!dev->resource[i].start)
  230. continue;
  231. if (dev->resource[i].flags & IORESOURCE_IO)
  232. pcibios_fixup_resource(&dev->resource[i],
  233. hose->io_space);
  234. else if (dev->resource[i].flags & IORESOURCE_MEM)
  235. pcibios_fixup_resource(&dev->resource[i],
  236. hose->mem_space);
  237. }
  238. }
  239. void __devinit
  240. pcibios_fixup_bus(struct pci_bus *bus)
  241. {
  242. /* Propagate hose info into the subordinate devices. */
  243. struct pci_controller *hose = bus->sysdata;
  244. struct pci_dev *dev = bus->self;
  245. if (!dev) {
  246. /* Root bus. */
  247. u32 pci_mem_end;
  248. u32 sg_base = hose->sg_pci ? hose->sg_pci->dma_base : ~0;
  249. unsigned long end;
  250. bus->resource[0] = hose->io_space;
  251. bus->resource[1] = hose->mem_space;
  252. /* Adjust hose mem_space limit to prevent PCI allocations
  253. in the iommu windows. */
  254. pci_mem_end = min((u32)__direct_map_base, sg_base) - 1;
  255. end = hose->mem_space->start + pci_mem_end;
  256. if (hose->mem_space->end > end)
  257. hose->mem_space->end = end;
  258. } else if (pci_probe_only &&
  259. (dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
  260. pci_read_bridge_bases(bus);
  261. pcibios_fixup_device_resources(dev, bus);
  262. }
  263. list_for_each_entry(dev, &bus->devices, bus_list) {
  264. pdev_save_srm_config(dev);
  265. if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
  266. pcibios_fixup_device_resources(dev, bus);
  267. }
  268. }
  269. void __init
  270. pcibios_update_irq(struct pci_dev *dev, int irq)
  271. {
  272. pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq);
  273. }
  274. void
  275. pcibios_resource_to_bus(struct pci_dev *dev, struct pci_bus_region *region,
  276. struct resource *res)
  277. {
  278. struct pci_controller *hose = (struct pci_controller *)dev->sysdata;
  279. unsigned long offset = 0;
  280. if (res->flags & IORESOURCE_IO)
  281. offset = hose->io_space->start;
  282. else if (res->flags & IORESOURCE_MEM)
  283. offset = hose->mem_space->start;
  284. region->start = res->start - offset;
  285. region->end = res->end - offset;
  286. }
  287. void pcibios_bus_to_resource(struct pci_dev *dev, struct resource *res,
  288. struct pci_bus_region *region)
  289. {
  290. struct pci_controller *hose = (struct pci_controller *)dev->sysdata;
  291. unsigned long offset = 0;
  292. if (res->flags & IORESOURCE_IO)
  293. offset = hose->io_space->start;
  294. else if (res->flags & IORESOURCE_MEM)
  295. offset = hose->mem_space->start;
  296. res->start = region->start + offset;
  297. res->end = region->end + offset;
  298. }
  299. #ifdef CONFIG_HOTPLUG
  300. EXPORT_SYMBOL(pcibios_resource_to_bus);
  301. EXPORT_SYMBOL(pcibios_bus_to_resource);
  302. #endif
  303. int
  304. pcibios_enable_device(struct pci_dev *dev, int mask)
  305. {
  306. return pci_enable_resources(dev, mask);
  307. }
  308. /*
  309. * If we set up a device for bus mastering, we need to check the latency
  310. * timer as certain firmware forgets to set it properly, as seen
  311. * on SX164 and LX164 with SRM.
  312. */
  313. void
  314. pcibios_set_master(struct pci_dev *dev)
  315. {
  316. u8 lat;
  317. pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
  318. if (lat >= 16) return;
  319. printk("PCI: Setting latency timer of device %s to 64\n",
  320. pci_name(dev));
  321. pci_write_config_byte(dev, PCI_LATENCY_TIMER, 64);
  322. }
  323. void __init
  324. pcibios_claim_one_bus(struct pci_bus *b)
  325. {
  326. struct pci_dev *dev;
  327. struct pci_bus *child_bus;
  328. list_for_each_entry(dev, &b->devices, bus_list) {
  329. int i;
  330. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  331. struct resource *r = &dev->resource[i];
  332. if (r->parent || !r->start || !r->flags)
  333. continue;
  334. if (pci_probe_only || (r->flags & IORESOURCE_PCI_FIXED))
  335. pci_claim_resource(dev, i);
  336. }
  337. }
  338. list_for_each_entry(child_bus, &b->children, node)
  339. pcibios_claim_one_bus(child_bus);
  340. }
  341. static void __init
  342. pcibios_claim_console_setup(void)
  343. {
  344. struct pci_bus *b;
  345. list_for_each_entry(b, &pci_root_buses, node)
  346. pcibios_claim_one_bus(b);
  347. }
  348. void __init
  349. common_init_pci(void)
  350. {
  351. struct pci_controller *hose;
  352. struct pci_bus *bus;
  353. int next_busno;
  354. int need_domain_info = 0;
  355. /* Scan all of the recorded PCI controllers. */
  356. for (next_busno = 0, hose = hose_head; hose; hose = hose->next) {
  357. bus = pci_scan_bus(next_busno, alpha_mv.pci_ops, hose);
  358. hose->bus = bus;
  359. hose->need_domain_info = need_domain_info;
  360. next_busno = bus->subordinate + 1;
  361. /* Don't allow 8-bit bus number overflow inside the hose -
  362. reserve some space for bridges. */
  363. if (next_busno > 224) {
  364. next_busno = 0;
  365. need_domain_info = 1;
  366. }
  367. }
  368. pcibios_claim_console_setup();
  369. pci_assign_unassigned_resources();
  370. pci_fixup_irqs(alpha_mv.pci_swizzle, alpha_mv.pci_map_irq);
  371. }
  372. struct pci_controller * __init
  373. alloc_pci_controller(void)
  374. {
  375. struct pci_controller *hose;
  376. hose = alloc_bootmem(sizeof(*hose));
  377. *hose_tail = hose;
  378. hose_tail = &hose->next;
  379. return hose;
  380. }
  381. struct resource * __init
  382. alloc_resource(void)
  383. {
  384. struct resource *res;
  385. res = alloc_bootmem(sizeof(*res));
  386. return res;
  387. }
  388. /* Provide information on locations of various I/O regions in physical
  389. memory. Do this on a per-card basis so that we choose the right hose. */
  390. asmlinkage long
  391. sys_pciconfig_iobase(long which, unsigned long bus, unsigned long dfn)
  392. {
  393. struct pci_controller *hose;
  394. struct pci_dev *dev;
  395. /* from hose or from bus.devfn */
  396. if (which & IOBASE_FROM_HOSE) {
  397. for(hose = hose_head; hose; hose = hose->next)
  398. if (hose->index == bus) break;
  399. if (!hose) return -ENODEV;
  400. } else {
  401. /* Special hook for ISA access. */
  402. if (bus == 0 && dfn == 0) {
  403. hose = pci_isa_hose;
  404. } else {
  405. dev = pci_get_bus_and_slot(bus, dfn);
  406. if (!dev)
  407. return -ENODEV;
  408. hose = dev->sysdata;
  409. pci_dev_put(dev);
  410. }
  411. }
  412. switch (which & ~IOBASE_FROM_HOSE) {
  413. case IOBASE_HOSE:
  414. return hose->index;
  415. case IOBASE_SPARSE_MEM:
  416. return hose->sparse_mem_base;
  417. case IOBASE_DENSE_MEM:
  418. return hose->dense_mem_base;
  419. case IOBASE_SPARSE_IO:
  420. return hose->sparse_io_base;
  421. case IOBASE_DENSE_IO:
  422. return hose->dense_io_base;
  423. case IOBASE_ROOT_BUS:
  424. return hose->bus->number;
  425. }
  426. return -EOPNOTSUPP;
  427. }
  428. /* Create an __iomem token from a PCI BAR. Copied from lib/iomap.c with
  429. no changes, since we don't want the other things in that object file. */
  430. void __iomem *pci_iomap(struct pci_dev *dev, int bar, unsigned long maxlen)
  431. {
  432. resource_size_t start = pci_resource_start(dev, bar);
  433. resource_size_t len = pci_resource_len(dev, bar);
  434. unsigned long flags = pci_resource_flags(dev, bar);
  435. if (!len || !start)
  436. return NULL;
  437. if (maxlen && len > maxlen)
  438. len = maxlen;
  439. if (flags & IORESOURCE_IO)
  440. return ioport_map(start, len);
  441. if (flags & IORESOURCE_MEM) {
  442. /* Not checking IORESOURCE_CACHEABLE because alpha does
  443. not distinguish between ioremap and ioremap_nocache. */
  444. return ioremap(start, len);
  445. }
  446. return NULL;
  447. }
  448. /* Destroy that token. Not copied from lib/iomap.c. */
  449. void pci_iounmap(struct pci_dev *dev, void __iomem * addr)
  450. {
  451. if (__is_mmio(addr))
  452. iounmap(addr);
  453. }
  454. EXPORT_SYMBOL(pci_iomap);
  455. EXPORT_SYMBOL(pci_iounmap);
  456. /* FIXME: Some boxes have multiple ISA bridges! */
  457. struct pci_dev *isa_bridge;
  458. EXPORT_SYMBOL(isa_bridge);