blob: 9601bad47a2e6d5a2bd04deffec5ec5744bfbcee [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 Dae1169af212012-12-14 14:34:31 +090037 enum dma_attr attr;
Inki Dae4744ad22012-12-07 17:51:27 +090038 unsigned int nr_pages;
Inki Dae2b358922012-03-16 18:47:05 +090039
Inki Dae1c248b72011-10-04 19:19:01 +090040 DRM_DEBUG_KMS("%s\n", __FILE__);
41
Inki Dae2b358922012-03-16 18:47:05 +090042 if (buf->dma_addr) {
43 DRM_DEBUG_KMS("already allocated.\n");
44 return 0;
45 }
46
Inki Dae0519f9a2012-10-20 07:53:42 -070047 init_dma_attrs(&buf->dma_attrs);
48
Inki Dae1169af212012-12-14 14:34:31 +090049 /*
50 * if EXYNOS_BO_CONTIG, fully physically contiguous memory
51 * region will be allocated else physically contiguous
52 * as possible.
53 */
54 if (flags & EXYNOS_BO_CONTIG)
55 dma_set_attr(DMA_ATTR_FORCE_CONTIGUOUS, &buf->dma_attrs);
56
57 /*
58 * if EXYNOS_BO_WC or EXYNOS_BO_NONCACHABLE, writecombine mapping
59 * else cachable mapping.
60 */
61 if (flags & EXYNOS_BO_WC || !(flags & EXYNOS_BO_CACHABLE))
Inki Dae0519f9a2012-10-20 07:53:42 -070062 attr = DMA_ATTR_WRITE_COMBINE;
Inki Dae1169af212012-12-14 14:34:31 +090063 else
64 attr = DMA_ATTR_NON_CONSISTENT;
Inki Dae0519f9a2012-10-20 07:53:42 -070065
66 dma_set_attr(attr, &buf->dma_attrs);
Inki Dae4744ad22012-12-07 17:51:27 +090067 dma_set_attr(DMA_ATTR_NO_KERNEL_MAPPING, &buf->dma_attrs);
Inki Dae0519f9a2012-10-20 07:53:42 -070068
Inki Dae4744ad22012-12-07 17:51:27 +090069 buf->pages = dma_alloc_attrs(dev->dev, buf->size,
Inki Dae0519f9a2012-10-20 07:53:42 -070070 &buf->dma_addr, GFP_KERNEL, &buf->dma_attrs);
Inki Dae4744ad22012-12-07 17:51:27 +090071 if (!buf->pages) {
Inki Dae0519f9a2012-10-20 07:53:42 -070072 DRM_ERROR("failed to allocate buffer.\n");
73 return -ENOMEM;
Inki Dae2b358922012-03-16 18:47:05 +090074 }
75
Inki Dae4744ad22012-12-07 17:51:27 +090076 nr_pages = buf->size >> PAGE_SHIFT;
77 buf->sgt = drm_prime_pages_to_sg(buf->pages, nr_pages);
Inki Dae2b358922012-03-16 18:47:05 +090078 if (!buf->sgt) {
Inki Dae4744ad22012-12-07 17:51:27 +090079 DRM_ERROR("failed to get sg table.\n");
Inki Dae61db75d2012-04-03 21:49:15 +090080 ret = -ENOMEM;
Inki Dae0519f9a2012-10-20 07:53:42 -070081 goto err_free_attrs;
Inki Dae61db75d2012-04-03 21:49:15 +090082 }
Inki Dae2b358922012-03-16 18:47:05 +090083
Inki Dae4744ad22012-12-07 17:51:27 +090084 DRM_DEBUG_KMS("dma_addr(0x%lx), size(0x%lx)\n",
Inki Dae2b358922012-03-16 18:47:05 +090085 (unsigned long)buf->dma_addr,
86 buf->size);
87
88 return ret;
Inki Dae0519f9a2012-10-20 07:53:42 -070089
Inki Dae0519f9a2012-10-20 07:53:42 -070090err_free_attrs:
Inki Dae4744ad22012-12-07 17:51:27 +090091 dma_free_attrs(dev->dev, buf->size, buf->pages,
Inki Dae0519f9a2012-10-20 07:53:42 -070092 (dma_addr_t)buf->dma_addr, &buf->dma_attrs);
93 buf->dma_addr = (dma_addr_t)NULL;
Inki Dae2b358922012-03-16 18:47:05 +090094
95 return ret;
Inki Dae1c248b72011-10-04 19:19:01 +090096}
97
98static void lowlevel_buffer_deallocate(struct drm_device *dev,
Inki Dae2b358922012-03-16 18:47:05 +090099 unsigned int flags, struct exynos_drm_gem_buf *buf)
Inki Dae1c248b72011-10-04 19:19:01 +0900100{
101 DRM_DEBUG_KMS("%s.\n", __FILE__);
102
Inki Dae2b358922012-03-16 18:47:05 +0900103 if (!buf->dma_addr) {
104 DRM_DEBUG_KMS("dma_addr is invalid.\n");
105 return;
106 }
107
Inki Dae4744ad22012-12-07 17:51:27 +0900108 DRM_DEBUG_KMS("dma_addr(0x%lx), size(0x%lx)\n",
Inki Dae2b358922012-03-16 18:47:05 +0900109 (unsigned long)buf->dma_addr,
110 buf->size);
111
112 sg_free_table(buf->sgt);
113
114 kfree(buf->sgt);
115 buf->sgt = NULL;
116
Inki Dae4744ad22012-12-07 17:51:27 +0900117 dma_free_attrs(dev->dev, buf->size, buf->pages,
Inki Dae0519f9a2012-10-20 07:53:42 -0700118 (dma_addr_t)buf->dma_addr, &buf->dma_attrs);
Inki Dae2b358922012-03-16 18:47:05 +0900119 buf->dma_addr = (dma_addr_t)NULL;
Inki Dae1c248b72011-10-04 19:19:01 +0900120}
121
Inki Dae2b358922012-03-16 18:47:05 +0900122struct exynos_drm_gem_buf *exynos_drm_init_buf(struct drm_device *dev,
123 unsigned int size)
Inki Dae1c248b72011-10-04 19:19:01 +0900124{
Inki Dae2c871122011-11-12 15:23:32 +0900125 struct exynos_drm_gem_buf *buffer;
Inki Dae1c248b72011-10-04 19:19:01 +0900126
127 DRM_DEBUG_KMS("%s.\n", __FILE__);
Inki Dae2c871122011-11-12 15:23:32 +0900128 DRM_DEBUG_KMS("desired size = 0x%x\n", size);
Inki Dae1c248b72011-10-04 19:19:01 +0900129
Inki Dae2c871122011-11-12 15:23:32 +0900130 buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
131 if (!buffer) {
132 DRM_ERROR("failed to allocate exynos_drm_gem_buf.\n");
Joonyoung Shimee5e7702011-12-13 14:20:23 +0900133 return NULL;
Inki Dae1c248b72011-10-04 19:19:01 +0900134 }
135
Inki Dae2c871122011-11-12 15:23:32 +0900136 buffer->size = size;
Inki Dae2c871122011-11-12 15:23:32 +0900137 return buffer;
Inki Dae1c248b72011-10-04 19:19:01 +0900138}
139
Inki Dae2b358922012-03-16 18:47:05 +0900140void exynos_drm_fini_buf(struct drm_device *dev,
141 struct exynos_drm_gem_buf *buffer)
Inki Dae1c248b72011-10-04 19:19:01 +0900142{
143 DRM_DEBUG_KMS("%s.\n", __FILE__);
144
Inki Dae2c871122011-11-12 15:23:32 +0900145 if (!buffer) {
146 DRM_DEBUG_KMS("buffer is null.\n");
Inki Dae1c248b72011-10-04 19:19:01 +0900147 return;
148 }
149
Inki Dae2c871122011-11-12 15:23:32 +0900150 kfree(buffer);
151 buffer = NULL;
Inki Dae1c248b72011-10-04 19:19:01 +0900152}
153
Inki Dae2b358922012-03-16 18:47:05 +0900154int exynos_drm_alloc_buf(struct drm_device *dev,
155 struct exynos_drm_gem_buf *buf, unsigned int flags)
156{
157
158 /*
159 * allocate memory region and set the memory information
160 * to vaddr and dma_addr of a buffer object.
161 */
162 if (lowlevel_buffer_allocate(dev, flags, buf) < 0)
163 return -ENOMEM;
164
165 return 0;
166}
167
168void exynos_drm_free_buf(struct drm_device *dev,
169 unsigned int flags, struct exynos_drm_gem_buf *buffer)
170{
171
172 lowlevel_buffer_deallocate(dev, flags, buffer);
173}