xref: /linux/arch/powerpc/platforms/ps3/mm.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
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 
99733862eSGeoff 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>
17f58a9d17SGeoff Levand #include <asm/udbg.h>
18f58a9d17SGeoff Levand #include <asm/lv1call.h>
19ae3a197eSDavid Howells #include <asm/setup.h>
20f58a9d17SGeoff Levand 
21f58a9d17SGeoff Levand #include "platform.h"
22f58a9d17SGeoff Levand 
23f58a9d17SGeoff Levand #if defined(DEBUG)
2483bb643dSGeert Uytterhoeven #define DBG udbg_printf
25f58a9d17SGeoff Levand #else
267424639aSMichael Ellerman #define DBG pr_devel
27f58a9d17SGeoff Levand #endif
28f58a9d17SGeoff Levand 
29f58a9d17SGeoff Levand enum {
30f58a9d17SGeoff Levand #if defined(CONFIG_PS3_DYNAMIC_DMA)
31f58a9d17SGeoff Levand 	USE_DYNAMIC_DMA = 1,
32f58a9d17SGeoff Levand #else
33f58a9d17SGeoff Levand 	USE_DYNAMIC_DMA = 0,
34f58a9d17SGeoff Levand #endif
35f58a9d17SGeoff Levand };
36f58a9d17SGeoff Levand 
37f58a9d17SGeoff Levand enum {
38f58a9d17SGeoff Levand 	PAGE_SHIFT_4K = 12U,
39f58a9d17SGeoff Levand 	PAGE_SHIFT_64K = 16U,
40f58a9d17SGeoff Levand 	PAGE_SHIFT_16M = 24U,
41f58a9d17SGeoff Levand };
42f58a9d17SGeoff Levand 
make_page_sizes(unsigned long a,unsigned long b)43f1ba9b94SNick Child static unsigned long __init make_page_sizes(unsigned long a, unsigned long b)
44f58a9d17SGeoff Levand {
45f58a9d17SGeoff Levand 	return (a << 56) | (b << 48);
46f58a9d17SGeoff Levand }
47f58a9d17SGeoff Levand 
48f58a9d17SGeoff Levand enum {
49f58a9d17SGeoff Levand 	ALLOCATE_MEMORY_TRY_ALT_UNIT = 0X04,
50f58a9d17SGeoff Levand 	ALLOCATE_MEMORY_ADDR_ZERO = 0X08,
51f58a9d17SGeoff Levand };
52f58a9d17SGeoff Levand 
53f58a9d17SGeoff Levand /* valid htab sizes are {18,19,20} = 256K, 512K, 1M */
54f58a9d17SGeoff Levand 
55f58a9d17SGeoff Levand enum {
56f58a9d17SGeoff Levand 	HTAB_SIZE_MAX = 20U, /* HV limit of 1MB */
57f58a9d17SGeoff Levand 	HTAB_SIZE_MIN = 18U, /* CPU limit of 256KB */
58f58a9d17SGeoff Levand };
59f58a9d17SGeoff Levand 
60f58a9d17SGeoff Levand /*============================================================================*/
61f58a9d17SGeoff Levand /* virtual address space routines                                             */
62f58a9d17SGeoff Levand /*============================================================================*/
63f58a9d17SGeoff Levand 
64f58a9d17SGeoff Levand /**
65f58a9d17SGeoff Levand  * struct mem_region - memory region structure
66f58a9d17SGeoff Levand  * @base: base address
67f58a9d17SGeoff Levand  * @size: size in bytes
68f58a9d17SGeoff Levand  * @offset: difference between base and rm.size
691e755c09SAndre Heider  * @destroy: flag if region should be destroyed upon shutdown
70f58a9d17SGeoff Levand  */
71f58a9d17SGeoff Levand 
72f58a9d17SGeoff Levand struct mem_region {
73b17b3df1SStephen Rothwell 	u64 base;
745418b9c6SStephen Rothwell 	u64 size;
75f58a9d17SGeoff Levand 	unsigned long offset;
761e755c09SAndre Heider 	int destroy;
77f58a9d17SGeoff Levand };
78f58a9d17SGeoff Levand 
79f58a9d17SGeoff Levand /**
80f58a9d17SGeoff Levand  * struct map - address space state variables holder
81f58a9d17SGeoff Levand  * @total: total memory available as reported by HV
82f58a9d17SGeoff Levand  * @vas_id - HV virtual address space id
83f58a9d17SGeoff Levand  * @htab_size: htab size in bytes
84f58a9d17SGeoff Levand  *
85f58a9d17SGeoff Levand  * The HV virtual address space (vas) allows for hotplug memory regions.
86f58a9d17SGeoff Levand  * Memory regions can be created and destroyed in the vas at runtime.
87f58a9d17SGeoff Levand  * @rm: real mode (bootmem) region
888ac5fd11SHector Martin  * @r1: highmem region(s)
89f58a9d17SGeoff Levand  *
90f58a9d17SGeoff Levand  * ps3 addresses
91f58a9d17SGeoff Levand  * virt_addr: a cpu 'translated' effective address
92f58a9d17SGeoff Levand  * phys_addr: an address in what Linux thinks is the physical address space
93f58a9d17SGeoff Levand  * lpar_addr: an address in the HV virtual address space
94f58a9d17SGeoff Levand  * bus_addr: an io controller 'translated' address on a device bus
95f58a9d17SGeoff Levand  */
96f58a9d17SGeoff Levand 
97f58a9d17SGeoff Levand struct map {
985418b9c6SStephen Rothwell 	u64 total;
99b17b3df1SStephen Rothwell 	u64 vas_id;
100b17b3df1SStephen Rothwell 	u64 htab_size;
101f58a9d17SGeoff Levand 	struct mem_region rm;
102f58a9d17SGeoff Levand 	struct mem_region r1;
103f58a9d17SGeoff Levand };
104f58a9d17SGeoff Levand 
105f58a9d17SGeoff Levand #define debug_dump_map(x) _debug_dump_map(x, __func__, __LINE__)
_debug_dump_map(const struct map * m,const char * func,int line)1066bb5cf10SGeoff Levand static void __maybe_unused _debug_dump_map(const struct map *m,
1076bb5cf10SGeoff Levand 	const char *func, int line)
108f58a9d17SGeoff Levand {
1095418b9c6SStephen Rothwell 	DBG("%s:%d: map.total     = %llxh\n", func, line, m->total);
1105418b9c6SStephen Rothwell 	DBG("%s:%d: map.rm.size   = %llxh\n", func, line, m->rm.size);
111b17b3df1SStephen Rothwell 	DBG("%s:%d: map.vas_id    = %llu\n", func, line, m->vas_id);
112b17b3df1SStephen Rothwell 	DBG("%s:%d: map.htab_size = %llxh\n", func, line, m->htab_size);
113b17b3df1SStephen Rothwell 	DBG("%s:%d: map.r1.base   = %llxh\n", func, line, m->r1.base);
114f58a9d17SGeoff Levand 	DBG("%s:%d: map.r1.offset = %lxh\n", func, line, m->r1.offset);
1155418b9c6SStephen Rothwell 	DBG("%s:%d: map.r1.size   = %llxh\n", func, line, m->r1.size);
116f58a9d17SGeoff Levand }
117f58a9d17SGeoff Levand 
118f58a9d17SGeoff Levand static struct map map;
119f58a9d17SGeoff Levand 
120f58a9d17SGeoff Levand /**
121f58a9d17SGeoff Levand  * ps3_mm_phys_to_lpar - translate a linux physical address to lpar address
122f58a9d17SGeoff Levand  * @phys_addr: linux physical address
123f58a9d17SGeoff Levand  */
124f58a9d17SGeoff Levand 
ps3_mm_phys_to_lpar(unsigned long phys_addr)125f58a9d17SGeoff Levand unsigned long ps3_mm_phys_to_lpar(unsigned long phys_addr)
126f58a9d17SGeoff Levand {
127f58a9d17SGeoff Levand 	BUG_ON(is_kernel_addr(phys_addr));
128f58a9d17SGeoff Levand 	return (phys_addr < map.rm.size || phys_addr >= map.total)
129f58a9d17SGeoff Levand 		? phys_addr : phys_addr + map.r1.offset;
130f58a9d17SGeoff Levand }
131f58a9d17SGeoff Levand 
132f58a9d17SGeoff Levand EXPORT_SYMBOL(ps3_mm_phys_to_lpar);
133f58a9d17SGeoff Levand 
134f58a9d17SGeoff Levand /**
135f58a9d17SGeoff Levand  * ps3_mm_vas_create - create the virtual address space
136f58a9d17SGeoff Levand  */
137f58a9d17SGeoff Levand 
ps3_mm_vas_create(unsigned long * htab_size)138f58a9d17SGeoff Levand void __init ps3_mm_vas_create(unsigned long* htab_size)
139f58a9d17SGeoff Levand {
140f58a9d17SGeoff Levand 	int result;
141b17b3df1SStephen Rothwell 	u64 start_address;
142b17b3df1SStephen Rothwell 	u64 size;
143b17b3df1SStephen Rothwell 	u64 access_right;
144b17b3df1SStephen Rothwell 	u64 max_page_size;
145b17b3df1SStephen Rothwell 	u64 flags;
146f58a9d17SGeoff Levand 
147f58a9d17SGeoff Levand 	result = lv1_query_logical_partition_address_region_info(0,
148f58a9d17SGeoff Levand 		&start_address, &size, &access_right, &max_page_size,
149f58a9d17SGeoff Levand 		&flags);
150f58a9d17SGeoff Levand 
151f58a9d17SGeoff Levand 	if (result) {
152f58a9d17SGeoff Levand 		DBG("%s:%d: lv1_query_logical_partition_address_region_info "
153f58a9d17SGeoff Levand 			"failed: %s\n", __func__, __LINE__,
154f58a9d17SGeoff Levand 			ps3_result(result));
155f58a9d17SGeoff Levand 		goto fail;
156f58a9d17SGeoff Levand 	}
157f58a9d17SGeoff Levand 
158f58a9d17SGeoff Levand 	if (max_page_size < PAGE_SHIFT_16M) {
159b17b3df1SStephen Rothwell 		DBG("%s:%d: bad max_page_size %llxh\n", __func__, __LINE__,
160f58a9d17SGeoff Levand 			max_page_size);
161f58a9d17SGeoff Levand 		goto fail;
162f58a9d17SGeoff Levand 	}
163f58a9d17SGeoff Levand 
164f58a9d17SGeoff Levand 	BUILD_BUG_ON(CONFIG_PS3_HTAB_SIZE > HTAB_SIZE_MAX);
165f58a9d17SGeoff Levand 	BUILD_BUG_ON(CONFIG_PS3_HTAB_SIZE < HTAB_SIZE_MIN);
166f58a9d17SGeoff Levand 
167f58a9d17SGeoff Levand 	result = lv1_construct_virtual_address_space(CONFIG_PS3_HTAB_SIZE,
168f58a9d17SGeoff Levand 			2, make_page_sizes(PAGE_SHIFT_16M, PAGE_SHIFT_64K),
169f58a9d17SGeoff Levand 			&map.vas_id, &map.htab_size);
170f58a9d17SGeoff Levand 
171f58a9d17SGeoff Levand 	if (result) {
172f58a9d17SGeoff Levand 		DBG("%s:%d: lv1_construct_virtual_address_space failed: %s\n",
173f58a9d17SGeoff Levand 			__func__, __LINE__, ps3_result(result));
174f58a9d17SGeoff Levand 		goto fail;
175f58a9d17SGeoff Levand 	}
176f58a9d17SGeoff Levand 
177f58a9d17SGeoff Levand 	result = lv1_select_virtual_address_space(map.vas_id);
178f58a9d17SGeoff Levand 
179f58a9d17SGeoff Levand 	if (result) {
180f58a9d17SGeoff Levand 		DBG("%s:%d: lv1_select_virtual_address_space failed: %s\n",
181f58a9d17SGeoff Levand 			__func__, __LINE__, ps3_result(result));
182f58a9d17SGeoff Levand 		goto fail;
183f58a9d17SGeoff Levand 	}
184f58a9d17SGeoff Levand 
185f58a9d17SGeoff Levand 	*htab_size = map.htab_size;
186f58a9d17SGeoff Levand 
187f58a9d17SGeoff Levand 	debug_dump_map(&map);
188f58a9d17SGeoff Levand 
189f58a9d17SGeoff Levand 	return;
190f58a9d17SGeoff Levand 
191f58a9d17SGeoff Levand fail:
192f58a9d17SGeoff Levand 	panic("ps3_mm_vas_create failed");
193f58a9d17SGeoff Levand }
194f58a9d17SGeoff Levand 
195f58a9d17SGeoff Levand /**
196f58a9d17SGeoff Levand  * ps3_mm_vas_destroy -
1978119cefdSHari Bathini  *
1988119cefdSHari Bathini  * called during kexec sequence with MMU off.
199f58a9d17SGeoff Levand  */
200f58a9d17SGeoff Levand 
ps3_mm_vas_destroy(void)2018119cefdSHari Bathini notrace void ps3_mm_vas_destroy(void)
202f58a9d17SGeoff Levand {
2036bb5cf10SGeoff Levand 	int result;
2046bb5cf10SGeoff Levand 
205f58a9d17SGeoff Levand 	if (map.vas_id) {
2066bb5cf10SGeoff Levand 		result = lv1_select_virtual_address_space(0);
20712655446SGeoff Levand 		result += lv1_destruct_virtual_address_space(map.vas_id);
20812655446SGeoff Levand 
20912655446SGeoff Levand 		if (result) {
21012655446SGeoff Levand 			lv1_panic(0);
21112655446SGeoff Levand 		}
21212655446SGeoff Levand 
213f58a9d17SGeoff Levand 		map.vas_id = 0;
214f58a9d17SGeoff Levand 	}
215f58a9d17SGeoff Levand }
216f58a9d17SGeoff Levand 
ps3_mm_get_repository_highmem(struct mem_region * r)217f1ba9b94SNick Child static int __init ps3_mm_get_repository_highmem(struct mem_region *r)
218d4b18bd6SGeoff Levand {
219d4b18bd6SGeoff Levand 	int result;
220d4b18bd6SGeoff Levand 
221d4b18bd6SGeoff Levand 	/* Assume a single highmem region. */
222d4b18bd6SGeoff Levand 
223d4b18bd6SGeoff Levand 	result = ps3_repository_read_highmem_info(0, &r->base, &r->size);
224d4b18bd6SGeoff Levand 
225d4b18bd6SGeoff Levand 	if (result)
226d4b18bd6SGeoff Levand 		goto zero_region;
227d4b18bd6SGeoff Levand 
228d4b18bd6SGeoff Levand 	if (!r->base || !r->size) {
229d4b18bd6SGeoff Levand 		result = -1;
230d4b18bd6SGeoff Levand 		goto zero_region;
231d4b18bd6SGeoff Levand 	}
232d4b18bd6SGeoff Levand 
233d4b18bd6SGeoff Levand 	r->offset = r->base - map.rm.size;
234d4b18bd6SGeoff Levand 
235d4b18bd6SGeoff Levand 	DBG("%s:%d: Found high region in repository: %llxh %llxh\n",
236d4b18bd6SGeoff Levand 	    __func__, __LINE__, r->base, r->size);
237d4b18bd6SGeoff Levand 
238d4b18bd6SGeoff Levand 	return 0;
239d4b18bd6SGeoff Levand 
240d4b18bd6SGeoff Levand zero_region:
241d4b18bd6SGeoff Levand 	DBG("%s:%d: No high region in repository.\n", __func__, __LINE__);
242d4b18bd6SGeoff Levand 
243d4b18bd6SGeoff Levand 	r->size = r->base = r->offset = 0;
244d4b18bd6SGeoff Levand 	return result;
245d4b18bd6SGeoff Levand }
246d4b18bd6SGeoff Levand 
ps3_mm_set_repository_highmem(const struct mem_region * r)247d4b18bd6SGeoff Levand static int ps3_mm_set_repository_highmem(const struct mem_region *r)
248d4b18bd6SGeoff Levand {
249d4b18bd6SGeoff Levand 	/* Assume a single highmem region. */
250d4b18bd6SGeoff Levand 
251d4b18bd6SGeoff Levand 	return r ? ps3_repository_write_highmem_info(0, r->base, r->size) :
252d4b18bd6SGeoff Levand 		ps3_repository_write_highmem_info(0, 0, 0);
253d4b18bd6SGeoff Levand }
254d4b18bd6SGeoff Levand 
255f58a9d17SGeoff Levand /**
256f58a9d17SGeoff Levand  * ps3_mm_region_create - create a memory region in the vas
257f58a9d17SGeoff Levand  * @r: pointer to a struct mem_region to accept initialized values
258f58a9d17SGeoff Levand  * @size: requested region size
259f58a9d17SGeoff Levand  *
260f58a9d17SGeoff Levand  * This implementation creates the region with the vas large page size.
261f58a9d17SGeoff Levand  * @size is rounded down to a multiple of the vas large page size.
262f58a9d17SGeoff Levand  */
263f58a9d17SGeoff Levand 
ps3_mm_region_create(struct mem_region * r,unsigned long size)26432f44a12SGeert Uytterhoeven static int ps3_mm_region_create(struct mem_region *r, unsigned long size)
265f58a9d17SGeoff Levand {
266f58a9d17SGeoff Levand 	int result;
267b17b3df1SStephen Rothwell 	u64 muid;
268f58a9d17SGeoff Levand 
269e96d904eSChristophe Leroy 	r->size = ALIGN_DOWN(size, 1 << PAGE_SHIFT_16M);
270f58a9d17SGeoff Levand 
271f58a9d17SGeoff Levand 	DBG("%s:%d requested  %lxh\n", __func__, __LINE__, size);
2725418b9c6SStephen Rothwell 	DBG("%s:%d actual     %llxh\n", __func__, __LINE__, r->size);
2735418b9c6SStephen Rothwell 	DBG("%s:%d difference %llxh (%lluMB)\n", __func__, __LINE__,
2745418b9c6SStephen Rothwell 		size - r->size, (size - r->size) / 1024 / 1024);
275f58a9d17SGeoff Levand 
276f58a9d17SGeoff Levand 	if (r->size == 0) {
277f58a9d17SGeoff Levand 		DBG("%s:%d: size == 0\n", __func__, __LINE__);
278f58a9d17SGeoff Levand 		result = -1;
279f58a9d17SGeoff Levand 		goto zero_region;
280f58a9d17SGeoff Levand 	}
281f58a9d17SGeoff Levand 
282f58a9d17SGeoff Levand 	result = lv1_allocate_memory(r->size, PAGE_SHIFT_16M, 0,
283f58a9d17SGeoff Levand 		ALLOCATE_MEMORY_TRY_ALT_UNIT, &r->base, &muid);
284f58a9d17SGeoff Levand 
285f58a9d17SGeoff Levand 	if (result || r->base < map.rm.size) {
286f58a9d17SGeoff Levand 		DBG("%s:%d: lv1_allocate_memory failed: %s\n",
287f58a9d17SGeoff Levand 			__func__, __LINE__, ps3_result(result));
288f58a9d17SGeoff Levand 		goto zero_region;
289f58a9d17SGeoff Levand 	}
290f58a9d17SGeoff Levand 
2911e755c09SAndre Heider 	r->destroy = 1;
292f58a9d17SGeoff Levand 	r->offset = r->base - map.rm.size;
293f58a9d17SGeoff Levand 	return result;
294f58a9d17SGeoff Levand 
295f58a9d17SGeoff Levand zero_region:
296f58a9d17SGeoff Levand 	r->size = r->base = r->offset = 0;
297f58a9d17SGeoff Levand 	return result;
298f58a9d17SGeoff Levand }
299f58a9d17SGeoff Levand 
300f58a9d17SGeoff Levand /**
301f58a9d17SGeoff Levand  * ps3_mm_region_destroy - destroy a memory region
302f58a9d17SGeoff Levand  * @r: pointer to struct mem_region
303f58a9d17SGeoff Levand  */
304f58a9d17SGeoff Levand 
ps3_mm_region_destroy(struct mem_region * r)30532f44a12SGeert Uytterhoeven static void ps3_mm_region_destroy(struct mem_region *r)
306f58a9d17SGeoff Levand {
3076bb5cf10SGeoff Levand 	int result;
3086bb5cf10SGeoff Levand 
3091e755c09SAndre Heider 	if (!r->destroy) {
3101e755c09SAndre Heider 		return;
3111e755c09SAndre Heider 	}
3121e755c09SAndre Heider 
313f58a9d17SGeoff Levand 	if (r->base) {
3146bb5cf10SGeoff Levand 		result = lv1_release_memory(r->base);
31512655446SGeoff Levand 
31612655446SGeoff Levand 		if (result) {
31712655446SGeoff Levand 			lv1_panic(0);
31812655446SGeoff Levand 		}
31912655446SGeoff Levand 
320f58a9d17SGeoff Levand 		r->size = r->base = r->offset = 0;
321f58a9d17SGeoff Levand 		map.total = map.rm.size;
322f58a9d17SGeoff Levand 	}
32312655446SGeoff Levand 
3245ae74630SGeoff Levand 	ps3_mm_set_repository_highmem(NULL);
325f58a9d17SGeoff Levand }
326f58a9d17SGeoff Levand 
327f58a9d17SGeoff Levand /*============================================================================*/
328f58a9d17SGeoff Levand /* dma routines                                                               */
329f58a9d17SGeoff Levand /*============================================================================*/
330f58a9d17SGeoff Levand 
331f58a9d17SGeoff Levand /**
3326bb5cf10SGeoff Levand  * dma_sb_lpar_to_bus - Translate an lpar address to ioc mapped bus address.
333f58a9d17SGeoff Levand  * @r: pointer to dma region structure
334f58a9d17SGeoff Levand  * @lpar_addr: HV lpar address
335f58a9d17SGeoff Levand  */
336f58a9d17SGeoff Levand 
dma_sb_lpar_to_bus(struct ps3_dma_region * r,unsigned long lpar_addr)3376bb5cf10SGeoff Levand static unsigned long dma_sb_lpar_to_bus(struct ps3_dma_region *r,
338f58a9d17SGeoff Levand 	unsigned long lpar_addr)
339f58a9d17SGeoff Levand {
3406bb5cf10SGeoff Levand 	if (lpar_addr >= map.rm.size)
3416bb5cf10SGeoff Levand 		lpar_addr -= map.r1.offset;
3426bb5cf10SGeoff Levand 	BUG_ON(lpar_addr < r->offset);
3436bb5cf10SGeoff Levand 	BUG_ON(lpar_addr >= r->offset + r->len);
3446bb5cf10SGeoff Levand 	return r->bus_addr + lpar_addr - r->offset;
345f58a9d17SGeoff Levand }
346f58a9d17SGeoff Levand 
347f58a9d17SGeoff Levand #define dma_dump_region(_a) _dma_dump_region(_a, __func__, __LINE__)
_dma_dump_region(const struct ps3_dma_region * r,const char * func,int line)3486bb5cf10SGeoff Levand static void  __maybe_unused _dma_dump_region(const struct ps3_dma_region *r,
3496bb5cf10SGeoff Levand 	const char *func, int line)
350f58a9d17SGeoff Levand {
3515c949070SStephen Rothwell 	DBG("%s:%d: dev        %llu:%llu\n", func, line, r->dev->bus_id,
3526bb5cf10SGeoff Levand 		r->dev->dev_id);
353f58a9d17SGeoff Levand 	DBG("%s:%d: page_size  %u\n", func, line, r->page_size);
354f58a9d17SGeoff Levand 	DBG("%s:%d: bus_addr   %lxh\n", func, line, r->bus_addr);
355f58a9d17SGeoff Levand 	DBG("%s:%d: len        %lxh\n", func, line, r->len);
3566bb5cf10SGeoff Levand 	DBG("%s:%d: offset     %lxh\n", func, line, r->offset);
357f58a9d17SGeoff Levand }
358f58a9d17SGeoff Levand 
359f58a9d17SGeoff Levand   /**
360f58a9d17SGeoff Levand  * dma_chunk - A chunk of dma pages mapped by the io controller.
361f58a9d17SGeoff Levand  * @region - The dma region that owns this chunk.
362f58a9d17SGeoff Levand  * @lpar_addr: Starting lpar address of the area to map.
363f58a9d17SGeoff Levand  * @bus_addr: Starting ioc bus address of the area to map.
364f58a9d17SGeoff Levand  * @len: Length in bytes of the area to map.
365f58a9d17SGeoff Levand  * @link: A struct list_head used with struct ps3_dma_region.chunk_list, the
366*1fd02f66SJulia Lawall  * list of all chunks owned by the region.
367f58a9d17SGeoff Levand  *
368f58a9d17SGeoff Levand  * This implementation uses a very simple dma page manager
369f58a9d17SGeoff Levand  * based on the dma_chunk structure.  This scheme assumes
370f58a9d17SGeoff Levand  * that all drivers use very well behaved dma ops.
371f58a9d17SGeoff Levand  */
372f58a9d17SGeoff Levand 
373f58a9d17SGeoff Levand struct dma_chunk {
374f58a9d17SGeoff Levand 	struct ps3_dma_region *region;
375f58a9d17SGeoff Levand 	unsigned long lpar_addr;
376f58a9d17SGeoff Levand 	unsigned long bus_addr;
377f58a9d17SGeoff Levand 	unsigned long len;
378f58a9d17SGeoff Levand 	struct list_head link;
379f58a9d17SGeoff Levand 	unsigned int usage_count;
380f58a9d17SGeoff Levand };
381f58a9d17SGeoff Levand 
382f58a9d17SGeoff Levand #define dma_dump_chunk(_a) _dma_dump_chunk(_a, __func__, __LINE__)
_dma_dump_chunk(const struct dma_chunk * c,const char * func,int line)383f58a9d17SGeoff Levand static void _dma_dump_chunk (const struct dma_chunk* c, const char* func,
384f58a9d17SGeoff Levand 	int line)
385f58a9d17SGeoff Levand {
3865c949070SStephen Rothwell 	DBG("%s:%d: r.dev        %llu:%llu\n", func, line,
3876bb5cf10SGeoff Levand 		c->region->dev->bus_id, c->region->dev->dev_id);
388f58a9d17SGeoff Levand 	DBG("%s:%d: r.bus_addr   %lxh\n", func, line, c->region->bus_addr);
389f58a9d17SGeoff Levand 	DBG("%s:%d: r.page_size  %u\n", func, line, c->region->page_size);
390f58a9d17SGeoff Levand 	DBG("%s:%d: r.len        %lxh\n", func, line, c->region->len);
3916bb5cf10SGeoff Levand 	DBG("%s:%d: r.offset     %lxh\n", func, line, c->region->offset);
392f58a9d17SGeoff Levand 	DBG("%s:%d: c.lpar_addr  %lxh\n", func, line, c->lpar_addr);
393f58a9d17SGeoff Levand 	DBG("%s:%d: c.bus_addr   %lxh\n", func, line, c->bus_addr);
394f58a9d17SGeoff Levand 	DBG("%s:%d: c.len        %lxh\n", func, line, c->len);
395f58a9d17SGeoff Levand }
396f58a9d17SGeoff Levand 
dma_find_chunk(struct ps3_dma_region * r,unsigned long bus_addr,unsigned long len)397f58a9d17SGeoff Levand static struct dma_chunk * dma_find_chunk(struct ps3_dma_region *r,
398f58a9d17SGeoff Levand 	unsigned long bus_addr, unsigned long len)
399f58a9d17SGeoff Levand {
400f58a9d17SGeoff Levand 	struct dma_chunk *c;
401e96d904eSChristophe Leroy 	unsigned long aligned_bus = ALIGN_DOWN(bus_addr, 1 << r->page_size);
402b7115316SChristophe Leroy 	unsigned long aligned_len = ALIGN(len+bus_addr-aligned_bus,
4036bb5cf10SGeoff Levand 					      1 << r->page_size);
404f58a9d17SGeoff Levand 
405f58a9d17SGeoff Levand 	list_for_each_entry(c, &r->chunk_list.head, link) {
406f58a9d17SGeoff Levand 		/* intersection */
4076bb5cf10SGeoff Levand 		if (aligned_bus >= c->bus_addr &&
4086bb5cf10SGeoff Levand 		    aligned_bus + aligned_len <= c->bus_addr + c->len)
409f58a9d17SGeoff Levand 			return c;
4106bb5cf10SGeoff Levand 
411f58a9d17SGeoff Levand 		/* below */
4126bb5cf10SGeoff Levand 		if (aligned_bus + aligned_len <= c->bus_addr)
413f58a9d17SGeoff Levand 			continue;
4146bb5cf10SGeoff Levand 
415f58a9d17SGeoff Levand 		/* above */
4166bb5cf10SGeoff Levand 		if (aligned_bus >= c->bus_addr + c->len)
417f58a9d17SGeoff Levand 			continue;
418f58a9d17SGeoff Levand 
419f58a9d17SGeoff Levand 		/* we don't handle the multi-chunk case for now */
420f58a9d17SGeoff Levand 		dma_dump_chunk(c);
421f58a9d17SGeoff Levand 		BUG();
422f58a9d17SGeoff Levand 	}
423f58a9d17SGeoff Levand 	return NULL;
424f58a9d17SGeoff Levand }
425f58a9d17SGeoff Levand 
dma_find_chunk_lpar(struct ps3_dma_region * r,unsigned long lpar_addr,unsigned long len)4266bb5cf10SGeoff Levand static struct dma_chunk *dma_find_chunk_lpar(struct ps3_dma_region *r,
4276bb5cf10SGeoff Levand 	unsigned long lpar_addr, unsigned long len)
4286bb5cf10SGeoff Levand {
4296bb5cf10SGeoff Levand 	struct dma_chunk *c;
430e96d904eSChristophe Leroy 	unsigned long aligned_lpar = ALIGN_DOWN(lpar_addr, 1 << r->page_size);
431b7115316SChristophe Leroy 	unsigned long aligned_len = ALIGN(len + lpar_addr - aligned_lpar,
4326bb5cf10SGeoff Levand 					      1 << r->page_size);
4336bb5cf10SGeoff Levand 
4346bb5cf10SGeoff Levand 	list_for_each_entry(c, &r->chunk_list.head, link) {
4356bb5cf10SGeoff Levand 		/* intersection */
4366bb5cf10SGeoff Levand 		if (c->lpar_addr <= aligned_lpar &&
4376bb5cf10SGeoff Levand 		    aligned_lpar < c->lpar_addr + c->len) {
4386bb5cf10SGeoff Levand 			if (aligned_lpar + aligned_len <= c->lpar_addr + c->len)
4396bb5cf10SGeoff Levand 				return c;
4406bb5cf10SGeoff Levand 			else {
4416bb5cf10SGeoff Levand 				dma_dump_chunk(c);
4426bb5cf10SGeoff Levand 				BUG();
4436bb5cf10SGeoff Levand 			}
4446bb5cf10SGeoff Levand 		}
4456bb5cf10SGeoff Levand 		/* below */
4466bb5cf10SGeoff Levand 		if (aligned_lpar + aligned_len <= c->lpar_addr) {
4476bb5cf10SGeoff Levand 			continue;
4486bb5cf10SGeoff Levand 		}
4496bb5cf10SGeoff Levand 		/* above */
4506bb5cf10SGeoff Levand 		if (c->lpar_addr + c->len <= aligned_lpar) {
4516bb5cf10SGeoff Levand 			continue;
4526bb5cf10SGeoff Levand 		}
4536bb5cf10SGeoff Levand 	}
4546bb5cf10SGeoff Levand 	return NULL;
4556bb5cf10SGeoff Levand }
4566bb5cf10SGeoff Levand 
dma_sb_free_chunk(struct dma_chunk * c)4576bb5cf10SGeoff Levand static int dma_sb_free_chunk(struct dma_chunk *c)
458f58a9d17SGeoff Levand {
459f58a9d17SGeoff Levand 	int result = 0;
460f58a9d17SGeoff Levand 
461f58a9d17SGeoff Levand 	if (c->bus_addr) {
4626bb5cf10SGeoff Levand 		result = lv1_unmap_device_dma_region(c->region->dev->bus_id,
4636bb5cf10SGeoff Levand 			c->region->dev->dev_id, c->bus_addr, c->len);
464f58a9d17SGeoff Levand 		BUG_ON(result);
465f58a9d17SGeoff Levand 	}
466f58a9d17SGeoff Levand 
467f58a9d17SGeoff Levand 	kfree(c);
468f58a9d17SGeoff Levand 	return result;
469f58a9d17SGeoff Levand }
470f58a9d17SGeoff Levand 
dma_ioc0_free_chunk(struct dma_chunk * c)4716bb5cf10SGeoff Levand static int dma_ioc0_free_chunk(struct dma_chunk *c)
4726bb5cf10SGeoff Levand {
4736bb5cf10SGeoff Levand 	int result = 0;
4746bb5cf10SGeoff Levand 	int iopage;
4756bb5cf10SGeoff Levand 	unsigned long offset;
4766bb5cf10SGeoff Levand 	struct ps3_dma_region *r = c->region;
4776bb5cf10SGeoff Levand 
4786bb5cf10SGeoff Levand 	DBG("%s:start\n", __func__);
4796bb5cf10SGeoff Levand 	for (iopage = 0; iopage < (c->len >> r->page_size); iopage++) {
4806bb5cf10SGeoff Levand 		offset = (1 << r->page_size) * iopage;
4816bb5cf10SGeoff Levand 		/* put INVALID entry */
4826bb5cf10SGeoff Levand 		result = lv1_put_iopte(0,
4836bb5cf10SGeoff Levand 				       c->bus_addr + offset,
4846bb5cf10SGeoff Levand 				       c->lpar_addr + offset,
4856bb5cf10SGeoff Levand 				       r->ioid,
4866bb5cf10SGeoff Levand 				       0);
4876bb5cf10SGeoff Levand 		DBG("%s: bus=%#lx, lpar=%#lx, ioid=%d\n", __func__,
4886bb5cf10SGeoff Levand 		    c->bus_addr + offset,
4896bb5cf10SGeoff Levand 		    c->lpar_addr + offset,
4906bb5cf10SGeoff Levand 		    r->ioid);
4916bb5cf10SGeoff Levand 
4926bb5cf10SGeoff Levand 		if (result) {
4936bb5cf10SGeoff Levand 			DBG("%s:%d: lv1_put_iopte failed: %s\n", __func__,
4946bb5cf10SGeoff Levand 			    __LINE__, ps3_result(result));
4956bb5cf10SGeoff Levand 		}
4966bb5cf10SGeoff Levand 	}
4976bb5cf10SGeoff Levand 	kfree(c);
4986bb5cf10SGeoff Levand 	DBG("%s:end\n", __func__);
4996bb5cf10SGeoff Levand 	return result;
5006bb5cf10SGeoff Levand }
5016bb5cf10SGeoff Levand 
502f58a9d17SGeoff Levand /**
5036bb5cf10SGeoff Levand  * dma_sb_map_pages - Maps dma pages into the io controller bus address space.
504f58a9d17SGeoff Levand  * @r: Pointer to a struct ps3_dma_region.
505f58a9d17SGeoff Levand  * @phys_addr: Starting physical address of the area to map.
506f58a9d17SGeoff Levand  * @len: Length in bytes of the area to map.
507f58a9d17SGeoff Levand  * c_out: A pointer to receive an allocated struct dma_chunk for this area.
508f58a9d17SGeoff Levand  *
509f58a9d17SGeoff Levand  * This is the lowest level dma mapping routine, and is the one that will
510f58a9d17SGeoff Levand  * make the HV call to add the pages into the io controller address space.
511f58a9d17SGeoff Levand  */
512f58a9d17SGeoff Levand 
dma_sb_map_pages(struct ps3_dma_region * r,unsigned long phys_addr,unsigned long len,struct dma_chunk ** c_out,u64 iopte_flag)5136bb5cf10SGeoff Levand static int dma_sb_map_pages(struct ps3_dma_region *r, unsigned long phys_addr,
5146bb5cf10SGeoff Levand 	    unsigned long len, struct dma_chunk **c_out, u64 iopte_flag)
515f58a9d17SGeoff Levand {
516f58a9d17SGeoff Levand 	int result;
517f58a9d17SGeoff Levand 	struct dma_chunk *c;
518f58a9d17SGeoff Levand 
519a0828cf5SMarkus Elfring 	c = kzalloc(sizeof(*c), GFP_ATOMIC);
520f58a9d17SGeoff Levand 	if (!c) {
521f58a9d17SGeoff Levand 		result = -ENOMEM;
522f58a9d17SGeoff Levand 		goto fail_alloc;
523f58a9d17SGeoff Levand 	}
524f58a9d17SGeoff Levand 
525f58a9d17SGeoff Levand 	c->region = r;
526f58a9d17SGeoff Levand 	c->lpar_addr = ps3_mm_phys_to_lpar(phys_addr);
5276bb5cf10SGeoff Levand 	c->bus_addr = dma_sb_lpar_to_bus(r, c->lpar_addr);
528f58a9d17SGeoff Levand 	c->len = len;
529f58a9d17SGeoff Levand 
5306bb5cf10SGeoff Levand 	BUG_ON(iopte_flag != 0xf800000000000000UL);
5316bb5cf10SGeoff Levand 	result = lv1_map_device_dma_region(c->region->dev->bus_id,
5326bb5cf10SGeoff Levand 					   c->region->dev->dev_id, c->lpar_addr,
5336bb5cf10SGeoff Levand 					   c->bus_addr, c->len, iopte_flag);
534f58a9d17SGeoff Levand 	if (result) {
535f58a9d17SGeoff Levand 		DBG("%s:%d: lv1_map_device_dma_region failed: %s\n",
536f58a9d17SGeoff Levand 			__func__, __LINE__, ps3_result(result));
537f58a9d17SGeoff Levand 		goto fail_map;
538f58a9d17SGeoff Levand 	}
539f58a9d17SGeoff Levand 
540f58a9d17SGeoff Levand 	list_add(&c->link, &r->chunk_list.head);
541f58a9d17SGeoff Levand 
542f58a9d17SGeoff Levand 	*c_out = c;
543f58a9d17SGeoff Levand 	return 0;
544f58a9d17SGeoff Levand 
545f58a9d17SGeoff Levand fail_map:
546f58a9d17SGeoff Levand 	kfree(c);
547f58a9d17SGeoff Levand fail_alloc:
548f58a9d17SGeoff Levand 	*c_out = NULL;
549f58a9d17SGeoff Levand 	DBG(" <- %s:%d\n", __func__, __LINE__);
550f58a9d17SGeoff Levand 	return result;
551f58a9d17SGeoff Levand }
552f58a9d17SGeoff Levand 
dma_ioc0_map_pages(struct ps3_dma_region * r,unsigned long phys_addr,unsigned long len,struct dma_chunk ** c_out,u64 iopte_flag)5536bb5cf10SGeoff Levand static int dma_ioc0_map_pages(struct ps3_dma_region *r, unsigned long phys_addr,
5546bb5cf10SGeoff Levand 			      unsigned long len, struct dma_chunk **c_out,
5556bb5cf10SGeoff Levand 			      u64 iopte_flag)
5566bb5cf10SGeoff Levand {
5576bb5cf10SGeoff Levand 	int result;
5586bb5cf10SGeoff Levand 	struct dma_chunk *c, *last;
5596bb5cf10SGeoff Levand 	int iopage, pages;
5606bb5cf10SGeoff Levand 	unsigned long offset;
5616bb5cf10SGeoff Levand 
5626bb5cf10SGeoff Levand 	DBG(KERN_ERR "%s: phy=%#lx, lpar%#lx, len=%#lx\n", __func__,
5636bb5cf10SGeoff Levand 	    phys_addr, ps3_mm_phys_to_lpar(phys_addr), len);
564a0828cf5SMarkus Elfring 	c = kzalloc(sizeof(*c), GFP_ATOMIC);
5656bb5cf10SGeoff Levand 	if (!c) {
5666bb5cf10SGeoff Levand 		result = -ENOMEM;
5676bb5cf10SGeoff Levand 		goto fail_alloc;
5686bb5cf10SGeoff Levand 	}
5696bb5cf10SGeoff Levand 
5706bb5cf10SGeoff Levand 	c->region = r;
5716bb5cf10SGeoff Levand 	c->len = len;
5726bb5cf10SGeoff Levand 	c->lpar_addr = ps3_mm_phys_to_lpar(phys_addr);
5736bb5cf10SGeoff Levand 	/* allocate IO address */
5746bb5cf10SGeoff Levand 	if (list_empty(&r->chunk_list.head)) {
5756bb5cf10SGeoff Levand 		/* first one */
5766bb5cf10SGeoff Levand 		c->bus_addr = r->bus_addr;
5776bb5cf10SGeoff Levand 	} else {
5786bb5cf10SGeoff Levand 		/* derive from last bus addr*/
5796bb5cf10SGeoff Levand 		last  = list_entry(r->chunk_list.head.next,
5806bb5cf10SGeoff Levand 				   struct dma_chunk, link);
5816bb5cf10SGeoff Levand 		c->bus_addr = last->bus_addr + last->len;
5826bb5cf10SGeoff Levand 		DBG("%s: last bus=%#lx, len=%#lx\n", __func__,
5836bb5cf10SGeoff Levand 		    last->bus_addr, last->len);
5846bb5cf10SGeoff Levand 	}
5856bb5cf10SGeoff Levand 
5866bb5cf10SGeoff Levand 	/* FIXME: check whether length exceeds region size */
5876bb5cf10SGeoff Levand 
5886bb5cf10SGeoff Levand 	/* build ioptes for the area */
5896bb5cf10SGeoff Levand 	pages = len >> r->page_size;
5905c949070SStephen Rothwell 	DBG("%s: pgsize=%#x len=%#lx pages=%#x iopteflag=%#llx\n", __func__,
5916bb5cf10SGeoff Levand 	    r->page_size, r->len, pages, iopte_flag);
5926bb5cf10SGeoff Levand 	for (iopage = 0; iopage < pages; iopage++) {
5936bb5cf10SGeoff Levand 		offset = (1 << r->page_size) * iopage;
5946bb5cf10SGeoff Levand 		result = lv1_put_iopte(0,
5956bb5cf10SGeoff Levand 				       c->bus_addr + offset,
5966bb5cf10SGeoff Levand 				       c->lpar_addr + offset,
5976bb5cf10SGeoff Levand 				       r->ioid,
5986bb5cf10SGeoff Levand 				       iopte_flag);
5996bb5cf10SGeoff Levand 		if (result) {
600f2c2cbccSJoe Perches 			pr_warn("%s:%d: lv1_put_iopte failed: %s\n",
6017e28060aSGeert Uytterhoeven 				__func__, __LINE__, ps3_result(result));
6026bb5cf10SGeoff Levand 			goto fail_map;
6036bb5cf10SGeoff Levand 		}
6046bb5cf10SGeoff Levand 		DBG("%s: pg=%d bus=%#lx, lpar=%#lx, ioid=%#x\n", __func__,
6056bb5cf10SGeoff Levand 		    iopage, c->bus_addr + offset, c->lpar_addr + offset,
6066bb5cf10SGeoff Levand 		    r->ioid);
6076bb5cf10SGeoff Levand 	}
6086bb5cf10SGeoff Levand 
6096bb5cf10SGeoff Levand 	/* be sure that last allocated one is inserted at head */
6106bb5cf10SGeoff Levand 	list_add(&c->link, &r->chunk_list.head);
6116bb5cf10SGeoff Levand 
6126bb5cf10SGeoff Levand 	*c_out = c;
6136bb5cf10SGeoff Levand 	DBG("%s: end\n", __func__);
6146bb5cf10SGeoff Levand 	return 0;
6156bb5cf10SGeoff Levand 
6166bb5cf10SGeoff Levand fail_map:
6176bb5cf10SGeoff Levand 	for (iopage--; 0 <= iopage; iopage--) {
6186bb5cf10SGeoff Levand 		lv1_put_iopte(0,
6196bb5cf10SGeoff Levand 			      c->bus_addr + offset,
6206bb5cf10SGeoff Levand 			      c->lpar_addr + offset,
6216bb5cf10SGeoff Levand 			      r->ioid,
6226bb5cf10SGeoff Levand 			      0);
6236bb5cf10SGeoff Levand 	}
6246bb5cf10SGeoff Levand 	kfree(c);
6256bb5cf10SGeoff Levand fail_alloc:
6266bb5cf10SGeoff Levand 	*c_out = NULL;
6276bb5cf10SGeoff Levand 	return result;
6286bb5cf10SGeoff Levand }
6296bb5cf10SGeoff Levand 
630f58a9d17SGeoff Levand /**
6316bb5cf10SGeoff Levand  * dma_sb_region_create - Create a device dma region.
632f58a9d17SGeoff Levand  * @r: Pointer to a struct ps3_dma_region.
633f58a9d17SGeoff Levand  *
634f58a9d17SGeoff Levand  * This is the lowest level dma region create routine, and is the one that
635f58a9d17SGeoff Levand  * will make the HV call to create the region.
636f58a9d17SGeoff Levand  */
637f58a9d17SGeoff Levand 
dma_sb_region_create(struct ps3_dma_region * r)6386bb5cf10SGeoff Levand static int dma_sb_region_create(struct ps3_dma_region *r)
639f58a9d17SGeoff Levand {
640f58a9d17SGeoff Levand 	int result;
641b17b3df1SStephen Rothwell 	u64 bus_addr;
642f58a9d17SGeoff Levand 
64362d80749SGeoff Levand 	DBG(" -> %s:%d:\n", __func__, __LINE__);
6446bb5cf10SGeoff Levand 
6456bb5cf10SGeoff Levand 	BUG_ON(!r);
6466bb5cf10SGeoff Levand 
6476bb5cf10SGeoff Levand 	if (!r->dev->bus_id) {
6485c949070SStephen Rothwell 		pr_info("%s:%d: %llu:%llu no dma\n", __func__, __LINE__,
6496bb5cf10SGeoff Levand 			r->dev->bus_id, r->dev->dev_id);
6506bb5cf10SGeoff Levand 		return 0;
6516bb5cf10SGeoff Levand 	}
6526bb5cf10SGeoff Levand 
6536bb5cf10SGeoff Levand 	DBG("%s:%u: len = 0x%lx, page_size = %u, offset = 0x%lx\n", __func__,
6546bb5cf10SGeoff Levand 	    __LINE__, r->len, r->page_size, r->offset);
6556bb5cf10SGeoff Levand 
6566bb5cf10SGeoff Levand 	BUG_ON(!r->len);
6576bb5cf10SGeoff Levand 	BUG_ON(!r->page_size);
6586bb5cf10SGeoff Levand 	BUG_ON(!r->region_ops);
6596bb5cf10SGeoff Levand 
660f58a9d17SGeoff Levand 	INIT_LIST_HEAD(&r->chunk_list.head);
661f58a9d17SGeoff Levand 	spin_lock_init(&r->chunk_list.lock);
662f58a9d17SGeoff Levand 
6636bb5cf10SGeoff Levand 	result = lv1_allocate_device_dma_region(r->dev->bus_id, r->dev->dev_id,
6646bb5cf10SGeoff Levand 		roundup_pow_of_two(r->len), r->page_size, r->region_type,
665b17b3df1SStephen Rothwell 		&bus_addr);
666b17b3df1SStephen Rothwell 	r->bus_addr = bus_addr;
667f58a9d17SGeoff Levand 
668f58a9d17SGeoff Levand 	if (result) {
669f58a9d17SGeoff Levand 		DBG("%s:%d: lv1_allocate_device_dma_region failed: %s\n",
670f58a9d17SGeoff Levand 			__func__, __LINE__, ps3_result(result));
671f58a9d17SGeoff Levand 		r->len = r->bus_addr = 0;
672f58a9d17SGeoff Levand 	}
673f58a9d17SGeoff Levand 
674f58a9d17SGeoff Levand 	return result;
675f58a9d17SGeoff Levand }
676f58a9d17SGeoff Levand 
dma_ioc0_region_create(struct ps3_dma_region * r)6776bb5cf10SGeoff Levand static int dma_ioc0_region_create(struct ps3_dma_region *r)
6786bb5cf10SGeoff Levand {
6796bb5cf10SGeoff Levand 	int result;
680b17b3df1SStephen Rothwell 	u64 bus_addr;
6816bb5cf10SGeoff Levand 
6826bb5cf10SGeoff Levand 	INIT_LIST_HEAD(&r->chunk_list.head);
6836bb5cf10SGeoff Levand 	spin_lock_init(&r->chunk_list.lock);
6846bb5cf10SGeoff Levand 
6856bb5cf10SGeoff Levand 	result = lv1_allocate_io_segment(0,
6866bb5cf10SGeoff Levand 					 r->len,
6876bb5cf10SGeoff Levand 					 r->page_size,
688b17b3df1SStephen Rothwell 					 &bus_addr);
689b17b3df1SStephen Rothwell 	r->bus_addr = bus_addr;
6906bb5cf10SGeoff Levand 	if (result) {
6916bb5cf10SGeoff Levand 		DBG("%s:%d: lv1_allocate_io_segment failed: %s\n",
6926bb5cf10SGeoff Levand 			__func__, __LINE__, ps3_result(result));
6936bb5cf10SGeoff Levand 		r->len = r->bus_addr = 0;
6946bb5cf10SGeoff Levand 	}
6956bb5cf10SGeoff Levand 	DBG("%s: len=%#lx, pg=%d, bus=%#lx\n", __func__,
6966bb5cf10SGeoff Levand 	    r->len, r->page_size, r->bus_addr);
6976bb5cf10SGeoff Levand 	return result;
6986bb5cf10SGeoff Levand }
6996bb5cf10SGeoff Levand 
700f58a9d17SGeoff Levand /**
701f58a9d17SGeoff Levand  * dma_region_free - Free a device dma region.
702f58a9d17SGeoff Levand  * @r: Pointer to a struct ps3_dma_region.
703f58a9d17SGeoff Levand  *
704f58a9d17SGeoff Levand  * This is the lowest level dma region free routine, and is the one that
705f58a9d17SGeoff Levand  * will make the HV call to free the region.
706f58a9d17SGeoff Levand  */
707f58a9d17SGeoff Levand 
dma_sb_region_free(struct ps3_dma_region * r)7086bb5cf10SGeoff Levand static int dma_sb_region_free(struct ps3_dma_region *r)
709f58a9d17SGeoff Levand {
710f58a9d17SGeoff Levand 	int result;
711f58a9d17SGeoff Levand 	struct dma_chunk *c;
712f58a9d17SGeoff Levand 	struct dma_chunk *tmp;
713f58a9d17SGeoff Levand 
7146bb5cf10SGeoff Levand 	BUG_ON(!r);
7156bb5cf10SGeoff Levand 
7166bb5cf10SGeoff Levand 	if (!r->dev->bus_id) {
7175c949070SStephen Rothwell 		pr_info("%s:%d: %llu:%llu no dma\n", __func__, __LINE__,
7186bb5cf10SGeoff Levand 			r->dev->bus_id, r->dev->dev_id);
7196bb5cf10SGeoff Levand 		return 0;
720f58a9d17SGeoff Levand 	}
721f58a9d17SGeoff Levand 
7226bb5cf10SGeoff Levand 	list_for_each_entry_safe(c, tmp, &r->chunk_list.head, link) {
7236bb5cf10SGeoff Levand 		list_del(&c->link);
7246bb5cf10SGeoff Levand 		dma_sb_free_chunk(c);
7256bb5cf10SGeoff Levand 	}
7266bb5cf10SGeoff Levand 
7276bb5cf10SGeoff Levand 	result = lv1_free_device_dma_region(r->dev->bus_id, r->dev->dev_id,
728f58a9d17SGeoff Levand 		r->bus_addr);
729f58a9d17SGeoff Levand 
730f58a9d17SGeoff Levand 	if (result)
731f58a9d17SGeoff Levand 		DBG("%s:%d: lv1_free_device_dma_region failed: %s\n",
732f58a9d17SGeoff Levand 			__func__, __LINE__, ps3_result(result));
733f58a9d17SGeoff Levand 
7346bb5cf10SGeoff Levand 	r->bus_addr = 0;
7356bb5cf10SGeoff Levand 
7366bb5cf10SGeoff Levand 	return result;
7376bb5cf10SGeoff Levand }
7386bb5cf10SGeoff Levand 
dma_ioc0_region_free(struct ps3_dma_region * r)7396bb5cf10SGeoff Levand static int dma_ioc0_region_free(struct ps3_dma_region *r)
7406bb5cf10SGeoff Levand {
7416bb5cf10SGeoff Levand 	int result;
7426bb5cf10SGeoff Levand 	struct dma_chunk *c, *n;
7436bb5cf10SGeoff Levand 
7446bb5cf10SGeoff Levand 	DBG("%s: start\n", __func__);
7456bb5cf10SGeoff Levand 	list_for_each_entry_safe(c, n, &r->chunk_list.head, link) {
7466bb5cf10SGeoff Levand 		list_del(&c->link);
7476bb5cf10SGeoff Levand 		dma_ioc0_free_chunk(c);
7486bb5cf10SGeoff Levand 	}
7496bb5cf10SGeoff Levand 
7506bb5cf10SGeoff Levand 	result = lv1_release_io_segment(0, r->bus_addr);
7516bb5cf10SGeoff Levand 
7526bb5cf10SGeoff Levand 	if (result)
7536bb5cf10SGeoff Levand 		DBG("%s:%d: lv1_free_device_dma_region failed: %s\n",
7546bb5cf10SGeoff Levand 			__func__, __LINE__, ps3_result(result));
7556bb5cf10SGeoff Levand 
7566bb5cf10SGeoff Levand 	r->bus_addr = 0;
7576bb5cf10SGeoff Levand 	DBG("%s: end\n", __func__);
758f58a9d17SGeoff Levand 
759f58a9d17SGeoff Levand 	return result;
760f58a9d17SGeoff Levand }
761f58a9d17SGeoff Levand 
762f58a9d17SGeoff Levand /**
7636bb5cf10SGeoff Levand  * dma_sb_map_area - Map an area of memory into a device dma region.
764f58a9d17SGeoff Levand  * @r: Pointer to a struct ps3_dma_region.
765f58a9d17SGeoff Levand  * @virt_addr: Starting virtual address of the area to map.
766f58a9d17SGeoff Levand  * @len: Length in bytes of the area to map.
767f58a9d17SGeoff Levand  * @bus_addr: A pointer to return the starting ioc bus address of the area to
768f58a9d17SGeoff Levand  * map.
769f58a9d17SGeoff Levand  *
770f58a9d17SGeoff Levand  * This is the common dma mapping routine.
771f58a9d17SGeoff Levand  */
772f58a9d17SGeoff Levand 
dma_sb_map_area(struct ps3_dma_region * r,unsigned long virt_addr,unsigned long len,dma_addr_t * bus_addr,u64 iopte_flag)7736bb5cf10SGeoff Levand static int dma_sb_map_area(struct ps3_dma_region *r, unsigned long virt_addr,
774494fd07aSStephen Rothwell 	   unsigned long len, dma_addr_t *bus_addr,
7756bb5cf10SGeoff Levand 	   u64 iopte_flag)
776f58a9d17SGeoff Levand {
777f58a9d17SGeoff Levand 	int result;
778f58a9d17SGeoff Levand 	unsigned long flags;
779f58a9d17SGeoff Levand 	struct dma_chunk *c;
780f58a9d17SGeoff Levand 	unsigned long phys_addr = is_kernel_addr(virt_addr) ? __pa(virt_addr)
781f58a9d17SGeoff Levand 		: virt_addr;
782e96d904eSChristophe Leroy 	unsigned long aligned_phys = ALIGN_DOWN(phys_addr, 1 << r->page_size);
783b7115316SChristophe Leroy 	unsigned long aligned_len = ALIGN(len + phys_addr - aligned_phys,
7846bb5cf10SGeoff Levand 					      1 << r->page_size);
7856bb5cf10SGeoff Levand 	*bus_addr = dma_sb_lpar_to_bus(r, ps3_mm_phys_to_lpar(phys_addr));
786f58a9d17SGeoff Levand 
787f58a9d17SGeoff Levand 	if (!USE_DYNAMIC_DMA) {
788f58a9d17SGeoff Levand 		unsigned long lpar_addr = ps3_mm_phys_to_lpar(phys_addr);
789f58a9d17SGeoff Levand 		DBG(" -> %s:%d\n", __func__, __LINE__);
790f58a9d17SGeoff Levand 		DBG("%s:%d virt_addr %lxh\n", __func__, __LINE__,
791f58a9d17SGeoff Levand 			virt_addr);
792f58a9d17SGeoff Levand 		DBG("%s:%d phys_addr %lxh\n", __func__, __LINE__,
793f58a9d17SGeoff Levand 			phys_addr);
794f58a9d17SGeoff Levand 		DBG("%s:%d lpar_addr %lxh\n", __func__, __LINE__,
795f58a9d17SGeoff Levand 			lpar_addr);
796f58a9d17SGeoff Levand 		DBG("%s:%d len       %lxh\n", __func__, __LINE__, len);
797494fd07aSStephen Rothwell 		DBG("%s:%d bus_addr  %llxh (%lxh)\n", __func__, __LINE__,
798f58a9d17SGeoff Levand 		*bus_addr, len);
799f58a9d17SGeoff Levand 	}
800f58a9d17SGeoff Levand 
801f58a9d17SGeoff Levand 	spin_lock_irqsave(&r->chunk_list.lock, flags);
802f58a9d17SGeoff Levand 	c = dma_find_chunk(r, *bus_addr, len);
803f58a9d17SGeoff Levand 
804f58a9d17SGeoff Levand 	if (c) {
8056bb5cf10SGeoff Levand 		DBG("%s:%d: reusing mapped chunk", __func__, __LINE__);
8066bb5cf10SGeoff Levand 		dma_dump_chunk(c);
807f58a9d17SGeoff Levand 		c->usage_count++;
808f58a9d17SGeoff Levand 		spin_unlock_irqrestore(&r->chunk_list.lock, flags);
809f58a9d17SGeoff Levand 		return 0;
810f58a9d17SGeoff Levand 	}
811f58a9d17SGeoff Levand 
8126bb5cf10SGeoff Levand 	result = dma_sb_map_pages(r, aligned_phys, aligned_len, &c, iopte_flag);
813f58a9d17SGeoff Levand 
814f58a9d17SGeoff Levand 	if (result) {
815f58a9d17SGeoff Levand 		*bus_addr = 0;
8166bb5cf10SGeoff Levand 		DBG("%s:%d: dma_sb_map_pages failed (%d)\n",
817f58a9d17SGeoff Levand 			__func__, __LINE__, result);
818f58a9d17SGeoff Levand 		spin_unlock_irqrestore(&r->chunk_list.lock, flags);
819f58a9d17SGeoff Levand 		return result;
820f58a9d17SGeoff Levand 	}
821f58a9d17SGeoff Levand 
822f58a9d17SGeoff Levand 	c->usage_count = 1;
823f58a9d17SGeoff Levand 
824f58a9d17SGeoff Levand 	spin_unlock_irqrestore(&r->chunk_list.lock, flags);
825f58a9d17SGeoff Levand 	return result;
826f58a9d17SGeoff Levand }
827f58a9d17SGeoff Levand 
dma_ioc0_map_area(struct ps3_dma_region * r,unsigned long virt_addr,unsigned long len,dma_addr_t * bus_addr,u64 iopte_flag)8286bb5cf10SGeoff Levand static int dma_ioc0_map_area(struct ps3_dma_region *r, unsigned long virt_addr,
829494fd07aSStephen Rothwell 	     unsigned long len, dma_addr_t *bus_addr,
8306bb5cf10SGeoff Levand 	     u64 iopte_flag)
8316bb5cf10SGeoff Levand {
8326bb5cf10SGeoff Levand 	int result;
8336bb5cf10SGeoff Levand 	unsigned long flags;
8346bb5cf10SGeoff Levand 	struct dma_chunk *c;
8356bb5cf10SGeoff Levand 	unsigned long phys_addr = is_kernel_addr(virt_addr) ? __pa(virt_addr)
8366bb5cf10SGeoff Levand 		: virt_addr;
837e96d904eSChristophe Leroy 	unsigned long aligned_phys = ALIGN_DOWN(phys_addr, 1 << r->page_size);
838b7115316SChristophe Leroy 	unsigned long aligned_len = ALIGN(len + phys_addr - aligned_phys,
8396bb5cf10SGeoff Levand 					      1 << r->page_size);
8406bb5cf10SGeoff Levand 
8416bb5cf10SGeoff Levand 	DBG(KERN_ERR "%s: vaddr=%#lx, len=%#lx\n", __func__,
8426bb5cf10SGeoff Levand 	    virt_addr, len);
8436bb5cf10SGeoff Levand 	DBG(KERN_ERR "%s: ph=%#lx a_ph=%#lx a_l=%#lx\n", __func__,
8446bb5cf10SGeoff Levand 	    phys_addr, aligned_phys, aligned_len);
8456bb5cf10SGeoff Levand 
8466bb5cf10SGeoff Levand 	spin_lock_irqsave(&r->chunk_list.lock, flags);
8476bb5cf10SGeoff Levand 	c = dma_find_chunk_lpar(r, ps3_mm_phys_to_lpar(phys_addr), len);
8486bb5cf10SGeoff Levand 
8496bb5cf10SGeoff Levand 	if (c) {
8506bb5cf10SGeoff Levand 		/* FIXME */
8516bb5cf10SGeoff Levand 		BUG();
8526bb5cf10SGeoff Levand 		*bus_addr = c->bus_addr + phys_addr - aligned_phys;
8536bb5cf10SGeoff Levand 		c->usage_count++;
8546bb5cf10SGeoff Levand 		spin_unlock_irqrestore(&r->chunk_list.lock, flags);
8556bb5cf10SGeoff Levand 		return 0;
8566bb5cf10SGeoff Levand 	}
8576bb5cf10SGeoff Levand 
8586bb5cf10SGeoff Levand 	result = dma_ioc0_map_pages(r, aligned_phys, aligned_len, &c,
8596bb5cf10SGeoff Levand 				    iopte_flag);
8606bb5cf10SGeoff Levand 
8616bb5cf10SGeoff Levand 	if (result) {
8626bb5cf10SGeoff Levand 		*bus_addr = 0;
8636bb5cf10SGeoff Levand 		DBG("%s:%d: dma_ioc0_map_pages failed (%d)\n",
8646bb5cf10SGeoff Levand 			__func__, __LINE__, result);
8656bb5cf10SGeoff Levand 		spin_unlock_irqrestore(&r->chunk_list.lock, flags);
8666bb5cf10SGeoff Levand 		return result;
8676bb5cf10SGeoff Levand 	}
8686bb5cf10SGeoff Levand 	*bus_addr = c->bus_addr + phys_addr - aligned_phys;
869494fd07aSStephen Rothwell 	DBG("%s: va=%#lx pa=%#lx a_pa=%#lx bus=%#llx\n", __func__,
8706bb5cf10SGeoff Levand 	    virt_addr, phys_addr, aligned_phys, *bus_addr);
8716bb5cf10SGeoff Levand 	c->usage_count = 1;
8726bb5cf10SGeoff Levand 
8736bb5cf10SGeoff Levand 	spin_unlock_irqrestore(&r->chunk_list.lock, flags);
8746bb5cf10SGeoff Levand 	return result;
8756bb5cf10SGeoff Levand }
8766bb5cf10SGeoff Levand 
877f58a9d17SGeoff Levand /**
8786bb5cf10SGeoff Levand  * dma_sb_unmap_area - Unmap an area of memory from a device dma region.
879f58a9d17SGeoff Levand  * @r: Pointer to a struct ps3_dma_region.
880f58a9d17SGeoff Levand  * @bus_addr: The starting ioc bus address of the area to unmap.
881f58a9d17SGeoff Levand  * @len: Length in bytes of the area to unmap.
882f58a9d17SGeoff Levand  *
883f58a9d17SGeoff Levand  * This is the common dma unmap routine.
884f58a9d17SGeoff Levand  */
885f58a9d17SGeoff Levand 
dma_sb_unmap_area(struct ps3_dma_region * r,dma_addr_t bus_addr,unsigned long len)886494fd07aSStephen Rothwell static int dma_sb_unmap_area(struct ps3_dma_region *r, dma_addr_t bus_addr,
887f58a9d17SGeoff Levand 	unsigned long len)
888f58a9d17SGeoff Levand {
889f58a9d17SGeoff Levand 	unsigned long flags;
890f58a9d17SGeoff Levand 	struct dma_chunk *c;
891f58a9d17SGeoff Levand 
892f58a9d17SGeoff Levand 	spin_lock_irqsave(&r->chunk_list.lock, flags);
893f58a9d17SGeoff Levand 	c = dma_find_chunk(r, bus_addr, len);
894f58a9d17SGeoff Levand 
895f58a9d17SGeoff Levand 	if (!c) {
896e96d904eSChristophe Leroy 		unsigned long aligned_bus = ALIGN_DOWN(bus_addr,
897f58a9d17SGeoff Levand 			1 << r->page_size);
898b7115316SChristophe Leroy 		unsigned long aligned_len = ALIGN(len + bus_addr
8996bb5cf10SGeoff Levand 			- aligned_bus, 1 << r->page_size);
900494fd07aSStephen Rothwell 		DBG("%s:%d: not found: bus_addr %llxh\n",
901f58a9d17SGeoff Levand 			__func__, __LINE__, bus_addr);
902f58a9d17SGeoff Levand 		DBG("%s:%d: not found: len %lxh\n",
903f58a9d17SGeoff Levand 			__func__, __LINE__, len);
904f58a9d17SGeoff Levand 		DBG("%s:%d: not found: aligned_bus %lxh\n",
905f58a9d17SGeoff Levand 			__func__, __LINE__, aligned_bus);
906f58a9d17SGeoff Levand 		DBG("%s:%d: not found: aligned_len %lxh\n",
907f58a9d17SGeoff Levand 			__func__, __LINE__, aligned_len);
908f58a9d17SGeoff Levand 		BUG();
909f58a9d17SGeoff Levand 	}
910f58a9d17SGeoff Levand 
911f58a9d17SGeoff Levand 	c->usage_count--;
912f58a9d17SGeoff Levand 
913f58a9d17SGeoff Levand 	if (!c->usage_count) {
914f58a9d17SGeoff Levand 		list_del(&c->link);
9156bb5cf10SGeoff Levand 		dma_sb_free_chunk(c);
916f58a9d17SGeoff Levand 	}
917f58a9d17SGeoff Levand 
918f58a9d17SGeoff Levand 	spin_unlock_irqrestore(&r->chunk_list.lock, flags);
919f58a9d17SGeoff Levand 	return 0;
920f58a9d17SGeoff Levand }
921f58a9d17SGeoff Levand 
dma_ioc0_unmap_area(struct ps3_dma_region * r,dma_addr_t bus_addr,unsigned long len)92232f44a12SGeert Uytterhoeven static int dma_ioc0_unmap_area(struct ps3_dma_region *r,
923494fd07aSStephen Rothwell 			dma_addr_t bus_addr, unsigned long len)
9246bb5cf10SGeoff Levand {
9256bb5cf10SGeoff Levand 	unsigned long flags;
9266bb5cf10SGeoff Levand 	struct dma_chunk *c;
9276bb5cf10SGeoff Levand 
928494fd07aSStephen Rothwell 	DBG("%s: start a=%#llx l=%#lx\n", __func__, bus_addr, len);
9296bb5cf10SGeoff Levand 	spin_lock_irqsave(&r->chunk_list.lock, flags);
9306bb5cf10SGeoff Levand 	c = dma_find_chunk(r, bus_addr, len);
9316bb5cf10SGeoff Levand 
9326bb5cf10SGeoff Levand 	if (!c) {
933e96d904eSChristophe Leroy 		unsigned long aligned_bus = ALIGN_DOWN(bus_addr,
9346bb5cf10SGeoff Levand 							1 << r->page_size);
935b7115316SChristophe Leroy 		unsigned long aligned_len = ALIGN(len + bus_addr
9366bb5cf10SGeoff Levand 						      - aligned_bus,
9376bb5cf10SGeoff Levand 						      1 << r->page_size);
938494fd07aSStephen Rothwell 		DBG("%s:%d: not found: bus_addr %llxh\n",
9396bb5cf10SGeoff Levand 		    __func__, __LINE__, bus_addr);
9406bb5cf10SGeoff Levand 		DBG("%s:%d: not found: len %lxh\n",
9416bb5cf10SGeoff Levand 		    __func__, __LINE__, len);
9426bb5cf10SGeoff Levand 		DBG("%s:%d: not found: aligned_bus %lxh\n",
9436bb5cf10SGeoff Levand 		    __func__, __LINE__, aligned_bus);
9446bb5cf10SGeoff Levand 		DBG("%s:%d: not found: aligned_len %lxh\n",
9456bb5cf10SGeoff Levand 		    __func__, __LINE__, aligned_len);
9466bb5cf10SGeoff Levand 		BUG();
9476bb5cf10SGeoff Levand 	}
9486bb5cf10SGeoff Levand 
9496bb5cf10SGeoff Levand 	c->usage_count--;
9506bb5cf10SGeoff Levand 
9516bb5cf10SGeoff Levand 	if (!c->usage_count) {
9526bb5cf10SGeoff Levand 		list_del(&c->link);
9536bb5cf10SGeoff Levand 		dma_ioc0_free_chunk(c);
9546bb5cf10SGeoff Levand 	}
9556bb5cf10SGeoff Levand 
9566bb5cf10SGeoff Levand 	spin_unlock_irqrestore(&r->chunk_list.lock, flags);
9576bb5cf10SGeoff Levand 	DBG("%s: end\n", __func__);
9586bb5cf10SGeoff Levand 	return 0;
9596bb5cf10SGeoff Levand }
9606bb5cf10SGeoff Levand 
961f58a9d17SGeoff Levand /**
9626bb5cf10SGeoff Levand  * dma_sb_region_create_linear - Setup a linear dma mapping for a device.
963f58a9d17SGeoff Levand  * @r: Pointer to a struct ps3_dma_region.
964f58a9d17SGeoff Levand  *
965f58a9d17SGeoff Levand  * This routine creates an HV dma region for the device and maps all available
966f58a9d17SGeoff Levand  * ram into the io controller bus address space.
967f58a9d17SGeoff Levand  */
968f58a9d17SGeoff Levand 
dma_sb_region_create_linear(struct ps3_dma_region * r)9696bb5cf10SGeoff Levand static int dma_sb_region_create_linear(struct ps3_dma_region *r)
970f58a9d17SGeoff Levand {
971f58a9d17SGeoff Levand 	int result;
972494fd07aSStephen Rothwell 	unsigned long virt_addr, len;
973494fd07aSStephen Rothwell 	dma_addr_t tmp;
974f58a9d17SGeoff Levand 
9756bb5cf10SGeoff Levand 	if (r->len > 16*1024*1024) {	/* FIXME: need proper fix */
976f58a9d17SGeoff Levand 		/* force 16M dma pages for linear mapping */
977f58a9d17SGeoff Levand 		if (r->page_size != PS3_DMA_16M) {
978f58a9d17SGeoff Levand 			pr_info("%s:%d: forcing 16M pages for linear map\n",
979f58a9d17SGeoff Levand 				__func__, __LINE__);
980f58a9d17SGeoff Levand 			r->page_size = PS3_DMA_16M;
981b7115316SChristophe Leroy 			r->len = ALIGN(r->len, 1 << r->page_size);
9826bb5cf10SGeoff Levand 		}
983f58a9d17SGeoff Levand 	}
984f58a9d17SGeoff Levand 
9856bb5cf10SGeoff Levand 	result = dma_sb_region_create(r);
986f58a9d17SGeoff Levand 	BUG_ON(result);
987f58a9d17SGeoff Levand 
9886bb5cf10SGeoff Levand 	if (r->offset < map.rm.size) {
9896bb5cf10SGeoff Levand 		/* Map (part of) 1st RAM chunk */
9906bb5cf10SGeoff Levand 		virt_addr = map.rm.base + r->offset;
9916bb5cf10SGeoff Levand 		len = map.rm.size - r->offset;
9926bb5cf10SGeoff Levand 		if (len > r->len)
9936bb5cf10SGeoff Levand 			len = r->len;
9946bb5cf10SGeoff Levand 		result = dma_sb_map_area(r, virt_addr, len, &tmp,
9955c6fc8dbSGeert Uytterhoeven 			CBE_IOPTE_PP_W | CBE_IOPTE_PP_R | CBE_IOPTE_SO_RW |
9965c6fc8dbSGeert Uytterhoeven 			CBE_IOPTE_M);
997f58a9d17SGeoff Levand 		BUG_ON(result);
9986bb5cf10SGeoff Levand 	}
999f58a9d17SGeoff Levand 
10006bb5cf10SGeoff Levand 	if (r->offset + r->len > map.rm.size) {
10016bb5cf10SGeoff Levand 		/* Map (part of) 2nd RAM chunk */
1002a628df1eSGeoff Levand 		virt_addr = map.rm.size;
10036bb5cf10SGeoff Levand 		len = r->len;
10046bb5cf10SGeoff Levand 		if (r->offset >= map.rm.size)
10056bb5cf10SGeoff Levand 			virt_addr += r->offset - map.rm.size;
1006f58a9d17SGeoff Levand 		else
10076bb5cf10SGeoff Levand 			len -= map.rm.size - r->offset;
10086bb5cf10SGeoff Levand 		result = dma_sb_map_area(r, virt_addr, len, &tmp,
10095c6fc8dbSGeert Uytterhoeven 			CBE_IOPTE_PP_W | CBE_IOPTE_PP_R | CBE_IOPTE_SO_RW |
10105c6fc8dbSGeert Uytterhoeven 			CBE_IOPTE_M);
1011f58a9d17SGeoff Levand 		BUG_ON(result);
10126bb5cf10SGeoff Levand 	}
1013f58a9d17SGeoff Levand 
1014f58a9d17SGeoff Levand 	return result;
1015f58a9d17SGeoff Levand }
1016f58a9d17SGeoff Levand 
1017f58a9d17SGeoff Levand /**
10186bb5cf10SGeoff Levand  * dma_sb_region_free_linear - Free a linear dma mapping for a device.
1019f58a9d17SGeoff Levand  * @r: Pointer to a struct ps3_dma_region.
1020f58a9d17SGeoff Levand  *
1021f58a9d17SGeoff Levand  * This routine will unmap all mapped areas and free the HV dma region.
1022f58a9d17SGeoff Levand  */
1023f58a9d17SGeoff Levand 
dma_sb_region_free_linear(struct ps3_dma_region * r)10246bb5cf10SGeoff Levand static int dma_sb_region_free_linear(struct ps3_dma_region *r)
1025f58a9d17SGeoff Levand {
1026f58a9d17SGeoff Levand 	int result;
1027494fd07aSStephen Rothwell 	dma_addr_t bus_addr;
1028494fd07aSStephen Rothwell 	unsigned long len, lpar_addr;
1029f58a9d17SGeoff Levand 
10306bb5cf10SGeoff Levand 	if (r->offset < map.rm.size) {
10316bb5cf10SGeoff Levand 		/* Unmap (part of) 1st RAM chunk */
10326bb5cf10SGeoff Levand 		lpar_addr = map.rm.base + r->offset;
10336bb5cf10SGeoff Levand 		len = map.rm.size - r->offset;
10346bb5cf10SGeoff Levand 		if (len > r->len)
10356bb5cf10SGeoff Levand 			len = r->len;
10366bb5cf10SGeoff Levand 		bus_addr = dma_sb_lpar_to_bus(r, lpar_addr);
10376bb5cf10SGeoff Levand 		result = dma_sb_unmap_area(r, bus_addr, len);
1038f58a9d17SGeoff Levand 		BUG_ON(result);
10396bb5cf10SGeoff Levand 	}
1040f58a9d17SGeoff Levand 
10416bb5cf10SGeoff Levand 	if (r->offset + r->len > map.rm.size) {
10426bb5cf10SGeoff Levand 		/* Unmap (part of) 2nd RAM chunk */
10436bb5cf10SGeoff Levand 		lpar_addr = map.r1.base;
10446bb5cf10SGeoff Levand 		len = r->len;
10456bb5cf10SGeoff Levand 		if (r->offset >= map.rm.size)
10466bb5cf10SGeoff Levand 			lpar_addr += r->offset - map.rm.size;
10476bb5cf10SGeoff Levand 		else
10486bb5cf10SGeoff Levand 			len -= map.rm.size - r->offset;
10496bb5cf10SGeoff Levand 		bus_addr = dma_sb_lpar_to_bus(r, lpar_addr);
10506bb5cf10SGeoff Levand 		result = dma_sb_unmap_area(r, bus_addr, len);
1051f58a9d17SGeoff Levand 		BUG_ON(result);
10526bb5cf10SGeoff Levand 	}
1053f58a9d17SGeoff Levand 
10546bb5cf10SGeoff Levand 	result = dma_sb_region_free(r);
1055f58a9d17SGeoff Levand 	BUG_ON(result);
1056f58a9d17SGeoff Levand 
1057f58a9d17SGeoff Levand 	return result;
1058f58a9d17SGeoff Levand }
1059f58a9d17SGeoff Levand 
1060f58a9d17SGeoff Levand /**
10616bb5cf10SGeoff Levand  * dma_sb_map_area_linear - Map an area of memory into a device dma region.
1062f58a9d17SGeoff Levand  * @r: Pointer to a struct ps3_dma_region.
1063f58a9d17SGeoff Levand  * @virt_addr: Starting virtual address of the area to map.
1064f58a9d17SGeoff Levand  * @len: Length in bytes of the area to map.
1065f58a9d17SGeoff Levand  * @bus_addr: A pointer to return the starting ioc bus address of the area to
1066f58a9d17SGeoff Levand  * map.
1067f58a9d17SGeoff Levand  *
10686bb5cf10SGeoff Levand  * This routine just returns the corresponding bus address.  Actual mapping
1069f58a9d17SGeoff Levand  * occurs in dma_region_create_linear().
1070f58a9d17SGeoff Levand  */
1071f58a9d17SGeoff Levand 
dma_sb_map_area_linear(struct ps3_dma_region * r,unsigned long virt_addr,unsigned long len,dma_addr_t * bus_addr,u64 iopte_flag)10726bb5cf10SGeoff Levand static int dma_sb_map_area_linear(struct ps3_dma_region *r,
1073494fd07aSStephen Rothwell 	unsigned long virt_addr, unsigned long len, dma_addr_t *bus_addr,
10746bb5cf10SGeoff Levand 	u64 iopte_flag)
1075f58a9d17SGeoff Levand {
1076f58a9d17SGeoff Levand 	unsigned long phys_addr = is_kernel_addr(virt_addr) ? __pa(virt_addr)
1077f58a9d17SGeoff Levand 		: virt_addr;
10786bb5cf10SGeoff Levand 	*bus_addr = dma_sb_lpar_to_bus(r, ps3_mm_phys_to_lpar(phys_addr));
1079f58a9d17SGeoff Levand 	return 0;
1080f58a9d17SGeoff Levand }
1081f58a9d17SGeoff Levand 
1082f58a9d17SGeoff Levand /**
1083f58a9d17SGeoff Levand  * dma_unmap_area_linear - Unmap an area of memory from a device dma region.
1084f58a9d17SGeoff Levand  * @r: Pointer to a struct ps3_dma_region.
1085f58a9d17SGeoff Levand  * @bus_addr: The starting ioc bus address of the area to unmap.
1086f58a9d17SGeoff Levand  * @len: Length in bytes of the area to unmap.
1087f58a9d17SGeoff Levand  *
10886bb5cf10SGeoff Levand  * This routine does nothing.  Unmapping occurs in dma_sb_region_free_linear().
1089f58a9d17SGeoff Levand  */
1090f58a9d17SGeoff Levand 
dma_sb_unmap_area_linear(struct ps3_dma_region * r,dma_addr_t bus_addr,unsigned long len)10916bb5cf10SGeoff Levand static int dma_sb_unmap_area_linear(struct ps3_dma_region *r,
1092494fd07aSStephen Rothwell 	dma_addr_t bus_addr, unsigned long len)
1093f58a9d17SGeoff Levand {
1094f58a9d17SGeoff Levand 	return 0;
10956bb5cf10SGeoff Levand };
10966bb5cf10SGeoff Levand 
10976bb5cf10SGeoff Levand static const struct ps3_dma_region_ops ps3_dma_sb_region_ops =  {
10986bb5cf10SGeoff Levand 	.create = dma_sb_region_create,
10996bb5cf10SGeoff Levand 	.free = dma_sb_region_free,
11006bb5cf10SGeoff Levand 	.map = dma_sb_map_area,
11016bb5cf10SGeoff Levand 	.unmap = dma_sb_unmap_area
11026bb5cf10SGeoff Levand };
11036bb5cf10SGeoff Levand 
11046bb5cf10SGeoff Levand static const struct ps3_dma_region_ops ps3_dma_sb_region_linear_ops = {
11056bb5cf10SGeoff Levand 	.create = dma_sb_region_create_linear,
11066bb5cf10SGeoff Levand 	.free = dma_sb_region_free_linear,
11076bb5cf10SGeoff Levand 	.map = dma_sb_map_area_linear,
11086bb5cf10SGeoff Levand 	.unmap = dma_sb_unmap_area_linear
11096bb5cf10SGeoff Levand };
11106bb5cf10SGeoff Levand 
11116bb5cf10SGeoff Levand static const struct ps3_dma_region_ops ps3_dma_ioc0_region_ops = {
11126bb5cf10SGeoff Levand 	.create = dma_ioc0_region_create,
11136bb5cf10SGeoff Levand 	.free = dma_ioc0_region_free,
11146bb5cf10SGeoff Levand 	.map = dma_ioc0_map_area,
11156bb5cf10SGeoff Levand 	.unmap = dma_ioc0_unmap_area
11166bb5cf10SGeoff Levand };
11176bb5cf10SGeoff Levand 
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)11186bb5cf10SGeoff Levand int ps3_dma_region_init(struct ps3_system_bus_device *dev,
11196bb5cf10SGeoff Levand 	struct ps3_dma_region *r, enum ps3_dma_page_size page_size,
11206bb5cf10SGeoff Levand 	enum ps3_dma_region_type region_type, void *addr, unsigned long len)
11216bb5cf10SGeoff Levand {
11226bb5cf10SGeoff Levand 	unsigned long lpar_addr;
11239733862eSGeoff Levand 	int result;
11246bb5cf10SGeoff Levand 
11256bb5cf10SGeoff Levand 	lpar_addr = addr ? ps3_mm_phys_to_lpar(__pa(addr)) : 0;
11266bb5cf10SGeoff Levand 
11276bb5cf10SGeoff Levand 	r->dev = dev;
11286bb5cf10SGeoff Levand 	r->page_size = page_size;
11296bb5cf10SGeoff Levand 	r->region_type = region_type;
11306bb5cf10SGeoff Levand 	r->offset = lpar_addr;
11316bb5cf10SGeoff Levand 	if (r->offset >= map.rm.size)
11326bb5cf10SGeoff Levand 		r->offset -= map.r1.offset;
1133b7115316SChristophe Leroy 	r->len = len ? len : ALIGN(map.total, 1 << r->page_size);
11346bb5cf10SGeoff Levand 
11359733862eSGeoff Levand 	dev->core.dma_mask = &r->dma_mask;
11369733862eSGeoff Levand 
11379733862eSGeoff Levand 	result = dma_set_mask_and_coherent(&dev->core, DMA_BIT_MASK(32));
11389733862eSGeoff Levand 
11399733862eSGeoff Levand 	if (result < 0) {
11409733862eSGeoff Levand 		dev_err(&dev->core, "%s:%d: dma_set_mask_and_coherent failed: %d\n",
11419733862eSGeoff Levand 			__func__, __LINE__, result);
11429733862eSGeoff Levand 		return result;
11439733862eSGeoff Levand 	}
11449733862eSGeoff Levand 
11456bb5cf10SGeoff Levand 	switch (dev->dev_type) {
11466bb5cf10SGeoff Levand 	case PS3_DEVICE_TYPE_SB:
11476bb5cf10SGeoff Levand 		r->region_ops =  (USE_DYNAMIC_DMA)
11486bb5cf10SGeoff Levand 			? &ps3_dma_sb_region_ops
11496bb5cf10SGeoff Levand 			: &ps3_dma_sb_region_linear_ops;
11506bb5cf10SGeoff Levand 		break;
11516bb5cf10SGeoff Levand 	case PS3_DEVICE_TYPE_IOC0:
11526bb5cf10SGeoff Levand 		r->region_ops = &ps3_dma_ioc0_region_ops;
11536bb5cf10SGeoff Levand 		break;
11546bb5cf10SGeoff Levand 	default:
11556bb5cf10SGeoff Levand 		BUG();
11566bb5cf10SGeoff Levand 		return -EINVAL;
1157f58a9d17SGeoff Levand 	}
11586bb5cf10SGeoff Levand 	return 0;
11596bb5cf10SGeoff Levand }
11606bb5cf10SGeoff Levand EXPORT_SYMBOL(ps3_dma_region_init);
1161f58a9d17SGeoff Levand 
ps3_dma_region_create(struct ps3_dma_region * r)1162f58a9d17SGeoff Levand int ps3_dma_region_create(struct ps3_dma_region *r)
1163f58a9d17SGeoff Levand {
11646bb5cf10SGeoff Levand 	BUG_ON(!r);
11656bb5cf10SGeoff Levand 	BUG_ON(!r->region_ops);
11666bb5cf10SGeoff Levand 	BUG_ON(!r->region_ops->create);
11676bb5cf10SGeoff Levand 	return r->region_ops->create(r);
1168f58a9d17SGeoff Levand }
11696bb5cf10SGeoff Levand EXPORT_SYMBOL(ps3_dma_region_create);
1170f58a9d17SGeoff Levand 
ps3_dma_region_free(struct ps3_dma_region * r)1171f58a9d17SGeoff Levand int ps3_dma_region_free(struct ps3_dma_region *r)
1172f58a9d17SGeoff Levand {
11736bb5cf10SGeoff Levand 	BUG_ON(!r);
11746bb5cf10SGeoff Levand 	BUG_ON(!r->region_ops);
11756bb5cf10SGeoff Levand 	BUG_ON(!r->region_ops->free);
11766bb5cf10SGeoff Levand 	return r->region_ops->free(r);
1177f58a9d17SGeoff Levand }
11786bb5cf10SGeoff Levand EXPORT_SYMBOL(ps3_dma_region_free);
1179f58a9d17SGeoff Levand 
ps3_dma_map(struct ps3_dma_region * r,unsigned long virt_addr,unsigned long len,dma_addr_t * bus_addr,u64 iopte_flag)1180f58a9d17SGeoff Levand int ps3_dma_map(struct ps3_dma_region *r, unsigned long virt_addr,
1181494fd07aSStephen Rothwell 	unsigned long len, dma_addr_t *bus_addr,
11826bb5cf10SGeoff Levand 	u64 iopte_flag)
1183f58a9d17SGeoff Levand {
11846bb5cf10SGeoff Levand 	return r->region_ops->map(r, virt_addr, len, bus_addr, iopte_flag);
1185f58a9d17SGeoff Levand }
1186f58a9d17SGeoff Levand 
ps3_dma_unmap(struct ps3_dma_region * r,dma_addr_t bus_addr,unsigned long len)1187494fd07aSStephen Rothwell int ps3_dma_unmap(struct ps3_dma_region *r, dma_addr_t bus_addr,
1188f58a9d17SGeoff Levand 	unsigned long len)
1189f58a9d17SGeoff Levand {
11906bb5cf10SGeoff Levand 	return r->region_ops->unmap(r, bus_addr, len);
1191f58a9d17SGeoff Levand }
1192f58a9d17SGeoff Levand 
1193f58a9d17SGeoff Levand /*============================================================================*/
1194f58a9d17SGeoff Levand /* system startup routines                                                    */
1195f58a9d17SGeoff Levand /*============================================================================*/
1196f58a9d17SGeoff Levand 
1197f58a9d17SGeoff Levand /**
1198f58a9d17SGeoff Levand  * ps3_mm_init - initialize the address space state variables
1199f58a9d17SGeoff Levand  */
1200f58a9d17SGeoff Levand 
ps3_mm_init(void)1201f58a9d17SGeoff Levand void __init ps3_mm_init(void)
1202f58a9d17SGeoff Levand {
1203f58a9d17SGeoff Levand 	int result;
1204f58a9d17SGeoff Levand 
1205f58a9d17SGeoff Levand 	DBG(" -> %s:%d\n", __func__, __LINE__);
1206f58a9d17SGeoff Levand 
1207f58a9d17SGeoff Levand 	result = ps3_repository_read_mm_info(&map.rm.base, &map.rm.size,
1208f58a9d17SGeoff Levand 		&map.total);
1209f58a9d17SGeoff Levand 
1210f58a9d17SGeoff Levand 	if (result)
1211f58a9d17SGeoff Levand 		panic("ps3_repository_read_mm_info() failed");
1212f58a9d17SGeoff Levand 
1213f58a9d17SGeoff Levand 	map.rm.offset = map.rm.base;
1214f58a9d17SGeoff Levand 	map.vas_id = map.htab_size = 0;
1215f58a9d17SGeoff Levand 
1216f58a9d17SGeoff Levand 	/* this implementation assumes map.rm.base is zero */
1217f58a9d17SGeoff Levand 
1218f58a9d17SGeoff Levand 	BUG_ON(map.rm.base);
1219f58a9d17SGeoff Levand 	BUG_ON(!map.rm.size);
1220f58a9d17SGeoff Levand 
12211e755c09SAndre Heider 	/* Check if we got the highmem region from an earlier boot step */
1222f58a9d17SGeoff Levand 
12235ae74630SGeoff Levand 	if (ps3_mm_get_repository_highmem(&map.r1)) {
12245ae74630SGeoff Levand 		result = ps3_mm_region_create(&map.r1, map.total - map.rm.size);
12255ae74630SGeoff Levand 
12265ae74630SGeoff Levand 		if (!result)
12275ae74630SGeoff Levand 			ps3_mm_set_repository_highmem(&map.r1);
12285ae74630SGeoff Levand 	}
1229f58a9d17SGeoff Levand 
12306bb5cf10SGeoff Levand 	/* correct map.total for the real total amount of memory we use */
12316bb5cf10SGeoff Levand 	map.total = map.rm.size + map.r1.size;
12326bb5cf10SGeoff Levand 
12338ac5fd11SHector Martin 	if (!map.r1.size) {
12348ac5fd11SHector Martin 		DBG("%s:%d: No highmem region found\n", __func__, __LINE__);
12358ac5fd11SHector Martin 	} else {
12368ac5fd11SHector Martin 		DBG("%s:%d: Adding highmem region: %llxh %llxh\n",
12378ac5fd11SHector Martin 			__func__, __LINE__, map.rm.size,
12388ac5fd11SHector Martin 			map.total - map.rm.size);
12398ac5fd11SHector Martin 		memblock_add(map.rm.size, map.total - map.rm.size);
12408ac5fd11SHector Martin 	}
12418ac5fd11SHector Martin 
1242f58a9d17SGeoff Levand 	DBG(" <- %s:%d\n", __func__, __LINE__);
1243f58a9d17SGeoff Levand }
1244f58a9d17SGeoff Levand 
1245f58a9d17SGeoff Levand /**
1246f58a9d17SGeoff Levand  * ps3_mm_shutdown - final cleanup of address space
12478119cefdSHari Bathini  *
12488119cefdSHari Bathini  * called during kexec sequence with MMU off.
1249f58a9d17SGeoff Levand  */
1250f58a9d17SGeoff Levand 
ps3_mm_shutdown(void)12518119cefdSHari Bathini notrace void ps3_mm_shutdown(void)
1252f58a9d17SGeoff Levand {
1253f58a9d17SGeoff Levand 	ps3_mm_region_destroy(&map.r1);
1254f58a9d17SGeoff Levand }
1255