system-bus.c 19 KB

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