Merge tag 'xtensa-20161005' of git://github.com/jcmvbkbc/linux-xtensa
[cascardo/linux.git] / drivers / gpu / drm / cirrus / cirrus_ttm.c
1 /*
2  * Copyright 2012 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
6  * "Software"), to deal in the Software without restriction, including
7  * without limitation the rights to use, copy, modify, merge, publish,
8  * distribute, sub license, and/or sell copies of the Software, and to
9  * permit persons to whom the Software is furnished to do so, subject to
10  * the following conditions:
11  *
12  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
15  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
16  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
17  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
18  * USE OR OTHER DEALINGS IN THE SOFTWARE.
19  *
20  * The above copyright notice and this permission notice (including the
21  * next paragraph) shall be included in all copies or substantial portions
22  * of the Software.
23  *
24  */
25 /*
26  * Authors: Dave Airlie <airlied@redhat.com>
27  */
28 #include <drm/drmP.h>
29 #include "cirrus_drv.h"
30 #include <ttm/ttm_page_alloc.h>
31
32 static inline struct cirrus_device *
33 cirrus_bdev(struct ttm_bo_device *bd)
34 {
35         return container_of(bd, struct cirrus_device, ttm.bdev);
36 }
37
38 static int
39 cirrus_ttm_mem_global_init(struct drm_global_reference *ref)
40 {
41         return ttm_mem_global_init(ref->object);
42 }
43
44 static void
45 cirrus_ttm_mem_global_release(struct drm_global_reference *ref)
46 {
47         ttm_mem_global_release(ref->object);
48 }
49
50 static int cirrus_ttm_global_init(struct cirrus_device *cirrus)
51 {
52         struct drm_global_reference *global_ref;
53         int r;
54
55         global_ref = &cirrus->ttm.mem_global_ref;
56         global_ref->global_type = DRM_GLOBAL_TTM_MEM;
57         global_ref->size = sizeof(struct ttm_mem_global);
58         global_ref->init = &cirrus_ttm_mem_global_init;
59         global_ref->release = &cirrus_ttm_mem_global_release;
60         r = drm_global_item_ref(global_ref);
61         if (r != 0) {
62                 DRM_ERROR("Failed setting up TTM memory accounting "
63                           "subsystem.\n");
64                 return r;
65         }
66
67         cirrus->ttm.bo_global_ref.mem_glob =
68                 cirrus->ttm.mem_global_ref.object;
69         global_ref = &cirrus->ttm.bo_global_ref.ref;
70         global_ref->global_type = DRM_GLOBAL_TTM_BO;
71         global_ref->size = sizeof(struct ttm_bo_global);
72         global_ref->init = &ttm_bo_global_init;
73         global_ref->release = &ttm_bo_global_release;
74         r = drm_global_item_ref(global_ref);
75         if (r != 0) {
76                 DRM_ERROR("Failed setting up TTM BO subsystem.\n");
77                 drm_global_item_unref(&cirrus->ttm.mem_global_ref);
78                 return r;
79         }
80         return 0;
81 }
82
83 static void
84 cirrus_ttm_global_release(struct cirrus_device *cirrus)
85 {
86         if (cirrus->ttm.mem_global_ref.release == NULL)
87                 return;
88
89         drm_global_item_unref(&cirrus->ttm.bo_global_ref.ref);
90         drm_global_item_unref(&cirrus->ttm.mem_global_ref);
91         cirrus->ttm.mem_global_ref.release = NULL;
92 }
93
94
95 static void cirrus_bo_ttm_destroy(struct ttm_buffer_object *tbo)
96 {
97         struct cirrus_bo *bo;
98
99         bo = container_of(tbo, struct cirrus_bo, bo);
100
101         drm_gem_object_release(&bo->gem);
102         kfree(bo);
103 }
104
105 static bool cirrus_ttm_bo_is_cirrus_bo(struct ttm_buffer_object *bo)
106 {
107         if (bo->destroy == &cirrus_bo_ttm_destroy)
108                 return true;
109         return false;
110 }
111
112 static int
113 cirrus_bo_init_mem_type(struct ttm_bo_device *bdev, uint32_t type,
114                      struct ttm_mem_type_manager *man)
115 {
116         switch (type) {
117         case TTM_PL_SYSTEM:
118                 man->flags = TTM_MEMTYPE_FLAG_MAPPABLE;
119                 man->available_caching = TTM_PL_MASK_CACHING;
120                 man->default_caching = TTM_PL_FLAG_CACHED;
121                 break;
122         case TTM_PL_VRAM:
123                 man->func = &ttm_bo_manager_func;
124                 man->flags = TTM_MEMTYPE_FLAG_FIXED |
125                         TTM_MEMTYPE_FLAG_MAPPABLE;
126                 man->available_caching = TTM_PL_FLAG_UNCACHED |
127                         TTM_PL_FLAG_WC;
128                 man->default_caching = TTM_PL_FLAG_WC;
129                 break;
130         default:
131                 DRM_ERROR("Unsupported memory type %u\n", (unsigned)type);
132                 return -EINVAL;
133         }
134         return 0;
135 }
136
137 static void
138 cirrus_bo_evict_flags(struct ttm_buffer_object *bo, struct ttm_placement *pl)
139 {
140         struct cirrus_bo *cirrusbo = cirrus_bo(bo);
141
142         if (!cirrus_ttm_bo_is_cirrus_bo(bo))
143                 return;
144
145         cirrus_ttm_placement(cirrusbo, TTM_PL_FLAG_SYSTEM);
146         *pl = cirrusbo->placement;
147 }
148
149 static int cirrus_bo_verify_access(struct ttm_buffer_object *bo, struct file *filp)
150 {
151         struct cirrus_bo *cirrusbo = cirrus_bo(bo);
152
153         return drm_vma_node_verify_access(&cirrusbo->gem.vma_node, filp);
154 }
155
156 static int cirrus_ttm_io_mem_reserve(struct ttm_bo_device *bdev,
157                                   struct ttm_mem_reg *mem)
158 {
159         struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
160         struct cirrus_device *cirrus = cirrus_bdev(bdev);
161
162         mem->bus.addr = NULL;
163         mem->bus.offset = 0;
164         mem->bus.size = mem->num_pages << PAGE_SHIFT;
165         mem->bus.base = 0;
166         mem->bus.is_iomem = false;
167         if (!(man->flags & TTM_MEMTYPE_FLAG_MAPPABLE))
168                 return -EINVAL;
169         switch (mem->mem_type) {
170         case TTM_PL_SYSTEM:
171                 /* system memory */
172                 return 0;
173         case TTM_PL_VRAM:
174                 mem->bus.offset = mem->start << PAGE_SHIFT;
175                 mem->bus.base = pci_resource_start(cirrus->dev->pdev, 0);
176                 mem->bus.is_iomem = true;
177                 break;
178         default:
179                 return -EINVAL;
180                 break;
181         }
182         return 0;
183 }
184
185 static void cirrus_ttm_io_mem_free(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
186 {
187 }
188
189 static void cirrus_ttm_backend_destroy(struct ttm_tt *tt)
190 {
191         ttm_tt_fini(tt);
192         kfree(tt);
193 }
194
195 static struct ttm_backend_func cirrus_tt_backend_func = {
196         .destroy = &cirrus_ttm_backend_destroy,
197 };
198
199
200 static struct ttm_tt *cirrus_ttm_tt_create(struct ttm_bo_device *bdev,
201                                  unsigned long size, uint32_t page_flags,
202                                  struct page *dummy_read_page)
203 {
204         struct ttm_tt *tt;
205
206         tt = kzalloc(sizeof(struct ttm_tt), GFP_KERNEL);
207         if (tt == NULL)
208                 return NULL;
209         tt->func = &cirrus_tt_backend_func;
210         if (ttm_tt_init(tt, bdev, size, page_flags, dummy_read_page)) {
211                 kfree(tt);
212                 return NULL;
213         }
214         return tt;
215 }
216
217 static int cirrus_ttm_tt_populate(struct ttm_tt *ttm)
218 {
219         return ttm_pool_populate(ttm);
220 }
221
222 static void cirrus_ttm_tt_unpopulate(struct ttm_tt *ttm)
223 {
224         ttm_pool_unpopulate(ttm);
225 }
226
227 struct ttm_bo_driver cirrus_bo_driver = {
228         .ttm_tt_create = cirrus_ttm_tt_create,
229         .ttm_tt_populate = cirrus_ttm_tt_populate,
230         .ttm_tt_unpopulate = cirrus_ttm_tt_unpopulate,
231         .init_mem_type = cirrus_bo_init_mem_type,
232         .evict_flags = cirrus_bo_evict_flags,
233         .move = NULL,
234         .verify_access = cirrus_bo_verify_access,
235         .io_mem_reserve = &cirrus_ttm_io_mem_reserve,
236         .io_mem_free = &cirrus_ttm_io_mem_free,
237         .lru_tail = &ttm_bo_default_lru_tail,
238         .swap_lru_tail = &ttm_bo_default_swap_lru_tail,
239 };
240
241 int cirrus_mm_init(struct cirrus_device *cirrus)
242 {
243         int ret;
244         struct drm_device *dev = cirrus->dev;
245         struct ttm_bo_device *bdev = &cirrus->ttm.bdev;
246
247         ret = cirrus_ttm_global_init(cirrus);
248         if (ret)
249                 return ret;
250
251         ret = ttm_bo_device_init(&cirrus->ttm.bdev,
252                                  cirrus->ttm.bo_global_ref.ref.object,
253                                  &cirrus_bo_driver,
254                                  dev->anon_inode->i_mapping,
255                                  DRM_FILE_PAGE_OFFSET,
256                                  true);
257         if (ret) {
258                 DRM_ERROR("Error initialising bo driver; %d\n", ret);
259                 return ret;
260         }
261
262         ret = ttm_bo_init_mm(bdev, TTM_PL_VRAM,
263                              cirrus->mc.vram_size >> PAGE_SHIFT);
264         if (ret) {
265                 DRM_ERROR("Failed ttm VRAM init: %d\n", ret);
266                 return ret;
267         }
268
269         cirrus->fb_mtrr = arch_phys_wc_add(pci_resource_start(dev->pdev, 0),
270                                            pci_resource_len(dev->pdev, 0));
271
272         cirrus->mm_inited = true;
273         return 0;
274 }
275
276 void cirrus_mm_fini(struct cirrus_device *cirrus)
277 {
278         if (!cirrus->mm_inited)
279                 return;
280
281         ttm_bo_device_release(&cirrus->ttm.bdev);
282
283         cirrus_ttm_global_release(cirrus);
284
285         arch_phys_wc_del(cirrus->fb_mtrr);
286         cirrus->fb_mtrr = 0;
287 }
288
289 void cirrus_ttm_placement(struct cirrus_bo *bo, int domain)
290 {
291         u32 c = 0;
292         unsigned i;
293         bo->placement.placement = bo->placements;
294         bo->placement.busy_placement = bo->placements;
295         if (domain & TTM_PL_FLAG_VRAM)
296                 bo->placements[c++].flags = TTM_PL_FLAG_WC | TTM_PL_FLAG_UNCACHED | TTM_PL_FLAG_VRAM;
297         if (domain & TTM_PL_FLAG_SYSTEM)
298                 bo->placements[c++].flags = TTM_PL_MASK_CACHING | TTM_PL_FLAG_SYSTEM;
299         if (!c)
300                 bo->placements[c++].flags = TTM_PL_MASK_CACHING | TTM_PL_FLAG_SYSTEM;
301         bo->placement.num_placement = c;
302         bo->placement.num_busy_placement = c;
303         for (i = 0; i < c; ++i) {
304                 bo->placements[i].fpfn = 0;
305                 bo->placements[i].lpfn = 0;
306         }
307 }
308
309 int cirrus_bo_create(struct drm_device *dev, int size, int align,
310                   uint32_t flags, struct cirrus_bo **pcirrusbo)
311 {
312         struct cirrus_device *cirrus = dev->dev_private;
313         struct cirrus_bo *cirrusbo;
314         size_t acc_size;
315         int ret;
316
317         cirrusbo = kzalloc(sizeof(struct cirrus_bo), GFP_KERNEL);
318         if (!cirrusbo)
319                 return -ENOMEM;
320
321         ret = drm_gem_object_init(dev, &cirrusbo->gem, size);
322         if (ret) {
323                 kfree(cirrusbo);
324                 return ret;
325         }
326
327         cirrusbo->bo.bdev = &cirrus->ttm.bdev;
328
329         cirrus_ttm_placement(cirrusbo, TTM_PL_FLAG_VRAM | TTM_PL_FLAG_SYSTEM);
330
331         acc_size = ttm_bo_dma_acc_size(&cirrus->ttm.bdev, size,
332                                        sizeof(struct cirrus_bo));
333
334         ret = ttm_bo_init(&cirrus->ttm.bdev, &cirrusbo->bo, size,
335                           ttm_bo_type_device, &cirrusbo->placement,
336                           align >> PAGE_SHIFT, false, NULL, acc_size,
337                           NULL, NULL, cirrus_bo_ttm_destroy);
338         if (ret)
339                 return ret;
340
341         *pcirrusbo = cirrusbo;
342         return 0;
343 }
344
345 static inline u64 cirrus_bo_gpu_offset(struct cirrus_bo *bo)
346 {
347         return bo->bo.offset;
348 }
349
350 int cirrus_bo_pin(struct cirrus_bo *bo, u32 pl_flag, u64 *gpu_addr)
351 {
352         int i, ret;
353
354         if (bo->pin_count) {
355                 bo->pin_count++;
356                 if (gpu_addr)
357                         *gpu_addr = cirrus_bo_gpu_offset(bo);
358         }
359
360         cirrus_ttm_placement(bo, pl_flag);
361         for (i = 0; i < bo->placement.num_placement; i++)
362                 bo->placements[i].flags |= TTM_PL_FLAG_NO_EVICT;
363         ret = ttm_bo_validate(&bo->bo, &bo->placement, false, false);
364         if (ret)
365                 return ret;
366
367         bo->pin_count = 1;
368         if (gpu_addr)
369                 *gpu_addr = cirrus_bo_gpu_offset(bo);
370         return 0;
371 }
372
373 int cirrus_bo_push_sysram(struct cirrus_bo *bo)
374 {
375         int i, ret;
376         if (!bo->pin_count) {
377                 DRM_ERROR("unpin bad %p\n", bo);
378                 return 0;
379         }
380         bo->pin_count--;
381         if (bo->pin_count)
382                 return 0;
383
384         if (bo->kmap.virtual)
385                 ttm_bo_kunmap(&bo->kmap);
386
387         cirrus_ttm_placement(bo, TTM_PL_FLAG_SYSTEM);
388         for (i = 0; i < bo->placement.num_placement ; i++)
389                 bo->placements[i].flags |= TTM_PL_FLAG_NO_EVICT;
390
391         ret = ttm_bo_validate(&bo->bo, &bo->placement, false, false);
392         if (ret) {
393                 DRM_ERROR("pushing to VRAM failed\n");
394                 return ret;
395         }
396         return 0;
397 }
398
399 int cirrus_mmap(struct file *filp, struct vm_area_struct *vma)
400 {
401         struct drm_file *file_priv;
402         struct cirrus_device *cirrus;
403
404         if (unlikely(vma->vm_pgoff < DRM_FILE_PAGE_OFFSET))
405                 return -EINVAL;
406
407         file_priv = filp->private_data;
408         cirrus = file_priv->minor->dev->dev_private;
409         return ttm_bo_mmap(filp, vma, &cirrus->ttm.bdev);
410 }