xref: /OK3568_Linux_fs/kernel/drivers/gpu/arm/bifrost/mali_kbase_platform_fake.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 // SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note
2 /*
3  *
4  * (C) COPYRIGHT 2011-2014, 2016-2017, 2020-2022 ARM Limited. All rights reserved.
5  *
6  * This program is free software and is provided to you under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation, and any use by you of this program is subject to the terms
9  * of such GNU license.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, you can access it online at
18  * http://www.gnu.org/licenses/gpl-2.0.html.
19  *
20  */
21 
22 #include <linux/errno.h>
23 #include <linux/export.h>
24 #include <linux/ioport.h>
25 #include <linux/platform_device.h>
26 #include <linux/string.h>
27 
28 /*
29  * This file is included only for type definitions and functions belonging to
30  * specific platform folders. Do not add dependencies with symbols that are
31  * defined somewhere else.
32  */
33 #include <mali_kbase_config.h>
34 
35 #ifndef CONFIG_OF
36 
37 #define PLATFORM_CONFIG_RESOURCE_COUNT 4
38 
39 static struct platform_device *mali_device;
40 
41 /**
42  * kbasep_config_parse_io_resources - Convert data in struct kbase_io_resources
43  * struct to Linux-specific resources
44  * @io_resources:      Input IO resource data
45  * @linux_resources:  Pointer to output array of Linux resource structures
46  *
47  * Function converts data in struct kbase_io_resources struct to an array of Linux resource structures. Note that function
48  * assumes that size of linux_resource array is at least PLATFORM_CONFIG_RESOURCE_COUNT.
49  * Resources are put in fixed order: I/O memory region, job IRQ, MMU IRQ, GPU IRQ.
50  */
kbasep_config_parse_io_resources(const struct kbase_io_resources * io_resources,struct resource * const linux_resources)51 static void kbasep_config_parse_io_resources(const struct kbase_io_resources *io_resources, struct resource *const linux_resources)
52 {
53 	if (!io_resources || !linux_resources) {
54 		pr_err("%s: couldn't find proper resources\n", __func__);
55 		return;
56 	}
57 
58 	memset(linux_resources, 0, PLATFORM_CONFIG_RESOURCE_COUNT * sizeof(struct resource));
59 
60 	linux_resources[0].start = io_resources->io_memory_region.start;
61 	linux_resources[0].end   = io_resources->io_memory_region.end;
62 	linux_resources[0].flags = IORESOURCE_MEM;
63 
64 	linux_resources[1].start = io_resources->job_irq_number;
65 	linux_resources[1].end   = io_resources->job_irq_number;
66 	linux_resources[1].flags = IORESOURCE_IRQ | IORESOURCE_IRQ_HIGHLEVEL;
67 
68 	linux_resources[2].start = io_resources->mmu_irq_number;
69 	linux_resources[2].end   = io_resources->mmu_irq_number;
70 	linux_resources[2].flags = IORESOURCE_IRQ | IORESOURCE_IRQ_HIGHLEVEL;
71 
72 	linux_resources[3].start = io_resources->gpu_irq_number;
73 	linux_resources[3].end   = io_resources->gpu_irq_number;
74 	linux_resources[3].flags = IORESOURCE_IRQ | IORESOURCE_IRQ_HIGHLEVEL;
75 }
76 
kbase_platform_register(void)77 int kbase_platform_register(void)
78 {
79 	struct kbase_platform_config *config;
80 	struct resource resources[PLATFORM_CONFIG_RESOURCE_COUNT];
81 	int err;
82 
83 	config = kbase_get_platform_config(); /* declared in midgard/mali_kbase_config.h but defined in platform folder */
84 	if (config == NULL) {
85 		pr_err("%s: couldn't get platform config\n", __func__);
86 		return -ENODEV;
87 	}
88 
89 	mali_device = platform_device_alloc("mali", 0);
90 	if (mali_device == NULL)
91 		return -ENOMEM;
92 
93 	kbasep_config_parse_io_resources(config->io_resources, resources);
94 	err = platform_device_add_resources(mali_device, resources, PLATFORM_CONFIG_RESOURCE_COUNT);
95 	if (err) {
96 		platform_device_put(mali_device);
97 		mali_device = NULL;
98 		return err;
99 	}
100 
101 	err = platform_device_add(mali_device);
102 	if (err) {
103 		platform_device_unregister(mali_device);
104 		mali_device = NULL;
105 		return err;
106 	}
107 
108 	return 0;
109 }
110 EXPORT_SYMBOL(kbase_platform_register);
111 
kbase_platform_unregister(void)112 void kbase_platform_unregister(void)
113 {
114 	if (mali_device)
115 		platform_device_unregister(mali_device);
116 }
117 EXPORT_SYMBOL(kbase_platform_unregister);
118 
119 #endif /* CONFIG_OF */
120