nv04_fifo.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. #ifdef __BIG_ENDIAN
  98. #define DMA_FETCH_ENDIANNESS NV_PFIFO_CACHE1_BIG_ENDIAN
  99. #else
  100. #define DMA_FETCH_ENDIANNESS 0
  101. #endif
  102. int
  103. nv04_fifo_create_context(struct nouveau_channel *chan)
  104. {
  105. struct drm_device *dev = chan->dev;
  106. struct drm_nouveau_private *dev_priv = dev->dev_private;
  107. unsigned long flags;
  108. int ret;
  109. ret = nouveau_gpuobj_new_fake(dev, NV04_RAMFC(chan->id), ~0,
  110. NV04_RAMFC__SIZE,
  111. NVOBJ_FLAG_ZERO_ALLOC |
  112. NVOBJ_FLAG_ZERO_FREE,
  113. NULL, &chan->ramfc);
  114. if (ret)
  115. return ret;
  116. spin_lock_irqsave(&dev_priv->context_switch_lock, flags);
  117. /* Setup initial state */
  118. dev_priv->engine.instmem.prepare_access(dev, true);
  119. RAMFC_WR(DMA_PUT, chan->pushbuf_base);
  120. RAMFC_WR(DMA_GET, chan->pushbuf_base);
  121. RAMFC_WR(DMA_INSTANCE, chan->pushbuf->instance >> 4);
  122. RAMFC_WR(DMA_FETCH, (NV_PFIFO_CACHE1_DMA_FETCH_TRIG_128_BYTES |
  123. NV_PFIFO_CACHE1_DMA_FETCH_SIZE_128_BYTES |
  124. NV_PFIFO_CACHE1_DMA_FETCH_MAX_REQS_8 |
  125. DMA_FETCH_ENDIANNESS));
  126. dev_priv->engine.instmem.finish_access(dev);
  127. /* enable the fifo dma operation */
  128. nv_wr32(dev, NV04_PFIFO_MODE,
  129. nv_rd32(dev, NV04_PFIFO_MODE) | (1 << chan->id));
  130. spin_unlock_irqrestore(&dev_priv->context_switch_lock, flags);
  131. return 0;
  132. }
  133. void
  134. nv04_fifo_destroy_context(struct nouveau_channel *chan)
  135. {
  136. struct drm_device *dev = chan->dev;
  137. nv_wr32(dev, NV04_PFIFO_MODE,
  138. nv_rd32(dev, NV04_PFIFO_MODE) & ~(1 << chan->id));
  139. nouveau_gpuobj_ref_del(dev, &chan->ramfc);
  140. }
  141. static void
  142. nv04_fifo_do_load_context(struct drm_device *dev, int chid)
  143. {
  144. struct drm_nouveau_private *dev_priv = dev->dev_private;
  145. uint32_t fc = NV04_RAMFC(chid), tmp;
  146. dev_priv->engine.instmem.prepare_access(dev, false);
  147. nv_wr32(dev, NV04_PFIFO_CACHE1_DMA_PUT, nv_ri32(dev, fc + 0));
  148. nv_wr32(dev, NV04_PFIFO_CACHE1_DMA_GET, nv_ri32(dev, fc + 4));
  149. tmp = nv_ri32(dev, fc + 8);
  150. nv_wr32(dev, NV04_PFIFO_CACHE1_DMA_INSTANCE, tmp & 0xFFFF);
  151. nv_wr32(dev, NV04_PFIFO_CACHE1_DMA_DCOUNT, tmp >> 16);
  152. nv_wr32(dev, NV04_PFIFO_CACHE1_DMA_STATE, nv_ri32(dev, fc + 12));
  153. nv_wr32(dev, NV04_PFIFO_CACHE1_DMA_FETCH, nv_ri32(dev, fc + 16));
  154. nv_wr32(dev, NV04_PFIFO_CACHE1_ENGINE, nv_ri32(dev, fc + 20));
  155. nv_wr32(dev, NV04_PFIFO_CACHE1_PULL1, nv_ri32(dev, fc + 24));
  156. dev_priv->engine.instmem.finish_access(dev);
  157. nv_wr32(dev, NV03_PFIFO_CACHE1_GET, 0);
  158. nv_wr32(dev, NV03_PFIFO_CACHE1_PUT, 0);
  159. }
  160. int
  161. nv04_fifo_load_context(struct nouveau_channel *chan)
  162. {
  163. uint32_t tmp;
  164. nv_wr32(chan->dev, NV03_PFIFO_CACHE1_PUSH1,
  165. NV03_PFIFO_CACHE1_PUSH1_DMA | chan->id);
  166. nv04_fifo_do_load_context(chan->dev, chan->id);
  167. nv_wr32(chan->dev, NV04_PFIFO_CACHE1_DMA_PUSH, 1);
  168. /* Reset NV04_PFIFO_CACHE1_DMA_CTL_AT_INFO to INVALID */
  169. tmp = nv_rd32(chan->dev, NV04_PFIFO_CACHE1_DMA_CTL) & ~(1 << 31);
  170. nv_wr32(chan->dev, NV04_PFIFO_CACHE1_DMA_CTL, tmp);
  171. return 0;
  172. }
  173. int
  174. nv04_fifo_unload_context(struct drm_device *dev)
  175. {
  176. struct drm_nouveau_private *dev_priv = dev->dev_private;
  177. struct nouveau_fifo_engine *pfifo = &dev_priv->engine.fifo;
  178. struct nouveau_channel *chan = NULL;
  179. uint32_t tmp;
  180. int chid;
  181. chid = pfifo->channel_id(dev);
  182. if (chid < 0 || chid >= dev_priv->engine.fifo.channels)
  183. return 0;
  184. chan = dev_priv->fifos[chid];
  185. if (!chan) {
  186. NV_ERROR(dev, "Inactive channel on PFIFO: %d\n", chid);
  187. return -EINVAL;
  188. }
  189. dev_priv->engine.instmem.prepare_access(dev, true);
  190. RAMFC_WR(DMA_PUT, nv_rd32(dev, NV04_PFIFO_CACHE1_DMA_PUT));
  191. RAMFC_WR(DMA_GET, nv_rd32(dev, NV04_PFIFO_CACHE1_DMA_GET));
  192. tmp = nv_rd32(dev, NV04_PFIFO_CACHE1_DMA_DCOUNT) << 16;
  193. tmp |= nv_rd32(dev, NV04_PFIFO_CACHE1_DMA_INSTANCE);
  194. RAMFC_WR(DMA_INSTANCE, tmp);
  195. RAMFC_WR(DMA_STATE, nv_rd32(dev, NV04_PFIFO_CACHE1_DMA_STATE));
  196. RAMFC_WR(DMA_FETCH, nv_rd32(dev, NV04_PFIFO_CACHE1_DMA_FETCH));
  197. RAMFC_WR(ENGINE, nv_rd32(dev, NV04_PFIFO_CACHE1_ENGINE));
  198. RAMFC_WR(PULL1_ENGINE, nv_rd32(dev, NV04_PFIFO_CACHE1_PULL1));
  199. dev_priv->engine.instmem.finish_access(dev);
  200. nv04_fifo_do_load_context(dev, pfifo->channels - 1);
  201. nv_wr32(dev, NV03_PFIFO_CACHE1_PUSH1, pfifo->channels - 1);
  202. return 0;
  203. }
  204. static void
  205. nv04_fifo_init_reset(struct drm_device *dev)
  206. {
  207. nv_wr32(dev, NV03_PMC_ENABLE,
  208. nv_rd32(dev, NV03_PMC_ENABLE) & ~NV_PMC_ENABLE_PFIFO);
  209. nv_wr32(dev, NV03_PMC_ENABLE,
  210. nv_rd32(dev, NV03_PMC_ENABLE) | NV_PMC_ENABLE_PFIFO);
  211. nv_wr32(dev, 0x003224, 0x000f0078);
  212. nv_wr32(dev, 0x002044, 0x0101ffff);
  213. nv_wr32(dev, 0x002040, 0x000000ff);
  214. nv_wr32(dev, 0x002500, 0x00000000);
  215. nv_wr32(dev, 0x003000, 0x00000000);
  216. nv_wr32(dev, 0x003050, 0x00000000);
  217. nv_wr32(dev, 0x003200, 0x00000000);
  218. nv_wr32(dev, 0x003250, 0x00000000);
  219. nv_wr32(dev, 0x003220, 0x00000000);
  220. nv_wr32(dev, 0x003250, 0x00000000);
  221. nv_wr32(dev, 0x003270, 0x00000000);
  222. nv_wr32(dev, 0x003210, 0x00000000);
  223. }
  224. static void
  225. nv04_fifo_init_ramxx(struct drm_device *dev)
  226. {
  227. struct drm_nouveau_private *dev_priv = dev->dev_private;
  228. nv_wr32(dev, NV03_PFIFO_RAMHT, (0x03 << 24) /* search 128 */ |
  229. ((dev_priv->ramht_bits - 9) << 16) |
  230. (dev_priv->ramht_offset >> 8));
  231. nv_wr32(dev, NV03_PFIFO_RAMRO, dev_priv->ramro_offset>>8);
  232. nv_wr32(dev, NV03_PFIFO_RAMFC, dev_priv->ramfc_offset >> 8);
  233. }
  234. static void
  235. nv04_fifo_init_intr(struct drm_device *dev)
  236. {
  237. nv_wr32(dev, 0x002100, 0xffffffff);
  238. nv_wr32(dev, 0x002140, 0xffffffff);
  239. }
  240. int
  241. nv04_fifo_init(struct drm_device *dev)
  242. {
  243. struct drm_nouveau_private *dev_priv = dev->dev_private;
  244. struct nouveau_fifo_engine *pfifo = &dev_priv->engine.fifo;
  245. int i;
  246. nv04_fifo_init_reset(dev);
  247. nv04_fifo_init_ramxx(dev);
  248. nv04_fifo_do_load_context(dev, pfifo->channels - 1);
  249. nv_wr32(dev, NV03_PFIFO_CACHE1_PUSH1, pfifo->channels - 1);
  250. nv04_fifo_init_intr(dev);
  251. pfifo->enable(dev);
  252. for (i = 0; i < dev_priv->engine.fifo.channels; i++) {
  253. if (dev_priv->fifos[i]) {
  254. uint32_t mode = nv_rd32(dev, NV04_PFIFO_MODE);
  255. nv_wr32(dev, NV04_PFIFO_MODE, mode | (1 << i));
  256. }
  257. }
  258. return 0;
  259. }