nv50_instmem.c 12 KB

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