Merge tag 'mailbox-v5.8' of git://git.linaro.org/landing-teams/working/fujitsu/integr...
[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 int qxl_init_mem_type(struct ttm_bo_device *bdev, uint32_t type,
52                              struct ttm_mem_type_manager *man)
53 {
54         struct qxl_device *qdev = qxl_get_qdev(bdev);
55         unsigned int gpu_offset_shift =
56                 64 - (qdev->rom->slot_gen_bits + qdev->rom->slot_id_bits + 8);
57         struct qxl_memslot *slot;
58
59         switch (type) {
60         case TTM_PL_SYSTEM:
61                 /* System memory */
62                 man->flags = TTM_MEMTYPE_FLAG_MAPPABLE;
63                 man->available_caching = TTM_PL_MASK_CACHING;
64                 man->default_caching = TTM_PL_FLAG_CACHED;
65                 break;
66         case TTM_PL_VRAM:
67         case TTM_PL_PRIV:
68                 /* "On-card" video ram */
69                 slot = (type == TTM_PL_VRAM) ?
70                         &qdev->main_slot : &qdev->surfaces_slot;
71                 slot->gpu_offset = (uint64_t)type << gpu_offset_shift;
72                 man->func = &ttm_bo_manager_func;
73                 man->gpu_offset = slot->gpu_offset;
74                 man->flags = TTM_MEMTYPE_FLAG_FIXED |
75                              TTM_MEMTYPE_FLAG_MAPPABLE;
76                 man->available_caching = TTM_PL_MASK_CACHING;
77                 man->default_caching = TTM_PL_FLAG_CACHED;
78                 break;
79         default:
80                 DRM_ERROR("Unsupported memory type %u\n", (unsigned int)type);
81                 return -EINVAL;
82         }
83         return 0;
84 }
85
86 static void qxl_evict_flags(struct ttm_buffer_object *bo,
87                                 struct ttm_placement *placement)
88 {
89         struct qxl_bo *qbo;
90         static const struct ttm_place placements = {
91                 .fpfn = 0,
92                 .lpfn = 0,
93                 .flags = TTM_PL_MASK_CACHING | TTM_PL_FLAG_SYSTEM
94         };
95
96         if (!qxl_ttm_bo_is_qxl_bo(bo)) {
97                 placement->placement = &placements;
98                 placement->busy_placement = &placements;
99                 placement->num_placement = 1;
100                 placement->num_busy_placement = 1;
101                 return;
102         }
103         qbo = to_qxl_bo(bo);
104         qxl_ttm_placement_from_domain(qbo, QXL_GEM_DOMAIN_CPU, false);
105         *placement = qbo->placement;
106 }
107
108 int qxl_ttm_io_mem_reserve(struct ttm_bo_device *bdev,
109                            struct ttm_mem_reg *mem)
110 {
111         struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
112         struct qxl_device *qdev = qxl_get_qdev(bdev);
113
114         mem->bus.addr = NULL;
115         mem->bus.offset = 0;
116         mem->bus.size = mem->num_pages << PAGE_SHIFT;
117         mem->bus.base = 0;
118         mem->bus.is_iomem = false;
119         if (!(man->flags & TTM_MEMTYPE_FLAG_MAPPABLE))
120                 return -EINVAL;
121         switch (mem->mem_type) {
122         case TTM_PL_SYSTEM:
123                 /* system memory */
124                 return 0;
125         case TTM_PL_VRAM:
126                 mem->bus.is_iomem = true;
127                 mem->bus.base = qdev->vram_base;
128                 mem->bus.offset = mem->start << PAGE_SHIFT;
129                 break;
130         case TTM_PL_PRIV:
131                 mem->bus.is_iomem = true;
132                 mem->bus.base = qdev->surfaceram_base;
133                 mem->bus.offset = mem->start << PAGE_SHIFT;
134                 break;
135         default:
136                 return -EINVAL;
137         }
138         return 0;
139 }
140
141 static void qxl_ttm_io_mem_free(struct ttm_bo_device *bdev,
142                                 struct ttm_mem_reg *mem)
143 {
144 }
145
146 /*
147  * TTM backend functions.
148  */
149 struct qxl_ttm_tt {
150         struct ttm_tt                   ttm;
151         struct qxl_device               *qdev;
152         u64                             offset;
153 };
154
155 static int qxl_ttm_backend_bind(struct ttm_tt *ttm,
156                                 struct ttm_mem_reg *bo_mem)
157 {
158         struct qxl_ttm_tt *gtt = (void *)ttm;
159
160         gtt->offset = (unsigned long)(bo_mem->start << PAGE_SHIFT);
161         if (!ttm->num_pages) {
162                 WARN(1, "nothing to bind %lu pages for mreg %p back %p!\n",
163                      ttm->num_pages, bo_mem, ttm);
164         }
165         /* Not implemented */
166         return -1;
167 }
168
169 static int qxl_ttm_backend_unbind(struct ttm_tt *ttm)
170 {
171         /* Not implemented */
172         return -1;
173 }
174
175 static void qxl_ttm_backend_destroy(struct ttm_tt *ttm)
176 {
177         struct qxl_ttm_tt *gtt = (void *)ttm;
178
179         ttm_tt_fini(&gtt->ttm);
180         kfree(gtt);
181 }
182
183 static struct ttm_backend_func qxl_backend_func = {
184         .bind = &qxl_ttm_backend_bind,
185         .unbind = &qxl_ttm_backend_unbind,
186         .destroy = &qxl_ttm_backend_destroy,
187 };
188
189 static struct ttm_tt *qxl_ttm_tt_create(struct ttm_buffer_object *bo,
190                                         uint32_t page_flags)
191 {
192         struct qxl_device *qdev;
193         struct qxl_ttm_tt *gtt;
194
195         qdev = qxl_get_qdev(bo->bdev);
196         gtt = kzalloc(sizeof(struct qxl_ttm_tt), GFP_KERNEL);
197         if (gtt == NULL)
198                 return NULL;
199         gtt->ttm.func = &qxl_backend_func;
200         gtt->qdev = qdev;
201         if (ttm_tt_init(&gtt->ttm, bo, page_flags)) {
202                 kfree(gtt);
203                 return NULL;
204         }
205         return &gtt->ttm;
206 }
207
208 static void qxl_move_null(struct ttm_buffer_object *bo,
209                              struct ttm_mem_reg *new_mem)
210 {
211         struct ttm_mem_reg *old_mem = &bo->mem;
212
213         BUG_ON(old_mem->mm_node != NULL);
214         *old_mem = *new_mem;
215         new_mem->mm_node = NULL;
216 }
217
218 static int qxl_bo_move(struct ttm_buffer_object *bo, bool evict,
219                        struct ttm_operation_ctx *ctx,
220                        struct ttm_mem_reg *new_mem)
221 {
222         struct ttm_mem_reg *old_mem = &bo->mem;
223         int ret;
224
225         ret = ttm_bo_wait(bo, ctx->interruptible, ctx->no_wait_gpu);
226         if (ret)
227                 return ret;
228
229         if (old_mem->mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
230                 qxl_move_null(bo, new_mem);
231                 return 0;
232         }
233         return ttm_bo_move_memcpy(bo, ctx, new_mem);
234 }
235
236 static void qxl_bo_move_notify(struct ttm_buffer_object *bo,
237                                bool evict,
238                                struct ttm_mem_reg *new_mem)
239 {
240         struct qxl_bo *qbo;
241         struct qxl_device *qdev;
242
243         if (!qxl_ttm_bo_is_qxl_bo(bo))
244                 return;
245         qbo = to_qxl_bo(bo);
246         qdev = to_qxl(qbo->tbo.base.dev);
247
248         if (bo->mem.mem_type == TTM_PL_PRIV && qbo->surface_id)
249                 qxl_surface_evict(qdev, qbo, new_mem ? true : false);
250 }
251
252 static struct ttm_bo_driver qxl_bo_driver = {
253         .ttm_tt_create = &qxl_ttm_tt_create,
254         .init_mem_type = &qxl_init_mem_type,
255         .eviction_valuable = ttm_bo_eviction_valuable,
256         .evict_flags = &qxl_evict_flags,
257         .move = &qxl_bo_move,
258         .io_mem_reserve = &qxl_ttm_io_mem_reserve,
259         .io_mem_free = &qxl_ttm_io_mem_free,
260         .move_notify = &qxl_bo_move_notify,
261 };
262
263 int qxl_ttm_init(struct qxl_device *qdev)
264 {
265         int r;
266         int num_io_pages; /* != rom->num_io_pages, we include surface0 */
267
268         /* No others user of address space so set it to 0 */
269         r = ttm_bo_device_init(&qdev->mman.bdev,
270                                &qxl_bo_driver,
271                                qdev->ddev.anon_inode->i_mapping,
272                                qdev->ddev.vma_offset_manager,
273                                false);
274         if (r) {
275                 DRM_ERROR("failed initializing buffer object driver(%d).\n", r);
276                 return r;
277         }
278         /* NOTE: this includes the framebuffer (aka surface 0) */
279         num_io_pages = qdev->rom->ram_header_offset / PAGE_SIZE;
280         r = ttm_bo_init_mm(&qdev->mman.bdev, TTM_PL_VRAM,
281                            num_io_pages);
282         if (r) {
283                 DRM_ERROR("Failed initializing VRAM heap.\n");
284                 return r;
285         }
286         r = ttm_bo_init_mm(&qdev->mman.bdev, TTM_PL_PRIV,
287                            qdev->surfaceram_size / PAGE_SIZE);
288         if (r) {
289                 DRM_ERROR("Failed initializing Surfaces heap.\n");
290                 return r;
291         }
292         DRM_INFO("qxl: %uM of VRAM memory size\n",
293                  (unsigned int)qdev->vram_size / (1024 * 1024));
294         DRM_INFO("qxl: %luM of IO pages memory ready (VRAM domain)\n",
295                  ((unsigned int)num_io_pages * PAGE_SIZE) / (1024 * 1024));
296         DRM_INFO("qxl: %uM of Surface memory size\n",
297                  (unsigned int)qdev->surfaceram_size / (1024 * 1024));
298         return 0;
299 }
300
301 void qxl_ttm_fini(struct qxl_device *qdev)
302 {
303         ttm_bo_clean_mm(&qdev->mman.bdev, TTM_PL_VRAM);
304         ttm_bo_clean_mm(&qdev->mman.bdev, TTM_PL_PRIV);
305         ttm_bo_device_release(&qdev->mman.bdev);
306         DRM_INFO("qxl: ttm finalized\n");
307 }
308
309 #define QXL_DEBUGFS_MEM_TYPES 2
310
311 #if defined(CONFIG_DEBUG_FS)
312 static int qxl_mm_dump_table(struct seq_file *m, void *data)
313 {
314         struct drm_info_node *node = (struct drm_info_node *)m->private;
315         struct drm_mm *mm = (struct drm_mm *)node->info_ent->data;
316         struct drm_printer p = drm_seq_file_printer(m);
317
318         spin_lock(&ttm_bo_glob.lru_lock);
319         drm_mm_print(mm, &p);
320         spin_unlock(&ttm_bo_glob.lru_lock);
321         return 0;
322 }
323 #endif
324
325 void qxl_ttm_debugfs_init(struct qxl_device *qdev)
326 {
327 #if defined(CONFIG_DEBUG_FS)
328         static struct drm_info_list qxl_mem_types_list[QXL_DEBUGFS_MEM_TYPES];
329         static char qxl_mem_types_names[QXL_DEBUGFS_MEM_TYPES][32];
330         unsigned int i;
331
332         for (i = 0; i < QXL_DEBUGFS_MEM_TYPES; i++) {
333                 if (i == 0)
334                         sprintf(qxl_mem_types_names[i], "qxl_mem_mm");
335                 else
336                         sprintf(qxl_mem_types_names[i], "qxl_surf_mm");
337                 qxl_mem_types_list[i].name = qxl_mem_types_names[i];
338                 qxl_mem_types_list[i].show = &qxl_mm_dump_table;
339                 qxl_mem_types_list[i].driver_features = 0;
340                 if (i == 0)
341                         qxl_mem_types_list[i].data = qdev->mman.bdev.man[TTM_PL_VRAM].priv;
342                 else
343                         qxl_mem_types_list[i].data = qdev->mman.bdev.man[TTM_PL_PRIV].priv;
344
345         }
346         qxl_debugfs_add_files(qdev, qxl_mem_types_list, i);
347 #endif
348 }