John Stultz | 1184ead | 2014-01-09 21:08:37 -0800 | [diff] [blame] | 1 | /* |
| 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 Gortmaker | 102f1a2 | 2014-01-21 16:22:12 -0500 | [diff] [blame] | 20 | #include <linux/init.h> |
John Stultz | 1184ead | 2014-01-09 21:08:37 -0800 | [diff] [blame] | 21 | #include <linux/bootmem.h> |
| 22 | #include <linux/memblock.h> |
| 23 | #include <linux/sizes.h> |
Chen Gang | 10f6f9c | 2014-01-18 17:04:06 +0800 | [diff] [blame] | 24 | #include <linux/io.h> |
John Stultz | 1184ead | 2014-01-09 21:08:37 -0800 | [diff] [blame] | 25 | #include "ion.h" |
| 26 | #include "ion_priv.h" |
| 27 | |
Wei Yongjun | 3a915dd | 2014-01-14 10:04:49 +0800 | [diff] [blame] | 28 | static struct ion_device *idev; |
| 29 | static struct ion_heap **heaps; |
John Stultz | 1184ead | 2014-01-09 21:08:37 -0800 | [diff] [blame] | 30 | |
Wei Yongjun | 3a915dd | 2014-01-14 10:04:49 +0800 | [diff] [blame] | 31 | static void *carveout_ptr; |
| 32 | static void *chunk_ptr; |
John Stultz | 4c45b1a | 2014-01-09 21:08:38 -0800 | [diff] [blame] | 33 | |
Wei Yongjun | 3a915dd | 2014-01-14 10:04:49 +0800 | [diff] [blame] | 34 | static struct ion_platform_heap dummy_heaps[] = { |
John Stultz | 1184ead | 2014-01-09 21:08:37 -0800 | [diff] [blame] | 35 | { |
| 36 | .id = ION_HEAP_TYPE_SYSTEM, |
| 37 | .type = ION_HEAP_TYPE_SYSTEM, |
| 38 | .name = "system", |
| 39 | }, |
| 40 | { |
John Stultz | 4c45b1a | 2014-01-09 21:08:38 -0800 | [diff] [blame] | 41 | .id = ION_HEAP_TYPE_CARVEOUT, |
| 42 | .type = ION_HEAP_TYPE_CARVEOUT, |
| 43 | .name = "carveout", |
| 44 | .size = SZ_4M, |
| 45 | }, |
| 46 | { |
| 47 | .id = ION_HEAP_TYPE_CHUNK, |
| 48 | .type = ION_HEAP_TYPE_CHUNK, |
| 49 | .name = "chunk", |
| 50 | .size = SZ_4M, |
| 51 | .align = SZ_16K, |
| 52 | .priv = (void *)(SZ_16K), |
| 53 | }, |
John Stultz | 1184ead | 2014-01-09 21:08:37 -0800 | [diff] [blame] | 54 | }; |
| 55 | |
Wei Yongjun | 3a915dd | 2014-01-14 10:04:49 +0800 | [diff] [blame] | 56 | static struct ion_platform_data dummy_ion_pdata = { |
Tomas Winkler | 18691f5 | 2014-01-22 19:15:55 +0200 | [diff] [blame] | 57 | .nr = ARRAY_SIZE(dummy_heaps), |
John Stultz | 1184ead | 2014-01-09 21:08:37 -0800 | [diff] [blame] | 58 | .heaps = dummy_heaps, |
| 59 | }; |
| 60 | |
| 61 | static int __init ion_dummy_init(void) |
| 62 | { |
| 63 | int i, err; |
| 64 | |
| 65 | idev = ion_device_create(NULL); |
Sudip Mukherjee | 9f563f1 | 2016-04-07 22:02:25 +0530 | [diff] [blame] | 66 | if (IS_ERR(idev)) |
| 67 | return PTR_ERR(idev); |
Phong Tran | d320c45 | 2014-08-13 20:37:04 +0700 | [diff] [blame] | 68 | heaps = kcalloc(dummy_ion_pdata.nr, sizeof(struct ion_heap *), |
John Stultz | 1184ead | 2014-01-09 21:08:37 -0800 | [diff] [blame] | 69 | GFP_KERNEL); |
| 70 | if (!heaps) |
Dan Carpenter | 630127f | 2014-01-20 13:30:01 +0300 | [diff] [blame] | 71 | return -ENOMEM; |
John Stultz | 1184ead | 2014-01-09 21:08:37 -0800 | [diff] [blame] | 72 | |
John Stultz | 4c45b1a | 2014-01-09 21:08:38 -0800 | [diff] [blame] | 73 | |
John Stultz | 1184ead | 2014-01-09 21:08:37 -0800 | [diff] [blame] | 74 | for (i = 0; i < dummy_ion_pdata.nr; i++) { |
| 75 | struct ion_platform_heap *heap_data = &dummy_ion_pdata.heaps[i]; |
| 76 | |
John Stultz | 4573300 | 2019-02-13 12:44:18 -0800 | [diff] [blame] | 77 | if (heap_data->type == ION_HEAP_TYPE_CARVEOUT) { |
| 78 | /* Allocate a dummy carveout heap */ |
| 79 | carveout_ptr = alloc_pages_exact(heap_data->size, |
| 80 | GFP_KERNEL); |
| 81 | if (!carveout_ptr) { |
| 82 | pr_err("ion_dummy: Could not allocate carveout\n"); |
| 83 | continue; |
| 84 | } |
| 85 | heap_data->base = virt_to_phys(carveout_ptr); |
| 86 | } |
John Stultz | 4c45b1a | 2014-01-09 21:08:38 -0800 | [diff] [blame] | 87 | |
John Stultz | 4573300 | 2019-02-13 12:44:18 -0800 | [diff] [blame] | 88 | if (heap_data->type == ION_HEAP_TYPE_CHUNK) { |
| 89 | /* Allocate a dummy chunk heap */ |
| 90 | chunk_ptr = alloc_pages_exact(heap_data->size, |
| 91 | GFP_KERNEL); |
| 92 | if (!chunk_ptr) { |
| 93 | pr_err("ion_dummy: Could not allocate chunk\n"); |
| 94 | continue; |
| 95 | } |
| 96 | heap_data->base = virt_to_phys(chunk_ptr); |
| 97 | } |
John Stultz | 4c45b1a | 2014-01-09 21:08:38 -0800 | [diff] [blame] | 98 | |
John Stultz | 1184ead | 2014-01-09 21:08:37 -0800 | [diff] [blame] | 99 | heaps[i] = ion_heap_create(heap_data); |
| 100 | if (IS_ERR_OR_NULL(heaps[i])) { |
| 101 | err = PTR_ERR(heaps[i]); |
| 102 | goto err; |
| 103 | } |
| 104 | ion_device_add_heap(idev, heaps[i]); |
| 105 | } |
| 106 | return 0; |
| 107 | err: |
John Stultz | a53fdae | 2019-02-13 13:59:12 -0800 | [diff] [blame] | 108 | for (i = 0; i < dummy_ion_pdata.nr; ++i) { |
John Stultz | 4573300 | 2019-02-13 12:44:18 -0800 | [diff] [blame] | 109 | struct ion_platform_heap *heap_data = &dummy_ion_pdata.heaps[i]; |
| 110 | |
John Stultz | a53fdae | 2019-02-13 13:59:12 -0800 | [diff] [blame] | 111 | if (!IS_ERR_OR_NULL(heaps[i])) |
| 112 | ion_heap_destroy(heaps[i]); |
John Stultz | 4573300 | 2019-02-13 12:44:18 -0800 | [diff] [blame] | 113 | |
| 114 | if (heap_data->type == ION_HEAP_TYPE_CARVEOUT) { |
| 115 | if (carveout_ptr) { |
| 116 | free_pages_exact(carveout_ptr, |
| 117 | heap_data->size); |
| 118 | } |
| 119 | carveout_ptr = NULL; |
| 120 | } |
| 121 | if (heap_data->type == ION_HEAP_TYPE_CHUNK) { |
| 122 | if (chunk_ptr) { |
| 123 | free_pages_exact(chunk_ptr, heap_data->size); |
| 124 | } |
| 125 | chunk_ptr = NULL; |
| 126 | } |
John Stultz | a53fdae | 2019-02-13 13:59:12 -0800 | [diff] [blame] | 127 | } |
John Stultz | 1184ead | 2014-01-09 21:08:37 -0800 | [diff] [blame] | 128 | kfree(heaps); |
| 129 | |
| 130 | return err; |
| 131 | } |
Paul Gortmaker | 102f1a2 | 2014-01-21 16:22:12 -0500 | [diff] [blame] | 132 | device_initcall(ion_dummy_init); |
John Stultz | 1184ead | 2014-01-09 21:08:37 -0800 | [diff] [blame] | 133 | |
| 134 | static void __exit ion_dummy_exit(void) |
| 135 | { |
| 136 | int i; |
| 137 | |
| 138 | ion_device_destroy(idev); |
| 139 | |
John Stultz | 4573300 | 2019-02-13 12:44:18 -0800 | [diff] [blame] | 140 | for (i = 0; i < dummy_ion_pdata.nr; ++i) { |
| 141 | struct ion_platform_heap *heap_data = &dummy_ion_pdata.heaps[i]; |
| 142 | |
John Stultz | a53fdae | 2019-02-13 13:59:12 -0800 | [diff] [blame] | 143 | if (!IS_ERR_OR_NULL(heaps[i])) |
| 144 | ion_heap_destroy(heaps[i]); |
John Stultz | 4573300 | 2019-02-13 12:44:18 -0800 | [diff] [blame] | 145 | |
| 146 | if (heap_data->type == ION_HEAP_TYPE_CARVEOUT) { |
| 147 | if (carveout_ptr) { |
| 148 | free_pages_exact(carveout_ptr, |
| 149 | heap_data->size); |
| 150 | } |
| 151 | carveout_ptr = NULL; |
| 152 | } |
| 153 | if (heap_data->type == ION_HEAP_TYPE_CHUNK) { |
| 154 | if (chunk_ptr) { |
| 155 | free_pages_exact(chunk_ptr, heap_data->size); |
| 156 | } |
| 157 | chunk_ptr = NULL; |
| 158 | } |
John Stultz | a53fdae | 2019-02-13 13:59:12 -0800 | [diff] [blame] | 159 | } |
John Stultz | 1184ead | 2014-01-09 21:08:37 -0800 | [diff] [blame] | 160 | kfree(heaps); |
John Stultz | 1184ead | 2014-01-09 21:08:37 -0800 | [diff] [blame] | 161 | } |
Paul Gortmaker | 102f1a2 | 2014-01-21 16:22:12 -0500 | [diff] [blame] | 162 | __exitcall(ion_dummy_exit); |