cirrus_ttm.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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 <drm/ttm/ttm_page_alloc.h>
  30. #include "cirrus_drv.h"
  31. static inline struct cirrus_device *
  32. cirrus_bdev(struct ttm_bo_device *bd)
  33. {
  34. return container_of(bd, struct cirrus_device, ttm.bdev);
  35. }
  36. static int
  37. cirrus_ttm_mem_global_init(struct drm_global_reference *ref)
  38. {
  39. return ttm_mem_global_init(ref->object);
  40. }
  41. static void
  42. cirrus_ttm_mem_global_release(struct drm_global_reference *ref)
  43. {
  44. ttm_mem_global_release(ref->object);
  45. }
  46. static int cirrus_ttm_global_init(struct cirrus_device *cirrus)
  47. {
  48. struct drm_global_reference *global_ref;
  49. int r;
  50. global_ref = &cirrus->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 = &cirrus_ttm_mem_global_init;
  54. global_ref->release = &cirrus_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. cirrus->ttm.bo_global_ref.mem_glob =
  62. cirrus->ttm.mem_global_ref.object;
  63. global_ref = &cirrus->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(&cirrus->ttm.mem_global_ref);
  72. return r;
  73. }
  74. return 0;
  75. }
  76. static void
  77. cirrus_ttm_global_release(struct cirrus_device *cirrus)
  78. {
  79. if (cirrus->ttm.mem_global_ref.release == NULL)
  80. return;
  81. drm_global_item_unref(&cirrus->ttm.bo_global_ref.ref);
  82. drm_global_item_unref(&cirrus->ttm.mem_global_ref);
  83. cirrus->ttm.mem_global_ref.release = NULL;
  84. }
  85. static void cirrus_bo_ttm_destroy(struct ttm_buffer_object *tbo)
  86. {
  87. struct cirrus_bo *bo;
  88. bo = container_of(tbo, struct cirrus_bo, bo);
  89. drm_gem_object_release(&bo->gem);
  90. kfree(bo);
  91. }
  92. static bool cirrus_ttm_bo_is_cirrus_bo(struct ttm_buffer_object *bo)
  93. {
  94. if (bo->destroy == &cirrus_bo_ttm_destroy)
  95. return true;
  96. return false;
  97. }
  98. static int
  99. cirrus_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. cirrus_bo_evict_flags(struct ttm_buffer_object *bo, struct ttm_placement *pl)
  124. {
  125. struct cirrus_bo *cirrusbo = cirrus_bo(bo);
  126. if (!cirrus_ttm_bo_is_cirrus_bo(bo))
  127. return;
  128. cirrus_ttm_placement(cirrusbo, TTM_PL_FLAG_SYSTEM);
  129. *pl = cirrusbo->placement;
  130. }
  131. static int cirrus_bo_verify_access(struct ttm_buffer_object *bo, struct file *filp)
  132. {
  133. struct cirrus_bo *cirrusbo = cirrus_bo(bo);
  134. return drm_vma_node_verify_access(&cirrusbo->gem.vma_node,
  135. filp->private_data);
  136. }
  137. static int cirrus_ttm_io_mem_reserve(struct ttm_bo_device *bdev,
  138. struct ttm_mem_reg *mem)
  139. {
  140. struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
  141. struct cirrus_device *cirrus = cirrus_bdev(bdev);
  142. mem->bus.addr = NULL;
  143. mem->bus.offset = 0;
  144. mem->bus.size = mem->num_pages << PAGE_SHIFT;
  145. mem->bus.base = 0;
  146. mem->bus.is_iomem = false;
  147. if (!(man->flags & TTM_MEMTYPE_FLAG_MAPPABLE))
  148. return -EINVAL;
  149. switch (mem->mem_type) {
  150. case TTM_PL_SYSTEM:
  151. /* system memory */
  152. return 0;
  153. case TTM_PL_VRAM:
  154. mem->bus.offset = mem->start << PAGE_SHIFT;
  155. mem->bus.base = pci_resource_start(cirrus->dev->pdev, 0);
  156. mem->bus.is_iomem = true;
  157. break;
  158. default:
  159. return -EINVAL;
  160. break;
  161. }
  162. return 0;
  163. }
  164. static void cirrus_ttm_io_mem_free(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
  165. {
  166. }
  167. static void cirrus_ttm_backend_destroy(struct ttm_tt *tt)
  168. {
  169. ttm_tt_fini(tt);
  170. kfree(tt);
  171. }
  172. static struct ttm_backend_func cirrus_tt_backend_func = {
  173. .destroy = &cirrus_ttm_backend_destroy,
  174. };
  175. static struct ttm_tt *cirrus_ttm_tt_create(struct ttm_buffer_object *bo,
  176. uint32_t page_flags)
  177. {
  178. struct ttm_tt *tt;
  179. tt = kzalloc(sizeof(struct ttm_tt), GFP_KERNEL);
  180. if (tt == NULL)
  181. return NULL;
  182. tt->func = &cirrus_tt_backend_func;
  183. if (ttm_tt_init(tt, bo, page_flags)) {
  184. kfree(tt);
  185. return NULL;
  186. }
  187. return tt;
  188. }
  189. struct ttm_bo_driver cirrus_bo_driver = {
  190. .ttm_tt_create = cirrus_ttm_tt_create,
  191. .init_mem_type = cirrus_bo_init_mem_type,
  192. .eviction_valuable = ttm_bo_eviction_valuable,
  193. .evict_flags = cirrus_bo_evict_flags,
  194. .move = NULL,
  195. .verify_access = cirrus_bo_verify_access,
  196. .io_mem_reserve = &cirrus_ttm_io_mem_reserve,
  197. .io_mem_free = &cirrus_ttm_io_mem_free,
  198. };
  199. int cirrus_mm_init(struct cirrus_device *cirrus)
  200. {
  201. int ret;
  202. struct drm_device *dev = cirrus->dev;
  203. struct ttm_bo_device *bdev = &cirrus->ttm.bdev;
  204. ret = cirrus_ttm_global_init(cirrus);
  205. if (ret)
  206. return ret;
  207. ret = ttm_bo_device_init(&cirrus->ttm.bdev,
  208. cirrus->ttm.bo_global_ref.ref.object,
  209. &cirrus_bo_driver,
  210. dev->anon_inode->i_mapping,
  211. DRM_FILE_PAGE_OFFSET,
  212. true);
  213. if (ret) {
  214. DRM_ERROR("Error initialising bo driver; %d\n", ret);
  215. return ret;
  216. }
  217. ret = ttm_bo_init_mm(bdev, TTM_PL_VRAM,
  218. cirrus->mc.vram_size >> PAGE_SHIFT);
  219. if (ret) {
  220. DRM_ERROR("Failed ttm VRAM init: %d\n", ret);
  221. return ret;
  222. }
  223. arch_io_reserve_memtype_wc(pci_resource_start(dev->pdev, 0),
  224. pci_resource_len(dev->pdev, 0));
  225. cirrus->fb_mtrr = arch_phys_wc_add(pci_resource_start(dev->pdev, 0),
  226. pci_resource_len(dev->pdev, 0));
  227. cirrus->mm_inited = true;
  228. return 0;
  229. }
  230. void cirrus_mm_fini(struct cirrus_device *cirrus)
  231. {
  232. struct drm_device *dev = cirrus->dev;
  233. if (!cirrus->mm_inited)
  234. return;
  235. ttm_bo_device_release(&cirrus->ttm.bdev);
  236. cirrus_ttm_global_release(cirrus);
  237. arch_phys_wc_del(cirrus->fb_mtrr);
  238. cirrus->fb_mtrr = 0;
  239. arch_io_free_memtype_wc(pci_resource_start(dev->pdev, 0),
  240. pci_resource_len(dev->pdev, 0));
  241. }
  242. void cirrus_ttm_placement(struct cirrus_bo *bo, int domain)
  243. {
  244. u32 c = 0;
  245. unsigned i;
  246. bo->placement.placement = bo->placements;
  247. bo->placement.busy_placement = bo->placements;
  248. if (domain & TTM_PL_FLAG_VRAM)
  249. bo->placements[c++].flags = TTM_PL_FLAG_WC | TTM_PL_FLAG_UNCACHED | TTM_PL_FLAG_VRAM;
  250. if (domain & TTM_PL_FLAG_SYSTEM)
  251. bo->placements[c++].flags = TTM_PL_MASK_CACHING | TTM_PL_FLAG_SYSTEM;
  252. if (!c)
  253. bo->placements[c++].flags = TTM_PL_MASK_CACHING | TTM_PL_FLAG_SYSTEM;
  254. bo->placement.num_placement = c;
  255. bo->placement.num_busy_placement = c;
  256. for (i = 0; i < c; ++i) {
  257. bo->placements[i].fpfn = 0;
  258. bo->placements[i].lpfn = 0;
  259. }
  260. }
  261. int cirrus_bo_create(struct drm_device *dev, int size, int align,
  262. uint32_t flags, struct cirrus_bo **pcirrusbo)
  263. {
  264. struct cirrus_device *cirrus = dev->dev_private;
  265. struct cirrus_bo *cirrusbo;
  266. size_t acc_size;
  267. int ret;
  268. cirrusbo = kzalloc(sizeof(struct cirrus_bo), GFP_KERNEL);
  269. if (!cirrusbo)
  270. return -ENOMEM;
  271. ret = drm_gem_object_init(dev, &cirrusbo->gem, size);
  272. if (ret) {
  273. kfree(cirrusbo);
  274. return ret;
  275. }
  276. cirrusbo->bo.bdev = &cirrus->ttm.bdev;
  277. cirrus_ttm_placement(cirrusbo, TTM_PL_FLAG_VRAM | TTM_PL_FLAG_SYSTEM);
  278. acc_size = ttm_bo_dma_acc_size(&cirrus->ttm.bdev, size,
  279. sizeof(struct cirrus_bo));
  280. ret = ttm_bo_init(&cirrus->ttm.bdev, &cirrusbo->bo, size,
  281. ttm_bo_type_device, &cirrusbo->placement,
  282. align >> PAGE_SHIFT, false, acc_size,
  283. NULL, NULL, cirrus_bo_ttm_destroy);
  284. if (ret)
  285. return ret;
  286. *pcirrusbo = cirrusbo;
  287. return 0;
  288. }
  289. static inline u64 cirrus_bo_gpu_offset(struct cirrus_bo *bo)
  290. {
  291. return bo->bo.offset;
  292. }
  293. int cirrus_bo_pin(struct cirrus_bo *bo, u32 pl_flag, u64 *gpu_addr)
  294. {
  295. struct ttm_operation_ctx ctx = { false, false };
  296. int i, ret;
  297. if (bo->pin_count) {
  298. bo->pin_count++;
  299. if (gpu_addr)
  300. *gpu_addr = cirrus_bo_gpu_offset(bo);
  301. }
  302. cirrus_ttm_placement(bo, pl_flag);
  303. for (i = 0; i < bo->placement.num_placement; i++)
  304. bo->placements[i].flags |= TTM_PL_FLAG_NO_EVICT;
  305. ret = ttm_bo_validate(&bo->bo, &bo->placement, &ctx);
  306. if (ret)
  307. return ret;
  308. bo->pin_count = 1;
  309. if (gpu_addr)
  310. *gpu_addr = cirrus_bo_gpu_offset(bo);
  311. return 0;
  312. }
  313. int cirrus_bo_push_sysram(struct cirrus_bo *bo)
  314. {
  315. struct ttm_operation_ctx ctx = { false, false };
  316. int i, ret;
  317. if (!bo->pin_count) {
  318. DRM_ERROR("unpin bad %p\n", bo);
  319. return 0;
  320. }
  321. bo->pin_count--;
  322. if (bo->pin_count)
  323. return 0;
  324. if (bo->kmap.virtual)
  325. ttm_bo_kunmap(&bo->kmap);
  326. cirrus_ttm_placement(bo, TTM_PL_FLAG_SYSTEM);
  327. for (i = 0; i < bo->placement.num_placement ; i++)
  328. bo->placements[i].flags |= TTM_PL_FLAG_NO_EVICT;
  329. ret = ttm_bo_validate(&bo->bo, &bo->placement, &ctx);
  330. if (ret) {
  331. DRM_ERROR("pushing to VRAM failed\n");
  332. return ret;
  333. }
  334. return 0;
  335. }
  336. int cirrus_mmap(struct file *filp, struct vm_area_struct *vma)
  337. {
  338. struct drm_file *file_priv;
  339. struct cirrus_device *cirrus;
  340. if (unlikely(vma->vm_pgoff < DRM_FILE_PAGE_OFFSET))
  341. return -EINVAL;
  342. file_priv = filp->private_data;
  343. cirrus = file_priv->minor->dev->dev_private;
  344. return ttm_bo_mmap(filp, vma, &cirrus->ttm.bdev);
  345. }