ibmebus.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. /*
  2. * IBM PowerPC IBM eBus Infrastructure Support.
  3. *
  4. * Copyright (c) 2005 IBM Corporation
  5. * Joachim Fenkes <[email protected]>
  6. * Heiko J Schick <[email protected]>
  7. *
  8. * All rights reserved.
  9. *
  10. * This source code is distributed under a dual license of GPL v2.0 and OpenIB
  11. * BSD.
  12. *
  13. * OpenIB BSD License
  14. *
  15. * Redistribution and use in source and binary forms, with or without
  16. * modification, are permitted provided that the following conditions are met:
  17. *
  18. * Redistributions of source code must retain the above copyright notice, this
  19. * list of conditions and the following disclaimer.
  20. *
  21. * Redistributions in binary form must reproduce the above copyright notice,
  22. * this list of conditions and the following disclaimer in the documentation
  23. * and/or other materials
  24. * provided with the distribution.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  27. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  29. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  30. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  31. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  32. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  33. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  34. * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  35. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  36. * POSSIBILITY OF SUCH DAMAGE.
  37. */
  38. #include <linux/init.h>
  39. #include <linux/export.h>
  40. #include <linux/console.h>
  41. #include <linux/kobject.h>
  42. #include <linux/dma-map-ops.h>
  43. #include <linux/interrupt.h>
  44. #include <linux/irqdomain.h>
  45. #include <linux/of.h>
  46. #include <linux/slab.h>
  47. #include <linux/stat.h>
  48. #include <linux/of_platform.h>
  49. #include <asm/ibmebus.h>
  50. #include <asm/machdep.h>
  51. static struct device ibmebus_bus_device = { /* fake "parent" device */
  52. .init_name = "ibmebus",
  53. };
  54. struct bus_type ibmebus_bus_type;
  55. /* These devices will automatically be added to the bus during init */
  56. static const struct of_device_id ibmebus_matches[] __initconst = {
  57. { .compatible = "IBM,lhca" },
  58. { .compatible = "IBM,lhea" },
  59. {},
  60. };
  61. static void *ibmebus_alloc_coherent(struct device *dev,
  62. size_t size,
  63. dma_addr_t *dma_handle,
  64. gfp_t flag,
  65. unsigned long attrs)
  66. {
  67. void *mem;
  68. mem = kmalloc(size, flag);
  69. *dma_handle = (dma_addr_t)mem;
  70. return mem;
  71. }
  72. static void ibmebus_free_coherent(struct device *dev,
  73. size_t size, void *vaddr,
  74. dma_addr_t dma_handle,
  75. unsigned long attrs)
  76. {
  77. kfree(vaddr);
  78. }
  79. static dma_addr_t ibmebus_map_page(struct device *dev,
  80. struct page *page,
  81. unsigned long offset,
  82. size_t size,
  83. enum dma_data_direction direction,
  84. unsigned long attrs)
  85. {
  86. return (dma_addr_t)(page_address(page) + offset);
  87. }
  88. static void ibmebus_unmap_page(struct device *dev,
  89. dma_addr_t dma_addr,
  90. size_t size,
  91. enum dma_data_direction direction,
  92. unsigned long attrs)
  93. {
  94. return;
  95. }
  96. static int ibmebus_map_sg(struct device *dev,
  97. struct scatterlist *sgl,
  98. int nents, enum dma_data_direction direction,
  99. unsigned long attrs)
  100. {
  101. struct scatterlist *sg;
  102. int i;
  103. for_each_sg(sgl, sg, nents, i) {
  104. sg->dma_address = (dma_addr_t) sg_virt(sg);
  105. sg->dma_length = sg->length;
  106. }
  107. return nents;
  108. }
  109. static void ibmebus_unmap_sg(struct device *dev,
  110. struct scatterlist *sg,
  111. int nents, enum dma_data_direction direction,
  112. unsigned long attrs)
  113. {
  114. return;
  115. }
  116. static int ibmebus_dma_supported(struct device *dev, u64 mask)
  117. {
  118. return mask == DMA_BIT_MASK(64);
  119. }
  120. static u64 ibmebus_dma_get_required_mask(struct device *dev)
  121. {
  122. return DMA_BIT_MASK(64);
  123. }
  124. static const struct dma_map_ops ibmebus_dma_ops = {
  125. .alloc = ibmebus_alloc_coherent,
  126. .free = ibmebus_free_coherent,
  127. .map_sg = ibmebus_map_sg,
  128. .unmap_sg = ibmebus_unmap_sg,
  129. .dma_supported = ibmebus_dma_supported,
  130. .get_required_mask = ibmebus_dma_get_required_mask,
  131. .map_page = ibmebus_map_page,
  132. .unmap_page = ibmebus_unmap_page,
  133. };
  134. static int ibmebus_match_path(struct device *dev, const void *data)
  135. {
  136. struct device_node *dn = to_platform_device(dev)->dev.of_node;
  137. struct device_node *tn = of_find_node_by_path(data);
  138. of_node_put(tn);
  139. return (tn == dn);
  140. }
  141. static int ibmebus_match_node(struct device *dev, const void *data)
  142. {
  143. return to_platform_device(dev)->dev.of_node == data;
  144. }
  145. static int ibmebus_create_device(struct device_node *dn)
  146. {
  147. struct platform_device *dev;
  148. int ret;
  149. dev = of_device_alloc(dn, NULL, &ibmebus_bus_device);
  150. if (!dev)
  151. return -ENOMEM;
  152. dev->dev.bus = &ibmebus_bus_type;
  153. dev->dev.dma_ops = &ibmebus_dma_ops;
  154. ret = of_device_add(dev);
  155. if (ret)
  156. platform_device_put(dev);
  157. return ret;
  158. }
  159. static int ibmebus_create_devices(const struct of_device_id *matches)
  160. {
  161. struct device_node *root, *child;
  162. struct device *dev;
  163. int ret = 0;
  164. root = of_find_node_by_path("/");
  165. for_each_child_of_node(root, child) {
  166. if (!of_match_node(matches, child))
  167. continue;
  168. dev = bus_find_device(&ibmebus_bus_type, NULL, child,
  169. ibmebus_match_node);
  170. if (dev) {
  171. put_device(dev);
  172. continue;
  173. }
  174. ret = ibmebus_create_device(child);
  175. if (ret) {
  176. printk(KERN_ERR "%s: failed to create device (%i)",
  177. __func__, ret);
  178. of_node_put(child);
  179. break;
  180. }
  181. }
  182. of_node_put(root);
  183. return ret;
  184. }
  185. int ibmebus_register_driver(struct platform_driver *drv)
  186. {
  187. /* If the driver uses devices that ibmebus doesn't know, add them */
  188. ibmebus_create_devices(drv->driver.of_match_table);
  189. drv->driver.bus = &ibmebus_bus_type;
  190. return driver_register(&drv->driver);
  191. }
  192. EXPORT_SYMBOL(ibmebus_register_driver);
  193. void ibmebus_unregister_driver(struct platform_driver *drv)
  194. {
  195. driver_unregister(&drv->driver);
  196. }
  197. EXPORT_SYMBOL(ibmebus_unregister_driver);
  198. int ibmebus_request_irq(u32 ist, irq_handler_t handler,
  199. unsigned long irq_flags, const char *devname,
  200. void *dev_id)
  201. {
  202. unsigned int irq = irq_create_mapping(NULL, ist);
  203. if (!irq)
  204. return -EINVAL;
  205. return request_irq(irq, handler, irq_flags, devname, dev_id);
  206. }
  207. EXPORT_SYMBOL(ibmebus_request_irq);
  208. void ibmebus_free_irq(u32 ist, void *dev_id)
  209. {
  210. unsigned int irq = irq_find_mapping(NULL, ist);
  211. free_irq(irq, dev_id);
  212. irq_dispose_mapping(irq);
  213. }
  214. EXPORT_SYMBOL(ibmebus_free_irq);
  215. static char *ibmebus_chomp(const char *in, size_t count)
  216. {
  217. char *out = kmalloc(count + 1, GFP_KERNEL);
  218. if (!out)
  219. return NULL;
  220. memcpy(out, in, count);
  221. out[count] = '\0';
  222. if (out[count - 1] == '\n')
  223. out[count - 1] = '\0';
  224. return out;
  225. }
  226. static ssize_t probe_store(struct bus_type *bus, const char *buf, size_t count)
  227. {
  228. struct device_node *dn = NULL;
  229. struct device *dev;
  230. char *path;
  231. ssize_t rc = 0;
  232. path = ibmebus_chomp(buf, count);
  233. if (!path)
  234. return -ENOMEM;
  235. dev = bus_find_device(&ibmebus_bus_type, NULL, path,
  236. ibmebus_match_path);
  237. if (dev) {
  238. put_device(dev);
  239. printk(KERN_WARNING "%s: %s has already been probed\n",
  240. __func__, path);
  241. rc = -EEXIST;
  242. goto out;
  243. }
  244. if ((dn = of_find_node_by_path(path))) {
  245. rc = ibmebus_create_device(dn);
  246. of_node_put(dn);
  247. } else {
  248. printk(KERN_WARNING "%s: no such device node: %s\n",
  249. __func__, path);
  250. rc = -ENODEV;
  251. }
  252. out:
  253. kfree(path);
  254. if (rc)
  255. return rc;
  256. return count;
  257. }
  258. static BUS_ATTR_WO(probe);
  259. static ssize_t remove_store(struct bus_type *bus, const char *buf, size_t count)
  260. {
  261. struct device *dev;
  262. char *path;
  263. path = ibmebus_chomp(buf, count);
  264. if (!path)
  265. return -ENOMEM;
  266. if ((dev = bus_find_device(&ibmebus_bus_type, NULL, path,
  267. ibmebus_match_path))) {
  268. of_device_unregister(to_platform_device(dev));
  269. put_device(dev);
  270. kfree(path);
  271. return count;
  272. } else {
  273. printk(KERN_WARNING "%s: %s not on the bus\n",
  274. __func__, path);
  275. kfree(path);
  276. return -ENODEV;
  277. }
  278. }
  279. static BUS_ATTR_WO(remove);
  280. static struct attribute *ibmbus_bus_attrs[] = {
  281. &bus_attr_probe.attr,
  282. &bus_attr_remove.attr,
  283. NULL,
  284. };
  285. ATTRIBUTE_GROUPS(ibmbus_bus);
  286. static int ibmebus_bus_bus_match(struct device *dev, struct device_driver *drv)
  287. {
  288. const struct of_device_id *matches = drv->of_match_table;
  289. if (!matches)
  290. return 0;
  291. return of_match_device(matches, dev) != NULL;
  292. }
  293. static int ibmebus_bus_device_probe(struct device *dev)
  294. {
  295. int error = -ENODEV;
  296. struct platform_driver *drv;
  297. struct platform_device *of_dev;
  298. drv = to_platform_driver(dev->driver);
  299. of_dev = to_platform_device(dev);
  300. if (!drv->probe)
  301. return error;
  302. get_device(dev);
  303. if (of_driver_match_device(dev, dev->driver))
  304. error = drv->probe(of_dev);
  305. if (error)
  306. put_device(dev);
  307. return error;
  308. }
  309. static void ibmebus_bus_device_remove(struct device *dev)
  310. {
  311. struct platform_device *of_dev = to_platform_device(dev);
  312. struct platform_driver *drv = to_platform_driver(dev->driver);
  313. if (dev->driver && drv->remove)
  314. drv->remove(of_dev);
  315. }
  316. static void ibmebus_bus_device_shutdown(struct device *dev)
  317. {
  318. struct platform_device *of_dev = to_platform_device(dev);
  319. struct platform_driver *drv = to_platform_driver(dev->driver);
  320. if (dev->driver && drv->shutdown)
  321. drv->shutdown(of_dev);
  322. }
  323. /*
  324. * ibmebus_bus_device_attrs
  325. */
  326. static ssize_t devspec_show(struct device *dev,
  327. struct device_attribute *attr, char *buf)
  328. {
  329. struct platform_device *ofdev;
  330. ofdev = to_platform_device(dev);
  331. return sprintf(buf, "%pOF\n", ofdev->dev.of_node);
  332. }
  333. static DEVICE_ATTR_RO(devspec);
  334. static ssize_t name_show(struct device *dev,
  335. struct device_attribute *attr, char *buf)
  336. {
  337. struct platform_device *ofdev;
  338. ofdev = to_platform_device(dev);
  339. return sprintf(buf, "%pOFn\n", ofdev->dev.of_node);
  340. }
  341. static DEVICE_ATTR_RO(name);
  342. static ssize_t modalias_show(struct device *dev,
  343. struct device_attribute *attr, char *buf)
  344. {
  345. return of_device_modalias(dev, buf, PAGE_SIZE);
  346. }
  347. static DEVICE_ATTR_RO(modalias);
  348. static struct attribute *ibmebus_bus_device_attrs[] = {
  349. &dev_attr_devspec.attr,
  350. &dev_attr_name.attr,
  351. &dev_attr_modalias.attr,
  352. NULL,
  353. };
  354. ATTRIBUTE_GROUPS(ibmebus_bus_device);
  355. struct bus_type ibmebus_bus_type = {
  356. .name = "ibmebus",
  357. .uevent = of_device_uevent_modalias,
  358. .bus_groups = ibmbus_bus_groups,
  359. .match = ibmebus_bus_bus_match,
  360. .probe = ibmebus_bus_device_probe,
  361. .remove = ibmebus_bus_device_remove,
  362. .shutdown = ibmebus_bus_device_shutdown,
  363. .dev_groups = ibmebus_bus_device_groups,
  364. };
  365. EXPORT_SYMBOL(ibmebus_bus_type);
  366. static int __init ibmebus_bus_init(void)
  367. {
  368. int err;
  369. printk(KERN_INFO "IBM eBus Device Driver\n");
  370. err = bus_register(&ibmebus_bus_type);
  371. if (err) {
  372. printk(KERN_ERR "%s: failed to register IBM eBus.\n",
  373. __func__);
  374. return err;
  375. }
  376. err = device_register(&ibmebus_bus_device);
  377. if (err) {
  378. printk(KERN_WARNING "%s: device_register returned %i\n",
  379. __func__, err);
  380. put_device(&ibmebus_bus_device);
  381. bus_unregister(&ibmebus_bus_type);
  382. return err;
  383. }
  384. err = ibmebus_create_devices(ibmebus_matches);
  385. if (err) {
  386. device_unregister(&ibmebus_bus_device);
  387. bus_unregister(&ibmebus_bus_type);
  388. return err;
  389. }
  390. return 0;
  391. }
  392. machine_postcore_initcall(pseries, ibmebus_bus_init);