xref: /linux/arch/powerpc/platforms/ps3/mm.c (revision 6bb5cf1025414fe00b20f3bef56135849e4ed3b8)
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>
24f58a9d17SGeoff Levand 
25e22ba7e3SArnd Bergmann #include <asm/firmware.h>
26f58a9d17SGeoff Levand #include <asm/lmb.h>
27f58a9d17SGeoff Levand #include <asm/udbg.h>
28f58a9d17SGeoff Levand #include <asm/lv1call.h>
29f58a9d17SGeoff Levand 
30f58a9d17SGeoff Levand #include "platform.h"
31f58a9d17SGeoff Levand 
32f58a9d17SGeoff Levand #if defined(DEBUG)
3383bb643dSGeert Uytterhoeven #define DBG udbg_printf
34f58a9d17SGeoff Levand #else
3583bb643dSGeert Uytterhoeven #define DBG pr_debug
36f58a9d17SGeoff Levand #endif
37f58a9d17SGeoff Levand 
38f58a9d17SGeoff Levand enum {
39f58a9d17SGeoff Levand #if defined(CONFIG_PS3_USE_LPAR_ADDR)
40f58a9d17SGeoff Levand 	USE_LPAR_ADDR = 1,
41f58a9d17SGeoff Levand #else
42f58a9d17SGeoff Levand 	USE_LPAR_ADDR = 0,
43f58a9d17SGeoff Levand #endif
44f58a9d17SGeoff Levand #if defined(CONFIG_PS3_DYNAMIC_DMA)
45f58a9d17SGeoff Levand 	USE_DYNAMIC_DMA = 1,
46f58a9d17SGeoff Levand #else
47f58a9d17SGeoff Levand 	USE_DYNAMIC_DMA = 0,
48f58a9d17SGeoff Levand #endif
49f58a9d17SGeoff Levand };
50f58a9d17SGeoff Levand 
51f58a9d17SGeoff Levand enum {
52f58a9d17SGeoff Levand 	PAGE_SHIFT_4K = 12U,
53f58a9d17SGeoff Levand 	PAGE_SHIFT_64K = 16U,
54f58a9d17SGeoff Levand 	PAGE_SHIFT_16M = 24U,
55f58a9d17SGeoff Levand };
56f58a9d17SGeoff Levand 
57f58a9d17SGeoff Levand static unsigned long make_page_sizes(unsigned long a, unsigned long b)
58f58a9d17SGeoff Levand {
59f58a9d17SGeoff Levand 	return (a << 56) | (b << 48);
60f58a9d17SGeoff Levand }
61f58a9d17SGeoff Levand 
62f58a9d17SGeoff Levand enum {
63f58a9d17SGeoff Levand 	ALLOCATE_MEMORY_TRY_ALT_UNIT = 0X04,
64f58a9d17SGeoff Levand 	ALLOCATE_MEMORY_ADDR_ZERO = 0X08,
65f58a9d17SGeoff Levand };
66f58a9d17SGeoff Levand 
67f58a9d17SGeoff Levand /* valid htab sizes are {18,19,20} = 256K, 512K, 1M */
68f58a9d17SGeoff Levand 
69f58a9d17SGeoff Levand enum {
70f58a9d17SGeoff Levand 	HTAB_SIZE_MAX = 20U, /* HV limit of 1MB */
71f58a9d17SGeoff Levand 	HTAB_SIZE_MIN = 18U, /* CPU limit of 256KB */
72f58a9d17SGeoff Levand };
73f58a9d17SGeoff Levand 
74f58a9d17SGeoff Levand /*============================================================================*/
75f58a9d17SGeoff Levand /* virtual address space routines                                             */
76f58a9d17SGeoff Levand /*============================================================================*/
77f58a9d17SGeoff Levand 
78f58a9d17SGeoff Levand /**
79f58a9d17SGeoff Levand  * struct mem_region - memory region structure
80f58a9d17SGeoff Levand  * @base: base address
81f58a9d17SGeoff Levand  * @size: size in bytes
82f58a9d17SGeoff Levand  * @offset: difference between base and rm.size
83f58a9d17SGeoff Levand  */
84f58a9d17SGeoff Levand 
85f58a9d17SGeoff Levand struct mem_region {
86f58a9d17SGeoff Levand 	unsigned long base;
87f58a9d17SGeoff Levand 	unsigned long size;
88f58a9d17SGeoff Levand 	unsigned long offset;
89f58a9d17SGeoff Levand };
90f58a9d17SGeoff Levand 
91f58a9d17SGeoff Levand /**
92f58a9d17SGeoff Levand  * struct map - address space state variables holder
93f58a9d17SGeoff Levand  * @total: total memory available as reported by HV
94f58a9d17SGeoff Levand  * @vas_id - HV virtual address space id
95f58a9d17SGeoff Levand  * @htab_size: htab size in bytes
96f58a9d17SGeoff Levand  *
97f58a9d17SGeoff Levand  * The HV virtual address space (vas) allows for hotplug memory regions.
98f58a9d17SGeoff Levand  * Memory regions can be created and destroyed in the vas at runtime.
99f58a9d17SGeoff Levand  * @rm: real mode (bootmem) region
100f58a9d17SGeoff Levand  * @r1: hotplug memory region(s)
101f58a9d17SGeoff Levand  *
102f58a9d17SGeoff Levand  * ps3 addresses
103f58a9d17SGeoff Levand  * virt_addr: a cpu 'translated' effective address
104f58a9d17SGeoff Levand  * phys_addr: an address in what Linux thinks is the physical address space
105f58a9d17SGeoff Levand  * lpar_addr: an address in the HV virtual address space
106f58a9d17SGeoff Levand  * bus_addr: an io controller 'translated' address on a device bus
107f58a9d17SGeoff Levand  */
108f58a9d17SGeoff Levand 
109f58a9d17SGeoff Levand struct map {
110f58a9d17SGeoff Levand 	unsigned long total;
111f58a9d17SGeoff Levand 	unsigned long vas_id;
112f58a9d17SGeoff Levand 	unsigned long htab_size;
113f58a9d17SGeoff Levand 	struct mem_region rm;
114f58a9d17SGeoff Levand 	struct mem_region r1;
115f58a9d17SGeoff Levand };
116f58a9d17SGeoff Levand 
117f58a9d17SGeoff Levand #define debug_dump_map(x) _debug_dump_map(x, __func__, __LINE__)
118*6bb5cf10SGeoff Levand static void __maybe_unused _debug_dump_map(const struct map *m,
119*6bb5cf10SGeoff Levand 	const char *func, int line)
120f58a9d17SGeoff Levand {
121f58a9d17SGeoff Levand 	DBG("%s:%d: map.total     = %lxh\n", func, line, m->total);
122f58a9d17SGeoff Levand 	DBG("%s:%d: map.rm.size   = %lxh\n", func, line, m->rm.size);
123f58a9d17SGeoff Levand 	DBG("%s:%d: map.vas_id    = %lu\n", func, line, m->vas_id);
124f58a9d17SGeoff Levand 	DBG("%s:%d: map.htab_size = %lxh\n", func, line, m->htab_size);
125f58a9d17SGeoff Levand 	DBG("%s:%d: map.r1.base   = %lxh\n", func, line, m->r1.base);
126f58a9d17SGeoff Levand 	DBG("%s:%d: map.r1.offset = %lxh\n", func, line, m->r1.offset);
127f58a9d17SGeoff Levand 	DBG("%s:%d: map.r1.size   = %lxh\n", func, line, m->r1.size);
128f58a9d17SGeoff Levand }
129f58a9d17SGeoff Levand 
130f58a9d17SGeoff Levand static struct map map;
131f58a9d17SGeoff Levand 
132f58a9d17SGeoff Levand /**
133f58a9d17SGeoff Levand  * ps3_mm_phys_to_lpar - translate a linux physical address to lpar address
134f58a9d17SGeoff Levand  * @phys_addr: linux physical address
135f58a9d17SGeoff Levand  */
136f58a9d17SGeoff Levand 
137f58a9d17SGeoff Levand unsigned long ps3_mm_phys_to_lpar(unsigned long phys_addr)
138f58a9d17SGeoff Levand {
139f58a9d17SGeoff Levand 	BUG_ON(is_kernel_addr(phys_addr));
140f58a9d17SGeoff Levand 	if (USE_LPAR_ADDR)
141f58a9d17SGeoff Levand 		return phys_addr;
142f58a9d17SGeoff Levand 	else
143f58a9d17SGeoff Levand 		return (phys_addr < map.rm.size || phys_addr >= map.total)
144f58a9d17SGeoff Levand 			? phys_addr : phys_addr + map.r1.offset;
145f58a9d17SGeoff Levand }
146f58a9d17SGeoff Levand 
147f58a9d17SGeoff Levand EXPORT_SYMBOL(ps3_mm_phys_to_lpar);
148f58a9d17SGeoff Levand 
149f58a9d17SGeoff Levand /**
150f58a9d17SGeoff Levand  * ps3_mm_vas_create - create the virtual address space
151f58a9d17SGeoff Levand  */
152f58a9d17SGeoff Levand 
153f58a9d17SGeoff Levand void __init ps3_mm_vas_create(unsigned long* htab_size)
154f58a9d17SGeoff Levand {
155f58a9d17SGeoff Levand 	int result;
156f58a9d17SGeoff Levand 	unsigned long start_address;
157f58a9d17SGeoff Levand 	unsigned long size;
158f58a9d17SGeoff Levand 	unsigned long access_right;
159f58a9d17SGeoff Levand 	unsigned long max_page_size;
160f58a9d17SGeoff Levand 	unsigned long flags;
161f58a9d17SGeoff Levand 
162f58a9d17SGeoff Levand 	result = lv1_query_logical_partition_address_region_info(0,
163f58a9d17SGeoff Levand 		&start_address, &size, &access_right, &max_page_size,
164f58a9d17SGeoff Levand 		&flags);
165f58a9d17SGeoff Levand 
166f58a9d17SGeoff Levand 	if (result) {
167f58a9d17SGeoff Levand 		DBG("%s:%d: lv1_query_logical_partition_address_region_info "
168f58a9d17SGeoff Levand 			"failed: %s\n", __func__, __LINE__,
169f58a9d17SGeoff Levand 			ps3_result(result));
170f58a9d17SGeoff Levand 		goto fail;
171f58a9d17SGeoff Levand 	}
172f58a9d17SGeoff Levand 
173f58a9d17SGeoff Levand 	if (max_page_size < PAGE_SHIFT_16M) {
174f58a9d17SGeoff Levand 		DBG("%s:%d: bad max_page_size %lxh\n", __func__, __LINE__,
175f58a9d17SGeoff Levand 			max_page_size);
176f58a9d17SGeoff Levand 		goto fail;
177f58a9d17SGeoff Levand 	}
178f58a9d17SGeoff Levand 
179f58a9d17SGeoff Levand 	BUILD_BUG_ON(CONFIG_PS3_HTAB_SIZE > HTAB_SIZE_MAX);
180f58a9d17SGeoff Levand 	BUILD_BUG_ON(CONFIG_PS3_HTAB_SIZE < HTAB_SIZE_MIN);
181f58a9d17SGeoff Levand 
182f58a9d17SGeoff Levand 	result = lv1_construct_virtual_address_space(CONFIG_PS3_HTAB_SIZE,
183f58a9d17SGeoff Levand 			2, make_page_sizes(PAGE_SHIFT_16M, PAGE_SHIFT_64K),
184f58a9d17SGeoff Levand 			&map.vas_id, &map.htab_size);
185f58a9d17SGeoff Levand 
186f58a9d17SGeoff Levand 	if (result) {
187f58a9d17SGeoff Levand 		DBG("%s:%d: lv1_construct_virtual_address_space failed: %s\n",
188f58a9d17SGeoff Levand 			__func__, __LINE__, ps3_result(result));
189f58a9d17SGeoff Levand 		goto fail;
190f58a9d17SGeoff Levand 	}
191f58a9d17SGeoff Levand 
192f58a9d17SGeoff Levand 	result = lv1_select_virtual_address_space(map.vas_id);
193f58a9d17SGeoff Levand 
194f58a9d17SGeoff Levand 	if (result) {
195f58a9d17SGeoff Levand 		DBG("%s:%d: lv1_select_virtual_address_space failed: %s\n",
196f58a9d17SGeoff Levand 			__func__, __LINE__, ps3_result(result));
197f58a9d17SGeoff Levand 		goto fail;
198f58a9d17SGeoff Levand 	}
199f58a9d17SGeoff Levand 
200f58a9d17SGeoff Levand 	*htab_size = map.htab_size;
201f58a9d17SGeoff Levand 
202f58a9d17SGeoff Levand 	debug_dump_map(&map);
203f58a9d17SGeoff Levand 
204f58a9d17SGeoff Levand 	return;
205f58a9d17SGeoff Levand 
206f58a9d17SGeoff Levand fail:
207f58a9d17SGeoff Levand 	panic("ps3_mm_vas_create failed");
208f58a9d17SGeoff Levand }
209f58a9d17SGeoff Levand 
210f58a9d17SGeoff Levand /**
211f58a9d17SGeoff Levand  * ps3_mm_vas_destroy -
212f58a9d17SGeoff Levand  */
213f58a9d17SGeoff Levand 
214f58a9d17SGeoff Levand void ps3_mm_vas_destroy(void)
215f58a9d17SGeoff Levand {
216*6bb5cf10SGeoff Levand 	int result;
217*6bb5cf10SGeoff Levand 
218*6bb5cf10SGeoff Levand 	DBG("%s:%d: map.vas_id    = %lu\n", __func__, __LINE__, map.vas_id);
219*6bb5cf10SGeoff Levand 
220f58a9d17SGeoff Levand 	if (map.vas_id) {
221*6bb5cf10SGeoff Levand 		result = lv1_select_virtual_address_space(0);
222*6bb5cf10SGeoff Levand 		BUG_ON(result);
223*6bb5cf10SGeoff Levand 		result = lv1_destruct_virtual_address_space(map.vas_id);
224*6bb5cf10SGeoff Levand 		BUG_ON(result);
225f58a9d17SGeoff Levand 		map.vas_id = 0;
226f58a9d17SGeoff Levand 	}
227f58a9d17SGeoff Levand }
228f58a9d17SGeoff Levand 
229f58a9d17SGeoff Levand /*============================================================================*/
230f58a9d17SGeoff Levand /* memory hotplug routines                                                    */
231f58a9d17SGeoff Levand /*============================================================================*/
232f58a9d17SGeoff Levand 
233f58a9d17SGeoff Levand /**
234f58a9d17SGeoff Levand  * ps3_mm_region_create - create a memory region in the vas
235f58a9d17SGeoff Levand  * @r: pointer to a struct mem_region to accept initialized values
236f58a9d17SGeoff Levand  * @size: requested region size
237f58a9d17SGeoff Levand  *
238f58a9d17SGeoff Levand  * This implementation creates the region with the vas large page size.
239f58a9d17SGeoff Levand  * @size is rounded down to a multiple of the vas large page size.
240f58a9d17SGeoff Levand  */
241f58a9d17SGeoff Levand 
242f58a9d17SGeoff Levand int ps3_mm_region_create(struct mem_region *r, unsigned long size)
243f58a9d17SGeoff Levand {
244f58a9d17SGeoff Levand 	int result;
245f58a9d17SGeoff Levand 	unsigned long muid;
246f58a9d17SGeoff Levand 
247f58a9d17SGeoff Levand 	r->size = _ALIGN_DOWN(size, 1 << PAGE_SHIFT_16M);
248f58a9d17SGeoff Levand 
249f58a9d17SGeoff Levand 	DBG("%s:%d requested  %lxh\n", __func__, __LINE__, size);
250f58a9d17SGeoff Levand 	DBG("%s:%d actual     %lxh\n", __func__, __LINE__, r->size);
251f58a9d17SGeoff Levand 	DBG("%s:%d difference %lxh (%luMB)\n", __func__, __LINE__,
252f58a9d17SGeoff Levand 		(unsigned long)(size - r->size),
253f58a9d17SGeoff Levand 		(size - r->size) / 1024 / 1024);
254f58a9d17SGeoff Levand 
255f58a9d17SGeoff Levand 	if (r->size == 0) {
256f58a9d17SGeoff Levand 		DBG("%s:%d: size == 0\n", __func__, __LINE__);
257f58a9d17SGeoff Levand 		result = -1;
258f58a9d17SGeoff Levand 		goto zero_region;
259f58a9d17SGeoff Levand 	}
260f58a9d17SGeoff Levand 
261f58a9d17SGeoff Levand 	result = lv1_allocate_memory(r->size, PAGE_SHIFT_16M, 0,
262f58a9d17SGeoff Levand 		ALLOCATE_MEMORY_TRY_ALT_UNIT, &r->base, &muid);
263f58a9d17SGeoff Levand 
264f58a9d17SGeoff Levand 	if (result || r->base < map.rm.size) {
265f58a9d17SGeoff Levand 		DBG("%s:%d: lv1_allocate_memory failed: %s\n",
266f58a9d17SGeoff Levand 			__func__, __LINE__, ps3_result(result));
267f58a9d17SGeoff Levand 		goto zero_region;
268f58a9d17SGeoff Levand 	}
269f58a9d17SGeoff Levand 
270f58a9d17SGeoff Levand 	r->offset = r->base - map.rm.size;
271f58a9d17SGeoff Levand 	return result;
272f58a9d17SGeoff Levand 
273f58a9d17SGeoff Levand zero_region:
274f58a9d17SGeoff Levand 	r->size = r->base = r->offset = 0;
275f58a9d17SGeoff Levand 	return result;
276f58a9d17SGeoff Levand }
277f58a9d17SGeoff Levand 
278f58a9d17SGeoff Levand /**
279f58a9d17SGeoff Levand  * ps3_mm_region_destroy - destroy a memory region
280f58a9d17SGeoff Levand  * @r: pointer to struct mem_region
281f58a9d17SGeoff Levand  */
282f58a9d17SGeoff Levand 
283f58a9d17SGeoff Levand void ps3_mm_region_destroy(struct mem_region *r)
284f58a9d17SGeoff Levand {
285*6bb5cf10SGeoff Levand 	int result;
286*6bb5cf10SGeoff Levand 
287*6bb5cf10SGeoff Levand 	DBG("%s:%d: r->base = %lxh\n", __func__, __LINE__, r->base);
288f58a9d17SGeoff Levand 	if (r->base) {
289*6bb5cf10SGeoff Levand 		result = lv1_release_memory(r->base);
290*6bb5cf10SGeoff Levand 		BUG_ON(result);
291f58a9d17SGeoff Levand 		r->size = r->base = r->offset = 0;
292f58a9d17SGeoff Levand 		map.total = map.rm.size;
293f58a9d17SGeoff Levand 	}
294f58a9d17SGeoff Levand }
295f58a9d17SGeoff Levand 
296f58a9d17SGeoff Levand /**
297f58a9d17SGeoff Levand  * ps3_mm_add_memory - hot add memory
298f58a9d17SGeoff Levand  */
299f58a9d17SGeoff Levand 
300f58a9d17SGeoff Levand static int __init ps3_mm_add_memory(void)
301f58a9d17SGeoff Levand {
302f58a9d17SGeoff Levand 	int result;
303f58a9d17SGeoff Levand 	unsigned long start_addr;
304f58a9d17SGeoff Levand 	unsigned long start_pfn;
305f58a9d17SGeoff Levand 	unsigned long nr_pages;
306f58a9d17SGeoff Levand 
307e22ba7e3SArnd Bergmann 	if (!firmware_has_feature(FW_FEATURE_PS3_LV1))
308ef596c69SGeert Uytterhoeven 		return -ENODEV;
309e22ba7e3SArnd Bergmann 
310f58a9d17SGeoff Levand 	BUG_ON(!mem_init_done);
311f58a9d17SGeoff Levand 
312f58a9d17SGeoff Levand 	start_addr = USE_LPAR_ADDR ? map.r1.base : map.rm.size;
313f58a9d17SGeoff Levand 	start_pfn = start_addr >> PAGE_SHIFT;
314f58a9d17SGeoff Levand 	nr_pages = (map.r1.size + PAGE_SIZE - 1) >> PAGE_SHIFT;
315f58a9d17SGeoff Levand 
316f58a9d17SGeoff Levand 	DBG("%s:%d: start_addr %lxh, start_pfn %lxh, nr_pages %lxh\n",
317f58a9d17SGeoff Levand 		__func__, __LINE__, start_addr, start_pfn, nr_pages);
318f58a9d17SGeoff Levand 
319f58a9d17SGeoff Levand 	result = add_memory(0, start_addr, map.r1.size);
320f58a9d17SGeoff Levand 
321f58a9d17SGeoff Levand 	if (result) {
322f58a9d17SGeoff Levand 		DBG("%s:%d: add_memory failed: (%d)\n",
323f58a9d17SGeoff Levand 			__func__, __LINE__, result);
324f58a9d17SGeoff Levand 		return result;
325f58a9d17SGeoff Levand 	}
326f58a9d17SGeoff Levand 
327f58a9d17SGeoff Levand 	result = online_pages(start_pfn, nr_pages);
328f58a9d17SGeoff Levand 
329f58a9d17SGeoff Levand 	if (result)
330f58a9d17SGeoff Levand 		DBG("%s:%d: online_pages failed: (%d)\n",
331f58a9d17SGeoff Levand 			__func__, __LINE__, result);
332f58a9d17SGeoff Levand 
333f58a9d17SGeoff Levand 	return result;
334f58a9d17SGeoff Levand }
335f58a9d17SGeoff Levand 
336f58a9d17SGeoff Levand core_initcall(ps3_mm_add_memory);
337f58a9d17SGeoff Levand 
338f58a9d17SGeoff Levand /*============================================================================*/
339f58a9d17SGeoff Levand /* dma routines                                                               */
340f58a9d17SGeoff Levand /*============================================================================*/
341f58a9d17SGeoff Levand 
342f58a9d17SGeoff Levand /**
343*6bb5cf10SGeoff Levand  * dma_sb_lpar_to_bus - Translate an lpar address to ioc mapped bus address.
344f58a9d17SGeoff Levand  * @r: pointer to dma region structure
345f58a9d17SGeoff Levand  * @lpar_addr: HV lpar address
346f58a9d17SGeoff Levand  */
347f58a9d17SGeoff Levand 
348*6bb5cf10SGeoff Levand static unsigned long dma_sb_lpar_to_bus(struct ps3_dma_region *r,
349f58a9d17SGeoff Levand 	unsigned long lpar_addr)
350f58a9d17SGeoff Levand {
351*6bb5cf10SGeoff Levand 	if (lpar_addr >= map.rm.size)
352*6bb5cf10SGeoff Levand 		lpar_addr -= map.r1.offset;
353*6bb5cf10SGeoff Levand 	BUG_ON(lpar_addr < r->offset);
354*6bb5cf10SGeoff Levand 	BUG_ON(lpar_addr >= r->offset + r->len);
355*6bb5cf10SGeoff Levand 	return r->bus_addr + lpar_addr - r->offset;
356f58a9d17SGeoff Levand }
357f58a9d17SGeoff Levand 
358f58a9d17SGeoff Levand #define dma_dump_region(_a) _dma_dump_region(_a, __func__, __LINE__)
359*6bb5cf10SGeoff Levand static void  __maybe_unused _dma_dump_region(const struct ps3_dma_region *r,
360*6bb5cf10SGeoff Levand 	const char *func, int line)
361f58a9d17SGeoff Levand {
362*6bb5cf10SGeoff Levand 	DBG("%s:%d: dev        %u:%u\n", func, line, r->dev->bus_id,
363*6bb5cf10SGeoff Levand 		r->dev->dev_id);
364f58a9d17SGeoff Levand 	DBG("%s:%d: page_size  %u\n", func, line, r->page_size);
365f58a9d17SGeoff Levand 	DBG("%s:%d: bus_addr   %lxh\n", func, line, r->bus_addr);
366f58a9d17SGeoff Levand 	DBG("%s:%d: len        %lxh\n", func, line, r->len);
367*6bb5cf10SGeoff Levand 	DBG("%s:%d: offset     %lxh\n", func, line, r->offset);
368f58a9d17SGeoff Levand }
369f58a9d17SGeoff Levand 
370f58a9d17SGeoff Levand   /**
371f58a9d17SGeoff Levand  * dma_chunk - A chunk of dma pages mapped by the io controller.
372f58a9d17SGeoff Levand  * @region - The dma region that owns this chunk.
373f58a9d17SGeoff Levand  * @lpar_addr: Starting lpar address of the area to map.
374f58a9d17SGeoff Levand  * @bus_addr: Starting ioc bus address of the area to map.
375f58a9d17SGeoff Levand  * @len: Length in bytes of the area to map.
376f58a9d17SGeoff Levand  * @link: A struct list_head used with struct ps3_dma_region.chunk_list, the
377f58a9d17SGeoff Levand  * list of all chuncks owned by the region.
378f58a9d17SGeoff Levand  *
379f58a9d17SGeoff Levand  * This implementation uses a very simple dma page manager
380f58a9d17SGeoff Levand  * based on the dma_chunk structure.  This scheme assumes
381f58a9d17SGeoff Levand  * that all drivers use very well behaved dma ops.
382f58a9d17SGeoff Levand  */
383f58a9d17SGeoff Levand 
384f58a9d17SGeoff Levand struct dma_chunk {
385f58a9d17SGeoff Levand 	struct ps3_dma_region *region;
386f58a9d17SGeoff Levand 	unsigned long lpar_addr;
387f58a9d17SGeoff Levand 	unsigned long bus_addr;
388f58a9d17SGeoff Levand 	unsigned long len;
389f58a9d17SGeoff Levand 	struct list_head link;
390f58a9d17SGeoff Levand 	unsigned int usage_count;
391f58a9d17SGeoff Levand };
392f58a9d17SGeoff Levand 
393f58a9d17SGeoff Levand #define dma_dump_chunk(_a) _dma_dump_chunk(_a, __func__, __LINE__)
394f58a9d17SGeoff Levand static void _dma_dump_chunk (const struct dma_chunk* c, const char* func,
395f58a9d17SGeoff Levand 	int line)
396f58a9d17SGeoff Levand {
397f58a9d17SGeoff Levand 	DBG("%s:%d: r.dev        %u:%u\n", func, line,
398*6bb5cf10SGeoff Levand 		c->region->dev->bus_id, c->region->dev->dev_id);
399f58a9d17SGeoff Levand 	DBG("%s:%d: r.bus_addr   %lxh\n", func, line, c->region->bus_addr);
400f58a9d17SGeoff Levand 	DBG("%s:%d: r.page_size  %u\n", func, line, c->region->page_size);
401f58a9d17SGeoff Levand 	DBG("%s:%d: r.len        %lxh\n", func, line, c->region->len);
402*6bb5cf10SGeoff Levand 	DBG("%s:%d: r.offset     %lxh\n", func, line, c->region->offset);
403f58a9d17SGeoff Levand 	DBG("%s:%d: c.lpar_addr  %lxh\n", func, line, c->lpar_addr);
404f58a9d17SGeoff Levand 	DBG("%s:%d: c.bus_addr   %lxh\n", func, line, c->bus_addr);
405f58a9d17SGeoff Levand 	DBG("%s:%d: c.len        %lxh\n", func, line, c->len);
406f58a9d17SGeoff Levand }
407f58a9d17SGeoff Levand 
408f58a9d17SGeoff Levand static struct dma_chunk * dma_find_chunk(struct ps3_dma_region *r,
409f58a9d17SGeoff Levand 	unsigned long bus_addr, unsigned long len)
410f58a9d17SGeoff Levand {
411f58a9d17SGeoff Levand 	struct dma_chunk *c;
412f58a9d17SGeoff Levand 	unsigned long aligned_bus = _ALIGN_DOWN(bus_addr, 1 << r->page_size);
413*6bb5cf10SGeoff Levand 	unsigned long aligned_len = _ALIGN_UP(len+bus_addr-aligned_bus,
414*6bb5cf10SGeoff Levand 					      1 << r->page_size);
415f58a9d17SGeoff Levand 
416f58a9d17SGeoff Levand 	list_for_each_entry(c, &r->chunk_list.head, link) {
417f58a9d17SGeoff Levand 		/* intersection */
418*6bb5cf10SGeoff Levand 		if (aligned_bus >= c->bus_addr &&
419*6bb5cf10SGeoff Levand 		    aligned_bus + aligned_len <= c->bus_addr + c->len)
420f58a9d17SGeoff Levand 			return c;
421*6bb5cf10SGeoff Levand 
422f58a9d17SGeoff Levand 		/* below */
423*6bb5cf10SGeoff Levand 		if (aligned_bus + aligned_len <= c->bus_addr)
424f58a9d17SGeoff Levand 			continue;
425*6bb5cf10SGeoff Levand 
426f58a9d17SGeoff Levand 		/* above */
427*6bb5cf10SGeoff Levand 		if (aligned_bus >= c->bus_addr + c->len)
428f58a9d17SGeoff Levand 			continue;
429f58a9d17SGeoff Levand 
430f58a9d17SGeoff Levand 		/* we don't handle the multi-chunk case for now */
431f58a9d17SGeoff Levand 		dma_dump_chunk(c);
432f58a9d17SGeoff Levand 		BUG();
433f58a9d17SGeoff Levand 	}
434f58a9d17SGeoff Levand 	return NULL;
435f58a9d17SGeoff Levand }
436f58a9d17SGeoff Levand 
437*6bb5cf10SGeoff Levand static struct dma_chunk *dma_find_chunk_lpar(struct ps3_dma_region *r,
438*6bb5cf10SGeoff Levand 	unsigned long lpar_addr, unsigned long len)
439*6bb5cf10SGeoff Levand {
440*6bb5cf10SGeoff Levand 	struct dma_chunk *c;
441*6bb5cf10SGeoff Levand 	unsigned long aligned_lpar = _ALIGN_DOWN(lpar_addr, 1 << r->page_size);
442*6bb5cf10SGeoff Levand 	unsigned long aligned_len = _ALIGN_UP(len + lpar_addr - aligned_lpar,
443*6bb5cf10SGeoff Levand 					      1 << r->page_size);
444*6bb5cf10SGeoff Levand 
445*6bb5cf10SGeoff Levand 	list_for_each_entry(c, &r->chunk_list.head, link) {
446*6bb5cf10SGeoff Levand 		/* intersection */
447*6bb5cf10SGeoff Levand 		if (c->lpar_addr <= aligned_lpar &&
448*6bb5cf10SGeoff Levand 		    aligned_lpar < c->lpar_addr + c->len) {
449*6bb5cf10SGeoff Levand 			if (aligned_lpar + aligned_len <= c->lpar_addr + c->len)
450*6bb5cf10SGeoff Levand 				return c;
451*6bb5cf10SGeoff Levand 			else {
452*6bb5cf10SGeoff Levand 				dma_dump_chunk(c);
453*6bb5cf10SGeoff Levand 				BUG();
454*6bb5cf10SGeoff Levand 			}
455*6bb5cf10SGeoff Levand 		}
456*6bb5cf10SGeoff Levand 		/* below */
457*6bb5cf10SGeoff Levand 		if (aligned_lpar + aligned_len <= c->lpar_addr) {
458*6bb5cf10SGeoff Levand 			continue;
459*6bb5cf10SGeoff Levand 		}
460*6bb5cf10SGeoff Levand 		/* above */
461*6bb5cf10SGeoff Levand 		if (c->lpar_addr + c->len <= aligned_lpar) {
462*6bb5cf10SGeoff Levand 			continue;
463*6bb5cf10SGeoff Levand 		}
464*6bb5cf10SGeoff Levand 	}
465*6bb5cf10SGeoff Levand 	return NULL;
466*6bb5cf10SGeoff Levand }
467*6bb5cf10SGeoff Levand 
468*6bb5cf10SGeoff Levand static int dma_sb_free_chunk(struct dma_chunk *c)
469f58a9d17SGeoff Levand {
470f58a9d17SGeoff Levand 	int result = 0;
471f58a9d17SGeoff Levand 
472f58a9d17SGeoff Levand 	if (c->bus_addr) {
473*6bb5cf10SGeoff Levand 		result = lv1_unmap_device_dma_region(c->region->dev->bus_id,
474*6bb5cf10SGeoff Levand 			c->region->dev->dev_id, c->bus_addr, c->len);
475f58a9d17SGeoff Levand 		BUG_ON(result);
476f58a9d17SGeoff Levand 	}
477f58a9d17SGeoff Levand 
478f58a9d17SGeoff Levand 	kfree(c);
479f58a9d17SGeoff Levand 	return result;
480f58a9d17SGeoff Levand }
481f58a9d17SGeoff Levand 
482*6bb5cf10SGeoff Levand static int dma_ioc0_free_chunk(struct dma_chunk *c)
483*6bb5cf10SGeoff Levand {
484*6bb5cf10SGeoff Levand 	int result = 0;
485*6bb5cf10SGeoff Levand 	int iopage;
486*6bb5cf10SGeoff Levand 	unsigned long offset;
487*6bb5cf10SGeoff Levand 	struct ps3_dma_region *r = c->region;
488*6bb5cf10SGeoff Levand 
489*6bb5cf10SGeoff Levand 	DBG("%s:start\n", __func__);
490*6bb5cf10SGeoff Levand 	for (iopage = 0; iopage < (c->len >> r->page_size); iopage++) {
491*6bb5cf10SGeoff Levand 		offset = (1 << r->page_size) * iopage;
492*6bb5cf10SGeoff Levand 		/* put INVALID entry */
493*6bb5cf10SGeoff Levand 		result = lv1_put_iopte(0,
494*6bb5cf10SGeoff Levand 				       c->bus_addr + offset,
495*6bb5cf10SGeoff Levand 				       c->lpar_addr + offset,
496*6bb5cf10SGeoff Levand 				       r->ioid,
497*6bb5cf10SGeoff Levand 				       0);
498*6bb5cf10SGeoff Levand 		DBG("%s: bus=%#lx, lpar=%#lx, ioid=%d\n", __func__,
499*6bb5cf10SGeoff Levand 		    c->bus_addr + offset,
500*6bb5cf10SGeoff Levand 		    c->lpar_addr + offset,
501*6bb5cf10SGeoff Levand 		    r->ioid);
502*6bb5cf10SGeoff Levand 
503*6bb5cf10SGeoff Levand 		if (result) {
504*6bb5cf10SGeoff Levand 			DBG("%s:%d: lv1_put_iopte failed: %s\n", __func__,
505*6bb5cf10SGeoff Levand 			    __LINE__, ps3_result(result));
506*6bb5cf10SGeoff Levand 		}
507*6bb5cf10SGeoff Levand 	}
508*6bb5cf10SGeoff Levand 	kfree(c);
509*6bb5cf10SGeoff Levand 	DBG("%s:end\n", __func__);
510*6bb5cf10SGeoff Levand 	return result;
511*6bb5cf10SGeoff Levand }
512*6bb5cf10SGeoff Levand 
513f58a9d17SGeoff Levand /**
514*6bb5cf10SGeoff Levand  * dma_sb_map_pages - Maps dma pages into the io controller bus address space.
515f58a9d17SGeoff Levand  * @r: Pointer to a struct ps3_dma_region.
516f58a9d17SGeoff Levand  * @phys_addr: Starting physical address of the area to map.
517f58a9d17SGeoff Levand  * @len: Length in bytes of the area to map.
518f58a9d17SGeoff Levand  * c_out: A pointer to receive an allocated struct dma_chunk for this area.
519f58a9d17SGeoff Levand  *
520f58a9d17SGeoff Levand  * This is the lowest level dma mapping routine, and is the one that will
521f58a9d17SGeoff Levand  * make the HV call to add the pages into the io controller address space.
522f58a9d17SGeoff Levand  */
523f58a9d17SGeoff Levand 
524*6bb5cf10SGeoff Levand static int dma_sb_map_pages(struct ps3_dma_region *r, unsigned long phys_addr,
525*6bb5cf10SGeoff Levand 	    unsigned long len, struct dma_chunk **c_out, u64 iopte_flag)
526f58a9d17SGeoff Levand {
527f58a9d17SGeoff Levand 	int result;
528f58a9d17SGeoff Levand 	struct dma_chunk *c;
529f58a9d17SGeoff Levand 
530f58a9d17SGeoff Levand 	c = kzalloc(sizeof(struct dma_chunk), GFP_ATOMIC);
531f58a9d17SGeoff Levand 
532f58a9d17SGeoff Levand 	if (!c) {
533f58a9d17SGeoff Levand 		result = -ENOMEM;
534f58a9d17SGeoff Levand 		goto fail_alloc;
535f58a9d17SGeoff Levand 	}
536f58a9d17SGeoff Levand 
537f58a9d17SGeoff Levand 	c->region = r;
538f58a9d17SGeoff Levand 	c->lpar_addr = ps3_mm_phys_to_lpar(phys_addr);
539*6bb5cf10SGeoff Levand 	c->bus_addr = dma_sb_lpar_to_bus(r, c->lpar_addr);
540f58a9d17SGeoff Levand 	c->len = len;
541f58a9d17SGeoff Levand 
542*6bb5cf10SGeoff Levand 	BUG_ON(iopte_flag != 0xf800000000000000UL);
543*6bb5cf10SGeoff Levand 	result = lv1_map_device_dma_region(c->region->dev->bus_id,
544*6bb5cf10SGeoff Levand 					   c->region->dev->dev_id, c->lpar_addr,
545*6bb5cf10SGeoff Levand 					   c->bus_addr, c->len, iopte_flag);
546f58a9d17SGeoff Levand 	if (result) {
547f58a9d17SGeoff Levand 		DBG("%s:%d: lv1_map_device_dma_region failed: %s\n",
548f58a9d17SGeoff Levand 			__func__, __LINE__, ps3_result(result));
549f58a9d17SGeoff Levand 		goto fail_map;
550f58a9d17SGeoff Levand 	}
551f58a9d17SGeoff Levand 
552f58a9d17SGeoff Levand 	list_add(&c->link, &r->chunk_list.head);
553f58a9d17SGeoff Levand 
554f58a9d17SGeoff Levand 	*c_out = c;
555f58a9d17SGeoff Levand 	return 0;
556f58a9d17SGeoff Levand 
557f58a9d17SGeoff Levand fail_map:
558f58a9d17SGeoff Levand 	kfree(c);
559f58a9d17SGeoff Levand fail_alloc:
560f58a9d17SGeoff Levand 	*c_out = NULL;
561f58a9d17SGeoff Levand 	DBG(" <- %s:%d\n", __func__, __LINE__);
562f58a9d17SGeoff Levand 	return result;
563f58a9d17SGeoff Levand }
564f58a9d17SGeoff Levand 
565*6bb5cf10SGeoff Levand static int dma_ioc0_map_pages(struct ps3_dma_region *r, unsigned long phys_addr,
566*6bb5cf10SGeoff Levand 			      unsigned long len, struct dma_chunk **c_out,
567*6bb5cf10SGeoff Levand 			      u64 iopte_flag)
568*6bb5cf10SGeoff Levand {
569*6bb5cf10SGeoff Levand 	int result;
570*6bb5cf10SGeoff Levand 	struct dma_chunk *c, *last;
571*6bb5cf10SGeoff Levand 	int iopage, pages;
572*6bb5cf10SGeoff Levand 	unsigned long offset;
573*6bb5cf10SGeoff Levand 
574*6bb5cf10SGeoff Levand 	DBG(KERN_ERR "%s: phy=%#lx, lpar%#lx, len=%#lx\n", __func__,
575*6bb5cf10SGeoff Levand 	    phys_addr, ps3_mm_phys_to_lpar(phys_addr), len);
576*6bb5cf10SGeoff Levand 	c = kzalloc(sizeof(struct dma_chunk), GFP_ATOMIC);
577*6bb5cf10SGeoff Levand 
578*6bb5cf10SGeoff Levand 	if (!c) {
579*6bb5cf10SGeoff Levand 		result = -ENOMEM;
580*6bb5cf10SGeoff Levand 		goto fail_alloc;
581*6bb5cf10SGeoff Levand 	}
582*6bb5cf10SGeoff Levand 
583*6bb5cf10SGeoff Levand 	c->region = r;
584*6bb5cf10SGeoff Levand 	c->len = len;
585*6bb5cf10SGeoff Levand 	c->lpar_addr = ps3_mm_phys_to_lpar(phys_addr);
586*6bb5cf10SGeoff Levand 	/* allocate IO address */
587*6bb5cf10SGeoff Levand 	if (list_empty(&r->chunk_list.head)) {
588*6bb5cf10SGeoff Levand 		/* first one */
589*6bb5cf10SGeoff Levand 		c->bus_addr = r->bus_addr;
590*6bb5cf10SGeoff Levand 	} else {
591*6bb5cf10SGeoff Levand 		/* derive from last bus addr*/
592*6bb5cf10SGeoff Levand 		last  = list_entry(r->chunk_list.head.next,
593*6bb5cf10SGeoff Levand 				   struct dma_chunk, link);
594*6bb5cf10SGeoff Levand 		c->bus_addr = last->bus_addr + last->len;
595*6bb5cf10SGeoff Levand 		DBG("%s: last bus=%#lx, len=%#lx\n", __func__,
596*6bb5cf10SGeoff Levand 		    last->bus_addr, last->len);
597*6bb5cf10SGeoff Levand 	}
598*6bb5cf10SGeoff Levand 
599*6bb5cf10SGeoff Levand 	/* FIXME: check whether length exceeds region size */
600*6bb5cf10SGeoff Levand 
601*6bb5cf10SGeoff Levand 	/* build ioptes for the area */
602*6bb5cf10SGeoff Levand 	pages = len >> r->page_size;
603*6bb5cf10SGeoff Levand 	DBG("%s: pgsize=%#x len=%#lx pages=%#x iopteflag=%#lx\n", __func__,
604*6bb5cf10SGeoff Levand 	    r->page_size, r->len, pages, iopte_flag);
605*6bb5cf10SGeoff Levand 	for (iopage = 0; iopage < pages; iopage++) {
606*6bb5cf10SGeoff Levand 		offset = (1 << r->page_size) * iopage;
607*6bb5cf10SGeoff Levand 		result = lv1_put_iopte(0,
608*6bb5cf10SGeoff Levand 				       c->bus_addr + offset,
609*6bb5cf10SGeoff Levand 				       c->lpar_addr + offset,
610*6bb5cf10SGeoff Levand 				       r->ioid,
611*6bb5cf10SGeoff Levand 				       iopte_flag);
612*6bb5cf10SGeoff Levand 		if (result) {
613*6bb5cf10SGeoff Levand 			printk(KERN_WARNING "%s:%d: lv1_map_device_dma_region "
614*6bb5cf10SGeoff Levand 				"failed: %s\n", __func__, __LINE__,
615*6bb5cf10SGeoff Levand 				ps3_result(result));
616*6bb5cf10SGeoff Levand 			goto fail_map;
617*6bb5cf10SGeoff Levand 		}
618*6bb5cf10SGeoff Levand 		DBG("%s: pg=%d bus=%#lx, lpar=%#lx, ioid=%#x\n", __func__,
619*6bb5cf10SGeoff Levand 		    iopage, c->bus_addr + offset, c->lpar_addr + offset,
620*6bb5cf10SGeoff Levand 		    r->ioid);
621*6bb5cf10SGeoff Levand 	}
622*6bb5cf10SGeoff Levand 
623*6bb5cf10SGeoff Levand 	/* be sure that last allocated one is inserted at head */
624*6bb5cf10SGeoff Levand 	list_add(&c->link, &r->chunk_list.head);
625*6bb5cf10SGeoff Levand 
626*6bb5cf10SGeoff Levand 	*c_out = c;
627*6bb5cf10SGeoff Levand 	DBG("%s: end\n", __func__);
628*6bb5cf10SGeoff Levand 	return 0;
629*6bb5cf10SGeoff Levand 
630*6bb5cf10SGeoff Levand fail_map:
631*6bb5cf10SGeoff Levand 	for (iopage--; 0 <= iopage; iopage--) {
632*6bb5cf10SGeoff Levand 		lv1_put_iopte(0,
633*6bb5cf10SGeoff Levand 			      c->bus_addr + offset,
634*6bb5cf10SGeoff Levand 			      c->lpar_addr + offset,
635*6bb5cf10SGeoff Levand 			      r->ioid,
636*6bb5cf10SGeoff Levand 			      0);
637*6bb5cf10SGeoff Levand 	}
638*6bb5cf10SGeoff Levand 	kfree(c);
639*6bb5cf10SGeoff Levand fail_alloc:
640*6bb5cf10SGeoff Levand 	*c_out = NULL;
641*6bb5cf10SGeoff Levand 	return result;
642*6bb5cf10SGeoff Levand }
643*6bb5cf10SGeoff Levand 
644f58a9d17SGeoff Levand /**
645*6bb5cf10SGeoff Levand  * dma_sb_region_create - Create a device dma region.
646f58a9d17SGeoff Levand  * @r: Pointer to a struct ps3_dma_region.
647f58a9d17SGeoff Levand  *
648f58a9d17SGeoff Levand  * This is the lowest level dma region create routine, and is the one that
649f58a9d17SGeoff Levand  * will make the HV call to create the region.
650f58a9d17SGeoff Levand  */
651f58a9d17SGeoff Levand 
652*6bb5cf10SGeoff Levand static int dma_sb_region_create(struct ps3_dma_region *r)
653f58a9d17SGeoff Levand {
654f58a9d17SGeoff Levand 	int result;
655f58a9d17SGeoff Levand 
656*6bb5cf10SGeoff Levand 	pr_info(" -> %s:%d:\n", __func__, __LINE__);
657*6bb5cf10SGeoff Levand 
658*6bb5cf10SGeoff Levand 	BUG_ON(!r);
659*6bb5cf10SGeoff Levand 
660*6bb5cf10SGeoff Levand 	if (!r->dev->bus_id) {
661*6bb5cf10SGeoff Levand 		pr_info("%s:%d: %u:%u no dma\n", __func__, __LINE__,
662*6bb5cf10SGeoff Levand 			r->dev->bus_id, r->dev->dev_id);
663*6bb5cf10SGeoff Levand 		return 0;
664*6bb5cf10SGeoff Levand 	}
665*6bb5cf10SGeoff Levand 
666*6bb5cf10SGeoff Levand 	DBG("%s:%u: len = 0x%lx, page_size = %u, offset = 0x%lx\n", __func__,
667*6bb5cf10SGeoff Levand 	    __LINE__, r->len, r->page_size, r->offset);
668*6bb5cf10SGeoff Levand 
669*6bb5cf10SGeoff Levand 	BUG_ON(!r->len);
670*6bb5cf10SGeoff Levand 	BUG_ON(!r->page_size);
671*6bb5cf10SGeoff Levand 	BUG_ON(!r->region_ops);
672*6bb5cf10SGeoff Levand 
673f58a9d17SGeoff Levand 	INIT_LIST_HEAD(&r->chunk_list.head);
674f58a9d17SGeoff Levand 	spin_lock_init(&r->chunk_list.lock);
675f58a9d17SGeoff Levand 
676*6bb5cf10SGeoff Levand 	result = lv1_allocate_device_dma_region(r->dev->bus_id, r->dev->dev_id,
677*6bb5cf10SGeoff Levand 		roundup_pow_of_two(r->len), r->page_size, r->region_type,
678*6bb5cf10SGeoff Levand 		&r->bus_addr);
679f58a9d17SGeoff Levand 
680f58a9d17SGeoff Levand 	if (result) {
681f58a9d17SGeoff Levand 		DBG("%s:%d: lv1_allocate_device_dma_region failed: %s\n",
682f58a9d17SGeoff Levand 			__func__, __LINE__, ps3_result(result));
683f58a9d17SGeoff Levand 		r->len = r->bus_addr = 0;
684f58a9d17SGeoff Levand 	}
685f58a9d17SGeoff Levand 
686f58a9d17SGeoff Levand 	return result;
687f58a9d17SGeoff Levand }
688f58a9d17SGeoff Levand 
689*6bb5cf10SGeoff Levand static int dma_ioc0_region_create(struct ps3_dma_region *r)
690*6bb5cf10SGeoff Levand {
691*6bb5cf10SGeoff Levand 	int result;
692*6bb5cf10SGeoff Levand 
693*6bb5cf10SGeoff Levand 	INIT_LIST_HEAD(&r->chunk_list.head);
694*6bb5cf10SGeoff Levand 	spin_lock_init(&r->chunk_list.lock);
695*6bb5cf10SGeoff Levand 
696*6bb5cf10SGeoff Levand 	result = lv1_allocate_io_segment(0,
697*6bb5cf10SGeoff Levand 					 r->len,
698*6bb5cf10SGeoff Levand 					 r->page_size,
699*6bb5cf10SGeoff Levand 					 &r->bus_addr);
700*6bb5cf10SGeoff Levand 	if (result) {
701*6bb5cf10SGeoff Levand 		DBG("%s:%d: lv1_allocate_io_segment failed: %s\n",
702*6bb5cf10SGeoff Levand 			__func__, __LINE__, ps3_result(result));
703*6bb5cf10SGeoff Levand 		r->len = r->bus_addr = 0;
704*6bb5cf10SGeoff Levand 	}
705*6bb5cf10SGeoff Levand 	DBG("%s: len=%#lx, pg=%d, bus=%#lx\n", __func__,
706*6bb5cf10SGeoff Levand 	    r->len, r->page_size, r->bus_addr);
707*6bb5cf10SGeoff Levand 	return result;
708*6bb5cf10SGeoff Levand }
709*6bb5cf10SGeoff 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 
718*6bb5cf10SGeoff 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 
724*6bb5cf10SGeoff Levand 	BUG_ON(!r);
725*6bb5cf10SGeoff Levand 
726*6bb5cf10SGeoff Levand 	if (!r->dev->bus_id) {
727*6bb5cf10SGeoff Levand 		pr_info("%s:%d: %u:%u no dma\n", __func__, __LINE__,
728*6bb5cf10SGeoff Levand 			r->dev->bus_id, r->dev->dev_id);
729*6bb5cf10SGeoff Levand 		return 0;
730f58a9d17SGeoff Levand 	}
731f58a9d17SGeoff Levand 
732*6bb5cf10SGeoff Levand 	list_for_each_entry_safe(c, tmp, &r->chunk_list.head, link) {
733*6bb5cf10SGeoff Levand 		list_del(&c->link);
734*6bb5cf10SGeoff Levand 		dma_sb_free_chunk(c);
735*6bb5cf10SGeoff Levand 	}
736*6bb5cf10SGeoff Levand 
737*6bb5cf10SGeoff 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 
744*6bb5cf10SGeoff Levand 	r->bus_addr = 0;
745*6bb5cf10SGeoff Levand 
746*6bb5cf10SGeoff Levand 	return result;
747*6bb5cf10SGeoff Levand }
748*6bb5cf10SGeoff Levand 
749*6bb5cf10SGeoff Levand static int dma_ioc0_region_free(struct ps3_dma_region *r)
750*6bb5cf10SGeoff Levand {
751*6bb5cf10SGeoff Levand 	int result;
752*6bb5cf10SGeoff Levand 	struct dma_chunk *c, *n;
753*6bb5cf10SGeoff Levand 
754*6bb5cf10SGeoff Levand 	DBG("%s: start\n", __func__);
755*6bb5cf10SGeoff Levand 	list_for_each_entry_safe(c, n, &r->chunk_list.head, link) {
756*6bb5cf10SGeoff Levand 		list_del(&c->link);
757*6bb5cf10SGeoff Levand 		dma_ioc0_free_chunk(c);
758*6bb5cf10SGeoff Levand 	}
759*6bb5cf10SGeoff Levand 
760*6bb5cf10SGeoff Levand 	result = lv1_release_io_segment(0, r->bus_addr);
761*6bb5cf10SGeoff Levand 
762*6bb5cf10SGeoff Levand 	if (result)
763*6bb5cf10SGeoff Levand 		DBG("%s:%d: lv1_free_device_dma_region failed: %s\n",
764*6bb5cf10SGeoff Levand 			__func__, __LINE__, ps3_result(result));
765*6bb5cf10SGeoff Levand 
766*6bb5cf10SGeoff Levand 	r->bus_addr = 0;
767*6bb5cf10SGeoff Levand 	DBG("%s: end\n", __func__);
768f58a9d17SGeoff Levand 
769f58a9d17SGeoff Levand 	return result;
770f58a9d17SGeoff Levand }
771f58a9d17SGeoff Levand 
772f58a9d17SGeoff Levand /**
773*6bb5cf10SGeoff 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 
783*6bb5cf10SGeoff Levand static int dma_sb_map_area(struct ps3_dma_region *r, unsigned long virt_addr,
784*6bb5cf10SGeoff Levand 	   unsigned long len, unsigned long *bus_addr,
785*6bb5cf10SGeoff 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;
792*6bb5cf10SGeoff Levand 	unsigned long aligned_phys = _ALIGN_DOWN(phys_addr, 1 << r->page_size);
793*6bb5cf10SGeoff Levand 	unsigned long aligned_len = _ALIGN_UP(len + phys_addr - aligned_phys,
794*6bb5cf10SGeoff Levand 					      1 << r->page_size);
795*6bb5cf10SGeoff 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);
807f58a9d17SGeoff Levand 		DBG("%s:%d bus_addr  %lxh (%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) {
815*6bb5cf10SGeoff Levand 		DBG("%s:%d: reusing mapped chunk", __func__, __LINE__);
816*6bb5cf10SGeoff 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 
822*6bb5cf10SGeoff 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;
826*6bb5cf10SGeoff 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 
838*6bb5cf10SGeoff Levand static int dma_ioc0_map_area(struct ps3_dma_region *r, unsigned long virt_addr,
839*6bb5cf10SGeoff Levand 	     unsigned long len, unsigned long *bus_addr,
840*6bb5cf10SGeoff Levand 	     u64 iopte_flag)
841*6bb5cf10SGeoff Levand {
842*6bb5cf10SGeoff Levand 	int result;
843*6bb5cf10SGeoff Levand 	unsigned long flags;
844*6bb5cf10SGeoff Levand 	struct dma_chunk *c;
845*6bb5cf10SGeoff Levand 	unsigned long phys_addr = is_kernel_addr(virt_addr) ? __pa(virt_addr)
846*6bb5cf10SGeoff Levand 		: virt_addr;
847*6bb5cf10SGeoff Levand 	unsigned long aligned_phys = _ALIGN_DOWN(phys_addr, 1 << r->page_size);
848*6bb5cf10SGeoff Levand 	unsigned long aligned_len = _ALIGN_UP(len + phys_addr - aligned_phys,
849*6bb5cf10SGeoff Levand 					      1 << r->page_size);
850*6bb5cf10SGeoff Levand 
851*6bb5cf10SGeoff Levand 	DBG(KERN_ERR "%s: vaddr=%#lx, len=%#lx\n", __func__,
852*6bb5cf10SGeoff Levand 	    virt_addr, len);
853*6bb5cf10SGeoff Levand 	DBG(KERN_ERR "%s: ph=%#lx a_ph=%#lx a_l=%#lx\n", __func__,
854*6bb5cf10SGeoff Levand 	    phys_addr, aligned_phys, aligned_len);
855*6bb5cf10SGeoff Levand 
856*6bb5cf10SGeoff Levand 	spin_lock_irqsave(&r->chunk_list.lock, flags);
857*6bb5cf10SGeoff Levand 	c = dma_find_chunk_lpar(r, ps3_mm_phys_to_lpar(phys_addr), len);
858*6bb5cf10SGeoff Levand 
859*6bb5cf10SGeoff Levand 	if (c) {
860*6bb5cf10SGeoff Levand 		/* FIXME */
861*6bb5cf10SGeoff Levand 		BUG();
862*6bb5cf10SGeoff Levand 		*bus_addr = c->bus_addr + phys_addr - aligned_phys;
863*6bb5cf10SGeoff Levand 		c->usage_count++;
864*6bb5cf10SGeoff Levand 		spin_unlock_irqrestore(&r->chunk_list.lock, flags);
865*6bb5cf10SGeoff Levand 		return 0;
866*6bb5cf10SGeoff Levand 	}
867*6bb5cf10SGeoff Levand 
868*6bb5cf10SGeoff Levand 	result = dma_ioc0_map_pages(r, aligned_phys, aligned_len, &c,
869*6bb5cf10SGeoff Levand 				    iopte_flag);
870*6bb5cf10SGeoff Levand 
871*6bb5cf10SGeoff Levand 	if (result) {
872*6bb5cf10SGeoff Levand 		*bus_addr = 0;
873*6bb5cf10SGeoff Levand 		DBG("%s:%d: dma_ioc0_map_pages failed (%d)\n",
874*6bb5cf10SGeoff Levand 			__func__, __LINE__, result);
875*6bb5cf10SGeoff Levand 		spin_unlock_irqrestore(&r->chunk_list.lock, flags);
876*6bb5cf10SGeoff Levand 		return result;
877*6bb5cf10SGeoff Levand 	}
878*6bb5cf10SGeoff Levand 	*bus_addr = c->bus_addr + phys_addr - aligned_phys;
879*6bb5cf10SGeoff Levand 	DBG("%s: va=%#lx pa=%#lx a_pa=%#lx bus=%#lx\n", __func__,
880*6bb5cf10SGeoff Levand 	    virt_addr, phys_addr, aligned_phys, *bus_addr);
881*6bb5cf10SGeoff Levand 	c->usage_count = 1;
882*6bb5cf10SGeoff Levand 
883*6bb5cf10SGeoff Levand 	spin_unlock_irqrestore(&r->chunk_list.lock, flags);
884*6bb5cf10SGeoff Levand 	return result;
885*6bb5cf10SGeoff Levand }
886*6bb5cf10SGeoff Levand 
887f58a9d17SGeoff Levand /**
888*6bb5cf10SGeoff 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 
896*6bb5cf10SGeoff Levand int dma_sb_unmap_area(struct ps3_dma_region *r, unsigned long 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);
908*6bb5cf10SGeoff Levand 		unsigned long aligned_len = _ALIGN_UP(len + bus_addr
909*6bb5cf10SGeoff Levand 			- aligned_bus, 1 << r->page_size);
910f58a9d17SGeoff Levand 		DBG("%s:%d: not found: bus_addr %lxh\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);
925*6bb5cf10SGeoff 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 
932*6bb5cf10SGeoff Levand int dma_ioc0_unmap_area(struct ps3_dma_region *r, unsigned long bus_addr,
933*6bb5cf10SGeoff Levand 			unsigned long len)
934*6bb5cf10SGeoff Levand {
935*6bb5cf10SGeoff Levand 	unsigned long flags;
936*6bb5cf10SGeoff Levand 	struct dma_chunk *c;
937*6bb5cf10SGeoff Levand 
938*6bb5cf10SGeoff Levand 	DBG("%s: start a=%#lx l=%#lx\n", __func__, bus_addr, len);
939*6bb5cf10SGeoff Levand 	spin_lock_irqsave(&r->chunk_list.lock, flags);
940*6bb5cf10SGeoff Levand 	c = dma_find_chunk(r, bus_addr, len);
941*6bb5cf10SGeoff Levand 
942*6bb5cf10SGeoff Levand 	if (!c) {
943*6bb5cf10SGeoff Levand 		unsigned long aligned_bus = _ALIGN_DOWN(bus_addr,
944*6bb5cf10SGeoff Levand 							1 << r->page_size);
945*6bb5cf10SGeoff Levand 		unsigned long aligned_len = _ALIGN_UP(len + bus_addr
946*6bb5cf10SGeoff Levand 						      - aligned_bus,
947*6bb5cf10SGeoff Levand 						      1 << r->page_size);
948*6bb5cf10SGeoff Levand 		DBG("%s:%d: not found: bus_addr %lxh\n",
949*6bb5cf10SGeoff Levand 		    __func__, __LINE__, bus_addr);
950*6bb5cf10SGeoff Levand 		DBG("%s:%d: not found: len %lxh\n",
951*6bb5cf10SGeoff Levand 		    __func__, __LINE__, len);
952*6bb5cf10SGeoff Levand 		DBG("%s:%d: not found: aligned_bus %lxh\n",
953*6bb5cf10SGeoff Levand 		    __func__, __LINE__, aligned_bus);
954*6bb5cf10SGeoff Levand 		DBG("%s:%d: not found: aligned_len %lxh\n",
955*6bb5cf10SGeoff Levand 		    __func__, __LINE__, aligned_len);
956*6bb5cf10SGeoff Levand 		BUG();
957*6bb5cf10SGeoff Levand 	}
958*6bb5cf10SGeoff Levand 
959*6bb5cf10SGeoff Levand 	c->usage_count--;
960*6bb5cf10SGeoff Levand 
961*6bb5cf10SGeoff Levand 	if (!c->usage_count) {
962*6bb5cf10SGeoff Levand 		list_del(&c->link);
963*6bb5cf10SGeoff Levand 		dma_ioc0_free_chunk(c);
964*6bb5cf10SGeoff Levand 	}
965*6bb5cf10SGeoff Levand 
966*6bb5cf10SGeoff Levand 	spin_unlock_irqrestore(&r->chunk_list.lock, flags);
967*6bb5cf10SGeoff Levand 	DBG("%s: end\n", __func__);
968*6bb5cf10SGeoff Levand 	return 0;
969*6bb5cf10SGeoff Levand }
970*6bb5cf10SGeoff Levand 
971f58a9d17SGeoff Levand /**
972*6bb5cf10SGeoff 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 
979*6bb5cf10SGeoff Levand static int dma_sb_region_create_linear(struct ps3_dma_region *r)
980f58a9d17SGeoff Levand {
981f58a9d17SGeoff Levand 	int result;
982*6bb5cf10SGeoff Levand 	unsigned long virt_addr, len, tmp;
983f58a9d17SGeoff Levand 
984*6bb5cf10SGeoff Levand 	if (r->len > 16*1024*1024) {	/* FIXME: need proper fix */
985f58a9d17SGeoff Levand 		/* force 16M dma pages for linear mapping */
986f58a9d17SGeoff Levand 		if (r->page_size != PS3_DMA_16M) {
987f58a9d17SGeoff Levand 			pr_info("%s:%d: forcing 16M pages for linear map\n",
988f58a9d17SGeoff Levand 				__func__, __LINE__);
989f58a9d17SGeoff Levand 			r->page_size = PS3_DMA_16M;
990*6bb5cf10SGeoff Levand 			r->len = _ALIGN_UP(r->len, 1 << r->page_size);
991*6bb5cf10SGeoff Levand 		}
992f58a9d17SGeoff Levand 	}
993f58a9d17SGeoff Levand 
994*6bb5cf10SGeoff Levand 	result = dma_sb_region_create(r);
995f58a9d17SGeoff Levand 	BUG_ON(result);
996f58a9d17SGeoff Levand 
997*6bb5cf10SGeoff Levand 	if (r->offset < map.rm.size) {
998*6bb5cf10SGeoff Levand 		/* Map (part of) 1st RAM chunk */
999*6bb5cf10SGeoff Levand 		virt_addr = map.rm.base + r->offset;
1000*6bb5cf10SGeoff Levand 		len = map.rm.size - r->offset;
1001*6bb5cf10SGeoff Levand 		if (len > r->len)
1002*6bb5cf10SGeoff Levand 			len = r->len;
1003*6bb5cf10SGeoff Levand 		result = dma_sb_map_area(r, virt_addr, len, &tmp,
1004*6bb5cf10SGeoff Levand 			IOPTE_PP_W | IOPTE_PP_R | IOPTE_SO_RW | IOPTE_M);
1005f58a9d17SGeoff Levand 		BUG_ON(result);
1006*6bb5cf10SGeoff Levand 	}
1007f58a9d17SGeoff Levand 
1008*6bb5cf10SGeoff Levand 	if (r->offset + r->len > map.rm.size) {
1009*6bb5cf10SGeoff Levand 		/* Map (part of) 2nd RAM chunk */
1010*6bb5cf10SGeoff Levand 		virt_addr = USE_LPAR_ADDR ? map.r1.base : map.rm.size;
1011*6bb5cf10SGeoff Levand 		len = r->len;
1012*6bb5cf10SGeoff Levand 		if (r->offset >= map.rm.size)
1013*6bb5cf10SGeoff Levand 			virt_addr += r->offset - map.rm.size;
1014f58a9d17SGeoff Levand 		else
1015*6bb5cf10SGeoff Levand 			len -= map.rm.size - r->offset;
1016*6bb5cf10SGeoff Levand 		result = dma_sb_map_area(r, virt_addr, len, &tmp,
1017*6bb5cf10SGeoff Levand 			IOPTE_PP_W | IOPTE_PP_R | IOPTE_SO_RW | IOPTE_M);
1018f58a9d17SGeoff Levand 		BUG_ON(result);
1019*6bb5cf10SGeoff Levand 	}
1020f58a9d17SGeoff Levand 
1021f58a9d17SGeoff Levand 	return result;
1022f58a9d17SGeoff Levand }
1023f58a9d17SGeoff Levand 
1024f58a9d17SGeoff Levand /**
1025*6bb5cf10SGeoff Levand  * dma_sb_region_free_linear - Free a linear dma mapping for a device.
1026f58a9d17SGeoff Levand  * @r: Pointer to a struct ps3_dma_region.
1027f58a9d17SGeoff Levand  *
1028f58a9d17SGeoff Levand  * This routine will unmap all mapped areas and free the HV dma region.
1029f58a9d17SGeoff Levand  */
1030f58a9d17SGeoff Levand 
1031*6bb5cf10SGeoff Levand static int dma_sb_region_free_linear(struct ps3_dma_region *r)
1032f58a9d17SGeoff Levand {
1033f58a9d17SGeoff Levand 	int result;
1034*6bb5cf10SGeoff Levand 	unsigned long bus_addr, len, lpar_addr;
1035f58a9d17SGeoff Levand 
1036*6bb5cf10SGeoff Levand 	if (r->offset < map.rm.size) {
1037*6bb5cf10SGeoff Levand 		/* Unmap (part of) 1st RAM chunk */
1038*6bb5cf10SGeoff Levand 		lpar_addr = map.rm.base + r->offset;
1039*6bb5cf10SGeoff Levand 		len = map.rm.size - r->offset;
1040*6bb5cf10SGeoff Levand 		if (len > r->len)
1041*6bb5cf10SGeoff Levand 			len = r->len;
1042*6bb5cf10SGeoff Levand 		bus_addr = dma_sb_lpar_to_bus(r, lpar_addr);
1043*6bb5cf10SGeoff Levand 		result = dma_sb_unmap_area(r, bus_addr, len);
1044f58a9d17SGeoff Levand 		BUG_ON(result);
1045*6bb5cf10SGeoff Levand 	}
1046f58a9d17SGeoff Levand 
1047*6bb5cf10SGeoff Levand 	if (r->offset + r->len > map.rm.size) {
1048*6bb5cf10SGeoff Levand 		/* Unmap (part of) 2nd RAM chunk */
1049*6bb5cf10SGeoff Levand 		lpar_addr = map.r1.base;
1050*6bb5cf10SGeoff Levand 		len = r->len;
1051*6bb5cf10SGeoff Levand 		if (r->offset >= map.rm.size)
1052*6bb5cf10SGeoff Levand 			lpar_addr += r->offset - map.rm.size;
1053*6bb5cf10SGeoff Levand 		else
1054*6bb5cf10SGeoff Levand 			len -= map.rm.size - r->offset;
1055*6bb5cf10SGeoff Levand 		bus_addr = dma_sb_lpar_to_bus(r, lpar_addr);
1056*6bb5cf10SGeoff Levand 		result = dma_sb_unmap_area(r, bus_addr, len);
1057f58a9d17SGeoff Levand 		BUG_ON(result);
1058*6bb5cf10SGeoff Levand 	}
1059f58a9d17SGeoff Levand 
1060*6bb5cf10SGeoff Levand 	result = dma_sb_region_free(r);
1061f58a9d17SGeoff Levand 	BUG_ON(result);
1062f58a9d17SGeoff Levand 
1063f58a9d17SGeoff Levand 	return result;
1064f58a9d17SGeoff Levand }
1065f58a9d17SGeoff Levand 
1066f58a9d17SGeoff Levand /**
1067*6bb5cf10SGeoff Levand  * dma_sb_map_area_linear - Map an area of memory into a device dma region.
1068f58a9d17SGeoff Levand  * @r: Pointer to a struct ps3_dma_region.
1069f58a9d17SGeoff Levand  * @virt_addr: Starting virtual address of the area to map.
1070f58a9d17SGeoff Levand  * @len: Length in bytes of the area to map.
1071f58a9d17SGeoff Levand  * @bus_addr: A pointer to return the starting ioc bus address of the area to
1072f58a9d17SGeoff Levand  * map.
1073f58a9d17SGeoff Levand  *
1074*6bb5cf10SGeoff Levand  * This routine just returns the corresponding bus address.  Actual mapping
1075f58a9d17SGeoff Levand  * occurs in dma_region_create_linear().
1076f58a9d17SGeoff Levand  */
1077f58a9d17SGeoff Levand 
1078*6bb5cf10SGeoff Levand static int dma_sb_map_area_linear(struct ps3_dma_region *r,
1079*6bb5cf10SGeoff Levand 	unsigned long virt_addr, unsigned long len, unsigned long *bus_addr,
1080*6bb5cf10SGeoff Levand 	u64 iopte_flag)
1081f58a9d17SGeoff Levand {
1082f58a9d17SGeoff Levand 	unsigned long phys_addr = is_kernel_addr(virt_addr) ? __pa(virt_addr)
1083f58a9d17SGeoff Levand 		: virt_addr;
1084*6bb5cf10SGeoff Levand 	*bus_addr = dma_sb_lpar_to_bus(r, ps3_mm_phys_to_lpar(phys_addr));
1085f58a9d17SGeoff Levand 	return 0;
1086f58a9d17SGeoff Levand }
1087f58a9d17SGeoff Levand 
1088f58a9d17SGeoff Levand /**
1089f58a9d17SGeoff Levand  * dma_unmap_area_linear - Unmap an area of memory from a device dma region.
1090f58a9d17SGeoff Levand  * @r: Pointer to a struct ps3_dma_region.
1091f58a9d17SGeoff Levand  * @bus_addr: The starting ioc bus address of the area to unmap.
1092f58a9d17SGeoff Levand  * @len: Length in bytes of the area to unmap.
1093f58a9d17SGeoff Levand  *
1094*6bb5cf10SGeoff Levand  * This routine does nothing.  Unmapping occurs in dma_sb_region_free_linear().
1095f58a9d17SGeoff Levand  */
1096f58a9d17SGeoff Levand 
1097*6bb5cf10SGeoff Levand static int dma_sb_unmap_area_linear(struct ps3_dma_region *r,
1098f58a9d17SGeoff Levand 	unsigned long bus_addr, unsigned long len)
1099f58a9d17SGeoff Levand {
1100f58a9d17SGeoff Levand 	return 0;
1101*6bb5cf10SGeoff Levand };
1102*6bb5cf10SGeoff Levand 
1103*6bb5cf10SGeoff Levand static const struct ps3_dma_region_ops ps3_dma_sb_region_ops =  {
1104*6bb5cf10SGeoff Levand 	.create = dma_sb_region_create,
1105*6bb5cf10SGeoff Levand 	.free = dma_sb_region_free,
1106*6bb5cf10SGeoff Levand 	.map = dma_sb_map_area,
1107*6bb5cf10SGeoff Levand 	.unmap = dma_sb_unmap_area
1108*6bb5cf10SGeoff Levand };
1109*6bb5cf10SGeoff Levand 
1110*6bb5cf10SGeoff Levand static const struct ps3_dma_region_ops ps3_dma_sb_region_linear_ops = {
1111*6bb5cf10SGeoff Levand 	.create = dma_sb_region_create_linear,
1112*6bb5cf10SGeoff Levand 	.free = dma_sb_region_free_linear,
1113*6bb5cf10SGeoff Levand 	.map = dma_sb_map_area_linear,
1114*6bb5cf10SGeoff Levand 	.unmap = dma_sb_unmap_area_linear
1115*6bb5cf10SGeoff Levand };
1116*6bb5cf10SGeoff Levand 
1117*6bb5cf10SGeoff Levand static const struct ps3_dma_region_ops ps3_dma_ioc0_region_ops = {
1118*6bb5cf10SGeoff Levand 	.create = dma_ioc0_region_create,
1119*6bb5cf10SGeoff Levand 	.free = dma_ioc0_region_free,
1120*6bb5cf10SGeoff Levand 	.map = dma_ioc0_map_area,
1121*6bb5cf10SGeoff Levand 	.unmap = dma_ioc0_unmap_area
1122*6bb5cf10SGeoff Levand };
1123*6bb5cf10SGeoff Levand 
1124*6bb5cf10SGeoff Levand int ps3_dma_region_init(struct ps3_system_bus_device *dev,
1125*6bb5cf10SGeoff Levand 	struct ps3_dma_region *r, enum ps3_dma_page_size page_size,
1126*6bb5cf10SGeoff Levand 	enum ps3_dma_region_type region_type, void *addr, unsigned long len)
1127*6bb5cf10SGeoff Levand {
1128*6bb5cf10SGeoff Levand 	unsigned long lpar_addr;
1129*6bb5cf10SGeoff Levand 
1130*6bb5cf10SGeoff Levand 	lpar_addr = addr ? ps3_mm_phys_to_lpar(__pa(addr)) : 0;
1131*6bb5cf10SGeoff Levand 
1132*6bb5cf10SGeoff Levand 	r->dev = dev;
1133*6bb5cf10SGeoff Levand 	r->page_size = page_size;
1134*6bb5cf10SGeoff Levand 	r->region_type = region_type;
1135*6bb5cf10SGeoff Levand 	r->offset = lpar_addr;
1136*6bb5cf10SGeoff Levand 	if (r->offset >= map.rm.size)
1137*6bb5cf10SGeoff Levand 		r->offset -= map.r1.offset;
1138*6bb5cf10SGeoff Levand 	r->len = len ? len : _ALIGN_UP(map.total, 1 << r->page_size);
1139*6bb5cf10SGeoff Levand 
1140*6bb5cf10SGeoff Levand 	switch (dev->dev_type) {
1141*6bb5cf10SGeoff Levand 	case PS3_DEVICE_TYPE_SB:
1142*6bb5cf10SGeoff Levand 		r->region_ops =  (USE_DYNAMIC_DMA)
1143*6bb5cf10SGeoff Levand 			? &ps3_dma_sb_region_ops
1144*6bb5cf10SGeoff Levand 			: &ps3_dma_sb_region_linear_ops;
1145*6bb5cf10SGeoff Levand 		break;
1146*6bb5cf10SGeoff Levand 	case PS3_DEVICE_TYPE_IOC0:
1147*6bb5cf10SGeoff Levand 		r->region_ops = &ps3_dma_ioc0_region_ops;
1148*6bb5cf10SGeoff Levand 		break;
1149*6bb5cf10SGeoff Levand 	default:
1150*6bb5cf10SGeoff Levand 		BUG();
1151*6bb5cf10SGeoff Levand 		return -EINVAL;
1152f58a9d17SGeoff Levand 	}
1153*6bb5cf10SGeoff Levand 	return 0;
1154*6bb5cf10SGeoff Levand }
1155*6bb5cf10SGeoff Levand EXPORT_SYMBOL(ps3_dma_region_init);
1156f58a9d17SGeoff Levand 
1157f58a9d17SGeoff Levand int ps3_dma_region_create(struct ps3_dma_region *r)
1158f58a9d17SGeoff Levand {
1159*6bb5cf10SGeoff Levand 	BUG_ON(!r);
1160*6bb5cf10SGeoff Levand 	BUG_ON(!r->region_ops);
1161*6bb5cf10SGeoff Levand 	BUG_ON(!r->region_ops->create);
1162*6bb5cf10SGeoff Levand 	return r->region_ops->create(r);
1163f58a9d17SGeoff Levand }
1164*6bb5cf10SGeoff Levand EXPORT_SYMBOL(ps3_dma_region_create);
1165f58a9d17SGeoff Levand 
1166f58a9d17SGeoff Levand int ps3_dma_region_free(struct ps3_dma_region *r)
1167f58a9d17SGeoff Levand {
1168*6bb5cf10SGeoff Levand 	BUG_ON(!r);
1169*6bb5cf10SGeoff Levand 	BUG_ON(!r->region_ops);
1170*6bb5cf10SGeoff Levand 	BUG_ON(!r->region_ops->free);
1171*6bb5cf10SGeoff Levand 	return r->region_ops->free(r);
1172f58a9d17SGeoff Levand }
1173*6bb5cf10SGeoff Levand EXPORT_SYMBOL(ps3_dma_region_free);
1174f58a9d17SGeoff Levand 
1175f58a9d17SGeoff Levand int ps3_dma_map(struct ps3_dma_region *r, unsigned long virt_addr,
1176*6bb5cf10SGeoff Levand 	unsigned long len, unsigned long *bus_addr,
1177*6bb5cf10SGeoff Levand 	u64 iopte_flag)
1178f58a9d17SGeoff Levand {
1179*6bb5cf10SGeoff Levand 	return r->region_ops->map(r, virt_addr, len, bus_addr, iopte_flag);
1180f58a9d17SGeoff Levand }
1181f58a9d17SGeoff Levand 
1182f58a9d17SGeoff Levand int ps3_dma_unmap(struct ps3_dma_region *r, unsigned long bus_addr,
1183f58a9d17SGeoff Levand 	unsigned long len)
1184f58a9d17SGeoff Levand {
1185*6bb5cf10SGeoff Levand 	return r->region_ops->unmap(r, bus_addr, len);
1186f58a9d17SGeoff Levand }
1187f58a9d17SGeoff Levand 
1188f58a9d17SGeoff Levand /*============================================================================*/
1189f58a9d17SGeoff Levand /* system startup routines                                                    */
1190f58a9d17SGeoff Levand /*============================================================================*/
1191f58a9d17SGeoff Levand 
1192f58a9d17SGeoff Levand /**
1193f58a9d17SGeoff Levand  * ps3_mm_init - initialize the address space state variables
1194f58a9d17SGeoff Levand  */
1195f58a9d17SGeoff Levand 
1196f58a9d17SGeoff Levand void __init ps3_mm_init(void)
1197f58a9d17SGeoff Levand {
1198f58a9d17SGeoff Levand 	int result;
1199f58a9d17SGeoff Levand 
1200f58a9d17SGeoff Levand 	DBG(" -> %s:%d\n", __func__, __LINE__);
1201f58a9d17SGeoff Levand 
1202f58a9d17SGeoff Levand 	result = ps3_repository_read_mm_info(&map.rm.base, &map.rm.size,
1203f58a9d17SGeoff Levand 		&map.total);
1204f58a9d17SGeoff Levand 
1205f58a9d17SGeoff Levand 	if (result)
1206f58a9d17SGeoff Levand 		panic("ps3_repository_read_mm_info() failed");
1207f58a9d17SGeoff Levand 
1208f58a9d17SGeoff Levand 	map.rm.offset = map.rm.base;
1209f58a9d17SGeoff Levand 	map.vas_id = map.htab_size = 0;
1210f58a9d17SGeoff Levand 
1211f58a9d17SGeoff Levand 	/* this implementation assumes map.rm.base is zero */
1212f58a9d17SGeoff Levand 
1213f58a9d17SGeoff Levand 	BUG_ON(map.rm.base);
1214f58a9d17SGeoff Levand 	BUG_ON(!map.rm.size);
1215f58a9d17SGeoff Levand 
1216f58a9d17SGeoff Levand 	lmb_add(map.rm.base, map.rm.size);
1217f58a9d17SGeoff Levand 	lmb_analyze();
1218f58a9d17SGeoff Levand 
1219f58a9d17SGeoff Levand 	/* arrange to do this in ps3_mm_add_memory */
1220f58a9d17SGeoff Levand 	ps3_mm_region_create(&map.r1, map.total - map.rm.size);
1221f58a9d17SGeoff Levand 
1222*6bb5cf10SGeoff Levand 	/* correct map.total for the real total amount of memory we use */
1223*6bb5cf10SGeoff Levand 	map.total = map.rm.size + map.r1.size;
1224*6bb5cf10SGeoff Levand 
1225f58a9d17SGeoff Levand 	DBG(" <- %s:%d\n", __func__, __LINE__);
1226f58a9d17SGeoff Levand }
1227f58a9d17SGeoff Levand 
1228f58a9d17SGeoff Levand /**
1229f58a9d17SGeoff Levand  * ps3_mm_shutdown - final cleanup of address space
1230f58a9d17SGeoff Levand  */
1231f58a9d17SGeoff Levand 
1232f58a9d17SGeoff Levand void ps3_mm_shutdown(void)
1233f58a9d17SGeoff Levand {
1234f58a9d17SGeoff Levand 	ps3_mm_region_destroy(&map.r1);
1235f58a9d17SGeoff Levand }
1236