drm/ttm: merge offset and base in ttm_bus_placement
[linux-2.6-microblaze.git] / drivers / gpu / drm / qxl / qxl_ttm.c
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
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>
32 #include <drm/ttm/ttm_bo_api.h>
33 #include <drm/ttm/ttm_bo_driver.h>
34 #include <drm/ttm/ttm_module.h>
35 #include <drm/ttm/ttm_page_alloc.h>
36 #include <drm/ttm/ttm_placement.h>
37
38 #include "qxl_drv.h"
39 #include "qxl_object.h"
40
41 static struct qxl_device *qxl_get_qdev(struct ttm_bo_device *bdev)
42 {
43         struct qxl_mman *mman;
44         struct qxl_device *qdev;
45
46         mman = container_of(bdev, struct qxl_mman, bdev);
47         qdev = container_of(mman, struct qxl_device, mman);
48         return qdev;
49 }
50
51 static void qxl_evict_flags(struct ttm_buffer_object *bo,
52                                 struct ttm_placement *placement)
53 {
54         struct qxl_bo *qbo;
55         static const struct ttm_place placements = {
56                 .fpfn = 0,
57                 .lpfn = 0,
58                 .flags = TTM_PL_MASK_CACHING | TTM_PL_FLAG_SYSTEM
59         };
60
61         if (!qxl_ttm_bo_is_qxl_bo(bo)) {
62                 placement->placement = &placements;
63                 placement->busy_placement = &placements;
64                 placement->num_placement = 1;
65                 placement->num_busy_placement = 1;
66                 return;
67         }
68         qbo = to_qxl_bo(bo);
69         qxl_ttm_placement_from_domain(qbo, QXL_GEM_DOMAIN_CPU, false);
70         *placement = qbo->placement;
71 }
72
73 int qxl_ttm_io_mem_reserve(struct ttm_bo_device *bdev,
74                            struct ttm_resource *mem)
75 {
76         struct qxl_device *qdev = qxl_get_qdev(bdev);
77
78         switch (mem->mem_type) {
79         case TTM_PL_SYSTEM:
80                 /* system memory */
81                 return 0;
82         case TTM_PL_VRAM:
83                 mem->bus.is_iomem = true;
84                 mem->bus.offset = (mem->start << PAGE_SHIFT) + qdev->vram_base;
85                 break;
86         case TTM_PL_PRIV:
87                 mem->bus.is_iomem = true;
88                 mem->bus.offset = (mem->start << PAGE_SHIFT) +
89                         qdev->surfaceram_base;
90                 break;
91         default:
92                 return -EINVAL;
93         }
94         return 0;
95 }
96
97 /*
98  * TTM backend functions.
99  */
100 struct qxl_ttm_tt {
101         struct ttm_tt                   ttm;
102         struct qxl_device               *qdev;
103         u64                             offset;
104 };
105
106 static int qxl_ttm_backend_bind(struct ttm_bo_device *bdev,
107                                 struct ttm_tt *ttm,
108                                 struct ttm_resource *bo_mem)
109 {
110         struct qxl_ttm_tt *gtt = (void *)ttm;
111
112         gtt->offset = (unsigned long)(bo_mem->start << PAGE_SHIFT);
113         if (!ttm->num_pages) {
114                 WARN(1, "nothing to bind %lu pages for mreg %p back %p!\n",
115                      ttm->num_pages, bo_mem, ttm);
116         }
117         /* Not implemented */
118         return -1;
119 }
120
121 static void qxl_ttm_backend_unbind(struct ttm_bo_device *bdev,
122                                    struct ttm_tt *ttm)
123 {
124         /* Not implemented */
125 }
126
127 static void qxl_ttm_backend_destroy(struct ttm_bo_device *bdev,
128                                     struct ttm_tt *ttm)
129 {
130         struct qxl_ttm_tt *gtt = (void *)ttm;
131
132         ttm_tt_fini(&gtt->ttm);
133         kfree(gtt);
134 }
135
136 static struct ttm_backend_func qxl_backend_func = {
137         .bind = &qxl_ttm_backend_bind,
138         .unbind = &qxl_ttm_backend_unbind,
139         .destroy = &qxl_ttm_backend_destroy,
140 };
141
142 static struct ttm_tt *qxl_ttm_tt_create(struct ttm_buffer_object *bo,
143                                         uint32_t page_flags)
144 {
145         struct qxl_device *qdev;
146         struct qxl_ttm_tt *gtt;
147
148         qdev = qxl_get_qdev(bo->bdev);
149         gtt = kzalloc(sizeof(struct qxl_ttm_tt), GFP_KERNEL);
150         if (gtt == NULL)
151                 return NULL;
152         gtt->ttm.func = &qxl_backend_func;
153         gtt->qdev = qdev;
154         if (ttm_tt_init(&gtt->ttm, bo, page_flags)) {
155                 kfree(gtt);
156                 return NULL;
157         }
158         return &gtt->ttm;
159 }
160
161 static void qxl_move_null(struct ttm_buffer_object *bo,
162                              struct ttm_resource *new_mem)
163 {
164         struct ttm_resource *old_mem = &bo->mem;
165
166         BUG_ON(old_mem->mm_node != NULL);
167         *old_mem = *new_mem;
168         new_mem->mm_node = NULL;
169 }
170
171 static int qxl_bo_move(struct ttm_buffer_object *bo, bool evict,
172                        struct ttm_operation_ctx *ctx,
173                        struct ttm_resource *new_mem)
174 {
175         struct ttm_resource *old_mem = &bo->mem;
176         int ret;
177
178         ret = ttm_bo_wait(bo, ctx->interruptible, ctx->no_wait_gpu);
179         if (ret)
180                 return ret;
181
182         if (old_mem->mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
183                 qxl_move_null(bo, new_mem);
184                 return 0;
185         }
186         return ttm_bo_move_memcpy(bo, ctx, new_mem);
187 }
188
189 static void qxl_bo_move_notify(struct ttm_buffer_object *bo,
190                                bool evict,
191                                struct ttm_resource *new_mem)
192 {
193         struct qxl_bo *qbo;
194         struct qxl_device *qdev;
195
196         if (!qxl_ttm_bo_is_qxl_bo(bo))
197                 return;
198         qbo = to_qxl_bo(bo);
199         qdev = to_qxl(qbo->tbo.base.dev);
200
201         if (bo->mem.mem_type == TTM_PL_PRIV && qbo->surface_id)
202                 qxl_surface_evict(qdev, qbo, new_mem ? true : false);
203 }
204
205 static struct ttm_bo_driver qxl_bo_driver = {
206         .ttm_tt_create = &qxl_ttm_tt_create,
207         .eviction_valuable = ttm_bo_eviction_valuable,
208         .evict_flags = &qxl_evict_flags,
209         .move = &qxl_bo_move,
210         .io_mem_reserve = &qxl_ttm_io_mem_reserve,
211         .move_notify = &qxl_bo_move_notify,
212 };
213
214 static int qxl_ttm_init_mem_type(struct qxl_device *qdev,
215                                  unsigned int type,
216                                  uint64_t size)
217 {
218         return ttm_range_man_init(&qdev->mman.bdev, type, TTM_PL_MASK_CACHING,
219                                   TTM_PL_FLAG_CACHED, false, size);
220 }
221
222 int qxl_ttm_init(struct qxl_device *qdev)
223 {
224         int r;
225         int num_io_pages; /* != rom->num_io_pages, we include surface0 */
226
227         /* No others user of address space so set it to 0 */
228         r = ttm_bo_device_init(&qdev->mman.bdev,
229                                &qxl_bo_driver,
230                                qdev->ddev.anon_inode->i_mapping,
231                                qdev->ddev.vma_offset_manager,
232                                false);
233         if (r) {
234                 DRM_ERROR("failed initializing buffer object driver(%d).\n", r);
235                 return r;
236         }
237         /* NOTE: this includes the framebuffer (aka surface 0) */
238         num_io_pages = qdev->rom->ram_header_offset / PAGE_SIZE;
239         r = qxl_ttm_init_mem_type(qdev, TTM_PL_VRAM, num_io_pages);
240         if (r) {
241                 DRM_ERROR("Failed initializing VRAM heap.\n");
242                 return r;
243         }
244         r = qxl_ttm_init_mem_type(qdev, TTM_PL_PRIV,
245                                   qdev->surfaceram_size / PAGE_SIZE);
246         if (r) {
247                 DRM_ERROR("Failed initializing Surfaces heap.\n");
248                 return r;
249         }
250         DRM_INFO("qxl: %uM of VRAM memory size\n",
251                  (unsigned int)qdev->vram_size / (1024 * 1024));
252         DRM_INFO("qxl: %luM of IO pages memory ready (VRAM domain)\n",
253                  ((unsigned int)num_io_pages * PAGE_SIZE) / (1024 * 1024));
254         DRM_INFO("qxl: %uM of Surface memory size\n",
255                  (unsigned int)qdev->surfaceram_size / (1024 * 1024));
256         return 0;
257 }
258
259 void qxl_ttm_fini(struct qxl_device *qdev)
260 {
261         ttm_range_man_fini(&qdev->mman.bdev, TTM_PL_VRAM);
262         ttm_range_man_fini(&qdev->mman.bdev, TTM_PL_PRIV);
263         ttm_bo_device_release(&qdev->mman.bdev);
264         DRM_INFO("qxl: ttm finalized\n");
265 }
266
267 #define QXL_DEBUGFS_MEM_TYPES 2
268
269 #if defined(CONFIG_DEBUG_FS)
270 static int qxl_mm_dump_table(struct seq_file *m, void *data)
271 {
272         struct drm_info_node *node = (struct drm_info_node *)m->private;
273         struct ttm_resource_manager *man = (struct ttm_resource_manager *)node->info_ent->data;
274         struct drm_printer p = drm_seq_file_printer(m);
275
276         ttm_resource_manager_debug(man, &p);
277         return 0;
278 }
279 #endif
280
281 void qxl_ttm_debugfs_init(struct qxl_device *qdev)
282 {
283 #if defined(CONFIG_DEBUG_FS)
284         static struct drm_info_list qxl_mem_types_list[QXL_DEBUGFS_MEM_TYPES];
285         static char qxl_mem_types_names[QXL_DEBUGFS_MEM_TYPES][32];
286         unsigned int i;
287
288         for (i = 0; i < QXL_DEBUGFS_MEM_TYPES; i++) {
289                 if (i == 0)
290                         sprintf(qxl_mem_types_names[i], "qxl_mem_mm");
291                 else
292                         sprintf(qxl_mem_types_names[i], "qxl_surf_mm");
293                 qxl_mem_types_list[i].name = qxl_mem_types_names[i];
294                 qxl_mem_types_list[i].show = &qxl_mm_dump_table;
295                 qxl_mem_types_list[i].driver_features = 0;
296                 if (i == 0)
297                         qxl_mem_types_list[i].data = ttm_manager_type(&qdev->mman.bdev, TTM_PL_VRAM);
298                 else
299                         qxl_mem_types_list[i].data = ttm_manager_type(&qdev->mman.bdev, TTM_PL_PRIV);
300
301         }
302         qxl_debugfs_add_files(qdev, qxl_mem_types_list, i);
303 #endif
304 }