13 #include <linux/module.h>
16 #include <linux/slab.h>
25 #define DEVICE_NAME "ps3vram"
28 #define XDR_BUF_SIZE (2 * 1024 * 1024)
29 #define XDR_IOIF 0x0c000000
31 #define FIFO_BASE XDR_IOIF
32 #define FIFO_SIZE (64 * 1024)
34 #define DMA_PAGE_SIZE (4 * 1024)
36 #define CACHE_PAGE_SIZE (256 * 1024)
37 #define CACHE_PAGE_COUNT ((XDR_BUF_SIZE - FIFO_SIZE) / CACHE_PAGE_SIZE)
39 #define CACHE_OFFSET CACHE_PAGE_SIZE
46 #define UPLOAD_SUBCH 1
47 #define DOWNLOAD_SUBCH 2
49 #define NV_MEMORY_TO_MEMORY_FORMAT_OFFSET_IN 0x0000030c
50 #define NV_MEMORY_TO_MEMORY_FORMAT_NOTIFY 0x00000104
52 #define CACHE_PAGE_PRESENT 1
53 #define CACHE_PAGE_DIRTY 2
90 static int ps3vram_major;
93 static const struct block_device_operations ps3vram_fops = {
98 #define DMA_NOTIFIER_HANDLE_BASE 0x66604200
99 #define DMA_NOTIFIER_OFFSET_BASE 0x1000
100 #define DMA_NOTIFIER_SIZE 0x40
103 static char *
size =
"256M";
107 static u32 *ps3vram_get_notifier(
void *reports,
int notifier)
119 for (i = 0; i < 4; i++)
120 notify[i] = 0xffffffff;
126 struct ps3vram_priv *priv = ps3_system_bus_get_drvdata(dev);
130 for (timeout = 20; timeout; timeout--) {
149 struct ps3vram_priv *priv = ps3_system_bus_get_drvdata(dev);
156 unsigned int timeout_ms)
158 struct ps3vram_priv *priv = ps3_system_bus_get_drvdata(dev);
182 ps3vram_out_ring(priv, (size << 18) | (chan << 13) | tag);
187 struct ps3vram_priv *priv = ps3_system_bus_get_drvdata(dev);
197 dev_err(&dev->
core,
"%s: lv1_gpu_fb_blit failed %d\n",
205 struct ps3vram_priv *priv = ps3_system_bus_get_drvdata(dev);
216 dev_err(&dev->
core,
"%s: lv1_gpu_fb_blit failed %d\n",
222 ps3vram_wait_ring(dev, 200);
223 ps3vram_rewind_ring(dev);
231 struct ps3vram_priv *priv = ps3_system_bus_get_drvdata(dev);
234 ps3vram_out_ring(priv, 0x31337303);
237 ps3vram_out_ring(priv, 0xfeed0001);
238 ps3vram_out_ring(priv, 0xfeed0000);
241 ps3vram_out_ring(priv, 0x3137c0de);
244 ps3vram_out_ring(priv, 0xfeed0000);
245 ps3vram_out_ring(priv, 0xfeed0001);
247 ps3vram_fire_ring(dev);
251 unsigned int src_offset,
unsigned int dst_offset,
254 struct ps3vram_priv *priv = ps3_system_bus_get_drvdata(dev);
258 ps3vram_out_ring(priv,
XDR_IOIF + src_offset);
259 ps3vram_out_ring(priv, dst_offset);
260 ps3vram_out_ring(priv, len);
261 ps3vram_out_ring(priv, len);
262 ps3vram_out_ring(priv, len);
263 ps3vram_out_ring(priv, count);
264 ps3vram_out_ring(priv, (1 << 8) | 1);
265 ps3vram_out_ring(priv, 0);
267 ps3vram_notifier_reset(dev);
270 ps3vram_out_ring(priv, 0);
272 ps3vram_out_ring(priv, 0);
273 ps3vram_fire_ring(dev);
274 if (ps3vram_notifier_wait(dev, 200) < 0) {
275 dev_warn(&dev->
core,
"%s: Notifier timeout\n", __func__);
283 unsigned int src_offset,
unsigned int dst_offset,
286 struct ps3vram_priv *priv = ps3_system_bus_get_drvdata(dev);
290 ps3vram_out_ring(priv, src_offset);
291 ps3vram_out_ring(priv,
XDR_IOIF + dst_offset);
292 ps3vram_out_ring(priv, len);
293 ps3vram_out_ring(priv, len);
294 ps3vram_out_ring(priv, len);
295 ps3vram_out_ring(priv, count);
296 ps3vram_out_ring(priv, (1 << 8) | 1);
297 ps3vram_out_ring(priv, 0);
299 ps3vram_notifier_reset(dev);
302 ps3vram_out_ring(priv, 0);
304 ps3vram_out_ring(priv, 0);
305 ps3vram_fire_ring(dev);
306 if (ps3vram_notifier_wait(dev, 200) < 0) {
307 dev_warn(&dev->
core,
"%s: Notifier timeout\n", __func__);
316 struct ps3vram_priv *priv = ps3_system_bus_get_drvdata(dev);
322 dev_dbg(&dev->
core,
"Flushing %d: 0x%08x\n", entry,
323 cache->
tags[entry].address);
328 "Failed to upload from 0x%x to " "0x%x size 0x%x\n",
332 cache->
tags[
entry].flags &= ~CACHE_PAGE_DIRTY;
338 struct ps3vram_priv *priv = ps3_system_bus_get_drvdata(dev);
341 dev_dbg(&dev->
core,
"Fetching %d: 0x%08x\n", entry, address);
342 if (ps3vram_download(dev, address,
347 "Failed to download from 0x%x to 0x%x size 0x%x\n",
358 struct ps3vram_priv *priv = ps3_system_bus_get_drvdata(dev);
364 ps3vram_cache_evict(dev, i);
365 cache->
tags[
i].flags = 0;
372 struct ps3vram_priv *priv = ps3_system_bus_get_drvdata(dev);
379 offset = (
unsigned int) (address & (cache->
page_size - 1));
380 base = (
unsigned int) (address - offset);
385 cache->
tags[
i].address == base) {
388 cache->
tags[i].address);
397 ps3vram_cache_evict(dev, i);
398 ps3vram_cache_load(dev, i, base);
406 struct ps3vram_priv *priv = ps3_system_bus_get_drvdata(dev);
413 dev_err(&dev->
core,
"Could not allocate cache tags\n");
417 dev_info(&dev->
core,
"Created ram cache: %d entries, %d KiB each\n",
425 struct ps3vram_priv *priv = ps3_system_bus_get_drvdata(dev);
427 ps3vram_cache_flush(dev);
434 struct ps3vram_priv *priv = ps3_system_bus_get_drvdata(dev);
437 dev_dbg(&dev->
core,
"%s: from=0x%08x len=0x%zx\n", __func__,
438 (
unsigned int)from, len);
440 if (from >= priv->
size)
443 if (len > priv->
size - from)
452 offset = (
unsigned int) (from & (priv->
cache.page_size - 1));
455 entry = ps3vram_cache_match(dev, from);
458 dev_dbg(&dev->
core,
"%s: from=%08x cached=%08x offset=%08x "
459 "avail=%08x count=%08x\n", __func__,
460 (
unsigned int)from, cached, offset, avail, count);
476 size_t len,
size_t *retlen,
const u_char *buf)
478 struct ps3vram_priv *priv = ps3_system_bus_get_drvdata(dev);
481 if (to >= priv->
size)
484 if (len > priv->
size - to)
485 len = priv->
size - to;
493 offset = (
unsigned int) (to & (priv->
cache.page_size - 1));
496 entry = ps3vram_cache_match(dev, to);
499 dev_dbg(&dev->
core,
"%s: to=%08x cached=%08x offset=%08x "
500 "avail=%08x count=%08x\n", __func__, (
unsigned int)to,
501 cached, offset, avail, count);
518 static int ps3vram_proc_show(
struct seq_file *
m,
void *
v)
533 .open = ps3vram_proc_open,
541 struct ps3vram_priv *priv = ps3_system_bus_get_drvdata(dev);
553 struct ps3vram_priv *priv = ps3_system_bus_get_drvdata(dev);
555 const char *
op = write ?
"write" :
"read";
556 loff_t offset = bio->bi_sector << 9;
558 struct bio_vec *bvec;
562 bio_for_each_segment(bvec, bio, i) {
565 size_t len = bvec->bv_len, retlen;
567 dev_dbg(&dev->
core,
" %s %zu bytes at offset %llu\n", op,
570 error = ps3vram_write(dev, offset, len, &retlen, ptr);
572 error = ps3vram_read(dev, offset, len, &retlen, ptr);
591 spin_lock_irq(&priv->
lock);
592 bio_list_pop(&priv->
list);
593 next = bio_list_peek(&priv->
list);
594 spin_unlock_irq(&priv->
lock);
600 static void ps3vram_make_request(
struct request_queue *
q,
struct bio *bio)
603 struct ps3vram_priv *priv = ps3_system_bus_get_drvdata(dev);
608 spin_lock_irq(&priv->
lock);
609 busy = !bio_list_empty(&priv->
list);
610 bio_list_add(&priv->
list, bio);
611 spin_unlock_irq(&priv->
lock);
617 bio = ps3vram_do_bio(dev, bio);
626 struct gendisk *gendisk;
627 u64 ddr_size, ddr_lpar, ctrl_lpar, info_lpar, reports_lpar,
628 reports_size, xdr_lpar;
638 bio_list_init(&priv->
list);
639 ps3_system_bus_set_drvdata(dev, priv);
645 dev_err(&dev->
core,
"Could not allocate XDR buffer\n");
656 dev_err(&dev->
core,
"ps3_open_hv_device failed\n");
658 goto out_free_xdr_buf;
665 dev_err(&dev->
core,
"Specified size is too small\n");
670 while (ddr_size > 0) {
671 status = lv1_gpu_memory_allocate(ddr_size, 0, 0, 0, 0,
676 ddr_size -= 1024*1024;
679 dev_err(&dev->
core,
"lv1_gpu_memory_allocate failed %d\n",
688 &info_lpar, &reports_lpar,
691 dev_err(&dev->
core,
"lv1_gpu_context_allocate failed %d\n",
694 goto out_free_memory;
704 dev_err(&dev->
core,
"lv1_gpu_context_iomap failed %d\n",
707 goto out_free_context;
714 goto out_unmap_context;
725 ps3vram_init_ring(dev);
728 priv->
size = ddr_size;
733 error = ps3vram_wait_ring(dev, 100);
736 dev_err(&dev->
core,
"Failed to initialize channels\n");
738 goto out_unmap_reports;
741 ps3vram_cache_init(dev);
742 ps3vram_proc_init(dev);
748 goto out_cache_cleanup;
752 queue->queuedata =
dev;
762 goto fail_cleanup_queue;
766 gendisk->major = ps3vram_major;
767 gendisk->first_minor = 0;
768 gendisk->fops = &ps3vram_fops;
769 gendisk->queue = queue;
770 gendisk->private_data =
dev;
771 gendisk->driverfs_dev = &dev->
core;
773 set_capacity(gendisk, priv->
size >> 9);
775 dev_info(&dev->
core,
"%s: Using %lu MiB of GPU memory\n",
776 gendisk->disk_name, get_capacity(gendisk) >> 11);
785 ps3vram_cache_cleanup(dev);
803 ps3_system_bus_set_drvdata(dev,
NULL);
810 struct ps3vram_priv *priv = ps3_system_bus_get_drvdata(dev);
816 ps3vram_cache_cleanup(dev);
827 ps3_system_bus_set_drvdata(dev,
NULL);
836 .probe = ps3vram_probe,
837 .remove = ps3vram_remove,
838 .shutdown = ps3vram_remove,
842 static int __init ps3vram_init(
void)
846 if (!firmware_has_feature(FW_FEATURE_PS3_LV1))
854 ps3vram_major =
error;
866 static void __exit ps3vram_exit(
void)