system-bus.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805
  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 <linux/slab.h>
  26. #include <asm/udbg.h>
  27. #include <asm/lv1call.h>
  28. #include <asm/firmware.h>
  29. #include <asm/cell-regs.h>
  30. #include "platform.h"
  31. static struct device ps3_system_bus = {
  32. .init_name = "ps3_system",
  33. };
  34. /* FIXME: need device usage counters! */
  35. struct {
  36. struct mutex mutex;
  37. int sb_11; /* usb 0 */
  38. int sb_12; /* usb 0 */
  39. int gpu;
  40. } static usage_hack;
  41. static int ps3_is_device(struct ps3_system_bus_device *dev, u64 bus_id,
  42. u64 dev_id)
  43. {
  44. return dev->bus_id == bus_id && dev->dev_id == dev_id;
  45. }
  46. static int ps3_open_hv_device_sb(struct ps3_system_bus_device *dev)
  47. {
  48. int result;
  49. BUG_ON(!dev->bus_id);
  50. mutex_lock(&usage_hack.mutex);
  51. if (ps3_is_device(dev, 1, 1)) {
  52. usage_hack.sb_11++;
  53. if (usage_hack.sb_11 > 1) {
  54. result = 0;
  55. goto done;
  56. }
  57. }
  58. if (ps3_is_device(dev, 1, 2)) {
  59. usage_hack.sb_12++;
  60. if (usage_hack.sb_12 > 1) {
  61. result = 0;
  62. goto done;
  63. }
  64. }
  65. result = lv1_open_device(dev->bus_id, dev->dev_id, 0);
  66. if (result) {
  67. pr_debug("%s:%d: lv1_open_device failed: %s\n", __func__,
  68. __LINE__, ps3_result(result));
  69. result = -EPERM;
  70. }
  71. done:
  72. mutex_unlock(&usage_hack.mutex);
  73. return result;
  74. }
  75. static int ps3_close_hv_device_sb(struct ps3_system_bus_device *dev)
  76. {
  77. int result;
  78. BUG_ON(!dev->bus_id);
  79. mutex_lock(&usage_hack.mutex);
  80. if (ps3_is_device(dev, 1, 1)) {
  81. usage_hack.sb_11--;
  82. if (usage_hack.sb_11) {
  83. result = 0;
  84. goto done;
  85. }
  86. }
  87. if (ps3_is_device(dev, 1, 2)) {
  88. usage_hack.sb_12--;
  89. if (usage_hack.sb_12) {
  90. result = 0;
  91. goto done;
  92. }
  93. }
  94. result = lv1_close_device(dev->bus_id, dev->dev_id);
  95. BUG_ON(result);
  96. done:
  97. mutex_unlock(&usage_hack.mutex);
  98. return result;
  99. }
  100. static int ps3_open_hv_device_gpu(struct ps3_system_bus_device *dev)
  101. {
  102. int result;
  103. mutex_lock(&usage_hack.mutex);
  104. usage_hack.gpu++;
  105. if (usage_hack.gpu > 1) {
  106. result = 0;
  107. goto done;
  108. }
  109. result = lv1_gpu_open(0);
  110. if (result) {
  111. pr_debug("%s:%d: lv1_gpu_open failed: %s\n", __func__,
  112. __LINE__, ps3_result(result));
  113. result = -EPERM;
  114. }
  115. done:
  116. mutex_unlock(&usage_hack.mutex);
  117. return result;
  118. }
  119. static int ps3_close_hv_device_gpu(struct ps3_system_bus_device *dev)
  120. {
  121. int result;
  122. mutex_lock(&usage_hack.mutex);
  123. usage_hack.gpu--;
  124. if (usage_hack.gpu) {
  125. result = 0;
  126. goto done;
  127. }
  128. result = lv1_gpu_close();
  129. BUG_ON(result);
  130. done:
  131. mutex_unlock(&usage_hack.mutex);
  132. return result;
  133. }
  134. int ps3_open_hv_device(struct ps3_system_bus_device *dev)
  135. {
  136. BUG_ON(!dev);
  137. pr_debug("%s:%d: match_id: %u\n", __func__, __LINE__, dev->match_id);
  138. switch (dev->match_id) {
  139. case PS3_MATCH_ID_EHCI:
  140. case PS3_MATCH_ID_OHCI:
  141. case PS3_MATCH_ID_GELIC:
  142. case PS3_MATCH_ID_STOR_DISK:
  143. case PS3_MATCH_ID_STOR_ROM:
  144. case PS3_MATCH_ID_STOR_FLASH:
  145. return ps3_open_hv_device_sb(dev);
  146. case PS3_MATCH_ID_SOUND:
  147. case PS3_MATCH_ID_GPU:
  148. return ps3_open_hv_device_gpu(dev);
  149. case PS3_MATCH_ID_AV_SETTINGS:
  150. case PS3_MATCH_ID_SYSTEM_MANAGER:
  151. pr_debug("%s:%d: unsupported match_id: %u\n", __func__,
  152. __LINE__, dev->match_id);
  153. pr_debug("%s:%d: bus_id: %llu\n", __func__, __LINE__,
  154. dev->bus_id);
  155. BUG();
  156. return -EINVAL;
  157. default:
  158. break;
  159. }
  160. pr_debug("%s:%d: unknown match_id: %u\n", __func__, __LINE__,
  161. dev->match_id);
  162. BUG();
  163. return -ENODEV;
  164. }
  165. EXPORT_SYMBOL_GPL(ps3_open_hv_device);
  166. int ps3_close_hv_device(struct ps3_system_bus_device *dev)
  167. {
  168. BUG_ON(!dev);
  169. pr_debug("%s:%d: match_id: %u\n", __func__, __LINE__, dev->match_id);
  170. switch (dev->match_id) {
  171. case PS3_MATCH_ID_EHCI:
  172. case PS3_MATCH_ID_OHCI:
  173. case PS3_MATCH_ID_GELIC:
  174. case PS3_MATCH_ID_STOR_DISK:
  175. case PS3_MATCH_ID_STOR_ROM:
  176. case PS3_MATCH_ID_STOR_FLASH:
  177. return ps3_close_hv_device_sb(dev);
  178. case PS3_MATCH_ID_SOUND:
  179. case PS3_MATCH_ID_GPU:
  180. return ps3_close_hv_device_gpu(dev);
  181. case PS3_MATCH_ID_AV_SETTINGS:
  182. case PS3_MATCH_ID_SYSTEM_MANAGER:
  183. pr_debug("%s:%d: unsupported match_id: %u\n", __func__,
  184. __LINE__, dev->match_id);
  185. pr_debug("%s:%d: bus_id: %llu\n", __func__, __LINE__,
  186. dev->bus_id);
  187. BUG();
  188. return -EINVAL;
  189. default:
  190. break;
  191. }
  192. pr_debug("%s:%d: unknown match_id: %u\n", __func__, __LINE__,
  193. dev->match_id);
  194. BUG();
  195. return -ENODEV;
  196. }
  197. EXPORT_SYMBOL_GPL(ps3_close_hv_device);
  198. #define dump_mmio_region(_a) _dump_mmio_region(_a, __func__, __LINE__)
  199. static void _dump_mmio_region(const struct ps3_mmio_region* r,
  200. const char* func, int line)
  201. {
  202. pr_debug("%s:%d: dev %llu:%llu\n", func, line, r->dev->bus_id,
  203. r->dev->dev_id);
  204. pr_debug("%s:%d: bus_addr %lxh\n", func, line, r->bus_addr);
  205. pr_debug("%s:%d: len %lxh\n", func, line, r->len);
  206. pr_debug("%s:%d: lpar_addr %lxh\n", func, line, r->lpar_addr);
  207. }
  208. static int ps3_sb_mmio_region_create(struct ps3_mmio_region *r)
  209. {
  210. int result;
  211. u64 lpar_addr;
  212. result = lv1_map_device_mmio_region(r->dev->bus_id, r->dev->dev_id,
  213. r->bus_addr, r->len, r->page_size, &lpar_addr);
  214. r->lpar_addr = lpar_addr;
  215. if (result) {
  216. pr_debug("%s:%d: lv1_map_device_mmio_region failed: %s\n",
  217. __func__, __LINE__, ps3_result(result));
  218. r->lpar_addr = 0;
  219. }
  220. dump_mmio_region(r);
  221. return result;
  222. }
  223. static int ps3_ioc0_mmio_region_create(struct ps3_mmio_region *r)
  224. {
  225. /* device specific; do nothing currently */
  226. return 0;
  227. }
  228. int ps3_mmio_region_create(struct ps3_mmio_region *r)
  229. {
  230. return r->mmio_ops->create(r);
  231. }
  232. EXPORT_SYMBOL_GPL(ps3_mmio_region_create);
  233. static int ps3_sb_free_mmio_region(struct ps3_mmio_region *r)
  234. {
  235. int result;
  236. dump_mmio_region(r);
  237. result = lv1_unmap_device_mmio_region(r->dev->bus_id, r->dev->dev_id,
  238. r->lpar_addr);
  239. if (result)
  240. pr_debug("%s:%d: lv1_unmap_device_mmio_region failed: %s\n",
  241. __func__, __LINE__, ps3_result(result));
  242. r->lpar_addr = 0;
  243. return result;
  244. }
  245. static int ps3_ioc0_free_mmio_region(struct ps3_mmio_region *r)
  246. {
  247. /* device specific; do nothing currently */
  248. return 0;
  249. }
  250. int ps3_free_mmio_region(struct ps3_mmio_region *r)
  251. {
  252. return r->mmio_ops->free(r);
  253. }
  254. EXPORT_SYMBOL_GPL(ps3_free_mmio_region);
  255. static const struct ps3_mmio_region_ops ps3_mmio_sb_region_ops = {
  256. .create = ps3_sb_mmio_region_create,
  257. .free = ps3_sb_free_mmio_region
  258. };
  259. static const struct ps3_mmio_region_ops ps3_mmio_ioc0_region_ops = {
  260. .create = ps3_ioc0_mmio_region_create,
  261. .free = ps3_ioc0_free_mmio_region
  262. };
  263. int ps3_mmio_region_init(struct ps3_system_bus_device *dev,
  264. struct ps3_mmio_region *r, unsigned long bus_addr, unsigned long len,
  265. enum ps3_mmio_page_size page_size)
  266. {
  267. r->dev = dev;
  268. r->bus_addr = bus_addr;
  269. r->len = len;
  270. r->page_size = page_size;
  271. switch (dev->dev_type) {
  272. case PS3_DEVICE_TYPE_SB:
  273. r->mmio_ops = &ps3_mmio_sb_region_ops;
  274. break;
  275. case PS3_DEVICE_TYPE_IOC0:
  276. r->mmio_ops = &ps3_mmio_ioc0_region_ops;
  277. break;
  278. default:
  279. BUG();
  280. return -EINVAL;
  281. }
  282. return 0;
  283. }
  284. EXPORT_SYMBOL_GPL(ps3_mmio_region_init);
  285. static int ps3_system_bus_match(struct device *_dev,
  286. struct device_driver *_drv)
  287. {
  288. int result;
  289. struct ps3_system_bus_driver *drv = ps3_drv_to_system_bus_drv(_drv);
  290. struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
  291. if (!dev->match_sub_id)
  292. result = dev->match_id == drv->match_id;
  293. else
  294. result = dev->match_sub_id == drv->match_sub_id &&
  295. dev->match_id == drv->match_id;
  296. if (result)
  297. pr_info("%s:%d: dev=%u.%u(%s), drv=%u.%u(%s): match\n",
  298. __func__, __LINE__,
  299. dev->match_id, dev->match_sub_id, dev_name(&dev->core),
  300. drv->match_id, drv->match_sub_id, drv->core.name);
  301. else
  302. pr_debug("%s:%d: dev=%u.%u(%s), drv=%u.%u(%s): miss\n",
  303. __func__, __LINE__,
  304. dev->match_id, dev->match_sub_id, dev_name(&dev->core),
  305. drv->match_id, drv->match_sub_id, drv->core.name);
  306. return result;
  307. }
  308. static int ps3_system_bus_probe(struct device *_dev)
  309. {
  310. int result = 0;
  311. struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
  312. struct ps3_system_bus_driver *drv;
  313. BUG_ON(!dev);
  314. dev_dbg(_dev, "%s:%d\n", __func__, __LINE__);
  315. drv = ps3_system_bus_dev_to_system_bus_drv(dev);
  316. BUG_ON(!drv);
  317. if (drv->probe)
  318. result = drv->probe(dev);
  319. else
  320. pr_debug("%s:%d: %s no probe method\n", __func__, __LINE__,
  321. dev_name(&dev->core));
  322. pr_debug(" <- %s:%d: %s\n", __func__, __LINE__, dev_name(&dev->core));
  323. return result;
  324. }
  325. static int ps3_system_bus_remove(struct device *_dev)
  326. {
  327. int result = 0;
  328. struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
  329. struct ps3_system_bus_driver *drv;
  330. BUG_ON(!dev);
  331. dev_dbg(_dev, "%s:%d\n", __func__, __LINE__);
  332. drv = ps3_system_bus_dev_to_system_bus_drv(dev);
  333. BUG_ON(!drv);
  334. if (drv->remove)
  335. result = drv->remove(dev);
  336. else
  337. dev_dbg(&dev->core, "%s:%d %s: no remove method\n",
  338. __func__, __LINE__, drv->core.name);
  339. pr_debug(" <- %s:%d: %s\n", __func__, __LINE__, dev_name(&dev->core));
  340. return result;
  341. }
  342. static void ps3_system_bus_shutdown(struct device *_dev)
  343. {
  344. struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
  345. struct ps3_system_bus_driver *drv;
  346. BUG_ON(!dev);
  347. dev_dbg(&dev->core, " -> %s:%d: match_id %d\n", __func__, __LINE__,
  348. dev->match_id);
  349. if (!dev->core.driver) {
  350. dev_dbg(&dev->core, "%s:%d: no driver bound\n", __func__,
  351. __LINE__);
  352. return;
  353. }
  354. drv = ps3_system_bus_dev_to_system_bus_drv(dev);
  355. BUG_ON(!drv);
  356. dev_dbg(&dev->core, "%s:%d: %s -> %s\n", __func__, __LINE__,
  357. dev_name(&dev->core), drv->core.name);
  358. if (drv->shutdown)
  359. drv->shutdown(dev);
  360. else if (drv->remove) {
  361. dev_dbg(&dev->core, "%s:%d %s: no shutdown, calling remove\n",
  362. __func__, __LINE__, drv->core.name);
  363. drv->remove(dev);
  364. } else {
  365. dev_dbg(&dev->core, "%s:%d %s: no shutdown method\n",
  366. __func__, __LINE__, drv->core.name);
  367. BUG();
  368. }
  369. dev_dbg(&dev->core, " <- %s:%d\n", __func__, __LINE__);
  370. }
  371. static int ps3_system_bus_uevent(struct device *_dev, struct kobj_uevent_env *env)
  372. {
  373. struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
  374. if (add_uevent_var(env, "MODALIAS=ps3:%d:%d", dev->match_id,
  375. dev->match_sub_id))
  376. return -ENOMEM;
  377. return 0;
  378. }
  379. static ssize_t modalias_show(struct device *_dev, struct device_attribute *a,
  380. char *buf)
  381. {
  382. struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
  383. int len = snprintf(buf, PAGE_SIZE, "ps3:%d:%d\n", dev->match_id,
  384. dev->match_sub_id);
  385. return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len;
  386. }
  387. static struct device_attribute ps3_system_bus_dev_attrs[] = {
  388. __ATTR_RO(modalias),
  389. __ATTR_NULL,
  390. };
  391. struct bus_type ps3_system_bus_type = {
  392. .name = "ps3_system_bus",
  393. .match = ps3_system_bus_match,
  394. .uevent = ps3_system_bus_uevent,
  395. .probe = ps3_system_bus_probe,
  396. .remove = ps3_system_bus_remove,
  397. .shutdown = ps3_system_bus_shutdown,
  398. .dev_attrs = ps3_system_bus_dev_attrs,
  399. };
  400. static int __init ps3_system_bus_init(void)
  401. {
  402. int result;
  403. if (!firmware_has_feature(FW_FEATURE_PS3_LV1))
  404. return -ENODEV;
  405. pr_debug(" -> %s:%d\n", __func__, __LINE__);
  406. mutex_init(&usage_hack.mutex);
  407. result = device_register(&ps3_system_bus);
  408. BUG_ON(result);
  409. result = bus_register(&ps3_system_bus_type);
  410. BUG_ON(result);
  411. pr_debug(" <- %s:%d\n", __func__, __LINE__);
  412. return result;
  413. }
  414. core_initcall(ps3_system_bus_init);
  415. /* Allocates a contiguous real buffer and creates mappings over it.
  416. * Returns the virtual address of the buffer and sets dma_handle
  417. * to the dma address (mapping) of the first page.
  418. */
  419. static void * ps3_alloc_coherent(struct device *_dev, size_t size,
  420. dma_addr_t *dma_handle, gfp_t flag)
  421. {
  422. int result;
  423. struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
  424. unsigned long virt_addr;
  425. flag &= ~(__GFP_DMA | __GFP_HIGHMEM);
  426. flag |= __GFP_ZERO;
  427. virt_addr = __get_free_pages(flag, get_order(size));
  428. if (!virt_addr) {
  429. pr_debug("%s:%d: get_free_pages failed\n", __func__, __LINE__);
  430. goto clean_none;
  431. }
  432. result = ps3_dma_map(dev->d_region, virt_addr, size, dma_handle,
  433. CBE_IOPTE_PP_W | CBE_IOPTE_PP_R |
  434. CBE_IOPTE_SO_RW | CBE_IOPTE_M);
  435. if (result) {
  436. pr_debug("%s:%d: ps3_dma_map failed (%d)\n",
  437. __func__, __LINE__, result);
  438. BUG_ON("check region type");
  439. goto clean_alloc;
  440. }
  441. return (void*)virt_addr;
  442. clean_alloc:
  443. free_pages(virt_addr, get_order(size));
  444. clean_none:
  445. dma_handle = NULL;
  446. return NULL;
  447. }
  448. static void ps3_free_coherent(struct device *_dev, size_t size, void *vaddr,
  449. dma_addr_t dma_handle)
  450. {
  451. struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
  452. ps3_dma_unmap(dev->d_region, dma_handle, size);
  453. free_pages((unsigned long)vaddr, get_order(size));
  454. }
  455. /* Creates TCEs for a user provided buffer. The user buffer must be
  456. * contiguous real kernel storage (not vmalloc). The address passed here
  457. * comprises a page address and offset into that page. The dma_addr_t
  458. * returned will point to the same byte within the page as was passed in.
  459. */
  460. static dma_addr_t ps3_sb_map_page(struct device *_dev, struct page *page,
  461. unsigned long offset, size_t size, enum dma_data_direction direction,
  462. struct dma_attrs *attrs)
  463. {
  464. struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
  465. int result;
  466. dma_addr_t bus_addr;
  467. void *ptr = page_address(page) + offset;
  468. result = ps3_dma_map(dev->d_region, (unsigned long)ptr, size,
  469. &bus_addr,
  470. CBE_IOPTE_PP_R | CBE_IOPTE_PP_W |
  471. CBE_IOPTE_SO_RW | CBE_IOPTE_M);
  472. if (result) {
  473. pr_debug("%s:%d: ps3_dma_map failed (%d)\n",
  474. __func__, __LINE__, result);
  475. }
  476. return bus_addr;
  477. }
  478. static dma_addr_t ps3_ioc0_map_page(struct device *_dev, struct page *page,
  479. unsigned long offset, size_t size,
  480. enum dma_data_direction direction,
  481. struct dma_attrs *attrs)
  482. {
  483. struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
  484. int result;
  485. dma_addr_t bus_addr;
  486. u64 iopte_flag;
  487. void *ptr = page_address(page) + offset;
  488. iopte_flag = CBE_IOPTE_M;
  489. switch (direction) {
  490. case DMA_BIDIRECTIONAL:
  491. iopte_flag |= CBE_IOPTE_PP_R | CBE_IOPTE_PP_W | CBE_IOPTE_SO_RW;
  492. break;
  493. case DMA_TO_DEVICE:
  494. iopte_flag |= CBE_IOPTE_PP_R | CBE_IOPTE_SO_R;
  495. break;
  496. case DMA_FROM_DEVICE:
  497. iopte_flag |= CBE_IOPTE_PP_W | CBE_IOPTE_SO_RW;
  498. break;
  499. default:
  500. /* not happned */
  501. BUG();
  502. };
  503. result = ps3_dma_map(dev->d_region, (unsigned long)ptr, size,
  504. &bus_addr, iopte_flag);
  505. if (result) {
  506. pr_debug("%s:%d: ps3_dma_map failed (%d)\n",
  507. __func__, __LINE__, result);
  508. }
  509. return bus_addr;
  510. }
  511. static void ps3_unmap_page(struct device *_dev, dma_addr_t dma_addr,
  512. size_t size, enum dma_data_direction direction, struct dma_attrs *attrs)
  513. {
  514. struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
  515. int result;
  516. result = ps3_dma_unmap(dev->d_region, dma_addr, size);
  517. if (result) {
  518. pr_debug("%s:%d: ps3_dma_unmap failed (%d)\n",
  519. __func__, __LINE__, result);
  520. }
  521. }
  522. static int ps3_sb_map_sg(struct device *_dev, struct scatterlist *sgl,
  523. int nents, enum dma_data_direction direction, struct dma_attrs *attrs)
  524. {
  525. #if defined(CONFIG_PS3_DYNAMIC_DMA)
  526. BUG_ON("do");
  527. return -EPERM;
  528. #else
  529. struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
  530. struct scatterlist *sg;
  531. int i;
  532. for_each_sg(sgl, sg, nents, i) {
  533. int result = ps3_dma_map(dev->d_region, sg_phys(sg),
  534. sg->length, &sg->dma_address, 0);
  535. if (result) {
  536. pr_debug("%s:%d: ps3_dma_map failed (%d)\n",
  537. __func__, __LINE__, result);
  538. return -EINVAL;
  539. }
  540. sg->dma_length = sg->length;
  541. }
  542. return nents;
  543. #endif
  544. }
  545. static int ps3_ioc0_map_sg(struct device *_dev, struct scatterlist *sg,
  546. int nents,
  547. enum dma_data_direction direction,
  548. struct dma_attrs *attrs)
  549. {
  550. BUG();
  551. return 0;
  552. }
  553. static void ps3_sb_unmap_sg(struct device *_dev, struct scatterlist *sg,
  554. int nents, enum dma_data_direction direction, struct dma_attrs *attrs)
  555. {
  556. #if defined(CONFIG_PS3_DYNAMIC_DMA)
  557. BUG_ON("do");
  558. #endif
  559. }
  560. static void ps3_ioc0_unmap_sg(struct device *_dev, struct scatterlist *sg,
  561. int nents, enum dma_data_direction direction,
  562. struct dma_attrs *attrs)
  563. {
  564. BUG();
  565. }
  566. static int ps3_dma_supported(struct device *_dev, u64 mask)
  567. {
  568. return mask >= DMA_BIT_MASK(32);
  569. }
  570. static struct dma_map_ops ps3_sb_dma_ops = {
  571. .alloc_coherent = ps3_alloc_coherent,
  572. .free_coherent = ps3_free_coherent,
  573. .map_sg = ps3_sb_map_sg,
  574. .unmap_sg = ps3_sb_unmap_sg,
  575. .dma_supported = ps3_dma_supported,
  576. .map_page = ps3_sb_map_page,
  577. .unmap_page = ps3_unmap_page,
  578. };
  579. static struct dma_map_ops ps3_ioc0_dma_ops = {
  580. .alloc_coherent = ps3_alloc_coherent,
  581. .free_coherent = ps3_free_coherent,
  582. .map_sg = ps3_ioc0_map_sg,
  583. .unmap_sg = ps3_ioc0_unmap_sg,
  584. .dma_supported = ps3_dma_supported,
  585. .map_page = ps3_ioc0_map_page,
  586. .unmap_page = ps3_unmap_page,
  587. };
  588. /**
  589. * ps3_system_bus_release_device - remove a device from the system bus
  590. */
  591. static void ps3_system_bus_release_device(struct device *_dev)
  592. {
  593. struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
  594. kfree(dev);
  595. }
  596. /**
  597. * ps3_system_bus_device_register - add a device to the system bus
  598. *
  599. * ps3_system_bus_device_register() expects the dev object to be allocated
  600. * dynamically by the caller. The system bus takes ownership of the dev
  601. * object and frees the object in ps3_system_bus_release_device().
  602. */
  603. int ps3_system_bus_device_register(struct ps3_system_bus_device *dev)
  604. {
  605. int result;
  606. static unsigned int dev_ioc0_count;
  607. static unsigned int dev_sb_count;
  608. static unsigned int dev_vuart_count;
  609. static unsigned int dev_lpm_count;
  610. if (!dev->core.parent)
  611. dev->core.parent = &ps3_system_bus;
  612. dev->core.bus = &ps3_system_bus_type;
  613. dev->core.release = ps3_system_bus_release_device;
  614. switch (dev->dev_type) {
  615. case PS3_DEVICE_TYPE_IOC0:
  616. dev->core.archdata.dma_ops = &ps3_ioc0_dma_ops;
  617. dev_set_name(&dev->core, "ioc0_%02x", ++dev_ioc0_count);
  618. break;
  619. case PS3_DEVICE_TYPE_SB:
  620. dev->core.archdata.dma_ops = &ps3_sb_dma_ops;
  621. dev_set_name(&dev->core, "sb_%02x", ++dev_sb_count);
  622. break;
  623. case PS3_DEVICE_TYPE_VUART:
  624. dev_set_name(&dev->core, "vuart_%02x", ++dev_vuart_count);
  625. break;
  626. case PS3_DEVICE_TYPE_LPM:
  627. dev_set_name(&dev->core, "lpm_%02x", ++dev_lpm_count);
  628. break;
  629. default:
  630. BUG();
  631. };
  632. dev->core.of_node = NULL;
  633. set_dev_node(&dev->core, 0);
  634. pr_debug("%s:%d add %s\n", __func__, __LINE__, dev_name(&dev->core));
  635. result = device_register(&dev->core);
  636. return result;
  637. }
  638. EXPORT_SYMBOL_GPL(ps3_system_bus_device_register);
  639. int ps3_system_bus_driver_register(struct ps3_system_bus_driver *drv)
  640. {
  641. int result;
  642. pr_debug(" -> %s:%d: %s\n", __func__, __LINE__, drv->core.name);
  643. if (!firmware_has_feature(FW_FEATURE_PS3_LV1))
  644. return -ENODEV;
  645. drv->core.bus = &ps3_system_bus_type;
  646. result = driver_register(&drv->core);
  647. pr_debug(" <- %s:%d: %s\n", __func__, __LINE__, drv->core.name);
  648. return result;
  649. }
  650. EXPORT_SYMBOL_GPL(ps3_system_bus_driver_register);
  651. void ps3_system_bus_driver_unregister(struct ps3_system_bus_driver *drv)
  652. {
  653. pr_debug(" -> %s:%d: %s\n", __func__, __LINE__, drv->core.name);
  654. driver_unregister(&drv->core);
  655. pr_debug(" <- %s:%d: %s\n", __func__, __LINE__, drv->core.name);
  656. }
  657. EXPORT_SYMBOL_GPL(ps3_system_bus_driver_unregister);