mm.c 32.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 *  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>
22
#include <linux/export.h>
Y
Yinghai Lu 已提交
23
#include <linux/memblock.h>
24
#include <linux/slab.h>
25

26
#include <asm/cell-regs.h>
27
#include <asm/firmware.h>
28
#include <asm/prom.h>
29 30
#include <asm/udbg.h>
#include <asm/lv1call.h>
31
#include <asm/setup.h>
32 33 34 35

#include "platform.h"

#if defined(DEBUG)
36
#define DBG udbg_printf
37
#else
38
#define DBG pr_devel
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
#endif

enum {
#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
81
 * @destroy: flag if region should be destroyed upon shutdown
82 83 84
 */

struct mem_region {
85
	u64 base;
86
	u64 size;
87
	unsigned long offset;
88
	int destroy;
89 90 91 92 93 94 95 96 97 98 99
};

/**
 * 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
100
 * @r1: highmem region(s)
101 102 103 104 105 106 107 108 109
 *
 * 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 {
110
	u64 total;
111 112
	u64 vas_id;
	u64 htab_size;
113 114 115 116 117
	struct mem_region rm;
	struct mem_region r1;
};

#define debug_dump_map(x) _debug_dump_map(x, __func__, __LINE__)
G
Geoff Levand 已提交
118 119
static void __maybe_unused _debug_dump_map(const struct map *m,
	const char *func, int line)
120
{
121 122
	DBG("%s:%d: map.total     = %llxh\n", func, line, m->total);
	DBG("%s:%d: map.rm.size   = %llxh\n", func, line, m->rm.size);
123 124 125
	DBG("%s:%d: map.vas_id    = %llu\n", func, line, m->vas_id);
	DBG("%s:%d: map.htab_size = %llxh\n", func, line, m->htab_size);
	DBG("%s:%d: map.r1.base   = %llxh\n", func, line, m->r1.base);
126
	DBG("%s:%d: map.r1.offset = %lxh\n", func, line, m->r1.offset);
127
	DBG("%s:%d: map.r1.size   = %llxh\n", func, line, m->r1.size);
128 129 130 131 132 133 134 135 136 137 138 139
}

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));
140 141
	return (phys_addr < map.rm.size || phys_addr >= map.total)
		? phys_addr : phys_addr + map.r1.offset;
142 143 144 145 146 147 148 149 150 151 152
}

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;
153 154 155 156 157
	u64 start_address;
	u64 size;
	u64 access_right;
	u64 max_page_size;
	u64 flags;
158 159 160 161 162 163 164 165 166 167 168 169 170

	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) {
171
		DBG("%s:%d: bad max_page_size %llxh\n", __func__, __LINE__,
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
			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)
{
G
Geoff Levand 已提交
213 214
	int result;

215
	DBG("%s:%d: map.vas_id    = %llu\n", __func__, __LINE__, map.vas_id);
G
Geoff Levand 已提交
216

217
	if (map.vas_id) {
G
Geoff Levand 已提交
218 219 220 221
		result = lv1_select_virtual_address_space(0);
		BUG_ON(result);
		result = lv1_destruct_virtual_address_space(map.vas_id);
		BUG_ON(result);
222 223 224 225
		map.vas_id = 0;
	}
}

226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
static int ps3_mm_get_repository_highmem(struct mem_region *r)
{
	int result;

	/* Assume a single highmem region. */

	result = ps3_repository_read_highmem_info(0, &r->base, &r->size);

	if (result)
		goto zero_region;

	if (!r->base || !r->size) {
		result = -1;
		goto zero_region;
	}

	r->offset = r->base - map.rm.size;

	DBG("%s:%d: Found high region in repository: %llxh %llxh\n",
	    __func__, __LINE__, r->base, r->size);

	return 0;

zero_region:
	DBG("%s:%d: No high region in repository.\n", __func__, __LINE__);

	r->size = r->base = r->offset = 0;
	return result;
}

static int ps3_mm_set_repository_highmem(const struct mem_region *r)
{
	/* Assume a single highmem region. */

	return r ? ps3_repository_write_highmem_info(0, r->base, r->size) :
		ps3_repository_write_highmem_info(0, 0, 0);
}

264 265 266 267 268 269 270 271 272
/**
 * 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.
 */

273
static int ps3_mm_region_create(struct mem_region *r, unsigned long size)
274 275
{
	int result;
276
	u64 muid;
277 278 279 280

	r->size = _ALIGN_DOWN(size, 1 << PAGE_SHIFT_16M);

	DBG("%s:%d requested  %lxh\n", __func__, __LINE__, size);
281 282 283
	DBG("%s:%d actual     %llxh\n", __func__, __LINE__, r->size);
	DBG("%s:%d difference %llxh (%lluMB)\n", __func__, __LINE__,
		size - r->size, (size - r->size) / 1024 / 1024);
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299

	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;
	}

300
	r->destroy = 1;
301 302 303 304 305 306 307 308 309 310 311 312 313
	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
 */

