system-bus.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814
  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/export.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. unsigned long attrs)
  422. {
  423. int result;
  424. struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
  425. unsigned long virt_addr;
  426. flag &= ~(__GFP_DMA | __GFP_HIGHMEM);
  427. flag |= __GFP_ZERO;
  428. virt_addr = __get_free_pages(flag, get_order(size));
  429. if (!virt_addr) {
  430. pr_debug("%s:%d: get_free_pages failed\n", __func__, __LINE__);
  431. goto clean_none;
  432. }
  433. result = ps3_dma_map(dev->d_region, virt_addr, size, dma_handle,
  434. CBE_IOPTE_PP_W | CBE_IOPTE_PP_R |
  435. CBE_IOPTE_SO_RW | CBE_IOPTE_M);
  436. if (result) {
  437. pr_debug("%s:%d: ps3_dma_map failed (%d)\n",
  438. __func__, __LINE__, result);
  439. BUG_ON("check region type");
  440. goto clean_alloc;
  441. }
  442. return (void*)virt_addr;
  443. clean_alloc:
  444. free_pages(virt_addr, get_order(size));
  445. clean_none:
  446. dma_handle = NULL;
  447. return NULL;
  448. }
  449. static void ps3_free_coherent(struct device *_dev, size_t size, void *vaddr,
  450. dma_addr_t dma_handle, unsigned long attrs)
  451. {
  452. struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
  453. ps3_dma_unmap(dev->d_region, dma_handle, size);
  454. free_pages((unsigned long)vaddr, get_order(size));
  455. }
  456. /* Creates TCEs for a user provided buffer. The user buffer must be
  457. * contiguous real kernel storage (not vmalloc). The address passed here
  458. * comprises a page address and offset into that page. The dma_addr_t
  459. * returned will point to the same byte within the page as was passed in.
  460. */
  461. static dma_addr_t ps3_sb_map_page(struct device *_dev, struct page *page,
  462. unsigned long offset, size_t size, enum dma_data_direction direction,
  463. unsigned long attrs)
  464. {
  465. struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
  466. int result;
  467. dma_addr_t bus_addr;
  468. void *ptr = page_address(page) + offset;
  469. result = ps3_dma_map(dev->d_region, (unsigned long)ptr, size,
  470. &bus_addr,
  471. CBE_IOPTE_PP_R | CBE_IOPTE_PP_W |
  472. CBE_IOPTE_SO_RW | CBE_IOPTE_M);
  473. if (result) {
  474. pr_debug("%s:%d: ps3_dma_map failed (%d)\n",
  475. __func__, __LINE__, result);
  476. }
  477. return bus_addr;
  478. }
  479. static dma_addr_t ps3_ioc0_map_page(struct device *_dev, struct page *page,
  480. unsigned long offset, size_t size,
  481. enum dma_data_direction direction,
  482. unsigned long attrs)
  483. {
  484. struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
  485. int result;
  486. dma_addr_t bus_addr;
  487. u64 iopte_flag;
  488. void *ptr = page_address(page) + offset;
  489. iopte_flag = CBE_IOPTE_M;
  490. switch (direction) {
  491. case DMA_BIDIRECTIONAL:
  492. iopte_flag |= CBE_IOPTE_PP_R | CBE_IOPTE_PP_W | CBE_IOPTE_SO_RW;
  493. break;
  494. case DMA_TO_DEVICE:
  495. iopte_flag |= CBE_IOPTE_PP_R | CBE_IOPTE_SO_R;
  496. break;
  497. case DMA_FROM_DEVICE:
  498. iopte_flag |= CBE_IOPTE_PP_W | CBE_IOPTE_SO_RW;
  499. break;
  500. default:
  501. /* not happned */
  502. BUG();
  503. };
  504. result = ps3_dma_map(dev->d_region, (unsigned long)ptr, size,
  505. &bus_addr, iopte_flag);
  506. if (result) {
  507. pr_debug("%s:%d: ps3_dma_map failed (%d)\n",
  508. __func__, __LINE__, result);
  509. }
  510. return bus_addr;
  511. }
  512. static void ps3_unmap_page(struct device *_dev, dma_addr_t dma_addr,
  513. size_t size, enum dma_data_direction direction, unsigned long attrs)
  514. {
  515. struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
  516. int result;
  517. result = ps3_dma_unmap(dev->d_region, dma_addr, size);
  518. if (result) {
  519. pr_debug("%s:%d: ps3_dma_unmap failed (%d)\n",
  520. __func__, __LINE__, result);
  521. }
  522. }
  523. static int ps3_sb_map_sg(struct device *_dev, struct scatterlist *sgl,
  524. int nents, enum dma_data_direction direction, unsigned long attrs)
  525. {
  526. #if defined(CONFIG_PS3_DYNAMIC_DMA)
  527. BUG_ON("do");
  528. return -EPERM;
  529. #else
  530. struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
  531. struct scatterlist *sg;
  532. int i;
  533. for_each_sg(sgl, sg, nents, i) {
  534. int result = ps3_dma_map(dev->d_region, sg_phys(sg),
  535. sg->length, &sg->dma_address, 0);
  536. if (result) {
  537. pr_debug("%s:%d: ps3_dma_map failed (%d)\n",
  538. __func__, __LINE__, result);
  539. return -EINVAL;
  540. }
  541. sg->dma_length = sg->length;
  542. }
  543. return nents;
  544. #endif
  545. }
  546. static int ps3_ioc0_map_sg(struct device *_dev, struct scatterlist *sg,
  547. int nents,
  548. enum dma_data_direction direction,
  549. unsigned long attrs)
  550. {
  551. BUG();
  552. return 0;
  553. }
  554. static void ps3_sb_unmap_sg(struct device *_dev, struct scatterlist *sg,
  555. int nents, enum dma_data_direction direction, unsigned long attrs)
  556. {
  557. #if defined(CONFIG_PS3_DYNAMIC_DMA)
  558. BUG_ON("do");
  559. #endif
  560. }
  561. static void ps3_ioc0_unmap_sg(struct device *_dev, struct scatterlist *sg,
  562. int nents, enum dma_data_direction direction,
  563. unsigned long attrs)
  564. {
  565. BUG();
  566. }
  567. static int ps3_dma_supported(struct device *_dev, u64 mask)
  568. {
  569. return mask >= DMA_BIT_MASK(32);
  570. }
  571. static u64 ps3_dma_get_required_mask(struct device *_dev)
  572. {
  573. return DMA_BIT_MASK(32);
  574. }
  575. static struct dma_map_ops ps3_sb_dma_ops = {
  576. .alloc = ps3_alloc_coherent,
  577. .free = ps3_free_coherent,
  578. .map_sg = ps3_sb_map_sg,
  579. .unmap_sg = ps3_sb_unmap_sg,
  580. .dma_supported = ps3_dma_supported,
  581. .get_required_mask = ps3_dma_get_required_mask,
  582. .map_page = ps3_sb_map_page,
  583. .unmap_page = ps3_unmap_page,
  584. };
  585. static struct dma_map_ops ps3_ioc0_dma_ops = {
  586. .alloc = ps3_alloc_coherent,
  587. .free = ps3_free_coherent,
  588. .map_sg = ps3_ioc0_map_sg,
  589. .unmap_sg = ps3_ioc0_unmap_sg,
  590. .dma_supported = ps3_dma_supported,
  591. .get_required_mask = ps3_dma_get_required_mask,
  592. .map_page = ps3_ioc0_map_page,
  593. .unmap_page = ps3_unmap_page,
  594. };
  595. /**
  596. * ps3_system_bus_release_device - remove a device from the system bus
  597. */
  598. static void ps3_system_bus_release_device(struct device *_dev)
  599. {
  600. struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
  601. kfree(dev);
  602. }
  603. /**
  604. * ps3_system_bus_device_register - add a device to the system bus
  605. *
  606. * ps3_system_bus_device_register() expects the dev object to be allocated
  607. * dynamically by the caller. The system bus takes ownership of the dev
  608. * object and frees the object in ps3_system_bus_release_device().
  609. */
  610. int ps3_system_bus_device_register(struct ps3_system_bus_device *dev)
  611. {
  612. int result;
  613. static unsigned int dev_ioc0_count;
  614. static unsigned int dev_sb_count;
  615. static unsigned int dev_vuart_count;
  616. static unsigned int dev_lpm_count;
  617. if (!dev->core.parent)
  618. dev->core.parent = &ps3_system_bus;
  619. dev->core.bus = &ps3_system_bus_type;
  620. dev->core.release = ps3_system_bus_release_device;
  621. switch (dev->dev_type) {
  622. case PS3_DEVICE_TYPE_IOC0:
  623. dev->core.archdata.dma_ops = &ps3_ioc0_dma_ops;
  624. dev_set_name(&dev->core, "ioc0_%02x", ++dev_ioc0_count);
  625. break;
  626. case PS3_DEVICE_TYPE_SB:
  627. dev->core.archdata.dma_ops = &ps3_sb_dma_ops;
  628. dev_set_name(&dev->core, "sb_%02x", ++dev_sb_count);
  629. break;
  630. case PS3_DEVICE_TYPE_VUART:
  631. dev_set_name(&dev->core, "vuart_%02x", ++dev_vuart_count);
  632. break;
  633. case PS3_DEVICE_TYPE_LPM:
  634. dev_set_name(&dev->core, "lpm_%02x", ++dev_lpm_count);
  635. break;
  636. default:
  637. BUG();
  638. };
  639. dev->core.of_node = NULL;
  640. set_dev_node(&dev->core, 0);
  641. pr_debug("%s:%d add %s\n", __func__, __LINE__, dev_name(&dev->core));
  642. result = device_register(&dev->core);
  643. return result;
  644. }
  645. EXPORT_SYMBOL_GPL(ps3_system_bus_device_register);
  646. int ps3_system_bus_driver_register(struct ps3_system_bus_driver *drv)
  647. {
  648. int result;
  649. pr_debug(" -> %s:%d: %s\n", __func__, __LINE__, drv->core.name);
  650. if (!firmware_has_feature(FW_FEATURE_PS3_LV1))
  651. return -ENODEV;
  652. drv->core.bus = &ps3_system_bus_type;
  653. result = driver_register(&drv->core);
  654. pr_debug(" <- %s:%d: %s\n", __func__, __LINE__, drv->core.name);
  655. return result;
  656. }
  657. EXPORT_SYMBOL_GPL(ps3_system_bus_driver_register);
  658. void ps3_system_bus_driver_unregister(struct ps3_system_bus_driver *drv)
  659. {
  660. pr_debug(" -> %s:%d: %s\n", __func__, __LINE__, drv->core.name);
  661. driver_unregister(&drv->core);
  662. pr_debug(" <- %s:%d: %s\n", __func__, __LINE__, drv->core.name);
  663. }
  664. EXPORT_SYMBOL_GPL(ps3_system_bus_driver_unregister);