blob: c579b261f0dce8825489e5af22071f103c1a5f62 [file] [log] [blame]
Jeff Boody28afec42012-01-18 15:47:46 -07001/* Copyright (c) 2002,2007-2012, Code Aurora Forum. All rights reserved.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 */
Steve Mucklef132c6c2012-06-06 18:30:57 -070013
14#include <linux/export.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070015#include <linux/vmalloc.h>
16#include <linux/memory_alloc.h>
17#include <asm/cacheflush.h>
Anshuman Danieecd5202012-02-17 19:52:49 +053018#include <linux/slab.h>
19#include <linux/kmemleak.h>
Jordan Crouse89bd3232012-07-02 17:50:15 -060020#include <linux/highmem.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070021
22#include "kgsl.h"
23#include "kgsl_sharedmem.h"
24#include "kgsl_cffdump.h"
25#include "kgsl_device.h"
26
Jordan Crouse1b897cf2011-10-12 16:57:48 -060027/* An attribute for showing per-process memory statistics */
28struct kgsl_mem_entry_attribute {
29 struct attribute attr;
30 int memtype;
31 ssize_t (*show)(struct kgsl_process_private *priv,
32 int type, char *buf);
33};
34
35#define to_mem_entry_attr(a) \
36container_of(a, struct kgsl_mem_entry_attribute, attr)
37
38#define __MEM_ENTRY_ATTR(_type, _name, _show) \
39{ \
40 .attr = { .name = __stringify(_name), .mode = 0444 }, \
41 .memtype = _type, \
42 .show = _show, \
43}
44
45/*
46 * A structure to hold the attributes for a particular memory type.
47 * For each memory type in each process we store the current and maximum
48 * memory usage and display the counts in sysfs. This structure and
49 * the following macro allow us to simplify the definition for those
50 * adding new memory types
51 */
52
53struct mem_entry_stats {
54 int memtype;
55 struct kgsl_mem_entry_attribute attr;
56 struct kgsl_mem_entry_attribute max_attr;
57};
58
59
60#define MEM_ENTRY_STAT(_type, _name) \
61{ \
62 .memtype = _type, \
63 .attr = __MEM_ENTRY_ATTR(_type, _name, mem_entry_show), \
64 .max_attr = __MEM_ENTRY_ATTR(_type, _name##_max, \
65 mem_entry_max_show), \
66}
67
68
Jordan Crouse7d3139b2012-05-18 10:05:02 -060069/*
70 * One page allocation for a guard region to protect against over-zealous
71 * GPU pre-fetch
72 */
73
74static struct page *kgsl_guard_page;
75
Jordan Crouse1b897cf2011-10-12 16:57:48 -060076/**
77 * Given a kobj, find the process structure attached to it
78 */
79
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070080static struct kgsl_process_private *
81_get_priv_from_kobj(struct kobject *kobj)
82{
83 struct kgsl_process_private *private;
84 unsigned long name;
85
86 if (!kobj)
87 return NULL;
88
89 if (sscanf(kobj->name, "%ld", &name) != 1)
90 return NULL;
91
92 list_for_each_entry(private, &kgsl_driver.process_list, list) {
93 if (private->pid == name)
94 return private;
95 }
96
97 return NULL;
98}
99
Jordan Crouse1b897cf2011-10-12 16:57:48 -0600100/**
101 * Show the current amount of memory allocated for the given memtype
102 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700103
104static ssize_t
Jordan Crouse1b897cf2011-10-12 16:57:48 -0600105mem_entry_show(struct kgsl_process_private *priv, int type, char *buf)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700106{
Jordan Crouse1b897cf2011-10-12 16:57:48 -0600107 return snprintf(buf, PAGE_SIZE, "%d\n", priv->stats[type].cur);
108}
109
110/**
111 * Show the maximum memory allocated for the given memtype through the life of
112 * the process
113 */
114
115static ssize_t
116mem_entry_max_show(struct kgsl_process_private *priv, int type, char *buf)
117{
118 return snprintf(buf, PAGE_SIZE, "%d\n", priv->stats[type].max);
119}
120
121
122static void mem_entry_sysfs_release(struct kobject *kobj)
123{
124}
125
126static ssize_t mem_entry_sysfs_show(struct kobject *kobj,
127 struct attribute *attr, char *buf)
128{
129 struct kgsl_mem_entry_attribute *pattr = to_mem_entry_attr(attr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700130 struct kgsl_process_private *priv;
Jordan Crouse1b897cf2011-10-12 16:57:48 -0600131 ssize_t ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700132
133 mutex_lock(&kgsl_driver.process_mutex);
134 priv = _get_priv_from_kobj(kobj);
135
Jordan Crouse1b897cf2011-10-12 16:57:48 -0600136 if (priv && pattr->show)
137 ret = pattr->show(priv, pattr->memtype, buf);
138 else
139 ret = -EIO;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700140
141 mutex_unlock(&kgsl_driver.process_mutex);
Jordan Crouse1b897cf2011-10-12 16:57:48 -0600142 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700143}
144
Jordan Crouse1b897cf2011-10-12 16:57:48 -0600145static const struct sysfs_ops mem_entry_sysfs_ops = {
146 .show = mem_entry_sysfs_show,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700147};
148
Jordan Crouse1b897cf2011-10-12 16:57:48 -0600149static struct kobj_type ktype_mem_entry = {
150 .sysfs_ops = &mem_entry_sysfs_ops,
151 .default_attrs = NULL,
152 .release = mem_entry_sysfs_release
153};
154
155static struct mem_entry_stats mem_stats[] = {
156 MEM_ENTRY_STAT(KGSL_MEM_ENTRY_KERNEL, kernel),
157#ifdef CONFIG_ANDROID_PMEM
158 MEM_ENTRY_STAT(KGSL_MEM_ENTRY_PMEM, pmem),
159#endif
160#ifdef CONFIG_ASHMEM
161 MEM_ENTRY_STAT(KGSL_MEM_ENTRY_ASHMEM, ashmem),
162#endif
163 MEM_ENTRY_STAT(KGSL_MEM_ENTRY_USER, user),
Jordan Crouse8eab35a2011-10-12 16:57:48 -0600164#ifdef CONFIG_ION
Jeremy Gebbenff6eab02012-01-09 09:42:21 -0700165 MEM_ENTRY_STAT(KGSL_MEM_ENTRY_ION, ion),
Jordan Crouse8eab35a2011-10-12 16:57:48 -0600166#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700167};
168
169void
170kgsl_process_uninit_sysfs(struct kgsl_process_private *private)
171{
Jordan Crouse1b897cf2011-10-12 16:57:48 -0600172 int i;
173
174 for (i = 0; i < ARRAY_SIZE(mem_stats); i++) {
175 sysfs_remove_file(&private->kobj, &mem_stats[i].attr.attr);
176 sysfs_remove_file(&private->kobj,
177 &mem_stats[i].max_attr.attr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700178 }
Jordan Crouse1b897cf2011-10-12 16:57:48 -0600179
180 kobject_put(&private->kobj);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700181}
182
183void
184kgsl_process_init_sysfs(struct kgsl_process_private *private)
185{
186 unsigned char name[16];
Jordan Crouse1b897cf2011-10-12 16:57:48 -0600187 int i, ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700188
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700189 snprintf(name, sizeof(name), "%d", private->pid);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700190
Jordan Crouse1b897cf2011-10-12 16:57:48 -0600191 if (kobject_init_and_add(&private->kobj, &ktype_mem_entry,
192 kgsl_driver.prockobj, name))
193 return;
194
195 for (i = 0; i < ARRAY_SIZE(mem_stats); i++) {
196 /* We need to check the value of sysfs_create_file, but we
197 * don't really care if it passed or not */
198
199 ret = sysfs_create_file(&private->kobj,
200 &mem_stats[i].attr.attr);
201 ret = sysfs_create_file(&private->kobj,
202 &mem_stats[i].max_attr.attr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700203 }
204}
205
206static int kgsl_drv_memstat_show(struct device *dev,
207 struct device_attribute *attr,
208 char *buf)
209{
210 unsigned int val = 0;
211
212 if (!strncmp(attr->attr.name, "vmalloc", 7))
213 val = kgsl_driver.stats.vmalloc;
214 else if (!strncmp(attr->attr.name, "vmalloc_max", 11))
215 val = kgsl_driver.stats.vmalloc_max;
Harsh Vardhan Dwivedif99c2632012-03-15 14:17:11 -0600216 else if (!strncmp(attr->attr.name, "page_alloc", 10))
217 val = kgsl_driver.stats.page_alloc;
218 else if (!strncmp(attr->attr.name, "page_alloc_max", 14))
219 val = kgsl_driver.stats.page_alloc_max;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700220 else if (!strncmp(attr->attr.name, "coherent", 8))
221 val = kgsl_driver.stats.coherent;
222 else if (!strncmp(attr->attr.name, "coherent_max", 12))
223 val = kgsl_driver.stats.coherent_max;
224 else if (!strncmp(attr->attr.name, "mapped", 6))
225 val = kgsl_driver.stats.mapped;
226 else if (!strncmp(attr->attr.name, "mapped_max", 10))
227 val = kgsl_driver.stats.mapped_max;
228
229 return snprintf(buf, PAGE_SIZE, "%u\n", val);
230}
231
232static int kgsl_drv_histogram_show(struct device *dev,
233 struct device_attribute *attr,
234 char *buf)
235{
236 int len = 0;
237 int i;
238
239 for (i = 0; i < 16; i++)
Jeremy Gebbena87bb862011-08-08 16:09:38 -0600240 len += snprintf(buf + len, PAGE_SIZE - len, "%d ",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700241 kgsl_driver.stats.histogram[i]);
242
Jeremy Gebbena87bb862011-08-08 16:09:38 -0600243 len += snprintf(buf + len, PAGE_SIZE - len, "\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700244 return len;
245}
246
247DEVICE_ATTR(vmalloc, 0444, kgsl_drv_memstat_show, NULL);
248DEVICE_ATTR(vmalloc_max, 0444, kgsl_drv_memstat_show, NULL);
Harsh Vardhan Dwivedif99c2632012-03-15 14:17:11 -0600249DEVICE_ATTR(page_alloc, 0444, kgsl_drv_memstat_show, NULL);
250DEVICE_ATTR(page_alloc_max, 0444, kgsl_drv_memstat_show, NULL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700251DEVICE_ATTR(coherent, 0444, kgsl_drv_memstat_show, NULL);
252DEVICE_ATTR(coherent_max, 0444, kgsl_drv_memstat_show, NULL);
253DEVICE_ATTR(mapped, 0444, kgsl_drv_memstat_show, NULL);
254DEVICE_ATTR(mapped_max, 0444, kgsl_drv_memstat_show, NULL);
255DEVICE_ATTR(histogram, 0444, kgsl_drv_histogram_show, NULL);
256
257static const struct device_attribute *drv_attr_list[] = {
258 &dev_attr_vmalloc,
259 &dev_attr_vmalloc_max,
Harsh Vardhan Dwivedif99c2632012-03-15 14:17:11 -0600260 &dev_attr_page_alloc,
261 &dev_attr_page_alloc_max,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700262 &dev_attr_coherent,
263 &dev_attr_coherent_max,
264 &dev_attr_mapped,
265 &dev_attr_mapped_max,
266 &dev_attr_histogram,
267 NULL
268};
269
270void
271kgsl_sharedmem_uninit_sysfs(void)
272{
273 kgsl_remove_device_sysfs_files(&kgsl_driver.virtdev, drv_attr_list);
274}
275
276int
277kgsl_sharedmem_init_sysfs(void)
278{
279 return kgsl_create_device_sysfs_files(&kgsl_driver.virtdev,
280 drv_attr_list);
281}
282
283#ifdef CONFIG_OUTER_CACHE
284static void _outer_cache_range_op(int op, unsigned long addr, size_t size)
285{
286 switch (op) {
287 case KGSL_CACHE_OP_FLUSH:
288 outer_flush_range(addr, addr + size);
289 break;
290 case KGSL_CACHE_OP_CLEAN:
291 outer_clean_range(addr, addr + size);
292 break;
293 case KGSL_CACHE_OP_INV:
294 outer_inv_range(addr, addr + size);
295 break;
296 }
297}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700298
Jordan Croused17e9aa2011-10-12 16:57:48 -0600299static void outer_cache_range_op_sg(struct scatterlist *sg, int sglen, int op)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700300{
Jordan Croused17e9aa2011-10-12 16:57:48 -0600301 struct scatterlist *s;
302 int i;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700303
Jordan Croused17e9aa2011-10-12 16:57:48 -0600304 for_each_sg(sg, s, sglen, i) {
Jeremy Gebben582fe312012-03-23 10:19:44 -0600305 unsigned int paddr = kgsl_get_sg_pa(s);
Jordan Croused17e9aa2011-10-12 16:57:48 -0600306 _outer_cache_range_op(op, paddr, s->length);
307 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700308}
309
Jordan Croused17e9aa2011-10-12 16:57:48 -0600310#else
311static void outer_cache_range_op_sg(struct scatterlist *sg, int sglen, int op)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700312{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700313}
314#endif
315
Harsh Vardhan Dwivedif99c2632012-03-15 14:17:11 -0600316static int kgsl_page_alloc_vmfault(struct kgsl_memdesc *memdesc,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700317 struct vm_area_struct *vma,
318 struct vm_fault *vmf)
319{
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -0600320 unsigned long offset;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700321 struct page *page;
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -0600322 int i;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700323
324 offset = (unsigned long) vmf->virtual_address - vma->vm_start;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700325
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -0600326 i = offset >> PAGE_SHIFT;
327 page = sg_page(&memdesc->sg[i]);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700328 if (page == NULL)
329 return VM_FAULT_SIGBUS;
330
331 get_page(page);
332
333 vmf->page = page;
334 return 0;
335}
336
Harsh Vardhan Dwivedif99c2632012-03-15 14:17:11 -0600337static int kgsl_page_alloc_vmflags(struct kgsl_memdesc *memdesc)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700338{
339 return VM_RESERVED | VM_DONTEXPAND;
340}
341
Harsh Vardhan Dwivedif99c2632012-03-15 14:17:11 -0600342static void kgsl_page_alloc_free(struct kgsl_memdesc *memdesc)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700343{
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -0600344 int i = 0;
345 struct scatterlist *sg;
Jordan Crouse7d3139b2012-05-18 10:05:02 -0600346 int sglen = memdesc->sglen;
347
348 /* Don't free the guard page if it was used */
349 if (memdesc->flags & KGSL_MEMDESC_GUARD_PAGE)
350 sglen--;
351
Harsh Vardhan Dwivedif99c2632012-03-15 14:17:11 -0600352 kgsl_driver.stats.page_alloc -= memdesc->size;
Jordan Crouse7d3139b2012-05-18 10:05:02 -0600353
Harsh Vardhan Dwivedif99c2632012-03-15 14:17:11 -0600354 if (memdesc->hostptr) {
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -0600355 vunmap(memdesc->hostptr);
Harsh Vardhan Dwivedif99c2632012-03-15 14:17:11 -0600356 kgsl_driver.stats.vmalloc -= memdesc->size;
357 }
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -0600358 if (memdesc->sg)
Jordan Crouse7d3139b2012-05-18 10:05:02 -0600359 for_each_sg(memdesc->sg, sg, sglen, i)
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -0600360 __free_page(sg_page(sg));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700361}
362
363static int kgsl_contiguous_vmflags(struct kgsl_memdesc *memdesc)
364{
365 return VM_RESERVED | VM_IO | VM_PFNMAP | VM_DONTEXPAND;
366}
367
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -0600368/*
Harsh Vardhan Dwivedif99c2632012-03-15 14:17:11 -0600369 * kgsl_page_alloc_map_kernel - Map the memory in memdesc to kernel address
370 * space
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -0600371 *
372 * @memdesc - The memory descriptor which contains information about the memory
373 *
374 * Return: 0 on success else error code
375 */
Harsh Vardhan Dwivedif99c2632012-03-15 14:17:11 -0600376static int kgsl_page_alloc_map_kernel(struct kgsl_memdesc *memdesc)
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -0600377{
378 if (!memdesc->hostptr) {
379 pgprot_t page_prot = pgprot_writecombine(PAGE_KERNEL);
380 struct page **pages = NULL;
381 struct scatterlist *sg;
Jordan Crouse7d3139b2012-05-18 10:05:02 -0600382 int sglen = memdesc->sglen;
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -0600383 int i;
Jordan Crouse7d3139b2012-05-18 10:05:02 -0600384
385 /* Don't map the guard page if it exists */
386 if (memdesc->flags & KGSL_MEMDESC_GUARD_PAGE)
387 sglen--;
388
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -0600389 /* create a list of pages to call vmap */
Iliyan Malcheva1a8c2a2012-07-05 13:36:47 -0700390 pages = kmalloc(sglen * sizeof(struct page *), GFP_KERNEL);
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -0600391 if (!pages) {
Iliyan Malcheva1a8c2a2012-07-05 13:36:47 -0700392 KGSL_CORE_ERR("kmalloc(%d) failed\n",
Jordan Crouse7d3139b2012-05-18 10:05:02 -0600393 sglen * sizeof(struct page *));
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -0600394 return -ENOMEM;
395 }
Jordan Crouse7d3139b2012-05-18 10:05:02 -0600396 for_each_sg(memdesc->sg, sg, sglen, i)
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -0600397 pages[i] = sg_page(sg);
Jordan Crouse7d3139b2012-05-18 10:05:02 -0600398 memdesc->hostptr = vmap(pages, sglen,
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -0600399 VM_IOREMAP, page_prot);
Harsh Vardhan Dwivedif99c2632012-03-15 14:17:11 -0600400 KGSL_STATS_ADD(memdesc->size, kgsl_driver.stats.vmalloc,
401 kgsl_driver.stats.vmalloc_max);
Iliyan Malcheva1a8c2a2012-07-05 13:36:47 -0700402 kfree(pages);
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -0600403 }
404 if (!memdesc->hostptr)
405 return -ENOMEM;
406
407 return 0;
408}
409
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700410static int kgsl_contiguous_vmfault(struct kgsl_memdesc *memdesc,
411 struct vm_area_struct *vma,
412 struct vm_fault *vmf)
413{
414 unsigned long offset, pfn;
415 int ret;
416
417 offset = ((unsigned long) vmf->virtual_address - vma->vm_start) >>
418 PAGE_SHIFT;
419
420 pfn = (memdesc->physaddr >> PAGE_SHIFT) + offset;
421 ret = vm_insert_pfn(vma, (unsigned long) vmf->virtual_address, pfn);
422
423 if (ret == -ENOMEM || ret == -EAGAIN)
424 return VM_FAULT_OOM;
425 else if (ret == -EFAULT)
426 return VM_FAULT_SIGBUS;
427
428 return VM_FAULT_NOPAGE;
429}
430
431static void kgsl_ebimem_free(struct kgsl_memdesc *memdesc)
432
433{
434 kgsl_driver.stats.coherent -= memdesc->size;
435 if (memdesc->hostptr)
436 iounmap(memdesc->hostptr);
437
438 free_contiguous_memory_by_paddr(memdesc->physaddr);
439}
440
Shubhraprakash Das387b6692012-08-11 18:26:29 -0700441static int kgsl_ebimem_map_kernel(struct kgsl_memdesc *memdesc)
442{
443 if (!memdesc->hostptr) {
444 memdesc->hostptr = ioremap(memdesc->physaddr, memdesc->size);
445 if (!memdesc->hostptr) {
446 KGSL_CORE_ERR("ioremap failed, addr:0x%p, size:0x%x\n",
447 memdesc->hostptr, memdesc->size);
448 return -ENOMEM;
449 }
450 }
451
452 return 0;
453}
454
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700455static void kgsl_coherent_free(struct kgsl_memdesc *memdesc)
456{
457 kgsl_driver.stats.coherent -= memdesc->size;
458 dma_free_coherent(NULL, memdesc->size,
459 memdesc->hostptr, memdesc->physaddr);
460}
461
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700462/* Global - also used by kgsl_drm.c */
Harsh Vardhan Dwivedif99c2632012-03-15 14:17:11 -0600463struct kgsl_memdesc_ops kgsl_page_alloc_ops = {
464 .free = kgsl_page_alloc_free,
465 .vmflags = kgsl_page_alloc_vmflags,
466 .vmfault = kgsl_page_alloc_vmfault,
467 .map_kernel_mem = kgsl_page_alloc_map_kernel,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700468};
Harsh Vardhan Dwivedif99c2632012-03-15 14:17:11 -0600469EXPORT_SYMBOL(kgsl_page_alloc_ops);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700470
471static struct kgsl_memdesc_ops kgsl_ebimem_ops = {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700472 .free = kgsl_ebimem_free,
473 .vmflags = kgsl_contiguous_vmflags,
474 .vmfault = kgsl_contiguous_vmfault,
Shubhraprakash Das387b6692012-08-11 18:26:29 -0700475 .map_kernel_mem = kgsl_ebimem_map_kernel,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700476};
477
478static struct kgsl_memdesc_ops kgsl_coherent_ops = {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700479 .free = kgsl_coherent_free,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700480};
481
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700482void kgsl_cache_range_op(struct kgsl_memdesc *memdesc, int op)
483{
484 void *addr = memdesc->hostptr;
485 int size = memdesc->size;
486
487 switch (op) {
488 case KGSL_CACHE_OP_FLUSH:
489 dmac_flush_range(addr, addr + size);
490 break;
491 case KGSL_CACHE_OP_CLEAN:
492 dmac_clean_range(addr, addr + size);
493 break;
494 case KGSL_CACHE_OP_INV:
495 dmac_inv_range(addr, addr + size);
496 break;
497 }
498
Jordan Croused17e9aa2011-10-12 16:57:48 -0600499 outer_cache_range_op_sg(memdesc->sg, memdesc->sglen, op);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700500}
501EXPORT_SYMBOL(kgsl_cache_range_op);
502
503static int
Harsh Vardhan Dwivedif99c2632012-03-15 14:17:11 -0600504_kgsl_sharedmem_page_alloc(struct kgsl_memdesc *memdesc,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700505 struct kgsl_pagetable *pagetable,
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -0600506 size_t size, unsigned int protflags)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700507{
Jordan Crouse89bd3232012-07-02 17:50:15 -0600508 int i, order, ret = 0;
Jordan Croused17e9aa2011-10-12 16:57:48 -0600509 int sglen = PAGE_ALIGN(size) / PAGE_SIZE;
Jordan Crouse89bd3232012-07-02 17:50:15 -0600510 struct page **pages = NULL;
511 pgprot_t page_prot = pgprot_writecombine(PAGE_KERNEL);
512 void *ptr;
Jordan Crouse2151cb92012-08-21 14:02:59 -0600513 struct sysinfo si;
514
515 /*
516 * Get the current memory information to be used in deciding if we
517 * should go ahead with this allocation
518 */
519
520 si_meminfo(&si);
521
522 /*
523 * Limit the size of the allocation to the amount of free memory minus
524 * 32MB. Why 32MB? Because thats the buffer that page_alloc uses and
525 * it just seems like a reasonable limit that won't make the OOM killer
526 * go all serial on us. Of course, if we are down this low all bets
527 * are off but above all do no harm.
528 */
529
530 if (size >= ((si.freeram << PAGE_SHIFT) - SZ_32M))
531 return -ENOMEM;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700532
Jordan Crouse7d3139b2012-05-18 10:05:02 -0600533 /*
534 * Add guard page to the end of the allocation when the
535 * IOMMU is in use.
536 */
537
538 if (kgsl_mmu_get_mmutype() == KGSL_MMU_TYPE_IOMMU)
539 sglen++;
540
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700541 memdesc->size = size;
542 memdesc->pagetable = pagetable;
543 memdesc->priv = KGSL_MEMFLAGS_CACHED;
Harsh Vardhan Dwivedif99c2632012-03-15 14:17:11 -0600544 memdesc->ops = &kgsl_page_alloc_ops;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700545
Jordan Crousea652a072012-04-06 16:26:33 -0600546 memdesc->sg = kgsl_sg_alloc(sglen);
547
Jordan Croused17e9aa2011-10-12 16:57:48 -0600548 if (memdesc->sg == NULL) {
Harsh Vardhan Dwivedif99c2632012-03-15 14:17:11 -0600549 KGSL_CORE_ERR("vmalloc(%d) failed\n",
550 sglen * sizeof(struct scatterlist));
Jordan Croused17e9aa2011-10-12 16:57:48 -0600551 ret = -ENOMEM;
552 goto done;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700553 }
554
Jordan Crouse89bd3232012-07-02 17:50:15 -0600555 /*
556 * Allocate space to store the list of pages to send to vmap.
557 * This is an array of pointers so we can track 1024 pages per page of
558 * allocation which means we can handle up to a 8MB buffer request with
559 * two pages; well within the acceptable limits for using kmalloc.
560 */
561
562 pages = kmalloc(sglen * sizeof(struct page *), GFP_KERNEL);
563
564 if (pages == NULL) {
565 KGSL_CORE_ERR("kmalloc (%d) failed\n",
566 sglen * sizeof(struct page *));
567 ret = -ENOMEM;
568 goto done;
569 }
570
Anshuman Danieecd5202012-02-17 19:52:49 +0530571 kmemleak_not_leak(memdesc->sg);
572
Jordan Croused17e9aa2011-10-12 16:57:48 -0600573 memdesc->sglen = sglen;
574 sg_init_table(memdesc->sg, sglen);
575
Jordan Crouse7d3139b2012-05-18 10:05:02 -0600576 for (i = 0; i < PAGE_ALIGN(size) / PAGE_SIZE; i++) {
Jordan Crouse89bd3232012-07-02 17:50:15 -0600577
578 /*
579 * Don't use GFP_ZERO here because it is faster to memset the
580 * range ourselves (see below)
581 */
582
583 pages[i] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
584 if (pages[i] == NULL) {
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -0600585 ret = -ENOMEM;
586 memdesc->sglen = i;
Jordan Croused17e9aa2011-10-12 16:57:48 -0600587 goto done;
588 }
Jordan Crouse89bd3232012-07-02 17:50:15 -0600589
590 sg_set_page(&memdesc->sg[i], pages[i], PAGE_SIZE, 0);
Jordan Croused17e9aa2011-10-12 16:57:48 -0600591 }
Jordan Crouse7d3139b2012-05-18 10:05:02 -0600592
593 /* ADd the guard page to the end of the sglist */
594
595 if (kgsl_mmu_get_mmutype() == KGSL_MMU_TYPE_IOMMU) {
Jordan Crouse89bd3232012-07-02 17:50:15 -0600596 /*
597 * It doesn't matter if we use GFP_ZERO here, this never
598 * gets mapped, and we only allocate it once in the life
599 * of the system
600 */
601
Jordan Crouse7d3139b2012-05-18 10:05:02 -0600602 if (kgsl_guard_page == NULL)
603 kgsl_guard_page = alloc_page(GFP_KERNEL | __GFP_ZERO |
604 __GFP_HIGHMEM);
605
606 if (kgsl_guard_page != NULL) {
607 sg_set_page(&memdesc->sg[sglen - 1], kgsl_guard_page,
608 PAGE_SIZE, 0);
609 memdesc->flags |= KGSL_MEMDESC_GUARD_PAGE;
610 } else
611 memdesc->sglen--;
612 }
613
Jordan Crouse89bd3232012-07-02 17:50:15 -0600614 /*
615 * All memory that goes to the user has to be zeroed out before it gets
616 * exposed to userspace. This means that the memory has to be mapped in
617 * the kernel, zeroed (memset) and then unmapped. This also means that
618 * the dcache has to be flushed to ensure coherency between the kernel
619 * and user pages. We used to pass __GFP_ZERO to alloc_page which mapped
620 * zeroed and unmaped each individual page, and then we had to turn
621 * around and call flush_dcache_page() on that page to clear the caches.
622 * This was killing us for performance. Instead, we found it is much
623 * faster to allocate the pages without GFP_ZERO, map the entire range,
624 * memset it, flush the range and then unmap - this results in a factor
625 * of 4 improvement for speed for large buffers. There is a small
626 * increase in speed for small buffers, but only on the order of a few
627 * microseconds at best. The only downside is that there needs to be
628 * enough temporary space in vmalloc to accomodate the map. This
629 * shouldn't be a problem, but if it happens, fall back to a much slower
630 * path
631 */
632
633 ptr = vmap(pages, i, VM_IOREMAP, page_prot);
634
635 if (ptr != NULL) {
636 memset(ptr, 0, memdesc->size);
637 dmac_flush_range(ptr, ptr + memdesc->size);
638 vunmap(ptr);
639 } else {
640 int j;
641
642 /* Very, very, very slow path */
643
644 for (j = 0; j < i; j++) {
645 ptr = kmap_atomic(pages[j]);
646 memset(ptr, 0, PAGE_SIZE);
647 dmac_flush_range(ptr, ptr + PAGE_SIZE);
648 kunmap_atomic(ptr);
649 }
650 }
651
Jeremy Gebben7018a212012-04-11 10:23:52 -0600652 outer_cache_range_op_sg(memdesc->sg, memdesc->sglen,
653 KGSL_CACHE_OP_FLUSH);
Jordan Croused17e9aa2011-10-12 16:57:48 -0600654
Jordan Croused17e9aa2011-10-12 16:57:48 -0600655 ret = kgsl_mmu_map(pagetable, memdesc, protflags);
656
657 if (ret)
658 goto done;
659
Harsh Vardhan Dwivedif99c2632012-03-15 14:17:11 -0600660 KGSL_STATS_ADD(size, kgsl_driver.stats.page_alloc,
661 kgsl_driver.stats.page_alloc_max);
Jordan Croused17e9aa2011-10-12 16:57:48 -0600662
663 order = get_order(size);
664
665 if (order < 16)
666 kgsl_driver.stats.histogram[order]++;
667
668done:
Jordan Crouse89bd3232012-07-02 17:50:15 -0600669 kfree(pages);
670
Jordan Croused17e9aa2011-10-12 16:57:48 -0600671 if (ret)
672 kgsl_sharedmem_free(memdesc);
673
674 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700675}
676
677int
Harsh Vardhan Dwivedif99c2632012-03-15 14:17:11 -0600678kgsl_sharedmem_page_alloc(struct kgsl_memdesc *memdesc,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700679 struct kgsl_pagetable *pagetable, size_t size)
680{
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -0600681 int ret = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700682 BUG_ON(size == 0);
683
684 size = ALIGN(size, PAGE_SIZE * 2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700685
Harsh Vardhan Dwivedif99c2632012-03-15 14:17:11 -0600686 ret = _kgsl_sharedmem_page_alloc(memdesc, pagetable, size,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700687 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -0600688 if (!ret)
Harsh Vardhan Dwivedif99c2632012-03-15 14:17:11 -0600689 ret = kgsl_page_alloc_map_kernel(memdesc);
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -0600690 if (ret)
691 kgsl_sharedmem_free(memdesc);
692 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700693}
Harsh Vardhan Dwivedif99c2632012-03-15 14:17:11 -0600694EXPORT_SYMBOL(kgsl_sharedmem_page_alloc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700695
696int
Harsh Vardhan Dwivedif99c2632012-03-15 14:17:11 -0600697kgsl_sharedmem_page_alloc_user(struct kgsl_memdesc *memdesc,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700698 struct kgsl_pagetable *pagetable,
699 size_t size, int flags)
700{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700701 unsigned int protflags;
702
Jordan Crousee752ab22012-08-20 13:26:05 -0600703 if (size == 0)
704 return -EINVAL;
Anshuman Danieecd5202012-02-17 19:52:49 +0530705
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700706 protflags = GSL_PT_PAGE_RV;
707 if (!(flags & KGSL_MEMFLAGS_GPUREADONLY))
708 protflags |= GSL_PT_PAGE_WV;
709
Harsh Vardhan Dwivedif99c2632012-03-15 14:17:11 -0600710 return _kgsl_sharedmem_page_alloc(memdesc, pagetable, size,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700711 protflags);
712}
Harsh Vardhan Dwivedif99c2632012-03-15 14:17:11 -0600713EXPORT_SYMBOL(kgsl_sharedmem_page_alloc_user);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700714
715int
716kgsl_sharedmem_alloc_coherent(struct kgsl_memdesc *memdesc, size_t size)
717{
Jordan Croused17e9aa2011-10-12 16:57:48 -0600718 int result = 0;
719
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700720 size = ALIGN(size, PAGE_SIZE);
721
Jordan Croused17e9aa2011-10-12 16:57:48 -0600722 memdesc->size = size;
723 memdesc->ops = &kgsl_coherent_ops;
724
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700725 memdesc->hostptr = dma_alloc_coherent(NULL, size, &memdesc->physaddr,
726 GFP_KERNEL);
727 if (memdesc->hostptr == NULL) {
728 KGSL_CORE_ERR("dma_alloc_coherent(%d) failed\n", size);
Jordan Croused17e9aa2011-10-12 16:57:48 -0600729 result = -ENOMEM;
730 goto err;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700731 }
732
Jordan Croused17e9aa2011-10-12 16:57:48 -0600733 result = memdesc_sg_phys(memdesc, memdesc->physaddr, size);
734 if (result)
735 goto err;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700736
737 /* Record statistics */
738
739 KGSL_STATS_ADD(size, kgsl_driver.stats.coherent,
740 kgsl_driver.stats.coherent_max);
741
Jordan Croused17e9aa2011-10-12 16:57:48 -0600742err:
743 if (result)
744 kgsl_sharedmem_free(memdesc);
745
746 return result;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700747}
748EXPORT_SYMBOL(kgsl_sharedmem_alloc_coherent);
749
750void kgsl_sharedmem_free(struct kgsl_memdesc *memdesc)
751{
752 if (memdesc == NULL || memdesc->size == 0)
753 return;
754
755 if (memdesc->gpuaddr)
756 kgsl_mmu_unmap(memdesc->pagetable, memdesc);
757
Jordan Croused17e9aa2011-10-12 16:57:48 -0600758 if (memdesc->ops && memdesc->ops->free)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700759 memdesc->ops->free(memdesc);
760
Jordan Crousea652a072012-04-06 16:26:33 -0600761 kgsl_sg_free(memdesc->sg, memdesc->sglen);
Jordan Croused17e9aa2011-10-12 16:57:48 -0600762
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700763 memset(memdesc, 0, sizeof(*memdesc));
764}
765EXPORT_SYMBOL(kgsl_sharedmem_free);
766
767static int
768_kgsl_sharedmem_ebimem(struct kgsl_memdesc *memdesc,
769 struct kgsl_pagetable *pagetable, size_t size)
770{
Jordan Croused17e9aa2011-10-12 16:57:48 -0600771 int result = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700772
Jordan Croused17e9aa2011-10-12 16:57:48 -0600773 memdesc->size = size;
774 memdesc->pagetable = pagetable;
775 memdesc->ops = &kgsl_ebimem_ops;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700776 memdesc->physaddr = allocate_contiguous_ebi_nomap(size, SZ_8K);
777
778 if (memdesc->physaddr == 0) {
779 KGSL_CORE_ERR("allocate_contiguous_ebi_nomap(%d) failed\n",
780 size);
781 return -ENOMEM;
782 }
783
Jordan Croused17e9aa2011-10-12 16:57:48 -0600784 result = memdesc_sg_phys(memdesc, memdesc->physaddr, size);
785
786 if (result)
787 goto err;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700788
789 result = kgsl_mmu_map(pagetable, memdesc,
790 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
791
792 if (result)
Jordan Croused17e9aa2011-10-12 16:57:48 -0600793 goto err;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700794
795 KGSL_STATS_ADD(size, kgsl_driver.stats.coherent,
796 kgsl_driver.stats.coherent_max);
797
Jordan Croused17e9aa2011-10-12 16:57:48 -0600798err:
799 if (result)
800 kgsl_sharedmem_free(memdesc);
801
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700802 return result;
803}
804
805int
806kgsl_sharedmem_ebimem_user(struct kgsl_memdesc *memdesc,
807 struct kgsl_pagetable *pagetable,
808 size_t size, int flags)
809{
810 size = ALIGN(size, PAGE_SIZE);
811 return _kgsl_sharedmem_ebimem(memdesc, pagetable, size);
812}
813EXPORT_SYMBOL(kgsl_sharedmem_ebimem_user);
814
815int
816kgsl_sharedmem_ebimem(struct kgsl_memdesc *memdesc,
817 struct kgsl_pagetable *pagetable, size_t size)
818{
819 int result;
820 size = ALIGN(size, 8192);
821 result = _kgsl_sharedmem_ebimem(memdesc, pagetable, size);
822
823 if (result)
824 return result;
825
826 memdesc->hostptr = ioremap(memdesc->physaddr, size);
827
828 if (memdesc->hostptr == NULL) {
829 KGSL_CORE_ERR("ioremap failed\n");
830 kgsl_sharedmem_free(memdesc);
831 return -ENOMEM;
832 }
833
834 return 0;
835}
836EXPORT_SYMBOL(kgsl_sharedmem_ebimem);
837
838int
839kgsl_sharedmem_readl(const struct kgsl_memdesc *memdesc,
840 uint32_t *dst,
841 unsigned int offsetbytes)
842{
Jeremy Gebbenaba13272012-01-31 17:31:23 -0700843 uint32_t *src;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700844 BUG_ON(memdesc == NULL || memdesc->hostptr == NULL || dst == NULL);
Jeremy Gebbenaba13272012-01-31 17:31:23 -0700845 WARN_ON(offsetbytes % sizeof(uint32_t) != 0);
846 if (offsetbytes % sizeof(uint32_t) != 0)
847 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700848
Jeremy Gebbenaba13272012-01-31 17:31:23 -0700849 WARN_ON(offsetbytes + sizeof(uint32_t) > memdesc->size);
850 if (offsetbytes + sizeof(uint32_t) > memdesc->size)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700851 return -ERANGE;
Jeremy Gebbenaba13272012-01-31 17:31:23 -0700852 src = (uint32_t *)(memdesc->hostptr + offsetbytes);
853 *dst = *src;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700854 return 0;
855}
856EXPORT_SYMBOL(kgsl_sharedmem_readl);
857
858int
859kgsl_sharedmem_writel(const struct kgsl_memdesc *memdesc,
860 unsigned int offsetbytes,
861 uint32_t src)
862{
Jeremy Gebbenaba13272012-01-31 17:31:23 -0700863 uint32_t *dst;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700864 BUG_ON(memdesc == NULL || memdesc->hostptr == NULL);
Jeremy Gebbenaba13272012-01-31 17:31:23 -0700865 WARN_ON(offsetbytes % sizeof(uint32_t) != 0);
866 if (offsetbytes % sizeof(uint32_t) != 0)
867 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700868
Jeremy Gebbenaba13272012-01-31 17:31:23 -0700869 WARN_ON(offsetbytes + sizeof(uint32_t) > memdesc->size);
870 if (offsetbytes + sizeof(uint32_t) > memdesc->size)
871 return -ERANGE;
Jeremy Gebbena3d07a42011-10-17 12:08:16 -0600872 kgsl_cffdump_setmem(memdesc->gpuaddr + offsetbytes,
Jeremy Gebbenaba13272012-01-31 17:31:23 -0700873 src, sizeof(uint32_t));
874 dst = (uint32_t *)(memdesc->hostptr + offsetbytes);
875 *dst = src;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700876 return 0;
877}
878EXPORT_SYMBOL(kgsl_sharedmem_writel);
879
880int
881kgsl_sharedmem_set(const struct kgsl_memdesc *memdesc, unsigned int offsetbytes,
882 unsigned int value, unsigned int sizebytes)
883{
884 BUG_ON(memdesc == NULL || memdesc->hostptr == NULL);
885 BUG_ON(offsetbytes + sizebytes > memdesc->size);
886
Jeremy Gebbena3d07a42011-10-17 12:08:16 -0600887 kgsl_cffdump_setmem(memdesc->gpuaddr + offsetbytes, value,
888 sizebytes);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700889 memset(memdesc->hostptr + offsetbytes, value, sizebytes);
890 return 0;
891}
892EXPORT_SYMBOL(kgsl_sharedmem_set);
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -0600893
894/*
895 * kgsl_sharedmem_map_vma - Map a user vma to physical memory
896 *
897 * @vma - The user vma to map
898 * @memdesc - The memory descriptor which contains information about the
899 * physical memory
900 *
901 * Return: 0 on success else error code
902 */
903int
904kgsl_sharedmem_map_vma(struct vm_area_struct *vma,
905 const struct kgsl_memdesc *memdesc)
906{
907 unsigned long addr = vma->vm_start;
908 unsigned long size = vma->vm_end - vma->vm_start;
909 int ret, i = 0;
910
911 if (!memdesc->sg || (size != memdesc->size) ||
912 (memdesc->sglen != (size / PAGE_SIZE)))
913 return -EINVAL;
914
915 for (; addr < vma->vm_end; addr += PAGE_SIZE, i++) {
916 ret = vm_insert_page(vma, addr, sg_page(&memdesc->sg[i]));
917 if (ret)
918 return ret;
919 }
920 return 0;
921}
922EXPORT_SYMBOL(kgsl_sharedmem_map_vma);