314
static void ps3_mm_region_destroy(struct mem_region *r)
315
{
G
Geoff Levand 已提交
316 317
	int result;

318 319 320 321 322 323
	if (!r->destroy) {
		pr_info("%s:%d: Not destroying high region: %llxh %llxh\n",
			__func__, __LINE__, r->base, r->size);
		return;
	}

324
	DBG("%s:%d: r->base = %llxh\n", __func__, __LINE__, r->base);
325

326
	if (r->base) {
G
Geoff Levand 已提交
327 328
		result = lv1_release_memory(r->base);
		BUG_ON(result);
329 330 331
		r->size = r->base = r->offset = 0;
		map.total = map.rm.size;
	}
332
	ps3_mm_set_repository_highmem(NULL);
333 334 335 336 337 338 339
}

/*============================================================================*/
/* dma routines                                                               */
/*============================================================================*/

/**
G
Geoff Levand 已提交
340
 * dma_sb_lpar_to_bus - Translate an lpar address to ioc mapped bus address.
341 342 343 344
 * @r: pointer to dma region structure
 * @lpar_addr: HV lpar address
 */

G
Geoff Levand 已提交
345
static unsigned long dma_sb_lpar_to_bus(struct ps3_dma_region *r,
346 347
	unsigned long lpar_addr)
{
G
Geoff Levand 已提交
348 349 350 351 352
	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;
353 354 355
}

#define dma_dump_region(_a) _dma_dump_region(_a, __func__, __LINE__)
G
Geoff Levand 已提交
356 357
static void  __maybe_unused _dma_dump_region(const struct ps3_dma_region *r,
	const char *func, int line)
358
{
359
	DBG("%s:%d: dev        %llu:%llu\n", func, line, r->dev->bus_id,
G
Geoff Levand 已提交
360
		r->dev->dev_id);
361 362 363
	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);
G
Geoff Levand 已提交
364
	DBG("%s:%d: offset     %lxh\n", func, line, r->offset);
365 366
}

G
Geoff Levand 已提交
367
  /**
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
 * 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)
{
394
	DBG("%s:%d: r.dev        %llu:%llu\n", func, line,
G
Geoff Levand 已提交
395
		c->region->dev->bus_id, c->region->dev->dev_id);
396 397 398
	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);
G
Geoff Levand 已提交
399
	DBG("%s:%d: r.offset     %lxh\n", func, line, c->region->offset);
400 401 402 403 404 405 406 407 408 409
	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);
G
Geoff Levand 已提交
410 411
	unsigned long aligned_len = _ALIGN_UP(len+bus_addr-aligned_bus,
					      1 << r->page_size);
412 413 414

	list_for_each_entry(c, &r->chunk_list.head, link) {
		/* intersection */
G
Geoff Levand 已提交
415 416
		if (aligned_bus >= c->bus_addr &&
		    aligned_bus + aligned_len <= c->bus_addr + c->len)
417
			return c;
G
Geoff Levand 已提交
418

419
		/* below */
G
Geoff Levand 已提交
420
		if (aligned_bus + aligned_len <= c->bus_addr)
421
			continue;
G
Geoff Levand 已提交
422

423
		/* above */
G
Geoff Levand 已提交
424
		if (aligned_bus >= c->bus_addr + c->len)
425 426 427 428 429 430 431 432 433
			continue;

		/* we don't handle the multi-chunk case for now */
		dma_dump_chunk(c);
		BUG();
	}
	return NULL;
}

G
Geoff Levand 已提交
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
static struct dma_chunk *dma_find_chunk_lpar(struct ps3_dma_region *r,
	unsigned long lpar_addr, unsigned long len)
{
	struct dma_chunk *c;
	unsigned long aligned_lpar = _ALIGN_DOWN(lpar_addr, 1 << r->page_size);
	unsigned long aligned_len = _ALIGN_UP(len + lpar_addr - aligned_lpar,
					      1 << r->page_size);

	list_for_each_entry(c, &r->chunk_list.head, link) {
		/* intersection */
		if (c->lpar_addr <= aligned_lpar &&
		    aligned_lpar < c->lpar_addr + c->len) {
			if (aligned_lpar + aligned_len <= c->lpar_addr + c->len)
				return c;
			else {
				dma_dump_chunk(c);
				BUG();
			}
		}
		/* below */
		if (aligned_lpar + aligned_len <= c->lpar_addr) {
			continue;
		}
		/* above */
		if (c->lpar_addr + c->len <= aligned_lpar) {
			continue;
		}
	}
	return NULL;
}

static int dma_sb_free_chunk(struct dma_chunk *c)
466 467 468 469
{
	int result = 0;

	if (c->bus_addr) {
G
Geoff Levand 已提交
470 471
		result = lv1_unmap_device_dma_region(c->region->dev->bus_id,
			c->region->dev->dev_id, c->bus_addr, c->len);
472 473 474 475 476 477 478
		BUG_ON(result);
	}

	kfree(c);
	return result;
}

G
Geoff Levand 已提交
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509
static int dma_ioc0_free_chunk(struct dma_chunk *c)
{
	int result = 0;
	int iopage;
	unsigned long offset;
	struct ps3_dma_region *r = c->region;

	DBG("%s:start\n", __func__);
	for (iopage = 0; iopage < (c->len >> r->page_size); iopage++) {
		offset = (1 << r->page_size) * iopage;
		/* put INVALID entry */
		result = lv1_put_iopte(0,
				       c->bus_addr + offset,
				       c->lpar_addr + offset,
				       r->ioid,
				       0);
		DBG("%s: bus=%#lx, lpar=%#lx, ioid=%d\n", __func__,
		    c->bus_addr + offset,
		    c->lpar_addr + offset,
		    r->ioid);

		if (result) {
			DBG("%s:%d: lv1_put_iopte failed: %s\n", __func__,
			    __LINE__, ps3_result(result));
		}
	}
	kfree(c);
	DBG("%s:end\n", __func__);
	return result;
}

