ps3vram.c 22 KB

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