blob: 5678870bff4850cbdf53eff4d58cc29b7df84434 [file] [log] [blame]
John Stultz1184ead2014-01-09 21:08:37 -08001/*
2 * drivers/gpu/ion/ion_dummy_driver.c
3 *
4 * Copyright (C) 2013 Linaro, Inc
5 *
6 * This software is licensed under the terms of the GNU General Public
7 * License version 2, as published by the Free Software Foundation, and
8 * may be copied, distributed, and modified under those terms.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 */
16
17#include <linux/err.h>
18#include <linux/platform_device.h>
19#include <linux/slab.h>
Paul Gortmaker102f1a22014-01-21 16:22:12 -050020#include <linux/init.h>
John Stultz1184ead2014-01-09 21:08:37 -080021#include <linux/bootmem.h>
22#include <linux/memblock.h>
23#include <linux/sizes.h>
Chen Gang10f6f9c2014-01-18 17:04:06 +080024#include <linux/io.h>
John Stultz1184ead2014-01-09 21:08:37 -080025#include "ion.h"
26#include "ion_priv.h"
27
Wei Yongjun3a915dd2014-01-14 10:04:49 +080028static struct ion_device *idev;
29static struct ion_heap **heaps;
John Stultz1184ead2014-01-09 21:08:37 -080030
Wei Yongjun3a915dd2014-01-14 10:04:49 +080031static void *carveout_ptr;
32static void *chunk_ptr;
John Stultz4c45b1a2014-01-09 21:08:38 -080033
Wei Yongjun3a915dd2014-01-14 10:04:49 +080034static struct ion_platform_heap dummy_heaps[] = {
John Stultz1184ead2014-01-09 21:08:37 -080035 {
36 .id = ION_HEAP_TYPE_SYSTEM,
37 .type = ION_HEAP_TYPE_SYSTEM,
38 .name = "system",
39 },
40 {
41 .id = ION_HEAP_TYPE_SYSTEM_CONTIG,
42 .type = ION_HEAP_TYPE_SYSTEM_CONTIG,
43 .name = "system contig",
44 },
John Stultz4c45b1a2014-01-09 21:08:38 -080045 {
46 .id = ION_HEAP_TYPE_CARVEOUT,
47 .type = ION_HEAP_TYPE_CARVEOUT,
48 .name = "carveout",
49 .size = SZ_4M,
50 },
51 {
52 .id = ION_HEAP_TYPE_CHUNK,
53 .type = ION_HEAP_TYPE_CHUNK,
54 .name = "chunk",
55 .size = SZ_4M,
56 .align = SZ_16K,
57 .priv = (void *)(SZ_16K),
58 },
John Stultz1184ead2014-01-09 21:08:37 -080059};
60
Wei Yongjun3a915dd2014-01-14 10:04:49 +080061static struct ion_platform_data dummy_ion_pdata = {
Tomas Winkler18691f52014-01-22 19:15:55 +020062 .nr = ARRAY_SIZE(dummy_heaps),
John Stultz1184ead2014-01-09 21:08:37 -080063 .heaps = dummy_heaps,
64};
65
66static int __init ion_dummy_init(void)
67{
68 int i, err;
69
70 idev = ion_device_create(NULL);
Phong Trand320c452014-08-13 20:37:04 +070071 heaps = kcalloc(dummy_ion_pdata.nr, sizeof(struct ion_heap *),
John Stultz1184ead2014-01-09 21:08:37 -080072 GFP_KERNEL);
73 if (!heaps)
Dan Carpenter630127f2014-01-20 13:30:01 +030074 return -ENOMEM;
John Stultz1184ead2014-01-09 21:08:37 -080075
John Stultz4c45b1a2014-01-09 21:08:38 -080076
77 /* Allocate a dummy carveout heap */
78 carveout_ptr = alloc_pages_exact(
79 dummy_heaps[ION_HEAP_TYPE_CARVEOUT].size,
80 GFP_KERNEL);
81 if (carveout_ptr)
82 dummy_heaps[ION_HEAP_TYPE_CARVEOUT].base =
83 virt_to_phys(carveout_ptr);
84 else
85 pr_err("ion_dummy: Could not allocate carveout\n");
86
87 /* Allocate a dummy chunk heap */
88 chunk_ptr = alloc_pages_exact(
89 dummy_heaps[ION_HEAP_TYPE_CHUNK].size,
90 GFP_KERNEL);
91 if (chunk_ptr)
92 dummy_heaps[ION_HEAP_TYPE_CHUNK].base = virt_to_phys(chunk_ptr);
93 else
94 pr_err("ion_dummy: Could not allocate chunk\n");
95
John Stultz1184ead2014-01-09 21:08:37 -080096 for (i = 0; i < dummy_ion_pdata.nr; i++) {
97 struct ion_platform_heap *heap_data = &dummy_ion_pdata.heaps[i];
98
John Stultz4c45b1a2014-01-09 21:08:38 -080099 if (heap_data->type == ION_HEAP_TYPE_CARVEOUT &&
100 !heap_data->base)
101 continue;
102
103 if (heap_data->type == ION_HEAP_TYPE_CHUNK && !heap_data->base)
104 continue;
105
John Stultz1184ead2014-01-09 21:08:37 -0800106 heaps[i] = ion_heap_create(heap_data);
107 if (IS_ERR_OR_NULL(heaps[i])) {
108 err = PTR_ERR(heaps[i]);
109 goto err;
110 }
111 ion_device_add_heap(idev, heaps[i]);
112 }
113 return 0;
114err:
Markus Elfring698f1402014-11-23 18:48:15 +0100115 for (i = 0; i < dummy_ion_pdata.nr; ++i)
116 ion_heap_destroy(heaps[i]);
John Stultz1184ead2014-01-09 21:08:37 -0800117 kfree(heaps);
118
John Stultz4c45b1a2014-01-09 21:08:38 -0800119 if (carveout_ptr) {
120 free_pages_exact(carveout_ptr,
121 dummy_heaps[ION_HEAP_TYPE_CARVEOUT].size);
122 carveout_ptr = NULL;
123 }
124 if (chunk_ptr) {
125 free_pages_exact(chunk_ptr,
126 dummy_heaps[ION_HEAP_TYPE_CHUNK].size);
127 chunk_ptr = NULL;
128 }
John Stultz1184ead2014-01-09 21:08:37 -0800129 return err;
130}
Paul Gortmaker102f1a22014-01-21 16:22:12 -0500131device_initcall(ion_dummy_init);
John Stultz1184ead2014-01-09 21:08:37 -0800132
133static void __exit ion_dummy_exit(void)
134{
135 int i;
136
137 ion_device_destroy(idev);
138
139 for (i = 0; i < dummy_ion_pdata.nr; i++)
140 ion_heap_destroy(heaps[i]);
141 kfree(heaps);
142
John Stultz4c45b1a2014-01-09 21:08:38 -0800143 if (carveout_ptr) {
144 free_pages_exact(carveout_ptr,
145 dummy_heaps[ION_HEAP_TYPE_CARVEOUT].size);
146 carveout_ptr = NULL;
147 }
148 if (chunk_ptr) {
149 free_pages_exact(chunk_ptr,
150 dummy_heaps[ION_HEAP_TYPE_CHUNK].size);
151 chunk_ptr = NULL;
152 }
John Stultz1184ead2014-01-09 21:08:37 -0800153}
Paul Gortmaker102f1a22014-01-21 16:22:12 -0500154__exitcall(ion_dummy_exit);