📄 mm.c
字号:
/* * PS3 address space management. * * Copyright (C) 2006 Sony Computer Entertainment Inc. * Copyright 2006 Sony Corp. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; version 2 of the License. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */#include <linux/kernel.h>#include <linux/module.h>#include <linux/memory_hotplug.h>#include <asm/firmware.h>#include <asm/lmb.h>#include <asm/udbg.h>#include <asm/lv1call.h>#include "platform.h"#if defined(DEBUG)#define DBG udbg_printf#else#define DBG pr_debug#endifenum {#if defined(CONFIG_PS3_USE_LPAR_ADDR) USE_LPAR_ADDR = 1,#else USE_LPAR_ADDR = 0,#endif#if defined(CONFIG_PS3_DYNAMIC_DMA) USE_DYNAMIC_DMA = 1,#else USE_DYNAMIC_DMA = 0,#endif};enum { PAGE_SHIFT_4K = 12U, PAGE_SHIFT_64K = 16U, PAGE_SHIFT_16M = 24U,};static unsigned long make_page_sizes(unsigned long a, unsigned long b){ return (a << 56) | (b << 48);}enum { ALLOCATE_MEMORY_TRY_ALT_UNIT = 0X04, ALLOCATE_MEMORY_ADDR_ZERO = 0X08,};/* valid htab sizes are {18,19,20} = 256K, 512K, 1M */enum { HTAB_SIZE_MAX = 20U, /* HV limit of 1MB */ HTAB_SIZE_MIN = 18U, /* CPU limit of 256KB */};/*============================================================================*//* virtual address space routines *//*============================================================================*//** * struct mem_region - memory region structure * @base: base address * @size: size in bytes * @offset: difference between base and rm.size */struct mem_region { unsigned long base; unsigned long size; unsigned long offset;};/** * struct map - address space state variables holder * @total: total memory available as reported by HV * @vas_id - HV virtual address space id * @htab_size: htab size in bytes * * The HV virtual address space (vas) allows for hotplug memory regions. * Memory regions can be created and destroyed in the vas at runtime. * @rm: real mode (bootmem) region * @r1: hotplug memory region(s) * * ps3 addresses * virt_addr: a cpu 'translated' effective address * phys_addr: an address in what Linux thinks is the physical address space * lpar_addr: an address in the HV virtual address space * bus_addr: an io controller 'translated' address on a device bus */struct map { unsigned long total; unsigned long vas_id; unsigned long htab_size; struct mem_region rm; struct mem_region r1;};#define debug_dump_map(x) _debug_dump_map(x, __func__, __LINE__)static void __maybe_unused _debug_dump_map(const struct map *m, const char *func, int line){ DBG("%s:%d: map.total = %lxh\n", func, line, m->total); DBG("%s:%d: map.rm.size = %lxh\n", func, line, m->rm.size); DBG("%s:%d: map.vas_id = %lu\n", func, line, m->vas_id); DBG("%s:%d: map.htab_size = %lxh\n", func, line, m->htab_size); DBG("%s:%d: map.r1.base = %lxh\n", func, line, m->r1.base); DBG("%s:%d: map.r1.offset = %lxh\n", func, line, m->r1.offset); DBG("%s:%d: map.r1.size = %lxh\n", func, line, m->r1.size);}static struct map map;/** * ps3_mm_phys_to_lpar - translate a linux physical address to lpar address * @phys_addr: linux physical address */unsigned long ps3_mm_phys_to_lpar(unsigned long phys_addr){ BUG_ON(is_kernel_addr(phys_addr)); if (USE_LPAR_ADDR) return phys_addr; else return (phys_addr < map.rm.size || phys_addr >= map.total) ? phys_addr : phys_addr + map.r1.offset;}EXPORT_SYMBOL(ps3_mm_phys_to_lpar);/** * ps3_mm_vas_create - create the virtual address space */void __init ps3_mm_vas_create(unsigned long* htab_size){ int result; unsigned long start_address; unsigned long size; unsigned long access_right; unsigned long max_page_size; unsigned long flags; result = lv1_query_logical_partition_address_region_info(0, &start_address, &size, &access_right, &max_page_size, &flags); if (result) { DBG("%s:%d: lv1_query_logical_partition_address_region_info " "failed: %s\n", __func__, __LINE__, ps3_result(result)); goto fail; } if (max_page_size < PAGE_SHIFT_16M) { DBG("%s:%d: bad max_page_size %lxh\n", __func__, __LINE__, max_page_size); goto fail; } BUILD_BUG_ON(CONFIG_PS3_HTAB_SIZE > HTAB_SIZE_MAX); BUILD_BUG_ON(CONFIG_PS3_HTAB_SIZE < HTAB_SIZE_MIN); result = lv1_construct_virtual_address_space(CONFIG_PS3_HTAB_SIZE, 2, make_page_sizes(PAGE_SHIFT_16M, PAGE_SHIFT_64K), &map.vas_id, &map.htab_size); if (result) { DBG("%s:%d: lv1_construct_virtual_address_space failed: %s\n", __func__, __LINE__, ps3_result(result)); goto fail; } result = lv1_select_virtual_address_space(map.vas_id); if (result) { DBG("%s:%d: lv1_select_virtual_address_space failed: %s\n", __func__, __LINE__, ps3_result(result)); goto fail; } *htab_size = map.htab_size; debug_dump_map(&map); return;fail: panic("ps3_mm_vas_create failed");}/** * ps3_mm_vas_destroy - */void ps3_mm_vas_destroy(void){ int result; DBG("%s:%d: map.vas_id = %lu\n", __func__, __LINE__, map.vas_id); if (map.vas_id) { result = lv1_select_virtual_address_space(0); BUG_ON(result); result = lv1_destruct_virtual_address_space(map.vas_id); BUG_ON(result); map.vas_id = 0; }}/*============================================================================*//* memory hotplug routines *//*============================================================================*//** * ps3_mm_region_create - create a memory region in the vas * @r: pointer to a struct mem_region to accept initialized values * @size: requested region size * * This implementation creates the region with the vas large page size. * @size is rounded down to a multiple of the vas large page size. */static int ps3_mm_region_create(struct mem_region *r, unsigned long size){ int result; unsigned long muid; r->size = _ALIGN_DOWN(size, 1 << PAGE_SHIFT_16M); DBG("%s:%d requested %lxh\n", __func__, __LINE__, size); DBG("%s:%d actual %lxh\n", __func__, __LINE__, r->size); DBG("%s:%d difference %lxh (%luMB)\n", __func__, __LINE__, (unsigned long)(size - r->size), (size - r->size) / 1024 / 1024); if (r->size == 0) { DBG("%s:%d: size == 0\n", __func__, __LINE__); result = -1; goto zero_region; } result = lv1_allocate_memory(r->size, PAGE_SHIFT_16M, 0, ALLOCATE_MEMORY_TRY_ALT_UNIT, &r->base, &muid); if (result || r->base < map.rm.size) { DBG("%s:%d: lv1_allocate_memory failed: %s\n", __func__, __LINE__, ps3_result(result)); goto zero_region; } r->offset = r->base - map.rm.size; return result;zero_region: r->size = r->base = r->offset = 0; return result;}/** * ps3_mm_region_destroy - destroy a memory region * @r: pointer to struct mem_region */static void ps3_mm_region_destroy(struct mem_region *r){ int result; DBG("%s:%d: r->base = %lxh\n", __func__, __LINE__, r->base); if (r->base) { result = lv1_release_memory(r->base); BUG_ON(result); r->size = r->base = r->offset = 0; map.total = map.rm.size; }}/** * ps3_mm_add_memory - hot add memory */static int __init ps3_mm_add_memory(void){ int result; unsigned long start_addr; unsigned long start_pfn; unsigned long nr_pages; if (!firmware_has_feature(FW_FEATURE_PS3_LV1)) return -ENODEV; BUG_ON(!mem_init_done); start_addr = USE_LPAR_ADDR ? map.r1.base : map.rm.size; start_pfn = start_addr >> PAGE_SHIFT; nr_pages = (map.r1.size + PAGE_SIZE - 1) >> PAGE_SHIFT; DBG("%s:%d: start_addr %lxh, start_pfn %lxh, nr_pages %lxh\n", __func__, __LINE__, start_addr, start_pfn, nr_pages); result = add_memory(0, start_addr, map.r1.size); if (result) { DBG("%s:%d: add_memory failed: (%d)\n", __func__, __LINE__, result); return result; } result = online_pages(start_pfn, nr_pages); if (result) DBG("%s:%d: online_pages failed: (%d)\n", __func__, __LINE__, result); return result;}core_initcall(ps3_mm_add_memory);/*============================================================================*//* dma routines *//*============================================================================*//** * dma_sb_lpar_to_bus - Translate an lpar address to ioc mapped bus address. * @r: pointer to dma region structure * @lpar_addr: HV lpar address */static unsigned long dma_sb_lpar_to_bus(struct ps3_dma_region *r, unsigned long lpar_addr){ if (lpar_addr >= map.rm.size) lpar_addr -= map.r1.offset; BUG_ON(lpar_addr < r->offset); BUG_ON(lpar_addr >= r->offset + r->len); return r->bus_addr + lpar_addr - r->offset;}#define dma_dump_region(_a) _dma_dump_region(_a, __func__, __LINE__)static void __maybe_unused _dma_dump_region(const struct ps3_dma_region *r, const char *func, int line){ DBG("%s:%d: dev %u:%u\n", func, line, r->dev->bus_id, r->dev->dev_id); DBG("%s:%d: page_size %u\n", func, line, r->page_size); DBG("%s:%d: bus_addr %lxh\n", func, line, r->bus_addr); DBG("%s:%d: len %lxh\n", func, line, r->len); DBG("%s:%d: offset %lxh\n", func, line, r->offset);} /** * dma_chunk - A chunk of dma pages mapped by the io controller. * @region - The dma region that owns this chunk. * @lpar_addr: Starting lpar address of the area to map. * @bus_addr: Starting ioc bus address of the area to map. * @len: Length in bytes of the area to map. * @link: A struct list_head used with struct ps3_dma_region.chunk_list, the * list of all chuncks owned by the region. * * This implementation uses a very simple dma page manager * based on the dma_chunk structure. This scheme assumes * that all drivers use very well behaved dma ops. */struct dma_chunk { struct ps3_dma_region *region; unsigned long lpar_addr; unsigned long bus_addr; unsigned long len; struct list_head link; unsigned int usage_count;};#define dma_dump_chunk(_a) _dma_dump_chunk(_a, __func__, __LINE__)static void _dma_dump_chunk (const struct dma_chunk* c, const char* func, int line){ DBG("%s:%d: r.dev %u:%u\n", func, line, c->region->dev->bus_id, c->region->dev->dev_id); DBG("%s:%d: r.bus_addr %lxh\n", func, line, c->region->bus_addr); DBG("%s:%d: r.page_size %u\n", func, line, c->region->page_size); DBG("%s:%d: r.len %lxh\n", func, line, c->region->len); DBG("%s:%d: r.offset %lxh\n", func, line, c->region->offset); DBG("%s:%d: c.lpar_addr %lxh\n", func, line, c->lpar_addr); DBG("%s:%d: c.bus_addr %lxh\n", func, line, c->bus_addr); DBG("%s:%d: c.len %lxh\n", func, line, c->len);}static struct dma_chunk * dma_find_chunk(struct ps3_dma_region *r, unsigned long bus_addr, unsigned long len){ struct dma_chunk *c; unsigned long aligned_bus = _ALIGN_DOWN(bus_addr, 1 << r->page_size);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -