xref: /linux/arch/powerpc/platforms/ps3/mm.c (revision 5c6fc8db768fb9990ee67ab052896fd46fbe2651)
1f58a9d17SGeoff Levand /*
2f58a9d17SGeoff Levand  *  PS3 address space management.
3f58a9d17SGeoff Levand  *
4f58a9d17SGeoff Levand  *  Copyright (C) 2006 Sony Computer Entertainment Inc.
5f58a9d17SGeoff Levand  *  Copyright 2006 Sony Corp.
6f58a9d17SGeoff Levand  *
7f58a9d17SGeoff Levand  *  This program is free software; you can redistribute it and/or modify
8f58a9d17SGeoff Levand  *  it under the terms of the GNU General Public License as published by
9f58a9d17SGeoff Levand  *  the Free Software Foundation; version 2 of the License.
10f58a9d17SGeoff Levand  *
11f58a9d17SGeoff Levand  *  This program is distributed in the hope that it will be useful,
12f58a9d17SGeoff Levand  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13f58a9d17SGeoff Levand  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14f58a9d17SGeoff Levand  *  GNU General Public License for more details.
15f58a9d17SGeoff Levand  *
16f58a9d17SGeoff Levand  *  You should have received a copy of the GNU General Public License
17f58a9d17SGeoff Levand  *  along with this program; if not, write to the Free Software
18f58a9d17SGeoff Levand  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19f58a9d17SGeoff Levand  */
20f58a9d17SGeoff Levand 
21f58a9d17SGeoff Levand #include <linux/kernel.h>
22f58a9d17SGeoff Levand #include <linux/module.h>
23f58a9d17SGeoff Levand #include <linux/memory_hotplug.h>
24d9b2b2a2SDavid S. Miller #include <linux/lmb.h>
25f58a9d17SGeoff Levand 
26e22ba7e3SArnd Bergmann #include <asm/firmware.h>
27*5c6fc8dbSGeert Uytterhoeven #include <asm/iommu.h>
28d9b2b2a2SDavid S. Miller #include <asm/prom.h>
29f58a9d17SGeoff Levand #include <asm/udbg.h>
30f58a9d17SGeoff Levand #include <asm/lv1call.h>
31f58a9d17SGeoff Levand 
32f58a9d17SGeoff Levand #include "platform.h"
33f58a9d17SGeoff Levand 
34f58a9d17SGeoff Levand #if defined(DEBUG)
3583bb643dSGeert Uytterhoeven #define DBG udbg_printf
36f58a9d17SGeoff Levand #else
3783bb643dSGeert Uytterhoeven #define DBG pr_debug
38f58a9d17SGeoff Levand #endif
39f58a9d17SGeoff Levand 
40f58a9d17SGeoff Levand enum {
41f58a9d17SGeoff Levand #if defined(CONFIG_PS3_DYNAMIC_DMA)
42f58a9d17SGeoff Levand 	USE_DYNAMIC_DMA = 1,
43f58a9d17SGeoff Levand #else
44f58a9d17SGeoff Levand 	USE_DYNAMIC_DMA = 0,
45f58a9d17SGeoff Levand #endif
46f58a9d17SGeoff Levand };
47f58a9d17SGeoff Levand 
48f58a9d17SGeoff Levand enum {
49f58a9d17SGeoff Levand 	PAGE_SHIFT_4K = 12U,
50f58a9d17SGeoff Levand 	PAGE_SHIFT_64K = 16U,
51f58a9d17SGeoff Levand 	PAGE_SHIFT_16M = 24U,
52f58a9d17SGeoff Levand };
53f58a9d17SGeoff Levand 
54f58a9d17SGeoff Levand static unsigned long make_page_sizes(unsigned long a, unsigned long b)
55f58a9d17SGeoff Levand {
56f58a9d17SGeoff Levand 	return (a << 56) | (b << 48);
57f58a9d17SGeoff Levand }
58f58a9d17SGeoff Levand 
59f58a9d17SGeoff Levand enum {
60f58a9d17SGeoff Levand 	ALLOCATE_MEMORY_TRY_ALT_UNIT = 0X04,
61f58a9d17SGeoff Levand 	ALLOCATE_MEMORY_ADDR_ZERO = 0X08,
62f58a9d17SGeoff Levand };
63f58a9d17SGeoff Levand 
64f58a9d17SGeoff Levand /* valid htab sizes are {18,19,20} = 256K, 512K, 1M */
65f58a9d17SGeoff Levand 
66f58a9d17SGeoff Levand enum {
67f58a9d17SGeoff Levand 	HTAB_SIZE_MAX = 20U, /* HV limit of 1MB */
68f58a9d17SGeoff Levand 	HTAB_SIZE_MIN = 18U, /* CPU limit of 256KB */
69f58a9d17SGeoff Levand };
70f58a9d17SGeoff Levand 
71f58a9d17SGeoff Levand /*============================================================================*/
72f58a9d17SGeoff Levand /* virtual address space routines                                             */
73f58a9d17SGeoff Levand /*============================================================================*/
74f58a9d17SGeoff Levand 
75f58a9d17SGeoff Levand /**
76f58a9d17SGeoff Levand  * struct mem_region - memory region structure
77f58a9d17SGeoff Levand  * @base: base address
78f58a9d17SGeoff Levand  * @size: size in bytes
79f58a9d17SGeoff Levand  * @offset: difference between base and rm.size
80f58a9d17SGeoff Levand  */
81f58a9d17SGeoff Levand 
82f58a9d17SGeoff Levand struct mem_region {
83b17b3df1SStephen Rothwell 	u64 base;
845418b9c6SStephen Rothwell 	u64 size;
85f58a9d17SGeoff Levand 	unsigned long offset;
86f58a9d17SGeoff Levand };
87f58a9d17SGeoff Levand 
88f58a9d17SGeoff Levand /**
89f58a9d17SGeoff Levand  * struct map - address space state variables holder
90f58a9d17SGeoff Levand  * @total: total memory available as reported by HV
91f58a9d17SGeoff Levand  * @vas_id - HV virtual address space id
92f58a9d17SGeoff Levand  * @htab_size: htab size in bytes
93f58a9d17SGeoff Levand  *
94f58a9d17SGeoff Levand  * The HV virtual address space (vas) allows for hotplug memory regions.
95f58a9d17SGeoff Levand  * Memory regions can be created and destroyed in the vas at runtime.
96f58a9d17SGeoff Levand  * @rm: real mode (bootmem) region
97f58a9d17SGeoff Levand  * @r1: hotplug memory region(s)
98f58a9d17SGeoff Levand  *
99f58a9d17SGeoff Levand  * ps3 addresses
100f58a9d17SGeoff Levand  * virt_addr: a cpu 'translated' effective address
101f58a9d17SGeoff Levand  * phys_addr: an address in what Linux thinks is the physical address space
102f58a9d17SGeoff Levand  * lpar_addr: an address in the HV virtual address space
103f58a9d17SGeoff Levand  * bus_addr: an io controller 'translated' address on a device bus
104f58a9d17SGeoff Levand  */
105f58a9d17SGeoff Levand 
106f58a9d17SGeoff Levand struct map {
1075418b9c6SStephen Rothwell 	u64 total;
108b17b3df1SStephen Rothwell 	u64 vas_id;
109b17b3df1SStephen Rothwell 	u64 htab_size;
110f58a9d17SGeoff Levand 	struct mem_region rm;
111f58a9d17SGeoff Levand 	struct mem_region r1;
112f58a9d17SGeoff Levand };
113f58a9d17SGeoff Levand 
114f58a9d17SGeoff Levand #define debug_dump_map(x) _debug_dump_map(x, __func__, __LINE__)
1156bb5cf10SGeoff Levand static void __maybe_unused _debug_dump_map(const struct map *m,
1166bb5cf10SGeoff Levand 	const char *func, int line)
117f58a9d17SGeoff Levand {
1185418b9c6SStephen Rothwell 	DBG("%s:%d: map.total     = %llxh\n", func, line, m->total);
1195418b9c6SStephen Rothwell 	DBG("%s:%d: map.rm.size   = %llxh\n", func, line, m->rm.size);
120b17b3df1SStephen Rothwell 	DBG("%s:%d: map.vas_id    = %llu\n", func, line, m->vas_id);
121b17b3df1SStephen Rothwell 	DBG("%s:%d: map.htab_size = %llxh\n", func, line, m->htab_size);
122b17b3df1SStephen Rothwell 	DBG("%s:%d: map.r1.base   = %llxh\n", func, line, m->r1.base);
123f58a9d17SGeoff Levand 	DBG("%s:%d: map.r1.offset = %lxh\n", func, line, m->r1.offset);
1245418b9c6SStephen Rothwell 	DBG("%s:%d: map.r1.size   = %llxh\n", func, line, m->r1.size);
125f58a9d17SGeoff Levand }
126f58a9d17SGeoff Levand 
127f58a9d17SGeoff Levand static struct map map;
128f58a9d17SGeoff Levand 
129f58a9d17SGeoff Levand /**
130f58a9d17SGeoff Levand  * ps3_mm_phys_to_lpar - translate a linux physical address to lpar address
131f58a9d17SGeoff Levand  * @phys_addr: linux physical address
132f58a9d17SGeoff Levand  */
133f58a9d17SGeoff Levand 
134f58a9d17SGeoff Levand unsigned long ps3_mm_phys_to_lpar(unsigned long phys_addr)
135f58a9d17SGeoff Levand {
136f58a9d17SGeoff Levand 	BUG_ON(is_kernel_addr(phys_addr));
137f58a9d17SGeoff Levand 	return (phys_addr < map.rm.size || phys_addr >= map.total)
138f58a9d17SGeoff Levand 		? phys_addr : phys_addr + map.r1.offset;
139f58a9d17SGeoff Levand }
140f58a9d17SGeoff Levand 
141f58a9d17SGeoff Levand EXPORT_SYMBOL(ps3_mm_phys_to_lpar);
142f58a9d17SGeoff Levand 
143f58a9d17SGeoff Levand /**
144f58a9d17SGeoff Levand  * ps3_mm_vas_create - create the virtual address space
145f58a9d17SGeoff Levand  */
146f58a9d17SGeoff Levand 
147f58a9d17SGeoff Levand void __init ps3_mm_vas_create(unsigned long* htab_size)
148f58a9d17SGeoff Levand {
149f58a9d17SGeoff Levand 	int result;
150b17b3df1SStephen Rothwell 	u64 start_address;
151b17b3df1SStephen Rothwell 	u64 size;
152b17b3df1SStephen Rothwell 	u64 access_right;
153b17b3df1SStephen Rothwell 	u64 max_page_size;
154b17b3df1SStephen Rothwell 	u64 flags;
155f58a9d17SGeoff Levand 
156f58a9d17SGeoff Levand 	result = lv1_query_logical_partition_address_region_info(0,
157f58a9d17SGeoff Levand 		&start_address, &size, &access_right, &max_page_size,
158f58a9d17SGeoff Levand 		&flags);
159f58a9d17SGeoff Levand 
160f58a9d17SGeoff Levand 	if (result) {
161f58a9d17SGeoff Levand 		DBG("%s:%d: lv1_query_logical_partition_address_region_info "
162f58a9d17SGeoff Levand 			"failed: %s\n", __func__, __LINE__,
163f58a9d17SGeoff Levand 			ps3_result(result));
164f58a9d17SGeoff Levand 		goto fail;
165f58a9d17SGeoff Levand 	}
166f58a9d17SGeoff Levand 
167f58a9d17SGeoff Levand 	if (max_page_size < PAGE_SHIFT_16M) {
168b17b3df1SStephen Rothwell 		DBG("%s:%d: bad max_page_size %llxh\n", __func__, __LINE__,
169f58a9d17SGeoff Levand 			max_page_size);
170f58a9d17SGeoff Levand 		goto fail;
171f58a9d17SGeoff Levand 	}
172f58a9d17SGeoff Levand 
173f58a9d17SGeoff Levand 	BUILD_BUG_ON(CONFIG_PS3_HTAB_SIZE > HTAB_SIZE_MAX);
174f58a9d17SGeoff Levand 	BUILD_BUG_ON(CONFIG_PS3_HTAB_SIZE < HTAB_SIZE_MIN);
175f58a9d17SGeoff Levand 
176f58a9d17SGeoff Levand 	result = lv1_construct_virtual_address_space(CONFIG_PS3_HTAB_SIZE,
177f58a9d17SGeoff Levand 			2, make_page_sizes(PAGE_SHIFT_16M, PAGE_SHIFT_64K),
178f58a9d17SGeoff Levand 			&map.vas_id, &map.htab_size);
179f58a9d17SGeoff Levand 
180f58a9d17SGeoff Levand 	if (result) {
181f58a9d17SGeoff Levand 		DBG("%s:%d: lv1_construct_virtual_address_space failed: %s\n",
182f58a9d17SGeoff Levand 			__func__, __LINE__, ps3_result(result));
183f58a9d17SGeoff Levand 		goto fail;
184f58a9d17SGeoff Levand 	}
185f58a9d17SGeoff Levand 
186f58a9d17SGeoff Levand 	result = lv1_select_virtual_address_space(map.vas_id);
187f58a9d17SGeoff Levand 
188f58a9d17SGeoff Levand 	if (result) {
189f58a9d17SGeoff Levand 		DBG("%s:%d: lv1_select_virtual_address_space failed: %s\n",
190f58a9d17SGeoff Levand 			__func__, __LINE__, ps3_result(result));
191f58a9d17SGeoff Levand 		goto fail;
192f58a9d17SGeoff Levand 	}
193f58a9d17SGeoff Levand 
194f58a9d17SGeoff Levand 	*htab_size = map.htab_size;
195f58a9d17SGeoff Levand 
196f58a9d17SGeoff Levand 	debug_dump_map(&map);
197f58a9d17SGeoff Levand 
198f58a9d17SGeoff Levand 	return;
199f58a9d17SGeoff Levand 
200f58a9d17SGeoff Levand fail:
201f58a9d17SGeoff Levand 	panic("ps3_mm_vas_create failed");
202f58a9d17SGeoff Levand }
203f58a9d17SGeoff Levand 
204f58a9d17SGeoff Levand /**
205f58a9d17SGeoff Levand  * ps3_mm_vas_destroy -
206f58a9d17SGeoff Levand  */
207f58a9d17SGeoff Levand 
208f58a9d17SGeoff Levand void ps3_mm_vas_destroy(void)
209f58a9d17SGeoff Levand {
2106bb5cf10SGeoff Levand 	int result;
2116bb5cf10SGeoff Levand 
212b17b3df1SStephen Rothwell 	DBG("%s:%d: map.vas_id    = %llu\n", __func__, __LINE__, map.vas_id);
2136bb5cf10SGeoff Levand 
214f58a9d17SGeoff Levand 	if (map.vas_id) {
2156bb5cf10SGeoff Levand 		result = lv1_select_virtual_address_space(0);
2166bb5cf10SGeoff Levand 		BUG_ON(result);
2176bb5cf10SGeoff Levand 		result = lv1_destruct_virtual_address_space(map.vas_id);
2186bb5cf10SGeoff Levand 		BUG_ON(result);
219f58a9d17SGeoff Levand 		map.vas_id = 0;
220f58a9d17SGeoff Levand 	}
221f58a9d17SGeoff Levand }
222f58a9d17SGeoff Levand 
223f58a9d17SGeoff Levand /*============================================================================*/
224f58a9d17SGeoff Levand /* memory hotplug routines                                                    */
225f58a9d17SGeoff Levand /*============================================================================*/
226f58a9d17SGeoff Levand 
227f58a9d17SGeoff Levand /**
228f58a9d17SGeoff Levand  * ps3_mm_region_create - create a memory region in the vas
229f58a9d17SGeoff Levand  * @r: pointer to a struct mem_region to accept initialized values
230f58a9d17SGeoff Levand  * @size: requested region size
231f58a9d17SGeoff Levand  *
232f58a9d17SGeoff Levand  * This implementation creates the region with the vas large page size.
233f58a9d17SGeoff Levand  * @size is rounded down to a multiple of the vas large page size.
234f58a9d17SGeoff Levand  */
235f58a9d17SGeoff Levand 
23632f44a12SGeert Uytterhoeven static int ps3_mm_region_create(struct mem_region *r, unsigned long size)
237f58a9d17SGeoff Levand {
238f58a9d17SGeoff Levand 	int result;
239b17b3df1SStephen Rothwell 	u64 muid;
240f58a9d17SGeoff Levand 
241f58a9d17SGeoff Levand 	r->size = _ALIGN_DOWN(size, 1 << PAGE_SHIFT_16M);
242f58a9d17SGeoff Levand 
243f58a9d17SGeoff Levand 	DBG("%s:%d requested  %lxh\n", __func__, __LINE__, size);
2445418b9c6SStephen Rothwell 	DBG("%s:%d actual     %llxh\n", __func__, __LINE__, r->size);
2455418b9c6SStephen Rothwell 	DBG("%s:%d difference %llxh (%lluMB)\n", __func__, __LINE__,
2465418b9c6SStephen Rothwell 		size - r->size, (size - r->size) / 1024 / 1024);
247f58a9d17SGeoff Levand 
248f58a9d17SGeoff Levand 	if (r->size == 0) {
249f58a9d17SGeoff Levand 		DBG("%s:%d: size == 0\n", __func__, __LINE__);
250f58a9d17SGeoff Levand 		result = -1;
251f58a9d17SGeoff Levand 		goto zero_region;
252f58a9d17SGeoff Levand 	}
253f58a9d17SGeoff Levand 
254f58a9d17SGeoff Levand 	result = lv1_allocate_memory(r->size, PAGE_SHIFT_16M, 0,
255f58a9d17SGeoff Levand 		ALLOCATE_MEMORY_TRY_ALT_UNIT, &r->base, &muid);
256f58a9d17SGeoff Levand 
257f58a9d17SGeoff Levand 	if (result || r->base < map.rm.size) {
258f58a9d17SGeoff Levand 		DBG("%s:%d: lv1_allocate_memory failed: %s\n",
259f58a9d17SGeoff Levand 			__func__, __LINE__, ps3_result(result));
260f58a9d17SGeoff Levand 		goto zero_region;
261f58a9d17SGeoff Levand 	}
262f58a9d17SGeoff Levand 
263f58a9d17SGeoff Levand 	r->offset = r->base - map.rm.size;
264f58a9d17SGeoff Levand 	return result;
265f58a9d17SGeoff Levand 
266f58a9d17SGeoff Levand zero_region:
267f58a9d17SGeoff Levand 	r->size = r->base = r->offset = 0;
268f58a9d17SGeoff Levand 	return result;
269f58a9d17SGeoff Levand }
270f58a9d17SGeoff Levand 
271f58a9d17SGeoff Levand /**
272f58a9d17SGeoff Levand  * ps3_mm_region_destroy - destroy a memory region
273f58a9d17SGeoff Levand  * @r: pointer to struct mem_region
274f58a9d17SGeoff Levand  */
275f58a9d17SGeoff Levand 
27632f44a12SGeert Uytterhoeven static void ps3_mm_region_destroy(struct mem_region *r)
277f58a9d17SGeoff Levand {
2786bb5cf10SGeoff Levand 	int result;
2796bb5cf10SGeoff Levand 
280b17b3df1SStephen Rothwell 	DBG("%s:%d: r->base = %llxh\n", __func__, __LINE__, r->base);
281f58a9d17SGeoff Levand 	if (r->base) {
2826bb5cf10SGeoff Levand 		result = lv1_release_memory(r->base);
2836bb5cf10SGeoff Levand 		BUG_ON(result);
284f58a9d17SGeoff Levand 		r->size = r->base = r->offset = 0;
285f58a9d17SGeoff Levand 		map.total = map.rm.size;
286f58a9d17SGeoff Levand 	}
287f58a9d17SGeoff Levand }
288f58a9d17SGeoff Levand 
289f58a9d17SGeoff Levand /**
290f58a9d17SGeoff Levand  * ps3_mm_add_memory - hot add memory
291f58a9d17SGeoff Levand  */
292f58a9d17SGeoff Levand 
293f58a9d17SGeoff Levand static int __init ps3_mm_add_memory(void)
294f58a9d17SGeoff Levand {
295f58a9d17SGeoff Levand 	int result;
296f58a9d17SGeoff Levand 	unsigned long start_addr;
297f58a9d17SGeoff Levand 	unsigned long start_pfn;
298f58a9d17SGeoff Levand 	unsigned long nr_pages;
299f58a9d17SGeoff Levand 
300e22ba7e3SArnd Bergmann 	if (!firmware_has_feature(FW_FEATURE_PS3_LV1))
301ef596c69SGeert Uytterhoeven 		return -ENODEV;
302e22ba7e3SArnd Bergmann 
303f58a9d17SGeoff Levand 	BUG_ON(!mem_init_done);
304f58a9d17SGeoff Levand 
305a628df1eSGeoff Levand 	start_addr = map.rm.size;
306f58a9d17SGeoff Levand 	start_pfn = start_addr >> PAGE_SHIFT;
307f58a9d17SGeoff Levand 	nr_pages = (map.r1.size + PAGE_SIZE - 1) >> PAGE_SHIFT;
308f58a9d17SGeoff Levand 
309f58a9d17SGeoff Levand 	DBG("%s:%d: start_addr %lxh, start_pfn %lxh, nr_pages %lxh\n",
310f58a9d17SGeoff Levand 		__func__, __LINE__, start_addr, start_pfn, nr_pages);
311f58a9d17SGeoff Levand 
312f58a9d17SGeoff Levand 	result = add_memory(0, start_addr, map.r1.size);
313f58a9d17SGeoff Levand 
314f58a9d17SGeoff Levand 	if (result) {
3159146cfc8SGeoff Levand 		pr_err("%s:%d: add_memory failed: (%d)\n",
316f58a9d17SGeoff Levand 			__func__, __LINE__, result);
317f58a9d17SGeoff Levand 		return result;
318f58a9d17SGeoff Levand 	}
319f58a9d17SGeoff Levand 
320ecc240f9SGeoff Levand 	lmb_add(start_addr, map.r1.size);
321ecc240f9SGeoff Levand 	lmb_analyze();
322ecc240f9SGeoff Levand 
323f58a9d17SGeoff Levand 	result = online_pages(start_pfn, nr_pages);
324f58a9d17SGeoff Levand 
325f58a9d17SGeoff Levand 	if (result)
3269146cfc8SGeoff Levand 		pr_err("%s:%d: online_pages failed: (%d)\n",
327f58a9d17SGeoff Levand 			__func__, __LINE__, result);
328f58a9d17SGeoff Levand 
329f58a9d17SGeoff Levand 	return result;
330f58a9d17SGeoff Levand }
331f58a9d17SGeoff Levand 
3320047656eSGeoff Levand device_initcall(ps3_mm_add_memory);
333f58a9d17SGeoff Levand 
334f58a9d17SGeoff Levand /*============================================================================*/
335f58a9d17SGeoff Levand /* dma routines                                                               */
336f58a9d17SGeoff Levand /*============================================================================*/
337f58a9d17SGeoff Levand 
338f58a9d17SGeoff Levand /**
3396bb5cf10SGeoff Levand  * dma_sb_lpar_to_bus - Translate an lpar address to ioc mapped bus address.
340f58a9d17SGeoff Levand  * @r: pointer to dma region structure
341f58a9d17SGeoff Levand  * @lpar_addr: HV lpar address
342f58a9d17SGeoff Levand  */
343f58a9d17SGeoff Levand 
3446bb5cf10SGeoff Levand static unsigned long dma_sb_lpar_to_bus(struct ps3_dma_region *r,
345f58a9d17SGeoff Levand 	unsigned long lpar_addr)
346f58a9d17SGeoff Levand {
3476bb5cf10SGeoff Levand 	if (lpar_addr >= map.rm.size)
3486bb5cf10SGeoff Levand 		lpar_addr -= map.r1.offset;
3496bb5cf10SGeoff Levand 	BUG_ON(lpar_addr < r->offset);
3506bb5cf10SGeoff Levand 	BUG_ON(lpar_addr >= r->offset + r->len);
3516bb5cf10SGeoff Levand 	return r->bus_addr + lpar_addr - r->offset;
352f58a9d17SGeoff Levand }
353f58a9d17SGeoff Levand 
354f58a9d17SGeoff Levand #define dma_dump_region(_a) _dma_dump_region(_a, __func__, __LINE__)
3556bb5cf10SGeoff Levand static void  __maybe_unused _dma_dump_region(const struct ps3_dma_region *r,
3566bb5cf10SGeoff Levand 	const char *func, int line)
357f58a9d17SGeoff Levand {
3585c949070SStephen Rothwell 	DBG("%s:%d: dev        %llu:%llu\n", func, line, r->dev->bus_id,
3596bb5cf10SGeoff Levand 		r->dev->dev_id);
360f58a9d17SGeoff Levand 	DBG("%s:%d: page_size  %u\n", func, line, r->page_size);
361f58a9d17SGeoff Levand 	DBG("%s:%d: bus_addr   %lxh\n", func, line, r->bus_addr);
362f58a9d17SGeoff Levand 	DBG("%s:%d: len        %lxh\n", func, line, r->len);
3636bb5cf10SGeoff Levand 	DBG("%s:%d: offset     %lxh\n", func, line, r->offset);
364f58a9d17SGeoff Levand }
365f58a9d17SGeoff Levand 
366f58a9d17SGeoff Levand   /**
367f58a9d17SGeoff Levand  * dma_chunk - A chunk of dma pages mapped by the io controller.
368f58a9d17SGeoff Levand  * @region - The dma region that owns this chunk.
369f58a9d17SGeoff Levand  * @lpar_addr: Starting lpar address of the area to map.
370f58a9d17SGeoff Levand  * @bus_addr: Starting ioc bus address of the area to map.
371f58a9d17SGeoff Levand  * @len: Length in bytes of the area to map.
372f58a9d17SGeoff Levand  * @link: A struct list_head used with struct ps3_dma_region.chunk_list, the
373f58a9d17SGeoff Levand  * list of all chuncks owned by the region.
374f58a9d17SGeoff Levand  *
375f58a9d17SGeoff Levand  * This implementation uses a very simple dma page manager
376f58a9d17SGeoff Levand  * based on the dma_chunk structure.  This scheme assumes
377f58a9d17SGeoff Levand  * that all drivers use very well behaved dma ops.
378f58a9d17SGeoff Levand  */
379f58a9d17SGeoff Levand 
380f58a9d17SGeoff Levand struct dma_chunk {
381f58a9d17SGeoff Levand 	struct ps3_dma_region *region;
382f58a9d17SGeoff Levand 	unsigned long lpar_addr;
383f58a9d17SGeoff Levand 	unsigned long bus_addr;
384f58a9d17SGeoff Levand 	unsigned long len;
385f58a9d17SGeoff Levand 	struct list_head link;
386f58a9d17SGeoff Levand 	unsigned int usage_count;
387f58a9d17SGeoff Levand };
388f58a9d17SGeoff Levand 
389f58a9d17SGeoff Levand #define dma_dump_chunk(_a) _dma_dump_chunk(_a, __func__, __LINE__)
390f58a9d17SGeoff Levand static void _dma_dump_chunk (const struct dma_chunk* c, const char* func,
391f58a9d17SGeoff Levand 	int line)
392f58a9d17SGeoff Levand {
3935c949070SStephen Rothwell 	DBG("%s:%d: r.dev        %llu:%llu\n", func, line,
3946bb5cf10SGeoff Levand 		c->region->dev->bus_id, c->region->dev->dev_id);
395f58a9d17SGeoff Levand 	DBG("%s:%d: r.bus_addr   %lxh\n", func, line, c->region->bus_addr);
396f58a9d17SGeoff Levand 	DBG("%s:%d: r.page_size  %u\n", func, line, c->region->page_size);
397f58a9d17SGeoff Levand 	DBG("%s:%d: r.len        %lxh\n", func, line, c->region->len);
3986bb5cf10SGeoff Levand 	DBG("%s:%d: r.offset     %lxh\n", func, line, c->region->offset);
399f58a9d17SGeoff Levand 	DBG("%s:%d: c.lpar_addr  %lxh\n", func, line, c->lpar_addr);
400f58a9d17SGeoff Levand 	DBG("%s:%d: c.bus_addr   %lxh\n", func, line, c->bus_addr);
401f58a9d17SGeoff Levand 	DBG("%s:%d: c.len        %lxh\n", func, line, c->len);
402f58a9d17SGeoff Levand }
403f58a9d17SGeoff Levand 
404f58a9d17SGeoff Levand static struct dma_chunk * dma_find_chunk(struct ps3_dma_region *r,
405f58a9d17SGeoff Levand 	unsigned long bus_addr, unsigned long len)
406f58a9d17SGeoff Levand {
407f58a9d17SGeoff Levand 	struct dma_chunk *c;
408f58a9d17SGeoff Levand 	unsigned long aligned_bus = _ALIGN_DOWN(bus_addr, 1 << r->page_size);
4096bb5cf10SGeoff Levand 	unsigned long aligned_len = _ALIGN_UP(len+bus_addr-aligned_bus,
4106bb5cf10SGeoff Levand 					      1 << r->page_size);
411f58a9d17SGeoff Levand 
412f58a9d17SGeoff Levand 	list_for_each_entry(c, &r->chunk_list.head, link) {
413f58a9d17SGeoff Levand 		/* intersection */
4146bb5cf10SGeoff Levand 		if (aligned_bus >= c->bus_addr &&
4156bb5cf10SGeoff Levand 		    aligned_bus + aligned_len <= c->bus_addr + c->len)
416f58a9d17SGeoff Levand 			return c;
4176bb5cf10SGeoff Levand 
418f58a9d17SGeoff Levand 		/* below */
4196bb5cf10SGeoff Levand 		if (aligned_bus + aligned_len <= c->bus_addr)
420f58a9d17SGeoff Levand 			continue;
4216bb5cf10SGeoff Levand 
422f58a9d17SGeoff Levand 		/* above */
4236bb5cf10SGeoff Levand 		if (aligned_bus >= c->bus_addr + c->len)
424f58a9d17SGeoff Levand 			continue;
425f58a9d17SGeoff Levand 
426f58a9d17SGeoff Levand 		/* we don't handle the multi-chunk case for now */
427f58a9d17SGeoff Levand 		dma_dump_chunk(c);
428f58a9d17SGeoff Levand 		BUG();
429f58a9d17SGeoff Levand 	}
430f58a9d17SGeoff Levand 	return NULL;
431f58a9d17SGeoff Levand }
432f58a9d17SGeoff Levand 
4336bb5cf10SGeoff Levand static struct dma_chunk *dma_find_chunk_lpar(struct ps3_dma_region *r,
4346bb5cf10SGeoff Levand 	unsigned long lpar_addr, unsigned long len)
4356bb5cf10SGeoff Levand {
4366bb5cf10SGeoff Levand 	struct dma_chunk *c;
4376bb5cf10SGeoff Levand 	unsigned long aligned_lpar = _ALIGN_DOWN(lpar_addr, 1 << r->page_size);
4386bb5cf10SGeoff Levand 	unsigned long aligned_len = _ALIGN_UP(len + lpar_addr - aligned_lpar,
4396bb5cf10SGeoff Levand 					      1 << r->page_size);
4406bb5cf10SGeoff Levand 
4416bb5cf10SGeoff Levand 	list_for_each_entry(c, &r->chunk_list.head, link) {
4426bb5cf10SGeoff Levand 		/* intersection */
4436bb5cf10SGeoff Levand 		if (c->lpar_addr <= aligned_lpar &&
4446bb5cf10SGeoff Levand 		    aligned_lpar < c->lpar_addr + c->len) {
4456bb5cf10SGeoff Levand 			if (aligned_lpar + aligned_len <= c->lpar_addr + c->len)
4466bb5cf10SGeoff Levand 				return c;
4476bb5cf10SGeoff Levand 			else {
4486bb5cf10SGeoff Levand 				dma_dump_chunk(c);
4496bb5cf10SGeoff Levand 				BUG();
4506bb5cf10SGeoff Levand 			}
4516bb5cf10SGeoff Levand 		}
4526bb5cf10SGeoff Levand 		/* below */
4536bb5cf10SGeoff Levand 		if (aligned_lpar + aligned_len <= c->lpar_addr) {
4546bb5cf10SGeoff Levand 			continue;
4556bb5cf10SGeoff Levand 		}
4566bb5cf10SGeoff Levand 		/* above */
4576bb5cf10SGeoff Levand 		if (c->lpar_addr + c->len <= aligned_lpar) {
4586bb5cf10SGeoff Levand 			continue;
4596bb5cf10SGeoff Levand 		}
4606bb5cf10SGeoff Levand 	}
4616bb5cf10SGeoff Levand 	return NULL;
4626bb5cf10SGeoff Levand }
4636bb5cf10SGeoff Levand 
4646bb5cf10SGeoff Levand static int dma_sb_free_chunk(struct dma_chunk *c)
465f58a9d17SGeoff Levand {
466f58a9d17SGeoff Levand 	int result = 0;
467f58a9d17SGeoff Levand 
468f58a9d17SGeoff Levand 	if (c->bus_addr) {
4696bb5cf10SGeoff Levand 		result = lv1_unmap_device_dma_region(c->region->dev->bus_id,
4706bb5cf10SGeoff Levand 			c->region->dev->dev_id, c->bus_addr, c->len);
471f58a9d17SGeoff Levand 		BUG_ON(result);
472f58a9d17SGeoff Levand 	}
473f58a9d17SGeoff Levand 
474f58a9d17SGeoff Levand 	kfree(c);
475f58a9d17SGeoff Levand 	return result;
476f58a9d17SGeoff Levand }
477f58a9d17SGeoff Levand 
4786bb5cf10SGeoff Levand static int dma_ioc0_free_chunk(struct dma_chunk *c)
4796bb5cf10SGeoff Levand {
4806bb5cf10SGeoff Levand 	int result = 0;
4816bb5cf10SGeoff Levand 	int iopage;
4826bb5cf10SGeoff Levand 	unsigned long offset;
4836bb5cf10SGeoff Levand 	struct ps3_dma_region *r = c->region;
4846bb5cf10SGeoff Levand 
4856bb5cf10SGeoff Levand 	DBG("%s:start\n", __func__);
4866bb5cf10SGeoff Levand 	for (iopage = 0; iopage < (c->len >> r->page_size); iopage++) {
4876bb5cf10SGeoff Levand 		offset = (1 << r->page_size) * iopage;
4886bb5cf10SGeoff Levand 		/* put INVALID entry */
4896bb5cf10SGeoff Levand 		result = lv1_put_iopte(0,
4906bb5cf10SGeoff Levand 				       c->bus_addr + offset,
4916bb5cf10SGeoff Levand 				       c->lpar_addr + offset,
4926bb5cf10SGeoff Levand 				       r->ioid,
4936bb5cf10SGeoff Levand 				       0);
4946bb5cf10SGeoff Levand 		DBG("%s: bus=%#lx, lpar=%#lx, ioid=%d\n", __func__,
4956bb5cf10SGeoff Levand 		    c->bus_addr + offset,
4966bb5cf10SGeoff Levand 		    c->lpar_addr + offset,
4976bb5cf10SGeoff Levand 		    r->ioid);
4986bb5cf10SGeoff Levand 
4996bb5cf10SGeoff Levand 		if (result) {
5006bb5cf10SGeoff Levand 			DBG("%s:%d: lv1_put_iopte failed: %s\n", __func__,
5016bb5cf10SGeoff Levand 			    __LINE__, ps3_result(result));
5026bb5cf10SGeoff Levand 		}
5036bb5cf10SGeoff Levand 	}
5046bb5cf10SGeoff Levand 	kfree(c);
5056bb5cf10SGeoff Levand 	DBG("%s:end\n", __func__);
5066bb5cf10SGeoff Levand 	return result;
5076bb5cf10SGeoff Levand }
5086bb5cf10SGeoff Levand 
509f58a9d17SGeoff Levand /**
5106bb5cf10SGeoff Levand  * dma_sb_map_pages - Maps dma pages into the io controller bus address space.
511f58a9d17SGeoff Levand  * @r: Pointer to a struct ps3_dma_region.
512f58a9d17SGeoff Levand  * @phys_addr: Starting physical address of the area to map.
513f58a9d17SGeoff Levand  * @len: Length in bytes of the area to map.
514f58a9d17SGeoff Levand  * c_out: A pointer to receive an allocated struct dma_chunk for this area.
515f58a9d17SGeoff Levand  *
516f58a9d17SGeoff Levand  * This is the lowest level dma mapping routine, and is the one that will
517f58a9d17SGeoff Levand  * make the HV call to add the pages into the io controller address space.
518f58a9d17SGeoff Levand  */
519f58a9d17SGeoff Levand 
5206bb5cf10SGeoff Levand static int dma_sb_map_pages(struct ps3_dma_region *r, unsigned long phys_addr,
5216bb5cf10SGeoff Levand 	    unsigned long len, struct dma_chunk **c_out, u64 iopte_flag)
522f58a9d17SGeoff Levand {
523f58a9d17SGeoff Levand 	int result;
524f58a9d17SGeoff Levand 	struct dma_chunk *c;
525f58a9d17SGeoff Levand 
526f58a9d17SGeoff Levand 	c = kzalloc(sizeof(struct dma_chunk), GFP_ATOMIC);
527f58a9d17SGeoff Levand 
528f58a9d17SGeoff Levand 	if (!c) {
529f58a9d17SGeoff Levand 		result = -ENOMEM;
530f58a9d17SGeoff Levand 		goto fail_alloc;
531f58a9d17SGeoff Levand 	}
532f58a9d17SGeoff Levand 
533f58a9d17SGeoff Levand 	c->region = r;
534f58a9d17SGeoff Levand 	c->lpar_addr = ps3_mm_phys_to_lpar(phys_addr);
5356bb5cf10SGeoff Levand 	c->bus_addr = dma_sb_lpar_to_bus(r, c->lpar_addr);
536f58a9d17SGeoff Levand 	c->len = len;
537f58a9d17SGeoff Levand 
5386bb5cf10SGeoff Levand 	BUG_ON(iopte_flag != 0xf800000000000000UL);
5396bb5cf10SGeoff Levand 	result = lv1_map_device_dma_region(c->region->dev->bus_id,
5406bb5cf10SGeoff Levand 					   c->region->dev->dev_id, c->lpar_addr,
5416bb5cf10SGeoff Levand 					   c->bus_addr, c->len, iopte_flag);
542f58a9d17SGeoff Levand 	if (result) {
543f58a9d17SGeoff Levand 		DBG("%s:%d: lv1_map_device_dma_region failed: %s\n",
544f58a9d17SGeoff Levand 			__func__, __LINE__, ps3_result(result));
545f58a9d17SGeoff Levand 		goto fail_map;
546f58a9d17SGeoff Levand 	}
547f58a9d17SGeoff Levand 
548f58a9d17SGeoff Levand 	list_add(&c->link, &r->chunk_list.head);
549f58a9d17SGeoff Levand 
550f58a9d17SGeoff Levand 	*c_out = c;
551f58a9d17SGeoff Levand 	return 0;
552f58a9d17SGeoff Levand 
553f58a9d17SGeoff Levand fail_map:
554f58a9d17SGeoff Levand 	kfree(c);
555f58a9d17SGeoff Levand fail_alloc:
556f58a9d17SGeoff Levand 	*c_out = NULL;
557f58a9d17SGeoff Levand 	DBG(" <- %s:%d\n", __func__, __LINE__);
558f58a9d17SGeoff Levand 	return result;
559f58a9d17SGeoff Levand }
560f58a9d17SGeoff Levand 
5616bb5cf10SGeoff Levand static int dma_ioc0_map_pages(struct ps3_dma_region *r, unsigned long phys_addr,
5626bb5cf10SGeoff Levand 			      unsigned long len, struct dma_chunk **c_out,
5636bb5cf10SGeoff Levand 			      u64 iopte_flag)
5646bb5cf10SGeoff Levand {
5656bb5cf10SGeoff Levand 	int result;
5666bb5cf10SGeoff Levand 	struct dma_chunk *c, *last;
5676bb5cf10SGeoff Levand 	int iopage, pages;
5686bb5cf10SGeoff Levand 	unsigned long offset;
5696bb5cf10SGeoff Levand 
5706bb5cf10SGeoff Levand 	DBG(KERN_ERR "%s: phy=%#lx, lpar%#lx, len=%#lx\n", __func__,
5716bb5cf10SGeoff Levand 	    phys_addr, ps3_mm_phys_to_lpar(phys_addr), len);
5726bb5cf10SGeoff Levand 	c = kzalloc(sizeof(struct dma_chunk), GFP_ATOMIC);
5736bb5cf10SGeoff Levand 
5746bb5cf10SGeoff Levand 	if (!c) {
5756bb5cf10SGeoff Levand 		result = -ENOMEM;
5766bb5cf10SGeoff Levand 		goto fail_alloc;
5776bb5cf10SGeoff Levand 	}
5786bb5cf10SGeoff Levand 
5796bb5cf10SGeoff Levand 	c->region = r;
5806bb5cf10SGeoff Levand 	c->len = len;
5816bb5cf10SGeoff Levand 	c->lpar_addr = ps3_mm_phys_to_lpar(phys_addr);
5826bb5cf10SGeoff Levand 	/* allocate IO address */
5836bb5cf10SGeoff Levand 	if (list_empty(&r->chunk_list.head)) {
5846bb5cf10SGeoff Levand 		/* first one */
5856bb5cf10SGeoff Levand 		c->bus_addr = r->bus_addr;
5866bb5cf10SGeoff Levand 	} else {
5876bb5cf10SGeoff Levand 		/* derive from last bus addr*/
5886bb5cf10SGeoff Levand 		last  = list_entry(r->chunk_list.head.next,
5896bb5cf10SGeoff Levand 				   struct dma_chunk, link);
5906bb5cf10SGeoff Levand 		c->bus_addr = last->bus_addr + last->len;
5916bb5cf10SGeoff Levand 		DBG("%s: last bus=%#lx, len=%#lx\n", __func__,
5926bb5cf10SGeoff Levand 		    last->bus_addr, last->len);
5936bb5cf10SGeoff Levand 	}
5946bb5cf10SGeoff Levand 
5956bb5cf10SGeoff Levand 	/* FIXME: check whether length exceeds region size */
5966bb5cf10SGeoff Levand 
5976bb5cf10SGeoff Levand 	/* build ioptes for the area */
5986bb5cf10SGeoff Levand 	pages = len >> r->page_size;
5995c949070SStephen Rothwell 	DBG("%s: pgsize=%#x len=%#lx pages=%#x iopteflag=%#llx\n", __func__,
6006bb5cf10SGeoff Levand 	    r->page_size, r->len, pages, iopte_flag);
6016bb5cf10SGeoff Levand 	for (iopage = 0; iopage < pages; iopage++) {
6026bb5cf10SGeoff Levand 		offset = (1 << r->page_size) * iopage;
6036bb5cf10SGeoff Levand 		result = lv1_put_iopte(0,
6046bb5cf10SGeoff Levand 				       c->bus_addr + offset,
6056bb5cf10SGeoff Levand 				       c->lpar_addr + offset,
6066bb5cf10SGeoff Levand 				       r->ioid,
6076bb5cf10SGeoff Levand 				       iopte_flag);
6086bb5cf10SGeoff Levand 		if (result) {
6096bb5cf10SGeoff Levand 			printk(KERN_WARNING "%s:%d: lv1_map_device_dma_region "
6106bb5cf10SGeoff Levand 				"failed: %s\n", __func__, __LINE__,
6116bb5cf10SGeoff Levand 				ps3_result(result));
6126bb5cf10SGeoff Levand 			goto fail_map;
6136bb5cf10SGeoff Levand 		}
6146bb5cf10SGeoff Levand 		DBG("%s: pg=%d bus=%#lx, lpar=%#lx, ioid=%#x\n", __func__,
6156bb5cf10SGeoff Levand 		    iopage, c->bus_addr + offset, c->lpar_addr + offset,
6166bb5cf10SGeoff Levand 		    r->ioid);
6176bb5cf10SGeoff Levand 	}
6186bb5cf10SGeoff Levand 
6196bb5cf10SGeoff Levand 	/* be sure that last allocated one is inserted at head */
6206bb5cf10SGeoff Levand 	list_add(&c->link, &r->chunk_list.head);
6216bb5cf10SGeoff Levand 
6226bb5cf10SGeoff Levand 	*c_out = c;
6236bb5cf10SGeoff Levand 	DBG("%s: end\n", __func__);
6246bb5cf10SGeoff Levand 	return 0;
6256bb5cf10SGeoff Levand 
6266bb5cf10SGeoff Levand fail_map:
6276bb5cf10SGeoff Levand 	for (iopage--; 0 <= iopage; iopage--) {
6286bb5cf10SGeoff Levand 		lv1_put_iopte(0,
6296bb5cf10SGeoff Levand 			      c->bus_addr + offset,
6306bb5cf10SGeoff Levand 			      c->lpar_addr + offset,
6316bb5cf10SGeoff Levand 			      r->ioid,
6326bb5cf10SGeoff Levand 			      0);
6336bb5cf10SGeoff Levand 	}
6346bb5cf10SGeoff Levand 	kfree(c);
6356bb5cf10SGeoff Levand fail_alloc:
6366bb5cf10SGeoff Levand 	*c_out = NULL;
6376bb5cf10SGeoff Levand 	return result;
6386bb5cf10SGeoff Levand }
6396bb5cf10SGeoff Levand 
640f58a9d17SGeoff Levand /**
6416bb5cf10SGeoff Levand  * dma_sb_region_create - Create a device dma region.
642f58a9d17SGeoff Levand  * @r: Pointer to a struct ps3_dma_region.
643f58a9d17SGeoff Levand  *
644f58a9d17SGeoff Levand  * This is the lowest level dma region create routine, and is the one that
645f58a9d17SGeoff Levand  * will make the HV call to create the region.
646f58a9d17SGeoff Levand  */
647f58a9d17SGeoff Levand 
6486bb5cf10SGeoff Levand static int dma_sb_region_create(struct ps3_dma_region *r)
649f58a9d17SGeoff Levand {
650f58a9d17SGeoff Levand 	int result;
651b17b3df1SStephen Rothwell 	u64 bus_addr;
652f58a9d17SGeoff Levand 
65362d80749SGeoff Levand 	DBG(" -> %s:%d:\n", __func__, __LINE__);
6546bb5cf10SGeoff Levand 
6556bb5cf10SGeoff Levand 	BUG_ON(!r);
6566bb5cf10SGeoff Levand 
6576bb5cf10SGeoff Levand 	if (!r->dev->bus_id) {
6585c949070SStephen Rothwell 		pr_info("%s:%d: %llu:%llu no dma\n", __func__, __LINE__,
6596bb5cf10SGeoff Levand 			r->dev->bus_id, r->dev->dev_id);
6606bb5cf10SGeoff Levand 		return 0;
6616bb5cf10SGeoff Levand 	}
6626bb5cf10SGeoff Levand 
6636bb5cf10SGeoff Levand 	DBG("%s:%u: len = 0x%lx, page_size = %u, offset = 0x%lx\n", __func__,
6646bb5cf10SGeoff Levand 	    __LINE__, r->len, r->page_size, r->offset);
6656bb5cf10SGeoff Levand 
6666bb5cf10SGeoff Levand 	BUG_ON(!r->len);
6676bb5cf10SGeoff Levand 	BUG_ON(!r->page_size);
6686bb5cf10SGeoff Levand 	BUG_ON(!r->region_ops);
6696bb5cf10SGeoff Levand 
670f58a9d17SGeoff Levand 	INIT_LIST_HEAD(&r->chunk_list.head);
671f58a9d17SGeoff Levand 	spin_lock_init(&r->chunk_list.lock);
672f58a9d17SGeoff Levand 
6736bb5cf10SGeoff Levand 	result = lv1_allocate_device_dma_region(r->dev->bus_id, r->dev->dev_id,
6746bb5cf10SGeoff Levand 		roundup_pow_of_two(r->len), r->page_size, r->region_type,
675b17b3df1SStephen Rothwell 		&bus_addr);
676b17b3df1SStephen Rothwell 	r->bus_addr = bus_addr;
677f58a9d17SGeoff Levand 
678f58a9d17SGeoff Levand 	if (result) {
679f58a9d17SGeoff Levand 		DBG("%s:%d: lv1_allocate_device_dma_region failed: %s\n",
680f58a9d17SGeoff Levand 			__func__, __LINE__, ps3_result(result));
681f58a9d17SGeoff Levand 		r->len = r->bus_addr = 0;
682f58a9d17SGeoff Levand 	}
683f58a9d17SGeoff Levand 
684f58a9d17SGeoff Levand 	return result;
685f58a9d17SGeoff Levand }
686f58a9d17SGeoff Levand 
6876bb5cf10SGeoff Levand static int dma_ioc0_region_create(struct ps3_dma_region *r)
6886bb5cf10SGeoff Levand {
6896bb5cf10SGeoff Levand 	int result;
690b17b3df1SStephen Rothwell 	u64 bus_addr;
6916bb5cf10SGeoff Levand 
6926bb5cf10SGeoff Levand 	INIT_LIST_HEAD(&r->chunk_list.head);
6936bb5cf10SGeoff Levand 	spin_lock_init(&r->chunk_list.lock);
6946bb5cf10SGeoff Levand 
6956bb5cf10SGeoff Levand 	result = lv1_allocate_io_segment(0,
6966bb5cf10SGeoff Levand 					 r->len,
6976bb5cf10SGeoff Levand 					 r->page_size,
698b17b3df1SStephen Rothwell 					 &bus_addr);
699b17b3df1SStephen Rothwell 	r->bus_addr = bus_addr;
7006bb5cf10SGeoff Levand 	if (result) {
7016bb5cf10SGeoff Levand 		DBG("%s:%d: lv1_allocate_io_segment failed: %s\n",
7026bb5cf10SGeoff Levand 			__func__, __LINE__, ps3_result(result));
7036bb5cf10SGeoff Levand 		r->len = r->bus_addr = 0;
7046bb5cf10SGeoff Levand 	}
7056bb5cf10SGeoff Levand 	DBG("%s: len=%#lx, pg=%d, bus=%#lx\n", __func__,
7066bb5cf10SGeoff Levand 	    r->len, r->page_size, r->bus_addr);
7076bb5cf10SGeoff Levand 	return result;
7086bb5cf10SGeoff Levand }
7096bb5cf10SGeoff Levand 
710f58a9d17SGeoff Levand /**
711f58a9d17SGeoff Levand  * dma_region_free - Free a device dma region.
712f58a9d17SGeoff Levand  * @r: Pointer to a struct ps3_dma_region.
713f58a9d17SGeoff Levand  *
714f58a9d17SGeoff Levand  * This is the lowest level dma region free routine, and is the one that
715f58a9d17SGeoff Levand  * will make the HV call to free the region.
716f58a9d17SGeoff Levand  */
717f58a9d17SGeoff Levand 
7186bb5cf10SGeoff Levand static int dma_sb_region_free(struct ps3_dma_region *r)
719f58a9d17SGeoff Levand {
720f58a9d17SGeoff Levand 	int result;
721f58a9d17SGeoff Levand 	struct dma_chunk *c;
722f58a9d17SGeoff Levand 	struct dma_chunk *tmp;
723f58a9d17SGeoff Levand 
7246bb5cf10SGeoff Levand 	BUG_ON(!r);
7256bb5cf10SGeoff Levand 
7266bb5cf10SGeoff Levand 	if (!r->dev->bus_id) {
7275c949070SStephen Rothwell 		pr_info("%s:%d: %llu:%llu no dma\n", __func__, __LINE__,
7286bb5cf10SGeoff Levand 			r->dev->bus_id, r->dev->dev_id);
7296bb5cf10SGeoff Levand 		return 0;
730f58a9d17SGeoff Levand 	}
731f58a9d17SGeoff Levand 
7326bb5cf10SGeoff Levand 	list_for_each_entry_safe(c, tmp, &r->chunk_list.head, link) {
7336bb5cf10SGeoff Levand 		list_del(&c->link);
7346bb5cf10SGeoff Levand 		dma_sb_free_chunk(c);
7356bb5cf10SGeoff Levand 	}
7366bb5cf10SGeoff Levand 
7376bb5cf10SGeoff Levand 	result = lv1_free_device_dma_region(r->dev->bus_id, r->dev->dev_id,
738f58a9d17SGeoff Levand 		r->bus_addr);
739f58a9d17SGeoff Levand 
740f58a9d17SGeoff Levand 	if (result)
741f58a9d17SGeoff Levand 		DBG("%s:%d: lv1_free_device_dma_region failed: %s\n",
742f58a9d17SGeoff Levand 			__func__, __LINE__, ps3_result(result));
743f58a9d17SGeoff Levand 
7446bb5cf10SGeoff Levand 	r->bus_addr = 0;
7456bb5cf10SGeoff Levand 
7466bb5cf10SGeoff Levand 	return result;
7476bb5cf10SGeoff Levand }
7486bb5cf10SGeoff Levand 
7496bb5cf10SGeoff Levand static int dma_ioc0_region_free(struct ps3_dma_region *r)
7506bb5cf10SGeoff Levand {
7516bb5cf10SGeoff Levand 	int result;
7526bb5cf10SGeoff Levand 	struct dma_chunk *c, *n;
7536bb5cf10SGeoff Levand 
7546bb5cf10SGeoff Levand 	DBG("%s: start\n", __func__);
7556bb5cf10SGeoff Levand 	list_for_each_entry_safe(c, n, &r->chunk_list.head, link) {
7566bb5cf10SGeoff Levand 		list_del(&c->link);
7576bb5cf10SGeoff Levand 		dma_ioc0_free_chunk(c);
7586bb5cf10SGeoff Levand 	}
7596bb5cf10SGeoff Levand 
7606bb5cf10SGeoff Levand 	result = lv1_release_io_segment(0, r->bus_addr);
7616bb5cf10SGeoff Levand 
7626bb5cf10SGeoff Levand 	if (result)
7636bb5cf10SGeoff Levand 		DBG("%s:%d: lv1_free_device_dma_region failed: %s\n",
7646bb5cf10SGeoff Levand 			__func__, __LINE__, ps3_result(result));
7656bb5cf10SGeoff Levand 
7666bb5cf10SGeoff Levand 	r->bus_addr = 0;
7676bb5cf10SGeoff Levand 	DBG("%s: end\n", __func__);
768f58a9d17SGeoff Levand 
769f58a9d17SGeoff Levand 	return result;
770f58a9d17SGeoff Levand }
771f58a9d17SGeoff Levand 
772f58a9d17SGeoff Levand /**
7736bb5cf10SGeoff Levand  * dma_sb_map_area - Map an area of memory into a device dma region.
774f58a9d17SGeoff Levand  * @r: Pointer to a struct ps3_dma_region.
775f58a9d17SGeoff Levand  * @virt_addr: Starting virtual address of the area to map.
776f58a9d17SGeoff Levand  * @len: Length in bytes of the area to map.
777f58a9d17SGeoff Levand  * @bus_addr: A pointer to return the starting ioc bus address of the area to
778f58a9d17SGeoff Levand  * map.
779f58a9d17SGeoff Levand  *
780f58a9d17SGeoff Levand  * This is the common dma mapping routine.
781f58a9d17SGeoff Levand  */
782f58a9d17SGeoff Levand 
7836bb5cf10SGeoff Levand static int dma_sb_map_area(struct ps3_dma_region *r, unsigned long virt_addr,
784494fd07aSStephen Rothwell 	   unsigned long len, dma_addr_t *bus_addr,
7856bb5cf10SGeoff Levand 	   u64 iopte_flag)
786f58a9d17SGeoff Levand {
787f58a9d17SGeoff Levand 	int result;
788f58a9d17SGeoff Levand 	unsigned long flags;
789f58a9d17SGeoff Levand 	struct dma_chunk *c;
790f58a9d17SGeoff Levand 	unsigned long phys_addr = is_kernel_addr(virt_addr) ? __pa(virt_addr)
791f58a9d17SGeoff Levand 		: virt_addr;
7926bb5cf10SGeoff Levand 	unsigned long aligned_phys = _ALIGN_DOWN(phys_addr, 1 << r->page_size);
7936bb5cf10SGeoff Levand 	unsigned long aligned_len = _ALIGN_UP(len + phys_addr - aligned_phys,
7946bb5cf10SGeoff Levand 					      1 << r->page_size);
7956bb5cf10SGeoff Levand 	*bus_addr = dma_sb_lpar_to_bus(r, ps3_mm_phys_to_lpar(phys_addr));
796f58a9d17SGeoff Levand 
797f58a9d17SGeoff Levand 	if (!USE_DYNAMIC_DMA) {
798f58a9d17SGeoff Levand 		unsigned long lpar_addr = ps3_mm_phys_to_lpar(phys_addr);
799f58a9d17SGeoff Levand 		DBG(" -> %s:%d\n", __func__, __LINE__);
800f58a9d17SGeoff Levand 		DBG("%s:%d virt_addr %lxh\n", __func__, __LINE__,
801f58a9d17SGeoff Levand 			virt_addr);
802f58a9d17SGeoff Levand 		DBG("%s:%d phys_addr %lxh\n", __func__, __LINE__,
803f58a9d17SGeoff Levand 			phys_addr);
804f58a9d17SGeoff Levand 		DBG("%s:%d lpar_addr %lxh\n", __func__, __LINE__,
805f58a9d17SGeoff Levand 			lpar_addr);
806f58a9d17SGeoff Levand 		DBG("%s:%d len       %lxh\n", __func__, __LINE__, len);
807494fd07aSStephen Rothwell 		DBG("%s:%d bus_addr  %llxh (%lxh)\n", __func__, __LINE__,
808f58a9d17SGeoff Levand 		*bus_addr, len);
809f58a9d17SGeoff Levand 	}
810f58a9d17SGeoff Levand 
811f58a9d17SGeoff Levand 	spin_lock_irqsave(&r->chunk_list.lock, flags);
812f58a9d17SGeoff Levand 	c = dma_find_chunk(r, *bus_addr, len);
813f58a9d17SGeoff Levand 
814f58a9d17SGeoff Levand 	if (c) {
8156bb5cf10SGeoff Levand 		DBG("%s:%d: reusing mapped chunk", __func__, __LINE__);
8166bb5cf10SGeoff Levand 		dma_dump_chunk(c);
817f58a9d17SGeoff Levand 		c->usage_count++;
818f58a9d17SGeoff Levand 		spin_unlock_irqrestore(&r->chunk_list.lock, flags);
819f58a9d17SGeoff Levand 		return 0;
820f58a9d17SGeoff Levand 	}
821f58a9d17SGeoff Levand 
8226bb5cf10SGeoff Levand 	result = dma_sb_map_pages(r, aligned_phys, aligned_len, &c, iopte_flag);
823f58a9d17SGeoff Levand 
824f58a9d17SGeoff Levand 	if (result) {
825f58a9d17SGeoff Levand 		*bus_addr = 0;
8266bb5cf10SGeoff Levand 		DBG("%s:%d: dma_sb_map_pages failed (%d)\n",
827f58a9d17SGeoff Levand 			__func__, __LINE__, result);
828f58a9d17SGeoff Levand 		spin_unlock_irqrestore(&r->chunk_list.lock, flags);
829f58a9d17SGeoff Levand 		return result;
830f58a9d17SGeoff Levand 	}
831f58a9d17SGeoff Levand 
832f58a9d17SGeoff Levand 	c->usage_count = 1;
833f58a9d17SGeoff Levand 
834f58a9d17SGeoff Levand 	spin_unlock_irqrestore(&r->chunk_list.lock, flags);
835f58a9d17SGeoff Levand 	return result;
836f58a9d17SGeoff Levand }
837f58a9d17SGeoff Levand 
8386bb5cf10SGeoff Levand static int dma_ioc0_map_area(struct ps3_dma_region *r, unsigned long virt_addr,
839494fd07aSStephen Rothwell 	     unsigned long len, dma_addr_t *bus_addr,
8406bb5cf10SGeoff Levand 	     u64 iopte_flag)
8416bb5cf10SGeoff Levand {
8426bb5cf10SGeoff Levand 	int result;
8436bb5cf10SGeoff Levand 	unsigned long flags;
8446bb5cf10SGeoff Levand 	struct dma_chunk *c;
8456bb5cf10SGeoff Levand 	unsigned long phys_addr = is_kernel_addr(virt_addr) ? __pa(virt_addr)
8466bb5cf10SGeoff Levand 		: virt_addr;
8476bb5cf10SGeoff Levand 	unsigned long aligned_phys = _ALIGN_DOWN(phys_addr, 1 << r->page_size);
8486bb5cf10SGeoff Levand 	unsigned long aligned_len = _ALIGN_UP(len + phys_addr - aligned_phys,
8496bb5cf10SGeoff Levand 					      1 << r->page_size);
8506bb5cf10SGeoff Levand 
8516bb5cf10SGeoff Levand 	DBG(KERN_ERR "%s: vaddr=%#lx, len=%#lx\n", __func__,
8526bb5cf10SGeoff Levand 	    virt_addr, len);
8536bb5cf10SGeoff Levand 	DBG(KERN_ERR "%s: ph=%#lx a_ph=%#lx a_l=%#lx\n", __func__,
8546bb5cf10SGeoff Levand 	    phys_addr, aligned_phys, aligned_len);
8556bb5cf10SGeoff Levand 
8566bb5cf10SGeoff Levand 	spin_lock_irqsave(&r->chunk_list.lock, flags);
8576bb5cf10SGeoff Levand 	c = dma_find_chunk_lpar(r, ps3_mm_phys_to_lpar(phys_addr), len);
8586bb5cf10SGeoff Levand 
8596bb5cf10SGeoff Levand 	if (c) {
8606bb5cf10SGeoff Levand 		/* FIXME */
8616bb5cf10SGeoff Levand 		BUG();
8626bb5cf10SGeoff Levand 		*bus_addr = c->bus_addr + phys_addr - aligned_phys;
8636bb5cf10SGeoff Levand 		c->usage_count++;
8646bb5cf10SGeoff Levand 		spin_unlock_irqrestore(&r->chunk_list.lock, flags);
8656bb5cf10SGeoff Levand 		return 0;
8666bb5cf10SGeoff Levand 	}
8676bb5cf10SGeoff Levand 
8686bb5cf10SGeoff Levand 	result = dma_ioc0_map_pages(r, aligned_phys, aligned_len, &c,
8696bb5cf10SGeoff Levand 				    iopte_flag);
8706bb5cf10SGeoff Levand 
8716bb5cf10SGeoff Levand 	if (result) {
8726bb5cf10SGeoff Levand 		*bus_addr = 0;
8736bb5cf10SGeoff Levand 		DBG("%s:%d: dma_ioc0_map_pages failed (%d)\n",
8746bb5cf10SGeoff Levand 			__func__, __LINE__, result);
8756bb5cf10SGeoff Levand 		spin_unlock_irqrestore(&r->chunk_list.lock, flags);
8766bb5cf10SGeoff Levand 		return result;
8776bb5cf10SGeoff Levand 	}
8786bb5cf10SGeoff Levand 	*bus_addr = c->bus_addr + phys_addr - aligned_phys;
879494fd07aSStephen Rothwell 	DBG("%s: va=%#lx pa=%#lx a_pa=%#lx bus=%#llx\n", __func__,
8806bb5cf10SGeoff Levand 	    virt_addr, phys_addr, aligned_phys, *bus_addr);
8816bb5cf10SGeoff Levand 	c->usage_count = 1;
8826bb5cf10SGeoff Levand 
8836bb5cf10SGeoff Levand 	spin_unlock_irqrestore(&r->chunk_list.lock, flags);
8846bb5cf10SGeoff Levand 	return result;
8856bb5cf10SGeoff Levand }
8866bb5cf10SGeoff Levand 
887f58a9d17SGeoff Levand /**
8886bb5cf10SGeoff Levand  * dma_sb_unmap_area - Unmap an area of memory from a device dma region.
889f58a9d17SGeoff Levand  * @r: Pointer to a struct ps3_dma_region.
890f58a9d17SGeoff Levand  * @bus_addr: The starting ioc bus address of the area to unmap.
891f58a9d17SGeoff Levand  * @len: Length in bytes of the area to unmap.
892f58a9d17SGeoff Levand  *
893f58a9d17SGeoff Levand  * This is the common dma unmap routine.
894f58a9d17SGeoff Levand  */
895f58a9d17SGeoff Levand 
896494fd07aSStephen Rothwell static int dma_sb_unmap_area(struct ps3_dma_region *r, dma_addr_t bus_addr,
897f58a9d17SGeoff Levand 	unsigned long len)
898f58a9d17SGeoff Levand {
899f58a9d17SGeoff Levand 	unsigned long flags;
900f58a9d17SGeoff Levand 	struct dma_chunk *c;
901f58a9d17SGeoff Levand 
902f58a9d17SGeoff Levand 	spin_lock_irqsave(&r->chunk_list.lock, flags);
903f58a9d17SGeoff Levand 	c = dma_find_chunk(r, bus_addr, len);
904f58a9d17SGeoff Levand 
905f58a9d17SGeoff Levand 	if (!c) {
906f58a9d17SGeoff Levand 		unsigned long aligned_bus = _ALIGN_DOWN(bus_addr,
907f58a9d17SGeoff Levand 			1 << r->page_size);
9086bb5cf10SGeoff Levand 		unsigned long aligned_len = _ALIGN_UP(len + bus_addr
9096bb5cf10SGeoff Levand 			- aligned_bus, 1 << r->page_size);
910494fd07aSStephen Rothwell 		DBG("%s:%d: not found: bus_addr %llxh\n",
911f58a9d17SGeoff Levand 			__func__, __LINE__, bus_addr);
912f58a9d17SGeoff Levand 		DBG("%s:%d: not found: len %lxh\n",
913f58a9d17SGeoff Levand 			__func__, __LINE__, len);
914f58a9d17SGeoff Levand 		DBG("%s:%d: not found: aligned_bus %lxh\n",
915f58a9d17SGeoff Levand 			__func__, __LINE__, aligned_bus);
916f58a9d17SGeoff Levand 		DBG("%s:%d: not found: aligned_len %lxh\n",
917f58a9d17SGeoff Levand 			__func__, __LINE__, aligned_len);
918f58a9d17SGeoff Levand 		BUG();
919f58a9d17SGeoff Levand 	}
920f58a9d17SGeoff Levand 
921f58a9d17SGeoff Levand 	c->usage_count--;
922f58a9d17SGeoff Levand 
923f58a9d17SGeoff Levand 	if (!c->usage_count) {
924f58a9d17SGeoff Levand 		list_del(&c->link);
9256bb5cf10SGeoff Levand 		dma_sb_free_chunk(c);
926f58a9d17SGeoff Levand 	}
927f58a9d17SGeoff Levand 
928f58a9d17SGeoff Levand 	spin_unlock_irqrestore(&r->chunk_list.lock, flags);
929f58a9d17SGeoff Levand 	return 0;
930f58a9d17SGeoff Levand }
931f58a9d17SGeoff Levand 
93232f44a12SGeert Uytterhoeven static int dma_ioc0_unmap_area(struct ps3_dma_region *r,
933494fd07aSStephen Rothwell 			dma_addr_t bus_addr, unsigned long len)
9346bb5cf10SGeoff Levand {
9356bb5cf10SGeoff Levand 	unsigned long flags;
9366bb5cf10SGeoff Levand 	struct dma_chunk *c;
9376bb5cf10SGeoff Levand 
938494fd07aSStephen Rothwell 	DBG("%s: start a=%#llx l=%#lx\n", __func__, bus_addr, len);
9396bb5cf10SGeoff Levand 	spin_lock_irqsave(&r->chunk_list.lock, flags);
9406bb5cf10SGeoff Levand 	c = dma_find_chunk(r, bus_addr, len);
9416bb5cf10SGeoff Levand 
9426bb5cf10SGeoff Levand 	if (!c) {
9436bb5cf10SGeoff Levand 		unsigned long aligned_bus = _ALIGN_DOWN(bus_addr,
9446bb5cf10SGeoff Levand 							1 << r->page_size);
9456bb5cf10SGeoff Levand 		unsigned long aligned_len = _ALIGN_UP(len + bus_addr
9466bb5cf10SGeoff Levand 						      - aligned_bus,
9476bb5cf10SGeoff Levand 						      1 << r->page_size);
948494fd07aSStephen Rothwell 		DBG("%s:%d: not found: bus_addr %llxh\n",
9496bb5cf10SGeoff Levand 		    __func__, __LINE__, bus_addr);
9506bb5cf10SGeoff Levand 		DBG("%s:%d: not found: len %lxh\n",
9516bb5cf10SGeoff Levand 		    __func__, __LINE__, len);
9526bb5cf10SGeoff Levand 		DBG("%s:%d: not found: aligned_bus %lxh\n",
9536bb5cf10SGeoff Levand 		    __func__, __LINE__, aligned_bus);
9546bb5cf10SGeoff Levand 		DBG("%s:%d: not found: aligned_len %lxh\n",
9556bb5cf10SGeoff Levand 		    __func__, __LINE__, aligned_len);
9566bb5cf10SGeoff Levand 		BUG();
9576bb5cf10SGeoff Levand 	}
9586bb5cf10SGeoff Levand 
9596bb5cf10SGeoff Levand 	c->usage_count--;
9606bb5cf10SGeoff Levand 
9616bb5cf10SGeoff Levand 	if (!c->usage_count) {
9626bb5cf10SGeoff Levand 		list_del(&c->link);
9636bb5cf10SGeoff Levand 		dma_ioc0_free_chunk(c);
9646bb5cf10SGeoff Levand 	}
9656bb5cf10SGeoff Levand 
9666bb5cf10SGeoff Levand 	spin_unlock_irqrestore(&r->chunk_list.lock, flags);
9676bb5cf10SGeoff Levand 	DBG("%s: end\n", __func__);
9686bb5cf10SGeoff Levand 	return 0;
9696bb5cf10SGeoff Levand }
9706bb5cf10SGeoff Levand 
971f58a9d17SGeoff Levand /**
9726bb5cf10SGeoff Levand  * dma_sb_region_create_linear - Setup a linear dma mapping for a device.
973f58a9d17SGeoff Levand  * @r: Pointer to a struct ps3_dma_region.
974f58a9d17SGeoff Levand  *
975f58a9d17SGeoff Levand  * This routine creates an HV dma region for the device and maps all available
976f58a9d17SGeoff Levand  * ram into the io controller bus address space.
977f58a9d17SGeoff Levand  */
978f58a9d17SGeoff Levand 
9796bb5cf10SGeoff Levand static int dma_sb_region_create_linear(struct ps3_dma_region *r)
980f58a9d17SGeoff Levand {
981f58a9d17SGeoff Levand 	int result;
982494fd07aSStephen Rothwell 	unsigned long virt_addr, len;
983494fd07aSStephen Rothwell 	dma_addr_t tmp;
984f58a9d17SGeoff Levand 
9856bb5cf10SGeoff Levand 	if (r->len > 16*1024*1024) {	/* FIXME: need proper fix */
986f58a9d17SGeoff Levand 		/* force 16M dma pages for linear mapping */
987f58a9d17SGeoff Levand 		if (r->page_size != PS3_DMA_16M) {
988f58a9d17SGeoff Levand 			pr_info("%s:%d: forcing 16M pages for linear map\n",
989f58a9d17SGeoff Levand 				__func__, __LINE__);
990f58a9d17SGeoff Levand 			r->page_size = PS3_DMA_16M;
9916bb5cf10SGeoff Levand 			r->len = _ALIGN_UP(r->len, 1 << r->page_size);
9926bb5cf10SGeoff Levand 		}
993f58a9d17SGeoff Levand 	}
994f58a9d17SGeoff Levand 
9956bb5cf10SGeoff Levand 	result = dma_sb_region_create(r);
996f58a9d17SGeoff Levand 	BUG_ON(result);
997f58a9d17SGeoff Levand 
9986bb5cf10SGeoff Levand 	if (r->offset < map.rm.size) {
9996bb5cf10SGeoff Levand 		/* Map (part of) 1st RAM chunk */
10006bb5cf10SGeoff Levand 		virt_addr = map.rm.base + r->offset;
10016bb5cf10SGeoff Levand 		len = map.rm.size - r->offset;
10026bb5cf10SGeoff Levand 		if (len > r->len)
10036bb5cf10SGeoff Levand 			len = r->len;
10046bb5cf10SGeoff Levand 		result = dma_sb_map_area(r, virt_addr, len, &tmp,
1005*5c6fc8dbSGeert Uytterhoeven 			CBE_IOPTE_PP_W | CBE_IOPTE_PP_R | CBE_IOPTE_SO_RW |
1006*5c6fc8dbSGeert Uytterhoeven 			CBE_IOPTE_M);
1007f58a9d17SGeoff Levand 		BUG_ON(result);
10086bb5cf10SGeoff Levand 	}
1009f58a9d17SGeoff Levand 
10106bb5cf10SGeoff Levand 	if (r->offset + r->len > map.rm.size) {
10116bb5cf10SGeoff Levand 		/* Map (part of) 2nd RAM chunk */
1012a628df1eSGeoff Levand 		virt_addr = map.rm.size;
10136bb5cf10SGeoff Levand 		len = r->len;
10146bb5cf10SGeoff Levand 		if (r->offset >= map.rm.size)
10156bb5cf10SGeoff Levand 			virt_addr += r->offset - map.rm.size;
1016f58a9d17SGeoff Levand 		else
10176bb5cf10SGeoff Levand 			len -= map.rm.size - r->offset;
10186bb5cf10SGeoff Levand 		result = dma_sb_map_area(r, virt_addr, len, &tmp,
1019*5c6fc8dbSGeert Uytterhoeven 			CBE_IOPTE_PP_W | CBE_IOPTE_PP_R | CBE_IOPTE_SO_RW |
1020*5c6fc8dbSGeert Uytterhoeven 			CBE_IOPTE_M);
1021f58a9d17SGeoff Levand 		BUG_ON(result);
10226bb5cf10SGeoff Levand 	}
1023f58a9d17SGeoff Levand 
1024f58a9d17SGeoff Levand 	return result;
1025f58a9d17SGeoff Levand }
1026f58a9d17SGeoff Levand 
1027f58a9d17SGeoff Levand /**
10286bb5cf10SGeoff Levand  * dma_sb_region_free_linear - Free a linear dma mapping for a device.
1029f58a9d17SGeoff Levand  * @r: Pointer to a struct ps3_dma_region.
1030f58a9d17SGeoff Levand  *
1031f58a9d17SGeoff Levand  * This routine will unmap all mapped areas and free the HV dma region.
1032f58a9d17SGeoff Levand  */
1033f58a9d17SGeoff Levand 
10346bb5cf10SGeoff Levand static int dma_sb_region_free_linear(struct ps3_dma_region *r)
1035f58a9d17SGeoff Levand {
1036f58a9d17SGeoff Levand 	int result;
1037494fd07aSStephen Rothwell 	dma_addr_t bus_addr;
1038494fd07aSStephen Rothwell 	unsigned long len, lpar_addr;
1039f58a9d17SGeoff Levand 
10406bb5cf10SGeoff Levand 	if (r->offset < map.rm.size) {
10416bb5cf10SGeoff Levand 		/* Unmap (part of) 1st RAM chunk */
10426bb5cf10SGeoff Levand 		lpar_addr = map.rm.base + r->offset;
10436bb5cf10SGeoff Levand 		len = map.rm.size - r->offset;
10446bb5cf10SGeoff Levand 		if (len > r->len)
10456bb5cf10SGeoff Levand 			len = r->len;
10466bb5cf10SGeoff Levand 		bus_addr = dma_sb_lpar_to_bus(r, lpar_addr);
10476bb5cf10SGeoff Levand 		result = dma_sb_unmap_area(r, bus_addr, len);
1048f58a9d17SGeoff Levand 		BUG_ON(result);
10496bb5cf10SGeoff Levand 	}
1050f58a9d17SGeoff Levand 
10516bb5cf10SGeoff Levand 	if (r->offset + r->len > map.rm.size) {
10526bb5cf10SGeoff Levand 		/* Unmap (part of) 2nd RAM chunk */
10536bb5cf10SGeoff Levand 		lpar_addr = map.r1.base;
10546bb5cf10SGeoff Levand 		len = r->len;
10556bb5cf10SGeoff Levand 		if (r->offset >= map.rm.size)
10566bb5cf10SGeoff Levand 			lpar_addr += r->offset - map.rm.size;
10576bb5cf10SGeoff Levand 		else
10586bb5cf10SGeoff Levand 			len -= map.rm.size - r->offset;
10596bb5cf10SGeoff Levand 		bus_addr = dma_sb_lpar_to_bus(r, lpar_addr);
10606bb5cf10SGeoff Levand 		result = dma_sb_unmap_area(r, bus_addr, len);
1061f58a9d17SGeoff Levand 		BUG_ON(result);
10626bb5cf10SGeoff Levand 	}
1063f58a9d17SGeoff Levand 
10646bb5cf10SGeoff Levand 	result = dma_sb_region_free(r);
1065f58a9d17SGeoff Levand 	BUG_ON(result);
1066f58a9d17SGeoff Levand 
1067f58a9d17SGeoff Levand 	return result;
1068f58a9d17SGeoff Levand }
1069f58a9d17SGeoff Levand 
1070f58a9d17SGeoff Levand /**
10716bb5cf10SGeoff Levand  * dma_sb_map_area_linear - Map an area of memory into a device dma region.
1072f58a9d17SGeoff Levand  * @r: Pointer to a struct ps3_dma_region.
1073f58a9d17SGeoff Levand  * @virt_addr: Starting virtual address of the area to map.
1074f58a9d17SGeoff Levand  * @len: Length in bytes of the area to map.
1075f58a9d17SGeoff Levand  * @bus_addr: A pointer to return the starting ioc bus address of the area to
1076f58a9d17SGeoff Levand  * map.
1077f58a9d17SGeoff Levand  *
10786bb5cf10SGeoff Levand  * This routine just returns the corresponding bus address.  Actual mapping
1079f58a9d17SGeoff Levand  * occurs in dma_region_create_linear().
1080f58a9d17SGeoff Levand  */
1081f58a9d17SGeoff Levand 
10826bb5cf10SGeoff Levand static int dma_sb_map_area_linear(struct ps3_dma_region *r,
1083494fd07aSStephen Rothwell 	unsigned long virt_addr, unsigned long len, dma_addr_t *bus_addr,
10846bb5cf10SGeoff Levand 	u64 iopte_flag)
1085f58a9d17SGeoff Levand {
1086f58a9d17SGeoff Levand 	unsigned long phys_addr = is_kernel_addr(virt_addr) ? __pa(virt_addr)
1087f58a9d17SGeoff Levand 		: virt_addr;
10886bb5cf10SGeoff Levand 	*bus_addr = dma_sb_lpar_to_bus(r, ps3_mm_phys_to_lpar(phys_addr));
1089f58a9d17SGeoff Levand 	return 0;
1090f58a9d17SGeoff Levand }
1091f58a9d17SGeoff Levand 
1092f58a9d17SGeoff Levand /**
1093f58a9d17SGeoff Levand  * dma_unmap_area_linear - Unmap an area of memory from a device dma region.
1094f58a9d17SGeoff Levand  * @r: Pointer to a struct ps3_dma_region.
1095f58a9d17SGeoff Levand  * @bus_addr: The starting ioc bus address of the area to unmap.
1096f58a9d17SGeoff Levand  * @len: Length in bytes of the area to unmap.
1097f58a9d17SGeoff Levand  *
10986bb5cf10SGeoff Levand  * This routine does nothing.  Unmapping occurs in dma_sb_region_free_linear().
1099f58a9d17SGeoff Levand  */
1100f58a9d17SGeoff Levand 
11016bb5cf10SGeoff Levand static int dma_sb_unmap_area_linear(struct ps3_dma_region *r,
1102494fd07aSStephen Rothwell 	dma_addr_t bus_addr, unsigned long len)
1103f58a9d17SGeoff Levand {
1104f58a9d17SGeoff Levand 	return 0;
11056bb5cf10SGeoff Levand };
11066bb5cf10SGeoff Levand 
11076bb5cf10SGeoff Levand static const struct ps3_dma_region_ops ps3_dma_sb_region_ops =  {
11086bb5cf10SGeoff Levand 	.create = dma_sb_region_create,
11096bb5cf10SGeoff Levand 	.free = dma_sb_region_free,
11106bb5cf10SGeoff Levand 	.map = dma_sb_map_area,
11116bb5cf10SGeoff Levand 	.unmap = dma_sb_unmap_area
11126bb5cf10SGeoff Levand };
11136bb5cf10SGeoff Levand 
11146bb5cf10SGeoff Levand static const struct ps3_dma_region_ops ps3_dma_sb_region_linear_ops = {
11156bb5cf10SGeoff Levand 	.create = dma_sb_region_create_linear,
11166bb5cf10SGeoff Levand 	.free = dma_sb_region_free_linear,
11176bb5cf10SGeoff Levand 	.map = dma_sb_map_area_linear,
11186bb5cf10SGeoff Levand 	.unmap = dma_sb_unmap_area_linear
11196bb5cf10SGeoff Levand };
11206bb5cf10SGeoff Levand 
11216bb5cf10SGeoff Levand static const struct ps3_dma_region_ops ps3_dma_ioc0_region_ops = {
11226bb5cf10SGeoff Levand 	.create = dma_ioc0_region_create,
11236bb5cf10SGeoff Levand 	.free = dma_ioc0_region_free,
11246bb5cf10SGeoff Levand 	.map = dma_ioc0_map_area,
11256bb5cf10SGeoff Levand 	.unmap = dma_ioc0_unmap_area
11266bb5cf10SGeoff Levand };
11276bb5cf10SGeoff Levand 
11286bb5cf10SGeoff Levand int ps3_dma_region_init(struct ps3_system_bus_device *dev,
11296bb5cf10SGeoff Levand 	struct ps3_dma_region *r, enum ps3_dma_page_size page_size,
11306bb5cf10SGeoff Levand 	enum ps3_dma_region_type region_type, void *addr, unsigned long len)
11316bb5cf10SGeoff Levand {
11326bb5cf10SGeoff Levand 	unsigned long lpar_addr;
11336bb5cf10SGeoff Levand 
11346bb5cf10SGeoff Levand 	lpar_addr = addr ? ps3_mm_phys_to_lpar(__pa(addr)) : 0;
11356bb5cf10SGeoff Levand 
11366bb5cf10SGeoff Levand 	r->dev = dev;
11376bb5cf10SGeoff Levand 	r->page_size = page_size;
11386bb5cf10SGeoff Levand 	r->region_type = region_type;
11396bb5cf10SGeoff Levand 	r->offset = lpar_addr;
11406bb5cf10SGeoff Levand 	if (r->offset >= map.rm.size)
11416bb5cf10SGeoff Levand 		r->offset -= map.r1.offset;
11426bb5cf10SGeoff Levand 	r->len = len ? len : _ALIGN_UP(map.total, 1 << r->page_size);
11436bb5cf10SGeoff Levand 
11446bb5cf10SGeoff Levand 	switch (dev->dev_type) {
11456bb5cf10SGeoff Levand 	case PS3_DEVICE_TYPE_SB:
11466bb5cf10SGeoff Levand 		r->region_ops =  (USE_DYNAMIC_DMA)
11476bb5cf10SGeoff Levand 			? &ps3_dma_sb_region_ops
11486bb5cf10SGeoff Levand 			: &ps3_dma_sb_region_linear_ops;
11496bb5cf10SGeoff Levand 		break;
11506bb5cf10SGeoff Levand 	case PS3_DEVICE_TYPE_IOC0:
11516bb5cf10SGeoff Levand 		r->region_ops = &ps3_dma_ioc0_region_ops;
11526bb5cf10SGeoff Levand 		break;
11536bb5cf10SGeoff Levand 	default:
11546bb5cf10SGeoff Levand 		BUG();
11556bb5cf10SGeoff Levand 		return -EINVAL;
1156f58a9d17SGeoff Levand 	}
11576bb5cf10SGeoff Levand 	return 0;
11586bb5cf10SGeoff Levand }
11596bb5cf10SGeoff Levand EXPORT_SYMBOL(ps3_dma_region_init);
1160f58a9d17SGeoff Levand 
1161f58a9d17SGeoff Levand int ps3_dma_region_create(struct ps3_dma_region *r)
1162f58a9d17SGeoff Levand {
11636bb5cf10SGeoff Levand 	BUG_ON(!r);
11646bb5cf10SGeoff Levand 	BUG_ON(!r->region_ops);
11656bb5cf10SGeoff Levand 	BUG_ON(!r->region_ops->create);
11666bb5cf10SGeoff Levand 	return r->region_ops->create(r);
1167f58a9d17SGeoff Levand }
11686bb5cf10SGeoff Levand EXPORT_SYMBOL(ps3_dma_region_create);
1169f58a9d17SGeoff Levand 
1170f58a9d17SGeoff Levand int ps3_dma_region_free(struct ps3_dma_region *r)
1171f58a9d17SGeoff Levand {
11726bb5cf10SGeoff Levand 	BUG_ON(!r);
11736bb5cf10SGeoff Levand 	BUG_ON(!r->region_ops);
11746bb5cf10SGeoff Levand 	BUG_ON(!r->region_ops->free);
11756bb5cf10SGeoff Levand 	return r->region_ops->free(r);
1176f58a9d17SGeoff Levand }
11776bb5cf10SGeoff Levand EXPORT_SYMBOL(ps3_dma_region_free);
1178f58a9d17SGeoff Levand 
1179f58a9d17SGeoff Levand int ps3_dma_map(struct ps3_dma_region *r, unsigned long virt_addr,
1180494fd07aSStephen Rothwell 	unsigned long len, dma_addr_t *bus_addr,
11816bb5cf10SGeoff Levand 	u64 iopte_flag)
1182f58a9d17SGeoff Levand {
11836bb5cf10SGeoff Levand 	return r->region_ops->map(r, virt_addr, len, bus_addr, iopte_flag);
1184f58a9d17SGeoff Levand }
1185f58a9d17SGeoff Levand 
1186494fd07aSStephen Rothwell int ps3_dma_unmap(struct ps3_dma_region *r, dma_addr_t bus_addr,
1187f58a9d17SGeoff Levand 	unsigned long len)
1188f58a9d17SGeoff Levand {
11896bb5cf10SGeoff Levand 	return r->region_ops->unmap(r, bus_addr, len);
1190f58a9d17SGeoff Levand }
1191f58a9d17SGeoff Levand 
1192f58a9d17SGeoff Levand /*============================================================================*/
1193f58a9d17SGeoff Levand /* system startup routines                                                    */
1194f58a9d17SGeoff Levand /*============================================================================*/
1195f58a9d17SGeoff Levand 
1196f58a9d17SGeoff Levand /**
1197f58a9d17SGeoff Levand  * ps3_mm_init - initialize the address space state variables
1198f58a9d17SGeoff Levand  */
1199f58a9d17SGeoff Levand 
1200f58a9d17SGeoff Levand void __init ps3_mm_init(void)
1201f58a9d17SGeoff Levand {
1202f58a9d17SGeoff Levand 	int result;
1203f58a9d17SGeoff Levand 
1204f58a9d17SGeoff Levand 	DBG(" -> %s:%d\n", __func__, __LINE__);
1205f58a9d17SGeoff Levand 
1206f58a9d17SGeoff Levand 	result = ps3_repository_read_mm_info(&map.rm.base, &map.rm.size,
1207f58a9d17SGeoff Levand 		&map.total);
1208f58a9d17SGeoff Levand 
1209f58a9d17SGeoff Levand 	if (result)
1210f58a9d17SGeoff Levand 		panic("ps3_repository_read_mm_info() failed");
1211f58a9d17SGeoff Levand 
1212f58a9d17SGeoff Levand 	map.rm.offset = map.rm.base;
1213f58a9d17SGeoff Levand 	map.vas_id = map.htab_size = 0;
1214f58a9d17SGeoff Levand 
1215f58a9d17SGeoff Levand 	/* this implementation assumes map.rm.base is zero */
1216f58a9d17SGeoff Levand 
1217f58a9d17SGeoff Levand 	BUG_ON(map.rm.base);
1218f58a9d17SGeoff Levand 	BUG_ON(!map.rm.size);
1219f58a9d17SGeoff Levand 
1220f58a9d17SGeoff Levand 
1221f58a9d17SGeoff Levand 	/* arrange to do this in ps3_mm_add_memory */
1222f58a9d17SGeoff Levand 	ps3_mm_region_create(&map.r1, map.total - map.rm.size);
1223f58a9d17SGeoff Levand 
12246bb5cf10SGeoff Levand 	/* correct map.total for the real total amount of memory we use */
12256bb5cf10SGeoff Levand 	map.total = map.rm.size + map.r1.size;
12266bb5cf10SGeoff Levand 
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