drm/qxl: fix usage of ttm_bo_init
[linux-2.6-microblaze.git] / drivers / gpu / drm / qxl / qxl_object.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 "qxl_drv.h"
27 #include "qxl_object.h"
28
29 #include <linux/io-mapping.h>
30 static void qxl_ttm_bo_destroy(struct ttm_buffer_object *tbo)
31 {
32         struct qxl_bo *bo;
33         struct qxl_device *qdev;
34
35         bo = to_qxl_bo(tbo);
36         qdev = to_qxl(bo->tbo.base.dev);
37
38         qxl_surface_evict(qdev, bo, false);
39         WARN_ON_ONCE(bo->map_count > 0);
40         mutex_lock(&qdev->gem.mutex);
41         list_del_init(&bo->list);
42         mutex_unlock(&qdev->gem.mutex);
43         drm_gem_object_release(&bo->tbo.base);
44         kfree(bo);
45 }
46
47 bool qxl_ttm_bo_is_qxl_bo(struct ttm_buffer_object *bo)
48 {
49         if (bo->destroy == &qxl_ttm_bo_destroy)
50                 return true;
51         return false;
52 }
53
54 void qxl_ttm_placement_from_domain(struct qxl_bo *qbo, u32 domain)
55 {
56         u32 c = 0;
57         u32 pflag = 0;
58         unsigned int i;
59
60         if (qbo->tbo.base.size <= PAGE_SIZE)
61                 pflag |= TTM_PL_FLAG_TOPDOWN;
62
63         qbo->placement.placement = qbo->placements;
64         qbo->placement.busy_placement = qbo->placements;
65         if (domain == QXL_GEM_DOMAIN_VRAM) {
66                 qbo->placements[c].mem_type = TTM_PL_VRAM;
67                 qbo->placements[c++].flags = TTM_PL_FLAG_CACHED | pflag;
68         }
69         if (domain == QXL_GEM_DOMAIN_SURFACE) {
70                 qbo->placements[c].mem_type = TTM_PL_PRIV;
71                 qbo->placements[c++].flags = TTM_PL_FLAG_CACHED | pflag;
72                 qbo->placements[c].mem_type = TTM_PL_VRAM;
73                 qbo->placements[c++].flags = TTM_PL_FLAG_CACHED | pflag;
74         }
75         if (domain == QXL_GEM_DOMAIN_CPU) {
76                 qbo->placements[c].mem_type = TTM_PL_SYSTEM;
77                 qbo->placements[c++].flags = TTM_PL_MASK_CACHING | pflag;
78         }
79         if (!c) {
80                 qbo->placements[c].mem_type = TTM_PL_SYSTEM;
81                 qbo->placements[c++].flags = TTM_PL_MASK_CACHING;
82         }
83         qbo->placement.num_placement = c;
84         qbo->placement.num_busy_placement = c;
85         for (i = 0; i < c; ++i) {
86                 qbo->placements[i].fpfn = 0;
87                 qbo->placements[i].lpfn = 0;
88         }
89 }
90
91 static const struct drm_gem_object_funcs qxl_object_funcs = {
92         .free = qxl_gem_object_free,
93         .open = qxl_gem_object_open,
94         .close = qxl_gem_object_close,
95         .pin = qxl_gem_prime_pin,
96         .unpin = qxl_gem_prime_unpin,
97         .get_sg_table = qxl_gem_prime_get_sg_table,
98         .vmap = qxl_gem_prime_vmap,
99         .vunmap = qxl_gem_prime_vunmap,
100         .mmap = drm_gem_ttm_mmap,
101         .print_info = drm_gem_ttm_print_info,
102 };
103
104 int qxl_bo_create(struct qxl_device *qdev,
105                   unsigned long size, bool kernel, bool pinned, u32 domain,
106                   struct qxl_surface *surf,
107                   struct qxl_bo **bo_ptr)
108 {
109         struct ttm_operation_ctx ctx = { !kernel, false };
110         struct qxl_bo *bo;
111         enum ttm_bo_type type;
112         int r;
113
114         if (kernel)
115                 type = ttm_bo_type_kernel;
116         else
117                 type = ttm_bo_type_device;
118         *bo_ptr = NULL;
119         bo = kzalloc(sizeof(struct qxl_bo), GFP_KERNEL);
120         if (bo == NULL)
121                 return -ENOMEM;
122         size = roundup(size, PAGE_SIZE);
123         r = drm_gem_object_init(&qdev->ddev, &bo->tbo.base, size);
124         if (unlikely(r)) {
125                 kfree(bo);
126                 return r;
127         }
128         bo->tbo.base.funcs = &qxl_object_funcs;
129         bo->type = domain;
130         bo->surface_id = 0;
131         INIT_LIST_HEAD(&bo->list);
132
133         if (surf)
134                 bo->surf = *surf;
135
136         qxl_ttm_placement_from_domain(bo, domain);
137
138         r = ttm_bo_init_reserved(&qdev->mman.bdev, &bo->tbo, size, type,
139                                  &bo->placement, 0, &ctx, size,
140                                  NULL, NULL, &qxl_ttm_bo_destroy);
141         if (unlikely(r != 0)) {
142                 if (r != -ERESTARTSYS)
143                         dev_err(qdev->ddev.dev,
144                                 "object_init failed for (%lu, 0x%08X)\n",
145                                 size, domain);
146                 return r;
147         }
148         if (pinned)
149                 ttm_bo_pin(&bo->tbo);
150         ttm_bo_unreserve(&bo->tbo);
151         *bo_ptr = bo;
152         return 0;
153 }
154
155 int qxl_bo_kmap(struct qxl_bo *bo, void **ptr)
156 {
157         bool is_iomem;
158         int r;
159
160         if (bo->kptr) {
161                 if (ptr)
162                         *ptr = bo->kptr;
163                 bo->map_count++;
164                 return 0;
165         }
166         r = ttm_bo_kmap(&bo->tbo, 0, bo->tbo.num_pages, &bo->kmap);
167         if (r)
168                 return r;
169         bo->kptr = ttm_kmap_obj_virtual(&bo->kmap, &is_iomem);
170         if (ptr)
171                 *ptr = bo->kptr;
172         bo->map_count = 1;
173         return 0;
174 }
175
176 void *qxl_bo_kmap_atomic_page(struct qxl_device *qdev,
177                               struct qxl_bo *bo, int page_offset)
178 {
179         unsigned long offset;
180         void *rptr;
181         int ret;
182         struct io_mapping *map;
183
184         if (bo->tbo.mem.mem_type == TTM_PL_VRAM)
185                 map = qdev->vram_mapping;
186         else if (bo->tbo.mem.mem_type == TTM_PL_PRIV)
187                 map = qdev->surface_mapping;
188         else
189                 goto fallback;
190
191         offset = bo->tbo.mem.start << PAGE_SHIFT;
192         return io_mapping_map_atomic_wc(map, offset + page_offset);
193 fallback:
194         if (bo->kptr) {
195                 rptr = bo->kptr + (page_offset * PAGE_SIZE);
196                 return rptr;
197         }
198
199         ret = qxl_bo_kmap(bo, &rptr);
200         if (ret)
201                 return NULL;
202
203         rptr += page_offset * PAGE_SIZE;
204         return rptr;
205 }
206
207 void qxl_bo_kunmap(struct qxl_bo *bo)
208 {
209         if (bo->kptr == NULL)
210                 return;
211         bo->map_count--;
212         if (bo->map_count > 0)
213                 return;
214         bo->kptr = NULL;
215         ttm_bo_kunmap(&bo->kmap);
216 }
217
218 void qxl_bo_kunmap_atomic_page(struct qxl_device *qdev,
219                                struct qxl_bo *bo, void *pmap)
220 {
221         if ((bo->tbo.mem.mem_type != TTM_PL_VRAM) &&
222             (bo->tbo.mem.mem_type != TTM_PL_PRIV))
223                 goto fallback;
224
225         io_mapping_unmap_atomic(pmap);
226         return;
227  fallback:
228         qxl_bo_kunmap(bo);
229 }
230
231 void qxl_bo_unref(struct qxl_bo **bo)
232 {
233         if ((*bo) == NULL)
234                 return;
235
236         drm_gem_object_put(&(*bo)->tbo.base);
237         *bo = NULL;
238 }
239
240 struct qxl_bo *qxl_bo_ref(struct qxl_bo *bo)
241 {
242         drm_gem_object_get(&bo->tbo.base);
243         return bo;
244 }
245
246 static int __qxl_bo_pin(struct qxl_bo *bo)
247 {
248         struct ttm_operation_ctx ctx = { false, false };
249         struct drm_device *ddev = bo->tbo.base.dev;
250         int r;
251
252         if (bo->tbo.pin_count) {
253                 ttm_bo_pin(&bo->tbo);
254                 return 0;
255         }
256         qxl_ttm_placement_from_domain(bo, bo->type);
257         r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
258         if (likely(r == 0))
259                 ttm_bo_pin(&bo->tbo);
260         if (unlikely(r != 0))
261                 dev_err(ddev->dev, "%p pin failed\n", bo);
262         return r;
263 }
264
265 static void __qxl_bo_unpin(struct qxl_bo *bo)
266 {
267         ttm_bo_unpin(&bo->tbo);
268 }
269
270 /*
271  * Reserve the BO before pinning the object.  If the BO was reserved
272  * beforehand, use the internal version directly __qxl_bo_pin.
273  *
274  */
275 int qxl_bo_pin(struct qxl_bo *bo)
276 {
277         int r;
278
279         r = qxl_bo_reserve(bo);
280         if (r)
281                 return r;
282
283         r = __qxl_bo_pin(bo);
284         qxl_bo_unreserve(bo);
285         return r;
286 }
287
288 /*
289  * Reserve the BO before pinning the object.  If the BO was reserved
290  * beforehand, use the internal version directly __qxl_bo_unpin.
291  *
292  */
293 int qxl_bo_unpin(struct qxl_bo *bo)
294 {
295         int r;
296
297         r = qxl_bo_reserve(bo);
298         if (r)
299                 return r;
300
301         __qxl_bo_unpin(bo);
302         qxl_bo_unreserve(bo);
303         return 0;
304 }
305
306 void qxl_bo_force_delete(struct qxl_device *qdev)
307 {
308         struct qxl_bo *bo, *n;
309
310         if (list_empty(&qdev->gem.objects))
311                 return;
312         dev_err(qdev->ddev.dev, "Userspace still has active objects !\n");
313         list_for_each_entry_safe(bo, n, &qdev->gem.objects, list) {
314                 dev_err(qdev->ddev.dev, "%p %p %lu %lu force free\n",
315                         &bo->tbo.base, bo, (unsigned long)bo->tbo.base.size,
316                         *((unsigned long *)&bo->tbo.base.refcount));
317                 mutex_lock(&qdev->gem.mutex);
318                 list_del_init(&bo->list);
319                 mutex_unlock(&qdev->gem.mutex);
320                 /* this should unref the ttm bo */
321                 drm_gem_object_put(&bo->tbo.base);
322         }
323 }
324
325 int qxl_bo_init(struct qxl_device *qdev)
326 {
327         return qxl_ttm_init(qdev);
328 }
329
330 void qxl_bo_fini(struct qxl_device *qdev)
331 {
332         qxl_ttm_fini(qdev);
333 }
334
335 int qxl_bo_check_id(struct qxl_device *qdev, struct qxl_bo *bo)
336 {
337         int ret;
338
339         if (bo->type == QXL_GEM_DOMAIN_SURFACE && bo->surface_id == 0) {
340                 /* allocate a surface id for this surface now */
341                 ret = qxl_surface_id_alloc(qdev, bo);
342                 if (ret)
343                         return ret;
344
345                 ret = qxl_hw_surface_alloc(qdev, bo);
346                 if (ret)
347                         return ret;
348         }
349         return 0;
350 }
351
352 int qxl_surf_evict(struct qxl_device *qdev)
353 {
354         return ttm_bo_evict_mm(&qdev->mman.bdev, TTM_PL_PRIV);
355 }
356
357 int qxl_vram_evict(struct qxl_device *qdev)
358 {
359         return ttm_bo_evict_mm(&qdev->mman.bdev, TTM_PL_VRAM);
360 }