1 /****************************************************************************
3 * Copyright (C) 2005 - 2013 by Vivante Corp.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the license, or
8 * (at your option) any later version.
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.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 *****************************************************************************/
22 #ifndef __gc_hal_kernel_h_
23 #define __gc_hal_kernel_h_
26 #include "gc_hal_kernel_hardware.h"
27 #include "gc_hal_driver.h"
30 #include "gc_hal_kernel_vg.h"
38 /*******************************************************************************
39 ***** New MMU Defination *******************************************************/
40 #define gcdMMU_MTLB_SHIFT 22
41 #define gcdMMU_STLB_4K_SHIFT 12
42 #define gcdMMU_STLB_64K_SHIFT 16
44 #define gcdMMU_MTLB_BITS (32 - gcdMMU_MTLB_SHIFT)
45 #define gcdMMU_PAGE_4K_BITS gcdMMU_STLB_4K_SHIFT
46 #define gcdMMU_STLB_4K_BITS (32 - gcdMMU_MTLB_BITS - gcdMMU_PAGE_4K_BITS)
47 #define gcdMMU_PAGE_64K_BITS gcdMMU_STLB_64K_SHIFT
48 #define gcdMMU_STLB_64K_BITS (32 - gcdMMU_MTLB_BITS - gcdMMU_PAGE_64K_BITS)
50 #define gcdMMU_MTLB_ENTRY_NUM (1 << gcdMMU_MTLB_BITS)
51 #define gcdMMU_MTLB_SIZE (gcdMMU_MTLB_ENTRY_NUM << 2)
52 #define gcdMMU_STLB_4K_ENTRY_NUM (1 << gcdMMU_STLB_4K_BITS)
53 #define gcdMMU_STLB_4K_SIZE (gcdMMU_STLB_4K_ENTRY_NUM << 2)
54 #define gcdMMU_PAGE_4K_SIZE (1 << gcdMMU_STLB_4K_SHIFT)
55 #define gcdMMU_STLB_64K_ENTRY_NUM (1 << gcdMMU_STLB_64K_BITS)
56 #define gcdMMU_STLB_64K_SIZE (gcdMMU_STLB_64K_ENTRY_NUM << 2)
57 #define gcdMMU_PAGE_64K_SIZE (1 << gcdMMU_STLB_64K_SHIFT)
59 #define gcdMMU_MTLB_MASK (~((1U << gcdMMU_MTLB_SHIFT)-1))
60 #define gcdMMU_STLB_4K_MASK ((~0U << gcdMMU_STLB_4K_SHIFT) ^ gcdMMU_MTLB_MASK)
61 #define gcdMMU_PAGE_4K_MASK (gcdMMU_PAGE_4K_SIZE - 1)
62 #define gcdMMU_STLB_64K_MASK ((~((1U << gcdMMU_STLB_64K_SHIFT)-1)) ^ gcdMMU_MTLB_MASK)
63 #define gcdMMU_PAGE_64K_MASK (gcdMMU_PAGE_64K_SIZE - 1)
65 /* Page offset definitions. */
66 #define gcdMMU_OFFSET_4K_BITS (32 - gcdMMU_MTLB_BITS - gcdMMU_STLB_4K_BITS)
67 #define gcdMMU_OFFSET_4K_MASK ((1U << gcdMMU_OFFSET_4K_BITS) - 1)
68 #define gcdMMU_OFFSET_16K_BITS (32 - gcdMMU_MTLB_BITS - gcdMMU_STLB_16K_BITS)
69 #define gcdMMU_OFFSET_16K_MASK ((1U << gcdMMU_OFFSET_16K_BITS) - 1)
71 /*******************************************************************************
72 ***** Process Secure Cache ****************************************************/
74 #define gcdSECURE_CACHE_LRU 1
75 #define gcdSECURE_CACHE_LINEAR 2
76 #define gcdSECURE_CACHE_HASH 3
77 #define gcdSECURE_CACHE_TABLE 4
79 typedef struct _gcskLOGICAL_CACHE * gcskLOGICAL_CACHE_PTR;
80 typedef struct _gcskLOGICAL_CACHE gcskLOGICAL_CACHE;
81 struct _gcskLOGICAL_CACHE
83 /* Logical address. */
86 /* DMAable address. */
89 #if gcdSECURE_CACHE_METHOD == gcdSECURE_CACHE_HASH
90 /* Pointer to the previous and next hash tables. */
91 gcskLOGICAL_CACHE_PTR nextHash;
92 gcskLOGICAL_CACHE_PTR prevHash;
95 #if gcdSECURE_CACHE_METHOD != gcdSECURE_CACHE_TABLE
96 /* Pointer to the previous and next slot. */
97 gcskLOGICAL_CACHE_PTR next;
98 gcskLOGICAL_CACHE_PTR prev;
101 #if gcdSECURE_CACHE_METHOD == gcdSECURE_CACHE_LINEAR
107 typedef struct _gcskSECURE_CACHE * gcskSECURE_CACHE_PTR;
108 typedef struct _gcskSECURE_CACHE
111 gcskLOGICAL_CACHE cache[1 + gcdSECURE_CACHE_SLOTS];
113 /* Last known index for LINEAR mode. */
114 gcskLOGICAL_CACHE_PTR cacheIndex;
116 /* Current free slot for LINEAR mode. */
119 /* Time stamp for LINEAR mode. */
120 gctUINT64 cacheStamp;
122 #if gcdSECURE_CACHE_METHOD == gcdSECURE_CACHE_HASH
123 /* Hash table for HASH mode. */
124 gcskLOGICAL_CACHE hash[256];
129 /*******************************************************************************
130 ***** Process Database Management *********************************************/
132 typedef enum _gceDATABASE_TYPE
134 gcvDB_VIDEO_MEMORY = 1, /* Video memory created. */
135 gcvDB_COMMAND_BUFFER, /* Command Buffer. */
136 gcvDB_NON_PAGED, /* Non paged memory. */
137 gcvDB_CONTIGUOUS, /* Contiguous memory. */
138 gcvDB_SIGNAL, /* Signal. */
139 gcvDB_VIDEO_MEMORY_LOCKED, /* Video memory locked. */
140 gcvDB_CONTEXT, /* Context */
141 gcvDB_IDLE, /* GPU idle. */
142 gcvDB_MAP_MEMORY, /* Map memory */
143 gcvDB_SHARED_INFO, /* Private data */
144 gcvDB_MAP_USER_MEMORY /* Map user memory */
148 typedef struct _gcsDATABASE_RECORD * gcsDATABASE_RECORD_PTR;
149 typedef struct _gcsDATABASE_RECORD
151 /* Pointer to kernel. */
154 /* Pointer to next database record. */
155 gcsDATABASE_RECORD_PTR next;
157 /* Type of record. */
158 gceDATABASE_TYPE type;
160 /* Data for record. */
162 gctPHYS_ADDR physical;
167 typedef struct _gcsDATABASE * gcsDATABASE_PTR;
168 typedef struct _gcsDATABASE
170 /* Pointer to next entry is hash list. */
171 gcsDATABASE_PTR next;
177 /* Sizes to query. */
178 gcsDATABASE_COUNTERS vidMem;
179 gcsDATABASE_COUNTERS nonPaged;
180 gcsDATABASE_COUNTERS contiguous;
181 gcsDATABASE_COUNTERS mapUserMemory;
182 gcsDATABASE_COUNTERS mapMemory;
184 /* Idle time management. */
188 /* Pointer to database. */
189 gcsDATABASE_RECORD_PTR list[48];
193 gcskSECURE_CACHE cache;
196 gctPOINTER handleDatabase;
197 gctPOINTER handleDatabaseMutex;
201 /* Create a process database that will contain all its allocations. */
203 gckKERNEL_CreateProcessDB(
205 IN gctUINT32 ProcessID
208 /* Add a record to the process database. */
210 gckKERNEL_AddProcessDB(
212 IN gctUINT32 ProcessID,
213 IN gceDATABASE_TYPE Type,
214 IN gctPOINTER Pointer,
215 IN gctPHYS_ADDR Physical,
219 /* Remove a record to the process database. */
221 gckKERNEL_RemoveProcessDB(
223 IN gctUINT32 ProcessID,
224 IN gceDATABASE_TYPE Type,
225 IN gctPOINTER Pointer
228 /* Destroy the process database. */
230 gckKERNEL_DestroyProcessDB(
232 IN gctUINT32 ProcessID
235 /* Find a record to the process database. */
237 gckKERNEL_FindProcessDB(
239 IN gctUINT32 ProcessID,
240 IN gctUINT32 ThreadID,
241 IN gceDATABASE_TYPE Type,
242 IN gctPOINTER Pointer,
243 OUT gcsDATABASE_RECORD_PTR Record
246 /* Query the process database. */
248 gckKERNEL_QueryProcessDB(
250 IN gctUINT32 ProcessID,
251 IN gctBOOL LastProcessID,
252 IN gceDATABASE_TYPE Type,
253 OUT gcuDATABASE_INFO * Info
256 /* Dump the process database. */
258 gckKERNEL_DumpProcessDB(
264 gckKERNEL_CreateIntegerDatabase(
266 OUT gctPOINTER * Database
270 gckKERNEL_DestroyIntegerDatabase(
272 IN gctPOINTER Database
276 gckKERNEL_AllocateIntegerId(
277 IN gctPOINTER Database,
278 IN gctPOINTER Pointer,
283 gckKERNEL_FreeIntegerId(
284 IN gctPOINTER Database,
289 gckKERNEL_QueryIntegerId(
290 IN gctPOINTER Database,
292 OUT gctPOINTER * Pointer
296 gckKERNEL_AllocateNameFromPointer(
298 IN gctPOINTER Pointer
302 gckKERNEL_QueryPointerFromName(
308 gckKERNEL_DeleteName(
314 /* Get secure cache from the process database. */
316 gckKERNEL_GetProcessDBCache(
318 IN gctUINT32 ProcessID,
319 OUT gcskSECURE_CACHE_PTR * Cache
323 /*******************************************************************************
324 ********* Timer Management ****************************************************/
325 typedef struct _gcsTIMER * gcsTIMER_PTR;
326 typedef struct _gcsTIMER
328 /* Start and Stop time holders. */
334 /******************************************************************************\
335 ********************************** Structures **********************************
336 \******************************************************************************/
341 /* Database management. */
342 gcsDATABASE_PTR db[16];
344 gcsDATABASE_PTR freeDatabase;
345 gcsDATABASE_RECORD_PTR freeRecord;
346 gcsDATABASE_PTR lastDatabase;
347 gctUINT32 lastProcessID;
350 gctUINT64 lastSlowdown;
351 gctUINT64 lastSlowdownIdle;
352 /* ID - Pointer database*/
353 gctPOINTER pointerDatabase;
354 gctPOINTER pointerDatabaseMutex;
357 #if gcdVIRTUAL_COMMAND_BUFFER
358 typedef struct _gckVIRTUAL_COMMAND_BUFFER * gckVIRTUAL_COMMAND_BUFFER_PTR;
359 typedef struct _gckVIRTUAL_COMMAND_BUFFER
361 gctPHYS_ADDR physical;
362 gctPOINTER userLogical;
363 gctPOINTER kernelLogical;
365 gctPOINTER pageTable;
366 gctUINT32 gpuAddress;
368 gckVIRTUAL_COMMAND_BUFFER_PTR next;
369 gckVIRTUAL_COMMAND_BUFFER_PTR prev;
372 gckVIRTUAL_COMMAND_BUFFER;
375 /* gckKERNEL object. */
381 /* Pointer to gckOS object. */
387 /* Pointer to gckHARDWARE object. */
388 gckHARDWARE hardware;
390 /* Pointer to gckCOMMAND object. */
393 /* Pointer to gckEVENT object. */
396 /* Pointer to context. */
399 /* Pointer to gckMMU object. */
402 /* Arom holding number of clients. */
403 gctPOINTER atomClients;
406 /* Enable profiling */
407 gctBOOL profileEnable;
409 /* The profile file name */
410 gctCHAR profileFileName[gcdMAX_PROFILE_FILE_NAME];
412 /* Clear profile register or not*/
413 gctBOOL profileCleanRegister;
417 #ifdef QNX_SINGLE_THREADED_DEBUGGING
418 gctPOINTER debugMutex;
421 /* Database management. */
425 #if gcdENABLE_RECOVERY
426 gctPOINTER resetFlagClearTimer;
427 gctPOINTER resetAtom;
428 gctUINT64 resetTimeStamp;
431 /* Pointer to gckEVENT object. */
439 #if gcdVIRTUAL_COMMAND_BUFFER
440 gckVIRTUAL_COMMAND_BUFFER_PTR virtualBufferHead;
441 gckVIRTUAL_COMMAND_BUFFER_PTR virtualBufferTail;
442 gctPOINTER virtualBufferLock;
450 struct _FrequencyHistory
456 /* gckDVFS object. */
460 gckHARDWARE hardware;
462 gctUINT32 pollingTime;
464 gctUINT32 totalConfig;
466 gctUINT8 currentScale;
467 struct _FrequencyHistory frequencyHistory[16];
470 /* gckCOMMAND object. */
476 /* Pointer to required object. */
480 /* Number of bytes per page. */
483 /* Current pipe select. */
484 gcePIPE_SELECT pipeSelect;
486 /* Command queue running flag. */
489 /* Idle flag and commit stamp. */
491 gctUINT64 commitStamp;
493 /* Command queue mutex. */
494 gctPOINTER mutexQueue;
496 /* Context switching mutex. */
497 gctPOINTER mutexContext;
499 /* Command queue power semaphore. */
500 gctPOINTER powerSemaphore;
502 /* Current command queue. */
503 struct _gcskCOMMAND_QUEUE
506 gctPHYS_ADDR physical;
509 queues[gcdCOMMAND_QUEUES];
511 gctPHYS_ADDR physical;
515 #if gcmIS_DEBUG(gcdDEBUG_TRACE)
519 /* The command queue is new. */
522 /* Context management. */
523 gckCONTEXT currContext;
525 /* Pointer to last WAIT command. */
526 gctPHYS_ADDR waitPhysical;
527 gctPOINTER waitLogical;
530 /* Command buffer alignment. */
532 gctSIZE_T reservedHead;
533 gctSIZE_T reservedTail;
535 /* Commit counter. */
536 gctPOINTER atomCommit;
538 /* Kernel process ID. */
539 gctUINT32 kernelProcessID;
541 /* End Event signal. */
542 gctSIGNAL endEventSignal;
545 /* Hint array copy buffer. */
546 gctBOOL hintArrayAllocated;
547 gctUINT hintArraySize;
548 gctUINT32_PTR hintArray;
552 typedef struct _gcsEVENT * gcsEVENT_PTR;
554 /* Structure holding one event to be processed. */
555 typedef struct _gcsEVENT
557 /* Pointer to next event in queue. */
560 /* Event information. */
561 gcsHAL_INTERFACE info;
563 /* Process ID owning the event. */
575 /* Structure holding a list of events to be processed by an interrupt. */
576 typedef struct _gcsEVENT_QUEUE * gcsEVENT_QUEUE_PTR;
577 typedef struct _gcsEVENT_QUEUE
582 /* Source of the event. */
583 gceKERNEL_WHERE source;
585 /* Pointer to head of event queue. */
588 /* Pointer to tail of event queue. */
591 /* Next list of events. */
592 gcsEVENT_QUEUE_PTR next;
597 gcdREPO_LIST_COUNT defines the maximum number of event queues with different
598 hardware module sources that may coexist at the same time. Only two sources
599 are supported - gcvKERNEL_COMMAND and gcvKERNEL_PIXEL. gcvKERNEL_COMMAND
600 source is used only for managing the kernel command queue and is only issued
601 when the current command queue gets full. Since we commit event queues every
602 time we commit command buffers, in the worst case we can have up to three
603 pending event queues:
605 - gcvKERNEL_COMMAND (queue overflow)
608 #define gcdREPO_LIST_COUNT 3
610 /* gckEVENT object. */
616 /* Pointer to required objects. */
622 gctUINT64 lastCommitStamp;
625 gctPOINTER eventQueueMutex;
627 /* Array of event queues. */
628 gcsEVENT_QUEUE queues[30];
632 /* Pending events. */
636 volatile gctUINT pending;
639 /* List of free event structures and its mutex. */
640 gcsEVENT_PTR freeEventList;
641 gctSIZE_T freeEventCount;
642 gctPOINTER freeEventMutex;
645 gcsEVENT_QUEUE_PTR queueHead;
646 gcsEVENT_QUEUE_PTR queueTail;
647 gcsEVENT_QUEUE_PTR freeList;
648 gcsEVENT_QUEUE repoList[gcdREPO_LIST_COUNT];
649 gctPOINTER eventListMutex;
651 gctPOINTER submitTimer;
654 /* Free all events belonging to a process. */
656 gckEVENT_FreeProcess(
658 IN gctUINT32 ProcessID
664 IN gctUINT32 ProcessID,
665 IN gctPHYS_ADDR Handle,
666 IN gctPOINTER Logical,
668 IN OUT gctSIZE_T * waitSize
671 /* gcuVIDMEM_NODE structure. */
672 typedef union _gcuVIDMEM_NODE
674 /* Allocated from gckVIDMEM. */
675 struct _gcsVIDMEM_NODE_VIDMEM
677 /* Owner of this node. */
680 /* Dual-linked list of nodes. */
681 gcuVIDMEM_NODE_PTR next;
682 gcuVIDMEM_NODE_PTR prev;
684 /* Dual linked list of free nodes. */
685 gcuVIDMEM_NODE_PTR nextFree;
686 gcuVIDMEM_NODE_PTR prevFree;
688 /* Information for this node. */
694 /* Client/server vaddr (mapped using mmap_join). */
698 /* Locked counter. */
705 /* Process ID owning this memory. */
708 /* Prevent compositor from freeing until client unlocks. */
712 gcsVIDMEM_NODE_SHARED_INFO sharedInfo;
714 #if gcdDYNAMIC_MAP_RESERVED_MEMORY && gcdENABLE_VG
715 gctPOINTER kernelVirtual;
720 /* Allocated from gckOS. */
721 struct _gcsVIDMEM_NODE_VIRTUAL
723 /* Pointer to gckKERNEL object. */
726 /* Information for this node. */
727 /* Contiguously allocated? */
729 /* mdl record pointer... a kmalloc address. Process agnostic. */
730 gctPHYS_ADDR physical;
732 /* do_mmap_pgoff address... mapped per-process. */
735 /* Page table information. */
736 /* Used only when node is not contiguous */
739 /* Used only when node is not contiguous */
740 gctPOINTER pageTables[gcdMAX_GPU_COUNT];
741 /* Pointer to gckKERNEL object who lock this. */
742 gckKERNEL lockKernels[gcdMAX_GPU_COUNT];
743 /* Actual physical address */
744 gctUINT32 addresses[gcdMAX_GPU_COUNT];
749 /* Locked counter. */
750 gctINT32 lockeds[gcdMAX_GPU_COUNT];
753 /* Single linked list of nodes. */
754 gcuVIDMEM_NODE_PTR next;
756 /* Unlock pending flag. */
757 gctBOOL unlockPendings[gcdMAX_GPU_COUNT];
759 /* Free pending flag. */
763 /* Process ID owning this memory. */
766 /* Owner process sets freed to true
767 * when it trys to free a locked
772 gcsVIDMEM_NODE_SHARED_INFO sharedInfo;
778 /* gckVIDMEM object. */
784 /* Pointer to gckOS object. */
787 /* Information for this video memory heap. */
788 gctUINT32 baseAddress;
792 /* Mapping for each type of surface. */
793 gctINT mapping[gcvSURF_NUM_TYPES];
795 /* Sentinel nodes for up to 8 banks. */
796 gcuVIDMEM_NODE sentinel[8];
798 /* Allocation threshold. */
801 /* The heap mutex. */
804 #if gcdUSE_VIDMEM_PER_PID
805 /* The Pid this VidMem belongs to. */
808 struct _gckVIDMEM* next;
818 /* Pointer to gckOS object. */
821 /* Pointer to gckHARDWARE object. */
822 gckHARDWARE hardware;
824 /* The page table mutex. */
825 gctPOINTER pageTableMutex;
827 /* Page table information. */
828 gctSIZE_T pageTableSize;
829 gctPHYS_ADDR pageTablePhysical;
830 gctUINT32_PTR pageTableLogical;
831 gctUINT32 pageTableEntries;
833 /* Master TLB information. */
835 gctPHYS_ADDR mtlbPhysical;
836 gctUINT32_PTR mtlbLogical;
837 gctUINT32 mtlbEntries;
843 gctPOINTER staticSTLB;
846 gctUINT32 dynamicMappingStart;
849 /* Single linked list of all allocated nodes. */
850 gctPOINTER nodeMutex;
851 gcuVIDMEM_NODE_PTR nodeList;
855 #if gcdVIRTUAL_COMMAND_BUFFER
857 gckOS_CreateKernelVirtualMapping(
858 IN gctPHYS_ADDR Physical,
859 OUT gctSIZE_T * PageCount,
860 OUT gctPOINTER * Logical
864 gckOS_DestroyKernelVirtualMapping(
865 IN gctPOINTER Logical
869 gckKERNEL_AllocateVirtualCommandBuffer(
871 IN gctBOOL InUserSpace,
872 IN OUT gctSIZE_T * Bytes,
873 OUT gctPHYS_ADDR * Physical,
874 OUT gctPOINTER * Logical
878 gckKERNEL_DestroyVirtualCommandBuffer(
881 IN gctPHYS_ADDR Physical,
882 IN gctPOINTER Logical
886 gckKERNEL_GetGPUAddress(
888 IN gctPOINTER Logical,
889 OUT gctUINT32 * Address
893 gckKERNEL_QueryGPUAddress(
895 IN gctUINT32 GpuAddress,
896 OUT gckVIRTUAL_COMMAND_BUFFER_PTR * Buffer
901 gckKERNEL_AttachProcess(
907 gckKERNEL_AttachProcessEx(
915 gckKERNEL_MapLogicalToPhysical(
917 IN gcskSECURE_CACHE_PTR Cache,
918 IN OUT gctPOINTER * Data
922 gckKERNEL_FlushTranslationCache(
924 IN gcskSECURE_CACHE_PTR Cache,
925 IN gctPOINTER Logical,
931 gckHARDWARE_QueryIdle(
932 IN gckHARDWARE Hardware,
933 OUT gctBOOL_PTR IsIdle
936 /******************************************************************************\
937 ******************************* gckCONTEXT Object *******************************
938 \******************************************************************************/
941 gckCONTEXT_Construct(
943 IN gckHARDWARE Hardware,
944 IN gctUINT32 ProcessID,
945 OUT gckCONTEXT * Context
950 IN gckCONTEXT Context
955 IN gckCONTEXT Context,
956 IN gctUINT32 ProcessID,
957 IN gcsSTATE_DELTA_PTR StateDelta
960 #if gcdLINK_QUEUE_SIZE
962 gckLINKQUEUE_Enqueue(
963 IN gckLINKQUEUE LinkQueue,
969 gckLINKQUEUE_GetData(
970 IN gckLINKQUEUE LinkQueue,
972 OUT gckLINKDATA * Data
981 #endif /* __gc_hal_kernel_h_ */