nv04_fifo.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /*
  2. * Copyright (C) 2007 Ben Skeggs.
  3. * All Rights Reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining
  6. * a copy of this software and associated documentation files (the
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sublicense, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * The above copyright notice and this permission notice (including the
  14. * next paragraph) shall be included in all copies or substantial
  15. * portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  20. * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
  21. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  22. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  23. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. *
  25. */
  26. #include "drmP.h"
  27. #include "drm.h"
  28. #include "nouveau_drv.h"
  29. #define NV04_RAMFC(c) (dev_priv->ramfc_offset + ((c) * NV04_RAMFC__SIZE))
  30. #define NV04_RAMFC__SIZE 32
  31. #define NV04_RAMFC_DMA_PUT 0x00
  32. #define NV04_RAMFC_DMA_GET 0x04
  33. #define NV04_RAMFC_DMA_INSTANCE 0x08
  34. #define NV04_RAMFC_DMA_STATE 0x0C
  35. #define NV04_RAMFC_DMA_FETCH 0x10
  36. #define NV04_RAMFC_ENGINE 0x14
  37. #define NV04_RAMFC_PULL1_ENGINE 0x18
  38. #define RAMFC_WR(offset, val) nv_wo32(dev, chan->ramfc->gpuobj, \
  39. NV04_RAMFC_##offset/4, (val))
  40. #define RAMFC_RD(offset) nv_ro32(dev, chan->ramfc->gpuobj, \
  41. NV04_RAMFC_##offset/4)
  42. void
  43. nv04_fifo_disable(struct drm_device *dev)
  44. {
  45. uint32_t tmp;
  46. tmp = nv_rd32(dev, NV04_PFIFO_CACHE1_DMA_PUSH);
  47. nv_wr32(dev, NV04_PFIFO_CACHE1_DMA_PUSH, tmp & ~1);
  48. nv_wr32(dev, NV03_PFIFO_CACHE1_PUSH0, 0);
  49. tmp = nv_rd32(dev, NV03_PFIFO_CACHE1_PULL1);
  50. nv_wr32(dev, NV04_PFIFO_CACHE1_PULL0, tmp & ~1);
  51. }
  52. void
  53. nv04_fifo_enable(struct drm_device *dev)
  54. {
  55. nv_wr32(dev, NV03_PFIFO_CACHE1_PUSH0, 1);
  56. nv_wr32(dev, NV04_PFIFO_CACHE1_PULL0, 1);
  57. }
  58. bool
  59. nv04_fifo_reassign(struct drm_device *dev, bool enable)
  60. {
  61. uint32_t reassign = nv_rd32(dev, NV03_PFIFO_CACHES);
  62. nv_wr32(dev, NV03_PFIFO_CACHES, enable ? 1 : 0);
  63. return (reassign == 1);
  64. }
  65. bool
  66. nv04_fifo_cache_flush(struct drm_device *dev)
  67. {
  68. struct drm_nouveau_private *dev_priv = dev->dev_private;
  69. struct nouveau_timer_engine *ptimer = &dev_priv->engine.timer;
  70. uint64_t start = ptimer->read(dev);
  71. do {
  72. if (nv_rd32(dev, NV03_PFIFO_CACHE1_GET) ==
  73. nv_rd32(dev, NV03_PFIFO_CACHE1_PUT))
  74. return true;
  75. } while (ptimer->read(dev) - start < 100000000);
  76. NV_ERROR(dev, "Timeout flushing the PFIFO cache.\n");
  77. return false;
  78. }
  79. bool
  80. nv04_fifo_cache_pull(struct drm_device *dev, bool enable)
  81. {
  82. uint32_t pull = nv_rd32(dev, NV04_PFIFO_CACHE1_PULL0);
  83. if (enable) {
  84. nv_wr32(dev, NV04_PFIFO_CACHE1_PULL0, pull | 1);
  85. } else {
  86. nv_wr32(dev, NV04_PFIFO_CACHE1_PULL0, pull & ~1);
  87. nv_wr32(dev, NV04_PFIFO_CACHE1_HASH, 0);
  88. }
  89. return !!(pull & 1);
  90. }
  91. int
  92. nv04_fifo_channel_id(struct drm_device *dev)
  93. {
  94. return nv_rd32(dev, NV03_PFIFO_CACHE1_PUSH1) &
  95. NV03_PFIFO_CACHE1_PUSH1_CHID_MASK;
  96. }
  97. int
  98. nv04_fifo_create_context(struct nouveau_channel *chan)
  99. {
  100. struct drm_device *dev = chan->dev;
  101. struct drm_nouveau_private *dev_priv = dev->dev_private;
  102. unsigned long flags;
  103. int ret;
  104. ret = nouveau_gpuobj_new_fake(dev, NV04_RAMFC(chan->id), ~0,
  105. NV04_RAMFC__SIZE,
  106. NVOBJ_FLAG_ZERO_ALLOC |
  107. NVOBJ_FLAG_ZERO_FREE,
  108. NULL, &chan->ramfc);
  109. if (ret)
  110. return ret;
  111. spin_lock_irqsave(&dev_priv->context_switch_lock, flags);
  112. /* Setup initial state */
  113. dev_priv->engine.instmem.prepare_access(dev, true);
  114. RAMFC_WR(DMA_PUT, chan->pushbuf_base);
  115. RAMFC_WR(DMA_GET, chan->pushbuf_base);
  116. RAMFC_WR(DMA_INSTANCE, chan->pushbuf->instance >> 4);
  117. RAMFC_WR(DMA_FETCH, (NV_PFIFO_CACHE1_DMA_FETCH_TRIG_128_BYTES |
  118. NV_PFIFO_CACHE1_DMA_FETCH_SIZE_128_BYTES |
  119. NV_PFIFO_CACHE1_DMA_FETCH_MAX_REQS_8 |
  120. #ifdef __BIG_ENDIAN
  121. NV_PFIFO_CACHE1_BIG_ENDIAN |
  122. #endif
  123. 0));
  124. dev_priv->engine.instmem.finish_access(dev);
  125. /* enable the fifo dma operation */
  126. nv_wr32(dev, NV04_PFIFO_MODE,
  127. nv_rd32(dev, NV04_PFIFO_MODE) | (1 << chan->id));
  128. spin_unlock_irqrestore(&dev_priv->context_switch_lock, flags);
  129. return 0;
  130. }
  131. void
  132. nv04_fifo_destroy_context(struct nouveau_channel *chan)
  133. {
  134. struct drm_device *dev = chan->dev;
  135. nv_wr32(dev, NV04_PFIFO_MODE,
  136. nv_rd32(dev, NV04_PFIFO_MODE) & ~(1 << chan->id));
  137. nouveau_gpuobj_ref_del(dev, &chan->ramfc);
  138. }
  139. static void
  140. nv04_fifo_do_load_context(struct drm_device *dev, int chid)
  141. {
  142. struct drm_nouveau_private *dev_priv = dev->dev_private;
  143. uint32_t fc = NV04_RAMFC(chid), tmp;
  144. dev_priv->engine.instmem.prepare_access(dev, false);
  145. nv_wr32(dev, NV04_PFIFO_CACHE1_DMA_PUT, nv_ri32(dev, fc + 0));
  146. nv_wr32(dev, NV04_PFIFO_CACHE1_DMA_GET, nv_ri32(dev, fc + 4));
  147. tmp = nv_ri32(dev, fc + 8);
  148. nv_wr32(dev, NV04_PFIFO_CACHE1_DMA_INSTANCE, tmp & 0xFFFF);
  149. nv_wr32(dev, NV04_PFIFO_CACHE1_DMA_DCOUNT, tmp >> 16);
  150. nv_wr32(dev, NV04_PFIFO_CACHE1_DMA_STATE, nv_ri32(dev, fc + 12));
  151. nv_wr32(dev, NV04_PFIFO_CACHE1_DMA_FETCH, nv_ri32(dev, fc + 16));
  152. nv_wr32(dev, NV04_PFIFO_CACHE1_ENGINE, nv_ri32(dev, fc + 20));
  153. nv_wr32(dev, NV04_PFIFO_CACHE1_PULL1, nv_ri32(dev, fc + 24));
  154. dev_priv->engine.instmem.finish_access(dev);
  155. nv_wr32(dev, NV03_PFIFO_CACHE1_GET, 0);
  156. nv_wr32(dev, NV03_PFIFO_CACHE1_PUT, 0);
  157. }
  158. int
  159. nv04_fifo_load_context(struct nouveau_channel *chan)
  160. {
  161. uint32_t tmp;
  162. nv_wr32(chan->dev, NV03_PFIFO_CACHE1_PUSH1,
  163. NV03_PFIFO_CACHE1_PUSH1_DMA | chan->id);
  164. nv04_fifo_do_load_context(chan->dev, chan->id);
  165. nv_wr32(chan->dev, NV04_PFIFO_CACHE1_DMA_PUSH, 1);
  166. /* Reset NV04_PFIFO_CACHE1_DMA_CTL_AT_INFO to INVALID */
  167. tmp = nv_rd32(chan->dev, NV04_PFIFO_CACHE1_DMA_CTL) & ~(1 << 31);
  168. nv_wr32(chan->dev, NV04_PFIFO_CACHE1_DMA_CTL, tmp);
  169. return 0;
  170. }
  171. int
  172. nv04_fifo_unload_context(struct drm_device *dev)
  173. {
  174. struct drm_nouveau_private *dev_priv = dev->dev_private;
  175. struct nouveau_fifo_engine *pfifo = &dev_priv->engine.fifo;
  176. struct nouveau_channel *chan = NULL;
  177. uint32_t tmp;
  178. int chid;
  179. chid = pfifo->channel_id(dev);
  180. if (chid < 0 || chid >= dev_priv->engine.fifo.channels)
  181. return 0;
  182. chan = dev_priv->fifos[chid];
  183. if (!chan) {
  184. NV_ERROR(dev, "Inactive channel on PFIFO: %d\n", chid);
  185. return -EINVAL;
  186. }
  187. dev_priv->engine.instmem.prepare_access(dev, true);
  188. RAMFC_WR(DMA_PUT, nv_rd32(dev, NV04_PFIFO_CACHE1_DMA_PUT));
  189. RAMFC_WR(DMA_GET, nv_rd32(dev, NV04_PFIFO_CACHE1_DMA_GET));
  190. tmp = nv_rd32(dev, NV04_PFIFO_CACHE1_DMA_DCOUNT) << 16;
  191. tmp |= nv_rd32(dev, NV04_PFIFO_CACHE1_DMA_INSTANCE);
  192. RAMFC_WR(DMA_INSTANCE, tmp);
  193. RAMFC_WR(DMA_STATE, nv_rd32(dev, NV04_PFIFO_CACHE1_DMA_STATE));
  194. RAMFC_WR(DMA_FETCH, nv_rd32(dev, NV04_PFIFO_CACHE1_DMA_FETCH));
  195. RAMFC_WR(ENGINE, nv_rd32(dev, NV04_PFIFO_CACHE1_ENGINE));
  196. RAMFC_WR(PULL1_ENGINE, nv_rd32(dev, NV04_PFIFO_CACHE1_PULL1));
  197. dev_priv->engine.instmem.finish_access(dev);
  198. nv04_fifo_do_load_context(dev, pfifo->channels - 1);
  199. nv_wr32(dev, NV03_PFIFO_CACHE1_PUSH1, pfifo->channels - 1);
  200. return 0;
  201. }
  202. static void
  203. nv04_fifo_init_reset(struct drm_device *dev)
  204. {
  205. nv_wr32(dev, NV03_PMC_ENABLE,
  206. nv_rd32(dev, NV03_PMC_ENABLE) & ~NV_PMC_ENABLE_PFIFO);
  207. nv_wr32(dev, NV03_PMC_ENABLE,
  208. nv_rd32(dev, NV03_PMC_ENABLE) | NV_PMC_ENABLE_PFIFO);
  209. nv_wr32(dev, 0x003224, 0x000f0078);
  210. nv_wr32(dev, 0x002044, 0x0101ffff);
  211. nv_wr32(dev, 0x002040, 0x000000ff);
  212. nv_wr32(dev, 0x002500, 0x00000000);
  213. nv_wr32(dev, 0x003000, 0x00000000);
  214. nv_wr32(dev, 0x003050, 0x00000000);
  215. nv_wr32(dev, 0x003200, 0x00000000);
  216. nv_wr32(dev, 0x003250, 0x00000000);
  217. nv_wr32(dev, 0x003220, 0x00000000);
  218. nv_wr32(dev, 0x003250, 0x00000000);
  219. nv_wr32(dev, 0x003270, 0x00000000);
  220. nv_wr32(dev, 0x003210, 0x00000000);
  221. }
  222. static void
  223. nv04_fifo_init_ramxx(struct drm_device *dev)
  224. {
  225. struct drm_nouveau_private *dev_priv = dev->dev_private;
  226. nv_wr32(dev, NV03_PFIFO_RAMHT, (0x03 << 24) /* search 128 */ |
  227. ((dev_priv->ramht_bits - 9) << 16) |
  228. (dev_priv->ramht_offset >> 8));
  229. nv_wr32(dev, NV03_PFIFO_RAMRO, dev_priv->ramro_offset>>8);
  230. nv_wr32(dev, NV03_PFIFO_RAMFC, dev_priv->ramfc_offset >> 8);
  231. }
  232. static void
  233. nv04_fifo_init_intr(struct drm_device *dev)
  234. {
  235. nv_wr32(dev, 0x002100, 0xffffffff);
  236. nv_wr32(dev, 0x002140, 0xffffffff);
  237. }
  238. int
  239. nv04_fifo_init(struct drm_device *dev)
  240. {
  241. struct drm_nouveau_private *dev_priv = dev->dev_private;
  242. struct nouveau_fifo_engine *pfifo = &dev_priv->engine.fifo;
  243. int i;
  244. nv04_fifo_init_reset(dev);
  245. nv04_fifo_init_ramxx(dev);
  246. nv04_fifo_do_load_context(dev, pfifo->channels - 1);
  247. nv_wr32(dev, NV03_PFIFO_CACHE1_PUSH1, pfifo->channels - 1);
  248. nv04_fifo_init_intr(dev);
  249. pfifo->enable(dev);
  250. for (i = 0; i < dev_priv->engine.fifo.channels; i++) {
  251. if (dev_priv->fifos[i]) {
  252. uint32_t mode = nv_rd32(dev, NV04_PFIFO_MODE);
  253. nv_wr32(dev, NV04_PFIFO_MODE, mode | (1 << i));
  254. }
  255. }
  256. return 0;
  257. }