aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHarsh Vardhan Dwivedi <hdwivedi@codeaurora.org>2012-03-29 23:23:11 (GMT)
committerJeremy Gebben <jgebben@codeaurora.org>2012-04-09 21:50:08 (GMT)
commit8cb835b656ae2c3faa79af4f374563d344b46d4a (patch)
treea8e638d90f24b4c192ee7fb17b0ecbf58d4e1521
parent3cd338619a4a9d78a7ba082b70467d0d35347cee (diff)
msm: kgsl: Allocate physical pages instead of using vmalloc
Replace vmalloc allocation with physical page allocation. For most allocations we do not need a kernel virual address. vmalloc uses up the kernel virtual address space. By replacing vmalloc with physical page alloction and mapping that allocation to kernel space only when it is required prevents the kgsl driver from using unnecessary vmalloc virtual space. Change-Id: Idc716c8366f837f06a61b154deacec65a3a0662e Signed-off-by: Harsh Vardhan Dwivedi <hdwivedi@codeaurora.org>
-rw-r--r--drivers/gpu/msm/adreno.c4
-rw-r--r--drivers/gpu/msm/adreno.h2
-rw-r--r--drivers/gpu/msm/kgsl.c4
-rw-r--r--drivers/gpu/msm/kgsl.h23
-rw-r--r--drivers/gpu/msm/kgsl_drm.c7
-rw-r--r--drivers/gpu/msm/kgsl_sharedmem.c121
-rw-r--r--drivers/gpu/msm/kgsl_sharedmem.h11
7 files changed, 120 insertions, 52 deletions
diff --git a/drivers/gpu/msm/adreno.c b/drivers/gpu/msm/adreno.c
index d7a104c..356df0d 100644
--- a/drivers/gpu/msm/adreno.c
+++ b/drivers/gpu/msm/adreno.c
@@ -975,7 +975,7 @@ static int adreno_suspend_context(struct kgsl_device *device)
return status;
}
-const struct kgsl_memdesc *adreno_find_region(struct kgsl_device *device,
+struct kgsl_memdesc *adreno_find_region(struct kgsl_device *device,
unsigned int pt_base,
unsigned int gpuaddr,
unsigned int size)
@@ -1034,7 +1034,7 @@ const struct kgsl_memdesc *adreno_find_region(struct kgsl_device *device,
uint8_t *adreno_convertaddr(struct kgsl_device *device, unsigned int pt_base,
unsigned int gpuaddr, unsigned int size)
{
- const struct kgsl_memdesc *memdesc;
+ struct kgsl_memdesc *memdesc;
memdesc = adreno_find_region(device, pt_base, gpuaddr, size);
diff --git a/drivers/gpu/msm/adreno.h b/drivers/gpu/msm/adreno.h
index 07ccb85..c370190 100644
--- a/drivers/gpu/msm/adreno.h
+++ b/drivers/gpu/msm/adreno.h
@@ -119,7 +119,7 @@ void adreno_regread(struct kgsl_device *device, unsigned int offsetwords,
void adreno_regwrite(struct kgsl_device *device, unsigned int offsetwords,
unsigned int value);
-const struct kgsl_memdesc *adreno_find_region(struct kgsl_device *device,
+struct kgsl_memdesc *adreno_find_region(struct kgsl_device *device,
unsigned int pt_base,
unsigned int gpuaddr,
unsigned int size);
diff --git a/drivers/gpu/msm/kgsl.c b/drivers/gpu/msm/kgsl.c
index 6c1199e..547727f 100644
--- a/drivers/gpu/msm/kgsl.c
+++ b/drivers/gpu/msm/kgsl.c
@@ -1286,9 +1286,9 @@ kgsl_ioctl_sharedmem_from_vmalloc(struct kgsl_device_private *dev_priv,
vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
- result = remap_vmalloc_range(vma, (void *) entry->memdesc.hostptr, 0);
+ result = kgsl_sharedmem_map_vma(vma, &entry->memdesc);
if (result) {
- KGSL_CORE_ERR("remap_vmalloc_range failed: %d\n", result);
+ KGSL_CORE_ERR("kgsl_sharedmem_map_vma failed: %d\n", result);
goto error_free_vmalloc;
}
diff --git a/drivers/gpu/msm/kgsl.h b/drivers/gpu/msm/kgsl.h
index 3dafbb9..87bba25 100644
--- a/drivers/gpu/msm/kgsl.h
+++ b/drivers/gpu/msm/kgsl.h
@@ -21,6 +21,7 @@
#include <linux/mutex.h>
#include <linux/cdev.h>
#include <linux/regulator/consumer.h>
+#include <linux/mm.h>
#define KGSL_NAME "kgsl"
@@ -98,7 +99,15 @@ struct kgsl_driver {
extern struct kgsl_driver kgsl_driver;
struct kgsl_pagetable;
-struct kgsl_memdesc_ops;
+struct kgsl_memdesc;
+
+struct kgsl_memdesc_ops {
+ int (*vmflags)(struct kgsl_memdesc *);
+ int (*vmfault)(struct kgsl_memdesc *, struct vm_area_struct *,
+ struct vm_fault *);
+ void (*free)(struct kgsl_memdesc *memdesc);
+ int (*map_kernel_mem)(struct kgsl_memdesc *);
+};
/* shared memory allocation */
struct kgsl_memdesc {
@@ -185,13 +194,15 @@ static inline int kgsl_gpuaddr_in_memdesc(const struct kgsl_memdesc *memdesc,
}
return 0;
}
-static inline uint8_t *kgsl_gpuaddr_to_vaddr(const struct kgsl_memdesc *memdesc,
+static inline uint8_t *kgsl_gpuaddr_to_vaddr(struct kgsl_memdesc *memdesc,
unsigned int gpuaddr)
{
- if (memdesc->hostptr == NULL || memdesc->gpuaddr == 0 ||
- (gpuaddr < memdesc->gpuaddr ||
- gpuaddr >= memdesc->gpuaddr + memdesc->size))
- return NULL;
+ if (memdesc->gpuaddr == 0 ||
+ gpuaddr < memdesc->gpuaddr ||
+ gpuaddr >= (memdesc->gpuaddr + memdesc->size) ||
+ (NULL == memdesc->hostptr && memdesc->ops->map_kernel_mem &&
+ memdesc->ops->map_kernel_mem(memdesc)))
+ return NULL;
return memdesc->hostptr + (gpuaddr - memdesc->gpuaddr);
}
diff --git a/drivers/gpu/msm/kgsl_drm.c b/drivers/gpu/msm/kgsl_drm.c
index 03f8c42..d43b29b 100644
--- a/drivers/gpu/msm/kgsl_drm.c
+++ b/drivers/gpu/msm/kgsl_drm.c
@@ -903,17 +903,18 @@ int kgsl_gem_kmem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
struct drm_gem_object *obj = vma->vm_private_data;
struct drm_device *dev = obj->dev;
struct drm_kgsl_gem_object *priv;
- unsigned long offset, pg;
+ unsigned long offset;
struct page *page;
+ int i;
mutex_lock(&dev->struct_mutex);
priv = obj->driver_private;
offset = (unsigned long) vmf->virtual_address - vma->vm_start;
- pg = (unsigned long) priv->memdesc.hostptr + offset;
+ i = offset >> PAGE_SHIFT;
+ page = sg_page(&(priv->memdesc.sg[i]));
- page = vmalloc_to_page((void *) pg);
if (!page) {
mutex_unlock(&dev->struct_mutex);
return VM_FAULT_SIGBUS;
diff --git a/drivers/gpu/msm/kgsl_sharedmem.c b/drivers/gpu/msm/kgsl_sharedmem.c
index 5634a19..d7d0809 100644
--- a/drivers/gpu/msm/kgsl_sharedmem.c
+++ b/drivers/gpu/msm/kgsl_sharedmem.c
@@ -299,13 +299,14 @@ static int kgsl_vmalloc_vmfault(struct kgsl_memdesc *memdesc,
struct vm_area_struct *vma,
struct vm_fault *vmf)
{
- unsigned long offset, pg;
+ unsigned long offset;
struct page *page;
+ int i;
offset = (unsigned long) vmf->virtual_address - vma->vm_start;
- pg = (unsigned long) memdesc->hostptr + offset;
- page = vmalloc_to_page((void *) pg);
+ i = offset >> PAGE_SHIFT;
+ page = sg_page(&memdesc->sg[i]);
if (page == NULL)
return VM_FAULT_SIGBUS;
@@ -322,8 +323,14 @@ static int kgsl_vmalloc_vmflags(struct kgsl_memdesc *memdesc)
static void kgsl_vmalloc_free(struct kgsl_memdesc *memdesc)
{
+ int i = 0;
+ struct scatterlist *sg;
kgsl_driver.stats.vmalloc -= memdesc->size;
- vfree(memdesc->hostptr);
+ if (memdesc->hostptr)
+ vunmap(memdesc->hostptr);
+ if (memdesc->sg)
+ for_each_sg(memdesc->sg, sg, memdesc->sglen, i)
+ __free_page(sg_page(sg));
}
static int kgsl_contiguous_vmflags(struct kgsl_memdesc *memdesc)
@@ -331,6 +338,39 @@ static int kgsl_contiguous_vmflags(struct kgsl_memdesc *memdesc)
return VM_RESERVED | VM_IO | VM_PFNMAP | VM_DONTEXPAND;
}
+/*
+ * kgsl_vmalloc_map_kernel - Map the memory in memdesc to kernel address space
+ *
+ * @memdesc - The memory descriptor which contains information about the memory
+ *
+ * Return: 0 on success else error code
+ */
+static int kgsl_vmalloc_map_kernel(struct kgsl_memdesc *memdesc)
+{
+ if (!memdesc->hostptr) {
+ pgprot_t page_prot = pgprot_writecombine(PAGE_KERNEL);
+ struct page **pages = NULL;
+ struct scatterlist *sg;
+ int i;
+ /* create a list of pages to call vmap */
+ pages = vmalloc(memdesc->sglen * sizeof(struct page *));
+ if (!pages) {
+ KGSL_CORE_ERR("vmalloc(%d) failed\n",
+ memdesc->sglen * sizeof(struct page *));
+ return -ENOMEM;
+ }
+ for_each_sg(memdesc->sg, sg, memdesc->sglen, i)
+ pages[i] = sg_page(sg);
+ memdesc->hostptr = vmap(pages, memdesc->sglen,
+ VM_IOREMAP, page_prot);
+ vfree(pages);
+ }
+ if (!memdesc->hostptr)
+ return -ENOMEM;
+
+ return 0;
+}
+
static int kgsl_contiguous_vmfault(struct kgsl_memdesc *memdesc,
struct vm_area_struct *vma,
struct vm_fault *vmf)
@@ -374,6 +414,7 @@ struct kgsl_memdesc_ops kgsl_vmalloc_ops = {
.free = kgsl_vmalloc_free,
.vmflags = kgsl_vmalloc_vmflags,
.vmfault = kgsl_vmalloc_vmfault,
+ .map_kernel_mem = kgsl_vmalloc_map_kernel,
};
EXPORT_SYMBOL(kgsl_vmalloc_ops);
@@ -411,7 +452,7 @@ EXPORT_SYMBOL(kgsl_cache_range_op);
static int
_kgsl_sharedmem_vmalloc(struct kgsl_memdesc *memdesc,
struct kgsl_pagetable *pagetable,
- void *ptr, size_t size, unsigned int protflags)
+ size_t size, unsigned int protflags)
{
int order, ret = 0;
int sglen = PAGE_ALIGN(size) / PAGE_SIZE;
@@ -421,7 +462,6 @@ _kgsl_sharedmem_vmalloc(struct kgsl_memdesc *memdesc,
memdesc->pagetable = pagetable;
memdesc->priv = KGSL_MEMFLAGS_CACHED;
memdesc->ops = &kgsl_vmalloc_ops;
- memdesc->hostptr = (void *) ptr;
memdesc->sg = vmalloc(sglen * sizeof(struct scatterlist));
if (memdesc->sg == NULL) {
@@ -434,17 +474,18 @@ _kgsl_sharedmem_vmalloc(struct kgsl_memdesc *memdesc,
memdesc->sglen = sglen;
sg_init_table(memdesc->sg, sglen);
- for (i = 0; i < memdesc->sglen; i++, ptr += PAGE_SIZE) {
- struct page *page = vmalloc_to_page(ptr);
+ for (i = 0; i < memdesc->sglen; i++) {
+ struct page *page = alloc_page(GFP_KERNEL | __GFP_ZERO |
+ __GFP_HIGHMEM);
if (!page) {
- ret = -EINVAL;
+ ret = -ENOMEM;
+ memdesc->sglen = i;
goto done;
}
+ flush_dcache_page(page);
sg_set_page(&memdesc->sg[i], page, PAGE_SIZE, 0);
}
- kgsl_cache_range_op(memdesc, KGSL_CACHE_OP_FLUSH);
-
ret = kgsl_mmu_map(pagetable, memdesc, protflags);
if (ret)
@@ -469,20 +510,18 @@ int
kgsl_sharedmem_vmalloc(struct kgsl_memdesc *memdesc,
struct kgsl_pagetable *pagetable, size_t size)
{
- void *ptr;
-
+ int ret = 0;
BUG_ON(size == 0);
size = ALIGN(size, PAGE_SIZE * 2);
- ptr = vmalloc(size);
- if (ptr == NULL) {
- KGSL_CORE_ERR("vmalloc(%d) failed\n", size);
- return -ENOMEM;
- }
-
- return _kgsl_sharedmem_vmalloc(memdesc, pagetable, ptr, size,
+ ret = _kgsl_sharedmem_vmalloc(memdesc, pagetable, size,
GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
+ if (!ret)
+ ret = kgsl_vmalloc_map_kernel(memdesc);
+ if (ret)
+ kgsl_sharedmem_free(memdesc);
+ return ret;
}
EXPORT_SYMBOL(kgsl_sharedmem_vmalloc);
@@ -491,25 +530,15 @@ kgsl_sharedmem_vmalloc_user(struct kgsl_memdesc *memdesc,
struct kgsl_pagetable *pagetable,
size_t size, int flags)
{
- void *ptr;
unsigned int protflags;
BUG_ON(size == 0);
- ptr = vmalloc_user(size);
-
- if (ptr == NULL) {
- KGSL_CORE_ERR("vmalloc_user(%d) failed: allocated=%d\n",
- size, kgsl_driver.stats.vmalloc);
- return -ENOMEM;
- }
-
- kmemleak_not_leak(ptr);
protflags = GSL_PT_PAGE_RV;
if (!(flags & KGSL_MEMFLAGS_GPUREADONLY))
protflags |= GSL_PT_PAGE_WV;
- return _kgsl_sharedmem_vmalloc(memdesc, pagetable, ptr, size,
+ return _kgsl_sharedmem_vmalloc(memdesc, pagetable, size,
protflags);
}
EXPORT_SYMBOL(kgsl_sharedmem_vmalloc_user);
@@ -692,3 +721,33 @@ kgsl_sharedmem_set(const struct kgsl_memdesc *memdesc, unsigned int offsetbytes,
return 0;
}
EXPORT_SYMBOL(kgsl_sharedmem_set);
+
+/*
+ * kgsl_sharedmem_map_vma - Map a user vma to physical memory
+ *
+ * @vma - The user vma to map
+ * @memdesc - The memory descriptor which contains information about the
+ * physical memory
+ *
+ * Return: 0 on success else error code
+ */
+int
+kgsl_sharedmem_map_vma(struct vm_area_struct *vma,
+ const struct kgsl_memdesc *memdesc)
+{
+ unsigned long addr = vma->vm_start;
+ unsigned long size = vma->vm_end - vma->vm_start;
+ int ret, i = 0;
+
+ if (!memdesc->sg || (size != memdesc->size) ||
+ (memdesc->sglen != (size / PAGE_SIZE)))
+ return -EINVAL;
+
+ for (; addr < vma->vm_end; addr += PAGE_SIZE, i++) {
+ ret = vm_insert_page(vma, addr, sg_page(&memdesc->sg[i]));
+ if (ret)
+ return ret;
+ }
+ return 0;
+}
+EXPORT_SYMBOL(kgsl_sharedmem_map_vma);
diff --git a/drivers/gpu/msm/kgsl_sharedmem.h b/drivers/gpu/msm/kgsl_sharedmem.h
index a1e4c91..effe2438 100644
--- a/drivers/gpu/msm/kgsl_sharedmem.h
+++ b/drivers/gpu/msm/kgsl_sharedmem.h
@@ -32,13 +32,6 @@ struct kgsl_process_private;
/** Set if the memdesc is mapped into all pagetables */
#define KGSL_MEMFLAGS_GLOBAL 0x00000002
-struct kgsl_memdesc_ops {
- int (*vmflags)(struct kgsl_memdesc *);
- int (*vmfault)(struct kgsl_memdesc *, struct vm_area_struct *,
- struct vm_fault *);
- void (*free)(struct kgsl_memdesc *memdesc);
-};
-
extern struct kgsl_memdesc_ops kgsl_vmalloc_ops;
int kgsl_sharedmem_vmalloc(struct kgsl_memdesc *memdesc,
@@ -92,6 +85,10 @@ static inline unsigned int kgsl_get_sg_pa(struct scatterlist *sg)
return pa;
}
+int
+kgsl_sharedmem_map_vma(struct vm_area_struct *vma,
+ const struct kgsl_memdesc *memdesc);
+
static inline int
memdesc_sg_phys(struct kgsl_memdesc *memdesc,
unsigned int physaddr, unsigned int size)