510
/**
G
Geoff Levand 已提交
511
 * dma_sb_map_pages - Maps dma pages into the io controller bus address space.
512 513 514 515 516 517 518 519 520
 * @r: Pointer to a struct ps3_dma_region.
 * @phys_addr: Starting physical address of the area to map.
 * @len: Length in bytes of the area to map.
 * c_out: A pointer to receive an allocated struct dma_chunk for this area.
 *
 * This is the lowest level dma mapping routine, and is the one that will
 * make the HV call to add the pages into the io controller address space.
 */

G
Geoff Levand 已提交
521 522
static int dma_sb_map_pages(struct ps3_dma_region *r, unsigned long phys_addr,
	    unsigned long len, struct dma_chunk **c_out, u64 iopte_flag)
523 524 525 526 527 528 529 530 531 532 533 534 535
{
	int result;
	struct dma_chunk *c;

	c = kzalloc(sizeof(struct dma_chunk), GFP_ATOMIC);

	if (!c) {
		result = -ENOMEM;
		goto fail_alloc;
	}

	c->region = r;
	c->lpar_addr = ps3_mm_phys_to_lpar(phys_addr);
G
Geoff Levand 已提交
536
	c->bus_addr = dma_sb_lpar_to_bus(r, c->lpar_addr);
537 538
	c->len = len;

G
Geoff Levand 已提交
539 540 541 542
	BUG_ON(iopte_flag != 0xf800000000000000UL);
	result = lv1_map_device_dma_region(c->region->dev->bus_id,
					   c->region->dev->dev_id, c->lpar_addr,
					   c->bus_addr, c->len, iopte_flag);
543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
	if (result) {
		DBG("%s:%d: lv1_map_device_dma_region failed: %s\n",
			__func__, __LINE__, ps3_result(result));
		goto fail_map;
	}

	list_add(&c->link, &r->chunk_list.head);

	*c_out = c;
	return 0;

fail_map:
	kfree(c);
fail_alloc:
	*c_out = NULL;
	DBG(" <- %s:%d\n", __func__, __LINE__);
	return result;
}

G
Geoff Levand 已提交
562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599
static int dma_ioc0_map_pages(struct ps3_dma_region *r, unsigned long phys_addr,
			      unsigned long len, struct dma_chunk **c_out,
			      u64 iopte_flag)
{
	int result;
	struct dma_chunk *c, *last;
	int iopage, pages;
	unsigned long offset;

	DBG(KERN_ERR "%s: phy=%#lx, lpar%#lx, len=%#lx\n", __func__,
	    phys_addr, ps3_mm_phys_to_lpar(phys_addr), len);
	c = kzalloc(sizeof(struct dma_chunk), GFP_ATOMIC);

	if (!c) {
		result = -ENOMEM;
		goto fail_alloc;
	}

	c->region = r;
	c->len = len;
	c->lpar_addr = ps3_mm_phys_to_lpar(phys_addr);
	/* allocate IO address */
	if (list_empty(&r->chunk_list.head)) {
		/* first one */
		c->bus_addr = r->bus_addr;
	} else {
		/* derive from last bus addr*/
		last  = list_entry(r->chunk_list.head.next,
				   struct dma_chunk, link);
		c->bus_addr = last->bus_addr + last->len;
		DBG("%s: last bus=%#lx, len=%#lx\n", __func__,
		    last->bus_addr, last->len);
	}

	/* FIXME: check whether length exceeds region size */

	/* build ioptes for the area */
	pages = len >> r->page_size;
600
	DBG("%s: pgsize=%#x len=%#lx pages=%#x iopteflag=%#llx\n", __func__,
G
Geoff Levand 已提交
601 602 603 604 605 606 607 608 609
	    r->page_size, r->len, pages, iopte_flag);
	for (iopage = 0; iopage < pages; iopage++) {
		offset = (1 << r->page_size) * iopage;
		result = lv1_put_iopte(0,
				       c->bus_addr + offset,
				       c->lpar_addr + offset,
				       r->ioid,
				       iopte_flag);
		if (result) {
610 611
			pr_warn("%s:%d: lv1_put_iopte failed: %s\n",
				__func__, __LINE__, ps3_result(result));
G
Geoff Levand 已提交
612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639
			goto fail_map;
		}
		DBG("%s: pg=%d bus=%#lx, lpar=%#lx, ioid=%#x\n", __func__,
		    iopage, c->bus_addr + offset, c->lpar_addr + offset,
		    r->ioid);
	}

	/* be sure that last allocated one is inserted at head */
	list_add(&c->link, &r->chunk_list.head);

	*c_out = c;
	DBG("%s: end\n", __func__);
	return 0;

fail_map:
	for (iopage--; 0 <= iopage; iopage--) {
		lv1_put_iopte(0,
			      c->bus_addr + offset,
			      c->lpar_addr + offset,
			      r->ioid,
			      0);
	}
	kfree(c);
fail_alloc:
	*c_out = NULL;
	return result;
}

