axonram.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. /*
  2. * (C) Copyright IBM Deutschland Entwicklung GmbH 2006
  3. *
  4. * Author: Maxim Shchetynin <maxim@de.ibm.com>
  5. *
  6. * Axon DDR2 device driver.
  7. * It registers one block device per Axon's DDR2 memory bank found on a system.
  8. * Block devices are called axonram?, their major and minor numbers are
  9. * available in /proc/devices, /proc/partitions or in /sys/block/axonram?/dev.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2, or (at your option)
  14. * any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  24. */
  25. #include <linux/bio.h>
  26. #include <linux/blkdev.h>
  27. #include <linux/dax.h>
  28. #include <linux/device.h>
  29. #include <linux/errno.h>
  30. #include <linux/fs.h>
  31. #include <linux/genhd.h>
  32. #include <linux/interrupt.h>
  33. #include <linux/io.h>
  34. #include <linux/ioport.h>
  35. #include <linux/irq.h>
  36. #include <linux/irqreturn.h>
  37. #include <linux/kernel.h>
  38. #include <linux/mm.h>
  39. #include <linux/mod_devicetable.h>
  40. #include <linux/module.h>
  41. #include <linux/slab.h>
  42. #include <linux/string.h>
  43. #include <linux/types.h>
  44. #include <linux/of_device.h>
  45. #include <linux/of_platform.h>
  46. #include <linux/pfn_t.h>
  47. #include <linux/uio.h>
  48. #include <asm/page.h>
  49. #include <asm/prom.h>
  50. #define AXON_RAM_MODULE_NAME "axonram"
  51. #define AXON_RAM_DEVICE_NAME "axonram"
  52. #define AXON_RAM_MINORS_PER_DISK 16
  53. #define AXON_RAM_BLOCK_SHIFT PAGE_SHIFT
  54. #define AXON_RAM_BLOCK_SIZE 1 << AXON_RAM_BLOCK_SHIFT
  55. #define AXON_RAM_SECTOR_SHIFT 9
  56. #define AXON_RAM_SECTOR_SIZE 1 << AXON_RAM_SECTOR_SHIFT
  57. #define AXON_RAM_IRQ_FLAGS IRQF_SHARED | IRQF_TRIGGER_RISING
  58. static int azfs_major, azfs_minor;
  59. struct axon_ram_bank {
  60. struct platform_device *device;
  61. struct gendisk *disk;
  62. struct dax_device *dax_dev;
  63. unsigned int irq_id;
  64. unsigned long ph_addr;
  65. unsigned long io_addr;
  66. unsigned long size;
  67. unsigned long ecc_counter;
  68. };
  69. static ssize_t
  70. axon_ram_sysfs_ecc(struct device *dev, struct device_attribute *attr, char *buf)
  71. {
  72. struct platform_device *device = to_platform_device(dev);
  73. struct axon_ram_bank *bank = device->dev.platform_data;
  74. BUG_ON(!bank);
  75. return sprintf(buf, "%ld\n", bank->ecc_counter);
  76. }
  77. static DEVICE_ATTR(ecc, S_IRUGO, axon_ram_sysfs_ecc, NULL);
  78. /**
  79. * axon_ram_irq_handler - interrupt handler for Axon RAM ECC
  80. * @irq: interrupt ID
  81. * @dev: pointer to of_device
  82. */
  83. static irqreturn_t
  84. axon_ram_irq_handler(int irq, void *dev)
  85. {
  86. struct platform_device *device = dev;
  87. struct axon_ram_bank *bank = device->dev.platform_data;
  88. BUG_ON(!bank);
  89. dev_err(&device->dev, "Correctable memory error occurred\n");
  90. bank->ecc_counter++;
  91. return IRQ_HANDLED;
  92. }
  93. /**
  94. * axon_ram_make_request - make_request() method for block device
  95. * @queue, @bio: see blk_queue_make_request()
  96. */
  97. static blk_qc_t
  98. axon_ram_make_request(struct request_queue *queue, struct bio *bio)
  99. {
  100. struct axon_ram_bank *bank = bio->bi_bdev->bd_disk->private_data;
  101. unsigned long phys_mem, phys_end;
  102. void *user_mem;
  103. struct bio_vec vec;
  104. unsigned int transfered;
  105. struct bvec_iter iter;
  106. phys_mem = bank->io_addr + (bio->bi_iter.bi_sector <<
  107. AXON_RAM_SECTOR_SHIFT);
  108. phys_end = bank->io_addr + bank->size;
  109. transfered = 0;
  110. bio_for_each_segment(vec, bio, iter) {
  111. if (unlikely(phys_mem + vec.bv_len > phys_end)) {
  112. bio_io_error(bio);
  113. return BLK_QC_T_NONE;
  114. }
  115. user_mem = page_address(vec.bv_page) + vec.bv_offset;
  116. if (bio_data_dir(bio) == READ)
  117. memcpy(user_mem, (void *) phys_mem, vec.bv_len);
  118. else
  119. memcpy((void *) phys_mem, user_mem, vec.bv_len);
  120. phys_mem += vec.bv_len;
  121. transfered += vec.bv_len;
  122. }
  123. bio_endio(bio);
  124. return BLK_QC_T_NONE;
  125. }
  126. static const struct block_device_operations axon_ram_devops = {
  127. .owner = THIS_MODULE,
  128. };
  129. static long
  130. __axon_ram_direct_access(struct axon_ram_bank *bank, pgoff_t pgoff, long nr_pages,
  131. void **kaddr, pfn_t *pfn)
  132. {
  133. resource_size_t offset = pgoff * PAGE_SIZE;
  134. *kaddr = (void *) bank->io_addr + offset;
  135. *pfn = phys_to_pfn_t(bank->ph_addr + offset, PFN_DEV);
  136. return (bank->size - offset) / PAGE_SIZE;
  137. }
  138. static long
  139. axon_ram_dax_direct_access(struct dax_device *dax_dev, pgoff_t pgoff, long nr_pages,
  140. void **kaddr, pfn_t *pfn)
  141. {
  142. struct axon_ram_bank *bank = dax_get_private(dax_dev);
  143. return __axon_ram_direct_access(bank, pgoff, nr_pages, kaddr, pfn);
  144. }
  145. static size_t axon_ram_copy_from_iter(struct dax_device *dax_dev, pgoff_t pgoff,
  146. void *addr, size_t bytes, struct iov_iter *i)
  147. {
  148. return copy_from_iter(addr, bytes, i);
  149. }
  150. static const struct dax_operations axon_ram_dax_ops = {
  151. .direct_access = axon_ram_dax_direct_access,
  152. .copy_from_iter = axon_ram_copy_from_iter,
  153. };
  154. /**
  155. * axon_ram_probe - probe() method for platform driver
  156. * @device: see platform_driver method
  157. */
  158. static int axon_ram_probe(struct platform_device *device)
  159. {
  160. static int axon_ram_bank_id = -1;
  161. struct axon_ram_bank *bank;
  162. struct resource resource;
  163. int rc = 0;
  164. axon_ram_bank_id++;
  165. dev_info(&device->dev, "Found memory controller on %pOF\n",
  166. device->dev.of_node);
  167. bank = kzalloc(sizeof(struct axon_ram_bank), GFP_KERNEL);
  168. if (bank == NULL) {
  169. dev_err(&device->dev, "Out of memory\n");
  170. rc = -ENOMEM;
  171. goto failed;
  172. }
  173. device->dev.platform_data = bank;
  174. bank->device = device;
  175. if (of_address_to_resource(device->dev.of_node, 0, &resource) != 0) {
  176. dev_err(&device->dev, "Cannot access device tree\n");
  177. rc = -EFAULT;
  178. goto failed;
  179. }
  180. bank->size = resource_size(&resource);
  181. if (bank->size == 0) {
  182. dev_err(&device->dev, "No DDR2 memory found for %s%d\n",
  183. AXON_RAM_DEVICE_NAME, axon_ram_bank_id);
  184. rc = -ENODEV;
  185. goto failed;
  186. }
  187. dev_info(&device->dev, "Register DDR2 memory device %s%d with %luMB\n",
  188. AXON_RAM_DEVICE_NAME, axon_ram_bank_id, bank->size >> 20);
  189. bank->ph_addr = resource.start;
  190. bank->io_addr = (unsigned long) ioremap_prot(
  191. bank->ph_addr, bank->size, _PAGE_NO_CACHE);
  192. if (bank->io_addr == 0) {
  193. dev_err(&device->dev, "ioremap() failed\n");
  194. rc = -EFAULT;
  195. goto failed;
  196. }
  197. bank->disk = alloc_disk(AXON_RAM_MINORS_PER_DISK);
  198. if (bank->disk == NULL) {
  199. dev_err(&device->dev, "Cannot register disk\n");
  200. rc = -EFAULT;
  201. goto failed;
  202. }
  203. bank->disk->major = azfs_major;
  204. bank->disk->first_minor = azfs_minor;
  205. bank->disk->fops = &axon_ram_devops;
  206. bank->disk->private_data = bank;
  207. sprintf(bank->disk->disk_name, "%s%d",
  208. AXON_RAM_DEVICE_NAME, axon_ram_bank_id);
  209. bank->dax_dev = alloc_dax(bank, bank->disk->disk_name,
  210. &axon_ram_dax_ops);
  211. if (!bank->dax_dev) {
  212. rc = -ENOMEM;
  213. goto failed;
  214. }
  215. bank->disk->queue = blk_alloc_queue(GFP_KERNEL);
  216. if (bank->disk->queue == NULL) {
  217. dev_err(&device->dev, "Cannot register disk queue\n");
  218. rc = -EFAULT;
  219. goto failed;
  220. }
  221. set_capacity(bank->disk, bank->size >> AXON_RAM_SECTOR_SHIFT);
  222. blk_queue_make_request(bank->disk->queue, axon_ram_make_request);
  223. blk_queue_logical_block_size(bank->disk->queue, AXON_RAM_SECTOR_SIZE);
  224. device_add_disk(&device->dev, bank->disk);
  225. bank->irq_id = irq_of_parse_and_map(device->dev.of_node, 0);
  226. if (!bank->irq_id) {
  227. dev_err(&device->dev, "Cannot access ECC interrupt ID\n");
  228. rc = -EFAULT;
  229. goto failed;
  230. }
  231. rc = request_irq(bank->irq_id, axon_ram_irq_handler,
  232. AXON_RAM_IRQ_FLAGS, bank->disk->disk_name, device);
  233. if (rc != 0) {
  234. dev_err(&device->dev, "Cannot register ECC interrupt handler\n");
  235. bank->irq_id = 0;
  236. rc = -EFAULT;
  237. goto failed;
  238. }
  239. rc = device_create_file(&device->dev, &dev_attr_ecc);
  240. if (rc != 0) {
  241. dev_err(&device->dev, "Cannot create sysfs file\n");
  242. rc = -EFAULT;
  243. goto failed;
  244. }
  245. azfs_minor += bank->disk->minors;
  246. return 0;
  247. failed:
  248. if (bank != NULL) {
  249. if (bank->irq_id)
  250. free_irq(bank->irq_id, device);
  251. if (bank->disk != NULL) {
  252. if (bank->disk->major > 0)
  253. unregister_blkdev(bank->disk->major,
  254. bank->disk->disk_name);
  255. if (bank->disk->flags & GENHD_FL_UP)
  256. del_gendisk(bank->disk);
  257. put_disk(bank->disk);
  258. }
  259. kill_dax(bank->dax_dev);
  260. put_dax(bank->dax_dev);
  261. device->dev.platform_data = NULL;
  262. if (bank->io_addr != 0)
  263. iounmap((void __iomem *) bank->io_addr);
  264. kfree(bank);
  265. }
  266. return rc;
  267. }
  268. /**
  269. * axon_ram_remove - remove() method for platform driver
  270. * @device: see of_platform_driver method
  271. */
  272. static int
  273. axon_ram_remove(struct platform_device *device)
  274. {
  275. struct axon_ram_bank *bank = device->dev.platform_data;
  276. BUG_ON(!bank || !bank->disk);
  277. device_remove_file(&device->dev, &dev_attr_ecc);
  278. free_irq(bank->irq_id, device);
  279. kill_dax(bank->dax_dev);
  280. put_dax(bank->dax_dev);
  281. del_gendisk(bank->disk);
  282. put_disk(bank->disk);
  283. iounmap((void __iomem *) bank->io_addr);
  284. kfree(bank);
  285. return 0;
  286. }
  287. static const struct of_device_id axon_ram_device_id[] = {
  288. {
  289. .type = "dma-memory"
  290. },
  291. {}
  292. };
  293. MODULE_DEVICE_TABLE(of, axon_ram_device_id);
  294. static struct platform_driver axon_ram_driver = {
  295. .probe = axon_ram_probe,
  296. .remove = axon_ram_remove,
  297. .driver = {
  298. .name = AXON_RAM_MODULE_NAME,
  299. .of_match_table = axon_ram_device_id,
  300. },
  301. };
  302. /**
  303. * axon_ram_init
  304. */
  305. static int __init
  306. axon_ram_init(void)
  307. {
  308. azfs_major = register_blkdev(azfs_major, AXON_RAM_DEVICE_NAME);
  309. if (azfs_major < 0) {
  310. printk(KERN_ERR "%s cannot become block device major number\n",
  311. AXON_RAM_MODULE_NAME);
  312. return -EFAULT;
  313. }
  314. azfs_minor = 0;
  315. return platform_driver_register(&axon_ram_driver);
  316. }
  317. /**
  318. * axon_ram_exit
  319. */
  320. static void __exit
  321. axon_ram_exit(void)
  322. {
  323. platform_driver_unregister(&axon_ram_driver);
  324. unregister_blkdev(azfs_major, AXON_RAM_DEVICE_NAME);
  325. }
  326. module_init(axon_ram_init);
  327. module_exit(axon_ram_exit);
  328. MODULE_LICENSE("GPL");
  329. MODULE_AUTHOR("Maxim Shchetynin <maxim@de.ibm.com>");
  330. MODULE_DESCRIPTION("Axon DDR2 RAM device driver for IBM Cell BE");