pci.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /*
  2. * arch/xtensa/kernel/pci.c
  3. *
  4. * PCI bios-type initialisation for PCI machines
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation; either version 2 of the License, or (at your
  9. * option) any later version.
  10. *
  11. * Copyright (C) 2001-2005 Tensilica Inc.
  12. *
  13. * Based largely on work from Cort (ppc/kernel/pci.c)
  14. * IO functions copied from sparc.
  15. *
  16. * Chris Zankel <chris@zankel.net>
  17. *
  18. */
  19. #include <linux/kernel.h>
  20. #include <linux/pci.h>
  21. #include <linux/delay.h>
  22. #include <linux/string.h>
  23. #include <linux/init.h>
  24. #include <linux/sched.h>
  25. #include <linux/errno.h>
  26. #include <linux/bootmem.h>
  27. #include <asm/pci-bridge.h>
  28. #include <asm/platform.h>
  29. #undef DEBUG
  30. #ifdef DEBUG
  31. #define DBG(x...) printk(x)
  32. #else
  33. #define DBG(x...)
  34. #endif
  35. /* PCI Controller */
  36. /*
  37. * pcibios_alloc_controller
  38. * pcibios_enable_device
  39. * pcibios_fixups
  40. * pcibios_align_resource
  41. * pcibios_fixup_bus
  42. * pcibios_setup
  43. * pci_bus_add_device
  44. * pci_mmap_page_range
  45. */
  46. struct pci_controller* pci_ctrl_head;
  47. struct pci_controller** pci_ctrl_tail = &pci_ctrl_head;
  48. static int pci_bus_count;
  49. /*
  50. * We need to avoid collisions with `mirrored' VGA ports
  51. * and other strange ISA hardware, so we always want the
  52. * addresses to be allocated in the 0x000-0x0ff region
  53. * modulo 0x400.
  54. *
  55. * Why? Because some silly external IO cards only decode
  56. * the low 10 bits of the IO address. The 0x00-0xff region
  57. * is reserved for motherboard devices that decode all 16
  58. * bits, so it's ok to allocate at, say, 0x2800-0x28ff,
  59. * but we want to try to avoid allocating at 0x2900-0x2bff
  60. * which might have be mirrored at 0x0100-0x03ff..
  61. */
  62. void
  63. pcibios_align_resource(void *data, struct resource *res, resource_size_t size,
  64. resource_size_t align)
  65. {
  66. struct pci_dev *dev = data;
  67. if (res->flags & IORESOURCE_IO) {
  68. resource_size_t start = res->start;
  69. if (size > 0x100) {
  70. printk(KERN_ERR "PCI: I/O Region %s/%d too large"
  71. " (%ld bytes)\n", pci_name(dev),
  72. dev->resource - res, size);
  73. }
  74. if (start & 0x300) {
  75. start = (start + 0x3ff) & ~0x3ff;
  76. res->start = start;
  77. }
  78. }
  79. }
  80. int
  81. pcibios_enable_resources(struct pci_dev *dev, int mask)
  82. {
  83. u16 cmd, old_cmd;
  84. int idx;
  85. struct resource *r;
  86. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  87. old_cmd = cmd;
  88. for(idx=0; idx<6; idx++) {
  89. r = &dev->resource[idx];
  90. if (!r->start && r->end) {
  91. printk (KERN_ERR "PCI: Device %s not available because "
  92. "of resource collisions\n", pci_name(dev));
  93. return -EINVAL;
  94. }
  95. if (r->flags & IORESOURCE_IO)
  96. cmd |= PCI_COMMAND_IO;
  97. if (r->flags & IORESOURCE_MEM)
  98. cmd |= PCI_COMMAND_MEMORY;
  99. }
  100. if (dev->resource[PCI_ROM_RESOURCE].start)
  101. cmd |= PCI_COMMAND_MEMORY;
  102. if (cmd != old_cmd) {
  103. printk("PCI: Enabling device %s (%04x -> %04x)\n",
  104. pci_name(dev), old_cmd, cmd);
  105. pci_write_config_word(dev, PCI_COMMAND, cmd);
  106. }
  107. return 0;
  108. }
  109. struct pci_controller * __init pcibios_alloc_controller(void)
  110. {
  111. struct pci_controller *pci_ctrl;
  112. pci_ctrl = (struct pci_controller *)alloc_bootmem(sizeof(*pci_ctrl));
  113. memset(pci_ctrl, 0, sizeof(struct pci_controller));
  114. *pci_ctrl_tail = pci_ctrl;
  115. pci_ctrl_tail = &pci_ctrl->next;
  116. return pci_ctrl;
  117. }
  118. static int __init pcibios_init(void)
  119. {
  120. struct pci_controller *pci_ctrl;
  121. struct pci_bus *bus;
  122. int next_busno = 0, i;
  123. printk("PCI: Probing PCI hardware\n");
  124. /* Scan all of the recorded PCI controllers. */
  125. for (pci_ctrl = pci_ctrl_head; pci_ctrl; pci_ctrl = pci_ctrl->next) {
  126. pci_ctrl->last_busno = 0xff;
  127. bus = pci_scan_bus(pci_ctrl->first_busno, pci_ctrl->ops,
  128. pci_ctrl);
  129. if (pci_ctrl->io_resource.flags) {
  130. unsigned long offs;
  131. offs = (unsigned long)pci_ctrl->io_space.base;
  132. pci_ctrl->io_resource.start += offs;
  133. pci_ctrl->io_resource.end += offs;
  134. bus->resource[0] = &pci_ctrl->io_resource;
  135. }
  136. for (i = 0; i < 3; ++i)
  137. if (pci_ctrl->mem_resources[i].flags)
  138. bus->resource[i+1] =&pci_ctrl->mem_resources[i];
  139. pci_ctrl->bus = bus;
  140. pci_ctrl->last_busno = bus->subordinate;
  141. if (next_busno <= pci_ctrl->last_busno)
  142. next_busno = pci_ctrl->last_busno+1;
  143. }
  144. pci_bus_count = next_busno;
  145. return platform_pcibios_fixup();
  146. }
  147. subsys_initcall(pcibios_init);
  148. void __init pcibios_fixup_bus(struct pci_bus *bus)
  149. {
  150. struct pci_controller *pci_ctrl = bus->sysdata;
  151. struct resource *res;
  152. unsigned long io_offset;
  153. int i;
  154. io_offset = (unsigned long)pci_ctrl->io_space.base;
  155. if (bus->parent == NULL) {
  156. /* this is a host bridge - fill in its resources */
  157. pci_ctrl->bus = bus;
  158. bus->resource[0] = res = &pci_ctrl->io_resource;
  159. if (!res->flags) {
  160. if (io_offset)
  161. printk (KERN_ERR "I/O resource not set for host"
  162. " bridge %d\n", pci_ctrl->index);
  163. res->start = 0;
  164. res->end = IO_SPACE_LIMIT;
  165. res->flags = IORESOURCE_IO;
  166. }
  167. res->start += io_offset;
  168. res->end += io_offset;
  169. for (i = 0; i < 3; i++) {
  170. res = &pci_ctrl->mem_resources[i];
  171. if (!res->flags) {
  172. if (i > 0)
  173. continue;
  174. printk(KERN_ERR "Memory resource not set for "
  175. "host bridge %d\n", pci_ctrl->index);
  176. res->start = 0;
  177. res->end = ~0U;
  178. res->flags = IORESOURCE_MEM;
  179. }
  180. bus->resource[i+1] = res;
  181. }
  182. } else {
  183. /* This is a subordinate bridge */
  184. pci_read_bridge_bases(bus);
  185. for (i = 0; i < 4; i++) {
  186. if ((res = bus->resource[i]) == NULL || !res->flags)
  187. continue;
  188. if (io_offset && (res->flags & IORESOURCE_IO)) {
  189. res->start += io_offset;
  190. res->end += io_offset;
  191. }
  192. }
  193. }
  194. }
  195. char __init *pcibios_setup(char *str)
  196. {
  197. return str;
  198. }
  199. /* the next one is stolen from the alpha port... */
  200. void __init
  201. pcibios_update_irq(struct pci_dev *dev, int irq)
  202. {
  203. pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq);
  204. }
  205. int pcibios_enable_device(struct pci_dev *dev, int mask)
  206. {
  207. u16 cmd, old_cmd;
  208. int idx;
  209. struct resource *r;
  210. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  211. old_cmd = cmd;
  212. for (idx=0; idx<6; idx++) {
  213. r = &dev->resource[idx];
  214. if (!r->start && r->end) {
  215. printk(KERN_ERR "PCI: Device %s not available because "
  216. "of resource collisions\n", pci_name(dev));
  217. return -EINVAL;
  218. }
  219. if (r->flags & IORESOURCE_IO)
  220. cmd |= PCI_COMMAND_IO;
  221. if (r->flags & IORESOURCE_MEM)
  222. cmd |= PCI_COMMAND_MEMORY;
  223. }
  224. if (cmd != old_cmd) {
  225. printk("PCI: Enabling device %s (%04x -> %04x)\n",
  226. pci_name(dev), old_cmd, cmd);
  227. pci_write_config_word(dev, PCI_COMMAND, cmd);
  228. }
  229. return 0;
  230. }
  231. #ifdef CONFIG_PROC_FS
  232. /*
  233. * Return the index of the PCI controller for device pdev.
  234. */
  235. int
  236. pci_controller_num(struct pci_dev *dev)
  237. {
  238. struct pci_controller *pci_ctrl = (struct pci_controller*) dev->sysdata;
  239. return pci_ctrl->index;
  240. }
  241. #endif /* CONFIG_PROC_FS */
  242. /*
  243. * Platform support for /proc/bus/pci/X/Y mmap()s,
  244. * modelled on the sparc64 implementation by Dave Miller.
  245. * -- paulus.
  246. */
  247. /*
  248. * Adjust vm_pgoff of VMA such that it is the physical page offset
  249. * corresponding to the 32-bit pci bus offset for DEV requested by the user.
  250. *
  251. * Basically, the user finds the base address for his device which he wishes
  252. * to mmap. They read the 32-bit value from the config space base register,
  253. * add whatever PAGE_SIZE multiple offset they wish, and feed this into the
  254. * offset parameter of mmap on /proc/bus/pci/XXX for that device.
  255. *
  256. * Returns negative error code on failure, zero on success.
  257. */
  258. static __inline__ int
  259. __pci_mmap_make_offset(struct pci_dev *dev, struct vm_area_struct *vma,
  260. enum pci_mmap_state mmap_state)
  261. {
  262. struct pci_controller *pci_ctrl = (struct pci_controller*) dev->sysdata;
  263. unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
  264. unsigned long io_offset = 0;
  265. int i, res_bit;
  266. if (pci_ctrl == 0)
  267. return -EINVAL; /* should never happen */
  268. /* If memory, add on the PCI bridge address offset */
  269. if (mmap_state == pci_mmap_mem) {
  270. res_bit = IORESOURCE_MEM;
  271. } else {
  272. io_offset = (unsigned long)pci_ctrl->io_space.base;
  273. offset += io_offset;
  274. res_bit = IORESOURCE_IO;
  275. }
  276. /*
  277. * Check that the offset requested corresponds to one of the
  278. * resources of the device.
  279. */
  280. for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
  281. struct resource *rp = &dev->resource[i];
  282. int flags = rp->flags;
  283. /* treat ROM as memory (should be already) */
  284. if (i == PCI_ROM_RESOURCE)
  285. flags |= IORESOURCE_MEM;
  286. /* Active and same type? */
  287. if ((flags & res_bit) == 0)
  288. continue;
  289. /* In the range of this resource? */
  290. if (offset < (rp->start & PAGE_MASK) || offset > rp->end)
  291. continue;
  292. /* found it! construct the final physical address */
  293. if (mmap_state == pci_mmap_io)
  294. offset += pci_ctrl->io_space.start - io_offset;
  295. vma->vm_pgoff = offset >> PAGE_SHIFT;
  296. return 0;
  297. }
  298. return -EINVAL;
  299. }
  300. /*
  301. * Set vm_page_prot of VMA, as appropriate for this architecture, for a pci
  302. * device mapping.
  303. */
  304. static __inline__ void
  305. __pci_mmap_set_pgprot(struct pci_dev *dev, struct vm_area_struct *vma,
  306. enum pci_mmap_state mmap_state, int write_combine)
  307. {
  308. int prot = pgprot_val(vma->vm_page_prot);
  309. /* Set to write-through */
  310. prot &= ~_PAGE_NO_CACHE;
  311. #if 0
  312. if (!write_combine)
  313. prot |= _PAGE_WRITETHRU;
  314. #endif
  315. vma->vm_page_prot = __pgprot(prot);
  316. }
  317. /*
  318. * Perform the actual remap of the pages for a PCI device mapping, as
  319. * appropriate for this architecture. The region in the process to map
  320. * is described by vm_start and vm_end members of VMA, the base physical
  321. * address is found in vm_pgoff.
  322. * The pci device structure is provided so that architectures may make mapping
  323. * decisions on a per-device or per-bus basis.
  324. *
  325. * Returns a negative error code on failure, zero on success.
  326. */
  327. int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma,
  328. enum pci_mmap_state mmap_state,
  329. int write_combine)
  330. {
  331. int ret;
  332. ret = __pci_mmap_make_offset(dev, vma, mmap_state);
  333. if (ret < 0)
  334. return ret;
  335. __pci_mmap_set_pgprot(dev, vma, mmap_state, write_combine);
  336. ret = io_remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
  337. vma->vm_end - vma->vm_start,vma->vm_page_prot);
  338. return ret;
  339. }