640
/**
G
Geoff Levand 已提交
641
 * dma_sb_region_create - Create a device dma region.
642 643 644 645 646 647
 * @r: Pointer to a struct ps3_dma_region.
 *
 * This is the lowest level dma region create routine, and is the one that
 * will make the HV call to create the region.
 */

G
Geoff Levand 已提交
648
static int dma_sb_region_create(struct ps3_dma_region *r)
649 650
{
	int result;
651
	u64 bus_addr;
652

G
Geoff Levand 已提交
653
	DBG(" -> %s:%d:\n", __func__, __LINE__);
G
Geoff Levand 已提交
654 655 656 657

	BUG_ON(!r);

	if (!r->dev->bus_id) {
658
		pr_info("%s:%d: %llu:%llu no dma\n", __func__, __LINE__,
G
Geoff Levand 已提交
659 660 661 662 663 664 665 666 667 668 669
			r->dev->bus_id, r->dev->dev_id);
		return 0;
	}

	DBG("%s:%u: len = 0x%lx, page_size = %u, offset = 0x%lx\n", __func__,
	    __LINE__, r->len, r->page_size, r->offset);

	BUG_ON(!r->len);
	BUG_ON(!r->page_size);
	BUG_ON(!r->region_ops);

670 671 672
	INIT_LIST_HEAD(&r->chunk_list.head);
	spin_lock_init(&r->chunk_list.lock);

G
Geoff Levand 已提交
673 674
	result = lv1_allocate_device_dma_region(r->dev->bus_id, r->dev->dev_id,
		roundup_pow_of_two(r->len), r->page_size, r->region_type,
675 676
		&bus_addr);
	r->bus_addr = bus_addr;
677 678 679 680 681 682 683 684 685 686

	if (result) {
		DBG("%s:%d: lv1_allocate_device_dma_region failed: %s\n",
			__func__, __LINE__, ps3_result(result));
		r->len = r->bus_addr = 0;
	}

	return result;
}

G
Geoff Levand 已提交
687 688 689
static int dma_ioc0_region_create(struct ps3_dma_region *r)
{
	int result;
690
	u64 bus_addr;
G
Geoff Levand 已提交
691 692 693 694 695 696 697

	INIT_LIST_HEAD(&r->chunk_list.head);
	spin_lock_init(&r->chunk_list.lock);

	result = lv1_allocate_io_segment(0,
					 r->len,
					 r->page_size,
698 699
					 &bus_addr);
	r->bus_addr = bus_addr;
G
Geoff Levand 已提交
700 701 702 703 704 705 706 707 708 709
	if (result) {
		DBG("%s:%d: lv1_allocate_io_segment failed: %s\n",
			__func__, __LINE__, ps3_result(result));
		r->len = r->bus_addr = 0;
	}
	DBG("%s: len=%#lx, pg=%d, bus=%#lx\n", __func__,
	    r->len, r->page_size, r->bus_addr);
	return result;
}

710 711 712 713 714 715 716 717
/**
 * dma_region_free - Free a device dma region.
 * @r: Pointer to a struct ps3_dma_region.
 *
 * This is the lowest level dma region free routine, and is the one that
 * will make the HV call to free the region.
 */

G
Geoff Levand 已提交
718
static int dma_sb_region_free(struct ps3_dma_region *r)
719 720 721 722 723
{
	int result;
	struct dma_chunk *c;
	struct dma_chunk *tmp;

G
Geoff Levand 已提交
724 725 726
	BUG_ON(!r);

	if (!r->dev->bus_id) {
727
		pr_info("%s:%d: %llu:%llu no dma\n", __func__, __LINE__,
G
Geoff Levand 已提交
728 729 730 731
			r->dev->bus_id, r->dev->dev_id);
		return 0;
	}

732 733
	list_for_each_entry_safe(c, tmp, &r->chunk_list.head, link) {
		list_del(&c->link);
G
Geoff Levand 已提交
734
		dma_sb_free_chunk(c);
735 736
	}

G
Geoff Levand 已提交
737
	result = lv1_free_device_dma_region(r->dev->bus_id, r->dev->dev_id,
738 739 740 741 742 743
		r->bus_addr);

	if (result)
		DBG("%s:%d: lv1_free_device_dma_region failed: %s\n",
			__func__, __LINE__, ps3_result(result));

G
Geoff Levand 已提交
744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767
	r->bus_addr = 0;

	return result;
}

static int dma_ioc0_region_free(struct ps3_dma_region *r)
{
	int result;
	struct dma_chunk *c, *n;

	DBG("%s: start\n", __func__);
	list_for_each_entry_safe(c, n, &r->chunk_list.head, link) {
		list_del(&c->link);
		dma_ioc0_free_chunk(c);
	}

	result = lv1_release_io_segment(0, r->bus_addr);

	if (result)
		DBG("%s:%d: lv1_free_device_dma_region failed: %s\n",
			__func__, __LINE__, ps3_result(result));

	r->bus_addr = 0;
	DBG("%s: end\n", __func__);
768 769 770 771 772

	return result;
}

