brcmsmac: rework of mac80211 .flush() callback operation
[pandora-kernel.git] / drivers / gpu / drm / exynos / exynos_drm_buf.c
1 /* exynos_drm_buf.c
2  *
3  * Copyright (c) 2011 Samsung Electronics Co., Ltd.
4  * Author: Inki Dae <inki.dae@samsung.com>
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice (including the next
14  * paragraph) shall be included in all copies or substantial portions of the
15  * Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20  * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23  * OTHER DEALINGS IN THE SOFTWARE.
24  */
25
26 #include <drm/drmP.h>
27 #include <drm/exynos_drm.h>
28
29 #include "exynos_drm_drv.h"
30 #include "exynos_drm_gem.h"
31 #include "exynos_drm_buf.h"
32
33 static int lowlevel_buffer_allocate(struct drm_device *dev,
34                 unsigned int flags, struct exynos_drm_gem_buf *buf)
35 {
36         int ret = 0;
37         enum dma_attr attr;
38         unsigned int nr_pages;
39
40         DRM_DEBUG_KMS("%s\n", __FILE__);
41
42         if (buf->dma_addr) {
43                 DRM_DEBUG_KMS("already allocated.\n");
44                 return 0;
45         }
46
47         init_dma_attrs(&buf->dma_attrs);
48
49         /*
50          * if EXYNOS_BO_CONTIG, fully physically contiguous memory
51          * region will be allocated else physically contiguous
52          * as possible.
53          */
54         if (flags & EXYNOS_BO_CONTIG)
55                 dma_set_attr(DMA_ATTR_FORCE_CONTIGUOUS, &buf->dma_attrs);
56
57         /*
58          * if EXYNOS_BO_WC or EXYNOS_BO_NONCACHABLE, writecombine mapping
59          * else cachable mapping.
60          */
61         if (flags & EXYNOS_BO_WC || !(flags & EXYNOS_BO_CACHABLE))
62                 attr = DMA_ATTR_WRITE_COMBINE;
63         else
64                 attr = DMA_ATTR_NON_CONSISTENT;
65
66         dma_set_attr(attr, &buf->dma_attrs);
67         dma_set_attr(DMA_ATTR_NO_KERNEL_MAPPING, &buf->dma_attrs);
68
69         buf->pages = dma_alloc_attrs(dev->dev, buf->size,
70                         &buf->dma_addr, GFP_KERNEL, &buf->dma_attrs);
71         if (!buf->pages) {
72                 DRM_ERROR("failed to allocate buffer.\n");
73                 return -ENOMEM;
74         }
75
76         nr_pages = buf->size >> PAGE_SHIFT;
77         buf->sgt = drm_prime_pages_to_sg(buf->pages, nr_pages);
78         if (!buf->sgt) {
79                 DRM_ERROR("failed to get sg table.\n");
80                 ret = -ENOMEM;
81                 goto err_free_attrs;
82         }
83
84         DRM_DEBUG_KMS("dma_addr(0x%lx), size(0x%lx)\n",
85                         (unsigned long)buf->dma_addr,
86                         buf->size);
87
88         return ret;
89
90 err_free_attrs:
91         dma_free_attrs(dev->dev, buf->size, buf->pages,
92                         (dma_addr_t)buf->dma_addr, &buf->dma_attrs);
93         buf->dma_addr = (dma_addr_t)NULL;
94
95         return ret;
96 }
97
98 static void lowlevel_buffer_deallocate(struct drm_device *dev,
99                 unsigned int flags, struct exynos_drm_gem_buf *buf)
100 {
101         DRM_DEBUG_KMS("%s.\n", __FILE__);
102
103         if (!buf->dma_addr) {
104                 DRM_DEBUG_KMS("dma_addr is invalid.\n");
105                 return;
106         }
107
108         DRM_DEBUG_KMS("dma_addr(0x%lx), size(0x%lx)\n",
109                         (unsigned long)buf->dma_addr,
110                         buf->size);
111
112         sg_free_table(buf->sgt);
113
114         kfree(buf->sgt);
115         buf->sgt = NULL;
116
117         dma_free_attrs(dev->dev, buf->size, buf->pages,
118                                 (dma_addr_t)buf->dma_addr, &buf->dma_attrs);
119         buf->dma_addr = (dma_addr_t)NULL;
120 }
121
122 struct exynos_drm_gem_buf *exynos_drm_init_buf(struct drm_device *dev,
123                                                 unsigned int size)
124 {
125         struct exynos_drm_gem_buf *buffer;
126
127         DRM_DEBUG_KMS("%s.\n", __FILE__);
128         DRM_DEBUG_KMS("desired size = 0x%x\n", size);
129
130         buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
131         if (!buffer) {
132                 DRM_ERROR("failed to allocate exynos_drm_gem_buf.\n");
133                 return NULL;
134         }
135
136         buffer->size = size;
137         return buffer;
138 }
139
140 void exynos_drm_fini_buf(struct drm_device *dev,
141                                 struct exynos_drm_gem_buf *buffer)
142 {
143         DRM_DEBUG_KMS("%s.\n", __FILE__);
144
145         if (!buffer) {
146                 DRM_DEBUG_KMS("buffer is null.\n");
147                 return;
148         }
149
150         kfree(buffer);
151         buffer = NULL;
152 }
153
154 int exynos_drm_alloc_buf(struct drm_device *dev,
155                 struct exynos_drm_gem_buf *buf, unsigned int flags)
156 {
157
158         /*
159          * allocate memory region and set the memory information
160          * to vaddr and dma_addr of a buffer object.
161          */
162         if (lowlevel_buffer_allocate(dev, flags, buf) < 0)
163                 return -ENOMEM;
164
165         return 0;
166 }
167
168 void exynos_drm_free_buf(struct drm_device *dev,
169                 unsigned int flags, struct exynos_drm_gem_buf *buffer)
170 {
171
172         lowlevel_buffer_deallocate(dev, flags, buffer);
173 }