nvc0_instmem.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * Copyright 2010 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. #include "drmP.h"
  25. #include "nouveau_drv.h"
  26. int
  27. nvc0_instmem_populate(struct drm_device *dev, struct nouveau_gpuobj *gpuobj,
  28. uint32_t *size)
  29. {
  30. int ret;
  31. *size = ALIGN(*size, 4096);
  32. if (*size == 0)
  33. return -EINVAL;
  34. ret = nouveau_bo_new(dev, NULL, *size, 0, TTM_PL_FLAG_VRAM, 0, 0x0000,
  35. true, false, &gpuobj->im_backing);
  36. if (ret) {
  37. NV_ERROR(dev, "error getting PRAMIN backing pages: %d\n", ret);
  38. return ret;
  39. }
  40. ret = nouveau_bo_pin(gpuobj->im_backing, TTM_PL_FLAG_VRAM);
  41. if (ret) {
  42. NV_ERROR(dev, "error pinning PRAMIN backing VRAM: %d\n", ret);
  43. nouveau_bo_ref(NULL, &gpuobj->im_backing);
  44. return ret;
  45. }
  46. gpuobj->im_backing_start = gpuobj->im_backing->bo.mem.mm_node->start;
  47. gpuobj->im_backing_start <<= PAGE_SHIFT;
  48. return 0;
  49. }
  50. void
  51. nvc0_instmem_clear(struct drm_device *dev, struct nouveau_gpuobj *gpuobj)
  52. {
  53. struct drm_nouveau_private *dev_priv = dev->dev_private;
  54. if (gpuobj && gpuobj->im_backing) {
  55. if (gpuobj->im_bound)
  56. dev_priv->engine.instmem.unbind(dev, gpuobj);
  57. nouveau_bo_unpin(gpuobj->im_backing);
  58. nouveau_bo_ref(NULL, &gpuobj->im_backing);
  59. gpuobj->im_backing = NULL;
  60. }
  61. }
  62. int
  63. nvc0_instmem_bind(struct drm_device *dev, struct nouveau_gpuobj *gpuobj)
  64. {
  65. struct drm_nouveau_private *dev_priv = dev->dev_private;
  66. uint32_t pte, pte_end;
  67. uint64_t vram;
  68. if (!gpuobj->im_backing || !gpuobj->im_pramin || gpuobj->im_bound)
  69. return -EINVAL;
  70. NV_DEBUG(dev, "st=0x%lx sz=0x%lx\n",
  71. gpuobj->im_pramin->start, gpuobj->im_pramin->size);
  72. pte = gpuobj->im_pramin->start >> 12;
  73. pte_end = (gpuobj->im_pramin->size >> 12) + pte;
  74. vram = gpuobj->im_backing_start;
  75. NV_DEBUG(dev, "pramin=0x%lx, pte=%d, pte_end=%d\n",
  76. gpuobj->im_pramin->start, pte, pte_end);
  77. NV_DEBUG(dev, "first vram page: 0x%08x\n", gpuobj->im_backing_start);
  78. while (pte < pte_end) {
  79. nv_wr32(dev, 0x702000 + (pte * 8), (vram >> 8) | 1);
  80. nv_wr32(dev, 0x702004 + (pte * 8), 0);
  81. vram += 4096;
  82. pte++;
  83. }
  84. dev_priv->engine.instmem.flush(dev);
  85. if (1) {
  86. u32 chan = nv_rd32(dev, 0x1700) << 16;
  87. nv_wr32(dev, 0x100cb8, (chan + 0x1000) >> 8);
  88. nv_wr32(dev, 0x100cbc, 0x80000005);
  89. }
  90. gpuobj->im_bound = 1;
  91. return 0;
  92. }
  93. int
  94. nvc0_instmem_unbind(struct drm_device *dev, struct nouveau_gpuobj *gpuobj)
  95. {
  96. struct drm_nouveau_private *dev_priv = dev->dev_private;
  97. uint32_t pte, pte_end;
  98. if (gpuobj->im_bound == 0)
  99. return -EINVAL;
  100. pte = gpuobj->im_pramin->start >> 12;
  101. pte_end = (gpuobj->im_pramin->size >> 12) + pte;
  102. while (pte < pte_end) {
  103. nv_wr32(dev, 0x702000 + (pte * 8), 0);
  104. nv_wr32(dev, 0x702004 + (pte * 8), 0);
  105. pte++;
  106. }
  107. dev_priv->engine.instmem.flush(dev);
  108. gpuobj->im_bound = 0;
  109. return 0;
  110. }
  111. void
  112. nvc0_instmem_flush(struct drm_device *dev)
  113. {
  114. nv_wr32(dev, 0x070000, 1);
  115. if (!nv_wait(0x070000, 0x00000002, 0x00000000))
  116. NV_ERROR(dev, "PRAMIN flush timeout\n");
  117. }
  118. int
  119. nvc0_instmem_suspend(struct drm_device *dev)
  120. {
  121. struct drm_nouveau_private *dev_priv = dev->dev_private;
  122. u32 *buf;
  123. int i;
  124. dev_priv->susres.ramin_copy = vmalloc(65536);
  125. if (!dev_priv->susres.ramin_copy)
  126. return -ENOMEM;
  127. buf = dev_priv->susres.ramin_copy;
  128. for (i = 0; i < 65536; i += 4)
  129. buf[i/4] = nv_rd32(dev, NV04_PRAMIN + i);
  130. return 0;
  131. }
  132. void
  133. nvc0_instmem_resume(struct drm_device *dev)
  134. {
  135. struct drm_nouveau_private *dev_priv = dev->dev_private;
  136. u32 *buf = dev_priv->susres.ramin_copy;
  137. u64 chan;
  138. int i;
  139. chan = dev_priv->vram_size - dev_priv->ramin_rsvd_vram;
  140. nv_wr32(dev, 0x001700, chan >> 16);
  141. for (i = 0; i < 65536; i += 4)
  142. nv_wr32(dev, NV04_PRAMIN + i, buf[i/4]);
  143. vfree(dev_priv->susres.ramin_copy);
  144. dev_priv->susres.ramin_copy = NULL;
  145. nv_wr32(dev, 0x001714, 0xc0000000 | (chan >> 12));
  146. }
  147. int
  148. nvc0_instmem_init(struct drm_device *dev)
  149. {
  150. struct drm_nouveau_private *dev_priv = dev->dev_private;
  151. u64 chan, pgt3, imem, lim3 = dev_priv->ramin_size - 1;
  152. int ret, i;
  153. dev_priv->ramin_rsvd_vram = 1 * 1024 * 1024;
  154. chan = dev_priv->vram_size - dev_priv->ramin_rsvd_vram;
  155. imem = 4096 + 4096 + 32768;
  156. nv_wr32(dev, 0x001700, chan >> 16);
  157. /* channel setup */
  158. nv_wr32(dev, 0x700200, lower_32_bits(chan + 0x1000));
  159. nv_wr32(dev, 0x700204, upper_32_bits(chan + 0x1000));
  160. nv_wr32(dev, 0x700208, lower_32_bits(lim3));
  161. nv_wr32(dev, 0x70020c, upper_32_bits(lim3));
  162. /* point pgd -> pgt */
  163. nv_wr32(dev, 0x701000, 0);
  164. nv_wr32(dev, 0x701004, ((chan + 0x2000) >> 8) | 1);
  165. /* point pgt -> physical vram for channel */
  166. pgt3 = 0x2000;
  167. for (i = 0; i < dev_priv->ramin_rsvd_vram; i += 4096, pgt3 += 8) {
  168. nv_wr32(dev, 0x700000 + pgt3, ((chan + i) >> 8) | 1);
  169. nv_wr32(dev, 0x700004 + pgt3, 0);
  170. }
  171. /* clear rest of pgt */
  172. for (; i < dev_priv->ramin_size; i += 4096, pgt3 += 8) {
  173. nv_wr32(dev, 0x700000 + pgt3, 0);
  174. nv_wr32(dev, 0x700004 + pgt3, 0);
  175. }
  176. /* point bar3 at the channel */
  177. nv_wr32(dev, 0x001714, 0xc0000000 | (chan >> 12));
  178. /* Global PRAMIN heap */
  179. ret = drm_mm_init(&dev_priv->ramin_heap, imem,
  180. dev_priv->ramin_size - imem);
  181. if (ret) {
  182. NV_ERROR(dev, "Failed to init RAMIN heap\n");
  183. return -ENOMEM;
  184. }
  185. /*XXX: incorrect, but needed to make hash func "work" */
  186. dev_priv->ramht_offset = 0x10000;
  187. dev_priv->ramht_bits = 9;
  188. dev_priv->ramht_size = (1 << dev_priv->ramht_bits) * 8;
  189. return 0;
  190. }
  191. void
  192. nvc0_instmem_takedown(struct drm_device *dev)
  193. {
  194. }