1 From d7ddc1aaee1ff6dd6a73bd3663b6c390800e0500 Mon Sep 17 00:00:00 2001
2 From: Geert Uytterhoeven <Geert.Uytterhoeven@sonycom.com>
3 Date: Wed, 25 Feb 2009 18:32:10 +0100
4 Subject: [PATCH] ps3/block: Replace mtd/ps3vram by block/ps3vram
6 Convert the PS3 Video RAM Storage Driver from a MTD driver to a plain block
7 device driver, as requested by Arnd Bergmann.
9 The ps3vram driver exposes unused video RAM on the PS3 as a block device
10 suitable for storage or swap. Fast data transfer is achieved using a local
11 cache in system RAM and DMA transfers via the GPU.
13 The new driver is ca. 50% faster for reading, and ca. 10% for writing.
15 Signed-off-by: Geert Uytterhoeven <Geert.Uytterhoeven@sonycom.com>
16 Acked-by: Jens Axboe <axboe@kernel.dk>
17 Acked-by: Geoff Levand <geoffrey.levand@am.sony.com>
18 Cc: Arnd Bergmann <arnd@arndb.de>
19 Cc: David Woodhouse <David.Woodhouse@intel.com>
20 Cc: Vivien Chappelier <vivien.chappelier@free.fr>
21 Cc: Jim Paris <jim@jtan.com>
25 The MTD-based PS3 Video RAM Storage Driver was integrated into the mainline
28 Ideally, we think it would be best if the existing MTD-based ps3vram driver
29 would be replaced by the new block-based ps3vram driver before 2.6.29 is
30 released. This would relieve the burden of supporting two different swap space
31 schemes on PS3 (swap on /dev/mtdblock0 vs. /dev/ps3vram) from the distro
32 maintainer's shoulders, as in that case there would never have been a stable
33 kernel version containing the MTD-based ps3vram driver.
35 Changes since previous submission (Wed, 4 Mar 2009 14:57:20 +0100 (CET)):
36 - Use blk_queue_make_request() to get rid of the thread
37 - Add a mutex (cfr. the old driver), as ps3vram_make_request() may be called
39 TO DO (after 2.6.29): use a spinlock and a list to queue requests while the
41 - Remove the old MTD-based ps3vram driver and rename ps3vram-ng to ps3vram
42 - Make PS3_VRAM depend on FB_PS3=y and m for now
43 ps3vram relies on ps3fb being initialized first. The easiest way to do this
44 is by making ps3vram modular, and ps3fb builtin
45 - Remove the dependency on ps3fb_videomemory.size
46 The loop to reduce ddr_size until it succeeds does the right thing anyway,
47 and a few MiB of DDR RAM are reserved by the hypervisor
48 - lv1 return codes can be int
49 - Correct a few debug annotations
51 arch/powerpc/platforms/ps3/Kconfig | 7 +
52 drivers/block/Makefile | 1 +
53 drivers/block/ps3vram.c | 865 ++++++++++++++++++++++++++++++++++++
54 create mode 100644 drivers/block/ps3vram.c
55 delete mode 100644 drivers/mtd/devices/ps3vram.c
57 diff --git a/arch/powerpc/platforms/ps3/Kconfig b/arch/powerpc/platforms/ps3/Kconfig
58 index 920cf7a..740ef56 100644
59 --- a/arch/powerpc/platforms/ps3/Kconfig
60 +++ b/arch/powerpc/platforms/ps3/Kconfig
61 @@ -128,6 +128,13 @@ config PS3_FLASH
62 be disabled on the kernel command line using "ps3flash=off", to
63 not allocate this fixed buffer.
66 + tristate "PS3 Video RAM Storage Driver"
67 + depends on FB_PS3=y && BLOCK && m
69 + This driver allows you to use excess PS3 video RAM as volatile
70 + storage or system swap.
73 tristate "PS3 Logical Performance Monitor support"
75 diff --git a/drivers/block/Makefile b/drivers/block/Makefile
76 index 204332b..87e120e 100644
77 --- a/drivers/block/Makefile
78 +++ b/drivers/block/Makefile
79 @@ -9,6 +9,7 @@ obj-$(CONFIG_MAC_FLOPPY) += swim3.o
80 obj-$(CONFIG_BLK_DEV_FD) += floppy.o
81 obj-$(CONFIG_AMIGA_FLOPPY) += amiflop.o
82 obj-$(CONFIG_PS3_DISK) += ps3disk.o
83 +obj-$(CONFIG_PS3_VRAM) += ps3vram.o
84 obj-$(CONFIG_ATARI_FLOPPY) += ataflop.o
85 obj-$(CONFIG_AMIGA_Z2RAM) += z2ram.o
86 obj-$(CONFIG_BLK_DEV_RAM) += brd.o
87 diff --git a/drivers/block/ps3vram.c b/drivers/block/ps3vram.c
89 index 0000000..393ed67
91 +++ b/drivers/block/ps3vram.c
94 + * ps3vram - Use extra PS3 video ram as MTD block device.
96 + * Copyright 2009 Sony Corporation
98 + * Based on the MTD ps3vram driver, which is
99 + * Copyright (c) 2007-2008 Jim Paris <jim@jtan.com>
100 + * Added support RSX DMA Vivien Chappelier <vivien.chappelier@free.fr>
103 +#include <linux/blkdev.h>
104 +#include <linux/delay.h>
105 +#include <linux/proc_fs.h>
106 +#include <linux/seq_file.h>
108 +#include <asm/firmware.h>
109 +#include <asm/lv1call.h>
110 +#include <asm/ps3.h>
113 +#define DEVICE_NAME "ps3vram"
116 +#define XDR_BUF_SIZE (2 * 1024 * 1024) /* XDR buffer (must be 1MiB aligned) */
117 +#define XDR_IOIF 0x0c000000
119 +#define FIFO_BASE XDR_IOIF
120 +#define FIFO_SIZE (64 * 1024)
122 +#define DMA_PAGE_SIZE (4 * 1024)
124 +#define CACHE_PAGE_SIZE (256 * 1024)
125 +#define CACHE_PAGE_COUNT ((XDR_BUF_SIZE - FIFO_SIZE) / CACHE_PAGE_SIZE)
127 +#define CACHE_OFFSET CACHE_PAGE_SIZE
128 +#define FIFO_OFFSET 0
130 +#define CTRL_PUT 0x10
131 +#define CTRL_GET 0x11
132 +#define CTRL_TOP 0x15
134 +#define UPLOAD_SUBCH 1
135 +#define DOWNLOAD_SUBCH 2
137 +#define NV_MEMORY_TO_MEMORY_FORMAT_OFFSET_IN 0x0000030c
138 +#define NV_MEMORY_TO_MEMORY_FORMAT_NOTIFY 0x00000104
140 +#define L1GPU_CONTEXT_ATTRIBUTE_FB_BLIT 0x601
142 +#define CACHE_PAGE_PRESENT 1
143 +#define CACHE_PAGE_DIRTY 2
145 +struct ps3vram_tag {
146 + unsigned int address;
147 + unsigned int flags;
150 +struct ps3vram_cache {
151 + unsigned int page_count;
152 + unsigned int page_size;
153 + struct ps3vram_tag *tags;
158 +struct ps3vram_priv {
159 + struct request_queue *queue;
160 + struct gendisk *gendisk;
165 + u64 context_handle;
168 + u8 __iomem *ddr_base;
174 + struct ps3vram_cache cache;
176 + /* Used to serialize cache/DMA operations */
181 +static int ps3vram_major;
184 +static struct block_device_operations ps3vram_fops = {
185 + .owner = THIS_MODULE,
189 +#define DMA_NOTIFIER_HANDLE_BASE 0x66604200 /* first DMA notifier handle */
190 +#define DMA_NOTIFIER_OFFSET_BASE 0x1000 /* first DMA notifier offset */
191 +#define DMA_NOTIFIER_SIZE 0x40
192 +#define NOTIFIER 7 /* notifier used for completion report */
194 +static char *size = "256M";
195 +module_param(size, charp, 0);
196 +MODULE_PARM_DESC(size, "memory size");
198 +static u32 *ps3vram_get_notifier(u32 *reports, int notifier)
200 + return (void *)reports + DMA_NOTIFIER_OFFSET_BASE +
201 + DMA_NOTIFIER_SIZE * notifier;
204 +static void ps3vram_notifier_reset(struct ps3_system_bus_device *dev)
206 + struct ps3vram_priv *priv = dev->core.driver_data;
207 + u32 *notify = ps3vram_get_notifier(priv->reports, NOTIFIER);
210 + for (i = 0; i < 4; i++)
211 + notify[i] = 0xffffffff;
214 +static int ps3vram_notifier_wait(struct ps3_system_bus_device *dev,
215 + unsigned int timeout_ms)
217 + struct ps3vram_priv *priv = dev->core.driver_data;
218 + u32 *notify = ps3vram_get_notifier(priv->reports, NOTIFIER);
219 + unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
225 + } while (time_before(jiffies, timeout));
230 +static void ps3vram_init_ring(struct ps3_system_bus_device *dev)
232 + struct ps3vram_priv *priv = dev->core.driver_data;
234 + priv->ctrl[CTRL_PUT] = FIFO_BASE + FIFO_OFFSET;
235 + priv->ctrl[CTRL_GET] = FIFO_BASE + FIFO_OFFSET;
238 +static int ps3vram_wait_ring(struct ps3_system_bus_device *dev,
239 + unsigned int timeout_ms)
241 + struct ps3vram_priv *priv = dev->core.driver_data;
242 + unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
245 + if (priv->ctrl[CTRL_PUT] == priv->ctrl[CTRL_GET])
248 + } while (time_before(jiffies, timeout));
250 + dev_warn(&dev->core, "FIFO timeout (%08x/%08x/%08x)\n",
251 + priv->ctrl[CTRL_PUT], priv->ctrl[CTRL_GET],
252 + priv->ctrl[CTRL_TOP]);
257 +static void ps3vram_out_ring(struct ps3vram_priv *priv, u32 data)
259 + *(priv->fifo_ptr)++ = data;
262 +static void ps3vram_begin_ring(struct ps3vram_priv *priv, u32 chan, u32 tag,
265 + ps3vram_out_ring(priv, (size << 18) | (chan << 13) | tag);
268 +static void ps3vram_rewind_ring(struct ps3_system_bus_device *dev)
270 + struct ps3vram_priv *priv = dev->core.driver_data;
273 + ps3vram_out_ring(priv, 0x20000000 | (FIFO_BASE + FIFO_OFFSET));
275 + priv->ctrl[CTRL_PUT] = FIFO_BASE + FIFO_OFFSET;
277 + /* asking the HV for a blit will kick the FIFO */
278 + status = lv1_gpu_context_attribute(priv->context_handle,
279 + L1GPU_CONTEXT_ATTRIBUTE_FB_BLIT, 0,
282 + dev_err(&dev->core,
283 + "%s: lv1_gpu_context_attribute failed %d\n", __func__,
286 + priv->fifo_ptr = priv->fifo_base;
289 +static void ps3vram_fire_ring(struct ps3_system_bus_device *dev)
291 + struct ps3vram_priv *priv = dev->core.driver_data;
294 + mutex_lock(&ps3_gpu_mutex);
296 + priv->ctrl[CTRL_PUT] = FIFO_BASE + FIFO_OFFSET +
297 + (priv->fifo_ptr - priv->fifo_base) * sizeof(u32);
299 + /* asking the HV for a blit will kick the FIFO */
300 + status = lv1_gpu_context_attribute(priv->context_handle,
301 + L1GPU_CONTEXT_ATTRIBUTE_FB_BLIT, 0,
304 + dev_err(&dev->core,
305 + "%s: lv1_gpu_context_attribute failed %d\n", __func__,
308 + if ((priv->fifo_ptr - priv->fifo_base) * sizeof(u32) >
309 + FIFO_SIZE - 1024) {
310 + dev_dbg(&dev->core, "FIFO full, rewinding\n");
311 + ps3vram_wait_ring(dev, 200);
312 + ps3vram_rewind_ring(dev);
315 + mutex_unlock(&ps3_gpu_mutex);
318 +static void ps3vram_bind(struct ps3_system_bus_device *dev)
320 + struct ps3vram_priv *priv = dev->core.driver_data;
322 + ps3vram_begin_ring(priv, UPLOAD_SUBCH, 0, 1);
323 + ps3vram_out_ring(priv, 0x31337303);
324 + ps3vram_begin_ring(priv, UPLOAD_SUBCH, 0x180, 3);
325 + ps3vram_out_ring(priv, DMA_NOTIFIER_HANDLE_BASE + NOTIFIER);
326 + ps3vram_out_ring(priv, 0xfeed0001); /* DMA system RAM instance */
327 + ps3vram_out_ring(priv, 0xfeed0000); /* DMA video RAM instance */
329 + ps3vram_begin_ring(priv, DOWNLOAD_SUBCH, 0, 1);
330 + ps3vram_out_ring(priv, 0x3137c0de);
331 + ps3vram_begin_ring(priv, DOWNLOAD_SUBCH, 0x180, 3);
332 + ps3vram_out_ring(priv, DMA_NOTIFIER_HANDLE_BASE + NOTIFIER);
333 + ps3vram_out_ring(priv, 0xfeed0000); /* DMA video RAM instance */
334 + ps3vram_out_ring(priv, 0xfeed0001); /* DMA system RAM instance */
336 + ps3vram_fire_ring(dev);
339 +static int ps3vram_upload(struct ps3_system_bus_device *dev,
340 + unsigned int src_offset, unsigned int dst_offset,
341 + int len, int count)
343 + struct ps3vram_priv *priv = dev->core.driver_data;
345 + ps3vram_begin_ring(priv, UPLOAD_SUBCH,
346 + NV_MEMORY_TO_MEMORY_FORMAT_OFFSET_IN, 8);
347 + ps3vram_out_ring(priv, XDR_IOIF + src_offset);
348 + ps3vram_out_ring(priv, dst_offset);
349 + ps3vram_out_ring(priv, len);
350 + ps3vram_out_ring(priv, len);
351 + ps3vram_out_ring(priv, len);
352 + ps3vram_out_ring(priv, count);
353 + ps3vram_out_ring(priv, (1 << 8) | 1);
354 + ps3vram_out_ring(priv, 0);
356 + ps3vram_notifier_reset(dev);
357 + ps3vram_begin_ring(priv, UPLOAD_SUBCH,
358 + NV_MEMORY_TO_MEMORY_FORMAT_NOTIFY, 1);
359 + ps3vram_out_ring(priv, 0);
360 + ps3vram_begin_ring(priv, UPLOAD_SUBCH, 0x100, 1);
361 + ps3vram_out_ring(priv, 0);
362 + ps3vram_fire_ring(dev);
363 + if (ps3vram_notifier_wait(dev, 200) < 0) {
364 + dev_warn(&dev->core, "%s: Notifier timeout\n", __func__);
371 +static int ps3vram_download(struct ps3_system_bus_device *dev,
372 + unsigned int src_offset, unsigned int dst_offset,
373 + int len, int count)
375 + struct ps3vram_priv *priv = dev->core.driver_data;
377 + ps3vram_begin_ring(priv, DOWNLOAD_SUBCH,
378 + NV_MEMORY_TO_MEMORY_FORMAT_OFFSET_IN, 8);
379 + ps3vram_out_ring(priv, src_offset);
380 + ps3vram_out_ring(priv, XDR_IOIF + dst_offset);
381 + ps3vram_out_ring(priv, len);
382 + ps3vram_out_ring(priv, len);
383 + ps3vram_out_ring(priv, len);
384 + ps3vram_out_ring(priv, count);
385 + ps3vram_out_ring(priv, (1 << 8) | 1);
386 + ps3vram_out_ring(priv, 0);
388 + ps3vram_notifier_reset(dev);
389 + ps3vram_begin_ring(priv, DOWNLOAD_SUBCH,
390 + NV_MEMORY_TO_MEMORY_FORMAT_NOTIFY, 1);
391 + ps3vram_out_ring(priv, 0);
392 + ps3vram_begin_ring(priv, DOWNLOAD_SUBCH, 0x100, 1);
393 + ps3vram_out_ring(priv, 0);
394 + ps3vram_fire_ring(dev);
395 + if (ps3vram_notifier_wait(dev, 200) < 0) {
396 + dev_warn(&dev->core, "%s: Notifier timeout\n", __func__);
403 +static void ps3vram_cache_evict(struct ps3_system_bus_device *dev, int entry)
405 + struct ps3vram_priv *priv = dev->core.driver_data;
406 + struct ps3vram_cache *cache = &priv->cache;
408 + if (!(cache->tags[entry].flags & CACHE_PAGE_DIRTY))
411 + dev_dbg(&dev->core, "Flushing %d: 0x%08x\n", entry,
412 + cache->tags[entry].address);
413 + if (ps3vram_upload(dev, CACHE_OFFSET + entry * cache->page_size,
414 + cache->tags[entry].address, DMA_PAGE_SIZE,
415 + cache->page_size / DMA_PAGE_SIZE) < 0) {
416 + dev_err(&dev->core,
417 + "Failed to upload from 0x%x to " "0x%x size 0x%x\n",
418 + entry * cache->page_size, cache->tags[entry].address,
421 + cache->tags[entry].flags &= ~CACHE_PAGE_DIRTY;
424 +static void ps3vram_cache_load(struct ps3_system_bus_device *dev, int entry,
425 + unsigned int address)
427 + struct ps3vram_priv *priv = dev->core.driver_data;
428 + struct ps3vram_cache *cache = &priv->cache;
430 + dev_dbg(&dev->core, "Fetching %d: 0x%08x\n", entry, address);
431 + if (ps3vram_download(dev, address,
432 + CACHE_OFFSET + entry * cache->page_size,
434 + cache->page_size / DMA_PAGE_SIZE) < 0) {
435 + dev_err(&dev->core,
436 + "Failed to download from 0x%x to 0x%x size 0x%x\n",
437 + address, entry * cache->page_size, cache->page_size);
440 + cache->tags[entry].address = address;
441 + cache->tags[entry].flags |= CACHE_PAGE_PRESENT;
445 +static void ps3vram_cache_flush(struct ps3_system_bus_device *dev)
447 + struct ps3vram_priv *priv = dev->core.driver_data;
448 + struct ps3vram_cache *cache = &priv->cache;
451 + dev_dbg(&dev->core, "FLUSH\n");
452 + for (i = 0; i < cache->page_count; i++) {
453 + ps3vram_cache_evict(dev, i);
454 + cache->tags[i].flags = 0;
458 +static unsigned int ps3vram_cache_match(struct ps3_system_bus_device *dev,
461 + struct ps3vram_priv *priv = dev->core.driver_data;
462 + struct ps3vram_cache *cache = &priv->cache;
464 + unsigned int offset;
466 + static int counter;
468 + offset = (unsigned int) (address & (cache->page_size - 1));
469 + base = (unsigned int) (address - offset);
471 + /* fully associative check */
472 + for (i = 0; i < cache->page_count; i++) {
473 + if ((cache->tags[i].flags & CACHE_PAGE_PRESENT) &&
474 + cache->tags[i].address == base) {
476 + dev_dbg(&dev->core, "Found entry %d: 0x%08x\n", i,
477 + cache->tags[i].address);
482 + /* choose a random entry */
483 + i = (jiffies + (counter++)) % cache->page_count;
484 + dev_dbg(&dev->core, "Using entry %d\n", i);
486 + ps3vram_cache_evict(dev, i);
487 + ps3vram_cache_load(dev, i, base);
493 +static int ps3vram_cache_init(struct ps3_system_bus_device *dev)
495 + struct ps3vram_priv *priv = dev->core.driver_data;
497 + priv->cache.page_count = CACHE_PAGE_COUNT;
498 + priv->cache.page_size = CACHE_PAGE_SIZE;
499 + priv->cache.tags = kzalloc(sizeof(struct ps3vram_tag) *
500 + CACHE_PAGE_COUNT, GFP_KERNEL);
501 + if (priv->cache.tags == NULL) {
502 + dev_err(&dev->core, "Could not allocate cache tags\n");
506 + dev_info(&dev->core, "Created ram cache: %d entries, %d KiB each\n",
507 + CACHE_PAGE_COUNT, CACHE_PAGE_SIZE / 1024);
512 +static void ps3vram_cache_cleanup(struct ps3_system_bus_device *dev)
514 + struct ps3vram_priv *priv = dev->core.driver_data;
516 + ps3vram_cache_flush(dev);
517 + kfree(priv->cache.tags);
520 +static int ps3vram_read(struct ps3_system_bus_device *dev, loff_t from,
521 + size_t len, size_t *retlen, u_char *buf)
523 + struct ps3vram_priv *priv = dev->core.driver_data;
524 + unsigned int cached, count;
526 + dev_dbg(&dev->core, "%s: from=0x%08x len=0x%zx\n", __func__,
527 + (unsigned int)from, len);
529 + if (from >= priv->size)
532 + if (len > priv->size - from)
533 + len = priv->size - from;
535 + /* Copy from vram to buf */
538 + unsigned int offset, avail;
539 + unsigned int entry;
541 + offset = (unsigned int) (from & (priv->cache.page_size - 1));
542 + avail = priv->cache.page_size - offset;
544 + mutex_lock(&priv->lock);
546 + entry = ps3vram_cache_match(dev, from);
547 + cached = CACHE_OFFSET + entry * priv->cache.page_size + offset;
549 + dev_dbg(&dev->core, "%s: from=%08x cached=%08x offset=%08x "
550 + "avail=%08x count=%08x\n", __func__,
551 + (unsigned int)from, cached, offset, avail, count);
555 + memcpy(buf, priv->xdr_buf + cached, avail);
557 + mutex_unlock(&priv->lock);
568 +static int ps3vram_write(struct ps3_system_bus_device *dev, loff_t to,
569 + size_t len, size_t *retlen, const u_char *buf)
571 + struct ps3vram_priv *priv = dev->core.driver_data;
572 + unsigned int cached, count;
574 + if (to >= priv->size)
577 + if (len > priv->size - to)
578 + len = priv->size - to;
580 + /* Copy from buf to vram */
583 + unsigned int offset, avail;
584 + unsigned int entry;
586 + offset = (unsigned int) (to & (priv->cache.page_size - 1));
587 + avail = priv->cache.page_size - offset;
589 + mutex_lock(&priv->lock);
591 + entry = ps3vram_cache_match(dev, to);
592 + cached = CACHE_OFFSET + entry * priv->cache.page_size + offset;
594 + dev_dbg(&dev->core, "%s: to=%08x cached=%08x offset=%08x "
595 + "avail=%08x count=%08x\n", __func__, (unsigned int)to,
596 + cached, offset, avail, count);
600 + memcpy(priv->xdr_buf + cached, buf, avail);
602 + priv->cache.tags[entry].flags |= CACHE_PAGE_DIRTY;
604 + mutex_unlock(&priv->lock);
615 +static int ps3vram_proc_show(struct seq_file *m, void *v)
617 + struct ps3vram_priv *priv = m->private;
619 + seq_printf(m, "hit:%u\nmiss:%u\n", priv->cache.hit, priv->cache.miss);
623 +static int ps3vram_proc_open(struct inode *inode, struct file *file)
625 + return single_open(file, ps3vram_proc_show, PDE(inode)->data);
628 +static const struct file_operations ps3vram_proc_fops = {
629 + .owner = THIS_MODULE,
630 + .open = ps3vram_proc_open,
632 + .llseek = seq_lseek,
633 + .release = single_release,
636 +static void __devinit ps3vram_proc_init(struct ps3_system_bus_device *dev)
638 + struct ps3vram_priv *priv = dev->core.driver_data;
639 + struct proc_dir_entry *pde;
641 + pde = proc_create(DEVICE_NAME, 0444, NULL, &ps3vram_proc_fops);
643 + dev_warn(&dev->core, "failed to create /proc entry\n");
647 + pde->owner = THIS_MODULE;
651 +static int ps3vram_make_request(struct request_queue *q, struct bio *bio)
653 + struct ps3_system_bus_device *dev = q->queuedata;
654 + int write = bio_data_dir(bio) == WRITE;
655 + const char *op = write ? "write" : "read";
656 + loff_t offset = bio->bi_sector << 9;
658 + struct bio_vec *bvec;
661 + dev_dbg(&dev->core, "%s\n", __func__);
663 + bio_for_each_segment(bvec, bio, i) {
664 + /* PS3 is ppc64, so we don't handle highmem */
665 + char *ptr = page_address(bvec->bv_page) + bvec->bv_offset;
666 + size_t len = bvec->bv_len, retlen;
668 + dev_dbg(&dev->core, " %s %zu bytes at offset %llu\n", op,
671 + error = ps3vram_write(dev, offset, len, &retlen, ptr);
673 + error = ps3vram_read(dev, offset, len, &retlen, ptr);
676 + dev_err(&dev->core, "%s failed\n", op);
680 + if (retlen != len) {
681 + dev_err(&dev->core, "Short %s\n", op);
688 + dev_dbg(&dev->core, "%s completed\n", op);
691 + bio_endio(bio, error);
695 +static int __devinit ps3vram_probe(struct ps3_system_bus_device *dev)
697 + struct ps3vram_priv *priv;
699 + struct request_queue *queue;
700 + struct gendisk *gendisk;
701 + u64 ddr_lpar, ctrl_lpar, info_lpar, reports_lpar, ddr_size,
705 + priv = kzalloc(sizeof(*priv), GFP_KERNEL);
711 + mutex_init(&priv->lock);
712 + dev->core.driver_data = priv;
714 + priv = dev->core.driver_data;
716 + /* Allocate XDR buffer (1MiB aligned) */
717 + priv->xdr_buf = (void *)__get_free_pages(GFP_KERNEL,
718 + get_order(XDR_BUF_SIZE));
719 + if (priv->xdr_buf == NULL) {
720 + dev_err(&dev->core, "Could not allocate XDR buffer\n");
722 + goto fail_free_priv;
725 + /* Put FIFO at begginning of XDR buffer */
726 + priv->fifo_base = (u32 *) (priv->xdr_buf + FIFO_OFFSET);
727 + priv->fifo_ptr = priv->fifo_base;
729 + /* XXX: Need to open GPU, in case ps3fb or snd_ps3 aren't loaded */
730 + if (ps3_open_hv_device(dev)) {
731 + dev_err(&dev->core, "ps3_open_hv_device failed\n");
733 + goto out_close_gpu;
736 + /* Request memory */
738 + ddr_size = ALIGN(memparse(size, &rest), 1024*1024);
740 + dev_err(&dev->core, "Specified size is too small\n");
742 + goto out_close_gpu;
745 + while (ddr_size > 0) {
746 + status = lv1_gpu_memory_allocate(ddr_size, 0, 0, 0, 0,
747 + &priv->memory_handle,
751 + ddr_size -= 1024*1024;
754 + dev_err(&dev->core, "lv1_gpu_memory_allocate failed %d\n",
757 + goto out_free_xdr_buf;
760 + /* Request context */
761 + status = lv1_gpu_context_allocate(priv->memory_handle, 0,
762 + &priv->context_handle, &ctrl_lpar,
763 + &info_lpar, &reports_lpar,
766 + dev_err(&dev->core, "lv1_gpu_context_allocate failed %d\n",
769 + goto out_free_memory;
772 + /* Map XDR buffer to RSX */
773 + status = lv1_gpu_context_iomap(priv->context_handle, XDR_IOIF,
774 + ps3_mm_phys_to_lpar(__pa(priv->xdr_buf)),
777 + dev_err(&dev->core, "lv1_gpu_context_iomap failed %d\n",
780 + goto out_free_context;
783 + priv->ddr_base = ioremap_flags(ddr_lpar, ddr_size, _PAGE_NO_CACHE);
785 + if (!priv->ddr_base) {
786 + dev_err(&dev->core, "ioremap DDR failed\n");
788 + goto out_free_context;
791 + priv->ctrl = ioremap(ctrl_lpar, 64 * 1024);
793 + dev_err(&dev->core, "ioremap CTRL failed\n");
795 + goto out_unmap_vram;
798 + priv->reports = ioremap(reports_lpar, reports_size);
799 + if (!priv->reports) {
800 + dev_err(&dev->core, "ioremap REPORTS failed\n");
802 + goto out_unmap_ctrl;
805 + mutex_lock(&ps3_gpu_mutex);
806 + ps3vram_init_ring(dev);
807 + mutex_unlock(&ps3_gpu_mutex);
809 + priv->size = ddr_size;
813 + mutex_lock(&ps3_gpu_mutex);
814 + error = ps3vram_wait_ring(dev, 100);
815 + mutex_unlock(&ps3_gpu_mutex);
817 + dev_err(&dev->core, "Failed to initialize channels\n");
818 + error = -ETIMEDOUT;
819 + goto out_unmap_reports;
822 + ps3vram_cache_init(dev);
823 + ps3vram_proc_init(dev);
825 + queue = blk_alloc_queue(GFP_KERNEL);
827 + dev_err(&dev->core, "blk_alloc_queue failed\n");
829 + goto out_cache_cleanup;
832 + priv->queue = queue;
833 + queue->queuedata = dev;
834 + blk_queue_make_request(queue, ps3vram_make_request);
835 + blk_queue_max_phys_segments(queue, MAX_PHYS_SEGMENTS);
836 + blk_queue_max_hw_segments(queue, MAX_HW_SEGMENTS);
837 + blk_queue_max_segment_size(queue, MAX_SEGMENT_SIZE);
838 + blk_queue_max_sectors(queue, SAFE_MAX_SECTORS);
840 + gendisk = alloc_disk(1);
842 + dev_err(&dev->core, "alloc_disk failed\n");
844 + goto fail_cleanup_queue;
847 + priv->gendisk = gendisk;
848 + gendisk->major = ps3vram_major;
849 + gendisk->first_minor = 0;
850 + gendisk->fops = &ps3vram_fops;
851 + gendisk->queue = queue;
852 + gendisk->private_data = dev;
853 + gendisk->driverfs_dev = &dev->core;
854 + strlcpy(gendisk->disk_name, DEVICE_NAME, sizeof(gendisk->disk_name));
855 + set_capacity(gendisk, priv->size >> 9);
857 + dev_info(&dev->core, "%s: Using %lu MiB of GPU memory\n",
858 + gendisk->disk_name, get_capacity(gendisk) >> 11);
864 + blk_cleanup_queue(queue);
866 + remove_proc_entry(DEVICE_NAME, NULL);
867 + ps3vram_cache_cleanup(dev);
869 + iounmap(priv->reports);
871 + iounmap(priv->ctrl);
873 + iounmap(priv->ddr_base);
875 + lv1_gpu_context_free(priv->context_handle);
877 + lv1_gpu_memory_free(priv->memory_handle);
879 + ps3_close_hv_device(dev);
881 + free_pages((unsigned long) priv->xdr_buf, get_order(XDR_BUF_SIZE));
884 + dev->core.driver_data = NULL;
889 +static int ps3vram_remove(struct ps3_system_bus_device *dev)
891 + struct ps3vram_priv *priv = dev->core.driver_data;
893 + del_gendisk(priv->gendisk);
894 + put_disk(priv->gendisk);
895 + blk_cleanup_queue(priv->queue);
896 + remove_proc_entry(DEVICE_NAME, NULL);
897 + ps3vram_cache_cleanup(dev);
898 + iounmap(priv->reports);
899 + iounmap(priv->ctrl);
900 + iounmap(priv->ddr_base);
901 + lv1_gpu_context_free(priv->context_handle);
902 + lv1_gpu_memory_free(priv->memory_handle);
903 + ps3_close_hv_device(dev);
904 + free_pages((unsigned long) priv->xdr_buf, get_order(XDR_BUF_SIZE));
906 + dev->core.driver_data = NULL;
910 +static struct ps3_system_bus_driver ps3vram = {
911 + .match_id = PS3_MATCH_ID_GPU,
912 + .match_sub_id = PS3_MATCH_SUB_ID_GPU_RAMDISK,
913 + .core.name = DEVICE_NAME,
914 + .core.owner = THIS_MODULE,
915 + .probe = ps3vram_probe,
916 + .remove = ps3vram_remove,
917 + .shutdown = ps3vram_remove,
921 +static int __init ps3vram_init(void)
925 + if (!firmware_has_feature(FW_FEATURE_PS3_LV1))
928 + error = register_blkdev(0, DEVICE_NAME);
930 + pr_err("%s: register_blkdev failed %d\n", DEVICE_NAME, error);
933 + ps3vram_major = error;
935 + pr_info("%s: registered block device major %d\n", DEVICE_NAME,
938 + error = ps3_system_bus_driver_register(&ps3vram);
940 + unregister_blkdev(ps3vram_major, DEVICE_NAME);
945 +static void __exit ps3vram_exit(void)
947 + ps3_system_bus_driver_unregister(&ps3vram);
948 + unregister_blkdev(ps3vram_major, DEVICE_NAME);
951 +module_init(ps3vram_init);
952 +module_exit(ps3vram_exit);
954 +MODULE_LICENSE("GPL");
955 +MODULE_DESCRIPTION("PS3 Video RAM Storage Driver");
956 +MODULE_AUTHOR("Sony Corporation");
957 +MODULE_ALIAS(PS3_MODULE_ALIAS_GPU_RAMDISK);