blob: 2c2ea9cb207b3ddfc88c12a82c468e3c714df918 [file] [log] [blame]
Geert Uytterhoevenf507cd22009-03-06 02:54:09 +00001/*
2 * ps3vram - Use extra PS3 video ram as MTD block device.
3 *
4 * Copyright 2009 Sony Corporation
5 *
6 * Based on the MTD ps3vram driver, which is
7 * Copyright (c) 2007-2008 Jim Paris <jim@jtan.com>
8 * Added support RSX DMA Vivien Chappelier <vivien.chappelier@free.fr>
9 */
10
11#include <linux/blkdev.h>
12#include <linux/delay.h>
13#include <linux/proc_fs.h>
14#include <linux/seq_file.h>
15
16#include <asm/firmware.h>
Geert Uytterhoeven56ac72d2009-06-10 04:38:47 +000017#include <asm/iommu.h>
Geert Uytterhoevenf507cd22009-03-06 02:54:09 +000018#include <asm/lv1call.h>
19#include <asm/ps3.h>
Geert Uytterhoevend3352c92009-06-10 04:38:48 +000020#include <asm/ps3gpu.h>
Geert Uytterhoevenf507cd22009-03-06 02:54:09 +000021
22
23#define DEVICE_NAME "ps3vram"
24
25
26#define XDR_BUF_SIZE (2 * 1024 * 1024) /* XDR buffer (must be 1MiB aligned) */
27#define XDR_IOIF 0x0c000000
28
29#define FIFO_BASE XDR_IOIF
30#define FIFO_SIZE (64 * 1024)
31
32#define DMA_PAGE_SIZE (4 * 1024)
33
34#define CACHE_PAGE_SIZE (256 * 1024)
35#define CACHE_PAGE_COUNT ((XDR_BUF_SIZE - FIFO_SIZE) / CACHE_PAGE_SIZE)
36
37#define CACHE_OFFSET CACHE_PAGE_SIZE
38#define FIFO_OFFSET 0
39
40#define CTRL_PUT 0x10
41#define CTRL_GET 0x11
42#define CTRL_TOP 0x15
43
44#define UPLOAD_SUBCH 1
45#define DOWNLOAD_SUBCH 2
46
47#define NV_MEMORY_TO_MEMORY_FORMAT_OFFSET_IN 0x0000030c
48#define NV_MEMORY_TO_MEMORY_FORMAT_NOTIFY 0x00000104
49
Geert Uytterhoevenf507cd22009-03-06 02:54:09 +000050#define CACHE_PAGE_PRESENT 1
51#define CACHE_PAGE_DIRTY 2
52
53struct ps3vram_tag {
54 unsigned int address;
55 unsigned int flags;
56};
57
58struct ps3vram_cache {
59 unsigned int page_count;
60 unsigned int page_size;
61 struct ps3vram_tag *tags;
62 unsigned int hit;
63 unsigned int miss;
64};
65
66struct ps3vram_priv {
67 struct request_queue *queue;
68 struct gendisk *gendisk;
69
70 u64 size;
71
72 u64 memory_handle;
73 u64 context_handle;
74 u32 *ctrl;
75 u32 *reports;
76 u8 __iomem *ddr_base;
77 u8 *xdr_buf;
78
79 u32 *fifo_base;
80 u32 *fifo_ptr;
81
82 struct ps3vram_cache cache;
83
84 /* Used to serialize cache/DMA operations */
85 struct mutex lock;
86};
87
88
89static int ps3vram_major;
90
91
92static struct block_device_operations ps3vram_fops = {
93 .owner = THIS_MODULE,
94};
95
96
97#define DMA_NOTIFIER_HANDLE_BASE 0x66604200 /* first DMA notifier handle */
98#define DMA_NOTIFIER_OFFSET_BASE 0x1000 /* first DMA notifier offset */
99#define DMA_NOTIFIER_SIZE 0x40
100#define NOTIFIER 7 /* notifier used for completion report */
101
102static char *size = "256M";
103module_param(size, charp, 0);
104MODULE_PARM_DESC(size, "memory size");
105
106static u32 *ps3vram_get_notifier(u32 *reports, int notifier)
107{
108 return (void *)reports + DMA_NOTIFIER_OFFSET_BASE +
109 DMA_NOTIFIER_SIZE * notifier;
110}
111
112static void ps3vram_notifier_reset(struct ps3_system_bus_device *dev)
113{
114 struct ps3vram_priv *priv = dev->core.driver_data;
115 u32 *notify = ps3vram_get_notifier(priv->reports, NOTIFIER);
116 int i;
117
118 for (i = 0; i < 4; i++)
119 notify[i] = 0xffffffff;
120}
121
122static int ps3vram_notifier_wait(struct ps3_system_bus_device *dev,
123 unsigned int timeout_ms)
124{
125 struct ps3vram_priv *priv = dev->core.driver_data;
126 u32 *notify = ps3vram_get_notifier(priv->reports, NOTIFIER);
127 unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
128
129 do {
130 if (!notify[3])
131 return 0;
132 msleep(1);
133 } while (time_before(jiffies, timeout));
134
135 return -ETIMEDOUT;
136}
137
138static void ps3vram_init_ring(struct ps3_system_bus_device *dev)
139{
140 struct ps3vram_priv *priv = dev->core.driver_data;
141
142 priv->ctrl[CTRL_PUT] = FIFO_BASE + FIFO_OFFSET;
143 priv->ctrl[CTRL_GET] = FIFO_BASE + FIFO_OFFSET;
144}
145
146static int ps3vram_wait_ring(struct ps3_system_bus_device *dev,
147 unsigned int timeout_ms)
148{
149 struct ps3vram_priv *priv = dev->core.driver_data;
150 unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
151
152 do {
153 if (priv->ctrl[CTRL_PUT] == priv->ctrl[CTRL_GET])
154 return 0;
155 msleep(1);
156 } while (time_before(jiffies, timeout));
157
158 dev_warn(&dev->core, "FIFO timeout (%08x/%08x/%08x)\n",
159 priv->ctrl[CTRL_PUT], priv->ctrl[CTRL_GET],
160 priv->ctrl[CTRL_TOP]);
161
162 return -ETIMEDOUT;
163}
164
165static void ps3vram_out_ring(struct ps3vram_priv *priv, u32 data)
166{
167 *(priv->fifo_ptr)++ = data;
168}
169
170static void ps3vram_begin_ring(struct ps3vram_priv *priv, u32 chan, u32 tag,
171 u32 size)
172{
173 ps3vram_out_ring(priv, (size << 18) | (chan << 13) | tag);
174}
175
176static void ps3vram_rewind_ring(struct ps3_system_bus_device *dev)
177{
178 struct ps3vram_priv *priv = dev->core.driver_data;
179 int status;
180
181 ps3vram_out_ring(priv, 0x20000000 | (FIFO_BASE + FIFO_OFFSET));
182
183 priv->ctrl[CTRL_PUT] = FIFO_BASE + FIFO_OFFSET;
184
185 /* asking the HV for a blit will kick the FIFO */
Geert Uytterhoevend3352c92009-06-10 04:38:48 +0000186 status = lv1_gpu_fb_blit(priv->context_handle, 0, 0, 0, 0);
Geert Uytterhoevenf507cd22009-03-06 02:54:09 +0000187 if (status)
Geert Uytterhoevend3352c92009-06-10 04:38:48 +0000188 dev_err(&dev->core, "%s: lv1_gpu_fb_blit failed %d\n",
189 __func__, status);
Geert Uytterhoevenf507cd22009-03-06 02:54:09 +0000190
191 priv->fifo_ptr = priv->fifo_base;
192}
193
194static void ps3vram_fire_ring(struct ps3_system_bus_device *dev)
195{
196 struct ps3vram_priv *priv = dev->core.driver_data;
197 int status;
198
199 mutex_lock(&ps3_gpu_mutex);
200
201 priv->ctrl[CTRL_PUT] = FIFO_BASE + FIFO_OFFSET +
202 (priv->fifo_ptr - priv->fifo_base) * sizeof(u32);
203
204 /* asking the HV for a blit will kick the FIFO */
Geert Uytterhoevend3352c92009-06-10 04:38:48 +0000205 status = lv1_gpu_fb_blit(priv->context_handle, 0, 0, 0, 0);
Geert Uytterhoevenf507cd22009-03-06 02:54:09 +0000206 if (status)
Geert Uytterhoevend3352c92009-06-10 04:38:48 +0000207 dev_err(&dev->core, "%s: lv1_gpu_fb_blit failed %d\n",
208 __func__, status);
Geert Uytterhoevenf507cd22009-03-06 02:54:09 +0000209
210 if ((priv->fifo_ptr - priv->fifo_base) * sizeof(u32) >
211 FIFO_SIZE - 1024) {
212 dev_dbg(&dev->core, "FIFO full, rewinding\n");
213 ps3vram_wait_ring(dev, 200);
214 ps3vram_rewind_ring(dev);
215 }
216
217 mutex_unlock(&ps3_gpu_mutex);
218}
219
220static void ps3vram_bind(struct ps3_system_bus_device *dev)
221{
222 struct ps3vram_priv *priv = dev->core.driver_data;
223
224 ps3vram_begin_ring(priv, UPLOAD_SUBCH, 0, 1);
225 ps3vram_out_ring(priv, 0x31337303);
226 ps3vram_begin_ring(priv, UPLOAD_SUBCH, 0x180, 3);
227 ps3vram_out_ring(priv, DMA_NOTIFIER_HANDLE_BASE + NOTIFIER);
228 ps3vram_out_ring(priv, 0xfeed0001); /* DMA system RAM instance */
229 ps3vram_out_ring(priv, 0xfeed0000); /* DMA video RAM instance */
230
231 ps3vram_begin_ring(priv, DOWNLOAD_SUBCH, 0, 1);
232 ps3vram_out_ring(priv, 0x3137c0de);
233 ps3vram_begin_ring(priv, DOWNLOAD_SUBCH, 0x180, 3);
234 ps3vram_out_ring(priv, DMA_NOTIFIER_HANDLE_BASE + NOTIFIER);
235 ps3vram_out_ring(priv, 0xfeed0000); /* DMA video RAM instance */
236 ps3vram_out_ring(priv, 0xfeed0001); /* DMA system RAM instance */
237
238 ps3vram_fire_ring(dev);
239}
240
241static int ps3vram_upload(struct ps3_system_bus_device *dev,
242 unsigned int src_offset, unsigned int dst_offset,
243 int len, int count)
244{
245 struct ps3vram_priv *priv = dev->core.driver_data;
246
247 ps3vram_begin_ring(priv, UPLOAD_SUBCH,
248 NV_MEMORY_TO_MEMORY_FORMAT_OFFSET_IN, 8);
249 ps3vram_out_ring(priv, XDR_IOIF + src_offset);
250 ps3vram_out_ring(priv, dst_offset);
251 ps3vram_out_ring(priv, len);
252 ps3vram_out_ring(priv, len);
253 ps3vram_out_ring(priv, len);
254 ps3vram_out_ring(priv, count);
255 ps3vram_out_ring(priv, (1 << 8) | 1);
256 ps3vram_out_ring(priv, 0);
257
258 ps3vram_notifier_reset(dev);
259 ps3vram_begin_ring(priv, UPLOAD_SUBCH,
260 NV_MEMORY_TO_MEMORY_FORMAT_NOTIFY, 1);
261 ps3vram_out_ring(priv, 0);
262 ps3vram_begin_ring(priv, UPLOAD_SUBCH, 0x100, 1);
263 ps3vram_out_ring(priv, 0);
264 ps3vram_fire_ring(dev);
265 if (ps3vram_notifier_wait(dev, 200) < 0) {
266 dev_warn(&dev->core, "%s: Notifier timeout\n", __func__);
267 return -1;
268 }
269
270 return 0;
271}
272
273static int ps3vram_download(struct ps3_system_bus_device *dev,
274 unsigned int src_offset, unsigned int dst_offset,
275 int len, int count)
276{
277 struct ps3vram_priv *priv = dev->core.driver_data;
278
279 ps3vram_begin_ring(priv, DOWNLOAD_SUBCH,
280 NV_MEMORY_TO_MEMORY_FORMAT_OFFSET_IN, 8);
281 ps3vram_out_ring(priv, src_offset);
282 ps3vram_out_ring(priv, XDR_IOIF + dst_offset);
283 ps3vram_out_ring(priv, len);
284 ps3vram_out_ring(priv, len);
285 ps3vram_out_ring(priv, len);
286 ps3vram_out_ring(priv, count);
287 ps3vram_out_ring(priv, (1 << 8) | 1);
288 ps3vram_out_ring(priv, 0);
289
290 ps3vram_notifier_reset(dev);
291 ps3vram_begin_ring(priv, DOWNLOAD_SUBCH,
292 NV_MEMORY_TO_MEMORY_FORMAT_NOTIFY, 1);
293 ps3vram_out_ring(priv, 0);
294 ps3vram_begin_ring(priv, DOWNLOAD_SUBCH, 0x100, 1);
295 ps3vram_out_ring(priv, 0);
296 ps3vram_fire_ring(dev);
297 if (ps3vram_notifier_wait(dev, 200) < 0) {
298 dev_warn(&dev->core, "%s: Notifier timeout\n", __func__);
299 return -1;
300 }
301
302 return 0;
303}
304
305static void ps3vram_cache_evict(struct ps3_system_bus_device *dev, int entry)
306{
307 struct ps3vram_priv *priv = dev->core.driver_data;
308 struct ps3vram_cache *cache = &priv->cache;
309
310 if (!(cache->tags[entry].flags & CACHE_PAGE_DIRTY))
311 return;
312
313 dev_dbg(&dev->core, "Flushing %d: 0x%08x\n", entry,
314 cache->tags[entry].address);
315 if (ps3vram_upload(dev, CACHE_OFFSET + entry * cache->page_size,
316 cache->tags[entry].address, DMA_PAGE_SIZE,
317 cache->page_size / DMA_PAGE_SIZE) < 0) {
318 dev_err(&dev->core,
319 "Failed to upload from 0x%x to " "0x%x size 0x%x\n",
320 entry * cache->page_size, cache->tags[entry].address,
321 cache->page_size);
322 }
323 cache->tags[entry].flags &= ~CACHE_PAGE_DIRTY;
324}
325
326static void ps3vram_cache_load(struct ps3_system_bus_device *dev, int entry,
327 unsigned int address)
328{
329 struct ps3vram_priv *priv = dev->core.driver_data;
330 struct ps3vram_cache *cache = &priv->cache;
331
332 dev_dbg(&dev->core, "Fetching %d: 0x%08x\n", entry, address);
333 if (ps3vram_download(dev, address,
334 CACHE_OFFSET + entry * cache->page_size,
335 DMA_PAGE_SIZE,
336 cache->page_size / DMA_PAGE_SIZE) < 0) {
337 dev_err(&dev->core,
338 "Failed to download from 0x%x to 0x%x size 0x%x\n",
339 address, entry * cache->page_size, cache->page_size);
340 }
341
342 cache->tags[entry].address = address;
343 cache->tags[entry].flags |= CACHE_PAGE_PRESENT;
344}
345
346
347static void ps3vram_cache_flush(struct ps3_system_bus_device *dev)
348{
349 struct ps3vram_priv *priv = dev->core.driver_data;
350 struct ps3vram_cache *cache = &priv->cache;
351 int i;
352
353 dev_dbg(&dev->core, "FLUSH\n");
354 for (i = 0; i < cache->page_count; i++) {
355 ps3vram_cache_evict(dev, i);
356 cache->tags[i].flags = 0;
357 }
358}
359
360static unsigned int ps3vram_cache_match(struct ps3_system_bus_device *dev,
361 loff_t address)
362{
363 struct ps3vram_priv *priv = dev->core.driver_data;
364 struct ps3vram_cache *cache = &priv->cache;
365 unsigned int base;
366 unsigned int offset;
367 int i;
368 static int counter;
369
370 offset = (unsigned int) (address & (cache->page_size - 1));
371 base = (unsigned int) (address - offset);
372
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 cache->hit++;
378 dev_dbg(&dev->core, "Found entry %d: 0x%08x\n", i,
379 cache->tags[i].address);
380 return i;
381 }
382 }
383
384 /* choose a random entry */
385 i = (jiffies + (counter++)) % cache->page_count;
386 dev_dbg(&dev->core, "Using entry %d\n", i);
387
388 ps3vram_cache_evict(dev, i);
389 ps3vram_cache_load(dev, i, base);
390
391 cache->miss++;
392 return i;
393}
394
395static int ps3vram_cache_init(struct ps3_system_bus_device *dev)
396{
397 struct ps3vram_priv *priv = dev->core.driver_data;
398
399 priv->cache.page_count = CACHE_PAGE_COUNT;
400 priv->cache.page_size = CACHE_PAGE_SIZE;
401 priv->cache.tags = kzalloc(sizeof(struct ps3vram_tag) *
402 CACHE_PAGE_COUNT, GFP_KERNEL);
403 if (priv->cache.tags == NULL) {
404 dev_err(&dev->core, "Could not allocate cache tags\n");
405 return -ENOMEM;
406 }
407
408 dev_info(&dev->core, "Created ram cache: %d entries, %d KiB each\n",
409 CACHE_PAGE_COUNT, CACHE_PAGE_SIZE / 1024);
410
411 return 0;
412}
413
414static void ps3vram_cache_cleanup(struct ps3_system_bus_device *dev)
415{
416 struct ps3vram_priv *priv = dev->core.driver_data;
417
418 ps3vram_cache_flush(dev);
419 kfree(priv->cache.tags);
420}
421
422static int ps3vram_read(struct ps3_system_bus_device *dev, loff_t from,
423 size_t len, size_t *retlen, u_char *buf)
424{
425 struct ps3vram_priv *priv = dev->core.driver_data;
426 unsigned int cached, count;
427
428 dev_dbg(&dev->core, "%s: from=0x%08x len=0x%zx\n", __func__,
429 (unsigned int)from, len);
430
431 if (from >= priv->size)
432 return -EIO;
433
434 if (len > priv->size - from)
435 len = priv->size - from;
436
437 /* Copy from vram to buf */
438 count = len;
439 while (count) {
440 unsigned int offset, avail;
441 unsigned int entry;
442
443 offset = (unsigned int) (from & (priv->cache.page_size - 1));
444 avail = priv->cache.page_size - offset;
445
446 mutex_lock(&priv->lock);
447
448 entry = ps3vram_cache_match(dev, from);
449 cached = CACHE_OFFSET + entry * priv->cache.page_size + offset;
450
451 dev_dbg(&dev->core, "%s: from=%08x cached=%08x offset=%08x "
452 "avail=%08x count=%08x\n", __func__,
453 (unsigned int)from, cached, offset, avail, count);
454
455 if (avail > count)
456 avail = count;
457 memcpy(buf, priv->xdr_buf + cached, avail);
458
459 mutex_unlock(&priv->lock);
460
461 buf += avail;
462 count -= avail;
463 from += avail;
464 }
465
466 *retlen = len;
467 return 0;
468}
469
470static int ps3vram_write(struct ps3_system_bus_device *dev, loff_t to,
471 size_t len, size_t *retlen, const u_char *buf)
472{
473 struct ps3vram_priv *priv = dev->core.driver_data;
474 unsigned int cached, count;
475
476 if (to >= priv->size)
477 return -EIO;
478
479 if (len > priv->size - to)
480 len = priv->size - to;
481
482 /* Copy from buf to vram */
483 count = len;
484 while (count) {
485 unsigned int offset, avail;
486 unsigned int entry;
487
488 offset = (unsigned int) (to & (priv->cache.page_size - 1));
489 avail = priv->cache.page_size - offset;
490
491 mutex_lock(&priv->lock);
492
493 entry = ps3vram_cache_match(dev, to);
494 cached = CACHE_OFFSET + entry * priv->cache.page_size + offset;
495
496 dev_dbg(&dev->core, "%s: to=%08x cached=%08x offset=%08x "
497 "avail=%08x count=%08x\n", __func__, (unsigned int)to,
498 cached, offset, avail, count);
499
500 if (avail > count)
501 avail = count;
502 memcpy(priv->xdr_buf + cached, buf, avail);
503
504 priv->cache.tags[entry].flags |= CACHE_PAGE_DIRTY;
505
506 mutex_unlock(&priv->lock);
507
508 buf += avail;
509 count -= avail;
510 to += avail;
511 }
512
513 *retlen = len;
514 return 0;
515}
516
517static int ps3vram_proc_show(struct seq_file *m, void *v)
518{
519 struct ps3vram_priv *priv = m->private;
520
521 seq_printf(m, "hit:%u\nmiss:%u\n", priv->cache.hit, priv->cache.miss);
522 return 0;
523}
524
525static int ps3vram_proc_open(struct inode *inode, struct file *file)
526{
527 return single_open(file, ps3vram_proc_show, PDE(inode)->data);
528}
529
530static const struct file_operations ps3vram_proc_fops = {
531 .owner = THIS_MODULE,
532 .open = ps3vram_proc_open,
533 .read = seq_read,
534 .llseek = seq_lseek,
535 .release = single_release,
536};
537
538static void __devinit ps3vram_proc_init(struct ps3_system_bus_device *dev)
539{
540 struct ps3vram_priv *priv = dev->core.driver_data;
541 struct proc_dir_entry *pde;
542
Geert Uytterhoeven3c20e2f22009-06-10 04:38:38 +0000543 pde = proc_create_data(DEVICE_NAME, 0444, NULL, &ps3vram_proc_fops,
544 priv);
545 if (!pde)
Geert Uytterhoevenf507cd22009-03-06 02:54:09 +0000546 dev_warn(&dev->core, "failed to create /proc entry\n");
Geert Uytterhoevenf507cd22009-03-06 02:54:09 +0000547}
548
549static int ps3vram_make_request(struct request_queue *q, struct bio *bio)
550{
551 struct ps3_system_bus_device *dev = q->queuedata;
552 int write = bio_data_dir(bio) == WRITE;
553 const char *op = write ? "write" : "read";
554 loff_t offset = bio->bi_sector << 9;
555 int error = 0;
556 struct bio_vec *bvec;
557 unsigned int i;
558
559 dev_dbg(&dev->core, "%s\n", __func__);
560
561 bio_for_each_segment(bvec, bio, i) {
562 /* PS3 is ppc64, so we don't handle highmem */
563 char *ptr = page_address(bvec->bv_page) + bvec->bv_offset;
564 size_t len = bvec->bv_len, retlen;
565
566 dev_dbg(&dev->core, " %s %zu bytes at offset %llu\n", op,
567 len, offset);
568 if (write)
569 error = ps3vram_write(dev, offset, len, &retlen, ptr);
570 else
571 error = ps3vram_read(dev, offset, len, &retlen, ptr);
572
573 if (error) {
574 dev_err(&dev->core, "%s failed\n", op);
575 goto out;
576 }
577
578 if (retlen != len) {
579 dev_err(&dev->core, "Short %s\n", op);
Geert Uytterhoeven734957c82009-06-10 04:38:37 +0000580 error = -EIO;
Geert Uytterhoevenf507cd22009-03-06 02:54:09 +0000581 goto out;
582 }
583
584 offset += len;
585 }
586
587 dev_dbg(&dev->core, "%s completed\n", op);
588
589out:
590 bio_endio(bio, error);
591 return 0;
592}
593
594static int __devinit ps3vram_probe(struct ps3_system_bus_device *dev)
595{
596 struct ps3vram_priv *priv;
597 int error, status;
598 struct request_queue *queue;
599 struct gendisk *gendisk;
Geert Uytterhoeven56ac72d2009-06-10 04:38:47 +0000600 u64 ddr_size, ddr_lpar, ctrl_lpar, info_lpar, reports_lpar,
601 reports_size, xdr_lpar;
Geert Uytterhoevenf507cd22009-03-06 02:54:09 +0000602 char *rest;
603
604 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
605 if (!priv) {
606 error = -ENOMEM;
607 goto fail;
608 }
609
610 mutex_init(&priv->lock);
611 dev->core.driver_data = priv;
612
613 priv = dev->core.driver_data;
614
615 /* Allocate XDR buffer (1MiB aligned) */
616 priv->xdr_buf = (void *)__get_free_pages(GFP_KERNEL,
617 get_order(XDR_BUF_SIZE));
618 if (priv->xdr_buf == NULL) {
619 dev_err(&dev->core, "Could not allocate XDR buffer\n");
620 error = -ENOMEM;
621 goto fail_free_priv;
622 }
623
624 /* Put FIFO at begginning of XDR buffer */
625 priv->fifo_base = (u32 *) (priv->xdr_buf + FIFO_OFFSET);
626 priv->fifo_ptr = priv->fifo_base;
627
628 /* XXX: Need to open GPU, in case ps3fb or snd_ps3 aren't loaded */
629 if (ps3_open_hv_device(dev)) {
630 dev_err(&dev->core, "ps3_open_hv_device failed\n");
631 error = -EAGAIN;
Jim Paris3273d872009-06-10 04:38:39 +0000632 goto out_free_xdr_buf;
Geert Uytterhoevenf507cd22009-03-06 02:54:09 +0000633 }
634
635 /* Request memory */
636 status = -1;
637 ddr_size = ALIGN(memparse(size, &rest), 1024*1024);
638 if (!ddr_size) {
639 dev_err(&dev->core, "Specified size is too small\n");
640 error = -EINVAL;
641 goto out_close_gpu;
642 }
643
644 while (ddr_size > 0) {
645 status = lv1_gpu_memory_allocate(ddr_size, 0, 0, 0, 0,
646 &priv->memory_handle,
647 &ddr_lpar);
648 if (!status)
649 break;
650 ddr_size -= 1024*1024;
651 }
652 if (status) {
653 dev_err(&dev->core, "lv1_gpu_memory_allocate failed %d\n",
654 status);
655 error = -ENOMEM;
Jim Paris3273d872009-06-10 04:38:39 +0000656 goto out_close_gpu;
Geert Uytterhoevenf507cd22009-03-06 02:54:09 +0000657 }
658
659 /* Request context */
660 status = lv1_gpu_context_allocate(priv->memory_handle, 0,
661 &priv->context_handle, &ctrl_lpar,
662 &info_lpar, &reports_lpar,
663 &reports_size);
664 if (status) {
665 dev_err(&dev->core, "lv1_gpu_context_allocate failed %d\n",
666 status);
667 error = -ENOMEM;
668 goto out_free_memory;
669 }
670
671 /* Map XDR buffer to RSX */
Geert Uytterhoeven56ac72d2009-06-10 04:38:47 +0000672 xdr_lpar = ps3_mm_phys_to_lpar(__pa(priv->xdr_buf));
Geert Uytterhoevenf507cd22009-03-06 02:54:09 +0000673 status = lv1_gpu_context_iomap(priv->context_handle, XDR_IOIF,
Geert Uytterhoeven56ac72d2009-06-10 04:38:47 +0000674 xdr_lpar, XDR_BUF_SIZE,
675 CBE_IOPTE_PP_W | CBE_IOPTE_PP_R |
676 CBE_IOPTE_M);
Geert Uytterhoevenf507cd22009-03-06 02:54:09 +0000677 if (status) {
678 dev_err(&dev->core, "lv1_gpu_context_iomap failed %d\n",
679 status);
680 error = -ENOMEM;
681 goto out_free_context;
682 }
683
684 priv->ddr_base = ioremap_flags(ddr_lpar, ddr_size, _PAGE_NO_CACHE);
685
686 if (!priv->ddr_base) {
687 dev_err(&dev->core, "ioremap DDR failed\n");
688 error = -ENOMEM;
Geert Uytterhoeven56ac72d2009-06-10 04:38:47 +0000689 goto out_unmap_context;
Geert Uytterhoevenf507cd22009-03-06 02:54:09 +0000690 }
691
692 priv->ctrl = ioremap(ctrl_lpar, 64 * 1024);
693 if (!priv->ctrl) {
694 dev_err(&dev->core, "ioremap CTRL failed\n");
695 error = -ENOMEM;
696 goto out_unmap_vram;
697 }
698
699 priv->reports = ioremap(reports_lpar, reports_size);
700 if (!priv->reports) {
701 dev_err(&dev->core, "ioremap REPORTS failed\n");
702 error = -ENOMEM;
703 goto out_unmap_ctrl;
704 }
705
706 mutex_lock(&ps3_gpu_mutex);
707 ps3vram_init_ring(dev);
708 mutex_unlock(&ps3_gpu_mutex);
709
710 priv->size = ddr_size;
711
712 ps3vram_bind(dev);
713
714 mutex_lock(&ps3_gpu_mutex);
715 error = ps3vram_wait_ring(dev, 100);
716 mutex_unlock(&ps3_gpu_mutex);
717 if (error < 0) {
718 dev_err(&dev->core, "Failed to initialize channels\n");
719 error = -ETIMEDOUT;
720 goto out_unmap_reports;
721 }
722
723 ps3vram_cache_init(dev);
724 ps3vram_proc_init(dev);
725
726 queue = blk_alloc_queue(GFP_KERNEL);
727 if (!queue) {
728 dev_err(&dev->core, "blk_alloc_queue failed\n");
729 error = -ENOMEM;
730 goto out_cache_cleanup;
731 }
732
733 priv->queue = queue;
734 queue->queuedata = dev;
735 blk_queue_make_request(queue, ps3vram_make_request);
736 blk_queue_max_phys_segments(queue, MAX_PHYS_SEGMENTS);
737 blk_queue_max_hw_segments(queue, MAX_HW_SEGMENTS);
738 blk_queue_max_segment_size(queue, MAX_SEGMENT_SIZE);
739 blk_queue_max_sectors(queue, SAFE_MAX_SECTORS);
740
741 gendisk = alloc_disk(1);
742 if (!gendisk) {
743 dev_err(&dev->core, "alloc_disk failed\n");
744 error = -ENOMEM;
745 goto fail_cleanup_queue;
746 }
747
748 priv->gendisk = gendisk;
749 gendisk->major = ps3vram_major;
750 gendisk->first_minor = 0;
751 gendisk->fops = &ps3vram_fops;
752 gendisk->queue = queue;
753 gendisk->private_data = dev;
754 gendisk->driverfs_dev = &dev->core;
755 strlcpy(gendisk->disk_name, DEVICE_NAME, sizeof(gendisk->disk_name));
756 set_capacity(gendisk, priv->size >> 9);
757
758 dev_info(&dev->core, "%s: Using %lu MiB of GPU memory\n",
759 gendisk->disk_name, get_capacity(gendisk) >> 11);
760
761 add_disk(gendisk);
762 return 0;
763
764fail_cleanup_queue:
765 blk_cleanup_queue(queue);
766out_cache_cleanup:
767 remove_proc_entry(DEVICE_NAME, NULL);
768 ps3vram_cache_cleanup(dev);
769out_unmap_reports:
770 iounmap(priv->reports);
771out_unmap_ctrl:
772 iounmap(priv->ctrl);
773out_unmap_vram:
774 iounmap(priv->ddr_base);
Geert Uytterhoeven56ac72d2009-06-10 04:38:47 +0000775out_unmap_context:
776 lv1_gpu_context_iomap(priv->context_handle, XDR_IOIF, xdr_lpar,
777 XDR_BUF_SIZE, CBE_IOPTE_M);
Geert Uytterhoevenf507cd22009-03-06 02:54:09 +0000778out_free_context:
779 lv1_gpu_context_free(priv->context_handle);
780out_free_memory:
781 lv1_gpu_memory_free(priv->memory_handle);
782out_close_gpu:
783 ps3_close_hv_device(dev);
784out_free_xdr_buf:
785 free_pages((unsigned long) priv->xdr_buf, get_order(XDR_BUF_SIZE));
786fail_free_priv:
787 kfree(priv);
788 dev->core.driver_data = NULL;
789fail:
790 return error;
791}
792
793static int ps3vram_remove(struct ps3_system_bus_device *dev)
794{
795 struct ps3vram_priv *priv = dev->core.driver_data;
796
797 del_gendisk(priv->gendisk);
798 put_disk(priv->gendisk);
799 blk_cleanup_queue(priv->queue);
800 remove_proc_entry(DEVICE_NAME, NULL);
801 ps3vram_cache_cleanup(dev);
802 iounmap(priv->reports);
803 iounmap(priv->ctrl);
804 iounmap(priv->ddr_base);
Geert Uytterhoeven56ac72d2009-06-10 04:38:47 +0000805 lv1_gpu_context_iomap(priv->context_handle, XDR_IOIF,
806 ps3_mm_phys_to_lpar(__pa(priv->xdr_buf)),
807 XDR_BUF_SIZE, CBE_IOPTE_M);
Geert Uytterhoevenf507cd22009-03-06 02:54:09 +0000808 lv1_gpu_context_free(priv->context_handle);
809 lv1_gpu_memory_free(priv->memory_handle);
810 ps3_close_hv_device(dev);
811 free_pages((unsigned long) priv->xdr_buf, get_order(XDR_BUF_SIZE));
812 kfree(priv);
813 dev->core.driver_data = NULL;
814 return 0;
815}
816
817static struct ps3_system_bus_driver ps3vram = {
818 .match_id = PS3_MATCH_ID_GPU,
819 .match_sub_id = PS3_MATCH_SUB_ID_GPU_RAMDISK,
820 .core.name = DEVICE_NAME,
821 .core.owner = THIS_MODULE,
822 .probe = ps3vram_probe,
823 .remove = ps3vram_remove,
824 .shutdown = ps3vram_remove,
825};
826
827
828static int __init ps3vram_init(void)
829{
830 int error;
831
832 if (!firmware_has_feature(FW_FEATURE_PS3_LV1))
833 return -ENODEV;
834
835 error = register_blkdev(0, DEVICE_NAME);
836 if (error <= 0) {
837 pr_err("%s: register_blkdev failed %d\n", DEVICE_NAME, error);
838 return error;
839 }
840 ps3vram_major = error;
841
842 pr_info("%s: registered block device major %d\n", DEVICE_NAME,
843 ps3vram_major);
844
845 error = ps3_system_bus_driver_register(&ps3vram);
846 if (error)
847 unregister_blkdev(ps3vram_major, DEVICE_NAME);
848
849 return error;
850}
851
852static void __exit ps3vram_exit(void)
853{
854 ps3_system_bus_driver_unregister(&ps3vram);
855 unregister_blkdev(ps3vram_major, DEVICE_NAME);
856}
857
858module_init(ps3vram_init);
859module_exit(ps3vram_exit);
860
861MODULE_LICENSE("GPL");
862MODULE_DESCRIPTION("PS3 Video RAM Storage Driver");
863MODULE_AUTHOR("Sony Corporation");
864MODULE_ALIAS(PS3_MODULE_ALIAS_GPU_RAMDISK);