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 */
145 gcvDB_SYNC_POINT, /* Sync point. */
146 gcvDB_VIDEO_MEMORY_RESERVED, /* Reserved video memory */
147 gcvDB_VIDEO_MEMORY_CONTIGUOUS, /* Contiguous video memory */
148 gcvDB_VIDEO_MEMORY_VIRTUAL, /* Virtual video memory */
152 typedef struct _gcsDATABASE_RECORD * gcsDATABASE_RECORD_PTR;
153 typedef struct _gcsDATABASE_RECORD
155 /* Pointer to kernel. */
158 /* Pointer to next database record. */
159 gcsDATABASE_RECORD_PTR next;
161 /* Type of record. */
162 gceDATABASE_TYPE type;
164 /* Data for record. */
166 gctPHYS_ADDR physical;
171 typedef struct _gcsDATABASE * gcsDATABASE_PTR;
172 typedef struct _gcsDATABASE
174 /* Pointer to next entry is hash list. */
175 gcsDATABASE_PTR next;
181 /* Sizes to query. */
182 gcsDATABASE_COUNTERS vidMem;
183 gcsDATABASE_COUNTERS nonPaged;
184 gcsDATABASE_COUNTERS contiguous;
185 gcsDATABASE_COUNTERS mapUserMemory;
186 gcsDATABASE_COUNTERS mapMemory;
187 gcsDATABASE_COUNTERS vidMemResv;
188 gcsDATABASE_COUNTERS vidMemCont;
189 gcsDATABASE_COUNTERS vidMemVirt;
191 /* Idle time management. */
195 /* Pointer to database. */
196 gcsDATABASE_RECORD_PTR list[48];
200 gcskSECURE_CACHE cache;
203 gctPOINTER handleDatabase;
204 gctPOINTER handleDatabaseMutex;
208 /* Create a process database that will contain all its allocations. */
210 gckKERNEL_CreateProcessDB(
212 IN gctUINT32 ProcessID
215 /* Add a record to the process database. */
217 gckKERNEL_AddProcessDB(
219 IN gctUINT32 ProcessID,
220 IN gceDATABASE_TYPE Type,
221 IN gctPOINTER Pointer,
222 IN gctPHYS_ADDR Physical,
226 /* Remove a record to the process database. */
228 gckKERNEL_RemoveProcessDB(
230 IN gctUINT32 ProcessID,
231 IN gceDATABASE_TYPE Type,
232 IN gctPOINTER Pointer
235 /* Destroy the process database. */
237 gckKERNEL_DestroyProcessDB(
239 IN gctUINT32 ProcessID
242 /* Find a record to the process database. */
244 gckKERNEL_FindProcessDB(
246 IN gctUINT32 ProcessID,
247 IN gctUINT32 ThreadID,
248 IN gceDATABASE_TYPE Type,
249 IN gctPOINTER Pointer,
250 OUT gcsDATABASE_RECORD_PTR Record
253 /* Query the process database. */
255 gckKERNEL_QueryProcessDB(
257 IN gctUINT32 ProcessID,
258 IN gctBOOL LastProcessID,
259 IN gceDATABASE_TYPE Type,
260 OUT gcuDATABASE_INFO * Info
263 /* Dump the process database. */
265 gckKERNEL_DumpProcessDB(
271 gckKERNEL_CreateIntegerDatabase(
273 OUT gctPOINTER * Database
277 gckKERNEL_DestroyIntegerDatabase(
279 IN gctPOINTER Database
283 gckKERNEL_AllocateIntegerId(
284 IN gctPOINTER Database,
285 IN gctPOINTER Pointer,
290 gckKERNEL_FreeIntegerId(
291 IN gctPOINTER Database,
296 gckKERNEL_QueryIntegerId(
297 IN gctPOINTER Database,
299 OUT gctPOINTER * Pointer
303 gckKERNEL_AllocateNameFromPointer(
305 IN gctPOINTER Pointer
309 gckKERNEL_QueryPointerFromName(
315 gckKERNEL_DeleteName(
321 /* Get secure cache from the process database. */
323 gckKERNEL_GetProcessDBCache(
325 IN gctUINT32 ProcessID,
326 OUT gcskSECURE_CACHE_PTR * Cache
330 /*******************************************************************************
331 ********* Timer Management ****************************************************/
332 typedef struct _gcsTIMER * gcsTIMER_PTR;
333 typedef struct _gcsTIMER
335 /* Start and Stop time holders. */
341 /******************************************************************************\
342 ********************************** Structures **********************************
343 \******************************************************************************/
348 /* Database management. */
349 gcsDATABASE_PTR db[16];
351 gcsDATABASE_PTR freeDatabase;
352 gcsDATABASE_RECORD_PTR freeRecord;
353 gcsDATABASE_PTR lastDatabase;
354 gctUINT32 lastProcessID;
357 gctUINT64 lastSlowdown;
358 gctUINT64 lastSlowdownIdle;
359 /* ID - Pointer database*/
360 gctPOINTER pointerDatabase;
361 gctPOINTER pointerDatabaseMutex;
364 #if gcdVIRTUAL_COMMAND_BUFFER
365 typedef struct _gckVIRTUAL_COMMAND_BUFFER * gckVIRTUAL_COMMAND_BUFFER_PTR;
366 typedef struct _gckVIRTUAL_COMMAND_BUFFER
368 gctPHYS_ADDR physical;
369 gctPOINTER userLogical;
370 gctPOINTER kernelLogical;
372 gctPOINTER pageTable;
373 gctUINT32 gpuAddress;
375 gckVIRTUAL_COMMAND_BUFFER_PTR next;
376 gckVIRTUAL_COMMAND_BUFFER_PTR prev;
379 gckVIRTUAL_COMMAND_BUFFER;
382 /* gckKERNEL object. */
388 /* Pointer to gckOS object. */
394 /* Pointer to gckHARDWARE object. */
395 gckHARDWARE hardware;
397 /* Pointer to gckCOMMAND object. */
400 /* Pointer to gckEVENT object. */
403 /* Pointer to context. */
406 /* Pointer to gckMMU object. */
409 /* Arom holding number of clients. */
410 gctPOINTER atomClients;
413 /* Enable profiling */
414 gctBOOL profileEnable;
416 /* Clear profile register or not*/
417 gctBOOL profileCleanRegister;
421 #ifdef QNX_SINGLE_THREADED_DEBUGGING
422 gctPOINTER debugMutex;
425 /* Database management. */
429 #if gcdENABLE_RECOVERY
430 gctPOINTER resetFlagClearTimer;
431 gctPOINTER resetAtom;
432 gctUINT64 resetTimeStamp;
435 /* Pointer to gckEVENT object. */
443 #if gcdVIRTUAL_COMMAND_BUFFER
444 gckVIRTUAL_COMMAND_BUFFER_PTR virtualBufferHead;
445 gckVIRTUAL_COMMAND_BUFFER_PTR virtualBufferTail;
446 gctPOINTER virtualBufferLock;
453 #if gcdANDROID_NATIVE_FENCE_SYNC
458 struct _FrequencyHistory
464 /* gckDVFS object. */
468 gckHARDWARE hardware;
470 gctUINT32 pollingTime;
472 gctUINT32 totalConfig;
474 gctUINT8 currentScale;
475 struct _FrequencyHistory frequencyHistory[16];
478 /* gckCOMMAND object. */
484 /* Pointer to required object. */
488 /* Number of bytes per page. */
491 /* Current pipe select. */
492 gcePIPE_SELECT pipeSelect;
494 /* Command queue running flag. */
497 /* Idle flag and commit stamp. */
499 gctUINT64 commitStamp;
501 /* Command queue mutex. */
502 gctPOINTER mutexQueue;
504 /* Context switching mutex. */
505 gctPOINTER mutexContext;
507 #if VIVANTE_PROFILER_CONTEXT
508 /* Context sequence mutex. */
509 gctPOINTER mutexContextSeq;
512 /* Command queue power semaphore. */
513 gctPOINTER powerSemaphore;
515 /* Current command queue. */
516 struct _gcskCOMMAND_QUEUE
519 gctPHYS_ADDR physical;
522 queues[gcdCOMMAND_QUEUES];
524 gctPHYS_ADDR physical;
528 #if gcmIS_DEBUG(gcdDEBUG_TRACE)
532 /* The command queue is new. */
535 /* Context management. */
536 gckCONTEXT currContext;
538 /* Pointer to last WAIT command. */
539 gctPHYS_ADDR waitPhysical;
540 gctPOINTER waitLogical;
543 /* Command buffer alignment. */
545 gctSIZE_T reservedHead;
546 gctSIZE_T reservedTail;
548 /* Commit counter. */
549 gctPOINTER atomCommit;
551 /* Kernel process ID. */
552 gctUINT32 kernelProcessID;
554 /* End Event signal. */
555 gctSIGNAL endEventSignal;
558 /* Hint array copy buffer. */
559 gctBOOL hintArrayAllocated;
560 gctUINT hintArraySize;
561 gctUINT32_PTR hintArray;
565 typedef struct _gcsEVENT * gcsEVENT_PTR;
567 /* Structure holding one event to be processed. */
568 typedef struct _gcsEVENT
570 /* Pointer to next event in queue. */
573 /* Event information. */
574 gcsHAL_INTERFACE info;
576 /* Process ID owning the event. */
588 /* Structure holding a list of events to be processed by an interrupt. */
589 typedef struct _gcsEVENT_QUEUE * gcsEVENT_QUEUE_PTR;
590 typedef struct _gcsEVENT_QUEUE
595 /* Source of the event. */
596 gceKERNEL_WHERE source;
598 /* Pointer to head of event queue. */
601 /* Pointer to tail of event queue. */
604 /* Next list of events. */
605 gcsEVENT_QUEUE_PTR next;
610 gcdREPO_LIST_COUNT defines the maximum number of event queues with different
611 hardware module sources that may coexist at the same time. Only two sources
612 are supported - gcvKERNEL_COMMAND and gcvKERNEL_PIXEL. gcvKERNEL_COMMAND
613 source is used only for managing the kernel command queue and is only issued
614 when the current command queue gets full. Since we commit event queues every
615 time we commit command buffers, in the worst case we can have up to three
616 pending event queues:
618 - gcvKERNEL_COMMAND (queue overflow)
621 #define gcdREPO_LIST_COUNT 3
623 /* gckEVENT object. */
629 /* Pointer to required objects. */
635 gctUINT64 lastCommitStamp;
638 gctPOINTER eventQueueMutex;
640 /* Array of event queues. */
641 gcsEVENT_QUEUE queues[30];
645 /* Pending events. */
649 volatile gctUINT pending;
652 /* List of free event structures and its mutex. */
653 gcsEVENT_PTR freeEventList;
654 gctSIZE_T freeEventCount;
655 gctPOINTER freeEventMutex;
658 gcsEVENT_QUEUE_PTR queueHead;
659 gcsEVENT_QUEUE_PTR queueTail;
660 gcsEVENT_QUEUE_PTR freeList;
661 gcsEVENT_QUEUE repoList[gcdREPO_LIST_COUNT];
662 gctPOINTER eventListMutex;
664 gctPOINTER submitTimer;
666 volatile gctBOOL inNotify;
669 /* Free all events belonging to a process. */
671 gckEVENT_FreeProcess(
673 IN gctUINT32 ProcessID
679 IN gctUINT32 ProcessID,
680 IN gctPHYS_ADDR Handle,
681 IN gctPOINTER Logical,
683 IN OUT gctSIZE_T * waitSize
691 /* gcuVIDMEM_NODE structure. */
692 typedef union _gcuVIDMEM_NODE
694 /* Allocated from gckVIDMEM. */
695 struct _gcsVIDMEM_NODE_VIDMEM
697 /* Owner of this node. */
700 /* Dual-linked list of nodes. */
701 gcuVIDMEM_NODE_PTR next;
702 gcuVIDMEM_NODE_PTR prev;
704 /* Dual linked list of free nodes. */
705 gcuVIDMEM_NODE_PTR nextFree;
706 gcuVIDMEM_NODE_PTR prevFree;
708 /* Information for this node. */
714 /* Client/server vaddr (mapped using mmap_join). */
718 /* Locked counter. */
725 /* Process ID owning this memory. */
728 /* Prevent compositor from freeing until client unlocks. */
732 gcsVIDMEM_NODE_SHARED_INFO sharedInfo;
734 #if gcdDYNAMIC_MAP_RESERVED_MEMORY && gcdENABLE_VG
735 gctPOINTER kernelVirtual;
740 /* Allocated from gckOS. */
741 struct _gcsVIDMEM_NODE_VIRTUAL
743 /* Pointer to gckKERNEL object. */
746 /* Information for this node. */
747 /* Contiguously allocated? */
749 /* mdl record pointer... a kmalloc address. Process agnostic. */
750 gctPHYS_ADDR physical;
752 /* do_mmap_pgoff address... mapped per-process. */
755 /* Page table information. */
756 /* Used only when node is not contiguous */
759 /* Used only when node is not contiguous */
760 gctPOINTER pageTables[gcdMAX_GPU_COUNT];
761 /* Pointer to gckKERNEL object who lock this. */
762 gckKERNEL lockKernels[gcdMAX_GPU_COUNT];
763 /* Actual physical address */
764 gctUINT32 addresses[gcdMAX_GPU_COUNT];
769 /* Locked counter. */
770 gctINT32 lockeds[gcdMAX_GPU_COUNT];
773 /* Single linked list of nodes. */
774 gcuVIDMEM_NODE_PTR next;
776 /* Unlock pending flag. */
777 gctBOOL unlockPendings[gcdMAX_GPU_COUNT];
779 /* Free pending flag. */
783 /* Process ID owning this memory. */
786 /* Owner process sets freed to true
787 * when it trys to free a locked
792 gcsVIDMEM_NODE_SHARED_INFO sharedInfo;
798 /* gckVIDMEM object. */
804 /* Pointer to gckOS object. */
807 /* Information for this video memory heap. */
808 gctUINT32 baseAddress;
812 /* Mapping for each type of surface. */
813 gctINT mapping[gcvSURF_NUM_TYPES];
815 /* Sentinel nodes for up to 8 banks. */
816 gcuVIDMEM_NODE sentinel[8];
818 /* Allocation threshold. */
821 /* The heap mutex. */
824 #if gcdUSE_VIDMEM_PER_PID
825 /* The Pid this VidMem belongs to. */
828 struct _gckVIDMEM* next;
838 /* Pointer to gckOS object. */
841 /* Pointer to gckHARDWARE object. */
842 gckHARDWARE hardware;
844 /* The page table mutex. */
845 gctPOINTER pageTableMutex;
847 /* Page table information. */
848 gctSIZE_T pageTableSize;
849 gctPHYS_ADDR pageTablePhysical;
850 gctUINT32_PTR pageTableLogical;
851 gctUINT32 pageTableEntries;
853 /* Master TLB information. */
855 gctPHYS_ADDR mtlbPhysical;
856 gctUINT32_PTR mtlbLogical;
857 gctUINT32 mtlbEntries;
863 gctPOINTER staticSTLB;
866 gctUINT32 dynamicMappingStart;
869 /* Single linked list of all allocated nodes. */
870 gctPOINTER nodeMutex;
871 gcuVIDMEM_NODE_PTR nodeList;
875 #if gcdVIRTUAL_COMMAND_BUFFER
877 gckOS_CreateKernelVirtualMapping(
878 IN gctPHYS_ADDR Physical,
879 OUT gctSIZE_T * PageCount,
880 OUT gctPOINTER * Logical
884 gckOS_DestroyKernelVirtualMapping(
885 IN gctPOINTER Logical
889 gckKERNEL_AllocateVirtualCommandBuffer(
891 IN gctBOOL InUserSpace,
892 IN OUT gctSIZE_T * Bytes,
893 OUT gctPHYS_ADDR * Physical,
894 OUT gctPOINTER * Logical
898 gckKERNEL_DestroyVirtualCommandBuffer(
901 IN gctPHYS_ADDR Physical,
902 IN gctPOINTER Logical
906 gckKERNEL_GetGPUAddress(
908 IN gctPOINTER Logical,
909 OUT gctUINT32 * Address
913 gckKERNEL_QueryGPUAddress(
915 IN gctUINT32 GpuAddress,
916 OUT gckVIRTUAL_COMMAND_BUFFER_PTR * Buffer
921 gckKERNEL_AttachProcess(
927 gckKERNEL_AttachProcessEx(
935 gckKERNEL_MapLogicalToPhysical(
937 IN gcskSECURE_CACHE_PTR Cache,
938 IN OUT gctPOINTER * Data
942 gckKERNEL_FlushTranslationCache(
944 IN gcskSECURE_CACHE_PTR Cache,
945 IN gctPOINTER Logical,
951 gckHARDWARE_QueryIdle(
952 IN gckHARDWARE Hardware,
953 OUT gctBOOL_PTR IsIdle
956 /******************************************************************************\
957 ******************************* gckCONTEXT Object *******************************
958 \******************************************************************************/
961 gckCONTEXT_Construct(
963 IN gckHARDWARE Hardware,
964 IN gctUINT32 ProcessID,
965 OUT gckCONTEXT * Context
970 IN gckCONTEXT Context
975 IN gckCONTEXT Context,
976 IN gctUINT32 ProcessID,
977 IN gcsSTATE_DELTA_PTR StateDelta
980 #if gcdLINK_QUEUE_SIZE
982 gckLINKQUEUE_Enqueue(
983 IN gckLINKQUEUE LinkQueue,
989 gckLINKQUEUE_GetData(
990 IN gckLINKQUEUE LinkQueue,
992 OUT gckLINKDATA * Data
1001 #endif /* __gc_hal_kernel_h_ */