blob: c2045880e674afc91a5295711042f4722c6aa510 [file] [log] [blame]
Geoff Levandf58a9d12006-11-23 00:46:51 +01001/*
2 * PS3 address space management.
3 *
4 * Copyright (C) 2006 Sony Computer Entertainment Inc.
5 * Copyright 2006 Sony Corp.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 2 of the License.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21#include <linux/kernel.h>
22#include <linux/module.h>
23#include <linux/memory_hotplug.h>
Yinghai Lu95f72d12010-07-12 14:36:09 +100024#include <linux/memblock.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Geoff Levandf58a9d12006-11-23 00:46:51 +010026
Geert Uytterhoeven9413c882009-07-29 02:06:42 +000027#include <asm/cell-regs.h>
Arnd Bergmanne22ba7e2006-11-27 19:18:57 +010028#include <asm/firmware.h>
David S. Millerd9b2b2a2008-02-13 16:56:49 -080029#include <asm/prom.h>
Geoff Levandf58a9d12006-11-23 00:46:51 +010030#include <asm/udbg.h>
Geoff Levandf58a9d12006-11-23 00:46:51 +010031#include <asm/lv1call.h>
32
33#include "platform.h"
34
35#if defined(DEBUG)
Geert Uytterhoeven83bb6432007-06-16 07:19:23 +100036#define DBG udbg_printf
Geoff Levandf58a9d12006-11-23 00:46:51 +010037#else
Michael Ellerman74246392009-06-22 15:56:39 +000038#define DBG pr_devel
Geoff Levandf58a9d12006-11-23 00:46:51 +010039#endif
40
41enum {
Geoff Levandf58a9d12006-11-23 00:46:51 +010042#if defined(CONFIG_PS3_DYNAMIC_DMA)
43 USE_DYNAMIC_DMA = 1,
44#else
45 USE_DYNAMIC_DMA = 0,
46#endif
47};
48
49enum {
50 PAGE_SHIFT_4K = 12U,
51 PAGE_SHIFT_64K = 16U,
52 PAGE_SHIFT_16M = 24U,
53};
54
55static unsigned long make_page_sizes(unsigned long a, unsigned long b)
56{
57 return (a << 56) | (b << 48);
58}
59
60enum {
61 ALLOCATE_MEMORY_TRY_ALT_UNIT = 0X04,
62 ALLOCATE_MEMORY_ADDR_ZERO = 0X08,
63};
64
65/* valid htab sizes are {18,19,20} = 256K, 512K, 1M */
66
67enum {
68 HTAB_SIZE_MAX = 20U, /* HV limit of 1MB */
69 HTAB_SIZE_MIN = 18U, /* CPU limit of 256KB */
70};
71
72/*============================================================================*/
73/* virtual address space routines */
74/*============================================================================*/
75
76/**
77 * struct mem_region - memory region structure
78 * @base: base address
79 * @size: size in bytes
80 * @offset: difference between base and rm.size
81 */
82
83struct mem_region {
Stephen Rothwellb17b3df2009-01-13 19:59:41 +000084 u64 base;
Stephen Rothwell5418b9c2009-01-13 20:01:28 +000085 u64 size;
Geoff Levandf58a9d12006-11-23 00:46:51 +010086 unsigned long offset;
87};
88
89/**
90 * struct map - address space state variables holder
91 * @total: total memory available as reported by HV
92 * @vas_id - HV virtual address space id
93 * @htab_size: htab size in bytes
94 *
95 * The HV virtual address space (vas) allows for hotplug memory regions.
96 * Memory regions can be created and destroyed in the vas at runtime.
97 * @rm: real mode (bootmem) region
98 * @r1: hotplug memory region(s)
99 *
100 * ps3 addresses
101 * virt_addr: a cpu 'translated' effective address
102 * phys_addr: an address in what Linux thinks is the physical address space
103 * lpar_addr: an address in the HV virtual address space
104 * bus_addr: an io controller 'translated' address on a device bus
105 */
106
107struct map {
Stephen Rothwell5418b9c2009-01-13 20:01:28 +0000108 u64 total;
Stephen Rothwellb17b3df2009-01-13 19:59:41 +0000109 u64 vas_id;
110 u64 htab_size;
Geoff Levandf58a9d12006-11-23 00:46:51 +0100111 struct mem_region rm;
112 struct mem_region r1;
113};
114
115#define debug_dump_map(x) _debug_dump_map(x, __func__, __LINE__)
Geoff Levand6bb5cf12007-06-16 07:52:02 +1000116static void __maybe_unused _debug_dump_map(const struct map *m,
117 const char *func, int line)
Geoff Levandf58a9d12006-11-23 00:46:51 +0100118{
Stephen Rothwell5418b9c2009-01-13 20:01:28 +0000119 DBG("%s:%d: map.total = %llxh\n", func, line, m->total);
120 DBG("%s:%d: map.rm.size = %llxh\n", func, line, m->rm.size);
Stephen Rothwellb17b3df2009-01-13 19:59:41 +0000121 DBG("%s:%d: map.vas_id = %llu\n", func, line, m->vas_id);
122 DBG("%s:%d: map.htab_size = %llxh\n", func, line, m->htab_size);
123 DBG("%s:%d: map.r1.base = %llxh\n", func, line, m->r1.base);
Geoff Levandf58a9d12006-11-23 00:46:51 +0100124 DBG("%s:%d: map.r1.offset = %lxh\n", func, line, m->r1.offset);
Stephen Rothwell5418b9c2009-01-13 20:01:28 +0000125 DBG("%s:%d: map.r1.size = %llxh\n", func, line, m->r1.size);
Geoff Levandf58a9d12006-11-23 00:46:51 +0100126}
127
128static struct map map;
129
130/**
131 * ps3_mm_phys_to_lpar - translate a linux physical address to lpar address
132 * @phys_addr: linux physical address
133 */
134
135unsigned long ps3_mm_phys_to_lpar(unsigned long phys_addr)
136{
137 BUG_ON(is_kernel_addr(phys_addr));
Geoff Levanda628df12008-01-19 07:32:59 +1100138 return (phys_addr < map.rm.size || phys_addr >= map.total)
139 ? phys_addr : phys_addr + map.r1.offset;
Geoff Levandf58a9d12006-11-23 00:46:51 +0100140}
141
142EXPORT_SYMBOL(ps3_mm_phys_to_lpar);
143
144/**
145 * ps3_mm_vas_create - create the virtual address space
146 */
147
148void __init ps3_mm_vas_create(unsigned long* htab_size)
149{
150 int result;
Stephen Rothwellb17b3df2009-01-13 19:59:41 +0000151 u64 start_address;
152 u64 size;
153 u64 access_right;
154 u64 max_page_size;
155 u64 flags;
Geoff Levandf58a9d12006-11-23 00:46:51 +0100156
157 result = lv1_query_logical_partition_address_region_info(0,
158 &start_address, &size, &access_right, &max_page_size,
159 &flags);
160
161 if (result) {
162 DBG("%s:%d: lv1_query_logical_partition_address_region_info "
163 "failed: %s\n", __func__, __LINE__,
164 ps3_result(result));
165 goto fail;
166 }
167
168 if (max_page_size < PAGE_SHIFT_16M) {
Stephen Rothwellb17b3df2009-01-13 19:59:41 +0000169 DBG("%s:%d: bad max_page_size %llxh\n", __func__, __LINE__,
Geoff Levandf58a9d12006-11-23 00:46:51 +0100170 max_page_size);
171 goto fail;
172 }
173
174 BUILD_BUG_ON(CONFIG_PS3_HTAB_SIZE > HTAB_SIZE_MAX);
175 BUILD_BUG_ON(CONFIG_PS3_HTAB_SIZE < HTAB_SIZE_MIN);
176
177 result = lv1_construct_virtual_address_space(CONFIG_PS3_HTAB_SIZE,
178 2, make_page_sizes(PAGE_SHIFT_16M, PAGE_SHIFT_64K),
179 &map.vas_id, &map.htab_size);
180
181 if (result) {
182 DBG("%s:%d: lv1_construct_virtual_address_space failed: %s\n",
183 __func__, __LINE__, ps3_result(result));
184 goto fail;
185 }
186
187 result = lv1_select_virtual_address_space(map.vas_id);
188
189 if (result) {
190 DBG("%s:%d: lv1_select_virtual_address_space failed: %s\n",
191 __func__, __LINE__, ps3_result(result));
192 goto fail;
193 }
194
195 *htab_size = map.htab_size;
196
197 debug_dump_map(&map);
198
199 return;
200
201fail:
202 panic("ps3_mm_vas_create failed");
203}
204
205/**
206 * ps3_mm_vas_destroy -
207 */
208
209void ps3_mm_vas_destroy(void)
210{
Geoff Levand6bb5cf12007-06-16 07:52:02 +1000211 int result;
212
Stephen Rothwellb17b3df2009-01-13 19:59:41 +0000213 DBG("%s:%d: map.vas_id = %llu\n", __func__, __LINE__, map.vas_id);
Geoff Levand6bb5cf12007-06-16 07:52:02 +1000214
Geoff Levandf58a9d12006-11-23 00:46:51 +0100215 if (map.vas_id) {
Geoff Levand6bb5cf12007-06-16 07:52:02 +1000216 result = lv1_select_virtual_address_space(0);
217 BUG_ON(result);
218 result = lv1_destruct_virtual_address_space(map.vas_id);
219 BUG_ON(result);
Geoff Levandf58a9d12006-11-23 00:46:51 +0100220 map.vas_id = 0;
221 }
222}
223
224/*============================================================================*/
225/* memory hotplug routines */
226/*============================================================================*/
227
228/**
229 * ps3_mm_region_create - create a memory region in the vas
230 * @r: pointer to a struct mem_region to accept initialized values
231 * @size: requested region size
232 *
233 * This implementation creates the region with the vas large page size.
234 * @size is rounded down to a multiple of the vas large page size.
235 */
236
Geert Uytterhoeven32f44a12007-06-16 08:07:23 +1000237static int ps3_mm_region_create(struct mem_region *r, unsigned long size)
Geoff Levandf58a9d12006-11-23 00:46:51 +0100238{
239 int result;
Stephen Rothwellb17b3df2009-01-13 19:59:41 +0000240 u64 muid;
Geoff Levandf58a9d12006-11-23 00:46:51 +0100241
242 r->size = _ALIGN_DOWN(size, 1 << PAGE_SHIFT_16M);
243
244 DBG("%s:%d requested %lxh\n", __func__, __LINE__, size);
Stephen Rothwell5418b9c2009-01-13 20:01:28 +0000245 DBG("%s:%d actual %llxh\n", __func__, __LINE__, r->size);
246 DBG("%s:%d difference %llxh (%lluMB)\n", __func__, __LINE__,
247 size - r->size, (size - r->size) / 1024 / 1024);
Geoff Levandf58a9d12006-11-23 00:46:51 +0100248
249 if (r->size == 0) {
250 DBG("%s:%d: size == 0\n", __func__, __LINE__);
251 result = -1;
252 goto zero_region;
253 }
254
255 result = lv1_allocate_memory(r->size, PAGE_SHIFT_16M, 0,
256 ALLOCATE_MEMORY_TRY_ALT_UNIT, &r->base, &muid);
257
258 if (result || r->base < map.rm.size) {
259 DBG("%s:%d: lv1_allocate_memory failed: %s\n",
260 __func__, __LINE__, ps3_result(result));
261 goto zero_region;
262 }
263
264 r->offset = r->base - map.rm.size;
265 return result;
266
267zero_region:
268 r->size = r->base = r->offset = 0;
269 return result;
270}
271
272/**
273 * ps3_mm_region_destroy - destroy a memory region
274 * @r: pointer to struct mem_region
275 */
276
Geert Uytterhoeven32f44a12007-06-16 08:07:23 +1000277static void ps3_mm_region_destroy(struct mem_region *r)
Geoff Levandf58a9d12006-11-23 00:46:51 +0100278{
Geoff Levand6bb5cf12007-06-16 07:52:02 +1000279 int result;
280
Stephen Rothwellb17b3df2009-01-13 19:59:41 +0000281 DBG("%s:%d: r->base = %llxh\n", __func__, __LINE__, r->base);
Geoff Levandf58a9d12006-11-23 00:46:51 +0100282 if (r->base) {
Geoff Levand6bb5cf12007-06-16 07:52:02 +1000283 result = lv1_release_memory(r->base);
284 BUG_ON(result);
Geoff Levandf58a9d12006-11-23 00:46:51 +0100285 r->size = r->base = r->offset = 0;
286 map.total = map.rm.size;
287 }
288}
289
290/**
291 * ps3_mm_add_memory - hot add memory
292 */
293
294static int __init ps3_mm_add_memory(void)
295{
296 int result;
297 unsigned long start_addr;
298 unsigned long start_pfn;
299 unsigned long nr_pages;
300
Arnd Bergmanne22ba7e2006-11-27 19:18:57 +0100301 if (!firmware_has_feature(FW_FEATURE_PS3_LV1))
Geert Uytterhoevenef596c62007-03-10 00:05:38 +0100302 return -ENODEV;
Arnd Bergmanne22ba7e2006-11-27 19:18:57 +0100303
Geoff Levandf58a9d12006-11-23 00:46:51 +0100304 BUG_ON(!mem_init_done);
305
Geoff Levanda628df12008-01-19 07:32:59 +1100306 start_addr = map.rm.size;
Geoff Levandf58a9d12006-11-23 00:46:51 +0100307 start_pfn = start_addr >> PAGE_SHIFT;
308 nr_pages = (map.r1.size + PAGE_SIZE - 1) >> PAGE_SHIFT;
309
310 DBG("%s:%d: start_addr %lxh, start_pfn %lxh, nr_pages %lxh\n",
311 __func__, __LINE__, start_addr, start_pfn, nr_pages);
312
313 result = add_memory(0, start_addr, map.r1.size);
314
315 if (result) {
Geoff Levand9146cfc2009-03-03 08:33:06 +0000316 pr_err("%s:%d: add_memory failed: (%d)\n",
Geoff Levandf58a9d12006-11-23 00:46:51 +0100317 __func__, __LINE__, result);
318 return result;
319 }
320
Yinghai Lu95f72d12010-07-12 14:36:09 +1000321 memblock_add(start_addr, map.r1.size);
322 memblock_analyze();
Geoff Levandecc240f2008-05-16 06:09:59 +1000323
Geoff Levandf58a9d12006-11-23 00:46:51 +0100324 result = online_pages(start_pfn, nr_pages);
325
326 if (result)
Geoff Levand9146cfc2009-03-03 08:33:06 +0000327 pr_err("%s:%d: online_pages failed: (%d)\n",
Geoff Levandf58a9d12006-11-23 00:46:51 +0100328 __func__, __LINE__, result);
329
330 return result;
331}
332
Geoff Levand00476562009-02-12 12:36:16 +0000333device_initcall(ps3_mm_add_memory);
Geoff Levandf58a9d12006-11-23 00:46:51 +0100334
335/*============================================================================*/
336/* dma routines */
337/*============================================================================*/
338
339/**
Geoff Levand6bb5cf12007-06-16 07:52:02 +1000340 * dma_sb_lpar_to_bus - Translate an lpar address to ioc mapped bus address.
Geoff Levandf58a9d12006-11-23 00:46:51 +0100341 * @r: pointer to dma region structure
342 * @lpar_addr: HV lpar address
343 */
344
Geoff Levand6bb5cf12007-06-16 07:52:02 +1000345static unsigned long dma_sb_lpar_to_bus(struct ps3_dma_region *r,
Geoff Levandf58a9d12006-11-23 00:46:51 +0100346 unsigned long lpar_addr)
347{
Geoff Levand6bb5cf12007-06-16 07:52:02 +1000348 if (lpar_addr >= map.rm.size)
349 lpar_addr -= map.r1.offset;
350 BUG_ON(lpar_addr < r->offset);
351 BUG_ON(lpar_addr >= r->offset + r->len);
352 return r->bus_addr + lpar_addr - r->offset;
Geoff Levandf58a9d12006-11-23 00:46:51 +0100353}
354
355#define dma_dump_region(_a) _dma_dump_region(_a, __func__, __LINE__)
Geoff Levand6bb5cf12007-06-16 07:52:02 +1000356static void __maybe_unused _dma_dump_region(const struct ps3_dma_region *r,
357 const char *func, int line)
Geoff Levandf58a9d12006-11-23 00:46:51 +0100358{
Stephen Rothwell5c949072009-01-13 20:02:39 +0000359 DBG("%s:%d: dev %llu:%llu\n", func, line, r->dev->bus_id,
Geoff Levand6bb5cf12007-06-16 07:52:02 +1000360 r->dev->dev_id);
Geoff Levandf58a9d12006-11-23 00:46:51 +0100361 DBG("%s:%d: page_size %u\n", func, line, r->page_size);
362 DBG("%s:%d: bus_addr %lxh\n", func, line, r->bus_addr);
363 DBG("%s:%d: len %lxh\n", func, line, r->len);
Geoff Levand6bb5cf12007-06-16 07:52:02 +1000364 DBG("%s:%d: offset %lxh\n", func, line, r->offset);
Geoff Levandf58a9d12006-11-23 00:46:51 +0100365}
366
Geoff Levand6bb5cf12007-06-16 07:52:02 +1000367 /**
Geoff Levandf58a9d12006-11-23 00:46:51 +0100368 * dma_chunk - A chunk of dma pages mapped by the io controller.
369 * @region - The dma region that owns this chunk.
370 * @lpar_addr: Starting lpar address of the area to map.
371 * @bus_addr: Starting ioc bus address of the area to map.
372 * @len: Length in bytes of the area to map.
373 * @link: A struct list_head used with struct ps3_dma_region.chunk_list, the
374 * list of all chuncks owned by the region.
375 *
376 * This implementation uses a very simple dma page manager
377 * based on the dma_chunk structure. This scheme assumes
378 * that all drivers use very well behaved dma ops.
379 */
380
381struct dma_chunk {
382 struct ps3_dma_region *region;
383 unsigned long lpar_addr;
384 unsigned long bus_addr;
385 unsigned long len;
386 struct list_head link;
387 unsigned int usage_count;
388};
389
390#define dma_dump_chunk(_a) _dma_dump_chunk(_a, __func__, __LINE__)
391static void _dma_dump_chunk (const struct dma_chunk* c, const char* func,
392 int line)
393{
Stephen Rothwell5c949072009-01-13 20:02:39 +0000394 DBG("%s:%d: r.dev %llu:%llu\n", func, line,
Geoff Levand6bb5cf12007-06-16 07:52:02 +1000395 c->region->dev->bus_id, c->region->dev->dev_id);
Geoff Levandf58a9d12006-11-23 00:46:51 +0100396 DBG("%s:%d: r.bus_addr %lxh\n", func, line, c->region->bus_addr);
397 DBG("%s:%d: r.page_size %u\n", func, line, c->region->page_size);
398 DBG("%s:%d: r.len %lxh\n", func, line, c->region->len);
Geoff Levand6bb5cf12007-06-16 07:52:02 +1000399 DBG("%s:%d: r.offset %lxh\n", func, line, c->region->offset);
Geoff Levandf58a9d12006-11-23 00:46:51 +0100400 DBG("%s:%d: c.lpar_addr %lxh\n", func, line, c->lpar_addr);
401 DBG("%s:%d: c.bus_addr %lxh\n", func, line, c->bus_addr);
402 DBG("%s:%d: c.len %lxh\n", func, line, c->len);
403}
404
405static struct dma_chunk * dma_find_chunk(struct ps3_dma_region *r,
406 unsigned long bus_addr, unsigned long len)
407{
408 struct dma_chunk *c;
409 unsigned long aligned_bus = _ALIGN_DOWN(bus_addr, 1 << r->page_size);
Geoff Levand6bb5cf12007-06-16 07:52:02 +1000410 unsigned long aligned_len = _ALIGN_UP(len+bus_addr-aligned_bus,
411 1 << r->page_size);
Geoff Levandf58a9d12006-11-23 00:46:51 +0100412
413 list_for_each_entry(c, &r->chunk_list.head, link) {
414 /* intersection */
Geoff Levand6bb5cf12007-06-16 07:52:02 +1000415 if (aligned_bus >= c->bus_addr &&
416 aligned_bus + aligned_len <= c->bus_addr + c->len)
Geoff Levandf58a9d12006-11-23 00:46:51 +0100417 return c;
Geoff Levand6bb5cf12007-06-16 07:52:02 +1000418
Geoff Levandf58a9d12006-11-23 00:46:51 +0100419 /* below */
Geoff Levand6bb5cf12007-06-16 07:52:02 +1000420 if (aligned_bus + aligned_len <= c->bus_addr)
Geoff Levandf58a9d12006-11-23 00:46:51 +0100421 continue;
Geoff Levand6bb5cf12007-06-16 07:52:02 +1000422
Geoff Levandf58a9d12006-11-23 00:46:51 +0100423 /* above */
Geoff Levand6bb5cf12007-06-16 07:52:02 +1000424 if (aligned_bus >= c->bus_addr + c->len)
Geoff Levandf58a9d12006-11-23 00:46:51 +0100425 continue;
Geoff Levandf58a9d12006-11-23 00:46:51 +0100426
427 /* we don't handle the multi-chunk case for now */
Geoff Levandf58a9d12006-11-23 00:46:51 +0100428 dma_dump_chunk(c);
429 BUG();
430 }
431 return NULL;
432}
433
Geoff Levand6bb5cf12007-06-16 07:52:02 +1000434static struct dma_chunk *dma_find_chunk_lpar(struct ps3_dma_region *r,
435 unsigned long lpar_addr, unsigned long len)
436{
437 struct dma_chunk *c;
438 unsigned long aligned_lpar = _ALIGN_DOWN(lpar_addr, 1 << r->page_size);
439 unsigned long aligned_len = _ALIGN_UP(len + lpar_addr - aligned_lpar,
440 1 << r->page_size);
441
442 list_for_each_entry(c, &r->chunk_list.head, link) {
443 /* intersection */
444 if (c->lpar_addr <= aligned_lpar &&
445 aligned_lpar < c->lpar_addr + c->len) {
446 if (aligned_lpar + aligned_len <= c->lpar_addr + c->len)
447 return c;
448 else {
449 dma_dump_chunk(c);
450 BUG();
451 }
452 }
453 /* below */
454 if (aligned_lpar + aligned_len <= c->lpar_addr) {
455 continue;
456 }
457 /* above */
458 if (c->lpar_addr + c->len <= aligned_lpar) {
459 continue;
460 }
461 }
462 return NULL;
463}
464
465static int dma_sb_free_chunk(struct dma_chunk *c)
Geoff Levandf58a9d12006-11-23 00:46:51 +0100466{
467 int result = 0;
468
469 if (c->bus_addr) {
Geoff Levand6bb5cf12007-06-16 07:52:02 +1000470 result = lv1_unmap_device_dma_region(c->region->dev->bus_id,
471 c->region->dev->dev_id, c->bus_addr, c->len);
Geoff Levandf58a9d12006-11-23 00:46:51 +0100472 BUG_ON(result);
473 }
474
475 kfree(c);
476 return result;
477}
478
Geoff Levand6bb5cf12007-06-16 07:52:02 +1000479static int dma_ioc0_free_chunk(struct dma_chunk *c)
480{
481 int result = 0;
482 int iopage;
483 unsigned long offset;
484 struct ps3_dma_region *r = c->region;
485
486 DBG("%s:start\n", __func__);
487 for (iopage = 0; iopage < (c->len >> r->page_size); iopage++) {
488 offset = (1 << r->page_size) * iopage;
489 /* put INVALID entry */
490 result = lv1_put_iopte(0,
491 c->bus_addr + offset,
492 c->lpar_addr + offset,
493 r->ioid,
494 0);
495 DBG("%s: bus=%#lx, lpar=%#lx, ioid=%d\n", __func__,
496 c->bus_addr + offset,
497 c->lpar_addr + offset,
498 r->ioid);
499
500 if (result) {
501 DBG("%s:%d: lv1_put_iopte failed: %s\n", __func__,
502 __LINE__, ps3_result(result));
503 }
504 }
505 kfree(c);
506 DBG("%s:end\n", __func__);
507 return result;
508}
509
Geoff Levandf58a9d12006-11-23 00:46:51 +0100510/**
Geoff Levand6bb5cf12007-06-16 07:52:02 +1000511 * dma_sb_map_pages - Maps dma pages into the io controller bus address space.
Geoff Levandf58a9d12006-11-23 00:46:51 +0100512 * @r: Pointer to a struct ps3_dma_region.
513 * @phys_addr: Starting physical address of the area to map.
514 * @len: Length in bytes of the area to map.
515 * c_out: A pointer to receive an allocated struct dma_chunk for this area.
516 *
517 * This is the lowest level dma mapping routine, and is the one that will
518 * make the HV call to add the pages into the io controller address space.
519 */
520
Geoff Levand6bb5cf12007-06-16 07:52:02 +1000521static int dma_sb_map_pages(struct ps3_dma_region *r, unsigned long phys_addr,
522 unsigned long len, struct dma_chunk **c_out, u64 iopte_flag)
Geoff Levandf58a9d12006-11-23 00:46:51 +0100523{
524 int result;
525 struct dma_chunk *c;
526
527 c = kzalloc(sizeof(struct dma_chunk), GFP_ATOMIC);
528
529 if (!c) {
530 result = -ENOMEM;
531 goto fail_alloc;
532 }
533
534 c->region = r;
535 c->lpar_addr = ps3_mm_phys_to_lpar(phys_addr);
Geoff Levand6bb5cf12007-06-16 07:52:02 +1000536 c->bus_addr = dma_sb_lpar_to_bus(r, c->lpar_addr);
Geoff Levandf58a9d12006-11-23 00:46:51 +0100537 c->len = len;
538
Geoff Levand6bb5cf12007-06-16 07:52:02 +1000539 BUG_ON(iopte_flag != 0xf800000000000000UL);
540 result = lv1_map_device_dma_region(c->region->dev->bus_id,
541 c->region->dev->dev_id, c->lpar_addr,
542 c->bus_addr, c->len, iopte_flag);
Geoff Levandf58a9d12006-11-23 00:46:51 +0100543 if (result) {
544 DBG("%s:%d: lv1_map_device_dma_region failed: %s\n",
545 __func__, __LINE__, ps3_result(result));
546 goto fail_map;
547 }
548
549 list_add(&c->link, &r->chunk_list.head);
550
551 *c_out = c;
552 return 0;
553
554fail_map:
555 kfree(c);
556fail_alloc:
557 *c_out = NULL;
558 DBG(" <- %s:%d\n", __func__, __LINE__);
559 return result;
560}
561
Geoff Levand6bb5cf12007-06-16 07:52:02 +1000562static int dma_ioc0_map_pages(struct ps3_dma_region *r, unsigned long phys_addr,
563 unsigned long len, struct dma_chunk **c_out,
564 u64 iopte_flag)
565{
566 int result;
567 struct dma_chunk *c, *last;
568 int iopage, pages;
569 unsigned long offset;
570
571 DBG(KERN_ERR "%s: phy=%#lx, lpar%#lx, len=%#lx\n", __func__,
572 phys_addr, ps3_mm_phys_to_lpar(phys_addr), len);
573 c = kzalloc(sizeof(struct dma_chunk), GFP_ATOMIC);
574
575 if (!c) {
576 result = -ENOMEM;
577 goto fail_alloc;
578 }
579
580 c->region = r;
581 c->len = len;
582 c->lpar_addr = ps3_mm_phys_to_lpar(phys_addr);
583 /* allocate IO address */
584 if (list_empty(&r->chunk_list.head)) {
585 /* first one */
586 c->bus_addr = r->bus_addr;
587 } else {
588 /* derive from last bus addr*/
589 last = list_entry(r->chunk_list.head.next,
590 struct dma_chunk, link);
591 c->bus_addr = last->bus_addr + last->len;
592 DBG("%s: last bus=%#lx, len=%#lx\n", __func__,
593 last->bus_addr, last->len);
594 }
595
596 /* FIXME: check whether length exceeds region size */
597
598 /* build ioptes for the area */
599 pages = len >> r->page_size;
Stephen Rothwell5c949072009-01-13 20:02:39 +0000600 DBG("%s: pgsize=%#x len=%#lx pages=%#x iopteflag=%#llx\n", __func__,
Geoff Levand6bb5cf12007-06-16 07:52:02 +1000601 r->page_size, r->len, pages, iopte_flag);
602 for (iopage = 0; iopage < pages; iopage++) {
603 offset = (1 << r->page_size) * iopage;
604 result = lv1_put_iopte(0,
605 c->bus_addr + offset,
606 c->lpar_addr + offset,
607 r->ioid,
608 iopte_flag);
609 if (result) {
Geert Uytterhoeven7e280602009-06-10 04:39:04 +0000610 pr_warning("%s:%d: lv1_put_iopte failed: %s\n",
611 __func__, __LINE__, ps3_result(result));
Geoff Levand6bb5cf12007-06-16 07:52:02 +1000612 goto fail_map;
613 }
614 DBG("%s: pg=%d bus=%#lx, lpar=%#lx, ioid=%#x\n", __func__,
615 iopage, c->bus_addr + offset, c->lpar_addr + offset,
616 r->ioid);
617 }
618
619 /* be sure that last allocated one is inserted at head */
620 list_add(&c->link, &r->chunk_list.head);
621
622 *c_out = c;
623 DBG("%s: end\n", __func__);
624 return 0;
625
626fail_map:
627 for (iopage--; 0 <= iopage; iopage--) {
628 lv1_put_iopte(0,
629 c->bus_addr + offset,
630 c->lpar_addr + offset,
631 r->ioid,
632 0);
633 }
634 kfree(c);
635fail_alloc:
636 *c_out = NULL;
637 return result;
638}
639
Geoff Levandf58a9d12006-11-23 00:46:51 +0100640/**
Geoff Levand6bb5cf12007-06-16 07:52:02 +1000641 * dma_sb_region_create - Create a device dma region.
Geoff Levandf58a9d12006-11-23 00:46:51 +0100642 * @r: Pointer to a struct ps3_dma_region.
643 *
644 * This is the lowest level dma region create routine, and is the one that
645 * will make the HV call to create the region.
646 */
647
Geoff Levand6bb5cf12007-06-16 07:52:02 +1000648static int dma_sb_region_create(struct ps3_dma_region *r)
Geoff Levandf58a9d12006-11-23 00:46:51 +0100649{
650 int result;
Stephen Rothwellb17b3df2009-01-13 19:59:41 +0000651 u64 bus_addr;
Geoff Levandf58a9d12006-11-23 00:46:51 +0100652
Geoff Levand62d80742008-12-03 13:52:15 +0000653 DBG(" -> %s:%d:\n", __func__, __LINE__);
Geoff Levand6bb5cf12007-06-16 07:52:02 +1000654
655 BUG_ON(!r);
656
657 if (!r->dev->bus_id) {
Stephen Rothwell5c949072009-01-13 20:02:39 +0000658 pr_info("%s:%d: %llu:%llu no dma\n", __func__, __LINE__,
Geoff Levand6bb5cf12007-06-16 07:52:02 +1000659 r->dev->bus_id, r->dev->dev_id);
660 return 0;
661 }
662
663 DBG("%s:%u: len = 0x%lx, page_size = %u, offset = 0x%lx\n", __func__,
664 __LINE__, r->len, r->page_size, r->offset);
665
666 BUG_ON(!r->len);
667 BUG_ON(!r->page_size);
668 BUG_ON(!r->region_ops);
669
Geoff Levandf58a9d12006-11-23 00:46:51 +0100670 INIT_LIST_HEAD(&r->chunk_list.head);
671 spin_lock_init(&r->chunk_list.lock);
672
Geoff Levand6bb5cf12007-06-16 07:52:02 +1000673 result = lv1_allocate_device_dma_region(r->dev->bus_id, r->dev->dev_id,
674 roundup_pow_of_two(r->len), r->page_size, r->region_type,
Stephen Rothwellb17b3df2009-01-13 19:59:41 +0000675 &bus_addr);
676 r->bus_addr = bus_addr;
Geoff Levandf58a9d12006-11-23 00:46:51 +0100677
678 if (result) {
679 DBG("%s:%d: lv1_allocate_device_dma_region failed: %s\n",
680 __func__, __LINE__, ps3_result(result));
681 r->len = r->bus_addr = 0;
682 }
683
684 return result;
685}
686
Geoff Levand6bb5cf12007-06-16 07:52:02 +1000687static int dma_ioc0_region_create(struct ps3_dma_region *r)
688{
689 int result;
Stephen Rothwellb17b3df2009-01-13 19:59:41 +0000690 u64 bus_addr;
Geoff Levand6bb5cf12007-06-16 07:52:02 +1000691
692 INIT_LIST_HEAD(&r->chunk_list.head);
693 spin_lock_init(&r->chunk_list.lock);
694
695 result = lv1_allocate_io_segment(0,
696 r->len,
697 r->page_size,
Stephen Rothwellb17b3df2009-01-13 19:59:41 +0000698 &bus_addr);
699 r->bus_addr = bus_addr;
Geoff Levand6bb5cf12007-06-16 07:52:02 +1000700 if (result) {
701 DBG("%s:%d: lv1_allocate_io_segment failed: %s\n",
702 __func__, __LINE__, ps3_result(result));
703 r->len = r->bus_addr = 0;
704 }
705 DBG("%s: len=%#lx, pg=%d, bus=%#lx\n", __func__,
706 r->len, r->page_size, r->bus_addr);
707 return result;
708}
709
Geoff Levandf58a9d12006-11-23 00:46:51 +0100710/**
711 * dma_region_free - Free a device dma region.
712 * @r: Pointer to a struct ps3_dma_region.
713 *
714 * This is the lowest level dma region free routine, and is the one that
715 * will make the HV call to free the region.
716 */
717
Geoff Levand6bb5cf12007-06-16 07:52:02 +1000718static int dma_sb_region_free(struct ps3_dma_region *r)
Geoff Levandf58a9d12006-11-23 00:46:51 +0100719{
720 int result;
721 struct dma_chunk *c;
722 struct dma_chunk *tmp;
723
Geoff Levand6bb5cf12007-06-16 07:52:02 +1000724 BUG_ON(!r);
725
726 if (!r->dev->bus_id) {
Stephen Rothwell5c949072009-01-13 20:02:39 +0000727 pr_info("%s:%d: %llu:%llu no dma\n", __func__, __LINE__,
Geoff Levand6bb5cf12007-06-16 07:52:02 +1000728 r->dev->bus_id, r->dev->dev_id);
729 return 0;
Geoff Levandf58a9d12006-11-23 00:46:51 +0100730 }
731
Geoff Levand6bb5cf12007-06-16 07:52:02 +1000732 list_for_each_entry_safe(c, tmp, &r->chunk_list.head, link) {
733 list_del(&c->link);
734 dma_sb_free_chunk(c);
735 }
736
737 result = lv1_free_device_dma_region(r->dev->bus_id, r->dev->dev_id,
Geoff Levandf58a9d12006-11-23 00:46:51 +0100738 r->bus_addr);
739
740 if (result)
741 DBG("%s:%d: lv1_free_device_dma_region failed: %s\n",
742 __func__, __LINE__, ps3_result(result));
743
Geoff Levand6bb5cf12007-06-16 07:52:02 +1000744 r->bus_addr = 0;
745
746 return result;
747}
748
749static int dma_ioc0_region_free(struct ps3_dma_region *r)
750{
751 int result;
752 struct dma_chunk *c, *n;
753
754 DBG("%s: start\n", __func__);
755 list_for_each_entry_safe(c, n, &r->chunk_list.head, link) {
756 list_del(&c->link);
757 dma_ioc0_free_chunk(c);
758 }
759
760 result = lv1_release_io_segment(0, r->bus_addr);
761
762 if (result)
763 DBG("%s:%d: lv1_free_device_dma_region failed: %s\n",
764 __func__, __LINE__, ps3_result(result));
765
766 r->bus_addr = 0;
767 DBG("%s: end\n", __func__);
Geoff Levandf58a9d12006-11-23 00:46:51 +0100768
769 return result;
770}
771
772/**
Geoff Levand6bb5cf12007-06-16 07:52:02 +1000773 * dma_sb_map_area - Map an area of memory into a device dma region.
Geoff Levandf58a9d12006-11-23 00:46:51 +0100774 * @r: Pointer to a struct ps3_dma_region.
775 * @virt_addr: Starting virtual address of the area to map.
776 * @len: Length in bytes of the area to map.
777 * @bus_addr: A pointer to return the starting ioc bus address of the area to
778 * map.
779 *
780 * This is the common dma mapping routine.
781 */
782
Geoff Levand6bb5cf12007-06-16 07:52:02 +1000783static int dma_sb_map_area(struct ps3_dma_region *r, unsigned long virt_addr,
Stephen Rothwell494fd07a2009-01-13 19:58:10 +0000784 unsigned long len, dma_addr_t *bus_addr,
Geoff Levand6bb5cf12007-06-16 07:52:02 +1000785 u64 iopte_flag)
Geoff Levandf58a9d12006-11-23 00:46:51 +0100786{
787 int result;
788 unsigned long flags;
789 struct dma_chunk *c;
790 unsigned long phys_addr = is_kernel_addr(virt_addr) ? __pa(virt_addr)
791 : virt_addr;
Geoff Levand6bb5cf12007-06-16 07:52:02 +1000792 unsigned long aligned_phys = _ALIGN_DOWN(phys_addr, 1 << r->page_size);
793 unsigned long aligned_len = _ALIGN_UP(len + phys_addr - aligned_phys,
794 1 << r->page_size);
795 *bus_addr = dma_sb_lpar_to_bus(r, ps3_mm_phys_to_lpar(phys_addr));
Geoff Levandf58a9d12006-11-23 00:46:51 +0100796
797 if (!USE_DYNAMIC_DMA) {
798 unsigned long lpar_addr = ps3_mm_phys_to_lpar(phys_addr);
799 DBG(" -> %s:%d\n", __func__, __LINE__);
800 DBG("%s:%d virt_addr %lxh\n", __func__, __LINE__,
801 virt_addr);
802 DBG("%s:%d phys_addr %lxh\n", __func__, __LINE__,
803 phys_addr);
804 DBG("%s:%d lpar_addr %lxh\n", __func__, __LINE__,
805 lpar_addr);
806 DBG("%s:%d len %lxh\n", __func__, __LINE__, len);
Stephen Rothwell494fd07a2009-01-13 19:58:10 +0000807 DBG("%s:%d bus_addr %llxh (%lxh)\n", __func__, __LINE__,
Geoff Levandf58a9d12006-11-23 00:46:51 +0100808 *bus_addr, len);
809 }
810
811 spin_lock_irqsave(&r->chunk_list.lock, flags);
812 c = dma_find_chunk(r, *bus_addr, len);
813
814 if (c) {
Geoff Levand6bb5cf12007-06-16 07:52:02 +1000815 DBG("%s:%d: reusing mapped chunk", __func__, __LINE__);
816 dma_dump_chunk(c);
Geoff Levandf58a9d12006-11-23 00:46:51 +0100817 c->usage_count++;
818 spin_unlock_irqrestore(&r->chunk_list.lock, flags);
819 return 0;
820 }
821
Geoff Levand6bb5cf12007-06-16 07:52:02 +1000822 result = dma_sb_map_pages(r, aligned_phys, aligned_len, &c, iopte_flag);
Geoff Levandf58a9d12006-11-23 00:46:51 +0100823
824 if (result) {
825 *bus_addr = 0;
Geoff Levand6bb5cf12007-06-16 07:52:02 +1000826 DBG("%s:%d: dma_sb_map_pages failed (%d)\n",
Geoff Levandf58a9d12006-11-23 00:46:51 +0100827 __func__, __LINE__, result);
828 spin_unlock_irqrestore(&r->chunk_list.lock, flags);
829 return result;
830 }
831
832 c->usage_count = 1;
833
834 spin_unlock_irqrestore(&r->chunk_list.lock, flags);
835 return result;
836}
837
Geoff Levand6bb5cf12007-06-16 07:52:02 +1000838static int dma_ioc0_map_area(struct ps3_dma_region *r, unsigned long virt_addr,
Stephen Rothwell494fd07a2009-01-13 19:58:10 +0000839 unsigned long len, dma_addr_t *bus_addr,
Geoff Levand6bb5cf12007-06-16 07:52:02 +1000840 u64 iopte_flag)
841{
842 int result;
843 unsigned long flags;
844 struct dma_chunk *c;
845 unsigned long phys_addr = is_kernel_addr(virt_addr) ? __pa(virt_addr)
846 : virt_addr;
847 unsigned long aligned_phys = _ALIGN_DOWN(phys_addr, 1 << r->page_size);
848 unsigned long aligned_len = _ALIGN_UP(len + phys_addr - aligned_phys,
849 1 << r->page_size);
850
851 DBG(KERN_ERR "%s: vaddr=%#lx, len=%#lx\n", __func__,
852 virt_addr, len);
853 DBG(KERN_ERR "%s: ph=%#lx a_ph=%#lx a_l=%#lx\n", __func__,
854 phys_addr, aligned_phys, aligned_len);
855
856 spin_lock_irqsave(&r->chunk_list.lock, flags);
857 c = dma_find_chunk_lpar(r, ps3_mm_phys_to_lpar(phys_addr), len);
858
859 if (c) {
860 /* FIXME */
861 BUG();
862 *bus_addr = c->bus_addr + phys_addr - aligned_phys;
863 c->usage_count++;
864 spin_unlock_irqrestore(&r->chunk_list.lock, flags);
865 return 0;
866 }
867
868 result = dma_ioc0_map_pages(r, aligned_phys, aligned_len, &c,
869 iopte_flag);
870
871 if (result) {
872 *bus_addr = 0;
873 DBG("%s:%d: dma_ioc0_map_pages failed (%d)\n",
874 __func__, __LINE__, result);
875 spin_unlock_irqrestore(&r->chunk_list.lock, flags);
876 return result;
877 }
878 *bus_addr = c->bus_addr + phys_addr - aligned_phys;
Stephen Rothwell494fd07a2009-01-13 19:58:10 +0000879 DBG("%s: va=%#lx pa=%#lx a_pa=%#lx bus=%#llx\n", __func__,
Geoff Levand6bb5cf12007-06-16 07:52:02 +1000880 virt_addr, phys_addr, aligned_phys, *bus_addr);
881 c->usage_count = 1;
882
883 spin_unlock_irqrestore(&r->chunk_list.lock, flags);
884 return result;
885}
886
Geoff Levandf58a9d12006-11-23 00:46:51 +0100887/**
Geoff Levand6bb5cf12007-06-16 07:52:02 +1000888 * dma_sb_unmap_area - Unmap an area of memory from a device dma region.
Geoff Levandf58a9d12006-11-23 00:46:51 +0100889 * @r: Pointer to a struct ps3_dma_region.
890 * @bus_addr: The starting ioc bus address of the area to unmap.
891 * @len: Length in bytes of the area to unmap.
892 *
893 * This is the common dma unmap routine.
894 */
895
Stephen Rothwell494fd07a2009-01-13 19:58:10 +0000896static int dma_sb_unmap_area(struct ps3_dma_region *r, dma_addr_t bus_addr,
Geoff Levandf58a9d12006-11-23 00:46:51 +0100897 unsigned long len)
898{
899 unsigned long flags;
900 struct dma_chunk *c;
901
902 spin_lock_irqsave(&r->chunk_list.lock, flags);
903 c = dma_find_chunk(r, bus_addr, len);
904
905 if (!c) {
906 unsigned long aligned_bus = _ALIGN_DOWN(bus_addr,
907 1 << r->page_size);
Geoff Levand6bb5cf12007-06-16 07:52:02 +1000908 unsigned long aligned_len = _ALIGN_UP(len + bus_addr
909 - aligned_bus, 1 << r->page_size);
Stephen Rothwell494fd07a2009-01-13 19:58:10 +0000910 DBG("%s:%d: not found: bus_addr %llxh\n",
Geoff Levandf58a9d12006-11-23 00:46:51 +0100911 __func__, __LINE__, bus_addr);
912 DBG("%s:%d: not found: len %lxh\n",
913 __func__, __LINE__, len);
914 DBG("%s:%d: not found: aligned_bus %lxh\n",
915 __func__, __LINE__, aligned_bus);
916 DBG("%s:%d: not found: aligned_len %lxh\n",
917 __func__, __LINE__, aligned_len);
918 BUG();
919 }
920
921 c->usage_count--;
922
923 if (!c->usage_count) {
924 list_del(&c->link);
Geoff Levand6bb5cf12007-06-16 07:52:02 +1000925 dma_sb_free_chunk(c);
Geoff Levandf58a9d12006-11-23 00:46:51 +0100926 }
927
928 spin_unlock_irqrestore(&r->chunk_list.lock, flags);
929 return 0;
930}
931
Geert Uytterhoeven32f44a12007-06-16 08:07:23 +1000932static int dma_ioc0_unmap_area(struct ps3_dma_region *r,
Stephen Rothwell494fd07a2009-01-13 19:58:10 +0000933 dma_addr_t bus_addr, unsigned long len)
Geoff Levand6bb5cf12007-06-16 07:52:02 +1000934{
935 unsigned long flags;
936 struct dma_chunk *c;
937
Stephen Rothwell494fd07a2009-01-13 19:58:10 +0000938 DBG("%s: start a=%#llx l=%#lx\n", __func__, bus_addr, len);
Geoff Levand6bb5cf12007-06-16 07:52:02 +1000939 spin_lock_irqsave(&r->chunk_list.lock, flags);
940 c = dma_find_chunk(r, bus_addr, len);
941
942 if (!c) {
943 unsigned long aligned_bus = _ALIGN_DOWN(bus_addr,
944 1 << r->page_size);
945 unsigned long aligned_len = _ALIGN_UP(len + bus_addr
946 - aligned_bus,
947 1 << r->page_size);
Stephen Rothwell494fd07a2009-01-13 19:58:10 +0000948 DBG("%s:%d: not found: bus_addr %llxh\n",
Geoff Levand6bb5cf12007-06-16 07:52:02 +1000949 __func__, __LINE__, bus_addr);
950 DBG("%s:%d: not found: len %lxh\n",
951 __func__, __LINE__, len);
952 DBG("%s:%d: not found: aligned_bus %lxh\n",
953 __func__, __LINE__, aligned_bus);
954 DBG("%s:%d: not found: aligned_len %lxh\n",
955 __func__, __LINE__, aligned_len);
956 BUG();
957 }
958
959 c->usage_count--;
960
961 if (!c->usage_count) {
962 list_del(&c->link);
963 dma_ioc0_free_chunk(c);
964 }
965
966 spin_unlock_irqrestore(&r->chunk_list.lock, flags);
967 DBG("%s: end\n", __func__);
968 return 0;
969}
970
Geoff Levandf58a9d12006-11-23 00:46:51 +0100971/**
Geoff Levand6bb5cf12007-06-16 07:52:02 +1000972 * dma_sb_region_create_linear - Setup a linear dma mapping for a device.
Geoff Levandf58a9d12006-11-23 00:46:51 +0100973 * @r: Pointer to a struct ps3_dma_region.
974 *
975 * This routine creates an HV dma region for the device and maps all available
976 * ram into the io controller bus address space.
977 */
978
Geoff Levand6bb5cf12007-06-16 07:52:02 +1000979static int dma_sb_region_create_linear(struct ps3_dma_region *r)
Geoff Levandf58a9d12006-11-23 00:46:51 +0100980{
981 int result;
Stephen Rothwell494fd07a2009-01-13 19:58:10 +0000982 unsigned long virt_addr, len;
983 dma_addr_t tmp;
Geoff Levandf58a9d12006-11-23 00:46:51 +0100984
Geoff Levand6bb5cf12007-06-16 07:52:02 +1000985 if (r->len > 16*1024*1024) { /* FIXME: need proper fix */
986 /* force 16M dma pages for linear mapping */
987 if (r->page_size != PS3_DMA_16M) {
988 pr_info("%s:%d: forcing 16M pages for linear map\n",
989 __func__, __LINE__);
990 r->page_size = PS3_DMA_16M;
991 r->len = _ALIGN_UP(r->len, 1 << r->page_size);
992 }
Geoff Levandf58a9d12006-11-23 00:46:51 +0100993 }
994
Geoff Levand6bb5cf12007-06-16 07:52:02 +1000995 result = dma_sb_region_create(r);
Geoff Levandf58a9d12006-11-23 00:46:51 +0100996 BUG_ON(result);
997
Geoff Levand6bb5cf12007-06-16 07:52:02 +1000998 if (r->offset < map.rm.size) {
999 /* Map (part of) 1st RAM chunk */
1000 virt_addr = map.rm.base + r->offset;
1001 len = map.rm.size - r->offset;
1002 if (len > r->len)
1003 len = r->len;
1004 result = dma_sb_map_area(r, virt_addr, len, &tmp,
Geert Uytterhoeven5c6fc8d2009-06-10 04:38:45 +00001005 CBE_IOPTE_PP_W | CBE_IOPTE_PP_R | CBE_IOPTE_SO_RW |
1006 CBE_IOPTE_M);
Geoff Levand6bb5cf12007-06-16 07:52:02 +10001007 BUG_ON(result);
1008 }
Geoff Levandf58a9d12006-11-23 00:46:51 +01001009
Geoff Levand6bb5cf12007-06-16 07:52:02 +10001010 if (r->offset + r->len > map.rm.size) {
1011 /* Map (part of) 2nd RAM chunk */
Geoff Levanda628df12008-01-19 07:32:59 +11001012 virt_addr = map.rm.size;
Geoff Levand6bb5cf12007-06-16 07:52:02 +10001013 len = r->len;
1014 if (r->offset >= map.rm.size)
1015 virt_addr += r->offset - map.rm.size;
1016 else
1017 len -= map.rm.size - r->offset;
1018 result = dma_sb_map_area(r, virt_addr, len, &tmp,
Geert Uytterhoeven5c6fc8d2009-06-10 04:38:45 +00001019 CBE_IOPTE_PP_W | CBE_IOPTE_PP_R | CBE_IOPTE_SO_RW |
1020 CBE_IOPTE_M);
Geoff Levand6bb5cf12007-06-16 07:52:02 +10001021 BUG_ON(result);
1022 }
Geoff Levandf58a9d12006-11-23 00:46:51 +01001023
1024 return result;
1025}
1026
1027/**
Geoff Levand6bb5cf12007-06-16 07:52:02 +10001028 * dma_sb_region_free_linear - Free a linear dma mapping for a device.
Geoff Levandf58a9d12006-11-23 00:46:51 +01001029 * @r: Pointer to a struct ps3_dma_region.
1030 *
1031 * This routine will unmap all mapped areas and free the HV dma region.
1032 */
1033
Geoff Levand6bb5cf12007-06-16 07:52:02 +10001034static int dma_sb_region_free_linear(struct ps3_dma_region *r)
Geoff Levandf58a9d12006-11-23 00:46:51 +01001035{
1036 int result;
Stephen Rothwell494fd07a2009-01-13 19:58:10 +00001037 dma_addr_t bus_addr;
1038 unsigned long len, lpar_addr;
Geoff Levandf58a9d12006-11-23 00:46:51 +01001039
Geoff Levand6bb5cf12007-06-16 07:52:02 +10001040 if (r->offset < map.rm.size) {
1041 /* Unmap (part of) 1st RAM chunk */
1042 lpar_addr = map.rm.base + r->offset;
1043 len = map.rm.size - r->offset;
1044 if (len > r->len)
1045 len = r->len;
1046 bus_addr = dma_sb_lpar_to_bus(r, lpar_addr);
1047 result = dma_sb_unmap_area(r, bus_addr, len);
1048 BUG_ON(result);
1049 }
Geoff Levandf58a9d12006-11-23 00:46:51 +01001050
Geoff Levand6bb5cf12007-06-16 07:52:02 +10001051 if (r->offset + r->len > map.rm.size) {
1052 /* Unmap (part of) 2nd RAM chunk */
1053 lpar_addr = map.r1.base;
1054 len = r->len;
1055 if (r->offset >= map.rm.size)
1056 lpar_addr += r->offset - map.rm.size;
1057 else
1058 len -= map.rm.size - r->offset;
1059 bus_addr = dma_sb_lpar_to_bus(r, lpar_addr);
1060 result = dma_sb_unmap_area(r, bus_addr, len);
1061 BUG_ON(result);
1062 }
Geoff Levandf58a9d12006-11-23 00:46:51 +01001063
Geoff Levand6bb5cf12007-06-16 07:52:02 +10001064 result = dma_sb_region_free(r);
Geoff Levandf58a9d12006-11-23 00:46:51 +01001065 BUG_ON(result);
1066
1067 return result;
1068}
1069
1070/**
Geoff Levand6bb5cf12007-06-16 07:52:02 +10001071 * dma_sb_map_area_linear - Map an area of memory into a device dma region.
Geoff Levandf58a9d12006-11-23 00:46:51 +01001072 * @r: Pointer to a struct ps3_dma_region.
1073 * @virt_addr: Starting virtual address of the area to map.
1074 * @len: Length in bytes of the area to map.
1075 * @bus_addr: A pointer to return the starting ioc bus address of the area to
1076 * map.
1077 *
Geoff Levand6bb5cf12007-06-16 07:52:02 +10001078 * This routine just returns the corresponding bus address. Actual mapping
Geoff Levandf58a9d12006-11-23 00:46:51 +01001079 * occurs in dma_region_create_linear().
1080 */
1081
Geoff Levand6bb5cf12007-06-16 07:52:02 +10001082static int dma_sb_map_area_linear(struct ps3_dma_region *r,
Stephen Rothwell494fd07a2009-01-13 19:58:10 +00001083 unsigned long virt_addr, unsigned long len, dma_addr_t *bus_addr,
Geoff Levand6bb5cf12007-06-16 07:52:02 +10001084 u64 iopte_flag)
Geoff Levandf58a9d12006-11-23 00:46:51 +01001085{
1086 unsigned long phys_addr = is_kernel_addr(virt_addr) ? __pa(virt_addr)
1087 : virt_addr;
Geoff Levand6bb5cf12007-06-16 07:52:02 +10001088 *bus_addr = dma_sb_lpar_to_bus(r, ps3_mm_phys_to_lpar(phys_addr));
Geoff Levandf58a9d12006-11-23 00:46:51 +01001089 return 0;
1090}
1091
1092/**
1093 * dma_unmap_area_linear - Unmap an area of memory from a device dma region.
1094 * @r: Pointer to a struct ps3_dma_region.
1095 * @bus_addr: The starting ioc bus address of the area to unmap.
1096 * @len: Length in bytes of the area to unmap.
1097 *
Geoff Levand6bb5cf12007-06-16 07:52:02 +10001098 * This routine does nothing. Unmapping occurs in dma_sb_region_free_linear().
Geoff Levandf58a9d12006-11-23 00:46:51 +01001099 */
1100
Geoff Levand6bb5cf12007-06-16 07:52:02 +10001101static int dma_sb_unmap_area_linear(struct ps3_dma_region *r,
Stephen Rothwell494fd07a2009-01-13 19:58:10 +00001102 dma_addr_t bus_addr, unsigned long len)
Geoff Levandf58a9d12006-11-23 00:46:51 +01001103{
1104 return 0;
Geoff Levand6bb5cf12007-06-16 07:52:02 +10001105};
1106
1107static const struct ps3_dma_region_ops ps3_dma_sb_region_ops = {
1108 .create = dma_sb_region_create,
1109 .free = dma_sb_region_free,
1110 .map = dma_sb_map_area,
1111 .unmap = dma_sb_unmap_area
1112};
1113
1114static const struct ps3_dma_region_ops ps3_dma_sb_region_linear_ops = {
1115 .create = dma_sb_region_create_linear,
1116 .free = dma_sb_region_free_linear,
1117 .map = dma_sb_map_area_linear,
1118 .unmap = dma_sb_unmap_area_linear
1119};
1120
1121static const struct ps3_dma_region_ops ps3_dma_ioc0_region_ops = {
1122 .create = dma_ioc0_region_create,
1123 .free = dma_ioc0_region_free,
1124 .map = dma_ioc0_map_area,
1125 .unmap = dma_ioc0_unmap_area
1126};
1127
1128int ps3_dma_region_init(struct ps3_system_bus_device *dev,
1129 struct ps3_dma_region *r, enum ps3_dma_page_size page_size,
1130 enum ps3_dma_region_type region_type, void *addr, unsigned long len)
1131{
1132 unsigned long lpar_addr;
1133
1134 lpar_addr = addr ? ps3_mm_phys_to_lpar(__pa(addr)) : 0;
1135
1136 r->dev = dev;
1137 r->page_size = page_size;
1138 r->region_type = region_type;
1139 r->offset = lpar_addr;
1140 if (r->offset >= map.rm.size)
1141 r->offset -= map.r1.offset;
1142 r->len = len ? len : _ALIGN_UP(map.total, 1 << r->page_size);
1143
1144 switch (dev->dev_type) {
1145 case PS3_DEVICE_TYPE_SB:
1146 r->region_ops = (USE_DYNAMIC_DMA)
1147 ? &ps3_dma_sb_region_ops
1148 : &ps3_dma_sb_region_linear_ops;
1149 break;
1150 case PS3_DEVICE_TYPE_IOC0:
1151 r->region_ops = &ps3_dma_ioc0_region_ops;
1152 break;
1153 default:
1154 BUG();
1155 return -EINVAL;
1156 }
1157 return 0;
Geoff Levandf58a9d12006-11-23 00:46:51 +01001158}
Geoff Levand6bb5cf12007-06-16 07:52:02 +10001159EXPORT_SYMBOL(ps3_dma_region_init);
Geoff Levandf58a9d12006-11-23 00:46:51 +01001160
1161int ps3_dma_region_create(struct ps3_dma_region *r)
1162{
Geoff Levand6bb5cf12007-06-16 07:52:02 +10001163 BUG_ON(!r);
1164 BUG_ON(!r->region_ops);
1165 BUG_ON(!r->region_ops->create);
1166 return r->region_ops->create(r);
Geoff Levandf58a9d12006-11-23 00:46:51 +01001167}
Geoff Levand6bb5cf12007-06-16 07:52:02 +10001168EXPORT_SYMBOL(ps3_dma_region_create);
Geoff Levandf58a9d12006-11-23 00:46:51 +01001169
1170int ps3_dma_region_free(struct ps3_dma_region *r)
1171{
Geoff Levand6bb5cf12007-06-16 07:52:02 +10001172 BUG_ON(!r);
1173 BUG_ON(!r->region_ops);
1174 BUG_ON(!r->region_ops->free);
1175 return r->region_ops->free(r);
Geoff Levandf58a9d12006-11-23 00:46:51 +01001176}
Geoff Levand6bb5cf12007-06-16 07:52:02 +10001177EXPORT_SYMBOL(ps3_dma_region_free);
Geoff Levandf58a9d12006-11-23 00:46:51 +01001178
1179int ps3_dma_map(struct ps3_dma_region *r, unsigned long virt_addr,
Stephen Rothwell494fd07a2009-01-13 19:58:10 +00001180 unsigned long len, dma_addr_t *bus_addr,
Geoff Levand6bb5cf12007-06-16 07:52:02 +10001181 u64 iopte_flag)
Geoff Levandf58a9d12006-11-23 00:46:51 +01001182{
Geoff Levand6bb5cf12007-06-16 07:52:02 +10001183 return r->region_ops->map(r, virt_addr, len, bus_addr, iopte_flag);
Geoff Levandf58a9d12006-11-23 00:46:51 +01001184}
1185
Stephen Rothwell494fd07a2009-01-13 19:58:10 +00001186int ps3_dma_unmap(struct ps3_dma_region *r, dma_addr_t bus_addr,
Geoff Levandf58a9d12006-11-23 00:46:51 +01001187 unsigned long len)
1188{
Geoff Levand6bb5cf12007-06-16 07:52:02 +10001189 return r->region_ops->unmap(r, bus_addr, len);
Geoff Levandf58a9d12006-11-23 00:46:51 +01001190}
1191
1192/*============================================================================*/
1193/* system startup routines */
1194/*============================================================================*/
1195
1196/**
1197 * ps3_mm_init - initialize the address space state variables
1198 */
1199
1200void __init ps3_mm_init(void)
1201{
1202 int result;
1203
1204 DBG(" -> %s:%d\n", __func__, __LINE__);
1205
1206 result = ps3_repository_read_mm_info(&map.rm.base, &map.rm.size,
1207 &map.total);
1208
1209 if (result)
1210 panic("ps3_repository_read_mm_info() failed");
1211
1212 map.rm.offset = map.rm.base;
1213 map.vas_id = map.htab_size = 0;
1214
1215 /* this implementation assumes map.rm.base is zero */
1216
1217 BUG_ON(map.rm.base);
1218 BUG_ON(!map.rm.size);
1219
Geoff Levandf58a9d12006-11-23 00:46:51 +01001220
1221 /* arrange to do this in ps3_mm_add_memory */
1222 ps3_mm_region_create(&map.r1, map.total - map.rm.size);
1223
Geoff Levand6bb5cf12007-06-16 07:52:02 +10001224 /* correct map.total for the real total amount of memory we use */
1225 map.total = map.rm.size + map.r1.size;
1226
Geoff Levandf58a9d12006-11-23 00:46:51 +01001227 DBG(" <- %s:%d\n", __func__, __LINE__);
1228}
1229
1230/**
1231 * ps3_mm_shutdown - final cleanup of address space
1232 */
1233
1234void ps3_mm_shutdown(void)
1235{
1236 ps3_mm_region_destroy(&map.r1);
Geoff Levandf58a9d12006-11-23 00:46:51 +01001237}