nv50_instmem.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. /*
  2. * Copyright (C) 2007 Ben Skeggs.
  3. *
  4. * All Rights Reserved.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining
  7. * a copy of this software and associated documentation files (the
  8. * "Software"), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sublicense, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * The above copyright notice and this permission notice (including the
  15. * next paragraph) shall be included in all copies or substantial
  16. * portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  19. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  20. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  21. * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
  22. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  23. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  24. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. *
  26. */
  27. #include "drmP.h"
  28. #include "drm.h"
  29. #include "nouveau_drv.h"
  30. #include "nouveau_vm.h"
  31. #define BAR1_VM_BASE 0x0020000000ULL
  32. #define BAR1_VM_SIZE pci_resource_len(dev->pdev, 1)
  33. #define BAR3_VM_BASE 0x0000000000ULL
  34. #define BAR3_VM_SIZE pci_resource_len(dev->pdev, 3)
  35. struct nv50_instmem_priv {
  36. uint32_t save1700[5]; /* 0x1700->0x1710 */
  37. struct nouveau_gpuobj *bar1_dmaobj;
  38. struct nouveau_gpuobj *bar3_dmaobj;
  39. };
  40. static void
  41. nv50_channel_del(struct nouveau_channel **pchan)
  42. {
  43. struct nouveau_channel *chan;
  44. chan = *pchan;
  45. *pchan = NULL;
  46. if (!chan)
  47. return;
  48. nouveau_gpuobj_ref(NULL, &chan->ramfc);
  49. nouveau_vm_ref(NULL, &chan->vm, chan->vm_pd);
  50. nouveau_gpuobj_ref(NULL, &chan->vm_pd);
  51. if (chan->ramin_heap.free_stack.next)
  52. drm_mm_takedown(&chan->ramin_heap);
  53. nouveau_gpuobj_ref(NULL, &chan->ramin);
  54. kfree(chan);
  55. }
  56. static int
  57. nv50_channel_new(struct drm_device *dev, u32 size, struct nouveau_vm *vm,
  58. struct nouveau_channel **pchan)
  59. {
  60. struct drm_nouveau_private *dev_priv = dev->dev_private;
  61. u32 pgd = (dev_priv->chipset == 0x50) ? 0x1400 : 0x0200;
  62. u32 fc = (dev_priv->chipset == 0x50) ? 0x0000 : 0x4200;
  63. struct nouveau_channel *chan;
  64. int ret, i;
  65. chan = kzalloc(sizeof(*chan), GFP_KERNEL);
  66. if (!chan)
  67. return -ENOMEM;
  68. chan->dev = dev;
  69. ret = nouveau_gpuobj_new(dev, NULL, size, 0x1000, 0, &chan->ramin);
  70. if (ret) {
  71. nv50_channel_del(&chan);
  72. return ret;
  73. }
  74. ret = drm_mm_init(&chan->ramin_heap, 0x6000, chan->ramin->size);
  75. if (ret) {
  76. nv50_channel_del(&chan);
  77. return ret;
  78. }
  79. ret = nouveau_gpuobj_new_fake(dev, chan->ramin->pinst == ~0 ? ~0 :
  80. chan->ramin->pinst + pgd,
  81. chan->ramin->vinst + pgd,
  82. 0x4000, NVOBJ_FLAG_ZERO_ALLOC,
  83. &chan->vm_pd);
  84. if (ret) {
  85. nv50_channel_del(&chan);
  86. return ret;
  87. }
  88. for (i = 0; i < 0x4000; i += 8) {
  89. nv_wo32(chan->vm_pd, i + 0, 0x00000000);
  90. nv_wo32(chan->vm_pd, i + 4, 0xdeadcafe);
  91. }
  92. ret = nouveau_vm_ref(vm, &chan->vm, chan->vm_pd);
  93. if (ret) {
  94. nv50_channel_del(&chan);
  95. return ret;
  96. }
  97. ret = nouveau_gpuobj_new_fake(dev, chan->ramin->pinst == ~0 ? ~0 :
  98. chan->ramin->pinst + fc,
  99. chan->ramin->vinst + fc, 0x100,
  100. NVOBJ_FLAG_ZERO_ALLOC, &chan->ramfc);
  101. if (ret) {
  102. nv50_channel_del(&chan);
  103. return ret;
  104. }
  105. *pchan = chan;
  106. return 0;
  107. }
  108. int
  109. nv50_instmem_init(struct drm_device *dev)
  110. {
  111. struct drm_nouveau_private *dev_priv = dev->dev_private;
  112. struct nv50_instmem_priv *priv;
  113. struct nouveau_channel *chan;
  114. struct nouveau_vm *vm;
  115. int ret, i;
  116. u32 tmp;
  117. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  118. if (!priv)
  119. return -ENOMEM;
  120. dev_priv->engine.instmem.priv = priv;
  121. /* Save state, will restore at takedown. */
  122. for (i = 0x1700; i <= 0x1710; i += 4)
  123. priv->save1700[(i-0x1700)/4] = nv_rd32(dev, i);
  124. /* Global PRAMIN heap */
  125. ret = drm_mm_init(&dev_priv->ramin_heap, 0, dev_priv->ramin_size);
  126. if (ret) {
  127. NV_ERROR(dev, "Failed to init RAMIN heap\n");
  128. goto error;
  129. }
  130. /* BAR3 */
  131. ret = nouveau_vm_new(dev, BAR3_VM_BASE, BAR3_VM_SIZE, BAR3_VM_BASE,
  132. 29, 12, 16, &dev_priv->bar3_vm);
  133. if (ret)
  134. goto error;
  135. ret = nouveau_gpuobj_new(dev, NULL, (BAR3_VM_SIZE >> 12) * 8,
  136. 0x1000, NVOBJ_FLAG_DONT_MAP |
  137. NVOBJ_FLAG_ZERO_ALLOC,
  138. &dev_priv->bar3_vm->pgt[0].obj);
  139. if (ret)
  140. goto error;
  141. dev_priv->bar3_vm->pgt[0].page_shift = 12;
  142. dev_priv->bar3_vm->pgt[0].refcount = 1;
  143. nv50_instmem_map(dev_priv->bar3_vm->pgt[0].obj);
  144. ret = nv50_channel_new(dev, 128 * 1024, dev_priv->bar3_vm, &chan);
  145. if (ret)
  146. goto error;
  147. dev_priv->channels.ptr[0] = dev_priv->channels.ptr[127] = chan;
  148. ret = nv50_gpuobj_dma_new(chan, 0x0000, BAR3_VM_BASE, BAR3_VM_SIZE,
  149. NV_MEM_TARGET_VM, NV_MEM_ACCESS_VM,
  150. NV_MEM_TYPE_VM, NV_MEM_COMP_VM,
  151. &priv->bar3_dmaobj);
  152. if (ret)
  153. goto error;
  154. nv_wr32(dev, 0x001704, 0x00000000 | (chan->ramin->vinst >> 12));
  155. nv_wr32(dev, 0x001704, 0x40000000 | (chan->ramin->vinst >> 12));
  156. nv_wr32(dev, 0x00170c, 0x80000000 | (priv->bar3_dmaobj->cinst >> 4));
  157. tmp = nv_ri32(dev, 0);
  158. nv_wi32(dev, 0, ~tmp);
  159. if (nv_ri32(dev, 0) != ~tmp) {
  160. NV_ERROR(dev, "PRAMIN readback failed\n");
  161. ret = -EIO;
  162. goto error;
  163. }
  164. nv_wi32(dev, 0, tmp);
  165. dev_priv->ramin_available = true;
  166. /* BAR1 */
  167. ret = nouveau_vm_new(dev, BAR1_VM_BASE, BAR1_VM_SIZE, BAR1_VM_BASE,
  168. 29, 12, 16, &vm);
  169. if (ret)
  170. goto error;
  171. ret = nouveau_vm_ref(vm, &dev_priv->bar1_vm, chan->vm_pd);
  172. if (ret)
  173. goto error;
  174. nouveau_vm_ref(NULL, &vm, NULL);
  175. ret = nv50_gpuobj_dma_new(chan, 0x0000, BAR1_VM_BASE, BAR1_VM_SIZE,
  176. NV_MEM_TARGET_VM, NV_MEM_ACCESS_VM,
  177. NV_MEM_TYPE_VM, NV_MEM_COMP_VM,
  178. &priv->bar1_dmaobj);
  179. if (ret)
  180. goto error;
  181. nv_wr32(dev, 0x001708, 0x80000000 | (priv->bar1_dmaobj->cinst >> 4));
  182. for (i = 0; i < 8; i++)
  183. nv_wr32(dev, 0x1900 + (i*4), 0);
  184. /* Create shared channel VM, space is reserved at the beginning
  185. * to catch "NULL pointer" references
  186. */
  187. ret = nouveau_vm_new(dev, 0, (1ULL << 40), 0x0020000000ULL,
  188. 29, 12, 16, &dev_priv->chan_vm);
  189. if (ret)
  190. return ret;
  191. return 0;
  192. error:
  193. nv50_instmem_takedown(dev);
  194. return ret;
  195. }
  196. void
  197. nv50_instmem_takedown(struct drm_device *dev)
  198. {
  199. struct drm_nouveau_private *dev_priv = dev->dev_private;
  200. struct nv50_instmem_priv *priv = dev_priv->engine.instmem.priv;
  201. struct nouveau_channel *chan = dev_priv->channels.ptr[0];
  202. int i;
  203. NV_DEBUG(dev, "\n");
  204. if (!priv)
  205. return;
  206. dev_priv->ramin_available = false;
  207. nouveau_vm_ref(NULL, &dev_priv->chan_vm, NULL);
  208. for (i = 0x1700; i <= 0x1710; i += 4)
  209. nv_wr32(dev, i, priv->save1700[(i - 0x1700) / 4]);
  210. nouveau_gpuobj_ref(NULL, &priv->bar3_dmaobj);
  211. nouveau_gpuobj_ref(NULL, &priv->bar1_dmaobj);
  212. nouveau_vm_ref(NULL, &dev_priv->bar1_vm, chan->vm_pd);
  213. dev_priv->channels.ptr[127] = 0;
  214. nv50_channel_del(&dev_priv->channels.ptr[0]);
  215. nouveau_gpuobj_ref(NULL, &dev_priv->bar3_vm->pgt[0].obj);
  216. nouveau_vm_ref(NULL, &dev_priv->bar3_vm, NULL);
  217. if (dev_priv->ramin_heap.free_stack.next)
  218. drm_mm_takedown(&dev_priv->ramin_heap);
  219. dev_priv->engine.instmem.priv = NULL;
  220. kfree(priv);
  221. }
  222. int
  223. nv50_instmem_suspend(struct drm_device *dev)
  224. {
  225. struct drm_nouveau_private *dev_priv = dev->dev_private;
  226. dev_priv->ramin_available = false;
  227. return 0;
  228. }
  229. void
  230. nv50_instmem_resume(struct drm_device *dev)
  231. {
  232. struct drm_nouveau_private *dev_priv = dev->dev_private;
  233. struct nv50_instmem_priv *priv = dev_priv->engine.instmem.priv;
  234. struct nouveau_channel *chan = dev_priv->channels.ptr[0];
  235. int i;
  236. /* Poke the relevant regs, and pray it works :) */
  237. nv_wr32(dev, NV50_PUNK_BAR_CFG_BASE, (chan->ramin->vinst >> 12));
  238. nv_wr32(dev, NV50_PUNK_UNK1710, 0);
  239. nv_wr32(dev, NV50_PUNK_BAR_CFG_BASE, (chan->ramin->vinst >> 12) |
  240. NV50_PUNK_BAR_CFG_BASE_VALID);
  241. nv_wr32(dev, NV50_PUNK_BAR1_CTXDMA, (priv->bar1_dmaobj->cinst >> 4) |
  242. NV50_PUNK_BAR1_CTXDMA_VALID);
  243. nv_wr32(dev, NV50_PUNK_BAR3_CTXDMA, (priv->bar3_dmaobj->cinst >> 4) |
  244. NV50_PUNK_BAR3_CTXDMA_VALID);
  245. for (i = 0; i < 8; i++)
  246. nv_wr32(dev, 0x1900 + (i*4), 0);
  247. dev_priv->ramin_available = true;
  248. }
  249. struct nv50_gpuobj_node {
  250. struct nouveau_vram *vram;
  251. struct nouveau_vma chan_vma;
  252. u32 align;
  253. };
  254. int
  255. nv50_instmem_get(struct nouveau_gpuobj *gpuobj, u32 size, u32 align)
  256. {
  257. struct drm_device *dev = gpuobj->dev;
  258. struct drm_nouveau_private *dev_priv = dev->dev_private;
  259. struct nv50_gpuobj_node *node = NULL;
  260. int ret;
  261. node = kzalloc(sizeof(*node), GFP_KERNEL);
  262. if (!node)
  263. return -ENOMEM;
  264. node->align = align;
  265. size = (size + 4095) & ~4095;
  266. align = max(align, (u32)4096);
  267. ret = nv50_vram_new(dev, size, align, 0, 0, &node->vram);
  268. if (ret) {
  269. kfree(node);
  270. return ret;
  271. }
  272. gpuobj->vinst = node->vram->offset;
  273. if (gpuobj->flags & NVOBJ_FLAG_VM) {
  274. ret = nouveau_vm_get(dev_priv->chan_vm, size, 12,
  275. NV_MEM_ACCESS_RW | NV_MEM_ACCESS_SYS,
  276. &node->chan_vma);
  277. if (ret) {
  278. nv50_vram_del(dev, &node->vram);
  279. kfree(node);
  280. return ret;
  281. }
  282. nouveau_vm_map(&node->chan_vma, node->vram);
  283. gpuobj->vinst = node->chan_vma.offset;
  284. }
  285. gpuobj->size = size;
  286. gpuobj->node = node;
  287. return 0;
  288. }
  289. void
  290. nv50_instmem_put(struct nouveau_gpuobj *gpuobj)
  291. {
  292. struct drm_device *dev = gpuobj->dev;
  293. struct nv50_gpuobj_node *node;
  294. node = gpuobj->node;
  295. gpuobj->node = NULL;
  296. if (node->chan_vma.node) {
  297. nouveau_vm_unmap(&node->chan_vma);
  298. nouveau_vm_put(&node->chan_vma);
  299. }
  300. nv50_vram_del(dev, &node->vram);
  301. kfree(node);
  302. }
  303. int
  304. nv50_instmem_map(struct nouveau_gpuobj *gpuobj)
  305. {
  306. struct drm_nouveau_private *dev_priv = gpuobj->dev->dev_private;
  307. struct nv50_gpuobj_node *node = gpuobj->node;
  308. int ret;
  309. ret = nouveau_vm_get(dev_priv->bar3_vm, gpuobj->size, 12,
  310. NV_MEM_ACCESS_RW, &node->vram->bar_vma);
  311. if (ret)
  312. return ret;
  313. nouveau_vm_map(&node->vram->bar_vma, node->vram);
  314. gpuobj->pinst = node->vram->bar_vma.offset;
  315. return 0;
  316. }
  317. void
  318. nv50_instmem_unmap(struct nouveau_gpuobj *gpuobj)
  319. {
  320. struct nv50_gpuobj_node *node = gpuobj->node;
  321. if (node->vram->bar_vma.node) {
  322. nouveau_vm_unmap(&node->vram->bar_vma);
  323. nouveau_vm_put(&node->vram->bar_vma);
  324. }
  325. }
  326. void
  327. nv50_instmem_flush(struct drm_device *dev)
  328. {
  329. nv_wr32(dev, 0x00330c, 0x00000001);
  330. if (!nv_wait(dev, 0x00330c, 0x00000002, 0x00000000))
  331. NV_ERROR(dev, "PRAMIN flush timeout\n");
  332. }
  333. void
  334. nv84_instmem_flush(struct drm_device *dev)
  335. {
  336. nv_wr32(dev, 0x070000, 0x00000001);
  337. if (!nv_wait(dev, 0x070000, 0x00000002, 0x00000000))
  338. NV_ERROR(dev, "PRAMIN flush timeout\n");
  339. }