xref: /openbmc/linux/arch/powerpc/platforms/ps3/mm.c (revision 9733862e50fdba55e7f1554e4286fcc5302ff28e)
1873e65bcSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2f58a9d17SGeoff Levand /*
3f58a9d17SGeoff Levand  *  PS3 address space management.
4f58a9d17SGeoff Levand  *
5f58a9d17SGeoff Levand  *  Copyright (C) 2006 Sony Computer Entertainment Inc.
6f58a9d17SGeoff Levand  *  Copyright 2006 Sony Corp.
7f58a9d17SGeoff Levand  */
8f58a9d17SGeoff Levand 
9*9733862eSGeoff Levand #include <linux/dma-mapping.h>
10f58a9d17SGeoff Levand #include <linux/kernel.h>
114b16f8e2SPaul Gortmaker #include <linux/export.h>
1295f72d1eSYinghai Lu #include <linux/memblock.h>
135a0e3ad6STejun Heo #include <linux/slab.h>
14f58a9d17SGeoff Levand 
159413c883SGeert Uytterhoeven #include <asm/cell-regs.h>
16e22ba7e3SArnd Bergmann #include <asm/firmware.h>
17d9b2b2a2SDavid S. Miller #include <asm/prom.h>
18f58a9d17SGeoff Levand #include <asm/udbg.h>
19f58a9d17SGeoff Levand #include <asm/lv1call.h>
20ae3a197eSDavid Howells #include <asm/setup.h>
21f58a9d17SGeoff Levand 
22f58a9d17SGeoff Levand #include "platform.h"
23f58a9d17SGeoff Levand 
24f58a9d17SGeoff Levand #if defined(DEBUG)
2583bb643dSGeert Uytterhoeven #define DBG udbg_printf
26f58a9d17SGeoff Levand #else
277424639aSMichael Ellerman #define DBG pr_devel
28f58a9d17SGeoff Levand #endif
29f58a9d17SGeoff Levand 
30f58a9d17SGeoff Levand enum {
31f58a9d17SGeoff Levand #if defined(CONFIG_PS3_DYNAMIC_DMA)
32f58a9d17SGeoff Levand 	USE_DYNAMIC_DMA = 1,
33f58a9d17SGeoff Levand #else
34f58a9d17SGeoff Levand 	USE_DYNAMIC_DMA = 0,
35f58a9d17SGeoff Levand #endif
36f58a9d17SGeoff Levand };
37f58a9d17SGeoff Levand 
38f58a9d17SGeoff Levand enum {
39f58a9d17SGeoff Levand 	PAGE_SHIFT_4K = 12U,
40f58a9d17SGeoff Levand 	PAGE_SHIFT_64K = 16U,
41f58a9d17SGeoff Levand 	PAGE_SHIFT_16M = 24U,
42f58a9d17SGeoff Levand };
43f58a9d17SGeoff Levand 
44f58a9d17SGeoff Levand static unsigned long make_page_sizes(unsigned long a, unsigned long b)
45f58a9d17SGeoff Levand {
46f58a9d17SGeoff Levand 	return (a << 56) | (b << 48);
47f58a9d17SGeoff Levand }
48f58a9d17SGeoff Levand 
49f58a9d17SGeoff Levand enum {
50f58a9d17SGeoff Levand 	ALLOCATE_MEMORY_TRY_ALT_UNIT = 0X04,
51f58a9d17SGeoff Levand 	ALLOCATE_MEMORY_ADDR_ZERO = 0X08,
52f58a9d17SGeoff Levand };
53f58a9d17SGeoff Levand 
54f58a9d17SGeoff Levand /* valid htab sizes are {18,19,20} = 256K, 512K, 1M */
55f58a9d17SGeoff Levand 
56f58a9d17SGeoff Levand enum {
57f58a9d17SGeoff Levand 	HTAB_SIZE_MAX = 20U, /* HV limit of 1MB */
58f58a9d17SGeoff Levand 	HTAB_SIZE_MIN = 18U, /* CPU limit of 256KB */
59f58a9d17SGeoff Levand };
60f58a9d17SGeoff Levand 
61f58a9d17SGeoff Levand /*============================================================================*/
62f58a9d17SGeoff Levand /* virtual address space routines                                             */
63f58a9d17SGeoff Levand /*============================================================================*/
64f58a9d17SGeoff Levand 
65f58a9d17SGeoff Levand /**
66f58a9d17SGeoff Levand  * struct mem_region - memory region structure
67f58a9d17SGeoff Levand  * @base: base address
68f58a9d17SGeoff Levand  * @size: size in bytes
69f58a9d17SGeoff Levand  * @offset: difference between base and rm.size
701e755c09SAndre Heider  * @destroy: flag if region should be destroyed upon shutdown
71f58a9d17SGeoff Levand  */
72f58a9d17SGeoff Levand 
73f58a9d17SGeoff Levand struct mem_region {
74b17b3df1SStephen Rothwell 	u64 base;
755418b9c6SStephen Rothwell 	u64 size;
76f58a9d17SGeoff Levand 	unsigned long offset;
771e755c09SAndre Heider 	int destroy;
78f58a9d17SGeoff Levand };
79f58a9d17SGeoff Levand 
80f58a9d17SGeoff Levand /**
81f58a9d17SGeoff Levand  * struct map - address space state variables holder
82f58a9d17SGeoff Levand  * @total: total memory available as reported by HV
83f58a9d17SGeoff Levand  * @vas_id - HV virtual address space id
84f58a9d17SGeoff Levand  * @htab_size: htab size in bytes
85f58a9d17SGeoff Levand  *
86f58a9d17SGeoff Levand  * The HV virtual address space (vas) allows for hotplug memory regions.
87f58a9d17SGeoff Levand  * Memory regions can be created and destroyed in the vas at runtime.
88f58a9d17SGeoff Levand  * @rm: real mode (bootmem) region
898ac5fd11SHector Martin  * @r1: highmem region(s)
90f58a9d17SGeoff Levand  *
91f58a9d17SGeoff Levand  * ps3 addresses
92f58a9d17SGeoff Levand  * virt_addr: a cpu 'translated' effective address
93f58a9d17SGeoff Levand  * phys_addr: an address in what Linux thinks is the physical address space
94f58a9d17SGeoff Levand  * lpar_addr: an address in the HV virtual address space
95f58a9d17SGeoff Levand  * bus_addr: an io controller 'translated' address on a device bus
96f58a9d17SGeoff Levand  */
97f58a9d17SGeoff Levand 
98f58a9d17SGeoff Levand struct map {
995418b9c6SStephen Rothwell 	u64 total;
100b17b3df1SStephen Rothwell 	u64 vas_id;
101b17b3df1SStephen Rothwell 	u64 htab_size;
102f58a9d17SGeoff Levand 	struct mem_region rm;
103f58a9d17SGeoff Levand 	struct mem_region r1;
104f58a9d17SGeoff Levand };
105f58a9d17SGeoff Levand 
106f58a9d17SGeoff Levand #define debug_dump_map(x) _debug_dump_map(x, __func__, __LINE__)
1076bb5cf10SGeoff Levand static void __maybe_unused _debug_dump_map(const struct map *m,
1086bb5cf10SGeoff Levand 	const char *func, int line)
109f58a9d17SGeoff Levand {
1105418b9c6SStephen Rothwell 	DBG("%s:%d: map.total     = %llxh\n", func, line, m->total);
1115418b9c6SStephen Rothwell 	DBG("%s:%d: map.rm.size   = %llxh\n", func, line, m->rm.size);
112b17b3df1SStephen Rothwell 	DBG("%s:%d: map.vas_id    = %llu\n", func, line, m->vas_id);
113b17b3df1SStephen Rothwell 	DBG("%s:%d: map.htab_size = %llxh\n", func, line, m->htab_size);
114b17b3df1SStephen Rothwell 	DBG("%s:%d: map.r1.base   = %llxh\n", func, line, m->r1.base);
115f58a9d17SGeoff Levand 	DBG("%s:%d: map.r1.offset = %lxh\n", func, line, m->r1.offset);
1165418b9c6SStephen Rothwell 	DBG("%s:%d: map.r1.size   = %llxh\n", func, line, m->r1.size);
117f58a9d17SGeoff Levand }
118f58a9d17SGeoff Levand 
119f58a9d17SGeoff Levand static struct map map;
120f58a9d17SGeoff Levand 
121f58a9d17SGeoff Levand /**
122f58a9d17SGeoff Levand  * ps3_mm_phys_to_lpar - translate a linux physical address to lpar address
123f58a9d17SGeoff Levand  * @phys_addr: linux physical address
124f58a9d17SGeoff Levand  */
125f58a9d17SGeoff Levand 
126f58a9d17SGeoff Levand unsigned long ps3_mm_phys_to_lpar(unsigned long phys_addr)
127f58a9d17SGeoff Levand {
128f58a9d17SGeoff Levand 	BUG_ON(is_kernel_addr(phys_addr));
129f58a9d17SGeoff Levand 	return (phys_addr < map.rm.size || phys_addr >= map.total)
130f58a9d17SGeoff Levand 		? phys_addr : phys_addr + map.r1.offset;
131f58a9d17SGeoff Levand }
132f58a9d17SGeoff Levand 
133f58a9d17SGeoff Levand EXPORT_SYMBOL(ps3_mm_phys_to_lpar);
134f58a9d17SGeoff Levand 
135f58a9d17SGeoff Levand /**
136f58a9d17SGeoff Levand  * ps3_mm_vas_create - create the virtual address space
137f58a9d17SGeoff Levand  */
138f58a9d17SGeoff Levand 
139f58a9d17SGeoff Levand void __init ps3_mm_vas_create(unsigned long* htab_size)
140f58a9d17SGeoff Levand {
141f58a9d17SGeoff Levand 	int result;
142b17b3df1SStephen Rothwell 	u64 start_address;
143b17b3df1SStephen Rothwell 	u64 size;
144b17b3df1SStephen Rothwell 	u64 access_right;
145b17b3df1SStephen Rothwell 	u64 max_page_size;
146b17b3df1SStephen Rothwell 	u64 flags;
147f58a9d17SGeoff Levand 
148f58a9d17SGeoff Levand 	result = lv1_query_logical_partition_address_region_info(0,
149f58a9d17SGeoff Levand 		&start_address, &size, &access_right, &max_page_size,
150f58a9d17SGeoff Levand 		&flags);
151f58a9d17SGeoff Levand 
152f58a9d17SGeoff Levand 	if (result) {
153f58a9d17SGeoff Levand 		DBG("%s:%d: lv1_query_logical_partition_address_region_info "
154f58a9d17SGeoff Levand 			"failed: %s\n", __func__, __LINE__,
155f58a9d17SGeoff Levand 			ps3_result(result));
156f58a9d17SGeoff Levand 		goto fail;
157f58a9d17SGeoff Levand 	}
158f58a9d17SGeoff Levand 
159f58a9d17SGeoff Levand 	if (max_page_size < PAGE_SHIFT_16M) {
160b17b3df1SStephen Rothwell 		DBG("%s:%d: bad max_page_size %llxh\n", __func__, __LINE__,
161f58a9d17SGeoff Levand 			max_page_size);
162f58a9d17SGeoff Levand 		goto fail;
163f58a9d17SGeoff Levand 	}
164f58a9d17SGeoff Levand 
165f58a9d17SGeoff Levand 	BUILD_BUG_ON(CONFIG_PS3_HTAB_SIZE > HTAB_SIZE_MAX);
166f58a9d17SGeoff Levand 	BUILD_BUG_ON(CONFIG_PS3_HTAB_SIZE < HTAB_SIZE_MIN);
167f58a9d17SGeoff Levand 
168f58a9d17SGeoff Levand 	result = lv1_construct_virtual_address_space(CONFIG_PS3_HTAB_SIZE,
169f58a9d17SGeoff Levand 			2, make_page_sizes(PAGE_SHIFT_16M, PAGE_SHIFT_64K),
170f58a9d17SGeoff Levand 			&map.vas_id, &map.htab_size);
171f58a9d17SGeoff Levand 
172f58a9d17SGeoff Levand 	if (result) {
173f58a9d17SGeoff Levand 		DBG("%s:%d: lv1_construct_virtual_address_space failed: %s\n",
174f58a9d17SGeoff Levand 			__func__, __LINE__, ps3_result(result));
175f58a9d17SGeoff Levand 		goto fail;
176f58a9d17SGeoff Levand 	}
177f58a9d17SGeoff Levand 
178f58a9d17SGeoff Levand 	result = lv1_select_virtual_address_space(map.vas_id);
179f58a9d17SGeoff Levand 
180f58a9d17SGeoff Levand 	if (result) {
181f58a9d17SGeoff Levand 		DBG("%s:%d: lv1_select_virtual_address_space failed: %s\n",
182f58a9d17SGeoff Levand 			__func__, __LINE__, ps3_result(result));
183f58a9d17SGeoff Levand 		goto fail;
184f58a9d17SGeoff Levand 	}
185f58a9d17SGeoff Levand 
186f58a9d17SGeoff Levand 	*htab_size = map.htab_size;
187f58a9d17SGeoff Levand 
188f58a9d17SGeoff Levand 	debug_dump_map(&map);
189f58a9d17SGeoff Levand 
190f58a9d17SGeoff Levand 	return;
191f58a9d17SGeoff Levand 
192f58a9d17SGeoff Levand fail:
193f58a9d17SGeoff Levand 	panic("ps3_mm_vas_create failed");
194f58a9d17SGeoff Levand }
195f58a9d17SGeoff Levand 
196f58a9d17SGeoff Levand /**
197f58a9d17SGeoff Levand  * ps3_mm_vas_destroy -
198f58a9d17SGeoff Levand  */
199f58a9d17SGeoff Levand 
200f58a9d17SGeoff Levand void ps3_mm_vas_destroy(void)
201f58a9d17SGeoff Levand {
2026bb5cf10SGeoff Levand 	int result;
2036bb5cf10SGeoff Levand 
204f58a9d17SGeoff Levand 	if (map.vas_id) {
2056bb5cf10SGeoff Levand 		result = lv1_select_virtual_address_space(0);
20612655446SGeoff Levand 		result += lv1_destruct_virtual_address_space(map.vas_id);
20712655446SGeoff Levand 
20812655446SGeoff Levand 		if (result) {
20912655446SGeoff Levand 			lv1_panic(0);
21012655446SGeoff Levand 		}
21112655446SGeoff Levand 
212f58a9d17SGeoff Levand 		map.vas_id = 0;
213f58a9d17SGeoff Levand 	}
214f58a9d17SGeoff Levand }
215f58a9d17SGeoff Levand 
216d4b18bd6SGeoff Levand static int ps3_mm_get_repository_highmem(struct mem_region *r)
217d4b18bd6SGeoff Levand {
218d4b18bd6SGeoff Levand 	int result;
219d4b18bd6SGeoff Levand 
220d4b18bd6SGeoff Levand 	/* Assume a single highmem region. */
221d4b18bd6SGeoff Levand 
222d4b18bd6SGeoff Levand 	result = ps3_repository_read_highmem_info(0, &r->base, &r->size);
223d4b18bd6SGeoff Levand 
224d4b18bd6SGeoff Levand 	if (result)
225d4b18bd6SGeoff Levand 		goto zero_region;
226d4b18bd6SGeoff Levand 
227d4b18bd6SGeoff Levand 	if (!r->base || !r->size) {
228d4b18bd6SGeoff Levand 		result = -1;
229d4b18bd6SGeoff Levand 		goto zero_region;
230d4b18bd6SGeoff Levand 	}
231d4b18bd6SGeoff Levand 
232d4b18bd6SGeoff Levand 	r->offset = r->base - map.rm.size;
233d4b18bd6SGeoff Levand 
234d4b18bd6SGeoff Levand 	DBG("%s:%d: Found high region in repository: %llxh %llxh\n",
235d4b18bd6SGeoff Levand 	    __func__, __LINE__, r->base, r->size);
236d4b18bd6SGeoff Levand 
237d4b18bd6SGeoff Levand 	return 0;
238d4b18bd6SGeoff Levand 
239d4b18bd6SGeoff Levand zero_region:
240d4b18bd6SGeoff Levand 	DBG("%s:%d: No high region in repository.\n", __func__, __LINE__);
241d4b18bd6SGeoff Levand 
242d4b18bd6SGeoff Levand 	r->size = r->base = r->offset = 0;
243d4b18bd6SGeoff Levand 	return result;
244d4b18bd6SGeoff Levand }
245d4b18bd6SGeoff Levand 
246d4b18bd6SGeoff Levand static int ps3_mm_set_repository_highmem(const struct mem_region *r)
247d4b18bd6SGeoff Levand {
248d4b18bd6SGeoff Levand 	/* Assume a single highmem region. */
249d4b18bd6SGeoff Levand 
250d4b18bd6SGeoff Levand 	return r ? ps3_repository_write_highmem_info(0, r->base, r->size) :
251d4b18bd6SGeoff Levand 		ps3_repository_write_highmem_info(0, 0, 0);
252d4b18bd6SGeoff Levand }
253d4b18bd6SGeoff Levand 
254f58a9d17SGeoff Levand /**
255f58a9d17SGeoff Levand  * ps3_mm_region_create - create a memory region in the vas
256f58a9d17SGeoff Levand  * @r: pointer to a struct mem_region to accept initialized values
257f58a9d17SGeoff Levand  * @size: requested region size
258f58a9d17SGeoff Levand  *
259f58a9d17SGeoff Levand  * This implementation creates the region with the vas large page size.
260f58a9d17SGeoff Levand  * @size is rounded down to a multiple of the vas large page size.
261f58a9d17SGeoff Levand  */
262f58a9d17SGeoff Levand 
26332f44a12SGeert Uytterhoeven static int ps3_mm_region_create(struct mem_region *r, unsigned long size)
264f58a9d17SGeoff Levand {
265f58a9d17SGeoff Levand 	int result;
266b17b3df1SStephen Rothwell 	u64 muid;
267f58a9d17SGeoff Levand 
268e96d904eSChristophe Leroy 	r->size = ALIGN_DOWN(size, 1 << PAGE_SHIFT_16M);
269f58a9d17SGeoff Levand 
270f58a9d17SGeoff Levand 	DBG("%s:%d requested  %lxh\n", __func__, __LINE__, size);
2715418b9c6SStephen Rothwell 	DBG("%s:%d actual     %llxh\n", __func__, __LINE__, r->size);
2725418b9c6SStephen Rothwell 	DBG("%s:%d difference %llxh (%lluMB)\n", __func__, __LINE__,
2735418b9c6SStephen Rothwell 		size - r->size, (size - r->size) / 1024 / 1024);
274f58a9d17SGeoff Levand 
275f58a9d17SGeoff Levand 	if (r->size == 0) {
276f58a9d17SGeoff Levand 		DBG("%s:%d: size == 0\n", __func__, __LINE__);
277f58a9d17SGeoff Levand 		result = -1;
278f58a9d17SGeoff Levand 		goto zero_region;
279f58a9d17SGeoff Levand 	}
280f58a9d17SGeoff Levand 
281f58a9d17SGeoff Levand 	result = lv1_allocate_memory(r->size, PAGE_SHIFT_16M, 0,
282f58a9d17SGeoff Levand 		ALLOCATE_MEMORY_TRY_ALT_UNIT, &r->base, &muid);
283f58a9d17SGeoff Levand 
284f58a9d17SGeoff Levand 	if (result || r->base < map.rm.size) {
285f58a9d17SGeoff Levand 		DBG("%s:%d: lv1_allocate_memory failed: %s\n",
286f58a9d17SGeoff Levand 			__func__, __LINE__, ps3_result(result));
287f58a9d17SGeoff Levand 		goto zero_region;
288f58a9d17SGeoff Levand 	}
289f58a9d17SGeoff Levand 
2901e755c09SAndre Heider 	r->destroy = 1;
291f58a9d17SGeoff Levand 	r->offset = r->base - map.rm.size;
292f58a9d17SGeoff Levand 	return result;
293f58a9d17SGeoff Levand 
294f58a9d17SGeoff Levand zero_region:
295f58a9d17SGeoff Levand 	r->size = r->base = r->offset = 0;
296f58a9d17SGeoff Levand 	return result;
297f58a9d17SGeoff Levand }
298f58a9d17SGeoff Levand 
299f58a9d17SGeoff Levand /**
300f58a9d17SGeoff Levand  * ps3_mm_region_destroy - destroy a memory region
301f58a9d17SGeoff Levand  * @r: pointer to struct mem_region
302f58a9d17SGeoff Levand  */
303f58a9d17SGeoff Levand 
30432f44a12SGeert Uytterhoeven static void ps3_mm_region_destroy(struct mem_region *r)
305f58a9d17SGeoff Levand {
3066bb5cf10SGeoff Levand 	int result;
3076bb5cf10SGeoff Levand 
3081e755c09SAndre Heider 	if (!r->destroy) {
3091e755c09SAndre Heider 		return;
3101e755c09SAndre Heider 	}
3111e755c09SAndre Heider 
312f58a9d17SGeoff Levand 	if (r->base) {
3136bb5cf10SGeoff Levand 		result = lv1_release_memory(r->base);
31412655446SGeoff Levand 
31512655446SGeoff Levand 		if (result) {
31612655446SGeoff Levand 			lv1_panic(0);
31712655446SGeoff Levand 		}
31812655446SGeoff Levand 
319f58a9d17SGeoff Levand 		r->size = r->base = r->offset = 0;
320f58a9d17SGeoff Levand 		map.total = map.rm.size;
321f58a9d17SGeoff Levand 	}
32212655446SGeoff Levand 
3235ae74630SGeoff Levand 	ps3_mm_set_repository_highmem(NULL);
324f58a9d17SGeoff Levand }
325f58a9d17SGeoff Levand 
326f58a9d17SGeoff Levand /*============================================================================*/
327f58a9d17SGeoff Levand /* dma routines                                                               */
328f58a9d17SGeoff Levand /*============================================================================*/
329f58a9d17SGeoff Levand 
330f58a9d17SGeoff Levand /**
3316bb5cf10SGeoff Levand  * dma_sb_lpar_to_bus - Translate an lpar address to ioc mapped bus address.
332f58a9d17SGeoff Levand  * @r: pointer to dma region structure
333f58a9d17SGeoff Levand  * @lpar_addr: HV lpar address
334f58a9d17SGeoff Levand  */
335f58a9d17SGeoff Levand 
3366bb5cf10SGeoff Levand static unsigned long dma_sb_lpar_to_bus(struct ps3_dma_region *r,
337f58a9d17SGeoff Levand 	unsigned long lpar_addr)
338f58a9d17SGeoff Levand {
3396bb5cf10SGeoff Levand 	if (lpar_addr >= map.rm.size)
3406bb5cf10SGeoff Levand 		lpar_addr -= map.r1.offset;
3416bb5cf10SGeoff Levand 	BUG_ON(lpar_addr < r->offset);
3426bb5cf10SGeoff Levand 	BUG_ON(lpar_addr >= r->offset + r->len);
3436bb5cf10SGeoff Levand 	return r->bus_addr + lpar_addr - r->offset;
344f58a9d17SGeoff Levand }
345f58a9d17SGeoff Levand 
346f58a9d17SGeoff Levand #define dma_dump_region(_a) _dma_dump_region(_a, __func__, __LINE__)
3476bb5cf10SGeoff Levand static void  __maybe_unused _dma_dump_region(const struct ps3_dma_region *r,
3486bb5cf10SGeoff Levand 	const char *func, int line)
349f58a9d17SGeoff Levand {
3505c949070SStephen Rothwell 	DBG("%s:%d: dev        %llu:%llu\n", func, line, r->dev->bus_id,
3516bb5cf10SGeoff Levand 		r->dev->dev_id);
352f58a9d17SGeoff Levand 	DBG("%s:%d: page_size  %u\n", func, line, r->page_size);
353f58a9d17SGeoff Levand 	DBG("%s:%d: bus_addr   %lxh\n", func, line, r->bus_addr);
354f58a9d17SGeoff Levand 	DBG("%s:%d: len        %lxh\n", func, line, r->len);
3556bb5cf10SGeoff Levand 	DBG("%s:%d: offset     %lxh\n", func, line, r->offset);
356f58a9d17SGeoff Levand }
357f58a9d17SGeoff Levand 
358f58a9d17SGeoff Levand   /**
359f58a9d17SGeoff Levand  * dma_chunk - A chunk of dma pages mapped by the io controller.
360f58a9d17SGeoff Levand  * @region - The dma region that owns this chunk.
361f58a9d17SGeoff Levand  * @lpar_addr: Starting lpar address of the area to map.
362f58a9d17SGeoff Levand  * @bus_addr: Starting ioc bus address of the area to map.
363f58a9d17SGeoff Levand  * @len: Length in bytes of the area to map.
364f58a9d17SGeoff Levand  * @link: A struct list_head used with struct ps3_dma_region.chunk_list, the
365f58a9d17SGeoff Levand  * list of all chuncks owned by the region.
366f58a9d17SGeoff Levand  *
367f58a9d17SGeoff Levand  * This implementation uses a very simple dma page manager
368f58a9d17SGeoff Levand  * based on the dma_chunk structure.  This scheme assumes
369f58a9d17SGeoff Levand  * that all drivers use very well behaved dma ops.
370f58a9d17SGeoff Levand  */
371f58a9d17SGeoff Levand 
372f58a9d17SGeoff Levand struct dma_chunk {
373f58a9d17SGeoff Levand 	struct ps3_dma_region *region;
374f58a9d17SGeoff Levand 	unsigned long lpar_addr;
375f58a9d17SGeoff Levand 	unsigned long bus_addr;
376f58a9d17SGeoff Levand 	unsigned long len;
377f58a9d17SGeoff Levand 	struct list_head link;
378f58a9d17SGeoff Levand 	unsigned int usage_count;
379f58a9d17SGeoff Levand };
380f58a9d17SGeoff Levand 
381f58a9d17SGeoff Levand #define dma_dump_chunk(_a) _dma_dump_chunk(_a, __func__, __LINE__)
382f58a9d17SGeoff Levand static void _dma_dump_chunk (const struct dma_chunk* c, const char* func,
383f58a9d17SGeoff Levand 	int line)
384f58a9d17SGeoff Levand {
3855c949070SStephen Rothwell 	DBG("%s:%d: r.dev        %llu:%llu\n", func, line,
3866bb5cf10SGeoff Levand 		c->region->dev->bus_id, c->region->dev->dev_id);
387f58a9d17SGeoff Levand 	DBG("%s:%d: r.bus_addr   %lxh\n", func, line, c->region->bus_addr);
388f58a9d17SGeoff Levand 	DBG("%s:%d: r.page_size  %u\n", func, line, c->region->page_size);
389f58a9d17SGeoff Levand 	DBG("%s:%d: r.len        %lxh\n", func, line, c->region->len);
3906bb5cf10SGeoff Levand 	DBG("%s:%d: r.offset     %lxh\n", func, line, c->region->offset);
391f58a9d17SGeoff Levand 	DBG("%s:%d: c.lpar_addr  %lxh\n", func, line, c->lpar_addr);
392f58a9d17SGeoff Levand 	DBG("%s:%d: c.bus_addr   %lxh\n", func, line, c->bus_addr);
393f58a9d17SGeoff Levand 	DBG("%s:%d: c.len        %lxh\n", func, line, c->len);
394f58a9d17SGeoff Levand }
395f58a9d17SGeoff Levand 
396f58a9d17SGeoff Levand static struct dma_chunk * dma_find_chunk(struct ps3_dma_region *r,
397f58a9d17SGeoff Levand 	unsigned long bus_addr, unsigned long len)
398f58a9d17SGeoff Levand {
399f58a9d17SGeoff Levand 	struct dma_chunk *c;
400e96d904eSChristophe Leroy 	unsigned long aligned_bus = ALIGN_DOWN(bus_addr, 1 << r->page_size);
401b7115316SChristophe Leroy 	unsigned long aligned_len = ALIGN(len+bus_addr-aligned_bus,
4026bb5cf10SGeoff Levand 					      1 << r->page_size);
403f58a9d17SGeoff Levand 
404f58a9d17SGeoff Levand 	list_for_each_entry(c, &r->chunk_list.head, link) {
405f58a9d17SGeoff Levand 		/* intersection */
4066bb5cf10SGeoff Levand 		if (aligned_bus >= c->bus_addr &&
4076bb5cf10SGeoff Levand 		    aligned_bus + aligned_len <= c->bus_addr + c->len)
408f58a9d17SGeoff Levand 			return c;
4096bb5cf10SGeoff Levand 
410f58a9d17SGeoff Levand 		/* below */
4116bb5cf10SGeoff Levand 		if (aligned_bus + aligned_len <= c->bus_addr)
412f58a9d17SGeoff Levand 			continue;
4136bb5cf10SGeoff Levand 
414f58a9d17SGeoff Levand 		/* above */
4156bb5cf10SGeoff Levand 		if (aligned_bus >= c->bus_addr + c->len)
416f58a9d17SGeoff Levand 			continue;
417f58a9d17SGeoff Levand 
418f58a9d17SGeoff Levand 		/* we don't handle the multi-chunk case for now */
419f58a9d17SGeoff Levand 		dma_dump_chunk(c);
420f58a9d17SGeoff Levand 		BUG();
421f58a9d17SGeoff Levand 	}
422f58a9d17SGeoff Levand 	return NULL;
423f58a9d17SGeoff Levand }
424f58a9d17SGeoff Levand 
4256bb5cf10SGeoff Levand static struct dma_chunk *dma_find_chunk_lpar(struct ps3_dma_region *r,
4266bb5cf10SGeoff Levand 	unsigned long lpar_addr, unsigned long len)
4276bb5cf10SGeoff Levand {
4286bb5cf10SGeoff Levand 	struct dma_chunk *c;
429e96d904eSChristophe Leroy 	unsigned long aligned_lpar = ALIGN_DOWN(lpar_addr, 1 << r->page_size);
430b7115316SChristophe Leroy 	unsigned long aligned_len = ALIGN(len + lpar_addr - aligned_lpar,
4316bb5cf10SGeoff Levand 					      1 << r->page_size);
4326bb5cf10SGeoff Levand 
4336bb5cf10SGeoff Levand 	list_for_each_entry(c, &r->chunk_list.head, link) {
4346bb5cf10SGeoff Levand 		/* intersection */
4356bb5cf10SGeoff Levand 		if (c->lpar_addr <= aligned_lpar &&
4366bb5cf10SGeoff Levand 		    aligned_lpar < c->lpar_addr + c->len) {
4376bb5cf10SGeoff Levand 			if (aligned_lpar + aligned_len <= c->lpar_addr + c->len)
4386bb5cf10SGeoff Levand 				return c;
4396bb5cf10SGeoff Levand 			else {
4406bb5cf10SGeoff Levand 				dma_dump_chunk(c);
4416bb5cf10SGeoff Levand 				BUG();
4426bb5cf10SGeoff Levand 			}
4436bb5cf10SGeoff Levand 		}
4446bb5cf10SGeoff Levand 		/* below */
4456bb5cf10SGeoff Levand 		if (aligned_lpar + aligned_len <= c->lpar_addr) {
4466bb5cf10SGeoff Levand 			continue;
4476bb5cf10SGeoff Levand 		}
4486bb5cf10SGeoff Levand 		/* above */
4496bb5cf10SGeoff Levand 		if (c->lpar_addr + c->len <= aligned_lpar) {
4506bb5cf10SGeoff Levand 			continue;
4516bb5cf10SGeoff Levand 		}
4526bb5cf10SGeoff Levand 	}
4536bb5cf10SGeoff Levand 	return NULL;
4546bb5cf10SGeoff Levand }
4556bb5cf10SGeoff Levand 
4566bb5cf10SGeoff Levand static int dma_sb_free_chunk(struct dma_chunk *c)
457f58a9d17SGeoff Levand {
458f58a9d17SGeoff Levand 	int result = 0;
459f58a9d17SGeoff Levand 
460f58a9d17SGeoff Levand 	if (c->bus_addr) {
4616bb5cf10SGeoff Levand 		result = lv1_unmap_device_dma_region(c->region->dev->bus_id,
4626bb5cf10SGeoff Levand 			c->region->dev->dev_id, c->bus_addr, c->len);
463f58a9d17SGeoff Levand 		BUG_ON(result);
464f58a9d17SGeoff Levand 	}
465f58a9d17SGeoff Levand 
466f58a9d17SGeoff Levand 	kfree(c);
467f58a9d17SGeoff Levand 	return result;
468f58a9d17SGeoff Levand }
469f58a9d17SGeoff Levand 
4706bb5cf10SGeoff Levand static int dma_ioc0_free_chunk(struct dma_chunk *c)
4716bb5cf10SGeoff Levand {
4726bb5cf10SGeoff Levand 	int result = 0;
4736bb5cf10SGeoff Levand 	int iopage;
4746bb5cf10SGeoff Levand 	unsigned long offset;
4756bb5cf10SGeoff Levand 	struct ps3_dma_region *r = c->region;
4766bb5cf10SGeoff Levand 
4776bb5cf10SGeoff Levand 	DBG("%s:start\n", __func__);
4786bb5cf10SGeoff Levand 	for (iopage = 0; iopage < (c->len >> r->page_size); iopage++) {
4796bb5cf10SGeoff Levand 		offset = (1 << r->page_size) * iopage;
4806bb5cf10SGeoff Levand 		/* put INVALID entry */
4816bb5cf10SGeoff Levand 		result = lv1_put_iopte(0,
4826bb5cf10SGeoff Levand 				       c->bus_addr + offset,
4836bb5cf10SGeoff Levand 				       c->lpar_addr + offset,
4846bb5cf10SGeoff Levand 				       r->ioid,
4856bb5cf10SGeoff Levand 				       0);
4866bb5cf10SGeoff Levand 		DBG("%s: bus=%#lx, lpar=%#lx, ioid=%d\n", __func__,
4876bb5cf10SGeoff Levand 		    c->bus_addr + offset,
4886bb5cf10SGeoff Levand 		    c->lpar_addr + offset,
4896bb5cf10SGeoff Levand 		    r->ioid);
4906bb5cf10SGeoff Levand 
4916bb5cf10SGeoff Levand 		if (result) {
4926bb5cf10SGeoff Levand 			DBG("%s:%d: lv1_put_iopte failed: %s\n", __func__,
4936bb5cf10SGeoff Levand 			    __LINE__, ps3_result(result));
4946bb5cf10SGeoff Levand 		}
4956bb5cf10SGeoff Levand 	}
4966bb5cf10SGeoff Levand 	kfree(c);
4976bb5cf10SGeoff Levand 	DBG("%s:end\n", __func__);
4986bb5cf10SGeoff Levand 	return result;
4996bb5cf10SGeoff Levand }
5006bb5cf10SGeoff Levand 
501f58a9d17SGeoff Levand /**
5026bb5cf10SGeoff Levand  * dma_sb_map_pages - Maps dma pages into the io controller bus address space.
503f58a9d17SGeoff Levand  * @r: Pointer to a struct ps3_dma_region.
504f58a9d17SGeoff Levand  * @phys_addr: Starting physical address of the area to map.
505f58a9d17SGeoff Levand  * @len: Length in bytes of the area to map.
506f58a9d17SGeoff Levand  * c_out: A pointer to receive an allocated struct dma_chunk for this area.
507f58a9d17SGeoff Levand  *
508f58a9d17SGeoff Levand  * This is the lowest level dma mapping routine, and is the one that will
509f58a9d17SGeoff Levand  * make the HV call to add the pages into the io controller address space.
510f58a9d17SGeoff Levand  */
511f58a9d17SGeoff Levand 
5126bb5cf10SGeoff Levand static int dma_sb_map_pages(struct ps3_dma_region *r, unsigned long phys_addr,
5136bb5cf10SGeoff Levand 	    unsigned long len, struct dma_chunk **c_out, u64 iopte_flag)
514f58a9d17SGeoff Levand {
515f58a9d17SGeoff Levand 	int result;
516f58a9d17SGeoff Levand 	struct dma_chunk *c;
517f58a9d17SGeoff Levand 
518a0828cf5SMarkus Elfring 	c = kzalloc(sizeof(*c), GFP_ATOMIC);
519f58a9d17SGeoff Levand 	if (!c) {
520f58a9d17SGeoff Levand 		result = -ENOMEM;
521f58a9d17SGeoff Levand 		goto fail_alloc;
522f58a9d17SGeoff Levand 	}
523f58a9d17SGeoff Levand 
524f58a9d17SGeoff Levand 	c->region = r;
525f58a9d17SGeoff Levand 	c->lpar_addr = ps3_mm_phys_to_lpar(phys_addr);
5266bb5cf10SGeoff Levand 	c->bus_addr = dma_sb_lpar_to_bus(r, c->lpar_addr);
527f58a9d17SGeoff Levand 	c->len = len;
528f58a9d17SGeoff Levand 
5296bb5cf10SGeoff Levand 	BUG_ON(iopte_flag != 0xf800000000000000UL);
5306bb5cf10SGeoff Levand 	result = lv1_map_device_dma_region(c->region->dev->bus_id,
5316bb5cf10SGeoff Levand 					   c->region->dev->dev_id, c->lpar_addr,
5326bb5cf10SGeoff Levand 					   c->bus_addr, c->len, iopte_flag);
533f58a9d17SGeoff Levand 	if (result) {
534f58a9d17SGeoff Levand 		DBG("%s:%d: lv1_map_device_dma_region failed: %s\n",
535f58a9d17SGeoff Levand 			__func__, __LINE__, ps3_result(result));
536f58a9d17SGeoff Levand 		goto fail_map;
537f58a9d17SGeoff Levand 	}
538f58a9d17SGeoff Levand 
539f58a9d17SGeoff Levand 	list_add(&c->link, &r->chunk_list.head);
540f58a9d17SGeoff Levand 
541f58a9d17SGeoff Levand 	*c_out = c;
542f58a9d17SGeoff Levand 	return 0;
543f58a9d17SGeoff Levand 
544f58a9d17SGeoff Levand fail_map:
545f58a9d17SGeoff Levand 	kfree(c);
546f58a9d17SGeoff Levand fail_alloc:
547f58a9d17SGeoff Levand 	*c_out = NULL;
548f58a9d17SGeoff Levand 	DBG(" <- %s:%d\n", __func__, __LINE__);
549f58a9d17SGeoff Levand 	return result;
550f58a9d17SGeoff Levand }
551f58a9d17SGeoff Levand 
5526bb5cf10SGeoff Levand static int dma_ioc0_map_pages(struct ps3_dma_region *r, unsigned long phys_addr,
5536bb5cf10SGeoff Levand 			      unsigned long len, struct dma_chunk **c_out,
5546bb5cf10SGeoff Levand 			      u64 iopte_flag)
5556bb5cf10SGeoff Levand {
5566bb5cf10SGeoff Levand 	int result;
5576bb5cf10SGeoff Levand 	struct dma_chunk *c, *last;
5586bb5cf10SGeoff Levand 	int iopage, pages;
5596bb5cf10SGeoff Levand 	unsigned long offset;
5606bb5cf10SGeoff Levand 
5616bb5cf10SGeoff Levand 	DBG(KERN_ERR "%s: phy=%#lx, lpar%#lx, len=%#lx\n", __func__,
5626bb5cf10SGeoff Levand 	    phys_addr, ps3_mm_phys_to_lpar(phys_addr), len);
563a0828cf5SMarkus Elfring 	c = kzalloc(sizeof(*c), GFP_ATOMIC);
5646bb5cf10SGeoff Levand 	if (!c) {
5656bb5cf10SGeoff Levand 		result = -ENOMEM;
5666bb5cf10SGeoff Levand 		goto fail_alloc;
5676bb5cf10SGeoff Levand 	}
5686bb5cf10SGeoff Levand 
5696bb5cf10SGeoff Levand 	c->region = r;
5706bb5cf10SGeoff Levand 	c->len = len;
5716bb5cf10SGeoff Levand 	c->lpar_addr = ps3_mm_phys_to_lpar(phys_addr);
5726bb5cf10SGeoff Levand 	/* allocate IO address */
5736bb5cf10SGeoff Levand 	if (list_empty(&r->chunk_list.head)) {
5746bb5cf10SGeoff Levand 		/* first one */
5756bb5cf10SGeoff Levand 		c->bus_addr = r->bus_addr;
5766bb5cf10SGeoff Levand 	} else {
5776bb5cf10SGeoff Levand 		/* derive from last bus addr*/
5786bb5cf10SGeoff Levand 		last  = list_entry(r->chunk_list.head.next,
5796bb5cf10SGeoff Levand 				   struct dma_chunk, link);
5806bb5cf10SGeoff Levand 		c->bus_addr = last->bus_addr + last->len;
5816bb5cf10SGeoff Levand 		DBG("%s: last bus=%#lx, len=%#lx\n", __func__,
5826bb5cf10SGeoff Levand 		    last->bus_addr, last->len);
5836bb5cf10SGeoff Levand 	}
5846bb5cf10SGeoff Levand 
5856bb5cf10SGeoff Levand 	/* FIXME: check whether length exceeds region size */
5866bb5cf10SGeoff Levand 
5876bb5cf10SGeoff Levand 	/* build ioptes for the area */
5886bb5cf10SGeoff Levand 	pages = len >> r->page_size;
5895c949070SStephen Rothwell 	DBG("%s: pgsize=%#x len=%#lx pages=%#x iopteflag=%#llx\n", __func__,
5906bb5cf10SGeoff Levand 	    r->page_size, r->len, pages, iopte_flag);
5916bb5cf10SGeoff Levand 	for (iopage = 0; iopage < pages; iopage++) {
5926bb5cf10SGeoff Levand 		offset = (1 << r->page_size) * iopage;
5936bb5cf10SGeoff Levand 		result = lv1_put_iopte(0,
5946bb5cf10SGeoff Levand 				       c->bus_addr + offset,
5956bb5cf10SGeoff Levand 				       c->lpar_addr + offset,
5966bb5cf10SGeoff Levand 				       r->ioid,
5976bb5cf10SGeoff Levand 				       iopte_flag);
5986bb5cf10SGeoff Levand 		if (result) {
599f2c2cbccSJoe Perches 			pr_warn("%s:%d: lv1_put_iopte failed: %s\n",
6007e28060aSGeert Uytterhoeven 				__func__, __LINE__, ps3_result(result));
6016bb5cf10SGeoff Levand 			goto fail_map;
6026bb5cf10SGeoff Levand 		}
6036bb5cf10SGeoff Levand 		DBG("%s: pg=%d bus=%#lx, lpar=%#lx, ioid=%#x\n", __func__,
6046bb5cf10SGeoff Levand 		    iopage, c->bus_addr + offset, c->lpar_addr + offset,
6056bb5cf10SGeoff Levand 		    r->ioid);
6066bb5cf10SGeoff Levand 	}
6076bb5cf10SGeoff Levand 
6086bb5cf10SGeoff Levand 	/* be sure that last allocated one is inserted at head */
6096bb5cf10SGeoff Levand 	list_add(&c->link, &r->chunk_list.head);
6106bb5cf10SGeoff Levand 
6116bb5cf10SGeoff Levand 	*c_out = c;
6126bb5cf10SGeoff Levand 	DBG("%s: end\n", __func__);
6136bb5cf10SGeoff Levand 	return 0;
6146bb5cf10SGeoff Levand 
6156bb5cf10SGeoff Levand fail_map:
6166bb5cf10SGeoff Levand 	for (iopage--; 0 <= iopage; iopage--) {
6176bb5cf10SGeoff Levand 		lv1_put_iopte(0,
6186bb5cf10SGeoff Levand 			      c->bus_addr + offset,
6196bb5cf10SGeoff Levand 			      c->lpar_addr + offset,
6206bb5cf10SGeoff Levand 			      r->ioid,
6216bb5cf10SGeoff Levand 			      0);
6226bb5cf10SGeoff Levand 	}
6236bb5cf10SGeoff Levand 	kfree(c);
6246bb5cf10SGeoff Levand fail_alloc:
6256bb5cf10SGeoff Levand 	*c_out = NULL;
6266bb5cf10SGeoff Levand 	return result;
6276bb5cf10SGeoff Levand }
6286bb5cf10SGeoff Levand 
629f58a9d17SGeoff Levand /**
6306bb5cf10SGeoff Levand  * dma_sb_region_create - Create a device dma region.
631f58a9d17SGeoff Levand  * @r: Pointer to a struct ps3_dma_region.
632f58a9d17SGeoff Levand  *
633f58a9d17SGeoff Levand  * This is the lowest level dma region create routine, and is the one that
634f58a9d17SGeoff Levand  * will make the HV call to create the region.
635f58a9d17SGeoff Levand  */
636f58a9d17SGeoff Levand 
6376bb5cf10SGeoff Levand static int dma_sb_region_create(struct ps3_dma_region *r)
638f58a9d17SGeoff Levand {
639f58a9d17SGeoff Levand 	int result;
640b17b3df1SStephen Rothwell 	u64 bus_addr;
641f58a9d17SGeoff Levand 
64262d80749SGeoff Levand 	DBG(" -> %s:%d:\n", __func__, __LINE__);
6436bb5cf10SGeoff Levand 
6446bb5cf10SGeoff Levand 	BUG_ON(!r);
6456bb5cf10SGeoff Levand 
6466bb5cf10SGeoff Levand 	if (!r->dev->bus_id) {
6475c949070SStephen Rothwell 		pr_info("%s:%d: %llu:%llu no dma\n", __func__, __LINE__,
6486bb5cf10SGeoff Levand 			r->dev->bus_id, r->dev->dev_id);
6496bb5cf10SGeoff Levand 		return 0;
6506bb5cf10SGeoff Levand 	}
6516bb5cf10SGeoff Levand 
6526bb5cf10SGeoff Levand 	DBG("%s:%u: len = 0x%lx, page_size = %u, offset = 0x%lx\n", __func__,
6536bb5cf10SGeoff Levand 	    __LINE__, r->len, r->page_size, r->offset);
6546bb5cf10SGeoff Levand 
6556bb5cf10SGeoff Levand 	BUG_ON(!r->len);
6566bb5cf10SGeoff Levand 	BUG_ON(!r->page_size);
6576bb5cf10SGeoff Levand 	BUG_ON(!r->region_ops);
6586bb5cf10SGeoff Levand 
659f58a9d17SGeoff Levand 	INIT_LIST_HEAD(&r->chunk_list.head);
660f58a9d17SGeoff Levand 	spin_lock_init(&r->chunk_list.lock);
661f58a9d17SGeoff Levand 
6626bb5cf10SGeoff Levand 	result = lv1_allocate_device_dma_region(r->dev->bus_id, r->dev->dev_id,
6636bb5cf10SGeoff Levand 		roundup_pow_of_two(r->len), r->page_size, r->region_type,
664b17b3df1SStephen Rothwell 		&bus_addr);
665b17b3df1SStephen Rothwell 	r->bus_addr = bus_addr;
666f58a9d17SGeoff Levand 
667f58a9d17SGeoff Levand 	if (result) {
668f58a9d17SGeoff Levand 		DBG("%s:%d: lv1_allocate_device_dma_region failed: %s\n",
669f58a9d17SGeoff Levand 			__func__, __LINE__, ps3_result(result));
670f58a9d17SGeoff Levand 		r->len = r->bus_addr = 0;
671f58a9d17SGeoff Levand 	}
672f58a9d17SGeoff Levand 
673f58a9d17SGeoff Levand 	return result;
674f58a9d17SGeoff Levand }
675f58a9d17SGeoff Levand 
6766bb5cf10SGeoff Levand static int dma_ioc0_region_create(struct ps3_dma_region *r)
6776bb5cf10SGeoff Levand {
6786bb5cf10SGeoff Levand 	int result;
679b17b3df1SStephen Rothwell 	u64 bus_addr;
6806bb5cf10SGeoff Levand 
6816bb5cf10SGeoff Levand 	INIT_LIST_HEAD(&r->chunk_list.head);
6826bb5cf10SGeoff Levand 	spin_lock_init(&r->chunk_list.lock);
6836bb5cf10SGeoff Levand 
6846bb5cf10SGeoff Levand 	result = lv1_allocate_io_segment(0,
6856bb5cf10SGeoff Levand 					 r->len,
6866bb5cf10SGeoff Levand 					 r->page_size,
687b17b3df1SStephen Rothwell 					 &bus_addr);
688b17b3df1SStephen Rothwell 	r->bus_addr = bus_addr;
6896bb5cf10SGeoff Levand 	if (result) {
6906bb5cf10SGeoff Levand 		DBG("%s:%d: lv1_allocate_io_segment failed: %s\n",
6916bb5cf10SGeoff Levand 			__func__, __LINE__, ps3_result(result));
6926bb5cf10SGeoff Levand 		r->len = r->bus_addr = 0;
6936bb5cf10SGeoff Levand 	}
6946bb5cf10SGeoff Levand 	DBG("%s: len=%#lx, pg=%d, bus=%#lx\n", __func__,
6956bb5cf10SGeoff Levand 	    r->len, r->page_size, r->bus_addr);
6966bb5cf10SGeoff Levand 	return result;
6976bb5cf10SGeoff Levand }
6986bb5cf10SGeoff Levand 
699f58a9d17SGeoff Levand /**
700f58a9d17SGeoff Levand  * dma_region_free - Free a device dma region.
701f58a9d17SGeoff Levand  * @r: Pointer to a struct ps3_dma_region.
702f58a9d17SGeoff Levand  *
703f58a9d17SGeoff Levand  * This is the lowest level dma region free routine, and is the one that
704f58a9d17SGeoff Levand  * will make the HV call to free the region.
705f58a9d17SGeoff Levand  */
706f58a9d17SGeoff Levand 
7076bb5cf10SGeoff Levand static int dma_sb_region_free(struct ps3_dma_region *r)
708f58a9d17SGeoff Levand {
709f58a9d17SGeoff Levand 	int result;
710f58a9d17SGeoff Levand 	struct dma_chunk *c;
711f58a9d17SGeoff Levand 	struct dma_chunk *tmp;
712f58a9d17SGeoff Levand 
7136bb5cf10SGeoff Levand 	BUG_ON(!r);
7146bb5cf10SGeoff Levand 
7156bb5cf10SGeoff Levand 	if (!r->dev->bus_id) {
7165c949070SStephen Rothwell 		pr_info("%s:%d: %llu:%llu no dma\n", __func__, __LINE__,
7176bb5cf10SGeoff Levand 			r->dev->bus_id, r->dev->dev_id);
7186bb5cf10SGeoff Levand 		return 0;
719f58a9d17SGeoff Levand 	}
720f58a9d17SGeoff Levand 
7216bb5cf10SGeoff Levand 	list_for_each_entry_safe(c, tmp, &r->chunk_list.head, link) {
7226bb5cf10SGeoff Levand 		list_del(&c->link);
7236bb5cf10SGeoff Levand 		dma_sb_free_chunk(c);
7246bb5cf10SGeoff Levand 	}
7256bb5cf10SGeoff Levand 
7266bb5cf10SGeoff Levand 	result = lv1_free_device_dma_region(r->dev->bus_id, r->dev->dev_id,
727f58a9d17SGeoff Levand 		r->bus_addr);
728f58a9d17SGeoff Levand 
729f58a9d17SGeoff Levand 	if (result)
730f58a9d17SGeoff Levand 		DBG("%s:%d: lv1_free_device_dma_region failed: %s\n",
731f58a9d17SGeoff Levand 			__func__, __LINE__, ps3_result(result));
732f58a9d17SGeoff Levand 
7336bb5cf10SGeoff Levand 	r->bus_addr = 0;
7346bb5cf10SGeoff Levand 
7356bb5cf10SGeoff Levand 	return result;
7366bb5cf10SGeoff Levand }
7376bb5cf10SGeoff Levand 
7386bb5cf10SGeoff Levand static int dma_ioc0_region_free(struct ps3_dma_region *r)
7396bb5cf10SGeoff Levand {
7406bb5cf10SGeoff Levand 	int result;
7416bb5cf10SGeoff Levand 	struct dma_chunk *c, *n;
7426bb5cf10SGeoff Levand 
7436bb5cf10SGeoff Levand 	DBG("%s: start\n", __func__);
7446bb5cf10SGeoff Levand 	list_for_each_entry_safe(c, n, &r->chunk_list.head, link) {
7456bb5cf10SGeoff Levand 		list_del(&c->link);
7466bb5cf10SGeoff Levand 		dma_ioc0_free_chunk(c);
7476bb5cf10SGeoff Levand 	}
7486bb5cf10SGeoff Levand 
7496bb5cf10SGeoff Levand 	result = lv1_release_io_segment(0, r->bus_addr);
7506bb5cf10SGeoff Levand 
7516bb5cf10SGeoff Levand 	if (result)
7526bb5cf10SGeoff Levand 		DBG("%s:%d: lv1_free_device_dma_region failed: %s\n",
7536bb5cf10SGeoff Levand 			__func__, __LINE__, ps3_result(result));
7546bb5cf10SGeoff Levand 
7556bb5cf10SGeoff Levand 	r->bus_addr = 0;
7566bb5cf10SGeoff Levand 	DBG("%s: end\n", __func__);
757f58a9d17SGeoff Levand 
758f58a9d17SGeoff Levand 	return result;
759f58a9d17SGeoff Levand }
760f58a9d17SGeoff Levand 
761f58a9d17SGeoff Levand /**
7626bb5cf10SGeoff Levand  * dma_sb_map_area - Map an area of memory into a device dma region.
763f58a9d17SGeoff Levand  * @r: Pointer to a struct ps3_dma_region.
764f58a9d17SGeoff Levand  * @virt_addr: Starting virtual address of the area to map.
765f58a9d17SGeoff Levand  * @len: Length in bytes of the area to map.
766f58a9d17SGeoff Levand  * @bus_addr: A pointer to return the starting ioc bus address of the area to
767f58a9d17SGeoff Levand  * map.
768f58a9d17SGeoff Levand  *
769f58a9d17SGeoff Levand  * This is the common dma mapping routine.
770f58a9d17SGeoff Levand  */
771f58a9d17SGeoff Levand 
7726bb5cf10SGeoff Levand static int dma_sb_map_area(struct ps3_dma_region *r, unsigned long virt_addr,
773494fd07aSStephen Rothwell 	   unsigned long len, dma_addr_t *bus_addr,
7746bb5cf10SGeoff Levand 	   u64 iopte_flag)
775f58a9d17SGeoff Levand {
776f58a9d17SGeoff Levand 	int result;
777f58a9d17SGeoff Levand 	unsigned long flags;
778f58a9d17SGeoff Levand 	struct dma_chunk *c;
779f58a9d17SGeoff Levand 	unsigned long phys_addr = is_kernel_addr(virt_addr) ? __pa(virt_addr)
780f58a9d17SGeoff Levand 		: virt_addr;
781e96d904eSChristophe Leroy 	unsigned long aligned_phys = ALIGN_DOWN(phys_addr, 1 << r->page_size);
782b7115316SChristophe Leroy 	unsigned long aligned_len = ALIGN(len + phys_addr - aligned_phys,
7836bb5cf10SGeoff Levand 					      1 << r->page_size);
7846bb5cf10SGeoff Levand 	*bus_addr = dma_sb_lpar_to_bus(r, ps3_mm_phys_to_lpar(phys_addr));
785f58a9d17SGeoff Levand 
786f58a9d17SGeoff Levand 	if (!USE_DYNAMIC_DMA) {
787f58a9d17SGeoff Levand 		unsigned long lpar_addr = ps3_mm_phys_to_lpar(phys_addr);
788f58a9d17SGeoff Levand 		DBG(" -> %s:%d\n", __func__, __LINE__);
789f58a9d17SGeoff Levand 		DBG("%s:%d virt_addr %lxh\n", __func__, __LINE__,
790f58a9d17SGeoff Levand 			virt_addr);
791f58a9d17SGeoff Levand 		DBG("%s:%d phys_addr %lxh\n", __func__, __LINE__,
792f58a9d17SGeoff Levand 			phys_addr);
793f58a9d17SGeoff Levand 		DBG("%s:%d lpar_addr %lxh\n", __func__, __LINE__,
794f58a9d17SGeoff Levand 			lpar_addr);
795f58a9d17SGeoff Levand 		DBG("%s:%d len       %lxh\n", __func__, __LINE__, len);
796494fd07aSStephen Rothwell 		DBG("%s:%d bus_addr  %llxh (%lxh)\n", __func__, __LINE__,
797f58a9d17SGeoff Levand 		*bus_addr, len);
798f58a9d17SGeoff Levand 	}
799f58a9d17SGeoff Levand 
800f58a9d17SGeoff Levand 	spin_lock_irqsave(&r->chunk_list.lock, flags);
801f58a9d17SGeoff Levand 	c = dma_find_chunk(r, *bus_addr, len);
802f58a9d17SGeoff Levand 
803f58a9d17SGeoff Levand 	if (c) {
8046bb5cf10SGeoff Levand 		DBG("%s:%d: reusing mapped chunk", __func__, __LINE__);
8056bb5cf10SGeoff Levand 		dma_dump_chunk(c);
806f58a9d17SGeoff Levand 		c->usage_count++;
807f58a9d17SGeoff Levand 		spin_unlock_irqrestore(&r->chunk_list.lock, flags);
808f58a9d17SGeoff Levand 		return 0;
809f58a9d17SGeoff Levand 	}
810f58a9d17SGeoff Levand 
8116bb5cf10SGeoff Levand 	result = dma_sb_map_pages(r, aligned_phys, aligned_len, &c, iopte_flag);
812f58a9d17SGeoff Levand 
813f58a9d17SGeoff Levand 	if (result) {
814f58a9d17SGeoff Levand 		*bus_addr = 0;
8156bb5cf10SGeoff Levand 		DBG("%s:%d: dma_sb_map_pages failed (%d)\n",
816f58a9d17SGeoff Levand 			__func__, __LINE__, result);
817f58a9d17SGeoff Levand 		spin_unlock_irqrestore(&r->chunk_list.lock, flags);
818f58a9d17SGeoff Levand 		return result;
819f58a9d17SGeoff Levand 	}
820f58a9d17SGeoff Levand 
821f58a9d17SGeoff Levand 	c->usage_count = 1;
822f58a9d17SGeoff Levand 
823f58a9d17SGeoff Levand 	spin_unlock_irqrestore(&r->chunk_list.lock, flags);
824f58a9d17SGeoff Levand 	return result;
825f58a9d17SGeoff Levand }
826f58a9d17SGeoff Levand 
8276bb5cf10SGeoff Levand static int dma_ioc0_map_area(struct ps3_dma_region *r, unsigned long virt_addr,
828494fd07aSStephen Rothwell 	     unsigned long len, dma_addr_t *bus_addr,
8296bb5cf10SGeoff Levand 	     u64 iopte_flag)
8306bb5cf10SGeoff Levand {
8316bb5cf10SGeoff Levand 	int result;
8326bb5cf10SGeoff Levand 	unsigned long flags;
8336bb5cf10SGeoff Levand 	struct dma_chunk *c;
8346bb5cf10SGeoff Levand 	unsigned long phys_addr = is_kernel_addr(virt_addr) ? __pa(virt_addr)
8356bb5cf10SGeoff Levand 		: virt_addr;
836e96d904eSChristophe Leroy 	unsigned long aligned_phys = ALIGN_DOWN(phys_addr, 1 << r->page_size);
837b7115316SChristophe Leroy 	unsigned long aligned_len = ALIGN(len + phys_addr - aligned_phys,
8386bb5cf10SGeoff Levand 					      1 << r->page_size);
8396bb5cf10SGeoff Levand 
8406bb5cf10SGeoff Levand 	DBG(KERN_ERR "%s: vaddr=%#lx, len=%#lx\n", __func__,
8416bb5cf10SGeoff Levand 	    virt_addr, len);
8426bb5cf10SGeoff Levand 	DBG(KERN_ERR "%s: ph=%#lx a_ph=%#lx a_l=%#lx\n", __func__,
8436bb5cf10SGeoff Levand 	    phys_addr, aligned_phys, aligned_len);
8446bb5cf10SGeoff Levand 
8456bb5cf10SGeoff Levand 	spin_lock_irqsave(&r->chunk_list.lock, flags);
8466bb5cf10SGeoff Levand 	c = dma_find_chunk_lpar(r, ps3_mm_phys_to_lpar(phys_addr), len);
8476bb5cf10SGeoff Levand 
8486bb5cf10SGeoff Levand 	if (c) {
8496bb5cf10SGeoff Levand 		/* FIXME */
8506bb5cf10SGeoff Levand 		BUG();
8516bb5cf10SGeoff Levand 		*bus_addr = c->bus_addr + phys_addr - aligned_phys;
8526bb5cf10SGeoff Levand 		c->usage_count++;
8536bb5cf10SGeoff Levand 		spin_unlock_irqrestore(&r->chunk_list.lock, flags);
8546bb5cf10SGeoff Levand 		return 0;
8556bb5cf10SGeoff Levand 	}
8566bb5cf10SGeoff Levand 
8576bb5cf10SGeoff Levand 	result = dma_ioc0_map_pages(r, aligned_phys, aligned_len, &c,
8586bb5cf10SGeoff Levand 				    iopte_flag);
8596bb5cf10SGeoff Levand 
8606bb5cf10SGeoff Levand 	if (result) {
8616bb5cf10SGeoff Levand 		*bus_addr = 0;
8626bb5cf10SGeoff Levand 		DBG("%s:%d: dma_ioc0_map_pages failed (%d)\n",
8636bb5cf10SGeoff Levand 			__func__, __LINE__, result);
8646bb5cf10SGeoff Levand 		spin_unlock_irqrestore(&r->chunk_list.lock, flags);
8656bb5cf10SGeoff Levand 		return result;
8666bb5cf10SGeoff Levand 	}
8676bb5cf10SGeoff Levand 	*bus_addr = c->bus_addr + phys_addr - aligned_phys;
868494fd07aSStephen Rothwell 	DBG("%s: va=%#lx pa=%#lx a_pa=%#lx bus=%#llx\n", __func__,
8696bb5cf10SGeoff Levand 	    virt_addr, phys_addr, aligned_phys, *bus_addr);
8706bb5cf10SGeoff Levand 	c->usage_count = 1;
8716bb5cf10SGeoff Levand 
8726bb5cf10SGeoff Levand 	spin_unlock_irqrestore(&r->chunk_list.lock, flags);
8736bb5cf10SGeoff Levand 	return result;
8746bb5cf10SGeoff Levand }
8756bb5cf10SGeoff Levand 
876f58a9d17SGeoff Levand /**
8776bb5cf10SGeoff Levand  * dma_sb_unmap_area - Unmap an area of memory from a device dma region.
878f58a9d17SGeoff Levand  * @r: Pointer to a struct ps3_dma_region.
879f58a9d17SGeoff Levand  * @bus_addr: The starting ioc bus address of the area to unmap.
880f58a9d17SGeoff Levand  * @len: Length in bytes of the area to unmap.
881f58a9d17SGeoff Levand  *
882f58a9d17SGeoff Levand  * This is the common dma unmap routine.
883f58a9d17SGeoff Levand  */
884f58a9d17SGeoff Levand 
885494fd07aSStephen Rothwell static int dma_sb_unmap_area(struct ps3_dma_region *r, dma_addr_t bus_addr,
886f58a9d17SGeoff Levand 	unsigned long len)
887f58a9d17SGeoff Levand {
888f58a9d17SGeoff Levand 	unsigned long flags;
889f58a9d17SGeoff Levand 	struct dma_chunk *c;
890f58a9d17SGeoff Levand 
891f58a9d17SGeoff Levand 	spin_lock_irqsave(&r->chunk_list.lock, flags);
892f58a9d17SGeoff Levand 	c = dma_find_chunk(r, bus_addr, len);
893f58a9d17SGeoff Levand 
894f58a9d17SGeoff Levand 	if (!c) {
895e96d904eSChristophe Leroy 		unsigned long aligned_bus = ALIGN_DOWN(bus_addr,
896f58a9d17SGeoff Levand 			1 << r->page_size);
897b7115316SChristophe Leroy 		unsigned long aligned_len = ALIGN(len + bus_addr
8986bb5cf10SGeoff Levand 			- aligned_bus, 1 << r->page_size);
899494fd07aSStephen Rothwell 		DBG("%s:%d: not found: bus_addr %llxh\n",
900f58a9d17SGeoff Levand 			__func__, __LINE__, bus_addr);
901f58a9d17SGeoff Levand 		DBG("%s:%d: not found: len %lxh\n",
902f58a9d17SGeoff Levand 			__func__, __LINE__, len);
903f58a9d17SGeoff Levand 		DBG("%s:%d: not found: aligned_bus %lxh\n",
904f58a9d17SGeoff Levand 			__func__, __LINE__, aligned_bus);
905f58a9d17SGeoff Levand 		DBG("%s:%d: not found: aligned_len %lxh\n",
906f58a9d17SGeoff Levand 			__func__, __LINE__, aligned_len);
907f58a9d17SGeoff Levand 		BUG();
908f58a9d17SGeoff Levand 	}
909f58a9d17SGeoff Levand 
910f58a9d17SGeoff Levand 	c->usage_count--;
911f58a9d17SGeoff Levand 
912f58a9d17SGeoff Levand 	if (!c->usage_count) {
913f58a9d17SGeoff Levand 		list_del(&c->link);
9146bb5cf10SGeoff Levand 		dma_sb_free_chunk(c);
915f58a9d17SGeoff Levand 	}
916f58a9d17SGeoff Levand 
917f58a9d17SGeoff Levand 	spin_unlock_irqrestore(&r->chunk_list.lock, flags);
918f58a9d17SGeoff Levand 	return 0;
919f58a9d17SGeoff Levand }
920f58a9d17SGeoff Levand 
92132f44a12SGeert Uytterhoeven static int dma_ioc0_unmap_area(struct ps3_dma_region *r,
922494fd07aSStephen Rothwell 			dma_addr_t bus_addr, unsigned long len)
9236bb5cf10SGeoff Levand {
9246bb5cf10SGeoff Levand 	unsigned long flags;
9256bb5cf10SGeoff Levand 	struct dma_chunk *c;
9266bb5cf10SGeoff Levand 
927494fd07aSStephen Rothwell 	DBG("%s: start a=%#llx l=%#lx\n", __func__, bus_addr, len);
9286bb5cf10SGeoff Levand 	spin_lock_irqsave(&r->chunk_list.lock, flags);
9296bb5cf10SGeoff Levand 	c = dma_find_chunk(r, bus_addr, len);
9306bb5cf10SGeoff Levand 
9316bb5cf10SGeoff Levand 	if (!c) {
932e96d904eSChristophe Leroy 		unsigned long aligned_bus = ALIGN_DOWN(bus_addr,
9336bb5cf10SGeoff Levand 							1 << r->page_size);
934b7115316SChristophe Leroy 		unsigned long aligned_len = ALIGN(len + bus_addr
9356bb5cf10SGeoff Levand 						      - aligned_bus,
9366bb5cf10SGeoff Levand 						      1 << r->page_size);
937494fd07aSStephen Rothwell 		DBG("%s:%d: not found: bus_addr %llxh\n",
9386bb5cf10SGeoff Levand 		    __func__, __LINE__, bus_addr);
9396bb5cf10SGeoff Levand 		DBG("%s:%d: not found: len %lxh\n",
9406bb5cf10SGeoff Levand 		    __func__, __LINE__, len);
9416bb5cf10SGeoff Levand 		DBG("%s:%d: not found: aligned_bus %lxh\n",
9426bb5cf10SGeoff Levand 		    __func__, __LINE__, aligned_bus);
9436bb5cf10SGeoff Levand 		DBG("%s:%d: not found: aligned_len %lxh\n",
9446bb5cf10SGeoff Levand 		    __func__, __LINE__, aligned_len);
9456bb5cf10SGeoff Levand 		BUG();
9466bb5cf10SGeoff Levand 	}
9476bb5cf10SGeoff Levand 
9486bb5cf10SGeoff Levand 	c->usage_count--;
9496bb5cf10SGeoff Levand 
9506bb5cf10SGeoff Levand 	if (!c->usage_count) {
9516bb5cf10SGeoff Levand 		list_del(&c->link);
9526bb5cf10SGeoff Levand 		dma_ioc0_free_chunk(c);
9536bb5cf10SGeoff Levand 	}
9546bb5cf10SGeoff Levand 
9556bb5cf10SGeoff Levand 	spin_unlock_irqrestore(&r->chunk_list.lock, flags);
9566bb5cf10SGeoff Levand 	DBG("%s: end\n", __func__);
9576bb5cf10SGeoff Levand 	return 0;
9586bb5cf10SGeoff Levand }
9596bb5cf10SGeoff Levand 
960f58a9d17SGeoff Levand /**
9616bb5cf10SGeoff Levand  * dma_sb_region_create_linear - Setup a linear dma mapping for a device.
962f58a9d17SGeoff Levand  * @r: Pointer to a struct ps3_dma_region.
963f58a9d17SGeoff Levand  *
964f58a9d17SGeoff Levand  * This routine creates an HV dma region for the device and maps all available
965f58a9d17SGeoff Levand  * ram into the io controller bus address space.
966f58a9d17SGeoff Levand  */
967f58a9d17SGeoff Levand 
9686bb5cf10SGeoff Levand static int dma_sb_region_create_linear(struct ps3_dma_region *r)
969f58a9d17SGeoff Levand {
970f58a9d17SGeoff Levand 	int result;
971494fd07aSStephen Rothwell 	unsigned long virt_addr, len;
972494fd07aSStephen Rothwell 	dma_addr_t tmp;
973f58a9d17SGeoff Levand 
9746bb5cf10SGeoff Levand 	if (r->len > 16*1024*1024) {	/* FIXME: need proper fix */
975f58a9d17SGeoff Levand 		/* force 16M dma pages for linear mapping */
976f58a9d17SGeoff Levand 		if (r->page_size != PS3_DMA_16M) {
977f58a9d17SGeoff Levand 			pr_info("%s:%d: forcing 16M pages for linear map\n",
978f58a9d17SGeoff Levand 				__func__, __LINE__);
979f58a9d17SGeoff Levand 			r->page_size = PS3_DMA_16M;
980b7115316SChristophe Leroy 			r->len = ALIGN(r->len, 1 << r->page_size);
9816bb5cf10SGeoff Levand 		}
982f58a9d17SGeoff Levand 	}
983f58a9d17SGeoff Levand 
9846bb5cf10SGeoff Levand 	result = dma_sb_region_create(r);
985f58a9d17SGeoff Levand 	BUG_ON(result);
986f58a9d17SGeoff Levand 
9876bb5cf10SGeoff Levand 	if (r->offset < map.rm.size) {
9886bb5cf10SGeoff Levand 		/* Map (part of) 1st RAM chunk */
9896bb5cf10SGeoff Levand 		virt_addr = map.rm.base + r->offset;
9906bb5cf10SGeoff Levand 		len = map.rm.size - r->offset;
9916bb5cf10SGeoff Levand 		if (len > r->len)
9926bb5cf10SGeoff Levand 			len = r->len;
9936bb5cf10SGeoff Levand 		result = dma_sb_map_area(r, virt_addr, len, &tmp,
9945c6fc8dbSGeert Uytterhoeven 			CBE_IOPTE_PP_W | CBE_IOPTE_PP_R | CBE_IOPTE_SO_RW |
9955c6fc8dbSGeert Uytterhoeven 			CBE_IOPTE_M);
996f58a9d17SGeoff Levand 		BUG_ON(result);
9976bb5cf10SGeoff Levand 	}
998f58a9d17SGeoff Levand 
9996bb5cf10SGeoff Levand 	if (r->offset + r->len > map.rm.size) {
10006bb5cf10SGeoff Levand 		/* Map (part of) 2nd RAM chunk */
1001a628df1eSGeoff Levand 		virt_addr = map.rm.size;
10026bb5cf10SGeoff Levand 		len = r->len;
10036bb5cf10SGeoff Levand 		if (r->offset >= map.rm.size)
10046bb5cf10SGeoff Levand 			virt_addr += r->offset - map.rm.size;
1005f58a9d17SGeoff Levand 		else
10066bb5cf10SGeoff Levand 			len -= map.rm.size - r->offset;
10076bb5cf10SGeoff Levand 		result = dma_sb_map_area(r, virt_addr, len, &tmp,
10085c6fc8dbSGeert Uytterhoeven 			CBE_IOPTE_PP_W | CBE_IOPTE_PP_R | CBE_IOPTE_SO_RW |
10095c6fc8dbSGeert Uytterhoeven 			CBE_IOPTE_M);
1010f58a9d17SGeoff Levand 		BUG_ON(result);
10116bb5cf10SGeoff Levand 	}
1012f58a9d17SGeoff Levand 
1013f58a9d17SGeoff Levand 	return result;
1014f58a9d17SGeoff Levand }
1015f58a9d17SGeoff Levand 
1016f58a9d17SGeoff Levand /**
10176bb5cf10SGeoff Levand  * dma_sb_region_free_linear - Free a linear dma mapping for a device.
1018f58a9d17SGeoff Levand  * @r: Pointer to a struct ps3_dma_region.
1019f58a9d17SGeoff Levand  *
1020f58a9d17SGeoff Levand  * This routine will unmap all mapped areas and free the HV dma region.
1021f58a9d17SGeoff Levand  */
1022f58a9d17SGeoff Levand 
10236bb5cf10SGeoff Levand static int dma_sb_region_free_linear(struct ps3_dma_region *r)
1024f58a9d17SGeoff Levand {
1025f58a9d17SGeoff Levand 	int result;
1026494fd07aSStephen Rothwell 	dma_addr_t bus_addr;
1027494fd07aSStephen Rothwell 	unsigned long len, lpar_addr;
1028f58a9d17SGeoff Levand 
10296bb5cf10SGeoff Levand 	if (r->offset < map.rm.size) {
10306bb5cf10SGeoff Levand 		/* Unmap (part of) 1st RAM chunk */
10316bb5cf10SGeoff Levand 		lpar_addr = map.rm.base + r->offset;
10326bb5cf10SGeoff Levand 		len = map.rm.size - r->offset;
10336bb5cf10SGeoff Levand 		if (len > r->len)
10346bb5cf10SGeoff Levand 			len = r->len;
10356bb5cf10SGeoff Levand 		bus_addr = dma_sb_lpar_to_bus(r, lpar_addr);
10366bb5cf10SGeoff Levand 		result = dma_sb_unmap_area(r, bus_addr, len);
1037f58a9d17SGeoff Levand 		BUG_ON(result);
10386bb5cf10SGeoff Levand 	}
1039f58a9d17SGeoff Levand 
10406bb5cf10SGeoff Levand 	if (r->offset + r->len > map.rm.size) {
10416bb5cf10SGeoff Levand 		/* Unmap (part of) 2nd RAM chunk */
10426bb5cf10SGeoff Levand 		lpar_addr = map.r1.base;
10436bb5cf10SGeoff Levand 		len = r->len;
10446bb5cf10SGeoff Levand 		if (r->offset >= map.rm.size)
10456bb5cf10SGeoff Levand 			lpar_addr += r->offset - map.rm.size;
10466bb5cf10SGeoff Levand 		else
10476bb5cf10SGeoff Levand 			len -= map.rm.size - r->offset;
10486bb5cf10SGeoff Levand 		bus_addr = dma_sb_lpar_to_bus(r, lpar_addr);
10496bb5cf10SGeoff Levand 		result = dma_sb_unmap_area(r, bus_addr, len);
1050f58a9d17SGeoff Levand 		BUG_ON(result);
10516bb5cf10SGeoff Levand 	}
1052f58a9d17SGeoff Levand 
10536bb5cf10SGeoff Levand 	result = dma_sb_region_free(r);
1054f58a9d17SGeoff Levand 	BUG_ON(result);
1055f58a9d17SGeoff Levand 
1056f58a9d17SGeoff Levand 	return result;
1057f58a9d17SGeoff Levand }
1058f58a9d17SGeoff Levand 
1059f58a9d17SGeoff Levand /**
10606bb5cf10SGeoff Levand  * dma_sb_map_area_linear - Map an area of memory into a device dma region.
1061f58a9d17SGeoff Levand  * @r: Pointer to a struct ps3_dma_region.
1062f58a9d17SGeoff Levand  * @virt_addr: Starting virtual address of the area to map.
1063f58a9d17SGeoff Levand  * @len: Length in bytes of the area to map.
1064f58a9d17SGeoff Levand  * @bus_addr: A pointer to return the starting ioc bus address of the area to
1065f58a9d17SGeoff Levand  * map.
1066f58a9d17SGeoff Levand  *
10676bb5cf10SGeoff Levand  * This routine just returns the corresponding bus address.  Actual mapping
1068f58a9d17SGeoff Levand  * occurs in dma_region_create_linear().
1069f58a9d17SGeoff Levand  */
1070f58a9d17SGeoff Levand 
10716bb5cf10SGeoff Levand static int dma_sb_map_area_linear(struct ps3_dma_region *r,
1072494fd07aSStephen Rothwell 	unsigned long virt_addr, unsigned long len, dma_addr_t *bus_addr,
10736bb5cf10SGeoff Levand 	u64 iopte_flag)
1074f58a9d17SGeoff Levand {
1075f58a9d17SGeoff Levand 	unsigned long phys_addr = is_kernel_addr(virt_addr) ? __pa(virt_addr)
1076f58a9d17SGeoff Levand 		: virt_addr;
10776bb5cf10SGeoff Levand 	*bus_addr = dma_sb_lpar_to_bus(r, ps3_mm_phys_to_lpar(phys_addr));
1078f58a9d17SGeoff Levand 	return 0;
1079f58a9d17SGeoff Levand }
1080f58a9d17SGeoff Levand 
1081f58a9d17SGeoff Levand /**
1082f58a9d17SGeoff Levand  * dma_unmap_area_linear - Unmap an area of memory from a device dma region.
1083f58a9d17SGeoff Levand  * @r: Pointer to a struct ps3_dma_region.
1084f58a9d17SGeoff Levand  * @bus_addr: The starting ioc bus address of the area to unmap.
1085f58a9d17SGeoff Levand  * @len: Length in bytes of the area to unmap.
1086f58a9d17SGeoff Levand  *
10876bb5cf10SGeoff Levand  * This routine does nothing.  Unmapping occurs in dma_sb_region_free_linear().
1088f58a9d17SGeoff Levand  */
1089f58a9d17SGeoff Levand 
10906bb5cf10SGeoff Levand static int dma_sb_unmap_area_linear(struct ps3_dma_region *r,
1091494fd07aSStephen Rothwell 	dma_addr_t bus_addr, unsigned long len)
1092f58a9d17SGeoff Levand {
1093f58a9d17SGeoff Levand 	return 0;
10946bb5cf10SGeoff Levand };
10956bb5cf10SGeoff Levand 
10966bb5cf10SGeoff Levand static const struct ps3_dma_region_ops ps3_dma_sb_region_ops =  {
10976bb5cf10SGeoff Levand 	.create = dma_sb_region_create,
10986bb5cf10SGeoff Levand 	.free = dma_sb_region_free,
10996bb5cf10SGeoff Levand 	.map = dma_sb_map_area,
11006bb5cf10SGeoff Levand 	.unmap = dma_sb_unmap_area
11016bb5cf10SGeoff Levand };
11026bb5cf10SGeoff Levand 
11036bb5cf10SGeoff Levand static const struct ps3_dma_region_ops ps3_dma_sb_region_linear_ops = {
11046bb5cf10SGeoff Levand 	.create = dma_sb_region_create_linear,
11056bb5cf10SGeoff Levand 	.free = dma_sb_region_free_linear,
11066bb5cf10SGeoff Levand 	.map = dma_sb_map_area_linear,
11076bb5cf10SGeoff Levand 	.unmap = dma_sb_unmap_area_linear
11086bb5cf10SGeoff Levand };
11096bb5cf10SGeoff Levand 
11106bb5cf10SGeoff Levand static const struct ps3_dma_region_ops ps3_dma_ioc0_region_ops = {
11116bb5cf10SGeoff Levand 	.create = dma_ioc0_region_create,
11126bb5cf10SGeoff Levand 	.free = dma_ioc0_region_free,
11136bb5cf10SGeoff Levand 	.map = dma_ioc0_map_area,
11146bb5cf10SGeoff Levand 	.unmap = dma_ioc0_unmap_area
11156bb5cf10SGeoff Levand };
11166bb5cf10SGeoff Levand 
11176bb5cf10SGeoff Levand int ps3_dma_region_init(struct ps3_system_bus_device *dev,
11186bb5cf10SGeoff Levand 	struct ps3_dma_region *r, enum ps3_dma_page_size page_size,
11196bb5cf10SGeoff Levand 	enum ps3_dma_region_type region_type, void *addr, unsigned long len)
11206bb5cf10SGeoff Levand {
11216bb5cf10SGeoff Levand 	unsigned long lpar_addr;
1122*9733862eSGeoff Levand 	int result;
11236bb5cf10SGeoff Levand 
11246bb5cf10SGeoff Levand 	lpar_addr = addr ? ps3_mm_phys_to_lpar(__pa(addr)) : 0;
11256bb5cf10SGeoff Levand 
11266bb5cf10SGeoff Levand 	r->dev = dev;
11276bb5cf10SGeoff Levand 	r->page_size = page_size;
11286bb5cf10SGeoff Levand 	r->region_type = region_type;
11296bb5cf10SGeoff Levand 	r->offset = lpar_addr;
11306bb5cf10SGeoff Levand 	if (r->offset >= map.rm.size)
11316bb5cf10SGeoff Levand 		r->offset -= map.r1.offset;
1132b7115316SChristophe Leroy 	r->len = len ? len : ALIGN(map.total, 1 << r->page_size);
11336bb5cf10SGeoff Levand 
1134*9733862eSGeoff Levand 	dev->core.dma_mask = &r->dma_mask;
1135*9733862eSGeoff Levand 
1136*9733862eSGeoff Levand 	result = dma_set_mask_and_coherent(&dev->core, DMA_BIT_MASK(32));
1137*9733862eSGeoff Levand 
1138*9733862eSGeoff Levand 	if (result < 0) {
1139*9733862eSGeoff Levand 		dev_err(&dev->core, "%s:%d: dma_set_mask_and_coherent failed: %d\n",
1140*9733862eSGeoff Levand 			__func__, __LINE__, result);
1141*9733862eSGeoff Levand 		return result;
1142*9733862eSGeoff Levand 	}
1143*9733862eSGeoff Levand 
11446bb5cf10SGeoff Levand 	switch (dev->dev_type) {
11456bb5cf10SGeoff Levand 	case PS3_DEVICE_TYPE_SB:
11466bb5cf10SGeoff Levand 		r->region_ops =  (USE_DYNAMIC_DMA)
11476bb5cf10SGeoff Levand 			? &ps3_dma_sb_region_ops
11486bb5cf10SGeoff Levand 			: &ps3_dma_sb_region_linear_ops;
11496bb5cf10SGeoff Levand 		break;
11506bb5cf10SGeoff Levand 	case PS3_DEVICE_TYPE_IOC0:
11516bb5cf10SGeoff Levand 		r->region_ops = &ps3_dma_ioc0_region_ops;
11526bb5cf10SGeoff Levand 		break;
11536bb5cf10SGeoff Levand 	default:
11546bb5cf10SGeoff Levand 		BUG();
11556bb5cf10SGeoff Levand 		return -EINVAL;
1156f58a9d17SGeoff Levand 	}
11576bb5cf10SGeoff Levand 	return 0;
11586bb5cf10SGeoff Levand }
11596bb5cf10SGeoff Levand EXPORT_SYMBOL(ps3_dma_region_init);
1160f58a9d17SGeoff Levand 
1161f58a9d17SGeoff Levand int ps3_dma_region_create(struct ps3_dma_region *r)
1162f58a9d17SGeoff Levand {
11636bb5cf10SGeoff Levand 	BUG_ON(!r);
11646bb5cf10SGeoff Levand 	BUG_ON(!r->region_ops);
11656bb5cf10SGeoff Levand 	BUG_ON(!r->region_ops->create);
11666bb5cf10SGeoff Levand 	return r->region_ops->create(r);
1167f58a9d17SGeoff Levand }
11686bb5cf10SGeoff Levand EXPORT_SYMBOL(ps3_dma_region_create);
1169f58a9d17SGeoff Levand 
1170f58a9d17SGeoff Levand int ps3_dma_region_free(struct ps3_dma_region *r)
1171f58a9d17SGeoff Levand {
11726bb5cf10SGeoff Levand 	BUG_ON(!r);
11736bb5cf10SGeoff Levand 	BUG_ON(!r->region_ops);
11746bb5cf10SGeoff Levand 	BUG_ON(!r->region_ops->free);
11756bb5cf10SGeoff Levand 	return r->region_ops->free(r);
1176f58a9d17SGeoff Levand }
11776bb5cf10SGeoff Levand EXPORT_SYMBOL(ps3_dma_region_free);
1178f58a9d17SGeoff Levand 
1179f58a9d17SGeoff Levand int ps3_dma_map(struct ps3_dma_region *r, unsigned long virt_addr,
1180494fd07aSStephen Rothwell 	unsigned long len, dma_addr_t *bus_addr,
11816bb5cf10SGeoff Levand 	u64 iopte_flag)
1182f58a9d17SGeoff Levand {
11836bb5cf10SGeoff Levand 	return r->region_ops->map(r, virt_addr, len, bus_addr, iopte_flag);
1184f58a9d17SGeoff Levand }
1185f58a9d17SGeoff Levand 
1186494fd07aSStephen Rothwell int ps3_dma_unmap(struct ps3_dma_region *r, dma_addr_t bus_addr,
1187f58a9d17SGeoff Levand 	unsigned long len)
1188f58a9d17SGeoff Levand {
11896bb5cf10SGeoff Levand 	return r->region_ops->unmap(r, bus_addr, len);
1190f58a9d17SGeoff Levand }
1191f58a9d17SGeoff Levand 
1192f58a9d17SGeoff Levand /*============================================================================*/
1193f58a9d17SGeoff Levand /* system startup routines                                                    */
1194f58a9d17SGeoff Levand /*============================================================================*/
1195f58a9d17SGeoff Levand 
1196f58a9d17SGeoff Levand /**
1197f58a9d17SGeoff Levand  * ps3_mm_init - initialize the address space state variables
1198f58a9d17SGeoff Levand  */
1199f58a9d17SGeoff Levand 
1200f58a9d17SGeoff Levand void __init ps3_mm_init(void)
1201f58a9d17SGeoff Levand {
1202f58a9d17SGeoff Levand 	int result;
1203f58a9d17SGeoff Levand 
1204f58a9d17SGeoff Levand 	DBG(" -> %s:%d\n", __func__, __LINE__);
1205f58a9d17SGeoff Levand 
1206f58a9d17SGeoff Levand 	result = ps3_repository_read_mm_info(&map.rm.base, &map.rm.size,
1207f58a9d17SGeoff Levand 		&map.total);
1208f58a9d17SGeoff Levand 
1209f58a9d17SGeoff Levand 	if (result)
1210f58a9d17SGeoff Levand 		panic("ps3_repository_read_mm_info() failed");
1211f58a9d17SGeoff Levand 
1212f58a9d17SGeoff Levand 	map.rm.offset = map.rm.base;
1213f58a9d17SGeoff Levand 	map.vas_id = map.htab_size = 0;
1214f58a9d17SGeoff Levand 
1215f58a9d17SGeoff Levand 	/* this implementation assumes map.rm.base is zero */
1216f58a9d17SGeoff Levand 
1217f58a9d17SGeoff Levand 	BUG_ON(map.rm.base);
1218f58a9d17SGeoff Levand 	BUG_ON(!map.rm.size);
1219f58a9d17SGeoff Levand 
12201e755c09SAndre Heider 	/* Check if we got the highmem region from an earlier boot step */
1221f58a9d17SGeoff Levand 
12225ae74630SGeoff Levand 	if (ps3_mm_get_repository_highmem(&map.r1)) {
12235ae74630SGeoff Levand 		result = ps3_mm_region_create(&map.r1, map.total - map.rm.size);
12245ae74630SGeoff Levand 
12255ae74630SGeoff Levand 		if (!result)
12265ae74630SGeoff Levand 			ps3_mm_set_repository_highmem(&map.r1);
12275ae74630SGeoff Levand 	}
1228f58a9d17SGeoff Levand 
12296bb5cf10SGeoff Levand 	/* correct map.total for the real total amount of memory we use */
12306bb5cf10SGeoff Levand 	map.total = map.rm.size + map.r1.size;
12316bb5cf10SGeoff Levand 
12328ac5fd11SHector Martin 	if (!map.r1.size) {
12338ac5fd11SHector Martin 		DBG("%s:%d: No highmem region found\n", __func__, __LINE__);
12348ac5fd11SHector Martin 	} else {
12358ac5fd11SHector Martin 		DBG("%s:%d: Adding highmem region: %llxh %llxh\n",
12368ac5fd11SHector Martin 			__func__, __LINE__, map.rm.size,
12378ac5fd11SHector Martin 			map.total - map.rm.size);
12388ac5fd11SHector Martin 		memblock_add(map.rm.size, map.total - map.rm.size);
12398ac5fd11SHector Martin 	}
12408ac5fd11SHector Martin 
1241f58a9d17SGeoff Levand 	DBG(" <- %s:%d\n", __func__, __LINE__);
1242f58a9d17SGeoff Levand }
1243f58a9d17SGeoff Levand 
1244f58a9d17SGeoff Levand /**
1245f58a9d17SGeoff Levand  * ps3_mm_shutdown - final cleanup of address space
1246f58a9d17SGeoff Levand  */
1247f58a9d17SGeoff Levand 
1248f58a9d17SGeoff Levand void ps3_mm_shutdown(void)
1249f58a9d17SGeoff Levand {
1250f58a9d17SGeoff Levand 	ps3_mm_region_destroy(&map.r1);
1251f58a9d17SGeoff Levand }
1252