2 * ps3vram - Use extra PS3 video ram as MTD block device.
4 * Copyright (c) 2007-2008 Jim Paris <jim@jtan.com>
5 * Added support RSX DMA Vivien Chappelier <vivien.chappelier@free.fr>
9 #include <linux/init.h>
10 #include <linux/kernel.h>
11 #include <linux/list.h>
12 #include <linux/module.h>
13 #include <linux/moduleparam.h>
14 #include <linux/slab.h>
15 #include <linux/version.h>
16 #include <linux/gfp.h>
17 #include <linux/delay.h>
18 #include <linux/mtd/mtd.h>
20 #include <asm/lv1call.h>
23 #define DEVICE_NAME "ps3vram"
25 #define XDR_BUF_SIZE (2 * 1024 * 1024) /* XDR buffer (must be 1MiB aligned) */
26 #define XDR_IOIF 0x0c000000
28 #define FIFO_BASE XDR_IOIF
29 #define FIFO_SIZE (64 * 1024)
31 #define DMA_PAGE_SIZE (4 * 1024)
33 #define CACHE_PAGE_SIZE (256 * 1024)
34 #define CACHE_PAGE_COUNT ((XDR_BUF_SIZE - FIFO_SIZE) / CACHE_PAGE_SIZE)
36 #define CACHE_OFFSET CACHE_PAGE_SIZE
43 #define UPLOAD_SUBCH 1
44 #define DOWNLOAD_SUBCH 2
46 #define NV_MEMORY_TO_MEMORY_FORMAT_OFFSET_IN 0x0000030c
47 #define NV_MEMORY_TO_MEMORY_FORMAT_NOTIFY 0x00000104
49 #define L1GPU_CONTEXT_ATTRIBUTE_FB_BLIT 0x601
51 struct mtd_info ps3vram_mtd;
53 #define CACHE_PAGE_PRESENT 1
54 #define CACHE_PAGE_DIRTY 2
56 #define dbg(fmt, args...) \
57 pr_debug("%s:%d " fmt "\n", __func__, __LINE__, ## args)
64 struct ps3vram_cache {
65 unsigned int page_count;
66 unsigned int page_size;
67 struct ps3vram_tag *tags;
71 uint64_t memory_handle;
72 uint64_t context_handle;
81 struct ps3vram_cache cache;
83 /* Used to serialize cache/DMA operations */
87 #define DMA_NOTIFIER_HANDLE_BASE 0x66604200 /* first DMA notifier handle */
88 #define DMA_NOTIFIER_OFFSET_BASE 0x1000 /* first DMA notifier offset */
89 #define DMA_NOTIFIER_SIZE 0x40
90 #define NOTIFIER 7 /* notifier used for completion report */
92 /* A trailing '-' means to subtract off ps3fb_videomemory.size */
94 module_param(size, charp, 0);
95 MODULE_PARM_DESC(size, "memory size");
97 static inline uint32_t *ps3vram_get_notifier(uint32_t *reports, int notifier)
99 return (void *) reports +
100 DMA_NOTIFIER_OFFSET_BASE +
101 DMA_NOTIFIER_SIZE * notifier;
104 static void ps3vram_notifier_reset(struct mtd_info *mtd)
107 struct ps3vram_priv *priv = mtd->priv;
108 uint32_t *notify = ps3vram_get_notifier(priv->reports, NOTIFIER);
109 for (i = 0; i < 4; i++)
110 notify[i] = 0xffffffff;
113 static int ps3vram_notifier_wait(struct mtd_info *mtd, int timeout_ms)
115 struct ps3vram_priv *priv = mtd->priv;
116 uint32_t *notify = ps3vram_get_notifier(priv->reports, NOTIFIER);
126 } while (timeout_ms--);
131 static void ps3vram_init_ring(struct mtd_info *mtd)
133 struct ps3vram_priv *priv = mtd->priv;
135 priv->ctrl[CTRL_PUT] = FIFO_BASE + FIFO_OFFSET;
136 priv->ctrl[CTRL_GET] = FIFO_BASE + FIFO_OFFSET;
139 static int ps3vram_wait_ring(struct mtd_info *mtd, int timeout)
141 struct ps3vram_priv *priv = mtd->priv;
143 /* wait until setup commands are processed */
146 if (priv->ctrl[CTRL_PUT] == priv->ctrl[CTRL_GET])
151 pr_err("FIFO timeout (%08x/%08x/%08x)\n", priv->ctrl[CTRL_PUT],
152 priv->ctrl[CTRL_GET], priv->ctrl[CTRL_TOP]);
159 static inline void ps3vram_out_ring(struct ps3vram_priv *priv, uint32_t data)
161 *(priv->fifo_ptr)++ = data;
164 static inline void ps3vram_begin_ring(struct ps3vram_priv *priv, uint32_t chan,
165 uint32_t tag, uint32_t size)
167 ps3vram_out_ring(priv, (size << 18) | (chan << 13) | tag);
170 static void ps3vram_rewind_ring(struct mtd_info *mtd)
172 struct ps3vram_priv *priv = mtd->priv;
175 ps3vram_out_ring(priv, 0x20000000 | (FIFO_BASE + FIFO_OFFSET));
177 priv->ctrl[CTRL_PUT] = FIFO_BASE + FIFO_OFFSET;
179 /* asking the HV for a blit will kick the fifo */
180 status = lv1_gpu_context_attribute(priv->context_handle,
181 L1GPU_CONTEXT_ATTRIBUTE_FB_BLIT,
184 pr_err("ps3vram: lv1_gpu_context_attribute FB_BLIT failed\n");
186 priv->fifo_ptr = priv->fifo_base;
189 static void ps3vram_fire_ring(struct mtd_info *mtd)
191 struct ps3vram_priv *priv = mtd->priv;
194 mutex_lock(&ps3_gpu_mutex);
196 priv->ctrl[CTRL_PUT] = FIFO_BASE + FIFO_OFFSET +
197 (priv->fifo_ptr - priv->fifo_base) * sizeof(uint32_t);
199 /* asking the HV for a blit will kick the fifo */
200 status = lv1_gpu_context_attribute(priv->context_handle,
201 L1GPU_CONTEXT_ATTRIBUTE_FB_BLIT,
204 pr_err("ps3vram: lv1_gpu_context_attribute FB_BLIT failed\n");
206 if ((priv->fifo_ptr - priv->fifo_base) * sizeof(uint32_t) >
208 dbg("fifo full, rewinding");
209 ps3vram_wait_ring(mtd, 200);
210 ps3vram_rewind_ring(mtd);
213 mutex_unlock(&ps3_gpu_mutex);
216 static void ps3vram_bind(struct mtd_info *mtd)
218 struct ps3vram_priv *priv = mtd->priv;
220 ps3vram_begin_ring(priv, UPLOAD_SUBCH, 0, 1);
221 ps3vram_out_ring(priv, 0x31337303);
222 ps3vram_begin_ring(priv, UPLOAD_SUBCH, 0x180, 3);
223 ps3vram_out_ring(priv, DMA_NOTIFIER_HANDLE_BASE + NOTIFIER);
224 ps3vram_out_ring(priv, 0xfeed0001); /* DMA system RAM instance */
225 ps3vram_out_ring(priv, 0xfeed0000); /* DMA video RAM instance */
227 ps3vram_begin_ring(priv, DOWNLOAD_SUBCH, 0, 1);
228 ps3vram_out_ring(priv, 0x3137c0de);
229 ps3vram_begin_ring(priv, DOWNLOAD_SUBCH, 0x180, 3);
230 ps3vram_out_ring(priv, DMA_NOTIFIER_HANDLE_BASE + NOTIFIER);
231 ps3vram_out_ring(priv, 0xfeed0000); /* DMA video RAM instance */
232 ps3vram_out_ring(priv, 0xfeed0001); /* DMA system RAM instance */
234 ps3vram_fire_ring(mtd);
237 static int ps3vram_upload(struct mtd_info *mtd, unsigned int src_offset,
238 unsigned int dst_offset, int len, int count)
240 struct ps3vram_priv *priv = mtd->priv;
242 ps3vram_begin_ring(priv, UPLOAD_SUBCH,
243 NV_MEMORY_TO_MEMORY_FORMAT_OFFSET_IN, 8);
244 ps3vram_out_ring(priv, XDR_IOIF + src_offset);
245 ps3vram_out_ring(priv, dst_offset);
246 ps3vram_out_ring(priv, len);
247 ps3vram_out_ring(priv, len);
248 ps3vram_out_ring(priv, len);
249 ps3vram_out_ring(priv, count);
250 ps3vram_out_ring(priv, (1 << 8) | 1);
251 ps3vram_out_ring(priv, 0);
253 ps3vram_notifier_reset(mtd);
254 ps3vram_begin_ring(priv, UPLOAD_SUBCH,
255 NV_MEMORY_TO_MEMORY_FORMAT_NOTIFY, 1);
256 ps3vram_out_ring(priv, 0);
257 ps3vram_begin_ring(priv, UPLOAD_SUBCH, 0x100, 1);
258 ps3vram_out_ring(priv, 0);
259 ps3vram_fire_ring(mtd);
260 if (ps3vram_notifier_wait(mtd, 200) < 0) {
261 pr_err("notifier timeout\n");
268 static int ps3vram_download(struct mtd_info *mtd, unsigned int src_offset,
269 unsigned int dst_offset, int len, int count)
271 struct ps3vram_priv *priv = mtd->priv;
273 ps3vram_begin_ring(priv, DOWNLOAD_SUBCH,
274 NV_MEMORY_TO_MEMORY_FORMAT_OFFSET_IN, 8);
275 ps3vram_out_ring(priv, src_offset);
276 ps3vram_out_ring(priv, XDR_IOIF + dst_offset);
277 ps3vram_out_ring(priv, len);
278 ps3vram_out_ring(priv, len);
279 ps3vram_out_ring(priv, len);
280 ps3vram_out_ring(priv, count);
281 ps3vram_out_ring(priv, (1 << 8) | 1);
282 ps3vram_out_ring(priv, 0);
284 ps3vram_notifier_reset(mtd);
285 ps3vram_begin_ring(priv, DOWNLOAD_SUBCH,
286 NV_MEMORY_TO_MEMORY_FORMAT_NOTIFY, 1);
287 ps3vram_out_ring(priv, 0);
288 ps3vram_begin_ring(priv, DOWNLOAD_SUBCH, 0x100, 1);
289 ps3vram_out_ring(priv, 0);
290 ps3vram_fire_ring(mtd);
291 if (ps3vram_notifier_wait(mtd, 200) < 0) {
292 pr_err("notifier timeout\n");
299 static void ps3vram_cache_evict(struct mtd_info *mtd, int entry)
301 struct ps3vram_priv *priv = mtd->priv;
302 struct ps3vram_cache *cache = &priv->cache;
304 if (cache->tags[entry].flags & CACHE_PAGE_DIRTY) {
305 dbg("flushing %d : 0x%08x", entry, cache->tags[entry].address);
306 if (ps3vram_upload(mtd,
307 CACHE_OFFSET + entry * cache->page_size,
308 cache->tags[entry].address,
310 cache->page_size / DMA_PAGE_SIZE) < 0) {
311 pr_err("failed to upload from 0x%x to 0x%x size 0x%x\n",
312 entry * cache->page_size,
313 cache->tags[entry].address,
316 cache->tags[entry].flags &= ~CACHE_PAGE_DIRTY;
320 static void ps3vram_cache_load(struct mtd_info *mtd, int entry,
321 unsigned int address)
323 struct ps3vram_priv *priv = mtd->priv;
324 struct ps3vram_cache *cache = &priv->cache;
326 dbg("fetching %d : 0x%08x", entry, address);
327 if (ps3vram_download(mtd,
329 CACHE_OFFSET + entry * cache->page_size,
331 cache->page_size / DMA_PAGE_SIZE) < 0) {
332 pr_err("failed to download from 0x%x to 0x%x size 0x%x\n",
334 entry * cache->page_size,
338 cache->tags[entry].address = address;
339 cache->tags[entry].flags |= CACHE_PAGE_PRESENT;
343 static void ps3vram_cache_flush(struct mtd_info *mtd)
345 struct ps3vram_priv *priv = mtd->priv;
346 struct ps3vram_cache *cache = &priv->cache;
350 for (i = 0; i < cache->page_count; i++) {
351 ps3vram_cache_evict(mtd, i);
352 cache->tags[i].flags = 0;
356 static unsigned int ps3vram_cache_match(struct mtd_info *mtd, loff_t address)
358 struct ps3vram_priv *priv = mtd->priv;
359 struct ps3vram_cache *cache = &priv->cache;
365 offset = (unsigned int) (address & (cache->page_size - 1));
366 base = (unsigned int) (address - offset);
368 /* fully associative check */
369 for (i = 0; i < cache->page_count; i++) {
370 if ((cache->tags[i].flags & CACHE_PAGE_PRESENT) &&
371 cache->tags[i].address == base) {
372 dbg("found entry %d : 0x%08x",
373 i, cache->tags[i].address);
378 /* choose a random entry */
379 i = (jiffies + (counter++)) % cache->page_count;
380 dbg("using cache entry %d", i);
382 ps3vram_cache_evict(mtd, i);
383 ps3vram_cache_load(mtd, i, base);
388 static int ps3vram_cache_init(struct mtd_info *mtd)
390 struct ps3vram_priv *priv = mtd->priv;
392 pr_info("creating cache: %d entries, %d bytes pages\n",
393 CACHE_PAGE_COUNT, CACHE_PAGE_SIZE);
395 priv->cache.page_count = CACHE_PAGE_COUNT;
396 priv->cache.page_size = CACHE_PAGE_SIZE;
397 priv->cache.tags = kzalloc(sizeof(struct ps3vram_tag) *
398 CACHE_PAGE_COUNT, GFP_KERNEL);
399 if (priv->cache.tags == NULL) {
400 pr_err("could not allocate cache tags\n");
407 static void ps3vram_cache_cleanup(struct mtd_info *mtd)
409 struct ps3vram_priv *priv = mtd->priv;
411 ps3vram_cache_flush(mtd);
412 kfree(priv->cache.tags);
415 static int ps3vram_erase(struct mtd_info *mtd, struct erase_info *instr)
417 struct ps3vram_priv *priv = mtd->priv;
419 if (instr->addr + instr->len > mtd->size)
422 mutex_lock(&priv->lock);
424 ps3vram_cache_flush(mtd);
426 /* Set bytes to 0xFF */
427 memset(priv->base + instr->addr, 0xFF, instr->len);
429 mutex_unlock(&priv->lock);
431 instr->state = MTD_ERASE_DONE;
432 mtd_erase_callback(instr);
438 static int ps3vram_read(struct mtd_info *mtd, loff_t from, size_t len,
439 size_t *retlen, u_char *buf)
441 struct ps3vram_priv *priv = mtd->priv;
442 unsigned int cached, count;
444 dbg("from = 0x%08x len = 0x%zx", (unsigned int) from, len);
446 if (from >= mtd->size)
449 if (len > mtd->size - from)
450 len = mtd->size - from;
452 /* Copy from vram to buf */
455 unsigned int offset, avail;
458 offset = (unsigned int) (from & (priv->cache.page_size - 1));
459 avail = priv->cache.page_size - offset;
461 mutex_lock(&priv->lock);
463 entry = ps3vram_cache_match(mtd, from);
464 cached = CACHE_OFFSET + entry * priv->cache.page_size + offset;
466 dbg("from=%08x cached=%08x offset=%08x avail=%08x count=%08x",
467 (unsigned)from, cached, offset, avail, count);
471 memcpy(buf, priv->xdr_buf + cached, avail);
473 mutex_unlock(&priv->lock);
484 static int ps3vram_write(struct mtd_info *mtd, loff_t to, size_t len,
485 size_t *retlen, const u_char *buf)
487 struct ps3vram_priv *priv = mtd->priv;
488 unsigned int cached, count;
493 if (len > mtd->size - to)
494 len = mtd->size - to;
496 /* Copy from buf to vram */
499 unsigned int offset, avail;
502 offset = (unsigned int) (to & (priv->cache.page_size - 1));
503 avail = priv->cache.page_size - offset;
505 mutex_lock(&priv->lock);
507 entry = ps3vram_cache_match(mtd, to);
508 cached = CACHE_OFFSET + entry * priv->cache.page_size + offset;
510 dbg("to=%08x cached=%08x offset=%08x avail=%08x count=%08x",
511 (unsigned) to, cached, offset, avail, count);
515 memcpy(priv->xdr_buf + cached, buf, avail);
517 priv->cache.tags[entry].flags |= CACHE_PAGE_DIRTY;
519 mutex_unlock(&priv->lock);
530 static int __devinit ps3vram_probe(struct ps3_system_bus_device *dev)
532 struct ps3vram_priv *priv;
534 uint64_t ddr_lpar, ctrl_lpar, info_lpar, reports_lpar;
536 uint64_t reports_size;
541 ps3vram_mtd.priv = kzalloc(sizeof(struct ps3vram_priv), GFP_KERNEL);
542 if (!ps3vram_mtd.priv)
544 priv = ps3vram_mtd.priv;
546 mutex_init(&priv->lock);
548 /* Allocate XDR buffer (1MiB aligned) */
549 priv->xdr_buf = (uint8_t *) __get_free_pages(GFP_KERNEL,
550 get_order(XDR_BUF_SIZE));
551 if (priv->xdr_buf == NULL) {
552 pr_err("ps3vram: could not allocate XDR buffer\n");
557 /* Put FIFO at begginning of XDR buffer */
558 priv->fifo_base = (uint32_t *) (priv->xdr_buf + FIFO_OFFSET);
559 priv->fifo_ptr = priv->fifo_base;
561 /* XXX: Need to open GPU, in case ps3fb or snd_ps3 aren't loaded */
562 if (ps3_open_hv_device(dev)) {
563 pr_err("ps3vram: ps3_open_hv_device failed\n");
570 ddr_size = memparse(size, &rest);
572 ddr_size -= ps3fb_videomemory.size;
573 ddr_size = ALIGN(ddr_size, 1024*1024);
575 printk(KERN_ERR "ps3vram: specified size is too small\n");
580 while (ddr_size > 0) {
581 status = lv1_gpu_memory_allocate(ddr_size, 0, 0, 0, 0,
582 &priv->memory_handle,
586 ddr_size -= 1024*1024;
588 if (status != 0 || ddr_size <= 0) {
589 pr_err("ps3vram: lv1_gpu_memory_allocate failed\n");
591 goto out_free_xdr_buf;
593 pr_info("ps3vram: allocated %u MiB of DDR memory\n",
594 (unsigned int) (ddr_size / 1024 / 1024));
596 /* Request context */
597 status = lv1_gpu_context_allocate(priv->memory_handle,
599 &priv->context_handle,
605 pr_err("ps3vram: lv1_gpu_context_allocate failed\n");
607 goto out_free_memory;
610 /* Map XDR buffer to RSX */
611 status = lv1_gpu_context_iomap(priv->context_handle, XDR_IOIF,
612 ps3_mm_phys_to_lpar(__pa(priv->xdr_buf)),
615 pr_err("ps3vram: lv1_gpu_context_iomap failed\n");
617 goto out_free_context;
620 priv->base = ioremap(ddr_lpar, ddr_size);
622 pr_err("ps3vram: ioremap failed\n");
624 goto out_free_context;
627 priv->ctrl = ioremap(ctrl_lpar, 64 * 1024);
629 pr_err("ps3vram: ioremap failed\n");
634 priv->reports = ioremap(reports_lpar, reports_size);
635 if (!priv->reports) {
636 pr_err("ps3vram: ioremap failed\n");
641 mutex_lock(&ps3_gpu_mutex);
642 ps3vram_init_ring(&ps3vram_mtd);
643 mutex_unlock(&ps3_gpu_mutex);
645 ps3vram_mtd.name = "ps3vram";
646 ps3vram_mtd.size = ddr_size;
647 ps3vram_mtd.flags = MTD_CAP_RAM;
648 ps3vram_mtd.erase = ps3vram_erase;
649 ps3vram_mtd.point = NULL;
650 ps3vram_mtd.unpoint = NULL;
651 ps3vram_mtd.read = ps3vram_read;
652 ps3vram_mtd.write = ps3vram_write;
653 ps3vram_mtd.owner = THIS_MODULE;
654 ps3vram_mtd.type = MTD_RAM;
655 ps3vram_mtd.erasesize = CACHE_PAGE_SIZE;
656 ps3vram_mtd.writesize = 1;
658 ps3vram_bind(&ps3vram_mtd);
660 mutex_lock(&ps3_gpu_mutex);
661 ret = ps3vram_wait_ring(&ps3vram_mtd, 100);
662 mutex_unlock(&ps3_gpu_mutex);
664 pr_err("failed to initialize channels\n");
666 goto out_unmap_reports;
669 ps3vram_cache_init(&ps3vram_mtd);
671 if (add_mtd_device(&ps3vram_mtd)) {
672 pr_err("ps3vram: failed to register device\n");
674 goto out_cache_cleanup;
677 pr_info("ps3vram mtd device registered, %lu bytes\n", ddr_size);
681 ps3vram_cache_cleanup(&ps3vram_mtd);
683 iounmap(priv->reports);
689 lv1_gpu_context_free(priv->context_handle);
691 lv1_gpu_memory_free(priv->memory_handle);
693 ps3_close_hv_device(dev);
695 free_pages((unsigned long) priv->xdr_buf, get_order(XDR_BUF_SIZE));
697 kfree(ps3vram_mtd.priv);
698 ps3vram_mtd.priv = NULL;
703 static int ps3vram_shutdown(struct ps3_system_bus_device *dev)
705 struct ps3vram_priv *priv;
707 priv = ps3vram_mtd.priv;
709 del_mtd_device(&ps3vram_mtd);
710 ps3vram_cache_cleanup(&ps3vram_mtd);
711 iounmap(priv->reports);
714 lv1_gpu_context_free(priv->context_handle);
715 lv1_gpu_memory_free(priv->memory_handle);
716 ps3_close_hv_device(dev);
717 free_pages((unsigned long) priv->xdr_buf, get_order(XDR_BUF_SIZE));
722 static struct ps3_system_bus_driver ps3vram_driver = {
723 .match_id = PS3_MATCH_ID_GPU,
724 .match_sub_id = PS3_MATCH_SUB_ID_GPU_RAMDISK,
725 .core.name = DEVICE_NAME,
726 .core.owner = THIS_MODULE,
727 .probe = ps3vram_probe,
728 .remove = ps3vram_shutdown,
729 .shutdown = ps3vram_shutdown,
732 static int __init ps3vram_init(void)
734 return ps3_system_bus_driver_register(&ps3vram_driver);
737 static void __exit ps3vram_exit(void)
739 ps3_system_bus_driver_unregister(&ps3vram_driver);
742 module_init(ps3vram_init);
743 module_exit(ps3vram_exit);
745 MODULE_LICENSE("GPL");
746 MODULE_AUTHOR("Jim Paris <jim@jtan.com>");
747 MODULE_DESCRIPTION("MTD driver for PS3 video RAM");
748 MODULE_ALIAS(PS3_MODULE_ALIAS_GPU_RAMDISK);