axonram.c 9.2 KB

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