system-bus.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  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. .bus_id = "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,
  40. unsigned int bus_id, unsigned int 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_GRAPHICS:
  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: %u\n", __func__,
  152. __LINE__, 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_GRAPHICS:
  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: %u\n", __func__,
  184. __LINE__, 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 %u:%u\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. result = lv1_map_device_mmio_region(r->dev->bus_id, r->dev->dev_id,
  210. r->bus_addr, r->len, r->page_size, &r->lpar_addr);
  211. if (result) {
  212. pr_debug("%s:%d: lv1_map_device_mmio_region failed: %s\n",
  213. __func__, __LINE__, ps3_result(result));
  214. r->lpar_addr = 0;
  215. }
  216. dump_mmio_region(r);
  217. return result;
  218. }
  219. static int ps3_ioc0_mmio_region_create(struct ps3_mmio_region *r)
  220. {
  221. /* device specific; do nothing currently */
  222. return 0;
  223. }
  224. int ps3_mmio_region_create(struct ps3_mmio_region *r)
  225. {
  226. return r->mmio_ops->create(r);
  227. }
  228. EXPORT_SYMBOL_GPL(ps3_mmio_region_create);
  229. static int ps3_sb_free_mmio_region(struct ps3_mmio_region *r)
  230. {
  231. int result;
  232. dump_mmio_region(r);
  233. ;
  234. result = lv1_unmap_device_mmio_region(r->dev->bus_id, r->dev->dev_id,
  235. r->lpar_addr);
  236. if (result)
  237. pr_debug("%s:%d: lv1_unmap_device_mmio_region failed: %s\n",
  238. __func__, __LINE__, ps3_result(result));
  239. r->lpar_addr = 0;
  240. return result;
  241. }
  242. static int ps3_ioc0_free_mmio_region(struct ps3_mmio_region *r)
  243. {
  244. /* device specific; do nothing currently */
  245. return 0;
  246. }
  247. int ps3_free_mmio_region(struct ps3_mmio_region *r)
  248. {
  249. return r->mmio_ops->free(r);
  250. }
  251. EXPORT_SYMBOL_GPL(ps3_free_mmio_region);
  252. static const struct ps3_mmio_region_ops ps3_mmio_sb_region_ops = {
  253. .create = ps3_sb_mmio_region_create,
  254. .free = ps3_sb_free_mmio_region
  255. };
  256. static const struct ps3_mmio_region_ops ps3_mmio_ioc0_region_ops = {
  257. .create = ps3_ioc0_mmio_region_create,
  258. .free = ps3_ioc0_free_mmio_region
  259. };
  260. int ps3_mmio_region_init(struct ps3_system_bus_device *dev,
  261. struct ps3_mmio_region *r, unsigned long bus_addr, unsigned long len,
  262. enum ps3_mmio_page_size page_size)
  263. {
  264. r->dev = dev;
  265. r->bus_addr = bus_addr;
  266. r->len = len;
  267. r->page_size = page_size;
  268. switch (dev->dev_type) {
  269. case PS3_DEVICE_TYPE_SB:
  270. r->mmio_ops = &ps3_mmio_sb_region_ops;
  271. break;
  272. case PS3_DEVICE_TYPE_IOC0:
  273. r->mmio_ops = &ps3_mmio_ioc0_region_ops;
  274. break;
  275. default:
  276. BUG();
  277. return -EINVAL;
  278. }
  279. return 0;
  280. }
  281. EXPORT_SYMBOL_GPL(ps3_mmio_region_init);
  282. static int ps3_system_bus_match(struct device *_dev,
  283. struct device_driver *_drv)
  284. {
  285. int result;
  286. struct ps3_system_bus_driver *drv = ps3_drv_to_system_bus_drv(_drv);
  287. struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
  288. result = dev->match_id == drv->match_id;
  289. pr_info("%s:%d: dev=%u(%s), drv=%u(%s): %s\n", __func__, __LINE__,
  290. dev->match_id, dev->core.bus_id, drv->match_id, drv->core.name,
  291. (result ? "match" : "miss"));
  292. return result;
  293. }
  294. static int ps3_system_bus_probe(struct device *_dev)
  295. {
  296. int result = 0;
  297. struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
  298. struct ps3_system_bus_driver *drv;
  299. BUG_ON(!dev);
  300. pr_info(" -> %s:%d: %s\n", __func__, __LINE__, _dev->bus_id);
  301. drv = ps3_system_bus_dev_to_system_bus_drv(dev);
  302. BUG_ON(!drv);
  303. if (drv->probe)
  304. result = drv->probe(dev);
  305. else
  306. pr_info("%s:%d: %s no probe method\n", __func__, __LINE__,
  307. dev->core.bus_id);
  308. pr_info(" <- %s:%d: %s\n", __func__, __LINE__, dev->core.bus_id);
  309. return result;
  310. }
  311. static int ps3_system_bus_remove(struct device *_dev)
  312. {
  313. int result = 0;
  314. struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
  315. struct ps3_system_bus_driver *drv;
  316. BUG_ON(!dev);
  317. pr_info(" -> %s:%d: %s\n", __func__, __LINE__, _dev->bus_id);
  318. drv = ps3_system_bus_dev_to_system_bus_drv(dev);
  319. BUG_ON(!drv);
  320. if (drv->remove)
  321. result = drv->remove(dev);
  322. else
  323. dev_dbg(&dev->core, "%s:%d %s: no remove method\n",
  324. __func__, __LINE__, drv->core.name);
  325. pr_info(" <- %s:%d: %s\n", __func__, __LINE__, dev->core.bus_id);
  326. return result;
  327. }
  328. static void ps3_system_bus_shutdown(struct device *_dev)
  329. {
  330. struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
  331. struct ps3_system_bus_driver *drv;
  332. BUG_ON(!dev);
  333. dev_dbg(&dev->core, " -> %s:%d: match_id %d\n", __func__, __LINE__,
  334. dev->match_id);
  335. if (!dev->core.driver) {
  336. dev_dbg(&dev->core, "%s:%d: no driver bound\n", __func__,
  337. __LINE__);
  338. return;
  339. }
  340. drv = ps3_system_bus_dev_to_system_bus_drv(dev);
  341. BUG_ON(!drv);
  342. dev_dbg(&dev->core, "%s:%d: %s -> %s\n", __func__, __LINE__,
  343. dev->core.bus_id, drv->core.name);
  344. if (drv->shutdown)
  345. drv->shutdown(dev);
  346. else if (drv->remove) {
  347. dev_dbg(&dev->core, "%s:%d %s: no shutdown, calling remove\n",
  348. __func__, __LINE__, drv->core.name);
  349. drv->remove(dev);
  350. } else {
  351. dev_dbg(&dev->core, "%s:%d %s: no shutdown method\n",
  352. __func__, __LINE__, drv->core.name);
  353. BUG();
  354. }
  355. dev_dbg(&dev->core, " <- %s:%d\n", __func__, __LINE__);
  356. }
  357. static int ps3_system_bus_uevent(struct device *_dev, struct kobj_uevent_env *env)
  358. {
  359. struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
  360. int i = 0, length = 0;
  361. if (add_uevent_var(env, "MODALIAS=ps3:%d", dev->match_id))
  362. return -ENOMEM;
  363. return 0;
  364. }
  365. static ssize_t modalias_show(struct device *_dev, struct device_attribute *a,
  366. char *buf)
  367. {
  368. struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
  369. int len = snprintf(buf, PAGE_SIZE, "ps3:%d\n", dev->match_id);
  370. return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len;
  371. }
  372. static struct device_attribute ps3_system_bus_dev_attrs[] = {
  373. __ATTR_RO(modalias),
  374. __ATTR_NULL,
  375. };
  376. struct bus_type ps3_system_bus_type = {
  377. .name = "ps3_system_bus",
  378. .match = ps3_system_bus_match,
  379. .uevent = ps3_system_bus_uevent,
  380. .probe = ps3_system_bus_probe,
  381. .remove = ps3_system_bus_remove,
  382. .shutdown = ps3_system_bus_shutdown,
  383. .dev_attrs = ps3_system_bus_dev_attrs,
  384. };
  385. static int __init ps3_system_bus_init(void)
  386. {
  387. int result;
  388. if (!firmware_has_feature(FW_FEATURE_PS3_LV1))
  389. return -ENODEV;
  390. pr_debug(" -> %s:%d\n", __func__, __LINE__);
  391. mutex_init(&usage_hack.mutex);
  392. result = device_register(&ps3_system_bus);
  393. BUG_ON(result);
  394. result = bus_register(&ps3_system_bus_type);
  395. BUG_ON(result);
  396. pr_debug(" <- %s:%d\n", __func__, __LINE__);
  397. return result;
  398. }
  399. core_initcall(ps3_system_bus_init);
  400. /* Allocates a contiguous real buffer and creates mappings over it.
  401. * Returns the virtual address of the buffer and sets dma_handle
  402. * to the dma address (mapping) of the first page.
  403. */
  404. static void * ps3_alloc_coherent(struct device *_dev, size_t size,
  405. dma_addr_t *dma_handle, gfp_t flag)
  406. {
  407. int result;
  408. struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
  409. unsigned long virt_addr;
  410. flag &= ~(__GFP_DMA | __GFP_HIGHMEM);
  411. flag |= __GFP_ZERO;
  412. virt_addr = __get_free_pages(flag, get_order(size));
  413. if (!virt_addr) {
  414. pr_debug("%s:%d: get_free_pages failed\n", __func__, __LINE__);
  415. goto clean_none;
  416. }
  417. result = ps3_dma_map(dev->d_region, virt_addr, size, dma_handle,
  418. IOPTE_PP_W | IOPTE_PP_R | IOPTE_SO_RW | IOPTE_M);
  419. if (result) {
  420. pr_debug("%s:%d: ps3_dma_map failed (%d)\n",
  421. __func__, __LINE__, result);
  422. BUG_ON("check region type");
  423. goto clean_alloc;
  424. }
  425. return (void*)virt_addr;
  426. clean_alloc:
  427. free_pages(virt_addr, get_order(size));
  428. clean_none:
  429. dma_handle = NULL;
  430. return NULL;
  431. }
  432. static void ps3_free_coherent(struct device *_dev, size_t size, void *vaddr,
  433. dma_addr_t dma_handle)
  434. {
  435. struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
  436. ps3_dma_unmap(dev->d_region, dma_handle, size);
  437. free_pages((unsigned long)vaddr, get_order(size));
  438. }
  439. /* Creates TCEs for a user provided buffer. The user buffer must be
  440. * contiguous real kernel storage (not vmalloc). The address of the buffer
  441. * passed here is the kernel (virtual) address of the buffer. The buffer
  442. * need not be page aligned, the dma_addr_t returned will point to the same
  443. * byte within the page as vaddr.
  444. */
  445. static dma_addr_t ps3_sb_map_single(struct device *_dev, void *ptr, size_t size,
  446. enum dma_data_direction direction)
  447. {
  448. struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
  449. int result;
  450. unsigned long bus_addr;
  451. result = ps3_dma_map(dev->d_region, (unsigned long)ptr, size,
  452. &bus_addr,
  453. IOPTE_PP_R | IOPTE_PP_W | IOPTE_SO_RW | IOPTE_M);
  454. if (result) {
  455. pr_debug("%s:%d: ps3_dma_map failed (%d)\n",
  456. __func__, __LINE__, result);
  457. }
  458. return bus_addr;
  459. }
  460. static dma_addr_t ps3_ioc0_map_single(struct device *_dev, void *ptr,
  461. size_t size,
  462. enum dma_data_direction direction)
  463. {
  464. struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
  465. int result;
  466. unsigned long bus_addr;
  467. u64 iopte_flag;
  468. iopte_flag = IOPTE_M;
  469. switch (direction) {
  470. case DMA_BIDIRECTIONAL:
  471. iopte_flag |= IOPTE_PP_R | IOPTE_PP_W | IOPTE_SO_RW;
  472. break;
  473. case DMA_TO_DEVICE:
  474. iopte_flag |= IOPTE_PP_R | IOPTE_SO_R;
  475. break;
  476. case DMA_FROM_DEVICE:
  477. iopte_flag |= IOPTE_PP_W | IOPTE_SO_RW;
  478. break;
  479. default:
  480. /* not happned */
  481. BUG();
  482. };
  483. result = ps3_dma_map(dev->d_region, (unsigned long)ptr, size,
  484. &bus_addr, iopte_flag);
  485. if (result) {
  486. pr_debug("%s:%d: ps3_dma_map failed (%d)\n",
  487. __func__, __LINE__, result);
  488. }
  489. return bus_addr;
  490. }
  491. static void ps3_unmap_single(struct device *_dev, dma_addr_t dma_addr,
  492. size_t size, enum dma_data_direction direction)
  493. {
  494. struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
  495. int result;
  496. result = ps3_dma_unmap(dev->d_region, dma_addr, size);
  497. if (result) {
  498. pr_debug("%s:%d: ps3_dma_unmap failed (%d)\n",
  499. __func__, __LINE__, result);
  500. }
  501. }
  502. static int ps3_sb_map_sg(struct device *_dev, struct scatterlist *sg, int nents,
  503. enum dma_data_direction direction)
  504. {
  505. #if defined(CONFIG_PS3_DYNAMIC_DMA)
  506. BUG_ON("do");
  507. return -EPERM;
  508. #else
  509. struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
  510. int i;
  511. for (i = 0; i < nents; i++, sg++) {
  512. int result = ps3_dma_map(dev->d_region,
  513. page_to_phys(sg->page) + sg->offset, sg->length,
  514. &sg->dma_address, 0);
  515. if (result) {
  516. pr_debug("%s:%d: ps3_dma_map failed (%d)\n",
  517. __func__, __LINE__, result);
  518. return -EINVAL;
  519. }
  520. sg->dma_length = sg->length;
  521. }
  522. return nents;
  523. #endif
  524. }
  525. static int ps3_ioc0_map_sg(struct device *_dev, struct scatterlist *sg,
  526. int nents,
  527. enum dma_data_direction direction)
  528. {
  529. BUG();
  530. return 0;
  531. }
  532. static void ps3_sb_unmap_sg(struct device *_dev, struct scatterlist *sg,
  533. int nents, enum dma_data_direction direction)
  534. {
  535. #if defined(CONFIG_PS3_DYNAMIC_DMA)
  536. BUG_ON("do");
  537. #endif
  538. }
  539. static void ps3_ioc0_unmap_sg(struct device *_dev, struct scatterlist *sg,
  540. int nents, enum dma_data_direction direction)
  541. {
  542. BUG();
  543. }
  544. static int ps3_dma_supported(struct device *_dev, u64 mask)
  545. {
  546. return mask >= DMA_32BIT_MASK;
  547. }
  548. static struct dma_mapping_ops ps3_sb_dma_ops = {
  549. .alloc_coherent = ps3_alloc_coherent,
  550. .free_coherent = ps3_free_coherent,
  551. .map_single = ps3_sb_map_single,
  552. .unmap_single = ps3_unmap_single,
  553. .map_sg = ps3_sb_map_sg,
  554. .unmap_sg = ps3_sb_unmap_sg,
  555. .dma_supported = ps3_dma_supported
  556. };
  557. static struct dma_mapping_ops ps3_ioc0_dma_ops = {
  558. .alloc_coherent = ps3_alloc_coherent,
  559. .free_coherent = ps3_free_coherent,
  560. .map_single = ps3_ioc0_map_single,
  561. .unmap_single = ps3_unmap_single,
  562. .map_sg = ps3_ioc0_map_sg,
  563. .unmap_sg = ps3_ioc0_unmap_sg,
  564. .dma_supported = ps3_dma_supported
  565. };
  566. /**
  567. * ps3_system_bus_release_device - remove a device from the system bus
  568. */
  569. static void ps3_system_bus_release_device(struct device *_dev)
  570. {
  571. struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
  572. kfree(dev);
  573. }
  574. /**
  575. * ps3_system_bus_device_register - add a device to the system bus
  576. *
  577. * ps3_system_bus_device_register() expects the dev object to be allocated
  578. * dynamically by the caller. The system bus takes ownership of the dev
  579. * object and frees the object in ps3_system_bus_release_device().
  580. */
  581. int ps3_system_bus_device_register(struct ps3_system_bus_device *dev)
  582. {
  583. int result;
  584. static unsigned int dev_ioc0_count;
  585. static unsigned int dev_sb_count;
  586. static unsigned int dev_vuart_count;
  587. if (!dev->core.parent)
  588. dev->core.parent = &ps3_system_bus;
  589. dev->core.bus = &ps3_system_bus_type;
  590. dev->core.release = ps3_system_bus_release_device;
  591. switch (dev->dev_type) {
  592. case PS3_DEVICE_TYPE_IOC0:
  593. dev->core.archdata.dma_ops = &ps3_ioc0_dma_ops;
  594. snprintf(dev->core.bus_id, sizeof(dev->core.bus_id),
  595. "ioc0_%02x", ++dev_ioc0_count);
  596. break;
  597. case PS3_DEVICE_TYPE_SB:
  598. dev->core.archdata.dma_ops = &ps3_sb_dma_ops;
  599. snprintf(dev->core.bus_id, sizeof(dev->core.bus_id),
  600. "sb_%02x", ++dev_sb_count);
  601. break;
  602. case PS3_DEVICE_TYPE_VUART:
  603. snprintf(dev->core.bus_id, sizeof(dev->core.bus_id),
  604. "vuart_%02x", ++dev_vuart_count);
  605. break;
  606. default:
  607. BUG();
  608. };
  609. dev->core.archdata.of_node = NULL;
  610. dev->core.archdata.numa_node = 0;
  611. pr_debug("%s:%d add %s\n", __func__, __LINE__, dev->core.bus_id);
  612. result = device_register(&dev->core);
  613. return result;
  614. }
  615. EXPORT_SYMBOL_GPL(ps3_system_bus_device_register);
  616. int ps3_system_bus_driver_register(struct ps3_system_bus_driver *drv)
  617. {
  618. int result;
  619. pr_debug(" -> %s:%d: %s\n", __func__, __LINE__, drv->core.name);
  620. if (!firmware_has_feature(FW_FEATURE_PS3_LV1))
  621. return -ENODEV;
  622. drv->core.bus = &ps3_system_bus_type;
  623. result = driver_register(&drv->core);
  624. pr_debug(" <- %s:%d: %s\n", __func__, __LINE__, drv->core.name);
  625. return result;
  626. }
  627. EXPORT_SYMBOL_GPL(ps3_system_bus_driver_register);
  628. void ps3_system_bus_driver_unregister(struct ps3_system_bus_driver *drv)
  629. {
  630. pr_debug(" -> %s:%d: %s\n", __func__, __LINE__, drv->core.name);
  631. driver_unregister(&drv->core);
  632. pr_debug(" <- %s:%d: %s\n", __func__, __LINE__, drv->core.name);
  633. }
  634. EXPORT_SYMBOL_GPL(ps3_system_bus_driver_unregister);