/**
G
Geoff Levand 已提交
773
 * dma_sb_map_area - Map an area of memory into a device dma region.
774 775 776 777 778 779 780 781 782
 * @r: Pointer to a struct ps3_dma_region.
 * @virt_addr: Starting virtual address of the area to map.
 * @len: Length in bytes of the area to map.
 * @bus_addr: A pointer to return the starting ioc bus address of the area to
 * map.
 *
 * This is the common dma mapping routine.
 */

G
Geoff Levand 已提交
783
static int dma_sb_map_area(struct ps3_dma_region *r, unsigned long virt_addr,
784
	   unsigned long len, dma_addr_t *bus_addr,
G
Geoff Levand 已提交
785
	   u64 iopte_flag)
786 787 788 789 790 791
{
	int result;
	unsigned long flags;
	struct dma_chunk *c;
	unsigned long phys_addr = is_kernel_addr(virt_addr) ? __pa(virt_addr)
		: virt_addr;
G
Geoff Levand 已提交
792 793 794 795
	unsigned long aligned_phys = _ALIGN_DOWN(phys_addr, 1 << r->page_size);
	unsigned long aligned_len = _ALIGN_UP(len + phys_addr - aligned_phys,
					      1 << r->page_size);
	*bus_addr = dma_sb_lpar_to_bus(r, ps3_mm_phys_to_lpar(phys_addr));
796 797 798 799 800 801 802 803 804 805 806

	if (!USE_DYNAMIC_DMA) {
		unsigned long lpar_addr = ps3_mm_phys_to_lpar(phys_addr);
		DBG(" -> %s:%d\n", __func__, __LINE__);
		DBG("%s:%d virt_addr %lxh\n", __func__, __LINE__,
			virt_addr);
		DBG("%s:%d phys_addr %lxh\n", __func__, __LINE__,
			phys_addr);
		DBG("%s:%d lpar_addr %lxh\n", __func__, __LINE__,
			lpar_addr);
		DBG("%s:%d len       %lxh\n", __func__, __LINE__, len);
807
		DBG("%s:%d bus_addr  %llxh (%lxh)\n", __func__, __LINE__,
808 809 810 811 812 813 814
		*bus_addr, len);
	}

	spin_lock_irqsave(&r->chunk_list.lock, flags);
	c = dma_find_chunk(r, *bus_addr, len);

	if (c) {
G
Geoff Levand 已提交
815 816
		DBG("%s:%d: reusing mapped chunk", __func__, __LINE__);
		dma_dump_chunk(c);
817 818 819 820 821
		c->usage_count++;
		spin_unlock_irqrestore(&r->chunk_list.lock, flags);
		return 0;
	}

G
Geoff Levand 已提交
822
	result = dma_sb_map_pages(r, aligned_phys, aligned_len, &c, iopte_flag);
823 824 825

	if (result) {
		*bus_addr = 0;
G
Geoff Levand 已提交
826
		DBG("%s:%d: dma_sb_map_pages failed (%d)\n",
827 828 829 830 831 832 833 834 835 836 837
			__func__, __LINE__, result);
		spin_unlock_irqrestore(&r->chunk_list.lock, flags);
		return result;
	}

	c->usage_count = 1;

	spin_unlock_irqrestore(&r->chunk_list.lock, flags);
	return result;
}

G
Geoff Levand 已提交
838
static int dma_ioc0_map_area(struct ps3_dma_region *r, unsigned long virt_addr,
839
	     unsigned long len, dma_addr_t *bus_addr,
G
Geoff Levand 已提交
840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878
	     u64 iopte_flag)
{
	int result;
	unsigned long flags;
	struct dma_chunk *c;
	unsigned long phys_addr = is_kernel_addr(virt_addr) ? __pa(virt_addr)
		: virt_addr;
	unsigned long aligned_phys = _ALIGN_DOWN(phys_addr, 1 << r->page_size);
	unsigned long aligned_len = _ALIGN_UP(len + phys_addr - aligned_phys,
					      1 << r->page_size);

	DBG(KERN_ERR "%s: vaddr=%#lx, len=%#lx\n", __func__,
	    virt_addr, len);
	DBG(KERN_ERR "%s: ph=%#lx a_ph=%#lx a_l=%#lx\n", __func__,
	    phys_addr, aligned_phys, aligned_len);

	spin_lock_irqsave(&r->chunk_list.lock, flags);
	c = dma_find_chunk_lpar(r, ps3_mm_phys_to_lpar(phys_addr), len);

	if (c) {
		/* FIXME */
		BUG();
		*bus_addr = c->bus_addr + phys_addr - aligned_phys;
		c->usage_count++;
		spin_unlock_irqrestore(&r->chunk_list.lock, flags);
		return 0;
	}

	result = dma_ioc0_map_pages(r, aligned_phys, aligned_len, &c,
				    iopte_flag);

	if (result) {
		*bus_addr = 0;
		DBG("%s:%d: dma_ioc0_map_pages failed (%d)\n",
			__func__, __LINE__, result);
		spin_unlock_irqrestore(&r->chunk_list.lock, flags);
		return result;
	}
	*bus_addr = c->bus_addr + phys_addr - aligned_phys;
879
	DBG("%s: va=%#lx pa=%#lx a_pa=%#lx bus=%#llx\n", __func__,
G
Geoff Levand 已提交
880 881 882 883 884 885 886
	    virt_addr, phys_addr, aligned_phys, *bus_addr);
	c->usage_count = 1;

	spin_unlock_irqrestore(&r->chunk_list.lock, flags);
	return result;
}

