Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 1 | /* 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 | |
| 26 | #include "drmP.h" |
| 27 | #include "drm.h" |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 28 | #include "exynos_drm.h" |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 29 | |
| 30 | #include "exynos_drm_drv.h" |
Inki Dae | 2c87112 | 2011-11-12 15:23:32 +0900 | [diff] [blame] | 31 | #include "exynos_drm_gem.h" |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 32 | #include "exynos_drm_buf.h" |
| 33 | |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 34 | static int lowlevel_buffer_allocate(struct drm_device *dev, |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 35 | unsigned int flags, struct exynos_drm_gem_buf *buf) |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 36 | { |
Inki Dae | 61db75d | 2012-04-03 21:49:15 +0900 | [diff] [blame] | 37 | dma_addr_t start_addr; |
Inki Dae | b2df26c | 2012-04-23 21:01:28 +0900 | [diff] [blame] | 38 | unsigned int npages, i = 0; |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 39 | struct scatterlist *sgl; |
| 40 | int ret = 0; |
| 41 | |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 42 | DRM_DEBUG_KMS("%s\n", __FILE__); |
| 43 | |
Inki Dae | dcf9af8 | 2012-04-03 21:27:58 +0900 | [diff] [blame] | 44 | if (IS_NONCONTIG_BUFFER(flags)) { |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 45 | DRM_DEBUG_KMS("not support allocation type.\n"); |
| 46 | return -EINVAL; |
| 47 | } |
| 48 | |
| 49 | if (buf->dma_addr) { |
| 50 | DRM_DEBUG_KMS("already allocated.\n"); |
| 51 | return 0; |
| 52 | } |
| 53 | |
| 54 | if (buf->size >= SZ_1M) { |
Inki Dae | dcf9af8 | 2012-04-03 21:27:58 +0900 | [diff] [blame] | 55 | npages = buf->size >> SECTION_SHIFT; |
Inki Dae | b2df26c | 2012-04-23 21:01:28 +0900 | [diff] [blame] | 56 | buf->page_size = SECTION_SIZE; |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 57 | } else if (buf->size >= SZ_64K) { |
Inki Dae | dcf9af8 | 2012-04-03 21:27:58 +0900 | [diff] [blame] | 58 | npages = buf->size >> 16; |
Inki Dae | b2df26c | 2012-04-23 21:01:28 +0900 | [diff] [blame] | 59 | buf->page_size = SZ_64K; |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 60 | } else { |
Inki Dae | dcf9af8 | 2012-04-03 21:27:58 +0900 | [diff] [blame] | 61 | npages = buf->size >> PAGE_SHIFT; |
Inki Dae | b2df26c | 2012-04-23 21:01:28 +0900 | [diff] [blame] | 62 | buf->page_size = PAGE_SIZE; |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | buf->sgt = kzalloc(sizeof(struct sg_table), GFP_KERNEL); |
| 66 | if (!buf->sgt) { |
| 67 | DRM_ERROR("failed to allocate sg table.\n"); |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 68 | return -ENOMEM; |
| 69 | } |
| 70 | |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 71 | ret = sg_alloc_table(buf->sgt, npages, GFP_KERNEL); |
| 72 | if (ret < 0) { |
| 73 | DRM_ERROR("failed to initialize sg table.\n"); |
| 74 | kfree(buf->sgt); |
| 75 | buf->sgt = NULL; |
| 76 | return -ENOMEM; |
| 77 | } |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 78 | |
Inki Dae | 61db75d | 2012-04-03 21:49:15 +0900 | [diff] [blame] | 79 | buf->kvaddr = dma_alloc_writecombine(dev->dev, buf->size, |
| 80 | &buf->dma_addr, GFP_KERNEL); |
| 81 | if (!buf->kvaddr) { |
| 82 | DRM_ERROR("failed to allocate buffer.\n"); |
| 83 | ret = -ENOMEM; |
| 84 | goto err1; |
| 85 | } |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 86 | |
| 87 | buf->pages = kzalloc(sizeof(struct page) * npages, GFP_KERNEL); |
| 88 | if (!buf->pages) { |
| 89 | DRM_ERROR("failed to allocate pages.\n"); |
| 90 | ret = -ENOMEM; |
| 91 | goto err2; |
| 92 | } |
| 93 | |
| 94 | sgl = buf->sgt->sgl; |
Inki Dae | 61db75d | 2012-04-03 21:49:15 +0900 | [diff] [blame] | 95 | start_addr = buf->dma_addr; |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 96 | |
| 97 | while (i < npages) { |
| 98 | buf->pages[i] = phys_to_page(start_addr); |
Inki Dae | b2df26c | 2012-04-23 21:01:28 +0900 | [diff] [blame] | 99 | sg_set_page(sgl, buf->pages[i], buf->page_size, 0); |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 100 | sg_dma_address(sgl) = start_addr; |
Inki Dae | b2df26c | 2012-04-23 21:01:28 +0900 | [diff] [blame] | 101 | start_addr += buf->page_size; |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 102 | sgl = sg_next(sgl); |
| 103 | i++; |
| 104 | } |
| 105 | |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 106 | DRM_DEBUG_KMS("vaddr(0x%lx), dma_addr(0x%lx), size(0x%lx)\n", |
| 107 | (unsigned long)buf->kvaddr, |
| 108 | (unsigned long)buf->dma_addr, |
| 109 | buf->size); |
| 110 | |
| 111 | return ret; |
| 112 | err2: |
| 113 | dma_free_writecombine(dev->dev, buf->size, buf->kvaddr, |
| 114 | (dma_addr_t)buf->dma_addr); |
| 115 | buf->dma_addr = (dma_addr_t)NULL; |
| 116 | err1: |
| 117 | sg_free_table(buf->sgt); |
| 118 | kfree(buf->sgt); |
| 119 | buf->sgt = NULL; |
| 120 | |
| 121 | return ret; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | static void lowlevel_buffer_deallocate(struct drm_device *dev, |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 125 | unsigned int flags, struct exynos_drm_gem_buf *buf) |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 126 | { |
| 127 | DRM_DEBUG_KMS("%s.\n", __FILE__); |
| 128 | |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 129 | /* |
| 130 | * release only physically continuous memory and |
| 131 | * non-continuous memory would be released by exynos |
| 132 | * gem framework. |
| 133 | */ |
Inki Dae | dcf9af8 | 2012-04-03 21:27:58 +0900 | [diff] [blame] | 134 | if (IS_NONCONTIG_BUFFER(flags)) { |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 135 | DRM_DEBUG_KMS("not support allocation type.\n"); |
| 136 | return; |
| 137 | } |
| 138 | |
| 139 | if (!buf->dma_addr) { |
| 140 | DRM_DEBUG_KMS("dma_addr is invalid.\n"); |
| 141 | return; |
| 142 | } |
| 143 | |
| 144 | DRM_DEBUG_KMS("vaddr(0x%lx), dma_addr(0x%lx), size(0x%lx)\n", |
| 145 | (unsigned long)buf->kvaddr, |
| 146 | (unsigned long)buf->dma_addr, |
| 147 | buf->size); |
| 148 | |
| 149 | sg_free_table(buf->sgt); |
| 150 | |
| 151 | kfree(buf->sgt); |
| 152 | buf->sgt = NULL; |
| 153 | |
| 154 | kfree(buf->pages); |
| 155 | buf->pages = NULL; |
| 156 | |
| 157 | dma_free_writecombine(dev->dev, buf->size, buf->kvaddr, |
| 158 | (dma_addr_t)buf->dma_addr); |
| 159 | buf->dma_addr = (dma_addr_t)NULL; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 160 | } |
| 161 | |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 162 | struct exynos_drm_gem_buf *exynos_drm_init_buf(struct drm_device *dev, |
| 163 | unsigned int size) |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 164 | { |
Inki Dae | 2c87112 | 2011-11-12 15:23:32 +0900 | [diff] [blame] | 165 | struct exynos_drm_gem_buf *buffer; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 166 | |
| 167 | DRM_DEBUG_KMS("%s.\n", __FILE__); |
Inki Dae | 2c87112 | 2011-11-12 15:23:32 +0900 | [diff] [blame] | 168 | DRM_DEBUG_KMS("desired size = 0x%x\n", size); |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 169 | |
Inki Dae | 2c87112 | 2011-11-12 15:23:32 +0900 | [diff] [blame] | 170 | buffer = kzalloc(sizeof(*buffer), GFP_KERNEL); |
| 171 | if (!buffer) { |
| 172 | DRM_ERROR("failed to allocate exynos_drm_gem_buf.\n"); |
Joonyoung Shim | ee5e770 | 2011-12-13 14:20:23 +0900 | [diff] [blame] | 173 | return NULL; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 174 | } |
| 175 | |
Inki Dae | 2c87112 | 2011-11-12 15:23:32 +0900 | [diff] [blame] | 176 | buffer->size = size; |
Inki Dae | 2c87112 | 2011-11-12 15:23:32 +0900 | [diff] [blame] | 177 | return buffer; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 178 | } |
| 179 | |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 180 | void exynos_drm_fini_buf(struct drm_device *dev, |
| 181 | struct exynos_drm_gem_buf *buffer) |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 182 | { |
| 183 | DRM_DEBUG_KMS("%s.\n", __FILE__); |
| 184 | |
Inki Dae | 2c87112 | 2011-11-12 15:23:32 +0900 | [diff] [blame] | 185 | if (!buffer) { |
| 186 | DRM_DEBUG_KMS("buffer is null.\n"); |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 187 | return; |
| 188 | } |
| 189 | |
Inki Dae | 2c87112 | 2011-11-12 15:23:32 +0900 | [diff] [blame] | 190 | kfree(buffer); |
| 191 | buffer = NULL; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 192 | } |
| 193 | |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 194 | int exynos_drm_alloc_buf(struct drm_device *dev, |
| 195 | struct exynos_drm_gem_buf *buf, unsigned int flags) |
| 196 | { |
| 197 | |
| 198 | /* |
| 199 | * allocate memory region and set the memory information |
| 200 | * to vaddr and dma_addr of a buffer object. |
| 201 | */ |
| 202 | if (lowlevel_buffer_allocate(dev, flags, buf) < 0) |
| 203 | return -ENOMEM; |
| 204 | |
| 205 | return 0; |
| 206 | } |
| 207 | |
| 208 | void exynos_drm_free_buf(struct drm_device *dev, |
| 209 | unsigned int flags, struct exynos_drm_gem_buf *buffer) |
| 210 | { |
| 211 | |
| 212 | lowlevel_buffer_deallocate(dev, flags, buffer); |
| 213 | } |