qxl_ttm.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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_module.h>
  33. #include <drm/ttm/ttm_page_alloc.h>
  34. #include <drm/ttm/ttm_placement.h>
  35. #include "qxl_drv.h"
  36. #include "qxl_object.h"
  37. static struct qxl_device *qxl_get_qdev(struct ttm_bo_device *bdev)
  38. {
  39. struct qxl_mman *mman;
  40. struct qxl_device *qdev;
  41. mman = container_of(bdev, struct qxl_mman, bdev);
  42. qdev = container_of(mman, struct qxl_device, mman);
  43. return qdev;
  44. }
  45. static void qxl_evict_flags(struct ttm_buffer_object *bo,
  46. struct ttm_placement *placement)
  47. {
  48. struct qxl_bo *qbo;
  49. static const struct ttm_place placements = {
  50. .fpfn = 0,
  51. .lpfn = 0,
  52. .mem_type = TTM_PL_SYSTEM,
  53. .flags = TTM_PL_MASK_CACHING
  54. };
  55. if (!qxl_ttm_bo_is_qxl_bo(bo)) {
  56. placement->placement = &placements;
  57. placement->busy_placement = &placements;
  58. placement->num_placement = 1;
  59. placement->num_busy_placement = 1;
  60. return;
  61. }
  62. qbo = to_qxl_bo(bo);
  63. qxl_ttm_placement_from_domain(qbo, QXL_GEM_DOMAIN_CPU, false);
  64. *placement = qbo->placement;
  65. }
  66. int qxl_ttm_io_mem_reserve(struct ttm_bo_device *bdev,
  67. struct ttm_resource *mem)
  68. {
  69. struct qxl_device *qdev = qxl_get_qdev(bdev);
  70. switch (mem->mem_type) {
  71. case TTM_PL_SYSTEM:
  72. /* system memory */
  73. return 0;
  74. case TTM_PL_VRAM:
  75. mem->bus.is_iomem = true;
  76. mem->bus.offset = (mem->start << PAGE_SHIFT) + qdev->vram_base;
  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. break;
  83. default:
  84. return -EINVAL;
  85. }
  86. return 0;
  87. }
  88. /*
  89. * TTM backend functions.
  90. */
  91. struct qxl_ttm_tt {
  92. struct ttm_tt ttm;
  93. struct qxl_device *qdev;
  94. u64 offset;
  95. };
  96. static int qxl_ttm_backend_bind(struct ttm_bo_device *bdev,
  97. struct ttm_tt *ttm,
  98. struct ttm_resource *bo_mem)
  99. {
  100. struct qxl_ttm_tt *gtt = (void *)ttm;
  101. gtt->offset = (unsigned long)(bo_mem->start << PAGE_SHIFT);
  102. if (!ttm->num_pages) {
  103. WARN(1, "nothing to bind %lu pages for mreg %p back %p!\n",
  104. ttm->num_pages, bo_mem, ttm);
  105. }
  106. /* Not implemented */
  107. return -1;
  108. }
  109. static void qxl_ttm_backend_unbind(struct ttm_bo_device *bdev,
  110. struct ttm_tt *ttm)
  111. {
  112. /* Not implemented */
  113. }
  114. static void qxl_ttm_backend_destroy(struct ttm_bo_device *bdev,
  115. struct ttm_tt *ttm)
  116. {
  117. struct qxl_ttm_tt *gtt = (void *)ttm;
  118. ttm_tt_destroy_common(bdev, ttm);
  119. ttm_tt_fini(&gtt->ttm);
  120. kfree(gtt);
  121. }
  122. static struct ttm_tt *qxl_ttm_tt_create(struct ttm_buffer_object *bo,
  123. uint32_t page_flags)
  124. {
  125. struct qxl_device *qdev;
  126. struct qxl_ttm_tt *gtt;
  127. qdev = qxl_get_qdev(bo->bdev);
  128. gtt = kzalloc(sizeof(struct qxl_ttm_tt), GFP_KERNEL);
  129. if (gtt == NULL)
  130. return NULL;
  131. gtt->qdev = qdev;
  132. if (ttm_tt_init(&gtt->ttm, bo, page_flags)) {
  133. kfree(gtt);
  134. return NULL;
  135. }
  136. return &gtt->ttm;
  137. }
  138. static int qxl_bo_move(struct ttm_buffer_object *bo, bool evict,
  139. struct ttm_operation_ctx *ctx,
  140. struct ttm_resource *new_mem)
  141. {
  142. struct ttm_resource *old_mem = &bo->mem;
  143. int ret;
  144. ret = ttm_bo_wait(bo, ctx->interruptible, ctx->no_wait_gpu);
  145. if (ret)
  146. return ret;
  147. if (old_mem->mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
  148. ttm_bo_move_null(bo, new_mem);
  149. return 0;
  150. }
  151. return ttm_bo_move_memcpy(bo, ctx, new_mem);
  152. }
  153. static void qxl_bo_move_notify(struct ttm_buffer_object *bo,
  154. bool evict,
  155. struct ttm_resource *new_mem)
  156. {
  157. struct qxl_bo *qbo;
  158. struct qxl_device *qdev;
  159. if (!qxl_ttm_bo_is_qxl_bo(bo))
  160. return;
  161. qbo = to_qxl_bo(bo);
  162. qdev = to_qxl(qbo->tbo.base.dev);
  163. if (bo->mem.mem_type == TTM_PL_PRIV && qbo->surface_id)
  164. qxl_surface_evict(qdev, qbo, new_mem ? true : false);
  165. }
  166. static struct ttm_bo_driver qxl_bo_driver = {
  167. .ttm_tt_create = &qxl_ttm_tt_create,
  168. .ttm_tt_bind = &qxl_ttm_backend_bind,
  169. .ttm_tt_destroy = &qxl_ttm_backend_destroy,
  170. .ttm_tt_unbind = &qxl_ttm_backend_unbind,
  171. .eviction_valuable = ttm_bo_eviction_valuable,
  172. .evict_flags = &qxl_evict_flags,
  173. .move = &qxl_bo_move,
  174. .io_mem_reserve = &qxl_ttm_io_mem_reserve,
  175. .move_notify = &qxl_bo_move_notify,
  176. };
  177. static int qxl_ttm_init_mem_type(struct qxl_device *qdev,
  178. unsigned int type,
  179. uint64_t size)
  180. {
  181. return ttm_range_man_init(&qdev->mman.bdev, type, false, size);
  182. }
  183. int qxl_ttm_init(struct qxl_device *qdev)
  184. {
  185. int r;
  186. int num_io_pages; /* != rom->num_io_pages, we include surface0 */
  187. /* No others user of address space so set it to 0 */
  188. r = ttm_bo_device_init(&qdev->mman.bdev,
  189. &qxl_bo_driver,
  190. qdev->ddev.anon_inode->i_mapping,
  191. qdev->ddev.vma_offset_manager,
  192. false);
  193. if (r) {
  194. DRM_ERROR("failed initializing buffer object driver(%d).\n", r);
  195. return r;
  196. }
  197. /* NOTE: this includes the framebuffer (aka surface 0) */
  198. num_io_pages = qdev->rom->ram_header_offset / PAGE_SIZE;
  199. r = qxl_ttm_init_mem_type(qdev, TTM_PL_VRAM, num_io_pages);
  200. if (r) {
  201. DRM_ERROR("Failed initializing VRAM heap.\n");
  202. return r;
  203. }
  204. r = qxl_ttm_init_mem_type(qdev, TTM_PL_PRIV,
  205. qdev->surfaceram_size / PAGE_SIZE);
  206. if (r) {
  207. DRM_ERROR("Failed initializing Surfaces heap.\n");
  208. return r;
  209. }
  210. DRM_INFO("qxl: %uM of VRAM memory size\n",
  211. (unsigned int)qdev->vram_size / (1024 * 1024));
  212. DRM_INFO("qxl: %luM of IO pages memory ready (VRAM domain)\n",
  213. ((unsigned int)num_io_pages * PAGE_SIZE) / (1024 * 1024));
  214. DRM_INFO("qxl: %uM of Surface memory size\n",
  215. (unsigned int)qdev->surfaceram_size / (1024 * 1024));
  216. return 0;
  217. }
  218. void qxl_ttm_fini(struct qxl_device *qdev)
  219. {
  220. ttm_range_man_fini(&qdev->mman.bdev, TTM_PL_VRAM);
  221. ttm_range_man_fini(&qdev->mman.bdev, TTM_PL_PRIV);
  222. ttm_bo_device_release(&qdev->mman.bdev);
  223. DRM_INFO("qxl: ttm finalized\n");
  224. }
  225. #define QXL_DEBUGFS_MEM_TYPES 2
  226. #if defined(CONFIG_DEBUG_FS)
  227. static int qxl_mm_dump_table(struct seq_file *m, void *data)
  228. {
  229. struct drm_info_node *node = (struct drm_info_node *)m->private;
  230. struct ttm_resource_manager *man = (struct ttm_resource_manager *)node->info_ent->data;
  231. struct drm_printer p = drm_seq_file_printer(m);
  232. ttm_resource_manager_debug(man, &p);
  233. return 0;
  234. }
  235. #endif
  236. void qxl_ttm_debugfs_init(struct qxl_device *qdev)
  237. {
  238. #if defined(CONFIG_DEBUG_FS)
  239. static struct drm_info_list qxl_mem_types_list[QXL_DEBUGFS_MEM_TYPES];
  240. static char qxl_mem_types_names[QXL_DEBUGFS_MEM_TYPES][32];
  241. unsigned int i;
  242. for (i = 0; i < QXL_DEBUGFS_MEM_TYPES; i++) {
  243. if (i == 0)
  244. sprintf(qxl_mem_types_names[i], "qxl_mem_mm");
  245. else
  246. sprintf(qxl_mem_types_names[i], "qxl_surf_mm");
  247. qxl_mem_types_list[i].name = qxl_mem_types_names[i];
  248. qxl_mem_types_list[i].show = &qxl_mm_dump_table;
  249. qxl_mem_types_list[i].driver_features = 0;
  250. if (i == 0)
  251. qxl_mem_types_list[i].data = ttm_manager_type(&qdev->mman.bdev, TTM_PL_VRAM);
  252. else
  253. qxl_mem_types_list[i].data = ttm_manager_type(&qdev->mman.bdev, TTM_PL_PRIV);
  254. }
  255. qxl_debugfs_add_files(qdev, qxl_mem_types_list, i);
  256. #endif
  257. }