nv50_instmem.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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. struct nv50_instmem_priv {
  32. uint32_t save1700[5]; /* 0x1700->0x1710 */
  33. struct nouveau_gpuobj *pramin_pt;
  34. struct nouveau_gpuobj *pramin_bar;
  35. struct nouveau_gpuobj *fb_bar;
  36. };
  37. static void
  38. nv50_channel_del(struct nouveau_channel **pchan)
  39. {
  40. struct nouveau_channel *chan;
  41. chan = *pchan;
  42. *pchan = NULL;
  43. if (!chan)
  44. return;
  45. nouveau_gpuobj_ref(NULL, &chan->ramfc);
  46. nouveau_gpuobj_ref(NULL, &chan->vm_pd);
  47. if (chan->ramin_heap.free_stack.next)
  48. drm_mm_takedown(&chan->ramin_heap);
  49. nouveau_gpuobj_ref(NULL, &chan->ramin);
  50. kfree(chan);
  51. }
  52. static int
  53. nv50_channel_new(struct drm_device *dev, u32 size,
  54. struct nouveau_channel **pchan)
  55. {
  56. struct drm_nouveau_private *dev_priv = dev->dev_private;
  57. u32 pgd = (dev_priv->chipset == 0x50) ? 0x1400 : 0x0200;
  58. u32 fc = (dev_priv->chipset == 0x50) ? 0x0000 : 0x4200;
  59. struct nouveau_channel *chan;
  60. int ret;
  61. chan = kzalloc(sizeof(*chan), GFP_KERNEL);
  62. if (!chan)
  63. return -ENOMEM;
  64. chan->dev = dev;
  65. ret = nouveau_gpuobj_new(dev, NULL, size, 0x1000, 0, &chan->ramin);
  66. if (ret) {
  67. nv50_channel_del(&chan);
  68. return ret;
  69. }
  70. ret = drm_mm_init(&chan->ramin_heap, 0x6000, chan->ramin->size);
  71. if (ret) {
  72. nv50_channel_del(&chan);
  73. return ret;
  74. }
  75. ret = nouveau_gpuobj_new_fake(dev, chan->ramin->pinst == ~0 ? ~0 :
  76. chan->ramin->pinst + pgd,
  77. chan->ramin->vinst + pgd,
  78. 0x4000, NVOBJ_FLAG_ZERO_ALLOC,
  79. &chan->vm_pd);
  80. if (ret) {
  81. nv50_channel_del(&chan);
  82. return ret;
  83. }
  84. ret = nouveau_gpuobj_new_fake(dev, chan->ramin->pinst == ~0 ? ~0 :
  85. chan->ramin->pinst + fc,
  86. chan->ramin->vinst + fc, 0x100,
  87. NVOBJ_FLAG_ZERO_ALLOC, &chan->ramfc);
  88. if (ret) {
  89. nv50_channel_del(&chan);
  90. return ret;
  91. }
  92. *pchan = chan;
  93. return 0;
  94. }
  95. int
  96. nv50_instmem_init(struct drm_device *dev)
  97. {
  98. struct drm_nouveau_private *dev_priv = dev->dev_private;
  99. struct nv50_instmem_priv *priv;
  100. struct nouveau_channel *chan;
  101. int ret, i;
  102. u32 tmp;
  103. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  104. if (!priv)
  105. return -ENOMEM;
  106. dev_priv->engine.instmem.priv = priv;
  107. /* Save state, will restore at takedown. */
  108. for (i = 0x1700; i <= 0x1710; i += 4)
  109. priv->save1700[(i-0x1700)/4] = nv_rd32(dev, i);
  110. /* Global PRAMIN heap */
  111. ret = drm_mm_init(&dev_priv->ramin_heap, 0, dev_priv->ramin_size);
  112. if (ret) {
  113. NV_ERROR(dev, "Failed to init RAMIN heap\n");
  114. return -ENOMEM;
  115. }
  116. /* we need a channel to plug into the hw to control the BARs */
  117. ret = nv50_channel_new(dev, 128*1024, &dev_priv->channels.ptr[0]);
  118. if (ret)
  119. return ret;
  120. chan = dev_priv->channels.ptr[127] = dev_priv->channels.ptr[0];
  121. /* allocate page table for PRAMIN BAR */
  122. ret = nouveau_gpuobj_new(dev, chan, (dev_priv->ramin_size >> 12) * 8,
  123. 0x1000, NVOBJ_FLAG_ZERO_ALLOC,
  124. &priv->pramin_pt);
  125. if (ret)
  126. return ret;
  127. nv_wo32(chan->vm_pd, 0x0000, priv->pramin_pt->vinst | 0x63);
  128. nv_wo32(chan->vm_pd, 0x0004, 0);
  129. /* DMA object for PRAMIN BAR */
  130. ret = nouveau_gpuobj_new(dev, chan, 6*4, 16, 0, &priv->pramin_bar);
  131. if (ret)
  132. return ret;
  133. nv_wo32(priv->pramin_bar, 0x00, 0x7fc00000);
  134. nv_wo32(priv->pramin_bar, 0x04, dev_priv->ramin_size - 1);
  135. nv_wo32(priv->pramin_bar, 0x08, 0x00000000);
  136. nv_wo32(priv->pramin_bar, 0x0c, 0x00000000);
  137. nv_wo32(priv->pramin_bar, 0x10, 0x00000000);
  138. nv_wo32(priv->pramin_bar, 0x14, 0x00000000);
  139. nv50_instmem_map(chan->ramin);
  140. /* poke regs... */
  141. nv_wr32(dev, 0x001704, 0x00000000 | (chan->ramin->vinst >> 12));
  142. nv_wr32(dev, 0x001704, 0x40000000 | (chan->ramin->vinst >> 12));
  143. nv_wr32(dev, 0x00170c, 0x80000000 | (priv->pramin_bar->cinst >> 4));
  144. tmp = nv_ri32(dev, 0);
  145. nv_wi32(dev, 0, ~tmp);
  146. if (nv_ri32(dev, 0) != ~tmp) {
  147. NV_ERROR(dev, "PRAMIN readback failed\n");
  148. return -EIO;
  149. }
  150. nv_wi32(dev, 0, tmp);
  151. dev_priv->ramin_available = true;
  152. /* Determine VM layout */
  153. dev_priv->vm_gart_base = roundup(NV50_VM_BLOCK, NV50_VM_BLOCK);
  154. dev_priv->vm_gart_size = NV50_VM_BLOCK;
  155. dev_priv->vm_vram_base = dev_priv->vm_gart_base + dev_priv->vm_gart_size;
  156. dev_priv->vm_vram_size = dev_priv->vram_size;
  157. if (dev_priv->vm_vram_size > NV50_VM_MAX_VRAM)
  158. dev_priv->vm_vram_size = NV50_VM_MAX_VRAM;
  159. dev_priv->vm_vram_size = roundup(dev_priv->vm_vram_size, NV50_VM_BLOCK);
  160. dev_priv->vm_vram_pt_nr = dev_priv->vm_vram_size / NV50_VM_BLOCK;
  161. dev_priv->vm_end = dev_priv->vm_vram_base + dev_priv->vm_vram_size;
  162. NV_DEBUG(dev, "NV50VM: GART 0x%016llx-0x%016llx\n",
  163. dev_priv->vm_gart_base,
  164. dev_priv->vm_gart_base + dev_priv->vm_gart_size - 1);
  165. NV_DEBUG(dev, "NV50VM: VRAM 0x%016llx-0x%016llx\n",
  166. dev_priv->vm_vram_base,
  167. dev_priv->vm_vram_base + dev_priv->vm_vram_size - 1);
  168. /* VRAM page table(s), mapped into VM at +1GiB */
  169. for (i = 0; i < dev_priv->vm_vram_pt_nr; i++) {
  170. ret = nouveau_gpuobj_new(dev, NULL, NV50_VM_BLOCK / 0x10000 * 8,
  171. 0, NVOBJ_FLAG_ZERO_ALLOC,
  172. &chan->vm_vram_pt[i]);
  173. if (ret) {
  174. NV_ERROR(dev, "Error creating VRAM PGT: %d\n", ret);
  175. dev_priv->vm_vram_pt_nr = i;
  176. return ret;
  177. }
  178. dev_priv->vm_vram_pt[i] = chan->vm_vram_pt[i];
  179. nv_wo32(chan->vm_pd, 0x10 + (i*8),
  180. chan->vm_vram_pt[i]->vinst | 0x61);
  181. nv_wo32(chan->vm_pd, 0x14 + (i*8), 0);
  182. }
  183. /* DMA object for FB BAR */
  184. ret = nouveau_gpuobj_new(dev, chan, 6*4, 16, 0, &priv->fb_bar);
  185. if (ret)
  186. return ret;
  187. nv_wo32(priv->fb_bar, 0x00, 0x7fc00000);
  188. nv_wo32(priv->fb_bar, 0x04, 0x40000000 +
  189. pci_resource_len(dev->pdev, 1) - 1);
  190. nv_wo32(priv->fb_bar, 0x08, 0x40000000);
  191. nv_wo32(priv->fb_bar, 0x0c, 0x00000000);
  192. nv_wo32(priv->fb_bar, 0x10, 0x00000000);
  193. nv_wo32(priv->fb_bar, 0x14, 0x00000000);
  194. dev_priv->engine.instmem.flush(dev);
  195. nv_wr32(dev, 0x001708, 0x80000000 | (priv->fb_bar->cinst >> 4));
  196. for (i = 0; i < 8; i++)
  197. nv_wr32(dev, 0x1900 + (i*4), 0);
  198. return 0;
  199. }
  200. void
  201. nv50_instmem_takedown(struct drm_device *dev)
  202. {
  203. struct drm_nouveau_private *dev_priv = dev->dev_private;
  204. struct nv50_instmem_priv *priv = dev_priv->engine.instmem.priv;
  205. struct nouveau_channel *chan = dev_priv->channels.ptr[0];
  206. int i;
  207. NV_DEBUG(dev, "\n");
  208. if (!priv)
  209. return;
  210. dev_priv->ramin_available = false;
  211. /* Restore state from before init */
  212. for (i = 0x1700; i <= 0x1710; i += 4)
  213. nv_wr32(dev, i, priv->save1700[(i - 0x1700) / 4]);
  214. nouveau_gpuobj_ref(NULL, &priv->fb_bar);
  215. nouveau_gpuobj_ref(NULL, &priv->pramin_bar);
  216. nouveau_gpuobj_ref(NULL, &priv->pramin_pt);
  217. /* Destroy dummy channel */
  218. if (chan) {
  219. for (i = 0; i < dev_priv->vm_vram_pt_nr; i++)
  220. nouveau_gpuobj_ref(NULL, &chan->vm_vram_pt[i]);
  221. dev_priv->vm_vram_pt_nr = 0;
  222. nv50_channel_del(&dev_priv->channels.ptr[0]);
  223. dev_priv->channels.ptr[127] = NULL;
  224. }
  225. dev_priv->engine.instmem.priv = NULL;
  226. kfree(priv);
  227. }
  228. int
  229. nv50_instmem_suspend(struct drm_device *dev)
  230. {
  231. struct drm_nouveau_private *dev_priv = dev->dev_private;
  232. dev_priv->ramin_available = false;
  233. return 0;
  234. }
  235. void
  236. nv50_instmem_resume(struct drm_device *dev)
  237. {
  238. struct drm_nouveau_private *dev_priv = dev->dev_private;
  239. struct nv50_instmem_priv *priv = dev_priv->engine.instmem.priv;
  240. struct nouveau_channel *chan = dev_priv->channels.ptr[0];
  241. int i;
  242. /* Poke the relevant regs, and pray it works :) */
  243. nv_wr32(dev, NV50_PUNK_BAR_CFG_BASE, (chan->ramin->vinst >> 12));
  244. nv_wr32(dev, NV50_PUNK_UNK1710, 0);
  245. nv_wr32(dev, NV50_PUNK_BAR_CFG_BASE, (chan->ramin->vinst >> 12) |
  246. NV50_PUNK_BAR_CFG_BASE_VALID);
  247. nv_wr32(dev, NV50_PUNK_BAR1_CTXDMA, (priv->fb_bar->cinst >> 4) |
  248. NV50_PUNK_BAR1_CTXDMA_VALID);
  249. nv_wr32(dev, NV50_PUNK_BAR3_CTXDMA, (priv->pramin_bar->cinst >> 4) |
  250. NV50_PUNK_BAR3_CTXDMA_VALID);
  251. for (i = 0; i < 8; i++)
  252. nv_wr32(dev, 0x1900 + (i*4), 0);
  253. dev_priv->ramin_available = true;
  254. }
  255. struct nv50_gpuobj_node {
  256. struct nouveau_bo *vram;
  257. struct drm_mm_node *ramin;
  258. u32 align;
  259. };
  260. int
  261. nv50_instmem_get(struct nouveau_gpuobj *gpuobj, u32 size, u32 align)
  262. {
  263. struct drm_device *dev = gpuobj->dev;
  264. struct nv50_gpuobj_node *node = NULL;
  265. int ret;
  266. node = kzalloc(sizeof(*node), GFP_KERNEL);
  267. if (!node)
  268. return -ENOMEM;
  269. node->align = align;
  270. ret = nouveau_bo_new(dev, NULL, size, align, TTM_PL_FLAG_VRAM,
  271. 0, 0x0000, true, false, &node->vram);
  272. if (ret) {
  273. NV_ERROR(dev, "error getting PRAMIN backing pages: %d\n", ret);
  274. WARN_ON(1);
  275. return ret;
  276. }
  277. ret = nouveau_bo_pin(node->vram, TTM_PL_FLAG_VRAM);
  278. if (ret) {
  279. NV_ERROR(dev, "error pinning PRAMIN backing VRAM: %d\n", ret);
  280. nouveau_bo_ref(NULL, &node->vram);
  281. return ret;
  282. }
  283. gpuobj->vinst = node->vram->bo.mem.start << PAGE_SHIFT;
  284. gpuobj->size = node->vram->bo.mem.num_pages << PAGE_SHIFT;
  285. gpuobj->node = node;
  286. return 0;
  287. }
  288. void
  289. nv50_instmem_put(struct nouveau_gpuobj *gpuobj)
  290. {
  291. struct nv50_gpuobj_node *node;
  292. node = gpuobj->node;
  293. gpuobj->node = NULL;
  294. nouveau_bo_unpin(node->vram);
  295. nouveau_bo_ref(NULL, &node->vram);
  296. kfree(node);
  297. }
  298. int
  299. nv50_instmem_map(struct nouveau_gpuobj *gpuobj)
  300. {
  301. struct drm_nouveau_private *dev_priv = gpuobj->dev->dev_private;
  302. struct nv50_instmem_priv *priv = dev_priv->engine.instmem.priv;
  303. struct nv50_gpuobj_node *node = gpuobj->node;
  304. struct drm_device *dev = gpuobj->dev;
  305. struct drm_mm_node *ramin = NULL;
  306. u32 pte, pte_end;
  307. u64 vram;
  308. do {
  309. if (drm_mm_pre_get(&dev_priv->ramin_heap))
  310. return -ENOMEM;
  311. spin_lock(&dev_priv->ramin_lock);
  312. ramin = drm_mm_search_free(&dev_priv->ramin_heap, gpuobj->size,
  313. node->align, 0);
  314. if (ramin == NULL) {
  315. spin_unlock(&dev_priv->ramin_lock);
  316. return -ENOMEM;
  317. }
  318. ramin = drm_mm_get_block_atomic(ramin, gpuobj->size, node->align);
  319. spin_unlock(&dev_priv->ramin_lock);
  320. } while (ramin == NULL);
  321. pte = (ramin->start >> 12) << 1;
  322. pte_end = ((ramin->size >> 12) << 1) + pte;
  323. vram = gpuobj->vinst;
  324. NV_DEBUG(dev, "pramin=0x%lx, pte=%d, pte_end=%d\n",
  325. ramin->start, pte, pte_end);
  326. NV_DEBUG(dev, "first vram page: 0x%010llx\n", gpuobj->vinst);
  327. vram |= 1;
  328. if (dev_priv->vram_sys_base) {
  329. vram += dev_priv->vram_sys_base;
  330. vram |= 0x30;
  331. }
  332. while (pte < pte_end) {
  333. nv_wo32(priv->pramin_pt, (pte * 4) + 0, lower_32_bits(vram));
  334. nv_wo32(priv->pramin_pt, (pte * 4) + 4, upper_32_bits(vram));
  335. vram += 0x1000;
  336. pte += 2;
  337. }
  338. dev_priv->engine.instmem.flush(dev);
  339. nv50_vm_flush_engine(dev, 6);
  340. node->ramin = ramin;
  341. gpuobj->pinst = ramin->start;
  342. return 0;
  343. }
  344. void
  345. nv50_instmem_unmap(struct nouveau_gpuobj *gpuobj)
  346. {
  347. struct drm_nouveau_private *dev_priv = gpuobj->dev->dev_private;
  348. struct nv50_instmem_priv *priv = dev_priv->engine.instmem.priv;
  349. struct nv50_gpuobj_node *node = gpuobj->node;
  350. u32 pte, pte_end;
  351. if (!node->ramin || !dev_priv->ramin_available)
  352. return;
  353. pte = (node->ramin->start >> 12) << 1;
  354. pte_end = ((node->ramin->size >> 12) << 1) + pte;
  355. while (pte < pte_end) {
  356. nv_wo32(priv->pramin_pt, (pte * 4) + 0, 0x00000000);
  357. nv_wo32(priv->pramin_pt, (pte * 4) + 4, 0x00000000);
  358. pte += 2;
  359. }
  360. dev_priv->engine.instmem.flush(gpuobj->dev);
  361. spin_lock(&dev_priv->ramin_lock);
  362. drm_mm_put_block(node->ramin);
  363. node->ramin = NULL;
  364. spin_unlock(&dev_priv->ramin_lock);
  365. }
  366. void
  367. nv50_instmem_flush(struct drm_device *dev)
  368. {
  369. nv_wr32(dev, 0x00330c, 0x00000001);
  370. if (!nv_wait(dev, 0x00330c, 0x00000002, 0x00000000))
  371. NV_ERROR(dev, "PRAMIN flush timeout\n");
  372. }
  373. void
  374. nv84_instmem_flush(struct drm_device *dev)
  375. {
  376. nv_wr32(dev, 0x070000, 0x00000001);
  377. if (!nv_wait(dev, 0x070000, 0x00000002, 0x00000000))
  378. NV_ERROR(dev, "PRAMIN flush timeout\n");
  379. }