system-bus.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /*
  2. * PS3 system bus driver.
  3. *
  4. * Copyright (C) 2006 Sony Computer Entertainment Inc.
  5. * Copyright 2006 Sony Corp.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; version 2 of the License.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #include <linux/kernel.h>
  21. #include <linux/init.h>
  22. #include <linux/module.h>
  23. #include <linux/dma-mapping.h>
  24. #include <linux/err.h>
  25. #include <asm/udbg.h>
  26. #include <asm/lv1call.h>
  27. #include <asm/firmware.h>
  28. #include "platform.h"
  29. #define dump_mmio_region(_a) _dump_mmio_region(_a, __func__, __LINE__)
  30. static void _dump_mmio_region(const struct ps3_mmio_region* r,
  31. const char* func, int line)
  32. {
  33. pr_debug("%s:%d: dev %u:%u\n", func, line, r->did.bus_id,
  34. r->did.dev_id);
  35. pr_debug("%s:%d: bus_addr %lxh\n", func, line, r->bus_addr);
  36. pr_debug("%s:%d: len %lxh\n", func, line, r->len);
  37. pr_debug("%s:%d: lpar_addr %lxh\n", func, line, r->lpar_addr);
  38. }
  39. int ps3_mmio_region_create(struct ps3_mmio_region *r)
  40. {
  41. int result;
  42. result = lv1_map_device_mmio_region(r->did.bus_id, r->did.dev_id,
  43. r->bus_addr, r->len, r->page_size, &r->lpar_addr);
  44. if (result) {
  45. pr_debug("%s:%d: lv1_map_device_mmio_region failed: %s\n",
  46. __func__, __LINE__, ps3_result(result));
  47. r->lpar_addr = 0;
  48. }
  49. dump_mmio_region(r);
  50. return result;
  51. }
  52. EXPORT_SYMBOL_GPL(ps3_mmio_region_create);
  53. int ps3_free_mmio_region(struct ps3_mmio_region *r)
  54. {
  55. int result;
  56. result = lv1_unmap_device_mmio_region(r->did.bus_id, r->did.dev_id,
  57. r->lpar_addr);
  58. if (result)
  59. pr_debug("%s:%d: lv1_unmap_device_mmio_region failed: %s\n",
  60. __func__, __LINE__, ps3_result(result));
  61. r->lpar_addr = 0;
  62. return result;
  63. }
  64. EXPORT_SYMBOL_GPL(ps3_free_mmio_region);
  65. static int ps3_system_bus_match(struct device *_dev,
  66. struct device_driver *_drv)
  67. {
  68. int result;
  69. struct ps3_system_bus_driver *drv = to_ps3_system_bus_driver(_drv);
  70. struct ps3_system_bus_device *dev = to_ps3_system_bus_device(_dev);
  71. result = dev->match_id == drv->match_id;
  72. pr_info("%s:%d: dev=%u(%s), drv=%u(%s): %s\n", __func__, __LINE__,
  73. dev->match_id, dev->core.bus_id, drv->match_id, drv->core.name,
  74. (result ? "match" : "miss"));
  75. return result;
  76. }
  77. static int ps3_system_bus_probe(struct device *_dev)
  78. {
  79. int result;
  80. struct ps3_system_bus_device *dev = to_ps3_system_bus_device(_dev);
  81. struct ps3_system_bus_driver *drv =
  82. to_ps3_system_bus_driver(_dev->driver);
  83. result = lv1_open_device(dev->did.bus_id, dev->did.dev_id, 0);
  84. if (result) {
  85. pr_debug("%s:%d: lv1_open_device failed (%d)\n",
  86. __func__, __LINE__, result);
  87. result = -EACCES;
  88. goto clean_none;
  89. }
  90. if (dev->d_region->did.bus_id) {
  91. result = ps3_dma_region_create(dev->d_region);
  92. if (result) {
  93. pr_debug("%s:%d: ps3_dma_region_create failed (%d)\n",
  94. __func__, __LINE__, result);
  95. BUG_ON("check region type");
  96. result = -EINVAL;
  97. goto clean_device;
  98. }
  99. }
  100. BUG_ON(!drv);
  101. if (drv->probe)
  102. result = drv->probe(dev);
  103. else
  104. pr_info("%s:%d: %s no probe method\n", __func__, __LINE__,
  105. dev->core.bus_id);
  106. if (result) {
  107. pr_debug("%s:%d: drv->probe failed\n", __func__, __LINE__);
  108. goto clean_dma;
  109. }
  110. return result;
  111. clean_dma:
  112. ps3_dma_region_free(dev->d_region);
  113. clean_device:
  114. lv1_close_device(dev->did.bus_id, dev->did.dev_id);
  115. clean_none:
  116. return result;
  117. }
  118. static int ps3_system_bus_remove(struct device *_dev)
  119. {
  120. struct ps3_system_bus_device *dev = to_ps3_system_bus_device(_dev);
  121. struct ps3_system_bus_driver *drv =
  122. to_ps3_system_bus_driver(_dev->driver);
  123. if (drv->remove)
  124. drv->remove(dev);
  125. else
  126. pr_info("%s:%d: %s no remove method\n", __func__, __LINE__,
  127. dev->core.bus_id);
  128. ps3_dma_region_free(dev->d_region);
  129. ps3_free_mmio_region(dev->m_region);
  130. lv1_close_device(dev->did.bus_id, dev->did.dev_id);
  131. return 0;
  132. }
  133. struct bus_type ps3_system_bus_type = {
  134. .name = "ps3_system_bus",
  135. .match = ps3_system_bus_match,
  136. .probe = ps3_system_bus_probe,
  137. .remove = ps3_system_bus_remove,
  138. };
  139. int __init ps3_system_bus_init(void)
  140. {
  141. int result;
  142. if (!firmware_has_feature(FW_FEATURE_PS3_LV1))
  143. return -ENODEV;
  144. result = bus_register(&ps3_system_bus_type);
  145. BUG_ON(result);
  146. return result;
  147. }
  148. core_initcall(ps3_system_bus_init);
  149. /* Allocates a contiguous real buffer and creates mappings over it.
  150. * Returns the virtual address of the buffer and sets dma_handle
  151. * to the dma address (mapping) of the first page.
  152. */
  153. static void * ps3_alloc_coherent(struct device *_dev, size_t size,
  154. dma_addr_t *dma_handle, gfp_t flag)
  155. {
  156. int result;
  157. struct ps3_system_bus_device *dev = to_ps3_system_bus_device(_dev);
  158. unsigned long virt_addr;
  159. BUG_ON(!dev->d_region->bus_addr);
  160. flag &= ~(__GFP_DMA | __GFP_HIGHMEM);
  161. flag |= __GFP_ZERO;
  162. virt_addr = __get_free_pages(flag, get_order(size));
  163. if (!virt_addr) {
  164. pr_debug("%s:%d: get_free_pages failed\n", __func__, __LINE__);
  165. goto clean_none;
  166. }
  167. result = ps3_dma_map(dev->d_region, virt_addr, size, dma_handle);
  168. if (result) {
  169. pr_debug("%s:%d: ps3_dma_map failed (%d)\n",
  170. __func__, __LINE__, result);
  171. BUG_ON("check region type");
  172. goto clean_alloc;
  173. }
  174. return (void*)virt_addr;
  175. clean_alloc:
  176. free_pages(virt_addr, get_order(size));
  177. clean_none:
  178. dma_handle = NULL;
  179. return NULL;
  180. }
  181. static void ps3_free_coherent(struct device *_dev, size_t size, void *vaddr,
  182. dma_addr_t dma_handle)
  183. {
  184. struct ps3_system_bus_device *dev = to_ps3_system_bus_device(_dev);
  185. ps3_dma_unmap(dev->d_region, dma_handle, size);
  186. free_pages((unsigned long)vaddr, get_order(size));
  187. }
  188. /* Creates TCEs for a user provided buffer. The user buffer must be
  189. * contiguous real kernel storage (not vmalloc). The address of the buffer
  190. * passed here is the kernel (virtual) address of the buffer. The buffer
  191. * need not be page aligned, the dma_addr_t returned will point to the same
  192. * byte within the page as vaddr.
  193. */
  194. static dma_addr_t ps3_map_single(struct device *_dev, void *ptr, size_t size,
  195. enum dma_data_direction direction)
  196. {
  197. struct ps3_system_bus_device *dev = to_ps3_system_bus_device(_dev);
  198. int result;
  199. unsigned long bus_addr;
  200. result = ps3_dma_map(dev->d_region, (unsigned long)ptr, size,
  201. &bus_addr);
  202. if (result) {
  203. pr_debug("%s:%d: ps3_dma_map failed (%d)\n",
  204. __func__, __LINE__, result);
  205. }
  206. return bus_addr;
  207. }
  208. static void ps3_unmap_single(struct device *_dev, dma_addr_t dma_addr,
  209. size_t size, enum dma_data_direction direction)
  210. {
  211. struct ps3_system_bus_device *dev = to_ps3_system_bus_device(_dev);
  212. int result;
  213. result = ps3_dma_unmap(dev->d_region, dma_addr, size);
  214. if (result) {
  215. pr_debug("%s:%d: ps3_dma_unmap failed (%d)\n",
  216. __func__, __LINE__, result);
  217. }
  218. }
  219. static int ps3_map_sg(struct device *_dev, struct scatterlist *sg, int nents,
  220. enum dma_data_direction direction)
  221. {
  222. #if defined(CONFIG_PS3_DYNAMIC_DMA)
  223. BUG_ON("do");
  224. return -EPERM;
  225. #else
  226. struct ps3_system_bus_device *dev = to_ps3_system_bus_device(_dev);
  227. int i;
  228. for (i = 0; i < nents; i++, sg++) {
  229. int result = ps3_dma_map(dev->d_region,
  230. page_to_phys(sg->page) + sg->offset, sg->length,
  231. &sg->dma_address);
  232. if (result) {
  233. pr_debug("%s:%d: ps3_dma_map failed (%d)\n",
  234. __func__, __LINE__, result);
  235. return -EINVAL;
  236. }
  237. sg->dma_length = sg->length;
  238. }
  239. return nents;
  240. #endif
  241. }
  242. static void ps3_unmap_sg(struct device *_dev, struct scatterlist *sg,
  243. int nents, enum dma_data_direction direction)
  244. {
  245. #if defined(CONFIG_PS3_DYNAMIC_DMA)
  246. BUG_ON("do");
  247. #endif
  248. }
  249. static int ps3_dma_supported(struct device *_dev, u64 mask)
  250. {
  251. return mask >= DMA_32BIT_MASK;
  252. }
  253. static struct dma_mapping_ops ps3_dma_ops = {
  254. .alloc_coherent = ps3_alloc_coherent,
  255. .free_coherent = ps3_free_coherent,
  256. .map_single = ps3_map_single,
  257. .unmap_single = ps3_unmap_single,
  258. .map_sg = ps3_map_sg,
  259. .unmap_sg = ps3_unmap_sg,
  260. .dma_supported = ps3_dma_supported
  261. };
  262. /**
  263. * ps3_system_bus_release_device - remove a device from the system bus
  264. */
  265. static void ps3_system_bus_release_device(struct device *_dev)
  266. {
  267. struct ps3_system_bus_device *dev = to_ps3_system_bus_device(_dev);
  268. kfree(dev);
  269. }
  270. /**
  271. * ps3_system_bus_device_register - add a device to the system bus
  272. *
  273. * ps3_system_bus_device_register() expects the dev object to be allocated
  274. * dynamically by the caller. The system bus takes ownership of the dev
  275. * object and frees the object in ps3_system_bus_release_device().
  276. */
  277. int ps3_system_bus_device_register(struct ps3_system_bus_device *dev)
  278. {
  279. int result;
  280. static unsigned int dev_count = 1;
  281. dev->core.parent = NULL;
  282. dev->core.bus = &ps3_system_bus_type;
  283. dev->core.release = ps3_system_bus_release_device;
  284. dev->core.archdata.of_node = NULL;
  285. dev->core.archdata.dma_ops = &ps3_dma_ops;
  286. dev->core.archdata.numa_node = 0;
  287. snprintf(dev->core.bus_id, sizeof(dev->core.bus_id), "sb_%02x",
  288. dev_count++);
  289. pr_debug("%s:%d add %s\n", __func__, __LINE__, dev->core.bus_id);
  290. result = device_register(&dev->core);
  291. return result;
  292. }
  293. EXPORT_SYMBOL_GPL(ps3_system_bus_device_register);
  294. int ps3_system_bus_driver_register(struct ps3_system_bus_driver *drv)
  295. {
  296. int result;
  297. drv->core.bus = &ps3_system_bus_type;
  298. result = driver_register(&drv->core);
  299. return result;
  300. }
  301. EXPORT_SYMBOL_GPL(ps3_system_bus_driver_register);
  302. void ps3_system_bus_driver_unregister(struct ps3_system_bus_driver *drv)
  303. {
  304. driver_unregister(&drv->core);
  305. }
  306. EXPORT_SYMBOL_GPL(ps3_system_bus_driver_unregister);