system-bus.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804
  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 <asm/cell-regs.h>
  29. #include "platform.h"
  30. static struct device ps3_system_bus = {
  31. .init_name = "ps3_system",
  32. };
  33. /* FIXME: need device usage counters! */
  34. struct {
  35. struct mutex mutex;
  36. int sb_11; /* usb 0 */
  37. int sb_12; /* usb 0 */
  38. int gpu;
  39. } static usage_hack;
  40. static int ps3_is_device(struct ps3_system_bus_device *dev, u64 bus_id,
  41. u64 dev_id)
  42. {
  43. return dev->bus_id == bus_id && dev->dev_id == dev_id;
  44. }
  45. static int ps3_open_hv_device_sb(struct ps3_system_bus_device *dev)
  46. {
  47. int result;
  48. BUG_ON(!dev->bus_id);
  49. mutex_lock(&usage_hack.mutex);
  50. if (ps3_is_device(dev, 1, 1)) {
  51. usage_hack.sb_11++;
  52. if (usage_hack.sb_11 > 1) {
  53. result = 0;
  54. goto done;
  55. }
  56. }
  57. if (ps3_is_device(dev, 1, 2)) {
  58. usage_hack.sb_12++;
  59. if (usage_hack.sb_12 > 1) {
  60. result = 0;
  61. goto done;
  62. }
  63. }
  64. result = lv1_open_device(dev->bus_id, dev->dev_id, 0);
  65. if (result) {
  66. pr_debug("%s:%d: lv1_open_device failed: %s\n", __func__,
  67. __LINE__, ps3_result(result));
  68. result = -EPERM;
  69. }
  70. done:
  71. mutex_unlock(&usage_hack.mutex);
  72. return result;
  73. }
  74. static int ps3_close_hv_device_sb(struct ps3_system_bus_device *dev)
  75. {
  76. int result;
  77. BUG_ON(!dev->bus_id);
  78. mutex_lock(&usage_hack.mutex);
  79. if (ps3_is_device(dev, 1, 1)) {
  80. usage_hack.sb_11--;
  81. if (usage_hack.sb_11) {
  82. result = 0;
  83. goto done;
  84. }
  85. }
  86. if (ps3_is_device(dev, 1, 2)) {
  87. usage_hack.sb_12--;
  88. if (usage_hack.sb_12) {
  89. result = 0;
  90. goto done;
  91. }
  92. }
  93. result = lv1_close_device(dev->bus_id, dev->dev_id);
  94. BUG_ON(result);
  95. done:
  96. mutex_unlock(&usage_hack.mutex);
  97. return result;
  98. }
  99. static int ps3_open_hv_device_gpu(struct ps3_system_bus_device *dev)
  100. {
  101. int result;
  102. mutex_lock(&usage_hack.mutex);
  103. usage_hack.gpu++;
  104. if (usage_hack.gpu > 1) {
  105. result = 0;
  106. goto done;
  107. }
  108. result = lv1_gpu_open(0);
  109. if (result) {
  110. pr_debug("%s:%d: lv1_gpu_open failed: %s\n", __func__,
  111. __LINE__, ps3_result(result));
  112. result = -EPERM;
  113. }
  114. done:
  115. mutex_unlock(&usage_hack.mutex);
  116. return result;
  117. }
  118. static int ps3_close_hv_device_gpu(struct ps3_system_bus_device *dev)
  119. {
  120. int result;
  121. mutex_lock(&usage_hack.mutex);
  122. usage_hack.gpu--;
  123. if (usage_hack.gpu) {
  124. result = 0;
  125. goto done;
  126. }
  127. result = lv1_gpu_close();
  128. BUG_ON(result);
  129. done:
  130. mutex_unlock(&usage_hack.mutex);
  131. return result;
  132. }
  133. int ps3_open_hv_device(struct ps3_system_bus_device *dev)
  134. {
  135. BUG_ON(!dev);
  136. pr_debug("%s:%d: match_id: %u\n", __func__, __LINE__, dev->match_id);
  137. switch (dev->match_id) {
  138. case PS3_MATCH_ID_EHCI:
  139. case PS3_MATCH_ID_OHCI:
  140. case PS3_MATCH_ID_GELIC:
  141. case PS3_MATCH_ID_STOR_DISK:
  142. case PS3_MATCH_ID_STOR_ROM:
  143. case PS3_MATCH_ID_STOR_FLASH:
  144. return ps3_open_hv_device_sb(dev);
  145. case PS3_MATCH_ID_SOUND:
  146. case PS3_MATCH_ID_GPU:
  147. return ps3_open_hv_device_gpu(dev);
  148. case PS3_MATCH_ID_AV_SETTINGS:
  149. case PS3_MATCH_ID_SYSTEM_MANAGER:
  150. pr_debug("%s:%d: unsupported match_id: %u\n", __func__,
  151. __LINE__, dev->match_id);
  152. pr_debug("%s:%d: bus_id: %llu\n", __func__, __LINE__,
  153. dev->bus_id);
  154. BUG();
  155. return -EINVAL;
  156. default:
  157. break;
  158. }
  159. pr_debug("%s:%d: unknown match_id: %u\n", __func__, __LINE__,
  160. dev->match_id);
  161. BUG();
  162. return -ENODEV;
  163. }
  164. EXPORT_SYMBOL_GPL(ps3_open_hv_device);
  165. int ps3_close_hv_device(struct ps3_system_bus_device *dev)
  166. {
  167. BUG_ON(!dev);
  168. pr_debug("%s:%d: match_id: %u\n", __func__, __LINE__, dev->match_id);
  169. switch (dev->match_id) {
  170. case PS3_MATCH_ID_EHCI:
  171. case PS3_MATCH_ID_OHCI:
  172. case PS3_MATCH_ID_GELIC:
  173. case PS3_MATCH_ID_STOR_DISK:
  174. case PS3_MATCH_ID_STOR_ROM:
  175. case PS3_MATCH_ID_STOR_FLASH:
  176. return ps3_close_hv_device_sb(dev);
  177. case PS3_MATCH_ID_SOUND:
  178. case PS3_MATCH_ID_GPU:
  179. return ps3_close_hv_device_gpu(dev);
  180. case PS3_MATCH_ID_AV_SETTINGS:
  181. case PS3_MATCH_ID_SYSTEM_MANAGER:
  182. pr_debug("%s:%d: unsupported match_id: %u\n", __func__,
  183. __LINE__, dev->match_id);
  184. pr_debug("%s:%d: bus_id: %llu\n", __func__, __LINE__,
  185. dev->bus_id);
  186. BUG();
  187. return -EINVAL;
  188. default:
  189. break;
  190. }
  191. pr_debug("%s:%d: unknown match_id: %u\n", __func__, __LINE__,
  192. dev->match_id);
  193. BUG();
  194. return -ENODEV;
  195. }
  196. EXPORT_SYMBOL_GPL(ps3_close_hv_device);
  197. #define dump_mmio_region(_a) _dump_mmio_region(_a, __func__, __LINE__)
  198. static void _dump_mmio_region(const struct ps3_mmio_region* r,
  199. const char* func, int line)
  200. {
  201. pr_debug("%s:%d: dev %llu:%llu\n", func, line, r->dev->bus_id,
  202. r->dev->dev_id);
  203. pr_debug("%s:%d: bus_addr %lxh\n", func, line, r->bus_addr);
  204. pr_debug("%s:%d: len %lxh\n", func, line, r->len);
  205. pr_debug("%s:%d: lpar_addr %lxh\n", func, line, r->lpar_addr);
  206. }
  207. static int ps3_sb_mmio_region_create(struct ps3_mmio_region *r)
  208. {
  209. int result;
  210. u64 lpar_addr;
  211. result = lv1_map_device_mmio_region(r->dev->bus_id, r->dev->dev_id,
  212. r->bus_addr, r->len, r->page_size, &lpar_addr);
  213. r->lpar_addr = lpar_addr;
  214. if (result) {
  215. pr_debug("%s:%d: lv1_map_device_mmio_region failed: %s\n",
  216. __func__, __LINE__, ps3_result(result));
  217. r->lpar_addr = 0;
  218. }
  219. dump_mmio_region(r);
  220. return result;
  221. }
  222. static int ps3_ioc0_mmio_region_create(struct ps3_mmio_region *r)
  223. {
  224. /* device specific; do nothing currently */
  225. return 0;
  226. }
  227. int ps3_mmio_region_create(struct ps3_mmio_region *r)
  228. {
  229. return r->mmio_ops->create(r);
  230. }
  231. EXPORT_SYMBOL_GPL(ps3_mmio_region_create);
  232. static int ps3_sb_free_mmio_region(struct ps3_mmio_region *r)
  233. {
  234. int result;
  235. dump_mmio_region(r);
  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. CBE_IOPTE_PP_W | CBE_IOPTE_PP_R |
  433. CBE_IOPTE_SO_RW | CBE_IOPTE_M);
  434. if (result) {
  435. pr_debug("%s:%d: ps3_dma_map failed (%d)\n",
  436. __func__, __LINE__, result);
  437. BUG_ON("check region type");
  438. goto clean_alloc;
  439. }
  440. return (void*)virt_addr;
  441. clean_alloc:
  442. free_pages(virt_addr, get_order(size));
  443. clean_none:
  444. dma_handle = NULL;
  445. return NULL;
  446. }
  447. static void ps3_free_coherent(struct device *_dev, size_t size, void *vaddr,
  448. dma_addr_t dma_handle)
  449. {
  450. struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
  451. ps3_dma_unmap(dev->d_region, dma_handle, size);
  452. free_pages((unsigned long)vaddr, get_order(size));
  453. }
  454. /* Creates TCEs for a user provided buffer. The user buffer must be
  455. * contiguous real kernel storage (not vmalloc). The address passed here
  456. * comprises a page address and offset into that page. The dma_addr_t
  457. * returned will point to the same byte within the page as was passed in.
  458. */
  459. static dma_addr_t ps3_sb_map_page(struct device *_dev, struct page *page,
  460. unsigned long offset, size_t size, enum dma_data_direction direction,
  461. struct dma_attrs *attrs)
  462. {
  463. struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
  464. int result;
  465. dma_addr_t bus_addr;
  466. void *ptr = page_address(page) + offset;
  467. result = ps3_dma_map(dev->d_region, (unsigned long)ptr, size,
  468. &bus_addr,
  469. CBE_IOPTE_PP_R | CBE_IOPTE_PP_W |
  470. CBE_IOPTE_SO_RW | CBE_IOPTE_M);
  471. if (result) {
  472. pr_debug("%s:%d: ps3_dma_map failed (%d)\n",
  473. __func__, __LINE__, result);
  474. }
  475. return bus_addr;
  476. }
  477. static dma_addr_t ps3_ioc0_map_page(struct device *_dev, struct page *page,
  478. unsigned long offset, size_t size,
  479. enum dma_data_direction direction,
  480. struct dma_attrs *attrs)
  481. {
  482. struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
  483. int result;
  484. dma_addr_t bus_addr;
  485. u64 iopte_flag;
  486. void *ptr = page_address(page) + offset;
  487. iopte_flag = CBE_IOPTE_M;
  488. switch (direction) {
  489. case DMA_BIDIRECTIONAL:
  490. iopte_flag |= CBE_IOPTE_PP_R | CBE_IOPTE_PP_W | CBE_IOPTE_SO_RW;
  491. break;
  492. case DMA_TO_DEVICE:
  493. iopte_flag |= CBE_IOPTE_PP_R | CBE_IOPTE_SO_R;
  494. break;
  495. case DMA_FROM_DEVICE:
  496. iopte_flag |= CBE_IOPTE_PP_W | CBE_IOPTE_SO_RW;
  497. break;
  498. default:
  499. /* not happned */
  500. BUG();
  501. };
  502. result = ps3_dma_map(dev->d_region, (unsigned long)ptr, size,
  503. &bus_addr, iopte_flag);
  504. if (result) {
  505. pr_debug("%s:%d: ps3_dma_map failed (%d)\n",
  506. __func__, __LINE__, result);
  507. }
  508. return bus_addr;
  509. }
  510. static void ps3_unmap_page(struct device *_dev, dma_addr_t dma_addr,
  511. size_t size, enum dma_data_direction direction, struct dma_attrs *attrs)
  512. {
  513. struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
  514. int result;
  515. result = ps3_dma_unmap(dev->d_region, dma_addr, size);
  516. if (result) {
  517. pr_debug("%s:%d: ps3_dma_unmap failed (%d)\n",
  518. __func__, __LINE__, result);
  519. }
  520. }
  521. static int ps3_sb_map_sg(struct device *_dev, struct scatterlist *sgl,
  522. int nents, enum dma_data_direction direction, struct dma_attrs *attrs)
  523. {
  524. #if defined(CONFIG_PS3_DYNAMIC_DMA)
  525. BUG_ON("do");
  526. return -EPERM;
  527. #else
  528. struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
  529. struct scatterlist *sg;
  530. int i;
  531. for_each_sg(sgl, sg, nents, i) {
  532. int result = ps3_dma_map(dev->d_region, sg_phys(sg),
  533. sg->length, &sg->dma_address, 0);
  534. if (result) {
  535. pr_debug("%s:%d: ps3_dma_map failed (%d)\n",
  536. __func__, __LINE__, result);
  537. return -EINVAL;
  538. }
  539. sg->dma_length = sg->length;
  540. }
  541. return nents;
  542. #endif
  543. }
  544. static int ps3_ioc0_map_sg(struct device *_dev, struct scatterlist *sg,
  545. int nents,
  546. enum dma_data_direction direction,
  547. struct dma_attrs *attrs)
  548. {
  549. BUG();
  550. return 0;
  551. }
  552. static void ps3_sb_unmap_sg(struct device *_dev, struct scatterlist *sg,
  553. int nents, enum dma_data_direction direction, struct dma_attrs *attrs)
  554. {
  555. #if defined(CONFIG_PS3_DYNAMIC_DMA)
  556. BUG_ON("do");
  557. #endif
  558. }
  559. static void ps3_ioc0_unmap_sg(struct device *_dev, struct scatterlist *sg,
  560. int nents, enum dma_data_direction direction,
  561. struct dma_attrs *attrs)
  562. {
  563. BUG();
  564. }
  565. static int ps3_dma_supported(struct device *_dev, u64 mask)
  566. {
  567. return mask >= DMA_BIT_MASK(32);
  568. }
  569. static struct dma_map_ops ps3_sb_dma_ops = {
  570. .alloc_coherent = ps3_alloc_coherent,
  571. .free_coherent = ps3_free_coherent,
  572. .map_sg = ps3_sb_map_sg,
  573. .unmap_sg = ps3_sb_unmap_sg,
  574. .dma_supported = ps3_dma_supported,
  575. .map_page = ps3_sb_map_page,
  576. .unmap_page = ps3_unmap_page,
  577. };
  578. static struct dma_map_ops ps3_ioc0_dma_ops = {
  579. .alloc_coherent = ps3_alloc_coherent,
  580. .free_coherent = ps3_free_coherent,
  581. .map_sg = ps3_ioc0_map_sg,
  582. .unmap_sg = ps3_ioc0_unmap_sg,
  583. .dma_supported = ps3_dma_supported,
  584. .map_page = ps3_ioc0_map_page,
  585. .unmap_page = ps3_unmap_page,
  586. };
  587. /**
  588. * ps3_system_bus_release_device - remove a device from the system bus
  589. */
  590. static void ps3_system_bus_release_device(struct device *_dev)
  591. {
  592. struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
  593. kfree(dev);
  594. }
  595. /**
  596. * ps3_system_bus_device_register - add a device to the system bus
  597. *
  598. * ps3_system_bus_device_register() expects the dev object to be allocated
  599. * dynamically by the caller. The system bus takes ownership of the dev
  600. * object and frees the object in ps3_system_bus_release_device().
  601. */
  602. int ps3_system_bus_device_register(struct ps3_system_bus_device *dev)
  603. {
  604. int result;
  605. static unsigned int dev_ioc0_count;
  606. static unsigned int dev_sb_count;
  607. static unsigned int dev_vuart_count;
  608. static unsigned int dev_lpm_count;
  609. if (!dev->core.parent)
  610. dev->core.parent = &ps3_system_bus;
  611. dev->core.bus = &ps3_system_bus_type;
  612. dev->core.release = ps3_system_bus_release_device;
  613. switch (dev->dev_type) {
  614. case PS3_DEVICE_TYPE_IOC0:
  615. dev->core.archdata.dma_ops = &ps3_ioc0_dma_ops;
  616. dev_set_name(&dev->core, "ioc0_%02x", ++dev_ioc0_count);
  617. break;
  618. case PS3_DEVICE_TYPE_SB:
  619. dev->core.archdata.dma_ops = &ps3_sb_dma_ops;
  620. dev_set_name(&dev->core, "sb_%02x", ++dev_sb_count);
  621. break;
  622. case PS3_DEVICE_TYPE_VUART:
  623. dev_set_name(&dev->core, "vuart_%02x", ++dev_vuart_count);
  624. break;
  625. case PS3_DEVICE_TYPE_LPM:
  626. dev_set_name(&dev->core, "lpm_%02x", ++dev_lpm_count);
  627. break;
  628. default:
  629. BUG();
  630. };
  631. dev->core.archdata.of_node = NULL;
  632. set_dev_node(&dev->core, 0);
  633. pr_debug("%s:%d add %s\n", __func__, __LINE__, dev_name(&dev->core));
  634. result = device_register(&dev->core);
  635. return result;
  636. }
  637. EXPORT_SYMBOL_GPL(ps3_system_bus_device_register);
  638. int ps3_system_bus_driver_register(struct ps3_system_bus_driver *drv)
  639. {
  640. int result;
  641. pr_debug(" -> %s:%d: %s\n", __func__, __LINE__, drv->core.name);
  642. if (!firmware_has_feature(FW_FEATURE_PS3_LV1))
  643. return -ENODEV;
  644. drv->core.bus = &ps3_system_bus_type;
  645. result = driver_register(&drv->core);
  646. pr_debug(" <- %s:%d: %s\n", __func__, __LINE__, drv->core.name);
  647. return result;
  648. }
  649. EXPORT_SYMBOL_GPL(ps3_system_bus_driver_register);
  650. void ps3_system_bus_driver_unregister(struct ps3_system_bus_driver *drv)
  651. {
  652. pr_debug(" -> %s:%d: %s\n", __func__, __LINE__, drv->core.name);
  653. driver_unregister(&drv->core);
  654. pr_debug(" <- %s:%d: %s\n", __func__, __LINE__, drv->core.name);
  655. }
  656. EXPORT_SYMBOL_GPL(ps3_system_bus_driver_unregister);