nouveau_ttm.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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_drm.h"
  27. #include "nouveau_ttm.h"
  28. #include "nouveau_gem.h"
  29. #include "drm_legacy.h"
  30. static int
  31. nouveau_vram_manager_init(struct ttm_mem_type_manager *man, unsigned long psize)
  32. {
  33. struct nouveau_drm *drm = nouveau_bdev(man->bdev);
  34. struct nvkm_fb *pfb = nvxx_fb(&drm->device);
  35. man->priv = pfb;
  36. return 0;
  37. }
  38. static int
  39. nouveau_vram_manager_fini(struct ttm_mem_type_manager *man)
  40. {
  41. man->priv = NULL;
  42. return 0;
  43. }
  44. static inline void
  45. nvkm_mem_node_cleanup(struct nvkm_mem *node)
  46. {
  47. if (node->vma[0].node) {
  48. nvkm_vm_unmap(&node->vma[0]);
  49. nvkm_vm_put(&node->vma[0]);
  50. }
  51. if (node->vma[1].node) {
  52. nvkm_vm_unmap(&node->vma[1]);
  53. nvkm_vm_put(&node->vma[1]);
  54. }
  55. }
  56. static void
  57. nouveau_vram_manager_del(struct ttm_mem_type_manager *man,
  58. struct ttm_mem_reg *mem)
  59. {
  60. struct nouveau_drm *drm = nouveau_bdev(man->bdev);
  61. struct nvkm_fb *pfb = nvxx_fb(&drm->device);
  62. nvkm_mem_node_cleanup(mem->mm_node);
  63. pfb->ram->put(pfb, (struct nvkm_mem **)&mem->mm_node);
  64. }
  65. static int
  66. nouveau_vram_manager_new(struct ttm_mem_type_manager *man,
  67. struct ttm_buffer_object *bo,
  68. const struct ttm_place *place,
  69. struct ttm_mem_reg *mem)
  70. {
  71. struct nouveau_drm *drm = nouveau_bdev(man->bdev);
  72. struct nvkm_fb *pfb = nvxx_fb(&drm->device);
  73. struct nouveau_bo *nvbo = nouveau_bo(bo);
  74. struct nvkm_mem *node;
  75. u32 size_nc = 0;
  76. int ret;
  77. if (drm->device.info.ram_size == 0)
  78. return -ENOMEM;
  79. if (nvbo->tile_flags & NOUVEAU_GEM_TILE_NONCONTIG)
  80. size_nc = 1 << nvbo->page_shift;
  81. ret = pfb->ram->get(pfb, mem->num_pages << PAGE_SHIFT,
  82. mem->page_alignment << PAGE_SHIFT, size_nc,
  83. (nvbo->tile_flags >> 8) & 0x3ff, &node);
  84. if (ret) {
  85. mem->mm_node = NULL;
  86. return (ret == -ENOSPC) ? 0 : ret;
  87. }
  88. node->page_shift = nvbo->page_shift;
  89. mem->mm_node = node;
  90. mem->start = node->offset >> PAGE_SHIFT;
  91. return 0;
  92. }
  93. static void
  94. nouveau_vram_manager_debug(struct ttm_mem_type_manager *man, const char *prefix)
  95. {
  96. struct nvkm_fb *pfb = man->priv;
  97. struct nvkm_mm *mm = &pfb->vram;
  98. struct nvkm_mm_node *r;
  99. u32 total = 0, free = 0;
  100. mutex_lock(&nv_subdev(pfb)->mutex);
  101. list_for_each_entry(r, &mm->nodes, nl_entry) {
  102. printk(KERN_DEBUG "%s %d: 0x%010llx 0x%010llx\n",
  103. prefix, r->type, ((u64)r->offset << 12),
  104. (((u64)r->offset + r->length) << 12));
  105. total += r->length;
  106. if (!r->type)
  107. free += r->length;
  108. }
  109. mutex_unlock(&nv_subdev(pfb)->mutex);
  110. printk(KERN_DEBUG "%s total: 0x%010llx free: 0x%010llx\n",
  111. prefix, (u64)total << 12, (u64)free << 12);
  112. printk(KERN_DEBUG "%s block: 0x%08x\n",
  113. prefix, mm->block_size << 12);
  114. }
  115. const struct ttm_mem_type_manager_func nouveau_vram_manager = {
  116. nouveau_vram_manager_init,
  117. nouveau_vram_manager_fini,
  118. nouveau_vram_manager_new,
  119. nouveau_vram_manager_del,
  120. nouveau_vram_manager_debug
  121. };
  122. static int
  123. nouveau_gart_manager_init(struct ttm_mem_type_manager *man, unsigned long psize)
  124. {
  125. return 0;
  126. }
  127. static int
  128. nouveau_gart_manager_fini(struct ttm_mem_type_manager *man)
  129. {
  130. return 0;
  131. }
  132. static void
  133. nouveau_gart_manager_del(struct ttm_mem_type_manager *man,
  134. struct ttm_mem_reg *mem)
  135. {
  136. nvkm_mem_node_cleanup(mem->mm_node);
  137. kfree(mem->mm_node);
  138. mem->mm_node = NULL;
  139. }
  140. static int
  141. nouveau_gart_manager_new(struct ttm_mem_type_manager *man,
  142. struct ttm_buffer_object *bo,
  143. const struct ttm_place *place,
  144. struct ttm_mem_reg *mem)
  145. {
  146. struct nouveau_drm *drm = nouveau_bdev(bo->bdev);
  147. struct nouveau_bo *nvbo = nouveau_bo(bo);
  148. struct nvkm_mem *node;
  149. node = kzalloc(sizeof(*node), GFP_KERNEL);
  150. if (!node)
  151. return -ENOMEM;
  152. node->page_shift = 12;
  153. switch (drm->device.info.family) {
  154. case NV_DEVICE_INFO_V0_TESLA:
  155. if (drm->device.info.chipset != 0x50)
  156. node->memtype = (nvbo->tile_flags & 0x7f00) >> 8;
  157. break;
  158. case NV_DEVICE_INFO_V0_FERMI:
  159. case NV_DEVICE_INFO_V0_KEPLER:
  160. node->memtype = (nvbo->tile_flags & 0xff00) >> 8;
  161. break;
  162. default:
  163. break;
  164. }
  165. mem->mm_node = node;
  166. mem->start = 0;
  167. return 0;
  168. }
  169. static void
  170. nouveau_gart_manager_debug(struct ttm_mem_type_manager *man, const char *prefix)
  171. {
  172. }
  173. const struct ttm_mem_type_manager_func nouveau_gart_manager = {
  174. nouveau_gart_manager_init,
  175. nouveau_gart_manager_fini,
  176. nouveau_gart_manager_new,
  177. nouveau_gart_manager_del,
  178. nouveau_gart_manager_debug
  179. };
  180. /*XXX*/
  181. #include <subdev/mmu/nv04.h>
  182. static int
  183. nv04_gart_manager_init(struct ttm_mem_type_manager *man, unsigned long psize)
  184. {
  185. struct nouveau_drm *drm = nouveau_bdev(man->bdev);
  186. struct nvkm_mmu *mmu = nvxx_mmu(&drm->device);
  187. struct nv04_mmu_priv *priv = (void *)mmu;
  188. struct nvkm_vm *vm = NULL;
  189. nvkm_vm_ref(priv->vm, &vm, NULL);
  190. man->priv = vm;
  191. return 0;
  192. }
  193. static int
  194. nv04_gart_manager_fini(struct ttm_mem_type_manager *man)
  195. {
  196. struct nvkm_vm *vm = man->priv;
  197. nvkm_vm_ref(NULL, &vm, NULL);
  198. man->priv = NULL;
  199. return 0;
  200. }
  201. static void
  202. nv04_gart_manager_del(struct ttm_mem_type_manager *man, struct ttm_mem_reg *mem)
  203. {
  204. struct nvkm_mem *node = mem->mm_node;
  205. if (node->vma[0].node)
  206. nvkm_vm_put(&node->vma[0]);
  207. kfree(mem->mm_node);
  208. mem->mm_node = NULL;
  209. }
  210. static int
  211. nv04_gart_manager_new(struct ttm_mem_type_manager *man,
  212. struct ttm_buffer_object *bo,
  213. const struct ttm_place *place,
  214. struct ttm_mem_reg *mem)
  215. {
  216. struct nvkm_mem *node;
  217. int ret;
  218. node = kzalloc(sizeof(*node), GFP_KERNEL);
  219. if (!node)
  220. return -ENOMEM;
  221. node->page_shift = 12;
  222. ret = nvkm_vm_get(man->priv, mem->num_pages << 12, node->page_shift,
  223. NV_MEM_ACCESS_RW, &node->vma[0]);
  224. if (ret) {
  225. kfree(node);
  226. return ret;
  227. }
  228. mem->mm_node = node;
  229. mem->start = node->vma[0].offset >> PAGE_SHIFT;
  230. return 0;
  231. }
  232. static void
  233. nv04_gart_manager_debug(struct ttm_mem_type_manager *man, const char *prefix)
  234. {
  235. }
  236. const struct ttm_mem_type_manager_func nv04_gart_manager = {
  237. nv04_gart_manager_init,
  238. nv04_gart_manager_fini,
  239. nv04_gart_manager_new,
  240. nv04_gart_manager_del,
  241. nv04_gart_manager_debug
  242. };
  243. int
  244. nouveau_ttm_mmap(struct file *filp, struct vm_area_struct *vma)
  245. {
  246. struct drm_file *file_priv = filp->private_data;
  247. struct nouveau_drm *drm = nouveau_drm(file_priv->minor->dev);
  248. if (unlikely(vma->vm_pgoff < DRM_FILE_PAGE_OFFSET))
  249. return drm_legacy_mmap(filp, vma);
  250. return ttm_bo_mmap(filp, vma, &drm->ttm.bdev);
  251. }
  252. static int
  253. nouveau_ttm_mem_global_init(struct drm_global_reference *ref)
  254. {
  255. return ttm_mem_global_init(ref->object);
  256. }
  257. static void
  258. nouveau_ttm_mem_global_release(struct drm_global_reference *ref)
  259. {
  260. ttm_mem_global_release(ref->object);
  261. }
  262. int
  263. nouveau_ttm_global_init(struct nouveau_drm *drm)
  264. {
  265. struct drm_global_reference *global_ref;
  266. int ret;
  267. global_ref = &drm->ttm.mem_global_ref;
  268. global_ref->global_type = DRM_GLOBAL_TTM_MEM;
  269. global_ref->size = sizeof(struct ttm_mem_global);
  270. global_ref->init = &nouveau_ttm_mem_global_init;
  271. global_ref->release = &nouveau_ttm_mem_global_release;
  272. ret = drm_global_item_ref(global_ref);
  273. if (unlikely(ret != 0)) {
  274. DRM_ERROR("Failed setting up TTM memory accounting\n");
  275. drm->ttm.mem_global_ref.release = NULL;
  276. return ret;
  277. }
  278. drm->ttm.bo_global_ref.mem_glob = global_ref->object;
  279. global_ref = &drm->ttm.bo_global_ref.ref;
  280. global_ref->global_type = DRM_GLOBAL_TTM_BO;
  281. global_ref->size = sizeof(struct ttm_bo_global);
  282. global_ref->init = &ttm_bo_global_init;
  283. global_ref->release = &ttm_bo_global_release;
  284. ret = drm_global_item_ref(global_ref);
  285. if (unlikely(ret != 0)) {
  286. DRM_ERROR("Failed setting up TTM BO subsystem\n");
  287. drm_global_item_unref(&drm->ttm.mem_global_ref);
  288. drm->ttm.mem_global_ref.release = NULL;
  289. return ret;
  290. }
  291. return 0;
  292. }
  293. void
  294. nouveau_ttm_global_release(struct nouveau_drm *drm)
  295. {
  296. if (drm->ttm.mem_global_ref.release == NULL)
  297. return;
  298. drm_global_item_unref(&drm->ttm.bo_global_ref.ref);
  299. drm_global_item_unref(&drm->ttm.mem_global_ref);
  300. drm->ttm.mem_global_ref.release = NULL;
  301. }
  302. int
  303. nouveau_ttm_init(struct nouveau_drm *drm)
  304. {
  305. struct drm_device *dev = drm->dev;
  306. u32 bits;
  307. int ret;
  308. bits = nvxx_mmu(&drm->device)->dma_bits;
  309. if (nv_device_is_pci(nvxx_device(&drm->device))) {
  310. if (drm->agp.stat == ENABLED ||
  311. !pci_dma_supported(dev->pdev, DMA_BIT_MASK(bits)))
  312. bits = 32;
  313. ret = pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(bits));
  314. if (ret)
  315. return ret;
  316. ret = pci_set_consistent_dma_mask(dev->pdev,
  317. DMA_BIT_MASK(bits));
  318. if (ret)
  319. pci_set_consistent_dma_mask(dev->pdev,
  320. DMA_BIT_MASK(32));
  321. }
  322. ret = nouveau_ttm_global_init(drm);
  323. if (ret)
  324. return ret;
  325. ret = ttm_bo_device_init(&drm->ttm.bdev,
  326. drm->ttm.bo_global_ref.ref.object,
  327. &nouveau_bo_driver,
  328. dev->anon_inode->i_mapping,
  329. DRM_FILE_PAGE_OFFSET,
  330. bits <= 32 ? true : false);
  331. if (ret) {
  332. NV_ERROR(drm, "error initialising bo driver, %d\n", ret);
  333. return ret;
  334. }
  335. /* VRAM init */
  336. drm->gem.vram_available = drm->device.info.ram_user;
  337. ret = ttm_bo_init_mm(&drm->ttm.bdev, TTM_PL_VRAM,
  338. drm->gem.vram_available >> PAGE_SHIFT);
  339. if (ret) {
  340. NV_ERROR(drm, "VRAM mm init failed, %d\n", ret);
  341. return ret;
  342. }
  343. drm->ttm.mtrr = arch_phys_wc_add(nv_device_resource_start(nvxx_device(&drm->device), 1),
  344. nv_device_resource_len(nvxx_device(&drm->device), 1));
  345. /* GART init */
  346. if (drm->agp.stat != ENABLED) {
  347. drm->gem.gart_available = nvxx_mmu(&drm->device)->limit;
  348. } else {
  349. drm->gem.gart_available = drm->agp.size;
  350. }
  351. ret = ttm_bo_init_mm(&drm->ttm.bdev, TTM_PL_TT,
  352. drm->gem.gart_available >> PAGE_SHIFT);
  353. if (ret) {
  354. NV_ERROR(drm, "GART mm init failed, %d\n", ret);
  355. return ret;
  356. }
  357. NV_INFO(drm, "VRAM: %d MiB\n", (u32)(drm->gem.vram_available >> 20));
  358. NV_INFO(drm, "GART: %d MiB\n", (u32)(drm->gem.gart_available >> 20));
  359. return 0;
  360. }
  361. void
  362. nouveau_ttm_fini(struct nouveau_drm *drm)
  363. {
  364. mutex_lock(&drm->dev->struct_mutex);
  365. ttm_bo_clean_mm(&drm->ttm.bdev, TTM_PL_VRAM);
  366. ttm_bo_clean_mm(&drm->ttm.bdev, TTM_PL_TT);
  367. mutex_unlock(&drm->dev->struct_mutex);
  368. ttm_bo_device_release(&drm->ttm.bdev);
  369. nouveau_ttm_global_release(drm);
  370. arch_phys_wc_del(drm->ttm.mtrr);
  371. drm->ttm.mtrr = 0;
  372. }