ps3vram.c 22 KB

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