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