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 | { |
| 41 | .id = ION_HEAP_TYPE_SYSTEM_CONTIG, |
| 42 | .type = ION_HEAP_TYPE_SYSTEM_CONTIG, |
| 43 | .name = "system contig", |
| 44 | }, |
John Stultz | 4c45b1a | 2014-01-09 21:08:38 -0800 | [diff] [blame] | 45 | { |
| 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 Stultz | 1184ead | 2014-01-09 21:08:37 -0800 | [diff] [blame] | 59 | }; |
| 60 | |
Wei Yongjun | 3a915dd | 2014-01-14 10:04:49 +0800 | [diff] [blame] | 61 | static struct ion_platform_data dummy_ion_pdata = { |
Tomas Winkler | 18691f5 | 2014-01-22 19:15:55 +0200 | [diff] [blame] | 62 | .nr = ARRAY_SIZE(dummy_heaps), |
John Stultz | 1184ead | 2014-01-09 21:08:37 -0800 | [diff] [blame] | 63 | .heaps = dummy_heaps, |
| 64 | }; |
| 65 | |
| 66 | static int __init ion_dummy_init(void) |
| 67 | { |
| 68 | int i, err; |
| 69 | |
| 70 | idev = ion_device_create(NULL); |
Sudip Mukherjee | 9f563f1 | 2016-04-07 22:02:25 +0530 | [diff] [blame] | 71 | if (IS_ERR(idev)) |
| 72 | return PTR_ERR(idev); |
Phong Tran | d320c45 | 2014-08-13 20:37:04 +0700 | [diff] [blame] | 73 | heaps = kcalloc(dummy_ion_pdata.nr, sizeof(struct ion_heap *), |
John Stultz | 1184ead | 2014-01-09 21:08:37 -0800 | [diff] [blame] | 74 | GFP_KERNEL); |
| 75 | if (!heaps) |
Dan Carpenter | 630127f | 2014-01-20 13:30:01 +0300 | [diff] [blame] | 76 | return -ENOMEM; |
John Stultz | 1184ead | 2014-01-09 21:08:37 -0800 | [diff] [blame] | 77 | |
John Stultz | 4c45b1a | 2014-01-09 21:08:38 -0800 | [diff] [blame] | 78 | |
| 79 | /* Allocate a dummy carveout heap */ |
| 80 | carveout_ptr = alloc_pages_exact( |
| 81 | dummy_heaps[ION_HEAP_TYPE_CARVEOUT].size, |
| 82 | GFP_KERNEL); |
| 83 | if (carveout_ptr) |
| 84 | dummy_heaps[ION_HEAP_TYPE_CARVEOUT].base = |
| 85 | virt_to_phys(carveout_ptr); |
| 86 | else |
| 87 | pr_err("ion_dummy: Could not allocate carveout\n"); |
| 88 | |
| 89 | /* Allocate a dummy chunk heap */ |
| 90 | chunk_ptr = alloc_pages_exact( |
| 91 | dummy_heaps[ION_HEAP_TYPE_CHUNK].size, |
| 92 | GFP_KERNEL); |
| 93 | if (chunk_ptr) |
| 94 | dummy_heaps[ION_HEAP_TYPE_CHUNK].base = virt_to_phys(chunk_ptr); |
| 95 | else |
| 96 | pr_err("ion_dummy: Could not allocate chunk\n"); |
| 97 | |
John Stultz | 1184ead | 2014-01-09 21:08:37 -0800 | [diff] [blame] | 98 | for (i = 0; i < dummy_ion_pdata.nr; i++) { |
| 99 | struct ion_platform_heap *heap_data = &dummy_ion_pdata.heaps[i]; |
| 100 | |
John Stultz | 4c45b1a | 2014-01-09 21:08:38 -0800 | [diff] [blame] | 101 | if (heap_data->type == ION_HEAP_TYPE_CARVEOUT && |
Ben LeMasurier | 679011b | 2016-08-22 07:45:53 -0600 | [diff] [blame] | 102 | !heap_data->base) |
John Stultz | 4c45b1a | 2014-01-09 21:08:38 -0800 | [diff] [blame] | 103 | continue; |
| 104 | |
| 105 | if (heap_data->type == ION_HEAP_TYPE_CHUNK && !heap_data->base) |
| 106 | continue; |
| 107 | |
John Stultz | 1184ead | 2014-01-09 21:08:37 -0800 | [diff] [blame] | 108 | heaps[i] = ion_heap_create(heap_data); |
| 109 | if (IS_ERR_OR_NULL(heaps[i])) { |
| 110 | err = PTR_ERR(heaps[i]); |
| 111 | goto err; |
| 112 | } |
| 113 | ion_device_add_heap(idev, heaps[i]); |
| 114 | } |
| 115 | return 0; |
| 116 | err: |
Markus Elfring | 698f140 | 2014-11-23 18:48:15 +0100 | [diff] [blame] | 117 | for (i = 0; i < dummy_ion_pdata.nr; ++i) |
| 118 | ion_heap_destroy(heaps[i]); |
John Stultz | 1184ead | 2014-01-09 21:08:37 -0800 | [diff] [blame] | 119 | kfree(heaps); |
| 120 | |
John Stultz | 4c45b1a | 2014-01-09 21:08:38 -0800 | [diff] [blame] | 121 | if (carveout_ptr) { |
| 122 | free_pages_exact(carveout_ptr, |
Ben LeMasurier | 679011b | 2016-08-22 07:45:53 -0600 | [diff] [blame] | 123 | dummy_heaps[ION_HEAP_TYPE_CARVEOUT].size); |
John Stultz | 4c45b1a | 2014-01-09 21:08:38 -0800 | [diff] [blame] | 124 | carveout_ptr = NULL; |
| 125 | } |
| 126 | if (chunk_ptr) { |
| 127 | free_pages_exact(chunk_ptr, |
Ben LeMasurier | 679011b | 2016-08-22 07:45:53 -0600 | [diff] [blame] | 128 | dummy_heaps[ION_HEAP_TYPE_CHUNK].size); |
John Stultz | 4c45b1a | 2014-01-09 21:08:38 -0800 | [diff] [blame] | 129 | chunk_ptr = NULL; |
| 130 | } |
John Stultz | 1184ead | 2014-01-09 21:08:37 -0800 | [diff] [blame] | 131 | return err; |
| 132 | } |
Paul Gortmaker | 102f1a2 | 2014-01-21 16:22:12 -0500 | [diff] [blame] | 133 | device_initcall(ion_dummy_init); |
John Stultz | 1184ead | 2014-01-09 21:08:37 -0800 | [diff] [blame] | 134 | |
| 135 | static void __exit ion_dummy_exit(void) |
| 136 | { |
| 137 | int i; |
| 138 | |
| 139 | ion_device_destroy(idev); |
| 140 | |
| 141 | for (i = 0; i < dummy_ion_pdata.nr; i++) |
| 142 | ion_heap_destroy(heaps[i]); |
| 143 | kfree(heaps); |
| 144 | |
John Stultz | 4c45b1a | 2014-01-09 21:08:38 -0800 | [diff] [blame] | 145 | if (carveout_ptr) { |
| 146 | free_pages_exact(carveout_ptr, |
Ben LeMasurier | 679011b | 2016-08-22 07:45:53 -0600 | [diff] [blame] | 147 | dummy_heaps[ION_HEAP_TYPE_CARVEOUT].size); |
John Stultz | 4c45b1a | 2014-01-09 21:08:38 -0800 | [diff] [blame] | 148 | carveout_ptr = NULL; |
| 149 | } |
| 150 | if (chunk_ptr) { |
| 151 | free_pages_exact(chunk_ptr, |
Ben LeMasurier | 679011b | 2016-08-22 07:45:53 -0600 | [diff] [blame] | 152 | dummy_heaps[ION_HEAP_TYPE_CHUNK].size); |
John Stultz | 4c45b1a | 2014-01-09 21:08:38 -0800 | [diff] [blame] | 153 | chunk_ptr = NULL; |
| 154 | } |
John Stultz | 1184ead | 2014-01-09 21:08:37 -0800 | [diff] [blame] | 155 | } |
Paul Gortmaker | 102f1a2 | 2014-01-21 16:22:12 -0500 | [diff] [blame] | 156 | __exitcall(ion_dummy_exit); |