nv50_instmem.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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. dev_priv->engine.instmem.flush(dev);
  158. dev_priv->ramin_available = true;
  159. tmp = nv_ro32(chan->ramin, 0);
  160. nv_wo32(chan->ramin, 0, ~tmp);
  161. if (nv_ro32(chan->ramin, 0) != ~tmp) {
  162. NV_ERROR(dev, "PRAMIN readback failed\n");
  163. ret = -EIO;
  164. goto error;
  165. }
  166. nv_wo32(chan->ramin, 0, tmp);
  167. /* BAR1 */
  168. ret = nouveau_vm_new(dev, BAR1_VM_BASE, BAR1_VM_SIZE, BAR1_VM_BASE,
  169. 29, 12, 16, &vm);
  170. if (ret)
  171. goto error;
  172. ret = nouveau_vm_ref(vm, &dev_priv->bar1_vm, chan->vm_pd);
  173. if (ret)
  174. goto error;
  175. nouveau_vm_ref(NULL, &vm, NULL);
  176. ret = nv50_gpuobj_dma_new(chan, 0x0000, BAR1_VM_BASE, BAR1_VM_SIZE,
  177. NV_MEM_TARGET_VM, NV_MEM_ACCESS_VM,
  178. NV_MEM_TYPE_VM, NV_MEM_COMP_VM,
  179. &priv->bar1_dmaobj);
  180. if (ret)
  181. goto error;
  182. nv_wr32(dev, 0x001708, 0x80000000 | (priv->bar1_dmaobj->cinst >> 4));
  183. for (i = 0; i < 8; i++)
  184. nv_wr32(dev, 0x1900 + (i*4), 0);
  185. /* Create shared channel VM, space is reserved at the beginning
  186. * to catch "NULL pointer" references
  187. */
  188. ret = nouveau_vm_new(dev, 0, (1ULL << 40), 0x0020000000ULL,
  189. 29, 12, 16, &dev_priv->chan_vm);
  190. if (ret)
  191. return ret;
  192. return 0;
  193. error:
  194. nv50_instmem_takedown(dev);
  195. return ret;
  196. }
  197. void
  198. nv50_instmem_takedown(struct drm_device *dev)
  199. {
  200. struct drm_nouveau_private *dev_priv = dev->dev_private;
  201. struct nv50_instmem_priv *priv = dev_priv->engine.instmem.priv;
  202. struct nouveau_channel *chan = dev_priv->channels.ptr[0];
  203. int i;
  204. NV_DEBUG(dev, "\n");
  205. if (!priv)
  206. return;
  207. dev_priv->ramin_available = false;
  208. nouveau_vm_ref(NULL, &dev_priv->chan_vm, NULL);
  209. for (i = 0x1700; i <= 0x1710; i += 4)
  210. nv_wr32(dev, i, priv->save1700[(i - 0x1700) / 4]);
  211. nouveau_gpuobj_ref(NULL, &priv->bar3_dmaobj);
  212. nouveau_gpuobj_ref(NULL, &priv->bar1_dmaobj);
  213. nouveau_vm_ref(NULL, &dev_priv->bar1_vm, chan->vm_pd);
  214. dev_priv->channels.ptr[127] = 0;
  215. nv50_channel_del(&dev_priv->channels.ptr[0]);
  216. nouveau_gpuobj_ref(NULL, &dev_priv->bar3_vm->pgt[0].obj);
  217. nouveau_vm_ref(NULL, &dev_priv->bar3_vm, NULL);
  218. if (dev_priv->ramin_heap.free_stack.next)
  219. drm_mm_takedown(&dev_priv->ramin_heap);
  220. dev_priv->engine.instmem.priv = NULL;
  221. kfree(priv);
  222. }
  223. int
  224. nv50_instmem_suspend(struct drm_device *dev)
  225. {
  226. struct drm_nouveau_private *dev_priv = dev->dev_private;
  227. dev_priv->ramin_available = false;
  228. return 0;
  229. }
  230. void
  231. nv50_instmem_resume(struct drm_device *dev)
  232. {
  233. struct drm_nouveau_private *dev_priv = dev->dev_private;
  234. struct nv50_instmem_priv *priv = dev_priv->engine.instmem.priv;
  235. struct nouveau_channel *chan = dev_priv->channels.ptr[0];
  236. int i;
  237. /* Poke the relevant regs, and pray it works :) */
  238. nv_wr32(dev, NV50_PUNK_BAR_CFG_BASE, (chan->ramin->vinst >> 12));
  239. nv_wr32(dev, NV50_PUNK_UNK1710, 0);
  240. nv_wr32(dev, NV50_PUNK_BAR_CFG_BASE, (chan->ramin->vinst >> 12) |
  241. NV50_PUNK_BAR_CFG_BASE_VALID);
  242. nv_wr32(dev, NV50_PUNK_BAR1_CTXDMA, (priv->bar1_dmaobj->cinst >> 4) |
  243. NV50_PUNK_BAR1_CTXDMA_VALID);
  244. nv_wr32(dev, NV50_PUNK_BAR3_CTXDMA, (priv->bar3_dmaobj->cinst >> 4) |
  245. NV50_PUNK_BAR3_CTXDMA_VALID);
  246. for (i = 0; i < 8; i++)
  247. nv_wr32(dev, 0x1900 + (i*4), 0);
  248. dev_priv->ramin_available = true;
  249. }
  250. struct nv50_gpuobj_node {
  251. struct nouveau_vram *vram;
  252. struct nouveau_vma chan_vma;
  253. u32 align;
  254. };
  255. int
  256. nv50_instmem_get(struct nouveau_gpuobj *gpuobj, u32 size, u32 align)
  257. {
  258. struct drm_device *dev = gpuobj->dev;
  259. struct drm_nouveau_private *dev_priv = dev->dev_private;
  260. struct nouveau_vram_engine *vram = &dev_priv->engine.vram;
  261. struct nv50_gpuobj_node *node = NULL;
  262. int ret;
  263. node = kzalloc(sizeof(*node), GFP_KERNEL);
  264. if (!node)
  265. return -ENOMEM;
  266. node->align = align;
  267. size = (size + 4095) & ~4095;
  268. align = max(align, (u32)4096);
  269. ret = vram->get(dev, size, align, 0, 0, &node->vram);
  270. if (ret) {
  271. kfree(node);
  272. return ret;
  273. }
  274. gpuobj->vinst = node->vram->offset;
  275. if (gpuobj->flags & NVOBJ_FLAG_VM) {
  276. ret = nouveau_vm_get(dev_priv->chan_vm, size, 12,
  277. NV_MEM_ACCESS_RW | NV_MEM_ACCESS_SYS,
  278. &node->chan_vma);
  279. if (ret) {
  280. vram->put(dev, &node->vram);
  281. kfree(node);
  282. return ret;
  283. }
  284. nouveau_vm_map(&node->chan_vma, node->vram);
  285. gpuobj->vinst = node->chan_vma.offset;
  286. }
  287. gpuobj->size = size;
  288. gpuobj->node = node;
  289. return 0;
  290. }
  291. void
  292. nv50_instmem_put(struct nouveau_gpuobj *gpuobj)
  293. {
  294. struct drm_device *dev = gpuobj->dev;
  295. struct drm_nouveau_private *dev_priv = dev->dev_private;
  296. struct nouveau_vram_engine *vram = &dev_priv->engine.vram;
  297. struct nv50_gpuobj_node *node;
  298. node = gpuobj->node;
  299. gpuobj->node = NULL;
  300. if (node->chan_vma.node) {
  301. nouveau_vm_unmap(&node->chan_vma);
  302. nouveau_vm_put(&node->chan_vma);
  303. }
  304. vram->put(dev, &node->vram);
  305. kfree(node);
  306. }
  307. int
  308. nv50_instmem_map(struct nouveau_gpuobj *gpuobj)
  309. {
  310. struct drm_nouveau_private *dev_priv = gpuobj->dev->dev_private;
  311. struct nv50_gpuobj_node *node = gpuobj->node;
  312. int ret;
  313. ret = nouveau_vm_get(dev_priv->bar3_vm, gpuobj->size, 12,
  314. NV_MEM_ACCESS_RW, &node->vram->bar_vma);
  315. if (ret)
  316. return ret;
  317. nouveau_vm_map(&node->vram->bar_vma, node->vram);
  318. gpuobj->pinst = node->vram->bar_vma.offset;
  319. return 0;
  320. }
  321. void
  322. nv50_instmem_unmap(struct nouveau_gpuobj *gpuobj)
  323. {
  324. struct nv50_gpuobj_node *node = gpuobj->node;
  325. if (node->vram->bar_vma.node) {
  326. nouveau_vm_unmap(&node->vram->bar_vma);
  327. nouveau_vm_put(&node->vram->bar_vma);
  328. }
  329. }
  330. void
  331. nv50_instmem_flush(struct drm_device *dev)
  332. {
  333. nv_wr32(dev, 0x00330c, 0x00000001);
  334. if (!nv_wait(dev, 0x00330c, 0x00000002, 0x00000000))
  335. NV_ERROR(dev, "PRAMIN flush timeout\n");
  336. }
  337. void
  338. nv84_instmem_flush(struct drm_device *dev)
  339. {
  340. nv_wr32(dev, 0x070000, 0x00000001);
  341. if (!nv_wait(dev, 0x070000, 0x00000002, 0x00000000))
  342. NV_ERROR(dev, "PRAMIN flush timeout\n");
  343. }