pci.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  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 Cypress IDE controller doesn't support native mode, but it
  62. has programmable addresses of IDE command/control registers.
  63. This violates PCI specifications, confuses the IDE subsystem and
  64. causes resource conflicts between the primary HD_CMD register and
  65. the floppy controller. Ugh. Fix that. */
  66. if (dev->class >> 8 == PCI_CLASS_STORAGE_IDE) {
  67. dev->resource[0].flags = 0;
  68. dev->resource[1].flags = 0;
  69. }
  70. /* The Cypress bridge responds on the PCI bus in the address range
  71. 0xffff0000-0xffffffff (conventional x86 BIOS ROM). There is no
  72. way to turn this off. The bridge also supports several extended
  73. BIOS ranges (disabled after power-up), and some consoles do turn
  74. them on. So if we use a large direct-map window, or a large SG
  75. window, we must avoid the entire 0xfff00000-0xffffffff region. */
  76. else if (dev->class >> 8 == PCI_CLASS_BRIDGE_ISA) {
  77. if (__direct_map_base + __direct_map_size >= 0xfff00000UL)
  78. __direct_map_size = 0xfff00000UL - __direct_map_base;
  79. else {
  80. struct pci_controller *hose = dev->sysdata;
  81. struct pci_iommu_arena *pci = hose->sg_pci;
  82. if (pci && pci->dma_base + pci->size >= 0xfff00000UL)
  83. pci->size = 0xfff00000UL - pci->dma_base;
  84. }
  85. }
  86. }
  87. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_CONTAQ, PCI_DEVICE_ID_CONTAQ_82C693, quirk_cypress);
  88. /* Called for each device after PCI setup is done. */
  89. static void __init
  90. 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. void
  105. pcibios_align_resource(void *data, 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(0x1000UL, 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. res->start = 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. char * __init
  171. pcibios_setup(char *str)
  172. {
  173. return str;
  174. }
  175. #ifdef ALPHA_RESTORE_SRM_SETUP
  176. static struct pdev_srm_saved_conf *srm_saved_configs;
  177. void __init
  178. pdev_save_srm_config(struct pci_dev *dev)
  179. {
  180. struct pdev_srm_saved_conf *tmp;
  181. static int printed = 0;
  182. if (!alpha_using_srm || pci_probe_only)
  183. return;
  184. if (!printed) {
  185. printk(KERN_INFO "pci: enabling save/restore of SRM state\n");
  186. printed = 1;
  187. }
  188. tmp = kmalloc(sizeof(*tmp), GFP_KERNEL);
  189. if (!tmp) {
  190. printk(KERN_ERR "%s: kmalloc() failed!\n", __FUNCTION__);
  191. return;
  192. }
  193. tmp->next = srm_saved_configs;
  194. tmp->dev = dev;
  195. pci_save_state(dev);
  196. srm_saved_configs = tmp;
  197. }
  198. void
  199. pci_restore_srm_config(void)
  200. {
  201. struct pdev_srm_saved_conf *tmp;
  202. /* No need to restore if probed only. */
  203. if (pci_probe_only)
  204. return;
  205. /* Restore SRM config. */
  206. for (tmp = srm_saved_configs; tmp; tmp = tmp->next) {
  207. pci_restore_state(tmp->dev);
  208. }
  209. }
  210. #endif
  211. void __init
  212. pcibios_fixup_resource(struct resource *res, struct resource *root)
  213. {
  214. res->start += root->start;
  215. res->end += root->start;
  216. }
  217. void __init
  218. pcibios_fixup_device_resources(struct pci_dev *dev, struct pci_bus *bus)
  219. {
  220. /* Update device resources. */
  221. struct pci_controller *hose = (struct pci_controller *)bus->sysdata;
  222. int i;
  223. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  224. if (!dev->resource[i].start)
  225. continue;
  226. if (dev->resource[i].flags & IORESOURCE_IO)
  227. pcibios_fixup_resource(&dev->resource[i],
  228. hose->io_space);
  229. else if (dev->resource[i].flags & IORESOURCE_MEM)
  230. pcibios_fixup_resource(&dev->resource[i],
  231. hose->mem_space);
  232. }
  233. }
  234. void __init
  235. pcibios_fixup_bus(struct pci_bus *bus)
  236. {
  237. /* Propagate hose info into the subordinate devices. */
  238. struct pci_controller *hose = bus->sysdata;
  239. struct pci_dev *dev = bus->self;
  240. if (!dev) {
  241. /* Root bus. */
  242. u32 pci_mem_end;
  243. u32 sg_base = hose->sg_pci ? hose->sg_pci->dma_base : ~0;
  244. unsigned long end;
  245. bus->resource[0] = hose->io_space;
  246. bus->resource[1] = hose->mem_space;
  247. /* Adjust hose mem_space limit to prevent PCI allocations
  248. in the iommu windows. */
  249. pci_mem_end = min((u32)__direct_map_base, sg_base) - 1;
  250. end = hose->mem_space->start + pci_mem_end;
  251. if (hose->mem_space->end > end)
  252. hose->mem_space->end = end;
  253. } else if (pci_probe_only &&
  254. (dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
  255. pci_read_bridge_bases(bus);
  256. pcibios_fixup_device_resources(dev, bus);
  257. }
  258. list_for_each_entry(dev, &bus->devices, bus_list) {
  259. pdev_save_srm_config(dev);
  260. if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
  261. pcibios_fixup_device_resources(dev, bus);
  262. }
  263. }
  264. void __init
  265. pcibios_update_irq(struct pci_dev *dev, int irq)
  266. {
  267. pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq);
  268. }
  269. /* Most Alphas have straight-forward swizzling needs. */
  270. u8 __init
  271. common_swizzle(struct pci_dev *dev, u8 *pinp)
  272. {
  273. u8 pin = *pinp;
  274. while (dev->bus->parent) {
  275. pin = bridge_swizzle(pin, PCI_SLOT(dev->devfn));
  276. /* Move up the chain of bridges. */
  277. dev = dev->bus->self;
  278. }
  279. *pinp = pin;
  280. /* The slot is the slot of the last bridge. */
  281. return PCI_SLOT(dev->devfn);
  282. }
  283. void __devinit
  284. pcibios_resource_to_bus(struct pci_dev *dev, struct pci_bus_region *region,
  285. struct resource *res)
  286. {
  287. struct pci_controller *hose = (struct pci_controller *)dev->sysdata;
  288. unsigned long offset = 0;
  289. if (res->flags & IORESOURCE_IO)
  290. offset = hose->io_space->start;
  291. else if (res->flags & IORESOURCE_MEM)
  292. offset = hose->mem_space->start;
  293. region->start = res->start - offset;
  294. region->end = res->end - offset;
  295. }
  296. void pcibios_bus_to_resource(struct pci_dev *dev, struct resource *res,
  297. struct pci_bus_region *region)
  298. {
  299. struct pci_controller *hose = (struct pci_controller *)dev->sysdata;
  300. unsigned long offset = 0;
  301. if (res->flags & IORESOURCE_IO)
  302. offset = hose->io_space->start;
  303. else if (res->flags & IORESOURCE_MEM)
  304. offset = hose->mem_space->start;
  305. res->start = region->start + offset;
  306. res->end = region->end + offset;
  307. }
  308. #ifdef CONFIG_HOTPLUG
  309. EXPORT_SYMBOL(pcibios_resource_to_bus);
  310. EXPORT_SYMBOL(pcibios_bus_to_resource);
  311. #endif
  312. int
  313. pcibios_enable_device(struct pci_dev *dev, int mask)
  314. {
  315. u16 cmd, oldcmd;
  316. int i;
  317. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  318. oldcmd = cmd;
  319. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  320. struct resource *res = &dev->resource[i];
  321. if (res->flags & IORESOURCE_IO)
  322. cmd |= PCI_COMMAND_IO;
  323. else if (res->flags & IORESOURCE_MEM)
  324. cmd |= PCI_COMMAND_MEMORY;
  325. }
  326. if (cmd != oldcmd) {
  327. printk(KERN_DEBUG "PCI: Enabling device: (%s), cmd %x\n",
  328. pci_name(dev), cmd);
  329. /* Enable the appropriate bits in the PCI command register. */
  330. pci_write_config_word(dev, PCI_COMMAND, cmd);
  331. }
  332. return 0;
  333. }
  334. /*
  335. * If we set up a device for bus mastering, we need to check the latency
  336. * timer as certain firmware forgets to set it properly, as seen
  337. * on SX164 and LX164 with SRM.
  338. */
  339. void
  340. pcibios_set_master(struct pci_dev *dev)
  341. {
  342. u8 lat;
  343. pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
  344. if (lat >= 16) return;
  345. printk("PCI: Setting latency timer of device %s to 64\n",
  346. pci_name(dev));
  347. pci_write_config_byte(dev, PCI_LATENCY_TIMER, 64);
  348. }
  349. static void __init
  350. pcibios_claim_one_bus(struct pci_bus *b)
  351. {
  352. struct pci_dev *dev;
  353. struct pci_bus *child_bus;
  354. list_for_each_entry(dev, &b->devices, bus_list) {
  355. int i;
  356. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  357. struct resource *r = &dev->resource[i];
  358. if (r->parent || !r->start || !r->flags)
  359. continue;
  360. pci_claim_resource(dev, i);
  361. }
  362. }
  363. list_for_each_entry(child_bus, &b->children, node)
  364. pcibios_claim_one_bus(child_bus);
  365. }
  366. static void __init
  367. pcibios_claim_console_setup(void)
  368. {
  369. struct pci_bus *b;
  370. list_for_each_entry(b, &pci_root_buses, node)
  371. pcibios_claim_one_bus(b);
  372. }
  373. void __init
  374. common_init_pci(void)
  375. {
  376. struct pci_controller *hose;
  377. struct pci_bus *bus;
  378. int next_busno;
  379. int need_domain_info = 0;
  380. /* Scan all of the recorded PCI controllers. */
  381. for (next_busno = 0, hose = hose_head; hose; hose = hose->next) {
  382. bus = pci_scan_bus(next_busno, alpha_mv.pci_ops, hose);
  383. hose->bus = bus;
  384. hose->need_domain_info = need_domain_info;
  385. next_busno = bus->subordinate + 1;
  386. /* Don't allow 8-bit bus number overflow inside the hose -
  387. reserve some space for bridges. */
  388. if (next_busno > 224) {
  389. next_busno = 0;
  390. need_domain_info = 1;
  391. }
  392. }
  393. if (pci_probe_only)
  394. pcibios_claim_console_setup();
  395. pci_assign_unassigned_resources();
  396. pci_fixup_irqs(alpha_mv.pci_swizzle, alpha_mv.pci_map_irq);
  397. }
  398. struct pci_controller * __init
  399. alloc_pci_controller(void)
  400. {
  401. struct pci_controller *hose;
  402. hose = alloc_bootmem(sizeof(*hose));
  403. *hose_tail = hose;
  404. hose_tail = &hose->next;
  405. return hose;
  406. }
  407. struct resource * __init
  408. alloc_resource(void)
  409. {
  410. struct resource *res;
  411. res = alloc_bootmem(sizeof(*res));
  412. return res;
  413. }
  414. /* Provide information on locations of various I/O regions in physical
  415. memory. Do this on a per-card basis so that we choose the right hose. */
  416. asmlinkage long
  417. sys_pciconfig_iobase(long which, unsigned long bus, unsigned long dfn)
  418. {
  419. struct pci_controller *hose;
  420. struct pci_dev *dev;
  421. /* from hose or from bus.devfn */
  422. if (which & IOBASE_FROM_HOSE) {
  423. for(hose = hose_head; hose; hose = hose->next)
  424. if (hose->index == bus) break;
  425. if (!hose) return -ENODEV;
  426. } else {
  427. /* Special hook for ISA access. */
  428. if (bus == 0 && dfn == 0) {
  429. hose = pci_isa_hose;
  430. } else {
  431. dev = pci_get_bus_and_slot(bus, dfn);
  432. if (!dev)
  433. return -ENODEV;
  434. hose = dev->sysdata;
  435. pci_dev_put(dev);
  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);
  482. /* FIXME: Some boxes have multiple ISA bridges! */
  483. struct pci_dev *isa_bridge;
  484. EXPORT_SYMBOL(isa_bridge);