887
/**
G
Geoff Levand 已提交
888
 * dma_sb_unmap_area - Unmap an area of memory from a device dma region.
889 890 891 892 893 894 895
 * @r: Pointer to a struct ps3_dma_region.
 * @bus_addr: The starting ioc bus address of the area to unmap.
 * @len: Length in bytes of the area to unmap.
 *
 * This is the common dma unmap routine.
 */

896
static int dma_sb_unmap_area(struct ps3_dma_region *r, dma_addr_t bus_addr,
897 898 899 900 901 902 903 904 905 906 907
	unsigned long len)
{
	unsigned long flags;
	struct dma_chunk *c;

	spin_lock_irqsave(&r->chunk_list.lock, flags);
	c = dma_find_chunk(r, bus_addr, len);

	if (!c) {
		unsigned long aligned_bus = _ALIGN_DOWN(bus_addr,
			1 << r->page_size);
G
Geoff Levand 已提交
908 909
		unsigned long aligned_len = _ALIGN_UP(len + bus_addr
			- aligned_bus, 1 << r->page_size);
910
		DBG("%s:%d: not found: bus_addr %llxh\n",
911 912 913 914 915 916 917 918 919 920 921 922 923 924
			__func__, __LINE__, bus_addr);
		DBG("%s:%d: not found: len %lxh\n",
			__func__, __LINE__, len);
		DBG("%s:%d: not found: aligned_bus %lxh\n",
			__func__, __LINE__, aligned_bus);
		DBG("%s:%d: not found: aligned_len %lxh\n",
			__func__, __LINE__, aligned_len);
		BUG();
	}

	c->usage_count--;

	if (!c->usage_count) {
		list_del(&c->link);
G
Geoff Levand 已提交
925
		dma_sb_free_chunk(c);
926 927 928 929 930 931
	}

	spin_unlock_irqrestore(&r->chunk_list.lock, flags);
	return 0;
}

932
static int dma_ioc0_unmap_area(struct ps3_dma_region *r,
933
			dma_addr_t bus_addr, unsigned long len)
G
Geoff Levand 已提交
934 935 936 937
{
	unsigned long flags;
	struct dma_chunk *c;

938
	DBG("%s: start a=%#llx l=%#lx\n", __func__, bus_addr, len);
G
Geoff Levand 已提交
939 940 941 942 943 944 945 946 947
	spin_lock_irqsave(&r->chunk_list.lock, flags);
	c = dma_find_chunk(r, bus_addr, len);

	if (!c) {
		unsigned long aligned_bus = _ALIGN_DOWN(bus_addr,
							1 << r->page_size);
		unsigned long aligned_len = _ALIGN_UP(len + bus_addr
						      - aligned_bus,
						      1 << r->page_size);
948
		DBG("%s:%d: not found: bus_addr %llxh\n",
G
Geoff Levand 已提交
949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970
		    __func__, __LINE__, bus_addr);
		DBG("%s:%d: not found: len %lxh\n",
		    __func__, __LINE__, len);
		DBG("%s:%d: not found: aligned_bus %lxh\n",
		    __func__, __LINE__, aligned_bus);
		DBG("%s:%d: not found: aligned_len %lxh\n",
		    __func__, __LINE__, aligned_len);
		BUG();
	}

	c->usage_count--;

	if (!c->usage_count) {
		list_del(&c->link);
		dma_ioc0_free_chunk(c);
	}

	spin_unlock_irqrestore(&r->chunk_list.lock, flags);
	DBG("%s: end\n", __func__);
	return 0;
}

971
/**
G
Geoff Levand 已提交
972
 * dma_sb_region_create_linear - Setup a linear dma mapping for a device.
973 974 975 976 977 978
 * @r: Pointer to a struct ps3_dma_region.
 *
 * This routine creates an HV dma region for the device and maps all available
 * ram into the io controller bus address space.
 */

