From 9d8708e9c132bff98c46ea1064ef815487274a89 Mon Sep 17 00:00:00 2001 From: =?utf8?q?=E6=9D=9C=E5=9D=A4=E6=98=8E?= Date: Mon, 22 Aug 2011 14:27:16 +0800 Subject: [PATCH] gpu: optimize for MapedNonPagedCache alloc --- .../rk29/vivante/hal/inc/gc_hal_options.h | 2 +- .../hal/kernel/gc_hal_kernel_video_memory.c | 4 +- .../hal/os/linux/kernel/gc_hal_kernel_os.c | 202 ++++++++++-------- 3 files changed, 116 insertions(+), 92 deletions(-) mode change 100755 => 100644 drivers/staging/rk29/vivante/hal/inc/gc_hal_options.h mode change 100755 => 100644 drivers/staging/rk29/vivante/hal/os/linux/kernel/gc_hal_kernel_os.c diff --git a/drivers/staging/rk29/vivante/hal/inc/gc_hal_options.h b/drivers/staging/rk29/vivante/hal/inc/gc_hal_options.h old mode 100755 new mode 100644 index 017866608073..be464ed867fd --- a/drivers/staging/rk29/vivante/hal/inc/gc_hal_options.h +++ b/drivers/staging/rk29/vivante/hal/inc/gc_hal_options.h @@ -330,7 +330,7 @@ 0 - no use cache m - The maximum number of cache unit */ -#define gcdkUSE_MAPED_NONPAGE_CACHE 100 +#define gcdkUSE_MAPED_NONPAGE_CACHE 20 #endif /* __gc_hal_options_h_ */ diff --git a/drivers/staging/rk29/vivante/hal/kernel/gc_hal_kernel_video_memory.c b/drivers/staging/rk29/vivante/hal/kernel/gc_hal_kernel_video_memory.c index ca399a0a01c1..0e7be33b3b82 100644 --- a/drivers/staging/rk29/vivante/hal/kernel/gc_hal_kernel_video_memory.c +++ b/drivers/staging/rk29/vivante/hal/kernel/gc_hal_kernel_video_memory.c @@ -831,8 +831,8 @@ gckVIDMEM_AllocateLinear( gcmkONERROR(gcvSTATUS_OUT_OF_MEMORY); } - // dkm: ¶àÔ¤Áô32KµÄ¿Õ¼ä£¬·ñÔòGPU»áÓзÃÎÊ·Ç·¨µØÖ·µÄ·çÏÕ - if (Bytes + (32 << 10) > Memory->freeBytes) + // dkm: ¶àÔ¤Áô64KµÄ¿Õ¼ä£¬·ñÔòGPU»áÓзÃÎÊ·Ç·¨µØÖ·µÄ·çÏÕ + if (Bytes + (64 << 10) > Memory->freeBytes) { /* Not enough memory. */ gcmkONERROR(gcvSTATUS_OUT_OF_MEMORY); diff --git a/drivers/staging/rk29/vivante/hal/os/linux/kernel/gc_hal_kernel_os.c b/drivers/staging/rk29/vivante/hal/os/linux/kernel/gc_hal_kernel_os.c old mode 100755 new mode 100644 index 2c193751b7c4..ae77f39a1b82 --- a/drivers/staging/rk29/vivante/hal/os/linux/kernel/gc_hal_kernel_os.c +++ b/drivers/staging/rk29/vivante/hal/os/linux/kernel/gc_hal_kernel_os.c @@ -86,7 +86,8 @@ gceSTATUS gckOS_FreeNonPagedMemoryRealy( IN gckOS Os, IN gctSIZE_T Bytes, IN gctPHYS_ADDR Physical, - IN gctPOINTER Logical + IN gctPOINTER Logical, + IN gctBOOL MemLock ); @@ -139,7 +140,7 @@ struct _gckOS // dkm : gcdkUSE_MAPED_NONPAGE_CACHE #if gcdkUSE_MAPED_NONPAGE_CACHE - gctUINT cacheNum; + gctINT cacheNum; gcsMapedNonPagedCache * cacheHead; gcsMapedNonPagedCache * cacheTail; #endif @@ -1233,134 +1234,156 @@ gckOS_UnmapMemory( #if gcdkUSE_MAPED_NONPAGE_CACHE static gctBOOL _AddMapedNonPagedCache(gckOS Os, PLINUX_MDL mdl) { - gcsMapedNonPagedCache *cache; + gcsMapedNonPagedCache *cache; + gctINT Num = 0; if(5!=mdl->numPages) return gcvFALSE; - - // add to maped nonpage cache - if(Os->cacheNum >= gcdkUSE_MAPED_NONPAGE_CACHE) { - printk("Add(%d) %d:%d fail!, no space!\n", Os->cacheNum, mdl->pid, mdl->numPages); - return gcvFALSE; + + /* Create Caches */ + if(gcvNULL==Os->cacheHead) + { + while(Num < gcdkUSE_MAPED_NONPAGE_CACHE) + { + cache = (gcsMapedNonPagedCache *)kmalloc(sizeof(gcsMapedNonPagedCache), GFP_ATOMIC); + if(gcvNULL==cache) break; + cache->mdl = gcvNULL; + + /* Add to list */ + if (Os->cacheHead == gcvNULL) { + cache->prev = gcvNULL; + cache->next = gcvNULL; + Os->cacheHead = Os->cacheTail = cache; + } else { + /* Add to the Head. */ + cache->prev = gcvNULL; + cache->next = Os->cacheHead; + Os->cacheHead->prev = cache; + Os->cacheHead = cache; + } + Num++; + } + if(Num < 1) return gcvFALSE; } - cache = (gcsMapedNonPagedCache *)kmalloc(sizeof(gcsMapedNonPagedCache), GFP_ATOMIC); - if(gcvNULL==cache) return gcvFALSE; - - cache->mdl = mdl; - - /* Add to list */ - if (Os->cacheHead == gcvNULL) { - cache->prev = gcvNULL; - cache->next = gcvNULL; - Os->cacheHead = Os->cacheTail = cache; - } else { - /* Add to the Head. */ + /* Get the last and Add it to the Head */ + cache = Os->cacheTail; + if(cache->prev) { + Os->cacheTail = cache->prev; + Os->cacheTail->next = gcvNULL; cache->prev = gcvNULL; cache->next = Os->cacheHead; Os->cacheHead->prev = cache; Os->cacheHead = cache; } - Os->cacheNum++; - - //printk("Add(%d) %d:%d\n", Os->cacheNum, mdl->pid, mdl->numPages); - + if(cache->mdl) { + //printk("Add(%d) pid:%d, But exist pid:%d, free and get it!\n", Os->cacheNum, mdl->pid, cache->mdl->pid); + gckOS_FreeNonPagedMemoryRealy(Os, cache->mdl->numPages, (gctPHYS_ADDR)cache->mdl, (gctPOINTER)cache->mdl->addr, gcvFALSE); + cache->mdl = gcvNULL; + } else { + Os->cacheNum++; + //printk("Add(%d) pid:%d succ!\n", Os->cacheNum, mdl->pid); + } + cache->mdl = mdl; return gcvTRUE; } static PLINUX_MDL _GetMapedNonPagedCache(gckOS Os, gctINT pid, gctINT numPages) { - gcsMapedNonPagedCache *cache; - PLINUX_MDL mdl; - int cnt = 0; + gcsMapedNonPagedCache *cache = gcvNULL; + PLINUX_MDL mdl = gcvNULL; if(5!=numPages) return gcvNULL; - if (Os->cacheHead == gcvNULL) { - //printk("Get(%d) %d:%d fail!, cache empty!\n", Os->cacheNum, pid, numPages); - return gcvNULL; - } - cache = Os->cacheHead; - while (cache != gcvNULL) - { - cnt ++; - if (cache->mdl->pid==pid && cache->mdl->numPages==numPages) break; - cache = cache->next; - } - - if (cache == gcvNULL) { - //printk("Get(%d) %d:%d fail!, no in cache!\n", Os->cacheNum, pid, numPages); - return gcvNULL; - } - - mdl = cache->mdl; - /* Remove the cache from list */ - if (cache == Os->cacheHead) { - Os->cacheHead = cache->next; - if (Os->cacheHead == gcvNULL) Os->cacheTail = gcvNULL; - } else { - cache->prev->next = cache->next; - if (cache == Os->cacheTail) { - Os->cacheTail = cache->prev; - } else { - cache->next->prev = cache->prev; + while (cache != gcvNULL) + { + if (cache->mdl && cache->mdl->pid==pid && cache->mdl->numPages==numPages) + { + /* Move the cache to the Tail */ + if(cache->next) { + if(cache->prev) { + cache->prev->next = cache->next; + cache->next->prev = cache->prev; + } else { + Os->cacheHead = cache->next; + Os->cacheHead->prev = gcvNULL; + } + + cache->prev = Os->cacheTail; + cache->next = gcvNULL; + Os->cacheTail->next = cache; + Os->cacheTail = cache; + } + + mdl = cache->mdl; + cache->mdl = gcvNULL; + Os->cacheNum--; + break; } + cache = cache->next; } - Os->cacheNum--; - kfree(cache); - - //printk("Get(%d) %d:%d -->%d\n", Os->cacheNum, mdl->pid, mdl->numPages, cnt); return mdl; } static void _FreeAllMapedNonPagedCache(gckOS Os, gctINT pid) { - gcsMapedNonPagedCache *cache, *nextCache; + gcsMapedNonPagedCache *cache, *next, *tail; MEMORY_LOCK(Os); cache = Os->cacheHead; + tail = Os->cacheTail; + /* free mdl that pid hit */ while (cache != gcvNULL) { - if (cache != Os->cacheTail) - nextCache = cache->next; - else - nextCache = gcvNULL; - - if(cache->mdl->pid==pid || 0==pid) + next = cache->next; + + if( (cache->mdl) && (cache->mdl->pid==pid || 0==pid)) { - /* Remove the cache from list */ - if (cache == Os->cacheHead) { - Os->cacheHead = cache->next; - if (Os->cacheHead == gcvNULL) Os->cacheTail = gcvNULL; - } else { - cache->prev->next = cache->next; - if (cache == Os->cacheTail) { - Os->cacheTail = cache->prev; - } else { + /* Move the cache to the Tail */ + if(cache->next) { + if(cache->prev) { + cache->prev->next = cache->next; cache->next->prev = cache->prev; + } else { + Os->cacheHead = cache->next; + Os->cacheHead->prev = gcvNULL; } - } - Os->cacheNum--; + + cache->prev = Os->cacheTail; + cache->next = gcvNULL; + Os->cacheTail->next = cache; + Os->cacheTail = cache; + } /* Real Free memory */ - MEMORY_UNLOCK(Os); - gckOS_FreeNonPagedMemoryRealy(Os, cache->mdl->numPages, (gctPHYS_ADDR)cache->mdl, (gctPOINTER)cache->mdl->addr); - MEMORY_LOCK(Os); - - kfree(cache); + gckOS_FreeNonPagedMemoryRealy(Os, cache->mdl->numPages, (gctPHYS_ADDR)cache->mdl, (gctPOINTER)cache->mdl->addr, gcvFALSE); + cache->mdl = gcvNULL; + Os->cacheNum--; } - cache = nextCache; + if(tail == cache) break; + cache = next; } - MEMORY_UNLOCK(Os); + //printk("FreeAll(%d) pid:%d succ!\n", Os->cacheNum, pid); - //printk("_FreeAllMapedNonPagedCache pid=%d, cacheNum=%d\n", pid, Os->cacheNum); + /* destroy all caches */ + if(0 == pid) + { + cache = Os->cacheHead; + while (cache != gcvNULL) + { + next = cache->next; + kfree(cache); + cache = next; + } + } + MEMORY_UNLOCK(Os); } #endif @@ -1755,7 +1778,7 @@ gceSTATUS gckOS_FreeNonPagedMemory( MEMORY_UNLOCK(Os); #endif - return gckOS_FreeNonPagedMemoryRealy(Os, Bytes, Physical, Logical); + return gckOS_FreeNonPagedMemoryRealy(Os, Bytes, Physical, Logical, gcvTRUE); } @@ -1763,7 +1786,8 @@ gceSTATUS gckOS_FreeNonPagedMemoryRealy( IN gckOS Os, IN gctSIZE_T Bytes, IN gctPHYS_ADDR Physical, - IN gctPOINTER Logical + IN gctPOINTER Logical, + IN gctBOOL MemLock ) { PLINUX_MDL mdl; @@ -1790,7 +1814,7 @@ gceSTATUS gckOS_FreeNonPagedMemoryRealy( /* Convert physical address into a pointer to a MDL. */ mdl = (PLINUX_MDL) Physical; - MEMORY_LOCK(Os); + if(MemLock) MEMORY_LOCK(Os); #ifndef NO_DMA_COHERENT dma_free_coherent(gcvNULL, @@ -1879,7 +1903,7 @@ gceSTATUS gckOS_FreeNonPagedMemoryRealy( } } - MEMORY_UNLOCK(Os); + if(MemLock) MEMORY_UNLOCK(Os); gcmkTRACE_ZONE(gcvLEVEL_INFO, gcvZONE_OS, -- 2.34.1