axonram.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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/device.h>
  28. #include <linux/errno.h>
  29. #include <linux/fs.h>
  30. #include <linux/genhd.h>
  31. #include <linux/interrupt.h>
  32. #include <linux/io.h>
  33. #include <linux/ioport.h>
  34. #include <linux/irq.h>
  35. #include <linux/irqreturn.h>
  36. #include <linux/kernel.h>
  37. #include <linux/mm.h>
  38. #include <linux/mod_devicetable.h>
  39. #include <linux/module.h>
  40. #include <linux/slab.h>
  41. #include <linux/string.h>
  42. #include <linux/types.h>
  43. #include <linux/of_device.h>
  44. #include <linux/of_platform.h>
  45. #include <linux/pfn_t.h>
  46. #include <asm/page.h>
  47. #include <asm/prom.h>
  48. #define AXON_RAM_MODULE_NAME "axonram"
  49. #define AXON_RAM_DEVICE_NAME "axonram"
  50. #define AXON_RAM_MINORS_PER_DISK 16
  51. #define AXON_RAM_BLOCK_SHIFT PAGE_SHIFT
  52. #define AXON_RAM_BLOCK_SIZE 1 << AXON_RAM_BLOCK_SHIFT
  53. #define AXON_RAM_SECTOR_SHIFT 9
  54. #define AXON_RAM_SECTOR_SIZE 1 << AXON_RAM_SECTOR_SHIFT
  55. #define AXON_RAM_IRQ_FLAGS IRQF_SHARED | IRQF_TRIGGER_RISING
  56. static int azfs_major, azfs_minor;
  57. struct axon_ram_bank {
  58. struct platform_device *device;
  59. struct gendisk *disk;
  60. unsigned int irq_id;
  61. unsigned long ph_addr;
  62. unsigned long io_addr;
  63. unsigned long size;
  64. unsigned long ecc_counter;
  65. };
  66. static ssize_t
  67. axon_ram_sysfs_ecc(struct device *dev, struct device_attribute *attr, char *buf)
  68. {
  69. struct platform_device *device = to_platform_device(dev);
  70. struct axon_ram_bank *bank = device->dev.platform_data;
  71. BUG_ON(!bank);
  72. return sprintf(buf, "%ld\n", bank->ecc_counter);
  73. }
  74. static DEVICE_ATTR(ecc, S_IRUGO, axon_ram_sysfs_ecc, NULL);
  75. /**
  76. * axon_ram_irq_handler - interrupt handler for Axon RAM ECC
  77. * @irq: interrupt ID
  78. * @dev: pointer to of_device
  79. */
  80. static irqreturn_t
  81. axon_ram_irq_handler(int irq, void *dev)
  82. {
  83. struct platform_device *device = dev;
  84. struct axon_ram_bank *bank = device->dev.platform_data;
  85. BUG_ON(!bank);
  86. dev_err(&device->dev, "Correctable memory error occurred\n");
  87. bank->ecc_counter++;
  88. return IRQ_HANDLED;
  89. }
  90. /**
  91. * axon_ram_make_request - make_request() method for block device
  92. * @queue, @bio: see blk_queue_make_request()
  93. */
  94. static blk_qc_t
  95. axon_ram_make_request(struct request_queue *queue, struct bio *bio)
  96. {
  97. struct axon_ram_bank *bank = bio->bi_bdev->bd_disk->private_data;
  98. unsigned long phys_mem, phys_end;
  99. void *user_mem;
  100. struct bio_vec vec;
  101. unsigned int transfered;
  102. struct bvec_iter iter;
  103. phys_mem = bank->io_addr + (bio->bi_iter.bi_sector <<
  104. AXON_RAM_SECTOR_SHIFT);
  105. phys_end = bank->io_addr + bank->size;
  106. transfered = 0;
  107. bio_for_each_segment(vec, bio, iter) {
  108. if (unlikely(phys_mem + vec.bv_len > phys_end)) {
  109. bio_io_error(bio);
  110. return BLK_QC_T_NONE;
  111. }
  112. user_mem = page_address(vec.bv_page) + vec.bv_offset;
  113. if (bio_data_dir(bio) == READ)
  114. memcpy(user_mem, (void *) phys_mem, vec.bv_len);
  115. else
  116. memcpy((void *) phys_mem, user_mem, vec.bv_len);
  117. phys_mem += vec.bv_len;
  118. transfered += vec.bv_len;
  119. }
  120. bio_endio(bio);
  121. return BLK_QC_T_NONE;
  122. }
  123. /**
  124. * axon_ram_direct_access - direct_access() method for block device
  125. * @device, @sector, @data: see block_device_operations method
  126. */
  127. static long
  128. axon_ram_direct_access(struct block_device *device, sector_t sector,
  129. void **kaddr, pfn_t *pfn, long size)
  130. {
  131. struct axon_ram_bank *bank = device->bd_disk->private_data;
  132. loff_t offset = (loff_t)sector << AXON_RAM_SECTOR_SHIFT;
  133. *kaddr = (void *) bank->io_addr + offset;
  134. *pfn = phys_to_pfn_t(bank->ph_addr + offset, PFN_DEV);
  135. return bank->size - offset;
  136. }
  137. static const struct block_device_operations axon_ram_devops = {
  138. .owner = THIS_MODULE,
  139. .direct_access = axon_ram_direct_access
  140. };
  141. /**
  142. * axon_ram_probe - probe() method for platform driver
  143. * @device: see platform_driver method
  144. */
  145. static int axon_ram_probe(struct platform_device *device)
  146. {
  147. static int axon_ram_bank_id = -1;
  148. struct axon_ram_bank *bank;
  149. struct resource resource;
  150. int rc = 0;
  151. axon_ram_bank_id++;
  152. dev_info(&device->dev, "Found memory controller on %s\n",
  153. device->dev.of_node->full_name);
  154. bank = kzalloc(sizeof(struct axon_ram_bank), GFP_KERNEL);
  155. if (bank == NULL) {
  156. dev_err(&device->dev, "Out of memory\n");
  157. rc = -ENOMEM;
  158. goto failed;
  159. }
  160. device->dev.platform_data = bank;
  161. bank->device = device;
  162. if (of_address_to_resource(device->dev.of_node, 0, &resource) != 0) {
  163. dev_err(&device->dev, "Cannot access device tree\n");
  164. rc = -EFAULT;
  165. goto failed;
  166. }
  167. bank->size = resource_size(&resource);
  168. if (bank->size == 0) {
  169. dev_err(&device->dev, "No DDR2 memory found for %s%d\n",
  170. AXON_RAM_DEVICE_NAME, axon_ram_bank_id);
  171. rc = -ENODEV;
  172. goto failed;
  173. }
  174. dev_info(&device->dev, "Register DDR2 memory device %s%d with %luMB\n",
  175. AXON_RAM_DEVICE_NAME, axon_ram_bank_id, bank->size >> 20);
  176. bank->ph_addr = resource.start;
  177. bank->io_addr = (unsigned long) ioremap_prot(
  178. bank->ph_addr, bank->size, _PAGE_NO_CACHE);
  179. if (bank->io_addr == 0) {
  180. dev_err(&device->dev, "ioremap() failed\n");
  181. rc = -EFAULT;
  182. goto failed;
  183. }
  184. bank->disk = alloc_disk(AXON_RAM_MINORS_PER_DISK);
  185. if (bank->disk == NULL) {
  186. dev_err(&device->dev, "Cannot register disk\n");
  187. rc = -EFAULT;
  188. goto failed;
  189. }
  190. bank->disk->major = azfs_major;
  191. bank->disk->first_minor = azfs_minor;
  192. bank->disk->fops = &axon_ram_devops;
  193. bank->disk->private_data = bank;
  194. sprintf(bank->disk->disk_name, "%s%d",
  195. AXON_RAM_DEVICE_NAME, axon_ram_bank_id);
  196. bank->disk->queue = blk_alloc_queue(GFP_KERNEL);
  197. if (bank->disk->queue == NULL) {
  198. dev_err(&device->dev, "Cannot register disk queue\n");
  199. rc = -EFAULT;
  200. goto failed;
  201. }
  202. set_capacity(bank->disk, bank->size >> AXON_RAM_SECTOR_SHIFT);
  203. blk_queue_make_request(bank->disk->queue, axon_ram_make_request);
  204. blk_queue_logical_block_size(bank->disk->queue, AXON_RAM_SECTOR_SIZE);
  205. device_add_disk(&device->dev, bank->disk);
  206. bank->irq_id = irq_of_parse_and_map(device->dev.of_node, 0);
  207. if (!bank->irq_id) {
  208. dev_err(&device->dev, "Cannot access ECC interrupt ID\n");
  209. rc = -EFAULT;
  210. goto failed;
  211. }
  212. rc = request_irq(bank->irq_id, axon_ram_irq_handler,
  213. AXON_RAM_IRQ_FLAGS, bank->disk->disk_name, device);
  214. if (rc != 0) {
  215. dev_err(&device->dev, "Cannot register ECC interrupt handler\n");
  216. bank->irq_id = 0;
  217. rc = -EFAULT;
  218. goto failed;
  219. }
  220. rc = device_create_file(&device->dev, &dev_attr_ecc);
  221. if (rc != 0) {
  222. dev_err(&device->dev, "Cannot create sysfs file\n");
  223. rc = -EFAULT;
  224. goto failed;
  225. }
  226. azfs_minor += bank->disk->minors;
  227. return 0;
  228. failed:
  229. if (bank != NULL) {
  230. if (bank->irq_id)
  231. free_irq(bank->irq_id, device);
  232. if (bank->disk != NULL) {
  233. if (bank->disk->major > 0)
  234. unregister_blkdev(bank->disk->major,
  235. bank->disk->disk_name);
  236. if (bank->disk->flags & GENHD_FL_UP)
  237. del_gendisk(bank->disk);
  238. put_disk(bank->disk);
  239. }
  240. device->dev.platform_data = NULL;
  241. if (bank->io_addr != 0)
  242. iounmap((void __iomem *) bank->io_addr);
  243. kfree(bank);
  244. }
  245. return rc;
  246. }
  247. /**
  248. * axon_ram_remove - remove() method for platform driver
  249. * @device: see of_platform_driver method
  250. */
  251. static int
  252. axon_ram_remove(struct platform_device *device)
  253. {
  254. struct axon_ram_bank *bank = device->dev.platform_data;
  255. BUG_ON(!bank || !bank->disk);
  256. device_remove_file(&device->dev, &dev_attr_ecc);
  257. free_irq(bank->irq_id, device);
  258. del_gendisk(bank->disk);
  259. put_disk(bank->disk);
  260. iounmap((void __iomem *) bank->io_addr);
  261. kfree(bank);
  262. return 0;
  263. }
  264. static const struct of_device_id axon_ram_device_id[] = {
  265. {
  266. .type = "dma-memory"
  267. },
  268. {}
  269. };
  270. MODULE_DEVICE_TABLE(of, axon_ram_device_id);
  271. static struct platform_driver axon_ram_driver = {
  272. .probe = axon_ram_probe,
  273. .remove = axon_ram_remove,
  274. .driver = {
  275. .name = AXON_RAM_MODULE_NAME,
  276. .of_match_table = axon_ram_device_id,
  277. },
  278. };
  279. /**
  280. * axon_ram_init
  281. */
  282. static int __init
  283. axon_ram_init(void)
  284. {
  285. azfs_major = register_blkdev(azfs_major, AXON_RAM_DEVICE_NAME);
  286. if (azfs_major < 0) {
  287. printk(KERN_ERR "%s cannot become block device major number\n",
  288. AXON_RAM_MODULE_NAME);
  289. return -EFAULT;
  290. }
  291. azfs_minor = 0;
  292. return platform_driver_register(&axon_ram_driver);
  293. }
  294. /**
  295. * axon_ram_exit
  296. */
  297. static void __exit
  298. axon_ram_exit(void)
  299. {
  300. platform_driver_unregister(&axon_ram_driver);
  301. unregister_blkdev(azfs_major, AXON_RAM_DEVICE_NAME);
  302. }
  303. module_init(axon_ram_init);
  304. module_exit(axon_ram_exit);
  305. MODULE_LICENSE("GPL");
  306. MODULE_AUTHOR("Maxim Shchetynin <maxim@de.ibm.com>");
  307. MODULE_DESCRIPTION("Axon DDR2 RAM device driver for IBM Cell BE");