xref: /openbmc/linux/arch/powerpc/platforms/ps3/mm.c (revision e96d904ede6756641563d27daa746875b478a6c8)
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 
9f58a9d17SGeoff Levand #include <linux/kernel.h>
104b16f8e2SPaul Gortmaker #include <linux/export.h>
1195f72d1eSYinghai Lu #include <linux/memblock.h>
125a0e3ad6STejun Heo #include <linux/slab.h>
13f58a9d17SGeoff Levand 
149413c883SGeert Uytterhoeven #include <asm/cell-regs.h>
15e22ba7e3SArnd Bergmann #include <asm/firmware.h>
16d9b2b2a2SDavid S. Miller #include <asm/prom.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 
43f58a9d17SGeoff Levand static unsigned long 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__)
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 
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 
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 -
197f58a9d17SGeoff Levand  */
198f58a9d17SGeoff Levand 
199f58a9d17SGeoff Levand void ps3_mm_vas_destroy(void)
200f58a9d17SGeoff Levand {
2016bb5cf10SGeoff Levand 	int result;
2026bb5cf10SGeoff Levand 
203b17b3df1SStephen Rothwell 	DBG("%s:%d: map.vas_id    = %llu\n", __func__, __LINE__, map.vas_id);
2046bb5cf10SGeoff Levand 
205f58a9d17SGeoff Levand 	if (map.vas_id) {
2066bb5cf10SGeoff Levand 		result = lv1_select_virtual_address_space(0);
2076bb5cf10SGeoff Levand 		BUG_ON(result);
2086bb5cf10SGeoff Levand 		result = lv1_destruct_virtual_address_space(map.vas_id);
2096bb5cf10SGeoff Levand 		BUG_ON(result);
210f58a9d17SGeoff Levand 		map.vas_id = 0;
211f58a9d17SGeoff Levand 	}
212f58a9d17SGeoff Levand }
213f58a9d17SGeoff Levand 
214d4b18bd6SGeoff Levand static int ps3_mm_get_repository_highmem(struct mem_region *r)
215d4b18bd6SGeoff Levand {
216d4b18bd6SGeoff Levand 	int result;
217d4b18bd6SGeoff Levand 
218d4b18bd6SGeoff Levand 	/* Assume a single highmem region. */
219d4b18bd6SGeoff Levand 
220d4b18bd6SGeoff Levand 	result = ps3_repository_read_highmem_info(0, &r->base, &r->size);
221d4b18bd6SGeoff Levand 
222d4b18bd6SGeoff Levand 	if (result)
223d4b18bd6SGeoff Levand 		goto zero_region;
224d4b18bd6SGeoff Levand 
225d4b18bd6SGeoff Levand 	if (!r->base || !r->size) {
226d4b18bd6SGeoff Levand 		result = -1;
227d4b18bd6SGeoff Levand 		goto zero_region;
228d4b18bd6SGeoff Levand 	}
229d4b18bd6SGeoff Levand 
230d4b18bd6SGeoff Levand 	r->offset = r->base - map.rm.size;
231d4b18bd6SGeoff Levand 
232d4b18bd6SGeoff Levand 	DBG("%s:%d: Found high region in repository: %llxh %llxh\n",
233d4b18bd6SGeoff Levand 	    __func__, __LINE__, r->base, r->size);
234d4b18bd6SGeoff Levand 
235d4b18bd6SGeoff Levand 	return 0;
236d4b18bd6SGeoff Levand 
237d4b18bd6SGeoff Levand zero_region:
238d4b18bd6SGeoff Levand 	DBG("%s:%d: No high region in repository.\n", __func__, __LINE__);
239d4b18bd6SGeoff Levand 
240d4b18bd6SGeoff Levand 	r->size = r->base = r->offset = 0;
241d4b18bd6SGeoff Levand 	return result;
242d4b18bd6SGeoff Levand }
243d4b18bd6SGeoff Levand 
244d4b18bd6SGeoff Levand static int ps3_mm_set_repository_highmem(const struct mem_region *r)
245d4b18bd6SGeoff Levand {
246d4b18bd6SGeoff Levand 	/* Assume a single highmem region. */
247d4b18bd6SGeoff Levand 
248d4b18bd6SGeoff Levand 	return r ? ps3_repository_write_highmem_info(0, r->base, r->size) :
249d4b18bd6SGeoff Levand 		ps3_repository_write_highmem_info(0, 0, 0);
250d4b18bd6SGeoff Levand }
251d4b18bd6SGeoff Levand 
252f58a9d17SGeoff Levand /**
253f58a9d17SGeoff Levand  * ps3_mm_region_create - create a memory region in the vas
254f58a9d17SGeoff Levand  * @r: pointer to a struct mem_region to accept initialized values
255f58a9d17SGeoff Levand  * @size: requested region size
256f58a9d17SGeoff Levand  *
257f58a9d17SGeoff Levand  * This implementation creates the region with the vas large page size.
258f58a9d17SGeoff Levand  * @size is rounded down to a multiple of the vas large page size.
259f58a9d17SGeoff Levand  */
260f58a9d17SGeoff Levand 
26132f44a12SGeert Uytterhoeven static int ps3_mm_region_create(struct mem_region *r, unsigned long size)
262f58a9d17SGeoff Levand {
263f58a9d17SGeoff Levand 	int result;
264b17b3df1SStephen Rothwell 	u64 muid;
265f58a9d17SGeoff Levand 
266*e96d904eSChristophe Leroy 	r->size = ALIGN_DOWN(size, 1 << PAGE_SHIFT_16M);
267f58a9d17SGeoff Levand 
268f58a9d17SGeoff Levand 	DBG("%s:%d requested  %lxh\n", __func__, __LINE__, size);
2695418b9c6SStephen Rothwell 	DBG("%s:%d actual     %llxh\n", __func__, __LINE__, r->size);
2705418b9c6SStephen Rothwell 	DBG("%s:%d difference %llxh (%lluMB)\n", __func__, __LINE__,
2715418b9c6SStephen Rothwell 		size - r->size, (size - r->size) / 1024 / 1024);
272f58a9d17SGeoff Levand 
273f58a9d17SGeoff Levand 	if (r->size == 0) {
274f58a9d17SGeoff Levand 		DBG("%s:%d: size == 0\n", __func__, __LINE__);
275f58a9d17SGeoff Levand 		result = -1;
276f58a9d17SGeoff Levand 		goto zero_region;
277f58a9d17SGeoff Levand 	}
278f58a9d17SGeoff Levand 
279f58a9d17SGeoff Levand 	result = lv1_allocate_memory(r->size, PAGE_SHIFT_16M, 0,
280f58a9d17SGeoff Levand 		ALLOCATE_MEMORY_TRY_ALT_UNIT, &r->base, &muid);
281f58a9d17SGeoff Levand 
282f58a9d17SGeoff Levand 	if (result || r->base < map.rm.size) {
283f58a9d17SGeoff Levand 		DBG("%s:%d: lv1_allocate_memory failed: %s\n",
284f58a9d17SGeoff Levand 			__func__, __LINE__, ps3_result(result));
285f58a9d17SGeoff Levand 		goto zero_region;
286f58a9d17SGeoff Levand 	}
287f58a9d17SGeoff Levand 
2881e755c09SAndre Heider 	r->destroy = 1;
289f58a9d17SGeoff Levand 	r->offset = r->base - map.rm.size;
290f58a9d17SGeoff Levand 	return result;
291f58a9d17SGeoff Levand 
292f58a9d17SGeoff Levand zero_region:
293f58a9d17SGeoff Levand 	r->size = r->base = r->offset = 0;
294f58a9d17SGeoff Levand 	return result;
295f58a9d17SGeoff Levand }
296f58a9d17SGeoff Levand 
297f58a9d17SGeoff Levand /**
298f58a9d17SGeoff Levand  * ps3_mm_region_destroy - destroy a memory region
299f58a9d17SGeoff Levand  * @r: pointer to struct mem_region
300f58a9d17SGeoff Levand  */
301f58a9d17SGeoff Levand 
30232f44a12SGeert Uytterhoeven static void ps3_mm_region_destroy(struct mem_region *r)
303f58a9d17SGeoff Levand {
3046bb5cf10SGeoff Levand 	int result;
3056bb5cf10SGeoff Levand 
3061e755c09SAndre Heider 	if (!r->destroy) {
3071e755c09SAndre Heider 		pr_info("%s:%d: Not destroying high region: %llxh %llxh\n",
3081e755c09SAndre Heider 			__func__, __LINE__, r->base, r->size);
3091e755c09SAndre Heider 		return;
3101e755c09SAndre Heider 	}
3111e755c09SAndre Heider 
312b17b3df1SStephen Rothwell 	DBG("%s:%d: r->base = %llxh\n", __func__, __LINE__, r->base);
3131e755c09SAndre Heider 
314f58a9d17SGeoff Levand 	if (r->base) {
3156bb5cf10SGeoff Levand 		result = lv1_release_memory(r->base);
3166bb5cf10SGeoff Levand 		BUG_ON(result);
317f58a9d17SGeoff Levand 		r->size = r->base = r->offset = 0;
318f58a9d17SGeoff Levand 		map.total = map.rm.size;
319f58a9d17SGeoff Levand 	}
3205ae74630SGeoff Levand 	ps3_mm_set_repository_highmem(NULL);
321f58a9d17SGeoff Levand }
322f58a9d17SGeoff Levand 
323f58a9d17SGeoff Levand /*============================================================================*/
324f58a9d17SGeoff Levand /* dma routines                                                               */
325f58a9d17SGeoff Levand /*============================================================================*/
326f58a9d17SGeoff Levand 
327f58a9d17SGeoff Levand /**
3286bb5cf10SGeoff Levand  * dma_sb_lpar_to_bus - Translate an lpar address to ioc mapped bus address.
329f58a9d17SGeoff Levand  * @r: pointer to dma region structure
330f58a9d17SGeoff Levand  * @lpar_addr: HV lpar address
331f58a9d17SGeoff Levand  */
332f58a9d17SGeoff Levand 
3336bb5cf10SGeoff Levand static unsigned long dma_sb_lpar_to_bus(struct ps3_dma_region *r,
334f58a9d17SGeoff Levand 	unsigned long lpar_addr)
335f58a9d17SGeoff Levand {
3366bb5cf10SGeoff Levand 	if (lpar_addr >= map.rm.size)
3376bb5cf10SGeoff Levand 		lpar_addr -= map.r1.offset;
3386bb5cf10SGeoff Levand 	BUG_ON(lpar_addr < r->offset);
3396bb5cf10SGeoff Levand 	BUG_ON(lpar_addr >= r->offset + r->len);
3406bb5cf10SGeoff Levand 	return r->bus_addr + lpar_addr - r->offset;
341f58a9d17SGeoff Levand }
342f58a9d17SGeoff Levand 
343f58a9d17SGeoff Levand #define dma_dump_region(_a) _dma_dump_region(_a, __func__, __LINE__)
3446bb5cf10SGeoff Levand static void  __maybe_unused _dma_dump_region(const struct ps3_dma_region *r,
3456bb5cf10SGeoff Levand 	const char *func, int line)
346f58a9d17SGeoff Levand {
3475c949070SStephen Rothwell 	DBG("%s:%d: dev        %llu:%llu\n", func, line, r->dev->bus_id,
3486bb5cf10SGeoff Levand 		r->dev->dev_id);
349f58a9d17SGeoff Levand 	DBG("%s:%d: page_size  %u\n", func, line, r->page_size);
350f58a9d17SGeoff Levand 	DBG("%s:%d: bus_addr   %lxh\n", func, line, r->bus_addr);
351f58a9d17SGeoff Levand 	DBG("%s:%d: len        %lxh\n", func, line, r->len);
3526bb5cf10SGeoff Levand 	DBG("%s:%d: offset     %lxh\n", func, line, r->offset);
353f58a9d17SGeoff Levand }
354f58a9d17SGeoff Levand 
355f58a9d17SGeoff Levand   /**
356f58a9d17SGeoff Levand  * dma_chunk - A chunk of dma pages mapped by the io controller.
357f58a9d17SGeoff Levand  * @region - The dma region that owns this chunk.
358f58a9d17SGeoff Levand  * @lpar_addr: Starting lpar address of the area to map.
359f58a9d17SGeoff Levand  * @bus_addr: Starting ioc bus address of the area to map.
360f58a9d17SGeoff Levand  * @len: Length in bytes of the area to map.
361f58a9d17SGeoff Levand  * @link: A struct list_head used with struct ps3_dma_region.chunk_list, the
362f58a9d17SGeoff Levand  * list of all chuncks owned by the region.
363f58a9d17SGeoff Levand  *
364f58a9d17SGeoff Levand  * This implementation uses a very simple dma page manager
365f58a9d17SGeoff Levand  * based on the dma_chunk structure.  This scheme assumes
366f58a9d17SGeoff Levand  * that all drivers use very well behaved dma ops.
367f58a9d17SGeoff Levand  */
368f58a9d17SGeoff Levand 
369f58a9d17SGeoff Levand struct dma_chunk {
370f58a9d17SGeoff Levand 	struct ps3_dma_region *region;
371f58a9d17SGeoff Levand 	unsigned long lpar_addr;
372f58a9d17SGeoff Levand 	unsigned long bus_addr;
373f58a9d17SGeoff Levand 	unsigned long len;
374f58a9d17SGeoff Levand 	struct list_head link;
375f58a9d17SGeoff Levand 	unsigned int usage_count;
376f58a9d17SGeoff Levand };
377f58a9d17SGeoff Levand 
378f58a9d17SGeoff Levand #define dma_dump_chunk(_a) _dma_dump_chunk(_a, __func__, __LINE__)
379f58a9d17SGeoff Levand static void _dma_dump_chunk (const struct dma_chunk* c, const char* func,
380f58a9d17SGeoff Levand 	int line)
381f58a9d17SGeoff Levand {
3825c949070SStephen Rothwell 	DBG("%s:%d: r.dev        %llu:%llu\n", func, line,
3836bb5cf10SGeoff Levand 		c->region->dev->bus_id, c->region->dev->dev_id);
384f58a9d17SGeoff Levand 	DBG("%s:%d: r.bus_addr   %lxh\n", func, line, c->region->bus_addr);
385f58a9d17SGeoff Levand 	DBG("%s:%d: r.page_size  %u\n", func, line, c->region->page_size);
386f58a9d17SGeoff Levand 	DBG("%s:%d: r.len        %lxh\n", func, line, c->region->len);
3876bb5cf10SGeoff Levand 	DBG("%s:%d: r.offset     %lxh\n", func, line, c->region->offset);
388f58a9d17SGeoff Levand 	DBG("%s:%d: c.lpar_addr  %lxh\n", func, line, c->lpar_addr);
389f58a9d17SGeoff Levand 	DBG("%s:%d: c.bus_addr   %lxh\n", func, line, c->bus_addr);
390f58a9d17SGeoff Levand 	DBG("%s:%d: c.len        %lxh\n", func, line, c->len);
391f58a9d17SGeoff Levand }
392f58a9d17SGeoff Levand 
393f58a9d17SGeoff Levand static struct dma_chunk * dma_find_chunk(struct ps3_dma_region *r,
394f58a9d17SGeoff Levand 	unsigned long bus_addr, unsigned long len)
395f58a9d17SGeoff Levand {
396f58a9d17SGeoff Levand 	struct dma_chunk *c;
397*e96d904eSChristophe Leroy 	unsigned long aligned_bus = ALIGN_DOWN(bus_addr, 1 << r->page_size);
3986bb5cf10SGeoff Levand 	unsigned long aligned_len = _ALIGN_UP(len+bus_addr-aligned_bus,
3996bb5cf10SGeoff Levand 					      1 << r->page_size);
400f58a9d17SGeoff Levand 
401f58a9d17SGeoff Levand 	list_for_each_entry(c, &r->chunk_list.head, link) {
402f58a9d17SGeoff Levand 		/* intersection */
4036bb5cf10SGeoff Levand 		if (aligned_bus >= c->bus_addr &&
4046bb5cf10SGeoff Levand 		    aligned_bus + aligned_len <= c->bus_addr + c->len)
405f58a9d17SGeoff Levand 			return c;
4066bb5cf10SGeoff Levand 
407f58a9d17SGeoff Levand 		/* below */
4086bb5cf10SGeoff Levand 		if (aligned_bus + aligned_len <= c->bus_addr)
409f58a9d17SGeoff Levand 			continue;
4106bb5cf10SGeoff Levand 
411f58a9d17SGeoff Levand 		/* above */
4126bb5cf10SGeoff Levand 		if (aligned_bus >= c->bus_addr + c->len)
413f58a9d17SGeoff Levand 			continue;
414f58a9d17SGeoff Levand 
415f58a9d17SGeoff Levand 		/* we don't handle the multi-chunk case for now */
416f58a9d17SGeoff Levand 		dma_dump_chunk(c);
417f58a9d17SGeoff Levand 		BUG();
418f58a9d17SGeoff Levand 	}
419f58a9d17SGeoff Levand 	return NULL;
420f58a9d17SGeoff Levand }
421f58a9d17SGeoff Levand 
4226bb5cf10SGeoff Levand static struct dma_chunk *dma_find_chunk_lpar(struct ps3_dma_region *r,
4236bb5cf10SGeoff Levand 	unsigned long lpar_addr, unsigned long len)
4246bb5cf10SGeoff Levand {
4256bb5cf10SGeoff Levand 	struct dma_chunk *c;
426*e96d904eSChristophe Leroy 	unsigned long aligned_lpar = ALIGN_DOWN(lpar_addr, 1 << r->page_size);
4276bb5cf10SGeoff Levand 	unsigned long aligned_len = _ALIGN_UP(len + lpar_addr - aligned_lpar,
4286bb5cf10SGeoff Levand 					      1 << r->page_size);
4296bb5cf10SGeoff Levand 
4306bb5cf10SGeoff Levand 	list_for_each_entry(c, &r->chunk_list.head, link) {
4316bb5cf10SGeoff Levand 		/* intersection */
4326bb5cf10SGeoff Levand 		if (c->lpar_addr <= aligned_lpar &&
4336bb5cf10SGeoff Levand 		    aligned_lpar < c->lpar_addr + c->len) {
4346bb5cf10SGeoff Levand 			if (aligned_lpar + aligned_len <= c->lpar_addr + c->len)
4356bb5cf10SGeoff Levand 				return c;
4366bb5cf10SGeoff Levand 			else {
4376bb5cf10SGeoff Levand 				dma_dump_chunk(c);
4386bb5cf10SGeoff Levand 				BUG();
4396bb5cf10SGeoff Levand 			}
4406bb5cf10SGeoff Levand 		}
4416bb5cf10SGeoff Levand 		/* below */
4426bb5cf10SGeoff Levand 		if (aligned_lpar + aligned_len <= c->lpar_addr) {
4436bb5cf10SGeoff Levand 			continue;
4446bb5cf10SGeoff Levand 		}
4456bb5cf10SGeoff Levand 		/* above */
4466bb5cf10SGeoff Levand 		if (c->lpar_addr + c->len <= aligned_lpar) {
4476bb5cf10SGeoff Levand 			continue;
4486bb5cf10SGeoff Levand 		}
4496bb5cf10SGeoff Levand 	}
4506bb5cf10SGeoff Levand 	return NULL;
4516bb5cf10SGeoff Levand }
4526bb5cf10SGeoff Levand 
4536bb5cf10SGeoff Levand static int dma_sb_free_chunk(struct dma_chunk *c)
454f58a9d17SGeoff Levand {
455f58a9d17SGeoff Levand 	int result = 0;
456f58a9d17SGeoff Levand 
457f58a9d17SGeoff Levand 	if (c->bus_addr) {
4586bb5cf10SGeoff Levand 		result = lv1_unmap_device_dma_region(c->region->dev->bus_id,
4596bb5cf10SGeoff Levand 			c->region->dev->dev_id, c->bus_addr, c->len);
460f58a9d17SGeoff Levand 		BUG_ON(result);
461f58a9d17SGeoff Levand 	}
462f58a9d17SGeoff Levand 
463f58a9d17SGeoff Levand 	kfree(c);
464f58a9d17SGeoff Levand 	return result;
465f58a9d17SGeoff Levand }
466f58a9d17SGeoff Levand 
4676bb5cf10SGeoff Levand static int dma_ioc0_free_chunk(struct dma_chunk *c)
4686bb5cf10SGeoff Levand {
4696bb5cf10SGeoff Levand 	int result = 0;
4706bb5cf10SGeoff Levand 	int iopage;
4716bb5cf10SGeoff Levand 	unsigned long offset;
4726bb5cf10SGeoff Levand 	struct ps3_dma_region *r = c->region;
4736bb5cf10SGeoff Levand 
4746bb5cf10SGeoff Levand 	DBG("%s:start\n", __func__);
4756bb5cf10SGeoff Levand 	for (iopage = 0; iopage < (c->len >> r->page_size); iopage++) {
4766bb5cf10SGeoff Levand 		offset = (1 << r->page_size) * iopage;
4776bb5cf10SGeoff Levand 		/* put INVALID entry */
4786bb5cf10SGeoff Levand 		result = lv1_put_iopte(0,
4796bb5cf10SGeoff Levand 				       c->bus_addr + offset,
4806bb5cf10SGeoff Levand 				       c->lpar_addr + offset,
4816bb5cf10SGeoff Levand 				       r->ioid,
4826bb5cf10SGeoff Levand 				       0);
4836bb5cf10SGeoff Levand 		DBG("%s: bus=%#lx, lpar=%#lx, ioid=%d\n", __func__,
4846bb5cf10SGeoff Levand 		    c->bus_addr + offset,
4856bb5cf10SGeoff Levand 		    c->lpar_addr + offset,
4866bb5cf10SGeoff Levand 		    r->ioid);
4876bb5cf10SGeoff Levand 
4886bb5cf10SGeoff Levand 		if (result) {
4896bb5cf10SGeoff Levand 			DBG("%s:%d: lv1_put_iopte failed: %s\n", __func__,
4906bb5cf10SGeoff Levand 			    __LINE__, ps3_result(result));
4916bb5cf10SGeoff Levand 		}
4926bb5cf10SGeoff Levand 	}
4936bb5cf10SGeoff Levand 	kfree(c);
4946bb5cf10SGeoff Levand 	DBG("%s:end\n", __func__);
4956bb5cf10SGeoff Levand 	return result;
4966bb5cf10SGeoff Levand }
4976bb5cf10SGeoff Levand 
498f58a9d17SGeoff Levand /**
4996bb5cf10SGeoff Levand  * dma_sb_map_pages - Maps dma pages into the io controller bus address space.
500f58a9d17SGeoff Levand  * @r: Pointer to a struct ps3_dma_region.
501f58a9d17SGeoff Levand  * @phys_addr: Starting physical address of the area to map.
502f58a9d17SGeoff Levand  * @len: Length in bytes of the area to map.
503f58a9d17SGeoff Levand  * c_out: A pointer to receive an allocated struct dma_chunk for this area.
504f58a9d17SGeoff Levand  *
505f58a9d17SGeoff Levand  * This is the lowest level dma mapping routine, and is the one that will
506f58a9d17SGeoff Levand  * make the HV call to add the pages into the io controller address space.
507f58a9d17SGeoff Levand  */
508f58a9d17SGeoff Levand 
5096bb5cf10SGeoff Levand static int dma_sb_map_pages(struct ps3_dma_region *r, unsigned long phys_addr,
5106bb5cf10SGeoff Levand 	    unsigned long len, struct dma_chunk **c_out, u64 iopte_flag)
511f58a9d17SGeoff Levand {
512f58a9d17SGeoff Levand 	int result;
513f58a9d17SGeoff Levand 	struct dma_chunk *c;
514f58a9d17SGeoff Levand 
515a0828cf5SMarkus Elfring 	c = kzalloc(sizeof(*c), GFP_ATOMIC);
516f58a9d17SGeoff Levand 	if (!c) {
517f58a9d17SGeoff Levand 		result = -ENOMEM;
518f58a9d17SGeoff Levand 		goto fail_alloc;
519f58a9d17SGeoff Levand 	}
520f58a9d17SGeoff Levand 
521f58a9d17SGeoff Levand 	c->region = r;
522f58a9d17SGeoff Levand 	c->lpar_addr = ps3_mm_phys_to_lpar(phys_addr);
5236bb5cf10SGeoff Levand 	c->bus_addr = dma_sb_lpar_to_bus(r, c->lpar_addr);
524f58a9d17SGeoff Levand 	c->len = len;
525f58a9d17SGeoff Levand 
5266bb5cf10SGeoff Levand 	BUG_ON(iopte_flag != 0xf800000000000000UL);
5276bb5cf10SGeoff Levand 	result = lv1_map_device_dma_region(c->region->dev->bus_id,
5286bb5cf10SGeoff Levand 					   c->region->dev->dev_id, c->lpar_addr,
5296bb5cf10SGeoff Levand 					   c->bus_addr, c->len, iopte_flag);
530f58a9d17SGeoff Levand 	if (result) {
531f58a9d17SGeoff Levand 		DBG("%s:%d: lv1_map_device_dma_region failed: %s\n",
532f58a9d17SGeoff Levand 			__func__, __LINE__, ps3_result(result));
533f58a9d17SGeoff Levand 		goto fail_map;
534f58a9d17SGeoff Levand 	}
535f58a9d17SGeoff Levand 
536f58a9d17SGeoff Levand 	list_add(&c->link, &r->chunk_list.head);
537f58a9d17SGeoff Levand 
538f58a9d17SGeoff Levand 	*c_out = c;
539f58a9d17SGeoff Levand 	return 0;
540f58a9d17SGeoff Levand 
541f58a9d17SGeoff Levand fail_map:
542f58a9d17SGeoff Levand 	kfree(c);
543f58a9d17SGeoff Levand fail_alloc:
544f58a9d17SGeoff Levand 	*c_out = NULL;
545f58a9d17SGeoff Levand 	DBG(" <- %s:%d\n", __func__, __LINE__);
546f58a9d17SGeoff Levand 	return result;
547f58a9d17SGeoff Levand }
548f58a9d17SGeoff Levand 
5496bb5cf10SGeoff Levand static int dma_ioc0_map_pages(struct ps3_dma_region *r, unsigned long phys_addr,
5506bb5cf10SGeoff Levand 			      unsigned long len, struct dma_chunk **c_out,
5516bb5cf10SGeoff Levand 			      u64 iopte_flag)
5526bb5cf10SGeoff Levand {
5536bb5cf10SGeoff Levand 	int result;
5546bb5cf10SGeoff Levand 	struct dma_chunk *c, *last;
5556bb5cf10SGeoff Levand 	int iopage, pages;
5566bb5cf10SGeoff Levand 	unsigned long offset;
5576bb5cf10SGeoff Levand 
5586bb5cf10SGeoff Levand 	DBG(KERN_ERR "%s: phy=%#lx, lpar%#lx, len=%#lx\n", __func__,
5596bb5cf10SGeoff Levand 	    phys_addr, ps3_mm_phys_to_lpar(phys_addr), len);
560a0828cf5SMarkus Elfring 	c = kzalloc(sizeof(*c), GFP_ATOMIC);
5616bb5cf10SGeoff Levand 	if (!c) {
5626bb5cf10SGeoff Levand 		result = -ENOMEM;
5636bb5cf10SGeoff Levand 		goto fail_alloc;
5646bb5cf10SGeoff Levand 	}
5656bb5cf10SGeoff Levand 
5666bb5cf10SGeoff Levand 	c->region = r;
5676bb5cf10SGeoff Levand 	c->len = len;
5686bb5cf10SGeoff Levand 	c->lpar_addr = ps3_mm_phys_to_lpar(phys_addr);
5696bb5cf10SGeoff Levand 	/* allocate IO address */
5706bb5cf10SGeoff Levand 	if (list_empty(&r->chunk_list.head)) {
5716bb5cf10SGeoff Levand 		/* first one */
5726bb5cf10SGeoff Levand 		c->bus_addr = r->bus_addr;
5736bb5cf10SGeoff Levand 	} else {
5746bb5cf10SGeoff Levand 		/* derive from last bus addr*/
5756bb5cf10SGeoff Levand 		last  = list_entry(r->chunk_list.head.next,
5766bb5cf10SGeoff Levand 				   struct dma_chunk, link);
5776bb5cf10SGeoff Levand 		c->bus_addr = last->bus_addr + last->len;
5786bb5cf10SGeoff Levand 		DBG("%s: last bus=%#lx, len=%#lx\n", __func__,
5796bb5cf10SGeoff Levand 		    last->bus_addr, last->len);
5806bb5cf10SGeoff Levand 	}
5816bb5cf10SGeoff Levand 
5826bb5cf10SGeoff Levand 	/* FIXME: check whether length exceeds region size */
5836bb5cf10SGeoff Levand 
5846bb5cf10SGeoff Levand 	/* build ioptes for the area */
5856bb5cf10SGeoff Levand 	pages = len >> r->page_size;
5865c949070SStephen Rothwell 	DBG("%s: pgsize=%#x len=%#lx pages=%#x iopteflag=%#llx\n", __func__,
5876bb5cf10SGeoff Levand 	    r->page_size, r->len, pages, iopte_flag);
5886bb5cf10SGeoff Levand 	for (iopage = 0; iopage < pages; iopage++) {
5896bb5cf10SGeoff Levand 		offset = (1 << r->page_size) * iopage;
5906bb5cf10SGeoff Levand 		result = lv1_put_iopte(0,
5916bb5cf10SGeoff Levand 				       c->bus_addr + offset,
5926bb5cf10SGeoff Levand 				       c->lpar_addr + offset,
5936bb5cf10SGeoff Levand 				       r->ioid,
5946bb5cf10SGeoff Levand 				       iopte_flag);
5956bb5cf10SGeoff Levand 		if (result) {
596f2c2cbccSJoe Perches 			pr_warn("%s:%d: lv1_put_iopte failed: %s\n",
5977e28060aSGeert Uytterhoeven 				__func__, __LINE__, ps3_result(result));
5986bb5cf10SGeoff Levand 			goto fail_map;
5996bb5cf10SGeoff Levand 		}
6006bb5cf10SGeoff Levand 		DBG("%s: pg=%d bus=%#lx, lpar=%#lx, ioid=%#x\n", __func__,
6016bb5cf10SGeoff Levand 		    iopage, c->bus_addr + offset, c->lpar_addr + offset,
6026bb5cf10SGeoff Levand 		    r->ioid);
6036bb5cf10SGeoff Levand 	}
6046bb5cf10SGeoff Levand 
6056bb5cf10SGeoff Levand 	/* be sure that last allocated one is inserted at head */
6066bb5cf10SGeoff Levand 	list_add(&c->link, &r->chunk_list.head);
6076bb5cf10SGeoff Levand 
6086bb5cf10SGeoff Levand 	*c_out = c;
6096bb5cf10SGeoff Levand 	DBG("%s: end\n", __func__);
6106bb5cf10SGeoff Levand 	return 0;
6116bb5cf10SGeoff Levand 
6126bb5cf10SGeoff Levand fail_map:
6136bb5cf10SGeoff Levand 	for (iopage--; 0 <= iopage; iopage--) {
6146bb5cf10SGeoff Levand 		lv1_put_iopte(0,
6156bb5cf10SGeoff Levand 			      c->bus_addr + offset,
6166bb5cf10SGeoff Levand 			      c->lpar_addr + offset,
6176bb5cf10SGeoff Levand 			      r->ioid,
6186bb5cf10SGeoff Levand 			      0);
6196bb5cf10SGeoff Levand 	}
6206bb5cf10SGeoff Levand 	kfree(c);
6216bb5cf10SGeoff Levand fail_alloc:
6226bb5cf10SGeoff Levand 	*c_out = NULL;
6236bb5cf10SGeoff Levand 	return result;
6246bb5cf10SGeoff Levand }
6256bb5cf10SGeoff Levand 
626f58a9d17SGeoff Levand /**
6276bb5cf10SGeoff Levand  * dma_sb_region_create - Create a device dma region.
628f58a9d17SGeoff Levand  * @r: Pointer to a struct ps3_dma_region.
629f58a9d17SGeoff Levand  *
630f58a9d17SGeoff Levand  * This is the lowest level dma region create routine, and is the one that
631f58a9d17SGeoff Levand  * will make the HV call to create the region.
632f58a9d17SGeoff Levand  */
633f58a9d17SGeoff Levand 
6346bb5cf10SGeoff Levand static int dma_sb_region_create(struct ps3_dma_region *r)
635f58a9d17SGeoff Levand {
636f58a9d17SGeoff Levand 	int result;
637b17b3df1SStephen Rothwell 	u64 bus_addr;
638f58a9d17SGeoff Levand 
63962d80749SGeoff Levand 	DBG(" -> %s:%d:\n", __func__, __LINE__);
6406bb5cf10SGeoff Levand 
6416bb5cf10SGeoff Levand 	BUG_ON(!r);
6426bb5cf10SGeoff Levand 
6436bb5cf10SGeoff Levand 	if (!r->dev->bus_id) {
6445c949070SStephen Rothwell 		pr_info("%s:%d: %llu:%llu no dma\n", __func__, __LINE__,
6456bb5cf10SGeoff Levand 			r->dev->bus_id, r->dev->dev_id);
6466bb5cf10SGeoff Levand 		return 0;
6476bb5cf10SGeoff Levand 	}
6486bb5cf10SGeoff Levand 
6496bb5cf10SGeoff Levand 	DBG("%s:%u: len = 0x%lx, page_size = %u, offset = 0x%lx\n", __func__,
6506bb5cf10SGeoff Levand 	    __LINE__, r->len, r->page_size, r->offset);
6516bb5cf10SGeoff Levand 
6526bb5cf10SGeoff Levand 	BUG_ON(!r->len);
6536bb5cf10SGeoff Levand 	BUG_ON(!r->page_size);
6546bb5cf10SGeoff Levand 	BUG_ON(!r->region_ops);
6556bb5cf10SGeoff Levand 
656f58a9d17SGeoff Levand 	INIT_LIST_HEAD(&r->chunk_list.head);
657f58a9d17SGeoff Levand 	spin_lock_init(&r->chunk_list.lock);
658f58a9d17SGeoff Levand 
6596bb5cf10SGeoff Levand 	result = lv1_allocate_device_dma_region(r->dev->bus_id, r->dev->dev_id,
6606bb5cf10SGeoff Levand 		roundup_pow_of_two(r->len), r->page_size, r->region_type,
661b17b3df1SStephen Rothwell 		&bus_addr);
662b17b3df1SStephen Rothwell 	r->bus_addr = bus_addr;
663f58a9d17SGeoff Levand 
664f58a9d17SGeoff Levand 	if (result) {
665f58a9d17SGeoff Levand 		DBG("%s:%d: lv1_allocate_device_dma_region failed: %s\n",
666f58a9d17SGeoff Levand 			__func__, __LINE__, ps3_result(result));
667f58a9d17SGeoff Levand 		r->len = r->bus_addr = 0;
668f58a9d17SGeoff Levand 	}
669f58a9d17SGeoff Levand 
670f58a9d17SGeoff Levand 	return result;
671f58a9d17SGeoff Levand }
672f58a9d17SGeoff Levand 
6736bb5cf10SGeoff Levand static int dma_ioc0_region_create(struct ps3_dma_region *r)
6746bb5cf10SGeoff Levand {
6756bb5cf10SGeoff Levand 	int result;
676b17b3df1SStephen Rothwell 	u64 bus_addr;
6776bb5cf10SGeoff Levand 
6786bb5cf10SGeoff Levand 	INIT_LIST_HEAD(&r->chunk_list.head);
6796bb5cf10SGeoff Levand 	spin_lock_init(&r->chunk_list.lock);
6806bb5cf10SGeoff Levand 
6816bb5cf10SGeoff Levand 	result = lv1_allocate_io_segment(0,
6826bb5cf10SGeoff Levand 					 r->len,
6836bb5cf10SGeoff Levand 					 r->page_size,
684b17b3df1SStephen Rothwell 					 &bus_addr);
685b17b3df1SStephen Rothwell 	r->bus_addr = bus_addr;
6866bb5cf10SGeoff Levand 	if (result) {
6876bb5cf10SGeoff Levand 		DBG("%s:%d: lv1_allocate_io_segment failed: %s\n",
6886bb5cf10SGeoff Levand 			__func__, __LINE__, ps3_result(result));
6896bb5cf10SGeoff Levand 		r->len = r->bus_addr = 0;
6906bb5cf10SGeoff Levand 	}
6916bb5cf10SGeoff Levand 	DBG("%s: len=%#lx, pg=%d, bus=%#lx\n", __func__,
6926bb5cf10SGeoff Levand 	    r->len, r->page_size, r->bus_addr);
6936bb5cf10SGeoff Levand 	return result;
6946bb5cf10SGeoff Levand }
6956bb5cf10SGeoff Levand 
696f58a9d17SGeoff Levand /**
697f58a9d17SGeoff Levand  * dma_region_free - Free a device dma region.
698f58a9d17SGeoff Levand  * @r: Pointer to a struct ps3_dma_region.
699f58a9d17SGeoff Levand  *
700f58a9d17SGeoff Levand  * This is the lowest level dma region free routine, and is the one that
701f58a9d17SGeoff Levand  * will make the HV call to free the region.
702f58a9d17SGeoff Levand  */
703f58a9d17SGeoff Levand 
7046bb5cf10SGeoff Levand static int dma_sb_region_free(struct ps3_dma_region *r)
705f58a9d17SGeoff Levand {
706f58a9d17SGeoff Levand 	int result;
707f58a9d17SGeoff Levand 	struct dma_chunk *c;
708f58a9d17SGeoff Levand 	struct dma_chunk *tmp;
709f58a9d17SGeoff Levand 
7106bb5cf10SGeoff Levand 	BUG_ON(!r);
7116bb5cf10SGeoff Levand 
7126bb5cf10SGeoff Levand 	if (!r->dev->bus_id) {
7135c949070SStephen Rothwell 		pr_info("%s:%d: %llu:%llu no dma\n", __func__, __LINE__,
7146bb5cf10SGeoff Levand 			r->dev->bus_id, r->dev->dev_id);
7156bb5cf10SGeoff Levand 		return 0;
716f58a9d17SGeoff Levand 	}
717f58a9d17SGeoff Levand 
7186bb5cf10SGeoff Levand 	list_for_each_entry_safe(c, tmp, &r->chunk_list.head, link) {
7196bb5cf10SGeoff Levand 		list_del(&c->link);
7206bb5cf10SGeoff Levand 		dma_sb_free_chunk(c);
7216bb5cf10SGeoff Levand 	}
7226bb5cf10SGeoff Levand 
7236bb5cf10SGeoff Levand 	result = lv1_free_device_dma_region(r->dev->bus_id, r->dev->dev_id,
724f58a9d17SGeoff Levand 		r->bus_addr);
725f58a9d17SGeoff Levand 
726f58a9d17SGeoff Levand 	if (result)
727f58a9d17SGeoff Levand 		DBG("%s:%d: lv1_free_device_dma_region failed: %s\n",
728f58a9d17SGeoff Levand 			__func__, __LINE__, ps3_result(result));
729f58a9d17SGeoff Levand 
7306bb5cf10SGeoff Levand 	r->bus_addr = 0;
7316bb5cf10SGeoff Levand 
7326bb5cf10SGeoff Levand 	return result;
7336bb5cf10SGeoff Levand }
7346bb5cf10SGeoff Levand 
7356bb5cf10SGeoff Levand static int dma_ioc0_region_free(struct ps3_dma_region *r)
7366bb5cf10SGeoff Levand {
7376bb5cf10SGeoff Levand 	int result;
7386bb5cf10SGeoff Levand 	struct dma_chunk *c, *n;
7396bb5cf10SGeoff Levand 
7406bb5cf10SGeoff Levand 	DBG("%s: start\n", __func__);
7416bb5cf10SGeoff Levand 	list_for_each_entry_safe(c, n, &r->chunk_list.head, link) {
7426bb5cf10SGeoff Levand 		list_del(&c->link);
7436bb5cf10SGeoff Levand 		dma_ioc0_free_chunk(c);
7446bb5cf10SGeoff Levand 	}
7456bb5cf10SGeoff Levand 
7466bb5cf10SGeoff Levand 	result = lv1_release_io_segment(0, r->bus_addr);
7476bb5cf10SGeoff Levand 
7486bb5cf10SGeoff Levand 	if (result)
7496bb5cf10SGeoff Levand 		DBG("%s:%d: lv1_free_device_dma_region failed: %s\n",
7506bb5cf10SGeoff Levand 			__func__, __LINE__, ps3_result(result));
7516bb5cf10SGeoff Levand 
7526bb5cf10SGeoff Levand 	r->bus_addr = 0;
7536bb5cf10SGeoff Levand 	DBG("%s: end\n", __func__);
754f58a9d17SGeoff Levand 
755f58a9d17SGeoff Levand 	return result;
756f58a9d17SGeoff Levand }
757f58a9d17SGeoff Levand 
758f58a9d17SGeoff Levand /**
7596bb5cf10SGeoff Levand  * dma_sb_map_area - Map an area of memory into a device dma region.
760f58a9d17SGeoff Levand  * @r: Pointer to a struct ps3_dma_region.
761f58a9d17SGeoff Levand  * @virt_addr: Starting virtual address of the area to map.
762f58a9d17SGeoff Levand  * @len: Length in bytes of the area to map.
763f58a9d17SGeoff Levand  * @bus_addr: A pointer to return the starting ioc bus address of the area to
764f58a9d17SGeoff Levand  * map.
765f58a9d17SGeoff Levand  *
766f58a9d17SGeoff Levand  * This is the common dma mapping routine.
767f58a9d17SGeoff Levand  */
768f58a9d17SGeoff Levand 
7696bb5cf10SGeoff Levand static int dma_sb_map_area(struct ps3_dma_region *r, unsigned long virt_addr,
770494fd07aSStephen Rothwell 	   unsigned long len, dma_addr_t *bus_addr,
7716bb5cf10SGeoff Levand 	   u64 iopte_flag)
772f58a9d17SGeoff Levand {
773f58a9d17SGeoff Levand 	int result;
774f58a9d17SGeoff Levand 	unsigned long flags;
775f58a9d17SGeoff Levand 	struct dma_chunk *c;
776f58a9d17SGeoff Levand 	unsigned long phys_addr = is_kernel_addr(virt_addr) ? __pa(virt_addr)
777f58a9d17SGeoff Levand 		: virt_addr;
778*e96d904eSChristophe Leroy 	unsigned long aligned_phys = ALIGN_DOWN(phys_addr, 1 << r->page_size);
7796bb5cf10SGeoff Levand 	unsigned long aligned_len = _ALIGN_UP(len + phys_addr - aligned_phys,
7806bb5cf10SGeoff Levand 					      1 << r->page_size);
7816bb5cf10SGeoff Levand 	*bus_addr = dma_sb_lpar_to_bus(r, ps3_mm_phys_to_lpar(phys_addr));
782f58a9d17SGeoff Levand 
783f58a9d17SGeoff Levand 	if (!USE_DYNAMIC_DMA) {
784f58a9d17SGeoff Levand 		unsigned long lpar_addr = ps3_mm_phys_to_lpar(phys_addr);
785f58a9d17SGeoff Levand 		DBG(" -> %s:%d\n", __func__, __LINE__);
786f58a9d17SGeoff Levand 		DBG("%s:%d virt_addr %lxh\n", __func__, __LINE__,
787f58a9d17SGeoff Levand 			virt_addr);
788f58a9d17SGeoff Levand 		DBG("%s:%d phys_addr %lxh\n", __func__, __LINE__,
789f58a9d17SGeoff Levand 			phys_addr);
790f58a9d17SGeoff Levand 		DBG("%s:%d lpar_addr %lxh\n", __func__, __LINE__,
791f58a9d17SGeoff Levand 			lpar_addr);
792f58a9d17SGeoff Levand 		DBG("%s:%d len       %lxh\n", __func__, __LINE__, len);
793494fd07aSStephen Rothwell 		DBG("%s:%d bus_addr  %llxh (%lxh)\n", __func__, __LINE__,
794f58a9d17SGeoff Levand 		*bus_addr, len);
795f58a9d17SGeoff Levand 	}
796f58a9d17SGeoff Levand 
797f58a9d17SGeoff Levand 	spin_lock_irqsave(&r->chunk_list.lock, flags);
798f58a9d17SGeoff Levand 	c = dma_find_chunk(r, *bus_addr, len);
799f58a9d17SGeoff Levand 
800f58a9d17SGeoff Levand 	if (c) {
8016bb5cf10SGeoff Levand 		DBG("%s:%d: reusing mapped chunk", __func__, __LINE__);
8026bb5cf10SGeoff Levand 		dma_dump_chunk(c);
803f58a9d17SGeoff Levand 		c->usage_count++;
804f58a9d17SGeoff Levand 		spin_unlock_irqrestore(&r->chunk_list.lock, flags);
805f58a9d17SGeoff Levand 		return 0;
806f58a9d17SGeoff Levand 	}
807f58a9d17SGeoff Levand 
8086bb5cf10SGeoff Levand 	result = dma_sb_map_pages(r, aligned_phys, aligned_len, &c, iopte_flag);
809f58a9d17SGeoff Levand 
810f58a9d17SGeoff Levand 	if (result) {
811f58a9d17SGeoff Levand 		*bus_addr = 0;
8126bb5cf10SGeoff Levand 		DBG("%s:%d: dma_sb_map_pages failed (%d)\n",
813f58a9d17SGeoff Levand 			__func__, __LINE__, result);
814f58a9d17SGeoff Levand 		spin_unlock_irqrestore(&r->chunk_list.lock, flags);
815f58a9d17SGeoff Levand 		return result;
816f58a9d17SGeoff Levand 	}
817f58a9d17SGeoff Levand 
818f58a9d17SGeoff Levand 	c->usage_count = 1;
819f58a9d17SGeoff Levand 
820f58a9d17SGeoff Levand 	spin_unlock_irqrestore(&r->chunk_list.lock, flags);
821f58a9d17SGeoff Levand 	return result;
822f58a9d17SGeoff Levand }
823f58a9d17SGeoff Levand 
8246bb5cf10SGeoff Levand static int dma_ioc0_map_area(struct ps3_dma_region *r, unsigned long virt_addr,
825494fd07aSStephen Rothwell 	     unsigned long len, dma_addr_t *bus_addr,
8266bb5cf10SGeoff Levand 	     u64 iopte_flag)
8276bb5cf10SGeoff Levand {
8286bb5cf10SGeoff Levand 	int result;
8296bb5cf10SGeoff Levand 	unsigned long flags;
8306bb5cf10SGeoff Levand 	struct dma_chunk *c;
8316bb5cf10SGeoff Levand 	unsigned long phys_addr = is_kernel_addr(virt_addr) ? __pa(virt_addr)
8326bb5cf10SGeoff Levand 		: virt_addr;
833*e96d904eSChristophe Leroy 	unsigned long aligned_phys = ALIGN_DOWN(phys_addr, 1 << r->page_size);
8346bb5cf10SGeoff Levand 	unsigned long aligned_len = _ALIGN_UP(len + phys_addr - aligned_phys,
8356bb5cf10SGeoff Levand 					      1 << r->page_size);
8366bb5cf10SGeoff Levand 
8376bb5cf10SGeoff Levand 	DBG(KERN_ERR "%s: vaddr=%#lx, len=%#lx\n", __func__,
8386bb5cf10SGeoff Levand 	    virt_addr, len);
8396bb5cf10SGeoff Levand 	DBG(KERN_ERR "%s: ph=%#lx a_ph=%#lx a_l=%#lx\n", __func__,
8406bb5cf10SGeoff Levand 	    phys_addr, aligned_phys, aligned_len);
8416bb5cf10SGeoff Levand 
8426bb5cf10SGeoff Levand 	spin_lock_irqsave(&r->chunk_list.lock, flags);
8436bb5cf10SGeoff Levand 	c = dma_find_chunk_lpar(r, ps3_mm_phys_to_lpar(phys_addr), len);
8446bb5cf10SGeoff Levand 
8456bb5cf10SGeoff Levand 	if (c) {
8466bb5cf10SGeoff Levand 		/* FIXME */
8476bb5cf10SGeoff Levand 		BUG();
8486bb5cf10SGeoff Levand 		*bus_addr = c->bus_addr + phys_addr - aligned_phys;
8496bb5cf10SGeoff Levand 		c->usage_count++;
8506bb5cf10SGeoff Levand 		spin_unlock_irqrestore(&r->chunk_list.lock, flags);
8516bb5cf10SGeoff Levand 		return 0;
8526bb5cf10SGeoff Levand 	}
8536bb5cf10SGeoff Levand 
8546bb5cf10SGeoff Levand 	result = dma_ioc0_map_pages(r, aligned_phys, aligned_len, &c,
8556bb5cf10SGeoff Levand 				    iopte_flag);
8566bb5cf10SGeoff Levand 
8576bb5cf10SGeoff Levand 	if (result) {
8586bb5cf10SGeoff Levand 		*bus_addr = 0;
8596bb5cf10SGeoff Levand 		DBG("%s:%d: dma_ioc0_map_pages failed (%d)\n",
8606bb5cf10SGeoff Levand 			__func__, __LINE__, result);
8616bb5cf10SGeoff Levand 		spin_unlock_irqrestore(&r->chunk_list.lock, flags);
8626bb5cf10SGeoff Levand 		return result;
8636bb5cf10SGeoff Levand 	}
8646bb5cf10SGeoff Levand 	*bus_addr = c->bus_addr + phys_addr - aligned_phys;
865494fd07aSStephen Rothwell 	DBG("%s: va=%#lx pa=%#lx a_pa=%#lx bus=%#llx\n", __func__,
8666bb5cf10SGeoff Levand 	    virt_addr, phys_addr, aligned_phys, *bus_addr);
8676bb5cf10SGeoff Levand 	c->usage_count = 1;
8686bb5cf10SGeoff Levand 
8696bb5cf10SGeoff Levand 	spin_unlock_irqrestore(&r->chunk_list.lock, flags);
8706bb5cf10SGeoff Levand 	return result;
8716bb5cf10SGeoff Levand }
8726bb5cf10SGeoff Levand 
873f58a9d17SGeoff Levand /**
8746bb5cf10SGeoff Levand  * dma_sb_unmap_area - Unmap an area of memory from a device dma region.
875f58a9d17SGeoff Levand  * @r: Pointer to a struct ps3_dma_region.
876f58a9d17SGeoff Levand  * @bus_addr: The starting ioc bus address of the area to unmap.
877f58a9d17SGeoff Levand  * @len: Length in bytes of the area to unmap.
878f58a9d17SGeoff Levand  *
879f58a9d17SGeoff Levand  * This is the common dma unmap routine.
880f58a9d17SGeoff Levand  */
881f58a9d17SGeoff Levand 
882494fd07aSStephen Rothwell static int dma_sb_unmap_area(struct ps3_dma_region *r, dma_addr_t bus_addr,
883f58a9d17SGeoff Levand 	unsigned long len)
884f58a9d17SGeoff Levand {
885f58a9d17SGeoff Levand 	unsigned long flags;
886f58a9d17SGeoff Levand 	struct dma_chunk *c;
887f58a9d17SGeoff Levand 
888f58a9d17SGeoff Levand 	spin_lock_irqsave(&r->chunk_list.lock, flags);
889f58a9d17SGeoff Levand 	c = dma_find_chunk(r, bus_addr, len);
890f58a9d17SGeoff Levand 
891f58a9d17SGeoff Levand 	if (!c) {
892*e96d904eSChristophe Leroy 		unsigned long aligned_bus = ALIGN_DOWN(bus_addr,
893f58a9d17SGeoff Levand 			1 << r->page_size);
8946bb5cf10SGeoff Levand 		unsigned long aligned_len = _ALIGN_UP(len + bus_addr
8956bb5cf10SGeoff Levand 			- aligned_bus, 1 << r->page_size);
896494fd07aSStephen Rothwell 		DBG("%s:%d: not found: bus_addr %llxh\n",
897f58a9d17SGeoff Levand 			__func__, __LINE__, bus_addr);
898f58a9d17SGeoff Levand 		DBG("%s:%d: not found: len %lxh\n",
899f58a9d17SGeoff Levand 			__func__, __LINE__, len);
900f58a9d17SGeoff Levand 		DBG("%s:%d: not found: aligned_bus %lxh\n",
901f58a9d17SGeoff Levand 			__func__, __LINE__, aligned_bus);
902f58a9d17SGeoff Levand 		DBG("%s:%d: not found: aligned_len %lxh\n",
903f58a9d17SGeoff Levand 			__func__, __LINE__, aligned_len);
904f58a9d17SGeoff Levand 		BUG();
905f58a9d17SGeoff Levand 	}
906f58a9d17SGeoff Levand 
907f58a9d17SGeoff Levand 	c->usage_count--;
908f58a9d17SGeoff Levand 
909f58a9d17SGeoff Levand 	if (!c->usage_count) {
910f58a9d17SGeoff Levand 		list_del(&c->link);
9116bb5cf10SGeoff Levand 		dma_sb_free_chunk(c);
912f58a9d17SGeoff Levand 	}
913f58a9d17SGeoff Levand 
914f58a9d17SGeoff Levand 	spin_unlock_irqrestore(&r->chunk_list.lock, flags);
915f58a9d17SGeoff Levand 	return 0;
916f58a9d17SGeoff Levand }
917f58a9d17SGeoff Levand 
91832f44a12SGeert Uytterhoeven static int dma_ioc0_unmap_area(struct ps3_dma_region *r,
919494fd07aSStephen Rothwell 			dma_addr_t bus_addr, unsigned long len)
9206bb5cf10SGeoff Levand {
9216bb5cf10SGeoff Levand 	unsigned long flags;
9226bb5cf10SGeoff Levand 	struct dma_chunk *c;
9236bb5cf10SGeoff Levand 
924494fd07aSStephen Rothwell 	DBG("%s: start a=%#llx l=%#lx\n", __func__, bus_addr, len);
9256bb5cf10SGeoff Levand 	spin_lock_irqsave(&r->chunk_list.lock, flags);
9266bb5cf10SGeoff Levand 	c = dma_find_chunk(r, bus_addr, len);
9276bb5cf10SGeoff Levand 
9286bb5cf10SGeoff Levand 	if (!c) {
929*e96d904eSChristophe Leroy 		unsigned long aligned_bus = ALIGN_DOWN(bus_addr,
9306bb5cf10SGeoff Levand 							1 << r->page_size);
9316bb5cf10SGeoff Levand 		unsigned long aligned_len = _ALIGN_UP(len + bus_addr
9326bb5cf10SGeoff Levand 						      - aligned_bus,
9336bb5cf10SGeoff Levand 						      1 << r->page_size);
934494fd07aSStephen Rothwell 		DBG("%s:%d: not found: bus_addr %llxh\n",
9356bb5cf10SGeoff Levand 		    __func__, __LINE__, bus_addr);
9366bb5cf10SGeoff Levand 		DBG("%s:%d: not found: len %lxh\n",
9376bb5cf10SGeoff Levand 		    __func__, __LINE__, len);
9386bb5cf10SGeoff Levand 		DBG("%s:%d: not found: aligned_bus %lxh\n",
9396bb5cf10SGeoff Levand 		    __func__, __LINE__, aligned_bus);
9406bb5cf10SGeoff Levand 		DBG("%s:%d: not found: aligned_len %lxh\n",
9416bb5cf10SGeoff Levand 		    __func__, __LINE__, aligned_len);
9426bb5cf10SGeoff Levand 		BUG();
9436bb5cf10SGeoff Levand 	}
9446bb5cf10SGeoff Levand 
9456bb5cf10SGeoff Levand 	c->usage_count--;
9466bb5cf10SGeoff Levand 
9476bb5cf10SGeoff Levand 	if (!c->usage_count) {
9486bb5cf10SGeoff Levand 		list_del(&c->link);
9496bb5cf10SGeoff Levand 		dma_ioc0_free_chunk(c);
9506bb5cf10SGeoff Levand 	}
9516bb5cf10SGeoff Levand 
9526bb5cf10SGeoff Levand 	spin_unlock_irqrestore(&r->chunk_list.lock, flags);
9536bb5cf10SGeoff Levand 	DBG("%s: end\n", __func__);
9546bb5cf10SGeoff Levand 	return 0;
9556bb5cf10SGeoff Levand }
9566bb5cf10SGeoff Levand 
957f58a9d17SGeoff Levand /**
9586bb5cf10SGeoff Levand  * dma_sb_region_create_linear - Setup a linear dma mapping for a device.
959f58a9d17SGeoff Levand  * @r: Pointer to a struct ps3_dma_region.
960f58a9d17SGeoff Levand  *
961f58a9d17SGeoff Levand  * This routine creates an HV dma region for the device and maps all available
962f58a9d17SGeoff Levand  * ram into the io controller bus address space.
963f58a9d17SGeoff Levand  */
964f58a9d17SGeoff Levand 
9656bb5cf10SGeoff Levand static int dma_sb_region_create_linear(struct ps3_dma_region *r)
966f58a9d17SGeoff Levand {
967f58a9d17SGeoff Levand 	int result;
968494fd07aSStephen Rothwell 	unsigned long virt_addr, len;
969494fd07aSStephen Rothwell 	dma_addr_t tmp;
970f58a9d17SGeoff Levand 
9716bb5cf10SGeoff Levand 	if (r->len > 16*1024*1024) {	/* FIXME: need proper fix */
972f58a9d17SGeoff Levand 		/* force 16M dma pages for linear mapping */
973f58a9d17SGeoff Levand 		if (r->page_size != PS3_DMA_16M) {
974f58a9d17SGeoff Levand 			pr_info("%s:%d: forcing 16M pages for linear map\n",
975f58a9d17SGeoff Levand 				__func__, __LINE__);
976f58a9d17SGeoff Levand 			r->page_size = PS3_DMA_16M;
9776bb5cf10SGeoff Levand 			r->len = _ALIGN_UP(r->len, 1 << r->page_size);
9786bb5cf10SGeoff Levand 		}
979f58a9d17SGeoff Levand 	}
980f58a9d17SGeoff Levand 
9816bb5cf10SGeoff Levand 	result = dma_sb_region_create(r);
982f58a9d17SGeoff Levand 	BUG_ON(result);
983f58a9d17SGeoff Levand 
9846bb5cf10SGeoff Levand 	if (r->offset < map.rm.size) {
9856bb5cf10SGeoff Levand 		/* Map (part of) 1st RAM chunk */
9866bb5cf10SGeoff Levand 		virt_addr = map.rm.base + r->offset;
9876bb5cf10SGeoff Levand 		len = map.rm.size - r->offset;
9886bb5cf10SGeoff Levand 		if (len > r->len)
9896bb5cf10SGeoff Levand 			len = r->len;
9906bb5cf10SGeoff Levand 		result = dma_sb_map_area(r, virt_addr, len, &tmp,
9915c6fc8dbSGeert Uytterhoeven 			CBE_IOPTE_PP_W | CBE_IOPTE_PP_R | CBE_IOPTE_SO_RW |
9925c6fc8dbSGeert Uytterhoeven 			CBE_IOPTE_M);
993f58a9d17SGeoff Levand 		BUG_ON(result);
9946bb5cf10SGeoff Levand 	}
995f58a9d17SGeoff Levand 
9966bb5cf10SGeoff Levand 	if (r->offset + r->len > map.rm.size) {
9976bb5cf10SGeoff Levand 		/* Map (part of) 2nd RAM chunk */
998a628df1eSGeoff Levand 		virt_addr = map.rm.size;
9996bb5cf10SGeoff Levand 		len = r->len;
10006bb5cf10SGeoff Levand 		if (r->offset >= map.rm.size)
10016bb5cf10SGeoff Levand 			virt_addr += r->offset - map.rm.size;
1002f58a9d17SGeoff Levand 		else
10036bb5cf10SGeoff Levand 			len -= map.rm.size - r->offset;
10046bb5cf10SGeoff Levand 		result = dma_sb_map_area(r, virt_addr, len, &tmp,
10055c6fc8dbSGeert Uytterhoeven 			CBE_IOPTE_PP_W | CBE_IOPTE_PP_R | CBE_IOPTE_SO_RW |
10065c6fc8dbSGeert Uytterhoeven 			CBE_IOPTE_M);
1007f58a9d17SGeoff Levand 		BUG_ON(result);
10086bb5cf10SGeoff Levand 	}
1009f58a9d17SGeoff Levand 
1010f58a9d17SGeoff Levand 	return result;
1011f58a9d17SGeoff Levand }
1012f58a9d17SGeoff Levand 
1013f58a9d17SGeoff Levand /**
10146bb5cf10SGeoff Levand  * dma_sb_region_free_linear - Free a linear dma mapping for a device.
1015f58a9d17SGeoff Levand  * @r: Pointer to a struct ps3_dma_region.
1016f58a9d17SGeoff Levand  *
1017f58a9d17SGeoff Levand  * This routine will unmap all mapped areas and free the HV dma region.
1018f58a9d17SGeoff Levand  */
1019f58a9d17SGeoff Levand 
10206bb5cf10SGeoff Levand static int dma_sb_region_free_linear(struct ps3_dma_region *r)
1021f58a9d17SGeoff Levand {
1022f58a9d17SGeoff Levand 	int result;
1023494fd07aSStephen Rothwell 	dma_addr_t bus_addr;
1024494fd07aSStephen Rothwell 	unsigned long len, lpar_addr;
1025f58a9d17SGeoff Levand 
10266bb5cf10SGeoff Levand 	if (r->offset < map.rm.size) {
10276bb5cf10SGeoff Levand 		/* Unmap (part of) 1st RAM chunk */
10286bb5cf10SGeoff Levand 		lpar_addr = map.rm.base + r->offset;
10296bb5cf10SGeoff Levand 		len = map.rm.size - r->offset;
10306bb5cf10SGeoff Levand 		if (len > r->len)
10316bb5cf10SGeoff Levand 			len = r->len;
10326bb5cf10SGeoff Levand 		bus_addr = dma_sb_lpar_to_bus(r, lpar_addr);
10336bb5cf10SGeoff Levand 		result = dma_sb_unmap_area(r, bus_addr, len);
1034f58a9d17SGeoff Levand 		BUG_ON(result);
10356bb5cf10SGeoff Levand 	}
1036f58a9d17SGeoff Levand 
10376bb5cf10SGeoff Levand 	if (r->offset + r->len > map.rm.size) {
10386bb5cf10SGeoff Levand 		/* Unmap (part of) 2nd RAM chunk */
10396bb5cf10SGeoff Levand 		lpar_addr = map.r1.base;
10406bb5cf10SGeoff Levand 		len = r->len;
10416bb5cf10SGeoff Levand 		if (r->offset >= map.rm.size)
10426bb5cf10SGeoff Levand 			lpar_addr += r->offset - map.rm.size;
10436bb5cf10SGeoff Levand 		else
10446bb5cf10SGeoff Levand 			len -= map.rm.size - r->offset;
10456bb5cf10SGeoff Levand 		bus_addr = dma_sb_lpar_to_bus(r, lpar_addr);
10466bb5cf10SGeoff Levand 		result = dma_sb_unmap_area(r, bus_addr, len);
1047f58a9d17SGeoff Levand 		BUG_ON(result);
10486bb5cf10SGeoff Levand 	}
1049f58a9d17SGeoff Levand 
10506bb5cf10SGeoff Levand 	result = dma_sb_region_free(r);
1051f58a9d17SGeoff Levand 	BUG_ON(result);
1052f58a9d17SGeoff Levand 
1053f58a9d17SGeoff Levand 	return result;
1054f58a9d17SGeoff Levand }
1055f58a9d17SGeoff Levand 
1056f58a9d17SGeoff Levand /**
10576bb5cf10SGeoff Levand  * dma_sb_map_area_linear - Map an area of memory into a device dma region.
1058f58a9d17SGeoff Levand  * @r: Pointer to a struct ps3_dma_region.
1059f58a9d17SGeoff Levand  * @virt_addr: Starting virtual address of the area to map.
1060f58a9d17SGeoff Levand  * @len: Length in bytes of the area to map.
1061f58a9d17SGeoff Levand  * @bus_addr: A pointer to return the starting ioc bus address of the area to
1062f58a9d17SGeoff Levand  * map.
1063f58a9d17SGeoff Levand  *
10646bb5cf10SGeoff Levand  * This routine just returns the corresponding bus address.  Actual mapping
1065f58a9d17SGeoff Levand  * occurs in dma_region_create_linear().
1066f58a9d17SGeoff Levand  */
1067f58a9d17SGeoff Levand 
10686bb5cf10SGeoff Levand static int dma_sb_map_area_linear(struct ps3_dma_region *r,
1069494fd07aSStephen Rothwell 	unsigned long virt_addr, unsigned long len, dma_addr_t *bus_addr,
10706bb5cf10SGeoff Levand 	u64 iopte_flag)
1071f58a9d17SGeoff Levand {
1072f58a9d17SGeoff Levand 	unsigned long phys_addr = is_kernel_addr(virt_addr) ? __pa(virt_addr)
1073f58a9d17SGeoff Levand 		: virt_addr;
10746bb5cf10SGeoff Levand 	*bus_addr = dma_sb_lpar_to_bus(r, ps3_mm_phys_to_lpar(phys_addr));
1075f58a9d17SGeoff Levand 	return 0;
1076f58a9d17SGeoff Levand }
1077f58a9d17SGeoff Levand 
1078f58a9d17SGeoff Levand /**
1079f58a9d17SGeoff Levand  * dma_unmap_area_linear - Unmap an area of memory from a device dma region.
1080f58a9d17SGeoff Levand  * @r: Pointer to a struct ps3_dma_region.
1081f58a9d17SGeoff Levand  * @bus_addr: The starting ioc bus address of the area to unmap.
1082f58a9d17SGeoff Levand  * @len: Length in bytes of the area to unmap.
1083f58a9d17SGeoff Levand  *
10846bb5cf10SGeoff Levand  * This routine does nothing.  Unmapping occurs in dma_sb_region_free_linear().
1085f58a9d17SGeoff Levand  */
1086f58a9d17SGeoff Levand 
10876bb5cf10SGeoff Levand static int dma_sb_unmap_area_linear(struct ps3_dma_region *r,
1088494fd07aSStephen Rothwell 	dma_addr_t bus_addr, unsigned long len)
1089f58a9d17SGeoff Levand {
1090f58a9d17SGeoff Levand 	return 0;
10916bb5cf10SGeoff Levand };
10926bb5cf10SGeoff Levand 
10936bb5cf10SGeoff Levand static const struct ps3_dma_region_ops ps3_dma_sb_region_ops =  {
10946bb5cf10SGeoff Levand 	.create = dma_sb_region_create,
10956bb5cf10SGeoff Levand 	.free = dma_sb_region_free,
10966bb5cf10SGeoff Levand 	.map = dma_sb_map_area,
10976bb5cf10SGeoff Levand 	.unmap = dma_sb_unmap_area
10986bb5cf10SGeoff Levand };
10996bb5cf10SGeoff Levand 
11006bb5cf10SGeoff Levand static const struct ps3_dma_region_ops ps3_dma_sb_region_linear_ops = {
11016bb5cf10SGeoff Levand 	.create = dma_sb_region_create_linear,
11026bb5cf10SGeoff Levand 	.free = dma_sb_region_free_linear,
11036bb5cf10SGeoff Levand 	.map = dma_sb_map_area_linear,
11046bb5cf10SGeoff Levand 	.unmap = dma_sb_unmap_area_linear
11056bb5cf10SGeoff Levand };
11066bb5cf10SGeoff Levand 
11076bb5cf10SGeoff Levand static const struct ps3_dma_region_ops ps3_dma_ioc0_region_ops = {
11086bb5cf10SGeoff Levand 	.create = dma_ioc0_region_create,
11096bb5cf10SGeoff Levand 	.free = dma_ioc0_region_free,
11106bb5cf10SGeoff Levand 	.map = dma_ioc0_map_area,
11116bb5cf10SGeoff Levand 	.unmap = dma_ioc0_unmap_area
11126bb5cf10SGeoff Levand };
11136bb5cf10SGeoff Levand 
11146bb5cf10SGeoff Levand int ps3_dma_region_init(struct ps3_system_bus_device *dev,
11156bb5cf10SGeoff Levand 	struct ps3_dma_region *r, enum ps3_dma_page_size page_size,
11166bb5cf10SGeoff Levand 	enum ps3_dma_region_type region_type, void *addr, unsigned long len)
11176bb5cf10SGeoff Levand {
11186bb5cf10SGeoff Levand 	unsigned long lpar_addr;
11196bb5cf10SGeoff Levand 
11206bb5cf10SGeoff Levand 	lpar_addr = addr ? ps3_mm_phys_to_lpar(__pa(addr)) : 0;
11216bb5cf10SGeoff Levand 
11226bb5cf10SGeoff Levand 	r->dev = dev;
11236bb5cf10SGeoff Levand 	r->page_size = page_size;
11246bb5cf10SGeoff Levand 	r->region_type = region_type;
11256bb5cf10SGeoff Levand 	r->offset = lpar_addr;
11266bb5cf10SGeoff Levand 	if (r->offset >= map.rm.size)
11276bb5cf10SGeoff Levand 		r->offset -= map.r1.offset;
11286bb5cf10SGeoff Levand 	r->len = len ? len : _ALIGN_UP(map.total, 1 << r->page_size);
11296bb5cf10SGeoff Levand 
11306bb5cf10SGeoff Levand 	switch (dev->dev_type) {
11316bb5cf10SGeoff Levand 	case PS3_DEVICE_TYPE_SB:
11326bb5cf10SGeoff Levand 		r->region_ops =  (USE_DYNAMIC_DMA)
11336bb5cf10SGeoff Levand 			? &ps3_dma_sb_region_ops
11346bb5cf10SGeoff Levand 			: &ps3_dma_sb_region_linear_ops;
11356bb5cf10SGeoff Levand 		break;
11366bb5cf10SGeoff Levand 	case PS3_DEVICE_TYPE_IOC0:
11376bb5cf10SGeoff Levand 		r->region_ops = &ps3_dma_ioc0_region_ops;
11386bb5cf10SGeoff Levand 		break;
11396bb5cf10SGeoff Levand 	default:
11406bb5cf10SGeoff Levand 		BUG();
11416bb5cf10SGeoff Levand 		return -EINVAL;
1142f58a9d17SGeoff Levand 	}
11436bb5cf10SGeoff Levand 	return 0;
11446bb5cf10SGeoff Levand }
11456bb5cf10SGeoff Levand EXPORT_SYMBOL(ps3_dma_region_init);
1146f58a9d17SGeoff Levand 
1147f58a9d17SGeoff Levand int ps3_dma_region_create(struct ps3_dma_region *r)
1148f58a9d17SGeoff Levand {
11496bb5cf10SGeoff Levand 	BUG_ON(!r);
11506bb5cf10SGeoff Levand 	BUG_ON(!r->region_ops);
11516bb5cf10SGeoff Levand 	BUG_ON(!r->region_ops->create);
11526bb5cf10SGeoff Levand 	return r->region_ops->create(r);
1153f58a9d17SGeoff Levand }
11546bb5cf10SGeoff Levand EXPORT_SYMBOL(ps3_dma_region_create);
1155f58a9d17SGeoff Levand 
1156f58a9d17SGeoff Levand int ps3_dma_region_free(struct ps3_dma_region *r)
1157f58a9d17SGeoff Levand {
11586bb5cf10SGeoff Levand 	BUG_ON(!r);
11596bb5cf10SGeoff Levand 	BUG_ON(!r->region_ops);
11606bb5cf10SGeoff Levand 	BUG_ON(!r->region_ops->free);
11616bb5cf10SGeoff Levand 	return r->region_ops->free(r);
1162f58a9d17SGeoff Levand }
11636bb5cf10SGeoff Levand EXPORT_SYMBOL(ps3_dma_region_free);
1164f58a9d17SGeoff Levand 
1165f58a9d17SGeoff Levand int ps3_dma_map(struct ps3_dma_region *r, unsigned long virt_addr,
1166494fd07aSStephen Rothwell 	unsigned long len, dma_addr_t *bus_addr,
11676bb5cf10SGeoff Levand 	u64 iopte_flag)
1168f58a9d17SGeoff Levand {
11696bb5cf10SGeoff Levand 	return r->region_ops->map(r, virt_addr, len, bus_addr, iopte_flag);
1170f58a9d17SGeoff Levand }
1171f58a9d17SGeoff Levand 
1172494fd07aSStephen Rothwell int ps3_dma_unmap(struct ps3_dma_region *r, dma_addr_t bus_addr,
1173f58a9d17SGeoff Levand 	unsigned long len)
1174f58a9d17SGeoff Levand {
11756bb5cf10SGeoff Levand 	return r->region_ops->unmap(r, bus_addr, len);
1176f58a9d17SGeoff Levand }
1177f58a9d17SGeoff Levand 
1178f58a9d17SGeoff Levand /*============================================================================*/
1179f58a9d17SGeoff Levand /* system startup routines                                                    */
1180f58a9d17SGeoff Levand /*============================================================================*/
1181f58a9d17SGeoff Levand 
1182f58a9d17SGeoff Levand /**
1183f58a9d17SGeoff Levand  * ps3_mm_init - initialize the address space state variables
1184f58a9d17SGeoff Levand  */
1185f58a9d17SGeoff Levand 
1186f58a9d17SGeoff Levand void __init ps3_mm_init(void)
1187f58a9d17SGeoff Levand {
1188f58a9d17SGeoff Levand 	int result;
1189f58a9d17SGeoff Levand 
1190f58a9d17SGeoff Levand 	DBG(" -> %s:%d\n", __func__, __LINE__);
1191f58a9d17SGeoff Levand 
1192f58a9d17SGeoff Levand 	result = ps3_repository_read_mm_info(&map.rm.base, &map.rm.size,
1193f58a9d17SGeoff Levand 		&map.total);
1194f58a9d17SGeoff Levand 
1195f58a9d17SGeoff Levand 	if (result)
1196f58a9d17SGeoff Levand 		panic("ps3_repository_read_mm_info() failed");
1197f58a9d17SGeoff Levand 
1198f58a9d17SGeoff Levand 	map.rm.offset = map.rm.base;
1199f58a9d17SGeoff Levand 	map.vas_id = map.htab_size = 0;
1200f58a9d17SGeoff Levand 
1201f58a9d17SGeoff Levand 	/* this implementation assumes map.rm.base is zero */
1202f58a9d17SGeoff Levand 
1203f58a9d17SGeoff Levand 	BUG_ON(map.rm.base);
1204f58a9d17SGeoff Levand 	BUG_ON(!map.rm.size);
1205f58a9d17SGeoff Levand 
12061e755c09SAndre Heider 	/* Check if we got the highmem region from an earlier boot step */
1207f58a9d17SGeoff Levand 
12085ae74630SGeoff Levand 	if (ps3_mm_get_repository_highmem(&map.r1)) {
12095ae74630SGeoff Levand 		result = ps3_mm_region_create(&map.r1, map.total - map.rm.size);
12105ae74630SGeoff Levand 
12115ae74630SGeoff Levand 		if (!result)
12125ae74630SGeoff Levand 			ps3_mm_set_repository_highmem(&map.r1);
12135ae74630SGeoff Levand 	}
1214f58a9d17SGeoff Levand 
12156bb5cf10SGeoff Levand 	/* correct map.total for the real total amount of memory we use */
12166bb5cf10SGeoff Levand 	map.total = map.rm.size + map.r1.size;
12176bb5cf10SGeoff Levand 
12188ac5fd11SHector Martin 	if (!map.r1.size) {
12198ac5fd11SHector Martin 		DBG("%s:%d: No highmem region found\n", __func__, __LINE__);
12208ac5fd11SHector Martin 	} else {
12218ac5fd11SHector Martin 		DBG("%s:%d: Adding highmem region: %llxh %llxh\n",
12228ac5fd11SHector Martin 			__func__, __LINE__, map.rm.size,
12238ac5fd11SHector Martin 			map.total - map.rm.size);
12248ac5fd11SHector Martin 		memblock_add(map.rm.size, map.total - map.rm.size);
12258ac5fd11SHector Martin 	}
12268ac5fd11SHector Martin 
1227f58a9d17SGeoff Levand 	DBG(" <- %s:%d\n", __func__, __LINE__);
1228f58a9d17SGeoff Levand }
1229f58a9d17SGeoff Levand 
1230f58a9d17SGeoff Levand /**
1231f58a9d17SGeoff Levand  * ps3_mm_shutdown - final cleanup of address space
1232f58a9d17SGeoff Levand  */
1233f58a9d17SGeoff Levand 
1234f58a9d17SGeoff Levand void ps3_mm_shutdown(void)
1235f58a9d17SGeoff Levand {
1236f58a9d17SGeoff Levand 	ps3_mm_region_destroy(&map.r1);
1237f58a9d17SGeoff Levand }
1238