blob: a51f29f2802bd2dbc173a5517de9332f3f6f2d3f [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 */
13#include <linux/vmalloc.h>
14#include <linux/memory_alloc.h>
15#include <asm/cacheflush.h>
Anshuman Danieecd5202012-02-17 19:52:49 +053016#include <linux/slab.h>
17#include <linux/kmemleak.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070018
19#include "kgsl.h"
20#include "kgsl_sharedmem.h"
21#include "kgsl_cffdump.h"
22#include "kgsl_device.h"
23
Jordan Crouse1b897cf2011-10-12 16:57:48 -060024/* An attribute for showing per-process memory statistics */
25struct kgsl_mem_entry_attribute {
26 struct attribute attr;
27 int memtype;
28 ssize_t (*show)(struct kgsl_process_private *priv,
29 int type, char *buf);
30};
31
32#define to_mem_entry_attr(a) \
33container_of(a, struct kgsl_mem_entry_attribute, attr)
34
35#define __MEM_ENTRY_ATTR(_type, _name, _show) \
36{ \
37 .attr = { .name = __stringify(_name), .mode = 0444 }, \
38 .memtype = _type, \
39 .show = _show, \
40}
41
42/*
43 * A structure to hold the attributes for a particular memory type.
44 * For each memory type in each process we store the current and maximum
45 * memory usage and display the counts in sysfs. This structure and
46 * the following macro allow us to simplify the definition for those
47 * adding new memory types
48 */
49
50struct mem_entry_stats {
51 int memtype;
52 struct kgsl_mem_entry_attribute attr;
53 struct kgsl_mem_entry_attribute max_attr;
54};
55
56
57#define MEM_ENTRY_STAT(_type, _name) \
58{ \
59 .memtype = _type, \
60 .attr = __MEM_ENTRY_ATTR(_type, _name, mem_entry_show), \
61 .max_attr = __MEM_ENTRY_ATTR(_type, _name##_max, \
62 mem_entry_max_show), \
63}
64
65
66/**
67 * Given a kobj, find the process structure attached to it
68 */
69
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070070static struct kgsl_process_private *
71_get_priv_from_kobj(struct kobject *kobj)
72{
73 struct kgsl_process_private *private;
74 unsigned long name;
75
76 if (!kobj)
77 return NULL;
78
79 if (sscanf(kobj->name, "%ld", &name) != 1)
80 return NULL;
81
82 list_for_each_entry(private, &kgsl_driver.process_list, list) {
83 if (private->pid == name)
84 return private;
85 }
86
87 return NULL;
88}
89
Jordan Crouse1b897cf2011-10-12 16:57:48 -060090/**
91 * Show the current amount of memory allocated for the given memtype
92 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070093
94static ssize_t
Jordan Crouse1b897cf2011-10-12 16:57:48 -060095mem_entry_show(struct kgsl_process_private *priv, int type, char *buf)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070096{
Jordan Crouse1b897cf2011-10-12 16:57:48 -060097 return snprintf(buf, PAGE_SIZE, "%d\n", priv->stats[type].cur);
98}
99
100/**
101 * Show the maximum memory allocated for the given memtype through the life of
102 * the process
103 */
104
105static ssize_t
106mem_entry_max_show(struct kgsl_process_private *priv, int type, char *buf)
107{
108 return snprintf(buf, PAGE_SIZE, "%d\n", priv->stats[type].max);
109}
110
111
112static void mem_entry_sysfs_release(struct kobject *kobj)
113{
114}
115
116static ssize_t mem_entry_sysfs_show(struct kobject *kobj,
117 struct attribute *attr, char *buf)
118{
119 struct kgsl_mem_entry_attribute *pattr = to_mem_entry_attr(attr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700120 struct kgsl_process_private *priv;
Jordan Crouse1b897cf2011-10-12 16:57:48 -0600121 ssize_t ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700122
123 mutex_lock(&kgsl_driver.process_mutex);
124 priv = _get_priv_from_kobj(kobj);
125
Jordan Crouse1b897cf2011-10-12 16:57:48 -0600126 if (priv && pattr->show)
127 ret = pattr->show(priv, pattr->memtype, buf);
128 else
129 ret = -EIO;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700130
131 mutex_unlock(&kgsl_driver.process_mutex);
Jordan Crouse1b897cf2011-10-12 16:57:48 -0600132 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700133}
134
Jordan Crouse1b897cf2011-10-12 16:57:48 -0600135static const struct sysfs_ops mem_entry_sysfs_ops = {
136 .show = mem_entry_sysfs_show,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700137};
138
Jordan Crouse1b897cf2011-10-12 16:57:48 -0600139static struct kobj_type ktype_mem_entry = {
140 .sysfs_ops = &mem_entry_sysfs_ops,
141 .default_attrs = NULL,
142 .release = mem_entry_sysfs_release
143};
144
145static struct mem_entry_stats mem_stats[] = {
146 MEM_ENTRY_STAT(KGSL_MEM_ENTRY_KERNEL, kernel),
147#ifdef CONFIG_ANDROID_PMEM
148 MEM_ENTRY_STAT(KGSL_MEM_ENTRY_PMEM, pmem),
149#endif
150#ifdef CONFIG_ASHMEM
151 MEM_ENTRY_STAT(KGSL_MEM_ENTRY_ASHMEM, ashmem),
152#endif
153 MEM_ENTRY_STAT(KGSL_MEM_ENTRY_USER, user),
Jordan Crouse8eab35a2011-10-12 16:57:48 -0600154#ifdef CONFIG_ION
Jeremy Gebbenff6eab02012-01-09 09:42:21 -0700155 MEM_ENTRY_STAT(KGSL_MEM_ENTRY_ION, ion),
Jordan Crouse8eab35a2011-10-12 16:57:48 -0600156#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700157};
158
159void
160kgsl_process_uninit_sysfs(struct kgsl_process_private *private)
161{
Jordan Crouse1b897cf2011-10-12 16:57:48 -0600162 int i;
163
164 for (i = 0; i < ARRAY_SIZE(mem_stats); i++) {
165 sysfs_remove_file(&private->kobj, &mem_stats[i].attr.attr);
166 sysfs_remove_file(&private->kobj,
167 &mem_stats[i].max_attr.attr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700168 }
Jordan Crouse1b897cf2011-10-12 16:57:48 -0600169
170 kobject_put(&private->kobj);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700171}
172
173void
174kgsl_process_init_sysfs(struct kgsl_process_private *private)
175{
176 unsigned char name[16];
Jordan Crouse1b897cf2011-10-12 16:57:48 -0600177 int i, ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700178
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700179 snprintf(name, sizeof(name), "%d", private->pid);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700180
Jordan Crouse1b897cf2011-10-12 16:57:48 -0600181 if (kobject_init_and_add(&private->kobj, &ktype_mem_entry,
182 kgsl_driver.prockobj, name))
183 return;
184
185 for (i = 0; i < ARRAY_SIZE(mem_stats); i++) {
186 /* We need to check the value of sysfs_create_file, but we
187 * don't really care if it passed or not */
188
189 ret = sysfs_create_file(&private->kobj,
190 &mem_stats[i].attr.attr);
191 ret = sysfs_create_file(&private->kobj,
192 &mem_stats[i].max_attr.attr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700193 }
194}
195
196static int kgsl_drv_memstat_show(struct device *dev,
197 struct device_attribute *attr,
198 char *buf)
199{
200 unsigned int val = 0;
201
202 if (!strncmp(attr->attr.name, "vmalloc", 7))
203 val = kgsl_driver.stats.vmalloc;
204 else if (!strncmp(attr->attr.name, "vmalloc_max", 11))
205 val = kgsl_driver.stats.vmalloc_max;
Harsh Vardhan Dwivedif99c2632012-03-15 14:17:11 -0600206 else if (!strncmp(attr->attr.name, "page_alloc", 10))
207 val = kgsl_driver.stats.page_alloc;
208 else if (!strncmp(attr->attr.name, "page_alloc_max", 14))
209 val = kgsl_driver.stats.page_alloc_max;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700210 else if (!strncmp(attr->attr.name, "coherent", 8))
211 val = kgsl_driver.stats.coherent;
212 else if (!strncmp(attr->attr.name, "coherent_max", 12))
213 val = kgsl_driver.stats.coherent_max;
214 else if (!strncmp(attr->attr.name, "mapped", 6))
215 val = kgsl_driver.stats.mapped;
216 else if (!strncmp(attr->attr.name, "mapped_max", 10))
217 val = kgsl_driver.stats.mapped_max;
218
219 return snprintf(buf, PAGE_SIZE, "%u\n", val);
220}
221
222static int kgsl_drv_histogram_show(struct device *dev,
223 struct device_attribute *attr,
224 char *buf)
225{
226 int len = 0;
227 int i;
228
229 for (i = 0; i < 16; i++)
Jeremy Gebbena87bb862011-08-08 16:09:38 -0600230 len += snprintf(buf + len, PAGE_SIZE - len, "%d ",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700231 kgsl_driver.stats.histogram[i]);
232
Jeremy Gebbena87bb862011-08-08 16:09:38 -0600233 len += snprintf(buf + len, PAGE_SIZE - len, "\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700234 return len;
235}
236
237DEVICE_ATTR(vmalloc, 0444, kgsl_drv_memstat_show, NULL);
238DEVICE_ATTR(vmalloc_max, 0444, kgsl_drv_memstat_show, NULL);
Harsh Vardhan Dwivedif99c2632012-03-15 14:17:11 -0600239DEVICE_ATTR(page_alloc, 0444, kgsl_drv_memstat_show, NULL);
240DEVICE_ATTR(page_alloc_max, 0444, kgsl_drv_memstat_show, NULL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700241DEVICE_ATTR(coherent, 0444, kgsl_drv_memstat_show, NULL);
242DEVICE_ATTR(coherent_max, 0444, kgsl_drv_memstat_show, NULL);
243DEVICE_ATTR(mapped, 0444, kgsl_drv_memstat_show, NULL);
244DEVICE_ATTR(mapped_max, 0444, kgsl_drv_memstat_show, NULL);
245DEVICE_ATTR(histogram, 0444, kgsl_drv_histogram_show, NULL);
246
247static const struct device_attribute *drv_attr_list[] = {
248 &dev_attr_vmalloc,
249 &dev_attr_vmalloc_max,
Harsh Vardhan Dwivedif99c2632012-03-15 14:17:11 -0600250 &dev_attr_page_alloc,
251 &dev_attr_page_alloc_max,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700252 &dev_attr_coherent,
253 &dev_attr_coherent_max,
254 &dev_attr_mapped,
255 &dev_attr_mapped_max,
256 &dev_attr_histogram,
257 NULL
258};
259
260void
261kgsl_sharedmem_uninit_sysfs(void)
262{
263 kgsl_remove_device_sysfs_files(&kgsl_driver.virtdev, drv_attr_list);
264}
265
266int
267kgsl_sharedmem_init_sysfs(void)
268{
269 return kgsl_create_device_sysfs_files(&kgsl_driver.virtdev,
270 drv_attr_list);
271}
272
273#ifdef CONFIG_OUTER_CACHE
274static void _outer_cache_range_op(int op, unsigned long addr, size_t size)
275{
276 switch (op) {
277 case KGSL_CACHE_OP_FLUSH:
278 outer_flush_range(addr, addr + size);
279 break;
280 case KGSL_CACHE_OP_CLEAN:
281 outer_clean_range(addr, addr + size);
282 break;
283 case KGSL_CACHE_OP_INV:
284 outer_inv_range(addr, addr + size);
285 break;
286 }
287}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700288
Jordan Croused17e9aa2011-10-12 16:57:48 -0600289static void outer_cache_range_op_sg(struct scatterlist *sg, int sglen, int op)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700290{
Jordan Croused17e9aa2011-10-12 16:57:48 -0600291 struct scatterlist *s;
292 int i;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700293
Jordan Croused17e9aa2011-10-12 16:57:48 -0600294 for_each_sg(sg, s, sglen, i) {
Jeremy Gebben582fe312012-03-23 10:19:44 -0600295 unsigned int paddr = kgsl_get_sg_pa(s);
Jordan Croused17e9aa2011-10-12 16:57:48 -0600296 _outer_cache_range_op(op, paddr, s->length);
297 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700298}
299
Jordan Croused17e9aa2011-10-12 16:57:48 -0600300#else
301static void outer_cache_range_op_sg(struct scatterlist *sg, int sglen, int op)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700302{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700303}
304#endif
305
Harsh Vardhan Dwivedif99c2632012-03-15 14:17:11 -0600306static int kgsl_page_alloc_vmfault(struct kgsl_memdesc *memdesc,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700307 struct vm_area_struct *vma,
308 struct vm_fault *vmf)
309{
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -0600310 unsigned long offset;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700311 struct page *page;
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -0600312 int i;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700313
314 offset = (unsigned long) vmf->virtual_address - vma->vm_start;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700315
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -0600316 i = offset >> PAGE_SHIFT;
317 page = sg_page(&memdesc->sg[i]);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700318 if (page == NULL)
319 return VM_FAULT_SIGBUS;
320
321 get_page(page);
322
323 vmf->page = page;
324 return 0;
325}
326
Harsh Vardhan Dwivedif99c2632012-03-15 14:17:11 -0600327static int kgsl_page_alloc_vmflags(struct kgsl_memdesc *memdesc)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700328{
329 return VM_RESERVED | VM_DONTEXPAND;
330}
331
Harsh Vardhan Dwivedif99c2632012-03-15 14:17:11 -0600332static void kgsl_page_alloc_free(struct kgsl_memdesc *memdesc)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700333{
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -0600334 int i = 0;
335 struct scatterlist *sg;
Harsh Vardhan Dwivedif99c2632012-03-15 14:17:11 -0600336 kgsl_driver.stats.page_alloc -= memdesc->size;
337 if (memdesc->hostptr) {
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -0600338 vunmap(memdesc->hostptr);
Harsh Vardhan Dwivedif99c2632012-03-15 14:17:11 -0600339 kgsl_driver.stats.vmalloc -= memdesc->size;
340 }
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -0600341 if (memdesc->sg)
342 for_each_sg(memdesc->sg, sg, memdesc->sglen, i)
343 __free_page(sg_page(sg));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700344}
345
346static int kgsl_contiguous_vmflags(struct kgsl_memdesc *memdesc)
347{
348 return VM_RESERVED | VM_IO | VM_PFNMAP | VM_DONTEXPAND;
349}
350
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -0600351/*
Harsh Vardhan Dwivedif99c2632012-03-15 14:17:11 -0600352 * kgsl_page_alloc_map_kernel - Map the memory in memdesc to kernel address
353 * space
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -0600354 *
355 * @memdesc - The memory descriptor which contains information about the memory
356 *
357 * Return: 0 on success else error code
358 */
Harsh Vardhan Dwivedif99c2632012-03-15 14:17:11 -0600359static int kgsl_page_alloc_map_kernel(struct kgsl_memdesc *memdesc)
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -0600360{
361 if (!memdesc->hostptr) {
362 pgprot_t page_prot = pgprot_writecombine(PAGE_KERNEL);
363 struct page **pages = NULL;
364 struct scatterlist *sg;
365 int i;
366 /* create a list of pages to call vmap */
367 pages = vmalloc(memdesc->sglen * sizeof(struct page *));
368 if (!pages) {
369 KGSL_CORE_ERR("vmalloc(%d) failed\n",
370 memdesc->sglen * sizeof(struct page *));
371 return -ENOMEM;
372 }
373 for_each_sg(memdesc->sg, sg, memdesc->sglen, i)
374 pages[i] = sg_page(sg);
375 memdesc->hostptr = vmap(pages, memdesc->sglen,
376 VM_IOREMAP, page_prot);
Harsh Vardhan Dwivedif99c2632012-03-15 14:17:11 -0600377 KGSL_STATS_ADD(memdesc->size, kgsl_driver.stats.vmalloc,
378 kgsl_driver.stats.vmalloc_max);
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -0600379 vfree(pages);
380 }
381 if (!memdesc->hostptr)
382 return -ENOMEM;
383
384 return 0;
385}
386
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700387static int kgsl_contiguous_vmfault(struct kgsl_memdesc *memdesc,
388 struct vm_area_struct *vma,
389 struct vm_fault *vmf)
390{
391 unsigned long offset, pfn;
392 int ret;
393
394 offset = ((unsigned long) vmf->virtual_address - vma->vm_start) >>
395 PAGE_SHIFT;
396
397 pfn = (memdesc->physaddr >> PAGE_SHIFT) + offset;
398 ret = vm_insert_pfn(vma, (unsigned long) vmf->virtual_address, pfn);
399
400 if (ret == -ENOMEM || ret == -EAGAIN)
401 return VM_FAULT_OOM;
402 else if (ret == -EFAULT)
403 return VM_FAULT_SIGBUS;
404
405 return VM_FAULT_NOPAGE;
406}
407
408static void kgsl_ebimem_free(struct kgsl_memdesc *memdesc)
409
410{
411 kgsl_driver.stats.coherent -= memdesc->size;
412 if (memdesc->hostptr)
413 iounmap(memdesc->hostptr);
414
415 free_contiguous_memory_by_paddr(memdesc->physaddr);
416}
417
418static void kgsl_coherent_free(struct kgsl_memdesc *memdesc)
419{
420 kgsl_driver.stats.coherent -= memdesc->size;
421 dma_free_coherent(NULL, memdesc->size,
422 memdesc->hostptr, memdesc->physaddr);
423}
424
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700425/* Global - also used by kgsl_drm.c */
Harsh Vardhan Dwivedif99c2632012-03-15 14:17:11 -0600426struct kgsl_memdesc_ops kgsl_page_alloc_ops = {
427 .free = kgsl_page_alloc_free,
428 .vmflags = kgsl_page_alloc_vmflags,
429 .vmfault = kgsl_page_alloc_vmfault,
430 .map_kernel_mem = kgsl_page_alloc_map_kernel,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700431};
Harsh Vardhan Dwivedif99c2632012-03-15 14:17:11 -0600432EXPORT_SYMBOL(kgsl_page_alloc_ops);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700433
434static struct kgsl_memdesc_ops kgsl_ebimem_ops = {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700435 .free = kgsl_ebimem_free,
436 .vmflags = kgsl_contiguous_vmflags,
437 .vmfault = kgsl_contiguous_vmfault,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700438};
439
440static struct kgsl_memdesc_ops kgsl_coherent_ops = {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700441 .free = kgsl_coherent_free,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700442};
443
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700444void kgsl_cache_range_op(struct kgsl_memdesc *memdesc, int op)
445{
446 void *addr = memdesc->hostptr;
447 int size = memdesc->size;
448
449 switch (op) {
450 case KGSL_CACHE_OP_FLUSH:
451 dmac_flush_range(addr, addr + size);
452 break;
453 case KGSL_CACHE_OP_CLEAN:
454 dmac_clean_range(addr, addr + size);
455 break;
456 case KGSL_CACHE_OP_INV:
457 dmac_inv_range(addr, addr + size);
458 break;
459 }
460
Jordan Croused17e9aa2011-10-12 16:57:48 -0600461 outer_cache_range_op_sg(memdesc->sg, memdesc->sglen, op);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700462}
463EXPORT_SYMBOL(kgsl_cache_range_op);
464
465static int
Harsh Vardhan Dwivedif99c2632012-03-15 14:17:11 -0600466_kgsl_sharedmem_page_alloc(struct kgsl_memdesc *memdesc,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700467 struct kgsl_pagetable *pagetable,
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -0600468 size_t size, unsigned int protflags)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700469{
Jordan Croused17e9aa2011-10-12 16:57:48 -0600470 int order, ret = 0;
471 int sglen = PAGE_ALIGN(size) / PAGE_SIZE;
472 int i;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700473
474 memdesc->size = size;
475 memdesc->pagetable = pagetable;
476 memdesc->priv = KGSL_MEMFLAGS_CACHED;
Harsh Vardhan Dwivedif99c2632012-03-15 14:17:11 -0600477 memdesc->ops = &kgsl_page_alloc_ops;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700478
Jordan Crousea652a072012-04-06 16:26:33 -0600479 memdesc->sg = kgsl_sg_alloc(sglen);
480
Jordan Croused17e9aa2011-10-12 16:57:48 -0600481 if (memdesc->sg == NULL) {
Harsh Vardhan Dwivedif99c2632012-03-15 14:17:11 -0600482 KGSL_CORE_ERR("vmalloc(%d) failed\n",
483 sglen * sizeof(struct scatterlist));
Jordan Croused17e9aa2011-10-12 16:57:48 -0600484 ret = -ENOMEM;
485 goto done;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700486 }
487
Anshuman Danieecd5202012-02-17 19:52:49 +0530488 kmemleak_not_leak(memdesc->sg);
489
Jordan Croused17e9aa2011-10-12 16:57:48 -0600490 memdesc->sglen = sglen;
491 sg_init_table(memdesc->sg, sglen);
492
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -0600493 for (i = 0; i < memdesc->sglen; i++) {
494 struct page *page = alloc_page(GFP_KERNEL | __GFP_ZERO |
495 __GFP_HIGHMEM);
Jordan Croused17e9aa2011-10-12 16:57:48 -0600496 if (!page) {
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -0600497 ret = -ENOMEM;
498 memdesc->sglen = i;
Jordan Croused17e9aa2011-10-12 16:57:48 -0600499 goto done;
500 }
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -0600501 flush_dcache_page(page);
Jordan Croused17e9aa2011-10-12 16:57:48 -0600502 sg_set_page(&memdesc->sg[i], page, PAGE_SIZE, 0);
503 }
Jeremy Gebben7018a212012-04-11 10:23:52 -0600504 outer_cache_range_op_sg(memdesc->sg, memdesc->sglen,
505 KGSL_CACHE_OP_FLUSH);
Jordan Croused17e9aa2011-10-12 16:57:48 -0600506
Jordan Croused17e9aa2011-10-12 16:57:48 -0600507 ret = kgsl_mmu_map(pagetable, memdesc, protflags);
508
509 if (ret)
510 goto done;
511
Harsh Vardhan Dwivedif99c2632012-03-15 14:17:11 -0600512 KGSL_STATS_ADD(size, kgsl_driver.stats.page_alloc,
513 kgsl_driver.stats.page_alloc_max);
Jordan Croused17e9aa2011-10-12 16:57:48 -0600514
515 order = get_order(size);
516
517 if (order < 16)
518 kgsl_driver.stats.histogram[order]++;
519
520done:
521 if (ret)
522 kgsl_sharedmem_free(memdesc);
523
524 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700525}
526
527int
Harsh Vardhan Dwivedif99c2632012-03-15 14:17:11 -0600528kgsl_sharedmem_page_alloc(struct kgsl_memdesc *memdesc,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700529 struct kgsl_pagetable *pagetable, size_t size)
530{
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -0600531 int ret = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700532 BUG_ON(size == 0);
533
534 size = ALIGN(size, PAGE_SIZE * 2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700535
Harsh Vardhan Dwivedif99c2632012-03-15 14:17:11 -0600536 ret = _kgsl_sharedmem_page_alloc(memdesc, pagetable, size,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700537 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -0600538 if (!ret)
Harsh Vardhan Dwivedif99c2632012-03-15 14:17:11 -0600539 ret = kgsl_page_alloc_map_kernel(memdesc);
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -0600540 if (ret)
541 kgsl_sharedmem_free(memdesc);
542 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700543}
Harsh Vardhan Dwivedif99c2632012-03-15 14:17:11 -0600544EXPORT_SYMBOL(kgsl_sharedmem_page_alloc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700545
546int
Harsh Vardhan Dwivedif99c2632012-03-15 14:17:11 -0600547kgsl_sharedmem_page_alloc_user(struct kgsl_memdesc *memdesc,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700548 struct kgsl_pagetable *pagetable,
549 size_t size, int flags)
550{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700551 unsigned int protflags;
552
553 BUG_ON(size == 0);
Anshuman Danieecd5202012-02-17 19:52:49 +0530554
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700555 protflags = GSL_PT_PAGE_RV;
556 if (!(flags & KGSL_MEMFLAGS_GPUREADONLY))
557 protflags |= GSL_PT_PAGE_WV;
558
Harsh Vardhan Dwivedif99c2632012-03-15 14:17:11 -0600559 return _kgsl_sharedmem_page_alloc(memdesc, pagetable, size,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700560 protflags);
561}
Harsh Vardhan Dwivedif99c2632012-03-15 14:17:11 -0600562EXPORT_SYMBOL(kgsl_sharedmem_page_alloc_user);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700563
564int
565kgsl_sharedmem_alloc_coherent(struct kgsl_memdesc *memdesc, size_t size)
566{
Jordan Croused17e9aa2011-10-12 16:57:48 -0600567 int result = 0;
568
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700569 size = ALIGN(size, PAGE_SIZE);
570
Jordan Croused17e9aa2011-10-12 16:57:48 -0600571 memdesc->size = size;
572 memdesc->ops = &kgsl_coherent_ops;
573
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700574 memdesc->hostptr = dma_alloc_coherent(NULL, size, &memdesc->physaddr,
575 GFP_KERNEL);
576 if (memdesc->hostptr == NULL) {
577 KGSL_CORE_ERR("dma_alloc_coherent(%d) failed\n", size);
Jordan Croused17e9aa2011-10-12 16:57:48 -0600578 result = -ENOMEM;
579 goto err;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700580 }
581
Jordan Croused17e9aa2011-10-12 16:57:48 -0600582 result = memdesc_sg_phys(memdesc, memdesc->physaddr, size);
583 if (result)
584 goto err;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700585
586 /* Record statistics */
587
588 KGSL_STATS_ADD(size, kgsl_driver.stats.coherent,
589 kgsl_driver.stats.coherent_max);
590
Jordan Croused17e9aa2011-10-12 16:57:48 -0600591err:
592 if (result)
593 kgsl_sharedmem_free(memdesc);
594
595 return result;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700596}
597EXPORT_SYMBOL(kgsl_sharedmem_alloc_coherent);
598
599void kgsl_sharedmem_free(struct kgsl_memdesc *memdesc)
600{
601 if (memdesc == NULL || memdesc->size == 0)
602 return;
603
604 if (memdesc->gpuaddr)
605 kgsl_mmu_unmap(memdesc->pagetable, memdesc);
606
Jordan Croused17e9aa2011-10-12 16:57:48 -0600607 if (memdesc->ops && memdesc->ops->free)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700608 memdesc->ops->free(memdesc);
609
Jordan Crousea652a072012-04-06 16:26:33 -0600610 kgsl_sg_free(memdesc->sg, memdesc->sglen);
Jordan Croused17e9aa2011-10-12 16:57:48 -0600611
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700612 memset(memdesc, 0, sizeof(*memdesc));
613}
614EXPORT_SYMBOL(kgsl_sharedmem_free);
615
616static int
617_kgsl_sharedmem_ebimem(struct kgsl_memdesc *memdesc,
618 struct kgsl_pagetable *pagetable, size_t size)
619{
Jordan Croused17e9aa2011-10-12 16:57:48 -0600620 int result = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700621
Jordan Croused17e9aa2011-10-12 16:57:48 -0600622 memdesc->size = size;
623 memdesc->pagetable = pagetable;
624 memdesc->ops = &kgsl_ebimem_ops;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700625 memdesc->physaddr = allocate_contiguous_ebi_nomap(size, SZ_8K);
626
627 if (memdesc->physaddr == 0) {
628 KGSL_CORE_ERR("allocate_contiguous_ebi_nomap(%d) failed\n",
629 size);
630 return -ENOMEM;
631 }
632
Jordan Croused17e9aa2011-10-12 16:57:48 -0600633 result = memdesc_sg_phys(memdesc, memdesc->physaddr, size);
634
635 if (result)
636 goto err;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700637
638 result = kgsl_mmu_map(pagetable, memdesc,
639 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
640
641 if (result)
Jordan Croused17e9aa2011-10-12 16:57:48 -0600642 goto err;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700643
644 KGSL_STATS_ADD(size, kgsl_driver.stats.coherent,
645 kgsl_driver.stats.coherent_max);
646
Jordan Croused17e9aa2011-10-12 16:57:48 -0600647err:
648 if (result)
649 kgsl_sharedmem_free(memdesc);
650
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700651 return result;
652}
653
654int
655kgsl_sharedmem_ebimem_user(struct kgsl_memdesc *memdesc,
656 struct kgsl_pagetable *pagetable,
657 size_t size, int flags)
658{
659 size = ALIGN(size, PAGE_SIZE);
660 return _kgsl_sharedmem_ebimem(memdesc, pagetable, size);
661}
662EXPORT_SYMBOL(kgsl_sharedmem_ebimem_user);
663
664int
665kgsl_sharedmem_ebimem(struct kgsl_memdesc *memdesc,
666 struct kgsl_pagetable *pagetable, size_t size)
667{
668 int result;
669 size = ALIGN(size, 8192);
670 result = _kgsl_sharedmem_ebimem(memdesc, pagetable, size);
671
672 if (result)
673 return result;
674
675 memdesc->hostptr = ioremap(memdesc->physaddr, size);
676
677 if (memdesc->hostptr == NULL) {
678 KGSL_CORE_ERR("ioremap failed\n");
679 kgsl_sharedmem_free(memdesc);
680 return -ENOMEM;
681 }
682
683 return 0;
684}
685EXPORT_SYMBOL(kgsl_sharedmem_ebimem);
686
687int
688kgsl_sharedmem_readl(const struct kgsl_memdesc *memdesc,
689 uint32_t *dst,
690 unsigned int offsetbytes)
691{
Jeremy Gebbenaba13272012-01-31 17:31:23 -0700692 uint32_t *src;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700693 BUG_ON(memdesc == NULL || memdesc->hostptr == NULL || dst == NULL);
Jeremy Gebbenaba13272012-01-31 17:31:23 -0700694 WARN_ON(offsetbytes % sizeof(uint32_t) != 0);
695 if (offsetbytes % sizeof(uint32_t) != 0)
696 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700697
Jeremy Gebbenaba13272012-01-31 17:31:23 -0700698 WARN_ON(offsetbytes + sizeof(uint32_t) > memdesc->size);
699 if (offsetbytes + sizeof(uint32_t) > memdesc->size)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700700 return -ERANGE;
Jeremy Gebbenaba13272012-01-31 17:31:23 -0700701 src = (uint32_t *)(memdesc->hostptr + offsetbytes);
702 *dst = *src;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700703 return 0;
704}
705EXPORT_SYMBOL(kgsl_sharedmem_readl);
706
707int
708kgsl_sharedmem_writel(const struct kgsl_memdesc *memdesc,
709 unsigned int offsetbytes,
710 uint32_t src)
711{
Jeremy Gebbenaba13272012-01-31 17:31:23 -0700712 uint32_t *dst;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700713 BUG_ON(memdesc == NULL || memdesc->hostptr == NULL);
Jeremy Gebbenaba13272012-01-31 17:31:23 -0700714 WARN_ON(offsetbytes % sizeof(uint32_t) != 0);
715 if (offsetbytes % sizeof(uint32_t) != 0)
716 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700717
Jeremy Gebbenaba13272012-01-31 17:31:23 -0700718 WARN_ON(offsetbytes + sizeof(uint32_t) > memdesc->size);
719 if (offsetbytes + sizeof(uint32_t) > memdesc->size)
720 return -ERANGE;
Jeremy Gebbena3d07a42011-10-17 12:08:16 -0600721 kgsl_cffdump_setmem(memdesc->gpuaddr + offsetbytes,
Jeremy Gebbenaba13272012-01-31 17:31:23 -0700722 src, sizeof(uint32_t));
723 dst = (uint32_t *)(memdesc->hostptr + offsetbytes);
724 *dst = src;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700725 return 0;
726}
727EXPORT_SYMBOL(kgsl_sharedmem_writel);
728
729int
730kgsl_sharedmem_set(const struct kgsl_memdesc *memdesc, unsigned int offsetbytes,
731 unsigned int value, unsigned int sizebytes)
732{
733 BUG_ON(memdesc == NULL || memdesc->hostptr == NULL);
734 BUG_ON(offsetbytes + sizebytes > memdesc->size);
735
Jeremy Gebbena3d07a42011-10-17 12:08:16 -0600736 kgsl_cffdump_setmem(memdesc->gpuaddr + offsetbytes, value,
737 sizebytes);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700738 memset(memdesc->hostptr + offsetbytes, value, sizebytes);
739 return 0;
740}
741EXPORT_SYMBOL(kgsl_sharedmem_set);
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -0600742
743/*
744 * kgsl_sharedmem_map_vma - Map a user vma to physical memory
745 *
746 * @vma - The user vma to map
747 * @memdesc - The memory descriptor which contains information about the
748 * physical memory
749 *
750 * Return: 0 on success else error code
751 */
752int
753kgsl_sharedmem_map_vma(struct vm_area_struct *vma,
754 const struct kgsl_memdesc *memdesc)
755{
756 unsigned long addr = vma->vm_start;
757 unsigned long size = vma->vm_end - vma->vm_start;
758 int ret, i = 0;
759
760 if (!memdesc->sg || (size != memdesc->size) ||
761 (memdesc->sglen != (size / PAGE_SIZE)))
762 return -EINVAL;
763
764 for (; addr < vma->vm_end; addr += PAGE_SIZE, i++) {
765 ret = vm_insert_page(vma, addr, sg_page(&memdesc->sg[i]));
766 if (ret)
767 return ret;
768 }
769 return 0;
770}
771EXPORT_SYMBOL(kgsl_sharedmem_map_vma);