]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/gpu/drm/gma500/gem_glue.c
Merge tag 'for-linus-v3.7-rc1' of git://oss.sgi.com/xfs/xfs
[karo-tx-linux.git] / drivers / gpu / drm / 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 #include "gem_glue.h"
23
24 void drm_gem_object_release_wrap(struct drm_gem_object *obj)
25 {
26         /* Remove the list map if one is present */
27         if (obj->map_list.map) {
28                 struct drm_gem_mm *mm = obj->dev->mm_private;
29                 struct drm_map_list *list = &obj->map_list;
30                 drm_ht_remove_item(&mm->offset_hash, &list->hash);
31                 drm_mm_put_block(list->file_offset_node);
32                 kfree(list->map);
33                 list->map = NULL;
34         }
35         drm_gem_object_release(obj);
36 }
37
38 /**
39  *      gem_create_mmap_offset          -       invent an mmap offset
40  *      @obj: our object
41  *
42  *      Standard implementation of offset generation for mmap as is
43  *      duplicated in several drivers. This belongs in GEM.
44  */
45 int gem_create_mmap_offset(struct drm_gem_object *obj)
46 {
47         struct drm_device *dev = obj->dev;
48         struct drm_gem_mm *mm = dev->mm_private;
49         struct drm_map_list *list;
50         struct drm_local_map *map;
51         int ret;
52
53         list = &obj->map_list;
54         list->map = kzalloc(sizeof(struct drm_map_list), GFP_KERNEL);
55         if (list->map == NULL)
56                 return -ENOMEM;
57         map = list->map;
58         map->type = _DRM_GEM;
59         map->size = obj->size;
60         map->handle = obj;
61
62         list->file_offset_node = drm_mm_search_free(&mm->offset_manager,
63                                         obj->size / PAGE_SIZE, 0, 0);
64         if (!list->file_offset_node) {
65                 dev_err(dev->dev, "failed to allocate offset for bo %d\n",
66                                                                 obj->name);
67                 ret = -ENOSPC;
68                 goto free_it;
69         }
70         list->file_offset_node = drm_mm_get_block(list->file_offset_node,
71                                         obj->size / PAGE_SIZE, 0);
72         if (!list->file_offset_node) {
73                 ret = -ENOMEM;
74                 goto free_it;
75         }
76         list->hash.key = list->file_offset_node->start;
77         ret = drm_ht_insert_item(&mm->offset_hash, &list->hash);
78         if (ret) {
79                 dev_err(dev->dev, "failed to add to map hash\n");
80                 goto free_mm;
81         }
82         return 0;
83
84 free_mm:
85         drm_mm_put_block(list->file_offset_node);
86 free_it:
87         kfree(list->map);
88         list->map = NULL;
89         return ret;
90 }