1 From cffb4add03b1fc83026b06dc3664279cfbf70155 Mon Sep 17 00:00:00 2001
2 From: Jim Paris <jim@jtan.com>
3 Date: Tue, 6 Jan 2009 11:32:10 +0000
4 Subject: [PATCH] mtd/ps3vram: Add ps3vram driver for accessing video RAM as MTD
6 Add ps3vram driver, which exposes unused video RAM on the PS3 as a MTD
7 device suitable for storage or swap. Fast data transfer is achieved
8 using a local cache in system RAM and DMA transfers via the GPU.
10 Signed-off-by: Vivien Chappelier <vivien.chappelier@free.fr>
11 Signed-off-by: Jim Paris <jim@jtan.com>
12 Acked-by: Geoff Levand <geoffrey.levand@am.sony.com>
13 Acked-by: David Woodhouse <David.Woodhouse@intel.com>
14 Signed-off-by: Benjamin Herrenschmidt <benh@kernel.crashing.org>
17 arch/powerpc/include/asm/ps3.h | 1 +
18 arch/powerpc/platforms/ps3/device-init.c | 37 ++
19 drivers/mtd/devices/Kconfig | 7 +
20 drivers/mtd/devices/Makefile | 1 +
21 drivers/mtd/devices/ps3vram.c | 776 ++++++++++++++++++++++++++++++
22 6 files changed, 828 insertions(+), 0 deletions(-)
23 create mode 100644 drivers/mtd/devices/ps3vram.c
25 diff --git a/MAINTAINERS b/MAINTAINERS
26 index a018844..246878f 100644
29 @@ -3484,6 +3484,12 @@ L: linuxppc-dev@ozlabs.org
30 L: cbe-oss-dev@ozlabs.org
36 +L: cbe-oss-dev@ozlabs.org
39 PVRUSB2 VIDEO4LINUX DRIVER
42 diff --git a/arch/powerpc/include/asm/ps3.h b/arch/powerpc/include/asm/ps3.h
43 index cff30c0..66b6505 100644
44 --- a/arch/powerpc/include/asm/ps3.h
45 +++ b/arch/powerpc/include/asm/ps3.h
46 @@ -320,6 +320,7 @@ enum ps3_match_id {
48 enum ps3_match_sub_id {
49 PS3_MATCH_SUB_ID_GPU_FB = 1,
50 + PS3_MATCH_SUB_ID_GPU_RAMDISK = 2,
53 #define PS3_MODULE_ALIAS_EHCI "ps3:1:0"
54 diff --git a/arch/powerpc/platforms/ps3/device-init.c b/arch/powerpc/platforms/ps3/device-init.c
55 index dbc124e..ca71a12 100644
56 --- a/arch/powerpc/platforms/ps3/device-init.c
57 +++ b/arch/powerpc/platforms/ps3/device-init.c
58 @@ -518,6 +518,41 @@ fail_device_register:
62 +static int __init ps3_register_ramdisk_device(void)
66 + struct ps3_system_bus_device dev;
69 + pr_debug(" -> %s:%d\n", __func__, __LINE__);
71 + p = kzalloc(sizeof(struct layout), GFP_KERNEL);
76 + p->dev.match_id = PS3_MATCH_ID_GPU;
77 + p->dev.match_sub_id = PS3_MATCH_SUB_ID_GPU_RAMDISK;
78 + p->dev.dev_type = PS3_DEVICE_TYPE_IOC0;
80 + result = ps3_system_bus_device_register(&p->dev);
83 + pr_debug("%s:%d ps3_system_bus_device_register failed\n",
84 + __func__, __LINE__);
85 + goto fail_device_register;
88 + pr_debug(" <- %s:%d\n", __func__, __LINE__);
91 +fail_device_register:
93 + pr_debug(" <- %s:%d failed\n", __func__, __LINE__);
98 * ps3_setup_dynamic_device - Setup a dynamic device from the repository
100 @@ -946,6 +981,8 @@ static int __init ps3_register_devices(void)
102 ps3_register_lpm_devices();
104 + ps3_register_ramdisk_device();
106 pr_debug(" <- %s:%d\n", __func__, __LINE__);
109 diff --git a/drivers/mtd/devices/Kconfig b/drivers/mtd/devices/Kconfig
110 index 6fde0a2..bc33200 100644
111 --- a/drivers/mtd/devices/Kconfig
112 +++ b/drivers/mtd/devices/Kconfig
113 @@ -120,6 +120,13 @@ config MTD_PHRAM
114 doesn't have access to, memory beyond the mem=xxx limit, nvram,
115 memory on the video card, etc...
118 + tristate "PS3 video RAM"
121 + This driver allows you to use excess PS3 video RAM as volatile
122 + storage or system swap.
125 tristate "28F160xx flash driver for LART"
126 depends on SA1100_LART
127 diff --git a/drivers/mtd/devices/Makefile b/drivers/mtd/devices/Makefile
128 index 0993d5c..e51521d 100644
129 --- a/drivers/mtd/devices/Makefile
130 +++ b/drivers/mtd/devices/Makefile
131 @@ -16,3 +16,4 @@ obj-$(CONFIG_MTD_LART) += lart.o
132 obj-$(CONFIG_MTD_BLOCK2MTD) += block2mtd.o
133 obj-$(CONFIG_MTD_DATAFLASH) += mtd_dataflash.o
134 obj-$(CONFIG_MTD_M25P80) += m25p80.o
135 +obj-$(CONFIG_MTD_PS3VRAM) += ps3vram.o
136 diff --git a/drivers/mtd/devices/ps3vram.c b/drivers/mtd/devices/ps3vram.c
138 index 0000000..26a4b57
140 +++ b/drivers/mtd/devices/ps3vram.c
143 + * ps3vram - Use extra PS3 video ram as MTD block device.
145 + * Copyright (c) 2007-2008 Jim Paris <jim@jtan.com>
146 + * Added support RSX DMA Vivien Chappelier <vivien.chappelier@free.fr>
149 +#include <linux/io.h>
150 +#include <linux/init.h>
151 +#include <linux/kernel.h>
152 +#include <linux/list.h>
153 +#include <linux/module.h>
154 +#include <linux/moduleparam.h>
155 +#include <linux/slab.h>
156 +#include <linux/version.h>
157 +#include <linux/gfp.h>
158 +#include <linux/delay.h>
159 +#include <linux/mtd/mtd.h>
161 +#include <asm/lv1call.h>
162 +#include <asm/ps3.h>
164 +#define DEVICE_NAME "ps3vram"
166 +#define XDR_BUF_SIZE (2 * 1024 * 1024) /* XDR buffer (must be 1MiB aligned) */
167 +#define XDR_IOIF 0x0c000000
169 +#define FIFO_BASE XDR_IOIF
170 +#define FIFO_SIZE (64 * 1024)
172 +#define DMA_PAGE_SIZE (4 * 1024)
174 +#define CACHE_PAGE_SIZE (256 * 1024)
175 +#define CACHE_PAGE_COUNT ((XDR_BUF_SIZE - FIFO_SIZE) / CACHE_PAGE_SIZE)
177 +#define CACHE_OFFSET CACHE_PAGE_SIZE
178 +#define FIFO_OFFSET 0
180 +#define CTRL_PUT 0x10
181 +#define CTRL_GET 0x11
182 +#define CTRL_TOP 0x15
184 +#define UPLOAD_SUBCH 1
185 +#define DOWNLOAD_SUBCH 2
187 +#define NV_MEMORY_TO_MEMORY_FORMAT_OFFSET_IN 0x0000030c
188 +#define NV_MEMORY_TO_MEMORY_FORMAT_NOTIFY 0x00000104
190 +#define L1GPU_CONTEXT_ATTRIBUTE_FB_BLIT 0x601
192 +struct mtd_info ps3vram_mtd;
194 +#define CACHE_PAGE_PRESENT 1
195 +#define CACHE_PAGE_DIRTY 2
197 +#define dbg(fmt, args...) \
198 + pr_debug("%s:%d " fmt "\n", __func__, __LINE__, ## args)
200 +struct ps3vram_tag {
201 + unsigned int address;
202 + unsigned int flags;
205 +struct ps3vram_cache {
206 + unsigned int page_count;
207 + unsigned int page_size;
208 + struct ps3vram_tag *tags;
211 +struct ps3vram_priv {
212 + uint64_t memory_handle;
213 + uint64_t context_handle;
219 + uint32_t *fifo_base;
220 + uint32_t *fifo_ptr;
222 + struct ps3vram_cache cache;
224 + /* Used to serialize cache/DMA operations */
228 +#define DMA_NOTIFIER_HANDLE_BASE 0x66604200 /* first DMA notifier handle */
229 +#define DMA_NOTIFIER_OFFSET_BASE 0x1000 /* first DMA notifier offset */
230 +#define DMA_NOTIFIER_SIZE 0x40
232 +#define NUM_NOTIFIERS 16
234 +#define NOTIFIER 7 /* notifier used for completion report */
236 +/* A trailing '-' means to subtract off ps3fb_videomemory.size */
237 +char *size = "256M-";
238 +module_param(size, charp, 0);
239 +MODULE_PARM_DESC(size, "memory size");
241 +static inline uint32_t *ps3vram_get_notifier(uint32_t *reports, int notifier)
243 + return (void *) reports +
244 + DMA_NOTIFIER_OFFSET_BASE +
245 + DMA_NOTIFIER_SIZE * notifier;
248 +static void ps3vram_notifier_reset(struct mtd_info *mtd)
251 + struct ps3vram_priv *priv = mtd->priv;
252 + uint32_t *notify = ps3vram_get_notifier(priv->reports, NOTIFIER);
253 + for (i = 0; i < 4; i++)
254 + notify[i] = 0xffffffff;
257 +static int ps3vram_notifier_wait(struct mtd_info *mtd, int timeout_ms)
259 + struct ps3vram_priv *priv = mtd->priv;
260 + uint32_t *notify = ps3vram_get_notifier(priv->reports, NOTIFIER);
262 + timeout_ms *= 1000;
265 + if (notify[3] == 0)
270 + } while (timeout_ms--);
275 +static void ps3vram_dump_ring(struct mtd_info *mtd)
277 + struct ps3vram_priv *priv = mtd->priv;
280 + pr_info("PUT = %08x GET = %08x\n", priv->ctrl[CTRL_PUT],
281 + priv->ctrl[CTRL_GET]);
282 + for (fifo = priv->fifo_base; fifo < priv->fifo_ptr; fifo++)
283 + pr_info("%p: %08x\n", fifo, *fifo);
286 +static void ps3vram_dump_reports(struct mtd_info *mtd)
288 + struct ps3vram_priv *priv = mtd->priv;
291 + for (i = 0; i < NUM_NOTIFIERS; i++) {
292 + uint32_t *n = ps3vram_get_notifier(priv->reports, i);
293 + pr_info("%p: %08x\n", n, *n);
297 +static void ps3vram_init_ring(struct mtd_info *mtd)
299 + struct ps3vram_priv *priv = mtd->priv;
301 + priv->ctrl[CTRL_PUT] = FIFO_BASE + FIFO_OFFSET;
302 + priv->ctrl[CTRL_GET] = FIFO_BASE + FIFO_OFFSET;
305 +static int ps3vram_wait_ring(struct mtd_info *mtd, int timeout)
307 + struct ps3vram_priv *priv = mtd->priv;
309 + /* wait until setup commands are processed */
311 + while (--timeout) {
312 + if (priv->ctrl[CTRL_PUT] == priv->ctrl[CTRL_GET])
316 + if (timeout == 0) {
317 + pr_err("FIFO timeout (%08x/%08x/%08x)\n", priv->ctrl[CTRL_PUT],
318 + priv->ctrl[CTRL_GET], priv->ctrl[CTRL_TOP]);
325 +static inline void ps3vram_out_ring(struct ps3vram_priv *priv, uint32_t data)
327 + *(priv->fifo_ptr)++ = data;
330 +static inline void ps3vram_begin_ring(struct ps3vram_priv *priv, uint32_t chan,
331 + uint32_t tag, uint32_t size)
333 + ps3vram_out_ring(priv, (size << 18) | (chan << 13) | tag);
336 +static void ps3vram_rewind_ring(struct mtd_info *mtd)
338 + struct ps3vram_priv *priv = mtd->priv;
341 + ps3vram_out_ring(priv, 0x20000000 | (FIFO_BASE + FIFO_OFFSET));
343 + priv->ctrl[CTRL_PUT] = FIFO_BASE + FIFO_OFFSET;
345 + /* asking the HV for a blit will kick the fifo */
346 + status = lv1_gpu_context_attribute(priv->context_handle,
347 + L1GPU_CONTEXT_ATTRIBUTE_FB_BLIT,
350 + pr_err("ps3vram: lv1_gpu_context_attribute FB_BLIT failed\n");
352 + priv->fifo_ptr = priv->fifo_base;
355 +static void ps3vram_fire_ring(struct mtd_info *mtd)
357 + struct ps3vram_priv *priv = mtd->priv;
360 + mutex_lock(&ps3_gpu_mutex);
362 + priv->ctrl[CTRL_PUT] = FIFO_BASE + FIFO_OFFSET +
363 + (priv->fifo_ptr - priv->fifo_base) * sizeof(uint32_t);
365 + /* asking the HV for a blit will kick the fifo */
366 + status = lv1_gpu_context_attribute(priv->context_handle,
367 + L1GPU_CONTEXT_ATTRIBUTE_FB_BLIT,
370 + pr_err("ps3vram: lv1_gpu_context_attribute FB_BLIT failed\n");
372 + if ((priv->fifo_ptr - priv->fifo_base) * sizeof(uint32_t) >
373 + FIFO_SIZE - 1024) {
374 + dbg("fifo full, rewinding");
375 + ps3vram_wait_ring(mtd, 200);
376 + ps3vram_rewind_ring(mtd);
379 + mutex_unlock(&ps3_gpu_mutex);
382 +static void ps3vram_bind(struct mtd_info *mtd)
384 + struct ps3vram_priv *priv = mtd->priv;
386 + ps3vram_begin_ring(priv, UPLOAD_SUBCH, 0, 1);
387 + ps3vram_out_ring(priv, 0x31337303);
388 + ps3vram_begin_ring(priv, UPLOAD_SUBCH, 0x180, 3);
389 + ps3vram_out_ring(priv, DMA_NOTIFIER_HANDLE_BASE + NOTIFIER);
390 + ps3vram_out_ring(priv, 0xfeed0001); /* DMA system RAM instance */
391 + ps3vram_out_ring(priv, 0xfeed0000); /* DMA video RAM instance */
393 + ps3vram_begin_ring(priv, DOWNLOAD_SUBCH, 0, 1);
394 + ps3vram_out_ring(priv, 0x3137c0de);
395 + ps3vram_begin_ring(priv, DOWNLOAD_SUBCH, 0x180, 3);
396 + ps3vram_out_ring(priv, DMA_NOTIFIER_HANDLE_BASE + NOTIFIER);
397 + ps3vram_out_ring(priv, 0xfeed0000); /* DMA video RAM instance */
398 + ps3vram_out_ring(priv, 0xfeed0001); /* DMA system RAM instance */
400 + ps3vram_fire_ring(mtd);
403 +static int ps3vram_upload(struct mtd_info *mtd, unsigned int src_offset,
404 + unsigned int dst_offset, int len, int count)
406 + struct ps3vram_priv *priv = mtd->priv;
408 + ps3vram_begin_ring(priv, UPLOAD_SUBCH,
409 + NV_MEMORY_TO_MEMORY_FORMAT_OFFSET_IN, 8);
410 + ps3vram_out_ring(priv, XDR_IOIF + src_offset);
411 + ps3vram_out_ring(priv, dst_offset);
412 + ps3vram_out_ring(priv, len);
413 + ps3vram_out_ring(priv, len);
414 + ps3vram_out_ring(priv, len);
415 + ps3vram_out_ring(priv, count);
416 + ps3vram_out_ring(priv, (1 << 8) | 1);
417 + ps3vram_out_ring(priv, 0);
419 + ps3vram_notifier_reset(mtd);
420 + ps3vram_begin_ring(priv, UPLOAD_SUBCH,
421 + NV_MEMORY_TO_MEMORY_FORMAT_NOTIFY, 1);
422 + ps3vram_out_ring(priv, 0);
423 + ps3vram_begin_ring(priv, UPLOAD_SUBCH, 0x100, 1);
424 + ps3vram_out_ring(priv, 0);
425 + ps3vram_fire_ring(mtd);
426 + if (ps3vram_notifier_wait(mtd, 200) < 0) {
427 + pr_err("notifier timeout\n");
428 + ps3vram_dump_ring(mtd);
429 + ps3vram_dump_reports(mtd);
436 +static int ps3vram_download(struct mtd_info *mtd, unsigned int src_offset,
437 + unsigned int dst_offset, int len, int count)
439 + struct ps3vram_priv *priv = mtd->priv;
441 + ps3vram_begin_ring(priv, DOWNLOAD_SUBCH,
442 + NV_MEMORY_TO_MEMORY_FORMAT_OFFSET_IN, 8);
443 + ps3vram_out_ring(priv, src_offset);
444 + ps3vram_out_ring(priv, XDR_IOIF + dst_offset);
445 + ps3vram_out_ring(priv, len);
446 + ps3vram_out_ring(priv, len);
447 + ps3vram_out_ring(priv, len);
448 + ps3vram_out_ring(priv, count);
449 + ps3vram_out_ring(priv, (1 << 8) | 1);
450 + ps3vram_out_ring(priv, 0);
452 + ps3vram_notifier_reset(mtd);
453 + ps3vram_begin_ring(priv, DOWNLOAD_SUBCH,
454 + NV_MEMORY_TO_MEMORY_FORMAT_NOTIFY, 1);
455 + ps3vram_out_ring(priv, 0);
456 + ps3vram_begin_ring(priv, DOWNLOAD_SUBCH, 0x100, 1);
457 + ps3vram_out_ring(priv, 0);
458 + ps3vram_fire_ring(mtd);
459 + if (ps3vram_notifier_wait(mtd, 200) < 0) {
460 + pr_err("notifier timeout\n");
461 + ps3vram_dump_ring(mtd);
462 + ps3vram_dump_reports(mtd);
469 +static void ps3vram_cache_evict(struct mtd_info *mtd, int entry)
471 + struct ps3vram_priv *priv = mtd->priv;
472 + struct ps3vram_cache *cache = &priv->cache;
474 + if (cache->tags[entry].flags & CACHE_PAGE_DIRTY) {
475 + dbg("flushing %d : 0x%08x", entry, cache->tags[entry].address);
476 + if (ps3vram_upload(mtd,
477 + CACHE_OFFSET + entry * cache->page_size,
478 + cache->tags[entry].address,
480 + cache->page_size / DMA_PAGE_SIZE) < 0) {
481 + pr_err("failed to upload from 0x%x to 0x%x size 0x%x\n",
482 + entry * cache->page_size,
483 + cache->tags[entry].address,
486 + cache->tags[entry].flags &= ~CACHE_PAGE_DIRTY;
490 +static void ps3vram_cache_load(struct mtd_info *mtd, int entry,
491 + unsigned int address)
493 + struct ps3vram_priv *priv = mtd->priv;
494 + struct ps3vram_cache *cache = &priv->cache;
496 + dbg("fetching %d : 0x%08x", entry, address);
497 + if (ps3vram_download(mtd,
499 + CACHE_OFFSET + entry * cache->page_size,
501 + cache->page_size / DMA_PAGE_SIZE) < 0) {
502 + pr_err("failed to download from 0x%x to 0x%x size 0x%x\n",
504 + entry * cache->page_size,
508 + cache->tags[entry].address = address;
509 + cache->tags[entry].flags |= CACHE_PAGE_PRESENT;
513 +static void ps3vram_cache_flush(struct mtd_info *mtd)
515 + struct ps3vram_priv *priv = mtd->priv;
516 + struct ps3vram_cache *cache = &priv->cache;
520 + for (i = 0; i < cache->page_count; i++) {
521 + ps3vram_cache_evict(mtd, i);
522 + cache->tags[i].flags = 0;
526 +static unsigned int ps3vram_cache_match(struct mtd_info *mtd, loff_t address)
528 + struct ps3vram_priv *priv = mtd->priv;
529 + struct ps3vram_cache *cache = &priv->cache;
531 + unsigned int offset;
533 + static int counter;
535 + offset = (unsigned int) (address & (cache->page_size - 1));
536 + base = (unsigned int) (address - offset);
538 + /* fully associative check */
539 + for (i = 0; i < cache->page_count; i++) {
540 + if ((cache->tags[i].flags & CACHE_PAGE_PRESENT) &&
541 + cache->tags[i].address == base) {
542 + dbg("found entry %d : 0x%08x",
543 + i, cache->tags[i].address);
548 + /* choose a random entry */
549 + i = (jiffies + (counter++)) % cache->page_count;
550 + dbg("using cache entry %d", i);
552 + ps3vram_cache_evict(mtd, i);
553 + ps3vram_cache_load(mtd, i, base);
558 +static int ps3vram_cache_init(struct mtd_info *mtd)
560 + struct ps3vram_priv *priv = mtd->priv;
562 + pr_info("creating cache: %d entries, %d bytes pages\n",
563 + CACHE_PAGE_COUNT, CACHE_PAGE_SIZE);
565 + priv->cache.page_count = CACHE_PAGE_COUNT;
566 + priv->cache.page_size = CACHE_PAGE_SIZE;
567 + priv->cache.tags = kzalloc(sizeof(struct ps3vram_tag) *
568 + CACHE_PAGE_COUNT, GFP_KERNEL);
569 + if (priv->cache.tags == NULL) {
570 + pr_err("could not allocate cache tags\n");
577 +static void ps3vram_cache_cleanup(struct mtd_info *mtd)
579 + struct ps3vram_priv *priv = mtd->priv;
581 + ps3vram_cache_flush(mtd);
582 + kfree(priv->cache.tags);
585 +static int ps3vram_erase(struct mtd_info *mtd, struct erase_info *instr)
587 + struct ps3vram_priv *priv = mtd->priv;
589 + if (instr->addr + instr->len > mtd->size)
592 + mutex_lock(&priv->lock);
594 + ps3vram_cache_flush(mtd);
596 + /* Set bytes to 0xFF */
597 + memset(priv->base + instr->addr, 0xFF, instr->len);
599 + mutex_unlock(&priv->lock);
601 + instr->state = MTD_ERASE_DONE;
602 + mtd_erase_callback(instr);
608 +static int ps3vram_read(struct mtd_info *mtd, loff_t from, size_t len,
609 + size_t *retlen, u_char *buf)
611 + struct ps3vram_priv *priv = mtd->priv;
612 + unsigned int cached, count;
614 + dbg("from = 0x%08x len = 0x%zx", (unsigned int) from, len);
616 + if (from >= mtd->size)
619 + if (len > mtd->size - from)
620 + len = mtd->size - from;
622 + /* Copy from vram to buf */
625 + unsigned int offset, avail;
626 + unsigned int entry;
628 + offset = (unsigned int) (from & (priv->cache.page_size - 1));
629 + avail = priv->cache.page_size - offset;
631 + mutex_lock(&priv->lock);
633 + entry = ps3vram_cache_match(mtd, from);
634 + cached = CACHE_OFFSET + entry * priv->cache.page_size + offset;
636 + dbg("from=%08x cached=%08x offset=%08x avail=%08x count=%08x",
637 + (unsigned)from, cached, offset, avail, count);
641 + memcpy(buf, priv->xdr_buf + cached, avail);
643 + mutex_unlock(&priv->lock);
654 +static int ps3vram_write(struct mtd_info *mtd, loff_t to, size_t len,
655 + size_t *retlen, const u_char *buf)
657 + struct ps3vram_priv *priv = mtd->priv;
658 + unsigned int cached, count;
660 + if (to >= mtd->size)
663 + if (len > mtd->size - to)
664 + len = mtd->size - to;
666 + /* Copy from buf to vram */
669 + unsigned int offset, avail;
670 + unsigned int entry;
672 + offset = (unsigned int) (to & (priv->cache.page_size - 1));
673 + avail = priv->cache.page_size - offset;
675 + mutex_lock(&priv->lock);
677 + entry = ps3vram_cache_match(mtd, to);
678 + cached = CACHE_OFFSET + entry * priv->cache.page_size + offset;
680 + dbg("to=%08x cached=%08x offset=%08x avail=%08x count=%08x",
681 + (unsigned) to, cached, offset, avail, count);
685 + memcpy(priv->xdr_buf + cached, buf, avail);
687 + priv->cache.tags[entry].flags |= CACHE_PAGE_DIRTY;
689 + mutex_unlock(&priv->lock);
700 +static int __devinit ps3vram_probe(struct ps3_system_bus_device *dev)
702 + struct ps3vram_priv *priv;
704 + uint64_t ddr_lpar, ctrl_lpar, info_lpar, reports_lpar;
706 + uint64_t reports_size;
711 + ps3vram_mtd.priv = kzalloc(sizeof(struct ps3vram_priv), GFP_KERNEL);
712 + if (!ps3vram_mtd.priv)
714 + priv = ps3vram_mtd.priv;
716 + mutex_init(&priv->lock);
718 + /* Allocate XDR buffer (1MiB aligned) */
719 + priv->xdr_buf = (uint8_t *) __get_free_pages(GFP_KERNEL,
720 + get_order(XDR_BUF_SIZE));
721 + if (priv->xdr_buf == NULL) {
722 + pr_err("ps3vram: could not allocate XDR buffer\n");
724 + goto out_free_priv;
727 + /* Put FIFO at begginning of XDR buffer */
728 + priv->fifo_base = (uint32_t *) (priv->xdr_buf + FIFO_OFFSET);
729 + priv->fifo_ptr = priv->fifo_base;
731 + /* XXX: Need to open GPU, in case ps3fb or snd_ps3 aren't loaded */
732 + if (ps3_open_hv_device(dev)) {
733 + pr_err("ps3vram: ps3_open_hv_device failed\n");
735 + goto out_close_gpu;
738 + /* Request memory */
740 + ddr_size = memparse(size, &rest);
742 + ddr_size -= ps3fb_videomemory.size;
743 + ddr_size = ALIGN(ddr_size, 1024*1024);
744 + if (ddr_size <= 0) {
745 + printk(KERN_ERR "ps3vram: specified size is too small\n");
747 + goto out_close_gpu;
750 + while (ddr_size > 0) {
751 + status = lv1_gpu_memory_allocate(ddr_size, 0, 0, 0, 0,
752 + &priv->memory_handle,
756 + ddr_size -= 1024*1024;
758 + if (status != 0 || ddr_size <= 0) {
759 + pr_err("ps3vram: lv1_gpu_memory_allocate failed\n");
761 + goto out_free_xdr_buf;
763 + pr_info("ps3vram: allocated %u MiB of DDR memory\n",
764 + (unsigned int) (ddr_size / 1024 / 1024));
766 + /* Request context */
767 + status = lv1_gpu_context_allocate(priv->memory_handle,
769 + &priv->context_handle,
775 + pr_err("ps3vram: lv1_gpu_context_allocate failed\n");
777 + goto out_free_memory;
780 + /* Map XDR buffer to RSX */
781 + status = lv1_gpu_context_iomap(priv->context_handle, XDR_IOIF,
782 + ps3_mm_phys_to_lpar(__pa(priv->xdr_buf)),
785 + pr_err("ps3vram: lv1_gpu_context_iomap failed\n");
787 + goto out_free_context;
790 + priv->base = ioremap(ddr_lpar, ddr_size);
792 + pr_err("ps3vram: ioremap failed\n");
794 + goto out_free_context;
797 + priv->ctrl = ioremap(ctrl_lpar, 64 * 1024);
799 + pr_err("ps3vram: ioremap failed\n");
801 + goto out_unmap_vram;
804 + priv->reports = ioremap(reports_lpar, reports_size);
805 + if (!priv->reports) {
806 + pr_err("ps3vram: ioremap failed\n");
808 + goto out_unmap_ctrl;
811 + mutex_lock(&ps3_gpu_mutex);
812 + ps3vram_init_ring(&ps3vram_mtd);
813 + mutex_unlock(&ps3_gpu_mutex);
815 + ps3vram_mtd.name = "ps3vram";
816 + ps3vram_mtd.size = ddr_size;
817 + ps3vram_mtd.flags = MTD_CAP_RAM;
818 + ps3vram_mtd.erase = ps3vram_erase;
819 + ps3vram_mtd.point = NULL;
820 + ps3vram_mtd.unpoint = NULL;
821 + ps3vram_mtd.read = ps3vram_read;
822 + ps3vram_mtd.write = ps3vram_write;
823 + ps3vram_mtd.owner = THIS_MODULE;
824 + ps3vram_mtd.type = MTD_RAM;
825 + ps3vram_mtd.erasesize = CACHE_PAGE_SIZE;
826 + ps3vram_mtd.writesize = 1;
828 + ps3vram_bind(&ps3vram_mtd);
830 + mutex_lock(&ps3_gpu_mutex);
831 + ret = ps3vram_wait_ring(&ps3vram_mtd, 100);
832 + mutex_unlock(&ps3_gpu_mutex);
834 + pr_err("failed to initialize channels\n");
836 + goto out_unmap_reports;
839 + ps3vram_cache_init(&ps3vram_mtd);
841 + if (add_mtd_device(&ps3vram_mtd)) {
842 + pr_err("ps3vram: failed to register device\n");
844 + goto out_cache_cleanup;
847 + pr_info("ps3vram mtd device registered, %lu bytes\n", ddr_size);
851 + ps3vram_cache_cleanup(&ps3vram_mtd);
853 + iounmap(priv->reports);
855 + iounmap(priv->ctrl);
857 + iounmap(priv->base);
859 + lv1_gpu_context_free(priv->context_handle);
861 + lv1_gpu_memory_free(priv->memory_handle);
863 + ps3_close_hv_device(dev);
865 + free_pages((unsigned long) priv->xdr_buf, get_order(XDR_BUF_SIZE));
867 + kfree(ps3vram_mtd.priv);
868 + ps3vram_mtd.priv = NULL;
873 +static int ps3vram_shutdown(struct ps3_system_bus_device *dev)
875 + struct ps3vram_priv *priv;
877 + priv = ps3vram_mtd.priv;
879 + del_mtd_device(&ps3vram_mtd);
880 + ps3vram_cache_cleanup(&ps3vram_mtd);
881 + iounmap(priv->reports);
882 + iounmap(priv->ctrl);
883 + iounmap(priv->base);
884 + lv1_gpu_context_free(priv->context_handle);
885 + lv1_gpu_memory_free(priv->memory_handle);
886 + ps3_close_hv_device(dev);
887 + free_pages((unsigned long) priv->xdr_buf, get_order(XDR_BUF_SIZE));
892 +static struct ps3_system_bus_driver ps3vram_driver = {
893 + .match_id = PS3_MATCH_ID_GPU,
894 + .match_sub_id = PS3_MATCH_SUB_ID_GPU_RAMDISK,
895 + .core.name = DEVICE_NAME,
896 + .core.owner = THIS_MODULE,
897 + .probe = ps3vram_probe,
898 + .remove = ps3vram_shutdown,
899 + .shutdown = ps3vram_shutdown,
902 +static int __init ps3vram_init(void)
904 + return ps3_system_bus_driver_register(&ps3vram_driver);
907 +static void __exit ps3vram_exit(void)
909 + ps3_system_bus_driver_unregister(&ps3vram_driver);
912 +module_init(ps3vram_init);
913 +module_exit(ps3vram_exit);
915 +MODULE_LICENSE("GPL");
916 +MODULE_AUTHOR("Jim Paris <jim@jtan.com>");
917 +MODULE_DESCRIPTION("MTD driver for PS3 video RAM");