nouveau_mem.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. * Copyright 2017 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 "nouveau_mem.h"
  23. #include "nouveau_drv.h"
  24. #include "nouveau_bo.h"
  25. #include <drm/ttm/ttm_bo_driver.h>
  26. #include <nvif/class.h>
  27. #include <nvif/if000a.h>
  28. #include <nvif/if500b.h>
  29. #include <nvif/if500d.h>
  30. #include <nvif/if900b.h>
  31. #include <nvif/if900d.h>
  32. int
  33. nouveau_mem_map(struct nouveau_mem *mem,
  34. struct nvif_vmm *vmm, struct nvif_vma *vma)
  35. {
  36. union {
  37. struct nv50_vmm_map_v0 nv50;
  38. struct gf100_vmm_map_v0 gf100;
  39. } args;
  40. u32 argc = 0;
  41. switch (vmm->object.oclass) {
  42. case NVIF_CLASS_VMM_NV04:
  43. break;
  44. case NVIF_CLASS_VMM_NV50:
  45. args.nv50.version = 0;
  46. args.nv50.ro = 0;
  47. args.nv50.priv = 0;
  48. args.nv50.kind = mem->kind;
  49. args.nv50.comp = mem->comp;
  50. argc = sizeof(args.nv50);
  51. break;
  52. case NVIF_CLASS_VMM_GF100:
  53. case NVIF_CLASS_VMM_GM200:
  54. case NVIF_CLASS_VMM_GP100:
  55. args.gf100.version = 0;
  56. if (mem->mem.type & NVIF_MEM_VRAM)
  57. args.gf100.vol = 0;
  58. else
  59. args.gf100.vol = 1;
  60. args.gf100.ro = 0;
  61. args.gf100.priv = 0;
  62. args.gf100.kind = mem->kind;
  63. argc = sizeof(args.gf100);
  64. break;
  65. default:
  66. WARN_ON(1);
  67. return -ENOSYS;
  68. }
  69. return nvif_vmm_map(vmm, vma->addr, mem->mem.size, &args, argc, &mem->mem, 0);
  70. }
  71. void
  72. nouveau_mem_fini(struct nouveau_mem *mem)
  73. {
  74. nvif_vmm_put(&mem->cli->drm->client.vmm.vmm, &mem->vma[1]);
  75. nvif_vmm_put(&mem->cli->drm->client.vmm.vmm, &mem->vma[0]);
  76. mutex_lock(&mem->cli->drm->master.lock);
  77. nvif_mem_dtor(&mem->mem);
  78. mutex_unlock(&mem->cli->drm->master.lock);
  79. }
  80. int
  81. nouveau_mem_host(struct ttm_resource *reg, struct ttm_tt *tt)
  82. {
  83. struct nouveau_mem *mem = nouveau_mem(reg);
  84. struct nouveau_cli *cli = mem->cli;
  85. struct nouveau_drm *drm = cli->drm;
  86. struct nvif_mmu *mmu = &cli->mmu;
  87. struct nvif_mem_ram_v0 args = {};
  88. u8 type;
  89. int ret;
  90. if (!nouveau_drm_use_coherent_gpu_mapping(drm))
  91. type = drm->ttm.type_ncoh[!!mem->kind];
  92. else
  93. type = drm->ttm.type_host[0];
  94. if (mem->kind && !(mmu->type[type].type & NVIF_MEM_KIND))
  95. mem->comp = mem->kind = 0;
  96. if (mem->comp && !(mmu->type[type].type & NVIF_MEM_COMP)) {
  97. if (mmu->object.oclass >= NVIF_CLASS_MMU_GF100)
  98. mem->kind = mmu->kind[mem->kind];
  99. mem->comp = 0;
  100. }
  101. if (tt->sg)
  102. args.sgl = tt->sg->sgl;
  103. else
  104. args.dma = tt->dma_address;
  105. mutex_lock(&drm->master.lock);
  106. ret = nvif_mem_ctor_type(mmu, "ttmHostMem", cli->mem->oclass, type, PAGE_SHIFT,
  107. reg->num_pages << PAGE_SHIFT,
  108. &args, sizeof(args), &mem->mem);
  109. mutex_unlock(&drm->master.lock);
  110. return ret;
  111. }
  112. int
  113. nouveau_mem_vram(struct ttm_resource *reg, bool contig, u8 page)
  114. {
  115. struct nouveau_mem *mem = nouveau_mem(reg);
  116. struct nouveau_cli *cli = mem->cli;
  117. struct nouveau_drm *drm = cli->drm;
  118. struct nvif_mmu *mmu = &cli->mmu;
  119. u64 size = ALIGN(reg->num_pages << PAGE_SHIFT, 1 << page);
  120. int ret;
  121. mutex_lock(&drm->master.lock);
  122. switch (cli->mem->oclass) {
  123. case NVIF_CLASS_MEM_GF100:
  124. ret = nvif_mem_ctor_type(mmu, "ttmVram", cli->mem->oclass,
  125. drm->ttm.type_vram, page, size,
  126. &(struct gf100_mem_v0) {
  127. .contig = contig,
  128. }, sizeof(struct gf100_mem_v0),
  129. &mem->mem);
  130. break;
  131. case NVIF_CLASS_MEM_NV50:
  132. ret = nvif_mem_ctor_type(mmu, "ttmVram", cli->mem->oclass,
  133. drm->ttm.type_vram, page, size,
  134. &(struct nv50_mem_v0) {
  135. .bankswz = mmu->kind[mem->kind] == 2,
  136. .contig = contig,
  137. }, sizeof(struct nv50_mem_v0),
  138. &mem->mem);
  139. break;
  140. default:
  141. ret = -ENOSYS;
  142. WARN_ON(1);
  143. break;
  144. }
  145. mutex_unlock(&drm->master.lock);
  146. reg->start = mem->mem.addr >> PAGE_SHIFT;
  147. return ret;
  148. }
  149. void
  150. nouveau_mem_del(struct ttm_resource_manager *man, struct ttm_resource *reg)
  151. {
  152. struct nouveau_mem *mem = nouveau_mem(reg);
  153. nouveau_mem_fini(mem);
  154. ttm_resource_fini(man, reg);
  155. kfree(mem);
  156. }
  157. int
  158. nouveau_mem_new(struct nouveau_cli *cli, u8 kind, u8 comp,
  159. struct ttm_resource **res)
  160. {
  161. struct nouveau_mem *mem;
  162. if (!(mem = kzalloc(sizeof(*mem), GFP_KERNEL)))
  163. return -ENOMEM;
  164. mem->cli = cli;
  165. mem->kind = kind;
  166. mem->comp = comp;
  167. *res = &mem->base;
  168. return 0;
  169. }
  170. bool
  171. nouveau_mem_intersects(struct ttm_resource *res,
  172. const struct ttm_place *place,
  173. size_t size)
  174. {
  175. u32 num_pages = PFN_UP(size);
  176. /* Don't evict BOs outside of the requested placement range */
  177. if (place->fpfn >= (res->start + num_pages) ||
  178. (place->lpfn && place->lpfn <= res->start))
  179. return false;
  180. return true;
  181. }
  182. bool
  183. nouveau_mem_compatible(struct ttm_resource *res,
  184. const struct ttm_place *place,
  185. size_t size)
  186. {
  187. u32 num_pages = PFN_UP(size);
  188. if (res->start < place->fpfn ||
  189. (place->lpfn && (res->start + num_pages) > place->lpfn))
  190. return false;
  191. return true;
  192. }