nouveau_ramht.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 uint32_t
  28. nouveau_ramht_hash_handle(struct nouveau_channel *chan, uint32_t 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. uint32_t 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. uint32_t offset)
  49. {
  50. struct drm_nouveau_private *dev_priv = dev->dev_private;
  51. uint32_t 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. int
  57. nouveau_ramht_insert(struct nouveau_channel *chan, u32 handle,
  58. struct nouveau_gpuobj *gpuobj)
  59. {
  60. struct drm_device *dev = chan->dev;
  61. struct drm_nouveau_private *dev_priv = dev->dev_private;
  62. struct nouveau_instmem_engine *instmem = &dev_priv->engine.instmem;
  63. struct nouveau_ramht_entry *entry;
  64. struct nouveau_gpuobj *ramht = chan->ramht->gpuobj;
  65. uint32_t ctx, co, ho;
  66. if (nouveau_ramht_find(chan, handle))
  67. return -EEXIST;
  68. entry = kmalloc(sizeof(*entry), GFP_KERNEL);
  69. if (!entry)
  70. return -ENOMEM;
  71. entry->channel = chan;
  72. entry->gpuobj = NULL;
  73. entry->handle = handle;
  74. list_add(&entry->head, &chan->ramht->entries);
  75. nouveau_gpuobj_ref(gpuobj, &entry->gpuobj);
  76. if (dev_priv->card_type < NV_40) {
  77. ctx = NV_RAMHT_CONTEXT_VALID | (gpuobj->cinst >> 4) |
  78. (chan->id << NV_RAMHT_CONTEXT_CHANNEL_SHIFT) |
  79. (gpuobj->engine << NV_RAMHT_CONTEXT_ENGINE_SHIFT);
  80. } else
  81. if (dev_priv->card_type < NV_50) {
  82. ctx = (gpuobj->cinst >> 4) |
  83. (chan->id << NV40_RAMHT_CONTEXT_CHANNEL_SHIFT) |
  84. (gpuobj->engine << NV40_RAMHT_CONTEXT_ENGINE_SHIFT);
  85. } else {
  86. if (gpuobj->engine == NVOBJ_ENGINE_DISPLAY) {
  87. ctx = (gpuobj->cinst << 10) | 2;
  88. } else {
  89. ctx = (gpuobj->cinst >> 4) |
  90. ((gpuobj->engine <<
  91. NV40_RAMHT_CONTEXT_ENGINE_SHIFT));
  92. }
  93. }
  94. co = ho = nouveau_ramht_hash_handle(chan, handle);
  95. do {
  96. if (!nouveau_ramht_entry_valid(dev, ramht, co)) {
  97. NV_DEBUG(dev,
  98. "insert ch%d 0x%08x: h=0x%08x, c=0x%08x\n",
  99. chan->id, co, handle, ctx);
  100. nv_wo32(ramht, co + 0, handle);
  101. nv_wo32(ramht, co + 4, ctx);
  102. instmem->flush(dev);
  103. return 0;
  104. }
  105. NV_DEBUG(dev, "collision ch%d 0x%08x: h=0x%08x\n",
  106. chan->id, co, nv_ro32(ramht, co));
  107. co += 8;
  108. if (co >= ramht->size)
  109. co = 0;
  110. } while (co != ho);
  111. NV_ERROR(dev, "RAMHT space exhausted. ch=%d\n", chan->id);
  112. list_del(&entry->head);
  113. kfree(entry);
  114. return -ENOMEM;
  115. }
  116. void
  117. nouveau_ramht_remove(struct nouveau_channel *chan, u32 handle)
  118. {
  119. struct drm_device *dev = chan->dev;
  120. struct drm_nouveau_private *dev_priv = dev->dev_private;
  121. struct nouveau_instmem_engine *instmem = &dev_priv->engine.instmem;
  122. struct nouveau_gpuobj *ramht = chan->ramht->gpuobj;
  123. struct nouveau_ramht_entry *entry, *tmp;
  124. u32 co, ho;
  125. list_for_each_entry_safe(entry, tmp, &chan->ramht->entries, head) {
  126. if (entry->channel != chan || entry->handle != handle)
  127. continue;
  128. nouveau_gpuobj_ref(NULL, &entry->gpuobj);
  129. list_del(&entry->head);
  130. kfree(entry);
  131. break;
  132. }
  133. co = ho = nouveau_ramht_hash_handle(chan, handle);
  134. do {
  135. if (nouveau_ramht_entry_valid(dev, ramht, co) &&
  136. (handle == nv_ro32(ramht, co))) {
  137. NV_DEBUG(dev,
  138. "remove ch%d 0x%08x: h=0x%08x, c=0x%08x\n",
  139. chan->id, co, handle, nv_ro32(ramht, co + 4));
  140. nv_wo32(ramht, co + 0, 0x00000000);
  141. nv_wo32(ramht, co + 4, 0x00000000);
  142. instmem->flush(dev);
  143. return;
  144. }
  145. co += 8;
  146. if (co >= ramht->size)
  147. co = 0;
  148. } while (co != ho);
  149. NV_ERROR(dev, "RAMHT entry not found. ch=%d, handle=0x%08x\n",
  150. chan->id, handle);
  151. }
  152. struct nouveau_gpuobj *
  153. nouveau_ramht_find(struct nouveau_channel *chan, u32 handle)
  154. {
  155. struct nouveau_ramht_entry *entry;
  156. list_for_each_entry(entry, &chan->ramht->entries, head) {
  157. if (entry->channel == chan && entry->handle == handle)
  158. return entry->gpuobj;
  159. }
  160. return NULL;
  161. }
  162. int
  163. nouveau_ramht_new(struct drm_device *dev, struct nouveau_gpuobj *gpuobj,
  164. struct nouveau_ramht **pramht)
  165. {
  166. struct nouveau_ramht *ramht;
  167. ramht = kzalloc(sizeof(*ramht), GFP_KERNEL);
  168. if (!ramht)
  169. return -ENOMEM;
  170. ramht->dev = dev;
  171. ramht->refcount = 1;
  172. ramht->bits = drm_order(gpuobj->size / 8);
  173. INIT_LIST_HEAD(&ramht->entries);
  174. nouveau_gpuobj_ref(gpuobj, &ramht->gpuobj);
  175. *pramht = ramht;
  176. return 0;
  177. }
  178. void
  179. nouveau_ramht_ref(struct nouveau_ramht *ref, struct nouveau_ramht **ptr,
  180. struct nouveau_channel *chan)
  181. {
  182. struct nouveau_ramht_entry *entry, *tmp;
  183. struct nouveau_ramht *ramht;
  184. if (ref)
  185. ref->refcount++;
  186. ramht = *ptr;
  187. if (ramht) {
  188. list_for_each_entry_safe(entry, tmp, &ramht->entries, head) {
  189. if (entry->channel == chan)
  190. nouveau_ramht_remove(chan, entry->handle);
  191. }
  192. if (--ramht->refcount == 0) {
  193. nouveau_gpuobj_ref(NULL, &ramht->gpuobj);
  194. kfree(ramht);
  195. }
  196. }
  197. *ptr = ref;
  198. }