ps3vram.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
  1. /**
  2. * ps3vram - Use extra PS3 video ram as MTD block device.
  3. *
  4. * Copyright (c) 2007-2008 Jim Paris <jim@jtan.com>
  5. * Added support RSX DMA Vivien Chappelier <vivien.chappelier@free.fr>
  6. */
  7. #include <linux/io.h>
  8. #include <linux/init.h>
  9. #include <linux/kernel.h>
  10. #include <linux/list.h>
  11. #include <linux/module.h>
  12. #include <linux/moduleparam.h>
  13. #include <linux/slab.h>
  14. #include <linux/version.h>
  15. #include <linux/gfp.h>
  16. #include <linux/delay.h>
  17. #include <linux/mtd/mtd.h>
  18. #include <asm/lv1call.h>
  19. #include <asm/ps3.h>
  20. #define DEVICE_NAME "ps3vram"
  21. #define XDR_BUF_SIZE (2 * 1024 * 1024) /* XDR buffer (must be 1MiB aligned) */
  22. #define XDR_IOIF 0x0c000000
  23. #define FIFO_BASE XDR_IOIF
  24. #define FIFO_SIZE (64 * 1024)
  25. #define DMA_PAGE_SIZE (4 * 1024)
  26. #define CACHE_PAGE_SIZE (256 * 1024)
  27. #define CACHE_PAGE_COUNT ((XDR_BUF_SIZE - FIFO_SIZE) / CACHE_PAGE_SIZE)
  28. #define CACHE_OFFSET CACHE_PAGE_SIZE
  29. #define FIFO_OFFSET 0
  30. #define CTRL_PUT 0x10
  31. #define CTRL_GET 0x11
  32. #define CTRL_TOP 0x15
  33. #define UPLOAD_SUBCH 1
  34. #define DOWNLOAD_SUBCH 2
  35. #define NV_MEMORY_TO_MEMORY_FORMAT_OFFSET_IN 0x0000030c
  36. #define NV_MEMORY_TO_MEMORY_FORMAT_NOTIFY 0x00000104
  37. #define L1GPU_CONTEXT_ATTRIBUTE_FB_BLIT 0x601
  38. struct mtd_info ps3vram_mtd;
  39. #define CACHE_PAGE_PRESENT 1
  40. #define CACHE_PAGE_DIRTY 2
  41. #define dbg(fmt, args...) \
  42. pr_debug("%s:%d " fmt "\n", __func__, __LINE__, ## args)
  43. struct ps3vram_tag {
  44. unsigned int address;
  45. unsigned int flags;
  46. };
  47. struct ps3vram_cache {
  48. unsigned int page_count;
  49. unsigned int page_size;
  50. struct ps3vram_tag *tags;
  51. };
  52. struct ps3vram_priv {
  53. uint64_t memory_handle;
  54. uint64_t context_handle;
  55. uint8_t *base;
  56. uint32_t *ctrl;
  57. uint32_t *reports;
  58. uint8_t *xdr_buf;
  59. uint32_t *fifo_base;
  60. uint32_t *fifo_ptr;
  61. struct ps3vram_cache cache;
  62. /* Used to serialize cache/DMA operations */
  63. struct mutex lock;
  64. };
  65. #define DMA_NOTIFIER_HANDLE_BASE 0x66604200 /* first DMA notifier handle */
  66. #define DMA_NOTIFIER_OFFSET_BASE 0x1000 /* first DMA notifier offset */
  67. #define DMA_NOTIFIER_SIZE 0x40
  68. #define NUM_NOTIFIERS 16
  69. #define NOTIFIER 7 /* notifier used for completion report */
  70. /* A trailing '-' means to subtract off ps3fb_videomemory.size */
  71. char *size = "256M-";
  72. module_param(size, charp, 0);
  73. MODULE_PARM_DESC(size, "memory size");
  74. static inline uint32_t *ps3vram_get_notifier(uint32_t *reports, int notifier)
  75. {
  76. return (void *) reports +
  77. DMA_NOTIFIER_OFFSET_BASE +
  78. DMA_NOTIFIER_SIZE * notifier;
  79. }
  80. static void ps3vram_notifier_reset(struct mtd_info *mtd)
  81. {
  82. int i;
  83. struct ps3vram_priv *priv = mtd->priv;
  84. uint32_t *notify = ps3vram_get_notifier(priv->reports, NOTIFIER);
  85. for (i = 0; i < 4; i++)
  86. notify[i] = 0xffffffff;
  87. }
  88. static int ps3vram_notifier_wait(struct mtd_info *mtd, int timeout_ms)
  89. {
  90. struct ps3vram_priv *priv = mtd->priv;
  91. uint32_t *notify = ps3vram_get_notifier(priv->reports, NOTIFIER);
  92. timeout_ms *= 1000;
  93. do {
  94. if (notify[3] == 0)
  95. return 0;
  96. if (timeout_ms)
  97. udelay(1);
  98. } while (timeout_ms--);
  99. return -1;
  100. }
  101. static void ps3vram_dump_ring(struct mtd_info *mtd)
  102. {
  103. struct ps3vram_priv *priv = mtd->priv;
  104. uint32_t *fifo;
  105. pr_info("PUT = %08x GET = %08x\n", priv->ctrl[CTRL_PUT],
  106. priv->ctrl[CTRL_GET]);
  107. for (fifo = priv->fifo_base; fifo < priv->fifo_ptr; fifo++)
  108. pr_info("%p: %08x\n", fifo, *fifo);
  109. }
  110. static void ps3vram_dump_reports(struct mtd_info *mtd)
  111. {
  112. struct ps3vram_priv *priv = mtd->priv;
  113. int i;
  114. for (i = 0; i < NUM_NOTIFIERS; i++) {
  115. uint32_t *n = ps3vram_get_notifier(priv->reports, i);
  116. pr_info("%p: %08x\n", n, *n);
  117. }
  118. }
  119. static void ps3vram_init_ring(struct mtd_info *mtd)
  120. {
  121. struct ps3vram_priv *priv = mtd->priv;
  122. priv->ctrl[CTRL_PUT] = FIFO_BASE + FIFO_OFFSET;
  123. priv->ctrl[CTRL_GET] = FIFO_BASE + FIFO_OFFSET;
  124. }
  125. static int ps3vram_wait_ring(struct mtd_info *mtd, int timeout)
  126. {
  127. struct ps3vram_priv *priv = mtd->priv;
  128. /* wait until setup commands are processed */
  129. timeout *= 1000;
  130. while (--timeout) {
  131. if (priv->ctrl[CTRL_PUT] == priv->ctrl[CTRL_GET])
  132. break;
  133. udelay(1);
  134. }
  135. if (timeout == 0) {
  136. pr_err("FIFO timeout (%08x/%08x/%08x)\n", priv->ctrl[CTRL_PUT],
  137. priv->ctrl[CTRL_GET], priv->ctrl[CTRL_TOP]);
  138. return -ETIMEDOUT;
  139. }
  140. return 0;
  141. }
  142. static inline void ps3vram_out_ring(struct ps3vram_priv *priv, uint32_t data)
  143. {
  144. *(priv->fifo_ptr)++ = data;
  145. }
  146. static inline void ps3vram_begin_ring(struct ps3vram_priv *priv, uint32_t chan,
  147. uint32_t tag, uint32_t size)
  148. {
  149. ps3vram_out_ring(priv, (size << 18) | (chan << 13) | tag);
  150. }
  151. static void ps3vram_rewind_ring(struct mtd_info *mtd)
  152. {
  153. struct ps3vram_priv *priv = mtd->priv;
  154. u64 status;
  155. ps3vram_out_ring(priv, 0x20000000 | (FIFO_BASE + FIFO_OFFSET));
  156. priv->ctrl[CTRL_PUT] = FIFO_BASE + FIFO_OFFSET;
  157. /* asking the HV for a blit will kick the fifo */
  158. status = lv1_gpu_context_attribute(priv->context_handle,
  159. L1GPU_CONTEXT_ATTRIBUTE_FB_BLIT,
  160. 0, 0, 0, 0);
  161. if (status)
  162. pr_err("ps3vram: lv1_gpu_context_attribute FB_BLIT failed\n");
  163. priv->fifo_ptr = priv->fifo_base;
  164. }
  165. static void ps3vram_fire_ring(struct mtd_info *mtd)
  166. {
  167. struct ps3vram_priv *priv = mtd->priv;
  168. u64 status;
  169. mutex_lock(&ps3_gpu_mutex);
  170. priv->ctrl[CTRL_PUT] = FIFO_BASE + FIFO_OFFSET +
  171. (priv->fifo_ptr - priv->fifo_base) * sizeof(uint32_t);
  172. /* asking the HV for a blit will kick the fifo */
  173. status = lv1_gpu_context_attribute(priv->context_handle,
  174. L1GPU_CONTEXT_ATTRIBUTE_FB_BLIT,
  175. 0, 0, 0, 0);
  176. if (status)
  177. pr_err("ps3vram: lv1_gpu_context_attribute FB_BLIT failed\n");
  178. if ((priv->fifo_ptr - priv->fifo_base) * sizeof(uint32_t) >
  179. FIFO_SIZE - 1024) {
  180. dbg("fifo full, rewinding");
  181. ps3vram_wait_ring(mtd, 200);
  182. ps3vram_rewind_ring(mtd);
  183. }
  184. mutex_unlock(&ps3_gpu_mutex);
  185. }
  186. static void ps3vram_bind(struct mtd_info *mtd)
  187. {
  188. struct ps3vram_priv *priv = mtd->priv;
  189. ps3vram_begin_ring(priv, UPLOAD_SUBCH, 0, 1);
  190. ps3vram_out_ring(priv, 0x31337303);
  191. ps3vram_begin_ring(priv, UPLOAD_SUBCH, 0x180, 3);
  192. ps3vram_out_ring(priv, DMA_NOTIFIER_HANDLE_BASE + NOTIFIER);
  193. ps3vram_out_ring(priv, 0xfeed0001); /* DMA system RAM instance */
  194. ps3vram_out_ring(priv, 0xfeed0000); /* DMA video RAM instance */
  195. ps3vram_begin_ring(priv, DOWNLOAD_SUBCH, 0, 1);
  196. ps3vram_out_ring(priv, 0x3137c0de);
  197. ps3vram_begin_ring(priv, DOWNLOAD_SUBCH, 0x180, 3);
  198. ps3vram_out_ring(priv, DMA_NOTIFIER_HANDLE_BASE + NOTIFIER);
  199. ps3vram_out_ring(priv, 0xfeed0000); /* DMA video RAM instance */
  200. ps3vram_out_ring(priv, 0xfeed0001); /* DMA system RAM instance */
  201. ps3vram_fire_ring(mtd);
  202. }
  203. static int ps3vram_upload(struct mtd_info *mtd, unsigned int src_offset,
  204. unsigned int dst_offset, int len, int count)
  205. {
  206. struct ps3vram_priv *priv = mtd->priv;
  207. ps3vram_begin_ring(priv, UPLOAD_SUBCH,
  208. NV_MEMORY_TO_MEMORY_FORMAT_OFFSET_IN, 8);
  209. ps3vram_out_ring(priv, XDR_IOIF + src_offset);
  210. ps3vram_out_ring(priv, dst_offset);
  211. ps3vram_out_ring(priv, len);
  212. ps3vram_out_ring(priv, len);
  213. ps3vram_out_ring(priv, len);
  214. ps3vram_out_ring(priv, count);
  215. ps3vram_out_ring(priv, (1 << 8) | 1);
  216. ps3vram_out_ring(priv, 0);
  217. ps3vram_notifier_reset(mtd);
  218. ps3vram_begin_ring(priv, UPLOAD_SUBCH,
  219. NV_MEMORY_TO_MEMORY_FORMAT_NOTIFY, 1);
  220. ps3vram_out_ring(priv, 0);
  221. ps3vram_begin_ring(priv, UPLOAD_SUBCH, 0x100, 1);
  222. ps3vram_out_ring(priv, 0);
  223. ps3vram_fire_ring(mtd);
  224. if (ps3vram_notifier_wait(mtd, 200) < 0) {
  225. pr_err("notifier timeout\n");
  226. ps3vram_dump_ring(mtd);
  227. ps3vram_dump_reports(mtd);
  228. return -1;
  229. }
  230. return 0;
  231. }
  232. static int ps3vram_download(struct mtd_info *mtd, unsigned int src_offset,
  233. unsigned int dst_offset, int len, int count)
  234. {
  235. struct ps3vram_priv *priv = mtd->priv;
  236. ps3vram_begin_ring(priv, DOWNLOAD_SUBCH,
  237. NV_MEMORY_TO_MEMORY_FORMAT_OFFSET_IN, 8);
  238. ps3vram_out_ring(priv, src_offset);
  239. ps3vram_out_ring(priv, XDR_IOIF + dst_offset);
  240. ps3vram_out_ring(priv, len);
  241. ps3vram_out_ring(priv, len);
  242. ps3vram_out_ring(priv, len);
  243. ps3vram_out_ring(priv, count);
  244. ps3vram_out_ring(priv, (1 << 8) | 1);
  245. ps3vram_out_ring(priv, 0);
  246. ps3vram_notifier_reset(mtd);
  247. ps3vram_begin_ring(priv, DOWNLOAD_SUBCH,
  248. NV_MEMORY_TO_MEMORY_FORMAT_NOTIFY, 1);
  249. ps3vram_out_ring(priv, 0);
  250. ps3vram_begin_ring(priv, DOWNLOAD_SUBCH, 0x100, 1);
  251. ps3vram_out_ring(priv, 0);
  252. ps3vram_fire_ring(mtd);
  253. if (ps3vram_notifier_wait(mtd, 200) < 0) {
  254. pr_err("notifier timeout\n");
  255. ps3vram_dump_ring(mtd);
  256. ps3vram_dump_reports(mtd);
  257. return -1;
  258. }
  259. return 0;
  260. }
  261. static void ps3vram_cache_evict(struct mtd_info *mtd, int entry)
  262. {
  263. struct ps3vram_priv *priv = mtd->priv;
  264. struct ps3vram_cache *cache = &priv->cache;
  265. if (cache->tags[entry].flags & CACHE_PAGE_DIRTY) {
  266. dbg("flushing %d : 0x%08x", entry, cache->tags[entry].address);
  267. if (ps3vram_upload(mtd,
  268. CACHE_OFFSET + entry * cache->page_size,
  269. cache->tags[entry].address,
  270. DMA_PAGE_SIZE,
  271. cache->page_size / DMA_PAGE_SIZE) < 0) {
  272. pr_err("failed to upload from 0x%x to 0x%x size 0x%x\n",
  273. entry * cache->page_size,
  274. cache->tags[entry].address,
  275. cache->page_size);
  276. }
  277. cache->tags[entry].flags &= ~CACHE_PAGE_DIRTY;
  278. }
  279. }
  280. static void ps3vram_cache_load(struct mtd_info *mtd, int entry,
  281. unsigned int address)
  282. {
  283. struct ps3vram_priv *priv = mtd->priv;
  284. struct ps3vram_cache *cache = &priv->cache;
  285. dbg("fetching %d : 0x%08x", entry, address);
  286. if (ps3vram_download(mtd,
  287. address,
  288. CACHE_OFFSET + entry * cache->page_size,
  289. DMA_PAGE_SIZE,
  290. cache->page_size / DMA_PAGE_SIZE) < 0) {
  291. pr_err("failed to download from 0x%x to 0x%x size 0x%x\n",
  292. address,
  293. entry * cache->page_size,
  294. cache->page_size);
  295. }
  296. cache->tags[entry].address = address;
  297. cache->tags[entry].flags |= CACHE_PAGE_PRESENT;
  298. }
  299. static void ps3vram_cache_flush(struct mtd_info *mtd)
  300. {
  301. struct ps3vram_priv *priv = mtd->priv;
  302. struct ps3vram_cache *cache = &priv->cache;
  303. int i;
  304. dbg("FLUSH");
  305. for (i = 0; i < cache->page_count; i++) {
  306. ps3vram_cache_evict(mtd, i);
  307. cache->tags[i].flags = 0;
  308. }
  309. }
  310. static unsigned int ps3vram_cache_match(struct mtd_info *mtd, loff_t address)
  311. {
  312. struct ps3vram_priv *priv = mtd->priv;
  313. struct ps3vram_cache *cache = &priv->cache;
  314. unsigned int base;
  315. unsigned int offset;
  316. int i;
  317. static int counter;
  318. offset = (unsigned int) (address & (cache->page_size - 1));
  319. base = (unsigned int) (address - offset);
  320. /* fully associative check */
  321. for (i = 0; i < cache->page_count; i++) {
  322. if ((cache->tags[i].flags & CACHE_PAGE_PRESENT) &&
  323. cache->tags[i].address == base) {
  324. dbg("found entry %d : 0x%08x",
  325. i, cache->tags[i].address);
  326. return i;
  327. }
  328. }
  329. /* choose a random entry */
  330. i = (jiffies + (counter++)) % cache->page_count;
  331. dbg("using cache entry %d", i);
  332. ps3vram_cache_evict(mtd, i);
  333. ps3vram_cache_load(mtd, i, base);
  334. return i;
  335. }
  336. static int ps3vram_cache_init(struct mtd_info *mtd)
  337. {
  338. struct ps3vram_priv *priv = mtd->priv;
  339. pr_info("creating cache: %d entries, %d bytes pages\n",
  340. CACHE_PAGE_COUNT, CACHE_PAGE_SIZE);
  341. priv->cache.page_count = CACHE_PAGE_COUNT;
  342. priv->cache.page_size = CACHE_PAGE_SIZE;
  343. priv->cache.tags = kzalloc(sizeof(struct ps3vram_tag) *
  344. CACHE_PAGE_COUNT, GFP_KERNEL);
  345. if (priv->cache.tags == NULL) {
  346. pr_err("could not allocate cache tags\n");
  347. return -ENOMEM;
  348. }
  349. return 0;
  350. }
  351. static void ps3vram_cache_cleanup(struct mtd_info *mtd)
  352. {
  353. struct ps3vram_priv *priv = mtd->priv;
  354. ps3vram_cache_flush(mtd);
  355. kfree(priv->cache.tags);
  356. }
  357. static int ps3vram_erase(struct mtd_info *mtd, struct erase_info *instr)
  358. {
  359. struct ps3vram_priv *priv = mtd->priv;
  360. if (instr->addr + instr->len > mtd->size)
  361. return -EINVAL;
  362. mutex_lock(&priv->lock);
  363. ps3vram_cache_flush(mtd);
  364. /* Set bytes to 0xFF */
  365. memset(priv->base + instr->addr, 0xFF, instr->len);
  366. mutex_unlock(&priv->lock);
  367. instr->state = MTD_ERASE_DONE;
  368. mtd_erase_callback(instr);
  369. return 0;
  370. }
  371. static int ps3vram_read(struct mtd_info *mtd, loff_t from, size_t len,
  372. size_t *retlen, u_char *buf)
  373. {
  374. struct ps3vram_priv *priv = mtd->priv;
  375. unsigned int cached, count;
  376. dbg("from = 0x%08x len = 0x%zx", (unsigned int) from, len);
  377. if (from >= mtd->size)
  378. return -EINVAL;
  379. if (len > mtd->size - from)
  380. len = mtd->size - from;
  381. /* Copy from vram to buf */
  382. count = len;
  383. while (count) {
  384. unsigned int offset, avail;
  385. unsigned int entry;
  386. offset = (unsigned int) (from & (priv->cache.page_size - 1));
  387. avail = priv->cache.page_size - offset;
  388. mutex_lock(&priv->lock);
  389. entry = ps3vram_cache_match(mtd, from);
  390. cached = CACHE_OFFSET + entry * priv->cache.page_size + offset;
  391. dbg("from=%08x cached=%08x offset=%08x avail=%08x count=%08x",
  392. (unsigned)from, cached, offset, avail, count);
  393. if (avail > count)
  394. avail = count;
  395. memcpy(buf, priv->xdr_buf + cached, avail);
  396. mutex_unlock(&priv->lock);
  397. buf += avail;
  398. count -= avail;
  399. from += avail;
  400. }
  401. *retlen = len;
  402. return 0;
  403. }
  404. static int ps3vram_write(struct mtd_info *mtd, loff_t to, size_t len,
  405. size_t *retlen, const u_char *buf)
  406. {
  407. struct ps3vram_priv *priv = mtd->priv;
  408. unsigned int cached, count;
  409. if (to >= mtd->size)
  410. return -EINVAL;
  411. if (len > mtd->size - to)
  412. len = mtd->size - to;
  413. /* Copy from buf to vram */
  414. count = len;
  415. while (count) {
  416. unsigned int offset, avail;
  417. unsigned int entry;
  418. offset = (unsigned int) (to & (priv->cache.page_size - 1));
  419. avail = priv->cache.page_size - offset;
  420. mutex_lock(&priv->lock);
  421. entry = ps3vram_cache_match(mtd, to);
  422. cached = CACHE_OFFSET + entry * priv->cache.page_size + offset;
  423. dbg("to=%08x cached=%08x offset=%08x avail=%08x count=%08x",
  424. (unsigned) to, cached, offset, avail, count);
  425. if (avail > count)
  426. avail = count;
  427. memcpy(priv->xdr_buf + cached, buf, avail);
  428. priv->cache.tags[entry].flags |= CACHE_PAGE_DIRTY;
  429. mutex_unlock(&priv->lock);
  430. buf += avail;
  431. count -= avail;
  432. to += avail;
  433. }
  434. *retlen = len;
  435. return 0;
  436. }
  437. static int __devinit ps3vram_probe(struct ps3_system_bus_device *dev)
  438. {
  439. struct ps3vram_priv *priv;
  440. uint64_t status;
  441. uint64_t ddr_lpar, ctrl_lpar, info_lpar, reports_lpar;
  442. int64_t ddr_size;
  443. uint64_t reports_size;
  444. int ret = -ENOMEM;
  445. char *rest;
  446. ret = -EIO;
  447. ps3vram_mtd.priv = kzalloc(sizeof(struct ps3vram_priv), GFP_KERNEL);
  448. if (!ps3vram_mtd.priv)
  449. goto out;
  450. priv = ps3vram_mtd.priv;
  451. mutex_init(&priv->lock);
  452. /* Allocate XDR buffer (1MiB aligned) */
  453. priv->xdr_buf = (uint8_t *) __get_free_pages(GFP_KERNEL,
  454. get_order(XDR_BUF_SIZE));
  455. if (priv->xdr_buf == NULL) {
  456. pr_err("ps3vram: could not allocate XDR buffer\n");
  457. ret = -ENOMEM;
  458. goto out_free_priv;
  459. }
  460. /* Put FIFO at begginning of XDR buffer */
  461. priv->fifo_base = (uint32_t *) (priv->xdr_buf + FIFO_OFFSET);
  462. priv->fifo_ptr = priv->fifo_base;
  463. /* XXX: Need to open GPU, in case ps3fb or snd_ps3 aren't loaded */
  464. if (ps3_open_hv_device(dev)) {
  465. pr_err("ps3vram: ps3_open_hv_device failed\n");
  466. ret = -EAGAIN;
  467. goto out_close_gpu;
  468. }
  469. /* Request memory */
  470. status = -1;
  471. ddr_size = memparse(size, &rest);
  472. if (*rest == '-')
  473. ddr_size -= ps3fb_videomemory.size;
  474. ddr_size = ALIGN(ddr_size, 1024*1024);
  475. if (ddr_size <= 0) {
  476. printk(KERN_ERR "ps3vram: specified size is too small\n");
  477. ret = -EINVAL;
  478. goto out_close_gpu;
  479. }
  480. while (ddr_size > 0) {
  481. status = lv1_gpu_memory_allocate(ddr_size, 0, 0, 0, 0,
  482. &priv->memory_handle,
  483. &ddr_lpar);
  484. if (status == 0)
  485. break;
  486. ddr_size -= 1024*1024;
  487. }
  488. if (status != 0 || ddr_size <= 0) {
  489. pr_err("ps3vram: lv1_gpu_memory_allocate failed\n");
  490. ret = -ENOMEM;
  491. goto out_free_xdr_buf;
  492. }
  493. pr_info("ps3vram: allocated %u MiB of DDR memory\n",
  494. (unsigned int) (ddr_size / 1024 / 1024));
  495. /* Request context */
  496. status = lv1_gpu_context_allocate(priv->memory_handle,
  497. 0,
  498. &priv->context_handle,
  499. &ctrl_lpar,
  500. &info_lpar,
  501. &reports_lpar,
  502. &reports_size);
  503. if (status) {
  504. pr_err("ps3vram: lv1_gpu_context_allocate failed\n");
  505. ret = -ENOMEM;
  506. goto out_free_memory;
  507. }
  508. /* Map XDR buffer to RSX */
  509. status = lv1_gpu_context_iomap(priv->context_handle, XDR_IOIF,
  510. ps3_mm_phys_to_lpar(__pa(priv->xdr_buf)),
  511. XDR_BUF_SIZE, 0);
  512. if (status) {
  513. pr_err("ps3vram: lv1_gpu_context_iomap failed\n");
  514. ret = -ENOMEM;
  515. goto out_free_context;
  516. }
  517. priv->base = ioremap(ddr_lpar, ddr_size);
  518. if (!priv->base) {
  519. pr_err("ps3vram: ioremap failed\n");
  520. ret = -ENOMEM;
  521. goto out_free_context;
  522. }
  523. priv->ctrl = ioremap(ctrl_lpar, 64 * 1024);
  524. if (!priv->ctrl) {
  525. pr_err("ps3vram: ioremap failed\n");
  526. ret = -ENOMEM;
  527. goto out_unmap_vram;
  528. }
  529. priv->reports = ioremap(reports_lpar, reports_size);
  530. if (!priv->reports) {
  531. pr_err("ps3vram: ioremap failed\n");
  532. ret = -ENOMEM;
  533. goto out_unmap_ctrl;
  534. }
  535. mutex_lock(&ps3_gpu_mutex);
  536. ps3vram_init_ring(&ps3vram_mtd);
  537. mutex_unlock(&ps3_gpu_mutex);
  538. ps3vram_mtd.name = "ps3vram";
  539. ps3vram_mtd.size = ddr_size;
  540. ps3vram_mtd.flags = MTD_CAP_RAM;
  541. ps3vram_mtd.erase = ps3vram_erase;
  542. ps3vram_mtd.point = NULL;
  543. ps3vram_mtd.unpoint = NULL;
  544. ps3vram_mtd.read = ps3vram_read;
  545. ps3vram_mtd.write = ps3vram_write;
  546. ps3vram_mtd.owner = THIS_MODULE;
  547. ps3vram_mtd.type = MTD_RAM;
  548. ps3vram_mtd.erasesize = CACHE_PAGE_SIZE;
  549. ps3vram_mtd.writesize = 1;
  550. ps3vram_bind(&ps3vram_mtd);
  551. mutex_lock(&ps3_gpu_mutex);
  552. ret = ps3vram_wait_ring(&ps3vram_mtd, 100);
  553. mutex_unlock(&ps3_gpu_mutex);
  554. if (ret < 0) {
  555. pr_err("failed to initialize channels\n");
  556. ret = -ETIMEDOUT;
  557. goto out_unmap_reports;
  558. }
  559. ps3vram_cache_init(&ps3vram_mtd);
  560. if (add_mtd_device(&ps3vram_mtd)) {
  561. pr_err("ps3vram: failed to register device\n");
  562. ret = -EAGAIN;
  563. goto out_cache_cleanup;
  564. }
  565. pr_info("ps3vram mtd device registered, %lu bytes\n", ddr_size);
  566. return 0;
  567. out_cache_cleanup:
  568. ps3vram_cache_cleanup(&ps3vram_mtd);
  569. out_unmap_reports:
  570. iounmap(priv->reports);
  571. out_unmap_ctrl:
  572. iounmap(priv->ctrl);
  573. out_unmap_vram:
  574. iounmap(priv->base);
  575. out_free_context:
  576. lv1_gpu_context_free(priv->context_handle);
  577. out_free_memory:
  578. lv1_gpu_memory_free(priv->memory_handle);
  579. out_close_gpu:
  580. ps3_close_hv_device(dev);
  581. out_free_xdr_buf:
  582. free_pages((unsigned long) priv->xdr_buf, get_order(XDR_BUF_SIZE));
  583. out_free_priv:
  584. kfree(ps3vram_mtd.priv);
  585. ps3vram_mtd.priv = NULL;
  586. out:
  587. return ret;
  588. }
  589. static int ps3vram_shutdown(struct ps3_system_bus_device *dev)
  590. {
  591. struct ps3vram_priv *priv;
  592. priv = ps3vram_mtd.priv;
  593. del_mtd_device(&ps3vram_mtd);
  594. ps3vram_cache_cleanup(&ps3vram_mtd);
  595. iounmap(priv->reports);
  596. iounmap(priv->ctrl);
  597. iounmap(priv->base);
  598. lv1_gpu_context_free(priv->context_handle);
  599. lv1_gpu_memory_free(priv->memory_handle);
  600. ps3_close_hv_device(dev);
  601. free_pages((unsigned long) priv->xdr_buf, get_order(XDR_BUF_SIZE));
  602. kfree(priv);
  603. return 0;
  604. }
  605. static struct ps3_system_bus_driver ps3vram_driver = {
  606. .match_id = PS3_MATCH_ID_GPU,
  607. .match_sub_id = PS3_MATCH_SUB_ID_GPU_RAMDISK,
  608. .core.name = DEVICE_NAME,
  609. .core.owner = THIS_MODULE,
  610. .probe = ps3vram_probe,
  611. .remove = ps3vram_shutdown,
  612. .shutdown = ps3vram_shutdown,
  613. };
  614. static int __init ps3vram_init(void)
  615. {
  616. return ps3_system_bus_driver_register(&ps3vram_driver);
  617. }
  618. static void __exit ps3vram_exit(void)
  619. {
  620. ps3_system_bus_driver_unregister(&ps3vram_driver);
  621. }
  622. module_init(ps3vram_init);
  623. module_exit(ps3vram_exit);
  624. MODULE_LICENSE("GPL");
  625. MODULE_AUTHOR("Jim Paris <jim@jtan.com>");
  626. MODULE_DESCRIPTION("MTD driver for PS3 video RAM");
  627. MODULE_ALIAS(PS3_MODULE_ALIAS_GPU_RAMDISK);