RK3368 GPU: Rogue N Init.
[firefly-linux-kernel-4.4.55.git] / drivers / staging / imgtec / rogue / pvr_gputrace.c
1 /*************************************************************************/ /*!
2 @File           pvr_gputrace.c
3 @Title          PVR GPU Trace module Linux implementation
4 @Copyright      Copyright (c) Imagination Technologies Ltd. All Rights Reserved
5 @License        Dual MIT/GPLv2
6
7 The contents of this file are subject to the MIT license as set out below.
8
9 Permission is hereby granted, free of charge, to any person obtaining a copy
10 of this software and associated documentation files (the "Software"), to deal
11 in the Software without restriction, including without limitation the rights
12 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 copies of the Software, and to permit persons to whom the Software is
14 furnished to do so, subject to the following conditions:
15
16 The above copyright notice and this permission notice shall be included in
17 all copies or substantial portions of the Software.
18
19 Alternatively, the contents of this file may be used under the terms of
20 the GNU General Public License Version 2 ("GPL") in which case the provisions
21 of GPL are applicable instead of those above.
22
23 If you wish to allow use of your version of this file only under the terms of
24 GPL, and not to allow others to use your version of this file under the terms
25 of the MIT license, indicate your decision by deleting the provisions above
26 and replace them with the notice and other provisions required by GPL as set
27 out in the file called "GPL-COPYING" included in this distribution. If you do
28 not delete the provisions above, a recipient may use your version of this file
29 under the terms of either the MIT license or GPL.
30
31 This License is also included in this distribution in the file called
32 "MIT-COPYING".
33
34 EXCEPT AS OTHERWISE STATED IN A NEGOTIATED AGREEMENT: (A) THE SOFTWARE IS
35 PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
36 BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
37 PURPOSE AND NONINFRINGEMENT; AND (B) IN NO EVENT SHALL THE AUTHORS OR
38 COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
39 IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
40 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
41 */ /**************************************************************************/
42
43 #include "pvrsrv_error.h"
44 #include "srvkm.h"
45 #include "pvr_debug.h"
46 #include "pvr_debugfs.h"
47 #include "pvr_uaccess.h"
48
49 #include "pvr_gputrace.h"
50 #include "rgxhwperf.h"
51
52 #include "trace_events.h"
53 #define CREATE_TRACE_POINTS
54 #include <trace/events/gpu.h>
55 #include "rogue_trace_events.h"
56
57
58
59 /******************************************************************************
60  Module internal implementation
61 ******************************************************************************/
62
63 /* DebugFS entry for the feature's on/off file */
64 static PVR_DEBUGFS_ENTRY_DATA *gpsPVRDebugFSGpuTracingOnEntry = NULL;
65
66 /* This variable is set when the gpu tracing is enabled but the HWPerf
67  * resources have not been initialised yet. This may most likely happen
68  * if the driver was built with SUPPORT_KERNEL_SRVINIT=1.
69  * If this variable is IMG_TRUE it means that the gpu tracing was enabled
70  * but the full initialisation is yet to be done. */
71 static IMG_BOOL gbFTraceGPUEventsPreEnabled = IMG_FALSE;
72 /* When this variable is IMG_TRUE it means that the gpu tracing has been fully
73  * initialised and enabled. */
74 static IMG_BOOL gbFTraceGPUEventsEnabled = IMG_FALSE;
75
76 /*
77   If SUPPORT_GPUTRACE_EVENTS is defined the drive is built with support
78   to route RGX HWPerf packets to the Linux FTrace mechanism. To allow
79   this routing feature to be switched on and off at run-time the following
80   debugfs entry is created:
81         /sys/kernel/debug/pvr/gpu_tracing_on
82   To enable GPU events in the FTrace log type the following on the target:
83         echo Y > /sys/kernel/debug/pvr/gpu_tracing_on
84   To disable, type:
85         echo N > /sys/kernel/debug/pvr/gpu_tracing_on
86
87   It is also possible to enable this feature at driver load by setting the
88   default application hint "EnableFTraceGPU=1" in /etc/powervr.ini.
89 */
90
91 static void *GpuTracingSeqStart(struct seq_file *psSeqFile, loff_t *puiPosition)
92 {
93         if (*puiPosition == 0)
94         {
95                 /* We want only one entry in the sequence, one call to show() */
96                 return (void*)1;
97         }
98
99         return NULL;
100 }
101
102
103 static void GpuTracingSeqStop(struct seq_file *psSeqFile, void *pvData)
104 {
105         PVR_UNREFERENCED_PARAMETER(psSeqFile);
106 }
107
108
109 static void *GpuTracingSeqNext(struct seq_file *psSeqFile, void *pvData, loff_t *puiPosition)
110 {
111         PVR_UNREFERENCED_PARAMETER(psSeqFile);
112         return NULL;
113 }
114
115
116 static int GpuTracingSeqShow(struct seq_file *psSeqFile, void *pvData)
117 {
118         const IMG_CHAR *pszInit = "N\n";
119
120         if (gbFTraceGPUEventsEnabled)
121                 // fully operational
122                 pszInit = "Y\n";
123         else if (gbFTraceGPUEventsPreEnabled)
124                 // partially initialised (probably HWPerf not initialised yet)
125                 pszInit = "P\n";
126
127         PVR_UNREFERENCED_PARAMETER(pvData);
128
129         seq_puts(psSeqFile, pszInit);
130         return 0;
131 }
132
133
134 static struct seq_operations gsGpuTracingReadOps =
135 {
136         .start = GpuTracingSeqStart,
137         .stop  = GpuTracingSeqStop,
138         .next  = GpuTracingSeqNext,
139         .show  = GpuTracingSeqShow,
140 };
141
142
143 static IMG_INT GpuTracingSet(const IMG_CHAR *buffer, size_t count, loff_t uiPosition, void *data)
144 {
145         IMG_CHAR cFirstChar;
146
147         PVR_UNREFERENCED_PARAMETER(uiPosition);
148         PVR_UNREFERENCED_PARAMETER(data);
149
150         if (!count)
151         {
152                 return -EINVAL;
153         }
154
155         if (pvr_copy_from_user(&cFirstChar, buffer, 1))
156         {
157                 return -EFAULT;
158         }
159
160         switch (cFirstChar)
161         {
162                 case '0':
163                 case 'n':
164                 case 'N':
165                 {
166                         PVRGpuTraceEnabledSet(IMG_FALSE);
167                         PVR_TRACE(("DISABLED GPU FTrace"));
168                         break;
169                 }
170                 case '1':
171                 case 'y':
172                 case 'Y':
173                 {
174                         if (PVRGpuTraceEnabledSet(IMG_TRUE) == PVRSRV_OK)
175                         {
176                                 PVR_TRACE(("ENABLED GPU FTrace"));
177                         }
178                         else
179                         {
180                                 PVR_TRACE(("FAILED to enable GPU FTrace"));
181                         }
182                         break;
183                 }
184         }
185
186         return count;
187 }
188
189
190 /******************************************************************************
191  Module In-bound API
192 ******************************************************************************/
193
194
195 void PVRGpuTraceClientWork(
196                 const IMG_UINT32 ui32CtxId,
197                 const IMG_UINT32 ui32JobId,
198                 const IMG_CHAR* pszKickType)
199 {
200         PVR_ASSERT(pszKickType);
201
202         PVR_DPF((PVR_DBG_VERBOSE, "PVRGpuTraceClientKick(%s): contextId %u, "
203                 "jobId %u", pszKickType, ui32CtxId, ui32JobId));
204
205         if (PVRGpuTraceEnabled())
206         {
207                 trace_gpu_job_enqueue(ui32CtxId, ui32JobId, pszKickType);
208         }
209 }
210
211
212 void PVRGpuTraceWorkSwitch(
213                 IMG_UINT64 ui64HWTimestampInOSTime,
214                 const IMG_UINT32 ui32CtxId,
215                 const IMG_UINT32 ui32CtxPriority,
216                 const IMG_UINT32 ui32JobId,
217                 const IMG_CHAR* pszWorkType,
218                 PVR_GPUTRACE_SWITCH_TYPE eSwType)
219 {
220         PVR_ASSERT(pszWorkType);
221
222         /* Invert the priority cause this is what systrace expects. Lower values
223          * convey a higher priority to systrace. */
224         trace_gpu_sched_switch(pszWorkType, ui64HWTimestampInOSTime,
225                         eSwType == PVR_GPUTRACE_SWITCH_TYPE_END ? 0 : ui32CtxId,
226                         2-ui32CtxPriority, ui32JobId);
227 }
228
229 void PVRGpuTraceUfo(
230                 IMG_UINT64 ui64OSTimestamp,
231                 const RGX_HWPERF_UFO_EV eEvType,
232                 const IMG_UINT32 ui32ExtJobRef,
233                 const IMG_UINT32 ui32CtxId,
234                 const IMG_UINT32 ui32JobId,
235                 const IMG_UINT32 ui32UFOCount,
236                 const RGX_HWPERF_UFO_DATA_ELEMENT *puData)
237 {
238         switch (eEvType) {
239                 case RGX_HWPERF_UFO_EV_UPDATE:
240                         trace_rogue_ufo_updates(ui64OSTimestamp, ui32CtxId,
241                                 ui32JobId, ui32UFOCount, puData);
242                         break;
243                 case RGX_HWPERF_UFO_EV_CHECK_SUCCESS:
244                         trace_rogue_ufo_checks_success(ui64OSTimestamp, ui32CtxId,
245                                         ui32JobId, IMG_FALSE, ui32UFOCount, puData);
246                         break;
247                 case RGX_HWPERF_UFO_EV_PRCHECK_SUCCESS:
248                         trace_rogue_ufo_checks_success(ui64OSTimestamp, ui32CtxId,
249                                         ui32JobId, IMG_TRUE, ui32UFOCount, puData);
250                         break;
251                 case RGX_HWPERF_UFO_EV_CHECK_FAIL:
252                         trace_rogue_ufo_checks_fail(ui64OSTimestamp, ui32CtxId,
253                                         ui32JobId, IMG_FALSE, ui32UFOCount, puData);
254                         break;
255                 case RGX_HWPERF_UFO_EV_PRCHECK_FAIL:
256                         trace_rogue_ufo_checks_fail(ui64OSTimestamp, ui32CtxId,
257                                         ui32JobId, IMG_TRUE, ui32UFOCount, puData);
258                         break;
259                 default:
260                         break;
261         }
262 }
263
264 void PVRGpuTraceFirmware(
265                 IMG_UINT64 ui64HWTimestampInOSTime,
266                 const IMG_CHAR* pszWorkType,
267                 PVR_GPUTRACE_SWITCH_TYPE eSwType)
268 {
269         trace_rogue_firmware_activity(ui64HWTimestampInOSTime, pszWorkType, eSwType);
270 }
271
272 void PVRGpuTraceEventsLost(
273                 const RGX_HWPERF_STREAM_ID eStreamId,
274                 const IMG_UINT32 ui32LastOrdinal,
275                 const IMG_UINT32 ui32CurrOrdinal)
276 {
277         trace_rogue_events_lost(eStreamId, ui32LastOrdinal, ui32CurrOrdinal);
278 }
279
280 PVRSRV_ERROR PVRGpuTraceInit(PVRSRV_DEVICE_NODE *psDeviceNode)
281 {
282         PVRSRV_ERROR eError;
283
284         eError = RGXHWPerfFTraceGPUInit(psDeviceNode);
285         if (eError != PVRSRV_OK)
286                 return eError;
287
288         eError = PVRDebugFSCreateEntry("gpu_tracing_on", NULL, &gsGpuTracingReadOps,
289                                        (PVRSRV_ENTRY_WRITE_FUNC *)GpuTracingSet,
290                                        NULL, NULL, NULL,
291                                        &gpsPVRDebugFSGpuTracingOnEntry);
292         if (eError != PVRSRV_OK)
293         {
294                 RGXHWPerfFTraceGPUDeInit(psDeviceNode);
295                 return eError;
296         }
297
298         return PVRSRV_OK;
299 }
300
301 void PVRGpuTraceDeInit(PVRSRV_DEVICE_NODE *psDeviceNode)
302 {
303         /* gbFTraceGPUEventsEnabled and gbFTraceGPUEventsPreEnabled are cleared
304          * in this function. */
305         PVRGpuTraceEnabledSet(IMG_FALSE);
306
307         RGXHWPerfFTraceGPUDeInit(psDeviceNode);
308
309         /* Can be NULL if driver startup failed */
310         if (gpsPVRDebugFSGpuTracingOnEntry)
311         {
312                 PVRDebugFSRemoveEntry(&gpsPVRDebugFSGpuTracingOnEntry);
313         }
314 }
315
316 IMG_BOOL PVRGpuTraceEnabled(void)
317 {
318         return gbFTraceGPUEventsEnabled;
319 }
320
321 void PVRGpuTraceSetEnabled(IMG_BOOL bEnabled)
322 {
323         gbFTraceGPUEventsEnabled = bEnabled;
324 }
325
326 IMG_BOOL PVRGpuTracePreEnabled(void)
327 {
328         return gbFTraceGPUEventsPreEnabled;
329 }
330
331 void PVRGpuTraceSetPreEnabled(IMG_BOOL bEnabled)
332 {
333         gbFTraceGPUEventsPreEnabled = bEnabled;
334 }
335
336 /******************************************************************************
337  End of file (pvr_gputrace.c)
338 ******************************************************************************/