blob: 48c589661cbe8815f99ac7589c99bfca1963a074 [file] [log] [blame]
Inki Dae1c248b72011-10-04 19:19:01 +09001/* 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
David Howells760285e2012-10-02 18:01:07 +010026#include <drm/drmP.h>
27#include <drm/exynos_drm.h>
Inki Dae1c248b72011-10-04 19:19:01 +090028
29#include "exynos_drm_drv.h"
Inki Dae2c871122011-11-12 15:23:32 +090030#include "exynos_drm_gem.h"
Inki Dae1c248b72011-10-04 19:19:01 +090031#include "exynos_drm_buf.h"
32
Inki Dae1c248b72011-10-04 19:19:01 +090033static int lowlevel_buffer_allocate(struct drm_device *dev,
Inki Dae2b358922012-03-16 18:47:05 +090034 unsigned int flags, struct exynos_drm_gem_buf *buf)
Inki Dae1c248b72011-10-04 19:19:01 +090035{
Inki Dae0519f9a2012-10-20 07:53:42 -070036 int ret = 0;
Inki Daeb2df26c2012-04-23 21:01:28 +090037 unsigned int npages, i = 0;
Inki Dae2b358922012-03-16 18:47:05 +090038 struct scatterlist *sgl;
Inki Dae0519f9a2012-10-20 07:53:42 -070039 enum dma_attr attr = DMA_ATTR_FORCE_CONTIGUOUS;
Inki Dae2b358922012-03-16 18:47:05 +090040
Inki Dae1c248b72011-10-04 19:19:01 +090041 DRM_DEBUG_KMS("%s\n", __FILE__);
42
Inki Dae2b358922012-03-16 18:47:05 +090043 if (buf->dma_addr) {
44 DRM_DEBUG_KMS("already allocated.\n");
45 return 0;
46 }
47
Inki Dae0519f9a2012-10-20 07:53:42 -070048 init_dma_attrs(&buf->dma_attrs);
49
50 if (flags & EXYNOS_BO_NONCONTIG)
51 attr = DMA_ATTR_WRITE_COMBINE;
52
53 dma_set_attr(attr, &buf->dma_attrs);
54
55 buf->kvaddr = dma_alloc_attrs(dev->dev, buf->size,
56 &buf->dma_addr, GFP_KERNEL, &buf->dma_attrs);
57 if (!buf->kvaddr) {
58 DRM_ERROR("failed to allocate buffer.\n");
59 return -ENOMEM;
Inki Dae2b358922012-03-16 18:47:05 +090060 }
61
62 buf->sgt = kzalloc(sizeof(struct sg_table), GFP_KERNEL);
63 if (!buf->sgt) {
64 DRM_ERROR("failed to allocate sg table.\n");
Inki Dae61db75d2012-04-03 21:49:15 +090065 ret = -ENOMEM;
Inki Dae0519f9a2012-10-20 07:53:42 -070066 goto err_free_attrs;
Inki Dae61db75d2012-04-03 21:49:15 +090067 }
Inki Dae2b358922012-03-16 18:47:05 +090068
Inki Dae0519f9a2012-10-20 07:53:42 -070069 ret = dma_get_sgtable(dev->dev, buf->sgt, buf->kvaddr, buf->dma_addr,
70 buf->size);
71 if (ret < 0) {
72 DRM_ERROR("failed to get sgtable.\n");
73 goto err_free_sgt;
74 }
75
76 npages = buf->sgt->nents;
77
Inki Dae2b358922012-03-16 18:47:05 +090078 buf->pages = kzalloc(sizeof(struct page) * npages, GFP_KERNEL);
79 if (!buf->pages) {
80 DRM_ERROR("failed to allocate pages.\n");
81 ret = -ENOMEM;
Inki Dae0519f9a2012-10-20 07:53:42 -070082 goto err_free_table;
Inki Dae2b358922012-03-16 18:47:05 +090083 }
84
85 sgl = buf->sgt->sgl;
Inki Dae2b358922012-03-16 18:47:05 +090086 while (i < npages) {
Inki Dae0519f9a2012-10-20 07:53:42 -070087 buf->pages[i] = sg_page(sgl);
Inki Dae2b358922012-03-16 18:47:05 +090088 sgl = sg_next(sgl);
89 i++;
90 }
91
Inki Dae2b358922012-03-16 18:47:05 +090092 DRM_DEBUG_KMS("vaddr(0x%lx), dma_addr(0x%lx), size(0x%lx)\n",
93 (unsigned long)buf->kvaddr,
94 (unsigned long)buf->dma_addr,
95 buf->size);
96
97 return ret;
Inki Dae0519f9a2012-10-20 07:53:42 -070098
99err_free_table:
Inki Dae2b358922012-03-16 18:47:05 +0900100 sg_free_table(buf->sgt);
Inki Dae0519f9a2012-10-20 07:53:42 -0700101err_free_sgt:
Inki Dae2b358922012-03-16 18:47:05 +0900102 kfree(buf->sgt);
103 buf->sgt = NULL;
Inki Dae0519f9a2012-10-20 07:53:42 -0700104err_free_attrs:
105 dma_free_attrs(dev->dev, buf->size, buf->kvaddr,
106 (dma_addr_t)buf->dma_addr, &buf->dma_attrs);
107 buf->dma_addr = (dma_addr_t)NULL;
Inki Dae2b358922012-03-16 18:47:05 +0900108
109 return ret;
Inki Dae1c248b72011-10-04 19:19:01 +0900110}
111
112static void lowlevel_buffer_deallocate(struct drm_device *dev,
Inki Dae2b358922012-03-16 18:47:05 +0900113 unsigned int flags, struct exynos_drm_gem_buf *buf)
Inki Dae1c248b72011-10-04 19:19:01 +0900114{
115 DRM_DEBUG_KMS("%s.\n", __FILE__);
116
Inki Dae2b358922012-03-16 18:47:05 +0900117 if (!buf->dma_addr) {
118 DRM_DEBUG_KMS("dma_addr is invalid.\n");
119 return;
120 }
121
122 DRM_DEBUG_KMS("vaddr(0x%lx), dma_addr(0x%lx), size(0x%lx)\n",
123 (unsigned long)buf->kvaddr,
124 (unsigned long)buf->dma_addr,
125 buf->size);
126
127 sg_free_table(buf->sgt);
128
129 kfree(buf->sgt);
130 buf->sgt = NULL;
131
Inki Dae0519f9a2012-10-20 07:53:42 -0700132 dma_free_attrs(dev->dev, buf->size, buf->kvaddr,
133 (dma_addr_t)buf->dma_addr, &buf->dma_attrs);
Inki Dae2b358922012-03-16 18:47:05 +0900134 buf->dma_addr = (dma_addr_t)NULL;
Inki Dae1c248b72011-10-04 19:19:01 +0900135}
136
Inki Dae2b358922012-03-16 18:47:05 +0900137struct exynos_drm_gem_buf *exynos_drm_init_buf(struct drm_device *dev,
138 unsigned int size)
Inki Dae1c248b72011-10-04 19:19:01 +0900139{
Inki Dae2c871122011-11-12 15:23:32 +0900140 struct exynos_drm_gem_buf *buffer;
Inki Dae1c248b72011-10-04 19:19:01 +0900141
142 DRM_DEBUG_KMS("%s.\n", __FILE__);
Inki Dae2c871122011-11-12 15:23:32 +0900143 DRM_DEBUG_KMS("desired size = 0x%x\n", size);
Inki Dae1c248b72011-10-04 19:19:01 +0900144
Inki Dae2c871122011-11-12 15:23:32 +0900145 buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
146 if (!buffer) {
147 DRM_ERROR("failed to allocate exynos_drm_gem_buf.\n");
Joonyoung Shimee5e7702011-12-13 14:20:23 +0900148 return NULL;
Inki Dae1c248b72011-10-04 19:19:01 +0900149 }
150
Inki Dae2c871122011-11-12 15:23:32 +0900151 buffer->size = size;
Inki Dae2c871122011-11-12 15:23:32 +0900152 return buffer;
Inki Dae1c248b72011-10-04 19:19:01 +0900153}
154
Inki Dae2b358922012-03-16 18:47:05 +0900155void exynos_drm_fini_buf(struct drm_device *dev,
156 struct exynos_drm_gem_buf *buffer)
Inki Dae1c248b72011-10-04 19:19:01 +0900157{
158 DRM_DEBUG_KMS("%s.\n", __FILE__);
159
Inki Dae2c871122011-11-12 15:23:32 +0900160 if (!buffer) {
161 DRM_DEBUG_KMS("buffer is null.\n");
Inki Dae1c248b72011-10-04 19:19:01 +0900162 return;
163 }
164
Inki Dae2c871122011-11-12 15:23:32 +0900165 kfree(buffer);
166 buffer = NULL;
Inki Dae1c248b72011-10-04 19:19:01 +0900167}
168
Inki Dae2b358922012-03-16 18:47:05 +0900169int exynos_drm_alloc_buf(struct drm_device *dev,
170 struct exynos_drm_gem_buf *buf, unsigned int flags)
171{
172
173 /*
174 * allocate memory region and set the memory information
175 * to vaddr and dma_addr of a buffer object.
176 */
177 if (lowlevel_buffer_allocate(dev, flags, buf) < 0)
178 return -ENOMEM;
179
180 return 0;
181}
182
183void exynos_drm_free_buf(struct drm_device *dev,
184 unsigned int flags, struct exynos_drm_gem_buf *buffer)
185{
186
187 lowlevel_buffer_deallocate(dev, flags, buffer);
188}