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 <drm/ttm/ttm_bo_api.h>
27 #include <drm/ttm/ttm_bo_driver.h>
28 #include <drm/ttm/ttm_placement.h>
29 #include <drm/ttm/ttm_page_alloc.h>
30 #include <drm/ttm/ttm_module.h>
33 #include <drm/qxl_drm.h>
35 #include "qxl_object.h"
37 #include <linux/delay.h>
39 static struct qxl_device
*qxl_get_qdev(struct ttm_bo_device
*bdev
)
41 struct qxl_mman
*mman
;
42 struct qxl_device
*qdev
;
44 mman
= container_of(bdev
, struct qxl_mman
, bdev
);
45 qdev
= container_of(mman
, struct qxl_device
, mman
);
49 static int qxl_ttm_mem_global_init(struct drm_global_reference
*ref
)
51 return ttm_mem_global_init(ref
->object
);
54 static void qxl_ttm_mem_global_release(struct drm_global_reference
*ref
)
56 ttm_mem_global_release(ref
->object
);
59 static int qxl_ttm_global_init(struct qxl_device
*qdev
)
61 struct drm_global_reference
*global_ref
;
64 qdev
->mman
.mem_global_referenced
= false;
65 global_ref
= &qdev
->mman
.mem_global_ref
;
66 global_ref
->global_type
= DRM_GLOBAL_TTM_MEM
;
67 global_ref
->size
= sizeof(struct ttm_mem_global
);
68 global_ref
->init
= &qxl_ttm_mem_global_init
;
69 global_ref
->release
= &qxl_ttm_mem_global_release
;
71 r
= drm_global_item_ref(global_ref
);
73 DRM_ERROR("Failed setting up TTM memory accounting "
78 qdev
->mman
.bo_global_ref
.mem_glob
=
79 qdev
->mman
.mem_global_ref
.object
;
80 global_ref
= &qdev
->mman
.bo_global_ref
.ref
;
81 global_ref
->global_type
= DRM_GLOBAL_TTM_BO
;
82 global_ref
->size
= sizeof(struct ttm_bo_global
);
83 global_ref
->init
= &ttm_bo_global_init
;
84 global_ref
->release
= &ttm_bo_global_release
;
85 r
= drm_global_item_ref(global_ref
);
87 DRM_ERROR("Failed setting up TTM BO subsystem.\n");
88 drm_global_item_unref(&qdev
->mman
.mem_global_ref
);
92 qdev
->mman
.mem_global_referenced
= true;
96 static void qxl_ttm_global_fini(struct qxl_device
*qdev
)
98 if (qdev
->mman
.mem_global_referenced
) {
99 drm_global_item_unref(&qdev
->mman
.bo_global_ref
.ref
);
100 drm_global_item_unref(&qdev
->mman
.mem_global_ref
);
101 qdev
->mman
.mem_global_referenced
= false;
105 static struct vm_operations_struct qxl_ttm_vm_ops
;
106 static const struct vm_operations_struct
*ttm_vm_ops
;
108 static int qxl_ttm_fault(struct vm_fault
*vmf
)
110 struct ttm_buffer_object
*bo
;
113 bo
= (struct ttm_buffer_object
*)vmf
->vma
->vm_private_data
;
115 return VM_FAULT_NOPAGE
;
116 r
= ttm_vm_ops
->fault(vmf
);
120 int qxl_mmap(struct file
*filp
, struct vm_area_struct
*vma
)
122 struct drm_file
*file_priv
;
123 struct qxl_device
*qdev
;
126 if (unlikely(vma
->vm_pgoff
< DRM_FILE_PAGE_OFFSET
))
129 file_priv
= filp
->private_data
;
130 qdev
= file_priv
->minor
->dev
->dev_private
;
133 "filp->private_data->minor->dev->dev_private == NULL\n");
136 DRM_DEBUG_DRIVER("filp->private_data = 0x%p, vma->vm_pgoff = %lx\n",
137 filp
->private_data
, vma
->vm_pgoff
);
139 r
= ttm_bo_mmap(filp
, vma
, &qdev
->mman
.bdev
);
140 if (unlikely(r
!= 0))
142 if (unlikely(ttm_vm_ops
== NULL
)) {
143 ttm_vm_ops
= vma
->vm_ops
;
144 qxl_ttm_vm_ops
= *ttm_vm_ops
;
145 qxl_ttm_vm_ops
.fault
= &qxl_ttm_fault
;
147 vma
->vm_ops
= &qxl_ttm_vm_ops
;
151 static int qxl_invalidate_caches(struct ttm_bo_device
*bdev
, uint32_t flags
)
156 static int qxl_init_mem_type(struct ttm_bo_device
*bdev
, uint32_t type
,
157 struct ttm_mem_type_manager
*man
)
162 man
->flags
= TTM_MEMTYPE_FLAG_MAPPABLE
;
163 man
->available_caching
= TTM_PL_MASK_CACHING
;
164 man
->default_caching
= TTM_PL_FLAG_CACHED
;
168 /* "On-card" video ram */
169 man
->func
= &ttm_bo_manager_func
;
171 man
->flags
= TTM_MEMTYPE_FLAG_FIXED
|
172 TTM_MEMTYPE_FLAG_MAPPABLE
;
173 man
->available_caching
= TTM_PL_MASK_CACHING
;
174 man
->default_caching
= TTM_PL_FLAG_CACHED
;
177 DRM_ERROR("Unsupported memory type %u\n", (unsigned)type
);
183 static void qxl_evict_flags(struct ttm_buffer_object
*bo
,
184 struct ttm_placement
*placement
)
187 static const struct ttm_place placements
= {
190 .flags
= TTM_PL_MASK_CACHING
| TTM_PL_FLAG_SYSTEM
193 if (!qxl_ttm_bo_is_qxl_bo(bo
)) {
194 placement
->placement
= &placements
;
195 placement
->busy_placement
= &placements
;
196 placement
->num_placement
= 1;
197 placement
->num_busy_placement
= 1;
201 qxl_ttm_placement_from_domain(qbo
, QXL_GEM_DOMAIN_CPU
, false);
202 *placement
= qbo
->placement
;
205 static int qxl_verify_access(struct ttm_buffer_object
*bo
, struct file
*filp
)
207 struct qxl_bo
*qbo
= to_qxl_bo(bo
);
209 return drm_vma_node_verify_access(&qbo
->gem_base
.vma_node
,
213 static int qxl_ttm_io_mem_reserve(struct ttm_bo_device
*bdev
,
214 struct ttm_mem_reg
*mem
)
216 struct ttm_mem_type_manager
*man
= &bdev
->man
[mem
->mem_type
];
217 struct qxl_device
*qdev
= qxl_get_qdev(bdev
);
219 mem
->bus
.addr
= NULL
;
221 mem
->bus
.size
= mem
->num_pages
<< PAGE_SHIFT
;
223 mem
->bus
.is_iomem
= false;
224 if (!(man
->flags
& TTM_MEMTYPE_FLAG_MAPPABLE
))
226 switch (mem
->mem_type
) {
231 mem
->bus
.is_iomem
= true;
232 mem
->bus
.base
= qdev
->vram_base
;
233 mem
->bus
.offset
= mem
->start
<< PAGE_SHIFT
;
236 mem
->bus
.is_iomem
= true;
237 mem
->bus
.base
= qdev
->surfaceram_base
;
238 mem
->bus
.offset
= mem
->start
<< PAGE_SHIFT
;
246 static void qxl_ttm_io_mem_free(struct ttm_bo_device
*bdev
,
247 struct ttm_mem_reg
*mem
)
252 * TTM backend functions.
255 struct ttm_dma_tt ttm
;
256 struct qxl_device
*qdev
;
260 static int qxl_ttm_backend_bind(struct ttm_tt
*ttm
,
261 struct ttm_mem_reg
*bo_mem
)
263 struct qxl_ttm_tt
*gtt
= (void *)ttm
;
265 gtt
->offset
= (unsigned long)(bo_mem
->start
<< PAGE_SHIFT
);
266 if (!ttm
->num_pages
) {
267 WARN(1, "nothing to bind %lu pages for mreg %p back %p!\n",
268 ttm
->num_pages
, bo_mem
, ttm
);
270 /* Not implemented */
274 static int qxl_ttm_backend_unbind(struct ttm_tt
*ttm
)
276 /* Not implemented */
280 static void qxl_ttm_backend_destroy(struct ttm_tt
*ttm
)
282 struct qxl_ttm_tt
*gtt
= (void *)ttm
;
284 ttm_dma_tt_fini(>t
->ttm
);
288 static struct ttm_backend_func qxl_backend_func
= {
289 .bind
= &qxl_ttm_backend_bind
,
290 .unbind
= &qxl_ttm_backend_unbind
,
291 .destroy
= &qxl_ttm_backend_destroy
,
294 static int qxl_ttm_tt_populate(struct ttm_tt
*ttm
,
295 struct ttm_operation_ctx
*ctx
)
299 if (ttm
->state
!= tt_unpopulated
)
302 r
= ttm_pool_populate(ttm
, ctx
);
309 static void qxl_ttm_tt_unpopulate(struct ttm_tt
*ttm
)
311 ttm_pool_unpopulate(ttm
);
314 static struct ttm_tt
*qxl_ttm_tt_create(struct ttm_bo_device
*bdev
,
315 unsigned long size
, uint32_t page_flags
,
316 struct page
*dummy_read_page
)
318 struct qxl_device
*qdev
;
319 struct qxl_ttm_tt
*gtt
;
321 qdev
= qxl_get_qdev(bdev
);
322 gtt
= kzalloc(sizeof(struct qxl_ttm_tt
), GFP_KERNEL
);
325 gtt
->ttm
.ttm
.func
= &qxl_backend_func
;
327 if (ttm_dma_tt_init(>t
->ttm
, bdev
, size
, page_flags
,
332 return >t
->ttm
.ttm
;
335 static void qxl_move_null(struct ttm_buffer_object
*bo
,
336 struct ttm_mem_reg
*new_mem
)
338 struct ttm_mem_reg
*old_mem
= &bo
->mem
;
340 BUG_ON(old_mem
->mm_node
!= NULL
);
342 new_mem
->mm_node
= NULL
;
345 static int qxl_bo_move(struct ttm_buffer_object
*bo
, bool evict
,
346 struct ttm_operation_ctx
*ctx
,
347 struct ttm_mem_reg
*new_mem
)
349 struct ttm_mem_reg
*old_mem
= &bo
->mem
;
352 ret
= ttm_bo_wait(bo
, ctx
->interruptible
, ctx
->no_wait_gpu
);
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
, ctx
, new_mem
);
364 static void qxl_bo_move_notify(struct ttm_buffer_object
*bo
,
366 struct ttm_mem_reg
*new_mem
)
369 struct qxl_device
*qdev
;
371 if (!qxl_ttm_bo_is_qxl_bo(bo
))
374 qdev
= qbo
->gem_base
.dev
->dev_private
;
376 if (bo
->mem
.mem_type
== TTM_PL_PRIV
&& qbo
->surface_id
)
377 qxl_surface_evict(qdev
, qbo
, new_mem
? true : false);
380 static struct ttm_bo_driver qxl_bo_driver
= {
381 .ttm_tt_create
= &qxl_ttm_tt_create
,
382 .ttm_tt_populate
= &qxl_ttm_tt_populate
,
383 .ttm_tt_unpopulate
= &qxl_ttm_tt_unpopulate
,
384 .invalidate_caches
= &qxl_invalidate_caches
,
385 .init_mem_type
= &qxl_init_mem_type
,
386 .eviction_valuable
= ttm_bo_eviction_valuable
,
387 .evict_flags
= &qxl_evict_flags
,
388 .move
= &qxl_bo_move
,
389 .verify_access
= &qxl_verify_access
,
390 .io_mem_reserve
= &qxl_ttm_io_mem_reserve
,
391 .io_mem_free
= &qxl_ttm_io_mem_free
,
392 .move_notify
= &qxl_bo_move_notify
,
395 int qxl_ttm_init(struct qxl_device
*qdev
)
398 int num_io_pages
; /* != rom->num_io_pages, we include surface0 */
400 r
= qxl_ttm_global_init(qdev
);
403 /* No others user of address space so set it to 0 */
404 r
= ttm_bo_device_init(&qdev
->mman
.bdev
,
405 qdev
->mman
.bo_global_ref
.ref
.object
,
407 qdev
->ddev
.anon_inode
->i_mapping
,
408 DRM_FILE_PAGE_OFFSET
, 0);
410 DRM_ERROR("failed initializing buffer object driver(%d).\n", r
);
413 /* NOTE: this includes the framebuffer (aka surface 0) */
414 num_io_pages
= qdev
->rom
->ram_header_offset
/ PAGE_SIZE
;
415 r
= ttm_bo_init_mm(&qdev
->mman
.bdev
, TTM_PL_VRAM
,
418 DRM_ERROR("Failed initializing VRAM heap.\n");
421 r
= ttm_bo_init_mm(&qdev
->mman
.bdev
, TTM_PL_PRIV
,
422 qdev
->surfaceram_size
/ PAGE_SIZE
);
424 DRM_ERROR("Failed initializing Surfaces heap.\n");
427 DRM_INFO("qxl: %uM of VRAM memory size\n",
428 (unsigned)qdev
->vram_size
/ (1024 * 1024));
429 DRM_INFO("qxl: %luM of IO pages memory ready (VRAM domain)\n",
430 ((unsigned)num_io_pages
* PAGE_SIZE
) / (1024 * 1024));
431 DRM_INFO("qxl: %uM of Surface memory size\n",
432 (unsigned)qdev
->surfaceram_size
/ (1024 * 1024));
436 void qxl_ttm_fini(struct qxl_device
*qdev
)
438 ttm_bo_clean_mm(&qdev
->mman
.bdev
, TTM_PL_VRAM
);
439 ttm_bo_clean_mm(&qdev
->mman
.bdev
, TTM_PL_PRIV
);
440 ttm_bo_device_release(&qdev
->mman
.bdev
);
441 qxl_ttm_global_fini(qdev
);
442 DRM_INFO("qxl: ttm finalized\n");
446 #define QXL_DEBUGFS_MEM_TYPES 2
448 #if defined(CONFIG_DEBUG_FS)
449 static int qxl_mm_dump_table(struct seq_file
*m
, void *data
)
451 struct drm_info_node
*node
= (struct drm_info_node
*)m
->private;
452 struct drm_mm
*mm
= (struct drm_mm
*)node
->info_ent
->data
;
453 struct drm_device
*dev
= node
->minor
->dev
;
454 struct qxl_device
*rdev
= dev
->dev_private
;
455 struct ttm_bo_global
*glob
= rdev
->mman
.bdev
.glob
;
456 struct drm_printer p
= drm_seq_file_printer(m
);
458 spin_lock(&glob
->lru_lock
);
459 drm_mm_print(mm
, &p
);
460 spin_unlock(&glob
->lru_lock
);
465 int qxl_ttm_debugfs_init(struct qxl_device
*qdev
)
467 #if defined(CONFIG_DEBUG_FS)
468 static struct drm_info_list qxl_mem_types_list
[QXL_DEBUGFS_MEM_TYPES
];
469 static char qxl_mem_types_names
[QXL_DEBUGFS_MEM_TYPES
][32];
472 for (i
= 0; i
< QXL_DEBUGFS_MEM_TYPES
; i
++) {
474 sprintf(qxl_mem_types_names
[i
], "qxl_mem_mm");
476 sprintf(qxl_mem_types_names
[i
], "qxl_surf_mm");
477 qxl_mem_types_list
[i
].name
= qxl_mem_types_names
[i
];
478 qxl_mem_types_list
[i
].show
= &qxl_mm_dump_table
;
479 qxl_mem_types_list
[i
].driver_features
= 0;
481 qxl_mem_types_list
[i
].data
= qdev
->mman
.bdev
.man
[TTM_PL_VRAM
].priv
;
483 qxl_mem_types_list
[i
].data
= qdev
->mman
.bdev
.man
[TTM_PL_PRIV
].priv
;
486 return qxl_debugfs_add_files(qdev
, qxl_mem_types_list
, i
);