8c29d8040775fed26edc34cb8b2e9e80ec3e2f2d
[openwrt.git] / target / linux / ps3 / patches-2.6.28 / 0010-ps3vram-ng.patch
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
5
6 Convert the PS3 Video RAM Storage Driver from a MTD driver to a plain block
7 device driver, as requested by Arnd Bergmann.
8
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.
12
13 The new driver is ca. 50% faster for reading, and ca. 10% for writing.
14
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>
22
23 Notes:
24
25 The MTD-based PS3 Video RAM Storage Driver was integrated into the mainline
26 kernel in 2.6.29-rc1.
27
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.
34
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
38 concurrently
39 TO DO (after 2.6.29): use a spinlock and a list to queue requests while the
40 driver is busy
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
50 ---
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
56
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.
64
65 +config PS3_VRAM
66 + tristate "PS3 Video RAM Storage Driver"
67 + depends on FB_PS3=y && BLOCK && m
68 + help
69 + This driver allows you to use excess PS3 video RAM as volatile
70 + storage or system swap.
71 +
72 config PS3_LPM
73 tristate "PS3 Logical Performance Monitor support"
74 depends on PPC_PS3
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
88 new file mode 100644
89 index 0000000..393ed67
90 --- /dev/null
91 +++ b/drivers/block/ps3vram.c
92 @@ -0,0 +1,865 @@
93 +/*
94 + * ps3vram - Use extra PS3 video ram as MTD block device.
95 + *
96 + * Copyright 2009 Sony Corporation
97 + *
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>
101 + */
102 +
103 +#include <linux/blkdev.h>
104 +#include <linux/delay.h>
105 +#include <linux/proc_fs.h>
106 +#include <linux/seq_file.h>
107 +
108 +#include <asm/firmware.h>
109 +#include <asm/lv1call.h>
110 +#include <asm/ps3.h>
111 +
112 +
113 +#define DEVICE_NAME "ps3vram"
114 +
115 +
116 +#define XDR_BUF_SIZE (2 * 1024 * 1024) /* XDR buffer (must be 1MiB aligned) */
117 +#define XDR_IOIF 0x0c000000
118 +
119 +#define FIFO_BASE XDR_IOIF
120 +#define FIFO_SIZE (64 * 1024)
121 +
122 +#define DMA_PAGE_SIZE (4 * 1024)
123 +
124 +#define CACHE_PAGE_SIZE (256 * 1024)
125 +#define CACHE_PAGE_COUNT ((XDR_BUF_SIZE - FIFO_SIZE) / CACHE_PAGE_SIZE)
126 +
127 +#define CACHE_OFFSET CACHE_PAGE_SIZE
128 +#define FIFO_OFFSET 0
129 +
130 +#define CTRL_PUT 0x10
131 +#define CTRL_GET 0x11
132 +#define CTRL_TOP 0x15
133 +
134 +#define UPLOAD_SUBCH 1
135 +#define DOWNLOAD_SUBCH 2
136 +
137 +#define NV_MEMORY_TO_MEMORY_FORMAT_OFFSET_IN 0x0000030c
138 +#define NV_MEMORY_TO_MEMORY_FORMAT_NOTIFY 0x00000104
139 +
140 +#define L1GPU_CONTEXT_ATTRIBUTE_FB_BLIT 0x601
141 +
142 +#define CACHE_PAGE_PRESENT 1
143 +#define CACHE_PAGE_DIRTY 2
144 +
145 +struct ps3vram_tag {
146 + unsigned int address;
147 + unsigned int flags;
148 +};
149 +
150 +struct ps3vram_cache {
151 + unsigned int page_count;
152 + unsigned int page_size;
153 + struct ps3vram_tag *tags;
154 + unsigned int hit;
155 + unsigned int miss;
156 +};
157 +
158 +struct ps3vram_priv {
159 + struct request_queue *queue;
160 + struct gendisk *gendisk;
161 +
162 + u64 size;
163 +
164 + u64 memory_handle;
165 + u64 context_handle;
166 + u32 *ctrl;
167 + u32 *reports;
168 + u8 __iomem *ddr_base;
169 + u8 *xdr_buf;
170 +
171 + u32 *fifo_base;
172 + u32 *fifo_ptr;
173 +
174 + struct ps3vram_cache cache;
175 +
176 + /* Used to serialize cache/DMA operations */
177 + struct mutex lock;
178 +};
179 +
180 +
181 +static int ps3vram_major;
182 +
183 +
184 +static struct block_device_operations ps3vram_fops = {
185 + .owner = THIS_MODULE,
186 +};
187 +
188 +
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 */
193 +
194 +static char *size = "256M";
195 +module_param(size, charp, 0);
196 +MODULE_PARM_DESC(size, "memory size");
197 +
198 +static u32 *ps3vram_get_notifier(u32 *reports, int notifier)
199 +{
200 + return (void *)reports + DMA_NOTIFIER_OFFSET_BASE +
201 + DMA_NOTIFIER_SIZE * notifier;
202 +}
203 +
204 +static void ps3vram_notifier_reset(struct ps3_system_bus_device *dev)
205 +{
206 + struct ps3vram_priv *priv = dev->core.driver_data;
207 + u32 *notify = ps3vram_get_notifier(priv->reports, NOTIFIER);
208 + int i;
209 +
210 + for (i = 0; i < 4; i++)
211 + notify[i] = 0xffffffff;
212 +}
213 +
214 +static int ps3vram_notifier_wait(struct ps3_system_bus_device *dev,
215 + unsigned int timeout_ms)
216 +{
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);
220 +
221 + do {
222 + if (!notify[3])
223 + return 0;
224 + msleep(1);
225 + } while (time_before(jiffies, timeout));
226 +
227 + return -ETIMEDOUT;
228 +}
229 +
230 +static void ps3vram_init_ring(struct ps3_system_bus_device *dev)
231 +{
232 + struct ps3vram_priv *priv = dev->core.driver_data;
233 +
234 + priv->ctrl[CTRL_PUT] = FIFO_BASE + FIFO_OFFSET;
235 + priv->ctrl[CTRL_GET] = FIFO_BASE + FIFO_OFFSET;
236 +}
237 +
238 +static int ps3vram_wait_ring(struct ps3_system_bus_device *dev,
239 + unsigned int timeout_ms)
240 +{
241 + struct ps3vram_priv *priv = dev->core.driver_data;
242 + unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
243 +
244 + do {
245 + if (priv->ctrl[CTRL_PUT] == priv->ctrl[CTRL_GET])
246 + return 0;
247 + msleep(1);
248 + } while (time_before(jiffies, timeout));
249 +
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]);
253 +
254 + return -ETIMEDOUT;
255 +}
256 +
257 +static void ps3vram_out_ring(struct ps3vram_priv *priv, u32 data)
258 +{
259 + *(priv->fifo_ptr)++ = data;
260 +}
261 +
262 +static void ps3vram_begin_ring(struct ps3vram_priv *priv, u32 chan, u32 tag,
263 + u32 size)
264 +{
265 + ps3vram_out_ring(priv, (size << 18) | (chan << 13) | tag);
266 +}
267 +
268 +static void ps3vram_rewind_ring(struct ps3_system_bus_device *dev)
269 +{
270 + struct ps3vram_priv *priv = dev->core.driver_data;
271 + int status;
272 +
273 + ps3vram_out_ring(priv, 0x20000000 | (FIFO_BASE + FIFO_OFFSET));
274 +
275 + priv->ctrl[CTRL_PUT] = FIFO_BASE + FIFO_OFFSET;
276 +
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,
280 + 0, 0, 0);
281 + if (status)
282 + dev_err(&dev->core,
283 + "%s: lv1_gpu_context_attribute failed %d\n", __func__,
284 + status);
285 +
286 + priv->fifo_ptr = priv->fifo_base;
287 +}
288 +
289 +static void ps3vram_fire_ring(struct ps3_system_bus_device *dev)
290 +{
291 + struct ps3vram_priv *priv = dev->core.driver_data;
292 + int status;
293 +
294 + mutex_lock(&ps3_gpu_mutex);
295 +
296 + priv->ctrl[CTRL_PUT] = FIFO_BASE + FIFO_OFFSET +
297 + (priv->fifo_ptr - priv->fifo_base) * sizeof(u32);
298 +
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,
302 + 0, 0, 0);
303 + if (status)
304 + dev_err(&dev->core,
305 + "%s: lv1_gpu_context_attribute failed %d\n", __func__,
306 + status);
307 +
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);
313 + }
314 +
315 + mutex_unlock(&ps3_gpu_mutex);
316 +}
317 +
318 +static void ps3vram_bind(struct ps3_system_bus_device *dev)
319 +{
320 + struct ps3vram_priv *priv = dev->core.driver_data;
321 +
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 */
328 +
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 */
335 +
336 + ps3vram_fire_ring(dev);
337 +}
338 +
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)
342 +{
343 + struct ps3vram_priv *priv = dev->core.driver_data;
344 +
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);
355 +
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__);
365 + return -1;
366 + }
367 +
368 + return 0;
369 +}
370 +
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)
374 +{
375 + struct ps3vram_priv *priv = dev->core.driver_data;
376 +
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);
387 +
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__);
397 + return -1;
398 + }
399 +
400 + return 0;
401 +}
402 +
403 +static void ps3vram_cache_evict(struct ps3_system_bus_device *dev, int entry)
404 +{
405 + struct ps3vram_priv *priv = dev->core.driver_data;
406 + struct ps3vram_cache *cache = &priv->cache;
407 +
408 + if (!(cache->tags[entry].flags & CACHE_PAGE_DIRTY))
409 + return;
410 +
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,
419 + cache->page_size);
420 + }
421 + cache->tags[entry].flags &= ~CACHE_PAGE_DIRTY;
422 +}
423 +
424 +static void ps3vram_cache_load(struct ps3_system_bus_device *dev, int entry,
425 + unsigned int address)
426 +{
427 + struct ps3vram_priv *priv = dev->core.driver_data;
428 + struct ps3vram_cache *cache = &priv->cache;
429 +
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,
433 + DMA_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);
438 + }
439 +
440 + cache->tags[entry].address = address;
441 + cache->tags[entry].flags |= CACHE_PAGE_PRESENT;
442 +}
443 +
444 +
445 +static void ps3vram_cache_flush(struct ps3_system_bus_device *dev)
446 +{
447 + struct ps3vram_priv *priv = dev->core.driver_data;
448 + struct ps3vram_cache *cache = &priv->cache;
449 + int i;
450 +
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;
455 + }
456 +}
457 +
458 +static unsigned int ps3vram_cache_match(struct ps3_system_bus_device *dev,
459 + loff_t address)
460 +{
461 + struct ps3vram_priv *priv = dev->core.driver_data;
462 + struct ps3vram_cache *cache = &priv->cache;
463 + unsigned int base;
464 + unsigned int offset;
465 + int i;
466 + static int counter;
467 +
468 + offset = (unsigned int) (address & (cache->page_size - 1));
469 + base = (unsigned int) (address - offset);
470 +
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) {
475 + cache->hit++;
476 + dev_dbg(&dev->core, "Found entry %d: 0x%08x\n", i,
477 + cache->tags[i].address);
478 + return i;
479 + }
480 + }
481 +
482 + /* choose a random entry */
483 + i = (jiffies + (counter++)) % cache->page_count;
484 + dev_dbg(&dev->core, "Using entry %d\n", i);
485 +
486 + ps3vram_cache_evict(dev, i);
487 + ps3vram_cache_load(dev, i, base);
488 +
489 + cache->miss++;
490 + return i;
491 +}
492 +
493 +static int ps3vram_cache_init(struct ps3_system_bus_device *dev)
494 +{
495 + struct ps3vram_priv *priv = dev->core.driver_data;
496 +
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");
503 + return -ENOMEM;
504 + }
505 +
506 + dev_info(&dev->core, "Created ram cache: %d entries, %d KiB each\n",
507 + CACHE_PAGE_COUNT, CACHE_PAGE_SIZE / 1024);
508 +
509 + return 0;
510 +}
511 +
512 +static void ps3vram_cache_cleanup(struct ps3_system_bus_device *dev)
513 +{
514 + struct ps3vram_priv *priv = dev->core.driver_data;
515 +
516 + ps3vram_cache_flush(dev);
517 + kfree(priv->cache.tags);
518 +}
519 +
520 +static int ps3vram_read(struct ps3_system_bus_device *dev, loff_t from,
521 + size_t len, size_t *retlen, u_char *buf)
522 +{
523 + struct ps3vram_priv *priv = dev->core.driver_data;
524 + unsigned int cached, count;
525 +
526 + dev_dbg(&dev->core, "%s: from=0x%08x len=0x%zx\n", __func__,
527 + (unsigned int)from, len);
528 +
529 + if (from >= priv->size)
530 + return -EIO;
531 +
532 + if (len > priv->size - from)
533 + len = priv->size - from;
534 +
535 + /* Copy from vram to buf */
536 + count = len;
537 + while (count) {
538 + unsigned int offset, avail;
539 + unsigned int entry;
540 +
541 + offset = (unsigned int) (from & (priv->cache.page_size - 1));
542 + avail = priv->cache.page_size - offset;
543 +
544 + mutex_lock(&priv->lock);
545 +
546 + entry = ps3vram_cache_match(dev, from);
547 + cached = CACHE_OFFSET + entry * priv->cache.page_size + offset;
548 +
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);
552 +
553 + if (avail > count)
554 + avail = count;
555 + memcpy(buf, priv->xdr_buf + cached, avail);
556 +
557 + mutex_unlock(&priv->lock);
558 +
559 + buf += avail;
560 + count -= avail;
561 + from += avail;
562 + }
563 +
564 + *retlen = len;
565 + return 0;
566 +}
567 +
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)
570 +{
571 + struct ps3vram_priv *priv = dev->core.driver_data;
572 + unsigned int cached, count;
573 +
574 + if (to >= priv->size)
575 + return -EIO;
576 +
577 + if (len > priv->size - to)
578 + len = priv->size - to;
579 +
580 + /* Copy from buf to vram */
581 + count = len;
582 + while (count) {
583 + unsigned int offset, avail;
584 + unsigned int entry;
585 +
586 + offset = (unsigned int) (to & (priv->cache.page_size - 1));
587 + avail = priv->cache.page_size - offset;
588 +
589 + mutex_lock(&priv->lock);
590 +
591 + entry = ps3vram_cache_match(dev, to);
592 + cached = CACHE_OFFSET + entry * priv->cache.page_size + offset;
593 +
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);
597 +
598 + if (avail > count)
599 + avail = count;
600 + memcpy(priv->xdr_buf + cached, buf, avail);
601 +
602 + priv->cache.tags[entry].flags |= CACHE_PAGE_DIRTY;
603 +
604 + mutex_unlock(&priv->lock);
605 +
606 + buf += avail;
607 + count -= avail;
608 + to += avail;
609 + }
610 +
611 + *retlen = len;
612 + return 0;
613 +}
614 +
615 +static int ps3vram_proc_show(struct seq_file *m, void *v)
616 +{
617 + struct ps3vram_priv *priv = m->private;
618 +
619 + seq_printf(m, "hit:%u\nmiss:%u\n", priv->cache.hit, priv->cache.miss);
620 + return 0;
621 +}
622 +
623 +static int ps3vram_proc_open(struct inode *inode, struct file *file)
624 +{
625 + return single_open(file, ps3vram_proc_show, PDE(inode)->data);
626 +}
627 +
628 +static const struct file_operations ps3vram_proc_fops = {
629 + .owner = THIS_MODULE,
630 + .open = ps3vram_proc_open,
631 + .read = seq_read,
632 + .llseek = seq_lseek,
633 + .release = single_release,
634 +};
635 +
636 +static void __devinit ps3vram_proc_init(struct ps3_system_bus_device *dev)
637 +{
638 + struct ps3vram_priv *priv = dev->core.driver_data;
639 + struct proc_dir_entry *pde;
640 +
641 + pde = proc_create(DEVICE_NAME, 0444, NULL, &ps3vram_proc_fops);
642 + if (!pde) {
643 + dev_warn(&dev->core, "failed to create /proc entry\n");
644 + return;
645 + }
646 +
647 + pde->owner = THIS_MODULE;
648 + pde->data = priv;
649 +}
650 +
651 +static int ps3vram_make_request(struct request_queue *q, struct bio *bio)
652 +{
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;
657 + int error = 0;
658 + struct bio_vec *bvec;
659 + unsigned int i;
660 +
661 + dev_dbg(&dev->core, "%s\n", __func__);
662 +
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;
667 +
668 + dev_dbg(&dev->core, " %s %zu bytes at offset %llu\n", op,
669 + len, offset);
670 + if (write)
671 + error = ps3vram_write(dev, offset, len, &retlen, ptr);
672 + else
673 + error = ps3vram_read(dev, offset, len, &retlen, ptr);
674 +
675 + if (error) {
676 + dev_err(&dev->core, "%s failed\n", op);
677 + goto out;
678 + }
679 +
680 + if (retlen != len) {
681 + dev_err(&dev->core, "Short %s\n", op);
682 + goto out;
683 + }
684 +
685 + offset += len;
686 + }
687 +
688 + dev_dbg(&dev->core, "%s completed\n", op);
689 +
690 +out:
691 + bio_endio(bio, error);
692 + return 0;
693 +}
694 +
695 +static int __devinit ps3vram_probe(struct ps3_system_bus_device *dev)
696 +{
697 + struct ps3vram_priv *priv;
698 + int error, status;
699 + struct request_queue *queue;
700 + struct gendisk *gendisk;
701 + u64 ddr_lpar, ctrl_lpar, info_lpar, reports_lpar, ddr_size,
702 + reports_size;
703 + char *rest;
704 +
705 + priv = kzalloc(sizeof(*priv), GFP_KERNEL);
706 + if (!priv) {
707 + error = -ENOMEM;
708 + goto fail;
709 + }
710 +
711 + mutex_init(&priv->lock);
712 + dev->core.driver_data = priv;
713 +
714 + priv = dev->core.driver_data;
715 +
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");
721 + error = -ENOMEM;
722 + goto fail_free_priv;
723 + }
724 +
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;
728 +
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");
732 + error = -EAGAIN;
733 + goto out_close_gpu;
734 + }
735 +
736 + /* Request memory */
737 + status = -1;
738 + ddr_size = ALIGN(memparse(size, &rest), 1024*1024);
739 + if (!ddr_size) {
740 + dev_err(&dev->core, "Specified size is too small\n");
741 + error = -EINVAL;
742 + goto out_close_gpu;
743 + }
744 +
745 + while (ddr_size > 0) {
746 + status = lv1_gpu_memory_allocate(ddr_size, 0, 0, 0, 0,
747 + &priv->memory_handle,
748 + &ddr_lpar);
749 + if (!status)
750 + break;
751 + ddr_size -= 1024*1024;
752 + }
753 + if (status) {
754 + dev_err(&dev->core, "lv1_gpu_memory_allocate failed %d\n",
755 + status);
756 + error = -ENOMEM;
757 + goto out_free_xdr_buf;
758 + }
759 +
760 + /* Request context */
761 + status = lv1_gpu_context_allocate(priv->memory_handle, 0,
762 + &priv->context_handle, &ctrl_lpar,
763 + &info_lpar, &reports_lpar,
764 + &reports_size);
765 + if (status) {
766 + dev_err(&dev->core, "lv1_gpu_context_allocate failed %d\n",
767 + status);
768 + error = -ENOMEM;
769 + goto out_free_memory;
770 + }
771 +
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)),
775 + XDR_BUF_SIZE, 0);
776 + if (status) {
777 + dev_err(&dev->core, "lv1_gpu_context_iomap failed %d\n",
778 + status);
779 + error = -ENOMEM;
780 + goto out_free_context;
781 + }
782 +
783 + priv->ddr_base = ioremap_flags(ddr_lpar, ddr_size, _PAGE_NO_CACHE);
784 +
785 + if (!priv->ddr_base) {
786 + dev_err(&dev->core, "ioremap DDR failed\n");
787 + error = -ENOMEM;
788 + goto out_free_context;
789 + }
790 +
791 + priv->ctrl = ioremap(ctrl_lpar, 64 * 1024);
792 + if (!priv->ctrl) {
793 + dev_err(&dev->core, "ioremap CTRL failed\n");
794 + error = -ENOMEM;
795 + goto out_unmap_vram;
796 + }
797 +
798 + priv->reports = ioremap(reports_lpar, reports_size);
799 + if (!priv->reports) {
800 + dev_err(&dev->core, "ioremap REPORTS failed\n");
801 + error = -ENOMEM;
802 + goto out_unmap_ctrl;
803 + }
804 +
805 + mutex_lock(&ps3_gpu_mutex);
806 + ps3vram_init_ring(dev);
807 + mutex_unlock(&ps3_gpu_mutex);
808 +
809 + priv->size = ddr_size;
810 +
811 + ps3vram_bind(dev);
812 +
813 + mutex_lock(&ps3_gpu_mutex);
814 + error = ps3vram_wait_ring(dev, 100);
815 + mutex_unlock(&ps3_gpu_mutex);
816 + if (error < 0) {
817 + dev_err(&dev->core, "Failed to initialize channels\n");
818 + error = -ETIMEDOUT;
819 + goto out_unmap_reports;
820 + }
821 +
822 + ps3vram_cache_init(dev);
823 + ps3vram_proc_init(dev);
824 +
825 + queue = blk_alloc_queue(GFP_KERNEL);
826 + if (!queue) {
827 + dev_err(&dev->core, "blk_alloc_queue failed\n");
828 + error = -ENOMEM;
829 + goto out_cache_cleanup;
830 + }
831 +
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);
839 +
840 + gendisk = alloc_disk(1);
841 + if (!gendisk) {
842 + dev_err(&dev->core, "alloc_disk failed\n");
843 + error = -ENOMEM;
844 + goto fail_cleanup_queue;
845 + }
846 +
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);
856 +
857 + dev_info(&dev->core, "%s: Using %lu MiB of GPU memory\n",
858 + gendisk->disk_name, get_capacity(gendisk) >> 11);
859 +
860 + add_disk(gendisk);
861 + return 0;
862 +
863 +fail_cleanup_queue:
864 + blk_cleanup_queue(queue);
865 +out_cache_cleanup:
866 + remove_proc_entry(DEVICE_NAME, NULL);
867 + ps3vram_cache_cleanup(dev);
868 +out_unmap_reports:
869 + iounmap(priv->reports);
870 +out_unmap_ctrl:
871 + iounmap(priv->ctrl);
872 +out_unmap_vram:
873 + iounmap(priv->ddr_base);
874 +out_free_context:
875 + lv1_gpu_context_free(priv->context_handle);
876 +out_free_memory:
877 + lv1_gpu_memory_free(priv->memory_handle);
878 +out_close_gpu:
879 + ps3_close_hv_device(dev);
880 +out_free_xdr_buf:
881 + free_pages((unsigned long) priv->xdr_buf, get_order(XDR_BUF_SIZE));
882 +fail_free_priv:
883 + kfree(priv);
884 + dev->core.driver_data = NULL;
885 +fail:
886 + return error;
887 +}
888 +
889 +static int ps3vram_remove(struct ps3_system_bus_device *dev)
890 +{
891 + struct ps3vram_priv *priv = dev->core.driver_data;
892 +
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));
905 + kfree(priv);
906 + dev->core.driver_data = NULL;
907 + return 0;
908 +}
909 +
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,
918 +};
919 +
920 +
921 +static int __init ps3vram_init(void)
922 +{
923 + int error;
924 +
925 + if (!firmware_has_feature(FW_FEATURE_PS3_LV1))
926 + return -ENODEV;
927 +
928 + error = register_blkdev(0, DEVICE_NAME);
929 + if (error <= 0) {
930 + pr_err("%s: register_blkdev failed %d\n", DEVICE_NAME, error);
931 + return error;
932 + }
933 + ps3vram_major = error;
934 +
935 + pr_info("%s: registered block device major %d\n", DEVICE_NAME,
936 + ps3vram_major);
937 +
938 + error = ps3_system_bus_driver_register(&ps3vram);
939 + if (error)
940 + unregister_blkdev(ps3vram_major, DEVICE_NAME);
941 +
942 + return error;
943 +}
944 +
945 +static void __exit ps3vram_exit(void)
946 +{
947 + ps3_system_bus_driver_unregister(&ps3vram);
948 + unregister_blkdev(ps3vram_major, DEVICE_NAME);
949 +}
950 +
951 +module_init(ps3vram_init);
952 +module_exit(ps3vram_exit);
953 +
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);
This page took 0.0988 seconds and 3 git commands to generate.