nvc0_instmem.c 6.0 KB

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