PageRenderTime 56ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/pci/setup-res.c

https://gitlab.com/jhalayashraj/nkernel
C | 341 lines | 243 code | 50 blank | 48 comment | 44 complexity | 3cdebb56edc52812bde96a7b005efb33 MD5 | raw file
  1. /*
  2. * drivers/pci/setup-res.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. * David Miller (davem@redhat.com)
  8. *
  9. * Support routines for initializing a PCI subsystem.
  10. */
  11. /* fixed for multiple pci buses, 1999 Andrea Arcangeli <andrea@suse.de> */
  12. /*
  13. * Nov 2000, Ivan Kokshaysky <ink@jurassic.park.msu.ru>
  14. * Resource sorting
  15. */
  16. #include <linux/init.h>
  17. #include <linux/kernel.h>
  18. #include <linux/export.h>
  19. #include <linux/pci.h>
  20. #include <linux/errno.h>
  21. #include <linux/ioport.h>
  22. #include <linux/cache.h>
  23. #include <linux/slab.h>
  24. #include "pci.h"
  25. void pci_update_resource(struct pci_dev *dev, int resno)
  26. {
  27. struct pci_bus_region region;
  28. u32 new, check, mask;
  29. int reg;
  30. enum pci_bar_type type;
  31. struct resource *res = dev->resource + resno;
  32. /*
  33. * Ignore resources for unimplemented BARs and unused resource slots
  34. * for 64 bit BARs.
  35. */
  36. if (!res->flags)
  37. return;
  38. /*
  39. * Ignore non-moveable resources. This might be legacy resources for
  40. * which no functional BAR register exists or another important
  41. * system resource we shouldn't move around.
  42. */
  43. if (res->flags & IORESOURCE_PCI_FIXED)
  44. return;
  45. pcibios_resource_to_bus(dev->bus, &region, res);
  46. new = region.start | (res->flags & PCI_REGION_FLAG_MASK);
  47. if (res->flags & IORESOURCE_IO)
  48. mask = (u32)PCI_BASE_ADDRESS_IO_MASK;
  49. else
  50. mask = (u32)PCI_BASE_ADDRESS_MEM_MASK;
  51. reg = pci_resource_bar(dev, resno, &type);
  52. if (!reg)
  53. return;
  54. if (type != pci_bar_unknown) {
  55. if (!(res->flags & IORESOURCE_ROM_ENABLE))
  56. return;
  57. new |= PCI_ROM_ADDRESS_ENABLE;
  58. }
  59. pci_write_config_dword(dev, reg, new);
  60. pci_read_config_dword(dev, reg, &check);
  61. if ((new ^ check) & mask) {
  62. dev_err(&dev->dev, "BAR %d: error updating (%#08x != %#08x)\n",
  63. resno, new, check);
  64. }
  65. if (res->flags & IORESOURCE_MEM_64) {
  66. new = region.start >> 16 >> 16;
  67. pci_write_config_dword(dev, reg + 4, new);
  68. pci_read_config_dword(dev, reg + 4, &check);
  69. if (check != new) {
  70. dev_err(&dev->dev, "BAR %d: error updating "
  71. "(high %#08x != %#08x)\n", resno, new, check);
  72. }
  73. }
  74. res->flags &= ~IORESOURCE_UNSET;
  75. dev_dbg(&dev->dev, "BAR %d: set to %pR (PCI address [%#llx-%#llx])\n",
  76. resno, res, (unsigned long long)region.start,
  77. (unsigned long long)region.end);
  78. }
  79. int pci_claim_resource(struct pci_dev *dev, int resource)
  80. {
  81. struct resource *res = &dev->resource[resource];
  82. struct resource *root, *conflict;
  83. root = pci_find_parent_resource(dev, res);
  84. if (!root) {
  85. dev_info(&dev->dev, "no compatible bridge window for %pR\n",
  86. res);
  87. return -EINVAL;
  88. }
  89. conflict = request_resource_conflict(root, res);
  90. if (conflict) {
  91. dev_info(&dev->dev,
  92. "address space collision: %pR conflicts with %s %pR\n",
  93. res, conflict->name, conflict);
  94. return -EBUSY;
  95. }
  96. return 0;
  97. }
  98. EXPORT_SYMBOL(pci_claim_resource);
  99. void pci_disable_bridge_window(struct pci_dev *dev)
  100. {
  101. dev_info(&dev->dev, "disabling bridge mem windows\n");
  102. /* MMIO Base/Limit */
  103. pci_write_config_dword(dev, PCI_MEMORY_BASE, 0x0000fff0);
  104. /* Prefetchable MMIO Base/Limit */
  105. pci_write_config_dword(dev, PCI_PREF_LIMIT_UPPER32, 0);
  106. pci_write_config_dword(dev, PCI_PREF_MEMORY_BASE, 0x0000fff0);
  107. pci_write_config_dword(dev, PCI_PREF_BASE_UPPER32, 0xffffffff);
  108. }
  109. static int __pci_assign_resource(struct pci_bus *bus, struct pci_dev *dev,
  110. int resno, resource_size_t size, resource_size_t align)
  111. {
  112. struct resource *res = dev->resource + resno;
  113. resource_size_t min;
  114. int ret;
  115. min = (res->flags & IORESOURCE_IO) ? PCIBIOS_MIN_IO : PCIBIOS_MIN_MEM;
  116. /* First, try exact prefetching match.. */
  117. ret = pci_bus_alloc_resource(bus, res, size, align, min,
  118. IORESOURCE_PREFETCH,
  119. pcibios_align_resource, dev);
  120. if (ret < 0 && (res->flags & IORESOURCE_PREFETCH)) {
  121. /*
  122. * That failed.
  123. *
  124. * But a prefetching area can handle a non-prefetching
  125. * window (it will just not perform as well).
  126. */
  127. ret = pci_bus_alloc_resource(bus, res, size, align, min, 0,
  128. pcibios_align_resource, dev);
  129. }
  130. return ret;
  131. }
  132. /*
  133. * Generic function that returns a value indicating that the device's
  134. * original BIOS BAR address was not saved and so is not available for
  135. * reinstatement.
  136. *
  137. * Can be over-ridden by architecture specific code that implements
  138. * reinstatement functionality rather than leaving it disabled when
  139. * normal allocation attempts fail.
  140. */
  141. resource_size_t __weak pcibios_retrieve_fw_addr(struct pci_dev *dev, int idx)
  142. {
  143. return 0;
  144. }
  145. static int pci_revert_fw_address(struct resource *res, struct pci_dev *dev,
  146. int resno, resource_size_t size)
  147. {
  148. struct resource *root, *conflict;
  149. resource_size_t fw_addr, start, end;
  150. int ret = 0;
  151. fw_addr = pcibios_retrieve_fw_addr(dev, resno);
  152. if (!fw_addr)
  153. return 1;
  154. start = res->start;
  155. end = res->end;
  156. res->start = fw_addr;
  157. res->end = res->start + size - 1;
  158. root = pci_find_parent_resource(dev, res);
  159. if (!root) {
  160. if (res->flags & IORESOURCE_IO)
  161. root = &ioport_resource;
  162. else
  163. root = &iomem_resource;
  164. }
  165. dev_info(&dev->dev, "BAR %d: trying firmware assignment %pR\n",
  166. resno, res);
  167. conflict = request_resource_conflict(root, res);
  168. if (conflict) {
  169. dev_info(&dev->dev,
  170. "BAR %d: %pR conflicts with %s %pR\n", resno,
  171. res, conflict->name, conflict);
  172. res->start = start;
  173. res->end = end;
  174. ret = 1;
  175. }
  176. return ret;
  177. }
  178. static int _pci_assign_resource(struct pci_dev *dev, int resno,
  179. resource_size_t size, resource_size_t min_align)
  180. {
  181. struct resource *res = dev->resource + resno;
  182. struct pci_bus *bus;
  183. int ret;
  184. char *type;
  185. bus = dev->bus;
  186. while ((ret = __pci_assign_resource(bus, dev, resno, size, min_align))) {
  187. if (!bus->parent || !bus->self->transparent)
  188. break;
  189. bus = bus->parent;
  190. }
  191. if (ret) {
  192. if (res->flags & IORESOURCE_MEM)
  193. if (res->flags & IORESOURCE_PREFETCH)
  194. type = "mem pref";
  195. else
  196. type = "mem";
  197. else if (res->flags & IORESOURCE_IO)
  198. type = "io";
  199. else
  200. type = "unknown";
  201. dev_info(&dev->dev,
  202. "BAR %d: can't assign %s (size %#llx)\n",
  203. resno, type, (unsigned long long) resource_size(res));
  204. }
  205. return ret;
  206. }
  207. int pci_reassign_resource(struct pci_dev *dev, int resno, resource_size_t addsize,
  208. resource_size_t min_align)
  209. {
  210. struct resource *res = dev->resource + resno;
  211. resource_size_t new_size;
  212. int ret;
  213. if (!res->parent) {
  214. dev_info(&dev->dev, "BAR %d: can't reassign an unassigned resource %pR "
  215. "\n", resno, res);
  216. return -EINVAL;
  217. }
  218. /* already aligned with min_align */
  219. new_size = resource_size(res) + addsize;
  220. ret = _pci_assign_resource(dev, resno, new_size, min_align);
  221. if (!ret) {
  222. res->flags &= ~IORESOURCE_STARTALIGN;
  223. dev_info(&dev->dev, "BAR %d: reassigned %pR\n", resno, res);
  224. if (resno < PCI_BRIDGE_RESOURCES)
  225. pci_update_resource(dev, resno);
  226. }
  227. return ret;
  228. }
  229. int pci_assign_resource(struct pci_dev *dev, int resno)
  230. {
  231. struct resource *res = dev->resource + resno;
  232. resource_size_t align, size;
  233. struct pci_bus *bus;
  234. int ret;
  235. align = pci_resource_alignment(dev, res);
  236. if (!align) {
  237. dev_info(&dev->dev, "BAR %d: can't assign %pR "
  238. "(bogus alignment)\n", resno, res);
  239. return -EINVAL;
  240. }
  241. bus = dev->bus;
  242. size = resource_size(res);
  243. ret = _pci_assign_resource(dev, resno, size, align);
  244. /*
  245. * If we failed to assign anything, let's try the address
  246. * where firmware left it. That at least has a chance of
  247. * working, which is better than just leaving it disabled.
  248. */
  249. if (ret < 0)
  250. ret = pci_revert_fw_address(res, dev, resno, size);
  251. if (!ret) {
  252. res->flags &= ~IORESOURCE_STARTALIGN;
  253. dev_info(&dev->dev, "BAR %d: assigned %pR\n", resno, res);
  254. if (resno < PCI_BRIDGE_RESOURCES)
  255. pci_update_resource(dev, resno);
  256. }
  257. return ret;
  258. }
  259. int pci_enable_resources(struct pci_dev *dev, int mask)
  260. {
  261. u16 cmd, old_cmd;
  262. int i;
  263. struct resource *r;
  264. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  265. old_cmd = cmd;
  266. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  267. if (!(mask & (1 << i)))
  268. continue;
  269. r = &dev->resource[i];
  270. if (!(r->flags & (IORESOURCE_IO | IORESOURCE_MEM)))
  271. continue;
  272. if ((i == PCI_ROM_RESOURCE) &&
  273. (!(r->flags & IORESOURCE_ROM_ENABLE)))
  274. continue;
  275. if (!r->parent) {
  276. dev_err(&dev->dev, "device not available "
  277. "(can't reserve %pR)\n", r);
  278. return -EINVAL;
  279. }
  280. if (r->flags & IORESOURCE_IO)
  281. cmd |= PCI_COMMAND_IO;
  282. if (r->flags & IORESOURCE_MEM)
  283. cmd |= PCI_COMMAND_MEMORY;
  284. }
  285. if (cmd != old_cmd) {
  286. dev_info(&dev->dev, "enabling device (%04x -> %04x)\n",
  287. old_cmd, cmd);
  288. pci_write_config_word(dev, PCI_COMMAND, cmd);
  289. }
  290. return 0;
  291. }