ast_ttm.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. /*
  2. * Copyright 2012 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
  6. * "Software"), to deal in the Software without restriction, including
  7. * without limitation the rights to use, copy, modify, merge, publish,
  8. * distribute, sub license, and/or sell copies of the Software, and to
  9. * permit persons to whom the Software is furnished to do so, subject to
  10. * the following conditions:
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  15. * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  16. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  17. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  18. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  19. *
  20. * The above copyright notice and this permission notice (including the
  21. * next paragraph) shall be included in all copies or substantial portions
  22. * of the Software.
  23. *
  24. */
  25. /*
  26. * Authors: Dave Airlie <airlied@redhat.com>
  27. */
  28. #include <drm/drmP.h>
  29. #include "ast_drv.h"
  30. #include <ttm/ttm_page_alloc.h>
  31. static inline struct ast_private *
  32. ast_bdev(struct ttm_bo_device *bd)
  33. {
  34. return container_of(bd, struct ast_private, ttm.bdev);
  35. }
  36. static int
  37. ast_ttm_mem_global_init(struct drm_global_reference *ref)
  38. {
  39. return ttm_mem_global_init(ref->object);
  40. }
  41. static void
  42. ast_ttm_mem_global_release(struct drm_global_reference *ref)
  43. {
  44. ttm_mem_global_release(ref->object);
  45. }
  46. static int ast_ttm_global_init(struct ast_private *ast)
  47. {
  48. struct drm_global_reference *global_ref;
  49. int r;
  50. global_ref = &ast->ttm.mem_global_ref;
  51. global_ref->global_type = DRM_GLOBAL_TTM_MEM;
  52. global_ref->size = sizeof(struct ttm_mem_global);
  53. global_ref->init = &ast_ttm_mem_global_init;
  54. global_ref->release = &ast_ttm_mem_global_release;
  55. r = drm_global_item_ref(global_ref);
  56. if (r != 0) {
  57. DRM_ERROR("Failed setting up TTM memory accounting "
  58. "subsystem.\n");
  59. return r;
  60. }
  61. ast->ttm.bo_global_ref.mem_glob =
  62. ast->ttm.mem_global_ref.object;
  63. global_ref = &ast->ttm.bo_global_ref.ref;
  64. global_ref->global_type = DRM_GLOBAL_TTM_BO;
  65. global_ref->size = sizeof(struct ttm_bo_global);
  66. global_ref->init = &ttm_bo_global_init;
  67. global_ref->release = &ttm_bo_global_release;
  68. r = drm_global_item_ref(global_ref);
  69. if (r != 0) {
  70. DRM_ERROR("Failed setting up TTM BO subsystem.\n");
  71. drm_global_item_unref(&ast->ttm.mem_global_ref);
  72. return r;
  73. }
  74. return 0;
  75. }
  76. static void
  77. ast_ttm_global_release(struct ast_private *ast)
  78. {
  79. if (ast->ttm.mem_global_ref.release == NULL)
  80. return;
  81. drm_global_item_unref(&ast->ttm.bo_global_ref.ref);
  82. drm_global_item_unref(&ast->ttm.mem_global_ref);
  83. ast->ttm.mem_global_ref.release = NULL;
  84. }
  85. static void ast_bo_ttm_destroy(struct ttm_buffer_object *tbo)
  86. {
  87. struct ast_bo *bo;
  88. bo = container_of(tbo, struct ast_bo, bo);
  89. drm_gem_object_release(&bo->gem);
  90. kfree(bo);
  91. }
  92. static bool ast_ttm_bo_is_ast_bo(struct ttm_buffer_object *bo)
  93. {
  94. if (bo->destroy == &ast_bo_ttm_destroy)
  95. return true;
  96. return false;
  97. }
  98. static int
  99. ast_bo_init_mem_type(struct ttm_bo_device *bdev, uint32_t type,
  100. struct ttm_mem_type_manager *man)
  101. {
  102. switch (type) {
  103. case TTM_PL_SYSTEM:
  104. man->flags = TTM_MEMTYPE_FLAG_MAPPABLE;
  105. man->available_caching = TTM_PL_MASK_CACHING;
  106. man->default_caching = TTM_PL_FLAG_CACHED;
  107. break;
  108. case TTM_PL_VRAM:
  109. man->func = &ttm_bo_manager_func;
  110. man->flags = TTM_MEMTYPE_FLAG_FIXED |
  111. TTM_MEMTYPE_FLAG_MAPPABLE;
  112. man->available_caching = TTM_PL_FLAG_UNCACHED |
  113. TTM_PL_FLAG_WC;
  114. man->default_caching = TTM_PL_FLAG_WC;
  115. break;
  116. default:
  117. DRM_ERROR("Unsupported memory type %u\n", (unsigned)type);
  118. return -EINVAL;
  119. }
  120. return 0;
  121. }
  122. static void
  123. ast_bo_evict_flags(struct ttm_buffer_object *bo, struct ttm_placement *pl)
  124. {
  125. struct ast_bo *astbo = ast_bo(bo);
  126. if (!ast_ttm_bo_is_ast_bo(bo))
  127. return;
  128. ast_ttm_placement(astbo, TTM_PL_FLAG_SYSTEM);
  129. *pl = astbo->placement;
  130. }
  131. static int ast_bo_verify_access(struct ttm_buffer_object *bo, struct file *filp)
  132. {
  133. struct ast_bo *astbo = ast_bo(bo);
  134. return drm_vma_node_verify_access(&astbo->gem.vma_node, filp);
  135. }
  136. static int ast_ttm_io_mem_reserve(struct ttm_bo_device *bdev,
  137. struct ttm_mem_reg *mem)
  138. {
  139. struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
  140. struct ast_private *ast = ast_bdev(bdev);
  141. mem->bus.addr = NULL;
  142. mem->bus.offset = 0;
  143. mem->bus.size = mem->num_pages << PAGE_SHIFT;
  144. mem->bus.base = 0;
  145. mem->bus.is_iomem = false;
  146. if (!(man->flags & TTM_MEMTYPE_FLAG_MAPPABLE))
  147. return -EINVAL;
  148. switch (mem->mem_type) {
  149. case TTM_PL_SYSTEM:
  150. /* system memory */
  151. return 0;
  152. case TTM_PL_VRAM:
  153. mem->bus.offset = mem->start << PAGE_SHIFT;
  154. mem->bus.base = pci_resource_start(ast->dev->pdev, 0);
  155. mem->bus.is_iomem = true;
  156. break;
  157. default:
  158. return -EINVAL;
  159. break;
  160. }
  161. return 0;
  162. }
  163. static void ast_ttm_io_mem_free(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
  164. {
  165. }
  166. static int ast_bo_move(struct ttm_buffer_object *bo,
  167. bool evict, bool interruptible,
  168. bool no_wait_gpu,
  169. struct ttm_mem_reg *new_mem)
  170. {
  171. int r;
  172. r = ttm_bo_move_memcpy(bo, evict, no_wait_gpu, new_mem);
  173. return r;
  174. }
  175. static void ast_ttm_backend_destroy(struct ttm_tt *tt)
  176. {
  177. ttm_tt_fini(tt);
  178. kfree(tt);
  179. }
  180. static struct ttm_backend_func ast_tt_backend_func = {
  181. .destroy = &ast_ttm_backend_destroy,
  182. };
  183. static struct ttm_tt *ast_ttm_tt_create(struct ttm_bo_device *bdev,
  184. unsigned long size, uint32_t page_flags,
  185. struct page *dummy_read_page)
  186. {
  187. struct ttm_tt *tt;
  188. tt = kzalloc(sizeof(struct ttm_tt), GFP_KERNEL);
  189. if (tt == NULL)
  190. return NULL;
  191. tt->func = &ast_tt_backend_func;
  192. if (ttm_tt_init(tt, bdev, size, page_flags, dummy_read_page)) {
  193. kfree(tt);
  194. return NULL;
  195. }
  196. return tt;
  197. }
  198. static int ast_ttm_tt_populate(struct ttm_tt *ttm)
  199. {
  200. return ttm_pool_populate(ttm);
  201. }
  202. static void ast_ttm_tt_unpopulate(struct ttm_tt *ttm)
  203. {
  204. ttm_pool_unpopulate(ttm);
  205. }
  206. struct ttm_bo_driver ast_bo_driver = {
  207. .ttm_tt_create = ast_ttm_tt_create,
  208. .ttm_tt_populate = ast_ttm_tt_populate,
  209. .ttm_tt_unpopulate = ast_ttm_tt_unpopulate,
  210. .init_mem_type = ast_bo_init_mem_type,
  211. .evict_flags = ast_bo_evict_flags,
  212. .move = ast_bo_move,
  213. .verify_access = ast_bo_verify_access,
  214. .io_mem_reserve = &ast_ttm_io_mem_reserve,
  215. .io_mem_free = &ast_ttm_io_mem_free,
  216. };
  217. int ast_mm_init(struct ast_private *ast)
  218. {
  219. int ret;
  220. struct drm_device *dev = ast->dev;
  221. struct ttm_bo_device *bdev = &ast->ttm.bdev;
  222. ret = ast_ttm_global_init(ast);
  223. if (ret)
  224. return ret;
  225. ret = ttm_bo_device_init(&ast->ttm.bdev,
  226. ast->ttm.bo_global_ref.ref.object,
  227. &ast_bo_driver,
  228. dev->anon_inode->i_mapping,
  229. DRM_FILE_PAGE_OFFSET,
  230. true);
  231. if (ret) {
  232. DRM_ERROR("Error initialising bo driver; %d\n", ret);
  233. return ret;
  234. }
  235. ret = ttm_bo_init_mm(bdev, TTM_PL_VRAM,
  236. ast->vram_size >> PAGE_SHIFT);
  237. if (ret) {
  238. DRM_ERROR("Failed ttm VRAM init: %d\n", ret);
  239. return ret;
  240. }
  241. arch_io_reserve_memtype_wc(pci_resource_start(dev->pdev, 0),
  242. pci_resource_len(dev->pdev, 0));
  243. ast->fb_mtrr = arch_phys_wc_add(pci_resource_start(dev->pdev, 0),
  244. pci_resource_len(dev->pdev, 0));
  245. return 0;
  246. }
  247. void ast_mm_fini(struct ast_private *ast)
  248. {
  249. struct drm_device *dev = ast->dev;
  250. ttm_bo_device_release(&ast->ttm.bdev);
  251. ast_ttm_global_release(ast);
  252. arch_phys_wc_del(ast->fb_mtrr);
  253. arch_io_free_memtype_wc(pci_resource_start(dev->pdev, 0),
  254. pci_resource_len(dev->pdev, 0));
  255. }
  256. void ast_ttm_placement(struct ast_bo *bo, int domain)
  257. {
  258. u32 c = 0;
  259. unsigned i;
  260. bo->placement.placement = bo->placements;
  261. bo->placement.busy_placement = bo->placements;
  262. if (domain & TTM_PL_FLAG_VRAM)
  263. bo->placements[c++].flags = TTM_PL_FLAG_WC | TTM_PL_FLAG_UNCACHED | TTM_PL_FLAG_VRAM;
  264. if (domain & TTM_PL_FLAG_SYSTEM)
  265. bo->placements[c++].flags = TTM_PL_FLAG_CACHED | TTM_PL_FLAG_SYSTEM;
  266. if (!c)
  267. bo->placements[c++].flags = TTM_PL_FLAG_CACHED | TTM_PL_FLAG_SYSTEM;
  268. bo->placement.num_placement = c;
  269. bo->placement.num_busy_placement = c;
  270. for (i = 0; i < c; ++i) {
  271. bo->placements[i].fpfn = 0;
  272. bo->placements[i].lpfn = 0;
  273. }
  274. }
  275. int ast_bo_create(struct drm_device *dev, int size, int align,
  276. uint32_t flags, struct ast_bo **pastbo)
  277. {
  278. struct ast_private *ast = dev->dev_private;
  279. struct ast_bo *astbo;
  280. size_t acc_size;
  281. int ret;
  282. astbo = kzalloc(sizeof(struct ast_bo), GFP_KERNEL);
  283. if (!astbo)
  284. return -ENOMEM;
  285. ret = drm_gem_object_init(dev, &astbo->gem, size);
  286. if (ret) {
  287. kfree(astbo);
  288. return ret;
  289. }
  290. astbo->bo.bdev = &ast->ttm.bdev;
  291. ast_ttm_placement(astbo, TTM_PL_FLAG_VRAM | TTM_PL_FLAG_SYSTEM);
  292. acc_size = ttm_bo_dma_acc_size(&ast->ttm.bdev, size,
  293. sizeof(struct ast_bo));
  294. ret = ttm_bo_init(&ast->ttm.bdev, &astbo->bo, size,
  295. ttm_bo_type_device, &astbo->placement,
  296. align >> PAGE_SHIFT, false, NULL, acc_size,
  297. NULL, NULL, ast_bo_ttm_destroy);
  298. if (ret)
  299. return ret;
  300. *pastbo = astbo;
  301. return 0;
  302. }
  303. static inline u64 ast_bo_gpu_offset(struct ast_bo *bo)
  304. {
  305. return bo->bo.offset;
  306. }
  307. int ast_bo_pin(struct ast_bo *bo, u32 pl_flag, u64 *gpu_addr)
  308. {
  309. int i, ret;
  310. if (bo->pin_count) {
  311. bo->pin_count++;
  312. if (gpu_addr)
  313. *gpu_addr = ast_bo_gpu_offset(bo);
  314. }
  315. ast_ttm_placement(bo, pl_flag);
  316. for (i = 0; i < bo->placement.num_placement; i++)
  317. bo->placements[i].flags |= TTM_PL_FLAG_NO_EVICT;
  318. ret = ttm_bo_validate(&bo->bo, &bo->placement, false, false);
  319. if (ret)
  320. return ret;
  321. bo->pin_count = 1;
  322. if (gpu_addr)
  323. *gpu_addr = ast_bo_gpu_offset(bo);
  324. return 0;
  325. }
  326. int ast_bo_unpin(struct ast_bo *bo)
  327. {
  328. int i, ret;
  329. if (!bo->pin_count) {
  330. DRM_ERROR("unpin bad %p\n", bo);
  331. return 0;
  332. }
  333. bo->pin_count--;
  334. if (bo->pin_count)
  335. return 0;
  336. for (i = 0; i < bo->placement.num_placement ; i++)
  337. bo->placements[i].flags &= ~TTM_PL_FLAG_NO_EVICT;
  338. ret = ttm_bo_validate(&bo->bo, &bo->placement, false, false);
  339. if (ret)
  340. return ret;
  341. return 0;
  342. }
  343. int ast_bo_push_sysram(struct ast_bo *bo)
  344. {
  345. int i, ret;
  346. if (!bo->pin_count) {
  347. DRM_ERROR("unpin bad %p\n", bo);
  348. return 0;
  349. }
  350. bo->pin_count--;
  351. if (bo->pin_count)
  352. return 0;
  353. if (bo->kmap.virtual)
  354. ttm_bo_kunmap(&bo->kmap);
  355. ast_ttm_placement(bo, TTM_PL_FLAG_SYSTEM);
  356. for (i = 0; i < bo->placement.num_placement ; i++)
  357. bo->placements[i].flags |= TTM_PL_FLAG_NO_EVICT;
  358. ret = ttm_bo_validate(&bo->bo, &bo->placement, false, false);
  359. if (ret) {
  360. DRM_ERROR("pushing to VRAM failed\n");
  361. return ret;
  362. }
  363. return 0;
  364. }
  365. int ast_mmap(struct file *filp, struct vm_area_struct *vma)
  366. {
  367. struct drm_file *file_priv;
  368. struct ast_private *ast;
  369. if (unlikely(vma->vm_pgoff < DRM_FILE_PAGE_OFFSET))
  370. return -EINVAL;
  371. file_priv = filp->private_data;
  372. ast = file_priv->minor->dev->dev_private;
  373. return ttm_bo_mmap(filp, vma, &ast->ttm.bdev);
  374. }