nouveau_mem.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. bool super;
  42. int ret;
  43. switch (vmm->object.oclass) {
  44. case NVIF_CLASS_VMM_NV04:
  45. break;
  46. case NVIF_CLASS_VMM_NV50:
  47. args.nv50.version = 0;
  48. args.nv50.ro = 0;
  49. args.nv50.priv = 0;
  50. args.nv50.kind = mem->kind;
  51. args.nv50.comp = mem->comp;
  52. argc = sizeof(args.nv50);
  53. break;
  54. case NVIF_CLASS_VMM_GF100:
  55. case NVIF_CLASS_VMM_GM200:
  56. case NVIF_CLASS_VMM_GP100:
  57. args.gf100.version = 0;
  58. if (mem->mem.type & NVIF_MEM_VRAM)
  59. args.gf100.vol = 0;
  60. else
  61. args.gf100.vol = 1;
  62. args.gf100.ro = 0;
  63. args.gf100.priv = 0;
  64. args.gf100.kind = mem->kind;
  65. argc = sizeof(args.gf100);
  66. break;
  67. default:
  68. WARN_ON(1);
  69. return -ENOSYS;
  70. }
  71. super = vmm->object.client->super;
  72. vmm->object.client->super = true;
  73. ret = nvif_vmm_map(vmm, vma->addr, mem->mem.size, &args, argc,
  74. &mem->mem, 0);
  75. vmm->object.client->super = super;
  76. return ret;
  77. }
  78. void
  79. nouveau_mem_fini(struct nouveau_mem *mem)
  80. {
  81. nvif_vmm_put(&mem->cli->drm->client.vmm.vmm, &mem->vma[1]);
  82. nvif_vmm_put(&mem->cli->drm->client.vmm.vmm, &mem->vma[0]);
  83. mutex_lock(&mem->cli->drm->master.lock);
  84. nvif_mem_dtor(&mem->mem);
  85. mutex_unlock(&mem->cli->drm->master.lock);
  86. }
  87. int
  88. nouveau_mem_host(struct ttm_resource *reg, struct ttm_dma_tt *tt)
  89. {
  90. struct nouveau_mem *mem = nouveau_mem(reg);
  91. struct nouveau_cli *cli = mem->cli;
  92. struct nouveau_drm *drm = cli->drm;
  93. struct nvif_mmu *mmu = &cli->mmu;
  94. struct nvif_mem_ram_v0 args = {};
  95. bool super = cli->base.super;
  96. u8 type;
  97. int ret;
  98. if (!nouveau_drm_use_coherent_gpu_mapping(drm))
  99. type = drm->ttm.type_ncoh[!!mem->kind];
  100. else
  101. type = drm->ttm.type_host[0];
  102. if (mem->kind && !(mmu->type[type].type & NVIF_MEM_KIND))
  103. mem->comp = mem->kind = 0;
  104. if (mem->comp && !(mmu->type[type].type & NVIF_MEM_COMP)) {
  105. if (mmu->object.oclass >= NVIF_CLASS_MMU_GF100)
  106. mem->kind = mmu->kind[mem->kind];
  107. mem->comp = 0;
  108. }
  109. if (tt->ttm.sg) args.sgl = tt->ttm.sg->sgl;
  110. else args.dma = tt->dma_address;
  111. mutex_lock(&drm->master.lock);
  112. cli->base.super = true;
  113. ret = nvif_mem_ctor_type(mmu, "ttmHostMem", cli->mem->oclass, type, PAGE_SHIFT,
  114. reg->num_pages << PAGE_SHIFT,
  115. &args, sizeof(args), &mem->mem);
  116. cli->base.super = super;
  117. mutex_unlock(&drm->master.lock);
  118. return ret;
  119. }
  120. int
  121. nouveau_mem_vram(struct ttm_resource *reg, bool contig, u8 page)
  122. {
  123. struct nouveau_mem *mem = nouveau_mem(reg);
  124. struct nouveau_cli *cli = mem->cli;
  125. struct nouveau_drm *drm = cli->drm;
  126. struct nvif_mmu *mmu = &cli->mmu;
  127. bool super = cli->base.super;
  128. u64 size = ALIGN(reg->num_pages << PAGE_SHIFT, 1 << page);
  129. int ret;
  130. mutex_lock(&drm->master.lock);
  131. cli->base.super = true;
  132. switch (cli->mem->oclass) {
  133. case NVIF_CLASS_MEM_GF100:
  134. ret = nvif_mem_ctor_type(mmu, "ttmVram", cli->mem->oclass,
  135. drm->ttm.type_vram, page, size,
  136. &(struct gf100_mem_v0) {
  137. .contig = contig,
  138. }, sizeof(struct gf100_mem_v0),
  139. &mem->mem);
  140. break;
  141. case NVIF_CLASS_MEM_NV50:
  142. ret = nvif_mem_ctor_type(mmu, "ttmVram", cli->mem->oclass,
  143. drm->ttm.type_vram, page, size,
  144. &(struct nv50_mem_v0) {
  145. .bankswz = mmu->kind[mem->kind] == 2,
  146. .contig = contig,
  147. }, sizeof(struct nv50_mem_v0),
  148. &mem->mem);
  149. break;
  150. default:
  151. ret = -ENOSYS;
  152. WARN_ON(1);
  153. break;
  154. }
  155. cli->base.super = super;
  156. mutex_unlock(&drm->master.lock);
  157. reg->start = mem->mem.addr >> PAGE_SHIFT;
  158. return ret;
  159. }
  160. void
  161. nouveau_mem_del(struct ttm_resource *reg)
  162. {
  163. struct nouveau_mem *mem = nouveau_mem(reg);
  164. if (!mem)
  165. return;
  166. nouveau_mem_fini(mem);
  167. kfree(reg->mm_node);
  168. reg->mm_node = NULL;
  169. }
  170. int
  171. nouveau_mem_new(struct nouveau_cli *cli, u8 kind, u8 comp,
  172. struct ttm_resource *reg)
  173. {
  174. struct nouveau_mem *mem;
  175. if (!(mem = kzalloc(sizeof(*mem), GFP_KERNEL)))
  176. return -ENOMEM;
  177. mem->cli = cli;
  178. mem->kind = kind;
  179. mem->comp = comp;
  180. reg->mm_node = mem;
  181. return 0;
  182. }