nouveau_ramht.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /*
  2. * Copyright 2010 Red Hat Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * Authors: Ben Skeggs
  23. */
  24. #include "drmP.h"
  25. #include "nouveau_drv.h"
  26. #include "nouveau_ramht.h"
  27. static u32
  28. nouveau_ramht_hash_handle(struct nouveau_channel *chan, u32 handle)
  29. {
  30. struct drm_device *dev = chan->dev;
  31. struct drm_nouveau_private *dev_priv = dev->dev_private;
  32. struct nouveau_ramht *ramht = chan->ramht;
  33. u32 hash = 0;
  34. int i;
  35. NV_DEBUG(dev, "ch%d handle=0x%08x\n", chan->id, handle);
  36. for (i = 32; i > 0; i -= ramht->bits) {
  37. hash ^= (handle & ((1 << ramht->bits) - 1));
  38. handle >>= ramht->bits;
  39. }
  40. if (dev_priv->card_type < NV_50)
  41. hash ^= chan->id << (ramht->bits - 4);
  42. hash <<= 3;
  43. NV_DEBUG(dev, "hash=0x%08x\n", hash);
  44. return hash;
  45. }
  46. static int
  47. nouveau_ramht_entry_valid(struct drm_device *dev, struct nouveau_gpuobj *ramht,
  48. u32 offset)
  49. {
  50. struct drm_nouveau_private *dev_priv = dev->dev_private;
  51. u32 ctx = nv_ro32(ramht, offset + 4);
  52. if (dev_priv->card_type < NV_40)
  53. return ((ctx & NV_RAMHT_CONTEXT_VALID) != 0);
  54. return (ctx != 0);
  55. }
  56. static int
  57. nouveau_ramht_entry_same_channel(struct nouveau_channel *chan,
  58. struct nouveau_gpuobj *ramht, u32 offset)
  59. {
  60. struct drm_nouveau_private *dev_priv = chan->dev->dev_private;
  61. u32 ctx = nv_ro32(ramht, offset + 4);
  62. if (dev_priv->card_type >= NV_50)
  63. return true;
  64. else if (dev_priv->card_type >= NV_40)
  65. return chan->id ==
  66. ((ctx >> NV40_RAMHT_CONTEXT_CHANNEL_SHIFT) & 0x1f);
  67. else
  68. return chan->id ==
  69. ((ctx >> NV_RAMHT_CONTEXT_CHANNEL_SHIFT) & 0x1f);
  70. }
  71. int
  72. nouveau_ramht_insert(struct nouveau_channel *chan, u32 handle,
  73. struct nouveau_gpuobj *gpuobj)
  74. {
  75. struct drm_device *dev = chan->dev;
  76. struct drm_nouveau_private *dev_priv = dev->dev_private;
  77. struct nouveau_instmem_engine *instmem = &dev_priv->engine.instmem;
  78. struct nouveau_ramht_entry *entry;
  79. struct nouveau_gpuobj *ramht = chan->ramht->gpuobj;
  80. unsigned long flags;
  81. u32 ctx, co, ho;
  82. if (nouveau_ramht_find(chan, handle))
  83. return -EEXIST;
  84. entry = kmalloc(sizeof(*entry), GFP_KERNEL);
  85. if (!entry)
  86. return -ENOMEM;
  87. entry->channel = chan;
  88. entry->gpuobj = NULL;
  89. entry->handle = handle;
  90. nouveau_gpuobj_ref(gpuobj, &entry->gpuobj);
  91. if (dev_priv->card_type < NV_40) {
  92. ctx = NV_RAMHT_CONTEXT_VALID | (gpuobj->pinst >> 4) |
  93. (chan->id << NV_RAMHT_CONTEXT_CHANNEL_SHIFT) |
  94. (gpuobj->engine << NV_RAMHT_CONTEXT_ENGINE_SHIFT);
  95. } else
  96. if (dev_priv->card_type < NV_50) {
  97. ctx = (gpuobj->pinst >> 4) |
  98. (chan->id << NV40_RAMHT_CONTEXT_CHANNEL_SHIFT) |
  99. (gpuobj->engine << NV40_RAMHT_CONTEXT_ENGINE_SHIFT);
  100. } else {
  101. if (gpuobj->engine == NVOBJ_ENGINE_DISPLAY) {
  102. ctx = (gpuobj->cinst << 10) | chan->id;
  103. } else {
  104. ctx = (gpuobj->cinst >> 4) |
  105. ((gpuobj->engine <<
  106. NV40_RAMHT_CONTEXT_ENGINE_SHIFT));
  107. }
  108. }
  109. spin_lock_irqsave(&chan->ramht->lock, flags);
  110. list_add(&entry->head, &chan->ramht->entries);
  111. co = ho = nouveau_ramht_hash_handle(chan, handle);
  112. do {
  113. if (!nouveau_ramht_entry_valid(dev, ramht, co)) {
  114. NV_DEBUG(dev,
  115. "insert ch%d 0x%08x: h=0x%08x, c=0x%08x\n",
  116. chan->id, co, handle, ctx);
  117. nv_wo32(ramht, co + 0, handle);
  118. nv_wo32(ramht, co + 4, ctx);
  119. spin_unlock_irqrestore(&chan->ramht->lock, flags);
  120. instmem->flush(dev);
  121. return 0;
  122. }
  123. NV_DEBUG(dev, "collision ch%d 0x%08x: h=0x%08x\n",
  124. chan->id, co, nv_ro32(ramht, co));
  125. co += 8;
  126. if (co >= ramht->size)
  127. co = 0;
  128. } while (co != ho);
  129. NV_ERROR(dev, "RAMHT space exhausted. ch=%d\n", chan->id);
  130. list_del(&entry->head);
  131. spin_unlock_irqrestore(&chan->ramht->lock, flags);
  132. kfree(entry);
  133. return -ENOMEM;
  134. }
  135. static struct nouveau_ramht_entry *
  136. nouveau_ramht_remove_entry(struct nouveau_channel *chan, u32 handle)
  137. {
  138. struct nouveau_ramht *ramht = chan ? chan->ramht : NULL;
  139. struct nouveau_ramht_entry *entry;
  140. unsigned long flags;
  141. if (!ramht)
  142. return NULL;
  143. spin_lock_irqsave(&ramht->lock, flags);
  144. list_for_each_entry(entry, &ramht->entries, head) {
  145. if (entry->channel == chan &&
  146. (!handle || entry->handle == handle)) {
  147. list_del(&entry->head);
  148. spin_unlock_irqrestore(&ramht->lock, flags);
  149. return entry;
  150. }
  151. }
  152. spin_unlock_irqrestore(&ramht->lock, flags);
  153. return NULL;
  154. }
  155. static void
  156. nouveau_ramht_remove_hash(struct nouveau_channel *chan, u32 handle)
  157. {
  158. struct drm_device *dev = chan->dev;
  159. struct drm_nouveau_private *dev_priv = dev->dev_private;
  160. struct nouveau_instmem_engine *instmem = &dev_priv->engine.instmem;
  161. struct nouveau_gpuobj *ramht = chan->ramht->gpuobj;
  162. unsigned long flags;
  163. u32 co, ho;
  164. spin_lock_irqsave(&chan->ramht->lock, flags);
  165. co = ho = nouveau_ramht_hash_handle(chan, handle);
  166. do {
  167. if (nouveau_ramht_entry_valid(dev, ramht, co) &&
  168. nouveau_ramht_entry_same_channel(chan, ramht, co) &&
  169. (handle == nv_ro32(ramht, co))) {
  170. NV_DEBUG(dev,
  171. "remove ch%d 0x%08x: h=0x%08x, c=0x%08x\n",
  172. chan->id, co, handle, nv_ro32(ramht, co + 4));
  173. nv_wo32(ramht, co + 0, 0x00000000);
  174. nv_wo32(ramht, co + 4, 0x00000000);
  175. instmem->flush(dev);
  176. goto out;
  177. }
  178. co += 8;
  179. if (co >= ramht->size)
  180. co = 0;
  181. } while (co != ho);
  182. NV_ERROR(dev, "RAMHT entry not found. ch=%d, handle=0x%08x\n",
  183. chan->id, handle);
  184. out:
  185. spin_unlock_irqrestore(&chan->ramht->lock, flags);
  186. }
  187. int
  188. nouveau_ramht_remove(struct nouveau_channel *chan, u32 handle)
  189. {
  190. struct nouveau_ramht_entry *entry;
  191. entry = nouveau_ramht_remove_entry(chan, handle);
  192. if (!entry)
  193. return -ENOENT;
  194. nouveau_ramht_remove_hash(chan, entry->handle);
  195. nouveau_gpuobj_ref(NULL, &entry->gpuobj);
  196. kfree(entry);
  197. return 0;
  198. }
  199. struct nouveau_gpuobj *
  200. nouveau_ramht_find(struct nouveau_channel *chan, u32 handle)
  201. {
  202. struct nouveau_ramht *ramht = chan->ramht;
  203. struct nouveau_ramht_entry *entry;
  204. struct nouveau_gpuobj *gpuobj = NULL;
  205. unsigned long flags;
  206. if (unlikely(!chan->ramht))
  207. return NULL;
  208. spin_lock_irqsave(&ramht->lock, flags);
  209. list_for_each_entry(entry, &chan->ramht->entries, head) {
  210. if (entry->channel == chan && entry->handle == handle) {
  211. gpuobj = entry->gpuobj;
  212. break;
  213. }
  214. }
  215. spin_unlock_irqrestore(&ramht->lock, flags);
  216. return gpuobj;
  217. }
  218. int
  219. nouveau_ramht_new(struct drm_device *dev, struct nouveau_gpuobj *gpuobj,
  220. struct nouveau_ramht **pramht)
  221. {
  222. struct nouveau_ramht *ramht;
  223. ramht = kzalloc(sizeof(*ramht), GFP_KERNEL);
  224. if (!ramht)
  225. return -ENOMEM;
  226. ramht->dev = dev;
  227. kref_init(&ramht->refcount);
  228. ramht->bits = drm_order(gpuobj->size / 8);
  229. INIT_LIST_HEAD(&ramht->entries);
  230. spin_lock_init(&ramht->lock);
  231. nouveau_gpuobj_ref(gpuobj, &ramht->gpuobj);
  232. *pramht = ramht;
  233. return 0;
  234. }
  235. static void
  236. nouveau_ramht_del(struct kref *ref)
  237. {
  238. struct nouveau_ramht *ramht =
  239. container_of(ref, struct nouveau_ramht, refcount);
  240. nouveau_gpuobj_ref(NULL, &ramht->gpuobj);
  241. kfree(ramht);
  242. }
  243. void
  244. nouveau_ramht_ref(struct nouveau_ramht *ref, struct nouveau_ramht **ptr,
  245. struct nouveau_channel *chan)
  246. {
  247. struct nouveau_ramht_entry *entry;
  248. struct nouveau_ramht *ramht;
  249. if (ref)
  250. kref_get(&ref->refcount);
  251. ramht = *ptr;
  252. if (ramht) {
  253. while ((entry = nouveau_ramht_remove_entry(chan, 0))) {
  254. nouveau_ramht_remove_hash(chan, entry->handle);
  255. nouveau_gpuobj_ref(NULL, &entry->gpuobj);
  256. kfree(entry);
  257. }
  258. kref_put(&ramht->refcount, nouveau_ramht_del);
  259. }
  260. *ptr = ref;
  261. }