blob: 72bf97b96ba0a676ceeea99dc0e72cfb472afbc6 [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 Dae0519f9a2012-10-20 07:53:42 -070037 enum dma_attr attr = DMA_ATTR_FORCE_CONTIGUOUS;
Inki Dae2b358922012-03-16 18:47:05 +090038
Inki Dae1c248b72011-10-04 19:19:01 +090039 DRM_DEBUG_KMS("%s\n", __FILE__);
40
Inki Dae2b358922012-03-16 18:47:05 +090041 if (buf->dma_addr) {
42 DRM_DEBUG_KMS("already allocated.\n");
43 return 0;
44 }
45
Inki Dae0519f9a2012-10-20 07:53:42 -070046 init_dma_attrs(&buf->dma_attrs);
47
48 if (flags & EXYNOS_BO_NONCONTIG)
49 attr = DMA_ATTR_WRITE_COMBINE;
50
51 dma_set_attr(attr, &buf->dma_attrs);
52
53 buf->kvaddr = dma_alloc_attrs(dev->dev, buf->size,
54 &buf->dma_addr, GFP_KERNEL, &buf->dma_attrs);
55 if (!buf->kvaddr) {
56 DRM_ERROR("failed to allocate buffer.\n");
57 return -ENOMEM;
Inki Dae2b358922012-03-16 18:47:05 +090058 }
59
60 buf->sgt = kzalloc(sizeof(struct sg_table), GFP_KERNEL);
61 if (!buf->sgt) {
62 DRM_ERROR("failed to allocate sg table.\n");
Inki Dae61db75d2012-04-03 21:49:15 +090063 ret = -ENOMEM;
Inki Dae0519f9a2012-10-20 07:53:42 -070064 goto err_free_attrs;
Inki Dae61db75d2012-04-03 21:49:15 +090065 }
Inki Dae2b358922012-03-16 18:47:05 +090066
Inki Dae0519f9a2012-10-20 07:53:42 -070067 ret = dma_get_sgtable(dev->dev, buf->sgt, buf->kvaddr, buf->dma_addr,
68 buf->size);
69 if (ret < 0) {
70 DRM_ERROR("failed to get sgtable.\n");
71 goto err_free_sgt;
72 }
73
Inki Dae2b358922012-03-16 18:47:05 +090074 DRM_DEBUG_KMS("vaddr(0x%lx), dma_addr(0x%lx), size(0x%lx)\n",
75 (unsigned long)buf->kvaddr,
76 (unsigned long)buf->dma_addr,
77 buf->size);
78
79 return ret;
Inki Dae0519f9a2012-10-20 07:53:42 -070080
Inki Dae0519f9a2012-10-20 07:53:42 -070081err_free_sgt:
Inki Dae2b358922012-03-16 18:47:05 +090082 kfree(buf->sgt);
83 buf->sgt = NULL;
Inki Dae0519f9a2012-10-20 07:53:42 -070084err_free_attrs:
85 dma_free_attrs(dev->dev, buf->size, buf->kvaddr,
86 (dma_addr_t)buf->dma_addr, &buf->dma_attrs);
87 buf->dma_addr = (dma_addr_t)NULL;
Inki Dae2b358922012-03-16 18:47:05 +090088
89 return ret;
Inki Dae1c248b72011-10-04 19:19:01 +090090}
91
92static void lowlevel_buffer_deallocate(struct drm_device *dev,
Inki Dae2b358922012-03-16 18:47:05 +090093 unsigned int flags, struct exynos_drm_gem_buf *buf)
Inki Dae1c248b72011-10-04 19:19:01 +090094{
95 DRM_DEBUG_KMS("%s.\n", __FILE__);
96
Inki Dae2b358922012-03-16 18:47:05 +090097 if (!buf->dma_addr) {
98 DRM_DEBUG_KMS("dma_addr is invalid.\n");
99 return;
100 }
101
102 DRM_DEBUG_KMS("vaddr(0x%lx), dma_addr(0x%lx), size(0x%lx)\n",
103 (unsigned long)buf->kvaddr,
104 (unsigned long)buf->dma_addr,
105 buf->size);
106
107 sg_free_table(buf->sgt);
108
109 kfree(buf->sgt);
110 buf->sgt = NULL;
111
Inki Dae0519f9a2012-10-20 07:53:42 -0700112 dma_free_attrs(dev->dev, buf->size, buf->kvaddr,
113 (dma_addr_t)buf->dma_addr, &buf->dma_attrs);
Inki Dae2b358922012-03-16 18:47:05 +0900114 buf->dma_addr = (dma_addr_t)NULL;
Inki Dae1c248b72011-10-04 19:19:01 +0900115}
116
Inki Dae2b358922012-03-16 18:47:05 +0900117struct exynos_drm_gem_buf *exynos_drm_init_buf(struct drm_device *dev,
118 unsigned int size)
Inki Dae1c248b72011-10-04 19:19:01 +0900119{
Inki Dae2c871122011-11-12 15:23:32 +0900120 struct exynos_drm_gem_buf *buffer;
Inki Dae1c248b72011-10-04 19:19:01 +0900121
122 DRM_DEBUG_KMS("%s.\n", __FILE__);
Inki Dae2c871122011-11-12 15:23:32 +0900123 DRM_DEBUG_KMS("desired size = 0x%x\n", size);
Inki Dae1c248b72011-10-04 19:19:01 +0900124
Inki Dae2c871122011-11-12 15:23:32 +0900125 buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
126 if (!buffer) {
127 DRM_ERROR("failed to allocate exynos_drm_gem_buf.\n");
Joonyoung Shimee5e7702011-12-13 14:20:23 +0900128 return NULL;
Inki Dae1c248b72011-10-04 19:19:01 +0900129 }
130
Inki Dae2c871122011-11-12 15:23:32 +0900131 buffer->size = size;
Inki Dae2c871122011-11-12 15:23:32 +0900132 return buffer;
Inki Dae1c248b72011-10-04 19:19:01 +0900133}
134
Inki Dae2b358922012-03-16 18:47:05 +0900135void exynos_drm_fini_buf(struct drm_device *dev,
136 struct exynos_drm_gem_buf *buffer)
Inki Dae1c248b72011-10-04 19:19:01 +0900137{
138 DRM_DEBUG_KMS("%s.\n", __FILE__);
139
Inki Dae2c871122011-11-12 15:23:32 +0900140 if (!buffer) {
141 DRM_DEBUG_KMS("buffer is null.\n");
Inki Dae1c248b72011-10-04 19:19:01 +0900142 return;
143 }
144
Inki Dae2c871122011-11-12 15:23:32 +0900145 kfree(buffer);
146 buffer = NULL;
Inki Dae1c248b72011-10-04 19:19:01 +0900147}
148
Inki Dae2b358922012-03-16 18:47:05 +0900149int exynos_drm_alloc_buf(struct drm_device *dev,
150 struct exynos_drm_gem_buf *buf, unsigned int flags)
151{
152
153 /*
154 * allocate memory region and set the memory information
155 * to vaddr and dma_addr of a buffer object.
156 */
157 if (lowlevel_buffer_allocate(dev, flags, buf) < 0)
158 return -ENOMEM;
159
160 return 0;
161}
162
163void exynos_drm_free_buf(struct drm_device *dev,
164 unsigned int flags, struct exynos_drm_gem_buf *buffer)
165{
166
167 lowlevel_buffer_deallocate(dev, flags, buffer);
168}