qxl_ttm.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * Copyright 2013 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 "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * Authors: Dave Airlie
  23. * Alon Levy
  24. */
  25. #include <linux/delay.h>
  26. #include <drm/drm.h>
  27. #include <drm/drm_file.h>
  28. #include <drm/drm_debugfs.h>
  29. #include <drm/qxl_drm.h>
  30. #include <drm/ttm/ttm_bo_api.h>
  31. #include <drm/ttm/ttm_bo_driver.h>
  32. #include <drm/ttm/ttm_placement.h>
  33. #include <drm/ttm/ttm_range_manager.h>
  34. #include "qxl_drv.h"
  35. #include "qxl_object.h"
  36. static struct qxl_device *qxl_get_qdev(struct ttm_device *bdev)
  37. {
  38. struct qxl_mman *mman;
  39. struct qxl_device *qdev;
  40. mman = container_of(bdev, struct qxl_mman, bdev);
  41. qdev = container_of(mman, struct qxl_device, mman);
  42. return qdev;
  43. }
  44. static void qxl_evict_flags(struct ttm_buffer_object *bo,
  45. struct ttm_placement *placement)
  46. {
  47. struct qxl_bo *qbo;
  48. static const struct ttm_place placements = {
  49. .fpfn = 0,
  50. .lpfn = 0,
  51. .mem_type = TTM_PL_SYSTEM,
  52. .flags = 0
  53. };
  54. if (!qxl_ttm_bo_is_qxl_bo(bo)) {
  55. placement->placement = &placements;
  56. placement->busy_placement = &placements;
  57. placement->num_placement = 1;
  58. placement->num_busy_placement = 1;
  59. return;
  60. }
  61. qbo = to_qxl_bo(bo);
  62. qxl_ttm_placement_from_domain(qbo, QXL_GEM_DOMAIN_CPU);
  63. *placement = qbo->placement;
  64. }
  65. int qxl_ttm_io_mem_reserve(struct ttm_device *bdev,
  66. struct ttm_resource *mem)
  67. {
  68. struct qxl_device *qdev = qxl_get_qdev(bdev);
  69. switch (mem->mem_type) {
  70. case TTM_PL_SYSTEM:
  71. /* system memory */
  72. return 0;
  73. case TTM_PL_VRAM:
  74. mem->bus.is_iomem = true;
  75. mem->bus.offset = (mem->start << PAGE_SHIFT) + qdev->vram_base;
  76. mem->bus.caching = ttm_write_combined;
  77. break;
  78. case TTM_PL_PRIV:
  79. mem->bus.is_iomem = true;
  80. mem->bus.offset = (mem->start << PAGE_SHIFT) +
  81. qdev->surfaceram_base;
  82. mem->bus.caching = ttm_write_combined;
  83. break;
  84. default:
  85. return -EINVAL;
  86. }
  87. return 0;
  88. }
  89. /*
  90. * TTM backend functions.
  91. */
  92. static void qxl_ttm_backend_destroy(struct ttm_device *bdev, struct ttm_tt *ttm)
  93. {
  94. ttm_tt_fini(ttm);
  95. kfree(ttm);
  96. }
  97. static struct ttm_tt *qxl_ttm_tt_create(struct ttm_buffer_object *bo,
  98. uint32_t page_flags)
  99. {
  100. struct ttm_tt *ttm;
  101. ttm = kzalloc(sizeof(struct ttm_tt), GFP_KERNEL);
  102. if (ttm == NULL)
  103. return NULL;
  104. if (ttm_tt_init(ttm, bo, page_flags, ttm_cached, 0)) {
  105. kfree(ttm);
  106. return NULL;
  107. }
  108. return ttm;
  109. }
  110. static void qxl_bo_move_notify(struct ttm_buffer_object *bo,
  111. struct ttm_resource *new_mem)
  112. {
  113. struct qxl_bo *qbo;
  114. struct qxl_device *qdev;
  115. if (!qxl_ttm_bo_is_qxl_bo(bo) || !bo->resource)
  116. return;
  117. qbo = to_qxl_bo(bo);
  118. qdev = to_qxl(qbo->tbo.base.dev);
  119. if (bo->resource->mem_type == TTM_PL_PRIV && qbo->surface_id)
  120. qxl_surface_evict(qdev, qbo, new_mem ? true : false);
  121. }
  122. static int qxl_bo_move(struct ttm_buffer_object *bo, bool evict,
  123. struct ttm_operation_ctx *ctx,
  124. struct ttm_resource *new_mem,
  125. struct ttm_place *hop)
  126. {
  127. struct ttm_resource *old_mem = bo->resource;
  128. int ret;
  129. qxl_bo_move_notify(bo, new_mem);
  130. ret = ttm_bo_wait_ctx(bo, ctx);
  131. if (ret)
  132. return ret;
  133. if (old_mem->mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
  134. ttm_bo_move_null(bo, new_mem);
  135. return 0;
  136. }
  137. return ttm_bo_move_memcpy(bo, ctx, new_mem);
  138. }
  139. static void qxl_bo_delete_mem_notify(struct ttm_buffer_object *bo)
  140. {
  141. qxl_bo_move_notify(bo, NULL);
  142. }
  143. static struct ttm_device_funcs qxl_bo_driver = {
  144. .ttm_tt_create = &qxl_ttm_tt_create,
  145. .ttm_tt_destroy = &qxl_ttm_backend_destroy,
  146. .eviction_valuable = ttm_bo_eviction_valuable,
  147. .evict_flags = &qxl_evict_flags,
  148. .move = &qxl_bo_move,
  149. .io_mem_reserve = &qxl_ttm_io_mem_reserve,
  150. .delete_mem_notify = &qxl_bo_delete_mem_notify,
  151. };
  152. static int qxl_ttm_init_mem_type(struct qxl_device *qdev,
  153. unsigned int type,
  154. uint64_t size)
  155. {
  156. return ttm_range_man_init(&qdev->mman.bdev, type, false, size);
  157. }
  158. int qxl_ttm_init(struct qxl_device *qdev)
  159. {
  160. int r;
  161. int num_io_pages; /* != rom->num_io_pages, we include surface0 */
  162. /* No others user of address space so set it to 0 */
  163. r = ttm_device_init(&qdev->mman.bdev, &qxl_bo_driver, NULL,
  164. qdev->ddev.anon_inode->i_mapping,
  165. qdev->ddev.vma_offset_manager,
  166. false, false);
  167. if (r) {
  168. DRM_ERROR("failed initializing buffer object driver(%d).\n", r);
  169. return r;
  170. }
  171. /* NOTE: this includes the framebuffer (aka surface 0) */
  172. num_io_pages = qdev->rom->ram_header_offset / PAGE_SIZE;
  173. r = qxl_ttm_init_mem_type(qdev, TTM_PL_VRAM, num_io_pages);
  174. if (r) {
  175. DRM_ERROR("Failed initializing VRAM heap.\n");
  176. return r;
  177. }
  178. r = qxl_ttm_init_mem_type(qdev, TTM_PL_PRIV,
  179. qdev->surfaceram_size / PAGE_SIZE);
  180. if (r) {
  181. DRM_ERROR("Failed initializing Surfaces heap.\n");
  182. return r;
  183. }
  184. DRM_INFO("qxl: %uM of VRAM memory size\n",
  185. (unsigned int)qdev->vram_size / (1024 * 1024));
  186. DRM_INFO("qxl: %luM of IO pages memory ready (VRAM domain)\n",
  187. ((unsigned int)num_io_pages * PAGE_SIZE) / (1024 * 1024));
  188. DRM_INFO("qxl: %uM of Surface memory size\n",
  189. (unsigned int)qdev->surfaceram_size / (1024 * 1024));
  190. return 0;
  191. }
  192. void qxl_ttm_fini(struct qxl_device *qdev)
  193. {
  194. ttm_range_man_fini(&qdev->mman.bdev, TTM_PL_VRAM);
  195. ttm_range_man_fini(&qdev->mman.bdev, TTM_PL_PRIV);
  196. ttm_device_fini(&qdev->mman.bdev);
  197. DRM_INFO("qxl: ttm finalized\n");
  198. }
  199. void qxl_ttm_debugfs_init(struct qxl_device *qdev)
  200. {
  201. #if defined(CONFIG_DEBUG_FS)
  202. ttm_resource_manager_create_debugfs(ttm_manager_type(&qdev->mman.bdev,
  203. TTM_PL_VRAM),
  204. qdev->ddev.primary->debugfs_root, "qxl_mem_mm");
  205. ttm_resource_manager_create_debugfs(ttm_manager_type(&qdev->mman.bdev,
  206. TTM_PL_PRIV),
  207. qdev->ddev.primary->debugfs_root, "qxl_surf_mm");
  208. #endif
  209. }