3 * Copyright (c) 2014 Spreadtrum Communications, Inc.
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
19 #include "video/ion_sprd.h"
20 #include "sprd_drm_drv.h"
21 #include "sprd_drm_gem.h"
22 #include "sprd_drm_buf.h"
24 static int lowlevel_buffer_allocate(struct drm_device *dev,
25 unsigned int flags, struct sprd_drm_gem_buf *buf)
27 struct scatterlist *sg = NULL;
28 struct sprd_drm_private *private;
29 unsigned int nr_pages = 0, i = 0, heap_id_mask;
30 unsigned long sgt_size = 0;
31 int ret = 0, mem_flags = 0;
33 DRM_DEBUG_KMS("%s\n", __FILE__);
36 DRM_DEBUG_KMS("already allocated.\n");
40 buf->page_size = buf->size;
41 private = dev->dev_private;
42 DRM_DEBUG_KMS("sprd_drm_ion_client:%p size:0x%x\n",
43 private->sprd_drm_ion_client,buf->size);
45 if (IS_DEV_SYSTEM_BUFFER(flags))
46 heap_id_mask = ION_HEAP_ID_MASK_SYSTEM;
47 else if (IS_DEV_MM_BUFFER(flags))
48 heap_id_mask = ION_HEAP_ID_MASK_MM;
49 else if (IS_DEV_OVERLAY_BUFFER(flags))
50 heap_id_mask = ION_HEAP_ID_MASK_OVERLAY;
51 else if (IS_DEV_GSP_BUFFER(flags))
52 heap_id_mask = ION_HEAP_ID_MASK_GSP;
54 heap_id_mask = ION_HEAP_ID_MASK_OVERLAY;
56 #ifdef CONFIG_SPRD_IOMMU
57 if (IS_NONCONTIG_BUFFER(flags)) {
58 if (heap_id_mask == ION_HEAP_ID_MASK_MM)
59 heap_id_mask = ION_HEAP_ID_MASK_MM_IOMMU;
60 else if (heap_id_mask == ION_HEAP_ID_MASK_GSP)
61 heap_id_mask = ION_HEAP_ID_MASK_GSP_IOMMU;
63 heap_id_mask = ION_HEAP_ID_MASK_SYSTEM;
67 if (IS_CACHABLE_BUFFER(flags))
68 mem_flags = ION_FLAG_CACHED;
70 buf->ion_handle = ion_alloc(private->sprd_drm_ion_client, buf->size,
71 SZ_4K, heap_id_mask, mem_flags);
73 if (IS_ERR((void *)buf->ion_handle)) {
74 DRM_ERROR("%s Could not allocate\n", __func__);
77 buf->sgt = ion_sg_table(private->sprd_drm_ion_client, buf->ion_handle);
79 DRM_ERROR("failed to get sg table.\n");
84 buf->dma_addr = sg_dma_address(buf->sgt->sgl);
86 DRM_ERROR("failed to get dma addr.\n");
90 for_each_sg(buf->sgt->sgl, sg, buf->sgt->nents, i)
93 sgt_size = sizeof(struct page) * nr_pages;
94 buf->pages = kzalloc(sgt_size, GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
97 order = get_order(sgt_size);
98 DRM_INFO("%s:sglist kzalloc failed: order:%d, trying vzalloc\n",
100 buf->pages = vzalloc(sgt_size);
102 DRM_ERROR("failed to allocate pages.\n");
108 for_each_sg(buf->sgt->sgl, sg, buf->sgt->nents, i) {
109 buf->pages[i] = phys_to_page(sg_dma_address(sg));
110 buf->page_size = sg_dma_len(sg);
113 DRM_DEBUG_KMS("dma_addr(0x%lx), size(0x%lx)\n",
114 (unsigned long)buf->dma_addr,
119 ion_free(private->sprd_drm_ion_client, buf->ion_handle);
120 buf->dma_addr = (dma_addr_t)NULL;
126 static void lowlevel_buffer_deallocate(struct drm_device *dev,
127 unsigned int flags, struct sprd_drm_gem_buf *buf)
129 struct sprd_drm_private *private;
131 private = dev->dev_private;
132 DRM_DEBUG_KMS("%s.\n", __FILE__);
134 if (is_vmalloc_addr(buf->pages))
140 ion_free(private->sprd_drm_ion_client, buf->ion_handle);
142 buf->dma_addr = (dma_addr_t)NULL;
146 struct sprd_drm_gem_buf *sprd_drm_init_buf(struct drm_device *dev,
149 struct sprd_drm_gem_buf *buffer;
151 DRM_DEBUG_KMS("%s.\n", __FILE__);
152 DRM_DEBUG_KMS("desired size = 0x%x\n", size);
154 buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
156 DRM_ERROR("failed to allocate sprd_drm_gem_buf.\n");
164 void sprd_drm_fini_buf(struct drm_device *dev,
165 struct sprd_drm_gem_buf *buffer)
167 DRM_DEBUG_KMS("%s.\n", __FILE__);
170 DRM_DEBUG_KMS("buffer is null.\n");
178 int sprd_drm_alloc_buf(struct drm_device *dev,
179 struct sprd_drm_gem_buf *buf, unsigned int flags)
183 * allocate memory region and set the memory information
184 * to vaddr and dma_addr of a buffer object.
186 if (lowlevel_buffer_allocate(dev, flags, buf) < 0)
192 void sprd_drm_free_buf(struct drm_device *dev,
193 unsigned int flags, struct sprd_drm_gem_buf *buffer)
195 lowlevel_buffer_deallocate(dev, flags, buffer);