blob: 11761330a6b8a52ca5d5fc8b74ac7c7d23f6bf6e [file] [log] [blame]
Dave Airlief64122c2013-02-25 14:47:55 +10001/*
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
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>
31#include <drm/drmP.h>
32#include <drm/drm.h>
33#include <drm/qxl_drm.h>
34#include "qxl_drv.h"
35#include "qxl_object.h"
36
37#include <linux/delay.h>
38static int qxl_ttm_debugfs_init(struct qxl_device *qdev);
39
40static struct qxl_device *qxl_get_qdev(struct ttm_bo_device *bdev)
41{
42 struct qxl_mman *mman;
43 struct qxl_device *qdev;
44
45 mman = container_of(bdev, struct qxl_mman, bdev);
46 qdev = container_of(mman, struct qxl_device, mman);
47 return qdev;
48}
49
50static int qxl_ttm_mem_global_init(struct drm_global_reference *ref)
51{
52 return ttm_mem_global_init(ref->object);
53}
54
55static void qxl_ttm_mem_global_release(struct drm_global_reference *ref)
56{
57 ttm_mem_global_release(ref->object);
58}
59
60static int qxl_ttm_global_init(struct qxl_device *qdev)
61{
62 struct drm_global_reference *global_ref;
63 int r;
64
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;
71
72 r = drm_global_item_ref(global_ref);
73 if (r != 0) {
74 DRM_ERROR("Failed setting up TTM memory accounting "
75 "subsystem.\n");
76 return r;
77 }
78
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);
87 if (r != 0) {
88 DRM_ERROR("Failed setting up TTM BO subsystem.\n");
89 drm_global_item_unref(&qdev->mman.mem_global_ref);
90 return r;
91 }
92
93 qdev->mman.mem_global_referenced = true;
94 return 0;
95}
96
97static void qxl_ttm_global_fini(struct qxl_device *qdev)
98{
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;
103 }
104}
105
106static struct vm_operations_struct qxl_ttm_vm_ops;
107static const struct vm_operations_struct *ttm_vm_ops;
108
109static int qxl_ttm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
110{
111 struct ttm_buffer_object *bo;
Dave Airlief64122c2013-02-25 14:47:55 +1000112 int r;
113
114 bo = (struct ttm_buffer_object *)vma->vm_private_data;
115 if (bo == NULL)
116 return VM_FAULT_NOPAGE;
Dave Airlief64122c2013-02-25 14:47:55 +1000117 r = ttm_vm_ops->fault(vma, vmf);
118 return r;
119}
120
121int qxl_mmap(struct file *filp, struct vm_area_struct *vma)
122{
123 struct drm_file *file_priv;
124 struct qxl_device *qdev;
125 int r;
126
127 if (unlikely(vma->vm_pgoff < DRM_FILE_PAGE_OFFSET)) {
128 pr_info("%s: vma->vm_pgoff (%ld) < DRM_FILE_PAGE_OFFSET\n",
129 __func__, vma->vm_pgoff);
Daniel Vetter884c6da2014-09-23 15:46:47 +0200130 return -EINVAL;
Dave Airlief64122c2013-02-25 14:47:55 +1000131 }
132
133 file_priv = filp->private_data;
134 qdev = file_priv->minor->dev->dev_private;
135 if (qdev == NULL) {
136 DRM_ERROR(
137 "filp->private_data->minor->dev->dev_private == NULL\n");
138 return -EINVAL;
139 }
140 QXL_INFO(qdev, "%s: filp->private_data = 0x%p, vma->vm_pgoff = %lx\n",
141 __func__, filp->private_data, vma->vm_pgoff);
142
143 r = ttm_bo_mmap(filp, vma, &qdev->mman.bdev);
144 if (unlikely(r != 0))
145 return r;
146 if (unlikely(ttm_vm_ops == NULL)) {
147 ttm_vm_ops = vma->vm_ops;
148 qxl_ttm_vm_ops = *ttm_vm_ops;
149 qxl_ttm_vm_ops.fault = &qxl_ttm_fault;
150 }
151 vma->vm_ops = &qxl_ttm_vm_ops;
152 return 0;
153}
154
155static int qxl_invalidate_caches(struct ttm_bo_device *bdev, uint32_t flags)
156{
157 return 0;
158}
159
160static int qxl_init_mem_type(struct ttm_bo_device *bdev, uint32_t type,
161 struct ttm_mem_type_manager *man)
162{
Dave Airlief64122c2013-02-25 14:47:55 +1000163 switch (type) {
164 case TTM_PL_SYSTEM:
165 /* System memory */
166 man->flags = TTM_MEMTYPE_FLAG_MAPPABLE;
167 man->available_caching = TTM_PL_MASK_CACHING;
168 man->default_caching = TTM_PL_FLAG_CACHED;
169 break;
170 case TTM_PL_VRAM:
Christian König283cde62016-09-12 13:34:37 +0200171 case TTM_PL_PRIV:
Dave Airlief64122c2013-02-25 14:47:55 +1000172 /* "On-card" video ram */
173 man->func = &ttm_bo_manager_func;
174 man->gpu_offset = 0;
175 man->flags = TTM_MEMTYPE_FLAG_FIXED |
176 TTM_MEMTYPE_FLAG_MAPPABLE;
177 man->available_caching = TTM_PL_MASK_CACHING;
178 man->default_caching = TTM_PL_FLAG_CACHED;
179 break;
180 default:
181 DRM_ERROR("Unsupported memory type %u\n", (unsigned)type);
182 return -EINVAL;
183 }
184 return 0;
185}
186
187static void qxl_evict_flags(struct ttm_buffer_object *bo,
188 struct ttm_placement *placement)
189{
190 struct qxl_bo *qbo;
Christian Königf1217ed2014-08-27 13:16:04 +0200191 static struct ttm_place placements = {
192 .fpfn = 0,
193 .lpfn = 0,
194 .flags = TTM_PL_MASK_CACHING | TTM_PL_FLAG_SYSTEM
195 };
Dave Airlief64122c2013-02-25 14:47:55 +1000196
197 if (!qxl_ttm_bo_is_qxl_bo(bo)) {
Dave Airlief64122c2013-02-25 14:47:55 +1000198 placement->placement = &placements;
199 placement->busy_placement = &placements;
200 placement->num_placement = 1;
201 placement->num_busy_placement = 1;
202 return;
203 }
Frediano Ziglio3ebf1c62015-12-15 12:20:56 +0100204 qbo = to_qxl_bo(bo);
Dave Airlie4f49ec92013-07-23 14:06:07 +1000205 qxl_ttm_placement_from_domain(qbo, QXL_GEM_DOMAIN_CPU, false);
Dave Airlief64122c2013-02-25 14:47:55 +1000206 *placement = qbo->placement;
207}
208
209static int qxl_verify_access(struct ttm_buffer_object *bo, struct file *filp)
210{
David Herrmannacb46522013-08-25 18:28:59 +0200211 struct qxl_bo *qbo = to_qxl_bo(bo);
212
David Herrmannd9a1f0b2016-09-01 14:48:33 +0200213 return drm_vma_node_verify_access(&qbo->gem_base.vma_node,
214 filp->private_data);
Dave Airlief64122c2013-02-25 14:47:55 +1000215}
216
217static int qxl_ttm_io_mem_reserve(struct ttm_bo_device *bdev,
218 struct ttm_mem_reg *mem)
219{
220 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
221 struct qxl_device *qdev = qxl_get_qdev(bdev);
222
223 mem->bus.addr = NULL;
224 mem->bus.offset = 0;
225 mem->bus.size = mem->num_pages << PAGE_SHIFT;
226 mem->bus.base = 0;
227 mem->bus.is_iomem = false;
228 if (!(man->flags & TTM_MEMTYPE_FLAG_MAPPABLE))
229 return -EINVAL;
230 switch (mem->mem_type) {
231 case TTM_PL_SYSTEM:
232 /* system memory */
233 return 0;
234 case TTM_PL_VRAM:
235 mem->bus.is_iomem = true;
236 mem->bus.base = qdev->vram_base;
237 mem->bus.offset = mem->start << PAGE_SHIFT;
238 break;
Christian König283cde62016-09-12 13:34:37 +0200239 case TTM_PL_PRIV:
Dave Airlief64122c2013-02-25 14:47:55 +1000240 mem->bus.is_iomem = true;
241 mem->bus.base = qdev->surfaceram_base;
242 mem->bus.offset = mem->start << PAGE_SHIFT;
243 break;
244 default:
245 return -EINVAL;
246 }
247 return 0;
248}
249
250static void qxl_ttm_io_mem_free(struct ttm_bo_device *bdev,
251 struct ttm_mem_reg *mem)
252{
253}
254
255/*
256 * TTM backend functions.
257 */
258struct qxl_ttm_tt {
259 struct ttm_dma_tt ttm;
260 struct qxl_device *qdev;
261 u64 offset;
262};
263
264static int qxl_ttm_backend_bind(struct ttm_tt *ttm,
265 struct ttm_mem_reg *bo_mem)
266{
267 struct qxl_ttm_tt *gtt = (void *)ttm;
268
269 gtt->offset = (unsigned long)(bo_mem->start << PAGE_SHIFT);
270 if (!ttm->num_pages) {
271 WARN(1, "nothing to bind %lu pages for mreg %p back %p!\n",
272 ttm->num_pages, bo_mem, ttm);
273 }
274 /* Not implemented */
275 return -1;
276}
277
278static int qxl_ttm_backend_unbind(struct ttm_tt *ttm)
279{
280 /* Not implemented */
281 return -1;
282}
283
284static void qxl_ttm_backend_destroy(struct ttm_tt *ttm)
285{
286 struct qxl_ttm_tt *gtt = (void *)ttm;
287
288 ttm_dma_tt_fini(&gtt->ttm);
289 kfree(gtt);
290}
291
292static struct ttm_backend_func qxl_backend_func = {
293 .bind = &qxl_ttm_backend_bind,
294 .unbind = &qxl_ttm_backend_unbind,
295 .destroy = &qxl_ttm_backend_destroy,
296};
297
298static int qxl_ttm_tt_populate(struct ttm_tt *ttm)
299{
300 int r;
301
302 if (ttm->state != tt_unpopulated)
303 return 0;
304
305 r = ttm_pool_populate(ttm);
306 if (r)
307 return r;
308
309 return 0;
310}
311
312static void qxl_ttm_tt_unpopulate(struct ttm_tt *ttm)
313{
314 ttm_pool_unpopulate(ttm);
315}
316
Dave Airlie6d01f1f2013-04-16 13:24:25 +1000317static struct ttm_tt *qxl_ttm_tt_create(struct ttm_bo_device *bdev,
318 unsigned long size, uint32_t page_flags,
319 struct page *dummy_read_page)
Dave Airlief64122c2013-02-25 14:47:55 +1000320{
321 struct qxl_device *qdev;
322 struct qxl_ttm_tt *gtt;
323
324 qdev = qxl_get_qdev(bdev);
325 gtt = kzalloc(sizeof(struct qxl_ttm_tt), GFP_KERNEL);
326 if (gtt == NULL)
327 return NULL;
328 gtt->ttm.ttm.func = &qxl_backend_func;
329 gtt->qdev = qdev;
330 if (ttm_dma_tt_init(&gtt->ttm, bdev, size, page_flags,
331 dummy_read_page)) {
332 kfree(gtt);
333 return NULL;
334 }
335 return &gtt->ttm.ttm;
336}
337
338static void qxl_move_null(struct ttm_buffer_object *bo,
339 struct ttm_mem_reg *new_mem)
340{
341 struct ttm_mem_reg *old_mem = &bo->mem;
342
343 BUG_ON(old_mem->mm_node != NULL);
344 *old_mem = *new_mem;
345 new_mem->mm_node = NULL;
346}
347
348static int qxl_bo_move(struct ttm_buffer_object *bo,
349 bool evict, bool interruptible,
350 bool no_wait_gpu,
351 struct ttm_mem_reg *new_mem)
352{
353 struct ttm_mem_reg *old_mem = &bo->mem;
Christian König88932a72016-06-06 10:17:53 +0200354 int ret;
355
356 ret = ttm_bo_wait(bo, interruptible, no_wait_gpu);
357 if (ret)
358 return ret;
359
360
Dave Airlief64122c2013-02-25 14:47:55 +1000361 if (old_mem->mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
362 qxl_move_null(bo, new_mem);
363 return 0;
364 }
Michel Dänzer4499f2a2016-08-08 12:28:26 +0900365 return ttm_bo_move_memcpy(bo, interruptible, no_wait_gpu,
366 new_mem);
Dave Airlief64122c2013-02-25 14:47:55 +1000367}
368
Dave Airlief64122c2013-02-25 14:47:55 +1000369static void qxl_bo_move_notify(struct ttm_buffer_object *bo,
370 struct ttm_mem_reg *new_mem)
371{
372 struct qxl_bo *qbo;
373 struct qxl_device *qdev;
374
375 if (!qxl_ttm_bo_is_qxl_bo(bo))
376 return;
Frediano Ziglio3ebf1c62015-12-15 12:20:56 +0100377 qbo = to_qxl_bo(bo);
Dave Airlief64122c2013-02-25 14:47:55 +1000378 qdev = qbo->gem_base.dev->dev_private;
379
Christian König283cde62016-09-12 13:34:37 +0200380 if (bo->mem.mem_type == TTM_PL_PRIV && qbo->surface_id)
Dave Airlief64122c2013-02-25 14:47:55 +1000381 qxl_surface_evict(qdev, qbo, new_mem ? true : false);
382}
383
384static struct ttm_bo_driver qxl_bo_driver = {
385 .ttm_tt_create = &qxl_ttm_tt_create,
386 .ttm_tt_populate = &qxl_ttm_tt_populate,
387 .ttm_tt_unpopulate = &qxl_ttm_tt_unpopulate,
388 .invalidate_caches = &qxl_invalidate_caches,
389 .init_mem_type = &qxl_init_mem_type,
Christian Königa2ab19fe2016-08-30 17:26:04 +0200390 .eviction_valuable = ttm_bo_eviction_valuable,
Dave Airlief64122c2013-02-25 14:47:55 +1000391 .evict_flags = &qxl_evict_flags,
392 .move = &qxl_bo_move,
393 .verify_access = &qxl_verify_access,
394 .io_mem_reserve = &qxl_ttm_io_mem_reserve,
395 .io_mem_free = &qxl_ttm_io_mem_free,
Dave Airlief64122c2013-02-25 14:47:55 +1000396 .move_notify = &qxl_bo_move_notify,
Christian König98c28722016-04-06 11:12:07 +0200397 .lru_tail = &ttm_bo_default_lru_tail,
398 .swap_lru_tail = &ttm_bo_default_swap_lru_tail,
Dave Airlief64122c2013-02-25 14:47:55 +1000399};
400
Dave Airlief64122c2013-02-25 14:47:55 +1000401int qxl_ttm_init(struct qxl_device *qdev)
402{
403 int r;
404 int num_io_pages; /* != rom->num_io_pages, we include surface0 */
405
406 r = qxl_ttm_global_init(qdev);
407 if (r)
408 return r;
409 /* No others user of address space so set it to 0 */
410 r = ttm_bo_device_init(&qdev->mman.bdev,
411 qdev->mman.bo_global_ref.ref.object,
David Herrmann44d847b2013-08-13 19:10:30 +0200412 &qxl_bo_driver,
413 qdev->ddev->anon_inode->i_mapping,
414 DRM_FILE_PAGE_OFFSET, 0);
Dave Airlief64122c2013-02-25 14:47:55 +1000415 if (r) {
416 DRM_ERROR("failed initializing buffer object driver(%d).\n", r);
417 return r;
418 }
419 /* NOTE: this includes the framebuffer (aka surface 0) */
420 num_io_pages = qdev->rom->ram_header_offset / PAGE_SIZE;
421 r = ttm_bo_init_mm(&qdev->mman.bdev, TTM_PL_VRAM,
422 num_io_pages);
423 if (r) {
424 DRM_ERROR("Failed initializing VRAM heap.\n");
425 return r;
426 }
Christian König283cde62016-09-12 13:34:37 +0200427 r = ttm_bo_init_mm(&qdev->mman.bdev, TTM_PL_PRIV,
Dave Airlief64122c2013-02-25 14:47:55 +1000428 qdev->surfaceram_size / PAGE_SIZE);
429 if (r) {
430 DRM_ERROR("Failed initializing Surfaces heap.\n");
431 return r;
432 }
433 DRM_INFO("qxl: %uM of VRAM memory size\n",
434 (unsigned)qdev->vram_size / (1024 * 1024));
435 DRM_INFO("qxl: %luM of IO pages memory ready (VRAM domain)\n",
436 ((unsigned)num_io_pages * PAGE_SIZE) / (1024 * 1024));
Gerd Hoffmannd9bbf182013-10-11 10:01:11 +0200437 DRM_INFO("qxl: %uM of Surface memory size\n",
438 (unsigned)qdev->surfaceram_size / (1024 * 1024));
Dave Airlief64122c2013-02-25 14:47:55 +1000439 r = qxl_ttm_debugfs_init(qdev);
440 if (r) {
441 DRM_ERROR("Failed to init debugfs\n");
442 return r;
443 }
444 return 0;
445}
446
447void qxl_ttm_fini(struct qxl_device *qdev)
448{
449 ttm_bo_clean_mm(&qdev->mman.bdev, TTM_PL_VRAM);
Christian König283cde62016-09-12 13:34:37 +0200450 ttm_bo_clean_mm(&qdev->mman.bdev, TTM_PL_PRIV);
Dave Airlief64122c2013-02-25 14:47:55 +1000451 ttm_bo_device_release(&qdev->mman.bdev);
452 qxl_ttm_global_fini(qdev);
453 DRM_INFO("qxl: ttm finalized\n");
454}
455
456
457#define QXL_DEBUGFS_MEM_TYPES 2
458
459#if defined(CONFIG_DEBUG_FS)
460static int qxl_mm_dump_table(struct seq_file *m, void *data)
461{
462 struct drm_info_node *node = (struct drm_info_node *)m->private;
463 struct drm_mm *mm = (struct drm_mm *)node->info_ent->data;
464 struct drm_device *dev = node->minor->dev;
465 struct qxl_device *rdev = dev->dev_private;
466 int ret;
467 struct ttm_bo_global *glob = rdev->mman.bdev.glob;
468
469 spin_lock(&glob->lru_lock);
470 ret = drm_mm_dump_table(m, mm);
471 spin_unlock(&glob->lru_lock);
472 return ret;
473}
474#endif
475
476static int qxl_ttm_debugfs_init(struct qxl_device *qdev)
477{
Dave Airliee1adc782013-04-18 11:47:40 +1000478#if defined(CONFIG_DEBUG_FS)
Dave Airlief64122c2013-02-25 14:47:55 +1000479 static struct drm_info_list qxl_mem_types_list[QXL_DEBUGFS_MEM_TYPES];
480 static char qxl_mem_types_names[QXL_DEBUGFS_MEM_TYPES][32];
481 unsigned i;
482
483 for (i = 0; i < QXL_DEBUGFS_MEM_TYPES; i++) {
484 if (i == 0)
485 sprintf(qxl_mem_types_names[i], "qxl_mem_mm");
486 else
487 sprintf(qxl_mem_types_names[i], "qxl_surf_mm");
488 qxl_mem_types_list[i].name = qxl_mem_types_names[i];
489 qxl_mem_types_list[i].show = &qxl_mm_dump_table;
490 qxl_mem_types_list[i].driver_features = 0;
491 if (i == 0)
492 qxl_mem_types_list[i].data = qdev->mman.bdev.man[TTM_PL_VRAM].priv;
493 else
Christian König283cde62016-09-12 13:34:37 +0200494 qxl_mem_types_list[i].data = qdev->mman.bdev.man[TTM_PL_PRIV].priv;
Dave Airlief64122c2013-02-25 14:47:55 +1000495
496 }
497 return qxl_debugfs_add_files(qdev, qxl_mem_types_list, i);
Dave Airliee1adc782013-04-18 11:47:40 +1000498#else
499 return 0;
500#endif
Dave Airlief64122c2013-02-25 14:47:55 +1000501}