blob: b341485c6a22544746ebf027197ea5aaf322a5d9 [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 */
Jordan Crouse7d3139b2012-05-18 10:05:02 -0600390 pages = vmalloc(sglen * sizeof(struct page *));
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -0600391 if (!pages) {
392 KGSL_CORE_ERR("vmalloc(%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);
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -0600402 vfree(pages);
403 }
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
441static void kgsl_coherent_free(struct kgsl_memdesc *memdesc)
442{
443 kgsl_driver.stats.coherent -= memdesc->size;
444 dma_free_coherent(NULL, memdesc->size,
445 memdesc->hostptr, memdesc->physaddr);
446}
447
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700448/* Global - also used by kgsl_drm.c */
Harsh Vardhan Dwivedif99c2632012-03-15 14:17:11 -0600449struct kgsl_memdesc_ops kgsl_page_alloc_ops = {
450 .free = kgsl_page_alloc_free,
451 .vmflags = kgsl_page_alloc_vmflags,
452 .vmfault = kgsl_page_alloc_vmfault,
453 .map_kernel_mem = kgsl_page_alloc_map_kernel,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700454};
Harsh Vardhan Dwivedif99c2632012-03-15 14:17:11 -0600455EXPORT_SYMBOL(kgsl_page_alloc_ops);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700456
457static struct kgsl_memdesc_ops kgsl_ebimem_ops = {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700458 .free = kgsl_ebimem_free,
459 .vmflags = kgsl_contiguous_vmflags,
460 .vmfault = kgsl_contiguous_vmfault,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700461};
462
463static struct kgsl_memdesc_ops kgsl_coherent_ops = {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700464 .free = kgsl_coherent_free,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700465};
466
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700467void kgsl_cache_range_op(struct kgsl_memdesc *memdesc, int op)
468{
469 void *addr = memdesc->hostptr;
470 int size = memdesc->size;
471
472 switch (op) {
473 case KGSL_CACHE_OP_FLUSH:
474 dmac_flush_range(addr, addr + size);
475 break;
476 case KGSL_CACHE_OP_CLEAN:
477 dmac_clean_range(addr, addr + size);
478 break;
479 case KGSL_CACHE_OP_INV:
480 dmac_inv_range(addr, addr + size);
481 break;
482 }
483
Jordan Croused17e9aa2011-10-12 16:57:48 -0600484 outer_cache_range_op_sg(memdesc->sg, memdesc->sglen, op);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700485}
486EXPORT_SYMBOL(kgsl_cache_range_op);
487
488static int
Harsh Vardhan Dwivedif99c2632012-03-15 14:17:11 -0600489_kgsl_sharedmem_page_alloc(struct kgsl_memdesc *memdesc,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700490 struct kgsl_pagetable *pagetable,
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -0600491 size_t size, unsigned int protflags)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700492{
Jordan Crouse89bd3232012-07-02 17:50:15 -0600493 int i, order, ret = 0;
Jordan Croused17e9aa2011-10-12 16:57:48 -0600494 int sglen = PAGE_ALIGN(size) / PAGE_SIZE;
Jordan Crouse89bd3232012-07-02 17:50:15 -0600495 struct page **pages = NULL;
496 pgprot_t page_prot = pgprot_writecombine(PAGE_KERNEL);
497 void *ptr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700498
Jordan Crouse7d3139b2012-05-18 10:05:02 -0600499 /*
500 * Add guard page to the end of the allocation when the
501 * IOMMU is in use.
502 */
503
504 if (kgsl_mmu_get_mmutype() == KGSL_MMU_TYPE_IOMMU)
505 sglen++;
506
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700507 memdesc->size = size;
508 memdesc->pagetable = pagetable;
509 memdesc->priv = KGSL_MEMFLAGS_CACHED;
Harsh Vardhan Dwivedif99c2632012-03-15 14:17:11 -0600510 memdesc->ops = &kgsl_page_alloc_ops;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700511
Jordan Crousea652a072012-04-06 16:26:33 -0600512 memdesc->sg = kgsl_sg_alloc(sglen);
513
Jordan Croused17e9aa2011-10-12 16:57:48 -0600514 if (memdesc->sg == NULL) {
Harsh Vardhan Dwivedif99c2632012-03-15 14:17:11 -0600515 KGSL_CORE_ERR("vmalloc(%d) failed\n",
516 sglen * sizeof(struct scatterlist));
Jordan Croused17e9aa2011-10-12 16:57:48 -0600517 ret = -ENOMEM;
518 goto done;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700519 }
520
Jordan Crouse89bd3232012-07-02 17:50:15 -0600521 /*
522 * Allocate space to store the list of pages to send to vmap.
523 * This is an array of pointers so we can track 1024 pages per page of
524 * allocation which means we can handle up to a 8MB buffer request with
525 * two pages; well within the acceptable limits for using kmalloc.
526 */
527
528 pages = kmalloc(sglen * sizeof(struct page *), GFP_KERNEL);
529
530 if (pages == NULL) {
531 KGSL_CORE_ERR("kmalloc (%d) failed\n",
532 sglen * sizeof(struct page *));
533 ret = -ENOMEM;
534 goto done;
535 }
536
Anshuman Danieecd5202012-02-17 19:52:49 +0530537 kmemleak_not_leak(memdesc->sg);
538
Jordan Croused17e9aa2011-10-12 16:57:48 -0600539 memdesc->sglen = sglen;
540 sg_init_table(memdesc->sg, sglen);
541
Jordan Crouse7d3139b2012-05-18 10:05:02 -0600542 for (i = 0; i < PAGE_ALIGN(size) / PAGE_SIZE; i++) {
Jordan Crouse89bd3232012-07-02 17:50:15 -0600543
544 /*
545 * Don't use GFP_ZERO here because it is faster to memset the
546 * range ourselves (see below)
547 */
548
549 pages[i] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
550 if (pages[i] == NULL) {
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -0600551 ret = -ENOMEM;
552 memdesc->sglen = i;
Jordan Croused17e9aa2011-10-12 16:57:48 -0600553 goto done;
554 }
Jordan Crouse89bd3232012-07-02 17:50:15 -0600555
556 sg_set_page(&memdesc->sg[i], pages[i], PAGE_SIZE, 0);
Jordan Croused17e9aa2011-10-12 16:57:48 -0600557 }
Jordan Crouse7d3139b2012-05-18 10:05:02 -0600558
559 /* ADd the guard page to the end of the sglist */
560
561 if (kgsl_mmu_get_mmutype() == KGSL_MMU_TYPE_IOMMU) {
Jordan Crouse89bd3232012-07-02 17:50:15 -0600562 /*
563 * It doesn't matter if we use GFP_ZERO here, this never
564 * gets mapped, and we only allocate it once in the life
565 * of the system
566 */
567
Jordan Crouse7d3139b2012-05-18 10:05:02 -0600568 if (kgsl_guard_page == NULL)
569 kgsl_guard_page = alloc_page(GFP_KERNEL | __GFP_ZERO |
570 __GFP_HIGHMEM);
571
572 if (kgsl_guard_page != NULL) {
573 sg_set_page(&memdesc->sg[sglen - 1], kgsl_guard_page,
574 PAGE_SIZE, 0);
575 memdesc->flags |= KGSL_MEMDESC_GUARD_PAGE;
576 } else
577 memdesc->sglen--;
578 }
579
Jordan Crouse89bd3232012-07-02 17:50:15 -0600580 /*
581 * All memory that goes to the user has to be zeroed out before it gets
582 * exposed to userspace. This means that the memory has to be mapped in
583 * the kernel, zeroed (memset) and then unmapped. This also means that
584 * the dcache has to be flushed to ensure coherency between the kernel
585 * and user pages. We used to pass __GFP_ZERO to alloc_page which mapped
586 * zeroed and unmaped each individual page, and then we had to turn
587 * around and call flush_dcache_page() on that page to clear the caches.
588 * This was killing us for performance. Instead, we found it is much
589 * faster to allocate the pages without GFP_ZERO, map the entire range,
590 * memset it, flush the range and then unmap - this results in a factor
591 * of 4 improvement for speed for large buffers. There is a small
592 * increase in speed for small buffers, but only on the order of a few
593 * microseconds at best. The only downside is that there needs to be
594 * enough temporary space in vmalloc to accomodate the map. This
595 * shouldn't be a problem, but if it happens, fall back to a much slower
596 * path
597 */
598
599 ptr = vmap(pages, i, VM_IOREMAP, page_prot);
600
601 if (ptr != NULL) {
602 memset(ptr, 0, memdesc->size);
603 dmac_flush_range(ptr, ptr + memdesc->size);
604 vunmap(ptr);
605 } else {
606 int j;
607
608 /* Very, very, very slow path */
609
610 for (j = 0; j < i; j++) {
611 ptr = kmap_atomic(pages[j]);
612 memset(ptr, 0, PAGE_SIZE);
613 dmac_flush_range(ptr, ptr + PAGE_SIZE);
614 kunmap_atomic(ptr);
615 }
616 }
617
Jeremy Gebben7018a212012-04-11 10:23:52 -0600618 outer_cache_range_op_sg(memdesc->sg, memdesc->sglen,
619 KGSL_CACHE_OP_FLUSH);
Jordan Croused17e9aa2011-10-12 16:57:48 -0600620
Jordan Croused17e9aa2011-10-12 16:57:48 -0600621 ret = kgsl_mmu_map(pagetable, memdesc, protflags);
622
623 if (ret)
624 goto done;
625
Harsh Vardhan Dwivedif99c2632012-03-15 14:17:11 -0600626 KGSL_STATS_ADD(size, kgsl_driver.stats.page_alloc,
627 kgsl_driver.stats.page_alloc_max);
Jordan Croused17e9aa2011-10-12 16:57:48 -0600628
629 order = get_order(size);
630
631 if (order < 16)
632 kgsl_driver.stats.histogram[order]++;
633
634done:
Jordan Crouse89bd3232012-07-02 17:50:15 -0600635 kfree(pages);
636
Jordan Croused17e9aa2011-10-12 16:57:48 -0600637 if (ret)
638 kgsl_sharedmem_free(memdesc);
639
640 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700641}
642
643int
Harsh Vardhan Dwivedif99c2632012-03-15 14:17:11 -0600644kgsl_sharedmem_page_alloc(struct kgsl_memdesc *memdesc,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700645 struct kgsl_pagetable *pagetable, size_t size)
646{
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -0600647 int ret = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700648 BUG_ON(size == 0);
649
650 size = ALIGN(size, PAGE_SIZE * 2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700651
Harsh Vardhan Dwivedif99c2632012-03-15 14:17:11 -0600652 ret = _kgsl_sharedmem_page_alloc(memdesc, pagetable, size,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700653 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -0600654 if (!ret)
Harsh Vardhan Dwivedif99c2632012-03-15 14:17:11 -0600655 ret = kgsl_page_alloc_map_kernel(memdesc);
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -0600656 if (ret)
657 kgsl_sharedmem_free(memdesc);
658 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700659}
Harsh Vardhan Dwivedif99c2632012-03-15 14:17:11 -0600660EXPORT_SYMBOL(kgsl_sharedmem_page_alloc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700661
662int
Harsh Vardhan Dwivedif99c2632012-03-15 14:17:11 -0600663kgsl_sharedmem_page_alloc_user(struct kgsl_memdesc *memdesc,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700664 struct kgsl_pagetable *pagetable,
665 size_t size, int flags)
666{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700667 unsigned int protflags;
668
669 BUG_ON(size == 0);
Anshuman Danieecd5202012-02-17 19:52:49 +0530670
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700671 protflags = GSL_PT_PAGE_RV;
672 if (!(flags & KGSL_MEMFLAGS_GPUREADONLY))
673 protflags |= GSL_PT_PAGE_WV;
674
Harsh Vardhan Dwivedif99c2632012-03-15 14:17:11 -0600675 return _kgsl_sharedmem_page_alloc(memdesc, pagetable, size,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700676 protflags);
677}
Harsh Vardhan Dwivedif99c2632012-03-15 14:17:11 -0600678EXPORT_SYMBOL(kgsl_sharedmem_page_alloc_user);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700679
680int
681kgsl_sharedmem_alloc_coherent(struct kgsl_memdesc *memdesc, size_t size)
682{
Jordan Croused17e9aa2011-10-12 16:57:48 -0600683 int result = 0;
684
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700685 size = ALIGN(size, PAGE_SIZE);
686
Jordan Croused17e9aa2011-10-12 16:57:48 -0600687 memdesc->size = size;
688 memdesc->ops = &kgsl_coherent_ops;
689
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700690 memdesc->hostptr = dma_alloc_coherent(NULL, size, &memdesc->physaddr,
691 GFP_KERNEL);
692 if (memdesc->hostptr == NULL) {
693 KGSL_CORE_ERR("dma_alloc_coherent(%d) failed\n", size);
Jordan Croused17e9aa2011-10-12 16:57:48 -0600694 result = -ENOMEM;
695 goto err;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700696 }
697
Jordan Croused17e9aa2011-10-12 16:57:48 -0600698 result = memdesc_sg_phys(memdesc, memdesc->physaddr, size);
699 if (result)
700 goto err;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700701
702 /* Record statistics */
703
704 KGSL_STATS_ADD(size, kgsl_driver.stats.coherent,
705 kgsl_driver.stats.coherent_max);
706
Jordan Croused17e9aa2011-10-12 16:57:48 -0600707err:
708 if (result)
709 kgsl_sharedmem_free(memdesc);
710
711 return result;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700712}
713EXPORT_SYMBOL(kgsl_sharedmem_alloc_coherent);
714
715void kgsl_sharedmem_free(struct kgsl_memdesc *memdesc)
716{
717 if (memdesc == NULL || memdesc->size == 0)
718 return;
719
720 if (memdesc->gpuaddr)
721 kgsl_mmu_unmap(memdesc->pagetable, memdesc);
722
Jordan Croused17e9aa2011-10-12 16:57:48 -0600723 if (memdesc->ops && memdesc->ops->free)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700724 memdesc->ops->free(memdesc);
725
Jordan Crousea652a072012-04-06 16:26:33 -0600726 kgsl_sg_free(memdesc->sg, memdesc->sglen);
Jordan Croused17e9aa2011-10-12 16:57:48 -0600727
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700728 memset(memdesc, 0, sizeof(*memdesc));
729}
730EXPORT_SYMBOL(kgsl_sharedmem_free);
731
732static int
733_kgsl_sharedmem_ebimem(struct kgsl_memdesc *memdesc,
734 struct kgsl_pagetable *pagetable, size_t size)
735{
Jordan Croused17e9aa2011-10-12 16:57:48 -0600736 int result = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700737
Jordan Croused17e9aa2011-10-12 16:57:48 -0600738 memdesc->size = size;
739 memdesc->pagetable = pagetable;
740 memdesc->ops = &kgsl_ebimem_ops;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700741 memdesc->physaddr = allocate_contiguous_ebi_nomap(size, SZ_8K);
742
743 if (memdesc->physaddr == 0) {
744 KGSL_CORE_ERR("allocate_contiguous_ebi_nomap(%d) failed\n",
745 size);
746 return -ENOMEM;
747 }
748
Jordan Croused17e9aa2011-10-12 16:57:48 -0600749 result = memdesc_sg_phys(memdesc, memdesc->physaddr, size);
750
751 if (result)
752 goto err;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700753
754 result = kgsl_mmu_map(pagetable, memdesc,
755 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
756
757 if (result)
Jordan Croused17e9aa2011-10-12 16:57:48 -0600758 goto err;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700759
760 KGSL_STATS_ADD(size, kgsl_driver.stats.coherent,
761 kgsl_driver.stats.coherent_max);
762
Jordan Croused17e9aa2011-10-12 16:57:48 -0600763err:
764 if (result)
765 kgsl_sharedmem_free(memdesc);
766
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700767 return result;
768}
769
770int
771kgsl_sharedmem_ebimem_user(struct kgsl_memdesc *memdesc,
772 struct kgsl_pagetable *pagetable,
773 size_t size, int flags)
774{
775 size = ALIGN(size, PAGE_SIZE);
776 return _kgsl_sharedmem_ebimem(memdesc, pagetable, size);
777}
778EXPORT_SYMBOL(kgsl_sharedmem_ebimem_user);
779
780int
781kgsl_sharedmem_ebimem(struct kgsl_memdesc *memdesc,
782 struct kgsl_pagetable *pagetable, size_t size)
783{
784 int result;
785 size = ALIGN(size, 8192);
786 result = _kgsl_sharedmem_ebimem(memdesc, pagetable, size);
787
788 if (result)
789 return result;
790
791 memdesc->hostptr = ioremap(memdesc->physaddr, size);
792
793 if (memdesc->hostptr == NULL) {
794 KGSL_CORE_ERR("ioremap failed\n");
795 kgsl_sharedmem_free(memdesc);
796 return -ENOMEM;
797 }
798
799 return 0;
800}
801EXPORT_SYMBOL(kgsl_sharedmem_ebimem);
802
803int
804kgsl_sharedmem_readl(const struct kgsl_memdesc *memdesc,
805 uint32_t *dst,
806 unsigned int offsetbytes)
807{
Jeremy Gebbenaba13272012-01-31 17:31:23 -0700808 uint32_t *src;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700809 BUG_ON(memdesc == NULL || memdesc->hostptr == NULL || dst == NULL);
Jeremy Gebbenaba13272012-01-31 17:31:23 -0700810 WARN_ON(offsetbytes % sizeof(uint32_t) != 0);
811 if (offsetbytes % sizeof(uint32_t) != 0)
812 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700813
Jeremy Gebbenaba13272012-01-31 17:31:23 -0700814 WARN_ON(offsetbytes + sizeof(uint32_t) > memdesc->size);
815 if (offsetbytes + sizeof(uint32_t) > memdesc->size)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700816 return -ERANGE;
Jeremy Gebbenaba13272012-01-31 17:31:23 -0700817 src = (uint32_t *)(memdesc->hostptr + offsetbytes);
818 *dst = *src;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700819 return 0;
820}
821EXPORT_SYMBOL(kgsl_sharedmem_readl);
822
823int
824kgsl_sharedmem_writel(const struct kgsl_memdesc *memdesc,
825 unsigned int offsetbytes,
826 uint32_t src)
827{
Jeremy Gebbenaba13272012-01-31 17:31:23 -0700828 uint32_t *dst;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700829 BUG_ON(memdesc == NULL || memdesc->hostptr == NULL);
Jeremy Gebbenaba13272012-01-31 17:31:23 -0700830 WARN_ON(offsetbytes % sizeof(uint32_t) != 0);
831 if (offsetbytes % sizeof(uint32_t) != 0)
832 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700833
Jeremy Gebbenaba13272012-01-31 17:31:23 -0700834 WARN_ON(offsetbytes + sizeof(uint32_t) > memdesc->size);
835 if (offsetbytes + sizeof(uint32_t) > memdesc->size)
836 return -ERANGE;
Jeremy Gebbena3d07a42011-10-17 12:08:16 -0600837 kgsl_cffdump_setmem(memdesc->gpuaddr + offsetbytes,
Jeremy Gebbenaba13272012-01-31 17:31:23 -0700838 src, sizeof(uint32_t));
839 dst = (uint32_t *)(memdesc->hostptr + offsetbytes);
840 *dst = src;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700841 return 0;
842}
843EXPORT_SYMBOL(kgsl_sharedmem_writel);
844
845int
846kgsl_sharedmem_set(const struct kgsl_memdesc *memdesc, unsigned int offsetbytes,
847 unsigned int value, unsigned int sizebytes)
848{
849 BUG_ON(memdesc == NULL || memdesc->hostptr == NULL);
850 BUG_ON(offsetbytes + sizebytes > memdesc->size);
851
Jeremy Gebbena3d07a42011-10-17 12:08:16 -0600852 kgsl_cffdump_setmem(memdesc->gpuaddr + offsetbytes, value,
853 sizebytes);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700854 memset(memdesc->hostptr + offsetbytes, value, sizebytes);
855 return 0;
856}
857EXPORT_SYMBOL(kgsl_sharedmem_set);
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -0600858
859/*
860 * kgsl_sharedmem_map_vma - Map a user vma to physical memory
861 *
862 * @vma - The user vma to map
863 * @memdesc - The memory descriptor which contains information about the
864 * physical memory
865 *
866 * Return: 0 on success else error code
867 */
868int
869kgsl_sharedmem_map_vma(struct vm_area_struct *vma,
870 const struct kgsl_memdesc *memdesc)
871{
872 unsigned long addr = vma->vm_start;
873 unsigned long size = vma->vm_end - vma->vm_start;
874 int ret, i = 0;
875
876 if (!memdesc->sg || (size != memdesc->size) ||
877 (memdesc->sglen != (size / PAGE_SIZE)))
878 return -EINVAL;
879
880 for (; addr < vma->vm_end; addr += PAGE_SIZE, i++) {
881 ret = vm_insert_page(vma, addr, sg_page(&memdesc->sg[i]));
882 if (ret)
883 return ret;
884 }
885 return 0;
886}
887EXPORT_SYMBOL(kgsl_sharedmem_map_vma);