nv50_instmem.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  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_ref *pramin_pt;
  33. struct nouveau_gpuobj_ref *pramin_bar;
  34. struct nouveau_gpuobj_ref *fb_bar;
  35. bool last_access_wr;
  36. };
  37. #define NV50_INSTMEM_PAGE_SHIFT 12
  38. #define NV50_INSTMEM_PAGE_SIZE (1 << NV50_INSTMEM_PAGE_SHIFT)
  39. #define NV50_INSTMEM_PT_SIZE(a) (((a) >> 12) << 3)
  40. /*NOTE: - Assumes 0x1700 already covers the correct MiB of PRAMIN
  41. */
  42. #define BAR0_WI32(g, o, v) do { \
  43. uint32_t offset; \
  44. if ((g)->im_backing) { \
  45. offset = (g)->im_backing_start; \
  46. } else { \
  47. offset = chan->ramin->gpuobj->im_backing_start; \
  48. offset += (g)->im_pramin->start; \
  49. } \
  50. offset += (o); \
  51. nv_wr32(dev, NV_RAMIN + (offset & 0xfffff), (v)); \
  52. } while (0)
  53. int
  54. nv50_instmem_init(struct drm_device *dev)
  55. {
  56. struct drm_nouveau_private *dev_priv = dev->dev_private;
  57. struct nouveau_channel *chan;
  58. uint32_t c_offset, c_size, c_ramfc, c_vmpd, c_base, pt_size;
  59. uint32_t save_nv001700;
  60. uint64_t v;
  61. struct nv50_instmem_priv *priv;
  62. int ret, i;
  63. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  64. if (!priv)
  65. return -ENOMEM;
  66. dev_priv->engine.instmem.priv = priv;
  67. /* Save state, will restore at takedown. */
  68. for (i = 0x1700; i <= 0x1710; i += 4)
  69. priv->save1700[(i-0x1700)/4] = nv_rd32(dev, i);
  70. /* Reserve the last MiB of VRAM, we should probably try to avoid
  71. * setting up the below tables over the top of the VBIOS image at
  72. * some point.
  73. */
  74. dev_priv->ramin_rsvd_vram = 1 << 20;
  75. c_offset = dev_priv->vram_size - dev_priv->ramin_rsvd_vram;
  76. c_size = 128 << 10;
  77. c_vmpd = ((dev_priv->chipset & 0xf0) == 0x50) ? 0x1400 : 0x200;
  78. c_ramfc = ((dev_priv->chipset & 0xf0) == 0x50) ? 0x0 : 0x20;
  79. c_base = c_vmpd + 0x4000;
  80. pt_size = NV50_INSTMEM_PT_SIZE(dev_priv->ramin_size);
  81. NV_DEBUG(dev, " Rsvd VRAM base: 0x%08x\n", c_offset);
  82. NV_DEBUG(dev, " VBIOS image: 0x%08x\n",
  83. (nv_rd32(dev, 0x619f04) & ~0xff) << 8);
  84. NV_DEBUG(dev, " Aperture size: %d MiB\n", dev_priv->ramin_size >> 20);
  85. NV_DEBUG(dev, " PT size: %d KiB\n", pt_size >> 10);
  86. /* Determine VM layout, we need to do this first to make sure
  87. * we allocate enough memory for all the page tables.
  88. */
  89. dev_priv->vm_gart_base = roundup(NV50_VM_BLOCK, NV50_VM_BLOCK);
  90. dev_priv->vm_gart_size = NV50_VM_BLOCK;
  91. dev_priv->vm_vram_base = dev_priv->vm_gart_base + dev_priv->vm_gart_size;
  92. dev_priv->vm_vram_size = dev_priv->vram_size;
  93. if (dev_priv->vm_vram_size > NV50_VM_MAX_VRAM)
  94. dev_priv->vm_vram_size = NV50_VM_MAX_VRAM;
  95. dev_priv->vm_vram_size = roundup(dev_priv->vm_vram_size, NV50_VM_BLOCK);
  96. dev_priv->vm_vram_pt_nr = dev_priv->vm_vram_size / NV50_VM_BLOCK;
  97. dev_priv->vm_end = dev_priv->vm_vram_base + dev_priv->vm_vram_size;
  98. NV_DEBUG(dev, "NV50VM: GART 0x%016llx-0x%016llx\n",
  99. dev_priv->vm_gart_base,
  100. dev_priv->vm_gart_base + dev_priv->vm_gart_size - 1);
  101. NV_DEBUG(dev, "NV50VM: VRAM 0x%016llx-0x%016llx\n",
  102. dev_priv->vm_vram_base,
  103. dev_priv->vm_vram_base + dev_priv->vm_vram_size - 1);
  104. c_size += dev_priv->vm_vram_pt_nr * (NV50_VM_BLOCK / 65536 * 8);
  105. /* Map BAR0 PRAMIN aperture over the memory we want to use */
  106. save_nv001700 = nv_rd32(dev, NV50_PUNK_BAR0_PRAMIN);
  107. nv_wr32(dev, NV50_PUNK_BAR0_PRAMIN, (c_offset >> 16));
  108. /* Create a fake channel, and use it as our "dummy" channels 0/127.
  109. * The main reason for creating a channel is so we can use the gpuobj
  110. * code. However, it's probably worth noting that NVIDIA also setup
  111. * their channels 0/127 with the same values they configure here.
  112. * So, there may be some other reason for doing this.
  113. *
  114. * Have to create the entire channel manually, as the real channel
  115. * creation code assumes we have PRAMIN access, and we don't until
  116. * we're done here.
  117. */
  118. chan = kzalloc(sizeof(*chan), GFP_KERNEL);
  119. if (!chan)
  120. return -ENOMEM;
  121. chan->id = 0;
  122. chan->dev = dev;
  123. chan->file_priv = (struct drm_file *)-2;
  124. dev_priv->fifos[0] = dev_priv->fifos[127] = chan;
  125. /* Channel's PRAMIN object + heap */
  126. ret = nouveau_gpuobj_new_fake(dev, 0, c_offset, c_size, 0,
  127. NULL, &chan->ramin);
  128. if (ret)
  129. return ret;
  130. if (nouveau_mem_init_heap(&chan->ramin_heap, c_base, c_size - c_base))
  131. return -ENOMEM;
  132. /* RAMFC + zero channel's PRAMIN up to start of VM pagedir */
  133. ret = nouveau_gpuobj_new_fake(dev, c_ramfc, c_offset + c_ramfc,
  134. 0x4000, 0, NULL, &chan->ramfc);
  135. if (ret)
  136. return ret;
  137. for (i = 0; i < c_vmpd; i += 4)
  138. BAR0_WI32(chan->ramin->gpuobj, i, 0);
  139. /* VM page directory */
  140. ret = nouveau_gpuobj_new_fake(dev, c_vmpd, c_offset + c_vmpd,
  141. 0x4000, 0, &chan->vm_pd, NULL);
  142. if (ret)
  143. return ret;
  144. for (i = 0; i < 0x4000; i += 8) {
  145. BAR0_WI32(chan->vm_pd, i + 0x00, 0x00000000);
  146. BAR0_WI32(chan->vm_pd, i + 0x04, 0x00000000);
  147. }
  148. /* PRAMIN page table, cheat and map into VM at 0x0000000000.
  149. * We map the entire fake channel into the start of the PRAMIN BAR
  150. */
  151. ret = nouveau_gpuobj_new_ref(dev, chan, NULL, 0, pt_size, 0x1000,
  152. 0, &priv->pramin_pt);
  153. if (ret)
  154. return ret;
  155. v = c_offset | 1;
  156. if (dev_priv->vram_sys_base) {
  157. v += dev_priv->vram_sys_base;
  158. v |= 0x30;
  159. }
  160. i = 0;
  161. while (v < dev_priv->vram_sys_base + c_offset + c_size) {
  162. BAR0_WI32(priv->pramin_pt->gpuobj, i + 0, lower_32_bits(v));
  163. BAR0_WI32(priv->pramin_pt->gpuobj, i + 4, upper_32_bits(v));
  164. v += 0x1000;
  165. i += 8;
  166. }
  167. while (i < pt_size) {
  168. BAR0_WI32(priv->pramin_pt->gpuobj, i + 0, 0x00000000);
  169. BAR0_WI32(priv->pramin_pt->gpuobj, i + 4, 0x00000000);
  170. i += 8;
  171. }
  172. BAR0_WI32(chan->vm_pd, 0x00, priv->pramin_pt->instance | 0x63);
  173. BAR0_WI32(chan->vm_pd, 0x04, 0x00000000);
  174. /* VRAM page table(s), mapped into VM at +1GiB */
  175. for (i = 0; i < dev_priv->vm_vram_pt_nr; i++) {
  176. ret = nouveau_gpuobj_new_ref(dev, chan, NULL, 0,
  177. NV50_VM_BLOCK/65536*8, 0, 0,
  178. &chan->vm_vram_pt[i]);
  179. if (ret) {
  180. NV_ERROR(dev, "Error creating VRAM page tables: %d\n",
  181. ret);
  182. dev_priv->vm_vram_pt_nr = i;
  183. return ret;
  184. }
  185. dev_priv->vm_vram_pt[i] = chan->vm_vram_pt[i]->gpuobj;
  186. for (v = 0; v < dev_priv->vm_vram_pt[i]->im_pramin->size;
  187. v += 4)
  188. BAR0_WI32(dev_priv->vm_vram_pt[i], v, 0);
  189. BAR0_WI32(chan->vm_pd, 0x10 + (i*8),
  190. chan->vm_vram_pt[i]->instance | 0x61);
  191. BAR0_WI32(chan->vm_pd, 0x14 + (i*8), 0);
  192. }
  193. /* DMA object for PRAMIN BAR */
  194. ret = nouveau_gpuobj_new_ref(dev, chan, chan, 0, 6*4, 16, 0,
  195. &priv->pramin_bar);
  196. if (ret)
  197. return ret;
  198. BAR0_WI32(priv->pramin_bar->gpuobj, 0x00, 0x7fc00000);
  199. BAR0_WI32(priv->pramin_bar->gpuobj, 0x04, dev_priv->ramin_size - 1);
  200. BAR0_WI32(priv->pramin_bar->gpuobj, 0x08, 0x00000000);
  201. BAR0_WI32(priv->pramin_bar->gpuobj, 0x0c, 0x00000000);
  202. BAR0_WI32(priv->pramin_bar->gpuobj, 0x10, 0x00000000);
  203. BAR0_WI32(priv->pramin_bar->gpuobj, 0x14, 0x00000000);
  204. /* DMA object for FB BAR */
  205. ret = nouveau_gpuobj_new_ref(dev, chan, chan, 0, 6*4, 16, 0,
  206. &priv->fb_bar);
  207. if (ret)
  208. return ret;
  209. BAR0_WI32(priv->fb_bar->gpuobj, 0x00, 0x7fc00000);
  210. BAR0_WI32(priv->fb_bar->gpuobj, 0x04, 0x40000000 +
  211. drm_get_resource_len(dev, 1) - 1);
  212. BAR0_WI32(priv->fb_bar->gpuobj, 0x08, 0x40000000);
  213. BAR0_WI32(priv->fb_bar->gpuobj, 0x0c, 0x00000000);
  214. BAR0_WI32(priv->fb_bar->gpuobj, 0x10, 0x00000000);
  215. BAR0_WI32(priv->fb_bar->gpuobj, 0x14, 0x00000000);
  216. /* Poke the relevant regs, and pray it works :) */
  217. nv_wr32(dev, NV50_PUNK_BAR_CFG_BASE, (chan->ramin->instance >> 12));
  218. nv_wr32(dev, NV50_PUNK_UNK1710, 0);
  219. nv_wr32(dev, NV50_PUNK_BAR_CFG_BASE, (chan->ramin->instance >> 12) |
  220. NV50_PUNK_BAR_CFG_BASE_VALID);
  221. nv_wr32(dev, NV50_PUNK_BAR1_CTXDMA, (priv->fb_bar->instance >> 4) |
  222. NV50_PUNK_BAR1_CTXDMA_VALID);
  223. nv_wr32(dev, NV50_PUNK_BAR3_CTXDMA, (priv->pramin_bar->instance >> 4) |
  224. NV50_PUNK_BAR3_CTXDMA_VALID);
  225. for (i = 0; i < 8; i++)
  226. nv_wr32(dev, 0x1900 + (i*4), 0);
  227. /* Assume that praying isn't enough, check that we can re-read the
  228. * entire fake channel back from the PRAMIN BAR */
  229. dev_priv->engine.instmem.prepare_access(dev, false);
  230. for (i = 0; i < c_size; i += 4) {
  231. if (nv_rd32(dev, NV_RAMIN + i) != nv_ri32(dev, i)) {
  232. NV_ERROR(dev, "Error reading back PRAMIN at 0x%08x\n",
  233. i);
  234. dev_priv->engine.instmem.finish_access(dev);
  235. return -EINVAL;
  236. }
  237. }
  238. dev_priv->engine.instmem.finish_access(dev);
  239. nv_wr32(dev, NV50_PUNK_BAR0_PRAMIN, save_nv001700);
  240. /* Global PRAMIN heap */
  241. if (nouveau_mem_init_heap(&dev_priv->ramin_heap,
  242. c_size, dev_priv->ramin_size - c_size)) {
  243. dev_priv->ramin_heap = NULL;
  244. NV_ERROR(dev, "Failed to init RAMIN heap\n");
  245. }
  246. /*XXX: incorrect, but needed to make hash func "work" */
  247. dev_priv->ramht_offset = 0x10000;
  248. dev_priv->ramht_bits = 9;
  249. dev_priv->ramht_size = (1 << dev_priv->ramht_bits);
  250. return 0;
  251. }
  252. void
  253. nv50_instmem_takedown(struct drm_device *dev)
  254. {
  255. struct drm_nouveau_private *dev_priv = dev->dev_private;
  256. struct nv50_instmem_priv *priv = dev_priv->engine.instmem.priv;
  257. struct nouveau_channel *chan = dev_priv->fifos[0];
  258. int i;
  259. NV_DEBUG(dev, "\n");
  260. if (!priv)
  261. return;
  262. /* Restore state from before init */
  263. for (i = 0x1700; i <= 0x1710; i += 4)
  264. nv_wr32(dev, i, priv->save1700[(i - 0x1700) / 4]);
  265. nouveau_gpuobj_ref_del(dev, &priv->fb_bar);
  266. nouveau_gpuobj_ref_del(dev, &priv->pramin_bar);
  267. nouveau_gpuobj_ref_del(dev, &priv->pramin_pt);
  268. /* Destroy dummy channel */
  269. if (chan) {
  270. for (i = 0; i < dev_priv->vm_vram_pt_nr; i++) {
  271. nouveau_gpuobj_ref_del(dev, &chan->vm_vram_pt[i]);
  272. dev_priv->vm_vram_pt[i] = NULL;
  273. }
  274. dev_priv->vm_vram_pt_nr = 0;
  275. nouveau_gpuobj_del(dev, &chan->vm_pd);
  276. nouveau_gpuobj_ref_del(dev, &chan->ramfc);
  277. nouveau_gpuobj_ref_del(dev, &chan->ramin);
  278. nouveau_mem_takedown(&chan->ramin_heap);
  279. dev_priv->fifos[0] = dev_priv->fifos[127] = NULL;
  280. kfree(chan);
  281. }
  282. dev_priv->engine.instmem.priv = NULL;
  283. kfree(priv);
  284. }
  285. int
  286. nv50_instmem_suspend(struct drm_device *dev)
  287. {
  288. struct drm_nouveau_private *dev_priv = dev->dev_private;
  289. struct nouveau_channel *chan = dev_priv->fifos[0];
  290. struct nouveau_gpuobj *ramin = chan->ramin->gpuobj;
  291. int i;
  292. ramin->im_backing_suspend = vmalloc(ramin->im_pramin->size);
  293. if (!ramin->im_backing_suspend)
  294. return -ENOMEM;
  295. for (i = 0; i < ramin->im_pramin->size; i += 4)
  296. ramin->im_backing_suspend[i/4] = nv_ri32(dev, i);
  297. return 0;
  298. }
  299. void
  300. nv50_instmem_resume(struct drm_device *dev)
  301. {
  302. struct drm_nouveau_private *dev_priv = dev->dev_private;
  303. struct nv50_instmem_priv *priv = dev_priv->engine.instmem.priv;
  304. struct nouveau_channel *chan = dev_priv->fifos[0];
  305. struct nouveau_gpuobj *ramin = chan->ramin->gpuobj;
  306. int i;
  307. nv_wr32(dev, NV50_PUNK_BAR0_PRAMIN, (ramin->im_backing_start >> 16));
  308. for (i = 0; i < ramin->im_pramin->size; i += 4)
  309. BAR0_WI32(ramin, i, ramin->im_backing_suspend[i/4]);
  310. vfree(ramin->im_backing_suspend);
  311. ramin->im_backing_suspend = NULL;
  312. /* Poke the relevant regs, and pray it works :) */
  313. nv_wr32(dev, NV50_PUNK_BAR_CFG_BASE, (chan->ramin->instance >> 12));
  314. nv_wr32(dev, NV50_PUNK_UNK1710, 0);
  315. nv_wr32(dev, NV50_PUNK_BAR_CFG_BASE, (chan->ramin->instance >> 12) |
  316. NV50_PUNK_BAR_CFG_BASE_VALID);
  317. nv_wr32(dev, NV50_PUNK_BAR1_CTXDMA, (priv->fb_bar->instance >> 4) |
  318. NV50_PUNK_BAR1_CTXDMA_VALID);
  319. nv_wr32(dev, NV50_PUNK_BAR3_CTXDMA, (priv->pramin_bar->instance >> 4) |
  320. NV50_PUNK_BAR3_CTXDMA_VALID);
  321. for (i = 0; i < 8; i++)
  322. nv_wr32(dev, 0x1900 + (i*4), 0);
  323. }
  324. int
  325. nv50_instmem_populate(struct drm_device *dev, struct nouveau_gpuobj *gpuobj,
  326. uint32_t *sz)
  327. {
  328. int ret;
  329. if (gpuobj->im_backing)
  330. return -EINVAL;
  331. *sz = ALIGN(*sz, NV50_INSTMEM_PAGE_SIZE);
  332. if (*sz == 0)
  333. return -EINVAL;
  334. ret = nouveau_bo_new(dev, NULL, *sz, 0, TTM_PL_FLAG_VRAM, 0, 0x0000,
  335. true, false, &gpuobj->im_backing);
  336. if (ret) {
  337. NV_ERROR(dev, "error getting PRAMIN backing pages: %d\n", ret);
  338. return ret;
  339. }
  340. ret = nouveau_bo_pin(gpuobj->im_backing, TTM_PL_FLAG_VRAM);
  341. if (ret) {
  342. NV_ERROR(dev, "error pinning PRAMIN backing VRAM: %d\n", ret);
  343. nouveau_bo_ref(NULL, &gpuobj->im_backing);
  344. return ret;
  345. }
  346. gpuobj->im_backing_start = gpuobj->im_backing->bo.mem.mm_node->start;
  347. gpuobj->im_backing_start <<= PAGE_SHIFT;
  348. return 0;
  349. }
  350. void
  351. nv50_instmem_clear(struct drm_device *dev, struct nouveau_gpuobj *gpuobj)
  352. {
  353. struct drm_nouveau_private *dev_priv = dev->dev_private;
  354. if (gpuobj && gpuobj->im_backing) {
  355. if (gpuobj->im_bound)
  356. dev_priv->engine.instmem.unbind(dev, gpuobj);
  357. nouveau_bo_unpin(gpuobj->im_backing);
  358. nouveau_bo_ref(NULL, &gpuobj->im_backing);
  359. gpuobj->im_backing = NULL;
  360. }
  361. }
  362. int
  363. nv50_instmem_bind(struct drm_device *dev, struct nouveau_gpuobj *gpuobj)
  364. {
  365. struct drm_nouveau_private *dev_priv = dev->dev_private;
  366. struct nv50_instmem_priv *priv = dev_priv->engine.instmem.priv;
  367. struct nouveau_gpuobj *pramin_pt = priv->pramin_pt->gpuobj;
  368. uint32_t pte, pte_end;
  369. uint64_t vram;
  370. if (!gpuobj->im_backing || !gpuobj->im_pramin || gpuobj->im_bound)
  371. return -EINVAL;
  372. NV_DEBUG(dev, "st=0x%0llx sz=0x%0llx\n",
  373. gpuobj->im_pramin->start, gpuobj->im_pramin->size);
  374. pte = (gpuobj->im_pramin->start >> 12) << 1;
  375. pte_end = ((gpuobj->im_pramin->size >> 12) << 1) + pte;
  376. vram = gpuobj->im_backing_start;
  377. NV_DEBUG(dev, "pramin=0x%llx, pte=%d, pte_end=%d\n",
  378. gpuobj->im_pramin->start, pte, pte_end);
  379. NV_DEBUG(dev, "first vram page: 0x%08x\n", gpuobj->im_backing_start);
  380. vram |= 1;
  381. if (dev_priv->vram_sys_base) {
  382. vram += dev_priv->vram_sys_base;
  383. vram |= 0x30;
  384. }
  385. dev_priv->engine.instmem.prepare_access(dev, true);
  386. while (pte < pte_end) {
  387. nv_wo32(dev, pramin_pt, pte++, lower_32_bits(vram));
  388. nv_wo32(dev, pramin_pt, pte++, upper_32_bits(vram));
  389. vram += NV50_INSTMEM_PAGE_SIZE;
  390. }
  391. dev_priv->engine.instmem.finish_access(dev);
  392. nv_wr32(dev, 0x100c80, 0x00040001);
  393. if (!nv_wait(0x100c80, 0x00000001, 0x00000000)) {
  394. NV_ERROR(dev, "timeout: (0x100c80 & 1) == 0 (1)\n");
  395. NV_ERROR(dev, "0x100c80 = 0x%08x\n", nv_rd32(dev, 0x100c80));
  396. return -EBUSY;
  397. }
  398. nv_wr32(dev, 0x100c80, 0x00060001);
  399. if (!nv_wait(0x100c80, 0x00000001, 0x00000000)) {
  400. NV_ERROR(dev, "timeout: (0x100c80 & 1) == 0 (2)\n");
  401. NV_ERROR(dev, "0x100c80 = 0x%08x\n", nv_rd32(dev, 0x100c80));
  402. return -EBUSY;
  403. }
  404. gpuobj->im_bound = 1;
  405. return 0;
  406. }
  407. int
  408. nv50_instmem_unbind(struct drm_device *dev, struct nouveau_gpuobj *gpuobj)
  409. {
  410. struct drm_nouveau_private *dev_priv = dev->dev_private;
  411. struct nv50_instmem_priv *priv = dev_priv->engine.instmem.priv;
  412. uint32_t pte, pte_end;
  413. if (gpuobj->im_bound == 0)
  414. return -EINVAL;
  415. pte = (gpuobj->im_pramin->start >> 12) << 1;
  416. pte_end = ((gpuobj->im_pramin->size >> 12) << 1) + pte;
  417. dev_priv->engine.instmem.prepare_access(dev, true);
  418. while (pte < pte_end) {
  419. nv_wo32(dev, priv->pramin_pt->gpuobj, pte++, 0x00000000);
  420. nv_wo32(dev, priv->pramin_pt->gpuobj, pte++, 0x00000000);
  421. }
  422. dev_priv->engine.instmem.finish_access(dev);
  423. gpuobj->im_bound = 0;
  424. return 0;
  425. }
  426. void
  427. nv50_instmem_prepare_access(struct drm_device *dev, bool write)
  428. {
  429. struct drm_nouveau_private *dev_priv = dev->dev_private;
  430. struct nv50_instmem_priv *priv = dev_priv->engine.instmem.priv;
  431. priv->last_access_wr = write;
  432. }
  433. void
  434. nv50_instmem_finish_access(struct drm_device *dev)
  435. {
  436. struct drm_nouveau_private *dev_priv = dev->dev_private;
  437. struct nv50_instmem_priv *priv = dev_priv->engine.instmem.priv;
  438. if (priv->last_access_wr) {
  439. nv_wr32(dev, 0x070000, 0x00000001);
  440. if (!nv_wait(0x070000, 0x00000001, 0x00000000))
  441. NV_ERROR(dev, "PRAMIN flush timeout\n");
  442. }
  443. }