qxl_ttm.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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 <ttm/ttm_bo_api.h>
  26. #include <ttm/ttm_bo_driver.h>
  27. #include <ttm/ttm_placement.h>
  28. #include <ttm/ttm_page_alloc.h>
  29. #include <ttm/ttm_module.h>
  30. #include <drm/drmP.h>
  31. #include <drm/drm.h>
  32. #include <drm/qxl_drm.h>
  33. #include "qxl_drv.h"
  34. #include "qxl_object.h"
  35. #include <linux/delay.h>
  36. static int qxl_ttm_debugfs_init(struct qxl_device *qdev);
  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 int qxl_ttm_mem_global_init(struct drm_global_reference *ref)
  46. {
  47. return ttm_mem_global_init(ref->object);
  48. }
  49. static void qxl_ttm_mem_global_release(struct drm_global_reference *ref)
  50. {
  51. ttm_mem_global_release(ref->object);
  52. }
  53. static int qxl_ttm_global_init(struct qxl_device *qdev)
  54. {
  55. struct drm_global_reference *global_ref;
  56. int r;
  57. qdev->mman.mem_global_referenced = false;
  58. global_ref = &qdev->mman.mem_global_ref;
  59. global_ref->global_type = DRM_GLOBAL_TTM_MEM;
  60. global_ref->size = sizeof(struct ttm_mem_global);
  61. global_ref->init = &qxl_ttm_mem_global_init;
  62. global_ref->release = &qxl_ttm_mem_global_release;
  63. r = drm_global_item_ref(global_ref);
  64. if (r != 0) {
  65. DRM_ERROR("Failed setting up TTM memory accounting "
  66. "subsystem.\n");
  67. return r;
  68. }
  69. qdev->mman.bo_global_ref.mem_glob =
  70. qdev->mman.mem_global_ref.object;
  71. global_ref = &qdev->mman.bo_global_ref.ref;
  72. global_ref->global_type = DRM_GLOBAL_TTM_BO;
  73. global_ref->size = sizeof(struct ttm_bo_global);
  74. global_ref->init = &ttm_bo_global_init;
  75. global_ref->release = &ttm_bo_global_release;
  76. r = drm_global_item_ref(global_ref);
  77. if (r != 0) {
  78. DRM_ERROR("Failed setting up TTM BO subsystem.\n");
  79. drm_global_item_unref(&qdev->mman.mem_global_ref);
  80. return r;
  81. }
  82. qdev->mman.mem_global_referenced = true;
  83. return 0;
  84. }
  85. static void qxl_ttm_global_fini(struct qxl_device *qdev)
  86. {
  87. if (qdev->mman.mem_global_referenced) {
  88. drm_global_item_unref(&qdev->mman.bo_global_ref.ref);
  89. drm_global_item_unref(&qdev->mman.mem_global_ref);
  90. qdev->mman.mem_global_referenced = false;
  91. }
  92. }
  93. static struct vm_operations_struct qxl_ttm_vm_ops;
  94. static const struct vm_operations_struct *ttm_vm_ops;
  95. static int qxl_ttm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  96. {
  97. struct ttm_buffer_object *bo;
  98. int r;
  99. bo = (struct ttm_buffer_object *)vma->vm_private_data;
  100. if (bo == NULL)
  101. return VM_FAULT_NOPAGE;
  102. r = ttm_vm_ops->fault(vma, vmf);
  103. return r;
  104. }
  105. int qxl_mmap(struct file *filp, struct vm_area_struct *vma)
  106. {
  107. struct drm_file *file_priv;
  108. struct qxl_device *qdev;
  109. int r;
  110. if (unlikely(vma->vm_pgoff < DRM_FILE_PAGE_OFFSET)) {
  111. pr_info("%s: vma->vm_pgoff (%ld) < DRM_FILE_PAGE_OFFSET\n",
  112. __func__, vma->vm_pgoff);
  113. return -EINVAL;
  114. }
  115. file_priv = filp->private_data;
  116. qdev = file_priv->minor->dev->dev_private;
  117. if (qdev == NULL) {
  118. DRM_ERROR(
  119. "filp->private_data->minor->dev->dev_private == NULL\n");
  120. return -EINVAL;
  121. }
  122. QXL_INFO(qdev, "%s: filp->private_data = 0x%p, vma->vm_pgoff = %lx\n",
  123. __func__, filp->private_data, vma->vm_pgoff);
  124. r = ttm_bo_mmap(filp, vma, &qdev->mman.bdev);
  125. if (unlikely(r != 0))
  126. return r;
  127. if (unlikely(ttm_vm_ops == NULL)) {
  128. ttm_vm_ops = vma->vm_ops;
  129. qxl_ttm_vm_ops = *ttm_vm_ops;
  130. qxl_ttm_vm_ops.fault = &qxl_ttm_fault;
  131. }
  132. vma->vm_ops = &qxl_ttm_vm_ops;
  133. return 0;
  134. }
  135. static int qxl_invalidate_caches(struct ttm_bo_device *bdev, uint32_t flags)
  136. {
  137. return 0;
  138. }
  139. static int qxl_init_mem_type(struct ttm_bo_device *bdev, uint32_t type,
  140. struct ttm_mem_type_manager *man)
  141. {
  142. switch (type) {
  143. case TTM_PL_SYSTEM:
  144. /* System memory */
  145. man->flags = TTM_MEMTYPE_FLAG_MAPPABLE;
  146. man->available_caching = TTM_PL_MASK_CACHING;
  147. man->default_caching = TTM_PL_FLAG_CACHED;
  148. break;
  149. case TTM_PL_VRAM:
  150. case TTM_PL_PRIV0:
  151. /* "On-card" video ram */
  152. man->func = &ttm_bo_manager_func;
  153. man->gpu_offset = 0;
  154. man->flags = TTM_MEMTYPE_FLAG_FIXED |
  155. TTM_MEMTYPE_FLAG_MAPPABLE;
  156. man->available_caching = TTM_PL_MASK_CACHING;
  157. man->default_caching = TTM_PL_FLAG_CACHED;
  158. break;
  159. default:
  160. DRM_ERROR("Unsupported memory type %u\n", (unsigned)type);
  161. return -EINVAL;
  162. }
  163. return 0;
  164. }
  165. static void qxl_evict_flags(struct ttm_buffer_object *bo,
  166. struct ttm_placement *placement)
  167. {
  168. struct qxl_bo *qbo;
  169. static struct ttm_place placements = {
  170. .fpfn = 0,
  171. .lpfn = 0,
  172. .flags = TTM_PL_MASK_CACHING | TTM_PL_FLAG_SYSTEM
  173. };
  174. if (!qxl_ttm_bo_is_qxl_bo(bo)) {
  175. placement->placement = &placements;
  176. placement->busy_placement = &placements;
  177. placement->num_placement = 1;
  178. placement->num_busy_placement = 1;
  179. return;
  180. }
  181. qbo = container_of(bo, struct qxl_bo, tbo);
  182. qxl_ttm_placement_from_domain(qbo, QXL_GEM_DOMAIN_CPU, false);
  183. *placement = qbo->placement;
  184. }
  185. static int qxl_verify_access(struct ttm_buffer_object *bo, struct file *filp)
  186. {
  187. struct qxl_bo *qbo = to_qxl_bo(bo);
  188. return drm_vma_node_verify_access(&qbo->gem_base.vma_node, filp);
  189. }
  190. static int qxl_ttm_io_mem_reserve(struct ttm_bo_device *bdev,
  191. struct ttm_mem_reg *mem)
  192. {
  193. struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
  194. struct qxl_device *qdev = qxl_get_qdev(bdev);
  195. mem->bus.addr = NULL;
  196. mem->bus.offset = 0;
  197. mem->bus.size = mem->num_pages << PAGE_SHIFT;
  198. mem->bus.base = 0;
  199. mem->bus.is_iomem = false;
  200. if (!(man->flags & TTM_MEMTYPE_FLAG_MAPPABLE))
  201. return -EINVAL;
  202. switch (mem->mem_type) {
  203. case TTM_PL_SYSTEM:
  204. /* system memory */
  205. return 0;
  206. case TTM_PL_VRAM:
  207. mem->bus.is_iomem = true;
  208. mem->bus.base = qdev->vram_base;
  209. mem->bus.offset = mem->start << PAGE_SHIFT;
  210. break;
  211. case TTM_PL_PRIV0:
  212. mem->bus.is_iomem = true;
  213. mem->bus.base = qdev->surfaceram_base;
  214. mem->bus.offset = mem->start << PAGE_SHIFT;
  215. break;
  216. default:
  217. return -EINVAL;
  218. }
  219. return 0;
  220. }
  221. static void qxl_ttm_io_mem_free(struct ttm_bo_device *bdev,
  222. struct ttm_mem_reg *mem)
  223. {
  224. }
  225. /*
  226. * TTM backend functions.
  227. */
  228. struct qxl_ttm_tt {
  229. struct ttm_dma_tt ttm;
  230. struct qxl_device *qdev;
  231. u64 offset;
  232. };
  233. static int qxl_ttm_backend_bind(struct ttm_tt *ttm,
  234. struct ttm_mem_reg *bo_mem)
  235. {
  236. struct qxl_ttm_tt *gtt = (void *)ttm;
  237. gtt->offset = (unsigned long)(bo_mem->start << PAGE_SHIFT);
  238. if (!ttm->num_pages) {
  239. WARN(1, "nothing to bind %lu pages for mreg %p back %p!\n",
  240. ttm->num_pages, bo_mem, ttm);
  241. }
  242. /* Not implemented */
  243. return -1;
  244. }
  245. static int qxl_ttm_backend_unbind(struct ttm_tt *ttm)
  246. {
  247. /* Not implemented */
  248. return -1;
  249. }
  250. static void qxl_ttm_backend_destroy(struct ttm_tt *ttm)
  251. {
  252. struct qxl_ttm_tt *gtt = (void *)ttm;
  253. ttm_dma_tt_fini(&gtt->ttm);
  254. kfree(gtt);
  255. }
  256. static struct ttm_backend_func qxl_backend_func = {
  257. .bind = &qxl_ttm_backend_bind,
  258. .unbind = &qxl_ttm_backend_unbind,
  259. .destroy = &qxl_ttm_backend_destroy,
  260. };
  261. static int qxl_ttm_tt_populate(struct ttm_tt *ttm)
  262. {
  263. int r;
  264. if (ttm->state != tt_unpopulated)
  265. return 0;
  266. r = ttm_pool_populate(ttm);
  267. if (r)
  268. return r;
  269. return 0;
  270. }
  271. static void qxl_ttm_tt_unpopulate(struct ttm_tt *ttm)
  272. {
  273. ttm_pool_unpopulate(ttm);
  274. }
  275. static struct ttm_tt *qxl_ttm_tt_create(struct ttm_bo_device *bdev,
  276. unsigned long size, uint32_t page_flags,
  277. struct page *dummy_read_page)
  278. {
  279. struct qxl_device *qdev;
  280. struct qxl_ttm_tt *gtt;
  281. qdev = qxl_get_qdev(bdev);
  282. gtt = kzalloc(sizeof(struct qxl_ttm_tt), GFP_KERNEL);
  283. if (gtt == NULL)
  284. return NULL;
  285. gtt->ttm.ttm.func = &qxl_backend_func;
  286. gtt->qdev = qdev;
  287. if (ttm_dma_tt_init(&gtt->ttm, bdev, size, page_flags,
  288. dummy_read_page)) {
  289. kfree(gtt);
  290. return NULL;
  291. }
  292. return &gtt->ttm.ttm;
  293. }
  294. static void qxl_move_null(struct ttm_buffer_object *bo,
  295. struct ttm_mem_reg *new_mem)
  296. {
  297. struct ttm_mem_reg *old_mem = &bo->mem;
  298. BUG_ON(old_mem->mm_node != NULL);
  299. *old_mem = *new_mem;
  300. new_mem->mm_node = NULL;
  301. }
  302. static int qxl_bo_move(struct ttm_buffer_object *bo,
  303. bool evict, bool interruptible,
  304. bool no_wait_gpu,
  305. struct ttm_mem_reg *new_mem)
  306. {
  307. struct ttm_mem_reg *old_mem = &bo->mem;
  308. if (old_mem->mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
  309. qxl_move_null(bo, new_mem);
  310. return 0;
  311. }
  312. return ttm_bo_move_memcpy(bo, evict, no_wait_gpu, new_mem);
  313. }
  314. static void qxl_bo_move_notify(struct ttm_buffer_object *bo,
  315. struct ttm_mem_reg *new_mem)
  316. {
  317. struct qxl_bo *qbo;
  318. struct qxl_device *qdev;
  319. if (!qxl_ttm_bo_is_qxl_bo(bo))
  320. return;
  321. qbo = container_of(bo, struct qxl_bo, tbo);
  322. qdev = qbo->gem_base.dev->dev_private;
  323. if (bo->mem.mem_type == TTM_PL_PRIV0 && qbo->surface_id)
  324. qxl_surface_evict(qdev, qbo, new_mem ? true : false);
  325. }
  326. static struct ttm_bo_driver qxl_bo_driver = {
  327. .ttm_tt_create = &qxl_ttm_tt_create,
  328. .ttm_tt_populate = &qxl_ttm_tt_populate,
  329. .ttm_tt_unpopulate = &qxl_ttm_tt_unpopulate,
  330. .invalidate_caches = &qxl_invalidate_caches,
  331. .init_mem_type = &qxl_init_mem_type,
  332. .evict_flags = &qxl_evict_flags,
  333. .move = &qxl_bo_move,
  334. .verify_access = &qxl_verify_access,
  335. .io_mem_reserve = &qxl_ttm_io_mem_reserve,
  336. .io_mem_free = &qxl_ttm_io_mem_free,
  337. .move_notify = &qxl_bo_move_notify,
  338. };
  339. int qxl_ttm_init(struct qxl_device *qdev)
  340. {
  341. int r;
  342. int num_io_pages; /* != rom->num_io_pages, we include surface0 */
  343. r = qxl_ttm_global_init(qdev);
  344. if (r)
  345. return r;
  346. /* No others user of address space so set it to 0 */
  347. r = ttm_bo_device_init(&qdev->mman.bdev,
  348. qdev->mman.bo_global_ref.ref.object,
  349. &qxl_bo_driver,
  350. qdev->ddev->anon_inode->i_mapping,
  351. DRM_FILE_PAGE_OFFSET, 0);
  352. if (r) {
  353. DRM_ERROR("failed initializing buffer object driver(%d).\n", r);
  354. return r;
  355. }
  356. /* NOTE: this includes the framebuffer (aka surface 0) */
  357. num_io_pages = qdev->rom->ram_header_offset / PAGE_SIZE;
  358. r = ttm_bo_init_mm(&qdev->mman.bdev, TTM_PL_VRAM,
  359. num_io_pages);
  360. if (r) {
  361. DRM_ERROR("Failed initializing VRAM heap.\n");
  362. return r;
  363. }
  364. r = ttm_bo_init_mm(&qdev->mman.bdev, TTM_PL_PRIV0,
  365. qdev->surfaceram_size / PAGE_SIZE);
  366. if (r) {
  367. DRM_ERROR("Failed initializing Surfaces heap.\n");
  368. return r;
  369. }
  370. DRM_INFO("qxl: %uM of VRAM memory size\n",
  371. (unsigned)qdev->vram_size / (1024 * 1024));
  372. DRM_INFO("qxl: %luM of IO pages memory ready (VRAM domain)\n",
  373. ((unsigned)num_io_pages * PAGE_SIZE) / (1024 * 1024));
  374. DRM_INFO("qxl: %uM of Surface memory size\n",
  375. (unsigned)qdev->surfaceram_size / (1024 * 1024));
  376. r = qxl_ttm_debugfs_init(qdev);
  377. if (r) {
  378. DRM_ERROR("Failed to init debugfs\n");
  379. return r;
  380. }
  381. return 0;
  382. }
  383. void qxl_ttm_fini(struct qxl_device *qdev)
  384. {
  385. ttm_bo_clean_mm(&qdev->mman.bdev, TTM_PL_VRAM);
  386. ttm_bo_clean_mm(&qdev->mman.bdev, TTM_PL_PRIV0);
  387. ttm_bo_device_release(&qdev->mman.bdev);
  388. qxl_ttm_global_fini(qdev);
  389. DRM_INFO("qxl: ttm finalized\n");
  390. }
  391. #define QXL_DEBUGFS_MEM_TYPES 2
  392. #if defined(CONFIG_DEBUG_FS)
  393. static int qxl_mm_dump_table(struct seq_file *m, void *data)
  394. {
  395. struct drm_info_node *node = (struct drm_info_node *)m->private;
  396. struct drm_mm *mm = (struct drm_mm *)node->info_ent->data;
  397. struct drm_device *dev = node->minor->dev;
  398. struct qxl_device *rdev = dev->dev_private;
  399. int ret;
  400. struct ttm_bo_global *glob = rdev->mman.bdev.glob;
  401. spin_lock(&glob->lru_lock);
  402. ret = drm_mm_dump_table(m, mm);
  403. spin_unlock(&glob->lru_lock);
  404. return ret;
  405. }
  406. #endif
  407. static int qxl_ttm_debugfs_init(struct qxl_device *qdev)
  408. {
  409. #if defined(CONFIG_DEBUG_FS)
  410. static struct drm_info_list qxl_mem_types_list[QXL_DEBUGFS_MEM_TYPES];
  411. static char qxl_mem_types_names[QXL_DEBUGFS_MEM_TYPES][32];
  412. unsigned i;
  413. for (i = 0; i < QXL_DEBUGFS_MEM_TYPES; i++) {
  414. if (i == 0)
  415. sprintf(qxl_mem_types_names[i], "qxl_mem_mm");
  416. else
  417. sprintf(qxl_mem_types_names[i], "qxl_surf_mm");
  418. qxl_mem_types_list[i].name = qxl_mem_types_names[i];
  419. qxl_mem_types_list[i].show = &qxl_mm_dump_table;
  420. qxl_mem_types_list[i].driver_features = 0;
  421. if (i == 0)
  422. qxl_mem_types_list[i].data = qdev->mman.bdev.man[TTM_PL_VRAM].priv;
  423. else
  424. qxl_mem_types_list[i].data = qdev->mman.bdev.man[TTM_PL_PRIV0].priv;
  425. }
  426. return qxl_debugfs_add_files(qdev, qxl_mem_types_list, i);
  427. #else
  428. return 0;
  429. #endif
  430. }