system-bus.c 19 KB

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