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)
38 unsigned int nr_pages;
40 DRM_DEBUG_KMS("%s\n", __FILE__);
43 DRM_DEBUG_KMS("already allocated.\n");
47 init_dma_attrs(&buf->dma_attrs);
50 * if EXYNOS_BO_CONTIG, fully physically contiguous memory
51 * region will be allocated else physically contiguous
54 if (flags & EXYNOS_BO_CONTIG)
55 dma_set_attr(DMA_ATTR_FORCE_CONTIGUOUS, &buf->dma_attrs);
58 * if EXYNOS_BO_WC or EXYNOS_BO_NONCACHABLE, writecombine mapping
59 * else cachable mapping.
61 if (flags & EXYNOS_BO_WC || !(flags & EXYNOS_BO_CACHABLE))
62 attr = DMA_ATTR_WRITE_COMBINE;
64 attr = DMA_ATTR_NON_CONSISTENT;
66 dma_set_attr(attr, &buf->dma_attrs);
67 dma_set_attr(DMA_ATTR_NO_KERNEL_MAPPING, &buf->dma_attrs);
69 buf->pages = dma_alloc_attrs(dev->dev, buf->size,
70 &buf->dma_addr, GFP_KERNEL, &buf->dma_attrs);
72 DRM_ERROR("failed to allocate buffer.\n");
76 nr_pages = buf->size >> PAGE_SHIFT;
77 buf->sgt = drm_prime_pages_to_sg(buf->pages, nr_pages);
79 DRM_ERROR("failed to get sg table.\n");
84 DRM_DEBUG_KMS("dma_addr(0x%lx), size(0x%lx)\n",
85 (unsigned long)buf->dma_addr,
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;
98 static void lowlevel_buffer_deallocate(struct drm_device *dev,
99 unsigned int flags, struct exynos_drm_gem_buf *buf)
101 DRM_DEBUG_KMS("%s.\n", __FILE__);
103 if (!buf->dma_addr) {
104 DRM_DEBUG_KMS("dma_addr is invalid.\n");
108 DRM_DEBUG_KMS("dma_addr(0x%lx), size(0x%lx)\n",
109 (unsigned long)buf->dma_addr,
112 sg_free_table(buf->sgt);
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;
122 struct exynos_drm_gem_buf *exynos_drm_init_buf(struct drm_device *dev,
125 struct exynos_drm_gem_buf *buffer;
127 DRM_DEBUG_KMS("%s.\n", __FILE__);
128 DRM_DEBUG_KMS("desired size = 0x%x\n", size);
130 buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
132 DRM_ERROR("failed to allocate exynos_drm_gem_buf.\n");
140 void exynos_drm_fini_buf(struct drm_device *dev,
141 struct exynos_drm_gem_buf *buffer)
143 DRM_DEBUG_KMS("%s.\n", __FILE__);
146 DRM_DEBUG_KMS("buffer is null.\n");
154 int exynos_drm_alloc_buf(struct drm_device *dev,
155 struct exynos_drm_gem_buf *buf, unsigned int flags)
159 * allocate memory region and set the memory information
160 * to vaddr and dma_addr of a buffer object.
162 if (lowlevel_buffer_allocate(dev, flags, buf) < 0)
168 void exynos_drm_free_buf(struct drm_device *dev,
169 unsigned int flags, struct exynos_drm_gem_buf *buffer)
172 lowlevel_buffer_deallocate(dev, flags, buffer);