Merge tag 'lsk-v4.4-16.05-android'
[firefly-linux-kernel-4.4.55.git] / drivers / gpu / arm / mali400 / mali / platform / rk30 / rk3066.c
1 /*
2  * This confidential and proprietary software may be used only as
3  * authorised by a licensing agreement from ARM Limited
4  * (C) COPYRIGHT 2009-2010, 2012 ARM Limited
5  * ALL RIGHTS RESERVED
6  * The entire notice above must be reproduced on all authorised
7  * copies and copies may only be made to the extent permitted
8  * by a licensing agreement from ARM Limited.
9  */
10
11
12 /**
13  * @file rk3066.c
14  * 实现 rk30_platform 中的 platform_specific_strategy_callbacks,
15  * 实际上也是 platform_dependent_part 的顶层.
16  *
17  * mali_device_driver(mdd) 包含两部分 :
18  *      .DP : platform_dependent_part :
19  *              依赖 platform 部分,
20  *              源码在 <mdd_src_dir>/mali/platform/<platform_name> 目录下.
21  *      .DP : common_parts : ARM 实现的通用的部分.
22  */
23
24 /* #define ENABLE_DEBUG_LOG */
25 #include "custom_log.h"
26
27 #include <linux/platform_device.h>
28 #include <linux/version.h>
29 #include <linux/pm.h>
30 #ifdef CONFIG_PM_RUNTIME
31 #include <linux/pm_runtime.h>
32 #endif
33 #include <linux/workqueue.h>
34 #include <linux/dma-mapping.h>
35 #include <linux/rockchip/cpu.h>
36
37 #include <linux/mali/mali_utgard.h>
38 #include "mali_kernel_common.h"
39 #include "mali_platform.h"
40 #include "arm_core_scaling.h"
41
42 #ifdef CONFIG_PM_RUNTIME
43 static int mali_runtime_suspend(struct device *device)
44 {
45         int ret = 0;
46
47         MALI_DEBUG_PRINT(4, ("mali_runtime_suspend() called\n"));
48
49         if (NULL != device->driver &&
50             NULL != device->driver->pm &&
51             NULL != device->driver->pm->runtime_suspend) {
52                 /* Need to notify Mali driver about this event */
53                 ret = device->driver->pm->runtime_suspend(device);
54         }
55
56         mali_platform_power_mode_change(MALI_POWER_MODE_LIGHT_SLEEP);
57
58         return ret;
59 }
60
61 static int mali_runtime_resume(struct device *device)
62 {
63         int ret = 0;
64
65         MALI_DEBUG_PRINT(4, ("mali_runtime_resume() called\n"));
66
67         mali_platform_power_mode_change(MALI_POWER_MODE_ON);
68
69         if (NULL != device->driver &&
70             NULL != device->driver->pm &&
71             NULL != device->driver->pm->runtime_resume) {
72                 /* Need to notify Mali driver about this event */
73                 ret = device->driver->pm->runtime_resume(device);
74         }
75
76         return ret;
77 }
78
79 static int mali_runtime_idle(struct device *device)
80 {
81         int ret = 0;
82
83         MALI_DEBUG_PRINT(4, ("mali_runtime_idle() called\n"));
84
85         if (NULL != device->driver &&
86             NULL != device->driver->pm &&
87             NULL != device->driver->pm->runtime_idle) {
88                 /* Need to notify Mali driver about this event */
89                 ret = device->driver->pm->runtime_idle(device);
90                 if (0 != ret)
91                         return ret;
92         }
93
94         pm_runtime_suspend(device);
95
96         return 0;
97 }
98 #endif
99
100 static int mali_os_suspend(struct device *device)
101 {
102         int ret = 0;
103
104         MALI_DEBUG_PRINT(4, ("mali_os_suspend() called\n"));
105
106         if (NULL != device->driver &&
107             NULL != device->driver->pm &&
108             NULL != device->driver->pm->suspend) {
109                 /* Need to notify Mali driver about this event */
110                 ret = device->driver->pm->suspend(device);
111         }
112
113         mali_platform_power_mode_change(MALI_POWER_MODE_DEEP_SLEEP);
114
115         return ret;
116 }
117
118 static int mali_os_resume(struct device *device)
119 {
120         int ret = 0;
121
122         MALI_DEBUG_PRINT(4, ("mali_os_resume() called\n"));
123
124         mali_platform_power_mode_change(MALI_POWER_MODE_ON);
125
126         if (NULL != device->driver &&
127             NULL != device->driver->pm &&
128             NULL != device->driver->pm->resume) {
129                 /* Need to notify Mali driver about this event */
130                 ret = device->driver->pm->resume(device);
131         }
132
133         return ret;
134 }
135
136 static int mali_os_freeze(struct device *device)
137 {
138         int ret = 0;
139
140         MALI_DEBUG_PRINT(4, ("mali_os_freeze() called\n"));
141
142         if (NULL != device->driver &&
143             NULL != device->driver->pm &&
144             NULL != device->driver->pm->freeze) {
145                 /* Need to notify Mali driver about this event */
146                 ret = device->driver->pm->freeze(device);
147         }
148
149         return ret;
150 }
151
152 static int mali_os_thaw(struct device *device)
153 {
154         int ret = 0;
155
156         MALI_DEBUG_PRINT(4, ("mali_os_thaw() called\n"));
157
158         if (NULL != device->driver &&
159             NULL != device->driver->pm &&
160             NULL != device->driver->pm->thaw) {
161                 /* Need to notify Mali driver about this event */
162                 ret = device->driver->pm->thaw(device);
163         }
164
165         return ret;
166 }
167
168 static const struct dev_pm_ops mali_gpu_device_type_pm_ops = {
169         .suspend = mali_os_suspend,
170         .resume = mali_os_resume,
171         .freeze = mali_os_freeze,
172         .thaw = mali_os_thaw,
173 #ifdef CONFIG_PM_RUNTIME
174         .runtime_suspend = mali_runtime_suspend,
175         .runtime_resume = mali_runtime_resume,
176         .runtime_idle = mali_runtime_idle,
177 #endif
178 };
179
180 static const struct device_type mali_gpu_device_device_type = {
181         .pm = &mali_gpu_device_type_pm_ops,
182 };
183
184 /**
185  * platform_specific_data_of_platform_device_of_mali_gpu.
186  *
187  * 类型 'struct mali_gpu_device_data' 由 common_part 定义,
188  * 实例也将被 common_part 引用,
189  * 比如通知 mali_utilization_event 等.
190  */
191 static const struct mali_gpu_device_data mali_gpu_data = {
192         .shared_mem_size = 1024 * 1024 * 1024, /* 1GB */
193         .fb_start = 0x40000000,
194         .fb_size = 0xb1000000,
195         .max_job_runtime = 60000, /* 60 seconds */
196         /* .utilization_interval = 0, */ /* 0ms */
197         .utilization_callback = mali_gpu_utilization_handler,
198 };
199
200 static void mali_platform_device_add_config(struct platform_device *pdev)
201 {
202         pdev->name = MALI_GPU_NAME_UTGARD,
203         pdev->id = 0;
204         pdev->dev.type = &mali_gpu_device_device_type;
205         pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask,
206         pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
207 }
208
209 /*---------------------------------------------------------------------------*/
210
211 /**
212  * 将被 common_part 回调的, 对 platform_device_of_mali_gpu 初始化的策略回调实现.
213  *
214  * .DP : platform_specific_strategy_callbacks_called_by_common_part,
215  *       platform_specific_strategy_callbacks :
216  *              被 common_part 调用的 平台相关的策略回调.
217  */
218 int mali_platform_device_init(struct platform_device *pdev)
219 {
220         int err = 0;
221         int num_pp_cores = 0;
222
223         MALI_DEBUG_PRINT(2, ("mali_platform_device_register() called\n"));
224
225         if (cpu_is_rk312x())
226                 num_pp_cores = 2;
227         else if (cpu_is_rk3036())
228                 num_pp_cores = 1;
229         else if (cpu_is_rk3188())
230                 num_pp_cores = 4;
231         else if (cpu_is_rk3228())
232                 num_pp_cores = 2;
233
234         mali_platform_device_add_config(pdev);
235
236         /* 将 platform_specific_data 添加到 platform_device_of_mali_gpu.
237          * 这里的 platform_specific_data 的类型由 common_part 定义. */
238         err = platform_device_add_data(pdev, &mali_gpu_data,
239                                        sizeof(mali_gpu_data));
240         if (err == 0) {
241                 /* .KP : 初始化 platform_device_of_mali_gpu 中,
242                  * 仅和 platform_dependent_part 相关的部分. */
243                 err = mali_platform_init(pdev);
244                 if (err == 0) {
245 #ifdef CONFIG_PM_RUNTIME
246                         pm_runtime_set_autosuspend_delay(&(pdev->dev), 1000);
247                         pm_runtime_use_autosuspend(&(pdev->dev));
248                         pm_runtime_enable(&(pdev->dev));
249 #endif
250                         MALI_DEBUG_ASSERT(0 < num_pp_cores);
251                         mali_core_scaling_init(num_pp_cores);
252                         return 0;
253                 }
254         }
255
256         return err;
257 }
258
259 /**
260  * 将被 common_part 回调的, 对 platform_device_of_mali_gpu 终止化的策略回调实现.
261  */
262 void mali_platform_device_deinit(struct platform_device *pdev)
263 {
264         MALI_DEBUG_PRINT(4, ("mali_platform_device_unregister() called\n"));
265
266         mali_platform_deinit(pdev);
267 }