G
Geoff Levand 已提交
979
static int dma_sb_region_create_linear(struct ps3_dma_region *r)
980 981
{
	int result;
982 983
	unsigned long virt_addr, len;
	dma_addr_t tmp;
G
Geoff Levand 已提交
984 985 986 987 988 989 990 991 992

	if (r->len > 16*1024*1024) {	/* FIXME: need proper fix */
		/* force 16M dma pages for linear mapping */
		if (r->page_size != PS3_DMA_16M) {
			pr_info("%s:%d: forcing 16M pages for linear map\n",
				__func__, __LINE__);
			r->page_size = PS3_DMA_16M;
			r->len = _ALIGN_UP(r->len, 1 << r->page_size);
		}
993 994
	}

G
Geoff Levand 已提交
995
	result = dma_sb_region_create(r);
996 997
	BUG_ON(result);

G
Geoff Levand 已提交
998 999 1000 1001 1002 1003 1004
	if (r->offset < map.rm.size) {
		/* Map (part of) 1st RAM chunk */
		virt_addr = map.rm.base + r->offset;
		len = map.rm.size - r->offset;
		if (len > r->len)
			len = r->len;
		result = dma_sb_map_area(r, virt_addr, len, &tmp,
1005 1006
			CBE_IOPTE_PP_W | CBE_IOPTE_PP_R | CBE_IOPTE_SO_RW |
			CBE_IOPTE_M);
G
Geoff Levand 已提交
1007 1008
		BUG_ON(result);
	}
1009

G
Geoff Levand 已提交
1010 1011
	if (r->offset + r->len > map.rm.size) {
		/* Map (part of) 2nd RAM chunk */
1012
		virt_addr = map.rm.size;
G
Geoff Levand 已提交
1013 1014 1015 1016 1017 1018
		len = r->len;
		if (r->offset >= map.rm.size)
			virt_addr += r->offset - map.rm.size;
		else
			len -= map.rm.size - r->offset;
		result = dma_sb_map_area(r, virt_addr, len, &tmp,
1019 1020
			CBE_IOPTE_PP_W | CBE_IOPTE_PP_R | CBE_IOPTE_SO_RW |
			CBE_IOPTE_M);
G
Geoff Levand 已提交
1021 1022
		BUG_ON(result);
	}
1023 1024 1025 1026 1027

	return result;
}

/**
G
Geoff Levand 已提交
1028
 * dma_sb_region_free_linear - Free a linear dma mapping for a device.
1029 1030 1031 1032 1033
 * @r: Pointer to a struct ps3_dma_region.
 *
 * This routine will unmap all mapped areas and free the HV dma region.
 */

G
Geoff Levand 已提交
1034
static int dma_sb_region_free_linear(struct ps3_dma_region *r)
1035 1036
{
	int result;
1037 1038
	dma_addr_t bus_addr;
	unsigned long len, lpar_addr;
G
Geoff Levand 已提交
1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049

	if (r->offset < map.rm.size) {
		/* Unmap (part of) 1st RAM chunk */
		lpar_addr = map.rm.base + r->offset;
		len = map.rm.size - r->offset;
		if (len > r->len)
			len = r->len;
		bus_addr = dma_sb_lpar_to_bus(r, lpar_addr);
		result = dma_sb_unmap_area(r, bus_addr, len);
		BUG_ON(result);
	}
1050

G
Geoff Levand 已提交
1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062
	if (r->offset + r->len > map.rm.size) {
		/* Unmap (part of) 2nd RAM chunk */
		lpar_addr = map.r1.base;
		len = r->len;
		if (r->offset >= map.rm.size)
			lpar_addr += r->offset - map.rm.size;
		else
			len -= map.rm.size - r->offset;
		bus_addr = dma_sb_lpar_to_bus(r, lpar_addr);
		result = dma_sb_unmap_area(r, bus_addr, len);
		BUG_ON(result);
	}
1063

G
Geoff Levand 已提交
1064
	result = dma_sb_region_free(r);
1065 1066 1067 1068 1069 1070
	BUG_ON(result);

	return result;
}

/**
G
Geoff Levand 已提交
1071
 * dma_sb_map_area_linear - Map an area of memory into a device dma region.
1072 1073 1074 1075 1076 1077
 * @r: Pointer to a struct ps3_dma_region.
 * @virt_addr: Starting virtual address of the area to map.
 * @len: Length in bytes of the area to map.
 * @bus_addr: A pointer to return the starting ioc bus address of the area to
 * map.
 *
G
Geoff Levand 已提交
1078
 * This routine just returns the corresponding bus address.  Actual mapping
1079 1080 1081
 * occurs in dma_region_create_linear().
 */

G
Geoff Levand 已提交
1082
static int dma_sb_map_area_linear(struct ps3_dma_region *r,
1083
	unsigned long virt_addr, unsigned long len, dma_addr_t *bus_addr,
G
Geoff Levand 已提交
1084
	u64 iopte_flag)
1085 1086 1087
{
	unsigned long phys_addr = is_kernel_addr(virt_addr) ? __pa(virt_addr)
		: virt_addr;
G
Geoff Levand 已提交
1088
	*bus_addr = dma_sb_lpar_to_bus(r, ps3_mm_phys_to_lpar(phys_addr));
1089 1090 1091 1092 1093 1094 1095 1096 1097
	return 0;
}

/**
 * dma_unmap_area_linear - Unmap an area of memory from a device dma region.
 * @r: Pointer to a struct ps3_dma_region.
 * @bus_addr: The starting ioc bus address of the area to unmap.
 * @len: Length in bytes of the area to unmap.
 *
G
Geoff Levand 已提交
1098
 * This routine does nothing.  Unmapping occurs in dma_sb_region_free_linear().
1099 1100
 */

G
Geoff Levand 已提交
1101
static int dma_sb_unmap_area_linear(struct ps3_dma_region *r,
1102
	dma_addr_t bus_addr, unsigned long len)
1103 1104
{
	return 0;
G
Geoff Levand 已提交
1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157
};

static const struct ps3_dma_region_ops ps3_dma_sb_region_ops =  {
	.create = dma_sb_region_create,
	.free = dma_sb_region_free,
	.map = dma_sb_map_area,
	.unmap = dma_sb_unmap_area
};

