qxl_ttm.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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 <drm/ttm/ttm_bo_api.h>
  26. #include <drm/ttm/ttm_bo_driver.h>
  27. #include <drm/ttm/ttm_placement.h>
  28. #include <drm/ttm/ttm_page_alloc.h>
  29. #include <drm/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 struct qxl_device *qxl_get_qdev(struct ttm_bo_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 int qxl_ttm_mem_global_init(struct drm_global_reference *ref)
  45. {
  46. return ttm_mem_global_init(ref->object);
  47. }
  48. static void qxl_ttm_mem_global_release(struct drm_global_reference *ref)
  49. {
  50. ttm_mem_global_release(ref->object);
  51. }
  52. static int qxl_ttm_global_init(struct qxl_device *qdev)
  53. {
  54. struct drm_global_reference *global_ref;
  55. int r;
  56. qdev->mman.mem_global_referenced = false;
  57. global_ref = &qdev->mman.mem_global_ref;
  58. global_ref->global_type = DRM_GLOBAL_TTM_MEM;
  59. global_ref->size = sizeof(struct ttm_mem_global);
  60. global_ref->init = &qxl_ttm_mem_global_init;
  61. global_ref->release = &qxl_ttm_mem_global_release;
  62. r = drm_global_item_ref(global_ref);
  63. if (r != 0) {
  64. DRM_ERROR("Failed setting up TTM memory accounting "
  65. "subsystem.\n");
  66. return r;
  67. }
  68. qdev->mman.bo_global_ref.mem_glob =
  69. qdev->mman.mem_global_ref.object;
  70. global_ref = &qdev->mman.bo_global_ref.ref;
  71. global_ref->global_type = DRM_GLOBAL_TTM_BO;
  72. global_ref->size = sizeof(struct ttm_bo_global);
  73. global_ref->init = &ttm_bo_global_init;
  74. global_ref->release = &ttm_bo_global_release;
  75. r = drm_global_item_ref(global_ref);
  76. if (r != 0) {
  77. DRM_ERROR("Failed setting up TTM BO subsystem.\n");
  78. drm_global_item_unref(&qdev->mman.mem_global_ref);
  79. return r;
  80. }
  81. qdev->mman.mem_global_referenced = true;
  82. return 0;
  83. }
  84. static void qxl_ttm_global_fini(struct qxl_device *qdev)
  85. {
  86. if (qdev->mman.mem_global_referenced) {
  87. drm_global_item_unref(&qdev->mman.bo_global_ref.ref);
  88. drm_global_item_unref(&qdev->mman.mem_global_ref);
  89. qdev->mman.mem_global_referenced = false;
  90. }
  91. }
  92. static struct vm_operations_struct qxl_ttm_vm_ops;
  93. static const struct vm_operations_struct *ttm_vm_ops;
  94. static int qxl_ttm_fault(struct vm_fault *vmf)
  95. {
  96. struct ttm_buffer_object *bo;
  97. int r;
  98. bo = (struct ttm_buffer_object *)vmf->vma->vm_private_data;
  99. if (bo == NULL)
  100. return VM_FAULT_NOPAGE;
  101. r = ttm_vm_ops->fault(vmf);
  102. return r;
  103. }
  104. int qxl_mmap(struct file *filp, struct vm_area_struct *vma)
  105. {
  106. struct drm_file *file_priv;
  107. struct qxl_device *qdev;
  108. int r;
  109. if (unlikely(vma->vm_pgoff < DRM_FILE_PAGE_OFFSET)) {
  110. pr_info("%s: vma->vm_pgoff (%ld) < DRM_FILE_PAGE_OFFSET\n",
  111. __func__, vma->vm_pgoff);
  112. return -EINVAL;
  113. }
  114. file_priv = filp->private_data;
  115. qdev = file_priv->minor->dev->dev_private;
  116. if (qdev == NULL) {
  117. DRM_ERROR(
  118. "filp->private_data->minor->dev->dev_private == NULL\n");
  119. return -EINVAL;
  120. }
  121. QXL_INFO(qdev, "%s: filp->private_data = 0x%p, vma->vm_pgoff = %lx\n",
  122. __func__, filp->private_data, vma->vm_pgoff);
  123. r = ttm_bo_mmap(filp, vma, &qdev->mman.bdev);
  124. if (unlikely(r != 0))
  125. return r;
  126. if (unlikely(ttm_vm_ops == NULL)) {
  127. ttm_vm_ops = vma->vm_ops;
  128. qxl_ttm_vm_ops = *ttm_vm_ops;
  129. qxl_ttm_vm_ops.fault = &qxl_ttm_fault;
  130. }
  131. vma->vm_ops = &qxl_ttm_vm_ops;
  132. return 0;
  133. }
  134. static int qxl_invalidate_caches(struct ttm_bo_device *bdev, uint32_t flags)
  135. {
  136. return 0;
  137. }
  138. static int qxl_init_mem_type(struct ttm_bo_device *bdev, uint32_t type,
  139. struct ttm_mem_type_manager *man)
  140. {
  141. switch (type) {
  142. case TTM_PL_SYSTEM:
  143. /* System memory */
  144. man->flags = TTM_MEMTYPE_FLAG_MAPPABLE;
  145. man->available_caching = TTM_PL_MASK_CACHING;
  146. man->default_caching = TTM_PL_FLAG_CACHED;
  147. break;
  148. case TTM_PL_VRAM:
  149. case TTM_PL_PRIV:
  150. /* "On-card" video ram */
  151. man->func = &ttm_bo_manager_func;
  152. man->gpu_offset = 0;
  153. man->flags = TTM_MEMTYPE_FLAG_FIXED |
  154. TTM_MEMTYPE_FLAG_MAPPABLE;
  155. man->available_caching = TTM_PL_MASK_CACHING;
  156. man->default_caching = TTM_PL_FLAG_CACHED;
  157. break;
  158. default:
  159. DRM_ERROR("Unsupported memory type %u\n", (unsigned)type);
  160. return -EINVAL;
  161. }
  162. return 0;
  163. }
  164. static void qxl_evict_flags(struct ttm_buffer_object *bo,
  165. struct ttm_placement *placement)
  166. {
  167. struct qxl_bo *qbo;
  168. static const struct ttm_place placements = {
  169. .fpfn = 0,
  170. .lpfn = 0,
  171. .flags = TTM_PL_MASK_CACHING | TTM_PL_FLAG_SYSTEM
  172. };
  173. if (!qxl_ttm_bo_is_qxl_bo(bo)) {
  174. placement->placement = &placements;
  175. placement->busy_placement = &placements;
  176. placement->num_placement = 1;
  177. placement->num_busy_placement = 1;
  178. return;
  179. }
  180. qbo = to_qxl_bo(bo);
  181. qxl_ttm_placement_from_domain(qbo, QXL_GEM_DOMAIN_CPU, false);
  182. *placement = qbo->placement;
  183. }
  184. static int qxl_verify_access(struct ttm_buffer_object *bo, struct file *filp)
  185. {
  186. struct qxl_bo *qbo = to_qxl_bo(bo);
  187. return drm_vma_node_verify_access(&qbo->gem_base.vma_node,
  188. filp->private_data);
  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_PRIV:
  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. int ret;
  309. ret = ttm_bo_wait(bo, interruptible, no_wait_gpu);
  310. if (ret)
  311. return ret;
  312. if (old_mem->mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
  313. qxl_move_null(bo, new_mem);
  314. return 0;
  315. }
  316. return ttm_bo_move_memcpy(bo, interruptible, no_wait_gpu,
  317. new_mem);
  318. }
  319. static void qxl_bo_move_notify(struct ttm_buffer_object *bo,
  320. bool evict,
  321. struct ttm_mem_reg *new_mem)
  322. {
  323. struct qxl_bo *qbo;
  324. struct qxl_device *qdev;
  325. if (!qxl_ttm_bo_is_qxl_bo(bo))
  326. return;
  327. qbo = to_qxl_bo(bo);
  328. qdev = qbo->gem_base.dev->dev_private;
  329. if (bo->mem.mem_type == TTM_PL_PRIV && qbo->surface_id)
  330. qxl_surface_evict(qdev, qbo, new_mem ? true : false);
  331. }
  332. static struct ttm_bo_driver qxl_bo_driver = {
  333. .ttm_tt_create = &qxl_ttm_tt_create,
  334. .ttm_tt_populate = &qxl_ttm_tt_populate,
  335. .ttm_tt_unpopulate = &qxl_ttm_tt_unpopulate,
  336. .invalidate_caches = &qxl_invalidate_caches,
  337. .init_mem_type = &qxl_init_mem_type,
  338. .eviction_valuable = ttm_bo_eviction_valuable,
  339. .evict_flags = &qxl_evict_flags,
  340. .move = &qxl_bo_move,
  341. .verify_access = &qxl_verify_access,
  342. .io_mem_reserve = &qxl_ttm_io_mem_reserve,
  343. .io_mem_free = &qxl_ttm_io_mem_free,
  344. .io_mem_pfn = ttm_bo_default_io_mem_pfn,
  345. .move_notify = &qxl_bo_move_notify,
  346. };
  347. int qxl_ttm_init(struct qxl_device *qdev)
  348. {
  349. int r;
  350. int num_io_pages; /* != rom->num_io_pages, we include surface0 */
  351. r = qxl_ttm_global_init(qdev);
  352. if (r)
  353. return r;
  354. /* No others user of address space so set it to 0 */
  355. r = ttm_bo_device_init(&qdev->mman.bdev,
  356. qdev->mman.bo_global_ref.ref.object,
  357. &qxl_bo_driver,
  358. qdev->ddev.anon_inode->i_mapping,
  359. DRM_FILE_PAGE_OFFSET, 0);
  360. if (r) {
  361. DRM_ERROR("failed initializing buffer object driver(%d).\n", r);
  362. return r;
  363. }
  364. /* NOTE: this includes the framebuffer (aka surface 0) */
  365. num_io_pages = qdev->rom->ram_header_offset / PAGE_SIZE;
  366. r = ttm_bo_init_mm(&qdev->mman.bdev, TTM_PL_VRAM,
  367. num_io_pages);
  368. if (r) {
  369. DRM_ERROR("Failed initializing VRAM heap.\n");
  370. return r;
  371. }
  372. r = ttm_bo_init_mm(&qdev->mman.bdev, TTM_PL_PRIV,
  373. qdev->surfaceram_size / PAGE_SIZE);
  374. if (r) {
  375. DRM_ERROR("Failed initializing Surfaces heap.\n");
  376. return r;
  377. }
  378. DRM_INFO("qxl: %uM of VRAM memory size\n",
  379. (unsigned)qdev->vram_size / (1024 * 1024));
  380. DRM_INFO("qxl: %luM of IO pages memory ready (VRAM domain)\n",
  381. ((unsigned)num_io_pages * PAGE_SIZE) / (1024 * 1024));
  382. DRM_INFO("qxl: %uM of Surface memory size\n",
  383. (unsigned)qdev->surfaceram_size / (1024 * 1024));
  384. return 0;
  385. }
  386. void qxl_ttm_fini(struct qxl_device *qdev)
  387. {
  388. ttm_bo_clean_mm(&qdev->mman.bdev, TTM_PL_VRAM);
  389. ttm_bo_clean_mm(&qdev->mman.bdev, TTM_PL_PRIV);
  390. ttm_bo_device_release(&qdev->mman.bdev);
  391. qxl_ttm_global_fini(qdev);
  392. DRM_INFO("qxl: ttm finalized\n");
  393. }
  394. #define QXL_DEBUGFS_MEM_TYPES 2
  395. #if defined(CONFIG_DEBUG_FS)
  396. static int qxl_mm_dump_table(struct seq_file *m, void *data)
  397. {
  398. struct drm_info_node *node = (struct drm_info_node *)m->private;
  399. struct drm_mm *mm = (struct drm_mm *)node->info_ent->data;
  400. struct drm_device *dev = node->minor->dev;
  401. struct qxl_device *rdev = dev->dev_private;
  402. struct ttm_bo_global *glob = rdev->mman.bdev.glob;
  403. struct drm_printer p = drm_seq_file_printer(m);
  404. spin_lock(&glob->lru_lock);
  405. drm_mm_print(mm, &p);
  406. spin_unlock(&glob->lru_lock);
  407. return 0;
  408. }
  409. #endif
  410. int qxl_ttm_debugfs_init(struct qxl_device *qdev)
  411. {
  412. #if defined(CONFIG_DEBUG_FS)
  413. static struct drm_info_list qxl_mem_types_list[QXL_DEBUGFS_MEM_TYPES];
  414. static char qxl_mem_types_names[QXL_DEBUGFS_MEM_TYPES][32];
  415. unsigned i;
  416. for (i = 0; i < QXL_DEBUGFS_MEM_TYPES; i++) {
  417. if (i == 0)
  418. sprintf(qxl_mem_types_names[i], "qxl_mem_mm");
  419. else
  420. sprintf(qxl_mem_types_names[i], "qxl_surf_mm");
  421. qxl_mem_types_list[i].name = qxl_mem_types_names[i];
  422. qxl_mem_types_list[i].show = &qxl_mm_dump_table;
  423. qxl_mem_types_list[i].driver_features = 0;
  424. if (i == 0)
  425. qxl_mem_types_list[i].data = qdev->mman.bdev.man[TTM_PL_VRAM].priv;
  426. else
  427. qxl_mem_types_list[i].data = qdev->mman.bdev.man[TTM_PL_PRIV].priv;
  428. }
  429. return qxl_debugfs_add_files(qdev, qxl_mem_types_list, i);
  430. #else
  431. return 0;
  432. #endif
  433. }