ps3vram.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879
  1. /*
  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. #include <linux/blkdev.h>
  11. #include <linux/delay.h>
  12. #include <linux/proc_fs.h>
  13. #include <linux/seq_file.h>
  14. #include <linux/slab.h>
  15. #include <asm/cell-regs.h>
  16. #include <asm/firmware.h>
  17. #include <asm/lv1call.h>
  18. #include <asm/ps3.h>
  19. #include <asm/ps3gpu.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 CACHE_PAGE_PRESENT 1
  38. #define CACHE_PAGE_DIRTY 2
  39. struct ps3vram_tag {
  40. unsigned int address;
  41. unsigned int flags;
  42. };
  43. struct ps3vram_cache {
  44. unsigned int page_count;
  45. unsigned int page_size;
  46. struct ps3vram_tag *tags;
  47. unsigned int hit;
  48. unsigned int miss;
  49. };
  50. struct ps3vram_priv {
  51. struct request_queue *queue;
  52. struct gendisk *gendisk;
  53. u64 size;
  54. u64 memory_handle;
  55. u64 context_handle;
  56. u32 *ctrl;
  57. void *reports;
  58. u8 *xdr_buf;
  59. u32 *fifo_base;
  60. u32 *fifo_ptr;
  61. struct ps3vram_cache cache;
  62. spinlock_t lock; /* protecting list of bios */
  63. struct bio_list list;
  64. };
  65. static int ps3vram_major;
  66. static const struct block_device_operations ps3vram_fops = {
  67. .owner = THIS_MODULE,
  68. };
  69. #define DMA_NOTIFIER_HANDLE_BASE 0x66604200 /* first DMA notifier handle */
  70. #define DMA_NOTIFIER_OFFSET_BASE 0x1000 /* first DMA notifier offset */
  71. #define DMA_NOTIFIER_SIZE 0x40
  72. #define NOTIFIER 7 /* notifier used for completion report */
  73. static char *size = "256M";
  74. module_param(size, charp, 0);
  75. MODULE_PARM_DESC(size, "memory size");
  76. static u32 *ps3vram_get_notifier(void *reports, int notifier)
  77. {
  78. return reports + DMA_NOTIFIER_OFFSET_BASE +
  79. DMA_NOTIFIER_SIZE * notifier;
  80. }
  81. static void ps3vram_notifier_reset(struct ps3_system_bus_device *dev)
  82. {
  83. struct ps3vram_priv *priv = ps3_system_bus_get_drvdata(dev);
  84. u32 *notify = ps3vram_get_notifier(priv->reports, NOTIFIER);
  85. int i;
  86. for (i = 0; i < 4; i++)
  87. notify[i] = 0xffffffff;
  88. }
  89. static int ps3vram_notifier_wait(struct ps3_system_bus_device *dev,
  90. unsigned int timeout_ms)
  91. {
  92. struct ps3vram_priv *priv = ps3_system_bus_get_drvdata(dev);
  93. u32 *notify = ps3vram_get_notifier(priv->reports, NOTIFIER);
  94. unsigned long timeout;
  95. for (timeout = 20; timeout; timeout--) {
  96. if (!notify[3])
  97. return 0;
  98. udelay(10);
  99. }
  100. timeout = jiffies + msecs_to_jiffies(timeout_ms);
  101. do {
  102. if (!notify[3])
  103. return 0;
  104. msleep(1);
  105. } while (time_before(jiffies, timeout));
  106. return -ETIMEDOUT;
  107. }
  108. static void ps3vram_init_ring(struct ps3_system_bus_device *dev)
  109. {
  110. struct ps3vram_priv *priv = ps3_system_bus_get_drvdata(dev);
  111. priv->ctrl[CTRL_PUT] = FIFO_BASE + FIFO_OFFSET;
  112. priv->ctrl[CTRL_GET] = FIFO_BASE + FIFO_OFFSET;
  113. }
  114. static int ps3vram_wait_ring(struct ps3_system_bus_device *dev,
  115. unsigned int timeout_ms)
  116. {
  117. struct ps3vram_priv *priv = ps3_system_bus_get_drvdata(dev);
  118. unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
  119. do {
  120. if (priv->ctrl[CTRL_PUT] == priv->ctrl[CTRL_GET])
  121. return 0;
  122. msleep(1);
  123. } while (time_before(jiffies, timeout));
  124. dev_warn(&dev->core, "FIFO timeout (%08x/%08x/%08x)\n",
  125. priv->ctrl[CTRL_PUT], priv->ctrl[CTRL_GET],
  126. priv->ctrl[CTRL_TOP]);
  127. return -ETIMEDOUT;
  128. }
  129. static void ps3vram_out_ring(struct ps3vram_priv *priv, u32 data)
  130. {
  131. *(priv->fifo_ptr)++ = data;
  132. }
  133. static void ps3vram_begin_ring(struct ps3vram_priv *priv, u32 chan, u32 tag,
  134. u32 size)
  135. {
  136. ps3vram_out_ring(priv, (size << 18) | (chan << 13) | tag);
  137. }
  138. static void ps3vram_rewind_ring(struct ps3_system_bus_device *dev)
  139. {
  140. struct ps3vram_priv *priv = ps3_system_bus_get_drvdata(dev);
  141. int status;
  142. ps3vram_out_ring(priv, 0x20000000 | (FIFO_BASE + FIFO_OFFSET));
  143. priv->ctrl[CTRL_PUT] = FIFO_BASE + FIFO_OFFSET;
  144. /* asking the HV for a blit will kick the FIFO */
  145. status = lv1_gpu_fb_blit(priv->context_handle, 0, 0, 0, 0);
  146. if (status)
  147. dev_err(&dev->core, "%s: lv1_gpu_fb_blit failed %d\n",
  148. __func__, status);
  149. priv->fifo_ptr = priv->fifo_base;
  150. }
  151. static void ps3vram_fire_ring(struct ps3_system_bus_device *dev)
  152. {
  153. struct ps3vram_priv *priv = ps3_system_bus_get_drvdata(dev);
  154. int status;
  155. mutex_lock(&ps3_gpu_mutex);
  156. priv->ctrl[CTRL_PUT] = FIFO_BASE + FIFO_OFFSET +
  157. (priv->fifo_ptr - priv->fifo_base) * sizeof(u32);
  158. /* asking the HV for a blit will kick the FIFO */
  159. status = lv1_gpu_fb_blit(priv->context_handle, 0, 0, 0, 0);
  160. if (status)
  161. dev_err(&dev->core, "%s: lv1_gpu_fb_blit failed %d\n",
  162. __func__, status);
  163. if ((priv->fifo_ptr - priv->fifo_base) * sizeof(u32) >
  164. FIFO_SIZE - 1024) {
  165. dev_dbg(&dev->core, "FIFO full, rewinding\n");
  166. ps3vram_wait_ring(dev, 200);
  167. ps3vram_rewind_ring(dev);
  168. }
  169. mutex_unlock(&ps3_gpu_mutex);
  170. }
  171. static void ps3vram_bind(struct ps3_system_bus_device *dev)
  172. {
  173. struct ps3vram_priv *priv = ps3_system_bus_get_drvdata(dev);
  174. ps3vram_begin_ring(priv, UPLOAD_SUBCH, 0, 1);
  175. ps3vram_out_ring(priv, 0x31337303);
  176. ps3vram_begin_ring(priv, UPLOAD_SUBCH, 0x180, 3);
  177. ps3vram_out_ring(priv, DMA_NOTIFIER_HANDLE_BASE + NOTIFIER);
  178. ps3vram_out_ring(priv, 0xfeed0001); /* DMA system RAM instance */
  179. ps3vram_out_ring(priv, 0xfeed0000); /* DMA video RAM instance */
  180. ps3vram_begin_ring(priv, DOWNLOAD_SUBCH, 0, 1);
  181. ps3vram_out_ring(priv, 0x3137c0de);
  182. ps3vram_begin_ring(priv, DOWNLOAD_SUBCH, 0x180, 3);
  183. ps3vram_out_ring(priv, DMA_NOTIFIER_HANDLE_BASE + NOTIFIER);
  184. ps3vram_out_ring(priv, 0xfeed0000); /* DMA video RAM instance */
  185. ps3vram_out_ring(priv, 0xfeed0001); /* DMA system RAM instance */
  186. ps3vram_fire_ring(dev);
  187. }
  188. static int ps3vram_upload(struct ps3_system_bus_device *dev,
  189. unsigned int src_offset, unsigned int dst_offset,
  190. int len, int count)
  191. {
  192. struct ps3vram_priv *priv = ps3_system_bus_get_drvdata(dev);
  193. ps3vram_begin_ring(priv, UPLOAD_SUBCH,
  194. NV_MEMORY_TO_MEMORY_FORMAT_OFFSET_IN, 8);
  195. ps3vram_out_ring(priv, XDR_IOIF + src_offset);
  196. ps3vram_out_ring(priv, dst_offset);
  197. ps3vram_out_ring(priv, len);
  198. ps3vram_out_ring(priv, len);
  199. ps3vram_out_ring(priv, len);
  200. ps3vram_out_ring(priv, count);
  201. ps3vram_out_ring(priv, (1 << 8) | 1);
  202. ps3vram_out_ring(priv, 0);
  203. ps3vram_notifier_reset(dev);
  204. ps3vram_begin_ring(priv, UPLOAD_SUBCH,
  205. NV_MEMORY_TO_MEMORY_FORMAT_NOTIFY, 1);
  206. ps3vram_out_ring(priv, 0);
  207. ps3vram_begin_ring(priv, UPLOAD_SUBCH, 0x100, 1);
  208. ps3vram_out_ring(priv, 0);
  209. ps3vram_fire_ring(dev);
  210. if (ps3vram_notifier_wait(dev, 200) < 0) {
  211. dev_warn(&dev->core, "%s: Notifier timeout\n", __func__);
  212. return -1;
  213. }
  214. return 0;
  215. }
  216. static int ps3vram_download(struct ps3_system_bus_device *dev,
  217. unsigned int src_offset, unsigned int dst_offset,
  218. int len, int count)
  219. {
  220. struct ps3vram_priv *priv = ps3_system_bus_get_drvdata(dev);
  221. ps3vram_begin_ring(priv, DOWNLOAD_SUBCH,
  222. NV_MEMORY_TO_MEMORY_FORMAT_OFFSET_IN, 8);
  223. ps3vram_out_ring(priv, src_offset);
  224. ps3vram_out_ring(priv, XDR_IOIF + dst_offset);
  225. ps3vram_out_ring(priv, len);
  226. ps3vram_out_ring(priv, len);
  227. ps3vram_out_ring(priv, len);
  228. ps3vram_out_ring(priv, count);
  229. ps3vram_out_ring(priv, (1 << 8) | 1);
  230. ps3vram_out_ring(priv, 0);
  231. ps3vram_notifier_reset(dev);
  232. ps3vram_begin_ring(priv, DOWNLOAD_SUBCH,
  233. NV_MEMORY_TO_MEMORY_FORMAT_NOTIFY, 1);
  234. ps3vram_out_ring(priv, 0);
  235. ps3vram_begin_ring(priv, DOWNLOAD_SUBCH, 0x100, 1);
  236. ps3vram_out_ring(priv, 0);
  237. ps3vram_fire_ring(dev);
  238. if (ps3vram_notifier_wait(dev, 200) < 0) {
  239. dev_warn(&dev->core, "%s: Notifier timeout\n", __func__);
  240. return -1;
  241. }
  242. return 0;
  243. }
  244. static void ps3vram_cache_evict(struct ps3_system_bus_device *dev, int entry)
  245. {
  246. struct ps3vram_priv *priv = ps3_system_bus_get_drvdata(dev);
  247. struct ps3vram_cache *cache = &priv->cache;
  248. if (!(cache->tags[entry].flags & CACHE_PAGE_DIRTY))
  249. return;
  250. dev_dbg(&dev->core, "Flushing %d: 0x%08x\n", entry,
  251. cache->tags[entry].address);
  252. if (ps3vram_upload(dev, CACHE_OFFSET + entry * cache->page_size,
  253. cache->tags[entry].address, DMA_PAGE_SIZE,
  254. cache->page_size / DMA_PAGE_SIZE) < 0) {
  255. dev_err(&dev->core,
  256. "Failed to upload from 0x%x to " "0x%x size 0x%x\n",
  257. entry * cache->page_size, cache->tags[entry].address,
  258. cache->page_size);
  259. }
  260. cache->tags[entry].flags &= ~CACHE_PAGE_DIRTY;
  261. }
  262. static void ps3vram_cache_load(struct ps3_system_bus_device *dev, int entry,
  263. unsigned int address)
  264. {
  265. struct ps3vram_priv *priv = ps3_system_bus_get_drvdata(dev);
  266. struct ps3vram_cache *cache = &priv->cache;
  267. dev_dbg(&dev->core, "Fetching %d: 0x%08x\n", entry, address);
  268. if (ps3vram_download(dev, address,
  269. CACHE_OFFSET + entry * cache->page_size,
  270. DMA_PAGE_SIZE,
  271. cache->page_size / DMA_PAGE_SIZE) < 0) {
  272. dev_err(&dev->core,
  273. "Failed to download from 0x%x to 0x%x size 0x%x\n",
  274. address, entry * cache->page_size, cache->page_size);
  275. }
  276. cache->tags[entry].address = address;
  277. cache->tags[entry].flags |= CACHE_PAGE_PRESENT;
  278. }
  279. static void ps3vram_cache_flush(struct ps3_system_bus_device *dev)
  280. {
  281. struct ps3vram_priv *priv = ps3_system_bus_get_drvdata(dev);
  282. struct ps3vram_cache *cache = &priv->cache;
  283. int i;
  284. dev_dbg(&dev->core, "FLUSH\n");
  285. for (i = 0; i < cache->page_count; i++) {
  286. ps3vram_cache_evict(dev, i);
  287. cache->tags[i].flags = 0;
  288. }
  289. }
  290. static unsigned int ps3vram_cache_match(struct ps3_system_bus_device *dev,
  291. loff_t address)
  292. {
  293. struct ps3vram_priv *priv = ps3_system_bus_get_drvdata(dev);
  294. struct ps3vram_cache *cache = &priv->cache;
  295. unsigned int base;
  296. unsigned int offset;
  297. int i;
  298. static int counter;
  299. offset = (unsigned int) (address & (cache->page_size - 1));
  300. base = (unsigned int) (address - offset);
  301. /* fully associative check */
  302. for (i = 0; i < cache->page_count; i++) {
  303. if ((cache->tags[i].flags & CACHE_PAGE_PRESENT) &&
  304. cache->tags[i].address == base) {
  305. cache->hit++;
  306. dev_dbg(&dev->core, "Found entry %d: 0x%08x\n", i,
  307. cache->tags[i].address);
  308. return i;
  309. }
  310. }
  311. /* choose a random entry */
  312. i = (jiffies + (counter++)) % cache->page_count;
  313. dev_dbg(&dev->core, "Using entry %d\n", i);
  314. ps3vram_cache_evict(dev, i);
  315. ps3vram_cache_load(dev, i, base);
  316. cache->miss++;
  317. return i;
  318. }
  319. static int ps3vram_cache_init(struct ps3_system_bus_device *dev)
  320. {
  321. struct ps3vram_priv *priv = ps3_system_bus_get_drvdata(dev);
  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(&dev->core, "Could not allocate cache tags\n");
  328. return -ENOMEM;
  329. }
  330. dev_info(&dev->core, "Created ram cache: %d entries, %d KiB each\n",
  331. CACHE_PAGE_COUNT, CACHE_PAGE_SIZE / 1024);
  332. return 0;
  333. }
  334. static void ps3vram_cache_cleanup(struct ps3_system_bus_device *dev)
  335. {
  336. struct ps3vram_priv *priv = ps3_system_bus_get_drvdata(dev);
  337. ps3vram_cache_flush(dev);
  338. kfree(priv->cache.tags);
  339. }
  340. static int ps3vram_read(struct ps3_system_bus_device *dev, loff_t from,
  341. size_t len, size_t *retlen, u_char *buf)
  342. {
  343. struct ps3vram_priv *priv = ps3_system_bus_get_drvdata(dev);
  344. unsigned int cached, count;
  345. dev_dbg(&dev->core, "%s: from=0x%08x len=0x%zx\n", __func__,
  346. (unsigned int)from, len);
  347. if (from >= priv->size)
  348. return -EIO;
  349. if (len > priv->size - from)
  350. len = priv->size - from;
  351. /* Copy from vram to buf */
  352. count = len;
  353. while (count) {
  354. unsigned int offset, avail;
  355. unsigned int entry;
  356. offset = (unsigned int) (from & (priv->cache.page_size - 1));
  357. avail = priv->cache.page_size - offset;
  358. entry = ps3vram_cache_match(dev, from);
  359. cached = CACHE_OFFSET + entry * priv->cache.page_size + offset;
  360. dev_dbg(&dev->core, "%s: from=%08x cached=%08x offset=%08x "
  361. "avail=%08x count=%08x\n", __func__,
  362. (unsigned int)from, cached, offset, avail, count);
  363. if (avail > count)
  364. avail = count;
  365. memcpy(buf, priv->xdr_buf + cached, avail);
  366. buf += avail;
  367. count -= avail;
  368. from += avail;
  369. }
  370. *retlen = len;
  371. return 0;
  372. }
  373. static int ps3vram_write(struct ps3_system_bus_device *dev, loff_t to,
  374. size_t len, size_t *retlen, const u_char *buf)
  375. {
  376. struct ps3vram_priv *priv = ps3_system_bus_get_drvdata(dev);
  377. unsigned int cached, count;
  378. if (to >= priv->size)
  379. return -EIO;
  380. if (len > priv->size - to)
  381. len = priv->size - to;
  382. /* Copy from buf to vram */
  383. count = len;
  384. while (count) {
  385. unsigned int offset, avail;
  386. unsigned int entry;
  387. offset = (unsigned int) (to & (priv->cache.page_size - 1));
  388. avail = priv->cache.page_size - offset;
  389. entry = ps3vram_cache_match(dev, to);
  390. cached = CACHE_OFFSET + entry * priv->cache.page_size + offset;
  391. dev_dbg(&dev->core, "%s: to=%08x cached=%08x offset=%08x "
  392. "avail=%08x count=%08x\n", __func__, (unsigned int)to,
  393. cached, offset, avail, count);
  394. if (avail > count)
  395. avail = count;
  396. memcpy(priv->xdr_buf + cached, buf, avail);
  397. priv->cache.tags[entry].flags |= CACHE_PAGE_DIRTY;
  398. buf += avail;
  399. count -= avail;
  400. to += avail;
  401. }
  402. *retlen = len;
  403. return 0;
  404. }
  405. static int ps3vram_proc_show(struct seq_file *m, void *v)
  406. {
  407. struct ps3vram_priv *priv = m->private;
  408. seq_printf(m, "hit:%u\nmiss:%u\n", priv->cache.hit, priv->cache.miss);
  409. return 0;
  410. }
  411. static int ps3vram_proc_open(struct inode *inode, struct file *file)
  412. {
  413. return single_open(file, ps3vram_proc_show, PDE(inode)->data);
  414. }
  415. static const struct file_operations ps3vram_proc_fops = {
  416. .owner = THIS_MODULE,
  417. .open = ps3vram_proc_open,
  418. .read = seq_read,
  419. .llseek = seq_lseek,
  420. .release = single_release,
  421. };
  422. static void __devinit ps3vram_proc_init(struct ps3_system_bus_device *dev)
  423. {
  424. struct ps3vram_priv *priv = ps3_system_bus_get_drvdata(dev);
  425. struct proc_dir_entry *pde;
  426. pde = proc_create_data(DEVICE_NAME, 0444, NULL, &ps3vram_proc_fops,
  427. priv);
  428. if (!pde)
  429. dev_warn(&dev->core, "failed to create /proc entry\n");
  430. }
  431. static struct bio *ps3vram_do_bio(struct ps3_system_bus_device *dev,
  432. struct bio *bio)
  433. {
  434. struct ps3vram_priv *priv = ps3_system_bus_get_drvdata(dev);
  435. int write = bio_data_dir(bio) == WRITE;
  436. const char *op = write ? "write" : "read";
  437. loff_t offset = bio->bi_sector << 9;
  438. int error = 0;
  439. struct bio_vec *bvec;
  440. unsigned int i;
  441. struct bio *next;
  442. bio_for_each_segment(bvec, bio, i) {
  443. /* PS3 is ppc64, so we don't handle highmem */
  444. char *ptr = page_address(bvec->bv_page) + bvec->bv_offset;
  445. size_t len = bvec->bv_len, retlen;
  446. dev_dbg(&dev->core, " %s %zu bytes at offset %llu\n", op,
  447. len, offset);
  448. if (write)
  449. error = ps3vram_write(dev, offset, len, &retlen, ptr);
  450. else
  451. error = ps3vram_read(dev, offset, len, &retlen, ptr);
  452. if (error) {
  453. dev_err(&dev->core, "%s failed\n", op);
  454. goto out;
  455. }
  456. if (retlen != len) {
  457. dev_err(&dev->core, "Short %s\n", op);
  458. error = -EIO;
  459. goto out;
  460. }
  461. offset += len;
  462. }
  463. dev_dbg(&dev->core, "%s completed\n", op);
  464. out:
  465. spin_lock_irq(&priv->lock);
  466. bio_list_pop(&priv->list);
  467. next = bio_list_peek(&priv->list);
  468. spin_unlock_irq(&priv->lock);
  469. bio_endio(bio, error);
  470. return next;
  471. }
  472. static int ps3vram_make_request(struct request_queue *q, struct bio *bio)
  473. {
  474. struct ps3_system_bus_device *dev = q->queuedata;
  475. struct ps3vram_priv *priv = ps3_system_bus_get_drvdata(dev);
  476. int busy;
  477. dev_dbg(&dev->core, "%s\n", __func__);
  478. spin_lock_irq(&priv->lock);
  479. busy = !bio_list_empty(&priv->list);
  480. bio_list_add(&priv->list, bio);
  481. spin_unlock_irq(&priv->lock);
  482. if (busy)
  483. return 0;
  484. do {
  485. bio = ps3vram_do_bio(dev, bio);
  486. } while (bio);
  487. return 0;
  488. }
  489. static int __devinit ps3vram_probe(struct ps3_system_bus_device *dev)
  490. {
  491. struct ps3vram_priv *priv;
  492. int error, status;
  493. struct request_queue *queue;
  494. struct gendisk *gendisk;
  495. u64 ddr_size, ddr_lpar, ctrl_lpar, info_lpar, reports_lpar,
  496. reports_size, xdr_lpar;
  497. char *rest;
  498. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  499. if (!priv) {
  500. error = -ENOMEM;
  501. goto fail;
  502. }
  503. spin_lock_init(&priv->lock);
  504. bio_list_init(&priv->list);
  505. ps3_system_bus_set_drvdata(dev, priv);
  506. /* Allocate XDR buffer (1MiB aligned) */
  507. priv->xdr_buf = (void *)__get_free_pages(GFP_KERNEL,
  508. get_order(XDR_BUF_SIZE));
  509. if (priv->xdr_buf == NULL) {
  510. dev_err(&dev->core, "Could not allocate XDR buffer\n");
  511. error = -ENOMEM;
  512. goto fail_free_priv;
  513. }
  514. /* Put FIFO at begginning of XDR buffer */
  515. priv->fifo_base = (u32 *) (priv->xdr_buf + FIFO_OFFSET);
  516. priv->fifo_ptr = priv->fifo_base;
  517. /* XXX: Need to open GPU, in case ps3fb or snd_ps3 aren't loaded */
  518. if (ps3_open_hv_device(dev)) {
  519. dev_err(&dev->core, "ps3_open_hv_device failed\n");
  520. error = -EAGAIN;
  521. goto out_free_xdr_buf;
  522. }
  523. /* Request memory */
  524. status = -1;
  525. ddr_size = ALIGN(memparse(size, &rest), 1024*1024);
  526. if (!ddr_size) {
  527. dev_err(&dev->core, "Specified size is too small\n");
  528. error = -EINVAL;
  529. goto out_close_gpu;
  530. }
  531. while (ddr_size > 0) {
  532. status = lv1_gpu_memory_allocate(ddr_size, 0, 0, 0, 0,
  533. &priv->memory_handle,
  534. &ddr_lpar);
  535. if (!status)
  536. break;
  537. ddr_size -= 1024*1024;
  538. }
  539. if (status) {
  540. dev_err(&dev->core, "lv1_gpu_memory_allocate failed %d\n",
  541. status);
  542. error = -ENOMEM;
  543. goto out_close_gpu;
  544. }
  545. /* Request context */
  546. status = lv1_gpu_context_allocate(priv->memory_handle, 0,
  547. &priv->context_handle, &ctrl_lpar,
  548. &info_lpar, &reports_lpar,
  549. &reports_size);
  550. if (status) {
  551. dev_err(&dev->core, "lv1_gpu_context_allocate failed %d\n",
  552. status);
  553. error = -ENOMEM;
  554. goto out_free_memory;
  555. }
  556. /* Map XDR buffer to RSX */
  557. xdr_lpar = ps3_mm_phys_to_lpar(__pa(priv->xdr_buf));
  558. status = lv1_gpu_context_iomap(priv->context_handle, XDR_IOIF,
  559. xdr_lpar, XDR_BUF_SIZE,
  560. CBE_IOPTE_PP_W | CBE_IOPTE_PP_R |
  561. CBE_IOPTE_M);
  562. if (status) {
  563. dev_err(&dev->core, "lv1_gpu_context_iomap failed %d\n",
  564. status);
  565. error = -ENOMEM;
  566. goto out_free_context;
  567. }
  568. priv->ctrl = ioremap(ctrl_lpar, 64 * 1024);
  569. if (!priv->ctrl) {
  570. dev_err(&dev->core, "ioremap CTRL failed\n");
  571. error = -ENOMEM;
  572. goto out_unmap_context;
  573. }
  574. priv->reports = ioremap(reports_lpar, reports_size);
  575. if (!priv->reports) {
  576. dev_err(&dev->core, "ioremap REPORTS failed\n");
  577. error = -ENOMEM;
  578. goto out_unmap_ctrl;
  579. }
  580. mutex_lock(&ps3_gpu_mutex);
  581. ps3vram_init_ring(dev);
  582. mutex_unlock(&ps3_gpu_mutex);
  583. priv->size = ddr_size;
  584. ps3vram_bind(dev);
  585. mutex_lock(&ps3_gpu_mutex);
  586. error = ps3vram_wait_ring(dev, 100);
  587. mutex_unlock(&ps3_gpu_mutex);
  588. if (error < 0) {
  589. dev_err(&dev->core, "Failed to initialize channels\n");
  590. error = -ETIMEDOUT;
  591. goto out_unmap_reports;
  592. }
  593. ps3vram_cache_init(dev);
  594. ps3vram_proc_init(dev);
  595. queue = blk_alloc_queue(GFP_KERNEL);
  596. if (!queue) {
  597. dev_err(&dev->core, "blk_alloc_queue failed\n");
  598. error = -ENOMEM;
  599. goto out_cache_cleanup;
  600. }
  601. priv->queue = queue;
  602. queue->queuedata = dev;
  603. blk_queue_make_request(queue, ps3vram_make_request);
  604. blk_queue_max_segments(queue, BLK_MAX_SEGMENTS);
  605. blk_queue_max_segment_size(queue, BLK_MAX_SEGMENT_SIZE);
  606. blk_queue_max_hw_sectors(queue, BLK_SAFE_MAX_SECTORS);
  607. gendisk = alloc_disk(1);
  608. if (!gendisk) {
  609. dev_err(&dev->core, "alloc_disk failed\n");
  610. error = -ENOMEM;
  611. goto fail_cleanup_queue;
  612. }
  613. priv->gendisk = gendisk;
  614. gendisk->major = ps3vram_major;
  615. gendisk->first_minor = 0;
  616. gendisk->fops = &ps3vram_fops;
  617. gendisk->queue = queue;
  618. gendisk->private_data = dev;
  619. gendisk->driverfs_dev = &dev->core;
  620. strlcpy(gendisk->disk_name, DEVICE_NAME, sizeof(gendisk->disk_name));
  621. set_capacity(gendisk, priv->size >> 9);
  622. dev_info(&dev->core, "%s: Using %lu MiB of GPU memory\n",
  623. gendisk->disk_name, get_capacity(gendisk) >> 11);
  624. add_disk(gendisk);
  625. return 0;
  626. fail_cleanup_queue:
  627. blk_cleanup_queue(queue);
  628. out_cache_cleanup:
  629. remove_proc_entry(DEVICE_NAME, NULL);
  630. ps3vram_cache_cleanup(dev);
  631. out_unmap_reports:
  632. iounmap(priv->reports);
  633. out_unmap_ctrl:
  634. iounmap(priv->ctrl);
  635. out_unmap_context:
  636. lv1_gpu_context_iomap(priv->context_handle, XDR_IOIF, xdr_lpar,
  637. XDR_BUF_SIZE, CBE_IOPTE_M);
  638. out_free_context:
  639. lv1_gpu_context_free(priv->context_handle);
  640. out_free_memory:
  641. lv1_gpu_memory_free(priv->memory_handle);
  642. out_close_gpu:
  643. ps3_close_hv_device(dev);
  644. out_free_xdr_buf:
  645. free_pages((unsigned long) priv->xdr_buf, get_order(XDR_BUF_SIZE));
  646. fail_free_priv:
  647. kfree(priv);
  648. ps3_system_bus_set_drvdata(dev, NULL);
  649. fail:
  650. return error;
  651. }
  652. static int ps3vram_remove(struct ps3_system_bus_device *dev)
  653. {
  654. struct ps3vram_priv *priv = ps3_system_bus_get_drvdata(dev);
  655. del_gendisk(priv->gendisk);
  656. put_disk(priv->gendisk);
  657. blk_cleanup_queue(priv->queue);
  658. remove_proc_entry(DEVICE_NAME, NULL);
  659. ps3vram_cache_cleanup(dev);
  660. iounmap(priv->reports);
  661. iounmap(priv->ctrl);
  662. lv1_gpu_context_iomap(priv->context_handle, XDR_IOIF,
  663. ps3_mm_phys_to_lpar(__pa(priv->xdr_buf)),
  664. XDR_BUF_SIZE, CBE_IOPTE_M);
  665. lv1_gpu_context_free(priv->context_handle);
  666. lv1_gpu_memory_free(priv->memory_handle);
  667. ps3_close_hv_device(dev);
  668. free_pages((unsigned long) priv->xdr_buf, get_order(XDR_BUF_SIZE));
  669. kfree(priv);
  670. ps3_system_bus_set_drvdata(dev, NULL);
  671. return 0;
  672. }
  673. static struct ps3_system_bus_driver ps3vram = {
  674. .match_id = PS3_MATCH_ID_GPU,
  675. .match_sub_id = PS3_MATCH_SUB_ID_GPU_RAMDISK,
  676. .core.name = DEVICE_NAME,
  677. .core.owner = THIS_MODULE,
  678. .probe = ps3vram_probe,
  679. .remove = ps3vram_remove,
  680. .shutdown = ps3vram_remove,
  681. };
  682. static int __init ps3vram_init(void)
  683. {
  684. int error;
  685. if (!firmware_has_feature(FW_FEATURE_PS3_LV1))
  686. return -ENODEV;
  687. error = register_blkdev(0, DEVICE_NAME);
  688. if (error <= 0) {
  689. pr_err("%s: register_blkdev failed %d\n", DEVICE_NAME, error);
  690. return error;
  691. }
  692. ps3vram_major = error;
  693. pr_info("%s: registered block device major %d\n", DEVICE_NAME,
  694. ps3vram_major);
  695. error = ps3_system_bus_driver_register(&ps3vram);
  696. if (error)
  697. unregister_blkdev(ps3vram_major, DEVICE_NAME);
  698. return error;
  699. }
  700. static void __exit ps3vram_exit(void)
  701. {
  702. ps3_system_bus_driver_unregister(&ps3vram);
  703. unregister_blkdev(ps3vram_major, DEVICE_NAME);
  704. }
  705. module_init(ps3vram_init);
  706. module_exit(ps3vram_exit);
  707. MODULE_LICENSE("GPL");
  708. MODULE_DESCRIPTION("PS3 Video RAM Storage Driver");
  709. MODULE_AUTHOR("Sony Corporation");
  710. MODULE_ALIAS(PS3_MODULE_ALIAS_GPU_RAMDISK);