3 * Copyright (c) 2011 Samsung Electronics Co., Ltd.
4 * Author: Inki Dae <inki.dae@samsung.com>
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:
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
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.
27 #include <drm/exynos_drm.h>
29 #include "exynos_drm_drv.h"
30 #include "exynos_drm_gem.h"
31 #include "exynos_drm_buf.h"
33 static int lowlevel_buffer_allocate(struct drm_device *dev,
34 unsigned int flags, struct exynos_drm_gem_buf *buf)
37 enum dma_attr attr = DMA_ATTR_FORCE_CONTIGUOUS;
39 DRM_DEBUG_KMS("%s\n", __FILE__);
42 DRM_DEBUG_KMS("already allocated.\n");
46 init_dma_attrs(&buf->dma_attrs);
48 if (flags & EXYNOS_BO_NONCONTIG)
49 attr = DMA_ATTR_WRITE_COMBINE;
51 dma_set_attr(attr, &buf->dma_attrs);
53 buf->kvaddr = dma_alloc_attrs(dev->dev, buf->size,
54 &buf->dma_addr, GFP_KERNEL, &buf->dma_attrs);
56 DRM_ERROR("failed to allocate buffer.\n");
60 buf->sgt = kzalloc(sizeof(struct sg_table), GFP_KERNEL);
62 DRM_ERROR("failed to allocate sg table.\n");
67 ret = dma_get_sgtable(dev->dev, buf->sgt, buf->kvaddr, buf->dma_addr,
70 DRM_ERROR("failed to get sgtable.\n");
74 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,
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;
92 static void lowlevel_buffer_deallocate(struct drm_device *dev,
93 unsigned int flags, struct exynos_drm_gem_buf *buf)
95 DRM_DEBUG_KMS("%s.\n", __FILE__);
98 DRM_DEBUG_KMS("dma_addr is invalid.\n");
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,
107 sg_free_table(buf->sgt);
112 dma_free_attrs(dev->dev, buf->size, buf->kvaddr,
113 (dma_addr_t)buf->dma_addr, &buf->dma_attrs);
114 buf->dma_addr = (dma_addr_t)NULL;
117 struct exynos_drm_gem_buf *exynos_drm_init_buf(struct drm_device *dev,
120 struct exynos_drm_gem_buf *buffer;
122 DRM_DEBUG_KMS("%s.\n", __FILE__);
123 DRM_DEBUG_KMS("desired size = 0x%x\n", size);
125 buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
127 DRM_ERROR("failed to allocate exynos_drm_gem_buf.\n");
135 void exynos_drm_fini_buf(struct drm_device *dev,
136 struct exynos_drm_gem_buf *buffer)
138 DRM_DEBUG_KMS("%s.\n", __FILE__);
141 DRM_DEBUG_KMS("buffer is null.\n");
149 int exynos_drm_alloc_buf(struct drm_device *dev,
150 struct exynos_drm_gem_buf *buf, unsigned int flags)
154 * allocate memory region and set the memory information
155 * to vaddr and dma_addr of a buffer object.
157 if (lowlevel_buffer_allocate(dev, flags, buf) < 0)
163 void exynos_drm_free_buf(struct drm_device *dev,
164 unsigned int flags, struct exynos_drm_gem_buf *buffer)
167 lowlevel_buffer_deallocate(dev, flags, buffer);