nv50_vram.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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_mm.h"
  27. static int types[0x80] = {
  28. 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  29. 1, 1, 1, 1, 0, 0, 0, 0, 2, 2, 2, 2, 0, 0, 0, 0,
  30. 1, 1, 1, 1, 1, 1, 1, 0, 2, 2, 2, 2, 2, 2, 2, 0,
  31. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  32. 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 0, 0,
  33. 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  34. 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 2, 2, 2, 2,
  35. 1, 0, 2, 0, 1, 0, 2, 0, 1, 1, 2, 2, 1, 1, 0, 0
  36. };
  37. bool
  38. nv50_vram_flags_valid(struct drm_device *dev, u32 tile_flags)
  39. {
  40. int type = (tile_flags & NOUVEAU_GEM_TILE_LAYOUT_MASK) >> 8;
  41. if (likely(type < ARRAY_SIZE(types) && types[type]))
  42. return true;
  43. return false;
  44. }
  45. void
  46. nv50_vram_del(struct drm_device *dev, struct nouveau_mem **pmem)
  47. {
  48. struct drm_nouveau_private *dev_priv = dev->dev_private;
  49. struct ttm_bo_device *bdev = &dev_priv->ttm.bdev;
  50. struct ttm_mem_type_manager *man = &bdev->man[TTM_PL_VRAM];
  51. struct nouveau_mm *mm = man->priv;
  52. struct nouveau_mm_node *this;
  53. struct nouveau_mem *mem;
  54. mem = *pmem;
  55. *pmem = NULL;
  56. if (unlikely(mem == NULL))
  57. return;
  58. mutex_lock(&mm->mutex);
  59. while (!list_empty(&mem->regions)) {
  60. this = list_first_entry(&mem->regions, struct nouveau_mm_node, rl_entry);
  61. list_del(&this->rl_entry);
  62. nouveau_mm_put(mm, this);
  63. }
  64. if (mem->tag) {
  65. drm_mm_put_block(mem->tag);
  66. mem->tag = NULL;
  67. }
  68. mutex_unlock(&mm->mutex);
  69. kfree(mem);
  70. }
  71. int
  72. nv50_vram_new(struct drm_device *dev, u64 size, u32 align, u32 size_nc,
  73. u32 memtype, struct nouveau_mem **pmem)
  74. {
  75. struct drm_nouveau_private *dev_priv = dev->dev_private;
  76. struct ttm_bo_device *bdev = &dev_priv->ttm.bdev;
  77. struct ttm_mem_type_manager *man = &bdev->man[TTM_PL_VRAM];
  78. struct nouveau_mm *mm = man->priv;
  79. struct nouveau_mm_node *r;
  80. struct nouveau_mem *mem;
  81. int comp = (memtype & 0x300) >> 8;
  82. int type = (memtype & 0x07f);
  83. int ret;
  84. if (!types[type])
  85. return -EINVAL;
  86. size >>= 12;
  87. align >>= 12;
  88. size_nc >>= 12;
  89. mem = kzalloc(sizeof(*mem), GFP_KERNEL);
  90. if (!mem)
  91. return -ENOMEM;
  92. mutex_lock(&mm->mutex);
  93. if (comp) {
  94. if (align == 16) {
  95. struct nouveau_fb_engine *pfb = &dev_priv->engine.fb;
  96. int n = (size >> 4) * comp;
  97. mem->tag = drm_mm_search_free(&pfb->tag_heap, n, 0, 0);
  98. if (mem->tag)
  99. mem->tag = drm_mm_get_block(mem->tag, n, 0);
  100. }
  101. if (unlikely(!mem->tag))
  102. comp = 0;
  103. }
  104. INIT_LIST_HEAD(&mem->regions);
  105. mem->dev = dev_priv->dev;
  106. mem->memtype = (comp << 7) | type;
  107. mem->size = size;
  108. do {
  109. ret = nouveau_mm_get(mm, types[type], size, size_nc, align, &r);
  110. if (ret) {
  111. mutex_unlock(&mm->mutex);
  112. nv50_vram_del(dev, &mem);
  113. return ret;
  114. }
  115. list_add_tail(&r->rl_entry, &mem->regions);
  116. size -= r->length;
  117. } while (size);
  118. mutex_unlock(&mm->mutex);
  119. r = list_first_entry(&mem->regions, struct nouveau_mm_node, rl_entry);
  120. mem->offset = (u64)r->offset << 12;
  121. *pmem = mem;
  122. return 0;
  123. }
  124. static u32
  125. nv50_vram_rblock(struct drm_device *dev)
  126. {
  127. struct drm_nouveau_private *dev_priv = dev->dev_private;
  128. int i, parts, colbits, rowbitsa, rowbitsb, banks;
  129. u64 rowsize, predicted;
  130. u32 r0, r4, rt, ru, rblock_size;
  131. r0 = nv_rd32(dev, 0x100200);
  132. r4 = nv_rd32(dev, 0x100204);
  133. rt = nv_rd32(dev, 0x100250);
  134. ru = nv_rd32(dev, 0x001540);
  135. NV_DEBUG(dev, "memcfg 0x%08x 0x%08x 0x%08x 0x%08x\n", r0, r4, rt, ru);
  136. for (i = 0, parts = 0; i < 8; i++) {
  137. if (ru & (0x00010000 << i))
  138. parts++;
  139. }
  140. colbits = (r4 & 0x0000f000) >> 12;
  141. rowbitsa = ((r4 & 0x000f0000) >> 16) + 8;
  142. rowbitsb = ((r4 & 0x00f00000) >> 20) + 8;
  143. banks = ((r4 & 0x01000000) ? 8 : 4);
  144. rowsize = parts * banks * (1 << colbits) * 8;
  145. predicted = rowsize << rowbitsa;
  146. if (r0 & 0x00000004)
  147. predicted += rowsize << rowbitsb;
  148. if (predicted != dev_priv->vram_size) {
  149. NV_WARN(dev, "memory controller reports %dMiB VRAM\n",
  150. (u32)(dev_priv->vram_size >> 20));
  151. NV_WARN(dev, "we calculated %dMiB VRAM\n",
  152. (u32)(predicted >> 20));
  153. }
  154. rblock_size = rowsize;
  155. if (rt & 1)
  156. rblock_size *= 3;
  157. NV_DEBUG(dev, "rblock %d bytes\n", rblock_size);
  158. return rblock_size;
  159. }
  160. int
  161. nv50_vram_init(struct drm_device *dev)
  162. {
  163. struct drm_nouveau_private *dev_priv = dev->dev_private;
  164. dev_priv->vram_size = nv_rd32(dev, 0x10020c);
  165. dev_priv->vram_size |= (dev_priv->vram_size & 0xff) << 32;
  166. dev_priv->vram_size &= 0xffffffff00ULL;
  167. switch (dev_priv->chipset) {
  168. case 0xaa:
  169. case 0xac:
  170. case 0xaf:
  171. dev_priv->vram_sys_base = (u64)nv_rd32(dev, 0x100e10) << 12;
  172. dev_priv->vram_rblock_size = 4096;
  173. break;
  174. default:
  175. dev_priv->vram_rblock_size = nv50_vram_rblock(dev);
  176. break;
  177. }
  178. return 0;
  179. }