2 * Copyright 2013 Red Hat Inc.
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
22 * Authors: Dave Airlie
26 #include <ttm/ttm_bo_api.h>
27 #include <ttm/ttm_bo_driver.h>
28 #include <ttm/ttm_placement.h>
29 #include <ttm/ttm_page_alloc.h>
30 #include <ttm/ttm_module.h>
33 #include <drm/qxl_drm.h>
35 #include "qxl_object.h"
37 #include <linux/delay.h>
38 static int qxl_ttm_debugfs_init(struct qxl_device
*qdev
);
40 static struct qxl_device
*qxl_get_qdev(struct ttm_bo_device
*bdev
)
42 struct qxl_mman
*mman
;
43 struct qxl_device
*qdev
;
45 mman
= container_of(bdev
, struct qxl_mman
, bdev
);
46 qdev
= container_of(mman
, struct qxl_device
, mman
);
50 static int qxl_ttm_mem_global_init(struct drm_global_reference
*ref
)
52 return ttm_mem_global_init(ref
->object
);
55 static void qxl_ttm_mem_global_release(struct drm_global_reference
*ref
)
57 ttm_mem_global_release(ref
->object
);
60 static int qxl_ttm_global_init(struct qxl_device
*qdev
)
62 struct drm_global_reference
*global_ref
;
65 qdev
->mman
.mem_global_referenced
= false;
66 global_ref
= &qdev
->mman
.mem_global_ref
;
67 global_ref
->global_type
= DRM_GLOBAL_TTM_MEM
;
68 global_ref
->size
= sizeof(struct ttm_mem_global
);
69 global_ref
->init
= &qxl_ttm_mem_global_init
;
70 global_ref
->release
= &qxl_ttm_mem_global_release
;
72 r
= drm_global_item_ref(global_ref
);
74 DRM_ERROR("Failed setting up TTM memory accounting "
79 qdev
->mman
.bo_global_ref
.mem_glob
=
80 qdev
->mman
.mem_global_ref
.object
;
81 global_ref
= &qdev
->mman
.bo_global_ref
.ref
;
82 global_ref
->global_type
= DRM_GLOBAL_TTM_BO
;
83 global_ref
->size
= sizeof(struct ttm_bo_global
);
84 global_ref
->init
= &ttm_bo_global_init
;
85 global_ref
->release
= &ttm_bo_global_release
;
86 r
= drm_global_item_ref(global_ref
);
88 DRM_ERROR("Failed setting up TTM BO subsystem.\n");
89 drm_global_item_unref(&qdev
->mman
.mem_global_ref
);
93 qdev
->mman
.mem_global_referenced
= true;
97 static void qxl_ttm_global_fini(struct qxl_device
*qdev
)
99 if (qdev
->mman
.mem_global_referenced
) {
100 drm_global_item_unref(&qdev
->mman
.bo_global_ref
.ref
);
101 drm_global_item_unref(&qdev
->mman
.mem_global_ref
);
102 qdev
->mman
.mem_global_referenced
= false;
106 static struct vm_operations_struct qxl_ttm_vm_ops
;
107 static const struct vm_operations_struct
*ttm_vm_ops
;
109 static int qxl_ttm_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
111 struct ttm_buffer_object
*bo
;
112 struct qxl_device
*qdev
;
115 bo
= (struct ttm_buffer_object
*)vma
->vm_private_data
;
117 return VM_FAULT_NOPAGE
;
118 qdev
= qxl_get_qdev(bo
->bdev
);
119 r
= ttm_vm_ops
->fault(vma
, vmf
);
123 int qxl_mmap(struct file
*filp
, struct vm_area_struct
*vma
)
125 struct drm_file
*file_priv
;
126 struct qxl_device
*qdev
;
129 if (unlikely(vma
->vm_pgoff
< DRM_FILE_PAGE_OFFSET
)) {
130 pr_info("%s: vma->vm_pgoff (%ld) < DRM_FILE_PAGE_OFFSET\n",
131 __func__
, vma
->vm_pgoff
);
132 return drm_mmap(filp
, vma
);
135 file_priv
= filp
->private_data
;
136 qdev
= file_priv
->minor
->dev
->dev_private
;
139 "filp->private_data->minor->dev->dev_private == NULL\n");
142 QXL_INFO(qdev
, "%s: filp->private_data = 0x%p, vma->vm_pgoff = %lx\n",
143 __func__
, filp
->private_data
, vma
->vm_pgoff
);
145 r
= ttm_bo_mmap(filp
, vma
, &qdev
->mman
.bdev
);
146 if (unlikely(r
!= 0))
148 if (unlikely(ttm_vm_ops
== NULL
)) {
149 ttm_vm_ops
= vma
->vm_ops
;
150 qxl_ttm_vm_ops
= *ttm_vm_ops
;
151 qxl_ttm_vm_ops
.fault
= &qxl_ttm_fault
;
153 vma
->vm_ops
= &qxl_ttm_vm_ops
;
157 static int qxl_invalidate_caches(struct ttm_bo_device
*bdev
, uint32_t flags
)
162 static int qxl_init_mem_type(struct ttm_bo_device
*bdev
, uint32_t type
,
163 struct ttm_mem_type_manager
*man
)
165 struct qxl_device
*qdev
;
167 qdev
= qxl_get_qdev(bdev
);
172 man
->flags
= TTM_MEMTYPE_FLAG_MAPPABLE
;
173 man
->available_caching
= TTM_PL_MASK_CACHING
;
174 man
->default_caching
= TTM_PL_FLAG_CACHED
;
178 /* "On-card" video ram */
179 man
->func
= &ttm_bo_manager_func
;
181 man
->flags
= TTM_MEMTYPE_FLAG_FIXED
|
182 TTM_MEMTYPE_FLAG_MAPPABLE
;
183 man
->available_caching
= TTM_PL_MASK_CACHING
;
184 man
->default_caching
= TTM_PL_FLAG_CACHED
;
187 DRM_ERROR("Unsupported memory type %u\n", (unsigned)type
);
193 static void qxl_evict_flags(struct ttm_buffer_object
*bo
,
194 struct ttm_placement
*placement
)
197 static u32 placements
= TTM_PL_MASK_CACHING
| TTM_PL_FLAG_SYSTEM
;
199 if (!qxl_ttm_bo_is_qxl_bo(bo
)) {
202 placement
->placement
= &placements
;
203 placement
->busy_placement
= &placements
;
204 placement
->num_placement
= 1;
205 placement
->num_busy_placement
= 1;
208 qbo
= container_of(bo
, struct qxl_bo
, tbo
);
209 qxl_ttm_placement_from_domain(qbo
, QXL_GEM_DOMAIN_CPU
, false);
210 *placement
= qbo
->placement
;
213 static int qxl_verify_access(struct ttm_buffer_object
*bo
, struct file
*filp
)
215 struct qxl_bo
*qbo
= to_qxl_bo(bo
);
217 return drm_vma_node_verify_access(&qbo
->gem_base
.vma_node
, filp
);
220 static int qxl_ttm_io_mem_reserve(struct ttm_bo_device
*bdev
,
221 struct ttm_mem_reg
*mem
)
223 struct ttm_mem_type_manager
*man
= &bdev
->man
[mem
->mem_type
];
224 struct qxl_device
*qdev
= qxl_get_qdev(bdev
);
226 mem
->bus
.addr
= NULL
;
228 mem
->bus
.size
= mem
->num_pages
<< PAGE_SHIFT
;
230 mem
->bus
.is_iomem
= false;
231 if (!(man
->flags
& TTM_MEMTYPE_FLAG_MAPPABLE
))
233 switch (mem
->mem_type
) {
238 mem
->bus
.is_iomem
= true;
239 mem
->bus
.base
= qdev
->vram_base
;
240 mem
->bus
.offset
= mem
->start
<< PAGE_SHIFT
;
243 mem
->bus
.is_iomem
= true;
244 mem
->bus
.base
= qdev
->surfaceram_base
;
245 mem
->bus
.offset
= mem
->start
<< PAGE_SHIFT
;
253 static void qxl_ttm_io_mem_free(struct ttm_bo_device
*bdev
,
254 struct ttm_mem_reg
*mem
)
259 * TTM backend functions.
262 struct ttm_dma_tt ttm
;
263 struct qxl_device
*qdev
;
267 static int qxl_ttm_backend_bind(struct ttm_tt
*ttm
,
268 struct ttm_mem_reg
*bo_mem
)
270 struct qxl_ttm_tt
*gtt
= (void *)ttm
;
272 gtt
->offset
= (unsigned long)(bo_mem
->start
<< PAGE_SHIFT
);
273 if (!ttm
->num_pages
) {
274 WARN(1, "nothing to bind %lu pages for mreg %p back %p!\n",
275 ttm
->num_pages
, bo_mem
, ttm
);
277 /* Not implemented */
281 static int qxl_ttm_backend_unbind(struct ttm_tt
*ttm
)
283 /* Not implemented */
287 static void qxl_ttm_backend_destroy(struct ttm_tt
*ttm
)
289 struct qxl_ttm_tt
*gtt
= (void *)ttm
;
291 ttm_dma_tt_fini(>t
->ttm
);
295 static struct ttm_backend_func qxl_backend_func
= {
296 .bind
= &qxl_ttm_backend_bind
,
297 .unbind
= &qxl_ttm_backend_unbind
,
298 .destroy
= &qxl_ttm_backend_destroy
,
301 static int qxl_ttm_tt_populate(struct ttm_tt
*ttm
)
305 if (ttm
->state
!= tt_unpopulated
)
308 r
= ttm_pool_populate(ttm
);
315 static void qxl_ttm_tt_unpopulate(struct ttm_tt
*ttm
)
317 ttm_pool_unpopulate(ttm
);
320 static struct ttm_tt
*qxl_ttm_tt_create(struct ttm_bo_device
*bdev
,
321 unsigned long size
, uint32_t page_flags
,
322 struct page
*dummy_read_page
)
324 struct qxl_device
*qdev
;
325 struct qxl_ttm_tt
*gtt
;
327 qdev
= qxl_get_qdev(bdev
);
328 gtt
= kzalloc(sizeof(struct qxl_ttm_tt
), GFP_KERNEL
);
331 gtt
->ttm
.ttm
.func
= &qxl_backend_func
;
333 if (ttm_dma_tt_init(>t
->ttm
, bdev
, size
, page_flags
,
338 return >t
->ttm
.ttm
;
341 static void qxl_move_null(struct ttm_buffer_object
*bo
,
342 struct ttm_mem_reg
*new_mem
)
344 struct ttm_mem_reg
*old_mem
= &bo
->mem
;
346 BUG_ON(old_mem
->mm_node
!= NULL
);
348 new_mem
->mm_node
= NULL
;
351 static int qxl_bo_move(struct ttm_buffer_object
*bo
,
352 bool evict
, bool interruptible
,
354 struct ttm_mem_reg
*new_mem
)
356 struct ttm_mem_reg
*old_mem
= &bo
->mem
;
357 if (old_mem
->mem_type
== TTM_PL_SYSTEM
&& bo
->ttm
== NULL
) {
358 qxl_move_null(bo
, new_mem
);
361 return ttm_bo_move_memcpy(bo
, evict
, no_wait_gpu
, new_mem
);
365 static int qxl_sync_obj_wait(void *sync_obj
,
366 bool lazy
, bool interruptible
)
368 struct qxl_fence
*qfence
= (struct qxl_fence
*)sync_obj
;
369 int count
= 0, sc
= 0;
370 struct qxl_bo
*bo
= container_of(qfence
, struct qxl_bo
, fence
);
372 if (qfence
->num_active_releases
== 0)
377 if (bo
->type
== QXL_GEM_DOMAIN_SURFACE
)
378 qxl_update_surface(qfence
->qdev
, bo
);
379 } else if (sc
>= 1) {
380 qxl_io_notify_oom(qfence
->qdev
);
385 for (count
= 0; count
< 10; count
++) {
387 ret
= qxl_queue_garbage_collect(qfence
->qdev
, true);
391 if (qfence
->num_active_releases
== 0)
395 if (qfence
->num_active_releases
) {
396 bool have_drawable_releases
= false;
398 struct radix_tree_iter iter
;
401 radix_tree_for_each_slot(slot
, &qfence
->tree
, &iter
, 0) {
402 struct qxl_release
*release
;
404 release_id
= iter
.index
;
405 release
= qxl_release_from_id_locked(qfence
->qdev
, release_id
);
409 if (release
->type
== QXL_RELEASE_DRAWABLE
)
410 have_drawable_releases
= true;
413 qxl_queue_garbage_collect(qfence
->qdev
, true);
415 if (have_drawable_releases
|| sc
< 4) {
418 usleep_range(500, 1000);
419 if (have_drawable_releases
&& sc
> 300) {
420 WARN(1, "sync obj %d still has outstanding releases %d %d %d %ld %d\n", sc
, bo
->surface_id
, bo
->is_primary
, bo
->pin_count
, (unsigned long)bo
->gem_base
.size
, qfence
->num_active_releases
);
429 static int qxl_sync_obj_flush(void *sync_obj
)
434 static void qxl_sync_obj_unref(void **sync_obj
)
438 static void *qxl_sync_obj_ref(void *sync_obj
)
443 static bool qxl_sync_obj_signaled(void *sync_obj
)
445 struct qxl_fence
*qfence
= (struct qxl_fence
*)sync_obj
;
446 return (qfence
->num_active_releases
== 0);
449 static void qxl_bo_move_notify(struct ttm_buffer_object
*bo
,
450 struct ttm_mem_reg
*new_mem
)
453 struct qxl_device
*qdev
;
455 if (!qxl_ttm_bo_is_qxl_bo(bo
))
457 qbo
= container_of(bo
, struct qxl_bo
, tbo
);
458 qdev
= qbo
->gem_base
.dev
->dev_private
;
460 if (bo
->mem
.mem_type
== TTM_PL_PRIV0
&& qbo
->surface_id
)
461 qxl_surface_evict(qdev
, qbo
, new_mem
? true : false);
464 static struct ttm_bo_driver qxl_bo_driver
= {
465 .ttm_tt_create
= &qxl_ttm_tt_create
,
466 .ttm_tt_populate
= &qxl_ttm_tt_populate
,
467 .ttm_tt_unpopulate
= &qxl_ttm_tt_unpopulate
,
468 .invalidate_caches
= &qxl_invalidate_caches
,
469 .init_mem_type
= &qxl_init_mem_type
,
470 .evict_flags
= &qxl_evict_flags
,
471 .move
= &qxl_bo_move
,
472 .verify_access
= &qxl_verify_access
,
473 .io_mem_reserve
= &qxl_ttm_io_mem_reserve
,
474 .io_mem_free
= &qxl_ttm_io_mem_free
,
475 .sync_obj_signaled
= &qxl_sync_obj_signaled
,
476 .sync_obj_wait
= &qxl_sync_obj_wait
,
477 .sync_obj_flush
= &qxl_sync_obj_flush
,
478 .sync_obj_unref
= &qxl_sync_obj_unref
,
479 .sync_obj_ref
= &qxl_sync_obj_ref
,
480 .move_notify
= &qxl_bo_move_notify
,
485 int qxl_ttm_init(struct qxl_device
*qdev
)
488 int num_io_pages
; /* != rom->num_io_pages, we include surface0 */
490 r
= qxl_ttm_global_init(qdev
);
493 /* No others user of address space so set it to 0 */
494 r
= ttm_bo_device_init(&qdev
->mman
.bdev
,
495 qdev
->mman
.bo_global_ref
.ref
.object
,
496 &qxl_bo_driver
, DRM_FILE_PAGE_OFFSET
, 0);
498 DRM_ERROR("failed initializing buffer object driver(%d).\n", r
);
501 /* NOTE: this includes the framebuffer (aka surface 0) */
502 num_io_pages
= qdev
->rom
->ram_header_offset
/ PAGE_SIZE
;
503 r
= ttm_bo_init_mm(&qdev
->mman
.bdev
, TTM_PL_VRAM
,
506 DRM_ERROR("Failed initializing VRAM heap.\n");
509 r
= ttm_bo_init_mm(&qdev
->mman
.bdev
, TTM_PL_PRIV0
,
510 qdev
->surfaceram_size
/ PAGE_SIZE
);
512 DRM_ERROR("Failed initializing Surfaces heap.\n");
515 DRM_INFO("qxl: %uM of VRAM memory size\n",
516 (unsigned)qdev
->vram_size
/ (1024 * 1024));
517 DRM_INFO("qxl: %luM of IO pages memory ready (VRAM domain)\n",
518 ((unsigned)num_io_pages
* PAGE_SIZE
) / (1024 * 1024));
519 DRM_INFO("qxl: %uM of Surface memory size\n",
520 (unsigned)qdev
->surfaceram_size
/ (1024 * 1024));
521 if (unlikely(qdev
->mman
.bdev
.dev_mapping
== NULL
))
522 qdev
->mman
.bdev
.dev_mapping
= qdev
->ddev
->dev_mapping
;
523 r
= qxl_ttm_debugfs_init(qdev
);
525 DRM_ERROR("Failed to init debugfs\n");
531 void qxl_ttm_fini(struct qxl_device
*qdev
)
533 ttm_bo_clean_mm(&qdev
->mman
.bdev
, TTM_PL_VRAM
);
534 ttm_bo_clean_mm(&qdev
->mman
.bdev
, TTM_PL_PRIV0
);
535 ttm_bo_device_release(&qdev
->mman
.bdev
);
536 qxl_ttm_global_fini(qdev
);
537 DRM_INFO("qxl: ttm finalized\n");
541 #define QXL_DEBUGFS_MEM_TYPES 2
543 #if defined(CONFIG_DEBUG_FS)
544 static int qxl_mm_dump_table(struct seq_file
*m
, void *data
)
546 struct drm_info_node
*node
= (struct drm_info_node
*)m
->private;
547 struct drm_mm
*mm
= (struct drm_mm
*)node
->info_ent
->data
;
548 struct drm_device
*dev
= node
->minor
->dev
;
549 struct qxl_device
*rdev
= dev
->dev_private
;
551 struct ttm_bo_global
*glob
= rdev
->mman
.bdev
.glob
;
553 spin_lock(&glob
->lru_lock
);
554 ret
= drm_mm_dump_table(m
, mm
);
555 spin_unlock(&glob
->lru_lock
);
560 static int qxl_ttm_debugfs_init(struct qxl_device
*qdev
)
562 #if defined(CONFIG_DEBUG_FS)
563 static struct drm_info_list qxl_mem_types_list
[QXL_DEBUGFS_MEM_TYPES
];
564 static char qxl_mem_types_names
[QXL_DEBUGFS_MEM_TYPES
][32];
567 for (i
= 0; i
< QXL_DEBUGFS_MEM_TYPES
; i
++) {
569 sprintf(qxl_mem_types_names
[i
], "qxl_mem_mm");
571 sprintf(qxl_mem_types_names
[i
], "qxl_surf_mm");
572 qxl_mem_types_list
[i
].name
= qxl_mem_types_names
[i
];
573 qxl_mem_types_list
[i
].show
= &qxl_mm_dump_table
;
574 qxl_mem_types_list
[i
].driver_features
= 0;
576 qxl_mem_types_list
[i
].data
= qdev
->mman
.bdev
.man
[TTM_PL_VRAM
].priv
;
578 qxl_mem_types_list
[i
].data
= qdev
->mman
.bdev
.man
[TTM_PL_PRIV0
].priv
;
581 return qxl_debugfs_add_files(qdev
, qxl_mem_types_list
, i
);