Merge branch 'fix/kconfig' into for-linus
[pandora-kernel.git] / drivers / staging / gma500 / gem_glue.c
1 /**************************************************************************
2  * Copyright (c) 2011, Intel Corporation.
3  * All Rights Reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms and conditions of the GNU General Public License,
7  * version 2, as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
17  *
18  **************************************************************************/
19
20 #include <drm/drmP.h>
21 #include <drm/drm.h>
22
23 /**
24  * Initialize an already allocated GEM object of the specified size with
25  * no GEM provided backing store. Instead the caller is responsible for
26  * backing the object and handling it.
27  */
28 int drm_gem_private_object_init(struct drm_device *dev,
29                         struct drm_gem_object *obj, size_t size)
30 {
31         BUG_ON((size & (PAGE_SIZE - 1)) != 0);
32
33         obj->dev = dev;
34         obj->filp = NULL;
35
36         kref_init(&obj->refcount);
37         atomic_set(&obj->handle_count, 0);
38         obj->size = size;
39
40         return 0;
41 }
42
43 void drm_gem_object_release_wrap(struct drm_gem_object *obj)
44 {
45         /* Remove the list map if one is present */
46         if (obj->map_list.map) {
47                 struct drm_gem_mm *mm = obj->dev->mm_private;
48                 struct drm_map_list *list = &obj->map_list;
49                 drm_ht_remove_item(&mm->offset_hash, &list->hash);
50                 drm_mm_put_block(list->file_offset_node);
51                 kfree(list->map);
52                 list->map = NULL;
53         }
54         if (obj->filp)
55                 drm_gem_object_release(obj);
56 }
57
58 /**
59  *      gem_create_mmap_offset          -       invent an mmap offset
60  *      @obj: our object
61  *
62  *      Standard implementation of offset generation for mmap as is
63  *      duplicated in several drivers. This belongs in GEM.
64  */
65 int gem_create_mmap_offset(struct drm_gem_object *obj)
66 {
67         struct drm_device *dev = obj->dev;
68         struct drm_gem_mm *mm = dev->mm_private;
69         struct drm_map_list *list;
70         struct drm_local_map *map;
71         int ret;
72
73         list = &obj->map_list;
74         list->map = kzalloc(sizeof(struct drm_map_list), GFP_KERNEL);
75         if (list->map == NULL)
76                 return -ENOMEM;
77         map = list->map;
78         map->type = _DRM_GEM;
79         map->size = obj->size;
80         map->handle = obj;
81
82         list->file_offset_node = drm_mm_search_free(&mm->offset_manager,
83                                         obj->size / PAGE_SIZE, 0, 0);
84         if (!list->file_offset_node) {
85                 dev_err(dev->dev, "failed to allocate offset for bo %d\n",
86                                                                 obj->name);
87                 ret = -ENOSPC;
88                 goto free_it;
89         }
90         list->file_offset_node = drm_mm_get_block(list->file_offset_node,
91                                         obj->size / PAGE_SIZE, 0);
92         if (!list->file_offset_node) {
93                 ret = -ENOMEM;
94                 goto free_it;
95         }
96         list->hash.key = list->file_offset_node->start;
97         ret = drm_ht_insert_item(&mm->offset_hash, &list->hash);
98         if (ret) {
99                 dev_err(dev->dev, "failed to add to map hash\n");
100                 goto free_mm;
101         }
102         return 0;
103
104 free_mm:
105         drm_mm_put_block(list->file_offset_node);
106 free_it:
107         kfree(list->map);
108         list->map = NULL;
109         return ret;
110 }