ps3vram.c 19 KB

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