nv50_instmem.c 11 KB

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