pci.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  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/config.h>
  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/bootmem.h>
  20. #include <linux/module.h>
  21. #include <linux/cache.h>
  22. #include <linux/slab.h>
  23. #include <asm/machvec.h>
  24. #include "proto.h"
  25. #include "pci_impl.h"
  26. /*
  27. * Some string constants used by the various core logics.
  28. */
  29. const char *const pci_io_names[] = {
  30. "PCI IO bus 0", "PCI IO bus 1", "PCI IO bus 2", "PCI IO bus 3",
  31. "PCI IO bus 4", "PCI IO bus 5", "PCI IO bus 6", "PCI IO bus 7"
  32. };
  33. const char *const pci_mem_names[] = {
  34. "PCI mem bus 0", "PCI mem bus 1", "PCI mem bus 2", "PCI mem bus 3",
  35. "PCI mem bus 4", "PCI mem bus 5", "PCI mem bus 6", "PCI mem bus 7"
  36. };
  37. const char pci_hae0_name[] = "HAE0";
  38. /* Indicate whether we respect the PCI setup left by console. */
  39. /*
  40. * Make this long-lived so that we know when shutting down
  41. * whether we probed only or not.
  42. */
  43. int pci_probe_only;
  44. /*
  45. * The PCI controller list.
  46. */
  47. struct pci_controller *hose_head, **hose_tail = &hose_head;
  48. struct pci_controller *pci_isa_hose;
  49. /*
  50. * Quirks.
  51. */
  52. static void __init
  53. quirk_isa_bridge(struct pci_dev *dev)
  54. {
  55. dev->class = PCI_CLASS_BRIDGE_ISA << 8;
  56. }
  57. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82378, quirk_isa_bridge);
  58. static void __init
  59. quirk_cypress(struct pci_dev *dev)
  60. {
  61. /* The Notorious Cy82C693 chip. */
  62. /* The Cypress IDE controller doesn't support native mode, but it
  63. has programmable addresses of IDE command/control registers.
  64. This violates PCI specifications, confuses the IDE subsystem and
  65. causes resource conflicts between the primary HD_CMD register and
  66. the floppy controller. Ugh. Fix that. */
  67. if (dev->class >> 8 == PCI_CLASS_STORAGE_IDE) {
  68. dev->resource[0].flags = 0;
  69. dev->resource[1].flags = 0;
  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. else 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 __init
  91. pcibios_fixup_final(struct pci_dev *dev)
  92. {
  93. unsigned int class = dev->class >> 8;
  94. if (class == PCI_CLASS_BRIDGE_ISA || class == PCI_CLASS_BRIDGE_EISA) {
  95. dev->dma_mask = MAX_ISA_DMA_ADDRESS - 1;
  96. isa_bridge = dev;
  97. }
  98. }
  99. DECLARE_PCI_FIXUP_FINAL(PCI_ANY_ID, PCI_ANY_ID, pcibios_fixup_final);
  100. /* Just declaring that the power-of-ten prefixes are actually the
  101. power-of-two ones doesn't make it true :) */
  102. #define KB 1024
  103. #define MB (1024*KB)
  104. #define GB (1024*MB)
  105. void
  106. pcibios_align_resource(void *data, struct resource *res,
  107. unsigned long size, unsigned long align)
  108. {
  109. struct pci_dev *dev = data;
  110. struct pci_controller *hose = dev->sysdata;
  111. unsigned long alignto;
  112. unsigned long start = res->start;
  113. if (res->flags & IORESOURCE_IO) {
  114. /* Make sure we start at our min on all hoses */
  115. if (start - hose->io_space->start < PCIBIOS_MIN_IO)
  116. start = PCIBIOS_MIN_IO + hose->io_space->start;
  117. /*
  118. * Put everything into 0x00-0xff region modulo 0x400
  119. */
  120. if (start & 0x300)
  121. start = (start + 0x3ff) & ~0x3ff;
  122. }
  123. else if (res->flags & IORESOURCE_MEM) {
  124. /* Make sure we start at our min on all hoses */
  125. if (start - hose->mem_space->start < PCIBIOS_MIN_MEM)
  126. start = PCIBIOS_MIN_MEM + hose->mem_space->start;
  127. /*
  128. * The following holds at least for the Low Cost
  129. * Alpha implementation of the PCI interface:
  130. *
  131. * In sparse memory address space, the first
  132. * octant (16MB) of every 128MB segment is
  133. * aliased to the very first 16 MB of the
  134. * address space (i.e., it aliases the ISA
  135. * memory address space). Thus, we try to
  136. * avoid allocating PCI devices in that range.
  137. * Can be allocated in 2nd-7th octant only.
  138. * Devices that need more than 112MB of
  139. * address space must be accessed through
  140. * dense memory space only!
  141. */
  142. /* Align to multiple of size of minimum base. */
  143. alignto = max(0x1000UL, align);
  144. start = ALIGN(start, alignto);
  145. if (hose->sparse_mem_base && size <= 7 * 16*MB) {
  146. if (((start / (16*MB)) & 0x7) == 0) {
  147. start &= ~(128*MB - 1);
  148. start += 16*MB;
  149. start = ALIGN(start, alignto);
  150. }
  151. if (start/(128*MB) != (start + size - 1)/(128*MB)) {
  152. start &= ~(128*MB - 1);
  153. start += (128 + 16)*MB;
  154. start = ALIGN(start, alignto);
  155. }
  156. }
  157. }
  158. res->start = start;
  159. }
  160. #undef KB
  161. #undef MB
  162. #undef GB
  163. static int __init
  164. pcibios_init(void)
  165. {
  166. if (alpha_mv.init_pci)
  167. alpha_mv.init_pci();
  168. return 0;
  169. }
  170. subsys_initcall(pcibios_init);
  171. char * __init
  172. pcibios_setup(char *str)
  173. {
  174. return str;
  175. }
  176. #ifdef ALPHA_RESTORE_SRM_SETUP
  177. static struct pdev_srm_saved_conf *srm_saved_configs;
  178. void __init
  179. pdev_save_srm_config(struct pci_dev *dev)
  180. {
  181. struct pdev_srm_saved_conf *tmp;
  182. static int printed = 0;
  183. if (!alpha_using_srm || pci_probe_only)
  184. return;
  185. if (!printed) {
  186. printk(KERN_INFO "pci: enabling save/restore of SRM state\n");
  187. printed = 1;
  188. }
  189. tmp = kmalloc(sizeof(*tmp), GFP_KERNEL);
  190. if (!tmp) {
  191. printk(KERN_ERR "%s: kmalloc() failed!\n", __FUNCTION__);
  192. return;
  193. }
  194. tmp->next = srm_saved_configs;
  195. tmp->dev = dev;
  196. pci_save_state(dev);
  197. srm_saved_configs = tmp;
  198. }
  199. void
  200. pci_restore_srm_config(void)
  201. {
  202. struct pdev_srm_saved_conf *tmp;
  203. /* No need to restore if probed only. */
  204. if (pci_probe_only)
  205. return;
  206. /* Restore SRM config. */
  207. for (tmp = srm_saved_configs; tmp; tmp = tmp->next) {
  208. pci_restore_state(tmp->dev);
  209. }
  210. }
  211. #endif
  212. void __init
  213. pcibios_fixup_resource(struct resource *res, struct resource *root)
  214. {
  215. res->start += root->start;
  216. res->end += root->start;
  217. }
  218. void __init
  219. pcibios_fixup_device_resources(struct pci_dev *dev, struct pci_bus *bus)
  220. {
  221. /* Update device resources. */
  222. struct pci_controller *hose = (struct pci_controller *)bus->sysdata;
  223. int i;
  224. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  225. if (!dev->resource[i].start)
  226. continue;
  227. if (dev->resource[i].flags & IORESOURCE_IO)
  228. pcibios_fixup_resource(&dev->resource[i],
  229. hose->io_space);
  230. else if (dev->resource[i].flags & IORESOURCE_MEM)
  231. pcibios_fixup_resource(&dev->resource[i],
  232. hose->mem_space);
  233. }
  234. }
  235. void __init
  236. pcibios_fixup_bus(struct pci_bus *bus)
  237. {
  238. /* Propagate hose info into the subordinate devices. */
  239. struct pci_controller *hose = bus->sysdata;
  240. struct pci_dev *dev = bus->self;
  241. if (!dev) {
  242. /* Root bus. */
  243. u32 pci_mem_end;
  244. u32 sg_base = hose->sg_pci ? hose->sg_pci->dma_base : ~0;
  245. unsigned long end;
  246. bus->resource[0] = hose->io_space;
  247. bus->resource[1] = hose->mem_space;
  248. /* Adjust hose mem_space limit to prevent PCI allocations
  249. in the iommu windows. */
  250. pci_mem_end = min((u32)__direct_map_base, sg_base) - 1;
  251. end = hose->mem_space->start + pci_mem_end;
  252. if (hose->mem_space->end > end)
  253. hose->mem_space->end = end;
  254. } else if (pci_probe_only &&
  255. (dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
  256. pci_read_bridge_bases(bus);
  257. pcibios_fixup_device_resources(dev, bus);
  258. }
  259. list_for_each_entry(dev, &bus->devices, bus_list) {
  260. pdev_save_srm_config(dev);
  261. if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
  262. pcibios_fixup_device_resources(dev, bus);
  263. }
  264. }
  265. void __init
  266. pcibios_update_irq(struct pci_dev *dev, int irq)
  267. {
  268. pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq);
  269. }
  270. /* Most Alphas have straight-forward swizzling needs. */
  271. u8 __init
  272. common_swizzle(struct pci_dev *dev, u8 *pinp)
  273. {
  274. u8 pin = *pinp;
  275. while (dev->bus->parent) {
  276. pin = bridge_swizzle(pin, PCI_SLOT(dev->devfn));
  277. /* Move up the chain of bridges. */
  278. dev = dev->bus->self;
  279. }
  280. *pinp = pin;
  281. /* The slot is the slot of the last bridge. */
  282. return PCI_SLOT(dev->devfn);
  283. }
  284. void __devinit
  285. pcibios_resource_to_bus(struct pci_dev *dev, struct pci_bus_region *region,
  286. struct resource *res)
  287. {
  288. struct pci_controller *hose = (struct pci_controller *)dev->sysdata;
  289. unsigned long offset = 0;
  290. if (res->flags & IORESOURCE_IO)
  291. offset = hose->io_space->start;
  292. else if (res->flags & IORESOURCE_MEM)
  293. offset = hose->mem_space->start;
  294. region->start = res->start - offset;
  295. region->end = res->end - offset;
  296. }
  297. void pcibios_bus_to_resource(struct pci_dev *dev, struct resource *res,
  298. struct pci_bus_region *region)
  299. {
  300. struct pci_controller *hose = (struct pci_controller *)dev->sysdata;
  301. unsigned long offset = 0;
  302. if (res->flags & IORESOURCE_IO)
  303. offset = hose->io_space->start;
  304. else if (res->flags & IORESOURCE_MEM)
  305. offset = hose->mem_space->start;
  306. res->start = region->start + offset;
  307. res->end = region->end + offset;
  308. }
  309. #ifdef CONFIG_HOTPLUG
  310. EXPORT_SYMBOL(pcibios_resource_to_bus);
  311. EXPORT_SYMBOL(pcibios_bus_to_resource);
  312. #endif
  313. int
  314. pcibios_enable_device(struct pci_dev *dev, int mask)
  315. {
  316. u16 cmd, oldcmd;
  317. int i;
  318. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  319. oldcmd = cmd;
  320. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  321. struct resource *res = &dev->resource[i];
  322. if (res->flags & IORESOURCE_IO)
  323. cmd |= PCI_COMMAND_IO;
  324. else if (res->flags & IORESOURCE_MEM)
  325. cmd |= PCI_COMMAND_MEMORY;
  326. }
  327. if (cmd != oldcmd) {
  328. printk(KERN_DEBUG "PCI: Enabling device: (%s), cmd %x\n",
  329. pci_name(dev), cmd);
  330. /* Enable the appropriate bits in the PCI command register. */
  331. pci_write_config_word(dev, PCI_COMMAND, cmd);
  332. }
  333. return 0;
  334. }
  335. /*
  336. * If we set up a device for bus mastering, we need to check the latency
  337. * timer as certain firmware forgets to set it properly, as seen
  338. * on SX164 and LX164 with SRM.
  339. */
  340. void
  341. pcibios_set_master(struct pci_dev *dev)
  342. {
  343. u8 lat;
  344. pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
  345. if (lat >= 16) return;
  346. printk("PCI: Setting latency timer of device %s to 64\n",
  347. pci_name(dev));
  348. pci_write_config_byte(dev, PCI_LATENCY_TIMER, 64);
  349. }
  350. static void __init
  351. pcibios_claim_one_bus(struct pci_bus *b)
  352. {
  353. struct pci_dev *dev;
  354. struct pci_bus *child_bus;
  355. list_for_each_entry(dev, &b->devices, bus_list) {
  356. int i;
  357. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  358. struct resource *r = &dev->resource[i];
  359. if (r->parent || !r->start || !r->flags)
  360. continue;
  361. pci_claim_resource(dev, i);
  362. }
  363. }
  364. list_for_each_entry(child_bus, &b->children, node)
  365. pcibios_claim_one_bus(child_bus);
  366. }
  367. static void __init
  368. pcibios_claim_console_setup(void)
  369. {
  370. struct pci_bus *b;
  371. list_for_each_entry(b, &pci_root_buses, node)
  372. pcibios_claim_one_bus(b);
  373. }
  374. void __init
  375. common_init_pci(void)
  376. {
  377. struct pci_controller *hose;
  378. struct pci_bus *bus;
  379. int next_busno;
  380. int need_domain_info = 0;
  381. /* Scan all of the recorded PCI controllers. */
  382. for (next_busno = 0, hose = hose_head; hose; hose = hose->next) {
  383. bus = pci_scan_bus(next_busno, alpha_mv.pci_ops, hose);
  384. hose->bus = bus;
  385. hose->need_domain_info = need_domain_info;
  386. next_busno = bus->subordinate + 1;
  387. /* Don't allow 8-bit bus number overflow inside the hose -
  388. reserve some space for bridges. */
  389. if (next_busno > 224) {
  390. next_busno = 0;
  391. need_domain_info = 1;
  392. }
  393. }
  394. if (pci_probe_only)
  395. pcibios_claim_console_setup();
  396. pci_assign_unassigned_resources();
  397. pci_fixup_irqs(alpha_mv.pci_swizzle, alpha_mv.pci_map_irq);
  398. }
  399. struct pci_controller * __init
  400. alloc_pci_controller(void)
  401. {
  402. struct pci_controller *hose;
  403. hose = alloc_bootmem(sizeof(*hose));
  404. *hose_tail = hose;
  405. hose_tail = &hose->next;
  406. return hose;
  407. }
  408. struct resource * __init
  409. alloc_resource(void)
  410. {
  411. struct resource *res;
  412. res = alloc_bootmem(sizeof(*res));
  413. return res;
  414. }
  415. /* Provide information on locations of various I/O regions in physical
  416. memory. Do this on a per-card basis so that we choose the right hose. */
  417. asmlinkage long
  418. sys_pciconfig_iobase(long which, unsigned long bus, unsigned long dfn)
  419. {
  420. struct pci_controller *hose;
  421. struct pci_dev *dev;
  422. /* from hose or from bus.devfn */
  423. if (which & IOBASE_FROM_HOSE) {
  424. for(hose = hose_head; hose; hose = hose->next)
  425. if (hose->index == bus) break;
  426. if (!hose) return -ENODEV;
  427. } else {
  428. /* Special hook for ISA access. */
  429. if (bus == 0 && dfn == 0) {
  430. hose = pci_isa_hose;
  431. } else {
  432. dev = pci_find_slot(bus, dfn);
  433. if (!dev)
  434. return -ENODEV;
  435. hose = dev->sysdata;
  436. }
  437. }
  438. switch (which & ~IOBASE_FROM_HOSE) {
  439. case IOBASE_HOSE:
  440. return hose->index;
  441. case IOBASE_SPARSE_MEM:
  442. return hose->sparse_mem_base;
  443. case IOBASE_DENSE_MEM:
  444. return hose->dense_mem_base;
  445. case IOBASE_SPARSE_IO:
  446. return hose->sparse_io_base;
  447. case IOBASE_DENSE_IO:
  448. return hose->dense_io_base;
  449. case IOBASE_ROOT_BUS:
  450. return hose->bus->number;
  451. }
  452. return -EOPNOTSUPP;
  453. }
  454. /* Create an __iomem token from a PCI BAR. Copied from lib/iomap.c with
  455. no changes, since we don't want the other things in that object file. */
  456. void __iomem *pci_iomap(struct pci_dev *dev, int bar, unsigned long maxlen)
  457. {
  458. unsigned long start = pci_resource_start(dev, bar);
  459. unsigned long len = pci_resource_len(dev, bar);
  460. unsigned long flags = pci_resource_flags(dev, bar);
  461. if (!len || !start)
  462. return NULL;
  463. if (maxlen && len > maxlen)
  464. len = maxlen;
  465. if (flags & IORESOURCE_IO)
  466. return ioport_map(start, len);
  467. if (flags & IORESOURCE_MEM) {
  468. /* Not checking IORESOURCE_CACHEABLE because alpha does
  469. not distinguish between ioremap and ioremap_nocache. */
  470. return ioremap(start, len);
  471. }
  472. return NULL;
  473. }
  474. /* Destroy that token. Not copied from lib/iomap.c. */
  475. void pci_iounmap(struct pci_dev *dev, void __iomem * addr)
  476. {
  477. if (__is_mmio(addr))
  478. iounmap(addr);
  479. }
  480. EXPORT_SYMBOL(pci_iomap);
  481. EXPORT_SYMBOL(pci_iounmap);