nv40.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /*
  2. * Copyright 2012 Red Hat Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * Authors: Ben Skeggs
  23. */
  24. #define nv40_instmem(p) container_of((p), struct nv40_instmem, base)
  25. #include "priv.h"
  26. #include <core/memory.h>
  27. #include <core/ramht.h>
  28. #include <engine/gr/nv40.h>
  29. struct nv40_instmem {
  30. struct nvkm_instmem base;
  31. struct nvkm_mm heap;
  32. void __iomem *iomem;
  33. };
  34. /******************************************************************************
  35. * instmem object implementation
  36. *****************************************************************************/
  37. #define nv40_instobj(p) container_of((p), struct nv40_instobj, memory)
  38. struct nv40_instobj {
  39. struct nvkm_memory memory;
  40. struct nv40_instmem *imem;
  41. struct nvkm_mm_node *node;
  42. };
  43. static enum nvkm_memory_target
  44. nv40_instobj_target(struct nvkm_memory *memory)
  45. {
  46. return NVKM_MEM_TARGET_INST;
  47. }
  48. static u64
  49. nv40_instobj_addr(struct nvkm_memory *memory)
  50. {
  51. return nv40_instobj(memory)->node->offset;
  52. }
  53. static u64
  54. nv40_instobj_size(struct nvkm_memory *memory)
  55. {
  56. return nv40_instobj(memory)->node->length;
  57. }
  58. static void __iomem *
  59. nv40_instobj_acquire(struct nvkm_memory *memory)
  60. {
  61. struct nv40_instobj *iobj = nv40_instobj(memory);
  62. return iobj->imem->iomem + iobj->node->offset;
  63. }
  64. static void
  65. nv40_instobj_release(struct nvkm_memory *memory)
  66. {
  67. }
  68. static u32
  69. nv40_instobj_rd32(struct nvkm_memory *memory, u64 offset)
  70. {
  71. struct nv40_instobj *iobj = nv40_instobj(memory);
  72. return ioread32_native(iobj->imem->iomem + iobj->node->offset + offset);
  73. }
  74. static void
  75. nv40_instobj_wr32(struct nvkm_memory *memory, u64 offset, u32 data)
  76. {
  77. struct nv40_instobj *iobj = nv40_instobj(memory);
  78. iowrite32_native(data, iobj->imem->iomem + iobj->node->offset + offset);
  79. }
  80. static void *
  81. nv40_instobj_dtor(struct nvkm_memory *memory)
  82. {
  83. struct nv40_instobj *iobj = nv40_instobj(memory);
  84. mutex_lock(&iobj->imem->base.subdev.mutex);
  85. nvkm_mm_free(&iobj->imem->heap, &iobj->node);
  86. mutex_unlock(&iobj->imem->base.subdev.mutex);
  87. return iobj;
  88. }
  89. static const struct nvkm_memory_func
  90. nv40_instobj_func = {
  91. .dtor = nv40_instobj_dtor,
  92. .target = nv40_instobj_target,
  93. .size = nv40_instobj_size,
  94. .addr = nv40_instobj_addr,
  95. .acquire = nv40_instobj_acquire,
  96. .release = nv40_instobj_release,
  97. .rd32 = nv40_instobj_rd32,
  98. .wr32 = nv40_instobj_wr32,
  99. };
  100. static int
  101. nv40_instobj_new(struct nvkm_instmem *base, u32 size, u32 align, bool zero,
  102. struct nvkm_memory **pmemory)
  103. {
  104. struct nv40_instmem *imem = nv40_instmem(base);
  105. struct nv40_instobj *iobj;
  106. int ret;
  107. if (!(iobj = kzalloc(sizeof(*iobj), GFP_KERNEL)))
  108. return -ENOMEM;
  109. *pmemory = &iobj->memory;
  110. nvkm_memory_ctor(&nv40_instobj_func, &iobj->memory);
  111. iobj->imem = imem;
  112. mutex_lock(&imem->base.subdev.mutex);
  113. ret = nvkm_mm_head(&imem->heap, 0, 1, size, size,
  114. align ? align : 1, &iobj->node);
  115. mutex_unlock(&imem->base.subdev.mutex);
  116. return ret;
  117. }
  118. /******************************************************************************
  119. * instmem subdev implementation
  120. *****************************************************************************/
  121. static u32
  122. nv40_instmem_rd32(struct nvkm_instmem *base, u32 addr)
  123. {
  124. return ioread32_native(nv40_instmem(base)->iomem + addr);
  125. }
  126. static void
  127. nv40_instmem_wr32(struct nvkm_instmem *base, u32 addr, u32 data)
  128. {
  129. iowrite32_native(data, nv40_instmem(base)->iomem + addr);
  130. }
  131. static int
  132. nv40_instmem_oneinit(struct nvkm_instmem *base)
  133. {
  134. struct nv40_instmem *imem = nv40_instmem(base);
  135. struct nvkm_device *device = imem->base.subdev.device;
  136. int ret, vs;
  137. /* PRAMIN aperture maps over the end of vram, reserve enough space
  138. * to fit graphics contexts for every channel, the magics come
  139. * from engine/gr/nv40.c
  140. */
  141. vs = hweight8((nvkm_rd32(device, 0x001540) & 0x0000ff00) >> 8);
  142. if (device->chipset == 0x40) imem->base.reserved = 0x6aa0 * vs;
  143. else if (device->chipset < 0x43) imem->base.reserved = 0x4f00 * vs;
  144. else if (nv44_gr_class(device)) imem->base.reserved = 0x4980 * vs;
  145. else imem->base.reserved = 0x4a40 * vs;
  146. imem->base.reserved += 16 * 1024;
  147. imem->base.reserved *= 32; /* per-channel */
  148. imem->base.reserved += 512 * 1024; /* pci(e)gart table */
  149. imem->base.reserved += 512 * 1024; /* object storage */
  150. imem->base.reserved = round_up(imem->base.reserved, 4096);
  151. ret = nvkm_mm_init(&imem->heap, 0, imem->base.reserved, 1);
  152. if (ret)
  153. return ret;
  154. /* 0x00000-0x10000: reserve for probable vbios image */
  155. ret = nvkm_memory_new(device, NVKM_MEM_TARGET_INST, 0x10000, 0, false,
  156. &imem->base.vbios);
  157. if (ret)
  158. return ret;
  159. /* 0x10000-0x18000: reserve for RAMHT */
  160. ret = nvkm_ramht_new(device, 0x08000, 0, NULL, &imem->base.ramht);
  161. if (ret)
  162. return ret;
  163. /* 0x18000-0x18200: reserve for RAMRO
  164. * 0x18200-0x20000: padding
  165. */
  166. ret = nvkm_memory_new(device, NVKM_MEM_TARGET_INST, 0x08000, 0, false,
  167. &imem->base.ramro);
  168. if (ret)
  169. return ret;
  170. /* 0x20000-0x21000: reserve for RAMFC
  171. * 0x21000-0x40000: padding and some unknown crap
  172. */
  173. ret = nvkm_memory_new(device, NVKM_MEM_TARGET_INST, 0x20000, 0, true,
  174. &imem->base.ramfc);
  175. if (ret)
  176. return ret;
  177. return 0;
  178. }
  179. static void *
  180. nv40_instmem_dtor(struct nvkm_instmem *base)
  181. {
  182. struct nv40_instmem *imem = nv40_instmem(base);
  183. nvkm_memory_del(&imem->base.ramfc);
  184. nvkm_memory_del(&imem->base.ramro);
  185. nvkm_ramht_del(&imem->base.ramht);
  186. nvkm_memory_del(&imem->base.vbios);
  187. nvkm_mm_fini(&imem->heap);
  188. if (imem->iomem)
  189. iounmap(imem->iomem);
  190. return imem;
  191. }
  192. static const struct nvkm_instmem_func
  193. nv40_instmem = {
  194. .dtor = nv40_instmem_dtor,
  195. .oneinit = nv40_instmem_oneinit,
  196. .rd32 = nv40_instmem_rd32,
  197. .wr32 = nv40_instmem_wr32,
  198. .memory_new = nv40_instobj_new,
  199. .persistent = false,
  200. .zero = false,
  201. };
  202. int
  203. nv40_instmem_new(struct nvkm_device *device, int index,
  204. struct nvkm_instmem **pimem)
  205. {
  206. struct nv40_instmem *imem;
  207. int bar;
  208. if (!(imem = kzalloc(sizeof(*imem), GFP_KERNEL)))
  209. return -ENOMEM;
  210. nvkm_instmem_ctor(&nv40_instmem, device, index, &imem->base);
  211. *pimem = &imem->base;
  212. /* map bar */
  213. if (device->func->resource_size(device, 2))
  214. bar = 2;
  215. else
  216. bar = 3;
  217. imem->iomem = ioremap(device->func->resource_addr(device, bar),
  218. device->func->resource_size(device, bar));
  219. if (!imem->iomem) {
  220. nvkm_error(&imem->base.subdev, "unable to map PRAMIN BAR\n");
  221. return -EFAULT;
  222. }
  223. return 0;
  224. }