nvc0_instmem.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. #include "nouveau_vm.h"
  27. struct nvc0_instmem_priv {
  28. struct nouveau_gpuobj *bar1_pgd;
  29. struct nouveau_channel *bar1;
  30. struct nouveau_gpuobj *bar3_pgd;
  31. struct nouveau_channel *bar3;
  32. };
  33. int
  34. nvc0_instmem_suspend(struct drm_device *dev)
  35. {
  36. struct drm_nouveau_private *dev_priv = dev->dev_private;
  37. dev_priv->ramin_available = false;
  38. return 0;
  39. }
  40. void
  41. nvc0_instmem_resume(struct drm_device *dev)
  42. {
  43. struct drm_nouveau_private *dev_priv = dev->dev_private;
  44. struct nvc0_instmem_priv *priv = dev_priv->engine.instmem.priv;
  45. nv_mask(dev, 0x100c80, 0x00000001, 0x00000000);
  46. nv_wr32(dev, 0x001704, 0x80000000 | priv->bar1->ramin->vinst >> 12);
  47. nv_wr32(dev, 0x001714, 0xc0000000 | priv->bar3->ramin->vinst >> 12);
  48. dev_priv->ramin_available = true;
  49. }
  50. static void
  51. nvc0_channel_del(struct nouveau_channel **pchan)
  52. {
  53. struct nouveau_channel *chan;
  54. chan = *pchan;
  55. *pchan = NULL;
  56. if (!chan)
  57. return;
  58. nouveau_vm_ref(NULL, &chan->vm, NULL);
  59. if (drm_mm_initialized(&chan->ramin_heap))
  60. drm_mm_takedown(&chan->ramin_heap);
  61. nouveau_gpuobj_ref(NULL, &chan->ramin);
  62. kfree(chan);
  63. }
  64. static int
  65. nvc0_channel_new(struct drm_device *dev, u32 size, struct nouveau_vm *vm,
  66. struct nouveau_channel **pchan,
  67. struct nouveau_gpuobj *pgd, u64 vm_size)
  68. {
  69. struct nouveau_channel *chan;
  70. int ret;
  71. chan = kzalloc(sizeof(*chan), GFP_KERNEL);
  72. if (!chan)
  73. return -ENOMEM;
  74. chan->dev = dev;
  75. ret = nouveau_gpuobj_new(dev, NULL, size, 0x1000, 0, &chan->ramin);
  76. if (ret) {
  77. nvc0_channel_del(&chan);
  78. return ret;
  79. }
  80. ret = drm_mm_init(&chan->ramin_heap, 0x1000, size - 0x1000);
  81. if (ret) {
  82. nvc0_channel_del(&chan);
  83. return ret;
  84. }
  85. ret = nouveau_vm_ref(vm, &chan->vm, NULL);
  86. if (ret) {
  87. nvc0_channel_del(&chan);
  88. return ret;
  89. }
  90. nv_wo32(chan->ramin, 0x0200, lower_32_bits(pgd->vinst));
  91. nv_wo32(chan->ramin, 0x0204, upper_32_bits(pgd->vinst));
  92. nv_wo32(chan->ramin, 0x0208, lower_32_bits(vm_size - 1));
  93. nv_wo32(chan->ramin, 0x020c, upper_32_bits(vm_size - 1));
  94. *pchan = chan;
  95. return 0;
  96. }
  97. int
  98. nvc0_instmem_init(struct drm_device *dev)
  99. {
  100. struct drm_nouveau_private *dev_priv = dev->dev_private;
  101. struct nouveau_instmem_engine *pinstmem = &dev_priv->engine.instmem;
  102. struct pci_dev *pdev = dev->pdev;
  103. struct nvc0_instmem_priv *priv;
  104. struct nouveau_vm *vm = NULL;
  105. int ret;
  106. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  107. if (!priv)
  108. return -ENOMEM;
  109. pinstmem->priv = priv;
  110. /* BAR3 VM */
  111. ret = nouveau_vm_new(dev, 0, pci_resource_len(pdev, 3), 0,
  112. &dev_priv->bar3_vm);
  113. if (ret)
  114. goto error;
  115. ret = nouveau_gpuobj_new(dev, NULL,
  116. (pci_resource_len(pdev, 3) >> 12) * 8, 0,
  117. NVOBJ_FLAG_DONT_MAP |
  118. NVOBJ_FLAG_ZERO_ALLOC,
  119. &dev_priv->bar3_vm->pgt[0].obj[0]);
  120. if (ret)
  121. goto error;
  122. dev_priv->bar3_vm->pgt[0].refcount[0] = 1;
  123. nv50_instmem_map(dev_priv->bar3_vm->pgt[0].obj[0]);
  124. ret = nouveau_gpuobj_new(dev, NULL, 0x8000, 4096,
  125. NVOBJ_FLAG_ZERO_ALLOC, &priv->bar3_pgd);
  126. if (ret)
  127. goto error;
  128. ret = nouveau_vm_ref(dev_priv->bar3_vm, &vm, priv->bar3_pgd);
  129. if (ret)
  130. goto error;
  131. nouveau_vm_ref(NULL, &vm, NULL);
  132. ret = nvc0_channel_new(dev, 8192, dev_priv->bar3_vm, &priv->bar3,
  133. priv->bar3_pgd, pci_resource_len(dev->pdev, 3));
  134. if (ret)
  135. goto error;
  136. /* BAR1 VM */
  137. ret = nouveau_vm_new(dev, 0, pci_resource_len(pdev, 1), 0, &vm);
  138. if (ret)
  139. goto error;
  140. ret = nouveau_gpuobj_new(dev, NULL, 0x8000, 4096,
  141. NVOBJ_FLAG_ZERO_ALLOC, &priv->bar1_pgd);
  142. if (ret)
  143. goto error;
  144. ret = nouveau_vm_ref(vm, &dev_priv->bar1_vm, priv->bar1_pgd);
  145. if (ret)
  146. goto error;
  147. nouveau_vm_ref(NULL, &vm, NULL);
  148. ret = nvc0_channel_new(dev, 8192, dev_priv->bar1_vm, &priv->bar1,
  149. priv->bar1_pgd, pci_resource_len(dev->pdev, 1));
  150. if (ret)
  151. goto error;
  152. /* channel vm */
  153. ret = nouveau_vm_new(dev, 0, (1ULL << 40), 0x0008000000ULL,
  154. &dev_priv->chan_vm);
  155. if (ret)
  156. goto error;
  157. nvc0_instmem_resume(dev);
  158. return 0;
  159. error:
  160. nvc0_instmem_takedown(dev);
  161. return ret;
  162. }
  163. void
  164. nvc0_instmem_takedown(struct drm_device *dev)
  165. {
  166. struct drm_nouveau_private *dev_priv = dev->dev_private;
  167. struct nvc0_instmem_priv *priv = dev_priv->engine.instmem.priv;
  168. struct nouveau_vm *vm = NULL;
  169. nvc0_instmem_suspend(dev);
  170. nv_wr32(dev, 0x1704, 0x00000000);
  171. nv_wr32(dev, 0x1714, 0x00000000);
  172. nouveau_vm_ref(NULL, &dev_priv->chan_vm, NULL);
  173. nvc0_channel_del(&priv->bar1);
  174. nouveau_vm_ref(NULL, &dev_priv->bar1_vm, priv->bar1_pgd);
  175. nouveau_gpuobj_ref(NULL, &priv->bar1_pgd);
  176. nvc0_channel_del(&priv->bar3);
  177. nouveau_vm_ref(dev_priv->bar3_vm, &vm, NULL);
  178. nouveau_vm_ref(NULL, &vm, priv->bar3_pgd);
  179. nouveau_gpuobj_ref(NULL, &priv->bar3_pgd);
  180. nouveau_gpuobj_ref(NULL, &dev_priv->bar3_vm->pgt[0].obj[0]);
  181. nouveau_vm_ref(NULL, &dev_priv->bar3_vm, NULL);
  182. dev_priv->engine.instmem.priv = NULL;
  183. kfree(priv);
  184. }