ramht.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * Copyright 2012 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. #include <core/ramht.h>
  23. #include <core/engine.h>
  24. #include <core/object.h>
  25. static u32
  26. nvkm_ramht_hash(struct nvkm_ramht *ramht, int chid, u32 handle)
  27. {
  28. u32 hash = 0;
  29. while (handle) {
  30. hash ^= (handle & ((1 << ramht->bits) - 1));
  31. handle >>= ramht->bits;
  32. }
  33. hash ^= chid << (ramht->bits - 4);
  34. return hash;
  35. }
  36. struct nvkm_gpuobj *
  37. nvkm_ramht_search(struct nvkm_ramht *ramht, int chid, u32 handle)
  38. {
  39. u32 co, ho;
  40. co = ho = nvkm_ramht_hash(ramht, chid, handle);
  41. do {
  42. if (ramht->data[co].chid == chid) {
  43. if (ramht->data[co].handle == handle)
  44. return ramht->data[co].inst;
  45. }
  46. if (++co >= ramht->size)
  47. co = 0;
  48. } while (co != ho);
  49. return NULL;
  50. }
  51. static int
  52. nvkm_ramht_update(struct nvkm_ramht *ramht, int co, struct nvkm_object *object,
  53. int chid, int addr, u32 handle, u32 context)
  54. {
  55. struct nvkm_ramht_data *data = &ramht->data[co];
  56. u64 inst = 0x00000040; /* just non-zero for <=g8x fifo ramht */
  57. int ret;
  58. nvkm_gpuobj_del(&data->inst);
  59. data->chid = chid;
  60. data->handle = handle;
  61. if (object) {
  62. ret = nvkm_object_bind(object, ramht->parent, 16, &data->inst);
  63. if (ret) {
  64. if (ret != -ENODEV) {
  65. data->chid = -1;
  66. return ret;
  67. }
  68. data->inst = NULL;
  69. }
  70. if (data->inst) {
  71. if (ramht->device->card_type >= NV_50)
  72. inst = data->inst->node->offset;
  73. else
  74. inst = data->inst->addr;
  75. }
  76. if (addr < 0) context |= inst << -addr;
  77. else context |= inst >> addr;
  78. }
  79. nvkm_kmap(ramht->gpuobj);
  80. nvkm_wo32(ramht->gpuobj, (co << 3) + 0, handle);
  81. nvkm_wo32(ramht->gpuobj, (co << 3) + 4, context);
  82. nvkm_done(ramht->gpuobj);
  83. return co + 1;
  84. }
  85. void
  86. nvkm_ramht_remove(struct nvkm_ramht *ramht, int cookie)
  87. {
  88. if (--cookie >= 0)
  89. nvkm_ramht_update(ramht, cookie, NULL, -1, 0, 0, 0);
  90. }
  91. int
  92. nvkm_ramht_insert(struct nvkm_ramht *ramht, struct nvkm_object *object,
  93. int chid, int addr, u32 handle, u32 context)
  94. {
  95. u32 co, ho;
  96. if (nvkm_ramht_search(ramht, chid, handle))
  97. return -EEXIST;
  98. co = ho = nvkm_ramht_hash(ramht, chid, handle);
  99. do {
  100. if (ramht->data[co].chid < 0) {
  101. return nvkm_ramht_update(ramht, co, object, chid,
  102. addr, handle, context);
  103. }
  104. if (++co >= ramht->size)
  105. co = 0;
  106. } while (co != ho);
  107. return -ENOSPC;
  108. }
  109. void
  110. nvkm_ramht_del(struct nvkm_ramht **pramht)
  111. {
  112. struct nvkm_ramht *ramht = *pramht;
  113. if (ramht) {
  114. nvkm_gpuobj_del(&ramht->gpuobj);
  115. vfree(*pramht);
  116. *pramht = NULL;
  117. }
  118. }
  119. int
  120. nvkm_ramht_new(struct nvkm_device *device, u32 size, u32 align,
  121. struct nvkm_gpuobj *parent, struct nvkm_ramht **pramht)
  122. {
  123. struct nvkm_ramht *ramht;
  124. int ret, i;
  125. if (!(ramht = *pramht = vzalloc(struct_size(ramht, data, (size >> 3)))))
  126. return -ENOMEM;
  127. ramht->device = device;
  128. ramht->parent = parent;
  129. ramht->size = size >> 3;
  130. ramht->bits = order_base_2(ramht->size);
  131. for (i = 0; i < ramht->size; i++)
  132. ramht->data[i].chid = -1;
  133. ret = nvkm_gpuobj_new(ramht->device, size, align, true,
  134. ramht->parent, &ramht->gpuobj);
  135. if (ret)
  136. nvkm_ramht_del(pramht);
  137. return ret;
  138. }