Linux 6.12-rc6
[linux-2.6-block.git] / drivers / gpu / drm / qxl / qxl_ttm.c
CommitLineData
f64122c1
DA
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
c0f4b75c
SR
26#include <linux/delay.h>
27
28#include <drm/drm.h>
29#include <drm/drm_file.h>
30#include <drm/drm_debugfs.h>
31#include <drm/qxl_drm.h>
a3185f91 32#include <drm/ttm/ttm_bo.h>
c0f4b75c 33#include <drm/ttm/ttm_placement.h>
3eb7d96e 34#include <drm/ttm/ttm_range_manager.h>
a3185f91 35#include <drm/ttm/ttm_tt.h>
c0f4b75c 36
f64122c1
DA
37#include "qxl_drv.h"
38#include "qxl_object.h"
39
8af8a109 40static struct qxl_device *qxl_get_qdev(struct ttm_device *bdev)
f64122c1
DA
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
f64122c1
DA
50static void qxl_evict_flags(struct ttm_buffer_object *bo,
51 struct ttm_placement *placement)
52{
53 struct qxl_bo *qbo;
9e2033a6 54 static const struct ttm_place placements = {
f1217ed0
CK
55 .fpfn = 0,
56 .lpfn = 0,
48e07c23 57 .mem_type = TTM_PL_SYSTEM,
ce65b874 58 .flags = 0
f1217ed0 59 };
f64122c1
DA
60
61 if (!qxl_ttm_bo_is_qxl_bo(bo)) {
f64122c1 62 placement->placement = &placements;
f64122c1 63 placement->num_placement = 1;
f64122c1
DA
64 return;
65 }
3ebf1c6d 66 qbo = to_qxl_bo(bo);
9d36d432 67 qxl_ttm_placement_from_domain(qbo, QXL_GEM_DOMAIN_CPU);
f64122c1
DA
68 *placement = qbo->placement;
69}
70
8af8a109 71int qxl_ttm_io_mem_reserve(struct ttm_device *bdev,
2966141a 72 struct ttm_resource *mem)
f64122c1 73{
f64122c1
DA
74 struct qxl_device *qdev = qxl_get_qdev(bdev);
75
f64122c1
DA
76 switch (mem->mem_type) {
77 case TTM_PL_SYSTEM:
78 /* system memory */
79 return 0;
80 case TTM_PL_VRAM:
81 mem->bus.is_iomem = true;
54d04ea8 82 mem->bus.offset = (mem->start << PAGE_SHIFT) + qdev->vram_base;
59ab4ee0 83 mem->bus.caching = ttm_write_combined;
f64122c1 84 break;
283cde69 85 case TTM_PL_PRIV:
f64122c1 86 mem->bus.is_iomem = true;
54d04ea8
CK
87 mem->bus.offset = (mem->start << PAGE_SHIFT) +
88 qdev->surfaceram_base;
59ab4ee0 89 mem->bus.caching = ttm_write_combined;
f64122c1
DA
90 break;
91 default:
92 return -EINVAL;
93 }
94 return 0;
95}
96
f64122c1
DA
97/*
98 * TTM backend functions.
99 */
8af8a109 100static void qxl_ttm_backend_destroy(struct ttm_device *bdev, struct ttm_tt *ttm)
f64122c1 101{
881c4117
DA
102 ttm_tt_fini(ttm);
103 kfree(ttm);
f64122c1
DA
104}
105
dde5da23
CK
106static struct ttm_tt *qxl_ttm_tt_create(struct ttm_buffer_object *bo,
107 uint32_t page_flags)
f64122c1 108{
881c4117 109 struct ttm_tt *ttm;
f64122c1 110
881c4117
DA
111 ttm = kzalloc(sizeof(struct ttm_tt), GFP_KERNEL);
112 if (ttm == NULL)
f64122c1 113 return NULL;
e36764ec 114 if (ttm_tt_init(ttm, bo, page_flags, ttm_cached, 0)) {
881c4117 115 kfree(ttm);
f64122c1
DA
116 return NULL;
117 }
881c4117 118 return ttm;
f64122c1
DA
119}
120
f64122c1 121static void qxl_bo_move_notify(struct ttm_buffer_object *bo,
2966141a 122 struct ttm_resource *new_mem)
f64122c1
DA
123{
124 struct qxl_bo *qbo;
125 struct qxl_device *qdev;
126
3efe180d 127 if (!qxl_ttm_bo_is_qxl_bo(bo) || !bo->resource)
f64122c1 128 return;
3ebf1c6d 129 qbo = to_qxl_bo(bo);
e304f8a0 130 qdev = to_qxl(qbo->tbo.base.dev);
f64122c1 131
d3116756 132 if (bo->resource->mem_type == TTM_PL_PRIV && qbo->surface_id)
f64122c1
DA
133 qxl_surface_evict(qdev, qbo, new_mem ? true : false);
134}
135
6d820003
DA
136static int qxl_bo_move(struct ttm_buffer_object *bo, bool evict,
137 struct ttm_operation_ctx *ctx,
ebdf5651
DA
138 struct ttm_resource *new_mem,
139 struct ttm_place *hop)
6d820003 140{
d3116756 141 struct ttm_resource *old_mem = bo->resource;
6d820003
DA
142 int ret;
143
b129ff30
MA
144 if (!old_mem) {
145 if (new_mem->mem_type != TTM_PL_SYSTEM) {
146 hop->mem_type = TTM_PL_SYSTEM;
147 hop->flags = TTM_PL_FLAG_TEMPORARY;
148 return -EMULTIHOP;
149 }
150
151 ttm_bo_move_null(bo, new_mem);
152 return 0;
153 }
154
f1c68b6a 155 qxl_bo_move_notify(bo, new_mem);
6d820003
DA
156
157 ret = ttm_bo_wait_ctx(bo, ctx);
158 if (ret)
f1c68b6a 159 return ret;
6d820003
DA
160
161 if (old_mem->mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
162 ttm_bo_move_null(bo, new_mem);
163 return 0;
164 }
f1c68b6a 165 return ttm_bo_move_memcpy(bo, ctx, new_mem);
6d820003
DA
166}
167
6a6e5988
DA
168static void qxl_bo_delete_mem_notify(struct ttm_buffer_object *bo)
169{
f1c68b6a 170 qxl_bo_move_notify(bo, NULL);
6a6e5988
DA
171}
172
8af8a109 173static struct ttm_device_funcs qxl_bo_driver = {
f64122c1 174 .ttm_tt_create = &qxl_ttm_tt_create,
dc2caa30 175 .ttm_tt_destroy = &qxl_ttm_backend_destroy,
a2ab19fe 176 .eviction_valuable = ttm_bo_eviction_valuable,
f64122c1
DA
177 .evict_flags = &qxl_evict_flags,
178 .move = &qxl_bo_move,
f64122c1 179 .io_mem_reserve = &qxl_ttm_io_mem_reserve,
6a6e5988 180 .delete_mem_notify = &qxl_bo_delete_mem_notify,
f64122c1
DA
181};
182
ccd0dc43
CK
183static int qxl_ttm_init_mem_type(struct qxl_device *qdev,
184 unsigned int type,
185 uint64_t size)
186{
9c3006a4 187 return ttm_range_man_init(&qdev->mman.bdev, type, false, size);
ccd0dc43
CK
188}
189
f64122c1
DA
190int qxl_ttm_init(struct qxl_device *qdev)
191{
192 int r;
193 int num_io_pages; /* != rom->num_io_pages, we include surface0 */
194
f64122c1 195 /* No others user of address space so set it to 0 */
8af8a109
CK
196 r = ttm_device_init(&qdev->mman.bdev, &qxl_bo_driver, NULL,
197 qdev->ddev.anon_inode->i_mapping,
198 qdev->ddev.vma_offset_manager,
199 false, false);
f64122c1
DA
200 if (r) {
201 DRM_ERROR("failed initializing buffer object driver(%d).\n", r);
202 return r;
203 }
204 /* NOTE: this includes the framebuffer (aka surface 0) */
205 num_io_pages = qdev->rom->ram_header_offset / PAGE_SIZE;
ccd0dc43 206 r = qxl_ttm_init_mem_type(qdev, TTM_PL_VRAM, num_io_pages);
f64122c1
DA
207 if (r) {
208 DRM_ERROR("Failed initializing VRAM heap.\n");
209 return r;
210 }
ccd0dc43
CK
211 r = qxl_ttm_init_mem_type(qdev, TTM_PL_PRIV,
212 qdev->surfaceram_size / PAGE_SIZE);
f64122c1
DA
213 if (r) {
214 DRM_ERROR("Failed initializing Surfaces heap.\n");
215 return r;
216 }
217 DRM_INFO("qxl: %uM of VRAM memory size\n",
1b000494 218 (unsigned int)qdev->vram_size / (1024 * 1024));
f64122c1 219 DRM_INFO("qxl: %luM of IO pages memory ready (VRAM domain)\n",
1b000494 220 ((unsigned int)num_io_pages * PAGE_SIZE) / (1024 * 1024));
d9bbf189 221 DRM_INFO("qxl: %uM of Surface memory size\n",
1b000494 222 (unsigned int)qdev->surfaceram_size / (1024 * 1024));
f64122c1
DA
223 return 0;
224}
225
226void qxl_ttm_fini(struct qxl_device *qdev)
227{
37205891
DA
228 ttm_range_man_fini(&qdev->mman.bdev, TTM_PL_VRAM);
229 ttm_range_man_fini(&qdev->mman.bdev, TTM_PL_PRIV);
8af8a109 230 ttm_device_fini(&qdev->mman.bdev);
f64122c1
DA
231 DRM_INFO("qxl: ttm finalized\n");
232}
233
7ce84471 234void qxl_ttm_debugfs_init(struct qxl_device *qdev)
f64122c1 235{
e1adc78c 236#if defined(CONFIG_DEBUG_FS)
d0719e09
ZR
237 ttm_resource_manager_create_debugfs(ttm_manager_type(&qdev->mman.bdev,
238 TTM_PL_VRAM),
239 qdev->ddev.primary->debugfs_root, "qxl_mem_mm");
240 ttm_resource_manager_create_debugfs(ttm_manager_type(&qdev->mman.bdev,
241 TTM_PL_PRIV),
242 qdev->ddev.primary->debugfs_root, "qxl_surf_mm");
e1adc78c 243#endif
f64122c1 244}