nouveau_ttm.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. /*
  2. * Copyright (c) 2007-2008 Tungsten Graphics, Inc., Cedar Park, TX., USA,
  3. * All Rights Reserved.
  4. * Copyright (c) 2009 VMware, Inc., Palo Alto, CA., USA,
  5. * All Rights Reserved.
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a
  8. * copy of this software and associated documentation files (the "Software"),
  9. * to deal in the Software without restriction, including without limitation
  10. * the rights to use, copy, modify, merge, publish, distribute, sub license,
  11. * and/or sell copies of the Software, and to permit persons to whom the
  12. * Software is furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice (including the
  15. * next paragraph) shall be included in all copies or substantial portions
  16. * of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  21. * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  22. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  23. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  24. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. */
  26. #include "nouveau_drv.h"
  27. #include "nouveau_ttm.h"
  28. #include "nouveau_gem.h"
  29. #include "drm_legacy.h"
  30. #include <core/tegra.h>
  31. static int
  32. nouveau_vram_manager_init(struct ttm_mem_type_manager *man, unsigned long psize)
  33. {
  34. struct nouveau_drm *drm = nouveau_bdev(man->bdev);
  35. struct nvkm_fb *fb = nvxx_fb(&drm->device);
  36. man->priv = fb;
  37. return 0;
  38. }
  39. static int
  40. nouveau_vram_manager_fini(struct ttm_mem_type_manager *man)
  41. {
  42. man->priv = NULL;
  43. return 0;
  44. }
  45. static inline void
  46. nvkm_mem_node_cleanup(struct nvkm_mem *node)
  47. {
  48. if (node->vma[0].node) {
  49. nvkm_vm_unmap(&node->vma[0]);
  50. nvkm_vm_put(&node->vma[0]);
  51. }
  52. if (node->vma[1].node) {
  53. nvkm_vm_unmap(&node->vma[1]);
  54. nvkm_vm_put(&node->vma[1]);
  55. }
  56. }
  57. static void
  58. nouveau_vram_manager_del(struct ttm_mem_type_manager *man,
  59. struct ttm_mem_reg *mem)
  60. {
  61. struct nouveau_drm *drm = nouveau_bdev(man->bdev);
  62. struct nvkm_ram *ram = nvxx_fb(&drm->device)->ram;
  63. nvkm_mem_node_cleanup(mem->mm_node);
  64. ram->func->put(ram, (struct nvkm_mem **)&mem->mm_node);
  65. }
  66. static int
  67. nouveau_vram_manager_new(struct ttm_mem_type_manager *man,
  68. struct ttm_buffer_object *bo,
  69. const struct ttm_place *place,
  70. struct ttm_mem_reg *mem)
  71. {
  72. struct nouveau_drm *drm = nouveau_bdev(man->bdev);
  73. struct nvkm_ram *ram = nvxx_fb(&drm->device)->ram;
  74. struct nouveau_bo *nvbo = nouveau_bo(bo);
  75. struct nvkm_mem *node;
  76. u32 size_nc = 0;
  77. int ret;
  78. if (drm->device.info.ram_size == 0)
  79. return -ENOMEM;
  80. if (nvbo->tile_flags & NOUVEAU_GEM_TILE_NONCONTIG)
  81. size_nc = 1 << nvbo->page_shift;
  82. ret = ram->func->get(ram, mem->num_pages << PAGE_SHIFT,
  83. mem->page_alignment << PAGE_SHIFT, size_nc,
  84. (nvbo->tile_flags >> 8) & 0x3ff, &node);
  85. if (ret) {
  86. mem->mm_node = NULL;
  87. return (ret == -ENOSPC) ? 0 : ret;
  88. }
  89. node->page_shift = nvbo->page_shift;
  90. mem->mm_node = node;
  91. mem->start = node->offset >> PAGE_SHIFT;
  92. return 0;
  93. }
  94. const struct ttm_mem_type_manager_func nouveau_vram_manager = {
  95. .init = nouveau_vram_manager_init,
  96. .takedown = nouveau_vram_manager_fini,
  97. .get_node = nouveau_vram_manager_new,
  98. .put_node = nouveau_vram_manager_del,
  99. };
  100. static int
  101. nouveau_gart_manager_init(struct ttm_mem_type_manager *man, unsigned long psize)
  102. {
  103. return 0;
  104. }
  105. static int
  106. nouveau_gart_manager_fini(struct ttm_mem_type_manager *man)
  107. {
  108. return 0;
  109. }
  110. static void
  111. nouveau_gart_manager_del(struct ttm_mem_type_manager *man,
  112. struct ttm_mem_reg *mem)
  113. {
  114. nvkm_mem_node_cleanup(mem->mm_node);
  115. kfree(mem->mm_node);
  116. mem->mm_node = NULL;
  117. }
  118. static int
  119. nouveau_gart_manager_new(struct ttm_mem_type_manager *man,
  120. struct ttm_buffer_object *bo,
  121. const struct ttm_place *place,
  122. struct ttm_mem_reg *mem)
  123. {
  124. struct nouveau_drm *drm = nouveau_bdev(bo->bdev);
  125. struct nouveau_bo *nvbo = nouveau_bo(bo);
  126. struct nvkm_mem *node;
  127. node = kzalloc(sizeof(*node), GFP_KERNEL);
  128. if (!node)
  129. return -ENOMEM;
  130. node->page_shift = 12;
  131. switch (drm->device.info.family) {
  132. case NV_DEVICE_INFO_V0_TNT:
  133. case NV_DEVICE_INFO_V0_CELSIUS:
  134. case NV_DEVICE_INFO_V0_KELVIN:
  135. case NV_DEVICE_INFO_V0_RANKINE:
  136. case NV_DEVICE_INFO_V0_CURIE:
  137. break;
  138. case NV_DEVICE_INFO_V0_TESLA:
  139. if (drm->device.info.chipset != 0x50)
  140. node->memtype = (nvbo->tile_flags & 0x7f00) >> 8;
  141. break;
  142. case NV_DEVICE_INFO_V0_FERMI:
  143. case NV_DEVICE_INFO_V0_KEPLER:
  144. case NV_DEVICE_INFO_V0_MAXWELL:
  145. case NV_DEVICE_INFO_V0_PASCAL:
  146. node->memtype = (nvbo->tile_flags & 0xff00) >> 8;
  147. break;
  148. default:
  149. NV_WARN(drm, "%s: unhandled family type %x\n", __func__,
  150. drm->device.info.family);
  151. break;
  152. }
  153. mem->mm_node = node;
  154. mem->start = 0;
  155. return 0;
  156. }
  157. static void
  158. nouveau_gart_manager_debug(struct ttm_mem_type_manager *man, const char *prefix)
  159. {
  160. }
  161. const struct ttm_mem_type_manager_func nouveau_gart_manager = {
  162. .init = nouveau_gart_manager_init,
  163. .takedown = nouveau_gart_manager_fini,
  164. .get_node = nouveau_gart_manager_new,
  165. .put_node = nouveau_gart_manager_del,
  166. .debug = nouveau_gart_manager_debug
  167. };
  168. /*XXX*/
  169. #include <subdev/mmu/nv04.h>
  170. static int
  171. nv04_gart_manager_init(struct ttm_mem_type_manager *man, unsigned long psize)
  172. {
  173. struct nouveau_drm *drm = nouveau_bdev(man->bdev);
  174. struct nvkm_mmu *mmu = nvxx_mmu(&drm->device);
  175. struct nv04_mmu *priv = (void *)mmu;
  176. struct nvkm_vm *vm = NULL;
  177. nvkm_vm_ref(priv->vm, &vm, NULL);
  178. man->priv = vm;
  179. return 0;
  180. }
  181. static int
  182. nv04_gart_manager_fini(struct ttm_mem_type_manager *man)
  183. {
  184. struct nvkm_vm *vm = man->priv;
  185. nvkm_vm_ref(NULL, &vm, NULL);
  186. man->priv = NULL;
  187. return 0;
  188. }
  189. static void
  190. nv04_gart_manager_del(struct ttm_mem_type_manager *man, struct ttm_mem_reg *mem)
  191. {
  192. struct nvkm_mem *node = mem->mm_node;
  193. if (node->vma[0].node)
  194. nvkm_vm_put(&node->vma[0]);
  195. kfree(mem->mm_node);
  196. mem->mm_node = NULL;
  197. }
  198. static int
  199. nv04_gart_manager_new(struct ttm_mem_type_manager *man,
  200. struct ttm_buffer_object *bo,
  201. const struct ttm_place *place,
  202. struct ttm_mem_reg *mem)
  203. {
  204. struct nvkm_mem *node;
  205. int ret;
  206. node = kzalloc(sizeof(*node), GFP_KERNEL);
  207. if (!node)
  208. return -ENOMEM;
  209. node->page_shift = 12;
  210. ret = nvkm_vm_get(man->priv, mem->num_pages << 12, node->page_shift,
  211. NV_MEM_ACCESS_RW, &node->vma[0]);
  212. if (ret) {
  213. kfree(node);
  214. return ret;
  215. }
  216. mem->mm_node = node;
  217. mem->start = node->vma[0].offset >> PAGE_SHIFT;
  218. return 0;
  219. }
  220. static void
  221. nv04_gart_manager_debug(struct ttm_mem_type_manager *man, const char *prefix)
  222. {
  223. }
  224. const struct ttm_mem_type_manager_func nv04_gart_manager = {
  225. .init = nv04_gart_manager_init,
  226. .takedown = nv04_gart_manager_fini,
  227. .get_node = nv04_gart_manager_new,
  228. .put_node = nv04_gart_manager_del,
  229. .debug = nv04_gart_manager_debug
  230. };
  231. int
  232. nouveau_ttm_mmap(struct file *filp, struct vm_area_struct *vma)
  233. {
  234. struct drm_file *file_priv = filp->private_data;
  235. struct nouveau_drm *drm = nouveau_drm(file_priv->minor->dev);
  236. if (unlikely(vma->vm_pgoff < DRM_FILE_PAGE_OFFSET))
  237. return drm_legacy_mmap(filp, vma);
  238. return ttm_bo_mmap(filp, vma, &drm->ttm.bdev);
  239. }
  240. static int
  241. nouveau_ttm_mem_global_init(struct drm_global_reference *ref)
  242. {
  243. return ttm_mem_global_init(ref->object);
  244. }
  245. static void
  246. nouveau_ttm_mem_global_release(struct drm_global_reference *ref)
  247. {
  248. ttm_mem_global_release(ref->object);
  249. }
  250. int
  251. nouveau_ttm_global_init(struct nouveau_drm *drm)
  252. {
  253. struct drm_global_reference *global_ref;
  254. int ret;
  255. global_ref = &drm->ttm.mem_global_ref;
  256. global_ref->global_type = DRM_GLOBAL_TTM_MEM;
  257. global_ref->size = sizeof(struct ttm_mem_global);
  258. global_ref->init = &nouveau_ttm_mem_global_init;
  259. global_ref->release = &nouveau_ttm_mem_global_release;
  260. ret = drm_global_item_ref(global_ref);
  261. if (unlikely(ret != 0)) {
  262. DRM_ERROR("Failed setting up TTM memory accounting\n");
  263. drm->ttm.mem_global_ref.release = NULL;
  264. return ret;
  265. }
  266. drm->ttm.bo_global_ref.mem_glob = global_ref->object;
  267. global_ref = &drm->ttm.bo_global_ref.ref;
  268. global_ref->global_type = DRM_GLOBAL_TTM_BO;
  269. global_ref->size = sizeof(struct ttm_bo_global);
  270. global_ref->init = &ttm_bo_global_init;
  271. global_ref->release = &ttm_bo_global_release;
  272. ret = drm_global_item_ref(global_ref);
  273. if (unlikely(ret != 0)) {
  274. DRM_ERROR("Failed setting up TTM BO subsystem\n");
  275. drm_global_item_unref(&drm->ttm.mem_global_ref);
  276. drm->ttm.mem_global_ref.release = NULL;
  277. return ret;
  278. }
  279. return 0;
  280. }
  281. void
  282. nouveau_ttm_global_release(struct nouveau_drm *drm)
  283. {
  284. if (drm->ttm.mem_global_ref.release == NULL)
  285. return;
  286. drm_global_item_unref(&drm->ttm.bo_global_ref.ref);
  287. drm_global_item_unref(&drm->ttm.mem_global_ref);
  288. drm->ttm.mem_global_ref.release = NULL;
  289. }
  290. int
  291. nouveau_ttm_init(struct nouveau_drm *drm)
  292. {
  293. struct nvkm_device *device = nvxx_device(&drm->device);
  294. struct nvkm_pci *pci = device->pci;
  295. struct drm_device *dev = drm->dev;
  296. u8 bits;
  297. int ret;
  298. if (pci && pci->agp.bridge) {
  299. drm->agp.bridge = pci->agp.bridge;
  300. drm->agp.base = pci->agp.base;
  301. drm->agp.size = pci->agp.size;
  302. drm->agp.cma = pci->agp.cma;
  303. }
  304. bits = nvxx_mmu(&drm->device)->dma_bits;
  305. if (nvxx_device(&drm->device)->func->pci) {
  306. if (drm->agp.bridge)
  307. bits = 32;
  308. } else if (device->func->tegra) {
  309. struct nvkm_device_tegra *tegra = device->func->tegra(device);
  310. /*
  311. * If the platform can use a IOMMU, then the addressable DMA
  312. * space is constrained by the IOMMU bit
  313. */
  314. if (tegra->func->iommu_bit)
  315. bits = min(bits, tegra->func->iommu_bit);
  316. }
  317. ret = dma_set_mask(dev->dev, DMA_BIT_MASK(bits));
  318. if (ret && bits != 32) {
  319. bits = 32;
  320. ret = dma_set_mask(dev->dev, DMA_BIT_MASK(bits));
  321. }
  322. if (ret)
  323. return ret;
  324. ret = dma_set_coherent_mask(dev->dev, DMA_BIT_MASK(bits));
  325. if (ret)
  326. dma_set_coherent_mask(dev->dev, DMA_BIT_MASK(32));
  327. ret = nouveau_ttm_global_init(drm);
  328. if (ret)
  329. return ret;
  330. ret = ttm_bo_device_init(&drm->ttm.bdev,
  331. drm->ttm.bo_global_ref.ref.object,
  332. &nouveau_bo_driver,
  333. dev->anon_inode->i_mapping,
  334. DRM_FILE_PAGE_OFFSET,
  335. bits <= 32 ? true : false);
  336. if (ret) {
  337. NV_ERROR(drm, "error initialising bo driver, %d\n", ret);
  338. return ret;
  339. }
  340. /* VRAM init */
  341. drm->gem.vram_available = drm->device.info.ram_user;
  342. arch_io_reserve_memtype_wc(device->func->resource_addr(device, 1),
  343. device->func->resource_size(device, 1));
  344. ret = ttm_bo_init_mm(&drm->ttm.bdev, TTM_PL_VRAM,
  345. drm->gem.vram_available >> PAGE_SHIFT);
  346. if (ret) {
  347. NV_ERROR(drm, "VRAM mm init failed, %d\n", ret);
  348. return ret;
  349. }
  350. drm->ttm.mtrr = arch_phys_wc_add(device->func->resource_addr(device, 1),
  351. device->func->resource_size(device, 1));
  352. /* GART init */
  353. if (!drm->agp.bridge) {
  354. drm->gem.gart_available = nvxx_mmu(&drm->device)->limit;
  355. } else {
  356. drm->gem.gart_available = drm->agp.size;
  357. }
  358. ret = ttm_bo_init_mm(&drm->ttm.bdev, TTM_PL_TT,
  359. drm->gem.gart_available >> PAGE_SHIFT);
  360. if (ret) {
  361. NV_ERROR(drm, "GART mm init failed, %d\n", ret);
  362. return ret;
  363. }
  364. NV_INFO(drm, "VRAM: %d MiB\n", (u32)(drm->gem.vram_available >> 20));
  365. NV_INFO(drm, "GART: %d MiB\n", (u32)(drm->gem.gart_available >> 20));
  366. return 0;
  367. }
  368. void
  369. nouveau_ttm_fini(struct nouveau_drm *drm)
  370. {
  371. struct nvkm_device *device = nvxx_device(&drm->device);
  372. ttm_bo_clean_mm(&drm->ttm.bdev, TTM_PL_VRAM);
  373. ttm_bo_clean_mm(&drm->ttm.bdev, TTM_PL_TT);
  374. ttm_bo_device_release(&drm->ttm.bdev);
  375. nouveau_ttm_global_release(drm);
  376. arch_phys_wc_del(drm->ttm.mtrr);
  377. drm->ttm.mtrr = 0;
  378. arch_io_free_memtype_wc(device->func->resource_addr(device, 1),
  379. device->func->resource_size(device, 1));
  380. }