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)
36 dma_addr_t start_addr;
37 unsigned int npages, i = 0;
38 struct scatterlist *sgl;
41 DRM_DEBUG_KMS("%s\n", __FILE__);
43 if (IS_NONCONTIG_BUFFER(flags)) {
44 DRM_DEBUG_KMS("not support allocation type.\n");
49 DRM_DEBUG_KMS("already allocated.\n");
53 if (buf->size >= SZ_1M) {
54 npages = buf->size >> SECTION_SHIFT;
55 buf->page_size = SECTION_SIZE;
56 } else if (buf->size >= SZ_64K) {
57 npages = buf->size >> 16;
58 buf->page_size = SZ_64K;
60 npages = buf->size >> PAGE_SHIFT;
61 buf->page_size = PAGE_SIZE;
64 buf->sgt = kzalloc(sizeof(struct sg_table), GFP_KERNEL);
66 DRM_ERROR("failed to allocate sg table.\n");
70 ret = sg_alloc_table(buf->sgt, npages, GFP_KERNEL);
72 DRM_ERROR("failed to initialize sg table.\n");
78 buf->kvaddr = dma_alloc_writecombine(dev->dev, buf->size,
79 &buf->dma_addr, GFP_KERNEL);
81 DRM_ERROR("failed to allocate buffer.\n");
86 buf->pages = kzalloc(sizeof(struct page) * npages, GFP_KERNEL);
88 DRM_ERROR("failed to allocate pages.\n");
94 start_addr = buf->dma_addr;
97 buf->pages[i] = phys_to_page(start_addr);
98 sg_set_page(sgl, buf->pages[i], buf->page_size, 0);
99 sg_dma_address(sgl) = start_addr;
100 start_addr += buf->page_size;
105 DRM_DEBUG_KMS("vaddr(0x%lx), dma_addr(0x%lx), size(0x%lx)\n",
106 (unsigned long)buf->kvaddr,
107 (unsigned long)buf->dma_addr,
112 dma_free_writecombine(dev->dev, buf->size, buf->kvaddr,
113 (dma_addr_t)buf->dma_addr);
114 buf->dma_addr = (dma_addr_t)NULL;
116 sg_free_table(buf->sgt);
123 static void lowlevel_buffer_deallocate(struct drm_device *dev,
124 unsigned int flags, struct exynos_drm_gem_buf *buf)
126 DRM_DEBUG_KMS("%s.\n", __FILE__);
129 * release only physically continuous memory and
130 * non-continuous memory would be released by exynos
133 if (IS_NONCONTIG_BUFFER(flags)) {
134 DRM_DEBUG_KMS("not support allocation type.\n");
138 if (!buf->dma_addr) {
139 DRM_DEBUG_KMS("dma_addr is invalid.\n");
143 DRM_DEBUG_KMS("vaddr(0x%lx), dma_addr(0x%lx), size(0x%lx)\n",
144 (unsigned long)buf->kvaddr,
145 (unsigned long)buf->dma_addr,
148 sg_free_table(buf->sgt);
156 dma_free_writecombine(dev->dev, buf->size, buf->kvaddr,
157 (dma_addr_t)buf->dma_addr);
158 buf->dma_addr = (dma_addr_t)NULL;
161 struct exynos_drm_gem_buf *exynos_drm_init_buf(struct drm_device *dev,
164 struct exynos_drm_gem_buf *buffer;
166 DRM_DEBUG_KMS("%s.\n", __FILE__);
167 DRM_DEBUG_KMS("desired size = 0x%x\n", size);
169 buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
171 DRM_ERROR("failed to allocate exynos_drm_gem_buf.\n");
179 void exynos_drm_fini_buf(struct drm_device *dev,
180 struct exynos_drm_gem_buf *buffer)
182 DRM_DEBUG_KMS("%s.\n", __FILE__);
185 DRM_DEBUG_KMS("buffer is null.\n");
193 int exynos_drm_alloc_buf(struct drm_device *dev,
194 struct exynos_drm_gem_buf *buf, unsigned int flags)
198 * allocate memory region and set the memory information
199 * to vaddr and dma_addr of a buffer object.
201 if (lowlevel_buffer_allocate(dev, flags, buf) < 0)
207 void exynos_drm_free_buf(struct drm_device *dev,
208 unsigned int flags, struct exynos_drm_gem_buf *buffer)
211 lowlevel_buffer_deallocate(dev, flags, buffer);