nouveau_ttm.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. // SPDX-License-Identifier: GPL-2.0 OR MIT
  2. /*
  3. * Copyright (c) 2007-2008 Tungsten Graphics, Inc., Cedar Park, TX., USA,
  4. * Copyright (c) 2009 VMware, Inc., Palo Alto, CA., USA,
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the "Software"),
  8. * to deal in the Software without restriction, including without limitation
  9. * the rights to use, copy, modify, merge, publish, distribute, sub license,
  10. * and/or sell copies of the Software, and to permit persons to whom the
  11. * Software is furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice (including the
  14. * next paragraph) shall be included in all copies or substantial portions
  15. * of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  20. * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  21. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  22. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  23. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. */
  25. #include "nouveau_drv.h"
  26. #include "nouveau_gem.h"
  27. #include "nouveau_mem.h"
  28. #include "nouveau_ttm.h"
  29. #include <drm/drm_legacy.h>
  30. #include <core/tegra.h>
  31. static void
  32. nouveau_manager_del(struct ttm_resource_manager *man, struct ttm_resource *reg)
  33. {
  34. nouveau_mem_del(reg);
  35. }
  36. static int
  37. nouveau_vram_manager_new(struct ttm_resource_manager *man,
  38. struct ttm_buffer_object *bo,
  39. const struct ttm_place *place,
  40. struct ttm_resource *reg)
  41. {
  42. struct nouveau_bo *nvbo = nouveau_bo(bo);
  43. struct nouveau_drm *drm = nouveau_bdev(bo->bdev);
  44. int ret;
  45. if (drm->client.device.info.ram_size == 0)
  46. return -ENOMEM;
  47. ret = nouveau_mem_new(&drm->master, nvbo->kind, nvbo->comp, reg);
  48. if (ret)
  49. return ret;
  50. ret = nouveau_mem_vram(reg, nvbo->contig, nvbo->page);
  51. if (ret) {
  52. nouveau_mem_del(reg);
  53. return ret;
  54. }
  55. return 0;
  56. }
  57. const struct ttm_resource_manager_func nouveau_vram_manager = {
  58. .alloc = nouveau_vram_manager_new,
  59. .free = nouveau_manager_del,
  60. };
  61. static int
  62. nouveau_gart_manager_new(struct ttm_resource_manager *man,
  63. struct ttm_buffer_object *bo,
  64. const struct ttm_place *place,
  65. struct ttm_resource *reg)
  66. {
  67. struct nouveau_bo *nvbo = nouveau_bo(bo);
  68. struct nouveau_drm *drm = nouveau_bdev(bo->bdev);
  69. int ret;
  70. ret = nouveau_mem_new(&drm->master, nvbo->kind, nvbo->comp, reg);
  71. if (ret)
  72. return ret;
  73. reg->start = 0;
  74. return 0;
  75. }
  76. const struct ttm_resource_manager_func nouveau_gart_manager = {
  77. .alloc = nouveau_gart_manager_new,
  78. .free = nouveau_manager_del,
  79. };
  80. static int
  81. nv04_gart_manager_new(struct ttm_resource_manager *man,
  82. struct ttm_buffer_object *bo,
  83. const struct ttm_place *place,
  84. struct ttm_resource *reg)
  85. {
  86. struct nouveau_bo *nvbo = nouveau_bo(bo);
  87. struct nouveau_drm *drm = nouveau_bdev(bo->bdev);
  88. struct nouveau_mem *mem;
  89. int ret;
  90. ret = nouveau_mem_new(&drm->master, nvbo->kind, nvbo->comp, reg);
  91. mem = nouveau_mem(reg);
  92. if (ret)
  93. return ret;
  94. ret = nvif_vmm_get(&mem->cli->vmm.vmm, PTES, false, 12, 0,
  95. reg->num_pages << PAGE_SHIFT, &mem->vma[0]);
  96. if (ret) {
  97. nouveau_mem_del(reg);
  98. return ret;
  99. }
  100. reg->start = mem->vma[0].addr >> PAGE_SHIFT;
  101. return 0;
  102. }
  103. const struct ttm_resource_manager_func nv04_gart_manager = {
  104. .alloc = nv04_gart_manager_new,
  105. .free = nouveau_manager_del,
  106. };
  107. static vm_fault_t nouveau_ttm_fault(struct vm_fault *vmf)
  108. {
  109. struct vm_area_struct *vma = vmf->vma;
  110. struct ttm_buffer_object *bo = vma->vm_private_data;
  111. pgprot_t prot;
  112. vm_fault_t ret;
  113. ret = ttm_bo_vm_reserve(bo, vmf);
  114. if (ret)
  115. return ret;
  116. nouveau_bo_del_io_reserve_lru(bo);
  117. prot = vm_get_page_prot(vma->vm_flags);
  118. ret = ttm_bo_vm_fault_reserved(vmf, prot, TTM_BO_VM_NUM_PREFAULT, 1);
  119. if (ret == VM_FAULT_RETRY && !(vmf->flags & FAULT_FLAG_RETRY_NOWAIT))
  120. return ret;
  121. nouveau_bo_add_io_reserve_lru(bo);
  122. dma_resv_unlock(bo->base.resv);
  123. return ret;
  124. }
  125. static struct vm_operations_struct nouveau_ttm_vm_ops = {
  126. .fault = nouveau_ttm_fault,
  127. .open = ttm_bo_vm_open,
  128. .close = ttm_bo_vm_close,
  129. .access = ttm_bo_vm_access
  130. };
  131. int
  132. nouveau_ttm_mmap(struct file *filp, struct vm_area_struct *vma)
  133. {
  134. struct drm_file *file_priv = filp->private_data;
  135. struct nouveau_drm *drm = nouveau_drm(file_priv->minor->dev);
  136. int ret;
  137. ret = ttm_bo_mmap(filp, vma, &drm->ttm.bdev);
  138. if (ret)
  139. return ret;
  140. vma->vm_ops = &nouveau_ttm_vm_ops;
  141. return 0;
  142. }
  143. static int
  144. nouveau_ttm_init_host(struct nouveau_drm *drm, u8 kind)
  145. {
  146. struct nvif_mmu *mmu = &drm->client.mmu;
  147. int typei;
  148. typei = nvif_mmu_type(mmu, NVIF_MEM_HOST | NVIF_MEM_MAPPABLE |
  149. kind | NVIF_MEM_COHERENT);
  150. if (typei < 0)
  151. return -ENOSYS;
  152. drm->ttm.type_host[!!kind] = typei;
  153. typei = nvif_mmu_type(mmu, NVIF_MEM_HOST | NVIF_MEM_MAPPABLE | kind);
  154. if (typei < 0)
  155. return -ENOSYS;
  156. drm->ttm.type_ncoh[!!kind] = typei;
  157. return 0;
  158. }
  159. static int
  160. nouveau_ttm_init_vram(struct nouveau_drm *drm)
  161. {
  162. if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_TESLA) {
  163. struct ttm_resource_manager *man = kzalloc(sizeof(*man), GFP_KERNEL);
  164. if (!man)
  165. return -ENOMEM;
  166. man->func = &nouveau_vram_manager;
  167. ttm_resource_manager_init(man,
  168. drm->gem.vram_available >> PAGE_SHIFT);
  169. ttm_set_driver_manager(&drm->ttm.bdev, TTM_PL_VRAM, man);
  170. ttm_resource_manager_set_used(man, true);
  171. return 0;
  172. } else {
  173. return ttm_range_man_init(&drm->ttm.bdev, TTM_PL_VRAM, false,
  174. drm->gem.vram_available >> PAGE_SHIFT);
  175. }
  176. }
  177. static void
  178. nouveau_ttm_fini_vram(struct nouveau_drm *drm)
  179. {
  180. struct ttm_resource_manager *man = ttm_manager_type(&drm->ttm.bdev, TTM_PL_VRAM);
  181. if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_TESLA) {
  182. ttm_resource_manager_set_used(man, false);
  183. ttm_resource_manager_force_list_clean(&drm->ttm.bdev, man);
  184. ttm_resource_manager_cleanup(man);
  185. ttm_set_driver_manager(&drm->ttm.bdev, TTM_PL_VRAM, NULL);
  186. kfree(man);
  187. } else
  188. ttm_range_man_fini(&drm->ttm.bdev, TTM_PL_VRAM);
  189. }
  190. static int
  191. nouveau_ttm_init_gtt(struct nouveau_drm *drm)
  192. {
  193. struct ttm_resource_manager *man;
  194. unsigned long size_pages = drm->gem.gart_available >> PAGE_SHIFT;
  195. const struct ttm_resource_manager_func *func = NULL;
  196. if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_TESLA)
  197. func = &nouveau_gart_manager;
  198. else if (!drm->agp.bridge)
  199. func = &nv04_gart_manager;
  200. else
  201. return ttm_range_man_init(&drm->ttm.bdev, TTM_PL_TT, true,
  202. size_pages);
  203. man = kzalloc(sizeof(*man), GFP_KERNEL);
  204. if (!man)
  205. return -ENOMEM;
  206. man->func = func;
  207. man->use_tt = true;
  208. ttm_resource_manager_init(man, size_pages);
  209. ttm_set_driver_manager(&drm->ttm.bdev, TTM_PL_TT, man);
  210. ttm_resource_manager_set_used(man, true);
  211. return 0;
  212. }
  213. static void
  214. nouveau_ttm_fini_gtt(struct nouveau_drm *drm)
  215. {
  216. struct ttm_resource_manager *man = ttm_manager_type(&drm->ttm.bdev, TTM_PL_TT);
  217. if (drm->client.device.info.family < NV_DEVICE_INFO_V0_TESLA &&
  218. drm->agp.bridge)
  219. ttm_range_man_fini(&drm->ttm.bdev, TTM_PL_TT);
  220. else {
  221. ttm_resource_manager_set_used(man, false);
  222. ttm_resource_manager_force_list_clean(&drm->ttm.bdev, man);
  223. ttm_resource_manager_cleanup(man);
  224. ttm_set_driver_manager(&drm->ttm.bdev, TTM_PL_TT, NULL);
  225. kfree(man);
  226. }
  227. }
  228. int
  229. nouveau_ttm_init(struct nouveau_drm *drm)
  230. {
  231. struct nvkm_device *device = nvxx_device(&drm->client.device);
  232. struct nvkm_pci *pci = device->pci;
  233. struct nvif_mmu *mmu = &drm->client.mmu;
  234. struct drm_device *dev = drm->dev;
  235. int typei, ret;
  236. ret = nouveau_ttm_init_host(drm, 0);
  237. if (ret)
  238. return ret;
  239. if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_TESLA &&
  240. drm->client.device.info.chipset != 0x50) {
  241. ret = nouveau_ttm_init_host(drm, NVIF_MEM_KIND);
  242. if (ret)
  243. return ret;
  244. }
  245. if (drm->client.device.info.platform != NV_DEVICE_INFO_V0_SOC &&
  246. drm->client.device.info.family >= NV_DEVICE_INFO_V0_TESLA) {
  247. typei = nvif_mmu_type(mmu, NVIF_MEM_VRAM | NVIF_MEM_MAPPABLE |
  248. NVIF_MEM_KIND |
  249. NVIF_MEM_COMP |
  250. NVIF_MEM_DISP);
  251. if (typei < 0)
  252. return -ENOSYS;
  253. drm->ttm.type_vram = typei;
  254. } else {
  255. drm->ttm.type_vram = -1;
  256. }
  257. if (pci && pci->agp.bridge) {
  258. drm->agp.bridge = pci->agp.bridge;
  259. drm->agp.base = pci->agp.base;
  260. drm->agp.size = pci->agp.size;
  261. drm->agp.cma = pci->agp.cma;
  262. }
  263. ret = ttm_bo_device_init(&drm->ttm.bdev,
  264. &nouveau_bo_driver,
  265. dev->anon_inode->i_mapping,
  266. dev->vma_offset_manager,
  267. drm->client.mmu.dmabits <= 32 ? true : false);
  268. if (ret) {
  269. NV_ERROR(drm, "error initialising bo driver, %d\n", ret);
  270. return ret;
  271. }
  272. /* VRAM init */
  273. drm->gem.vram_available = drm->client.device.info.ram_user;
  274. arch_io_reserve_memtype_wc(device->func->resource_addr(device, 1),
  275. device->func->resource_size(device, 1));
  276. ret = nouveau_ttm_init_vram(drm);
  277. if (ret) {
  278. NV_ERROR(drm, "VRAM mm init failed, %d\n", ret);
  279. return ret;
  280. }
  281. drm->ttm.mtrr = arch_phys_wc_add(device->func->resource_addr(device, 1),
  282. device->func->resource_size(device, 1));
  283. /* GART init */
  284. if (!drm->agp.bridge) {
  285. drm->gem.gart_available = drm->client.vmm.vmm.limit;
  286. } else {
  287. drm->gem.gart_available = drm->agp.size;
  288. }
  289. ret = nouveau_ttm_init_gtt(drm);
  290. if (ret) {
  291. NV_ERROR(drm, "GART mm init failed, %d\n", ret);
  292. return ret;
  293. }
  294. mutex_init(&drm->ttm.io_reserve_mutex);
  295. INIT_LIST_HEAD(&drm->ttm.io_reserve_lru);
  296. NV_INFO(drm, "VRAM: %d MiB\n", (u32)(drm->gem.vram_available >> 20));
  297. NV_INFO(drm, "GART: %d MiB\n", (u32)(drm->gem.gart_available >> 20));
  298. return 0;
  299. }
  300. void
  301. nouveau_ttm_fini(struct nouveau_drm *drm)
  302. {
  303. struct nvkm_device *device = nvxx_device(&drm->client.device);
  304. nouveau_ttm_fini_vram(drm);
  305. nouveau_ttm_fini_gtt(drm);
  306. ttm_bo_device_release(&drm->ttm.bdev);
  307. arch_phys_wc_del(drm->ttm.mtrr);
  308. drm->ttm.mtrr = 0;
  309. arch_io_free_memtype_wc(device->func->resource_addr(device, 1),
  310. device->func->resource_size(device, 1));
  311. }