pci.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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. /* PCI Controller */
  30. /*
  31. * pcibios_alloc_controller
  32. * pcibios_enable_device
  33. * pcibios_fixups
  34. * pcibios_align_resource
  35. * pcibios_fixup_bus
  36. * pci_bus_add_device
  37. * pci_mmap_page_range
  38. */
  39. struct pci_controller* pci_ctrl_head;
  40. struct pci_controller** pci_ctrl_tail = &pci_ctrl_head;
  41. static int pci_bus_count;
  42. /*
  43. * We need to avoid collisions with `mirrored' VGA ports
  44. * and other strange ISA hardware, so we always want the
  45. * addresses to be allocated in the 0x000-0x0ff region
  46. * modulo 0x400.
  47. *
  48. * Why? Because some silly external IO cards only decode
  49. * the low 10 bits of the IO address. The 0x00-0xff region
  50. * is reserved for motherboard devices that decode all 16
  51. * bits, so it's ok to allocate at, say, 0x2800-0x28ff,
  52. * but we want to try to avoid allocating at 0x2900-0x2bff
  53. * which might have be mirrored at 0x0100-0x03ff..
  54. */
  55. resource_size_t
  56. pcibios_align_resource(void *data, const struct resource *res,
  57. resource_size_t size, resource_size_t align)
  58. {
  59. struct pci_dev *dev = data;
  60. resource_size_t start = res->start;
  61. if (res->flags & IORESOURCE_IO) {
  62. if (size > 0x100) {
  63. pr_err("PCI: I/O Region %s/%d too large (%u bytes)\n",
  64. pci_name(dev), dev->resource - res,
  65. size);
  66. }
  67. if (start & 0x300)
  68. start = (start + 0x3ff) & ~0x3ff;
  69. }
  70. return start;
  71. }
  72. int
  73. pcibios_enable_resources(struct pci_dev *dev, int mask)
  74. {
  75. u16 cmd, old_cmd;
  76. int idx;
  77. struct resource *r;
  78. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  79. old_cmd = cmd;
  80. for(idx=0; idx<6; idx++) {
  81. r = &dev->resource[idx];
  82. if (!r->start && r->end) {
  83. pr_err("PCI: Device %s not available because "
  84. "of resource collisions\n", pci_name(dev));
  85. return -EINVAL;
  86. }
  87. if (r->flags & IORESOURCE_IO)
  88. cmd |= PCI_COMMAND_IO;
  89. if (r->flags & IORESOURCE_MEM)
  90. cmd |= PCI_COMMAND_MEMORY;
  91. }
  92. if (dev->resource[PCI_ROM_RESOURCE].start)
  93. cmd |= PCI_COMMAND_MEMORY;
  94. if (cmd != old_cmd) {
  95. pr_info("PCI: Enabling device %s (%04x -> %04x)\n",
  96. pci_name(dev), old_cmd, cmd);
  97. pci_write_config_word(dev, PCI_COMMAND, cmd);
  98. }
  99. return 0;
  100. }
  101. struct pci_controller * __init pcibios_alloc_controller(void)
  102. {
  103. struct pci_controller *pci_ctrl;
  104. pci_ctrl = (struct pci_controller *)alloc_bootmem(sizeof(*pci_ctrl));
  105. memset(pci_ctrl, 0, sizeof(struct pci_controller));
  106. *pci_ctrl_tail = pci_ctrl;
  107. pci_ctrl_tail = &pci_ctrl->next;
  108. return pci_ctrl;
  109. }
  110. static void __init pci_controller_apertures(struct pci_controller *pci_ctrl,
  111. struct list_head *resources)
  112. {
  113. struct resource *res;
  114. unsigned long io_offset;
  115. int i;
  116. io_offset = (unsigned long)pci_ctrl->io_space.base;
  117. res = &pci_ctrl->io_resource;
  118. if (!res->flags) {
  119. if (io_offset)
  120. pr_err("I/O resource not set for host bridge %d\n",
  121. pci_ctrl->index);
  122. res->start = 0;
  123. res->end = IO_SPACE_LIMIT;
  124. res->flags = IORESOURCE_IO;
  125. }
  126. res->start += io_offset;
  127. res->end += io_offset;
  128. pci_add_resource_offset(resources, res, io_offset);
  129. for (i = 0; i < 3; i++) {
  130. res = &pci_ctrl->mem_resources[i];
  131. if (!res->flags) {
  132. if (i > 0)
  133. continue;
  134. pr_err("Memory resource not set for host bridge %d\n",
  135. pci_ctrl->index);
  136. res->start = 0;
  137. res->end = ~0U;
  138. res->flags = IORESOURCE_MEM;
  139. }
  140. pci_add_resource(resources, res);
  141. }
  142. }
  143. static int __init pcibios_init(void)
  144. {
  145. struct pci_controller *pci_ctrl;
  146. struct list_head resources;
  147. struct pci_bus *bus;
  148. int next_busno = 0, ret;
  149. pr_info("PCI: Probing PCI hardware\n");
  150. /* Scan all of the recorded PCI controllers. */
  151. for (pci_ctrl = pci_ctrl_head; pci_ctrl; pci_ctrl = pci_ctrl->next) {
  152. pci_ctrl->last_busno = 0xff;
  153. INIT_LIST_HEAD(&resources);
  154. pci_controller_apertures(pci_ctrl, &resources);
  155. bus = pci_scan_root_bus(NULL, pci_ctrl->first_busno,
  156. pci_ctrl->ops, pci_ctrl, &resources);
  157. if (!bus)
  158. continue;
  159. pci_ctrl->bus = bus;
  160. pci_ctrl->last_busno = bus->busn_res.end;
  161. if (next_busno <= pci_ctrl->last_busno)
  162. next_busno = pci_ctrl->last_busno+1;
  163. }
  164. pci_bus_count = next_busno;
  165. ret = platform_pcibios_fixup();
  166. if (ret)
  167. return ret;
  168. for (pci_ctrl = pci_ctrl_head; pci_ctrl; pci_ctrl = pci_ctrl->next) {
  169. if (pci_ctrl->bus)
  170. pci_bus_add_devices(pci_ctrl->bus);
  171. }
  172. return 0;
  173. }
  174. subsys_initcall(pcibios_init);
  175. void pcibios_fixup_bus(struct pci_bus *bus)
  176. {
  177. if (bus->parent) {
  178. /* This is a subordinate bridge */
  179. pci_read_bridge_bases(bus);
  180. }
  181. }
  182. void pcibios_set_master(struct pci_dev *dev)
  183. {
  184. /* No special bus mastering setup handling */
  185. }
  186. int pcibios_enable_device(struct pci_dev *dev, int mask)
  187. {
  188. u16 cmd, old_cmd;
  189. int idx;
  190. struct resource *r;
  191. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  192. old_cmd = cmd;
  193. for (idx=0; idx<6; idx++) {
  194. r = &dev->resource[idx];
  195. if (!r->start && r->end) {
  196. pr_err("PCI: Device %s not available because "
  197. "of resource collisions\n", pci_name(dev));
  198. return -EINVAL;
  199. }
  200. if (r->flags & IORESOURCE_IO)
  201. cmd |= PCI_COMMAND_IO;
  202. if (r->flags & IORESOURCE_MEM)
  203. cmd |= PCI_COMMAND_MEMORY;
  204. }
  205. if (cmd != old_cmd) {
  206. pr_info("PCI: Enabling device %s (%04x -> %04x)\n",
  207. pci_name(dev), old_cmd, cmd);
  208. pci_write_config_word(dev, PCI_COMMAND, cmd);
  209. }
  210. return 0;
  211. }
  212. #ifdef CONFIG_PROC_FS
  213. /*
  214. * Return the index of the PCI controller for device pdev.
  215. */
  216. int
  217. pci_controller_num(struct pci_dev *dev)
  218. {
  219. struct pci_controller *pci_ctrl = (struct pci_controller*) dev->sysdata;
  220. return pci_ctrl->index;
  221. }
  222. #endif /* CONFIG_PROC_FS */
  223. /*
  224. * Platform support for /proc/bus/pci/X/Y mmap()s,
  225. * modelled on the sparc64 implementation by Dave Miller.
  226. * -- paulus.
  227. */
  228. /*
  229. * Adjust vm_pgoff of VMA such that it is the physical page offset
  230. * corresponding to the 32-bit pci bus offset for DEV requested by the user.
  231. *
  232. * Basically, the user finds the base address for his device which he wishes
  233. * to mmap. They read the 32-bit value from the config space base register,
  234. * add whatever PAGE_SIZE multiple offset they wish, and feed this into the
  235. * offset parameter of mmap on /proc/bus/pci/XXX for that device.
  236. *
  237. * Returns negative error code on failure, zero on success.
  238. */
  239. static __inline__ int
  240. __pci_mmap_make_offset(struct pci_dev *dev, struct vm_area_struct *vma,
  241. enum pci_mmap_state mmap_state)
  242. {
  243. struct pci_controller *pci_ctrl = (struct pci_controller*) dev->sysdata;
  244. unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
  245. unsigned long io_offset = 0;
  246. int i, res_bit;
  247. if (pci_ctrl == 0)
  248. return -EINVAL; /* should never happen */
  249. /* If memory, add on the PCI bridge address offset */
  250. if (mmap_state == pci_mmap_mem) {
  251. res_bit = IORESOURCE_MEM;
  252. } else {
  253. io_offset = (unsigned long)pci_ctrl->io_space.base;
  254. offset += io_offset;
  255. res_bit = IORESOURCE_IO;
  256. }
  257. /*
  258. * Check that the offset requested corresponds to one of the
  259. * resources of the device.
  260. */
  261. for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
  262. struct resource *rp = &dev->resource[i];
  263. int flags = rp->flags;
  264. /* treat ROM as memory (should be already) */
  265. if (i == PCI_ROM_RESOURCE)
  266. flags |= IORESOURCE_MEM;
  267. /* Active and same type? */
  268. if ((flags & res_bit) == 0)
  269. continue;
  270. /* In the range of this resource? */
  271. if (offset < (rp->start & PAGE_MASK) || offset > rp->end)
  272. continue;
  273. /* found it! construct the final physical address */
  274. if (mmap_state == pci_mmap_io)
  275. offset += pci_ctrl->io_space.start - io_offset;
  276. vma->vm_pgoff = offset >> PAGE_SHIFT;
  277. return 0;
  278. }
  279. return -EINVAL;
  280. }
  281. /*
  282. * Perform the actual remap of the pages for a PCI device mapping, as
  283. * appropriate for this architecture. The region in the process to map
  284. * is described by vm_start and vm_end members of VMA, the base physical
  285. * address is found in vm_pgoff.
  286. * The pci device structure is provided so that architectures may make mapping
  287. * decisions on a per-device or per-bus basis.
  288. *
  289. * Returns a negative error code on failure, zero on success.
  290. */
  291. int pci_mmap_page_range(struct pci_dev *dev, int bar,
  292. struct vm_area_struct *vma,
  293. enum pci_mmap_state mmap_state,
  294. int write_combine)
  295. {
  296. int ret;
  297. ret = __pci_mmap_make_offset(dev, vma, mmap_state);
  298. if (ret < 0)
  299. return ret;
  300. vma->vm_page_prot = pgprot_device(vma->vm_page_prot);
  301. ret = io_remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
  302. vma->vm_end - vma->vm_start,vma->vm_page_prot);
  303. return ret;
  304. }