system-bus.c 19 KB

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