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
61 struct ps3vram_cache {
62 unsigned int page_count;
63 unsigned int page_size;
64 struct ps3vram_tag *tags;
68 uint64_t memory_handle;
69 uint64_t context_handle;
79 struct ps3vram_cache cache;
81 /* Used to serialize cache/DMA operations */
85 #define DMA_NOTIFIER_HANDLE_BASE 0x66604200 /* first DMA notifier handle */
86 #define DMA_NOTIFIER_OFFSET_BASE 0x1000 /* first DMA notifier offset */
87 #define DMA_NOTIFIER_SIZE 0x40
88 #define NOTIFIER 7 /* notifier used for completion report */
90 /* A trailing '-' means to subtract off ps3fb_videomemory.size */
92 module_param(size, charp, 0);
93 MODULE_PARM_DESC(size, "memory size");
95 static inline uint32_t *ps3vram_get_notifier(uint32_t *reports, int notifier)
97 return (void *) reports +
98 DMA_NOTIFIER_OFFSET_BASE +
99 DMA_NOTIFIER_SIZE * notifier;
102 static void ps3vram_notifier_reset(struct mtd_info *mtd)
105 struct ps3vram_priv *priv = mtd->priv;
106 uint32_t *notify = ps3vram_get_notifier(priv->reports, NOTIFIER);
107 for (i = 0; i < 4; i++)
108 notify[i] = 0xffffffff;
111 static int ps3vram_notifier_wait(struct mtd_info *mtd, int timeout_ms)
113 struct ps3vram_priv *priv = mtd->priv;
114 uint32_t *notify = ps3vram_get_notifier(priv->reports, NOTIFIER);
124 } while (timeout_ms--);
129 static void ps3vram_init_ring(struct mtd_info *mtd)
131 struct ps3vram_priv *priv = mtd->priv;
133 priv->ctrl[CTRL_PUT] = FIFO_BASE + FIFO_OFFSET;
134 priv->ctrl[CTRL_GET] = FIFO_BASE + FIFO_OFFSET;
137 static int ps3vram_wait_ring(struct mtd_info *mtd, int timeout)
139 struct ps3vram_priv *priv = mtd->priv;
141 /* wait until setup commands are processed */
144 if (priv->ctrl[CTRL_PUT] == priv->ctrl[CTRL_GET])
149 dev_dbg(priv->dev, "%s:%d: FIFO timeout (%08x/%08x/%08x)\n",
150 __func__, __LINE__, priv->ctrl[CTRL_PUT],
151 priv->ctrl[CTRL_GET], priv->ctrl[CTRL_TOP]);
158 static inline void ps3vram_out_ring(struct ps3vram_priv *priv, uint32_t data)
160 *(priv->fifo_ptr)++ = data;
163 static inline void ps3vram_begin_ring(struct ps3vram_priv *priv, uint32_t chan,
164 uint32_t tag, uint32_t size)
166 ps3vram_out_ring(priv, (size << 18) | (chan << 13) | tag);
169 static void ps3vram_rewind_ring(struct mtd_info *mtd)
171 struct ps3vram_priv *priv = mtd->priv;
174 ps3vram_out_ring(priv, 0x20000000 | (FIFO_BASE + FIFO_OFFSET));
176 priv->ctrl[CTRL_PUT] = FIFO_BASE + FIFO_OFFSET;
178 /* asking the HV for a blit will kick the fifo */
179 status = lv1_gpu_context_attribute(priv->context_handle,
180 L1GPU_CONTEXT_ATTRIBUTE_FB_BLIT,
183 dev_err(priv->dev, "%s:%d: lv1_gpu_context_attribute 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 dev_err(priv->dev, "%s:%d: lv1_gpu_context_attribute failed\n",
207 if ((priv->fifo_ptr - priv->fifo_base) * sizeof(uint32_t) >
209 dev_dbg(priv->dev, "%s:%d: fifo full, rewinding\n", __func__,
211 ps3vram_wait_ring(mtd, 200);
212 ps3vram_rewind_ring(mtd);
215 mutex_unlock(&ps3_gpu_mutex);
218 static void ps3vram_bind(struct mtd_info *mtd)
220 struct ps3vram_priv *priv = mtd->priv;
222 ps3vram_begin_ring(priv, UPLOAD_SUBCH, 0, 1);
223 ps3vram_out_ring(priv, 0x31337303);
224 ps3vram_begin_ring(priv, UPLOAD_SUBCH, 0x180, 3);
225 ps3vram_out_ring(priv, DMA_NOTIFIER_HANDLE_BASE + NOTIFIER);
226 ps3vram_out_ring(priv, 0xfeed0001); /* DMA system RAM instance */
227 ps3vram_out_ring(priv, 0xfeed0000); /* DMA video RAM instance */
229 ps3vram_begin_ring(priv, DOWNLOAD_SUBCH, 0, 1);
230 ps3vram_out_ring(priv, 0x3137c0de);
231 ps3vram_begin_ring(priv, DOWNLOAD_SUBCH, 0x180, 3);
232 ps3vram_out_ring(priv, DMA_NOTIFIER_HANDLE_BASE + NOTIFIER);
233 ps3vram_out_ring(priv, 0xfeed0000); /* DMA video RAM instance */
234 ps3vram_out_ring(priv, 0xfeed0001); /* DMA system RAM instance */
236 ps3vram_fire_ring(mtd);
239 static int ps3vram_upload(struct mtd_info *mtd, unsigned int src_offset,
240 unsigned int dst_offset, int len, int count)
242 struct ps3vram_priv *priv = mtd->priv;
244 ps3vram_begin_ring(priv, UPLOAD_SUBCH,
245 NV_MEMORY_TO_MEMORY_FORMAT_OFFSET_IN, 8);
246 ps3vram_out_ring(priv, XDR_IOIF + src_offset);
247 ps3vram_out_ring(priv, dst_offset);
248 ps3vram_out_ring(priv, len);
249 ps3vram_out_ring(priv, len);
250 ps3vram_out_ring(priv, len);
251 ps3vram_out_ring(priv, count);
252 ps3vram_out_ring(priv, (1 << 8) | 1);
253 ps3vram_out_ring(priv, 0);
255 ps3vram_notifier_reset(mtd);
256 ps3vram_begin_ring(priv, UPLOAD_SUBCH,
257 NV_MEMORY_TO_MEMORY_FORMAT_NOTIFY, 1);
258 ps3vram_out_ring(priv, 0);
259 ps3vram_begin_ring(priv, UPLOAD_SUBCH, 0x100, 1);
260 ps3vram_out_ring(priv, 0);
261 ps3vram_fire_ring(mtd);
262 if (ps3vram_notifier_wait(mtd, 200) < 0) {
263 dev_dbg(priv->dev, "%s:%d: notifier timeout\n", __func__,
271 static int ps3vram_download(struct mtd_info *mtd, unsigned int src_offset,
272 unsigned int dst_offset, int len, int count)
274 struct ps3vram_priv *priv = mtd->priv;
276 ps3vram_begin_ring(priv, DOWNLOAD_SUBCH,
277 NV_MEMORY_TO_MEMORY_FORMAT_OFFSET_IN, 8);
278 ps3vram_out_ring(priv, src_offset);
279 ps3vram_out_ring(priv, XDR_IOIF + dst_offset);
280 ps3vram_out_ring(priv, len);
281 ps3vram_out_ring(priv, len);
282 ps3vram_out_ring(priv, len);
283 ps3vram_out_ring(priv, count);
284 ps3vram_out_ring(priv, (1 << 8) | 1);
285 ps3vram_out_ring(priv, 0);
287 ps3vram_notifier_reset(mtd);
288 ps3vram_begin_ring(priv, DOWNLOAD_SUBCH,
289 NV_MEMORY_TO_MEMORY_FORMAT_NOTIFY, 1);
290 ps3vram_out_ring(priv, 0);
291 ps3vram_begin_ring(priv, DOWNLOAD_SUBCH, 0x100, 1);
292 ps3vram_out_ring(priv, 0);
293 ps3vram_fire_ring(mtd);
294 if (ps3vram_notifier_wait(mtd, 200) < 0) {
295 dev_dbg(priv->dev, "%s:%d: notifier timeout\n", __func__,
303 static void ps3vram_cache_evict(struct mtd_info *mtd, int entry)
305 struct ps3vram_priv *priv = mtd->priv;
306 struct ps3vram_cache *cache = &priv->cache;
308 if (cache->tags[entry].flags & CACHE_PAGE_DIRTY) {
309 dev_dbg(priv->dev, "%s:%d: flushing %d : 0x%08x\n", __func__,
310 __LINE__, entry, cache->tags[entry].address);
311 if (ps3vram_upload(mtd,
312 CACHE_OFFSET + entry * cache->page_size,
313 cache->tags[entry].address,
315 cache->page_size / DMA_PAGE_SIZE) < 0) {
316 dev_dbg(priv->dev, "%s:%d: failed to upload from "
317 "0x%x to 0x%x size 0x%x\n", __func__, __LINE__,
318 entry * cache->page_size,
319 cache->tags[entry].address, cache->page_size);
321 cache->tags[entry].flags &= ~CACHE_PAGE_DIRTY;
325 static void ps3vram_cache_load(struct mtd_info *mtd, int entry,
326 unsigned int address)
328 struct ps3vram_priv *priv = mtd->priv;
329 struct ps3vram_cache *cache = &priv->cache;
331 dev_dbg(priv->dev, "%s:%d: fetching %d : 0x%08x\n", __func__, __LINE__,
333 if (ps3vram_download(mtd,
335 CACHE_OFFSET + entry * cache->page_size,
337 cache->page_size / DMA_PAGE_SIZE) < 0) {
338 dev_err(priv->dev, "%s:%d: failed to download from "
339 "0x%x to 0x%x size 0x%x\n", __func__, __LINE__, address,
340 entry * cache->page_size, cache->page_size);
343 cache->tags[entry].address = address;
344 cache->tags[entry].flags |= CACHE_PAGE_PRESENT;
348 static void ps3vram_cache_flush(struct mtd_info *mtd)
350 struct ps3vram_priv *priv = mtd->priv;
351 struct ps3vram_cache *cache = &priv->cache;
354 dev_dbg(priv->dev, "%s:%d: FLUSH\n", __func__, __LINE__);
355 for (i = 0; i < cache->page_count; i++) {
356 ps3vram_cache_evict(mtd, i);
357 cache->tags[i].flags = 0;
361 static unsigned int ps3vram_cache_match(struct mtd_info *mtd, loff_t address)
363 struct ps3vram_priv *priv = mtd->priv;
364 struct ps3vram_cache *cache = &priv->cache;
370 offset = (unsigned int) (address & (cache->page_size - 1));
371 base = (unsigned int) (address - offset);
373 /* fully associative check */
374 for (i = 0; i < cache->page_count; i++) {
375 if ((cache->tags[i].flags & CACHE_PAGE_PRESENT) &&
376 cache->tags[i].address == base) {
377 dev_dbg(priv->dev, "%s:%d: found entry %d : 0x%08x\n",
378 __func__, __LINE__, i, cache->tags[i].address);
383 /* choose a random entry */
384 i = (jiffies + (counter++)) % cache->page_count;
385 dev_dbg(priv->dev, "%s:%d: using entry %d\n", __func__, __LINE__, i);
387 ps3vram_cache_evict(mtd, i);
388 ps3vram_cache_load(mtd, i, base);
393 static int ps3vram_cache_init(struct mtd_info *mtd)
395 struct ps3vram_priv *priv = mtd->priv;
397 priv->cache.page_count = CACHE_PAGE_COUNT;
398 priv->cache.page_size = CACHE_PAGE_SIZE;
399 priv->cache.tags = kzalloc(sizeof(struct ps3vram_tag) *
400 CACHE_PAGE_COUNT, GFP_KERNEL);
401 if (priv->cache.tags == NULL) {
402 dev_err(priv->dev, "%s:%d: could not allocate cache tags\n",
407 dev_info(priv->dev, "created ram cache: %d entries, %d KiB each\n",
408 CACHE_PAGE_COUNT, CACHE_PAGE_SIZE / 1024);
413 static void ps3vram_cache_cleanup(struct mtd_info *mtd)
415 struct ps3vram_priv *priv = mtd->priv;
417 ps3vram_cache_flush(mtd);
418 kfree(priv->cache.tags);
421 static int ps3vram_erase(struct mtd_info *mtd, struct erase_info *instr)
423 struct ps3vram_priv *priv = mtd->priv;
425 if (instr->addr + instr->len > mtd->size)
428 mutex_lock(&priv->lock);
430 ps3vram_cache_flush(mtd);
432 /* Set bytes to 0xFF */
433 memset(priv->base + instr->addr, 0xFF, instr->len);
435 mutex_unlock(&priv->lock);
437 instr->state = MTD_ERASE_DONE;
438 mtd_erase_callback(instr);
443 static int ps3vram_read(struct mtd_info *mtd, loff_t from, size_t len,
444 size_t *retlen, u_char *buf)
446 struct ps3vram_priv *priv = mtd->priv;
447 unsigned int cached, count;
449 dev_dbg(priv->dev, "%s:%d: from=0x%08x len=0x%zx\n", __func__, __LINE__,
450 (unsigned int)from, len);
452 if (from >= mtd->size)
455 if (len > mtd->size - from)
456 len = mtd->size - from;
458 /* Copy from vram to buf */
461 unsigned int offset, avail;
464 offset = (unsigned int) (from & (priv->cache.page_size - 1));
465 avail = priv->cache.page_size - offset;
467 mutex_lock(&priv->lock);
469 entry = ps3vram_cache_match(mtd, from);
470 cached = CACHE_OFFSET + entry * priv->cache.page_size + offset;
472 dev_dbg(priv->dev, "%s:%d: from=%08x cached=%08x offset=%08x "
473 "avail=%08x count=%08x\n", __func__, __LINE__,
474 (unsigned int)from, cached, offset, avail, count);
478 memcpy(buf, priv->xdr_buf + cached, avail);
480 mutex_unlock(&priv->lock);
491 static int ps3vram_write(struct mtd_info *mtd, loff_t to, size_t len,
492 size_t *retlen, const u_char *buf)
494 struct ps3vram_priv *priv = mtd->priv;
495 unsigned int cached, count;
500 if (len > mtd->size - to)
501 len = mtd->size - to;
503 /* Copy from buf to vram */
506 unsigned int offset, avail;
509 offset = (unsigned int) (to & (priv->cache.page_size - 1));
510 avail = priv->cache.page_size - offset;
512 mutex_lock(&priv->lock);
514 entry = ps3vram_cache_match(mtd, to);
515 cached = CACHE_OFFSET + entry * priv->cache.page_size + offset;
517 dev_dbg(priv->dev, "%s:%d: to=%08x cached=%08x offset=%08x "
518 "avail=%08x count=%08x\n", __func__, __LINE__,
519 (unsigned int)to, cached, offset, avail, count);
523 memcpy(priv->xdr_buf + cached, buf, avail);
525 priv->cache.tags[entry].flags |= CACHE_PAGE_DIRTY;
527 mutex_unlock(&priv->lock);
538 static int __devinit ps3vram_probe(struct ps3_system_bus_device *dev)
540 struct ps3vram_priv *priv;
542 uint64_t ddr_lpar, ctrl_lpar, info_lpar, reports_lpar;
544 uint64_t reports_size;
549 ps3vram_mtd.priv = kzalloc(sizeof(struct ps3vram_priv), GFP_KERNEL);
550 if (!ps3vram_mtd.priv)
552 priv = ps3vram_mtd.priv;
554 mutex_init(&priv->lock);
555 priv->dev = &dev->core;
557 /* Allocate XDR buffer (1MiB aligned) */
558 priv->xdr_buf = (uint8_t *) __get_free_pages(GFP_KERNEL,
559 get_order(XDR_BUF_SIZE));
560 if (priv->xdr_buf == NULL) {
561 dev_dbg(&dev->core, "%s:%d: could not allocate XDR buffer\n",
567 /* Put FIFO at begginning of XDR buffer */
568 priv->fifo_base = (uint32_t *) (priv->xdr_buf + FIFO_OFFSET);
569 priv->fifo_ptr = priv->fifo_base;
571 /* XXX: Need to open GPU, in case ps3fb or snd_ps3 aren't loaded */
572 if (ps3_open_hv_device(dev)) {
573 dev_err(&dev->core, "%s:%d: ps3_open_hv_device failed\n",
581 ddr_size = memparse(size, &rest);
583 ddr_size -= ps3fb_videomemory.size;
584 ddr_size = ALIGN(ddr_size, 1024*1024);
586 dev_err(&dev->core, "%s:%d: specified size is too small\n",
592 while (ddr_size > 0) {
593 status = lv1_gpu_memory_allocate(ddr_size, 0, 0, 0, 0,
594 &priv->memory_handle,
598 ddr_size -= 1024*1024;
600 if (status != 0 || ddr_size <= 0) {
601 dev_err(&dev->core, "%s:%d: lv1_gpu_memory_allocate failed\n",
604 goto out_free_xdr_buf;
607 /* Request context */
608 status = lv1_gpu_context_allocate(priv->memory_handle,
610 &priv->context_handle,
616 dev_err(&dev->core, "%s:%d: lv1_gpu_context_allocate failed\n",
619 goto out_free_memory;
622 /* Map XDR buffer to RSX */
623 status = lv1_gpu_context_iomap(priv->context_handle, XDR_IOIF,
624 ps3_mm_phys_to_lpar(__pa(priv->xdr_buf)),
627 dev_err(&dev->core, "%s:%d: lv1_gpu_context_iomap failed\n",
630 goto out_free_context;
633 priv->base = ioremap(ddr_lpar, ddr_size);
635 dev_err(&dev->core, "%s:%d: ioremap failed\n", __func__,
638 goto out_free_context;
641 priv->ctrl = ioremap(ctrl_lpar, 64 * 1024);
643 dev_err(&dev->core, "%s:%d: ioremap failed\n", __func__,
649 priv->reports = ioremap(reports_lpar, reports_size);
650 if (!priv->reports) {
651 dev_err(&dev->core, "%s:%d: ioremap failed\n", __func__,
657 mutex_lock(&ps3_gpu_mutex);
658 ps3vram_init_ring(&ps3vram_mtd);
659 mutex_unlock(&ps3_gpu_mutex);
661 ps3vram_mtd.name = "ps3vram";
662 ps3vram_mtd.size = ddr_size;
663 ps3vram_mtd.flags = MTD_CAP_RAM;
664 ps3vram_mtd.erase = ps3vram_erase;
665 ps3vram_mtd.point = NULL;
666 ps3vram_mtd.unpoint = NULL;
667 ps3vram_mtd.read = ps3vram_read;
668 ps3vram_mtd.write = ps3vram_write;
669 ps3vram_mtd.owner = THIS_MODULE;
670 ps3vram_mtd.type = MTD_RAM;
671 ps3vram_mtd.erasesize = CACHE_PAGE_SIZE;
672 ps3vram_mtd.writesize = 1;
674 ps3vram_bind(&ps3vram_mtd);
676 mutex_lock(&ps3_gpu_mutex);
677 ret = ps3vram_wait_ring(&ps3vram_mtd, 100);
678 mutex_unlock(&ps3_gpu_mutex);
680 dev_err(&dev->core, "%s:%d: failed to initialize channels\n",
683 goto out_unmap_reports;
686 ps3vram_cache_init(&ps3vram_mtd);
688 if (add_mtd_device(&ps3vram_mtd)) {
689 dev_err(&dev->core, "%s:%d: add_mtd_device failed\n",
692 goto out_cache_cleanup;
695 dev_info(&dev->core, "reserved %u MiB of gpu memory\n",
696 (unsigned int)(ddr_size / 1024 / 1024));
701 ps3vram_cache_cleanup(&ps3vram_mtd);
703 iounmap(priv->reports);
709 lv1_gpu_context_free(priv->context_handle);
711 lv1_gpu_memory_free(priv->memory_handle);
713 ps3_close_hv_device(dev);
715 free_pages((unsigned long) priv->xdr_buf, get_order(XDR_BUF_SIZE));
717 kfree(ps3vram_mtd.priv);
718 ps3vram_mtd.priv = NULL;
723 static int ps3vram_shutdown(struct ps3_system_bus_device *dev)
725 struct ps3vram_priv *priv;
727 priv = ps3vram_mtd.priv;
729 del_mtd_device(&ps3vram_mtd);
730 ps3vram_cache_cleanup(&ps3vram_mtd);
731 iounmap(priv->reports);
734 lv1_gpu_context_free(priv->context_handle);
735 lv1_gpu_memory_free(priv->memory_handle);
736 ps3_close_hv_device(dev);
737 free_pages((unsigned long) priv->xdr_buf, get_order(XDR_BUF_SIZE));
742 static struct ps3_system_bus_driver ps3vram_driver = {
743 .match_id = PS3_MATCH_ID_GPU,
744 .match_sub_id = PS3_MATCH_SUB_ID_GPU_RAMDISK,
745 .core.name = DEVICE_NAME,
746 .core.owner = THIS_MODULE,
747 .probe = ps3vram_probe,
748 .remove = ps3vram_shutdown,
749 .shutdown = ps3vram_shutdown,
752 static int __init ps3vram_init(void)
754 return ps3_system_bus_driver_register(&ps3vram_driver);
757 static void __exit ps3vram_exit(void)
759 ps3_system_bus_driver_unregister(&ps3vram_driver);
762 module_init(ps3vram_init);
763 module_exit(ps3vram_exit);
765 MODULE_LICENSE("GPL");
766 MODULE_AUTHOR("Jim Paris <jim@jtan.com>");
767 MODULE_DESCRIPTION("MTD driver for PS3 video RAM");
768 MODULE_ALIAS(PS3_MODULE_ALIAS_GPU_RAMDISK);