mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
synced 2025-01-18 02:46:06 +00:00
0069455bcb
Patch series "Memory allocation profiling", v6. Overview: Low overhead [1] per-callsite memory allocation profiling. Not just for debug kernels, overhead low enough to be deployed in production. Example output: root@moria-kvm:~# sort -rn /proc/allocinfo 127664128 31168 mm/page_ext.c:270 func:alloc_page_ext 56373248 4737 mm/slub.c:2259 func:alloc_slab_page 14880768 3633 mm/readahead.c:247 func:page_cache_ra_unbounded 14417920 3520 mm/mm_init.c:2530 func:alloc_large_system_hash 13377536 234 block/blk-mq.c:3421 func:blk_mq_alloc_rqs 11718656 2861 mm/filemap.c:1919 func:__filemap_get_folio 9192960 2800 kernel/fork.c:307 func:alloc_thread_stack_node 4206592 4 net/netfilter/nf_conntrack_core.c:2567 func:nf_ct_alloc_hashtable 4136960 1010 drivers/staging/ctagmod/ctagmod.c:20 [ctagmod] func:ctagmod_start 3940352 962 mm/memory.c:4214 func:alloc_anon_folio 2894464 22613 fs/kernfs/dir.c:615 func:__kernfs_new_node ... Usage: kconfig options: - CONFIG_MEM_ALLOC_PROFILING - CONFIG_MEM_ALLOC_PROFILING_ENABLED_BY_DEFAULT - CONFIG_MEM_ALLOC_PROFILING_DEBUG adds warnings for allocations that weren't accounted because of a missing annotation sysctl: /proc/sys/vm/mem_profiling Runtime info: /proc/allocinfo Notes: [1]: Overhead To measure the overhead we are comparing the following configurations: (1) Baseline with CONFIG_MEMCG_KMEM=n (2) Disabled by default (CONFIG_MEM_ALLOC_PROFILING=y && CONFIG_MEM_ALLOC_PROFILING_BY_DEFAULT=n) (3) Enabled by default (CONFIG_MEM_ALLOC_PROFILING=y && CONFIG_MEM_ALLOC_PROFILING_BY_DEFAULT=y) (4) Enabled at runtime (CONFIG_MEM_ALLOC_PROFILING=y && CONFIG_MEM_ALLOC_PROFILING_BY_DEFAULT=n && /proc/sys/vm/mem_profiling=1) (5) Baseline with CONFIG_MEMCG_KMEM=y && allocating with __GFP_ACCOUNT (6) Disabled by default (CONFIG_MEM_ALLOC_PROFILING=y && CONFIG_MEM_ALLOC_PROFILING_BY_DEFAULT=n) && CONFIG_MEMCG_KMEM=y (7) Enabled by default (CONFIG_MEM_ALLOC_PROFILING=y && CONFIG_MEM_ALLOC_PROFILING_BY_DEFAULT=y) && CONFIG_MEMCG_KMEM=y Performance overhead: To evaluate performance we implemented an in-kernel test executing multiple get_free_page/free_page and kmalloc/kfree calls with allocation sizes growing from 8 to 240 bytes with CPU frequency set to max and CPU affinity set to a specific CPU to minimize the noise. Below are results from running the test on Ubuntu 22.04.2 LTS with 6.8.0-rc1 kernel on 56 core Intel Xeon: kmalloc pgalloc (1 baseline) 6.764s 16.902s (2 default disabled) 6.793s (+0.43%) 17.007s (+0.62%) (3 default enabled) 7.197s (+6.40%) 23.666s (+40.02%) (4 runtime enabled) 7.405s (+9.48%) 23.901s (+41.41%) (5 memcg) 13.388s (+97.94%) 48.460s (+186.71%) (6 def disabled+memcg) 13.332s (+97.10%) 48.105s (+184.61%) (7 def enabled+memcg) 13.446s (+98.78%) 54.963s (+225.18%) Memory overhead: Kernel size: text data bss dec diff (1) 26515311 18890222 17018880 62424413 (2) 26524728 19423818 16740352 62688898 264485 (3) 26524724 19423818 16740352 62688894 264481 (4) 26524728 19423818 16740352 62688898 264485 (5) 26541782 18964374 16957440 62463596 39183 Memory consumption on a 56 core Intel CPU with 125GB of memory: Code tags: 192 kB PageExts: 262144 kB (256MB) SlabExts: 9876 kB (9.6MB) PcpuExts: 512 kB (0.5MB) Total overhead is 0.2% of total memory. Benchmarks: Hackbench tests run 100 times: hackbench -s 512 -l 200 -g 15 -f 25 -P baseline disabled profiling enabled profiling avg 0.3543 0.3559 (+0.0016) 0.3566 (+0.0023) stdev 0.0137 0.0188 0.0077 hackbench -l 10000 baseline disabled profiling enabled profiling avg 6.4218 6.4306 (+0.0088) 6.5077 (+0.0859) stdev 0.0933 0.0286 0.0489 stress-ng tests: stress-ng --class memory --seq 4 -t 60 stress-ng --class cpu --seq 4 -t 60 Results posted at: https://evilpiepirate.org/~kent/memalloc_prof_v4_stress-ng/ [2] https://lore.kernel.org/all/20240306182440.2003814-1-surenb@google.com/ This patch (of 37): The next patch drops vmalloc.h from a system header in order to fix a circular dependency; this adds it to all the files that were pulling it in implicitly. [kent.overstreet@linux.dev: fix arch/alpha/lib/memcpy.c] Link: https://lkml.kernel.org/r/20240327002152.3339937-1-kent.overstreet@linux.dev [surenb@google.com: fix arch/x86/mm/numa_32.c] Link: https://lkml.kernel.org/r/20240402180933.1663992-1-surenb@google.com [kent.overstreet@linux.dev: a few places were depending on sizes.h] Link: https://lkml.kernel.org/r/20240404034744.1664840-1-kent.overstreet@linux.dev [arnd@arndb.de: fix mm/kasan/hw_tags.c] Link: https://lkml.kernel.org/r/20240404124435.3121534-1-arnd@kernel.org [surenb@google.com: fix arc build] Link: https://lkml.kernel.org/r/20240405225115.431056-1-surenb@google.com Link: https://lkml.kernel.org/r/20240321163705.3067592-1-surenb@google.com Link: https://lkml.kernel.org/r/20240321163705.3067592-2-surenb@google.com Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev> Signed-off-by: Suren Baghdasaryan <surenb@google.com> Signed-off-by: Arnd Bergmann <arnd@arndb.de> Reviewed-by: Pasha Tatashin <pasha.tatashin@soleen.com> Tested-by: Kees Cook <keescook@chromium.org> Cc: Alexander Viro <viro@zeniv.linux.org.uk> Cc: Alex Gaynor <alex.gaynor@gmail.com> Cc: Alice Ryhl <aliceryhl@google.com> Cc: Andreas Hindborg <a.hindborg@samsung.com> Cc: Benno Lossin <benno.lossin@proton.me> Cc: "Björn Roy Baron" <bjorn3_gh@protonmail.com> Cc: Boqun Feng <boqun.feng@gmail.com> Cc: Christoph Lameter <cl@linux.com> Cc: Dennis Zhou <dennis@kernel.org> Cc: Gary Guo <gary@garyguo.net> Cc: Miguel Ojeda <ojeda@kernel.org> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Tejun Heo <tj@kernel.org> Cc: Vlastimil Babka <vbabka@suse.cz> Cc: Wedson Almeida Filho <wedsonaf@gmail.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
774 lines
17 KiB
C
774 lines
17 KiB
C
// SPDX-License-Identifier: GPL-2.0-only
|
|
/**************************************************************************
|
|
* Copyright (c) 2007, Intel Corporation.
|
|
*
|
|
**************************************************************************/
|
|
|
|
#include <linux/highmem.h>
|
|
#include <linux/vmalloc.h>
|
|
|
|
#include "mmu.h"
|
|
#include "psb_drv.h"
|
|
#include "psb_reg.h"
|
|
|
|
/*
|
|
* Code for the SGX MMU:
|
|
*/
|
|
|
|
/*
|
|
* clflush on one processor only:
|
|
* clflush should apparently flush the cache line on all processors in an
|
|
* SMP system.
|
|
*/
|
|
|
|
/*
|
|
* kmap atomic:
|
|
* The usage of the slots must be completely encapsulated within a spinlock, and
|
|
* no other functions that may be using the locks for other purposed may be
|
|
* called from within the locked region.
|
|
* Since the slots are per processor, this will guarantee that we are the only
|
|
* user.
|
|
*/
|
|
|
|
/*
|
|
* TODO: Inserting ptes from an interrupt handler:
|
|
* This may be desirable for some SGX functionality where the GPU can fault in
|
|
* needed pages. For that, we need to make an atomic insert_pages function, that
|
|
* may fail.
|
|
* If it fails, the caller need to insert the page using a workqueue function,
|
|
* but on average it should be fast.
|
|
*/
|
|
|
|
static inline uint32_t psb_mmu_pt_index(uint32_t offset)
|
|
{
|
|
return (offset >> PSB_PTE_SHIFT) & 0x3FF;
|
|
}
|
|
|
|
static inline uint32_t psb_mmu_pd_index(uint32_t offset)
|
|
{
|
|
return offset >> PSB_PDE_SHIFT;
|
|
}
|
|
|
|
static inline void psb_clflush(void *addr)
|
|
{
|
|
__asm__ __volatile__("clflush (%0)\n" : : "r"(addr) : "memory");
|
|
}
|
|
|
|
static inline void psb_mmu_clflush(struct psb_mmu_driver *driver, void *addr)
|
|
{
|
|
if (!driver->has_clflush)
|
|
return;
|
|
|
|
mb();
|
|
psb_clflush(addr);
|
|
mb();
|
|
}
|
|
|
|
static void psb_mmu_flush_pd_locked(struct psb_mmu_driver *driver, int force)
|
|
{
|
|
struct drm_device *dev = driver->dev;
|
|
struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
|
|
|
|
if (atomic_read(&driver->needs_tlbflush) || force) {
|
|
uint32_t val = PSB_RSGX32(PSB_CR_BIF_CTRL);
|
|
PSB_WSGX32(val | _PSB_CB_CTRL_INVALDC, PSB_CR_BIF_CTRL);
|
|
|
|
/* Make sure data cache is turned off before enabling it */
|
|
wmb();
|
|
PSB_WSGX32(val & ~_PSB_CB_CTRL_INVALDC, PSB_CR_BIF_CTRL);
|
|
(void)PSB_RSGX32(PSB_CR_BIF_CTRL);
|
|
if (driver->msvdx_mmu_invaldc)
|
|
atomic_set(driver->msvdx_mmu_invaldc, 1);
|
|
}
|
|
atomic_set(&driver->needs_tlbflush, 0);
|
|
}
|
|
|
|
#if 0
|
|
static void psb_mmu_flush_pd(struct psb_mmu_driver *driver, int force)
|
|
{
|
|
down_write(&driver->sem);
|
|
psb_mmu_flush_pd_locked(driver, force);
|
|
up_write(&driver->sem);
|
|
}
|
|
#endif
|
|
|
|
void psb_mmu_flush(struct psb_mmu_driver *driver)
|
|
{
|
|
struct drm_device *dev = driver->dev;
|
|
struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
|
|
uint32_t val;
|
|
|
|
down_write(&driver->sem);
|
|
val = PSB_RSGX32(PSB_CR_BIF_CTRL);
|
|
if (atomic_read(&driver->needs_tlbflush))
|
|
PSB_WSGX32(val | _PSB_CB_CTRL_INVALDC, PSB_CR_BIF_CTRL);
|
|
else
|
|
PSB_WSGX32(val | _PSB_CB_CTRL_FLUSH, PSB_CR_BIF_CTRL);
|
|
|
|
/* Make sure data cache is turned off and MMU is flushed before
|
|
restoring bank interface control register */
|
|
wmb();
|
|
PSB_WSGX32(val & ~(_PSB_CB_CTRL_FLUSH | _PSB_CB_CTRL_INVALDC),
|
|
PSB_CR_BIF_CTRL);
|
|
(void)PSB_RSGX32(PSB_CR_BIF_CTRL);
|
|
|
|
atomic_set(&driver->needs_tlbflush, 0);
|
|
if (driver->msvdx_mmu_invaldc)
|
|
atomic_set(driver->msvdx_mmu_invaldc, 1);
|
|
up_write(&driver->sem);
|
|
}
|
|
|
|
void psb_mmu_set_pd_context(struct psb_mmu_pd *pd, int hw_context)
|
|
{
|
|
struct drm_device *dev = pd->driver->dev;
|
|
struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
|
|
uint32_t offset = (hw_context == 0) ? PSB_CR_BIF_DIR_LIST_BASE0 :
|
|
PSB_CR_BIF_DIR_LIST_BASE1 + hw_context * 4;
|
|
|
|
down_write(&pd->driver->sem);
|
|
PSB_WSGX32(page_to_pfn(pd->p) << PAGE_SHIFT, offset);
|
|
wmb();
|
|
psb_mmu_flush_pd_locked(pd->driver, 1);
|
|
pd->hw_context = hw_context;
|
|
up_write(&pd->driver->sem);
|
|
|
|
}
|
|
|
|
static inline unsigned long psb_pd_addr_end(unsigned long addr,
|
|
unsigned long end)
|
|
{
|
|
addr = (addr + PSB_PDE_MASK + 1) & ~PSB_PDE_MASK;
|
|
return (addr < end) ? addr : end;
|
|
}
|
|
|
|
static inline uint32_t psb_mmu_mask_pte(uint32_t pfn, int type)
|
|
{
|
|
uint32_t mask = PSB_PTE_VALID;
|
|
|
|
if (type & PSB_MMU_CACHED_MEMORY)
|
|
mask |= PSB_PTE_CACHED;
|
|
if (type & PSB_MMU_RO_MEMORY)
|
|
mask |= PSB_PTE_RO;
|
|
if (type & PSB_MMU_WO_MEMORY)
|
|
mask |= PSB_PTE_WO;
|
|
|
|
return (pfn << PAGE_SHIFT) | mask;
|
|
}
|
|
|
|
struct psb_mmu_pd *psb_mmu_alloc_pd(struct psb_mmu_driver *driver,
|
|
int trap_pagefaults, int invalid_type)
|
|
{
|
|
struct psb_mmu_pd *pd = kmalloc(sizeof(*pd), GFP_KERNEL);
|
|
uint32_t *v;
|
|
int i;
|
|
|
|
if (!pd)
|
|
return NULL;
|
|
|
|
pd->p = alloc_page(GFP_DMA32);
|
|
if (!pd->p)
|
|
goto out_err1;
|
|
pd->dummy_pt = alloc_page(GFP_DMA32);
|
|
if (!pd->dummy_pt)
|
|
goto out_err2;
|
|
pd->dummy_page = alloc_page(GFP_DMA32);
|
|
if (!pd->dummy_page)
|
|
goto out_err3;
|
|
|
|
if (!trap_pagefaults) {
|
|
pd->invalid_pde = psb_mmu_mask_pte(page_to_pfn(pd->dummy_pt),
|
|
invalid_type);
|
|
pd->invalid_pte = psb_mmu_mask_pte(page_to_pfn(pd->dummy_page),
|
|
invalid_type);
|
|
} else {
|
|
pd->invalid_pde = 0;
|
|
pd->invalid_pte = 0;
|
|
}
|
|
|
|
v = kmap_local_page(pd->dummy_pt);
|
|
for (i = 0; i < (PAGE_SIZE / sizeof(uint32_t)); ++i)
|
|
v[i] = pd->invalid_pte;
|
|
|
|
kunmap_local(v);
|
|
|
|
v = kmap_local_page(pd->p);
|
|
for (i = 0; i < (PAGE_SIZE / sizeof(uint32_t)); ++i)
|
|
v[i] = pd->invalid_pde;
|
|
|
|
kunmap_local(v);
|
|
|
|
clear_page(kmap(pd->dummy_page));
|
|
kunmap(pd->dummy_page);
|
|
|
|
pd->tables = vmalloc_user(sizeof(struct psb_mmu_pt *) * 1024);
|
|
if (!pd->tables)
|
|
goto out_err4;
|
|
|
|
pd->hw_context = -1;
|
|
pd->pd_mask = PSB_PTE_VALID;
|
|
pd->driver = driver;
|
|
|
|
return pd;
|
|
|
|
out_err4:
|
|
__free_page(pd->dummy_page);
|
|
out_err3:
|
|
__free_page(pd->dummy_pt);
|
|
out_err2:
|
|
__free_page(pd->p);
|
|
out_err1:
|
|
kfree(pd);
|
|
return NULL;
|
|
}
|
|
|
|
static void psb_mmu_free_pt(struct psb_mmu_pt *pt)
|
|
{
|
|
__free_page(pt->p);
|
|
kfree(pt);
|
|
}
|
|
|
|
void psb_mmu_free_pagedir(struct psb_mmu_pd *pd)
|
|
{
|
|
struct psb_mmu_driver *driver = pd->driver;
|
|
struct drm_device *dev = driver->dev;
|
|
struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
|
|
struct psb_mmu_pt *pt;
|
|
int i;
|
|
|
|
down_write(&driver->sem);
|
|
if (pd->hw_context != -1) {
|
|
PSB_WSGX32(0, PSB_CR_BIF_DIR_LIST_BASE0 + pd->hw_context * 4);
|
|
psb_mmu_flush_pd_locked(driver, 1);
|
|
}
|
|
|
|
/* Should take the spinlock here, but we don't need to do that
|
|
since we have the semaphore in write mode. */
|
|
|
|
for (i = 0; i < 1024; ++i) {
|
|
pt = pd->tables[i];
|
|
if (pt)
|
|
psb_mmu_free_pt(pt);
|
|
}
|
|
|
|
vfree(pd->tables);
|
|
__free_page(pd->dummy_page);
|
|
__free_page(pd->dummy_pt);
|
|
__free_page(pd->p);
|
|
kfree(pd);
|
|
up_write(&driver->sem);
|
|
}
|
|
|
|
static struct psb_mmu_pt *psb_mmu_alloc_pt(struct psb_mmu_pd *pd)
|
|
{
|
|
struct psb_mmu_pt *pt = kmalloc(sizeof(*pt), GFP_KERNEL);
|
|
void *v;
|
|
uint32_t clflush_add = pd->driver->clflush_add >> PAGE_SHIFT;
|
|
uint32_t clflush_count = PAGE_SIZE / clflush_add;
|
|
spinlock_t *lock = &pd->driver->lock;
|
|
uint8_t *clf;
|
|
uint32_t *ptes;
|
|
int i;
|
|
|
|
if (!pt)
|
|
return NULL;
|
|
|
|
pt->p = alloc_page(GFP_DMA32);
|
|
if (!pt->p) {
|
|
kfree(pt);
|
|
return NULL;
|
|
}
|
|
|
|
spin_lock(lock);
|
|
|
|
v = kmap_atomic(pt->p);
|
|
clf = (uint8_t *) v;
|
|
ptes = (uint32_t *) v;
|
|
for (i = 0; i < (PAGE_SIZE / sizeof(uint32_t)); ++i)
|
|
*ptes++ = pd->invalid_pte;
|
|
|
|
if (pd->driver->has_clflush && pd->hw_context != -1) {
|
|
mb();
|
|
for (i = 0; i < clflush_count; ++i) {
|
|
psb_clflush(clf);
|
|
clf += clflush_add;
|
|
}
|
|
mb();
|
|
}
|
|
kunmap_atomic(v);
|
|
spin_unlock(lock);
|
|
|
|
pt->count = 0;
|
|
pt->pd = pd;
|
|
pt->index = 0;
|
|
|
|
return pt;
|
|
}
|
|
|
|
static struct psb_mmu_pt *psb_mmu_pt_alloc_map_lock(struct psb_mmu_pd *pd,
|
|
unsigned long addr)
|
|
{
|
|
uint32_t index = psb_mmu_pd_index(addr);
|
|
struct psb_mmu_pt *pt;
|
|
uint32_t *v;
|
|
spinlock_t *lock = &pd->driver->lock;
|
|
|
|
spin_lock(lock);
|
|
pt = pd->tables[index];
|
|
while (!pt) {
|
|
spin_unlock(lock);
|
|
pt = psb_mmu_alloc_pt(pd);
|
|
if (!pt)
|
|
return NULL;
|
|
spin_lock(lock);
|
|
|
|
if (pd->tables[index]) {
|
|
spin_unlock(lock);
|
|
psb_mmu_free_pt(pt);
|
|
spin_lock(lock);
|
|
pt = pd->tables[index];
|
|
continue;
|
|
}
|
|
|
|
v = kmap_atomic(pd->p);
|
|
pd->tables[index] = pt;
|
|
v[index] = (page_to_pfn(pt->p) << 12) | pd->pd_mask;
|
|
pt->index = index;
|
|
kunmap_atomic((void *) v);
|
|
|
|
if (pd->hw_context != -1) {
|
|
psb_mmu_clflush(pd->driver, (void *)&v[index]);
|
|
atomic_set(&pd->driver->needs_tlbflush, 1);
|
|
}
|
|
}
|
|
pt->v = kmap_atomic(pt->p);
|
|
return pt;
|
|
}
|
|
|
|
static struct psb_mmu_pt *psb_mmu_pt_map_lock(struct psb_mmu_pd *pd,
|
|
unsigned long addr)
|
|
{
|
|
uint32_t index = psb_mmu_pd_index(addr);
|
|
struct psb_mmu_pt *pt;
|
|
spinlock_t *lock = &pd->driver->lock;
|
|
|
|
spin_lock(lock);
|
|
pt = pd->tables[index];
|
|
if (!pt) {
|
|
spin_unlock(lock);
|
|
return NULL;
|
|
}
|
|
pt->v = kmap_atomic(pt->p);
|
|
return pt;
|
|
}
|
|
|
|
static void psb_mmu_pt_unmap_unlock(struct psb_mmu_pt *pt)
|
|
{
|
|
struct psb_mmu_pd *pd = pt->pd;
|
|
uint32_t *v;
|
|
|
|
kunmap_atomic(pt->v);
|
|
if (pt->count == 0) {
|
|
v = kmap_atomic(pd->p);
|
|
v[pt->index] = pd->invalid_pde;
|
|
pd->tables[pt->index] = NULL;
|
|
|
|
if (pd->hw_context != -1) {
|
|
psb_mmu_clflush(pd->driver, (void *)&v[pt->index]);
|
|
atomic_set(&pd->driver->needs_tlbflush, 1);
|
|
}
|
|
kunmap_atomic(v);
|
|
spin_unlock(&pd->driver->lock);
|
|
psb_mmu_free_pt(pt);
|
|
return;
|
|
}
|
|
spin_unlock(&pd->driver->lock);
|
|
}
|
|
|
|
static inline void psb_mmu_set_pte(struct psb_mmu_pt *pt, unsigned long addr,
|
|
uint32_t pte)
|
|
{
|
|
pt->v[psb_mmu_pt_index(addr)] = pte;
|
|
}
|
|
|
|
static inline void psb_mmu_invalidate_pte(struct psb_mmu_pt *pt,
|
|
unsigned long addr)
|
|
{
|
|
pt->v[psb_mmu_pt_index(addr)] = pt->pd->invalid_pte;
|
|
}
|
|
|
|
struct psb_mmu_pd *psb_mmu_get_default_pd(struct psb_mmu_driver *driver)
|
|
{
|
|
struct psb_mmu_pd *pd;
|
|
|
|
down_read(&driver->sem);
|
|
pd = driver->default_pd;
|
|
up_read(&driver->sem);
|
|
|
|
return pd;
|
|
}
|
|
|
|
void psb_mmu_driver_takedown(struct psb_mmu_driver *driver)
|
|
{
|
|
struct drm_device *dev = driver->dev;
|
|
struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
|
|
|
|
PSB_WSGX32(driver->bif_ctrl, PSB_CR_BIF_CTRL);
|
|
psb_mmu_free_pagedir(driver->default_pd);
|
|
kfree(driver);
|
|
}
|
|
|
|
struct psb_mmu_driver *psb_mmu_driver_init(struct drm_device *dev,
|
|
int trap_pagefaults,
|
|
int invalid_type,
|
|
atomic_t *msvdx_mmu_invaldc)
|
|
{
|
|
struct psb_mmu_driver *driver;
|
|
struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
|
|
|
|
driver = kmalloc(sizeof(*driver), GFP_KERNEL);
|
|
|
|
if (!driver)
|
|
return NULL;
|
|
|
|
driver->dev = dev;
|
|
driver->default_pd = psb_mmu_alloc_pd(driver, trap_pagefaults,
|
|
invalid_type);
|
|
if (!driver->default_pd)
|
|
goto out_err1;
|
|
|
|
spin_lock_init(&driver->lock);
|
|
init_rwsem(&driver->sem);
|
|
down_write(&driver->sem);
|
|
atomic_set(&driver->needs_tlbflush, 1);
|
|
driver->msvdx_mmu_invaldc = msvdx_mmu_invaldc;
|
|
|
|
driver->bif_ctrl = PSB_RSGX32(PSB_CR_BIF_CTRL);
|
|
PSB_WSGX32(driver->bif_ctrl | _PSB_CB_CTRL_CLEAR_FAULT,
|
|
PSB_CR_BIF_CTRL);
|
|
PSB_WSGX32(driver->bif_ctrl & ~_PSB_CB_CTRL_CLEAR_FAULT,
|
|
PSB_CR_BIF_CTRL);
|
|
|
|
driver->has_clflush = 0;
|
|
|
|
if (boot_cpu_has(X86_FEATURE_CLFLUSH)) {
|
|
uint32_t tfms, misc, cap0, cap4, clflush_size;
|
|
|
|
/*
|
|
* clflush size is determined at kernel setup for x86_64 but not
|
|
* for i386. We have to do it here.
|
|
*/
|
|
|
|
cpuid(0x00000001, &tfms, &misc, &cap0, &cap4);
|
|
clflush_size = ((misc >> 8) & 0xff) * 8;
|
|
driver->has_clflush = 1;
|
|
driver->clflush_add =
|
|
PAGE_SIZE * clflush_size / sizeof(uint32_t);
|
|
driver->clflush_mask = driver->clflush_add - 1;
|
|
driver->clflush_mask = ~driver->clflush_mask;
|
|
}
|
|
|
|
up_write(&driver->sem);
|
|
return driver;
|
|
|
|
out_err1:
|
|
kfree(driver);
|
|
return NULL;
|
|
}
|
|
|
|
static void psb_mmu_flush_ptes(struct psb_mmu_pd *pd, unsigned long address,
|
|
uint32_t num_pages, uint32_t desired_tile_stride,
|
|
uint32_t hw_tile_stride)
|
|
{
|
|
struct psb_mmu_pt *pt;
|
|
uint32_t rows = 1;
|
|
uint32_t i;
|
|
unsigned long addr;
|
|
unsigned long end;
|
|
unsigned long next;
|
|
unsigned long add;
|
|
unsigned long row_add;
|
|
unsigned long clflush_add = pd->driver->clflush_add;
|
|
unsigned long clflush_mask = pd->driver->clflush_mask;
|
|
|
|
if (!pd->driver->has_clflush)
|
|
return;
|
|
|
|
if (hw_tile_stride)
|
|
rows = num_pages / desired_tile_stride;
|
|
else
|
|
desired_tile_stride = num_pages;
|
|
|
|
add = desired_tile_stride << PAGE_SHIFT;
|
|
row_add = hw_tile_stride << PAGE_SHIFT;
|
|
mb();
|
|
for (i = 0; i < rows; ++i) {
|
|
|
|
addr = address;
|
|
end = addr + add;
|
|
|
|
do {
|
|
next = psb_pd_addr_end(addr, end);
|
|
pt = psb_mmu_pt_map_lock(pd, addr);
|
|
if (!pt)
|
|
continue;
|
|
do {
|
|
psb_clflush(&pt->v[psb_mmu_pt_index(addr)]);
|
|
} while (addr += clflush_add,
|
|
(addr & clflush_mask) < next);
|
|
|
|
psb_mmu_pt_unmap_unlock(pt);
|
|
} while (addr = next, next != end);
|
|
address += row_add;
|
|
}
|
|
mb();
|
|
}
|
|
|
|
void psb_mmu_remove_pfn_sequence(struct psb_mmu_pd *pd,
|
|
unsigned long address, uint32_t num_pages)
|
|
{
|
|
struct psb_mmu_pt *pt;
|
|
unsigned long addr;
|
|
unsigned long end;
|
|
unsigned long next;
|
|
unsigned long f_address = address;
|
|
|
|
down_read(&pd->driver->sem);
|
|
|
|
addr = address;
|
|
end = addr + (num_pages << PAGE_SHIFT);
|
|
|
|
do {
|
|
next = psb_pd_addr_end(addr, end);
|
|
pt = psb_mmu_pt_alloc_map_lock(pd, addr);
|
|
if (!pt)
|
|
goto out;
|
|
do {
|
|
psb_mmu_invalidate_pte(pt, addr);
|
|
--pt->count;
|
|
} while (addr += PAGE_SIZE, addr < next);
|
|
psb_mmu_pt_unmap_unlock(pt);
|
|
|
|
} while (addr = next, next != end);
|
|
|
|
out:
|
|
if (pd->hw_context != -1)
|
|
psb_mmu_flush_ptes(pd, f_address, num_pages, 1, 1);
|
|
|
|
up_read(&pd->driver->sem);
|
|
|
|
if (pd->hw_context != -1)
|
|
psb_mmu_flush(pd->driver);
|
|
|
|
return;
|
|
}
|
|
|
|
void psb_mmu_remove_pages(struct psb_mmu_pd *pd, unsigned long address,
|
|
uint32_t num_pages, uint32_t desired_tile_stride,
|
|
uint32_t hw_tile_stride)
|
|
{
|
|
struct psb_mmu_pt *pt;
|
|
uint32_t rows = 1;
|
|
uint32_t i;
|
|
unsigned long addr;
|
|
unsigned long end;
|
|
unsigned long next;
|
|
unsigned long add;
|
|
unsigned long row_add;
|
|
unsigned long f_address = address;
|
|
|
|
if (hw_tile_stride)
|
|
rows = num_pages / desired_tile_stride;
|
|
else
|
|
desired_tile_stride = num_pages;
|
|
|
|
add = desired_tile_stride << PAGE_SHIFT;
|
|
row_add = hw_tile_stride << PAGE_SHIFT;
|
|
|
|
down_read(&pd->driver->sem);
|
|
|
|
/* Make sure we only need to flush this processor's cache */
|
|
|
|
for (i = 0; i < rows; ++i) {
|
|
|
|
addr = address;
|
|
end = addr + add;
|
|
|
|
do {
|
|
next = psb_pd_addr_end(addr, end);
|
|
pt = psb_mmu_pt_map_lock(pd, addr);
|
|
if (!pt)
|
|
continue;
|
|
do {
|
|
psb_mmu_invalidate_pte(pt, addr);
|
|
--pt->count;
|
|
|
|
} while (addr += PAGE_SIZE, addr < next);
|
|
psb_mmu_pt_unmap_unlock(pt);
|
|
|
|
} while (addr = next, next != end);
|
|
address += row_add;
|
|
}
|
|
if (pd->hw_context != -1)
|
|
psb_mmu_flush_ptes(pd, f_address, num_pages,
|
|
desired_tile_stride, hw_tile_stride);
|
|
|
|
up_read(&pd->driver->sem);
|
|
|
|
if (pd->hw_context != -1)
|
|
psb_mmu_flush(pd->driver);
|
|
}
|
|
|
|
int psb_mmu_insert_pfn_sequence(struct psb_mmu_pd *pd, uint32_t start_pfn,
|
|
unsigned long address, uint32_t num_pages,
|
|
int type)
|
|
{
|
|
struct psb_mmu_pt *pt;
|
|
uint32_t pte;
|
|
unsigned long addr;
|
|
unsigned long end;
|
|
unsigned long next;
|
|
unsigned long f_address = address;
|
|
int ret = -ENOMEM;
|
|
|
|
down_read(&pd->driver->sem);
|
|
|
|
addr = address;
|
|
end = addr + (num_pages << PAGE_SHIFT);
|
|
|
|
do {
|
|
next = psb_pd_addr_end(addr, end);
|
|
pt = psb_mmu_pt_alloc_map_lock(pd, addr);
|
|
if (!pt) {
|
|
ret = -ENOMEM;
|
|
goto out;
|
|
}
|
|
do {
|
|
pte = psb_mmu_mask_pte(start_pfn++, type);
|
|
psb_mmu_set_pte(pt, addr, pte);
|
|
pt->count++;
|
|
} while (addr += PAGE_SIZE, addr < next);
|
|
psb_mmu_pt_unmap_unlock(pt);
|
|
|
|
} while (addr = next, next != end);
|
|
ret = 0;
|
|
|
|
out:
|
|
if (pd->hw_context != -1)
|
|
psb_mmu_flush_ptes(pd, f_address, num_pages, 1, 1);
|
|
|
|
up_read(&pd->driver->sem);
|
|
|
|
if (pd->hw_context != -1)
|
|
psb_mmu_flush(pd->driver);
|
|
|
|
return ret;
|
|
}
|
|
|
|
int psb_mmu_insert_pages(struct psb_mmu_pd *pd, struct page **pages,
|
|
unsigned long address, uint32_t num_pages,
|
|
uint32_t desired_tile_stride, uint32_t hw_tile_stride,
|
|
int type)
|
|
{
|
|
struct psb_mmu_pt *pt;
|
|
uint32_t rows = 1;
|
|
uint32_t i;
|
|
uint32_t pte;
|
|
unsigned long addr;
|
|
unsigned long end;
|
|
unsigned long next;
|
|
unsigned long add;
|
|
unsigned long row_add;
|
|
unsigned long f_address = address;
|
|
int ret = -ENOMEM;
|
|
|
|
if (hw_tile_stride) {
|
|
if (num_pages % desired_tile_stride != 0)
|
|
return -EINVAL;
|
|
rows = num_pages / desired_tile_stride;
|
|
} else {
|
|
desired_tile_stride = num_pages;
|
|
}
|
|
|
|
add = desired_tile_stride << PAGE_SHIFT;
|
|
row_add = hw_tile_stride << PAGE_SHIFT;
|
|
|
|
down_read(&pd->driver->sem);
|
|
|
|
for (i = 0; i < rows; ++i) {
|
|
|
|
addr = address;
|
|
end = addr + add;
|
|
|
|
do {
|
|
next = psb_pd_addr_end(addr, end);
|
|
pt = psb_mmu_pt_alloc_map_lock(pd, addr);
|
|
if (!pt)
|
|
goto out;
|
|
do {
|
|
pte = psb_mmu_mask_pte(page_to_pfn(*pages++),
|
|
type);
|
|
psb_mmu_set_pte(pt, addr, pte);
|
|
pt->count++;
|
|
} while (addr += PAGE_SIZE, addr < next);
|
|
psb_mmu_pt_unmap_unlock(pt);
|
|
|
|
} while (addr = next, next != end);
|
|
|
|
address += row_add;
|
|
}
|
|
|
|
ret = 0;
|
|
out:
|
|
if (pd->hw_context != -1)
|
|
psb_mmu_flush_ptes(pd, f_address, num_pages,
|
|
desired_tile_stride, hw_tile_stride);
|
|
|
|
up_read(&pd->driver->sem);
|
|
|
|
if (pd->hw_context != -1)
|
|
psb_mmu_flush(pd->driver);
|
|
|
|
return ret;
|
|
}
|
|
|
|
int psb_mmu_virtual_to_pfn(struct psb_mmu_pd *pd, uint32_t virtual,
|
|
unsigned long *pfn)
|
|
{
|
|
int ret;
|
|
struct psb_mmu_pt *pt;
|
|
uint32_t tmp;
|
|
spinlock_t *lock = &pd->driver->lock;
|
|
|
|
down_read(&pd->driver->sem);
|
|
pt = psb_mmu_pt_map_lock(pd, virtual);
|
|
if (!pt) {
|
|
uint32_t *v;
|
|
|
|
spin_lock(lock);
|
|
v = kmap_atomic(pd->p);
|
|
tmp = v[psb_mmu_pd_index(virtual)];
|
|
kunmap_atomic(v);
|
|
spin_unlock(lock);
|
|
|
|
if (tmp != pd->invalid_pde || !(tmp & PSB_PTE_VALID) ||
|
|
!(pd->invalid_pte & PSB_PTE_VALID)) {
|
|
ret = -EINVAL;
|
|
goto out;
|
|
}
|
|
ret = 0;
|
|
*pfn = pd->invalid_pte >> PAGE_SHIFT;
|
|
goto out;
|
|
}
|
|
tmp = pt->v[psb_mmu_pt_index(virtual)];
|
|
if (!(tmp & PSB_PTE_VALID)) {
|
|
ret = -EINVAL;
|
|
} else {
|
|
ret = 0;
|
|
*pfn = tmp >> PAGE_SHIFT;
|
|
}
|
|
psb_mmu_pt_unmap_unlock(pt);
|
|
out:
|
|
up_read(&pd->driver->sem);
|
|
return ret;
|
|
}
|