RK3368 GPU: Rogue N Init.
[firefly-linux-kernel-4.4.55.git] / drivers / staging / imgtec / rogue / rgxsrvinit_script.c
1 /*************************************************************************/ /*!
2 @File
3 @Title          Services script routines used at initialisation time
4 @Copyright      Copyright (c) Imagination Technologies Ltd. All Rights Reserved
5 @Description    Device specific functions
6 @License        Dual MIT/GPLv2
7
8 The contents of this file are subject to the MIT license as set out below.
9
10 Permission is hereby granted, free of charge, to any person obtaining a copy
11 of this software and associated documentation files (the "Software"), to deal
12 in the Software without restriction, including without limitation the rights
13 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 copies of the Software, and to permit persons to whom the Software is
15 furnished to do so, subject to the following conditions:
16
17 The above copyright notice and this permission notice shall be included in
18 all copies or substantial portions of the Software.
19
20 Alternatively, the contents of this file may be used under the terms of
21 the GNU General Public License Version 2 ("GPL") in which case the provisions
22 of GPL are applicable instead of those above.
23
24 If you wish to allow use of your version of this file only under the terms of
25 GPL, and not to allow others to use your version of this file under the terms
26 of the MIT license, indicate your decision by deleting the provisions above
27 and replace them with the notice and other provisions required by GPL as set
28 out in the file called "GPL-COPYING" included in this distribution. If you do
29 not delete the provisions above, a recipient may use your version of this file
30 under the terms of either the MIT license or GPL.
31
32 This License is also included in this distribution in the file called
33 "MIT-COPYING".
34
35 EXCEPT AS OTHERWISE STATED IN A NEGOTIATED AGREEMENT: (A) THE SOFTWARE IS
36 PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
37 BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
38 PURPOSE AND NONINFRINGEMENT; AND (B) IN NO EVENT SHALL THE AUTHORS OR
39 COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
40 IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
41 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
42 */ /**************************************************************************/
43
44 #include "rgxsrvinit_script.h"
45 #include "srvinit_osfunc.h"
46 #include "pvr_debug.h"
47
48
49 /*!
50 *******************************************************************************
51
52  @Function     OutOfScriptSpace
53
54  @Description  Checks for script space failure
55
56  @Input        psScript
57
58  @Return       IMG_BOOL
59
60 ******************************************************************************/
61 static IMG_BOOL OutOfScriptSpace(RGX_SCRIPT_BUILD *psScript)
62 {
63         if (psScript->ui32CurrComm >= psScript->ui32MaxLen)
64         {
65                 psScript->bOutOfSpace = IMG_TRUE;
66         }
67
68         return psScript->bOutOfSpace;
69 }
70
71
72 /*!
73 *******************************************************************************
74
75  @Function     NextScriptCommand
76
77  @Description  Gets next script command to populate
78
79  @Input        psScript
80
81  @Return       IMG_BOOL
82
83 ******************************************************************************/
84 static RGX_INIT_COMMAND* NextScriptCommand(RGX_SCRIPT_BUILD *psScript)
85 {
86         if (OutOfScriptSpace(psScript))
87         {
88                 PVR_DPF((PVR_DBG_ERROR, "NextScriptCommand: Out of space for commands (%d)",
89                          psScript->ui32MaxLen));
90                 return NULL;
91         }
92
93         return &psScript->psCommands[psScript->ui32CurrComm++];
94 }
95
96
97 IMG_BOOL ScriptWriteRGXReg(RGX_SCRIPT_BUILD *psScript,
98                            IMG_UINT32 ui32Offset,
99                            IMG_UINT32 ui32Value)
100 {
101         RGX_INIT_COMMAND *psComm = NextScriptCommand(psScript);
102
103         if (psComm != NULL)
104         {
105                 psComm->sWriteHWReg.eOp = RGX_INIT_OP_WRITE_HW_REG;
106                 psComm->sWriteHWReg.ui32Offset = ui32Offset;
107                 psComm->sWriteHWReg.ui32Value = ui32Value;
108
109                 return IMG_TRUE;
110         }
111
112         return IMG_FALSE;
113 }
114
115
116 IMG_BOOL ScriptPoll64RGXReg(RGX_SCRIPT_BUILD *psScript,
117                             IMG_UINT32 ui32Offset,
118                             IMG_UINT64 ui64Value,
119                             IMG_UINT64 ui64PollMask)
120 {
121         RGX_INIT_COMMAND *psComm = NextScriptCommand(psScript);
122
123         if (psComm != NULL)
124         {
125                 psComm->sPoll64HWReg.eOp = RGX_INIT_OP_POLL_64_HW_REG;
126                 psComm->sPoll64HWReg.ui32Offset = ui32Offset;
127                 psComm->sPoll64HWReg.ui64Value = ui64Value;
128                 psComm->sPoll64HWReg.ui64Mask = ui64PollMask;
129                 return IMG_TRUE;
130         }
131
132         return IMG_FALSE;
133 }
134
135
136 IMG_BOOL ScriptPollRGXReg(RGX_SCRIPT_BUILD *psScript,
137                           IMG_UINT32 ui32Offset,
138                           IMG_UINT32 ui32Value,
139                           IMG_UINT32 ui32PollMask)
140 {
141         RGX_INIT_COMMAND *psComm = NextScriptCommand(psScript);
142
143         if (psComm != NULL)
144         {
145                 psComm->sPollHWReg.eOp = RGX_INIT_OP_POLL_HW_REG;
146                 psComm->sPollHWReg.ui32Offset = ui32Offset;
147                 psComm->sPollHWReg.ui32Value = ui32Value;
148                 psComm->sPollHWReg.ui32Mask = ui32PollMask;
149                 return IMG_TRUE;
150         }
151
152         return IMG_FALSE;
153 }
154
155
156 IMG_BOOL ScriptDBGReadRGXReg(RGX_SCRIPT_BUILD *psScript,
157                              RGX_INIT_OPERATION eOp,
158                              IMG_UINT32 ui32Offset,
159                              IMG_CHAR *pszName)
160 {
161         RGX_INIT_COMMAND *psComm = NextScriptCommand(psScript);
162
163         PVR_ASSERT(strlen(pszName) < RGX_DBG_CMD_NAME_SIZE);
164
165         if (psComm != NULL)
166         {
167                 PVR_ASSERT((eOp == RGX_INIT_OP_DBG_READ32_HW_REG) ||
168                            (eOp == RGX_INIT_OP_DBG_READ64_HW_REG));
169
170                 psComm->sDBGReadHWReg.eOp = eOp;
171                 psComm->sDBGReadHWReg.ui32Offset = ui32Offset;
172
173                 strcpy(&psComm->sDBGReadHWReg.aszName[0], pszName);
174
175                 return IMG_TRUE;
176         }
177
178         return IMG_FALSE;
179 }
180
181
182 IMG_BOOL ScriptDBGCalc(RGX_SCRIPT_BUILD *psScript,
183                        RGX_INIT_OPERATION eOp,
184                        IMG_UINT32 ui32Offset1,
185                        IMG_UINT32 ui32Offset2,
186                        IMG_UINT32 ui32Offset3,
187                        IMG_CHAR *pszName)
188 {
189         RGX_INIT_COMMAND *psComm = NextScriptCommand(psScript);
190
191         PVR_ASSERT(strlen(pszName) < RGX_DBG_CMD_NAME_SIZE);
192
193         if (psComm != NULL)
194         {
195                 PVR_ASSERT(eOp == RGX_INIT_OP_DBG_CALC);
196
197                 psComm->sDBGCalc.eOp = eOp;
198                 psComm->sDBGCalc.ui32Offset1 = ui32Offset1;
199                 psComm->sDBGCalc.ui32Offset2 = ui32Offset2;
200                 psComm->sDBGCalc.ui32Offset3 = ui32Offset3;
201                 strcpy(&psComm->sDBGCalc.aszName[0], pszName);
202
203                 return IMG_TRUE;
204         }
205
206         return IMG_FALSE;
207 }
208
209
210 #if defined(RGX_FEATURE_META) || defined(SUPPORT_KERNEL_SRVINIT)
211 IMG_BOOL ScriptWriteRGXRegPDUMPOnly(RGX_SCRIPT_BUILD *psScript,
212                                     IMG_UINT32 ui32Offset,
213                                     IMG_UINT32 ui32Value)
214 {
215         RGX_INIT_COMMAND *psComm = NextScriptCommand(psScript);
216
217         if (psComm != NULL)
218         {
219                 psComm->sPDumpHWReg.eOp = RGX_INIT_OP_PDUMP_HW_REG;
220                 psComm->sPDumpHWReg.ui32Offset = ui32Offset;
221                 psComm->sPDumpHWReg.ui32Value = ui32Value;
222
223                 return IMG_TRUE;
224         }
225
226         return IMG_FALSE;
227 }
228
229
230 /*!
231 *******************************************************************************
232
233  @Function      ScriptPrepareReadMetaRegThroughSP
234
235  @Description   Add script entries for reading a reg through Meta slave port
236
237  @Input         psScript
238  @Input         ui32RegAddr
239
240  @Return        IMG_BOOL
241
242 ******************************************************************************/
243 static IMG_BOOL ScriptPrepareReadMetaRegThroughSP(RGX_SCRIPT_BUILD *psScript,
244                                                   IMG_UINT32 ui32RegAddr)
245 {
246         IMG_BOOL bCmdAdded = IMG_FALSE;
247
248         /* Wait for Slave Port to be Ready */
249         bCmdAdded = ScriptPollRGXReg(psScript,
250                                      RGX_CR_META_SP_MSLVCTRL1,
251                                      RGX_CR_META_SP_MSLVCTRL1_READY_EN |
252                                      RGX_CR_META_SP_MSLVCTRL1_GBLPORT_IDLE_EN,
253                                      RGX_CR_META_SP_MSLVCTRL1_READY_EN |
254                                      RGX_CR_META_SP_MSLVCTRL1_GBLPORT_IDLE_EN);
255         if (!bCmdAdded) return IMG_FALSE;
256         
257         /* Issue a Read */
258         bCmdAdded = ScriptWriteRGXReg(psScript,
259                                       RGX_CR_META_SP_MSLVCTRL0,
260                                       ui32RegAddr | RGX_CR_META_SP_MSLVCTRL0_RD_EN);
261         if (!bCmdAdded) return IMG_FALSE;
262
263         /* Wait for Slave Port to be Ready: read complete */
264         bCmdAdded = ScriptPollRGXReg(psScript,
265                                      RGX_CR_META_SP_MSLVCTRL1,
266                                      RGX_CR_META_SP_MSLVCTRL1_READY_EN |
267                                      RGX_CR_META_SP_MSLVCTRL1_GBLPORT_IDLE_EN,
268                                      RGX_CR_META_SP_MSLVCTRL1_READY_EN |
269                                      RGX_CR_META_SP_MSLVCTRL1_GBLPORT_IDLE_EN);
270
271         return bCmdAdded;
272 }
273
274
275 IMG_BOOL ScriptDBGReadMetaRegThroughSP(RGX_SCRIPT_BUILD *psScript,
276                                        IMG_UINT32 ui32RegAddr,
277                                        IMG_CHAR *pszName)
278 {
279         IMG_BOOL bCmdsAdded = IMG_FALSE;
280
281         /* Issue a Read */
282         bCmdsAdded = ScriptPrepareReadMetaRegThroughSP(psScript, ui32RegAddr);
283         if (!bCmdsAdded) return IMG_FALSE;
284
285         /* Read the value */
286         bCmdsAdded = ScriptDBGReadRGXReg(psScript,
287                                          RGX_INIT_OP_DBG_READ32_HW_REG,
288                                          RGX_CR_META_SP_MSLVDATAX,
289                                          pszName);
290
291         return bCmdsAdded;
292 }
293
294
295 /*!
296 *******************************************************************************
297
298  @Function      ScriptCondPollRGXReg
299
300  @Description   Sets up a script entry for a conditional register poll
301
302  @Input         psScript
303  @Input         ui32CondOffset
304  @Input         ui32CondValue
305  @Input         ui32CondPollMask
306  @Input         ui32Offset
307  @Input         ui32Value
308  @Input         ui32PollMask
309
310  @return        IMG_BOOL
311
312 ******************************************************************************/
313 static IMG_BOOL ScriptCondPollRGXReg(RGX_SCRIPT_BUILD *psScript,
314                                      IMG_UINT32 ui32CondOffset,
315                                      IMG_UINT32 ui32CondValue,
316                                      IMG_UINT32 ui32CondPollMask,
317                                      IMG_UINT32 ui32Offset,
318                                      IMG_UINT32 ui32Value,
319                                      IMG_UINT32 ui32PollMask)
320 {
321         RGX_INIT_COMMAND *psComm = NextScriptCommand(psScript);
322
323         if (psComm != NULL)
324         {
325                 psComm->sCondPollHWReg.eOp = RGX_INIT_OP_COND_POLL_HW_REG;
326                 psComm->sCondPollHWReg.ui32CondOffset = ui32CondOffset;
327                 psComm->sCondPollHWReg.ui32CondValue = ui32CondValue;
328                 psComm->sCondPollHWReg.ui32CondMask = ui32CondPollMask;
329                 psComm->sCondPollHWReg.ui32Offset = ui32Offset;
330                 psComm->sCondPollHWReg.ui32Value = ui32Value;
331                 psComm->sCondPollHWReg.ui32Mask = ui32PollMask;
332                 return IMG_TRUE;
333         }
334
335         return IMG_FALSE;
336 }
337
338
339 IMG_BOOL ScriptMetaRegCondPollRGXReg(RGX_SCRIPT_BUILD *psScript,
340                                      IMG_UINT32 ui32MetaRegAddr,
341                                      IMG_UINT32 ui32MetaRegValue,
342                                      IMG_UINT32 ui32MetaRegMask,
343                                      IMG_UINT32 ui32RegAddr,
344                                      IMG_UINT32 ui32RegValue,
345                                      IMG_UINT32 ui32RegMask)
346 {
347         IMG_BOOL bCmdsAdded = IMG_FALSE;
348
349         /* Issue a Read */
350         bCmdsAdded = ScriptPrepareReadMetaRegThroughSP(psScript, ui32MetaRegAddr);
351         if (!bCmdsAdded) return IMG_FALSE;
352
353         /* Read the value */
354         bCmdsAdded = ScriptCondPollRGXReg(psScript,
355                                           RGX_CR_META_SP_MSLVDATAX,
356                                           ui32MetaRegValue,
357                                           ui32MetaRegMask,
358                                           ui32RegAddr,
359                                           ui32RegValue,
360                                           ui32RegMask);
361
362         return bCmdsAdded;
363 }
364
365
366 IMG_BOOL ScriptWriteMetaRegThroughSP(RGX_SCRIPT_BUILD *psScript,
367                                      IMG_UINT32 ui32RegAddr,
368                                      IMG_UINT32 ui32RegValue)
369 {
370         IMG_BOOL bCmdAdded = IMG_FALSE;
371
372         /* Wait for Slave Port to be Ready */
373         bCmdAdded = ScriptPollRGXReg(psScript,
374                                      RGX_CR_META_SP_MSLVCTRL1,
375                                      RGX_CR_META_SP_MSLVCTRL1_READY_EN |
376                                      RGX_CR_META_SP_MSLVCTRL1_GBLPORT_IDLE_EN,
377                                      RGX_CR_META_SP_MSLVCTRL1_READY_EN |
378                                      RGX_CR_META_SP_MSLVCTRL1_GBLPORT_IDLE_EN);
379         if (!bCmdAdded) return IMG_FALSE;
380
381         /* Issue a Write */
382         bCmdAdded = ScriptWriteRGXReg(psScript,
383                                       RGX_CR_META_SP_MSLVCTRL0,
384                                       ui32RegAddr);
385         if (!bCmdAdded) return IMG_FALSE;
386
387         bCmdAdded = ScriptWriteRGXReg(psScript,
388                                       RGX_CR_META_SP_MSLVDATAT,
389                                       ui32RegValue);
390
391         /* Wait for complete to be done on the next attempt to read/write */
392
393         return bCmdAdded;
394 }
395
396
397 /*!
398 *******************************************************************************
399
400  @Function      ScriptInsertLoopPoint
401
402  @Description   Inserts a loop point in the startup script
403
404  @Input         psScript
405
406  @Return        IMG_BOOL
407
408 ******************************************************************************/
409 static IMG_BOOL ScriptInsertLoopPoint(RGX_SCRIPT_BUILD *psScript)
410 {
411         RGX_INIT_COMMAND *psComm = NextScriptCommand(psScript);
412
413         if (psComm != NULL)
414         {
415                 psComm->eOp = RGX_INIT_OP_LOOP_POINT;
416                 return IMG_TRUE;
417         }
418
419         return IMG_FALSE;
420 }
421
422
423 /*!
424 *******************************************************************************
425
426  @Function      ScriptConditionalBranchOnReg
427
428  @Description   Conditionally branches back to the last loop point in the script.
429                 Condition is satisfied by the contents of a register
430
431  @Input         psScript
432  @Input         ui32Offset
433  @Input         ui32Value
434  @Input         ui32Mask
435
436  @Return        IMG_BOOL
437
438 ******************************************************************************/
439 static IMG_BOOL ScriptConditionalBranchOnReg(RGX_SCRIPT_BUILD *psScript,
440                                              IMG_UINT32 ui32Offset,
441                                              IMG_UINT32 ui32Value,
442                                              IMG_UINT32 ui32Mask)
443 {
444         RGX_INIT_COMMAND *psComm = NextScriptCommand(psScript);
445
446         if (psComm != NULL)
447         {
448                 psComm->eOp = RGX_INIT_OP_COND_BRANCH;
449                 psComm->sConditionalBranchPoint.ui32Offset = ui32Offset;
450                 psComm->sConditionalBranchPoint.ui32Value = ui32Value;
451                 psComm->sConditionalBranchPoint.ui32Mask = ui32Mask;
452                 return IMG_TRUE;
453         }
454
455         return IMG_FALSE;
456 }
457
458
459 IMG_BOOL ScriptPollMetaRegThroughSP(RGX_SCRIPT_BUILD *psScript,
460                                     IMG_UINT32 ui32Offset,
461                                     IMG_UINT32 ui32PollValue,
462                                     IMG_UINT32 ui32PollMask)
463 {
464         IMG_BOOL bCmdsAdded = IMG_FALSE;
465
466         bCmdsAdded = ScriptInsertLoopPoint(psScript);
467         if (!bCmdsAdded) return IMG_FALSE;
468
469         bCmdsAdded = ScriptPrepareReadMetaRegThroughSP(psScript, ui32Offset);
470         if (!bCmdsAdded) return IMG_FALSE;
471
472         bCmdsAdded = ScriptConditionalBranchOnReg(psScript,
473                                                   RGX_CR_META_SP_MSLVDATAX,
474                                                   ui32PollValue,
475                                                   ui32PollMask);
476         return bCmdsAdded;
477 }
478
479
480 IMG_BOOL ScriptDBGReadMetaCoreReg(RGX_SCRIPT_BUILD *psScript,
481                                   IMG_UINT32 ui32RegAddr,
482                                   IMG_CHAR *pszName)
483 {
484         IMG_BOOL bCmdsAdded = IMG_FALSE;
485
486         /* Core Read Ready? */
487         bCmdsAdded = ScriptPollMetaRegThroughSP(psScript,
488                                                 META_CR_TXUXXRXRQ_OFFSET,
489                                                 META_CR_TXUXXRXRQ_DREADY_BIT,
490                                                 META_CR_TXUXXRXRQ_DREADY_BIT);
491
492         /* Set the reg we are interested in reading */
493         bCmdsAdded = ScriptWriteMetaRegThroughSP(psScript,
494                                                  META_CR_TXUXXRXRQ_OFFSET,
495                                                  ui32RegAddr | META_CR_TXUXXRXRQ_RDnWR_BIT);
496         if (!bCmdsAdded) return IMG_FALSE;
497
498         /* Core Read Done? */
499         bCmdsAdded = ScriptPollMetaRegThroughSP(psScript,
500                                                 META_CR_TXUXXRXRQ_OFFSET,
501                                                 META_CR_TXUXXRXRQ_DREADY_BIT,
502                                                 META_CR_TXUXXRXRQ_DREADY_BIT);
503
504         /* Read the value */
505         ScriptDBGReadMetaRegThroughSP(psScript, META_CR_TXUXXRXDT_OFFSET, pszName);
506
507         return IMG_TRUE;
508
509 }
510 #endif /* RGX_FEATURE_META */
511
512
513 IMG_BOOL ScriptDBGString(RGX_SCRIPT_BUILD *psScript,
514                          const IMG_CHAR *aszString)
515 {
516         RGX_INIT_COMMAND *psComm = NextScriptCommand(psScript);
517
518         if (psComm != NULL)
519         {
520                 psComm->sDBGString.eOp = RGX_INIT_OP_DBG_STRING;
521                 strcpy(psComm->sDBGString.aszString, aszString);
522                 if (strlen(aszString) >= (sizeof(psComm->sDBGString.aszString) - 2))
523                 {
524                         psComm->sDBGString.aszString[RGX_DBG_CMD_NAME_SIZE-1] = '\0';
525                 }
526                 return IMG_TRUE;
527         }
528
529         return IMG_FALSE;
530 }
531
532
533 IMG_BOOL ScriptHalt(RGX_SCRIPT_BUILD *psScript)
534 {
535         RGX_INIT_COMMAND *psComm = NextScriptCommand(psScript);
536
537         if (psComm != NULL)
538         {
539                 psComm->eOp = RGX_INIT_OP_HALT;
540                 return IMG_TRUE;
541         }
542
543         return IMG_FALSE;
544 }
545
546
547 /******************************************************************************
548  End of file (rgxsrvinit_script.c)
549 ******************************************************************************/