nvc0_vram.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. /* 0 = unsupported
  28. * 1 = non-compressed
  29. * 3 = compressed
  30. */
  31. static const u8 types[256] = {
  32. 1, 1, 3, 3, 3, 3, 0, 3, 3, 3, 3, 0, 0, 0, 0, 0,
  33. 0, 1, 0, 0, 0, 0, 0, 3, 3, 3, 3, 0, 0, 0, 0, 0,
  34. 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,
  35. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 3, 3,
  36. 3, 3, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  37. 0, 3, 3, 3, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  38. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  39. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0,
  40. 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 0, 1, 1, 1, 1, 0,
  41. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  42. 0, 0, 0, 3, 3, 3, 3, 1, 1, 1, 1, 0, 0, 0, 0, 0,
  43. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3,
  44. 3, 3, 3, 1, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3,
  45. 3, 3, 0, 0, 0, 0, 0, 0, 3, 0, 0, 3, 0, 3, 0, 3,
  46. 3, 0, 3, 3, 3, 3, 3, 0, 0, 3, 0, 3, 0, 3, 3, 0,
  47. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 1, 1, 0
  48. };
  49. bool
  50. nvc0_vram_flags_valid(struct drm_device *dev, u32 tile_flags)
  51. {
  52. u8 memtype = (tile_flags & NOUVEAU_GEM_TILE_LAYOUT_MASK) >> 8;
  53. return likely((types[memtype] == 1));
  54. }
  55. int
  56. nvc0_vram_new(struct drm_device *dev, u64 size, u32 align, u32 ncmin,
  57. u32 type, struct nouveau_mem **pmem)
  58. {
  59. struct drm_nouveau_private *dev_priv = dev->dev_private;
  60. struct nouveau_mm *mm = &dev_priv->engine.vram.mm;
  61. struct nouveau_mm_node *r;
  62. struct nouveau_mem *mem;
  63. int ret;
  64. size >>= 12;
  65. align >>= 12;
  66. ncmin >>= 12;
  67. mem = kzalloc(sizeof(*mem), GFP_KERNEL);
  68. if (!mem)
  69. return -ENOMEM;
  70. INIT_LIST_HEAD(&mem->regions);
  71. mem->dev = dev_priv->dev;
  72. mem->memtype = (type & 0xff);
  73. mem->size = size;
  74. mutex_lock(&mm->mutex);
  75. do {
  76. ret = nouveau_mm_get(mm, 1, size, ncmin, align, &r);
  77. if (ret) {
  78. mutex_unlock(&mm->mutex);
  79. nv50_vram_del(dev, &mem);
  80. return ret;
  81. }
  82. list_add_tail(&r->rl_entry, &mem->regions);
  83. size -= r->length;
  84. } while (size);
  85. mutex_unlock(&mm->mutex);
  86. r = list_first_entry(&mem->regions, struct nouveau_mm_node, rl_entry);
  87. mem->offset = (u64)r->offset << 12;
  88. *pmem = mem;
  89. return 0;
  90. }
  91. int
  92. nvc0_vram_init(struct drm_device *dev)
  93. {
  94. struct drm_nouveau_private *dev_priv = dev->dev_private;
  95. struct nouveau_vram_engine *vram = &dev_priv->engine.vram;
  96. const u32 rsvd_head = ( 256 * 1024) >> 12; /* vga memory */
  97. const u32 rsvd_tail = (1024 * 1024) >> 12; /* vbios etc */
  98. u32 parts = nv_rd32(dev, 0x022438);
  99. u32 pmask = nv_rd32(dev, 0x022554);
  100. u32 bsize = nv_rd32(dev, 0x10f20c);
  101. u32 offset, length;
  102. bool uniform = true;
  103. int ret, part;
  104. NV_DEBUG(dev, "0x100800: 0x%08x\n", nv_rd32(dev, 0x100800));
  105. NV_DEBUG(dev, "parts 0x%08x mask 0x%08x\n", parts, pmask);
  106. dev_priv->vram_type = nouveau_mem_vbios_type(dev);
  107. dev_priv->vram_rank_B = !!(nv_rd32(dev, 0x10f200) & 0x00000004);
  108. /* read amount of vram attached to each memory controller */
  109. for (part = 0; part < parts; part++) {
  110. if (!(pmask & (1 << part))) {
  111. u32 psize = nv_rd32(dev, 0x11020c + (part * 0x1000));
  112. if (psize != bsize) {
  113. if (psize < bsize)
  114. bsize = psize;
  115. uniform = false;
  116. }
  117. NV_DEBUG(dev, "%d: mem_amount 0x%08x\n", part, psize);
  118. dev_priv->vram_size += (u64)psize << 20;
  119. }
  120. }
  121. /* if all controllers have the same amount attached, there's no holes */
  122. if (uniform) {
  123. offset = rsvd_head;
  124. length = (dev_priv->vram_size >> 12) - rsvd_head - rsvd_tail;
  125. return nouveau_mm_init(&vram->mm, offset, length, 1);
  126. }
  127. /* otherwise, address lowest common amount from 0GiB */
  128. ret = nouveau_mm_init(&vram->mm, rsvd_head, (bsize << 8) * parts, 1);
  129. if (ret)
  130. return ret;
  131. /* and the rest starting from (8GiB + common_size) */
  132. offset = (0x0200000000ULL >> 12) + (bsize << 8);
  133. length = (dev_priv->vram_size >> 12) - (bsize << 8) - rsvd_tail;
  134. ret = nouveau_mm_init(&vram->mm, offset, length, 0);
  135. if (ret) {
  136. nouveau_mm_fini(&vram->mm);
  137. return ret;
  138. }
  139. return 0;
  140. }