static const struct ps3_dma_region_ops ps3_dma_sb_region_linear_ops = {
	.create = dma_sb_region_create_linear,
	.free = dma_sb_region_free_linear,
	.map = dma_sb_map_area_linear,
	.unmap = dma_sb_unmap_area_linear
};

static const struct ps3_dma_region_ops ps3_dma_ioc0_region_ops = {
	.create = dma_ioc0_region_create,
	.free = dma_ioc0_region_free,
	.map = dma_ioc0_map_area,
	.unmap = dma_ioc0_unmap_area
};

int ps3_dma_region_init(struct ps3_system_bus_device *dev,
	struct ps3_dma_region *r, enum ps3_dma_page_size page_size,
	enum ps3_dma_region_type region_type, void *addr, unsigned long len)
{
	unsigned long lpar_addr;

	lpar_addr = addr ? ps3_mm_phys_to_lpar(__pa(addr)) : 0;

	r->dev = dev;
	r->page_size = page_size;
	r->region_type = region_type;
	r->offset = lpar_addr;
	if (r->offset >= map.rm.size)
		r->offset -= map.r1.offset;
	r->len = len ? len : _ALIGN_UP(map.total, 1 << r->page_size);

	switch (dev->dev_type) {
	case PS3_DEVICE_TYPE_SB:
		r->region_ops =  (USE_DYNAMIC_DMA)
			? &ps3_dma_sb_region_ops
			: &ps3_dma_sb_region_linear_ops;
		break;
	case PS3_DEVICE_TYPE_IOC0:
		r->region_ops = &ps3_dma_ioc0_region_ops;
		break;
	default:
		BUG();
		return -EINVAL;
	}
	return 0;
1158
}
G
Geoff Levand 已提交
1159
EXPORT_SYMBOL(ps3_dma_region_init);
1160 1161 1162

int ps3_dma_region_create(struct ps3_dma_region *r)
{
G
Geoff Levand 已提交
1163 1164 1165 1166
	BUG_ON(!r);
	BUG_ON(!r->region_ops);
	BUG_ON(!r->region_ops->create);
	return r->region_ops->create(r);
1167
}
G
Geoff Levand 已提交
1168
EXPORT_SYMBOL(ps3_dma_region_create);
1169 1170 1171

int ps3_dma_region_free(struct ps3_dma_region *r)
{
G
Geoff Levand 已提交
1172 1173 1174 1175
	BUG_ON(!r);
	BUG_ON(!r->region_ops);
	BUG_ON(!r->region_ops->free);
	return r->region_ops->free(r);
1176
}
G
Geoff Levand 已提交
1177
EXPORT_SYMBOL(ps3_dma_region_free);
1178 1179

int ps3_dma_map(struct ps3_dma_region *r, unsigned long virt_addr,
1180
	unsigned long len, dma_addr_t *bus_addr,
G
Geoff Levand 已提交
1181
	u64 iopte_flag)
1182
{
G
Geoff Levand 已提交
1183
	return r->region_ops->map(r, virt_addr, len, bus_addr, iopte_flag);
1184 1185
}

1186
int ps3_dma_unmap(struct ps3_dma_region *r, dma_addr_t bus_addr,
1187 1188
	unsigned long len)
{
G
Geoff Levand 已提交
1189
	return r->region_ops->unmap(r, bus_addr, len);
1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219
}

/*============================================================================*/
/* system startup routines                                                    */
/*============================================================================*/

/**
 * ps3_mm_init - initialize the address space state variables
 */

void __init ps3_mm_init(void)
{
	int result;

	DBG(" -> %s:%d\n", __func__, __LINE__);

	result = ps3_repository_read_mm_info(&map.rm.base, &map.rm.size,
		&map.total);

	if (result)
		panic("ps3_repository_read_mm_info() failed");

	map.rm.offset = map.rm.base;
	map.vas_id = map.htab_size = 0;

	/* this implementation assumes map.rm.base is zero */

	BUG_ON(map.rm.base);
	BUG_ON(!map.rm.size);

1220
	/* Check if we got the highmem region from an earlier boot step */
1221

1222 1223 1224 1225 1226 1227
	if (ps3_mm_get_repository_highmem(&map.r1)) {
		result = ps3_mm_region_create(&map.r1, map.total - map.rm.size);

		if (!result)
			ps3_mm_set_repository_highmem(&map.r1);
	}
1228

G
Geoff Levand 已提交
1229 1230 1231
	/* correct map.total for the real total amount of memory we use */
	map.total = map.rm.size + map.r1.size;

1232 1233 1234 1235 1236 1237 1238 1239 1240
	if (!map.r1.size) {
		DBG("%s:%d: No highmem region found\n", __func__, __LINE__);
	} else {
		DBG("%s:%d: Adding highmem region: %llxh %llxh\n",
			__func__, __LINE__, map.rm.size,
			map.total - map.rm.size);
		memblock_add(map.rm.size, map.total - map.rm.size);
	}

1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251
	DBG(" <- %s:%d\n", __func__, __LINE__);
}

/**
 * ps3_mm_shutdown - final cleanup of address space
 */

void ps3_mm_shutdown(void)
{
	ps3_mm_region_destroy(&map.r1);
}